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