]> Pileus Git - ~andy/linux/blob - drivers/staging/comedi/drivers/adl_pci7296.c
staging: comedi: export alloc_subdevices as comedi_alloc_subdevices
[~andy/linux] / drivers / staging / comedi / drivers / adl_pci7296.c
1 /*
2     comedi/drivers/adl_pci7296.c
3
4     COMEDI - Linux Control and Measurement Device Interface
5     Copyright (C) 2000 David A. Schleef <ds@schleef.org>
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_pci7296
24 Description: Driver for the Adlink PCI-7296 96 ch. digital io board
25 Devices: [ADLink] PCI-7296 (adl_pci7296)
26 Author: Jon Grierson <jd@renko.co.uk>
27 Updated: Mon, 14 Apr 2008 15:05:56 +0100
28 Status: testing
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 #include "8255.h"
41 /* #include "8253.h" */
42
43 #define PORT1A 0
44 #define PORT2A 4
45 #define PORT3A 8
46 #define PORT4A 12
47
48 #define PCI_DEVICE_ID_PCI7296 0x7296
49
50 struct adl_pci7296_private {
51         int data;
52         struct pci_dev *pci_dev;
53 };
54
55 #define devpriv ((struct adl_pci7296_private *)dev->private)
56
57 static struct pci_dev *adl_pci7296_find_pci(struct comedi_device *dev,
58                                             struct comedi_devconfig *it)
59 {
60         struct pci_dev *pcidev = NULL;
61         int bus = it->options[0];
62         int slot = it->options[1];
63
64         for_each_pci_dev(pcidev) {
65                 if (pcidev->vendor != PCI_VENDOR_ID_ADLINK ||
66                     pcidev->device != PCI_DEVICE_ID_PCI7296)
67                         continue;
68                 if (bus || slot) {
69                         /* requested particular bus/slot */
70                         if (pcidev->bus->number != bus ||
71                             PCI_SLOT(pcidev->devfn) != slot)
72                                 continue;
73                 }
74                 return pcidev;
75         }
76         printk(KERN_ERR
77                 "comedi%d: no supported board found! (req. bus/slot : %d/%d)\n",
78                dev->minor, bus, slot);
79         return NULL;
80 }
81
82 static int adl_pci7296_attach(struct comedi_device *dev,
83                               struct comedi_devconfig *it)
84 {
85         struct comedi_subdevice *s;
86         int ret;
87
88         printk(KERN_INFO "comedi%d: attach adl_pci7432\n", dev->minor);
89
90         dev->board_name = "pci7432";
91
92         if (alloc_private(dev, sizeof(struct adl_pci7296_private)) < 0)
93                 return -ENOMEM;
94
95         if (comedi_alloc_subdevices(dev, 4) < 0)
96                 return -ENOMEM;
97
98         devpriv->pci_dev = adl_pci7296_find_pci(dev, it);
99         if (!devpriv->pci_dev)
100                 return -EIO;
101
102         if (comedi_pci_enable(devpriv->pci_dev, "adl_pci7296") < 0) {
103                 printk(KERN_ERR
104                         "comedi%d: Failed to enable PCI device and request regions\n",
105                         dev->minor);
106                 return -EIO;
107         }
108
109         dev->iobase = pci_resource_start(devpriv->pci_dev, 2);
110         printk(KERN_INFO "comedi: base addr %4lx\n", dev->iobase);
111
112         /*  four 8255 digital io subdevices */
113         s = dev->subdevices + 0;
114         subdev_8255_init(dev, s, NULL, (unsigned long)(dev->iobase));
115
116         s = dev->subdevices + 1;
117         ret = subdev_8255_init(dev, s, NULL,
118                                 (unsigned long)(dev->iobase + PORT2A));
119         if (ret < 0)
120                 return ret;
121
122         s = dev->subdevices + 2;
123         ret = subdev_8255_init(dev, s, NULL,
124                                 (unsigned long)(dev->iobase + PORT3A));
125         if (ret < 0)
126                 return ret;
127
128         s = dev->subdevices + 3;
129         ret = subdev_8255_init(dev, s, NULL,
130                                 (unsigned long)(dev->iobase + PORT4A));
131         if (ret < 0)
132                 return ret;
133
134         printk(KERN_DEBUG "comedi%d: adl_pci7432 attached\n", dev->minor);
135
136         return 0;
137 }
138
139 static void adl_pci7296_detach(struct comedi_device *dev)
140 {
141         if (devpriv && devpriv->pci_dev) {
142                 if (dev->iobase)
143                         comedi_pci_disable(devpriv->pci_dev);
144                 pci_dev_put(devpriv->pci_dev);
145         }
146         if (dev->subdevices) {
147                 subdev_8255_cleanup(dev, dev->subdevices + 0);
148                 subdev_8255_cleanup(dev, dev->subdevices + 1);
149                 subdev_8255_cleanup(dev, dev->subdevices + 2);
150                 subdev_8255_cleanup(dev, dev->subdevices + 3);
151         }
152 }
153
154 static struct comedi_driver adl_pci7296_driver = {
155         .driver_name    = "adl_pci7296",
156         .module         = THIS_MODULE,
157         .attach         = adl_pci7296_attach,
158         .detach         = adl_pci7296_detach,
159 };
160
161 static int __devinit adl_pci7296_pci_probe(struct pci_dev *dev,
162                                            const struct pci_device_id *ent)
163 {
164         return comedi_pci_auto_config(dev, &adl_pci7296_driver);
165 }
166
167 static void __devexit adl_pci7296_pci_remove(struct pci_dev *dev)
168 {
169         comedi_pci_auto_unconfig(dev);
170 }
171
172 static DEFINE_PCI_DEVICE_TABLE(adl_pci7296_pci_table) = {
173         { PCI_DEVICE(PCI_VENDOR_ID_ADLINK, PCI_DEVICE_ID_PCI7296) },
174         { 0 }
175 };
176 MODULE_DEVICE_TABLE(pci, adl_pci7296_pci_table);
177
178 static struct pci_driver adl_pci7296_pci_driver = {
179         .name           = "adl_pci7296",
180         .id_table       = adl_pci7296_pci_table,
181         .probe          = adl_pci7296_pci_probe,
182         .remove         = __devexit_p(adl_pci7296_pci_remove),
183 };
184 module_comedi_pci_driver(adl_pci7296_driver, adl_pci7296_pci_driver);
185
186 MODULE_AUTHOR("Comedi http://www.comedi.org");
187 MODULE_DESCRIPTION("Comedi low-level driver");
188 MODULE_LICENSE("GPL");