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