K2.6 patches and update.
[tomato.git] / release / src / router / udev / extras / usb_id / usb_id.c
blobd36a27b3ccbb5aafb9fb839323f6f974ab01294a
1 /*
2 * usb_id.c
4 * Identify an USB (block) device
6 * Copyright (c) 2005 SUSE Linux Products GmbH, Germany
8 * Author:
9 * Hannes Reinecke <hare@suse.de>
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation version 2 of the License.
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <stdarg.h>
19 #include <unistd.h>
20 #include <string.h>
21 #include <ctype.h>
22 #include <errno.h>
23 #include <getopt.h>
25 #include "../../udev.h"
27 #define MAX_PATH_LEN 512
28 #define MAX_SERIAL_LEN 256
29 #define BLKGETSIZE64 _IOR(0x12,114,size_t)
31 #ifdef USE_LOG
32 void log_message(int priority, const char *format, ...)
34 va_list args;
35 static int udev_log = -1;
37 if (udev_log == -1) {
38 const char *value;
40 value = getenv("UDEV_LOG");
41 if (value)
42 udev_log = log_priority(value);
43 else
44 udev_log = LOG_ERR;
47 if (priority > udev_log)
48 return;
50 va_start(args, format);
51 vsyslog(priority, format, args);
52 va_end(args);
54 #endif
56 static char vendor_str[64];
57 static char model_str[64];
58 static char serial_str[MAX_SERIAL_LEN];
59 static char revision_str[64];
60 static char type_str[64];
61 static char instance_str[64];
63 static int use_usb_info;
64 static int use_num_info;
66 static void set_str(char *to, const char *from, size_t count)
68 size_t i, j, len;
70 /* strip trailing whitespace */
71 len = strnlen(from, count);
72 while (len && isspace(from[len-1]))
73 len--;
75 /* strip leading whitespace */
76 i = 0;
77 while (isspace(from[i]) && (i < len))
78 i++;
80 j = 0;
81 while (i < len) {
82 /* substitute multiple whitespace */
83 if (isspace(from[i])) {
84 while (isspace(from[i]))
85 i++;
86 to[j++] = '_';
88 /* Replace '/' with '.' */
89 if (from[i] == '/') {
90 to[j++] = '.';
91 i++;
92 continue;
94 /* skip non-printable chars */
95 if (!isalnum(from[i]) && !ispunct(from[i])) {
96 i++;
97 continue;
99 to[j++] = from[i++];
101 to[j] = '\0';
104 static void set_usb_iftype(char *to, int if_class_num, size_t len)
106 char *type = "generic";
108 switch (if_class_num) {
109 case 1:
110 type = "audio";
111 break;
112 case 3:
113 type = "hid";
114 break;
115 case 7:
116 type = "printer";
117 break;
118 case 8:
119 type = "storage";
120 break;
121 case 2: /* CDC-Control */
122 case 5: /* Physical */
123 case 6: /* Image */
124 case 9: /* HUB */
125 case 0x0a: /* CDC-Data */
126 case 0x0b: /* Chip/Smart Card */
127 case 0x0d: /* Content Security */
128 case 0x0e: /* Video */
129 case 0xdc: /* Diagnostic Device */
130 case 0xe0: /* Wireless Controller */
131 case 0xf2: /* Application-specific */
132 case 0xff: /* Vendor-specific */
133 break;
134 default:
135 break;
137 strncpy(to, type, len);
138 to[len-1] = '\0';
141 static int set_usb_mass_storage_ifsubtype(char *to, const char *from, size_t len)
143 int type_num = 0;
144 char *eptr;
145 char *type = "generic";
147 type_num = strtoul(from, &eptr, 0);
148 if (eptr != from) {
149 switch (type_num) {
150 case 2:
151 type = "cd";
152 break;
153 case 3:
154 type = "tape";
155 break;
156 case 4: /* UFI */
157 case 5: /* SFF-8070i */
158 type = "floppy";
159 break;
160 case 1: /* RBC devices */
161 case 6: /* Transparent SPC-2 devices */
162 type = "disk";
163 break;
164 default:
165 break;
168 strlcpy(to, type, len);
170 return type_num;
173 static void set_scsi_type(char *to, const char *from, size_t len)
175 int type_num;
176 char *eptr;
177 char *type = "generic";
179 type_num = strtoul(from, &eptr, 0);
180 if (eptr != from) {
181 switch (type_num) {
182 case 0:
183 case 0xe:
184 type = "disk";
185 break;
186 case 1:
187 type = "tape";
188 break;
189 case 4:
190 case 7:
191 case 0xf:
192 type = "optical";
193 break;
194 case 5:
195 type = "cd";
196 break;
197 default:
198 break;
201 strlcpy(to, type, len);
205 * A unique USB identification is generated like this:
207 * 1.) Get the USB device type from DeviceClass, InterfaceClass
208 * and InterfaceSubClass
209 * 2.) If the device type is 'Mass-Storage/SPC-2' or 'Mass-Storage/RBC'
210 * use the SCSI vendor and model as USB-Vendor and USB-model.
211 * 3.) Otherwise use the USB manufacturer and product as
212 * USB-Vendor and USB-model. Any non-printable characters
213 * in those strings will be skipped; a slash '/' will be converted
214 * into a full stop '.'.
215 * 4.) If that fails, too, we will use idVendor and idProduct
216 * as USB-Vendor and USB-model.
217 * 5.) The USB identification is the USB-vendor and USB-model
218 * string concatenated with an underscore '_'.
219 * 6.) If the device supplies a serial number, this number
220 * is concatenated with the identification with an underscore '_'.
222 static int usb_id(const char *devpath)
224 struct sysfs_device *dev;
225 struct sysfs_device *dev_interface;
226 struct sysfs_device *dev_usb;
227 const char *scsi_model, *scsi_vendor, *scsi_type, *scsi_rev;
228 const char *usb_model = NULL, *usb_vendor = NULL, *usb_rev, *usb_serial;
229 const char *if_class, *if_subclass;
230 int if_class_num;
231 int protocol = 0;
233 dbg("devpath %s\n", devpath);
235 /* get all usb specific information: dev_interface, if_class, dev_usb */
236 dev = sysfs_device_get(devpath);
237 if (dev == NULL) {
238 err("unable to access '%s'", devpath);
239 return 1;
242 /* usb interface directory */
243 dev_interface = sysfs_device_get_parent_with_subsystem(dev, "usb");
244 if (dev_interface == NULL) {
245 info("unable to access usb_interface device of '%s'", devpath);
246 return 1;
249 if_class = sysfs_attr_get_value(dev_interface->devpath, "bInterfaceClass");
250 if (!if_class) {
251 info("%s: cannot get bInterfaceClass attribute", dev_interface->kernel);
252 return 1;
254 if_class_num = strtoul(if_class, NULL, 16);
255 if (if_class_num == 8) {
256 if_subclass = sysfs_attr_get_value(dev_interface->devpath, "bInterfaceSubClass");
257 if (if_subclass != NULL)
258 protocol = set_usb_mass_storage_ifsubtype(type_str, if_subclass, sizeof(type_str)-1);
259 } else
260 set_usb_iftype(type_str, if_class_num, sizeof(type_str)-1);
262 info("%s: if_class %d protocol %d\n", dev_interface->devpath, if_class_num, protocol);
264 /* usb device directory */
265 dev_usb = sysfs_device_get_parent_with_subsystem(dev_interface, "usb");
266 if (!dev_usb) {
267 info("unable to find parent 'usb' device of '%s'", devpath);
268 return 1;
271 /* mass storage */
272 if (protocol == 6 && !use_usb_info) {
273 struct sysfs_device *dev_scsi;
274 int host, bus, target, lun;
276 /* get scsi device */
277 dev_scsi = sysfs_device_get_parent_with_subsystem(dev, "scsi");
278 if (dev_scsi == NULL) {
279 info("unable to find parent 'scsi' device of '%s'", devpath);
280 goto fallback;
282 if (sscanf(dev_scsi->kernel, "%d:%d:%d:%d", &host, &bus, &target, &lun) != 4) {
283 info("invalid scsi device '%s'", dev_scsi->kernel);
284 goto fallback;
287 /* Generic SPC-2 device */
288 scsi_vendor = sysfs_attr_get_value(dev_scsi->devpath, "vendor");
289 if (!scsi_vendor) {
290 info("%s: cannot get SCSI vendor attribute", dev_scsi->kernel);
291 goto fallback;
293 set_str(vendor_str, scsi_vendor, sizeof(vendor_str)-1);
295 scsi_model = sysfs_attr_get_value(dev_scsi->devpath, "model");
296 if (!scsi_model) {
297 info("%s: cannot get SCSI model attribute", dev_scsi->kernel);
298 goto fallback;
300 set_str(model_str, scsi_model, sizeof(model_str)-1);
302 scsi_type = sysfs_attr_get_value(dev_scsi->devpath, "type");
303 if (!scsi_type) {
304 info("%s: cannot get SCSI type attribute", dev_scsi->kernel);
305 goto fallback;
307 set_scsi_type(type_str, scsi_type, sizeof(type_str)-1);
309 scsi_rev = sysfs_attr_get_value(dev_scsi->devpath, "rev");
310 if (!scsi_rev) {
311 info("%s: cannot get SCSI revision attribute", dev_scsi->kernel);
312 goto fallback;
314 set_str(revision_str, scsi_rev, sizeof(revision_str)-1);
317 * some broken devices have the same identifiers
318 * for all luns, export the target:lun number
320 sprintf(instance_str, "%d:%d", target, lun);
323 fallback:
324 /* Fallback to USB vendor & device */
325 if (vendor_str[0] == '\0') {
326 if (!use_num_info)
327 if (!(usb_vendor = sysfs_attr_get_value(dev_usb->devpath, "manufacturer")))
328 dbg("No USB vendor string found, using idVendor");
330 if (!usb_vendor) {
331 if (!(usb_vendor = sysfs_attr_get_value(dev_usb->devpath, "idVendor"))) {
332 dbg("No USB vendor information available\n");
333 sprintf(vendor_str,"0000");
336 set_str(vendor_str,usb_vendor, sizeof(vendor_str) - 1);
339 if (model_str[0] == '\0') {
340 if (!use_num_info)
341 if (!(usb_model = sysfs_attr_get_value(dev_usb->devpath, "product")))
342 dbg("No USB model string found, using idProduct");
344 if (!usb_model) {
345 if (!(usb_model = sysfs_attr_get_value(dev_usb->devpath, "idProduct")))
346 dbg("No USB model information available\n"); sprintf(model_str,"0000");
348 set_str(model_str, usb_model, sizeof(model_str) - 1);
351 if (revision_str[0] == '\0') {
352 usb_rev = sysfs_attr_get_value(dev_usb->devpath, "bcdDevice");
353 if (usb_rev)
354 set_str(revision_str, usb_rev, sizeof(revision_str)-1);
357 if (serial_str[0] == '\0') {
358 usb_serial = sysfs_attr_get_value(dev_usb->devpath, "serial");
359 if (usb_serial)
360 set_str(serial_str, usb_serial, sizeof(serial_str)-1);
362 return 0;
365 int main(int argc, char **argv)
367 int retval = 0;
368 const char *env;
369 char devpath[MAX_PATH_LEN];
370 static int export;
371 static const struct option options[] = {
372 { "usb-info", 0, NULL, 'u' },
373 { "num-info", 0, NULL, 'n' },
374 { "export", 0, NULL, 'x' },
375 { "help", 0, NULL, 'h' },
379 logging_init("usb_id");
380 sysfs_init();
382 while (1) {
383 int option;
385 option = getopt_long(argc, argv, "nuxh", options, NULL);
386 if (option == -1)
387 break;
389 switch (option) {
390 case 'n':
391 use_num_info = 1;
392 use_usb_info = 1;
393 break;
394 case 'u':
395 use_usb_info = 1;
396 break;
397 case 'x':
398 export = 1;
399 break;
400 case 'h':
401 printf("Usage: usb_id [--usb-info] [--num-info] [--export] [--help] <devpath>\n"
402 " --usb-info use usb strings instead\n"
403 " --num-info use numerical values\n"
404 " --export print values as environemt keys\n"
405 " --help print this help text\n\n");
406 default:
407 retval = 1;
408 goto exit;
412 env = getenv("DEVPATH");
413 if (env != NULL)
414 strlcpy(devpath, env, sizeof(devpath));
415 else {
416 if (argv[optind] == NULL) {
417 fprintf(stderr, "No device specified\n");
418 retval = 1;
419 goto exit;
421 strlcpy(devpath, argv[optind], sizeof(devpath));
424 retval = usb_id(devpath);
426 if (retval == 0) {
427 char serial[256];
429 strlcpy(serial, vendor_str, sizeof(serial));
430 strlcat(serial, "_", sizeof(serial));
431 strlcat(serial, model_str, sizeof(serial));
432 if (serial_str[0] != '\0') {
433 strlcat(serial, "_", sizeof(serial));
434 strlcat(serial, serial_str, sizeof(serial));
436 if (instance_str[0] != '\0') {
437 strlcat(serial, "-", sizeof(serial));
438 strlcat(serial, instance_str, sizeof(serial));
441 if (export) {
442 printf("ID_VENDOR=%s\n", vendor_str);
443 printf("ID_MODEL=%s\n", model_str);
444 printf("ID_REVISION=%s\n", revision_str);
445 printf("ID_SERIAL=%s\n", serial);
446 if (serial_str[0] != '\0')
447 printf("ID_SERIAL_SHORT=%s\n", serial_str);
448 printf("ID_TYPE=%s\n", type_str);
449 if (instance_str[0] != '\0')
450 printf("ID_INSTANCE=%s\n", instance_str);
451 printf("ID_BUS=usb\n");
452 } else
453 printf("%s\n", serial);
456 exit:
457 sysfs_cleanup();
458 logging_close();
459 return retval;