2 * The PCI Library -- Device Filtering
4 * Copyright (c) 1998--2014 Martin Mares <mj@ucw.cz>
6 * Can be freely distributed and used under the terms of the GNU GPL.
14 void pci_filter_init_v33(struct pci_access *a UNUSED, struct pci_filter *f) VERSIONED_ABI;
15 char *pci_filter_parse_slot_v33(struct pci_filter *f, char *str) VERSIONED_ABI;
16 char *pci_filter_parse_id_v33(struct pci_filter *f, char *str) VERSIONED_ABI;
17 int pci_filter_match_v33(struct pci_filter *f, struct pci_dev *d) VERSIONED_ABI;
20 pci_filter_init_v33(struct pci_access *a UNUSED, struct pci_filter *f)
22 f->domain = f->bus = f->slot = f->func = -1;
23 f->vendor = f->device = f->device_class = -1;
26 /* Slot filter syntax: [[[domain]:][bus]:][slot][.[func]] */
29 pci_filter_parse_slot_v33(struct pci_filter *f, char *str)
31 char *colon = strrchr(str, ':');
32 char *dot = strchr((colon ? colon + 1 : str), '.');
34 char *e, *bus, *colon2;
40 colon2 = strchr(str, ':');
45 if (str[0] && strcmp(str, "*"))
47 long int x = strtol(str, &e, 16);
48 if ((e && *e) || (x < 0 || x > 0xffff))
49 return "Invalid domain number";
55 if (bus[0] && strcmp(bus, "*"))
57 long int x = strtol(bus, &e, 16);
58 if ((e && *e) || (x < 0 || x > 0xff))
59 return "Invalid bus number";
65 if (mid[0] && strcmp(mid, "*"))
67 long int x = strtol(mid, &e, 16);
68 if ((e && *e) || (x < 0 || x > 0x1f))
69 return "Invalid slot number";
72 if (dot && dot[0] && strcmp(dot, "*"))
74 long int x = strtol(dot, &e, 16);
75 if ((e && *e) || (x < 0 || x > 7))
76 return "Invalid function number";
82 /* ID filter syntax: [vendor]:[device][:class] */
85 pci_filter_parse_id_v33(struct pci_filter *f, char *str)
93 return "':' expected";
95 if (str[0] && strcmp(str, "*"))
97 long int x = strtol(str, &e, 16);
98 if ((e && *e) || (x < 0 || x > 0xffff))
99 return "Invalid vendor ID";
105 if (s[0] && strcmp(s, "*"))
107 long int x = strtol(s, &e, 16);
108 if ((e && *e) || (x < 0 || x > 0xffff))
109 return "Invalid device ID";
112 if (c && c[0] && strcmp(s, "*"))
114 long int x = strtol(c, &e, 16);
115 if ((e && *e) || (x < 0 || x > 0xffff))
116 return "Invalid class code";
123 pci_filter_match_v33(struct pci_filter *f, struct pci_dev *d)
125 if ((f->domain >= 0 && f->domain != d->domain) ||
126 (f->bus >= 0 && f->bus != d->bus) ||
127 (f->slot >= 0 && f->slot != d->dev) ||
128 (f->func >= 0 && f->func != d->func))
130 if (f->device >= 0 || f->vendor >= 0)
132 pci_fill_info_v33(d, PCI_FILL_IDENT);
133 if ((f->device >= 0 && f->device != d->device_id) ||
134 (f->vendor >= 0 && f->vendor != d->vendor_id))
137 if (f->device_class >= 0)
139 pci_fill_info(d, PCI_FILL_CLASS);
140 if (f->device_class != d->device_class)
147 * Before pciutils v3.3, struct pci_filter had fewer fields,
148 * so we have to provide compatibility wrappers.
151 struct pci_filter_v30 {
152 int domain, bus, slot, func; /* -1 = ANY */
156 void pci_filter_init_v30(struct pci_access *a, struct pci_filter_v30 *f) VERSIONED_ABI;
157 char *pci_filter_parse_slot_v30(struct pci_filter_v30 *f, char *str) VERSIONED_ABI;
158 char *pci_filter_parse_id_v30(struct pci_filter_v30 *f, char *str) VERSIONED_ABI;
159 int pci_filter_match_v30(struct pci_filter_v30 *f, struct pci_dev *d) VERSIONED_ABI;
162 pci_filter_import_v30(struct pci_filter_v30 *old, struct pci_filter *new)
164 new->domain = old->domain;
166 new->slot = old->slot;
167 new->func = old->func;
168 new->vendor = old->vendor;
169 new->device = old->device;
170 new->device_class = -1;
174 pci_filter_export_v30(struct pci_filter *new, struct pci_filter_v30 *old)
176 old->domain = new->domain;
178 old->slot = new->slot;
179 old->func = new->func;
180 old->vendor = new->vendor;
181 old->device = new->device;
185 pci_filter_init_v30(struct pci_access *a, struct pci_filter_v30 *f)
187 struct pci_filter new;
188 pci_filter_init_v33(a, &new);
189 pci_filter_export_v30(&new, f);
193 pci_filter_parse_slot_v30(struct pci_filter_v30 *f, char *str)
195 struct pci_filter new;
197 pci_filter_import_v30(f, &new);
198 if (err = pci_filter_parse_slot_v33(&new, str))
200 pci_filter_export_v30(&new, f);
205 pci_filter_parse_id_v30(struct pci_filter_v30 *f, char *str)
207 struct pci_filter new;
209 pci_filter_import_v30(f, &new);
210 if (err = pci_filter_parse_id_v33(&new, str))
212 if (new.device_class >= 0)
213 return "Filtering by class not supported in this program";
214 pci_filter_export_v30(&new, f);
219 pci_filter_match_v30(struct pci_filter_v30 *f, struct pci_dev *d)
221 struct pci_filter new;
222 pci_filter_import_v30(f, &new);
223 return pci_filter_match_v33(&new, d);
226 STATIC_ALIAS(void pci_filter_init(struct pci_access *a, struct pci_filter *f), pci_filter_init_v33(a, f));
227 SYMBOL_VERSION(pci_filter_init_v30, pci_filter_init@LIBPCI_3.0);
228 SYMBOL_VERSION(pci_filter_init_v33, pci_filter_init@@LIBPCI_3.3);
230 STATIC_ALIAS(char *pci_filter_parse_slot(struct pci_filter *f, char *str), pci_filter_parse_slot_v33(f, str));
231 SYMBOL_VERSION(pci_filter_parse_slot_v30, pci_filter_parse_slot@LIBPCI_3.0);
232 SYMBOL_VERSION(pci_filter_parse_slot_v33, pci_filter_parse_slot@@LIBPCI_3.3);
234 STATIC_ALIAS(char *pci_filter_parse_id(struct pci_filter *f, char *str), pci_filter_parse_id_v33(f, str));
235 SYMBOL_VERSION(pci_filter_parse_id_v30, pci_filter_parse_id@LIBPCI_3.0);
236 SYMBOL_VERSION(pci_filter_parse_id_v33, pci_filter_parse_id@@LIBPCI_3.3);
238 STATIC_ALIAS(int pci_filter_match(struct pci_filter *f, struct pci_dev *d), pci_filter_match_v33(f, d));
239 SYMBOL_VERSION(pci_filter_match_v30, pci_filter_match@LIBPCI_3.0);
240 SYMBOL_VERSION(pci_filter_match_v33, pci_filter_match@@LIBPCI_3.3);