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