chiark / gitweb /
92492c8665dbf77ee7e0a37c622974fad8b308c5
[elogind.git] / extras / usb_id / usb_id.c
1 /*
2  * usb_id - identify an USB device
3  *
4  * Copyright (c) 2005 SUSE Linux Products GmbH, Germany
5  *
6  * Author:
7  *      Hannes Reinecke <hare@suse.de>
8  *
9  *      This program is free software; you can redistribute it and/or modify it
10  *      under the terms of the GNU General Public License as published by the
11  *      Free Software Foundation version 2 of the License.
12  */
13
14 #include <config.h>
15
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>
24
25 #include "../../udev/udev.h"
26
27 #define MAX_PATH_LEN                    512
28 #define MAX_SERIAL_LEN                  256
29 #define BLKGETSIZE64 _IOR(0x12,114,size_t)
30
31 #ifdef USE_LOG
32 void log_message(int priority, const char *format, ...)
33 {
34         va_list args;
35         static int udev_log = -1;
36
37         if (udev_log == -1) {
38                 const char *value;
39
40                 value = getenv("UDEV_LOG");
41                 if (value)
42                         udev_log = log_priority(value);
43                 else
44                         udev_log = LOG_ERR;
45         }
46
47         if (priority > udev_log)
48                 return;
49
50         va_start(args, format);
51         vsyslog(priority, format, args);
52         va_end(args);
53 }
54 #endif
55
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];
62
63 static int use_usb_info;
64 static int use_num_info;
65
66 static void set_str(char *to, const char *from, size_t count)
67 {
68         size_t i, j, len;
69
70         /* strip trailing whitespace */
71         len = strnlen(from, count);
72         while (len && isspace(from[len-1]))
73                 len--;
74
75         /* strip leading whitespace */
76         i = 0;
77         while (isspace(from[i]) && (i < len))
78                 i++;
79
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++] = '_';
87                 }
88                 /* Replace '/' with '.' */
89                 if (from[i] == '/') {
90                         to[j++] = '.';
91                         i++;
92                         continue;
93                 }
94                 /* skip non-printable chars */
95                 if (!isalnum(from[i]) && !ispunct(from[i])) {
96                         i++;
97                         continue;
98                 }
99                 to[j++] = from[i++];
100         }
101         to[j] = '\0';
102 }
103
104 static void set_usb_iftype(char *to, int if_class_num, size_t len)
105 {
106         char *type = "generic";
107
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;
136         }
137         strncpy(to, type, len);
138         to[len-1] = '\0';
139 }
140
141 static int set_usb_mass_storage_ifsubtype(char *to, const char *from, size_t len)
142 {
143         int type_num = 0;
144         char *eptr;
145         char *type = "generic";
146
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;
166                 }
167         }
168         strlcpy(to, type, len);
169
170         return type_num;
171 }
172
173 static void set_scsi_type(char *to, const char *from, size_t len)
174 {
175         int type_num;
176         char *eptr;
177         char *type = "generic";
178
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;
199                 }
200         }
201         strlcpy(to, type, len);
202 }
203
204 /*
205  * A unique USB identification is generated like this:
206  *
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 '_'.
221  */
222 static int usb_id(const char *devpath)
223 {
224         struct sysfs_device *dev;
225         struct sysfs_device *dev_interface;
226         struct sysfs_device *dev_usb;
227         const char *if_class, *if_subclass;
228         int if_class_num;
229         int protocol = 0;
230
231         dbg("devpath %s\n", devpath);
232
233         /* get all usb specific information: dev_interface, if_class, dev_usb */
234         dev = sysfs_device_get(devpath);
235         if (dev == NULL) {
236                 err("unable to access '%s'\n", devpath);
237                 return 1;
238         }
239
240         /* usb interface directory */
241         dev_interface = sysfs_device_get_parent_with_subsystem(dev, "usb");
242         if (dev_interface == NULL) {
243                 info("unable to access usb_interface device of '%s'\n", devpath);
244                 return 1;
245         }
246
247         if_class = sysfs_attr_get_value(dev_interface->devpath, "bInterfaceClass");
248         if (!if_class) {
249                 info("%s: cannot get bInterfaceClass attribute\n", dev_interface->kernel);
250                 return 1;
251         }
252         if_class_num = strtoul(if_class, NULL, 16);
253         if (if_class_num == 8) {
254                 if_subclass = sysfs_attr_get_value(dev_interface->devpath, "bInterfaceSubClass");
255                 if (if_subclass != NULL)
256                         protocol = set_usb_mass_storage_ifsubtype(type_str, if_subclass, sizeof(type_str)-1);
257         } else
258                 set_usb_iftype(type_str, if_class_num, sizeof(type_str)-1);
259
260         info("%s: if_class %d protocol %d\n", dev_interface->devpath, if_class_num, protocol);
261
262         /* usb device directory */
263         dev_usb = sysfs_device_get_parent_with_subsystem(dev_interface, "usb");
264         if (!dev_usb) {
265                 info("unable to find parent 'usb' device of '%s'\n", devpath);
266                 return 1;
267         }
268
269         /* mass storage */
270         if (protocol == 6 && !use_usb_info) {
271                 struct sysfs_device *dev_scsi;
272                 const char *scsi_model, *scsi_vendor, *scsi_type, *scsi_rev;
273                 int host, bus, target, lun;
274
275                 /* get scsi device */
276                 dev_scsi = sysfs_device_get_parent_with_subsystem(dev, "scsi");
277                 if (dev_scsi == NULL) {
278                         info("unable to find parent 'scsi' device of '%s'\n", devpath);
279                         goto fallback;
280                 }
281                 if (sscanf(dev_scsi->kernel, "%d:%d:%d:%d", &host, &bus, &target, &lun) != 4) {
282                         info("invalid scsi device '%s'\n", dev_scsi->kernel);
283                         goto fallback;
284                 }
285
286                 /* Generic SPC-2 device */
287                 scsi_vendor = sysfs_attr_get_value(dev_scsi->devpath, "vendor");
288                 if (!scsi_vendor) {
289                         info("%s: cannot get SCSI vendor attribute\n", dev_scsi->kernel);
290                         goto fallback;
291                 }
292                 set_str(vendor_str, scsi_vendor, sizeof(vendor_str)-1);
293
294                 scsi_model = sysfs_attr_get_value(dev_scsi->devpath, "model");
295                 if (!scsi_model) {
296                         info("%s: cannot get SCSI model attribute\n", dev_scsi->kernel);
297                         goto fallback;
298                 }
299                 set_str(model_str, scsi_model, sizeof(model_str)-1);
300
301                 scsi_type = sysfs_attr_get_value(dev_scsi->devpath, "type");
302                 if (!scsi_type) {
303                         info("%s: cannot get SCSI type attribute\n", dev_scsi->kernel);
304                         goto fallback;
305                 }
306                 set_scsi_type(type_str, scsi_type, sizeof(type_str)-1);
307
308                 scsi_rev = sysfs_attr_get_value(dev_scsi->devpath, "rev");
309                 if (!scsi_rev) {
310                         info("%s: cannot get SCSI revision attribute\n", dev_scsi->kernel);
311                         goto fallback;
312                 }
313                 set_str(revision_str, scsi_rev, sizeof(revision_str)-1);
314
315                 /*
316                  * some broken devices have the same identifiers
317                  * for all luns, export the target:lun number
318                  */
319                 sprintf(instance_str, "%d:%d", target, lun);
320         }
321
322 fallback:
323         /* fallback to USB vendor & device */
324         if (vendor_str[0] == '\0') {
325                 const char *usb_vendor = NULL;
326
327                 if (!use_num_info)
328                         usb_vendor = sysfs_attr_get_value(dev_usb->devpath, "manufacturer");
329
330                 if (!usb_vendor)
331                         usb_vendor = sysfs_attr_get_value(dev_usb->devpath, "idVendor");
332
333                 if (!usb_vendor) {
334                         info("No USB vendor information available\n");
335                         return 1;
336                 }
337                 set_str(vendor_str, usb_vendor, sizeof(vendor_str)-1);
338         }
339
340         if (model_str[0] == '\0') {
341                 const char *usb_model = NULL;
342
343                 if (!use_num_info)
344                         usb_model = sysfs_attr_get_value(dev_usb->devpath, "product");
345
346                 if (!usb_model)
347                         usb_model = sysfs_attr_get_value(dev_usb->devpath, "idProduct");
348
349                 if (!usb_model) {
350                         dbg("No USB model information available\n");
351                         return 1;
352                 }
353                 set_str(model_str, usb_model, sizeof(model_str)-1);
354         }
355
356         if (revision_str[0] == '\0') {
357                 const char *usb_rev;
358
359                 usb_rev = sysfs_attr_get_value(dev_usb->devpath, "bcdDevice");
360                 if (usb_rev)
361                         set_str(revision_str, usb_rev, sizeof(revision_str)-1);
362         }
363
364         if (serial_str[0] == '\0') {
365                 const char *usb_serial;
366
367                 usb_serial = sysfs_attr_get_value(dev_usb->devpath, "serial");
368                 if (usb_serial)
369                         set_str(serial_str, usb_serial, sizeof(serial_str)-1);
370         }
371         return 0;
372 }
373
374 int main(int argc, char **argv)
375 {
376         int retval = 0;
377         const char *env;
378         char devpath[MAX_PATH_LEN];
379         static int export;
380         static const struct option options[] = {
381                 { "usb-info", 0, NULL, 'u' },
382                 { "num-info", 0, NULL, 'n' },
383                 { "export", 0, NULL, 'x' },
384                 { "help", 0, NULL, 'h' },
385                 {}
386         };
387
388         logging_init("usb_id");
389         sysfs_init();
390
391         while (1) {
392                 int option;
393
394                 option = getopt_long(argc, argv, "nuxh", options, NULL);
395                 if (option == -1)
396                         break;
397
398                 switch (option) {
399                 case 'n':
400                         use_num_info = 1;
401                         use_usb_info = 1;
402                         break;
403                 case 'u':
404                         use_usb_info = 1;
405                         break;
406                 case 'x':
407                         export = 1;
408                         break;
409                 case 'h':
410                         printf("Usage: usb_id [--usb-info] [--num-info] [--export] [--help] <devpath>\n"
411                                "  --usb-info  use usb strings instead\n"
412                                "  --num-info  use numerical values\n"
413                                "  --export    print values as environemt keys\n"
414                                "  --help      print this help text\n\n");
415                 default:
416                         retval = 1;
417                         goto exit;
418                 }
419         }
420
421         env = getenv("DEVPATH");
422         if (env != NULL)
423                 strlcpy(devpath, env, sizeof(devpath));
424         else {
425                 if (argv[optind] == NULL) {
426                         fprintf(stderr, "No device specified\n");
427                         retval = 1;
428                         goto exit;
429                 }
430                 strlcpy(devpath, argv[optind], sizeof(devpath));
431         }
432
433         retval = usb_id(devpath);
434
435         if (retval == 0) {
436                 char serial[256];
437
438                 strlcpy(serial, vendor_str, sizeof(serial));
439                 strlcat(serial, "_", sizeof(serial));
440                 strlcat(serial, model_str, sizeof(serial));
441                 if (serial_str[0] != '\0') {
442                         strlcat(serial, "_", sizeof(serial));
443                         strlcat(serial, serial_str, sizeof(serial));
444                 }
445                 if (instance_str[0] != '\0') {
446                         strlcat(serial, "-", sizeof(serial));
447                         strlcat(serial, instance_str, sizeof(serial));
448                 }
449
450                 if (export) {
451                         printf("ID_VENDOR=%s\n", vendor_str);
452                         printf("ID_MODEL=%s\n", model_str);
453                         printf("ID_REVISION=%s\n", revision_str);
454                         printf("ID_SERIAL=%s\n", serial);
455                         if (serial_str[0] != '\0')
456                                 printf("ID_SERIAL_SHORT=%s\n", serial_str);
457                         printf("ID_TYPE=%s\n", type_str);
458                         if (instance_str[0] != '\0')
459                                 printf("ID_INSTANCE=%s\n", instance_str);
460                         printf("ID_BUS=usb\n");
461                 } else
462                         printf("%s\n", serial);
463         }
464
465 exit:
466         sysfs_cleanup();
467         logging_close();
468         return retval;
469 }