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