patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / drivers / pci / pci-driver.c
1 /*
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>
10 #include <linux/pci-dynids.h>
11 #include "pci.h"
12
13 /*
14  *  Registration of PCI drivers and handling of hot-pluggable devices.
15  */
16
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
22  * 
23  * Returns the matching pci_device_id structure or %NULL if there is no match.
24  */
25
26 static inline const struct pci_device_id *
27 pci_match_one_device(const struct pci_device_id *id, const struct pci_dev *dev)
28 {
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;
36 }
37
38 /*
39  * Dynamic device IDs are disabled for !CONFIG_HOTPLUG
40  */
41
42 #ifdef CONFIG_HOTPLUG
43 /**
44  * pci_device_probe_dynamic()
45  *
46  * Walk the dynamic ID list looking for a match.
47  * returns 0 and sets pci_dev->driver when drv claims pci_dev, else error.
48  */
49 static int
50 pci_device_probe_dynamic(struct pci_driver *drv, struct pci_dev *pci_dev)
51 {
52         int error = -ENODEV;
53         struct list_head *pos;
54         struct dynid *dynid;
55
56         spin_lock(&drv->dynids.lock);
57         list_for_each(pos, &drv->dynids.list) {
58                 dynid = list_entry(pos, struct dynid, node);
59                 if (pci_match_one_device(&dynid->id, pci_dev)) {
60                         spin_unlock(&drv->dynids.lock);
61                         error = drv->probe(pci_dev, &dynid->id);
62                         if (error >= 0) {
63                                 pci_dev->driver = drv;
64                                 return 0;
65                         }
66                         return error;
67                 }
68         }
69         spin_unlock(&drv->dynids.lock);
70         return error;
71 }
72
73 static inline void
74 dynid_init(struct dynid *dynid)
75 {
76         memset(dynid, 0, sizeof(*dynid));
77         INIT_LIST_HEAD(&dynid->node);
78 }
79
80 /**
81  * store_new_id
82  *
83  * Adds a new dynamic pci device ID to this driver,
84  * and causes the driver to probe for all devices again.
85  */
86 static inline ssize_t
87 store_new_id(struct device_driver *driver, const char *buf, size_t count)
88 {
89         struct dynid *dynid;
90         struct bus_type * bus;
91         struct pci_driver *pdrv = to_pci_driver(driver);
92         __u32 vendor=PCI_ANY_ID, device=PCI_ANY_ID, subvendor=PCI_ANY_ID,
93                 subdevice=PCI_ANY_ID, class=0, class_mask=0;
94         unsigned long driver_data=0;
95         int fields=0;
96
97         fields = sscanf(buf, "%x %x %x %x %x %x %lux",
98                         &vendor, &device, &subvendor, &subdevice,
99                         &class, &class_mask, &driver_data);
100         if (fields < 0)
101                 return -EINVAL;
102
103         dynid = kmalloc(sizeof(*dynid), GFP_KERNEL);
104         if (!dynid)
105                 return -ENOMEM;
106         dynid_init(dynid);
107
108         dynid->id.vendor = vendor;
109         dynid->id.device = device;
110         dynid->id.subvendor = subvendor;
111         dynid->id.subdevice = subdevice;
112         dynid->id.class = class;
113         dynid->id.class_mask = class_mask;
114         dynid->id.driver_data = pdrv->dynids.use_driver_data ?
115                 driver_data : 0UL;
116
117         spin_lock(&pdrv->dynids.lock);
118         list_add_tail(&pdrv->dynids.list, &dynid->node);
119         spin_unlock(&pdrv->dynids.lock);
120
121         bus = get_bus(pdrv->driver.bus);
122         if (bus) {
123                 if (get_driver(&pdrv->driver)) {
124                         down_write(&bus->subsys.rwsem);
125                         driver_attach(&pdrv->driver);
126                         up_write(&bus->subsys.rwsem);
127                         put_driver(&pdrv->driver);
128                 }
129                 put_bus(bus);
130         }
131
132         return count;
133 }
134
135 static DRIVER_ATTR(new_id, S_IWUSR, NULL, store_new_id);
136 static inline void
137 pci_init_dynids(struct pci_dynids *dynids)
138 {
139         memset(dynids, 0, sizeof(*dynids));
140         spin_lock_init(&dynids->lock);
141         INIT_LIST_HEAD(&dynids->list);
142 }
143
144 static void
145 pci_free_dynids(struct pci_driver *drv)
146 {
147         struct list_head *pos, *n;
148         struct dynid *dynid;
149
150         spin_lock(&drv->dynids.lock);
151         list_for_each_safe(pos, n, &drv->dynids.list) {
152                 dynid = list_entry(pos, struct dynid, node);
153                 list_del(&dynid->node);
154                 kfree(dynid);
155         }
156         spin_unlock(&drv->dynids.lock);
157 }
158
159 static int
160 pci_create_newid_file(struct pci_driver *drv)
161 {
162         int error = 0;
163         if (drv->probe != NULL)
164                 error = sysfs_create_file(&drv->driver.kobj,
165                                           &driver_attr_new_id.attr);
166         return error;
167 }
168
169 static int
170 pci_bus_match_dynids(const struct pci_dev *pci_dev, struct pci_driver *pci_drv)
171 {
172         struct list_head *pos;
173         struct dynid *dynid;
174
175         spin_lock(&pci_drv->dynids.lock);
176         list_for_each(pos, &pci_drv->dynids.list) {
177                 dynid = list_entry(pos, struct dynid, node);
178                 if (pci_match_one_device(&dynid->id, pci_dev)) {
179                         spin_unlock(&pci_drv->dynids.lock);
180                         return 1;
181                 }
182         }
183         spin_unlock(&pci_drv->dynids.lock);
184         return 0;
185 }
186
187 #else /* !CONFIG_HOTPLUG */
188 static inline int pci_device_probe_dynamic(struct pci_driver *drv, struct pci_dev *pci_dev)
189 {
190         return -ENODEV;
191 }
192 static inline void dynid_init(struct dynid *dynid) {}
193 static inline void pci_init_dynids(struct pci_dynids *dynids) {}
194 static inline void pci_free_dynids(struct pci_driver *drv) {}
195 static inline int pci_create_newid_file(struct pci_driver *drv)
196 {
197         return 0;
198 }
199 static inline int pci_bus_match_dynids(const struct pci_dev *pci_dev, struct pci_driver *pci_drv)
200 {
201         return 0;
202 }
203 #endif
204
205 /**
206  * pci_match_device - Tell if a PCI device structure has a matching
207  *                    PCI device id structure
208  * @ids: array of PCI device id structures to search in
209  * @dev: the PCI device structure to match against
210  * 
211  * Used by a driver to check whether a PCI device present in the
212  * system is in its list of supported devices.Returns the matching
213  * pci_device_id structure or %NULL if there is no match.
214  */
215 const struct pci_device_id *
216 pci_match_device(const struct pci_device_id *ids, const struct pci_dev *dev)
217 {
218         while (ids->vendor || ids->subvendor || ids->class_mask) {
219                 if (pci_match_one_device(ids, dev))
220                         return ids;
221                 ids++;
222         }
223         return NULL;
224 }
225
226 /**
227  * pci_device_probe_static()
228  * 
229  * returns 0 and sets pci_dev->driver when drv claims pci_dev, else error.
230  */
231 static int
232 pci_device_probe_static(struct pci_driver *drv, struct pci_dev *pci_dev)
233 {                  
234         int error = -ENODEV;
235         const struct pci_device_id *id;
236
237         if (!drv->id_table)
238                 return error;
239         id = pci_match_device(drv->id_table, pci_dev);
240         if (id)
241                 error = drv->probe(pci_dev, id);
242         if (error >= 0) {
243                 pci_dev->driver = drv;
244                 error = 0;
245         }
246         return error;
247 }
248
249 /**
250  * __pci_device_probe()
251  * 
252  * returns 0  on success, else error.
253  * side-effect: pci_dev->driver is set to drv when drv claims pci_dev.
254  */
255 static int
256 __pci_device_probe(struct pci_driver *drv, struct pci_dev *pci_dev)
257 {                  
258         int error = 0;
259
260         if (!pci_dev->driver && drv->probe) {
261                 error = pci_device_probe_static(drv, pci_dev);
262                 if (error == -ENODEV)
263                         error = pci_device_probe_dynamic(drv, pci_dev);
264         }
265         return error;
266 }
267
268 static int pci_device_probe(struct device * dev)
269 {
270         int error = 0;
271         struct pci_driver *drv;
272         struct pci_dev *pci_dev;
273
274         drv = to_pci_driver(dev->driver);
275         pci_dev = to_pci_dev(dev);
276         pci_dev_get(pci_dev);
277         error = __pci_device_probe(drv, pci_dev);
278         if (error)
279                 pci_dev_put(pci_dev);
280
281         return error;
282 }
283
284 static int pci_device_remove(struct device * dev)
285 {
286         struct pci_dev * pci_dev = to_pci_dev(dev);
287         struct pci_driver * drv = pci_dev->driver;
288
289         if (drv) {
290                 if (drv->remove)
291                         drv->remove(pci_dev);
292                 pci_dev->driver = NULL;
293         }
294         pci_dev_put(pci_dev);
295         return 0;
296 }
297
298 static int pci_device_suspend(struct device * dev, u32 state)
299 {
300         struct pci_dev * pci_dev = to_pci_dev(dev);
301         struct pci_driver * drv = pci_dev->driver;
302         int i = 0;
303
304         if (drv && drv->suspend)
305                 i = drv->suspend(pci_dev,state);
306                 
307         pci_save_state(pci_dev, pci_dev->saved_config_space);
308         return i;
309 }
310
311
312 /* 
313  * Default resume method for devices that have no driver provided resume,
314  * or not even a driver at all.
315  */
316 static void pci_default_resume(struct pci_dev *pci_dev)
317 {
318         /* restore the PCI config space */
319         pci_restore_state(pci_dev, pci_dev->saved_config_space);
320         /* if the device was enabled before suspend, reenable */
321         if (pci_dev->is_enabled)
322                 pci_enable_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);
326 }
327
328 static int pci_device_resume(struct device * dev)
329 {
330         struct pci_dev * pci_dev = to_pci_dev(dev);
331         struct pci_driver * drv = pci_dev->driver;
332
333         if (drv && drv->resume)
334                 drv->resume(pci_dev);
335         else
336                 pci_default_resume(pci_dev);
337         return 0;
338 }
339
340
341 #define kobj_to_pci_driver(obj) container_of(obj, struct device_driver, kobj)
342 #define attr_to_driver_attribute(obj) container_of(obj, struct driver_attribute, attr)
343
344 static ssize_t
345 pci_driver_attr_show(struct kobject * kobj, struct attribute *attr, char *buf)
346 {
347         struct device_driver *driver = kobj_to_pci_driver(kobj);
348         struct driver_attribute *dattr = attr_to_driver_attribute(attr);
349         ssize_t ret = 0;
350
351         if (get_driver(driver)) {
352                 if (dattr->show)
353                         ret = dattr->show(driver, buf);
354                 put_driver(driver);
355         }
356         return ret;
357 }
358
359 static ssize_t
360 pci_driver_attr_store(struct kobject * kobj, struct attribute *attr,
361                       const char *buf, size_t count)
362 {
363         struct device_driver *driver = kobj_to_pci_driver(kobj);
364         struct driver_attribute *dattr = attr_to_driver_attribute(attr);
365         ssize_t ret = 0;
366
367         if (get_driver(driver)) {
368                 if (dattr->store)
369                         ret = dattr->store(driver, buf, count);
370                 put_driver(driver);
371         }
372         return ret;
373 }
374
375 static struct sysfs_ops pci_driver_sysfs_ops = {
376         .show = pci_driver_attr_show,
377         .store = pci_driver_attr_store,
378 };
379 static struct kobj_type pci_driver_kobj_type = {
380         .sysfs_ops = &pci_driver_sysfs_ops,
381 };
382
383 static int
384 pci_populate_driver_dir(struct pci_driver *drv)
385 {
386         return pci_create_newid_file(drv);
387 }
388
389 /**
390  * pci_register_driver - register a new pci driver
391  * @drv: the driver structure to register
392  * 
393  * Adds the driver structure to the list of registered drivers
394  * Returns the number of pci devices which were claimed by the driver
395  * during registration.  The driver remains registered even if the
396  * return value is zero.
397  */
398 int
399 pci_register_driver(struct pci_driver *drv)
400 {
401         int count = 0;
402
403         /* initialize common driver fields */
404         drv->driver.name = drv->name;
405         drv->driver.bus = &pci_bus_type;
406         drv->driver.probe = pci_device_probe;
407         drv->driver.remove = pci_device_remove;
408         drv->driver.kobj.ktype = &pci_driver_kobj_type;
409         pci_init_dynids(&drv->dynids);
410
411         /* register with core */
412         count = driver_register(&drv->driver);
413
414         if (count >= 0) {
415                 pci_populate_driver_dir(drv);
416         }
417
418         return count ? count : 1;
419 }
420
421 /**
422  * pci_unregister_driver - unregister a pci driver
423  * @drv: the driver structure to unregister
424  * 
425  * Deletes the driver structure from the list of registered PCI drivers,
426  * gives it a chance to clean up by calling its remove() function for
427  * each device it was responsible for, and marks those devices as
428  * driverless.
429  */
430
431 void
432 pci_unregister_driver(struct pci_driver *drv)
433 {
434         driver_unregister(&drv->driver);
435         pci_free_dynids(drv);
436 }
437
438 static struct pci_driver pci_compat_driver = {
439         .name = "compat"
440 };
441
442 /**
443  * pci_dev_driver - get the pci_driver of a device
444  * @dev: the device to query
445  *
446  * Returns the appropriate pci_driver structure or %NULL if there is no 
447  * registered driver for the device.
448  */
449 struct pci_driver *
450 pci_dev_driver(const struct pci_dev *dev)
451 {
452         if (dev->driver)
453                 return dev->driver;
454         else {
455                 int i;
456                 for(i=0; i<=PCI_ROM_RESOURCE; i++)
457                         if (dev->resource[i].flags & IORESOURCE_BUSY)
458                                 return &pci_compat_driver;
459         }
460         return NULL;
461 }
462
463 /**
464  * pci_bus_match - Tell if a PCI device structure has a matching PCI device id structure
465  * @ids: array of PCI device id structures to search in
466  * @dev: the PCI device structure to match against
467  * 
468  * Used by a driver to check whether a PCI device present in the
469  * system is in its list of supported devices.Returns the matching
470  * pci_device_id structure or %NULL if there is no match.
471  */
472 static int pci_bus_match(struct device * dev, struct device_driver * drv) 
473 {
474         const struct pci_dev * pci_dev = to_pci_dev(dev);
475         struct pci_driver * pci_drv = to_pci_driver(drv);
476         const struct pci_device_id * ids = pci_drv->id_table;
477         const struct pci_device_id *found_id;
478
479         if (!ids)
480                 return 0;
481
482         found_id = pci_match_device(ids, pci_dev);
483         if (found_id)
484                 return 1;
485
486         return pci_bus_match_dynids(pci_dev, pci_drv);
487 }
488
489 /**
490  * pci_dev_get - increments the reference count of the pci device structure
491  * @dev: the device being referenced
492  *
493  * Each live reference to a device should be refcounted.
494  *
495  * Drivers for PCI devices should normally record such references in
496  * their probe() methods, when they bind to a device, and release
497  * them by calling pci_dev_put(), in their disconnect() methods.
498  *
499  * A pointer to the device with the incremented reference counter is returned.
500  */
501 struct pci_dev *pci_dev_get(struct pci_dev *dev)
502 {
503         struct device *tmp;
504
505         if (!dev)
506                 return NULL;
507
508         tmp = get_device(&dev->dev);
509         if (tmp)        
510                 return to_pci_dev(tmp);
511         else
512                 return NULL;
513 }
514
515 /**
516  * pci_dev_put - release a use of the pci device structure
517  * @dev: device that's been disconnected
518  *
519  * Must be called when a user of a device is finished with it.  When the last
520  * user of the device calls this function, the memory of the device is freed.
521  */
522 void pci_dev_put(struct pci_dev *dev)
523 {
524         if (dev)
525                 put_device(&dev->dev);
526 }
527
528 #ifndef CONFIG_HOTPLUG
529 int pci_hotplug (struct device *dev, char **envp, int num_envp,
530                  char *buffer, int buffer_size)
531 {
532         return -ENODEV;
533 }
534 #endif
535
536 struct bus_type pci_bus_type = {
537         .name           = "pci",
538         .match          = pci_bus_match,
539         .hotplug        = pci_hotplug,
540         .suspend        = pci_device_suspend,
541         .resume         = pci_device_resume,
542 };
543
544 static int __init pci_driver_init(void)
545 {
546         return bus_register(&pci_bus_type);
547 }
548
549 postcore_initcall(pci_driver_init);
550
551 EXPORT_SYMBOL(pci_match_device);
552 EXPORT_SYMBOL(pci_register_driver);
553 EXPORT_SYMBOL(pci_unregister_driver);
554 EXPORT_SYMBOL(pci_dev_driver);
555 EXPORT_SYMBOL(pci_bus_type);
556 EXPORT_SYMBOL(pci_dev_get);
557 EXPORT_SYMBOL(pci_dev_put);