9 static Pcidev* pcilist;
10 static Pcidev* pcitail;
15 int fd, i, j, n, bno, dno, fno;
16 char buf[1024], *base, *s;
20 trace("pcicfginit\n");
21 if((fd = open(base = "/dev/pci", OREAD)) < 0)
22 if((fd = open(base = "#$/pci", OREAD)) < 0)
24 n = dirreadall(fd, &d);
28 if(strstr(d[i].name, "ctl") == nil)
31 strncpy(buf, d[i].name, sizeof(buf));
32 bno = strtoul(buf, &s, 10);
33 dno = strtoul(s+1, &s, 10);
34 fno = strtoul(s+1, nil, 10);
36 p = mallocz(sizeof(*p), 1);
37 p->tbdf = MKBUS(BusPCI, bno, dno, fno);
38 sprint(buf, "%s/%d.%d.%draw", base, bno, dno, fno);
39 if((p->rawfd = open(buf, ORDWR)) < 0){
43 sprint(buf, "%s/%d.%d.%dctl", base, bno, dno, fno);
44 if((fd = open(buf, OREAD)) < 0){
49 if((j = read(fd, buf, sizeof(buf)-1)) <= 0){
58 p->ccrb = strtol(buf, nil, 16);
59 p->ccru = strtol(buf + 3, nil, 16);
60 p->vid = strtol(buf + 9, &s, 16);
61 p->did = strtol(s + 1, &s, 16);
62 p->intl = strtol(s + 1, &s, 10);
64 p->rid = pcicfgr8(p, PciRID);
66 trace("%d.%d.%d: did=%X vid=%X rid=%X intl=%d ccru=%X\n",
67 bno, dno, fno, p->did, p->vid, p->rid, p->intl, p->ccru);
70 j = strtol(s+1, &s, 10);
71 if(j < 0 || j >= nelem(p->mem))
73 p->mem[j].bar = strtoul(s+1, &s, 16);
74 p->mem[j].size = strtoul(s+1, &s, 10);
75 trace("\tmem[%d] = %lux %d\n", j, p->mem[j].bar, p->mem[j].size);
87 pcicfgrw(Pcidev *pcidev, int rno, int data, int len, int read)
92 memset(buf, 0, sizeof(buf));
93 if(pread(pcidev->rawfd, buf, len, rno) != len)
119 if(pwrite(pcidev->rawfd, buf, len, rno) != len)
126 pcicfgr8(Pcidev* pcidev, int rno)
128 return pcicfgrw(pcidev, rno, 0, 1, 1);
132 pcicfgw8(Pcidev* pcidev, int rno, int data)
134 pcicfgrw(pcidev, rno, data, 1, 0);
138 pcicfgr16(Pcidev* pcidev, int rno)
140 return pcicfgrw(pcidev, rno, 0, 2, 1);
144 pcicfgw16(Pcidev* pcidev, int rno, int data)
146 pcicfgrw(pcidev, rno, data, 2, 0);
150 pcicfgr32(Pcidev* pcidev, int rno)
152 return pcicfgrw(pcidev, rno, 0, 4, 1);
156 pcicfgw32(Pcidev* pcidev, int rno, int data)
158 pcicfgrw(pcidev, rno, data, 4, 0);
162 pcimatch(Pcidev* prev, int vid, int did)
173 if(prev->vid == vid && (did == 0 || prev->did == did))