xref: /freebsd/usr.sbin/usbconfig/dump.c (revision 9162f64b58d01ec01481d60b6cdc06ffd8e8c7fc)
1 /* $FreeBSD$ */
2 /*-
3  * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <stdint.h>
30 #include <err.h>
31 #include <string.h>
32 #include <pwd.h>
33 #include <grp.h>
34 #include <ctype.h>
35 
36 #include <libusb20.h>
37 #include <libusb20_desc.h>
38 
39 #include "dump.h"
40 
41 #define	DUMP0(n,type,field,...) dump_field(pdev, "  ", #field, n->field);
42 #define	DUMP1(n,type,field,...) dump_field(pdev, "    ", #field, n->field);
43 #define	DUMP2(n,type,field,...) dump_field(pdev, "      ", #field, n->field);
44 #define	DUMP3(n,type,field,...) dump_field(pdev, "        ", #field, n->field);
45 
46 const char *
47 dump_mode(uint8_t value)
48 {
49 	if (value == LIBUSB20_MODE_HOST)
50 		return ("HOST");
51 	return ("DEVICE");
52 }
53 
54 const char *
55 dump_speed(uint8_t value)
56 {
57 	;				/* style fix */
58 	switch (value) {
59 	case LIBUSB20_SPEED_LOW:
60 		return ("LOW (1.5Mbps)");
61 	case LIBUSB20_SPEED_FULL:
62 		return ("FULL (12Mbps)");
63 	case LIBUSB20_SPEED_HIGH:
64 		return ("HIGH (480Mbps)");
65 	case LIBUSB20_SPEED_VARIABLE:
66 		return ("VARIABLE (52-480Mbps)");
67 	case LIBUSB20_SPEED_SUPER:
68 		return ("SUPER (4.8Gbps)");
69 	default:
70 		break;
71 	}
72 	return ("unknown");
73 }
74 
75 const char *
76 dump_power_mode(uint8_t value)
77 {
78 	;				/* style fix */
79 	switch (value) {
80 	case LIBUSB20_POWER_OFF:
81 		return ("OFF");
82 	case LIBUSB20_POWER_ON:
83 		return ("ON");
84 	case LIBUSB20_POWER_SAVE:
85 		return ("SAVE");
86 	case LIBUSB20_POWER_SUSPEND:
87 		return ("SUSPEND");
88 	case LIBUSB20_POWER_RESUME:
89 		return ("RESUME");
90 	default:
91 		return ("UNKNOWN");
92 	}
93 }
94 
95 static void
96 dump_field(struct libusb20_device *pdev, const char *plevel,
97     const char *field, uint32_t value)
98 {
99 	uint8_t temp_string[256];
100 
101 	printf("%s%s = 0x%04x ", plevel, field, value);
102 
103 	if ((field[0] != 'i') || (field[1] == 'd')) {
104 		printf("\n");
105 		return;
106 	}
107 	if (value == 0) {
108 		printf(" <no string>\n");
109 		return;
110 	}
111 	if (libusb20_dev_req_string_simple_sync(pdev, value,
112 	    temp_string, sizeof(temp_string))) {
113 		printf(" <retrieving string failed>\n");
114 		return;
115 	}
116 	printf(" <%s>\n", temp_string);
117 	return;
118 }
119 
120 static void
121 dump_extra(struct libusb20_me_struct *str, const char *plevel)
122 {
123 	const uint8_t *ptr;
124 	uint8_t x;
125 
126 	ptr = NULL;
127 
128 	while ((ptr = libusb20_desc_foreach(str, ptr))) {
129 		printf("\n" "%sAdditional Descriptor\n\n", plevel);
130 		printf("%sbLength = 0x%02x\n", plevel, ptr[0]);
131 		printf("%sbDescriptorType = 0x%02x\n", plevel, ptr[1]);
132 		if (ptr[0] > 1)
133 			printf("%sbDescriptorSubType = 0x%02x\n",
134 			    plevel, ptr[2]);
135 		printf("%s RAW dump: ", plevel);
136 		for (x = 0; x != ptr[0]; x++) {
137 			if ((x % 8) == 0) {
138 				printf("\n%s 0x%02x | ", plevel, x);
139 			}
140 			printf("0x%02x%s", ptr[x],
141 			    (x != (ptr[0] - 1)) ? ", " : (x % 8) ? "\n" : "");
142 		}
143 		printf("\n");
144 	}
145 	return;
146 }
147 
148 static void
149 dump_endpoint(struct libusb20_device *pdev,
150     struct libusb20_endpoint *ep)
151 {
152 	struct LIBUSB20_ENDPOINT_DESC_DECODED *edesc;
153 
154 	edesc = &ep->desc;
155 	LIBUSB20_ENDPOINT_DESC(DUMP3, edesc);
156 	dump_extra(&ep->extra, "  " "  " "  ");
157 	return;
158 }
159 
160 static void
161 dump_iface(struct libusb20_device *pdev,
162     struct libusb20_interface *iface)
163 {
164 	struct LIBUSB20_INTERFACE_DESC_DECODED *idesc;
165 	uint8_t z;
166 
167 	idesc = &iface->desc;
168 	LIBUSB20_INTERFACE_DESC(DUMP2, idesc);
169 	dump_extra(&iface->extra, "  " "  " "  ");
170 
171 	for (z = 0; z != iface->num_endpoints; z++) {
172 		printf("\n     Endpoint %u\n", z);
173 		dump_endpoint(pdev, iface->endpoints + z);
174 	}
175 	return;
176 }
177 
178 void
179 dump_device_info(struct libusb20_device *pdev)
180 {
181 	printf("%s, cfg=%u md=%s spd=%s pwr=%s\n",
182 	    libusb20_dev_get_desc(pdev),
183 	    libusb20_dev_get_config_index(pdev),
184 	    dump_mode(libusb20_dev_get_mode(pdev)),
185 	    dump_speed(libusb20_dev_get_speed(pdev)),
186 	    dump_power_mode(libusb20_dev_get_power_mode(pdev)));
187 	return;
188 }
189 
190 void
191 dump_be_quirk_names(struct libusb20_backend *pbe)
192 {
193 	struct libusb20_quirk q;
194 	uint16_t x;
195 	int error;
196 
197 	memset(&q, 0, sizeof(q));
198 
199 	printf("\nDumping list of supported quirks:\n\n");
200 
201 	for (x = 0; x != 0xFFFF; x++) {
202 
203 		error = libusb20_be_get_quirk_name(pbe, x, &q);
204 		if (error) {
205 			if (x == 0) {
206 				printf("No quirk names - maybe the USB quirk "
207 				    "module has not been loaded.\n");
208 			}
209 			break;
210 		}
211 		if (strcmp(q.quirkname, "UQ_NONE"))
212 			printf("%s\n", q.quirkname);
213 	}
214 	printf("\n");
215 	return;
216 }
217 
218 void
219 dump_be_dev_quirks(struct libusb20_backend *pbe)
220 {
221 	struct libusb20_quirk q;
222 	uint16_t x;
223 	int error;
224 
225 	memset(&q, 0, sizeof(q));
226 
227 	printf("\nDumping current device quirks:\n\n");
228 
229 	for (x = 0; x != 0xFFFF; x++) {
230 
231 		error = libusb20_be_get_dev_quirk(pbe, x, &q);
232 		if (error) {
233 			if (x == 0) {
234 				printf("No device quirks - maybe the USB quirk "
235 				    "module has not been loaded.\n");
236 			}
237 			break;
238 		}
239 		if (strcmp(q.quirkname, "UQ_NONE")) {
240 			printf("VID=0x%04x PID=0x%04x REVLO=0x%04x "
241 			    "REVHI=0x%04x QUIRK=%s\n",
242 			    q.vid, q.pid, q.bcdDeviceLow,
243 			    q.bcdDeviceHigh, q.quirkname);
244 		}
245 	}
246 	printf("\n");
247 	return;
248 }
249 
250 void
251 dump_be_access(struct libusb20_backend *pbe)
252 {
253 	struct group *gr;
254 	struct passwd *pw;
255 	const char *owner;
256 	const char *group;
257 	uid_t uid;
258 	gid_t gid;
259 	mode_t mode;
260 
261 	if (libusb20_be_get_owner(pbe, &uid, &gid)) {
262 		err(1, "could not get owner");
263 	}
264 	if (libusb20_be_get_perm(pbe, &mode)) {
265 		err(1, "could not get permission");
266 	}
267 	owner = (pw = getpwuid(uid)) ? pw->pw_name : "UNKNOWN";
268 	group = (gr = getgrgid(gid)) ? gr->gr_name : "UNKNOWN";
269 
270 	if (mode || 1) {
271 		printf("Global Access: %s:%s 0%o\n", owner, group, mode);
272 	} else {
273 		printf("Global Access: <not set>\n");
274 	}
275 	return;
276 }
277 
278 void
279 dump_device_access(struct libusb20_device *pdev, uint8_t iface)
280 {
281 	struct group *gr;
282 	struct passwd *pw;
283 	const char *owner;
284 	const char *group;
285 	uid_t uid;
286 	gid_t gid;
287 	mode_t mode;
288 
289 	if (libusb20_dev_get_owner(pdev, &uid, &gid)) {
290 		err(1, "could not get owner");
291 	}
292 	if (libusb20_dev_get_perm(pdev, &mode)) {
293 		err(1, "could not get permission");
294 	}
295 	if (mode) {
296 		owner = (pw = getpwuid(uid)) ? pw->pw_name : "UNKNOWN";
297 		group = (gr = getgrgid(gid)) ? gr->gr_name : "UNKNOWN";
298 
299 		printf("  " "Device Access: %s:%s 0%o\n", owner, group, mode);
300 
301 	} else {
302 		printf("  " "Device Access: <not set>\n");
303 	}
304 
305 	if (iface == 0xFF) {
306 		for (iface = 0; iface != 0xFF; iface++) {
307 			if (dump_device_iface_access(pdev, iface)) {
308 				break;
309 			}
310 		}
311 	} else {
312 		if (dump_device_iface_access(pdev, iface)) {
313 			err(1, "could not get interface access info");
314 		}
315 	}
316 	return;
317 }
318 
319 int
320 dump_device_iface_access(struct libusb20_device *pdev, uint8_t iface)
321 {
322 	struct group *gr;
323 	struct passwd *pw;
324 	const char *owner;
325 	const char *group;
326 	uid_t uid;
327 	gid_t gid;
328 	mode_t mode;
329 	int error;
330 
331 	if ((error = libusb20_dev_get_iface_owner(pdev, iface, &uid, &gid))) {
332 		return (error);
333 	}
334 	if ((error = libusb20_dev_get_iface_perm(pdev, iface, &mode))) {
335 		return (error);
336 	}
337 	if (mode) {
338 
339 		owner = (pw = getpwuid(uid)) ? pw->pw_name : "UNKNOWN";
340 		group = (gr = getgrgid(gid)) ? gr->gr_name : "UNKNOWN";
341 
342 		printf("    " "Interface %u Access: %s:%s 0%o\n", iface, owner, group, mode);
343 	} else {
344 		printf("    " "Interface %u Access: <not set>\n", iface);
345 	}
346 
347 	return (0);
348 }
349 
350 void
351 dump_device_desc(struct libusb20_device *pdev)
352 {
353 	struct LIBUSB20_DEVICE_DESC_DECODED *ddesc;
354 
355 	ddesc = libusb20_dev_get_device_desc(pdev);
356 	LIBUSB20_DEVICE_DESC(DUMP0, ddesc);
357 	return;
358 }
359 
360 void
361 dump_config(struct libusb20_device *pdev, uint8_t all_cfg)
362 {
363 	struct LIBUSB20_CONFIG_DESC_DECODED *cdesc;
364 	struct LIBUSB20_DEVICE_DESC_DECODED *ddesc;
365 	struct libusb20_config *pcfg = NULL;
366 	uint8_t cfg_index;
367 	uint8_t cfg_index_end;
368 	uint8_t x;
369 	uint8_t y;
370 
371 	ddesc = libusb20_dev_get_device_desc(pdev);
372 
373 	if (all_cfg) {
374 		cfg_index = 0;
375 		cfg_index_end = ddesc->bNumConfigurations;
376 	} else {
377 		cfg_index = libusb20_dev_get_config_index(pdev);
378 		cfg_index_end = cfg_index + 1;
379 	}
380 
381 	for (; cfg_index != cfg_index_end; cfg_index++) {
382 
383 		pcfg = libusb20_dev_alloc_config(pdev, cfg_index);
384 		if (!pcfg) {
385 			continue;
386 		}
387 		printf("\n Configuration index %u\n\n", cfg_index);
388 		cdesc = &(pcfg->desc);
389 		LIBUSB20_CONFIG_DESC(DUMP1, cdesc);
390 		dump_extra(&(pcfg->extra), "  " "  ");
391 
392 		for (x = 0; x != pcfg->num_interface; x++) {
393 			printf("\n    Interface %u\n", x);
394 			dump_iface(pdev, pcfg->interface + x);
395 			printf("\n");
396 			for (y = 0; y != (pcfg->interface + x)->num_altsetting; y++) {
397 				printf("\n    Interface %u Alt %u\n", x, y + 1);
398 				dump_iface(pdev,
399 				    (pcfg->interface + x)->altsetting + y);
400 				printf("\n");
401 			}
402 		}
403 		printf("\n");
404 		free(pcfg);
405 	}
406 	return;
407 }
408