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