Constified ranges, board structures, and miscellaneous data.
[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         const char *name;
56         unsigned 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         const comedi_lrange *range;
65 };
66 static const 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 ((const 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[0].name,
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         unsigned long iobase;
115         unsigned int iosize;
116
117         iobase = it->options[0];
118         printk("comedi%d: poc: using %s iobase 0x%lx\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(!request_region(iobase, iosize, "dac02"))
132         {
133                 printk("I/O port conflict: failed to allocate ports 0x%lx to 0x%lx\n",
134                         iobase, iobase + iosize - 1);
135                 return -EIO;
136         }
137         dev->iobase = iobase;
138
139         if(alloc_subdevices(dev, 1) < 0)
140                 return -ENOMEM;
141         if(alloc_private(dev,sizeof(lsampl_t)*this_board->n_chan) < 0)
142                 return -ENOMEM;
143
144         /* analog output subdevice */
145         s=dev->subdevices + 0;
146         s->type = this_board->type;
147         s->n_chan = this_board->n_chan;
148         s->maxdata = (1<<this_board->n_bits)-1;
149         s->range_table = this_board->range;
150         s->insn_write = this_board->winsn;
151         s->insn_read = this_board->rinsn;
152         s->insn_bits = this_board->insnbits;
153         if(s->type==COMEDI_SUBD_AO || s->type==COMEDI_SUBD_DO){
154                 s->subdev_flags = SDF_WRITABLE;
155         }
156
157         return 0;
158 }
159
160 static int poc_detach(comedi_device *dev)
161 {
162         /* only free stuff if it has been allocated by _attach */
163         if(dev->iobase)
164                 release_region(dev->iobase, this_board->iosize);
165
166         printk("comedi%d: dac02: remove\n", dev->minor);
167
168         return 0;
169 }
170
171 static int readback_insn(comedi_device *dev,comedi_subdevice *s,comedi_insn *insn,lsampl_t *data)
172 {
173         int chan;
174
175         chan = CR_CHAN(insn->chanspec);
176         data[0]=((lsampl_t *)dev->private)[chan];
177
178         return 1;
179 }
180
181 /* DAC-02 registers */
182 #define DAC02_LSB(a)    (2 * a)
183 #define DAC02_MSB(a)    (2 * a + 1)
184
185 static int dac02_ao_winsn(comedi_device *dev,comedi_subdevice *s,comedi_insn *insn,lsampl_t *data)
186 {
187         int temp;
188         int chan;
189         int output;
190
191         chan = CR_CHAN(insn->chanspec);
192         ((lsampl_t *)dev->private)[chan] = data[0];
193         output = data[0];
194 #ifdef wrong
195         // convert to complementary binary if range is bipolar
196         if((CR_RANGE(insn->chanspec) & 0x2) == 0)
197                 output = ~output;
198 #endif
199         temp = (output << 4) & 0xf0;
200         outb(temp, dev->iobase + DAC02_LSB(chan));
201         temp = (output >> 4) & 0xff;
202         outb(temp, dev->iobase + DAC02_MSB(chan));
203
204         return 1;
205 }
206
207 static int pcl733_insn_bits(comedi_device *dev,comedi_subdevice *s,
208         comedi_insn *insn,lsampl_t *data)
209 {
210         if(insn->n!=2)return -EINVAL;
211
212         data[1] = inb(dev->iobase + 0);
213         data[1] |= (inb(dev->iobase + 1) << 8);
214         data[1] |= (inb(dev->iobase + 2) << 16);
215         data[1] |= (inb(dev->iobase + 3) << 24);
216
217         return 2;
218 }
219
220 static int pcl734_insn_bits(comedi_device *dev,comedi_subdevice *s,
221         comedi_insn *insn,lsampl_t *data)
222 {
223         if(insn->n!=2)return -EINVAL;
224         if(data[0]){
225                 s->state &= ~data[0];
226                 s->state |= (data[0]&data[1]);
227                 if((data[0]>>0)&0xff)
228                         outb((s->state>>0)&0xff, dev->iobase + 0);
229                 if((data[0]>>8)&0xff)
230                         outb((s->state>>8)&0xff, dev->iobase + 1);
231                 if((data[0]>>16)&0xff)
232                         outb((s->state>>16)&0xff, dev->iobase + 2);
233                 if((data[0]>>24)&0xff)
234                         outb((s->state>>24)&0xff, dev->iobase + 3);
235         }
236         data[1] = s->state;
237
238         return 2;
239 }
240
241 COMEDI_INITCLEANUP(driver_poc);
242