industrialio-buffer.c 24 KB

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