uml: activate_mm: remove the dead PF_BORROWED_MM check
[linux-2.6/linux-loongson.git] / drivers / pci / pci-driver.c
blob72cf61ed8f966d20f99c32d6acd77424e419ee69
1 /*
2 * drivers/pci/pci-driver.c
4 * (C) Copyright 2002-2004, 2007 Greg Kroah-Hartman <greg@kroah.com>
5 * (C) Copyright 2007 Novell Inc.
7 * Released under the GPL v2 only.
9 */
11 #include <linux/pci.h>
12 #include <linux/module.h>
13 #include <linux/init.h>
14 #include <linux/device.h>
15 #include <linux/mempolicy.h>
16 #include <linux/string.h>
17 #include <linux/slab.h>
18 #include <linux/sched.h>
19 #include "pci.h"
22 * Dynamic device IDs are disabled for !CONFIG_HOTPLUG
25 struct pci_dynid {
26 struct list_head node;
27 struct pci_device_id id;
30 #ifdef CONFIG_HOTPLUG
32 /**
33 * 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
38 * Adds a new dynamic pci device ID to this driver,
39 * and causes the driver to probe for all devices again.
41 static ssize_t
42 store_new_id(struct device_driver *driver, const char *buf, size_t count)
44 struct pci_dynid *dynid;
45 struct pci_driver *pdrv = to_pci_driver(driver);
46 __u32 vendor, device, subvendor=PCI_ANY_ID,
47 subdevice=PCI_ANY_ID, class=0, class_mask=0;
48 unsigned long driver_data=0;
49 int fields=0;
50 int retval = 0;
52 fields = sscanf(buf, "%x %x %x %x %x %x %lux",
53 &vendor, &device, &subvendor, &subdevice,
54 &class, &class_mask, &driver_data);
55 if (fields < 2)
56 return -EINVAL;
58 dynid = kzalloc(sizeof(*dynid), GFP_KERNEL);
59 if (!dynid)
60 return -ENOMEM;
62 dynid->id.vendor = vendor;
63 dynid->id.device = device;
64 dynid->id.subvendor = subvendor;
65 dynid->id.subdevice = subdevice;
66 dynid->id.class = class;
67 dynid->id.class_mask = class_mask;
68 dynid->id.driver_data = pdrv->dynids.use_driver_data ?
69 driver_data : 0UL;
71 spin_lock(&pdrv->dynids.lock);
72 list_add_tail(&dynid->node, &pdrv->dynids.list);
73 spin_unlock(&pdrv->dynids.lock);
75 if (get_driver(&pdrv->driver)) {
76 retval = driver_attach(&pdrv->driver);
77 put_driver(&pdrv->driver);
80 if (retval)
81 return retval;
82 return count;
84 static DRIVER_ATTR(new_id, S_IWUSR, NULL, store_new_id);
86 static void
87 pci_free_dynids(struct pci_driver *drv)
89 struct pci_dynid *dynid, *n;
91 spin_lock(&drv->dynids.lock);
92 list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) {
93 list_del(&dynid->node);
94 kfree(dynid);
96 spin_unlock(&drv->dynids.lock);
99 static int
100 pci_create_newid_file(struct pci_driver *drv)
102 int error = 0;
103 if (drv->probe != NULL)
104 error = driver_create_file(&drv->driver, &driver_attr_new_id);
105 return error;
108 static void pci_remove_newid_file(struct pci_driver *drv)
110 driver_remove_file(&drv->driver, &driver_attr_new_id);
112 #else /* !CONFIG_HOTPLUG */
113 static inline void pci_free_dynids(struct pci_driver *drv) {}
114 static inline int pci_create_newid_file(struct pci_driver *drv)
116 return 0;
118 static inline void pci_remove_newid_file(struct pci_driver *drv) {}
119 #endif
122 * pci_match_id - See if a pci device matches a given pci_id table
123 * @ids: array of PCI device id structures to search in
124 * @dev: the PCI device structure to match against.
126 * Used by a driver to check whether a PCI device present in the
127 * system is in its list of supported devices. Returns the matching
128 * pci_device_id structure or %NULL if there is no match.
130 * Deprecated, don't use this as it will not catch any dynamic ids
131 * that a driver might want to check for.
133 const struct pci_device_id *pci_match_id(const struct pci_device_id *ids,
134 struct pci_dev *dev)
136 if (ids) {
137 while (ids->vendor || ids->subvendor || ids->class_mask) {
138 if (pci_match_one_device(ids, dev))
139 return ids;
140 ids++;
143 return NULL;
147 * pci_match_device - Tell if a PCI device structure has a matching PCI device id structure
148 * @drv: the PCI driver to match against
149 * @dev: the PCI device structure to match against
151 * Used by a driver to check whether a PCI device present in the
152 * system is in its list of supported devices. Returns the matching
153 * pci_device_id structure or %NULL if there is no match.
155 static const struct pci_device_id *pci_match_device(struct pci_driver *drv,
156 struct pci_dev *dev)
158 struct pci_dynid *dynid;
160 /* Look at the dynamic ids first, before the static ones */
161 spin_lock(&drv->dynids.lock);
162 list_for_each_entry(dynid, &drv->dynids.list, node) {
163 if (pci_match_one_device(&dynid->id, dev)) {
164 spin_unlock(&drv->dynids.lock);
165 return &dynid->id;
168 spin_unlock(&drv->dynids.lock);
170 return pci_match_id(drv->id_table, dev);
173 static int pci_call_probe(struct pci_driver *drv, struct pci_dev *dev,
174 const struct pci_device_id *id)
176 int error;
177 #ifdef CONFIG_NUMA
178 /* Execute driver initialization on node where the
179 device's bus is attached to. This way the driver likely
180 allocates its local memory on the right node without
181 any need to change it. */
182 struct mempolicy *oldpol;
183 cpumask_t oldmask = current->cpus_allowed;
184 int node = pcibus_to_node(dev->bus);
186 if (node >= 0) {
187 node_to_cpumask_ptr(nodecpumask, node);
188 set_cpus_allowed_ptr(current, nodecpumask);
190 /* And set default memory allocation policy */
191 oldpol = current->mempolicy;
192 current->mempolicy = NULL; /* fall back to system default policy */
193 #endif
194 error = drv->probe(dev, id);
195 #ifdef CONFIG_NUMA
196 set_cpus_allowed_ptr(current, &oldmask);
197 current->mempolicy = oldpol;
198 #endif
199 return error;
203 * __pci_device_probe()
204 * @drv: driver to call to check if it wants the PCI device
205 * @pci_dev: PCI device being probed
207 * returns 0 on success, else error.
208 * side-effect: pci_dev->driver is set to drv when drv claims pci_dev.
210 static int
211 __pci_device_probe(struct pci_driver *drv, struct pci_dev *pci_dev)
213 const struct pci_device_id *id;
214 int error = 0;
216 if (!pci_dev->driver && drv->probe) {
217 error = -ENODEV;
219 id = pci_match_device(drv, pci_dev);
220 if (id)
221 error = pci_call_probe(drv, pci_dev, id);
222 if (error >= 0) {
223 pci_dev->driver = drv;
224 error = 0;
227 return error;
230 static int pci_device_probe(struct device * dev)
232 int error = 0;
233 struct pci_driver *drv;
234 struct pci_dev *pci_dev;
236 drv = to_pci_driver(dev->driver);
237 pci_dev = to_pci_dev(dev);
238 pci_dev_get(pci_dev);
239 error = __pci_device_probe(drv, pci_dev);
240 if (error)
241 pci_dev_put(pci_dev);
243 return error;
246 static int pci_device_remove(struct device * dev)
248 struct pci_dev * pci_dev = to_pci_dev(dev);
249 struct pci_driver * drv = pci_dev->driver;
251 if (drv) {
252 if (drv->remove)
253 drv->remove(pci_dev);
254 pci_dev->driver = NULL;
258 * If the device is still on, set the power state as "unknown",
259 * since it might change by the next time we load the driver.
261 if (pci_dev->current_state == PCI_D0)
262 pci_dev->current_state = PCI_UNKNOWN;
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...
273 pci_dev_put(pci_dev);
274 return 0;
277 static int pci_device_suspend(struct device * dev, pm_message_t state)
279 struct pci_dev * pci_dev = to_pci_dev(dev);
280 struct pci_driver * drv = pci_dev->driver;
281 int i = 0;
283 if (drv && drv->suspend) {
284 i = drv->suspend(pci_dev, state);
285 suspend_report_result(drv->suspend, i);
286 } else {
287 pci_save_state(pci_dev);
289 * mark its power state as "unknown", since we don't know if
290 * e.g. the BIOS will change its device state when we suspend.
292 if (pci_dev->current_state == PCI_D0)
293 pci_dev->current_state = PCI_UNKNOWN;
295 return i;
298 static int pci_device_suspend_late(struct device * dev, pm_message_t state)
300 struct pci_dev * pci_dev = to_pci_dev(dev);
301 struct pci_driver * drv = pci_dev->driver;
302 int i = 0;
304 if (drv && drv->suspend_late) {
305 i = drv->suspend_late(pci_dev, state);
306 suspend_report_result(drv->suspend_late, i);
308 return i;
312 * Default resume method for devices that have no driver provided resume,
313 * or not even a driver at all.
315 static int pci_default_resume(struct pci_dev *pci_dev)
317 int retval = 0;
319 /* restore the PCI config space */
320 pci_restore_state(pci_dev);
321 /* if the device was enabled before suspend, reenable */
322 retval = pci_reenable_device(pci_dev);
323 /* if the device was busmaster before the suspend, make it busmaster again */
324 if (pci_dev->is_busmaster)
325 pci_set_master(pci_dev);
327 return retval;
330 static int pci_device_resume(struct device * dev)
332 int error;
333 struct pci_dev * pci_dev = to_pci_dev(dev);
334 struct pci_driver * drv = pci_dev->driver;
336 if (drv && drv->resume)
337 error = drv->resume(pci_dev);
338 else
339 error = pci_default_resume(pci_dev);
340 return error;
343 static int pci_device_resume_early(struct device * dev)
345 int error = 0;
346 struct pci_dev * pci_dev = to_pci_dev(dev);
347 struct pci_driver * drv = pci_dev->driver;
349 pci_fixup_device(pci_fixup_resume, pci_dev);
351 if (drv && drv->resume_early)
352 error = drv->resume_early(pci_dev);
353 return error;
356 static void pci_device_shutdown(struct device *dev)
358 struct pci_dev *pci_dev = to_pci_dev(dev);
359 struct pci_driver *drv = pci_dev->driver;
361 if (drv && drv->shutdown)
362 drv->shutdown(pci_dev);
363 pci_msi_shutdown(pci_dev);
364 pci_msix_shutdown(pci_dev);
368 * __pci_register_driver - register a new pci driver
369 * @drv: the driver structure to register
370 * @owner: owner module of drv
371 * @mod_name: module name string
373 * Adds the driver structure to the list of registered drivers.
374 * Returns a negative value on error, otherwise 0.
375 * If no error occurred, the driver remains registered even if
376 * no device was claimed during registration.
378 int __pci_register_driver(struct pci_driver *drv, struct module *owner,
379 const char *mod_name)
381 int error;
383 /* initialize common driver fields */
384 drv->driver.name = drv->name;
385 drv->driver.bus = &pci_bus_type;
386 drv->driver.owner = owner;
387 drv->driver.mod_name = mod_name;
389 spin_lock_init(&drv->dynids.lock);
390 INIT_LIST_HEAD(&drv->dynids.list);
392 /* register with core */
393 error = driver_register(&drv->driver);
394 if (error)
395 return error;
397 error = pci_create_newid_file(drv);
398 if (error)
399 driver_unregister(&drv->driver);
401 return error;
405 * pci_unregister_driver - unregister a pci driver
406 * @drv: the driver structure to unregister
408 * Deletes the driver structure from the list of registered PCI drivers,
409 * gives it a chance to clean up by calling its remove() function for
410 * each device it was responsible for, and marks those devices as
411 * driverless.
414 void
415 pci_unregister_driver(struct pci_driver *drv)
417 pci_remove_newid_file(drv);
418 driver_unregister(&drv->driver);
419 pci_free_dynids(drv);
422 static struct pci_driver pci_compat_driver = {
423 .name = "compat"
427 * pci_dev_driver - get the pci_driver of a device
428 * @dev: the device to query
430 * Returns the appropriate pci_driver structure or %NULL if there is no
431 * registered driver for the device.
433 struct pci_driver *
434 pci_dev_driver(const struct pci_dev *dev)
436 if (dev->driver)
437 return dev->driver;
438 else {
439 int i;
440 for(i=0; i<=PCI_ROM_RESOURCE; i++)
441 if (dev->resource[i].flags & IORESOURCE_BUSY)
442 return &pci_compat_driver;
444 return NULL;
448 * pci_bus_match - Tell if a PCI device structure has a matching PCI device id structure
449 * @dev: the PCI device structure to match against
450 * @drv: the device driver to search for matching PCI device id structures
452 * Used by a driver to check whether a PCI device present in the
453 * system is in its list of supported devices. Returns the matching
454 * pci_device_id structure or %NULL if there is no match.
456 static int pci_bus_match(struct device *dev, struct device_driver *drv)
458 struct pci_dev *pci_dev = to_pci_dev(dev);
459 struct pci_driver *pci_drv = to_pci_driver(drv);
460 const struct pci_device_id *found_id;
462 found_id = pci_match_device(pci_drv, pci_dev);
463 if (found_id)
464 return 1;
466 return 0;
470 * pci_dev_get - increments the reference count of the pci device structure
471 * @dev: the device being referenced
473 * Each live reference to a device should be refcounted.
475 * Drivers for PCI devices should normally record such references in
476 * their probe() methods, when they bind to a device, and release
477 * them by calling pci_dev_put(), in their disconnect() methods.
479 * A pointer to the device with the incremented reference counter is returned.
481 struct pci_dev *pci_dev_get(struct pci_dev *dev)
483 if (dev)
484 get_device(&dev->dev);
485 return dev;
489 * pci_dev_put - release a use of the pci device structure
490 * @dev: device that's been disconnected
492 * Must be called when a user of a device is finished with it. When the last
493 * user of the device calls this function, the memory of the device is freed.
495 void pci_dev_put(struct pci_dev *dev)
497 if (dev)
498 put_device(&dev->dev);
501 #ifndef CONFIG_HOTPLUG
502 int pci_uevent(struct device *dev, struct kobj_uevent_env *env)
504 return -ENODEV;
506 #endif
508 struct bus_type pci_bus_type = {
509 .name = "pci",
510 .match = pci_bus_match,
511 .uevent = pci_uevent,
512 .probe = pci_device_probe,
513 .remove = pci_device_remove,
514 .suspend = pci_device_suspend,
515 .suspend_late = pci_device_suspend_late,
516 .resume_early = pci_device_resume_early,
517 .resume = pci_device_resume,
518 .shutdown = pci_device_shutdown,
519 .dev_attrs = pci_dev_attrs,
522 static int __init pci_driver_init(void)
524 return bus_register(&pci_bus_type);
527 postcore_initcall(pci_driver_init);
529 EXPORT_SYMBOL(pci_match_id);
530 EXPORT_SYMBOL(__pci_register_driver);
531 EXPORT_SYMBOL(pci_unregister_driver);
532 EXPORT_SYMBOL(pci_dev_driver);
533 EXPORT_SYMBOL(pci_bus_type);
534 EXPORT_SYMBOL(pci_dev_get);
535 EXPORT_SYMBOL(pci_dev_put);