industrialio-buffer.c 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* The industrial I/O core
  3. *
  4. * Copyright (c) 2008 Jonathan Cameron
  5. *
  6. * Handling of buffer allocation / resizing.
  7. *
  8. * Things to look at here.
  9. * - Better memory allocation techniques?
  10. * - Alternative access techniques?
  11. */
  12. #include <linux/atomic.h>
  13. #include <linux/anon_inodes.h>
  14. #include <linux/cleanup.h>
  15. #include <linux/kernel.h>
  16. #include <linux/export.h>
  17. #include <linux/device.h>
  18. #include <linux/dma-buf.h>
  19. #include <linux/dma-fence.h>
  20. #include <linux/dma-resv.h>
  21. #include <linux/file.h>
  22. #include <linux/fs.h>
  23. #include <linux/cdev.h>
  24. #include <linux/slab.h>
  25. #include <linux/mm.h>
  26. #include <linux/poll.h>
  27. #include <linux/sched/signal.h>
  28. #include <linux/iio/iio.h>
  29. #include <linux/iio/iio-opaque.h>
  30. #include "iio_core.h"
  31. #include "iio_core_trigger.h"
  32. #include <linux/iio/sysfs.h>
  33. #include <linux/iio/buffer.h>
  34. #include <linux/iio/buffer_impl.h>
  35. #define DMABUF_ENQUEUE_TIMEOUT_MS 5000
  36. MODULE_IMPORT_NS("DMA_BUF");
  37. struct iio_dmabuf_priv {
  38. struct list_head entry;
  39. struct kref ref;
  40. struct iio_buffer *buffer;
  41. struct iio_dma_buffer_block *block;
  42. u64 context;
  43. /* Spinlock used for locking the dma_fence */
  44. spinlock_t lock;
  45. struct dma_buf_attachment *attach;
  46. struct sg_table *sgt;
  47. enum dma_data_direction dir;
  48. atomic_t seqno;
  49. };
  50. struct iio_dma_fence {
  51. struct dma_fence base;
  52. struct iio_dmabuf_priv *priv;
  53. struct work_struct work;
  54. };
  55. static const char * const iio_endian_prefix[] = {
  56. [IIO_BE] = "be",
  57. [IIO_LE] = "le",
  58. };
  59. static bool iio_buffer_is_active(struct iio_buffer *buf)
  60. {
  61. return !list_empty(&buf->buffer_list);
  62. }
  63. static size_t iio_buffer_data_available(struct iio_buffer *buf)
  64. {
  65. return buf->access->data_available(buf);
  66. }
  67. static int iio_buffer_flush_hwfifo(struct iio_dev *indio_dev,
  68. struct iio_buffer *buf, size_t required)
  69. {
  70. if (!indio_dev->info->hwfifo_flush_to_buffer)
  71. return -ENODEV;
  72. return indio_dev->info->hwfifo_flush_to_buffer(indio_dev, required);
  73. }
  74. static bool iio_buffer_ready(struct iio_dev *indio_dev, struct iio_buffer *buf,
  75. size_t to_wait, int to_flush)
  76. {
  77. size_t avail;
  78. int flushed = 0;
  79. /* wakeup if the device was unregistered */
  80. if (!indio_dev->info)
  81. return true;
  82. /* drain the buffer if it was disabled */
  83. if (!iio_buffer_is_active(buf)) {
  84. to_wait = min_t(size_t, to_wait, 1);
  85. to_flush = 0;
  86. }
  87. avail = iio_buffer_data_available(buf);
  88. if (avail >= to_wait) {
  89. /* force a flush for non-blocking reads */
  90. if (!to_wait && avail < to_flush)
  91. iio_buffer_flush_hwfifo(indio_dev, buf,
  92. to_flush - avail);
  93. return true;
  94. }
  95. if (to_flush)
  96. flushed = iio_buffer_flush_hwfifo(indio_dev, buf,
  97. to_wait - avail);
  98. if (flushed <= 0)
  99. return false;
  100. if (avail + flushed >= to_wait)
  101. return true;
  102. return false;
  103. }
  104. /**
  105. * iio_buffer_read() - chrdev read for buffer access
  106. * @filp: File structure pointer for the char device
  107. * @buf: Destination buffer for iio buffer read
  108. * @n: First n bytes to read
  109. * @f_ps: Long offset provided by the user as a seek position
  110. *
  111. * This function relies on all buffer implementations having an
  112. * iio_buffer as their first element.
  113. *
  114. * Return: negative values corresponding to error codes or ret != 0
  115. * for ending the reading activity
  116. **/
  117. static ssize_t iio_buffer_read(struct file *filp, char __user *buf,
  118. size_t n, loff_t *f_ps)
  119. {
  120. struct iio_dev_buffer_pair *ib = filp->private_data;
  121. struct iio_buffer *rb = ib->buffer;
  122. struct iio_dev *indio_dev = ib->indio_dev;
  123. DEFINE_WAIT_FUNC(wait, woken_wake_function);
  124. size_t datum_size;
  125. size_t to_wait;
  126. int ret = 0;
  127. if (!indio_dev->info)
  128. return -ENODEV;
  129. if (!rb || !rb->access->read)
  130. return -EINVAL;
  131. if (rb->direction != IIO_BUFFER_DIRECTION_IN)
  132. return -EPERM;
  133. datum_size = rb->bytes_per_datum;
  134. /*
  135. * If datum_size is 0 there will never be anything to read from the
  136. * buffer, so signal end of file now.
  137. */
  138. if (!datum_size)
  139. return 0;
  140. if (filp->f_flags & O_NONBLOCK)
  141. to_wait = 0;
  142. else
  143. to_wait = min_t(size_t, n / datum_size, rb->watermark);
  144. add_wait_queue(&rb->pollq, &wait);
  145. do {
  146. if (!indio_dev->info) {
  147. ret = -ENODEV;
  148. break;
  149. }
  150. if (!iio_buffer_ready(indio_dev, rb, to_wait, n / datum_size)) {
  151. if (signal_pending(current)) {
  152. ret = -ERESTARTSYS;
  153. break;
  154. }
  155. wait_woken(&wait, TASK_INTERRUPTIBLE,
  156. MAX_SCHEDULE_TIMEOUT);
  157. continue;
  158. }
  159. ret = rb->access->read(rb, n, buf);
  160. if (ret == 0 && (filp->f_flags & O_NONBLOCK))
  161. ret = -EAGAIN;
  162. } while (ret == 0);
  163. remove_wait_queue(&rb->pollq, &wait);
  164. return ret;
  165. }
  166. static size_t iio_buffer_space_available(struct iio_buffer *buf)
  167. {
  168. if (buf->access->space_available)
  169. return buf->access->space_available(buf);
  170. return SIZE_MAX;
  171. }
  172. static ssize_t iio_buffer_write(struct file *filp, const char __user *buf,
  173. size_t n, loff_t *f_ps)
  174. {
  175. struct iio_dev_buffer_pair *ib = filp->private_data;
  176. struct iio_buffer *rb = ib->buffer;
  177. struct iio_dev *indio_dev = ib->indio_dev;
  178. DEFINE_WAIT_FUNC(wait, woken_wake_function);
  179. int ret = 0;
  180. size_t written;
  181. if (!indio_dev->info)
  182. return -ENODEV;
  183. if (!rb || !rb->access->write)
  184. return -EINVAL;
  185. if (rb->direction != IIO_BUFFER_DIRECTION_OUT)
  186. return -EPERM;
  187. written = 0;
  188. add_wait_queue(&rb->pollq, &wait);
  189. do {
  190. if (!indio_dev->info) {
  191. ret = -ENODEV;
  192. break;
  193. }
  194. if (!iio_buffer_space_available(rb)) {
  195. if (signal_pending(current)) {
  196. ret = -ERESTARTSYS;
  197. break;
  198. }
  199. if (filp->f_flags & O_NONBLOCK) {
  200. if (!written)
  201. ret = -EAGAIN;
  202. break;
  203. }
  204. wait_woken(&wait, TASK_INTERRUPTIBLE,
  205. MAX_SCHEDULE_TIMEOUT);
  206. continue;
  207. }
  208. ret = rb->access->write(rb, n - written, buf + written);
  209. if (ret < 0)
  210. break;
  211. written += ret;
  212. } while (written != n);
  213. remove_wait_queue(&rb->pollq, &wait);
  214. return ret < 0 ? ret : written;
  215. }
  216. /**
  217. * iio_buffer_poll() - poll the buffer to find out if it has data
  218. * @filp: File structure pointer for device access
  219. * @wait: Poll table structure pointer for which the driver adds
  220. * a wait queue
  221. *
  222. * Return: (EPOLLIN | EPOLLRDNORM) if data is available for reading
  223. * or 0 for other cases
  224. */
  225. static __poll_t iio_buffer_poll(struct file *filp,
  226. struct poll_table_struct *wait)
  227. {
  228. struct iio_dev_buffer_pair *ib = filp->private_data;
  229. struct iio_buffer *rb = ib->buffer;
  230. struct iio_dev *indio_dev = ib->indio_dev;
  231. if (!indio_dev->info || !rb)
  232. return 0;
  233. poll_wait(filp, &rb->pollq, wait);
  234. switch (rb->direction) {
  235. case IIO_BUFFER_DIRECTION_IN:
  236. if (iio_buffer_ready(indio_dev, rb, rb->watermark, 0))
  237. return EPOLLIN | EPOLLRDNORM;
  238. break;
  239. case IIO_BUFFER_DIRECTION_OUT:
  240. if (iio_buffer_space_available(rb))
  241. return EPOLLOUT | EPOLLWRNORM;
  242. break;
  243. }
  244. return 0;
  245. }
  246. ssize_t iio_buffer_read_wrapper(struct file *filp, char __user *buf,
  247. size_t n, loff_t *f_ps)
  248. {
  249. struct iio_dev_buffer_pair *ib = filp->private_data;
  250. struct iio_buffer *rb = ib->buffer;
  251. /* check if buffer was opened through new API */
  252. if (test_bit(IIO_BUSY_BIT_POS, &rb->flags))
  253. return -EBUSY;
  254. return iio_buffer_read(filp, buf, n, f_ps);
  255. }
  256. ssize_t iio_buffer_write_wrapper(struct file *filp, const char __user *buf,
  257. size_t n, loff_t *f_ps)
  258. {
  259. struct iio_dev_buffer_pair *ib = filp->private_data;
  260. struct iio_buffer *rb = ib->buffer;
  261. /* check if buffer was opened through new API */
  262. if (test_bit(IIO_BUSY_BIT_POS, &rb->flags))
  263. return -EBUSY;
  264. return iio_buffer_write(filp, buf, n, f_ps);
  265. }
  266. __poll_t iio_buffer_poll_wrapper(struct file *filp,
  267. struct poll_table_struct *wait)
  268. {
  269. struct iio_dev_buffer_pair *ib = filp->private_data;
  270. struct iio_buffer *rb = ib->buffer;
  271. /* check if buffer was opened through new API */
  272. if (test_bit(IIO_BUSY_BIT_POS, &rb->flags))
  273. return 0;
  274. return iio_buffer_poll(filp, wait);
  275. }
  276. /**
  277. * iio_buffer_wakeup_poll - Wakes up the buffer waitqueue
  278. * @indio_dev: The IIO device
  279. *
  280. * Wakes up the event waitqueue used for poll(). Should usually
  281. * be called when the device is unregistered.
  282. */
  283. void iio_buffer_wakeup_poll(struct iio_dev *indio_dev)
  284. {
  285. struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
  286. struct iio_buffer *buffer;
  287. unsigned int i;
  288. for (i = 0; i < iio_dev_opaque->attached_buffers_cnt; i++) {
  289. buffer = iio_dev_opaque->attached_buffers[i];
  290. wake_up(&buffer->pollq);
  291. }
  292. }
  293. int iio_pop_from_buffer(struct iio_buffer *buffer, void *data)
  294. {
  295. if (!buffer || !buffer->access || !buffer->access->remove_from)
  296. return -EINVAL;
  297. return buffer->access->remove_from(buffer, data);
  298. }
  299. EXPORT_SYMBOL_GPL(iio_pop_from_buffer);
  300. void iio_buffer_init(struct iio_buffer *buffer)
  301. {
  302. INIT_LIST_HEAD(&buffer->demux_list);
  303. INIT_LIST_HEAD(&buffer->buffer_list);
  304. INIT_LIST_HEAD(&buffer->dmabufs);
  305. mutex_init(&buffer->dmabufs_mutex);
  306. init_waitqueue_head(&buffer->pollq);
  307. kref_init(&buffer->ref);
  308. if (!buffer->watermark)
  309. buffer->watermark = 1;
  310. }
  311. EXPORT_SYMBOL(iio_buffer_init);
  312. void iio_device_detach_buffers(struct iio_dev *indio_dev)
  313. {
  314. struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
  315. struct iio_buffer *buffer;
  316. unsigned int i;
  317. for (i = 0; i < iio_dev_opaque->attached_buffers_cnt; i++) {
  318. buffer = iio_dev_opaque->attached_buffers[i];
  319. iio_buffer_put(buffer);
  320. }
  321. kfree(iio_dev_opaque->attached_buffers);
  322. }
  323. static ssize_t iio_show_scan_index(struct device *dev,
  324. struct device_attribute *attr,
  325. char *buf)
  326. {
  327. return sysfs_emit(buf, "%u\n", to_iio_dev_attr(attr)->c->scan_index);
  328. }
  329. static ssize_t iio_show_fixed_type(struct device *dev,
  330. struct device_attribute *attr,
  331. char *buf)
  332. {
  333. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  334. struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
  335. const struct iio_scan_type *scan_type;
  336. u8 type;
  337. scan_type = iio_get_current_scan_type(indio_dev, this_attr->c);
  338. if (IS_ERR(scan_type))
  339. return PTR_ERR(scan_type);
  340. type = scan_type->endianness;
  341. if (type == IIO_CPU) {
  342. #ifdef __LITTLE_ENDIAN
  343. type = IIO_LE;
  344. #else
  345. type = IIO_BE;
  346. #endif
  347. }
  348. if (scan_type->repeat > 1)
  349. return sysfs_emit(buf, "%s:%c%d/%dX%d>>%u\n",
  350. iio_endian_prefix[type],
  351. scan_type->sign,
  352. scan_type->realbits,
  353. scan_type->storagebits,
  354. scan_type->repeat,
  355. scan_type->shift);
  356. else
  357. return sysfs_emit(buf, "%s:%c%d/%d>>%u\n",
  358. iio_endian_prefix[type],
  359. scan_type->sign,
  360. scan_type->realbits,
  361. scan_type->storagebits,
  362. scan_type->shift);
  363. }
  364. static ssize_t iio_scan_el_show(struct device *dev,
  365. struct device_attribute *attr,
  366. char *buf)
  367. {
  368. int ret;
  369. struct iio_buffer *buffer = to_iio_dev_attr(attr)->buffer;
  370. /* Ensure ret is 0 or 1. */
  371. ret = !!test_bit(to_iio_dev_attr(attr)->address,
  372. buffer->scan_mask);
  373. return sysfs_emit(buf, "%d\n", ret);
  374. }
  375. /* Note NULL used as error indicator as it doesn't make sense. */
  376. static const unsigned long *iio_scan_mask_match(const unsigned long *av_masks,
  377. unsigned int masklength,
  378. const unsigned long *mask,
  379. bool strict)
  380. {
  381. if (bitmap_empty(mask, masklength))
  382. return NULL;
  383. /*
  384. * The condition here do not handle multi-long masks correctly.
  385. * It only checks the first long to be zero, and will use such mask
  386. * as a terminator even if there was bits set after the first long.
  387. *
  388. * Correct check would require using:
  389. * while (!bitmap_empty(av_masks, masklength))
  390. * instead. This is potentially hazardous because the
  391. * avaliable_scan_masks is a zero terminated array of longs - and
  392. * using the proper bitmap_empty() check for multi-long wide masks
  393. * would require the array to be terminated with multiple zero longs -
  394. * which is not such an usual pattern.
  395. *
  396. * As writing of this no multi-long wide masks were found in-tree, so
  397. * the simple while (*av_masks) check is working.
  398. */
  399. while (*av_masks) {
  400. if (strict) {
  401. if (bitmap_equal(mask, av_masks, masklength))
  402. return av_masks;
  403. } else {
  404. if (bitmap_subset(mask, av_masks, masklength))
  405. return av_masks;
  406. }
  407. av_masks += BITS_TO_LONGS(masklength);
  408. }
  409. return NULL;
  410. }
  411. static bool iio_validate_scan_mask(struct iio_dev *indio_dev,
  412. const unsigned long *mask)
  413. {
  414. if (!indio_dev->setup_ops->validate_scan_mask)
  415. return true;
  416. return indio_dev->setup_ops->validate_scan_mask(indio_dev, mask);
  417. }
  418. /**
  419. * iio_scan_mask_set() - set particular bit in the scan mask
  420. * @indio_dev: the iio device
  421. * @buffer: the buffer whose scan mask we are interested in
  422. * @bit: the bit to be set.
  423. *
  424. * Note that at this point we have no way of knowing what other
  425. * buffers might request, hence this code only verifies that the
  426. * individual buffers request is plausible.
  427. */
  428. static int iio_scan_mask_set(struct iio_dev *indio_dev,
  429. struct iio_buffer *buffer, int bit)
  430. {
  431. unsigned int masklength = iio_get_masklength(indio_dev);
  432. const unsigned long *mask;
  433. unsigned long *trialmask;
  434. if (!masklength) {
  435. WARN(1, "Trying to set scanmask prior to registering buffer\n");
  436. return -EINVAL;
  437. }
  438. trialmask = bitmap_alloc(masklength, GFP_KERNEL);
  439. if (!trialmask)
  440. return -ENOMEM;
  441. bitmap_copy(trialmask, buffer->scan_mask, masklength);
  442. set_bit(bit, trialmask);
  443. if (!iio_validate_scan_mask(indio_dev, trialmask))
  444. goto err_invalid_mask;
  445. if (indio_dev->available_scan_masks) {
  446. mask = iio_scan_mask_match(indio_dev->available_scan_masks,
  447. masklength, trialmask, false);
  448. if (!mask)
  449. goto err_invalid_mask;
  450. }
  451. bitmap_copy(buffer->scan_mask, trialmask, masklength);
  452. bitmap_free(trialmask);
  453. return 0;
  454. err_invalid_mask:
  455. bitmap_free(trialmask);
  456. return -EINVAL;
  457. }
  458. static int iio_scan_mask_clear(struct iio_buffer *buffer, int bit)
  459. {
  460. clear_bit(bit, buffer->scan_mask);
  461. return 0;
  462. }
  463. static int iio_scan_mask_query(struct iio_dev *indio_dev,
  464. struct iio_buffer *buffer, int bit)
  465. {
  466. if (bit > iio_get_masklength(indio_dev))
  467. return -EINVAL;
  468. if (!buffer->scan_mask)
  469. return 0;
  470. /* Ensure return value is 0 or 1. */
  471. return !!test_bit(bit, buffer->scan_mask);
  472. };
  473. static ssize_t iio_scan_el_store(struct device *dev,
  474. struct device_attribute *attr,
  475. const char *buf,
  476. size_t len)
  477. {
  478. int ret;
  479. bool state;
  480. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  481. struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
  482. struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
  483. struct iio_buffer *buffer = this_attr->buffer;
  484. ret = kstrtobool(buf, &state);
  485. if (ret < 0)
  486. return ret;
  487. guard(mutex)(&iio_dev_opaque->mlock);
  488. if (iio_buffer_is_active(buffer))
  489. return -EBUSY;
  490. ret = iio_scan_mask_query(indio_dev, buffer, this_attr->address);
  491. if (ret < 0)
  492. return ret;
  493. if (state && ret)
  494. return len;
  495. if (state)
  496. ret = iio_scan_mask_set(indio_dev, buffer, this_attr->address);
  497. else
  498. ret = iio_scan_mask_clear(buffer, this_attr->address);
  499. if (ret)
  500. return ret;
  501. return len;
  502. }
  503. static ssize_t iio_scan_el_ts_show(struct device *dev,
  504. struct device_attribute *attr,
  505. char *buf)
  506. {
  507. struct iio_buffer *buffer = to_iio_dev_attr(attr)->buffer;
  508. return sysfs_emit(buf, "%d\n", buffer->scan_timestamp);
  509. }
  510. static ssize_t iio_scan_el_ts_store(struct device *dev,
  511. struct device_attribute *attr,
  512. const char *buf,
  513. size_t len)
  514. {
  515. int ret;
  516. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  517. struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
  518. struct iio_buffer *buffer = to_iio_dev_attr(attr)->buffer;
  519. bool state;
  520. ret = kstrtobool(buf, &state);
  521. if (ret < 0)
  522. return ret;
  523. guard(mutex)(&iio_dev_opaque->mlock);
  524. if (iio_buffer_is_active(buffer))
  525. return -EBUSY;
  526. buffer->scan_timestamp = state;
  527. return len;
  528. }
  529. static int iio_buffer_add_channel_sysfs(struct iio_dev *indio_dev,
  530. struct iio_buffer *buffer,
  531. const struct iio_chan_spec *chan)
  532. {
  533. int ret, attrcount = 0;
  534. ret = __iio_add_chan_devattr("index",
  535. chan,
  536. &iio_show_scan_index,
  537. NULL,
  538. 0,
  539. IIO_SEPARATE,
  540. &indio_dev->dev,
  541. buffer,
  542. &buffer->buffer_attr_list);
  543. if (ret)
  544. return ret;
  545. attrcount++;
  546. ret = __iio_add_chan_devattr("type",
  547. chan,
  548. &iio_show_fixed_type,
  549. NULL,
  550. 0,
  551. IIO_SEPARATE,
  552. &indio_dev->dev,
  553. buffer,
  554. &buffer->buffer_attr_list);
  555. if (ret)
  556. return ret;
  557. attrcount++;
  558. if (chan->type != IIO_TIMESTAMP)
  559. ret = __iio_add_chan_devattr("en",
  560. chan,
  561. &iio_scan_el_show,
  562. &iio_scan_el_store,
  563. chan->scan_index,
  564. IIO_SEPARATE,
  565. &indio_dev->dev,
  566. buffer,
  567. &buffer->buffer_attr_list);
  568. else
  569. ret = __iio_add_chan_devattr("en",
  570. chan,
  571. &iio_scan_el_ts_show,
  572. &iio_scan_el_ts_store,
  573. chan->scan_index,
  574. IIO_SEPARATE,
  575. &indio_dev->dev,
  576. buffer,
  577. &buffer->buffer_attr_list);
  578. if (ret)
  579. return ret;
  580. attrcount++;
  581. ret = attrcount;
  582. return ret;
  583. }
  584. static ssize_t length_show(struct device *dev, struct device_attribute *attr,
  585. char *buf)
  586. {
  587. struct iio_buffer *buffer = to_iio_dev_attr(attr)->buffer;
  588. return sysfs_emit(buf, "%d\n", buffer->length);
  589. }
  590. static ssize_t length_store(struct device *dev, struct device_attribute *attr,
  591. const char *buf, size_t len)
  592. {
  593. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  594. struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
  595. struct iio_buffer *buffer = to_iio_dev_attr(attr)->buffer;
  596. unsigned int val;
  597. int ret;
  598. ret = kstrtouint(buf, 10, &val);
  599. if (ret)
  600. return ret;
  601. if (val == buffer->length)
  602. return len;
  603. guard(mutex)(&iio_dev_opaque->mlock);
  604. if (iio_buffer_is_active(buffer))
  605. return -EBUSY;
  606. buffer->access->set_length(buffer, val);
  607. if (buffer->length && buffer->length < buffer->watermark)
  608. buffer->watermark = buffer->length;
  609. return len;
  610. }
  611. static ssize_t enable_show(struct device *dev, struct device_attribute *attr,
  612. char *buf)
  613. {
  614. struct iio_buffer *buffer = to_iio_dev_attr(attr)->buffer;
  615. return sysfs_emit(buf, "%d\n", iio_buffer_is_active(buffer));
  616. }
  617. static int iio_storage_bytes_for_si(struct iio_dev *indio_dev,
  618. unsigned int scan_index)
  619. {
  620. const struct iio_chan_spec *ch;
  621. const struct iio_scan_type *scan_type;
  622. unsigned int bytes;
  623. ch = iio_find_channel_from_si(indio_dev, scan_index);
  624. scan_type = iio_get_current_scan_type(indio_dev, ch);
  625. if (IS_ERR(scan_type))
  626. return PTR_ERR(scan_type);
  627. bytes = scan_type->storagebits / 8;
  628. if (scan_type->repeat > 1)
  629. bytes *= scan_type->repeat;
  630. return bytes;
  631. }
  632. static int iio_storage_bytes_for_timestamp(struct iio_dev *indio_dev)
  633. {
  634. struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
  635. return iio_storage_bytes_for_si(indio_dev,
  636. iio_dev_opaque->scan_index_timestamp);
  637. }
  638. static int iio_compute_scan_bytes(struct iio_dev *indio_dev,
  639. const unsigned long *mask, bool timestamp)
  640. {
  641. unsigned int bytes = 0;
  642. int length, i, largest = 0;
  643. /* How much space will the demuxed element take? */
  644. for_each_set_bit(i, mask, iio_get_masklength(indio_dev)) {
  645. length = iio_storage_bytes_for_si(indio_dev, i);
  646. if (length < 0)
  647. return length;
  648. bytes = ALIGN(bytes, length);
  649. bytes += length;
  650. largest = max(largest, length);
  651. }
  652. if (timestamp) {
  653. length = iio_storage_bytes_for_timestamp(indio_dev);
  654. if (length < 0)
  655. return length;
  656. bytes = ALIGN(bytes, length);
  657. bytes += length;
  658. largest = max(largest, length);
  659. }
  660. bytes = ALIGN(bytes, largest);
  661. return bytes;
  662. }
  663. static void iio_buffer_activate(struct iio_dev *indio_dev,
  664. struct iio_buffer *buffer)
  665. {
  666. struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
  667. iio_buffer_get(buffer);
  668. list_add(&buffer->buffer_list, &iio_dev_opaque->buffer_list);
  669. }
  670. static void iio_buffer_deactivate(struct iio_buffer *buffer)
  671. {
  672. list_del_init(&buffer->buffer_list);
  673. wake_up_interruptible(&buffer->pollq);
  674. iio_buffer_put(buffer);
  675. }
  676. static void iio_buffer_deactivate_all(struct iio_dev *indio_dev)
  677. {
  678. struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
  679. struct iio_buffer *buffer, *_buffer;
  680. list_for_each_entry_safe(buffer, _buffer,
  681. &iio_dev_opaque->buffer_list, buffer_list)
  682. iio_buffer_deactivate(buffer);
  683. }
  684. static int iio_buffer_enable(struct iio_buffer *buffer,
  685. struct iio_dev *indio_dev)
  686. {
  687. if (!buffer->access->enable)
  688. return 0;
  689. return buffer->access->enable(buffer, indio_dev);
  690. }
  691. static int iio_buffer_disable(struct iio_buffer *buffer,
  692. struct iio_dev *indio_dev)
  693. {
  694. if (!buffer->access->disable)
  695. return 0;
  696. return buffer->access->disable(buffer, indio_dev);
  697. }
  698. static void iio_buffer_update_bytes_per_datum(struct iio_dev *indio_dev,
  699. struct iio_buffer *buffer)
  700. {
  701. unsigned int bytes;
  702. if (!buffer->access->set_bytes_per_datum)
  703. return;
  704. bytes = iio_compute_scan_bytes(indio_dev, buffer->scan_mask,
  705. buffer->scan_timestamp);
  706. buffer->access->set_bytes_per_datum(buffer, bytes);
  707. }
  708. static int iio_buffer_request_update(struct iio_dev *indio_dev,
  709. struct iio_buffer *buffer)
  710. {
  711. int ret;
  712. iio_buffer_update_bytes_per_datum(indio_dev, buffer);
  713. if (buffer->access->request_update) {
  714. ret = buffer->access->request_update(buffer);
  715. if (ret) {
  716. dev_dbg(&indio_dev->dev,
  717. "Buffer not started: buffer parameter update failed (%d)\n",
  718. ret);
  719. return ret;
  720. }
  721. }
  722. return 0;
  723. }
  724. static void iio_free_scan_mask(struct iio_dev *indio_dev,
  725. const unsigned long *mask)
  726. {
  727. /* If the mask is dynamically allocated free it, otherwise do nothing */
  728. if (!indio_dev->available_scan_masks)
  729. bitmap_free(mask);
  730. }
  731. struct iio_device_config {
  732. unsigned int mode;
  733. unsigned int watermark;
  734. const unsigned long *scan_mask;
  735. unsigned int scan_bytes;
  736. bool scan_timestamp;
  737. };
  738. static int iio_verify_update(struct iio_dev *indio_dev,
  739. struct iio_buffer *insert_buffer,
  740. struct iio_buffer *remove_buffer,
  741. struct iio_device_config *config)
  742. {
  743. struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
  744. unsigned int masklength = iio_get_masklength(indio_dev);
  745. unsigned long *compound_mask;
  746. const unsigned long *scan_mask;
  747. bool strict_scanmask = false;
  748. struct iio_buffer *buffer;
  749. bool scan_timestamp;
  750. unsigned int modes;
  751. if (insert_buffer &&
  752. bitmap_empty(insert_buffer->scan_mask, masklength)) {
  753. dev_dbg(&indio_dev->dev,
  754. "At least one scan element must be enabled first\n");
  755. return -EINVAL;
  756. }
  757. memset(config, 0, sizeof(*config));
  758. config->watermark = ~0;
  759. /*
  760. * If there is just one buffer and we are removing it there is nothing
  761. * to verify.
  762. */
  763. if (remove_buffer && !insert_buffer &&
  764. list_is_singular(&iio_dev_opaque->buffer_list))
  765. return 0;
  766. modes = indio_dev->modes;
  767. list_for_each_entry(buffer, &iio_dev_opaque->buffer_list, buffer_list) {
  768. if (buffer == remove_buffer)
  769. continue;
  770. modes &= buffer->access->modes;
  771. config->watermark = min(config->watermark, buffer->watermark);
  772. }
  773. if (insert_buffer) {
  774. modes &= insert_buffer->access->modes;
  775. config->watermark = min(config->watermark,
  776. insert_buffer->watermark);
  777. }
  778. /* Definitely possible for devices to support both of these. */
  779. if ((modes & INDIO_BUFFER_TRIGGERED) && indio_dev->trig) {
  780. config->mode = INDIO_BUFFER_TRIGGERED;
  781. } else if (modes & INDIO_BUFFER_HARDWARE) {
  782. /*
  783. * Keep things simple for now and only allow a single buffer to
  784. * be connected in hardware mode.
  785. */
  786. if (insert_buffer && !list_empty(&iio_dev_opaque->buffer_list))
  787. return -EINVAL;
  788. config->mode = INDIO_BUFFER_HARDWARE;
  789. strict_scanmask = true;
  790. } else if (modes & INDIO_BUFFER_SOFTWARE) {
  791. config->mode = INDIO_BUFFER_SOFTWARE;
  792. } else {
  793. /* Can only occur on first buffer */
  794. if (indio_dev->modes & INDIO_BUFFER_TRIGGERED)
  795. dev_dbg(&indio_dev->dev, "Buffer not started: no trigger\n");
  796. return -EINVAL;
  797. }
  798. /* What scan mask do we actually have? */
  799. compound_mask = bitmap_zalloc(masklength, GFP_KERNEL);
  800. if (!compound_mask)
  801. return -ENOMEM;
  802. scan_timestamp = false;
  803. list_for_each_entry(buffer, &iio_dev_opaque->buffer_list, buffer_list) {
  804. if (buffer == remove_buffer)
  805. continue;
  806. bitmap_or(compound_mask, compound_mask, buffer->scan_mask,
  807. masklength);
  808. scan_timestamp |= buffer->scan_timestamp;
  809. }
  810. if (insert_buffer) {
  811. bitmap_or(compound_mask, compound_mask,
  812. insert_buffer->scan_mask, masklength);
  813. scan_timestamp |= insert_buffer->scan_timestamp;
  814. }
  815. if (indio_dev->available_scan_masks) {
  816. scan_mask = iio_scan_mask_match(indio_dev->available_scan_masks,
  817. masklength, compound_mask,
  818. strict_scanmask);
  819. bitmap_free(compound_mask);
  820. if (!scan_mask)
  821. return -EINVAL;
  822. } else {
  823. scan_mask = compound_mask;
  824. }
  825. config->scan_bytes = iio_compute_scan_bytes(indio_dev,
  826. scan_mask, scan_timestamp);
  827. config->scan_mask = scan_mask;
  828. config->scan_timestamp = scan_timestamp;
  829. return 0;
  830. }
  831. /**
  832. * struct iio_demux_table - table describing demux memcpy ops
  833. * @from: index to copy from
  834. * @to: index to copy to
  835. * @length: how many bytes to copy
  836. * @l: list head used for management
  837. */
  838. struct iio_demux_table {
  839. unsigned int from;
  840. unsigned int to;
  841. unsigned int length;
  842. struct list_head l;
  843. };
  844. static void iio_buffer_demux_free(struct iio_buffer *buffer)
  845. {
  846. struct iio_demux_table *p, *q;
  847. list_for_each_entry_safe(p, q, &buffer->demux_list, l) {
  848. list_del(&p->l);
  849. kfree(p);
  850. }
  851. }
  852. static int iio_buffer_add_demux(struct iio_buffer *buffer,
  853. struct iio_demux_table **p, unsigned int in_loc,
  854. unsigned int out_loc,
  855. unsigned int length)
  856. {
  857. if (*p && (*p)->from + (*p)->length == in_loc &&
  858. (*p)->to + (*p)->length == out_loc) {
  859. (*p)->length += length;
  860. } else {
  861. *p = kmalloc_obj(**p);
  862. if (!(*p))
  863. return -ENOMEM;
  864. (*p)->from = in_loc;
  865. (*p)->to = out_loc;
  866. (*p)->length = length;
  867. list_add_tail(&(*p)->l, &buffer->demux_list);
  868. }
  869. return 0;
  870. }
  871. static int iio_buffer_update_demux(struct iio_dev *indio_dev,
  872. struct iio_buffer *buffer)
  873. {
  874. unsigned int masklength = iio_get_masklength(indio_dev);
  875. int ret, in_ind = -1, out_ind, length;
  876. unsigned int in_loc = 0, out_loc = 0;
  877. struct iio_demux_table *p = NULL;
  878. /* Clear out any old demux */
  879. iio_buffer_demux_free(buffer);
  880. kfree(buffer->demux_bounce);
  881. buffer->demux_bounce = NULL;
  882. /* First work out which scan mode we will actually have */
  883. if (bitmap_equal(indio_dev->active_scan_mask,
  884. buffer->scan_mask, masklength))
  885. return 0;
  886. /* Now we have the two masks, work from least sig and build up sizes */
  887. for_each_set_bit(out_ind, buffer->scan_mask, masklength) {
  888. in_ind = find_next_bit(indio_dev->active_scan_mask,
  889. masklength, in_ind + 1);
  890. while (in_ind != out_ind) {
  891. ret = iio_storage_bytes_for_si(indio_dev, in_ind);
  892. if (ret < 0)
  893. goto error_clear_mux_table;
  894. length = ret;
  895. /* Make sure we are aligned */
  896. in_loc = roundup(in_loc, length) + length;
  897. in_ind = find_next_bit(indio_dev->active_scan_mask,
  898. masklength, in_ind + 1);
  899. }
  900. ret = iio_storage_bytes_for_si(indio_dev, in_ind);
  901. if (ret < 0)
  902. goto error_clear_mux_table;
  903. length = ret;
  904. out_loc = roundup(out_loc, length);
  905. in_loc = roundup(in_loc, length);
  906. ret = iio_buffer_add_demux(buffer, &p, in_loc, out_loc, length);
  907. if (ret)
  908. goto error_clear_mux_table;
  909. out_loc += length;
  910. in_loc += length;
  911. }
  912. /* Relies on scan_timestamp being last */
  913. if (buffer->scan_timestamp) {
  914. ret = iio_storage_bytes_for_timestamp(indio_dev);
  915. if (ret < 0)
  916. goto error_clear_mux_table;
  917. length = ret;
  918. out_loc = roundup(out_loc, length);
  919. in_loc = roundup(in_loc, length);
  920. ret = iio_buffer_add_demux(buffer, &p, in_loc, out_loc, length);
  921. if (ret)
  922. goto error_clear_mux_table;
  923. out_loc += length;
  924. }
  925. buffer->demux_bounce = kzalloc(out_loc, GFP_KERNEL);
  926. if (!buffer->demux_bounce) {
  927. ret = -ENOMEM;
  928. goto error_clear_mux_table;
  929. }
  930. return 0;
  931. error_clear_mux_table:
  932. iio_buffer_demux_free(buffer);
  933. return ret;
  934. }
  935. static int iio_update_demux(struct iio_dev *indio_dev)
  936. {
  937. struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
  938. struct iio_buffer *buffer;
  939. int ret;
  940. list_for_each_entry(buffer, &iio_dev_opaque->buffer_list, buffer_list) {
  941. ret = iio_buffer_update_demux(indio_dev, buffer);
  942. if (ret < 0)
  943. goto error_clear_mux_table;
  944. }
  945. return 0;
  946. error_clear_mux_table:
  947. list_for_each_entry(buffer, &iio_dev_opaque->buffer_list, buffer_list)
  948. iio_buffer_demux_free(buffer);
  949. return ret;
  950. }
  951. static int iio_enable_buffers(struct iio_dev *indio_dev,
  952. struct iio_device_config *config)
  953. {
  954. struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
  955. struct iio_buffer *buffer, *tmp = NULL;
  956. int ret;
  957. indio_dev->active_scan_mask = config->scan_mask;
  958. ACCESS_PRIVATE(indio_dev, scan_timestamp) = config->scan_timestamp;
  959. indio_dev->scan_bytes = config->scan_bytes;
  960. iio_dev_opaque->currentmode = config->mode;
  961. iio_update_demux(indio_dev);
  962. /* Wind up again */
  963. if (indio_dev->setup_ops->preenable) {
  964. ret = indio_dev->setup_ops->preenable(indio_dev);
  965. if (ret) {
  966. dev_dbg(&indio_dev->dev,
  967. "Buffer not started: buffer preenable failed (%d)\n", ret);
  968. goto err_undo_config;
  969. }
  970. }
  971. if (indio_dev->info->update_scan_mode) {
  972. ret = indio_dev->info
  973. ->update_scan_mode(indio_dev,
  974. indio_dev->active_scan_mask);
  975. if (ret < 0) {
  976. dev_dbg(&indio_dev->dev,
  977. "Buffer not started: update scan mode failed (%d)\n",
  978. ret);
  979. goto err_run_postdisable;
  980. }
  981. }
  982. if (indio_dev->info->hwfifo_set_watermark)
  983. indio_dev->info->hwfifo_set_watermark(indio_dev,
  984. config->watermark);
  985. list_for_each_entry(buffer, &iio_dev_opaque->buffer_list, buffer_list) {
  986. ret = iio_buffer_enable(buffer, indio_dev);
  987. if (ret) {
  988. tmp = buffer;
  989. goto err_disable_buffers;
  990. }
  991. }
  992. if (iio_dev_opaque->currentmode == INDIO_BUFFER_TRIGGERED) {
  993. ret = iio_trigger_attach_poll_func(indio_dev->trig,
  994. indio_dev->pollfunc);
  995. if (ret)
  996. goto err_disable_buffers;
  997. }
  998. if (indio_dev->setup_ops->postenable) {
  999. ret = indio_dev->setup_ops->postenable(indio_dev);
  1000. if (ret) {
  1001. dev_dbg(&indio_dev->dev,
  1002. "Buffer not started: postenable failed (%d)\n", ret);
  1003. goto err_detach_pollfunc;
  1004. }
  1005. }
  1006. return 0;
  1007. err_detach_pollfunc:
  1008. if (iio_dev_opaque->currentmode == INDIO_BUFFER_TRIGGERED) {
  1009. iio_trigger_detach_poll_func(indio_dev->trig,
  1010. indio_dev->pollfunc);
  1011. }
  1012. err_disable_buffers:
  1013. buffer = list_prepare_entry(tmp, &iio_dev_opaque->buffer_list, buffer_list);
  1014. list_for_each_entry_continue_reverse(buffer, &iio_dev_opaque->buffer_list,
  1015. buffer_list)
  1016. iio_buffer_disable(buffer, indio_dev);
  1017. err_run_postdisable:
  1018. if (indio_dev->setup_ops->postdisable)
  1019. indio_dev->setup_ops->postdisable(indio_dev);
  1020. err_undo_config:
  1021. iio_dev_opaque->currentmode = INDIO_DIRECT_MODE;
  1022. indio_dev->active_scan_mask = NULL;
  1023. return ret;
  1024. }
  1025. static int iio_disable_buffers(struct iio_dev *indio_dev)
  1026. {
  1027. struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
  1028. struct iio_buffer *buffer;
  1029. int ret = 0;
  1030. int ret2;
  1031. /* Wind down existing buffers - iff there are any */
  1032. if (list_empty(&iio_dev_opaque->buffer_list))
  1033. return 0;
  1034. /*
  1035. * If things go wrong at some step in disable we still need to continue
  1036. * to perform the other steps, otherwise we leave the device in a
  1037. * inconsistent state. We return the error code for the first error we
  1038. * encountered.
  1039. */
  1040. if (indio_dev->setup_ops->predisable) {
  1041. ret2 = indio_dev->setup_ops->predisable(indio_dev);
  1042. if (ret2 && !ret)
  1043. ret = ret2;
  1044. }
  1045. if (iio_dev_opaque->currentmode == INDIO_BUFFER_TRIGGERED) {
  1046. iio_trigger_detach_poll_func(indio_dev->trig,
  1047. indio_dev->pollfunc);
  1048. }
  1049. list_for_each_entry(buffer, &iio_dev_opaque->buffer_list, buffer_list) {
  1050. ret2 = iio_buffer_disable(buffer, indio_dev);
  1051. if (ret2 && !ret)
  1052. ret = ret2;
  1053. }
  1054. if (indio_dev->setup_ops->postdisable) {
  1055. ret2 = indio_dev->setup_ops->postdisable(indio_dev);
  1056. if (ret2 && !ret)
  1057. ret = ret2;
  1058. }
  1059. iio_free_scan_mask(indio_dev, indio_dev->active_scan_mask);
  1060. indio_dev->active_scan_mask = NULL;
  1061. iio_dev_opaque->currentmode = INDIO_DIRECT_MODE;
  1062. return ret;
  1063. }
  1064. static int __iio_update_buffers(struct iio_dev *indio_dev,
  1065. struct iio_buffer *insert_buffer,
  1066. struct iio_buffer *remove_buffer)
  1067. {
  1068. struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
  1069. struct iio_device_config new_config;
  1070. int ret;
  1071. ret = iio_verify_update(indio_dev, insert_buffer, remove_buffer,
  1072. &new_config);
  1073. if (ret)
  1074. return ret;
  1075. if (insert_buffer) {
  1076. ret = iio_buffer_request_update(indio_dev, insert_buffer);
  1077. if (ret)
  1078. goto err_free_config;
  1079. }
  1080. ret = iio_disable_buffers(indio_dev);
  1081. if (ret)
  1082. goto err_deactivate_all;
  1083. if (remove_buffer)
  1084. iio_buffer_deactivate(remove_buffer);
  1085. if (insert_buffer)
  1086. iio_buffer_activate(indio_dev, insert_buffer);
  1087. /* If no buffers in list, we are done */
  1088. if (list_empty(&iio_dev_opaque->buffer_list))
  1089. return 0;
  1090. ret = iio_enable_buffers(indio_dev, &new_config);
  1091. if (ret)
  1092. goto err_deactivate_all;
  1093. return 0;
  1094. err_deactivate_all:
  1095. /*
  1096. * We've already verified that the config is valid earlier. If things go
  1097. * wrong in either enable or disable the most likely reason is an IO
  1098. * error from the device. In this case there is no good recovery
  1099. * strategy. Just make sure to disable everything and leave the device
  1100. * in a sane state. With a bit of luck the device might come back to
  1101. * life again later and userspace can try again.
  1102. */
  1103. iio_buffer_deactivate_all(indio_dev);
  1104. err_free_config:
  1105. iio_free_scan_mask(indio_dev, new_config.scan_mask);
  1106. return ret;
  1107. }
  1108. int iio_update_buffers(struct iio_dev *indio_dev,
  1109. struct iio_buffer *insert_buffer,
  1110. struct iio_buffer *remove_buffer)
  1111. {
  1112. struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
  1113. if (insert_buffer == remove_buffer)
  1114. return 0;
  1115. if (insert_buffer &&
  1116. insert_buffer->direction == IIO_BUFFER_DIRECTION_OUT)
  1117. return -EINVAL;
  1118. guard(mutex)(&iio_dev_opaque->info_exist_lock);
  1119. guard(mutex)(&iio_dev_opaque->mlock);
  1120. if (insert_buffer && iio_buffer_is_active(insert_buffer))
  1121. insert_buffer = NULL;
  1122. if (remove_buffer && !iio_buffer_is_active(remove_buffer))
  1123. remove_buffer = NULL;
  1124. if (!insert_buffer && !remove_buffer)
  1125. return 0;
  1126. if (!indio_dev->info)
  1127. return -ENODEV;
  1128. return __iio_update_buffers(indio_dev, insert_buffer, remove_buffer);
  1129. }
  1130. EXPORT_SYMBOL_GPL(iio_update_buffers);
  1131. void iio_disable_all_buffers(struct iio_dev *indio_dev)
  1132. {
  1133. iio_disable_buffers(indio_dev);
  1134. iio_buffer_deactivate_all(indio_dev);
  1135. }
  1136. static ssize_t enable_store(struct device *dev, struct device_attribute *attr,
  1137. const char *buf, size_t len)
  1138. {
  1139. int ret;
  1140. bool requested_state;
  1141. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  1142. struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
  1143. struct iio_buffer *buffer = to_iio_dev_attr(attr)->buffer;
  1144. bool inlist;
  1145. ret = kstrtobool(buf, &requested_state);
  1146. if (ret < 0)
  1147. return ret;
  1148. guard(mutex)(&iio_dev_opaque->mlock);
  1149. /* Find out if it is in the list */
  1150. inlist = iio_buffer_is_active(buffer);
  1151. /* Already in desired state */
  1152. if (inlist == requested_state)
  1153. return len;
  1154. if (requested_state)
  1155. ret = __iio_update_buffers(indio_dev, buffer, NULL);
  1156. else
  1157. ret = __iio_update_buffers(indio_dev, NULL, buffer);
  1158. if (ret)
  1159. return ret;
  1160. return len;
  1161. }
  1162. static ssize_t watermark_show(struct device *dev, struct device_attribute *attr,
  1163. char *buf)
  1164. {
  1165. struct iio_buffer *buffer = to_iio_dev_attr(attr)->buffer;
  1166. return sysfs_emit(buf, "%u\n", buffer->watermark);
  1167. }
  1168. static ssize_t watermark_store(struct device *dev,
  1169. struct device_attribute *attr,
  1170. const char *buf, size_t len)
  1171. {
  1172. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  1173. struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
  1174. struct iio_buffer *buffer = to_iio_dev_attr(attr)->buffer;
  1175. unsigned int val;
  1176. int ret;
  1177. ret = kstrtouint(buf, 10, &val);
  1178. if (ret)
  1179. return ret;
  1180. if (!val)
  1181. return -EINVAL;
  1182. guard(mutex)(&iio_dev_opaque->mlock);
  1183. if (val > buffer->length)
  1184. return -EINVAL;
  1185. if (iio_buffer_is_active(buffer))
  1186. return -EBUSY;
  1187. buffer->watermark = val;
  1188. return len;
  1189. }
  1190. static ssize_t data_available_show(struct device *dev,
  1191. struct device_attribute *attr, char *buf)
  1192. {
  1193. struct iio_buffer *buffer = to_iio_dev_attr(attr)->buffer;
  1194. return sysfs_emit(buf, "%zu\n", iio_buffer_data_available(buffer));
  1195. }
  1196. static ssize_t direction_show(struct device *dev,
  1197. struct device_attribute *attr,
  1198. char *buf)
  1199. {
  1200. struct iio_buffer *buffer = to_iio_dev_attr(attr)->buffer;
  1201. switch (buffer->direction) {
  1202. case IIO_BUFFER_DIRECTION_IN:
  1203. return sysfs_emit(buf, "in\n");
  1204. case IIO_BUFFER_DIRECTION_OUT:
  1205. return sysfs_emit(buf, "out\n");
  1206. default:
  1207. return -EINVAL;
  1208. }
  1209. }
  1210. static DEVICE_ATTR_RW(length);
  1211. static struct device_attribute dev_attr_length_ro = __ATTR_RO(length);
  1212. static DEVICE_ATTR_RW(enable);
  1213. static DEVICE_ATTR_RW(watermark);
  1214. static struct device_attribute dev_attr_watermark_ro = __ATTR_RO(watermark);
  1215. static DEVICE_ATTR_RO(data_available);
  1216. static DEVICE_ATTR_RO(direction);
  1217. /*
  1218. * When adding new attributes here, put the at the end, at least until
  1219. * the code that handles the length/length_ro & watermark/watermark_ro
  1220. * assignments gets cleaned up. Otherwise these can create some weird
  1221. * duplicate attributes errors under some setups.
  1222. */
  1223. static struct attribute *iio_buffer_attrs[] = {
  1224. &dev_attr_length.attr,
  1225. &dev_attr_enable.attr,
  1226. &dev_attr_watermark.attr,
  1227. &dev_attr_data_available.attr,
  1228. &dev_attr_direction.attr,
  1229. };
  1230. #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
  1231. static struct attribute *iio_buffer_wrap_attr(struct iio_buffer *buffer,
  1232. struct attribute *attr)
  1233. {
  1234. struct device_attribute *dattr = to_dev_attr(attr);
  1235. struct iio_dev_attr *iio_attr;
  1236. iio_attr = kzalloc_obj(*iio_attr);
  1237. if (!iio_attr)
  1238. return NULL;
  1239. iio_attr->buffer = buffer;
  1240. memcpy(&iio_attr->dev_attr, dattr, sizeof(iio_attr->dev_attr));
  1241. iio_attr->dev_attr.attr.name = kstrdup_const(attr->name, GFP_KERNEL);
  1242. if (!iio_attr->dev_attr.attr.name) {
  1243. kfree(iio_attr);
  1244. return NULL;
  1245. }
  1246. sysfs_attr_init(&iio_attr->dev_attr.attr);
  1247. list_add(&iio_attr->l, &buffer->buffer_attr_list);
  1248. return &iio_attr->dev_attr.attr;
  1249. }
  1250. static int iio_buffer_register_legacy_sysfs_groups(struct iio_dev *indio_dev,
  1251. struct attribute **buffer_attrs,
  1252. int buffer_attrcount,
  1253. int scan_el_attrcount)
  1254. {
  1255. struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
  1256. struct attribute_group *group;
  1257. struct attribute **attrs;
  1258. int ret;
  1259. attrs = kzalloc_objs(*attrs, buffer_attrcount + 1);
  1260. if (!attrs)
  1261. return -ENOMEM;
  1262. memcpy(attrs, buffer_attrs, buffer_attrcount * sizeof(*attrs));
  1263. group = &iio_dev_opaque->legacy_buffer_group;
  1264. group->attrs = attrs;
  1265. group->name = "buffer";
  1266. ret = iio_device_register_sysfs_group(indio_dev, group);
  1267. if (ret)
  1268. goto error_free_buffer_attrs;
  1269. attrs = kzalloc_objs(*attrs, scan_el_attrcount + 1);
  1270. if (!attrs) {
  1271. ret = -ENOMEM;
  1272. goto error_free_buffer_attrs;
  1273. }
  1274. memcpy(attrs, &buffer_attrs[buffer_attrcount],
  1275. scan_el_attrcount * sizeof(*attrs));
  1276. group = &iio_dev_opaque->legacy_scan_el_group;
  1277. group->attrs = attrs;
  1278. group->name = "scan_elements";
  1279. ret = iio_device_register_sysfs_group(indio_dev, group);
  1280. if (ret)
  1281. goto error_free_scan_el_attrs;
  1282. return 0;
  1283. error_free_scan_el_attrs:
  1284. kfree(iio_dev_opaque->legacy_scan_el_group.attrs);
  1285. error_free_buffer_attrs:
  1286. kfree(iio_dev_opaque->legacy_buffer_group.attrs);
  1287. return ret;
  1288. }
  1289. static void iio_buffer_unregister_legacy_sysfs_groups(struct iio_dev *indio_dev)
  1290. {
  1291. struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
  1292. kfree(iio_dev_opaque->legacy_buffer_group.attrs);
  1293. kfree(iio_dev_opaque->legacy_scan_el_group.attrs);
  1294. }
  1295. static void iio_buffer_dmabuf_release(struct kref *ref)
  1296. {
  1297. struct iio_dmabuf_priv *priv = container_of(ref, struct iio_dmabuf_priv, ref);
  1298. struct dma_buf_attachment *attach = priv->attach;
  1299. struct iio_buffer *buffer = priv->buffer;
  1300. struct dma_buf *dmabuf = attach->dmabuf;
  1301. dma_buf_unmap_attachment_unlocked(attach, priv->sgt, priv->dir);
  1302. buffer->access->detach_dmabuf(buffer, priv->block);
  1303. dma_buf_detach(attach->dmabuf, attach);
  1304. dma_buf_put(dmabuf);
  1305. kfree(priv);
  1306. }
  1307. static void iio_buffer_dmabuf_get(struct dma_buf_attachment *attach)
  1308. {
  1309. struct iio_dmabuf_priv *priv = attach->importer_priv;
  1310. kref_get(&priv->ref);
  1311. }
  1312. static void iio_buffer_dmabuf_put(struct dma_buf_attachment *attach)
  1313. {
  1314. struct iio_dmabuf_priv *priv = attach->importer_priv;
  1315. kref_put(&priv->ref, iio_buffer_dmabuf_release);
  1316. }
  1317. static int iio_buffer_chrdev_release(struct inode *inode, struct file *filep)
  1318. {
  1319. struct iio_dev_buffer_pair *ib = filep->private_data;
  1320. struct iio_dev *indio_dev = ib->indio_dev;
  1321. struct iio_buffer *buffer = ib->buffer;
  1322. struct iio_dmabuf_priv *priv, *tmp;
  1323. wake_up(&buffer->pollq);
  1324. guard(mutex)(&buffer->dmabufs_mutex);
  1325. /* Close all attached DMABUFs */
  1326. list_for_each_entry_safe(priv, tmp, &buffer->dmabufs, entry) {
  1327. list_del_init(&priv->entry);
  1328. iio_buffer_dmabuf_put(priv->attach);
  1329. }
  1330. kfree(ib);
  1331. clear_bit(IIO_BUSY_BIT_POS, &buffer->flags);
  1332. iio_device_put(indio_dev);
  1333. return 0;
  1334. }
  1335. static int iio_dma_resv_lock(struct dma_buf *dmabuf, bool nonblock)
  1336. {
  1337. if (!nonblock)
  1338. return dma_resv_lock_interruptible(dmabuf->resv, NULL);
  1339. if (!dma_resv_trylock(dmabuf->resv))
  1340. return -EBUSY;
  1341. return 0;
  1342. }
  1343. static struct device *iio_buffer_get_dma_dev(const struct iio_dev *indio_dev,
  1344. struct iio_buffer *buffer)
  1345. {
  1346. if (buffer->access->get_dma_dev)
  1347. return buffer->access->get_dma_dev(buffer);
  1348. return indio_dev->dev.parent;
  1349. }
  1350. static struct dma_buf_attachment *
  1351. iio_buffer_find_attachment(struct iio_dev_buffer_pair *ib,
  1352. struct dma_buf *dmabuf, bool nonblock)
  1353. {
  1354. struct iio_buffer *buffer = ib->buffer;
  1355. struct device *dma_dev = iio_buffer_get_dma_dev(ib->indio_dev, buffer);
  1356. struct dma_buf_attachment *attach = NULL;
  1357. struct iio_dmabuf_priv *priv;
  1358. guard(mutex)(&buffer->dmabufs_mutex);
  1359. list_for_each_entry(priv, &buffer->dmabufs, entry) {
  1360. if (priv->attach->dev == dma_dev
  1361. && priv->attach->dmabuf == dmabuf) {
  1362. attach = priv->attach;
  1363. break;
  1364. }
  1365. }
  1366. if (attach)
  1367. iio_buffer_dmabuf_get(attach);
  1368. return attach ?: ERR_PTR(-EPERM);
  1369. }
  1370. static int iio_buffer_attach_dmabuf(struct iio_dev_buffer_pair *ib,
  1371. int __user *user_fd, bool nonblock)
  1372. {
  1373. struct iio_dev *indio_dev = ib->indio_dev;
  1374. struct iio_buffer *buffer = ib->buffer;
  1375. struct device *dma_dev = iio_buffer_get_dma_dev(indio_dev, buffer);
  1376. struct dma_buf_attachment *attach;
  1377. struct iio_dmabuf_priv *priv, *each;
  1378. struct dma_buf *dmabuf;
  1379. int err, fd;
  1380. if (!buffer->access->attach_dmabuf
  1381. || !buffer->access->detach_dmabuf
  1382. || !buffer->access->enqueue_dmabuf)
  1383. return -EPERM;
  1384. if (copy_from_user(&fd, user_fd, sizeof(fd)))
  1385. return -EFAULT;
  1386. priv = kzalloc_obj(*priv);
  1387. if (!priv)
  1388. return -ENOMEM;
  1389. spin_lock_init(&priv->lock);
  1390. priv->context = dma_fence_context_alloc(1);
  1391. dmabuf = dma_buf_get(fd);
  1392. if (IS_ERR(dmabuf)) {
  1393. err = PTR_ERR(dmabuf);
  1394. goto err_free_priv;
  1395. }
  1396. attach = dma_buf_attach(dmabuf, dma_dev);
  1397. if (IS_ERR(attach)) {
  1398. err = PTR_ERR(attach);
  1399. goto err_dmabuf_put;
  1400. }
  1401. err = iio_dma_resv_lock(dmabuf, nonblock);
  1402. if (err)
  1403. goto err_dmabuf_detach;
  1404. priv->dir = buffer->direction == IIO_BUFFER_DIRECTION_IN
  1405. ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
  1406. priv->sgt = dma_buf_map_attachment(attach, priv->dir);
  1407. if (IS_ERR(priv->sgt)) {
  1408. err = PTR_ERR(priv->sgt);
  1409. dev_err(&indio_dev->dev, "Unable to map attachment: %d\n", err);
  1410. goto err_resv_unlock;
  1411. }
  1412. kref_init(&priv->ref);
  1413. priv->buffer = buffer;
  1414. priv->attach = attach;
  1415. attach->importer_priv = priv;
  1416. priv->block = buffer->access->attach_dmabuf(buffer, attach);
  1417. if (IS_ERR(priv->block)) {
  1418. err = PTR_ERR(priv->block);
  1419. goto err_dmabuf_unmap_attachment;
  1420. }
  1421. dma_resv_unlock(dmabuf->resv);
  1422. mutex_lock(&buffer->dmabufs_mutex);
  1423. /*
  1424. * Check whether we already have an attachment for this driver/DMABUF
  1425. * combo. If we do, refuse to attach.
  1426. */
  1427. list_for_each_entry(each, &buffer->dmabufs, entry) {
  1428. if (each->attach->dev == dma_dev
  1429. && each->attach->dmabuf == dmabuf) {
  1430. /*
  1431. * We unlocked the reservation object, so going through
  1432. * the cleanup code would mean re-locking it first.
  1433. * At this stage it is simpler to free the attachment
  1434. * using iio_buffer_dma_put().
  1435. */
  1436. mutex_unlock(&buffer->dmabufs_mutex);
  1437. iio_buffer_dmabuf_put(attach);
  1438. return -EBUSY;
  1439. }
  1440. }
  1441. /* Otherwise, add the new attachment to our dmabufs list. */
  1442. list_add(&priv->entry, &buffer->dmabufs);
  1443. mutex_unlock(&buffer->dmabufs_mutex);
  1444. return 0;
  1445. err_dmabuf_unmap_attachment:
  1446. dma_buf_unmap_attachment(attach, priv->sgt, priv->dir);
  1447. err_resv_unlock:
  1448. dma_resv_unlock(dmabuf->resv);
  1449. err_dmabuf_detach:
  1450. dma_buf_detach(dmabuf, attach);
  1451. err_dmabuf_put:
  1452. dma_buf_put(dmabuf);
  1453. err_free_priv:
  1454. kfree(priv);
  1455. return err;
  1456. }
  1457. static int iio_buffer_detach_dmabuf(struct iio_dev_buffer_pair *ib,
  1458. int __user *user_req, bool nonblock)
  1459. {
  1460. struct iio_buffer *buffer = ib->buffer;
  1461. struct iio_dev *indio_dev = ib->indio_dev;
  1462. struct device *dma_dev = iio_buffer_get_dma_dev(indio_dev, buffer);
  1463. struct iio_dmabuf_priv *priv;
  1464. struct dma_buf *dmabuf;
  1465. int dmabuf_fd, ret = -EPERM;
  1466. if (copy_from_user(&dmabuf_fd, user_req, sizeof(dmabuf_fd)))
  1467. return -EFAULT;
  1468. dmabuf = dma_buf_get(dmabuf_fd);
  1469. if (IS_ERR(dmabuf))
  1470. return PTR_ERR(dmabuf);
  1471. guard(mutex)(&buffer->dmabufs_mutex);
  1472. list_for_each_entry(priv, &buffer->dmabufs, entry) {
  1473. if (priv->attach->dev == dma_dev
  1474. && priv->attach->dmabuf == dmabuf) {
  1475. list_del(&priv->entry);
  1476. /* Unref the reference from iio_buffer_attach_dmabuf() */
  1477. iio_buffer_dmabuf_put(priv->attach);
  1478. ret = 0;
  1479. break;
  1480. }
  1481. }
  1482. dma_buf_put(dmabuf);
  1483. return ret;
  1484. }
  1485. static const char *
  1486. iio_buffer_dma_fence_get_driver_name(struct dma_fence *fence)
  1487. {
  1488. return "iio";
  1489. }
  1490. static void iio_buffer_dma_fence_release(struct dma_fence *fence)
  1491. {
  1492. struct iio_dma_fence *iio_fence =
  1493. container_of(fence, struct iio_dma_fence, base);
  1494. kfree(iio_fence);
  1495. }
  1496. static const struct dma_fence_ops iio_buffer_dma_fence_ops = {
  1497. .get_driver_name = iio_buffer_dma_fence_get_driver_name,
  1498. .get_timeline_name = iio_buffer_dma_fence_get_driver_name,
  1499. .release = iio_buffer_dma_fence_release,
  1500. };
  1501. static int iio_buffer_enqueue_dmabuf(struct iio_dev_buffer_pair *ib,
  1502. struct iio_dmabuf __user *iio_dmabuf_req,
  1503. bool nonblock)
  1504. {
  1505. struct iio_buffer *buffer = ib->buffer;
  1506. struct iio_dmabuf iio_dmabuf;
  1507. struct dma_buf_attachment *attach;
  1508. struct iio_dmabuf_priv *priv;
  1509. struct iio_dma_fence *fence;
  1510. struct dma_buf *dmabuf;
  1511. unsigned long timeout;
  1512. bool cookie, cyclic, dma_to_ram;
  1513. long retl;
  1514. u32 seqno;
  1515. int ret;
  1516. if (copy_from_user(&iio_dmabuf, iio_dmabuf_req, sizeof(iio_dmabuf)))
  1517. return -EFAULT;
  1518. if (iio_dmabuf.flags & ~IIO_BUFFER_DMABUF_SUPPORTED_FLAGS)
  1519. return -EINVAL;
  1520. cyclic = iio_dmabuf.flags & IIO_BUFFER_DMABUF_CYCLIC;
  1521. /* Cyclic flag is only supported on output buffers */
  1522. if (cyclic && buffer->direction != IIO_BUFFER_DIRECTION_OUT)
  1523. return -EINVAL;
  1524. dmabuf = dma_buf_get(iio_dmabuf.fd);
  1525. if (IS_ERR(dmabuf))
  1526. return PTR_ERR(dmabuf);
  1527. if (!iio_dmabuf.bytes_used || iio_dmabuf.bytes_used > dmabuf->size) {
  1528. ret = -EINVAL;
  1529. goto err_dmabuf_put;
  1530. }
  1531. attach = iio_buffer_find_attachment(ib, dmabuf, nonblock);
  1532. if (IS_ERR(attach)) {
  1533. ret = PTR_ERR(attach);
  1534. goto err_dmabuf_put;
  1535. }
  1536. priv = attach->importer_priv;
  1537. fence = kmalloc_obj(*fence);
  1538. if (!fence) {
  1539. ret = -ENOMEM;
  1540. goto err_attachment_put;
  1541. }
  1542. fence->priv = priv;
  1543. seqno = atomic_add_return(1, &priv->seqno);
  1544. /*
  1545. * The transfers are guaranteed to be processed in the order they are
  1546. * enqueued, so we can use a simple incrementing sequence number for
  1547. * the dma_fence.
  1548. */
  1549. dma_fence_init(&fence->base, &iio_buffer_dma_fence_ops,
  1550. &priv->lock, priv->context, seqno);
  1551. ret = iio_dma_resv_lock(dmabuf, nonblock);
  1552. if (ret)
  1553. goto err_fence_put;
  1554. timeout = nonblock ? 0 : msecs_to_jiffies(DMABUF_ENQUEUE_TIMEOUT_MS);
  1555. dma_to_ram = buffer->direction == IIO_BUFFER_DIRECTION_IN;
  1556. /* Make sure we don't have writers */
  1557. retl = dma_resv_wait_timeout(dmabuf->resv,
  1558. dma_resv_usage_rw(dma_to_ram),
  1559. true, timeout);
  1560. if (retl == 0)
  1561. retl = -EBUSY;
  1562. if (retl < 0) {
  1563. ret = (int)retl;
  1564. goto err_resv_unlock;
  1565. }
  1566. if (buffer->access->lock_queue)
  1567. buffer->access->lock_queue(buffer);
  1568. ret = dma_resv_reserve_fences(dmabuf->resv, 1);
  1569. if (ret)
  1570. goto err_queue_unlock;
  1571. dma_resv_add_fence(dmabuf->resv, &fence->base,
  1572. dma_to_ram ? DMA_RESV_USAGE_WRITE : DMA_RESV_USAGE_READ);
  1573. dma_resv_unlock(dmabuf->resv);
  1574. cookie = dma_fence_begin_signalling();
  1575. ret = buffer->access->enqueue_dmabuf(buffer, priv->block, &fence->base,
  1576. priv->sgt, iio_dmabuf.bytes_used,
  1577. cyclic);
  1578. if (ret) {
  1579. /*
  1580. * DMABUF enqueue failed, but we already added the fence.
  1581. * Signal the error through the fence completion mechanism.
  1582. */
  1583. iio_buffer_signal_dmabuf_done(&fence->base, ret);
  1584. }
  1585. if (buffer->access->unlock_queue)
  1586. buffer->access->unlock_queue(buffer);
  1587. dma_fence_end_signalling(cookie);
  1588. dma_buf_put(dmabuf);
  1589. return ret;
  1590. err_queue_unlock:
  1591. if (buffer->access->unlock_queue)
  1592. buffer->access->unlock_queue(buffer);
  1593. err_resv_unlock:
  1594. dma_resv_unlock(dmabuf->resv);
  1595. err_fence_put:
  1596. dma_fence_put(&fence->base);
  1597. err_attachment_put:
  1598. iio_buffer_dmabuf_put(attach);
  1599. err_dmabuf_put:
  1600. dma_buf_put(dmabuf);
  1601. return ret;
  1602. }
  1603. static void iio_buffer_cleanup(struct work_struct *work)
  1604. {
  1605. struct iio_dma_fence *fence =
  1606. container_of(work, struct iio_dma_fence, work);
  1607. struct iio_dmabuf_priv *priv = fence->priv;
  1608. struct dma_buf_attachment *attach = priv->attach;
  1609. dma_fence_put(&fence->base);
  1610. iio_buffer_dmabuf_put(attach);
  1611. }
  1612. void iio_buffer_signal_dmabuf_done(struct dma_fence *fence, int ret)
  1613. {
  1614. struct iio_dma_fence *iio_fence =
  1615. container_of(fence, struct iio_dma_fence, base);
  1616. bool cookie = dma_fence_begin_signalling();
  1617. /*
  1618. * Get a reference to the fence, so that it's not freed as soon as
  1619. * it's signaled.
  1620. */
  1621. dma_fence_get(fence);
  1622. fence->error = ret;
  1623. dma_fence_signal(fence);
  1624. dma_fence_end_signalling(cookie);
  1625. /*
  1626. * The fence will be unref'd in iio_buffer_cleanup.
  1627. * It can't be done here, as the unref functions might try to lock the
  1628. * resv object, which can deadlock.
  1629. */
  1630. INIT_WORK(&iio_fence->work, iio_buffer_cleanup);
  1631. schedule_work(&iio_fence->work);
  1632. }
  1633. EXPORT_SYMBOL_GPL(iio_buffer_signal_dmabuf_done);
  1634. static long iio_buffer_chrdev_ioctl(struct file *filp,
  1635. unsigned int cmd, unsigned long arg)
  1636. {
  1637. struct iio_dev_buffer_pair *ib = filp->private_data;
  1638. void __user *_arg = (void __user *)arg;
  1639. bool nonblock = filp->f_flags & O_NONBLOCK;
  1640. switch (cmd) {
  1641. case IIO_BUFFER_DMABUF_ATTACH_IOCTL:
  1642. return iio_buffer_attach_dmabuf(ib, _arg, nonblock);
  1643. case IIO_BUFFER_DMABUF_DETACH_IOCTL:
  1644. return iio_buffer_detach_dmabuf(ib, _arg, nonblock);
  1645. case IIO_BUFFER_DMABUF_ENQUEUE_IOCTL:
  1646. return iio_buffer_enqueue_dmabuf(ib, _arg, nonblock);
  1647. default:
  1648. return -EINVAL;
  1649. }
  1650. }
  1651. static const struct file_operations iio_buffer_chrdev_fileops = {
  1652. .owner = THIS_MODULE,
  1653. .llseek = noop_llseek,
  1654. .read = iio_buffer_read,
  1655. .write = iio_buffer_write,
  1656. .unlocked_ioctl = iio_buffer_chrdev_ioctl,
  1657. .compat_ioctl = compat_ptr_ioctl,
  1658. .poll = iio_buffer_poll,
  1659. .release = iio_buffer_chrdev_release,
  1660. };
  1661. static long iio_device_buffer_getfd(struct iio_dev *indio_dev, unsigned long arg)
  1662. {
  1663. struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
  1664. int __user *ival = (int __user *)arg;
  1665. struct iio_dev_buffer_pair *ib;
  1666. struct iio_buffer *buffer;
  1667. int fd, idx, ret;
  1668. if (copy_from_user(&idx, ival, sizeof(idx)))
  1669. return -EFAULT;
  1670. if (idx >= iio_dev_opaque->attached_buffers_cnt)
  1671. return -ENODEV;
  1672. iio_device_get(indio_dev);
  1673. buffer = iio_dev_opaque->attached_buffers[idx];
  1674. if (test_and_set_bit(IIO_BUSY_BIT_POS, &buffer->flags)) {
  1675. ret = -EBUSY;
  1676. goto error_iio_dev_put;
  1677. }
  1678. ib = kzalloc_obj(*ib);
  1679. if (!ib) {
  1680. ret = -ENOMEM;
  1681. goto error_clear_busy_bit;
  1682. }
  1683. ib->indio_dev = indio_dev;
  1684. ib->buffer = buffer;
  1685. fd = anon_inode_getfd("iio:buffer", &iio_buffer_chrdev_fileops,
  1686. ib, O_RDWR | O_CLOEXEC);
  1687. if (fd < 0) {
  1688. ret = fd;
  1689. goto error_free_ib;
  1690. }
  1691. if (copy_to_user(ival, &fd, sizeof(fd))) {
  1692. /*
  1693. * "Leak" the fd, as there's not much we can do about this
  1694. * anyway. 'fd' might have been closed already, as
  1695. * anon_inode_getfd() called fd_install() on it, which made
  1696. * it reachable by userland.
  1697. *
  1698. * Instead of allowing a malicious user to play tricks with
  1699. * us, rely on the process exit path to do any necessary
  1700. * cleanup, as in releasing the file, if still needed.
  1701. */
  1702. return -EFAULT;
  1703. }
  1704. return 0;
  1705. error_free_ib:
  1706. kfree(ib);
  1707. error_clear_busy_bit:
  1708. clear_bit(IIO_BUSY_BIT_POS, &buffer->flags);
  1709. error_iio_dev_put:
  1710. iio_device_put(indio_dev);
  1711. return ret;
  1712. }
  1713. static long iio_device_buffer_ioctl(struct iio_dev *indio_dev, struct file *filp,
  1714. unsigned int cmd, unsigned long arg)
  1715. {
  1716. switch (cmd) {
  1717. case IIO_BUFFER_GET_FD_IOCTL:
  1718. return iio_device_buffer_getfd(indio_dev, arg);
  1719. default:
  1720. return IIO_IOCTL_UNHANDLED;
  1721. }
  1722. }
  1723. static int iio_channel_validate_scan_type(struct device *dev, int ch,
  1724. const struct iio_scan_type *scan_type)
  1725. {
  1726. /* Verify that sample bits fit into storage */
  1727. if (scan_type->storagebits < scan_type->realbits + scan_type->shift) {
  1728. dev_err(dev,
  1729. "Channel %d storagebits (%d) < shifted realbits (%d + %d)\n",
  1730. ch, scan_type->storagebits,
  1731. scan_type->realbits,
  1732. scan_type->shift);
  1733. return -EINVAL;
  1734. }
  1735. return 0;
  1736. }
  1737. static int __iio_buffer_alloc_sysfs_and_mask(struct iio_buffer *buffer,
  1738. struct iio_dev *indio_dev,
  1739. int index)
  1740. {
  1741. struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
  1742. unsigned int masklength = iio_get_masklength(indio_dev);
  1743. struct iio_dev_attr *p;
  1744. const struct iio_dev_attr *id_attr;
  1745. struct attribute **attr;
  1746. int ret, i, attrn, scan_el_attrcount, buffer_attrcount;
  1747. const struct iio_chan_spec *channels;
  1748. buffer_attrcount = 0;
  1749. if (buffer->attrs) {
  1750. while (buffer->attrs[buffer_attrcount])
  1751. buffer_attrcount++;
  1752. }
  1753. buffer_attrcount += ARRAY_SIZE(iio_buffer_attrs);
  1754. scan_el_attrcount = 0;
  1755. INIT_LIST_HEAD(&buffer->buffer_attr_list);
  1756. channels = indio_dev->channels;
  1757. if (channels) {
  1758. /* new magic */
  1759. for (i = 0; i < indio_dev->num_channels; i++) {
  1760. const struct iio_scan_type *scan_type;
  1761. if (channels[i].scan_index < 0)
  1762. continue;
  1763. if (channels[i].has_ext_scan_type) {
  1764. int j;
  1765. /*
  1766. * get_current_scan_type is required when using
  1767. * extended scan types.
  1768. */
  1769. if (!indio_dev->info->get_current_scan_type) {
  1770. ret = -EINVAL;
  1771. goto error_cleanup_dynamic;
  1772. }
  1773. for (j = 0; j < channels[i].num_ext_scan_type; j++) {
  1774. scan_type = &channels[i].ext_scan_type[j];
  1775. ret = iio_channel_validate_scan_type(
  1776. &indio_dev->dev, i, scan_type);
  1777. if (ret)
  1778. goto error_cleanup_dynamic;
  1779. }
  1780. } else {
  1781. scan_type = &channels[i].scan_type;
  1782. ret = iio_channel_validate_scan_type(
  1783. &indio_dev->dev, i, scan_type);
  1784. if (ret)
  1785. goto error_cleanup_dynamic;
  1786. }
  1787. ret = iio_buffer_add_channel_sysfs(indio_dev, buffer,
  1788. &channels[i]);
  1789. if (ret < 0)
  1790. goto error_cleanup_dynamic;
  1791. scan_el_attrcount += ret;
  1792. if (channels[i].type == IIO_TIMESTAMP)
  1793. iio_dev_opaque->scan_index_timestamp =
  1794. channels[i].scan_index;
  1795. }
  1796. if (masklength && !buffer->scan_mask) {
  1797. buffer->scan_mask = bitmap_zalloc(masklength,
  1798. GFP_KERNEL);
  1799. if (!buffer->scan_mask) {
  1800. ret = -ENOMEM;
  1801. goto error_cleanup_dynamic;
  1802. }
  1803. }
  1804. }
  1805. attrn = buffer_attrcount + scan_el_attrcount;
  1806. attr = kzalloc_objs(*attr, attrn + 1);
  1807. if (!attr) {
  1808. ret = -ENOMEM;
  1809. goto error_free_scan_mask;
  1810. }
  1811. memcpy(attr, iio_buffer_attrs, sizeof(iio_buffer_attrs));
  1812. if (!buffer->access->set_length)
  1813. attr[0] = &dev_attr_length_ro.attr;
  1814. if (buffer->access->flags & INDIO_BUFFER_FLAG_FIXED_WATERMARK)
  1815. attr[2] = &dev_attr_watermark_ro.attr;
  1816. if (buffer->attrs)
  1817. for (i = 0, id_attr = buffer->attrs[i];
  1818. (id_attr = buffer->attrs[i]); i++)
  1819. attr[ARRAY_SIZE(iio_buffer_attrs) + i] =
  1820. (struct attribute *)&id_attr->dev_attr.attr;
  1821. buffer->buffer_group.attrs = attr;
  1822. for (i = 0; i < buffer_attrcount; i++) {
  1823. struct attribute *wrapped;
  1824. wrapped = iio_buffer_wrap_attr(buffer, attr[i]);
  1825. if (!wrapped) {
  1826. ret = -ENOMEM;
  1827. goto error_free_buffer_attrs;
  1828. }
  1829. attr[i] = wrapped;
  1830. }
  1831. attrn = 0;
  1832. list_for_each_entry(p, &buffer->buffer_attr_list, l)
  1833. attr[attrn++] = &p->dev_attr.attr;
  1834. buffer->buffer_group.name = kasprintf(GFP_KERNEL, "buffer%d", index);
  1835. if (!buffer->buffer_group.name) {
  1836. ret = -ENOMEM;
  1837. goto error_free_buffer_attrs;
  1838. }
  1839. ret = iio_device_register_sysfs_group(indio_dev, &buffer->buffer_group);
  1840. if (ret)
  1841. goto error_free_buffer_attr_group_name;
  1842. /* we only need to register the legacy groups for the first buffer */
  1843. if (index > 0)
  1844. return 0;
  1845. ret = iio_buffer_register_legacy_sysfs_groups(indio_dev, attr,
  1846. buffer_attrcount,
  1847. scan_el_attrcount);
  1848. if (ret)
  1849. goto error_free_buffer_attr_group_name;
  1850. return 0;
  1851. error_free_buffer_attr_group_name:
  1852. kfree(buffer->buffer_group.name);
  1853. error_free_buffer_attrs:
  1854. kfree(buffer->buffer_group.attrs);
  1855. error_free_scan_mask:
  1856. bitmap_free(buffer->scan_mask);
  1857. error_cleanup_dynamic:
  1858. iio_free_chan_devattr_list(&buffer->buffer_attr_list);
  1859. return ret;
  1860. }
  1861. static void __iio_buffer_free_sysfs_and_mask(struct iio_buffer *buffer,
  1862. struct iio_dev *indio_dev,
  1863. int index)
  1864. {
  1865. if (index == 0)
  1866. iio_buffer_unregister_legacy_sysfs_groups(indio_dev);
  1867. bitmap_free(buffer->scan_mask);
  1868. kfree(buffer->buffer_group.name);
  1869. kfree(buffer->buffer_group.attrs);
  1870. iio_free_chan_devattr_list(&buffer->buffer_attr_list);
  1871. }
  1872. int iio_buffers_alloc_sysfs_and_mask(struct iio_dev *indio_dev)
  1873. {
  1874. struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
  1875. const struct iio_chan_spec *channels;
  1876. struct iio_buffer *buffer;
  1877. int ret, i, idx;
  1878. size_t sz;
  1879. channels = indio_dev->channels;
  1880. if (channels) {
  1881. int ml = 0;
  1882. for (i = 0; i < indio_dev->num_channels; i++)
  1883. ml = max(ml, channels[i].scan_index + 1);
  1884. ACCESS_PRIVATE(indio_dev, masklength) = ml;
  1885. }
  1886. if (!iio_dev_opaque->attached_buffers_cnt)
  1887. return 0;
  1888. for (idx = 0; idx < iio_dev_opaque->attached_buffers_cnt; idx++) {
  1889. buffer = iio_dev_opaque->attached_buffers[idx];
  1890. ret = __iio_buffer_alloc_sysfs_and_mask(buffer, indio_dev, idx);
  1891. if (ret)
  1892. goto error_unwind_sysfs_and_mask;
  1893. }
  1894. sz = sizeof(*iio_dev_opaque->buffer_ioctl_handler);
  1895. iio_dev_opaque->buffer_ioctl_handler = kzalloc(sz, GFP_KERNEL);
  1896. if (!iio_dev_opaque->buffer_ioctl_handler) {
  1897. ret = -ENOMEM;
  1898. goto error_unwind_sysfs_and_mask;
  1899. }
  1900. iio_dev_opaque->buffer_ioctl_handler->ioctl = iio_device_buffer_ioctl;
  1901. iio_device_ioctl_handler_register(indio_dev,
  1902. iio_dev_opaque->buffer_ioctl_handler);
  1903. return 0;
  1904. error_unwind_sysfs_and_mask:
  1905. while (idx--) {
  1906. buffer = iio_dev_opaque->attached_buffers[idx];
  1907. __iio_buffer_free_sysfs_and_mask(buffer, indio_dev, idx);
  1908. }
  1909. return ret;
  1910. }
  1911. void iio_buffers_free_sysfs_and_mask(struct iio_dev *indio_dev)
  1912. {
  1913. struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
  1914. struct iio_buffer *buffer;
  1915. int i;
  1916. if (!iio_dev_opaque->attached_buffers_cnt)
  1917. return;
  1918. iio_device_ioctl_handler_unregister(iio_dev_opaque->buffer_ioctl_handler);
  1919. kfree(iio_dev_opaque->buffer_ioctl_handler);
  1920. for (i = iio_dev_opaque->attached_buffers_cnt - 1; i >= 0; i--) {
  1921. buffer = iio_dev_opaque->attached_buffers[i];
  1922. __iio_buffer_free_sysfs_and_mask(buffer, indio_dev, i);
  1923. }
  1924. }
  1925. /**
  1926. * iio_validate_scan_mask_onehot() - Validates that exactly one channel is selected
  1927. * @indio_dev: the iio device
  1928. * @mask: scan mask to be checked
  1929. *
  1930. * Return true if exactly one bit is set in the scan mask, false otherwise. It
  1931. * can be used for devices where only one channel can be active for sampling at
  1932. * a time.
  1933. */
  1934. bool iio_validate_scan_mask_onehot(struct iio_dev *indio_dev,
  1935. const unsigned long *mask)
  1936. {
  1937. return bitmap_weight(mask, iio_get_masklength(indio_dev)) == 1;
  1938. }
  1939. EXPORT_SYMBOL_GPL(iio_validate_scan_mask_onehot);
  1940. static const void *iio_demux(struct iio_buffer *buffer,
  1941. const void *datain)
  1942. {
  1943. struct iio_demux_table *t;
  1944. if (list_empty(&buffer->demux_list))
  1945. return datain;
  1946. list_for_each_entry(t, &buffer->demux_list, l)
  1947. memcpy(buffer->demux_bounce + t->to,
  1948. datain + t->from, t->length);
  1949. return buffer->demux_bounce;
  1950. }
  1951. static int iio_push_to_buffer(struct iio_buffer *buffer, const void *data)
  1952. {
  1953. const void *dataout = iio_demux(buffer, data);
  1954. int ret;
  1955. ret = buffer->access->store_to(buffer, dataout);
  1956. if (ret)
  1957. return ret;
  1958. /*
  1959. * We can't just test for watermark to decide if we wake the poll queue
  1960. * because read may request less samples than the watermark.
  1961. */
  1962. wake_up_interruptible_poll(&buffer->pollq, EPOLLIN | EPOLLRDNORM);
  1963. return 0;
  1964. }
  1965. /**
  1966. * iio_push_to_buffers() - push to a registered buffer.
  1967. * @indio_dev: iio_dev structure for device.
  1968. * @data: Full scan.
  1969. *
  1970. * Context: Any context.
  1971. * Return: 0 on success, negative error code on failure.
  1972. */
  1973. int iio_push_to_buffers(struct iio_dev *indio_dev, const void *data)
  1974. {
  1975. struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
  1976. int ret;
  1977. struct iio_buffer *buf;
  1978. list_for_each_entry(buf, &iio_dev_opaque->buffer_list, buffer_list) {
  1979. ret = iio_push_to_buffer(buf, data);
  1980. if (ret < 0)
  1981. return ret;
  1982. }
  1983. return 0;
  1984. }
  1985. EXPORT_SYMBOL_GPL(iio_push_to_buffers);
  1986. /**
  1987. * iio_push_to_buffers_with_ts_unaligned() - push to registered buffer,
  1988. * no alignment or space requirements.
  1989. * @indio_dev: iio_dev structure for device.
  1990. * @data: channel data excluding the timestamp.
  1991. * @data_sz: size of data.
  1992. * @timestamp: timestamp for the sample data.
  1993. *
  1994. * This special variant of iio_push_to_buffers_with_timestamp() does
  1995. * not require space for the timestamp, or 8 byte alignment of data.
  1996. * It does however require an allocation on first call and additional
  1997. * copies on all calls, so should be avoided if possible.
  1998. *
  1999. * Context: May sleep.
  2000. * Return: 0 on success, negative error code on failure.
  2001. */
  2002. int iio_push_to_buffers_with_ts_unaligned(struct iio_dev *indio_dev,
  2003. const void *data,
  2004. size_t data_sz,
  2005. int64_t timestamp)
  2006. {
  2007. struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
  2008. might_sleep();
  2009. /*
  2010. * Conservative estimate - we can always safely copy the minimum
  2011. * of either the data provided or the length of the destination buffer.
  2012. * This relaxed limit allows the calling drivers to be lax about
  2013. * tracking the size of the data they are pushing, at the cost of
  2014. * unnecessary copying of padding.
  2015. */
  2016. data_sz = min_t(size_t, indio_dev->scan_bytes, data_sz);
  2017. if (iio_dev_opaque->bounce_buffer_size != indio_dev->scan_bytes) {
  2018. void *bb;
  2019. bb = devm_krealloc(&indio_dev->dev,
  2020. iio_dev_opaque->bounce_buffer,
  2021. indio_dev->scan_bytes, GFP_KERNEL);
  2022. if (!bb)
  2023. return -ENOMEM;
  2024. iio_dev_opaque->bounce_buffer = bb;
  2025. iio_dev_opaque->bounce_buffer_size = indio_dev->scan_bytes;
  2026. }
  2027. memcpy(iio_dev_opaque->bounce_buffer, data, data_sz);
  2028. return iio_push_to_buffers_with_timestamp(indio_dev,
  2029. iio_dev_opaque->bounce_buffer,
  2030. timestamp);
  2031. }
  2032. EXPORT_SYMBOL_GPL(iio_push_to_buffers_with_ts_unaligned);
  2033. /**
  2034. * iio_buffer_release() - Free a buffer's resources
  2035. * @ref: Pointer to the kref embedded in the iio_buffer struct
  2036. *
  2037. * This function is called when the last reference to the buffer has been
  2038. * dropped. It will typically free all resources allocated by the buffer. Do not
  2039. * call this function manually, always use iio_buffer_put() when done using a
  2040. * buffer.
  2041. */
  2042. static void iio_buffer_release(struct kref *ref)
  2043. {
  2044. struct iio_buffer *buffer = container_of(ref, struct iio_buffer, ref);
  2045. mutex_destroy(&buffer->dmabufs_mutex);
  2046. buffer->access->release(buffer);
  2047. }
  2048. /**
  2049. * iio_buffer_get() - Grab a reference to the buffer
  2050. * @buffer: The buffer to grab a reference for, may be NULL
  2051. *
  2052. * Returns the pointer to the buffer that was passed into the function.
  2053. */
  2054. struct iio_buffer *iio_buffer_get(struct iio_buffer *buffer)
  2055. {
  2056. if (buffer)
  2057. kref_get(&buffer->ref);
  2058. return buffer;
  2059. }
  2060. EXPORT_SYMBOL_GPL(iio_buffer_get);
  2061. /**
  2062. * iio_buffer_put() - Release the reference to the buffer
  2063. * @buffer: The buffer to release the reference for, may be NULL
  2064. */
  2065. void iio_buffer_put(struct iio_buffer *buffer)
  2066. {
  2067. if (buffer)
  2068. kref_put(&buffer->ref, iio_buffer_release);
  2069. }
  2070. EXPORT_SYMBOL_GPL(iio_buffer_put);
  2071. /**
  2072. * iio_device_attach_buffer - Attach a buffer to a IIO device
  2073. * @indio_dev: The device the buffer should be attached to
  2074. * @buffer: The buffer to attach to the device
  2075. *
  2076. * Return 0 if successful, negative if error.
  2077. *
  2078. * This function attaches a buffer to a IIO device. The buffer stays attached to
  2079. * the device until the device is freed. For legacy reasons, the first attached
  2080. * buffer will also be assigned to 'indio_dev->buffer'.
  2081. * The array allocated here, will be free'd via the iio_device_detach_buffers()
  2082. * call which is handled by the iio_device_free().
  2083. */
  2084. int iio_device_attach_buffer(struct iio_dev *indio_dev,
  2085. struct iio_buffer *buffer)
  2086. {
  2087. struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
  2088. struct iio_buffer **new, **old = iio_dev_opaque->attached_buffers;
  2089. unsigned int cnt = iio_dev_opaque->attached_buffers_cnt;
  2090. cnt++;
  2091. new = krealloc(old, sizeof(*new) * cnt, GFP_KERNEL);
  2092. if (!new)
  2093. return -ENOMEM;
  2094. iio_dev_opaque->attached_buffers = new;
  2095. buffer = iio_buffer_get(buffer);
  2096. /* first buffer is legacy; attach it to the IIO device directly */
  2097. if (!indio_dev->buffer)
  2098. indio_dev->buffer = buffer;
  2099. iio_dev_opaque->attached_buffers[cnt - 1] = buffer;
  2100. iio_dev_opaque->attached_buffers_cnt = cnt;
  2101. return 0;
  2102. }
  2103. EXPORT_SYMBOL_GPL(iio_device_attach_buffer);