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 * pci_match_device - Tell if a PCI device structure has a matching
40 * PCI device id structure
41 * @ids: array of PCI device id structures to search in
42 * @dev: the PCI device structure to match against
44 * Used by a driver to check whether a PCI device present in the
45 * system is in its list of supported devices.Returns the matching
46 * pci_device_id structure or %NULL if there is no match.
48 const struct pci_device_id
*
49 pci_match_device(const struct pci_device_id
*ids
, const struct pci_dev
*dev
)
51 while (ids
->vendor
|| ids
->subvendor
|| ids
->class_mask
) {
52 if (pci_match_one_device(ids
, dev
))
60 * pci_device_probe_static()
62 * returns 0 and sets pci_dev->driver when drv claims pci_dev, else error.
65 pci_device_probe_static(struct pci_driver
*drv
, struct pci_dev
*pci_dev
)
68 const struct pci_device_id
*id
;
72 id
= pci_match_device(drv
->id_table
, pci_dev
);
74 error
= drv
->probe(pci_dev
, id
);
76 pci_dev
->driver
= drv
;
83 * pci_device_probe_dynamic()
85 * Walk the dynamic ID list looking for a match.
86 * returns 0 and sets pci_dev->driver when drv claims pci_dev, else error.
89 pci_device_probe_dynamic(struct pci_driver
*drv
, struct pci_dev
*pci_dev
)
92 struct list_head
*pos
;
95 spin_lock(&drv
->dynids
.lock
);
96 list_for_each(pos
, &drv
->dynids
.list
) {
97 dynid
= list_entry(pos
, struct dynid
, node
);
98 if (pci_match_one_device(&dynid
->id
, pci_dev
)) {
99 spin_unlock(&drv
->dynids
.lock
);
100 error
= drv
->probe(pci_dev
, &dynid
->id
);
102 pci_dev
->driver
= drv
;
108 spin_unlock(&drv
->dynids
.lock
);
113 * __pci_device_probe()
115 * returns 0 on success, else error.
116 * side-effect: pci_dev->driver is set to drv when drv claims pci_dev.
119 __pci_device_probe(struct pci_driver
*drv
, struct pci_dev
*pci_dev
)
123 if (!pci_dev
->driver
&& drv
->probe
) {
124 error
= pci_device_probe_static(drv
, pci_dev
);
128 error
= pci_device_probe_dynamic(drv
, pci_dev
);
133 static int pci_device_probe(struct device
* dev
)
136 struct pci_driver
*drv
;
137 struct pci_dev
*pci_dev
;
139 drv
= to_pci_driver(dev
->driver
);
140 pci_dev
= to_pci_dev(dev
);
141 pci_dev_get(pci_dev
);
142 error
= __pci_device_probe(drv
, pci_dev
);
144 pci_dev_put(pci_dev
);
149 static int pci_device_remove(struct device
* dev
)
151 struct pci_dev
* pci_dev
= to_pci_dev(dev
);
152 struct pci_driver
* drv
= pci_dev
->driver
;
156 drv
->remove(pci_dev
);
157 pci_dev
->driver
= NULL
;
159 pci_dev_put(pci_dev
);
163 static int pci_device_suspend(struct device
* dev
, u32 state
, u32 level
)
165 struct pci_dev
* pci_dev
= to_pci_dev(dev
);
168 if (pci_dev
->driver
) {
169 if (level
== SUSPEND_SAVE_STATE
&& pci_dev
->driver
->save_state
)
170 error
= pci_dev
->driver
->save_state(pci_dev
,state
);
171 else if (level
== SUSPEND_POWER_DOWN
&& pci_dev
->driver
->suspend
)
172 error
= pci_dev
->driver
->suspend(pci_dev
,state
);
177 static int pci_device_resume(struct device
* dev
, u32 level
)
179 struct pci_dev
* pci_dev
= to_pci_dev(dev
);
181 if (pci_dev
->driver
) {
182 /* We may not call PCI drivers resume at
183 RESUME_POWER_ON because interrupts are not yet
184 working at that point. Calling resume at
185 RESUME_RESTORE_STATE seems like solution. */
186 if (level
== RESUME_RESTORE_STATE
&& pci_dev
->driver
->resume
)
187 pci_dev
->driver
->resume(pci_dev
);
193 dynid_init(struct dynid
*dynid
)
195 memset(dynid
, 0, sizeof(*dynid
));
196 INIT_LIST_HEAD(&dynid
->node
);
205 * Adds a new dynamic pci device ID to this driver,
206 * and causes the driver to probe for all devices again.
208 static inline ssize_t
209 store_new_id(struct device_driver
* driver
, const char * buf
, size_t count
)
212 struct bus_type
* bus
;
213 struct pci_driver
*pdrv
= to_pci_driver(driver
);
214 __u32 vendor
=PCI_ANY_ID
, device
=PCI_ANY_ID
, subvendor
=PCI_ANY_ID
,
215 subdevice
=PCI_ANY_ID
, class=0, class_mask
=0;
216 unsigned long driver_data
=0;
219 fields
= sscanf(buf
, "%x %x %x %x %x %x %lux",
220 &vendor
, &device
, &subvendor
, &subdevice
,
221 &class, &class_mask
, &driver_data
);
225 dynid
= kmalloc(sizeof(*dynid
), GFP_KERNEL
);
230 dynid
->id
.vendor
= vendor
;
231 dynid
->id
.device
= device
;
232 dynid
->id
.subvendor
= subvendor
;
233 dynid
->id
.subdevice
= subdevice
;
234 dynid
->id
.class = class;
235 dynid
->id
.class_mask
= class_mask
;
236 dynid
->id
.driver_data
= pdrv
->dynids
.use_driver_data
?
239 spin_lock(&pdrv
->dynids
.lock
);
240 list_add_tail(&pdrv
->dynids
.list
, &dynid
->node
);
241 spin_unlock(&pdrv
->dynids
.lock
);
243 bus
= get_bus(pdrv
->driver
.bus
);
245 if (get_driver(&pdrv
->driver
)) {
246 down_write(&bus
->subsys
.rwsem
);
247 driver_attach(&pdrv
->driver
);
248 up_write(&bus
->subsys
.rwsem
);
249 put_driver(&pdrv
->driver
);
257 static DRIVER_ATTR(new_id
, S_IWUSR
, NULL
, store_new_id
);
259 #define kobj_to_pci_driver(obj) container_of(obj, struct device_driver, kobj)
260 #define attr_to_driver_attribute(obj) container_of(obj, struct driver_attribute, attr)
263 pci_driver_attr_show(struct kobject
* kobj
, struct attribute
*attr
, char *buf
)
265 struct device_driver
*driver
= kobj_to_pci_driver(kobj
);
266 struct driver_attribute
*dattr
= attr_to_driver_attribute(attr
);
269 if (get_driver(driver
)) {
271 ret
= dattr
->show(driver
, buf
);
278 pci_driver_attr_store(struct kobject
* kobj
, struct attribute
*attr
,
279 const char *buf
, size_t count
)
281 struct device_driver
*driver
= kobj_to_pci_driver(kobj
);
282 struct driver_attribute
*dattr
= attr_to_driver_attribute(attr
);
285 if (get_driver(driver
)) {
287 ret
= dattr
->store(driver
, buf
, count
);
293 static struct sysfs_ops pci_driver_sysfs_ops
= {
294 .show
= pci_driver_attr_show
,
295 .store
= pci_driver_attr_store
,
297 static struct kobj_type pci_driver_kobj_type
= {
298 .sysfs_ops
= &pci_driver_sysfs_ops
,
302 pci_populate_driver_dir(struct pci_driver
* drv
)
306 if (drv
->probe
!= NULL
)
307 error
= sysfs_create_file(&drv
->driver
.kobj
,
308 &driver_attr_new_id
.attr
);
313 pci_init_dynids(struct pci_dynids
*dynids
)
315 memset(dynids
, 0, sizeof(*dynids
));
316 spin_lock_init(&dynids
->lock
);
317 INIT_LIST_HEAD(&dynids
->list
);
321 pci_free_dynids(struct pci_driver
*drv
)
323 struct list_head
*pos
, *n
;
326 spin_lock(&drv
->dynids
.lock
);
327 list_for_each_safe(pos
, n
, &drv
->dynids
.list
) {
328 dynid
= list_entry(pos
, struct dynid
, node
);
329 list_del(&dynid
->node
);
332 spin_unlock(&drv
->dynids
.lock
);
337 * pci_register_driver - register a new pci driver
338 * @drv: the driver structure to register
340 * Adds the driver structure to the list of registered drivers
341 * Returns the number of pci devices which were claimed by the driver
342 * during registration. The driver remains registered even if the
343 * return value is zero.
346 pci_register_driver(struct pci_driver
*drv
)
350 /* initialize common driver fields */
351 drv
->driver
.name
= drv
->name
;
352 drv
->driver
.bus
= &pci_bus_type
;
353 drv
->driver
.probe
= pci_device_probe
;
354 drv
->driver
.resume
= pci_device_resume
;
355 drv
->driver
.suspend
= pci_device_suspend
;
356 drv
->driver
.remove
= pci_device_remove
;
357 drv
->driver
.kobj
.ktype
= &pci_driver_kobj_type
;
358 pci_init_dynids(&drv
->dynids
);
360 /* register with core */
361 count
= driver_register(&drv
->driver
);
364 pci_populate_driver_dir(drv
);
367 return count
? count
: 1;
371 * pci_unregister_driver - unregister a pci driver
372 * @drv: the driver structure to unregister
374 * Deletes the driver structure from the list of registered PCI drivers,
375 * gives it a chance to clean up by calling its remove() function for
376 * each device it was responsible for, and marks those devices as
381 pci_unregister_driver(struct pci_driver
*drv
)
383 driver_unregister(&drv
->driver
);
384 pci_free_dynids(drv
);
387 static struct pci_driver pci_compat_driver
= {
392 * pci_dev_driver - get the pci_driver of a device
393 * @dev: the device to query
395 * Returns the appropriate pci_driver structure or %NULL if there is no
396 * registered driver for the device.
399 pci_dev_driver(const struct pci_dev
*dev
)
405 for(i
=0; i
<=PCI_ROM_RESOURCE
; i
++)
406 if (dev
->resource
[i
].flags
& IORESOURCE_BUSY
)
407 return &pci_compat_driver
;
413 * pci_bus_match - Tell if a PCI device structure has a matching PCI device id structure
414 * @ids: array of PCI device id structures to search in
415 * @dev: the PCI device structure to match against
417 * Used by a driver to check whether a PCI device present in the
418 * system is in its list of supported devices.Returns the matching
419 * pci_device_id structure or %NULL if there is no match.
421 static int pci_bus_match(struct device
* dev
, struct device_driver
* drv
)
423 const struct pci_dev
* pci_dev
= to_pci_dev(dev
);
424 struct pci_driver
* pci_drv
= to_pci_driver(drv
);
425 const struct pci_device_id
* ids
= pci_drv
->id_table
;
426 const struct pci_device_id
*found_id
;
427 struct list_head
*pos
;
433 found_id
= pci_match_device(ids
, pci_dev
);
437 spin_lock(&pci_drv
->dynids
.lock
);
438 list_for_each(pos
, &pci_drv
->dynids
.list
) {
439 dynid
= list_entry(pos
, struct dynid
, node
);
440 if (pci_match_one_device(&dynid
->id
, pci_dev
)) {
441 spin_unlock(&pci_drv
->dynids
.lock
);
445 spin_unlock(&pci_drv
->dynids
.lock
);
451 * pci_dev_get - increments the reference count of the pci device structure
452 * @dev: the device being referenced
454 * Each live reference to a device should be refcounted.
456 * Drivers for PCI devices should normally record such references in
457 * their probe() methods, when they bind to a device, and release
458 * them by calling pci_dev_put(), in their disconnect() methods.
460 * A pointer to the device with the incremented reference counter is returned.
462 struct pci_dev
*pci_dev_get(struct pci_dev
*dev
)
469 tmp
= get_device(&dev
->dev
);
471 return to_pci_dev(tmp
);
477 * pci_dev_put - release a use of the pci device structure
478 * @dev: device that's been disconnected
480 * Must be called when a user of a device is finished with it. When the last
481 * user of the device calls this function, the memory of the device is freed.
483 void pci_dev_put(struct pci_dev
*dev
)
486 put_device(&dev
->dev
);
489 #ifndef CONFIG_HOTPLUG
490 int pci_hotplug (struct device
*dev
, char **envp
, int num_envp
,
491 char *buffer
, int buffer_size
)
497 struct bus_type pci_bus_type
= {
499 .match
= pci_bus_match
,
500 .hotplug
= pci_hotplug
,
503 static int __init
pci_driver_init(void)
505 return bus_register(&pci_bus_type
);
508 postcore_initcall(pci_driver_init
);
510 EXPORT_SYMBOL(pci_match_device
);
511 EXPORT_SYMBOL(pci_register_driver
);
512 EXPORT_SYMBOL(pci_unregister_driver
);
513 EXPORT_SYMBOL(pci_dev_driver
);
514 EXPORT_SYMBOL(pci_bus_type
);
515 EXPORT_SYMBOL(pci_dev_get
);
516 EXPORT_SYMBOL(pci_dev_put
);