2 comedi/drivers/ssv_dnp.c
3 generic comedi driver for SSV Embedded Systems' DIL/Net-PCs
4 Copyright (C) 2001 Robert Schwebel <robert@schwebel.de>
6 COMEDI - Linux Control and Measurement Device Interface
7 Copyright (C) 2000 David A. Schleef <ds@schleef.org>
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 Description: SSV Embedded Systems DIL/Net-PC
27 Author: Robert Schwebel <robert@schwebel.de>
28 Devices: [SSV Embedded Systems] DIL/Net-PC 1486 (dnp-1486)
32 /* include files ----------------------------------------------------------- */
34 #include <linux/comedidev.h>
37 /* Some global definitions: the registers of the DNP ----------------------- */
39 /* For port A and B the mode register has bits corresponding to the output */
40 /* pins, where Bit-N = 0 -> input, Bit-N = 1 -> output. Note that bits */
41 /* 4 to 7 correspond to pin 0..3 for port C data register. Ensure that bits */
42 /* 0..3 remain unchanged! For details about Port C Mode Register see */
43 /* the remarks in dnp_insn_config() below. */
45 #define CSCIR 0x22 /* Chip Setup and Control Index Register */
46 #define CSCDR 0x23 /* Chip Setup and Control Data Register */
47 #define PAMR 0xa5 /* Port A Mode Register */
48 #define PADR 0xa9 /* Port A Data Register */
49 #define PBMR 0xa4 /* Port B Mode Register */
50 #define PBDR 0xa8 /* Port B Data Register */
51 #define PCMR 0xa3 /* Port C Mode Register */
52 #define PCDR 0xa7 /* Port C Data Register */
55 /* This data structure holds information about the supported boards -------- */
57 typedef struct dnp_board_struct{
64 static dnp_board dnp_boards[] = { /* we only support one DNP 'board' */
65 { /* variant at the moment */
74 /* Useful for shorthand access to the particular board structure ----------- */
75 #define thisboard ((dnp_board *)dev->board_ptr)
78 /* This structure is for data unique to the DNP driver --------------------- */
83 /* Shorthand macro for faster access to the private data ------------------- */
84 #define devpriv ((dnp_private *)dev->private)
87 /* ------------------------------------------------------------------------- */
88 /* The comedi_driver structure tells the Comedi core module which functions */
89 /* to call to configure/deconfigure (attach/detach) the board, and also */
90 /* about the kernel module that contains the device code. */
92 /* In the following section we define the API of this driver. */
93 /* ------------------------------------------------------------------------- */
95 static int dnp_attach(comedi_device *dev,comedi_devconfig *it);
96 static int dnp_detach(comedi_device *dev);
98 static comedi_driver driver_dnp = {
99 driver_name: "ssv_dnp",
103 board_name: &dnp_boards[0].name, /* only necessary for non-PnP devs */
104 offset: sizeof(dnp_board), /* like ISA-PnP, PCI or PCMCIA. */
105 num_names: sizeof(dnp_boards) / sizeof(dnp_board),
107 COMEDI_INITCLEANUP(driver_dnp);
109 static int dnp_dio_insn_bits(
116 static int dnp_dio_insn_config(
124 /* ------------------------------------------------------------------------- */
125 /* Attach is called by comedi core to configure the driver for a particular */
126 /* board. If you specified a board_name array in the driver structure, */
127 /* dev->board_ptr contains that address. */
128 /* ------------------------------------------------------------------------- */
130 static int dnp_attach(comedi_device *dev,comedi_devconfig *it)
135 printk("comedi%d: dnp: ",dev->minor);
137 /* Autoprobing: this should find out which board we have. Currently only */
138 /* the 1486 board is supported and autoprobing is not implemented :-) */
139 //dev->board_ptr = dnp_probe(dev);
141 /* Initialize the name of the board. We can use the "thisboard" macro now. */
142 dev->board_name = thisboard->name;
144 /* Allocate the private structure area. alloc_private() is a convenient */
145 /* macro defined in comedidev.h. */
146 if(alloc_private(dev,sizeof(dnp_private_data))<0) return -ENOMEM;
148 /* Allocate the subdevice structures. alloc_subdevice() is a convenient */
149 /* macro defined in comedidev.h. */
151 if(alloc_subdevices(dev, 1)<0) return -ENOMEM;
154 /* digital i/o subdevice */
155 s->type = COMEDI_SUBD_DIO;
156 s->subdev_flags = SDF_READABLE|SDF_WRITABLE;
159 s->range_table =& range_digital;
160 s->insn_bits = dnp_dio_insn_bits;
161 s->insn_config = dnp_dio_insn_config;
163 printk("attached\n");
165 /* We use the I/O ports 0x22,0x23 and 0xa3-0xa9, which are always
166 * allocated for the primary 8259, so we don't need to allocate them
169 /* configure all ports as input (default) */
170 outb(PAMR,CSCIR); outb(0x00,CSCDR);
171 outb(PBMR,CSCIR); outb(0x00,CSCDR);
172 outb(PCMR,CSCIR); outb((inb(CSCDR) & 0xAA),CSCDR);
179 /* ------------------------------------------------------------------------- */
180 /* detach is called to deconfigure a device. It should deallocate the */
181 /* resources. This function is also called when _attach() fails, so it */
182 /* should be careful not to release resources that were not necessarily */
183 /* allocated by _attach(). dev->private and dev->subdevices are */
184 /* deallocated automatically by the core. */
185 /* ------------------------------------------------------------------------- */
187 static int dnp_detach(comedi_device *dev)
190 /* configure all ports as input (default) */
191 outb(PAMR,CSCIR); outb(0x00,CSCDR);
192 outb(PBMR,CSCIR); outb(0x00,CSCDR);
193 outb(PCMR,CSCIR); outb((inb(CSCDR) & 0xAA),CSCDR);
195 /* announce that we are finished */
196 printk("comedi%d: dnp: remove\n",dev->minor);
203 /* ------------------------------------------------------------------------- */
204 /* The insn_bits interface allows packed reading/writing of DIO channels. */
205 /* The comedi core can convert between insn_bits and insn_read/write, so you */
206 /* are able to use these instructions as well. */
207 /* ------------------------------------------------------------------------- */
209 static int dnp_dio_insn_bits(
216 if (insn->n!=2) return -EINVAL; /* insn uses data[0] and data[1] */
218 /* The insn data is a mask in data[0] and the new data in data[1], each */
219 /* channel cooresponding to a bit. */
221 /* Ports A and B are straight forward: each bit corresponds to an output */
222 /* pin with the same order. Port C is different: bits 0...3 correspond to */
223 /* bits 4...7 of the output register (PCDR). */
230 & ~(u8)(data[0] & 0x0000FF))
231 | (u8)(data[1] & 0x0000FF),
238 & ~(u8)((data[0] & 0x00FF00) >> 8))
239 | (u8)((data[1] & 0x00FF00) >> 8),
246 & ~(u8)((data[0] & 0x0F0000) >> 12))
247 | (u8)((data[1] & 0x0F0000) >> 12),
252 /* on return, data[1] contains the value of the digital input lines. */
254 data[0] = inb(CSCDR);
256 data[0] += inb(CSCDR) << 8;
258 data[0] += ((inb(CSCDR) & 0xF0) << 12);
265 /* ------------------------------------------------------------------------- */
266 /* Configure the direction of the bidirectional digital i/o pins. chanspec */
267 /* contains the channel to be changed and data[0] contains either */
268 /* COMEDI_INPUT or COMEDI_OUTPUT. */
269 /* ------------------------------------------------------------------------- */
271 static int dnp_dio_insn_config(
281 int chan=CR_CHAN(insn->chanspec); /* reduces chanspec to lower 16 bits */
285 case INSN_CONFIG_DIO_OUTPUT:
286 case INSN_CONFIG_DIO_INPUT:
288 case INSN_CONFIG_DIO_QUERY:
289 data[1] = (inb(CSCDR) & (1 << chan)) ? COMEDI_OUTPUT : COMEDI_INPUT;
296 /* Test: which port does the channel belong to? */
298 /* We have to pay attention with port C: this is the meaning of PCMR: */
299 /* Bit in PCMR: 7 6 5 4 3 2 1 0 */
300 /* Corresponding port C pin: d 3 d 2 d 1 d 0 d= don't touch */
302 if ((chan >= 0) && (chan <= 7)) {
306 else if ((chan >= 8) && (chan <= 15)) {
311 else if ((chan >= 16) && (chan <= 19)) {
312 /* this is port C; multiplication with 2 brings bits into correct */
313 /* position for PCMR! */
318 else { return -EINVAL; }
320 /* read 'old' direction of the port and set bits (out=1, in=0) */
321 register_buffer = inb(CSCDR);
322 if (data[0] == COMEDI_OUTPUT) { register_buffer |= (1<<chan); }
323 else { register_buffer &= ~(1<<chan); }
324 outb(register_buffer,CSCDR);