2 * Copyright 1996 Massachusetts Institute of Technology
4 * Permission to use, copy, modify, and distribute this software and
5 * its documentation for any purpose and without fee is hereby
6 * granted, provided that both the above copyright notice and this
7 * permission notice appear in all copies, that both the above
8 * copyright notice and this permission notice appear in all
9 * supporting documentation, and that the name of M.I.T. not be used
10 * in advertising or publicity pertaining to distribution of the
11 * software without specific, written prior permission. M.I.T. makes
12 * no representations about the suitability of this software for any
13 * purpose. It is provided "as is" without express or implied
16 * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''. M.I.T. DISCLAIMS
17 * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
18 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
20 * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
26 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 static const char rcsid[] =
35 #include <sys/types.h>
36 #include <sys/fcntl.h>
44 #include <sys/pciio.h>
45 #include <sys/queue.h>
47 #include <dev/pci/pcireg.h>
49 #include "pathnames.h"
52 struct pci_device_info
54 TAILQ_ENTRY(pci_device_info) link;
59 struct pci_vendor_info
61 TAILQ_ENTRY(pci_vendor_info) link;
62 TAILQ_HEAD(,pci_device_info) devs;
67 TAILQ_HEAD(,pci_vendor_info) pci_vendors;
69 static void list_devs(int verbose, int caps);
70 static void list_verbose(struct pci_conf *p);
71 static const char *guess_class(struct pci_conf *p);
72 static const char *guess_subclass(struct pci_conf *p);
73 static int load_vendors(void);
74 static void readit(const char *, const char *, int);
75 static void writeit(const char *, const char *, const char *, int);
76 static void chkattached(const char *, int);
78 static int exitstatus = 0;
83 fprintf(stderr, "%s\n%s\n%s\n%s\n",
84 "usage: pciconf -l [-cv]",
85 " pciconf -a selector",
86 " pciconf -r [-b | -h] selector addr[:addr2]",
87 " pciconf -w [-b | -h] selector addr value");
92 main(int argc, char **argv)
95 int listmode, readmode, writemode, attachedmode, caps, verbose;
98 listmode = readmode = writemode = attachedmode = caps = verbose = byte = isshort = 0;
100 while ((c = getopt(argc, argv, "abchlrwv")) != -1) {
139 if ((listmode && optind != argc)
140 || (writemode && optind + 3 != argc)
141 || (readmode && optind + 2 != argc)
142 || (attachedmode && optind + 1 != argc))
146 list_devs(verbose, caps);
147 } else if (attachedmode) {
148 chkattached(argv[optind],
149 byte ? 1 : isshort ? 2 : 4);
150 } else if (readmode) {
151 readit(argv[optind], argv[optind + 1],
152 byte ? 1 : isshort ? 2 : 4);
153 } else if (writemode) {
154 writeit(argv[optind], argv[optind + 1], argv[optind + 2],
155 byte ? 1 : isshort ? 2 : 4);
164 list_devs(int verbose, int caps)
167 struct pci_conf_io pc;
168 struct pci_conf conf[255], *p;
174 fd = open(_PATH_DEVPCI, caps ? O_RDWR : O_RDONLY, 0);
176 err(1, "%s", _PATH_DEVPCI);
178 bzero(&pc, sizeof(struct pci_conf_io));
179 pc.match_buf_len = sizeof(conf);
183 if (ioctl(fd, PCIOCGETCONF, &pc) == -1)
184 err(1, "ioctl(PCIOCGETCONF)");
187 * 255 entries should be more than enough for most people,
188 * but if someone has more devices, and then changes things
189 * around between ioctls, we'll do the cheezy thing and
190 * just bail. The alternative would be to go back to the
191 * beginning of the list, and print things twice, which may
194 if (pc.status == PCI_GETCONF_LIST_CHANGED) {
195 warnx("PCI device list changed, please try again");
199 } else if (pc.status == PCI_GETCONF_ERROR) {
200 warnx("error returned from PCIOCGETCONF ioctl");
205 for (p = conf; p < &conf[pc.num_matches]; p++) {
206 printf("%s%d@pci%d:%d:%d:%d:\tclass=0x%06x card=0x%08x "
207 "chip=0x%08x rev=0x%02x hdr=0x%02x\n",
208 (p->pd_name && *p->pd_name) ? p->pd_name :
210 (p->pd_name && *p->pd_name) ? (int)p->pd_unit :
211 none_count++, p->pc_sel.pc_domain,
212 p->pc_sel.pc_bus, p->pc_sel.pc_dev,
213 p->pc_sel.pc_func, (p->pc_class << 16) |
214 (p->pc_subclass << 8) | p->pc_progif,
215 (p->pc_subdevice << 16) | p->pc_subvendor,
216 (p->pc_device << 16) | p->pc_vendor,
217 p->pc_revid, p->pc_hdr);
223 } while (pc.status == PCI_GETCONF_MORE_DEVS);
229 list_verbose(struct pci_conf *p)
231 struct pci_vendor_info *vi;
232 struct pci_device_info *di;
235 TAILQ_FOREACH(vi, &pci_vendors, link) {
236 if (vi->id == p->pc_vendor) {
237 printf(" vendor = '%s'\n", vi->desc);
244 TAILQ_FOREACH(di, &vi->devs, link) {
245 if (di->id == p->pc_device) {
246 printf(" device = '%s'\n", di->desc);
251 if ((dp = guess_class(p)) != NULL)
252 printf(" class = %s\n", dp);
253 if ((dp = guess_subclass(p)) != NULL)
254 printf(" subclass = %s\n", dp);
258 * This is a direct cut-and-paste from the table in sys/dev/pci/pci.c.
265 } pci_nomatch_tab[] = {
266 {PCIC_OLD, -1, "old"},
267 {PCIC_OLD, PCIS_OLD_NONVGA, "non-VGA display device"},
268 {PCIC_OLD, PCIS_OLD_VGA, "VGA-compatible display device"},
269 {PCIC_STORAGE, -1, "mass storage"},
270 {PCIC_STORAGE, PCIS_STORAGE_SCSI, "SCSI"},
271 {PCIC_STORAGE, PCIS_STORAGE_IDE, "ATA"},
272 {PCIC_STORAGE, PCIS_STORAGE_FLOPPY, "floppy disk"},
273 {PCIC_STORAGE, PCIS_STORAGE_IPI, "IPI"},
274 {PCIC_STORAGE, PCIS_STORAGE_RAID, "RAID"},
275 {PCIC_NETWORK, -1, "network"},
276 {PCIC_NETWORK, PCIS_NETWORK_ETHERNET, "ethernet"},
277 {PCIC_NETWORK, PCIS_NETWORK_TOKENRING, "token ring"},
278 {PCIC_NETWORK, PCIS_NETWORK_FDDI, "fddi"},
279 {PCIC_NETWORK, PCIS_NETWORK_ATM, "ATM"},
280 {PCIC_NETWORK, PCIS_NETWORK_ISDN, "ISDN"},
281 {PCIC_DISPLAY, -1, "display"},
282 {PCIC_DISPLAY, PCIS_DISPLAY_VGA, "VGA"},
283 {PCIC_DISPLAY, PCIS_DISPLAY_XGA, "XGA"},
284 {PCIC_DISPLAY, PCIS_DISPLAY_3D, "3D"},
285 {PCIC_MULTIMEDIA, -1, "multimedia"},
286 {PCIC_MULTIMEDIA, PCIS_MULTIMEDIA_VIDEO, "video"},
287 {PCIC_MULTIMEDIA, PCIS_MULTIMEDIA_AUDIO, "audio"},
288 {PCIC_MULTIMEDIA, PCIS_MULTIMEDIA_TELE, "telephony"},
289 {PCIC_MEMORY, -1, "memory"},
290 {PCIC_MEMORY, PCIS_MEMORY_RAM, "RAM"},
291 {PCIC_MEMORY, PCIS_MEMORY_FLASH, "flash"},
292 {PCIC_BRIDGE, -1, "bridge"},
293 {PCIC_BRIDGE, PCIS_BRIDGE_HOST, "HOST-PCI"},
294 {PCIC_BRIDGE, PCIS_BRIDGE_ISA, "PCI-ISA"},
295 {PCIC_BRIDGE, PCIS_BRIDGE_EISA, "PCI-EISA"},
296 {PCIC_BRIDGE, PCIS_BRIDGE_MCA, "PCI-MCA"},
297 {PCIC_BRIDGE, PCIS_BRIDGE_PCI, "PCI-PCI"},
298 {PCIC_BRIDGE, PCIS_BRIDGE_PCMCIA, "PCI-PCMCIA"},
299 {PCIC_BRIDGE, PCIS_BRIDGE_NUBUS, "PCI-NuBus"},
300 {PCIC_BRIDGE, PCIS_BRIDGE_CARDBUS, "PCI-CardBus"},
301 {PCIC_BRIDGE, PCIS_BRIDGE_RACEWAY, "PCI-RACEway"},
302 {PCIC_SIMPLECOMM, -1, "simple comms"},
303 {PCIC_SIMPLECOMM, PCIS_SIMPLECOMM_UART, "UART"}, /* could detect 16550 */
304 {PCIC_SIMPLECOMM, PCIS_SIMPLECOMM_PAR, "parallel port"},
305 {PCIC_SIMPLECOMM, PCIS_SIMPLECOMM_MULSER, "multiport serial"},
306 {PCIC_SIMPLECOMM, PCIS_SIMPLECOMM_MODEM, "generic modem"},
307 {PCIC_BASEPERIPH, -1, "base peripheral"},
308 {PCIC_BASEPERIPH, PCIS_BASEPERIPH_PIC, "interrupt controller"},
309 {PCIC_BASEPERIPH, PCIS_BASEPERIPH_DMA, "DMA controller"},
310 {PCIC_BASEPERIPH, PCIS_BASEPERIPH_TIMER, "timer"},
311 {PCIC_BASEPERIPH, PCIS_BASEPERIPH_RTC, "realtime clock"},
312 {PCIC_BASEPERIPH, PCIS_BASEPERIPH_PCIHOT, "PCI hot-plug controller"},
313 {PCIC_INPUTDEV, -1, "input device"},
314 {PCIC_INPUTDEV, PCIS_INPUTDEV_KEYBOARD, "keyboard"},
315 {PCIC_INPUTDEV, PCIS_INPUTDEV_DIGITIZER,"digitizer"},
316 {PCIC_INPUTDEV, PCIS_INPUTDEV_MOUSE, "mouse"},
317 {PCIC_INPUTDEV, PCIS_INPUTDEV_SCANNER, "scanner"},
318 {PCIC_INPUTDEV, PCIS_INPUTDEV_GAMEPORT, "gameport"},
319 {PCIC_DOCKING, -1, "docking station"},
320 {PCIC_PROCESSOR, -1, "processor"},
321 {PCIC_SERIALBUS, -1, "serial bus"},
322 {PCIC_SERIALBUS, PCIS_SERIALBUS_FW, "FireWire"},
323 {PCIC_SERIALBUS, PCIS_SERIALBUS_ACCESS, "AccessBus"},
324 {PCIC_SERIALBUS, PCIS_SERIALBUS_SSA, "SSA"},
325 {PCIC_SERIALBUS, PCIS_SERIALBUS_USB, "USB"},
326 {PCIC_SERIALBUS, PCIS_SERIALBUS_FC, "Fibre Channel"},
327 {PCIC_SERIALBUS, PCIS_SERIALBUS_SMBUS, "SMBus"},
328 {PCIC_WIRELESS, -1, "wireless controller"},
329 {PCIC_WIRELESS, PCIS_WIRELESS_IRDA, "iRDA"},
330 {PCIC_WIRELESS, PCIS_WIRELESS_IR, "IR"},
331 {PCIC_WIRELESS, PCIS_WIRELESS_RF, "RF"},
332 {PCIC_INTELLIIO, -1, "intelligent I/O controller"},
333 {PCIC_INTELLIIO, PCIS_INTELLIIO_I2O, "I2O"},
334 {PCIC_SATCOM, -1, "satellite communication"},
335 {PCIC_SATCOM, PCIS_SATCOM_TV, "sat TV"},
336 {PCIC_SATCOM, PCIS_SATCOM_AUDIO, "sat audio"},
337 {PCIC_SATCOM, PCIS_SATCOM_VOICE, "sat voice"},
338 {PCIC_SATCOM, PCIS_SATCOM_DATA, "sat data"},
339 {PCIC_CRYPTO, -1, "encrypt/decrypt"},
340 {PCIC_CRYPTO, PCIS_CRYPTO_NETCOMP, "network/computer crypto"},
341 {PCIC_CRYPTO, PCIS_CRYPTO_NETCOMP, "entertainment crypto"},
342 {PCIC_DASP, -1, "dasp"},
343 {PCIC_DASP, PCIS_DASP_DPIO, "DPIO module"},
348 guess_class(struct pci_conf *p)
352 for (i = 0; pci_nomatch_tab[i].desc != NULL; i++) {
353 if (pci_nomatch_tab[i].class == p->pc_class)
354 return(pci_nomatch_tab[i].desc);
360 guess_subclass(struct pci_conf *p)
364 for (i = 0; pci_nomatch_tab[i].desc != NULL; i++) {
365 if ((pci_nomatch_tab[i].class == p->pc_class) &&
366 (pci_nomatch_tab[i].subclass == p->pc_subclass))
367 return(pci_nomatch_tab[i].desc);
377 struct pci_vendor_info *cv;
378 struct pci_device_info *cd;
379 char buf[1024], str[1024];
384 * Locate the database and initialise.
386 TAILQ_INIT(&pci_vendors);
387 if ((dbf = getenv("PCICONF_VENDOR_DATABASE")) == NULL)
389 if ((db = fopen(dbf, "r")) == NULL)
396 * Scan input lines from the database
399 if (fgets(buf, sizeof(buf), db) == NULL)
402 if ((ch = strchr(buf, '#')) != NULL)
404 ch = strchr(buf, '\0') - 1;
405 while (ch > buf && isspace(*ch))
410 /* Can't handle subvendor / subdevice entries yet */
411 if (buf[0] == '\t' && buf[1] == '\t')
414 /* Check for vendor entry */
415 if (buf[0] != '\t' && sscanf(buf, "%04x %[^\n]", &id, str) == 2) {
416 if ((id == 0) || (strlen(str) < 1))
418 if ((cv = malloc(sizeof(struct pci_vendor_info))) == NULL) {
419 warn("allocating vendor entry");
423 if ((cv->desc = strdup(str)) == NULL) {
425 warn("allocating vendor description");
430 TAILQ_INIT(&cv->devs);
431 TAILQ_INSERT_TAIL(&pci_vendors, cv, link);
435 /* Check for device entry */
436 if (buf[0] == '\t' && sscanf(buf + 1, "%04x %[^\n]", &id, str) == 2) {
437 if ((id == 0) || (strlen(str) < 1))
440 warnx("device entry with no vendor!");
443 if ((cd = malloc(sizeof(struct pci_device_info))) == NULL) {
444 warn("allocating device entry");
448 if ((cd->desc = strdup(str)) == NULL) {
450 warn("allocating device description");
455 TAILQ_INSERT_TAIL(&cv->devs, cd, link);
459 /* It's a comment or junk, ignore it */
469 read_config(int fd, struct pcisel *sel, long reg, int width)
477 if (ioctl(fd, PCIOCREAD, &pi) < 0)
478 err(1, "ioctl(PCIOCREAD)");
484 getsel(const char *str)
486 char *ep = strchr(str, '@');
489 unsigned long selarr[4];
499 if (strncmp(ep, "pci", 3) == 0) {
503 selarr[i++] = strtoul(ep, &ep, 10);
504 } while ((*ep == ':' || *ep == '.') && *++ep != '\0' && i < 4);
507 sel.pc_func = selarr[--i];
510 sel.pc_dev = selarr[--i];
511 sel.pc_bus = selarr[--i];
513 sel.pc_domain = selarr[--i];
517 if (*ep != '\x0' || ep == epbase)
518 errx(1, "cannot parse selector %s", str);
523 readone(int fd, struct pcisel *sel, long reg, int width)
526 printf("%0*x", width*2, read_config(fd, sel, reg, width));
530 readit(const char *name, const char *reg, int width)
540 fd = open(_PATH_DEVPCI, O_RDWR, 0);
542 err(1, "%s", _PATH_DEVPCI);
544 rend = rstart = strtol(reg, &end, 0);
545 if (end && *end == ':') {
547 rend = strtol(end, (char **) 0, 0);
550 for (i = 1, r = rstart; r <= rend; i++, r += width) {
551 readone(fd, &sel, r, width);
554 putchar(i % (16/width) ? ' ' : '\n');
556 if (i % (16/width) != 1)
562 writeit(const char *name, const char *reg, const char *data, int width)
567 pi.pi_sel = getsel(name);
568 pi.pi_reg = strtoul(reg, (char **)0, 0); /* XXX error check */
570 pi.pi_data = strtoul(data, (char **)0, 0); /* XXX error check */
572 fd = open(_PATH_DEVPCI, O_RDWR, 0);
574 err(1, "%s", _PATH_DEVPCI);
576 if (ioctl(fd, PCIOCWRITE, &pi) < 0)
577 err(1, "ioctl(PCIOCWRITE)");
581 chkattached(const char *name, int width)
586 pi.pi_sel = getsel(name);
591 fd = open(_PATH_DEVPCI, O_RDWR, 0);
593 err(1, "%s", _PATH_DEVPCI);
595 if (ioctl(fd, PCIOCATTACHED, &pi) < 0)
596 err(1, "ioctl(PCIOCATTACHED)");
598 exitstatus = pi.pi_data ? 0 : 2; /* exit(2), if NOT attached */
599 printf("%s: %s%s\n", name, pi.pi_data == 0 ? "not " : "", "attached");