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