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 v2+.
8 * SPDX-License-Identifier: GPL-2.0-or-later
16 void pci_filter_init_v38(struct pci_access *a UNUSED, struct pci_filter *f) VERSIONED_ABI;
17 char *pci_filter_parse_slot_v38(struct pci_filter *f, char *str) VERSIONED_ABI;
18 char *pci_filter_parse_id_v38(struct pci_filter *f, char *str) VERSIONED_ABI;
19 int pci_filter_match_v38(struct pci_filter *f, struct pci_dev *d) VERSIONED_ABI;
22 pci_filter_init_v38(struct pci_access *a UNUSED, struct pci_filter *f)
24 memset((byte *) f, 0, sizeof(*f));
25 f->domain = f->bus = f->slot = f->func = -1;
26 f->vendor = f->device = -1;
28 f->device_class_mask = ~0U;
35 split_to_fields(char *str, char *buffer, int sep, char **fields, int num_fields)
39 if (strlen(str) >= BUF_SIZE)
40 return "Expression too long";
50 return "Too many fields";
52 while (*str && *str != sep)
59 while (i < num_fields)
66 field_defined(char *field)
68 return field && field[0] && strcmp(field, "*");
72 parse_hex_field(char *str, int *outp, unsigned int *maskp, unsigned int max)
75 unsigned int mask = ~0U;
76 unsigned int bound = 0;
78 if (!field_defined(str))
79 return 1; // and keep the defaults
81 // Historically, filters allowed writing hexadecimal numbers with leading "0x".
82 // This was never intentional nor documented, but some people relied on it.
83 if (!maskp && str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
91 if ((c == 'x' || c == 'X') && maskp)
94 bound = (bound << 4) | 1;
99 if (c >= '0' && c <= '9')
101 else if (c >= 'A' && c <= 'F')
103 else if (c >= 'a' && c <= 'f')
108 out = (out << 4) | d;
109 bound = (bound << 4) | d;
110 mask = (mask << 4) | 0xf;
123 /* Slot filter syntax: [[[domain]:][bus]:][slot][.[func]] */
126 pci_filter_parse_slot_v38(struct pci_filter *f, char *str)
132 if (err = split_to_fields(str, buf, ':', fields, 3))
138 if (!parse_hex_field(fields[0], &f->domain, NULL, 0x7fffffff))
139 return "Invalid domain number";
145 if (!parse_hex_field(fields[i], &f->bus, NULL, 0xff))
146 return "Invalid bus number";
150 char *fdev = fields[i];
151 if (field_defined(fdev))
154 if (split_to_fields(fdev, NULL, '.', sfields, 2))
155 return "Invalid slot/function number";
157 if (!parse_hex_field(sfields[0], &f->slot, NULL, 0x1f))
158 return "Invalid slot number";
160 if (!parse_hex_field(sfields[1], &f->func, NULL, 7))
161 return "Invalid function number";
167 /* ID filter syntax: [vendor]:[device][:class[:progif]] */
170 pci_filter_parse_id_v38(struct pci_filter *f, char *str)
176 if (err = split_to_fields(str, buf, ':', fields, 4))
180 return "At least two fields must be given";
182 if (!parse_hex_field(fields[0], &f->vendor, NULL, 0xffff))
183 return "Invalid vendor ID";
185 if (!parse_hex_field(fields[1], &f->device, NULL, 0xffff))
186 return "Invalid device ID";
188 if (!parse_hex_field(fields[2], &f->device_class, &f->device_class_mask, 0xffff))
189 return "Invalid class code";
191 if (!parse_hex_field(fields[3], &f->prog_if, NULL, 0xff))
192 return "Invalid programming interface code";
198 pci_filter_match_v38(struct pci_filter *f, struct pci_dev *d)
200 if ((f->domain >= 0 && f->domain != d->domain) ||
201 (f->bus >= 0 && f->bus != d->bus) ||
202 (f->slot >= 0 && f->slot != d->dev) ||
203 (f->func >= 0 && f->func != d->func))
205 if (f->device >= 0 || f->vendor >= 0)
207 pci_fill_info_v38(d, PCI_FILL_IDENT);
208 if ((f->device >= 0 && f->device != d->device_id) ||
209 (f->vendor >= 0 && f->vendor != d->vendor_id))
212 if (f->device_class >= 0)
214 pci_fill_info_v38(d, PCI_FILL_CLASS);
215 if ((f->device_class ^ d->device_class) & f->device_class_mask)
220 pci_fill_info_v38(d, PCI_FILL_CLASS_EXT);
221 if (f->prog_if != d->prog_if)
228 * Before pciutils v3.3, struct pci_filter had fewer fields,
229 * so we have to provide compatibility wrappers.
232 struct pci_filter_v30 {
233 int domain, bus, slot, func; /* -1 = ANY */
237 void pci_filter_init_v30(struct pci_access *a, struct pci_filter_v30 *f) VERSIONED_ABI;
238 char *pci_filter_parse_slot_v30(struct pci_filter_v30 *f, char *str) VERSIONED_ABI;
239 char *pci_filter_parse_id_v30(struct pci_filter_v30 *f, char *str) VERSIONED_ABI;
240 int pci_filter_match_v30(struct pci_filter_v30 *f, struct pci_dev *d) VERSIONED_ABI;
243 pci_filter_import_v30(struct pci_filter_v30 *old, struct pci_filter *new)
245 new->domain = old->domain;
247 new->slot = old->slot;
248 new->func = old->func;
249 new->vendor = old->vendor;
250 new->device = old->device;
251 new->device_class = -1;
252 new->device_class_mask = ~0U;
257 pci_filter_export_v30(struct pci_filter *new, struct pci_filter_v30 *old)
259 old->domain = new->domain;
261 old->slot = new->slot;
262 old->func = new->func;
263 old->vendor = new->vendor;
264 old->device = new->device;
268 pci_filter_init_v30(struct pci_access *a, struct pci_filter_v30 *f)
270 struct pci_filter new;
271 pci_filter_init_v38(a, &new);
272 pci_filter_export_v30(&new, f);
276 pci_filter_parse_slot_v30(struct pci_filter_v30 *f, char *str)
278 struct pci_filter new;
280 pci_filter_import_v30(f, &new);
281 if (err = pci_filter_parse_slot_v38(&new, str))
283 pci_filter_export_v30(&new, f);
288 pci_filter_parse_id_v30(struct pci_filter_v30 *f, char *str)
290 struct pci_filter new;
292 pci_filter_import_v30(f, &new);
293 if (err = pci_filter_parse_id_v38(&new, str))
295 if (new.device_class >= 0 || new.prog_if >= 0)
296 return "Filtering by class or programming interface not supported in this program";
297 pci_filter_export_v30(&new, f);
302 pci_filter_match_v30(struct pci_filter_v30 *f, struct pci_dev *d)
304 struct pci_filter new;
305 pci_filter_import_v30(f, &new);
306 return pci_filter_match_v38(&new, d);
309 // Version 3.3 is the same as version 3.8, only device_class_mask and prog_if were not implemented
310 // (their positions in struct pci_filter were declared as RFU).
312 STATIC_ALIAS(void pci_filter_init(struct pci_access *a, struct pci_filter *f), pci_filter_init_v38(a, f));
313 DEFINE_ALIAS(void pci_filter_init_v33(struct pci_access *a, struct pci_filter *f), pci_filter_init_v38);
314 SYMBOL_VERSION(pci_filter_init_v30, pci_filter_init@LIBPCI_3.0);
315 SYMBOL_VERSION(pci_filter_init_v33, pci_filter_init@LIBPCI_3.3);
316 SYMBOL_VERSION(pci_filter_init_v38, pci_filter_init@@LIBPCI_3.8);
318 STATIC_ALIAS(char *pci_filter_parse_slot(struct pci_filter *f, char *str), pci_filter_parse_slot_v38(f, str));
319 DEFINE_ALIAS(char *pci_filter_parse_slot_v33(struct pci_filter *f, char *str), pci_filter_parse_slot_v38);
320 SYMBOL_VERSION(pci_filter_parse_slot_v30, pci_filter_parse_slot@LIBPCI_3.0);
321 SYMBOL_VERSION(pci_filter_parse_slot_v33, pci_filter_parse_slot@LIBPCI_3.3);
322 SYMBOL_VERSION(pci_filter_parse_slot_v38, pci_filter_parse_slot@@LIBPCI_3.8);
324 STATIC_ALIAS(char *pci_filter_parse_id(struct pci_filter *f, char *str), pci_filter_parse_id_v38(f, str));
325 DEFINE_ALIAS(char *pci_filter_parse_id_v33(struct pci_filter *f, char *str), pci_filter_parse_id_v38);
326 SYMBOL_VERSION(pci_filter_parse_id_v30, pci_filter_parse_id@LIBPCI_3.0);
327 SYMBOL_VERSION(pci_filter_parse_id_v33, pci_filter_parse_id@LIBPCI_3.3);
328 SYMBOL_VERSION(pci_filter_parse_id_v38, pci_filter_parse_id@@LIBPCI_3.8);
330 STATIC_ALIAS(int pci_filter_match(struct pci_filter *f, struct pci_dev *d), pci_filter_match_v38(f, d));
331 DEFINE_ALIAS(int pci_filter_match_v33(struct pci_filter *f, struct pci_dev *d), pci_filter_match_v38);
332 SYMBOL_VERSION(pci_filter_match_v30, pci_filter_match@LIBPCI_3.0);
333 SYMBOL_VERSION(pci_filter_match_v33, pci_filter_match@LIBPCI_3.3);
334 SYMBOL_VERSION(pci_filter_match_v38, pci_filter_match@@LIBPCI_3.8);