search.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. /*
  2. * PCI searching functions.
  3. *
  4. * Copyright (C) 1993 -- 1997 Drew Eckhardt, Frederic Potter,
  5. * David Mosberger-Tang
  6. * Copyright (C) 1997 -- 2000 Martin Mares <mj@ucw.cz>
  7. * Copyright (C) 2003 -- 2004 Greg Kroah-Hartman <greg@kroah.com>
  8. */
  9. #include <linux/init.h>
  10. #include <linux/pci.h>
  11. #include <linux/module.h>
  12. #include <linux/interrupt.h>
  13. #include "pci.h"
  14. DECLARE_RWSEM(pci_bus_sem);
  15. static struct pci_bus * __devinit
  16. pci_do_find_bus(struct pci_bus* bus, unsigned char busnr)
  17. {
  18. struct pci_bus* child;
  19. struct list_head *tmp;
  20. if(bus->number == busnr)
  21. return bus;
  22. list_for_each(tmp, &bus->children) {
  23. child = pci_do_find_bus(pci_bus_b(tmp), busnr);
  24. if(child)
  25. return child;
  26. }
  27. return NULL;
  28. }
  29. /**
  30. * pci_find_bus - locate PCI bus from a given domain and bus number
  31. * @domain: number of PCI domain to search
  32. * @busnr: number of desired PCI bus
  33. *
  34. * Given a PCI bus number and domain number, the desired PCI bus is located
  35. * in the global list of PCI buses. If the bus is found, a pointer to its
  36. * data structure is returned. If no bus is found, %NULL is returned.
  37. */
  38. struct pci_bus * pci_find_bus(int domain, int busnr)
  39. {
  40. struct pci_bus *bus = NULL;
  41. struct pci_bus *tmp_bus;
  42. while ((bus = pci_find_next_bus(bus)) != NULL) {
  43. if (pci_domain_nr(bus) != domain)
  44. continue;
  45. tmp_bus = pci_do_find_bus(bus, busnr);
  46. if (tmp_bus)
  47. return tmp_bus;
  48. }
  49. return NULL;
  50. }
  51. /**
  52. * pci_find_next_bus - begin or continue searching for a PCI bus
  53. * @from: Previous PCI bus found, or %NULL for new search.
  54. *
  55. * Iterates through the list of known PCI busses. A new search is
  56. * initiated by passing %NULL as the @from argument. Otherwise if
  57. * @from is not %NULL, searches continue from next device on the
  58. * global list.
  59. */
  60. struct pci_bus *
  61. pci_find_next_bus(const struct pci_bus *from)
  62. {
  63. struct list_head *n;
  64. struct pci_bus *b = NULL;
  65. WARN_ON(in_interrupt());
  66. down_read(&pci_bus_sem);
  67. n = from ? from->node.next : pci_root_buses.next;
  68. if (n != &pci_root_buses)
  69. b = pci_bus_b(n);
  70. up_read(&pci_bus_sem);
  71. return b;
  72. }
  73. /**
  74. * pci_find_slot - locate PCI device from a given PCI slot
  75. * @bus: number of PCI bus on which desired PCI device resides
  76. * @devfn: encodes number of PCI slot in which the desired PCI
  77. * device resides and the logical device number within that slot
  78. * in case of multi-function devices.
  79. *
  80. * Given a PCI bus and slot/function number, the desired PCI device
  81. * is located in system global list of PCI devices. If the device
  82. * is found, a pointer to its data structure is returned. If no
  83. * device is found, %NULL is returned.
  84. */
  85. struct pci_dev *
  86. pci_find_slot(unsigned int bus, unsigned int devfn)
  87. {
  88. struct pci_dev *dev = NULL;
  89. while ((dev = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
  90. if (dev->bus->number == bus && dev->devfn == devfn)
  91. return dev;
  92. }
  93. return NULL;
  94. }
  95. /**
  96. * pci_get_slot - locate PCI device for a given PCI slot
  97. * @bus: PCI bus on which desired PCI device resides
  98. * @devfn: encodes number of PCI slot in which the desired PCI
  99. * device resides and the logical device number within that slot
  100. * in case of multi-function devices.
  101. *
  102. * Given a PCI bus and slot/function number, the desired PCI device
  103. * is located in the list of PCI devices.
  104. * If the device is found, its reference count is increased and this
  105. * function returns a pointer to its data structure. The caller must
  106. * decrement the reference count by calling pci_dev_put().
  107. * If no device is found, %NULL is returned.
  108. */
  109. struct pci_dev * pci_get_slot(struct pci_bus *bus, unsigned int devfn)
  110. {
  111. struct list_head *tmp;
  112. struct pci_dev *dev;
  113. WARN_ON(in_interrupt());
  114. down_read(&pci_bus_sem);
  115. list_for_each(tmp, &bus->devices) {
  116. dev = pci_dev_b(tmp);
  117. if (dev->devfn == devfn)
  118. goto out;
  119. }
  120. dev = NULL;
  121. out:
  122. pci_dev_get(dev);
  123. up_read(&pci_bus_sem);
  124. return dev;
  125. }
  126. /**
  127. * pci_get_bus_and_slot - locate PCI device from a given PCI slot
  128. * @bus: number of PCI bus on which desired PCI device resides
  129. * @devfn: encodes number of PCI slot in which the desired PCI
  130. * device resides and the logical device number within that slot
  131. * in case of multi-function devices.
  132. *
  133. * Given a PCI bus and slot/function number, the desired PCI device
  134. * is located in system global list of PCI devices. If the device
  135. * is found, a pointer to its data structure is returned. If no
  136. * device is found, %NULL is returned. The returned device has its
  137. * reference count bumped by one.
  138. */
  139. struct pci_dev * pci_get_bus_and_slot(unsigned int bus, unsigned int devfn)
  140. {
  141. struct pci_dev *dev = NULL;
  142. while ((dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
  143. if (dev->bus->number == bus && dev->devfn == devfn)
  144. return dev;
  145. }
  146. return NULL;
  147. }
  148. /**
  149. * pci_find_subsys - begin or continue searching for a PCI device by vendor/subvendor/device/subdevice id
  150. * @vendor: PCI vendor id to match, or %PCI_ANY_ID to match all vendor ids
  151. * @device: PCI device id to match, or %PCI_ANY_ID to match all device ids
  152. * @ss_vendor: PCI subsystem vendor id to match, or %PCI_ANY_ID to match all vendor ids
  153. * @ss_device: PCI subsystem device id to match, or %PCI_ANY_ID to match all device ids
  154. * @from: Previous PCI device found in search, or %NULL for new search.
  155. *
  156. * Iterates through the list of known PCI devices. If a PCI device is
  157. * found with a matching @vendor, @device, @ss_vendor and @ss_device, a
  158. * pointer to its device structure is returned. Otherwise, %NULL is returned.
  159. * A new search is initiated by passing %NULL as the @from argument.
  160. * Otherwise if @from is not %NULL, searches continue from next device
  161. * on the global list.
  162. *
  163. * NOTE: Do not use this function any more; use pci_get_subsys() instead, as
  164. * the PCI device returned by this function can disappear at any moment in
  165. * time.
  166. */
  167. static struct pci_dev * pci_find_subsys(unsigned int vendor,
  168. unsigned int device,
  169. unsigned int ss_vendor,
  170. unsigned int ss_device,
  171. const struct pci_dev *from)
  172. {
  173. struct list_head *n;
  174. struct pci_dev *dev;
  175. WARN_ON(in_interrupt());
  176. down_read(&pci_bus_sem);
  177. n = from ? from->global_list.next : pci_devices.next;
  178. while (n && (n != &pci_devices)) {
  179. dev = pci_dev_g(n);
  180. if ((vendor == PCI_ANY_ID || dev->vendor == vendor) &&
  181. (device == PCI_ANY_ID || dev->device == device) &&
  182. (ss_vendor == PCI_ANY_ID || dev->subsystem_vendor == ss_vendor) &&
  183. (ss_device == PCI_ANY_ID || dev->subsystem_device == ss_device))
  184. goto exit;
  185. n = n->next;
  186. }
  187. dev = NULL;
  188. exit:
  189. up_read(&pci_bus_sem);
  190. return dev;
  191. }
  192. /**
  193. * pci_find_device - begin or continue searching for a PCI device by vendor/device id
  194. * @vendor: PCI vendor id to match, or %PCI_ANY_ID to match all vendor ids
  195. * @device: PCI device id to match, or %PCI_ANY_ID to match all device ids
  196. * @from: Previous PCI device found in search, or %NULL for new search.
  197. *
  198. * Iterates through the list of known PCI devices. If a PCI device is found
  199. * with a matching @vendor and @device, a pointer to its device structure is
  200. * returned. Otherwise, %NULL is returned.
  201. * A new search is initiated by passing %NULL as the @from argument.
  202. * Otherwise if @from is not %NULL, searches continue from next device
  203. * on the global list.
  204. *
  205. * NOTE: Do not use this function any more; use pci_get_device() instead, as
  206. * the PCI device returned by this function can disappear at any moment in
  207. * time.
  208. */
  209. struct pci_dev *
  210. pci_find_device(unsigned int vendor, unsigned int device, const struct pci_dev *from)
  211. {
  212. return pci_find_subsys(vendor, device, PCI_ANY_ID, PCI_ANY_ID, from);
  213. }
  214. /**
  215. * pci_get_subsys - begin or continue searching for a PCI device by vendor/subvendor/device/subdevice id
  216. * @vendor: PCI vendor id to match, or %PCI_ANY_ID to match all vendor ids
  217. * @device: PCI device id to match, or %PCI_ANY_ID to match all device ids
  218. * @ss_vendor: PCI subsystem vendor id to match, or %PCI_ANY_ID to match all vendor ids
  219. * @ss_device: PCI subsystem device id to match, or %PCI_ANY_ID to match all device ids
  220. * @from: Previous PCI device found in search, or %NULL for new search.
  221. *
  222. * Iterates through the list of known PCI devices. If a PCI device is found
  223. * with a matching @vendor, @device, @ss_vendor and @ss_device, a pointer to its
  224. * device structure is returned, and the reference count to the device is
  225. * incremented. Otherwise, %NULL is returned. A new search is initiated by
  226. * passing %NULL as the @from argument. Otherwise if @from is not %NULL,
  227. * searches continue from next device on the global list.
  228. * The reference count for @from is always decremented if it is not %NULL.
  229. */
  230. struct pci_dev *
  231. pci_get_subsys(unsigned int vendor, unsigned int device,
  232. unsigned int ss_vendor, unsigned int ss_device,
  233. struct pci_dev *from)
  234. {
  235. struct list_head *n;
  236. struct pci_dev *dev;
  237. WARN_ON(in_interrupt());
  238. down_read(&pci_bus_sem);
  239. n = from ? from->global_list.next : pci_devices.next;
  240. while (n && (n != &pci_devices)) {
  241. dev = pci_dev_g(n);
  242. if ((vendor == PCI_ANY_ID || dev->vendor == vendor) &&
  243. (device == PCI_ANY_ID || dev->device == device) &&
  244. (ss_vendor == PCI_ANY_ID || dev->subsystem_vendor == ss_vendor) &&
  245. (ss_device == PCI_ANY_ID || dev->subsystem_device == ss_device))
  246. goto exit;
  247. n = n->next;
  248. }
  249. dev = NULL;
  250. exit:
  251. dev = pci_dev_get(dev);
  252. up_read(&pci_bus_sem);
  253. pci_dev_put(from);
  254. return dev;
  255. }
  256. /**
  257. * pci_get_device - begin or continue searching for a PCI device by vendor/device id
  258. * @vendor: PCI vendor id to match, or %PCI_ANY_ID to match all vendor ids
  259. * @device: PCI device id to match, or %PCI_ANY_ID to match all device ids
  260. * @from: Previous PCI device found in search, or %NULL for new search.
  261. *
  262. * Iterates through the list of known PCI devices. If a PCI device is
  263. * found with a matching @vendor and @device, the reference count to the
  264. * device is incremented and a pointer to its device structure is returned.
  265. * Otherwise, %NULL is returned. A new search is initiated by passing %NULL
  266. * as the @from argument. Otherwise if @from is not %NULL, searches continue
  267. * from next device on the global list. The reference count for @from is
  268. * always decremented if it is not %NULL.
  269. */
  270. struct pci_dev *
  271. pci_get_device(unsigned int vendor, unsigned int device, struct pci_dev *from)
  272. {
  273. return pci_get_subsys(vendor, device, PCI_ANY_ID, PCI_ANY_ID, from);
  274. }
  275. /**
  276. * pci_get_device_reverse - begin or continue searching for a PCI device by vendor/device id
  277. * @vendor: PCI vendor id to match, or %PCI_ANY_ID to match all vendor ids
  278. * @device: PCI device id to match, or %PCI_ANY_ID to match all device ids
  279. * @from: Previous PCI device found in search, or %NULL for new search.
  280. *
  281. * Iterates through the list of known PCI devices in the reverse order of
  282. * pci_get_device.
  283. * If a PCI device is found with a matching @vendor and @device, the reference
  284. * count to the device is incremented and a pointer to its device structure
  285. * is returned Otherwise, %NULL is returned. A new search is initiated by
  286. * passing %NULL as the @from argument. Otherwise if @from is not %NULL,
  287. * searches continue from next device on the global list. The reference
  288. * count for @from is always decremented if it is not %NULL.
  289. */
  290. struct pci_dev *
  291. pci_get_device_reverse(unsigned int vendor, unsigned int device, struct pci_dev *from)
  292. {
  293. struct list_head *n;
  294. struct pci_dev *dev;
  295. WARN_ON(in_interrupt());
  296. down_read(&pci_bus_sem);
  297. n = from ? from->global_list.prev : pci_devices.prev;
  298. while (n && (n != &pci_devices)) {
  299. dev = pci_dev_g(n);
  300. if ((vendor == PCI_ANY_ID || dev->vendor == vendor) &&
  301. (device == PCI_ANY_ID || dev->device == device))
  302. goto exit;
  303. n = n->prev;
  304. }
  305. dev = NULL;
  306. exit:
  307. dev = pci_dev_get(dev);
  308. up_read(&pci_bus_sem);
  309. pci_dev_put(from);
  310. return dev;
  311. }
  312. /**
  313. * pci_find_device_reverse - begin or continue searching for a PCI device by vendor/device id
  314. * @vendor: PCI vendor id to match, or %PCI_ANY_ID to match all vendor ids
  315. * @device: PCI device id to match, or %PCI_ANY_ID to match all device ids
  316. * @from: Previous PCI device found in search, or %NULL for new search.
  317. *
  318. * Iterates through the list of known PCI devices in the reverse order of
  319. * pci_find_device().
  320. * If a PCI device is found with a matching @vendor and @device, a pointer to
  321. * its device structure is returned. Otherwise, %NULL is returned.
  322. * A new search is initiated by passing %NULL as the @from argument.
  323. * Otherwise if @from is not %NULL, searches continue from previous device
  324. * on the global list.
  325. */
  326. struct pci_dev *
  327. pci_find_device_reverse(unsigned int vendor, unsigned int device, const struct pci_dev *from)
  328. {
  329. struct list_head *n;
  330. struct pci_dev *dev;
  331. WARN_ON(in_interrupt());
  332. down_read(&pci_bus_sem);
  333. n = from ? from->global_list.prev : pci_devices.prev;
  334. while (n && (n != &pci_devices)) {
  335. dev = pci_dev_g(n);
  336. if ((vendor == PCI_ANY_ID || dev->vendor == vendor) &&
  337. (device == PCI_ANY_ID || dev->device == device))
  338. goto exit;
  339. n = n->prev;
  340. }
  341. dev = NULL;
  342. exit:
  343. up_read(&pci_bus_sem);
  344. return dev;
  345. }
  346. /**
  347. * pci_get_class - begin or continue searching for a PCI device by class
  348. * @class: search for a PCI device with this class designation
  349. * @from: Previous PCI device found in search, or %NULL for new search.
  350. *
  351. * Iterates through the list of known PCI devices. If a PCI device is
  352. * found with a matching @class, the reference count to the device is
  353. * incremented and a pointer to its device structure is returned.
  354. * Otherwise, %NULL is returned.
  355. * A new search is initiated by passing %NULL as the @from argument.
  356. * Otherwise if @from is not %NULL, searches continue from next device
  357. * on the global list. The reference count for @from is always decremented
  358. * if it is not %NULL.
  359. */
  360. struct pci_dev *pci_get_class(unsigned int class, struct pci_dev *from)
  361. {
  362. struct list_head *n;
  363. struct pci_dev *dev;
  364. WARN_ON(in_interrupt());
  365. down_read(&pci_bus_sem);
  366. n = from ? from->global_list.next : pci_devices.next;
  367. while (n && (n != &pci_devices)) {
  368. dev = pci_dev_g(n);
  369. if (dev->class == class)
  370. goto exit;
  371. n = n->next;
  372. }
  373. dev = NULL;
  374. exit:
  375. dev = pci_dev_get(dev);
  376. up_read(&pci_bus_sem);
  377. pci_dev_put(from);
  378. return dev;
  379. }
  380. /**
  381. * pci_dev_present - Returns 1 if device matching the device list is present, 0 if not.
  382. * @ids: A pointer to a null terminated list of struct pci_device_id structures
  383. * that describe the type of PCI device the caller is trying to find.
  384. *
  385. * Obvious fact: You do not have a reference to any device that might be found
  386. * by this function, so if that device is removed from the system right after
  387. * this function is finished, the value will be stale. Use this function to
  388. * find devices that are usually built into a system, or for a general hint as
  389. * to if another device happens to be present at this specific moment in time.
  390. */
  391. int pci_dev_present(const struct pci_device_id *ids)
  392. {
  393. struct pci_dev *dev;
  394. int found = 0;
  395. WARN_ON(in_interrupt());
  396. down_read(&pci_bus_sem);
  397. while (ids->vendor || ids->subvendor || ids->class_mask) {
  398. list_for_each_entry(dev, &pci_devices, global_list) {
  399. if (pci_match_one_device(ids, dev)) {
  400. found = 1;
  401. goto exit;
  402. }
  403. }
  404. ids++;
  405. }
  406. exit:
  407. up_read(&pci_bus_sem);
  408. return found;
  409. }
  410. EXPORT_SYMBOL(pci_dev_present);
  411. EXPORT_SYMBOL(pci_find_device);
  412. EXPORT_SYMBOL(pci_find_device_reverse);
  413. EXPORT_SYMBOL(pci_find_slot);
  414. /* For boot time work */
  415. EXPORT_SYMBOL(pci_find_bus);
  416. EXPORT_SYMBOL(pci_find_next_bus);
  417. /* For everyone */
  418. EXPORT_SYMBOL(pci_get_device);
  419. EXPORT_SYMBOL(pci_get_device_reverse);
  420. EXPORT_SYMBOL(pci_get_subsys);
  421. EXPORT_SYMBOL(pci_get_slot);
  422. EXPORT_SYMBOL(pci_get_bus_and_slot);
  423. EXPORT_SYMBOL(pci_get_class);