2 * The PCI Library -- Device Filtering
4 * Copyright (c) 1998--2022 Martin Mares <mj@ucw.cz>
6 * Can be freely distributed and used under the terms of the GNU GPL.
14 void pci_filter_init_v38(struct pci_access *a UNUSED, struct pci_filter *f) VERSIONED_ABI;
15 char *pci_filter_parse_slot_v38(struct pci_filter *f, char *str) VERSIONED_ABI;
16 char *pci_filter_parse_id_v38(struct pci_filter *f, char *str) VERSIONED_ABI;
17 int pci_filter_match_v38(struct pci_filter *f, struct pci_dev *d) VERSIONED_ABI;
20 pci_filter_init_v38(struct pci_access *a UNUSED, struct pci_filter *f)
22 memset((byte *) f, 0, sizeof(*f));
23 f->domain = f->bus = f->slot = f->func = -1;
24 f->vendor = f->device = -1;
26 f->device_class_mask = ~0U;
33 split_to_fields(char *str, char *buffer, int sep, char **fields, int num_fields)
37 if (strlen(str) >= BUF_SIZE)
38 return "Expression too long";
48 return "Too many fields";
50 while (*str && *str != sep)
57 while (i < num_fields)
64 field_defined(char *field)
66 return field && field[0] && strcmp(field, "*");
70 parse_hex_field(char *str, int *outp, unsigned int *maskp, unsigned int max)
73 unsigned int mask = ~0U;
74 unsigned int bound = 0;
76 if (!field_defined(str))
77 return 1; // and keep the defaults
79 // Historically, filters allowed writing hexadecimal numbers with leading "0x".
80 // This was never intentional nor documented, but some people relied on it.
81 if (!maskp && str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
89 if ((c == 'x' || c == 'X') && maskp)
92 bound = (bound << 4) | 1;
97 if (c >= '0' && c <= '9')
99 else if (c >= 'A' && c <= 'F')
101 else if (c >= 'a' && c <= 'f')
106 out = (out << 4) | d;
107 bound = (bound << 4) | d;
108 mask = (mask << 4) | 0xf;
121 /* Slot filter syntax: [[[domain]:][bus]:][slot][.[func]] */
124 pci_filter_parse_slot_v38(struct pci_filter *f, char *str)
130 if (err = split_to_fields(str, buf, ':', fields, 3))
136 if (!parse_hex_field(fields[0], &f->domain, NULL, 0x7fffffff))
137 return "Invalid domain number";
143 if (!parse_hex_field(fields[i], &f->bus, NULL, 0xff))
144 return "Invalid bus number";
148 char *fdev = fields[i];
149 if (field_defined(fdev))
152 if (split_to_fields(fdev, NULL, '.', sfields, 2))
153 return "Invalid slot/function number";
155 if (!parse_hex_field(sfields[0], &f->slot, NULL, 0x1f))
156 return "Invalid slot number";
158 if (!parse_hex_field(sfields[1], &f->func, NULL, 7))
159 return "Invalid function number";
165 /* ID filter syntax: [vendor]:[device][:class[:progif]] */
168 pci_filter_parse_id_v38(struct pci_filter *f, char *str)
174 if (err = split_to_fields(str, buf, ':', fields, 4))
178 return "At least two fields must be given";
180 if (!parse_hex_field(fields[0], &f->vendor, NULL, 0xffff))
181 return "Invalid vendor ID";
183 if (!parse_hex_field(fields[1], &f->device, NULL, 0xffff))
184 return "Invalid device ID";
186 if (!parse_hex_field(fields[2], &f->device_class, &f->device_class_mask, 0xffff))
187 return "Invalid class code";
189 if (!parse_hex_field(fields[3], &f->prog_if, NULL, 0xff))
190 return "Invalid programming interface code";
196 pci_filter_match_v38(struct pci_filter *f, struct pci_dev *d)
198 if ((f->domain >= 0 && f->domain != d->domain) ||
199 (f->bus >= 0 && f->bus != d->bus) ||
200 (f->slot >= 0 && f->slot != d->dev) ||
201 (f->func >= 0 && f->func != d->func))
203 if (f->device >= 0 || f->vendor >= 0)
205 pci_fill_info_v38(d, PCI_FILL_IDENT);
206 if ((f->device >= 0 && f->device != d->device_id) ||
207 (f->vendor >= 0 && f->vendor != d->vendor_id))
210 if (f->device_class >= 0)
212 pci_fill_info_v38(d, PCI_FILL_CLASS);
213 if ((f->device_class ^ d->device_class) & f->device_class_mask)
218 pci_fill_info_v38(d, PCI_FILL_CLASS_EXT);
219 if (f->prog_if != d->prog_if)
226 * Before pciutils v3.3, struct pci_filter had fewer fields,
227 * so we have to provide compatibility wrappers.
230 struct pci_filter_v30 {
231 int domain, bus, slot, func; /* -1 = ANY */
235 void pci_filter_init_v30(struct pci_access *a, struct pci_filter_v30 *f) VERSIONED_ABI;
236 char *pci_filter_parse_slot_v30(struct pci_filter_v30 *f, char *str) VERSIONED_ABI;
237 char *pci_filter_parse_id_v30(struct pci_filter_v30 *f, char *str) VERSIONED_ABI;
238 int pci_filter_match_v30(struct pci_filter_v30 *f, struct pci_dev *d) VERSIONED_ABI;
241 pci_filter_import_v30(struct pci_filter_v30 *old, struct pci_filter *new)
243 new->domain = old->domain;
245 new->slot = old->slot;
246 new->func = old->func;
247 new->vendor = old->vendor;
248 new->device = old->device;
249 new->device_class = -1;
250 new->device_class_mask = ~0U;
255 pci_filter_export_v30(struct pci_filter *new, struct pci_filter_v30 *old)
257 old->domain = new->domain;
259 old->slot = new->slot;
260 old->func = new->func;
261 old->vendor = new->vendor;
262 old->device = new->device;
266 pci_filter_init_v30(struct pci_access *a, struct pci_filter_v30 *f)
268 struct pci_filter new;
269 pci_filter_init_v38(a, &new);
270 pci_filter_export_v30(&new, f);
274 pci_filter_parse_slot_v30(struct pci_filter_v30 *f, char *str)
276 struct pci_filter new;
278 pci_filter_import_v30(f, &new);
279 if (err = pci_filter_parse_slot_v38(&new, str))
281 pci_filter_export_v30(&new, f);
286 pci_filter_parse_id_v30(struct pci_filter_v30 *f, char *str)
288 struct pci_filter new;
290 pci_filter_import_v30(f, &new);
291 if (err = pci_filter_parse_id_v38(&new, str))
293 if (new.device_class >= 0 || new.prog_if >= 0)
294 return "Filtering by class or programming interface not supported in this program";
295 pci_filter_export_v30(&new, f);
300 pci_filter_match_v30(struct pci_filter_v30 *f, struct pci_dev *d)
302 struct pci_filter new;
303 pci_filter_import_v30(f, &new);
304 return pci_filter_match_v38(&new, d);
307 // Version 3.3 is the same as version 3.8, only device_class_mask and prog_if were not implemented
308 // (their positions in struct pci_filter were declared as RFU).
310 STATIC_ALIAS(void pci_filter_init(struct pci_access *a, struct pci_filter *f), pci_filter_init_v38(a, f));
311 DEFINE_ALIAS(void pci_filter_init_v33(struct pci_access *a, struct pci_filter *f), pci_filter_init_v38);
312 SYMBOL_VERSION(pci_filter_init_v30, pci_filter_init@LIBPCI_3.0);
313 SYMBOL_VERSION(pci_filter_init_v33, pci_filter_init@LIBPCI_3.3);
314 SYMBOL_VERSION(pci_filter_init_v38, pci_filter_init@@LIBPCI_3.8);
316 STATIC_ALIAS(char *pci_filter_parse_slot(struct pci_filter *f, char *str), pci_filter_parse_slot_v38(f, str));
317 DEFINE_ALIAS(char *pci_filter_parse_slot_v33(struct pci_filter *f, char *str), pci_filter_parse_slot_v38);
318 SYMBOL_VERSION(pci_filter_parse_slot_v30, pci_filter_parse_slot@LIBPCI_3.0);
319 SYMBOL_VERSION(pci_filter_parse_slot_v33, pci_filter_parse_slot@LIBPCI_3.3);
320 SYMBOL_VERSION(pci_filter_parse_slot_v38, pci_filter_parse_slot@@LIBPCI_3.8);
322 STATIC_ALIAS(char *pci_filter_parse_id(struct pci_filter *f, char *str), pci_filter_parse_id_v38(f, str));
323 DEFINE_ALIAS(char *pci_filter_parse_id_v33(struct pci_filter *f, char *str), pci_filter_parse_id_v38);
324 SYMBOL_VERSION(pci_filter_parse_id_v30, pci_filter_parse_id@LIBPCI_3.0);
325 SYMBOL_VERSION(pci_filter_parse_id_v33, pci_filter_parse_id@LIBPCI_3.3);
326 SYMBOL_VERSION(pci_filter_parse_id_v38, pci_filter_parse_id@@LIBPCI_3.8);
328 STATIC_ALIAS(int pci_filter_match(struct pci_filter *f, struct pci_dev *d), pci_filter_match_v38(f, d));
329 DEFINE_ALIAS(int pci_filter_match_v33(struct pci_filter *f, struct pci_dev *d), pci_filter_match_v38);
330 SYMBOL_VERSION(pci_filter_match_v30, pci_filter_match@LIBPCI_3.0);
331 SYMBOL_VERSION(pci_filter_match_v33, pci_filter_match@LIBPCI_3.3);
332 SYMBOL_VERSION(pci_filter_match_v38, pci_filter_match@@LIBPCI_3.8);