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