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