GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / drivers / staging / comedi / drivers / das08.c
blob9462ceb802ee7fa87d70b3b3e56b505d681f98de
1 /*
2 * comedi/drivers/das08.c
3 * DAS08 driver
5 * COMEDI - Linux Control and Measurement Device Interface
6 * Copyright (C) 2000 David A. Schleef <ds@schleef.org>
7 * Copyright (C) 2001,2002,2003 Frank Mori Hess <fmhess@users.sourceforge.net>
8 * Copyright (C) 2004 Salvador E. Tropea <set@users.sf.net> <set@ieee.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 *****************************************************************
28 * Driver: das08
29 * Description: DAS-08 compatible boards
30 * Author: Warren Jasper, ds, Frank Hess
31 * Devices: [Keithley Metrabyte] DAS08 (isa-das08),
32 * [ComputerBoards] DAS08 (isa-das08), DAS08-PGM (das08-pgm),
33 * DAS08-PGH (das08-pgh), DAS08-PGL (das08-pgl), DAS08-AOH (das08-aoh),
34 * DAS08-AOL (das08-aol), DAS08-AOM (das08-aom), DAS08/JR-AO (das08/jr-ao),
35 * DAS08/JR-16-AO (das08jr-16-ao), PCI-DAS08 (das08),
36 * PC104-DAS08 (pc104-das08), DAS08/JR/16 (das08jr/16)
37 * Status: works
39 * This is a rewrite of the das08 and das08jr drivers.
41 * Options (for ISA cards):
42 * [0] - base io address
44 * Options (for pci-das08):
45 * [0] - bus (optional)
46 * [1] = slot (optional)
48 * The das08 driver doesn't support asynchronous commands, since
49 * the cheap das08 hardware doesn't really support them. The
50 * comedi_rt_timer driver can be used to emulate commands for this
51 * driver.
54 #include "../comedidev.h"
56 #include <linux/delay.h>
58 #include "comedi_pci.h"
59 #include "8255.h"
60 #include "das08.h"
62 #define DRV_NAME "das08"
64 #define PCI_VENDOR_ID_COMPUTERBOARDS 0x1307
65 #define PCI_DEVICE_ID_PCIDAS08 0x29
66 #define PCIDAS08_SIZE 0x54
68 /* pci configuration registers */
69 #define INTCSR 0x4c
70 #define INTR1_ENABLE 0x1
71 #define INTR1_HIGH_POLARITY 0x2
72 #define PCI_INTR_ENABLE 0x40
73 #define INTR1_EDGE_TRIG 0x100 /* requires high polarity */
74 #define CNTRL 0x50
75 #define CNTRL_DIR 0x2
76 #define CNTRL_INTR 0x4
79 cio-das08.pdf
81 "isa-das08"
83 0 a/d bits 0-3 start 8 bit
84 1 a/d bits 4-11 start 12 bit
85 2 eoc, ip1-3, irq, mux op1-4, inte, mux
86 3 unused unused
87 4567 8254
88 89ab 8255
90 requires hard-wiring for async ai
94 #define DAS08_LSB 0
95 #define DAS08_MSB 1
96 #define DAS08_TRIG_12BIT 1
97 #define DAS08_STATUS 2
98 #define DAS08_EOC (1<<7)
99 #define DAS08_IRQ (1<<3)
100 #define DAS08_IP(x) (((x)>>4)&0x7)
101 #define DAS08_CONTROL 2
102 #define DAS08_MUX_MASK 0x7
103 #define DAS08_MUX(x) ((x) & DAS08_MUX_MASK)
104 #define DAS08_INTE (1<<3)
105 #define DAS08_DO_MASK 0xf0
106 #define DAS08_OP(x) (((x) << 4) & DAS08_DO_MASK)
109 cio-das08jr.pdf
111 "das08/jr-ao"
113 0 a/d bits 0-3 unused
114 1 a/d bits 4-11 start 12 bit
115 2 eoc, mux mux
116 3 di do
117 4 unused ao0_lsb
118 5 unused ao0_msb
119 6 unused ao1_lsb
120 7 unused ao1_msb
124 #define DAS08JR_DIO 3
125 #define DAS08JR_AO_LSB(x) ((x) ? 6 : 4)
126 #define DAS08JR_AO_MSB(x) ((x) ? 7 : 5)
129 cio-das08_aox.pdf
131 "das08-aoh"
132 "das08-aol"
133 "das08-aom"
135 0 a/d bits 0-3 start 8 bit
136 1 a/d bits 4-11 start 12 bit
137 2 eoc, ip1-3, irq, mux op1-4, inte, mux
138 3 mux, gain status gain control
139 4567 8254
140 8 unused ao0_lsb
141 9 unused ao0_msb
142 a unused ao1_lsb
143 b unused ao1_msb
144 89ab
145 cdef 8255
148 #define DAS08AO_GAIN_CONTROL 3
149 #define DAS08AO_GAIN_STATUS 3
151 #define DAS08AO_AO_LSB(x) ((x) ? 0xa : 8)
152 #define DAS08AO_AO_MSB(x) ((x) ? 0xb : 9)
153 #define DAS08AO_AO_UPDATE 8
155 /* gainlist same as _pgx_ below */
157 static int das08_ai_rinsn(struct comedi_device *dev, struct comedi_subdevice *s,
158 struct comedi_insn *insn, unsigned int *data);
159 static int das08_di_rbits(struct comedi_device *dev, struct comedi_subdevice *s,
160 struct comedi_insn *insn, unsigned int *data);
161 static int das08_do_wbits(struct comedi_device *dev, struct comedi_subdevice *s,
162 struct comedi_insn *insn, unsigned int *data);
163 static int das08jr_di_rbits(struct comedi_device *dev,
164 struct comedi_subdevice *s,
165 struct comedi_insn *insn, unsigned int *data);
166 static int das08jr_do_wbits(struct comedi_device *dev,
167 struct comedi_subdevice *s,
168 struct comedi_insn *insn, unsigned int *data);
169 static int das08jr_ao_winsn(struct comedi_device *dev,
170 struct comedi_subdevice *s,
171 struct comedi_insn *insn, unsigned int *data);
172 static int das08ao_ao_winsn(struct comedi_device *dev,
173 struct comedi_subdevice *s,
174 struct comedi_insn *insn, unsigned int *data);
175 static void i8254_set_mode_low(unsigned int base, int channel,
176 unsigned int mode);
178 static const struct comedi_lrange range_das08_pgl = { 9, {
179 BIP_RANGE(10),
180 BIP_RANGE(5),
181 BIP_RANGE(2.5),
182 BIP_RANGE(1.25),
183 BIP_RANGE(0.625),
184 UNI_RANGE(10),
185 UNI_RANGE(5),
186 UNI_RANGE(2.5),
187 UNI_RANGE(1.25)
191 static const struct comedi_lrange range_das08_pgh = { 12, {
192 BIP_RANGE(10),
193 BIP_RANGE(5),
194 BIP_RANGE(1),
195 BIP_RANGE(0.5),
196 BIP_RANGE(0.1),
197 BIP_RANGE(0.05),
198 BIP_RANGE(0.01),
199 BIP_RANGE(0.005),
200 UNI_RANGE(10),
201 UNI_RANGE(1),
202 UNI_RANGE(0.1),
203 UNI_RANGE(0.01),
207 static const struct comedi_lrange range_das08_pgm = { 9, {
208 BIP_RANGE(10),
209 BIP_RANGE(5),
210 BIP_RANGE(0.5),
211 BIP_RANGE(0.05),
212 BIP_RANGE(0.01),
213 UNI_RANGE(10),
214 UNI_RANGE(1),
215 UNI_RANGE(0.1),
216 UNI_RANGE(0.01)
218 }; /*
219 cio-das08jr.pdf
221 "das08/jr-ao"
223 0 a/d bits 0-3 unused
224 1 a/d bits 4-11 start 12 bit
225 2 eoc, mux mux
226 3 di do
227 4 unused ao0_lsb
228 5 unused ao0_msb
229 6 unused ao1_lsb
230 7 unused ao1_msb
234 static const struct comedi_lrange *const das08_ai_lranges[] = {
235 &range_unknown,
236 &range_bipolar5,
237 &range_das08_pgh,
238 &range_das08_pgl,
239 &range_das08_pgm,
242 static const int das08_pgh_gainlist[] = {
243 8, 0, 10, 2, 12, 4, 14, 6, 1, 3, 5, 7
245 static const int das08_pgl_gainlist[] = { 8, 0, 2, 4, 6, 1, 3, 5, 7 };
246 static const int das08_pgm_gainlist[] = { 8, 0, 10, 12, 14, 9, 11, 13, 15 };
248 static const int *const das08_gainlists[] = {
249 NULL,
250 NULL,
251 das08_pgh_gainlist,
252 das08_pgl_gainlist,
253 das08_pgm_gainlist,
256 static const struct das08_board_struct das08_boards[] = {
258 .name = "isa-das08", /* cio-das08.pdf */
259 .bustype = isa,
260 .ai = das08_ai_rinsn,
261 .ai_nbits = 12,
262 .ai_pg = das08_pg_none,
263 .ai_encoding = das08_encode12,
264 .ao = NULL,
265 .ao_nbits = 12,
266 .di = das08_di_rbits,
267 .do_ = das08_do_wbits,
268 .do_nchan = 4,
269 .i8255_offset = 8,
270 .i8254_offset = 4,
271 .iosize = 16, /* unchecked */
274 .name = "das08-pgm", /* cio-das08pgx.pdf */
275 .bustype = isa,
276 .ai = das08_ai_rinsn,
277 .ai_nbits = 12,
278 .ai_pg = das08_pgm,
279 .ai_encoding = das08_encode12,
280 .ao = NULL,
281 .di = das08_di_rbits,
282 .do_ = das08_do_wbits,
283 .do_nchan = 4,
284 .i8255_offset = 0,
285 .i8254_offset = 0x04,
286 .iosize = 16, /* unchecked */
289 .name = "das08-pgh", /* cio-das08pgx.pdf */
290 .bustype = isa,
291 .ai = das08_ai_rinsn,
292 .ai_nbits = 12,
293 .ai_pg = das08_pgh,
294 .ai_encoding = das08_encode12,
295 .ao = NULL,
296 .di = das08_di_rbits,
297 .do_ = das08_do_wbits,
298 .do_nchan = 4,
299 .i8255_offset = 0,
300 .i8254_offset = 0x04,
301 .iosize = 16, /* unchecked */
304 .name = "das08-pgl", /* cio-das08pgx.pdf */
305 .bustype = isa,
306 .ai = das08_ai_rinsn,
307 .ai_nbits = 12,
308 .ai_pg = das08_pgl,
309 .ai_encoding = das08_encode12,
310 .ao = NULL,
311 .di = das08_di_rbits,
312 .do_ = das08_do_wbits,
313 .do_nchan = 4,
314 .i8255_offset = 0,
315 .i8254_offset = 0x04,
316 .iosize = 16, /* unchecked */
319 .name = "das08-aoh", /* cio-das08_aox.pdf */
320 .bustype = isa,
321 .ai = das08_ai_rinsn,
322 .ai_nbits = 12,
323 .ai_pg = das08_pgh,
324 .ai_encoding = das08_encode12,
325 .ao = das08ao_ao_winsn, /* 8 */
326 .ao_nbits = 12,
327 .di = das08_di_rbits,
328 .do_ = das08_do_wbits,
329 .do_nchan = 4,
330 .i8255_offset = 0x0c,
331 .i8254_offset = 0x04,
332 .iosize = 16, /* unchecked */
335 .name = "das08-aol", /* cio-das08_aox.pdf */
336 .bustype = isa,
337 .ai = das08_ai_rinsn,
338 .ai_nbits = 12,
339 .ai_pg = das08_pgl,
340 .ai_encoding = das08_encode12,
341 .ao = das08ao_ao_winsn, /* 8 */
342 .ao_nbits = 12,
343 .di = das08_di_rbits,
344 .do_ = das08_do_wbits,
345 .do_nchan = 4,
346 .i8255_offset = 0x0c,
347 .i8254_offset = 0x04,
348 .iosize = 16, /* unchecked */
351 .name = "das08-aom", /* cio-das08_aox.pdf */
352 .bustype = isa,
353 .ai = das08_ai_rinsn,
354 .ai_nbits = 12,
355 .ai_pg = das08_pgm,
356 .ai_encoding = das08_encode12,
357 .ao = das08ao_ao_winsn, /* 8 */
358 .ao_nbits = 12,
359 .di = das08_di_rbits,
360 .do_ = das08_do_wbits,
361 .do_nchan = 4,
362 .i8255_offset = 0x0c,
363 .i8254_offset = 0x04,
364 .iosize = 16, /* unchecked */
367 .name = "das08/jr-ao", /* cio-das08-jr-ao.pdf */
368 .bustype = isa,
369 .ai = das08_ai_rinsn,
370 .ai_nbits = 12,
371 .ai_pg = das08_pg_none,
372 .ai_encoding = das08_encode12,
373 .ao = das08jr_ao_winsn,
374 .ao_nbits = 12,
375 .di = das08jr_di_rbits,
376 .do_ = das08jr_do_wbits,
377 .do_nchan = 8,
378 .i8255_offset = 0,
379 .i8254_offset = 0,
380 .iosize = 16, /* unchecked */
383 .name = "das08jr-16-ao", /* cio-das08jr-16-ao.pdf */
384 .bustype = isa,
385 .ai = das08_ai_rinsn,
386 .ai_nbits = 16,
387 .ai_pg = das08_pg_none,
388 .ai_encoding = das08_encode12,
389 .ao = das08jr_ao_winsn,
390 .ao_nbits = 16,
391 .di = das08jr_di_rbits,
392 .do_ = das08jr_do_wbits,
393 .do_nchan = 8,
394 .i8255_offset = 0,
395 .i8254_offset = 0x04,
396 .iosize = 16, /* unchecked */
398 #ifdef CONFIG_COMEDI_PCI
400 .name = "das08", /* pci-das08 */
401 .id = PCI_DEVICE_ID_PCIDAS08,
402 .bustype = pci,
403 .ai = das08_ai_rinsn,
404 .ai_nbits = 12,
405 .ai_pg = das08_bipolar5,
406 .ai_encoding = das08_encode12,
407 .ao = NULL,
408 .ao_nbits = 0,
409 .di = das08_di_rbits,
410 .do_ = das08_do_wbits,
411 .do_nchan = 4,
412 .i8255_offset = 0,
413 .i8254_offset = 4,
414 .iosize = 8,
416 #endif
418 .name = "pc104-das08",
419 .bustype = pc104,
420 .ai = das08_ai_rinsn,
421 .ai_nbits = 12,
422 .ai_pg = das08_pg_none,
423 .ai_encoding = das08_encode12,
424 .ao = NULL,
425 .ao_nbits = 0,
426 .di = das08_di_rbits,
427 .do_ = das08_do_wbits,
428 .do_nchan = 4,
429 .i8255_offset = 0,
430 .i8254_offset = 4,
431 .iosize = 16, /* unchecked */
434 .name = "das08jr/16",
435 .bustype = isa,
436 .ai = das08_ai_rinsn,
437 .ai_nbits = 16,
438 .ai_pg = das08_pg_none,
439 .ai_encoding = das08_encode16,
440 .ao = NULL,
441 .ao_nbits = 0,
442 .di = das08jr_di_rbits,
443 .do_ = das08jr_do_wbits,
444 .do_nchan = 8,
445 .i8255_offset = 0,
446 .i8254_offset = 0,
447 .iosize = 16, /* unchecked */
451 #ifdef CONFIG_COMEDI_PCMCIA
452 struct das08_board_struct das08_cs_boards[NUM_DAS08_CS_BOARDS] = {
454 .name = "pcm-das08",
455 .id = 0x0,
456 .bustype = pcmcia,
457 .ai = das08_ai_rinsn,
458 .ai_nbits = 12,
459 .ai_pg = das08_bipolar5,
460 .ai_encoding = das08_pcm_encode12,
461 .ao = NULL,
462 .ao_nbits = 0,
463 .di = das08_di_rbits,
464 .do_ = das08_do_wbits,
465 .do_nchan = 3,
466 .i8255_offset = 0,
467 .i8254_offset = 0,
468 .iosize = 16,
470 /* duplicate so driver name can be used also */
472 .name = "das08_cs",
473 .id = 0x0,
474 .bustype = pcmcia,
475 .ai = das08_ai_rinsn,
476 .ai_nbits = 12,
477 .ai_pg = das08_bipolar5,
478 .ai_encoding = das08_pcm_encode12,
479 .ao = NULL,
480 .ao_nbits = 0,
481 .di = das08_di_rbits,
482 .do_ = das08_do_wbits,
483 .do_nchan = 3,
484 .i8255_offset = 0,
485 .i8254_offset = 0,
486 .iosize = 16,
489 #endif
491 #ifdef CONFIG_COMEDI_PCI
492 static DEFINE_PCI_DEVICE_TABLE(das08_pci_table) = {
494 PCI_VENDOR_ID_COMPUTERBOARDS, PCI_DEVICE_ID_PCIDAS08,
495 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0}, {
499 MODULE_DEVICE_TABLE(pci, das08_pci_table);
500 #endif
502 #define devpriv ((struct das08_private_struct *)dev->private)
503 #define thisboard ((const struct das08_board_struct *)dev->board_ptr)
505 #define TIMEOUT 100000
507 static int das08_ai_rinsn(struct comedi_device *dev, struct comedi_subdevice *s,
508 struct comedi_insn *insn, unsigned int *data)
510 int i, n;
511 int chan;
512 int range;
513 int lsb, msb;
515 chan = CR_CHAN(insn->chanspec);
516 range = CR_RANGE(insn->chanspec);
518 /* clear crap */
519 inb(dev->iobase + DAS08_LSB);
520 inb(dev->iobase + DAS08_MSB);
522 /* set multiplexer */
523 /* lock to prevent race with digital output */
524 spin_lock(&dev->spinlock);
525 devpriv->do_mux_bits &= ~DAS08_MUX_MASK;
526 devpriv->do_mux_bits |= DAS08_MUX(chan);
527 outb(devpriv->do_mux_bits, dev->iobase + DAS08_CONTROL);
528 spin_unlock(&dev->spinlock);
530 if (s->range_table->length > 1) {
531 /* set gain/range */
532 range = CR_RANGE(insn->chanspec);
533 outb(devpriv->pg_gainlist[range],
534 dev->iobase + DAS08AO_GAIN_CONTROL);
537 for (n = 0; n < insn->n; n++) {
538 /* clear over-range bits for 16-bit boards */
539 if (thisboard->ai_nbits == 16)
540 if (inb(dev->iobase + DAS08_MSB) & 0x80)
541 printk(KERN_INFO "das08: over-range\n");
543 /* trigger conversion */
544 outb_p(0, dev->iobase + DAS08_TRIG_12BIT);
546 for (i = 0; i < TIMEOUT; i++) {
547 if (!(inb(dev->iobase + DAS08_STATUS) & DAS08_EOC))
548 break;
550 if (i == TIMEOUT) {
551 printk(KERN_ERR "das08: timeout\n");
552 return -ETIME;
554 msb = inb(dev->iobase + DAS08_MSB);
555 lsb = inb(dev->iobase + DAS08_LSB);
556 if (thisboard->ai_encoding == das08_encode12) {
557 data[n] = (lsb >> 4) | (msb << 4);
558 } else if (thisboard->ai_encoding == das08_pcm_encode12) {
559 data[n] = (msb << 8) + lsb;
560 } else if (thisboard->ai_encoding == das08_encode16) {
561 /* FPOS 16-bit boards are sign-magnitude */
562 if (msb & 0x80)
563 data[n] = (1 << 15) | lsb | ((msb & 0x7f) << 8);
564 else
565 data[n] = (1 << 15) - (lsb | (msb & 0x7f) << 8);
566 } else {
567 comedi_error(dev, "bug! unknown ai encoding");
568 return -1;
572 return n;
575 static int das08_di_rbits(struct comedi_device *dev, struct comedi_subdevice *s,
576 struct comedi_insn *insn, unsigned int *data)
578 data[0] = 0;
579 data[1] = DAS08_IP(inb(dev->iobase + DAS08_STATUS));
581 return 2;
584 static int das08_do_wbits(struct comedi_device *dev, struct comedi_subdevice *s,
585 struct comedi_insn *insn, unsigned int *data)
587 int wbits;
589 /* get current settings of digital output lines */
590 wbits = (devpriv->do_mux_bits >> 4) & 0xf;
591 /* null bits we are going to set */
592 wbits &= ~data[0];
593 /* set new bit values */
594 wbits |= data[0] & data[1];
595 /* remember digital output bits */
596 /* prevent race with setting of analog input mux */
597 spin_lock(&dev->spinlock);
598 devpriv->do_mux_bits &= ~DAS08_DO_MASK;
599 devpriv->do_mux_bits |= DAS08_OP(wbits);
600 outb(devpriv->do_mux_bits, dev->iobase + DAS08_CONTROL);
601 spin_unlock(&dev->spinlock);
603 data[1] = wbits;
605 return 2;
608 static int das08jr_di_rbits(struct comedi_device *dev,
609 struct comedi_subdevice *s,
610 struct comedi_insn *insn, unsigned int *data)
612 data[0] = 0;
613 data[1] = inb(dev->iobase + DAS08JR_DIO);
615 return 2;
618 static int das08jr_do_wbits(struct comedi_device *dev,
619 struct comedi_subdevice *s,
620 struct comedi_insn *insn, unsigned int *data)
622 /* null bits we are going to set */
623 devpriv->do_bits &= ~data[0];
624 /* set new bit values */
625 devpriv->do_bits |= data[0] & data[1];
626 outb(devpriv->do_bits, dev->iobase + DAS08JR_DIO);
628 data[1] = devpriv->do_bits;
630 return 2;
633 static int das08jr_ao_winsn(struct comedi_device *dev,
634 struct comedi_subdevice *s,
635 struct comedi_insn *insn, unsigned int *data)
637 int n;
638 int lsb, msb;
639 int chan;
641 lsb = data[0] & 0xff;
642 msb = (data[0] >> 8) & 0xf;
644 chan = CR_CHAN(insn->chanspec);
646 for (n = 0; n < insn->n; n++) {
647 outb(lsb, dev->iobase + DAS08JR_AO_LSB(chan));
648 outb(msb, dev->iobase + DAS08JR_AO_MSB(chan));
650 /* load DACs */
651 inb(dev->iobase + DAS08JR_DIO);
654 return n;
659 * The -aox boards have the DACs at a different offset and use
660 * a different method to force an update.
663 static int das08ao_ao_winsn(struct comedi_device *dev,
664 struct comedi_subdevice *s,
665 struct comedi_insn *insn, unsigned int *data)
667 int n;
668 int lsb, msb;
669 int chan;
671 lsb = data[0] & 0xff;
672 msb = (data[0] >> 8) & 0xf;
674 chan = CR_CHAN(insn->chanspec);
676 for (n = 0; n < insn->n; n++) {
677 outb(lsb, dev->iobase + DAS08AO_AO_LSB(chan));
678 outb(msb, dev->iobase + DAS08AO_AO_MSB(chan));
680 /* load DACs */
681 inb(dev->iobase + DAS08AO_AO_UPDATE);
684 return n;
687 static unsigned int i8254_read_channel_low(unsigned int base, int chan)
689 unsigned int msb, lsb;
691 /* The following instructions must be in order.
692 We must avoid other process reading the counter's value in the
693 middle.
694 The spin_lock isn't needed since ioctl calls grab the big kernel
695 lock automatically */
696 /*spin_lock(sp); */
697 outb(chan << 6, base + I8254_CTRL);
698 base += chan;
699 lsb = inb(base);
700 msb = inb(base);
701 /*spin_unlock(sp); */
703 return lsb | (msb << 8);
706 static void i8254_write_channel_low(unsigned int base, int chan,
707 unsigned int value)
709 unsigned int msb, lsb;
711 lsb = value & 0xFF;
712 msb = value >> 8;
714 /* write lsb, then msb */
715 base += chan;
716 /* See comments in i8254_read_channel_low */
717 /*spin_lock(sp); */
718 outb(lsb, base);
719 outb(msb, base);
720 /*spin_unlock(sp); */
723 static unsigned int i8254_read_channel(struct i8254_struct *st, int channel)
725 int chan = st->logic2phys[channel];
727 return i8254_read_channel_low(st->iobase, chan);
730 static void i8254_write_channel(struct i8254_struct *st, int channel,
731 unsigned int value)
733 int chan = st->logic2phys[channel];
735 i8254_write_channel_low(st->iobase, chan, value);
738 static void i8254_initialize(struct i8254_struct *st)
740 int i;
741 for (i = 0; i < 3; ++i)
742 i8254_set_mode_low(st->iobase, i, st->mode[i]);
745 static void i8254_set_mode_low(unsigned int base, int channel,
746 unsigned int mode)
748 outb((channel << 6) | 0x30 | (mode & 0x0F), base + I8254_CTRL);
751 static void i8254_set_mode(struct i8254_struct *st, int channel,
752 unsigned int mode)
754 int chan = st->logic2phys[channel];
756 st->mode[chan] = mode;
757 return i8254_set_mode_low(st->iobase, chan, mode);
760 static unsigned int i8254_read_status_low(unsigned int base, int channel)
762 outb(0xE0 | (2 << channel), base + I8254_CTRL);
763 return inb(base + channel);
766 static unsigned int i8254_read_status(struct i8254_struct *st, int channel)
768 int chan = st->logic2phys[channel];
770 return i8254_read_status_low(st->iobase, chan);
773 static int das08_counter_read(struct comedi_device *dev,
774 struct comedi_subdevice *s,
775 struct comedi_insn *insn, unsigned int *data)
777 int chan = insn->chanspec;
779 /* printk("Reading counter channel %d ",chan); */
780 data[0] = i8254_read_channel(&devpriv->i8254, chan);
781 /* printk("=> 0x%08X\n",data[0]); */
783 return 1;
786 static int das08_counter_write(struct comedi_device *dev,
787 struct comedi_subdevice *s,
788 struct comedi_insn *insn, unsigned int *data)
790 int chan = insn->chanspec;
792 /* printk("Writing counter channel %d with 0x%04X\n",chan,data[0]); */
793 i8254_write_channel(&devpriv->i8254, chan, data[0]);
795 return 1;
798 static int das08_counter_config(struct comedi_device *dev,
799 struct comedi_subdevice *s,
800 struct comedi_insn *insn, unsigned int *data)
802 int chan = insn->chanspec;
804 if (insn->n != 2)
805 return -EINVAL;
807 switch (data[0]) {
808 case INSN_CONFIG_SET_COUNTER_MODE:
809 i8254_set_mode(&devpriv->i8254, chan, data[1]);
810 break;
811 case INSN_CONFIG_8254_READ_STATUS:
812 data[1] = i8254_read_status(&devpriv->i8254, chan);
813 break;
814 default:
815 return -EINVAL;
816 break;
818 return 2;
821 static int das08_attach(struct comedi_device *dev, struct comedi_devconfig *it);
823 static struct comedi_driver driver_das08 = {
824 .driver_name = DRV_NAME,
825 .module = THIS_MODULE,
826 .attach = das08_attach,
827 .detach = das08_common_detach,
828 .board_name = &das08_boards[0].name,
829 .num_names = sizeof(das08_boards) / sizeof(struct das08_board_struct),
830 .offset = sizeof(struct das08_board_struct),
833 int das08_common_attach(struct comedi_device *dev, unsigned long iobase)
835 struct comedi_subdevice *s;
836 int ret;
838 /* allocate ioports for non-pcmcia, non-pci boards */
839 if ((thisboard->bustype != pcmcia) && (thisboard->bustype != pci)) {
840 printk(KERN_INFO " iobase 0x%lx\n", iobase);
841 if (!request_region(iobase, thisboard->iosize, DRV_NAME)) {
842 printk(KERN_ERR " I/O port conflict\n");
843 return -EIO;
846 dev->iobase = iobase;
848 dev->board_name = thisboard->name;
850 ret = alloc_subdevices(dev, 6);
851 if (ret < 0)
852 return ret;
854 s = dev->subdevices + 0;
855 /* ai */
856 if (thisboard->ai) {
857 s->type = COMEDI_SUBD_AI;
858 s->subdev_flags = SDF_READABLE | SDF_GROUND;
859 s->n_chan = 8;
860 s->maxdata = (1 << thisboard->ai_nbits) - 1;
861 s->range_table = das08_ai_lranges[thisboard->ai_pg];
862 s->insn_read = thisboard->ai;
863 devpriv->pg_gainlist = das08_gainlists[thisboard->ai_pg];
864 } else {
865 s->type = COMEDI_SUBD_UNUSED;
868 s = dev->subdevices + 1;
869 /* ao */
870 if (thisboard->ao) {
871 s->type = COMEDI_SUBD_AO;
872 s->subdev_flags = SDF_WRITABLE;
873 s->n_chan = 2;
874 s->maxdata = (1 << thisboard->ao_nbits) - 1;
875 s->range_table = &range_bipolar5;
876 s->insn_write = thisboard->ao;
877 } else {
878 s->type = COMEDI_SUBD_UNUSED;
881 s = dev->subdevices + 2;
882 /* di */
883 if (thisboard->di) {
884 s->type = COMEDI_SUBD_DI;
885 s->subdev_flags = SDF_READABLE;
886 s->n_chan = (thisboard->di == das08_di_rbits) ? 3 : 8;
887 s->maxdata = 1;
888 s->range_table = &range_digital;
889 s->insn_bits = thisboard->di;
890 } else {
891 s->type = COMEDI_SUBD_UNUSED;
894 s = dev->subdevices + 3;
895 /* do */
896 if (thisboard->do_) {
897 s->type = COMEDI_SUBD_DO;
898 s->subdev_flags = SDF_WRITABLE | SDF_READABLE;
899 s->n_chan = thisboard->do_nchan;
900 s->maxdata = 1;
901 s->range_table = &range_digital;
902 s->insn_bits = thisboard->do_;
903 } else {
904 s->type = COMEDI_SUBD_UNUSED;
907 s = dev->subdevices + 4;
908 /* 8255 */
909 if (thisboard->i8255_offset != 0) {
910 subdev_8255_init(dev, s, NULL, (unsigned long)(dev->iobase +
911 thisboard->
912 i8255_offset));
913 } else {
914 s->type = COMEDI_SUBD_UNUSED;
917 s = dev->subdevices + 5;
918 /* 8254 */
919 if (thisboard->i8254_offset != 0) {
920 s->type = COMEDI_SUBD_COUNTER;
921 s->subdev_flags = SDF_WRITABLE | SDF_READABLE;
922 s->n_chan = 3;
923 s->maxdata = 0xFFFF;
924 s->insn_read = das08_counter_read;
925 s->insn_write = das08_counter_write;
926 s->insn_config = das08_counter_config;
927 /* Set-up the 8254 structure */
928 devpriv->i8254.channels = 3;
929 devpriv->i8254.logic2phys[0] = 0;
930 devpriv->i8254.logic2phys[1] = 1;
931 devpriv->i8254.logic2phys[2] = 2;
932 devpriv->i8254.iobase = iobase + thisboard->i8254_offset;
933 devpriv->i8254.mode[0] =
934 devpriv->i8254.mode[1] =
935 devpriv->i8254.mode[2] = I8254_MODE0 | I8254_BINARY;
936 i8254_initialize(&devpriv->i8254);
937 } else {
938 s->type = COMEDI_SUBD_UNUSED;
941 return 0;
943 EXPORT_SYMBOL_GPL(das08_common_attach);
945 static int das08_attach(struct comedi_device *dev, struct comedi_devconfig *it)
947 int ret;
948 unsigned long iobase;
949 #ifdef CONFIG_COMEDI_PCI
950 unsigned long pci_iobase = 0;
951 struct pci_dev *pdev = NULL;
952 #endif
954 ret = alloc_private(dev, sizeof(struct das08_private_struct));
955 if (ret < 0)
956 return ret;
958 printk(KERN_INFO "comedi%d: das08: ", dev->minor);
959 /* deal with a pci board */
960 if (thisboard->bustype == pci) {
961 #ifdef CONFIG_COMEDI_PCI
962 if (it->options[0] || it->options[1]) {
963 printk("bus %i slot %i ",
964 it->options[0], it->options[1]);
966 printk("\n");
967 /* find card */
968 for_each_pci_dev(pdev) {
969 if (pdev->vendor == PCI_VENDOR_ID_COMPUTERBOARDS
970 && pdev->device == PCI_DEVICE_ID_PCIDAS08) {
971 if (it->options[0] || it->options[1]) {
972 if (pdev->bus->number == it->options[0]
973 && PCI_SLOT(pdev->devfn) ==
974 it->options[1]) {
975 break;
977 } else {
978 break;
982 if (!pdev) {
983 printk(KERN_ERR "No pci das08 cards found\n");
984 return -EIO;
986 devpriv->pdev = pdev;
987 /* enable PCI device and reserve I/O spaces */
988 if (comedi_pci_enable(pdev, DRV_NAME)) {
989 printk(KERN_ERR " Error enabling PCI device and "
990 "requesting regions\n");
991 return -EIO;
993 /* read base addresses */
994 pci_iobase = pci_resource_start(pdev, 1);
995 iobase = pci_resource_start(pdev, 2);
996 printk(KERN_INFO "pcibase 0x%lx iobase 0x%lx\n",
997 pci_iobase, iobase);
998 devpriv->pci_iobase = pci_iobase;
999 #else /* CONFIG_COMEDI_PCI */
1000 printk(KERN_ERR "this driver has not been built with PCI support.\n");
1001 return -EINVAL;
1002 #endif /* CONFIG_COMEDI_PCI */
1003 } else {
1004 iobase = it->options[0];
1006 printk(KERN_INFO "\n");
1008 return das08_common_attach(dev, iobase);
1012 int das08_common_detach(struct comedi_device *dev)
1014 printk(KERN_INFO "comedi%d: das08: remove\n", dev->minor);
1016 if (dev->subdevices)
1017 subdev_8255_cleanup(dev, dev->subdevices + 4);
1019 /* deallocate ioports for non-pcmcia, non-pci boards */
1020 if ((thisboard->bustype != pcmcia) && (thisboard->bustype != pci)) {
1021 if (dev->iobase)
1022 release_region(dev->iobase, thisboard->iosize);
1024 #ifdef CONFIG_COMEDI_PCI
1025 if (devpriv) {
1026 if (devpriv->pdev) {
1027 if (devpriv->pci_iobase)
1028 comedi_pci_disable(devpriv->pdev);
1030 pci_dev_put(devpriv->pdev);
1033 #endif
1035 return 0;
1037 EXPORT_SYMBOL_GPL(das08_common_detach);
1039 #ifdef CONFIG_COMEDI_PCI
1040 static int __devinit driver_das08_pci_probe(struct pci_dev *dev,
1041 const struct pci_device_id *ent)
1043 return comedi_pci_auto_config(dev, driver_das08.driver_name);
1046 static void __devexit driver_das08_pci_remove(struct pci_dev *dev)
1048 comedi_pci_auto_unconfig(dev);
1051 static struct pci_driver driver_das08_pci_driver = {
1052 .id_table = das08_pci_table,
1053 .probe = &driver_das08_pci_probe,
1054 .remove = __devexit_p(&driver_das08_pci_remove)
1057 static int __init driver_das08_init_module(void)
1059 int retval;
1061 retval = comedi_driver_register(&driver_das08);
1062 if (retval < 0)
1063 return retval;
1065 driver_das08_pci_driver.name = (char *)driver_das08.driver_name;
1066 return pci_register_driver(&driver_das08_pci_driver);
1069 static void __exit driver_das08_cleanup_module(void)
1071 pci_unregister_driver(&driver_das08_pci_driver);
1072 comedi_driver_unregister(&driver_das08);
1075 module_init(driver_das08_init_module);
1076 module_exit(driver_das08_cleanup_module);
1077 #else
1078 static int __init driver_das08_init_module(void)
1080 return comedi_driver_register(&driver_das08);
1083 static void __exit driver_das08_cleanup_module(void)
1085 comedi_driver_unregister(&driver_das08);
1088 module_init(driver_das08_init_module);
1089 module_exit(driver_das08_cleanup_module);
1090 #endif
1092 #ifdef CONFIG_COMEDI_PCMCIA
1093 EXPORT_SYMBOL_GPL(das08_cs_boards);
1094 #endif
1096 MODULE_AUTHOR("Comedi http://www.comedi.org");
1097 MODULE_DESCRIPTION("Comedi low-level driver");
1098 MODULE_LICENSE("GPL");