Fix gcc-4.1 warnings (undefined macro in #if).
[comedi.git] / comedi / drivers / poc.c
1 /*
2     comedi/drivers/poc.c
3     Mini-drivers for POC (Piece of Crap) boards
4     Copyright (C) 2000 Frank Mori Hess <fmhess@users.sourceforge.net>
5     Copyright (C) 2001 David A. Schleef <ds@schleef.org>
6
7     This program is free software; you can redistribute it and/or modify
8     it under the terms of the GNU General Public License as published by
9     the Free Software Foundation; either version 2 of the License, or
10     (at your option) any later version.
11
12     This program is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15     GNU General Public License for more details.
16
17     You should have received a copy of the GNU General Public License
18     along with this program; if not, write to the Free Software
19     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21 /*
22 Driver: poc.o
23 Description: Generic driver for very simple devices
24 Author: ds
25 Devices: [Keithley Metrabyte] DAC-02 (dac02), [Advantech] PCL-733 (pcl733),
26   PCL-734 (pcl734)
27 Updated: Sat, 16 Mar 2002 17:34:48 -0800
28 Status: unknown
29
30 This driver is indended to support very simple ISA-based devices,
31 including:
32   dac02 - Keithley DAC-02 analog output board
33   pcl733 - Advantech PCL-733
34   pcl734 - Advantech PCL-734
35
36 Configuration options:
37   [0] - I/O port base
38 */
39
40 #include <linux/comedidev.h>
41
42 #include <linux/ioport.h>
43
44 static int poc_attach(comedi_device *dev,comedi_devconfig *it);
45 static int poc_detach(comedi_device *dev);
46 static int readback_insn(comedi_device *dev,comedi_subdevice *s,comedi_insn *insn,lsampl_t *data);
47
48 static int dac02_ao_winsn(comedi_device *dev,comedi_subdevice *s,comedi_insn *insn,lsampl_t *data);
49 static int pcl733_insn_bits(comedi_device *dev,comedi_subdevice *s,
50         comedi_insn *insn,lsampl_t *data);
51 static int pcl734_insn_bits(comedi_device *dev,comedi_subdevice *s,
52         comedi_insn *insn,lsampl_t *data);
53
54 struct boarddef_struct{
55         char *name;
56         int iosize;
57         int (*setup)(comedi_device *);
58         int type;
59         int n_chan;
60         int n_bits;
61         int (*winsn)(comedi_device *,comedi_subdevice *,comedi_insn *,lsampl_t *);
62         int (*rinsn)(comedi_device *,comedi_subdevice *,comedi_insn *,lsampl_t *);
63         int (*insnbits)(comedi_device *,comedi_subdevice *,comedi_insn *,lsampl_t *);
64         comedi_lrange* range;
65 };
66 static struct boarddef_struct boards[]={
67         {
68         name:           "dac02",
69         iosize:         8,
70         //setup:                dac02_setup,
71         type:           COMEDI_SUBD_AO,
72         n_chan:         2,
73         n_bits:         12,
74         winsn:          dac02_ao_winsn,
75         rinsn:          readback_insn,
76         range:          &range_unknown,
77         },
78         {
79         name:           "pcl733",
80         iosize:         4,
81         type:           COMEDI_SUBD_DI,
82         n_chan:         32,
83         n_bits:         1,
84         insnbits:       pcl733_insn_bits,
85         range:          &range_digital,
86         },
87         {
88         name:           "pcl734",
89         iosize:         4,
90         type:           COMEDI_SUBD_DO,
91         n_chan:         32,
92         n_bits:         1,
93         insnbits:       pcl734_insn_bits,
94         range:          &range_digital,
95         },
96 };
97 #define n_boards (sizeof(boards)/sizeof(boards[0]))
98 #define this_board ((struct boarddef_struct *)dev->board_ptr)
99
100 static comedi_driver driver_poc=
101 {
102         driver_name:    "poc",
103         module:         THIS_MODULE,
104         attach:         poc_attach,
105         detach:         poc_detach,
106         board_name:     boards,
107         num_names:      n_boards,
108         offset:         sizeof(boards[0]),
109 };
110
111 static int poc_attach(comedi_device *dev, comedi_devconfig *it)
112 {
113         comedi_subdevice *s;
114         int iosize, iobase;
115
116         iobase = it->options[0];
117         printk("comedi%d: poc: using %s iobase 0x%x\n", dev->minor,
118                 this_board->name, iobase);
119
120         dev->board_name = this_board->name;
121
122         if(iobase == 0)
123         {
124                 printk("io base address required\n");
125                 return -EINVAL;
126         }
127
128         iosize = this_board->iosize;
129         /* check if io addresses are available */
130         if(!request_region(iobase, iosize, "dac02"))
131         {
132                 printk("I/O port conflict: failed to allocate ports 0x%x to 0x%x\n",
133                         iobase, iobase + iosize - 1);
134                 return -EIO;
135         }
136         dev->iobase = iobase;
137
138         if(alloc_subdevices(dev, 1) < 0)
139                 return -ENOMEM;
140         if(alloc_private(dev,sizeof(lsampl_t)*this_board->n_chan) < 0)
141                 return -ENOMEM;
142
143         /* analog output subdevice */
144         s=dev->subdevices + 0;
145         s->type = this_board->type;
146         s->n_chan = this_board->n_chan;
147         s->maxdata = (1<<this_board->n_bits)-1;
148         s->range_table = this_board->range;
149         s->insn_write = this_board->winsn;
150         s->insn_read = this_board->rinsn;
151         s->insn_bits = this_board->insnbits;
152         if(s->type==COMEDI_SUBD_AO || s->type==COMEDI_SUBD_DO){
153                 s->subdev_flags = SDF_WRITABLE;
154         }
155
156         return 0;
157 }
158
159 static int poc_detach(comedi_device *dev)
160 {
161         /* only free stuff if it has been allocated by _attach */
162         if(dev->iobase)
163                 release_region(dev->iobase, this_board->iosize);
164
165         printk("comedi%d: dac02: remove\n", dev->minor);
166
167         return 0;
168 }
169
170 static int readback_insn(comedi_device *dev,comedi_subdevice *s,comedi_insn *insn,lsampl_t *data)
171 {
172         int chan;
173
174         chan = CR_CHAN(insn->chanspec);
175         data[0]=((lsampl_t *)dev->private)[chan];
176
177         return 1;
178 }
179
180 /* DAC-02 registers */
181 #define DAC02_LSB(a)    (2 * a)
182 #define DAC02_MSB(a)    (2 * a + 1)
183
184 static int dac02_ao_winsn(comedi_device *dev,comedi_subdevice *s,comedi_insn *insn,lsampl_t *data)
185 {
186         int temp;
187         int chan;
188         int output;
189
190         chan = CR_CHAN(insn->chanspec);
191         ((lsampl_t *)dev->private)[chan] = data[0];
192         output = data[0];
193 #ifdef wrong
194         // convert to complementary binary if range is bipolar
195         if((CR_RANGE(insn->chanspec) & 0x2) == 0)
196                 output = ~output;
197 #endif
198         temp = (output << 4) & 0xf0;
199         outb(temp, dev->iobase + DAC02_LSB(chan));
200         temp = (output >> 4) & 0xff;
201         outb(temp, dev->iobase + DAC02_MSB(chan));
202
203         return 1;
204 }
205
206 static int pcl733_insn_bits(comedi_device *dev,comedi_subdevice *s,
207         comedi_insn *insn,lsampl_t *data)
208 {
209         if(insn->n!=2)return -EINVAL;
210         
211         data[1] = inb(dev->iobase + 0);
212         data[1] |= (inb(dev->iobase + 1) << 8);
213         data[1] |= (inb(dev->iobase + 2) << 16);
214         data[1] |= (inb(dev->iobase + 3) << 24);
215
216         return 2;
217 }
218
219 static int pcl734_insn_bits(comedi_device *dev,comedi_subdevice *s,
220         comedi_insn *insn,lsampl_t *data)
221 {
222         if(insn->n!=2)return -EINVAL;
223         if(data[0]){
224                 s->state &= ~data[0];
225                 s->state |= (data[0]&data[1]);
226                 if((data[0]>>0)&0xff)
227                         outb((s->state>>0)&0xff, dev->iobase + 0);
228                 if((data[0]>>8)&0xff)
229                         outb((s->state>>8)&0xff, dev->iobase + 1);
230                 if((data[0]>>16)&0xff)
231                         outb((s->state>>16)&0xff, dev->iobase + 2);
232                 if((data[0]>>24)&0xff)
233                         outb((s->state>>24)&0xff, dev->iobase + 3);
234         }
235         data[1] = s->state;
236
237         return 2;
238 }
239
240 COMEDI_INITCLEANUP(driver_poc);
241