industrialio-buffer.c 24 KB

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