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