converted all drivers to use comedi_recognize() with name info
[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  * This driver is a kind of miscellaneous driver for very
23  * simple ISA boards.  
24  */
25 /*
26     dac02 - Keithley DAC-02 analog output board driver
27
28 The card's ranges are set by the wiring you use.  The driver only
29 uses the range specified to decide whether or not to take the
30 complement of the data before sending it to the card (since the
31 bipolar outputs of the card are inverted.)
32
33 */
34
35 #include <linux/kernel.h>
36 #include <linux/module.h>
37 #include <linux/sched.h>
38 #include <linux/mm.h>
39 #include <linux/malloc.h>
40 #include <linux/errno.h>
41 #include <linux/ioport.h>
42 #include <linux/delay.h>
43 #include <linux/interrupt.h>
44 #include <linux/timex.h>
45 #include <linux/timer.h>
46 #include <asm/io.h>
47 #include <linux/comedidev.h>
48
49 /* DAC-02 registers */
50 #define DAC02_LSB(a)    (2 * a)
51 #define DAC02_MSB(a)    (2 * a + 1)
52
53 static int poc_attach(comedi_device *dev,comedi_devconfig *it);
54 static int poc_detach(comedi_device *dev);
55 static int dac02_ao_winsn(comedi_device *dev,comedi_subdevice *s,comedi_insn *insn,lsampl_t *data);
56 static int readback_insn(comedi_device *dev,comedi_subdevice *s,comedi_insn *insn,lsampl_t *data);
57
58 struct boarddef_struct{
59         char *name;
60         int iosize;
61         int (*setup)(comedi_device *);
62         int type;
63         int n_chan;
64         int n_bits;
65         int (*winsn)(comedi_device *,comedi_subdevice *,comedi_insn *,lsampl_t *);
66         int (*rinsn)(comedi_device *,comedi_subdevice *,comedi_insn *,lsampl_t *);
67 };
68 static struct boarddef_struct boards[]={
69         {
70         name:           "dac02",
71         iosize:         8,
72         //setup:                dac02_setup,
73         type:           COMEDI_SUBD_AO,
74         n_chan:         2,
75         n_bits:         12,
76         winsn:          dac02_ao_winsn,
77         rinsn:          readback_insn,
78         }
79 };
80 #define n_boards (sizeof(boards)/sizeof(boards[0]))
81 #define this_board ((struct boarddef_struct *)dev->board_ptr)
82
83 comedi_driver driver_poc=
84 {
85         driver_name:    "poc",
86         module:         THIS_MODULE,
87         attach:         poc_attach,
88         detach:         poc_detach,
89         board_name:     boards,
90         num_names:      n_boards,
91         offset:         sizeof(boards[0]),
92 };
93
94 // analog output ranges
95 static comedi_lrange range_dac02 = {
96         4,
97         {
98                 RANGE( -5, 5 ),
99                 RANGE( -10, 10 ),
100                 RANGE( 0, 5 ),
101                 RANGE( 0, 10 ),
102         }
103 };
104
105 static int poc_attach(comedi_device *dev, comedi_devconfig *it)
106 {
107         comedi_subdevice *s;
108         int iosize, iobase;
109
110         iobase = it->options[0];
111         printk("comedi%d: poc: using %s iobase 0x%x\n", dev->minor,
112                 this_board->name, iobase);
113
114         dev->board_name = this_board->name;
115
116         if(iobase == 0)
117         {
118                 printk("io base address required\n");
119                 return -EINVAL;
120         }
121
122         iosize = this_board->iosize;
123         /* check if io addresses are available */
124         if(check_region(iobase, iosize) < 0)
125         {
126                 printk("I/O port conflict: failed to allocate ports 0x%x to 0x%x\n",
127                         iobase, iobase + iosize - 1);
128                 return -EIO;
129         }
130         request_region(iobase, iosize, "dac02");
131         dev->iobase = iobase;
132         dev->iosize = iosize;
133
134         dev->n_subdevices = 1;
135         if(alloc_subdevices(dev) < 0)
136                 return -ENOMEM;
137         if(alloc_private(dev,sizeof(lsampl_t)*this_board->n_chan) < 0)
138                 return -ENOMEM;
139
140         /* analog output subdevice */
141         s=dev->subdevices + 0;
142         s->type = this_board->type;
143         s->n_chan = this_board->n_chan;
144         s->maxdata = (1<<this_board->n_bits)-1;
145         s->range_table = &range_dac02; // XXX
146         s->insn_write = this_board->winsn;
147         s->insn_write = this_board->rinsn;
148         if(s->type==COMEDI_SUBD_AO || s->type==COMEDI_SUBD_DO){
149                 s->subdev_flags = SDF_WRITEABLE;
150         }
151
152         return 0;
153 }
154
155 static int poc_detach(comedi_device *dev)
156 {
157         /* only free stuff if it has been allocated by _attach */
158         if(dev->iobase)
159                 release_region(dev->iobase, dev->iosize);
160
161         printk("comedi%d: dac02: remove\n", dev->minor);
162
163         return 0;
164 }
165
166 static int readback_insn(comedi_device *dev,comedi_subdevice *s,comedi_insn *insn,lsampl_t *data)
167 {
168         int chan;
169
170         chan = CR_CHAN(insn->chanspec);
171         data[0]=((lsampl_t *)dev->private)[chan];
172
173         return 1;
174 }
175
176 static int dac02_ao_winsn(comedi_device *dev,comedi_subdevice *s,comedi_insn *insn,lsampl_t *data)
177 {
178         int temp;
179         int chan;
180         int output;
181
182         chan = CR_CHAN(insn->chanspec);
183         ((lsampl_t *)dev->private)[chan] = data[0];
184         output = data[0];
185 #if wrong
186         // convert to complementary binary if range is bipolar
187         if((CR_RANGE(insn->chanspec) & 0x2) == 0)
188                 output = ~output;
189 #endif
190         temp = (output << 4) & 0xf0;
191         outb(temp, dev->iobase + DAC02_LSB(chan));
192         temp = (output >> 4) & 0xff;
193         outb(temp, dev->iobase + DAC02_MSB(chan));
194
195         return 1;
196 }
197
198 COMEDI_INITCLEANUP(driver_poc);
199