Use 'bool' type for 'bool' module parameters, if supported.
[comedi.git] / comedi / drivers.c
1 /*
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 <linux/device.h>
28 #include <linux/module.h>
29 #include <linux/pci.h>
30 #include <linux/usb.h>
31 #include <linux/errno.h>
32 #include <linux/kernel.h>
33 #include <linux/sched.h>
34 #include <linux/fcntl.h>
35 #include <linux/delay.h>
36 #include <linux/ioport.h>
37 #include <linux/mm.h>
38 #include <linux/slab.h>
39 #include <linux/comedidev.h>
40 #include <linux/wrapper.h>
41 #include <linux/highmem.h>      /* for SuSE brokenness */
42 #include <linux/vmalloc.h>
43 #include <linux/cdev.h>
44 #include <linux/dma-mapping.h>
45
46 #include <asm/io.h>
47 #include <asm/system.h>
48
49 #include "comedi_fops.h"
50
51 static int postconfig(comedi_device * dev);
52 static int insn_rw_emulate_bits(comedi_device * dev, comedi_subdevice * s,
53         comedi_insn * insn, lsampl_t * data);
54 static void *comedi_recognize(comedi_driver * driv, const char *name);
55 static void comedi_report_boards(comedi_driver * driv);
56 static int poll_invalid(comedi_device * dev, comedi_subdevice * s);
57 int comedi_buf_alloc(comedi_device * dev, comedi_subdevice * s,
58         unsigned long new_size);
59
60 comedi_driver *comedi_drivers;
61
62 int comedi_modprobe(int minor)
63 {
64         return -EINVAL;
65 }
66
67 static void cleanup_device(comedi_device * dev)
68 {
69         int i;
70         comedi_subdevice *s;
71
72         if (dev->subdevices) {
73                 for (i = 0; i < dev->n_subdevices; i++) {
74                         s = dev->subdevices + i;
75                         comedi_free_subdevice_minor(s);
76                         if (s->async) {
77                                 comedi_buf_alloc(dev, s, 0);
78                                 kfree(s->async);
79                         }
80                 }
81                 kfree(dev->subdevices);
82                 dev->subdevices = NULL;
83                 dev->n_subdevices = 0;
84         }
85         if (dev->private) {
86                 kfree(dev->private);
87                 dev->private = NULL;
88         }
89         dev->driver = 0;
90         dev->board_name = NULL;
91         dev->board_ptr = NULL;
92         dev->iobase = 0;
93         dev->irq = 0;
94         dev->read_subdev = NULL;
95         dev->write_subdev = NULL;
96         dev->open = NULL;
97         dev->close = NULL;
98         comedi_set_hw_dev(dev, NULL);
99 }
100
101 static void __comedi_device_detach(comedi_device * dev)
102 {
103         dev->attached = 0;
104         if (dev->driver) {
105                 dev->driver->detach(dev);
106         } else {
107                 printk("BUG: dev->driver=NULL in comedi_device_detach()\n");
108         }
109         cleanup_device(dev);
110 }
111
112 void comedi_device_detach(comedi_device * dev)
113 {
114         if (!dev->attached)
115                 return;
116         __comedi_device_detach(dev);
117 }
118
119 int comedi_device_attach(comedi_device * dev, comedi_devconfig * it)
120 {
121         comedi_driver *driv;
122         int ret;
123
124         if (dev->attached)
125                 return -EBUSY;
126
127         for (driv = comedi_drivers; driv; driv = driv->next) {
128                 if (!try_module_get(driv->module)) {
129                         printk("comedi: failed to increment module count, skipping\n");
130                         continue;
131                 }
132                 if (driv->num_names) {
133                         dev->board_ptr = comedi_recognize(driv, it->board_name);
134                         if (dev->board_ptr == NULL) {
135                                 module_put(driv->module);
136                                 continue;
137                         }
138                 } else {
139                         if (strcmp(driv->driver_name, it->board_name)) {
140                                 module_put(driv->module);
141                                 continue;
142                         }
143                 }
144                 //initialize dev->driver here so comedi_error() can be called from attach
145                 dev->driver = driv;
146                 ret = driv->attach(dev, it);
147                 if (ret < 0) {
148                         module_put(dev->driver->module);
149                         __comedi_device_detach(dev);
150                         return ret;
151                 }
152                 goto attached;
153         }
154
155         // recognize has failed if we get here
156         // report valid board names before returning error
157         for (driv = comedi_drivers; driv; driv = driv->next) {
158                 if (!try_module_get(driv->module)) {
159                         printk("comedi: failed to increment module count\n");
160                         continue;
161                 }
162                 comedi_report_boards(driv);
163                 module_put(driv->module);
164         }
165         return -EIO;
166
167 attached:
168         /* do a little post-config cleanup */
169         ret = postconfig(dev);
170         module_put(dev->driver->module);
171         if (ret < 0) {
172                 __comedi_device_detach(dev);
173                 return ret;
174         }
175
176         if (!dev->board_name) {
177                 printk("BUG: dev->board_name=<%p>\n", dev->board_name);
178                 dev->board_name = "BUG";
179         }
180         smp_wmb();
181         dev->attached = 1;
182
183         return 0;
184 }
185
186 int comedi_driver_register(comedi_driver * driver)
187 {
188         driver->next = comedi_drivers;
189         comedi_drivers = driver;
190
191         return 0;
192 }
193
194 int comedi_driver_unregister(comedi_driver * driver)
195 {
196         comedi_driver *prev;
197         int i;
198
199         /* check for devices using this driver */
200         for (i = 0; i < COMEDI_NUM_BOARD_MINORS; i++) {
201                 struct comedi_device_file_info *dev_file_info = comedi_get_device_file_info(i);
202                 comedi_device *dev;
203
204                 if(dev_file_info == NULL) continue;
205                 dev = dev_file_info->device;
206
207                 mutex_lock(&dev->mutex);
208                 if (dev->attached && dev->driver == driver) {
209                         if (dev->use_count)
210                                 printk("BUG! detaching device with use_count=%d\n", dev->use_count);
211                         comedi_device_detach(dev);
212                 }
213                 mutex_unlock(&dev->mutex);
214         }
215
216         if (comedi_drivers == driver) {
217                 comedi_drivers = driver->next;
218                 return 0;
219         }
220
221         for (prev = comedi_drivers; prev->next; prev = prev->next) {
222                 if (prev->next == driver) {
223                         prev->next = driver->next;
224                         return 0;
225                 }
226         }
227         return -EINVAL;
228 }
229
230 static int postconfig(comedi_device * dev)
231 {
232         int i;
233         comedi_subdevice *s;
234         comedi_async *async = NULL;
235         int ret;
236
237         for (i = 0; i < dev->n_subdevices; i++) {
238                 s = dev->subdevices + i;
239
240                 if (s->type == COMEDI_SUBD_UNUSED)
241                         continue;
242
243                 if (s->len_chanlist == 0)
244                         s->len_chanlist = 1;
245
246                 if (s->do_cmd) {
247                         BUG_ON((s->subdev_flags & (SDF_CMD_READ |
248                                 SDF_CMD_WRITE)) == 0);
249                         BUG_ON(!s->do_cmdtest);
250
251                         async = kzalloc(sizeof(comedi_async), GFP_KERNEL);
252                         if (async == NULL) {
253                                 printk("failed to allocate async struct\n");
254                                 return -ENOMEM;
255                         }
256                         init_waitqueue_head(&async->wait_head);
257                         async->subdevice = s;
258                         s->async = async;
259
260 #define DEFAULT_BUF_MAXSIZE (64*1024)
261 #define DEFAULT_BUF_SIZE (64*1024)
262
263                         async->max_bufsize = DEFAULT_BUF_MAXSIZE;
264
265                         async->prealloc_buf = NULL;
266                         async->prealloc_bufsz = 0;
267                         if (comedi_buf_alloc(dev, s, DEFAULT_BUF_SIZE) < 0) {
268                                 printk("Buffer allocation failed\n");
269                                 return -ENOMEM;
270                         }
271                         if (s->buf_change) {
272                                 ret = s->buf_change(dev, s, DEFAULT_BUF_SIZE);
273                                 if (ret < 0)
274                                         return ret;
275                         }
276                         comedi_alloc_subdevice_minor(dev, s);
277                 }
278
279                 if (!s->range_table && !s->range_table_list)
280                         s->range_table = &range_unknown;
281
282                 if (!s->insn_read && s->insn_bits)
283                         s->insn_read = insn_rw_emulate_bits;
284                 if (!s->insn_write && s->insn_bits)
285                         s->insn_write = insn_rw_emulate_bits;
286
287                 if (!s->insn_read)
288                         s->insn_read = insn_inval;
289                 if (!s->insn_write)
290                         s->insn_write = insn_inval;
291                 if (!s->insn_bits)
292                         s->insn_bits = insn_inval;
293                 if (!s->insn_config)
294                         s->insn_config = insn_inval;
295
296                 if (!s->poll)
297                         s->poll = poll_invalid;
298         }
299
300         return 0;
301 }
302
303 // generic recognize function for drivers that register their supported board names
304 void *comedi_recognize(comedi_driver * driv, const char *name)
305 {
306         unsigned i;
307         const char *const *name_ptr = driv->board_name;
308         for (i = 0; i < driv->num_names; i++) {
309                 if (strcmp(*name_ptr, name) == 0)
310                         return (void *)name_ptr;
311                 name_ptr =
312                         (const char *const *)((const char *)name_ptr +
313                         driv->offset);
314         }
315
316         return NULL;
317 }
318
319 void comedi_report_boards(comedi_driver * driv)
320 {
321         unsigned int i;
322         const char *const *name_ptr;
323
324         printk("comedi: valid board names for %s driver are:\n",
325                 driv->driver_name);
326
327         name_ptr = driv->board_name;
328         for (i = 0; i < driv->num_names; i++) {
329                 printk(" %s\n", *name_ptr);
330                 name_ptr = (const char **)((char *)name_ptr + driv->offset);
331         }
332
333         if (driv->num_names == 0)
334                 printk(" %s\n", driv->driver_name);
335 }
336
337 static int poll_invalid(comedi_device * dev, comedi_subdevice * s)
338 {
339         return -EINVAL;
340 }
341
342 int insn_inval(comedi_device * dev, comedi_subdevice * s,
343         comedi_insn * insn, lsampl_t * data)
344 {
345         return -EINVAL;
346 }
347
348 static int insn_rw_emulate_bits(comedi_device * dev, comedi_subdevice * s,
349         comedi_insn * insn, lsampl_t * data)
350 {
351         comedi_insn new_insn;
352         int ret;
353         static const unsigned channels_per_bitfield = 32;
354         unsigned chan = CR_CHAN(insn->chanspec);
355         const unsigned base_bitfield_channel =
356                 (chan < channels_per_bitfield) ? 0 : chan;
357         lsampl_t new_data[2];
358
359         if ((insn->insn == INSN_WRITE) && !(s->subdev_flags & SDF_WRITABLE))
360                 return -EINVAL;
361
362         if (insn->n == 0) 
363                 return 0;
364
365         memset(new_data, 0, sizeof(new_data));
366         memset(&new_insn, 0, sizeof(new_insn));
367         new_insn.insn = INSN_BITS;
368         new_insn.chanspec = base_bitfield_channel;
369         new_insn.n = 2;
370         new_insn.data = new_data;
371         new_insn.subdev = insn->subdev;
372
373         if (insn->insn == INSN_WRITE) {
374                 new_data[0] = 1 << (chan - base_bitfield_channel);      /* mask */
375                 new_data[1] = data[0] ? (1 << (chan - base_bitfield_channel)) : 0;      /* bits */
376         }
377
378         ret = s->insn_bits(dev, s, &new_insn, new_data);
379         if (ret < 0)
380                 return ret;
381
382         if (insn->insn == INSN_READ) {
383                 data[0] = (new_data[1] >> (chan - base_bitfield_channel)) & 1;
384         }
385
386         return 1;
387 }
388
389 static inline unsigned long uvirt_to_kva(pgd_t * pgd, unsigned long adr)
390 {
391         unsigned long ret = 0UL;
392         pmd_t *pmd;
393         pte_t *ptep, pte;
394         pud_t *pud;
395
396         if (!pgd_none(*pgd)) {
397                 pud = pud_offset(pgd, adr);
398                 pmd = pmd_offset(pud, adr);
399                 if (!pmd_none(*pmd)) {
400                         ptep = pte_offset_kernel(pmd, adr);
401                         pte = *ptep;
402                         if (pte_present(pte)) {
403                                 ret = (unsigned long)
404                                         page_address(pte_page(pte));
405                                 ret |= (adr & (PAGE_SIZE - 1));
406                         }
407                 }
408         }
409         return ret;
410 }
411
412 static inline unsigned long kvirt_to_kva(unsigned long adr)
413 {
414         unsigned long va, kva;
415
416         va = adr;
417         kva = uvirt_to_kva(pgd_offset_k(va), va);
418
419         return kva;
420 }
421
422 int comedi_buf_alloc(comedi_device * dev, comedi_subdevice * s,
423         unsigned long new_size)
424 {
425         comedi_async *async = s->async;
426
427         /* Round up new_size to multiple of PAGE_SIZE */
428         new_size = (new_size + PAGE_SIZE - 1) & PAGE_MASK;
429
430         /* if no change is required, do nothing */
431         if (async->prealloc_buf && async->prealloc_bufsz == new_size) {
432                 return 0;
433         }
434         // deallocate old buffer
435         if (async->prealloc_buf) {
436                 vunmap(async->prealloc_buf);
437                 async->prealloc_buf = NULL;
438                 async->prealloc_bufsz = 0;
439         }
440         if (async->buf_page_list) {
441                 unsigned i;
442                 for (i = 0; i < async->n_buf_pages; ++i) {
443                         if (async->buf_page_list[i].virt_addr) {
444                                 mem_map_unreserve(virt_to_page(async->
445                                                 buf_page_list[i].virt_addr));
446                                 if (s->async_dma_dir != DMA_NONE) {
447                                         dma_free_coherent(dev->hw_dev,
448                                                 PAGE_SIZE,
449                                                 async->buf_page_list[i].
450                                                 virt_addr,
451                                                 async->buf_page_list[i].
452                                                 dma_addr);
453                                 } else {
454                                         free_page((unsigned long)async->
455                                                 buf_page_list[i].virt_addr);
456                                 }
457                         }
458                 }
459                 vfree(async->buf_page_list);
460                 async->buf_page_list = NULL;
461                 async->n_buf_pages = 0;
462         }
463         // allocate new buffer
464         if (new_size) {
465                 unsigned i = 0;
466                 unsigned n_pages = new_size >> PAGE_SHIFT;
467                 struct page **pages = NULL;
468
469                 async->buf_page_list =
470                         vmalloc(sizeof(struct comedi_buf_page) * n_pages);
471                 if (async->buf_page_list) {
472                         memset(async->buf_page_list, 0,
473                                 sizeof(struct comedi_buf_page) * n_pages);
474                         pages = vmalloc(sizeof(struct page *) * n_pages);
475                 }
476                 if (pages) {
477                         for (i = 0; i < n_pages; i++) {
478                                 if (s->async_dma_dir != DMA_NONE) {
479                                         async->buf_page_list[i].virt_addr =
480                                                 dma_alloc_coherent(dev->hw_dev,
481                                                 PAGE_SIZE,
482                                                 &async->buf_page_list[i].
483                                                 dma_addr,
484                                                 GFP_KERNEL | __GFP_COMP);
485                                 } else {
486                                         async->buf_page_list[i].virt_addr =
487                                                 (void *)
488                                                 get_zeroed_page(GFP_KERNEL);
489                                 }
490                                 if (async->buf_page_list[i].virt_addr == NULL) {
491                                         break;
492                                 }
493                                 mem_map_reserve(virt_to_page(async->
494                                                 buf_page_list[i].virt_addr));
495                                 pages[i] =
496                                         virt_to_page(async->buf_page_list[i].
497                                         virt_addr);
498                         }
499                 }
500                 if (i == n_pages) {
501                         async->prealloc_buf =
502                                 vmap(pages, n_pages, VM_MAP,
503                                 PAGE_KERNEL_NOCACHE);
504                 }
505                 if (pages) {
506                         vfree(pages);
507                 }
508                 if (async->prealloc_buf == NULL) {
509                         /* Some allocation failed above. */
510                         if (async->buf_page_list) {
511                                 for (i = 0; i < n_pages; i++) {
512                                         if (async->buf_page_list[i].virt_addr ==
513                                                 NULL) {
514                                                 break;
515                                         }
516                                         mem_map_unreserve(virt_to_page(async->
517                                                         buf_page_list[i].
518                                                         virt_addr));
519                                         if (s->async_dma_dir != DMA_NONE) {
520                                                 dma_free_coherent(dev->hw_dev,
521                                                         PAGE_SIZE,
522                                                         async->buf_page_list[i].
523                                                         virt_addr,
524                                                         async->buf_page_list[i].
525                                                         dma_addr);
526                                         } else {
527                                                 free_page((unsigned long)async->
528                                                         buf_page_list[i].
529                                                         virt_addr);
530                                         }
531                                 }
532                                 vfree(async->buf_page_list);
533                                 async->buf_page_list = NULL;
534                         }
535                         return -ENOMEM;
536                 }
537                 async->n_buf_pages = n_pages;
538         }
539         async->prealloc_bufsz = new_size;
540
541         return 0;
542 }
543
544 /* munging is applied to data by core as it passes between user
545  * and kernel space */
546 unsigned int comedi_buf_munge(comedi_async * async, unsigned int num_bytes)
547 {
548         comedi_subdevice *s = async->subdevice;
549         unsigned int count = 0;
550         const unsigned num_sample_bytes = bytes_per_sample(s);
551
552         if (s->munge == NULL || (async->cmd.flags & CMDF_RAWDATA)) {
553                 async->munge_count += num_bytes;
554                 if ((int)(async->munge_count - async->buf_write_count) > 0)
555                         BUG();
556                 return num_bytes;
557         }
558         /* don't munge partial samples */
559         num_bytes -= num_bytes % num_sample_bytes;
560         while (count < num_bytes) {
561                 int block_size;
562
563                 block_size = num_bytes - count;
564                 if (block_size < 0) {
565                         rt_printk("%s: %s: bug! block_size is negative\n",
566                                 __FILE__, __FUNCTION__);
567                         break;
568                 }
569                 if ((int)(async->munge_ptr + block_size -
570                                 async->prealloc_bufsz) > 0)
571                         block_size = async->prealloc_bufsz - async->munge_ptr;
572
573                 s->munge(s->device, s, async->prealloc_buf + async->munge_ptr,
574                         block_size, async->munge_chan);
575
576                 smp_wmb();      //barrier insures data is munged in buffer before munge_count is incremented
577
578                 async->munge_chan += block_size / num_sample_bytes;
579                 async->munge_chan %= async->cmd.chanlist_len;
580                 async->munge_count += block_size;
581                 async->munge_ptr += block_size;
582                 async->munge_ptr %= async->prealloc_bufsz;
583                 count += block_size;
584         }
585         if ((int)(async->munge_count - async->buf_write_count) > 0)
586                 BUG();
587         return count;
588 }
589
590 unsigned int comedi_buf_write_n_available(comedi_async * async)
591 {
592         unsigned int free_end;
593         unsigned int nbytes;
594
595         if (async == NULL)
596                 return 0;
597
598         free_end = async->buf_read_count + async->prealloc_bufsz;
599         nbytes = free_end - async->buf_write_alloc_count;
600         nbytes -= nbytes % bytes_per_sample(async->subdevice);
601         /* barrier insures the read of buf_read_count in this
602            query occurs before any following writes to the buffer which
603            might be based on the return value from this query.
604          */
605         smp_mb();
606         return nbytes;
607 }
608
609 /* allocates chunk for the writer from free buffer space */
610 unsigned int comedi_buf_write_alloc(comedi_async * async, unsigned int nbytes)
611 {
612         unsigned int free_end = async->buf_read_count + async->prealloc_bufsz;
613
614         if ((int)(async->buf_write_alloc_count + nbytes - free_end) > 0) {
615                 nbytes = free_end - async->buf_write_alloc_count;
616         }
617         async->buf_write_alloc_count += nbytes;
618         /* barrier insures the read of buf_read_count above occurs before
619            we write data to the write-alloc'ed buffer space */
620         smp_mb();
621         return nbytes;
622 }
623
624 /* allocates nothing unless it can completely fulfill the request */
625 unsigned int comedi_buf_write_alloc_strict(comedi_async * async,
626         unsigned int nbytes)
627 {
628         unsigned int free_end = async->buf_read_count + async->prealloc_bufsz;
629
630         if ((int)(async->buf_write_alloc_count + nbytes - free_end) > 0) {
631                 nbytes = 0;
632         }
633         async->buf_write_alloc_count += nbytes;
634         /* barrier insures the read of buf_read_count above occurs before
635            we write data to the write-alloc'ed buffer space */
636         smp_mb();
637         return nbytes;
638 }
639
640 /* transfers a chunk from writer to filled buffer space */
641 unsigned comedi_buf_write_free(comedi_async * async, unsigned int nbytes)
642 {
643         if ((int)(async->buf_write_count + nbytes -
644                         async->buf_write_alloc_count) > 0) {
645                 rt_printk
646                         ("comedi: attempted to write-free more bytes than have been write-allocated.\n");
647                 nbytes = async->buf_write_alloc_count - async->buf_write_count;
648         }
649         async->buf_write_count += nbytes;
650         async->buf_write_ptr += nbytes;
651         comedi_buf_munge(async, async->buf_write_count - async->munge_count);
652         if (async->buf_write_ptr >= async->prealloc_bufsz) {
653                 async->buf_write_ptr %= async->prealloc_bufsz;
654         }
655         return nbytes;
656 }
657
658 /* allocates a chunk for the reader from filled (and munged) buffer space */
659 unsigned comedi_buf_read_alloc(comedi_async * async, unsigned nbytes)
660 {
661         if ((int)(async->buf_read_alloc_count + nbytes - async->munge_count) >
662                 0) {
663                 nbytes = async->munge_count - async->buf_read_alloc_count;
664         }
665         async->buf_read_alloc_count += nbytes;
666         /* barrier insures read of munge_count occurs before we actually read
667            data out of buffer */
668         smp_rmb();
669         return nbytes;
670 }
671
672 /* transfers control of a chunk from reader to free buffer space */
673 unsigned comedi_buf_read_free(comedi_async * async, unsigned int nbytes)
674 {
675         // barrier insures data has been read out of buffer before read count is incremented
676         smp_mb();
677         if ((int)(async->buf_read_count + nbytes -
678                         async->buf_read_alloc_count) > 0) {
679                 rt_printk
680                         ("comedi: attempted to read-free more bytes than have been read-allocated.\n");
681                 nbytes = async->buf_read_alloc_count - async->buf_read_count;
682         }
683         async->buf_read_count += nbytes;
684         async->buf_read_ptr += nbytes;
685         async->buf_read_ptr %= async->prealloc_bufsz;
686         return nbytes;
687 }
688
689 void comedi_buf_memcpy_to(comedi_async * async, unsigned int offset,
690         const void *data, unsigned int num_bytes)
691 {
692         unsigned int write_ptr = async->buf_write_ptr + offset;
693
694         if (write_ptr >= async->prealloc_bufsz)
695                 write_ptr %= async->prealloc_bufsz;
696
697         while (num_bytes) {
698                 unsigned int block_size;
699
700                 if (write_ptr + num_bytes > async->prealloc_bufsz)
701                         block_size = async->prealloc_bufsz - write_ptr;
702                 else
703                         block_size = num_bytes;
704
705                 memcpy(async->prealloc_buf + write_ptr, data, block_size);
706
707                 data += block_size;
708                 num_bytes -= block_size;
709
710                 write_ptr = 0;
711         }
712 }
713
714 void comedi_buf_memcpy_from(comedi_async * async, unsigned int offset,
715         void *dest, unsigned int nbytes)
716 {
717         void *src;
718         unsigned int read_ptr = async->buf_read_ptr + offset;
719
720         if (read_ptr >= async->prealloc_bufsz)
721                 read_ptr %= async->prealloc_bufsz;
722
723         while (nbytes) {
724                 unsigned int block_size;
725
726                 src = async->prealloc_buf + read_ptr;
727
728                 if (nbytes >= async->prealloc_bufsz - read_ptr)
729                         block_size = async->prealloc_bufsz - read_ptr;
730                 else
731                         block_size = nbytes;
732
733                 memcpy(dest, src, block_size);
734                 nbytes -= block_size;
735                 dest += block_size;
736                 read_ptr = 0;
737         }
738 }
739
740 unsigned int comedi_buf_read_n_available(comedi_async * async)
741 {
742         unsigned num_bytes;
743
744         if (async == NULL)
745                 return 0;
746         num_bytes = async->munge_count - async->buf_read_count;
747         /* barrier insures the read of munge_count in this
748            query occurs before any following reads of the buffer which
749            might be based on the return value from this query.
750          */
751         smp_rmb();
752         return num_bytes;
753 }
754
755 int comedi_buf_get(comedi_async * async, sampl_t * x)
756 {
757         unsigned int n = comedi_buf_read_n_available(async);
758
759         if (n < sizeof(sampl_t))
760                 return 0;
761         comedi_buf_read_alloc(async, sizeof(sampl_t));
762         *x = *(sampl_t *) (async->prealloc_buf + async->buf_read_ptr);
763         comedi_buf_read_free(async, sizeof(sampl_t));
764         return 1;
765 }
766
767 int comedi_buf_put(comedi_async * async, sampl_t x)
768 {
769         unsigned int n = comedi_buf_write_alloc_strict(async, sizeof(sampl_t));
770
771         if (n < sizeof(sampl_t)) {
772                 async->events |= COMEDI_CB_ERROR;
773                 return 0;
774         }
775         *(sampl_t *) (async->prealloc_buf + async->buf_write_ptr) = x;
776         comedi_buf_write_free(async, sizeof(sampl_t));
777         return 1;
778 }
779
780 void comedi_reset_async_buf(comedi_async * async)
781 {
782         async->buf_write_alloc_count = 0;
783         async->buf_write_count = 0;
784         async->buf_read_alloc_count = 0;
785         async->buf_read_count = 0;
786
787         async->buf_write_ptr = 0;
788         async->buf_read_ptr = 0;
789
790         async->cur_chan = 0;
791         async->scan_progress = 0;
792         async->munge_chan = 0;
793         async->munge_count = 0;
794         async->munge_ptr = 0;
795
796         async->events = 0;
797 }
798
799 int comedi_auto_config(struct device *hardware_device, const char *board_name, const int *options, unsigned num_options)
800 {
801         comedi_devconfig it;
802         int minor;
803         struct comedi_device_file_info *dev_file_info;
804         int retval;
805         unsigned *private_data = NULL;
806
807         if(!comedi_autoconfig) {
808                 dev_set_drvdata(hardware_device, NULL);
809                 return 0;
810         }
811
812         minor = comedi_alloc_board_minor(hardware_device);
813         if(minor < 0) return minor;
814         
815         private_data = kmalloc(sizeof(unsigned), GFP_KERNEL);
816         if(private_data == NULL) {
817                 retval = -ENOMEM;
818                 goto cleanup;
819         }
820         *private_data = minor;
821         dev_set_drvdata(hardware_device, private_data);
822
823         dev_file_info = comedi_get_device_file_info(minor);
824
825         memset(&it, 0, sizeof(it));
826         strncpy(it.board_name, board_name, COMEDI_NAMELEN);
827         it.board_name[COMEDI_NAMELEN - 1] = '\0';
828         BUG_ON(num_options > COMEDI_NDEVCONFOPTS);
829         memcpy(it.options, options, num_options * sizeof(int));
830
831         mutex_lock(&dev_file_info->device->mutex);
832         retval = comedi_device_attach(dev_file_info->device, &it);
833         mutex_unlock(&dev_file_info->device->mutex);
834
835 cleanup:        
836         if(retval < 0)
837         {
838                 kfree(private_data);
839                 comedi_free_board_minor(minor);
840         }
841         return retval;
842 }
843
844 void comedi_auto_unconfig(struct device *hardware_device)
845 {
846         unsigned *minor = (unsigned *)dev_get_drvdata(hardware_device);
847         if(minor == NULL) return;
848
849         BUG_ON(*minor >= COMEDI_NUM_BOARD_MINORS);
850
851         comedi_free_board_minor(*minor);
852         dev_set_drvdata(hardware_device, NULL);
853         kfree(minor);
854 }
855
856 int comedi_pci_auto_config(struct pci_dev *pcidev, const char *board_name)
857 {
858         int options[2];
859
860         // pci bus
861         options[0] = pcidev->bus->number;
862         // pci slot
863         options[1] = PCI_SLOT(pcidev->devfn);
864
865         return comedi_auto_config(&pcidev->dev, board_name, options, sizeof(options) / sizeof(options[0]));
866 }
867
868 void comedi_pci_auto_unconfig(struct pci_dev *pcidev)
869 {
870         comedi_auto_unconfig(&pcidev->dev);
871 }
872
873 int comedi_usb_auto_config(struct usb_device *usbdev,
874         const char *board_name)
875 {
876         BUG_ON(usbdev == NULL);
877         return comedi_auto_config(&usbdev->dev, board_name, NULL, 0);
878 }
879
880 void comedi_usb_auto_unconfig(struct usb_device *usbdev)
881 {
882         BUG_ON(usbdev == NULL);
883         comedi_auto_unconfig(&usbdev->dev);
884 }