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
84 if ((c == 'x' || c == 'X') && maskp)
87 bound = (bound << 4) | 1;
92 if (c >= '0' && c <= '9')
94 else if (c >= 'A' && c <= 'F')
96 else if (c >= 'a' && c <= 'f')
101 out = (out << 4) | d;
102 bound = (bound << 4) | d;
103 mask = (mask << 4) | 0xf;
116 /* Slot filter syntax: [[[domain]:][bus]:][slot][.[func]] */
119 pci_filter_parse_slot_v38(struct pci_filter *f, char *str)
125 if (err = split_to_fields(str, buf, ':', fields, 3))
131 if (!parse_hex_field(fields[0], &f->domain, NULL, 0x7fffffff))
132 return "Invalid domain number";
138 if (!parse_hex_field(fields[i], &f->bus, NULL, 0xff))
139 return "Invalid bus number";
143 char *fdev = fields[i];
144 if (field_defined(fdev))
147 if (split_to_fields(fdev, NULL, '.', sfields, 2))
148 return "Invalid slot/function number";
150 if (!parse_hex_field(sfields[0], &f->slot, NULL, 0x1f))
151 return "Invalid slot number";
153 if (!parse_hex_field(sfields[1], &f->func, NULL, 7))
154 return "Invalid function number";
160 /* ID filter syntax: [vendor]:[device][:class[:progif]] */
163 pci_filter_parse_id_v38(struct pci_filter *f, char *str)
169 if (err = split_to_fields(str, buf, ':', fields, 4))
173 return "At least two fields must be given";
175 if (!parse_hex_field(fields[0], &f->vendor, NULL, 0xffff))
176 return "Invalid vendor ID";
178 if (!parse_hex_field(fields[1], &f->device, NULL, 0xffff))
179 return "Invalid device ID";
181 if (!parse_hex_field(fields[2], &f->device_class, &f->device_class_mask, 0xffff))
182 return "Invalid class code";
184 if (!parse_hex_field(fields[3], &f->prog_if, NULL, 0xff))
185 return "Invalid programming interface code";
191 pci_filter_match_v38(struct pci_filter *f, struct pci_dev *d)
193 if ((f->domain >= 0 && f->domain != d->domain) ||
194 (f->bus >= 0 && f->bus != d->bus) ||
195 (f->slot >= 0 && f->slot != d->dev) ||
196 (f->func >= 0 && f->func != d->func))
198 if (f->device >= 0 || f->vendor >= 0)
200 pci_fill_info_v38(d, PCI_FILL_IDENT);
201 if ((f->device >= 0 && f->device != d->device_id) ||
202 (f->vendor >= 0 && f->vendor != d->vendor_id))
205 if (f->device_class >= 0)
207 pci_fill_info(d, PCI_FILL_CLASS);
208 if ((f->device_class ^ d->device_class) & f->device_class_mask)
213 pci_fill_info(d, PCI_FILL_CLASS_EXT);
214 if (f->prog_if != d->prog_if)
221 * Before pciutils v3.3, struct pci_filter had fewer fields,
222 * so we have to provide compatibility wrappers.
225 struct pci_filter_v30 {
226 int domain, bus, slot, func; /* -1 = ANY */
230 void pci_filter_init_v30(struct pci_access *a, struct pci_filter_v30 *f) VERSIONED_ABI;
231 char *pci_filter_parse_slot_v30(struct pci_filter_v30 *f, char *str) VERSIONED_ABI;
232 char *pci_filter_parse_id_v30(struct pci_filter_v30 *f, char *str) VERSIONED_ABI;
233 int pci_filter_match_v30(struct pci_filter_v30 *f, struct pci_dev *d) VERSIONED_ABI;
236 pci_filter_import_v30(struct pci_filter_v30 *old, struct pci_filter *new)
238 new->domain = old->domain;
240 new->slot = old->slot;
241 new->func = old->func;
242 new->vendor = old->vendor;
243 new->device = old->device;
244 new->device_class = -1;
245 new->device_class_mask = ~0U;
250 pci_filter_export_v30(struct pci_filter *new, struct pci_filter_v30 *old)
252 old->domain = new->domain;
254 old->slot = new->slot;
255 old->func = new->func;
256 old->vendor = new->vendor;
257 old->device = new->device;
261 pci_filter_init_v30(struct pci_access *a, struct pci_filter_v30 *f)
263 struct pci_filter new;
264 pci_filter_init_v38(a, &new);
265 pci_filter_export_v30(&new, f);
269 pci_filter_parse_slot_v30(struct pci_filter_v30 *f, char *str)
271 struct pci_filter new;
273 pci_filter_import_v30(f, &new);
274 if (err = pci_filter_parse_slot_v38(&new, str))
276 pci_filter_export_v30(&new, f);
281 pci_filter_parse_id_v30(struct pci_filter_v30 *f, char *str)
283 struct pci_filter new;
285 pci_filter_import_v30(f, &new);
286 if (err = pci_filter_parse_id_v38(&new, str))
288 if (new.device_class >= 0 || new.prog_if >= 0)
289 return "Filtering by class or programming interface not supported in this program";
290 pci_filter_export_v30(&new, f);
295 pci_filter_match_v30(struct pci_filter_v30 *f, struct pci_dev *d)
297 struct pci_filter new;
298 pci_filter_import_v30(f, &new);
299 return pci_filter_match_v38(&new, d);
302 // Version 3.3 is the same as version 3.8, only device_class_mask and prog_if were not implemented
303 // (their positions in struct pci_filter were declared as RFU).
305 STATIC_ALIAS(void pci_filter_init(struct pci_access *a, struct pci_filter *f), pci_filter_init_v38(a, f));
306 DEFINE_ALIAS(void pci_filter_init_v33(struct pci_access *a, struct pci_filter *f), pci_filter_init_v38);
307 SYMBOL_VERSION(pci_filter_init_v30, pci_filter_init@LIBPCI_3.0);
308 SYMBOL_VERSION(pci_filter_init_v33, pci_filter_init@LIBPCI_3.3);
309 SYMBOL_VERSION(pci_filter_init_v38, pci_filter_init@@LIBPCI_3.8);
311 STATIC_ALIAS(char *pci_filter_parse_slot(struct pci_filter *f, char *str), pci_filter_parse_slot_v38(f, str));
312 DEFINE_ALIAS(char *pci_filter_parse_slot_v33(struct pci_filter *f, char *str), pci_filter_parse_slot_v38);
313 SYMBOL_VERSION(pci_filter_parse_slot_v30, pci_filter_parse_slot@LIBPCI_3.0);
314 SYMBOL_VERSION(pci_filter_parse_slot_v33, pci_filter_parse_slot@LIBPCI_3.3);
315 SYMBOL_VERSION(pci_filter_parse_slot_v38, pci_filter_parse_slot@@LIBPCI_3.8);
317 STATIC_ALIAS(char *pci_filter_parse_id(struct pci_filter *f, char *str), pci_filter_parse_id_v38(f, str));
318 DEFINE_ALIAS(char *pci_filter_parse_id_v33(struct pci_filter *f, char *str), pci_filter_parse_id_v38);
319 SYMBOL_VERSION(pci_filter_parse_id_v30, pci_filter_parse_id@LIBPCI_3.0);
320 SYMBOL_VERSION(pci_filter_parse_id_v33, pci_filter_parse_id@LIBPCI_3.3);
321 SYMBOL_VERSION(pci_filter_parse_id_v38, pci_filter_parse_id@@LIBPCI_3.8);
323 STATIC_ALIAS(int pci_filter_match(struct pci_filter *f, struct pci_dev *d), pci_filter_match_v38(f, d));
324 DEFINE_ALIAS(int pci_filter_match_v33(struct pci_filter *f, struct pci_dev *d), pci_filter_match_v38);
325 SYMBOL_VERSION(pci_filter_match_v30, pci_filter_match@LIBPCI_3.0);
326 SYMBOL_VERSION(pci_filter_match_v33, pci_filter_match@LIBPCI_3.3);
327 SYMBOL_VERSION(pci_filter_match_v38, pci_filter_match@@LIBPCI_3.8);