blob: 4940a53c56a305f347495777143e2b53e341a054 [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;
Milton Milleredbc25c2008-07-10 16:29:37 -050068 dynid->id.driver_data = driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70 spin_lock(&pdrv->dynids.lock);
Michael Ellermana56bc692007-09-14 15:33:13 +100071 list_add_tail(&dynid->node, &pdrv->dynids.list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 spin_unlock(&pdrv->dynids.lock);
73
Greg Kroah-Hartman75865852005-06-30 02:18:12 -070074 if (get_driver(&pdrv->driver)) {
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -070075 retval = driver_attach(&pdrv->driver);
Greg Kroah-Hartman75865852005-06-30 02:18:12 -070076 put_driver(&pdrv->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 }
78
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -070079 if (retval)
80 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 return count;
82}
Linus Torvalds1da177e2005-04-16 15:20:36 -070083static DRIVER_ATTR(new_id, S_IWUSR, NULL, store_new_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
85static void
86pci_free_dynids(struct pci_driver *drv)
87{
Greg Kroah-Hartman75865852005-06-30 02:18:12 -070088 struct pci_dynid *dynid, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
90 spin_lock(&drv->dynids.lock);
Greg Kroah-Hartman75865852005-06-30 02:18:12 -070091 list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 list_del(&dynid->node);
93 kfree(dynid);
94 }
95 spin_unlock(&drv->dynids.lock);
96}
97
98static int
99pci_create_newid_file(struct pci_driver *drv)
100{
101 int error = 0;
102 if (drv->probe != NULL)
Greg Kroah-Hartman03d43b12007-11-28 12:23:18 -0800103 error = driver_create_file(&drv->driver, &driver_attr_new_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 return error;
105}
106
Greg Kroah-Hartman03d43b12007-11-28 12:23:18 -0800107static void pci_remove_newid_file(struct pci_driver *drv)
108{
109 driver_remove_file(&drv->driver, &driver_attr_new_id);
110}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111#else /* !CONFIG_HOTPLUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112static inline void pci_free_dynids(struct pci_driver *drv) {}
113static inline int pci_create_newid_file(struct pci_driver *drv)
114{
115 return 0;
116}
Greg Kroah-Hartman03d43b12007-11-28 12:23:18 -0800117static inline void pci_remove_newid_file(struct pci_driver *drv) {}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118#endif
119
120/**
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700121 * pci_match_id - See if a pci device matches a given pci_id table
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 * @ids: array of PCI device id structures to search in
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700123 * @dev: the PCI device structure to match against.
124 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 * Used by a driver to check whether a PCI device present in the
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700126 * system is in its list of supported devices. Returns the matching
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 * pci_device_id structure or %NULL if there is no match.
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700128 *
Randy Dunlap8b607562007-05-09 07:19:14 +0200129 * Deprecated, don't use this as it will not catch any dynamic ids
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700130 * that a driver might want to check for.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 */
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700132const struct pci_device_id *pci_match_id(const struct pci_device_id *ids,
133 struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134{
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700135 if (ids) {
136 while (ids->vendor || ids->subvendor || ids->class_mask) {
137 if (pci_match_one_device(ids, dev))
138 return ids;
139 ids++;
140 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 }
142 return NULL;
143}
144
145/**
Randy Dunlapae9608a2007-01-09 21:41:01 -0800146 * pci_match_device - Tell if a PCI device structure has a matching PCI device id structure
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700147 * @drv: the PCI driver to match against
Henrik Kretzschmar39ba4872006-08-15 10:57:16 +0200148 * @dev: the PCI device structure to match against
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700149 *
150 * Used by a driver to check whether a PCI device present in the
151 * system is in its list of supported devices. Returns the matching
152 * pci_device_id structure or %NULL if there is no match.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 */
Adrian Bunkd73460d2007-10-24 18:27:18 +0200154static const struct pci_device_id *pci_match_device(struct pci_driver *drv,
155 struct pci_dev *dev)
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700156{
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700157 struct pci_dynid *dynid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Russell King7461b602006-11-29 21:18:04 +0000159 /* Look at the dynamic ids first, before the static ones */
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700160 spin_lock(&drv->dynids.lock);
161 list_for_each_entry(dynid, &drv->dynids.list, node) {
162 if (pci_match_one_device(&dynid->id, dev)) {
163 spin_unlock(&drv->dynids.lock);
164 return &dynid->id;
165 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 }
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700167 spin_unlock(&drv->dynids.lock);
Russell King7461b602006-11-29 21:18:04 +0000168
169 return pci_match_id(drv->id_table, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170}
171
Andi Kleend42c6992005-07-06 19:56:03 +0200172static int pci_call_probe(struct pci_driver *drv, struct pci_dev *dev,
173 const struct pci_device_id *id)
174{
175 int error;
176#ifdef CONFIG_NUMA
177 /* Execute driver initialization on node where the
178 device's bus is attached to. This way the driver likely
179 allocates its local memory on the right node without
180 any need to change it. */
181 struct mempolicy *oldpol;
182 cpumask_t oldmask = current->cpus_allowed;
Yinghai Lu4efeb4d2008-05-12 21:21:05 +0200183 int node = dev_to_node(&dev->dev);
Mike Travisf70316d2008-04-04 18:11:06 -0700184
185 if (node >= 0) {
186 node_to_cpumask_ptr(nodecpumask, node);
187 set_cpus_allowed_ptr(current, nodecpumask);
188 }
Andi Kleend42c6992005-07-06 19:56:03 +0200189 /* And set default memory allocation policy */
190 oldpol = current->mempolicy;
Lee Schermerhorn74e27e42007-11-21 15:07:05 -0800191 current->mempolicy = NULL; /* fall back to system default policy */
Andi Kleend42c6992005-07-06 19:56:03 +0200192#endif
193 error = drv->probe(dev, id);
194#ifdef CONFIG_NUMA
Mike Travisf70316d2008-04-04 18:11:06 -0700195 set_cpus_allowed_ptr(current, &oldmask);
Andi Kleend42c6992005-07-06 19:56:03 +0200196 current->mempolicy = oldpol;
197#endif
198 return error;
199}
200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201/**
202 * __pci_device_probe()
Randy Dunlap8f7020d2005-10-23 11:57:38 -0700203 * @drv: driver to call to check if it wants the PCI device
204 * @pci_dev: PCI device being probed
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 *
Randy Dunlap8f7020d2005-10-23 11:57:38 -0700206 * returns 0 on success, else error.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 * side-effect: pci_dev->driver is set to drv when drv claims pci_dev.
208 */
209static int
210__pci_device_probe(struct pci_driver *drv, struct pci_dev *pci_dev)
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700211{
212 const struct pci_device_id *id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 int error = 0;
214
215 if (!pci_dev->driver && drv->probe) {
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700216 error = -ENODEV;
217
218 id = pci_match_device(drv, pci_dev);
219 if (id)
Andi Kleend42c6992005-07-06 19:56:03 +0200220 error = pci_call_probe(drv, pci_dev, id);
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700221 if (error >= 0) {
222 pci_dev->driver = drv;
223 error = 0;
224 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 }
226 return error;
227}
228
229static int pci_device_probe(struct device * dev)
230{
231 int error = 0;
232 struct pci_driver *drv;
233 struct pci_dev *pci_dev;
234
235 drv = to_pci_driver(dev->driver);
236 pci_dev = to_pci_dev(dev);
237 pci_dev_get(pci_dev);
238 error = __pci_device_probe(drv, pci_dev);
239 if (error)
240 pci_dev_put(pci_dev);
241
242 return error;
243}
244
245static int pci_device_remove(struct device * dev)
246{
247 struct pci_dev * pci_dev = to_pci_dev(dev);
248 struct pci_driver * drv = pci_dev->driver;
249
250 if (drv) {
251 if (drv->remove)
252 drv->remove(pci_dev);
253 pci_dev->driver = NULL;
254 }
255
256 /*
Shaohua Li2449e062006-10-20 14:45:32 -0700257 * If the device is still on, set the power state as "unknown",
258 * since it might change by the next time we load the driver.
259 */
260 if (pci_dev->current_state == PCI_D0)
261 pci_dev->current_state = PCI_UNKNOWN;
262
263 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 * We would love to complain here if pci_dev->is_enabled is set, that
265 * the driver should have called pci_disable_device(), but the
266 * unfortunate fact is there are too many odd BIOS and bridge setups
267 * that don't like drivers doing that all of the time.
268 * Oh well, we can dream of sane hardware when we sleep, no matter how
269 * horrible the crap we have to deal with is when we are awake...
270 */
271
272 pci_dev_put(pci_dev);
273 return 0;
274}
275
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200276static void pci_device_shutdown(struct device *dev)
277{
278 struct pci_dev *pci_dev = to_pci_dev(dev);
279 struct pci_driver *drv = pci_dev->driver;
280
281 if (drv && drv->shutdown)
282 drv->shutdown(pci_dev);
283 pci_msi_shutdown(pci_dev);
284 pci_msix_shutdown(pci_dev);
285}
286
287#ifdef CONFIG_PM_SLEEP
288
289/*
290 * Default "suspend" method for devices that have no driver provided suspend,
291 * or not even a driver at all.
292 */
293static void pci_default_pm_suspend(struct pci_dev *pci_dev)
294{
295 pci_save_state(pci_dev);
296 /*
297 * mark its power state as "unknown", since we don't know if
298 * e.g. the BIOS will change its device state when we suspend.
299 */
300 if (pci_dev->current_state == PCI_D0)
301 pci_dev->current_state = PCI_UNKNOWN;
302}
303
304/*
305 * Default "resume" method for devices that have no driver provided resume,
306 * or not even a driver at all.
307 */
308static int pci_default_pm_resume(struct pci_dev *pci_dev)
309{
310 int retval = 0;
311
312 /* restore the PCI config space */
313 pci_restore_state(pci_dev);
314 /* if the device was enabled before suspend, reenable */
315 retval = pci_reenable_device(pci_dev);
316 /*
317 * if the device was busmaster before the suspend, make it busmaster
318 * again
319 */
320 if (pci_dev->is_busmaster)
321 pci_set_master(pci_dev);
322
323 return retval;
324}
325
326static int pci_legacy_suspend(struct device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327{
328 struct pci_dev * pci_dev = to_pci_dev(dev);
329 struct pci_driver * drv = pci_dev->driver;
330 int i = 0;
331
Andrew Morton02669492006-03-23 01:38:34 -0800332 if (drv && drv->suspend) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 i = drv->suspend(pci_dev, state);
Andrew Morton02669492006-03-23 01:38:34 -0800334 suspend_report_result(drv->suspend, i);
335 } else {
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200336 pci_default_pm_suspend(pci_dev);
Andrew Morton02669492006-03-23 01:38:34 -0800337 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 return i;
339}
340
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200341static int pci_legacy_suspend_late(struct device *dev, pm_message_t state)
Linus Torvaldscbd69db2006-06-24 14:50:29 -0700342{
343 struct pci_dev * pci_dev = to_pci_dev(dev);
344 struct pci_driver * drv = pci_dev->driver;
345 int i = 0;
346
347 if (drv && drv->suspend_late) {
348 i = drv->suspend_late(pci_dev, state);
349 suspend_report_result(drv->suspend_late, i);
350 }
351 return i;
352}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200354static int pci_legacy_resume(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355{
Jean Delvare8d92bc22006-04-18 14:49:56 +0200356 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 struct pci_dev * pci_dev = to_pci_dev(dev);
358 struct pci_driver * drv = pci_dev->driver;
359
360 if (drv && drv->resume)
Jean Delvare8d92bc22006-04-18 14:49:56 +0200361 error = drv->resume(pci_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 else
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200363 error = pci_default_pm_resume(pci_dev);
Jean Delvare8d92bc22006-04-18 14:49:56 +0200364 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365}
366
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200367static int pci_legacy_resume_early(struct device *dev)
Linus Torvaldscbd69db2006-06-24 14:50:29 -0700368{
369 int error = 0;
370 struct pci_dev * pci_dev = to_pci_dev(dev);
371 struct pci_driver * drv = pci_dev->driver;
372
373 if (drv && drv->resume_early)
374 error = drv->resume_early(pci_dev);
375 return error;
376}
377
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200378static int pci_pm_prepare(struct device *dev)
379{
380 struct device_driver *drv = dev->driver;
381 int error = 0;
382
383 if (drv && drv->pm && drv->pm->prepare)
384 error = drv->pm->prepare(dev);
385
386 return error;
387}
388
389static void pci_pm_complete(struct device *dev)
390{
391 struct device_driver *drv = dev->driver;
392
393 if (drv && drv->pm && drv->pm->complete)
394 drv->pm->complete(dev);
395}
396
397#ifdef CONFIG_SUSPEND
398
399static int pci_pm_suspend(struct device *dev)
400{
401 struct pci_dev *pci_dev = to_pci_dev(dev);
402 struct device_driver *drv = dev->driver;
403 int error = 0;
404
405 if (drv && drv->pm) {
406 if (drv->pm->suspend) {
407 error = drv->pm->suspend(dev);
408 suspend_report_result(drv->pm->suspend, error);
409 } else {
410 pci_default_pm_suspend(pci_dev);
411 }
412 } else {
413 error = pci_legacy_suspend(dev, PMSG_SUSPEND);
414 }
415 pci_fixup_device(pci_fixup_suspend, pci_dev);
416
417 return error;
418}
419
420static int pci_pm_suspend_noirq(struct device *dev)
Greg KHc8958172005-04-08 14:53:31 +0900421{
422 struct pci_dev *pci_dev = to_pci_dev(dev);
423 struct pci_driver *drv = pci_dev->driver;
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200424 int error = 0;
Greg KHc8958172005-04-08 14:53:31 +0900425
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200426 if (drv && drv->pm) {
427 if (drv->pm->suspend_noirq) {
428 error = drv->pm->suspend_noirq(dev);
429 suspend_report_result(drv->pm->suspend_noirq, error);
430 }
431 } else {
432 error = pci_legacy_suspend_late(dev, PMSG_SUSPEND);
433 }
434
435 return error;
Greg KHc8958172005-04-08 14:53:31 +0900436}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200438static int pci_pm_resume(struct device *dev)
439{
440 struct pci_dev *pci_dev = to_pci_dev(dev);
441 struct device_driver *drv = dev->driver;
442 int error;
443
444 pci_fixup_device(pci_fixup_resume, pci_dev);
445
446 if (drv && drv->pm) {
447 error = drv->pm->resume ? drv->pm->resume(dev) :
448 pci_default_pm_resume(pci_dev);
449 } else {
450 error = pci_legacy_resume(dev);
451 }
452
453 return error;
454}
455
456static int pci_pm_resume_noirq(struct device *dev)
457{
458 struct pci_dev *pci_dev = to_pci_dev(dev);
459 struct pci_driver *drv = pci_dev->driver;
460 int error = 0;
461
462 pci_fixup_device(pci_fixup_resume_early, pci_dev);
463
464 if (drv && drv->pm) {
465 if (drv->pm->resume_noirq)
466 error = drv->pm->resume_noirq(dev);
467 } else {
468 error = pci_legacy_resume_early(dev);
469 }
470
471 return error;
472}
473
474#else /* !CONFIG_SUSPEND */
475
476#define pci_pm_suspend NULL
477#define pci_pm_suspend_noirq NULL
478#define pci_pm_resume NULL
479#define pci_pm_resume_noirq NULL
480
481#endif /* !CONFIG_SUSPEND */
482
483#ifdef CONFIG_HIBERNATION
484
485static int pci_pm_freeze(struct device *dev)
486{
487 struct pci_dev *pci_dev = to_pci_dev(dev);
488 struct device_driver *drv = dev->driver;
489 int error = 0;
490
491 if (drv && drv->pm) {
492 if (drv->pm->freeze) {
493 error = drv->pm->freeze(dev);
494 suspend_report_result(drv->pm->freeze, error);
495 } else {
496 pci_default_pm_suspend(pci_dev);
497 }
498 } else {
499 error = pci_legacy_suspend(dev, PMSG_FREEZE);
500 pci_fixup_device(pci_fixup_suspend, pci_dev);
501 }
502
503 return error;
504}
505
506static int pci_pm_freeze_noirq(struct device *dev)
507{
508 struct pci_dev *pci_dev = to_pci_dev(dev);
509 struct pci_driver *drv = pci_dev->driver;
510 int error = 0;
511
512 if (drv && drv->pm) {
513 if (drv->pm->freeze_noirq) {
514 error = drv->pm->freeze_noirq(dev);
515 suspend_report_result(drv->pm->freeze_noirq, error);
516 }
517 } else {
518 error = pci_legacy_suspend_late(dev, PMSG_FREEZE);
519 }
520
521 return error;
522}
523
524static int pci_pm_thaw(struct device *dev)
525{
526 struct device_driver *drv = dev->driver;
527 int error = 0;
528
529 if (drv && drv->pm) {
530 if (drv->pm->thaw)
531 error = drv->pm->thaw(dev);
532 } else {
533 pci_fixup_device(pci_fixup_resume, to_pci_dev(dev));
534 error = pci_legacy_resume(dev);
535 }
536
537 return error;
538}
539
540static int pci_pm_thaw_noirq(struct device *dev)
541{
542 struct pci_dev *pci_dev = to_pci_dev(dev);
543 struct pci_driver *drv = pci_dev->driver;
544 int error = 0;
545
546 if (drv && drv->pm) {
547 if (drv->pm->thaw_noirq)
548 error = drv->pm->thaw_noirq(dev);
549 } else {
550 pci_fixup_device(pci_fixup_resume_early, pci_dev);
551 error = pci_legacy_resume_early(dev);
552 }
553
554 return error;
555}
556
557static int pci_pm_poweroff(struct device *dev)
558{
559 struct device_driver *drv = dev->driver;
560 int error = 0;
561
562 pci_fixup_device(pci_fixup_suspend, to_pci_dev(dev));
563
564 if (drv && drv->pm) {
565 if (drv->pm->poweroff) {
566 error = drv->pm->poweroff(dev);
567 suspend_report_result(drv->pm->poweroff, error);
568 }
569 } else {
570 error = pci_legacy_suspend(dev, PMSG_HIBERNATE);
571 }
572
573 return error;
574}
575
576static int pci_pm_poweroff_noirq(struct device *dev)
577{
578 struct pci_dev *pci_dev = to_pci_dev(dev);
579 struct pci_driver *drv = pci_dev->driver;
580 int error = 0;
581
582 if (drv && drv->pm) {
583 if (drv->pm->poweroff_noirq) {
584 error = drv->pm->poweroff_noirq(dev);
585 suspend_report_result(drv->pm->poweroff_noirq, error);
586 }
587 } else {
588 error = pci_legacy_suspend_late(dev, PMSG_HIBERNATE);
589 }
590
591 return error;
592}
593
594static int pci_pm_restore(struct device *dev)
595{
596 struct pci_dev *pci_dev = to_pci_dev(dev);
597 struct device_driver *drv = dev->driver;
598 int error;
599
600 if (drv && drv->pm) {
601 error = drv->pm->restore ? drv->pm->restore(dev) :
602 pci_default_pm_resume(pci_dev);
603 } else {
604 error = pci_legacy_resume(dev);
605 }
606 pci_fixup_device(pci_fixup_resume, pci_dev);
607
608 return error;
609}
610
611static int pci_pm_restore_noirq(struct device *dev)
612{
613 struct pci_dev *pci_dev = to_pci_dev(dev);
614 struct pci_driver *drv = pci_dev->driver;
615 int error = 0;
616
617 pci_fixup_device(pci_fixup_resume, pci_dev);
618
619 if (drv && drv->pm) {
620 if (drv->pm->restore_noirq)
621 error = drv->pm->restore_noirq(dev);
622 } else {
623 error = pci_legacy_resume_early(dev);
624 }
625 pci_fixup_device(pci_fixup_resume_early, pci_dev);
626
627 return error;
628}
629
630#else /* !CONFIG_HIBERNATION */
631
632#define pci_pm_freeze NULL
633#define pci_pm_freeze_noirq NULL
634#define pci_pm_thaw NULL
635#define pci_pm_thaw_noirq NULL
636#define pci_pm_poweroff NULL
637#define pci_pm_poweroff_noirq NULL
638#define pci_pm_restore NULL
639#define pci_pm_restore_noirq NULL
640
641#endif /* !CONFIG_HIBERNATION */
642
643struct pm_ext_ops pci_pm_ops = {
644 .base = {
645 .prepare = pci_pm_prepare,
646 .complete = pci_pm_complete,
647 .suspend = pci_pm_suspend,
648 .resume = pci_pm_resume,
649 .freeze = pci_pm_freeze,
650 .thaw = pci_pm_thaw,
651 .poweroff = pci_pm_poweroff,
652 .restore = pci_pm_restore,
653 },
654 .suspend_noirq = pci_pm_suspend_noirq,
655 .resume_noirq = pci_pm_resume_noirq,
656 .freeze_noirq = pci_pm_freeze_noirq,
657 .thaw_noirq = pci_pm_thaw_noirq,
658 .poweroff_noirq = pci_pm_poweroff_noirq,
659 .restore_noirq = pci_pm_restore_noirq,
660};
661
662#define PCI_PM_OPS_PTR &pci_pm_ops
663
664#else /* !CONFIG_PM_SLEEP */
665
666#define PCI_PM_OPS_PTR NULL
667
668#endif /* !CONFIG_PM_SLEEP */
669
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670/**
Laurent riffard863b18f2005-10-27 23:12:54 +0200671 * __pci_register_driver - register a new pci driver
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 * @drv: the driver structure to register
Laurent riffard863b18f2005-10-27 23:12:54 +0200673 * @owner: owner module of drv
Randy Dunlapf95d8822007-02-10 14:41:56 -0800674 * @mod_name: module name string
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 *
676 * Adds the driver structure to the list of registered drivers.
677 * Returns a negative value on error, otherwise 0.
Steven Coleeaae4b32005-05-03 18:38:30 -0600678 * If no error occurred, the driver remains registered even if
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 * no device was claimed during registration.
680 */
Greg Kroah-Hartman725522b2007-01-15 11:50:02 -0800681int __pci_register_driver(struct pci_driver *drv, struct module *owner,
682 const char *mod_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683{
684 int error;
685
686 /* initialize common driver fields */
687 drv->driver.name = drv->name;
688 drv->driver.bus = &pci_bus_type;
Laurent riffard863b18f2005-10-27 23:12:54 +0200689 drv->driver.owner = owner;
Greg Kroah-Hartman725522b2007-01-15 11:50:02 -0800690 drv->driver.mod_name = mod_name;
Alan Cox50b00752006-08-16 17:42:18 +0100691
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200692 if (drv->pm)
693 drv->driver.pm = &drv->pm->base;
694
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700695 spin_lock_init(&drv->dynids.lock);
696 INIT_LIST_HEAD(&drv->dynids.list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
698 /* register with core */
699 error = driver_register(&drv->driver);
Akinobu Mita50bf14b2006-11-08 19:53:59 -0800700 if (error)
701 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Akinobu Mita50bf14b2006-11-08 19:53:59 -0800703 error = pci_create_newid_file(drv);
704 if (error)
705 driver_unregister(&drv->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
707 return error;
708}
709
710/**
711 * pci_unregister_driver - unregister a pci driver
712 * @drv: the driver structure to unregister
713 *
714 * Deletes the driver structure from the list of registered PCI drivers,
715 * gives it a chance to clean up by calling its remove() function for
716 * each device it was responsible for, and marks those devices as
717 * driverless.
718 */
719
720void
721pci_unregister_driver(struct pci_driver *drv)
722{
Greg Kroah-Hartman03d43b12007-11-28 12:23:18 -0800723 pci_remove_newid_file(drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 driver_unregister(&drv->driver);
725 pci_free_dynids(drv);
726}
727
728static struct pci_driver pci_compat_driver = {
729 .name = "compat"
730};
731
732/**
733 * pci_dev_driver - get the pci_driver of a device
734 * @dev: the device to query
735 *
736 * Returns the appropriate pci_driver structure or %NULL if there is no
737 * registered driver for the device.
738 */
739struct pci_driver *
740pci_dev_driver(const struct pci_dev *dev)
741{
742 if (dev->driver)
743 return dev->driver;
744 else {
745 int i;
746 for(i=0; i<=PCI_ROM_RESOURCE; i++)
747 if (dev->resource[i].flags & IORESOURCE_BUSY)
748 return &pci_compat_driver;
749 }
750 return NULL;
751}
752
753/**
754 * pci_bus_match - Tell if a PCI device structure has a matching PCI device id structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 * @dev: the PCI device structure to match against
Randy Dunlap8f7020d2005-10-23 11:57:38 -0700756 * @drv: the device driver to search for matching PCI device id structures
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 *
758 * Used by a driver to check whether a PCI device present in the
Randy Dunlap8f7020d2005-10-23 11:57:38 -0700759 * system is in its list of supported devices. Returns the matching
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 * pci_device_id structure or %NULL if there is no match.
761 */
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700762static int pci_bus_match(struct device *dev, struct device_driver *drv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763{
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700764 struct pci_dev *pci_dev = to_pci_dev(dev);
765 struct pci_driver *pci_drv = to_pci_driver(drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 const struct pci_device_id *found_id;
767
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700768 found_id = pci_match_device(pci_drv, pci_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 if (found_id)
770 return 1;
771
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700772 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773}
774
775/**
776 * pci_dev_get - increments the reference count of the pci device structure
777 * @dev: the device being referenced
778 *
779 * Each live reference to a device should be refcounted.
780 *
781 * Drivers for PCI devices should normally record such references in
782 * their probe() methods, when they bind to a device, and release
783 * them by calling pci_dev_put(), in their disconnect() methods.
784 *
785 * A pointer to the device with the incremented reference counter is returned.
786 */
787struct pci_dev *pci_dev_get(struct pci_dev *dev)
788{
789 if (dev)
790 get_device(&dev->dev);
791 return dev;
792}
793
794/**
795 * pci_dev_put - release a use of the pci device structure
796 * @dev: device that's been disconnected
797 *
798 * Must be called when a user of a device is finished with it. When the last
799 * user of the device calls this function, the memory of the device is freed.
800 */
801void pci_dev_put(struct pci_dev *dev)
802{
803 if (dev)
804 put_device(&dev->dev);
805}
806
807#ifndef CONFIG_HOTPLUG
Kay Sievers7eff2e72007-08-14 15:15:12 +0200808int pci_uevent(struct device *dev, struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809{
810 return -ENODEV;
811}
812#endif
813
814struct bus_type pci_bus_type = {
815 .name = "pci",
816 .match = pci_bus_match,
Kay Sievers312c0042005-11-16 09:00:00 +0100817 .uevent = pci_uevent,
Russell Kingb15d6862006-01-05 14:30:22 +0000818 .probe = pci_device_probe,
819 .remove = pci_device_remove,
Linus Torvaldscbd69db2006-06-24 14:50:29 -0700820 .shutdown = pci_device_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 .dev_attrs = pci_dev_attrs,
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200822 .pm = PCI_PM_OPS_PTR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823};
824
825static int __init pci_driver_init(void)
826{
827 return bus_register(&pci_bus_type);
828}
829
830postcore_initcall(pci_driver_init);
831
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700832EXPORT_SYMBOL(pci_match_id);
Laurent riffard863b18f2005-10-27 23:12:54 +0200833EXPORT_SYMBOL(__pci_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834EXPORT_SYMBOL(pci_unregister_driver);
835EXPORT_SYMBOL(pci_dev_driver);
836EXPORT_SYMBOL(pci_bus_type);
837EXPORT_SYMBOL(pci_dev_get);
838EXPORT_SYMBOL(pci_dev_put);