blob: 73e362992694a4c60e00a0f8bec7038446342494 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * drivers/pci/pci-driver.c
3 *
4 */
5
6#include <linux/pci.h>
7#include <linux/module.h>
8#include <linux/init.h>
9#include <linux/device.h>
Andi Kleend42c6992005-07-06 19:56:03 +020010#include <linux/mempolicy.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080011#include <linux/string.h>
12#include <linux/slab.h>
Tim Schmielau8c65b4a2005-11-07 00:59:43 -080013#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include "pci.h"
15
16/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 * Dynamic device IDs are disabled for !CONFIG_HOTPLUG
18 */
19
Greg Kroah-Hartman75865852005-06-30 02:18:12 -070020struct pci_dynid {
21 struct list_head node;
22 struct pci_device_id id;
23};
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Greg KH3d3c2ae2005-07-06 09:09:38 -070025#ifdef CONFIG_HOTPLUG
26
Linus Torvalds1da177e2005-04-16 15:20:36 -070027/**
Randy Dunlap8f7020d2005-10-23 11:57:38 -070028 * store_new_id - add a new PCI device ID to this driver and re-probe devices
29 * @driver: target device driver
30 * @buf: buffer for scanning device ID data
31 * @count: input size
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 *
33 * Adds a new dynamic pci device ID to this driver,
34 * and causes the driver to probe for all devices again.
35 */
Randy Dunlapf8eb1002005-10-28 20:36:51 -070036static ssize_t
Linus Torvalds1da177e2005-04-16 15:20:36 -070037store_new_id(struct device_driver *driver, const char *buf, size_t count)
38{
Greg Kroah-Hartman75865852005-06-30 02:18:12 -070039 struct pci_dynid *dynid;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 struct pci_driver *pdrv = to_pci_driver(driver);
Jean Delvare6ba18632007-04-07 17:21:28 +020041 __u32 vendor, device, subvendor=PCI_ANY_ID,
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 subdevice=PCI_ANY_ID, class=0, class_mask=0;
43 unsigned long driver_data=0;
44 int fields=0;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -070045 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47 fields = sscanf(buf, "%x %x %x %x %x %x %lux",
48 &vendor, &device, &subvendor, &subdevice,
49 &class, &class_mask, &driver_data);
Jean Delvare6ba18632007-04-07 17:21:28 +020050 if (fields < 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 return -EINVAL;
52
Eric Sesterhennf5afe802006-02-28 15:34:49 +010053 dynid = kzalloc(sizeof(*dynid), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 if (!dynid)
55 return -ENOMEM;
56
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 dynid->id.vendor = vendor;
58 dynid->id.device = device;
59 dynid->id.subvendor = subvendor;
60 dynid->id.subdevice = subdevice;
61 dynid->id.class = class;
62 dynid->id.class_mask = class_mask;
63 dynid->id.driver_data = pdrv->dynids.use_driver_data ?
64 driver_data : 0UL;
65
66 spin_lock(&pdrv->dynids.lock);
Michael Ellermana56bc692007-09-14 15:33:13 +100067 list_add_tail(&dynid->node, &pdrv->dynids.list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 spin_unlock(&pdrv->dynids.lock);
69
Greg Kroah-Hartman75865852005-06-30 02:18:12 -070070 if (get_driver(&pdrv->driver)) {
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -070071 retval = driver_attach(&pdrv->driver);
Greg Kroah-Hartman75865852005-06-30 02:18:12 -070072 put_driver(&pdrv->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 }
74
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -070075 if (retval)
76 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 return count;
78}
Linus Torvalds1da177e2005-04-16 15:20:36 -070079static DRIVER_ATTR(new_id, S_IWUSR, NULL, store_new_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81static void
82pci_free_dynids(struct pci_driver *drv)
83{
Greg Kroah-Hartman75865852005-06-30 02:18:12 -070084 struct pci_dynid *dynid, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86 spin_lock(&drv->dynids.lock);
Greg Kroah-Hartman75865852005-06-30 02:18:12 -070087 list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 list_del(&dynid->node);
89 kfree(dynid);
90 }
91 spin_unlock(&drv->dynids.lock);
92}
93
94static int
95pci_create_newid_file(struct pci_driver *drv)
96{
97 int error = 0;
98 if (drv->probe != NULL)
Greg Kroah-Hartman03d43b12007-11-28 12:23:18 -080099 error = driver_create_file(&drv->driver, &driver_attr_new_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 return error;
101}
102
Greg Kroah-Hartman03d43b12007-11-28 12:23:18 -0800103static void pci_remove_newid_file(struct pci_driver *drv)
104{
105 driver_remove_file(&drv->driver, &driver_attr_new_id);
106}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107#else /* !CONFIG_HOTPLUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108static inline void pci_free_dynids(struct pci_driver *drv) {}
109static inline int pci_create_newid_file(struct pci_driver *drv)
110{
111 return 0;
112}
Greg Kroah-Hartman03d43b12007-11-28 12:23:18 -0800113static inline void pci_remove_newid_file(struct pci_driver *drv) {}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114#endif
115
116/**
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700117 * pci_match_id - See if a pci device matches a given pci_id table
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 * @ids: array of PCI device id structures to search in
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700119 * @dev: the PCI device structure to match against.
120 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 * Used by a driver to check whether a PCI device present in the
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700122 * system is in its list of supported devices. Returns the matching
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 * pci_device_id structure or %NULL if there is no match.
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700124 *
Randy Dunlap8b607562007-05-09 07:19:14 +0200125 * Deprecated, don't use this as it will not catch any dynamic ids
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700126 * that a driver might want to check for.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 */
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700128const struct pci_device_id *pci_match_id(const struct pci_device_id *ids,
129 struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130{
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700131 if (ids) {
132 while (ids->vendor || ids->subvendor || ids->class_mask) {
133 if (pci_match_one_device(ids, dev))
134 return ids;
135 ids++;
136 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 }
138 return NULL;
139}
140
141/**
Randy Dunlapae9608a2007-01-09 21:41:01 -0800142 * pci_match_device - Tell if a PCI device structure has a matching PCI device id structure
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700143 * @drv: the PCI driver to match against
Henrik Kretzschmar39ba4872006-08-15 10:57:16 +0200144 * @dev: the PCI device structure to match against
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700145 *
146 * Used by a driver to check whether a PCI device present in the
147 * system is in its list of supported devices. Returns the matching
148 * pci_device_id structure or %NULL if there is no match.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 */
Adrian Bunkd73460d2007-10-24 18:27:18 +0200150static const struct pci_device_id *pci_match_device(struct pci_driver *drv,
151 struct pci_dev *dev)
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700152{
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700153 struct pci_dynid *dynid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Russell King7461b602006-11-29 21:18:04 +0000155 /* Look at the dynamic ids first, before the static ones */
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700156 spin_lock(&drv->dynids.lock);
157 list_for_each_entry(dynid, &drv->dynids.list, node) {
158 if (pci_match_one_device(&dynid->id, dev)) {
159 spin_unlock(&drv->dynids.lock);
160 return &dynid->id;
161 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 }
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700163 spin_unlock(&drv->dynids.lock);
Russell King7461b602006-11-29 21:18:04 +0000164
165 return pci_match_id(drv->id_table, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166}
167
Andi Kleend42c6992005-07-06 19:56:03 +0200168static int pci_call_probe(struct pci_driver *drv, struct pci_dev *dev,
169 const struct pci_device_id *id)
170{
171 int error;
172#ifdef CONFIG_NUMA
173 /* Execute driver initialization on node where the
174 device's bus is attached to. This way the driver likely
175 allocates its local memory on the right node without
176 any need to change it. */
177 struct mempolicy *oldpol;
178 cpumask_t oldmask = current->cpus_allowed;
179 int node = pcibus_to_node(dev->bus);
180 if (node >= 0 && node_online(node))
181 set_cpus_allowed(current, node_to_cpumask(node));
182 /* And set default memory allocation policy */
183 oldpol = current->mempolicy;
184 current->mempolicy = &default_policy;
185 mpol_get(current->mempolicy);
186#endif
187 error = drv->probe(dev, id);
188#ifdef CONFIG_NUMA
189 set_cpus_allowed(current, oldmask);
190 mpol_free(current->mempolicy);
191 current->mempolicy = oldpol;
192#endif
193 return error;
194}
195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196/**
197 * __pci_device_probe()
Randy Dunlap8f7020d2005-10-23 11:57:38 -0700198 * @drv: driver to call to check if it wants the PCI device
199 * @pci_dev: PCI device being probed
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 *
Randy Dunlap8f7020d2005-10-23 11:57:38 -0700201 * returns 0 on success, else error.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 * side-effect: pci_dev->driver is set to drv when drv claims pci_dev.
203 */
204static int
205__pci_device_probe(struct pci_driver *drv, struct pci_dev *pci_dev)
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700206{
207 const struct pci_device_id *id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 int error = 0;
209
210 if (!pci_dev->driver && drv->probe) {
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700211 error = -ENODEV;
212
213 id = pci_match_device(drv, pci_dev);
214 if (id)
Andi Kleend42c6992005-07-06 19:56:03 +0200215 error = pci_call_probe(drv, pci_dev, id);
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700216 if (error >= 0) {
217 pci_dev->driver = drv;
218 error = 0;
219 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 }
221 return error;
222}
223
224static int pci_device_probe(struct device * dev)
225{
226 int error = 0;
227 struct pci_driver *drv;
228 struct pci_dev *pci_dev;
229
230 drv = to_pci_driver(dev->driver);
231 pci_dev = to_pci_dev(dev);
232 pci_dev_get(pci_dev);
233 error = __pci_device_probe(drv, pci_dev);
234 if (error)
235 pci_dev_put(pci_dev);
236
237 return error;
238}
239
240static int pci_device_remove(struct device * dev)
241{
242 struct pci_dev * pci_dev = to_pci_dev(dev);
243 struct pci_driver * drv = pci_dev->driver;
244
245 if (drv) {
246 if (drv->remove)
247 drv->remove(pci_dev);
248 pci_dev->driver = NULL;
249 }
250
251 /*
Shaohua Li2449e062006-10-20 14:45:32 -0700252 * If the device is still on, set the power state as "unknown",
253 * since it might change by the next time we load the driver.
254 */
255 if (pci_dev->current_state == PCI_D0)
256 pci_dev->current_state = PCI_UNKNOWN;
257
258 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 * We would love to complain here if pci_dev->is_enabled is set, that
260 * the driver should have called pci_disable_device(), but the
261 * unfortunate fact is there are too many odd BIOS and bridge setups
262 * that don't like drivers doing that all of the time.
263 * Oh well, we can dream of sane hardware when we sleep, no matter how
264 * horrible the crap we have to deal with is when we are awake...
265 */
266
267 pci_dev_put(pci_dev);
268 return 0;
269}
270
271static int pci_device_suspend(struct device * dev, pm_message_t state)
272{
273 struct pci_dev * pci_dev = to_pci_dev(dev);
274 struct pci_driver * drv = pci_dev->driver;
275 int i = 0;
276
Andrew Morton02669492006-03-23 01:38:34 -0800277 if (drv && drv->suspend) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 i = drv->suspend(pci_dev, state);
Andrew Morton02669492006-03-23 01:38:34 -0800279 suspend_report_result(drv->suspend, i);
280 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 pci_save_state(pci_dev);
Shaohua Li2449e062006-10-20 14:45:32 -0700282 /*
283 * mark its power state as "unknown", since we don't know if
284 * e.g. the BIOS will change its device state when we suspend.
285 */
286 if (pci_dev->current_state == PCI_D0)
287 pci_dev->current_state = PCI_UNKNOWN;
Andrew Morton02669492006-03-23 01:38:34 -0800288 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 return i;
290}
291
Linus Torvaldscbd69db2006-06-24 14:50:29 -0700292static int pci_device_suspend_late(struct device * dev, pm_message_t state)
293{
294 struct pci_dev * pci_dev = to_pci_dev(dev);
295 struct pci_driver * drv = pci_dev->driver;
296 int i = 0;
297
298 if (drv && drv->suspend_late) {
299 i = drv->suspend_late(pci_dev, state);
300 suspend_report_result(drv->suspend_late, i);
301 }
302 return i;
303}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
Greg Kroah-Hartman95a62962005-07-28 11:37:33 -0700305/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 * Default resume method for devices that have no driver provided resume,
307 * or not even a driver at all.
308 */
Jean Delvare8d92bc22006-04-18 14:49:56 +0200309static int pci_default_resume(struct pci_dev *pci_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310{
Jean Delvare8d92bc22006-04-18 14:49:56 +0200311 int retval = 0;
Greg Kroah-Hartman95a62962005-07-28 11:37:33 -0700312
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 /* restore the PCI config space */
314 pci_restore_state(pci_dev);
315 /* if the device was enabled before suspend, reenable */
Tejun Heo0b62e132007-07-27 14:43:35 +0900316 retval = pci_reenable_device(pci_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 /* if the device was busmaster before the suspend, make it busmaster again */
318 if (pci_dev->is_busmaster)
319 pci_set_master(pci_dev);
Jean Delvare8d92bc22006-04-18 14:49:56 +0200320
321 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322}
323
324static int pci_device_resume(struct device * dev)
325{
Jean Delvare8d92bc22006-04-18 14:49:56 +0200326 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 struct pci_dev * pci_dev = to_pci_dev(dev);
328 struct pci_driver * drv = pci_dev->driver;
329
330 if (drv && drv->resume)
Jean Delvare8d92bc22006-04-18 14:49:56 +0200331 error = drv->resume(pci_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 else
Jean Delvare8d92bc22006-04-18 14:49:56 +0200333 error = pci_default_resume(pci_dev);
334 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335}
336
Linus Torvaldscbd69db2006-06-24 14:50:29 -0700337static int pci_device_resume_early(struct device * dev)
338{
339 int error = 0;
340 struct pci_dev * pci_dev = to_pci_dev(dev);
341 struct pci_driver * drv = pci_dev->driver;
342
Alan Cox1597cac2006-12-04 15:14:45 -0800343 pci_fixup_device(pci_fixup_resume, pci_dev);
344
Linus Torvaldscbd69db2006-06-24 14:50:29 -0700345 if (drv && drv->resume_early)
346 error = drv->resume_early(pci_dev);
347 return error;
348}
349
Greg KHc8958172005-04-08 14:53:31 +0900350static void pci_device_shutdown(struct device *dev)
351{
352 struct pci_dev *pci_dev = to_pci_dev(dev);
353 struct pci_driver *drv = pci_dev->driver;
354
355 if (drv && drv->shutdown)
356 drv->shutdown(pci_dev);
357}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
359#define kobj_to_pci_driver(obj) container_of(obj, struct device_driver, kobj)
360#define attr_to_driver_attribute(obj) container_of(obj, struct driver_attribute, attr)
361
362static ssize_t
363pci_driver_attr_show(struct kobject * kobj, struct attribute *attr, char *buf)
364{
365 struct device_driver *driver = kobj_to_pci_driver(kobj);
366 struct driver_attribute *dattr = attr_to_driver_attribute(attr);
Dmitry Torokhovfc7e4822005-04-29 01:26:27 -0500367 ssize_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
Dmitry Torokhovfc7e4822005-04-29 01:26:27 -0500369 if (!get_driver(driver))
370 return -ENODEV;
371
372 ret = dattr->show ? dattr->show(driver, buf) : -EIO;
373
374 put_driver(driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 return ret;
376}
377
378static ssize_t
379pci_driver_attr_store(struct kobject * kobj, struct attribute *attr,
380 const char *buf, size_t count)
381{
382 struct device_driver *driver = kobj_to_pci_driver(kobj);
383 struct driver_attribute *dattr = attr_to_driver_attribute(attr);
Dmitry Torokhovfc7e4822005-04-29 01:26:27 -0500384 ssize_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
Dmitry Torokhovfc7e4822005-04-29 01:26:27 -0500386 if (!get_driver(driver))
387 return -ENODEV;
388
389 ret = dattr->store ? dattr->store(driver, buf, count) : -EIO;
390
391 put_driver(driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 return ret;
393}
394
395static struct sysfs_ops pci_driver_sysfs_ops = {
396 .show = pci_driver_attr_show,
397 .store = pci_driver_attr_store,
398};
399static struct kobj_type pci_driver_kobj_type = {
400 .sysfs_ops = &pci_driver_sysfs_ops,
401};
402
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403/**
Laurent riffard863b18f2005-10-27 23:12:54 +0200404 * __pci_register_driver - register a new pci driver
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 * @drv: the driver structure to register
Laurent riffard863b18f2005-10-27 23:12:54 +0200406 * @owner: owner module of drv
Randy Dunlapf95d8822007-02-10 14:41:56 -0800407 * @mod_name: module name string
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 *
409 * Adds the driver structure to the list of registered drivers.
410 * Returns a negative value on error, otherwise 0.
Steven Coleeaae4b32005-05-03 18:38:30 -0600411 * If no error occurred, the driver remains registered even if
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 * no device was claimed during registration.
413 */
Greg Kroah-Hartman725522b2007-01-15 11:50:02 -0800414int __pci_register_driver(struct pci_driver *drv, struct module *owner,
415 const char *mod_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416{
417 int error;
418
419 /* initialize common driver fields */
420 drv->driver.name = drv->name;
421 drv->driver.bus = &pci_bus_type;
Laurent riffard863b18f2005-10-27 23:12:54 +0200422 drv->driver.owner = owner;
Greg Kroah-Hartman725522b2007-01-15 11:50:02 -0800423 drv->driver.mod_name = mod_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 drv->driver.kobj.ktype = &pci_driver_kobj_type;
Alan Cox50b00752006-08-16 17:42:18 +0100425
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700426 spin_lock_init(&drv->dynids.lock);
427 INIT_LIST_HEAD(&drv->dynids.list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
429 /* register with core */
430 error = driver_register(&drv->driver);
Akinobu Mita50bf14b2006-11-08 19:53:59 -0800431 if (error)
432 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
Akinobu Mita50bf14b2006-11-08 19:53:59 -0800434 error = pci_create_newid_file(drv);
435 if (error)
436 driver_unregister(&drv->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
438 return error;
439}
440
441/**
442 * pci_unregister_driver - unregister a pci driver
443 * @drv: the driver structure to unregister
444 *
445 * Deletes the driver structure from the list of registered PCI drivers,
446 * gives it a chance to clean up by calling its remove() function for
447 * each device it was responsible for, and marks those devices as
448 * driverless.
449 */
450
451void
452pci_unregister_driver(struct pci_driver *drv)
453{
Greg Kroah-Hartman03d43b12007-11-28 12:23:18 -0800454 pci_remove_newid_file(drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 driver_unregister(&drv->driver);
456 pci_free_dynids(drv);
457}
458
459static struct pci_driver pci_compat_driver = {
460 .name = "compat"
461};
462
463/**
464 * pci_dev_driver - get the pci_driver of a device
465 * @dev: the device to query
466 *
467 * Returns the appropriate pci_driver structure or %NULL if there is no
468 * registered driver for the device.
469 */
470struct pci_driver *
471pci_dev_driver(const struct pci_dev *dev)
472{
473 if (dev->driver)
474 return dev->driver;
475 else {
476 int i;
477 for(i=0; i<=PCI_ROM_RESOURCE; i++)
478 if (dev->resource[i].flags & IORESOURCE_BUSY)
479 return &pci_compat_driver;
480 }
481 return NULL;
482}
483
484/**
485 * pci_bus_match - Tell if a PCI device structure has a matching PCI device id structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 * @dev: the PCI device structure to match against
Randy Dunlap8f7020d2005-10-23 11:57:38 -0700487 * @drv: the device driver to search for matching PCI device id structures
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 *
489 * Used by a driver to check whether a PCI device present in the
Randy Dunlap8f7020d2005-10-23 11:57:38 -0700490 * system is in its list of supported devices. Returns the matching
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 * pci_device_id structure or %NULL if there is no match.
492 */
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700493static int pci_bus_match(struct device *dev, struct device_driver *drv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494{
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700495 struct pci_dev *pci_dev = to_pci_dev(dev);
496 struct pci_driver *pci_drv = to_pci_driver(drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 const struct pci_device_id *found_id;
498
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700499 found_id = pci_match_device(pci_drv, pci_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 if (found_id)
501 return 1;
502
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700503 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504}
505
506/**
507 * pci_dev_get - increments the reference count of the pci device structure
508 * @dev: the device being referenced
509 *
510 * Each live reference to a device should be refcounted.
511 *
512 * Drivers for PCI devices should normally record such references in
513 * their probe() methods, when they bind to a device, and release
514 * them by calling pci_dev_put(), in their disconnect() methods.
515 *
516 * A pointer to the device with the incremented reference counter is returned.
517 */
518struct pci_dev *pci_dev_get(struct pci_dev *dev)
519{
520 if (dev)
521 get_device(&dev->dev);
522 return dev;
523}
524
525/**
526 * pci_dev_put - release a use of the pci device structure
527 * @dev: device that's been disconnected
528 *
529 * Must be called when a user of a device is finished with it. When the last
530 * user of the device calls this function, the memory of the device is freed.
531 */
532void pci_dev_put(struct pci_dev *dev)
533{
534 if (dev)
535 put_device(&dev->dev);
536}
537
538#ifndef CONFIG_HOTPLUG
Kay Sievers7eff2e72007-08-14 15:15:12 +0200539int pci_uevent(struct device *dev, struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540{
541 return -ENODEV;
542}
543#endif
544
545struct bus_type pci_bus_type = {
546 .name = "pci",
547 .match = pci_bus_match,
Kay Sievers312c0042005-11-16 09:00:00 +0100548 .uevent = pci_uevent,
Russell Kingb15d6862006-01-05 14:30:22 +0000549 .probe = pci_device_probe,
550 .remove = pci_device_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 .suspend = pci_device_suspend,
Linus Torvaldscbd69db2006-06-24 14:50:29 -0700552 .suspend_late = pci_device_suspend_late,
553 .resume_early = pci_device_resume_early,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 .resume = pci_device_resume,
Linus Torvaldscbd69db2006-06-24 14:50:29 -0700555 .shutdown = pci_device_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 .dev_attrs = pci_dev_attrs,
557};
558
559static int __init pci_driver_init(void)
560{
561 return bus_register(&pci_bus_type);
562}
563
564postcore_initcall(pci_driver_init);
565
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700566EXPORT_SYMBOL(pci_match_id);
Laurent riffard863b18f2005-10-27 23:12:54 +0200567EXPORT_SYMBOL(__pci_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568EXPORT_SYMBOL(pci_unregister_driver);
569EXPORT_SYMBOL(pci_dev_driver);
570EXPORT_SYMBOL(pci_bus_type);
571EXPORT_SYMBOL(pci_dev_get);
572EXPORT_SYMBOL(pci_dev_put);