industrialio-buffer.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483
  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. in_ind = find_next_bit(indio_dev->active_scan_mask,
  737. indio_dev->masklength,
  738. in_ind + 1);
  739. length = iio_storage_bytes_for_si(indio_dev, in_ind);
  740. /* Make sure we are aligned */
  741. in_loc = roundup(in_loc, length) + length;
  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);