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#include <linux/module.h>
77#include <linux/interrupt.h>
78
79#include "../comedidev.h"
80
81#include "comedi_fc.h"
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100#define PCMUIO_PORT_REG(x) (0x00 + (x))
101#define PCMUIO_INT_PENDING_REG 0x06
102#define PCMUIO_PAGE_LOCK_REG 0x07
103#define PCMUIO_LOCK_PORT(x) ((1 << (x)) & 0x3f)
104#define PCMUIO_PAGE(x) (((x) & 0x3) << 6)
105#define PCMUIO_PAGE_MASK PCMUIO_PAGE(3)
106#define PCMUIO_PAGE_POL 1
107#define PCMUIO_PAGE_ENAB 2
108#define PCMUIO_PAGE_INT_ID 3
109#define PCMUIO_PAGE_REG(x) (0x08 + (x))
110
111#define PCMUIO_ASIC_IOSIZE 0x10
112#define PCMUIO_MAX_ASICS 2
113
114struct pcmuio_board {
115 const char *name;
116 const int num_asics;
117};
118
119static const struct pcmuio_board pcmuio_boards[] = {
120 {
121 .name = "pcmuio48",
122 .num_asics = 1,
123 }, {
124 .name = "pcmuio96",
125 .num_asics = 2,
126 },
127};
128
129struct pcmuio_asic {
130 spinlock_t pagelock;
131 spinlock_t spinlock;
132 unsigned int enabled_mask;
133 unsigned int stop_count;
134 unsigned int active:1;
135 unsigned int continuous:1;
136};
137
138struct pcmuio_private {
139 struct pcmuio_asic asics[PCMUIO_MAX_ASICS];
140 unsigned int irq2;
141};
142
143static inline unsigned long pcmuio_asic_iobase(struct comedi_device *dev,
144 int asic)
145{
146 return dev->iobase + (asic * PCMUIO_ASIC_IOSIZE);
147}
148
149static inline int pcmuio_subdevice_to_asic(struct comedi_subdevice *s)
150{
151
152
153
154
155 return s->index / 2;
156}
157
158static inline int pcmuio_subdevice_to_port(struct comedi_subdevice *s)
159{
160
161
162
163
164 return (s->index % 2) ? 3 : 0;
165}
166
167static void pcmuio_write(struct comedi_device *dev, unsigned int val,
168 int asic, int page, int port)
169{
170 struct pcmuio_private *devpriv = dev->private;
171 struct pcmuio_asic *chip = &devpriv->asics[asic];
172 unsigned long iobase = pcmuio_asic_iobase(dev, asic);
173 unsigned long flags;
174
175 spin_lock_irqsave(&chip->pagelock, flags);
176 if (page == 0) {
177
178 outb(val & 0xff, iobase + PCMUIO_PORT_REG(port + 0));
179 outb((val >> 8) & 0xff, iobase + PCMUIO_PORT_REG(port + 1));
180 outb((val >> 16) & 0xff, iobase + PCMUIO_PORT_REG(port + 2));
181 } else {
182 outb(PCMUIO_PAGE(page), iobase + PCMUIO_PAGE_LOCK_REG);
183 outb(val & 0xff, iobase + PCMUIO_PAGE_REG(0));
184 outb((val >> 8) & 0xff, iobase + PCMUIO_PAGE_REG(1));
185 outb((val >> 16) & 0xff, iobase + PCMUIO_PAGE_REG(2));
186 }
187 spin_unlock_irqrestore(&chip->pagelock, flags);
188}
189
190static unsigned int pcmuio_read(struct comedi_device *dev,
191 int asic, int page, int port)
192{
193 struct pcmuio_private *devpriv = dev->private;
194 struct pcmuio_asic *chip = &devpriv->asics[asic];
195 unsigned long iobase = pcmuio_asic_iobase(dev, asic);
196 unsigned long flags;
197 unsigned int val;
198
199 spin_lock_irqsave(&chip->pagelock, flags);
200 if (page == 0) {
201
202 val = inb(iobase + PCMUIO_PORT_REG(port + 0));
203 val |= (inb(iobase + PCMUIO_PORT_REG(port + 1)) << 8);
204 val |= (inb(iobase + PCMUIO_PORT_REG(port + 2)) << 16);
205 } else {
206 outb(PCMUIO_PAGE(page), iobase + PCMUIO_PAGE_LOCK_REG);
207 val = inb(iobase + PCMUIO_PAGE_REG(0));
208 val |= (inb(iobase + PCMUIO_PAGE_REG(1)) << 8);
209 val |= (inb(iobase + PCMUIO_PAGE_REG(2)) << 16);
210 }
211 spin_unlock_irqrestore(&chip->pagelock, flags);
212
213 return val;
214}
215
216
217
218
219
220
221
222
223
224
225
226static int pcmuio_dio_insn_bits(struct comedi_device *dev,
227 struct comedi_subdevice *s,
228 struct comedi_insn *insn,
229 unsigned int *data)
230{
231 int asic = pcmuio_subdevice_to_asic(s);
232 int port = pcmuio_subdevice_to_port(s);
233 unsigned int chanmask = (1 << s->n_chan) - 1;
234 unsigned int mask;
235 unsigned int val;
236
237 mask = comedi_dio_update_state(s, data);
238 if (mask) {
239
240
241
242
243
244
245
246
247 val = ~s->state & chanmask;
248 val &= s->io_bits;
249 pcmuio_write(dev, val, asic, 0, port);
250 }
251
252
253 val = pcmuio_read(dev, asic, 0, port);
254
255
256 data[1] = ~val & chanmask;
257
258 return insn->n;
259}
260
261static int pcmuio_dio_insn_config(struct comedi_device *dev,
262 struct comedi_subdevice *s,
263 struct comedi_insn *insn,
264 unsigned int *data)
265{
266 int asic = pcmuio_subdevice_to_asic(s);
267 int port = pcmuio_subdevice_to_port(s);
268 int ret;
269
270 ret = comedi_dio_insn_config(dev, s, insn, data, 0);
271 if (ret)
272 return ret;
273
274 if (data[0] == INSN_CONFIG_DIO_INPUT)
275 pcmuio_write(dev, s->io_bits, asic, 0, port);
276
277 return insn->n;
278}
279
280static void pcmuio_reset(struct comedi_device *dev)
281{
282 const struct pcmuio_board *board = comedi_board(dev);
283 int asic;
284
285 for (asic = 0; asic < board->num_asics; ++asic) {
286
287 pcmuio_write(dev, 0, asic, 0, 0);
288 pcmuio_write(dev, 0, asic, 0, 3);
289
290
291 pcmuio_write(dev, 0, asic, PCMUIO_PAGE_POL, 0);
292 pcmuio_write(dev, 0, asic, PCMUIO_PAGE_ENAB, 0);
293 pcmuio_write(dev, 0, asic, PCMUIO_PAGE_INT_ID, 0);
294 }
295}
296
297
298static void pcmuio_stop_intr(struct comedi_device *dev,
299 struct comedi_subdevice *s)
300{
301 struct pcmuio_private *devpriv = dev->private;
302 int asic = pcmuio_subdevice_to_asic(s);
303 struct pcmuio_asic *chip = &devpriv->asics[asic];
304
305 chip->enabled_mask = 0;
306 chip->active = 0;
307 s->async->inttrig = NULL;
308
309
310 pcmuio_write(dev, 0, asic, PCMUIO_PAGE_ENAB, 0);
311}
312
313static void pcmuio_handle_intr_subdev(struct comedi_device *dev,
314 struct comedi_subdevice *s,
315 unsigned triggered)
316{
317 struct pcmuio_private *devpriv = dev->private;
318 int asic = pcmuio_subdevice_to_asic(s);
319 struct pcmuio_asic *chip = &devpriv->asics[asic];
320 unsigned int len = s->async->cmd.chanlist_len;
321 unsigned oldevents = s->async->events;
322 unsigned int val = 0;
323 unsigned long flags;
324 unsigned int i;
325
326 spin_lock_irqsave(&chip->spinlock, flags);
327
328 if (!chip->active)
329 goto done;
330
331 if (!(triggered & chip->enabled_mask))
332 goto done;
333
334 for (i = 0; i < len; i++) {
335 unsigned int chan = CR_CHAN(s->async->cmd.chanlist[i]);
336 if (triggered & (1 << chan))
337 val |= (1 << i);
338 }
339
340
341 if (comedi_buf_put(s->async, val) &&
342 comedi_buf_put(s->async, val >> 16)) {
343 s->async->events |= (COMEDI_CB_BLOCK | COMEDI_CB_EOS);
344 } else {
345
346
347 pcmuio_stop_intr(dev, s);
348 }
349
350
351 if (!chip->continuous) {
352
353 if (chip->stop_count > 0) {
354 chip->stop_count--;
355 if (chip->stop_count == 0) {
356 s->async->events |= COMEDI_CB_EOA;
357
358 pcmuio_stop_intr(dev, s);
359 }
360 }
361 }
362
363done:
364 spin_unlock_irqrestore(&chip->spinlock, flags);
365
366 if (oldevents != s->async->events)
367 comedi_event(dev, s);
368}
369
370static int pcmuio_handle_asic_interrupt(struct comedi_device *dev, int asic)
371{
372
373 struct comedi_subdevice *s = &dev->subdevices[asic * 2];
374 unsigned long iobase = pcmuio_asic_iobase(dev, asic);
375 unsigned int val;
376
377
378 val = inb(iobase + PCMUIO_INT_PENDING_REG) & 0x07;
379 if (!val)
380 return 0;
381
382
383 val = pcmuio_read(dev, asic, PCMUIO_PAGE_INT_ID, 0);
384 pcmuio_write(dev, 0, asic, PCMUIO_PAGE_INT_ID, 0);
385
386
387 pcmuio_handle_intr_subdev(dev, s, val);
388
389 return 1;
390}
391
392static irqreturn_t pcmuio_interrupt(int irq, void *d)
393{
394 struct comedi_device *dev = d;
395 struct pcmuio_private *devpriv = dev->private;
396 int handled = 0;
397
398 if (irq == dev->irq)
399 handled += pcmuio_handle_asic_interrupt(dev, 0);
400 if (irq == devpriv->irq2)
401 handled += pcmuio_handle_asic_interrupt(dev, 1);
402
403 return handled ? IRQ_HANDLED : IRQ_NONE;
404}
405
406
407static int pcmuio_start_intr(struct comedi_device *dev,
408 struct comedi_subdevice *s)
409{
410 struct pcmuio_private *devpriv = dev->private;
411 int asic = pcmuio_subdevice_to_asic(s);
412 struct pcmuio_asic *chip = &devpriv->asics[asic];
413 struct comedi_cmd *cmd = &s->async->cmd;
414 unsigned int bits = 0;
415 unsigned int pol_bits = 0;
416 int i;
417
418 if (!chip->continuous && chip->stop_count == 0) {
419
420 s->async->events |= COMEDI_CB_EOA;
421 chip->active = 0;
422 return 1;
423 }
424
425 chip->enabled_mask = 0;
426 chip->active = 1;
427 if (cmd->chanlist) {
428 for (i = 0; i < cmd->chanlist_len; i++) {
429 unsigned int chanspec = cmd->chanlist[i];
430 unsigned int chan = CR_CHAN(chanspec);
431 unsigned int range = CR_RANGE(chanspec);
432 unsigned int aref = CR_AREF(chanspec);
433
434 bits |= (1 << chan);
435 pol_bits |= ((aref || range) ? 1 : 0) << chan;
436 }
437 }
438 bits &= ((1 << s->n_chan) - 1);
439 chip->enabled_mask = bits;
440
441
442 pcmuio_write(dev, pol_bits, asic, PCMUIO_PAGE_POL, 0);
443 pcmuio_write(dev, bits, asic, PCMUIO_PAGE_ENAB, 0);
444
445 return 0;
446}
447
448static int pcmuio_cancel(struct comedi_device *dev, struct comedi_subdevice *s)
449{
450 struct pcmuio_private *devpriv = dev->private;
451 int asic = pcmuio_subdevice_to_asic(s);
452 struct pcmuio_asic *chip = &devpriv->asics[asic];
453 unsigned long flags;
454
455 spin_lock_irqsave(&chip->spinlock, flags);
456 if (chip->active)
457 pcmuio_stop_intr(dev, s);
458 spin_unlock_irqrestore(&chip->spinlock, flags);
459
460 return 0;
461}
462
463
464
465
466static int
467pcmuio_inttrig_start_intr(struct comedi_device *dev, struct comedi_subdevice *s,
468 unsigned int trignum)
469{
470 struct pcmuio_private *devpriv = dev->private;
471 int asic = pcmuio_subdevice_to_asic(s);
472 struct pcmuio_asic *chip = &devpriv->asics[asic];
473 unsigned long flags;
474 int event = 0;
475
476 if (trignum != 0)
477 return -EINVAL;
478
479 spin_lock_irqsave(&chip->spinlock, flags);
480 s->async->inttrig = NULL;
481 if (chip->active)
482 event = pcmuio_start_intr(dev, s);
483
484 spin_unlock_irqrestore(&chip->spinlock, flags);
485
486 if (event)
487 comedi_event(dev, s);
488
489 return 1;
490}
491
492
493
494
495static int pcmuio_cmd(struct comedi_device *dev, struct comedi_subdevice *s)
496{
497 struct pcmuio_private *devpriv = dev->private;
498 struct comedi_cmd *cmd = &s->async->cmd;
499 int asic = pcmuio_subdevice_to_asic(s);
500 struct pcmuio_asic *chip = &devpriv->asics[asic];
501 unsigned long flags;
502 int event = 0;
503
504 spin_lock_irqsave(&chip->spinlock, flags);
505 chip->active = 1;
506
507
508 switch (cmd->stop_src) {
509 case TRIG_COUNT:
510 chip->continuous = 0;
511 chip->stop_count = cmd->stop_arg;
512 break;
513 default:
514
515 chip->continuous = 1;
516 chip->stop_count = 0;
517 break;
518 }
519
520
521 switch (cmd->start_src) {
522 case TRIG_INT:
523 s->async->inttrig = pcmuio_inttrig_start_intr;
524 break;
525 default:
526
527 event = pcmuio_start_intr(dev, s);
528 break;
529 }
530 spin_unlock_irqrestore(&chip->spinlock, flags);
531
532 if (event)
533 comedi_event(dev, s);
534
535 return 0;
536}
537
538static int pcmuio_cmdtest(struct comedi_device *dev,
539 struct comedi_subdevice *s,
540 struct comedi_cmd *cmd)
541{
542 int err = 0;
543
544
545
546 err |= cfc_check_trigger_src(&cmd->start_src, TRIG_NOW | TRIG_INT);
547 err |= cfc_check_trigger_src(&cmd->scan_begin_src, TRIG_EXT);
548 err |= cfc_check_trigger_src(&cmd->convert_src, TRIG_NOW);
549 err |= cfc_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
550 err |= cfc_check_trigger_src(&cmd->stop_src, TRIG_COUNT | TRIG_NONE);
551
552 if (err)
553 return 1;
554
555
556
557 err |= cfc_check_trigger_is_unique(cmd->start_src);
558 err |= cfc_check_trigger_is_unique(cmd->stop_src);
559
560
561
562 if (err)
563 return 2;
564
565
566
567 err |= cfc_check_trigger_arg_is(&cmd->start_arg, 0);
568 err |= cfc_check_trigger_arg_is(&cmd->scan_begin_arg, 0);
569 err |= cfc_check_trigger_arg_is(&cmd->convert_arg, 0);
570 err |= cfc_check_trigger_arg_is(&cmd->scan_end_arg, cmd->chanlist_len);
571
572 switch (cmd->stop_src) {
573 case TRIG_COUNT:
574
575 break;
576 case TRIG_NONE:
577 err |= cfc_check_trigger_arg_is(&cmd->stop_arg, 0);
578 break;
579 default:
580 break;
581 }
582
583 if (err)
584 return 3;
585
586
587
588
589
590 return 0;
591}
592
593static int pcmuio_attach(struct comedi_device *dev, struct comedi_devconfig *it)
594{
595 const struct pcmuio_board *board = comedi_board(dev);
596 struct comedi_subdevice *s;
597 struct pcmuio_private *devpriv;
598 int ret;
599 int i;
600
601 ret = comedi_request_region(dev, it->options[0],
602 board->num_asics * PCMUIO_ASIC_IOSIZE);
603 if (ret)
604 return ret;
605
606 devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
607 if (!devpriv)
608 return -ENOMEM;
609
610 for (i = 0; i < PCMUIO_MAX_ASICS; ++i) {
611 struct pcmuio_asic *chip = &devpriv->asics[i];
612
613 spin_lock_init(&chip->pagelock);
614 spin_lock_init(&chip->spinlock);
615 }
616
617 pcmuio_reset(dev);
618
619 if (it->options[1]) {
620
621 ret = request_irq(it->options[1], pcmuio_interrupt, 0,
622 dev->board_name, dev);
623 if (ret == 0)
624 dev->irq = it->options[1];
625 }
626
627 if (board->num_asics == 2) {
628 if (it->options[2] == dev->irq) {
629
630 devpriv->irq2 = it->options[2];
631 } else if (it->options[2]) {
632
633 ret = request_irq(it->options[2], pcmuio_interrupt, 0,
634 dev->board_name, dev);
635 if (ret == 0)
636 devpriv->irq2 = it->options[2];
637 }
638 }
639
640 ret = comedi_alloc_subdevices(dev, board->num_asics * 2);
641 if (ret)
642 return ret;
643
644 for (i = 0; i < dev->n_subdevices; ++i) {
645 s = &dev->subdevices[i];
646 s->type = COMEDI_SUBD_DIO;
647 s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
648 s->n_chan = 24;
649 s->maxdata = 1;
650 s->range_table = &range_digital;
651 s->insn_bits = pcmuio_dio_insn_bits;
652 s->insn_config = pcmuio_dio_insn_config;
653
654
655 if ((i == 0 && dev->irq) || (i == 2 && devpriv->irq2)) {
656
657 dev->read_subdev = s;
658 s->subdev_flags |= SDF_CMD_READ;
659 s->len_chanlist = s->n_chan;
660 s->cancel = pcmuio_cancel;
661 s->do_cmd = pcmuio_cmd;
662 s->do_cmdtest = pcmuio_cmdtest;
663 }
664 }
665
666 return 0;
667}
668
669static void pcmuio_detach(struct comedi_device *dev)
670{
671 struct pcmuio_private *devpriv = dev->private;
672
673 if (devpriv) {
674 pcmuio_reset(dev);
675
676
677 if (devpriv->irq2 && devpriv->irq2 != dev->irq)
678 free_irq(devpriv->irq2, dev);
679 }
680 comedi_legacy_detach(dev);
681}
682
683static struct comedi_driver pcmuio_driver = {
684 .driver_name = "pcmuio",
685 .module = THIS_MODULE,
686 .attach = pcmuio_attach,
687 .detach = pcmuio_detach,
688 .board_name = &pcmuio_boards[0].name,
689 .offset = sizeof(struct pcmuio_board),
690 .num_names = ARRAY_SIZE(pcmuio_boards),
691};
692module_comedi_driver(pcmuio_driver);
693
694MODULE_AUTHOR("Comedi http://www.comedi.org");
695MODULE_DESCRIPTION("Comedi low-level driver");
696MODULE_LICENSE("GPL");
697