Home | History | Annotate | Line # | Download | only in usbdevs
usbdevs.c revision 1.5
      1 /*	$NetBSD: usbdevs.c,v 1.5 1998/11/25 22:17:08 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;
     52 
     53 void usage __P((void));
     54 void usbdev __P((int f, int a, int rec));
     55 void usbdump __P((int f));
     56 void dumpone __P((char *name, int f, int addr));
     57 int main __P((int, char **));
     58 
     59 extern char *__progname;
     60 
     61 void
     62 usage()
     63 {
     64 	fprintf(stderr, "Usage: %s [-a addr] [-f dev] [-v]\n", __progname);
     65 	exit(1);
     66 }
     67 
     68 char done[USB_MAX_DEVICES];
     69 int indent;
     70 
     71 void
     72 usbdev(f, a, rec)
     73 	int f;
     74 	int a;
     75 	int rec;
     76 {
     77 	struct usb_device_info di;
     78 	int e, p;
     79 
     80 	di.addr = a;
     81 	e = ioctl(f, USB_DEVICEINFO, &di);
     82 	if (e)
     83 		return;
     84 	done[a] = 1;
     85 	printf("addr %d: ", di.addr);
     86 	if (verbose) {
     87 		if (di.lowspeed)
     88 			printf("low speed, ");
     89 		if (di.power)
     90 			printf("power %d mA, ", di.power);
     91 		else
     92 			printf("self powered, ");
     93 		if (di.config)
     94 			printf("config %d, ", di.config);
     95 		else
     96 			printf("unconfigured, ");
     97 	}
     98 	printf("%s, %s", di.product, di.vendor);
     99 	if (verbose)
    100 		printf(", rev %s", di.revision);
    101 	printf("\n");
    102 	if (!rec)
    103 		return;
    104 	for (p = 0; p < di.nports; p++) {
    105 		int s = di.ports[p];
    106 		if (s >= USB_MAX_DEVICES) {
    107 			if (verbose) {
    108 				printf("%*sport %d %s\n", indent+1, "", p+1,
    109 				       s == USB_PORT_ENABLED ? "enabled" :
    110 				       s == USB_PORT_SUSPENDED ? "suspended" :
    111 				       s == USB_PORT_POWERED ? "powered" :
    112 				       s == USB_PORT_DISABLED ? "disabled" :
    113 				       "???");
    114 
    115 			}
    116 			continue;
    117 		}
    118 		indent++;
    119 		printf("%*s", indent, "");
    120 		if (verbose)
    121 			printf("port %d ", p+1);
    122 		usbdev(f, di.ports[p], 1);
    123 		indent--;
    124 	}
    125 }
    126 
    127 void
    128 usbdump(f)
    129 	int f;
    130 {
    131 	int a;
    132 
    133 	for (a = 1; a < USB_MAX_DEVICES; a++) {
    134 		if (!done[a])
    135 			usbdev(f, a, 1);
    136 	}
    137 }
    138 
    139 void
    140 dumpone(name, f, addr)
    141 	char *name;
    142 	int f;
    143 	int addr;
    144 {
    145 	if (verbose)
    146 		printf("Controller %s:\n", name);
    147 	indent = 0;
    148 	memset(done, 0, sizeof done);
    149 	if (addr)
    150 		usbdev(f, addr, 0);
    151 	else
    152 		usbdump(f);
    153 }
    154 
    155 int
    156 main(argc, argv)
    157 	int argc;
    158 	char **argv;
    159 {
    160 	int ch, i, f;
    161 	char buf[50];
    162 	extern int optind;
    163 	extern char *optarg;
    164 	char *dev = 0;
    165 	int addr = 0;
    166 	int ncont;
    167 
    168 	while ((ch = getopt(argc, argv, "a:f:v")) != -1) {
    169 		switch(ch) {
    170 		case 'a':
    171 			addr = atoi(optarg);
    172 			break;
    173 		case 'f':
    174 			dev = optarg;
    175 			break;
    176 		case 'v':
    177 			verbose = 1;
    178 			break;
    179 		case '?':
    180 		default:
    181 			usage();
    182 		}
    183 	}
    184 	argc -= optind;
    185 	argv += optind;
    186 
    187 	if (dev == 0) {
    188 		for (ncont = 0, i = 0; i < 10; i++) {
    189 			sprintf(buf, "%s%d", USBDEV, i);
    190 			f = open(buf, O_RDONLY);
    191 			if (f >= 0) {
    192 				ncont++;
    193 				dumpone(buf, f, addr);
    194 				close(f);
    195 			} else {
    196 				if (errno == EACCES)
    197 					warn("%s", buf);
    198 			}
    199 		}
    200 		if (verbose && ncont == 0)
    201 			printf("%s: no USB controllers found\n", __progname);
    202 	} else {
    203 		f = open(dev, O_RDONLY);
    204 		if (f >= 0)
    205 			dumpone(dev, f, addr);
    206 		else
    207 			err(1, "%s", dev);
    208 	}
    209 	exit(0);
    210 }
    211