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