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