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