staging: comedi: cb_pcidio: fix for newer PCI-DIO48H
[pandora-kernel.git] / drivers / staging / comedi / drivers / cb_pcidio.c
1 /*
2     comedi/drivers/cb_pcidio.c
3     A Comedi driver for PCI-DIO24H & PCI-DIO48H of ComputerBoards (currently MeasurementComputing)
4
5     COMEDI - Linux Control and Measurement Device Interface
6     Copyright (C) 2000 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: cb_pcidio
25 Description: ComputerBoards' DIO boards with PCI interface
26 Devices: [Measurement Computing] PCI-DIO24 (cb_pcidio), PCI-DIO24H, PCI-DIO48H
27 Author: Yoshiya Matsuzaka
28 Updated: Mon, 29 Oct 2007 15:40:47 +0000
29 Status: experimental
30
31 This driver has been modified from skel.c of comedi-0.7.70.
32
33 Configuration Options:
34   [0] - PCI bus of device (optional)
35   [1] - PCI slot of device (optional)
36   If bus/slot is not specified, the first available PCI device will
37   be used.
38
39 Passing a zero for an option is the same as leaving it unspecified.
40 */
41
42 /*------------------------------ HEADER FILES ---------------------------------*/
43 #include "../comedidev.h"
44 #include "comedi_pci.h"
45 #include "8255.h"
46
47 /*-------------------------- MACROS and DATATYPES -----------------------------*/
48 #define PCI_VENDOR_ID_CB        0x1307
49
50 /*
51  * Board descriptions for two imaginary boards.  Describing the
52  * boards in this way is optional, and completely driver-dependent.
53  * Some drivers use arrays such as this, other do not.
54  */
55 struct pcidio_board {
56         const char *name;       /*  name of the board */
57         int dev_id;
58         int n_8255;             /*  number of 8255 chips on board */
59 };
60
61 static const struct pcidio_board pcidio_boards[] = {
62         {
63          .name = "pci-dio24",
64          .dev_id = 0x0028,
65          .n_8255 = 1,
66          },
67         {
68          .name = "pci-dio24h",
69          .dev_id = 0x0014,
70          .n_8255 = 1,
71          },
72         {
73          .name = "pci-dio48h",
74          .dev_id = 0x000b,
75          .n_8255 = 2,
76          },
77 };
78
79 /* This is used by modprobe to translate PCI IDs to drivers.  Should
80  * only be used for PCI and ISA-PnP devices */
81 /* Please add your PCI vendor ID to comedidev.h, and it will be forwarded
82  * upstream. */
83 static DEFINE_PCI_DEVICE_TABLE(pcidio_pci_table) = {
84         { PCI_DEVICE(PCI_VENDOR_ID_CB, 0x0028) },
85         { PCI_DEVICE(PCI_VENDOR_ID_CB, 0x0014) },
86         { PCI_DEVICE(PCI_VENDOR_ID_CB, 0x000b) },
87         { 0 }
88 };
89
90 MODULE_DEVICE_TABLE(pci, pcidio_pci_table);
91
92 /*
93  * Useful for shorthand access to the particular board structure
94  */
95 #define thisboard ((const struct pcidio_board *)dev->board_ptr)
96
97 /* this structure is for data unique to this hardware driver.  If
98    several hardware drivers keep similar information in this structure,
99    feel free to suggest moving the variable to the struct comedi_device struct.  */
100 struct pcidio_private {
101         int data;               /*  currently unused */
102
103         /* would be useful for a PCI device */
104         struct pci_dev *pci_dev;
105
106         /* used for DO readback, currently unused */
107         unsigned int do_readback[4];    /* up to 4 unsigned int suffice to hold 96 bits for PCI-DIO96 */
108
109         unsigned long dio_reg_base;     /*  address of port A of the first 8255 chip on board */
110 };
111
112 /*
113  * most drivers define the following macro to make it easy to
114  * access the private structure.
115  */
116 #define devpriv ((struct pcidio_private *)dev->private)
117
118 /*
119  * The struct comedi_driver structure tells the Comedi core module
120  * which functions to call to configure/deconfigure (attach/detach)
121  * the board, and also about the kernel module that contains
122  * the device code.
123  */
124 static int pcidio_attach(struct comedi_device *dev,
125                          struct comedi_devconfig *it);
126 static int pcidio_detach(struct comedi_device *dev);
127 static struct comedi_driver driver_cb_pcidio = {
128         .driver_name = "cb_pcidio",
129         .module = THIS_MODULE,
130         .attach = pcidio_attach,
131         .detach = pcidio_detach,
132
133 /* It is not necessary to implement the following members if you are
134  * writing a driver for a ISA PnP or PCI card */
135
136         /* Most drivers will support multiple types of boards by
137          * having an array of board structures.  These were defined
138          * in pcidio_boards[] above.  Note that the element 'name'
139          * was first in the structure -- Comedi uses this fact to
140          * extract the name of the board without knowing any details
141          * about the structure except for its length.
142          * When a device is attached (by comedi_config), the name
143          * of the device is given to Comedi, and Comedi tries to
144          * match it by going through the list of board names.  If
145          * there is a match, the address of the pointer is put
146          * into dev->board_ptr and driver->attach() is called.
147          *
148          * Note that these are not necessary if you can determine
149          * the type of board in software.  ISA PnP, PCI, and PCMCIA
150          * devices are such boards.
151          */
152
153 /* The following fields should NOT be initialized if you are dealing
154  * with PCI devices
155  *
156  *      .board_name = pcidio_boards,
157  *      .offset = sizeof(struct pcidio_board),
158  *      .num_names = sizeof(pcidio_boards) / sizeof(structpcidio_board),
159  */
160
161 };
162
163 /*------------------------------- FUNCTIONS -----------------------------------*/
164
165 /*
166  * Attach is called by the Comedi core to configure the driver
167  * for a particular board.  If you specified a board_name array
168  * in the driver structure, dev->board_ptr contains that
169  * address.
170  */
171 static int pcidio_attach(struct comedi_device *dev, struct comedi_devconfig *it)
172 {
173         struct pci_dev *pcidev = NULL;
174         int index;
175         int i;
176
177         printk("comedi%d: cb_pcidio: \n", dev->minor);
178
179 /*
180  * Allocate the private structure area.  alloc_private() is a
181  * convenient macro defined in comedidev.h.
182  */
183         if (alloc_private(dev, sizeof(struct pcidio_private)) < 0)
184                 return -ENOMEM;
185 /*
186  * If you can probe the device to determine what device in a series
187  * it is, this is the place to do it.  Otherwise, dev->board_ptr
188  * should already be initialized.
189  */
190 /*
191  * Probe the device to determine what device in the series it is.
192  */
193
194         for_each_pci_dev(pcidev) {
195                 /*  is it not a computer boards card? */
196                 if (pcidev->vendor != PCI_VENDOR_ID_CB)
197                         continue;
198                 /*  loop through cards supported by this driver */
199                 for (index = 0; index < ARRAY_SIZE(pcidio_boards); index++) {
200                         if (pcidio_boards[index].dev_id != pcidev->device)
201                                 continue;
202
203                         /*  was a particular bus/slot requested? */
204                         if (it->options[0] || it->options[1]) {
205                                 /*  are we on the wrong bus/slot? */
206                                 if (pcidev->bus->number != it->options[0] ||
207                                     PCI_SLOT(pcidev->devfn) != it->options[1]) {
208                                         continue;
209                                 }
210                         }
211                         dev->board_ptr = pcidio_boards + index;
212                         goto found;
213                 }
214         }
215
216         printk("No supported ComputerBoards/MeasurementComputing card found on "
217                "requested position\n");
218         return -EIO;
219
220 found:
221
222 /*
223  * Initialize dev->board_name.  Note that we can use the "thisboard"
224  * macro now, since we just initialized it in the last line.
225  */
226         dev->board_name = thisboard->name;
227
228         devpriv->pci_dev = pcidev;
229         printk("Found %s on bus %i, slot %i\n", thisboard->name,
230                devpriv->pci_dev->bus->number,
231                PCI_SLOT(devpriv->pci_dev->devfn));
232         if (comedi_pci_enable(pcidev, thisboard->name)) {
233                 printk
234                     ("cb_pcidio: failed to enable PCI device and request regions\n");
235                 return -EIO;
236         }
237         /*
238          * Use PCI BAR 2 region if non-zero length, else use PCI BAR 1 region.
239          * PCI BAR 1 is only used for older PCI-DIO48H boards.  At some point
240          * the PCI-DIO48H was redesigned to use the same PCI interface chip
241          * (and same PCI BAR region) as the other boards.
242          */
243         devpriv->dio_reg_base =
244             pci_resource_start(devpriv->pci_dev,
245                                (pci_resource_len(pcidev, 2) ? 2 : 1));
246
247 /*
248  * Allocate the subdevice structures.  alloc_subdevice() is a
249  * convenient macro defined in comedidev.h.
250  */
251         if (alloc_subdevices(dev, thisboard->n_8255) < 0)
252                 return -ENOMEM;
253
254         for (i = 0; i < thisboard->n_8255; i++) {
255                 subdev_8255_init(dev, dev->subdevices + i,
256                                  NULL, devpriv->dio_reg_base + i * 4);
257                 printk(" subdev %d: base = 0x%lx\n", i,
258                        devpriv->dio_reg_base + i * 4);
259         }
260
261         printk("attached\n");
262         return 1;
263 }
264
265 /*
266  * _detach is called to deconfigure a device.  It should deallocate
267  * resources.
268  * This function is also called when _attach() fails, so it should be
269  * careful not to release resources that were not necessarily
270  * allocated by _attach().  dev->private and dev->subdevices are
271  * deallocated automatically by the core.
272  */
273 static int pcidio_detach(struct comedi_device *dev)
274 {
275         printk("comedi%d: cb_pcidio: remove\n", dev->minor);
276         if (devpriv) {
277                 if (devpriv->pci_dev) {
278                         if (devpriv->dio_reg_base)
279                                 comedi_pci_disable(devpriv->pci_dev);
280                         pci_dev_put(devpriv->pci_dev);
281                 }
282         }
283         if (dev->subdevices) {
284                 int i;
285                 for (i = 0; i < thisboard->n_8255; i++)
286                         subdev_8255_cleanup(dev, dev->subdevices + i);
287         }
288         return 0;
289 }
290
291 /*
292  * A convenient macro that defines init_module() and cleanup_module(),
293  * as necessary.
294  */
295 static int __devinit driver_cb_pcidio_pci_probe(struct pci_dev *dev,
296                                                 const struct pci_device_id *ent)
297 {
298         return comedi_pci_auto_config(dev, driver_cb_pcidio.driver_name);
299 }
300
301 static void __devexit driver_cb_pcidio_pci_remove(struct pci_dev *dev)
302 {
303         comedi_pci_auto_unconfig(dev);
304 }
305
306 static struct pci_driver driver_cb_pcidio_pci_driver = {
307         .id_table = pcidio_pci_table,
308         .probe = &driver_cb_pcidio_pci_probe,
309         .remove = __devexit_p(&driver_cb_pcidio_pci_remove)
310 };
311
312 static int __init driver_cb_pcidio_init_module(void)
313 {
314         int retval;
315
316         retval = comedi_driver_register(&driver_cb_pcidio);
317         if (retval < 0)
318                 return retval;
319
320         driver_cb_pcidio_pci_driver.name = (char *)driver_cb_pcidio.driver_name;
321         return pci_register_driver(&driver_cb_pcidio_pci_driver);
322 }
323
324 static void __exit driver_cb_pcidio_cleanup_module(void)
325 {
326         pci_unregister_driver(&driver_cb_pcidio_pci_driver);
327         comedi_driver_unregister(&driver_cb_pcidio);
328 }
329
330 module_init(driver_cb_pcidio_init_module);
331 module_exit(driver_cb_pcidio_cleanup_module);
332
333 MODULE_AUTHOR("Comedi http://www.comedi.org");
334 MODULE_DESCRIPTION("Comedi low-level driver");
335 MODULE_LICENSE("GPL");