1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
|
/*
module/drivers.c
functions for manipulating drivers
COMEDI - Linux Control and Measurement Device Interface
Copyright (C) 1997-2000 David A. Schleef <ds@schleef.org>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
*/
#include <linux/device.h>
#include <linux/module.h>
#include <linux/errno.h>
#include <linux/kconfig.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/fcntl.h>
#include <linux/ioport.h>
#include <linux/mm.h>
#include <linux/slab.h>
#include <linux/highmem.h> /* for SuSE brokenness */
#include <linux/vmalloc.h>
#include <linux/cdev.h>
#include <linux/dma-mapping.h>
#include <linux/io.h>
#include <linux/interrupt.h>
#include <linux/firmware.h>
#include "comedidev.h"
#include "comedi_internal.h"
struct comedi_driver *comedi_drivers;
DEFINE_MUTEX(comedi_drivers_list_lock);
int comedi_set_hw_dev(struct comedi_device *dev, struct device *hw_dev)
{
if (hw_dev == dev->hw_dev)
return 0;
if (dev->hw_dev != NULL)
return -EEXIST;
dev->hw_dev = get_device(hw_dev);
return 0;
}
EXPORT_SYMBOL_GPL(comedi_set_hw_dev);
static void comedi_clear_hw_dev(struct comedi_device *dev)
{
put_device(dev->hw_dev);
dev->hw_dev = NULL;
}
/**
* comedi_alloc_devpriv() - Allocate memory for the device private data.
* @dev: comedi_device struct
* @size: size of the memory to allocate
*/
void *comedi_alloc_devpriv(struct comedi_device *dev, size_t size)
{
dev->private = kzalloc(size, GFP_KERNEL);
return dev->private;
}
EXPORT_SYMBOL_GPL(comedi_alloc_devpriv);
int comedi_alloc_subdevices(struct comedi_device *dev, int num_subdevices)
{
struct comedi_subdevice *s;
int i;
if (num_subdevices < 1)
return -EINVAL;
s = kcalloc(num_subdevices, sizeof(*s), GFP_KERNEL);
if (!s)
return -ENOMEM;
dev->subdevices = s;
dev->n_subdevices = num_subdevices;
for (i = 0; i < num_subdevices; ++i) {
s = &dev->subdevices[i];
s->device = dev;
s->index = i;
s->async_dma_dir = DMA_NONE;
spin_lock_init(&s->spin_lock);
s->minor = -1;
}
return 0;
}
EXPORT_SYMBOL_GPL(comedi_alloc_subdevices);
static void comedi_device_detach_cleanup(struct comedi_device *dev)
{
int i;
struct comedi_subdevice *s;
if (dev->subdevices) {
for (i = 0; i < dev->n_subdevices; i++) {
s = &dev->subdevices[i];
if (s->runflags & SRF_FREE_SPRIV)
kfree(s->private);
comedi_free_subdevice_minor(s);
if (s->async) {
comedi_buf_alloc(dev, s, 0);
kfree(s->async);
}
}
kfree(dev->subdevices);
dev->subdevices = NULL;
dev->n_subdevices = 0;
}
kfree(dev->private);
dev->private = NULL;
dev->driver = NULL;
dev->board_name = NULL;
dev->board_ptr = NULL;
dev->iobase = 0;
dev->iolen = 0;
dev->ioenabled = false;
dev->irq = 0;
dev->read_subdev = NULL;
dev->write_subdev = NULL;
dev->open = NULL;
dev->close = NULL;
comedi_clear_hw_dev(dev);
}
void comedi_device_detach(struct comedi_device *dev)
{
comedi_device_cancel_all(dev);
down_write(&dev->attach_lock);
dev->attached = false;
dev->detach_count++;
if (dev->driver)
dev->driver->detach(dev);
comedi_device_detach_cleanup(dev);
up_write(&dev->attach_lock);
}
static int poll_invalid(struct comedi_device *dev, struct comedi_subdevice *s)
{
return -EINVAL;
}
int insn_inval(struct comedi_device *dev, struct comedi_subdevice *s,
struct comedi_insn *insn, unsigned int *data)
{
return -EINVAL;
}
/**
* comedi_dio_insn_config() - boilerplate (*insn_config) for DIO subdevices.
* @dev: comedi_device struct
* @s: comedi_subdevice struct
* @insn: comedi_insn struct
* @data: parameters for the @insn
* @mask: io_bits mask for grouped channels
*/
int comedi_dio_insn_config(struct comedi_device *dev,
struct comedi_subdevice *s,
struct comedi_insn *insn,
unsigned int *data,
unsigned int mask)
{
unsigned int chan_mask = 1 << CR_CHAN(insn->chanspec);
if (!mask)
mask = chan_mask;
switch (data[0]) {
case INSN_CONFIG_DIO_INPUT:
s->io_bits &= ~mask;
break;
case INSN_CONFIG_DIO_OUTPUT:
s->io_bits |= mask;
break;
case INSN_CONFIG_DIO_QUERY:
data[1] = (s->io_bits & mask) ? COMEDI_OUTPUT : COMEDI_INPUT;
return insn->n;
default:
return -EINVAL;
}
return 0;
}
EXPORT_SYMBOL_GPL(comedi_dio_insn_config);
/**
* comedi_dio_update_state() - update the internal state of DIO subdevices.
* @s: comedi_subdevice struct
* @data: the channel mask and bits to update
*/
unsigned int comedi_dio_update_state(struct comedi_subdevice *s,
unsigned int *data)
{
unsigned int chanmask = (s->n_chan < 32) ? ((1 << s->n_chan) - 1)
: 0xffffffff;
unsigned int mask = data[0] & chanmask;
unsigned int bits = data[1];
if (mask) {
s->state &= ~mask;
s->state |= (bits & mask);
}
return mask;
}
EXPORT_SYMBOL_GPL(comedi_dio_update_state);
static int insn_rw_emulate_bits(struct comedi_device *dev,
struct comedi_subdevice *s,
struct comedi_insn *insn, unsigned int *data)
{
struct comedi_insn new_insn;
int ret;
static const unsigned channels_per_bitfield = 32;
unsigned chan = CR_CHAN(insn->chanspec);
const unsigned base_bitfield_channel =
(chan < channels_per_bitfield) ? 0 : chan;
unsigned int new_data[2];
memset(new_data, 0, sizeof(new_data));
memset(&new_insn, 0, sizeof(new_insn));
new_insn.insn = INSN_BITS;
new_insn.chanspec = base_bitfield_channel;
new_insn.n = 2;
new_insn.subdev = insn->subdev;
if (insn->insn == INSN_WRITE) {
if (!(s->subdev_flags & SDF_WRITABLE))
return -EINVAL;
new_data[0] = 1 << (chan - base_bitfield_channel); /* mask */
new_data[1] = data[0] ? (1 << (chan - base_bitfield_channel))
: 0; /* bits */
}
ret = s->insn_bits(dev, s, &new_insn, new_data);
if (ret < 0)
return ret;
if (insn->insn == INSN_READ)
data[0] = (new_data[1] >> (chan - base_bitfield_channel)) & 1;
return 1;
}
static int __comedi_device_postconfig_async(struct comedi_device *dev,
struct comedi_subdevice *s)
{
struct comedi_async *async;
unsigned int buf_size;
int ret;
if ((s->subdev_flags & (SDF_CMD_READ | SDF_CMD_WRITE)) == 0) {
dev_warn(dev->class_dev,
"async subdevices must support SDF_CMD_READ or SDF_CMD_WRITE\n");
return -EINVAL;
}
if (!s->do_cmdtest) {
dev_warn(dev->class_dev,
"async subdevices must have a do_cmdtest() function\n");
return -EINVAL;
}
async = kzalloc(sizeof(*async), GFP_KERNEL);
if (!async)
return -ENOMEM;
init_waitqueue_head(&async->wait_head);
async->subdevice = s;
s->async = async;
async->max_bufsize = comedi_default_buf_maxsize_kb * 1024;
buf_size = comedi_default_buf_size_kb * 1024;
if (buf_size > async->max_bufsize)
buf_size = async->max_bufsize;
if (comedi_buf_alloc(dev, s, buf_size) < 0) {
dev_warn(dev->class_dev, "Buffer allocation failed\n");
return -ENOMEM;
}
if (s->buf_change) {
ret = s->buf_change(dev, s, buf_size);
if (ret < 0)
return ret;
}
comedi_alloc_subdevice_minor(s);
return 0;
}
static int __comedi_device_postconfig(struct comedi_device *dev)
{
struct comedi_subdevice *s;
int ret;
int i;
for (i = 0; i < dev->n_subdevices; i++) {
s = &dev->subdevices[i];
if (s->type == COMEDI_SUBD_UNUSED)
continue;
if (s->type == COMEDI_SUBD_DO) {
if (s->n_chan < 32)
s->io_bits = (1 << s->n_chan) - 1;
else
s->io_bits = 0xffffffff;
}
if (s->len_chanlist == 0)
s->len_chanlist = 1;
if (s->do_cmd) {
ret = __comedi_device_postconfig_async(dev, s);
if (ret)
return ret;
}
if (!s->range_table && !s->range_table_list)
s->range_table = &range_unknown;
if (!s->insn_read && s->insn_bits)
s->insn_read = insn_rw_emulate_bits;
if (!s->insn_write && s->insn_bits)
s->insn_write = insn_rw_emulate_bits;
if (!s->insn_read)
s->insn_read = insn_inval;
if (!s->insn_write)
s->insn_write = insn_inval;
if (!s->insn_bits)
s->insn_bits = insn_inval;
if (!s->insn_config)
s->insn_config = insn_inval;
if (!s->poll)
s->poll = poll_invalid;
}
return 0;
}
/* do a little post-config cleanup */
static int comedi_device_postconfig(struct comedi_device *dev)
{
int ret;
ret = __comedi_device_postconfig(dev);
if (ret < 0)
return ret;
down_write(&dev->attach_lock);
dev->attached = true;
up_write(&dev->attach_lock);
return 0;
}
/*
* Generic recognize function for drivers that register their supported
* board names.
*
* 'driv->board_name' points to a 'const char *' member within the
* zeroth element of an array of some private board information
* structure, say 'struct foo_board' containing a member 'const char
* *board_name' that is initialized to point to a board name string that
* is one of the candidates matched against this function's 'name'
* parameter.
*
* 'driv->offset' is the size of the private board information
* structure, say 'sizeof(struct foo_board)', and 'driv->num_names' is
* the length of the array of private board information structures.
*
* If one of the board names in the array of private board information
* structures matches the name supplied to this function, the function
* returns a pointer to the pointer to the board name, otherwise it
* returns NULL. The return value ends up in the 'board_ptr' member of
* a 'struct comedi_device' that the low-level comedi driver's
* 'attach()' hook can convert to a point to a particular element of its
* array of private board information structures by subtracting the
* offset of the member that points to the board name. (No subtraction
* is required if the board name pointer is the first member of the
* private board information structure, which is generally the case.)
*/
static void *comedi_recognize(struct comedi_driver *driv, const char *name)
{
char **name_ptr = (char **)driv->board_name;
int i;
for (i = 0; i < driv->num_names; i++) {
if (strcmp(*name_ptr, name) == 0)
return name_ptr;
name_ptr = (void *)name_ptr + driv->offset;
}
return NULL;
}
static void comedi_report_boards(struct comedi_driver *driv)
{
unsigned int i;
const char *const *name_ptr;
pr_info("comedi: valid board names for %s driver are:\n",
driv->driver_name);
name_ptr = driv->board_name;
for (i = 0; i < driv->num_names; i++) {
pr_info(" %s\n", *name_ptr);
name_ptr = (const char **)((char *)name_ptr + driv->offset);
}
if (driv->num_names == 0)
pr_info(" %s\n", driv->driver_name);
}
/**
* comedi_load_firmware() - Request and load firmware for a device.
* @dev: comedi_device struct
* @hw_device: device struct for the comedi_device
* @name: the name of the firmware image
* @cb: callback to the upload the firmware image
* @context: private context from the driver
*/
int comedi_load_firmware(struct comedi_device *dev,
struct device *device,
const char *name,
int (*cb)(struct comedi_device *dev,
const u8 *data, size_t size,
unsigned long context),
unsigned long context)
{
const struct firmware *fw;
int ret;
if (!cb)
return -EINVAL;
ret = request_firmware(&fw, name, device);
if (ret == 0) {
ret = cb(dev, fw->data, fw->size, context);
release_firmware(fw);
}
return ret < 0 ? ret : 0;
}
EXPORT_SYMBOL_GPL(comedi_load_firmware);
/**
* __comedi_request_region() - Request an I/O reqion for a legacy driver.
* @dev: comedi_device struct
* @start: base address of the I/O reqion
* @len: length of the I/O region
*/
int __comedi_request_region(struct comedi_device *dev,
unsigned long start, unsigned long len)
{
if (!start) {
dev_warn(dev->class_dev,
"%s: a I/O base address must be specified\n",
dev->board_name);
return -EINVAL;
}
if (!request_region(start, len, dev->board_name)) {
dev_warn(dev->class_dev, "%s: I/O port conflict (%#lx,%lu)\n",
dev->board_name, start, len);
return -EIO;
}
return 0;
}
EXPORT_SYMBOL_GPL(__comedi_request_region);
/**
* comedi_request_region() - Request an I/O reqion for a legacy driver.
* @dev: comedi_device struct
* @start: base address of the I/O reqion
* @len: length of the I/O region
*/
int comedi_request_region(struct comedi_device *dev,
unsigned long start, unsigned long len)
{
int ret;
ret = __comedi_request_region(dev, start, len);
if (ret == 0) {
dev->iobase = start;
dev->iolen = len;
}
return ret;
}
EXPORT_SYMBOL_GPL(comedi_request_region);
/**
* comedi_legacy_detach() - A generic (*detach) function for legacy drivers.
* @dev: comedi_device struct
*/
void comedi_legacy_detach(struct comedi_device *dev)
{
if (dev->irq) {
free_irq(dev->irq, dev);
dev->irq = 0;
}
if (dev->iobase && dev->iolen) {
release_region(dev->iobase, dev->iolen);
dev->iobase = 0;
dev->iolen = 0;
}
}
EXPORT_SYMBOL_GPL(comedi_legacy_detach);
int comedi_device_attach(struct comedi_device *dev, struct comedi_devconfig *it)
{
struct comedi_driver *driv;
int ret;
if (dev->attached)
return -EBUSY;
mutex_lock(&comedi_drivers_list_lock);
for (driv = comedi_drivers; driv; driv = driv->next) {
if (!try_module_get(driv->module))
continue;
if (driv->num_names) {
dev->board_ptr = comedi_recognize(driv, it->board_name);
if (dev->board_ptr)
break;
} else if (strcmp(driv->driver_name, it->board_name) == 0)
break;
module_put(driv->module);
}
if (driv == NULL) {
/* recognize has failed if we get here */
/* report valid board names before returning error */
for (driv = comedi_drivers; driv; driv = driv->next) {
if (!try_module_get(driv->module))
continue;
comedi_report_boards(driv);
module_put(driv->module);
}
ret = -EIO;
goto out;
}
if (driv->attach == NULL) {
/* driver does not support manual configuration */
dev_warn(dev->class_dev,
"driver '%s' does not support attach using comedi_config\n",
driv->driver_name);
module_put(driv->module);
ret = -ENOSYS;
goto out;
}
/* initialize dev->driver here so
* comedi_error() can be called from attach */
dev->driver = driv;
dev->board_name = dev->board_ptr ? *(const char **)dev->board_ptr
: dev->driver->driver_name;
ret = driv->attach(dev, it);
if (ret >= 0)
ret = comedi_device_postconfig(dev);
if (ret < 0) {
comedi_device_detach(dev);
module_put(driv->module);
}
/* On success, the driver module count has been incremented. */
out:
mutex_unlock(&comedi_drivers_list_lock);
return ret;
}
int comedi_auto_config(struct device *hardware_device,
struct comedi_driver *driver, unsigned long context)
{
struct comedi_device *dev;
int ret;
if (!hardware_device) {
pr_warn("BUG! comedi_auto_config called with NULL hardware_device\n");
return -EINVAL;
}
if (!driver) {
dev_warn(hardware_device,
"BUG! comedi_auto_config called with NULL comedi driver\n");
return -EINVAL;
}
if (!driver->auto_attach) {
dev_warn(hardware_device,
"BUG! comedi driver '%s' has no auto_attach handler\n",
driver->driver_name);
return -EINVAL;
}
dev = comedi_alloc_board_minor(hardware_device);
if (IS_ERR(dev)) {
dev_warn(hardware_device,
"driver '%s' could not create device.\n",
driver->driver_name);
return PTR_ERR(dev);
}
/* Note: comedi_alloc_board_minor() locked dev->mutex. */
dev->driver = driver;
dev->board_name = dev->driver->driver_name;
ret = driver->auto_attach(dev, context);
if (ret >= 0)
ret = comedi_device_postconfig(dev);
mutex_unlock(&dev->mutex);
if (ret < 0) {
dev_warn(hardware_device,
"driver '%s' failed to auto-configure device.\n",
driver->driver_name);
comedi_release_hardware_device(hardware_device);
} else {
/*
* class_dev should be set properly here
* after a successful auto config
*/
dev_info(dev->class_dev,
"driver '%s' has successfully auto-configured '%s'.\n",
driver->driver_name, dev->board_name);
}
return ret;
}
EXPORT_SYMBOL_GPL(comedi_auto_config);
void comedi_auto_unconfig(struct device *hardware_device)
{
if (hardware_device == NULL)
return;
comedi_release_hardware_device(hardware_device);
}
EXPORT_SYMBOL_GPL(comedi_auto_unconfig);
int comedi_driver_register(struct comedi_driver *driver)
{
mutex_lock(&comedi_drivers_list_lock);
driver->next = comedi_drivers;
comedi_drivers = driver;
mutex_unlock(&comedi_drivers_list_lock);
return 0;
}
EXPORT_SYMBOL_GPL(comedi_driver_register);
void comedi_driver_unregister(struct comedi_driver *driver)
{
struct comedi_driver *prev;
int i;
/* unlink the driver */
mutex_lock(&comedi_drivers_list_lock);
if (comedi_drivers == driver) {
comedi_drivers = driver->next;
} else {
for (prev = comedi_drivers; prev->next; prev = prev->next) {
if (prev->next == driver) {
prev->next = driver->next;
break;
}
}
}
mutex_unlock(&comedi_drivers_list_lock);
/* check for devices using this driver */
for (i = 0; i < COMEDI_NUM_BOARD_MINORS; i++) {
struct comedi_device *dev = comedi_dev_get_from_minor(i);
if (!dev)
continue;
mutex_lock(&dev->mutex);
if (dev->attached && dev->driver == driver) {
if (dev->use_count)
dev_warn(dev->class_dev,
"BUG! detaching device with use_count=%d\n",
dev->use_count);
comedi_device_detach(dev);
}
mutex_unlock(&dev->mutex);
comedi_dev_put(dev);
}
}
EXPORT_SYMBOL_GPL(comedi_driver_unregister);
|