industrialio-buffer.c 64 KB

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