search.c 13 KB

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