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;
303 if (drv && drv->suspend)
304 return drv->suspend(pci_dev,state);
308 static int pci_device_resume(struct device * dev)
310 struct pci_dev * pci_dev = to_pci_dev(dev);
311 struct pci_driver * drv = pci_dev->driver;
313 if (drv && drv->resume)
314 drv->resume(pci_dev);
319 #define kobj_to_pci_driver(obj) container_of(obj, struct device_driver, kobj)
320 #define attr_to_driver_attribute(obj) container_of(obj, struct driver_attribute, attr)
323 pci_driver_attr_show(struct kobject * kobj, struct attribute *attr, char *buf)
325 struct device_driver *driver = kobj_to_pci_driver(kobj);
326 struct driver_attribute *dattr = attr_to_driver_attribute(attr);
329 if (get_driver(driver)) {
331 ret = dattr->show(driver, buf);
338 pci_driver_attr_store(struct kobject * kobj, struct attribute *attr,
339 const char *buf, size_t count)
341 struct device_driver *driver = kobj_to_pci_driver(kobj);
342 struct driver_attribute *dattr = attr_to_driver_attribute(attr);
345 if (get_driver(driver)) {
347 ret = dattr->store(driver, buf, count);
353 static struct sysfs_ops pci_driver_sysfs_ops = {
354 .show = pci_driver_attr_show,
355 .store = pci_driver_attr_store,
357 static struct kobj_type pci_driver_kobj_type = {
358 .sysfs_ops = &pci_driver_sysfs_ops,
362 pci_populate_driver_dir(struct pci_driver *drv)
364 return pci_create_newid_file(drv);
368 * pci_register_driver - register a new pci driver
369 * @drv: the driver structure to register
371 * Adds the driver structure to the list of registered drivers
372 * Returns the number of pci devices which were claimed by the driver
373 * during registration. The driver remains registered even if the
374 * return value is zero.
377 pci_register_driver(struct pci_driver *drv)
381 /* initialize common driver fields */
382 drv->driver.name = drv->name;
383 drv->driver.bus = &pci_bus_type;
384 drv->driver.probe = pci_device_probe;
385 drv->driver.remove = pci_device_remove;
386 drv->driver.kobj.ktype = &pci_driver_kobj_type;
387 pci_init_dynids(&drv->dynids);
389 /* register with core */
390 count = driver_register(&drv->driver);
393 pci_populate_driver_dir(drv);
396 return count ? count : 1;
400 * pci_unregister_driver - unregister a pci driver
401 * @drv: the driver structure to unregister
403 * Deletes the driver structure from the list of registered PCI drivers,
404 * gives it a chance to clean up by calling its remove() function for
405 * each device it was responsible for, and marks those devices as
410 pci_unregister_driver(struct pci_driver *drv)
412 driver_unregister(&drv->driver);
413 pci_free_dynids(drv);
416 static struct pci_driver pci_compat_driver = {
421 * pci_dev_driver - get the pci_driver of a device
422 * @dev: the device to query
424 * Returns the appropriate pci_driver structure or %NULL if there is no
425 * registered driver for the device.
428 pci_dev_driver(const struct pci_dev *dev)
434 for(i=0; i<=PCI_ROM_RESOURCE; i++)
435 if (dev->resource[i].flags & IORESOURCE_BUSY)
436 return &pci_compat_driver;
442 * pci_bus_match - Tell if a PCI device structure has a matching PCI device id structure
443 * @ids: array of PCI device id structures to search in
444 * @dev: the PCI device structure to match against
446 * Used by a driver to check whether a PCI device present in the
447 * system is in its list of supported devices.Returns the matching
448 * pci_device_id structure or %NULL if there is no match.
450 static int pci_bus_match(struct device * dev, struct device_driver * drv)
452 const struct pci_dev * pci_dev = to_pci_dev(dev);
453 struct pci_driver * pci_drv = to_pci_driver(drv);
454 const struct pci_device_id * ids = pci_drv->id_table;
455 const struct pci_device_id *found_id;
460 found_id = pci_match_device(ids, pci_dev);
464 return pci_bus_match_dynids(pci_dev, pci_drv);
468 * pci_dev_get - increments the reference count of the pci device structure
469 * @dev: the device being referenced
471 * Each live reference to a device should be refcounted.
473 * Drivers for PCI devices should normally record such references in
474 * their probe() methods, when they bind to a device, and release
475 * them by calling pci_dev_put(), in their disconnect() methods.
477 * A pointer to the device with the incremented reference counter is returned.
479 struct pci_dev *pci_dev_get(struct pci_dev *dev)
486 tmp = get_device(&dev->dev);
488 return to_pci_dev(tmp);
494 * pci_dev_put - release a use of the pci device structure
495 * @dev: device that's been disconnected
497 * Must be called when a user of a device is finished with it. When the last
498 * user of the device calls this function, the memory of the device is freed.
500 void pci_dev_put(struct pci_dev *dev)
503 put_device(&dev->dev);
506 #ifndef CONFIG_HOTPLUG
507 int pci_hotplug (struct device *dev, char **envp, int num_envp,
508 char *buffer, int buffer_size)
514 struct bus_type pci_bus_type = {
516 .match = pci_bus_match,
517 .hotplug = pci_hotplug,
518 .suspend = pci_device_suspend,
519 .resume = pci_device_resume,
522 static int __init pci_driver_init(void)
524 return bus_register(&pci_bus_type);
527 postcore_initcall(pci_driver_init);
529 EXPORT_SYMBOL(pci_match_device);
530 EXPORT_SYMBOL(pci_register_driver);
531 EXPORT_SYMBOL(pci_unregister_driver);
532 EXPORT_SYMBOL(pci_dev_driver);
533 EXPORT_SYMBOL(pci_bus_type);
534 EXPORT_SYMBOL(pci_dev_get);
535 EXPORT_SYMBOL(pci_dev_put);