comedi_buf.c 17 KB

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