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