Staging: comedi: Give the addi_apci_* drivers different driver names
[safe/jmp/linux-2.6] / drivers / staging / comedi / drivers / fl512.c
1 /*
2     comedi/drivers/fl512.c
3     Anders Gnistrup <ex18@kalman.iau.dtu.dk>
4 */
5
6 /*
7 Driver: fl512
8 Description: unknown
9 Author: Anders Gnistrup <ex18@kalman.iau.dtu.dk>
10 Devices: [unknown] FL512 (fl512)
11 Status: unknown
12
13 Digital I/O is not supported.
14
15 Configuration options:
16   [0] - I/O port base address
17 */
18
19 #define DEBUG 0
20
21 #include "../comedidev.h"
22
23 #include <linux/delay.h>
24 #include <linux/ioport.h>
25
26 #define FL512_SIZE 16           /* the size of the used memory */
27 struct fl512_private {
28
29         short ao_readback[2];
30 };
31
32 #define devpriv ((struct fl512_private *) dev->private)
33
34 static const struct comedi_lrange range_fl512 = { 4, {
35                                                       BIP_RANGE(0.5),
36                                                       BIP_RANGE(1),
37                                                       BIP_RANGE(5),
38                                                       BIP_RANGE(10),
39                                                       UNI_RANGE(1),
40                                                       UNI_RANGE(5),
41                                                       UNI_RANGE(10),
42                                                       }
43 };
44
45 static int fl512_attach(struct comedi_device *dev, struct comedi_devconfig *it);
46 static int fl512_detach(struct comedi_device *dev);
47
48 static struct comedi_driver driver_fl512 = {
49         .driver_name = "fl512",
50         .module = THIS_MODULE,
51         .attach = fl512_attach,
52         .detach = fl512_detach,
53 };
54
55 COMEDI_INITCLEANUP(driver_fl512);
56
57 static int fl512_ai_insn(struct comedi_device *dev,
58                          struct comedi_subdevice *s, struct comedi_insn *insn,
59                          unsigned int *data);
60 static int fl512_ao_insn(struct comedi_device *dev, struct comedi_subdevice *s,
61                          struct comedi_insn *insn, unsigned int *data);
62 static int fl512_ao_insn_readback(struct comedi_device *dev,
63                                   struct comedi_subdevice *s,
64                                   struct comedi_insn *insn, unsigned int *data);
65
66 /*
67  * fl512_ai_insn : this is the analog input function
68  */
69 static int fl512_ai_insn(struct comedi_device *dev,
70                          struct comedi_subdevice *s, struct comedi_insn *insn,
71                          unsigned int *data)
72 {
73         int n;
74         unsigned int lo_byte, hi_byte;
75         char chan = CR_CHAN(insn->chanspec);
76         unsigned long iobase = dev->iobase;
77
78         for (n = 0; n < insn->n; n++) { /* sample n times on selected channel */
79                 /* XXX probably can move next step out of for() loop -- will
80                  * make AI a little bit faster. */
81                 outb(chan, iobase + 2); /* select chan */
82                 outb(0, iobase + 3);    /* start conversion */
83                 /* XXX should test "done" flag instead of delay */
84                 udelay(30);     /* sleep 30 usec */
85                 lo_byte = inb(iobase + 2);      /* low 8 byte */
86                 hi_byte = inb(iobase + 3) & 0xf; /* high 4 bit and mask */
87                 data[n] = lo_byte + (hi_byte << 8);
88         }
89         return n;
90 }
91
92 /*
93  * fl512_ao_insn : used to write to a DA port n times
94  */
95 static int fl512_ao_insn(struct comedi_device *dev,
96                          struct comedi_subdevice *s, struct comedi_insn *insn,
97                          unsigned int *data)
98 {
99         int n;
100         int chan = CR_CHAN(insn->chanspec);     /* get chan to write */
101         unsigned long iobase = dev->iobase;     /* get base address  */
102
103         for (n = 0; n < insn->n; n++) { /* write n data set */
104                 /* write low byte   */
105                 outb(data[n] & 0x0ff, iobase + 4 + 2 * chan);
106                 /* write high byte  */
107                 outb((data[n] & 0xf00) >> 8, iobase + 4 + 2 * chan);
108                 inb(iobase + 4 + 2 * chan);     /* trig */
109
110                 devpriv->ao_readback[chan] = data[n];
111         }
112         return n;
113 }
114
115 /*
116  * fl512_ao_insn_readback : used to read previous values written to
117  * DA port
118  */
119 static int fl512_ao_insn_readback(struct comedi_device *dev,
120                                   struct comedi_subdevice *s,
121                                   struct comedi_insn *insn, unsigned int *data)
122 {
123         int n;
124         int chan = CR_CHAN(insn->chanspec);
125
126         for (n = 0; n < insn->n; n++)
127                 data[n] = devpriv->ao_readback[chan];
128
129         return n;
130 }
131
132 /*
133  * start attach
134  */
135 static int fl512_attach(struct comedi_device *dev, struct comedi_devconfig *it)
136 {
137         unsigned long iobase;
138
139         /* pointer to the subdevice: Analog in, Analog out,
140            (not made ->and Digital IO) */
141         struct comedi_subdevice *s;
142
143         iobase = it->options[0];
144         printk(KERN_INFO "comedi:%d fl512: 0x%04lx", dev->minor, iobase);
145         if (!request_region(iobase, FL512_SIZE, "fl512")) {
146                 printk(KERN_WARNING " I/O port conflict\n");
147                 return -EIO;
148         }
149         dev->iobase = iobase;
150         dev->board_name = "fl512";
151         if (alloc_private(dev, sizeof(struct fl512_private)) < 0)
152                 return -ENOMEM;
153
154 #if DEBUG
155         printk(KERN_DEBUG "malloc ok\n");
156 #endif
157
158         if (alloc_subdevices(dev, 2) < 0)
159                 return -ENOMEM;
160
161         /*
162          * this if the definitions of the supdevices, 2 have been defined
163          */
164         /* Analog indput */
165         s = dev->subdevices + 0;
166         /* define subdevice as Analog In */
167         s->type = COMEDI_SUBD_AI;
168         /* you can read it from userspace */
169         s->subdev_flags = SDF_READABLE | SDF_GROUND;
170         /* Number of Analog input channels */
171         s->n_chan = 16;
172         /* accept only 12 bits of data */
173         s->maxdata = 0x0fff;
174         /* device use one of the ranges */
175         s->range_table = &range_fl512;
176         /* function to call when read AD */
177         s->insn_read = fl512_ai_insn;
178         printk(KERN_INFO "comedi: fl512: subdevice 0 initialized\n");
179
180         /* Analog output */
181         s = dev->subdevices + 1;
182         /* define subdevice as Analog OUT */
183         s->type = COMEDI_SUBD_AO;
184         /* you can write it from userspace */
185         s->subdev_flags = SDF_WRITABLE;
186         /* Number of Analog output channels */
187         s->n_chan = 2;
188         /* accept only 12 bits of data */
189         s->maxdata = 0x0fff;
190         /* device use one of the ranges */
191         s->range_table = &range_fl512;
192         /* function to call when write DA */
193         s->insn_write = fl512_ao_insn;
194         /* function to call when reading DA */
195         s->insn_read = fl512_ao_insn_readback;
196         printk(KERN_INFO "comedi: fl512: subdevice 1 initialized\n");
197
198         return 1;
199 }
200
201 static int fl512_detach(struct comedi_device *dev)
202 {
203         if (dev->iobase)
204                 release_region(dev->iobase, FL512_SIZE);
205         printk(KERN_INFO "comedi%d: fl512: dummy i detach\n", dev->minor);
206         return 0;
207 }