blob: 72cf61ed8f966d20f99c32d6acd77424e419ee69 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * drivers/pci/pci-driver.c
3 *
Greg Kroah-Hartman2b937302007-11-28 12:23:18 -08004 * (C) Copyright 2002-2004, 2007 Greg Kroah-Hartman <greg@kroah.com>
5 * (C) Copyright 2007 Novell Inc.
6 *
7 * Released under the GPL v2 only.
8 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
10
11#include <linux/pci.h>
12#include <linux/module.h>
13#include <linux/init.h>
14#include <linux/device.h>
Andi Kleend42c6992005-07-06 19:56:03 +020015#include <linux/mempolicy.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080016#include <linux/string.h>
17#include <linux/slab.h>
Tim Schmielau8c65b4a2005-11-07 00:59:43 -080018#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include "pci.h"
20
21/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 * Dynamic device IDs are disabled for !CONFIG_HOTPLUG
23 */
24
Greg Kroah-Hartman75865852005-06-30 02:18:12 -070025struct pci_dynid {
26 struct list_head node;
27 struct pci_device_id id;
28};
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Greg KH3d3c2ae2005-07-06 09:09:38 -070030#ifdef CONFIG_HOTPLUG
31
Linus Torvalds1da177e2005-04-16 15:20:36 -070032/**
Randy Dunlap8f7020d2005-10-23 11:57:38 -070033 * store_new_id - add a new PCI device ID to this driver and re-probe devices
34 * @driver: target device driver
35 * @buf: buffer for scanning device ID data
36 * @count: input size
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 *
38 * Adds a new dynamic pci device ID to this driver,
39 * and causes the driver to probe for all devices again.
40 */
Randy Dunlapf8eb1002005-10-28 20:36:51 -070041static ssize_t
Linus Torvalds1da177e2005-04-16 15:20:36 -070042store_new_id(struct device_driver *driver, const char *buf, size_t count)
43{
Greg Kroah-Hartman75865852005-06-30 02:18:12 -070044 struct pci_dynid *dynid;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 struct pci_driver *pdrv = to_pci_driver(driver);
Jean Delvare6ba18632007-04-07 17:21:28 +020046 __u32 vendor, device, subvendor=PCI_ANY_ID,
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 subdevice=PCI_ANY_ID, class=0, class_mask=0;
48 unsigned long driver_data=0;
49 int fields=0;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -070050 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52 fields = sscanf(buf, "%x %x %x %x %x %x %lux",
53 &vendor, &device, &subvendor, &subdevice,
54 &class, &class_mask, &driver_data);
Jean Delvare6ba18632007-04-07 17:21:28 +020055 if (fields < 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 return -EINVAL;
57
Eric Sesterhennf5afe802006-02-28 15:34:49 +010058 dynid = kzalloc(sizeof(*dynid), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 if (!dynid)
60 return -ENOMEM;
61
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 dynid->id.vendor = vendor;
63 dynid->id.device = device;
64 dynid->id.subvendor = subvendor;
65 dynid->id.subdevice = subdevice;
66 dynid->id.class = class;
67 dynid->id.class_mask = class_mask;
68 dynid->id.driver_data = pdrv->dynids.use_driver_data ?
69 driver_data : 0UL;
70
71 spin_lock(&pdrv->dynids.lock);
Michael Ellermana56bc692007-09-14 15:33:13 +100072 list_add_tail(&dynid->node, &pdrv->dynids.list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 spin_unlock(&pdrv->dynids.lock);
74
Greg Kroah-Hartman75865852005-06-30 02:18:12 -070075 if (get_driver(&pdrv->driver)) {
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -070076 retval = driver_attach(&pdrv->driver);
Greg Kroah-Hartman75865852005-06-30 02:18:12 -070077 put_driver(&pdrv->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 }
79
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -070080 if (retval)
81 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 return count;
83}
Linus Torvalds1da177e2005-04-16 15:20:36 -070084static DRIVER_ATTR(new_id, S_IWUSR, NULL, store_new_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86static void
87pci_free_dynids(struct pci_driver *drv)
88{
Greg Kroah-Hartman75865852005-06-30 02:18:12 -070089 struct pci_dynid *dynid, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
91 spin_lock(&drv->dynids.lock);
Greg Kroah-Hartman75865852005-06-30 02:18:12 -070092 list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 list_del(&dynid->node);
94 kfree(dynid);
95 }
96 spin_unlock(&drv->dynids.lock);
97}
98
99static int
100pci_create_newid_file(struct pci_driver *drv)
101{
102 int error = 0;
103 if (drv->probe != NULL)
Greg Kroah-Hartman03d43b12007-11-28 12:23:18 -0800104 error = driver_create_file(&drv->driver, &driver_attr_new_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 return error;
106}
107
Greg Kroah-Hartman03d43b12007-11-28 12:23:18 -0800108static void pci_remove_newid_file(struct pci_driver *drv)
109{
110 driver_remove_file(&drv->driver, &driver_attr_new_id);
111}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112#else /* !CONFIG_HOTPLUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113static inline void pci_free_dynids(struct pci_driver *drv) {}
114static inline int pci_create_newid_file(struct pci_driver *drv)
115{
116 return 0;
117}
Greg Kroah-Hartman03d43b12007-11-28 12:23:18 -0800118static inline void pci_remove_newid_file(struct pci_driver *drv) {}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119#endif
120
121/**
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700122 * pci_match_id - See if a pci device matches a given pci_id table
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 * @ids: array of PCI device id structures to search in
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700124 * @dev: the PCI device structure to match against.
125 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 * Used by a driver to check whether a PCI device present in the
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700127 * system is in its list of supported devices. Returns the matching
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 * pci_device_id structure or %NULL if there is no match.
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700129 *
Randy Dunlap8b607562007-05-09 07:19:14 +0200130 * Deprecated, don't use this as it will not catch any dynamic ids
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700131 * that a driver might want to check for.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 */
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700133const struct pci_device_id *pci_match_id(const struct pci_device_id *ids,
134 struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135{
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700136 if (ids) {
137 while (ids->vendor || ids->subvendor || ids->class_mask) {
138 if (pci_match_one_device(ids, dev))
139 return ids;
140 ids++;
141 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 }
143 return NULL;
144}
145
146/**
Randy Dunlapae9608a2007-01-09 21:41:01 -0800147 * pci_match_device - Tell if a PCI device structure has a matching PCI device id structure
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700148 * @drv: the PCI driver to match against
Henrik Kretzschmar39ba4872006-08-15 10:57:16 +0200149 * @dev: the PCI device structure to match against
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700150 *
151 * Used by a driver to check whether a PCI device present in the
152 * system is in its list of supported devices. Returns the matching
153 * pci_device_id structure or %NULL if there is no match.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 */
Adrian Bunkd73460d2007-10-24 18:27:18 +0200155static const struct pci_device_id *pci_match_device(struct pci_driver *drv,
156 struct pci_dev *dev)
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700157{
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700158 struct pci_dynid *dynid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
Russell King7461b602006-11-29 21:18:04 +0000160 /* Look at the dynamic ids first, before the static ones */
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700161 spin_lock(&drv->dynids.lock);
162 list_for_each_entry(dynid, &drv->dynids.list, node) {
163 if (pci_match_one_device(&dynid->id, dev)) {
164 spin_unlock(&drv->dynids.lock);
165 return &dynid->id;
166 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 }
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700168 spin_unlock(&drv->dynids.lock);
Russell King7461b602006-11-29 21:18:04 +0000169
170 return pci_match_id(drv->id_table, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171}
172
Andi Kleend42c6992005-07-06 19:56:03 +0200173static int pci_call_probe(struct pci_driver *drv, struct pci_dev *dev,
174 const struct pci_device_id *id)
175{
176 int error;
177#ifdef CONFIG_NUMA
178 /* Execute driver initialization on node where the
179 device's bus is attached to. This way the driver likely
180 allocates its local memory on the right node without
181 any need to change it. */
182 struct mempolicy *oldpol;
183 cpumask_t oldmask = current->cpus_allowed;
184 int node = pcibus_to_node(dev->bus);
Mike Travisf70316d2008-04-04 18:11:06 -0700185
186 if (node >= 0) {
187 node_to_cpumask_ptr(nodecpumask, node);
188 set_cpus_allowed_ptr(current, nodecpumask);
189 }
Andi Kleend42c6992005-07-06 19:56:03 +0200190 /* And set default memory allocation policy */
191 oldpol = current->mempolicy;
Lee Schermerhorn74e27e42007-11-21 15:07:05 -0800192 current->mempolicy = NULL; /* fall back to system default policy */
Andi Kleend42c6992005-07-06 19:56:03 +0200193#endif
194 error = drv->probe(dev, id);
195#ifdef CONFIG_NUMA
Mike Travisf70316d2008-04-04 18:11:06 -0700196 set_cpus_allowed_ptr(current, &oldmask);
Andi Kleend42c6992005-07-06 19:56:03 +0200197 current->mempolicy = oldpol;
198#endif
199 return error;
200}
201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202/**
203 * __pci_device_probe()
Randy Dunlap8f7020d2005-10-23 11:57:38 -0700204 * @drv: driver to call to check if it wants the PCI device
205 * @pci_dev: PCI device being probed
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 *
Randy Dunlap8f7020d2005-10-23 11:57:38 -0700207 * returns 0 on success, else error.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 * side-effect: pci_dev->driver is set to drv when drv claims pci_dev.
209 */
210static int
211__pci_device_probe(struct pci_driver *drv, struct pci_dev *pci_dev)
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700212{
213 const struct pci_device_id *id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 int error = 0;
215
216 if (!pci_dev->driver && drv->probe) {
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700217 error = -ENODEV;
218
219 id = pci_match_device(drv, pci_dev);
220 if (id)
Andi Kleend42c6992005-07-06 19:56:03 +0200221 error = pci_call_probe(drv, pci_dev, id);
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700222 if (error >= 0) {
223 pci_dev->driver = drv;
224 error = 0;
225 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 }
227 return error;
228}
229
230static int pci_device_probe(struct device * dev)
231{
232 int error = 0;
233 struct pci_driver *drv;
234 struct pci_dev *pci_dev;
235
236 drv = to_pci_driver(dev->driver);
237 pci_dev = to_pci_dev(dev);
238 pci_dev_get(pci_dev);
239 error = __pci_device_probe(drv, pci_dev);
240 if (error)
241 pci_dev_put(pci_dev);
242
243 return error;
244}
245
246static int pci_device_remove(struct device * dev)
247{
248 struct pci_dev * pci_dev = to_pci_dev(dev);
249 struct pci_driver * drv = pci_dev->driver;
250
251 if (drv) {
252 if (drv->remove)
253 drv->remove(pci_dev);
254 pci_dev->driver = NULL;
255 }
256
257 /*
Shaohua Li2449e062006-10-20 14:45:32 -0700258 * If the device is still on, set the power state as "unknown",
259 * since it might change by the next time we load the driver.
260 */
261 if (pci_dev->current_state == PCI_D0)
262 pci_dev->current_state = PCI_UNKNOWN;
263
264 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 * We would love to complain here if pci_dev->is_enabled is set, that
266 * the driver should have called pci_disable_device(), but the
267 * unfortunate fact is there are too many odd BIOS and bridge setups
268 * that don't like drivers doing that all of the time.
269 * Oh well, we can dream of sane hardware when we sleep, no matter how
270 * horrible the crap we have to deal with is when we are awake...
271 */
272
273 pci_dev_put(pci_dev);
274 return 0;
275}
276
277static int pci_device_suspend(struct device * dev, pm_message_t state)
278{
279 struct pci_dev * pci_dev = to_pci_dev(dev);
280 struct pci_driver * drv = pci_dev->driver;
281 int i = 0;
282
Andrew Morton02669492006-03-23 01:38:34 -0800283 if (drv && drv->suspend) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 i = drv->suspend(pci_dev, state);
Andrew Morton02669492006-03-23 01:38:34 -0800285 suspend_report_result(drv->suspend, i);
286 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 pci_save_state(pci_dev);
Shaohua Li2449e062006-10-20 14:45:32 -0700288 /*
289 * mark its power state as "unknown", since we don't know if
290 * e.g. the BIOS will change its device state when we suspend.
291 */
292 if (pci_dev->current_state == PCI_D0)
293 pci_dev->current_state = PCI_UNKNOWN;
Andrew Morton02669492006-03-23 01:38:34 -0800294 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 return i;
296}
297
Linus Torvaldscbd69db2006-06-24 14:50:29 -0700298static int pci_device_suspend_late(struct device * dev, pm_message_t state)
299{
300 struct pci_dev * pci_dev = to_pci_dev(dev);
301 struct pci_driver * drv = pci_dev->driver;
302 int i = 0;
303
304 if (drv && drv->suspend_late) {
305 i = drv->suspend_late(pci_dev, state);
306 suspend_report_result(drv->suspend_late, i);
307 }
308 return i;
309}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Greg Kroah-Hartman95a62962005-07-28 11:37:33 -0700311/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 * Default resume method for devices that have no driver provided resume,
313 * or not even a driver at all.
314 */
Jean Delvare8d92bc22006-04-18 14:49:56 +0200315static int pci_default_resume(struct pci_dev *pci_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316{
Jean Delvare8d92bc22006-04-18 14:49:56 +0200317 int retval = 0;
Greg Kroah-Hartman95a62962005-07-28 11:37:33 -0700318
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 /* restore the PCI config space */
320 pci_restore_state(pci_dev);
321 /* if the device was enabled before suspend, reenable */
Tejun Heo0b62e132007-07-27 14:43:35 +0900322 retval = pci_reenable_device(pci_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 /* if the device was busmaster before the suspend, make it busmaster again */
324 if (pci_dev->is_busmaster)
325 pci_set_master(pci_dev);
Jean Delvare8d92bc22006-04-18 14:49:56 +0200326
327 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328}
329
330static int pci_device_resume(struct device * dev)
331{
Jean Delvare8d92bc22006-04-18 14:49:56 +0200332 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 struct pci_dev * pci_dev = to_pci_dev(dev);
334 struct pci_driver * drv = pci_dev->driver;
335
336 if (drv && drv->resume)
Jean Delvare8d92bc22006-04-18 14:49:56 +0200337 error = drv->resume(pci_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 else
Jean Delvare8d92bc22006-04-18 14:49:56 +0200339 error = pci_default_resume(pci_dev);
340 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341}
342
Linus Torvaldscbd69db2006-06-24 14:50:29 -0700343static int pci_device_resume_early(struct device * dev)
344{
345 int error = 0;
346 struct pci_dev * pci_dev = to_pci_dev(dev);
347 struct pci_driver * drv = pci_dev->driver;
348
Alan Cox1597cac2006-12-04 15:14:45 -0800349 pci_fixup_device(pci_fixup_resume, pci_dev);
350
Linus Torvaldscbd69db2006-06-24 14:50:29 -0700351 if (drv && drv->resume_early)
352 error = drv->resume_early(pci_dev);
353 return error;
354}
355
Greg KHc8958172005-04-08 14:53:31 +0900356static void pci_device_shutdown(struct device *dev)
357{
358 struct pci_dev *pci_dev = to_pci_dev(dev);
359 struct pci_driver *drv = pci_dev->driver;
360
361 if (drv && drv->shutdown)
362 drv->shutdown(pci_dev);
Yinghai Lud52877c2008-04-23 14:58:09 -0700363 pci_msi_shutdown(pci_dev);
364 pci_msix_shutdown(pci_dev);
Greg KHc8958172005-04-08 14:53:31 +0900365}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367/**
Laurent riffard863b18f2005-10-27 23:12:54 +0200368 * __pci_register_driver - register a new pci driver
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 * @drv: the driver structure to register
Laurent riffard863b18f2005-10-27 23:12:54 +0200370 * @owner: owner module of drv
Randy Dunlapf95d8822007-02-10 14:41:56 -0800371 * @mod_name: module name string
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 *
373 * Adds the driver structure to the list of registered drivers.
374 * Returns a negative value on error, otherwise 0.
Steven Coleeaae4b32005-05-03 18:38:30 -0600375 * If no error occurred, the driver remains registered even if
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 * no device was claimed during registration.
377 */
Greg Kroah-Hartman725522b2007-01-15 11:50:02 -0800378int __pci_register_driver(struct pci_driver *drv, struct module *owner,
379 const char *mod_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380{
381 int error;
382
383 /* initialize common driver fields */
384 drv->driver.name = drv->name;
385 drv->driver.bus = &pci_bus_type;
Laurent riffard863b18f2005-10-27 23:12:54 +0200386 drv->driver.owner = owner;
Greg Kroah-Hartman725522b2007-01-15 11:50:02 -0800387 drv->driver.mod_name = mod_name;
Alan Cox50b00752006-08-16 17:42:18 +0100388
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700389 spin_lock_init(&drv->dynids.lock);
390 INIT_LIST_HEAD(&drv->dynids.list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
392 /* register with core */
393 error = driver_register(&drv->driver);
Akinobu Mita50bf14b2006-11-08 19:53:59 -0800394 if (error)
395 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
Akinobu Mita50bf14b2006-11-08 19:53:59 -0800397 error = pci_create_newid_file(drv);
398 if (error)
399 driver_unregister(&drv->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
401 return error;
402}
403
404/**
405 * pci_unregister_driver - unregister a pci driver
406 * @drv: the driver structure to unregister
407 *
408 * Deletes the driver structure from the list of registered PCI drivers,
409 * gives it a chance to clean up by calling its remove() function for
410 * each device it was responsible for, and marks those devices as
411 * driverless.
412 */
413
414void
415pci_unregister_driver(struct pci_driver *drv)
416{
Greg Kroah-Hartman03d43b12007-11-28 12:23:18 -0800417 pci_remove_newid_file(drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 driver_unregister(&drv->driver);
419 pci_free_dynids(drv);
420}
421
422static struct pci_driver pci_compat_driver = {
423 .name = "compat"
424};
425
426/**
427 * pci_dev_driver - get the pci_driver of a device
428 * @dev: the device to query
429 *
430 * Returns the appropriate pci_driver structure or %NULL if there is no
431 * registered driver for the device.
432 */
433struct pci_driver *
434pci_dev_driver(const struct pci_dev *dev)
435{
436 if (dev->driver)
437 return dev->driver;
438 else {
439 int i;
440 for(i=0; i<=PCI_ROM_RESOURCE; i++)
441 if (dev->resource[i].flags & IORESOURCE_BUSY)
442 return &pci_compat_driver;
443 }
444 return NULL;
445}
446
447/**
448 * pci_bus_match - Tell if a PCI device structure has a matching PCI device id structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 * @dev: the PCI device structure to match against
Randy Dunlap8f7020d2005-10-23 11:57:38 -0700450 * @drv: the device driver to search for matching PCI device id structures
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 *
452 * Used by a driver to check whether a PCI device present in the
Randy Dunlap8f7020d2005-10-23 11:57:38 -0700453 * system is in its list of supported devices. Returns the matching
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 * pci_device_id structure or %NULL if there is no match.
455 */
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700456static int pci_bus_match(struct device *dev, struct device_driver *drv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457{
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700458 struct pci_dev *pci_dev = to_pci_dev(dev);
459 struct pci_driver *pci_drv = to_pci_driver(drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 const struct pci_device_id *found_id;
461
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700462 found_id = pci_match_device(pci_drv, pci_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 if (found_id)
464 return 1;
465
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700466 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467}
468
469/**
470 * pci_dev_get - increments the reference count of the pci device structure
471 * @dev: the device being referenced
472 *
473 * Each live reference to a device should be refcounted.
474 *
475 * Drivers for PCI devices should normally record such references in
476 * their probe() methods, when they bind to a device, and release
477 * them by calling pci_dev_put(), in their disconnect() methods.
478 *
479 * A pointer to the device with the incremented reference counter is returned.
480 */
481struct pci_dev *pci_dev_get(struct pci_dev *dev)
482{
483 if (dev)
484 get_device(&dev->dev);
485 return dev;
486}
487
488/**
489 * pci_dev_put - release a use of the pci device structure
490 * @dev: device that's been disconnected
491 *
492 * Must be called when a user of a device is finished with it. When the last
493 * user of the device calls this function, the memory of the device is freed.
494 */
495void pci_dev_put(struct pci_dev *dev)
496{
497 if (dev)
498 put_device(&dev->dev);
499}
500
501#ifndef CONFIG_HOTPLUG
Kay Sievers7eff2e72007-08-14 15:15:12 +0200502int pci_uevent(struct device *dev, struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503{
504 return -ENODEV;
505}
506#endif
507
508struct bus_type pci_bus_type = {
509 .name = "pci",
510 .match = pci_bus_match,
Kay Sievers312c0042005-11-16 09:00:00 +0100511 .uevent = pci_uevent,
Russell Kingb15d6862006-01-05 14:30:22 +0000512 .probe = pci_device_probe,
513 .remove = pci_device_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 .suspend = pci_device_suspend,
Linus Torvaldscbd69db2006-06-24 14:50:29 -0700515 .suspend_late = pci_device_suspend_late,
516 .resume_early = pci_device_resume_early,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 .resume = pci_device_resume,
Linus Torvaldscbd69db2006-06-24 14:50:29 -0700518 .shutdown = pci_device_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 .dev_attrs = pci_dev_attrs,
520};
521
522static int __init pci_driver_init(void)
523{
524 return bus_register(&pci_bus_type);
525}
526
527postcore_initcall(pci_driver_init);
528
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700529EXPORT_SYMBOL(pci_match_id);
Laurent riffard863b18f2005-10-27 23:12:54 +0200530EXPORT_SYMBOL(__pci_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531EXPORT_SYMBOL(pci_unregister_driver);
532EXPORT_SYMBOL(pci_dev_driver);
533EXPORT_SYMBOL(pci_bus_type);
534EXPORT_SYMBOL(pci_dev_get);
535EXPORT_SYMBOL(pci_dev_put);