pci-driver.c 13 KB

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