]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/staging/comedi/drivers.c
staging: comedi: pcl724: add support for the PCM-IO48 PC/104 board
[mirror_ubuntu-bionic-kernel.git] / drivers / staging / comedi / drivers.c
CommitLineData
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>
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
ed9eccbe
DS
17*/
18
ed9eccbe
DS
19#include <linux/device.h>
20#include <linux/module.h>
ed9eccbe 21#include <linux/errno.h>
78b10615 22#include <linux/kconfig.h>
ed9eccbe
DS
23#include <linux/kernel.h>
24#include <linux/sched.h>
25#include <linux/fcntl.h>
ed9eccbe
DS
26#include <linux/ioport.h>
27#include <linux/mm.h>
28#include <linux/slab.h>
ed9eccbe
DS
29#include <linux/highmem.h> /* for SuSE brokenness */
30#include <linux/vmalloc.h>
31#include <linux/cdev.h>
32#include <linux/dma-mapping.h>
5617f9da 33#include <linux/io.h>
3d1fe3f7 34#include <linux/interrupt.h>
9ff8b151 35#include <linux/firmware.h>
ed9eccbe 36
242e7ad9 37#include "comedidev.h"
3a5fa275 38#include "comedi_internal.h"
242e7ad9 39
139dfbdf 40struct comedi_driver *comedi_drivers;
ed9eccbe 41
da717511
IA
42int comedi_set_hw_dev(struct comedi_device *dev, struct device *hw_dev)
43{
de06d7c6
IA
44 if (hw_dev == dev->hw_dev)
45 return 0;
46 if (dev->hw_dev != NULL)
47 return -EEXIST;
da717511 48 dev->hw_dev = get_device(hw_dev);
da717511
IA
49 return 0;
50}
51EXPORT_SYMBOL_GPL(comedi_set_hw_dev);
52
de06d7c6
IA
53static void comedi_clear_hw_dev(struct comedi_device *dev)
54{
55 put_device(dev->hw_dev);
56 dev->hw_dev = NULL;
57}
58
54db996e
HS
59/**
60 * comedi_alloc_devpriv() - Allocate memory for the device private data.
61 * @dev: comedi_device struct
62 * @size: size of the memory to allocate
63 */
64void *comedi_alloc_devpriv(struct comedi_device *dev, size_t size)
65{
66 dev->private = kzalloc(size, GFP_KERNEL);
67 return dev->private;
68}
69EXPORT_SYMBOL_GPL(comedi_alloc_devpriv);
70
8b9ba6e5 71int comedi_alloc_subdevices(struct comedi_device *dev, int num_subdevices)
2f0b9d08 72{
03afcf47 73 struct comedi_subdevice *s;
8b9ba6e5 74 int i;
2f0b9d08 75
7f801c41
HS
76 if (num_subdevices < 1)
77 return -EINVAL;
03afcf47
HS
78
79 s = kcalloc(num_subdevices, sizeof(*s), GFP_KERNEL);
80 if (!s)
2f0b9d08 81 return -ENOMEM;
03afcf47 82 dev->subdevices = s;
fba1d0fa 83 dev->n_subdevices = num_subdevices;
03afcf47 84
2f0b9d08 85 for (i = 0; i < num_subdevices; ++i) {
5e4c58ce 86 s = &dev->subdevices[i];
03afcf47 87 s->device = dev;
90a35c15 88 s->index = i;
03afcf47
HS
89 s->async_dma_dir = DMA_NONE;
90 spin_lock_init(&s->spin_lock);
91 s->minor = -1;
2f0b9d08
HS
92 }
93 return 0;
94}
95EXPORT_SYMBOL_GPL(comedi_alloc_subdevices);
96
71b5f4f1 97static void cleanup_device(struct comedi_device *dev)
ed9eccbe
DS
98{
99 int i;
34c43922 100 struct comedi_subdevice *s;
ed9eccbe
DS
101
102 if (dev->subdevices) {
103 for (i = 0; i < dev->n_subdevices; i++) {
5e4c58ce 104 s = &dev->subdevices[i];
588ba6dc
HS
105 if (s->runflags & SRF_FREE_SPRIV)
106 kfree(s->private);
ed9eccbe
DS
107 comedi_free_subdevice_minor(s);
108 if (s->async) {
109 comedi_buf_alloc(dev, s, 0);
110 kfree(s->async);
111 }
112 }
113 kfree(dev->subdevices);
114 dev->subdevices = NULL;
115 dev->n_subdevices = 0;
116 }
dedd1325
BP
117 kfree(dev->private);
118 dev->private = NULL;
7029a874 119 dev->driver = NULL;
ed9eccbe
DS
120 dev->board_name = NULL;
121 dev->board_ptr = NULL;
122 dev->iobase = 0;
316f97f1 123 dev->iolen = 0;
00ca6884 124 dev->ioenabled = false;
ed9eccbe
DS
125 dev->irq = 0;
126 dev->read_subdev = NULL;
127 dev->write_subdev = NULL;
128 dev->open = NULL;
129 dev->close = NULL;
de06d7c6 130 comedi_clear_hw_dev(dev);
ed9eccbe
DS
131}
132
016599f5 133void comedi_device_detach(struct comedi_device *dev)
ed9eccbe 134{
a7401cdd 135 dev->attached = false;
5617f9da 136 if (dev->driver)
ed9eccbe 137 dev->driver->detach(dev);
ed9eccbe
DS
138 cleanup_device(dev);
139}
140
01fca378 141static int poll_invalid(struct comedi_device *dev, struct comedi_subdevice *s)
ed9eccbe 142{
01fca378 143 return -EINVAL;
ed9eccbe
DS
144}
145
01fca378
HS
146int insn_inval(struct comedi_device *dev, struct comedi_subdevice *s,
147 struct comedi_insn *insn, unsigned int *data)
ed9eccbe 148{
01fca378 149 return -EINVAL;
ed9eccbe
DS
150}
151
01fca378
HS
152static int insn_rw_emulate_bits(struct comedi_device *dev,
153 struct comedi_subdevice *s,
154 struct comedi_insn *insn, unsigned int *data)
ed9eccbe 155{
01fca378
HS
156 struct comedi_insn new_insn;
157 int ret;
158 static const unsigned channels_per_bitfield = 32;
ed9eccbe 159
01fca378
HS
160 unsigned chan = CR_CHAN(insn->chanspec);
161 const unsigned base_bitfield_channel =
162 (chan < channels_per_bitfield) ? 0 : chan;
163 unsigned int new_data[2];
164 memset(new_data, 0, sizeof(new_data));
165 memset(&new_insn, 0, sizeof(new_insn));
166 new_insn.insn = INSN_BITS;
167 new_insn.chanspec = base_bitfield_channel;
168 new_insn.n = 2;
169 new_insn.subdev = insn->subdev;
ed9eccbe 170
01fca378
HS
171 if (insn->insn == INSN_WRITE) {
172 if (!(s->subdev_flags & SDF_WRITABLE))
173 return -EINVAL;
174 new_data[0] = 1 << (chan - base_bitfield_channel); /* mask */
175 new_data[1] = data[0] ? (1 << (chan - base_bitfield_channel))
176 : 0; /* bits */
ed9eccbe
DS
177 }
178
01fca378
HS
179 ret = s->insn_bits(dev, s, &new_insn, new_data);
180 if (ret < 0)
181 return ret;
ed9eccbe 182
01fca378
HS
183 if (insn->insn == INSN_READ)
184 data[0] = (new_data[1] >> (chan - base_bitfield_channel)) & 1;
185
186 return 1;
ed9eccbe
DS
187}
188
40f58a65
HS
189static int __comedi_device_postconfig_async(struct comedi_device *dev,
190 struct comedi_subdevice *s)
191{
192 struct comedi_async *async;
193 unsigned int buf_size;
194 int ret;
195
57b71c3e
HS
196 if ((s->subdev_flags & (SDF_CMD_READ | SDF_CMD_WRITE)) == 0) {
197 dev_warn(dev->class_dev,
198 "async subdevices must support SDF_CMD_READ or SDF_CMD_WRITE\n");
199 return -EINVAL;
200 }
201 if (!s->do_cmdtest) {
202 dev_warn(dev->class_dev,
203 "async subdevices must have a do_cmdtest() function\n");
204 return -EINVAL;
205 }
40f58a65
HS
206
207 async = kzalloc(sizeof(*async), GFP_KERNEL);
78110bb8 208 if (!async)
40f58a65 209 return -ENOMEM;
78110bb8 210
40f58a65
HS
211 init_waitqueue_head(&async->wait_head);
212 async->subdevice = s;
213 s->async = async;
214
215 async->max_bufsize = comedi_default_buf_maxsize_kb * 1024;
216 buf_size = comedi_default_buf_size_kb * 1024;
217 if (buf_size > async->max_bufsize)
218 buf_size = async->max_bufsize;
219
220 if (comedi_buf_alloc(dev, s, buf_size) < 0) {
221 dev_warn(dev->class_dev, "Buffer allocation failed\n");
222 return -ENOMEM;
223 }
224 if (s->buf_change) {
225 ret = s->buf_change(dev, s, buf_size);
226 if (ret < 0)
227 return ret;
228 }
229
f65cc544 230 comedi_alloc_subdevice_minor(s);
40f58a65
HS
231
232 return 0;
233}
234
235static int __comedi_device_postconfig(struct comedi_device *dev)
ed9eccbe 236{
34c43922 237 struct comedi_subdevice *s;
ed9eccbe 238 int ret;
40f58a65 239 int i;
ed9eccbe
DS
240
241 for (i = 0; i < dev->n_subdevices; i++) {
5e4c58ce 242 s = &dev->subdevices[i];
ed9eccbe
DS
243
244 if (s->type == COMEDI_SUBD_UNUSED)
245 continue;
246
247 if (s->len_chanlist == 0)
248 s->len_chanlist = 1;
249
250 if (s->do_cmd) {
40f58a65
HS
251 ret = __comedi_device_postconfig_async(dev, s);
252 if (ret)
253 return ret;
ed9eccbe
DS
254 }
255
256 if (!s->range_table && !s->range_table_list)
257 s->range_table = &range_unknown;
258
259 if (!s->insn_read && s->insn_bits)
260 s->insn_read = insn_rw_emulate_bits;
261 if (!s->insn_write && s->insn_bits)
262 s->insn_write = insn_rw_emulate_bits;
263
264 if (!s->insn_read)
265 s->insn_read = insn_inval;
266 if (!s->insn_write)
267 s->insn_write = insn_inval;
268 if (!s->insn_bits)
269 s->insn_bits = insn_inval;
270 if (!s->insn_config)
271 s->insn_config = insn_inval;
272
273 if (!s->poll)
274 s->poll = poll_invalid;
275 }
276
277 return 0;
278}
279
01fca378 280/* do a little post-config cleanup */
01fca378
HS
281static int comedi_device_postconfig(struct comedi_device *dev)
282{
b2a644b4
IA
283 int ret;
284
285 ret = __comedi_device_postconfig(dev);
ae5dd5fc 286 if (ret < 0)
01fca378 287 return ret;
01fca378 288 smp_wmb();
a7401cdd 289 dev->attached = true;
01fca378
HS
290 return 0;
291}
292
4e2f002f
IA
293/*
294 * Generic recognize function for drivers that register their supported
295 * board names.
296 *
297 * 'driv->board_name' points to a 'const char *' member within the
298 * zeroth element of an array of some private board information
299 * structure, say 'struct foo_board' containing a member 'const char
300 * *board_name' that is initialized to point to a board name string that
301 * is one of the candidates matched against this function's 'name'
302 * parameter.
303 *
304 * 'driv->offset' is the size of the private board information
305 * structure, say 'sizeof(struct foo_board)', and 'driv->num_names' is
306 * the length of the array of private board information structures.
307 *
308 * If one of the board names in the array of private board information
309 * structures matches the name supplied to this function, the function
310 * returns a pointer to the pointer to the board name, otherwise it
311 * returns NULL. The return value ends up in the 'board_ptr' member of
312 * a 'struct comedi_device' that the low-level comedi driver's
313 * 'attach()' hook can convert to a point to a particular element of its
314 * array of private board information structures by subtracting the
315 * offset of the member that points to the board name. (No subtraction
316 * is required if the board name pointer is the first member of the
317 * private board information structure, which is generally the case.)
318 */
7029a874 319static void *comedi_recognize(struct comedi_driver *driv, const char *name)
ed9eccbe 320{
1c9de58a
DC
321 char **name_ptr = (char **)driv->board_name;
322 int i;
323
ed9eccbe
DS
324 for (i = 0; i < driv->num_names; i++) {
325 if (strcmp(*name_ptr, name) == 0)
1c9de58a
DC
326 return name_ptr;
327 name_ptr = (void *)name_ptr + driv->offset;
ed9eccbe
DS
328 }
329
330 return NULL;
331}
332
7029a874 333static void comedi_report_boards(struct comedi_driver *driv)
ed9eccbe
DS
334{
335 unsigned int i;
336 const char *const *name_ptr;
337
4f870fe6
IA
338 pr_info("comedi: valid board names for %s driver are:\n",
339 driv->driver_name);
ed9eccbe
DS
340
341 name_ptr = driv->board_name;
342 for (i = 0; i < driv->num_names; i++) {
4f870fe6 343 pr_info(" %s\n", *name_ptr);
ed9eccbe
DS
344 name_ptr = (const char **)((char *)name_ptr + driv->offset);
345 }
346
347 if (driv->num_names == 0)
4f870fe6 348 pr_info(" %s\n", driv->driver_name);
ed9eccbe
DS
349}
350
9ff8b151
HS
351/**
352 * comedi_load_firmware() - Request and load firmware for a device.
353 * @dev: comedi_device struct
354 * @hw_device: device struct for the comedi_device
355 * @name: the name of the firmware image
356 * @cb: callback to the upload the firmware image
d569541e 357 * @context: private context from the driver
9ff8b151
HS
358 */
359int comedi_load_firmware(struct comedi_device *dev,
360 struct device *device,
361 const char *name,
362 int (*cb)(struct comedi_device *dev,
d569541e
HS
363 const u8 *data, size_t size,
364 unsigned long context),
365 unsigned long context)
9ff8b151
HS
366{
367 const struct firmware *fw;
368 int ret;
369
370 if (!cb)
371 return -EINVAL;
372
373 ret = request_firmware(&fw, name, device);
374 if (ret == 0) {
d569541e 375 ret = cb(dev, fw->data, fw->size, context);
9ff8b151
HS
376 release_firmware(fw);
377 }
378
379 return ret;
380}
381EXPORT_SYMBOL_GPL(comedi_load_firmware);
382
f375ac5f 383/**
ca8b2964 384 * __comedi_request_region() - Request an I/O reqion for a legacy driver.
f375ac5f
HS
385 * @dev: comedi_device struct
386 * @start: base address of the I/O reqion
387 * @len: length of the I/O region
388 */
ca8b2964
HS
389int __comedi_request_region(struct comedi_device *dev,
390 unsigned long start, unsigned long len)
f375ac5f
HS
391{
392 if (!start) {
393 dev_warn(dev->class_dev,
394 "%s: a I/O base address must be specified\n",
395 dev->board_name);
396 return -EINVAL;
397 }
398
399 if (!request_region(start, len, dev->board_name)) {
400 dev_warn(dev->class_dev, "%s: I/O port conflict (%#lx,%lu)\n",
401 dev->board_name, start, len);
402 return -EIO;
403 }
f375ac5f
HS
404
405 return 0;
406}
ca8b2964
HS
407EXPORT_SYMBOL_GPL(__comedi_request_region);
408
409/**
410 * comedi_request_region() - Request an I/O reqion for a legacy driver.
411 * @dev: comedi_device struct
412 * @start: base address of the I/O reqion
413 * @len: length of the I/O region
414 */
415int comedi_request_region(struct comedi_device *dev,
416 unsigned long start, unsigned long len)
417{
418 int ret;
419
420 ret = __comedi_request_region(dev, start, len);
316f97f1 421 if (ret == 0) {
ca8b2964 422 dev->iobase = start;
316f97f1
HS
423 dev->iolen = len;
424 }
ca8b2964
HS
425
426 return ret;
427}
f375ac5f
HS
428EXPORT_SYMBOL_GPL(comedi_request_region);
429
316f97f1
HS
430/**
431 * comedi_legacy_detach() - A generic (*detach) function for legacy drivers.
432 * @dev: comedi_device struct
433 */
434void comedi_legacy_detach(struct comedi_device *dev)
435{
3d1fe3f7
HS
436 if (dev->irq) {
437 free_irq(dev->irq, dev);
438 dev->irq = 0;
439 }
316f97f1
HS
440 if (dev->iobase && dev->iolen) {
441 release_region(dev->iobase, dev->iolen);
442 dev->iobase = 0;
443 dev->iolen = 0;
444 }
445}
446EXPORT_SYMBOL_GPL(comedi_legacy_detach);
447
01fca378 448int comedi_device_attach(struct comedi_device *dev, struct comedi_devconfig *it)
ed9eccbe 449{
01fca378
HS
450 struct comedi_driver *driv;
451 int ret;
452
453 if (dev->attached)
454 return -EBUSY;
455
456 for (driv = comedi_drivers; driv; driv = driv->next) {
457 if (!try_module_get(driv->module))
458 continue;
459 if (driv->num_names) {
460 dev->board_ptr = comedi_recognize(driv, it->board_name);
461 if (dev->board_ptr)
462 break;
463 } else if (strcmp(driv->driver_name, it->board_name) == 0)
464 break;
465 module_put(driv->module);
466 }
467 if (driv == NULL) {
468 /* recognize has failed if we get here */
469 /* report valid board names before returning error */
470 for (driv = comedi_drivers; driv; driv = driv->next) {
471 if (!try_module_get(driv->module))
472 continue;
473 comedi_report_boards(driv);
474 module_put(driv->module);
475 }
476 return -EIO;
477 }
478 if (driv->attach == NULL) {
479 /* driver does not support manual configuration */
480 dev_warn(dev->class_dev,
481 "driver '%s' does not support attach using comedi_config\n",
482 driv->driver_name);
483 module_put(driv->module);
484 return -ENOSYS;
485 }
486 /* initialize dev->driver here so
487 * comedi_error() can be called from attach */
488 dev->driver = driv;
34b68400
HS
489 dev->board_name = dev->board_ptr ? *(const char **)dev->board_ptr
490 : dev->driver->driver_name;
01fca378 491 ret = driv->attach(dev, it);
74ece108
IA
492 if (ret >= 0)
493 ret = comedi_device_postconfig(dev);
01fca378 494 if (ret < 0) {
016599f5 495 comedi_device_detach(dev);
dcd7b8bd 496 module_put(dev->driver->module);
01fca378 497 }
b2a644b4
IA
498 /* On success, the driver module count has been incremented. */
499 return ret;
ed9eccbe
DS
500}
501
a588da1d
IA
502int comedi_auto_config(struct device *hardware_device,
503 struct comedi_driver *driver, unsigned long context)
f4011670 504{
6013a9a5 505 struct comedi_device *dev;
f4011670
IA
506 int ret;
507
f08e0ac5
IA
508 if (!hardware_device) {
509 pr_warn("BUG! comedi_auto_config called with NULL hardware_device\n");
510 return -EINVAL;
511 }
512 if (!driver) {
513 dev_warn(hardware_device,
514 "BUG! comedi_auto_config called with NULL comedi driver\n");
515 return -EINVAL;
516 }
517
a588da1d
IA
518 if (!driver->auto_attach) {
519 dev_warn(hardware_device,
520 "BUG! comedi driver '%s' has no auto_attach handler\n",
521 driver->driver_name);
522 return -EINVAL;
523 }
524
6013a9a5
HS
525 dev = comedi_alloc_board_minor(hardware_device);
526 if (IS_ERR(dev))
527 return PTR_ERR(dev);
528 /* Note: comedi_alloc_board_minor() locked dev->mutex. */
f4011670 529
6013a9a5 530 dev->driver = driver;
34b68400 531 dev->board_name = dev->driver->driver_name;
6013a9a5 532 ret = driver->auto_attach(dev, context);
74ece108 533 if (ret >= 0)
6013a9a5 534 ret = comedi_device_postconfig(dev);
b2a644b4 535 if (ret < 0)
6013a9a5
HS
536 comedi_device_detach(dev);
537 mutex_unlock(&dev->mutex);
f4011670
IA
538
539 if (ret < 0)
f5b31e15 540 comedi_release_hardware_device(hardware_device);
f4011670
IA
541 return ret;
542}
8ed705af
IA
543EXPORT_SYMBOL_GPL(comedi_auto_config);
544
545void comedi_auto_unconfig(struct device *hardware_device)
ed9eccbe 546{
c43435d7
IA
547 if (hardware_device == NULL)
548 return;
3346b798 549 comedi_release_hardware_device(hardware_device);
ed9eccbe 550}
8ed705af 551EXPORT_SYMBOL_GPL(comedi_auto_unconfig);
1ae6b20b
HS
552
553int comedi_driver_register(struct comedi_driver *driver)
554{
555 driver->next = comedi_drivers;
556 comedi_drivers = driver;
557
558 return 0;
559}
5660e742 560EXPORT_SYMBOL_GPL(comedi_driver_register);
1ae6b20b
HS
561
562int comedi_driver_unregister(struct comedi_driver *driver)
563{
564 struct comedi_driver *prev;
565 int i;
566
567 /* check for devices using this driver */
568 for (i = 0; i < COMEDI_NUM_BOARD_MINORS; i++) {
569 struct comedi_device *dev = comedi_dev_from_minor(i);
570
571 if (!dev)
572 continue;
573
574 mutex_lock(&dev->mutex);
575 if (dev->attached && dev->driver == driver) {
576 if (dev->use_count)
577 dev_warn(dev->class_dev,
578 "BUG! detaching device with use_count=%d\n",
579 dev->use_count);
580 comedi_device_detach(dev);
581 }
582 mutex_unlock(&dev->mutex);
583 }
584
585 if (comedi_drivers == driver) {
586 comedi_drivers = driver->next;
587 return 0;
588 }
589
590 for (prev = comedi_drivers; prev->next; prev = prev->next) {
591 if (prev->next == driver) {
592 prev->next = driver->next;
593 return 0;
594 }
595 }
596 return -EINVAL;
597}
5660e742 598EXPORT_SYMBOL_GPL(comedi_driver_unregister);