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