]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/staging/comedi/drivers.c
staging: comedi: cancel commands before detaching device
[linux.git] / drivers / staging / comedi / drivers.c
1 /*
2     module/drivers.c
3     functions for manipulating drivers
4
5     COMEDI - Linux Control and Measurement Device Interface
6     Copyright (C) 1997-2000 David A. Schleef <ds@schleef.org>
7
8     This program is free software; you can redistribute it and/or modify
9     it under the terms of the GNU General Public License as published by
10     the Free Software Foundation; either version 2 of the License, or
11     (at your option) any later version.
12
13     This program is distributed in the hope that it will be useful,
14     but WITHOUT ANY WARRANTY; without even the implied warranty of
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16     GNU General Public License for more details.
17 */
18
19 #include <linux/device.h>
20 #include <linux/module.h>
21 #include <linux/errno.h>
22 #include <linux/kconfig.h>
23 #include <linux/kernel.h>
24 #include <linux/sched.h>
25 #include <linux/fcntl.h>
26 #include <linux/ioport.h>
27 #include <linux/mm.h>
28 #include <linux/slab.h>
29 #include <linux/highmem.h>      /* for SuSE brokenness */
30 #include <linux/vmalloc.h>
31 #include <linux/cdev.h>
32 #include <linux/dma-mapping.h>
33 #include <linux/io.h>
34 #include <linux/interrupt.h>
35 #include <linux/firmware.h>
36
37 #include "comedidev.h"
38 #include "comedi_internal.h"
39
40 struct comedi_driver *comedi_drivers;
41 DEFINE_MUTEX(comedi_drivers_list_lock);
42
43 int comedi_set_hw_dev(struct comedi_device *dev, struct device *hw_dev)
44 {
45         if (hw_dev == dev->hw_dev)
46                 return 0;
47         if (dev->hw_dev != NULL)
48                 return -EEXIST;
49         dev->hw_dev = get_device(hw_dev);
50         return 0;
51 }
52 EXPORT_SYMBOL_GPL(comedi_set_hw_dev);
53
54 static void comedi_clear_hw_dev(struct comedi_device *dev)
55 {
56         put_device(dev->hw_dev);
57         dev->hw_dev = NULL;
58 }
59
60 /**
61  * comedi_alloc_devpriv() - Allocate memory for the device private data.
62  * @dev: comedi_device struct
63  * @size: size of the memory to allocate
64  */
65 void *comedi_alloc_devpriv(struct comedi_device *dev, size_t size)
66 {
67         dev->private = kzalloc(size, GFP_KERNEL);
68         return dev->private;
69 }
70 EXPORT_SYMBOL_GPL(comedi_alloc_devpriv);
71
72 int comedi_alloc_subdevices(struct comedi_device *dev, int num_subdevices)
73 {
74         struct comedi_subdevice *s;
75         int i;
76
77         if (num_subdevices < 1)
78                 return -EINVAL;
79
80         s = kcalloc(num_subdevices, sizeof(*s), GFP_KERNEL);
81         if (!s)
82                 return -ENOMEM;
83         dev->subdevices = s;
84         dev->n_subdevices = num_subdevices;
85
86         for (i = 0; i < num_subdevices; ++i) {
87                 s = &dev->subdevices[i];
88                 s->device = dev;
89                 s->index = i;
90                 s->async_dma_dir = DMA_NONE;
91                 spin_lock_init(&s->spin_lock);
92                 s->minor = -1;
93         }
94         return 0;
95 }
96 EXPORT_SYMBOL_GPL(comedi_alloc_subdevices);
97
98 static void comedi_device_detach_cleanup(struct comedi_device *dev)
99 {
100         int i;
101         struct comedi_subdevice *s;
102
103         if (dev->subdevices) {
104                 for (i = 0; i < dev->n_subdevices; i++) {
105                         s = &dev->subdevices[i];
106                         if (s->runflags & SRF_FREE_SPRIV)
107                                 kfree(s->private);
108                         comedi_free_subdevice_minor(s);
109                         if (s->async) {
110                                 comedi_buf_alloc(dev, s, 0);
111                                 kfree(s->async);
112                         }
113                 }
114                 kfree(dev->subdevices);
115                 dev->subdevices = NULL;
116                 dev->n_subdevices = 0;
117         }
118         kfree(dev->private);
119         dev->private = NULL;
120         dev->driver = NULL;
121         dev->board_name = NULL;
122         dev->board_ptr = NULL;
123         dev->iobase = 0;
124         dev->iolen = 0;
125         dev->ioenabled = false;
126         dev->irq = 0;
127         dev->read_subdev = NULL;
128         dev->write_subdev = NULL;
129         dev->open = NULL;
130         dev->close = NULL;
131         comedi_clear_hw_dev(dev);
132 }
133
134 void comedi_device_detach(struct comedi_device *dev)
135 {
136         comedi_device_cancel_all(dev);
137         down_write(&dev->attach_lock);
138         dev->attached = false;
139         if (dev->driver)
140                 dev->driver->detach(dev);
141         comedi_device_detach_cleanup(dev);
142         up_write(&dev->attach_lock);
143 }
144
145 static int poll_invalid(struct comedi_device *dev, struct comedi_subdevice *s)
146 {
147         return -EINVAL;
148 }
149
150 int insn_inval(struct comedi_device *dev, struct comedi_subdevice *s,
151                struct comedi_insn *insn, unsigned int *data)
152 {
153         return -EINVAL;
154 }
155
156 /**
157  * comedi_dio_insn_config() - boilerplate (*insn_config) for DIO subdevices.
158  * @dev: comedi_device struct
159  * @s: comedi_subdevice struct
160  * @insn: comedi_insn struct
161  * @data: parameters for the @insn
162  * @mask: io_bits mask for grouped channels
163  */
164 int comedi_dio_insn_config(struct comedi_device *dev,
165                            struct comedi_subdevice *s,
166                            struct comedi_insn *insn,
167                            unsigned int *data,
168                            unsigned int mask)
169 {
170         unsigned int chan_mask = 1 << CR_CHAN(insn->chanspec);
171
172         if (!mask)
173                 mask = chan_mask;
174
175         switch (data[0]) {
176         case INSN_CONFIG_DIO_INPUT:
177                 s->io_bits &= ~mask;
178                 break;
179
180         case INSN_CONFIG_DIO_OUTPUT:
181                 s->io_bits |= mask;
182                 break;
183
184         case INSN_CONFIG_DIO_QUERY:
185                 data[1] = (s->io_bits & mask) ? COMEDI_OUTPUT : COMEDI_INPUT;
186                 return insn->n;
187
188         default:
189                 return -EINVAL;
190         }
191
192         return 0;
193 }
194 EXPORT_SYMBOL_GPL(comedi_dio_insn_config);
195
196 /**
197  * comedi_dio_update_state() - update the internal state of DIO subdevices.
198  * @s: comedi_subdevice struct
199  * @data: the channel mask and bits to update
200  */
201 unsigned int comedi_dio_update_state(struct comedi_subdevice *s,
202                                      unsigned int *data)
203 {
204         unsigned int chanmask = (s->n_chan < 32) ? ((1 << s->n_chan) - 1)
205                                                  : 0xffffffff;
206         unsigned int mask = data[0] & chanmask;
207         unsigned int bits = data[1];
208
209         if (mask) {
210                 s->state &= ~mask;
211                 s->state |= (bits & mask);
212         }
213
214         return mask;
215 }
216 EXPORT_SYMBOL_GPL(comedi_dio_update_state);
217
218 static int insn_rw_emulate_bits(struct comedi_device *dev,
219                                 struct comedi_subdevice *s,
220                                 struct comedi_insn *insn, unsigned int *data)
221 {
222         struct comedi_insn new_insn;
223         int ret;
224         static const unsigned channels_per_bitfield = 32;
225
226         unsigned chan = CR_CHAN(insn->chanspec);
227         const unsigned base_bitfield_channel =
228             (chan < channels_per_bitfield) ? 0 : chan;
229         unsigned int new_data[2];
230         memset(new_data, 0, sizeof(new_data));
231         memset(&new_insn, 0, sizeof(new_insn));
232         new_insn.insn = INSN_BITS;
233         new_insn.chanspec = base_bitfield_channel;
234         new_insn.n = 2;
235         new_insn.subdev = insn->subdev;
236
237         if (insn->insn == INSN_WRITE) {
238                 if (!(s->subdev_flags & SDF_WRITABLE))
239                         return -EINVAL;
240                 new_data[0] = 1 << (chan - base_bitfield_channel); /* mask */
241                 new_data[1] = data[0] ? (1 << (chan - base_bitfield_channel))
242                               : 0; /* bits */
243         }
244
245         ret = s->insn_bits(dev, s, &new_insn, new_data);
246         if (ret < 0)
247                 return ret;
248
249         if (insn->insn == INSN_READ)
250                 data[0] = (new_data[1] >> (chan - base_bitfield_channel)) & 1;
251
252         return 1;
253 }
254
255 static int __comedi_device_postconfig_async(struct comedi_device *dev,
256                                             struct comedi_subdevice *s)
257 {
258         struct comedi_async *async;
259         unsigned int buf_size;
260         int ret;
261
262         if ((s->subdev_flags & (SDF_CMD_READ | SDF_CMD_WRITE)) == 0) {
263                 dev_warn(dev->class_dev,
264                          "async subdevices must support SDF_CMD_READ or SDF_CMD_WRITE\n");
265                 return -EINVAL;
266         }
267         if (!s->do_cmdtest) {
268                 dev_warn(dev->class_dev,
269                          "async subdevices must have a do_cmdtest() function\n");
270                 return -EINVAL;
271         }
272
273         async = kzalloc(sizeof(*async), GFP_KERNEL);
274         if (!async)
275                 return -ENOMEM;
276
277         init_waitqueue_head(&async->wait_head);
278         async->subdevice = s;
279         s->async = async;
280
281         async->max_bufsize = comedi_default_buf_maxsize_kb * 1024;
282         buf_size = comedi_default_buf_size_kb * 1024;
283         if (buf_size > async->max_bufsize)
284                 buf_size = async->max_bufsize;
285
286         if (comedi_buf_alloc(dev, s, buf_size) < 0) {
287                 dev_warn(dev->class_dev, "Buffer allocation failed\n");
288                 return -ENOMEM;
289         }
290         if (s->buf_change) {
291                 ret = s->buf_change(dev, s, buf_size);
292                 if (ret < 0)
293                         return ret;
294         }
295
296         comedi_alloc_subdevice_minor(s);
297
298         return 0;
299 }
300
301 static int __comedi_device_postconfig(struct comedi_device *dev)
302 {
303         struct comedi_subdevice *s;
304         int ret;
305         int i;
306
307         for (i = 0; i < dev->n_subdevices; i++) {
308                 s = &dev->subdevices[i];
309
310                 if (s->type == COMEDI_SUBD_UNUSED)
311                         continue;
312
313                 if (s->type == COMEDI_SUBD_DO) {
314                         if (s->n_chan < 32)
315                                 s->io_bits = (1 << s->n_chan) - 1;
316                         else
317                                 s->io_bits = 0xffffffff;
318                 }
319
320                 if (s->len_chanlist == 0)
321                         s->len_chanlist = 1;
322
323                 if (s->do_cmd) {
324                         ret = __comedi_device_postconfig_async(dev, s);
325                         if (ret)
326                                 return ret;
327                 }
328
329                 if (!s->range_table && !s->range_table_list)
330                         s->range_table = &range_unknown;
331
332                 if (!s->insn_read && s->insn_bits)
333                         s->insn_read = insn_rw_emulate_bits;
334                 if (!s->insn_write && s->insn_bits)
335                         s->insn_write = insn_rw_emulate_bits;
336
337                 if (!s->insn_read)
338                         s->insn_read = insn_inval;
339                 if (!s->insn_write)
340                         s->insn_write = insn_inval;
341                 if (!s->insn_bits)
342                         s->insn_bits = insn_inval;
343                 if (!s->insn_config)
344                         s->insn_config = insn_inval;
345
346                 if (!s->poll)
347                         s->poll = poll_invalid;
348         }
349
350         return 0;
351 }
352
353 /* do a little post-config cleanup */
354 static int comedi_device_postconfig(struct comedi_device *dev)
355 {
356         int ret;
357
358         ret = __comedi_device_postconfig(dev);
359         if (ret < 0)
360                 return ret;
361         down_write(&dev->attach_lock);
362         dev->attached = true;
363         up_write(&dev->attach_lock);
364         return 0;
365 }
366
367 /*
368  * Generic recognize function for drivers that register their supported
369  * board names.
370  *
371  * 'driv->board_name' points to a 'const char *' member within the
372  * zeroth element of an array of some private board information
373  * structure, say 'struct foo_board' containing a member 'const char
374  * *board_name' that is initialized to point to a board name string that
375  * is one of the candidates matched against this function's 'name'
376  * parameter.
377  *
378  * 'driv->offset' is the size of the private board information
379  * structure, say 'sizeof(struct foo_board)', and 'driv->num_names' is
380  * the length of the array of private board information structures.
381  *
382  * If one of the board names in the array of private board information
383  * structures matches the name supplied to this function, the function
384  * returns a pointer to the pointer to the board name, otherwise it
385  * returns NULL.  The return value ends up in the 'board_ptr' member of
386  * a 'struct comedi_device' that the low-level comedi driver's
387  * 'attach()' hook can convert to a point to a particular element of its
388  * array of private board information structures by subtracting the
389  * offset of the member that points to the board name.  (No subtraction
390  * is required if the board name pointer is the first member of the
391  * private board information structure, which is generally the case.)
392  */
393 static void *comedi_recognize(struct comedi_driver *driv, const char *name)
394 {
395         char **name_ptr = (char **)driv->board_name;
396         int i;
397
398         for (i = 0; i < driv->num_names; i++) {
399                 if (strcmp(*name_ptr, name) == 0)
400                         return name_ptr;
401                 name_ptr = (void *)name_ptr + driv->offset;
402         }
403
404         return NULL;
405 }
406
407 static void comedi_report_boards(struct comedi_driver *driv)
408 {
409         unsigned int i;
410         const char *const *name_ptr;
411
412         pr_info("comedi: valid board names for %s driver are:\n",
413                 driv->driver_name);
414
415         name_ptr = driv->board_name;
416         for (i = 0; i < driv->num_names; i++) {
417                 pr_info(" %s\n", *name_ptr);
418                 name_ptr = (const char **)((char *)name_ptr + driv->offset);
419         }
420
421         if (driv->num_names == 0)
422                 pr_info(" %s\n", driv->driver_name);
423 }
424
425 /**
426  * comedi_load_firmware() - Request and load firmware for a device.
427  * @dev: comedi_device struct
428  * @hw_device: device struct for the comedi_device
429  * @name: the name of the firmware image
430  * @cb: callback to the upload the firmware image
431  * @context: private context from the driver
432  */
433 int comedi_load_firmware(struct comedi_device *dev,
434                          struct device *device,
435                          const char *name,
436                          int (*cb)(struct comedi_device *dev,
437                                    const u8 *data, size_t size,
438                                    unsigned long context),
439                          unsigned long context)
440 {
441         const struct firmware *fw;
442         int ret;
443
444         if (!cb)
445                 return -EINVAL;
446
447         ret = request_firmware(&fw, name, device);
448         if (ret == 0) {
449                 ret = cb(dev, fw->data, fw->size, context);
450                 release_firmware(fw);
451         }
452
453         return ret;
454 }
455 EXPORT_SYMBOL_GPL(comedi_load_firmware);
456
457 /**
458  * __comedi_request_region() - Request an I/O reqion for a legacy driver.
459  * @dev: comedi_device struct
460  * @start: base address of the I/O reqion
461  * @len: length of the I/O region
462  */
463 int __comedi_request_region(struct comedi_device *dev,
464                             unsigned long start, unsigned long len)
465 {
466         if (!start) {
467                 dev_warn(dev->class_dev,
468                          "%s: a I/O base address must be specified\n",
469                          dev->board_name);
470                 return -EINVAL;
471         }
472
473         if (!request_region(start, len, dev->board_name)) {
474                 dev_warn(dev->class_dev, "%s: I/O port conflict (%#lx,%lu)\n",
475                          dev->board_name, start, len);
476                 return -EIO;
477         }
478
479         return 0;
480 }
481 EXPORT_SYMBOL_GPL(__comedi_request_region);
482
483 /**
484  * comedi_request_region() - Request an I/O reqion for a legacy driver.
485  * @dev: comedi_device struct
486  * @start: base address of the I/O reqion
487  * @len: length of the I/O region
488  */
489 int comedi_request_region(struct comedi_device *dev,
490                           unsigned long start, unsigned long len)
491 {
492         int ret;
493
494         ret = __comedi_request_region(dev, start, len);
495         if (ret == 0) {
496                 dev->iobase = start;
497                 dev->iolen = len;
498         }
499
500         return ret;
501 }
502 EXPORT_SYMBOL_GPL(comedi_request_region);
503
504 /**
505  * comedi_legacy_detach() - A generic (*detach) function for legacy drivers.
506  * @dev: comedi_device struct
507  */
508 void comedi_legacy_detach(struct comedi_device *dev)
509 {
510         if (dev->irq) {
511                 free_irq(dev->irq, dev);
512                 dev->irq = 0;
513         }
514         if (dev->iobase && dev->iolen) {
515                 release_region(dev->iobase, dev->iolen);
516                 dev->iobase = 0;
517                 dev->iolen = 0;
518         }
519 }
520 EXPORT_SYMBOL_GPL(comedi_legacy_detach);
521
522 int comedi_device_attach(struct comedi_device *dev, struct comedi_devconfig *it)
523 {
524         struct comedi_driver *driv;
525         int ret;
526
527         if (dev->attached)
528                 return -EBUSY;
529
530         mutex_lock(&comedi_drivers_list_lock);
531         for (driv = comedi_drivers; driv; driv = driv->next) {
532                 if (!try_module_get(driv->module))
533                         continue;
534                 if (driv->num_names) {
535                         dev->board_ptr = comedi_recognize(driv, it->board_name);
536                         if (dev->board_ptr)
537                                 break;
538                 } else if (strcmp(driv->driver_name, it->board_name) == 0)
539                         break;
540                 module_put(driv->module);
541         }
542         if (driv == NULL) {
543                 /*  recognize has failed if we get here */
544                 /*  report valid board names before returning error */
545                 for (driv = comedi_drivers; driv; driv = driv->next) {
546                         if (!try_module_get(driv->module))
547                                 continue;
548                         comedi_report_boards(driv);
549                         module_put(driv->module);
550                 }
551                 ret = -EIO;
552                 goto out;
553         }
554         if (driv->attach == NULL) {
555                 /* driver does not support manual configuration */
556                 dev_warn(dev->class_dev,
557                          "driver '%s' does not support attach using comedi_config\n",
558                          driv->driver_name);
559                 module_put(driv->module);
560                 ret = -ENOSYS;
561                 goto out;
562         }
563         /* initialize dev->driver here so
564          * comedi_error() can be called from attach */
565         dev->driver = driv;
566         dev->board_name = dev->board_ptr ? *(const char **)dev->board_ptr
567                                          : dev->driver->driver_name;
568         ret = driv->attach(dev, it);
569         if (ret >= 0)
570                 ret = comedi_device_postconfig(dev);
571         if (ret < 0) {
572                 comedi_device_detach(dev);
573                 module_put(driv->module);
574         }
575         /* On success, the driver module count has been incremented. */
576 out:
577         mutex_unlock(&comedi_drivers_list_lock);
578         return ret;
579 }
580
581 int comedi_auto_config(struct device *hardware_device,
582                        struct comedi_driver *driver, unsigned long context)
583 {
584         struct comedi_device *dev;
585         int ret;
586
587         if (!hardware_device) {
588                 pr_warn("BUG! comedi_auto_config called with NULL hardware_device\n");
589                 return -EINVAL;
590         }
591         if (!driver) {
592                 dev_warn(hardware_device,
593                          "BUG! comedi_auto_config called with NULL comedi driver\n");
594                 return -EINVAL;
595         }
596
597         if (!driver->auto_attach) {
598                 dev_warn(hardware_device,
599                          "BUG! comedi driver '%s' has no auto_attach handler\n",
600                          driver->driver_name);
601                 return -EINVAL;
602         }
603
604         dev = comedi_alloc_board_minor(hardware_device);
605         if (IS_ERR(dev))
606                 return PTR_ERR(dev);
607         /* Note: comedi_alloc_board_minor() locked dev->mutex. */
608
609         dev->driver = driver;
610         dev->board_name = dev->driver->driver_name;
611         ret = driver->auto_attach(dev, context);
612         if (ret >= 0)
613                 ret = comedi_device_postconfig(dev);
614         if (ret < 0)
615                 comedi_device_detach(dev);
616         mutex_unlock(&dev->mutex);
617
618         if (ret < 0)
619                 comedi_release_hardware_device(hardware_device);
620         return ret;
621 }
622 EXPORT_SYMBOL_GPL(comedi_auto_config);
623
624 void comedi_auto_unconfig(struct device *hardware_device)
625 {
626         if (hardware_device == NULL)
627                 return;
628         comedi_release_hardware_device(hardware_device);
629 }
630 EXPORT_SYMBOL_GPL(comedi_auto_unconfig);
631
632 int comedi_driver_register(struct comedi_driver *driver)
633 {
634         mutex_lock(&comedi_drivers_list_lock);
635         driver->next = comedi_drivers;
636         comedi_drivers = driver;
637         mutex_unlock(&comedi_drivers_list_lock);
638
639         return 0;
640 }
641 EXPORT_SYMBOL_GPL(comedi_driver_register);
642
643 void comedi_driver_unregister(struct comedi_driver *driver)
644 {
645         struct comedi_driver *prev;
646         int i;
647
648         /* unlink the driver */
649         mutex_lock(&comedi_drivers_list_lock);
650         if (comedi_drivers == driver) {
651                 comedi_drivers = driver->next;
652         } else {
653                 for (prev = comedi_drivers; prev->next; prev = prev->next) {
654                         if (prev->next == driver) {
655                                 prev->next = driver->next;
656                                 break;
657                         }
658                 }
659         }
660         mutex_unlock(&comedi_drivers_list_lock);
661
662         /* check for devices using this driver */
663         for (i = 0; i < COMEDI_NUM_BOARD_MINORS; i++) {
664                 struct comedi_device *dev = comedi_dev_from_minor(i);
665
666                 if (!dev)
667                         continue;
668
669                 mutex_lock(&dev->mutex);
670                 if (dev->attached && dev->driver == driver) {
671                         if (dev->use_count)
672                                 dev_warn(dev->class_dev,
673                                          "BUG! detaching device with use_count=%d\n",
674                                          dev->use_count);
675                         comedi_device_detach(dev);
676                 }
677                 mutex_unlock(&dev->mutex);
678         }
679 }
680 EXPORT_SYMBOL_GPL(comedi_driver_unregister);