1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * PCI searching functions
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright (C) 1993 -- 1997 Drew Eckhardt, Frederic Potter,
6*4882a593Smuzhiyun * David Mosberger-Tang
7*4882a593Smuzhiyun * Copyright (C) 1997 -- 2000 Martin Mares <mj@ucw.cz>
8*4882a593Smuzhiyun * Copyright (C) 2003 -- 2004 Greg Kroah-Hartman <greg@kroah.com>
9*4882a593Smuzhiyun */
10*4882a593Smuzhiyun
11*4882a593Smuzhiyun #include <linux/pci.h>
12*4882a593Smuzhiyun #include <linux/slab.h>
13*4882a593Smuzhiyun #include <linux/module.h>
14*4882a593Smuzhiyun #include <linux/interrupt.h>
15*4882a593Smuzhiyun #include "pci.h"
16*4882a593Smuzhiyun
17*4882a593Smuzhiyun DECLARE_RWSEM(pci_bus_sem);
18*4882a593Smuzhiyun
19*4882a593Smuzhiyun /*
20*4882a593Smuzhiyun * pci_for_each_dma_alias - Iterate over DMA aliases for a device
21*4882a593Smuzhiyun * @pdev: starting downstream device
22*4882a593Smuzhiyun * @fn: function to call for each alias
23*4882a593Smuzhiyun * @data: opaque data to pass to @fn
24*4882a593Smuzhiyun *
25*4882a593Smuzhiyun * Starting @pdev, walk up the bus calling @fn for each possible alias
26*4882a593Smuzhiyun * of @pdev at the root bus.
27*4882a593Smuzhiyun */
pci_for_each_dma_alias(struct pci_dev * pdev,int (* fn)(struct pci_dev * pdev,u16 alias,void * data),void * data)28*4882a593Smuzhiyun int pci_for_each_dma_alias(struct pci_dev *pdev,
29*4882a593Smuzhiyun int (*fn)(struct pci_dev *pdev,
30*4882a593Smuzhiyun u16 alias, void *data), void *data)
31*4882a593Smuzhiyun {
32*4882a593Smuzhiyun struct pci_bus *bus;
33*4882a593Smuzhiyun int ret;
34*4882a593Smuzhiyun
35*4882a593Smuzhiyun /*
36*4882a593Smuzhiyun * The device may have an explicit alias requester ID for DMA where the
37*4882a593Smuzhiyun * requester is on another PCI bus.
38*4882a593Smuzhiyun */
39*4882a593Smuzhiyun pdev = pci_real_dma_dev(pdev);
40*4882a593Smuzhiyun
41*4882a593Smuzhiyun ret = fn(pdev, pci_dev_id(pdev), data);
42*4882a593Smuzhiyun if (ret)
43*4882a593Smuzhiyun return ret;
44*4882a593Smuzhiyun
45*4882a593Smuzhiyun /*
46*4882a593Smuzhiyun * If the device is broken and uses an alias requester ID for
47*4882a593Smuzhiyun * DMA, iterate over that too.
48*4882a593Smuzhiyun */
49*4882a593Smuzhiyun if (unlikely(pdev->dma_alias_mask)) {
50*4882a593Smuzhiyun unsigned int devfn;
51*4882a593Smuzhiyun
52*4882a593Smuzhiyun for_each_set_bit(devfn, pdev->dma_alias_mask, MAX_NR_DEVFNS) {
53*4882a593Smuzhiyun ret = fn(pdev, PCI_DEVID(pdev->bus->number, devfn),
54*4882a593Smuzhiyun data);
55*4882a593Smuzhiyun if (ret)
56*4882a593Smuzhiyun return ret;
57*4882a593Smuzhiyun }
58*4882a593Smuzhiyun }
59*4882a593Smuzhiyun
60*4882a593Smuzhiyun for (bus = pdev->bus; !pci_is_root_bus(bus); bus = bus->parent) {
61*4882a593Smuzhiyun struct pci_dev *tmp;
62*4882a593Smuzhiyun
63*4882a593Smuzhiyun /* Skip virtual buses */
64*4882a593Smuzhiyun if (!bus->self)
65*4882a593Smuzhiyun continue;
66*4882a593Smuzhiyun
67*4882a593Smuzhiyun tmp = bus->self;
68*4882a593Smuzhiyun
69*4882a593Smuzhiyun /* stop at bridge where translation unit is associated */
70*4882a593Smuzhiyun if (tmp->dev_flags & PCI_DEV_FLAGS_BRIDGE_XLATE_ROOT)
71*4882a593Smuzhiyun return ret;
72*4882a593Smuzhiyun
73*4882a593Smuzhiyun /*
74*4882a593Smuzhiyun * PCIe-to-PCI/X bridges alias transactions from downstream
75*4882a593Smuzhiyun * devices using the subordinate bus number (PCI Express to
76*4882a593Smuzhiyun * PCI/PCI-X Bridge Spec, rev 1.0, sec 2.3). For all cases
77*4882a593Smuzhiyun * where the upstream bus is PCI/X we alias to the bridge
78*4882a593Smuzhiyun * (there are various conditions in the previous reference
79*4882a593Smuzhiyun * where the bridge may take ownership of transactions, even
80*4882a593Smuzhiyun * when the secondary interface is PCI-X).
81*4882a593Smuzhiyun */
82*4882a593Smuzhiyun if (pci_is_pcie(tmp)) {
83*4882a593Smuzhiyun switch (pci_pcie_type(tmp)) {
84*4882a593Smuzhiyun case PCI_EXP_TYPE_ROOT_PORT:
85*4882a593Smuzhiyun case PCI_EXP_TYPE_UPSTREAM:
86*4882a593Smuzhiyun case PCI_EXP_TYPE_DOWNSTREAM:
87*4882a593Smuzhiyun continue;
88*4882a593Smuzhiyun case PCI_EXP_TYPE_PCI_BRIDGE:
89*4882a593Smuzhiyun ret = fn(tmp,
90*4882a593Smuzhiyun PCI_DEVID(tmp->subordinate->number,
91*4882a593Smuzhiyun PCI_DEVFN(0, 0)), data);
92*4882a593Smuzhiyun if (ret)
93*4882a593Smuzhiyun return ret;
94*4882a593Smuzhiyun continue;
95*4882a593Smuzhiyun case PCI_EXP_TYPE_PCIE_BRIDGE:
96*4882a593Smuzhiyun ret = fn(tmp, pci_dev_id(tmp), data);
97*4882a593Smuzhiyun if (ret)
98*4882a593Smuzhiyun return ret;
99*4882a593Smuzhiyun continue;
100*4882a593Smuzhiyun }
101*4882a593Smuzhiyun } else {
102*4882a593Smuzhiyun if (tmp->dev_flags & PCI_DEV_FLAG_PCIE_BRIDGE_ALIAS)
103*4882a593Smuzhiyun ret = fn(tmp,
104*4882a593Smuzhiyun PCI_DEVID(tmp->subordinate->number,
105*4882a593Smuzhiyun PCI_DEVFN(0, 0)), data);
106*4882a593Smuzhiyun else
107*4882a593Smuzhiyun ret = fn(tmp, pci_dev_id(tmp), data);
108*4882a593Smuzhiyun if (ret)
109*4882a593Smuzhiyun return ret;
110*4882a593Smuzhiyun }
111*4882a593Smuzhiyun }
112*4882a593Smuzhiyun
113*4882a593Smuzhiyun return ret;
114*4882a593Smuzhiyun }
115*4882a593Smuzhiyun
pci_do_find_bus(struct pci_bus * bus,unsigned char busnr)116*4882a593Smuzhiyun static struct pci_bus *pci_do_find_bus(struct pci_bus *bus, unsigned char busnr)
117*4882a593Smuzhiyun {
118*4882a593Smuzhiyun struct pci_bus *child;
119*4882a593Smuzhiyun struct pci_bus *tmp;
120*4882a593Smuzhiyun
121*4882a593Smuzhiyun if (bus->number == busnr)
122*4882a593Smuzhiyun return bus;
123*4882a593Smuzhiyun
124*4882a593Smuzhiyun list_for_each_entry(tmp, &bus->children, node) {
125*4882a593Smuzhiyun child = pci_do_find_bus(tmp, busnr);
126*4882a593Smuzhiyun if (child)
127*4882a593Smuzhiyun return child;
128*4882a593Smuzhiyun }
129*4882a593Smuzhiyun return NULL;
130*4882a593Smuzhiyun }
131*4882a593Smuzhiyun
132*4882a593Smuzhiyun /**
133*4882a593Smuzhiyun * pci_find_bus - locate PCI bus from a given domain and bus number
134*4882a593Smuzhiyun * @domain: number of PCI domain to search
135*4882a593Smuzhiyun * @busnr: number of desired PCI bus
136*4882a593Smuzhiyun *
137*4882a593Smuzhiyun * Given a PCI bus number and domain number, the desired PCI bus is located
138*4882a593Smuzhiyun * in the global list of PCI buses. If the bus is found, a pointer to its
139*4882a593Smuzhiyun * data structure is returned. If no bus is found, %NULL is returned.
140*4882a593Smuzhiyun */
pci_find_bus(int domain,int busnr)141*4882a593Smuzhiyun struct pci_bus *pci_find_bus(int domain, int busnr)
142*4882a593Smuzhiyun {
143*4882a593Smuzhiyun struct pci_bus *bus = NULL;
144*4882a593Smuzhiyun struct pci_bus *tmp_bus;
145*4882a593Smuzhiyun
146*4882a593Smuzhiyun while ((bus = pci_find_next_bus(bus)) != NULL) {
147*4882a593Smuzhiyun if (pci_domain_nr(bus) != domain)
148*4882a593Smuzhiyun continue;
149*4882a593Smuzhiyun tmp_bus = pci_do_find_bus(bus, busnr);
150*4882a593Smuzhiyun if (tmp_bus)
151*4882a593Smuzhiyun return tmp_bus;
152*4882a593Smuzhiyun }
153*4882a593Smuzhiyun return NULL;
154*4882a593Smuzhiyun }
155*4882a593Smuzhiyun EXPORT_SYMBOL(pci_find_bus);
156*4882a593Smuzhiyun
157*4882a593Smuzhiyun /**
158*4882a593Smuzhiyun * pci_find_next_bus - begin or continue searching for a PCI bus
159*4882a593Smuzhiyun * @from: Previous PCI bus found, or %NULL for new search.
160*4882a593Smuzhiyun *
161*4882a593Smuzhiyun * Iterates through the list of known PCI buses. A new search is
162*4882a593Smuzhiyun * initiated by passing %NULL as the @from argument. Otherwise if
163*4882a593Smuzhiyun * @from is not %NULL, searches continue from next device on the
164*4882a593Smuzhiyun * global list.
165*4882a593Smuzhiyun */
pci_find_next_bus(const struct pci_bus * from)166*4882a593Smuzhiyun struct pci_bus *pci_find_next_bus(const struct pci_bus *from)
167*4882a593Smuzhiyun {
168*4882a593Smuzhiyun struct list_head *n;
169*4882a593Smuzhiyun struct pci_bus *b = NULL;
170*4882a593Smuzhiyun
171*4882a593Smuzhiyun WARN_ON(in_interrupt());
172*4882a593Smuzhiyun down_read(&pci_bus_sem);
173*4882a593Smuzhiyun n = from ? from->node.next : pci_root_buses.next;
174*4882a593Smuzhiyun if (n != &pci_root_buses)
175*4882a593Smuzhiyun b = list_entry(n, struct pci_bus, node);
176*4882a593Smuzhiyun up_read(&pci_bus_sem);
177*4882a593Smuzhiyun return b;
178*4882a593Smuzhiyun }
179*4882a593Smuzhiyun EXPORT_SYMBOL(pci_find_next_bus);
180*4882a593Smuzhiyun
181*4882a593Smuzhiyun /**
182*4882a593Smuzhiyun * pci_get_slot - locate PCI device for a given PCI slot
183*4882a593Smuzhiyun * @bus: PCI bus on which desired PCI device resides
184*4882a593Smuzhiyun * @devfn: encodes number of PCI slot in which the desired PCI
185*4882a593Smuzhiyun * device resides and the logical device number within that slot
186*4882a593Smuzhiyun * in case of multi-function devices.
187*4882a593Smuzhiyun *
188*4882a593Smuzhiyun * Given a PCI bus and slot/function number, the desired PCI device
189*4882a593Smuzhiyun * is located in the list of PCI devices.
190*4882a593Smuzhiyun * If the device is found, its reference count is increased and this
191*4882a593Smuzhiyun * function returns a pointer to its data structure. The caller must
192*4882a593Smuzhiyun * decrement the reference count by calling pci_dev_put().
193*4882a593Smuzhiyun * If no device is found, %NULL is returned.
194*4882a593Smuzhiyun */
pci_get_slot(struct pci_bus * bus,unsigned int devfn)195*4882a593Smuzhiyun struct pci_dev *pci_get_slot(struct pci_bus *bus, unsigned int devfn)
196*4882a593Smuzhiyun {
197*4882a593Smuzhiyun struct pci_dev *dev;
198*4882a593Smuzhiyun
199*4882a593Smuzhiyun WARN_ON(in_interrupt());
200*4882a593Smuzhiyun down_read(&pci_bus_sem);
201*4882a593Smuzhiyun
202*4882a593Smuzhiyun list_for_each_entry(dev, &bus->devices, bus_list) {
203*4882a593Smuzhiyun if (dev->devfn == devfn)
204*4882a593Smuzhiyun goto out;
205*4882a593Smuzhiyun }
206*4882a593Smuzhiyun
207*4882a593Smuzhiyun dev = NULL;
208*4882a593Smuzhiyun out:
209*4882a593Smuzhiyun pci_dev_get(dev);
210*4882a593Smuzhiyun up_read(&pci_bus_sem);
211*4882a593Smuzhiyun return dev;
212*4882a593Smuzhiyun }
213*4882a593Smuzhiyun EXPORT_SYMBOL(pci_get_slot);
214*4882a593Smuzhiyun
215*4882a593Smuzhiyun /**
216*4882a593Smuzhiyun * pci_get_domain_bus_and_slot - locate PCI device for a given PCI domain (segment), bus, and slot
217*4882a593Smuzhiyun * @domain: PCI domain/segment on which the PCI device resides.
218*4882a593Smuzhiyun * @bus: PCI bus on which desired PCI device resides
219*4882a593Smuzhiyun * @devfn: encodes number of PCI slot in which the desired PCI device
220*4882a593Smuzhiyun * resides and the logical device number within that slot in case of
221*4882a593Smuzhiyun * multi-function devices.
222*4882a593Smuzhiyun *
223*4882a593Smuzhiyun * Given a PCI domain, bus, and slot/function number, the desired PCI
224*4882a593Smuzhiyun * device is located in the list of PCI devices. If the device is
225*4882a593Smuzhiyun * found, its reference count is increased and this function returns a
226*4882a593Smuzhiyun * pointer to its data structure. The caller must decrement the
227*4882a593Smuzhiyun * reference count by calling pci_dev_put(). If no device is found,
228*4882a593Smuzhiyun * %NULL is returned.
229*4882a593Smuzhiyun */
pci_get_domain_bus_and_slot(int domain,unsigned int bus,unsigned int devfn)230*4882a593Smuzhiyun struct pci_dev *pci_get_domain_bus_and_slot(int domain, unsigned int bus,
231*4882a593Smuzhiyun unsigned int devfn)
232*4882a593Smuzhiyun {
233*4882a593Smuzhiyun struct pci_dev *dev = NULL;
234*4882a593Smuzhiyun
235*4882a593Smuzhiyun for_each_pci_dev(dev) {
236*4882a593Smuzhiyun if (pci_domain_nr(dev->bus) == domain &&
237*4882a593Smuzhiyun (dev->bus->number == bus && dev->devfn == devfn))
238*4882a593Smuzhiyun return dev;
239*4882a593Smuzhiyun }
240*4882a593Smuzhiyun return NULL;
241*4882a593Smuzhiyun }
242*4882a593Smuzhiyun EXPORT_SYMBOL(pci_get_domain_bus_and_slot);
243*4882a593Smuzhiyun
match_pci_dev_by_id(struct device * dev,const void * data)244*4882a593Smuzhiyun static int match_pci_dev_by_id(struct device *dev, const void *data)
245*4882a593Smuzhiyun {
246*4882a593Smuzhiyun struct pci_dev *pdev = to_pci_dev(dev);
247*4882a593Smuzhiyun const struct pci_device_id *id = data;
248*4882a593Smuzhiyun
249*4882a593Smuzhiyun if (pci_match_one_device(id, pdev))
250*4882a593Smuzhiyun return 1;
251*4882a593Smuzhiyun return 0;
252*4882a593Smuzhiyun }
253*4882a593Smuzhiyun
254*4882a593Smuzhiyun /*
255*4882a593Smuzhiyun * pci_get_dev_by_id - begin or continue searching for a PCI device by id
256*4882a593Smuzhiyun * @id: pointer to struct pci_device_id to match for the device
257*4882a593Smuzhiyun * @from: Previous PCI device found in search, or %NULL for new search.
258*4882a593Smuzhiyun *
259*4882a593Smuzhiyun * Iterates through the list of known PCI devices. If a PCI device is found
260*4882a593Smuzhiyun * with a matching id a pointer to its device structure is returned, and the
261*4882a593Smuzhiyun * reference count to the device is incremented. Otherwise, %NULL is returned.
262*4882a593Smuzhiyun * A new search is initiated by passing %NULL as the @from argument. Otherwise
263*4882a593Smuzhiyun * if @from is not %NULL, searches continue from next device on the global
264*4882a593Smuzhiyun * list. The reference count for @from is always decremented if it is not
265*4882a593Smuzhiyun * %NULL.
266*4882a593Smuzhiyun *
267*4882a593Smuzhiyun * This is an internal function for use by the other search functions in
268*4882a593Smuzhiyun * this file.
269*4882a593Smuzhiyun */
pci_get_dev_by_id(const struct pci_device_id * id,struct pci_dev * from)270*4882a593Smuzhiyun static struct pci_dev *pci_get_dev_by_id(const struct pci_device_id *id,
271*4882a593Smuzhiyun struct pci_dev *from)
272*4882a593Smuzhiyun {
273*4882a593Smuzhiyun struct device *dev;
274*4882a593Smuzhiyun struct device *dev_start = NULL;
275*4882a593Smuzhiyun struct pci_dev *pdev = NULL;
276*4882a593Smuzhiyun
277*4882a593Smuzhiyun WARN_ON(in_interrupt());
278*4882a593Smuzhiyun if (from)
279*4882a593Smuzhiyun dev_start = &from->dev;
280*4882a593Smuzhiyun dev = bus_find_device(&pci_bus_type, dev_start, (void *)id,
281*4882a593Smuzhiyun match_pci_dev_by_id);
282*4882a593Smuzhiyun if (dev)
283*4882a593Smuzhiyun pdev = to_pci_dev(dev);
284*4882a593Smuzhiyun pci_dev_put(from);
285*4882a593Smuzhiyun return pdev;
286*4882a593Smuzhiyun }
287*4882a593Smuzhiyun
288*4882a593Smuzhiyun /**
289*4882a593Smuzhiyun * pci_get_subsys - begin or continue searching for a PCI device by vendor/subvendor/device/subdevice id
290*4882a593Smuzhiyun * @vendor: PCI vendor id to match, or %PCI_ANY_ID to match all vendor ids
291*4882a593Smuzhiyun * @device: PCI device id to match, or %PCI_ANY_ID to match all device ids
292*4882a593Smuzhiyun * @ss_vendor: PCI subsystem vendor id to match, or %PCI_ANY_ID to match all vendor ids
293*4882a593Smuzhiyun * @ss_device: PCI subsystem device id to match, or %PCI_ANY_ID to match all device ids
294*4882a593Smuzhiyun * @from: Previous PCI device found in search, or %NULL for new search.
295*4882a593Smuzhiyun *
296*4882a593Smuzhiyun * Iterates through the list of known PCI devices. If a PCI device is found
297*4882a593Smuzhiyun * with a matching @vendor, @device, @ss_vendor and @ss_device, a pointer to its
298*4882a593Smuzhiyun * device structure is returned, and the reference count to the device is
299*4882a593Smuzhiyun * incremented. Otherwise, %NULL is returned. A new search is initiated by
300*4882a593Smuzhiyun * passing %NULL as the @from argument. Otherwise if @from is not %NULL,
301*4882a593Smuzhiyun * searches continue from next device on the global list.
302*4882a593Smuzhiyun * The reference count for @from is always decremented if it is not %NULL.
303*4882a593Smuzhiyun */
pci_get_subsys(unsigned int vendor,unsigned int device,unsigned int ss_vendor,unsigned int ss_device,struct pci_dev * from)304*4882a593Smuzhiyun struct pci_dev *pci_get_subsys(unsigned int vendor, unsigned int device,
305*4882a593Smuzhiyun unsigned int ss_vendor, unsigned int ss_device,
306*4882a593Smuzhiyun struct pci_dev *from)
307*4882a593Smuzhiyun {
308*4882a593Smuzhiyun struct pci_device_id id = {
309*4882a593Smuzhiyun .vendor = vendor,
310*4882a593Smuzhiyun .device = device,
311*4882a593Smuzhiyun .subvendor = ss_vendor,
312*4882a593Smuzhiyun .subdevice = ss_device,
313*4882a593Smuzhiyun };
314*4882a593Smuzhiyun
315*4882a593Smuzhiyun return pci_get_dev_by_id(&id, from);
316*4882a593Smuzhiyun }
317*4882a593Smuzhiyun EXPORT_SYMBOL(pci_get_subsys);
318*4882a593Smuzhiyun
319*4882a593Smuzhiyun /**
320*4882a593Smuzhiyun * pci_get_device - begin or continue searching for a PCI device by vendor/device id
321*4882a593Smuzhiyun * @vendor: PCI vendor id to match, or %PCI_ANY_ID to match all vendor ids
322*4882a593Smuzhiyun * @device: PCI device id to match, or %PCI_ANY_ID to match all device ids
323*4882a593Smuzhiyun * @from: Previous PCI device found in search, or %NULL for new search.
324*4882a593Smuzhiyun *
325*4882a593Smuzhiyun * Iterates through the list of known PCI devices. If a PCI device is
326*4882a593Smuzhiyun * found with a matching @vendor and @device, the reference count to the
327*4882a593Smuzhiyun * device is incremented and a pointer to its device structure is returned.
328*4882a593Smuzhiyun * Otherwise, %NULL is returned. A new search is initiated by passing %NULL
329*4882a593Smuzhiyun * as the @from argument. Otherwise if @from is not %NULL, searches continue
330*4882a593Smuzhiyun * from next device on the global list. The reference count for @from is
331*4882a593Smuzhiyun * always decremented if it is not %NULL.
332*4882a593Smuzhiyun */
pci_get_device(unsigned int vendor,unsigned int device,struct pci_dev * from)333*4882a593Smuzhiyun struct pci_dev *pci_get_device(unsigned int vendor, unsigned int device,
334*4882a593Smuzhiyun struct pci_dev *from)
335*4882a593Smuzhiyun {
336*4882a593Smuzhiyun return pci_get_subsys(vendor, device, PCI_ANY_ID, PCI_ANY_ID, from);
337*4882a593Smuzhiyun }
338*4882a593Smuzhiyun EXPORT_SYMBOL(pci_get_device);
339*4882a593Smuzhiyun
340*4882a593Smuzhiyun /**
341*4882a593Smuzhiyun * pci_get_class - begin or continue searching for a PCI device by class
342*4882a593Smuzhiyun * @class: search for a PCI device with this class designation
343*4882a593Smuzhiyun * @from: Previous PCI device found in search, or %NULL for new search.
344*4882a593Smuzhiyun *
345*4882a593Smuzhiyun * Iterates through the list of known PCI devices. If a PCI device is
346*4882a593Smuzhiyun * found with a matching @class, the reference count to the device is
347*4882a593Smuzhiyun * incremented and a pointer to its device structure is returned.
348*4882a593Smuzhiyun * Otherwise, %NULL is returned.
349*4882a593Smuzhiyun * A new search is initiated by passing %NULL as the @from argument.
350*4882a593Smuzhiyun * Otherwise if @from is not %NULL, searches continue from next device
351*4882a593Smuzhiyun * on the global list. The reference count for @from is always decremented
352*4882a593Smuzhiyun * if it is not %NULL.
353*4882a593Smuzhiyun */
pci_get_class(unsigned int class,struct pci_dev * from)354*4882a593Smuzhiyun struct pci_dev *pci_get_class(unsigned int class, struct pci_dev *from)
355*4882a593Smuzhiyun {
356*4882a593Smuzhiyun struct pci_device_id id = {
357*4882a593Smuzhiyun .vendor = PCI_ANY_ID,
358*4882a593Smuzhiyun .device = PCI_ANY_ID,
359*4882a593Smuzhiyun .subvendor = PCI_ANY_ID,
360*4882a593Smuzhiyun .subdevice = PCI_ANY_ID,
361*4882a593Smuzhiyun .class_mask = PCI_ANY_ID,
362*4882a593Smuzhiyun .class = class,
363*4882a593Smuzhiyun };
364*4882a593Smuzhiyun
365*4882a593Smuzhiyun return pci_get_dev_by_id(&id, from);
366*4882a593Smuzhiyun }
367*4882a593Smuzhiyun EXPORT_SYMBOL(pci_get_class);
368*4882a593Smuzhiyun
369*4882a593Smuzhiyun /**
370*4882a593Smuzhiyun * pci_dev_present - Returns 1 if device matching the device list is present, 0 if not.
371*4882a593Smuzhiyun * @ids: A pointer to a null terminated list of struct pci_device_id structures
372*4882a593Smuzhiyun * that describe the type of PCI device the caller is trying to find.
373*4882a593Smuzhiyun *
374*4882a593Smuzhiyun * Obvious fact: You do not have a reference to any device that might be found
375*4882a593Smuzhiyun * by this function, so if that device is removed from the system right after
376*4882a593Smuzhiyun * this function is finished, the value will be stale. Use this function to
377*4882a593Smuzhiyun * find devices that are usually built into a system, or for a general hint as
378*4882a593Smuzhiyun * to if another device happens to be present at this specific moment in time.
379*4882a593Smuzhiyun */
pci_dev_present(const struct pci_device_id * ids)380*4882a593Smuzhiyun int pci_dev_present(const struct pci_device_id *ids)
381*4882a593Smuzhiyun {
382*4882a593Smuzhiyun struct pci_dev *found = NULL;
383*4882a593Smuzhiyun
384*4882a593Smuzhiyun WARN_ON(in_interrupt());
385*4882a593Smuzhiyun while (ids->vendor || ids->subvendor || ids->class_mask) {
386*4882a593Smuzhiyun found = pci_get_dev_by_id(ids, NULL);
387*4882a593Smuzhiyun if (found) {
388*4882a593Smuzhiyun pci_dev_put(found);
389*4882a593Smuzhiyun return 1;
390*4882a593Smuzhiyun }
391*4882a593Smuzhiyun ids++;
392*4882a593Smuzhiyun }
393*4882a593Smuzhiyun
394*4882a593Smuzhiyun return 0;
395*4882a593Smuzhiyun }
396*4882a593Smuzhiyun EXPORT_SYMBOL(pci_dev_present);
397