xref: /freebsd/usr.sbin/pciconf/pciconf.c (revision c243e4902be8df1e643c76b5f18b68bb77cc5268)
1 /*
2  * Copyright 1996 Massachusetts Institute of Technology
3  *
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
14  * warranty.
15  *
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
27  * SUCH DAMAGE.
28  */
29 
30 #ifndef lint
31 static const char rcsid[] =
32   "$FreeBSD$";
33 #endif /* not lint */
34 
35 #include <sys/types.h>
36 #include <sys/fcntl.h>
37 
38 #include <ctype.h>
39 #include <err.h>
40 #include <inttypes.h>
41 #include <stdlib.h>
42 #include <stdio.h>
43 #include <string.h>
44 #include <unistd.h>
45 #include <sys/pciio.h>
46 #include <sys/queue.h>
47 
48 #include <dev/pci/pcireg.h>
49 
50 #include "pathnames.h"
51 #include "pciconf.h"
52 
53 struct pci_device_info
54 {
55     TAILQ_ENTRY(pci_device_info)	link;
56     int					id;
57     char				*desc;
58 };
59 
60 struct pci_vendor_info
61 {
62     TAILQ_ENTRY(pci_vendor_info)	link;
63     TAILQ_HEAD(,pci_device_info)	devs;
64     int					id;
65     char				*desc;
66 };
67 
68 TAILQ_HEAD(,pci_vendor_info)	pci_vendors;
69 
70 static void list_bars(int fd, struct pci_conf *p);
71 static void list_devs(int verbose, int bars, int caps, int errors);
72 static void list_verbose(struct pci_conf *p);
73 static const char *guess_class(struct pci_conf *p);
74 static const char *guess_subclass(struct pci_conf *p);
75 static int load_vendors(void);
76 static void readit(const char *, const char *, int);
77 static void writeit(const char *, const char *, const char *, int);
78 static void chkattached(const char *);
79 
80 static int exitstatus = 0;
81 
82 static void
83 usage(void)
84 {
85 	fprintf(stderr, "%s\n%s\n%s\n%s\n",
86 		"usage: pciconf -l [-bcev]",
87 		"       pciconf -a selector",
88 		"       pciconf -r [-b | -h] selector addr[:addr2]",
89 		"       pciconf -w [-b | -h] selector addr value");
90 	exit (1);
91 }
92 
93 int
94 main(int argc, char **argv)
95 {
96 	int c;
97 	int listmode, readmode, writemode, attachedmode;
98 	int bars, caps, errors, verbose;
99 	int byte, isshort;
100 
101 	listmode = readmode = writemode = attachedmode = 0;
102 	bars = caps = errors = verbose = byte = isshort = 0;
103 
104 	while ((c = getopt(argc, argv, "abcehlrwv")) != -1) {
105 		switch(c) {
106 		case 'a':
107 			attachedmode = 1;
108 			break;
109 
110 		case 'b':
111 			bars = 1;
112 			byte = 1;
113 			break;
114 
115 		case 'c':
116 			caps = 1;
117 			break;
118 
119 		case 'e':
120 			errors = 1;
121 			break;
122 
123 		case 'h':
124 			isshort = 1;
125 			break;
126 
127 		case 'l':
128 			listmode = 1;
129 			break;
130 
131 		case 'r':
132 			readmode = 1;
133 			break;
134 
135 		case 'w':
136 			writemode = 1;
137 			break;
138 
139 		case 'v':
140 			verbose = 1;
141 			break;
142 
143 		default:
144 			usage();
145 		}
146 	}
147 
148 	if ((listmode && optind != argc)
149 	    || (writemode && optind + 3 != argc)
150 	    || (readmode && optind + 2 != argc)
151 	    || (attachedmode && optind + 1 != argc))
152 		usage();
153 
154 	if (listmode) {
155 		list_devs(verbose, bars, caps, errors);
156 	} else if (attachedmode) {
157 		chkattached(argv[optind]);
158 	} else if (readmode) {
159 		readit(argv[optind], argv[optind + 1],
160 		    byte ? 1 : isshort ? 2 : 4);
161 	} else if (writemode) {
162 		writeit(argv[optind], argv[optind + 1], argv[optind + 2],
163 		    byte ? 1 : isshort ? 2 : 4);
164 	} else {
165 		usage();
166 	}
167 
168 	return exitstatus;
169 }
170 
171 static void
172 list_devs(int verbose, int bars, int caps, int errors)
173 {
174 	int fd;
175 	struct pci_conf_io pc;
176 	struct pci_conf conf[255], *p;
177 	int none_count = 0;
178 
179 	if (verbose)
180 		load_vendors();
181 
182 	fd = open(_PATH_DEVPCI, (caps || errors) ? O_RDWR : O_RDONLY, 0);
183 	if (fd < 0)
184 		err(1, "%s", _PATH_DEVPCI);
185 
186 	bzero(&pc, sizeof(struct pci_conf_io));
187 	pc.match_buf_len = sizeof(conf);
188 	pc.matches = conf;
189 
190 	do {
191 		if (ioctl(fd, PCIOCGETCONF, &pc) == -1)
192 			err(1, "ioctl(PCIOCGETCONF)");
193 
194 		/*
195 		 * 255 entries should be more than enough for most people,
196 		 * but if someone has more devices, and then changes things
197 		 * around between ioctls, we'll do the cheesy thing and
198 		 * just bail.  The alternative would be to go back to the
199 		 * beginning of the list, and print things twice, which may
200 		 * not be desirable.
201 		 */
202 		if (pc.status == PCI_GETCONF_LIST_CHANGED) {
203 			warnx("PCI device list changed, please try again");
204 			exitstatus = 1;
205 			close(fd);
206 			return;
207 		} else if (pc.status ==  PCI_GETCONF_ERROR) {
208 			warnx("error returned from PCIOCGETCONF ioctl");
209 			exitstatus = 1;
210 			close(fd);
211 			return;
212 		}
213 		for (p = conf; p < &conf[pc.num_matches]; p++) {
214 			printf("%s%d@pci%d:%d:%d:%d:\tclass=0x%06x card=0x%08x "
215 			    "chip=0x%08x rev=0x%02x hdr=0x%02x\n",
216 			    (p->pd_name && *p->pd_name) ? p->pd_name :
217 			    "none",
218 			    (p->pd_name && *p->pd_name) ? (int)p->pd_unit :
219 			    none_count++, p->pc_sel.pc_domain,
220 			    p->pc_sel.pc_bus, p->pc_sel.pc_dev,
221 			    p->pc_sel.pc_func, (p->pc_class << 16) |
222 			    (p->pc_subclass << 8) | p->pc_progif,
223 			    (p->pc_subdevice << 16) | p->pc_subvendor,
224 			    (p->pc_device << 16) | p->pc_vendor,
225 			    p->pc_revid, p->pc_hdr);
226 			if (verbose)
227 				list_verbose(p);
228 			if (bars)
229 				list_bars(fd, p);
230 			if (caps)
231 				list_caps(fd, p);
232 			if (errors)
233 				list_errors(fd, p);
234 		}
235 	} while (pc.status == PCI_GETCONF_MORE_DEVS);
236 
237 	close(fd);
238 }
239 
240 static void
241 list_bars(int fd, struct pci_conf *p)
242 {
243 	struct pci_bar_io bar;
244 	uint64_t base;
245 	const char *type;
246 	int i, range, max;
247 
248 	switch (p->pc_hdr & PCIM_HDRTYPE) {
249 	case PCIM_HDRTYPE_NORMAL:
250 		max = PCIR_MAX_BAR_0;
251 		break;
252 	case PCIM_HDRTYPE_BRIDGE:
253 		max = PCIR_MAX_BAR_1;
254 		break;
255 	case PCIM_HDRTYPE_CARDBUS:
256 		max = PCIR_MAX_BAR_2;
257 		break;
258 	default:
259 		return;
260 	}
261 
262 	for (i = 0; i <= max; i++) {
263 		bar.pbi_sel = p->pc_sel;
264 		bar.pbi_reg = PCIR_BAR(i);
265 		if (ioctl(fd, PCIOCGETBAR, &bar) < 0)
266 			continue;
267 		if (PCI_BAR_IO(bar.pbi_base)) {
268 			type = "I/O Port";
269 			range = 32;
270 			base = bar.pbi_base & PCIM_BAR_IO_BASE;
271 		} else {
272 			if (bar.pbi_base & PCIM_BAR_MEM_PREFETCH)
273 				type = "Prefetchable Memory";
274 			else
275 				type = "Memory";
276 			switch (bar.pbi_base & PCIM_BAR_MEM_TYPE) {
277 			case PCIM_BAR_MEM_32:
278 				range = 32;
279 				break;
280 			case PCIM_BAR_MEM_1MB:
281 				range = 20;
282 				break;
283 			case PCIM_BAR_MEM_64:
284 				range = 64;
285 				break;
286 			default:
287 				range = -1;
288 			}
289 			base = bar.pbi_base & ~((uint64_t)0xf);
290 		}
291 		printf("    bar   [%02x] = type %s, range %2d, base %#jx, ",
292 		    PCIR_BAR(i), type, range, (uintmax_t)base);
293 		printf("size %2d, %s\n", (int)bar.pbi_length,
294 		    bar.pbi_enabled ? "enabled" : "disabled");
295 	}
296 }
297 
298 static void
299 list_verbose(struct pci_conf *p)
300 {
301 	struct pci_vendor_info	*vi;
302 	struct pci_device_info	*di;
303 	const char *dp;
304 
305 	TAILQ_FOREACH(vi, &pci_vendors, link) {
306 		if (vi->id == p->pc_vendor) {
307 			printf("    vendor     = '%s'\n", vi->desc);
308 			break;
309 		}
310 	}
311 	if (vi == NULL) {
312 		di = NULL;
313 	} else {
314 		TAILQ_FOREACH(di, &vi->devs, link) {
315 			if (di->id == p->pc_device) {
316 				printf("    device     = '%s'\n", di->desc);
317 				break;
318 			}
319 		}
320 	}
321 	if ((dp = guess_class(p)) != NULL)
322 		printf("    class      = %s\n", dp);
323 	if ((dp = guess_subclass(p)) != NULL)
324 		printf("    subclass   = %s\n", dp);
325 }
326 
327 /*
328  * This is a direct cut-and-paste from the table in sys/dev/pci/pci.c.
329  */
330 static struct
331 {
332 	int	class;
333 	int	subclass;
334 	const char *desc;
335 } pci_nomatch_tab[] = {
336 	{PCIC_OLD,		-1,			"old"},
337 	{PCIC_OLD,		PCIS_OLD_NONVGA,	"non-VGA display device"},
338 	{PCIC_OLD,		PCIS_OLD_VGA,		"VGA-compatible display device"},
339 	{PCIC_STORAGE,		-1,			"mass storage"},
340 	{PCIC_STORAGE,		PCIS_STORAGE_SCSI,	"SCSI"},
341 	{PCIC_STORAGE,		PCIS_STORAGE_IDE,	"ATA"},
342 	{PCIC_STORAGE,		PCIS_STORAGE_FLOPPY,	"floppy disk"},
343 	{PCIC_STORAGE,		PCIS_STORAGE_IPI,	"IPI"},
344 	{PCIC_STORAGE,		PCIS_STORAGE_RAID,	"RAID"},
345 	{PCIC_STORAGE,		PCIS_STORAGE_ATA_ADMA,	"ATA (ADMA)"},
346 	{PCIC_STORAGE,		PCIS_STORAGE_SATA,	"SATA"},
347 	{PCIC_STORAGE,		PCIS_STORAGE_SAS,	"SAS"},
348 	{PCIC_NETWORK,		-1,			"network"},
349 	{PCIC_NETWORK,		PCIS_NETWORK_ETHERNET,	"ethernet"},
350 	{PCIC_NETWORK,		PCIS_NETWORK_TOKENRING,	"token ring"},
351 	{PCIC_NETWORK,		PCIS_NETWORK_FDDI,	"fddi"},
352 	{PCIC_NETWORK,		PCIS_NETWORK_ATM,	"ATM"},
353 	{PCIC_NETWORK,		PCIS_NETWORK_ISDN,	"ISDN"},
354 	{PCIC_DISPLAY,		-1,			"display"},
355 	{PCIC_DISPLAY,		PCIS_DISPLAY_VGA,	"VGA"},
356 	{PCIC_DISPLAY,		PCIS_DISPLAY_XGA,	"XGA"},
357 	{PCIC_DISPLAY,		PCIS_DISPLAY_3D,	"3D"},
358 	{PCIC_MULTIMEDIA,	-1,			"multimedia"},
359 	{PCIC_MULTIMEDIA,	PCIS_MULTIMEDIA_VIDEO,	"video"},
360 	{PCIC_MULTIMEDIA,	PCIS_MULTIMEDIA_AUDIO,	"audio"},
361 	{PCIC_MULTIMEDIA,	PCIS_MULTIMEDIA_TELE,	"telephony"},
362 	{PCIC_MULTIMEDIA,	PCIS_MULTIMEDIA_HDA,	"HDA"},
363 	{PCIC_MEMORY,		-1,			"memory"},
364 	{PCIC_MEMORY,		PCIS_MEMORY_RAM,	"RAM"},
365 	{PCIC_MEMORY,		PCIS_MEMORY_FLASH,	"flash"},
366 	{PCIC_BRIDGE,		-1,			"bridge"},
367 	{PCIC_BRIDGE,		PCIS_BRIDGE_HOST,	"HOST-PCI"},
368 	{PCIC_BRIDGE,		PCIS_BRIDGE_ISA,	"PCI-ISA"},
369 	{PCIC_BRIDGE,		PCIS_BRIDGE_EISA,	"PCI-EISA"},
370 	{PCIC_BRIDGE,		PCIS_BRIDGE_MCA,	"PCI-MCA"},
371 	{PCIC_BRIDGE,		PCIS_BRIDGE_PCI,	"PCI-PCI"},
372 	{PCIC_BRIDGE,		PCIS_BRIDGE_PCMCIA,	"PCI-PCMCIA"},
373 	{PCIC_BRIDGE,		PCIS_BRIDGE_NUBUS,	"PCI-NuBus"},
374 	{PCIC_BRIDGE,		PCIS_BRIDGE_CARDBUS,	"PCI-CardBus"},
375 	{PCIC_BRIDGE,		PCIS_BRIDGE_RACEWAY,	"PCI-RACEway"},
376 	{PCIC_SIMPLECOMM,	-1,			"simple comms"},
377 	{PCIC_SIMPLECOMM,	PCIS_SIMPLECOMM_UART,	"UART"},	/* could detect 16550 */
378 	{PCIC_SIMPLECOMM,	PCIS_SIMPLECOMM_PAR,	"parallel port"},
379 	{PCIC_SIMPLECOMM,	PCIS_SIMPLECOMM_MULSER,	"multiport serial"},
380 	{PCIC_SIMPLECOMM,	PCIS_SIMPLECOMM_MODEM,	"generic modem"},
381 	{PCIC_BASEPERIPH,	-1,			"base peripheral"},
382 	{PCIC_BASEPERIPH,	PCIS_BASEPERIPH_PIC,	"interrupt controller"},
383 	{PCIC_BASEPERIPH,	PCIS_BASEPERIPH_DMA,	"DMA controller"},
384 	{PCIC_BASEPERIPH,	PCIS_BASEPERIPH_TIMER,	"timer"},
385 	{PCIC_BASEPERIPH,	PCIS_BASEPERIPH_RTC,	"realtime clock"},
386 	{PCIC_BASEPERIPH,	PCIS_BASEPERIPH_PCIHOT,	"PCI hot-plug controller"},
387 	{PCIC_BASEPERIPH,	PCIS_BASEPERIPH_SDHC,	"SD host controller"},
388 	{PCIC_INPUTDEV,		-1,			"input device"},
389 	{PCIC_INPUTDEV,		PCIS_INPUTDEV_KEYBOARD,	"keyboard"},
390 	{PCIC_INPUTDEV,		PCIS_INPUTDEV_DIGITIZER,"digitizer"},
391 	{PCIC_INPUTDEV,		PCIS_INPUTDEV_MOUSE,	"mouse"},
392 	{PCIC_INPUTDEV,		PCIS_INPUTDEV_SCANNER,	"scanner"},
393 	{PCIC_INPUTDEV,		PCIS_INPUTDEV_GAMEPORT,	"gameport"},
394 	{PCIC_DOCKING,		-1,			"docking station"},
395 	{PCIC_PROCESSOR,	-1,			"processor"},
396 	{PCIC_SERIALBUS,	-1,			"serial bus"},
397 	{PCIC_SERIALBUS,	PCIS_SERIALBUS_FW,	"FireWire"},
398 	{PCIC_SERIALBUS,	PCIS_SERIALBUS_ACCESS,	"AccessBus"},
399 	{PCIC_SERIALBUS,	PCIS_SERIALBUS_SSA,	"SSA"},
400 	{PCIC_SERIALBUS,	PCIS_SERIALBUS_USB,	"USB"},
401 	{PCIC_SERIALBUS,	PCIS_SERIALBUS_FC,	"Fibre Channel"},
402 	{PCIC_SERIALBUS,	PCIS_SERIALBUS_SMBUS,	"SMBus"},
403 	{PCIC_WIRELESS,		-1,			"wireless controller"},
404 	{PCIC_WIRELESS,		PCIS_WIRELESS_IRDA,	"iRDA"},
405 	{PCIC_WIRELESS,		PCIS_WIRELESS_IR,	"IR"},
406 	{PCIC_WIRELESS,		PCIS_WIRELESS_RF,	"RF"},
407 	{PCIC_INTELLIIO,	-1,			"intelligent I/O controller"},
408 	{PCIC_INTELLIIO,	PCIS_INTELLIIO_I2O,	"I2O"},
409 	{PCIC_SATCOM,		-1,			"satellite communication"},
410 	{PCIC_SATCOM,		PCIS_SATCOM_TV,		"sat TV"},
411 	{PCIC_SATCOM,		PCIS_SATCOM_AUDIO,	"sat audio"},
412 	{PCIC_SATCOM,		PCIS_SATCOM_VOICE,	"sat voice"},
413 	{PCIC_SATCOM,		PCIS_SATCOM_DATA,	"sat data"},
414 	{PCIC_CRYPTO,		-1,			"encrypt/decrypt"},
415 	{PCIC_CRYPTO,		PCIS_CRYPTO_NETCOMP,	"network/computer crypto"},
416 	{PCIC_CRYPTO,		PCIS_CRYPTO_NETCOMP,	"entertainment crypto"},
417 	{PCIC_DASP,		-1,			"dasp"},
418 	{PCIC_DASP,		PCIS_DASP_DPIO,		"DPIO module"},
419 	{0, 0,		NULL}
420 };
421 
422 static const char *
423 guess_class(struct pci_conf *p)
424 {
425 	int	i;
426 
427 	for (i = 0; pci_nomatch_tab[i].desc != NULL; i++) {
428 		if (pci_nomatch_tab[i].class == p->pc_class)
429 			return(pci_nomatch_tab[i].desc);
430 	}
431 	return(NULL);
432 }
433 
434 static const char *
435 guess_subclass(struct pci_conf *p)
436 {
437 	int	i;
438 
439 	for (i = 0; pci_nomatch_tab[i].desc != NULL; i++) {
440 		if ((pci_nomatch_tab[i].class == p->pc_class) &&
441 		    (pci_nomatch_tab[i].subclass == p->pc_subclass))
442 			return(pci_nomatch_tab[i].desc);
443 	}
444 	return(NULL);
445 }
446 
447 static int
448 load_vendors(void)
449 {
450 	const char *dbf;
451 	FILE *db;
452 	struct pci_vendor_info *cv;
453 	struct pci_device_info *cd;
454 	char buf[1024], str[1024];
455 	char *ch;
456 	int id, error;
457 
458 	/*
459 	 * Locate the database and initialise.
460 	 */
461 	TAILQ_INIT(&pci_vendors);
462 	if ((dbf = getenv("PCICONF_VENDOR_DATABASE")) == NULL)
463 		dbf = _PATH_PCIVDB;
464 	if ((db = fopen(dbf, "r")) == NULL)
465 		return(1);
466 	cv = NULL;
467 	cd = NULL;
468 	error = 0;
469 
470 	/*
471 	 * Scan input lines from the database
472 	 */
473 	for (;;) {
474 		if (fgets(buf, sizeof(buf), db) == NULL)
475 			break;
476 
477 		if ((ch = strchr(buf, '#')) != NULL)
478 			*ch = '\0';
479 		ch = strchr(buf, '\0') - 1;
480 		while (ch > buf && isspace(*ch))
481 			*ch-- = '\0';
482 		if (ch <= buf)
483 			continue;
484 
485 		/* Can't handle subvendor / subdevice entries yet */
486 		if (buf[0] == '\t' && buf[1] == '\t')
487 			continue;
488 
489 		/* Check for vendor entry */
490 		if (buf[0] != '\t' && sscanf(buf, "%04x %[^\n]", &id, str) == 2) {
491 			if ((id == 0) || (strlen(str) < 1))
492 				continue;
493 			if ((cv = malloc(sizeof(struct pci_vendor_info))) == NULL) {
494 				warn("allocating vendor entry");
495 				error = 1;
496 				break;
497 			}
498 			if ((cv->desc = strdup(str)) == NULL) {
499 				free(cv);
500 				warn("allocating vendor description");
501 				error = 1;
502 				break;
503 			}
504 			cv->id = id;
505 			TAILQ_INIT(&cv->devs);
506 			TAILQ_INSERT_TAIL(&pci_vendors, cv, link);
507 			continue;
508 		}
509 
510 		/* Check for device entry */
511 		if (buf[0] == '\t' && sscanf(buf + 1, "%04x %[^\n]", &id, str) == 2) {
512 			if ((id == 0) || (strlen(str) < 1))
513 				continue;
514 			if (cv == NULL) {
515 				warnx("device entry with no vendor!");
516 				continue;
517 			}
518 			if ((cd = malloc(sizeof(struct pci_device_info))) == NULL) {
519 				warn("allocating device entry");
520 				error = 1;
521 				break;
522 			}
523 			if ((cd->desc = strdup(str)) == NULL) {
524 				free(cd);
525 				warn("allocating device description");
526 				error = 1;
527 				break;
528 			}
529 			cd->id = id;
530 			TAILQ_INSERT_TAIL(&cv->devs, cd, link);
531 			continue;
532 		}
533 
534 		/* It's a comment or junk, ignore it */
535 	}
536 	if (ferror(db))
537 		error = 1;
538 	fclose(db);
539 
540 	return(error);
541 }
542 
543 uint32_t
544 read_config(int fd, struct pcisel *sel, long reg, int width)
545 {
546 	struct pci_io pi;
547 
548 	pi.pi_sel = *sel;
549 	pi.pi_reg = reg;
550 	pi.pi_width = width;
551 
552 	if (ioctl(fd, PCIOCREAD, &pi) < 0)
553 		err(1, "ioctl(PCIOCREAD)");
554 
555 	return (pi.pi_data);
556 }
557 
558 static struct pcisel
559 getsel(const char *str)
560 {
561 	char *ep = strchr(str, '@');
562 	char *epbase;
563 	struct pcisel sel;
564 	unsigned long selarr[4];
565 	int i;
566 
567 	if (ep == NULL)
568 		ep = (char *)str;
569 	else
570 		ep++;
571 
572 	epbase = ep;
573 
574 	if (strncmp(ep, "pci", 3) == 0) {
575 		ep += 3;
576 		i = 0;
577 		do {
578 			selarr[i++] = strtoul(ep, &ep, 10);
579 		} while ((*ep == ':' || *ep == '.') && *++ep != '\0' && i < 4);
580 
581 		if (i > 2)
582 			sel.pc_func = selarr[--i];
583 		else
584 			sel.pc_func = 0;
585 		sel.pc_dev = selarr[--i];
586 		sel.pc_bus = selarr[--i];
587 		if (i > 0)
588 			sel.pc_domain = selarr[--i];
589 		else
590 			sel.pc_domain = 0;
591 	}
592 	if (*ep != '\x0' || ep == epbase)
593 		errx(1, "cannot parse selector %s", str);
594 	return sel;
595 }
596 
597 static void
598 readone(int fd, struct pcisel *sel, long reg, int width)
599 {
600 
601 	printf("%0*x", width*2, read_config(fd, sel, reg, width));
602 }
603 
604 static void
605 readit(const char *name, const char *reg, int width)
606 {
607 	long rstart;
608 	long rend;
609 	long r;
610 	char *end;
611 	int i;
612 	int fd;
613 	struct pcisel sel;
614 
615 	fd = open(_PATH_DEVPCI, O_RDWR, 0);
616 	if (fd < 0)
617 		err(1, "%s", _PATH_DEVPCI);
618 
619 	rend = rstart = strtol(reg, &end, 0);
620 	if (end && *end == ':') {
621 		end++;
622 		rend = strtol(end, (char **) 0, 0);
623 	}
624 	sel = getsel(name);
625 	for (i = 1, r = rstart; r <= rend; i++, r += width) {
626 		readone(fd, &sel, r, width);
627 		if (i && !(i % 8))
628 			putchar(' ');
629 		putchar(i % (16/width) ? ' ' : '\n');
630 	}
631 	if (i % (16/width) != 1)
632 		putchar('\n');
633 	close(fd);
634 }
635 
636 static void
637 writeit(const char *name, const char *reg, const char *data, int width)
638 {
639 	int fd;
640 	struct pci_io pi;
641 
642 	pi.pi_sel = getsel(name);
643 	pi.pi_reg = strtoul(reg, (char **)0, 0); /* XXX error check */
644 	pi.pi_width = width;
645 	pi.pi_data = strtoul(data, (char **)0, 0); /* XXX error check */
646 
647 	fd = open(_PATH_DEVPCI, O_RDWR, 0);
648 	if (fd < 0)
649 		err(1, "%s", _PATH_DEVPCI);
650 
651 	if (ioctl(fd, PCIOCWRITE, &pi) < 0)
652 		err(1, "ioctl(PCIOCWRITE)");
653 }
654 
655 static void
656 chkattached(const char *name)
657 {
658 	int fd;
659 	struct pci_io pi;
660 
661 	pi.pi_sel = getsel(name);
662 
663 	fd = open(_PATH_DEVPCI, O_RDWR, 0);
664 	if (fd < 0)
665 		err(1, "%s", _PATH_DEVPCI);
666 
667 	if (ioctl(fd, PCIOCATTACHED, &pi) < 0)
668 		err(1, "ioctl(PCIOCATTACHED)");
669 
670 	exitstatus = pi.pi_data ? 0 : 2; /* exit(2), if NOT attached */
671 	printf("%s: %s%s\n", name, pi.pi_data == 0 ? "not " : "", "attached");
672 }
673