industrialio-buffer.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934
  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: buffer preenable failed (%d)\n", ret);
  481. goto error_remove_inserted;
  482. }
  483. }
  484. indio_dev->scan_bytes =
  485. iio_compute_scan_bytes(indio_dev,
  486. indio_dev->active_scan_mask,
  487. indio_dev->scan_timestamp);
  488. list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list)
  489. if (buffer->access->request_update) {
  490. ret = buffer->access->request_update(buffer);
  491. if (ret) {
  492. printk(KERN_INFO
  493. "Buffer not started: buffer parameter update failed (%d)\n", ret);
  494. goto error_run_postdisable;
  495. }
  496. }
  497. if (indio_dev->info->update_scan_mode) {
  498. ret = indio_dev->info
  499. ->update_scan_mode(indio_dev,
  500. indio_dev->active_scan_mask);
  501. if (ret < 0) {
  502. printk(KERN_INFO "Buffer not started: update scan mode failed (%d)\n", ret);
  503. goto error_run_postdisable;
  504. }
  505. }
  506. /* Definitely possible for devices to support both of these.*/
  507. if (indio_dev->modes & INDIO_BUFFER_TRIGGERED) {
  508. if (!indio_dev->trig) {
  509. printk(KERN_INFO "Buffer not started: no trigger\n");
  510. ret = -EINVAL;
  511. /* Can only occur on first buffer */
  512. goto error_run_postdisable;
  513. }
  514. indio_dev->currentmode = INDIO_BUFFER_TRIGGERED;
  515. } else if (indio_dev->modes & INDIO_BUFFER_HARDWARE) {
  516. indio_dev->currentmode = INDIO_BUFFER_HARDWARE;
  517. } else { /* should never be reached */
  518. ret = -EINVAL;
  519. goto error_run_postdisable;
  520. }
  521. if (indio_dev->setup_ops->postenable) {
  522. ret = indio_dev->setup_ops->postenable(indio_dev);
  523. if (ret) {
  524. printk(KERN_INFO
  525. "Buffer not started: postenable failed (%d)\n", ret);
  526. indio_dev->currentmode = INDIO_DIRECT_MODE;
  527. if (indio_dev->setup_ops->postdisable)
  528. indio_dev->setup_ops->postdisable(indio_dev);
  529. goto error_disable_all_buffers;
  530. }
  531. }
  532. if (indio_dev->available_scan_masks)
  533. kfree(compound_mask);
  534. else
  535. kfree(old_mask);
  536. return success;
  537. error_disable_all_buffers:
  538. indio_dev->currentmode = INDIO_DIRECT_MODE;
  539. error_run_postdisable:
  540. if (indio_dev->setup_ops->postdisable)
  541. indio_dev->setup_ops->postdisable(indio_dev);
  542. error_remove_inserted:
  543. if (insert_buffer)
  544. list_del(&insert_buffer->buffer_list);
  545. indio_dev->active_scan_mask = old_mask;
  546. kfree(compound_mask);
  547. error_ret:
  548. return ret;
  549. }
  550. EXPORT_SYMBOL_GPL(iio_update_buffers);
  551. ssize_t iio_buffer_store_enable(struct device *dev,
  552. struct device_attribute *attr,
  553. const char *buf,
  554. size_t len)
  555. {
  556. int ret;
  557. bool requested_state;
  558. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  559. struct iio_buffer *pbuf = indio_dev->buffer;
  560. bool inlist;
  561. ret = strtobool(buf, &requested_state);
  562. if (ret < 0)
  563. return ret;
  564. mutex_lock(&indio_dev->mlock);
  565. /* Find out if it is in the list */
  566. inlist = iio_buffer_is_active(indio_dev, pbuf);
  567. /* Already in desired state */
  568. if (inlist == requested_state)
  569. goto done;
  570. if (requested_state)
  571. ret = iio_update_buffers(indio_dev,
  572. indio_dev->buffer, NULL);
  573. else
  574. ret = iio_update_buffers(indio_dev,
  575. NULL, indio_dev->buffer);
  576. if (ret < 0)
  577. goto done;
  578. done:
  579. mutex_unlock(&indio_dev->mlock);
  580. return (ret < 0) ? ret : len;
  581. }
  582. EXPORT_SYMBOL(iio_buffer_store_enable);
  583. int iio_sw_buffer_preenable(struct iio_dev *indio_dev)
  584. {
  585. struct iio_buffer *buffer;
  586. unsigned bytes;
  587. dev_dbg(&indio_dev->dev, "%s\n", __func__);
  588. list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list)
  589. if (buffer->access->set_bytes_per_datum) {
  590. bytes = iio_compute_scan_bytes(indio_dev,
  591. buffer->scan_mask,
  592. buffer->scan_timestamp);
  593. buffer->access->set_bytes_per_datum(buffer, bytes);
  594. }
  595. return 0;
  596. }
  597. EXPORT_SYMBOL(iio_sw_buffer_preenable);
  598. /**
  599. * iio_validate_scan_mask_onehot() - Validates that exactly one channel is selected
  600. * @indio_dev: the iio device
  601. * @mask: scan mask to be checked
  602. *
  603. * Return true if exactly one bit is set in the scan mask, false otherwise. It
  604. * can be used for devices where only one channel can be active for sampling at
  605. * a time.
  606. */
  607. bool iio_validate_scan_mask_onehot(struct iio_dev *indio_dev,
  608. const unsigned long *mask)
  609. {
  610. return bitmap_weight(mask, indio_dev->masklength) == 1;
  611. }
  612. EXPORT_SYMBOL_GPL(iio_validate_scan_mask_onehot);
  613. static bool iio_validate_scan_mask(struct iio_dev *indio_dev,
  614. const unsigned long *mask)
  615. {
  616. if (!indio_dev->setup_ops->validate_scan_mask)
  617. return true;
  618. return indio_dev->setup_ops->validate_scan_mask(indio_dev, mask);
  619. }
  620. /**
  621. * iio_scan_mask_set() - set particular bit in the scan mask
  622. * @buffer: the buffer whose scan mask we are interested in
  623. * @bit: the bit to be set.
  624. *
  625. * Note that at this point we have no way of knowing what other
  626. * buffers might request, hence this code only verifies that the
  627. * individual buffers request is plausible.
  628. */
  629. int iio_scan_mask_set(struct iio_dev *indio_dev,
  630. struct iio_buffer *buffer, int bit)
  631. {
  632. const unsigned long *mask;
  633. unsigned long *trialmask;
  634. trialmask = kmalloc(sizeof(*trialmask)*
  635. BITS_TO_LONGS(indio_dev->masklength),
  636. GFP_KERNEL);
  637. if (trialmask == NULL)
  638. return -ENOMEM;
  639. if (!indio_dev->masklength) {
  640. WARN_ON("trying to set scanmask prior to registering buffer\n");
  641. goto err_invalid_mask;
  642. }
  643. bitmap_copy(trialmask, buffer->scan_mask, indio_dev->masklength);
  644. set_bit(bit, trialmask);
  645. if (!iio_validate_scan_mask(indio_dev, trialmask))
  646. goto err_invalid_mask;
  647. if (indio_dev->available_scan_masks) {
  648. mask = iio_scan_mask_match(indio_dev->available_scan_masks,
  649. indio_dev->masklength,
  650. trialmask);
  651. if (!mask)
  652. goto err_invalid_mask;
  653. }
  654. bitmap_copy(buffer->scan_mask, trialmask, indio_dev->masklength);
  655. kfree(trialmask);
  656. return 0;
  657. err_invalid_mask:
  658. kfree(trialmask);
  659. return -EINVAL;
  660. }
  661. EXPORT_SYMBOL_GPL(iio_scan_mask_set);
  662. int iio_scan_mask_query(struct iio_dev *indio_dev,
  663. struct iio_buffer *buffer, int bit)
  664. {
  665. if (bit > indio_dev->masklength)
  666. return -EINVAL;
  667. if (!buffer->scan_mask)
  668. return 0;
  669. return test_bit(bit, buffer->scan_mask);
  670. };
  671. EXPORT_SYMBOL_GPL(iio_scan_mask_query);
  672. /**
  673. * struct iio_demux_table() - table describing demux memcpy ops
  674. * @from: index to copy from
  675. * @to: index to copy to
  676. * @length: how many bytes to copy
  677. * @l: list head used for management
  678. */
  679. struct iio_demux_table {
  680. unsigned from;
  681. unsigned to;
  682. unsigned length;
  683. struct list_head l;
  684. };
  685. static unsigned char *iio_demux(struct iio_buffer *buffer,
  686. unsigned char *datain)
  687. {
  688. struct iio_demux_table *t;
  689. if (list_empty(&buffer->demux_list))
  690. return datain;
  691. list_for_each_entry(t, &buffer->demux_list, l)
  692. memcpy(buffer->demux_bounce + t->to,
  693. datain + t->from, t->length);
  694. return buffer->demux_bounce;
  695. }
  696. static int iio_push_to_buffer(struct iio_buffer *buffer, unsigned char *data)
  697. {
  698. unsigned char *dataout = iio_demux(buffer, data);
  699. return buffer->access->store_to(buffer, dataout);
  700. }
  701. static void iio_buffer_demux_free(struct iio_buffer *buffer)
  702. {
  703. struct iio_demux_table *p, *q;
  704. list_for_each_entry_safe(p, q, &buffer->demux_list, l) {
  705. list_del(&p->l);
  706. kfree(p);
  707. }
  708. }
  709. int iio_push_to_buffers(struct iio_dev *indio_dev, unsigned char *data)
  710. {
  711. int ret;
  712. struct iio_buffer *buf;
  713. list_for_each_entry(buf, &indio_dev->buffer_list, buffer_list) {
  714. ret = iio_push_to_buffer(buf, data);
  715. if (ret < 0)
  716. return ret;
  717. }
  718. return 0;
  719. }
  720. EXPORT_SYMBOL_GPL(iio_push_to_buffers);
  721. static int iio_buffer_update_demux(struct iio_dev *indio_dev,
  722. struct iio_buffer *buffer)
  723. {
  724. const struct iio_chan_spec *ch;
  725. int ret, in_ind = -1, out_ind, length;
  726. unsigned in_loc = 0, out_loc = 0;
  727. struct iio_demux_table *p;
  728. /* Clear out any old demux */
  729. iio_buffer_demux_free(buffer);
  730. kfree(buffer->demux_bounce);
  731. buffer->demux_bounce = NULL;
  732. /* First work out which scan mode we will actually have */
  733. if (bitmap_equal(indio_dev->active_scan_mask,
  734. buffer->scan_mask,
  735. indio_dev->masklength))
  736. return 0;
  737. /* Now we have the two masks, work from least sig and build up sizes */
  738. for_each_set_bit(out_ind,
  739. indio_dev->active_scan_mask,
  740. indio_dev->masklength) {
  741. in_ind = find_next_bit(indio_dev->active_scan_mask,
  742. indio_dev->masklength,
  743. in_ind + 1);
  744. while (in_ind != out_ind) {
  745. in_ind = find_next_bit(indio_dev->active_scan_mask,
  746. indio_dev->masklength,
  747. in_ind + 1);
  748. ch = iio_find_channel_from_si(indio_dev, in_ind);
  749. length = ch->scan_type.storagebits/8;
  750. /* Make sure we are aligned */
  751. in_loc += length;
  752. if (in_loc % length)
  753. in_loc += length - in_loc % length;
  754. }
  755. p = kmalloc(sizeof(*p), GFP_KERNEL);
  756. if (p == NULL) {
  757. ret = -ENOMEM;
  758. goto error_clear_mux_table;
  759. }
  760. ch = iio_find_channel_from_si(indio_dev, in_ind);
  761. length = ch->scan_type.storagebits/8;
  762. if (out_loc % length)
  763. out_loc += length - out_loc % length;
  764. if (in_loc % length)
  765. in_loc += length - in_loc % length;
  766. p->from = in_loc;
  767. p->to = out_loc;
  768. p->length = length;
  769. list_add_tail(&p->l, &buffer->demux_list);
  770. out_loc += length;
  771. in_loc += length;
  772. }
  773. /* Relies on scan_timestamp being last */
  774. if (buffer->scan_timestamp) {
  775. p = kmalloc(sizeof(*p), GFP_KERNEL);
  776. if (p == NULL) {
  777. ret = -ENOMEM;
  778. goto error_clear_mux_table;
  779. }
  780. ch = iio_find_channel_from_si(indio_dev,
  781. indio_dev->scan_index_timestamp);
  782. length = ch->scan_type.storagebits/8;
  783. if (out_loc % length)
  784. out_loc += length - out_loc % length;
  785. if (in_loc % length)
  786. in_loc += length - in_loc % length;
  787. p->from = in_loc;
  788. p->to = out_loc;
  789. p->length = length;
  790. list_add_tail(&p->l, &buffer->demux_list);
  791. out_loc += length;
  792. in_loc += length;
  793. }
  794. buffer->demux_bounce = kzalloc(out_loc, GFP_KERNEL);
  795. if (buffer->demux_bounce == NULL) {
  796. ret = -ENOMEM;
  797. goto error_clear_mux_table;
  798. }
  799. return 0;
  800. error_clear_mux_table:
  801. iio_buffer_demux_free(buffer);
  802. return ret;
  803. }
  804. int iio_update_demux(struct iio_dev *indio_dev)
  805. {
  806. struct iio_buffer *buffer;
  807. int ret;
  808. list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list) {
  809. ret = iio_buffer_update_demux(indio_dev, buffer);
  810. if (ret < 0)
  811. goto error_clear_mux_table;
  812. }
  813. return 0;
  814. error_clear_mux_table:
  815. list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list)
  816. iio_buffer_demux_free(buffer);
  817. return ret;
  818. }
  819. EXPORT_SYMBOL_GPL(iio_update_demux);