industrialio-buffer.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936
  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. static bool iio_buffer_is_active(struct iio_dev *indio_dev,
  32. struct iio_buffer *buf)
  33. {
  34. struct list_head *p;
  35. list_for_each(p, &indio_dev->buffer_list)
  36. if (p == &buf->buffer_list)
  37. return true;
  38. return false;
  39. }
  40. /**
  41. * iio_buffer_read_first_n_outer() - chrdev read for buffer access
  42. *
  43. * This function relies on all buffer implementations having an
  44. * iio_buffer as their first element.
  45. **/
  46. ssize_t iio_buffer_read_first_n_outer(struct file *filp, char __user *buf,
  47. size_t n, loff_t *f_ps)
  48. {
  49. struct iio_dev *indio_dev = filp->private_data;
  50. struct iio_buffer *rb = indio_dev->buffer;
  51. if (!rb || !rb->access->read_first_n)
  52. return -EINVAL;
  53. return rb->access->read_first_n(rb, n, buf);
  54. }
  55. /**
  56. * iio_buffer_poll() - poll the buffer to find out if it has data
  57. */
  58. unsigned int iio_buffer_poll(struct file *filp,
  59. struct poll_table_struct *wait)
  60. {
  61. struct iio_dev *indio_dev = filp->private_data;
  62. struct iio_buffer *rb = indio_dev->buffer;
  63. poll_wait(filp, &rb->pollq, wait);
  64. if (rb->stufftoread)
  65. return POLLIN | POLLRDNORM;
  66. /* need a way of knowing if there may be enough data... */
  67. return 0;
  68. }
  69. void iio_buffer_init(struct iio_buffer *buffer)
  70. {
  71. INIT_LIST_HEAD(&buffer->demux_list);
  72. init_waitqueue_head(&buffer->pollq);
  73. }
  74. EXPORT_SYMBOL(iio_buffer_init);
  75. static ssize_t iio_show_scan_index(struct device *dev,
  76. struct device_attribute *attr,
  77. char *buf)
  78. {
  79. return sprintf(buf, "%u\n", to_iio_dev_attr(attr)->c->scan_index);
  80. }
  81. static ssize_t iio_show_fixed_type(struct device *dev,
  82. struct device_attribute *attr,
  83. char *buf)
  84. {
  85. struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
  86. u8 type = this_attr->c->scan_type.endianness;
  87. if (type == IIO_CPU) {
  88. #ifdef __LITTLE_ENDIAN
  89. type = IIO_LE;
  90. #else
  91. type = IIO_BE;
  92. #endif
  93. }
  94. return sprintf(buf, "%s:%c%d/%d>>%u\n",
  95. iio_endian_prefix[type],
  96. this_attr->c->scan_type.sign,
  97. this_attr->c->scan_type.realbits,
  98. this_attr->c->scan_type.storagebits,
  99. this_attr->c->scan_type.shift);
  100. }
  101. static ssize_t iio_scan_el_show(struct device *dev,
  102. struct device_attribute *attr,
  103. char *buf)
  104. {
  105. int ret;
  106. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  107. ret = test_bit(to_iio_dev_attr(attr)->address,
  108. indio_dev->buffer->scan_mask);
  109. return sprintf(buf, "%d\n", ret);
  110. }
  111. static int iio_scan_mask_clear(struct iio_buffer *buffer, int bit)
  112. {
  113. clear_bit(bit, buffer->scan_mask);
  114. return 0;
  115. }
  116. static ssize_t iio_scan_el_store(struct device *dev,
  117. struct device_attribute *attr,
  118. const char *buf,
  119. size_t len)
  120. {
  121. int ret;
  122. bool state;
  123. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  124. struct iio_buffer *buffer = indio_dev->buffer;
  125. struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
  126. ret = strtobool(buf, &state);
  127. if (ret < 0)
  128. return ret;
  129. mutex_lock(&indio_dev->mlock);
  130. if (iio_buffer_is_active(indio_dev, indio_dev->buffer)) {
  131. ret = -EBUSY;
  132. goto error_ret;
  133. }
  134. ret = iio_scan_mask_query(indio_dev, buffer, this_attr->address);
  135. if (ret < 0)
  136. goto error_ret;
  137. if (!state && ret) {
  138. ret = iio_scan_mask_clear(buffer, this_attr->address);
  139. if (ret)
  140. goto error_ret;
  141. } else if (state && !ret) {
  142. ret = iio_scan_mask_set(indio_dev, buffer, this_attr->address);
  143. if (ret)
  144. goto error_ret;
  145. }
  146. error_ret:
  147. mutex_unlock(&indio_dev->mlock);
  148. return ret < 0 ? ret : len;
  149. }
  150. static ssize_t iio_scan_el_ts_show(struct device *dev,
  151. struct device_attribute *attr,
  152. char *buf)
  153. {
  154. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  155. return sprintf(buf, "%d\n", indio_dev->buffer->scan_timestamp);
  156. }
  157. static ssize_t iio_scan_el_ts_store(struct device *dev,
  158. struct device_attribute *attr,
  159. const char *buf,
  160. size_t len)
  161. {
  162. int ret;
  163. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  164. bool state;
  165. ret = strtobool(buf, &state);
  166. if (ret < 0)
  167. return ret;
  168. mutex_lock(&indio_dev->mlock);
  169. if (iio_buffer_is_active(indio_dev, indio_dev->buffer)) {
  170. ret = -EBUSY;
  171. goto error_ret;
  172. }
  173. indio_dev->buffer->scan_timestamp = state;
  174. error_ret:
  175. mutex_unlock(&indio_dev->mlock);
  176. return ret ? ret : len;
  177. }
  178. static int iio_buffer_add_channel_sysfs(struct iio_dev *indio_dev,
  179. const struct iio_chan_spec *chan)
  180. {
  181. int ret, attrcount = 0;
  182. struct iio_buffer *buffer = indio_dev->buffer;
  183. ret = __iio_add_chan_devattr("index",
  184. chan,
  185. &iio_show_scan_index,
  186. NULL,
  187. 0,
  188. 0,
  189. &indio_dev->dev,
  190. &buffer->scan_el_dev_attr_list);
  191. if (ret)
  192. goto error_ret;
  193. attrcount++;
  194. ret = __iio_add_chan_devattr("type",
  195. chan,
  196. &iio_show_fixed_type,
  197. NULL,
  198. 0,
  199. 0,
  200. &indio_dev->dev,
  201. &buffer->scan_el_dev_attr_list);
  202. if (ret)
  203. goto error_ret;
  204. attrcount++;
  205. if (chan->type != IIO_TIMESTAMP)
  206. ret = __iio_add_chan_devattr("en",
  207. chan,
  208. &iio_scan_el_show,
  209. &iio_scan_el_store,
  210. chan->scan_index,
  211. 0,
  212. &indio_dev->dev,
  213. &buffer->scan_el_dev_attr_list);
  214. else
  215. ret = __iio_add_chan_devattr("en",
  216. chan,
  217. &iio_scan_el_ts_show,
  218. &iio_scan_el_ts_store,
  219. chan->scan_index,
  220. 0,
  221. &indio_dev->dev,
  222. &buffer->scan_el_dev_attr_list);
  223. attrcount++;
  224. ret = attrcount;
  225. error_ret:
  226. return ret;
  227. }
  228. static void iio_buffer_remove_and_free_scan_dev_attr(struct iio_dev *indio_dev,
  229. struct iio_dev_attr *p)
  230. {
  231. kfree(p->dev_attr.attr.name);
  232. kfree(p);
  233. }
  234. static void __iio_buffer_attr_cleanup(struct iio_dev *indio_dev)
  235. {
  236. struct iio_dev_attr *p, *n;
  237. struct iio_buffer *buffer = indio_dev->buffer;
  238. list_for_each_entry_safe(p, n,
  239. &buffer->scan_el_dev_attr_list, l)
  240. iio_buffer_remove_and_free_scan_dev_attr(indio_dev, p);
  241. }
  242. static const char * const iio_scan_elements_group_name = "scan_elements";
  243. int iio_buffer_register(struct iio_dev *indio_dev,
  244. const struct iio_chan_spec *channels,
  245. int num_channels)
  246. {
  247. struct iio_dev_attr *p;
  248. struct attribute **attr;
  249. struct iio_buffer *buffer = indio_dev->buffer;
  250. int ret, i, attrn, attrcount, attrcount_orig = 0;
  251. if (buffer->attrs)
  252. indio_dev->groups[indio_dev->groupcounter++] = buffer->attrs;
  253. if (buffer->scan_el_attrs != NULL) {
  254. attr = buffer->scan_el_attrs->attrs;
  255. while (*attr++ != NULL)
  256. attrcount_orig++;
  257. }
  258. attrcount = attrcount_orig;
  259. INIT_LIST_HEAD(&buffer->scan_el_dev_attr_list);
  260. if (channels) {
  261. /* new magic */
  262. for (i = 0; i < num_channels; i++) {
  263. if (channels[i].scan_index < 0)
  264. continue;
  265. /* Establish necessary mask length */
  266. if (channels[i].scan_index >
  267. (int)indio_dev->masklength - 1)
  268. indio_dev->masklength
  269. = channels[i].scan_index + 1;
  270. ret = iio_buffer_add_channel_sysfs(indio_dev,
  271. &channels[i]);
  272. if (ret < 0)
  273. goto error_cleanup_dynamic;
  274. attrcount += ret;
  275. if (channels[i].type == IIO_TIMESTAMP)
  276. indio_dev->scan_index_timestamp =
  277. channels[i].scan_index;
  278. }
  279. if (indio_dev->masklength && buffer->scan_mask == NULL) {
  280. buffer->scan_mask = kcalloc(BITS_TO_LONGS(indio_dev->masklength),
  281. sizeof(*buffer->scan_mask),
  282. GFP_KERNEL);
  283. if (buffer->scan_mask == NULL) {
  284. ret = -ENOMEM;
  285. goto error_cleanup_dynamic;
  286. }
  287. }
  288. }
  289. buffer->scan_el_group.name = iio_scan_elements_group_name;
  290. buffer->scan_el_group.attrs = kcalloc(attrcount + 1,
  291. sizeof(buffer->scan_el_group.attrs[0]),
  292. GFP_KERNEL);
  293. if (buffer->scan_el_group.attrs == NULL) {
  294. ret = -ENOMEM;
  295. goto error_free_scan_mask;
  296. }
  297. if (buffer->scan_el_attrs)
  298. memcpy(buffer->scan_el_group.attrs, buffer->scan_el_attrs,
  299. sizeof(buffer->scan_el_group.attrs[0])*attrcount_orig);
  300. attrn = attrcount_orig;
  301. list_for_each_entry(p, &buffer->scan_el_dev_attr_list, l)
  302. buffer->scan_el_group.attrs[attrn++] = &p->dev_attr.attr;
  303. indio_dev->groups[indio_dev->groupcounter++] = &buffer->scan_el_group;
  304. return 0;
  305. error_free_scan_mask:
  306. kfree(buffer->scan_mask);
  307. error_cleanup_dynamic:
  308. __iio_buffer_attr_cleanup(indio_dev);
  309. return ret;
  310. }
  311. EXPORT_SYMBOL(iio_buffer_register);
  312. void iio_buffer_unregister(struct iio_dev *indio_dev)
  313. {
  314. kfree(indio_dev->buffer->scan_mask);
  315. kfree(indio_dev->buffer->scan_el_group.attrs);
  316. __iio_buffer_attr_cleanup(indio_dev);
  317. }
  318. EXPORT_SYMBOL(iio_buffer_unregister);
  319. ssize_t iio_buffer_read_length(struct device *dev,
  320. struct device_attribute *attr,
  321. char *buf)
  322. {
  323. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  324. struct iio_buffer *buffer = indio_dev->buffer;
  325. if (buffer->access->get_length)
  326. return sprintf(buf, "%d\n",
  327. buffer->access->get_length(buffer));
  328. return 0;
  329. }
  330. EXPORT_SYMBOL(iio_buffer_read_length);
  331. ssize_t iio_buffer_write_length(struct device *dev,
  332. struct device_attribute *attr,
  333. const char *buf,
  334. size_t len)
  335. {
  336. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  337. struct iio_buffer *buffer = indio_dev->buffer;
  338. unsigned int val;
  339. int ret;
  340. ret = kstrtouint(buf, 10, &val);
  341. if (ret)
  342. return ret;
  343. if (buffer->access->get_length)
  344. if (val == buffer->access->get_length(buffer))
  345. return len;
  346. mutex_lock(&indio_dev->mlock);
  347. if (iio_buffer_is_active(indio_dev, indio_dev->buffer)) {
  348. ret = -EBUSY;
  349. } else {
  350. if (buffer->access->set_length)
  351. buffer->access->set_length(buffer, val);
  352. ret = 0;
  353. }
  354. mutex_unlock(&indio_dev->mlock);
  355. return ret ? ret : len;
  356. }
  357. EXPORT_SYMBOL(iio_buffer_write_length);
  358. ssize_t iio_buffer_show_enable(struct device *dev,
  359. struct device_attribute *attr,
  360. char *buf)
  361. {
  362. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  363. return sprintf(buf, "%d\n",
  364. iio_buffer_is_active(indio_dev,
  365. indio_dev->buffer));
  366. }
  367. EXPORT_SYMBOL(iio_buffer_show_enable);
  368. /* note NULL used as error indicator as it doesn't make sense. */
  369. static const unsigned long *iio_scan_mask_match(const unsigned long *av_masks,
  370. unsigned int masklength,
  371. const unsigned long *mask)
  372. {
  373. if (bitmap_empty(mask, masklength))
  374. return NULL;
  375. while (*av_masks) {
  376. if (bitmap_subset(mask, av_masks, masklength))
  377. return av_masks;
  378. av_masks += BITS_TO_LONGS(masklength);
  379. }
  380. return NULL;
  381. }
  382. static int iio_compute_scan_bytes(struct iio_dev *indio_dev, const long *mask,
  383. bool timestamp)
  384. {
  385. const struct iio_chan_spec *ch;
  386. unsigned bytes = 0;
  387. int length, i;
  388. /* How much space will the demuxed element take? */
  389. for_each_set_bit(i, mask,
  390. indio_dev->masklength) {
  391. ch = iio_find_channel_from_si(indio_dev, i);
  392. length = ch->scan_type.storagebits / 8;
  393. bytes = ALIGN(bytes, length);
  394. bytes += length;
  395. }
  396. if (timestamp) {
  397. ch = iio_find_channel_from_si(indio_dev,
  398. indio_dev->scan_index_timestamp);
  399. length = ch->scan_type.storagebits / 8;
  400. bytes = ALIGN(bytes, length);
  401. bytes += length;
  402. }
  403. return bytes;
  404. }
  405. int iio_update_buffers(struct iio_dev *indio_dev,
  406. struct iio_buffer *insert_buffer,
  407. struct iio_buffer *remove_buffer)
  408. {
  409. int ret;
  410. int success = 0;
  411. struct iio_buffer *buffer;
  412. unsigned long *compound_mask;
  413. const unsigned long *old_mask;
  414. /* Wind down existing buffers - iff there are any */
  415. if (!list_empty(&indio_dev->buffer_list)) {
  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. /* Keep a copy of current setup to allow roll back */
  429. old_mask = indio_dev->active_scan_mask;
  430. if (!indio_dev->available_scan_masks)
  431. indio_dev->active_scan_mask = NULL;
  432. if (remove_buffer)
  433. list_del(&remove_buffer->buffer_list);
  434. if (insert_buffer)
  435. list_add(&insert_buffer->buffer_list, &indio_dev->buffer_list);
  436. /* If no buffers in list, we are done */
  437. if (list_empty(&indio_dev->buffer_list)) {
  438. indio_dev->currentmode = INDIO_DIRECT_MODE;
  439. if (indio_dev->available_scan_masks == NULL)
  440. kfree(old_mask);
  441. return 0;
  442. }
  443. /* What scan mask do we actually have ?*/
  444. compound_mask = kcalloc(BITS_TO_LONGS(indio_dev->masklength),
  445. sizeof(long), GFP_KERNEL);
  446. if (compound_mask == NULL) {
  447. if (indio_dev->available_scan_masks == NULL)
  448. kfree(old_mask);
  449. return -ENOMEM;
  450. }
  451. indio_dev->scan_timestamp = 0;
  452. list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list) {
  453. bitmap_or(compound_mask, compound_mask, buffer->scan_mask,
  454. indio_dev->masklength);
  455. indio_dev->scan_timestamp |= buffer->scan_timestamp;
  456. }
  457. if (indio_dev->available_scan_masks) {
  458. indio_dev->active_scan_mask =
  459. iio_scan_mask_match(indio_dev->available_scan_masks,
  460. indio_dev->masklength,
  461. compound_mask);
  462. if (indio_dev->active_scan_mask == NULL) {
  463. /*
  464. * Roll back.
  465. * Note can only occur when adding a buffer.
  466. */
  467. list_del(&insert_buffer->buffer_list);
  468. indio_dev->active_scan_mask = old_mask;
  469. success = -EINVAL;
  470. }
  471. } else {
  472. indio_dev->active_scan_mask = compound_mask;
  473. }
  474. iio_update_demux(indio_dev);
  475. /* Wind up again */
  476. if (indio_dev->setup_ops->preenable) {
  477. ret = indio_dev->setup_ops->preenable(indio_dev);
  478. if (ret) {
  479. printk(KERN_ERR
  480. "Buffer not started:"
  481. "buffer preenable failed\n");
  482. goto error_remove_inserted;
  483. }
  484. }
  485. indio_dev->scan_bytes =
  486. iio_compute_scan_bytes(indio_dev,
  487. indio_dev->active_scan_mask,
  488. indio_dev->scan_timestamp);
  489. list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list)
  490. if (buffer->access->request_update) {
  491. ret = buffer->access->request_update(buffer);
  492. if (ret) {
  493. printk(KERN_INFO
  494. "Buffer not started:"
  495. "buffer parameter update failed\n");
  496. goto error_run_postdisable;
  497. }
  498. }
  499. if (indio_dev->info->update_scan_mode) {
  500. ret = indio_dev->info
  501. ->update_scan_mode(indio_dev,
  502. indio_dev->active_scan_mask);
  503. if (ret < 0) {
  504. printk(KERN_INFO "update scan mode failed\n");
  505. goto error_run_postdisable;
  506. }
  507. }
  508. /* Definitely possible for devices to support both of these.*/
  509. if (indio_dev->modes & INDIO_BUFFER_TRIGGERED) {
  510. if (!indio_dev->trig) {
  511. printk(KERN_INFO "Buffer not started: no trigger\n");
  512. ret = -EINVAL;
  513. /* Can only occur on first buffer */
  514. goto error_run_postdisable;
  515. }
  516. indio_dev->currentmode = INDIO_BUFFER_TRIGGERED;
  517. } else if (indio_dev->modes & INDIO_BUFFER_HARDWARE) {
  518. indio_dev->currentmode = INDIO_BUFFER_HARDWARE;
  519. } else { /* should never be reached */
  520. ret = -EINVAL;
  521. goto error_run_postdisable;
  522. }
  523. if (indio_dev->setup_ops->postenable) {
  524. ret = indio_dev->setup_ops->postenable(indio_dev);
  525. if (ret) {
  526. printk(KERN_INFO
  527. "Buffer not started: postenable failed\n");
  528. indio_dev->currentmode = INDIO_DIRECT_MODE;
  529. if (indio_dev->setup_ops->postdisable)
  530. indio_dev->setup_ops->postdisable(indio_dev);
  531. goto error_disable_all_buffers;
  532. }
  533. }
  534. if (indio_dev->available_scan_masks)
  535. kfree(compound_mask);
  536. else
  537. kfree(old_mask);
  538. return success;
  539. error_disable_all_buffers:
  540. indio_dev->currentmode = INDIO_DIRECT_MODE;
  541. error_run_postdisable:
  542. if (indio_dev->setup_ops->postdisable)
  543. indio_dev->setup_ops->postdisable(indio_dev);
  544. error_remove_inserted:
  545. if (insert_buffer)
  546. list_del(&insert_buffer->buffer_list);
  547. indio_dev->active_scan_mask = old_mask;
  548. kfree(compound_mask);
  549. error_ret:
  550. return ret;
  551. }
  552. EXPORT_SYMBOL_GPL(iio_update_buffers);
  553. ssize_t iio_buffer_store_enable(struct device *dev,
  554. struct device_attribute *attr,
  555. const char *buf,
  556. size_t len)
  557. {
  558. int ret;
  559. bool requested_state;
  560. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  561. struct iio_buffer *pbuf = indio_dev->buffer;
  562. bool inlist;
  563. ret = strtobool(buf, &requested_state);
  564. if (ret < 0)
  565. return ret;
  566. mutex_lock(&indio_dev->mlock);
  567. /* Find out if it is in the list */
  568. inlist = iio_buffer_is_active(indio_dev, pbuf);
  569. /* Already in desired state */
  570. if (inlist == requested_state)
  571. goto done;
  572. if (requested_state)
  573. ret = iio_update_buffers(indio_dev,
  574. indio_dev->buffer, NULL);
  575. else
  576. ret = iio_update_buffers(indio_dev,
  577. NULL, indio_dev->buffer);
  578. if (ret < 0)
  579. goto done;
  580. done:
  581. mutex_unlock(&indio_dev->mlock);
  582. return (ret < 0) ? ret : len;
  583. }
  584. EXPORT_SYMBOL(iio_buffer_store_enable);
  585. int iio_sw_buffer_preenable(struct iio_dev *indio_dev)
  586. {
  587. struct iio_buffer *buffer;
  588. unsigned bytes;
  589. dev_dbg(&indio_dev->dev, "%s\n", __func__);
  590. list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list)
  591. if (buffer->access->set_bytes_per_datum) {
  592. bytes = iio_compute_scan_bytes(indio_dev,
  593. buffer->scan_mask,
  594. buffer->scan_timestamp);
  595. buffer->access->set_bytes_per_datum(buffer, bytes);
  596. }
  597. return 0;
  598. }
  599. EXPORT_SYMBOL(iio_sw_buffer_preenable);
  600. /**
  601. * iio_validate_scan_mask_onehot() - Validates that exactly one channel is selected
  602. * @indio_dev: the iio device
  603. * @mask: scan mask to be checked
  604. *
  605. * Return true if exactly one bit is set in the scan mask, false otherwise. It
  606. * can be used for devices where only one channel can be active for sampling at
  607. * a time.
  608. */
  609. bool iio_validate_scan_mask_onehot(struct iio_dev *indio_dev,
  610. const unsigned long *mask)
  611. {
  612. return bitmap_weight(mask, indio_dev->masklength) == 1;
  613. }
  614. EXPORT_SYMBOL_GPL(iio_validate_scan_mask_onehot);
  615. static bool iio_validate_scan_mask(struct iio_dev *indio_dev,
  616. const unsigned long *mask)
  617. {
  618. if (!indio_dev->setup_ops->validate_scan_mask)
  619. return true;
  620. return indio_dev->setup_ops->validate_scan_mask(indio_dev, mask);
  621. }
  622. /**
  623. * iio_scan_mask_set() - set particular bit in the scan mask
  624. * @buffer: the buffer whose scan mask we are interested in
  625. * @bit: the bit to be set.
  626. *
  627. * Note that at this point we have no way of knowing what other
  628. * buffers might request, hence this code only verifies that the
  629. * individual buffers request is plausible.
  630. */
  631. int iio_scan_mask_set(struct iio_dev *indio_dev,
  632. struct iio_buffer *buffer, int bit)
  633. {
  634. const unsigned long *mask;
  635. unsigned long *trialmask;
  636. trialmask = kmalloc(sizeof(*trialmask)*
  637. BITS_TO_LONGS(indio_dev->masklength),
  638. GFP_KERNEL);
  639. if (trialmask == NULL)
  640. return -ENOMEM;
  641. if (!indio_dev->masklength) {
  642. WARN_ON("trying to set scanmask prior to registering buffer\n");
  643. goto err_invalid_mask;
  644. }
  645. bitmap_copy(trialmask, buffer->scan_mask, indio_dev->masklength);
  646. set_bit(bit, trialmask);
  647. if (!iio_validate_scan_mask(indio_dev, trialmask))
  648. goto err_invalid_mask;
  649. if (indio_dev->available_scan_masks) {
  650. mask = iio_scan_mask_match(indio_dev->available_scan_masks,
  651. indio_dev->masklength,
  652. trialmask);
  653. if (!mask)
  654. goto err_invalid_mask;
  655. }
  656. bitmap_copy(buffer->scan_mask, trialmask, indio_dev->masklength);
  657. kfree(trialmask);
  658. return 0;
  659. err_invalid_mask:
  660. kfree(trialmask);
  661. return -EINVAL;
  662. }
  663. EXPORT_SYMBOL_GPL(iio_scan_mask_set);
  664. int iio_scan_mask_query(struct iio_dev *indio_dev,
  665. struct iio_buffer *buffer, int bit)
  666. {
  667. if (bit > indio_dev->masklength)
  668. return -EINVAL;
  669. if (!buffer->scan_mask)
  670. return 0;
  671. return test_bit(bit, buffer->scan_mask);
  672. };
  673. EXPORT_SYMBOL_GPL(iio_scan_mask_query);
  674. /**
  675. * struct iio_demux_table() - table describing demux memcpy ops
  676. * @from: index to copy from
  677. * @to: index to copy to
  678. * @length: how many bytes to copy
  679. * @l: list head used for management
  680. */
  681. struct iio_demux_table {
  682. unsigned from;
  683. unsigned to;
  684. unsigned length;
  685. struct list_head l;
  686. };
  687. static unsigned char *iio_demux(struct iio_buffer *buffer,
  688. unsigned char *datain)
  689. {
  690. struct iio_demux_table *t;
  691. if (list_empty(&buffer->demux_list))
  692. return datain;
  693. list_for_each_entry(t, &buffer->demux_list, l)
  694. memcpy(buffer->demux_bounce + t->to,
  695. datain + t->from, t->length);
  696. return buffer->demux_bounce;
  697. }
  698. static int iio_push_to_buffer(struct iio_buffer *buffer, unsigned char *data)
  699. {
  700. unsigned char *dataout = iio_demux(buffer, data);
  701. return buffer->access->store_to(buffer, dataout);
  702. }
  703. static void iio_buffer_demux_free(struct iio_buffer *buffer)
  704. {
  705. struct iio_demux_table *p, *q;
  706. list_for_each_entry_safe(p, q, &buffer->demux_list, l) {
  707. list_del(&p->l);
  708. kfree(p);
  709. }
  710. }
  711. int iio_push_to_buffers(struct iio_dev *indio_dev, unsigned char *data)
  712. {
  713. int ret;
  714. struct iio_buffer *buf;
  715. list_for_each_entry(buf, &indio_dev->buffer_list, buffer_list) {
  716. ret = iio_push_to_buffer(buf, data);
  717. if (ret < 0)
  718. return ret;
  719. }
  720. return 0;
  721. }
  722. EXPORT_SYMBOL_GPL(iio_push_to_buffers);
  723. static int iio_buffer_update_demux(struct iio_dev *indio_dev,
  724. struct iio_buffer *buffer)
  725. {
  726. const struct iio_chan_spec *ch;
  727. int ret, in_ind = -1, out_ind, length;
  728. unsigned in_loc = 0, out_loc = 0;
  729. struct iio_demux_table *p;
  730. /* Clear out any old demux */
  731. iio_buffer_demux_free(buffer);
  732. kfree(buffer->demux_bounce);
  733. buffer->demux_bounce = NULL;
  734. /* First work out which scan mode we will actually have */
  735. if (bitmap_equal(indio_dev->active_scan_mask,
  736. buffer->scan_mask,
  737. indio_dev->masklength))
  738. return 0;
  739. /* Now we have the two masks, work from least sig and build up sizes */
  740. for_each_set_bit(out_ind,
  741. indio_dev->active_scan_mask,
  742. indio_dev->masklength) {
  743. in_ind = find_next_bit(indio_dev->active_scan_mask,
  744. indio_dev->masklength,
  745. in_ind + 1);
  746. while (in_ind != out_ind) {
  747. in_ind = find_next_bit(indio_dev->active_scan_mask,
  748. indio_dev->masklength,
  749. in_ind + 1);
  750. ch = iio_find_channel_from_si(indio_dev, in_ind);
  751. length = ch->scan_type.storagebits/8;
  752. /* Make sure we are aligned */
  753. in_loc += length;
  754. if (in_loc % length)
  755. in_loc += length - in_loc % length;
  756. }
  757. p = kmalloc(sizeof(*p), GFP_KERNEL);
  758. if (p == NULL) {
  759. ret = -ENOMEM;
  760. goto error_clear_mux_table;
  761. }
  762. ch = iio_find_channel_from_si(indio_dev, in_ind);
  763. length = ch->scan_type.storagebits/8;
  764. if (out_loc % length)
  765. out_loc += length - out_loc % length;
  766. if (in_loc % length)
  767. in_loc += length - in_loc % length;
  768. p->from = in_loc;
  769. p->to = out_loc;
  770. p->length = length;
  771. list_add_tail(&p->l, &buffer->demux_list);
  772. out_loc += length;
  773. in_loc += length;
  774. }
  775. /* Relies on scan_timestamp being last */
  776. if (buffer->scan_timestamp) {
  777. p = kmalloc(sizeof(*p), GFP_KERNEL);
  778. if (p == NULL) {
  779. ret = -ENOMEM;
  780. goto error_clear_mux_table;
  781. }
  782. ch = iio_find_channel_from_si(indio_dev,
  783. indio_dev->scan_index_timestamp);
  784. length = ch->scan_type.storagebits/8;
  785. if (out_loc % length)
  786. out_loc += length - out_loc % length;
  787. if (in_loc % length)
  788. in_loc += length - in_loc % length;
  789. p->from = in_loc;
  790. p->to = out_loc;
  791. p->length = length;
  792. list_add_tail(&p->l, &buffer->demux_list);
  793. out_loc += length;
  794. in_loc += length;
  795. }
  796. buffer->demux_bounce = kzalloc(out_loc, GFP_KERNEL);
  797. if (buffer->demux_bounce == NULL) {
  798. ret = -ENOMEM;
  799. goto error_clear_mux_table;
  800. }
  801. return 0;
  802. error_clear_mux_table:
  803. iio_buffer_demux_free(buffer);
  804. return ret;
  805. }
  806. int iio_update_demux(struct iio_dev *indio_dev)
  807. {
  808. struct iio_buffer *buffer;
  809. int ret;
  810. list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list) {
  811. ret = iio_buffer_update_demux(indio_dev, buffer);
  812. if (ret < 0)
  813. goto error_clear_mux_table;
  814. }
  815. return 0;
  816. error_clear_mux_table:
  817. list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list)
  818. iio_buffer_demux_free(buffer);
  819. return ret;
  820. }
  821. EXPORT_SYMBOL_GPL(iio_update_demux);