industrialio-buffer.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791
  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. = 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_validate_scan_mask_onehot() - Validates that exactly one channel is selected
  510. * @indio_dev: the iio device
  511. * @mask: scan mask to be checked
  512. *
  513. * Return true if exactly one bit is set in the scan mask, false otherwise. It
  514. * can be used for devices where only one channel can be active for sampling at
  515. * a time.
  516. */
  517. bool iio_validate_scan_mask_onehot(struct iio_dev *indio_dev,
  518. const unsigned long *mask)
  519. {
  520. return bitmap_weight(mask, indio_dev->masklength) == 1;
  521. }
  522. EXPORT_SYMBOL_GPL(iio_validate_scan_mask_onehot);
  523. static bool iio_validate_scan_mask(struct iio_dev *indio_dev,
  524. const unsigned long *mask)
  525. {
  526. if (!indio_dev->setup_ops->validate_scan_mask)
  527. return true;
  528. return indio_dev->setup_ops->validate_scan_mask(indio_dev, mask);
  529. }
  530. /**
  531. * iio_scan_mask_set() - set particular bit in the scan mask
  532. * @buffer: the buffer whose scan mask we are interested in
  533. * @bit: the bit to be set.
  534. **/
  535. int iio_scan_mask_set(struct iio_dev *indio_dev,
  536. struct iio_buffer *buffer, int bit)
  537. {
  538. const unsigned long *mask;
  539. unsigned long *trialmask;
  540. trialmask = kmalloc(sizeof(*trialmask)*
  541. BITS_TO_LONGS(indio_dev->masklength),
  542. GFP_KERNEL);
  543. if (trialmask == NULL)
  544. return -ENOMEM;
  545. if (!indio_dev->masklength) {
  546. WARN_ON("trying to set scanmask prior to registering buffer\n");
  547. goto err_invalid_mask;
  548. }
  549. bitmap_copy(trialmask, buffer->scan_mask, indio_dev->masklength);
  550. set_bit(bit, trialmask);
  551. if (!iio_validate_scan_mask(indio_dev, trialmask))
  552. goto err_invalid_mask;
  553. if (indio_dev->available_scan_masks) {
  554. mask = iio_scan_mask_match(indio_dev->available_scan_masks,
  555. indio_dev->masklength,
  556. trialmask);
  557. if (!mask)
  558. goto err_invalid_mask;
  559. }
  560. bitmap_copy(buffer->scan_mask, trialmask, indio_dev->masklength);
  561. kfree(trialmask);
  562. return 0;
  563. err_invalid_mask:
  564. kfree(trialmask);
  565. return -EINVAL;
  566. }
  567. EXPORT_SYMBOL_GPL(iio_scan_mask_set);
  568. int iio_scan_mask_query(struct iio_dev *indio_dev,
  569. struct iio_buffer *buffer, int bit)
  570. {
  571. if (bit > indio_dev->masklength)
  572. return -EINVAL;
  573. if (!buffer->scan_mask)
  574. return 0;
  575. return test_bit(bit, buffer->scan_mask);
  576. };
  577. EXPORT_SYMBOL_GPL(iio_scan_mask_query);
  578. /**
  579. * struct iio_demux_table() - table describing demux memcpy ops
  580. * @from: index to copy from
  581. * @to: index to copy to
  582. * @length: how many bytes to copy
  583. * @l: list head used for management
  584. */
  585. struct iio_demux_table {
  586. unsigned from;
  587. unsigned to;
  588. unsigned length;
  589. struct list_head l;
  590. };
  591. static unsigned char *iio_demux(struct iio_buffer *buffer,
  592. unsigned char *datain)
  593. {
  594. struct iio_demux_table *t;
  595. if (list_empty(&buffer->demux_list))
  596. return datain;
  597. list_for_each_entry(t, &buffer->demux_list, l)
  598. memcpy(buffer->demux_bounce + t->to,
  599. datain + t->from, t->length);
  600. return buffer->demux_bounce;
  601. }
  602. int iio_push_to_buffer(struct iio_buffer *buffer, unsigned char *data,
  603. s64 timestamp)
  604. {
  605. unsigned char *dataout = iio_demux(buffer, data);
  606. return buffer->access->store_to(buffer, dataout, timestamp);
  607. }
  608. EXPORT_SYMBOL_GPL(iio_push_to_buffer);
  609. static void iio_buffer_demux_free(struct iio_buffer *buffer)
  610. {
  611. struct iio_demux_table *p, *q;
  612. list_for_each_entry_safe(p, q, &buffer->demux_list, l) {
  613. list_del(&p->l);
  614. kfree(p);
  615. }
  616. }
  617. int iio_update_demux(struct iio_dev *indio_dev)
  618. {
  619. const struct iio_chan_spec *ch;
  620. struct iio_buffer *buffer = indio_dev->buffer;
  621. int ret, in_ind = -1, out_ind, length;
  622. unsigned in_loc = 0, out_loc = 0;
  623. struct iio_demux_table *p;
  624. /* Clear out any old demux */
  625. iio_buffer_demux_free(buffer);
  626. kfree(buffer->demux_bounce);
  627. buffer->demux_bounce = NULL;
  628. /* First work out which scan mode we will actually have */
  629. if (bitmap_equal(indio_dev->active_scan_mask,
  630. buffer->scan_mask,
  631. indio_dev->masklength))
  632. return 0;
  633. /* Now we have the two masks, work from least sig and build up sizes */
  634. for_each_set_bit(out_ind,
  635. indio_dev->active_scan_mask,
  636. indio_dev->masklength) {
  637. in_ind = find_next_bit(indio_dev->active_scan_mask,
  638. indio_dev->masklength,
  639. in_ind + 1);
  640. while (in_ind != out_ind) {
  641. in_ind = find_next_bit(indio_dev->active_scan_mask,
  642. indio_dev->masklength,
  643. in_ind + 1);
  644. ch = iio_find_channel_from_si(indio_dev, in_ind);
  645. length = ch->scan_type.storagebits/8;
  646. /* Make sure we are aligned */
  647. in_loc += length;
  648. if (in_loc % length)
  649. in_loc += length - in_loc % length;
  650. }
  651. p = kmalloc(sizeof(*p), GFP_KERNEL);
  652. if (p == NULL) {
  653. ret = -ENOMEM;
  654. goto error_clear_mux_table;
  655. }
  656. ch = iio_find_channel_from_si(indio_dev, in_ind);
  657. length = ch->scan_type.storagebits/8;
  658. if (out_loc % length)
  659. out_loc += length - out_loc % length;
  660. if (in_loc % length)
  661. in_loc += length - in_loc % length;
  662. p->from = in_loc;
  663. p->to = out_loc;
  664. p->length = length;
  665. list_add_tail(&p->l, &buffer->demux_list);
  666. out_loc += length;
  667. in_loc += length;
  668. }
  669. /* Relies on scan_timestamp being last */
  670. if (buffer->scan_timestamp) {
  671. p = kmalloc(sizeof(*p), GFP_KERNEL);
  672. if (p == NULL) {
  673. ret = -ENOMEM;
  674. goto error_clear_mux_table;
  675. }
  676. ch = iio_find_channel_from_si(indio_dev,
  677. indio_dev->scan_index_timestamp);
  678. length = ch->scan_type.storagebits/8;
  679. if (out_loc % length)
  680. out_loc += length - out_loc % length;
  681. if (in_loc % length)
  682. in_loc += length - in_loc % length;
  683. p->from = in_loc;
  684. p->to = out_loc;
  685. p->length = length;
  686. list_add_tail(&p->l, &buffer->demux_list);
  687. out_loc += length;
  688. in_loc += length;
  689. }
  690. buffer->demux_bounce = kzalloc(out_loc, GFP_KERNEL);
  691. if (buffer->demux_bounce == NULL) {
  692. ret = -ENOMEM;
  693. goto error_clear_mux_table;
  694. }
  695. return 0;
  696. error_clear_mux_table:
  697. iio_buffer_demux_free(buffer);
  698. return ret;
  699. }
  700. EXPORT_SYMBOL_GPL(iio_update_demux);