PCMCIA: use proper call to driver_create_file
[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>
d42c6997 10#include <linux/mempolicy.h>
4e57b681
TS
11#include <linux/string.h>
12#include <linux/slab.h>
8c65b4a6 13#include <linux/sched.h>
1da177e4
LT
14#include "pci.h"
15
1da177e4
LT
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 27/**
8f7020d3
RD
28 * store_new_id - add a new PCI device ID to this driver and re-probe devices
29 * @driver: target device driver
30 * @buf: buffer for scanning device ID data
31 * @count: input size
1da177e4
LT
32 *
33 * Adds a new dynamic pci device ID to this driver,
34 * and causes the driver to probe for all devices again.
35 */
f8eb1005 36static ssize_t
1da177e4
LT
37store_new_id(struct device_driver *driver, const char *buf, size_t count)
38{
75865858 39 struct pci_dynid *dynid;
1da177e4 40 struct pci_driver *pdrv = to_pci_driver(driver);
6ba18636 41 __u32 vendor, device, subvendor=PCI_ANY_ID,
1da177e4
LT
42 subdevice=PCI_ANY_ID, class=0, class_mask=0;
43 unsigned long driver_data=0;
44 int fields=0;
b19441af 45 int retval = 0;
1da177e4
LT
46
47 fields = sscanf(buf, "%x %x %x %x %x %x %lux",
48 &vendor, &device, &subvendor, &subdevice,
49 &class, &class_mask, &driver_data);
6ba18636 50 if (fields < 2)
1da177e4
LT
51 return -EINVAL;
52
f5afe806 53 dynid = kzalloc(sizeof(*dynid), GFP_KERNEL);
1da177e4
LT
54 if (!dynid)
55 return -ENOMEM;
56
1da177e4
LT
57 dynid->id.vendor = vendor;
58 dynid->id.device = device;
59 dynid->id.subvendor = subvendor;
60 dynid->id.subdevice = subdevice;
61 dynid->id.class = class;
62 dynid->id.class_mask = class_mask;
63 dynid->id.driver_data = pdrv->dynids.use_driver_data ?
64 driver_data : 0UL;
65
66 spin_lock(&pdrv->dynids.lock);
a56bc69a 67 list_add_tail(&dynid->node, &pdrv->dynids.list);
1da177e4
LT
68 spin_unlock(&pdrv->dynids.lock);
69
75865858 70 if (get_driver(&pdrv->driver)) {
b19441af 71 retval = driver_attach(&pdrv->driver);
75865858 72 put_driver(&pdrv->driver);
1da177e4
LT
73 }
74
b19441af
GKH
75 if (retval)
76 return retval;
1da177e4
LT
77 return count;
78}
1da177e4 79static DRIVER_ATTR(new_id, S_IWUSR, NULL, store_new_id);
1da177e4
LT
80
81static void
82pci_free_dynids(struct pci_driver *drv)
83{
75865858 84 struct pci_dynid *dynid, *n;
1da177e4
LT
85
86 spin_lock(&drv->dynids.lock);
75865858 87 list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) {
1da177e4
LT
88 list_del(&dynid->node);
89 kfree(dynid);
90 }
91 spin_unlock(&drv->dynids.lock);
92}
93
94static int
95pci_create_newid_file(struct pci_driver *drv)
96{
97 int error = 0;
98 if (drv->probe != NULL)
99 error = sysfs_create_file(&drv->driver.kobj,
100 &driver_attr_new_id.attr);
101 return error;
102}
103
1da177e4 104#else /* !CONFIG_HOTPLUG */
1da177e4
LT
105static inline void pci_free_dynids(struct pci_driver *drv) {}
106static inline int pci_create_newid_file(struct pci_driver *drv)
107{
108 return 0;
109}
1da177e4
LT
110#endif
111
112/**
75865858 113 * pci_match_id - See if a pci device matches a given pci_id table
1da177e4 114 * @ids: array of PCI device id structures to search in
75865858
GKH
115 * @dev: the PCI device structure to match against.
116 *
1da177e4 117 * Used by a driver to check whether a PCI device present in the
75865858 118 * system is in its list of supported devices. Returns the matching
1da177e4 119 * pci_device_id structure or %NULL if there is no match.
75865858 120 *
8b60756a 121 * Deprecated, don't use this as it will not catch any dynamic ids
75865858 122 * that a driver might want to check for.
1da177e4 123 */
75865858
GKH
124const struct pci_device_id *pci_match_id(const struct pci_device_id *ids,
125 struct pci_dev *dev)
1da177e4 126{
75865858
GKH
127 if (ids) {
128 while (ids->vendor || ids->subvendor || ids->class_mask) {
129 if (pci_match_one_device(ids, dev))
130 return ids;
131 ids++;
132 }
1da177e4
LT
133 }
134 return NULL;
135}
136
137/**
ae9608af 138 * pci_match_device - Tell if a PCI device structure has a matching PCI device id structure
75865858 139 * @drv: the PCI driver to match against
39ba487f 140 * @dev: the PCI device structure to match against
75865858
GKH
141 *
142 * Used by a driver to check whether a PCI device present in the
143 * system is in its list of supported devices. Returns the matching
144 * pci_device_id structure or %NULL if there is no match.
1da177e4 145 */
d73460d7
AB
146static const struct pci_device_id *pci_match_device(struct pci_driver *drv,
147 struct pci_dev *dev)
75865858 148{
75865858 149 struct pci_dynid *dynid;
1da177e4 150
7461b60a 151 /* Look at the dynamic ids first, before the static ones */
75865858
GKH
152 spin_lock(&drv->dynids.lock);
153 list_for_each_entry(dynid, &drv->dynids.list, node) {
154 if (pci_match_one_device(&dynid->id, dev)) {
155 spin_unlock(&drv->dynids.lock);
156 return &dynid->id;
157 }
1da177e4 158 }
75865858 159 spin_unlock(&drv->dynids.lock);
7461b60a
RK
160
161 return pci_match_id(drv->id_table, dev);
1da177e4
LT
162}
163
d42c6997
AK
164static int pci_call_probe(struct pci_driver *drv, struct pci_dev *dev,
165 const struct pci_device_id *id)
166{
167 int error;
168#ifdef CONFIG_NUMA
169 /* Execute driver initialization on node where the
170 device's bus is attached to. This way the driver likely
171 allocates its local memory on the right node without
172 any need to change it. */
173 struct mempolicy *oldpol;
174 cpumask_t oldmask = current->cpus_allowed;
175 int node = pcibus_to_node(dev->bus);
176 if (node >= 0 && node_online(node))
177 set_cpus_allowed(current, node_to_cpumask(node));
178 /* And set default memory allocation policy */
179 oldpol = current->mempolicy;
180 current->mempolicy = &default_policy;
181 mpol_get(current->mempolicy);
182#endif
183 error = drv->probe(dev, id);
184#ifdef CONFIG_NUMA
185 set_cpus_allowed(current, oldmask);
186 mpol_free(current->mempolicy);
187 current->mempolicy = oldpol;
188#endif
189 return error;
190}
191
1da177e4
LT
192/**
193 * __pci_device_probe()
8f7020d3
RD
194 * @drv: driver to call to check if it wants the PCI device
195 * @pci_dev: PCI device being probed
1da177e4 196 *
8f7020d3 197 * returns 0 on success, else error.
1da177e4
LT
198 * side-effect: pci_dev->driver is set to drv when drv claims pci_dev.
199 */
200static int
201__pci_device_probe(struct pci_driver *drv, struct pci_dev *pci_dev)
75865858
GKH
202{
203 const struct pci_device_id *id;
1da177e4
LT
204 int error = 0;
205
206 if (!pci_dev->driver && drv->probe) {
75865858
GKH
207 error = -ENODEV;
208
209 id = pci_match_device(drv, pci_dev);
210 if (id)
d42c6997 211 error = pci_call_probe(drv, pci_dev, id);
75865858
GKH
212 if (error >= 0) {
213 pci_dev->driver = drv;
214 error = 0;
215 }
1da177e4
LT
216 }
217 return error;
218}
219
220static int pci_device_probe(struct device * dev)
221{
222 int error = 0;
223 struct pci_driver *drv;
224 struct pci_dev *pci_dev;
225
226 drv = to_pci_driver(dev->driver);
227 pci_dev = to_pci_dev(dev);
228 pci_dev_get(pci_dev);
229 error = __pci_device_probe(drv, pci_dev);
230 if (error)
231 pci_dev_put(pci_dev);
232
233 return error;
234}
235
236static int pci_device_remove(struct device * dev)
237{
238 struct pci_dev * pci_dev = to_pci_dev(dev);
239 struct pci_driver * drv = pci_dev->driver;
240
241 if (drv) {
242 if (drv->remove)
243 drv->remove(pci_dev);
244 pci_dev->driver = NULL;
245 }
246
2449e06a
SL
247 /*
248 * If the device is still on, set the power state as "unknown",
249 * since it might change by the next time we load the driver.
250 */
251 if (pci_dev->current_state == PCI_D0)
252 pci_dev->current_state = PCI_UNKNOWN;
253
1da177e4
LT
254 /*
255 * We would love to complain here if pci_dev->is_enabled is set, that
256 * the driver should have called pci_disable_device(), but the
257 * unfortunate fact is there are too many odd BIOS and bridge setups
258 * that don't like drivers doing that all of the time.
259 * Oh well, we can dream of sane hardware when we sleep, no matter how
260 * horrible the crap we have to deal with is when we are awake...
261 */
262
263 pci_dev_put(pci_dev);
264 return 0;
265}
266
267static int pci_device_suspend(struct device * dev, pm_message_t state)
268{
269 struct pci_dev * pci_dev = to_pci_dev(dev);
270 struct pci_driver * drv = pci_dev->driver;
271 int i = 0;
272
02669492 273 if (drv && drv->suspend) {
1da177e4 274 i = drv->suspend(pci_dev, state);
02669492
AM
275 suspend_report_result(drv->suspend, i);
276 } else {
1da177e4 277 pci_save_state(pci_dev);
2449e06a
SL
278 /*
279 * mark its power state as "unknown", since we don't know if
280 * e.g. the BIOS will change its device state when we suspend.
281 */
282 if (pci_dev->current_state == PCI_D0)
283 pci_dev->current_state = PCI_UNKNOWN;
02669492 284 }
1da177e4
LT
285 return i;
286}
287
cbd69dbb
LT
288static int pci_device_suspend_late(struct device * dev, pm_message_t state)
289{
290 struct pci_dev * pci_dev = to_pci_dev(dev);
291 struct pci_driver * drv = pci_dev->driver;
292 int i = 0;
293
294 if (drv && drv->suspend_late) {
295 i = drv->suspend_late(pci_dev, state);
296 suspend_report_result(drv->suspend_late, i);
297 }
298 return i;
299}
1da177e4 300
95a62965 301/*
1da177e4
LT
302 * Default resume method for devices that have no driver provided resume,
303 * or not even a driver at all.
304 */
8d92bc22 305static int pci_default_resume(struct pci_dev *pci_dev)
1da177e4 306{
8d92bc22 307 int retval = 0;
95a62965 308
1da177e4
LT
309 /* restore the PCI config space */
310 pci_restore_state(pci_dev);
311 /* if the device was enabled before suspend, reenable */
0b62e13b 312 retval = pci_reenable_device(pci_dev);
1da177e4
LT
313 /* if the device was busmaster before the suspend, make it busmaster again */
314 if (pci_dev->is_busmaster)
315 pci_set_master(pci_dev);
8d92bc22
JD
316
317 return retval;
1da177e4
LT
318}
319
320static int pci_device_resume(struct device * dev)
321{
8d92bc22 322 int error;
1da177e4
LT
323 struct pci_dev * pci_dev = to_pci_dev(dev);
324 struct pci_driver * drv = pci_dev->driver;
325
326 if (drv && drv->resume)
8d92bc22 327 error = drv->resume(pci_dev);
1da177e4 328 else
8d92bc22
JD
329 error = pci_default_resume(pci_dev);
330 return error;
1da177e4
LT
331}
332
cbd69dbb
LT
333static int pci_device_resume_early(struct device * dev)
334{
335 int error = 0;
336 struct pci_dev * pci_dev = to_pci_dev(dev);
337 struct pci_driver * drv = pci_dev->driver;
338
1597cacb
AC
339 pci_fixup_device(pci_fixup_resume, pci_dev);
340
cbd69dbb
LT
341 if (drv && drv->resume_early)
342 error = drv->resume_early(pci_dev);
343 return error;
344}
345
c8958177
GKH
346static void pci_device_shutdown(struct device *dev)
347{
348 struct pci_dev *pci_dev = to_pci_dev(dev);
349 struct pci_driver *drv = pci_dev->driver;
350
351 if (drv && drv->shutdown)
352 drv->shutdown(pci_dev);
353}
1da177e4
LT
354
355#define kobj_to_pci_driver(obj) container_of(obj, struct device_driver, kobj)
356#define attr_to_driver_attribute(obj) container_of(obj, struct driver_attribute, attr)
357
358static ssize_t
359pci_driver_attr_show(struct kobject * kobj, struct attribute *attr, char *buf)
360{
361 struct device_driver *driver = kobj_to_pci_driver(kobj);
362 struct driver_attribute *dattr = attr_to_driver_attribute(attr);
fc7e4828 363 ssize_t ret;
1da177e4 364
fc7e4828
DT
365 if (!get_driver(driver))
366 return -ENODEV;
367
368 ret = dattr->show ? dattr->show(driver, buf) : -EIO;
369
370 put_driver(driver);
1da177e4
LT
371 return ret;
372}
373
374static ssize_t
375pci_driver_attr_store(struct kobject * kobj, struct attribute *attr,
376 const char *buf, size_t count)
377{
378 struct device_driver *driver = kobj_to_pci_driver(kobj);
379 struct driver_attribute *dattr = attr_to_driver_attribute(attr);
fc7e4828 380 ssize_t ret;
1da177e4 381
fc7e4828
DT
382 if (!get_driver(driver))
383 return -ENODEV;
384
385 ret = dattr->store ? dattr->store(driver, buf, count) : -EIO;
386
387 put_driver(driver);
1da177e4
LT
388 return ret;
389}
390
391static struct sysfs_ops pci_driver_sysfs_ops = {
392 .show = pci_driver_attr_show,
393 .store = pci_driver_attr_store,
394};
395static struct kobj_type pci_driver_kobj_type = {
396 .sysfs_ops = &pci_driver_sysfs_ops,
397};
398
1da177e4 399/**
863b18f4 400 * __pci_register_driver - register a new pci driver
1da177e4 401 * @drv: the driver structure to register
863b18f4 402 * @owner: owner module of drv
f95d882d 403 * @mod_name: module name string
1da177e4
LT
404 *
405 * Adds the driver structure to the list of registered drivers.
406 * Returns a negative value on error, otherwise 0.
eaae4b3a 407 * If no error occurred, the driver remains registered even if
1da177e4
LT
408 * no device was claimed during registration.
409 */
725522b5
GKH
410int __pci_register_driver(struct pci_driver *drv, struct module *owner,
411 const char *mod_name)
1da177e4
LT
412{
413 int error;
414
415 /* initialize common driver fields */
416 drv->driver.name = drv->name;
417 drv->driver.bus = &pci_bus_type;
863b18f4 418 drv->driver.owner = owner;
725522b5 419 drv->driver.mod_name = mod_name;
1da177e4 420 drv->driver.kobj.ktype = &pci_driver_kobj_type;
50b00755 421
75865858
GKH
422 spin_lock_init(&drv->dynids.lock);
423 INIT_LIST_HEAD(&drv->dynids.list);
1da177e4
LT
424
425 /* register with core */
426 error = driver_register(&drv->driver);
50bf14b3
AM
427 if (error)
428 return error;
1da177e4 429
50bf14b3
AM
430 error = pci_create_newid_file(drv);
431 if (error)
432 driver_unregister(&drv->driver);
1da177e4
LT
433
434 return error;
435}
436
437/**
438 * pci_unregister_driver - unregister a pci driver
439 * @drv: the driver structure to unregister
440 *
441 * Deletes the driver structure from the list of registered PCI drivers,
442 * gives it a chance to clean up by calling its remove() function for
443 * each device it was responsible for, and marks those devices as
444 * driverless.
445 */
446
447void
448pci_unregister_driver(struct pci_driver *drv)
449{
450 driver_unregister(&drv->driver);
451 pci_free_dynids(drv);
452}
453
454static struct pci_driver pci_compat_driver = {
455 .name = "compat"
456};
457
458/**
459 * pci_dev_driver - get the pci_driver of a device
460 * @dev: the device to query
461 *
462 * Returns the appropriate pci_driver structure or %NULL if there is no
463 * registered driver for the device.
464 */
465struct pci_driver *
466pci_dev_driver(const struct pci_dev *dev)
467{
468 if (dev->driver)
469 return dev->driver;
470 else {
471 int i;
472 for(i=0; i<=PCI_ROM_RESOURCE; i++)
473 if (dev->resource[i].flags & IORESOURCE_BUSY)
474 return &pci_compat_driver;
475 }
476 return NULL;
477}
478
479/**
480 * pci_bus_match - Tell if a PCI device structure has a matching PCI device id structure
1da177e4 481 * @dev: the PCI device structure to match against
8f7020d3 482 * @drv: the device driver to search for matching PCI device id structures
1da177e4
LT
483 *
484 * Used by a driver to check whether a PCI device present in the
8f7020d3 485 * system is in its list of supported devices. Returns the matching
1da177e4
LT
486 * pci_device_id structure or %NULL if there is no match.
487 */
75865858 488static int pci_bus_match(struct device *dev, struct device_driver *drv)
1da177e4 489{
75865858
GKH
490 struct pci_dev *pci_dev = to_pci_dev(dev);
491 struct pci_driver *pci_drv = to_pci_driver(drv);
1da177e4
LT
492 const struct pci_device_id *found_id;
493
75865858 494 found_id = pci_match_device(pci_drv, pci_dev);
1da177e4
LT
495 if (found_id)
496 return 1;
497
75865858 498 return 0;
1da177e4
LT
499}
500
501/**
502 * pci_dev_get - increments the reference count of the pci device structure
503 * @dev: the device being referenced
504 *
505 * Each live reference to a device should be refcounted.
506 *
507 * Drivers for PCI devices should normally record such references in
508 * their probe() methods, when they bind to a device, and release
509 * them by calling pci_dev_put(), in their disconnect() methods.
510 *
511 * A pointer to the device with the incremented reference counter is returned.
512 */
513struct pci_dev *pci_dev_get(struct pci_dev *dev)
514{
515 if (dev)
516 get_device(&dev->dev);
517 return dev;
518}
519
520/**
521 * pci_dev_put - release a use of the pci device structure
522 * @dev: device that's been disconnected
523 *
524 * Must be called when a user of a device is finished with it. When the last
525 * user of the device calls this function, the memory of the device is freed.
526 */
527void pci_dev_put(struct pci_dev *dev)
528{
529 if (dev)
530 put_device(&dev->dev);
531}
532
533#ifndef CONFIG_HOTPLUG
7eff2e7a 534int pci_uevent(struct device *dev, struct kobj_uevent_env *env)
1da177e4
LT
535{
536 return -ENODEV;
537}
538#endif
539
540struct bus_type pci_bus_type = {
541 .name = "pci",
542 .match = pci_bus_match,
312c004d 543 .uevent = pci_uevent,
b15d686a
RK
544 .probe = pci_device_probe,
545 .remove = pci_device_remove,
1da177e4 546 .suspend = pci_device_suspend,
cbd69dbb
LT
547 .suspend_late = pci_device_suspend_late,
548 .resume_early = pci_device_resume_early,
1da177e4 549 .resume = pci_device_resume,
cbd69dbb 550 .shutdown = pci_device_shutdown,
1da177e4
LT
551 .dev_attrs = pci_dev_attrs,
552};
553
554static int __init pci_driver_init(void)
555{
556 return bus_register(&pci_bus_type);
557}
558
559postcore_initcall(pci_driver_init);
560
75865858 561EXPORT_SYMBOL(pci_match_id);
863b18f4 562EXPORT_SYMBOL(__pci_register_driver);
1da177e4
LT
563EXPORT_SYMBOL(pci_unregister_driver);
564EXPORT_SYMBOL(pci_dev_driver);
565EXPORT_SYMBOL(pci_bus_type);
566EXPORT_SYMBOL(pci_dev_get);
567EXPORT_SYMBOL(pci_dev_put);