]>
Commit | Line | Data |
---|---|---|
ed9eccbe DS |
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 <[email protected]> | |
7 | ||
8 | This program is free software; you can redistribute it and/or modify | |
9 | it under the terms of the GNU General Public License as published by | |
10 | the Free Software Foundation; either version 2 of the License, or | |
11 | (at your option) any later version. | |
12 | ||
13 | This program is distributed in the hope that it will be useful, | |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
19 | along with this program; if not, write to the Free Software | |
20 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
21 | ||
22 | */ | |
23 | ||
ed9eccbe DS |
24 | #include <linux/device.h> |
25 | #include <linux/module.h> | |
ed9eccbe | 26 | #include <linux/errno.h> |
78b10615 | 27 | #include <linux/kconfig.h> |
ed9eccbe DS |
28 | #include <linux/kernel.h> |
29 | #include <linux/sched.h> | |
30 | #include <linux/fcntl.h> | |
31 | #include <linux/delay.h> | |
32 | #include <linux/ioport.h> | |
33 | #include <linux/mm.h> | |
34 | #include <linux/slab.h> | |
ed9eccbe DS |
35 | #include <linux/highmem.h> /* for SuSE brokenness */ |
36 | #include <linux/vmalloc.h> | |
37 | #include <linux/cdev.h> | |
38 | #include <linux/dma-mapping.h> | |
5617f9da | 39 | #include <linux/io.h> |
ed9eccbe | 40 | |
242e7ad9 | 41 | #include "comedidev.h" |
3a5fa275 | 42 | #include "comedi_internal.h" |
242e7ad9 | 43 | |
139dfbdf | 44 | struct comedi_driver *comedi_drivers; |
ed9eccbe | 45 | |
8b9ba6e5 | 46 | int comedi_alloc_subdevices(struct comedi_device *dev, int num_subdevices) |
2f0b9d08 | 47 | { |
03afcf47 | 48 | struct comedi_subdevice *s; |
8b9ba6e5 | 49 | int i; |
2f0b9d08 | 50 | |
7f801c41 HS |
51 | if (num_subdevices < 1) |
52 | return -EINVAL; | |
03afcf47 HS |
53 | |
54 | s = kcalloc(num_subdevices, sizeof(*s), GFP_KERNEL); | |
55 | if (!s) | |
2f0b9d08 | 56 | return -ENOMEM; |
03afcf47 | 57 | dev->subdevices = s; |
fba1d0fa | 58 | dev->n_subdevices = num_subdevices; |
03afcf47 | 59 | |
2f0b9d08 | 60 | for (i = 0; i < num_subdevices; ++i) { |
5e4c58ce | 61 | s = &dev->subdevices[i]; |
03afcf47 | 62 | s->device = dev; |
90a35c15 | 63 | s->index = i; |
03afcf47 HS |
64 | s->async_dma_dir = DMA_NONE; |
65 | spin_lock_init(&s->spin_lock); | |
66 | s->minor = -1; | |
2f0b9d08 HS |
67 | } |
68 | return 0; | |
69 | } | |
70 | EXPORT_SYMBOL_GPL(comedi_alloc_subdevices); | |
71 | ||
71b5f4f1 | 72 | static void cleanup_device(struct comedi_device *dev) |
ed9eccbe DS |
73 | { |
74 | int i; | |
34c43922 | 75 | struct comedi_subdevice *s; |
ed9eccbe DS |
76 | |
77 | if (dev->subdevices) { | |
78 | for (i = 0; i < dev->n_subdevices; i++) { | |
5e4c58ce | 79 | s = &dev->subdevices[i]; |
ed9eccbe DS |
80 | comedi_free_subdevice_minor(s); |
81 | if (s->async) { | |
82 | comedi_buf_alloc(dev, s, 0); | |
83 | kfree(s->async); | |
84 | } | |
85 | } | |
86 | kfree(dev->subdevices); | |
87 | dev->subdevices = NULL; | |
88 | dev->n_subdevices = 0; | |
89 | } | |
dedd1325 BP |
90 | kfree(dev->private); |
91 | dev->private = NULL; | |
7029a874 | 92 | dev->driver = NULL; |
ed9eccbe DS |
93 | dev->board_name = NULL; |
94 | dev->board_ptr = NULL; | |
95 | dev->iobase = 0; | |
96 | dev->irq = 0; | |
97 | dev->read_subdev = NULL; | |
98 | dev->write_subdev = NULL; | |
99 | dev->open = NULL; | |
100 | dev->close = NULL; | |
101 | comedi_set_hw_dev(dev, NULL); | |
102 | } | |
103 | ||
71b5f4f1 | 104 | static void __comedi_device_detach(struct comedi_device *dev) |
ed9eccbe DS |
105 | { |
106 | dev->attached = 0; | |
5617f9da | 107 | if (dev->driver) |
ed9eccbe | 108 | dev->driver->detach(dev); |
5617f9da | 109 | else |
4f870fe6 IA |
110 | dev_warn(dev->class_dev, |
111 | "BUG: dev->driver=NULL in comedi_device_detach()\n"); | |
ed9eccbe DS |
112 | cleanup_device(dev); |
113 | } | |
114 | ||
71b5f4f1 | 115 | void comedi_device_detach(struct comedi_device *dev) |
ed9eccbe DS |
116 | { |
117 | if (!dev->attached) | |
118 | return; | |
119 | __comedi_device_detach(dev); | |
120 | } | |
121 | ||
01fca378 | 122 | static int poll_invalid(struct comedi_device *dev, struct comedi_subdevice *s) |
ed9eccbe | 123 | { |
01fca378 | 124 | return -EINVAL; |
ed9eccbe DS |
125 | } |
126 | ||
01fca378 HS |
127 | int insn_inval(struct comedi_device *dev, struct comedi_subdevice *s, |
128 | struct comedi_insn *insn, unsigned int *data) | |
ed9eccbe | 129 | { |
01fca378 | 130 | return -EINVAL; |
ed9eccbe DS |
131 | } |
132 | ||
01fca378 HS |
133 | static int insn_rw_emulate_bits(struct comedi_device *dev, |
134 | struct comedi_subdevice *s, | |
135 | struct comedi_insn *insn, unsigned int *data) | |
ed9eccbe | 136 | { |
01fca378 HS |
137 | struct comedi_insn new_insn; |
138 | int ret; | |
139 | static const unsigned channels_per_bitfield = 32; | |
ed9eccbe | 140 | |
01fca378 HS |
141 | unsigned chan = CR_CHAN(insn->chanspec); |
142 | const unsigned base_bitfield_channel = | |
143 | (chan < channels_per_bitfield) ? 0 : chan; | |
144 | unsigned int new_data[2]; | |
145 | memset(new_data, 0, sizeof(new_data)); | |
146 | memset(&new_insn, 0, sizeof(new_insn)); | |
147 | new_insn.insn = INSN_BITS; | |
148 | new_insn.chanspec = base_bitfield_channel; | |
149 | new_insn.n = 2; | |
150 | new_insn.subdev = insn->subdev; | |
ed9eccbe | 151 | |
01fca378 HS |
152 | if (insn->insn == INSN_WRITE) { |
153 | if (!(s->subdev_flags & SDF_WRITABLE)) | |
154 | return -EINVAL; | |
155 | new_data[0] = 1 << (chan - base_bitfield_channel); /* mask */ | |
156 | new_data[1] = data[0] ? (1 << (chan - base_bitfield_channel)) | |
157 | : 0; /* bits */ | |
ed9eccbe DS |
158 | } |
159 | ||
01fca378 HS |
160 | ret = s->insn_bits(dev, s, &new_insn, new_data); |
161 | if (ret < 0) | |
162 | return ret; | |
ed9eccbe | 163 | |
01fca378 HS |
164 | if (insn->insn == INSN_READ) |
165 | data[0] = (new_data[1] >> (chan - base_bitfield_channel)) & 1; | |
166 | ||
167 | return 1; | |
ed9eccbe DS |
168 | } |
169 | ||
40f58a65 HS |
170 | static int __comedi_device_postconfig_async(struct comedi_device *dev, |
171 | struct comedi_subdevice *s) | |
172 | { | |
173 | struct comedi_async *async; | |
174 | unsigned int buf_size; | |
175 | int ret; | |
176 | ||
57b71c3e HS |
177 | if ((s->subdev_flags & (SDF_CMD_READ | SDF_CMD_WRITE)) == 0) { |
178 | dev_warn(dev->class_dev, | |
179 | "async subdevices must support SDF_CMD_READ or SDF_CMD_WRITE\n"); | |
180 | return -EINVAL; | |
181 | } | |
182 | if (!s->do_cmdtest) { | |
183 | dev_warn(dev->class_dev, | |
184 | "async subdevices must have a do_cmdtest() function\n"); | |
185 | return -EINVAL; | |
186 | } | |
40f58a65 HS |
187 | |
188 | async = kzalloc(sizeof(*async), GFP_KERNEL); | |
189 | if (!async) { | |
190 | dev_warn(dev->class_dev, "failed to allocate async struct\n"); | |
191 | return -ENOMEM; | |
192 | } | |
193 | init_waitqueue_head(&async->wait_head); | |
194 | async->subdevice = s; | |
195 | s->async = async; | |
196 | ||
197 | async->max_bufsize = comedi_default_buf_maxsize_kb * 1024; | |
198 | buf_size = comedi_default_buf_size_kb * 1024; | |
199 | if (buf_size > async->max_bufsize) | |
200 | buf_size = async->max_bufsize; | |
201 | ||
202 | if (comedi_buf_alloc(dev, s, buf_size) < 0) { | |
203 | dev_warn(dev->class_dev, "Buffer allocation failed\n"); | |
204 | return -ENOMEM; | |
205 | } | |
206 | if (s->buf_change) { | |
207 | ret = s->buf_change(dev, s, buf_size); | |
208 | if (ret < 0) | |
209 | return ret; | |
210 | } | |
211 | ||
212 | comedi_alloc_subdevice_minor(dev, s); | |
213 | ||
214 | return 0; | |
215 | } | |
216 | ||
217 | static int __comedi_device_postconfig(struct comedi_device *dev) | |
ed9eccbe | 218 | { |
34c43922 | 219 | struct comedi_subdevice *s; |
ed9eccbe | 220 | int ret; |
40f58a65 | 221 | int i; |
ed9eccbe DS |
222 | |
223 | for (i = 0; i < dev->n_subdevices; i++) { | |
5e4c58ce | 224 | s = &dev->subdevices[i]; |
ed9eccbe DS |
225 | |
226 | if (s->type == COMEDI_SUBD_UNUSED) | |
227 | continue; | |
228 | ||
229 | if (s->len_chanlist == 0) | |
230 | s->len_chanlist = 1; | |
231 | ||
232 | if (s->do_cmd) { | |
40f58a65 HS |
233 | ret = __comedi_device_postconfig_async(dev, s); |
234 | if (ret) | |
235 | return ret; | |
ed9eccbe DS |
236 | } |
237 | ||
238 | if (!s->range_table && !s->range_table_list) | |
239 | s->range_table = &range_unknown; | |
240 | ||
241 | if (!s->insn_read && s->insn_bits) | |
242 | s->insn_read = insn_rw_emulate_bits; | |
243 | if (!s->insn_write && s->insn_bits) | |
244 | s->insn_write = insn_rw_emulate_bits; | |
245 | ||
246 | if (!s->insn_read) | |
247 | s->insn_read = insn_inval; | |
248 | if (!s->insn_write) | |
249 | s->insn_write = insn_inval; | |
250 | if (!s->insn_bits) | |
251 | s->insn_bits = insn_inval; | |
252 | if (!s->insn_config) | |
253 | s->insn_config = insn_inval; | |
254 | ||
255 | if (!s->poll) | |
256 | s->poll = poll_invalid; | |
257 | } | |
258 | ||
259 | return 0; | |
260 | } | |
261 | ||
01fca378 HS |
262 | /* do a little post-config cleanup */ |
263 | /* called with module refcount incremented, decrements it */ | |
264 | static int comedi_device_postconfig(struct comedi_device *dev) | |
265 | { | |
40f58a65 | 266 | int ret = __comedi_device_postconfig(dev); |
01fca378 HS |
267 | module_put(dev->driver->module); |
268 | if (ret < 0) { | |
269 | __comedi_device_detach(dev); | |
270 | return ret; | |
271 | } | |
272 | if (!dev->board_name) { | |
273 | dev_warn(dev->class_dev, "BUG: dev->board_name=NULL\n"); | |
274 | dev->board_name = "BUG"; | |
275 | } | |
276 | smp_wmb(); | |
277 | dev->attached = 1; | |
278 | return 0; | |
279 | } | |
280 | ||
4e2f002f IA |
281 | /* |
282 | * Generic recognize function for drivers that register their supported | |
283 | * board names. | |
284 | * | |
285 | * 'driv->board_name' points to a 'const char *' member within the | |
286 | * zeroth element of an array of some private board information | |
287 | * structure, say 'struct foo_board' containing a member 'const char | |
288 | * *board_name' that is initialized to point to a board name string that | |
289 | * is one of the candidates matched against this function's 'name' | |
290 | * parameter. | |
291 | * | |
292 | * 'driv->offset' is the size of the private board information | |
293 | * structure, say 'sizeof(struct foo_board)', and 'driv->num_names' is | |
294 | * the length of the array of private board information structures. | |
295 | * | |
296 | * If one of the board names in the array of private board information | |
297 | * structures matches the name supplied to this function, the function | |
298 | * returns a pointer to the pointer to the board name, otherwise it | |
299 | * returns NULL. The return value ends up in the 'board_ptr' member of | |
300 | * a 'struct comedi_device' that the low-level comedi driver's | |
301 | * 'attach()' hook can convert to a point to a particular element of its | |
302 | * array of private board information structures by subtracting the | |
303 | * offset of the member that points to the board name. (No subtraction | |
304 | * is required if the board name pointer is the first member of the | |
305 | * private board information structure, which is generally the case.) | |
306 | */ | |
7029a874 | 307 | static void *comedi_recognize(struct comedi_driver *driv, const char *name) |
ed9eccbe | 308 | { |
1c9de58a DC |
309 | char **name_ptr = (char **)driv->board_name; |
310 | int i; | |
311 | ||
ed9eccbe DS |
312 | for (i = 0; i < driv->num_names; i++) { |
313 | if (strcmp(*name_ptr, name) == 0) | |
1c9de58a DC |
314 | return name_ptr; |
315 | name_ptr = (void *)name_ptr + driv->offset; | |
ed9eccbe DS |
316 | } |
317 | ||
318 | return NULL; | |
319 | } | |
320 | ||
7029a874 | 321 | static void comedi_report_boards(struct comedi_driver *driv) |
ed9eccbe DS |
322 | { |
323 | unsigned int i; | |
324 | const char *const *name_ptr; | |
325 | ||
4f870fe6 IA |
326 | pr_info("comedi: valid board names for %s driver are:\n", |
327 | driv->driver_name); | |
ed9eccbe DS |
328 | |
329 | name_ptr = driv->board_name; | |
330 | for (i = 0; i < driv->num_names; i++) { | |
4f870fe6 | 331 | pr_info(" %s\n", *name_ptr); |
ed9eccbe DS |
332 | name_ptr = (const char **)((char *)name_ptr + driv->offset); |
333 | } | |
334 | ||
335 | if (driv->num_names == 0) | |
4f870fe6 | 336 | pr_info(" %s\n", driv->driver_name); |
ed9eccbe DS |
337 | } |
338 | ||
01fca378 | 339 | int comedi_device_attach(struct comedi_device *dev, struct comedi_devconfig *it) |
ed9eccbe | 340 | { |
01fca378 HS |
341 | struct comedi_driver *driv; |
342 | int ret; | |
343 | ||
344 | if (dev->attached) | |
345 | return -EBUSY; | |
346 | ||
347 | for (driv = comedi_drivers; driv; driv = driv->next) { | |
348 | if (!try_module_get(driv->module)) | |
349 | continue; | |
350 | if (driv->num_names) { | |
351 | dev->board_ptr = comedi_recognize(driv, it->board_name); | |
352 | if (dev->board_ptr) | |
353 | break; | |
354 | } else if (strcmp(driv->driver_name, it->board_name) == 0) | |
355 | break; | |
356 | module_put(driv->module); | |
357 | } | |
358 | if (driv == NULL) { | |
359 | /* recognize has failed if we get here */ | |
360 | /* report valid board names before returning error */ | |
361 | for (driv = comedi_drivers; driv; driv = driv->next) { | |
362 | if (!try_module_get(driv->module)) | |
363 | continue; | |
364 | comedi_report_boards(driv); | |
365 | module_put(driv->module); | |
366 | } | |
367 | return -EIO; | |
368 | } | |
369 | if (driv->attach == NULL) { | |
370 | /* driver does not support manual configuration */ | |
371 | dev_warn(dev->class_dev, | |
372 | "driver '%s' does not support attach using comedi_config\n", | |
373 | driv->driver_name); | |
374 | module_put(driv->module); | |
375 | return -ENOSYS; | |
376 | } | |
377 | /* initialize dev->driver here so | |
378 | * comedi_error() can be called from attach */ | |
379 | dev->driver = driv; | |
380 | ret = driv->attach(dev, it); | |
381 | if (ret < 0) { | |
382 | module_put(dev->driver->module); | |
383 | __comedi_device_detach(dev); | |
384 | return ret; | |
385 | } | |
386 | return comedi_device_postconfig(dev); | |
ed9eccbe DS |
387 | } |
388 | ||
01fca378 | 389 | int comedi_driver_register(struct comedi_driver *driver) |
ed9eccbe | 390 | { |
01fca378 HS |
391 | driver->next = comedi_drivers; |
392 | comedi_drivers = driver; | |
393 | ||
394 | return 0; | |
ed9eccbe | 395 | } |
01fca378 | 396 | EXPORT_SYMBOL(comedi_driver_register); |
ed9eccbe | 397 | |
01fca378 | 398 | int comedi_driver_unregister(struct comedi_driver *driver) |
ed9eccbe | 399 | { |
01fca378 HS |
400 | struct comedi_driver *prev; |
401 | int i; | |
ed9eccbe | 402 | |
01fca378 HS |
403 | /* check for devices using this driver */ |
404 | for (i = 0; i < COMEDI_NUM_BOARD_MINORS; i++) { | |
405 | struct comedi_device *dev = comedi_dev_from_minor(i); | |
ed9eccbe | 406 | |
01fca378 HS |
407 | if (!dev) |
408 | continue; | |
ed9eccbe | 409 | |
01fca378 HS |
410 | mutex_lock(&dev->mutex); |
411 | if (dev->attached && dev->driver == driver) { | |
412 | if (dev->use_count) | |
413 | dev_warn(dev->class_dev, | |
414 | "BUG! detaching device with use_count=%d\n", | |
415 | dev->use_count); | |
416 | comedi_device_detach(dev); | |
417 | } | |
418 | mutex_unlock(&dev->mutex); | |
419 | } | |
ed9eccbe | 420 | |
01fca378 HS |
421 | if (comedi_drivers == driver) { |
422 | comedi_drivers = driver->next; | |
423 | return 0; | |
424 | } | |
ed9eccbe | 425 | |
01fca378 HS |
426 | for (prev = comedi_drivers; prev->next; prev = prev->next) { |
427 | if (prev->next == driver) { | |
428 | prev->next = driver->next; | |
429 | return 0; | |
430 | } | |
431 | } | |
432 | return -EINVAL; | |
ed9eccbe | 433 | } |
01fca378 | 434 | EXPORT_SYMBOL(comedi_driver_unregister); |
ed9eccbe | 435 | |
a588da1d IA |
436 | int comedi_auto_config(struct device *hardware_device, |
437 | struct comedi_driver *driver, unsigned long context) | |
f4011670 IA |
438 | { |
439 | int minor; | |
f4011670 IA |
440 | struct comedi_device *comedi_dev; |
441 | int ret; | |
442 | ||
443 | if (!comedi_autoconfig) | |
444 | return 0; | |
445 | ||
a588da1d IA |
446 | if (!driver->auto_attach) { |
447 | dev_warn(hardware_device, | |
448 | "BUG! comedi driver '%s' has no auto_attach handler\n", | |
449 | driver->driver_name); | |
450 | return -EINVAL; | |
451 | } | |
452 | ||
f4011670 IA |
453 | minor = comedi_alloc_board_minor(hardware_device); |
454 | if (minor < 0) | |
455 | return minor; | |
456 | ||
4da5fa9a | 457 | comedi_dev = comedi_dev_from_minor(minor); |
f4011670 IA |
458 | |
459 | mutex_lock(&comedi_dev->mutex); | |
460 | if (comedi_dev->attached) | |
461 | ret = -EBUSY; | |
4f870fe6 | 462 | else if (!try_module_get(driver->module)) |
f4011670 | 463 | ret = -EIO; |
4f870fe6 | 464 | else { |
26cbd465 | 465 | comedi_set_hw_dev(comedi_dev, hardware_device); |
f4011670 | 466 | comedi_dev->driver = driver; |
a588da1d | 467 | ret = driver->auto_attach(comedi_dev, context); |
f4011670 IA |
468 | if (ret < 0) { |
469 | module_put(driver->module); | |
470 | __comedi_device_detach(comedi_dev); | |
471 | } else { | |
472 | ret = comedi_device_postconfig(comedi_dev); | |
473 | } | |
474 | } | |
475 | mutex_unlock(&comedi_dev->mutex); | |
476 | ||
477 | if (ret < 0) | |
478 | comedi_free_board_minor(minor); | |
479 | return ret; | |
480 | } | |
8ed705af IA |
481 | EXPORT_SYMBOL_GPL(comedi_auto_config); |
482 | ||
483 | void comedi_auto_unconfig(struct device *hardware_device) | |
ed9eccbe | 484 | { |
c43435d7 | 485 | int minor; |
ed9eccbe | 486 | |
c43435d7 IA |
487 | if (hardware_device == NULL) |
488 | return; | |
489 | minor = comedi_find_board_minor(hardware_device); | |
490 | if (minor < 0) | |
491 | return; | |
c43435d7 | 492 | comedi_free_board_minor(minor); |
ed9eccbe | 493 | } |
8ed705af | 494 | EXPORT_SYMBOL_GPL(comedi_auto_unconfig); |