| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | * drivers/pci/pci-driver.c | 
|  | 3 | * | 
| Greg Kroah-Hartman | 2b93730 | 2007-11-28 12:23:18 -0800 | [diff] [blame] | 4 | * (C) Copyright 2002-2004, 2007 Greg Kroah-Hartman <greg@kroah.com> | 
|  | 5 | * (C) Copyright 2007 Novell Inc. | 
|  | 6 | * | 
|  | 7 | * Released under the GPL v2 only. | 
|  | 8 | * | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | */ | 
|  | 10 |  | 
|  | 11 | #include <linux/pci.h> | 
|  | 12 | #include <linux/module.h> | 
|  | 13 | #include <linux/init.h> | 
|  | 14 | #include <linux/device.h> | 
| Andi Kleen | d42c699 | 2005-07-06 19:56:03 +0200 | [diff] [blame] | 15 | #include <linux/mempolicy.h> | 
| Tim Schmielau | 4e57b68 | 2005-10-30 15:03:48 -0800 | [diff] [blame] | 16 | #include <linux/string.h> | 
|  | 17 | #include <linux/slab.h> | 
| Tim Schmielau | 8c65b4a | 2005-11-07 00:59:43 -0800 | [diff] [blame] | 18 | #include <linux/sched.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include "pci.h" | 
|  | 20 |  | 
|  | 21 | /* | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | * Dynamic device IDs are disabled for !CONFIG_HOTPLUG | 
|  | 23 | */ | 
|  | 24 |  | 
| Greg Kroah-Hartman | 7586585 | 2005-06-30 02:18:12 -0700 | [diff] [blame] | 25 | struct pci_dynid { | 
|  | 26 | struct list_head node; | 
|  | 27 | struct pci_device_id id; | 
|  | 28 | }; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 |  | 
| Greg KH | 3d3c2ae | 2005-07-06 09:09:38 -0700 | [diff] [blame] | 30 | #ifdef CONFIG_HOTPLUG | 
|  | 31 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | /** | 
| Randy Dunlap | 8f7020d | 2005-10-23 11:57:38 -0700 | [diff] [blame] | 33 | * store_new_id - add a new PCI device ID to this driver and re-probe devices | 
|  | 34 | * @driver: target device driver | 
|  | 35 | * @buf: buffer for scanning device ID data | 
|  | 36 | * @count: input size | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | * | 
|  | 38 | * Adds a new dynamic pci device ID to this driver, | 
|  | 39 | * and causes the driver to probe for all devices again. | 
|  | 40 | */ | 
| Randy Dunlap | f8eb100 | 2005-10-28 20:36:51 -0700 | [diff] [blame] | 41 | static ssize_t | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | store_new_id(struct device_driver *driver, const char *buf, size_t count) | 
|  | 43 | { | 
| Greg Kroah-Hartman | 7586585 | 2005-06-30 02:18:12 -0700 | [diff] [blame] | 44 | struct pci_dynid *dynid; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | struct pci_driver *pdrv = to_pci_driver(driver); | 
| Jean Delvare | 6ba1863 | 2007-04-07 17:21:28 +0200 | [diff] [blame] | 46 | __u32 vendor, device, subvendor=PCI_ANY_ID, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | subdevice=PCI_ANY_ID, class=0, class_mask=0; | 
|  | 48 | unsigned long driver_data=0; | 
|  | 49 | int fields=0; | 
| Greg Kroah-Hartman | b19441a | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 50 | int retval = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 |  | 
|  | 52 | fields = sscanf(buf, "%x %x %x %x %x %x %lux", | 
|  | 53 | &vendor, &device, &subvendor, &subdevice, | 
|  | 54 | &class, &class_mask, &driver_data); | 
| Jean Delvare | 6ba1863 | 2007-04-07 17:21:28 +0200 | [diff] [blame] | 55 | if (fields < 2) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | return -EINVAL; | 
|  | 57 |  | 
| Eric Sesterhenn | f5afe80 | 2006-02-28 15:34:49 +0100 | [diff] [blame] | 58 | dynid = kzalloc(sizeof(*dynid), GFP_KERNEL); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | if (!dynid) | 
|  | 60 | return -ENOMEM; | 
|  | 61 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | dynid->id.vendor = vendor; | 
|  | 63 | dynid->id.device = device; | 
|  | 64 | dynid->id.subvendor = subvendor; | 
|  | 65 | dynid->id.subdevice = subdevice; | 
|  | 66 | dynid->id.class = class; | 
|  | 67 | dynid->id.class_mask = class_mask; | 
|  | 68 | dynid->id.driver_data = pdrv->dynids.use_driver_data ? | 
|  | 69 | driver_data : 0UL; | 
|  | 70 |  | 
|  | 71 | spin_lock(&pdrv->dynids.lock); | 
| Michael Ellerman | a56bc69 | 2007-09-14 15:33:13 +1000 | [diff] [blame] | 72 | list_add_tail(&dynid->node, &pdrv->dynids.list); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | spin_unlock(&pdrv->dynids.lock); | 
|  | 74 |  | 
| Greg Kroah-Hartman | 7586585 | 2005-06-30 02:18:12 -0700 | [diff] [blame] | 75 | if (get_driver(&pdrv->driver)) { | 
| Greg Kroah-Hartman | b19441a | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 76 | retval = driver_attach(&pdrv->driver); | 
| Greg Kroah-Hartman | 7586585 | 2005-06-30 02:18:12 -0700 | [diff] [blame] | 77 | put_driver(&pdrv->driver); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | } | 
|  | 79 |  | 
| Greg Kroah-Hartman | b19441a | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 80 | if (retval) | 
|  | 81 | return retval; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | return count; | 
|  | 83 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | static DRIVER_ATTR(new_id, S_IWUSR, NULL, store_new_id); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 |  | 
|  | 86 | static void | 
|  | 87 | pci_free_dynids(struct pci_driver *drv) | 
|  | 88 | { | 
| Greg Kroah-Hartman | 7586585 | 2005-06-30 02:18:12 -0700 | [diff] [blame] | 89 | struct pci_dynid *dynid, *n; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 |  | 
|  | 91 | spin_lock(&drv->dynids.lock); | 
| Greg Kroah-Hartman | 7586585 | 2005-06-30 02:18:12 -0700 | [diff] [blame] | 92 | list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | list_del(&dynid->node); | 
|  | 94 | kfree(dynid); | 
|  | 95 | } | 
|  | 96 | spin_unlock(&drv->dynids.lock); | 
|  | 97 | } | 
|  | 98 |  | 
|  | 99 | static int | 
|  | 100 | pci_create_newid_file(struct pci_driver *drv) | 
|  | 101 | { | 
|  | 102 | int error = 0; | 
|  | 103 | if (drv->probe != NULL) | 
| Greg Kroah-Hartman | 03d43b1 | 2007-11-28 12:23:18 -0800 | [diff] [blame] | 104 | error = driver_create_file(&drv->driver, &driver_attr_new_id); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | return error; | 
|  | 106 | } | 
|  | 107 |  | 
| Greg Kroah-Hartman | 03d43b1 | 2007-11-28 12:23:18 -0800 | [diff] [blame] | 108 | static void pci_remove_newid_file(struct pci_driver *drv) | 
|  | 109 | { | 
|  | 110 | driver_remove_file(&drv->driver, &driver_attr_new_id); | 
|  | 111 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | #else /* !CONFIG_HOTPLUG */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | static inline void pci_free_dynids(struct pci_driver *drv) {} | 
|  | 114 | static inline int pci_create_newid_file(struct pci_driver *drv) | 
|  | 115 | { | 
|  | 116 | return 0; | 
|  | 117 | } | 
| Greg Kroah-Hartman | 03d43b1 | 2007-11-28 12:23:18 -0800 | [diff] [blame] | 118 | static inline void pci_remove_newid_file(struct pci_driver *drv) {} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | #endif | 
|  | 120 |  | 
|  | 121 | /** | 
| Greg Kroah-Hartman | 7586585 | 2005-06-30 02:18:12 -0700 | [diff] [blame] | 122 | * pci_match_id - See if a pci device matches a given pci_id table | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | * @ids: array of PCI device id structures to search in | 
| Greg Kroah-Hartman | 7586585 | 2005-06-30 02:18:12 -0700 | [diff] [blame] | 124 | * @dev: the PCI device structure to match against. | 
|  | 125 | * | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | * Used by a driver to check whether a PCI device present in the | 
| Greg Kroah-Hartman | 7586585 | 2005-06-30 02:18:12 -0700 | [diff] [blame] | 127 | * system is in its list of supported devices.  Returns the matching | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | * pci_device_id structure or %NULL if there is no match. | 
| Greg Kroah-Hartman | 7586585 | 2005-06-30 02:18:12 -0700 | [diff] [blame] | 129 | * | 
| Randy Dunlap | 8b60756 | 2007-05-09 07:19:14 +0200 | [diff] [blame] | 130 | * Deprecated, don't use this as it will not catch any dynamic ids | 
| Greg Kroah-Hartman | 7586585 | 2005-06-30 02:18:12 -0700 | [diff] [blame] | 131 | * that a driver might want to check for. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | */ | 
| Greg Kroah-Hartman | 7586585 | 2005-06-30 02:18:12 -0700 | [diff] [blame] | 133 | const struct pci_device_id *pci_match_id(const struct pci_device_id *ids, | 
|  | 134 | struct pci_dev *dev) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | { | 
| Greg Kroah-Hartman | 7586585 | 2005-06-30 02:18:12 -0700 | [diff] [blame] | 136 | if (ids) { | 
|  | 137 | while (ids->vendor || ids->subvendor || ids->class_mask) { | 
|  | 138 | if (pci_match_one_device(ids, dev)) | 
|  | 139 | return ids; | 
|  | 140 | ids++; | 
|  | 141 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | } | 
|  | 143 | return NULL; | 
|  | 144 | } | 
|  | 145 |  | 
|  | 146 | /** | 
| Randy Dunlap | ae9608a | 2007-01-09 21:41:01 -0800 | [diff] [blame] | 147 | * pci_match_device - Tell if a PCI device structure has a matching PCI device id structure | 
| Greg Kroah-Hartman | 7586585 | 2005-06-30 02:18:12 -0700 | [diff] [blame] | 148 | * @drv: the PCI driver to match against | 
| Henrik Kretzschmar | 39ba487 | 2006-08-15 10:57:16 +0200 | [diff] [blame] | 149 | * @dev: the PCI device structure to match against | 
| Greg Kroah-Hartman | 7586585 | 2005-06-30 02:18:12 -0700 | [diff] [blame] | 150 | * | 
|  | 151 | * Used by a driver to check whether a PCI device present in the | 
|  | 152 | * system is in its list of supported devices.  Returns the matching | 
|  | 153 | * pci_device_id structure or %NULL if there is no match. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | */ | 
| Adrian Bunk | d73460d | 2007-10-24 18:27:18 +0200 | [diff] [blame] | 155 | static const struct pci_device_id *pci_match_device(struct pci_driver *drv, | 
|  | 156 | struct pci_dev *dev) | 
| Greg Kroah-Hartman | 7586585 | 2005-06-30 02:18:12 -0700 | [diff] [blame] | 157 | { | 
| Greg Kroah-Hartman | 7586585 | 2005-06-30 02:18:12 -0700 | [diff] [blame] | 158 | struct pci_dynid *dynid; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 |  | 
| Russell King | 7461b60 | 2006-11-29 21:18:04 +0000 | [diff] [blame] | 160 | /* Look at the dynamic ids first, before the static ones */ | 
| Greg Kroah-Hartman | 7586585 | 2005-06-30 02:18:12 -0700 | [diff] [blame] | 161 | spin_lock(&drv->dynids.lock); | 
|  | 162 | list_for_each_entry(dynid, &drv->dynids.list, node) { | 
|  | 163 | if (pci_match_one_device(&dynid->id, dev)) { | 
|  | 164 | spin_unlock(&drv->dynids.lock); | 
|  | 165 | return &dynid->id; | 
|  | 166 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | } | 
| Greg Kroah-Hartman | 7586585 | 2005-06-30 02:18:12 -0700 | [diff] [blame] | 168 | spin_unlock(&drv->dynids.lock); | 
| Russell King | 7461b60 | 2006-11-29 21:18:04 +0000 | [diff] [blame] | 169 |  | 
|  | 170 | return pci_match_id(drv->id_table, dev); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | } | 
|  | 172 |  | 
| Andi Kleen | d42c699 | 2005-07-06 19:56:03 +0200 | [diff] [blame] | 173 | static int pci_call_probe(struct pci_driver *drv, struct pci_dev *dev, | 
|  | 174 | const struct pci_device_id *id) | 
|  | 175 | { | 
|  | 176 | int error; | 
|  | 177 | #ifdef CONFIG_NUMA | 
|  | 178 | /* Execute driver initialization on node where the | 
|  | 179 | device's bus is attached to.  This way the driver likely | 
|  | 180 | allocates its local memory on the right node without | 
|  | 181 | any need to change it. */ | 
|  | 182 | struct mempolicy *oldpol; | 
|  | 183 | cpumask_t oldmask = current->cpus_allowed; | 
| Yinghai Lu | 4efeb4d | 2008-05-12 21:21:05 +0200 | [diff] [blame] | 184 | int node = dev_to_node(&dev->dev); | 
| Mike Travis | f70316d | 2008-04-04 18:11:06 -0700 | [diff] [blame] | 185 |  | 
|  | 186 | if (node >= 0) { | 
|  | 187 | node_to_cpumask_ptr(nodecpumask, node); | 
|  | 188 | set_cpus_allowed_ptr(current, nodecpumask); | 
|  | 189 | } | 
| Andi Kleen | d42c699 | 2005-07-06 19:56:03 +0200 | [diff] [blame] | 190 | /* And set default memory allocation policy */ | 
|  | 191 | oldpol = current->mempolicy; | 
| Lee Schermerhorn | 74e27e4 | 2007-11-21 15:07:05 -0800 | [diff] [blame] | 192 | current->mempolicy = NULL;	/* fall back to system default policy */ | 
| Andi Kleen | d42c699 | 2005-07-06 19:56:03 +0200 | [diff] [blame] | 193 | #endif | 
|  | 194 | error = drv->probe(dev, id); | 
|  | 195 | #ifdef CONFIG_NUMA | 
| Mike Travis | f70316d | 2008-04-04 18:11:06 -0700 | [diff] [blame] | 196 | set_cpus_allowed_ptr(current, &oldmask); | 
| Andi Kleen | d42c699 | 2005-07-06 19:56:03 +0200 | [diff] [blame] | 197 | current->mempolicy = oldpol; | 
|  | 198 | #endif | 
|  | 199 | return error; | 
|  | 200 | } | 
|  | 201 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | /** | 
|  | 203 | * __pci_device_probe() | 
| Randy Dunlap | 8f7020d | 2005-10-23 11:57:38 -0700 | [diff] [blame] | 204 | * @drv: driver to call to check if it wants the PCI device | 
|  | 205 | * @pci_dev: PCI device being probed | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | * | 
| Randy Dunlap | 8f7020d | 2005-10-23 11:57:38 -0700 | [diff] [blame] | 207 | * returns 0 on success, else error. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | * side-effect: pci_dev->driver is set to drv when drv claims pci_dev. | 
|  | 209 | */ | 
|  | 210 | static int | 
|  | 211 | __pci_device_probe(struct pci_driver *drv, struct pci_dev *pci_dev) | 
| Greg Kroah-Hartman | 7586585 | 2005-06-30 02:18:12 -0700 | [diff] [blame] | 212 | { | 
|  | 213 | const struct pci_device_id *id; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | int error = 0; | 
|  | 215 |  | 
|  | 216 | if (!pci_dev->driver && drv->probe) { | 
| Greg Kroah-Hartman | 7586585 | 2005-06-30 02:18:12 -0700 | [diff] [blame] | 217 | error = -ENODEV; | 
|  | 218 |  | 
|  | 219 | id = pci_match_device(drv, pci_dev); | 
|  | 220 | if (id) | 
| Andi Kleen | d42c699 | 2005-07-06 19:56:03 +0200 | [diff] [blame] | 221 | error = pci_call_probe(drv, pci_dev, id); | 
| Greg Kroah-Hartman | 7586585 | 2005-06-30 02:18:12 -0700 | [diff] [blame] | 222 | if (error >= 0) { | 
|  | 223 | pci_dev->driver = drv; | 
|  | 224 | error = 0; | 
|  | 225 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | } | 
|  | 227 | return error; | 
|  | 228 | } | 
|  | 229 |  | 
|  | 230 | static int pci_device_probe(struct device * dev) | 
|  | 231 | { | 
|  | 232 | int error = 0; | 
|  | 233 | struct pci_driver *drv; | 
|  | 234 | struct pci_dev *pci_dev; | 
|  | 235 |  | 
|  | 236 | drv = to_pci_driver(dev->driver); | 
|  | 237 | pci_dev = to_pci_dev(dev); | 
|  | 238 | pci_dev_get(pci_dev); | 
|  | 239 | error = __pci_device_probe(drv, pci_dev); | 
|  | 240 | if (error) | 
|  | 241 | pci_dev_put(pci_dev); | 
|  | 242 |  | 
|  | 243 | return error; | 
|  | 244 | } | 
|  | 245 |  | 
|  | 246 | static int pci_device_remove(struct device * dev) | 
|  | 247 | { | 
|  | 248 | struct pci_dev * pci_dev = to_pci_dev(dev); | 
|  | 249 | struct pci_driver * drv = pci_dev->driver; | 
|  | 250 |  | 
|  | 251 | if (drv) { | 
|  | 252 | if (drv->remove) | 
|  | 253 | drv->remove(pci_dev); | 
|  | 254 | pci_dev->driver = NULL; | 
|  | 255 | } | 
|  | 256 |  | 
|  | 257 | /* | 
| Shaohua Li | 2449e06 | 2006-10-20 14:45:32 -0700 | [diff] [blame] | 258 | * If the device is still on, set the power state as "unknown", | 
|  | 259 | * since it might change by the next time we load the driver. | 
|  | 260 | */ | 
|  | 261 | if (pci_dev->current_state == PCI_D0) | 
|  | 262 | pci_dev->current_state = PCI_UNKNOWN; | 
|  | 263 |  | 
|  | 264 | /* | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | * We would love to complain here if pci_dev->is_enabled is set, that | 
|  | 266 | * the driver should have called pci_disable_device(), but the | 
|  | 267 | * unfortunate fact is there are too many odd BIOS and bridge setups | 
|  | 268 | * that don't like drivers doing that all of the time. | 
|  | 269 | * Oh well, we can dream of sane hardware when we sleep, no matter how | 
|  | 270 | * horrible the crap we have to deal with is when we are awake... | 
|  | 271 | */ | 
|  | 272 |  | 
|  | 273 | pci_dev_put(pci_dev); | 
|  | 274 | return 0; | 
|  | 275 | } | 
|  | 276 |  | 
| Rafael J. Wysocki | bbb44d9 | 2008-05-20 00:49:04 +0200 | [diff] [blame] | 277 | static void pci_device_shutdown(struct device *dev) | 
|  | 278 | { | 
|  | 279 | struct pci_dev *pci_dev = to_pci_dev(dev); | 
|  | 280 | struct pci_driver *drv = pci_dev->driver; | 
|  | 281 |  | 
|  | 282 | if (drv && drv->shutdown) | 
|  | 283 | drv->shutdown(pci_dev); | 
|  | 284 | pci_msi_shutdown(pci_dev); | 
|  | 285 | pci_msix_shutdown(pci_dev); | 
|  | 286 | } | 
|  | 287 |  | 
|  | 288 | #ifdef CONFIG_PM_SLEEP | 
|  | 289 |  | 
|  | 290 | /* | 
|  | 291 | * Default "suspend" method for devices that have no driver provided suspend, | 
|  | 292 | * or not even a driver at all. | 
|  | 293 | */ | 
|  | 294 | static void pci_default_pm_suspend(struct pci_dev *pci_dev) | 
|  | 295 | { | 
|  | 296 | pci_save_state(pci_dev); | 
|  | 297 | /* | 
|  | 298 | * mark its power state as "unknown", since we don't know if | 
|  | 299 | * e.g. the BIOS will change its device state when we suspend. | 
|  | 300 | */ | 
|  | 301 | if (pci_dev->current_state == PCI_D0) | 
|  | 302 | pci_dev->current_state = PCI_UNKNOWN; | 
|  | 303 | } | 
|  | 304 |  | 
|  | 305 | /* | 
|  | 306 | * Default "resume" method for devices that have no driver provided resume, | 
|  | 307 | * or not even a driver at all. | 
|  | 308 | */ | 
|  | 309 | static int pci_default_pm_resume(struct pci_dev *pci_dev) | 
|  | 310 | { | 
|  | 311 | int retval = 0; | 
|  | 312 |  | 
|  | 313 | /* restore the PCI config space */ | 
|  | 314 | pci_restore_state(pci_dev); | 
|  | 315 | /* if the device was enabled before suspend, reenable */ | 
|  | 316 | retval = pci_reenable_device(pci_dev); | 
|  | 317 | /* | 
|  | 318 | * if the device was busmaster before the suspend, make it busmaster | 
|  | 319 | * again | 
|  | 320 | */ | 
|  | 321 | if (pci_dev->is_busmaster) | 
|  | 322 | pci_set_master(pci_dev); | 
|  | 323 |  | 
|  | 324 | return retval; | 
|  | 325 | } | 
|  | 326 |  | 
|  | 327 | static int pci_legacy_suspend(struct device *dev, pm_message_t state) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | { | 
|  | 329 | struct pci_dev * pci_dev = to_pci_dev(dev); | 
|  | 330 | struct pci_driver * drv = pci_dev->driver; | 
|  | 331 | int i = 0; | 
|  | 332 |  | 
| Andrew Morton | 0266949 | 2006-03-23 01:38:34 -0800 | [diff] [blame] | 333 | if (drv && drv->suspend) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | i = drv->suspend(pci_dev, state); | 
| Andrew Morton | 0266949 | 2006-03-23 01:38:34 -0800 | [diff] [blame] | 335 | suspend_report_result(drv->suspend, i); | 
|  | 336 | } else { | 
| Rafael J. Wysocki | bbb44d9 | 2008-05-20 00:49:04 +0200 | [diff] [blame] | 337 | pci_default_pm_suspend(pci_dev); | 
| Andrew Morton | 0266949 | 2006-03-23 01:38:34 -0800 | [diff] [blame] | 338 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | return i; | 
|  | 340 | } | 
|  | 341 |  | 
| Rafael J. Wysocki | bbb44d9 | 2008-05-20 00:49:04 +0200 | [diff] [blame] | 342 | static int pci_legacy_suspend_late(struct device *dev, pm_message_t state) | 
| Linus Torvalds | cbd69db | 2006-06-24 14:50:29 -0700 | [diff] [blame] | 343 | { | 
|  | 344 | struct pci_dev * pci_dev = to_pci_dev(dev); | 
|  | 345 | struct pci_driver * drv = pci_dev->driver; | 
|  | 346 | int i = 0; | 
|  | 347 |  | 
|  | 348 | if (drv && drv->suspend_late) { | 
|  | 349 | i = drv->suspend_late(pci_dev, state); | 
|  | 350 | suspend_report_result(drv->suspend_late, i); | 
|  | 351 | } | 
|  | 352 | return i; | 
|  | 353 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 |  | 
| Rafael J. Wysocki | bbb44d9 | 2008-05-20 00:49:04 +0200 | [diff] [blame] | 355 | static int pci_legacy_resume(struct device *dev) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | { | 
| Jean Delvare | 8d92bc2 | 2006-04-18 14:49:56 +0200 | [diff] [blame] | 357 | int error; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | struct pci_dev * pci_dev = to_pci_dev(dev); | 
|  | 359 | struct pci_driver * drv = pci_dev->driver; | 
|  | 360 |  | 
|  | 361 | if (drv && drv->resume) | 
| Jean Delvare | 8d92bc2 | 2006-04-18 14:49:56 +0200 | [diff] [blame] | 362 | error = drv->resume(pci_dev); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | else | 
| Rafael J. Wysocki | bbb44d9 | 2008-05-20 00:49:04 +0200 | [diff] [blame] | 364 | error = pci_default_pm_resume(pci_dev); | 
| Jean Delvare | 8d92bc2 | 2006-04-18 14:49:56 +0200 | [diff] [blame] | 365 | return error; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | } | 
|  | 367 |  | 
| Rafael J. Wysocki | bbb44d9 | 2008-05-20 00:49:04 +0200 | [diff] [blame] | 368 | static int pci_legacy_resume_early(struct device *dev) | 
| Linus Torvalds | cbd69db | 2006-06-24 14:50:29 -0700 | [diff] [blame] | 369 | { | 
|  | 370 | int error = 0; | 
|  | 371 | struct pci_dev * pci_dev = to_pci_dev(dev); | 
|  | 372 | struct pci_driver * drv = pci_dev->driver; | 
|  | 373 |  | 
|  | 374 | if (drv && drv->resume_early) | 
|  | 375 | error = drv->resume_early(pci_dev); | 
|  | 376 | return error; | 
|  | 377 | } | 
|  | 378 |  | 
| Rafael J. Wysocki | bbb44d9 | 2008-05-20 00:49:04 +0200 | [diff] [blame] | 379 | static int pci_pm_prepare(struct device *dev) | 
|  | 380 | { | 
|  | 381 | struct device_driver *drv = dev->driver; | 
|  | 382 | int error = 0; | 
|  | 383 |  | 
|  | 384 | if (drv && drv->pm && drv->pm->prepare) | 
|  | 385 | error = drv->pm->prepare(dev); | 
|  | 386 |  | 
|  | 387 | return error; | 
|  | 388 | } | 
|  | 389 |  | 
|  | 390 | static void pci_pm_complete(struct device *dev) | 
|  | 391 | { | 
|  | 392 | struct device_driver *drv = dev->driver; | 
|  | 393 |  | 
|  | 394 | if (drv && drv->pm && drv->pm->complete) | 
|  | 395 | drv->pm->complete(dev); | 
|  | 396 | } | 
|  | 397 |  | 
|  | 398 | #ifdef CONFIG_SUSPEND | 
|  | 399 |  | 
|  | 400 | static int pci_pm_suspend(struct device *dev) | 
|  | 401 | { | 
|  | 402 | struct pci_dev *pci_dev = to_pci_dev(dev); | 
|  | 403 | struct device_driver *drv = dev->driver; | 
|  | 404 | int error = 0; | 
|  | 405 |  | 
|  | 406 | if (drv && drv->pm) { | 
|  | 407 | if (drv->pm->suspend) { | 
|  | 408 | error = drv->pm->suspend(dev); | 
|  | 409 | suspend_report_result(drv->pm->suspend, error); | 
|  | 410 | } else { | 
|  | 411 | pci_default_pm_suspend(pci_dev); | 
|  | 412 | } | 
|  | 413 | } else { | 
|  | 414 | error = pci_legacy_suspend(dev, PMSG_SUSPEND); | 
|  | 415 | } | 
|  | 416 | pci_fixup_device(pci_fixup_suspend, pci_dev); | 
|  | 417 |  | 
|  | 418 | return error; | 
|  | 419 | } | 
|  | 420 |  | 
|  | 421 | static int pci_pm_suspend_noirq(struct device *dev) | 
| Greg KH | c895817 | 2005-04-08 14:53:31 +0900 | [diff] [blame] | 422 | { | 
|  | 423 | struct pci_dev *pci_dev = to_pci_dev(dev); | 
|  | 424 | struct pci_driver *drv = pci_dev->driver; | 
| Rafael J. Wysocki | bbb44d9 | 2008-05-20 00:49:04 +0200 | [diff] [blame] | 425 | int error = 0; | 
| Greg KH | c895817 | 2005-04-08 14:53:31 +0900 | [diff] [blame] | 426 |  | 
| Rafael J. Wysocki | bbb44d9 | 2008-05-20 00:49:04 +0200 | [diff] [blame] | 427 | if (drv && drv->pm) { | 
|  | 428 | if (drv->pm->suspend_noirq) { | 
|  | 429 | error = drv->pm->suspend_noirq(dev); | 
|  | 430 | suspend_report_result(drv->pm->suspend_noirq, error); | 
|  | 431 | } | 
|  | 432 | } else { | 
|  | 433 | error = pci_legacy_suspend_late(dev, PMSG_SUSPEND); | 
|  | 434 | } | 
|  | 435 |  | 
|  | 436 | return error; | 
| Greg KH | c895817 | 2005-04-08 14:53:31 +0900 | [diff] [blame] | 437 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 |  | 
| Rafael J. Wysocki | bbb44d9 | 2008-05-20 00:49:04 +0200 | [diff] [blame] | 439 | static int pci_pm_resume(struct device *dev) | 
|  | 440 | { | 
|  | 441 | struct pci_dev *pci_dev = to_pci_dev(dev); | 
|  | 442 | struct device_driver *drv = dev->driver; | 
|  | 443 | int error; | 
|  | 444 |  | 
|  | 445 | pci_fixup_device(pci_fixup_resume, pci_dev); | 
|  | 446 |  | 
|  | 447 | if (drv && drv->pm) { | 
|  | 448 | error = drv->pm->resume ? drv->pm->resume(dev) : | 
|  | 449 | pci_default_pm_resume(pci_dev); | 
|  | 450 | } else { | 
|  | 451 | error = pci_legacy_resume(dev); | 
|  | 452 | } | 
|  | 453 |  | 
|  | 454 | return error; | 
|  | 455 | } | 
|  | 456 |  | 
|  | 457 | static int pci_pm_resume_noirq(struct device *dev) | 
|  | 458 | { | 
|  | 459 | struct pci_dev *pci_dev = to_pci_dev(dev); | 
|  | 460 | struct pci_driver *drv = pci_dev->driver; | 
|  | 461 | int error = 0; | 
|  | 462 |  | 
|  | 463 | pci_fixup_device(pci_fixup_resume_early, pci_dev); | 
|  | 464 |  | 
|  | 465 | if (drv && drv->pm) { | 
|  | 466 | if (drv->pm->resume_noirq) | 
|  | 467 | error = drv->pm->resume_noirq(dev); | 
|  | 468 | } else { | 
|  | 469 | error = pci_legacy_resume_early(dev); | 
|  | 470 | } | 
|  | 471 |  | 
|  | 472 | return error; | 
|  | 473 | } | 
|  | 474 |  | 
|  | 475 | #else /* !CONFIG_SUSPEND */ | 
|  | 476 |  | 
|  | 477 | #define pci_pm_suspend		NULL | 
|  | 478 | #define pci_pm_suspend_noirq	NULL | 
|  | 479 | #define pci_pm_resume		NULL | 
|  | 480 | #define pci_pm_resume_noirq	NULL | 
|  | 481 |  | 
|  | 482 | #endif /* !CONFIG_SUSPEND */ | 
|  | 483 |  | 
|  | 484 | #ifdef CONFIG_HIBERNATION | 
|  | 485 |  | 
|  | 486 | static int pci_pm_freeze(struct device *dev) | 
|  | 487 | { | 
|  | 488 | struct pci_dev *pci_dev = to_pci_dev(dev); | 
|  | 489 | struct device_driver *drv = dev->driver; | 
|  | 490 | int error = 0; | 
|  | 491 |  | 
|  | 492 | if (drv && drv->pm) { | 
|  | 493 | if (drv->pm->freeze) { | 
|  | 494 | error = drv->pm->freeze(dev); | 
|  | 495 | suspend_report_result(drv->pm->freeze, error); | 
|  | 496 | } else { | 
|  | 497 | pci_default_pm_suspend(pci_dev); | 
|  | 498 | } | 
|  | 499 | } else { | 
|  | 500 | error = pci_legacy_suspend(dev, PMSG_FREEZE); | 
|  | 501 | pci_fixup_device(pci_fixup_suspend, pci_dev); | 
|  | 502 | } | 
|  | 503 |  | 
|  | 504 | return error; | 
|  | 505 | } | 
|  | 506 |  | 
|  | 507 | static int pci_pm_freeze_noirq(struct device *dev) | 
|  | 508 | { | 
|  | 509 | struct pci_dev *pci_dev = to_pci_dev(dev); | 
|  | 510 | struct pci_driver *drv = pci_dev->driver; | 
|  | 511 | int error = 0; | 
|  | 512 |  | 
|  | 513 | if (drv && drv->pm) { | 
|  | 514 | if (drv->pm->freeze_noirq) { | 
|  | 515 | error = drv->pm->freeze_noirq(dev); | 
|  | 516 | suspend_report_result(drv->pm->freeze_noirq, error); | 
|  | 517 | } | 
|  | 518 | } else { | 
|  | 519 | error = pci_legacy_suspend_late(dev, PMSG_FREEZE); | 
|  | 520 | } | 
|  | 521 |  | 
|  | 522 | return error; | 
|  | 523 | } | 
|  | 524 |  | 
|  | 525 | static int pci_pm_thaw(struct device *dev) | 
|  | 526 | { | 
|  | 527 | struct device_driver *drv = dev->driver; | 
|  | 528 | int error = 0; | 
|  | 529 |  | 
|  | 530 | if (drv && drv->pm) { | 
|  | 531 | if (drv->pm->thaw) | 
|  | 532 | error =  drv->pm->thaw(dev); | 
|  | 533 | } else { | 
|  | 534 | pci_fixup_device(pci_fixup_resume, to_pci_dev(dev)); | 
|  | 535 | error = pci_legacy_resume(dev); | 
|  | 536 | } | 
|  | 537 |  | 
|  | 538 | return error; | 
|  | 539 | } | 
|  | 540 |  | 
|  | 541 | static int pci_pm_thaw_noirq(struct device *dev) | 
|  | 542 | { | 
|  | 543 | struct pci_dev *pci_dev = to_pci_dev(dev); | 
|  | 544 | struct pci_driver *drv = pci_dev->driver; | 
|  | 545 | int error = 0; | 
|  | 546 |  | 
|  | 547 | if (drv && drv->pm) { | 
|  | 548 | if (drv->pm->thaw_noirq) | 
|  | 549 | error = drv->pm->thaw_noirq(dev); | 
|  | 550 | } else { | 
|  | 551 | pci_fixup_device(pci_fixup_resume_early, pci_dev); | 
|  | 552 | error = pci_legacy_resume_early(dev); | 
|  | 553 | } | 
|  | 554 |  | 
|  | 555 | return error; | 
|  | 556 | } | 
|  | 557 |  | 
|  | 558 | static int pci_pm_poweroff(struct device *dev) | 
|  | 559 | { | 
|  | 560 | struct device_driver *drv = dev->driver; | 
|  | 561 | int error = 0; | 
|  | 562 |  | 
|  | 563 | pci_fixup_device(pci_fixup_suspend, to_pci_dev(dev)); | 
|  | 564 |  | 
|  | 565 | if (drv && drv->pm) { | 
|  | 566 | if (drv->pm->poweroff) { | 
|  | 567 | error = drv->pm->poweroff(dev); | 
|  | 568 | suspend_report_result(drv->pm->poweroff, error); | 
|  | 569 | } | 
|  | 570 | } else { | 
|  | 571 | error = pci_legacy_suspend(dev, PMSG_HIBERNATE); | 
|  | 572 | } | 
|  | 573 |  | 
|  | 574 | return error; | 
|  | 575 | } | 
|  | 576 |  | 
|  | 577 | static int pci_pm_poweroff_noirq(struct device *dev) | 
|  | 578 | { | 
|  | 579 | struct pci_dev *pci_dev = to_pci_dev(dev); | 
|  | 580 | struct pci_driver *drv = pci_dev->driver; | 
|  | 581 | int error = 0; | 
|  | 582 |  | 
|  | 583 | if (drv && drv->pm) { | 
|  | 584 | if (drv->pm->poweroff_noirq) { | 
|  | 585 | error = drv->pm->poweroff_noirq(dev); | 
|  | 586 | suspend_report_result(drv->pm->poweroff_noirq, error); | 
|  | 587 | } | 
|  | 588 | } else { | 
|  | 589 | error = pci_legacy_suspend_late(dev, PMSG_HIBERNATE); | 
|  | 590 | } | 
|  | 591 |  | 
|  | 592 | return error; | 
|  | 593 | } | 
|  | 594 |  | 
|  | 595 | static int pci_pm_restore(struct device *dev) | 
|  | 596 | { | 
|  | 597 | struct pci_dev *pci_dev = to_pci_dev(dev); | 
|  | 598 | struct device_driver *drv = dev->driver; | 
|  | 599 | int error; | 
|  | 600 |  | 
|  | 601 | if (drv && drv->pm) { | 
|  | 602 | error = drv->pm->restore ? drv->pm->restore(dev) : | 
|  | 603 | pci_default_pm_resume(pci_dev); | 
|  | 604 | } else { | 
|  | 605 | error = pci_legacy_resume(dev); | 
|  | 606 | } | 
|  | 607 | pci_fixup_device(pci_fixup_resume, pci_dev); | 
|  | 608 |  | 
|  | 609 | return error; | 
|  | 610 | } | 
|  | 611 |  | 
|  | 612 | static int pci_pm_restore_noirq(struct device *dev) | 
|  | 613 | { | 
|  | 614 | struct pci_dev *pci_dev = to_pci_dev(dev); | 
|  | 615 | struct pci_driver *drv = pci_dev->driver; | 
|  | 616 | int error = 0; | 
|  | 617 |  | 
|  | 618 | pci_fixup_device(pci_fixup_resume, pci_dev); | 
|  | 619 |  | 
|  | 620 | if (drv && drv->pm) { | 
|  | 621 | if (drv->pm->restore_noirq) | 
|  | 622 | error = drv->pm->restore_noirq(dev); | 
|  | 623 | } else { | 
|  | 624 | error = pci_legacy_resume_early(dev); | 
|  | 625 | } | 
|  | 626 | pci_fixup_device(pci_fixup_resume_early, pci_dev); | 
|  | 627 |  | 
|  | 628 | return error; | 
|  | 629 | } | 
|  | 630 |  | 
|  | 631 | #else /* !CONFIG_HIBERNATION */ | 
|  | 632 |  | 
|  | 633 | #define pci_pm_freeze		NULL | 
|  | 634 | #define pci_pm_freeze_noirq	NULL | 
|  | 635 | #define pci_pm_thaw		NULL | 
|  | 636 | #define pci_pm_thaw_noirq	NULL | 
|  | 637 | #define pci_pm_poweroff		NULL | 
|  | 638 | #define pci_pm_poweroff_noirq	NULL | 
|  | 639 | #define pci_pm_restore		NULL | 
|  | 640 | #define pci_pm_restore_noirq	NULL | 
|  | 641 |  | 
|  | 642 | #endif /* !CONFIG_HIBERNATION */ | 
|  | 643 |  | 
|  | 644 | struct pm_ext_ops pci_pm_ops = { | 
|  | 645 | .base = { | 
|  | 646 | .prepare = pci_pm_prepare, | 
|  | 647 | .complete = pci_pm_complete, | 
|  | 648 | .suspend = pci_pm_suspend, | 
|  | 649 | .resume = pci_pm_resume, | 
|  | 650 | .freeze = pci_pm_freeze, | 
|  | 651 | .thaw = pci_pm_thaw, | 
|  | 652 | .poweroff = pci_pm_poweroff, | 
|  | 653 | .restore = pci_pm_restore, | 
|  | 654 | }, | 
|  | 655 | .suspend_noirq = pci_pm_suspend_noirq, | 
|  | 656 | .resume_noirq = pci_pm_resume_noirq, | 
|  | 657 | .freeze_noirq = pci_pm_freeze_noirq, | 
|  | 658 | .thaw_noirq = pci_pm_thaw_noirq, | 
|  | 659 | .poweroff_noirq = pci_pm_poweroff_noirq, | 
|  | 660 | .restore_noirq = pci_pm_restore_noirq, | 
|  | 661 | }; | 
|  | 662 |  | 
|  | 663 | #define PCI_PM_OPS_PTR	&pci_pm_ops | 
|  | 664 |  | 
|  | 665 | #else /* !CONFIG_PM_SLEEP */ | 
|  | 666 |  | 
|  | 667 | #define PCI_PM_OPS_PTR	NULL | 
|  | 668 |  | 
|  | 669 | #endif /* !CONFIG_PM_SLEEP */ | 
|  | 670 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 671 | /** | 
| Laurent riffard | 863b18f | 2005-10-27 23:12:54 +0200 | [diff] [blame] | 672 | * __pci_register_driver - register a new pci driver | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 673 | * @drv: the driver structure to register | 
| Laurent riffard | 863b18f | 2005-10-27 23:12:54 +0200 | [diff] [blame] | 674 | * @owner: owner module of drv | 
| Randy Dunlap | f95d882 | 2007-02-10 14:41:56 -0800 | [diff] [blame] | 675 | * @mod_name: module name string | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 676 | * | 
|  | 677 | * Adds the driver structure to the list of registered drivers. | 
|  | 678 | * Returns a negative value on error, otherwise 0. | 
| Steven Cole | eaae4b3 | 2005-05-03 18:38:30 -0600 | [diff] [blame] | 679 | * If no error occurred, the driver remains registered even if | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 680 | * no device was claimed during registration. | 
|  | 681 | */ | 
| Greg Kroah-Hartman | 725522b | 2007-01-15 11:50:02 -0800 | [diff] [blame] | 682 | int __pci_register_driver(struct pci_driver *drv, struct module *owner, | 
|  | 683 | const char *mod_name) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 684 | { | 
|  | 685 | int error; | 
|  | 686 |  | 
|  | 687 | /* initialize common driver fields */ | 
|  | 688 | drv->driver.name = drv->name; | 
|  | 689 | drv->driver.bus = &pci_bus_type; | 
| Laurent riffard | 863b18f | 2005-10-27 23:12:54 +0200 | [diff] [blame] | 690 | drv->driver.owner = owner; | 
| Greg Kroah-Hartman | 725522b | 2007-01-15 11:50:02 -0800 | [diff] [blame] | 691 | drv->driver.mod_name = mod_name; | 
| Alan Cox | 50b0075 | 2006-08-16 17:42:18 +0100 | [diff] [blame] | 692 |  | 
| Rafael J. Wysocki | bbb44d9 | 2008-05-20 00:49:04 +0200 | [diff] [blame] | 693 | if (drv->pm) | 
|  | 694 | drv->driver.pm = &drv->pm->base; | 
|  | 695 |  | 
| Greg Kroah-Hartman | 7586585 | 2005-06-30 02:18:12 -0700 | [diff] [blame] | 696 | spin_lock_init(&drv->dynids.lock); | 
|  | 697 | INIT_LIST_HEAD(&drv->dynids.list); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 698 |  | 
|  | 699 | /* register with core */ | 
|  | 700 | error = driver_register(&drv->driver); | 
| Akinobu Mita | 50bf14b | 2006-11-08 19:53:59 -0800 | [diff] [blame] | 701 | if (error) | 
|  | 702 | return error; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 |  | 
| Akinobu Mita | 50bf14b | 2006-11-08 19:53:59 -0800 | [diff] [blame] | 704 | error = pci_create_newid_file(drv); | 
|  | 705 | if (error) | 
|  | 706 | driver_unregister(&drv->driver); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 707 |  | 
|  | 708 | return error; | 
|  | 709 | } | 
|  | 710 |  | 
|  | 711 | /** | 
|  | 712 | * pci_unregister_driver - unregister a pci driver | 
|  | 713 | * @drv: the driver structure to unregister | 
|  | 714 | * | 
|  | 715 | * Deletes the driver structure from the list of registered PCI drivers, | 
|  | 716 | * gives it a chance to clean up by calling its remove() function for | 
|  | 717 | * each device it was responsible for, and marks those devices as | 
|  | 718 | * driverless. | 
|  | 719 | */ | 
|  | 720 |  | 
|  | 721 | void | 
|  | 722 | pci_unregister_driver(struct pci_driver *drv) | 
|  | 723 | { | 
| Greg Kroah-Hartman | 03d43b1 | 2007-11-28 12:23:18 -0800 | [diff] [blame] | 724 | pci_remove_newid_file(drv); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 725 | driver_unregister(&drv->driver); | 
|  | 726 | pci_free_dynids(drv); | 
|  | 727 | } | 
|  | 728 |  | 
|  | 729 | static struct pci_driver pci_compat_driver = { | 
|  | 730 | .name = "compat" | 
|  | 731 | }; | 
|  | 732 |  | 
|  | 733 | /** | 
|  | 734 | * pci_dev_driver - get the pci_driver of a device | 
|  | 735 | * @dev: the device to query | 
|  | 736 | * | 
|  | 737 | * Returns the appropriate pci_driver structure or %NULL if there is no | 
|  | 738 | * registered driver for the device. | 
|  | 739 | */ | 
|  | 740 | struct pci_driver * | 
|  | 741 | pci_dev_driver(const struct pci_dev *dev) | 
|  | 742 | { | 
|  | 743 | if (dev->driver) | 
|  | 744 | return dev->driver; | 
|  | 745 | else { | 
|  | 746 | int i; | 
|  | 747 | for(i=0; i<=PCI_ROM_RESOURCE; i++) | 
|  | 748 | if (dev->resource[i].flags & IORESOURCE_BUSY) | 
|  | 749 | return &pci_compat_driver; | 
|  | 750 | } | 
|  | 751 | return NULL; | 
|  | 752 | } | 
|  | 753 |  | 
|  | 754 | /** | 
|  | 755 | * pci_bus_match - Tell if a PCI device structure has a matching PCI device id structure | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 756 | * @dev: the PCI device structure to match against | 
| Randy Dunlap | 8f7020d | 2005-10-23 11:57:38 -0700 | [diff] [blame] | 757 | * @drv: the device driver to search for matching PCI device id structures | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 758 | * | 
|  | 759 | * Used by a driver to check whether a PCI device present in the | 
| Randy Dunlap | 8f7020d | 2005-10-23 11:57:38 -0700 | [diff] [blame] | 760 | * system is in its list of supported devices. Returns the matching | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 761 | * pci_device_id structure or %NULL if there is no match. | 
|  | 762 | */ | 
| Greg Kroah-Hartman | 7586585 | 2005-06-30 02:18:12 -0700 | [diff] [blame] | 763 | static int pci_bus_match(struct device *dev, struct device_driver *drv) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 764 | { | 
| Greg Kroah-Hartman | 7586585 | 2005-06-30 02:18:12 -0700 | [diff] [blame] | 765 | struct pci_dev *pci_dev = to_pci_dev(dev); | 
|  | 766 | struct pci_driver *pci_drv = to_pci_driver(drv); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 767 | const struct pci_device_id *found_id; | 
|  | 768 |  | 
| Greg Kroah-Hartman | 7586585 | 2005-06-30 02:18:12 -0700 | [diff] [blame] | 769 | found_id = pci_match_device(pci_drv, pci_dev); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 770 | if (found_id) | 
|  | 771 | return 1; | 
|  | 772 |  | 
| Greg Kroah-Hartman | 7586585 | 2005-06-30 02:18:12 -0700 | [diff] [blame] | 773 | return 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 774 | } | 
|  | 775 |  | 
|  | 776 | /** | 
|  | 777 | * pci_dev_get - increments the reference count of the pci device structure | 
|  | 778 | * @dev: the device being referenced | 
|  | 779 | * | 
|  | 780 | * Each live reference to a device should be refcounted. | 
|  | 781 | * | 
|  | 782 | * Drivers for PCI devices should normally record such references in | 
|  | 783 | * their probe() methods, when they bind to a device, and release | 
|  | 784 | * them by calling pci_dev_put(), in their disconnect() methods. | 
|  | 785 | * | 
|  | 786 | * A pointer to the device with the incremented reference counter is returned. | 
|  | 787 | */ | 
|  | 788 | struct pci_dev *pci_dev_get(struct pci_dev *dev) | 
|  | 789 | { | 
|  | 790 | if (dev) | 
|  | 791 | get_device(&dev->dev); | 
|  | 792 | return dev; | 
|  | 793 | } | 
|  | 794 |  | 
|  | 795 | /** | 
|  | 796 | * pci_dev_put - release a use of the pci device structure | 
|  | 797 | * @dev: device that's been disconnected | 
|  | 798 | * | 
|  | 799 | * Must be called when a user of a device is finished with it.  When the last | 
|  | 800 | * user of the device calls this function, the memory of the device is freed. | 
|  | 801 | */ | 
|  | 802 | void pci_dev_put(struct pci_dev *dev) | 
|  | 803 | { | 
|  | 804 | if (dev) | 
|  | 805 | put_device(&dev->dev); | 
|  | 806 | } | 
|  | 807 |  | 
|  | 808 | #ifndef CONFIG_HOTPLUG | 
| Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 809 | int pci_uevent(struct device *dev, struct kobj_uevent_env *env) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 810 | { | 
|  | 811 | return -ENODEV; | 
|  | 812 | } | 
|  | 813 | #endif | 
|  | 814 |  | 
|  | 815 | struct bus_type pci_bus_type = { | 
|  | 816 | .name		= "pci", | 
|  | 817 | .match		= pci_bus_match, | 
| Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 818 | .uevent		= pci_uevent, | 
| Russell King | b15d686 | 2006-01-05 14:30:22 +0000 | [diff] [blame] | 819 | .probe		= pci_device_probe, | 
|  | 820 | .remove		= pci_device_remove, | 
| Linus Torvalds | cbd69db | 2006-06-24 14:50:29 -0700 | [diff] [blame] | 821 | .shutdown	= pci_device_shutdown, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 822 | .dev_attrs	= pci_dev_attrs, | 
| Rafael J. Wysocki | bbb44d9 | 2008-05-20 00:49:04 +0200 | [diff] [blame] | 823 | .pm		= PCI_PM_OPS_PTR, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 824 | }; | 
|  | 825 |  | 
|  | 826 | static int __init pci_driver_init(void) | 
|  | 827 | { | 
|  | 828 | return bus_register(&pci_bus_type); | 
|  | 829 | } | 
|  | 830 |  | 
|  | 831 | postcore_initcall(pci_driver_init); | 
|  | 832 |  | 
| Greg Kroah-Hartman | 7586585 | 2005-06-30 02:18:12 -0700 | [diff] [blame] | 833 | EXPORT_SYMBOL(pci_match_id); | 
| Laurent riffard | 863b18f | 2005-10-27 23:12:54 +0200 | [diff] [blame] | 834 | EXPORT_SYMBOL(__pci_register_driver); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 835 | EXPORT_SYMBOL(pci_unregister_driver); | 
|  | 836 | EXPORT_SYMBOL(pci_dev_driver); | 
|  | 837 | EXPORT_SYMBOL(pci_bus_type); | 
|  | 838 | EXPORT_SYMBOL(pci_dev_get); | 
|  | 839 | EXPORT_SYMBOL(pci_dev_put); |