H Hartley Sweeten | ea082fb | 2013-01-09 13:25:06 -0700 | [diff] [blame] | 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 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 20 | */ |
| 21 | |
| 22 | #include "comedidev.h" |
| 23 | #include "comedi_internal.h" |
| 24 | |
H Hartley Sweeten | 6bd7645 | 2013-01-09 13:26:26 -0700 | [diff] [blame] | 25 | #ifdef PAGE_KERNEL_NOCACHE |
| 26 | #define COMEDI_PAGE_PROTECTION PAGE_KERNEL_NOCACHE |
| 27 | #else |
| 28 | #define COMEDI_PAGE_PROTECTION PAGE_KERNEL |
| 29 | #endif |
| 30 | |
H Hartley Sweeten | 718c4d6 | 2013-01-09 13:25:54 -0700 | [diff] [blame] | 31 | static void __comedi_buf_free(struct comedi_device *dev, |
| 32 | struct comedi_subdevice *s, |
| 33 | unsigned n_pages) |
| 34 | { |
| 35 | struct comedi_async *async = s->async; |
| 36 | struct comedi_buf_page *buf; |
| 37 | unsigned i; |
| 38 | |
| 39 | if (async->prealloc_buf) { |
| 40 | vunmap(async->prealloc_buf); |
| 41 | async->prealloc_buf = NULL; |
| 42 | async->prealloc_bufsz = 0; |
| 43 | } |
| 44 | |
| 45 | if (!async->buf_page_list) |
| 46 | return; |
| 47 | |
| 48 | for (i = 0; i < n_pages; ++i) { |
| 49 | buf = &async->buf_page_list[i]; |
| 50 | if (buf->virt_addr) { |
| 51 | clear_bit(PG_reserved, |
| 52 | &(virt_to_page(buf->virt_addr)->flags)); |
| 53 | if (s->async_dma_dir != DMA_NONE) { |
Ian Abbott | 4efc4bb | 2013-05-10 14:07:15 +0100 | [diff] [blame^] | 54 | #ifdef CONFIG_HAS_DMA |
H Hartley Sweeten | 718c4d6 | 2013-01-09 13:25:54 -0700 | [diff] [blame] | 55 | dma_free_coherent(dev->hw_dev, |
| 56 | PAGE_SIZE, |
| 57 | buf->virt_addr, |
| 58 | buf->dma_addr); |
Ian Abbott | 4efc4bb | 2013-05-10 14:07:15 +0100 | [diff] [blame^] | 59 | #endif |
H Hartley Sweeten | 718c4d6 | 2013-01-09 13:25:54 -0700 | [diff] [blame] | 60 | } else { |
| 61 | free_page((unsigned long)buf->virt_addr); |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | vfree(async->buf_page_list); |
| 66 | async->buf_page_list = NULL; |
| 67 | async->n_buf_pages = 0; |
| 68 | } |
| 69 | |
H Hartley Sweeten | 6bd7645 | 2013-01-09 13:26:26 -0700 | [diff] [blame] | 70 | static void __comedi_buf_alloc(struct comedi_device *dev, |
| 71 | struct comedi_subdevice *s, |
| 72 | unsigned n_pages) |
| 73 | { |
| 74 | struct comedi_async *async = s->async; |
| 75 | struct page **pages = NULL; |
| 76 | struct comedi_buf_page *buf; |
| 77 | unsigned i; |
| 78 | |
| 79 | async->buf_page_list = vzalloc(sizeof(*buf) * n_pages); |
| 80 | if (async->buf_page_list) |
| 81 | pages = vmalloc(sizeof(struct page *) * n_pages); |
| 82 | |
| 83 | if (!pages) |
| 84 | return; |
| 85 | |
| 86 | for (i = 0; i < n_pages; i++) { |
| 87 | buf = &async->buf_page_list[i]; |
| 88 | if (s->async_dma_dir != DMA_NONE) |
Ian Abbott | 4efc4bb | 2013-05-10 14:07:15 +0100 | [diff] [blame^] | 89 | #ifdef CONFIG_HAS_DMA |
H Hartley Sweeten | 6bd7645 | 2013-01-09 13:26:26 -0700 | [diff] [blame] | 90 | buf->virt_addr = dma_alloc_coherent(dev->hw_dev, |
| 91 | PAGE_SIZE, |
| 92 | &buf->dma_addr, |
| 93 | GFP_KERNEL | |
| 94 | __GFP_COMP); |
Ian Abbott | 4efc4bb | 2013-05-10 14:07:15 +0100 | [diff] [blame^] | 95 | #else |
| 96 | break; |
| 97 | #endif |
H Hartley Sweeten | 6bd7645 | 2013-01-09 13:26:26 -0700 | [diff] [blame] | 98 | else |
| 99 | buf->virt_addr = (void *)get_zeroed_page(GFP_KERNEL); |
| 100 | if (!buf->virt_addr) |
| 101 | break; |
| 102 | |
| 103 | set_bit(PG_reserved, &(virt_to_page(buf->virt_addr)->flags)); |
| 104 | |
| 105 | pages[i] = virt_to_page(buf->virt_addr); |
| 106 | } |
| 107 | |
| 108 | /* vmap the prealloc_buf if all the pages were allocated */ |
| 109 | if (i == n_pages) |
| 110 | async->prealloc_buf = vmap(pages, n_pages, VM_MAP, |
| 111 | COMEDI_PAGE_PROTECTION); |
| 112 | |
| 113 | vfree(pages); |
| 114 | } |
| 115 | |
H Hartley Sweeten | ea082fb | 2013-01-09 13:25:06 -0700 | [diff] [blame] | 116 | int comedi_buf_alloc(struct comedi_device *dev, struct comedi_subdevice *s, |
| 117 | unsigned long new_size) |
| 118 | { |
| 119 | struct comedi_async *async = s->async; |
| 120 | |
| 121 | /* Round up new_size to multiple of PAGE_SIZE */ |
| 122 | new_size = (new_size + PAGE_SIZE - 1) & PAGE_MASK; |
| 123 | |
| 124 | /* if no change is required, do nothing */ |
| 125 | if (async->prealloc_buf && async->prealloc_bufsz == new_size) |
| 126 | return 0; |
| 127 | |
H Hartley Sweeten | 718c4d6 | 2013-01-09 13:25:54 -0700 | [diff] [blame] | 128 | /* deallocate old buffer */ |
| 129 | __comedi_buf_free(dev, s, async->n_buf_pages); |
| 130 | |
H Hartley Sweeten | 6bd7645 | 2013-01-09 13:26:26 -0700 | [diff] [blame] | 131 | /* allocate new buffer */ |
H Hartley Sweeten | ea082fb | 2013-01-09 13:25:06 -0700 | [diff] [blame] | 132 | if (new_size) { |
H Hartley Sweeten | ea082fb | 2013-01-09 13:25:06 -0700 | [diff] [blame] | 133 | unsigned n_pages = new_size >> PAGE_SHIFT; |
H Hartley Sweeten | ea082fb | 2013-01-09 13:25:06 -0700 | [diff] [blame] | 134 | |
H Hartley Sweeten | 6bd7645 | 2013-01-09 13:26:26 -0700 | [diff] [blame] | 135 | __comedi_buf_alloc(dev, s, n_pages); |
H Hartley Sweeten | ea082fb | 2013-01-09 13:25:06 -0700 | [diff] [blame] | 136 | |
H Hartley Sweeten | 718c4d6 | 2013-01-09 13:25:54 -0700 | [diff] [blame] | 137 | if (!async->prealloc_buf) { |
H Hartley Sweeten | 6bd7645 | 2013-01-09 13:26:26 -0700 | [diff] [blame] | 138 | /* allocation failed */ |
H Hartley Sweeten | 718c4d6 | 2013-01-09 13:25:54 -0700 | [diff] [blame] | 139 | __comedi_buf_free(dev, s, n_pages); |
H Hartley Sweeten | ea082fb | 2013-01-09 13:25:06 -0700 | [diff] [blame] | 140 | return -ENOMEM; |
| 141 | } |
| 142 | async->n_buf_pages = n_pages; |
| 143 | } |
| 144 | async->prealloc_bufsz = new_size; |
| 145 | |
| 146 | return 0; |
| 147 | } |
| 148 | |
H Hartley Sweeten | 61c9fb0 | 2013-01-09 13:27:07 -0700 | [diff] [blame] | 149 | void comedi_buf_reset(struct comedi_async *async) |
H Hartley Sweeten | ea082fb | 2013-01-09 13:25:06 -0700 | [diff] [blame] | 150 | { |
| 151 | async->buf_write_alloc_count = 0; |
| 152 | async->buf_write_count = 0; |
| 153 | async->buf_read_alloc_count = 0; |
| 154 | async->buf_read_count = 0; |
| 155 | |
| 156 | async->buf_write_ptr = 0; |
| 157 | async->buf_read_ptr = 0; |
| 158 | |
| 159 | async->cur_chan = 0; |
| 160 | async->scan_progress = 0; |
| 161 | async->munge_chan = 0; |
| 162 | async->munge_count = 0; |
| 163 | async->munge_ptr = 0; |
| 164 | |
| 165 | async->events = 0; |
| 166 | } |
| 167 | |
H Hartley Sweeten | f8f76e9 | 2013-01-09 13:27:48 -0700 | [diff] [blame] | 168 | static unsigned int comedi_buf_write_n_available(struct comedi_async *async) |
H Hartley Sweeten | ea082fb | 2013-01-09 13:25:06 -0700 | [diff] [blame] | 169 | { |
H Hartley Sweeten | f8f76e9 | 2013-01-09 13:27:48 -0700 | [diff] [blame] | 170 | unsigned int free_end = async->buf_read_count + async->prealloc_bufsz; |
H Hartley Sweeten | ea082fb | 2013-01-09 13:25:06 -0700 | [diff] [blame] | 171 | |
H Hartley Sweeten | f8f76e9 | 2013-01-09 13:27:48 -0700 | [diff] [blame] | 172 | return free_end - async->buf_write_alloc_count; |
| 173 | } |
H Hartley Sweeten | ea082fb | 2013-01-09 13:25:06 -0700 | [diff] [blame] | 174 | |
H Hartley Sweeten | f8f76e9 | 2013-01-09 13:27:48 -0700 | [diff] [blame] | 175 | static unsigned int __comedi_buf_write_alloc(struct comedi_async *async, |
| 176 | unsigned int nbytes, |
| 177 | int strict) |
| 178 | { |
| 179 | unsigned int available = comedi_buf_write_n_available(async); |
| 180 | |
| 181 | if (nbytes > available) |
| 182 | nbytes = strict ? 0 : available; |
| 183 | |
| 184 | async->buf_write_alloc_count += nbytes; |
| 185 | |
| 186 | /* |
| 187 | * ensure the async buffer 'counts' are read and updated |
| 188 | * before we write data to the write-alloc'ed buffer space |
H Hartley Sweeten | ea082fb | 2013-01-09 13:25:06 -0700 | [diff] [blame] | 189 | */ |
| 190 | smp_mb(); |
H Hartley Sweeten | f8f76e9 | 2013-01-09 13:27:48 -0700 | [diff] [blame] | 191 | |
H Hartley Sweeten | ea082fb | 2013-01-09 13:25:06 -0700 | [diff] [blame] | 192 | return nbytes; |
| 193 | } |
| 194 | |
| 195 | /* allocates chunk for the writer from free buffer space */ |
| 196 | unsigned int comedi_buf_write_alloc(struct comedi_async *async, |
| 197 | unsigned int nbytes) |
| 198 | { |
H Hartley Sweeten | f8f76e9 | 2013-01-09 13:27:48 -0700 | [diff] [blame] | 199 | return __comedi_buf_write_alloc(async, nbytes, 0); |
H Hartley Sweeten | ea082fb | 2013-01-09 13:25:06 -0700 | [diff] [blame] | 200 | } |
H Hartley Sweeten | 5660e74 | 2013-04-12 10:11:54 -0700 | [diff] [blame] | 201 | EXPORT_SYMBOL_GPL(comedi_buf_write_alloc); |
H Hartley Sweeten | ea082fb | 2013-01-09 13:25:06 -0700 | [diff] [blame] | 202 | |
H Hartley Sweeten | 8d4be66 | 2013-01-09 13:29:19 -0700 | [diff] [blame] | 203 | /* |
| 204 | * munging is applied to data by core as it passes between user |
| 205 | * and kernel space |
| 206 | */ |
H Hartley Sweeten | ea082fb | 2013-01-09 13:25:06 -0700 | [diff] [blame] | 207 | static unsigned int comedi_buf_munge(struct comedi_async *async, |
| 208 | unsigned int num_bytes) |
| 209 | { |
| 210 | struct comedi_subdevice *s = async->subdevice; |
| 211 | unsigned int count = 0; |
| 212 | const unsigned num_sample_bytes = bytes_per_sample(s); |
| 213 | |
H Hartley Sweeten | 8d4be66 | 2013-01-09 13:29:19 -0700 | [diff] [blame] | 214 | if (!s->munge || (async->cmd.flags & CMDF_RAWDATA)) { |
H Hartley Sweeten | ea082fb | 2013-01-09 13:25:06 -0700 | [diff] [blame] | 215 | async->munge_count += num_bytes; |
H Hartley Sweeten | 8d4be66 | 2013-01-09 13:29:19 -0700 | [diff] [blame] | 216 | count = num_bytes; |
| 217 | } else { |
| 218 | /* don't munge partial samples */ |
| 219 | num_bytes -= num_bytes % num_sample_bytes; |
| 220 | while (count < num_bytes) { |
| 221 | int block_size = num_bytes - count; |
| 222 | unsigned int buf_end; |
H Hartley Sweeten | ea082fb | 2013-01-09 13:25:06 -0700 | [diff] [blame] | 223 | |
H Hartley Sweeten | 8d4be66 | 2013-01-09 13:29:19 -0700 | [diff] [blame] | 224 | buf_end = async->prealloc_bufsz - async->munge_ptr; |
| 225 | if (block_size > buf_end) |
| 226 | block_size = buf_end; |
| 227 | |
| 228 | s->munge(s->device, s, |
| 229 | async->prealloc_buf + async->munge_ptr, |
| 230 | block_size, async->munge_chan); |
| 231 | |
| 232 | /* |
| 233 | * ensure data is munged in buffer before the |
| 234 | * async buffer munge_count is incremented |
| 235 | */ |
| 236 | smp_wmb(); |
| 237 | |
| 238 | async->munge_chan += block_size / num_sample_bytes; |
| 239 | async->munge_chan %= async->cmd.chanlist_len; |
| 240 | async->munge_count += block_size; |
| 241 | async->munge_ptr += block_size; |
| 242 | async->munge_ptr %= async->prealloc_bufsz; |
| 243 | count += block_size; |
H Hartley Sweeten | ea082fb | 2013-01-09 13:25:06 -0700 | [diff] [blame] | 244 | } |
H Hartley Sweeten | ea082fb | 2013-01-09 13:25:06 -0700 | [diff] [blame] | 245 | } |
H Hartley Sweeten | 8d4be66 | 2013-01-09 13:29:19 -0700 | [diff] [blame] | 246 | |
H Hartley Sweeten | ea082fb | 2013-01-09 13:25:06 -0700 | [diff] [blame] | 247 | return count; |
| 248 | } |
| 249 | |
H Hartley Sweeten | 8bd650f | 2013-01-09 13:32:18 -0700 | [diff] [blame] | 250 | unsigned int comedi_buf_write_n_allocated(struct comedi_async *async) |
| 251 | { |
| 252 | return async->buf_write_alloc_count - async->buf_write_count; |
| 253 | } |
| 254 | |
H Hartley Sweeten | ea082fb | 2013-01-09 13:25:06 -0700 | [diff] [blame] | 255 | /* transfers a chunk from writer to filled buffer space */ |
H Hartley Sweeten | 8ae560a | 2013-01-09 13:32:56 -0700 | [diff] [blame] | 256 | unsigned int comedi_buf_write_free(struct comedi_async *async, |
| 257 | unsigned int nbytes) |
H Hartley Sweeten | ea082fb | 2013-01-09 13:25:06 -0700 | [diff] [blame] | 258 | { |
H Hartley Sweeten | d21af4c | 2013-01-09 13:29:53 -0700 | [diff] [blame] | 259 | unsigned int allocated = comedi_buf_write_n_allocated(async); |
| 260 | |
H Hartley Sweeten | 6166ce8 | 2013-01-10 10:37:56 -0700 | [diff] [blame] | 261 | if (nbytes > allocated) |
H Hartley Sweeten | d21af4c | 2013-01-09 13:29:53 -0700 | [diff] [blame] | 262 | nbytes = allocated; |
H Hartley Sweeten | 6166ce8 | 2013-01-10 10:37:56 -0700 | [diff] [blame] | 263 | |
H Hartley Sweeten | ea082fb | 2013-01-09 13:25:06 -0700 | [diff] [blame] | 264 | async->buf_write_count += nbytes; |
| 265 | async->buf_write_ptr += nbytes; |
| 266 | comedi_buf_munge(async, async->buf_write_count - async->munge_count); |
| 267 | if (async->buf_write_ptr >= async->prealloc_bufsz) |
| 268 | async->buf_write_ptr %= async->prealloc_bufsz; |
| 269 | |
| 270 | return nbytes; |
| 271 | } |
H Hartley Sweeten | 5660e74 | 2013-04-12 10:11:54 -0700 | [diff] [blame] | 272 | EXPORT_SYMBOL_GPL(comedi_buf_write_free); |
H Hartley Sweeten | ea082fb | 2013-01-09 13:25:06 -0700 | [diff] [blame] | 273 | |
| 274 | unsigned int comedi_buf_read_n_available(struct comedi_async *async) |
| 275 | { |
| 276 | unsigned num_bytes; |
| 277 | |
H Hartley Sweeten | 43f9137 | 2013-01-09 13:30:22 -0700 | [diff] [blame] | 278 | if (!async) |
H Hartley Sweeten | ea082fb | 2013-01-09 13:25:06 -0700 | [diff] [blame] | 279 | return 0; |
H Hartley Sweeten | 43f9137 | 2013-01-09 13:30:22 -0700 | [diff] [blame] | 280 | |
H Hartley Sweeten | ea082fb | 2013-01-09 13:25:06 -0700 | [diff] [blame] | 281 | num_bytes = async->munge_count - async->buf_read_count; |
H Hartley Sweeten | 43f9137 | 2013-01-09 13:30:22 -0700 | [diff] [blame] | 282 | |
| 283 | /* |
| 284 | * ensure the async buffer 'counts' are read before we |
| 285 | * attempt to read data from the buffer |
H Hartley Sweeten | ea082fb | 2013-01-09 13:25:06 -0700 | [diff] [blame] | 286 | */ |
| 287 | smp_rmb(); |
H Hartley Sweeten | 43f9137 | 2013-01-09 13:30:22 -0700 | [diff] [blame] | 288 | |
H Hartley Sweeten | ea082fb | 2013-01-09 13:25:06 -0700 | [diff] [blame] | 289 | return num_bytes; |
| 290 | } |
H Hartley Sweeten | 5660e74 | 2013-04-12 10:11:54 -0700 | [diff] [blame] | 291 | EXPORT_SYMBOL_GPL(comedi_buf_read_n_available); |
H Hartley Sweeten | ea082fb | 2013-01-09 13:25:06 -0700 | [diff] [blame] | 292 | |
| 293 | /* allocates a chunk for the reader from filled (and munged) buffer space */ |
H Hartley Sweeten | 8ae560a | 2013-01-09 13:32:56 -0700 | [diff] [blame] | 294 | unsigned int comedi_buf_read_alloc(struct comedi_async *async, |
| 295 | unsigned int nbytes) |
H Hartley Sweeten | ea082fb | 2013-01-09 13:25:06 -0700 | [diff] [blame] | 296 | { |
H Hartley Sweeten | 034cbd1 | 2013-01-09 13:30:49 -0700 | [diff] [blame] | 297 | unsigned int available; |
| 298 | |
| 299 | available = async->munge_count - async->buf_read_alloc_count; |
| 300 | if (nbytes > available) |
| 301 | nbytes = available; |
| 302 | |
H Hartley Sweeten | ea082fb | 2013-01-09 13:25:06 -0700 | [diff] [blame] | 303 | async->buf_read_alloc_count += nbytes; |
H Hartley Sweeten | 034cbd1 | 2013-01-09 13:30:49 -0700 | [diff] [blame] | 304 | |
| 305 | /* |
| 306 | * ensure the async buffer 'counts' are read before we |
| 307 | * attempt to read data from the read-alloc'ed buffer space |
| 308 | */ |
H Hartley Sweeten | ea082fb | 2013-01-09 13:25:06 -0700 | [diff] [blame] | 309 | smp_rmb(); |
H Hartley Sweeten | 034cbd1 | 2013-01-09 13:30:49 -0700 | [diff] [blame] | 310 | |
H Hartley Sweeten | ea082fb | 2013-01-09 13:25:06 -0700 | [diff] [blame] | 311 | return nbytes; |
| 312 | } |
H Hartley Sweeten | 5660e74 | 2013-04-12 10:11:54 -0700 | [diff] [blame] | 313 | EXPORT_SYMBOL_GPL(comedi_buf_read_alloc); |
H Hartley Sweeten | ea082fb | 2013-01-09 13:25:06 -0700 | [diff] [blame] | 314 | |
H Hartley Sweeten | 5b2b64b | 2013-01-09 13:31:46 -0700 | [diff] [blame] | 315 | static unsigned int comedi_buf_read_n_allocated(struct comedi_async *async) |
| 316 | { |
| 317 | return async->buf_read_alloc_count - async->buf_read_count; |
| 318 | } |
| 319 | |
H Hartley Sweeten | ea082fb | 2013-01-09 13:25:06 -0700 | [diff] [blame] | 320 | /* transfers control of a chunk from reader to free buffer space */ |
H Hartley Sweeten | 8ae560a | 2013-01-09 13:32:56 -0700 | [diff] [blame] | 321 | unsigned int comedi_buf_read_free(struct comedi_async *async, |
| 322 | unsigned int nbytes) |
H Hartley Sweeten | ea082fb | 2013-01-09 13:25:06 -0700 | [diff] [blame] | 323 | { |
H Hartley Sweeten | 3abfa10 | 2013-01-09 13:31:16 -0700 | [diff] [blame] | 324 | unsigned int allocated; |
| 325 | |
| 326 | /* |
| 327 | * ensure data has been read out of buffer before |
| 328 | * the async read count is incremented |
| 329 | */ |
H Hartley Sweeten | ea082fb | 2013-01-09 13:25:06 -0700 | [diff] [blame] | 330 | smp_mb(); |
H Hartley Sweeten | 3abfa10 | 2013-01-09 13:31:16 -0700 | [diff] [blame] | 331 | |
| 332 | allocated = comedi_buf_read_n_allocated(async); |
H Hartley Sweeten | 215040e | 2013-01-10 10:38:20 -0700 | [diff] [blame] | 333 | if (nbytes > allocated) |
H Hartley Sweeten | 3abfa10 | 2013-01-09 13:31:16 -0700 | [diff] [blame] | 334 | nbytes = allocated; |
H Hartley Sweeten | 215040e | 2013-01-10 10:38:20 -0700 | [diff] [blame] | 335 | |
H Hartley Sweeten | ea082fb | 2013-01-09 13:25:06 -0700 | [diff] [blame] | 336 | async->buf_read_count += nbytes; |
| 337 | async->buf_read_ptr += nbytes; |
| 338 | async->buf_read_ptr %= async->prealloc_bufsz; |
| 339 | return nbytes; |
| 340 | } |
H Hartley Sweeten | 5660e74 | 2013-04-12 10:11:54 -0700 | [diff] [blame] | 341 | EXPORT_SYMBOL_GPL(comedi_buf_read_free); |
H Hartley Sweeten | ea082fb | 2013-01-09 13:25:06 -0700 | [diff] [blame] | 342 | |
| 343 | int comedi_buf_put(struct comedi_async *async, short x) |
| 344 | { |
H Hartley Sweeten | 47181ea | 2013-01-09 13:28:20 -0700 | [diff] [blame] | 345 | unsigned int n = __comedi_buf_write_alloc(async, sizeof(short), 1); |
H Hartley Sweeten | ea082fb | 2013-01-09 13:25:06 -0700 | [diff] [blame] | 346 | |
| 347 | if (n < sizeof(short)) { |
| 348 | async->events |= COMEDI_CB_ERROR; |
| 349 | return 0; |
| 350 | } |
| 351 | *(short *)(async->prealloc_buf + async->buf_write_ptr) = x; |
| 352 | comedi_buf_write_free(async, sizeof(short)); |
| 353 | return 1; |
| 354 | } |
H Hartley Sweeten | 5660e74 | 2013-04-12 10:11:54 -0700 | [diff] [blame] | 355 | EXPORT_SYMBOL_GPL(comedi_buf_put); |
H Hartley Sweeten | ea082fb | 2013-01-09 13:25:06 -0700 | [diff] [blame] | 356 | |
| 357 | int comedi_buf_get(struct comedi_async *async, short *x) |
| 358 | { |
| 359 | unsigned int n = comedi_buf_read_n_available(async); |
| 360 | |
| 361 | if (n < sizeof(short)) |
| 362 | return 0; |
| 363 | comedi_buf_read_alloc(async, sizeof(short)); |
| 364 | *x = *(short *)(async->prealloc_buf + async->buf_read_ptr); |
| 365 | comedi_buf_read_free(async, sizeof(short)); |
| 366 | return 1; |
| 367 | } |
H Hartley Sweeten | 5660e74 | 2013-04-12 10:11:54 -0700 | [diff] [blame] | 368 | EXPORT_SYMBOL_GPL(comedi_buf_get); |
H Hartley Sweeten | ea082fb | 2013-01-09 13:25:06 -0700 | [diff] [blame] | 369 | |
| 370 | void comedi_buf_memcpy_to(struct comedi_async *async, unsigned int offset, |
| 371 | const void *data, unsigned int num_bytes) |
| 372 | { |
| 373 | unsigned int write_ptr = async->buf_write_ptr + offset; |
| 374 | |
| 375 | if (write_ptr >= async->prealloc_bufsz) |
| 376 | write_ptr %= async->prealloc_bufsz; |
| 377 | |
| 378 | while (num_bytes) { |
| 379 | unsigned int block_size; |
| 380 | |
| 381 | if (write_ptr + num_bytes > async->prealloc_bufsz) |
| 382 | block_size = async->prealloc_bufsz - write_ptr; |
| 383 | else |
| 384 | block_size = num_bytes; |
| 385 | |
| 386 | memcpy(async->prealloc_buf + write_ptr, data, block_size); |
| 387 | |
| 388 | data += block_size; |
| 389 | num_bytes -= block_size; |
| 390 | |
| 391 | write_ptr = 0; |
| 392 | } |
| 393 | } |
H Hartley Sweeten | 5660e74 | 2013-04-12 10:11:54 -0700 | [diff] [blame] | 394 | EXPORT_SYMBOL_GPL(comedi_buf_memcpy_to); |
H Hartley Sweeten | ea082fb | 2013-01-09 13:25:06 -0700 | [diff] [blame] | 395 | |
| 396 | void comedi_buf_memcpy_from(struct comedi_async *async, unsigned int offset, |
| 397 | void *dest, unsigned int nbytes) |
| 398 | { |
| 399 | void *src; |
| 400 | unsigned int read_ptr = async->buf_read_ptr + offset; |
| 401 | |
| 402 | if (read_ptr >= async->prealloc_bufsz) |
| 403 | read_ptr %= async->prealloc_bufsz; |
| 404 | |
| 405 | while (nbytes) { |
| 406 | unsigned int block_size; |
| 407 | |
| 408 | src = async->prealloc_buf + read_ptr; |
| 409 | |
| 410 | if (nbytes >= async->prealloc_bufsz - read_ptr) |
| 411 | block_size = async->prealloc_bufsz - read_ptr; |
| 412 | else |
| 413 | block_size = nbytes; |
| 414 | |
| 415 | memcpy(dest, src, block_size); |
| 416 | nbytes -= block_size; |
| 417 | dest += block_size; |
| 418 | read_ptr = 0; |
| 419 | } |
| 420 | } |
H Hartley Sweeten | 5660e74 | 2013-04-12 10:11:54 -0700 | [diff] [blame] | 421 | EXPORT_SYMBOL_GPL(comedi_buf_memcpy_from); |