/spare/repo/netdev-2.6 branch 'ieee80211'
[linux-2.6/verdex.git] / drivers / pci / pci-driver.c
blobe4115a0d5ba6152852e43840eb56f702481d8bac
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 "pci.h"
13 * Registration of PCI drivers and handling of hot-pluggable devices.
17 * Dynamic device IDs are disabled for !CONFIG_HOTPLUG
20 struct pci_dynid {
21 struct list_head node;
22 struct pci_device_id id;
25 #ifdef CONFIG_HOTPLUG
27 /**
28 * store_new_id
30 * Adds a new dynamic pci device ID to this driver,
31 * and causes the driver to probe for all devices again.
33 static inline ssize_t
34 store_new_id(struct device_driver *driver, const char *buf, size_t count)
36 struct pci_dynid *dynid;
37 struct pci_driver *pdrv = to_pci_driver(driver);
38 __u32 vendor=PCI_ANY_ID, device=PCI_ANY_ID, subvendor=PCI_ANY_ID,
39 subdevice=PCI_ANY_ID, class=0, class_mask=0;
40 unsigned long driver_data=0;
41 int fields=0;
43 fields = sscanf(buf, "%x %x %x %x %x %x %lux",
44 &vendor, &device, &subvendor, &subdevice,
45 &class, &class_mask, &driver_data);
46 if (fields < 0)
47 return -EINVAL;
49 dynid = kmalloc(sizeof(*dynid), GFP_KERNEL);
50 if (!dynid)
51 return -ENOMEM;
53 memset(dynid, 0, sizeof(*dynid));
54 INIT_LIST_HEAD(&dynid->node);
55 dynid->id.vendor = vendor;
56 dynid->id.device = device;
57 dynid->id.subvendor = subvendor;
58 dynid->id.subdevice = subdevice;
59 dynid->id.class = class;
60 dynid->id.class_mask = class_mask;
61 dynid->id.driver_data = pdrv->dynids.use_driver_data ?
62 driver_data : 0UL;
64 spin_lock(&pdrv->dynids.lock);
65 list_add_tail(&pdrv->dynids.list, &dynid->node);
66 spin_unlock(&pdrv->dynids.lock);
68 if (get_driver(&pdrv->driver)) {
69 driver_attach(&pdrv->driver);
70 put_driver(&pdrv->driver);
73 return count;
75 static DRIVER_ATTR(new_id, S_IWUSR, NULL, store_new_id);
77 static void
78 pci_free_dynids(struct pci_driver *drv)
80 struct pci_dynid *dynid, *n;
82 spin_lock(&drv->dynids.lock);
83 list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) {
84 list_del(&dynid->node);
85 kfree(dynid);
87 spin_unlock(&drv->dynids.lock);
90 static int
91 pci_create_newid_file(struct pci_driver *drv)
93 int error = 0;
94 if (drv->probe != NULL)
95 error = sysfs_create_file(&drv->driver.kobj,
96 &driver_attr_new_id.attr);
97 return error;
100 #else /* !CONFIG_HOTPLUG */
101 static inline void pci_free_dynids(struct pci_driver *drv) {}
102 static inline int pci_create_newid_file(struct pci_driver *drv)
104 return 0;
106 #endif
109 * pci_match_id - See if a pci device matches a given pci_id table
110 * @ids: array of PCI device id structures to search in
111 * @dev: the PCI device structure to match against.
113 * Used by a driver to check whether a PCI device present in the
114 * system is in its list of supported devices. Returns the matching
115 * pci_device_id structure or %NULL if there is no match.
117 * Depreciated, don't use this as it will not catch any dynamic ids
118 * that a driver might want to check for.
120 const struct pci_device_id *pci_match_id(const struct pci_device_id *ids,
121 struct pci_dev *dev)
123 if (ids) {
124 while (ids->vendor || ids->subvendor || ids->class_mask) {
125 if (pci_match_one_device(ids, dev))
126 return ids;
127 ids++;
130 return NULL;
134 * pci_match_device - Tell if a PCI device structure has a matching
135 * PCI device id structure
136 * @ids: array of PCI device id structures to search in
137 * @dev: the PCI device structure to match against
138 * @drv: the PCI driver to match against
140 * Used by a driver to check whether a PCI device present in the
141 * system is in its list of supported devices. Returns the matching
142 * pci_device_id structure or %NULL if there is no match.
144 const struct pci_device_id *pci_match_device(struct pci_driver *drv,
145 struct pci_dev *dev)
147 const struct pci_device_id *id;
148 struct pci_dynid *dynid;
150 id = pci_match_id(drv->id_table, dev);
151 if (id)
152 return id;
154 /* static ids didn't match, lets look at the dynamic ones */
155 spin_lock(&drv->dynids.lock);
156 list_for_each_entry(dynid, &drv->dynids.list, node) {
157 if (pci_match_one_device(&dynid->id, dev)) {
158 spin_unlock(&drv->dynids.lock);
159 return &dynid->id;
162 spin_unlock(&drv->dynids.lock);
163 return NULL;
167 * __pci_device_probe()
169 * returns 0 on success, else error.
170 * side-effect: pci_dev->driver is set to drv when drv claims pci_dev.
172 static int
173 __pci_device_probe(struct pci_driver *drv, struct pci_dev *pci_dev)
175 const struct pci_device_id *id;
176 int error = 0;
178 if (!pci_dev->driver && drv->probe) {
179 error = -ENODEV;
181 id = pci_match_device(drv, pci_dev);
182 if (id)
183 error = drv->probe(pci_dev, id);
184 if (error >= 0) {
185 pci_dev->driver = drv;
186 error = 0;
189 return error;
192 static int pci_device_probe(struct device * dev)
194 int error = 0;
195 struct pci_driver *drv;
196 struct pci_dev *pci_dev;
198 drv = to_pci_driver(dev->driver);
199 pci_dev = to_pci_dev(dev);
200 pci_dev_get(pci_dev);
201 error = __pci_device_probe(drv, pci_dev);
202 if (error)
203 pci_dev_put(pci_dev);
205 return error;
208 static int pci_device_remove(struct device * dev)
210 struct pci_dev * pci_dev = to_pci_dev(dev);
211 struct pci_driver * drv = pci_dev->driver;
213 if (drv) {
214 if (drv->remove)
215 drv->remove(pci_dev);
216 pci_dev->driver = NULL;
220 * We would love to complain here if pci_dev->is_enabled is set, that
221 * the driver should have called pci_disable_device(), but the
222 * unfortunate fact is there are too many odd BIOS and bridge setups
223 * that don't like drivers doing that all of the time.
224 * Oh well, we can dream of sane hardware when we sleep, no matter how
225 * horrible the crap we have to deal with is when we are awake...
228 pci_dev_put(pci_dev);
229 return 0;
232 static int pci_device_suspend(struct device * dev, pm_message_t state)
234 struct pci_dev * pci_dev = to_pci_dev(dev);
235 struct pci_driver * drv = pci_dev->driver;
236 int i = 0;
238 if (drv && drv->suspend)
239 i = drv->suspend(pci_dev, state);
240 else
241 pci_save_state(pci_dev);
242 return i;
247 * Default resume method for devices that have no driver provided resume,
248 * or not even a driver at all.
250 static void pci_default_resume(struct pci_dev *pci_dev)
252 /* restore the PCI config space */
253 pci_restore_state(pci_dev);
254 /* if the device was enabled before suspend, reenable */
255 if (pci_dev->is_enabled)
256 pci_enable_device(pci_dev);
257 /* if the device was busmaster before the suspend, make it busmaster again */
258 if (pci_dev->is_busmaster)
259 pci_set_master(pci_dev);
262 static int pci_device_resume(struct device * dev)
264 struct pci_dev * pci_dev = to_pci_dev(dev);
265 struct pci_driver * drv = pci_dev->driver;
267 if (drv && drv->resume)
268 drv->resume(pci_dev);
269 else
270 pci_default_resume(pci_dev);
271 return 0;
274 static void pci_device_shutdown(struct device *dev)
276 struct pci_dev *pci_dev = to_pci_dev(dev);
277 struct pci_driver *drv = pci_dev->driver;
279 if (drv && drv->shutdown)
280 drv->shutdown(pci_dev);
283 #define kobj_to_pci_driver(obj) container_of(obj, struct device_driver, kobj)
284 #define attr_to_driver_attribute(obj) container_of(obj, struct driver_attribute, attr)
286 static ssize_t
287 pci_driver_attr_show(struct kobject * kobj, struct attribute *attr, char *buf)
289 struct device_driver *driver = kobj_to_pci_driver(kobj);
290 struct driver_attribute *dattr = attr_to_driver_attribute(attr);
291 ssize_t ret;
293 if (!get_driver(driver))
294 return -ENODEV;
296 ret = dattr->show ? dattr->show(driver, buf) : -EIO;
298 put_driver(driver);
299 return ret;
302 static ssize_t
303 pci_driver_attr_store(struct kobject * kobj, struct attribute *attr,
304 const char *buf, size_t count)
306 struct device_driver *driver = kobj_to_pci_driver(kobj);
307 struct driver_attribute *dattr = attr_to_driver_attribute(attr);
308 ssize_t ret;
310 if (!get_driver(driver))
311 return -ENODEV;
313 ret = dattr->store ? dattr->store(driver, buf, count) : -EIO;
315 put_driver(driver);
316 return ret;
319 static struct sysfs_ops pci_driver_sysfs_ops = {
320 .show = pci_driver_attr_show,
321 .store = pci_driver_attr_store,
323 static struct kobj_type pci_driver_kobj_type = {
324 .sysfs_ops = &pci_driver_sysfs_ops,
328 * pci_register_driver - register a new pci driver
329 * @drv: the driver structure to register
331 * Adds the driver structure to the list of registered drivers.
332 * Returns a negative value on error, otherwise 0.
333 * If no error occurred, the driver remains registered even if
334 * no device was claimed during registration.
336 int pci_register_driver(struct pci_driver *drv)
338 int error;
340 /* initialize common driver fields */
341 drv->driver.name = drv->name;
342 drv->driver.bus = &pci_bus_type;
343 drv->driver.probe = pci_device_probe;
344 drv->driver.remove = pci_device_remove;
345 /* FIXME, once all of the existing PCI drivers have been fixed to set
346 * the pci shutdown function, this test can go away. */
347 if (!drv->driver.shutdown)
348 drv->driver.shutdown = pci_device_shutdown;
349 drv->driver.owner = drv->owner;
350 drv->driver.kobj.ktype = &pci_driver_kobj_type;
352 spin_lock_init(&drv->dynids.lock);
353 INIT_LIST_HEAD(&drv->dynids.list);
355 /* register with core */
356 error = driver_register(&drv->driver);
358 if (!error)
359 error = pci_create_newid_file(drv);
361 return error;
365 * pci_unregister_driver - unregister a pci driver
366 * @drv: the driver structure to unregister
368 * Deletes the driver structure from the list of registered PCI drivers,
369 * gives it a chance to clean up by calling its remove() function for
370 * each device it was responsible for, and marks those devices as
371 * driverless.
374 void
375 pci_unregister_driver(struct pci_driver *drv)
377 driver_unregister(&drv->driver);
378 pci_free_dynids(drv);
381 static struct pci_driver pci_compat_driver = {
382 .name = "compat"
386 * pci_dev_driver - get the pci_driver of a device
387 * @dev: the device to query
389 * Returns the appropriate pci_driver structure or %NULL if there is no
390 * registered driver for the device.
392 struct pci_driver *
393 pci_dev_driver(const struct pci_dev *dev)
395 if (dev->driver)
396 return dev->driver;
397 else {
398 int i;
399 for(i=0; i<=PCI_ROM_RESOURCE; i++)
400 if (dev->resource[i].flags & IORESOURCE_BUSY)
401 return &pci_compat_driver;
403 return NULL;
407 * pci_bus_match - Tell if a PCI device structure has a matching PCI device id structure
408 * @ids: array of PCI device id structures to search in
409 * @dev: the PCI device structure to match against
411 * Used by a driver to check whether a PCI device present in the
412 * system is in its list of supported devices.Returns the matching
413 * pci_device_id structure or %NULL if there is no match.
415 static int pci_bus_match(struct device *dev, struct device_driver *drv)
417 struct pci_dev *pci_dev = to_pci_dev(dev);
418 struct pci_driver *pci_drv = to_pci_driver(drv);
419 const struct pci_device_id *found_id;
421 found_id = pci_match_device(pci_drv, pci_dev);
422 if (found_id)
423 return 1;
425 return 0;
429 * pci_dev_get - increments the reference count of the pci device structure
430 * @dev: the device being referenced
432 * Each live reference to a device should be refcounted.
434 * Drivers for PCI devices should normally record such references in
435 * their probe() methods, when they bind to a device, and release
436 * them by calling pci_dev_put(), in their disconnect() methods.
438 * A pointer to the device with the incremented reference counter is returned.
440 struct pci_dev *pci_dev_get(struct pci_dev *dev)
442 if (dev)
443 get_device(&dev->dev);
444 return dev;
448 * pci_dev_put - release a use of the pci device structure
449 * @dev: device that's been disconnected
451 * Must be called when a user of a device is finished with it. When the last
452 * user of the device calls this function, the memory of the device is freed.
454 void pci_dev_put(struct pci_dev *dev)
456 if (dev)
457 put_device(&dev->dev);
460 #ifndef CONFIG_HOTPLUG
461 int pci_hotplug (struct device *dev, char **envp, int num_envp,
462 char *buffer, int buffer_size)
464 return -ENODEV;
466 #endif
468 struct bus_type pci_bus_type = {
469 .name = "pci",
470 .match = pci_bus_match,
471 .hotplug = pci_hotplug,
472 .suspend = pci_device_suspend,
473 .resume = pci_device_resume,
474 .dev_attrs = pci_dev_attrs,
477 static int __init pci_driver_init(void)
479 return bus_register(&pci_bus_type);
482 postcore_initcall(pci_driver_init);
484 EXPORT_SYMBOL(pci_match_id);
485 EXPORT_SYMBOL(pci_match_device);
486 EXPORT_SYMBOL(pci_register_driver);
487 EXPORT_SYMBOL(pci_unregister_driver);
488 EXPORT_SYMBOL(pci_dev_driver);
489 EXPORT_SYMBOL(pci_bus_type);
490 EXPORT_SYMBOL(pci_dev_get);
491 EXPORT_SYMBOL(pci_dev_put);