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