A
download usbmodules.c
Language: C
License: GPL
Copyright: (C) 1999 Thomas Sailer (sailer@ife.ee.ethz.ch) (C) 2000, 2001 Yggdrasil Computing, Inc.
LOC: 337
Project Info
Linux-USB Project(linux-usb)
Server: SourceForge
Type: cvs
...nux‑usb\linux‑usb\usbutils\
   .cvsignore
   aclocal.m4
   config.h.in
   configure.in
   devtree.c
   devtree.h
   list.h
   lsusb.8
   lsusb.c
   Makefile.am
   Makefile.in
   names.c
   names.h
   update-usbids.sh
   usb.ids
   usbmisc.c
   usbmisc.h
   usbmodules.8
   usbmodules.c
   usbmodules.h
   usbutils.spec

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
/*****************************************************************************/

/*
 *      usmodules.c  --  pcimodules like utility for the USB bus
 *     
 *	lsusb.c is derived from:
 *
 *	lspci.c					by Thomas Sailer,
 *	pcimodules.c				by Adam J. Richter
 *	linux-2.4.0-test10/include/linux/usb.h	probably by Randy Dunlap
 *
 *	The code in usbmodules not derived from elsewhere was written by
 *	Adam J. Richter.  David Brownell added the --mapfile and --version
 *	options. Aurelien Jarno modified the code to use libusb.
 *
 *	Copyright (C) 2000, 2001  Yggdrasil Computing, Inc.
 *      Copyright (C) 1999  Thomas Sailer (sailer@ife.ee.ethz.ch)
 *
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *
 */

/*****************************************************************************/

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <stdarg.h>
#include <sys/param.h>
#include <sys/utsname.h>
#include <usb.h>

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "usbmodules.h"
#include "usbmisc.h"

#define _GNU_SOURCE
#include <getopt.h>

#define OPT_STRING "c:d:hi:m:p:t:v"
static struct option long_options[] = {
	{"check",	required_argument,	NULL,	'c'},
	{"device",      required_argument,      NULL,   'd'},
	{"help",	no_argument, 		NULL,	'h'},
	{"interface",	required_argument,	NULL,	'i'},
	{"mapfile",     required_argument,      NULL,   'm'},
	{"product",	required_argument,	NULL,	'p'},
	{"type",	required_argument,	NULL,	't'},
	{"version",	no_argument, 		NULL,	'v'},
	{ 0,            0,                      NULL,   0}
};

#define MODDIR "/lib/modules"
#define USBMAP "modules.usbmap"

#define LINELENGTH     8000 

static char *checkname = NULL;
struct usbmap_entry *usbmap_list;

static void *
xmalloc(unsigned int size) {
       void *result = malloc(size);
       if (result == NULL) {
               fprintf(stderr, "Memory allocation failure.\n");
               exit(1);
       }
       return result;
}

static int
scan_without_flags(const char *line, struct usbmap_entry *entry, char *name) {
	unsigned int driver_info;
	if (sscanf(line,
		   "%s 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x",
		   name,
		   &entry->idVendor,
		   &entry->idProduct,
		   &entry->bcdDevice_lo,
		   &entry->bcdDevice_hi,
		   &entry->bDeviceClass,
		   &entry->bDeviceSubClass,
		   &entry->bDeviceProtocol,
		   &entry->bInterfaceClass,
		   &entry->bInterfaceSubClass,
		   &entry->bInterfaceProtocol,
		   &driver_info) != 12)
		return 0;

	entry->match_flags = 0;

	/* idVendor==0 is the wildcard for both idVendor and idProduct,
	   because idProduct==0 is a legitimate product ID. */
	if (entry->idVendor)
		entry->match_flags |= USB_MATCH_VENDOR | USB_MATCH_PRODUCT;

	if (entry->bcdDevice_lo)
		entry->match_flags |= USB_MATCH_DEV_LO;

	if (entry->bcdDevice_hi)
		entry->match_flags |= USB_MATCH_DEV_HI;

	if (entry->bDeviceClass)
		entry->match_flags |= USB_MATCH_DEV_CLASS;

	if (entry->bDeviceSubClass)
		entry->match_flags |= USB_MATCH_DEV_SUBCLASS;

	if (entry->bDeviceProtocol)
		entry->match_flags |= USB_MATCH_DEV_PROTOCOL;

	if (entry->bInterfaceClass)
		entry->match_flags |= USB_MATCH_INT_CLASS;

	if (entry->bInterfaceSubClass)
		entry->match_flags |= USB_MATCH_INT_SUBCLASS;

	if (entry->bInterfaceProtocol)
		entry->match_flags |= USB_MATCH_INT_PROTOCOL;

	return 1;
}

