]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/staging/comedi/drivers.c
Merge branch 'release' of git://git.kernel.org/pub/scm/linux/kernel/git/rzhang/linux
[karo-tx-linux.git] / drivers / staging / comedi / drivers.c
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
44 struct comedi_driver *comedi_drivers;
45
46 int 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 }
55 EXPORT_SYMBOL_GPL(comedi_set_hw_dev);
56
57 static void comedi_clear_hw_dev(struct comedi_device *dev)
58 {
59         put_device(dev->hw_dev);
60         dev->hw_dev = NULL;
61 }
62
63 int 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 }
87 EXPORT_SYMBOL_GPL(comedi_alloc_subdevices);
88
89 static 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->irq = 0;
114         dev->read_subdev = NULL;
115         dev->write_subdev = NULL;
116         dev->open = NULL;
117         dev->close = NULL;
118         comedi_clear_hw_dev(dev);
119 }
120
121 static void __comedi_device_detach(struct comedi_device *dev)
122 {
123         dev->attached = 0;
124         if (dev->driver)
125                 dev->driver->detach(dev);
126         else
127                 dev_warn(dev->class_dev,
128                          "BUG: dev->driver=NULL in comedi_device_detach()\n");
129         cleanup_device(dev);
130 }
131
132 void comedi_device_detach(struct comedi_device *dev)
133 {
134         if (!dev->attached)
135                 return;
136         __comedi_device_detach(dev);
137 }
138
139 static int poll_invalid(struct comedi_device *dev, struct comedi_subdevice *s)
140 {
141         return -EINVAL;
142 }
143
144 int insn_inval(struct comedi_device *dev, struct comedi_subdevice *s,
145                struct comedi_insn *insn, unsigned int *data)
146 {
147         return -EINVAL;
148 }
149
150 static int insn_rw_emulate_bits(struct comedi_device *dev,
151                                 struct comedi_subdevice *s,
152                                 struct comedi_insn *insn, unsigned int *data)
153 {
154         struct comedi_insn new_insn;
155         int ret;
156         static const unsigned channels_per_bitfield = 32;
157
158         unsigned chan = CR_CHAN(insn->chanspec);
159         const unsigned base_bitfield_channel =
160             (chan < channels_per_bitfield) ? 0 : chan;
161         unsigned int new_data[2];
162         memset(new_data, 0, sizeof(new_data));
163         memset(&new_insn, 0, sizeof(new_insn));
164         new_insn.insn = INSN_BITS;
165         new_insn.chanspec = base_bitfield_channel;
166         new_insn.n = 2;
167         new_insn.subdev = insn->subdev;
168
169         if (insn->insn == INSN_WRITE) {
170                 if (!(s->subdev_flags & SDF_WRITABLE))
171                         return -EINVAL;
172                 new_data[0] = 1 << (chan - base_bitfield_channel); /* mask */
173                 new_data[1] = data[0] ? (1 << (chan - base_bitfield_channel))
174                               : 0; /* bits */
175         }
176
177         ret = s->insn_bits(dev, s, &new_insn, new_data);
178         if (ret < 0)
179                 return ret;
180
181         if (insn->insn == INSN_READ)
182                 data[0] = (new_data[1] >> (chan - base_bitfield_channel)) & 1;
183
184         return 1;
185 }
186
187 static int __comedi_device_postconfig_async(struct comedi_device *dev,
188                                             struct comedi_subdevice *s)
189 {
190         struct comedi_async *async;
191         unsigned int buf_size;
192         int ret;
193
194         if ((s->subdev_flags & (SDF_CMD_READ | SDF_CMD_WRITE)) == 0) {
195                 dev_warn(dev->class_dev,
196                          "async subdevices must support SDF_CMD_READ or SDF_CMD_WRITE\n");
197                 return -EINVAL;
198         }
199         if (!s->do_cmdtest) {
200                 dev_warn(dev->class_dev,
201                          "async subdevices must have a do_cmdtest() function\n");
202                 return -EINVAL;
203         }
204
205         async = kzalloc(sizeof(*async), GFP_KERNEL);
206         if (!async)
207                 return -ENOMEM;
208
209         init_waitqueue_head(&async->wait_head);
210         async->subdevice = s;
211         s->async = async;
212
213         async->max_bufsize = comedi_default_buf_maxsize_kb * 1024;
214         buf_size = comedi_default_buf_size_kb * 1024;
215         if (buf_size > async->max_bufsize)
216                 buf_size = async->max_bufsize;
217
218         if (comedi_buf_alloc(dev, s, buf_size) < 0) {
219                 dev_warn(dev->class_dev, "Buffer allocation failed\n");
220                 return -ENOMEM;
221         }
222         if (s->buf_change) {
223                 ret = s->buf_change(dev, s, buf_size);
224                 if (ret < 0)
225                         return ret;
226         }
227
228         comedi_alloc_subdevice_minor(s);
229
230         return 0;
231 }
232
233 static int __comedi_device_postconfig(struct comedi_device *dev)
234 {
235         struct comedi_subdevice *s;
236         int ret;
237         int i;
238
239         for (i = 0; i < dev->n_subdevices; i++) {
240                 s = &dev->subdevices[i];
241
242                 if (s->type == COMEDI_SUBD_UNUSED)
243                         continue;
244
245                 if (s->len_chanlist == 0)
246                         s->len_chanlist = 1;
247
248                 if (s->do_cmd) {
249                         ret = __comedi_device_postconfig_async(dev, s);
250                         if (ret)
251                                 return ret;
252                 }
253
254                 if (!s->range_table && !s->range_table_list)
255                         s->range_table = &range_unknown;
256
257                 if (!s->insn_read && s->insn_bits)
258                         s->insn_read = insn_rw_emulate_bits;
259                 if (!s->insn_write && s->insn_bits)
260                         s->insn_write = insn_rw_emulate_bits;
261
262                 if (!s->insn_read)
263                         s->insn_read = insn_inval;
264                 if (!s->insn_write)
265                         s->insn_write = insn_inval;
266                 if (!s->insn_bits)
267                         s->insn_bits = insn_inval;
268                 if (!s->insn_config)
269                         s->insn_config = insn_inval;
270
271                 if (!s->poll)
272                         s->poll = poll_invalid;
273         }
274
275         return 0;
276 }
277
278 /* do a little post-config cleanup */
279 /* called with module refcount incremented, decrements it */
280 static int comedi_device_postconfig(struct comedi_device *dev)
281 {
282         int ret = __comedi_device_postconfig(dev);
283         module_put(dev->driver->module);
284         if (ret < 0) {
285                 __comedi_device_detach(dev);
286                 return ret;
287         }
288         if (!dev->board_name) {
289                 dev_warn(dev->class_dev, "BUG: dev->board_name=NULL\n");
290                 dev->board_name = "BUG";
291         }
292         smp_wmb();
293         dev->attached = 1;
294         return 0;
295 }
296
297 /*
298  * Generic recognize function for drivers that register their supported
299  * board names.
300  *
301  * 'driv->board_name' points to a 'const char *' member within the
302  * zeroth element of an array of some private board information
303  * structure, say 'struct foo_board' containing a member 'const char
304  * *board_name' that is initialized to point to a board name string that
305  * is one of the candidates matched against this function's 'name'
306  * parameter.
307  *
308  * 'driv->offset' is the size of the private board information
309  * structure, say 'sizeof(struct foo_board)', and 'driv->num_names' is
310  * the length of the array of private board information structures.
311  *
312  * If one of the board names in the array of private board information
313  * structures matches the name supplied to this function, the function
314  * returns a pointer to the pointer to the board name, otherwise it
315  * returns NULL.  The return value ends up in the 'board_ptr' member of
316  * a 'struct comedi_device' that the low-level comedi driver's
317  * 'attach()' hook can convert to a point to a particular element of its
318  * array of private board information structures by subtracting the
319  * offset of the member that points to the board name.  (No subtraction
320  * is required if the board name pointer is the first member of the
321  * private board information structure, which is generally the case.)
322  */
323 static void *comedi_recognize(struct comedi_driver *driv, const char *name)
324 {
325         char **name_ptr = (char **)driv->board_name;
326         int i;
327
328         for (i = 0; i < driv->num_names; i++) {
329                 if (strcmp(*name_ptr, name) == 0)
330                         return name_ptr;
331                 name_ptr = (void *)name_ptr + driv->offset;
332         }
333
334         return NULL;
335 }
336
337 static void comedi_report_boards(struct comedi_driver *driv)
338 {
339         unsigned int i;
340         const char *const *name_ptr;
341
342         pr_info("comedi: valid board names for %s driver are:\n",
343                 driv->driver_name);
344
345         name_ptr = driv->board_name;
346         for (i = 0; i < driv->num_names; i++) {
347                 pr_info(" %s\n", *name_ptr);
348                 name_ptr = (const char **)((char *)name_ptr + driv->offset);
349         }
350
351         if (driv->num_names == 0)
352                 pr_info(" %s\n", driv->driver_name);
353 }
354
355 int comedi_device_attach(struct comedi_device *dev, struct comedi_devconfig *it)
356 {
357         struct comedi_driver *driv;
358         int ret;
359
360         if (dev->attached)
361                 return -EBUSY;
362
363         for (driv = comedi_drivers; driv; driv = driv->next) {
364                 if (!try_module_get(driv->module))
365                         continue;
366                 if (driv->num_names) {
367                         dev->board_ptr = comedi_recognize(driv, it->board_name);
368                         if (dev->board_ptr)
369                                 break;
370                 } else if (strcmp(driv->driver_name, it->board_name) == 0)
371                         break;
372                 module_put(driv->module);
373         }
374         if (driv == NULL) {
375                 /*  recognize has failed if we get here */
376                 /*  report valid board names before returning error */
377                 for (driv = comedi_drivers; driv; driv = driv->next) {
378                         if (!try_module_get(driv->module))
379                                 continue;
380                         comedi_report_boards(driv);
381                         module_put(driv->module);
382                 }
383                 return -EIO;
384         }
385         if (driv->attach == NULL) {
386                 /* driver does not support manual configuration */
387                 dev_warn(dev->class_dev,
388                          "driver '%s' does not support attach using comedi_config\n",
389                          driv->driver_name);
390                 module_put(driv->module);
391                 return -ENOSYS;
392         }
393         /* initialize dev->driver here so
394          * comedi_error() can be called from attach */
395         dev->driver = driv;
396         ret = driv->attach(dev, it);
397         if (ret < 0) {
398                 module_put(dev->driver->module);
399                 __comedi_device_detach(dev);
400                 return ret;
401         }
402         return comedi_device_postconfig(dev);
403 }
404
405 int comedi_auto_config(struct device *hardware_device,
406                        struct comedi_driver *driver, unsigned long context)
407 {
408         int minor;
409         struct comedi_device *comedi_dev;
410         int ret;
411
412         if (!driver->auto_attach) {
413                 dev_warn(hardware_device,
414                          "BUG! comedi driver '%s' has no auto_attach handler\n",
415                          driver->driver_name);
416                 return -EINVAL;
417         }
418
419         minor = comedi_alloc_board_minor(hardware_device);
420         if (minor < 0)
421                 return minor;
422
423         comedi_dev = comedi_dev_from_minor(minor);
424
425         mutex_lock(&comedi_dev->mutex);
426         if (comedi_dev->attached)
427                 ret = -EBUSY;
428         else if (!try_module_get(driver->module))
429                 ret = -EIO;
430         else {
431                 comedi_set_hw_dev(comedi_dev, hardware_device);
432                 comedi_dev->driver = driver;
433                 ret = driver->auto_attach(comedi_dev, context);
434                 if (ret < 0) {
435                         module_put(driver->module);
436                         __comedi_device_detach(comedi_dev);
437                 } else {
438                         ret = comedi_device_postconfig(comedi_dev);
439                 }
440         }
441         mutex_unlock(&comedi_dev->mutex);
442
443         if (ret < 0)
444                 comedi_free_board_minor(minor);
445         return ret;
446 }
447 EXPORT_SYMBOL_GPL(comedi_auto_config);
448
449 void comedi_auto_unconfig(struct device *hardware_device)
450 {
451         int minor;
452
453         if (hardware_device == NULL)
454                 return;
455         minor = comedi_find_board_minor(hardware_device);
456         if (minor < 0)
457                 return;
458         comedi_free_board_minor(minor);
459 }
460 EXPORT_SYMBOL_GPL(comedi_auto_unconfig);
461
462 int comedi_driver_register(struct comedi_driver *driver)
463 {
464         driver->next = comedi_drivers;
465         comedi_drivers = driver;
466
467         return 0;
468 }
469 EXPORT_SYMBOL(comedi_driver_register);
470
471 int comedi_driver_unregister(struct comedi_driver *driver)
472 {
473         struct comedi_driver *prev;
474         int i;
475
476         /* check for devices using this driver */
477         for (i = 0; i < COMEDI_NUM_BOARD_MINORS; i++) {
478                 struct comedi_device *dev = comedi_dev_from_minor(i);
479
480                 if (!dev)
481                         continue;
482
483                 mutex_lock(&dev->mutex);
484                 if (dev->attached && dev->driver == driver) {
485                         if (dev->use_count)
486                                 dev_warn(dev->class_dev,
487                                          "BUG! detaching device with use_count=%d\n",
488                                          dev->use_count);
489                         comedi_device_detach(dev);
490                 }
491                 mutex_unlock(&dev->mutex);
492         }
493
494         if (comedi_drivers == driver) {
495                 comedi_drivers = driver->next;
496                 return 0;
497         }
498
499         for (prev = comedi_drivers; prev->next; prev = prev->next) {
500                 if (prev->next == driver) {
501                         prev->next = driver->next;
502                         return 0;
503                 }
504         }
505         return -EINVAL;
506 }
507 EXPORT_SYMBOL(comedi_driver_unregister);