Got rid of unnecessary casts when initializing comedi_driver.board_name
[comedi.git] / comedi / drivers / pcl726.c
1 /*
2     comedi/drivers/pcl726.c
3
4     hardware driver for Advantech cards:
5      card:   PCL-726, PCL-727, PCL-728
6      driver: pcl726,  pcl727,  pcl728
7     and for ADLink cards:
8      card:   ACL-6126, ACL-6128
9      driver: acl6126,  acl6128
10
11     COMEDI - Linux Control and Measurement Device Interface
12     Copyright (C) 1998 David A. Schleef <ds@schleef.org>
13
14     This program is free software; you can redistribute it and/or modify
15     it under the terms of the GNU General Public License as published by
16     the Free Software Foundation; either version 2 of the License, or
17     (at your option) any later version.
18
19     This program is distributed in the hope that it will be useful,
20     but WITHOUT ANY WARRANTY; without even the implied warranty of
21     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22     GNU General Public License for more details.
23
24     You should have received a copy of the GNU General Public License
25     along with this program; if not, write to the Free Software
26     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27
28 */
29 /*
30 Driver: pcl726.o
31 Description: Advantech PCL-726 & compatibles
32 Author: ds
33 Status: untested
34 Devices: [Advantech] PCL-726 (pcl726), PCL-727 (pcl727), PCL-728 (pcl728),
35   [ADLink] ACL-6126 (acl6126), ACL-6128 (acl6128)
36
37 Interrupts are not supported.
38
39     Options for PCL-726:
40      [0] - IO Base
41      [2]...[7] - D/A output range for channel 1-6:
42                0: 0-5V, 1: 0-10V, 2: +/-5V, 3: +/-10V,
43                4: 4-20mA, 5: unknown (external reference)
44
45     Options for PCL-727:
46      [0] - IO Base
47      [2]...[13] - D/A output range for channel 1-12:
48                0: 0-5V, 1: 0-10V, 2: +/-5V,
49                3: 4-20mA
50
51     Options for PCL-728 and ACL-6128:
52      [0] - IO Base
53      [2], [3] - D/A output range for channel 1 and 2:
54                0: 0-5V, 1: 0-10V, 2: +/-5V, 3: +/-10V,
55                4: 4-20mA, 5: 0-20mA
56
57     Options for ACL-6126:
58      [0] - IO Base
59      [1] - IRQ (0=disable, 3, 5, 6, 7, 9, 10, 11, 12, 15) (currently ignored)
60      [2]...[7] - D/A output range for channel 1-6:
61                0: 0-5V, 1: 0-10V, 2: +/-5V, 3: +/-10V,
62                4: 4-20mA
63 */
64
65 /*
66     Thanks to Circuit Specialists for having programming info (!) on
67     their web page.  (http://www.cir.com/)
68 */
69
70 #include <linux/comedidev.h>
71
72 #include <linux/ioport.h>
73
74
75 #undef ACL6126_IRQ      /* no interrupt support (yet) */
76
77
78 #define PCL726_SIZE 16
79 #define PCL727_SIZE 32
80 #define PCL728_SIZE 8
81
82 #define PCL726_DAC0_HI 0
83 #define PCL726_DAC0_LO 1
84
85 #define PCL726_DO_HI 12
86 #define PCL726_DO_LO 13
87 #define PCL726_DI_HI 14
88 #define PCL726_DI_LO 15
89
90 #define PCL727_DO_HI 24
91 #define PCL727_DO_LO 25
92 #define PCL727_DI_HI  0
93 #define PCL727_DI_LO  1
94
95 static comedi_lrange range_4_20mA={ 1, {RANGE_mA(4,20)}};
96 static comedi_lrange range_0_20mA={ 1, {RANGE_mA(0,20)}};
97
98 static comedi_lrange *rangelist_726[]={
99         &range_unipolar5, &range_unipolar10,
100         &range_bipolar5,  &range_bipolar10,
101         &range_4_20mA,    &range_unknown
102 };
103
104 static comedi_lrange *rangelist_727[]={
105         &range_unipolar5, &range_unipolar10,
106         &range_bipolar5,
107         &range_4_20mA
108 };
109
110 static comedi_lrange *rangelist_728[]={
111         &range_unipolar5, &range_unipolar10,
112         &range_bipolar5,  &range_bipolar10,
113         &range_4_20mA,    &range_0_20mA
114 };
115
116 static int pcl726_attach(comedi_device *dev,comedi_devconfig *it);
117 static int pcl726_detach(comedi_device *dev);
118
119 typedef struct {
120         const char              *name;          // driver name
121         int             n_aochan;       // num of D/A chans
122         int             num_of_ranges;  // num of ranges
123         unsigned int    IRQbits;        // allowed interrupts
124         unsigned int    io_range;       // len of IO space
125         char            have_dio;       // 1=card have DI/DO ports
126         int             di_hi;          // ports for DI/DO operations
127         int             di_lo;
128         int             do_hi;
129         int             do_lo;
130         comedi_lrange   **range_type_list;// list of supported ranges
131 } boardtype;
132
133 static boardtype boardtypes[] =
134 {
135         {"pcl726",   6, 6, 0x0000, PCL726_SIZE, 1,
136          PCL726_DI_HI, PCL726_DI_LO, PCL726_DO_HI, PCL726_DO_LO,
137          &rangelist_726[0], },
138         {"pcl727",  12, 4, 0x0000, PCL727_SIZE, 1,
139          PCL727_DI_HI, PCL727_DI_LO, PCL727_DO_HI, PCL727_DO_LO,
140          &rangelist_727[0], },
141         {"pcl728",   2, 6, 0x0000, PCL728_SIZE, 0,
142          0, 0, 0, 0,
143          &rangelist_728[0], },
144         {"acl6126",  6, 5, 0x96e8, PCL726_SIZE, 1,
145          PCL726_DI_HI, PCL726_DI_LO, PCL726_DO_HI, PCL726_DO_LO,
146          &rangelist_726[0], },
147         {"acl6128",  2, 6, 0x0000, PCL728_SIZE, 0,
148          0, 0, 0, 0,
149          &rangelist_728[0], },
150 };
151 #define n_boardtypes (sizeof(boardtypes)/sizeof(boardtype))
152 #define this_board ((boardtype *)dev->board_ptr)
153
154 static comedi_driver driver_pcl726={
155         driver_name:    "pcl726",
156         module:         THIS_MODULE,
157         attach:         pcl726_attach,
158         detach:         pcl726_detach,
159         board_name:     &boardtypes[0].name,
160         num_names:      n_boardtypes,
161         offset:         sizeof(boardtype),
162 };
163 COMEDI_INITCLEANUP(driver_pcl726);
164
165 typedef struct{
166         int bipolar[12];
167         comedi_lrange *rangelist[12];
168         lsampl_t ao_readback[12];
169 }pcl726_private;
170 #define devpriv ((pcl726_private *)dev->private)
171
172
173 static int pcl726_ao_insn(comedi_device *dev,comedi_subdevice *s,
174         comedi_insn *insn,lsampl_t *data)
175 {
176         int hi,lo;
177         int n;
178         int chan=CR_CHAN(insn->chanspec);
179
180         for(n=0;n<insn->n;n++){
181                 lo=data[n]&0xff;
182                 hi=(data[n]>>8)&0xf;
183                 if(devpriv->bipolar[chan])hi^=0x8;
184                 /*
185                  * the programming info did not say which order
186                  * to write bytes.  switch the order of the next
187                  * two lines if you get glitches.
188                  */
189                 outb(hi,dev->iobase+PCL726_DAC0_HI + 2*chan);
190                 outb(lo,dev->iobase+PCL726_DAC0_LO + 2*chan);
191                 devpriv->ao_readback[chan]=data[n];
192         }
193
194         return n;
195 }
196
197 static int pcl726_ao_insn_read(comedi_device *dev,comedi_subdevice *s,
198         comedi_insn *insn,lsampl_t *data)
199 {
200         int chan=CR_CHAN(insn->chanspec);
201         int n;
202
203         for(n=0;n<insn->n;n++){
204                 data[n]=devpriv->ao_readback[chan];
205         }
206         return n;
207 }
208
209 static int pcl726_di_insn_bits(comedi_device *dev,comedi_subdevice *s,
210         comedi_insn *insn,lsampl_t *data)
211 {
212         if(insn->n!=2)return -EINVAL;
213
214         data[1]=inb(dev->iobase+this_board->di_lo)|
215                 (inb(dev->iobase+this_board->di_hi)<<8);
216
217         return 2;
218 }
219
220 static int pcl726_do_insn_bits(comedi_device *dev,comedi_subdevice *s,
221         comedi_insn *insn,lsampl_t *data)
222 {
223         if(insn->n!=2)return -EINVAL;
224
225         if(data[0]){
226                 s->state &= ~data[0];
227                 s->state |= data[0]&data[1];
228         }
229         if(data[1]&0x00ff)
230                 outb(s->state&0xff,dev->iobase+this_board->do_lo);
231         if(data[1]&0xff00)
232                 outb((s->state>>8),dev->iobase+this_board->do_hi);
233
234         data[1] = s->state;
235
236         return 2;
237 }
238
239 static int pcl726_attach(comedi_device *dev,comedi_devconfig *it)
240 {
241         comedi_subdevice *s;
242         unsigned long iobase;
243         unsigned int iorange;
244         int ret,i;
245 #ifdef ACL6126_IRQ
246         unsigned int irq;
247 #endif
248
249         iobase=it->options[0];
250         iorange=this_board->io_range;
251         printk("comedi%d: pcl726: board=%s, 0x%03lx ",dev->minor,this_board->name,iobase);
252         if(!request_region(iobase, iorange, "pcl726")){
253                 printk("I/O port conflict\n");
254                 return -EIO;
255         }
256
257         dev->iobase=iobase;
258
259         dev->board_name = this_board->name;
260
261         if((ret=alloc_private(dev,sizeof(pcl726_private)))<0)
262                 return -ENOMEM;
263
264         for (i=0; i<12; i++) {
265                 devpriv->bipolar[i]=0;
266                 devpriv->rangelist[i]=&range_unknown;
267         }
268
269 #ifdef ACL6126_IRQ
270         irq=0;
271         if (boardtypes[board].IRQbits!=0) { /* board support IRQ */
272                 irq=it->options[1];
273                 devpriv->first_chan=2;
274                 if (irq)  {/* we want to use IRQ */
275                         if (((1<<irq)&boardtypes[board].IRQbits)==0) {
276                                 rt_printk(", IRQ %d is out of allowed range, DISABLING IT",irq);
277                                 irq=0; /* Bad IRQ */
278                         } else {
279                                 if (comedi_request_irq(irq, interrupt_pcl818, 0, "pcl726", dev)) {
280                                         rt_printk(", unable to allocate IRQ %d, DISABLING IT", irq);
281                                         irq=0; /* Can't use IRQ */
282                                 } else {
283                                         rt_printk(", irq=%d", irq);
284                                 }
285                         }
286                 }
287         }
288
289         dev->irq = irq;
290 #endif
291
292         printk("\n");
293
294         if((ret=alloc_subdevices(dev, 3))<0)
295                 return ret;
296
297         s=dev->subdevices+0;
298         /* ao */
299         s->type=COMEDI_SUBD_AO;
300         s->subdev_flags=SDF_WRITABLE|SDF_GROUND;
301         s->n_chan=this_board->n_aochan;
302         s->maxdata=0xfff;
303         s->len_chanlist=1;
304         s->insn_write=pcl726_ao_insn;
305         s->insn_read=pcl726_ao_insn_read;
306         s->range_table_list = devpriv->rangelist;
307         for (i=0; i<this_board->n_aochan; i++) {
308                 int j;
309
310                 j = it->options[2+1];
311                 if ((j<0)||(j>=this_board->num_of_ranges)) {
312                         printk("Invalid range for channel %d! Must be 0<=%d<%d\n",
313                                 i,j,this_board->num_of_ranges-1);
314                         j=0;
315                 }
316                 devpriv->rangelist[i]=this_board->range_type_list[j];
317                 if (devpriv->rangelist[i]->range[0].min==-devpriv->rangelist[i]->range[0].max)
318                         devpriv->bipolar[i]=1;  /* bipolar range */
319         }
320
321         s=dev->subdevices+1;
322         /* di */
323         if (!this_board->have_dio){
324                 s->type = COMEDI_SUBD_UNUSED;
325         }else{
326                 s->type=COMEDI_SUBD_DI;
327                 s->subdev_flags=SDF_READABLE|SDF_GROUND;
328                 s->n_chan=16;
329                 s->maxdata=1;
330                 s->len_chanlist=1;
331                 s->insn_bits=pcl726_di_insn_bits;
332                 s->range_table=&range_digital;
333         }
334
335         s=dev->subdevices+2;
336         /* do */
337         if (!this_board->have_dio){
338                 s->type = COMEDI_SUBD_UNUSED;
339         }else{
340                 s->type=COMEDI_SUBD_DO;
341                 s->subdev_flags=SDF_WRITABLE|SDF_GROUND;
342                 s->n_chan=16;
343                 s->maxdata=1;
344                 s->len_chanlist=1;
345                 s->insn_bits=pcl726_do_insn_bits;
346                 s->range_table=&range_digital;
347         }
348
349         return 0;
350 }
351
352
353 static int pcl726_detach(comedi_device *dev)
354 {
355 //      printk("comedi%d: pcl726: remove\n",dev->minor);
356
357 #ifdef ACL6126_IRQ
358         if(dev->irq){
359                 comedi_free_irq(dev->irq,dev);
360         }
361 #endif
362
363         if(dev->iobase)
364                 release_region(dev->iobase,this_board->io_range);
365
366         return 0;
367 }
368
369