static int
scan_with_flags(const char *line, struct usbmap_entry *entry, char *name) {
	unsigned int driver_info;
	return (sscanf(line, "%s 0x%x 0x%x "
		       "0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x",
		       name,
		       &entry->match_flags,
		       &entry->idVendor,
		       &entry->idProduct,
		       &entry->bcdDevice_lo,
		       &entry->bcdDevice_hi,
		       &entry->bDeviceClass,
		       &entry->bDeviceSubClass,
		       &entry->bDeviceProtocol,
		       &entry->bInterfaceClass,
		       &entry->bInterfaceSubClass,
		       &entry->bInterfaceProtocol,
		       &driver_info) == 13);
}

void
read_modules_usbmap(char *pathname)
{
       char filename[MAXPATHLEN];
       FILE *usbmap_file;
       char line[LINELENGTH];
       struct usbmap_entry *prev;
       struct usbmap_entry *entry;
       char name[LINELENGTH];

       if (pathname == NULL) {
	       struct utsname utsname;
	       if (uname(&utsname) < 0) {
		       perror("uname");
		       exit(1);
	       }
	       sprintf(filename, "%s/%s/%s", MODDIR, utsname.release, USBMAP);
	       pathname = filename;
       }
       if ((usbmap_file = fopen(pathname, "r")) == NULL) {
               perror(pathname);
               exit(1);
       }

       prev = NULL;
       while(fgets(line, LINELENGTH, usbmap_file) != NULL) {
               if (line[0] == '#')
                       continue;

	       if (line[0] == '\n')
		       continue;

               entry = xmalloc(sizeof(struct usbmap_entry));

	       if (!scan_with_flags(line, entry, name) &&
		   !scan_without_flags(line, entry, name)) {
                       fprintf (stderr,
                               "modules.usbmap unparsable line: %s.\n", line);
                       free(entry);
                       continue;
               }

               /* Optimize memory allocation a bit, in case someday we
                  have Linux systems with ~100,000 modules.  It also
                  allows us to just compare pointers to avoid trying
                  to load a module twice. */
               if (prev == NULL || strcmp(name, prev->name) != 0) {
                       entry->name = xmalloc(strlen(name)+1);
                       strcpy(entry->name, name);
                       entry->selected_ptr = &entry->selected;
                       entry->selected = 0;
                       prev = entry;
               } else {
                       entry->name = prev->name;
                       entry->selected_ptr = prev->selected_ptr;
               }
               entry->next = usbmap_list;
               usbmap_list = entry;
       }
       fclose(usbmap_file);
}

/* Match modules is called once per interface.  We know that
   each device has at least one interface, because, according
   to the USB 2.0 Specification, section 9.6.3, "A USB device has
   one or more configuration descriptors.  Each configuration has
   one or more interfaces and each interface has zero or more endpoints."
   So, there must be at least one interface on a device.
*/

static void
match_modules(struct usb_device_descriptor *device_descriptor, 
              struct usb_interface_descriptor *interface_descriptor)
{
        struct usbmap_entry *mod;

        for (mod = usbmap_list; mod != NULL; mod = mod->next) {

                if ((mod->match_flags & USB_MATCH_VENDOR) &&
                    mod->idVendor != device_descriptor->idVendor)
                        continue;

                if ((mod->match_flags & USB_MATCH_PRODUCT) &&
                    mod->idProduct != device_descriptor->idProduct)
                        continue;

                if ((mod->match_flags & USB_MATCH_DEV_LO) &&
                    mod->bcdDevice_lo > device_descriptor->bcdDevice)
                        continue;

                if ((mod->match_flags & USB_MATCH_DEV_HI) &&
                    mod->bcdDevice_hi < device_descriptor->bcdDevice)
                        continue;

                if ((mod->match_flags & USB_MATCH_DEV_CLASS) &&
                    mod->bDeviceClass != device_descriptor->bDeviceClass)
                        continue;

                if ((mod->match_flags & USB_MATCH_DEV_SUBCLASS) &&
                    mod->bDeviceSubClass != device_descriptor->bDeviceSubClass)
                        continue;

                if ((mod->match_flags & USB_MATCH_DEV_PROTOCOL) &&
                    mod->bDeviceProtocol != device_descriptor->bDeviceProtocol)
                        continue;

                if ((mod->match_flags & USB_MATCH_INT_CLASS) &&
                    mod->bInterfaceClass != interface_descriptor->bInterfaceClass)
                        continue;

                if ((mod->match_flags & USB_MATCH_INT_SUBCLASS) &&
                    mod->bInterfaceSubClass != interface_descriptor->bInterfaceSubClass)
                        continue;

                if ((mod->match_flags & USB_MATCH_INT_PROTOCOL) &&
                    mod->bInterfaceProtocol != interface_descriptor->bInterfaceProtocol)
                        continue;

		if (checkname != NULL) {
			if (strcmp(checkname, mod->name) == 0)
				exit(0);  /* Program returns "success" */
		} else if (!(*mod->selected_ptr)) {
                        *(mod->selected_ptr) = 1;
                        printf ("%s\n", mod->name);
                }
        }
}

