industrialio-buffer.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  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. if (channels[i].scan_index < 0)
  256. continue;
  257. /* Establish necessary mask length */
  258. if (channels[i].scan_index >
  259. (int)indio_dev->masklength - 1)
  260. indio_dev->masklength
  261. = indio_dev->channels[i].scan_index + 1;
  262. ret = iio_buffer_add_channel_sysfs(indio_dev,
  263. &channels[i]);
  264. if (ret < 0)
  265. goto error_cleanup_dynamic;
  266. attrcount += ret;
  267. if (channels[i].type == IIO_TIMESTAMP)
  268. indio_dev->scan_index_timestamp =
  269. channels[i].scan_index;
  270. }
  271. if (indio_dev->masklength && buffer->scan_mask == NULL) {
  272. buffer->scan_mask = kcalloc(BITS_TO_LONGS(indio_dev->masklength),
  273. sizeof(*buffer->scan_mask),
  274. GFP_KERNEL);
  275. if (buffer->scan_mask == NULL) {
  276. ret = -ENOMEM;
  277. goto error_cleanup_dynamic;
  278. }
  279. }
  280. }
  281. buffer->scan_el_group.name = iio_scan_elements_group_name;
  282. buffer->scan_el_group.attrs = kcalloc(attrcount + 1,
  283. sizeof(buffer->scan_el_group.attrs[0]),
  284. GFP_KERNEL);
  285. if (buffer->scan_el_group.attrs == NULL) {
  286. ret = -ENOMEM;
  287. goto error_free_scan_mask;
  288. }
  289. if (buffer->scan_el_attrs)
  290. memcpy(buffer->scan_el_group.attrs, buffer->scan_el_attrs,
  291. sizeof(buffer->scan_el_group.attrs[0])*attrcount_orig);
  292. attrn = attrcount_orig;
  293. list_for_each_entry(p, &buffer->scan_el_dev_attr_list, l)
  294. buffer->scan_el_group.attrs[attrn++] = &p->dev_attr.attr;
  295. indio_dev->groups[indio_dev->groupcounter++] = &buffer->scan_el_group;
  296. return 0;
  297. error_free_scan_mask:
  298. kfree(buffer->scan_mask);
  299. error_cleanup_dynamic:
  300. __iio_buffer_attr_cleanup(indio_dev);
  301. return ret;
  302. }
  303. EXPORT_SYMBOL(iio_buffer_register);
  304. void iio_buffer_unregister(struct iio_dev *indio_dev)
  305. {
  306. kfree(indio_dev->buffer->scan_mask);
  307. kfree(indio_dev->buffer->scan_el_group.attrs);
  308. __iio_buffer_attr_cleanup(indio_dev);
  309. }
  310. EXPORT_SYMBOL(iio_buffer_unregister);
  311. ssize_t iio_buffer_read_length(struct device *dev,
  312. struct device_attribute *attr,
  313. char *buf)
  314. {
  315. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  316. struct iio_buffer *buffer = indio_dev->buffer;
  317. if (buffer->access->get_length)
  318. return sprintf(buf, "%d\n",
  319. buffer->access->get_length(buffer));
  320. return 0;
  321. }
  322. EXPORT_SYMBOL(iio_buffer_read_length);
  323. ssize_t iio_buffer_write_length(struct device *dev,
  324. struct device_attribute *attr,
  325. const char *buf,
  326. size_t len)
  327. {
  328. int ret;
  329. ulong val;
  330. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  331. struct iio_buffer *buffer = indio_dev->buffer;
  332. ret = strict_strtoul(buf, 10, &val);
  333. if (ret)
  334. return ret;
  335. if (buffer->access->get_length)
  336. if (val == buffer->access->get_length(buffer))
  337. return len;
  338. mutex_lock(&indio_dev->mlock);
  339. if (iio_buffer_enabled(indio_dev)) {
  340. ret = -EBUSY;
  341. } else {
  342. if (buffer->access->set_length)
  343. buffer->access->set_length(buffer, val);
  344. ret = 0;
  345. }
  346. mutex_unlock(&indio_dev->mlock);
  347. return ret ? ret : len;
  348. }
  349. EXPORT_SYMBOL(iio_buffer_write_length);
  350. ssize_t iio_buffer_store_enable(struct device *dev,
  351. struct device_attribute *attr,
  352. const char *buf,
  353. size_t len)
  354. {
  355. int ret;
  356. bool requested_state, current_state;
  357. int previous_mode;
  358. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  359. struct iio_buffer *buffer = indio_dev->buffer;
  360. mutex_lock(&indio_dev->mlock);
  361. previous_mode = indio_dev->currentmode;
  362. requested_state = !(buf[0] == '0');
  363. current_state = iio_buffer_enabled(indio_dev);
  364. if (current_state == requested_state) {
  365. printk(KERN_INFO "iio-buffer, current state requested again\n");
  366. goto done;
  367. }
  368. if (requested_state) {
  369. if (indio_dev->setup_ops->preenable) {
  370. ret = indio_dev->setup_ops->preenable(indio_dev);
  371. if (ret) {
  372. printk(KERN_ERR
  373. "Buffer not started:"
  374. "buffer preenable failed\n");
  375. goto error_ret;
  376. }
  377. }
  378. if (buffer->access->request_update) {
  379. ret = buffer->access->request_update(buffer);
  380. if (ret) {
  381. printk(KERN_INFO
  382. "Buffer not started:"
  383. "buffer parameter update failed\n");
  384. goto error_ret;
  385. }
  386. }
  387. /* Definitely possible for devices to support both of these.*/
  388. if (indio_dev->modes & INDIO_BUFFER_TRIGGERED) {
  389. if (!indio_dev->trig) {
  390. printk(KERN_INFO
  391. "Buffer not started: no trigger\n");
  392. ret = -EINVAL;
  393. goto error_ret;
  394. }
  395. indio_dev->currentmode = INDIO_BUFFER_TRIGGERED;
  396. } else if (indio_dev->modes & INDIO_BUFFER_HARDWARE)
  397. indio_dev->currentmode = INDIO_BUFFER_HARDWARE;
  398. else { /* should never be reached */
  399. ret = -EINVAL;
  400. goto error_ret;
  401. }
  402. if (indio_dev->setup_ops->postenable) {
  403. ret = indio_dev->setup_ops->postenable(indio_dev);
  404. if (ret) {
  405. printk(KERN_INFO
  406. "Buffer not started:"
  407. "postenable failed\n");
  408. indio_dev->currentmode = previous_mode;
  409. if (indio_dev->setup_ops->postdisable)
  410. indio_dev->setup_ops->
  411. postdisable(indio_dev);
  412. goto error_ret;
  413. }
  414. }
  415. } else {
  416. if (indio_dev->setup_ops->predisable) {
  417. ret = indio_dev->setup_ops->predisable(indio_dev);
  418. if (ret)
  419. goto error_ret;
  420. }
  421. indio_dev->currentmode = INDIO_DIRECT_MODE;
  422. if (indio_dev->setup_ops->postdisable) {
  423. ret = indio_dev->setup_ops->postdisable(indio_dev);
  424. if (ret)
  425. goto error_ret;
  426. }
  427. }
  428. done:
  429. mutex_unlock(&indio_dev->mlock);
  430. return len;
  431. error_ret:
  432. mutex_unlock(&indio_dev->mlock);
  433. return ret;
  434. }
  435. EXPORT_SYMBOL(iio_buffer_store_enable);
  436. ssize_t iio_buffer_show_enable(struct device *dev,
  437. struct device_attribute *attr,
  438. char *buf)
  439. {
  440. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  441. return sprintf(buf, "%d\n", iio_buffer_enabled(indio_dev));
  442. }
  443. EXPORT_SYMBOL(iio_buffer_show_enable);
  444. /* note NULL used as error indicator as it doesn't make sense. */
  445. static const unsigned long *iio_scan_mask_match(const unsigned long *av_masks,
  446. unsigned int masklength,
  447. const unsigned long *mask)
  448. {
  449. if (bitmap_empty(mask, masklength))
  450. return NULL;
  451. while (*av_masks) {
  452. if (bitmap_subset(mask, av_masks, masklength))
  453. return av_masks;
  454. av_masks += BITS_TO_LONGS(masklength);
  455. }
  456. return NULL;
  457. }
  458. static int iio_compute_scan_bytes(struct iio_dev *indio_dev, const long *mask,
  459. bool timestamp)
  460. {
  461. const struct iio_chan_spec *ch;
  462. unsigned bytes = 0;
  463. int length, i;
  464. /* How much space will the demuxed element take? */
  465. for_each_set_bit(i, mask,
  466. indio_dev->masklength) {
  467. ch = iio_find_channel_from_si(indio_dev, i);
  468. length = ch->scan_type.storagebits / 8;
  469. bytes = ALIGN(bytes, length);
  470. bytes += length;
  471. }
  472. if (timestamp) {
  473. ch = iio_find_channel_from_si(indio_dev,
  474. indio_dev->scan_index_timestamp);
  475. length = ch->scan_type.storagebits / 8;
  476. bytes = ALIGN(bytes, length);
  477. bytes += length;
  478. }
  479. return bytes;
  480. }
  481. int iio_sw_buffer_preenable(struct iio_dev *indio_dev)
  482. {
  483. struct iio_buffer *buffer = indio_dev->buffer;
  484. dev_dbg(&indio_dev->dev, "%s\n", __func__);
  485. /* How much space will the demuxed element take? */
  486. indio_dev->scan_bytes =
  487. iio_compute_scan_bytes(indio_dev, buffer->scan_mask,
  488. buffer->scan_timestamp);
  489. buffer->access->set_bytes_per_datum(buffer, indio_dev->scan_bytes);
  490. /* What scan mask do we actually have ?*/
  491. if (indio_dev->available_scan_masks)
  492. indio_dev->active_scan_mask =
  493. iio_scan_mask_match(indio_dev->available_scan_masks,
  494. indio_dev->masklength,
  495. buffer->scan_mask);
  496. else
  497. indio_dev->active_scan_mask = buffer->scan_mask;
  498. if (indio_dev->active_scan_mask == NULL)
  499. return -EINVAL;
  500. iio_update_demux(indio_dev);
  501. if (indio_dev->info->update_scan_mode)
  502. return indio_dev->info
  503. ->update_scan_mode(indio_dev,
  504. indio_dev->active_scan_mask);
  505. return 0;
  506. }
  507. EXPORT_SYMBOL(iio_sw_buffer_preenable);
  508. /**
  509. * iio_scan_mask_set() - set particular bit in the scan mask
  510. * @buffer: the buffer whose scan mask we are interested in
  511. * @bit: the bit to be set.
  512. **/
  513. int iio_scan_mask_set(struct iio_dev *indio_dev,
  514. struct iio_buffer *buffer, int bit)
  515. {
  516. const unsigned long *mask;
  517. unsigned long *trialmask;
  518. trialmask = kmalloc(sizeof(*trialmask)*
  519. BITS_TO_LONGS(indio_dev->masklength),
  520. GFP_KERNEL);
  521. if (trialmask == NULL)
  522. return -ENOMEM;
  523. if (!indio_dev->masklength) {
  524. WARN_ON("trying to set scanmask prior to registering buffer\n");
  525. kfree(trialmask);
  526. return -EINVAL;
  527. }
  528. bitmap_copy(trialmask, buffer->scan_mask, indio_dev->masklength);
  529. set_bit(bit, trialmask);
  530. if (indio_dev->available_scan_masks) {
  531. mask = iio_scan_mask_match(indio_dev->available_scan_masks,
  532. indio_dev->masklength,
  533. trialmask);
  534. if (!mask) {
  535. kfree(trialmask);
  536. return -EINVAL;
  537. }
  538. }
  539. bitmap_copy(buffer->scan_mask, trialmask, indio_dev->masklength);
  540. kfree(trialmask);
  541. return 0;
  542. };
  543. EXPORT_SYMBOL_GPL(iio_scan_mask_set);
  544. int iio_scan_mask_query(struct iio_dev *indio_dev,
  545. struct iio_buffer *buffer, int bit)
  546. {
  547. if (bit > indio_dev->masklength)
  548. return -EINVAL;
  549. if (!buffer->scan_mask)
  550. return 0;
  551. return test_bit(bit, buffer->scan_mask);
  552. };
  553. EXPORT_SYMBOL_GPL(iio_scan_mask_query);
  554. /**
  555. * struct iio_demux_table() - table describing demux memcpy ops
  556. * @from: index to copy from
  557. * @to: index to copy to
  558. * @length: how many bytes to copy
  559. * @l: list head used for management
  560. */
  561. struct iio_demux_table {
  562. unsigned from;
  563. unsigned to;
  564. unsigned length;
  565. struct list_head l;
  566. };
  567. static unsigned char *iio_demux(struct iio_buffer *buffer,
  568. unsigned char *datain)
  569. {
  570. struct iio_demux_table *t;
  571. if (list_empty(&buffer->demux_list))
  572. return datain;
  573. list_for_each_entry(t, &buffer->demux_list, l)
  574. memcpy(buffer->demux_bounce + t->to,
  575. datain + t->from, t->length);
  576. return buffer->demux_bounce;
  577. }
  578. int iio_push_to_buffer(struct iio_buffer *buffer, unsigned char *data,
  579. s64 timestamp)
  580. {
  581. unsigned char *dataout = iio_demux(buffer, data);
  582. return buffer->access->store_to(buffer, dataout, timestamp);
  583. }
  584. EXPORT_SYMBOL_GPL(iio_push_to_buffer);
  585. static void iio_buffer_demux_free(struct iio_buffer *buffer)
  586. {
  587. struct iio_demux_table *p, *q;
  588. list_for_each_entry_safe(p, q, &buffer->demux_list, l) {
  589. list_del(&p->l);
  590. kfree(p);
  591. }
  592. }
  593. int iio_update_demux(struct iio_dev *indio_dev)
  594. {
  595. const struct iio_chan_spec *ch;
  596. struct iio_buffer *buffer = indio_dev->buffer;
  597. int ret, in_ind = -1, out_ind, length;
  598. unsigned in_loc = 0, out_loc = 0;
  599. struct iio_demux_table *p;
  600. /* Clear out any old demux */
  601. iio_buffer_demux_free(buffer);
  602. kfree(buffer->demux_bounce);
  603. buffer->demux_bounce = NULL;
  604. /* First work out which scan mode we will actually have */
  605. if (bitmap_equal(indio_dev->active_scan_mask,
  606. buffer->scan_mask,
  607. indio_dev->masklength))
  608. return 0;
  609. /* Now we have the two masks, work from least sig and build up sizes */
  610. for_each_set_bit(out_ind,
  611. indio_dev->active_scan_mask,
  612. indio_dev->masklength) {
  613. in_ind = find_next_bit(indio_dev->active_scan_mask,
  614. indio_dev->masklength,
  615. in_ind + 1);
  616. while (in_ind != out_ind) {
  617. in_ind = find_next_bit(indio_dev->active_scan_mask,
  618. indio_dev->masklength,
  619. in_ind + 1);
  620. ch = iio_find_channel_from_si(indio_dev, in_ind);
  621. length = ch->scan_type.storagebits/8;
  622. /* Make sure we are aligned */
  623. in_loc += length;
  624. if (in_loc % length)
  625. in_loc += length - in_loc % length;
  626. }
  627. p = kmalloc(sizeof(*p), GFP_KERNEL);
  628. if (p == NULL) {
  629. ret = -ENOMEM;
  630. goto error_clear_mux_table;
  631. }
  632. ch = iio_find_channel_from_si(indio_dev, in_ind);
  633. length = ch->scan_type.storagebits/8;
  634. if (out_loc % length)
  635. out_loc += length - out_loc % length;
  636. if (in_loc % length)
  637. in_loc += length - in_loc % length;
  638. p->from = in_loc;
  639. p->to = out_loc;
  640. p->length = length;
  641. list_add_tail(&p->l, &buffer->demux_list);
  642. out_loc += length;
  643. in_loc += length;
  644. }
  645. /* Relies on scan_timestamp being last */
  646. if (buffer->scan_timestamp) {
  647. p = kmalloc(sizeof(*p), GFP_KERNEL);
  648. if (p == NULL) {
  649. ret = -ENOMEM;
  650. goto error_clear_mux_table;
  651. }
  652. ch = iio_find_channel_from_si(indio_dev,
  653. indio_dev->scan_index_timestamp);
  654. length = ch->scan_type.storagebits/8;
  655. if (out_loc % length)
  656. out_loc += length - out_loc % length;
  657. if (in_loc % length)
  658. in_loc += length - in_loc % length;
  659. p->from = in_loc;
  660. p->to = out_loc;
  661. p->length = length;
  662. list_add_tail(&p->l, &buffer->demux_list);
  663. out_loc += length;
  664. in_loc += length;
  665. }
  666. buffer->demux_bounce = kzalloc(out_loc, GFP_KERNEL);
  667. if (buffer->demux_bounce == NULL) {
  668. ret = -ENOMEM;
  669. goto error_clear_mux_table;
  670. }
  671. return 0;
  672. error_clear_mux_table:
  673. iio_buffer_demux_free(buffer);
  674. return ret;
  675. }
  676. EXPORT_SYMBOL_GPL(iio_update_demux);