Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Bjorn Helgaas | f762598 | 2013-11-14 11:28:18 -0700 | [diff] [blame] | 2 | * PCI searching functions. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 12 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <linux/module.h> |
| 14 | #include <linux/interrupt.h> |
| 15 | #include "pci.h" |
| 16 | |
Zhang Yanmin | d71374d | 2006-06-02 12:35:43 +0800 | [diff] [blame] | 17 | DECLARE_RWSEM(pci_bus_sem); |
Amos Kong | ce29ca3 | 2012-05-23 10:20:35 -0600 | [diff] [blame] | 18 | EXPORT_SYMBOL_GPL(pci_bus_sem); |
| 19 | |
Keshavamurthy, Anil S | 994a65e | 2007-10-21 16:41:46 -0700 | [diff] [blame] | 20 | /* |
Alex Williamson | c25dc82 | 2014-05-22 17:07:30 -0600 | [diff] [blame^] | 21 | * pci_for_each_dma_alias - Iterate over DMA aliases for a device |
| 22 | * @pdev: starting downstream device |
| 23 | * @fn: function to call for each alias |
| 24 | * @data: opaque data to pass to @fn |
| 25 | * |
| 26 | * Starting @pdev, walk up the bus calling @fn for each possible alias |
| 27 | * of @pdev at the root bus. |
| 28 | */ |
| 29 | int pci_for_each_dma_alias(struct pci_dev *pdev, |
| 30 | int (*fn)(struct pci_dev *pdev, |
| 31 | u16 alias, void *data), void *data) |
| 32 | { |
| 33 | struct pci_bus *bus; |
| 34 | int ret; |
| 35 | |
| 36 | ret = fn(pdev, PCI_DEVID(pdev->bus->number, pdev->devfn), data); |
| 37 | if (ret) |
| 38 | return ret; |
| 39 | |
| 40 | for (bus = pdev->bus; !pci_is_root_bus(bus); bus = bus->parent) { |
| 41 | struct pci_dev *tmp; |
| 42 | |
| 43 | /* Skip virtual buses */ |
| 44 | if (!bus->self) |
| 45 | continue; |
| 46 | |
| 47 | tmp = bus->self; |
| 48 | |
| 49 | /* |
| 50 | * PCIe-to-PCI/X bridges alias transactions from downstream |
| 51 | * devices using the subordinate bus number (PCI Express to |
| 52 | * PCI/PCI-X Bridge Spec, rev 1.0, sec 2.3). For all cases |
| 53 | * where the upstream bus is PCI/X we alias to the bridge |
| 54 | * (there are various conditions in the previous reference |
| 55 | * where the bridge may take ownership of transactions, even |
| 56 | * when the secondary interface is PCI-X). |
| 57 | */ |
| 58 | if (pci_is_pcie(tmp)) { |
| 59 | switch (pci_pcie_type(tmp)) { |
| 60 | case PCI_EXP_TYPE_ROOT_PORT: |
| 61 | case PCI_EXP_TYPE_UPSTREAM: |
| 62 | case PCI_EXP_TYPE_DOWNSTREAM: |
| 63 | continue; |
| 64 | case PCI_EXP_TYPE_PCI_BRIDGE: |
| 65 | ret = fn(tmp, |
| 66 | PCI_DEVID(tmp->subordinate->number, |
| 67 | PCI_DEVFN(0, 0)), data); |
| 68 | if (ret) |
| 69 | return ret; |
| 70 | continue; |
| 71 | case PCI_EXP_TYPE_PCIE_BRIDGE: |
| 72 | ret = fn(tmp, |
| 73 | PCI_DEVID(tmp->bus->number, |
| 74 | tmp->devfn), data); |
| 75 | if (ret) |
| 76 | return ret; |
| 77 | continue; |
| 78 | } |
| 79 | } else { |
| 80 | ret = fn(tmp, PCI_DEVID(tmp->bus->number, tmp->devfn), |
| 81 | data); |
| 82 | if (ret) |
| 83 | return ret; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | return ret; |
| 88 | } |
| 89 | |
| 90 | /* |
Stefan Assmann | 45e829e | 2009-12-03 06:49:24 -0500 | [diff] [blame] | 91 | * find the upstream PCIe-to-PCI bridge of a PCI device |
Keshavamurthy, Anil S | 994a65e | 2007-10-21 16:41:46 -0700 | [diff] [blame] | 92 | * if the device is PCIE, return NULL |
Stefan Assmann | 45e829e | 2009-12-03 06:49:24 -0500 | [diff] [blame] | 93 | * if the device isn't connected to a PCIe bridge (that is its parent is a |
Keshavamurthy, Anil S | 994a65e | 2007-10-21 16:41:46 -0700 | [diff] [blame] | 94 | * legacy PCI bridge and the bridge is directly connected to bus 0), return its |
| 95 | * parent |
| 96 | */ |
| 97 | struct pci_dev * |
| 98 | pci_find_upstream_pcie_bridge(struct pci_dev *pdev) |
| 99 | { |
| 100 | struct pci_dev *tmp = NULL; |
| 101 | |
Kenji Kaneshige | 5f4d91a | 2009-11-11 14:36:17 +0900 | [diff] [blame] | 102 | if (pci_is_pcie(pdev)) |
Keshavamurthy, Anil S | 994a65e | 2007-10-21 16:41:46 -0700 | [diff] [blame] | 103 | return NULL; |
| 104 | while (1) { |
Kenji Kaneshige | 6e3f36d | 2009-05-26 16:06:10 +0900 | [diff] [blame] | 105 | if (pci_is_root_bus(pdev->bus)) |
Keshavamurthy, Anil S | 994a65e | 2007-10-21 16:41:46 -0700 | [diff] [blame] | 106 | break; |
| 107 | pdev = pdev->bus->self; |
| 108 | /* a p2p bridge */ |
Kenji Kaneshige | 5f4d91a | 2009-11-11 14:36:17 +0900 | [diff] [blame] | 109 | if (!pci_is_pcie(pdev)) { |
Keshavamurthy, Anil S | 994a65e | 2007-10-21 16:41:46 -0700 | [diff] [blame] | 110 | tmp = pdev; |
| 111 | continue; |
| 112 | } |
Stefan Assmann | 45e829e | 2009-12-03 06:49:24 -0500 | [diff] [blame] | 113 | /* PCI device should connect to a PCIe bridge */ |
Yijing Wang | 62f87c0 | 2012-07-24 17:20:03 +0800 | [diff] [blame] | 114 | if (pci_pcie_type(pdev) != PCI_EXP_TYPE_PCI_BRIDGE) { |
Keshavamurthy, Anil S | 994a65e | 2007-10-21 16:41:46 -0700 | [diff] [blame] | 115 | /* Busted hardware? */ |
| 116 | WARN_ON_ONCE(1); |
| 117 | return NULL; |
| 118 | } |
| 119 | return pdev; |
| 120 | } |
| 121 | |
| 122 | return tmp; |
| 123 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | |
Sam Ravnborg | 96bde06 | 2007-03-26 21:53:30 -0800 | [diff] [blame] | 125 | static struct pci_bus *pci_do_find_bus(struct pci_bus *bus, unsigned char busnr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | { |
Yijing Wang | 94e6a9b | 2014-02-13 21:14:03 +0800 | [diff] [blame] | 127 | struct pci_bus *child; |
| 128 | struct pci_bus *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | |
| 130 | if(bus->number == busnr) |
| 131 | return bus; |
| 132 | |
Yijing Wang | 94e6a9b | 2014-02-13 21:14:03 +0800 | [diff] [blame] | 133 | list_for_each_entry(tmp, &bus->children, node) { |
| 134 | child = pci_do_find_bus(tmp, busnr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | if(child) |
| 136 | return child; |
| 137 | } |
| 138 | return NULL; |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * pci_find_bus - locate PCI bus from a given domain and bus number |
| 143 | * @domain: number of PCI domain to search |
| 144 | * @busnr: number of desired PCI bus |
| 145 | * |
| 146 | * Given a PCI bus number and domain number, the desired PCI bus is located |
| 147 | * in the global list of PCI buses. If the bus is found, a pointer to its |
| 148 | * data structure is returned. If no bus is found, %NULL is returned. |
| 149 | */ |
Randy Dunlap | c8439cfc | 2006-07-18 14:33:16 -0700 | [diff] [blame] | 150 | struct pci_bus * pci_find_bus(int domain, int busnr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | { |
| 152 | struct pci_bus *bus = NULL; |
| 153 | struct pci_bus *tmp_bus; |
| 154 | |
| 155 | while ((bus = pci_find_next_bus(bus)) != NULL) { |
| 156 | if (pci_domain_nr(bus) != domain) |
| 157 | continue; |
| 158 | tmp_bus = pci_do_find_bus(bus, busnr); |
| 159 | if (tmp_bus) |
| 160 | return tmp_bus; |
| 161 | } |
| 162 | return NULL; |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * pci_find_next_bus - begin or continue searching for a PCI bus |
| 167 | * @from: Previous PCI bus found, or %NULL for new search. |
| 168 | * |
Bjorn Helgaas | f762598 | 2013-11-14 11:28:18 -0700 | [diff] [blame] | 169 | * Iterates through the list of known PCI buses. A new search is |
Randy Dunlap | d75763d | 2006-07-30 03:03:41 -0700 | [diff] [blame] | 170 | * initiated by passing %NULL as the @from argument. Otherwise if |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | * @from is not %NULL, searches continue from next device on the |
| 172 | * global list. |
| 173 | */ |
Bjorn Helgaas | f762598 | 2013-11-14 11:28:18 -0700 | [diff] [blame] | 174 | struct pci_bus * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | pci_find_next_bus(const struct pci_bus *from) |
| 176 | { |
| 177 | struct list_head *n; |
| 178 | struct pci_bus *b = NULL; |
| 179 | |
| 180 | WARN_ON(in_interrupt()); |
Zhang Yanmin | d71374d | 2006-06-02 12:35:43 +0800 | [diff] [blame] | 181 | down_read(&pci_bus_sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | n = from ? from->node.next : pci_root_buses.next; |
| 183 | if (n != &pci_root_buses) |
Yijing Wang | 94e6a9b | 2014-02-13 21:14:03 +0800 | [diff] [blame] | 184 | b = list_entry(n, struct pci_bus, node); |
Zhang Yanmin | d71374d | 2006-06-02 12:35:43 +0800 | [diff] [blame] | 185 | up_read(&pci_bus_sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | return b; |
| 187 | } |
| 188 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | /** |
| 190 | * pci_get_slot - locate PCI device for a given PCI slot |
| 191 | * @bus: PCI bus on which desired PCI device resides |
Bjorn Helgaas | f762598 | 2013-11-14 11:28:18 -0700 | [diff] [blame] | 192 | * @devfn: encodes number of PCI slot in which the desired PCI |
| 193 | * device resides and the logical device number within that slot |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | * in case of multi-function devices. |
| 195 | * |
Bjorn Helgaas | f762598 | 2013-11-14 11:28:18 -0700 | [diff] [blame] | 196 | * Given a PCI bus and slot/function number, the desired PCI device |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | * is located in the list of PCI devices. |
| 198 | * If the device is found, its reference count is increased and this |
| 199 | * function returns a pointer to its data structure. The caller must |
| 200 | * decrement the reference count by calling pci_dev_put(). |
| 201 | * If no device is found, %NULL is returned. |
| 202 | */ |
Bjorn Helgaas | 66455f5 | 2012-08-17 15:53:27 -0600 | [diff] [blame] | 203 | struct pci_dev *pci_get_slot(struct pci_bus *bus, unsigned int devfn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | struct pci_dev *dev; |
| 206 | |
| 207 | WARN_ON(in_interrupt()); |
Zhang Yanmin | d71374d | 2006-06-02 12:35:43 +0800 | [diff] [blame] | 208 | down_read(&pci_bus_sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | |
Bjorn Helgaas | 66455f5 | 2012-08-17 15:53:27 -0600 | [diff] [blame] | 210 | list_for_each_entry(dev, &bus->devices, bus_list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | if (dev->devfn == devfn) |
| 212 | goto out; |
| 213 | } |
| 214 | |
| 215 | dev = NULL; |
| 216 | out: |
| 217 | pci_dev_get(dev); |
Zhang Yanmin | d71374d | 2006-06-02 12:35:43 +0800 | [diff] [blame] | 218 | up_read(&pci_bus_sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | return dev; |
| 220 | } |
| 221 | |
| 222 | /** |
Andrew Patterson | 3c299dc | 2009-10-12 13:14:00 -0600 | [diff] [blame] | 223 | * pci_get_domain_bus_and_slot - locate PCI device for a given PCI domain (segment), bus, and slot |
| 224 | * @domain: PCI domain/segment on which the PCI device resides. |
| 225 | * @bus: PCI bus on which desired PCI device resides |
| 226 | * @devfn: encodes number of PCI slot in which the desired PCI device |
| 227 | * resides and the logical device number within that slot in case of |
| 228 | * multi-function devices. |
Alan Cox | 29f3eb6 | 2006-10-16 16:20:21 -0700 | [diff] [blame] | 229 | * |
Andrew Patterson | 3c299dc | 2009-10-12 13:14:00 -0600 | [diff] [blame] | 230 | * Given a PCI domain, bus, and slot/function number, the desired PCI |
| 231 | * device is located in the list of PCI devices. If the device is |
| 232 | * found, its reference count is increased and this function returns a |
| 233 | * pointer to its data structure. The caller must decrement the |
| 234 | * reference count by calling pci_dev_put(). If no device is found, |
| 235 | * %NULL is returned. |
Alan Cox | 29f3eb6 | 2006-10-16 16:20:21 -0700 | [diff] [blame] | 236 | */ |
Andrew Patterson | 3c299dc | 2009-10-12 13:14:00 -0600 | [diff] [blame] | 237 | struct pci_dev *pci_get_domain_bus_and_slot(int domain, unsigned int bus, |
| 238 | unsigned int devfn) |
Alan Cox | 29f3eb6 | 2006-10-16 16:20:21 -0700 | [diff] [blame] | 239 | { |
| 240 | struct pci_dev *dev = NULL; |
| 241 | |
Kulikov Vasiliy | 4e344b1 | 2010-07-03 20:04:39 +0400 | [diff] [blame] | 242 | for_each_pci_dev(dev) { |
Andrew Patterson | 3c299dc | 2009-10-12 13:14:00 -0600 | [diff] [blame] | 243 | if (pci_domain_nr(dev->bus) == domain && |
| 244 | (dev->bus->number == bus && dev->devfn == devfn)) |
Alan Cox | 29f3eb6 | 2006-10-16 16:20:21 -0700 | [diff] [blame] | 245 | return dev; |
| 246 | } |
| 247 | return NULL; |
| 248 | } |
Andrew Patterson | 3c299dc | 2009-10-12 13:14:00 -0600 | [diff] [blame] | 249 | EXPORT_SYMBOL(pci_get_domain_bus_and_slot); |
Alan Cox | 29f3eb6 | 2006-10-16 16:20:21 -0700 | [diff] [blame] | 250 | |
Greg Kroah-Hartman | 95247b5 | 2008-02-13 11:03:58 -0800 | [diff] [blame] | 251 | static int match_pci_dev_by_id(struct device *dev, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | { |
Greg Kroah-Hartman | 95247b5 | 2008-02-13 11:03:58 -0800 | [diff] [blame] | 253 | struct pci_dev *pdev = to_pci_dev(dev); |
| 254 | struct pci_device_id *id = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | |
Greg Kroah-Hartman | 95247b5 | 2008-02-13 11:03:58 -0800 | [diff] [blame] | 256 | if (pci_match_one_device(id, pdev)) |
| 257 | return 1; |
| 258 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | } |
| 260 | |
Greg Kroah-Hartman | 95247b5 | 2008-02-13 11:03:58 -0800 | [diff] [blame] | 261 | /* |
| 262 | * pci_get_dev_by_id - begin or continue searching for a PCI device by id |
| 263 | * @id: pointer to struct pci_device_id to match for the device |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | * @from: Previous PCI device found in search, or %NULL for new search. |
| 265 | * |
Randy Dunlap | d75763d | 2006-07-30 03:03:41 -0700 | [diff] [blame] | 266 | * Iterates through the list of known PCI devices. If a PCI device is found |
Greg Kroah-Hartman | 95247b5 | 2008-02-13 11:03:58 -0800 | [diff] [blame] | 267 | * with a matching id a pointer to its device structure is returned, and the |
| 268 | * reference count to the device is incremented. Otherwise, %NULL is returned. |
| 269 | * A new search is initiated by passing %NULL as the @from argument. Otherwise |
| 270 | * if @from is not %NULL, searches continue from next device on the global |
| 271 | * list. The reference count for @from is always decremented if it is not |
| 272 | * %NULL. |
| 273 | * |
| 274 | * This is an internal function for use by the other search functions in |
| 275 | * this file. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | */ |
Greg Kroah-Hartman | 95247b5 | 2008-02-13 11:03:58 -0800 | [diff] [blame] | 277 | static struct pci_dev *pci_get_dev_by_id(const struct pci_device_id *id, |
Greg KH | b08508c | 2008-08-26 08:20:34 -0700 | [diff] [blame] | 278 | struct pci_dev *from) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | { |
Greg Kroah-Hartman | 95247b5 | 2008-02-13 11:03:58 -0800 | [diff] [blame] | 280 | struct device *dev; |
| 281 | struct device *dev_start = NULL; |
| 282 | struct pci_dev *pdev = NULL; |
| 283 | |
| 284 | WARN_ON(in_interrupt()); |
Matthew Wilcox | c4ed02f | 2008-10-20 19:13:08 -0600 | [diff] [blame] | 285 | if (from) |
| 286 | dev_start = &from->dev; |
Greg Kroah-Hartman | 95247b5 | 2008-02-13 11:03:58 -0800 | [diff] [blame] | 287 | dev = bus_find_device(&pci_bus_type, dev_start, (void *)id, |
| 288 | match_pci_dev_by_id); |
| 289 | if (dev) |
| 290 | pdev = to_pci_dev(dev); |
Greg KH | ebca4f1 | 2008-08-21 13:47:58 -0700 | [diff] [blame] | 291 | if (from) |
| 292 | pci_dev_put(from); |
Greg Kroah-Hartman | 95247b5 | 2008-02-13 11:03:58 -0800 | [diff] [blame] | 293 | return pdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | /** |
| 297 | * pci_get_subsys - begin or continue searching for a PCI device by vendor/subvendor/device/subdevice id |
| 298 | * @vendor: PCI vendor id to match, or %PCI_ANY_ID to match all vendor ids |
| 299 | * @device: PCI device id to match, or %PCI_ANY_ID to match all device ids |
| 300 | * @ss_vendor: PCI subsystem vendor id to match, or %PCI_ANY_ID to match all vendor ids |
| 301 | * @ss_device: PCI subsystem device id to match, or %PCI_ANY_ID to match all device ids |
| 302 | * @from: Previous PCI device found in search, or %NULL for new search. |
| 303 | * |
Randy Dunlap | d75763d | 2006-07-30 03:03:41 -0700 | [diff] [blame] | 304 | * Iterates through the list of known PCI devices. If a PCI device is found |
| 305 | * 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] | 306 | * device structure is returned, and the reference count to the device is |
| 307 | * incremented. Otherwise, %NULL is returned. A new search is initiated by |
Randy Dunlap | d75763d | 2006-07-30 03:03:41 -0700 | [diff] [blame] | 308 | * passing %NULL as the @from argument. Otherwise if @from is not %NULL, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | * searches continue from next device on the global list. |
| 310 | * The reference count for @from is always decremented if it is not %NULL. |
| 311 | */ |
Greg Kroah-Hartman | 95247b5 | 2008-02-13 11:03:58 -0800 | [diff] [blame] | 312 | struct pci_dev *pci_get_subsys(unsigned int vendor, unsigned int device, |
| 313 | unsigned int ss_vendor, unsigned int ss_device, |
Greg KH | b08508c | 2008-08-26 08:20:34 -0700 | [diff] [blame] | 314 | struct pci_dev *from) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | { |
Feng Tang | b9443f4 | 2012-08-23 15:45:03 +0800 | [diff] [blame] | 316 | struct pci_device_id id = { |
| 317 | .vendor = vendor, |
| 318 | .device = device, |
| 319 | .subvendor = ss_vendor, |
| 320 | .subdevice = ss_device, |
| 321 | }; |
Ard van Breemen | 6ae4adf | 2007-01-05 16:36:21 -0800 | [diff] [blame] | 322 | |
Feng Tang | b9443f4 | 2012-08-23 15:45:03 +0800 | [diff] [blame] | 323 | return pci_get_dev_by_id(&id, from); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | /** |
| 327 | * pci_get_device - begin or continue searching for a PCI device by vendor/device id |
| 328 | * @vendor: PCI vendor id to match, or %PCI_ANY_ID to match all vendor ids |
| 329 | * @device: PCI device id to match, or %PCI_ANY_ID to match all device ids |
| 330 | * @from: Previous PCI device found in search, or %NULL for new search. |
| 331 | * |
| 332 | * Iterates through the list of known PCI devices. If a PCI device is |
| 333 | * found with a matching @vendor and @device, the reference count to the |
| 334 | * device is incremented and a pointer to its device structure is returned. |
| 335 | * Otherwise, %NULL is returned. A new search is initiated by passing %NULL |
Randy Dunlap | d75763d | 2006-07-30 03:03:41 -0700 | [diff] [blame] | 336 | * as the @from argument. Otherwise if @from is not %NULL, searches continue |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | * from next device on the global list. The reference count for @from is |
| 338 | * always decremented if it is not %NULL. |
| 339 | */ |
| 340 | struct pci_dev * |
| 341 | pci_get_device(unsigned int vendor, unsigned int device, struct pci_dev *from) |
| 342 | { |
| 343 | return pci_get_subsys(vendor, device, PCI_ANY_ID, PCI_ANY_ID, from); |
| 344 | } |
| 345 | |
Alan Cox | 29f3eb6 | 2006-10-16 16:20:21 -0700 | [diff] [blame] | 346 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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. |
Randy Dunlap | d75763d | 2006-07-30 03:03:41 -0700 | [diff] [blame] | 355 | * A new search is initiated by passing %NULL as the @from argument. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | { |
Feng Tang | b9443f4 | 2012-08-23 15:45:03 +0800 | [diff] [blame] | 362 | struct pci_device_id id = { |
| 363 | .vendor = PCI_ANY_ID, |
| 364 | .device = PCI_ANY_ID, |
| 365 | .subvendor = PCI_ANY_ID, |
| 366 | .subdevice = PCI_ANY_ID, |
| 367 | .class_mask = PCI_ANY_ID, |
| 368 | .class = class, |
| 369 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | |
Feng Tang | b9443f4 | 2012-08-23 15:45:03 +0800 | [diff] [blame] | 371 | return pci_get_dev_by_id(&id, from); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | } |
| 373 | |
Greg Kroah-Hartman | 448432c | 2008-02-12 13:36:20 -0800 | [diff] [blame] | 374 | /** |
| 375 | * pci_dev_present - Returns 1 if device matching the device list is present, 0 if not. |
| 376 | * @ids: A pointer to a null terminated list of struct pci_device_id structures |
| 377 | * that describe the type of PCI device the caller is trying to find. |
| 378 | * |
| 379 | * Obvious fact: You do not have a reference to any device that might be found |
| 380 | * by this function, so if that device is removed from the system right after |
| 381 | * this function is finished, the value will be stale. Use this function to |
| 382 | * find devices that are usually built into a system, or for a general hint as |
| 383 | * to if another device happens to be present at this specific moment in time. |
| 384 | */ |
| 385 | int pci_dev_present(const struct pci_device_id *ids) |
Alan Cox | d86f90f | 2006-12-04 15:14:44 -0800 | [diff] [blame] | 386 | { |
Greg Kroah-Hartman | 95247b5 | 2008-02-13 11:03:58 -0800 | [diff] [blame] | 387 | struct pci_dev *found = NULL; |
Alan Cox | d86f90f | 2006-12-04 15:14:44 -0800 | [diff] [blame] | 388 | |
| 389 | WARN_ON(in_interrupt()); |
Alan Cox | d86f90f | 2006-12-04 15:14:44 -0800 | [diff] [blame] | 390 | while (ids->vendor || ids->subvendor || ids->class_mask) { |
Greg Kroah-Hartman | 95247b5 | 2008-02-13 11:03:58 -0800 | [diff] [blame] | 391 | found = pci_get_dev_by_id(ids, NULL); |
Jiang Liu | d5af7d9 | 2013-01-21 13:20:45 -0800 | [diff] [blame] | 392 | if (found) { |
| 393 | pci_dev_put(found); |
| 394 | return 1; |
| 395 | } |
Alan Cox | d86f90f | 2006-12-04 15:14:44 -0800 | [diff] [blame] | 396 | ids++; |
| 397 | } |
Jiang Liu | d5af7d9 | 2013-01-21 13:20:45 -0800 | [diff] [blame] | 398 | |
Greg Kroah-Hartman | 448432c | 2008-02-12 13:36:20 -0800 | [diff] [blame] | 399 | return 0; |
Alan Cox | d86f90f | 2006-12-04 15:14:44 -0800 | [diff] [blame] | 400 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | EXPORT_SYMBOL(pci_dev_present); |
| 402 | |
Alan Cox | 29f3eb6 | 2006-10-16 16:20:21 -0700 | [diff] [blame] | 403 | /* For boot time work */ |
| 404 | EXPORT_SYMBOL(pci_find_bus); |
| 405 | EXPORT_SYMBOL(pci_find_next_bus); |
| 406 | /* For everyone */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | EXPORT_SYMBOL(pci_get_device); |
| 408 | EXPORT_SYMBOL(pci_get_subsys); |
| 409 | EXPORT_SYMBOL(pci_get_slot); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | EXPORT_SYMBOL(pci_get_class); |