]> mj.ucw.cz Git - pciutils.git/blob - filter.c
Intermediate version of pciutils.
[pciutils.git] / filter.c
1 /*
2  *      $Id: filter.c,v 1.1 1998/03/31 21:02:14 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 #include <linux/pci.h>
15
16 #include "pciutils.h"
17
18 void
19 filter_init(struct pci_filter *f)
20 {
21   f->bus = f->slot = f->func = -1;
22   f->vendor = f->device = -1;
23 }
24
25 /* Slot filter syntax: [[bus]:][slot][.[func]] */
26
27 char *
28 filter_parse_slot(struct pci_filter *f, char *str)
29 {
30   char *colon = strchr(str, ':');
31   char *dot = strchr((colon ? colon + 1 : str), '.');
32   char *mid = str;
33   char *e;
34
35   if (colon)
36     {
37       *colon++ = 0;
38       mid = colon;
39       if (str[0] && strcmp(str, "*"))
40         {
41           long int x = strtol(str, &e, 16);
42           if ((e && *e) || (x < 0 || x >= 0xff))
43             return "Invalid bus number";
44           f->bus = x;
45         }
46     }
47   if (dot)
48     *dot++ = 0;
49   if (mid[0] && strcmp(mid, "*"))
50     {
51       long int x = strtol(str, &e, 16);
52       if ((e && *e) || (x < 0 || x >= 0x1f))
53         return "Invalid slot number";
54       f->slot = x;
55     }
56   if (dot && dot[0] && strcmp(dot, "*"))
57     {
58       long int x = strtol(dot, &e, 16);
59       if ((e && *e) || (x < 0 || x >= 7))
60         return "Invalid function number";
61       f->func = x;
62     }
63   return NULL;
64 }
65
66 /* ID filter syntax: [vendor]:[device] */
67
68 char *
69 filter_parse_id(struct pci_filter *f, char *str)
70 {
71   char *s, *e;
72
73   if (!*str)
74     return NULL;
75   s = strchr(str, ':');
76   if (!s)
77     return "':' expected";
78   *s++ = 0;
79   if (str[0] && strcmp(str, "*"))
80     {
81       long int x = strtol(str, &e, 16);
82       if ((e && *e) || (x < 0 || x >= 0xffff))
83         return "Invalid vendor ID";
84       f->vendor = x;
85     }
86   if (s[0] && strcmp(s, "*"))
87     {
88       long int x = strtol(s, &e, 16);
89       if ((e && *e) || (x < 0 || x >= 0xffff))
90         return "Invalid device ID";
91       f->device = x;
92     }
93   return NULL;
94 }
95
96 int
97 filter_match(struct pci_filter *f, byte bus, byte devfn, word vendid, word devid)
98 {
99   if ((f->bus >= 0 && f->bus != bus) ||
100       (f->slot >= 0 && f->slot != PCI_SLOT(devfn)) ||
101       (f->func >= 0 && f->func != PCI_FUNC(devfn)) ||
102       (f->device >= 0 && f->device != devid) ||
103       (f->vendor >= 0 && f->vendor != vendid))
104     return 0;
105   return 1;
106 }