| 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; | 
|  | 184 | int node = pcibus_to_node(dev->bus); | 
|  | 185 | if (node >= 0 && node_online(node)) | 
|  | 186 | set_cpus_allowed(current, node_to_cpumask(node)); | 
|  | 187 | /* And set default memory allocation policy */ | 
|  | 188 | oldpol = current->mempolicy; | 
|  | 189 | current->mempolicy = &default_policy; | 
|  | 190 | mpol_get(current->mempolicy); | 
|  | 191 | #endif | 
|  | 192 | error = drv->probe(dev, id); | 
|  | 193 | #ifdef CONFIG_NUMA | 
|  | 194 | set_cpus_allowed(current, oldmask); | 
|  | 195 | mpol_free(current->mempolicy); | 
|  | 196 | current->mempolicy = oldpol; | 
|  | 197 | #endif | 
|  | 198 | return error; | 
|  | 199 | } | 
|  | 200 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | /** | 
|  | 202 | * __pci_device_probe() | 
| Randy Dunlap | 8f7020d | 2005-10-23 11:57:38 -0700 | [diff] [blame] | 203 | * @drv: driver to call to check if it wants the PCI device | 
|  | 204 | * @pci_dev: PCI device being probed | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | * | 
| Randy Dunlap | 8f7020d | 2005-10-23 11:57:38 -0700 | [diff] [blame] | 206 | * returns 0 on success, else error. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | * side-effect: pci_dev->driver is set to drv when drv claims pci_dev. | 
|  | 208 | */ | 
|  | 209 | static int | 
|  | 210 | __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] | 211 | { | 
|  | 212 | const struct pci_device_id *id; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | int error = 0; | 
|  | 214 |  | 
|  | 215 | if (!pci_dev->driver && drv->probe) { | 
| Greg Kroah-Hartman | 7586585 | 2005-06-30 02:18:12 -0700 | [diff] [blame] | 216 | error = -ENODEV; | 
|  | 217 |  | 
|  | 218 | id = pci_match_device(drv, pci_dev); | 
|  | 219 | if (id) | 
| Andi Kleen | d42c699 | 2005-07-06 19:56:03 +0200 | [diff] [blame] | 220 | error = pci_call_probe(drv, pci_dev, id); | 
| Greg Kroah-Hartman | 7586585 | 2005-06-30 02:18:12 -0700 | [diff] [blame] | 221 | if (error >= 0) { | 
|  | 222 | pci_dev->driver = drv; | 
|  | 223 | error = 0; | 
|  | 224 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | } | 
|  | 226 | return error; | 
|  | 227 | } | 
|  | 228 |  | 
|  | 229 | static int pci_device_probe(struct device * dev) | 
|  | 230 | { | 
|  | 231 | int error = 0; | 
|  | 232 | struct pci_driver *drv; | 
|  | 233 | struct pci_dev *pci_dev; | 
|  | 234 |  | 
|  | 235 | drv = to_pci_driver(dev->driver); | 
|  | 236 | pci_dev = to_pci_dev(dev); | 
|  | 237 | pci_dev_get(pci_dev); | 
|  | 238 | error = __pci_device_probe(drv, pci_dev); | 
|  | 239 | if (error) | 
|  | 240 | pci_dev_put(pci_dev); | 
|  | 241 |  | 
|  | 242 | return error; | 
|  | 243 | } | 
|  | 244 |  | 
|  | 245 | static int pci_device_remove(struct device * dev) | 
|  | 246 | { | 
|  | 247 | struct pci_dev * pci_dev = to_pci_dev(dev); | 
|  | 248 | struct pci_driver * drv = pci_dev->driver; | 
|  | 249 |  | 
|  | 250 | if (drv) { | 
|  | 251 | if (drv->remove) | 
|  | 252 | drv->remove(pci_dev); | 
|  | 253 | pci_dev->driver = NULL; | 
|  | 254 | } | 
|  | 255 |  | 
|  | 256 | /* | 
| Shaohua Li | 2449e06 | 2006-10-20 14:45:32 -0700 | [diff] [blame] | 257 | * If the device is still on, set the power state as "unknown", | 
|  | 258 | * since it might change by the next time we load the driver. | 
|  | 259 | */ | 
|  | 260 | if (pci_dev->current_state == PCI_D0) | 
|  | 261 | pci_dev->current_state = PCI_UNKNOWN; | 
|  | 262 |  | 
|  | 263 | /* | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | * We would love to complain here if pci_dev->is_enabled is set, that | 
|  | 265 | * the driver should have called pci_disable_device(), but the | 
|  | 266 | * unfortunate fact is there are too many odd BIOS and bridge setups | 
|  | 267 | * that don't like drivers doing that all of the time. | 
|  | 268 | * Oh well, we can dream of sane hardware when we sleep, no matter how | 
|  | 269 | * horrible the crap we have to deal with is when we are awake... | 
|  | 270 | */ | 
|  | 271 |  | 
|  | 272 | pci_dev_put(pci_dev); | 
|  | 273 | return 0; | 
|  | 274 | } | 
|  | 275 |  | 
|  | 276 | static int pci_device_suspend(struct device * dev, pm_message_t state) | 
|  | 277 | { | 
|  | 278 | struct pci_dev * pci_dev = to_pci_dev(dev); | 
|  | 279 | struct pci_driver * drv = pci_dev->driver; | 
|  | 280 | int i = 0; | 
|  | 281 |  | 
| Andrew Morton | 0266949 | 2006-03-23 01:38:34 -0800 | [diff] [blame] | 282 | if (drv && drv->suspend) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | i = drv->suspend(pci_dev, state); | 
| Andrew Morton | 0266949 | 2006-03-23 01:38:34 -0800 | [diff] [blame] | 284 | suspend_report_result(drv->suspend, i); | 
|  | 285 | } else { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | pci_save_state(pci_dev); | 
| Shaohua Li | 2449e06 | 2006-10-20 14:45:32 -0700 | [diff] [blame] | 287 | /* | 
|  | 288 | * mark its power state as "unknown", since we don't know if | 
|  | 289 | * e.g. the BIOS will change its device state when we suspend. | 
|  | 290 | */ | 
|  | 291 | if (pci_dev->current_state == PCI_D0) | 
|  | 292 | pci_dev->current_state = PCI_UNKNOWN; | 
| Andrew Morton | 0266949 | 2006-03-23 01:38:34 -0800 | [diff] [blame] | 293 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | return i; | 
|  | 295 | } | 
|  | 296 |  | 
| Linus Torvalds | cbd69db | 2006-06-24 14:50:29 -0700 | [diff] [blame] | 297 | static int pci_device_suspend_late(struct device * dev, pm_message_t state) | 
|  | 298 | { | 
|  | 299 | struct pci_dev * pci_dev = to_pci_dev(dev); | 
|  | 300 | struct pci_driver * drv = pci_dev->driver; | 
|  | 301 | int i = 0; | 
|  | 302 |  | 
|  | 303 | if (drv && drv->suspend_late) { | 
|  | 304 | i = drv->suspend_late(pci_dev, state); | 
|  | 305 | suspend_report_result(drv->suspend_late, i); | 
|  | 306 | } | 
|  | 307 | return i; | 
|  | 308 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 |  | 
| Greg Kroah-Hartman | 95a6296 | 2005-07-28 11:37:33 -0700 | [diff] [blame] | 310 | /* | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | * Default resume method for devices that have no driver provided resume, | 
|  | 312 | * or not even a driver at all. | 
|  | 313 | */ | 
| Jean Delvare | 8d92bc2 | 2006-04-18 14:49:56 +0200 | [diff] [blame] | 314 | static int pci_default_resume(struct pci_dev *pci_dev) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | { | 
| Jean Delvare | 8d92bc2 | 2006-04-18 14:49:56 +0200 | [diff] [blame] | 316 | int retval = 0; | 
| Greg Kroah-Hartman | 95a6296 | 2005-07-28 11:37:33 -0700 | [diff] [blame] | 317 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | /* restore the PCI config space */ | 
|  | 319 | pci_restore_state(pci_dev); | 
|  | 320 | /* if the device was enabled before suspend, reenable */ | 
| Tejun Heo | 0b62e13 | 2007-07-27 14:43:35 +0900 | [diff] [blame] | 321 | retval = pci_reenable_device(pci_dev); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | /* if the device was busmaster before the suspend, make it busmaster again */ | 
|  | 323 | if (pci_dev->is_busmaster) | 
|  | 324 | pci_set_master(pci_dev); | 
| Jean Delvare | 8d92bc2 | 2006-04-18 14:49:56 +0200 | [diff] [blame] | 325 |  | 
|  | 326 | return retval; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | } | 
|  | 328 |  | 
|  | 329 | static int pci_device_resume(struct device * dev) | 
|  | 330 | { | 
| Jean Delvare | 8d92bc2 | 2006-04-18 14:49:56 +0200 | [diff] [blame] | 331 | int error; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | struct pci_dev * pci_dev = to_pci_dev(dev); | 
|  | 333 | struct pci_driver * drv = pci_dev->driver; | 
|  | 334 |  | 
|  | 335 | if (drv && drv->resume) | 
| Jean Delvare | 8d92bc2 | 2006-04-18 14:49:56 +0200 | [diff] [blame] | 336 | error = drv->resume(pci_dev); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | else | 
| Jean Delvare | 8d92bc2 | 2006-04-18 14:49:56 +0200 | [diff] [blame] | 338 | error = pci_default_resume(pci_dev); | 
|  | 339 | return error; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | } | 
|  | 341 |  | 
| Linus Torvalds | cbd69db | 2006-06-24 14:50:29 -0700 | [diff] [blame] | 342 | static int pci_device_resume_early(struct device * dev) | 
|  | 343 | { | 
|  | 344 | int error = 0; | 
|  | 345 | struct pci_dev * pci_dev = to_pci_dev(dev); | 
|  | 346 | struct pci_driver * drv = pci_dev->driver; | 
|  | 347 |  | 
| Alan Cox | 1597cac | 2006-12-04 15:14:45 -0800 | [diff] [blame] | 348 | pci_fixup_device(pci_fixup_resume, pci_dev); | 
|  | 349 |  | 
| Linus Torvalds | cbd69db | 2006-06-24 14:50:29 -0700 | [diff] [blame] | 350 | if (drv && drv->resume_early) | 
|  | 351 | error = drv->resume_early(pci_dev); | 
|  | 352 | return error; | 
|  | 353 | } | 
|  | 354 |  | 
| Greg KH | c895817 | 2005-04-08 14:53:31 +0900 | [diff] [blame] | 355 | static void pci_device_shutdown(struct device *dev) | 
|  | 356 | { | 
|  | 357 | struct pci_dev *pci_dev = to_pci_dev(dev); | 
|  | 358 | struct pci_driver *drv = pci_dev->driver; | 
|  | 359 |  | 
|  | 360 | if (drv && drv->shutdown) | 
|  | 361 | drv->shutdown(pci_dev); | 
|  | 362 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | /** | 
| Laurent riffard | 863b18f | 2005-10-27 23:12:54 +0200 | [diff] [blame] | 365 | * __pci_register_driver - register a new pci driver | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | * @drv: the driver structure to register | 
| Laurent riffard | 863b18f | 2005-10-27 23:12:54 +0200 | [diff] [blame] | 367 | * @owner: owner module of drv | 
| Randy Dunlap | f95d882 | 2007-02-10 14:41:56 -0800 | [diff] [blame] | 368 | * @mod_name: module name string | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | * | 
|  | 370 | * Adds the driver structure to the list of registered drivers. | 
|  | 371 | * Returns a negative value on error, otherwise 0. | 
| Steven Cole | eaae4b3 | 2005-05-03 18:38:30 -0600 | [diff] [blame] | 372 | * If no error occurred, the driver remains registered even if | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | * no device was claimed during registration. | 
|  | 374 | */ | 
| Greg Kroah-Hartman | 725522b | 2007-01-15 11:50:02 -0800 | [diff] [blame] | 375 | int __pci_register_driver(struct pci_driver *drv, struct module *owner, | 
|  | 376 | const char *mod_name) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | { | 
|  | 378 | int error; | 
|  | 379 |  | 
|  | 380 | /* initialize common driver fields */ | 
|  | 381 | drv->driver.name = drv->name; | 
|  | 382 | drv->driver.bus = &pci_bus_type; | 
| Laurent riffard | 863b18f | 2005-10-27 23:12:54 +0200 | [diff] [blame] | 383 | drv->driver.owner = owner; | 
| Greg Kroah-Hartman | 725522b | 2007-01-15 11:50:02 -0800 | [diff] [blame] | 384 | drv->driver.mod_name = mod_name; | 
| Alan Cox | 50b0075 | 2006-08-16 17:42:18 +0100 | [diff] [blame] | 385 |  | 
| Greg Kroah-Hartman | 7586585 | 2005-06-30 02:18:12 -0700 | [diff] [blame] | 386 | spin_lock_init(&drv->dynids.lock); | 
|  | 387 | INIT_LIST_HEAD(&drv->dynids.list); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 |  | 
|  | 389 | /* register with core */ | 
|  | 390 | error = driver_register(&drv->driver); | 
| Akinobu Mita | 50bf14b | 2006-11-08 19:53:59 -0800 | [diff] [blame] | 391 | if (error) | 
|  | 392 | return error; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 |  | 
| Akinobu Mita | 50bf14b | 2006-11-08 19:53:59 -0800 | [diff] [blame] | 394 | error = pci_create_newid_file(drv); | 
|  | 395 | if (error) | 
|  | 396 | driver_unregister(&drv->driver); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 |  | 
|  | 398 | return error; | 
|  | 399 | } | 
|  | 400 |  | 
|  | 401 | /** | 
|  | 402 | * pci_unregister_driver - unregister a pci driver | 
|  | 403 | * @drv: the driver structure to unregister | 
|  | 404 | * | 
|  | 405 | * Deletes the driver structure from the list of registered PCI drivers, | 
|  | 406 | * gives it a chance to clean up by calling its remove() function for | 
|  | 407 | * each device it was responsible for, and marks those devices as | 
|  | 408 | * driverless. | 
|  | 409 | */ | 
|  | 410 |  | 
|  | 411 | void | 
|  | 412 | pci_unregister_driver(struct pci_driver *drv) | 
|  | 413 | { | 
| Greg Kroah-Hartman | 03d43b1 | 2007-11-28 12:23:18 -0800 | [diff] [blame] | 414 | pci_remove_newid_file(drv); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | driver_unregister(&drv->driver); | 
|  | 416 | pci_free_dynids(drv); | 
|  | 417 | } | 
|  | 418 |  | 
|  | 419 | static struct pci_driver pci_compat_driver = { | 
|  | 420 | .name = "compat" | 
|  | 421 | }; | 
|  | 422 |  | 
|  | 423 | /** | 
|  | 424 | * pci_dev_driver - get the pci_driver of a device | 
|  | 425 | * @dev: the device to query | 
|  | 426 | * | 
|  | 427 | * Returns the appropriate pci_driver structure or %NULL if there is no | 
|  | 428 | * registered driver for the device. | 
|  | 429 | */ | 
|  | 430 | struct pci_driver * | 
|  | 431 | pci_dev_driver(const struct pci_dev *dev) | 
|  | 432 | { | 
|  | 433 | if (dev->driver) | 
|  | 434 | return dev->driver; | 
|  | 435 | else { | 
|  | 436 | int i; | 
|  | 437 | for(i=0; i<=PCI_ROM_RESOURCE; i++) | 
|  | 438 | if (dev->resource[i].flags & IORESOURCE_BUSY) | 
|  | 439 | return &pci_compat_driver; | 
|  | 440 | } | 
|  | 441 | return NULL; | 
|  | 442 | } | 
|  | 443 |  | 
|  | 444 | /** | 
|  | 445 | * 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] | 446 | * @dev: the PCI device structure to match against | 
| Randy Dunlap | 8f7020d | 2005-10-23 11:57:38 -0700 | [diff] [blame] | 447 | * @drv: the device driver to search for matching PCI device id structures | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | * | 
|  | 449 | * 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] | 450 | * system is in its list of supported devices. Returns the matching | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | * pci_device_id structure or %NULL if there is no match. | 
|  | 452 | */ | 
| Greg Kroah-Hartman | 7586585 | 2005-06-30 02:18:12 -0700 | [diff] [blame] | 453 | static int pci_bus_match(struct device *dev, struct device_driver *drv) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | { | 
| Greg Kroah-Hartman | 7586585 | 2005-06-30 02:18:12 -0700 | [diff] [blame] | 455 | struct pci_dev *pci_dev = to_pci_dev(dev); | 
|  | 456 | struct pci_driver *pci_drv = to_pci_driver(drv); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | const struct pci_device_id *found_id; | 
|  | 458 |  | 
| Greg Kroah-Hartman | 7586585 | 2005-06-30 02:18:12 -0700 | [diff] [blame] | 459 | found_id = pci_match_device(pci_drv, pci_dev); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | if (found_id) | 
|  | 461 | return 1; | 
|  | 462 |  | 
| Greg Kroah-Hartman | 7586585 | 2005-06-30 02:18:12 -0700 | [diff] [blame] | 463 | return 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | } | 
|  | 465 |  | 
|  | 466 | /** | 
|  | 467 | * pci_dev_get - increments the reference count of the pci device structure | 
|  | 468 | * @dev: the device being referenced | 
|  | 469 | * | 
|  | 470 | * Each live reference to a device should be refcounted. | 
|  | 471 | * | 
|  | 472 | * Drivers for PCI devices should normally record such references in | 
|  | 473 | * their probe() methods, when they bind to a device, and release | 
|  | 474 | * them by calling pci_dev_put(), in their disconnect() methods. | 
|  | 475 | * | 
|  | 476 | * A pointer to the device with the incremented reference counter is returned. | 
|  | 477 | */ | 
|  | 478 | struct pci_dev *pci_dev_get(struct pci_dev *dev) | 
|  | 479 | { | 
|  | 480 | if (dev) | 
|  | 481 | get_device(&dev->dev); | 
|  | 482 | return dev; | 
|  | 483 | } | 
|  | 484 |  | 
|  | 485 | /** | 
|  | 486 | * pci_dev_put - release a use of the pci device structure | 
|  | 487 | * @dev: device that's been disconnected | 
|  | 488 | * | 
|  | 489 | * Must be called when a user of a device is finished with it.  When the last | 
|  | 490 | * user of the device calls this function, the memory of the device is freed. | 
|  | 491 | */ | 
|  | 492 | void pci_dev_put(struct pci_dev *dev) | 
|  | 493 | { | 
|  | 494 | if (dev) | 
|  | 495 | put_device(&dev->dev); | 
|  | 496 | } | 
|  | 497 |  | 
|  | 498 | #ifndef CONFIG_HOTPLUG | 
| Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 499 | int pci_uevent(struct device *dev, struct kobj_uevent_env *env) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | { | 
|  | 501 | return -ENODEV; | 
|  | 502 | } | 
|  | 503 | #endif | 
|  | 504 |  | 
|  | 505 | struct bus_type pci_bus_type = { | 
|  | 506 | .name		= "pci", | 
|  | 507 | .match		= pci_bus_match, | 
| Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 508 | .uevent		= pci_uevent, | 
| Russell King | b15d686 | 2006-01-05 14:30:22 +0000 | [diff] [blame] | 509 | .probe		= pci_device_probe, | 
|  | 510 | .remove		= pci_device_remove, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | .suspend	= pci_device_suspend, | 
| Linus Torvalds | cbd69db | 2006-06-24 14:50:29 -0700 | [diff] [blame] | 512 | .suspend_late	= pci_device_suspend_late, | 
|  | 513 | .resume_early	= pci_device_resume_early, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | .resume		= pci_device_resume, | 
| Linus Torvalds | cbd69db | 2006-06-24 14:50:29 -0700 | [diff] [blame] | 515 | .shutdown	= pci_device_shutdown, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | .dev_attrs	= pci_dev_attrs, | 
|  | 517 | }; | 
|  | 518 |  | 
|  | 519 | static int __init pci_driver_init(void) | 
|  | 520 | { | 
|  | 521 | return bus_register(&pci_bus_type); | 
|  | 522 | } | 
|  | 523 |  | 
|  | 524 | postcore_initcall(pci_driver_init); | 
|  | 525 |  | 
| Greg Kroah-Hartman | 7586585 | 2005-06-30 02:18:12 -0700 | [diff] [blame] | 526 | EXPORT_SYMBOL(pci_match_id); | 
| Laurent riffard | 863b18f | 2005-10-27 23:12:54 +0200 | [diff] [blame] | 527 | EXPORT_SYMBOL(__pci_register_driver); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | EXPORT_SYMBOL(pci_unregister_driver); | 
|  | 529 | EXPORT_SYMBOL(pci_dev_driver); | 
|  | 530 | EXPORT_SYMBOL(pci_bus_type); | 
|  | 531 | EXPORT_SYMBOL(pci_dev_get); | 
|  | 532 | EXPORT_SYMBOL(pci_dev_put); |