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