1/*
2 * (C) Copyright IBM Corporation 2006
3 * All Rights Reserved.
4 *
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:
11 *
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
14 * Software.
15 *
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.
23 */
24
25/**
26 * \file common_iterator.c
27 * Platform independent iterator support routines.
28 *
29 * \author Ian Romanick <idr@us.ibm.com>
30 */
31#ifdef HAVE_CONFIG_H
32#include "config.h"
33#endif
34
35#include <stdlib.h>
36#include <string.h>
37
38#include "pciaccess.h"
39#include "pciaccess_private.h"
40
41/**
42 * Track device iteration state
43 *
44 * \private
45 */
46struct pci_device_iterator {
47    unsigned next_index;
48
49    enum {
50	match_any,
51	match_slot,
52	match_id
53    } mode;
54
55    union {
56	struct pci_slot_match   slot;
57	struct pci_id_match     id;
58    } match;
59};
60
61
62/**
63 * Create an iterator based on a regular expression.
64 *
65 * \return
66 * A pointer to a fully initialized \c pci_device_iterator structure on
67 * success, or \c NULL on failure.
68 *
69 * \sa pci_id_match_iterator_create, pci_device_next, pci_iterator_destroy
70 */
71struct pci_device_iterator *
72pci_slot_match_iterator_create( const struct pci_slot_match * match )
73{
74    struct pci_device_iterator * iter;
75
76    if ( pci_sys == NULL ) {
77	return NULL;
78    }
79
80    iter = malloc( sizeof( *iter ) );
81    if ( iter != NULL ) {
82	iter->next_index = 0;
83
84	if ( match != NULL ) {
85	    iter->mode = match_slot;
86
87	    (void) memcpy( & iter->match.slot, match, sizeof( *match ) );
88	}
89	else {
90	    iter->mode = match_any;
91	}
92    }
93
94    return iter;
95}
96
97
98/**
99 * Create an iterator based on a regular expression.
100 *
101 * \return
102 * A pointer to a fully initialized \c pci_device_iterator structure on
103 * success, or \c NULL on failure.
104 *
105 * \sa pci_slot_match_iterator_create, pci_device_next, pci_iterator_destroy
106 */
107struct pci_device_iterator *
108pci_id_match_iterator_create( const struct pci_id_match * match )
109{
110    struct pci_device_iterator * iter;
111
112    if ( pci_sys == NULL ) {
113	return NULL;
114    }
115
116    iter = malloc( sizeof( *iter ) );
117    if ( iter != NULL ) {
118	iter->next_index = 0;
119
120	if ( match != NULL ) {
121	    iter->mode = match_id;
122
123	    (void) memcpy( & iter->match.id, match, sizeof( *match ) );
124	}
125	else {
126	    iter->mode = match_any;
127	}
128    }
129
130    return iter;
131}
132
133
134/**
135 * Destroy an iterator previously created with \c pci_iterator_create.
136 *
137 * \param iter  Iterator to be destroyed.
138 *
139 * \sa pci_device_next, pci_iterator_create
140 */
141void
142pci_iterator_destroy( struct pci_device_iterator * iter )
143{
144    if ( iter != NULL ) {
145	free( iter );
146    }
147}
148
149
150/**
151 * Iterate to the next PCI device.
152 *
153 * \param iter  Device iterator returned by \c pci_device_iterate.
154 *
155 * \return
156 * A pointer to a \c pci_device, or \c NULL when all devices have been
157 * iterated.
158 */
159struct pci_device *
160pci_device_next( struct pci_device_iterator * iter )
161{
162    struct pci_device_private * d = NULL;
163
164    if (!iter)
165	return NULL;
166
167    switch( iter->mode ) {
168    case match_any:
169	if ( iter->next_index < pci_sys->num_devices ) {
170	    d = & pci_sys->devices[ iter->next_index ];
171	    iter->next_index++;
172	}
173
174	break;
175
176    case match_slot: {
177	while ( iter->next_index < pci_sys->num_devices ) {
178	    struct pci_device_private * const temp =
179	      & pci_sys->devices[ iter->next_index ];
180
181	    iter->next_index++;
182	    if ( PCI_ID_COMPARE( iter->match.slot.domain, temp->base.domain )
183		 && PCI_ID_COMPARE( iter->match.slot.bus, temp->base.bus )
184		 && PCI_ID_COMPARE( iter->match.slot.dev, temp->base.dev )
185		 && PCI_ID_COMPARE( iter->match.slot.func, temp->base.func ) ) {
186		d = temp;
187		break;
188	    }
189	}
190
191	break;
192    }
193
194    case match_id: {
195	while ( iter->next_index < pci_sys->num_devices ) {
196	    struct pci_device_private * const temp =
197	      & pci_sys->devices[ iter->next_index ];
198
199	    iter->next_index++;
200	    if ( PCI_ID_COMPARE( iter->match.id.vendor_id, temp->base.vendor_id )
201		 && PCI_ID_COMPARE( iter->match.id.device_id, temp->base.device_id )
202		 && PCI_ID_COMPARE( iter->match.id.subvendor_id, temp->base.subvendor_id )
203		 && PCI_ID_COMPARE( iter->match.id.subdevice_id, temp->base.subdevice_id )
204		 && ((temp->base.device_class & iter->match.id.device_class_mask)
205		     == iter->match.id.device_class) ) {
206		d = temp;
207		break;
208	    }
209	}
210
211	break;
212    }
213    }
214
215    return (struct pci_device *) d;
216}
217
218
219struct pci_device *
220pci_device_find_by_slot( uint32_t domain, uint32_t bus, uint32_t dev,
221			 uint32_t func )
222{
223    struct pci_device_iterator  iter;
224
225
226    iter.next_index = 0;
227    iter.mode = match_slot;
228    iter.match.slot.domain = domain;
229    iter.match.slot.bus = bus;
230    iter.match.slot.dev = dev;
231    iter.match.slot.func = func;
232
233    return pci_device_next( & iter );
234}
235