Home | History | Annotate | Line # | Download | only in usbdevs
usbdevs.c revision 1.15
      1 /*	$NetBSD: usbdevs.c,v 1.15 2000/12/30 13:57:44 augustss Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Lennart Augustsson (augustss (at) netbsd.org).
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 #include <stdio.h>
     40 #include <stdlib.h>
     41 #include <string.h>
     42 #include <sys/types.h>
     43 #include <fcntl.h>
     44 #include <unistd.h>
     45 #include <err.h>
     46 #include <errno.h>
     47 #include <dev/usb/usb.h>
     48 
     49 #define USBDEV "/dev/usb"
     50 
     51 int verbose = 0;
     52 int showdevs = 0;
     53 
     54 void usage(void);
     55 void usbdev(int f, int a, int rec);
     56 void usbdump(int f);
     57 void dumpone(char *name, int f, int addr);
     58 int main(int, char **);
     59 
     60 extern char *__progname;
     61 
     62 void
     63 usage()
     64 {
     65 	fprintf(stderr, "Usage: %s [-v] [-a addr] [-f dev]\n", __progname);
     66 	exit(1);
     67 }
     68 
     69 char done[USB_MAX_DEVICES];
     70 int indent;
     71 
     72 void
     73 usbdev(int f, int a, int rec)
     74 {
     75 	struct usb_device_info di;
     76 	int e, p, i;
     77 
     78 	di.addr = a;
     79 	e = ioctl(f, USB_DEVICEINFO, &di);
     80 	if (e) {
     81 		if (errno != ENXIO)
     82 			printf("addr %d: I/O error\n", a);
     83 		return;
     84 	}
     85 	printf("addr %d: ", a);
     86 	done[a] = 1;
     87 	if (verbose) {
     88 		if (di.lowspeed)
     89 			printf("low speed, ");
     90 		if (di.power)
     91 			printf("power %d mA, ", di.power);
     92 		else
     93 			printf("self powered, ");
     94 		if (di.config)
     95 			printf("config %d, ", di.config);
     96 		else
     97 			printf("unconfigured, ");
     98 	}
     99 	if (verbose) {
    100 		printf("%s(0x%04x), %s(0x%04x), rev %s",
    101 		       di.product, di.productNo,
    102 		       di.vendor, di.vendorNo, di.release);
    103 	} else
    104 		printf("%s, %s", di.product, di.vendor);
    105 	printf("\n");
    106 	if (showdevs) {
    107 		for (i = 0; i < USB_MAX_DEVNAMES; i++)
    108 			if (di.devnames[i][0])
    109 				printf("%*s  %s\n", indent, "",
    110 				       di.devnames[i]);
    111 	}
    112 	if (!rec)
    113 		return;
    114 	for (p = 0; p < di.nports; p++) {
    115 		int s = di.ports[p];
    116 		if (s >= USB_MAX_DEVICES) {
    117 			if (verbose) {
    118 				printf("%*sport %d %s\n", indent+1, "", p+1,
    119 				       s == USB_PORT_ENABLED ? "enabled" :
    120 				       s == USB_PORT_SUSPENDED ? "suspended" :
    121 				       s == USB_PORT_POWERED ? "powered" :
    122 				       s == USB_PORT_DISABLED ? "disabled" :
    123 				       "???");
    124 
    125 			}
    126 			continue;
    127 		}
    128 		indent++;
    129 		printf("%*s", indent, "");
    130 		if (verbose)
    131 			printf("port %d ", p+1);
    132 		if (s == 0)
    133 			printf("addr 0 should never happen!\n");
    134 		else
    135 			usbdev(f, s, 1);
    136 		indent--;
    137 	}
    138 }
    139 
    140 void
    141 usbdump(int f)
    142 {
    143 	int a;
    144 
    145 	for (a = 1; a < USB_MAX_DEVICES; a++) {
    146 		if (!done[a])
    147 			usbdev(f, a, 1);
    148 	}
    149 }
    150 
    151 void
    152 dumpone(char *name, int f, int addr)
    153 {
    154 	if (verbose)
    155 		printf("Controller %s:\n", name);
    156 	indent = 0;
    157 	memset(done, 0, sizeof done);
    158 	if (addr)
    159 		usbdev(f, addr, 0);
    160 	else
    161 		usbdump(f);
    162 }
    163 
    164 int
    165 main(int argc, char **argv)
    166 {
    167 	int ch, i, f;
    168 	char buf[50];
    169 	char *dev = 0;
    170 	int addr = 0;
    171 	int ncont;
    172 
    173 	while ((ch = getopt(argc, argv, "a:df:v")) != -1) {
    174 		switch(ch) {
    175 		case 'a':
    176 			addr = atoi(optarg);
    177 			break;
    178 		case 'd':
    179 			showdevs++;
    180 			break;
    181 		case 'f':
    182 			dev = optarg;
    183 			break;
    184 		case 'v':
    185 			verbose = 1;
    186 			break;
    187 		case '?':
    188 		default:
    189 			usage();
    190 		}
    191 	}
    192 	argc -= optind;
    193 	argv += optind;
    194 
    195 	if (dev == 0) {
    196 		for (ncont = 0, i = 0; i < 10; i++) {
    197 			sprintf(buf, "%s%d", USBDEV, i);
    198 			f = open(buf, O_RDONLY);
    199 			if (f >= 0) {
    200 				dumpone(buf, f, addr);
    201 				close(f);
    202 			} else {
    203 				if (errno == ENOENT || errno == ENXIO)
    204 					continue;
    205 				warn("%s", buf);
    206 			}
    207 			ncont++;
    208 		}
    209 		if (verbose && ncont == 0)
    210 			printf("%s: no USB controllers found\n", __progname);
    211 	} else {
    212 		f = open(dev, O_RDONLY);
    213 		if (f >= 0)
    214 			dumpone(dev, f, addr);
    215 		else
    216 			err(1, "%s", dev);
    217 	}
    218 	exit(0);
    219 }
    220