Add the rt linux 4.1.3-rt3 as base
[kvmfornfv.git] / kernel / 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     Copyright (C) 2002 Frank Mori Hess <fmhess@users.sourceforge.net>
8
9     This program is free software; you can redistribute it and/or modify
10     it under the terms of the GNU General Public License as published by
11     the Free Software Foundation; either version 2 of the License, or
12     (at your option) any later version.
13
14     This program is distributed in the hope that it will be useful,
15     but WITHOUT ANY WARRANTY; without even the implied warranty of
16     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17     GNU General Public License for more details.
18 */
19
20 #include <linux/device.h>
21 #include <linux/module.h>
22 #include <linux/errno.h>
23 #include <linux/kconfig.h>
24 #include <linux/kernel.h>
25 #include <linux/sched.h>
26 #include <linux/fcntl.h>
27 #include <linux/ioport.h>
28 #include <linux/mm.h>
29 #include <linux/slab.h>
30 #include <linux/highmem.h>      /* for SuSE brokenness */
31 #include <linux/vmalloc.h>
32 #include <linux/cdev.h>
33 #include <linux/dma-mapping.h>
34 #include <linux/io.h>
35 #include <linux/interrupt.h>
36 #include <linux/firmware.h>
37
38 #include "comedidev.h"
39 #include "comedi_internal.h"
40
41 struct comedi_driver *comedi_drivers;
42 /* protects access to comedi_drivers */
43 DEFINE_MUTEX(comedi_drivers_list_lock);
44
45 int comedi_set_hw_dev(struct comedi_device *dev, struct device *hw_dev)
46 {
47         if (hw_dev == dev->hw_dev)
48                 return 0;
49         if (dev->hw_dev)
50                 return -EEXIST;
51         dev->hw_dev = get_device(hw_dev);
52         return 0;
53 }
54 EXPORT_SYMBOL_GPL(comedi_set_hw_dev);
55
56 static void comedi_clear_hw_dev(struct comedi_device *dev)
57 {
58         put_device(dev->hw_dev);
59         dev->hw_dev = NULL;
60 }
61
62 /**
63  * comedi_alloc_devpriv() - Allocate memory for the device private data.
64  * @dev: comedi_device struct
65  * @size: size of the memory to allocate
66  */
67 void *comedi_alloc_devpriv(struct comedi_device *dev, size_t size)
68 {
69         dev->private = kzalloc(size, GFP_KERNEL);
70         return dev->private;
71 }
72 EXPORT_SYMBOL_GPL(comedi_alloc_devpriv);
73
74 int comedi_alloc_subdevices(struct comedi_device *dev, int num_subdevices)
75 {
76         struct comedi_subdevice *s;
77         int i;
78
79         if (num_subdevices < 1)
80                 return -EINVAL;
81
82         s = kcalloc(num_subdevices, sizeof(*s), GFP_KERNEL);
83         if (!s)
84                 return -ENOMEM;
85         dev->subdevices = s;
86         dev->n_subdevices = num_subdevices;
87
88         for (i = 0; i < num_subdevices; ++i) {
89                 s = &dev->subdevices[i];
90                 s->device = dev;
91                 s->index = i;
92                 s->async_dma_dir = DMA_NONE;
93                 spin_lock_init(&s->spin_lock);
94                 s->minor = -1;
95         }
96         return 0;
97 }
98 EXPORT_SYMBOL_GPL(comedi_alloc_subdevices);
99
100 /**
101  * comedi_alloc_subdev_readback() - Allocate memory for the subdevice readback.
102  * @s: comedi_subdevice struct
103  */
104 int comedi_alloc_subdev_readback(struct comedi_subdevice *s)
105 {
106         if (!s->n_chan)
107                 return -EINVAL;
108
109         s->readback = kcalloc(s->n_chan, sizeof(*s->readback), GFP_KERNEL);
110         if (!s->readback)
111                 return -ENOMEM;
112
113         if (!s->insn_read)
114                 s->insn_read = comedi_readback_insn_read;
115
116         return 0;
117 }
118 EXPORT_SYMBOL_GPL(comedi_alloc_subdev_readback);
119
120 static void comedi_device_detach_cleanup(struct comedi_device *dev)
121 {
122         int i;
123         struct comedi_subdevice *s;
124
125         if (dev->subdevices) {
126                 for (i = 0; i < dev->n_subdevices; i++) {
127                         s = &dev->subdevices[i];
128                         if (s->runflags & COMEDI_SRF_FREE_SPRIV)
129                                 kfree(s->private);
130                         comedi_free_subdevice_minor(s);
131                         if (s->async) {
132                                 comedi_buf_alloc(dev, s, 0);
133                                 kfree(s->async);
134                         }
135                         kfree(s->readback);
136                 }
137                 kfree(dev->subdevices);
138                 dev->subdevices = NULL;
139                 dev->n_subdevices = 0;
140         }
141         kfree(dev->private);
142         kfree(dev->pacer);
143         dev->private = NULL;
144         dev->pacer = NULL;
145         dev->driver = NULL;
146         dev->board_name = NULL;
147         dev->board_ptr = NULL;
148         dev->mmio = NULL;
149         dev->iobase = 0;
150         dev->iolen = 0;
151         dev->ioenabled = false;
152         dev->irq = 0;
153         dev->read_subdev = NULL;
154         dev->write_subdev = NULL;
155         dev->open = NULL;
156         dev->close = NULL;
157         comedi_clear_hw_dev(dev);
158 }
159
160 void comedi_device_detach(struct comedi_device *dev)
161 {
162         comedi_device_cancel_all(dev);
163         down_write(&dev->attach_lock);
164         dev->attached = false;
165         dev->detach_count++;
166         if (dev->driver)
167                 dev->driver->detach(dev);
168         comedi_device_detach_cleanup(dev);
169         up_write(&dev->attach_lock);
170 }
171
172 static int poll_invalid(struct comedi_device *dev, struct comedi_subdevice *s)
173 {
174         return -EINVAL;
175 }
176
177 int insn_inval(struct comedi_device *dev, struct comedi_subdevice *s,
178                struct comedi_insn *insn, unsigned int *data)
179 {
180         return -EINVAL;
181 }
182
183 /**
184  * comedi_readback_insn_read() - A generic (*insn_read) for subdevice readback.
185  * @dev: comedi_device struct
186  * @s: comedi_subdevice struct
187  * @insn: comedi_insn struct
188  * @data: pointer to return the readback data
189  */
190 int comedi_readback_insn_read(struct comedi_device *dev,
191                               struct comedi_subdevice *s,
192                               struct comedi_insn *insn,
193                               unsigned int *data)
194 {
195         unsigned int chan = CR_CHAN(insn->chanspec);
196         int i;
197
198         if (!s->readback)
199                 return -EINVAL;
200
201         for (i = 0; i < insn->n; i++)
202                 data[i] = s->readback[chan];
203
204         return insn->n;
205 }
206 EXPORT_SYMBOL_GPL(comedi_readback_insn_read);
207
208 /**
209  * comedi_timeout() - busy-wait for a driver condition to occur.
210  * @dev: comedi_device struct
211  * @s: comedi_subdevice struct
212  * @insn: comedi_insn struct
213  * @cb: callback to check for the condition
214  * @context: private context from the driver
215  */
216 int comedi_timeout(struct comedi_device *dev,
217                    struct comedi_subdevice *s,
218                    struct comedi_insn *insn,
219                    int (*cb)(struct comedi_device *dev,
220                              struct comedi_subdevice *s,
221                              struct comedi_insn *insn,
222                              unsigned long context),
223                    unsigned long context)
224 {
225         unsigned long timeout = jiffies + msecs_to_jiffies(COMEDI_TIMEOUT_MS);
226         int ret;
227
228         while (time_before(jiffies, timeout)) {
229                 ret = cb(dev, s, insn, context);
230                 if (ret != -EBUSY)
231                         return ret;     /* success (0) or non EBUSY errno */
232                 cpu_relax();
233         }
234         return -ETIMEDOUT;
235 }
236 EXPORT_SYMBOL_GPL(comedi_timeout);
237
238 /**
239  * comedi_dio_insn_config() - boilerplate (*insn_config) for DIO subdevices.
240  * @dev: comedi_device struct
241  * @s: comedi_subdevice struct
242  * @insn: comedi_insn struct
243  * @data: parameters for the @insn
244  * @mask: io_bits mask for grouped channels
245  */
246 int comedi_dio_insn_config(struct comedi_device *dev,
247                            struct comedi_subdevice *s,
248                            struct comedi_insn *insn,
249                            unsigned int *data,
250                            unsigned int mask)
251 {
252         unsigned int chan_mask = 1 << CR_CHAN(insn->chanspec);
253
254         if (!mask)
255                 mask = chan_mask;
256
257         switch (data[0]) {
258         case INSN_CONFIG_DIO_INPUT:
259                 s->io_bits &= ~mask;
260                 break;
261
262         case INSN_CONFIG_DIO_OUTPUT:
263                 s->io_bits |= mask;
264                 break;
265
266         case INSN_CONFIG_DIO_QUERY:
267                 data[1] = (s->io_bits & mask) ? COMEDI_OUTPUT : COMEDI_INPUT;
268                 return insn->n;
269
270         default:
271                 return -EINVAL;
272         }
273
274         return 0;
275 }
276 EXPORT_SYMBOL_GPL(comedi_dio_insn_config);
277
278 /**
279  * comedi_dio_update_state() - update the internal state of DIO subdevices.
280  * @s: comedi_subdevice struct
281  * @data: the channel mask and bits to update
282  */
283 unsigned int comedi_dio_update_state(struct comedi_subdevice *s,
284                                      unsigned int *data)
285 {
286         unsigned int chanmask = (s->n_chan < 32) ? ((1 << s->n_chan) - 1)
287                                                  : 0xffffffff;
288         unsigned int mask = data[0] & chanmask;
289         unsigned int bits = data[1];
290
291         if (mask) {
292                 s->state &= ~mask;
293                 s->state |= (bits & mask);
294         }
295
296         return mask;
297 }
298 EXPORT_SYMBOL_GPL(comedi_dio_update_state);
299
300 /**
301  * comedi_bytes_per_scan - get length of asynchronous command "scan" in bytes
302  * @s: comedi_subdevice struct
303  *
304  * Determines the overall scan length according to the subdevice type and the
305  * number of channels in the scan.
306  *
307  * For digital input, output or input/output subdevices, samples for multiple
308  * channels are assumed to be packed into one or more unsigned short or
309  * unsigned int values according to the subdevice's SDF_LSAMPL flag.  For other
310  * types of subdevice, samples are assumed to occupy a whole unsigned short or
311  * unsigned int according to the SDF_LSAMPL flag.
312  *
313  * Returns the overall scan length in bytes.
314  */
315 unsigned int comedi_bytes_per_scan(struct comedi_subdevice *s)
316 {
317         struct comedi_cmd *cmd = &s->async->cmd;
318         unsigned int num_samples;
319         unsigned int bits_per_sample;
320
321         switch (s->type) {
322         case COMEDI_SUBD_DI:
323         case COMEDI_SUBD_DO:
324         case COMEDI_SUBD_DIO:
325                 bits_per_sample = 8 * comedi_bytes_per_sample(s);
326                 num_samples = DIV_ROUND_UP(cmd->scan_end_arg, bits_per_sample);
327                 break;
328         default:
329                 num_samples = cmd->scan_end_arg;
330                 break;
331         }
332         return comedi_samples_to_bytes(s, num_samples);
333 }
334 EXPORT_SYMBOL_GPL(comedi_bytes_per_scan);
335
336 /**
337  * comedi_nscans_left - return the number of scans left in the command
338  * @s: comedi_subdevice struct
339  * @nscans: the expected number of scans
340  *
341  * If nscans is 0, the number of scans available in the async buffer will be
342  * used. Otherwise the expected number of scans will be used.
343  *
344  * If the async command has a stop_src of TRIG_COUNT, the nscans will be
345  * checked against the number of scans left in the command.
346  *
347  * The return value will then be either the expected number of scans or the
348  * number of scans remaining in the command.
349  */
350 unsigned int comedi_nscans_left(struct comedi_subdevice *s,
351                                 unsigned int nscans)
352 {
353         struct comedi_async *async = s->async;
354         struct comedi_cmd *cmd = &async->cmd;
355
356         if (nscans == 0) {
357                 unsigned int nbytes = comedi_buf_read_n_available(s);
358
359                 nscans = nbytes / comedi_bytes_per_scan(s);
360         }
361
362         if (cmd->stop_src == TRIG_COUNT) {
363                 unsigned int scans_left = 0;
364
365                 if (async->scans_done < cmd->stop_arg)
366                         scans_left = cmd->stop_arg - async->scans_done;
367
368                 if (nscans > scans_left)
369                         nscans = scans_left;
370         }
371         return nscans;
372 }
373 EXPORT_SYMBOL_GPL(comedi_nscans_left);
374
375 /**
376  * comedi_nsamples_left - return the number of samples left in the command
377  * @s: comedi_subdevice struct
378  * @nsamples: the expected number of samples
379  *
380  * Returns the expected number of samples of the number of samples remaining
381  * in the command.
382  */
383 unsigned int comedi_nsamples_left(struct comedi_subdevice *s,
384                                   unsigned int nsamples)
385 {
386         struct comedi_async *async = s->async;
387         struct comedi_cmd *cmd = &async->cmd;
388
389         if (cmd->stop_src == TRIG_COUNT) {
390                 /* +1 to force comedi_nscans_left() to return the scans left */
391                 unsigned int nscans = (nsamples / cmd->scan_end_arg) + 1;
392                 unsigned int scans_left = comedi_nscans_left(s, nscans);
393                 unsigned int scan_pos =
394                     comedi_bytes_to_samples(s, async->scan_progress);
395                 unsigned long long samples_left = 0;
396
397                 if (scans_left) {
398                         samples_left = ((unsigned long long)scans_left *
399                                         cmd->scan_end_arg) - scan_pos;
400                 }
401
402                 if (samples_left < nsamples)
403                         nsamples = samples_left;
404         }
405         return nsamples;
406 }
407 EXPORT_SYMBOL_GPL(comedi_nsamples_left);
408
409 /**
410  * comedi_inc_scan_progress - update scan progress in asynchronous command
411  * @s: comedi_subdevice struct
412  * @num_bytes: amount of data in bytes to increment scan progress
413  *
414  * Increments the scan progress by the number of bytes specified by num_bytes.
415  * If the scan progress reaches or exceeds the scan length in bytes, reduce
416  * it modulo the scan length in bytes and set the "end of scan" asynchronous
417  * event flag to be processed later.
418  */
419 void comedi_inc_scan_progress(struct comedi_subdevice *s,
420                               unsigned int num_bytes)
421 {
422         struct comedi_async *async = s->async;
423         struct comedi_cmd *cmd = &async->cmd;
424         unsigned int scan_length = comedi_bytes_per_scan(s);
425
426         /* track the 'cur_chan' for non-SDF_PACKED subdevices */
427         if (!(s->subdev_flags & SDF_PACKED)) {
428                 async->cur_chan += comedi_bytes_to_samples(s, num_bytes);
429                 async->cur_chan %= cmd->chanlist_len;
430         }
431
432         async->scan_progress += num_bytes;
433         if (async->scan_progress >= scan_length) {
434                 unsigned int nscans = async->scan_progress / scan_length;
435
436                 if (async->scans_done < (UINT_MAX - nscans))
437                         async->scans_done += nscans;
438                 else
439                         async->scans_done = UINT_MAX;
440
441                 async->scan_progress %= scan_length;
442                 async->events |= COMEDI_CB_EOS;
443         }
444 }
445 EXPORT_SYMBOL_GPL(comedi_inc_scan_progress);
446
447 /**
448  * comedi_handle_events - handle events and possibly stop acquisition
449  * @dev: comedi_device struct
450  * @s: comedi_subdevice struct
451  *
452  * Handles outstanding asynchronous acquisition event flags associated
453  * with the subdevice.  Call the subdevice's "->cancel()" handler if the
454  * "end of acquisition", "error" or "overflow" event flags are set in order
455  * to stop the acquisition at the driver level.
456  *
457  * Calls comedi_event() to further process the event flags, which may mark
458  * the asynchronous command as no longer running, possibly terminated with
459  * an error, and may wake up tasks.
460  *
461  * Return a bit-mask of the handled events.
462  */
463 unsigned int comedi_handle_events(struct comedi_device *dev,
464                                   struct comedi_subdevice *s)
465 {
466         unsigned int events = s->async->events;
467
468         if (events == 0)
469                 return events;
470
471         if (events & COMEDI_CB_CANCEL_MASK)
472                 s->cancel(dev, s);
473
474         comedi_event(dev, s);
475
476         return events;
477 }
478 EXPORT_SYMBOL_GPL(comedi_handle_events);
479
480 static int insn_rw_emulate_bits(struct comedi_device *dev,
481                                 struct comedi_subdevice *s,
482                                 struct comedi_insn *insn, unsigned int *data)
483 {
484         struct comedi_insn new_insn;
485         int ret;
486         static const unsigned channels_per_bitfield = 32;
487
488         unsigned chan = CR_CHAN(insn->chanspec);
489         const unsigned base_bitfield_channel =
490             (chan < channels_per_bitfield) ? 0 : chan;
491         unsigned int new_data[2];
492
493         memset(new_data, 0, sizeof(new_data));
494         memset(&new_insn, 0, sizeof(new_insn));
495         new_insn.insn = INSN_BITS;
496         new_insn.chanspec = base_bitfield_channel;
497         new_insn.n = 2;
498         new_insn.subdev = insn->subdev;
499
500         if (insn->insn == INSN_WRITE) {
501                 if (!(s->subdev_flags & SDF_WRITABLE))
502                         return -EINVAL;
503                 new_data[0] = 1 << (chan - base_bitfield_channel); /* mask */
504                 new_data[1] = data[0] ? (1 << (chan - base_bitfield_channel))
505                               : 0; /* bits */
506         }
507
508         ret = s->insn_bits(dev, s, &new_insn, new_data);
509         if (ret < 0)
510                 return ret;
511
512         if (insn->insn == INSN_READ)
513                 data[0] = (new_data[1] >> (chan - base_bitfield_channel)) & 1;
514
515         return 1;
516 }
517
518 static int __comedi_device_postconfig_async(struct comedi_device *dev,
519                                             struct comedi_subdevice *s)
520 {
521         struct comedi_async *async;
522         unsigned int buf_size;
523         int ret;
524
525         if ((s->subdev_flags & (SDF_CMD_READ | SDF_CMD_WRITE)) == 0) {
526                 dev_warn(dev->class_dev,
527                          "async subdevices must support SDF_CMD_READ or SDF_CMD_WRITE\n");
528                 return -EINVAL;
529         }
530         if (!s->do_cmdtest) {
531                 dev_warn(dev->class_dev,
532                          "async subdevices must have a do_cmdtest() function\n");
533                 return -EINVAL;
534         }
535
536         async = kzalloc(sizeof(*async), GFP_KERNEL);
537         if (!async)
538                 return -ENOMEM;
539
540         init_waitqueue_head(&async->wait_head);
541         s->async = async;
542
543         async->max_bufsize = comedi_default_buf_maxsize_kb * 1024;
544         buf_size = comedi_default_buf_size_kb * 1024;
545         if (buf_size > async->max_bufsize)
546                 buf_size = async->max_bufsize;
547
548         if (comedi_buf_alloc(dev, s, buf_size) < 0) {
549                 dev_warn(dev->class_dev, "Buffer allocation failed\n");
550                 return -ENOMEM;
551         }
552         if (s->buf_change) {
553                 ret = s->buf_change(dev, s);
554                 if (ret < 0)
555                         return ret;
556         }
557
558         comedi_alloc_subdevice_minor(s);
559
560         return 0;
561 }
562
563 static int __comedi_device_postconfig(struct comedi_device *dev)
564 {
565         struct comedi_subdevice *s;
566         int ret;
567         int i;
568
569         for (i = 0; i < dev->n_subdevices; i++) {
570                 s = &dev->subdevices[i];
571
572                 if (s->type == COMEDI_SUBD_UNUSED)
573                         continue;
574
575                 if (s->type == COMEDI_SUBD_DO) {
576                         if (s->n_chan < 32)
577                                 s->io_bits = (1 << s->n_chan) - 1;
578                         else
579                                 s->io_bits = 0xffffffff;
580                 }
581
582                 if (s->len_chanlist == 0)
583                         s->len_chanlist = 1;
584
585                 if (s->do_cmd) {
586                         ret = __comedi_device_postconfig_async(dev, s);
587                         if (ret)
588                                 return ret;
589                 }
590
591                 if (!s->range_table && !s->range_table_list)
592                         s->range_table = &range_unknown;
593
594                 if (!s->insn_read && s->insn_bits)
595                         s->insn_read = insn_rw_emulate_bits;
596                 if (!s->insn_write && s->insn_bits)
597                         s->insn_write = insn_rw_emulate_bits;
598
599                 if (!s->insn_read)
600                         s->insn_read = insn_inval;
601                 if (!s->insn_write)
602                         s->insn_write = insn_inval;
603                 if (!s->insn_bits)
604                         s->insn_bits = insn_inval;
605                 if (!s->insn_config)
606                         s->insn_config = insn_inval;
607
608                 if (!s->poll)
609                         s->poll = poll_invalid;
610         }
611
612         return 0;
613 }
614
615 /* do a little post-config cleanup */
616 static int comedi_device_postconfig(struct comedi_device *dev)
617 {
618         int ret;
619
620         ret = __comedi_device_postconfig(dev);
621         if (ret < 0)
622                 return ret;
623         down_write(&dev->attach_lock);
624         dev->attached = true;
625         up_write(&dev->attach_lock);
626         return 0;
627 }
628
629 /*
630  * Generic recognize function for drivers that register their supported
631  * board names.
632  *
633  * 'driv->board_name' points to a 'const char *' member within the
634  * zeroth element of an array of some private board information
635  * structure, say 'struct foo_board' containing a member 'const char
636  * *board_name' that is initialized to point to a board name string that
637  * is one of the candidates matched against this function's 'name'
638  * parameter.
639  *
640  * 'driv->offset' is the size of the private board information
641  * structure, say 'sizeof(struct foo_board)', and 'driv->num_names' is
642  * the length of the array of private board information structures.
643  *
644  * If one of the board names in the array of private board information
645  * structures matches the name supplied to this function, the function
646  * returns a pointer to the pointer to the board name, otherwise it
647  * returns NULL.  The return value ends up in the 'board_ptr' member of
648  * a 'struct comedi_device' that the low-level comedi driver's
649  * 'attach()' hook can convert to a point to a particular element of its
650  * array of private board information structures by subtracting the
651  * offset of the member that points to the board name.  (No subtraction
652  * is required if the board name pointer is the first member of the
653  * private board information structure, which is generally the case.)
654  */
655 static void *comedi_recognize(struct comedi_driver *driv, const char *name)
656 {
657         char **name_ptr = (char **)driv->board_name;
658         int i;
659
660         for (i = 0; i < driv->num_names; i++) {
661                 if (strcmp(*name_ptr, name) == 0)
662                         return name_ptr;
663                 name_ptr = (void *)name_ptr + driv->offset;
664         }
665
666         return NULL;
667 }
668
669 static void comedi_report_boards(struct comedi_driver *driv)
670 {
671         unsigned int i;
672         const char *const *name_ptr;
673
674         pr_info("comedi: valid board names for %s driver are:\n",
675                 driv->driver_name);
676
677         name_ptr = driv->board_name;
678         for (i = 0; i < driv->num_names; i++) {
679                 pr_info(" %s\n", *name_ptr);
680                 name_ptr = (const char **)((char *)name_ptr + driv->offset);
681         }
682
683         if (driv->num_names == 0)
684                 pr_info(" %s\n", driv->driver_name);
685 }
686
687 /**
688  * comedi_load_firmware() - Request and load firmware for a device.
689  * @dev: comedi_device struct
690  * @hw_device: device struct for the comedi_device
691  * @name: the name of the firmware image
692  * @cb: callback to the upload the firmware image
693  * @context: private context from the driver
694  */
695 int comedi_load_firmware(struct comedi_device *dev,
696                          struct device *device,
697                          const char *name,
698                          int (*cb)(struct comedi_device *dev,
699                                    const u8 *data, size_t size,
700                                    unsigned long context),
701                          unsigned long context)
702 {
703         const struct firmware *fw;
704         int ret;
705
706         if (!cb)
707                 return -EINVAL;
708
709         ret = request_firmware(&fw, name, device);
710         if (ret == 0) {
711                 ret = cb(dev, fw->data, fw->size, context);
712                 release_firmware(fw);
713         }
714
715         return ret < 0 ? ret : 0;
716 }
717 EXPORT_SYMBOL_GPL(comedi_load_firmware);
718
719 /**
720  * __comedi_request_region() - Request an I/O reqion for a legacy driver.
721  * @dev: comedi_device struct
722  * @start: base address of the I/O reqion
723  * @len: length of the I/O region
724  */
725 int __comedi_request_region(struct comedi_device *dev,
726                             unsigned long start, unsigned long len)
727 {
728         if (!start) {
729                 dev_warn(dev->class_dev,
730                          "%s: a I/O base address must be specified\n",
731                          dev->board_name);
732                 return -EINVAL;
733         }
734
735         if (!request_region(start, len, dev->board_name)) {
736                 dev_warn(dev->class_dev, "%s: I/O port conflict (%#lx,%lu)\n",
737                          dev->board_name, start, len);
738                 return -EIO;
739         }
740
741         return 0;
742 }
743 EXPORT_SYMBOL_GPL(__comedi_request_region);
744
745 /**
746  * comedi_request_region() - Request an I/O reqion for a legacy driver.
747  * @dev: comedi_device struct
748  * @start: base address of the I/O reqion
749  * @len: length of the I/O region
750  */
751 int comedi_request_region(struct comedi_device *dev,
752                           unsigned long start, unsigned long len)
753 {
754         int ret;
755
756         ret = __comedi_request_region(dev, start, len);
757         if (ret == 0) {
758                 dev->iobase = start;
759                 dev->iolen = len;
760         }
761
762         return ret;
763 }
764 EXPORT_SYMBOL_GPL(comedi_request_region);
765
766 /**
767  * comedi_legacy_detach() - A generic (*detach) function for legacy drivers.
768  * @dev: comedi_device struct
769  */
770 void comedi_legacy_detach(struct comedi_device *dev)
771 {
772         if (dev->irq) {
773                 free_irq(dev->irq, dev);
774                 dev->irq = 0;
775         }
776         if (dev->iobase && dev->iolen) {
777                 release_region(dev->iobase, dev->iolen);
778                 dev->iobase = 0;
779                 dev->iolen = 0;
780         }
781 }
782 EXPORT_SYMBOL_GPL(comedi_legacy_detach);
783
784 int comedi_device_attach(struct comedi_device *dev, struct comedi_devconfig *it)
785 {
786         struct comedi_driver *driv;
787         int ret;
788
789         if (dev->attached)
790                 return -EBUSY;
791
792         mutex_lock(&comedi_drivers_list_lock);
793         for (driv = comedi_drivers; driv; driv = driv->next) {
794                 if (!try_module_get(driv->module))
795                         continue;
796                 if (driv->num_names) {
797                         dev->board_ptr = comedi_recognize(driv, it->board_name);
798                         if (dev->board_ptr)
799                                 break;
800                 } else if (strcmp(driv->driver_name, it->board_name) == 0) {
801                         break;
802                 }
803                 module_put(driv->module);
804         }
805         if (!driv) {
806                 /*  recognize has failed if we get here */
807                 /*  report valid board names before returning error */
808                 for (driv = comedi_drivers; driv; driv = driv->next) {
809                         if (!try_module_get(driv->module))
810                                 continue;
811                         comedi_report_boards(driv);
812                         module_put(driv->module);
813                 }
814                 ret = -EIO;
815                 goto out;
816         }
817         if (!driv->attach) {
818                 /* driver does not support manual configuration */
819                 dev_warn(dev->class_dev,
820                          "driver '%s' does not support attach using comedi_config\n",
821                          driv->driver_name);
822                 module_put(driv->module);
823                 ret = -ENOSYS;
824                 goto out;
825         }
826         dev->driver = driv;
827         dev->board_name = dev->board_ptr ? *(const char **)dev->board_ptr
828                                          : dev->driver->driver_name;
829         ret = driv->attach(dev, it);
830         if (ret >= 0)
831                 ret = comedi_device_postconfig(dev);
832         if (ret < 0) {
833                 comedi_device_detach(dev);
834                 module_put(driv->module);
835         }
836         /* On success, the driver module count has been incremented. */
837 out:
838         mutex_unlock(&comedi_drivers_list_lock);
839         return ret;
840 }
841
842 int comedi_auto_config(struct device *hardware_device,
843                        struct comedi_driver *driver, unsigned long context)
844 {
845         struct comedi_device *dev;
846         int ret;
847
848         if (!hardware_device) {
849                 pr_warn("BUG! comedi_auto_config called with NULL hardware_device\n");
850                 return -EINVAL;
851         }
852         if (!driver) {
853                 dev_warn(hardware_device,
854                          "BUG! comedi_auto_config called with NULL comedi driver\n");
855                 return -EINVAL;
856         }
857
858         if (!driver->auto_attach) {
859                 dev_warn(hardware_device,
860                          "BUG! comedi driver '%s' has no auto_attach handler\n",
861                          driver->driver_name);
862                 return -EINVAL;
863         }
864
865         dev = comedi_alloc_board_minor(hardware_device);
866         if (IS_ERR(dev)) {
867                 dev_warn(hardware_device,
868                          "driver '%s' could not create device.\n",
869                          driver->driver_name);
870                 return PTR_ERR(dev);
871         }
872         /* Note: comedi_alloc_board_minor() locked dev->mutex. */
873
874         dev->driver = driver;
875         dev->board_name = dev->driver->driver_name;
876         ret = driver->auto_attach(dev, context);
877         if (ret >= 0)
878                 ret = comedi_device_postconfig(dev);
879         mutex_unlock(&dev->mutex);
880
881         if (ret < 0) {
882                 dev_warn(hardware_device,
883                          "driver '%s' failed to auto-configure device.\n",
884                          driver->driver_name);
885                 comedi_release_hardware_device(hardware_device);
886         } else {
887                 /*
888                  * class_dev should be set properly here
889                  *  after a successful auto config
890                  */
891                 dev_info(dev->class_dev,
892                          "driver '%s' has successfully auto-configured '%s'.\n",
893                          driver->driver_name, dev->board_name);
894         }
895         return ret;
896 }
897 EXPORT_SYMBOL_GPL(comedi_auto_config);
898
899 void comedi_auto_unconfig(struct device *hardware_device)
900 {
901         if (!hardware_device)
902                 return;
903         comedi_release_hardware_device(hardware_device);
904 }
905 EXPORT_SYMBOL_GPL(comedi_auto_unconfig);
906
907 int comedi_driver_register(struct comedi_driver *driver)
908 {
909         mutex_lock(&comedi_drivers_list_lock);
910         driver->next = comedi_drivers;
911         comedi_drivers = driver;
912         mutex_unlock(&comedi_drivers_list_lock);
913
914         return 0;
915 }
916 EXPORT_SYMBOL_GPL(comedi_driver_register);
917
918 void comedi_driver_unregister(struct comedi_driver *driver)
919 {
920         struct comedi_driver *prev;
921         int i;
922
923         /* unlink the driver */
924         mutex_lock(&comedi_drivers_list_lock);
925         if (comedi_drivers == driver) {
926                 comedi_drivers = driver->next;
927         } else {
928                 for (prev = comedi_drivers; prev->next; prev = prev->next) {
929                         if (prev->next == driver) {
930                                 prev->next = driver->next;
931                                 break;
932                         }
933                 }
934         }
935         mutex_unlock(&comedi_drivers_list_lock);
936
937         /* check for devices using this driver */
938         for (i = 0; i < COMEDI_NUM_BOARD_MINORS; i++) {
939                 struct comedi_device *dev = comedi_dev_get_from_minor(i);
940
941                 if (!dev)
942                         continue;
943
944                 mutex_lock(&dev->mutex);
945                 if (dev->attached && dev->driver == driver) {
946                         if (dev->use_count)
947                                 dev_warn(dev->class_dev,
948                                          "BUG! detaching device with use_count=%d\n",
949                                          dev->use_count);
950                         comedi_device_detach(dev);
951                 }
952                 mutex_unlock(&dev->mutex);
953                 comedi_dev_put(dev);
954         }
955 }
956 EXPORT_SYMBOL_GPL(comedi_driver_unregister);