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