78becbdd17a87c6574d3aca51810210e8cb88387
[safe/jmp/linux-2.6] / drivers / staging / comedi / drivers / adl_pci7432.c
1 /*
2     comedi/drivers/adl_pci7432.c
3
4     Hardware comedi driver fot PCI7432 Adlink card
5     Copyright (C) 2004 Michel Lachine <mike@mikelachaine.ca>
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 /*
23 Driver: adl_pci7432
24 Description: Driver for the Adlink PCI-7432 64 ch. isolated digital io board
25 Devices: [ADLink] PCI-7432 (adl_pci7432)
26 Author: Michel Lachaine <mike@mikelachaine.ca>
27 Status: experimental
28 Updated: Mon, 14 Apr 2008 15:08:14 +0100
29
30 Configuration Options:
31   [0] - PCI bus of device (optional)
32   [1] - PCI slot of device (optional)
33   If bus/slot is not specified, the first supported
34   PCI device found will be used.
35 */
36
37 #include "../comedidev.h"
38 #include <linux/kernel.h>
39 #include "comedi_pci.h"
40
41 #define PCI7432_DI      0x00
42 #define PCI7432_DO          0x00
43
44 #define PCI_DEVICE_ID_PCI7432 0x7432
45
46 static DEFINE_PCI_DEVICE_TABLE(adl_pci7432_pci_table) = {
47         {PCI_VENDOR_ID_ADLINK, PCI_DEVICE_ID_PCI7432, PCI_ANY_ID, PCI_ANY_ID, 0,
48                 0, 0},
49         {0}
50 };
51
52 MODULE_DEVICE_TABLE(pci, adl_pci7432_pci_table);
53
54 struct adl_pci7432_private {
55         int data;
56         struct pci_dev *pci_dev;
57 };
58
59 #define devpriv ((struct adl_pci7432_private *)dev->private)
60
61 static int adl_pci7432_attach(struct comedi_device *dev, struct comedi_devconfig *it);
62 static int adl_pci7432_detach(struct comedi_device *dev);
63 static struct comedi_driver driver_adl_pci7432 = {
64         .driver_name = "adl_pci7432",
65         .module = THIS_MODULE,
66         .attach = adl_pci7432_attach,
67         .detach = adl_pci7432_detach,
68 };
69
70 /* Digital IO */
71
72 static int adl_pci7432_di_insn_bits(struct comedi_device *dev, struct comedi_subdevice *s,
73         struct comedi_insn *insn, unsigned int *data);
74
75 static int adl_pci7432_do_insn_bits(struct comedi_device *dev, struct comedi_subdevice *s,
76         struct comedi_insn *insn, unsigned int *data);
77
78 /*            */
79
80 static int adl_pci7432_attach(struct comedi_device *dev, struct comedi_devconfig *it)
81 {
82         struct pci_dev *pcidev;
83         struct comedi_subdevice *s;
84         int bus, slot;
85
86         printk("comedi: attempt to attach...\n");
87         printk("comedi%d: adl_pci7432\n", dev->minor);
88
89         dev->board_name = "pci7432";
90         bus = it->options[0];
91         slot = it->options[1];
92
93         if (alloc_private(dev, sizeof(struct adl_pci7432_private)) < 0)
94                 return -ENOMEM;
95
96         if (alloc_subdevices(dev, 2) < 0)
97                 return -ENOMEM;
98
99         for (pcidev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, NULL);
100                 pcidev != NULL;
101                 pcidev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, pcidev)) {
102
103                 if (pcidev->vendor == PCI_VENDOR_ID_ADLINK &&
104                         pcidev->device == PCI_DEVICE_ID_PCI7432) {
105                         if (bus || slot) {
106                                 /* requested particular bus/slot */
107                                 if (pcidev->bus->number != bus
108                                         || PCI_SLOT(pcidev->devfn) != slot) {
109                                         continue;
110                                 }
111                         }
112                         devpriv->pci_dev = pcidev;
113                         if (comedi_pci_enable(pcidev, "adl_pci7432") < 0) {
114                                 printk("comedi%d: Failed to enable PCI device and request regions\n", dev->minor);
115                                 return -EIO;
116                         }
117                         dev->iobase = pci_resource_start(pcidev, 2);
118                         printk("comedi: base addr %4lx\n", dev->iobase);
119
120                         s = dev->subdevices + 0;
121                         s->type = COMEDI_SUBD_DI;
122                         s->subdev_flags =
123                                 SDF_READABLE | SDF_GROUND | SDF_COMMON;
124                         s->n_chan = 32;
125                         s->maxdata = 1;
126                         s->len_chanlist = 32;
127                         s->io_bits = 0x00000000;
128                         s->range_table = &range_digital;
129                         s->insn_bits = adl_pci7432_di_insn_bits;
130
131                         s = dev->subdevices + 1;
132                         s->type = COMEDI_SUBD_DO;
133                         s->subdev_flags =
134                                 SDF_WRITABLE | SDF_GROUND | SDF_COMMON;
135                         s->n_chan = 32;
136                         s->maxdata = 1;
137                         s->len_chanlist = 32;
138                         s->io_bits = 0xffffffff;
139                         s->range_table = &range_digital;
140                         s->insn_bits = adl_pci7432_do_insn_bits;
141
142                         printk("comedi: attached\n");
143
144                         return 1;
145                 }
146         }
147
148         printk("comedi%d: no supported board found! (req. bus/slot : %d/%d)\n",
149                 dev->minor, bus, slot);
150         return -EIO;
151 }
152
153 static int adl_pci7432_detach(struct comedi_device *dev)
154 {
155         printk("comedi%d: pci7432: remove\n", dev->minor);
156
157         if (devpriv && devpriv->pci_dev) {
158                 if (dev->iobase) {
159                         comedi_pci_disable(devpriv->pci_dev);
160                 }
161                 pci_dev_put(devpriv->pci_dev);
162         }
163
164         return 0;
165 }
166
167 static int adl_pci7432_do_insn_bits(struct comedi_device *dev, struct comedi_subdevice *s,
168         struct comedi_insn *insn, unsigned int *data)
169 {
170         printk("comedi: pci7432_do_insn_bits called\n");
171         printk("comedi: data0: %8x data1: %8x\n", data[0], data[1]);
172
173         if (insn->n != 2)
174                 return -EINVAL;
175
176         if (data[0]) {
177                 s->state &= ~data[0];
178                 s->state |= (data[0] & data[1]);
179
180                 printk("comedi: out: %8x on iobase %4lx\n", s->state,
181                         dev->iobase + PCI7432_DO);
182                 outl(s->state & 0xffffffff, dev->iobase + PCI7432_DO);
183         }
184         return 2;
185 }
186
187 static int adl_pci7432_di_insn_bits(struct comedi_device *dev, struct comedi_subdevice *s,
188         struct comedi_insn *insn, unsigned int *data)
189 {
190         printk("comedi: pci7432_di_insn_bits called\n");
191         printk("comedi: data0: %8x data1: %8x\n", data[0], data[1]);
192
193         if (insn->n != 2)
194                 return -EINVAL;
195
196         data[1] = inl(dev->iobase + PCI7432_DI) & 0xffffffff;
197         printk("comedi: data1 %8x\n", data[1]);
198
199         return 2;
200 }
201
202 COMEDI_PCI_INITCLEANUP(driver_adl_pci7432, adl_pci7432_pci_table);