blob: 5b15033a94bf6106cc5b26b9da157ba24be60613 [file] [log] [blame]
David Schleefed9eccb2008-11-04 20:29:31 -08001/*
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.
David Schleefed9eccb2008-11-04 20:29:31 -080017*/
18
David Schleefed9eccb2008-11-04 20:29:31 -080019#include <linux/device.h>
20#include <linux/module.h>
David Schleefed9eccb2008-11-04 20:29:31 -080021#include <linux/errno.h>
Randy Dunlap78b10612012-06-11 16:35:50 -070022#include <linux/kconfig.h>
David Schleefed9eccb2008-11-04 20:29:31 -080023#include <linux/kernel.h>
24#include <linux/sched.h>
25#include <linux/fcntl.h>
David Schleefed9eccb2008-11-04 20:29:31 -080026#include <linux/ioport.h>
27#include <linux/mm.h>
28#include <linux/slab.h>
David Schleefed9eccb2008-11-04 20:29:31 -080029#include <linux/highmem.h> /* for SuSE brokenness */
30#include <linux/vmalloc.h>
31#include <linux/cdev.h>
32#include <linux/dma-mapping.h>
Andrea Gelmini5617f9d2010-02-26 17:37:00 +010033#include <linux/io.h>
H Hartley Sweeten3d1fe3f2013-04-18 14:34:37 -070034#include <linux/interrupt.h>
H Hartley Sweeten9ff8b152013-05-17 11:17:00 -070035#include <linux/firmware.h>
David Schleefed9eccb2008-11-04 20:29:31 -080036
Greg Kroah-Hartman242e7ad2010-05-03 15:20:29 -070037#include "comedidev.h"
Ian Abbott3a5fa272012-06-19 10:17:44 +010038#include "comedi_internal.h"
Greg Kroah-Hartman242e7ad2010-05-03 15:20:29 -070039
Bill Pemberton139dfbd2009-03-16 22:05:25 -040040struct comedi_driver *comedi_drivers;
Ian Abbottc383e2d2013-06-27 14:50:58 +010041DEFINE_MUTEX(comedi_drivers_list_lock);
David Schleefed9eccb2008-11-04 20:29:31 -080042
Ian Abbottda717512013-02-01 13:23:19 +000043int comedi_set_hw_dev(struct comedi_device *dev, struct device *hw_dev)
44{
Ian Abbottde06d7c2013-02-01 13:23:20 +000045 if (hw_dev == dev->hw_dev)
46 return 0;
47 if (dev->hw_dev != NULL)
48 return -EEXIST;
Ian Abbottda717512013-02-01 13:23:19 +000049 dev->hw_dev = get_device(hw_dev);
Ian Abbottda717512013-02-01 13:23:19 +000050 return 0;
51}
52EXPORT_SYMBOL_GPL(comedi_set_hw_dev);
53
Ian Abbottde06d7c2013-02-01 13:23:20 +000054static void comedi_clear_hw_dev(struct comedi_device *dev)
55{
56 put_device(dev->hw_dev);
57 dev->hw_dev = NULL;
58}
59
H Hartley Sweeten54db9962013-06-24 16:55:14 -070060/**
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 */
65void *comedi_alloc_devpriv(struct comedi_device *dev, size_t size)
66{
67 dev->private = kzalloc(size, GFP_KERNEL);
68 return dev->private;
69}
70EXPORT_SYMBOL_GPL(comedi_alloc_devpriv);
71
H Hartley Sweeten8b9ba6e2012-06-12 11:57:27 -070072int comedi_alloc_subdevices(struct comedi_device *dev, int num_subdevices)
H Hartley Sweeten2f0b9d02012-06-11 17:45:15 -070073{
H Hartley Sweeten03afcf42012-06-12 11:59:55 -070074 struct comedi_subdevice *s;
H Hartley Sweeten8b9ba6e2012-06-12 11:57:27 -070075 int i;
H Hartley Sweeten2f0b9d02012-06-11 17:45:15 -070076
H Hartley Sweeten7f801c42012-06-12 11:57:45 -070077 if (num_subdevices < 1)
78 return -EINVAL;
H Hartley Sweeten03afcf42012-06-12 11:59:55 -070079
80 s = kcalloc(num_subdevices, sizeof(*s), GFP_KERNEL);
81 if (!s)
H Hartley Sweeten2f0b9d02012-06-11 17:45:15 -070082 return -ENOMEM;
H Hartley Sweeten03afcf42012-06-12 11:59:55 -070083 dev->subdevices = s;
H Hartley Sweetenfba1d0f2012-06-12 11:58:27 -070084 dev->n_subdevices = num_subdevices;
H Hartley Sweeten03afcf42012-06-12 11:59:55 -070085
H Hartley Sweeten2f0b9d02012-06-11 17:45:15 -070086 for (i = 0; i < num_subdevices; ++i) {
H Hartley Sweeten5e4c58c2012-09-05 18:21:25 -070087 s = &dev->subdevices[i];
H Hartley Sweeten03afcf42012-06-12 11:59:55 -070088 s->device = dev;
H Hartley Sweeten90a35c12012-12-19 17:27:02 -070089 s->index = i;
H Hartley Sweeten03afcf42012-06-12 11:59:55 -070090 s->async_dma_dir = DMA_NONE;
91 spin_lock_init(&s->spin_lock);
92 s->minor = -1;
H Hartley Sweeten2f0b9d02012-06-11 17:45:15 -070093 }
94 return 0;
95}
96EXPORT_SYMBOL_GPL(comedi_alloc_subdevices);
97
Ian Abbott3867e202013-11-08 15:03:26 +000098static void comedi_device_detach_cleanup(struct comedi_device *dev)
David Schleefed9eccb2008-11-04 20:29:31 -080099{
100 int i;
Bill Pemberton34c43922009-03-16 22:05:14 -0400101 struct comedi_subdevice *s;
David Schleefed9eccb2008-11-04 20:29:31 -0800102
103 if (dev->subdevices) {
104 for (i = 0; i < dev->n_subdevices; i++) {
H Hartley Sweeten5e4c58c2012-09-05 18:21:25 -0700105 s = &dev->subdevices[i];
H Hartley Sweeten588ba6d2013-06-11 11:32:29 -0700106 if (s->runflags & SRF_FREE_SPRIV)
107 kfree(s->private);
David Schleefed9eccb2008-11-04 20:29:31 -0800108 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 }
Bill Pembertondedd1322009-03-16 22:04:18 -0400118 kfree(dev->private);
119 dev->private = NULL;
Greg Kroah-Hartman7029a872010-05-03 15:55:45 -0700120 dev->driver = NULL;
David Schleefed9eccb2008-11-04 20:29:31 -0800121 dev->board_name = NULL;
122 dev->board_ptr = NULL;
123 dev->iobase = 0;
H Hartley Sweeten316f97f2013-04-18 14:31:29 -0700124 dev->iolen = 0;
Ian Abbott00ca68842013-03-15 13:15:35 +0000125 dev->ioenabled = false;
David Schleefed9eccb2008-11-04 20:29:31 -0800126 dev->irq = 0;
127 dev->read_subdev = NULL;
128 dev->write_subdev = NULL;
129 dev->open = NULL;
130 dev->close = NULL;
Ian Abbottde06d7c2013-02-01 13:23:20 +0000131 comedi_clear_hw_dev(dev);
David Schleefed9eccb2008-11-04 20:29:31 -0800132}
133
Ian Abbott016599f2013-04-04 14:58:58 +0100134void comedi_device_detach(struct comedi_device *dev)
David Schleefed9eccb2008-11-04 20:29:31 -0800135{
Ian Abbottd19db512013-11-08 15:03:28 +0000136 comedi_device_cancel_all(dev);
Ian Abbottbf11c132013-11-08 15:03:25 +0000137 down_write(&dev->attach_lock);
Ian Abbotta7401cd2013-03-15 13:15:33 +0000138 dev->attached = false;
Ian Abbottef77c0b2013-11-08 15:03:29 +0000139 dev->detach_count++;
Andrea Gelmini5617f9d2010-02-26 17:37:00 +0100140 if (dev->driver)
David Schleefed9eccb2008-11-04 20:29:31 -0800141 dev->driver->detach(dev);
Ian Abbott3867e202013-11-08 15:03:26 +0000142 comedi_device_detach_cleanup(dev);
Ian Abbottbf11c132013-11-08 15:03:25 +0000143 up_write(&dev->attach_lock);
David Schleefed9eccb2008-11-04 20:29:31 -0800144}
145
H Hartley Sweeten01fca372013-01-21 14:36:11 -0700146static int poll_invalid(struct comedi_device *dev, struct comedi_subdevice *s)
147{
148 return -EINVAL;
149}
150
151int insn_inval(struct comedi_device *dev, struct comedi_subdevice *s,
152 struct comedi_insn *insn, unsigned int *data)
153{
154 return -EINVAL;
155}
156
H Hartley Sweetene523c6c2013-08-06 09:31:35 -0700157/**
158 * comedi_dio_insn_config() - boilerplate (*insn_config) for DIO subdevices.
159 * @dev: comedi_device struct
160 * @s: comedi_subdevice struct
161 * @insn: comedi_insn struct
162 * @data: parameters for the @insn
163 * @mask: io_bits mask for grouped channels
164 */
165int comedi_dio_insn_config(struct comedi_device *dev,
166 struct comedi_subdevice *s,
167 struct comedi_insn *insn,
168 unsigned int *data,
169 unsigned int mask)
170{
171 unsigned int chan_mask = 1 << CR_CHAN(insn->chanspec);
172
173 if (!mask)
174 mask = chan_mask;
175
176 switch (data[0]) {
177 case INSN_CONFIG_DIO_INPUT:
178 s->io_bits &= ~mask;
179 break;
180
181 case INSN_CONFIG_DIO_OUTPUT:
182 s->io_bits |= mask;
183 break;
184
185 case INSN_CONFIG_DIO_QUERY:
186 data[1] = (s->io_bits & mask) ? COMEDI_OUTPUT : COMEDI_INPUT;
187 return insn->n;
188
189 default:
190 return -EINVAL;
191 }
192
193 return 0;
194}
195EXPORT_SYMBOL_GPL(comedi_dio_insn_config);
196
H Hartley Sweeten05e60b12013-08-30 11:04:56 -0700197/**
198 * comedi_dio_update_state() - update the internal state of DIO subdevices.
199 * @s: comedi_subdevice struct
200 * @data: the channel mask and bits to update
201 */
202unsigned int comedi_dio_update_state(struct comedi_subdevice *s,
203 unsigned int *data)
204{
205 unsigned int chanmask = (s->n_chan < 32) ? ((1 << s->n_chan) - 1)
206 : 0xffffffff;
207 unsigned int mask = data[0] & chanmask;
208 unsigned int bits = data[1];
209
210 if (mask) {
211 s->state &= ~mask;
212 s->state |= (bits & mask);
213 }
214
215 return mask;
216}
217EXPORT_SYMBOL_GPL(comedi_dio_update_state);
218
H Hartley Sweeten01fca372013-01-21 14:36:11 -0700219static int insn_rw_emulate_bits(struct comedi_device *dev,
220 struct comedi_subdevice *s,
221 struct comedi_insn *insn, unsigned int *data)
222{
223 struct comedi_insn new_insn;
224 int ret;
225 static const unsigned channels_per_bitfield = 32;
226
227 unsigned chan = CR_CHAN(insn->chanspec);
228 const unsigned base_bitfield_channel =
229 (chan < channels_per_bitfield) ? 0 : chan;
230 unsigned int new_data[2];
231 memset(new_data, 0, sizeof(new_data));
232 memset(&new_insn, 0, sizeof(new_insn));
233 new_insn.insn = INSN_BITS;
234 new_insn.chanspec = base_bitfield_channel;
235 new_insn.n = 2;
236 new_insn.subdev = insn->subdev;
237
238 if (insn->insn == INSN_WRITE) {
239 if (!(s->subdev_flags & SDF_WRITABLE))
240 return -EINVAL;
241 new_data[0] = 1 << (chan - base_bitfield_channel); /* mask */
242 new_data[1] = data[0] ? (1 << (chan - base_bitfield_channel))
243 : 0; /* bits */
244 }
245
246 ret = s->insn_bits(dev, s, &new_insn, new_data);
247 if (ret < 0)
248 return ret;
249
250 if (insn->insn == INSN_READ)
251 data[0] = (new_data[1] >> (chan - base_bitfield_channel)) & 1;
252
253 return 1;
254}
255
H Hartley Sweeten40f58a62013-01-21 14:36:40 -0700256static int __comedi_device_postconfig_async(struct comedi_device *dev,
257 struct comedi_subdevice *s)
H Hartley Sweeten01fca372013-01-21 14:36:11 -0700258{
H Hartley Sweeten40f58a62013-01-21 14:36:40 -0700259 struct comedi_async *async;
260 unsigned int buf_size;
H Hartley Sweeten01fca372013-01-21 14:36:11 -0700261 int ret;
262
H Hartley Sweeten57b71c32013-01-21 14:37:15 -0700263 if ((s->subdev_flags & (SDF_CMD_READ | SDF_CMD_WRITE)) == 0) {
264 dev_warn(dev->class_dev,
265 "async subdevices must support SDF_CMD_READ or SDF_CMD_WRITE\n");
266 return -EINVAL;
267 }
268 if (!s->do_cmdtest) {
269 dev_warn(dev->class_dev,
270 "async subdevices must have a do_cmdtest() function\n");
271 return -EINVAL;
272 }
H Hartley Sweeten40f58a62013-01-21 14:36:40 -0700273
274 async = kzalloc(sizeof(*async), GFP_KERNEL);
Joe Perches78110bb2013-02-11 09:41:29 -0800275 if (!async)
H Hartley Sweeten40f58a62013-01-21 14:36:40 -0700276 return -ENOMEM;
Joe Perches78110bb2013-02-11 09:41:29 -0800277
H Hartley Sweeten40f58a62013-01-21 14:36:40 -0700278 init_waitqueue_head(&async->wait_head);
279 async->subdevice = s;
280 s->async = async;
281
282 async->max_bufsize = comedi_default_buf_maxsize_kb * 1024;
283 buf_size = comedi_default_buf_size_kb * 1024;
284 if (buf_size > async->max_bufsize)
285 buf_size = async->max_bufsize;
286
287 if (comedi_buf_alloc(dev, s, buf_size) < 0) {
288 dev_warn(dev->class_dev, "Buffer allocation failed\n");
289 return -ENOMEM;
290 }
291 if (s->buf_change) {
292 ret = s->buf_change(dev, s, buf_size);
293 if (ret < 0)
294 return ret;
295 }
296
Ian Abbottf65cc542013-02-01 10:20:30 +0000297 comedi_alloc_subdevice_minor(s);
H Hartley Sweeten40f58a62013-01-21 14:36:40 -0700298
299 return 0;
300}
301
302static int __comedi_device_postconfig(struct comedi_device *dev)
303{
304 struct comedi_subdevice *s;
305 int ret;
306 int i;
307
H Hartley Sweeten01fca372013-01-21 14:36:11 -0700308 for (i = 0; i < dev->n_subdevices; i++) {
309 s = &dev->subdevices[i];
310
311 if (s->type == COMEDI_SUBD_UNUSED)
312 continue;
313
H Hartley Sweeten09567cb2013-08-30 10:47:03 -0700314 if (s->type == COMEDI_SUBD_DO) {
315 if (s->n_chan < 32)
316 s->io_bits = (1 << s->n_chan) - 1;
317 else
318 s->io_bits = 0xffffffff;
319 }
320
H Hartley Sweeten01fca372013-01-21 14:36:11 -0700321 if (s->len_chanlist == 0)
322 s->len_chanlist = 1;
323
324 if (s->do_cmd) {
H Hartley Sweeten40f58a62013-01-21 14:36:40 -0700325 ret = __comedi_device_postconfig_async(dev, s);
326 if (ret)
327 return ret;
H Hartley Sweeten01fca372013-01-21 14:36:11 -0700328 }
329
330 if (!s->range_table && !s->range_table_list)
331 s->range_table = &range_unknown;
332
333 if (!s->insn_read && s->insn_bits)
334 s->insn_read = insn_rw_emulate_bits;
335 if (!s->insn_write && s->insn_bits)
336 s->insn_write = insn_rw_emulate_bits;
337
338 if (!s->insn_read)
339 s->insn_read = insn_inval;
340 if (!s->insn_write)
341 s->insn_write = insn_inval;
342 if (!s->insn_bits)
343 s->insn_bits = insn_inval;
344 if (!s->insn_config)
345 s->insn_config = insn_inval;
346
347 if (!s->poll)
348 s->poll = poll_invalid;
349 }
350
351 return 0;
352}
353
Ian Abbott3902a372012-03-30 17:15:00 +0100354/* do a little post-config cleanup */
Ian Abbott3902a372012-03-30 17:15:00 +0100355static int comedi_device_postconfig(struct comedi_device *dev)
356{
Ian Abbottb2a644b2013-04-04 14:58:56 +0100357 int ret;
358
359 ret = __comedi_device_postconfig(dev);
H Hartley Sweetenae5dd5f2013-04-08 10:56:02 -0700360 if (ret < 0)
Ian Abbott3902a372012-03-30 17:15:00 +0100361 return ret;
Ian Abbottbf11c132013-11-08 15:03:25 +0000362 down_write(&dev->attach_lock);
Ian Abbotta7401cd2013-03-15 13:15:33 +0000363 dev->attached = true;
Ian Abbottbf11c132013-11-08 15:03:25 +0000364 up_write(&dev->attach_lock);
Ian Abbott3902a372012-03-30 17:15:00 +0100365 return 0;
366}
367
H Hartley Sweeten01fca372013-01-21 14:36:11 -0700368/*
369 * Generic recognize function for drivers that register their supported
370 * board names.
371 *
372 * 'driv->board_name' points to a 'const char *' member within the
373 * zeroth element of an array of some private board information
374 * structure, say 'struct foo_board' containing a member 'const char
375 * *board_name' that is initialized to point to a board name string that
376 * is one of the candidates matched against this function's 'name'
377 * parameter.
378 *
379 * 'driv->offset' is the size of the private board information
380 * structure, say 'sizeof(struct foo_board)', and 'driv->num_names' is
381 * the length of the array of private board information structures.
382 *
383 * If one of the board names in the array of private board information
384 * structures matches the name supplied to this function, the function
385 * returns a pointer to the pointer to the board name, otherwise it
386 * returns NULL. The return value ends up in the 'board_ptr' member of
387 * a 'struct comedi_device' that the low-level comedi driver's
388 * 'attach()' hook can convert to a point to a particular element of its
389 * array of private board information structures by subtracting the
390 * offset of the member that points to the board name. (No subtraction
391 * is required if the board name pointer is the first member of the
392 * private board information structure, which is generally the case.)
393 */
394static void *comedi_recognize(struct comedi_driver *driv, const char *name)
395{
396 char **name_ptr = (char **)driv->board_name;
397 int i;
398
399 for (i = 0; i < driv->num_names; i++) {
400 if (strcmp(*name_ptr, name) == 0)
401 return name_ptr;
402 name_ptr = (void *)name_ptr + driv->offset;
403 }
404
405 return NULL;
406}
407
408static void comedi_report_boards(struct comedi_driver *driv)
409{
410 unsigned int i;
411 const char *const *name_ptr;
412
413 pr_info("comedi: valid board names for %s driver are:\n",
414 driv->driver_name);
415
416 name_ptr = driv->board_name;
417 for (i = 0; i < driv->num_names; i++) {
418 pr_info(" %s\n", *name_ptr);
419 name_ptr = (const char **)((char *)name_ptr + driv->offset);
420 }
421
422 if (driv->num_names == 0)
423 pr_info(" %s\n", driv->driver_name);
424}
425
H Hartley Sweetenf375ac52013-04-09 16:05:54 -0700426/**
H Hartley Sweeten9ff8b152013-05-17 11:17:00 -0700427 * comedi_load_firmware() - Request and load firmware for a device.
428 * @dev: comedi_device struct
429 * @hw_device: device struct for the comedi_device
430 * @name: the name of the firmware image
431 * @cb: callback to the upload the firmware image
H Hartley Sweetend5695412013-05-17 11:18:01 -0700432 * @context: private context from the driver
H Hartley Sweeten9ff8b152013-05-17 11:17:00 -0700433 */
434int comedi_load_firmware(struct comedi_device *dev,
435 struct device *device,
436 const char *name,
437 int (*cb)(struct comedi_device *dev,
H Hartley Sweetend5695412013-05-17 11:18:01 -0700438 const u8 *data, size_t size,
439 unsigned long context),
440 unsigned long context)
H Hartley Sweeten9ff8b152013-05-17 11:17:00 -0700441{
442 const struct firmware *fw;
443 int ret;
444
445 if (!cb)
446 return -EINVAL;
447
448 ret = request_firmware(&fw, name, device);
449 if (ret == 0) {
H Hartley Sweetend5695412013-05-17 11:18:01 -0700450 ret = cb(dev, fw->data, fw->size, context);
H Hartley Sweeten9ff8b152013-05-17 11:17:00 -0700451 release_firmware(fw);
452 }
453
H Hartley Sweetenc6236c02013-12-10 16:31:25 -0700454 return ret < 0 ? ret : 0;
H Hartley Sweeten9ff8b152013-05-17 11:17:00 -0700455}
456EXPORT_SYMBOL_GPL(comedi_load_firmware);
457
458/**
H Hartley Sweetenca8b2962013-04-09 16:30:11 -0700459 * __comedi_request_region() - Request an I/O reqion for a legacy driver.
H Hartley Sweetenf375ac52013-04-09 16:05:54 -0700460 * @dev: comedi_device struct
461 * @start: base address of the I/O reqion
462 * @len: length of the I/O region
463 */
H Hartley Sweetenca8b2962013-04-09 16:30:11 -0700464int __comedi_request_region(struct comedi_device *dev,
465 unsigned long start, unsigned long len)
H Hartley Sweetenf375ac52013-04-09 16:05:54 -0700466{
467 if (!start) {
468 dev_warn(dev->class_dev,
469 "%s: a I/O base address must be specified\n",
470 dev->board_name);
471 return -EINVAL;
472 }
473
474 if (!request_region(start, len, dev->board_name)) {
475 dev_warn(dev->class_dev, "%s: I/O port conflict (%#lx,%lu)\n",
476 dev->board_name, start, len);
477 return -EIO;
478 }
H Hartley Sweetenf375ac52013-04-09 16:05:54 -0700479
480 return 0;
481}
H Hartley Sweetenca8b2962013-04-09 16:30:11 -0700482EXPORT_SYMBOL_GPL(__comedi_request_region);
483
484/**
485 * comedi_request_region() - Request an I/O reqion for a legacy driver.
486 * @dev: comedi_device struct
487 * @start: base address of the I/O reqion
488 * @len: length of the I/O region
489 */
490int comedi_request_region(struct comedi_device *dev,
491 unsigned long start, unsigned long len)
492{
493 int ret;
494
495 ret = __comedi_request_region(dev, start, len);
H Hartley Sweeten316f97f2013-04-18 14:31:29 -0700496 if (ret == 0) {
H Hartley Sweetenca8b2962013-04-09 16:30:11 -0700497 dev->iobase = start;
H Hartley Sweeten316f97f2013-04-18 14:31:29 -0700498 dev->iolen = len;
499 }
H Hartley Sweetenca8b2962013-04-09 16:30:11 -0700500
501 return ret;
502}
H Hartley Sweetenf375ac52013-04-09 16:05:54 -0700503EXPORT_SYMBOL_GPL(comedi_request_region);
504
H Hartley Sweeten316f97f2013-04-18 14:31:29 -0700505/**
506 * comedi_legacy_detach() - A generic (*detach) function for legacy drivers.
507 * @dev: comedi_device struct
508 */
509void comedi_legacy_detach(struct comedi_device *dev)
510{
H Hartley Sweeten3d1fe3f2013-04-18 14:34:37 -0700511 if (dev->irq) {
512 free_irq(dev->irq, dev);
513 dev->irq = 0;
514 }
H Hartley Sweeten316f97f2013-04-18 14:31:29 -0700515 if (dev->iobase && dev->iolen) {
516 release_region(dev->iobase, dev->iolen);
517 dev->iobase = 0;
518 dev->iolen = 0;
519 }
520}
521EXPORT_SYMBOL_GPL(comedi_legacy_detach);
522
Bill Pemberton0707bb02009-03-16 22:06:20 -0400523int comedi_device_attach(struct comedi_device *dev, struct comedi_devconfig *it)
David Schleefed9eccb2008-11-04 20:29:31 -0800524{
Bill Pemberton139dfbd2009-03-16 22:05:25 -0400525 struct comedi_driver *driv;
David Schleefed9eccb2008-11-04 20:29:31 -0800526 int ret;
527
528 if (dev->attached)
529 return -EBUSY;
530
Ian Abbottc383e2d2013-06-27 14:50:58 +0100531 mutex_lock(&comedi_drivers_list_lock);
David Schleefed9eccb2008-11-04 20:29:31 -0800532 for (driv = comedi_drivers; driv; driv = driv->next) {
Güngör Erseymen559e9a62012-09-11 17:56:42 +0300533 if (!try_module_get(driv->module))
David Schleefed9eccb2008-11-04 20:29:31 -0800534 continue;
David Schleefed9eccb2008-11-04 20:29:31 -0800535 if (driv->num_names) {
536 dev->board_ptr = comedi_recognize(driv, it->board_name);
Ian Abbott3902a372012-03-30 17:15:00 +0100537 if (dev->board_ptr)
538 break;
Ian Abbott80eb7a52012-08-14 11:29:17 +0100539 } else if (strcmp(driv->driver_name, it->board_name) == 0)
Ian Abbott3902a372012-03-30 17:15:00 +0100540 break;
David Schleefed9eccb2008-11-04 20:29:31 -0800541 module_put(driv->module);
542 }
Ian Abbott3902a372012-03-30 17:15:00 +0100543 if (driv == NULL) {
544 /* recognize has failed if we get here */
545 /* report valid board names before returning error */
546 for (driv = comedi_drivers; driv; driv = driv->next) {
Güngör Erseymen559e9a62012-09-11 17:56:42 +0300547 if (!try_module_get(driv->module))
Ian Abbott3902a372012-03-30 17:15:00 +0100548 continue;
Ian Abbott3902a372012-03-30 17:15:00 +0100549 comedi_report_boards(driv);
550 module_put(driv->module);
551 }
Ian Abbottc383e2d2013-06-27 14:50:58 +0100552 ret = -EIO;
553 goto out;
Ian Abbott3902a372012-03-30 17:15:00 +0100554 }
Ian Abbott8c3714d2012-08-15 15:02:45 +0100555 if (driv->attach == NULL) {
556 /* driver does not support manual configuration */
557 dev_warn(dev->class_dev,
558 "driver '%s' does not support attach using comedi_config\n",
559 driv->driver_name);
560 module_put(driv->module);
Ian Abbottc383e2d2013-06-27 14:50:58 +0100561 ret = -ENOSYS;
562 goto out;
Ian Abbott8c3714d2012-08-15 15:02:45 +0100563 }
Ian Abbott3902a372012-03-30 17:15:00 +0100564 /* initialize dev->driver here so
565 * comedi_error() can be called from attach */
566 dev->driver = driv;
H Hartley Sweeten34b68402013-04-08 10:55:29 -0700567 dev->board_name = dev->board_ptr ? *(const char **)dev->board_ptr
568 : dev->driver->driver_name;
Ian Abbott3902a372012-03-30 17:15:00 +0100569 ret = driv->attach(dev, it);
Ian Abbott74ece1082013-04-04 14:58:59 +0100570 if (ret >= 0)
571 ret = comedi_device_postconfig(dev);
David Schleefed9eccb2008-11-04 20:29:31 -0800572 if (ret < 0) {
Ian Abbott016599f2013-04-04 14:58:58 +0100573 comedi_device_detach(dev);
Ian Abbott3955dfa2013-08-23 12:37:17 +0100574 module_put(driv->module);
David Schleefed9eccb2008-11-04 20:29:31 -0800575 }
Ian Abbottb2a644b2013-04-04 14:58:56 +0100576 /* On success, the driver module count has been incremented. */
Ian Abbottc383e2d2013-06-27 14:50:58 +0100577out:
578 mutex_unlock(&comedi_drivers_list_lock);
Ian Abbottb2a644b2013-04-04 14:58:56 +0100579 return ret;
David Schleefed9eccb2008-11-04 20:29:31 -0800580}
581
Ian Abbotta588da12012-11-14 13:10:38 +0000582int comedi_auto_config(struct device *hardware_device,
583 struct comedi_driver *driver, unsigned long context)
Ian Abbottf40116702012-03-30 17:15:01 +0100584{
H Hartley Sweeten6013a9a2013-04-08 10:55:05 -0700585 struct comedi_device *dev;
Ian Abbottf40116702012-03-30 17:15:01 +0100586 int ret;
587
Ian Abbottf08e0ac2013-04-04 14:58:42 +0100588 if (!hardware_device) {
589 pr_warn("BUG! comedi_auto_config called with NULL hardware_device\n");
590 return -EINVAL;
591 }
592 if (!driver) {
593 dev_warn(hardware_device,
594 "BUG! comedi_auto_config called with NULL comedi driver\n");
595 return -EINVAL;
596 }
597
Ian Abbotta588da12012-11-14 13:10:38 +0000598 if (!driver->auto_attach) {
599 dev_warn(hardware_device,
600 "BUG! comedi driver '%s' has no auto_attach handler\n",
601 driver->driver_name);
602 return -EINVAL;
603 }
604
H Hartley Sweeten6013a9a2013-04-08 10:55:05 -0700605 dev = comedi_alloc_board_minor(hardware_device);
Bernd Porrbcb62322014-01-07 21:42:25 +0000606 if (IS_ERR(dev)) {
607 dev_warn(hardware_device,
608 "driver '%s' could not create device.\n",
609 driver->driver_name);
H Hartley Sweeten6013a9a2013-04-08 10:55:05 -0700610 return PTR_ERR(dev);
Bernd Porrbcb62322014-01-07 21:42:25 +0000611 }
H Hartley Sweeten6013a9a2013-04-08 10:55:05 -0700612 /* Note: comedi_alloc_board_minor() locked dev->mutex. */
Ian Abbottf40116702012-03-30 17:15:01 +0100613
H Hartley Sweeten6013a9a2013-04-08 10:55:05 -0700614 dev->driver = driver;
H Hartley Sweeten34b68402013-04-08 10:55:29 -0700615 dev->board_name = dev->driver->driver_name;
H Hartley Sweeten6013a9a2013-04-08 10:55:05 -0700616 ret = driver->auto_attach(dev, context);
Ian Abbott74ece1082013-04-04 14:58:59 +0100617 if (ret >= 0)
H Hartley Sweeten6013a9a2013-04-08 10:55:05 -0700618 ret = comedi_device_postconfig(dev);
H Hartley Sweeten6013a9a2013-04-08 10:55:05 -0700619 mutex_unlock(&dev->mutex);
Ian Abbottf40116702012-03-30 17:15:01 +0100620
Bernd Porrbcb62322014-01-07 21:42:25 +0000621 if (ret < 0) {
622 dev_warn(hardware_device,
623 "driver '%s' failed to auto-configure device.\n",
624 driver->driver_name);
Ian Abbottf5b31e12013-04-04 14:58:48 +0100625 comedi_release_hardware_device(hardware_device);
Bernd Porrbcb62322014-01-07 21:42:25 +0000626 } else {
627 /*
628 * class_dev should be set properly here
629 * after a successful auto config
630 */
631 dev_info(dev->class_dev,
632 "driver '%s' has successfully auto-configured '%s'.\n",
633 driver->driver_name, dev->board_name);
634 }
Ian Abbottf40116702012-03-30 17:15:01 +0100635 return ret;
636}
Ian Abbott8ed705a2012-10-27 21:44:14 +0100637EXPORT_SYMBOL_GPL(comedi_auto_config);
638
639void comedi_auto_unconfig(struct device *hardware_device)
David Schleefed9eccb2008-11-04 20:29:31 -0800640{
Ian Abbottc43435d2012-03-30 17:14:58 +0100641 if (hardware_device == NULL)
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530642 return;
Ian Abbott3346b792013-04-04 14:58:47 +0100643 comedi_release_hardware_device(hardware_device);
David Schleefed9eccb2008-11-04 20:29:31 -0800644}
Ian Abbott8ed705a2012-10-27 21:44:14 +0100645EXPORT_SYMBOL_GPL(comedi_auto_unconfig);
H Hartley Sweeten1ae6b202013-01-30 15:24:38 -0700646
647int comedi_driver_register(struct comedi_driver *driver)
648{
Ian Abbottc383e2d2013-06-27 14:50:58 +0100649 mutex_lock(&comedi_drivers_list_lock);
H Hartley Sweeten1ae6b202013-01-30 15:24:38 -0700650 driver->next = comedi_drivers;
651 comedi_drivers = driver;
Ian Abbottc383e2d2013-06-27 14:50:58 +0100652 mutex_unlock(&comedi_drivers_list_lock);
H Hartley Sweeten1ae6b202013-01-30 15:24:38 -0700653
654 return 0;
655}
H Hartley Sweeten5660e742013-04-12 10:11:54 -0700656EXPORT_SYMBOL_GPL(comedi_driver_register);
H Hartley Sweeten1ae6b202013-01-30 15:24:38 -0700657
Ian Abbott99c0e262013-06-27 14:50:57 +0100658void comedi_driver_unregister(struct comedi_driver *driver)
H Hartley Sweeten1ae6b202013-01-30 15:24:38 -0700659{
660 struct comedi_driver *prev;
661 int i;
662
Ian Abbottc383e2d2013-06-27 14:50:58 +0100663 /* unlink the driver */
664 mutex_lock(&comedi_drivers_list_lock);
665 if (comedi_drivers == driver) {
666 comedi_drivers = driver->next;
667 } else {
668 for (prev = comedi_drivers; prev->next; prev = prev->next) {
669 if (prev->next == driver) {
670 prev->next = driver->next;
671 break;
672 }
673 }
674 }
675 mutex_unlock(&comedi_drivers_list_lock);
676
H Hartley Sweeten1ae6b202013-01-30 15:24:38 -0700677 /* check for devices using this driver */
678 for (i = 0; i < COMEDI_NUM_BOARD_MINORS; i++) {
Ian Abbotta200fad2013-11-08 15:03:35 +0000679 struct comedi_device *dev = comedi_dev_get_from_minor(i);
H Hartley Sweeten1ae6b202013-01-30 15:24:38 -0700680
681 if (!dev)
682 continue;
683
684 mutex_lock(&dev->mutex);
685 if (dev->attached && dev->driver == driver) {
686 if (dev->use_count)
687 dev_warn(dev->class_dev,
688 "BUG! detaching device with use_count=%d\n",
689 dev->use_count);
690 comedi_device_detach(dev);
691 }
692 mutex_unlock(&dev->mutex);
Ian Abbotta200fad2013-11-08 15:03:35 +0000693 comedi_dev_put(dev);
H Hartley Sweeten1ae6b202013-01-30 15:24:38 -0700694 }
H Hartley Sweeten1ae6b202013-01-30 15:24:38 -0700695}
H Hartley Sweeten5660e742013-04-12 10:11:54 -0700696EXPORT_SYMBOL_GPL(comedi_driver_unregister);