blob: 6e13e450dc73979812fc8b50accbd19ef336f82f [file] [log] [blame]
David Schleefed9eccb2008-11-04 20:29:31 -08001/*
2 module/drivers.c
3 functions for manipulating drivers
4
5 COMEDI - Linux Control and Measurement Device Interface
6 Copyright (C) 1997-2000 David A. Schleef <ds@schleef.org>
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 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21
22*/
23
24#define _GNU_SOURCE
25
26#define __NO_VERSION__
27#include "comedi_fops.h"
28#include <linux/device.h>
29#include <linux/module.h>
30#include <linux/pci.h>
Bernd Porrc28264d2008-12-08 23:30:13 +000031#include <linux/usb.h>
David Schleefed9eccb2008-11-04 20:29:31 -080032#include <linux/errno.h>
33#include <linux/kernel.h>
34#include <linux/sched.h>
35#include <linux/fcntl.h>
36#include <linux/delay.h>
37#include <linux/ioport.h>
38#include <linux/mm.h>
39#include <linux/slab.h>
40#include "comedidev.h"
41#include "wrapper.h"
42#include <linux/highmem.h> /* for SuSE brokenness */
43#include <linux/vmalloc.h>
44#include <linux/cdev.h>
45#include <linux/dma-mapping.h>
46
47#include <asm/io.h>
48#include <asm/system.h>
49
Bill Pemberton71b5f4f2009-03-16 22:05:08 -040050static int postconfig(struct comedi_device *dev);
Bill Pemberton34c43922009-03-16 22:05:14 -040051static int insn_rw_emulate_bits(struct comedi_device *dev, struct comedi_subdevice *s,
Bill Pemberton90035c02009-03-16 22:05:53 -040052 struct comedi_insn *insn, unsigned int *data);
Bill Pemberton139dfbd2009-03-16 22:05:25 -040053static void *comedi_recognize(struct comedi_driver * driv, const char *name);
54static void comedi_report_boards(struct comedi_driver *driv);
Bill Pemberton34c43922009-03-16 22:05:14 -040055static int poll_invalid(struct comedi_device *dev, struct comedi_subdevice *s);
56int comedi_buf_alloc(struct comedi_device *dev, struct comedi_subdevice *s,
David Schleefed9eccb2008-11-04 20:29:31 -080057 unsigned long new_size);
58
Bill Pemberton139dfbd2009-03-16 22:05:25 -040059struct comedi_driver *comedi_drivers;
David Schleefed9eccb2008-11-04 20:29:31 -080060
61int comedi_modprobe(int minor)
62{
63 return -EINVAL;
64}
65
Bill Pemberton71b5f4f2009-03-16 22:05:08 -040066static void cleanup_device(struct comedi_device *dev)
David Schleefed9eccb2008-11-04 20:29:31 -080067{
68 int i;
Bill Pemberton34c43922009-03-16 22:05:14 -040069 struct comedi_subdevice *s;
David Schleefed9eccb2008-11-04 20:29:31 -080070
71 if (dev->subdevices) {
72 for (i = 0; i < dev->n_subdevices; i++) {
73 s = dev->subdevices + i;
74 comedi_free_subdevice_minor(s);
75 if (s->async) {
76 comedi_buf_alloc(dev, s, 0);
77 kfree(s->async);
78 }
79 }
80 kfree(dev->subdevices);
81 dev->subdevices = NULL;
82 dev->n_subdevices = 0;
83 }
Bill Pembertondedd1322009-03-16 22:04:18 -040084 kfree(dev->private);
85 dev->private = NULL;
David Schleefed9eccb2008-11-04 20:29:31 -080086 dev->driver = 0;
87 dev->board_name = NULL;
88 dev->board_ptr = NULL;
89 dev->iobase = 0;
90 dev->irq = 0;
91 dev->read_subdev = NULL;
92 dev->write_subdev = NULL;
93 dev->open = NULL;
94 dev->close = NULL;
95 comedi_set_hw_dev(dev, NULL);
96}
97
Bill Pemberton71b5f4f2009-03-16 22:05:08 -040098static void __comedi_device_detach(struct comedi_device *dev)
David Schleefed9eccb2008-11-04 20:29:31 -080099{
100 dev->attached = 0;
101 if (dev->driver) {
102 dev->driver->detach(dev);
103 } else {
104 printk("BUG: dev->driver=NULL in comedi_device_detach()\n");
105 }
106 cleanup_device(dev);
107}
108
Bill Pemberton71b5f4f2009-03-16 22:05:08 -0400109void comedi_device_detach(struct comedi_device *dev)
David Schleefed9eccb2008-11-04 20:29:31 -0800110{
111 if (!dev->attached)
112 return;
113 __comedi_device_detach(dev);
114}
115
Bill Pemberton0707bb02009-03-16 22:06:20 -0400116int comedi_device_attach(struct comedi_device *dev, struct comedi_devconfig *it)
David Schleefed9eccb2008-11-04 20:29:31 -0800117{
Bill Pemberton139dfbd2009-03-16 22:05:25 -0400118 struct comedi_driver *driv;
David Schleefed9eccb2008-11-04 20:29:31 -0800119 int ret;
120
121 if (dev->attached)
122 return -EBUSY;
123
124 for (driv = comedi_drivers; driv; driv = driv->next) {
125 if (!try_module_get(driv->module)) {
126 printk("comedi: failed to increment module count, skipping\n");
127 continue;
128 }
129 if (driv->num_names) {
130 dev->board_ptr = comedi_recognize(driv, it->board_name);
131 if (dev->board_ptr == NULL) {
132 module_put(driv->module);
133 continue;
134 }
135 } else {
136 if (strcmp(driv->driver_name, it->board_name)) {
137 module_put(driv->module);
138 continue;
139 }
140 }
Bill Pembertonb6c77752009-03-16 22:03:24 -0400141 /* initialize dev->driver here so comedi_error() can be called from attach */
David Schleefed9eccb2008-11-04 20:29:31 -0800142 dev->driver = driv;
143 ret = driv->attach(dev, it);
144 if (ret < 0) {
145 module_put(dev->driver->module);
146 __comedi_device_detach(dev);
147 return ret;
148 }
149 goto attached;
150 }
151
Bill Pembertonb6c77752009-03-16 22:03:24 -0400152 /* recognize has failed if we get here */
153 /* report valid board names before returning error */
David Schleefed9eccb2008-11-04 20:29:31 -0800154 for (driv = comedi_drivers; driv; driv = driv->next) {
155 if (!try_module_get(driv->module)) {
156 printk("comedi: failed to increment module count\n");
157 continue;
158 }
159 comedi_report_boards(driv);
160 module_put(driv->module);
161 }
162 return -EIO;
163
164attached:
165 /* do a little post-config cleanup */
166 ret = postconfig(dev);
167 module_put(dev->driver->module);
168 if (ret < 0) {
169 __comedi_device_detach(dev);
170 return ret;
171 }
172
173 if (!dev->board_name) {
174 printk("BUG: dev->board_name=<%p>\n", dev->board_name);
175 dev->board_name = "BUG";
176 }
177 smp_wmb();
178 dev->attached = 1;
179
180 return 0;
181}
182
Bill Pemberton139dfbd2009-03-16 22:05:25 -0400183int comedi_driver_register(struct comedi_driver *driver)
David Schleefed9eccb2008-11-04 20:29:31 -0800184{
185 driver->next = comedi_drivers;
186 comedi_drivers = driver;
187
188 return 0;
189}
190
Bill Pemberton139dfbd2009-03-16 22:05:25 -0400191int comedi_driver_unregister(struct comedi_driver *driver)
David Schleefed9eccb2008-11-04 20:29:31 -0800192{
Bill Pemberton139dfbd2009-03-16 22:05:25 -0400193 struct comedi_driver *prev;
David Schleefed9eccb2008-11-04 20:29:31 -0800194 int i;
195
196 /* check for devices using this driver */
197 for (i = 0; i < COMEDI_NUM_BOARD_MINORS; i++) {
198 struct comedi_device_file_info *dev_file_info = comedi_get_device_file_info(i);
Bill Pemberton71b5f4f2009-03-16 22:05:08 -0400199 struct comedi_device *dev;
David Schleefed9eccb2008-11-04 20:29:31 -0800200
201 if(dev_file_info == NULL) continue;
202 dev = dev_file_info->device;
203
204 mutex_lock(&dev->mutex);
205 if (dev->attached && dev->driver == driver) {
206 if (dev->use_count)
207 printk("BUG! detaching device with use_count=%d\n", dev->use_count);
208 comedi_device_detach(dev);
209 }
210 mutex_unlock(&dev->mutex);
211 }
212
213 if (comedi_drivers == driver) {
214 comedi_drivers = driver->next;
215 return 0;
216 }
217
218 for (prev = comedi_drivers; prev->next; prev = prev->next) {
219 if (prev->next == driver) {
220 prev->next = driver->next;
221 return 0;
222 }
223 }
224 return -EINVAL;
225}
226
Bill Pemberton71b5f4f2009-03-16 22:05:08 -0400227static int postconfig(struct comedi_device *dev)
David Schleefed9eccb2008-11-04 20:29:31 -0800228{
229 int i;
Bill Pemberton34c43922009-03-16 22:05:14 -0400230 struct comedi_subdevice *s;
Bill Pembertond1636792009-03-16 22:05:20 -0400231 struct comedi_async *async = NULL;
David Schleefed9eccb2008-11-04 20:29:31 -0800232 int ret;
233
234 for (i = 0; i < dev->n_subdevices; i++) {
235 s = dev->subdevices + i;
236
237 if (s->type == COMEDI_SUBD_UNUSED)
238 continue;
239
240 if (s->len_chanlist == 0)
241 s->len_chanlist = 1;
242
243 if (s->do_cmd) {
244 BUG_ON((s->subdev_flags & (SDF_CMD_READ |
245 SDF_CMD_WRITE)) == 0);
246 BUG_ON(!s->do_cmdtest);
247
Bill Pembertond1636792009-03-16 22:05:20 -0400248 async = kzalloc(sizeof(struct comedi_async), GFP_KERNEL);
David Schleefed9eccb2008-11-04 20:29:31 -0800249 if (async == NULL) {
250 printk("failed to allocate async struct\n");
251 return -ENOMEM;
252 }
253 init_waitqueue_head(&async->wait_head);
254 async->subdevice = s;
255 s->async = async;
256
257#define DEFAULT_BUF_MAXSIZE (64*1024)
258#define DEFAULT_BUF_SIZE (64*1024)
259
260 async->max_bufsize = DEFAULT_BUF_MAXSIZE;
261
262 async->prealloc_buf = NULL;
263 async->prealloc_bufsz = 0;
264 if (comedi_buf_alloc(dev, s, DEFAULT_BUF_SIZE) < 0) {
265 printk("Buffer allocation failed\n");
266 return -ENOMEM;
267 }
268 if (s->buf_change) {
269 ret = s->buf_change(dev, s, DEFAULT_BUF_SIZE);
270 if (ret < 0)
271 return ret;
272 }
273 comedi_alloc_subdevice_minor(dev, s);
274 }
275
276 if (!s->range_table && !s->range_table_list)
277 s->range_table = &range_unknown;
278
279 if (!s->insn_read && s->insn_bits)
280 s->insn_read = insn_rw_emulate_bits;
281 if (!s->insn_write && s->insn_bits)
282 s->insn_write = insn_rw_emulate_bits;
283
284 if (!s->insn_read)
285 s->insn_read = insn_inval;
286 if (!s->insn_write)
287 s->insn_write = insn_inval;
288 if (!s->insn_bits)
289 s->insn_bits = insn_inval;
290 if (!s->insn_config)
291 s->insn_config = insn_inval;
292
293 if (!s->poll)
294 s->poll = poll_invalid;
295 }
296
297 return 0;
298}
299
Bill Pembertonb6c77752009-03-16 22:03:24 -0400300/* generic recognize function for drivers that register their supported board names */
Bill Pemberton139dfbd2009-03-16 22:05:25 -0400301void *comedi_recognize(struct comedi_driver * driv, const char *name)
David Schleefed9eccb2008-11-04 20:29:31 -0800302{
303 unsigned i;
304 const char *const *name_ptr = driv->board_name;
305 for (i = 0; i < driv->num_names; i++) {
306 if (strcmp(*name_ptr, name) == 0)
307 return (void *)name_ptr;
308 name_ptr =
309 (const char *const *)((const char *)name_ptr +
310 driv->offset);
311 }
312
313 return NULL;
314}
315
Bill Pemberton139dfbd2009-03-16 22:05:25 -0400316void comedi_report_boards(struct comedi_driver *driv)
David Schleefed9eccb2008-11-04 20:29:31 -0800317{
318 unsigned int i;
319 const char *const *name_ptr;
320
321 printk("comedi: valid board names for %s driver are:\n",
322 driv->driver_name);
323
324 name_ptr = driv->board_name;
325 for (i = 0; i < driv->num_names; i++) {
326 printk(" %s\n", *name_ptr);
327 name_ptr = (const char **)((char *)name_ptr + driv->offset);
328 }
329
330 if (driv->num_names == 0)
331 printk(" %s\n", driv->driver_name);
332}
333
Bill Pemberton34c43922009-03-16 22:05:14 -0400334static int poll_invalid(struct comedi_device *dev, struct comedi_subdevice *s)
David Schleefed9eccb2008-11-04 20:29:31 -0800335{
336 return -EINVAL;
337}
338
Bill Pemberton34c43922009-03-16 22:05:14 -0400339int insn_inval(struct comedi_device *dev, struct comedi_subdevice *s,
Bill Pemberton90035c02009-03-16 22:05:53 -0400340 struct comedi_insn *insn, unsigned int *data)
David Schleefed9eccb2008-11-04 20:29:31 -0800341{
342 return -EINVAL;
343}
344
Bill Pemberton34c43922009-03-16 22:05:14 -0400345static int insn_rw_emulate_bits(struct comedi_device *dev, struct comedi_subdevice *s,
Bill Pemberton90035c02009-03-16 22:05:53 -0400346 struct comedi_insn *insn, unsigned int *data)
David Schleefed9eccb2008-11-04 20:29:31 -0800347{
Bill Pemberton90035c02009-03-16 22:05:53 -0400348 struct comedi_insn new_insn;
David Schleefed9eccb2008-11-04 20:29:31 -0800349 int ret;
350 static const unsigned channels_per_bitfield = 32;
351
352 unsigned chan = CR_CHAN(insn->chanspec);
353 const unsigned base_bitfield_channel =
354 (chan < channels_per_bitfield) ? 0 : chan;
Bill Pemberton790c5542009-03-16 22:05:02 -0400355 unsigned int new_data[2];
David Schleefed9eccb2008-11-04 20:29:31 -0800356 memset(new_data, 0, sizeof(new_data));
357 memset(&new_insn, 0, sizeof(new_insn));
358 new_insn.insn = INSN_BITS;
359 new_insn.chanspec = base_bitfield_channel;
360 new_insn.n = 2;
361 new_insn.data = new_data;
362 new_insn.subdev = insn->subdev;
363
364 if (insn->insn == INSN_WRITE) {
365 if (!(s->subdev_flags & SDF_WRITABLE))
366 return -EINVAL;
367 new_data[0] = 1 << (chan - base_bitfield_channel); /* mask */
368 new_data[1] = data[0] ? (1 << (chan - base_bitfield_channel)) : 0; /* bits */
369 }
370
371 ret = s->insn_bits(dev, s, &new_insn, new_data);
372 if (ret < 0)
373 return ret;
374
375 if (insn->insn == INSN_READ) {
376 data[0] = (new_data[1] >> (chan - base_bitfield_channel)) & 1;
377 }
378
379 return 1;
380}
381
Bill Pembertone473e912009-03-16 22:03:29 -0400382static inline unsigned long uvirt_to_kva(pgd_t *pgd, unsigned long adr)
David Schleefed9eccb2008-11-04 20:29:31 -0800383{
384 unsigned long ret = 0UL;
385 pmd_t *pmd;
386 pte_t *ptep, pte;
387 pud_t *pud;
388
389 if (!pgd_none(*pgd)) {
390 pud = pud_offset(pgd, adr);
391 pmd = pmd_offset(pud, adr);
392 if (!pmd_none(*pmd)) {
393 ptep = pte_offset_kernel(pmd, adr);
394 pte = *ptep;
395 if (pte_present(pte)) {
396 ret = (unsigned long)
397 page_address(pte_page(pte));
398 ret |= (adr & (PAGE_SIZE - 1));
399 }
400 }
401 }
402 return ret;
403}
404
405static inline unsigned long kvirt_to_kva(unsigned long adr)
406{
407 unsigned long va, kva;
408
409 va = adr;
410 kva = uvirt_to_kva(pgd_offset_k(va), va);
411
412 return kva;
413}
414
Bill Pemberton34c43922009-03-16 22:05:14 -0400415int comedi_buf_alloc(struct comedi_device *dev, struct comedi_subdevice *s,
David Schleefed9eccb2008-11-04 20:29:31 -0800416 unsigned long new_size)
417{
Bill Pembertond1636792009-03-16 22:05:20 -0400418 struct comedi_async *async = s->async;
David Schleefed9eccb2008-11-04 20:29:31 -0800419
420 /* Round up new_size to multiple of PAGE_SIZE */
421 new_size = (new_size + PAGE_SIZE - 1) & PAGE_MASK;
422
423 /* if no change is required, do nothing */
424 if (async->prealloc_buf && async->prealloc_bufsz == new_size) {
425 return 0;
426 }
Bill Pembertonb6c77752009-03-16 22:03:24 -0400427 /* deallocate old buffer */
David Schleefed9eccb2008-11-04 20:29:31 -0800428 if (async->prealloc_buf) {
429 vunmap(async->prealloc_buf);
430 async->prealloc_buf = NULL;
431 async->prealloc_bufsz = 0;
432 }
433 if (async->buf_page_list) {
434 unsigned i;
435 for (i = 0; i < async->n_buf_pages; ++i) {
436 if (async->buf_page_list[i].virt_addr) {
437 mem_map_unreserve(virt_to_page(async->
438 buf_page_list[i].virt_addr));
439 if (s->async_dma_dir != DMA_NONE) {
440 dma_free_coherent(dev->hw_dev,
441 PAGE_SIZE,
442 async->buf_page_list[i].
443 virt_addr,
444 async->buf_page_list[i].
445 dma_addr);
446 } else {
447 free_page((unsigned long)async->
448 buf_page_list[i].virt_addr);
449 }
450 }
451 }
452 vfree(async->buf_page_list);
453 async->buf_page_list = NULL;
454 async->n_buf_pages = 0;
455 }
Bill Pembertonb6c77752009-03-16 22:03:24 -0400456 /* allocate new buffer */
David Schleefed9eccb2008-11-04 20:29:31 -0800457 if (new_size) {
458 unsigned i = 0;
459 unsigned n_pages = new_size >> PAGE_SHIFT;
460 struct page **pages = NULL;
461
462 async->buf_page_list =
463 vmalloc(sizeof(struct comedi_buf_page) * n_pages);
464 if (async->buf_page_list) {
465 memset(async->buf_page_list, 0,
466 sizeof(struct comedi_buf_page) * n_pages);
467 pages = vmalloc(sizeof(struct page *) * n_pages);
468 }
469 if (pages) {
470 for (i = 0; i < n_pages; i++) {
471 if (s->async_dma_dir != DMA_NONE) {
472 async->buf_page_list[i].virt_addr =
473 dma_alloc_coherent(dev->hw_dev,
474 PAGE_SIZE,
475 &async->buf_page_list[i].
476 dma_addr,
477 GFP_KERNEL | __GFP_COMP);
478 } else {
479 async->buf_page_list[i].virt_addr =
480 (void *)
481 get_zeroed_page(GFP_KERNEL);
482 }
483 if (async->buf_page_list[i].virt_addr == NULL) {
484 break;
485 }
486 mem_map_reserve(virt_to_page(async->
487 buf_page_list[i].virt_addr));
488 pages[i] =
489 virt_to_page(async->buf_page_list[i].
490 virt_addr);
491 }
492 }
493 if (i == n_pages) {
494 async->prealloc_buf =
495 vmap(pages, n_pages, VM_MAP,
496 PAGE_KERNEL_NOCACHE);
497 }
498 if (pages) {
499 vfree(pages);
500 }
501 if (async->prealloc_buf == NULL) {
502 /* Some allocation failed above. */
503 if (async->buf_page_list) {
504 for (i = 0; i < n_pages; i++) {
505 if (async->buf_page_list[i].virt_addr ==
506 NULL) {
507 break;
508 }
509 mem_map_unreserve(virt_to_page(async->
510 buf_page_list[i].
511 virt_addr));
512 if (s->async_dma_dir != DMA_NONE) {
513 dma_free_coherent(dev->hw_dev,
514 PAGE_SIZE,
515 async->buf_page_list[i].
516 virt_addr,
517 async->buf_page_list[i].
518 dma_addr);
519 } else {
520 free_page((unsigned long)async->
521 buf_page_list[i].
522 virt_addr);
523 }
524 }
525 vfree(async->buf_page_list);
526 async->buf_page_list = NULL;
527 }
528 return -ENOMEM;
529 }
530 async->n_buf_pages = n_pages;
531 }
532 async->prealloc_bufsz = new_size;
533
534 return 0;
535}
536
537/* munging is applied to data by core as it passes between user
538 * and kernel space */
Bill Pembertond1636792009-03-16 22:05:20 -0400539unsigned int comedi_buf_munge(struct comedi_async *async, unsigned int num_bytes)
David Schleefed9eccb2008-11-04 20:29:31 -0800540{
Bill Pemberton34c43922009-03-16 22:05:14 -0400541 struct comedi_subdevice *s = async->subdevice;
David Schleefed9eccb2008-11-04 20:29:31 -0800542 unsigned int count = 0;
543 const unsigned num_sample_bytes = bytes_per_sample(s);
544
545 if (s->munge == NULL || (async->cmd.flags & CMDF_RAWDATA)) {
546 async->munge_count += num_bytes;
Stoyan Gaydarov2961f242009-03-10 00:10:27 -0500547 BUG_ON((int)(async->munge_count - async->buf_write_count) > 0);
David Schleefed9eccb2008-11-04 20:29:31 -0800548 return num_bytes;
549 }
550 /* don't munge partial samples */
551 num_bytes -= num_bytes % num_sample_bytes;
552 while (count < num_bytes) {
553 int block_size;
554
555 block_size = num_bytes - count;
556 if (block_size < 0) {
557 rt_printk("%s: %s: bug! block_size is negative\n",
Harvey Harrisond599edc2009-01-07 14:31:57 -0800558 __FILE__, __func__);
David Schleefed9eccb2008-11-04 20:29:31 -0800559 break;
560 }
561 if ((int)(async->munge_ptr + block_size -
562 async->prealloc_bufsz) > 0)
563 block_size = async->prealloc_bufsz - async->munge_ptr;
564
565 s->munge(s->device, s, async->prealloc_buf + async->munge_ptr,
566 block_size, async->munge_chan);
567
Bill Pembertonb6c77752009-03-16 22:03:24 -0400568 smp_wmb(); /* barrier insures data is munged in buffer before munge_count is incremented */
David Schleefed9eccb2008-11-04 20:29:31 -0800569
570 async->munge_chan += block_size / num_sample_bytes;
571 async->munge_chan %= async->cmd.chanlist_len;
572 async->munge_count += block_size;
573 async->munge_ptr += block_size;
574 async->munge_ptr %= async->prealloc_bufsz;
575 count += block_size;
576 }
Stoyan Gaydarov2961f242009-03-10 00:10:27 -0500577 BUG_ON((int)(async->munge_count - async->buf_write_count) > 0);
David Schleefed9eccb2008-11-04 20:29:31 -0800578 return count;
579}
580
Bill Pembertond1636792009-03-16 22:05:20 -0400581unsigned int comedi_buf_write_n_available(struct comedi_async *async)
David Schleefed9eccb2008-11-04 20:29:31 -0800582{
583 unsigned int free_end;
584 unsigned int nbytes;
585
586 if (async == NULL)
587 return 0;
588
589 free_end = async->buf_read_count + async->prealloc_bufsz;
590 nbytes = free_end - async->buf_write_alloc_count;
591 nbytes -= nbytes % bytes_per_sample(async->subdevice);
592 /* barrier insures the read of buf_read_count in this
593 query occurs before any following writes to the buffer which
594 might be based on the return value from this query.
595 */
596 smp_mb();
597 return nbytes;
598}
599
600/* allocates chunk for the writer from free buffer space */
Bill Pembertond1636792009-03-16 22:05:20 -0400601unsigned int comedi_buf_write_alloc(struct comedi_async *async, unsigned int nbytes)
David Schleefed9eccb2008-11-04 20:29:31 -0800602{
603 unsigned int free_end = async->buf_read_count + async->prealloc_bufsz;
604
605 if ((int)(async->buf_write_alloc_count + nbytes - free_end) > 0) {
606 nbytes = free_end - async->buf_write_alloc_count;
607 }
608 async->buf_write_alloc_count += nbytes;
609 /* barrier insures the read of buf_read_count above occurs before
610 we write data to the write-alloc'ed buffer space */
611 smp_mb();
612 return nbytes;
613}
614
615/* allocates nothing unless it can completely fulfill the request */
Bill Pembertond1636792009-03-16 22:05:20 -0400616unsigned int comedi_buf_write_alloc_strict(struct comedi_async *async,
David Schleefed9eccb2008-11-04 20:29:31 -0800617 unsigned int nbytes)
618{
619 unsigned int free_end = async->buf_read_count + async->prealloc_bufsz;
620
621 if ((int)(async->buf_write_alloc_count + nbytes - free_end) > 0) {
622 nbytes = 0;
623 }
624 async->buf_write_alloc_count += nbytes;
625 /* barrier insures the read of buf_read_count above occurs before
626 we write data to the write-alloc'ed buffer space */
627 smp_mb();
628 return nbytes;
629}
630
631/* transfers a chunk from writer to filled buffer space */
Bill Pembertond1636792009-03-16 22:05:20 -0400632unsigned comedi_buf_write_free(struct comedi_async *async, unsigned int nbytes)
David Schleefed9eccb2008-11-04 20:29:31 -0800633{
634 if ((int)(async->buf_write_count + nbytes -
635 async->buf_write_alloc_count) > 0) {
636 rt_printk
637 ("comedi: attempted to write-free more bytes than have been write-allocated.\n");
638 nbytes = async->buf_write_alloc_count - async->buf_write_count;
639 }
640 async->buf_write_count += nbytes;
641 async->buf_write_ptr += nbytes;
642 comedi_buf_munge(async, async->buf_write_count - async->munge_count);
643 if (async->buf_write_ptr >= async->prealloc_bufsz) {
644 async->buf_write_ptr %= async->prealloc_bufsz;
645 }
646 return nbytes;
647}
648
649/* allocates a chunk for the reader from filled (and munged) buffer space */
Bill Pembertond1636792009-03-16 22:05:20 -0400650unsigned comedi_buf_read_alloc(struct comedi_async *async, unsigned nbytes)
David Schleefed9eccb2008-11-04 20:29:31 -0800651{
652 if ((int)(async->buf_read_alloc_count + nbytes - async->munge_count) >
653 0) {
654 nbytes = async->munge_count - async->buf_read_alloc_count;
655 }
656 async->buf_read_alloc_count += nbytes;
657 /* barrier insures read of munge_count occurs before we actually read
658 data out of buffer */
659 smp_rmb();
660 return nbytes;
661}
662
663/* transfers control of a chunk from reader to free buffer space */
Bill Pembertond1636792009-03-16 22:05:20 -0400664unsigned comedi_buf_read_free(struct comedi_async *async, unsigned int nbytes)
David Schleefed9eccb2008-11-04 20:29:31 -0800665{
Bill Pembertonb6c77752009-03-16 22:03:24 -0400666 /* barrier insures data has been read out of buffer before read count is incremented */
David Schleefed9eccb2008-11-04 20:29:31 -0800667 smp_mb();
668 if ((int)(async->buf_read_count + nbytes -
669 async->buf_read_alloc_count) > 0) {
670 rt_printk
671 ("comedi: attempted to read-free more bytes than have been read-allocated.\n");
672 nbytes = async->buf_read_alloc_count - async->buf_read_count;
673 }
674 async->buf_read_count += nbytes;
675 async->buf_read_ptr += nbytes;
676 async->buf_read_ptr %= async->prealloc_bufsz;
677 return nbytes;
678}
679
Bill Pembertond1636792009-03-16 22:05:20 -0400680void comedi_buf_memcpy_to(struct comedi_async *async, unsigned int offset,
David Schleefed9eccb2008-11-04 20:29:31 -0800681 const void *data, unsigned int num_bytes)
682{
683 unsigned int write_ptr = async->buf_write_ptr + offset;
684
685 if (write_ptr >= async->prealloc_bufsz)
686 write_ptr %= async->prealloc_bufsz;
687
688 while (num_bytes) {
689 unsigned int block_size;
690
691 if (write_ptr + num_bytes > async->prealloc_bufsz)
692 block_size = async->prealloc_bufsz - write_ptr;
693 else
694 block_size = num_bytes;
695
696 memcpy(async->prealloc_buf + write_ptr, data, block_size);
697
698 data += block_size;
699 num_bytes -= block_size;
700
701 write_ptr = 0;
702 }
703}
704
Bill Pembertond1636792009-03-16 22:05:20 -0400705void comedi_buf_memcpy_from(struct comedi_async *async, unsigned int offset,
David Schleefed9eccb2008-11-04 20:29:31 -0800706 void *dest, unsigned int nbytes)
707{
708 void *src;
709 unsigned int read_ptr = async->buf_read_ptr + offset;
710
711 if (read_ptr >= async->prealloc_bufsz)
712 read_ptr %= async->prealloc_bufsz;
713
714 while (nbytes) {
715 unsigned int block_size;
716
717 src = async->prealloc_buf + read_ptr;
718
719 if (nbytes >= async->prealloc_bufsz - read_ptr)
720 block_size = async->prealloc_bufsz - read_ptr;
721 else
722 block_size = nbytes;
723
724 memcpy(dest, src, block_size);
725 nbytes -= block_size;
726 dest += block_size;
727 read_ptr = 0;
728 }
729}
730
Bill Pembertond1636792009-03-16 22:05:20 -0400731unsigned int comedi_buf_read_n_available(struct comedi_async *async)
David Schleefed9eccb2008-11-04 20:29:31 -0800732{
733 unsigned num_bytes;
734
735 if (async == NULL)
736 return 0;
737 num_bytes = async->munge_count - async->buf_read_count;
738 /* barrier insures the read of munge_count in this
739 query occurs before any following reads of the buffer which
740 might be based on the return value from this query.
741 */
742 smp_rmb();
743 return num_bytes;
744}
745
Bill Pembertond1636792009-03-16 22:05:20 -0400746int comedi_buf_get(struct comedi_async *async, short *x)
David Schleefed9eccb2008-11-04 20:29:31 -0800747{
748 unsigned int n = comedi_buf_read_n_available(async);
749
Bill Pemberton790c5542009-03-16 22:05:02 -0400750 if (n < sizeof(short))
David Schleefed9eccb2008-11-04 20:29:31 -0800751 return 0;
Bill Pemberton790c5542009-03-16 22:05:02 -0400752 comedi_buf_read_alloc(async, sizeof(short));
753 *x = *(short *) (async->prealloc_buf + async->buf_read_ptr);
754 comedi_buf_read_free(async, sizeof(short));
David Schleefed9eccb2008-11-04 20:29:31 -0800755 return 1;
756}
757
Bill Pembertond1636792009-03-16 22:05:20 -0400758int comedi_buf_put(struct comedi_async *async, short x)
David Schleefed9eccb2008-11-04 20:29:31 -0800759{
Bill Pemberton790c5542009-03-16 22:05:02 -0400760 unsigned int n = comedi_buf_write_alloc_strict(async, sizeof(short));
David Schleefed9eccb2008-11-04 20:29:31 -0800761
Bill Pemberton790c5542009-03-16 22:05:02 -0400762 if (n < sizeof(short)) {
David Schleefed9eccb2008-11-04 20:29:31 -0800763 async->events |= COMEDI_CB_ERROR;
764 return 0;
765 }
Bill Pemberton790c5542009-03-16 22:05:02 -0400766 *(short *) (async->prealloc_buf + async->buf_write_ptr) = x;
767 comedi_buf_write_free(async, sizeof(short));
David Schleefed9eccb2008-11-04 20:29:31 -0800768 return 1;
769}
770
Bill Pembertond1636792009-03-16 22:05:20 -0400771void comedi_reset_async_buf(struct comedi_async *async)
David Schleefed9eccb2008-11-04 20:29:31 -0800772{
773 async->buf_write_alloc_count = 0;
774 async->buf_write_count = 0;
775 async->buf_read_alloc_count = 0;
776 async->buf_read_count = 0;
777
778 async->buf_write_ptr = 0;
779 async->buf_read_ptr = 0;
780
781 async->cur_chan = 0;
782 async->scan_progress = 0;
783 async->munge_chan = 0;
784 async->munge_count = 0;
785 async->munge_ptr = 0;
786
787 async->events = 0;
788}
789
790int comedi_auto_config(struct device *hardware_device, const char *board_name, const int *options, unsigned num_options)
791{
Bill Pemberton0707bb02009-03-16 22:06:20 -0400792 struct comedi_devconfig it;
David Schleefed9eccb2008-11-04 20:29:31 -0800793 int minor;
794 struct comedi_device_file_info *dev_file_info;
795 int retval;
Bernd Porrc28264d2008-12-08 23:30:13 +0000796 unsigned *private_data = NULL;
David Schleefed9eccb2008-11-04 20:29:31 -0800797
Ian Abbott719548e2008-12-09 11:07:22 +0000798 if (!comedi_autoconfig) {
799 dev_set_drvdata(hardware_device, NULL);
800 return 0;
801 }
Ian Abbott6a9d7a22008-12-08 17:05:50 +0000802
David Schleefed9eccb2008-11-04 20:29:31 -0800803 minor = comedi_alloc_board_minor(hardware_device);
804 if(minor < 0) return minor;
Bernd Porrc28264d2008-12-08 23:30:13 +0000805
806 private_data = kmalloc(sizeof(unsigned), GFP_KERNEL);
807 if (private_data == NULL) {
808 retval = -ENOMEM;
809 goto cleanup;
810 }
811 *private_data = minor;
812 dev_set_drvdata(hardware_device, private_data);
David Schleefed9eccb2008-11-04 20:29:31 -0800813
814 dev_file_info = comedi_get_device_file_info(minor);
815
816 memset(&it, 0, sizeof(it));
817 strncpy(it.board_name, board_name, COMEDI_NAMELEN);
818 it.board_name[COMEDI_NAMELEN - 1] = '\0';
819 BUG_ON(num_options > COMEDI_NDEVCONFOPTS);
820 memcpy(it.options, options, num_options * sizeof(int));
821
822 mutex_lock(&dev_file_info->device->mutex);
823 retval = comedi_device_attach(dev_file_info->device, &it);
824 mutex_unlock(&dev_file_info->device->mutex);
Bernd Porrc28264d2008-12-08 23:30:13 +0000825
826cleanup:
David Schleefed9eccb2008-11-04 20:29:31 -0800827 if(retval < 0)
828 {
Bernd Porrc28264d2008-12-08 23:30:13 +0000829 kfree(private_data);
David Schleefed9eccb2008-11-04 20:29:31 -0800830 comedi_free_board_minor(minor);
831 }
832 return retval;
833}
834
835void comedi_auto_unconfig(struct device *hardware_device)
836{
Bernd Porrc28264d2008-12-08 23:30:13 +0000837 unsigned *minor = (unsigned *)dev_get_drvdata(hardware_device);
838 if(minor == NULL) return;
David Schleefed9eccb2008-11-04 20:29:31 -0800839
Bernd Porrc28264d2008-12-08 23:30:13 +0000840 BUG_ON(*minor >= COMEDI_NUM_BOARD_MINORS);
David Schleefed9eccb2008-11-04 20:29:31 -0800841
Bernd Porrc28264d2008-12-08 23:30:13 +0000842 comedi_free_board_minor(*minor);
843 dev_set_drvdata(hardware_device, NULL);
844 kfree(minor);
David Schleefed9eccb2008-11-04 20:29:31 -0800845}
846
847int comedi_pci_auto_config(struct pci_dev *pcidev, const char *board_name)
848{
849 int options[2];
850
Bill Pembertonb6c77752009-03-16 22:03:24 -0400851 /* pci bus */
David Schleefed9eccb2008-11-04 20:29:31 -0800852 options[0] = pcidev->bus->number;
Bill Pembertonb6c77752009-03-16 22:03:24 -0400853 /* pci slot */
David Schleefed9eccb2008-11-04 20:29:31 -0800854 options[1] = PCI_SLOT(pcidev->devfn);
855
856 return comedi_auto_config(&pcidev->dev, board_name, options, sizeof(options) / sizeof(options[0]));
857}
858
859void comedi_pci_auto_unconfig(struct pci_dev *pcidev)
860{
861 comedi_auto_unconfig(&pcidev->dev);
862}
Bernd Porrc28264d2008-12-08 23:30:13 +0000863
864int comedi_usb_auto_config(struct usb_device *usbdev,
865 const char *board_name)
866{
867 BUG_ON(usbdev == NULL);
868 return comedi_auto_config(&usbdev->dev, board_name, NULL, 0);
869}
870
871void comedi_usb_auto_unconfig(struct usb_device *usbdev)
872{
873 BUG_ON(usbdev == NULL);
874 comedi_auto_unconfig(&usbdev->dev);
875}