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