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