industrialio-buffer.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396
  1. /* The industrial I/O core
  2. *
  3. * Copyright (c) 2008 Jonathan Cameron
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 as published by
  7. * the Free Software Foundation.
  8. *
  9. * Handling of buffer allocation / resizing.
  10. *
  11. *
  12. * Things to look at here.
  13. * - Better memory allocation techniques?
  14. * - Alternative access techniques?
  15. */
  16. #include <linux/kernel.h>
  17. #include <linux/export.h>
  18. #include <linux/device.h>
  19. #include <linux/fs.h>
  20. #include <linux/cdev.h>
  21. #include <linux/slab.h>
  22. #include <linux/poll.h>
  23. #include <linux/sched.h>
  24. #include <linux/iio/iio.h>
  25. #include "iio_core.h"
  26. #include <linux/iio/sysfs.h>
  27. #include <linux/iio/buffer.h>
  28. static const char * const iio_endian_prefix[] = {
  29. [IIO_BE] = "be",
  30. [IIO_LE] = "le",
  31. };
  32. static bool iio_buffer_is_active(struct iio_buffer *buf)
  33. {
  34. return !list_empty(&buf->buffer_list);
  35. }
  36. static size_t iio_buffer_data_available(struct iio_buffer *buf)
  37. {
  38. return buf->access->data_available(buf);
  39. }
  40. static int iio_buffer_flush_hwfifo(struct iio_dev *indio_dev,
  41. struct iio_buffer *buf, size_t required)
  42. {
  43. if (!indio_dev->info->hwfifo_flush_to_buffer)
  44. return -ENODEV;
  45. return indio_dev->info->hwfifo_flush_to_buffer(indio_dev, required);
  46. }
  47. static bool iio_buffer_ready(struct iio_dev *indio_dev, struct iio_buffer *buf,
  48. size_t to_wait, int to_flush)
  49. {
  50. size_t avail;
  51. int flushed = 0;
  52. /* wakeup if the device was unregistered */
  53. if (!indio_dev->info)
  54. return true;
  55. /* drain the buffer if it was disabled */
  56. if (!iio_buffer_is_active(buf)) {
  57. to_wait = min_t(size_t, to_wait, 1);
  58. to_flush = 0;
  59. }
  60. avail = iio_buffer_data_available(buf);
  61. if (avail >= to_wait) {
  62. /* force a flush for non-blocking reads */
  63. if (!to_wait && avail < to_flush)
  64. iio_buffer_flush_hwfifo(indio_dev, buf,
  65. to_flush - avail);
  66. return true;
  67. }
  68. if (to_flush)
  69. flushed = iio_buffer_flush_hwfifo(indio_dev, buf,
  70. to_wait - avail);
  71. if (flushed <= 0)
  72. return false;
  73. if (avail + flushed >= to_wait)
  74. return true;
  75. return false;
  76. }
  77. /**
  78. * iio_buffer_read_first_n_outer() - chrdev read for buffer access
  79. * @filp: File structure pointer for the char device
  80. * @buf: Destination buffer for iio buffer read
  81. * @n: First n bytes to read
  82. * @f_ps: Long offset provided by the user as a seek position
  83. *
  84. * This function relies on all buffer implementations having an
  85. * iio_buffer as their first element.
  86. *
  87. * Return: negative values corresponding to error codes or ret != 0
  88. * for ending the reading activity
  89. **/
  90. ssize_t iio_buffer_read_first_n_outer(struct file *filp, char __user *buf,
  91. size_t n, loff_t *f_ps)
  92. {
  93. struct iio_dev *indio_dev = filp->private_data;
  94. struct iio_buffer *rb = indio_dev->buffer;
  95. DEFINE_WAIT_FUNC(wait, woken_wake_function);
  96. size_t datum_size;
  97. size_t to_wait;
  98. int ret = 0;
  99. if (!indio_dev->info)
  100. return -ENODEV;
  101. if (!rb || !rb->access->read_first_n)
  102. return -EINVAL;
  103. datum_size = rb->bytes_per_datum;
  104. /*
  105. * If datum_size is 0 there will never be anything to read from the
  106. * buffer, so signal end of file now.
  107. */
  108. if (!datum_size)
  109. return 0;
  110. if (filp->f_flags & O_NONBLOCK)
  111. to_wait = 0;
  112. else
  113. to_wait = min_t(size_t, n / datum_size, rb->watermark);
  114. add_wait_queue(&rb->pollq, &wait);
  115. do {
  116. if (!indio_dev->info) {
  117. ret = -ENODEV;
  118. break;
  119. }
  120. if (!iio_buffer_ready(indio_dev, rb, to_wait, n / datum_size)) {
  121. if (signal_pending(current)) {
  122. ret = -ERESTARTSYS;
  123. break;
  124. }
  125. wait_woken(&wait, TASK_INTERRUPTIBLE,
  126. MAX_SCHEDULE_TIMEOUT);
  127. continue;
  128. }
  129. ret = rb->access->read_first_n(rb, n, buf);
  130. if (ret == 0 && (filp->f_flags & O_NONBLOCK))
  131. ret = -EAGAIN;
  132. } while (ret == 0);
  133. remove_wait_queue(&rb->pollq, &wait);
  134. return ret;
  135. }
  136. /**
  137. * iio_buffer_poll() - poll the buffer to find out if it has data
  138. * @filp: File structure pointer for device access
  139. * @wait: Poll table structure pointer for which the driver adds
  140. * a wait queue
  141. *
  142. * Return: (POLLIN | POLLRDNORM) if data is available for reading
  143. * or 0 for other cases
  144. */
  145. unsigned int iio_buffer_poll(struct file *filp,
  146. struct poll_table_struct *wait)
  147. {
  148. struct iio_dev *indio_dev = filp->private_data;
  149. struct iio_buffer *rb = indio_dev->buffer;
  150. if (!indio_dev->info || rb == NULL)
  151. return 0;
  152. poll_wait(filp, &rb->pollq, wait);
  153. if (iio_buffer_ready(indio_dev, rb, rb->watermark, 0))
  154. return POLLIN | POLLRDNORM;
  155. return 0;
  156. }
  157. /**
  158. * iio_buffer_wakeup_poll - Wakes up the buffer waitqueue
  159. * @indio_dev: The IIO device
  160. *
  161. * Wakes up the event waitqueue used for poll(). Should usually
  162. * be called when the device is unregistered.
  163. */
  164. void iio_buffer_wakeup_poll(struct iio_dev *indio_dev)
  165. {
  166. if (!indio_dev->buffer)
  167. return;
  168. wake_up(&indio_dev->buffer->pollq);
  169. }
  170. void iio_buffer_init(struct iio_buffer *buffer)
  171. {
  172. INIT_LIST_HEAD(&buffer->demux_list);
  173. INIT_LIST_HEAD(&buffer->buffer_list);
  174. init_waitqueue_head(&buffer->pollq);
  175. kref_init(&buffer->ref);
  176. buffer->watermark = 1;
  177. }
  178. EXPORT_SYMBOL(iio_buffer_init);
  179. static ssize_t iio_show_scan_index(struct device *dev,
  180. struct device_attribute *attr,
  181. char *buf)
  182. {
  183. return sprintf(buf, "%u\n", to_iio_dev_attr(attr)->c->scan_index);
  184. }
  185. static ssize_t iio_show_fixed_type(struct device *dev,
  186. struct device_attribute *attr,
  187. char *buf)
  188. {
  189. struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
  190. u8 type = this_attr->c->scan_type.endianness;
  191. if (type == IIO_CPU) {
  192. #ifdef __LITTLE_ENDIAN
  193. type = IIO_LE;
  194. #else
  195. type = IIO_BE;
  196. #endif
  197. }
  198. if (this_attr->c->scan_type.repeat > 1)
  199. return sprintf(buf, "%s:%c%d/%dX%d>>%u\n",
  200. iio_endian_prefix[type],
  201. this_attr->c->scan_type.sign,
  202. this_attr->c->scan_type.realbits,
  203. this_attr->c->scan_type.storagebits,
  204. this_attr->c->scan_type.repeat,
  205. this_attr->c->scan_type.shift);
  206. else
  207. return sprintf(buf, "%s:%c%d/%d>>%u\n",
  208. iio_endian_prefix[type],
  209. this_attr->c->scan_type.sign,
  210. this_attr->c->scan_type.realbits,
  211. this_attr->c->scan_type.storagebits,
  212. this_attr->c->scan_type.shift);
  213. }
  214. static ssize_t iio_scan_el_show(struct device *dev,
  215. struct device_attribute *attr,
  216. char *buf)
  217. {
  218. int ret;
  219. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  220. /* Ensure ret is 0 or 1. */
  221. ret = !!test_bit(to_iio_dev_attr(attr)->address,
  222. indio_dev->buffer->scan_mask);
  223. return sprintf(buf, "%d\n", ret);
  224. }
  225. /* Note NULL used as error indicator as it doesn't make sense. */
  226. static const unsigned long *iio_scan_mask_match(const unsigned long *av_masks,
  227. unsigned int masklength,
  228. const unsigned long *mask,
  229. bool strict)
  230. {
  231. if (bitmap_empty(mask, masklength))
  232. return NULL;
  233. while (*av_masks) {
  234. if (strict) {
  235. if (bitmap_equal(mask, av_masks, masklength))
  236. return av_masks;
  237. } else {
  238. if (bitmap_subset(mask, av_masks, masklength))
  239. return av_masks;
  240. }
  241. av_masks += BITS_TO_LONGS(masklength);
  242. }
  243. return NULL;
  244. }
  245. static bool iio_validate_scan_mask(struct iio_dev *indio_dev,
  246. const unsigned long *mask)
  247. {
  248. if (!indio_dev->setup_ops->validate_scan_mask)
  249. return true;
  250. return indio_dev->setup_ops->validate_scan_mask(indio_dev, mask);
  251. }
  252. /**
  253. * iio_scan_mask_set() - set particular bit in the scan mask
  254. * @indio_dev: the iio device
  255. * @buffer: the buffer whose scan mask we are interested in
  256. * @bit: the bit to be set.
  257. *
  258. * Note that at this point we have no way of knowing what other
  259. * buffers might request, hence this code only verifies that the
  260. * individual buffers request is plausible.
  261. */
  262. static int iio_scan_mask_set(struct iio_dev *indio_dev,
  263. struct iio_buffer *buffer, int bit)
  264. {
  265. const unsigned long *mask;
  266. unsigned long *trialmask;
  267. trialmask = kmalloc(sizeof(*trialmask)*
  268. BITS_TO_LONGS(indio_dev->masklength),
  269. GFP_KERNEL);
  270. if (trialmask == NULL)
  271. return -ENOMEM;
  272. if (!indio_dev->masklength) {
  273. WARN(1, "Trying to set scanmask prior to registering buffer\n");
  274. goto err_invalid_mask;
  275. }
  276. bitmap_copy(trialmask, buffer->scan_mask, indio_dev->masklength);
  277. set_bit(bit, trialmask);
  278. if (!iio_validate_scan_mask(indio_dev, trialmask))
  279. goto err_invalid_mask;
  280. if (indio_dev->available_scan_masks) {
  281. mask = iio_scan_mask_match(indio_dev->available_scan_masks,
  282. indio_dev->masklength,
  283. trialmask, false);
  284. if (!mask)
  285. goto err_invalid_mask;
  286. }
  287. bitmap_copy(buffer->scan_mask, trialmask, indio_dev->masklength);
  288. kfree(trialmask);
  289. return 0;
  290. err_invalid_mask:
  291. kfree(trialmask);
  292. return -EINVAL;
  293. }
  294. static int iio_scan_mask_clear(struct iio_buffer *buffer, int bit)
  295. {
  296. clear_bit(bit, buffer->scan_mask);
  297. return 0;
  298. }
  299. static ssize_t iio_scan_el_store(struct device *dev,
  300. struct device_attribute *attr,
  301. const char *buf,
  302. size_t len)
  303. {
  304. int ret;
  305. bool state;
  306. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  307. struct iio_buffer *buffer = indio_dev->buffer;
  308. struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
  309. ret = strtobool(buf, &state);
  310. if (ret < 0)
  311. return ret;
  312. mutex_lock(&indio_dev->mlock);
  313. if (iio_buffer_is_active(indio_dev->buffer)) {
  314. ret = -EBUSY;
  315. goto error_ret;
  316. }
  317. ret = iio_scan_mask_query(indio_dev, buffer, this_attr->address);
  318. if (ret < 0)
  319. goto error_ret;
  320. if (!state && ret) {
  321. ret = iio_scan_mask_clear(buffer, this_attr->address);
  322. if (ret)
  323. goto error_ret;
  324. } else if (state && !ret) {
  325. ret = iio_scan_mask_set(indio_dev, buffer, this_attr->address);
  326. if (ret)
  327. goto error_ret;
  328. }
  329. error_ret:
  330. mutex_unlock(&indio_dev->mlock);
  331. return ret < 0 ? ret : len;
  332. }
  333. static ssize_t iio_scan_el_ts_show(struct device *dev,
  334. struct device_attribute *attr,
  335. char *buf)
  336. {
  337. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  338. return sprintf(buf, "%d\n", indio_dev->buffer->scan_timestamp);
  339. }
  340. static ssize_t iio_scan_el_ts_store(struct device *dev,
  341. struct device_attribute *attr,
  342. const char *buf,
  343. size_t len)
  344. {
  345. int ret;
  346. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  347. bool state;
  348. ret = strtobool(buf, &state);
  349. if (ret < 0)
  350. return ret;
  351. mutex_lock(&indio_dev->mlock);
  352. if (iio_buffer_is_active(indio_dev->buffer)) {
  353. ret = -EBUSY;
  354. goto error_ret;
  355. }
  356. indio_dev->buffer->scan_timestamp = state;
  357. error_ret:
  358. mutex_unlock(&indio_dev->mlock);
  359. return ret ? ret : len;
  360. }
  361. static int iio_buffer_add_channel_sysfs(struct iio_dev *indio_dev,
  362. const struct iio_chan_spec *chan)
  363. {
  364. int ret, attrcount = 0;
  365. struct iio_buffer *buffer = indio_dev->buffer;
  366. ret = __iio_add_chan_devattr("index",
  367. chan,
  368. &iio_show_scan_index,
  369. NULL,
  370. 0,
  371. IIO_SEPARATE,
  372. &indio_dev->dev,
  373. &buffer->scan_el_dev_attr_list);
  374. if (ret)
  375. return ret;
  376. attrcount++;
  377. ret = __iio_add_chan_devattr("type",
  378. chan,
  379. &iio_show_fixed_type,
  380. NULL,
  381. 0,
  382. 0,
  383. &indio_dev->dev,
  384. &buffer->scan_el_dev_attr_list);
  385. if (ret)
  386. return ret;
  387. attrcount++;
  388. if (chan->type != IIO_TIMESTAMP)
  389. ret = __iio_add_chan_devattr("en",
  390. chan,
  391. &iio_scan_el_show,
  392. &iio_scan_el_store,
  393. chan->scan_index,
  394. 0,
  395. &indio_dev->dev,
  396. &buffer->scan_el_dev_attr_list);
  397. else
  398. ret = __iio_add_chan_devattr("en",
  399. chan,
  400. &iio_scan_el_ts_show,
  401. &iio_scan_el_ts_store,
  402. chan->scan_index,
  403. 0,
  404. &indio_dev->dev,
  405. &buffer->scan_el_dev_attr_list);
  406. if (ret)
  407. return ret;
  408. attrcount++;
  409. ret = attrcount;
  410. return ret;
  411. }
  412. static ssize_t iio_buffer_read_length(struct device *dev,
  413. struct device_attribute *attr,
  414. char *buf)
  415. {
  416. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  417. struct iio_buffer *buffer = indio_dev->buffer;
  418. return sprintf(buf, "%d\n", buffer->length);
  419. }
  420. static ssize_t iio_buffer_write_length(struct device *dev,
  421. struct device_attribute *attr,
  422. const char *buf, size_t len)
  423. {
  424. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  425. struct iio_buffer *buffer = indio_dev->buffer;
  426. unsigned int val;
  427. int ret;
  428. ret = kstrtouint(buf, 10, &val);
  429. if (ret)
  430. return ret;
  431. if (val == buffer->length)
  432. return len;
  433. mutex_lock(&indio_dev->mlock);
  434. if (iio_buffer_is_active(indio_dev->buffer)) {
  435. ret = -EBUSY;
  436. } else {
  437. buffer->access->set_length(buffer, val);
  438. ret = 0;
  439. }
  440. if (ret)
  441. goto out;
  442. if (buffer->length && buffer->length < buffer->watermark)
  443. buffer->watermark = buffer->length;
  444. out:
  445. mutex_unlock(&indio_dev->mlock);
  446. return ret ? ret : len;
  447. }
  448. static ssize_t iio_buffer_show_enable(struct device *dev,
  449. struct device_attribute *attr,
  450. char *buf)
  451. {
  452. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  453. return sprintf(buf, "%d\n", iio_buffer_is_active(indio_dev->buffer));
  454. }
  455. static int iio_compute_scan_bytes(struct iio_dev *indio_dev,
  456. const unsigned long *mask, bool timestamp)
  457. {
  458. const struct iio_chan_spec *ch;
  459. unsigned bytes = 0;
  460. int length, i;
  461. /* How much space will the demuxed element take? */
  462. for_each_set_bit(i, mask,
  463. indio_dev->masklength) {
  464. ch = iio_find_channel_from_si(indio_dev, i);
  465. if (ch->scan_type.repeat > 1)
  466. length = ch->scan_type.storagebits / 8 *
  467. ch->scan_type.repeat;
  468. else
  469. length = ch->scan_type.storagebits / 8;
  470. bytes = ALIGN(bytes, length);
  471. bytes += length;
  472. }
  473. if (timestamp) {
  474. ch = iio_find_channel_from_si(indio_dev,
  475. indio_dev->scan_index_timestamp);
  476. if (ch->scan_type.repeat > 1)
  477. length = ch->scan_type.storagebits / 8 *
  478. ch->scan_type.repeat;
  479. else
  480. length = ch->scan_type.storagebits / 8;
  481. bytes = ALIGN(bytes, length);
  482. bytes += length;
  483. }
  484. return bytes;
  485. }
  486. static void iio_buffer_activate(struct iio_dev *indio_dev,
  487. struct iio_buffer *buffer)
  488. {
  489. iio_buffer_get(buffer);
  490. list_add(&buffer->buffer_list, &indio_dev->buffer_list);
  491. }
  492. static void iio_buffer_deactivate(struct iio_buffer *buffer)
  493. {
  494. list_del_init(&buffer->buffer_list);
  495. wake_up_interruptible(&buffer->pollq);
  496. iio_buffer_put(buffer);
  497. }
  498. static void iio_buffer_deactivate_all(struct iio_dev *indio_dev)
  499. {
  500. struct iio_buffer *buffer, *_buffer;
  501. list_for_each_entry_safe(buffer, _buffer,
  502. &indio_dev->buffer_list, buffer_list)
  503. iio_buffer_deactivate(buffer);
  504. }
  505. static void iio_buffer_update_bytes_per_datum(struct iio_dev *indio_dev,
  506. struct iio_buffer *buffer)
  507. {
  508. unsigned int bytes;
  509. if (!buffer->access->set_bytes_per_datum)
  510. return;
  511. bytes = iio_compute_scan_bytes(indio_dev, buffer->scan_mask,
  512. buffer->scan_timestamp);
  513. buffer->access->set_bytes_per_datum(buffer, bytes);
  514. }
  515. static int iio_buffer_request_update(struct iio_dev *indio_dev,
  516. struct iio_buffer *buffer)
  517. {
  518. int ret;
  519. iio_buffer_update_bytes_per_datum(indio_dev, buffer);
  520. if (buffer->access->request_update) {
  521. ret = buffer->access->request_update(buffer);
  522. if (ret) {
  523. dev_dbg(&indio_dev->dev,
  524. "Buffer not started: buffer parameter update failed (%d)\n",
  525. ret);
  526. return ret;
  527. }
  528. }
  529. return 0;
  530. }
  531. static void iio_free_scan_mask(struct iio_dev *indio_dev,
  532. const unsigned long *mask)
  533. {
  534. /* If the mask is dynamically allocated free it, otherwise do nothing */
  535. if (!indio_dev->available_scan_masks)
  536. kfree(mask);
  537. }
  538. struct iio_device_config {
  539. unsigned int mode;
  540. const unsigned long *scan_mask;
  541. unsigned int scan_bytes;
  542. bool scan_timestamp;
  543. };
  544. static int iio_verify_update(struct iio_dev *indio_dev,
  545. struct iio_buffer *insert_buffer, struct iio_buffer *remove_buffer,
  546. struct iio_device_config *config)
  547. {
  548. unsigned long *compound_mask;
  549. const unsigned long *scan_mask;
  550. bool strict_scanmask = false;
  551. struct iio_buffer *buffer;
  552. bool scan_timestamp;
  553. unsigned int modes;
  554. memset(config, 0, sizeof(*config));
  555. /*
  556. * If there is just one buffer and we are removing it there is nothing
  557. * to verify.
  558. */
  559. if (remove_buffer && !insert_buffer &&
  560. list_is_singular(&indio_dev->buffer_list))
  561. return 0;
  562. modes = indio_dev->modes;
  563. list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list) {
  564. if (buffer == remove_buffer)
  565. continue;
  566. modes &= buffer->access->modes;
  567. }
  568. if (insert_buffer)
  569. modes &= insert_buffer->access->modes;
  570. /* Definitely possible for devices to support both of these. */
  571. if ((modes & INDIO_BUFFER_TRIGGERED) && indio_dev->trig) {
  572. config->mode = INDIO_BUFFER_TRIGGERED;
  573. } else if (modes & INDIO_BUFFER_HARDWARE) {
  574. /*
  575. * Keep things simple for now and only allow a single buffer to
  576. * be connected in hardware mode.
  577. */
  578. if (insert_buffer && !list_empty(&indio_dev->buffer_list))
  579. return -EINVAL;
  580. config->mode = INDIO_BUFFER_HARDWARE;
  581. strict_scanmask = true;
  582. } else if (modes & INDIO_BUFFER_SOFTWARE) {
  583. config->mode = INDIO_BUFFER_SOFTWARE;
  584. } else {
  585. /* Can only occur on first buffer */
  586. if (indio_dev->modes & INDIO_BUFFER_TRIGGERED)
  587. dev_dbg(&indio_dev->dev, "Buffer not started: no trigger\n");
  588. return -EINVAL;
  589. }
  590. /* What scan mask do we actually have? */
  591. compound_mask = kcalloc(BITS_TO_LONGS(indio_dev->masklength),
  592. sizeof(long), GFP_KERNEL);
  593. if (compound_mask == NULL)
  594. return -ENOMEM;
  595. scan_timestamp = false;
  596. list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list) {
  597. if (buffer == remove_buffer)
  598. continue;
  599. bitmap_or(compound_mask, compound_mask, buffer->scan_mask,
  600. indio_dev->masklength);
  601. scan_timestamp |= buffer->scan_timestamp;
  602. }
  603. if (insert_buffer) {
  604. bitmap_or(compound_mask, compound_mask,
  605. insert_buffer->scan_mask, indio_dev->masklength);
  606. scan_timestamp |= insert_buffer->scan_timestamp;
  607. }
  608. if (indio_dev->available_scan_masks) {
  609. scan_mask = iio_scan_mask_match(indio_dev->available_scan_masks,
  610. indio_dev->masklength,
  611. compound_mask,
  612. strict_scanmask);
  613. kfree(compound_mask);
  614. if (scan_mask == NULL)
  615. return -EINVAL;
  616. } else {
  617. scan_mask = compound_mask;
  618. }
  619. config->scan_bytes = iio_compute_scan_bytes(indio_dev,
  620. scan_mask, scan_timestamp);
  621. config->scan_mask = scan_mask;
  622. config->scan_timestamp = scan_timestamp;
  623. return 0;
  624. }
  625. static int iio_enable_buffers(struct iio_dev *indio_dev,
  626. struct iio_device_config *config)
  627. {
  628. int ret;
  629. indio_dev->active_scan_mask = config->scan_mask;
  630. indio_dev->scan_timestamp = config->scan_timestamp;
  631. indio_dev->scan_bytes = config->scan_bytes;
  632. iio_update_demux(indio_dev);
  633. /* Wind up again */
  634. if (indio_dev->setup_ops->preenable) {
  635. ret = indio_dev->setup_ops->preenable(indio_dev);
  636. if (ret) {
  637. dev_dbg(&indio_dev->dev,
  638. "Buffer not started: buffer preenable failed (%d)\n", ret);
  639. goto err_undo_config;
  640. }
  641. }
  642. if (indio_dev->info->update_scan_mode) {
  643. ret = indio_dev->info
  644. ->update_scan_mode(indio_dev,
  645. indio_dev->active_scan_mask);
  646. if (ret < 0) {
  647. dev_dbg(&indio_dev->dev,
  648. "Buffer not started: update scan mode failed (%d)\n",
  649. ret);
  650. goto err_run_postdisable;
  651. }
  652. }
  653. indio_dev->currentmode = config->mode;
  654. if (indio_dev->setup_ops->postenable) {
  655. ret = indio_dev->setup_ops->postenable(indio_dev);
  656. if (ret) {
  657. dev_dbg(&indio_dev->dev,
  658. "Buffer not started: postenable failed (%d)\n", ret);
  659. goto err_run_postdisable;
  660. }
  661. }
  662. return 0;
  663. err_run_postdisable:
  664. indio_dev->currentmode = INDIO_DIRECT_MODE;
  665. if (indio_dev->setup_ops->postdisable)
  666. indio_dev->setup_ops->postdisable(indio_dev);
  667. err_undo_config:
  668. indio_dev->active_scan_mask = NULL;
  669. return ret;
  670. }
  671. static int iio_disable_buffers(struct iio_dev *indio_dev)
  672. {
  673. int ret = 0;
  674. int ret2;
  675. /* Wind down existing buffers - iff there are any */
  676. if (list_empty(&indio_dev->buffer_list))
  677. return 0;
  678. /*
  679. * If things go wrong at some step in disable we still need to continue
  680. * to perform the other steps, otherwise we leave the device in a
  681. * inconsistent state. We return the error code for the first error we
  682. * encountered.
  683. */
  684. if (indio_dev->setup_ops->predisable) {
  685. ret2 = indio_dev->setup_ops->predisable(indio_dev);
  686. if (ret2 && !ret)
  687. ret = ret2;
  688. }
  689. indio_dev->currentmode = INDIO_DIRECT_MODE;
  690. if (indio_dev->setup_ops->postdisable) {
  691. ret2 = indio_dev->setup_ops->postdisable(indio_dev);
  692. if (ret2 && !ret)
  693. ret = ret2;
  694. }
  695. iio_free_scan_mask(indio_dev, indio_dev->active_scan_mask);
  696. indio_dev->active_scan_mask = NULL;
  697. return ret;
  698. }
  699. static int __iio_update_buffers(struct iio_dev *indio_dev,
  700. struct iio_buffer *insert_buffer,
  701. struct iio_buffer *remove_buffer)
  702. {
  703. struct iio_device_config new_config;
  704. int ret;
  705. ret = iio_verify_update(indio_dev, insert_buffer, remove_buffer,
  706. &new_config);
  707. if (ret)
  708. return ret;
  709. if (insert_buffer) {
  710. ret = iio_buffer_request_update(indio_dev, insert_buffer);
  711. if (ret)
  712. goto err_free_config;
  713. }
  714. ret = iio_disable_buffers(indio_dev);
  715. if (ret)
  716. goto err_deactivate_all;
  717. if (remove_buffer)
  718. iio_buffer_deactivate(remove_buffer);
  719. if (insert_buffer)
  720. iio_buffer_activate(indio_dev, insert_buffer);
  721. /* If no buffers in list, we are done */
  722. if (list_empty(&indio_dev->buffer_list))
  723. return 0;
  724. ret = iio_enable_buffers(indio_dev, &new_config);
  725. if (ret)
  726. goto err_deactivate_all;
  727. return 0;
  728. err_deactivate_all:
  729. /*
  730. * We've already verified that the config is valid earlier. If things go
  731. * wrong in either enable or disable the most likely reason is an IO
  732. * error from the device. In this case there is no good recovery
  733. * strategy. Just make sure to disable everything and leave the device
  734. * in a sane state. With a bit of luck the device might come back to
  735. * life again later and userspace can try again.
  736. */
  737. iio_buffer_deactivate_all(indio_dev);
  738. err_free_config:
  739. iio_free_scan_mask(indio_dev, new_config.scan_mask);
  740. return ret;
  741. }
  742. int iio_update_buffers(struct iio_dev *indio_dev,
  743. struct iio_buffer *insert_buffer,
  744. struct iio_buffer *remove_buffer)
  745. {
  746. int ret;
  747. if (insert_buffer == remove_buffer)
  748. return 0;
  749. mutex_lock(&indio_dev->info_exist_lock);
  750. mutex_lock(&indio_dev->mlock);
  751. if (insert_buffer && iio_buffer_is_active(insert_buffer))
  752. insert_buffer = NULL;
  753. if (remove_buffer && !iio_buffer_is_active(remove_buffer))
  754. remove_buffer = NULL;
  755. if (!insert_buffer && !remove_buffer) {
  756. ret = 0;
  757. goto out_unlock;
  758. }
  759. if (indio_dev->info == NULL) {
  760. ret = -ENODEV;
  761. goto out_unlock;
  762. }
  763. ret = __iio_update_buffers(indio_dev, insert_buffer, remove_buffer);
  764. out_unlock:
  765. mutex_unlock(&indio_dev->mlock);
  766. mutex_unlock(&indio_dev->info_exist_lock);
  767. return ret;
  768. }
  769. EXPORT_SYMBOL_GPL(iio_update_buffers);
  770. void iio_disable_all_buffers(struct iio_dev *indio_dev)
  771. {
  772. iio_disable_buffers(indio_dev);
  773. iio_buffer_deactivate_all(indio_dev);
  774. }
  775. static ssize_t iio_buffer_store_enable(struct device *dev,
  776. struct device_attribute *attr,
  777. const char *buf,
  778. size_t len)
  779. {
  780. int ret;
  781. bool requested_state;
  782. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  783. bool inlist;
  784. ret = strtobool(buf, &requested_state);
  785. if (ret < 0)
  786. return ret;
  787. mutex_lock(&indio_dev->mlock);
  788. /* Find out if it is in the list */
  789. inlist = iio_buffer_is_active(indio_dev->buffer);
  790. /* Already in desired state */
  791. if (inlist == requested_state)
  792. goto done;
  793. if (requested_state)
  794. ret = __iio_update_buffers(indio_dev,
  795. indio_dev->buffer, NULL);
  796. else
  797. ret = __iio_update_buffers(indio_dev,
  798. NULL, indio_dev->buffer);
  799. done:
  800. mutex_unlock(&indio_dev->mlock);
  801. return (ret < 0) ? ret : len;
  802. }
  803. static const char * const iio_scan_elements_group_name = "scan_elements";
  804. static ssize_t iio_buffer_show_watermark(struct device *dev,
  805. struct device_attribute *attr,
  806. char *buf)
  807. {
  808. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  809. struct iio_buffer *buffer = indio_dev->buffer;
  810. return sprintf(buf, "%u\n", buffer->watermark);
  811. }
  812. static ssize_t iio_buffer_store_watermark(struct device *dev,
  813. struct device_attribute *attr,
  814. const char *buf,
  815. size_t len)
  816. {
  817. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  818. struct iio_buffer *buffer = indio_dev->buffer;
  819. unsigned int val;
  820. int ret;
  821. ret = kstrtouint(buf, 10, &val);
  822. if (ret)
  823. return ret;
  824. if (!val)
  825. return -EINVAL;
  826. mutex_lock(&indio_dev->mlock);
  827. if (val > buffer->length) {
  828. ret = -EINVAL;
  829. goto out;
  830. }
  831. if (iio_buffer_is_active(indio_dev->buffer)) {
  832. ret = -EBUSY;
  833. goto out;
  834. }
  835. buffer->watermark = val;
  836. if (indio_dev->info->hwfifo_set_watermark)
  837. indio_dev->info->hwfifo_set_watermark(indio_dev, val);
  838. out:
  839. mutex_unlock(&indio_dev->mlock);
  840. return ret ? ret : len;
  841. }
  842. static DEVICE_ATTR(length, S_IRUGO | S_IWUSR, iio_buffer_read_length,
  843. iio_buffer_write_length);
  844. static struct device_attribute dev_attr_length_ro = __ATTR(length,
  845. S_IRUGO, iio_buffer_read_length, NULL);
  846. static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR,
  847. iio_buffer_show_enable, iio_buffer_store_enable);
  848. static DEVICE_ATTR(watermark, S_IRUGO | S_IWUSR,
  849. iio_buffer_show_watermark, iio_buffer_store_watermark);
  850. static struct attribute *iio_buffer_attrs[] = {
  851. &dev_attr_length.attr,
  852. &dev_attr_enable.attr,
  853. &dev_attr_watermark.attr,
  854. };
  855. int iio_buffer_alloc_sysfs_and_mask(struct iio_dev *indio_dev)
  856. {
  857. struct iio_dev_attr *p;
  858. struct attribute **attr;
  859. struct iio_buffer *buffer = indio_dev->buffer;
  860. int ret, i, attrn, attrcount, attrcount_orig = 0;
  861. const struct iio_chan_spec *channels;
  862. channels = indio_dev->channels;
  863. if (channels) {
  864. int ml = indio_dev->masklength;
  865. for (i = 0; i < indio_dev->num_channels; i++)
  866. ml = max(ml, channels[i].scan_index + 1);
  867. indio_dev->masklength = ml;
  868. }
  869. if (!buffer)
  870. return 0;
  871. attrcount = 0;
  872. if (buffer->attrs) {
  873. while (buffer->attrs[attrcount] != NULL)
  874. attrcount++;
  875. }
  876. attr = kcalloc(attrcount + ARRAY_SIZE(iio_buffer_attrs) + 1,
  877. sizeof(struct attribute *), GFP_KERNEL);
  878. if (!attr)
  879. return -ENOMEM;
  880. memcpy(attr, iio_buffer_attrs, sizeof(iio_buffer_attrs));
  881. if (!buffer->access->set_length)
  882. attr[0] = &dev_attr_length_ro.attr;
  883. if (buffer->attrs)
  884. memcpy(&attr[ARRAY_SIZE(iio_buffer_attrs)], buffer->attrs,
  885. sizeof(struct attribute *) * attrcount);
  886. attr[attrcount + ARRAY_SIZE(iio_buffer_attrs)] = NULL;
  887. buffer->buffer_group.name = "buffer";
  888. buffer->buffer_group.attrs = attr;
  889. indio_dev->groups[indio_dev->groupcounter++] = &buffer->buffer_group;
  890. if (buffer->scan_el_attrs != NULL) {
  891. attr = buffer->scan_el_attrs->attrs;
  892. while (*attr++ != NULL)
  893. attrcount_orig++;
  894. }
  895. attrcount = attrcount_orig;
  896. INIT_LIST_HEAD(&buffer->scan_el_dev_attr_list);
  897. channels = indio_dev->channels;
  898. if (channels) {
  899. /* new magic */
  900. for (i = 0; i < indio_dev->num_channels; i++) {
  901. if (channels[i].scan_index < 0)
  902. continue;
  903. ret = iio_buffer_add_channel_sysfs(indio_dev,
  904. &channels[i]);
  905. if (ret < 0)
  906. goto error_cleanup_dynamic;
  907. attrcount += ret;
  908. if (channels[i].type == IIO_TIMESTAMP)
  909. indio_dev->scan_index_timestamp =
  910. channels[i].scan_index;
  911. }
  912. if (indio_dev->masklength && buffer->scan_mask == NULL) {
  913. buffer->scan_mask = kcalloc(BITS_TO_LONGS(indio_dev->masklength),
  914. sizeof(*buffer->scan_mask),
  915. GFP_KERNEL);
  916. if (buffer->scan_mask == NULL) {
  917. ret = -ENOMEM;
  918. goto error_cleanup_dynamic;
  919. }
  920. }
  921. }
  922. buffer->scan_el_group.name = iio_scan_elements_group_name;
  923. buffer->scan_el_group.attrs = kcalloc(attrcount + 1,
  924. sizeof(buffer->scan_el_group.attrs[0]),
  925. GFP_KERNEL);
  926. if (buffer->scan_el_group.attrs == NULL) {
  927. ret = -ENOMEM;
  928. goto error_free_scan_mask;
  929. }
  930. if (buffer->scan_el_attrs)
  931. memcpy(buffer->scan_el_group.attrs, buffer->scan_el_attrs,
  932. sizeof(buffer->scan_el_group.attrs[0])*attrcount_orig);
  933. attrn = attrcount_orig;
  934. list_for_each_entry(p, &buffer->scan_el_dev_attr_list, l)
  935. buffer->scan_el_group.attrs[attrn++] = &p->dev_attr.attr;
  936. indio_dev->groups[indio_dev->groupcounter++] = &buffer->scan_el_group;
  937. return 0;
  938. error_free_scan_mask:
  939. kfree(buffer->scan_mask);
  940. error_cleanup_dynamic:
  941. iio_free_chan_devattr_list(&buffer->scan_el_dev_attr_list);
  942. kfree(indio_dev->buffer->buffer_group.attrs);
  943. return ret;
  944. }
  945. void iio_buffer_free_sysfs_and_mask(struct iio_dev *indio_dev)
  946. {
  947. if (!indio_dev->buffer)
  948. return;
  949. kfree(indio_dev->buffer->scan_mask);
  950. kfree(indio_dev->buffer->buffer_group.attrs);
  951. kfree(indio_dev->buffer->scan_el_group.attrs);
  952. iio_free_chan_devattr_list(&indio_dev->buffer->scan_el_dev_attr_list);
  953. }
  954. /**
  955. * iio_validate_scan_mask_onehot() - Validates that exactly one channel is selected
  956. * @indio_dev: the iio device
  957. * @mask: scan mask to be checked
  958. *
  959. * Return true if exactly one bit is set in the scan mask, false otherwise. It
  960. * can be used for devices where only one channel can be active for sampling at
  961. * a time.
  962. */
  963. bool iio_validate_scan_mask_onehot(struct iio_dev *indio_dev,
  964. const unsigned long *mask)
  965. {
  966. return bitmap_weight(mask, indio_dev->masklength) == 1;
  967. }
  968. EXPORT_SYMBOL_GPL(iio_validate_scan_mask_onehot);
  969. int iio_scan_mask_query(struct iio_dev *indio_dev,
  970. struct iio_buffer *buffer, int bit)
  971. {
  972. if (bit > indio_dev->masklength)
  973. return -EINVAL;
  974. if (!buffer->scan_mask)
  975. return 0;
  976. /* Ensure return value is 0 or 1. */
  977. return !!test_bit(bit, buffer->scan_mask);
  978. };
  979. EXPORT_SYMBOL_GPL(iio_scan_mask_query);
  980. /**
  981. * struct iio_demux_table - table describing demux memcpy ops
  982. * @from: index to copy from
  983. * @to: index to copy to
  984. * @length: how many bytes to copy
  985. * @l: list head used for management
  986. */
  987. struct iio_demux_table {
  988. unsigned from;
  989. unsigned to;
  990. unsigned length;
  991. struct list_head l;
  992. };
  993. static const void *iio_demux(struct iio_buffer *buffer,
  994. const void *datain)
  995. {
  996. struct iio_demux_table *t;
  997. if (list_empty(&buffer->demux_list))
  998. return datain;
  999. list_for_each_entry(t, &buffer->demux_list, l)
  1000. memcpy(buffer->demux_bounce + t->to,
  1001. datain + t->from, t->length);
  1002. return buffer->demux_bounce;
  1003. }
  1004. static int iio_push_to_buffer(struct iio_buffer *buffer, const void *data)
  1005. {
  1006. const void *dataout = iio_demux(buffer, data);
  1007. int ret;
  1008. ret = buffer->access->store_to(buffer, dataout);
  1009. if (ret)
  1010. return ret;
  1011. /*
  1012. * We can't just test for watermark to decide if we wake the poll queue
  1013. * because read may request less samples than the watermark.
  1014. */
  1015. wake_up_interruptible_poll(&buffer->pollq, POLLIN | POLLRDNORM);
  1016. return 0;
  1017. }
  1018. static void iio_buffer_demux_free(struct iio_buffer *buffer)
  1019. {
  1020. struct iio_demux_table *p, *q;
  1021. list_for_each_entry_safe(p, q, &buffer->demux_list, l) {
  1022. list_del(&p->l);
  1023. kfree(p);
  1024. }
  1025. }
  1026. int iio_push_to_buffers(struct iio_dev *indio_dev, const void *data)
  1027. {
  1028. int ret;
  1029. struct iio_buffer *buf;
  1030. list_for_each_entry(buf, &indio_dev->buffer_list, buffer_list) {
  1031. ret = iio_push_to_buffer(buf, data);
  1032. if (ret < 0)
  1033. return ret;
  1034. }
  1035. return 0;
  1036. }
  1037. EXPORT_SYMBOL_GPL(iio_push_to_buffers);
  1038. static int iio_buffer_add_demux(struct iio_buffer *buffer,
  1039. struct iio_demux_table **p, unsigned int in_loc, unsigned int out_loc,
  1040. unsigned int length)
  1041. {
  1042. if (*p && (*p)->from + (*p)->length == in_loc &&
  1043. (*p)->to + (*p)->length == out_loc) {
  1044. (*p)->length += length;
  1045. } else {
  1046. *p = kmalloc(sizeof(**p), GFP_KERNEL);
  1047. if (*p == NULL)
  1048. return -ENOMEM;
  1049. (*p)->from = in_loc;
  1050. (*p)->to = out_loc;
  1051. (*p)->length = length;
  1052. list_add_tail(&(*p)->l, &buffer->demux_list);
  1053. }
  1054. return 0;
  1055. }
  1056. static int iio_buffer_update_demux(struct iio_dev *indio_dev,
  1057. struct iio_buffer *buffer)
  1058. {
  1059. const struct iio_chan_spec *ch;
  1060. int ret, in_ind = -1, out_ind, length;
  1061. unsigned in_loc = 0, out_loc = 0;
  1062. struct iio_demux_table *p = NULL;
  1063. /* Clear out any old demux */
  1064. iio_buffer_demux_free(buffer);
  1065. kfree(buffer->demux_bounce);
  1066. buffer->demux_bounce = NULL;
  1067. /* First work out which scan mode we will actually have */
  1068. if (bitmap_equal(indio_dev->active_scan_mask,
  1069. buffer->scan_mask,
  1070. indio_dev->masklength))
  1071. return 0;
  1072. /* Now we have the two masks, work from least sig and build up sizes */
  1073. for_each_set_bit(out_ind,
  1074. buffer->scan_mask,
  1075. indio_dev->masklength) {
  1076. in_ind = find_next_bit(indio_dev->active_scan_mask,
  1077. indio_dev->masklength,
  1078. in_ind + 1);
  1079. while (in_ind != out_ind) {
  1080. in_ind = find_next_bit(indio_dev->active_scan_mask,
  1081. indio_dev->masklength,
  1082. in_ind + 1);
  1083. ch = iio_find_channel_from_si(indio_dev, in_ind);
  1084. if (ch->scan_type.repeat > 1)
  1085. length = ch->scan_type.storagebits / 8 *
  1086. ch->scan_type.repeat;
  1087. else
  1088. length = ch->scan_type.storagebits / 8;
  1089. /* Make sure we are aligned */
  1090. in_loc = roundup(in_loc, length) + length;
  1091. }
  1092. ch = iio_find_channel_from_si(indio_dev, in_ind);
  1093. if (ch->scan_type.repeat > 1)
  1094. length = ch->scan_type.storagebits / 8 *
  1095. ch->scan_type.repeat;
  1096. else
  1097. length = ch->scan_type.storagebits / 8;
  1098. out_loc = roundup(out_loc, length);
  1099. in_loc = roundup(in_loc, length);
  1100. ret = iio_buffer_add_demux(buffer, &p, in_loc, out_loc, length);
  1101. if (ret)
  1102. goto error_clear_mux_table;
  1103. out_loc += length;
  1104. in_loc += length;
  1105. }
  1106. /* Relies on scan_timestamp being last */
  1107. if (buffer->scan_timestamp) {
  1108. ch = iio_find_channel_from_si(indio_dev,
  1109. indio_dev->scan_index_timestamp);
  1110. if (ch->scan_type.repeat > 1)
  1111. length = ch->scan_type.storagebits / 8 *
  1112. ch->scan_type.repeat;
  1113. else
  1114. length = ch->scan_type.storagebits / 8;
  1115. out_loc = roundup(out_loc, length);
  1116. in_loc = roundup(in_loc, length);
  1117. ret = iio_buffer_add_demux(buffer, &p, in_loc, out_loc, length);
  1118. if (ret)
  1119. goto error_clear_mux_table;
  1120. out_loc += length;
  1121. in_loc += length;
  1122. }
  1123. buffer->demux_bounce = kzalloc(out_loc, GFP_KERNEL);
  1124. if (buffer->demux_bounce == NULL) {
  1125. ret = -ENOMEM;
  1126. goto error_clear_mux_table;
  1127. }
  1128. return 0;
  1129. error_clear_mux_table:
  1130. iio_buffer_demux_free(buffer);
  1131. return ret;
  1132. }
  1133. int iio_update_demux(struct iio_dev *indio_dev)
  1134. {
  1135. struct iio_buffer *buffer;
  1136. int ret;
  1137. list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list) {
  1138. ret = iio_buffer_update_demux(indio_dev, buffer);
  1139. if (ret < 0)
  1140. goto error_clear_mux_table;
  1141. }
  1142. return 0;
  1143. error_clear_mux_table:
  1144. list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list)
  1145. iio_buffer_demux_free(buffer);
  1146. return ret;
  1147. }
  1148. EXPORT_SYMBOL_GPL(iio_update_demux);
  1149. /**
  1150. * iio_buffer_release() - Free a buffer's resources
  1151. * @ref: Pointer to the kref embedded in the iio_buffer struct
  1152. *
  1153. * This function is called when the last reference to the buffer has been
  1154. * dropped. It will typically free all resources allocated by the buffer. Do not
  1155. * call this function manually, always use iio_buffer_put() when done using a
  1156. * buffer.
  1157. */
  1158. static void iio_buffer_release(struct kref *ref)
  1159. {
  1160. struct iio_buffer *buffer = container_of(ref, struct iio_buffer, ref);
  1161. buffer->access->release(buffer);
  1162. }
  1163. /**
  1164. * iio_buffer_get() - Grab a reference to the buffer
  1165. * @buffer: The buffer to grab a reference for, may be NULL
  1166. *
  1167. * Returns the pointer to the buffer that was passed into the function.
  1168. */
  1169. struct iio_buffer *iio_buffer_get(struct iio_buffer *buffer)
  1170. {
  1171. if (buffer)
  1172. kref_get(&buffer->ref);
  1173. return buffer;
  1174. }
  1175. EXPORT_SYMBOL_GPL(iio_buffer_get);
  1176. /**
  1177. * iio_buffer_put() - Release the reference to the buffer
  1178. * @buffer: The buffer to release the reference for, may be NULL
  1179. */
  1180. void iio_buffer_put(struct iio_buffer *buffer)
  1181. {
  1182. if (buffer)
  1183. kref_put(&buffer->ref, iio_buffer_release);
  1184. }
  1185. EXPORT_SYMBOL_GPL(iio_buffer_put);