]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/staging/comedi/drivers/adl_pci7432.c
staging: comedi: new adl_pci7x3x driver
[karo-tx-linux.git] / 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
40 #define PCI7432_DI      0x00
41 #define PCI7432_DO          0x00
42
43 #define PCI_DEVICE_ID_PCI7432 0x7432
44
45 static int adl_pci7432_do_insn_bits(struct comedi_device *dev,
46                                     struct comedi_subdevice *s,
47                                     struct comedi_insn *insn,
48                                     unsigned int *data)
49 {
50         printk(KERN_DEBUG "comedi: pci7432_do_insn_bits called\n");
51         printk(KERN_DEBUG "comedi: data0: %8x data1: %8x\n", data[0], data[1]);
52
53         if (data[0]) {
54                 s->state &= ~data[0];
55                 s->state |= (data[0] & data[1]);
56
57                 printk(KERN_DEBUG "comedi: out: %8x on iobase %4lx\n", s->state,
58                        dev->iobase + PCI7432_DO);
59                 outl(s->state & 0xffffffff, dev->iobase + PCI7432_DO);
60         }
61         return insn->n;
62 }
63
64 static int adl_pci7432_di_insn_bits(struct comedi_device *dev,
65                                     struct comedi_subdevice *s,
66                                     struct comedi_insn *insn,
67                                     unsigned int *data)
68 {
69         printk(KERN_DEBUG "comedi: pci7432_di_insn_bits called\n");
70         printk(KERN_DEBUG "comedi: data0: %8x data1: %8x\n", data[0], data[1]);
71
72         data[1] = inl(dev->iobase + PCI7432_DI) & 0xffffffff;
73         printk(KERN_DEBUG "comedi: data1 %8x\n", data[1]);
74
75         return insn->n;
76 }
77
78 static struct pci_dev *adl_pci7432_find_pci(struct comedi_device *dev,
79                                             struct comedi_devconfig *it)
80 {
81         struct pci_dev *pcidev = NULL;
82         int bus = it->options[0];
83         int slot = it->options[1];
84
85         for_each_pci_dev(pcidev) {
86                 if (pcidev->vendor != PCI_VENDOR_ID_ADLINK ||
87                     pcidev->device != PCI_DEVICE_ID_PCI7432)
88                         continue;
89                 if (bus || slot) {
90                         /* requested particular bus/slot */
91                         if (pcidev->bus->number != bus ||
92                             PCI_SLOT(pcidev->devfn) != slot)
93                                 continue;
94                 }
95                 return pcidev;
96         }
97         printk(KERN_ERR
98                 "comedi%d: no supported board found! (req. bus/slot : %d/%d)\n",
99                dev->minor, bus, slot);
100         return NULL;
101 }
102
103 static int adl_pci7432_attach(struct comedi_device *dev,
104                               struct comedi_devconfig *it)
105 {
106         struct pci_dev *pcidev;
107         struct comedi_subdevice *s;
108         int ret;
109
110         printk(KERN_INFO "comedi%d: attach adl_pci7432\n", dev->minor);
111
112         dev->board_name = "pci7432";
113
114         ret = comedi_alloc_subdevices(dev, 2);
115         if (ret)
116                 return ret;
117
118         pcidev = adl_pci7432_find_pci(dev, it);
119         if (!pcidev)
120                 return -EIO;
121         comedi_set_hw_dev(dev, &pcidev->dev);
122
123         if (comedi_pci_enable(pcidev, "adl_pci7432") < 0) {
124                 printk(KERN_ERR "comedi%d: Failed to enable PCI device and request regions\n",
125                         dev->minor);
126                 return -EIO;
127         }
128         dev->iobase = pci_resource_start(pcidev, 2);
129         printk(KERN_INFO "comedi: base addr %4lx\n", dev->iobase);
130
131         s = dev->subdevices + 0;
132         s->type = COMEDI_SUBD_DI;
133         s->subdev_flags = SDF_READABLE | SDF_GROUND | SDF_COMMON;
134         s->n_chan = 32;
135         s->maxdata = 1;
136         s->len_chanlist = 32;
137         s->io_bits = 0x00000000;
138         s->range_table = &range_digital;
139         s->insn_bits = adl_pci7432_di_insn_bits;
140
141         s = dev->subdevices + 1;
142         s->type = COMEDI_SUBD_DO;
143         s->subdev_flags = SDF_WRITABLE | SDF_GROUND | SDF_COMMON;
144         s->n_chan = 32;
145         s->maxdata = 1;
146         s->len_chanlist = 32;
147         s->io_bits = 0xffffffff;
148         s->range_table = &range_digital;
149         s->insn_bits = adl_pci7432_do_insn_bits;
150
151         printk(KERN_DEBUG "comedi%d: adl_pci7432 attached\n", dev->minor);
152         return 0;
153 }
154
155 static void adl_pci7432_detach(struct comedi_device *dev)
156 {
157         struct pci_dev *pcidev = comedi_to_pci_dev(dev);
158
159         if (pcidev) {
160                 if (dev->iobase)
161                         comedi_pci_disable(pcidev);
162                 pci_dev_put(pcidev);
163         }
164 }
165
166 static struct comedi_driver adl_pci7432_driver = {
167         .driver_name    = "adl_pci7432",
168         .module         = THIS_MODULE,
169         .attach         = adl_pci7432_attach,
170         .detach         = adl_pci7432_detach,
171 };
172
173 static int __devinit adl_pci7432_pci_probe(struct pci_dev *dev,
174                                            const struct pci_device_id *ent)
175 {
176         return comedi_pci_auto_config(dev, &adl_pci7432_driver);
177 }
178
179 static void __devexit adl_pci7432_pci_remove(struct pci_dev *dev)
180 {
181         comedi_pci_auto_unconfig(dev);
182 }
183
184 static DEFINE_PCI_DEVICE_TABLE(adl_pci7432_pci_table) = {
185         { PCI_DEVICE(PCI_VENDOR_ID_ADLINK, PCI_DEVICE_ID_PCI7432) },
186         { 0 }
187 };
188 MODULE_DEVICE_TABLE(pci, adl_pci7432_pci_table);
189
190 static struct pci_driver adl_pci7432_pci_driver = {
191         .name           = "adl_pci7432",
192         .id_table       = adl_pci7432_pci_table,
193         .probe          = adl_pci7432_pci_probe,
194         .remove         = __devexit_p(adl_pci7432_pci_remove),
195 };
196 module_comedi_pci_driver(adl_pci7432_driver, adl_pci7432_pci_driver);
197
198 MODULE_AUTHOR("Comedi http://www.comedi.org");
199 MODULE_DESCRIPTION("Comedi low-level driver");
200 MODULE_LICENSE("GPL");