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>
43 #include <sys/pciio.h>
44 #include <sys/queue.h>
46 #include <dev/pci/pcireg.h>
48 #include "pathnames.h"
50 struct pci_device_info
52 TAILQ_ENTRY(pci_device_info) link;
57 struct pci_vendor_info
59 TAILQ_ENTRY(pci_vendor_info) link;
60 TAILQ_HEAD(,pci_device_info) devs;
65 TAILQ_HEAD(,pci_vendor_info) pci_vendors;
67 static void list_devs(int vendors);
68 static void list_verbose(struct pci_conf *p);
69 static char *guess_class(struct pci_conf *p);
70 static char *guess_subclass(struct pci_conf *p);
71 static int load_vendors(void);
72 static void readit(const char *, const char *, int);
73 static void writeit(const char *, const char *, const char *, int);
74 static void chkattached(const char *, int);
76 static int exitstatus = 0;
81 fprintf(stderr, "%s\n%s\n%s\n%s\n",
82 "usage: pciconf -l [-v]",
83 " pciconf -a selector",
84 " pciconf -r [-b | -h] selector addr[:addr2]",
85 " pciconf -w [-b | -h] selector addr value");
90 main(int argc, char **argv)
93 int listmode, readmode, writemode, attachedmode, verbose;
96 listmode = readmode = writemode = attachedmode = verbose = byte = isshort = 0;
98 while ((c = getopt(argc, argv, "alrwbhv")) != -1) {
133 if ((listmode && optind != argc)
134 || (writemode && optind + 3 != argc)
135 || (readmode && optind + 2 != argc)
136 || (attachedmode && optind + 1 != argc))
141 } else if (attachedmode) {
142 chkattached(argv[optind],
143 byte ? 1 : isshort ? 2 : 4);
144 } else if (readmode) {
145 readit(argv[optind], argv[optind + 1],
146 byte ? 1 : isshort ? 2 : 4);
147 } else if (writemode) {
148 writeit(argv[optind], argv[optind + 1], argv[optind + 2],
149 byte ? 1 : isshort ? 2 : 4);
158 list_devs(int verbose)
161 struct pci_conf_io pc;
162 struct pci_conf conf[255], *p;
168 fd = open(_PATH_DEVPCI, O_RDONLY, 0);
170 err(1, "%s", _PATH_DEVPCI);
172 bzero(&pc, sizeof(struct pci_conf_io));
173 pc.match_buf_len = sizeof(conf);
177 if (ioctl(fd, PCIOCGETCONF, &pc) == -1)
178 err(1, "ioctl(PCIOCGETCONF)");
181 * 255 entries should be more than enough for most people,
182 * but if someone has more devices, and then changes things
183 * around between ioctls, we'll do the cheezy thing and
184 * just bail. The alternative would be to go back to the
185 * beginning of the list, and print things twice, which may
188 if (pc.status == PCI_GETCONF_LIST_CHANGED) {
189 warnx("PCI device list changed, please try again");
193 } else if (pc.status == PCI_GETCONF_ERROR) {
194 warnx("error returned from PCIOCGETCONF ioctl");
199 for (p = conf; p < &conf[pc.num_matches]; p++) {
201 printf("%s%d@pci%d:%d:%d:\tclass=0x%06x card=0x%08x "
202 "chip=0x%08x rev=0x%02x hdr=0x%02x\n",
203 (p->pd_name && *p->pd_name) ? p->pd_name :
205 (p->pd_name && *p->pd_name) ? (int)p->pd_unit :
207 p->pc_sel.pc_bus, p->pc_sel.pc_dev,
208 p->pc_sel.pc_func, (p->pc_class << 16) |
209 (p->pc_subclass << 8) | p->pc_progif,
210 (p->pc_subdevice << 16) | p->pc_subvendor,
211 (p->pc_device << 16) | p->pc_vendor,
212 p->pc_revid, p->pc_hdr);
216 } while (pc.status == PCI_GETCONF_MORE_DEVS);
222 list_verbose(struct pci_conf *p)
224 struct pci_vendor_info *vi;
225 struct pci_device_info *di;
228 TAILQ_FOREACH(vi, &pci_vendors, link) {
229 if (vi->id == p->pc_vendor) {
230 printf(" vendor = '%s'\n", vi->desc);
237 TAILQ_FOREACH(di, &vi->devs, link) {
238 if (di->id == p->pc_device) {
239 printf(" device = '%s'\n", di->desc);
244 if ((dp = guess_class(p)) != NULL)
245 printf(" class = %s\n", dp);
246 if ((dp = guess_subclass(p)) != NULL)
247 printf(" subclass = %s\n", dp);
251 * This is a direct cut-and-paste from the table in sys/dev/pci/pci.c.
258 } pci_nomatch_tab[] = {
259 {PCIC_OLD, -1, "old"},
260 {PCIC_OLD, PCIS_OLD_NONVGA, "non-VGA display device"},
261 {PCIC_OLD, PCIS_OLD_VGA, "VGA-compatible display device"},
262 {PCIC_STORAGE, -1, "mass storage"},
263 {PCIC_STORAGE, PCIS_STORAGE_SCSI, "SCSI"},
264 {PCIC_STORAGE, PCIS_STORAGE_IDE, "ATA"},
265 {PCIC_STORAGE, PCIS_STORAGE_FLOPPY, "floppy disk"},
266 {PCIC_STORAGE, PCIS_STORAGE_IPI, "IPI"},
267 {PCIC_STORAGE, PCIS_STORAGE_RAID, "RAID"},
268 {PCIC_NETWORK, -1, "network"},
269 {PCIC_NETWORK, PCIS_NETWORK_ETHERNET, "ethernet"},
270 {PCIC_NETWORK, PCIS_NETWORK_TOKENRING, "token ring"},
271 {PCIC_NETWORK, PCIS_NETWORK_FDDI, "fddi"},
272 {PCIC_NETWORK, PCIS_NETWORK_ATM, "ATM"},
273 {PCIC_NETWORK, PCIS_NETWORK_ISDN, "ISDN"},
274 {PCIC_DISPLAY, -1, "display"},
275 {PCIC_DISPLAY, PCIS_DISPLAY_VGA, "VGA"},
276 {PCIC_DISPLAY, PCIS_DISPLAY_XGA, "XGA"},
277 {PCIC_DISPLAY, PCIS_DISPLAY_3D, "3D"},
278 {PCIC_MULTIMEDIA, -1, "multimedia"},
279 {PCIC_MULTIMEDIA, PCIS_MULTIMEDIA_VIDEO, "video"},
280 {PCIC_MULTIMEDIA, PCIS_MULTIMEDIA_AUDIO, "audio"},
281 {PCIC_MULTIMEDIA, PCIS_MULTIMEDIA_TELE, "telephony"},
282 {PCIC_MEMORY, -1, "memory"},
283 {PCIC_MEMORY, PCIS_MEMORY_RAM, "RAM"},
284 {PCIC_MEMORY, PCIS_MEMORY_FLASH, "flash"},
285 {PCIC_BRIDGE, -1, "bridge"},
286 {PCIC_BRIDGE, PCIS_BRIDGE_HOST, "HOST-PCI"},
287 {PCIC_BRIDGE, PCIS_BRIDGE_ISA, "PCI-ISA"},
288 {PCIC_BRIDGE, PCIS_BRIDGE_EISA, "PCI-EISA"},
289 {PCIC_BRIDGE, PCIS_BRIDGE_MCA, "PCI-MCA"},
290 {PCIC_BRIDGE, PCIS_BRIDGE_PCI, "PCI-PCI"},
291 {PCIC_BRIDGE, PCIS_BRIDGE_PCMCIA, "PCI-PCMCIA"},
292 {PCIC_BRIDGE, PCIS_BRIDGE_NUBUS, "PCI-NuBus"},
293 {PCIC_BRIDGE, PCIS_BRIDGE_CARDBUS, "PCI-CardBus"},
294 {PCIC_BRIDGE, PCIS_BRIDGE_RACEWAY, "PCI-RACEway"},
295 {PCIC_SIMPLECOMM, -1, "simple comms"},
296 {PCIC_SIMPLECOMM, PCIS_SIMPLECOMM_UART, "UART"}, /* could detect 16550 */
297 {PCIC_SIMPLECOMM, PCIS_SIMPLECOMM_PAR, "parallel port"},
298 {PCIC_SIMPLECOMM, PCIS_SIMPLECOMM_MULSER, "multiport serial"},
299 {PCIC_SIMPLECOMM, PCIS_SIMPLECOMM_MODEM, "generic modem"},
300 {PCIC_BASEPERIPH, -1, "base peripheral"},
301 {PCIC_BASEPERIPH, PCIS_BASEPERIPH_PIC, "interrupt controller"},
302 {PCIC_BASEPERIPH, PCIS_BASEPERIPH_DMA, "DMA controller"},
303 {PCIC_BASEPERIPH, PCIS_BASEPERIPH_TIMER, "timer"},
304 {PCIC_BASEPERIPH, PCIS_BASEPERIPH_RTC, "realtime clock"},
305 {PCIC_BASEPERIPH, PCIS_BASEPERIPH_PCIHOT, "PCI hot-plug controller"},
306 {PCIC_INPUTDEV, -1, "input device"},
307 {PCIC_INPUTDEV, PCIS_INPUTDEV_KEYBOARD, "keyboard"},
308 {PCIC_INPUTDEV, PCIS_INPUTDEV_DIGITIZER,"digitizer"},
309 {PCIC_INPUTDEV, PCIS_INPUTDEV_MOUSE, "mouse"},
310 {PCIC_INPUTDEV, PCIS_INPUTDEV_SCANNER, "scanner"},
311 {PCIC_INPUTDEV, PCIS_INPUTDEV_GAMEPORT, "gameport"},
312 {PCIC_DOCKING, -1, "docking station"},
313 {PCIC_PROCESSOR, -1, "processor"},
314 {PCIC_SERIALBUS, -1, "serial bus"},
315 {PCIC_SERIALBUS, PCIS_SERIALBUS_FW, "FireWire"},
316 {PCIC_SERIALBUS, PCIS_SERIALBUS_ACCESS, "AccessBus"},
317 {PCIC_SERIALBUS, PCIS_SERIALBUS_SSA, "SSA"},
318 {PCIC_SERIALBUS, PCIS_SERIALBUS_USB, "USB"},
319 {PCIC_SERIALBUS, PCIS_SERIALBUS_FC, "Fibre Channel"},
320 {PCIC_SERIALBUS, PCIS_SERIALBUS_SMBUS, "SMBus"},
321 {PCIC_WIRELESS, -1, "wireless controller"},
322 {PCIC_WIRELESS, PCIS_WIRELESS_IRDA, "iRDA"},
323 {PCIC_WIRELESS, PCIS_WIRELESS_IR, "IR"},
324 {PCIC_WIRELESS, PCIS_WIRELESS_RF, "RF"},
325 {PCIC_INTELLIIO, -1, "intelligent I/O controller"},
326 {PCIC_INTELLIIO, PCIS_INTELLIIO_I2O, "I2O"},
327 {PCIC_SATCOM, -1, "satellite communication"},
328 {PCIC_SATCOM, PCIS_SATCOM_TV, "sat TV"},
329 {PCIC_SATCOM, PCIS_SATCOM_AUDIO, "sat audio"},
330 {PCIC_SATCOM, PCIS_SATCOM_VOICE, "sat voice"},
331 {PCIC_SATCOM, PCIS_SATCOM_DATA, "sat data"},
332 {PCIC_CRYPTO, -1, "encrypt/decrypt"},
333 {PCIC_CRYPTO, PCIS_CRYPTO_NETCOMP, "network/computer crypto"},
334 {PCIC_CRYPTO, PCIS_CRYPTO_NETCOMP, "entertainment crypto"},
335 {PCIC_DASP, -1, "dasp"},
336 {PCIC_DASP, PCIS_DASP_DPIO, "DPIO module"},
341 guess_class(struct pci_conf *p)
345 for (i = 0; pci_nomatch_tab[i].desc != NULL; i++) {
346 if (pci_nomatch_tab[i].class == p->pc_class)
347 return(pci_nomatch_tab[i].desc);
353 guess_subclass(struct pci_conf *p)
357 for (i = 0; pci_nomatch_tab[i].desc != NULL; i++) {
358 if ((pci_nomatch_tab[i].class == p->pc_class) &&
359 (pci_nomatch_tab[i].subclass == p->pc_subclass))
360 return(pci_nomatch_tab[i].desc);
370 struct pci_vendor_info *cv;
371 struct pci_device_info *cd;
372 char buf[1024], str[1024];
377 * Locate the database and initialise.
379 TAILQ_INIT(&pci_vendors);
380 if ((dbf = getenv("PCICONF_VENDOR_DATABASE")) == NULL)
382 if ((db = fopen(dbf, "r")) == NULL)
389 * Scan input lines from the database
392 if (fgets(buf, sizeof(buf), db) == NULL)
395 if ((ch = strchr(buf, '#')) != NULL)
397 ch = strchr(buf, '\0') - 1;
398 while (ch > buf && isspace(*ch))
403 /* Can't handle subvendor / subdevice entries yet */
404 if (buf[0] == '\t' && buf[1] == '\t')
407 /* Check for vendor entry */
408 if (buf[0] != '\t' && sscanf(buf, "%04x %[^\n]", &id, str) == 2) {
409 if ((id == 0) || (strlen(str) < 1))
411 if ((cv = malloc(sizeof(struct pci_vendor_info))) == NULL) {
412 warn("allocating vendor entry");
416 if ((cv->desc = strdup(str)) == NULL) {
418 warn("allocating vendor description");
423 TAILQ_INIT(&cv->devs);
424 TAILQ_INSERT_TAIL(&pci_vendors, cv, link);
428 /* Check for device entry */
429 if (buf[0] == '\t' && sscanf(buf + 1, "%04x %[^\n]", &id, str) == 2) {
430 if ((id == 0) || (strlen(str) < 1))
433 warnx("device entry with no vendor!");
436 if ((cd = malloc(sizeof(struct pci_device_info))) == NULL) {
437 warn("allocating device entry");
441 if ((cd->desc = strdup(str)) == NULL) {
443 warn("allocating device description");
448 TAILQ_INSERT_TAIL(&cv->devs, cd, link);
452 /* It's a comment or junk, ignore it */
463 getsel(const char *str)
465 char *ep = strchr(str, '@');
476 if (strncmp(ep, "pci", 3) == 0) {
478 sel.pc_bus = strtoul(ep, &ep, 0);
479 if (!ep || *ep++ != ':')
480 errx(1, "cannot parse selector %s", str);
481 sel.pc_dev = strtoul(ep, &ep, 0);
482 if (!ep || *ep != ':') {
486 sel.pc_func = strtoul(ep, &ep, 0);
491 if (*ep != '\x0' || ep == epbase)
492 errx(1, "cannot parse selector %s", str);
497 readone(int fd, struct pcisel *sel, long reg, int width)
505 if (ioctl(fd, PCIOCREAD, &pi) < 0)
506 err(1, "ioctl(PCIOCREAD)");
508 printf("%0*x", width*2, pi.pi_data);
512 readit(const char *name, const char *reg, int width)
522 fd = open(_PATH_DEVPCI, O_RDWR, 0);
524 err(1, "%s", _PATH_DEVPCI);
526 rend = rstart = strtol(reg, &end, 0);
527 if (end && *end == ':') {
529 rend = strtol(end, (char **) 0, 0);
532 for (i = 1, r = rstart; r <= rend; i++, r += width) {
533 readone(fd, &sel, r, width);
534 if (i && !(i % 8)) putchar(' ');
535 putchar(i % (16/width) ? ' ' : '\n');
537 if (i % (16/width) != 1)
543 writeit(const char *name, const char *reg, const char *data, int width)
548 pi.pi_sel = getsel(name);
549 pi.pi_reg = strtoul(reg, (char **)0, 0); /* XXX error check */
551 pi.pi_data = strtoul(data, (char **)0, 0); /* XXX error check */
553 fd = open(_PATH_DEVPCI, O_RDWR, 0);
555 err(1, "%s", _PATH_DEVPCI);
557 if (ioctl(fd, PCIOCWRITE, &pi) < 0)
558 err(1, "ioctl(PCIOCWRITE)");
562 chkattached (const char *name, int width)
567 pi.pi_sel = getsel(name);
572 fd = open(_PATH_DEVPCI, O_RDWR, 0);
574 err(1, "%s", _PATH_DEVPCI);
576 if (ioctl(fd, PCIOCATTACHED, &pi) < 0)
577 err(1, "ioctl(PCIOCATTACHED)");
579 exitstatus = pi.pi_data ? 0 : 2; /* exit(2), if NOT attached */
580 printf("%s: %s%s\n", name, pi.pi_data == 0 ? "not " : "", "attached");