static void process_device(const char *path)
{
	struct usb_device *dev;
	struct usb_dev_handle *udev;
	int i, j, k;

	dev = get_usb_device(path);

	if (!dev) {
		fprintf(stderr, "Cannot open %s\n", path);	       
		return;
	}
		
			
	udev = usb_open(dev);
	
	for (i = 0 ; i < dev->descriptor.bNumConfigurations ; i++)
		for (j = 0 ; j < dev->config[i].bNumInterfaces ; j++) 
		       	for (k = 0 ; k < dev->config[i].interface[j].num_altsetting ; k++)
			        match_modules(&dev->descriptor, 
					      &dev->config[i].interface[j].altsetting[k]);

	usb_close(udev);
}

static void process_args(char *product,
			 char *type,
			 char *interface)
{
	int a, b, c;
	struct usb_device_descriptor dd;
	struct usb_interface_descriptor id;

	memset(&dd, 0, sizeof(dd));
	memset(&id, 0, sizeof(id));
	if (product == NULL ||
	    sscanf(product, "%hx/%hx/%hx", &dd.idVendor, &dd.idProduct, &dd.bcdDevice) != 3) {
		fprintf(stderr, "Bad product format: '%s'\n", product);
		return;
	}
	if (type == NULL || sscanf(type, "%d/%d/%d", &a, &b, &c) != 3) {
		fprintf(stderr, "Bad type format: '%s'", type);
		return;
	}
	dd.bDeviceClass = a;
	dd.bDeviceSubClass = b;
	dd.bDeviceProtocol = c;
	if (dd.bDeviceClass == 0) {
		/* interface must be specified for device class 0 */
		if (interface == NULL ||
		    sscanf(interface, "%d/%d/%d", &a, &b, &c) != 3) {
			fprintf(stderr, "Bad interface format: '%s'\n", interface);
			return;
		}
		id.bInterfaceClass = a;
		id.bInterfaceSubClass = b;
		id.bInterfaceProtocol = c;
	} else {
		/* interface maybe given. if so, check and use arg */
		if (interface != NULL && *interface != '\0' &&
		    sscanf(interface, "%d/%d/%d", &a, &b, &c) != 3) {
			fprintf(stderr, "Bad interface format: '%s'\n", interface);
			return;
		}
		id.bInterfaceClass = a;
		id.bInterfaceSubClass = b;
		id.bInterfaceProtocol = c;
	}
    	match_modules(&dd, &id);
}

int main (int argc, char *argv[])
{
       int opt_index = 0;
       int opt;
       char *device = NULL;
       char *pathname = NULL;
       char *product = NULL, *type = NULL, *interface = NULL;

       while ((opt = getopt_long(argc, argv, OPT_STRING, long_options, &opt_index)) != -1) {
               switch(opt) {
	       case 'c':
		       checkname = optarg;
		       break;
	       case 'd':
		       device = optarg;
		       break;
	       case 'h':
		       printf ("Usage: usbmodules [options]...\n"
			       "Lists kernel modules corresponding to USB devices currently plugged\n"
			       "\n"
			       "OPTIONS\n"
			       "  -d, --device /proc/bus/usb/NNN/NNN\n"
			       "      Selects which device usbmodules will examine\n"
			       "  -c, --check module\n"
			       "      Check if the given module's exported USB ID patterns matches\n"
			       "  -m, --mapfile /etc/hotplug/usb.handmap\n"
			       "      Specify a mapfile\n"
			       "  -p, --product xx/xx/xx\n"
			       "  -t, --type dd/dd/dd\n"
			       "  -i, --interface dd/dd/dd\n"
			       "  -h, --help\n"
			       "      Print help screen\n"
			       "  -v, --version\n"
			       "      Show version of program\n"
			       "\n");
		       return 0;
	       case 'm':
		       pathname = optarg;
		       break;
	       case 'i':
		       interface = optarg;
		       break;
	       case 'p':
		       product = optarg;
		       break;
	       case 't':
		       type = optarg;
		       break;
	       case 'v':
		       puts (VERSION);
		       return 0;
	       default:
		       fprintf(stderr,
			       "Unknown argument character \"%c\".\n",
			       opt);
		       return 1;
               }
       }       

       if (device == NULL &&
	   (product == NULL || type == NULL || interface == NULL) ) {
               fprintf (stderr,
                        "You must specify a device with something like:\n"
                        "\tusbmodules --device /proc/bus/usb/001/009\n"
			"or\n"
			"\tusbmodules --product 82d/100/100 --type 0/0/0 --interface 0/0/0\n");
              return 1;
       }
       
       read_modules_usbmap(pathname);
       usb_init();
       usb_find_busses();
       usb_find_devices();
       if (device != NULL)
	       process_device(device);
       if (product != NULL && type != NULL)
	       process_args(product, type, interface);

	if (checkname != NULL)
		return 1; /* The module being checked was not needed */

       return 0;
}

About Koders | Resources | Downloads | Support | Black Duck | Terms of Service | DMCA | Privacy Policy | Contact Us