[MIPS] Use common definitions from asm-generic/signal.h
[linux-2.6/linux-mips/linux-dm7025.git] / drivers / pci / pci-driver.c
blobd8ace1f90dd2a13f864bb2f87b649377c11d4c14
1 /*
2 * drivers/pci/pci-driver.c
4 */
6 #include <linux/pci.h>
7 #include <linux/module.h>
8 #include <linux/init.h>
9 #include <linux/device.h>
10 #include <linux/mempolicy.h>
11 #include <linux/string.h>
12 #include <linux/slab.h>
13 #include <linux/sched.h>
14 #include "pci.h"
17 * Registration of PCI drivers and handling of hot-pluggable devices.
20 /* multithreaded probe logic */
21 static int pci_multithread_probe =
22 #ifdef CONFIG_PCI_MULTITHREAD_PROBE
24 #else
26 #endif
27 __module_param_call("", pci_multithread_probe, param_set_bool, param_get_bool, &pci_multithread_probe, 0644);
31 * Dynamic device IDs are disabled for !CONFIG_HOTPLUG
34 struct pci_dynid {
35 struct list_head node;
36 struct pci_device_id id;
39 #ifdef CONFIG_HOTPLUG
41 /**
42 * 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
47 * Adds a new dynamic pci device ID to this driver,
48 * and causes the driver to probe for all devices again.
50 static ssize_t
51 store_new_id(struct device_driver *driver, const char *buf, size_t count)
53 struct pci_dynid *dynid;
54 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;
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;
66 dynid = kzalloc(sizeof(*dynid), GFP_KERNEL);
67 if (!dynid)
68 return -ENOMEM;
70 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;
80 spin_lock(&pdrv->dynids.lock);
81 list_add_tail(&pdrv->dynids.list, &dynid->node);
82 spin_unlock(&pdrv->dynids.lock);
84 if (get_driver(&pdrv->driver)) {
85 driver_attach(&pdrv->driver);
86 put_driver(&pdrv->driver);
89 return count;
91 static DRIVER_ATTR(new_id, S_IWUSR, NULL, store_new_id);
93 static void
94 pci_free_dynids(struct pci_driver *drv)
96 struct pci_dynid *dynid, *n;
98 spin_lock(&drv->dynids.lock);
99 list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) {
100 list_del(&dynid->node);
101 kfree(dynid);
103 spin_unlock(&drv->dynids.lock);
106 static int
107 pci_create_newid_file(struct pci_driver *drv)
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;
116 #else /* !CONFIG_HOTPLUG */
117 static inline void pci_free_dynids(struct pci_driver *drv) {}
118 static inline int pci_create_newid_file(struct pci_driver *drv)
120 return 0;
122 #endif
125 * pci_match_id - See if a pci device matches a given pci_id table
126 * @ids: array of PCI device id structures to search in
127 * @dev: the PCI device structure to match against.
129 * Used by a driver to check whether a PCI device present in the
130 * system is in its list of supported devices. Returns the matching
131 * pci_device_id structure or %NULL if there is no match.
133 * Depreciated, don't use this as it will not catch any dynamic ids
134 * that a driver might want to check for.
136 const struct pci_device_id *pci_match_id(const struct pci_device_id *ids,
137 struct pci_dev *dev)
139 if (ids) {
140 while (ids->vendor || ids->subvendor || ids->class_mask) {
141 if (pci_match_one_device(ids, dev))
142 return ids;
143 ids++;
146 return NULL;
150 * pci_match_device - Tell if a PCI device structure has a matching
151 * PCI device id structure
152 * @drv: the PCI driver to match against
153 * @dev: the PCI device structure to match against
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.
159 const struct pci_device_id *pci_match_device(struct pci_driver *drv,
160 struct pci_dev *dev)
162 const struct pci_device_id *id;
163 struct pci_dynid *dynid;
165 id = pci_match_id(drv->id_table, dev);
166 if (id)
167 return id;
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;
177 spin_unlock(&drv->dynids.lock);
178 return NULL;
181 static int pci_call_probe(struct pci_driver *drv, struct pci_dev *dev,
182 const struct pci_device_id *id)
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;
210 * __pci_device_probe()
211 * @drv: driver to call to check if it wants the PCI device
212 * @pci_dev: PCI device being probed
214 * returns 0 on success, else error.
215 * side-effect: pci_dev->driver is set to drv when drv claims pci_dev.
217 static int
218 __pci_device_probe(struct pci_driver *drv, struct pci_dev *pci_dev)
220 const struct pci_device_id *id;
221 int error = 0;
223 if (!pci_dev->driver && drv->probe) {
224 error = -ENODEV;
226 id = pci_match_device(drv, pci_dev);
227 if (id)
228 error = pci_call_probe(drv, pci_dev, id);
229 if (error >= 0) {
230 pci_dev->driver = drv;
231 error = 0;
234 return error;
237 static int pci_device_probe(struct device * dev)
239 int error = 0;
240 struct pci_driver *drv;
241 struct pci_dev *pci_dev;
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);
250 return error;
253 static int pci_device_remove(struct device * dev)
255 struct pci_dev * pci_dev = to_pci_dev(dev);
256 struct pci_driver * drv = pci_dev->driver;
258 if (drv) {
259 if (drv->remove)
260 drv->remove(pci_dev);
261 pci_dev->driver = NULL;
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 return i;
292 static int pci_device_suspend_late(struct device * dev, pm_message_t state)
294 struct pci_dev * pci_dev = to_pci_dev(dev);
295 struct pci_driver * drv = pci_dev->driver;
296 int i = 0;
298 if (drv && drv->suspend_late) {
299 i = drv->suspend_late(pci_dev, state);
300 suspend_report_result(drv->suspend_late, i);
302 return i;
306 * Default resume method for devices that have no driver provided resume,
307 * or not even a driver at all.
309 static int pci_default_resume(struct pci_dev *pci_dev)
311 int retval = 0;
313 /* 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)
317 retval = pci_enable_device(pci_dev);
318 /* if the device was busmaster before the suspend, make it busmaster again */
319 if (pci_dev->is_busmaster)
320 pci_set_master(pci_dev);
322 return retval;
325 static int pci_device_resume(struct device * dev)
327 int error;
328 struct pci_dev * pci_dev = to_pci_dev(dev);
329 struct pci_driver * drv = pci_dev->driver;
331 if (drv && drv->resume)
332 error = drv->resume(pci_dev);
333 else
334 error = pci_default_resume(pci_dev);
335 return error;
338 static int pci_device_resume_early(struct device * dev)
340 int error = 0;
341 struct pci_dev * pci_dev = to_pci_dev(dev);
342 struct pci_driver * drv = pci_dev->driver;
344 if (drv && drv->resume_early)
345 error = drv->resume_early(pci_dev);
346 return error;
349 static void pci_device_shutdown(struct device *dev)
351 struct pci_dev *pci_dev = to_pci_dev(dev);
352 struct pci_driver *drv = pci_dev->driver;
354 if (drv && drv->shutdown)
355 drv->shutdown(pci_dev);
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)
361 static ssize_t
362 pci_driver_attr_show(struct kobject * kobj, struct attribute *attr, char *buf)
364 struct device_driver *driver = kobj_to_pci_driver(kobj);
365 struct driver_attribute *dattr = attr_to_driver_attribute(attr);
366 ssize_t ret;
368 if (!get_driver(driver))
369 return -ENODEV;
371 ret = dattr->show ? dattr->show(driver, buf) : -EIO;
373 put_driver(driver);
374 return ret;
377 static ssize_t
378 pci_driver_attr_store(struct kobject * kobj, struct attribute *attr,
379 const char *buf, size_t count)
381 struct device_driver *driver = kobj_to_pci_driver(kobj);
382 struct driver_attribute *dattr = attr_to_driver_attribute(attr);
383 ssize_t ret;
385 if (!get_driver(driver))
386 return -ENODEV;
388 ret = dattr->store ? dattr->store(driver, buf, count) : -EIO;
390 put_driver(driver);
391 return ret;
394 static struct sysfs_ops pci_driver_sysfs_ops = {
395 .show = pci_driver_attr_show,
396 .store = pci_driver_attr_store,
398 static struct kobj_type pci_driver_kobj_type = {
399 .sysfs_ops = &pci_driver_sysfs_ops,
403 * __pci_register_driver - register a new pci driver
404 * @drv: the driver structure to register
405 * @owner: owner module of drv
407 * Adds the driver structure to the list of registered drivers.
408 * Returns a negative value on error, otherwise 0.
409 * If no error occurred, the driver remains registered even if
410 * no device was claimed during registration.
412 int __pci_register_driver(struct pci_driver *drv, struct module *owner)
414 int error;
416 /* initialize common driver fields */
417 drv->driver.name = drv->name;
418 drv->driver.bus = &pci_bus_type;
419 drv->driver.owner = owner;
420 drv->driver.kobj.ktype = &pci_driver_kobj_type;
421 drv->driver.multithread_probe = pci_multithread_probe;
423 spin_lock_init(&drv->dynids.lock);
424 INIT_LIST_HEAD(&drv->dynids.list);
426 /* register with core */
427 error = driver_register(&drv->driver);
429 if (!error)
430 error = pci_create_newid_file(drv);
432 return error;
436 * pci_unregister_driver - unregister a pci driver
437 * @drv: the driver structure to unregister
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.
445 void
446 pci_unregister_driver(struct pci_driver *drv)
448 driver_unregister(&drv->driver);
449 pci_free_dynids(drv);
452 static struct pci_driver pci_compat_driver = {
453 .name = "compat"
457 * pci_dev_driver - get the pci_driver of a device
458 * @dev: the device to query
460 * Returns the appropriate pci_driver structure or %NULL if there is no
461 * registered driver for the device.
463 struct pci_driver *
464 pci_dev_driver(const struct pci_dev *dev)
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;
474 return NULL;
478 * pci_bus_match - Tell if a PCI device structure has a matching PCI device id structure
479 * @dev: the PCI device structure to match against
480 * @drv: the device driver to search for matching PCI device id structures
482 * Used by a driver to check whether a PCI device present in the
483 * system is in its list of supported devices. Returns the matching
484 * pci_device_id structure or %NULL if there is no match.
486 static int pci_bus_match(struct device *dev, struct device_driver *drv)
488 struct pci_dev *pci_dev = to_pci_dev(dev);
489 struct pci_driver *pci_drv = to_pci_driver(drv);
490 const struct pci_device_id *found_id;
492 found_id = pci_match_device(pci_drv, pci_dev);
493 if (found_id)
494 return 1;
496 return 0;
500 * pci_dev_get - increments the reference count of the pci device structure
501 * @dev: the device being referenced
503 * Each live reference to a device should be refcounted.
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.
509 * A pointer to the device with the incremented reference counter is returned.
511 struct pci_dev *pci_dev_get(struct pci_dev *dev)
513 if (dev)
514 get_device(&dev->dev);
515 return dev;
519 * pci_dev_put - release a use of the pci device structure
520 * @dev: device that's been disconnected
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.
525 void pci_dev_put(struct pci_dev *dev)
527 if (dev)
528 put_device(&dev->dev);
531 #ifndef CONFIG_HOTPLUG
532 int pci_uevent(struct device *dev, char **envp, int num_envp,
533 char *buffer, int buffer_size)
535 return -ENODEV;
537 #endif
539 struct bus_type pci_bus_type = {
540 .name = "pci",
541 .match = pci_bus_match,
542 .uevent = pci_uevent,
543 .probe = pci_device_probe,
544 .remove = pci_device_remove,
545 .suspend = pci_device_suspend,
546 .suspend_late = pci_device_suspend_late,
547 .resume_early = pci_device_resume_early,
548 .resume = pci_device_resume,
549 .shutdown = pci_device_shutdown,
550 .dev_attrs = pci_dev_attrs,
553 static int __init pci_driver_init(void)
555 return bus_register(&pci_bus_type);
558 postcore_initcall(pci_driver_init);
560 EXPORT_SYMBOL(pci_match_id);
561 EXPORT_SYMBOL(pci_match_device);
562 EXPORT_SYMBOL(__pci_register_driver);
563 EXPORT_SYMBOL(pci_unregister_driver);
564 EXPORT_SYMBOL(pci_dev_driver);
565 EXPORT_SYMBOL(pci_bus_type);
566 EXPORT_SYMBOL(pci_dev_get);
567 EXPORT_SYMBOL(pci_dev_put);