summaryrefslogtreecommitdiffstats
path: root/isys/pci/pciprobe.c
blob: de874b95f98a26168d264ac9882b459c23003ac4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#include <ctype.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <sys/stat.h>

#include <pci/pci.h>

#include "pciprobe.h"

struct pciDevice * pciDeviceList = NULL;
static int numPciDevices = 0;
static struct pci_access *pacc=NULL;

static int devCmp(const void * a, const void * b) {
    const struct pciDevice * one = a;
    const struct pciDevice * two = b;
    int x=0,y=0;
    
    x = (one->vendor - two->vendor);
    y = (one->device - two->device);
    if (x)
      return x;
    else
      return y;
}

static int vendCmp(const void * a, const void * b) {
    const struct pciDevice * one = a;
    const struct pciDevice * two = b;
    
    return (one->vendor - two->vendor);
}


char *getVendor(unsigned int vendor) {
    struct pciDevice *searchDev, key;
    char *tmpstr;
    
    key.vendor = vendor;
    
    searchDev = bsearch(&key,pciDeviceList,numPciDevices,
			sizeof(struct pciDevice), vendCmp);
    if (searchDev) {
	int x;
	
	x=strchr(searchDev->desc,'|')-searchDev->desc-1;
	tmpstr=calloc(x,sizeof(char));
	tmpstr=strncpy(tmpstr,searchDev->desc,x);
	return tmpstr;
    } else {
	return NULL;
    }
}

void probePciFreeDrivers(void) {
    int i;

    if (!pciDeviceList) return;

    for (i = 0; i < numPciDevices; i++) {
    	free(pciDeviceList[i].driver);
    	free(pciDeviceList[i].desc);
    }

    free(pciDeviceList);

    pciDeviceList = NULL;
    numPciDevices = 0;
}

int probePciReadDrivers(const char * fn) {
    int fd;
    struct stat sb;
    char * buf;
    int numDrivers;
    char * start, * chptr;
    struct pciDevice * nextDevice;
    char module[5000];
    char descrip[5000];

    fd = open(fn, O_RDONLY);
    if (fd < 0) return -1;

    fstat(fd, &sb);
    buf = alloca(sb.st_size + 1);
    read(fd, buf, sb.st_size);
    buf[sb.st_size] = '\0';
    close(fd);

    /* upper bound */
    numDrivers = 1;
    start = buf;
    while ((start = strchr(start, '\n'))) {
	numDrivers++;
	start++;
    }

    pciDeviceList = realloc(pciDeviceList, sizeof(*pciDeviceList) *
				(numPciDevices + numDrivers));
    nextDevice = pciDeviceList + numPciDevices;

    start = buf;
    while (start && *start) {
	while (isspace(*start)) start++;
	if (*start != '#' && *start != '\n') {
	    if (sscanf(start, "%x %x %s \"%[^\"]", &nextDevice->vendor,
		       &nextDevice->device, module, descrip ) == 4) {
		numPciDevices++;
		chptr = strchr(module, '.');
		if (chptr) *chptr = '\0';

		nextDevice->driver = strdup(module);
		nextDevice->desc = strdup(descrip);
		nextDevice++;
	    }
	}

	start = strchr(start, '\n');
	if (start) start++;
    }

    qsort(pciDeviceList, numPciDevices, sizeof(*pciDeviceList), devCmp);

    return 0;
}

struct pciDevice * pciGetDeviceInfo(unsigned int vend, unsigned int dev) {
    struct pciDevice *searchDev, key;
    
    key.vendor = vend;
    key.device = dev;
    
    searchDev = bsearch(&key,pciDeviceList,numPciDevices,
			sizeof(struct pciDevice), devCmp);
    if (!searchDev) {
	char *namebuf;

	searchDev = malloc(sizeof(struct pciDevice));
	searchDev->vendor = vend;
	searchDev->device = dev;
	searchDev->driver = strdup("unknown");
	searchDev->desc = calloc(128, sizeof(char));
	namebuf = getVendor(vend);
	if (!namebuf) {
	    snprintf(searchDev->desc,128,
		     "Unknown vendor unknown device %04x:%04x",
		     searchDev->vendor, searchDev->device);
	} else {
	    snprintf(searchDev->desc,128,
		     "%s unknown device %04x:%04x",
		     namebuf, searchDev->vendor, searchDev->device);
	}
    }
    return searchDev;
}

struct pciDevice ** probePci(unsigned int type, int all) {
    struct pciDevice **devices=NULL;
    struct pci_dev *p;
    int numDevices=0;
    
    pacc = pci_alloc();
    if (!pacc) return NULL;
    pci_init(pacc);
    pci_scan_bus(pacc);
    for (p = pacc->devices; p; p=p->next) {
	byte config[256];
	int x=64;
	struct pciDevice *dev;
	
	memset(config,256,0);
	pci_read_block(p, 0, config, x);
	if (x<128 &&  (config[PCI_HEADER_TYPE] & 0x7f) == PCI_HEADER_TYPE_CARDBUS) {
	    pci_read_block(p, 0, config+64, 64);
	    x=128;
	}
        dev = pciGetDeviceInfo(p->vendor_id,p->device_id);
	dev->type = config[PCI_CLASS_DEVICE+1] << 8 | config[PCI_CLASS_DEVICE];
	if (all || (strcmp(dev->driver,"unknown") && strcmp(dev->driver,"ignore"))) {
	    if (!type || (type<0xff && (type==dev->type>>8))
		|| (type==dev->type)) {
		if (!numDevices) {
		    devices = malloc(sizeof(struct pciDevice *));
		} else {
		    devices = realloc(devices,(numDevices+1)*sizeof(struct pciDevice *));
		}
		devices[numDevices] = dev;
		numDevices++;
	    } 
	}
    }
    pci_cleanup(pacc);
    if (devices) {
	devices = realloc(devices,(numDevices+1)*sizeof(struct pciDevice *));
	devices[numDevices] = NULL;
    }
    return devices;
}

#ifdef TESTING
int main(int argc, char **argv) {
    struct pciDevice **list,*dev;
    int x=0;
    
    if (probePciReadDrivers("pcitable")) {
	perror("error reading pci table");
	exit(0);
    }
    list = probePci(0,1);
    if (list)
      while ((dev=list[x])) {
	  printf("%04x %04x %s (%s)\n",dev->vendor,dev->device,
		 dev->desc, dev->driver);
	  x++;
      }
    exit(0);
}
#endif