industrialio-buffer.c 24 KB

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