industrialio-buffer.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482
  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: (EPOLLIN | EPOLLRDNORM) if data is available for reading
  144. * or 0 for other cases
  145. */
  146. __poll_t 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 || rb == NULL)
  152. return 0;
  153. poll_wait(filp, &rb->pollq, wait);
  154. if (iio_buffer_ready(indio_dev, rb, rb->watermark, 0))
  155. return EPOLLIN | EPOLLRDNORM;
  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 = kcalloc(BITS_TO_LONGS(indio_dev->masklength),
  281. sizeof(*trialmask), GFP_KERNEL);
  282. if (trialmask == NULL)
  283. return -ENOMEM;
  284. if (!indio_dev->masklength) {
  285. WARN(1, "Trying to set scanmask prior to registering buffer\n");
  286. goto err_invalid_mask;
  287. }
  288. bitmap_copy(trialmask, buffer->scan_mask, indio_dev->masklength);
  289. set_bit(bit, trialmask);
  290. if (!iio_validate_scan_mask(indio_dev, trialmask))
  291. goto err_invalid_mask;
  292. if (indio_dev->available_scan_masks) {
  293. mask = iio_scan_mask_match(indio_dev->available_scan_masks,
  294. indio_dev->masklength,
  295. trialmask, false);
  296. if (!mask)
  297. goto err_invalid_mask;
  298. }
  299. bitmap_copy(buffer->scan_mask, trialmask, indio_dev->masklength);
  300. kfree(trialmask);
  301. return 0;
  302. err_invalid_mask:
  303. kfree(trialmask);
  304. return -EINVAL;
  305. }
  306. static int iio_scan_mask_clear(struct iio_buffer *buffer, int bit)
  307. {
  308. clear_bit(bit, buffer->scan_mask);
  309. return 0;
  310. }
  311. static int iio_scan_mask_query(struct iio_dev *indio_dev,
  312. struct iio_buffer *buffer, int bit)
  313. {
  314. if (bit > indio_dev->masklength)
  315. return -EINVAL;
  316. if (!buffer->scan_mask)
  317. return 0;
  318. /* Ensure return value is 0 or 1. */
  319. return !!test_bit(bit, buffer->scan_mask);
  320. };
  321. static ssize_t iio_scan_el_store(struct device *dev,
  322. struct device_attribute *attr,
  323. const char *buf,
  324. size_t len)
  325. {
  326. int ret;
  327. bool state;
  328. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  329. struct iio_buffer *buffer = indio_dev->buffer;
  330. struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
  331. ret = strtobool(buf, &state);
  332. if (ret < 0)
  333. return ret;
  334. mutex_lock(&indio_dev->mlock);
  335. if (iio_buffer_is_active(indio_dev->buffer)) {
  336. ret = -EBUSY;
  337. goto error_ret;
  338. }
  339. ret = iio_scan_mask_query(indio_dev, buffer, this_attr->address);
  340. if (ret < 0)
  341. goto error_ret;
  342. if (!state && ret) {
  343. ret = iio_scan_mask_clear(buffer, this_attr->address);
  344. if (ret)
  345. goto error_ret;
  346. } else if (state && !ret) {
  347. ret = iio_scan_mask_set(indio_dev, buffer, this_attr->address);
  348. if (ret)
  349. goto error_ret;
  350. }
  351. error_ret:
  352. mutex_unlock(&indio_dev->mlock);
  353. return ret < 0 ? ret : len;
  354. }
  355. static ssize_t iio_scan_el_ts_show(struct device *dev,
  356. struct device_attribute *attr,
  357. char *buf)
  358. {
  359. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  360. return sprintf(buf, "%d\n", indio_dev->buffer->scan_timestamp);
  361. }
  362. static ssize_t iio_scan_el_ts_store(struct device *dev,
  363. struct device_attribute *attr,
  364. const char *buf,
  365. size_t len)
  366. {
  367. int ret;
  368. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  369. bool state;
  370. ret = strtobool(buf, &state);
  371. if (ret < 0)
  372. return ret;
  373. mutex_lock(&indio_dev->mlock);
  374. if (iio_buffer_is_active(indio_dev->buffer)) {
  375. ret = -EBUSY;
  376. goto error_ret;
  377. }
  378. indio_dev->buffer->scan_timestamp = state;
  379. error_ret:
  380. mutex_unlock(&indio_dev->mlock);
  381. return ret ? ret : len;
  382. }
  383. static int iio_buffer_add_channel_sysfs(struct iio_dev *indio_dev,
  384. const struct iio_chan_spec *chan)
  385. {
  386. int ret, attrcount = 0;
  387. struct iio_buffer *buffer = indio_dev->buffer;
  388. ret = __iio_add_chan_devattr("index",
  389. chan,
  390. &iio_show_scan_index,
  391. NULL,
  392. 0,
  393. IIO_SEPARATE,
  394. &indio_dev->dev,
  395. &buffer->scan_el_dev_attr_list);
  396. if (ret)
  397. return ret;
  398. attrcount++;
  399. ret = __iio_add_chan_devattr("type",
  400. chan,
  401. &iio_show_fixed_type,
  402. NULL,
  403. 0,
  404. 0,
  405. &indio_dev->dev,
  406. &buffer->scan_el_dev_attr_list);
  407. if (ret)
  408. return ret;
  409. attrcount++;
  410. if (chan->type != IIO_TIMESTAMP)
  411. ret = __iio_add_chan_devattr("en",
  412. chan,
  413. &iio_scan_el_show,
  414. &iio_scan_el_store,
  415. chan->scan_index,
  416. 0,
  417. &indio_dev->dev,
  418. &buffer->scan_el_dev_attr_list);
  419. else
  420. ret = __iio_add_chan_devattr("en",
  421. chan,
  422. &iio_scan_el_ts_show,
  423. &iio_scan_el_ts_store,
  424. chan->scan_index,
  425. 0,
  426. &indio_dev->dev,
  427. &buffer->scan_el_dev_attr_list);
  428. if (ret)
  429. return ret;
  430. attrcount++;
  431. ret = attrcount;
  432. return ret;
  433. }
  434. static ssize_t iio_buffer_read_length(struct device *dev,
  435. struct device_attribute *attr,
  436. char *buf)
  437. {
  438. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  439. struct iio_buffer *buffer = indio_dev->buffer;
  440. return sprintf(buf, "%d\n", buffer->length);
  441. }
  442. static ssize_t iio_buffer_write_length(struct device *dev,
  443. struct device_attribute *attr,
  444. const char *buf, size_t len)
  445. {
  446. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  447. struct iio_buffer *buffer = indio_dev->buffer;
  448. unsigned int val;
  449. int ret;
  450. ret = kstrtouint(buf, 10, &val);
  451. if (ret)
  452. return ret;
  453. if (val == buffer->length)
  454. return len;
  455. mutex_lock(&indio_dev->mlock);
  456. if (iio_buffer_is_active(indio_dev->buffer)) {
  457. ret = -EBUSY;
  458. } else {
  459. buffer->access->set_length(buffer, val);
  460. ret = 0;
  461. }
  462. if (ret)
  463. goto out;
  464. if (buffer->length && buffer->length < buffer->watermark)
  465. buffer->watermark = buffer->length;
  466. out:
  467. mutex_unlock(&indio_dev->mlock);
  468. return ret ? ret : len;
  469. }
  470. static ssize_t iio_buffer_show_enable(struct device *dev,
  471. struct device_attribute *attr,
  472. char *buf)
  473. {
  474. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  475. return sprintf(buf, "%d\n", iio_buffer_is_active(indio_dev->buffer));
  476. }
  477. static unsigned int iio_storage_bytes_for_si(struct iio_dev *indio_dev,
  478. unsigned int scan_index)
  479. {
  480. const struct iio_chan_spec *ch;
  481. unsigned int bytes;
  482. ch = iio_find_channel_from_si(indio_dev, scan_index);
  483. bytes = ch->scan_type.storagebits / 8;
  484. if (ch->scan_type.repeat > 1)
  485. bytes *= ch->scan_type.repeat;
  486. return bytes;
  487. }
  488. static unsigned int iio_storage_bytes_for_timestamp(struct iio_dev *indio_dev)
  489. {
  490. return iio_storage_bytes_for_si(indio_dev,
  491. indio_dev->scan_index_timestamp);
  492. }
  493. static int iio_compute_scan_bytes(struct iio_dev *indio_dev,
  494. const unsigned long *mask, bool timestamp)
  495. {
  496. unsigned bytes = 0;
  497. int length, i, largest = 0;
  498. /* How much space will the demuxed element take? */
  499. for_each_set_bit(i, mask,
  500. indio_dev->masklength) {
  501. length = iio_storage_bytes_for_si(indio_dev, i);
  502. bytes = ALIGN(bytes, length);
  503. bytes += length;
  504. largest = max(largest, length);
  505. }
  506. if (timestamp) {
  507. length = iio_storage_bytes_for_timestamp(indio_dev);
  508. bytes = ALIGN(bytes, length);
  509. bytes += length;
  510. largest = max(largest, length);
  511. }
  512. bytes = ALIGN(bytes, largest);
  513. return bytes;
  514. }
  515. static void iio_buffer_activate(struct iio_dev *indio_dev,
  516. struct iio_buffer *buffer)
  517. {
  518. iio_buffer_get(buffer);
  519. list_add(&buffer->buffer_list, &indio_dev->buffer_list);
  520. }
  521. static void iio_buffer_deactivate(struct iio_buffer *buffer)
  522. {
  523. list_del_init(&buffer->buffer_list);
  524. wake_up_interruptible(&buffer->pollq);
  525. iio_buffer_put(buffer);
  526. }
  527. static void iio_buffer_deactivate_all(struct iio_dev *indio_dev)
  528. {
  529. struct iio_buffer *buffer, *_buffer;
  530. list_for_each_entry_safe(buffer, _buffer,
  531. &indio_dev->buffer_list, buffer_list)
  532. iio_buffer_deactivate(buffer);
  533. }
  534. static int iio_buffer_enable(struct iio_buffer *buffer,
  535. struct iio_dev *indio_dev)
  536. {
  537. if (!buffer->access->enable)
  538. return 0;
  539. return buffer->access->enable(buffer, indio_dev);
  540. }
  541. static int iio_buffer_disable(struct iio_buffer *buffer,
  542. struct iio_dev *indio_dev)
  543. {
  544. if (!buffer->access->disable)
  545. return 0;
  546. return buffer->access->disable(buffer, indio_dev);
  547. }
  548. static void iio_buffer_update_bytes_per_datum(struct iio_dev *indio_dev,
  549. struct iio_buffer *buffer)
  550. {
  551. unsigned int bytes;
  552. if (!buffer->access->set_bytes_per_datum)
  553. return;
  554. bytes = iio_compute_scan_bytes(indio_dev, buffer->scan_mask,
  555. buffer->scan_timestamp);
  556. buffer->access->set_bytes_per_datum(buffer, bytes);
  557. }
  558. static int iio_buffer_request_update(struct iio_dev *indio_dev,
  559. struct iio_buffer *buffer)
  560. {
  561. int ret;
  562. iio_buffer_update_bytes_per_datum(indio_dev, buffer);
  563. if (buffer->access->request_update) {
  564. ret = buffer->access->request_update(buffer);
  565. if (ret) {
  566. dev_dbg(&indio_dev->dev,
  567. "Buffer not started: buffer parameter update failed (%d)\n",
  568. ret);
  569. return ret;
  570. }
  571. }
  572. return 0;
  573. }
  574. static void iio_free_scan_mask(struct iio_dev *indio_dev,
  575. const unsigned long *mask)
  576. {
  577. /* If the mask is dynamically allocated free it, otherwise do nothing */
  578. if (!indio_dev->available_scan_masks)
  579. kfree(mask);
  580. }
  581. struct iio_device_config {
  582. unsigned int mode;
  583. unsigned int watermark;
  584. const unsigned long *scan_mask;
  585. unsigned int scan_bytes;
  586. bool scan_timestamp;
  587. };
  588. static int iio_verify_update(struct iio_dev *indio_dev,
  589. struct iio_buffer *insert_buffer, struct iio_buffer *remove_buffer,
  590. struct iio_device_config *config)
  591. {
  592. unsigned long *compound_mask;
  593. const unsigned long *scan_mask;
  594. bool strict_scanmask = false;
  595. struct iio_buffer *buffer;
  596. bool scan_timestamp;
  597. unsigned int modes;
  598. memset(config, 0, sizeof(*config));
  599. config->watermark = ~0;
  600. /*
  601. * If there is just one buffer and we are removing it there is nothing
  602. * to verify.
  603. */
  604. if (remove_buffer && !insert_buffer &&
  605. list_is_singular(&indio_dev->buffer_list))
  606. return 0;
  607. modes = indio_dev->modes;
  608. list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list) {
  609. if (buffer == remove_buffer)
  610. continue;
  611. modes &= buffer->access->modes;
  612. config->watermark = min(config->watermark, buffer->watermark);
  613. }
  614. if (insert_buffer) {
  615. modes &= insert_buffer->access->modes;
  616. config->watermark = min(config->watermark,
  617. insert_buffer->watermark);
  618. }
  619. /* Definitely possible for devices to support both of these. */
  620. if ((modes & INDIO_BUFFER_TRIGGERED) && indio_dev->trig) {
  621. config->mode = INDIO_BUFFER_TRIGGERED;
  622. } else if (modes & INDIO_BUFFER_HARDWARE) {
  623. /*
  624. * Keep things simple for now and only allow a single buffer to
  625. * be connected in hardware mode.
  626. */
  627. if (insert_buffer && !list_empty(&indio_dev->buffer_list))
  628. return -EINVAL;
  629. config->mode = INDIO_BUFFER_HARDWARE;
  630. strict_scanmask = true;
  631. } else if (modes & INDIO_BUFFER_SOFTWARE) {
  632. config->mode = INDIO_BUFFER_SOFTWARE;
  633. } else {
  634. /* Can only occur on first buffer */
  635. if (indio_dev->modes & INDIO_BUFFER_TRIGGERED)
  636. dev_dbg(&indio_dev->dev, "Buffer not started: no trigger\n");
  637. return -EINVAL;
  638. }
  639. /* What scan mask do we actually have? */
  640. compound_mask = kcalloc(BITS_TO_LONGS(indio_dev->masklength),
  641. sizeof(long), GFP_KERNEL);
  642. if (compound_mask == NULL)
  643. return -ENOMEM;
  644. scan_timestamp = false;
  645. list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list) {
  646. if (buffer == remove_buffer)
  647. continue;
  648. bitmap_or(compound_mask, compound_mask, buffer->scan_mask,
  649. indio_dev->masklength);
  650. scan_timestamp |= buffer->scan_timestamp;
  651. }
  652. if (insert_buffer) {
  653. bitmap_or(compound_mask, compound_mask,
  654. insert_buffer->scan_mask, indio_dev->masklength);
  655. scan_timestamp |= insert_buffer->scan_timestamp;
  656. }
  657. if (indio_dev->available_scan_masks) {
  658. scan_mask = iio_scan_mask_match(indio_dev->available_scan_masks,
  659. indio_dev->masklength,
  660. compound_mask,
  661. strict_scanmask);
  662. kfree(compound_mask);
  663. if (scan_mask == NULL)
  664. return -EINVAL;
  665. } else {
  666. scan_mask = compound_mask;
  667. }
  668. config->scan_bytes = iio_compute_scan_bytes(indio_dev,
  669. scan_mask, scan_timestamp);
  670. config->scan_mask = scan_mask;
  671. config->scan_timestamp = scan_timestamp;
  672. return 0;
  673. }
  674. /**
  675. * struct iio_demux_table - table describing demux memcpy ops
  676. * @from: index to copy from
  677. * @to: index to copy to
  678. * @length: how many bytes to copy
  679. * @l: list head used for management
  680. */
  681. struct iio_demux_table {
  682. unsigned from;
  683. unsigned to;
  684. unsigned length;
  685. struct list_head l;
  686. };
  687. static void iio_buffer_demux_free(struct iio_buffer *buffer)
  688. {
  689. struct iio_demux_table *p, *q;
  690. list_for_each_entry_safe(p, q, &buffer->demux_list, l) {
  691. list_del(&p->l);
  692. kfree(p);
  693. }
  694. }
  695. static int iio_buffer_add_demux(struct iio_buffer *buffer,
  696. struct iio_demux_table **p, unsigned int in_loc, unsigned int out_loc,
  697. unsigned int length)
  698. {
  699. if (*p && (*p)->from + (*p)->length == in_loc &&
  700. (*p)->to + (*p)->length == out_loc) {
  701. (*p)->length += length;
  702. } else {
  703. *p = kmalloc(sizeof(**p), GFP_KERNEL);
  704. if (*p == NULL)
  705. return -ENOMEM;
  706. (*p)->from = in_loc;
  707. (*p)->to = out_loc;
  708. (*p)->length = length;
  709. list_add_tail(&(*p)->l, &buffer->demux_list);
  710. }
  711. return 0;
  712. }
  713. static int iio_buffer_update_demux(struct iio_dev *indio_dev,
  714. struct iio_buffer *buffer)
  715. {
  716. int ret, in_ind = -1, out_ind, length;
  717. unsigned in_loc = 0, out_loc = 0;
  718. struct iio_demux_table *p = NULL;
  719. /* Clear out any old demux */
  720. iio_buffer_demux_free(buffer);
  721. kfree(buffer->demux_bounce);
  722. buffer->demux_bounce = NULL;
  723. /* First work out which scan mode we will actually have */
  724. if (bitmap_equal(indio_dev->active_scan_mask,
  725. buffer->scan_mask,
  726. indio_dev->masklength))
  727. return 0;
  728. /* Now we have the two masks, work from least sig and build up sizes */
  729. for_each_set_bit(out_ind,
  730. buffer->scan_mask,
  731. indio_dev->masklength) {
  732. in_ind = find_next_bit(indio_dev->active_scan_mask,
  733. indio_dev->masklength,
  734. in_ind + 1);
  735. while (in_ind != out_ind) {
  736. length = iio_storage_bytes_for_si(indio_dev, in_ind);
  737. /* Make sure we are aligned */
  738. in_loc = roundup(in_loc, length) + length;
  739. in_ind = find_next_bit(indio_dev->active_scan_mask,
  740. indio_dev->masklength,
  741. in_ind + 1);
  742. }
  743. length = iio_storage_bytes_for_si(indio_dev, in_ind);
  744. out_loc = roundup(out_loc, length);
  745. in_loc = roundup(in_loc, length);
  746. ret = iio_buffer_add_demux(buffer, &p, in_loc, out_loc, length);
  747. if (ret)
  748. goto error_clear_mux_table;
  749. out_loc += length;
  750. in_loc += length;
  751. }
  752. /* Relies on scan_timestamp being last */
  753. if (buffer->scan_timestamp) {
  754. length = iio_storage_bytes_for_timestamp(indio_dev);
  755. out_loc = roundup(out_loc, length);
  756. in_loc = roundup(in_loc, length);
  757. ret = iio_buffer_add_demux(buffer, &p, in_loc, out_loc, length);
  758. if (ret)
  759. goto error_clear_mux_table;
  760. out_loc += length;
  761. in_loc += length;
  762. }
  763. buffer->demux_bounce = kzalloc(out_loc, GFP_KERNEL);
  764. if (buffer->demux_bounce == NULL) {
  765. ret = -ENOMEM;
  766. goto error_clear_mux_table;
  767. }
  768. return 0;
  769. error_clear_mux_table:
  770. iio_buffer_demux_free(buffer);
  771. return ret;
  772. }
  773. static int iio_update_demux(struct iio_dev *indio_dev)
  774. {
  775. struct iio_buffer *buffer;
  776. int ret;
  777. list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list) {
  778. ret = iio_buffer_update_demux(indio_dev, buffer);
  779. if (ret < 0)
  780. goto error_clear_mux_table;
  781. }
  782. return 0;
  783. error_clear_mux_table:
  784. list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list)
  785. iio_buffer_demux_free(buffer);
  786. return ret;
  787. }
  788. static int iio_enable_buffers(struct iio_dev *indio_dev,
  789. struct iio_device_config *config)
  790. {
  791. struct iio_buffer *buffer;
  792. int ret;
  793. indio_dev->active_scan_mask = config->scan_mask;
  794. indio_dev->scan_timestamp = config->scan_timestamp;
  795. indio_dev->scan_bytes = config->scan_bytes;
  796. iio_update_demux(indio_dev);
  797. /* Wind up again */
  798. if (indio_dev->setup_ops->preenable) {
  799. ret = indio_dev->setup_ops->preenable(indio_dev);
  800. if (ret) {
  801. dev_dbg(&indio_dev->dev,
  802. "Buffer not started: buffer preenable failed (%d)\n", ret);
  803. goto err_undo_config;
  804. }
  805. }
  806. if (indio_dev->info->update_scan_mode) {
  807. ret = indio_dev->info
  808. ->update_scan_mode(indio_dev,
  809. indio_dev->active_scan_mask);
  810. if (ret < 0) {
  811. dev_dbg(&indio_dev->dev,
  812. "Buffer not started: update scan mode failed (%d)\n",
  813. ret);
  814. goto err_run_postdisable;
  815. }
  816. }
  817. if (indio_dev->info->hwfifo_set_watermark)
  818. indio_dev->info->hwfifo_set_watermark(indio_dev,
  819. config->watermark);
  820. list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list) {
  821. ret = iio_buffer_enable(buffer, indio_dev);
  822. if (ret)
  823. goto err_disable_buffers;
  824. }
  825. indio_dev->currentmode = config->mode;
  826. if (indio_dev->setup_ops->postenable) {
  827. ret = indio_dev->setup_ops->postenable(indio_dev);
  828. if (ret) {
  829. dev_dbg(&indio_dev->dev,
  830. "Buffer not started: postenable failed (%d)\n", ret);
  831. goto err_disable_buffers;
  832. }
  833. }
  834. return 0;
  835. err_disable_buffers:
  836. list_for_each_entry_continue_reverse(buffer, &indio_dev->buffer_list,
  837. buffer_list)
  838. iio_buffer_disable(buffer, indio_dev);
  839. err_run_postdisable:
  840. indio_dev->currentmode = INDIO_DIRECT_MODE;
  841. if (indio_dev->setup_ops->postdisable)
  842. indio_dev->setup_ops->postdisable(indio_dev);
  843. err_undo_config:
  844. indio_dev->active_scan_mask = NULL;
  845. return ret;
  846. }
  847. static int iio_disable_buffers(struct iio_dev *indio_dev)
  848. {
  849. struct iio_buffer *buffer;
  850. int ret = 0;
  851. int ret2;
  852. /* Wind down existing buffers - iff there are any */
  853. if (list_empty(&indio_dev->buffer_list))
  854. return 0;
  855. /*
  856. * If things go wrong at some step in disable we still need to continue
  857. * to perform the other steps, otherwise we leave the device in a
  858. * inconsistent state. We return the error code for the first error we
  859. * encountered.
  860. */
  861. if (indio_dev->setup_ops->predisable) {
  862. ret2 = indio_dev->setup_ops->predisable(indio_dev);
  863. if (ret2 && !ret)
  864. ret = ret2;
  865. }
  866. list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list) {
  867. ret2 = iio_buffer_disable(buffer, indio_dev);
  868. if (ret2 && !ret)
  869. ret = ret2;
  870. }
  871. indio_dev->currentmode = INDIO_DIRECT_MODE;
  872. if (indio_dev->setup_ops->postdisable) {
  873. ret2 = indio_dev->setup_ops->postdisable(indio_dev);
  874. if (ret2 && !ret)
  875. ret = ret2;
  876. }
  877. iio_free_scan_mask(indio_dev, indio_dev->active_scan_mask);
  878. indio_dev->active_scan_mask = NULL;
  879. return ret;
  880. }
  881. static int __iio_update_buffers(struct iio_dev *indio_dev,
  882. struct iio_buffer *insert_buffer,
  883. struct iio_buffer *remove_buffer)
  884. {
  885. struct iio_device_config new_config;
  886. int ret;
  887. ret = iio_verify_update(indio_dev, insert_buffer, remove_buffer,
  888. &new_config);
  889. if (ret)
  890. return ret;
  891. if (insert_buffer) {
  892. ret = iio_buffer_request_update(indio_dev, insert_buffer);
  893. if (ret)
  894. goto err_free_config;
  895. }
  896. ret = iio_disable_buffers(indio_dev);
  897. if (ret)
  898. goto err_deactivate_all;
  899. if (remove_buffer)
  900. iio_buffer_deactivate(remove_buffer);
  901. if (insert_buffer)
  902. iio_buffer_activate(indio_dev, insert_buffer);
  903. /* If no buffers in list, we are done */
  904. if (list_empty(&indio_dev->buffer_list))
  905. return 0;
  906. ret = iio_enable_buffers(indio_dev, &new_config);
  907. if (ret)
  908. goto err_deactivate_all;
  909. return 0;
  910. err_deactivate_all:
  911. /*
  912. * We've already verified that the config is valid earlier. If things go
  913. * wrong in either enable or disable the most likely reason is an IO
  914. * error from the device. In this case there is no good recovery
  915. * strategy. Just make sure to disable everything and leave the device
  916. * in a sane state. With a bit of luck the device might come back to
  917. * life again later and userspace can try again.
  918. */
  919. iio_buffer_deactivate_all(indio_dev);
  920. err_free_config:
  921. iio_free_scan_mask(indio_dev, new_config.scan_mask);
  922. return ret;
  923. }
  924. int iio_update_buffers(struct iio_dev *indio_dev,
  925. struct iio_buffer *insert_buffer,
  926. struct iio_buffer *remove_buffer)
  927. {
  928. int ret;
  929. if (insert_buffer == remove_buffer)
  930. return 0;
  931. mutex_lock(&indio_dev->info_exist_lock);
  932. mutex_lock(&indio_dev->mlock);
  933. if (insert_buffer && iio_buffer_is_active(insert_buffer))
  934. insert_buffer = NULL;
  935. if (remove_buffer && !iio_buffer_is_active(remove_buffer))
  936. remove_buffer = NULL;
  937. if (!insert_buffer && !remove_buffer) {
  938. ret = 0;
  939. goto out_unlock;
  940. }
  941. if (indio_dev->info == NULL) {
  942. ret = -ENODEV;
  943. goto out_unlock;
  944. }
  945. ret = __iio_update_buffers(indio_dev, insert_buffer, remove_buffer);
  946. out_unlock:
  947. mutex_unlock(&indio_dev->mlock);
  948. mutex_unlock(&indio_dev->info_exist_lock);
  949. return ret;
  950. }
  951. EXPORT_SYMBOL_GPL(iio_update_buffers);
  952. void iio_disable_all_buffers(struct iio_dev *indio_dev)
  953. {
  954. iio_disable_buffers(indio_dev);
  955. iio_buffer_deactivate_all(indio_dev);
  956. }
  957. static ssize_t iio_buffer_store_enable(struct device *dev,
  958. struct device_attribute *attr,
  959. const char *buf,
  960. size_t len)
  961. {
  962. int ret;
  963. bool requested_state;
  964. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  965. bool inlist;
  966. ret = strtobool(buf, &requested_state);
  967. if (ret < 0)
  968. return ret;
  969. mutex_lock(&indio_dev->mlock);
  970. /* Find out if it is in the list */
  971. inlist = iio_buffer_is_active(indio_dev->buffer);
  972. /* Already in desired state */
  973. if (inlist == requested_state)
  974. goto done;
  975. if (requested_state)
  976. ret = __iio_update_buffers(indio_dev,
  977. indio_dev->buffer, NULL);
  978. else
  979. ret = __iio_update_buffers(indio_dev,
  980. NULL, indio_dev->buffer);
  981. done:
  982. mutex_unlock(&indio_dev->mlock);
  983. return (ret < 0) ? ret : len;
  984. }
  985. static const char * const iio_scan_elements_group_name = "scan_elements";
  986. static ssize_t iio_buffer_show_watermark(struct device *dev,
  987. struct device_attribute *attr,
  988. char *buf)
  989. {
  990. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  991. struct iio_buffer *buffer = indio_dev->buffer;
  992. return sprintf(buf, "%u\n", buffer->watermark);
  993. }
  994. static ssize_t iio_buffer_store_watermark(struct device *dev,
  995. struct device_attribute *attr,
  996. const char *buf,
  997. size_t len)
  998. {
  999. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  1000. struct iio_buffer *buffer = indio_dev->buffer;
  1001. unsigned int val;
  1002. int ret;
  1003. ret = kstrtouint(buf, 10, &val);
  1004. if (ret)
  1005. return ret;
  1006. if (!val)
  1007. return -EINVAL;
  1008. mutex_lock(&indio_dev->mlock);
  1009. if (val > buffer->length) {
  1010. ret = -EINVAL;
  1011. goto out;
  1012. }
  1013. if (iio_buffer_is_active(indio_dev->buffer)) {
  1014. ret = -EBUSY;
  1015. goto out;
  1016. }
  1017. buffer->watermark = val;
  1018. out:
  1019. mutex_unlock(&indio_dev->mlock);
  1020. return ret ? ret : len;
  1021. }
  1022. static ssize_t iio_dma_show_data_available(struct device *dev,
  1023. struct device_attribute *attr,
  1024. char *buf)
  1025. {
  1026. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  1027. size_t bytes;
  1028. bytes = iio_buffer_data_available(indio_dev->buffer);
  1029. return sprintf(buf, "%zu\n", bytes);
  1030. }
  1031. static DEVICE_ATTR(length, S_IRUGO | S_IWUSR, iio_buffer_read_length,
  1032. iio_buffer_write_length);
  1033. static struct device_attribute dev_attr_length_ro = __ATTR(length,
  1034. S_IRUGO, iio_buffer_read_length, NULL);
  1035. static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR,
  1036. iio_buffer_show_enable, iio_buffer_store_enable);
  1037. static DEVICE_ATTR(watermark, S_IRUGO | S_IWUSR,
  1038. iio_buffer_show_watermark, iio_buffer_store_watermark);
  1039. static struct device_attribute dev_attr_watermark_ro = __ATTR(watermark,
  1040. S_IRUGO, iio_buffer_show_watermark, NULL);
  1041. static DEVICE_ATTR(data_available, S_IRUGO,
  1042. iio_dma_show_data_available, NULL);
  1043. static struct attribute *iio_buffer_attrs[] = {
  1044. &dev_attr_length.attr,
  1045. &dev_attr_enable.attr,
  1046. &dev_attr_watermark.attr,
  1047. &dev_attr_data_available.attr,
  1048. };
  1049. int iio_buffer_alloc_sysfs_and_mask(struct iio_dev *indio_dev)
  1050. {
  1051. struct iio_dev_attr *p;
  1052. struct attribute **attr;
  1053. struct iio_buffer *buffer = indio_dev->buffer;
  1054. int ret, i, attrn, attrcount, attrcount_orig = 0;
  1055. const struct iio_chan_spec *channels;
  1056. channels = indio_dev->channels;
  1057. if (channels) {
  1058. int ml = indio_dev->masklength;
  1059. for (i = 0; i < indio_dev->num_channels; i++)
  1060. ml = max(ml, channels[i].scan_index + 1);
  1061. indio_dev->masklength = ml;
  1062. }
  1063. if (!buffer)
  1064. return 0;
  1065. attrcount = 0;
  1066. if (buffer->attrs) {
  1067. while (buffer->attrs[attrcount] != NULL)
  1068. attrcount++;
  1069. }
  1070. attr = kcalloc(attrcount + ARRAY_SIZE(iio_buffer_attrs) + 1,
  1071. sizeof(struct attribute *), GFP_KERNEL);
  1072. if (!attr)
  1073. return -ENOMEM;
  1074. memcpy(attr, iio_buffer_attrs, sizeof(iio_buffer_attrs));
  1075. if (!buffer->access->set_length)
  1076. attr[0] = &dev_attr_length_ro.attr;
  1077. if (buffer->access->flags & INDIO_BUFFER_FLAG_FIXED_WATERMARK)
  1078. attr[2] = &dev_attr_watermark_ro.attr;
  1079. if (buffer->attrs)
  1080. memcpy(&attr[ARRAY_SIZE(iio_buffer_attrs)], buffer->attrs,
  1081. sizeof(struct attribute *) * attrcount);
  1082. attr[attrcount + ARRAY_SIZE(iio_buffer_attrs)] = NULL;
  1083. buffer->buffer_group.name = "buffer";
  1084. buffer->buffer_group.attrs = attr;
  1085. indio_dev->groups[indio_dev->groupcounter++] = &buffer->buffer_group;
  1086. if (buffer->scan_el_attrs != NULL) {
  1087. attr = buffer->scan_el_attrs->attrs;
  1088. while (*attr++ != NULL)
  1089. attrcount_orig++;
  1090. }
  1091. attrcount = attrcount_orig;
  1092. INIT_LIST_HEAD(&buffer->scan_el_dev_attr_list);
  1093. channels = indio_dev->channels;
  1094. if (channels) {
  1095. /* new magic */
  1096. for (i = 0; i < indio_dev->num_channels; i++) {
  1097. if (channels[i].scan_index < 0)
  1098. continue;
  1099. ret = iio_buffer_add_channel_sysfs(indio_dev,
  1100. &channels[i]);
  1101. if (ret < 0)
  1102. goto error_cleanup_dynamic;
  1103. attrcount += ret;
  1104. if (channels[i].type == IIO_TIMESTAMP)
  1105. indio_dev->scan_index_timestamp =
  1106. channels[i].scan_index;
  1107. }
  1108. if (indio_dev->masklength && buffer->scan_mask == NULL) {
  1109. buffer->scan_mask = kcalloc(BITS_TO_LONGS(indio_dev->masklength),
  1110. sizeof(*buffer->scan_mask),
  1111. GFP_KERNEL);
  1112. if (buffer->scan_mask == NULL) {
  1113. ret = -ENOMEM;
  1114. goto error_cleanup_dynamic;
  1115. }
  1116. }
  1117. }
  1118. buffer->scan_el_group.name = iio_scan_elements_group_name;
  1119. buffer->scan_el_group.attrs = kcalloc(attrcount + 1,
  1120. sizeof(buffer->scan_el_group.attrs[0]),
  1121. GFP_KERNEL);
  1122. if (buffer->scan_el_group.attrs == NULL) {
  1123. ret = -ENOMEM;
  1124. goto error_free_scan_mask;
  1125. }
  1126. if (buffer->scan_el_attrs)
  1127. memcpy(buffer->scan_el_group.attrs, buffer->scan_el_attrs,
  1128. sizeof(buffer->scan_el_group.attrs[0])*attrcount_orig);
  1129. attrn = attrcount_orig;
  1130. list_for_each_entry(p, &buffer->scan_el_dev_attr_list, l)
  1131. buffer->scan_el_group.attrs[attrn++] = &p->dev_attr.attr;
  1132. indio_dev->groups[indio_dev->groupcounter++] = &buffer->scan_el_group;
  1133. return 0;
  1134. error_free_scan_mask:
  1135. kfree(buffer->scan_mask);
  1136. error_cleanup_dynamic:
  1137. iio_free_chan_devattr_list(&buffer->scan_el_dev_attr_list);
  1138. kfree(indio_dev->buffer->buffer_group.attrs);
  1139. return ret;
  1140. }
  1141. void iio_buffer_free_sysfs_and_mask(struct iio_dev *indio_dev)
  1142. {
  1143. if (!indio_dev->buffer)
  1144. return;
  1145. kfree(indio_dev->buffer->scan_mask);
  1146. kfree(indio_dev->buffer->buffer_group.attrs);
  1147. kfree(indio_dev->buffer->scan_el_group.attrs);
  1148. iio_free_chan_devattr_list(&indio_dev->buffer->scan_el_dev_attr_list);
  1149. }
  1150. /**
  1151. * iio_validate_scan_mask_onehot() - Validates that exactly one channel is selected
  1152. * @indio_dev: the iio device
  1153. * @mask: scan mask to be checked
  1154. *
  1155. * Return true if exactly one bit is set in the scan mask, false otherwise. It
  1156. * can be used for devices where only one channel can be active for sampling at
  1157. * a time.
  1158. */
  1159. bool iio_validate_scan_mask_onehot(struct iio_dev *indio_dev,
  1160. const unsigned long *mask)
  1161. {
  1162. return bitmap_weight(mask, indio_dev->masklength) == 1;
  1163. }
  1164. EXPORT_SYMBOL_GPL(iio_validate_scan_mask_onehot);
  1165. static const void *iio_demux(struct iio_buffer *buffer,
  1166. const void *datain)
  1167. {
  1168. struct iio_demux_table *t;
  1169. if (list_empty(&buffer->demux_list))
  1170. return datain;
  1171. list_for_each_entry(t, &buffer->demux_list, l)
  1172. memcpy(buffer->demux_bounce + t->to,
  1173. datain + t->from, t->length);
  1174. return buffer->demux_bounce;
  1175. }
  1176. static int iio_push_to_buffer(struct iio_buffer *buffer, const void *data)
  1177. {
  1178. const void *dataout = iio_demux(buffer, data);
  1179. int ret;
  1180. ret = buffer->access->store_to(buffer, dataout);
  1181. if (ret)
  1182. return ret;
  1183. /*
  1184. * We can't just test for watermark to decide if we wake the poll queue
  1185. * because read may request less samples than the watermark.
  1186. */
  1187. wake_up_interruptible_poll(&buffer->pollq, EPOLLIN | EPOLLRDNORM);
  1188. return 0;
  1189. }
  1190. /**
  1191. * iio_push_to_buffers() - push to a registered buffer.
  1192. * @indio_dev: iio_dev structure for device.
  1193. * @data: Full scan.
  1194. */
  1195. int iio_push_to_buffers(struct iio_dev *indio_dev, const void *data)
  1196. {
  1197. int ret;
  1198. struct iio_buffer *buf;
  1199. list_for_each_entry(buf, &indio_dev->buffer_list, buffer_list) {
  1200. ret = iio_push_to_buffer(buf, data);
  1201. if (ret < 0)
  1202. return ret;
  1203. }
  1204. return 0;
  1205. }
  1206. EXPORT_SYMBOL_GPL(iio_push_to_buffers);
  1207. /**
  1208. * iio_buffer_release() - Free a buffer's resources
  1209. * @ref: Pointer to the kref embedded in the iio_buffer struct
  1210. *
  1211. * This function is called when the last reference to the buffer has been
  1212. * dropped. It will typically free all resources allocated by the buffer. Do not
  1213. * call this function manually, always use iio_buffer_put() when done using a
  1214. * buffer.
  1215. */
  1216. static void iio_buffer_release(struct kref *ref)
  1217. {
  1218. struct iio_buffer *buffer = container_of(ref, struct iio_buffer, ref);
  1219. buffer->access->release(buffer);
  1220. }
  1221. /**
  1222. * iio_buffer_get() - Grab a reference to the buffer
  1223. * @buffer: The buffer to grab a reference for, may be NULL
  1224. *
  1225. * Returns the pointer to the buffer that was passed into the function.
  1226. */
  1227. struct iio_buffer *iio_buffer_get(struct iio_buffer *buffer)
  1228. {
  1229. if (buffer)
  1230. kref_get(&buffer->ref);
  1231. return buffer;
  1232. }
  1233. EXPORT_SYMBOL_GPL(iio_buffer_get);
  1234. /**
  1235. * iio_buffer_put() - Release the reference to the buffer
  1236. * @buffer: The buffer to release the reference for, may be NULL
  1237. */
  1238. void iio_buffer_put(struct iio_buffer *buffer)
  1239. {
  1240. if (buffer)
  1241. kref_put(&buffer->ref, iio_buffer_release);
  1242. }
  1243. EXPORT_SYMBOL_GPL(iio_buffer_put);
  1244. /**
  1245. * iio_device_attach_buffer - Attach a buffer to a IIO device
  1246. * @indio_dev: The device the buffer should be attached to
  1247. * @buffer: The buffer to attach to the device
  1248. *
  1249. * This function attaches a buffer to a IIO device. The buffer stays attached to
  1250. * the device until the device is freed. The function should only be called at
  1251. * most once per device.
  1252. */
  1253. void iio_device_attach_buffer(struct iio_dev *indio_dev,
  1254. struct iio_buffer *buffer)
  1255. {
  1256. indio_dev->buffer = iio_buffer_get(buffer);
  1257. }
  1258. EXPORT_SYMBOL_GPL(iio_device_attach_buffer);