staging: comedi: drivers: propogate errno from subdev_8255_init()
[sfrench/cifs-2.6.git] / drivers / staging / comedi / drivers / cb_pcimdas.c
1 /*
2     comedi/drivers/cb_pcimdas.c
3     Comedi driver for Computer Boards PCIM-DAS1602/16
4
5     COMEDI - Linux Control and Measurement Device Interface
6     Copyright (C) 2000 David A. Schleef <ds@schleef.org>
7
8     This program is free software; you can redistribute it and/or modify
9     it under the terms of the GNU General Public License as published by
10     the Free Software Foundation; either version 2 of the License, or
11     (at your option) any later version.
12
13     This program is distributed in the hope that it will be useful,
14     but WITHOUT ANY WARRANTY; without even the implied warranty of
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16     GNU General Public License for more details.
17 */
18 /*
19 Driver: cb_pcimdas
20 Description: Measurement Computing PCI Migration series boards
21 Devices: [ComputerBoards] PCIM-DAS1602/16 (cb_pcimdas)
22 Author: Richard Bytheway
23 Updated: Wed, 13 Nov 2002 12:34:56 +0000
24 Status: experimental
25
26 Written to support the PCIM-DAS1602/16 on a 2.4 series kernel.
27
28 Configuration Options:
29     [0] - PCI bus number
30     [1] - PCI slot number
31
32 Developed from cb_pcidas and skel by Richard Bytheway (mocelet@sucs.org).
33 Only supports DIO, AO and simple AI in it's present form.
34 No interrupts, multi channel or FIFO AI, although the card looks like it could support this.
35 See http://www.mccdaq.com/PDFs/Manuals/pcim-das1602-16.pdf for more details.
36 */
37
38 #include <linux/module.h>
39 #include <linux/pci.h>
40 #include <linux/interrupt.h>
41
42 #include "../comedidev.h"
43
44 #include "plx9052.h"
45 #include "8255.h"
46
47 /* Registers for the PCIM-DAS1602/16 */
48
49 /* sizes of io regions (bytes) */
50 #define BADR3_SIZE 16
51
52 /* DAC Offsets */
53 #define ADC_TRIG 0
54 #define DAC0_OFFSET 2
55 #define DAC1_OFFSET 4
56
57 /* AI and Counter Constants */
58 #define MUX_LIMITS 0
59 #define MAIN_CONN_DIO 1
60 #define ADC_STAT 2
61 #define ADC_CONV_STAT 3
62 #define ADC_INT 4
63 #define ADC_PACER 5
64 #define BURST_MODE 6
65 #define PROG_GAIN 7
66 #define CLK8254_1_DATA 8
67 #define CLK8254_2_DATA 9
68 #define CLK8254_3_DATA 10
69 #define CLK8254_CONTROL 11
70 #define USER_COUNTER 12
71 #define RESID_COUNT_H 13
72 #define RESID_COUNT_L 14
73
74 /*
75  * this structure is for data unique to this hardware driver.  If
76  * several hardware drivers keep similar information in this structure,
77  * feel free to suggest moving the variable to the struct comedi_device
78  * struct.
79  */
80 struct cb_pcimdas_private {
81         /* base addresses */
82         unsigned long BADR3;
83
84         /* Used for AO readback */
85         unsigned int ao_readback[2];
86 };
87
88 /*
89  * "instructions" read/write data in "one-shot" or "software-triggered"
90  * mode.
91  */
92 static int cb_pcimdas_ai_rinsn(struct comedi_device *dev,
93                                struct comedi_subdevice *s,
94                                struct comedi_insn *insn, unsigned int *data)
95 {
96         struct cb_pcimdas_private *devpriv = dev->private;
97         int n, i;
98         unsigned int d;
99         unsigned int busy;
100         int chan = CR_CHAN(insn->chanspec);
101         unsigned short chanlims;
102         int maxchans;
103
104         /*  only support sw initiated reads from a single channel */
105
106         /* check channel number */
107         if ((inb(devpriv->BADR3 + 2) & 0x20) == 0)      /* differential mode */
108                 maxchans = s->n_chan / 2;
109         else
110                 maxchans = s->n_chan;
111
112         if (chan > (maxchans - 1))
113                 return -ETIMEDOUT;      /* *** Wrong error code. Fixme. */
114
115         /* configure for sw initiated read */
116         d = inb(devpriv->BADR3 + 5);
117         if ((d & 0x03) > 0) {   /* only reset if needed. */
118                 d = d & 0xfd;
119                 outb(d, devpriv->BADR3 + 5);
120         }
121         outb(0x01, devpriv->BADR3 + 6); /* set bursting off, conversions on */
122         outb(0x00, devpriv->BADR3 + 7); /* set range to 10V. UP/BP is controlled by a switch on the board */
123
124         /*
125          * write channel limits to multiplexer, set Low (bits 0-3) and
126          * High (bits 4-7) channels to chan.
127          */
128         chanlims = chan | (chan << 4);
129         outb(chanlims, devpriv->BADR3 + 0);
130
131         /* convert n samples */
132         for (n = 0; n < insn->n; n++) {
133                 /* trigger conversion */
134                 outw(0, dev->iobase + 0);
135
136 #define TIMEOUT 1000            /* typically takes 5 loops on a lightly loaded Pentium 100MHz, */
137                 /* this is likely to be 100 loops on a 2GHz machine, so set 1000 as the limit. */
138
139                 /* wait for conversion to end */
140                 for (i = 0; i < TIMEOUT; i++) {
141                         busy = inb(devpriv->BADR3 + 2) & 0x80;
142                         if (!busy)
143                                 break;
144                 }
145                 if (i == TIMEOUT)
146                         return -ETIMEDOUT;
147
148                 /* read data */
149                 data[n] = inw(dev->iobase + 0);
150         }
151
152         /* return the number of samples read/written */
153         return n;
154 }
155
156 static int cb_pcimdas_ao_winsn(struct comedi_device *dev,
157                                struct comedi_subdevice *s,
158                                struct comedi_insn *insn, unsigned int *data)
159 {
160         struct cb_pcimdas_private *devpriv = dev->private;
161         int i;
162         int chan = CR_CHAN(insn->chanspec);
163
164         /* Writing a list of values to an AO channel is probably not
165          * very useful, but that's how the interface is defined. */
166         for (i = 0; i < insn->n; i++) {
167                 switch (chan) {
168                 case 0:
169                         outw(data[i] & 0x0FFF, dev->iobase + DAC0_OFFSET);
170                         break;
171                 case 1:
172                         outw(data[i] & 0x0FFF, dev->iobase + DAC1_OFFSET);
173                         break;
174                 default:
175                         return -1;
176                 }
177                 devpriv->ao_readback[chan] = data[i];
178         }
179
180         /* return the number of samples read/written */
181         return i;
182 }
183
184 /* AO subdevices should have a read insn as well as a write insn.
185  * Usually this means copying a value stored in devpriv. */
186 static int cb_pcimdas_ao_rinsn(struct comedi_device *dev,
187                                struct comedi_subdevice *s,
188                                struct comedi_insn *insn, unsigned int *data)
189 {
190         struct cb_pcimdas_private *devpriv = dev->private;
191         int i;
192         int chan = CR_CHAN(insn->chanspec);
193
194         for (i = 0; i < insn->n; i++)
195                 data[i] = devpriv->ao_readback[chan];
196
197         return i;
198 }
199
200 static int cb_pcimdas_auto_attach(struct comedi_device *dev,
201                                             unsigned long context_unused)
202 {
203         struct pci_dev *pcidev = comedi_to_pci_dev(dev);
204         struct cb_pcimdas_private *devpriv;
205         struct comedi_subdevice *s;
206         unsigned long iobase_8255;
207         int ret;
208
209         devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
210         if (!devpriv)
211                 return -ENOMEM;
212
213         ret = comedi_pci_enable(dev);
214         if (ret)
215                 return ret;
216
217         dev->iobase = pci_resource_start(pcidev, 2);
218         devpriv->BADR3 = pci_resource_start(pcidev, 3);
219         iobase_8255 = pci_resource_start(pcidev, 4);
220
221         ret = comedi_alloc_subdevices(dev, 3);
222         if (ret)
223                 return ret;
224
225         s = &dev->subdevices[0];
226         /* dev->read_subdev=s; */
227         /*  analog input subdevice */
228         s->type = COMEDI_SUBD_AI;
229         s->subdev_flags = SDF_READABLE | SDF_GROUND;
230         s->n_chan = 16;
231         s->maxdata = 0xffff;
232         s->range_table = &range_unknown;
233         s->len_chanlist = 1;    /*  This is the maximum chanlist length that */
234         /*  the board can handle */
235         s->insn_read = cb_pcimdas_ai_rinsn;
236
237         s = &dev->subdevices[1];
238         /*  analog output subdevice */
239         s->type = COMEDI_SUBD_AO;
240         s->subdev_flags = SDF_WRITABLE;
241         s->n_chan = 2;
242         s->maxdata = 0xfff;
243         /* ranges are hardware settable, but not software readable. */
244         s->range_table = &range_unknown;
245         s->insn_write = &cb_pcimdas_ao_winsn;
246         s->insn_read = &cb_pcimdas_ao_rinsn;
247
248         s = &dev->subdevices[2];
249         /* digital i/o subdevice */
250         ret = subdev_8255_init(dev, s, NULL, iobase_8255);
251         if (ret)
252                 return ret;
253
254         dev_info(dev->class_dev, "%s attached\n", dev->board_name);
255
256         return 0;
257 }
258
259 static void cb_pcimdas_detach(struct comedi_device *dev)
260 {
261         if (dev->irq)
262                 free_irq(dev->irq, dev);
263         comedi_pci_disable(dev);
264 }
265
266 static struct comedi_driver cb_pcimdas_driver = {
267         .driver_name    = "cb_pcimdas",
268         .module         = THIS_MODULE,
269         .auto_attach    = cb_pcimdas_auto_attach,
270         .detach         = cb_pcimdas_detach,
271 };
272
273 static int cb_pcimdas_pci_probe(struct pci_dev *dev,
274                                 const struct pci_device_id *id)
275 {
276         return comedi_pci_auto_config(dev, &cb_pcimdas_driver,
277                                       id->driver_data);
278 }
279
280 static const struct pci_device_id cb_pcimdas_pci_table[] = {
281         { PCI_DEVICE(PCI_VENDOR_ID_CB, 0x0056) },
282         { 0 }
283 };
284 MODULE_DEVICE_TABLE(pci, cb_pcimdas_pci_table);
285
286 static struct pci_driver cb_pcimdas_pci_driver = {
287         .name           = "cb_pcimdas",
288         .id_table       = cb_pcimdas_pci_table,
289         .probe          = cb_pcimdas_pci_probe,
290         .remove         = comedi_pci_auto_unconfig,
291 };
292 module_comedi_pci_driver(cb_pcimdas_driver, cb_pcimdas_pci_driver);
293
294 MODULE_AUTHOR("Comedi http://www.comedi.org");
295 MODULE_DESCRIPTION("Comedi low-level driver");
296 MODULE_LICENSE("GPL");