comedi_buf.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * comedi_buf.c
  4. *
  5. * COMEDI - Linux Control and Measurement Device Interface
  6. * Copyright (C) 1997-2000 David A. Schleef <ds@schleef.org>
  7. * Copyright (C) 2002 Frank Mori Hess <fmhess@users.sourceforge.net>
  8. */
  9. #include <linux/vmalloc.h>
  10. #include <linux/slab.h>
  11. #include <linux/comedi/comedidev.h>
  12. #include "comedi_internal.h"
  13. #ifdef PAGE_KERNEL_NOCACHE
  14. #define COMEDI_PAGE_PROTECTION PAGE_KERNEL_NOCACHE
  15. #else
  16. #define COMEDI_PAGE_PROTECTION PAGE_KERNEL
  17. #endif
  18. static void comedi_buf_map_kref_release(struct kref *kref)
  19. {
  20. struct comedi_buf_map *bm =
  21. container_of(kref, struct comedi_buf_map, refcount);
  22. struct comedi_buf_page *buf;
  23. unsigned int i;
  24. if (bm->page_list) {
  25. if (bm->dma_dir != DMA_NONE) {
  26. for (i = 0; i < bm->n_pages; i++) {
  27. buf = &bm->page_list[i];
  28. dma_free_coherent(bm->dma_hw_dev, PAGE_SIZE,
  29. buf->virt_addr,
  30. buf->dma_addr);
  31. }
  32. } else {
  33. for (i = 0; i < bm->n_pages; i++) {
  34. buf = &bm->page_list[i];
  35. ClearPageReserved(virt_to_page(buf->virt_addr));
  36. free_page((unsigned long)buf->virt_addr);
  37. }
  38. }
  39. vfree(bm->page_list);
  40. }
  41. if (bm->dma_dir != DMA_NONE)
  42. put_device(bm->dma_hw_dev);
  43. kfree(bm);
  44. }
  45. static void __comedi_buf_free(struct comedi_device *dev,
  46. struct comedi_subdevice *s)
  47. {
  48. struct comedi_async *async = s->async;
  49. struct comedi_buf_map *bm;
  50. unsigned long flags;
  51. async->prealloc_bufsz = 0;
  52. spin_lock_irqsave(&s->spin_lock, flags);
  53. bm = async->buf_map;
  54. async->buf_map = NULL;
  55. spin_unlock_irqrestore(&s->spin_lock, flags);
  56. comedi_buf_map_put(bm);
  57. }
  58. static struct comedi_buf_map *
  59. comedi_buf_map_alloc(struct comedi_device *dev, enum dma_data_direction dma_dir,
  60. unsigned int n_pages)
  61. {
  62. struct comedi_buf_map *bm;
  63. struct comedi_buf_page *buf;
  64. unsigned int i;
  65. bm = kzalloc_obj(*bm);
  66. if (!bm)
  67. return NULL;
  68. kref_init(&bm->refcount);
  69. bm->dma_dir = dma_dir;
  70. if (bm->dma_dir != DMA_NONE) {
  71. /* Need ref to hardware device to free buffer later. */
  72. bm->dma_hw_dev = get_device(dev->hw_dev);
  73. }
  74. bm->page_list = vzalloc(sizeof(*buf) * n_pages);
  75. if (!bm->page_list)
  76. goto err;
  77. if (bm->dma_dir != DMA_NONE) {
  78. for (i = 0; i < n_pages; i++) {
  79. buf = &bm->page_list[i];
  80. buf->virt_addr =
  81. dma_alloc_coherent(bm->dma_hw_dev, PAGE_SIZE,
  82. &buf->dma_addr, GFP_KERNEL);
  83. if (!buf->virt_addr)
  84. break;
  85. }
  86. } else {
  87. for (i = 0; i < n_pages; i++) {
  88. buf = &bm->page_list[i];
  89. buf->virt_addr = (void *)get_zeroed_page(GFP_KERNEL);
  90. if (!buf->virt_addr)
  91. break;
  92. SetPageReserved(virt_to_page(buf->virt_addr));
  93. }
  94. }
  95. bm->n_pages = i;
  96. if (i < n_pages)
  97. goto err;
  98. return bm;
  99. err:
  100. comedi_buf_map_put(bm);
  101. return NULL;
  102. }
  103. static void __comedi_buf_alloc(struct comedi_device *dev,
  104. struct comedi_subdevice *s,
  105. unsigned int n_pages)
  106. {
  107. struct comedi_async *async = s->async;
  108. struct comedi_buf_map *bm;
  109. unsigned long flags;
  110. if (!IS_ENABLED(CONFIG_HAS_DMA) && s->async_dma_dir != DMA_NONE) {
  111. dev_err(dev->class_dev,
  112. "dma buffer allocation not supported\n");
  113. return;
  114. }
  115. bm = comedi_buf_map_alloc(dev, s->async_dma_dir, n_pages);
  116. if (!bm)
  117. return;
  118. spin_lock_irqsave(&s->spin_lock, flags);
  119. async->buf_map = bm;
  120. spin_unlock_irqrestore(&s->spin_lock, flags);
  121. async->prealloc_bufsz = n_pages << PAGE_SHIFT;
  122. }
  123. void comedi_buf_map_get(struct comedi_buf_map *bm)
  124. {
  125. if (bm)
  126. kref_get(&bm->refcount);
  127. }
  128. int comedi_buf_map_put(struct comedi_buf_map *bm)
  129. {
  130. if (bm)
  131. return kref_put(&bm->refcount, comedi_buf_map_kref_release);
  132. return 1;
  133. }
  134. /* helper for "access" vm operation */
  135. int comedi_buf_map_access(struct comedi_buf_map *bm, unsigned long offset,
  136. void *buf, int len, int write)
  137. {
  138. unsigned int pgoff = offset_in_page(offset);
  139. unsigned long pg = offset >> PAGE_SHIFT;
  140. int done = 0;
  141. while (done < len && pg < bm->n_pages) {
  142. int l = min_t(int, len - done, PAGE_SIZE - pgoff);
  143. void *b = bm->page_list[pg].virt_addr + pgoff;
  144. if (write)
  145. memcpy(b, buf, l);
  146. else
  147. memcpy(buf, b, l);
  148. buf += l;
  149. done += l;
  150. pg++;
  151. pgoff = 0;
  152. }
  153. return done;
  154. }
  155. /* returns s->async->buf_map and increments its kref refcount */
  156. struct comedi_buf_map *
  157. comedi_buf_map_from_subdev_get(struct comedi_subdevice *s)
  158. {
  159. struct comedi_async *async = s->async;
  160. struct comedi_buf_map *bm = NULL;
  161. unsigned long flags;
  162. if (!async)
  163. return NULL;
  164. spin_lock_irqsave(&s->spin_lock, flags);
  165. bm = async->buf_map;
  166. /* only want it if buffer pages allocated */
  167. if (bm && bm->n_pages)
  168. comedi_buf_map_get(bm);
  169. else
  170. bm = NULL;
  171. spin_unlock_irqrestore(&s->spin_lock, flags);
  172. return bm;
  173. }
  174. bool comedi_buf_is_mmapped(struct comedi_subdevice *s)
  175. {
  176. struct comedi_buf_map *bm = s->async->buf_map;
  177. return bm && (kref_read(&bm->refcount) > 1);
  178. }
  179. int comedi_buf_alloc(struct comedi_device *dev, struct comedi_subdevice *s,
  180. unsigned long new_size)
  181. {
  182. struct comedi_async *async = s->async;
  183. lockdep_assert_held(&dev->mutex);
  184. /* Round up new_size to multiple of PAGE_SIZE */
  185. new_size = (new_size + PAGE_SIZE - 1) & PAGE_MASK;
  186. /* if no change is required, do nothing */
  187. if (async->prealloc_bufsz == new_size)
  188. return 0;
  189. /* deallocate old buffer */
  190. __comedi_buf_free(dev, s);
  191. /* allocate new buffer */
  192. if (new_size) {
  193. unsigned int n_pages = new_size >> PAGE_SHIFT;
  194. __comedi_buf_alloc(dev, s, n_pages);
  195. if (!async->prealloc_bufsz)
  196. return -ENOMEM;
  197. }
  198. return 0;
  199. }
  200. void comedi_buf_reset(struct comedi_subdevice *s)
  201. {
  202. struct comedi_async *async = s->async;
  203. async->buf_write_alloc_count = 0;
  204. async->buf_write_count = 0;
  205. async->buf_read_alloc_count = 0;
  206. async->buf_read_count = 0;
  207. async->buf_write_ptr = 0;
  208. async->buf_read_ptr = 0;
  209. async->cur_chan = 0;
  210. async->scans_done = 0;
  211. async->scan_progress = 0;
  212. async->munge_chan = 0;
  213. async->munge_count = 0;
  214. async->munge_ptr = 0;
  215. async->events = 0;
  216. }
  217. static unsigned int comedi_buf_write_n_unalloc(struct comedi_subdevice *s)
  218. {
  219. struct comedi_async *async = s->async;
  220. unsigned int free_end = async->buf_read_count + async->prealloc_bufsz;
  221. return free_end - async->buf_write_alloc_count;
  222. }
  223. unsigned int comedi_buf_write_n_available(struct comedi_subdevice *s)
  224. {
  225. struct comedi_async *async = s->async;
  226. unsigned int free_end = async->buf_read_count + async->prealloc_bufsz;
  227. return free_end - async->buf_write_count;
  228. }
  229. unsigned int _comedi_buf_write_alloc(struct comedi_subdevice *s,
  230. unsigned int nbytes)
  231. {
  232. struct comedi_async *async = s->async;
  233. unsigned int unalloc = comedi_buf_write_n_unalloc(s);
  234. if (nbytes > unalloc)
  235. nbytes = unalloc;
  236. async->buf_write_alloc_count += nbytes;
  237. /*
  238. * ensure the async buffer 'counts' are read and updated
  239. * before we write data to the write-alloc'ed buffer space
  240. */
  241. smp_mb();
  242. return nbytes;
  243. }
  244. /**
  245. * comedi_buf_write_alloc() - Reserve buffer space for writing
  246. * @s: COMEDI subdevice.
  247. * @nbytes: Maximum space to reserve in bytes.
  248. *
  249. * Reserve up to @nbytes bytes of space to be written in the COMEDI acquisition
  250. * data buffer associated with the subdevice. The amount reserved is limited
  251. * by the space available.
  252. *
  253. * Return: The amount of space reserved in bytes.
  254. */
  255. unsigned int comedi_buf_write_alloc(struct comedi_subdevice *s,
  256. unsigned int nbytes)
  257. {
  258. if (comedi_get_is_subdevice_running(s)) {
  259. nbytes = _comedi_buf_write_alloc(s, nbytes);
  260. comedi_put_is_subdevice_running(s);
  261. } else {
  262. nbytes = 0;
  263. }
  264. return nbytes;
  265. }
  266. EXPORT_SYMBOL_GPL(comedi_buf_write_alloc);
  267. /*
  268. * munging is applied to data by core as it passes between user
  269. * and kernel space
  270. */
  271. static unsigned int comedi_buf_munge(struct comedi_subdevice *s,
  272. unsigned int num_bytes)
  273. {
  274. struct comedi_async *async = s->async;
  275. struct comedi_buf_page *buf_page_list = async->buf_map->page_list;
  276. unsigned int count = 0;
  277. const unsigned int num_sample_bytes = comedi_bytes_per_sample(s);
  278. if (!s->munge || (async->cmd.flags & CMDF_RAWDATA) || async->cmd.chanlist_len == 0) {
  279. async->munge_count += num_bytes;
  280. return num_bytes;
  281. }
  282. /* don't munge partial samples */
  283. num_bytes -= num_bytes % num_sample_bytes;
  284. while (count < num_bytes) {
  285. /*
  286. * Do not munge beyond page boundary.
  287. * Note: prealloc_bufsz is a multiple of PAGE_SIZE.
  288. */
  289. unsigned int page = async->munge_ptr >> PAGE_SHIFT;
  290. unsigned int offset = offset_in_page(async->munge_ptr);
  291. unsigned int block_size =
  292. min(num_bytes - count, PAGE_SIZE - offset);
  293. s->munge(s->device, s, buf_page_list[page].virt_addr + offset,
  294. block_size, async->munge_chan);
  295. /*
  296. * ensure data is munged in buffer before the
  297. * async buffer munge_count is incremented
  298. */
  299. smp_wmb();
  300. async->munge_chan += block_size / num_sample_bytes;
  301. async->munge_chan %= async->cmd.chanlist_len;
  302. async->munge_count += block_size;
  303. async->munge_ptr += block_size;
  304. if (async->munge_ptr == async->prealloc_bufsz)
  305. async->munge_ptr = 0;
  306. count += block_size;
  307. }
  308. return count;
  309. }
  310. unsigned int comedi_buf_write_n_allocated(struct comedi_subdevice *s)
  311. {
  312. struct comedi_async *async = s->async;
  313. return async->buf_write_alloc_count - async->buf_write_count;
  314. }
  315. unsigned int _comedi_buf_write_free(struct comedi_subdevice *s,
  316. unsigned int nbytes)
  317. {
  318. struct comedi_async *async = s->async;
  319. unsigned int allocated = comedi_buf_write_n_allocated(s);
  320. if (nbytes > allocated)
  321. nbytes = allocated;
  322. async->buf_write_count += nbytes;
  323. async->buf_write_ptr += nbytes;
  324. comedi_buf_munge(s, async->buf_write_count - async->munge_count);
  325. if (async->buf_write_ptr >= async->prealloc_bufsz)
  326. async->buf_write_ptr %= async->prealloc_bufsz;
  327. return nbytes;
  328. }
  329. /**
  330. * comedi_buf_write_free() - Free buffer space after it is written
  331. * @s: COMEDI subdevice.
  332. * @nbytes: Maximum space to free in bytes.
  333. *
  334. * Free up to @nbytes bytes of space previously reserved for writing in the
  335. * COMEDI acquisition data buffer associated with the subdevice. The amount of
  336. * space freed is limited to the amount that was reserved. The freed space is
  337. * assumed to have been filled with sample data by the writer.
  338. *
  339. * If the samples in the freed space need to be "munged", do so here. The
  340. * freed space becomes available for allocation by the reader.
  341. *
  342. * Return: The amount of space freed in bytes.
  343. */
  344. unsigned int comedi_buf_write_free(struct comedi_subdevice *s,
  345. unsigned int nbytes)
  346. {
  347. if (comedi_get_is_subdevice_running(s)) {
  348. nbytes = _comedi_buf_write_free(s, nbytes);
  349. comedi_put_is_subdevice_running(s);
  350. } else {
  351. nbytes = 0;
  352. }
  353. return nbytes;
  354. }
  355. EXPORT_SYMBOL_GPL(comedi_buf_write_free);
  356. unsigned int _comedi_buf_read_n_available(struct comedi_subdevice *s)
  357. {
  358. struct comedi_async *async = s->async;
  359. unsigned int num_bytes;
  360. if (!async)
  361. return 0;
  362. num_bytes = async->munge_count - async->buf_read_count;
  363. /*
  364. * ensure the async buffer 'counts' are read before we
  365. * attempt to read data from the buffer
  366. */
  367. smp_rmb();
  368. return num_bytes;
  369. }
  370. /**
  371. * comedi_buf_read_n_available() - Determine amount of readable buffer space
  372. * @s: COMEDI subdevice.
  373. *
  374. * Determine the amount of readable buffer space in the COMEDI acquisition data
  375. * buffer associated with the subdevice. The readable buffer space is that
  376. * which has been freed by the writer and "munged" to the sample data format
  377. * expected by COMEDI if necessary.
  378. *
  379. * Return: The amount of readable buffer space.
  380. */
  381. unsigned int comedi_buf_read_n_available(struct comedi_subdevice *s)
  382. {
  383. unsigned int num_bytes;
  384. if (comedi_get_is_subdevice_running(s)) {
  385. num_bytes = _comedi_buf_read_n_available(s);
  386. comedi_put_is_subdevice_running(s);
  387. } else {
  388. num_bytes = 0;
  389. }
  390. return num_bytes;
  391. }
  392. EXPORT_SYMBOL_GPL(comedi_buf_read_n_available);
  393. unsigned int _comedi_buf_read_alloc(struct comedi_subdevice *s,
  394. unsigned int nbytes)
  395. {
  396. struct comedi_async *async = s->async;
  397. unsigned int available;
  398. available = async->munge_count - async->buf_read_alloc_count;
  399. if (nbytes > available)
  400. nbytes = available;
  401. async->buf_read_alloc_count += nbytes;
  402. /*
  403. * ensure the async buffer 'counts' are read before we
  404. * attempt to read data from the read-alloc'ed buffer space
  405. */
  406. smp_rmb();
  407. return nbytes;
  408. }
  409. /**
  410. * comedi_buf_read_alloc() - Reserve buffer space for reading
  411. * @s: COMEDI subdevice.
  412. * @nbytes: Maximum space to reserve in bytes.
  413. *
  414. * Reserve up to @nbytes bytes of previously written and "munged" buffer space
  415. * for reading in the COMEDI acquisition data buffer associated with the
  416. * subdevice. The amount reserved is limited to the space available. The
  417. * reader can read from the reserved space and then free it. A reader is also
  418. * allowed to read from the space before reserving it as long as it determines
  419. * the amount of readable data available, but the space needs to be marked as
  420. * reserved before it can be freed.
  421. *
  422. * Return: The amount of space reserved in bytes.
  423. */
  424. unsigned int comedi_buf_read_alloc(struct comedi_subdevice *s,
  425. unsigned int nbytes)
  426. {
  427. if (comedi_get_is_subdevice_running(s)) {
  428. nbytes = _comedi_buf_read_alloc(s, nbytes);
  429. comedi_put_is_subdevice_running(s);
  430. } else {
  431. nbytes = 0;
  432. }
  433. return nbytes;
  434. }
  435. EXPORT_SYMBOL_GPL(comedi_buf_read_alloc);
  436. static unsigned int comedi_buf_read_n_allocated(struct comedi_async *async)
  437. {
  438. return async->buf_read_alloc_count - async->buf_read_count;
  439. }
  440. unsigned int _comedi_buf_read_free(struct comedi_subdevice *s,
  441. unsigned int nbytes)
  442. {
  443. struct comedi_async *async = s->async;
  444. unsigned int allocated;
  445. /*
  446. * ensure data has been read out of buffer before
  447. * the async read count is incremented
  448. */
  449. smp_mb();
  450. allocated = comedi_buf_read_n_allocated(async);
  451. if (nbytes > allocated)
  452. nbytes = allocated;
  453. async->buf_read_count += nbytes;
  454. async->buf_read_ptr += nbytes;
  455. async->buf_read_ptr %= async->prealloc_bufsz;
  456. return nbytes;
  457. }
  458. /**
  459. * comedi_buf_read_free() - Free buffer space after it has been read
  460. * @s: COMEDI subdevice.
  461. * @nbytes: Maximum space to free in bytes.
  462. *
  463. * Free up to @nbytes bytes of buffer space previously reserved for reading in
  464. * the COMEDI acquisition data buffer associated with the subdevice. The
  465. * amount of space freed is limited to the amount that was reserved.
  466. *
  467. * The freed space becomes available for allocation by the writer.
  468. *
  469. * Return: The amount of space freed in bytes.
  470. */
  471. unsigned int comedi_buf_read_free(struct comedi_subdevice *s,
  472. unsigned int nbytes)
  473. {
  474. if (comedi_get_is_subdevice_running(s)) {
  475. nbytes = _comedi_buf_read_free(s, nbytes);
  476. comedi_put_is_subdevice_running(s);
  477. } else {
  478. nbytes = 0;
  479. }
  480. return nbytes;
  481. }
  482. EXPORT_SYMBOL_GPL(comedi_buf_read_free);
  483. static void comedi_buf_memcpy_to(struct comedi_subdevice *s,
  484. const void *data, unsigned int num_bytes)
  485. {
  486. struct comedi_async *async = s->async;
  487. struct comedi_buf_page *buf_page_list = async->buf_map->page_list;
  488. unsigned int write_ptr = async->buf_write_ptr;
  489. while (num_bytes) {
  490. /*
  491. * Do not copy beyond page boundary.
  492. * Note: prealloc_bufsz is a multiple of PAGE_SIZE.
  493. */
  494. unsigned int page = write_ptr >> PAGE_SHIFT;
  495. unsigned int offset = offset_in_page(write_ptr);
  496. unsigned int block_size = min(num_bytes, PAGE_SIZE - offset);
  497. memcpy(buf_page_list[page].virt_addr + offset,
  498. data, block_size);
  499. data += block_size;
  500. num_bytes -= block_size;
  501. write_ptr += block_size;
  502. if (write_ptr == async->prealloc_bufsz)
  503. write_ptr = 0;
  504. }
  505. }
  506. static void comedi_buf_memcpy_from(struct comedi_subdevice *s,
  507. void *dest, unsigned int nbytes)
  508. {
  509. struct comedi_async *async = s->async;
  510. struct comedi_buf_page *buf_page_list = async->buf_map->page_list;
  511. unsigned int read_ptr = async->buf_read_ptr;
  512. while (nbytes) {
  513. /*
  514. * Do not copy beyond page boundary.
  515. * Note: prealloc_bufsz is a multiple of PAGE_SIZE.
  516. */
  517. unsigned int page = read_ptr >> PAGE_SHIFT;
  518. unsigned int offset = offset_in_page(read_ptr);
  519. unsigned int block_size = min(nbytes, PAGE_SIZE - offset);
  520. memcpy(dest, buf_page_list[page].virt_addr + offset,
  521. block_size);
  522. nbytes -= block_size;
  523. dest += block_size;
  524. read_ptr += block_size;
  525. if (read_ptr == async->prealloc_bufsz)
  526. read_ptr = 0;
  527. }
  528. }
  529. static unsigned int _comedi_buf_write_samples(struct comedi_subdevice *s,
  530. const void *data,
  531. unsigned int nsamples)
  532. {
  533. unsigned int max_samples;
  534. unsigned int nbytes;
  535. /*
  536. * Make sure there is enough room in the buffer for all the samples.
  537. * If not, clamp the nsamples to the number that will fit, flag the
  538. * buffer overrun and add the samples that fit.
  539. */
  540. max_samples = comedi_bytes_to_samples(s, comedi_buf_write_n_unalloc(s));
  541. if (nsamples > max_samples) {
  542. dev_warn(s->device->class_dev, "buffer overrun\n");
  543. s->async->events |= COMEDI_CB_OVERFLOW;
  544. nsamples = max_samples;
  545. }
  546. if (nsamples == 0)
  547. return 0;
  548. nbytes = comedi_samples_to_bytes(s, nsamples);
  549. nbytes = _comedi_buf_write_alloc(s, nbytes);
  550. comedi_buf_memcpy_to(s, data, nbytes);
  551. _comedi_buf_write_free(s, nbytes);
  552. _comedi_inc_scan_progress(s, nbytes);
  553. s->async->events |= COMEDI_CB_BLOCK;
  554. return nbytes;
  555. }
  556. /**
  557. * comedi_buf_write_samples() - Write sample data to COMEDI buffer
  558. * @s: COMEDI subdevice.
  559. * @data: Pointer to source samples.
  560. * @nsamples: Number of samples to write.
  561. *
  562. * Write up to @nsamples samples to the COMEDI acquisition data buffer
  563. * associated with the subdevice, mark it as written and update the
  564. * acquisition scan progress. If there is not enough room for the specified
  565. * number of samples, the number of samples written is limited to the number
  566. * that will fit and the %COMEDI_CB_OVERFLOW event flag is set to cause the
  567. * acquisition to terminate with an overrun error. Set the %COMEDI_CB_BLOCK
  568. * event flag if any samples are written to cause waiting tasks to be woken
  569. * when the event flags are processed.
  570. *
  571. * Return: The amount of data written in bytes.
  572. */
  573. unsigned int comedi_buf_write_samples(struct comedi_subdevice *s,
  574. const void *data, unsigned int nsamples)
  575. {
  576. unsigned int nbytes;
  577. if (comedi_get_is_subdevice_running(s)) {
  578. nbytes = _comedi_buf_write_samples(s, data, nsamples);
  579. comedi_put_is_subdevice_running(s);
  580. } else {
  581. nbytes = 0;
  582. }
  583. return nbytes;
  584. }
  585. EXPORT_SYMBOL_GPL(comedi_buf_write_samples);
  586. static unsigned int _comedi_buf_read_samples(struct comedi_subdevice *s,
  587. void *data, unsigned int nsamples)
  588. {
  589. unsigned int max_samples;
  590. unsigned int nbytes;
  591. /* clamp nsamples to the number of full samples available */
  592. max_samples = comedi_bytes_to_samples(s,
  593. _comedi_buf_read_n_available(s));
  594. if (nsamples > max_samples)
  595. nsamples = max_samples;
  596. if (nsamples == 0)
  597. return 0;
  598. nbytes = _comedi_buf_read_alloc(s,
  599. comedi_samples_to_bytes(s, nsamples));
  600. comedi_buf_memcpy_from(s, data, nbytes);
  601. _comedi_buf_read_free(s, nbytes);
  602. _comedi_inc_scan_progress(s, nbytes);
  603. s->async->events |= COMEDI_CB_BLOCK;
  604. return nbytes;
  605. }
  606. /**
  607. * comedi_buf_read_samples() - Read sample data from COMEDI buffer
  608. * @s: COMEDI subdevice.
  609. * @data: Pointer to destination.
  610. * @nsamples: Maximum number of samples to read.
  611. *
  612. * Read up to @nsamples samples from the COMEDI acquisition data buffer
  613. * associated with the subdevice, mark it as read and update the acquisition
  614. * scan progress. Limit the number of samples read to the number available.
  615. * Set the %COMEDI_CB_BLOCK event flag if any samples are read to cause waiting
  616. * tasks to be woken when the event flags are processed.
  617. *
  618. * Return: The amount of data read in bytes.
  619. */
  620. unsigned int comedi_buf_read_samples(struct comedi_subdevice *s,
  621. void *data, unsigned int nsamples)
  622. {
  623. unsigned int nbytes;
  624. if (comedi_get_is_subdevice_running(s)) {
  625. nbytes = _comedi_buf_read_samples(s, data, nsamples);
  626. comedi_put_is_subdevice_running(s);
  627. } else {
  628. nbytes = 0;
  629. }
  630. return nbytes;
  631. }
  632. EXPORT_SYMBOL_GPL(comedi_buf_read_samples);