Huge monster patch that removes unnessary headers
[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@uiuc.edu>
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 Device names: dac02
25 Author: ds
26 Devices: [Keithley Metrabyte] DAC-02 (dac02), [Advantech] PCL-733 (pcl733),
27   PCL-734 (pcl734)
28 Updated: Sat, 16 Mar 2002 17:34:48 -0800
29 Status: unknown
30
31 This driver is indended to support very simple ISA-based devices,
32 including:
33   dac02 - Keithley DAC-02 analog output board
34   pcl733 - Advantech PCL-733
35   pcl734 - Advantech PCL-734
36
37 Configuration options:
38   [0] - I/O port base
39 */
40
41 #include <linux/comedidev.h>
42
43 #include <linux/ioport.h>
44
45 static int poc_attach(comedi_device *dev,comedi_devconfig *it);
46 static int poc_detach(comedi_device *dev);
47 static int readback_insn(comedi_device *dev,comedi_subdevice *s,comedi_insn *insn,lsampl_t *data);
48
49 static int dac02_ao_winsn(comedi_device *dev,comedi_subdevice *s,comedi_insn *insn,lsampl_t *data);
50 static int pcl733_insn_bits(comedi_device *dev,comedi_subdevice *s,
51         comedi_insn *insn,lsampl_t *data);
52 static int pcl734_insn_bits(comedi_device *dev,comedi_subdevice *s,
53         comedi_insn *insn,lsampl_t *data);
54
55 struct boarddef_struct{
56         char *name;
57         int iosize;
58         int (*setup)(comedi_device *);
59         int type;
60         int n_chan;
61         int n_bits;
62         int (*winsn)(comedi_device *,comedi_subdevice *,comedi_insn *,lsampl_t *);
63         int (*rinsn)(comedi_device *,comedi_subdevice *,comedi_insn *,lsampl_t *);
64         int (*insnbits)(comedi_device *,comedi_subdevice *,comedi_insn *,lsampl_t *);
65         comedi_lrange* range;
66 };
67 static struct boarddef_struct boards[]={
68         {
69         name:           "dac02",
70         iosize:         8,
71         //setup:                dac02_setup,
72         type:           COMEDI_SUBD_AO,
73         n_chan:         2,
74         n_bits:         12,
75         winsn:          dac02_ao_winsn,
76         rinsn:          readback_insn,
77         range:          &range_unknown,
78         },
79         {
80         name:           "pcl733",
81         iosize:         4,
82         type:           COMEDI_SUBD_DI,
83         n_chan:         32,
84         n_bits:         1,
85         insnbits:       pcl733_insn_bits,
86         range:          &range_digital,
87         },
88         {
89         name:           "pcl734",
90         iosize:         4,
91         type:           COMEDI_SUBD_DO,
92         n_chan:         32,
93         n_bits:         1,
94         insnbits:       pcl734_insn_bits,
95         range:          &range_digital,
96         },
97 };
98 #define n_boards (sizeof(boards)/sizeof(boards[0]))
99 #define this_board ((struct boarddef_struct *)dev->board_ptr)
100
101 static comedi_driver driver_poc=
102 {
103         driver_name:    "poc",
104         module:         THIS_MODULE,
105         attach:         poc_attach,
106         detach:         poc_detach,
107         board_name:     boards,
108         num_names:      n_boards,
109         offset:         sizeof(boards[0]),
110 };
111
112 static int poc_attach(comedi_device *dev, comedi_devconfig *it)
113 {
114         comedi_subdevice *s;
115         int iosize, iobase;
116
117         iobase = it->options[0];
118         printk("comedi%d: poc: using %s iobase 0x%x\n", dev->minor,
119                 this_board->name, iobase);
120
121         dev->board_name = this_board->name;
122
123         if(iobase == 0)
124         {
125                 printk("io base address required\n");
126                 return -EINVAL;
127         }
128
129         iosize = this_board->iosize;
130         /* check if io addresses are available */
131         if(check_region(iobase, iosize) < 0)
132         {
133                 printk("I/O port conflict: failed to allocate ports 0x%x to 0x%x\n",
134                         iobase, iobase + iosize - 1);
135                 return -EIO;
136         }
137         request_region(iobase, iosize, "dac02");
138         dev->iobase = iobase;
139
140         dev->n_subdevices = 1;
141         if(alloc_subdevices(dev) < 0)
142                 return -ENOMEM;
143         if(alloc_private(dev,sizeof(lsampl_t)*this_board->n_chan) < 0)
144                 return -ENOMEM;
145
146         /* analog output subdevice */
147         s=dev->subdevices + 0;
148         s->type = this_board->type;
149         s->n_chan = this_board->n_chan;
150         s->maxdata = (1<<this_board->n_bits)-1;
151         s->range_table = this_board->range;
152         s->insn_write = this_board->winsn;
153         s->insn_read = this_board->rinsn;
154         s->insn_bits = this_board->insnbits;
155         if(s->type==COMEDI_SUBD_AO || s->type==COMEDI_SUBD_DO){
156                 s->subdev_flags = SDF_WRITABLE;
157         }
158
159         return 0;
160 }
161
162 static int poc_detach(comedi_device *dev)
163 {
164         /* only free stuff if it has been allocated by _attach */
165         if(dev->iobase)
166                 release_region(dev->iobase, this_board->iosize);
167
168         printk("comedi%d: dac02: remove\n", dev->minor);
169
170         return 0;
171 }
172
173 static int readback_insn(comedi_device *dev,comedi_subdevice *s,comedi_insn *insn,lsampl_t *data)
174 {
175         int chan;
176
177         chan = CR_CHAN(insn->chanspec);
178         data[0]=((lsampl_t *)dev->private)[chan];
179
180         return 1;
181 }
182
183 /* DAC-02 registers */
184 #define DAC02_LSB(a)    (2 * a)
185 #define DAC02_MSB(a)    (2 * a + 1)
186
187 static int dac02_ao_winsn(comedi_device *dev,comedi_subdevice *s,comedi_insn *insn,lsampl_t *data)
188 {
189         int temp;
190         int chan;
191         int output;
192
193         chan = CR_CHAN(insn->chanspec);
194         ((lsampl_t *)dev->private)[chan] = data[0];
195         output = data[0];
196 #if wrong
197         // convert to complementary binary if range is bipolar
198         if((CR_RANGE(insn->chanspec) & 0x2) == 0)
199                 output = ~output;
200 #endif
201         temp = (output << 4) & 0xf0;
202         outb(temp, dev->iobase + DAC02_LSB(chan));
203         temp = (output >> 4) & 0xff;
204         outb(temp, dev->iobase + DAC02_MSB(chan));
205
206         return 1;
207 }
208
209 static int pcl733_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 + 0);
215         data[1] |= (inb(dev->iobase + 1) << 8);
216         data[1] |= (inb(dev->iobase + 2) << 16);
217         data[1] |= (inb(dev->iobase + 3) << 24);
218
219         return 2;
220 }
221
222 static int pcl734_insn_bits(comedi_device *dev,comedi_subdevice *s,
223         comedi_insn *insn,lsampl_t *data)
224 {
225         if(insn->n!=2)return -EINVAL;
226         if(data[0]){
227                 s->state &= ~data[0];
228                 s->state |= (data[0]&data[1]);
229                 if((data[0]>>0)&0xff)
230                         outb((s->state>>0)&0xff, dev->iobase + 0);
231                 if((data[0]>>8)&0xff)
232                         outb((s->state>>8)&0xff, dev->iobase + 1);
233                 if((data[0]>>16)&0xff)
234                         outb((s->state>>16)&0xff, dev->iobase + 2);
235                 if((data[0]>>24)&0xff)
236                         outb((s->state>>24)&0xff, dev->iobase + 3);
237         }
238         data[1] = s->state;
239
240         return 2;
241 }
242
243 COMEDI_INITCLEANUP(driver_poc);
244