e5fe2e1f83f712455f9ca8f646ea66cd28f7e0bd
[firefly-linux-kernel-4.4.55.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 #include <linux/device.h>
25 #include <linux/module.h>
26 #include <linux/pci.h>
27 #include <pcmcia/cistpl.h>
28 #include <pcmcia/ds.h>
29 #include <linux/usb.h>
30 #include <linux/errno.h>
31 #include <linux/kconfig.h>
32 #include <linux/kernel.h>
33 #include <linux/sched.h>
34 #include <linux/fcntl.h>
35 #include <linux/delay.h>
36 #include <linux/ioport.h>
37 #include <linux/mm.h>
38 #include <linux/slab.h>
39 #include <linux/highmem.h>      /* for SuSE brokenness */
40 #include <linux/vmalloc.h>
41 #include <linux/cdev.h>
42 #include <linux/dma-mapping.h>
43 #include <linux/io.h>
44
45 #include "comedidev.h"
46 #include "comedi_internal.h"
47
48 struct comedi_driver *comedi_drivers;
49
50 int comedi_alloc_subdevices(struct comedi_device *dev, int num_subdevices)
51 {
52         struct comedi_subdevice *s;
53         int i;
54
55         if (num_subdevices < 1)
56                 return -EINVAL;
57
58         s = kcalloc(num_subdevices, sizeof(*s), GFP_KERNEL);
59         if (!s)
60                 return -ENOMEM;
61         dev->subdevices = s;
62         dev->n_subdevices = num_subdevices;
63
64         for (i = 0; i < num_subdevices; ++i) {
65                 s = &dev->subdevices[i];
66                 s->device = dev;
67                 s->index = i;
68                 s->async_dma_dir = DMA_NONE;
69                 spin_lock_init(&s->spin_lock);
70                 s->minor = -1;
71         }
72         return 0;
73 }
74 EXPORT_SYMBOL_GPL(comedi_alloc_subdevices);
75
76 static void cleanup_device(struct comedi_device *dev)
77 {
78         int i;
79         struct comedi_subdevice *s;
80
81         if (dev->subdevices) {
82                 for (i = 0; i < dev->n_subdevices; i++) {
83                         s = &dev->subdevices[i];
84                         comedi_free_subdevice_minor(s);
85                         if (s->async) {
86                                 comedi_buf_alloc(dev, s, 0);
87                                 kfree(s->async);
88                         }
89                 }
90                 kfree(dev->subdevices);
91                 dev->subdevices = NULL;
92                 dev->n_subdevices = 0;
93         }
94         kfree(dev->private);
95         dev->private = NULL;
96         dev->driver = NULL;
97         dev->board_name = NULL;
98         dev->board_ptr = NULL;
99         dev->iobase = 0;
100         dev->irq = 0;
101         dev->read_subdev = NULL;
102         dev->write_subdev = NULL;
103         dev->open = NULL;
104         dev->close = NULL;
105         comedi_set_hw_dev(dev, NULL);
106 }
107
108 static void __comedi_device_detach(struct comedi_device *dev)
109 {
110         dev->attached = 0;
111         if (dev->driver)
112                 dev->driver->detach(dev);
113         else
114                 dev_warn(dev->class_dev,
115                          "BUG: dev->driver=NULL in comedi_device_detach()\n");
116         cleanup_device(dev);
117 }
118
119 void comedi_device_detach(struct comedi_device *dev)
120 {
121         if (!dev->attached)
122                 return;
123         __comedi_device_detach(dev);
124 }
125
126 static int poll_invalid(struct comedi_device *dev, struct comedi_subdevice *s)
127 {
128         return -EINVAL;
129 }
130
131 int insn_inval(struct comedi_device *dev, struct comedi_subdevice *s,
132                struct comedi_insn *insn, unsigned int *data)
133 {
134         return -EINVAL;
135 }
136
137 static int insn_rw_emulate_bits(struct comedi_device *dev,
138                                 struct comedi_subdevice *s,
139                                 struct comedi_insn *insn, unsigned int *data)
140 {
141         struct comedi_insn new_insn;
142         int ret;
143         static const unsigned channels_per_bitfield = 32;
144
145         unsigned chan = CR_CHAN(insn->chanspec);
146         const unsigned base_bitfield_channel =
147             (chan < channels_per_bitfield) ? 0 : chan;
148         unsigned int new_data[2];
149         memset(new_data, 0, sizeof(new_data));
150         memset(&new_insn, 0, sizeof(new_insn));
151         new_insn.insn = INSN_BITS;
152         new_insn.chanspec = base_bitfield_channel;
153         new_insn.n = 2;
154         new_insn.subdev = insn->subdev;
155
156         if (insn->insn == INSN_WRITE) {
157                 if (!(s->subdev_flags & SDF_WRITABLE))
158                         return -EINVAL;
159                 new_data[0] = 1 << (chan - base_bitfield_channel); /* mask */
160                 new_data[1] = data[0] ? (1 << (chan - base_bitfield_channel))
161                               : 0; /* bits */
162         }
163
164         ret = s->insn_bits(dev, s, &new_insn, new_data);
165         if (ret < 0)
166                 return ret;
167
168         if (insn->insn == INSN_READ)
169                 data[0] = (new_data[1] >> (chan - base_bitfield_channel)) & 1;
170
171         return 1;
172 }
173
174 static int __comedi_device_postconfig_async(struct comedi_device *dev,
175                                             struct comedi_subdevice *s)
176 {
177         struct comedi_async *async;
178         unsigned int buf_size;
179         int ret;
180
181         BUG_ON((s->subdev_flags & (SDF_CMD_READ | SDF_CMD_WRITE)) == 0);
182         BUG_ON(!s->do_cmdtest);
183
184         async = kzalloc(sizeof(*async), GFP_KERNEL);
185         if (!async) {
186                 dev_warn(dev->class_dev, "failed to allocate async struct\n");
187                 return -ENOMEM;
188         }
189         init_waitqueue_head(&async->wait_head);
190         async->subdevice = s;
191         s->async = async;
192
193         async->max_bufsize = comedi_default_buf_maxsize_kb * 1024;
194         buf_size = comedi_default_buf_size_kb * 1024;
195         if (buf_size > async->max_bufsize)
196                 buf_size = async->max_bufsize;
197
198         if (comedi_buf_alloc(dev, s, buf_size) < 0) {
199                 dev_warn(dev->class_dev, "Buffer allocation failed\n");
200                 return -ENOMEM;
201         }
202         if (s->buf_change) {
203                 ret = s->buf_change(dev, s, buf_size);
204                 if (ret < 0)
205                         return ret;
206         }
207
208         comedi_alloc_subdevice_minor(dev, s);
209
210         return 0;
211 }
212
213 static int __comedi_device_postconfig(struct comedi_device *dev)
214 {
215         struct comedi_subdevice *s;
216         int ret;
217         int i;
218
219         for (i = 0; i < dev->n_subdevices; i++) {
220                 s = &dev->subdevices[i];
221
222                 if (s->type == COMEDI_SUBD_UNUSED)
223                         continue;
224
225                 if (s->len_chanlist == 0)
226                         s->len_chanlist = 1;
227
228                 if (s->do_cmd) {
229                         ret = __comedi_device_postconfig_async(dev, s);
230                         if (ret)
231                                 return ret;
232                 }
233
234                 if (!s->range_table && !s->range_table_list)
235                         s->range_table = &range_unknown;
236
237                 if (!s->insn_read && s->insn_bits)
238                         s->insn_read = insn_rw_emulate_bits;
239                 if (!s->insn_write && s->insn_bits)
240                         s->insn_write = insn_rw_emulate_bits;
241
242                 if (!s->insn_read)
243                         s->insn_read = insn_inval;
244                 if (!s->insn_write)
245                         s->insn_write = insn_inval;
246                 if (!s->insn_bits)
247                         s->insn_bits = insn_inval;
248                 if (!s->insn_config)
249                         s->insn_config = insn_inval;
250
251                 if (!s->poll)
252                         s->poll = poll_invalid;
253         }
254
255         return 0;
256 }
257
258 /* do a little post-config cleanup */
259 /* called with module refcount incremented, decrements it */
260 static int comedi_device_postconfig(struct comedi_device *dev)
261 {
262         int ret = __comedi_device_postconfig(dev);
263         module_put(dev->driver->module);
264         if (ret < 0) {
265                 __comedi_device_detach(dev);
266                 return ret;
267         }
268         if (!dev->board_name) {
269                 dev_warn(dev->class_dev, "BUG: dev->board_name=NULL\n");
270                 dev->board_name = "BUG";
271         }
272         smp_wmb();
273         dev->attached = 1;
274         return 0;
275 }
276
277 /*
278  * Generic recognize function for drivers that register their supported
279  * board names.
280  *
281  * 'driv->board_name' points to a 'const char *' member within the
282  * zeroth element of an array of some private board information
283  * structure, say 'struct foo_board' containing a member 'const char
284  * *board_name' that is initialized to point to a board name string that
285  * is one of the candidates matched against this function's 'name'
286  * parameter.
287  *
288  * 'driv->offset' is the size of the private board information
289  * structure, say 'sizeof(struct foo_board)', and 'driv->num_names' is
290  * the length of the array of private board information structures.
291  *
292  * If one of the board names in the array of private board information
293  * structures matches the name supplied to this function, the function
294  * returns a pointer to the pointer to the board name, otherwise it
295  * returns NULL.  The return value ends up in the 'board_ptr' member of
296  * a 'struct comedi_device' that the low-level comedi driver's
297  * 'attach()' hook can convert to a point to a particular element of its
298  * array of private board information structures by subtracting the
299  * offset of the member that points to the board name.  (No subtraction
300  * is required if the board name pointer is the first member of the
301  * private board information structure, which is generally the case.)
302  */
303 static void *comedi_recognize(struct comedi_driver *driv, const char *name)
304 {
305         char **name_ptr = (char **)driv->board_name;
306         int i;
307
308         for (i = 0; i < driv->num_names; i++) {
309                 if (strcmp(*name_ptr, name) == 0)
310                         return name_ptr;
311                 name_ptr = (void *)name_ptr + driv->offset;
312         }
313
314         return NULL;
315 }
316
317 static void comedi_report_boards(struct comedi_driver *driv)
318 {
319         unsigned int i;
320         const char *const *name_ptr;
321
322         pr_info("comedi: valid board names for %s driver are:\n",
323                 driv->driver_name);
324
325         name_ptr = driv->board_name;
326         for (i = 0; i < driv->num_names; i++) {
327                 pr_info(" %s\n", *name_ptr);
328                 name_ptr = (const char **)((char *)name_ptr + driv->offset);
329         }
330
331         if (driv->num_names == 0)
332                 pr_info(" %s\n", driv->driver_name);
333 }
334
335 int comedi_device_attach(struct comedi_device *dev, struct comedi_devconfig *it)
336 {
337         struct comedi_driver *driv;
338         int ret;
339
340         if (dev->attached)
341                 return -EBUSY;
342
343         for (driv = comedi_drivers; driv; driv = driv->next) {
344                 if (!try_module_get(driv->module))
345                         continue;
346                 if (driv->num_names) {
347                         dev->board_ptr = comedi_recognize(driv, it->board_name);
348                         if (dev->board_ptr)
349                                 break;
350                 } else if (strcmp(driv->driver_name, it->board_name) == 0)
351                         break;
352                 module_put(driv->module);
353         }
354         if (driv == NULL) {
355                 /*  recognize has failed if we get here */
356                 /*  report valid board names before returning error */
357                 for (driv = comedi_drivers; driv; driv = driv->next) {
358                         if (!try_module_get(driv->module))
359                                 continue;
360                         comedi_report_boards(driv);
361                         module_put(driv->module);
362                 }
363                 return -EIO;
364         }
365         if (driv->attach == NULL) {
366                 /* driver does not support manual configuration */
367                 dev_warn(dev->class_dev,
368                          "driver '%s' does not support attach using comedi_config\n",
369                          driv->driver_name);
370                 module_put(driv->module);
371                 return -ENOSYS;
372         }
373         /* initialize dev->driver here so
374          * comedi_error() can be called from attach */
375         dev->driver = driv;
376         ret = driv->attach(dev, it);
377         if (ret < 0) {
378                 module_put(dev->driver->module);
379                 __comedi_device_detach(dev);
380                 return ret;
381         }
382         return comedi_device_postconfig(dev);
383 }
384
385 int comedi_driver_register(struct comedi_driver *driver)
386 {
387         driver->next = comedi_drivers;
388         comedi_drivers = driver;
389
390         return 0;
391 }
392 EXPORT_SYMBOL(comedi_driver_register);
393
394 int comedi_driver_unregister(struct comedi_driver *driver)
395 {
396         struct comedi_driver *prev;
397         int i;
398
399         /* check for devices using this driver */
400         for (i = 0; i < COMEDI_NUM_BOARD_MINORS; i++) {
401                 struct comedi_device *dev = comedi_dev_from_minor(i);
402
403                 if (!dev)
404                         continue;
405
406                 mutex_lock(&dev->mutex);
407                 if (dev->attached && dev->driver == driver) {
408                         if (dev->use_count)
409                                 dev_warn(dev->class_dev,
410                                          "BUG! detaching device with use_count=%d\n",
411                                          dev->use_count);
412                         comedi_device_detach(dev);
413                 }
414                 mutex_unlock(&dev->mutex);
415         }
416
417         if (comedi_drivers == driver) {
418                 comedi_drivers = driver->next;
419                 return 0;
420         }
421
422         for (prev = comedi_drivers; prev->next; prev = prev->next) {
423                 if (prev->next == driver) {
424                         prev->next = driver->next;
425                         return 0;
426                 }
427         }
428         return -EINVAL;
429 }
430 EXPORT_SYMBOL(comedi_driver_unregister);
431
432 int comedi_auto_config(struct device *hardware_device,
433                        struct comedi_driver *driver, unsigned long context)
434 {
435         int minor;
436         struct comedi_device *comedi_dev;
437         int ret;
438
439         if (!comedi_autoconfig)
440                 return 0;
441
442         if (!driver->auto_attach) {
443                 dev_warn(hardware_device,
444                          "BUG! comedi driver '%s' has no auto_attach handler\n",
445                          driver->driver_name);
446                 return -EINVAL;
447         }
448
449         minor = comedi_alloc_board_minor(hardware_device);
450         if (minor < 0)
451                 return minor;
452
453         comedi_dev = comedi_dev_from_minor(minor);
454
455         mutex_lock(&comedi_dev->mutex);
456         if (comedi_dev->attached)
457                 ret = -EBUSY;
458         else if (!try_module_get(driver->module))
459                 ret = -EIO;
460         else {
461                 comedi_set_hw_dev(comedi_dev, hardware_device);
462                 comedi_dev->driver = driver;
463                 ret = driver->auto_attach(comedi_dev, context);
464                 if (ret < 0) {
465                         module_put(driver->module);
466                         __comedi_device_detach(comedi_dev);
467                 } else {
468                         ret = comedi_device_postconfig(comedi_dev);
469                 }
470         }
471         mutex_unlock(&comedi_dev->mutex);
472
473         if (ret < 0)
474                 comedi_free_board_minor(minor);
475         return ret;
476 }
477 EXPORT_SYMBOL_GPL(comedi_auto_config);
478
479 void comedi_auto_unconfig(struct device *hardware_device)
480 {
481         int minor;
482
483         if (hardware_device == NULL)
484                 return;
485         minor = comedi_find_board_minor(hardware_device);
486         if (minor < 0)
487                 return;
488         BUG_ON(minor >= COMEDI_NUM_BOARD_MINORS);
489         comedi_free_board_minor(minor);
490 }
491 EXPORT_SYMBOL_GPL(comedi_auto_unconfig);
492
493 /**
494  * comedi_pci_enable() - Enable the PCI device and request the regions.
495  * @pdev: pci_dev struct
496  * @res_name: name for the requested reqource
497  */
498 int comedi_pci_enable(struct pci_dev *pdev, const char *res_name)
499 {
500         int rc;
501
502         rc = pci_enable_device(pdev);
503         if (rc < 0)
504                 return rc;
505
506         rc = pci_request_regions(pdev, res_name);
507         if (rc < 0)
508                 pci_disable_device(pdev);
509
510         return rc;
511 }
512 EXPORT_SYMBOL_GPL(comedi_pci_enable);
513
514 /**
515  * comedi_pci_disable() - Release the regions and disable the PCI device.
516  * @pdev: pci_dev struct
517  *
518  * This must be matched with a previous successful call to comedi_pci_enable().
519  */
520 void comedi_pci_disable(struct pci_dev *pdev)
521 {
522         pci_release_regions(pdev);
523         pci_disable_device(pdev);
524 }
525 EXPORT_SYMBOL_GPL(comedi_pci_disable);
526
527 int comedi_pci_driver_register(struct comedi_driver *comedi_driver,
528                 struct pci_driver *pci_driver)
529 {
530         int ret;
531
532         ret = comedi_driver_register(comedi_driver);
533         if (ret < 0)
534                 return ret;
535
536         ret = pci_register_driver(pci_driver);
537         if (ret < 0) {
538                 comedi_driver_unregister(comedi_driver);
539                 return ret;
540         }
541
542         return 0;
543 }
544 EXPORT_SYMBOL_GPL(comedi_pci_driver_register);
545
546 void comedi_pci_driver_unregister(struct comedi_driver *comedi_driver,
547                 struct pci_driver *pci_driver)
548 {
549         pci_unregister_driver(pci_driver);
550         comedi_driver_unregister(comedi_driver);
551 }
552 EXPORT_SYMBOL_GPL(comedi_pci_driver_unregister);
553
554 #if IS_ENABLED(CONFIG_PCMCIA)
555 int comedi_pcmcia_driver_register(struct comedi_driver *comedi_driver,
556                 struct pcmcia_driver *pcmcia_driver)
557 {
558         int ret;
559
560         ret = comedi_driver_register(comedi_driver);
561         if (ret < 0)
562                 return ret;
563
564         ret = pcmcia_register_driver(pcmcia_driver);
565         if (ret < 0) {
566                 comedi_driver_unregister(comedi_driver);
567                 return ret;
568         }
569
570         return 0;
571 }
572 EXPORT_SYMBOL_GPL(comedi_pcmcia_driver_register);
573
574 void comedi_pcmcia_driver_unregister(struct comedi_driver *comedi_driver,
575                 struct pcmcia_driver *pcmcia_driver)
576 {
577         pcmcia_unregister_driver(pcmcia_driver);
578         comedi_driver_unregister(comedi_driver);
579 }
580 EXPORT_SYMBOL_GPL(comedi_pcmcia_driver_unregister);
581
582 #endif
583
584 #if IS_ENABLED(CONFIG_USB)
585
586 int comedi_usb_driver_register(struct comedi_driver *comedi_driver,
587                 struct usb_driver *usb_driver)
588 {
589         int ret;
590
591         ret = comedi_driver_register(comedi_driver);
592         if (ret < 0)
593                 return ret;
594
595         ret = usb_register(usb_driver);
596         if (ret < 0) {
597                 comedi_driver_unregister(comedi_driver);
598                 return ret;
599         }
600
601         return 0;
602 }
603 EXPORT_SYMBOL_GPL(comedi_usb_driver_register);
604
605 void comedi_usb_driver_unregister(struct comedi_driver *comedi_driver,
606                 struct usb_driver *usb_driver)
607 {
608         usb_deregister(usb_driver);
609         comedi_driver_unregister(comedi_driver);
610 }
611 EXPORT_SYMBOL_GPL(comedi_usb_driver_unregister);
612
613 #endif