Got rid of unnecessary casts when initializing comedi_driver.board_name
[comedi.git] / 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.o
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 <linux/comedidev.h>
35
36
37 /* Some global definitions: the registers of the DNP ----------------------- */
38 /*                                                                           */
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.                                   */
44
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                        */
53
54
55 /* This data structure holds information about the supported boards -------- */
56
57 typedef struct dnp_board_struct{
58         const char *name;
59         int ai_chans;
60         int ai_bits;
61         int have_dio;
62 } dnp_board;
63
64 static dnp_board dnp_boards[] = {              /* we only support one DNP 'board'   */
65         {                               /* variant at the moment             */
66         name:           "dnp-1486",
67         ai_chans:       16,
68         ai_bits:        12,
69         have_dio:       1,
70         },
71 };
72
73
74 /* Useful for shorthand access to the particular board structure ----------- */
75 #define thisboard ((dnp_board *)dev->board_ptr)
76
77
78 /* This structure is for data unique to the DNP driver --------------------- */
79 typedef struct{
80   //
81 } dnp_private_data;
82
83 /* Shorthand macro for faster access to the private data ------------------- */
84 #define devpriv ((dnp_private *)dev->private)
85
86
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.                    */
91 /*                                                                           */
92 /* In the following section we define the API of this driver.                */
93 /* ------------------------------------------------------------------------- */
94
95 static int dnp_attach(comedi_device *dev,comedi_devconfig *it);
96 static int dnp_detach(comedi_device *dev);
97
98 static comedi_driver driver_dnp = {
99   driver_name:  "ssv_dnp",
100   module:       THIS_MODULE,
101   attach:       dnp_attach,
102   detach:       dnp_detach,
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),
106 };
107 COMEDI_INITCLEANUP(driver_dnp);
108
109 static int dnp_dio_insn_bits(
110   comedi_device    *dev,
111   comedi_subdevice *s,
112   comedi_insn      *insn,
113   lsampl_t         *data
114 );
115
116 static int dnp_dio_insn_config(
117   comedi_device    *dev,
118   comedi_subdevice *s,
119   comedi_insn      *insn,
120   lsampl_t *data
121 );
122
123
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 /* ------------------------------------------------------------------------- */
129
130 static int dnp_attach(comedi_device *dev,comedi_devconfig *it)
131 {
132
133   comedi_subdevice *s;
134
135   printk("comedi%d: dnp: ",dev->minor);
136
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);
140
141   /* Initialize the name of the board. We can use the "thisboard" macro now. */
142   dev->board_name = thisboard->name;
143
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;
147
148   /* Allocate the subdevice structures. alloc_subdevice() is a convenient    */
149   /* macro defined in comedidev.h.                                           */
150
151   if(alloc_subdevices(dev, 1)<0) return -ENOMEM;
152
153   s=dev->subdevices+0;
154   /* digital i/o subdevice                                                   */
155   s->type         =  COMEDI_SUBD_DIO;
156   s->subdev_flags =  SDF_READABLE|SDF_WRITABLE;
157   s->n_chan       =  20;
158   s->maxdata      =  1;
159   s->range_table  =& range_digital;
160   s->insn_bits    =  dnp_dio_insn_bits;
161   s->insn_config  =  dnp_dio_insn_config;
162
163   printk("attached\n");
164
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
167    * ourselves. */
168
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);
173
174   return 1;
175
176 }
177
178
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 /* ------------------------------------------------------------------------- */
186
187 static int dnp_detach(comedi_device *dev)
188 {
189
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);
194
195   /* announce that we are finished                                           */
196   printk("comedi%d: dnp: remove\n",dev->minor);
197
198   return 0;
199
200 }
201
202
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 /* ------------------------------------------------------------------------- */
208
209 static int dnp_dio_insn_bits(
210   comedi_device    *dev,
211   comedi_subdevice *s,
212   comedi_insn      *insn,
213   lsampl_t         *data)
214 {
215
216   if (insn->n!=2) return -EINVAL;       /* insn uses data[0] and data[1]     */
217
218   /* The insn data is a mask in data[0] and the new data in data[1], each    */
219   /* channel cooresponding to a bit.                                         */
220
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).                               */
224
225   if(data[0]){
226
227     outb(PADR,CSCIR);
228     outb(
229       (inb(CSCDR)
230       & ~(u8)(data[0] & 0x0000FF))
231       |  (u8)(data[1] & 0x0000FF),
232       CSCDR
233     );
234
235     outb(PBDR,CSCIR);
236     outb(
237       (inb(CSCDR)
238       & ~(u8)((data[0] & 0x00FF00) >> 8))
239       |  (u8)((data[1] & 0x00FF00) >> 8),
240       CSCDR
241     );
242
243     outb(PCDR,CSCIR);
244     outb(
245       (inb(CSCDR)
246       & ~(u8)((data[0] & 0x0F0000) >> 12))
247       |  (u8)((data[1] & 0x0F0000) >> 12),
248       CSCDR
249     );
250   }
251
252   /* on return, data[1] contains the value of the digital input lines.       */
253   outb(PADR,CSCIR);
254   data[0] = inb(CSCDR);
255   outb(PBDR,CSCIR);
256   data[0] += inb(CSCDR) << 8;
257   outb(PCDR,CSCIR);
258   data[0] += ((inb(CSCDR) & 0xF0) << 12);
259
260   return 2;
261
262 }
263
264
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 /* ------------------------------------------------------------------------- */
270
271 static int dnp_dio_insn_config(
272   comedi_device    *dev,
273   comedi_subdevice *s,
274   comedi_insn      *insn,
275   lsampl_t         *data
276 )
277 {
278
279   u8 register_buffer;
280
281   int chan=CR_CHAN(insn->chanspec);     /* reduces chanspec to lower 16 bits */
282
283   switch(data[0])
284   {
285   case INSN_CONFIG_DIO_OUTPUT:
286   case INSN_CONFIG_DIO_INPUT:
287     break;
288   case INSN_CONFIG_DIO_QUERY:
289     data[1] = (inb(CSCDR) & (1 << chan)) ? COMEDI_OUTPUT : COMEDI_INPUT;
290     return insn->n;
291     break;
292   default:
293     return -EINVAL;
294     break;
295   }
296   /* Test: which port does the channel belong to?                            */
297
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              */
301
302   if      ((chan >=  0) && (chan <=  7)) {
303     /* this is port A */
304     outb(PAMR,CSCIR);
305   }
306   else if ((chan >=  8) && (chan <= 15)) {
307     /* this is port B */
308     chan -= 8;
309     outb(PBMR,CSCIR);
310   }
311   else if ((chan >= 16) && (chan <= 19)) {
312     /* this is port C; multiplication with 2 brings bits into correct        */
313     /* position for PCMR!                                                    */
314     chan -= 16;
315     chan *= 2;
316     outb(PCMR,CSCIR);
317   }
318   else { return -EINVAL; }
319
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);
325
326   return 1;
327
328 }
329