pci-driver.c 13 KB

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