Merge with Linux 2.5.74.
[linux-2.6/linux-mips.git] / drivers / pci / pci-driver.c
blob4d7bafca6222db1f5fbaca73af1cec7acc090784
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/pci-dynids.h>
11 #include "pci.h"
14 * Registration of PCI drivers and handling of hot-pluggable devices.
17 /**
18 * pci_match_one_device - Tell if a PCI device structure has a matching
19 * PCI device id structure
20 * @id: single PCI device id structure to match
21 * @dev: the PCI device structure to match against
23 * Returns the matching pci_device_id structure or %NULL if there is no match.
26 static inline const struct pci_device_id *
27 pci_match_one_device(const struct pci_device_id *id, const struct pci_dev *dev)
29 if ((id->vendor == PCI_ANY_ID || id->vendor == dev->vendor) &&
30 (id->device == PCI_ANY_ID || id->device == dev->device) &&
31 (id->subvendor == PCI_ANY_ID || id->subvendor == dev->subsystem_vendor) &&
32 (id->subdevice == PCI_ANY_ID || id->subdevice == dev->subsystem_device) &&
33 !((id->class ^ dev->class) & id->class_mask))
34 return id;
35 return NULL;
38 /**
39 * pci_match_device - Tell if a PCI device structure has a matching
40 * PCI device id structure
41 * @ids: array of PCI device id structures to search in
42 * @dev: the PCI device structure to match against
44 * Used by a driver to check whether a PCI device present in the
45 * system is in its list of supported devices.Returns the matching
46 * pci_device_id structure or %NULL if there is no match.
48 const struct pci_device_id *
49 pci_match_device(const struct pci_device_id *ids, const struct pci_dev *dev)
51 while (ids->vendor || ids->subvendor || ids->class_mask) {
52 if (pci_match_one_device(ids, dev))
53 return ids;
54 ids++;
56 return NULL;
59 /**
60 * pci_device_probe_static()
62 * returns 0 and sets pci_dev->driver when drv claims pci_dev, else error.
64 static int
65 pci_device_probe_static(struct pci_driver *drv, struct pci_dev *pci_dev)
67 int error = -ENODEV;
68 const struct pci_device_id *id;
70 if (!drv->id_table)
71 return error;
72 id = pci_match_device(drv->id_table, pci_dev);
73 if (id)
74 error = drv->probe(pci_dev, id);
75 if (error >= 0) {
76 pci_dev->driver = drv;
77 return 0;
79 return error;
82 /**
83 * pci_device_probe_dynamic()
85 * Walk the dynamic ID list looking for a match.
86 * returns 0 and sets pci_dev->driver when drv claims pci_dev, else error.
88 static int
89 pci_device_probe_dynamic(struct pci_driver *drv, struct pci_dev *pci_dev)
91 int error = -ENODEV;
92 struct list_head *pos;
93 struct dynid *dynid;
95 spin_lock(&drv->dynids.lock);
96 list_for_each(pos, &drv->dynids.list) {
97 dynid = list_entry(pos, struct dynid, node);
98 if (pci_match_one_device(&dynid->id, pci_dev)) {
99 spin_unlock(&drv->dynids.lock);
100 error = drv->probe(pci_dev, &dynid->id);
101 if (error >= 0) {
102 pci_dev->driver = drv;
103 return 0;
105 return error;
108 spin_unlock(&drv->dynids.lock);
109 return error;
113 * __pci_device_probe()
115 * returns 0 on success, else error.
116 * side-effect: pci_dev->driver is set to drv when drv claims pci_dev.
118 static int
119 __pci_device_probe(struct pci_driver *drv, struct pci_dev *pci_dev)
121 int error = 0;
123 if (!pci_dev->driver && drv->probe) {
124 error = pci_device_probe_static(drv, pci_dev);
125 if (error >= 0)
126 return error;
128 error = pci_device_probe_dynamic(drv, pci_dev);
130 return error;
133 static int pci_device_probe(struct device * dev)
135 int error = 0;
136 struct pci_driver *drv;
137 struct pci_dev *pci_dev;
139 drv = to_pci_driver(dev->driver);
140 pci_dev = to_pci_dev(dev);
141 pci_dev_get(pci_dev);
142 error = __pci_device_probe(drv, pci_dev);
143 if (error)
144 pci_dev_put(pci_dev);
146 return error;
149 static int pci_device_remove(struct device * dev)
151 struct pci_dev * pci_dev = to_pci_dev(dev);
152 struct pci_driver * drv = pci_dev->driver;
154 if (drv) {
155 if (drv->remove)
156 drv->remove(pci_dev);
157 pci_dev->driver = NULL;
159 pci_dev_put(pci_dev);
160 return 0;
163 static int pci_device_suspend(struct device * dev, u32 state, u32 level)
165 struct pci_dev * pci_dev = to_pci_dev(dev);
166 int error = 0;
168 if (pci_dev->driver) {
169 if (level == SUSPEND_SAVE_STATE && pci_dev->driver->save_state)
170 error = pci_dev->driver->save_state(pci_dev,state);
171 else if (level == SUSPEND_POWER_DOWN && pci_dev->driver->suspend)
172 error = pci_dev->driver->suspend(pci_dev,state);
174 return error;
177 static int pci_device_resume(struct device * dev, u32 level)
179 struct pci_dev * pci_dev = to_pci_dev(dev);
181 if (pci_dev->driver) {
182 /* We may not call PCI drivers resume at
183 RESUME_POWER_ON because interrupts are not yet
184 working at that point. Calling resume at
185 RESUME_RESTORE_STATE seems like solution. */
186 if (level == RESUME_RESTORE_STATE && pci_dev->driver->resume)
187 pci_dev->driver->resume(pci_dev);
189 return 0;
192 static inline void
193 dynid_init(struct dynid *dynid)
195 memset(dynid, 0, sizeof(*dynid));
196 INIT_LIST_HEAD(&dynid->node);
200 * store_new_id
201 * @ pdrv
202 * @ buf
203 * @ count
205 * Adds a new dynamic pci device ID to this driver,
206 * and causes the driver to probe for all devices again.
208 static inline ssize_t
209 store_new_id(struct device_driver * driver, const char * buf, size_t count)
211 struct dynid *dynid;
212 struct bus_type * bus;
213 struct pci_driver *pdrv = to_pci_driver(driver);
214 __u32 vendor=PCI_ANY_ID, device=PCI_ANY_ID, subvendor=PCI_ANY_ID,
215 subdevice=PCI_ANY_ID, class=0, class_mask=0;
216 unsigned long driver_data=0;
217 int fields=0;
219 fields = sscanf(buf, "%x %x %x %x %x %x %lux",
220 &vendor, &device, &subvendor, &subdevice,
221 &class, &class_mask, &driver_data);
222 if (fields < 0)
223 return -EINVAL;
225 dynid = kmalloc(sizeof(*dynid), GFP_KERNEL);
226 if (!dynid)
227 return -ENOMEM;
228 dynid_init(dynid);
230 dynid->id.vendor = vendor;
231 dynid->id.device = device;
232 dynid->id.subvendor = subvendor;
233 dynid->id.subdevice = subdevice;
234 dynid->id.class = class;
235 dynid->id.class_mask = class_mask;
236 dynid->id.driver_data = pdrv->dynids.use_driver_data ?
237 driver_data : 0UL;
239 spin_lock(&pdrv->dynids.lock);
240 list_add_tail(&pdrv->dynids.list, &dynid->node);
241 spin_unlock(&pdrv->dynids.lock);
243 bus = get_bus(pdrv->driver.bus);
244 if (bus) {
245 if (get_driver(&pdrv->driver)) {
246 down_write(&bus->subsys.rwsem);
247 driver_attach(&pdrv->driver);
248 up_write(&bus->subsys.rwsem);
249 put_driver(&pdrv->driver);
251 put_bus(bus);
254 return count;
257 static DRIVER_ATTR(new_id, S_IWUSR, NULL, store_new_id);
259 #define kobj_to_pci_driver(obj) container_of(obj, struct device_driver, kobj)
260 #define attr_to_driver_attribute(obj) container_of(obj, struct driver_attribute, attr)
262 static ssize_t
263 pci_driver_attr_show(struct kobject * kobj, struct attribute *attr, char *buf)
265 struct device_driver *driver = kobj_to_pci_driver(kobj);
266 struct driver_attribute *dattr = attr_to_driver_attribute(attr);
267 ssize_t ret = 0;
269 if (get_driver(driver)) {
270 if (dattr->show)
271 ret = dattr->show(driver, buf);
272 put_driver(driver);
274 return ret;
277 static ssize_t
278 pci_driver_attr_store(struct kobject * kobj, struct attribute *attr,
279 const char *buf, size_t count)
281 struct device_driver *driver = kobj_to_pci_driver(kobj);
282 struct driver_attribute *dattr = attr_to_driver_attribute(attr);
283 ssize_t ret = 0;
285 if (get_driver(driver)) {
286 if (dattr->store)
287 ret = dattr->store(driver, buf, count);
288 put_driver(driver);
290 return ret;
293 static struct sysfs_ops pci_driver_sysfs_ops = {
294 .show = pci_driver_attr_show,
295 .store = pci_driver_attr_store,
297 static struct kobj_type pci_driver_kobj_type = {
298 .sysfs_ops = &pci_driver_sysfs_ops,
301 static int
302 pci_populate_driver_dir(struct pci_driver * drv)
304 int error = 0;
306 if (drv->probe != NULL)
307 error = sysfs_create_file(&drv->driver.kobj,
308 &driver_attr_new_id.attr);
309 return error;
312 static inline void
313 pci_init_dynids(struct pci_dynids *dynids)
315 memset(dynids, 0, sizeof(*dynids));
316 spin_lock_init(&dynids->lock);
317 INIT_LIST_HEAD(&dynids->list);
320 static void
321 pci_free_dynids(struct pci_driver *drv)
323 struct list_head *pos, *n;
324 struct dynid *dynid;
326 spin_lock(&drv->dynids.lock);
327 list_for_each_safe(pos, n, &drv->dynids.list) {
328 dynid = list_entry(pos, struct dynid, node);
329 list_del(&dynid->node);
330 kfree(dynid);
332 spin_unlock(&drv->dynids.lock);
337 * pci_register_driver - register a new pci driver
338 * @drv: the driver structure to register
340 * Adds the driver structure to the list of registered drivers
341 * Returns the number of pci devices which were claimed by the driver
342 * during registration. The driver remains registered even if the
343 * return value is zero.
346 pci_register_driver(struct pci_driver *drv)
348 int count = 0;
350 /* initialize common driver fields */
351 drv->driver.name = drv->name;
352 drv->driver.bus = &pci_bus_type;
353 drv->driver.probe = pci_device_probe;
354 drv->driver.resume = pci_device_resume;
355 drv->driver.suspend = pci_device_suspend;
356 drv->driver.remove = pci_device_remove;
357 drv->driver.kobj.ktype = &pci_driver_kobj_type;
358 pci_init_dynids(&drv->dynids);
360 /* register with core */
361 count = driver_register(&drv->driver);
363 if (count >= 0) {
364 pci_populate_driver_dir(drv);
367 return count ? count : 1;
371 * pci_unregister_driver - unregister a pci driver
372 * @drv: the driver structure to unregister
374 * Deletes the driver structure from the list of registered PCI drivers,
375 * gives it a chance to clean up by calling its remove() function for
376 * each device it was responsible for, and marks those devices as
377 * driverless.
380 void
381 pci_unregister_driver(struct pci_driver *drv)
383 driver_unregister(&drv->driver);
384 pci_free_dynids(drv);
387 static struct pci_driver pci_compat_driver = {
388 .name = "compat"
392 * pci_dev_driver - get the pci_driver of a device
393 * @dev: the device to query
395 * Returns the appropriate pci_driver structure or %NULL if there is no
396 * registered driver for the device.
398 struct pci_driver *
399 pci_dev_driver(const struct pci_dev *dev)
401 if (dev->driver)
402 return dev->driver;
403 else {
404 int i;
405 for(i=0; i<=PCI_ROM_RESOURCE; i++)
406 if (dev->resource[i].flags & IORESOURCE_BUSY)
407 return &pci_compat_driver;
409 return NULL;
413 * pci_bus_match - Tell if a PCI device structure has a matching PCI device id structure
414 * @ids: array of PCI device id structures to search in
415 * @dev: the PCI device structure to match against
417 * Used by a driver to check whether a PCI device present in the
418 * system is in its list of supported devices.Returns the matching
419 * pci_device_id structure or %NULL if there is no match.
421 static int pci_bus_match(struct device * dev, struct device_driver * drv)
423 const struct pci_dev * pci_dev = to_pci_dev(dev);
424 struct pci_driver * pci_drv = to_pci_driver(drv);
425 const struct pci_device_id * ids = pci_drv->id_table;
426 const struct pci_device_id *found_id;
427 struct list_head *pos;
428 struct dynid *dynid;
430 if (!ids)
431 return 0;
433 found_id = pci_match_device(ids, pci_dev);
434 if (found_id)
435 return 1;
437 spin_lock(&pci_drv->dynids.lock);
438 list_for_each(pos, &pci_drv->dynids.list) {
439 dynid = list_entry(pos, struct dynid, node);
440 if (pci_match_one_device(&dynid->id, pci_dev)) {
441 spin_unlock(&pci_drv->dynids.lock);
442 return 1;
445 spin_unlock(&pci_drv->dynids.lock);
447 return 0;
451 * pci_dev_get - increments the reference count of the pci device structure
452 * @dev: the device being referenced
454 * Each live reference to a device should be refcounted.
456 * Drivers for PCI devices should normally record such references in
457 * their probe() methods, when they bind to a device, and release
458 * them by calling pci_dev_put(), in their disconnect() methods.
460 * A pointer to the device with the incremented reference counter is returned.
462 struct pci_dev *pci_dev_get(struct pci_dev *dev)
464 struct device *tmp;
466 if (!dev)
467 return NULL;
469 tmp = get_device(&dev->dev);
470 if (tmp)
471 return to_pci_dev(tmp);
472 else
473 return NULL;
477 * pci_dev_put - release a use of the pci device structure
478 * @dev: device that's been disconnected
480 * Must be called when a user of a device is finished with it. When the last
481 * user of the device calls this function, the memory of the device is freed.
483 void pci_dev_put(struct pci_dev *dev)
485 if (dev)
486 put_device(&dev->dev);
489 #ifndef CONFIG_HOTPLUG
490 int pci_hotplug (struct device *dev, char **envp, int num_envp,
491 char *buffer, int buffer_size)
493 return -ENODEV;
495 #endif
497 struct bus_type pci_bus_type = {
498 .name = "pci",
499 .match = pci_bus_match,
500 .hotplug = pci_hotplug,
503 static int __init pci_driver_init(void)
505 return bus_register(&pci_bus_type);
508 postcore_initcall(pci_driver_init);
510 EXPORT_SYMBOL(pci_match_device);
511 EXPORT_SYMBOL(pci_register_driver);
512 EXPORT_SYMBOL(pci_unregister_driver);
513 EXPORT_SYMBOL(pci_dev_driver);
514 EXPORT_SYMBOL(pci_bus_type);
515 EXPORT_SYMBOL(pci_dev_get);
516 EXPORT_SYMBOL(pci_dev_put);