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