industrialio-buffer.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755
  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/iio/iio.h>
  24. #include "iio_core.h"
  25. #include <linux/iio/sysfs.h>
  26. #include <linux/iio/buffer.h>
  27. static const char * const iio_endian_prefix[] = {
  28. [IIO_BE] = "be",
  29. [IIO_LE] = "le",
  30. };
  31. /**
  32. * iio_buffer_read_first_n_outer() - chrdev read for buffer access
  33. *
  34. * This function relies on all buffer implementations having an
  35. * iio_buffer as their first element.
  36. **/
  37. ssize_t iio_buffer_read_first_n_outer(struct file *filp, char __user *buf,
  38. size_t n, loff_t *f_ps)
  39. {
  40. struct iio_dev *indio_dev = filp->private_data;
  41. struct iio_buffer *rb = indio_dev->buffer;
  42. if (!rb || !rb->access->read_first_n)
  43. return -EINVAL;
  44. return rb->access->read_first_n(rb, n, buf);
  45. }
  46. /**
  47. * iio_buffer_poll() - poll the buffer to find out if it has data
  48. */
  49. unsigned int iio_buffer_poll(struct file *filp,
  50. struct poll_table_struct *wait)
  51. {
  52. struct iio_dev *indio_dev = filp->private_data;
  53. struct iio_buffer *rb = indio_dev->buffer;
  54. poll_wait(filp, &rb->pollq, wait);
  55. if (rb->stufftoread)
  56. return POLLIN | POLLRDNORM;
  57. /* need a way of knowing if there may be enough data... */
  58. return 0;
  59. }
  60. void iio_buffer_init(struct iio_buffer *buffer)
  61. {
  62. INIT_LIST_HEAD(&buffer->demux_list);
  63. init_waitqueue_head(&buffer->pollq);
  64. }
  65. EXPORT_SYMBOL(iio_buffer_init);
  66. static ssize_t iio_show_scan_index(struct device *dev,
  67. struct device_attribute *attr,
  68. char *buf)
  69. {
  70. return sprintf(buf, "%u\n", to_iio_dev_attr(attr)->c->scan_index);
  71. }
  72. static ssize_t iio_show_fixed_type(struct device *dev,
  73. struct device_attribute *attr,
  74. char *buf)
  75. {
  76. struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
  77. u8 type = this_attr->c->scan_type.endianness;
  78. if (type == IIO_CPU) {
  79. #ifdef __LITTLE_ENDIAN
  80. type = IIO_LE;
  81. #else
  82. type = IIO_BE;
  83. #endif
  84. }
  85. return sprintf(buf, "%s:%c%d/%d>>%u\n",
  86. iio_endian_prefix[type],
  87. this_attr->c->scan_type.sign,
  88. this_attr->c->scan_type.realbits,
  89. this_attr->c->scan_type.storagebits,
  90. this_attr->c->scan_type.shift);
  91. }
  92. static ssize_t iio_scan_el_show(struct device *dev,
  93. struct device_attribute *attr,
  94. char *buf)
  95. {
  96. int ret;
  97. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  98. ret = test_bit(to_iio_dev_attr(attr)->address,
  99. indio_dev->buffer->scan_mask);
  100. return sprintf(buf, "%d\n", ret);
  101. }
  102. static int iio_scan_mask_clear(struct iio_buffer *buffer, int bit)
  103. {
  104. clear_bit(bit, buffer->scan_mask);
  105. return 0;
  106. }
  107. static ssize_t iio_scan_el_store(struct device *dev,
  108. struct device_attribute *attr,
  109. const char *buf,
  110. size_t len)
  111. {
  112. int ret;
  113. bool state;
  114. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  115. struct iio_buffer *buffer = indio_dev->buffer;
  116. struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
  117. ret = strtobool(buf, &state);
  118. if (ret < 0)
  119. return ret;
  120. mutex_lock(&indio_dev->mlock);
  121. if (iio_buffer_enabled(indio_dev)) {
  122. ret = -EBUSY;
  123. goto error_ret;
  124. }
  125. ret = iio_scan_mask_query(indio_dev, buffer, this_attr->address);
  126. if (ret < 0)
  127. goto error_ret;
  128. if (!state && ret) {
  129. ret = iio_scan_mask_clear(buffer, this_attr->address);
  130. if (ret)
  131. goto error_ret;
  132. } else if (state && !ret) {
  133. ret = iio_scan_mask_set(indio_dev, buffer, this_attr->address);
  134. if (ret)
  135. goto error_ret;
  136. }
  137. error_ret:
  138. mutex_unlock(&indio_dev->mlock);
  139. return ret < 0 ? ret : len;
  140. }
  141. static ssize_t iio_scan_el_ts_show(struct device *dev,
  142. struct device_attribute *attr,
  143. char *buf)
  144. {
  145. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  146. return sprintf(buf, "%d\n", indio_dev->buffer->scan_timestamp);
  147. }
  148. static ssize_t iio_scan_el_ts_store(struct device *dev,
  149. struct device_attribute *attr,
  150. const char *buf,
  151. size_t len)
  152. {
  153. int ret;
  154. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  155. bool state;
  156. ret = strtobool(buf, &state);
  157. if (ret < 0)
  158. return ret;
  159. mutex_lock(&indio_dev->mlock);
  160. if (iio_buffer_enabled(indio_dev)) {
  161. ret = -EBUSY;
  162. goto error_ret;
  163. }
  164. indio_dev->buffer->scan_timestamp = state;
  165. indio_dev->scan_timestamp = state;
  166. error_ret:
  167. mutex_unlock(&indio_dev->mlock);
  168. return ret ? ret : len;
  169. }
  170. static int iio_buffer_add_channel_sysfs(struct iio_dev *indio_dev,
  171. const struct iio_chan_spec *chan)
  172. {
  173. int ret, attrcount = 0;
  174. struct iio_buffer *buffer = indio_dev->buffer;
  175. ret = __iio_add_chan_devattr("index",
  176. chan,
  177. &iio_show_scan_index,
  178. NULL,
  179. 0,
  180. 0,
  181. &indio_dev->dev,
  182. &buffer->scan_el_dev_attr_list);
  183. if (ret)
  184. goto error_ret;
  185. attrcount++;
  186. ret = __iio_add_chan_devattr("type",
  187. chan,
  188. &iio_show_fixed_type,
  189. NULL,
  190. 0,
  191. 0,
  192. &indio_dev->dev,
  193. &buffer->scan_el_dev_attr_list);
  194. if (ret)
  195. goto error_ret;
  196. attrcount++;
  197. if (chan->type != IIO_TIMESTAMP)
  198. ret = __iio_add_chan_devattr("en",
  199. chan,
  200. &iio_scan_el_show,
  201. &iio_scan_el_store,
  202. chan->scan_index,
  203. 0,
  204. &indio_dev->dev,
  205. &buffer->scan_el_dev_attr_list);
  206. else
  207. ret = __iio_add_chan_devattr("en",
  208. chan,
  209. &iio_scan_el_ts_show,
  210. &iio_scan_el_ts_store,
  211. chan->scan_index,
  212. 0,
  213. &indio_dev->dev,
  214. &buffer->scan_el_dev_attr_list);
  215. attrcount++;
  216. ret = attrcount;
  217. error_ret:
  218. return ret;
  219. }
  220. static void iio_buffer_remove_and_free_scan_dev_attr(struct iio_dev *indio_dev,
  221. struct iio_dev_attr *p)
  222. {
  223. kfree(p->dev_attr.attr.name);
  224. kfree(p);
  225. }
  226. static void __iio_buffer_attr_cleanup(struct iio_dev *indio_dev)
  227. {
  228. struct iio_dev_attr *p, *n;
  229. struct iio_buffer *buffer = indio_dev->buffer;
  230. list_for_each_entry_safe(p, n,
  231. &buffer->scan_el_dev_attr_list, l)
  232. iio_buffer_remove_and_free_scan_dev_attr(indio_dev, p);
  233. }
  234. static const char * const iio_scan_elements_group_name = "scan_elements";
  235. int iio_buffer_register(struct iio_dev *indio_dev,
  236. const struct iio_chan_spec *channels,
  237. int num_channels)
  238. {
  239. struct iio_dev_attr *p;
  240. struct attribute **attr;
  241. struct iio_buffer *buffer = indio_dev->buffer;
  242. int ret, i, attrn, attrcount, attrcount_orig = 0;
  243. if (buffer->attrs)
  244. indio_dev->groups[indio_dev->groupcounter++] = buffer->attrs;
  245. if (buffer->scan_el_attrs != NULL) {
  246. attr = buffer->scan_el_attrs->attrs;
  247. while (*attr++ != NULL)
  248. attrcount_orig++;
  249. }
  250. attrcount = attrcount_orig;
  251. INIT_LIST_HEAD(&buffer->scan_el_dev_attr_list);
  252. if (channels) {
  253. /* new magic */
  254. for (i = 0; i < num_channels; i++) {
  255. /* Establish necessary mask length */
  256. if (channels[i].scan_index >
  257. (int)indio_dev->masklength - 1)
  258. indio_dev->masklength
  259. = indio_dev->channels[i].scan_index + 1;
  260. ret = iio_buffer_add_channel_sysfs(indio_dev,
  261. &channels[i]);
  262. if (ret < 0)
  263. goto error_cleanup_dynamic;
  264. attrcount += ret;
  265. if (channels[i].type == IIO_TIMESTAMP)
  266. indio_dev->scan_index_timestamp =
  267. channels[i].scan_index;
  268. }
  269. if (indio_dev->masklength && buffer->scan_mask == NULL) {
  270. buffer->scan_mask = kcalloc(BITS_TO_LONGS(indio_dev->masklength),
  271. sizeof(*buffer->scan_mask),
  272. GFP_KERNEL);
  273. if (buffer->scan_mask == NULL) {
  274. ret = -ENOMEM;
  275. goto error_cleanup_dynamic;
  276. }
  277. }
  278. }
  279. buffer->scan_el_group.name = iio_scan_elements_group_name;
  280. buffer->scan_el_group.attrs = kcalloc(attrcount + 1,
  281. sizeof(buffer->scan_el_group.attrs[0]),
  282. GFP_KERNEL);
  283. if (buffer->scan_el_group.attrs == NULL) {
  284. ret = -ENOMEM;
  285. goto error_free_scan_mask;
  286. }
  287. if (buffer->scan_el_attrs)
  288. memcpy(buffer->scan_el_group.attrs, buffer->scan_el_attrs,
  289. sizeof(buffer->scan_el_group.attrs[0])*attrcount_orig);
  290. attrn = attrcount_orig;
  291. list_for_each_entry(p, &buffer->scan_el_dev_attr_list, l)
  292. buffer->scan_el_group.attrs[attrn++] = &p->dev_attr.attr;
  293. indio_dev->groups[indio_dev->groupcounter++] = &buffer->scan_el_group;
  294. return 0;
  295. error_free_scan_mask:
  296. kfree(buffer->scan_mask);
  297. error_cleanup_dynamic:
  298. __iio_buffer_attr_cleanup(indio_dev);
  299. return ret;
  300. }
  301. EXPORT_SYMBOL(iio_buffer_register);
  302. void iio_buffer_unregister(struct iio_dev *indio_dev)
  303. {
  304. kfree(indio_dev->buffer->scan_mask);
  305. kfree(indio_dev->buffer->scan_el_group.attrs);
  306. __iio_buffer_attr_cleanup(indio_dev);
  307. }
  308. EXPORT_SYMBOL(iio_buffer_unregister);
  309. ssize_t iio_buffer_read_length(struct device *dev,
  310. struct device_attribute *attr,
  311. char *buf)
  312. {
  313. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  314. struct iio_buffer *buffer = indio_dev->buffer;
  315. if (buffer->access->get_length)
  316. return sprintf(buf, "%d\n",
  317. buffer->access->get_length(buffer));
  318. return 0;
  319. }
  320. EXPORT_SYMBOL(iio_buffer_read_length);
  321. ssize_t iio_buffer_write_length(struct device *dev,
  322. struct device_attribute *attr,
  323. const char *buf,
  324. size_t len)
  325. {
  326. int ret;
  327. ulong val;
  328. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  329. struct iio_buffer *buffer = indio_dev->buffer;
  330. ret = strict_strtoul(buf, 10, &val);
  331. if (ret)
  332. return ret;
  333. if (buffer->access->get_length)
  334. if (val == buffer->access->get_length(buffer))
  335. return len;
  336. mutex_lock(&indio_dev->mlock);
  337. if (iio_buffer_enabled(indio_dev)) {
  338. ret = -EBUSY;
  339. } else {
  340. if (buffer->access->set_length)
  341. buffer->access->set_length(buffer, val);
  342. ret = 0;
  343. }
  344. mutex_unlock(&indio_dev->mlock);
  345. return ret ? ret : len;
  346. }
  347. EXPORT_SYMBOL(iio_buffer_write_length);
  348. ssize_t iio_buffer_store_enable(struct device *dev,
  349. struct device_attribute *attr,
  350. const char *buf,
  351. size_t len)
  352. {
  353. int ret;
  354. bool requested_state, current_state;
  355. int previous_mode;
  356. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  357. struct iio_buffer *buffer = indio_dev->buffer;
  358. mutex_lock(&indio_dev->mlock);
  359. previous_mode = indio_dev->currentmode;
  360. requested_state = !(buf[0] == '0');
  361. current_state = iio_buffer_enabled(indio_dev);
  362. if (current_state == requested_state) {
  363. printk(KERN_INFO "iio-buffer, current state requested again\n");
  364. goto done;
  365. }
  366. if (requested_state) {
  367. if (indio_dev->setup_ops->preenable) {
  368. ret = indio_dev->setup_ops->preenable(indio_dev);
  369. if (ret) {
  370. printk(KERN_ERR
  371. "Buffer not started:"
  372. "buffer preenable failed\n");
  373. goto error_ret;
  374. }
  375. }
  376. if (buffer->access->request_update) {
  377. ret = buffer->access->request_update(buffer);
  378. if (ret) {
  379. printk(KERN_INFO
  380. "Buffer not started:"
  381. "buffer parameter update failed\n");
  382. goto error_ret;
  383. }
  384. }
  385. /* Definitely possible for devices to support both of these.*/
  386. if (indio_dev->modes & INDIO_BUFFER_TRIGGERED) {
  387. if (!indio_dev->trig) {
  388. printk(KERN_INFO
  389. "Buffer not started: no trigger\n");
  390. ret = -EINVAL;
  391. goto error_ret;
  392. }
  393. indio_dev->currentmode = INDIO_BUFFER_TRIGGERED;
  394. } else if (indio_dev->modes & INDIO_BUFFER_HARDWARE)
  395. indio_dev->currentmode = INDIO_BUFFER_HARDWARE;
  396. else { /* should never be reached */
  397. ret = -EINVAL;
  398. goto error_ret;
  399. }
  400. if (indio_dev->setup_ops->postenable) {
  401. ret = indio_dev->setup_ops->postenable(indio_dev);
  402. if (ret) {
  403. printk(KERN_INFO
  404. "Buffer not started:"
  405. "postenable failed\n");
  406. indio_dev->currentmode = previous_mode;
  407. if (indio_dev->setup_ops->postdisable)
  408. indio_dev->setup_ops->
  409. postdisable(indio_dev);
  410. goto error_ret;
  411. }
  412. }
  413. } else {
  414. if (indio_dev->setup_ops->predisable) {
  415. ret = indio_dev->setup_ops->predisable(indio_dev);
  416. if (ret)
  417. goto error_ret;
  418. }
  419. indio_dev->currentmode = INDIO_DIRECT_MODE;
  420. if (indio_dev->setup_ops->postdisable) {
  421. ret = indio_dev->setup_ops->postdisable(indio_dev);
  422. if (ret)
  423. goto error_ret;
  424. }
  425. }
  426. done:
  427. mutex_unlock(&indio_dev->mlock);
  428. return len;
  429. error_ret:
  430. mutex_unlock(&indio_dev->mlock);
  431. return ret;
  432. }
  433. EXPORT_SYMBOL(iio_buffer_store_enable);
  434. ssize_t iio_buffer_show_enable(struct device *dev,
  435. struct device_attribute *attr,
  436. char *buf)
  437. {
  438. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  439. return sprintf(buf, "%d\n", iio_buffer_enabled(indio_dev));
  440. }
  441. EXPORT_SYMBOL(iio_buffer_show_enable);
  442. /* note NULL used as error indicator as it doesn't make sense. */
  443. static const unsigned long *iio_scan_mask_match(const unsigned long *av_masks,
  444. unsigned int masklength,
  445. const unsigned long *mask)
  446. {
  447. if (bitmap_empty(mask, masklength))
  448. return NULL;
  449. while (*av_masks) {
  450. if (bitmap_subset(mask, av_masks, masklength))
  451. return av_masks;
  452. av_masks += BITS_TO_LONGS(masklength);
  453. }
  454. return NULL;
  455. }
  456. static int iio_compute_scan_bytes(struct iio_dev *indio_dev, const long *mask,
  457. bool timestamp)
  458. {
  459. const struct iio_chan_spec *ch;
  460. unsigned bytes = 0;
  461. int length, i;
  462. /* How much space will the demuxed element take? */
  463. for_each_set_bit(i, mask,
  464. indio_dev->masklength) {
  465. ch = iio_find_channel_from_si(indio_dev, i);
  466. length = ch->scan_type.storagebits / 8;
  467. bytes = ALIGN(bytes, length);
  468. bytes += length;
  469. }
  470. if (timestamp) {
  471. ch = iio_find_channel_from_si(indio_dev,
  472. indio_dev->scan_index_timestamp);
  473. length = ch->scan_type.storagebits / 8;
  474. bytes = ALIGN(bytes, length);
  475. bytes += length;
  476. }
  477. return bytes;
  478. }
  479. int iio_sw_buffer_preenable(struct iio_dev *indio_dev)
  480. {
  481. struct iio_buffer *buffer = indio_dev->buffer;
  482. dev_dbg(&indio_dev->dev, "%s\n", __func__);
  483. /* How much space will the demuxed element take? */
  484. indio_dev->scan_bytes =
  485. iio_compute_scan_bytes(indio_dev, buffer->scan_mask,
  486. buffer->scan_timestamp);
  487. buffer->access->set_bytes_per_datum(buffer, indio_dev->scan_bytes);
  488. /* What scan mask do we actually have ?*/
  489. if (indio_dev->available_scan_masks)
  490. indio_dev->active_scan_mask =
  491. iio_scan_mask_match(indio_dev->available_scan_masks,
  492. indio_dev->masklength,
  493. buffer->scan_mask);
  494. else
  495. indio_dev->active_scan_mask = buffer->scan_mask;
  496. iio_update_demux(indio_dev);
  497. if (indio_dev->info->update_scan_mode)
  498. return indio_dev->info
  499. ->update_scan_mode(indio_dev,
  500. indio_dev->active_scan_mask);
  501. return 0;
  502. }
  503. EXPORT_SYMBOL(iio_sw_buffer_preenable);
  504. /**
  505. * iio_scan_mask_set() - set particular bit in the scan mask
  506. * @buffer: the buffer whose scan mask we are interested in
  507. * @bit: the bit to be set.
  508. **/
  509. int iio_scan_mask_set(struct iio_dev *indio_dev,
  510. struct iio_buffer *buffer, int bit)
  511. {
  512. const unsigned long *mask;
  513. unsigned long *trialmask;
  514. trialmask = kmalloc(sizeof(*trialmask)*
  515. BITS_TO_LONGS(indio_dev->masklength),
  516. GFP_KERNEL);
  517. if (trialmask == NULL)
  518. return -ENOMEM;
  519. if (!indio_dev->masklength) {
  520. WARN_ON("trying to set scanmask prior to registering buffer\n");
  521. kfree(trialmask);
  522. return -EINVAL;
  523. }
  524. bitmap_copy(trialmask, buffer->scan_mask, indio_dev->masklength);
  525. set_bit(bit, trialmask);
  526. if (indio_dev->available_scan_masks) {
  527. mask = iio_scan_mask_match(indio_dev->available_scan_masks,
  528. indio_dev->masklength,
  529. trialmask);
  530. if (!mask) {
  531. kfree(trialmask);
  532. return -EINVAL;
  533. }
  534. }
  535. bitmap_copy(buffer->scan_mask, trialmask, indio_dev->masklength);
  536. kfree(trialmask);
  537. return 0;
  538. };
  539. EXPORT_SYMBOL_GPL(iio_scan_mask_set);
  540. int iio_scan_mask_query(struct iio_dev *indio_dev,
  541. struct iio_buffer *buffer, int bit)
  542. {
  543. if (bit > indio_dev->masklength)
  544. return -EINVAL;
  545. if (!buffer->scan_mask)
  546. return 0;
  547. return test_bit(bit, buffer->scan_mask);
  548. };
  549. EXPORT_SYMBOL_GPL(iio_scan_mask_query);
  550. /**
  551. * struct iio_demux_table() - table describing demux memcpy ops
  552. * @from: index to copy from
  553. * @to: index to copy to
  554. * @length: how many bytes to copy
  555. * @l: list head used for management
  556. */
  557. struct iio_demux_table {
  558. unsigned from;
  559. unsigned to;
  560. unsigned length;
  561. struct list_head l;
  562. };
  563. static unsigned char *iio_demux(struct iio_buffer *buffer,
  564. unsigned char *datain)
  565. {
  566. struct iio_demux_table *t;
  567. if (list_empty(&buffer->demux_list))
  568. return datain;
  569. list_for_each_entry(t, &buffer->demux_list, l)
  570. memcpy(buffer->demux_bounce + t->to,
  571. datain + t->from, t->length);
  572. return buffer->demux_bounce;
  573. }
  574. int iio_push_to_buffer(struct iio_buffer *buffer, unsigned char *data,
  575. s64 timestamp)
  576. {
  577. unsigned char *dataout = iio_demux(buffer, data);
  578. return buffer->access->store_to(buffer, dataout, timestamp);
  579. }
  580. EXPORT_SYMBOL_GPL(iio_push_to_buffer);
  581. static void iio_buffer_demux_free(struct iio_buffer *buffer)
  582. {
  583. struct iio_demux_table *p, *q;
  584. list_for_each_entry_safe(p, q, &buffer->demux_list, l) {
  585. list_del(&p->l);
  586. kfree(p);
  587. }
  588. }
  589. int iio_update_demux(struct iio_dev *indio_dev)
  590. {
  591. const struct iio_chan_spec *ch;
  592. struct iio_buffer *buffer = indio_dev->buffer;
  593. int ret, in_ind = -1, out_ind, length;
  594. unsigned in_loc = 0, out_loc = 0;
  595. struct iio_demux_table *p;
  596. /* Clear out any old demux */
  597. iio_buffer_demux_free(buffer);
  598. kfree(buffer->demux_bounce);
  599. buffer->demux_bounce = NULL;
  600. /* First work out which scan mode we will actually have */
  601. if (bitmap_equal(indio_dev->active_scan_mask,
  602. buffer->scan_mask,
  603. indio_dev->masklength))
  604. return 0;
  605. /* Now we have the two masks, work from least sig and build up sizes */
  606. for_each_set_bit(out_ind,
  607. indio_dev->active_scan_mask,
  608. indio_dev->masklength) {
  609. in_ind = find_next_bit(indio_dev->active_scan_mask,
  610. indio_dev->masklength,
  611. in_ind + 1);
  612. while (in_ind != out_ind) {
  613. in_ind = find_next_bit(indio_dev->active_scan_mask,
  614. indio_dev->masklength,
  615. in_ind + 1);
  616. ch = iio_find_channel_from_si(indio_dev, in_ind);
  617. length = ch->scan_type.storagebits/8;
  618. /* Make sure we are aligned */
  619. in_loc += length;
  620. if (in_loc % length)
  621. in_loc += length - in_loc % length;
  622. }
  623. p = kmalloc(sizeof(*p), GFP_KERNEL);
  624. if (p == NULL) {
  625. ret = -ENOMEM;
  626. goto error_clear_mux_table;
  627. }
  628. ch = iio_find_channel_from_si(indio_dev, in_ind);
  629. length = ch->scan_type.storagebits/8;
  630. if (out_loc % length)
  631. out_loc += length - out_loc % length;
  632. if (in_loc % length)
  633. in_loc += length - in_loc % length;
  634. p->from = in_loc;
  635. p->to = out_loc;
  636. p->length = length;
  637. list_add_tail(&p->l, &buffer->demux_list);
  638. out_loc += length;
  639. in_loc += length;
  640. }
  641. /* Relies on scan_timestamp being last */
  642. if (buffer->scan_timestamp) {
  643. p = kmalloc(sizeof(*p), GFP_KERNEL);
  644. if (p == NULL) {
  645. ret = -ENOMEM;
  646. goto error_clear_mux_table;
  647. }
  648. ch = iio_find_channel_from_si(indio_dev,
  649. indio_dev->scan_index_timestamp);
  650. length = ch->scan_type.storagebits/8;
  651. if (out_loc % length)
  652. out_loc += length - out_loc % length;
  653. if (in_loc % length)
  654. in_loc += length - in_loc % length;
  655. p->from = in_loc;
  656. p->to = out_loc;
  657. p->length = length;
  658. list_add_tail(&p->l, &buffer->demux_list);
  659. out_loc += length;
  660. in_loc += length;
  661. }
  662. buffer->demux_bounce = kzalloc(out_loc, GFP_KERNEL);
  663. if (buffer->demux_bounce == NULL) {
  664. ret = -ENOMEM;
  665. goto error_clear_mux_table;
  666. }
  667. return 0;
  668. error_clear_mux_table:
  669. iio_buffer_demux_free(buffer);
  670. return ret;
  671. }
  672. EXPORT_SYMBOL_GPL(iio_update_demux);