chiark / gitweb /
usb_id: add ID_USB_INTERFACES=:0e0100:0e0200:010100:010200:
[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 <stdio.h>
15 #include <stdlib.h>
16 #include <stdarg.h>
17 #include <unistd.h>
18 #include <string.h>
19 #include <ctype.h>
20 #include <fcntl.h>
21 #include <errno.h>
22 #include <getopt.h>
23
24 #include "../../udev/udev.h"
25
26 int debug;
27
28 static void log_fn(struct udev *udev, int priority,
29                    const char *file, int line, const char *fn,
30                    const char *format, va_list args)
31 {
32         if (debug) {
33                 fprintf(stderr, "%s: ", fn != NULL ? fn : file);
34                 vfprintf(stderr, format, args);
35         } else {
36                 vsyslog(priority, format, args);
37         }
38 }
39
40 static char vendor_str[64];
41 static char model_str[64];
42 static char model_str_enc[256];
43 static char vendor_str_enc[256];
44 static char serial_str[UTIL_NAME_SIZE];
45 static char packed_if_str[UTIL_NAME_SIZE];
46 static char revision_str[64];
47 static char type_str[64];
48 static char instance_str[64];
49
50 static int use_usb_info;
51 static int use_num_info;
52
53 static void set_usb_iftype(char *to, int if_class_num, size_t len)
54 {
55         char *type = "generic";
56
57         switch (if_class_num) {
58         case 1:
59                 type = "audio";
60                 break;
61         case 2: /* CDC-Control */
62                 break;
63         case 3:
64                 type = "hid";
65                 break;
66         case 5: /* Physical */
67                 break;
68         case 6:
69                 type = "media";
70                 break;
71         case 7:
72                 type = "printer";
73                 break;
74         case 8:
75                 type = "storage";
76                 break;
77         case 9:
78                 type = "hub";
79                 break;
80         case 0x0a: /* CDC-Data */
81                 break;
82         case 0x0b: /* Chip/Smart Card */
83                 break;
84         case 0x0d: /* Content Security */
85                 break;
86         case 0x0e:
87                 type = "video";
88                 break;
89         case 0xdc: /* Diagnostic Device */
90                 break;
91         case 0xe0: /* Wireless Controller */
92                 break;
93         case 0xfe: /* Application-specific */
94                 break;
95         case 0xff: /* Vendor-specific */
96                 break;
97         default:
98                 break;
99         }
100         strncpy(to, type, len);
101         to[len-1] = '\0';
102 }
103
104 static int set_usb_mass_storage_ifsubtype(char *to, const char *from, size_t len)
105 {
106         int type_num = 0;
107         char *eptr;
108         char *type = "generic";
109
110         type_num = strtoul(from, &eptr, 0);
111         if (eptr != from) {
112                 switch (type_num) {
113                 case 2:
114                         type = "atapi";
115                         break;
116                 case 3:
117                         type = "tape";
118                         break;
119                 case 4: /* UFI */
120                 case 5: /* SFF-8070i */
121                         type = "floppy";
122                         break;
123                 case 1: /* RBC devices */
124                         type = "rbc";
125                         break;
126                 case 6: /* Transparent SPC-2 devices */
127                         type = "scsi";
128                         break;
129                 default:
130                         break;
131                 }
132         }
133         util_strlcpy(to, type, len);
134         return type_num;
135 }
136
137 static void set_scsi_type(char *to, const char *from, size_t len)
138 {
139         int type_num;
140         char *eptr;
141         char *type = "generic";
142
143         type_num = strtoul(from, &eptr, 0);
144         if (eptr != from) {
145                 switch (type_num) {
146                 case 0:
147                 case 0xe:
148                         type = "disk";
149                         break;
150                 case 1:
151                         type = "tape";
152                         break;
153                 case 4:
154                 case 7:
155                 case 0xf:
156                         type = "optical";
157                         break;
158                 case 5:
159                         type = "cd";
160                         break;
161                 default:
162                         break;
163                 }
164         }
165         util_strlcpy(to, type, len);
166 }
167
168 #define USB_DT_DEVICE                   0x01
169 #define USB_DT_INTERFACE                0x04
170
171 static int dev_if_packed_info(struct udev_device *dev, char *ifs_str, size_t len)
172 {
173         char *filename = NULL;
174         int fd;
175         ssize_t size;
176         unsigned char buf[18 + 65535];
177         unsigned int pos, strpos;
178         struct usb_interface_descriptor {
179                 u_int8_t        bLength;
180                 u_int8_t        bDescriptorType;
181                 u_int8_t        bInterfaceNumber;
182                 u_int8_t        bAlternateSetting;
183                 u_int8_t        bNumEndpoints;
184                 u_int8_t        bInterfaceClass;
185                 u_int8_t        bInterfaceSubClass;
186                 u_int8_t        bInterfaceProtocol;
187                 u_int8_t        iInterface;
188         } __attribute__((packed));
189         int err;
190
191         if (asprintf(&filename, "%s/descriptors", udev_device_get_syspath(dev)) < 0) {
192                 err = -1;
193                 goto out;
194         }
195         fd = open(filename, O_RDONLY);
196         if (fd < 0) {
197                 fprintf(stderr, "error opening USB device 'descriptors' file\n");
198                 err = -1;
199                 goto out;
200         }
201         size = read(fd, buf, sizeof(buf));
202         close(fd);
203         if (size < 18 || size == sizeof(buf)) {
204                 err = -1;
205                 goto out;
206         }
207
208         pos = 0;
209         strpos = 0;
210         while (pos < sizeof(buf) && strpos+7 < len) {
211                 struct usb_interface_descriptor *desc;
212                 char if_str[8];
213
214                 desc = (struct usb_interface_descriptor *) &buf[pos];
215                 if (desc->bLength < 3)
216                         break;
217                 pos += desc->bLength;
218
219                 if (desc->bDescriptorType != USB_DT_INTERFACE)
220                         continue;
221
222                 if (snprintf(if_str, 8, "%02x%02x%02x:",
223                              desc->bInterfaceClass,
224                              desc->bInterfaceSubClass,
225                              desc->bInterfaceProtocol) != 7)
226                         continue;
227
228                 if (strstr(ifs_str, if_str) != NULL)
229                         continue;
230
231                 memcpy(&ifs_str[strpos], if_str, 8),
232                 strpos += 7;
233         }
234 out:
235         free(filename);
236         return 0;
237 }
238
239 /*
240  * A unique USB identification is generated like this:
241  *
242  * 1.) Get the USB device type from DeviceClass, InterfaceClass
243  *     and InterfaceSubClass
244  * 2.) If the device type is 'Mass-Storage/SPC-2' or 'Mass-Storage/RBC'
245  *     use the SCSI vendor and model as USB-Vendor and USB-model.
246  * 3.) Otherwise use the USB manufacturer and product as
247  *     USB-Vendor and USB-model. Any non-printable characters
248  *     in those strings will be skipped; a slash '/' will be converted
249  *     into a full stop '.'.
250  * 4.) If that fails, too, we will use idVendor and idProduct
251  *     as USB-Vendor and USB-model.
252  * 5.) The USB identification is the USB-vendor and USB-model
253  *     string concatenated with an underscore '_'.
254  * 6.) If the device supplies a serial number, this number
255  *     is concatenated with the identification with an underscore '_'.
256  */
257 static int usb_id(struct udev_device *dev)
258 {
259         struct udev *udev = udev_device_get_udev(dev);
260         struct udev_device *dev_interface;
261         struct udev_device *dev_usb;
262         const char *if_class, *if_subclass;
263         int if_class_num;
264         int protocol = 0;
265
266         dbg(udev, "syspath %s\n", udev_device_get_syspath(dev));
267
268         /* usb interface directory */
269         dev_interface = udev_device_get_parent_with_subsystem_devtype(dev, "usb", "usb_interface");
270         if (dev_interface == NULL) {
271                 info(udev, "unable to access usb_interface device of '%s'\n",
272                      udev_device_get_syspath(dev));
273                 return 1;
274         }
275
276         if_class = udev_device_get_sysattr_value(dev_interface, "bInterfaceClass");
277         if (!if_class) {
278                 info(udev, "%s: cannot get bInterfaceClass attribute\n",
279                      udev_device_get_sysname(dev));
280                 return 1;
281         }
282
283         if_class_num = strtoul(if_class, NULL, 16);
284         if (if_class_num == 8) {
285                 /* mass storage */
286                 if_subclass = udev_device_get_sysattr_value(dev_interface, "bInterfaceSubClass");
287                 if (if_subclass != NULL)
288                         protocol = set_usb_mass_storage_ifsubtype(type_str, if_subclass, sizeof(type_str)-1);
289         } else {
290                 set_usb_iftype(type_str, if_class_num, sizeof(type_str)-1);
291         }
292
293         info(udev, "%s: if_class %d protocol %d\n",
294              udev_device_get_syspath(dev_interface), if_class_num, protocol);
295
296         /* usb device directory */
297         dev_usb = udev_device_get_parent_with_subsystem_devtype(dev_interface, "usb", "usb_device");
298         if (!dev_usb) {
299                 info(udev, "unable to find parent 'usb' device of '%s'\n",
300                      udev_device_get_syspath(dev));
301                 return 1;
302         }
303
304         /* all interfaces of the device in a single string */
305         dev_if_packed_info(dev_usb, packed_if_str, sizeof(packed_if_str));
306
307         /* mass storage : SCSI or ATAPI */
308         if ((protocol == 6 || protocol == 2) && !use_usb_info) {
309                 struct udev_device *dev_scsi;
310                 const char *scsi_model, *scsi_vendor, *scsi_type, *scsi_rev;
311                 int host, bus, target, lun;
312
313                 /* get scsi device */
314                 dev_scsi = udev_device_get_parent_with_subsystem_devtype(dev, "scsi", "scsi_device");
315                 if (dev_scsi == NULL) {
316                         info(udev, "unable to find parent 'scsi' device of '%s'\n",
317                              udev_device_get_syspath(dev));
318                         goto fallback;
319                 }
320                 if (sscanf(udev_device_get_sysname(dev_scsi), "%d:%d:%d:%d", &host, &bus, &target, &lun) != 4) {
321                         info(udev, "invalid scsi device '%s'\n", udev_device_get_sysname(dev_scsi));
322                         goto fallback;
323                 }
324
325                 /* Generic SPC-2 device */
326                 scsi_vendor = udev_device_get_sysattr_value(dev_scsi, "vendor");
327                 if (!scsi_vendor) {
328                         info(udev, "%s: cannot get SCSI vendor attribute\n",
329                              udev_device_get_sysname(dev_scsi));
330                         goto fallback;
331                 }
332                 udev_util_encode_string(scsi_vendor, vendor_str_enc, sizeof(vendor_str_enc));
333                 udev_util_replace_whitespace(scsi_vendor, vendor_str, sizeof(vendor_str)-1);
334                 udev_util_replace_chars(vendor_str, NULL);
335
336                 scsi_model = udev_device_get_sysattr_value(dev_scsi, "model");
337                 if (!scsi_model) {
338                         info(udev, "%s: cannot get SCSI model attribute\n",
339                              udev_device_get_sysname(dev_scsi));
340                         goto fallback;
341                 }
342                 udev_util_encode_string(scsi_model, model_str_enc, sizeof(model_str_enc));
343                 udev_util_replace_whitespace(scsi_model, model_str, sizeof(model_str)-1);
344                 udev_util_replace_chars(model_str, NULL);
345
346                 scsi_type = udev_device_get_sysattr_value(dev_scsi, "type");
347                 if (!scsi_type) {
348                         info(udev, "%s: cannot get SCSI type attribute\n",
349                              udev_device_get_sysname(dev_scsi));
350                         goto fallback;
351                 }
352                 set_scsi_type(type_str, scsi_type, sizeof(type_str)-1);
353
354                 scsi_rev = udev_device_get_sysattr_value(dev_scsi, "rev");
355                 if (!scsi_rev) {
356                         info(udev, "%s: cannot get SCSI revision attribute\n",
357                              udev_device_get_sysname(dev_scsi));
358                         goto fallback;
359                 }
360                 udev_util_replace_whitespace(scsi_rev, revision_str, sizeof(revision_str)-1);
361                 udev_util_replace_chars(revision_str, NULL);
362
363                 /*
364                  * some broken devices have the same identifiers
365                  * for all luns, export the target:lun number
366                  */
367                 sprintf(instance_str, "%d:%d", target, lun);
368         }
369
370 fallback:
371         /* fallback to USB vendor & device */
372         if (vendor_str[0] == '\0') {
373                 const char *usb_vendor = NULL;
374
375                 if (!use_num_info)
376                         usb_vendor = udev_device_get_sysattr_value(dev_usb, "manufacturer");
377
378                 if (!usb_vendor)
379                         usb_vendor = udev_device_get_sysattr_value(dev_usb, "idVendor");
380
381                 if (!usb_vendor) {
382                         info(udev, "No USB vendor information available\n");
383                         return 1;
384                 }
385                 udev_util_encode_string(usb_vendor, vendor_str_enc, sizeof(vendor_str_enc));
386                 udev_util_replace_whitespace(usb_vendor, vendor_str, sizeof(vendor_str)-1);
387                 udev_util_replace_chars(vendor_str, NULL);
388         }
389
390         if (model_str[0] == '\0') {
391                 const char *usb_model = NULL;
392
393                 if (!use_num_info)
394                         usb_model = udev_device_get_sysattr_value(dev_usb, "product");
395
396                 if (!usb_model)
397                         usb_model = udev_device_get_sysattr_value(dev_usb, "idProduct");
398
399                 if (!usb_model) {
400                         dbg(udev, "No USB model information available\n");
401                         return 1;
402                 }
403                 udev_util_encode_string(usb_model, model_str_enc, sizeof(model_str_enc));
404                 udev_util_replace_whitespace(usb_model, model_str, sizeof(model_str)-1);
405                 udev_util_replace_chars(model_str, NULL);
406         }
407
408         if (revision_str[0] == '\0') {
409                 const char *usb_rev;
410
411                 usb_rev = udev_device_get_sysattr_value(dev_usb, "bcdDevice");
412                 if (usb_rev) {
413                         udev_util_replace_whitespace(usb_rev, revision_str, sizeof(revision_str)-1);
414                         udev_util_replace_chars(revision_str, NULL);
415                 }
416         }
417
418         if (serial_str[0] == '\0') {
419                 const char *usb_serial;
420
421                 usb_serial = udev_device_get_sysattr_value(dev_usb, "serial");
422                 if (usb_serial) {
423                         udev_util_replace_whitespace(usb_serial, serial_str, sizeof(serial_str)-1);
424                         udev_util_replace_chars(serial_str, NULL);
425                 }
426         }
427         return 0;
428 }
429
430 int main(int argc, char **argv)
431 {
432         static const struct option options[] = {
433                 { "usb-info", no_argument, NULL, 'u' },
434                 { "num-info", no_argument, NULL, 'n' },
435                 { "export", no_argument, NULL, 'x' },
436                 { "debug", no_argument, NULL, 'd' },
437                 { "help", no_argument, NULL, 'h' },
438                 {}
439         };
440         struct udev *udev;
441         struct udev_device *dev = NULL;
442         char syspath[UTIL_PATH_SIZE];
443         const char *devpath;
444         static int export;
445         int retval = 0;
446
447         udev = udev_new();
448         if (udev == NULL)
449                 goto exit;
450
451         logging_init("usb_id");
452         udev_set_log_fn(udev, log_fn);
453
454         while (1) {
455                 int option;
456
457                 option = getopt_long(argc, argv, "dnuxh", options, NULL);
458                 if (option == -1)
459                         break;
460
461                 switch (option) {
462                 case 'd':
463                         debug = 1;
464                         if (udev_get_log_priority(udev) < LOG_INFO)
465                                 udev_set_log_priority(udev, LOG_INFO);
466                         break;
467                 case 'n':
468                         use_num_info = 1;
469                         use_usb_info = 1;
470                         break;
471                 case 'u':
472                         use_usb_info = 1;
473                         break;
474                 case 'x':
475                         export = 1;
476                         break;
477                 case 'h':
478                         printf("Usage: usb_id [--usb-info] [--num-info] [--export] [--help] <devpath>\n"
479                                "  --usb-info  use usb strings instead\n"
480                                "  --num-info  use numerical values\n"
481                                "  --export    print values as environment keys\n"
482                                "  --help      print this help text\n\n");
483                 default:
484                         retval = 1;
485                         goto exit;
486                 }
487         }
488
489         devpath = argv[optind];
490         if (devpath == NULL) {
491                 fprintf(stderr, "No device specified\n");
492                 retval = 1;
493                 goto exit;
494         }
495
496         util_strlcpy(syspath, udev_get_sys_path(udev), sizeof(syspath));
497         util_strlcat(syspath, devpath, sizeof(syspath));
498         dev = udev_device_new_from_syspath(udev, syspath);
499         if (dev == NULL) {
500                 err(udev, "unable to access '%s'\n", devpath);
501                 return 1;
502         }
503
504         retval = usb_id(dev);
505         if (retval == 0) {
506                 char serial[256];
507
508                 util_strlcpy(serial, vendor_str, sizeof(serial));
509                 util_strlcat(serial, "_", sizeof(serial));
510                 util_strlcat(serial, model_str, sizeof(serial));
511                 if (serial_str[0] != '\0') {
512                         util_strlcat(serial, "_", sizeof(serial));
513                         util_strlcat(serial, serial_str, sizeof(serial));
514                 }
515                 if (instance_str[0] != '\0') {
516                         util_strlcat(serial, "-", sizeof(serial));
517                         util_strlcat(serial, instance_str, sizeof(serial));
518                 }
519
520                 if (export) {
521                         printf("ID_VENDOR=%s\n", vendor_str);
522                         printf("ID_VENDOR_ENC=%s\n", vendor_str_enc);
523                         printf("ID_MODEL=%s\n", model_str);
524                         printf("ID_MODEL_ENC=%s\n", model_str_enc);
525                         printf("ID_REVISION=%s\n", revision_str);
526                         printf("ID_SERIAL=%s\n", serial);
527                         if (serial_str[0] != '\0')
528                                 printf("ID_SERIAL_SHORT=%s\n", serial_str);
529                         printf("ID_TYPE=%s\n", type_str);
530                         if (instance_str[0] != '\0')
531                                 printf("ID_INSTANCE=%s\n", instance_str);
532                         printf("ID_BUS=usb\n");
533                         if (packed_if_str[0] != '\0')
534                                 printf("ID_USB_INTERFACES=:%s\n", packed_if_str);
535                 } else
536                         printf("%s\n", serial);
537         }
538
539 exit:
540         udev_device_unref(dev);
541         udev_unref(udev);
542         logging_close();
543         return retval;
544 }