Merge tag 'ntb-3.12' of git://github.com/jonmason/ntb
[cascardo/linux.git] / drivers / staging / comedi / drivers / dmm32at.c
1 /*
2     comedi/drivers/dmm32at.c
3     Diamond Systems mm32at code for a Comedi driver
4
5     COMEDI - Linux Control and Measurement Device Interface
6     Copyright (C) 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 Driver: dmm32at
20 Description: Diamond Systems mm32at driver.
21 Devices:
22 Author: Perry J. Piplani <perry.j.piplani@nasa.gov>
23 Updated: Fri Jun  4 09:13:24 CDT 2004
24 Status: experimental
25
26 This driver is for the Diamond Systems MM-32-AT board
27 http://www.diamondsystems.com/products/diamondmm32at It is being used
28 on serveral projects inside NASA, without problems so far. For analog
29 input commands, TRIG_EXT is not yet supported at all..
30
31 Configuration Options:
32   comedi_config /dev/comedi0 dmm32at baseaddr,irq
33 */
34
35 #include <linux/module.h>
36 #include <linux/delay.h>
37 #include <linux/interrupt.h>
38 #include "../comedidev.h"
39
40 #include "comedi_fc.h"
41
42 /* Board register addresses */
43
44 #define DMM32AT_MEMSIZE 0x10
45
46 #define DMM32AT_CONV 0x00
47 #define DMM32AT_AILSB 0x00
48 #define DMM32AT_AUXDOUT 0x01
49 #define DMM32AT_AIMSB 0x01
50 #define DMM32AT_AILOW 0x02
51 #define DMM32AT_AIHIGH 0x03
52
53 #define DMM32AT_DACLSB 0x04
54 #define DMM32AT_DACSTAT 0x04
55 #define DMM32AT_DACMSB 0x05
56
57 #define DMM32AT_FIFOCNTRL 0x07
58 #define DMM32AT_FIFOSTAT 0x07
59
60 #define DMM32AT_CNTRL 0x08
61 #define DMM32AT_AISTAT 0x08
62
63 #define DMM32AT_INTCLOCK 0x09
64
65 #define DMM32AT_CNTRDIO 0x0a
66
67 #define DMM32AT_AICONF 0x0b
68 #define DMM32AT_AIRBACK 0x0b
69
70 #define DMM32AT_CLK1 0x0d
71 #define DMM32AT_CLK2 0x0e
72 #define DMM32AT_CLKCT 0x0f
73
74 #define DMM32AT_DIOA 0x0c
75 #define DMM32AT_DIOB 0x0d
76 #define DMM32AT_DIOC 0x0e
77 #define DMM32AT_DIOCONF 0x0f
78
79 /* Board register values. */
80
81 /* DMM32AT_DACSTAT 0x04 */
82 #define DMM32AT_DACBUSY 0x80
83
84 /* DMM32AT_FIFOCNTRL 0x07 */
85 #define DMM32AT_FIFORESET 0x02
86 #define DMM32AT_SCANENABLE 0x04
87
88 /* DMM32AT_CNTRL 0x08 */
89 #define DMM32AT_RESET 0x20
90 #define DMM32AT_INTRESET 0x08
91 #define DMM32AT_CLKACC 0x00
92 #define DMM32AT_DIOACC 0x01
93
94 /* DMM32AT_AISTAT 0x08 */
95 #define DMM32AT_STATUS 0x80
96
97 /* DMM32AT_INTCLOCK 0x09 */
98 #define DMM32AT_ADINT 0x80
99 #define DMM32AT_CLKSEL 0x03
100
101 /* DMM32AT_CNTRDIO 0x0a */
102 #define DMM32AT_FREQ12 0x80
103
104 /* DMM32AT_AICONF 0x0b */
105 #define DMM32AT_RANGE_U10 0x0c
106 #define DMM32AT_RANGE_U5 0x0d
107 #define DMM32AT_RANGE_B10 0x08
108 #define DMM32AT_RANGE_B5 0x00
109 #define DMM32AT_SCINT_20 0x00
110 #define DMM32AT_SCINT_15 0x10
111 #define DMM32AT_SCINT_10 0x20
112 #define DMM32AT_SCINT_5 0x30
113
114 /* DMM32AT_CLKCT 0x0f */
115 #define DMM32AT_CLKCT1 0x56     /* mode3 counter 1 - write low byte only */
116 #define DMM32AT_CLKCT2 0xb6     /*  mode3 counter 2 - write high and low byte */
117
118 /* DMM32AT_DIOCONF 0x0f */
119 #define DMM32AT_DIENABLE 0x80
120 #define DMM32AT_DIRA 0x10
121 #define DMM32AT_DIRB 0x02
122 #define DMM32AT_DIRCL 0x01
123 #define DMM32AT_DIRCH 0x08
124
125 /* board AI ranges in comedi structure */
126 static const struct comedi_lrange dmm32at_airanges = {
127         4,
128         {
129          UNI_RANGE(10),
130          UNI_RANGE(5),
131          BIP_RANGE(10),
132          BIP_RANGE(5),
133          }
134 };
135
136 /* register values for above ranges */
137 static const unsigned char dmm32at_rangebits[] = {
138         DMM32AT_RANGE_U10,
139         DMM32AT_RANGE_U5,
140         DMM32AT_RANGE_B10,
141         DMM32AT_RANGE_B5,
142 };
143
144 /* only one of these ranges is valid, as set by a jumper on the
145  * board. The application should only use the range set by the jumper
146  */
147 static const struct comedi_lrange dmm32at_aoranges = {
148         4,
149         {
150          UNI_RANGE(10),
151          UNI_RANGE(5),
152          BIP_RANGE(10),
153          BIP_RANGE(5),
154          }
155 };
156
157 struct dmm32at_private {
158
159         int data;
160         int ai_inuse;
161         unsigned int ai_scans_left;
162
163         /* Used for AO readback */
164         unsigned int ao_readback[4];
165         unsigned char dio_config;
166
167 };
168
169 static int dmm32at_ai_rinsn(struct comedi_device *dev,
170                             struct comedi_subdevice *s,
171                             struct comedi_insn *insn, unsigned int *data)
172 {
173         int n, i;
174         unsigned int d;
175         unsigned char status;
176         unsigned short msb, lsb;
177         unsigned char chan;
178         int range;
179
180         /* get the channel and range number */
181
182         chan = CR_CHAN(insn->chanspec) & (s->n_chan - 1);
183         range = CR_RANGE(insn->chanspec);
184
185         /* printk("channel=0x%02x, range=%d\n",chan,range); */
186
187         /* zero scan and fifo control and reset fifo */
188         outb(DMM32AT_FIFORESET, dev->iobase + DMM32AT_FIFOCNTRL);
189
190         /* write the ai channel range regs */
191         outb(chan, dev->iobase + DMM32AT_AILOW);
192         outb(chan, dev->iobase + DMM32AT_AIHIGH);
193         /* set the range bits */
194         outb(dmm32at_rangebits[range], dev->iobase + DMM32AT_AICONF);
195
196         /* wait for circuit to settle */
197         for (i = 0; i < 40000; i++) {
198                 status = inb(dev->iobase + DMM32AT_AIRBACK);
199                 if ((status & DMM32AT_STATUS) == 0)
200                         break;
201         }
202         if (i == 40000) {
203                 printk(KERN_WARNING "dmm32at: timeout\n");
204                 return -ETIMEDOUT;
205         }
206
207         /* convert n samples */
208         for (n = 0; n < insn->n; n++) {
209                 /* trigger conversion */
210                 outb(0xff, dev->iobase + DMM32AT_CONV);
211                 /* wait for conversion to end */
212                 for (i = 0; i < 40000; i++) {
213                         status = inb(dev->iobase + DMM32AT_AISTAT);
214                         if ((status & DMM32AT_STATUS) == 0)
215                                 break;
216                 }
217                 if (i == 40000) {
218                         printk(KERN_WARNING "dmm32at: timeout\n");
219                         return -ETIMEDOUT;
220                 }
221
222                 /* read data */
223                 lsb = inb(dev->iobase + DMM32AT_AILSB);
224                 msb = inb(dev->iobase + DMM32AT_AIMSB);
225
226                 /* invert sign bit to make range unsigned, this is an
227                    idiosyncrasy of the diamond board, it return
228                    conversions as a signed value, i.e. -32768 to
229                    32767, flipping the bit and interpreting it as
230                    signed gives you a range of 0 to 65535 which is
231                    used by comedi */
232                 d = ((msb ^ 0x0080) << 8) + lsb;
233
234                 data[n] = d;
235         }
236
237         /* return the number of samples read/written */
238         return n;
239 }
240
241 static int dmm32at_ns_to_timer(unsigned int *ns, int round)
242 {
243         /* trivial timer */
244         return *ns;
245 }
246
247 static int dmm32at_ai_cmdtest(struct comedi_device *dev,
248                               struct comedi_subdevice *s,
249                               struct comedi_cmd *cmd)
250 {
251         int err = 0;
252         int tmp;
253         int start_chan, gain, i;
254
255         /* Step 1 : check if triggers are trivially valid */
256
257         err |= cfc_check_trigger_src(&cmd->start_src, TRIG_NOW);
258         err |= cfc_check_trigger_src(&cmd->scan_begin_src,
259                                         TRIG_TIMER /*| TRIG_EXT */);
260         err |= cfc_check_trigger_src(&cmd->convert_src,
261                                         TRIG_TIMER /*| TRIG_EXT */);
262         err |= cfc_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
263         err |= cfc_check_trigger_src(&cmd->stop_src, TRIG_COUNT | TRIG_NONE);
264
265         if (err)
266                 return 1;
267
268         /* Step 2a : make sure trigger sources are unique */
269
270         err |= cfc_check_trigger_is_unique(cmd->scan_begin_src);
271         err |= cfc_check_trigger_is_unique(cmd->convert_src);
272         err |= cfc_check_trigger_is_unique(cmd->stop_src);
273
274         /* Step 2b : and mutually compatible */
275
276         if (err)
277                 return 2;
278
279         /* Step 3: check if arguments are trivially valid */
280
281         err |= cfc_check_trigger_arg_is(&cmd->start_arg, 0);
282
283 #define MAX_SCAN_SPEED  1000000 /* in nanoseconds */
284 #define MIN_SCAN_SPEED  1000000000      /* in nanoseconds */
285
286         if (cmd->scan_begin_src == TRIG_TIMER) {
287                 err |= cfc_check_trigger_arg_min(&cmd->scan_begin_arg,
288                                                  MAX_SCAN_SPEED);
289                 err |= cfc_check_trigger_arg_max(&cmd->scan_begin_arg,
290                                                  MIN_SCAN_SPEED);
291         } else {
292                 /* external trigger */
293                 /* should be level/edge, hi/lo specification here */
294                 /* should specify multiple external triggers */
295                 err |= cfc_check_trigger_arg_max(&cmd->scan_begin_arg, 9);
296         }
297
298         if (cmd->convert_src == TRIG_TIMER) {
299                 if (cmd->convert_arg >= 17500)
300                         cmd->convert_arg = 20000;
301                 else if (cmd->convert_arg >= 12500)
302                         cmd->convert_arg = 15000;
303                 else if (cmd->convert_arg >= 7500)
304                         cmd->convert_arg = 10000;
305                 else
306                         cmd->convert_arg = 5000;
307         } else {
308                 /* external trigger */
309                 /* see above */
310                 err |= cfc_check_trigger_arg_max(&cmd->convert_arg, 9);
311         }
312
313         err |= cfc_check_trigger_arg_is(&cmd->scan_end_arg, cmd->chanlist_len);
314
315         if (cmd->stop_src == TRIG_COUNT) {
316                 err |= cfc_check_trigger_arg_max(&cmd->stop_arg, 0xfffffff0);
317                 err |= cfc_check_trigger_arg_min(&cmd->stop_arg, 1);
318         } else {
319                 /* TRIG_NONE */
320                 err |= cfc_check_trigger_arg_is(&cmd->stop_arg, 0);
321         }
322
323         if (err)
324                 return 3;
325
326         /* step 4: fix up any arguments */
327
328         if (cmd->scan_begin_src == TRIG_TIMER) {
329                 tmp = cmd->scan_begin_arg;
330                 dmm32at_ns_to_timer(&cmd->scan_begin_arg,
331                                     cmd->flags & TRIG_ROUND_MASK);
332                 if (tmp != cmd->scan_begin_arg)
333                         err++;
334         }
335         if (cmd->convert_src == TRIG_TIMER) {
336                 tmp = cmd->convert_arg;
337                 dmm32at_ns_to_timer(&cmd->convert_arg,
338                                     cmd->flags & TRIG_ROUND_MASK);
339                 if (tmp != cmd->convert_arg)
340                         err++;
341                 if (cmd->scan_begin_src == TRIG_TIMER &&
342                     cmd->scan_begin_arg <
343                     cmd->convert_arg * cmd->scan_end_arg) {
344                         cmd->scan_begin_arg =
345                             cmd->convert_arg * cmd->scan_end_arg;
346                         err++;
347                 }
348         }
349
350         if (err)
351                 return 4;
352
353         /* step 5 check the channel list, the channel list for this
354            board must be consecutive and gains must be the same */
355
356         if (cmd->chanlist) {
357                 gain = CR_RANGE(cmd->chanlist[0]);
358                 start_chan = CR_CHAN(cmd->chanlist[0]);
359                 for (i = 1; i < cmd->chanlist_len; i++) {
360                         if (CR_CHAN(cmd->chanlist[i]) !=
361                             (start_chan + i) % s->n_chan) {
362                                 comedi_error(dev,
363                                              "entries in chanlist must be consecutive channels, counting upwards\n");
364                                 err++;
365                         }
366                         if (CR_RANGE(cmd->chanlist[i]) != gain) {
367                                 comedi_error(dev,
368                                              "entries in chanlist must all have the same gain\n");
369                                 err++;
370                         }
371                 }
372         }
373
374         if (err)
375                 return 5;
376
377         return 0;
378 }
379
380 static void dmm32at_setaitimer(struct comedi_device *dev, unsigned int nansec)
381 {
382         unsigned char lo1, lo2, hi2;
383         unsigned short both2;
384
385         /* based on 10mhz clock */
386         lo1 = 200;
387         both2 = nansec / 20000;
388         hi2 = (both2 & 0xff00) >> 8;
389         lo2 = both2 & 0x00ff;
390
391         /* set the counter frequency to 10mhz */
392         outb(0, dev->iobase + DMM32AT_CNTRDIO);
393
394         /* get access to the clock regs */
395         outb(DMM32AT_CLKACC, dev->iobase + DMM32AT_CNTRL);
396
397         /* write the counter 1 control word and low byte to counter */
398         outb(DMM32AT_CLKCT1, dev->iobase + DMM32AT_CLKCT);
399         outb(lo1, dev->iobase + DMM32AT_CLK1);
400
401         /* write the counter 2 control word and low byte then to counter */
402         outb(DMM32AT_CLKCT2, dev->iobase + DMM32AT_CLKCT);
403         outb(lo2, dev->iobase + DMM32AT_CLK2);
404         outb(hi2, dev->iobase + DMM32AT_CLK2);
405
406         /* enable the ai conversion interrupt and the clock to start scans */
407         outb(DMM32AT_ADINT | DMM32AT_CLKSEL, dev->iobase + DMM32AT_INTCLOCK);
408 }
409
410 static int dmm32at_ai_cmd(struct comedi_device *dev, struct comedi_subdevice *s)
411 {
412         struct dmm32at_private *devpriv = dev->private;
413         struct comedi_cmd *cmd = &s->async->cmd;
414         int i, range;
415         unsigned char chanlo, chanhi, status;
416
417         if (!cmd->chanlist)
418                 return -EINVAL;
419
420         /* get the channel list and range */
421         chanlo = CR_CHAN(cmd->chanlist[0]) & (s->n_chan - 1);
422         chanhi = chanlo + cmd->chanlist_len - 1;
423         if (chanhi >= s->n_chan)
424                 return -EINVAL;
425         range = CR_RANGE(cmd->chanlist[0]);
426
427         /* reset fifo */
428         outb(DMM32AT_FIFORESET, dev->iobase + DMM32AT_FIFOCNTRL);
429
430         /* set scan enable */
431         outb(DMM32AT_SCANENABLE, dev->iobase + DMM32AT_FIFOCNTRL);
432
433         /* write the ai channel range regs */
434         outb(chanlo, dev->iobase + DMM32AT_AILOW);
435         outb(chanhi, dev->iobase + DMM32AT_AIHIGH);
436
437         /* set the range bits */
438         outb(dmm32at_rangebits[range], dev->iobase + DMM32AT_AICONF);
439
440         /* reset the interrupt just in case */
441         outb(DMM32AT_INTRESET, dev->iobase + DMM32AT_CNTRL);
442
443         if (cmd->stop_src == TRIG_COUNT)
444                 devpriv->ai_scans_left = cmd->stop_arg;
445         else {                  /* TRIG_NONE */
446                 devpriv->ai_scans_left = 0xffffffff; /* indicates TRIG_NONE to
447                                                       * isr */
448         }
449
450         /* wait for circuit to settle */
451         for (i = 0; i < 40000; i++) {
452                 status = inb(dev->iobase + DMM32AT_AIRBACK);
453                 if ((status & DMM32AT_STATUS) == 0)
454                         break;
455         }
456         if (i == 40000) {
457                 printk(KERN_WARNING "dmm32at: timeout\n");
458                 return -ETIMEDOUT;
459         }
460
461         if (devpriv->ai_scans_left > 1) {
462                 /* start the clock and enable the interrupts */
463                 dmm32at_setaitimer(dev, cmd->scan_begin_arg);
464         } else {
465                 /* start the interrups and initiate a single scan */
466                 outb(DMM32AT_ADINT, dev->iobase + DMM32AT_INTCLOCK);
467                 outb(0xff, dev->iobase + DMM32AT_CONV);
468         }
469
470 /*      printk("dmmat32 in command\n"); */
471
472 /*      for(i=0;i<cmd->chanlist_len;i++) */
473 /*              comedi_buf_put(s->async,i*100); */
474
475 /*      s->async->events |= COMEDI_CB_EOA; */
476 /*      comedi_event(dev, s); */
477
478         return 0;
479
480 }
481
482 static int dmm32at_ai_cancel(struct comedi_device *dev,
483                              struct comedi_subdevice *s)
484 {
485         struct dmm32at_private *devpriv = dev->private;
486
487         devpriv->ai_scans_left = 1;
488         return 0;
489 }
490
491 static irqreturn_t dmm32at_isr(int irq, void *d)
492 {
493         struct comedi_device *dev = d;
494         struct dmm32at_private *devpriv = dev->private;
495         unsigned char intstat;
496         unsigned int samp;
497         unsigned short msb, lsb;
498         int i;
499
500         if (!dev->attached) {
501                 comedi_error(dev, "spurious interrupt");
502                 return IRQ_HANDLED;
503         }
504
505         intstat = inb(dev->iobase + DMM32AT_INTCLOCK);
506
507         if (intstat & DMM32AT_ADINT) {
508                 struct comedi_subdevice *s = dev->read_subdev;
509                 struct comedi_cmd *cmd = &s->async->cmd;
510
511                 for (i = 0; i < cmd->chanlist_len; i++) {
512                         /* read data */
513                         lsb = inb(dev->iobase + DMM32AT_AILSB);
514                         msb = inb(dev->iobase + DMM32AT_AIMSB);
515
516                         /* invert sign bit to make range unsigned */
517                         samp = ((msb ^ 0x0080) << 8) + lsb;
518                         comedi_buf_put(s->async, samp);
519                 }
520
521                 if (devpriv->ai_scans_left != 0xffffffff) {     /* TRIG_COUNT */
522                         devpriv->ai_scans_left--;
523                         if (devpriv->ai_scans_left == 0) {
524                                 /* disable further interrupts and clocks */
525                                 outb(0x0, dev->iobase + DMM32AT_INTCLOCK);
526                                 /* set the buffer to be flushed with an EOF */
527                                 s->async->events |= COMEDI_CB_EOA;
528                         }
529
530                 }
531                 /* flush the buffer */
532                 comedi_event(dev, s);
533         }
534
535         /* reset the interrupt */
536         outb(DMM32AT_INTRESET, dev->iobase + DMM32AT_CNTRL);
537         return IRQ_HANDLED;
538 }
539
540 static int dmm32at_ao_winsn(struct comedi_device *dev,
541                             struct comedi_subdevice *s,
542                             struct comedi_insn *insn, unsigned int *data)
543 {
544         struct dmm32at_private *devpriv = dev->private;
545         int i;
546         int chan = CR_CHAN(insn->chanspec);
547         unsigned char hi, lo, status;
548
549         /* Writing a list of values to an AO channel is probably not
550          * very useful, but that's how the interface is defined. */
551         for (i = 0; i < insn->n; i++) {
552
553                 devpriv->ao_readback[chan] = data[i];
554
555                 /* get the low byte */
556                 lo = data[i] & 0x00ff;
557                 /* high byte also contains channel number */
558                 hi = (data[i] >> 8) + chan * (1 << 6);
559                 /* printk("writing 0x%02x  0x%02x\n",hi,lo); */
560                 /* write the low and high values to the board */
561                 outb(lo, dev->iobase + DMM32AT_DACLSB);
562                 outb(hi, dev->iobase + DMM32AT_DACMSB);
563
564                 /* wait for circuit to settle */
565                 for (i = 0; i < 40000; i++) {
566                         status = inb(dev->iobase + DMM32AT_DACSTAT);
567                         if ((status & DMM32AT_DACBUSY) == 0)
568                                 break;
569                 }
570                 if (i == 40000) {
571                         printk(KERN_WARNING "dmm32at: timeout\n");
572                         return -ETIMEDOUT;
573                 }
574                 /* dummy read to update trigger the output */
575                 status = inb(dev->iobase + DMM32AT_DACMSB);
576
577         }
578
579         /* return the number of samples read/written */
580         return i;
581 }
582
583 static int dmm32at_ao_rinsn(struct comedi_device *dev,
584                             struct comedi_subdevice *s,
585                             struct comedi_insn *insn, unsigned int *data)
586 {
587         struct dmm32at_private *devpriv = dev->private;
588         int i;
589         int chan = CR_CHAN(insn->chanspec);
590
591         for (i = 0; i < insn->n; i++)
592                 data[i] = devpriv->ao_readback[chan];
593
594         return i;
595 }
596
597 static int dmm32at_dio_insn_bits(struct comedi_device *dev,
598                                  struct comedi_subdevice *s,
599                                  struct comedi_insn *insn, unsigned int *data)
600 {
601         struct dmm32at_private *devpriv = dev->private;
602         unsigned char diobits;
603
604         /* The insn data is a mask in data[0] and the new data
605          * in data[1], each channel cooresponding to a bit. */
606         if (data[0]) {
607                 s->state &= ~data[0];
608                 s->state |= data[0] & data[1];
609                 /* Write out the new digital output lines */
610                 /* outw(s->state,dev->iobase + DMM32AT_DIO); */
611         }
612
613         /* get access to the DIO regs */
614         outb(DMM32AT_DIOACC, dev->iobase + DMM32AT_CNTRL);
615
616         /* if either part of dio is set for output */
617         if (((devpriv->dio_config & DMM32AT_DIRCL) == 0) ||
618             ((devpriv->dio_config & DMM32AT_DIRCH) == 0)) {
619                 diobits = (s->state & 0x00ff0000) >> 16;
620                 outb(diobits, dev->iobase + DMM32AT_DIOC);
621         }
622         if ((devpriv->dio_config & DMM32AT_DIRB) == 0) {
623                 diobits = (s->state & 0x0000ff00) >> 8;
624                 outb(diobits, dev->iobase + DMM32AT_DIOB);
625         }
626         if ((devpriv->dio_config & DMM32AT_DIRA) == 0) {
627                 diobits = (s->state & 0x000000ff);
628                 outb(diobits, dev->iobase + DMM32AT_DIOA);
629         }
630
631         /* now read the state back in */
632         s->state = inb(dev->iobase + DMM32AT_DIOC);
633         s->state <<= 8;
634         s->state |= inb(dev->iobase + DMM32AT_DIOB);
635         s->state <<= 8;
636         s->state |= inb(dev->iobase + DMM32AT_DIOA);
637         data[1] = s->state;
638
639         /* on return, data[1] contains the value of the digital
640          * input and output lines. */
641         /* data[1]=inw(dev->iobase + DMM32AT_DIO); */
642         /* or we could just return the software copy of the output values if
643          * it was a purely digital output subdevice */
644         /* data[1]=s->state; */
645
646         return insn->n;
647 }
648
649 static int dmm32at_dio_insn_config(struct comedi_device *dev,
650                                    struct comedi_subdevice *s,
651                                    struct comedi_insn *insn,
652                                    unsigned int *data)
653 {
654         struct dmm32at_private *devpriv = dev->private;
655         unsigned int chan = CR_CHAN(insn->chanspec);
656         unsigned int mask;
657         unsigned char chanbit;
658         int ret;
659
660         if (chan < 8) {
661                 mask = 0x0000ff;
662                 chanbit = DMM32AT_DIRA;
663         } else if (chan < 16) {
664                 mask = 0x00ff00;
665                 chanbit = DMM32AT_DIRB;
666         } else if (chan < 20) {
667                 mask = 0x0f0000;
668                 chanbit = DMM32AT_DIRCL;
669         } else {
670                 mask = 0xf00000;
671                 chanbit = DMM32AT_DIRCH;
672         }
673
674         ret = comedi_dio_insn_config(dev, s, insn, data, mask);
675         if (ret)
676                 return ret;
677
678         if (data[0] == INSN_CONFIG_DIO_OUTPUT)
679                 devpriv->dio_config &= ~chanbit;
680         else
681                 devpriv->dio_config |= chanbit;
682         /* get access to the DIO regs */
683         outb(DMM32AT_DIOACC, dev->iobase + DMM32AT_CNTRL);
684         /* set the DIO's to the new configuration setting */
685         outb(devpriv->dio_config, dev->iobase + DMM32AT_DIOCONF);
686
687         return insn->n;
688 }
689
690 static int dmm32at_attach(struct comedi_device *dev,
691                           struct comedi_devconfig *it)
692 {
693         struct dmm32at_private *devpriv;
694         int ret;
695         struct comedi_subdevice *s;
696         unsigned char aihi, ailo, fifostat, aistat, intstat, airback;
697         unsigned int irq;
698
699         irq = it->options[1];
700
701         ret = comedi_request_region(dev, it->options[0], DMM32AT_MEMSIZE);
702         if (ret)
703                 return ret;
704
705         /* the following just makes sure the board is there and gets
706            it to a known state */
707
708         /* reset the board */
709         outb(DMM32AT_RESET, dev->iobase + DMM32AT_CNTRL);
710
711         /* allow a millisecond to reset */
712         udelay(1000);
713
714         /* zero scan and fifo control */
715         outb(0x0, dev->iobase + DMM32AT_FIFOCNTRL);
716
717         /* zero interrupt and clock control */
718         outb(0x0, dev->iobase + DMM32AT_INTCLOCK);
719
720         /* write a test channel range, the high 3 bits should drop */
721         outb(0x80, dev->iobase + DMM32AT_AILOW);
722         outb(0xff, dev->iobase + DMM32AT_AIHIGH);
723
724         /* set the range at 10v unipolar */
725         outb(DMM32AT_RANGE_U10, dev->iobase + DMM32AT_AICONF);
726
727         /* should take 10 us to settle, here's a hundred */
728         udelay(100);
729
730         /* read back the values */
731         ailo = inb(dev->iobase + DMM32AT_AILOW);
732         aihi = inb(dev->iobase + DMM32AT_AIHIGH);
733         fifostat = inb(dev->iobase + DMM32AT_FIFOSTAT);
734         aistat = inb(dev->iobase + DMM32AT_AISTAT);
735         intstat = inb(dev->iobase + DMM32AT_INTCLOCK);
736         airback = inb(dev->iobase + DMM32AT_AIRBACK);
737
738         printk(KERN_DEBUG "dmm32at: lo=0x%02x hi=0x%02x fifostat=0x%02x\n",
739                ailo, aihi, fifostat);
740         printk(KERN_DEBUG
741                "dmm32at: aistat=0x%02x intstat=0x%02x airback=0x%02x\n",
742                aistat, intstat, airback);
743
744         if ((ailo != 0x00) || (aihi != 0x1f) || (fifostat != 0x80) ||
745             (aistat != 0x60 || (intstat != 0x00) || airback != 0x0c)) {
746                 printk(KERN_ERR "dmmat32: board detection failed\n");
747                 return -EIO;
748         }
749
750         /* board is there, register interrupt */
751         if (irq) {
752                 ret = request_irq(irq, dmm32at_isr, 0, dev->board_name, dev);
753                 if (ret < 0) {
754                         printk(KERN_ERR "dmm32at: irq conflict\n");
755                         return ret;
756                 }
757                 dev->irq = irq;
758         }
759
760         devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
761         if (!devpriv)
762                 return -ENOMEM;
763
764         ret = comedi_alloc_subdevices(dev, 3);
765         if (ret)
766                 return ret;
767
768         s = &dev->subdevices[0];
769         dev->read_subdev = s;
770         /* analog input subdevice */
771         s->type = COMEDI_SUBD_AI;
772         /* we support single-ended (ground) and differential */
773         s->subdev_flags = SDF_READABLE | SDF_GROUND | SDF_DIFF | SDF_CMD_READ;
774         s->n_chan = 32;
775         s->maxdata = 0xffff;
776         s->range_table = &dmm32at_airanges;
777         s->len_chanlist = 32;   /* This is the maximum chanlist length that
778                                    the board can handle */
779         s->insn_read = dmm32at_ai_rinsn;
780         s->do_cmd = dmm32at_ai_cmd;
781         s->do_cmdtest = dmm32at_ai_cmdtest;
782         s->cancel = dmm32at_ai_cancel;
783
784         s = &dev->subdevices[1];
785         /* analog output subdevice */
786         s->type = COMEDI_SUBD_AO;
787         s->subdev_flags = SDF_WRITABLE;
788         s->n_chan = 4;
789         s->maxdata = 0x0fff;
790         s->range_table = &dmm32at_aoranges;
791         s->insn_write = dmm32at_ao_winsn;
792         s->insn_read = dmm32at_ao_rinsn;
793
794         s = &dev->subdevices[2];
795         /* digital i/o subdevice */
796
797         /* get access to the DIO regs */
798         outb(DMM32AT_DIOACC, dev->iobase + DMM32AT_CNTRL);
799         /* set the DIO's to the defualt input setting */
800         devpriv->dio_config = DMM32AT_DIRA | DMM32AT_DIRB |
801                 DMM32AT_DIRCL | DMM32AT_DIRCH | DMM32AT_DIENABLE;
802         outb(devpriv->dio_config, dev->iobase + DMM32AT_DIOCONF);
803
804         /* set up the subdevice */
805         s->type = COMEDI_SUBD_DIO;
806         s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
807         s->n_chan = 24;
808         s->maxdata = 1;
809         s->state = 0;
810         s->range_table = &range_digital;
811         s->insn_bits = dmm32at_dio_insn_bits;
812         s->insn_config = dmm32at_dio_insn_config;
813
814         /* success */
815         printk(KERN_INFO "comedi%d: dmm32at: attached\n", dev->minor);
816
817         return 1;
818
819 }
820
821 static struct comedi_driver dmm32at_driver = {
822         .driver_name    = "dmm32at",
823         .module         = THIS_MODULE,
824         .attach         = dmm32at_attach,
825         .detach         = comedi_legacy_detach,
826 };
827 module_comedi_driver(dmm32at_driver);
828
829 MODULE_AUTHOR("Comedi http://www.comedi.org");
830 MODULE_DESCRIPTION("Comedi low-level driver");
831 MODULE_LICENSE("GPL");