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