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