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