industrialio-buffer.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422
  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 size_t iio_buffer_data_available(struct iio_buffer *buf)
  37. {
  38. return buf->access->data_available(buf);
  39. }
  40. static int iio_buffer_flush_hwfifo(struct iio_dev *indio_dev,
  41. struct iio_buffer *buf, size_t required)
  42. {
  43. if (!indio_dev->info->hwfifo_flush_to_buffer)
  44. return -ENODEV;
  45. return indio_dev->info->hwfifo_flush_to_buffer(indio_dev, required);
  46. }
  47. static bool iio_buffer_ready(struct iio_dev *indio_dev, struct iio_buffer *buf,
  48. size_t to_wait, int to_flush)
  49. {
  50. size_t avail;
  51. int flushed = 0;
  52. /* wakeup if the device was unregistered */
  53. if (!indio_dev->info)
  54. return true;
  55. /* drain the buffer if it was disabled */
  56. if (!iio_buffer_is_active(buf)) {
  57. to_wait = min_t(size_t, to_wait, 1);
  58. to_flush = 0;
  59. }
  60. avail = iio_buffer_data_available(buf);
  61. if (avail >= to_wait) {
  62. /* force a flush for non-blocking reads */
  63. if (!to_wait && avail < to_flush)
  64. iio_buffer_flush_hwfifo(indio_dev, buf,
  65. to_flush - avail);
  66. return true;
  67. }
  68. if (to_flush)
  69. flushed = iio_buffer_flush_hwfifo(indio_dev, buf,
  70. to_wait - avail);
  71. if (flushed <= 0)
  72. return false;
  73. if (avail + flushed >= to_wait)
  74. return true;
  75. return false;
  76. }
  77. /**
  78. * iio_buffer_read_first_n_outer() - chrdev read for buffer access
  79. * @filp: File structure pointer for the char device
  80. * @buf: Destination buffer for iio buffer read
  81. * @n: First n bytes to read
  82. * @f_ps: Long offset provided by the user as a seek position
  83. *
  84. * This function relies on all buffer implementations having an
  85. * iio_buffer as their first element.
  86. *
  87. * Return: negative values corresponding to error codes or ret != 0
  88. * for ending the reading activity
  89. **/
  90. ssize_t iio_buffer_read_first_n_outer(struct file *filp, char __user *buf,
  91. size_t n, loff_t *f_ps)
  92. {
  93. struct iio_dev *indio_dev = filp->private_data;
  94. struct iio_buffer *rb = indio_dev->buffer;
  95. size_t datum_size;
  96. size_t to_wait;
  97. int ret;
  98. if (!indio_dev->info)
  99. return -ENODEV;
  100. if (!rb || !rb->access->read_first_n)
  101. return -EINVAL;
  102. datum_size = rb->bytes_per_datum;
  103. /*
  104. * If datum_size is 0 there will never be anything to read from the
  105. * buffer, so signal end of file now.
  106. */
  107. if (!datum_size)
  108. return 0;
  109. if (filp->f_flags & O_NONBLOCK)
  110. to_wait = 0;
  111. else
  112. to_wait = min_t(size_t, n / datum_size, rb->watermark);
  113. do {
  114. ret = wait_event_interruptible(rb->pollq,
  115. iio_buffer_ready(indio_dev, rb, to_wait, n / datum_size));
  116. if (ret)
  117. return ret;
  118. if (!indio_dev->info)
  119. return -ENODEV;
  120. ret = rb->access->read_first_n(rb, n, buf);
  121. if (ret == 0 && (filp->f_flags & O_NONBLOCK))
  122. ret = -EAGAIN;
  123. } while (ret == 0);
  124. return ret;
  125. }
  126. /**
  127. * iio_buffer_poll() - poll the buffer to find out if it has data
  128. * @filp: File structure pointer for device access
  129. * @wait: Poll table structure pointer for which the driver adds
  130. * a wait queue
  131. *
  132. * Return: (POLLIN | POLLRDNORM) if data is available for reading
  133. * or 0 for other cases
  134. */
  135. unsigned int iio_buffer_poll(struct file *filp,
  136. struct poll_table_struct *wait)
  137. {
  138. struct iio_dev *indio_dev = filp->private_data;
  139. struct iio_buffer *rb = indio_dev->buffer;
  140. if (!indio_dev->info)
  141. return 0;
  142. poll_wait(filp, &rb->pollq, wait);
  143. if (iio_buffer_ready(indio_dev, rb, rb->watermark, 0))
  144. return POLLIN | POLLRDNORM;
  145. return 0;
  146. }
  147. /**
  148. * iio_buffer_wakeup_poll - Wakes up the buffer waitqueue
  149. * @indio_dev: The IIO device
  150. *
  151. * Wakes up the event waitqueue used for poll(). Should usually
  152. * be called when the device is unregistered.
  153. */
  154. void iio_buffer_wakeup_poll(struct iio_dev *indio_dev)
  155. {
  156. if (!indio_dev->buffer)
  157. return;
  158. wake_up(&indio_dev->buffer->pollq);
  159. }
  160. void iio_buffer_init(struct iio_buffer *buffer)
  161. {
  162. INIT_LIST_HEAD(&buffer->demux_list);
  163. INIT_LIST_HEAD(&buffer->buffer_list);
  164. init_waitqueue_head(&buffer->pollq);
  165. kref_init(&buffer->ref);
  166. if (!buffer->watermark)
  167. buffer->watermark = 1;
  168. }
  169. EXPORT_SYMBOL(iio_buffer_init);
  170. static ssize_t iio_show_scan_index(struct device *dev,
  171. struct device_attribute *attr,
  172. char *buf)
  173. {
  174. return sprintf(buf, "%u\n", to_iio_dev_attr(attr)->c->scan_index);
  175. }
  176. static ssize_t iio_show_fixed_type(struct device *dev,
  177. struct device_attribute *attr,
  178. char *buf)
  179. {
  180. struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
  181. u8 type = this_attr->c->scan_type.endianness;
  182. if (type == IIO_CPU) {
  183. #ifdef __LITTLE_ENDIAN
  184. type = IIO_LE;
  185. #else
  186. type = IIO_BE;
  187. #endif
  188. }
  189. if (this_attr->c->scan_type.repeat > 1)
  190. return sprintf(buf, "%s:%c%d/%dX%d>>%u\n",
  191. iio_endian_prefix[type],
  192. this_attr->c->scan_type.sign,
  193. this_attr->c->scan_type.realbits,
  194. this_attr->c->scan_type.storagebits,
  195. this_attr->c->scan_type.repeat,
  196. this_attr->c->scan_type.shift);
  197. else
  198. return sprintf(buf, "%s:%c%d/%d>>%u\n",
  199. iio_endian_prefix[type],
  200. this_attr->c->scan_type.sign,
  201. this_attr->c->scan_type.realbits,
  202. this_attr->c->scan_type.storagebits,
  203. this_attr->c->scan_type.shift);
  204. }
  205. static ssize_t iio_scan_el_show(struct device *dev,
  206. struct device_attribute *attr,
  207. char *buf)
  208. {
  209. int ret;
  210. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  211. /* Ensure ret is 0 or 1. */
  212. ret = !!test_bit(to_iio_dev_attr(attr)->address,
  213. indio_dev->buffer->scan_mask);
  214. return sprintf(buf, "%d\n", ret);
  215. }
  216. /* Note NULL used as error indicator as it doesn't make sense. */
  217. static const unsigned long *iio_scan_mask_match(const unsigned long *av_masks,
  218. unsigned int masklength,
  219. const unsigned long *mask,
  220. bool strict)
  221. {
  222. if (bitmap_empty(mask, masklength))
  223. return NULL;
  224. while (*av_masks) {
  225. if (strict) {
  226. if (bitmap_equal(mask, av_masks, masklength))
  227. return av_masks;
  228. } else {
  229. if (bitmap_subset(mask, av_masks, masklength))
  230. return av_masks;
  231. }
  232. av_masks += BITS_TO_LONGS(masklength);
  233. }
  234. return NULL;
  235. }
  236. static bool iio_validate_scan_mask(struct iio_dev *indio_dev,
  237. const unsigned long *mask)
  238. {
  239. if (!indio_dev->setup_ops->validate_scan_mask)
  240. return true;
  241. return indio_dev->setup_ops->validate_scan_mask(indio_dev, mask);
  242. }
  243. /**
  244. * iio_scan_mask_set() - set particular bit in the scan mask
  245. * @indio_dev: the iio device
  246. * @buffer: the buffer whose scan mask we are interested in
  247. * @bit: the bit to be set.
  248. *
  249. * Note that at this point we have no way of knowing what other
  250. * buffers might request, hence this code only verifies that the
  251. * individual buffers request is plausible.
  252. */
  253. static int iio_scan_mask_set(struct iio_dev *indio_dev,
  254. struct iio_buffer *buffer, int bit)
  255. {
  256. const unsigned long *mask;
  257. unsigned long *trialmask;
  258. trialmask = kmalloc(sizeof(*trialmask)*
  259. BITS_TO_LONGS(indio_dev->masklength),
  260. GFP_KERNEL);
  261. if (trialmask == NULL)
  262. return -ENOMEM;
  263. if (!indio_dev->masklength) {
  264. WARN(1, "Trying to set scanmask prior to registering buffer\n");
  265. goto err_invalid_mask;
  266. }
  267. bitmap_copy(trialmask, buffer->scan_mask, indio_dev->masklength);
  268. set_bit(bit, trialmask);
  269. if (!iio_validate_scan_mask(indio_dev, trialmask))
  270. goto err_invalid_mask;
  271. if (indio_dev->available_scan_masks) {
  272. mask = iio_scan_mask_match(indio_dev->available_scan_masks,
  273. indio_dev->masklength,
  274. trialmask, false);
  275. if (!mask)
  276. goto err_invalid_mask;
  277. }
  278. bitmap_copy(buffer->scan_mask, trialmask, indio_dev->masklength);
  279. kfree(trialmask);
  280. return 0;
  281. err_invalid_mask:
  282. kfree(trialmask);
  283. return -EINVAL;
  284. }
  285. static int iio_scan_mask_clear(struct iio_buffer *buffer, int bit)
  286. {
  287. clear_bit(bit, buffer->scan_mask);
  288. return 0;
  289. }
  290. static ssize_t iio_scan_el_store(struct device *dev,
  291. struct device_attribute *attr,
  292. const char *buf,
  293. size_t len)
  294. {
  295. int ret;
  296. bool state;
  297. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  298. struct iio_buffer *buffer = indio_dev->buffer;
  299. struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
  300. ret = strtobool(buf, &state);
  301. if (ret < 0)
  302. return ret;
  303. mutex_lock(&indio_dev->mlock);
  304. if (iio_buffer_is_active(indio_dev->buffer)) {
  305. ret = -EBUSY;
  306. goto error_ret;
  307. }
  308. ret = iio_scan_mask_query(indio_dev, buffer, this_attr->address);
  309. if (ret < 0)
  310. goto error_ret;
  311. if (!state && ret) {
  312. ret = iio_scan_mask_clear(buffer, this_attr->address);
  313. if (ret)
  314. goto error_ret;
  315. } else if (state && !ret) {
  316. ret = iio_scan_mask_set(indio_dev, buffer, this_attr->address);
  317. if (ret)
  318. goto error_ret;
  319. }
  320. error_ret:
  321. mutex_unlock(&indio_dev->mlock);
  322. return ret < 0 ? ret : len;
  323. }
  324. static ssize_t iio_scan_el_ts_show(struct device *dev,
  325. struct device_attribute *attr,
  326. char *buf)
  327. {
  328. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  329. return sprintf(buf, "%d\n", indio_dev->buffer->scan_timestamp);
  330. }
  331. static ssize_t iio_scan_el_ts_store(struct device *dev,
  332. struct device_attribute *attr,
  333. const char *buf,
  334. size_t len)
  335. {
  336. int ret;
  337. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  338. bool state;
  339. ret = strtobool(buf, &state);
  340. if (ret < 0)
  341. return ret;
  342. mutex_lock(&indio_dev->mlock);
  343. if (iio_buffer_is_active(indio_dev->buffer)) {
  344. ret = -EBUSY;
  345. goto error_ret;
  346. }
  347. indio_dev->buffer->scan_timestamp = state;
  348. error_ret:
  349. mutex_unlock(&indio_dev->mlock);
  350. return ret ? ret : len;
  351. }
  352. static int iio_buffer_add_channel_sysfs(struct iio_dev *indio_dev,
  353. const struct iio_chan_spec *chan)
  354. {
  355. int ret, attrcount = 0;
  356. struct iio_buffer *buffer = indio_dev->buffer;
  357. ret = __iio_add_chan_devattr("index",
  358. chan,
  359. &iio_show_scan_index,
  360. NULL,
  361. 0,
  362. IIO_SEPARATE,
  363. &indio_dev->dev,
  364. &buffer->scan_el_dev_attr_list);
  365. if (ret)
  366. return ret;
  367. attrcount++;
  368. ret = __iio_add_chan_devattr("type",
  369. chan,
  370. &iio_show_fixed_type,
  371. NULL,
  372. 0,
  373. 0,
  374. &indio_dev->dev,
  375. &buffer->scan_el_dev_attr_list);
  376. if (ret)
  377. return ret;
  378. attrcount++;
  379. if (chan->type != IIO_TIMESTAMP)
  380. ret = __iio_add_chan_devattr("en",
  381. chan,
  382. &iio_scan_el_show,
  383. &iio_scan_el_store,
  384. chan->scan_index,
  385. 0,
  386. &indio_dev->dev,
  387. &buffer->scan_el_dev_attr_list);
  388. else
  389. ret = __iio_add_chan_devattr("en",
  390. chan,
  391. &iio_scan_el_ts_show,
  392. &iio_scan_el_ts_store,
  393. chan->scan_index,
  394. 0,
  395. &indio_dev->dev,
  396. &buffer->scan_el_dev_attr_list);
  397. if (ret)
  398. return ret;
  399. attrcount++;
  400. ret = attrcount;
  401. return ret;
  402. }
  403. static ssize_t iio_buffer_read_length(struct device *dev,
  404. struct device_attribute *attr,
  405. char *buf)
  406. {
  407. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  408. struct iio_buffer *buffer = indio_dev->buffer;
  409. return sprintf(buf, "%d\n", buffer->length);
  410. }
  411. static ssize_t iio_buffer_write_length(struct device *dev,
  412. struct device_attribute *attr,
  413. const char *buf, size_t len)
  414. {
  415. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  416. struct iio_buffer *buffer = indio_dev->buffer;
  417. unsigned int val;
  418. int ret;
  419. ret = kstrtouint(buf, 10, &val);
  420. if (ret)
  421. return ret;
  422. if (val == buffer->length)
  423. return len;
  424. mutex_lock(&indio_dev->mlock);
  425. if (iio_buffer_is_active(indio_dev->buffer)) {
  426. ret = -EBUSY;
  427. } else {
  428. buffer->access->set_length(buffer, val);
  429. ret = 0;
  430. }
  431. if (ret)
  432. goto out;
  433. if (buffer->length && buffer->length < buffer->watermark)
  434. buffer->watermark = buffer->length;
  435. out:
  436. mutex_unlock(&indio_dev->mlock);
  437. return ret ? ret : len;
  438. }
  439. static ssize_t iio_buffer_show_enable(struct device *dev,
  440. struct device_attribute *attr,
  441. char *buf)
  442. {
  443. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  444. return sprintf(buf, "%d\n", iio_buffer_is_active(indio_dev->buffer));
  445. }
  446. static unsigned int iio_storage_bytes_for_si(struct iio_dev *indio_dev,
  447. unsigned int scan_index)
  448. {
  449. const struct iio_chan_spec *ch;
  450. unsigned int bytes;
  451. ch = iio_find_channel_from_si(indio_dev, scan_index);
  452. bytes = ch->scan_type.storagebits / 8;
  453. if (ch->scan_type.repeat > 1)
  454. bytes *= ch->scan_type.repeat;
  455. return bytes;
  456. }
  457. static unsigned int iio_storage_bytes_for_timestamp(struct iio_dev *indio_dev)
  458. {
  459. return iio_storage_bytes_for_si(indio_dev,
  460. indio_dev->scan_index_timestamp);
  461. }
  462. static int iio_compute_scan_bytes(struct iio_dev *indio_dev,
  463. const unsigned long *mask, bool timestamp)
  464. {
  465. unsigned bytes = 0;
  466. int length, i;
  467. /* How much space will the demuxed element take? */
  468. for_each_set_bit(i, mask,
  469. indio_dev->masklength) {
  470. length = iio_storage_bytes_for_si(indio_dev, i);
  471. bytes = ALIGN(bytes, length);
  472. bytes += length;
  473. }
  474. if (timestamp) {
  475. length = iio_storage_bytes_for_timestamp(indio_dev);
  476. bytes = ALIGN(bytes, length);
  477. bytes += length;
  478. }
  479. return bytes;
  480. }
  481. static void iio_buffer_activate(struct iio_dev *indio_dev,
  482. struct iio_buffer *buffer)
  483. {
  484. iio_buffer_get(buffer);
  485. list_add(&buffer->buffer_list, &indio_dev->buffer_list);
  486. }
  487. static void iio_buffer_deactivate(struct iio_buffer *buffer)
  488. {
  489. list_del_init(&buffer->buffer_list);
  490. wake_up_interruptible(&buffer->pollq);
  491. iio_buffer_put(buffer);
  492. }
  493. static void iio_buffer_deactivate_all(struct iio_dev *indio_dev)
  494. {
  495. struct iio_buffer *buffer, *_buffer;
  496. list_for_each_entry_safe(buffer, _buffer,
  497. &indio_dev->buffer_list, buffer_list)
  498. iio_buffer_deactivate(buffer);
  499. }
  500. static int iio_buffer_enable(struct iio_buffer *buffer,
  501. struct iio_dev *indio_dev)
  502. {
  503. if (!buffer->access->enable)
  504. return 0;
  505. return buffer->access->enable(buffer, indio_dev);
  506. }
  507. static int iio_buffer_disable(struct iio_buffer *buffer,
  508. struct iio_dev *indio_dev)
  509. {
  510. if (!buffer->access->disable)
  511. return 0;
  512. return buffer->access->disable(buffer, indio_dev);
  513. }
  514. static void iio_buffer_update_bytes_per_datum(struct iio_dev *indio_dev,
  515. struct iio_buffer *buffer)
  516. {
  517. unsigned int bytes;
  518. if (!buffer->access->set_bytes_per_datum)
  519. return;
  520. bytes = iio_compute_scan_bytes(indio_dev, buffer->scan_mask,
  521. buffer->scan_timestamp);
  522. buffer->access->set_bytes_per_datum(buffer, bytes);
  523. }
  524. static int iio_buffer_request_update(struct iio_dev *indio_dev,
  525. struct iio_buffer *buffer)
  526. {
  527. int ret;
  528. iio_buffer_update_bytes_per_datum(indio_dev, buffer);
  529. if (buffer->access->request_update) {
  530. ret = buffer->access->request_update(buffer);
  531. if (ret) {
  532. dev_dbg(&indio_dev->dev,
  533. "Buffer not started: buffer parameter update failed (%d)\n",
  534. ret);
  535. return ret;
  536. }
  537. }
  538. return 0;
  539. }
  540. static void iio_free_scan_mask(struct iio_dev *indio_dev,
  541. const unsigned long *mask)
  542. {
  543. /* If the mask is dynamically allocated free it, otherwise do nothing */
  544. if (!indio_dev->available_scan_masks)
  545. kfree(mask);
  546. }
  547. struct iio_device_config {
  548. unsigned int mode;
  549. unsigned int watermark;
  550. const unsigned long *scan_mask;
  551. unsigned int scan_bytes;
  552. bool scan_timestamp;
  553. };
  554. static int iio_verify_update(struct iio_dev *indio_dev,
  555. struct iio_buffer *insert_buffer, struct iio_buffer *remove_buffer,
  556. struct iio_device_config *config)
  557. {
  558. unsigned long *compound_mask;
  559. const unsigned long *scan_mask;
  560. bool strict_scanmask = false;
  561. struct iio_buffer *buffer;
  562. bool scan_timestamp;
  563. unsigned int modes;
  564. memset(config, 0, sizeof(*config));
  565. /*
  566. * If there is just one buffer and we are removing it there is nothing
  567. * to verify.
  568. */
  569. if (remove_buffer && !insert_buffer &&
  570. list_is_singular(&indio_dev->buffer_list))
  571. return 0;
  572. modes = indio_dev->modes;
  573. list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list) {
  574. if (buffer == remove_buffer)
  575. continue;
  576. modes &= buffer->access->modes;
  577. config->watermark = min(config->watermark, buffer->watermark);
  578. }
  579. if (insert_buffer) {
  580. modes &= insert_buffer->access->modes;
  581. config->watermark = min(config->watermark,
  582. insert_buffer->watermark);
  583. }
  584. /* Definitely possible for devices to support both of these. */
  585. if ((modes & INDIO_BUFFER_TRIGGERED) && indio_dev->trig) {
  586. config->mode = INDIO_BUFFER_TRIGGERED;
  587. } else if (modes & INDIO_BUFFER_HARDWARE) {
  588. /*
  589. * Keep things simple for now and only allow a single buffer to
  590. * be connected in hardware mode.
  591. */
  592. if (insert_buffer && !list_empty(&indio_dev->buffer_list))
  593. return -EINVAL;
  594. config->mode = INDIO_BUFFER_HARDWARE;
  595. strict_scanmask = true;
  596. } else if (modes & INDIO_BUFFER_SOFTWARE) {
  597. config->mode = INDIO_BUFFER_SOFTWARE;
  598. } else {
  599. /* Can only occur on first buffer */
  600. if (indio_dev->modes & INDIO_BUFFER_TRIGGERED)
  601. dev_dbg(&indio_dev->dev, "Buffer not started: no trigger\n");
  602. return -EINVAL;
  603. }
  604. /* What scan mask do we actually have? */
  605. compound_mask = kcalloc(BITS_TO_LONGS(indio_dev->masklength),
  606. sizeof(long), GFP_KERNEL);
  607. if (compound_mask == NULL)
  608. return -ENOMEM;
  609. scan_timestamp = false;
  610. list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list) {
  611. if (buffer == remove_buffer)
  612. continue;
  613. bitmap_or(compound_mask, compound_mask, buffer->scan_mask,
  614. indio_dev->masklength);
  615. scan_timestamp |= buffer->scan_timestamp;
  616. }
  617. if (insert_buffer) {
  618. bitmap_or(compound_mask, compound_mask,
  619. insert_buffer->scan_mask, indio_dev->masklength);
  620. scan_timestamp |= insert_buffer->scan_timestamp;
  621. }
  622. if (indio_dev->available_scan_masks) {
  623. scan_mask = iio_scan_mask_match(indio_dev->available_scan_masks,
  624. indio_dev->masklength,
  625. compound_mask,
  626. strict_scanmask);
  627. kfree(compound_mask);
  628. if (scan_mask == NULL)
  629. return -EINVAL;
  630. } else {
  631. scan_mask = compound_mask;
  632. }
  633. config->scan_bytes = iio_compute_scan_bytes(indio_dev,
  634. scan_mask, scan_timestamp);
  635. config->scan_mask = scan_mask;
  636. config->scan_timestamp = scan_timestamp;
  637. return 0;
  638. }
  639. static int iio_enable_buffers(struct iio_dev *indio_dev,
  640. struct iio_device_config *config)
  641. {
  642. struct iio_buffer *buffer;
  643. int ret;
  644. indio_dev->active_scan_mask = config->scan_mask;
  645. indio_dev->scan_timestamp = config->scan_timestamp;
  646. indio_dev->scan_bytes = config->scan_bytes;
  647. iio_update_demux(indio_dev);
  648. /* Wind up again */
  649. if (indio_dev->setup_ops->preenable) {
  650. ret = indio_dev->setup_ops->preenable(indio_dev);
  651. if (ret) {
  652. dev_dbg(&indio_dev->dev,
  653. "Buffer not started: buffer preenable failed (%d)\n", ret);
  654. goto err_undo_config;
  655. }
  656. }
  657. if (indio_dev->info->update_scan_mode) {
  658. ret = indio_dev->info
  659. ->update_scan_mode(indio_dev,
  660. indio_dev->active_scan_mask);
  661. if (ret < 0) {
  662. dev_dbg(&indio_dev->dev,
  663. "Buffer not started: update scan mode failed (%d)\n",
  664. ret);
  665. goto err_run_postdisable;
  666. }
  667. }
  668. if (indio_dev->info->hwfifo_set_watermark)
  669. indio_dev->info->hwfifo_set_watermark(indio_dev,
  670. config->watermark);
  671. list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list) {
  672. ret = iio_buffer_enable(buffer, indio_dev);
  673. if (ret)
  674. goto err_disable_buffers;
  675. }
  676. indio_dev->currentmode = config->mode;
  677. if (indio_dev->setup_ops->postenable) {
  678. ret = indio_dev->setup_ops->postenable(indio_dev);
  679. if (ret) {
  680. dev_dbg(&indio_dev->dev,
  681. "Buffer not started: postenable failed (%d)\n", ret);
  682. goto err_disable_buffers;
  683. }
  684. }
  685. return 0;
  686. err_disable_buffers:
  687. list_for_each_entry_continue_reverse(buffer, &indio_dev->buffer_list,
  688. buffer_list)
  689. iio_buffer_disable(buffer, indio_dev);
  690. err_run_postdisable:
  691. indio_dev->currentmode = INDIO_DIRECT_MODE;
  692. if (indio_dev->setup_ops->postdisable)
  693. indio_dev->setup_ops->postdisable(indio_dev);
  694. err_undo_config:
  695. indio_dev->active_scan_mask = NULL;
  696. return ret;
  697. }
  698. static int iio_disable_buffers(struct iio_dev *indio_dev)
  699. {
  700. struct iio_buffer *buffer;
  701. int ret = 0;
  702. int ret2;
  703. /* Wind down existing buffers - iff there are any */
  704. if (list_empty(&indio_dev->buffer_list))
  705. return 0;
  706. /*
  707. * If things go wrong at some step in disable we still need to continue
  708. * to perform the other steps, otherwise we leave the device in a
  709. * inconsistent state. We return the error code for the first error we
  710. * encountered.
  711. */
  712. if (indio_dev->setup_ops->predisable) {
  713. ret2 = indio_dev->setup_ops->predisable(indio_dev);
  714. if (ret2 && !ret)
  715. ret = ret2;
  716. }
  717. list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list) {
  718. ret2 = iio_buffer_disable(buffer, indio_dev);
  719. if (ret2 && !ret)
  720. ret = ret2;
  721. }
  722. indio_dev->currentmode = INDIO_DIRECT_MODE;
  723. if (indio_dev->setup_ops->postdisable) {
  724. ret2 = indio_dev->setup_ops->postdisable(indio_dev);
  725. if (ret2 && !ret)
  726. ret = ret2;
  727. }
  728. iio_free_scan_mask(indio_dev, indio_dev->active_scan_mask);
  729. indio_dev->active_scan_mask = NULL;
  730. return ret;
  731. }
  732. static int __iio_update_buffers(struct iio_dev *indio_dev,
  733. struct iio_buffer *insert_buffer,
  734. struct iio_buffer *remove_buffer)
  735. {
  736. struct iio_device_config new_config;
  737. int ret;
  738. ret = iio_verify_update(indio_dev, insert_buffer, remove_buffer,
  739. &new_config);
  740. if (ret)
  741. return ret;
  742. if (insert_buffer) {
  743. ret = iio_buffer_request_update(indio_dev, insert_buffer);
  744. if (ret)
  745. goto err_free_config;
  746. }
  747. ret = iio_disable_buffers(indio_dev);
  748. if (ret)
  749. goto err_deactivate_all;
  750. if (remove_buffer)
  751. iio_buffer_deactivate(remove_buffer);
  752. if (insert_buffer)
  753. iio_buffer_activate(indio_dev, insert_buffer);
  754. /* If no buffers in list, we are done */
  755. if (list_empty(&indio_dev->buffer_list))
  756. return 0;
  757. ret = iio_enable_buffers(indio_dev, &new_config);
  758. if (ret)
  759. goto err_deactivate_all;
  760. return 0;
  761. err_deactivate_all:
  762. /*
  763. * We've already verified that the config is valid earlier. If things go
  764. * wrong in either enable or disable the most likely reason is an IO
  765. * error from the device. In this case there is no good recovery
  766. * strategy. Just make sure to disable everything and leave the device
  767. * in a sane state. With a bit of luck the device might come back to
  768. * life again later and userspace can try again.
  769. */
  770. iio_buffer_deactivate_all(indio_dev);
  771. err_free_config:
  772. iio_free_scan_mask(indio_dev, new_config.scan_mask);
  773. return ret;
  774. }
  775. int iio_update_buffers(struct iio_dev *indio_dev,
  776. struct iio_buffer *insert_buffer,
  777. struct iio_buffer *remove_buffer)
  778. {
  779. int ret;
  780. if (insert_buffer == remove_buffer)
  781. return 0;
  782. mutex_lock(&indio_dev->info_exist_lock);
  783. mutex_lock(&indio_dev->mlock);
  784. if (insert_buffer && iio_buffer_is_active(insert_buffer))
  785. insert_buffer = NULL;
  786. if (remove_buffer && !iio_buffer_is_active(remove_buffer))
  787. remove_buffer = NULL;
  788. if (!insert_buffer && !remove_buffer) {
  789. ret = 0;
  790. goto out_unlock;
  791. }
  792. if (indio_dev->info == NULL) {
  793. ret = -ENODEV;
  794. goto out_unlock;
  795. }
  796. ret = __iio_update_buffers(indio_dev, insert_buffer, remove_buffer);
  797. out_unlock:
  798. mutex_unlock(&indio_dev->mlock);
  799. mutex_unlock(&indio_dev->info_exist_lock);
  800. return ret;
  801. }
  802. EXPORT_SYMBOL_GPL(iio_update_buffers);
  803. void iio_disable_all_buffers(struct iio_dev *indio_dev)
  804. {
  805. iio_disable_buffers(indio_dev);
  806. iio_buffer_deactivate_all(indio_dev);
  807. }
  808. static ssize_t iio_buffer_store_enable(struct device *dev,
  809. struct device_attribute *attr,
  810. const char *buf,
  811. size_t len)
  812. {
  813. int ret;
  814. bool requested_state;
  815. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  816. bool inlist;
  817. ret = strtobool(buf, &requested_state);
  818. if (ret < 0)
  819. return ret;
  820. mutex_lock(&indio_dev->mlock);
  821. /* Find out if it is in the list */
  822. inlist = iio_buffer_is_active(indio_dev->buffer);
  823. /* Already in desired state */
  824. if (inlist == requested_state)
  825. goto done;
  826. if (requested_state)
  827. ret = __iio_update_buffers(indio_dev,
  828. indio_dev->buffer, NULL);
  829. else
  830. ret = __iio_update_buffers(indio_dev,
  831. NULL, indio_dev->buffer);
  832. done:
  833. mutex_unlock(&indio_dev->mlock);
  834. return (ret < 0) ? ret : len;
  835. }
  836. static const char * const iio_scan_elements_group_name = "scan_elements";
  837. static ssize_t iio_buffer_show_watermark(struct device *dev,
  838. struct device_attribute *attr,
  839. char *buf)
  840. {
  841. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  842. struct iio_buffer *buffer = indio_dev->buffer;
  843. return sprintf(buf, "%u\n", buffer->watermark);
  844. }
  845. static ssize_t iio_buffer_store_watermark(struct device *dev,
  846. struct device_attribute *attr,
  847. const char *buf,
  848. size_t len)
  849. {
  850. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  851. struct iio_buffer *buffer = indio_dev->buffer;
  852. unsigned int val;
  853. int ret;
  854. ret = kstrtouint(buf, 10, &val);
  855. if (ret)
  856. return ret;
  857. if (!val)
  858. return -EINVAL;
  859. mutex_lock(&indio_dev->mlock);
  860. if (val > buffer->length) {
  861. ret = -EINVAL;
  862. goto out;
  863. }
  864. if (iio_buffer_is_active(indio_dev->buffer)) {
  865. ret = -EBUSY;
  866. goto out;
  867. }
  868. buffer->watermark = val;
  869. out:
  870. mutex_unlock(&indio_dev->mlock);
  871. return ret ? ret : len;
  872. }
  873. static DEVICE_ATTR(length, S_IRUGO | S_IWUSR, iio_buffer_read_length,
  874. iio_buffer_write_length);
  875. static struct device_attribute dev_attr_length_ro = __ATTR(length,
  876. S_IRUGO, iio_buffer_read_length, NULL);
  877. static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR,
  878. iio_buffer_show_enable, iio_buffer_store_enable);
  879. static DEVICE_ATTR(watermark, S_IRUGO | S_IWUSR,
  880. iio_buffer_show_watermark, iio_buffer_store_watermark);
  881. static struct device_attribute dev_attr_watermark_ro = __ATTR(watermark,
  882. S_IRUGO, iio_buffer_show_watermark, NULL);
  883. static struct attribute *iio_buffer_attrs[] = {
  884. &dev_attr_length.attr,
  885. &dev_attr_enable.attr,
  886. &dev_attr_watermark.attr,
  887. };
  888. int iio_buffer_alloc_sysfs_and_mask(struct iio_dev *indio_dev)
  889. {
  890. struct iio_dev_attr *p;
  891. struct attribute **attr;
  892. struct iio_buffer *buffer = indio_dev->buffer;
  893. int ret, i, attrn, attrcount, attrcount_orig = 0;
  894. const struct iio_chan_spec *channels;
  895. channels = indio_dev->channels;
  896. if (channels) {
  897. int ml = indio_dev->masklength;
  898. for (i = 0; i < indio_dev->num_channels; i++)
  899. ml = max(ml, channels[i].scan_index + 1);
  900. indio_dev->masklength = ml;
  901. }
  902. if (!buffer)
  903. return 0;
  904. attrcount = 0;
  905. if (buffer->attrs) {
  906. while (buffer->attrs[attrcount] != NULL)
  907. attrcount++;
  908. }
  909. attr = kcalloc(attrcount + ARRAY_SIZE(iio_buffer_attrs) + 1,
  910. sizeof(struct attribute *), GFP_KERNEL);
  911. if (!attr)
  912. return -ENOMEM;
  913. memcpy(attr, iio_buffer_attrs, sizeof(iio_buffer_attrs));
  914. if (!buffer->access->set_length)
  915. attr[0] = &dev_attr_length_ro.attr;
  916. if (buffer->access->flags & INDIO_BUFFER_FLAG_FIXED_WATERMARK)
  917. attr[2] = &dev_attr_watermark_ro.attr;
  918. if (buffer->attrs)
  919. memcpy(&attr[ARRAY_SIZE(iio_buffer_attrs)], buffer->attrs,
  920. sizeof(struct attribute *) * attrcount);
  921. attr[attrcount + ARRAY_SIZE(iio_buffer_attrs)] = NULL;
  922. buffer->buffer_group.name = "buffer";
  923. buffer->buffer_group.attrs = attr;
  924. indio_dev->groups[indio_dev->groupcounter++] = &buffer->buffer_group;
  925. if (buffer->scan_el_attrs != NULL) {
  926. attr = buffer->scan_el_attrs->attrs;
  927. while (*attr++ != NULL)
  928. attrcount_orig++;
  929. }
  930. attrcount = attrcount_orig;
  931. INIT_LIST_HEAD(&buffer->scan_el_dev_attr_list);
  932. channels = indio_dev->channels;
  933. if (channels) {
  934. /* new magic */
  935. for (i = 0; i < indio_dev->num_channels; i++) {
  936. if (channels[i].scan_index < 0)
  937. continue;
  938. ret = iio_buffer_add_channel_sysfs(indio_dev,
  939. &channels[i]);
  940. if (ret < 0)
  941. goto error_cleanup_dynamic;
  942. attrcount += ret;
  943. if (channels[i].type == IIO_TIMESTAMP)
  944. indio_dev->scan_index_timestamp =
  945. channels[i].scan_index;
  946. }
  947. if (indio_dev->masklength && buffer->scan_mask == NULL) {
  948. buffer->scan_mask = kcalloc(BITS_TO_LONGS(indio_dev->masklength),
  949. sizeof(*buffer->scan_mask),
  950. GFP_KERNEL);
  951. if (buffer->scan_mask == NULL) {
  952. ret = -ENOMEM;
  953. goto error_cleanup_dynamic;
  954. }
  955. }
  956. }
  957. buffer->scan_el_group.name = iio_scan_elements_group_name;
  958. buffer->scan_el_group.attrs = kcalloc(attrcount + 1,
  959. sizeof(buffer->scan_el_group.attrs[0]),
  960. GFP_KERNEL);
  961. if (buffer->scan_el_group.attrs == NULL) {
  962. ret = -ENOMEM;
  963. goto error_free_scan_mask;
  964. }
  965. if (buffer->scan_el_attrs)
  966. memcpy(buffer->scan_el_group.attrs, buffer->scan_el_attrs,
  967. sizeof(buffer->scan_el_group.attrs[0])*attrcount_orig);
  968. attrn = attrcount_orig;
  969. list_for_each_entry(p, &buffer->scan_el_dev_attr_list, l)
  970. buffer->scan_el_group.attrs[attrn++] = &p->dev_attr.attr;
  971. indio_dev->groups[indio_dev->groupcounter++] = &buffer->scan_el_group;
  972. return 0;
  973. error_free_scan_mask:
  974. kfree(buffer->scan_mask);
  975. error_cleanup_dynamic:
  976. iio_free_chan_devattr_list(&buffer->scan_el_dev_attr_list);
  977. kfree(indio_dev->buffer->buffer_group.attrs);
  978. return ret;
  979. }
  980. void iio_buffer_free_sysfs_and_mask(struct iio_dev *indio_dev)
  981. {
  982. if (!indio_dev->buffer)
  983. return;
  984. kfree(indio_dev->buffer->scan_mask);
  985. kfree(indio_dev->buffer->buffer_group.attrs);
  986. kfree(indio_dev->buffer->scan_el_group.attrs);
  987. iio_free_chan_devattr_list(&indio_dev->buffer->scan_el_dev_attr_list);
  988. }
  989. /**
  990. * iio_validate_scan_mask_onehot() - Validates that exactly one channel is selected
  991. * @indio_dev: the iio device
  992. * @mask: scan mask to be checked
  993. *
  994. * Return true if exactly one bit is set in the scan mask, false otherwise. It
  995. * can be used for devices where only one channel can be active for sampling at
  996. * a time.
  997. */
  998. bool iio_validate_scan_mask_onehot(struct iio_dev *indio_dev,
  999. const unsigned long *mask)
  1000. {
  1001. return bitmap_weight(mask, indio_dev->masklength) == 1;
  1002. }
  1003. EXPORT_SYMBOL_GPL(iio_validate_scan_mask_onehot);
  1004. int iio_scan_mask_query(struct iio_dev *indio_dev,
  1005. struct iio_buffer *buffer, int bit)
  1006. {
  1007. if (bit > indio_dev->masklength)
  1008. return -EINVAL;
  1009. if (!buffer->scan_mask)
  1010. return 0;
  1011. /* Ensure return value is 0 or 1. */
  1012. return !!test_bit(bit, buffer->scan_mask);
  1013. };
  1014. EXPORT_SYMBOL_GPL(iio_scan_mask_query);
  1015. /**
  1016. * struct iio_demux_table - table describing demux memcpy ops
  1017. * @from: index to copy from
  1018. * @to: index to copy to
  1019. * @length: how many bytes to copy
  1020. * @l: list head used for management
  1021. */
  1022. struct iio_demux_table {
  1023. unsigned from;
  1024. unsigned to;
  1025. unsigned length;
  1026. struct list_head l;
  1027. };
  1028. static const void *iio_demux(struct iio_buffer *buffer,
  1029. const void *datain)
  1030. {
  1031. struct iio_demux_table *t;
  1032. if (list_empty(&buffer->demux_list))
  1033. return datain;
  1034. list_for_each_entry(t, &buffer->demux_list, l)
  1035. memcpy(buffer->demux_bounce + t->to,
  1036. datain + t->from, t->length);
  1037. return buffer->demux_bounce;
  1038. }
  1039. static int iio_push_to_buffer(struct iio_buffer *buffer, const void *data)
  1040. {
  1041. const void *dataout = iio_demux(buffer, data);
  1042. int ret;
  1043. ret = buffer->access->store_to(buffer, dataout);
  1044. if (ret)
  1045. return ret;
  1046. /*
  1047. * We can't just test for watermark to decide if we wake the poll queue
  1048. * because read may request less samples than the watermark.
  1049. */
  1050. wake_up_interruptible_poll(&buffer->pollq, POLLIN | POLLRDNORM);
  1051. return 0;
  1052. }
  1053. static void iio_buffer_demux_free(struct iio_buffer *buffer)
  1054. {
  1055. struct iio_demux_table *p, *q;
  1056. list_for_each_entry_safe(p, q, &buffer->demux_list, l) {
  1057. list_del(&p->l);
  1058. kfree(p);
  1059. }
  1060. }
  1061. int iio_push_to_buffers(struct iio_dev *indio_dev, const void *data)
  1062. {
  1063. int ret;
  1064. struct iio_buffer *buf;
  1065. list_for_each_entry(buf, &indio_dev->buffer_list, buffer_list) {
  1066. ret = iio_push_to_buffer(buf, data);
  1067. if (ret < 0)
  1068. return ret;
  1069. }
  1070. return 0;
  1071. }
  1072. EXPORT_SYMBOL_GPL(iio_push_to_buffers);
  1073. static int iio_buffer_add_demux(struct iio_buffer *buffer,
  1074. struct iio_demux_table **p, unsigned int in_loc, unsigned int out_loc,
  1075. unsigned int length)
  1076. {
  1077. if (*p && (*p)->from + (*p)->length == in_loc &&
  1078. (*p)->to + (*p)->length == out_loc) {
  1079. (*p)->length += length;
  1080. } else {
  1081. *p = kmalloc(sizeof(**p), GFP_KERNEL);
  1082. if (*p == NULL)
  1083. return -ENOMEM;
  1084. (*p)->from = in_loc;
  1085. (*p)->to = out_loc;
  1086. (*p)->length = length;
  1087. list_add_tail(&(*p)->l, &buffer->demux_list);
  1088. }
  1089. return 0;
  1090. }
  1091. static int iio_buffer_update_demux(struct iio_dev *indio_dev,
  1092. struct iio_buffer *buffer)
  1093. {
  1094. int ret, in_ind = -1, out_ind, length;
  1095. unsigned in_loc = 0, out_loc = 0;
  1096. struct iio_demux_table *p = NULL;
  1097. /* Clear out any old demux */
  1098. iio_buffer_demux_free(buffer);
  1099. kfree(buffer->demux_bounce);
  1100. buffer->demux_bounce = NULL;
  1101. /* First work out which scan mode we will actually have */
  1102. if (bitmap_equal(indio_dev->active_scan_mask,
  1103. buffer->scan_mask,
  1104. indio_dev->masklength))
  1105. return 0;
  1106. /* Now we have the two masks, work from least sig and build up sizes */
  1107. for_each_set_bit(out_ind,
  1108. buffer->scan_mask,
  1109. indio_dev->masklength) {
  1110. in_ind = find_next_bit(indio_dev->active_scan_mask,
  1111. indio_dev->masklength,
  1112. in_ind + 1);
  1113. while (in_ind != out_ind) {
  1114. in_ind = find_next_bit(indio_dev->active_scan_mask,
  1115. indio_dev->masklength,
  1116. in_ind + 1);
  1117. length = iio_storage_bytes_for_si(indio_dev, in_ind);
  1118. /* Make sure we are aligned */
  1119. in_loc = roundup(in_loc, length) + length;
  1120. }
  1121. length = iio_storage_bytes_for_si(indio_dev, in_ind);
  1122. out_loc = roundup(out_loc, length);
  1123. in_loc = roundup(in_loc, length);
  1124. ret = iio_buffer_add_demux(buffer, &p, in_loc, out_loc, length);
  1125. if (ret)
  1126. goto error_clear_mux_table;
  1127. out_loc += length;
  1128. in_loc += length;
  1129. }
  1130. /* Relies on scan_timestamp being last */
  1131. if (buffer->scan_timestamp) {
  1132. length = iio_storage_bytes_for_timestamp(indio_dev);
  1133. out_loc = roundup(out_loc, length);
  1134. in_loc = roundup(in_loc, length);
  1135. ret = iio_buffer_add_demux(buffer, &p, in_loc, out_loc, length);
  1136. if (ret)
  1137. goto error_clear_mux_table;
  1138. out_loc += length;
  1139. in_loc += length;
  1140. }
  1141. buffer->demux_bounce = kzalloc(out_loc, GFP_KERNEL);
  1142. if (buffer->demux_bounce == NULL) {
  1143. ret = -ENOMEM;
  1144. goto error_clear_mux_table;
  1145. }
  1146. return 0;
  1147. error_clear_mux_table:
  1148. iio_buffer_demux_free(buffer);
  1149. return ret;
  1150. }
  1151. int iio_update_demux(struct iio_dev *indio_dev)
  1152. {
  1153. struct iio_buffer *buffer;
  1154. int ret;
  1155. list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list) {
  1156. ret = iio_buffer_update_demux(indio_dev, buffer);
  1157. if (ret < 0)
  1158. goto error_clear_mux_table;
  1159. }
  1160. return 0;
  1161. error_clear_mux_table:
  1162. list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list)
  1163. iio_buffer_demux_free(buffer);
  1164. return ret;
  1165. }
  1166. EXPORT_SYMBOL_GPL(iio_update_demux);
  1167. /**
  1168. * iio_buffer_release() - Free a buffer's resources
  1169. * @ref: Pointer to the kref embedded in the iio_buffer struct
  1170. *
  1171. * This function is called when the last reference to the buffer has been
  1172. * dropped. It will typically free all resources allocated by the buffer. Do not
  1173. * call this function manually, always use iio_buffer_put() when done using a
  1174. * buffer.
  1175. */
  1176. static void iio_buffer_release(struct kref *ref)
  1177. {
  1178. struct iio_buffer *buffer = container_of(ref, struct iio_buffer, ref);
  1179. buffer->access->release(buffer);
  1180. }
  1181. /**
  1182. * iio_buffer_get() - Grab a reference to the buffer
  1183. * @buffer: The buffer to grab a reference for, may be NULL
  1184. *
  1185. * Returns the pointer to the buffer that was passed into the function.
  1186. */
  1187. struct iio_buffer *iio_buffer_get(struct iio_buffer *buffer)
  1188. {
  1189. if (buffer)
  1190. kref_get(&buffer->ref);
  1191. return buffer;
  1192. }
  1193. EXPORT_SYMBOL_GPL(iio_buffer_get);
  1194. /**
  1195. * iio_buffer_put() - Release the reference to the buffer
  1196. * @buffer: The buffer to release the reference for, may be NULL
  1197. */
  1198. void iio_buffer_put(struct iio_buffer *buffer)
  1199. {
  1200. if (buffer)
  1201. kref_put(&buffer->ref, iio_buffer_release);
  1202. }
  1203. EXPORT_SYMBOL_GPL(iio_buffer_put);