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