Skip to content

Commit 1c79b75

Browse files
committed
lsusb: add -v option to show bDeviceClass and DEVNAME
1 parent f72d270 commit 1c79b75

File tree

1 file changed

+22
-7
lines changed

1 file changed

+22
-7
lines changed

util-linux/lsusb.c

+22-7
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,25 @@
1515
static int FAST_FUNC fileAction(
1616
const char *fileName,
1717
struct stat *statbuf UNUSED_PARAM,
18-
void *userData UNUSED_PARAM,
18+
void *userData,
1919
int depth UNUSED_PARAM)
2020
{
2121
parser_t *parser;
2222
char *tokens[4];
23-
char *busnum = NULL, *devnum = NULL;
23+
char *busnum = NULL, *devnum = NULL, *devname = NULL;
2424
int product_vid = 0, product_did = 0;
2525
char *uevent_filename = concat_path_file(fileName, "/uevent");
2626

2727
parser = config_open2(uevent_filename, fopen_for_read);
2828
free(uevent_filename);
2929

30-
while (config_read(parser, tokens, 4, 2, "\\/=", PARSE_NORMAL)) {
30+
while (config_read(parser, tokens, 4, 2, "\\=", PARSE_NORMAL)) {
3131
if ((parser->lineno == 1) && strcmp(tokens[0], "DEVTYPE") == 0) {
3232
break;
3333
}
3434

3535
if (strcmp(tokens[0], "PRODUCT") == 0) {
36-
product_vid = xstrtou(tokens[1], 16);
37-
product_did = xstrtou(tokens[2], 16);
36+
sscanf(tokens[1], "%x/%x/", &product_vid, &product_did);
3837
continue;
3938
}
4039

@@ -47,13 +46,29 @@ static int FAST_FUNC fileAction(
4746
devnum = xstrdup(tokens[1]);
4847
continue;
4948
}
49+
50+
if (strcmp(tokens[0], "DEVNAME") == 0) {
51+
devname = xstrdup(tokens[1]);
52+
continue;
53+
}
5054
}
5155
config_close(parser);
5256

5357
if (busnum) {
54-
printf("Bus %s Device %s: ID %04x:%04x\n", busnum, devnum, product_vid, product_did);
58+
char extra[127] = "";
59+
if (userData) {
60+
int class;
61+
uevent_filename = concat_path_file(fileName, "/bDeviceClass");
62+
FILE *tfp = fopen_for_read(uevent_filename);
63+
free(uevent_filename);
64+
fscanf(tfp, "%x", &class);
65+
fclose(tfp);
66+
snprintf(extra, sizeof(extra), " Class: %02X\t/dev/%s", class, devname);
67+
}
68+
printf("Bus %s Device %s: ID %04x:%04x%s\n", busnum, devnum, product_vid, product_did, extra);
5569
free(busnum);
5670
free(devnum);
71+
free(devname);
5772
}
5873

5974
return TRUE;
@@ -68,7 +83,7 @@ int lsusb_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
6883
ACTION_RECURSE,
6984
fileAction,
7085
NULL, /* dirAction */
71-
NULL, /* userData */
86+
argc > 1 ? argv[1] : NULL, /* userData */
7287
0 /* depth */);
7388

7489
return EXIT_SUCCESS;

0 commit comments

Comments
 (0)