chiark / gitweb /
vol_id: fix language in manpage
[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 = 0;
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 err;
237 }
238
239 /*
240  * A unique USB identification is generated like this:
241  *
242  * 1.) Get the USB device type from InterfaceClass and InterfaceSubClass
243  * 2.) If the device type is 'Mass-Storage/SPC-2' or 'Mass-Storage/RBC'
244  *     use the SCSI vendor and model as USB-Vendor and USB-model.
245  * 3.) Otherwise use the USB manufacturer and product as
246  *     USB-Vendor and USB-model. Any non-printable characters
247  *     in those strings will be skipped; a slash '/' will be converted
248  *     into a full stop '.'.
249  * 4.) If that fails, too, we will use idVendor and idProduct
250  *     as USB-Vendor and USB-model.
251  * 5.) The USB identification is the USB-vendor and USB-model
252  *     string concatenated with an underscore '_'.
253  * 6.) If the device supplies a serial number, this number
254  *     is concatenated with the identification with an underscore '_'.
255  */
256 static int usb_id(struct udev_device *dev)
257 {
258         struct udev *udev = udev_device_get_udev(dev);
259         struct udev_device *dev_interface = NULL;
260         struct udev_device *dev_usb = NULL;
261         const char *if_class, *if_subclass;
262         int if_class_num;
263         int protocol = 0;
264
265         dbg(udev, "syspath %s\n", udev_device_get_syspath(dev));
266
267         /* shortcut, if we are called directly for a "usb_device" type */
268         if (udev_device_get_devtype(dev) != NULL && strcmp(udev_device_get_devtype(dev), "usb_device") == 0) {
269                 dev_if_packed_info(dev, packed_if_str, sizeof(packed_if_str));
270                 dev_usb = dev;
271                 goto fallback;
272         }
273
274         /* usb interface directory */
275         dev_interface = udev_device_get_parent_with_subsystem_devtype(dev, "usb", "usb_interface");
276         if (dev_interface == NULL) {
277                 info(udev, "unable to access usb_interface device of '%s'\n",
278                      udev_device_get_syspath(dev));
279                 return 1;
280         }
281
282         if_class = udev_device_get_sysattr_value(dev_interface, "bInterfaceClass");
283         if (!if_class) {
284                 info(udev, "%s: cannot get bInterfaceClass attribute\n",
285                      udev_device_get_sysname(dev));
286                 return 1;
287         }
288
289         if_class_num = strtoul(if_class, NULL, 16);
290         if (if_class_num == 8) {
291                 /* mass storage */
292                 if_subclass = udev_device_get_sysattr_value(dev_interface, "bInterfaceSubClass");
293                 if (if_subclass != NULL)
294                         protocol = set_usb_mass_storage_ifsubtype(type_str, if_subclass, sizeof(type_str)-1);
295         } else {
296                 set_usb_iftype(type_str, if_class_num, sizeof(type_str)-1);
297         }
298
299         info(udev, "%s: if_class %d protocol %d\n",
300              udev_device_get_syspath(dev_interface), if_class_num, protocol);
301
302         /* usb device directory */
303         dev_usb = udev_device_get_parent_with_subsystem_devtype(dev_interface, "usb", "usb_device");
304         if (!dev_usb) {
305                 info(udev, "unable to find parent 'usb' device of '%s'\n",
306                      udev_device_get_syspath(dev));
307                 return 1;
308         }
309
310         /* all interfaces of the device in a single string */
311         dev_if_packed_info(dev_usb, packed_if_str, sizeof(packed_if_str));
312
313         /* mass storage : SCSI or ATAPI */
314         if ((protocol == 6 || protocol == 2) && !use_usb_info) {
315                 struct udev_device *dev_scsi;
316                 const char *scsi_model, *scsi_vendor, *scsi_type, *scsi_rev;
317                 int host, bus, target, lun;
318
319                 /* get scsi device */
320                 dev_scsi = udev_device_get_parent_with_subsystem_devtype(dev, "scsi", "scsi_device");
321                 if (dev_scsi == NULL) {
322                         info(udev, "unable to find parent 'scsi' device of '%s'\n",
323                              udev_device_get_syspath(dev));
324                         goto fallback;
325                 }
326                 if (sscanf(udev_device_get_sysname(dev_scsi), "%d:%d:%d:%d", &host, &bus, &target, &lun) != 4) {
327                         info(udev, "invalid scsi device '%s'\n", udev_device_get_sysname(dev_scsi));
328                         goto fallback;
329                 }
330
331                 /* Generic SPC-2 device */
332                 scsi_vendor = udev_device_get_sysattr_value(dev_scsi, "vendor");
333                 if (!scsi_vendor) {
334                         info(udev, "%s: cannot get SCSI vendor attribute\n",
335                              udev_device_get_sysname(dev_scsi));
336                         goto fallback;
337                 }
338                 udev_util_encode_string(scsi_vendor, vendor_str_enc, sizeof(vendor_str_enc));
339                 udev_util_replace_whitespace(scsi_vendor, vendor_str, sizeof(vendor_str)-1);
340                 udev_util_replace_chars(vendor_str, NULL);
341
342                 scsi_model = udev_device_get_sysattr_value(dev_scsi, "model");
343                 if (!scsi_model) {
344                         info(udev, "%s: cannot get SCSI model attribute\n",
345                              udev_device_get_sysname(dev_scsi));
346                         goto fallback;
347                 }
348                 udev_util_encode_string(scsi_model, model_str_enc, sizeof(model_str_enc));
349                 udev_util_replace_whitespace(scsi_model, model_str, sizeof(model_str)-1);
350                 udev_util_replace_chars(model_str, NULL);
351
352                 scsi_type = udev_device_get_sysattr_value(dev_scsi, "type");
353                 if (!scsi_type) {
354                         info(udev, "%s: cannot get SCSI type attribute\n",
355                              udev_device_get_sysname(dev_scsi));
356                         goto fallback;
357                 }
358                 set_scsi_type(type_str, scsi_type, sizeof(type_str)-1);
359
360                 scsi_rev = udev_device_get_sysattr_value(dev_scsi, "rev");
361                 if (!scsi_rev) {
362                         info(udev, "%s: cannot get SCSI revision attribute\n",
363                              udev_device_get_sysname(dev_scsi));
364                         goto fallback;
365                 }
366                 udev_util_replace_whitespace(scsi_rev, revision_str, sizeof(revision_str)-1);
367                 udev_util_replace_chars(revision_str, NULL);
368
369                 /*
370                  * some broken devices have the same identifiers
371                  * for all luns, export the target:lun number
372                  */
373                 sprintf(instance_str, "%d:%d", target, lun);
374         }
375
376 fallback:
377         /* fallback to USB vendor & device */
378         if (vendor_str[0] == '\0') {
379                 const char *usb_vendor = NULL;
380
381                 if (!use_num_info)
382                         usb_vendor = udev_device_get_sysattr_value(dev_usb, "manufacturer");
383
384                 if (!usb_vendor)
385                         usb_vendor = udev_device_get_sysattr_value(dev_usb, "idVendor");
386
387                 if (!usb_vendor) {
388                         info(udev, "No USB vendor information available\n");
389                         return 1;
390                 }
391                 udev_util_encode_string(usb_vendor, vendor_str_enc, sizeof(vendor_str_enc));
392                 udev_util_replace_whitespace(usb_vendor, vendor_str, sizeof(vendor_str)-1);
393                 udev_util_replace_chars(vendor_str, NULL);
394         }
395
396         if (model_str[0] == '\0') {
397                 const char *usb_model = NULL;
398
399                 if (!use_num_info)
400                         usb_model = udev_device_get_sysattr_value(dev_usb, "product");
401
402                 if (!usb_model)
403                         usb_model = udev_device_get_sysattr_value(dev_usb, "idProduct");
404
405                 if (!usb_model) {
406                         dbg(udev, "No USB model information available\n");
407                         return 1;
408                 }
409                 udev_util_encode_string(usb_model, model_str_enc, sizeof(model_str_enc));
410                 udev_util_replace_whitespace(usb_model, model_str, sizeof(model_str)-1);
411                 udev_util_replace_chars(model_str, NULL);
412         }
413
414         if (revision_str[0] == '\0') {
415                 const char *usb_rev;
416
417                 usb_rev = udev_device_get_sysattr_value(dev_usb, "bcdDevice");
418                 if (usb_rev) {
419                         udev_util_replace_whitespace(usb_rev, revision_str, sizeof(revision_str)-1);
420                         udev_util_replace_chars(revision_str, NULL);
421                 }
422         }
423
424         if (serial_str[0] == '\0') {
425                 const char *usb_serial;
426
427                 usb_serial = udev_device_get_sysattr_value(dev_usb, "serial");
428                 if (usb_serial) {
429                         udev_util_replace_whitespace(usb_serial, serial_str, sizeof(serial_str)-1);
430                         udev_util_replace_chars(serial_str, NULL);
431                 }
432         }
433         return 0;
434 }
435
436 int main(int argc, char **argv)
437 {
438         static const struct option options[] = {
439                 { "usb-info", no_argument, NULL, 'u' },
440                 { "num-info", no_argument, NULL, 'n' },
441                 { "export", no_argument, NULL, 'x' },
442                 { "debug", no_argument, NULL, 'd' },
443                 { "help", no_argument, NULL, 'h' },
444                 {}
445         };
446         struct udev *udev;
447         struct udev_device *dev = NULL;
448         char syspath[UTIL_PATH_SIZE];
449         const char *devpath;
450         static int export;
451         int retval = 0;
452
453         udev = udev_new();
454         if (udev == NULL)
455                 goto exit;
456
457         logging_init("usb_id");
458         udev_set_log_fn(udev, log_fn);
459
460         while (1) {
461                 int option;
462
463                 option = getopt_long(argc, argv, "dnuxh", options, NULL);
464                 if (option == -1)
465                         break;
466
467                 switch (option) {
468                 case 'd':
469                         debug = 1;
470                         if (udev_get_log_priority(udev) < LOG_INFO)
471                                 udev_set_log_priority(udev, LOG_INFO);
472                         break;
473                 case 'n':
474                         use_num_info = 1;
475                         use_usb_info = 1;
476                         break;
477                 case 'u':
478                         use_usb_info = 1;
479                         break;
480                 case 'x':
481                         export = 1;
482                         break;
483                 case 'h':
484                         printf("Usage: usb_id [--usb-info] [--num-info] [--export] [--help] <devpath>\n"
485                                "  --usb-info  use usb strings instead\n"
486                                "  --num-info  use numerical values\n"
487                                "  --export    print values as environment keys\n"
488                                "  --help      print this help text\n\n");
489                 default:
490                         retval = 1;
491                         goto exit;
492                 }
493         }
494
495         devpath = argv[optind];
496         if (devpath == NULL) {
497                 fprintf(stderr, "No device specified\n");
498                 retval = 1;
499                 goto exit;
500         }
501
502         util_strlcpy(syspath, udev_get_sys_path(udev), sizeof(syspath));
503         util_strlcat(syspath, devpath, sizeof(syspath));
504         dev = udev_device_new_from_syspath(udev, syspath);
505         if (dev == NULL) {
506                 err(udev, "unable to access '%s'\n", devpath);
507                 return 1;
508         }
509
510         retval = usb_id(dev);
511         if (retval == 0) {
512                 char serial[256];
513
514                 util_strlcpy(serial, vendor_str, sizeof(serial));
515                 util_strlcat(serial, "_", sizeof(serial));
516                 util_strlcat(serial, model_str, sizeof(serial));
517                 if (serial_str[0] != '\0') {
518                         util_strlcat(serial, "_", sizeof(serial));
519                         util_strlcat(serial, serial_str, sizeof(serial));
520                 }
521                 if (instance_str[0] != '\0') {
522                         util_strlcat(serial, "-", sizeof(serial));
523                         util_strlcat(serial, instance_str, sizeof(serial));
524                 }
525
526                 if (export) {
527                         printf("ID_VENDOR=%s\n", vendor_str);
528                         printf("ID_VENDOR_ENC=%s\n", vendor_str_enc);
529                         printf("ID_MODEL=%s\n", model_str);
530                         printf("ID_MODEL_ENC=%s\n", model_str_enc);
531                         printf("ID_REVISION=%s\n", revision_str);
532                         printf("ID_SERIAL=%s\n", serial);
533                         if (serial_str[0] != '\0')
534                                 printf("ID_SERIAL_SHORT=%s\n", serial_str);
535                         if (type_str[0] != '\0')
536                                 printf("ID_TYPE=%s\n", type_str);
537                         if (instance_str[0] != '\0')
538                                 printf("ID_INSTANCE=%s\n", instance_str);
539                         printf("ID_BUS=usb\n");
540                         if (packed_if_str[0] != '\0')
541                                 printf("ID_USB_INTERFACES=:%s\n", packed_if_str);
542                 } else
543                         printf("%s\n", serial);
544         }
545
546 exit:
547         udev_device_unref(dev);
548         udev_unref(udev);
549         logging_close();
550         return retval;
551 }