pci-driver.c 14 KB

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