drivers.c revision d569541e537e13136fc775a902cda06f4c48bbe1
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
19#include <linux/device.h>
20#include <linux/module.h>
21#include <linux/errno.h>
22#include <linux/kconfig.h>
23#include <linux/kernel.h>
24#include <linux/sched.h>
25#include <linux/fcntl.h>
26#include <linux/delay.h>
27#include <linux/ioport.h>
28#include <linux/mm.h>
29#include <linux/slab.h>
30#include <linux/highmem.h>	/* for SuSE brokenness */
31#include <linux/vmalloc.h>
32#include <linux/cdev.h>
33#include <linux/dma-mapping.h>
34#include <linux/io.h>
35#include <linux/interrupt.h>
36#include <linux/firmware.h>
37
38#include "comedidev.h"
39#include "comedi_internal.h"
40
41struct comedi_driver *comedi_drivers;
42
43int comedi_set_hw_dev(struct comedi_device *dev, struct device *hw_dev)
44{
45	if (hw_dev == dev->hw_dev)
46		return 0;
47	if (dev->hw_dev != NULL)
48		return -EEXIST;
49	dev->hw_dev = get_device(hw_dev);
50	return 0;
51}
52EXPORT_SYMBOL_GPL(comedi_set_hw_dev);
53
54static void comedi_clear_hw_dev(struct comedi_device *dev)
55{
56	put_device(dev->hw_dev);
57	dev->hw_dev = NULL;
58}
59
60int comedi_alloc_subdevices(struct comedi_device *dev, int num_subdevices)
61{
62	struct comedi_subdevice *s;
63	int i;
64
65	if (num_subdevices < 1)
66		return -EINVAL;
67
68	s = kcalloc(num_subdevices, sizeof(*s), GFP_KERNEL);
69	if (!s)
70		return -ENOMEM;
71	dev->subdevices = s;
72	dev->n_subdevices = num_subdevices;
73
74	for (i = 0; i < num_subdevices; ++i) {
75		s = &dev->subdevices[i];
76		s->device = dev;
77		s->index = i;
78		s->async_dma_dir = DMA_NONE;
79		spin_lock_init(&s->spin_lock);
80		s->minor = -1;
81	}
82	return 0;
83}
84EXPORT_SYMBOL_GPL(comedi_alloc_subdevices);
85
86void comedi_spriv_free(struct comedi_device *dev, int subdev_num)
87{
88	struct comedi_subdevice *s;
89
90	if (dev->subdevices && subdev_num < dev->n_subdevices) {
91		s = &dev->subdevices[subdev_num];
92		kfree(s->private);
93		s->private = NULL;
94	}
95}
96EXPORT_SYMBOL_GPL(comedi_spriv_free);
97
98static void cleanup_device(struct comedi_device *dev)
99{
100	int i;
101	struct comedi_subdevice *s;
102
103	if (dev->subdevices) {
104		for (i = 0; i < dev->n_subdevices; i++) {
105			s = &dev->subdevices[i];
106			comedi_free_subdevice_minor(s);
107			if (s->async) {
108				comedi_buf_alloc(dev, s, 0);
109				kfree(s->async);
110			}
111		}
112		kfree(dev->subdevices);
113		dev->subdevices = NULL;
114		dev->n_subdevices = 0;
115	}
116	kfree(dev->private);
117	dev->private = NULL;
118	dev->driver = NULL;
119	dev->board_name = NULL;
120	dev->board_ptr = NULL;
121	dev->iobase = 0;
122	dev->iolen = 0;
123	dev->ioenabled = false;
124	dev->irq = 0;
125	dev->read_subdev = NULL;
126	dev->write_subdev = NULL;
127	dev->open = NULL;
128	dev->close = NULL;
129	comedi_clear_hw_dev(dev);
130}
131
132void comedi_device_detach(struct comedi_device *dev)
133{
134	dev->attached = false;
135	if (dev->driver)
136		dev->driver->detach(dev);
137	cleanup_device(dev);
138}
139
140static int poll_invalid(struct comedi_device *dev, struct comedi_subdevice *s)
141{
142	return -EINVAL;
143}
144
145int insn_inval(struct comedi_device *dev, struct comedi_subdevice *s,
146	       struct comedi_insn *insn, unsigned int *data)
147{
148	return -EINVAL;
149}
150
151static int insn_rw_emulate_bits(struct comedi_device *dev,
152				struct comedi_subdevice *s,
153				struct comedi_insn *insn, unsigned int *data)
154{
155	struct comedi_insn new_insn;
156	int ret;
157	static const unsigned channels_per_bitfield = 32;
158
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;
169
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 */
176	}
177
178	ret = s->insn_bits(dev, s, &new_insn, new_data);
179	if (ret < 0)
180		return ret;
181
182	if (insn->insn == INSN_READ)
183		data[0] = (new_data[1] >> (chan - base_bitfield_channel)) & 1;
184
185	return 1;
186}
187
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
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	}
205
206	async = kzalloc(sizeof(*async), GFP_KERNEL);
207	if (!async)
208		return -ENOMEM;
209
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
229	comedi_alloc_subdevice_minor(s);
230
231	return 0;
232}
233
234static int __comedi_device_postconfig(struct comedi_device *dev)
235{
236	struct comedi_subdevice *s;
237	int ret;
238	int i;
239
240	for (i = 0; i < dev->n_subdevices; i++) {
241		s = &dev->subdevices[i];
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) {
250			ret = __comedi_device_postconfig_async(dev, s);
251			if (ret)
252				return ret;
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
279/* do a little post-config cleanup */
280static int comedi_device_postconfig(struct comedi_device *dev)
281{
282	int ret;
283
284	ret = __comedi_device_postconfig(dev);
285	if (ret < 0)
286		return ret;
287	smp_wmb();
288	dev->attached = true;
289	return 0;
290}
291
292/*
293 * Generic recognize function for drivers that register their supported
294 * board names.
295 *
296 * 'driv->board_name' points to a 'const char *' member within the
297 * zeroth element of an array of some private board information
298 * structure, say 'struct foo_board' containing a member 'const char
299 * *board_name' that is initialized to point to a board name string that
300 * is one of the candidates matched against this function's 'name'
301 * parameter.
302 *
303 * 'driv->offset' is the size of the private board information
304 * structure, say 'sizeof(struct foo_board)', and 'driv->num_names' is
305 * the length of the array of private board information structures.
306 *
307 * If one of the board names in the array of private board information
308 * structures matches the name supplied to this function, the function
309 * returns a pointer to the pointer to the board name, otherwise it
310 * returns NULL.  The return value ends up in the 'board_ptr' member of
311 * a 'struct comedi_device' that the low-level comedi driver's
312 * 'attach()' hook can convert to a point to a particular element of its
313 * array of private board information structures by subtracting the
314 * offset of the member that points to the board name.  (No subtraction
315 * is required if the board name pointer is the first member of the
316 * private board information structure, which is generally the case.)
317 */
318static void *comedi_recognize(struct comedi_driver *driv, const char *name)
319{
320	char **name_ptr = (char **)driv->board_name;
321	int i;
322
323	for (i = 0; i < driv->num_names; i++) {
324		if (strcmp(*name_ptr, name) == 0)
325			return name_ptr;
326		name_ptr = (void *)name_ptr + driv->offset;
327	}
328
329	return NULL;
330}
331
332static void comedi_report_boards(struct comedi_driver *driv)
333{
334	unsigned int i;
335	const char *const *name_ptr;
336
337	pr_info("comedi: valid board names for %s driver are:\n",
338		driv->driver_name);
339
340	name_ptr = driv->board_name;
341	for (i = 0; i < driv->num_names; i++) {
342		pr_info(" %s\n", *name_ptr);
343		name_ptr = (const char **)((char *)name_ptr + driv->offset);
344	}
345
346	if (driv->num_names == 0)
347		pr_info(" %s\n", driv->driver_name);
348}
349
350/**
351 * comedi_load_firmware() - Request and load firmware for a device.
352 * @dev: comedi_device struct
353 * @hw_device: device struct for the comedi_device
354 * @name: the name of the firmware image
355 * @cb: callback to the upload the firmware image
356 * @context: private context from the driver
357 */
358int comedi_load_firmware(struct comedi_device *dev,
359			 struct device *device,
360			 const char *name,
361			 int (*cb)(struct comedi_device *dev,
362				   const u8 *data, size_t size,
363				   unsigned long context),
364			 unsigned long context)
365{
366	const struct firmware *fw;
367	int ret;
368
369	if (!cb)
370		return -EINVAL;
371
372	ret = request_firmware(&fw, name, device);
373	if (ret == 0) {
374		ret = cb(dev, fw->data, fw->size, context);
375		release_firmware(fw);
376	}
377
378	return ret;
379}
380EXPORT_SYMBOL_GPL(comedi_load_firmware);
381
382/**
383 * __comedi_request_region() - Request an I/O reqion for a legacy driver.
384 * @dev: comedi_device struct
385 * @start: base address of the I/O reqion
386 * @len: length of the I/O region
387 */
388int __comedi_request_region(struct comedi_device *dev,
389			    unsigned long start, unsigned long len)
390{
391	if (!start) {
392		dev_warn(dev->class_dev,
393			 "%s: a I/O base address must be specified\n",
394			 dev->board_name);
395		return -EINVAL;
396	}
397
398	if (!request_region(start, len, dev->board_name)) {
399		dev_warn(dev->class_dev, "%s: I/O port conflict (%#lx,%lu)\n",
400			 dev->board_name, start, len);
401		return -EIO;
402	}
403
404	return 0;
405}
406EXPORT_SYMBOL_GPL(__comedi_request_region);
407
408/**
409 * comedi_request_region() - Request an I/O reqion for a legacy driver.
410 * @dev: comedi_device struct
411 * @start: base address of the I/O reqion
412 * @len: length of the I/O region
413 */
414int comedi_request_region(struct comedi_device *dev,
415			  unsigned long start, unsigned long len)
416{
417	int ret;
418
419	ret = __comedi_request_region(dev, start, len);
420	if (ret == 0) {
421		dev->iobase = start;
422		dev->iolen = len;
423	}
424
425	return ret;
426}
427EXPORT_SYMBOL_GPL(comedi_request_region);
428
429/**
430 * comedi_legacy_detach() - A generic (*detach) function for legacy drivers.
431 * @dev: comedi_device struct
432 */
433void comedi_legacy_detach(struct comedi_device *dev)
434{
435	if (dev->irq) {
436		free_irq(dev->irq, dev);
437		dev->irq = 0;
438	}
439	if (dev->iobase && dev->iolen) {
440		release_region(dev->iobase, dev->iolen);
441		dev->iobase = 0;
442		dev->iolen = 0;
443	}
444}
445EXPORT_SYMBOL_GPL(comedi_legacy_detach);
446
447int comedi_device_attach(struct comedi_device *dev, struct comedi_devconfig *it)
448{
449	struct comedi_driver *driv;
450	int ret;
451
452	if (dev->attached)
453		return -EBUSY;
454
455	for (driv = comedi_drivers; driv; driv = driv->next) {
456		if (!try_module_get(driv->module))
457			continue;
458		if (driv->num_names) {
459			dev->board_ptr = comedi_recognize(driv, it->board_name);
460			if (dev->board_ptr)
461				break;
462		} else if (strcmp(driv->driver_name, it->board_name) == 0)
463			break;
464		module_put(driv->module);
465	}
466	if (driv == NULL) {
467		/*  recognize has failed if we get here */
468		/*  report valid board names before returning error */
469		for (driv = comedi_drivers; driv; driv = driv->next) {
470			if (!try_module_get(driv->module))
471				continue;
472			comedi_report_boards(driv);
473			module_put(driv->module);
474		}
475		return -EIO;
476	}
477	if (driv->attach == NULL) {
478		/* driver does not support manual configuration */
479		dev_warn(dev->class_dev,
480			 "driver '%s' does not support attach using comedi_config\n",
481			 driv->driver_name);
482		module_put(driv->module);
483		return -ENOSYS;
484	}
485	/* initialize dev->driver here so
486	 * comedi_error() can be called from attach */
487	dev->driver = driv;
488	dev->board_name = dev->board_ptr ? *(const char **)dev->board_ptr
489					 : dev->driver->driver_name;
490	ret = driv->attach(dev, it);
491	if (ret >= 0)
492		ret = comedi_device_postconfig(dev);
493	if (ret < 0) {
494		comedi_device_detach(dev);
495		module_put(dev->driver->module);
496	}
497	/* On success, the driver module count has been incremented. */
498	return ret;
499}
500
501int comedi_auto_config(struct device *hardware_device,
502		       struct comedi_driver *driver, unsigned long context)
503{
504	struct comedi_device *dev;
505	int ret;
506
507	if (!hardware_device) {
508		pr_warn("BUG! comedi_auto_config called with NULL hardware_device\n");
509		return -EINVAL;
510	}
511	if (!driver) {
512		dev_warn(hardware_device,
513			 "BUG! comedi_auto_config called with NULL comedi driver\n");
514		return -EINVAL;
515	}
516
517	if (!driver->auto_attach) {
518		dev_warn(hardware_device,
519			 "BUG! comedi driver '%s' has no auto_attach handler\n",
520			 driver->driver_name);
521		return -EINVAL;
522	}
523
524	dev = comedi_alloc_board_minor(hardware_device);
525	if (IS_ERR(dev))
526		return PTR_ERR(dev);
527	/* Note: comedi_alloc_board_minor() locked dev->mutex. */
528
529	dev->driver = driver;
530	dev->board_name = dev->driver->driver_name;
531	ret = driver->auto_attach(dev, context);
532	if (ret >= 0)
533		ret = comedi_device_postconfig(dev);
534	if (ret < 0)
535		comedi_device_detach(dev);
536	mutex_unlock(&dev->mutex);
537
538	if (ret < 0)
539		comedi_release_hardware_device(hardware_device);
540	return ret;
541}
542EXPORT_SYMBOL_GPL(comedi_auto_config);
543
544void comedi_auto_unconfig(struct device *hardware_device)
545{
546	if (hardware_device == NULL)
547		return;
548	comedi_release_hardware_device(hardware_device);
549}
550EXPORT_SYMBOL_GPL(comedi_auto_unconfig);
551
552int comedi_driver_register(struct comedi_driver *driver)
553{
554	driver->next = comedi_drivers;
555	comedi_drivers = driver;
556
557	return 0;
558}
559EXPORT_SYMBOL_GPL(comedi_driver_register);
560
561int comedi_driver_unregister(struct comedi_driver *driver)
562{
563	struct comedi_driver *prev;
564	int i;
565
566	/* check for devices using this driver */
567	for (i = 0; i < COMEDI_NUM_BOARD_MINORS; i++) {
568		struct comedi_device *dev = comedi_dev_from_minor(i);
569
570		if (!dev)
571			continue;
572
573		mutex_lock(&dev->mutex);
574		if (dev->attached && dev->driver == driver) {
575			if (dev->use_count)
576				dev_warn(dev->class_dev,
577					 "BUG! detaching device with use_count=%d\n",
578					 dev->use_count);
579			comedi_device_detach(dev);
580		}
581		mutex_unlock(&dev->mutex);
582	}
583
584	if (comedi_drivers == driver) {
585		comedi_drivers = driver->next;
586		return 0;
587	}
588
589	for (prev = comedi_drivers; prev->next; prev = prev->next) {
590		if (prev->next == driver) {
591			prev->next = driver->next;
592			return 0;
593		}
594	}
595	return -EINVAL;
596}
597EXPORT_SYMBOL_GPL(comedi_driver_unregister);
598