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