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;
305 /* Translate PM_SUSPEND_xx states to PCI device states */
306 static u32 state_conversion[] = {
308 [PM_SUSPEND_STANDBY] = 1,
309 [PM_SUSPEND_MEM] = 3,
310 [PM_SUSPEND_DISK] = 3,
313 if (state >= sizeof(state_conversion) / sizeof(state_conversion[1]))
316 dev_state = state_conversion[state];
317 if (drv && drv->suspend)
318 i = drv->suspend(pci_dev, dev_state);
320 pci_save_state(pci_dev, pci_dev->saved_config_space);
326 * Default resume method for devices that have no driver provided resume,
327 * or not even a driver at all.
329 static void pci_default_resume(struct pci_dev *pci_dev)
331 /* restore the PCI config space */
332 pci_restore_state(pci_dev, pci_dev->saved_config_space);
333 /* if the device was enabled before suspend, reenable */
334 if (pci_dev->is_enabled)
335 pci_enable_device(pci_dev);
336 /* if the device was busmaster before the suspend, make it busmaster again */
337 if (pci_dev->is_busmaster)
338 pci_set_master(pci_dev);
341 static int pci_device_resume(struct device * dev)
343 struct pci_dev * pci_dev = to_pci_dev(dev);
344 struct pci_driver * drv = pci_dev->driver;
346 if (drv && drv->resume)
347 drv->resume(pci_dev);
349 pci_default_resume(pci_dev);
354 #define kobj_to_pci_driver(obj) container_of(obj, struct device_driver, kobj)
355 #define attr_to_driver_attribute(obj) container_of(obj, struct driver_attribute, attr)
358 pci_driver_attr_show(struct kobject * kobj, struct attribute *attr, char *buf)
360 struct device_driver *driver = kobj_to_pci_driver(kobj);
361 struct driver_attribute *dattr = attr_to_driver_attribute(attr);
364 if (get_driver(driver)) {
366 ret = dattr->show(driver, buf);
373 pci_driver_attr_store(struct kobject * kobj, struct attribute *attr,
374 const char *buf, size_t count)
376 struct device_driver *driver = kobj_to_pci_driver(kobj);
377 struct driver_attribute *dattr = attr_to_driver_attribute(attr);
380 if (get_driver(driver)) {
382 ret = dattr->store(driver, buf, count);
388 static struct sysfs_ops pci_driver_sysfs_ops = {
389 .show = pci_driver_attr_show,
390 .store = pci_driver_attr_store,
392 static struct kobj_type pci_driver_kobj_type = {
393 .sysfs_ops = &pci_driver_sysfs_ops,
397 pci_populate_driver_dir(struct pci_driver *drv)
399 return pci_create_newid_file(drv);
403 * pci_register_driver - register a new pci driver
404 * @drv: the driver structure to register
406 * Adds the driver structure to the list of registered drivers.
407 * Returns a negative value on error. The driver remains registered
408 * even if no device was claimed during registration.
411 pci_register_driver(struct pci_driver *drv)
415 /* initialize common driver fields */
416 drv->driver.name = drv->name;
417 drv->driver.bus = &pci_bus_type;
418 drv->driver.probe = pci_device_probe;
419 drv->driver.remove = pci_device_remove;
420 drv->driver.kobj.ktype = &pci_driver_kobj_type;
421 pci_init_dynids(&drv->dynids);
423 /* register with core */
424 count = driver_register(&drv->driver);
427 pci_populate_driver_dir(drv);
430 return count ? count : 1;
434 * pci_unregister_driver - unregister a pci driver
435 * @drv: the driver structure to unregister
437 * Deletes the driver structure from the list of registered PCI drivers,
438 * gives it a chance to clean up by calling its remove() function for
439 * each device it was responsible for, and marks those devices as
444 pci_unregister_driver(struct pci_driver *drv)
446 driver_unregister(&drv->driver);
447 pci_free_dynids(drv);
450 static struct pci_driver pci_compat_driver = {
455 * pci_dev_driver - get the pci_driver of a device
456 * @dev: the device to query
458 * Returns the appropriate pci_driver structure or %NULL if there is no
459 * registered driver for the device.
462 pci_dev_driver(const struct pci_dev *dev)
468 for(i=0; i<=PCI_ROM_RESOURCE; i++)
469 if (dev->resource[i].flags & IORESOURCE_BUSY)
470 return &pci_compat_driver;
476 * pci_bus_match - Tell if a PCI device structure has a matching PCI device id structure
477 * @ids: array of PCI device id structures to search in
478 * @dev: the PCI device structure to match against
480 * Used by a driver to check whether a PCI device present in the
481 * system is in its list of supported devices.Returns the matching
482 * pci_device_id structure or %NULL if there is no match.
484 static int pci_bus_match(struct device * dev, struct device_driver * drv)
486 const struct pci_dev * pci_dev = to_pci_dev(dev);
487 struct pci_driver * pci_drv = to_pci_driver(drv);
488 const struct pci_device_id * ids = pci_drv->id_table;
489 const struct pci_device_id *found_id;
494 found_id = pci_match_device(ids, pci_dev);
498 return pci_bus_match_dynids(pci_dev, pci_drv);
502 * pci_dev_get - increments the reference count of the pci device structure
503 * @dev: the device being referenced
505 * Each live reference to a device should be refcounted.
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.
511 * A pointer to the device with the incremented reference counter is returned.
513 struct pci_dev *pci_dev_get(struct pci_dev *dev)
520 tmp = get_device(&dev->dev);
522 return to_pci_dev(tmp);
528 * pci_dev_put - release a use of the pci device structure
529 * @dev: device that's been disconnected
531 * Must be called when a user of a device is finished with it. When the last
532 * user of the device calls this function, the memory of the device is freed.
534 void pci_dev_put(struct pci_dev *dev)
537 put_device(&dev->dev);
540 #ifndef CONFIG_HOTPLUG
541 int pci_hotplug (struct device *dev, char **envp, int num_envp,
542 char *buffer, int buffer_size)
548 struct bus_type pci_bus_type = {
550 .match = pci_bus_match,
551 .hotplug = pci_hotplug,
552 .suspend = pci_device_suspend,
553 .resume = pci_device_resume,
554 .dev_attrs = pci_dev_attrs,
557 static int __init pci_driver_init(void)
559 return bus_register(&pci_bus_type);
562 postcore_initcall(pci_driver_init);
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);