Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * drivers/pci/pci-driver.c |
| 3 | * |
| 4 | */ |
| 5 | |
| 6 | #include <linux/pci.h> |
| 7 | #include <linux/module.h> |
| 8 | #include <linux/init.h> |
| 9 | #include <linux/device.h> |
Andi Kleen | d42c699 | 2005-07-06 19:56:03 +0200 | [diff] [blame] | 10 | #include <linux/mempolicy.h> |
Tim Schmielau | 4e57b68 | 2005-10-30 15:03:48 -0800 | [diff] [blame] | 11 | #include <linux/string.h> |
| 12 | #include <linux/slab.h> |
Tim Schmielau | 8c65b4a | 2005-11-07 00:59:43 -0800 | [diff] [blame] | 13 | #include <linux/sched.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include "pci.h" |
| 15 | |
| 16 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | * Dynamic device IDs are disabled for !CONFIG_HOTPLUG |
| 18 | */ |
| 19 | |
Greg Kroah-Hartman | 7586585 | 2005-06-30 02:18:12 -0700 | [diff] [blame] | 20 | struct pci_dynid { |
| 21 | struct list_head node; |
| 22 | struct pci_device_id id; |
| 23 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | |
Greg KH | 3d3c2ae | 2005-07-06 09:09:38 -0700 | [diff] [blame] | 25 | #ifdef CONFIG_HOTPLUG |
| 26 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | /** |
Randy Dunlap | 8f7020d | 2005-10-23 11:57:38 -0700 | [diff] [blame] | 28 | * store_new_id - add a new PCI device ID to this driver and re-probe devices |
| 29 | * @driver: target device driver |
| 30 | * @buf: buffer for scanning device ID data |
| 31 | * @count: input size |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | * |
| 33 | * Adds a new dynamic pci device ID to this driver, |
| 34 | * and causes the driver to probe for all devices again. |
| 35 | */ |
Randy Dunlap | f8eb100 | 2005-10-28 20:36:51 -0700 | [diff] [blame] | 36 | static ssize_t |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | store_new_id(struct device_driver *driver, const char *buf, size_t count) |
| 38 | { |
Greg Kroah-Hartman | 7586585 | 2005-06-30 02:18:12 -0700 | [diff] [blame] | 39 | struct pci_dynid *dynid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | struct pci_driver *pdrv = to_pci_driver(driver); |
Jean Delvare | 6ba1863 | 2007-04-07 17:21:28 +0200 | [diff] [blame] | 41 | __u32 vendor, device, subvendor=PCI_ANY_ID, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | subdevice=PCI_ANY_ID, class=0, class_mask=0; |
| 43 | unsigned long driver_data=0; |
| 44 | int fields=0; |
Greg Kroah-Hartman | b19441a | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 45 | int retval = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | |
| 47 | fields = sscanf(buf, "%x %x %x %x %x %x %lux", |
| 48 | &vendor, &device, &subvendor, &subdevice, |
| 49 | &class, &class_mask, &driver_data); |
Jean Delvare | 6ba1863 | 2007-04-07 17:21:28 +0200 | [diff] [blame] | 50 | if (fields < 2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | return -EINVAL; |
| 52 | |
Eric Sesterhenn | f5afe80 | 2006-02-28 15:34:49 +0100 | [diff] [blame] | 53 | dynid = kzalloc(sizeof(*dynid), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | if (!dynid) |
| 55 | return -ENOMEM; |
| 56 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | dynid->id.vendor = vendor; |
| 58 | dynid->id.device = device; |
| 59 | dynid->id.subvendor = subvendor; |
| 60 | dynid->id.subdevice = subdevice; |
| 61 | dynid->id.class = class; |
| 62 | dynid->id.class_mask = class_mask; |
| 63 | dynid->id.driver_data = pdrv->dynids.use_driver_data ? |
| 64 | driver_data : 0UL; |
| 65 | |
| 66 | spin_lock(&pdrv->dynids.lock); |
Michael Ellerman | a56bc69 | 2007-09-14 15:33:13 +1000 | [diff] [blame^] | 67 | list_add_tail(&dynid->node, &pdrv->dynids.list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | spin_unlock(&pdrv->dynids.lock); |
| 69 | |
Greg Kroah-Hartman | 7586585 | 2005-06-30 02:18:12 -0700 | [diff] [blame] | 70 | if (get_driver(&pdrv->driver)) { |
Greg Kroah-Hartman | b19441a | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 71 | retval = driver_attach(&pdrv->driver); |
Greg Kroah-Hartman | 7586585 | 2005-06-30 02:18:12 -0700 | [diff] [blame] | 72 | put_driver(&pdrv->driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | } |
| 74 | |
Greg Kroah-Hartman | b19441a | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 75 | if (retval) |
| 76 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | return count; |
| 78 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | static DRIVER_ATTR(new_id, S_IWUSR, NULL, store_new_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | |
| 81 | static void |
| 82 | pci_free_dynids(struct pci_driver *drv) |
| 83 | { |
Greg Kroah-Hartman | 7586585 | 2005-06-30 02:18:12 -0700 | [diff] [blame] | 84 | struct pci_dynid *dynid, *n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | |
| 86 | spin_lock(&drv->dynids.lock); |
Greg Kroah-Hartman | 7586585 | 2005-06-30 02:18:12 -0700 | [diff] [blame] | 87 | list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | list_del(&dynid->node); |
| 89 | kfree(dynid); |
| 90 | } |
| 91 | spin_unlock(&drv->dynids.lock); |
| 92 | } |
| 93 | |
| 94 | static int |
| 95 | pci_create_newid_file(struct pci_driver *drv) |
| 96 | { |
| 97 | int error = 0; |
| 98 | if (drv->probe != NULL) |
| 99 | error = sysfs_create_file(&drv->driver.kobj, |
| 100 | &driver_attr_new_id.attr); |
| 101 | return error; |
| 102 | } |
| 103 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | #else /* !CONFIG_HOTPLUG */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | static inline void pci_free_dynids(struct pci_driver *drv) {} |
| 106 | static inline int pci_create_newid_file(struct pci_driver *drv) |
| 107 | { |
| 108 | return 0; |
| 109 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | #endif |
| 111 | |
| 112 | /** |
Greg Kroah-Hartman | 7586585 | 2005-06-30 02:18:12 -0700 | [diff] [blame] | 113 | * 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] | 114 | * @ids: array of PCI device id structures to search in |
Greg Kroah-Hartman | 7586585 | 2005-06-30 02:18:12 -0700 | [diff] [blame] | 115 | * @dev: the PCI device structure to match against. |
| 116 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | * 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] | 118 | * system is in its list of supported devices. Returns the matching |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | * pci_device_id structure or %NULL if there is no match. |
Greg Kroah-Hartman | 7586585 | 2005-06-30 02:18:12 -0700 | [diff] [blame] | 120 | * |
Randy Dunlap | 8b60756 | 2007-05-09 07:19:14 +0200 | [diff] [blame] | 121 | * 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] | 122 | * that a driver might want to check for. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | */ |
Greg Kroah-Hartman | 7586585 | 2005-06-30 02:18:12 -0700 | [diff] [blame] | 124 | const struct pci_device_id *pci_match_id(const struct pci_device_id *ids, |
| 125 | struct pci_dev *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | { |
Greg Kroah-Hartman | 7586585 | 2005-06-30 02:18:12 -0700 | [diff] [blame] | 127 | if (ids) { |
| 128 | while (ids->vendor || ids->subvendor || ids->class_mask) { |
| 129 | if (pci_match_one_device(ids, dev)) |
| 130 | return ids; |
| 131 | ids++; |
| 132 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | } |
| 134 | return NULL; |
| 135 | } |
| 136 | |
| 137 | /** |
Randy Dunlap | ae9608a | 2007-01-09 21:41:01 -0800 | [diff] [blame] | 138 | * 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] | 139 | * @drv: the PCI driver to match against |
Henrik Kretzschmar | 39ba487 | 2006-08-15 10:57:16 +0200 | [diff] [blame] | 140 | * @dev: the PCI device structure to match against |
Greg Kroah-Hartman | 7586585 | 2005-06-30 02:18:12 -0700 | [diff] [blame] | 141 | * |
| 142 | * Used by a driver to check whether a PCI device present in the |
| 143 | * system is in its list of supported devices. Returns the matching |
| 144 | * pci_device_id structure or %NULL if there is no match. |
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_device(struct pci_driver *drv, |
| 147 | struct pci_dev *dev) |
| 148 | { |
Greg Kroah-Hartman | 7586585 | 2005-06-30 02:18:12 -0700 | [diff] [blame] | 149 | struct pci_dynid *dynid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | |
Russell King | 7461b60 | 2006-11-29 21:18:04 +0000 | [diff] [blame] | 151 | /* Look at the dynamic ids first, before the static ones */ |
Greg Kroah-Hartman | 7586585 | 2005-06-30 02:18:12 -0700 | [diff] [blame] | 152 | spin_lock(&drv->dynids.lock); |
| 153 | list_for_each_entry(dynid, &drv->dynids.list, node) { |
| 154 | if (pci_match_one_device(&dynid->id, dev)) { |
| 155 | spin_unlock(&drv->dynids.lock); |
| 156 | return &dynid->id; |
| 157 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | } |
Greg Kroah-Hartman | 7586585 | 2005-06-30 02:18:12 -0700 | [diff] [blame] | 159 | spin_unlock(&drv->dynids.lock); |
Russell King | 7461b60 | 2006-11-29 21:18:04 +0000 | [diff] [blame] | 160 | |
| 161 | return pci_match_id(drv->id_table, dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | } |
| 163 | |
Andi Kleen | d42c699 | 2005-07-06 19:56:03 +0200 | [diff] [blame] | 164 | static int pci_call_probe(struct pci_driver *drv, struct pci_dev *dev, |
| 165 | const struct pci_device_id *id) |
| 166 | { |
| 167 | int error; |
| 168 | #ifdef CONFIG_NUMA |
| 169 | /* Execute driver initialization on node where the |
| 170 | device's bus is attached to. This way the driver likely |
| 171 | allocates its local memory on the right node without |
| 172 | any need to change it. */ |
| 173 | struct mempolicy *oldpol; |
| 174 | cpumask_t oldmask = current->cpus_allowed; |
| 175 | int node = pcibus_to_node(dev->bus); |
| 176 | if (node >= 0 && node_online(node)) |
| 177 | set_cpus_allowed(current, node_to_cpumask(node)); |
| 178 | /* And set default memory allocation policy */ |
| 179 | oldpol = current->mempolicy; |
| 180 | current->mempolicy = &default_policy; |
| 181 | mpol_get(current->mempolicy); |
| 182 | #endif |
| 183 | error = drv->probe(dev, id); |
| 184 | #ifdef CONFIG_NUMA |
| 185 | set_cpus_allowed(current, oldmask); |
| 186 | mpol_free(current->mempolicy); |
| 187 | current->mempolicy = oldpol; |
| 188 | #endif |
| 189 | return error; |
| 190 | } |
| 191 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | /** |
| 193 | * __pci_device_probe() |
Randy Dunlap | 8f7020d | 2005-10-23 11:57:38 -0700 | [diff] [blame] | 194 | * @drv: driver to call to check if it wants the PCI device |
| 195 | * @pci_dev: PCI device being probed |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | * |
Randy Dunlap | 8f7020d | 2005-10-23 11:57:38 -0700 | [diff] [blame] | 197 | * returns 0 on success, else error. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | * side-effect: pci_dev->driver is set to drv when drv claims pci_dev. |
| 199 | */ |
| 200 | static int |
| 201 | __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] | 202 | { |
| 203 | const struct pci_device_id *id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | int error = 0; |
| 205 | |
| 206 | if (!pci_dev->driver && drv->probe) { |
Greg Kroah-Hartman | 7586585 | 2005-06-30 02:18:12 -0700 | [diff] [blame] | 207 | error = -ENODEV; |
| 208 | |
| 209 | id = pci_match_device(drv, pci_dev); |
| 210 | if (id) |
Andi Kleen | d42c699 | 2005-07-06 19:56:03 +0200 | [diff] [blame] | 211 | error = pci_call_probe(drv, pci_dev, id); |
Greg Kroah-Hartman | 7586585 | 2005-06-30 02:18:12 -0700 | [diff] [blame] | 212 | if (error >= 0) { |
| 213 | pci_dev->driver = drv; |
| 214 | error = 0; |
| 215 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | } |
| 217 | return error; |
| 218 | } |
| 219 | |
| 220 | static int pci_device_probe(struct device * dev) |
| 221 | { |
| 222 | int error = 0; |
| 223 | struct pci_driver *drv; |
| 224 | struct pci_dev *pci_dev; |
| 225 | |
| 226 | drv = to_pci_driver(dev->driver); |
| 227 | pci_dev = to_pci_dev(dev); |
| 228 | pci_dev_get(pci_dev); |
| 229 | error = __pci_device_probe(drv, pci_dev); |
| 230 | if (error) |
| 231 | pci_dev_put(pci_dev); |
| 232 | |
| 233 | return error; |
| 234 | } |
| 235 | |
| 236 | static int pci_device_remove(struct device * dev) |
| 237 | { |
| 238 | struct pci_dev * pci_dev = to_pci_dev(dev); |
| 239 | struct pci_driver * drv = pci_dev->driver; |
| 240 | |
| 241 | if (drv) { |
| 242 | if (drv->remove) |
| 243 | drv->remove(pci_dev); |
| 244 | pci_dev->driver = NULL; |
| 245 | } |
| 246 | |
| 247 | /* |
Shaohua Li | 2449e06 | 2006-10-20 14:45:32 -0700 | [diff] [blame] | 248 | * If the device is still on, set the power state as "unknown", |
| 249 | * since it might change by the next time we load the driver. |
| 250 | */ |
| 251 | if (pci_dev->current_state == PCI_D0) |
| 252 | pci_dev->current_state = PCI_UNKNOWN; |
| 253 | |
| 254 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | * We would love to complain here if pci_dev->is_enabled is set, that |
| 256 | * the driver should have called pci_disable_device(), but the |
| 257 | * unfortunate fact is there are too many odd BIOS and bridge setups |
| 258 | * that don't like drivers doing that all of the time. |
| 259 | * Oh well, we can dream of sane hardware when we sleep, no matter how |
| 260 | * horrible the crap we have to deal with is when we are awake... |
| 261 | */ |
| 262 | |
| 263 | pci_dev_put(pci_dev); |
| 264 | return 0; |
| 265 | } |
| 266 | |
| 267 | static int pci_device_suspend(struct device * dev, pm_message_t state) |
| 268 | { |
| 269 | struct pci_dev * pci_dev = to_pci_dev(dev); |
| 270 | struct pci_driver * drv = pci_dev->driver; |
| 271 | int i = 0; |
| 272 | |
Andrew Morton | 0266949 | 2006-03-23 01:38:34 -0800 | [diff] [blame] | 273 | if (drv && drv->suspend) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | i = drv->suspend(pci_dev, state); |
Andrew Morton | 0266949 | 2006-03-23 01:38:34 -0800 | [diff] [blame] | 275 | suspend_report_result(drv->suspend, i); |
| 276 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | pci_save_state(pci_dev); |
Shaohua Li | 2449e06 | 2006-10-20 14:45:32 -0700 | [diff] [blame] | 278 | /* |
| 279 | * mark its power state as "unknown", since we don't know if |
| 280 | * e.g. the BIOS will change its device state when we suspend. |
| 281 | */ |
| 282 | if (pci_dev->current_state == PCI_D0) |
| 283 | pci_dev->current_state = PCI_UNKNOWN; |
Andrew Morton | 0266949 | 2006-03-23 01:38:34 -0800 | [diff] [blame] | 284 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | return i; |
| 286 | } |
| 287 | |
Linus Torvalds | cbd69db | 2006-06-24 14:50:29 -0700 | [diff] [blame] | 288 | static int pci_device_suspend_late(struct device * dev, pm_message_t state) |
| 289 | { |
| 290 | struct pci_dev * pci_dev = to_pci_dev(dev); |
| 291 | struct pci_driver * drv = pci_dev->driver; |
| 292 | int i = 0; |
| 293 | |
| 294 | if (drv && drv->suspend_late) { |
| 295 | i = drv->suspend_late(pci_dev, state); |
| 296 | suspend_report_result(drv->suspend_late, i); |
| 297 | } |
| 298 | return i; |
| 299 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | |
Greg Kroah-Hartman | 95a6296 | 2005-07-28 11:37:33 -0700 | [diff] [blame] | 301 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | * Default resume method for devices that have no driver provided resume, |
| 303 | * or not even a driver at all. |
| 304 | */ |
Jean Delvare | 8d92bc2 | 2006-04-18 14:49:56 +0200 | [diff] [blame] | 305 | static int pci_default_resume(struct pci_dev *pci_dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | { |
Jean Delvare | 8d92bc2 | 2006-04-18 14:49:56 +0200 | [diff] [blame] | 307 | int retval = 0; |
Greg Kroah-Hartman | 95a6296 | 2005-07-28 11:37:33 -0700 | [diff] [blame] | 308 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | /* restore the PCI config space */ |
| 310 | pci_restore_state(pci_dev); |
| 311 | /* if the device was enabled before suspend, reenable */ |
Tejun Heo | 0b62e13 | 2007-07-27 14:43:35 +0900 | [diff] [blame] | 312 | retval = pci_reenable_device(pci_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | /* if the device was busmaster before the suspend, make it busmaster again */ |
| 314 | if (pci_dev->is_busmaster) |
| 315 | pci_set_master(pci_dev); |
Jean Delvare | 8d92bc2 | 2006-04-18 14:49:56 +0200 | [diff] [blame] | 316 | |
| 317 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | static int pci_device_resume(struct device * dev) |
| 321 | { |
Jean Delvare | 8d92bc2 | 2006-04-18 14:49:56 +0200 | [diff] [blame] | 322 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | struct pci_dev * pci_dev = to_pci_dev(dev); |
| 324 | struct pci_driver * drv = pci_dev->driver; |
| 325 | |
| 326 | if (drv && drv->resume) |
Jean Delvare | 8d92bc2 | 2006-04-18 14:49:56 +0200 | [diff] [blame] | 327 | error = drv->resume(pci_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | else |
Jean Delvare | 8d92bc2 | 2006-04-18 14:49:56 +0200 | [diff] [blame] | 329 | error = pci_default_resume(pci_dev); |
| 330 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | } |
| 332 | |
Linus Torvalds | cbd69db | 2006-06-24 14:50:29 -0700 | [diff] [blame] | 333 | static int pci_device_resume_early(struct device * dev) |
| 334 | { |
| 335 | int error = 0; |
| 336 | struct pci_dev * pci_dev = to_pci_dev(dev); |
| 337 | struct pci_driver * drv = pci_dev->driver; |
| 338 | |
Alan Cox | 1597cac | 2006-12-04 15:14:45 -0800 | [diff] [blame] | 339 | pci_fixup_device(pci_fixup_resume, pci_dev); |
| 340 | |
Linus Torvalds | cbd69db | 2006-06-24 14:50:29 -0700 | [diff] [blame] | 341 | if (drv && drv->resume_early) |
| 342 | error = drv->resume_early(pci_dev); |
| 343 | return error; |
| 344 | } |
| 345 | |
Greg KH | c895817 | 2005-04-08 14:53:31 +0900 | [diff] [blame] | 346 | static void pci_device_shutdown(struct device *dev) |
| 347 | { |
| 348 | struct pci_dev *pci_dev = to_pci_dev(dev); |
| 349 | struct pci_driver *drv = pci_dev->driver; |
| 350 | |
| 351 | if (drv && drv->shutdown) |
| 352 | drv->shutdown(pci_dev); |
| 353 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | |
| 355 | #define kobj_to_pci_driver(obj) container_of(obj, struct device_driver, kobj) |
| 356 | #define attr_to_driver_attribute(obj) container_of(obj, struct driver_attribute, attr) |
| 357 | |
| 358 | static ssize_t |
| 359 | pci_driver_attr_show(struct kobject * kobj, struct attribute *attr, char *buf) |
| 360 | { |
| 361 | struct device_driver *driver = kobj_to_pci_driver(kobj); |
| 362 | struct driver_attribute *dattr = attr_to_driver_attribute(attr); |
Dmitry Torokhov | fc7e482 | 2005-04-29 01:26:27 -0500 | [diff] [blame] | 363 | ssize_t ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | |
Dmitry Torokhov | fc7e482 | 2005-04-29 01:26:27 -0500 | [diff] [blame] | 365 | if (!get_driver(driver)) |
| 366 | return -ENODEV; |
| 367 | |
| 368 | ret = dattr->show ? dattr->show(driver, buf) : -EIO; |
| 369 | |
| 370 | put_driver(driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | return ret; |
| 372 | } |
| 373 | |
| 374 | static ssize_t |
| 375 | pci_driver_attr_store(struct kobject * kobj, struct attribute *attr, |
| 376 | const char *buf, size_t count) |
| 377 | { |
| 378 | struct device_driver *driver = kobj_to_pci_driver(kobj); |
| 379 | struct driver_attribute *dattr = attr_to_driver_attribute(attr); |
Dmitry Torokhov | fc7e482 | 2005-04-29 01:26:27 -0500 | [diff] [blame] | 380 | ssize_t ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | |
Dmitry Torokhov | fc7e482 | 2005-04-29 01:26:27 -0500 | [diff] [blame] | 382 | if (!get_driver(driver)) |
| 383 | return -ENODEV; |
| 384 | |
| 385 | ret = dattr->store ? dattr->store(driver, buf, count) : -EIO; |
| 386 | |
| 387 | put_driver(driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | return ret; |
| 389 | } |
| 390 | |
| 391 | static struct sysfs_ops pci_driver_sysfs_ops = { |
| 392 | .show = pci_driver_attr_show, |
| 393 | .store = pci_driver_attr_store, |
| 394 | }; |
| 395 | static struct kobj_type pci_driver_kobj_type = { |
| 396 | .sysfs_ops = &pci_driver_sysfs_ops, |
| 397 | }; |
| 398 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | /** |
Laurent riffard | 863b18f | 2005-10-27 23:12:54 +0200 | [diff] [blame] | 400 | * __pci_register_driver - register a new pci driver |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | * @drv: the driver structure to register |
Laurent riffard | 863b18f | 2005-10-27 23:12:54 +0200 | [diff] [blame] | 402 | * @owner: owner module of drv |
Randy Dunlap | f95d882 | 2007-02-10 14:41:56 -0800 | [diff] [blame] | 403 | * @mod_name: module name string |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | * |
| 405 | * Adds the driver structure to the list of registered drivers. |
| 406 | * Returns a negative value on error, otherwise 0. |
Steven Cole | eaae4b3 | 2005-05-03 18:38:30 -0600 | [diff] [blame] | 407 | * If no error occurred, the driver remains registered even if |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 | * no device was claimed during registration. |
| 409 | */ |
Greg Kroah-Hartman | 725522b | 2007-01-15 11:50:02 -0800 | [diff] [blame] | 410 | int __pci_register_driver(struct pci_driver *drv, struct module *owner, |
| 411 | const char *mod_name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | { |
| 413 | int error; |
| 414 | |
| 415 | /* initialize common driver fields */ |
| 416 | drv->driver.name = drv->name; |
| 417 | drv->driver.bus = &pci_bus_type; |
Laurent riffard | 863b18f | 2005-10-27 23:12:54 +0200 | [diff] [blame] | 418 | drv->driver.owner = owner; |
Greg Kroah-Hartman | 725522b | 2007-01-15 11:50:02 -0800 | [diff] [blame] | 419 | drv->driver.mod_name = mod_name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | drv->driver.kobj.ktype = &pci_driver_kobj_type; |
Alan Cox | 50b0075 | 2006-08-16 17:42:18 +0100 | [diff] [blame] | 421 | |
Greg Kroah-Hartman | 7586585 | 2005-06-30 02:18:12 -0700 | [diff] [blame] | 422 | spin_lock_init(&drv->dynids.lock); |
| 423 | INIT_LIST_HEAD(&drv->dynids.list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | |
| 425 | /* register with core */ |
| 426 | error = driver_register(&drv->driver); |
Akinobu Mita | 50bf14b | 2006-11-08 19:53:59 -0800 | [diff] [blame] | 427 | if (error) |
| 428 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | |
Akinobu Mita | 50bf14b | 2006-11-08 19:53:59 -0800 | [diff] [blame] | 430 | error = pci_create_newid_file(drv); |
| 431 | if (error) |
| 432 | driver_unregister(&drv->driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | |
| 434 | return error; |
| 435 | } |
| 436 | |
| 437 | /** |
| 438 | * pci_unregister_driver - unregister a pci driver |
| 439 | * @drv: the driver structure to unregister |
| 440 | * |
| 441 | * Deletes the driver structure from the list of registered PCI drivers, |
| 442 | * gives it a chance to clean up by calling its remove() function for |
| 443 | * each device it was responsible for, and marks those devices as |
| 444 | * driverless. |
| 445 | */ |
| 446 | |
| 447 | void |
| 448 | pci_unregister_driver(struct pci_driver *drv) |
| 449 | { |
| 450 | driver_unregister(&drv->driver); |
| 451 | pci_free_dynids(drv); |
| 452 | } |
| 453 | |
| 454 | static struct pci_driver pci_compat_driver = { |
| 455 | .name = "compat" |
| 456 | }; |
| 457 | |
| 458 | /** |
| 459 | * pci_dev_driver - get the pci_driver of a device |
| 460 | * @dev: the device to query |
| 461 | * |
| 462 | * Returns the appropriate pci_driver structure or %NULL if there is no |
| 463 | * registered driver for the device. |
| 464 | */ |
| 465 | struct pci_driver * |
| 466 | pci_dev_driver(const struct pci_dev *dev) |
| 467 | { |
| 468 | if (dev->driver) |
| 469 | return dev->driver; |
| 470 | else { |
| 471 | int i; |
| 472 | for(i=0; i<=PCI_ROM_RESOURCE; i++) |
| 473 | if (dev->resource[i].flags & IORESOURCE_BUSY) |
| 474 | return &pci_compat_driver; |
| 475 | } |
| 476 | return NULL; |
| 477 | } |
| 478 | |
| 479 | /** |
| 480 | * 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] | 481 | * @dev: the PCI device structure to match against |
Randy Dunlap | 8f7020d | 2005-10-23 11:57:38 -0700 | [diff] [blame] | 482 | * @drv: the device driver to search for matching PCI device id structures |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | * |
| 484 | * 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] | 485 | * system is in its list of supported devices. Returns the matching |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 486 | * pci_device_id structure or %NULL if there is no match. |
| 487 | */ |
Greg Kroah-Hartman | 7586585 | 2005-06-30 02:18:12 -0700 | [diff] [blame] | 488 | static int pci_bus_match(struct device *dev, struct device_driver *drv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | { |
Greg Kroah-Hartman | 7586585 | 2005-06-30 02:18:12 -0700 | [diff] [blame] | 490 | struct pci_dev *pci_dev = to_pci_dev(dev); |
| 491 | struct pci_driver *pci_drv = to_pci_driver(drv); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | const struct pci_device_id *found_id; |
| 493 | |
Greg Kroah-Hartman | 7586585 | 2005-06-30 02:18:12 -0700 | [diff] [blame] | 494 | found_id = pci_match_device(pci_drv, pci_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | if (found_id) |
| 496 | return 1; |
| 497 | |
Greg Kroah-Hartman | 7586585 | 2005-06-30 02:18:12 -0700 | [diff] [blame] | 498 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | /** |
| 502 | * pci_dev_get - increments the reference count of the pci device structure |
| 503 | * @dev: the device being referenced |
| 504 | * |
| 505 | * Each live reference to a device should be refcounted. |
| 506 | * |
| 507 | * Drivers for PCI devices should normally record such references in |
| 508 | * their probe() methods, when they bind to a device, and release |
| 509 | * them by calling pci_dev_put(), in their disconnect() methods. |
| 510 | * |
| 511 | * A pointer to the device with the incremented reference counter is returned. |
| 512 | */ |
| 513 | struct pci_dev *pci_dev_get(struct pci_dev *dev) |
| 514 | { |
| 515 | if (dev) |
| 516 | get_device(&dev->dev); |
| 517 | return dev; |
| 518 | } |
| 519 | |
| 520 | /** |
| 521 | * pci_dev_put - release a use of the pci device structure |
| 522 | * @dev: device that's been disconnected |
| 523 | * |
| 524 | * Must be called when a user of a device is finished with it. When the last |
| 525 | * user of the device calls this function, the memory of the device is freed. |
| 526 | */ |
| 527 | void pci_dev_put(struct pci_dev *dev) |
| 528 | { |
| 529 | if (dev) |
| 530 | put_device(&dev->dev); |
| 531 | } |
| 532 | |
| 533 | #ifndef CONFIG_HOTPLUG |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 534 | int pci_uevent(struct device *dev, char **envp, int num_envp, |
| 535 | char *buffer, int buffer_size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | { |
| 537 | return -ENODEV; |
| 538 | } |
| 539 | #endif |
| 540 | |
| 541 | struct bus_type pci_bus_type = { |
| 542 | .name = "pci", |
| 543 | .match = pci_bus_match, |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 544 | .uevent = pci_uevent, |
Russell King | b15d686 | 2006-01-05 14:30:22 +0000 | [diff] [blame] | 545 | .probe = pci_device_probe, |
| 546 | .remove = pci_device_remove, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | .suspend = pci_device_suspend, |
Linus Torvalds | cbd69db | 2006-06-24 14:50:29 -0700 | [diff] [blame] | 548 | .suspend_late = pci_device_suspend_late, |
| 549 | .resume_early = pci_device_resume_early, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | .resume = pci_device_resume, |
Linus Torvalds | cbd69db | 2006-06-24 14:50:29 -0700 | [diff] [blame] | 551 | .shutdown = pci_device_shutdown, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | .dev_attrs = pci_dev_attrs, |
| 553 | }; |
| 554 | |
| 555 | static int __init pci_driver_init(void) |
| 556 | { |
| 557 | return bus_register(&pci_bus_type); |
| 558 | } |
| 559 | |
| 560 | postcore_initcall(pci_driver_init); |
| 561 | |
Greg Kroah-Hartman | 7586585 | 2005-06-30 02:18:12 -0700 | [diff] [blame] | 562 | EXPORT_SYMBOL(pci_match_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | EXPORT_SYMBOL(pci_match_device); |
Laurent riffard | 863b18f | 2005-10-27 23:12:54 +0200 | [diff] [blame] | 564 | EXPORT_SYMBOL(__pci_register_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 565 | EXPORT_SYMBOL(pci_unregister_driver); |
| 566 | EXPORT_SYMBOL(pci_dev_driver); |
| 567 | EXPORT_SYMBOL(pci_bus_type); |
| 568 | EXPORT_SYMBOL(pci_dev_get); |
| 569 | EXPORT_SYMBOL(pci_dev_put); |