2 * drivers/pci/pci-driver.c
7 #include <linux/module.h>
8 #include <linux/init.h>
9 #include <linux/device.h>
10 #include <linux/pci-dynids.h>
14 * Registration of PCI drivers and handling of hot-pluggable devices.
18 * pci_match_one_device - Tell if a PCI device structure has a matching
19 * PCI device id structure
20 * @id: single PCI device id structure to match
21 * @dev: the PCI device structure to match against
23 * Returns the matching pci_device_id structure or %NULL if there is no match.
26 static inline const struct pci_device_id *
27 pci_match_one_device(const struct pci_device_id *id, const struct pci_dev *dev)
29 if ((id->vendor == PCI_ANY_ID || id->vendor == dev->vendor) &&
30 (id->device == PCI_ANY_ID || id->device == dev->device) &&
31 (id->subvendor == PCI_ANY_ID || id->subvendor == dev->subsystem_vendor) &&
32 (id->subdevice == PCI_ANY_ID || id->subdevice == dev->subsystem_device) &&
33 !((id->class ^ dev->class) & id->class_mask))
39 * Dynamic device IDs are disabled for !CONFIG_HOTPLUG
44 * pci_device_probe_dynamic()
46 * Walk the dynamic ID list looking for a match.
47 * returns 0 and sets pci_dev->driver when drv claims pci_dev, else error.
50 pci_device_probe_dynamic(struct pci_driver *drv, struct pci_dev *pci_dev)
53 struct list_head *pos;
56 spin_lock(&drv->dynids.lock);
57 list_for_each(pos, &drv->dynids.list) {
58 dynid = list_entry(pos, struct dynid, node);
59 if (pci_match_one_device(&dynid->id, pci_dev)) {
60 spin_unlock(&drv->dynids.lock);
61 error = drv->probe(pci_dev, &dynid->id);
63 pci_dev->driver = drv;
69 spin_unlock(&drv->dynids.lock);
74 dynid_init(struct dynid *dynid)
76 memset(dynid, 0, sizeof(*dynid));
77 INIT_LIST_HEAD(&dynid->node);
83 * Adds a new dynamic pci device ID to this driver,
84 * and causes the driver to probe for all devices again.
87 store_new_id(struct device_driver *driver, const char *buf, size_t count)
90 struct bus_type * bus;
91 struct pci_driver *pdrv = to_pci_driver(driver);
92 __u32 vendor=PCI_ANY_ID, device=PCI_ANY_ID, subvendor=PCI_ANY_ID,
93 subdevice=PCI_ANY_ID, class=0, class_mask=0;
94 unsigned long driver_data=0;
97 fields = sscanf(buf, "%x %x %x %x %x %x %lux",
98 &vendor, &device, &subvendor, &subdevice,
99 &class, &class_mask, &driver_data);
103 dynid = kmalloc(sizeof(*dynid), GFP_KERNEL);
108 dynid->id.vendor = vendor;
109 dynid->id.device = device;
110 dynid->id.subvendor = subvendor;
111 dynid->id.subdevice = subdevice;
112 dynid->id.class = class;
113 dynid->id.class_mask = class_mask;
114 dynid->id.driver_data = pdrv->dynids.use_driver_data ?
117 spin_lock(&pdrv->dynids.lock);
118 list_add_tail(&pdrv->dynids.list, &dynid->node);
119 spin_unlock(&pdrv->dynids.lock);
121 bus = get_bus(pdrv->driver.bus);
123 if (get_driver(&pdrv->driver)) {
124 down_write(&bus->subsys.rwsem);
125 driver_attach(&pdrv->driver);
126 up_write(&bus->subsys.rwsem);
127 put_driver(&pdrv->driver);
135 static DRIVER_ATTR(new_id, S_IWUSR, NULL, store_new_id);
137 pci_init_dynids(struct pci_dynids *dynids)
139 memset(dynids, 0, sizeof(*dynids));
140 spin_lock_init(&dynids->lock);
141 INIT_LIST_HEAD(&dynids->list);
145 pci_free_dynids(struct pci_driver *drv)
147 struct list_head *pos, *n;
150 spin_lock(&drv->dynids.lock);
151 list_for_each_safe(pos, n, &drv->dynids.list) {
152 dynid = list_entry(pos, struct dynid, node);
153 list_del(&dynid->node);
156 spin_unlock(&drv->dynids.lock);
160 pci_create_newid_file(struct pci_driver *drv)
163 if (drv->probe != NULL)
164 error = sysfs_create_file(&drv->driver.kobj,
165 &driver_attr_new_id.attr);
170 pci_bus_match_dynids(const struct pci_dev *pci_dev, struct pci_driver *pci_drv)
172 struct list_head *pos;
175 spin_lock(&pci_drv->dynids.lock);
176 list_for_each(pos, &pci_drv->dynids.list) {
177 dynid = list_entry(pos, struct dynid, node);
178 if (pci_match_one_device(&dynid->id, pci_dev)) {
179 spin_unlock(&pci_drv->dynids.lock);
183 spin_unlock(&pci_drv->dynids.lock);
187 #else /* !CONFIG_HOTPLUG */
188 static inline int pci_device_probe_dynamic(struct pci_driver *drv, struct pci_dev *pci_dev)
192 static inline void dynid_init(struct dynid *dynid) {}
193 static inline void pci_init_dynids(struct pci_dynids *dynids) {}
194 static inline void pci_free_dynids(struct pci_driver *drv) {}
195 static inline int pci_create_newid_file(struct pci_driver *drv)
199 static inline int pci_bus_match_dynids(const struct pci_dev *pci_dev, struct pci_driver *pci_drv)
206 * pci_match_device - Tell if a PCI device structure has a matching
207 * PCI device id structure
208 * @ids: array of PCI device id structures to search in
209 * @dev: the PCI device structure to match against
211 * Used by a driver to check whether a PCI device present in the
212 * system is in its list of supported devices.Returns the matching
213 * pci_device_id structure or %NULL if there is no match.
215 const struct pci_device_id *
216 pci_match_device(const struct pci_device_id *ids, const struct pci_dev *dev)
218 while (ids->vendor || ids->subvendor || ids->class_mask) {
219 if (pci_match_one_device(ids, dev))
227 * pci_device_probe_static()
229 * returns 0 and sets pci_dev->driver when drv claims pci_dev, else error.
232 pci_device_probe_static(struct pci_driver *drv, struct pci_dev *pci_dev)
235 const struct pci_device_id *id;
239 id = pci_match_device(drv->id_table, pci_dev);
241 error = drv->probe(pci_dev, id);
243 pci_dev->driver = drv;
250 * __pci_device_probe()
252 * returns 0 on success, else error.
253 * side-effect: pci_dev->driver is set to drv when drv claims pci_dev.
256 __pci_device_probe(struct pci_driver *drv, struct pci_dev *pci_dev)
260 if (!pci_dev->driver && drv->probe) {
261 error = pci_device_probe_static(drv, pci_dev);
262 if (error == -ENODEV)
263 error = pci_device_probe_dynamic(drv, pci_dev);
268 static int pci_device_probe(struct device * dev)
271 struct pci_driver *drv;
272 struct pci_dev *pci_dev;
274 drv = to_pci_driver(dev->driver);
275 pci_dev = to_pci_dev(dev);
276 pci_dev_get(pci_dev);
277 error = __pci_device_probe(drv, pci_dev);
279 pci_dev_put(pci_dev);
284 static int pci_device_remove(struct device * dev)
286 struct pci_dev * pci_dev = to_pci_dev(dev);
287 struct pci_driver * drv = pci_dev->driver;
291 drv->remove(pci_dev);
292 pci_dev->driver = NULL;
294 pci_dev_put(pci_dev);
298 static int pci_device_suspend(struct device * dev, u32 state)
300 struct pci_dev * pci_dev = to_pci_dev(dev);
301 struct pci_driver * drv = pci_dev->driver;
304 if (drv && drv->suspend)
305 i = drv->suspend(pci_dev,state);
307 pci_save_state(pci_dev, pci_dev->saved_config_space);
313 * Default resume method for devices that have no driver provided resume,
314 * or not even a driver at all.
316 static void pci_default_resume(struct pci_dev *pci_dev)
318 /* restore the PCI config space */
319 pci_restore_state(pci_dev, pci_dev->saved_config_space);
320 /* if the device was enabled before suspend, reenable */
321 if (pci_dev->is_enabled)
322 pci_enable_device(pci_dev);
323 /* if the device was busmaster before the suspend, make it busmaster again */
324 if (pci_dev->is_busmaster)
325 pci_set_master(pci_dev);
328 static int pci_device_resume(struct device * dev)
330 struct pci_dev * pci_dev = to_pci_dev(dev);
331 struct pci_driver * drv = pci_dev->driver;
333 if (drv && drv->resume)
334 drv->resume(pci_dev);
336 pci_default_resume(pci_dev);
341 #define kobj_to_pci_driver(obj) container_of(obj, struct device_driver, kobj)
342 #define attr_to_driver_attribute(obj) container_of(obj, struct driver_attribute, attr)
345 pci_driver_attr_show(struct kobject * kobj, struct attribute *attr, char *buf)
347 struct device_driver *driver = kobj_to_pci_driver(kobj);
348 struct driver_attribute *dattr = attr_to_driver_attribute(attr);
351 if (get_driver(driver)) {
353 ret = dattr->show(driver, buf);
360 pci_driver_attr_store(struct kobject * kobj, struct attribute *attr,
361 const char *buf, size_t count)
363 struct device_driver *driver = kobj_to_pci_driver(kobj);
364 struct driver_attribute *dattr = attr_to_driver_attribute(attr);
367 if (get_driver(driver)) {
369 ret = dattr->store(driver, buf, count);
375 static struct sysfs_ops pci_driver_sysfs_ops = {
376 .show = pci_driver_attr_show,
377 .store = pci_driver_attr_store,
379 static struct kobj_type pci_driver_kobj_type = {
380 .sysfs_ops = &pci_driver_sysfs_ops,
384 pci_populate_driver_dir(struct pci_driver *drv)
386 return pci_create_newid_file(drv);
390 * pci_register_driver - register a new pci driver
391 * @drv: the driver structure to register
393 * Adds the driver structure to the list of registered drivers
394 * Returns the number of pci devices which were claimed by the driver
395 * during registration. The driver remains registered even if the
396 * return value is zero.
399 pci_register_driver(struct pci_driver *drv)
403 /* initialize common driver fields */
404 drv->driver.name = drv->name;
405 drv->driver.bus = &pci_bus_type;
406 drv->driver.probe = pci_device_probe;
407 drv->driver.remove = pci_device_remove;
408 drv->driver.kobj.ktype = &pci_driver_kobj_type;
409 pci_init_dynids(&drv->dynids);
411 /* register with core */
412 count = driver_register(&drv->driver);
415 pci_populate_driver_dir(drv);
418 return count ? count : 1;
422 * pci_unregister_driver - unregister a pci driver
423 * @drv: the driver structure to unregister
425 * Deletes the driver structure from the list of registered PCI drivers,
426 * gives it a chance to clean up by calling its remove() function for
427 * each device it was responsible for, and marks those devices as
432 pci_unregister_driver(struct pci_driver *drv)
434 driver_unregister(&drv->driver);
435 pci_free_dynids(drv);
438 static struct pci_driver pci_compat_driver = {
443 * pci_dev_driver - get the pci_driver of a device
444 * @dev: the device to query
446 * Returns the appropriate pci_driver structure or %NULL if there is no
447 * registered driver for the device.
450 pci_dev_driver(const struct pci_dev *dev)
456 for(i=0; i<=PCI_ROM_RESOURCE; i++)
457 if (dev->resource[i].flags & IORESOURCE_BUSY)
458 return &pci_compat_driver;
464 * pci_bus_match - Tell if a PCI device structure has a matching PCI device id structure
465 * @ids: array of PCI device id structures to search in
466 * @dev: the PCI device structure to match against
468 * Used by a driver to check whether a PCI device present in the
469 * system is in its list of supported devices.Returns the matching
470 * pci_device_id structure or %NULL if there is no match.
472 static int pci_bus_match(struct device * dev, struct device_driver * drv)
474 const struct pci_dev * pci_dev = to_pci_dev(dev);
475 struct pci_driver * pci_drv = to_pci_driver(drv);
476 const struct pci_device_id * ids = pci_drv->id_table;
477 const struct pci_device_id *found_id;
482 found_id = pci_match_device(ids, pci_dev);
486 return pci_bus_match_dynids(pci_dev, pci_drv);
490 * pci_dev_get - increments the reference count of the pci device structure
491 * @dev: the device being referenced
493 * Each live reference to a device should be refcounted.
495 * Drivers for PCI devices should normally record such references in
496 * their probe() methods, when they bind to a device, and release
497 * them by calling pci_dev_put(), in their disconnect() methods.
499 * A pointer to the device with the incremented reference counter is returned.
501 struct pci_dev *pci_dev_get(struct pci_dev *dev)
508 tmp = get_device(&dev->dev);
510 return to_pci_dev(tmp);
516 * pci_dev_put - release a use of the pci device structure
517 * @dev: device that's been disconnected
519 * Must be called when a user of a device is finished with it. When the last
520 * user of the device calls this function, the memory of the device is freed.
522 void pci_dev_put(struct pci_dev *dev)
525 put_device(&dev->dev);
528 #ifndef CONFIG_HOTPLUG
529 int pci_hotplug (struct device *dev, char **envp, int num_envp,
530 char *buffer, int buffer_size)
536 struct bus_type pci_bus_type = {
538 .match = pci_bus_match,
539 .hotplug = pci_hotplug,
540 .suspend = pci_device_suspend,
541 .resume = pci_device_resume,
544 static int __init pci_driver_init(void)
546 return bus_register(&pci_bus_type);
549 postcore_initcall(pci_driver_init);
551 EXPORT_SYMBOL(pci_match_device);
552 EXPORT_SYMBOL(pci_register_driver);
553 EXPORT_SYMBOL(pci_unregister_driver);
554 EXPORT_SYMBOL(pci_dev_driver);
555 EXPORT_SYMBOL(pci_bus_type);
556 EXPORT_SYMBOL(pci_dev_get);
557 EXPORT_SYMBOL(pci_dev_put);