Merge 3.9-rc5 into staging-next
[deliverable/linux.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.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21
22*/
23
ed9eccbe
DS
24#include <linux/device.h>
25#include <linux/module.h>
ed9eccbe 26#include <linux/errno.h>
78b10615 27#include <linux/kconfig.h>
ed9eccbe
DS
28#include <linux/kernel.h>
29#include <linux/sched.h>
30#include <linux/fcntl.h>
31#include <linux/delay.h>
32#include <linux/ioport.h>
33#include <linux/mm.h>
34#include <linux/slab.h>
ed9eccbe
DS
35#include <linux/highmem.h> /* for SuSE brokenness */
36#include <linux/vmalloc.h>
37#include <linux/cdev.h>
38#include <linux/dma-mapping.h>
5617f9da 39#include <linux/io.h>
ed9eccbe 40
242e7ad9 41#include "comedidev.h"
3a5fa275 42#include "comedi_internal.h"
242e7ad9 43
139dfbdf 44struct comedi_driver *comedi_drivers;
ed9eccbe 45
da717511
IA
46int comedi_set_hw_dev(struct comedi_device *dev, struct device *hw_dev)
47{
de06d7c6
IA
48 if (hw_dev == dev->hw_dev)
49 return 0;
50 if (dev->hw_dev != NULL)
51 return -EEXIST;
da717511 52 dev->hw_dev = get_device(hw_dev);
da717511
IA
53 return 0;
54}
55EXPORT_SYMBOL_GPL(comedi_set_hw_dev);
56
de06d7c6
IA
57static void comedi_clear_hw_dev(struct comedi_device *dev)
58{
59 put_device(dev->hw_dev);
60 dev->hw_dev = NULL;
61}
62
8b9ba6e5 63int comedi_alloc_subdevices(struct comedi_device *dev, int num_subdevices)
2f0b9d08 64{
03afcf47 65 struct comedi_subdevice *s;
8b9ba6e5 66 int i;
2f0b9d08 67
7f801c41
HS
68 if (num_subdevices < 1)
69 return -EINVAL;
03afcf47
HS
70
71 s = kcalloc(num_subdevices, sizeof(*s), GFP_KERNEL);
72 if (!s)
2f0b9d08 73 return -ENOMEM;
03afcf47 74 dev->subdevices = s;
fba1d0fa 75 dev->n_subdevices = num_subdevices;
03afcf47 76
2f0b9d08 77 for (i = 0; i < num_subdevices; ++i) {
5e4c58ce 78 s = &dev->subdevices[i];
03afcf47 79 s->device = dev;
90a35c15 80 s->index = i;
03afcf47
HS
81 s->async_dma_dir = DMA_NONE;
82 spin_lock_init(&s->spin_lock);
83 s->minor = -1;
2f0b9d08
HS
84 }
85 return 0;
86}
87EXPORT_SYMBOL_GPL(comedi_alloc_subdevices);
88
71b5f4f1 89static void cleanup_device(struct comedi_device *dev)
ed9eccbe
DS
90{
91 int i;
34c43922 92 struct comedi_subdevice *s;
ed9eccbe
DS
93
94 if (dev->subdevices) {
95 for (i = 0; i < dev->n_subdevices; i++) {
5e4c58ce 96 s = &dev->subdevices[i];
ed9eccbe
DS
97 comedi_free_subdevice_minor(s);
98 if (s->async) {
99 comedi_buf_alloc(dev, s, 0);
100 kfree(s->async);
101 }
102 }
103 kfree(dev->subdevices);
104 dev->subdevices = NULL;
105 dev->n_subdevices = 0;
106 }
dedd1325
BP
107 kfree(dev->private);
108 dev->private = NULL;
7029a874 109 dev->driver = NULL;
ed9eccbe
DS
110 dev->board_name = NULL;
111 dev->board_ptr = NULL;
112 dev->iobase = 0;
00ca6884 113 dev->ioenabled = false;
ed9eccbe
DS
114 dev->irq = 0;
115 dev->read_subdev = NULL;
116 dev->write_subdev = NULL;
117 dev->open = NULL;
118 dev->close = NULL;
de06d7c6 119 comedi_clear_hw_dev(dev);
ed9eccbe
DS
120}
121
71b5f4f1 122static void __comedi_device_detach(struct comedi_device *dev)
ed9eccbe 123{
a7401cdd 124 dev->attached = false;
5617f9da 125 if (dev->driver)
ed9eccbe 126 dev->driver->detach(dev);
5617f9da 127 else
4f870fe6
IA
128 dev_warn(dev->class_dev,
129 "BUG: dev->driver=NULL in comedi_device_detach()\n");
ed9eccbe
DS
130 cleanup_device(dev);
131}
132
71b5f4f1 133void comedi_device_detach(struct comedi_device *dev)
ed9eccbe
DS
134{
135 if (!dev->attached)
136 return;
137 __comedi_device_detach(dev);
138}
139
01fca378 140static int poll_invalid(struct comedi_device *dev, struct comedi_subdevice *s)
ed9eccbe 141{
01fca378 142 return -EINVAL;
ed9eccbe
DS
143}
144
01fca378
HS
145int insn_inval(struct comedi_device *dev, struct comedi_subdevice *s,
146 struct comedi_insn *insn, unsigned int *data)
ed9eccbe 147{
01fca378 148 return -EINVAL;
ed9eccbe
DS
149}
150
01fca378
HS
151static int insn_rw_emulate_bits(struct comedi_device *dev,
152 struct comedi_subdevice *s,
153 struct comedi_insn *insn, unsigned int *data)
ed9eccbe 154{
01fca378
HS
155 struct comedi_insn new_insn;
156 int ret;
157 static const unsigned channels_per_bitfield = 32;
ed9eccbe 158
01fca378
HS
159 unsigned chan = CR_CHAN(insn->chanspec);
160 const unsigned base_bitfield_channel =
161 (chan < channels_per_bitfield) ? 0 : chan;
162 unsigned int new_data[2];
163 memset(new_data, 0, sizeof(new_data));
164 memset(&new_insn, 0, sizeof(new_insn));
165 new_insn.insn = INSN_BITS;
166 new_insn.chanspec = base_bitfield_channel;
167 new_insn.n = 2;
168 new_insn.subdev = insn->subdev;
ed9eccbe 169
01fca378
HS
170 if (insn->insn == INSN_WRITE) {
171 if (!(s->subdev_flags & SDF_WRITABLE))
172 return -EINVAL;
173 new_data[0] = 1 << (chan - base_bitfield_channel); /* mask */
174 new_data[1] = data[0] ? (1 << (chan - base_bitfield_channel))
175 : 0; /* bits */
ed9eccbe
DS
176 }
177
01fca378
HS
178 ret = s->insn_bits(dev, s, &new_insn, new_data);
179 if (ret < 0)
180 return ret;
ed9eccbe 181
01fca378
HS
182 if (insn->insn == INSN_READ)
183 data[0] = (new_data[1] >> (chan - base_bitfield_channel)) & 1;
184
185 return 1;
ed9eccbe
DS
186}
187
40f58a65
HS
188static int __comedi_device_postconfig_async(struct comedi_device *dev,
189 struct comedi_subdevice *s)
190{
191 struct comedi_async *async;
192 unsigned int buf_size;
193 int ret;
194
57b71c3e
HS
195 if ((s->subdev_flags & (SDF_CMD_READ | SDF_CMD_WRITE)) == 0) {
196 dev_warn(dev->class_dev,
197 "async subdevices must support SDF_CMD_READ or SDF_CMD_WRITE\n");
198 return -EINVAL;
199 }
200 if (!s->do_cmdtest) {
201 dev_warn(dev->class_dev,
202 "async subdevices must have a do_cmdtest() function\n");
203 return -EINVAL;
204 }
40f58a65
HS
205
206 async = kzalloc(sizeof(*async), GFP_KERNEL);
78110bb8 207 if (!async)
40f58a65 208 return -ENOMEM;
78110bb8 209
40f58a65
HS
210 init_waitqueue_head(&async->wait_head);
211 async->subdevice = s;
212 s->async = async;
213
214 async->max_bufsize = comedi_default_buf_maxsize_kb * 1024;
215 buf_size = comedi_default_buf_size_kb * 1024;
216 if (buf_size > async->max_bufsize)
217 buf_size = async->max_bufsize;
218
219 if (comedi_buf_alloc(dev, s, buf_size) < 0) {
220 dev_warn(dev->class_dev, "Buffer allocation failed\n");
221 return -ENOMEM;
222 }
223 if (s->buf_change) {
224 ret = s->buf_change(dev, s, buf_size);
225 if (ret < 0)
226 return ret;
227 }
228
f65cc544 229 comedi_alloc_subdevice_minor(s);
40f58a65
HS
230
231 return 0;
232}
233
234static int __comedi_device_postconfig(struct comedi_device *dev)
ed9eccbe 235{
34c43922 236 struct comedi_subdevice *s;
ed9eccbe 237 int ret;
40f58a65 238 int i;
ed9eccbe
DS
239
240 for (i = 0; i < dev->n_subdevices; i++) {
5e4c58ce 241 s = &dev->subdevices[i];
ed9eccbe
DS
242
243 if (s->type == COMEDI_SUBD_UNUSED)
244 continue;
245
246 if (s->len_chanlist == 0)
247 s->len_chanlist = 1;
248
249 if (s->do_cmd) {
40f58a65
HS
250 ret = __comedi_device_postconfig_async(dev, s);
251 if (ret)
252 return ret;
ed9eccbe
DS
253 }
254
255 if (!s->range_table && !s->range_table_list)
256 s->range_table = &range_unknown;
257
258 if (!s->insn_read && s->insn_bits)
259 s->insn_read = insn_rw_emulate_bits;
260 if (!s->insn_write && s->insn_bits)
261 s->insn_write = insn_rw_emulate_bits;
262
263 if (!s->insn_read)
264 s->insn_read = insn_inval;
265 if (!s->insn_write)
266 s->insn_write = insn_inval;
267 if (!s->insn_bits)
268 s->insn_bits = insn_inval;
269 if (!s->insn_config)
270 s->insn_config = insn_inval;
271
272 if (!s->poll)
273 s->poll = poll_invalid;
274 }
275
276 return 0;
277}
278
01fca378
HS
279/* do a little post-config cleanup */
280/* called with module refcount incremented, decrements it */
281static int comedi_device_postconfig(struct comedi_device *dev)
282{
40f58a65 283 int ret = __comedi_device_postconfig(dev);
01fca378
HS
284 module_put(dev->driver->module);
285 if (ret < 0) {
286 __comedi_device_detach(dev);
287 return ret;
288 }
289 if (!dev->board_name) {
290 dev_warn(dev->class_dev, "BUG: dev->board_name=NULL\n");
291 dev->board_name = "BUG";
292 }
293 smp_wmb();
a7401cdd 294 dev->attached = true;
01fca378
HS
295 return 0;
296}
297
4e2f002f
IA
298/*
299 * Generic recognize function for drivers that register their supported
300 * board names.
301 *
302 * 'driv->board_name' points to a 'const char *' member within the
303 * zeroth element of an array of some private board information
304 * structure, say 'struct foo_board' containing a member 'const char
305 * *board_name' that is initialized to point to a board name string that
306 * is one of the candidates matched against this function's 'name'
307 * parameter.
308 *
309 * 'driv->offset' is the size of the private board information
310 * structure, say 'sizeof(struct foo_board)', and 'driv->num_names' is
311 * the length of the array of private board information structures.
312 *
313 * If one of the board names in the array of private board information
314 * structures matches the name supplied to this function, the function
315 * returns a pointer to the pointer to the board name, otherwise it
316 * returns NULL. The return value ends up in the 'board_ptr' member of
317 * a 'struct comedi_device' that the low-level comedi driver's
318 * 'attach()' hook can convert to a point to a particular element of its
319 * array of private board information structures by subtracting the
320 * offset of the member that points to the board name. (No subtraction
321 * is required if the board name pointer is the first member of the
322 * private board information structure, which is generally the case.)
323 */
7029a874 324static void *comedi_recognize(struct comedi_driver *driv, const char *name)
ed9eccbe 325{
1c9de58a
DC
326 char **name_ptr = (char **)driv->board_name;
327 int i;
328
ed9eccbe
DS
329 for (i = 0; i < driv->num_names; i++) {
330 if (strcmp(*name_ptr, name) == 0)
1c9de58a
DC
331 return name_ptr;
332 name_ptr = (void *)name_ptr + driv->offset;
ed9eccbe
DS
333 }
334
335 return NULL;
336}
337
7029a874 338static void comedi_report_boards(struct comedi_driver *driv)
ed9eccbe
DS
339{
340 unsigned int i;
341 const char *const *name_ptr;
342
4f870fe6
IA
343 pr_info("comedi: valid board names for %s driver are:\n",
344 driv->driver_name);
ed9eccbe
DS
345
346 name_ptr = driv->board_name;
347 for (i = 0; i < driv->num_names; i++) {
4f870fe6 348 pr_info(" %s\n", *name_ptr);
ed9eccbe
DS
349 name_ptr = (const char **)((char *)name_ptr + driv->offset);
350 }
351
352 if (driv->num_names == 0)
4f870fe6 353 pr_info(" %s\n", driv->driver_name);
ed9eccbe
DS
354}
355
01fca378 356int comedi_device_attach(struct comedi_device *dev, struct comedi_devconfig *it)
ed9eccbe 357{
01fca378
HS
358 struct comedi_driver *driv;
359 int ret;
360
361 if (dev->attached)
362 return -EBUSY;
363
364 for (driv = comedi_drivers; driv; driv = driv->next) {
365 if (!try_module_get(driv->module))
366 continue;
367 if (driv->num_names) {
368 dev->board_ptr = comedi_recognize(driv, it->board_name);
369 if (dev->board_ptr)
370 break;
371 } else if (strcmp(driv->driver_name, it->board_name) == 0)
372 break;
373 module_put(driv->module);
374 }
375 if (driv == NULL) {
376 /* recognize has failed if we get here */
377 /* report valid board names before returning error */
378 for (driv = comedi_drivers; driv; driv = driv->next) {
379 if (!try_module_get(driv->module))
380 continue;
381 comedi_report_boards(driv);
382 module_put(driv->module);
383 }
384 return -EIO;
385 }
386 if (driv->attach == NULL) {
387 /* driver does not support manual configuration */
388 dev_warn(dev->class_dev,
389 "driver '%s' does not support attach using comedi_config\n",
390 driv->driver_name);
391 module_put(driv->module);
392 return -ENOSYS;
393 }
394 /* initialize dev->driver here so
395 * comedi_error() can be called from attach */
396 dev->driver = driv;
397 ret = driv->attach(dev, it);
398 if (ret < 0) {
399 module_put(dev->driver->module);
400 __comedi_device_detach(dev);
401 return ret;
402 }
403 return comedi_device_postconfig(dev);
ed9eccbe
DS
404}
405
a588da1d
IA
406int comedi_auto_config(struct device *hardware_device,
407 struct comedi_driver *driver, unsigned long context)
f4011670
IA
408{
409 int minor;
f4011670
IA
410 struct comedi_device *comedi_dev;
411 int ret;
412
a588da1d
IA
413 if (!driver->auto_attach) {
414 dev_warn(hardware_device,
415 "BUG! comedi driver '%s' has no auto_attach handler\n",
416 driver->driver_name);
417 return -EINVAL;
418 }
419
f4011670
IA
420 minor = comedi_alloc_board_minor(hardware_device);
421 if (minor < 0)
422 return minor;
423
4da5fa9a 424 comedi_dev = comedi_dev_from_minor(minor);
f4011670
IA
425
426 mutex_lock(&comedi_dev->mutex);
427 if (comedi_dev->attached)
428 ret = -EBUSY;
4f870fe6 429 else if (!try_module_get(driver->module))
f4011670 430 ret = -EIO;
4f870fe6 431 else {
26cbd465 432 comedi_set_hw_dev(comedi_dev, hardware_device);
f4011670 433 comedi_dev->driver = driver;
a588da1d 434 ret = driver->auto_attach(comedi_dev, context);
f4011670
IA
435 if (ret < 0) {
436 module_put(driver->module);
437 __comedi_device_detach(comedi_dev);
438 } else {
439 ret = comedi_device_postconfig(comedi_dev);
440 }
441 }
442 mutex_unlock(&comedi_dev->mutex);
443
444 if (ret < 0)
445 comedi_free_board_minor(minor);
446 return ret;
447}
8ed705af
IA
448EXPORT_SYMBOL_GPL(comedi_auto_config);
449
450void comedi_auto_unconfig(struct device *hardware_device)
ed9eccbe 451{
c43435d7 452 int minor;
ed9eccbe 453
c43435d7
IA
454 if (hardware_device == NULL)
455 return;
456 minor = comedi_find_board_minor(hardware_device);
457 if (minor < 0)
458 return;
c43435d7 459 comedi_free_board_minor(minor);
ed9eccbe 460}
8ed705af 461EXPORT_SYMBOL_GPL(comedi_auto_unconfig);
1ae6b20b
HS
462
463int comedi_driver_register(struct comedi_driver *driver)
464{
465 driver->next = comedi_drivers;
466 comedi_drivers = driver;
467
468 return 0;
469}
470EXPORT_SYMBOL(comedi_driver_register);
471
472int comedi_driver_unregister(struct comedi_driver *driver)
473{
474 struct comedi_driver *prev;
475 int i;
476
477 /* check for devices using this driver */
478 for (i = 0; i < COMEDI_NUM_BOARD_MINORS; i++) {
479 struct comedi_device *dev = comedi_dev_from_minor(i);
480
481 if (!dev)
482 continue;
483
484 mutex_lock(&dev->mutex);
485 if (dev->attached && dev->driver == driver) {
486 if (dev->use_count)
487 dev_warn(dev->class_dev,
488 "BUG! detaching device with use_count=%d\n",
489 dev->use_count);
490 comedi_device_detach(dev);
491 }
492 mutex_unlock(&dev->mutex);
493 }
494
495 if (comedi_drivers == driver) {
496 comedi_drivers = driver->next;
497 return 0;
498 }
499
500 for (prev = comedi_drivers; prev->next; prev = prev->next) {
501 if (prev->next == driver) {
502 prev->next = driver->next;
503 return 0;
504 }
505 }
506 return -EINVAL;
507}
508EXPORT_SYMBOL(comedi_driver_unregister);
This page took 0.458617 seconds and 5 git commands to generate.