linux-old/Documentation/pci.txt
<<
>>
Prefs
   1                         How To Write Linux PCI Drivers
   2
   3                   by Martin Mares <mj@ucw.cz> on 07-Feb-2000
   4
   5~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   6The world of PCI is vast and it's full of (mostly unpleasant) surprises.
   7Different PCI devices have different requirements and different bugs --
   8because of this, the PCI support layer in Linux kernel is not as trivial
   9as one would wish. This short pamphlet tries to help all potential driver
  10authors to find their way through the deep forests of PCI handling.
  11
  12
  130. Structure of PCI drivers
  14~~~~~~~~~~~~~~~~~~~~~~~~~~~
  15There exist two kinds of PCI drivers: new-style ones (which leave most of
  16probing for devices to the PCI layer and support online insertion and removal
  17of devices [thus supporting PCI, hot-pluggable PCI and CardBus in single
  18driver]) and old-style ones which just do all the probing themselves. Unless
  19you have a very good reason to do so, please don't use the old way of probing
  20in any new code. After the driver finds the devices it wishes to operate
  21on (either the old or the new way), it needs to perform the following steps:
  22
  23        Enable the device
  24        Access device configuration space
  25        Discover resources (addresses and IRQ numbers) provided by the device
  26        Allocate these resources
  27        Communicate with the device
  28
  29Most of these topics are covered by the following sections, for the rest
  30look at <linux/pci.h>, it's hopefully well commented.
  31
  32If the PCI subsystem is not configured (CONFIG_PCI is not set), most of
  33the functions described below are defined as inline functions either completely
  34empty or just returning an appropriate error codes to avoid lots of ifdefs
  35in the drivers.
  36
  37
  381. New-style drivers
  39~~~~~~~~~~~~~~~~~~~~
  40The new-style drivers just call pci_register_driver during their initialization
  41with a pointer to a structure describing the driver (struct pci_driver) which
  42contains:
  43
  44        name            Name of the driver
  45        id_table        Pointer to table of device ID's the driver is
  46                        interested in.  Most drivers should export this
  47                        table using MODULE_DEVICE_TABLE(pci,...).
  48                        Set to NULL to call probe() function for every
  49                        PCI device known to the system.
  50        probe           Pointer to a probing function which gets called (during
  51                        execution of pci_register_driver for already existing
  52                        devices or later if a new device gets inserted) for all
  53                        PCI devices which match the ID table and are not handled
  54                        by the other drivers yet. This function gets passed a pointer
  55                        to the pci_dev structure representing the device and also
  56                        which entry in the ID table did the device match. It returns
  57                        zero when the driver has accepted the device or an error
  58                        code (negative number) otherwise. This function always gets
  59                        called from process context, so it can sleep.
  60        remove          Pointer to a function which gets called whenever a device
  61                        being handled by this driver is removed (either during
  62                        deregistration of the driver or when it's manually pulled
  63                        out of a hot-pluggable slot). This function always gets
  64                        called from process context, so it can sleep.
  65        save_state      Save a device's state before it's suspend.
  66        suspend         Put device into low power state.
  67        resume          Wake device from low power state.
  68        enable_wake     Enable device to generate wake events from a low power state.
  69
  70                        (Please see Documentation/power/pci.txt for descriptions
  71                        of PCI Power Management and the related functions)
  72
  73The ID table is an array of struct pci_device_id ending with a all-zero entry.
  74Each entry consists of:
  75
  76        vendor, device  Vendor and device ID to match (or PCI_ANY_ID)
  77        subvendor,      Subsystem vendor and device ID to match (or PCI_ANY_ID)
  78        subdevice
  79        class,          Device class to match. The class_mask tells which bits
  80        class_mask      of the class are honored during the comparison.
  81        driver_data     Data private to the driver.
  82
  83When the driver exits, it just calls pci_unregister_driver() and the PCI layer
  84automatically calls the remove hook for all devices handled by the driver.
  85
  86Please mark the initialization and cleanup functions where appropriate
  87(the corresponding macros are defined in <linux/init.h>):
  88
  89        __init          Initialization code. Thrown away after the driver
  90                        initializes.
  91        __exit          Exit code. Ignored for non-modular drivers.
  92        __devinit       Device initialization code. Identical to __init if
  93                        the kernel is not compiled with CONFIG_HOTPLUG, normal
  94                        function otherwise.
  95        __devexit       The same for __exit.
  96
  97Tips:
  98        The module_init()/module_exit() functions (and all initialization
  99        functions called only from these) should be marked __init/exit.
 100        The struct pci_driver shouldn't be marked with any of these tags.
 101        The ID table array should be marked __devinitdata.
 102        The probe() and remove() functions (and all initialization
 103        functions called only from these) should be marked __devinit/exit.
 104        If you are sure the driver is not a hotplug driver then use only 
 105        __init/exit __initdata/exitdata.
 106
 107        Pointers to functions marked as __devexit must be created using
 108        __devexit_p(function_name).  That will generate the function
 109        name or NULL if the __devexit function will be discarded.
 110
 111
 1122. How to find PCI devices manually (the old style)
 113~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 114PCI drivers not using the pci_register_driver() interface search
 115for PCI devices manually using the following constructs:
 116
 117Searching by vendor and device ID:
 118
 119        struct pci_dev *dev = NULL;
 120        while (dev = pci_find_device(VENDOR_ID, DEVICE_ID, dev))
 121                configure_device(dev);
 122
 123Searching by class ID (iterate in a similar way):
 124
 125        pci_find_class(CLASS_ID, dev)
 126
 127Searching by both vendor/device and subsystem vendor/device ID:
 128
 129        pci_find_subsys(VENDOR_ID, DEVICE_ID, SUBSYS_VENDOR_ID, SUBSYS_DEVICE_ID, dev).
 130
 131   You can use the constant PCI_ANY_ID as a wildcard replacement for
 132VENDOR_ID or DEVICE_ID.  This allows searching for any device from a
 133specific vendor, for example.
 134
 135   In case you need to decide according to some more complex criteria,
 136you can walk the list of all known PCI devices yourself:
 137
 138        struct pci_dev *dev;
 139        pci_for_each_dev(dev) {
 140                ... do anything you want with dev ...
 141        }
 142
 143For compatibility with device ordering in older kernels, you can also
 144use pci_for_each_dev_reverse(dev) for walking the list in the opposite
 145direction.
 146
 147
 1483. Enabling devices
 149~~~~~~~~~~~~~~~~~~~
 150   Before you do anything with the device you've found, you need to enable
 151it by calling pci_enable_device() which enables I/O and memory regions of
 152the device, assigns missing resources if needed and wakes up the device
 153if it was in suspended state. Please note that this function can fail.
 154
 155   If you want to use the device in bus mastering mode, call pci_set_master()
 156which enables the bus master bit in PCI_COMMAND register and also fixes
 157the latency timer value if it's set to something bogus by the BIOS.
 158
 159
 1604. How to access PCI config space
 161~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 162   You can use pci_(read|write)_config_(byte|word|dword) to access the config
 163space of a device represented by struct pci_dev *. All these functions return 0
 164when successful or an error code (PCIBIOS_...) which can be translated to a text
 165string by pcibios_strerror. Most drivers expect that accesses to valid PCI
 166devices don't fail.
 167
 168   If you access fields in the standard portion of the config header, please
 169use symbolic names of locations and bits declared in <linux/pci.h>.
 170
 171   If you need to access Extended PCI Capability registers, just call
 172pci_find_capability() for the particular capability and it will find the
 173corresponding register block for you.
 174
 175
 1765. Addresses and interrupts
 177~~~~~~~~~~~~~~~~~~~~~~~~~~~
 178   Memory and port addresses and interrupt numbers should NOT be read from the
 179config space. You should use the values in the pci_dev structure as they might
 180have been remapped by the kernel.
 181
 182   See Documentation/IO-mapping.txt for how to access device memory.
 183
 184   You still need to call request_region() for I/O regions and request_mem_region()
 185for memory regions to make sure nobody else is using the same device.
 186
 187   All interrupt handlers should be registered with SA_SHIRQ and use the devid
 188to map IRQs to devices (remember that all PCI interrupts are shared).
 189
 190
 1916. Other interesting functions
 192~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 193pci_find_slot()                 Find pci_dev corresponding to given bus and
 194                                slot numbers.
 195pci_set_power_state()           Set PCI Power Management state (0=D0 ... 3=D3)
 196pci_find_capability()           Find specified capability in device's capability
 197                                list.
 198pci_module_init()               Inline helper function for ensuring correct
 199                                pci_driver initialization and error handling.
 200pci_resource_start()            Returns bus start address for a given PCI region
 201pci_resource_end()              Returns bus end address for a given PCI region
 202pci_resource_len()              Returns the byte length of a PCI region
 203pci_set_drvdata()               Set private driver data pointer for a pci_dev
 204pci_get_drvdata()               Return private driver data pointer for a pci_dev
 205
 206
 2077. Miscellaneous hints
 208~~~~~~~~~~~~~~~~~~~~~~
 209When displaying PCI slot names to the user (for example when a driver wants
 210to tell the user what card has it found), please use pci_dev->slot_name
 211for this purpose.
 212
 213Always refer to the PCI devices by a pointer to the pci_dev structure.
 214All PCI layer functions use this identification and it's the only
 215reasonable one. Don't use bus/slot/function numbers except for very
 216special purposes -- on systems with multiple primary buses their semantics
 217can be pretty complex.
 218
 219If you're going to use PCI bus mastering DMA, take a look at
 220Documentation/DMA-mapping.txt.
 221
 222
 2238. Obsolete functions
 224~~~~~~~~~~~~~~~~~~~~~
 225There are several functions kept only for compatibility with old drivers
 226not updated to the new PCI interface. Please don't use them in new code.
 227
 228pcibios_present()               Since ages, you don't need to test presence
 229                                of PCI subsystem when trying to talk with it.
 230                                If it's not there, the list of PCI devices
 231                                is empty and all functions for searching for
 232                                devices just return NULL.
 233pcibios_(read|write)_*          Superseded by their pci_(read|write)_*
 234                                counterparts.
 235pcibios_find_*                  Superseded by their pci_find_* counterparts.
 236
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.