[PATCH] PCI Hotplug: rpaphp: Purify hotplug
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / pci / pci-driver.c
CommitLineData
1da177e4
LT
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>
1da177e4
LT
10#include "pci.h"
11
12/*
13 * Registration of PCI drivers and handling of hot-pluggable devices.
14 */
15
16/*
17 * Dynamic device IDs are disabled for !CONFIG_HOTPLUG
18 */
19
75865858
GKH
20struct pci_dynid {
21 struct list_head node;
22 struct pci_device_id id;
23};
1da177e4 24
3d3c2ae1
GKH
25#ifdef CONFIG_HOTPLUG
26
1da177e4
LT
27/**
28 * store_new_id
29 *
30 * Adds a new dynamic pci device ID to this driver,
31 * and causes the driver to probe for all devices again.
32 */
33static inline ssize_t
34store_new_id(struct device_driver *driver, const char *buf, size_t count)
35{
75865858 36 struct pci_dynid *dynid;
1da177e4
LT
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;
42
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;
48
49 dynid = kmalloc(sizeof(*dynid), GFP_KERNEL);
50 if (!dynid)
51 return -ENOMEM;
52
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;
63
64 spin_lock(&pdrv->dynids.lock);
65 list_add_tail(&pdrv->dynids.list, &dynid->node);
66 spin_unlock(&pdrv->dynids.lock);
67
75865858
GKH
68 if (get_driver(&pdrv->driver)) {
69 driver_attach(&pdrv->driver);
70 put_driver(&pdrv->driver);
1da177e4
LT
71 }
72
73 return count;
74}
1da177e4 75static DRIVER_ATTR(new_id, S_IWUSR, NULL, store_new_id);
1da177e4
LT
76
77static void
78pci_free_dynids(struct pci_driver *drv)
79{
75865858 80 struct pci_dynid *dynid, *n;
1da177e4
LT
81
82 spin_lock(&drv->dynids.lock);
75865858 83 list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) {
1da177e4
LT
84 list_del(&dynid->node);
85 kfree(dynid);
86 }
87 spin_unlock(&drv->dynids.lock);
88}
89
90static int
91pci_create_newid_file(struct pci_driver *drv)
92{
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;
98}
99
1da177e4 100#else /* !CONFIG_HOTPLUG */
1da177e4
LT
101static inline void pci_free_dynids(struct pci_driver *drv) {}
102static inline int pci_create_newid_file(struct pci_driver *drv)
103{
104 return 0;
105}
1da177e4
LT
106#endif
107
108/**
75865858 109 * pci_match_id - See if a pci device matches a given pci_id table
1da177e4 110 * @ids: array of PCI device id structures to search in
75865858
GKH
111 * @dev: the PCI device structure to match against.
112 *
1da177e4 113 * Used by a driver to check whether a PCI device present in the
75865858 114 * system is in its list of supported devices. Returns the matching
1da177e4 115 * pci_device_id structure or %NULL if there is no match.
75865858
GKH
116 *
117 * Depreciated, don't use this as it will not catch any dynamic ids
118 * that a driver might want to check for.
1da177e4 119 */
75865858
GKH
120const struct pci_device_id *pci_match_id(const struct pci_device_id *ids,
121 struct pci_dev *dev)
1da177e4 122{
75865858
GKH
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++;
128 }
1da177e4
LT
129 }
130 return NULL;
131}
132
133/**
75865858
GKH
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
139 *
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.
1da177e4 143 */
75865858
GKH
144const struct pci_device_id *pci_match_device(struct pci_driver *drv,
145 struct pci_dev *dev)
146{
1da177e4 147 const struct pci_device_id *id;
75865858 148 struct pci_dynid *dynid;
1da177e4 149
75865858 150 id = pci_match_id(drv->id_table, dev);
1da177e4 151 if (id)
75865858
GKH
152 return id;
153
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;
160 }
1da177e4 161 }
75865858
GKH
162 spin_unlock(&drv->dynids.lock);
163 return NULL;
1da177e4
LT
164}
165
166/**
167 * __pci_device_probe()
168 *
169 * returns 0 on success, else error.
170 * side-effect: pci_dev->driver is set to drv when drv claims pci_dev.
171 */
172static int
173__pci_device_probe(struct pci_driver *drv, struct pci_dev *pci_dev)
75865858
GKH
174{
175 const struct pci_device_id *id;
1da177e4
LT
176 int error = 0;
177
178 if (!pci_dev->driver && drv->probe) {
75865858
GKH
179 error = -ENODEV;
180
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;
187 }
1da177e4
LT
188 }
189 return error;
190}
191
192static int pci_device_probe(struct device * dev)
193{
194 int error = 0;
195 struct pci_driver *drv;
196 struct pci_dev *pci_dev;
197
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);
204
205 return error;
206}
207
208static int pci_device_remove(struct device * dev)
209{
210 struct pci_dev * pci_dev = to_pci_dev(dev);
211 struct pci_driver * drv = pci_dev->driver;
212
213 if (drv) {
214 if (drv->remove)
215 drv->remove(pci_dev);
216 pci_dev->driver = NULL;
217 }
218
219 /*
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...
226 */
227
228 pci_dev_put(pci_dev);
229 return 0;
230}
231
232static int pci_device_suspend(struct device * dev, pm_message_t state)
233{
234 struct pci_dev * pci_dev = to_pci_dev(dev);
235 struct pci_driver * drv = pci_dev->driver;
236 int i = 0;
237
238 if (drv && drv->suspend)
239 i = drv->suspend(pci_dev, state);
240 else
241 pci_save_state(pci_dev);
242 return i;
243}
244
245
246/*
247 * Default resume method for devices that have no driver provided resume,
248 * or not even a driver at all.
249 */
250static void pci_default_resume(struct pci_dev *pci_dev)
251{
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);
260}
261
262static int pci_device_resume(struct device * dev)
263{
264 struct pci_dev * pci_dev = to_pci_dev(dev);
265 struct pci_driver * drv = pci_dev->driver;
266
267 if (drv && drv->resume)
268 drv->resume(pci_dev);
269 else
270 pci_default_resume(pci_dev);
271 return 0;
272}
273
c8958177
GKH
274static void pci_device_shutdown(struct device *dev)
275{
276 struct pci_dev *pci_dev = to_pci_dev(dev);
277 struct pci_driver *drv = pci_dev->driver;
278
279 if (drv && drv->shutdown)
280 drv->shutdown(pci_dev);
281}
1da177e4
LT
282
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)
285
286static ssize_t
287pci_driver_attr_show(struct kobject * kobj, struct attribute *attr, char *buf)
288{
289 struct device_driver *driver = kobj_to_pci_driver(kobj);
290 struct driver_attribute *dattr = attr_to_driver_attribute(attr);
fc7e4828 291 ssize_t ret;
1da177e4 292
fc7e4828
DT
293 if (!get_driver(driver))
294 return -ENODEV;
295
296 ret = dattr->show ? dattr->show(driver, buf) : -EIO;
297
298 put_driver(driver);
1da177e4
LT
299 return ret;
300}
301
302static ssize_t
303pci_driver_attr_store(struct kobject * kobj, struct attribute *attr,
304 const char *buf, size_t count)
305{
306 struct device_driver *driver = kobj_to_pci_driver(kobj);
307 struct driver_attribute *dattr = attr_to_driver_attribute(attr);
fc7e4828 308 ssize_t ret;
1da177e4 309
fc7e4828
DT
310 if (!get_driver(driver))
311 return -ENODEV;
312
313 ret = dattr->store ? dattr->store(driver, buf, count) : -EIO;
314
315 put_driver(driver);
1da177e4
LT
316 return ret;
317}
318
319static struct sysfs_ops pci_driver_sysfs_ops = {
320 .show = pci_driver_attr_show,
321 .store = pci_driver_attr_store,
322};
323static struct kobj_type pci_driver_kobj_type = {
324 .sysfs_ops = &pci_driver_sysfs_ops,
325};
326
1da177e4
LT
327/**
328 * pci_register_driver - register a new pci driver
329 * @drv: the driver structure to register
330 *
331 * Adds the driver structure to the list of registered drivers.
332 * Returns a negative value on error, otherwise 0.
eaae4b3a 333 * If no error occurred, the driver remains registered even if
1da177e4
LT
334 * no device was claimed during registration.
335 */
336int pci_register_driver(struct pci_driver *drv)
337{
338 int error;
339
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;
794f5bfa
CH
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)
c83d9945 348 drv->driver.shutdown = pci_device_shutdown;
1da177e4
LT
349 drv->driver.owner = drv->owner;
350 drv->driver.kobj.ktype = &pci_driver_kobj_type;
75865858
GKH
351
352 spin_lock_init(&drv->dynids.lock);
353 INIT_LIST_HEAD(&drv->dynids.list);
1da177e4
LT
354
355 /* register with core */
356 error = driver_register(&drv->driver);
357
358 if (!error)
75865858 359 error = pci_create_newid_file(drv);
1da177e4
LT
360
361 return error;
362}
363
364/**
365 * pci_unregister_driver - unregister a pci driver
366 * @drv: the driver structure to unregister
367 *
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.
372 */
373
374void
375pci_unregister_driver(struct pci_driver *drv)
376{
377 driver_unregister(&drv->driver);
378 pci_free_dynids(drv);
379}
380
381static struct pci_driver pci_compat_driver = {
382 .name = "compat"
383};
384
385/**
386 * pci_dev_driver - get the pci_driver of a device
387 * @dev: the device to query
388 *
389 * Returns the appropriate pci_driver structure or %NULL if there is no
390 * registered driver for the device.
391 */
392struct pci_driver *
393pci_dev_driver(const struct pci_dev *dev)
394{
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;
402 }
403 return NULL;
404}
405
406/**
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
410 *
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.
414 */
75865858 415static int pci_bus_match(struct device *dev, struct device_driver *drv)
1da177e4 416{
75865858
GKH
417 struct pci_dev *pci_dev = to_pci_dev(dev);
418 struct pci_driver *pci_drv = to_pci_driver(drv);
1da177e4
LT
419 const struct pci_device_id *found_id;
420
75865858 421 found_id = pci_match_device(pci_drv, pci_dev);
1da177e4
LT
422 if (found_id)
423 return 1;
424
75865858 425 return 0;
1da177e4
LT
426}
427
428/**
429 * pci_dev_get - increments the reference count of the pci device structure
430 * @dev: the device being referenced
431 *
432 * Each live reference to a device should be refcounted.
433 *
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.
437 *
438 * A pointer to the device with the incremented reference counter is returned.
439 */
440struct pci_dev *pci_dev_get(struct pci_dev *dev)
441{
442 if (dev)
443 get_device(&dev->dev);
444 return dev;
445}
446
447/**
448 * pci_dev_put - release a use of the pci device structure
449 * @dev: device that's been disconnected
450 *
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.
453 */
454void pci_dev_put(struct pci_dev *dev)
455{
456 if (dev)
457 put_device(&dev->dev);
458}
459
460#ifndef CONFIG_HOTPLUG
461int pci_hotplug (struct device *dev, char **envp, int num_envp,
462 char *buffer, int buffer_size)
463{
464 return -ENODEV;
465}
466#endif
467
468struct 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,
475};
476
477static int __init pci_driver_init(void)
478{
479 return bus_register(&pci_bus_type);
480}
481
482postcore_initcall(pci_driver_init);
483
75865858 484EXPORT_SYMBOL(pci_match_id);
1da177e4
LT
485EXPORT_SYMBOL(pci_match_device);
486EXPORT_SYMBOL(pci_register_driver);
487EXPORT_SYMBOL(pci_unregister_driver);
488EXPORT_SYMBOL(pci_dev_driver);
489EXPORT_SYMBOL(pci_bus_type);
490EXPORT_SYMBOL(pci_dev_get);
491EXPORT_SYMBOL(pci_dev_put);