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