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