blob: d8ace1f90dd2a13f864bb2f87b649377c11d4c14 [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/*
17 * Registration of PCI drivers and handling of hot-pluggable devices.
18 */
19
Greg Kroah-Hartman0f397f82006-07-18 10:59:59 -070020/* multithreaded probe logic */
21static int pci_multithread_probe =
22#ifdef CONFIG_PCI_MULTITHREAD_PROBE
23 1;
24#else
25 0;
26#endif
27__module_param_call("", pci_multithread_probe, param_set_bool, param_get_bool, &pci_multithread_probe, 0644);
28
29
Linus Torvalds1da177e2005-04-16 15:20:36 -070030/*
31 * Dynamic device IDs are disabled for !CONFIG_HOTPLUG
32 */
33
Greg Kroah-Hartman75865852005-06-30 02:18:12 -070034struct pci_dynid {
35 struct list_head node;
36 struct pci_device_id id;
37};
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Greg KH3d3c2ae2005-07-06 09:09:38 -070039#ifdef CONFIG_HOTPLUG
40
Linus Torvalds1da177e2005-04-16 15:20:36 -070041/**
Randy Dunlap8f7020d2005-10-23 11:57:38 -070042 * store_new_id - add a new PCI device ID to this driver and re-probe devices
43 * @driver: target device driver
44 * @buf: buffer for scanning device ID data
45 * @count: input size
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 *
47 * Adds a new dynamic pci device ID to this driver,
48 * and causes the driver to probe for all devices again.
49 */
Randy Dunlapf8eb1002005-10-28 20:36:51 -070050static ssize_t
Linus Torvalds1da177e2005-04-16 15:20:36 -070051store_new_id(struct device_driver *driver, const char *buf, size_t count)
52{
Greg Kroah-Hartman75865852005-06-30 02:18:12 -070053 struct pci_dynid *dynid;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 struct pci_driver *pdrv = to_pci_driver(driver);
55 __u32 vendor=PCI_ANY_ID, device=PCI_ANY_ID, subvendor=PCI_ANY_ID,
56 subdevice=PCI_ANY_ID, class=0, class_mask=0;
57 unsigned long driver_data=0;
58 int fields=0;
59
60 fields = sscanf(buf, "%x %x %x %x %x %x %lux",
61 &vendor, &device, &subvendor, &subdevice,
62 &class, &class_mask, &driver_data);
63 if (fields < 0)
64 return -EINVAL;
65
Eric Sesterhennf5afe802006-02-28 15:34:49 +010066 dynid = kzalloc(sizeof(*dynid), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 if (!dynid)
68 return -ENOMEM;
69
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 INIT_LIST_HEAD(&dynid->node);
71 dynid->id.vendor = vendor;
72 dynid->id.device = device;
73 dynid->id.subvendor = subvendor;
74 dynid->id.subdevice = subdevice;
75 dynid->id.class = class;
76 dynid->id.class_mask = class_mask;
77 dynid->id.driver_data = pdrv->dynids.use_driver_data ?
78 driver_data : 0UL;
79
80 spin_lock(&pdrv->dynids.lock);
81 list_add_tail(&pdrv->dynids.list, &dynid->node);
82 spin_unlock(&pdrv->dynids.lock);
83
Greg Kroah-Hartman75865852005-06-30 02:18:12 -070084 if (get_driver(&pdrv->driver)) {
85 driver_attach(&pdrv->driver);
86 put_driver(&pdrv->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 }
88
89 return count;
90}
Linus Torvalds1da177e2005-04-16 15:20:36 -070091static DRIVER_ATTR(new_id, S_IWUSR, NULL, store_new_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
93static void
94pci_free_dynids(struct pci_driver *drv)
95{
Greg Kroah-Hartman75865852005-06-30 02:18:12 -070096 struct pci_dynid *dynid, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
98 spin_lock(&drv->dynids.lock);
Greg Kroah-Hartman75865852005-06-30 02:18:12 -070099 list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 list_del(&dynid->node);
101 kfree(dynid);
102 }
103 spin_unlock(&drv->dynids.lock);
104}
105
106static int
107pci_create_newid_file(struct pci_driver *drv)
108{
109 int error = 0;
110 if (drv->probe != NULL)
111 error = sysfs_create_file(&drv->driver.kobj,
112 &driver_attr_new_id.attr);
113 return error;
114}
115
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116#else /* !CONFIG_HOTPLUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117static inline void pci_free_dynids(struct pci_driver *drv) {}
118static inline int pci_create_newid_file(struct pci_driver *drv)
119{
120 return 0;
121}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122#endif
123
124/**
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700125 * pci_match_id - See if a pci device matches a given pci_id table
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 * @ids: array of PCI device id structures to search in
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700127 * @dev: the PCI device structure to match against.
128 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 * Used by a driver to check whether a PCI device present in the
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700130 * system is in its list of supported devices. Returns the matching
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 * pci_device_id structure or %NULL if there is no match.
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700132 *
133 * Depreciated, don't use this as it will not catch any dynamic ids
134 * that a driver might want to check for.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 */
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700136const struct pci_device_id *pci_match_id(const struct pci_device_id *ids,
137 struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138{
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700139 if (ids) {
140 while (ids->vendor || ids->subvendor || ids->class_mask) {
141 if (pci_match_one_device(ids, dev))
142 return ids;
143 ids++;
144 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 }
146 return NULL;
147}
148
149/**
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700150 * pci_match_device - Tell if a PCI device structure has a matching
151 * PCI device id structure
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700152 * @drv: the PCI driver to match against
Henrik Kretzschmar39ba4872006-08-15 10:57:16 +0200153 * @dev: the PCI device structure to match against
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700154 *
155 * Used by a driver to check whether a PCI device present in the
156 * system is in its list of supported devices. Returns the matching
157 * pci_device_id structure or %NULL if there is no match.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 */
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700159const struct pci_device_id *pci_match_device(struct pci_driver *drv,
160 struct pci_dev *dev)
161{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 const struct pci_device_id *id;
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700163 struct pci_dynid *dynid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700165 id = pci_match_id(drv->id_table, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 if (id)
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700167 return id;
168
169 /* static ids didn't match, lets look at the dynamic ones */
170 spin_lock(&drv->dynids.lock);
171 list_for_each_entry(dynid, &drv->dynids.list, node) {
172 if (pci_match_one_device(&dynid->id, dev)) {
173 spin_unlock(&drv->dynids.lock);
174 return &dynid->id;
175 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 }
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700177 spin_unlock(&drv->dynids.lock);
178 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179}
180
Andi Kleend42c6992005-07-06 19:56:03 +0200181static int pci_call_probe(struct pci_driver *drv, struct pci_dev *dev,
182 const struct pci_device_id *id)
183{
184 int error;
185#ifdef CONFIG_NUMA
186 /* Execute driver initialization on node where the
187 device's bus is attached to. This way the driver likely
188 allocates its local memory on the right node without
189 any need to change it. */
190 struct mempolicy *oldpol;
191 cpumask_t oldmask = current->cpus_allowed;
192 int node = pcibus_to_node(dev->bus);
193 if (node >= 0 && node_online(node))
194 set_cpus_allowed(current, node_to_cpumask(node));
195 /* And set default memory allocation policy */
196 oldpol = current->mempolicy;
197 current->mempolicy = &default_policy;
198 mpol_get(current->mempolicy);
199#endif
200 error = drv->probe(dev, id);
201#ifdef CONFIG_NUMA
202 set_cpus_allowed(current, oldmask);
203 mpol_free(current->mempolicy);
204 current->mempolicy = oldpol;
205#endif
206 return error;
207}
208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209/**
210 * __pci_device_probe()
Randy Dunlap8f7020d2005-10-23 11:57:38 -0700211 * @drv: driver to call to check if it wants the PCI device
212 * @pci_dev: PCI device being probed
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 *
Randy Dunlap8f7020d2005-10-23 11:57:38 -0700214 * returns 0 on success, else error.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 * side-effect: pci_dev->driver is set to drv when drv claims pci_dev.
216 */
217static int
218__pci_device_probe(struct pci_driver *drv, struct pci_dev *pci_dev)
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700219{
220 const struct pci_device_id *id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 int error = 0;
222
223 if (!pci_dev->driver && drv->probe) {
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700224 error = -ENODEV;
225
226 id = pci_match_device(drv, pci_dev);
227 if (id)
Andi Kleend42c6992005-07-06 19:56:03 +0200228 error = pci_call_probe(drv, pci_dev, id);
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700229 if (error >= 0) {
230 pci_dev->driver = drv;
231 error = 0;
232 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 }
234 return error;
235}
236
237static int pci_device_probe(struct device * dev)
238{
239 int error = 0;
240 struct pci_driver *drv;
241 struct pci_dev *pci_dev;
242
243 drv = to_pci_driver(dev->driver);
244 pci_dev = to_pci_dev(dev);
245 pci_dev_get(pci_dev);
246 error = __pci_device_probe(drv, pci_dev);
247 if (error)
248 pci_dev_put(pci_dev);
249
250 return error;
251}
252
253static int pci_device_remove(struct device * dev)
254{
255 struct pci_dev * pci_dev = to_pci_dev(dev);
256 struct pci_driver * drv = pci_dev->driver;
257
258 if (drv) {
259 if (drv->remove)
260 drv->remove(pci_dev);
261 pci_dev->driver = NULL;
262 }
263
264 /*
265 * 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);
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 */
316 if (pci_dev->is_enabled)
Greg Kroah-Hartman95a62962005-07-28 11:37:33 -0700317 retval = pci_enable_device(pci_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 /* if the device was busmaster before the suspend, make it busmaster again */
319 if (pci_dev->is_busmaster)
320 pci_set_master(pci_dev);
Jean Delvare8d92bc22006-04-18 14:49:56 +0200321
322 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323}
324
325static int pci_device_resume(struct device * dev)
326{
Jean Delvare8d92bc22006-04-18 14:49:56 +0200327 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 struct pci_dev * pci_dev = to_pci_dev(dev);
329 struct pci_driver * drv = pci_dev->driver;
330
331 if (drv && drv->resume)
Jean Delvare8d92bc22006-04-18 14:49:56 +0200332 error = drv->resume(pci_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 else
Jean Delvare8d92bc22006-04-18 14:49:56 +0200334 error = pci_default_resume(pci_dev);
335 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336}
337
Linus Torvaldscbd69db2006-06-24 14:50:29 -0700338static int pci_device_resume_early(struct device * dev)
339{
340 int error = 0;
341 struct pci_dev * pci_dev = to_pci_dev(dev);
342 struct pci_driver * drv = pci_dev->driver;
343
344 if (drv && drv->resume_early)
345 error = drv->resume_early(pci_dev);
346 return error;
347}
348
Greg KHc8958172005-04-08 14:53:31 +0900349static void pci_device_shutdown(struct device *dev)
350{
351 struct pci_dev *pci_dev = to_pci_dev(dev);
352 struct pci_driver *drv = pci_dev->driver;
353
354 if (drv && drv->shutdown)
355 drv->shutdown(pci_dev);
356}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
358#define kobj_to_pci_driver(obj) container_of(obj, struct device_driver, kobj)
359#define attr_to_driver_attribute(obj) container_of(obj, struct driver_attribute, attr)
360
361static ssize_t
362pci_driver_attr_show(struct kobject * kobj, struct attribute *attr, char *buf)
363{
364 struct device_driver *driver = kobj_to_pci_driver(kobj);
365 struct driver_attribute *dattr = attr_to_driver_attribute(attr);
Dmitry Torokhovfc7e4822005-04-29 01:26:27 -0500366 ssize_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
Dmitry Torokhovfc7e4822005-04-29 01:26:27 -0500368 if (!get_driver(driver))
369 return -ENODEV;
370
371 ret = dattr->show ? dattr->show(driver, buf) : -EIO;
372
373 put_driver(driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 return ret;
375}
376
377static ssize_t
378pci_driver_attr_store(struct kobject * kobj, struct attribute *attr,
379 const char *buf, size_t count)
380{
381 struct device_driver *driver = kobj_to_pci_driver(kobj);
382 struct driver_attribute *dattr = attr_to_driver_attribute(attr);
Dmitry Torokhovfc7e4822005-04-29 01:26:27 -0500383 ssize_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
Dmitry Torokhovfc7e4822005-04-29 01:26:27 -0500385 if (!get_driver(driver))
386 return -ENODEV;
387
388 ret = dattr->store ? dattr->store(driver, buf, count) : -EIO;
389
390 put_driver(driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 return ret;
392}
393
394static struct sysfs_ops pci_driver_sysfs_ops = {
395 .show = pci_driver_attr_show,
396 .store = pci_driver_attr_store,
397};
398static struct kobj_type pci_driver_kobj_type = {
399 .sysfs_ops = &pci_driver_sysfs_ops,
400};
401
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402/**
Laurent riffard863b18f2005-10-27 23:12:54 +0200403 * __pci_register_driver - register a new pci driver
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 * @drv: the driver structure to register
Laurent riffard863b18f2005-10-27 23:12:54 +0200405 * @owner: owner module of drv
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 *
407 * Adds the driver structure to the list of registered drivers.
408 * Returns a negative value on error, otherwise 0.
Steven Coleeaae4b32005-05-03 18:38:30 -0600409 * If no error occurred, the driver remains registered even if
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 * no device was claimed during registration.
411 */
Laurent riffard863b18f2005-10-27 23:12:54 +0200412int __pci_register_driver(struct pci_driver *drv, struct module *owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413{
414 int error;
415
416 /* initialize common driver fields */
417 drv->driver.name = drv->name;
418 drv->driver.bus = &pci_bus_type;
Laurent riffard863b18f2005-10-27 23:12:54 +0200419 drv->driver.owner = owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 drv->driver.kobj.ktype = &pci_driver_kobj_type;
Greg Kroah-Hartman0f397f82006-07-18 10:59:59 -0700421 drv->driver.multithread_probe = pci_multithread_probe;
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700422
423 spin_lock_init(&drv->dynids.lock);
424 INIT_LIST_HEAD(&drv->dynids.list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
426 /* register with core */
427 error = driver_register(&drv->driver);
428
429 if (!error)
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700430 error = pci_create_newid_file(drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
432 return error;
433}
434
435/**
436 * pci_unregister_driver - unregister a pci driver
437 * @drv: the driver structure to unregister
438 *
439 * Deletes the driver structure from the list of registered PCI drivers,
440 * gives it a chance to clean up by calling its remove() function for
441 * each device it was responsible for, and marks those devices as
442 * driverless.
443 */
444
445void
446pci_unregister_driver(struct pci_driver *drv)
447{
448 driver_unregister(&drv->driver);
449 pci_free_dynids(drv);
450}
451
452static struct pci_driver pci_compat_driver = {
453 .name = "compat"
454};
455
456/**
457 * pci_dev_driver - get the pci_driver of a device
458 * @dev: the device to query
459 *
460 * Returns the appropriate pci_driver structure or %NULL if there is no
461 * registered driver for the device.
462 */
463struct pci_driver *
464pci_dev_driver(const struct pci_dev *dev)
465{
466 if (dev->driver)
467 return dev->driver;
468 else {
469 int i;
470 for(i=0; i<=PCI_ROM_RESOURCE; i++)
471 if (dev->resource[i].flags & IORESOURCE_BUSY)
472 return &pci_compat_driver;
473 }
474 return NULL;
475}
476
477/**
478 * pci_bus_match - Tell if a PCI device structure has a matching PCI device id structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 * @dev: the PCI device structure to match against
Randy Dunlap8f7020d2005-10-23 11:57:38 -0700480 * @drv: the device driver to search for matching PCI device id structures
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 *
482 * Used by a driver to check whether a PCI device present in the
Randy Dunlap8f7020d2005-10-23 11:57:38 -0700483 * system is in its list of supported devices. Returns the matching
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 * pci_device_id structure or %NULL if there is no match.
485 */
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700486static int pci_bus_match(struct device *dev, struct device_driver *drv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487{
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700488 struct pci_dev *pci_dev = to_pci_dev(dev);
489 struct pci_driver *pci_drv = to_pci_driver(drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 const struct pci_device_id *found_id;
491
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700492 found_id = pci_match_device(pci_drv, pci_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 if (found_id)
494 return 1;
495
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700496 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497}
498
499/**
500 * pci_dev_get - increments the reference count of the pci device structure
501 * @dev: the device being referenced
502 *
503 * Each live reference to a device should be refcounted.
504 *
505 * Drivers for PCI devices should normally record such references in
506 * their probe() methods, when they bind to a device, and release
507 * them by calling pci_dev_put(), in their disconnect() methods.
508 *
509 * A pointer to the device with the incremented reference counter is returned.
510 */
511struct pci_dev *pci_dev_get(struct pci_dev *dev)
512{
513 if (dev)
514 get_device(&dev->dev);
515 return dev;
516}
517
518/**
519 * pci_dev_put - release a use of the pci device structure
520 * @dev: device that's been disconnected
521 *
522 * Must be called when a user of a device is finished with it. When the last
523 * user of the device calls this function, the memory of the device is freed.
524 */
525void pci_dev_put(struct pci_dev *dev)
526{
527 if (dev)
528 put_device(&dev->dev);
529}
530
531#ifndef CONFIG_HOTPLUG
Kay Sievers312c0042005-11-16 09:00:00 +0100532int pci_uevent(struct device *dev, char **envp, int num_envp,
533 char *buffer, int buffer_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534{
535 return -ENODEV;
536}
537#endif
538
539struct bus_type pci_bus_type = {
540 .name = "pci",
541 .match = pci_bus_match,
Kay Sievers312c0042005-11-16 09:00:00 +0100542 .uevent = pci_uevent,
Russell Kingb15d6862006-01-05 14:30:22 +0000543 .probe = pci_device_probe,
544 .remove = pci_device_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 .suspend = pci_device_suspend,
Linus Torvaldscbd69db2006-06-24 14:50:29 -0700546 .suspend_late = pci_device_suspend_late,
547 .resume_early = pci_device_resume_early,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 .resume = pci_device_resume,
Linus Torvaldscbd69db2006-06-24 14:50:29 -0700549 .shutdown = pci_device_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 .dev_attrs = pci_dev_attrs,
551};
552
553static int __init pci_driver_init(void)
554{
555 return bus_register(&pci_bus_type);
556}
557
558postcore_initcall(pci_driver_init);
559
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700560EXPORT_SYMBOL(pci_match_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561EXPORT_SYMBOL(pci_match_device);
Laurent riffard863b18f2005-10-27 23:12:54 +0200562EXPORT_SYMBOL(__pci_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563EXPORT_SYMBOL(pci_unregister_driver);
564EXPORT_SYMBOL(pci_dev_driver);
565EXPORT_SYMBOL(pci_bus_type);
566EXPORT_SYMBOL(pci_dev_get);
567EXPORT_SYMBOL(pci_dev_put);