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