search.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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_find_subsys - begin or continue searching for a PCI device by vendor/subvendor/device/subdevice id
  128. * @vendor: PCI vendor id to match, or %PCI_ANY_ID to match all vendor ids
  129. * @device: PCI device id to match, or %PCI_ANY_ID to match all device ids
  130. * @ss_vendor: PCI subsystem vendor id to match, or %PCI_ANY_ID to match all vendor ids
  131. * @ss_device: PCI subsystem device id to match, or %PCI_ANY_ID to match all device ids
  132. * @from: Previous PCI device found in search, or %NULL for new search.
  133. *
  134. * Iterates through the list of known PCI devices. If a PCI device is
  135. * found with a matching @vendor, @device, @ss_vendor and @ss_device, a
  136. * pointer to its device structure is returned. Otherwise, %NULL is returned.
  137. * A new search is initiated by passing %NULL as the @from argument.
  138. * Otherwise if @from is not %NULL, searches continue from next device
  139. * on the global list.
  140. *
  141. * NOTE: Do not use this function any more; use pci_get_subsys() instead, as
  142. * the PCI device returned by this function can disappear at any moment in
  143. * time.
  144. */
  145. static struct pci_dev * pci_find_subsys(unsigned int vendor,
  146. unsigned int device,
  147. unsigned int ss_vendor,
  148. unsigned int ss_device,
  149. const struct pci_dev *from)
  150. {
  151. struct list_head *n;
  152. struct pci_dev *dev;
  153. WARN_ON(in_interrupt());
  154. down_read(&pci_bus_sem);
  155. n = from ? from->global_list.next : pci_devices.next;
  156. while (n && (n != &pci_devices)) {
  157. dev = pci_dev_g(n);
  158. if ((vendor == PCI_ANY_ID || dev->vendor == vendor) &&
  159. (device == PCI_ANY_ID || dev->device == device) &&
  160. (ss_vendor == PCI_ANY_ID || dev->subsystem_vendor == ss_vendor) &&
  161. (ss_device == PCI_ANY_ID || dev->subsystem_device == ss_device))
  162. goto exit;
  163. n = n->next;
  164. }
  165. dev = NULL;
  166. exit:
  167. up_read(&pci_bus_sem);
  168. return dev;
  169. }
  170. /**
  171. * pci_find_device - begin or continue searching for a PCI device by vendor/device id
  172. * @vendor: PCI vendor id to match, or %PCI_ANY_ID to match all vendor ids
  173. * @device: PCI device id to match, or %PCI_ANY_ID to match all device ids
  174. * @from: Previous PCI device found in search, or %NULL for new search.
  175. *
  176. * Iterates through the list of known PCI devices. If a PCI device is found
  177. * with a matching @vendor and @device, a pointer to its device structure is
  178. * returned. Otherwise, %NULL is returned.
  179. * A new search is initiated by passing %NULL as the @from argument.
  180. * Otherwise if @from is not %NULL, searches continue from next device
  181. * on the global list.
  182. *
  183. * NOTE: Do not use this function any more; use pci_get_device() instead, as
  184. * the PCI device returned by this function can disappear at any moment in
  185. * time.
  186. */
  187. struct pci_dev *
  188. pci_find_device(unsigned int vendor, unsigned int device, const struct pci_dev *from)
  189. {
  190. return pci_find_subsys(vendor, device, PCI_ANY_ID, PCI_ANY_ID, from);
  191. }
  192. /**
  193. * pci_get_subsys - begin or continue searching for a PCI device by vendor/subvendor/device/subdevice 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. * @ss_vendor: PCI subsystem vendor id to match, or %PCI_ANY_ID to match all vendor ids
  197. * @ss_device: PCI subsystem device id to match, or %PCI_ANY_ID to match all device ids
  198. * @from: Previous PCI device found in search, or %NULL for new search.
  199. *
  200. * Iterates through the list of known PCI devices. If a PCI device is found
  201. * with a matching @vendor, @device, @ss_vendor and @ss_device, a pointer to its
  202. * device structure is returned, and the reference count to the device is
  203. * incremented. Otherwise, %NULL is returned. A new search is initiated by
  204. * passing %NULL as the @from argument. Otherwise if @from is not %NULL,
  205. * searches continue from next device on the global list.
  206. * The reference count for @from is always decremented if it is not %NULL.
  207. */
  208. struct pci_dev *
  209. pci_get_subsys(unsigned int vendor, unsigned int device,
  210. unsigned int ss_vendor, unsigned int ss_device,
  211. struct pci_dev *from)
  212. {
  213. struct list_head *n;
  214. struct pci_dev *dev;
  215. WARN_ON(in_interrupt());
  216. down_read(&pci_bus_sem);
  217. n = from ? from->global_list.next : pci_devices.next;
  218. while (n && (n != &pci_devices)) {
  219. dev = pci_dev_g(n);
  220. if ((vendor == PCI_ANY_ID || dev->vendor == vendor) &&
  221. (device == PCI_ANY_ID || dev->device == device) &&
  222. (ss_vendor == PCI_ANY_ID || dev->subsystem_vendor == ss_vendor) &&
  223. (ss_device == PCI_ANY_ID || dev->subsystem_device == ss_device))
  224. goto exit;
  225. n = n->next;
  226. }
  227. dev = NULL;
  228. exit:
  229. dev = pci_dev_get(dev);
  230. up_read(&pci_bus_sem);
  231. pci_dev_put(from);
  232. return dev;
  233. }
  234. /**
  235. * pci_get_device - begin or continue searching for a PCI device by vendor/device id
  236. * @vendor: PCI vendor id to match, or %PCI_ANY_ID to match all vendor ids
  237. * @device: PCI device id to match, or %PCI_ANY_ID to match all device ids
  238. * @from: Previous PCI device found in search, or %NULL for new search.
  239. *
  240. * Iterates through the list of known PCI devices. If a PCI device is
  241. * found with a matching @vendor and @device, the reference count to the
  242. * device is incremented and a pointer to its device structure is returned.
  243. * Otherwise, %NULL is returned. A new search is initiated by passing %NULL
  244. * as the @from argument. Otherwise if @from is not %NULL, searches continue
  245. * from next device on the global list. The reference count for @from is
  246. * always decremented if it is not %NULL.
  247. */
  248. struct pci_dev *
  249. pci_get_device(unsigned int vendor, unsigned int device, struct pci_dev *from)
  250. {
  251. return pci_get_subsys(vendor, device, PCI_ANY_ID, PCI_ANY_ID, from);
  252. }
  253. /**
  254. * pci_find_device_reverse - begin or continue searching for a PCI device by vendor/device id
  255. * @vendor: PCI vendor id to match, or %PCI_ANY_ID to match all vendor ids
  256. * @device: PCI device id to match, or %PCI_ANY_ID to match all device ids
  257. * @from: Previous PCI device found in search, or %NULL for new search.
  258. *
  259. * Iterates through the list of known PCI devices in the reverse order of
  260. * pci_find_device().
  261. * If a PCI device is found with a matching @vendor and @device, a pointer to
  262. * its device structure is returned. Otherwise, %NULL is returned.
  263. * A new search is initiated by passing %NULL as the @from argument.
  264. * Otherwise if @from is not %NULL, searches continue from previous device
  265. * on the global list.
  266. */
  267. struct pci_dev *
  268. pci_find_device_reverse(unsigned int vendor, unsigned int device, const struct pci_dev *from)
  269. {
  270. struct list_head *n;
  271. struct pci_dev *dev;
  272. WARN_ON(in_interrupt());
  273. down_read(&pci_bus_sem);
  274. n = from ? from->global_list.prev : pci_devices.prev;
  275. while (n && (n != &pci_devices)) {
  276. dev = pci_dev_g(n);
  277. if ((vendor == PCI_ANY_ID || dev->vendor == vendor) &&
  278. (device == PCI_ANY_ID || dev->device == device))
  279. goto exit;
  280. n = n->prev;
  281. }
  282. dev = NULL;
  283. exit:
  284. up_read(&pci_bus_sem);
  285. return dev;
  286. }
  287. /**
  288. * pci_get_class - begin or continue searching for a PCI device by class
  289. * @class: search for a PCI device with this class designation
  290. * @from: Previous PCI device found in search, or %NULL for new search.
  291. *
  292. * Iterates through the list of known PCI devices. If a PCI device is
  293. * found with a matching @class, the reference count to the device is
  294. * incremented and a pointer to its device structure is returned.
  295. * Otherwise, %NULL is returned.
  296. * A new search is initiated by passing %NULL as the @from argument.
  297. * Otherwise if @from is not %NULL, searches continue from next device
  298. * on the global list. The reference count for @from is always decremented
  299. * if it is not %NULL.
  300. */
  301. struct pci_dev *pci_get_class(unsigned int class, struct pci_dev *from)
  302. {
  303. struct list_head *n;
  304. struct pci_dev *dev;
  305. WARN_ON(in_interrupt());
  306. down_read(&pci_bus_sem);
  307. n = from ? from->global_list.next : pci_devices.next;
  308. while (n && (n != &pci_devices)) {
  309. dev = pci_dev_g(n);
  310. if (dev->class == class)
  311. goto exit;
  312. n = n->next;
  313. }
  314. dev = NULL;
  315. exit:
  316. dev = pci_dev_get(dev);
  317. up_read(&pci_bus_sem);
  318. pci_dev_put(from);
  319. return dev;
  320. }
  321. /**
  322. * pci_dev_present - Returns 1 if device matching the device list is present, 0 if not.
  323. * @ids: A pointer to a null terminated list of struct pci_device_id structures
  324. * that describe the type of PCI device the caller is trying to find.
  325. *
  326. * Obvious fact: You do not have a reference to any device that might be found
  327. * by this function, so if that device is removed from the system right after
  328. * this function is finished, the value will be stale. Use this function to
  329. * find devices that are usually built into a system, or for a general hint as
  330. * to if another device happens to be present at this specific moment in time.
  331. */
  332. int pci_dev_present(const struct pci_device_id *ids)
  333. {
  334. struct pci_dev *dev;
  335. int found = 0;
  336. WARN_ON(in_interrupt());
  337. down_read(&pci_bus_sem);
  338. while (ids->vendor || ids->subvendor || ids->class_mask) {
  339. list_for_each_entry(dev, &pci_devices, global_list) {
  340. if (pci_match_one_device(ids, dev)) {
  341. found = 1;
  342. goto exit;
  343. }
  344. }
  345. ids++;
  346. }
  347. exit:
  348. up_read(&pci_bus_sem);
  349. return found;
  350. }
  351. EXPORT_SYMBOL(pci_dev_present);
  352. EXPORT_SYMBOL(pci_find_bus);
  353. EXPORT_SYMBOL(pci_find_next_bus);
  354. EXPORT_SYMBOL(pci_find_device);
  355. EXPORT_SYMBOL(pci_find_device_reverse);
  356. EXPORT_SYMBOL(pci_find_slot);
  357. EXPORT_SYMBOL(pci_get_device);
  358. EXPORT_SYMBOL(pci_get_subsys);
  359. EXPORT_SYMBOL(pci_get_slot);
  360. EXPORT_SYMBOL(pci_get_class);