]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/staging/comedi/drivers/adl_pci7230.c
Merge tag 'tty-3.6-rc3' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/tty
[karo-tx-linux.git] / drivers / staging / comedi / drivers / adl_pci7230.c
1 /*
2     comedi/drivers/adl_pci7230.c
3
4     Hardware comedi driver fot PCI7230 Adlink card
5     Copyright (C) 2010 David Fernandez <dfcastelao@gmail.com>
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_pci7230
24 Description: Driver for the Adlink PCI-7230 32 ch. isolated digital io board
25 Devices: [ADLink] PCI-7230 (adl_pci7230)
26 Author: David Fernandez <dfcastelao@gmail.com>
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 PCI7230_DI      0x00
41 #define PCI7230_DO          0x00
42
43 #define PCI_DEVICE_ID_PCI7230 0x7230
44
45 static int adl_pci7230_do_insn_bits(struct comedi_device *dev,
46         struct comedi_subdevice *s,
47         struct comedi_insn *insn,
48         unsigned int *data)
49 {
50         if (data[0]) {
51                 s->state &= ~data[0];
52                 s->state |= (data[0] & data[1]);
53
54                 outl((s->state  << 16) & 0xffffffff, dev->iobase + PCI7230_DO);
55         }
56
57         return insn->n;
58 }
59
60 static int adl_pci7230_di_insn_bits(struct comedi_device *dev,
61         struct comedi_subdevice *s,
62         struct comedi_insn *insn,
63         unsigned int *data)
64 {
65         data[1] = inl(dev->iobase + PCI7230_DI) & 0xffffffff;
66
67         return insn->n;
68 }
69
70 static struct pci_dev *adl_pci7230_find_pci(struct comedi_device *dev,
71         struct comedi_devconfig *it)
72 {
73         struct pci_dev *pcidev = NULL;
74         int bus = it->options[0];
75         int slot = it->options[1];
76
77         for_each_pci_dev(pcidev) {
78                 if (pcidev->vendor != PCI_VENDOR_ID_ADLINK ||
79                     pcidev->device != PCI_DEVICE_ID_PCI7230)
80                         continue;
81                 if (bus || slot) {
82                         /* requested particular bus/slot */
83                         if (pcidev->bus->number != bus ||
84                             PCI_SLOT(pcidev->devfn) != slot)
85                                 continue;
86                 }
87                 return pcidev;
88         }
89         printk(KERN_ERR "comedi%d: no supported board found! (req. bus/slot : %d/%d)\n",
90                 dev->minor, bus, slot);
91         return NULL;
92 }
93
94 static int adl_pci7230_attach(struct comedi_device *dev,
95         struct comedi_devconfig *it)
96 {
97         struct comedi_subdevice *s;
98         struct pci_dev *pcidev;
99         int ret;
100
101         printk(KERN_INFO "comedi%d: adl_pci7230\n", dev->minor);
102
103         dev->board_name = "pci7230";
104
105         ret = comedi_alloc_subdevices(dev, 2);
106         if (ret)
107                 return ret;
108
109         pcidev = adl_pci7230_find_pci(dev, it);
110         if (!pcidev)
111                 return -EIO;
112         comedi_set_hw_dev(dev, &pcidev->dev);
113
114         if (comedi_pci_enable(pcidev, "adl_pci7230") < 0) {
115                 printk(KERN_ERR "comedi%d: Failed to enable PCI device and request regions\n",
116                         dev->minor);
117                 return -EIO;
118         }
119         dev->iobase = pci_resource_start(pcidev, 2);
120         printk(KERN_DEBUG "comedi: base addr %4lx\n", dev->iobase);
121
122         s = dev->subdevices + 0;
123         /* Isolated do */
124         s->type = COMEDI_SUBD_DO;
125         s->subdev_flags = SDF_WRITABLE | SDF_GROUND | SDF_COMMON;
126         s->n_chan = 16;
127         s->maxdata = 1;
128         s->range_table = &range_digital;
129         s->insn_bits = adl_pci7230_do_insn_bits;
130
131         s = dev->subdevices + 1;
132         /* Isolated di */
133         s->type = COMEDI_SUBD_DI;
134         s->subdev_flags = SDF_READABLE | SDF_GROUND | SDF_COMMON;
135         s->n_chan = 16;
136         s->maxdata = 1;
137         s->range_table = &range_digital;
138         s->insn_bits = adl_pci7230_di_insn_bits;
139
140         printk(KERN_DEBUG "comedi: attached\n");
141
142         return 1;
143 }
144
145 static void adl_pci7230_detach(struct comedi_device *dev)
146 {
147         struct pci_dev *pcidev = comedi_to_pci_dev(dev);
148
149         if (pcidev) {
150                 if (dev->iobase)
151                         comedi_pci_disable(pcidev);
152                 pci_dev_put(pcidev);
153         }
154 }
155
156 static struct comedi_driver adl_pci7230_driver = {
157         .driver_name    = "adl_pci7230",
158         .module         = THIS_MODULE,
159         .attach         = adl_pci7230_attach,
160         .detach         = adl_pci7230_detach,
161 };
162
163 static int __devinit adl_pci7230_pci_probe(struct pci_dev *dev,
164                                            const struct pci_device_id *ent)
165 {
166         return comedi_pci_auto_config(dev, &adl_pci7230_driver);
167 }
168
169 static void __devexit adl_pci7230_pci_remove(struct pci_dev *dev)
170 {
171         comedi_pci_auto_unconfig(dev);
172 }
173
174 static DEFINE_PCI_DEVICE_TABLE(adl_pci7230_pci_table) = {
175         { PCI_DEVICE(PCI_VENDOR_ID_ADLINK, PCI_DEVICE_ID_PCI7230) },
176         { 0 }
177 };
178 MODULE_DEVICE_TABLE(pci, adl_pci7230_pci_table);
179
180 static struct pci_driver adl_pci7230_pci_driver = {
181         .name           = "adl_pci7230",
182         .id_table       = adl_pci7230_pci_table,
183         .probe          = adl_pci7230_pci_probe,
184         .remove         = __devexit_p(adl_pci7230_pci_remove),
185 };
186 module_comedi_pci_driver(adl_pci7230_driver, adl_pci7230_pci_driver);
187
188 MODULE_AUTHOR("Comedi http://www.comedi.org");
189 MODULE_DESCRIPTION("Comedi low-level driver");
190 MODULE_LICENSE("GPL");