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