2 * (C) Copyright IBM Corporation 2006
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * on the rights to use, copy, modify, merge, publish, distribute, sub
9 * license, and/or sell copies of the Software, and to permit persons to whom
10 * the Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * IBM AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
26 * \file common_iterator.c
27 * Platform independent iterator support routines.
29 * \author Ian Romanick <idr@us.ibm.com>
35 #include "pciaccess.h"
36 #include "pciaccess_private.h"
39 * Track device iteration state
43 struct pci_device_iterator {
53 struct pci_slot_match slot;
54 struct pci_id_match id;
60 * Create an iterator based on a regular expression.
63 * A pointer to a fully initialized \c pci_device_iterator structure on
64 * success, or \c NULL on failure.
66 * \sa pci_id_match_iterator_create, pci_device_next, pci_iterator_destroy
68 struct pci_device_iterator *
69 pci_slot_match_iterator_create( const struct pci_slot_match * match )
71 struct pci_device_iterator * iter;
73 if ( pci_sys == NULL ) {
77 iter = malloc( sizeof( *iter ) );
81 if ( match != NULL ) {
82 iter->mode = match_slot;
84 (void) memcpy( & iter->match.slot, match, sizeof( *match ) );
87 iter->mode = match_any;
96 * Create an iterator based on a regular expression.
99 * A pointer to a fully initialized \c pci_device_iterator structure on
100 * success, or \c NULL on failure.
102 * \sa pci_slot_match_iterator_create, pci_device_next, pci_iterator_destroy
104 struct pci_device_iterator *
105 pci_id_match_iterator_create( const struct pci_id_match * match )
107 struct pci_device_iterator * iter;
109 if ( pci_sys == NULL ) {
113 iter = malloc( sizeof( *iter ) );
114 if ( iter != NULL ) {
115 iter->next_index = 0;
117 if ( match != NULL ) {
118 iter->mode = match_id;
120 (void) memcpy( & iter->match.id, match, sizeof( *match ) );
123 iter->mode = match_any;
132 * Destroy an iterator previously created with \c pci_iterator_create.
134 * \param iter Iterator to be destroyed.
136 * \sa pci_device_next, pci_iterator_create
139 pci_iterator_destroy( struct pci_device_iterator * iter )
141 if ( iter != NULL ) {
148 * Iterate to the next PCI device.
150 * \param iter Device iterator returned by \c pci_device_iterate.
153 * A pointer to a \c pci_device, or \c NULL when all devices have been
157 pci_device_next( struct pci_device_iterator * iter )
159 struct pci_device_private * d = NULL;
164 switch( iter->mode ) {
166 if ( iter->next_index < pci_sys->num_devices ) {
167 d = & pci_sys->devices[ iter->next_index ];
174 while ( iter->next_index < pci_sys->num_devices ) {
175 struct pci_device_private * const temp =
176 & pci_sys->devices[ iter->next_index ];
179 if ( PCI_ID_COMPARE( iter->match.slot.domain, temp->base.domain )
180 && PCI_ID_COMPARE( iter->match.slot.bus, temp->base.bus )
181 && PCI_ID_COMPARE( iter->match.slot.dev, temp->base.dev )
182 && PCI_ID_COMPARE( iter->match.slot.func, temp->base.func ) ) {
192 while ( iter->next_index < pci_sys->num_devices ) {
193 struct pci_device_private * const temp =
194 & pci_sys->devices[ iter->next_index ];
197 if ( PCI_ID_COMPARE( iter->match.id.vendor_id, temp->base.vendor_id )
198 && PCI_ID_COMPARE( iter->match.id.device_id, temp->base.device_id )
199 && PCI_ID_COMPARE( iter->match.id.subvendor_id, temp->base.subvendor_id )
200 && PCI_ID_COMPARE( iter->match.id.subdevice_id, temp->base.subdevice_id )
201 && ((temp->base.device_class & iter->match.id.device_class_mask)
202 == iter->match.id.device_class) ) {
212 return (struct pci_device *) d;
217 pci_device_find_by_slot( uint32_t domain, uint32_t bus, uint32_t dev,
220 struct pci_device_iterator iter;
224 iter.mode = match_slot;
225 iter.match.slot.domain = domain;
226 iter.match.slot.bus = bus;
227 iter.match.slot.dev = dev;
228 iter.match.slot.func = func;
230 return pci_device_next( & iter );