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