2 comedi/drivers/cb_pcidio.c
3 A Comedi driver for PCI-DIO24H & PCI-DIO48H of ComputerBoards (currently MeasurementComputing)
5 COMEDI - Linux Control and Measurement Device Interface
6 Copyright (C) 2000 David A. Schleef <ds@schleef.org>
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.
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.
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.
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
31 This driver has been modified from skel.c of comedi-0.7.70.
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
39 Passing a zero for an option is the same as leaving it unspecified.
42 /*------------------------------ HEADER FILES ---------------------------------*/
43 #include "../comedidev.h"
44 #include "comedi_pci.h"
47 /*-------------------------- MACROS and DATATYPES -----------------------------*/
48 #define PCI_VENDOR_ID_CB 0x1307
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.
56 const char *name; /* anme of the board */
57 int n_8255; /* number of 8255 chips on board */
59 /* indices of base address regions */
60 int pcicontroler_badrindex;
61 int dioregs_badrindex;
64 static const struct pcidio_board pcidio_boards[] = {
68 .pcicontroler_badrindex = 1,
69 .dioregs_badrindex = 2,
74 .pcicontroler_badrindex = 1,
75 .dioregs_badrindex = 2,
80 .pcicontroler_badrindex = 0,
81 .dioregs_badrindex = 1,
85 /* This is used by modprobe to translate PCI IDs to drivers. Should
86 * only be used for PCI and ISA-PnP devices */
87 /* Please add your PCI vendor ID to comedidev.h, and it will be forwarded
89 static DEFINE_PCI_DEVICE_TABLE(pcidio_pci_table) = {
91 PCI_VENDOR_ID_CB, 0x0028, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0}, {
92 PCI_VENDOR_ID_CB, 0x0014, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0}, {
93 PCI_VENDOR_ID_CB, 0x000b, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0}, {
97 MODULE_DEVICE_TABLE(pci, pcidio_pci_table);
100 * Useful for shorthand access to the particular board structure
102 #define thisboard ((const struct pcidio_board *)dev->board_ptr)
104 /* this structure is for data unique to this hardware driver. If
105 several hardware drivers keep similar information in this structure,
106 feel free to suggest moving the variable to the struct comedi_device struct. */
107 struct pcidio_private {
108 int data; /* curently unused */
110 /* would be useful for a PCI device */
111 struct pci_dev *pci_dev;
113 /* used for DO readback, curently unused */
114 unsigned int do_readback[4]; /* up to 4 unsigned int suffice to hold 96 bits for PCI-DIO96 */
116 unsigned long dio_reg_base; /* address of port A of the first 8255 chip on board */
120 * most drivers define the following macro to make it easy to
121 * access the private structure.
123 #define devpriv ((struct pcidio_private *)dev->private)
126 * The struct comedi_driver structure tells the Comedi core module
127 * which functions to call to configure/deconfigure (attach/detach)
128 * the board, and also about the kernel module that contains
131 static int pcidio_attach(struct comedi_device *dev,
132 struct comedi_devconfig *it);
133 static int pcidio_detach(struct comedi_device *dev);
134 static struct comedi_driver driver_cb_pcidio = {
135 .driver_name = "cb_pcidio",
136 .module = THIS_MODULE,
137 .attach = pcidio_attach,
138 .detach = pcidio_detach,
140 /* It is not necessary to implement the following members if you are
141 * writing a driver for a ISA PnP or PCI card */
143 /* Most drivers will support multiple types of boards by
144 * having an array of board structures. These were defined
145 * in pcidio_boards[] above. Note that the element 'name'
146 * was first in the structure -- Comedi uses this fact to
147 * extract the name of the board without knowing any details
148 * about the structure except for its length.
149 * When a device is attached (by comedi_config), the name
150 * of the device is given to Comedi, and Comedi tries to
151 * match it by going through the list of board names. If
152 * there is a match, the address of the pointer is put
153 * into dev->board_ptr and driver->attach() is called.
155 * Note that these are not necessary if you can determine
156 * the type of board in software. ISA PnP, PCI, and PCMCIA
157 * devices are such boards.
160 /* The following fields should NOT be initialized if you are dealing
163 * .board_name = pcidio_boards,
164 * .offset = sizeof(struct pcidio_board),
165 * .num_names = sizeof(pcidio_boards) / sizeof(structpcidio_board),
170 /*------------------------------- FUNCTIONS -----------------------------------*/
173 * Attach is called by the Comedi core to configure the driver
174 * for a particular board. If you specified a board_name array
175 * in the driver structure, dev->board_ptr contains that
178 static int pcidio_attach(struct comedi_device *dev, struct comedi_devconfig *it)
180 struct pci_dev *pcidev = NULL;
184 printk("comedi%d: cb_pcidio: \n", dev->minor);
187 * Allocate the private structure area. alloc_private() is a
188 * convenient macro defined in comedidev.h.
190 if (alloc_private(dev, sizeof(struct pcidio_private)) < 0)
193 * If you can probe the device to determine what device in a series
194 * it is, this is the place to do it. Otherwise, dev->board_ptr
195 * should already be initialized.
198 * Probe the device to determine what device in the series it is.
201 for (pcidev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, NULL);
203 pcidev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, pcidev)) {
204 /* is it not a computer boards card? */
205 if (pcidev->vendor != PCI_VENDOR_ID_CB)
207 /* loop through cards supported by this driver */
208 for (index = 0; index < ARRAY_SIZE(pcidio_boards); index++) {
209 if (pcidio_pci_table[index].device != pcidev->device)
212 /* was a particular bus/slot requested? */
213 if (it->options[0] || it->options[1]) {
214 /* are we on the wrong bus/slot? */
215 if (pcidev->bus->number != it->options[0] ||
216 PCI_SLOT(pcidev->devfn) != it->options[1]) {
220 dev->board_ptr = pcidio_boards + index;
225 printk("No supported ComputerBoards/MeasurementComputing card found on "
226 "requested position\n");
232 * Initialize dev->board_name. Note that we can use the "thisboard"
233 * macro now, since we just initialized it in the last line.
235 dev->board_name = thisboard->name;
237 devpriv->pci_dev = pcidev;
238 printk("Found %s on bus %i, slot %i\n", thisboard->name,
239 devpriv->pci_dev->bus->number,
240 PCI_SLOT(devpriv->pci_dev->devfn));
241 if (comedi_pci_enable(pcidev, thisboard->name)) {
243 ("cb_pcidio: failed to enable PCI device and request regions\n");
246 devpriv->dio_reg_base
248 pci_resource_start(devpriv->pci_dev,
249 pcidio_boards[index].dioregs_badrindex);
252 * Allocate the subdevice structures. alloc_subdevice() is a
253 * convenient macro defined in comedidev.h.
255 if (alloc_subdevices(dev, thisboard->n_8255) < 0)
258 for (i = 0; i < thisboard->n_8255; i++) {
259 subdev_8255_init(dev, dev->subdevices + i,
260 NULL, devpriv->dio_reg_base + i * 4);
261 printk(" subdev %d: base = 0x%lx\n", i,
262 devpriv->dio_reg_base + i * 4);
265 printk("attached\n");
270 * _detach is called to deconfigure a device. It should deallocate
272 * This function is also called when _attach() fails, so it should be
273 * careful not to release resources that were not necessarily
274 * allocated by _attach(). dev->private and dev->subdevices are
275 * deallocated automatically by the core.
277 static int pcidio_detach(struct comedi_device *dev)
279 printk("comedi%d: cb_pcidio: remove\n", dev->minor);
281 if (devpriv->pci_dev) {
282 if (devpriv->dio_reg_base) {
283 comedi_pci_disable(devpriv->pci_dev);
285 pci_dev_put(devpriv->pci_dev);
288 if (dev->subdevices) {
290 for (i = 0; i < thisboard->n_8255; i++) {
291 subdev_8255_cleanup(dev, dev->subdevices + i);
298 * A convenient macro that defines init_module() and cleanup_module(),
301 COMEDI_PCI_INITCLEANUP(driver_cb_pcidio, pcidio_pci_table);