pci-driver.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  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. * Registration of PCI drivers and handling of hot-pluggable devices.
  16. */
  17. /*
  18. * Dynamic device IDs are disabled for !CONFIG_HOTPLUG
  19. */
  20. struct pci_dynid {
  21. struct list_head node;
  22. struct pci_device_id id;
  23. };
  24. #ifdef CONFIG_HOTPLUG
  25. /**
  26. * store_new_id - add a new PCI device ID to this driver and re-probe devices
  27. * @driver: target device driver
  28. * @buf: buffer for scanning device ID data
  29. * @count: input size
  30. *
  31. * Adds a new dynamic pci device ID to this driver,
  32. * and causes the driver to probe for all devices again.
  33. */
  34. static ssize_t
  35. store_new_id(struct device_driver *driver, const char *buf, size_t count)
  36. {
  37. struct pci_dynid *dynid;
  38. struct pci_driver *pdrv = to_pci_driver(driver);
  39. __u32 vendor=PCI_ANY_ID, device=PCI_ANY_ID, subvendor=PCI_ANY_ID,
  40. subdevice=PCI_ANY_ID, class=0, class_mask=0;
  41. unsigned long driver_data=0;
  42. int fields=0;
  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. dynid = kzalloc(sizeof(*dynid), GFP_KERNEL);
  49. if (!dynid)
  50. return -ENOMEM;
  51. INIT_LIST_HEAD(&dynid->node);
  52. dynid->id.vendor = vendor;
  53. dynid->id.device = device;
  54. dynid->id.subvendor = subvendor;
  55. dynid->id.subdevice = subdevice;
  56. dynid->id.class = class;
  57. dynid->id.class_mask = class_mask;
  58. dynid->id.driver_data = pdrv->dynids.use_driver_data ?
  59. driver_data : 0UL;
  60. spin_lock(&pdrv->dynids.lock);
  61. list_add_tail(&pdrv->dynids.list, &dynid->node);
  62. spin_unlock(&pdrv->dynids.lock);
  63. if (get_driver(&pdrv->driver)) {
  64. driver_attach(&pdrv->driver);
  65. put_driver(&pdrv->driver);
  66. }
  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. * Depreciated, 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
  123. * PCI device id structure
  124. * @ids: array of PCI device id structures to search in
  125. * @dev: the PCI device structure to match against
  126. * @drv: the PCI driver to match against
  127. *
  128. * Used by a driver to check whether a PCI device present in the
  129. * system is in its list of supported devices. Returns the matching
  130. * pci_device_id structure or %NULL if there is no match.
  131. */
  132. const struct pci_device_id *pci_match_device(struct pci_driver *drv,
  133. struct pci_dev *dev)
  134. {
  135. const struct pci_device_id *id;
  136. struct pci_dynid *dynid;
  137. id = pci_match_id(drv->id_table, dev);
  138. if (id)
  139. return id;
  140. /* static ids didn't match, lets look at the dynamic ones */
  141. spin_lock(&drv->dynids.lock);
  142. list_for_each_entry(dynid, &drv->dynids.list, node) {
  143. if (pci_match_one_device(&dynid->id, dev)) {
  144. spin_unlock(&drv->dynids.lock);
  145. return &dynid->id;
  146. }
  147. }
  148. spin_unlock(&drv->dynids.lock);
  149. return NULL;
  150. }
  151. static int pci_call_probe(struct pci_driver *drv, struct pci_dev *dev,
  152. const struct pci_device_id *id)
  153. {
  154. int error;
  155. #ifdef CONFIG_NUMA
  156. /* Execute driver initialization on node where the
  157. device's bus is attached to. This way the driver likely
  158. allocates its local memory on the right node without
  159. any need to change it. */
  160. struct mempolicy *oldpol;
  161. cpumask_t oldmask = current->cpus_allowed;
  162. int node = pcibus_to_node(dev->bus);
  163. if (node >= 0 && node_online(node))
  164. set_cpus_allowed(current, node_to_cpumask(node));
  165. /* And set default memory allocation policy */
  166. oldpol = current->mempolicy;
  167. current->mempolicy = &default_policy;
  168. mpol_get(current->mempolicy);
  169. #endif
  170. error = drv->probe(dev, id);
  171. #ifdef CONFIG_NUMA
  172. set_cpus_allowed(current, oldmask);
  173. mpol_free(current->mempolicy);
  174. current->mempolicy = oldpol;
  175. #endif
  176. return error;
  177. }
  178. /**
  179. * __pci_device_probe()
  180. * @drv: driver to call to check if it wants the PCI device
  181. * @pci_dev: PCI device being probed
  182. *
  183. * returns 0 on success, else error.
  184. * side-effect: pci_dev->driver is set to drv when drv claims pci_dev.
  185. */
  186. static int
  187. __pci_device_probe(struct pci_driver *drv, struct pci_dev *pci_dev)
  188. {
  189. const struct pci_device_id *id;
  190. int error = 0;
  191. if (!pci_dev->driver && drv->probe) {
  192. error = -ENODEV;
  193. id = pci_match_device(drv, pci_dev);
  194. if (id)
  195. error = pci_call_probe(drv, pci_dev, id);
  196. if (error >= 0) {
  197. pci_dev->driver = drv;
  198. error = 0;
  199. }
  200. }
  201. return error;
  202. }
  203. static int pci_device_probe(struct device * dev)
  204. {
  205. int error = 0;
  206. struct pci_driver *drv;
  207. struct pci_dev *pci_dev;
  208. drv = to_pci_driver(dev->driver);
  209. pci_dev = to_pci_dev(dev);
  210. pci_dev_get(pci_dev);
  211. error = __pci_device_probe(drv, pci_dev);
  212. if (error)
  213. pci_dev_put(pci_dev);
  214. return error;
  215. }
  216. static int pci_device_remove(struct device * dev)
  217. {
  218. struct pci_dev * pci_dev = to_pci_dev(dev);
  219. struct pci_driver * drv = pci_dev->driver;
  220. if (drv) {
  221. if (drv->remove)
  222. drv->remove(pci_dev);
  223. pci_dev->driver = NULL;
  224. }
  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. return i;
  248. }
  249. /*
  250. * Default resume method for devices that have no driver provided resume,
  251. * or not even a driver at all.
  252. */
  253. static void pci_default_resume(struct pci_dev *pci_dev)
  254. {
  255. int retval;
  256. /* restore the PCI config space */
  257. pci_restore_state(pci_dev);
  258. /* if the device was enabled before suspend, reenable */
  259. if (pci_dev->is_enabled)
  260. retval = pci_enable_device(pci_dev);
  261. /* if the device was busmaster before the suspend, make it busmaster again */
  262. if (pci_dev->is_busmaster)
  263. pci_set_master(pci_dev);
  264. }
  265. static int pci_device_resume(struct device * dev)
  266. {
  267. struct pci_dev * pci_dev = to_pci_dev(dev);
  268. struct pci_driver * drv = pci_dev->driver;
  269. if (drv && drv->resume)
  270. drv->resume(pci_dev);
  271. else
  272. pci_default_resume(pci_dev);
  273. return 0;
  274. }
  275. static void pci_device_shutdown(struct device *dev)
  276. {
  277. struct pci_dev *pci_dev = to_pci_dev(dev);
  278. struct pci_driver *drv = pci_dev->driver;
  279. if (drv && drv->shutdown)
  280. drv->shutdown(pci_dev);
  281. }
  282. #define kobj_to_pci_driver(obj) container_of(obj, struct device_driver, kobj)
  283. #define attr_to_driver_attribute(obj) container_of(obj, struct driver_attribute, attr)
  284. static ssize_t
  285. pci_driver_attr_show(struct kobject * kobj, struct attribute *attr, char *buf)
  286. {
  287. struct device_driver *driver = kobj_to_pci_driver(kobj);
  288. struct driver_attribute *dattr = attr_to_driver_attribute(attr);
  289. ssize_t ret;
  290. if (!get_driver(driver))
  291. return -ENODEV;
  292. ret = dattr->show ? dattr->show(driver, buf) : -EIO;
  293. put_driver(driver);
  294. return ret;
  295. }
  296. static ssize_t
  297. pci_driver_attr_store(struct kobject * kobj, struct attribute *attr,
  298. const char *buf, size_t count)
  299. {
  300. struct device_driver *driver = kobj_to_pci_driver(kobj);
  301. struct driver_attribute *dattr = attr_to_driver_attribute(attr);
  302. ssize_t ret;
  303. if (!get_driver(driver))
  304. return -ENODEV;
  305. ret = dattr->store ? dattr->store(driver, buf, count) : -EIO;
  306. put_driver(driver);
  307. return ret;
  308. }
  309. static struct sysfs_ops pci_driver_sysfs_ops = {
  310. .show = pci_driver_attr_show,
  311. .store = pci_driver_attr_store,
  312. };
  313. static struct kobj_type pci_driver_kobj_type = {
  314. .sysfs_ops = &pci_driver_sysfs_ops,
  315. };
  316. /**
  317. * __pci_register_driver - register a new pci driver
  318. * @drv: the driver structure to register
  319. * @owner: owner module of drv
  320. *
  321. * Adds the driver structure to the list of registered drivers.
  322. * Returns a negative value on error, otherwise 0.
  323. * If no error occurred, the driver remains registered even if
  324. * no device was claimed during registration.
  325. */
  326. int __pci_register_driver(struct pci_driver *drv, struct module *owner)
  327. {
  328. int error;
  329. /* initialize common driver fields */
  330. drv->driver.name = drv->name;
  331. drv->driver.bus = &pci_bus_type;
  332. drv->driver.owner = owner;
  333. drv->driver.kobj.ktype = &pci_driver_kobj_type;
  334. spin_lock_init(&drv->dynids.lock);
  335. INIT_LIST_HEAD(&drv->dynids.list);
  336. /* register with core */
  337. error = driver_register(&drv->driver);
  338. if (!error)
  339. error = pci_create_newid_file(drv);
  340. return error;
  341. }
  342. /**
  343. * pci_unregister_driver - unregister a pci driver
  344. * @drv: the driver structure to unregister
  345. *
  346. * Deletes the driver structure from the list of registered PCI drivers,
  347. * gives it a chance to clean up by calling its remove() function for
  348. * each device it was responsible for, and marks those devices as
  349. * driverless.
  350. */
  351. void
  352. pci_unregister_driver(struct pci_driver *drv)
  353. {
  354. driver_unregister(&drv->driver);
  355. pci_free_dynids(drv);
  356. }
  357. static struct pci_driver pci_compat_driver = {
  358. .name = "compat"
  359. };
  360. /**
  361. * pci_dev_driver - get the pci_driver of a device
  362. * @dev: the device to query
  363. *
  364. * Returns the appropriate pci_driver structure or %NULL if there is no
  365. * registered driver for the device.
  366. */
  367. struct pci_driver *
  368. pci_dev_driver(const struct pci_dev *dev)
  369. {
  370. if (dev->driver)
  371. return dev->driver;
  372. else {
  373. int i;
  374. for(i=0; i<=PCI_ROM_RESOURCE; i++)
  375. if (dev->resource[i].flags & IORESOURCE_BUSY)
  376. return &pci_compat_driver;
  377. }
  378. return NULL;
  379. }
  380. /**
  381. * pci_bus_match - Tell if a PCI device structure has a matching PCI device id structure
  382. * @dev: the PCI device structure to match against
  383. * @drv: the device driver to search for matching PCI device id structures
  384. *
  385. * Used by a driver to check whether a PCI device present in the
  386. * system is in its list of supported devices. Returns the matching
  387. * pci_device_id structure or %NULL if there is no match.
  388. */
  389. static int pci_bus_match(struct device *dev, struct device_driver *drv)
  390. {
  391. struct pci_dev *pci_dev = to_pci_dev(dev);
  392. struct pci_driver *pci_drv = to_pci_driver(drv);
  393. const struct pci_device_id *found_id;
  394. found_id = pci_match_device(pci_drv, pci_dev);
  395. if (found_id)
  396. return 1;
  397. return 0;
  398. }
  399. /**
  400. * pci_dev_get - increments the reference count of the pci device structure
  401. * @dev: the device being referenced
  402. *
  403. * Each live reference to a device should be refcounted.
  404. *
  405. * Drivers for PCI devices should normally record such references in
  406. * their probe() methods, when they bind to a device, and release
  407. * them by calling pci_dev_put(), in their disconnect() methods.
  408. *
  409. * A pointer to the device with the incremented reference counter is returned.
  410. */
  411. struct pci_dev *pci_dev_get(struct pci_dev *dev)
  412. {
  413. if (dev)
  414. get_device(&dev->dev);
  415. return dev;
  416. }
  417. /**
  418. * pci_dev_put - release a use of the pci device structure
  419. * @dev: device that's been disconnected
  420. *
  421. * Must be called when a user of a device is finished with it. When the last
  422. * user of the device calls this function, the memory of the device is freed.
  423. */
  424. void pci_dev_put(struct pci_dev *dev)
  425. {
  426. if (dev)
  427. put_device(&dev->dev);
  428. }
  429. #ifndef CONFIG_HOTPLUG
  430. int pci_uevent(struct device *dev, char **envp, int num_envp,
  431. char *buffer, int buffer_size)
  432. {
  433. return -ENODEV;
  434. }
  435. #endif
  436. struct bus_type pci_bus_type = {
  437. .name = "pci",
  438. .match = pci_bus_match,
  439. .uevent = pci_uevent,
  440. .probe = pci_device_probe,
  441. .remove = pci_device_remove,
  442. .suspend = pci_device_suspend,
  443. .shutdown = pci_device_shutdown,
  444. .resume = pci_device_resume,
  445. .dev_attrs = pci_dev_attrs,
  446. };
  447. static int __init pci_driver_init(void)
  448. {
  449. return bus_register(&pci_bus_type);
  450. }
  451. postcore_initcall(pci_driver_init);
  452. EXPORT_SYMBOL(pci_match_id);
  453. EXPORT_SYMBOL(pci_match_device);
  454. EXPORT_SYMBOL(__pci_register_driver);
  455. EXPORT_SYMBOL(pci_unregister_driver);
  456. EXPORT_SYMBOL(pci_dev_driver);
  457. EXPORT_SYMBOL(pci_bus_type);
  458. EXPORT_SYMBOL(pci_dev_get);
  459. EXPORT_SYMBOL(pci_dev_put);