drivers.c revision f08e0ac59a93db2d8abc7000e2647164104524c6
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
24#include <linux/device.h>
25#include <linux/module.h>
26#include <linux/errno.h>
27#include <linux/kconfig.h>
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>
35#include <linux/highmem.h>	/* for SuSE brokenness */
36#include <linux/vmalloc.h>
37#include <linux/cdev.h>
38#include <linux/dma-mapping.h>
39#include <linux/io.h>
40
41#include "comedidev.h"
42#include "comedi_internal.h"
43
44struct comedi_driver *comedi_drivers;
45
46int comedi_set_hw_dev(struct comedi_device *dev, struct device *hw_dev)
47{
48	if (hw_dev == dev->hw_dev)
49		return 0;
50	if (dev->hw_dev != NULL)
51		return -EEXIST;
52	dev->hw_dev = get_device(hw_dev);
53	return 0;
54}
55EXPORT_SYMBOL_GPL(comedi_set_hw_dev);
56
57static void comedi_clear_hw_dev(struct comedi_device *dev)
58{
59	put_device(dev->hw_dev);
60	dev->hw_dev = NULL;
61}
62
63int comedi_alloc_subdevices(struct comedi_device *dev, int num_subdevices)
64{
65	struct comedi_subdevice *s;
66	int i;
67
68	if (num_subdevices < 1)
69		return -EINVAL;
70
71	s = kcalloc(num_subdevices, sizeof(*s), GFP_KERNEL);
72	if (!s)
73		return -ENOMEM;
74	dev->subdevices = s;
75	dev->n_subdevices = num_subdevices;
76
77	for (i = 0; i < num_subdevices; ++i) {
78		s = &dev->subdevices[i];
79		s->device = dev;
80		s->index = i;
81		s->async_dma_dir = DMA_NONE;
82		spin_lock_init(&s->spin_lock);
83		s->minor = -1;
84	}
85	return 0;
86}
87EXPORT_SYMBOL_GPL(comedi_alloc_subdevices);
88
89static void cleanup_device(struct comedi_device *dev)
90{
91	int i;
92	struct comedi_subdevice *s;
93
94	if (dev->subdevices) {
95		for (i = 0; i < dev->n_subdevices; i++) {
96			s = &dev->subdevices[i];
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	}
107	kfree(dev->private);
108	dev->private = NULL;
109	dev->driver = NULL;
110	dev->board_name = NULL;
111	dev->board_ptr = NULL;
112	dev->iobase = 0;
113	dev->ioenabled = false;
114	dev->irq = 0;
115	dev->read_subdev = NULL;
116	dev->write_subdev = NULL;
117	dev->open = NULL;
118	dev->close = NULL;
119	comedi_clear_hw_dev(dev);
120}
121
122static void __comedi_device_detach(struct comedi_device *dev)
123{
124	dev->attached = false;
125	if (dev->driver)
126		dev->driver->detach(dev);
127	else
128		dev_warn(dev->class_dev,
129			 "BUG: dev->driver=NULL in comedi_device_detach()\n");
130	cleanup_device(dev);
131}
132
133void comedi_device_detach(struct comedi_device *dev)
134{
135	if (!dev->attached)
136		return;
137	__comedi_device_detach(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 */
280/* called with module refcount incremented, decrements it */
281static int comedi_device_postconfig(struct comedi_device *dev)
282{
283	int ret = __comedi_device_postconfig(dev);
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();
294	dev->attached = true;
295	return 0;
296}
297
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 */
324static void *comedi_recognize(struct comedi_driver *driv, const char *name)
325{
326	char **name_ptr = (char **)driv->board_name;
327	int i;
328
329	for (i = 0; i < driv->num_names; i++) {
330		if (strcmp(*name_ptr, name) == 0)
331			return name_ptr;
332		name_ptr = (void *)name_ptr + driv->offset;
333	}
334
335	return NULL;
336}
337
338static void comedi_report_boards(struct comedi_driver *driv)
339{
340	unsigned int i;
341	const char *const *name_ptr;
342
343	pr_info("comedi: valid board names for %s driver are:\n",
344		driv->driver_name);
345
346	name_ptr = driv->board_name;
347	for (i = 0; i < driv->num_names; i++) {
348		pr_info(" %s\n", *name_ptr);
349		name_ptr = (const char **)((char *)name_ptr + driv->offset);
350	}
351
352	if (driv->num_names == 0)
353		pr_info(" %s\n", driv->driver_name);
354}
355
356int comedi_device_attach(struct comedi_device *dev, struct comedi_devconfig *it)
357{
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);
404}
405
406int comedi_auto_config(struct device *hardware_device,
407		       struct comedi_driver *driver, unsigned long context)
408{
409	int minor;
410	struct comedi_device *comedi_dev;
411	int ret;
412
413	if (!hardware_device) {
414		pr_warn("BUG! comedi_auto_config called with NULL hardware_device\n");
415		return -EINVAL;
416	}
417	if (!driver) {
418		dev_warn(hardware_device,
419			 "BUG! comedi_auto_config called with NULL comedi driver\n");
420		return -EINVAL;
421	}
422
423	if (!driver->auto_attach) {
424		dev_warn(hardware_device,
425			 "BUG! comedi driver '%s' has no auto_attach handler\n",
426			 driver->driver_name);
427		return -EINVAL;
428	}
429
430	minor = comedi_alloc_board_minor(hardware_device);
431	if (minor < 0)
432		return minor;
433
434	comedi_dev = comedi_dev_from_minor(minor);
435
436	mutex_lock(&comedi_dev->mutex);
437	if (comedi_dev->attached)
438		ret = -EBUSY;
439	else if (!try_module_get(driver->module))
440		ret = -EIO;
441	else {
442		comedi_set_hw_dev(comedi_dev, hardware_device);
443		comedi_dev->driver = driver;
444		ret = driver->auto_attach(comedi_dev, context);
445		if (ret < 0) {
446			module_put(driver->module);
447			__comedi_device_detach(comedi_dev);
448		} else {
449			ret = comedi_device_postconfig(comedi_dev);
450		}
451	}
452	mutex_unlock(&comedi_dev->mutex);
453
454	if (ret < 0)
455		comedi_free_board_minor(minor);
456	return ret;
457}
458EXPORT_SYMBOL_GPL(comedi_auto_config);
459
460void comedi_auto_unconfig(struct device *hardware_device)
461{
462	int minor;
463
464	if (hardware_device == NULL)
465		return;
466	minor = comedi_find_board_minor(hardware_device);
467	if (minor < 0)
468		return;
469	comedi_free_board_minor(minor);
470}
471EXPORT_SYMBOL_GPL(comedi_auto_unconfig);
472
473int comedi_driver_register(struct comedi_driver *driver)
474{
475	driver->next = comedi_drivers;
476	comedi_drivers = driver;
477
478	return 0;
479}
480EXPORT_SYMBOL(comedi_driver_register);
481
482int comedi_driver_unregister(struct comedi_driver *driver)
483{
484	struct comedi_driver *prev;
485	int i;
486
487	/* check for devices using this driver */
488	for (i = 0; i < COMEDI_NUM_BOARD_MINORS; i++) {
489		struct comedi_device *dev = comedi_dev_from_minor(i);
490
491		if (!dev)
492			continue;
493
494		mutex_lock(&dev->mutex);
495		if (dev->attached && dev->driver == driver) {
496			if (dev->use_count)
497				dev_warn(dev->class_dev,
498					 "BUG! detaching device with use_count=%d\n",
499					 dev->use_count);
500			comedi_device_detach(dev);
501		}
502		mutex_unlock(&dev->mutex);
503	}
504
505	if (comedi_drivers == driver) {
506		comedi_drivers = driver->next;
507		return 0;
508	}
509
510	for (prev = comedi_drivers; prev->next; prev = prev->next) {
511		if (prev->next == driver) {
512			prev->next = driver->next;
513			return 0;
514		}
515	}
516	return -EINVAL;
517}
518EXPORT_SYMBOL(comedi_driver_unregister);
519