2 * drivers/pci/pci-driver.c
7 #include <linux/module.h>
8 #include <linux/init.h>
9 #include <linux/device.h>
10 #include <linux/mempolicy.h>
11 #include <linux/string.h>
12 #include <linux/slab.h>
13 #include <linux/sched.h>
17 * Dynamic device IDs are disabled for !CONFIG_HOTPLUG
21 struct list_head node;
22 struct pci_device_id id;
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
33 * Adds a new dynamic pci device ID to this driver,
34 * and causes the driver to probe for all devices again.
37 store_new_id(struct device_driver *driver, const char *buf, size_t count)
39 struct pci_dynid *dynid;
40 struct pci_driver *pdrv = to_pci_driver(driver);
41 __u32 vendor, device, subvendor=PCI_ANY_ID,
42 subdevice=PCI_ANY_ID, class=0, class_mask=0;
43 unsigned long driver_data=0;
47 fields = sscanf(buf, "%x %x %x %x %x %x %lux",
48 &vendor, &device, &subvendor, &subdevice,
49 &class, &class_mask, &driver_data);
53 dynid = kzalloc(sizeof(*dynid), GFP_KERNEL);
57 INIT_LIST_HEAD(&dynid->node);
58 dynid->id.vendor = vendor;
59 dynid->id.device = device;
60 dynid->id.subvendor = subvendor;
61 dynid->id.subdevice = subdevice;
62 dynid->id.class = class;
63 dynid->id.class_mask = class_mask;
64 dynid->id.driver_data = pdrv->dynids.use_driver_data ?
67 spin_lock(&pdrv->dynids.lock);
68 list_add_tail(&pdrv->dynids.list, &dynid->node);
69 spin_unlock(&pdrv->dynids.lock);
71 if (get_driver(&pdrv->driver)) {
72 retval = driver_attach(&pdrv->driver);
73 put_driver(&pdrv->driver);
80 static DRIVER_ATTR(new_id, S_IWUSR, NULL, store_new_id);
83 pci_free_dynids(struct pci_driver *drv)
85 struct pci_dynid *dynid, *n;
87 spin_lock(&drv->dynids.lock);
88 list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) {
89 list_del(&dynid->node);
92 spin_unlock(&drv->dynids.lock);
96 pci_create_newid_file(struct pci_driver *drv)
99 if (drv->probe != NULL)
100 error = sysfs_create_file(&drv->driver.kobj,
101 &driver_attr_new_id.attr);
105 #else /* !CONFIG_HOTPLUG */
106 static inline void pci_free_dynids(struct pci_driver *drv) {}
107 static inline int pci_create_newid_file(struct pci_driver *drv)
114 * pci_match_id - See if a pci device matches a given pci_id table
115 * @ids: array of PCI device id structures to search in
116 * @dev: the PCI device structure to match against.
118 * Used by a driver to check whether a PCI device present in the
119 * system is in its list of supported devices. Returns the matching
120 * pci_device_id structure or %NULL if there is no match.
122 * Deprecated, don't use this as it will not catch any dynamic ids
123 * that a driver might want to check for.
125 const struct pci_device_id *pci_match_id(const struct pci_device_id *ids,
129 while (ids->vendor || ids->subvendor || ids->class_mask) {
130 if (pci_match_one_device(ids, dev))
139 * pci_match_device - Tell if a PCI device structure has a matching PCI device id structure
140 * @drv: the PCI driver to match against
141 * @dev: the PCI device structure to match against
143 * Used by a driver to check whether a PCI device present in the
144 * system is in its list of supported devices. Returns the matching
145 * pci_device_id structure or %NULL if there is no match.
147 const struct pci_device_id *pci_match_device(struct pci_driver *drv,
150 struct pci_dynid *dynid;
152 /* Look at the dynamic ids first, before the static ones */
153 spin_lock(&drv->dynids.lock);
154 list_for_each_entry(dynid, &drv->dynids.list, node) {
155 if (pci_match_one_device(&dynid->id, dev)) {
156 spin_unlock(&drv->dynids.lock);
160 spin_unlock(&drv->dynids.lock);
162 return pci_match_id(drv->id_table, dev);
165 static int pci_call_probe(struct pci_driver *drv, struct pci_dev *dev,
166 const struct pci_device_id *id)
170 /* Execute driver initialization on node where the
171 device's bus is attached to. This way the driver likely
172 allocates its local memory on the right node without
173 any need to change it. */
174 struct mempolicy *oldpol;
175 cpumask_t oldmask = current->cpus_allowed;
176 int node = pcibus_to_node(dev->bus);
177 if (node >= 0 && node_online(node))
178 set_cpus_allowed(current, node_to_cpumask(node));
179 /* And set default memory allocation policy */
180 oldpol = current->mempolicy;
181 current->mempolicy = &default_policy;
182 mpol_get(current->mempolicy);
184 error = drv->probe(dev, id);
186 set_cpus_allowed(current, oldmask);
187 mpol_free(current->mempolicy);
188 current->mempolicy = oldpol;
194 * __pci_device_probe()
195 * @drv: driver to call to check if it wants the PCI device
196 * @pci_dev: PCI device being probed
198 * returns 0 on success, else error.
199 * side-effect: pci_dev->driver is set to drv when drv claims pci_dev.
202 __pci_device_probe(struct pci_driver *drv, struct pci_dev *pci_dev)
204 const struct pci_device_id *id;
207 if (!pci_dev->driver && drv->probe) {
210 id = pci_match_device(drv, pci_dev);
212 error = pci_call_probe(drv, pci_dev, id);
214 pci_dev->driver = drv;
221 static int pci_device_probe(struct device * dev)
224 struct pci_driver *drv;
225 struct pci_dev *pci_dev;
227 drv = to_pci_driver(dev->driver);
228 pci_dev = to_pci_dev(dev);
229 pci_dev_get(pci_dev);
230 error = __pci_device_probe(drv, pci_dev);
232 pci_dev_put(pci_dev);
237 static int pci_device_remove(struct device * dev)
239 struct pci_dev * pci_dev = to_pci_dev(dev);
240 struct pci_driver * drv = pci_dev->driver;
244 drv->remove(pci_dev);
245 pci_dev->driver = NULL;
249 * If the device is still on, set the power state as "unknown",
250 * since it might change by the next time we load the driver.
252 if (pci_dev->current_state == PCI_D0)
253 pci_dev->current_state = PCI_UNKNOWN;
256 * We would love to complain here if pci_dev->is_enabled is set, that
257 * the driver should have called pci_disable_device(), but the
258 * unfortunate fact is there are too many odd BIOS and bridge setups
259 * that don't like drivers doing that all of the time.
260 * Oh well, we can dream of sane hardware when we sleep, no matter how
261 * horrible the crap we have to deal with is when we are awake...
264 pci_dev_put(pci_dev);
268 static int pci_device_suspend(struct device * dev, pm_message_t state)
270 struct pci_dev * pci_dev = to_pci_dev(dev);
271 struct pci_driver * drv = pci_dev->driver;
274 if (drv && drv->suspend) {
275 i = drv->suspend(pci_dev, state);
276 suspend_report_result(drv->suspend, i);
278 pci_save_state(pci_dev);
280 * mark its power state as "unknown", since we don't know if
281 * e.g. the BIOS will change its device state when we suspend.
283 if (pci_dev->current_state == PCI_D0)
284 pci_dev->current_state = PCI_UNKNOWN;
289 static int pci_device_suspend_late(struct device * dev, pm_message_t state)
291 struct pci_dev * pci_dev = to_pci_dev(dev);
292 struct pci_driver * drv = pci_dev->driver;
295 if (drv && drv->suspend_late) {
296 i = drv->suspend_late(pci_dev, state);
297 suspend_report_result(drv->suspend_late, i);
303 * Default resume method for devices that have no driver provided resume,
304 * or not even a driver at all.
306 static int pci_default_resume(struct pci_dev *pci_dev)
310 /* restore the PCI config space */
311 pci_restore_state(pci_dev);
312 /* if the device was enabled before suspend, reenable */
313 retval = __pci_reenable_device(pci_dev);
314 /* if the device was busmaster before the suspend, make it busmaster again */
315 if (pci_dev->is_busmaster)
316 pci_set_master(pci_dev);
321 static int pci_device_resume(struct device * dev)
324 struct pci_dev * pci_dev = to_pci_dev(dev);
325 struct pci_driver * drv = pci_dev->driver;
327 if (drv && drv->resume)
328 error = drv->resume(pci_dev);
330 error = pci_default_resume(pci_dev);
334 static int pci_device_resume_early(struct device * dev)
337 struct pci_dev * pci_dev = to_pci_dev(dev);
338 struct pci_driver * drv = pci_dev->driver;
340 pci_fixup_device(pci_fixup_resume, pci_dev);
342 if (drv && drv->resume_early)
343 error = drv->resume_early(pci_dev);
347 static void pci_device_shutdown(struct device *dev)
349 struct pci_dev *pci_dev = to_pci_dev(dev);
350 struct pci_driver *drv = pci_dev->driver;
352 if (drv && drv->shutdown)
353 drv->shutdown(pci_dev);
356 #define kobj_to_pci_driver(obj) container_of(obj, struct device_driver, kobj)
357 #define attr_to_driver_attribute(obj) container_of(obj, struct driver_attribute, attr)
360 pci_driver_attr_show(struct kobject * kobj, struct attribute *attr, char *buf)
362 struct device_driver *driver = kobj_to_pci_driver(kobj);
363 struct driver_attribute *dattr = attr_to_driver_attribute(attr);
366 if (!get_driver(driver))
369 ret = dattr->show ? dattr->show(driver, buf) : -EIO;
376 pci_driver_attr_store(struct kobject * kobj, struct attribute *attr,
377 const char *buf, size_t count)
379 struct device_driver *driver = kobj_to_pci_driver(kobj);
380 struct driver_attribute *dattr = attr_to_driver_attribute(attr);
383 if (!get_driver(driver))
386 ret = dattr->store ? dattr->store(driver, buf, count) : -EIO;
392 static struct sysfs_ops pci_driver_sysfs_ops = {
393 .show = pci_driver_attr_show,
394 .store = pci_driver_attr_store,
396 static struct kobj_type pci_driver_kobj_type = {
397 .sysfs_ops = &pci_driver_sysfs_ops,
401 * __pci_register_driver - register a new pci driver
402 * @drv: the driver structure to register
403 * @owner: owner module of drv
404 * @mod_name: module name string
406 * Adds the driver structure to the list of registered drivers.
407 * Returns a negative value on error, otherwise 0.
408 * If no error occurred, the driver remains registered even if
409 * no device was claimed during registration.
411 int __pci_register_driver(struct pci_driver *drv, struct module *owner,
412 const char *mod_name)
416 /* initialize common driver fields */
417 drv->driver.name = drv->name;
418 drv->driver.bus = &pci_bus_type;
419 drv->driver.owner = owner;
420 drv->driver.mod_name = mod_name;
421 drv->driver.kobj.ktype = &pci_driver_kobj_type;
423 spin_lock_init(&drv->dynids.lock);
424 INIT_LIST_HEAD(&drv->dynids.list);
426 /* register with core */
427 error = driver_register(&drv->driver);
431 error = pci_create_newid_file(drv);
433 driver_unregister(&drv->driver);
439 * pci_unregister_driver - unregister a pci driver
440 * @drv: the driver structure to unregister
442 * Deletes the driver structure from the list of registered PCI drivers,
443 * gives it a chance to clean up by calling its remove() function for
444 * each device it was responsible for, and marks those devices as
449 pci_unregister_driver(struct pci_driver *drv)
451 driver_unregister(&drv->driver);
452 pci_free_dynids(drv);
455 static struct pci_driver pci_compat_driver = {
460 * pci_dev_driver - get the pci_driver of a device
461 * @dev: the device to query
463 * Returns the appropriate pci_driver structure or %NULL if there is no
464 * registered driver for the device.
467 pci_dev_driver(const struct pci_dev *dev)
473 for(i=0; i<=PCI_ROM_RESOURCE; i++)
474 if (dev->resource[i].flags & IORESOURCE_BUSY)
475 return &pci_compat_driver;
481 * pci_bus_match - Tell if a PCI device structure has a matching PCI device id structure
482 * @dev: the PCI device structure to match against
483 * @drv: the device driver to search for matching PCI device id structures
485 * Used by a driver to check whether a PCI device present in the
486 * system is in its list of supported devices. Returns the matching
487 * pci_device_id structure or %NULL if there is no match.
489 static int pci_bus_match(struct device *dev, struct device_driver *drv)
491 struct pci_dev *pci_dev = to_pci_dev(dev);
492 struct pci_driver *pci_drv = to_pci_driver(drv);
493 const struct pci_device_id *found_id;
495 found_id = pci_match_device(pci_drv, pci_dev);
503 * pci_dev_get - increments the reference count of the pci device structure
504 * @dev: the device being referenced
506 * Each live reference to a device should be refcounted.
508 * Drivers for PCI devices should normally record such references in
509 * their probe() methods, when they bind to a device, and release
510 * them by calling pci_dev_put(), in their disconnect() methods.
512 * A pointer to the device with the incremented reference counter is returned.
514 struct pci_dev *pci_dev_get(struct pci_dev *dev)
517 get_device(&dev->dev);
522 * pci_dev_put - release a use of the pci device structure
523 * @dev: device that's been disconnected
525 * Must be called when a user of a device is finished with it. When the last
526 * user of the device calls this function, the memory of the device is freed.
528 void pci_dev_put(struct pci_dev *dev)
531 put_device(&dev->dev);
534 #ifndef CONFIG_HOTPLUG
535 int pci_uevent(struct device *dev, char **envp, int num_envp,
536 char *buffer, int buffer_size)
542 struct bus_type pci_bus_type = {
544 .match = pci_bus_match,
545 .uevent = pci_uevent,
546 .probe = pci_device_probe,
547 .remove = pci_device_remove,
548 .suspend = pci_device_suspend,
549 .suspend_late = pci_device_suspend_late,
550 .resume_early = pci_device_resume_early,
551 .resume = pci_device_resume,
552 .shutdown = pci_device_shutdown,
553 .dev_attrs = pci_dev_attrs,
556 static int __init pci_driver_init(void)
558 return bus_register(&pci_bus_type);
561 postcore_initcall(pci_driver_init);
563 EXPORT_SYMBOL(pci_match_id);
564 EXPORT_SYMBOL(pci_match_device);
565 EXPORT_SYMBOL(__pci_register_driver);
566 EXPORT_SYMBOL(pci_unregister_driver);
567 EXPORT_SYMBOL(pci_dev_driver);
568 EXPORT_SYMBOL(pci_bus_type);
569 EXPORT_SYMBOL(pci_dev_get);
570 EXPORT_SYMBOL(pci_dev_put);