blob: 888191a3b0d164875d35ffaa6739dc9d4a7524a7 [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 Delvareb41d6cf2008-08-17 21:06:59 +020046 const struct pci_device_id *ids = pdrv->id_table;
Jean Delvare6ba18632007-04-07 17:21:28 +020047 __u32 vendor, device, subvendor=PCI_ANY_ID,
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 subdevice=PCI_ANY_ID, class=0, class_mask=0;
49 unsigned long driver_data=0;
50 int fields=0;
Chris Wright2debb4d2008-11-25 19:36:10 -080051 int retval=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Jean Delvareb41d6cf2008-08-17 21:06:59 +020053 fields = sscanf(buf, "%x %x %x %x %x %x %lx",
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 &vendor, &device, &subvendor, &subdevice,
55 &class, &class_mask, &driver_data);
Jean Delvare6ba18632007-04-07 17:21:28 +020056 if (fields < 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 return -EINVAL;
58
Jean Delvareb41d6cf2008-08-17 21:06:59 +020059 /* Only accept driver_data values that match an existing id_table
60 entry */
Chris Wright2debb4d2008-11-25 19:36:10 -080061 if (ids) {
62 retval = -EINVAL;
63 while (ids->vendor || ids->subvendor || ids->class_mask) {
64 if (driver_data == ids->driver_data) {
65 retval = 0;
66 break;
67 }
68 ids++;
Jean Delvareb41d6cf2008-08-17 21:06:59 +020069 }
Chris Wright2debb4d2008-11-25 19:36:10 -080070 if (retval) /* No match */
71 return retval;
Jean Delvareb41d6cf2008-08-17 21:06:59 +020072 }
Jean Delvareb41d6cf2008-08-17 21:06:59 +020073
Eric Sesterhennf5afe802006-02-28 15:34:49 +010074 dynid = kzalloc(sizeof(*dynid), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 if (!dynid)
76 return -ENOMEM;
77
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 dynid->id.vendor = vendor;
79 dynid->id.device = device;
80 dynid->id.subvendor = subvendor;
81 dynid->id.subdevice = subdevice;
82 dynid->id.class = class;
83 dynid->id.class_mask = class_mask;
Milton Milleredbc25c2008-07-10 16:29:37 -050084 dynid->id.driver_data = driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86 spin_lock(&pdrv->dynids.lock);
Michael Ellermana56bc692007-09-14 15:33:13 +100087 list_add_tail(&dynid->node, &pdrv->dynids.list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 spin_unlock(&pdrv->dynids.lock);
89
Greg Kroah-Hartman75865852005-06-30 02:18:12 -070090 if (get_driver(&pdrv->driver)) {
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -070091 retval = driver_attach(&pdrv->driver);
Greg Kroah-Hartman75865852005-06-30 02:18:12 -070092 put_driver(&pdrv->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 }
94
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -070095 if (retval)
96 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 return count;
98}
Linus Torvalds1da177e2005-04-16 15:20:36 -070099static DRIVER_ATTR(new_id, S_IWUSR, NULL, store_new_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
101static void
102pci_free_dynids(struct pci_driver *drv)
103{
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700104 struct pci_dynid *dynid, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
106 spin_lock(&drv->dynids.lock);
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700107 list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 list_del(&dynid->node);
109 kfree(dynid);
110 }
111 spin_unlock(&drv->dynids.lock);
112}
113
114static int
115pci_create_newid_file(struct pci_driver *drv)
116{
117 int error = 0;
118 if (drv->probe != NULL)
Greg Kroah-Hartman03d43b12007-11-28 12:23:18 -0800119 error = driver_create_file(&drv->driver, &driver_attr_new_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 return error;
121}
122
Greg Kroah-Hartman03d43b12007-11-28 12:23:18 -0800123static void pci_remove_newid_file(struct pci_driver *drv)
124{
125 driver_remove_file(&drv->driver, &driver_attr_new_id);
126}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127#else /* !CONFIG_HOTPLUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128static inline void pci_free_dynids(struct pci_driver *drv) {}
129static inline int pci_create_newid_file(struct pci_driver *drv)
130{
131 return 0;
132}
Greg Kroah-Hartman03d43b12007-11-28 12:23:18 -0800133static inline void pci_remove_newid_file(struct pci_driver *drv) {}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134#endif
135
136/**
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700137 * pci_match_id - See if a pci device matches a given pci_id table
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 * @ids: array of PCI device id structures to search in
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700139 * @dev: the PCI device structure to match against.
140 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 * Used by a driver to check whether a PCI device present in the
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700142 * system is in its list of supported devices. Returns the matching
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 * pci_device_id structure or %NULL if there is no match.
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700144 *
Randy Dunlap8b607562007-05-09 07:19:14 +0200145 * Deprecated, don't use this as it will not catch any dynamic ids
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700146 * that a driver might want to check for.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 */
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700148const struct pci_device_id *pci_match_id(const struct pci_device_id *ids,
149 struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150{
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700151 if (ids) {
152 while (ids->vendor || ids->subvendor || ids->class_mask) {
153 if (pci_match_one_device(ids, dev))
154 return ids;
155 ids++;
156 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 }
158 return NULL;
159}
160
161/**
Randy Dunlapae9608a2007-01-09 21:41:01 -0800162 * pci_match_device - Tell if a PCI device structure has a matching PCI device id structure
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700163 * @drv: the PCI driver to match against
Henrik Kretzschmar39ba4872006-08-15 10:57:16 +0200164 * @dev: the PCI device structure to match against
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700165 *
166 * Used by a driver to check whether a PCI device present in the
167 * system is in its list of supported devices. Returns the matching
168 * pci_device_id structure or %NULL if there is no match.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 */
Adrian Bunkd73460d2007-10-24 18:27:18 +0200170static const struct pci_device_id *pci_match_device(struct pci_driver *drv,
171 struct pci_dev *dev)
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700172{
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700173 struct pci_dynid *dynid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
Russell King7461b602006-11-29 21:18:04 +0000175 /* Look at the dynamic ids first, before the static ones */
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700176 spin_lock(&drv->dynids.lock);
177 list_for_each_entry(dynid, &drv->dynids.list, node) {
178 if (pci_match_one_device(&dynid->id, dev)) {
179 spin_unlock(&drv->dynids.lock);
180 return &dynid->id;
181 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 }
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700183 spin_unlock(&drv->dynids.lock);
Russell King7461b602006-11-29 21:18:04 +0000184
185 return pci_match_id(drv->id_table, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186}
187
Andi Kleend42c6992005-07-06 19:56:03 +0200188static int pci_call_probe(struct pci_driver *drv, struct pci_dev *dev,
189 const struct pci_device_id *id)
190{
191 int error;
192#ifdef CONFIG_NUMA
193 /* Execute driver initialization on node where the
194 device's bus is attached to. This way the driver likely
195 allocates its local memory on the right node without
196 any need to change it. */
197 struct mempolicy *oldpol;
198 cpumask_t oldmask = current->cpus_allowed;
Yinghai Lu4efeb4d2008-05-12 21:21:05 +0200199 int node = dev_to_node(&dev->dev);
Mike Travisf70316d2008-04-04 18:11:06 -0700200
201 if (node >= 0) {
202 node_to_cpumask_ptr(nodecpumask, node);
203 set_cpus_allowed_ptr(current, nodecpumask);
204 }
Andi Kleend42c6992005-07-06 19:56:03 +0200205 /* And set default memory allocation policy */
206 oldpol = current->mempolicy;
Lee Schermerhorn74e27e42007-11-21 15:07:05 -0800207 current->mempolicy = NULL; /* fall back to system default policy */
Andi Kleend42c6992005-07-06 19:56:03 +0200208#endif
209 error = drv->probe(dev, id);
210#ifdef CONFIG_NUMA
Mike Travisf70316d2008-04-04 18:11:06 -0700211 set_cpus_allowed_ptr(current, &oldmask);
Andi Kleend42c6992005-07-06 19:56:03 +0200212 current->mempolicy = oldpol;
213#endif
214 return error;
215}
216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217/**
218 * __pci_device_probe()
Randy Dunlap8f7020d2005-10-23 11:57:38 -0700219 * @drv: driver to call to check if it wants the PCI device
220 * @pci_dev: PCI device being probed
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 *
Randy Dunlap8f7020d2005-10-23 11:57:38 -0700222 * returns 0 on success, else error.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 * side-effect: pci_dev->driver is set to drv when drv claims pci_dev.
224 */
225static int
226__pci_device_probe(struct pci_driver *drv, struct pci_dev *pci_dev)
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700227{
228 const struct pci_device_id *id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 int error = 0;
230
231 if (!pci_dev->driver && drv->probe) {
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700232 error = -ENODEV;
233
234 id = pci_match_device(drv, pci_dev);
235 if (id)
Andi Kleend42c6992005-07-06 19:56:03 +0200236 error = pci_call_probe(drv, pci_dev, id);
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700237 if (error >= 0) {
238 pci_dev->driver = drv;
239 error = 0;
240 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 }
242 return error;
243}
244
245static int pci_device_probe(struct device * dev)
246{
247 int error = 0;
248 struct pci_driver *drv;
249 struct pci_dev *pci_dev;
250
251 drv = to_pci_driver(dev->driver);
252 pci_dev = to_pci_dev(dev);
253 pci_dev_get(pci_dev);
254 error = __pci_device_probe(drv, pci_dev);
255 if (error)
256 pci_dev_put(pci_dev);
257
258 return error;
259}
260
261static int pci_device_remove(struct device * dev)
262{
263 struct pci_dev * pci_dev = to_pci_dev(dev);
264 struct pci_driver * drv = pci_dev->driver;
265
266 if (drv) {
267 if (drv->remove)
268 drv->remove(pci_dev);
269 pci_dev->driver = NULL;
270 }
271
272 /*
Shaohua Li2449e062006-10-20 14:45:32 -0700273 * If the device is still on, set the power state as "unknown",
274 * since it might change by the next time we load the driver.
275 */
276 if (pci_dev->current_state == PCI_D0)
277 pci_dev->current_state = PCI_UNKNOWN;
278
279 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 * We would love to complain here if pci_dev->is_enabled is set, that
281 * the driver should have called pci_disable_device(), but the
282 * unfortunate fact is there are too many odd BIOS and bridge setups
283 * that don't like drivers doing that all of the time.
284 * Oh well, we can dream of sane hardware when we sleep, no matter how
285 * horrible the crap we have to deal with is when we are awake...
286 */
287
288 pci_dev_put(pci_dev);
289 return 0;
290}
291
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200292static void pci_device_shutdown(struct device *dev)
293{
294 struct pci_dev *pci_dev = to_pci_dev(dev);
295 struct pci_driver *drv = pci_dev->driver;
296
297 if (drv && drv->shutdown)
298 drv->shutdown(pci_dev);
299 pci_msi_shutdown(pci_dev);
300 pci_msix_shutdown(pci_dev);
301}
302
303#ifdef CONFIG_PM_SLEEP
304
Rafael J. Wysocki355a72d2008-12-08 00:34:57 +0100305static bool pci_has_legacy_pm_support(struct pci_dev *pci_dev)
306{
307 struct pci_driver *drv = pci_dev->driver;
308
309 return drv && (drv->suspend || drv->suspend_late || drv->resume
310 || drv->resume_early);
311}
312
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200313/*
314 * Default "suspend" method for devices that have no driver provided suspend,
315 * or not even a driver at all.
316 */
317static void pci_default_pm_suspend(struct pci_dev *pci_dev)
318{
319 pci_save_state(pci_dev);
320 /*
321 * mark its power state as "unknown", since we don't know if
322 * e.g. the BIOS will change its device state when we suspend.
323 */
324 if (pci_dev->current_state == PCI_D0)
325 pci_dev->current_state = PCI_UNKNOWN;
326}
327
328/*
329 * Default "resume" method for devices that have no driver provided resume,
Rafael J. Wysocki355a72d2008-12-08 00:34:57 +0100330 * or not even a driver at all (first part).
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200331 */
Rafael J. Wysocki355a72d2008-12-08 00:34:57 +0100332static void pci_default_pm_resume_early(struct pci_dev *pci_dev)
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200333{
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200334 /* restore the PCI config space */
335 pci_restore_state(pci_dev);
Rafael J. Wysocki355a72d2008-12-08 00:34:57 +0100336}
337
338/*
339 * Default "resume" method for devices that have no driver provided resume,
340 * or not even a driver at all (second part).
341 */
342static int pci_default_pm_resume_late(struct pci_dev *pci_dev)
343{
344 int retval;
345
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200346 /* if the device was enabled before suspend, reenable */
347 retval = pci_reenable_device(pci_dev);
348 /*
349 * if the device was busmaster before the suspend, make it busmaster
350 * again
351 */
352 if (pci_dev->is_busmaster)
353 pci_set_master(pci_dev);
354
355 return retval;
356}
357
358static int pci_legacy_suspend(struct device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359{
360 struct pci_dev * pci_dev = to_pci_dev(dev);
361 struct pci_driver * drv = pci_dev->driver;
362 int i = 0;
363
Andrew Morton02669492006-03-23 01:38:34 -0800364 if (drv && drv->suspend) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 i = drv->suspend(pci_dev, state);
Andrew Morton02669492006-03-23 01:38:34 -0800366 suspend_report_result(drv->suspend, i);
367 } else {
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200368 pci_default_pm_suspend(pci_dev);
Andrew Morton02669492006-03-23 01:38:34 -0800369 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 return i;
371}
372
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200373static int pci_legacy_suspend_late(struct device *dev, pm_message_t state)
Linus Torvaldscbd69db2006-06-24 14:50:29 -0700374{
375 struct pci_dev * pci_dev = to_pci_dev(dev);
376 struct pci_driver * drv = pci_dev->driver;
377 int i = 0;
378
379 if (drv && drv->suspend_late) {
380 i = drv->suspend_late(pci_dev, state);
381 suspend_report_result(drv->suspend_late, i);
382 }
383 return i;
384}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200386static int pci_legacy_resume(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387{
Jean Delvare8d92bc22006-04-18 14:49:56 +0200388 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 struct pci_dev * pci_dev = to_pci_dev(dev);
390 struct pci_driver * drv = pci_dev->driver;
391
Rafael J. Wysocki355a72d2008-12-08 00:34:57 +0100392 if (drv && drv->resume) {
Jean Delvare8d92bc22006-04-18 14:49:56 +0200393 error = drv->resume(pci_dev);
Rafael J. Wysocki355a72d2008-12-08 00:34:57 +0100394 } else {
395 pci_default_pm_resume_early(pci_dev);
396 error = pci_default_pm_resume_late(pci_dev);
397 }
Jean Delvare8d92bc22006-04-18 14:49:56 +0200398 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399}
400
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200401static int pci_legacy_resume_early(struct device *dev)
Linus Torvaldscbd69db2006-06-24 14:50:29 -0700402{
403 int error = 0;
404 struct pci_dev * pci_dev = to_pci_dev(dev);
405 struct pci_driver * drv = pci_dev->driver;
406
407 if (drv && drv->resume_early)
408 error = drv->resume_early(pci_dev);
409 return error;
410}
411
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200412static int pci_pm_prepare(struct device *dev)
413{
414 struct device_driver *drv = dev->driver;
415 int error = 0;
416
417 if (drv && drv->pm && drv->pm->prepare)
418 error = drv->pm->prepare(dev);
419
420 return error;
421}
422
423static void pci_pm_complete(struct device *dev)
424{
425 struct device_driver *drv = dev->driver;
426
427 if (drv && drv->pm && drv->pm->complete)
428 drv->pm->complete(dev);
429}
430
431#ifdef CONFIG_SUSPEND
432
433static int pci_pm_suspend(struct device *dev)
434{
435 struct pci_dev *pci_dev = to_pci_dev(dev);
436 struct device_driver *drv = dev->driver;
437 int error = 0;
438
439 if (drv && drv->pm) {
440 if (drv->pm->suspend) {
441 error = drv->pm->suspend(dev);
442 suspend_report_result(drv->pm->suspend, error);
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200443 }
Rafael J. Wysocki355a72d2008-12-08 00:34:57 +0100444 } else if (pci_has_legacy_pm_support(pci_dev)) {
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200445 error = pci_legacy_suspend(dev, PMSG_SUSPEND);
446 }
447 pci_fixup_device(pci_fixup_suspend, pci_dev);
448
449 return error;
450}
451
452static int pci_pm_suspend_noirq(struct device *dev)
Greg KHc8958172005-04-08 14:53:31 +0900453{
Rafael J. Wysocki355a72d2008-12-08 00:34:57 +0100454 struct pci_dev *pci_dev = to_pci_dev(dev);
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200455 struct device_driver *drv = dev->driver;
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200456 int error = 0;
Greg KHc8958172005-04-08 14:53:31 +0900457
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200458 if (drv && drv->pm) {
459 if (drv->pm->suspend_noirq) {
460 error = drv->pm->suspend_noirq(dev);
461 suspend_report_result(drv->pm->suspend_noirq, error);
462 }
Rafael J. Wysocki355a72d2008-12-08 00:34:57 +0100463 } else if (pci_has_legacy_pm_support(pci_dev)) {
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200464 error = pci_legacy_suspend_late(dev, PMSG_SUSPEND);
Rafael J. Wysocki355a72d2008-12-08 00:34:57 +0100465 } else {
466 pci_default_pm_suspend(pci_dev);
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200467 }
468
469 return error;
Greg KHc8958172005-04-08 14:53:31 +0900470}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200472static int pci_pm_resume(struct device *dev)
473{
474 struct pci_dev *pci_dev = to_pci_dev(dev);
475 struct device_driver *drv = dev->driver;
Rafael J. Wysocki355a72d2008-12-08 00:34:57 +0100476 int error = 0;
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200477
478 pci_fixup_device(pci_fixup_resume, pci_dev);
479
480 if (drv && drv->pm) {
Rafael J. Wysocki355a72d2008-12-08 00:34:57 +0100481 if (drv->pm->resume)
482 error = drv->pm->resume(dev);
483 } else if (pci_has_legacy_pm_support(pci_dev)) {
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200484 error = pci_legacy_resume(dev);
Rafael J. Wysocki355a72d2008-12-08 00:34:57 +0100485 } else {
486 error = pci_default_pm_resume_late(pci_dev);
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200487 }
488
489 return error;
490}
491
492static int pci_pm_resume_noirq(struct device *dev)
493{
Rafael J. Wysocki355a72d2008-12-08 00:34:57 +0100494 struct pci_dev *pci_dev = to_pci_dev(dev);
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200495 struct device_driver *drv = dev->driver;
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200496 int error = 0;
497
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200498 pci_fixup_device(pci_fixup_resume_early, to_pci_dev(dev));
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200499
500 if (drv && drv->pm) {
501 if (drv->pm->resume_noirq)
502 error = drv->pm->resume_noirq(dev);
Rafael J. Wysocki355a72d2008-12-08 00:34:57 +0100503 } else if (pci_has_legacy_pm_support(pci_dev)) {
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200504 error = pci_legacy_resume_early(dev);
Rafael J. Wysocki355a72d2008-12-08 00:34:57 +0100505 } else {
506 pci_default_pm_resume_early(pci_dev);
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200507 }
508
509 return error;
510}
511
512#else /* !CONFIG_SUSPEND */
513
514#define pci_pm_suspend NULL
515#define pci_pm_suspend_noirq NULL
516#define pci_pm_resume NULL
517#define pci_pm_resume_noirq NULL
518
519#endif /* !CONFIG_SUSPEND */
520
521#ifdef CONFIG_HIBERNATION
522
523static int pci_pm_freeze(struct device *dev)
524{
525 struct pci_dev *pci_dev = to_pci_dev(dev);
526 struct device_driver *drv = dev->driver;
527 int error = 0;
528
529 if (drv && drv->pm) {
530 if (drv->pm->freeze) {
531 error = drv->pm->freeze(dev);
532 suspend_report_result(drv->pm->freeze, error);
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200533 }
Rafael J. Wysocki355a72d2008-12-08 00:34:57 +0100534 } else if (pci_has_legacy_pm_support(pci_dev)) {
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200535 error = pci_legacy_suspend(dev, PMSG_FREEZE);
536 pci_fixup_device(pci_fixup_suspend, pci_dev);
537 }
538
539 return error;
540}
541
542static int pci_pm_freeze_noirq(struct device *dev)
543{
Rafael J. Wysocki355a72d2008-12-08 00:34:57 +0100544 struct pci_dev *pci_dev = to_pci_dev(dev);
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200545 struct device_driver *drv = dev->driver;
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200546 int error = 0;
547
548 if (drv && drv->pm) {
549 if (drv->pm->freeze_noirq) {
550 error = drv->pm->freeze_noirq(dev);
551 suspend_report_result(drv->pm->freeze_noirq, error);
552 }
Rafael J. Wysocki355a72d2008-12-08 00:34:57 +0100553 } else if (pci_has_legacy_pm_support(pci_dev)) {
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200554 error = pci_legacy_suspend_late(dev, PMSG_FREEZE);
Rafael J. Wysocki355a72d2008-12-08 00:34:57 +0100555 } else {
556 pci_default_pm_suspend(pci_dev);
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200557 }
558
559 return error;
560}
561
562static int pci_pm_thaw(struct device *dev)
563{
Rafael J. Wysocki355a72d2008-12-08 00:34:57 +0100564 struct pci_dev *pci_dev = to_pci_dev(dev);
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200565 struct device_driver *drv = dev->driver;
566 int error = 0;
567
568 if (drv && drv->pm) {
569 if (drv->pm->thaw)
570 error = drv->pm->thaw(dev);
Rafael J. Wysocki355a72d2008-12-08 00:34:57 +0100571 } else if (pci_has_legacy_pm_support(pci_dev)) {
572 pci_fixup_device(pci_fixup_resume, pci_dev);
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200573 error = pci_legacy_resume(dev);
574 }
575
576 return error;
577}
578
579static int pci_pm_thaw_noirq(struct device *dev)
580{
Rafael J. Wysocki355a72d2008-12-08 00:34:57 +0100581 struct pci_dev *pci_dev = to_pci_dev(dev);
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200582 struct device_driver *drv = dev->driver;
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200583 int error = 0;
584
585 if (drv && drv->pm) {
586 if (drv->pm->thaw_noirq)
587 error = drv->pm->thaw_noirq(dev);
Rafael J. Wysocki355a72d2008-12-08 00:34:57 +0100588 } else if (pci_has_legacy_pm_support(pci_dev)) {
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200589 pci_fixup_device(pci_fixup_resume_early, to_pci_dev(dev));
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200590 error = pci_legacy_resume_early(dev);
591 }
592
593 return error;
594}
595
596static int pci_pm_poweroff(struct device *dev)
597{
Rafael J. Wysocki355a72d2008-12-08 00:34:57 +0100598 struct pci_dev *pci_dev = to_pci_dev(dev);
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200599 struct device_driver *drv = dev->driver;
600 int error = 0;
601
Rafael J. Wysocki355a72d2008-12-08 00:34:57 +0100602 pci_fixup_device(pci_fixup_suspend, pci_dev);
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200603
604 if (drv && drv->pm) {
605 if (drv->pm->poweroff) {
606 error = drv->pm->poweroff(dev);
607 suspend_report_result(drv->pm->poweroff, error);
608 }
Rafael J. Wysocki355a72d2008-12-08 00:34:57 +0100609 } else if (pci_has_legacy_pm_support(pci_dev)) {
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200610 error = pci_legacy_suspend(dev, PMSG_HIBERNATE);
611 }
612
613 return error;
614}
615
616static int pci_pm_poweroff_noirq(struct device *dev)
617{
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200618 struct device_driver *drv = dev->driver;
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200619 int error = 0;
620
621 if (drv && drv->pm) {
622 if (drv->pm->poweroff_noirq) {
623 error = drv->pm->poweroff_noirq(dev);
624 suspend_report_result(drv->pm->poweroff_noirq, error);
625 }
Rafael J. Wysocki355a72d2008-12-08 00:34:57 +0100626 } else if (pci_has_legacy_pm_support(to_pci_dev(dev))) {
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200627 error = pci_legacy_suspend_late(dev, PMSG_HIBERNATE);
628 }
629
630 return error;
631}
632
633static int pci_pm_restore(struct device *dev)
634{
635 struct pci_dev *pci_dev = to_pci_dev(dev);
636 struct device_driver *drv = dev->driver;
Rafael J. Wysocki355a72d2008-12-08 00:34:57 +0100637 int error = 0;
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200638
639 if (drv && drv->pm) {
Rafael J. Wysocki355a72d2008-12-08 00:34:57 +0100640 if (drv->pm->restore)
641 error = drv->pm->restore(dev);
642 } else if (pci_has_legacy_pm_support(pci_dev)) {
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200643 error = pci_legacy_resume(dev);
Rafael J. Wysocki355a72d2008-12-08 00:34:57 +0100644 } else {
645 error = pci_default_pm_resume_late(pci_dev);
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200646 }
647 pci_fixup_device(pci_fixup_resume, pci_dev);
648
649 return error;
650}
651
652static int pci_pm_restore_noirq(struct device *dev)
653{
654 struct pci_dev *pci_dev = to_pci_dev(dev);
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200655 struct device_driver *drv = dev->driver;
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200656 int error = 0;
657
658 pci_fixup_device(pci_fixup_resume, pci_dev);
659
660 if (drv && drv->pm) {
661 if (drv->pm->restore_noirq)
662 error = drv->pm->restore_noirq(dev);
Rafael J. Wysocki355a72d2008-12-08 00:34:57 +0100663 } else if (pci_has_legacy_pm_support(pci_dev)) {
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200664 error = pci_legacy_resume_early(dev);
Rafael J. Wysocki355a72d2008-12-08 00:34:57 +0100665 } else {
666 pci_default_pm_resume_early(pci_dev);
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200667 }
668 pci_fixup_device(pci_fixup_resume_early, pci_dev);
669
670 return error;
671}
672
673#else /* !CONFIG_HIBERNATION */
674
675#define pci_pm_freeze NULL
676#define pci_pm_freeze_noirq NULL
677#define pci_pm_thaw NULL
678#define pci_pm_thaw_noirq NULL
679#define pci_pm_poweroff NULL
680#define pci_pm_poweroff_noirq NULL
681#define pci_pm_restore NULL
682#define pci_pm_restore_noirq NULL
683
684#endif /* !CONFIG_HIBERNATION */
685
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200686struct dev_pm_ops pci_dev_pm_ops = {
687 .prepare = pci_pm_prepare,
688 .complete = pci_pm_complete,
689 .suspend = pci_pm_suspend,
690 .resume = pci_pm_resume,
691 .freeze = pci_pm_freeze,
692 .thaw = pci_pm_thaw,
693 .poweroff = pci_pm_poweroff,
694 .restore = pci_pm_restore,
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200695 .suspend_noirq = pci_pm_suspend_noirq,
696 .resume_noirq = pci_pm_resume_noirq,
697 .freeze_noirq = pci_pm_freeze_noirq,
698 .thaw_noirq = pci_pm_thaw_noirq,
699 .poweroff_noirq = pci_pm_poweroff_noirq,
700 .restore_noirq = pci_pm_restore_noirq,
701};
702
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200703#define PCI_PM_OPS_PTR (&pci_dev_pm_ops)
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200704
705#else /* !CONFIG_PM_SLEEP */
706
707#define PCI_PM_OPS_PTR NULL
708
709#endif /* !CONFIG_PM_SLEEP */
710
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711/**
Laurent riffard863b18f2005-10-27 23:12:54 +0200712 * __pci_register_driver - register a new pci driver
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 * @drv: the driver structure to register
Laurent riffard863b18f2005-10-27 23:12:54 +0200714 * @owner: owner module of drv
Randy Dunlapf95d8822007-02-10 14:41:56 -0800715 * @mod_name: module name string
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 *
717 * Adds the driver structure to the list of registered drivers.
718 * Returns a negative value on error, otherwise 0.
Steven Coleeaae4b32005-05-03 18:38:30 -0600719 * If no error occurred, the driver remains registered even if
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 * no device was claimed during registration.
721 */
Greg Kroah-Hartman725522b2007-01-15 11:50:02 -0800722int __pci_register_driver(struct pci_driver *drv, struct module *owner,
723 const char *mod_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724{
725 int error;
726
727 /* initialize common driver fields */
728 drv->driver.name = drv->name;
729 drv->driver.bus = &pci_bus_type;
Laurent riffard863b18f2005-10-27 23:12:54 +0200730 drv->driver.owner = owner;
Greg Kroah-Hartman725522b2007-01-15 11:50:02 -0800731 drv->driver.mod_name = mod_name;
Alan Cox50b00752006-08-16 17:42:18 +0100732
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700733 spin_lock_init(&drv->dynids.lock);
734 INIT_LIST_HEAD(&drv->dynids.list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
736 /* register with core */
737 error = driver_register(&drv->driver);
Akinobu Mita50bf14b2006-11-08 19:53:59 -0800738 if (error)
739 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
Akinobu Mita50bf14b2006-11-08 19:53:59 -0800741 error = pci_create_newid_file(drv);
742 if (error)
743 driver_unregister(&drv->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
745 return error;
746}
747
748/**
749 * pci_unregister_driver - unregister a pci driver
750 * @drv: the driver structure to unregister
751 *
752 * Deletes the driver structure from the list of registered PCI drivers,
753 * gives it a chance to clean up by calling its remove() function for
754 * each device it was responsible for, and marks those devices as
755 * driverless.
756 */
757
758void
759pci_unregister_driver(struct pci_driver *drv)
760{
Greg Kroah-Hartman03d43b12007-11-28 12:23:18 -0800761 pci_remove_newid_file(drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 driver_unregister(&drv->driver);
763 pci_free_dynids(drv);
764}
765
766static struct pci_driver pci_compat_driver = {
767 .name = "compat"
768};
769
770/**
771 * pci_dev_driver - get the pci_driver of a device
772 * @dev: the device to query
773 *
774 * Returns the appropriate pci_driver structure or %NULL if there is no
775 * registered driver for the device.
776 */
777struct pci_driver *
778pci_dev_driver(const struct pci_dev *dev)
779{
780 if (dev->driver)
781 return dev->driver;
782 else {
783 int i;
784 for(i=0; i<=PCI_ROM_RESOURCE; i++)
785 if (dev->resource[i].flags & IORESOURCE_BUSY)
786 return &pci_compat_driver;
787 }
788 return NULL;
789}
790
791/**
792 * pci_bus_match - Tell if a PCI device structure has a matching PCI device id structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 * @dev: the PCI device structure to match against
Randy Dunlap8f7020d2005-10-23 11:57:38 -0700794 * @drv: the device driver to search for matching PCI device id structures
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 *
796 * Used by a driver to check whether a PCI device present in the
Randy Dunlap8f7020d2005-10-23 11:57:38 -0700797 * system is in its list of supported devices. Returns the matching
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 * pci_device_id structure or %NULL if there is no match.
799 */
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700800static int pci_bus_match(struct device *dev, struct device_driver *drv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801{
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700802 struct pci_dev *pci_dev = to_pci_dev(dev);
803 struct pci_driver *pci_drv = to_pci_driver(drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 const struct pci_device_id *found_id;
805
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700806 found_id = pci_match_device(pci_drv, pci_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 if (found_id)
808 return 1;
809
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700810 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811}
812
813/**
814 * pci_dev_get - increments the reference count of the pci device structure
815 * @dev: the device being referenced
816 *
817 * Each live reference to a device should be refcounted.
818 *
819 * Drivers for PCI devices should normally record such references in
820 * their probe() methods, when they bind to a device, and release
821 * them by calling pci_dev_put(), in their disconnect() methods.
822 *
823 * A pointer to the device with the incremented reference counter is returned.
824 */
825struct pci_dev *pci_dev_get(struct pci_dev *dev)
826{
827 if (dev)
828 get_device(&dev->dev);
829 return dev;
830}
831
832/**
833 * pci_dev_put - release a use of the pci device structure
834 * @dev: device that's been disconnected
835 *
836 * Must be called when a user of a device is finished with it. When the last
837 * user of the device calls this function, the memory of the device is freed.
838 */
839void pci_dev_put(struct pci_dev *dev)
840{
841 if (dev)
842 put_device(&dev->dev);
843}
844
845#ifndef CONFIG_HOTPLUG
Kay Sievers7eff2e72007-08-14 15:15:12 +0200846int pci_uevent(struct device *dev, struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847{
848 return -ENODEV;
849}
850#endif
851
852struct bus_type pci_bus_type = {
853 .name = "pci",
854 .match = pci_bus_match,
Kay Sievers312c0042005-11-16 09:00:00 +0100855 .uevent = pci_uevent,
Russell Kingb15d6862006-01-05 14:30:22 +0000856 .probe = pci_device_probe,
857 .remove = pci_device_remove,
Linus Torvaldscbd69db2006-06-24 14:50:29 -0700858 .shutdown = pci_device_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 .dev_attrs = pci_dev_attrs,
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200860 .pm = PCI_PM_OPS_PTR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861};
862
863static int __init pci_driver_init(void)
864{
865 return bus_register(&pci_bus_type);
866}
867
868postcore_initcall(pci_driver_init);
869
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700870EXPORT_SYMBOL(pci_match_id);
Laurent riffard863b18f2005-10-27 23:12:54 +0200871EXPORT_SYMBOL(__pci_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872EXPORT_SYMBOL(pci_unregister_driver);
873EXPORT_SYMBOL(pci_dev_driver);
874EXPORT_SYMBOL(pci_bus_type);
875EXPORT_SYMBOL(pci_dev_get);
876EXPORT_SYMBOL(pci_dev_put);