industrialio-buffer.c 27 KB

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