Staging: comedi: Remove hw_fifo_info_t typedef
[linux-2.6] / drivers / staging / comedi / drivers / ni_670x.c
1 /*
2     comedi/drivers/ni_670x.c
3     Hardware driver for NI 670x devices
4
5     COMEDI - Linux Control and Measurement Device Interface
6     Copyright (C) 1997-2001 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     You should have received a copy of the GNU General Public License
19     along with this program; if not, write to the Free Software
20     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21
22 */
23 /*
24 Driver: ni_670x
25 Description: National Instruments 670x
26 Author: Bart Joris <bjoris@advalvas.be>
27 Updated: Wed, 11 Dec 2002 18:25:35 -0800
28 Devices: [National Instruments] PCI-6703 (ni_670x), PCI-6704
29 Status: unknown
30
31 Commands are not supported.
32 */
33
34 /*
35         Bart Joris <bjoris@advalvas.be> Last updated on 20/08/2001
36
37         Manuals:
38
39         322110a.pdf     PCI/PXI-6704 User Manual
40         322110b.pdf     PCI/PXI-6703/6704 User Manual
41
42 */
43
44 #include "../comedidev.h"
45
46 #include "mite.h"
47
48 #define PCI_VENDOR_ID_NATINST   0x1093
49
50 #define AO_VALUE_OFFSET                 0x00
51 #define AO_CHAN_OFFSET                  0x0c
52 #define AO_STATUS_OFFSET                0x10
53 #define AO_CONTROL_OFFSET               0x10
54 #define DIO_PORT0_DIR_OFFSET    0x20
55 #define DIO_PORT0_DATA_OFFSET   0x24
56 #define DIO_PORT1_DIR_OFFSET    0x28
57 #define DIO_PORT1_DATA_OFFSET   0x2c
58 #define MISC_STATUS_OFFSET              0x14
59 #define MISC_CONTROL_OFFSET             0x14
60
61 /* Board description*/
62
63 typedef struct ni_670x_board_struct {
64         unsigned short dev_id;
65         const char *name;
66         unsigned short ao_chans;
67         unsigned short ao_bits;
68 } ni_670x_board;
69 static const ni_670x_board ni_670x_boards[] = {
70         {
71               dev_id:   0x2c90,
72               name:     "PCI-6703",
73               ao_chans:16,
74               ao_bits:  16,
75                 },
76         {
77               dev_id:   0x1920,
78               name:     "PXI-6704",
79               ao_chans:32,
80               ao_bits:  16,
81                 },
82         {
83               dev_id:   0x1290,
84               name:     "PCI-6704",
85               ao_chans:32,
86               ao_bits:  16,
87                 },
88 };
89
90 static DEFINE_PCI_DEVICE_TABLE(ni_670x_pci_table) = {
91         {PCI_VENDOR_ID_NATINST, 0x2c90, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
92         {PCI_VENDOR_ID_NATINST, 0x1920, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
93         //{ PCI_VENDOR_ID_NATINST, 0x0000, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
94         {0}
95 };
96
97 MODULE_DEVICE_TABLE(pci, ni_670x_pci_table);
98
99 #define thisboard ((ni_670x_board *)dev->board_ptr)
100
101 struct ni_670x_private {
102
103         struct mite_struct *mite;
104         int boardtype;
105         int dio;
106         unsigned int ao_readback[32];
107 };
108
109
110 #define devpriv ((struct ni_670x_private *)dev->private)
111 #define n_ni_670x_boards (sizeof(ni_670x_boards)/sizeof(ni_670x_boards[0]))
112
113 static int ni_670x_attach(struct comedi_device * dev, struct comedi_devconfig * it);
114 static int ni_670x_detach(struct comedi_device * dev);
115
116 static struct comedi_driver driver_ni_670x = {
117       driver_name:"ni_670x",
118       module:THIS_MODULE,
119       attach:ni_670x_attach,
120       detach:ni_670x_detach,
121 };
122
123 COMEDI_PCI_INITCLEANUP(driver_ni_670x, ni_670x_pci_table);
124
125 static struct comedi_lrange range_0_20mA = { 1, {RANGE_mA(0, 20)} };
126
127 static int ni_670x_find_device(struct comedi_device * dev, int bus, int slot);
128
129 static int ni_670x_ao_winsn(struct comedi_device * dev, struct comedi_subdevice * s,
130         struct comedi_insn * insn, unsigned int * data);
131 static int ni_670x_ao_rinsn(struct comedi_device * dev, struct comedi_subdevice * s,
132         struct comedi_insn * insn, unsigned int * data);
133 static int ni_670x_dio_insn_bits(struct comedi_device * dev, struct comedi_subdevice * s,
134         struct comedi_insn * insn, unsigned int * data);
135 static int ni_670x_dio_insn_config(struct comedi_device * dev, struct comedi_subdevice * s,
136         struct comedi_insn * insn, unsigned int * data);
137
138 static int ni_670x_attach(struct comedi_device * dev, struct comedi_devconfig * it)
139 {
140         struct comedi_subdevice *s;
141         int ret;
142         int i;
143
144         printk("comedi%d: ni_670x: ", dev->minor);
145
146         if ((ret = alloc_private(dev, sizeof(struct ni_670x_private))) < 0)
147                 return ret;
148
149         ret = ni_670x_find_device(dev, it->options[0], it->options[1]);
150         if (ret < 0)
151                 return ret;
152
153         ret = mite_setup(devpriv->mite);
154         if (ret < 0) {
155                 printk("error setting up mite\n");
156                 return ret;
157         }
158         dev->board_name = thisboard->name;
159         dev->irq = mite_irq(devpriv->mite);
160         printk(" %s", dev->board_name);
161
162         if (alloc_subdevices(dev, 2) < 0)
163                 return -ENOMEM;
164
165         s = dev->subdevices + 0;
166         /* analog output subdevice */
167         s->type = COMEDI_SUBD_AO;
168         s->subdev_flags = SDF_WRITABLE;
169         s->n_chan = thisboard->ao_chans;
170         s->maxdata = 0xffff;
171         if (s->n_chan == 32) {
172                 const struct comedi_lrange **range_table_list;
173
174                 range_table_list = kmalloc(sizeof(struct comedi_lrange *) * 32,
175                         GFP_KERNEL);
176                 if (!range_table_list)
177                         return -ENOMEM;
178                 s->range_table_list = range_table_list;
179                 for (i = 0; i < 16; i++) {
180                         range_table_list[i] = &range_bipolar10;
181                         range_table_list[16 + i] = &range_0_20mA;
182                 }
183         } else {
184                 s->range_table = &range_bipolar10;
185         }
186         s->insn_write = &ni_670x_ao_winsn;
187         s->insn_read = &ni_670x_ao_rinsn;
188
189         s = dev->subdevices + 1;
190         /* digital i/o subdevice */
191         s->type = COMEDI_SUBD_DIO;
192         s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
193         s->n_chan = 8;
194         s->maxdata = 1;
195         s->range_table = &range_digital;
196         s->insn_bits = ni_670x_dio_insn_bits;
197         s->insn_config = ni_670x_dio_insn_config;
198
199         writel(0x10, devpriv->mite->daq_io_addr + MISC_CONTROL_OFFSET); /* Config of misc registers */
200         writel(0x00, devpriv->mite->daq_io_addr + AO_CONTROL_OFFSET);   /* Config of ao registers */
201
202         printk("attached\n");
203
204         return 1;
205 }
206
207 static int ni_670x_detach(struct comedi_device * dev)
208 {
209         printk("comedi%d: ni_670x: remove\n", dev->minor);
210
211         if (dev->subdevices[0].range_table_list) {
212                 kfree(dev->subdevices[0].range_table_list);
213         }
214         if (dev->private && devpriv->mite)
215                 mite_unsetup(devpriv->mite);
216
217         if (dev->irq)
218                 comedi_free_irq(dev->irq, dev);
219
220         return 0;
221 }
222
223 static int ni_670x_ao_winsn(struct comedi_device * dev, struct comedi_subdevice * s,
224         struct comedi_insn * insn, unsigned int * data)
225 {
226         int i;
227         int chan = CR_CHAN(insn->chanspec);
228
229         /* Channel number mapping :
230
231            NI 6703/ NI 6704     | NI 6704 Only
232            ----------------------------------------------------
233            vch(0)       :       0       | ich(16)       :       1
234            vch(1)       :       2       | ich(17)       :       3
235            .    :       .       |   .                   .
236            .    :       .       |   .                   .
237            .    :       .       |   .                   .
238            vch(15)      :       30      | ich(31)       :       31      */
239
240         for (i = 0; i < insn->n; i++) {
241                 writel(((chan & 15) << 1) | ((chan & 16) >> 4), devpriv->mite->daq_io_addr + AO_CHAN_OFFSET);   /* First write in channel register which channel to use */
242                 writel(data[i], devpriv->mite->daq_io_addr + AO_VALUE_OFFSET);  /* write channel value */
243                 devpriv->ao_readback[chan] = data[i];
244         }
245
246         return i;
247 }
248
249 static int ni_670x_ao_rinsn(struct comedi_device * dev, struct comedi_subdevice * s,
250         struct comedi_insn * insn, unsigned int * data)
251 {
252         int i;
253         int chan = CR_CHAN(insn->chanspec);
254
255         for (i = 0; i < insn->n; i++)
256                 data[i] = devpriv->ao_readback[chan];
257
258         return i;
259 }
260
261 static int ni_670x_dio_insn_bits(struct comedi_device * dev, struct comedi_subdevice * s,
262         struct comedi_insn * insn, unsigned int * data)
263 {
264         if (insn->n != 2)
265                 return -EINVAL;
266
267         /* The insn data is a mask in data[0] and the new data
268          * in data[1], each channel cooresponding to a bit. */
269         if (data[0]) {
270                 s->state &= ~data[0];
271                 s->state |= data[0] & data[1];
272                 writel(s->state,
273                         devpriv->mite->daq_io_addr + DIO_PORT0_DATA_OFFSET);
274         }
275
276         /* on return, data[1] contains the value of the digital
277          * input lines. */
278         data[1] = readl(devpriv->mite->daq_io_addr + DIO_PORT0_DATA_OFFSET);
279
280         return 2;
281 }
282
283 static int ni_670x_dio_insn_config(struct comedi_device * dev, struct comedi_subdevice * s,
284         struct comedi_insn * insn, unsigned int * data)
285 {
286         int chan = CR_CHAN(insn->chanspec);
287
288         switch (data[0]) {
289         case INSN_CONFIG_DIO_OUTPUT:
290                 s->io_bits |= 1 << chan;
291                 break;
292         case INSN_CONFIG_DIO_INPUT:
293                 s->io_bits &= ~(1 << chan);
294                 break;
295         case INSN_CONFIG_DIO_QUERY:
296                 data[1] =
297                         (s->
298                         io_bits & (1 << chan)) ? COMEDI_OUTPUT : COMEDI_INPUT;
299                 return insn->n;
300                 break;
301         default:
302                 return -EINVAL;
303                 break;
304         }
305         writel(s->io_bits, devpriv->mite->daq_io_addr + DIO_PORT0_DIR_OFFSET);
306
307         return insn->n;
308 }
309
310 static int ni_670x_find_device(struct comedi_device * dev, int bus, int slot)
311 {
312         struct mite_struct *mite;
313         int i;
314
315         for (mite = mite_devices; mite; mite = mite->next) {
316                 if (mite->used)
317                         continue;
318                 if (bus || slot) {
319                         if (bus != mite->pcidev->bus->number
320                                 || slot != PCI_SLOT(mite->pcidev->devfn))
321                                 continue;
322                 }
323
324                 for (i = 0; i < n_ni_670x_boards; i++) {
325                         if (mite_device_id(mite) == ni_670x_boards[i].dev_id) {
326                                 dev->board_ptr = ni_670x_boards + i;
327                                 devpriv->mite = mite;
328
329                                 return 0;
330                         }
331                 }
332         }
333         printk("no device found\n");
334         mite_list_devices();
335         return -EIO;
336 }