industrialio-buffer.c 35 KB

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