Staging: comedi: Remove comedi_subdevice typedef
[safe/jmp/linux-2.6] / drivers / staging / comedi / drivers / ssv_dnp.c
1 /*
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>
5
6     COMEDI - Linux Control and Measurement Device Interface
7     Copyright (C) 2000 David A. Schleef <ds@schleef.org>
8
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.
13
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.
18
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.
22
23 */
24 /*
25 Driver: ssv_dnp
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)
29 Status: unknown
30 */
31
32 /* include files ----------------------------------------------------------- */
33
34 #include "../comedidev.h"
35
36 /* Some global definitions: the registers of the DNP ----------------------- */
37 /*                                                                           */
38 /* For port A and B the mode register has bits corresponding to the output   */
39 /* pins, where Bit-N = 0 -> input, Bit-N = 1 -> output. Note that bits       */
40 /* 4 to 7 correspond to pin 0..3 for port C data register. Ensure that bits  */
41 /* 0..3 remain unchanged! For details about Port C Mode Register see         */
42 /* the remarks in dnp_insn_config() below.                                   */
43
44 #define CSCIR 0x22              /* Chip Setup and Control Index Register       */
45 #define CSCDR 0x23              /* Chip Setup and Control Data Register        */
46 #define PAMR  0xa5              /* Port A Mode Register                        */
47 #define PADR  0xa9              /* Port A Data Register                        */
48 #define PBMR  0xa4              /* Port B Mode Register                        */
49 #define PBDR  0xa8              /* Port B Data Register                        */
50 #define PCMR  0xa3              /* Port C Mode Register                        */
51 #define PCDR  0xa7              /* Port C Data Register                        */
52
53 /* This data structure holds information about the supported boards -------- */
54
55 typedef struct dnp_board_struct {
56         const char *name;
57         int ai_chans;
58         int ai_bits;
59         int have_dio;
60 } dnp_board;
61
62 static const dnp_board dnp_boards[] = { /* we only support one DNP 'board'   */
63         {                       /* variant at the moment             */
64               name:     "dnp-1486",
65               ai_chans:16,
66               ai_bits:  12,
67               have_dio:1,
68                 },
69 };
70
71 /* Useful for shorthand access to the particular board structure ----------- */
72 #define thisboard ((const dnp_board *)dev->board_ptr)
73
74 /* This structure is for data unique to the DNP driver --------------------- */
75 typedef struct {
76         //
77 } dnp_private_data;
78
79 /* Shorthand macro for faster access to the private data ------------------- */
80 #define devpriv ((dnp_private *)dev->private)
81
82 /* ------------------------------------------------------------------------- */
83 /* The comedi_driver structure tells the Comedi core module which functions  */
84 /* to call to configure/deconfigure (attach/detach) the board, and also      */
85 /* about the kernel module that contains the device code.                    */
86 /*                                                                           */
87 /* In the following section we define the API of this driver.                */
88 /* ------------------------------------------------------------------------- */
89
90 static int dnp_attach(struct comedi_device * dev, comedi_devconfig * it);
91 static int dnp_detach(struct comedi_device * dev);
92
93 static comedi_driver driver_dnp = {
94       driver_name:"ssv_dnp",
95       module:THIS_MODULE,
96       attach:dnp_attach,
97       detach:dnp_detach,
98       board_name:&dnp_boards[0].name,
99         /* only necessary for non-PnP devs   */
100       offset:sizeof(dnp_board),/* like ISA-PnP, PCI or PCMCIA.      */
101       num_names:sizeof(dnp_boards) / sizeof(dnp_board),
102 };
103
104 COMEDI_INITCLEANUP(driver_dnp);
105
106 static int dnp_dio_insn_bits(struct comedi_device * dev,
107         struct comedi_subdevice * s, comedi_insn * insn, unsigned int * data);
108
109 static int dnp_dio_insn_config(struct comedi_device * dev,
110         struct comedi_subdevice * s, comedi_insn * insn, unsigned int * data);
111
112 /* ------------------------------------------------------------------------- */
113 /* Attach is called by comedi core to configure the driver for a particular  */
114 /* board. If you specified a board_name array in the driver structure,       */
115 /* dev->board_ptr contains that address.                                     */
116 /* ------------------------------------------------------------------------- */
117
118 static int dnp_attach(struct comedi_device * dev, comedi_devconfig * it)
119 {
120
121         struct comedi_subdevice *s;
122
123         printk("comedi%d: dnp: ", dev->minor);
124
125         /* Autoprobing: this should find out which board we have. Currently only   */
126         /* the 1486 board is supported and autoprobing is not implemented :-)      */
127         //dev->board_ptr = dnp_probe(dev);
128
129         /* Initialize the name of the board. We can use the "thisboard" macro now. */
130         dev->board_name = thisboard->name;
131
132         /* Allocate the private structure area. alloc_private() is a convenient    */
133         /* macro defined in comedidev.h.                                           */
134         if (alloc_private(dev, sizeof(dnp_private_data)) < 0)
135                 return -ENOMEM;
136
137         /* Allocate the subdevice structures. alloc_subdevice() is a convenient    */
138         /* macro defined in comedidev.h.                                           */
139
140         if (alloc_subdevices(dev, 1) < 0)
141                 return -ENOMEM;
142
143         s = dev->subdevices + 0;
144         /* digital i/o subdevice                                                   */
145         s->type = COMEDI_SUBD_DIO;
146         s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
147         s->n_chan = 20;
148         s->maxdata = 1;
149         s->range_table = &range_digital;
150         s->insn_bits = dnp_dio_insn_bits;
151         s->insn_config = dnp_dio_insn_config;
152
153         printk("attached\n");
154
155         /* We use the I/O ports 0x22,0x23 and 0xa3-0xa9, which are always
156          * allocated for the primary 8259, so we don't need to allocate them
157          * ourselves. */
158
159         /* configure all ports as input (default)                                  */
160         outb(PAMR, CSCIR);
161         outb(0x00, CSCDR);
162         outb(PBMR, CSCIR);
163         outb(0x00, CSCDR);
164         outb(PCMR, CSCIR);
165         outb((inb(CSCDR) & 0xAA), CSCDR);
166
167         return 1;
168
169 }
170
171 /* ------------------------------------------------------------------------- */
172 /* detach is called to deconfigure a device. It should deallocate the        */
173 /* resources. This function is also called when _attach() fails, so it       */
174 /* should be careful not to release resources that were not necessarily      */
175 /* allocated by _attach(). dev->private and dev->subdevices are              */
176 /* deallocated automatically by the core.                                    */
177 /* ------------------------------------------------------------------------- */
178
179 static int dnp_detach(struct comedi_device * dev)
180 {
181
182         /* configure all ports as input (default)                                  */
183         outb(PAMR, CSCIR);
184         outb(0x00, CSCDR);
185         outb(PBMR, CSCIR);
186         outb(0x00, CSCDR);
187         outb(PCMR, CSCIR);
188         outb((inb(CSCDR) & 0xAA), CSCDR);
189
190         /* announce that we are finished                                           */
191         printk("comedi%d: dnp: remove\n", dev->minor);
192
193         return 0;
194
195 }
196
197 /* ------------------------------------------------------------------------- */
198 /* The insn_bits interface allows packed reading/writing of DIO channels.    */
199 /* The comedi core can convert between insn_bits and insn_read/write, so you */
200 /* are able to use these instructions as well.                               */
201 /* ------------------------------------------------------------------------- */
202
203 static int dnp_dio_insn_bits(struct comedi_device * dev,
204         struct comedi_subdevice * s, comedi_insn * insn, unsigned int * data)
205 {
206
207         if (insn->n != 2)
208                 return -EINVAL; /* insn uses data[0] and data[1]     */
209
210         /* The insn data is a mask in data[0] and the new data in data[1], each    */
211         /* channel cooresponding to a bit.                                         */
212
213         /* Ports A and B are straight forward: each bit corresponds to an output   */
214         /* pin with the same order. Port C is different: bits 0...3 correspond to  */
215         /* bits 4...7 of the output register (PCDR).                               */
216
217         if (data[0]) {
218
219                 outb(PADR, CSCIR);
220                 outb((inb(CSCDR)
221                                 & ~(u8) (data[0] & 0x0000FF))
222                         | (u8) (data[1] & 0x0000FF), CSCDR);
223
224                 outb(PBDR, CSCIR);
225                 outb((inb(CSCDR)
226                                 & ~(u8) ((data[0] & 0x00FF00) >> 8))
227                         | (u8) ((data[1] & 0x00FF00) >> 8), CSCDR);
228
229                 outb(PCDR, CSCIR);
230                 outb((inb(CSCDR)
231                                 & ~(u8) ((data[0] & 0x0F0000) >> 12))
232                         | (u8) ((data[1] & 0x0F0000) >> 12), CSCDR);
233         }
234
235         /* on return, data[1] contains the value of the digital input lines.       */
236         outb(PADR, CSCIR);
237         data[0] = inb(CSCDR);
238         outb(PBDR, CSCIR);
239         data[0] += inb(CSCDR) << 8;
240         outb(PCDR, CSCIR);
241         data[0] += ((inb(CSCDR) & 0xF0) << 12);
242
243         return 2;
244
245 }
246
247 /* ------------------------------------------------------------------------- */
248 /* Configure the direction of the bidirectional digital i/o pins. chanspec   */
249 /* contains the channel to be changed and data[0] contains either            */
250 /* COMEDI_INPUT or COMEDI_OUTPUT.                                            */
251 /* ------------------------------------------------------------------------- */
252
253 static int dnp_dio_insn_config(struct comedi_device * dev,
254         struct comedi_subdevice * s, comedi_insn * insn, unsigned int * data)
255 {
256
257         u8 register_buffer;
258
259         int chan = CR_CHAN(insn->chanspec);     /* reduces chanspec to lower 16 bits */
260
261         switch (data[0]) {
262         case INSN_CONFIG_DIO_OUTPUT:
263         case INSN_CONFIG_DIO_INPUT:
264                 break;
265         case INSN_CONFIG_DIO_QUERY:
266                 data[1] =
267                         (inb(CSCDR) & (1 << chan)) ? COMEDI_OUTPUT :
268                         COMEDI_INPUT;
269                 return insn->n;
270                 break;
271         default:
272                 return -EINVAL;
273                 break;
274         }
275         /* Test: which port does the channel belong to?                            */
276
277         /* We have to pay attention with port C: this is the meaning of PCMR:      */
278         /* Bit in PCMR:              7 6 5 4 3 2 1 0                               */
279         /* Corresponding port C pin: d 3 d 2 d 1 d 0   d= don't touch              */
280
281         if ((chan >= 0) && (chan <= 7)) {
282                 /* this is port A */
283                 outb(PAMR, CSCIR);
284         } else if ((chan >= 8) && (chan <= 15)) {
285                 /* this is port B */
286                 chan -= 8;
287                 outb(PBMR, CSCIR);
288         } else if ((chan >= 16) && (chan <= 19)) {
289                 /* this is port C; multiplication with 2 brings bits into correct        */
290                 /* position for PCMR!                                                    */
291                 chan -= 16;
292                 chan *= 2;
293                 outb(PCMR, CSCIR);
294         } else {
295                 return -EINVAL;
296         }
297
298         /* read 'old' direction of the port and set bits (out=1, in=0)             */
299         register_buffer = inb(CSCDR);
300         if (data[0] == COMEDI_OUTPUT) {
301                 register_buffer |= (1 << chan);
302         } else {
303                 register_buffer &= ~(1 << chan);
304         }
305         outb(register_buffer, CSCDR);
306
307         return 1;
308
309 }