]> mj.ucw.cz Git - pciutils.git/blob - lib/filter.c
2698cf85463bf59535d847c809452bad29799271
[pciutils.git] / lib / filter.c
1 /*
2  *      $Id: filter.c,v 1.3 2002/03/30 15:39:25 mj Exp $
3  *
4  *      Linux PCI Library -- Device Filtering
5  *
6  *      Copyright (c) 1998--2002 Martin Mares <mj@ucw.cz>
7  *
8  *      Can be freely distributed and used under the terms of the GNU GPL.
9  */
10
11 #include <stdlib.h>
12 #include <string.h>
13
14 #include "internal.h"
15
16 void
17 pci_filter_init(struct pci_access * UNUSED a, struct pci_filter *f)
18 {
19   f->bus = f->slot = f->func = -1;
20   f->vendor = f->device = -1;
21 }
22
23 /* Slot filter syntax: [[bus]:][slot][.[func]] */
24
25 char *
26 pci_filter_parse_slot(struct pci_filter *f, char *str)
27 {
28   char *colon = strchr(str, ':');
29   char *dot = strchr((colon ? colon + 1 : str), '.');
30   char *mid = str;
31   char *e;
32
33   if (colon)
34     {
35       *colon++ = 0;
36       mid = colon;
37       if (str[0] && strcmp(str, "*"))
38         {
39           long int x = strtol(str, &e, 16);
40           if ((e && *e) || (x < 0 || x > 0xff))
41             return "Invalid bus number";
42           f->bus = x;
43         }
44     }
45   if (dot)
46     *dot++ = 0;
47   if (mid[0] && strcmp(mid, "*"))
48     {
49       long int x = strtol(mid, &e, 16);
50       if ((e && *e) || (x < 0 || x > 0x1f))
51         return "Invalid slot number";
52       f->slot = x;
53     }
54   if (dot && dot[0] && strcmp(dot, "*"))
55     {
56       long int x = strtol(dot, &e, 16);
57       if ((e && *e) || (x < 0 || x > 7))
58         return "Invalid function number";
59       f->func = x;
60     }
61   return NULL;
62 }
63
64 /* ID filter syntax: [vendor]:[device] */
65
66 char *
67 pci_filter_parse_id(struct pci_filter *f, char *str)
68 {
69   char *s, *e;
70
71   if (!*str)
72     return NULL;
73   s = strchr(str, ':');
74   if (!s)
75     return "':' expected";
76   *s++ = 0;
77   if (str[0] && strcmp(str, "*"))
78     {
79       long int x = strtol(str, &e, 16);
80       if ((e && *e) || (x < 0 || x >= 0xffff))
81         return "Invalid vendor ID";
82       f->vendor = x;
83     }
84   if (s[0] && strcmp(s, "*"))
85     {
86       long int x = strtol(s, &e, 16);
87       if ((e && *e) || (x < 0 || x >= 0xffff))
88         return "Invalid device ID";
89       f->device = x;
90     }
91   return NULL;
92 }
93
94 int
95 pci_filter_match(struct pci_filter *f, struct pci_dev *d)
96 {
97   if ((f->bus >= 0 && f->bus != d->bus) ||
98       (f->slot >= 0 && f->slot != d->dev) ||
99       (f->func >= 0 && f->func != d->func))
100     return 0;
101   if (f->device >= 0 || f->vendor >= 0)
102     {
103       pci_fill_info(d, PCI_FILL_IDENT);
104       if ((f->device >= 0 && f->device != d->device_id) ||
105           (f->vendor >= 0 && f->vendor != d->vendor_id))
106         return 0;
107     }
108   return 1;
109 }