[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[debian-devel:06127] Re: IDEA - PCI devices auto-detection
吉山です。こんなん出ました。
/etc/probepci.conf にデバイスドライバのデータベースを作って、このコ
マンドの出力を /etc/modules に出すと (PCI に関しては) PnP になります。
---
/*
* probepci.c
* programmed by A.Yoshiyama
* on Sat Nov 21 15:59:10 JST 1998
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define DATABASEFILE "/etc/probepci.conf"
/*
#define DATABASEFILE "/home/yosshy/deb/probepci.conf"
*/
#define DELIM " ,\t"
#define PCIDEVICEFILE "/proc/bus/pci/devices"
typedef struct driverdb_t {
char *driver;
struct driverdb_t *next;
} driverdb;
typedef struct pcidb_t {
unsigned int id;
char *driver;
struct pcidb_t *next;
} pcidb;
driverdb *
newdriverdb(char *str)
{
driverdb *p;
if ((p = (driverdb *)malloc(sizeof(driverdb))) == NULL) {
perror("driverdb");
exit (1);
}
p->driver = strdup(str);
p->next = NULL;
return p;
}
pcidb *
newpcidb(char *driver, char *vender, char *device)
{
pcidb *p;
if ((p = (pcidb *)malloc(sizeof(pcidb))) == NULL) {
perror("pcidb");
exit(1);
}
p->id = (unsigned int)strtol(vender, (char **)NULL, 16) << 16 |
(unsigned int)strtol(device, (char **)NULL, 16);
p->driver = driver;
p->next = NULL;
return p;
}
pcidb *pstart = NULL;
driverdb *dstart = NULL;
int
readdb(void)
{
pcidb *pcur;
driverdb *dcur, *dmatch;
FILE *FP;
char buf[BUFSIZ];
char *driver, *vender, *device;
if ((FP = fopen(DATABASEFILE, "r")) == NULL) {
perror("can't open database file " DATABASEFILE);
exit (1);
}
while (fgets(buf, BUFSIZ, FP) != NULL) {
if ((driver = strtok(buf, DELIM)) == NULL) continue;
if (driver[0] == '#') continue;
if (dstart == NULL)
dmatch = dstart = newdriverdb(driver);
else {
for (dmatch = NULL, dcur = dstart; dcur->next; dcur = dcur->next)
if (strcmp(dcur->driver, driver) == 0) {
dmatch = dcur;
break;
}
if (dmatch == NULL) {
dcur->next = newdriverdb(driver);
dmatch = dcur->next;
}
}
if ((vender = strtok(NULL, DELIM)) == NULL) continue;
while ((device = strtok(NULL, DELIM)) != NULL) {
if (pstart == NULL) {
pstart = pcur = newpcidb(dmatch->driver, vender, device);
} else {
pcur->next = newpcidb(dmatch->driver, vender, device);
pcur = pcur->next;
}
}
}
fclose(FP);
return 0;
}
int
main(int argc, char **argv)
{
pcidb *pcur;
char *device, buf[BUFSIZ];
FILE *FP;
readdb();
/*
for (pcur = pstart; pcur; pcur = pcur->next)
printf("%x:%s\n", pcur->id, pcur->driver);
*/
if ((FP = fopen(PCIDEVICEFILE,"r")) == NULL) {
perror("Can't open pci device file " PCIDEVICEFILE);
exit(1);
}
while (fgets(buf, BUFSIZ, FP) != NULL) {
if (strtok(buf, "\t") == NULL) continue;
if ((device = strtok(NULL, "\t")) == NULL) continue;
for (pcur = pstart; pcur; pcur = pcur->next)
if ((unsigned int)strtol(device,(char **)NULL,16) == pcur->id)
/*
printf("found:%x:%s\n",pcur->id,pcur->driver);
*/
puts(pcur->driver);
}
fclose(FP);
return 0;
}
---
Name: 吉山あきら Akira Yoshiyama
E-mail: yosshy@debian.or.jp (runlevel1@xxxxxxxxxxxxxxxxx)
URL: http://jedi.seg.kobe-u.ac.jp/~yosshy/linux.html