industrialio-buffer.c 36 KB

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