industrialio-buffer.c 28 KB

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