chiark / gitweb /
0bcd267f67aff5d385c25283aa82b11eacace2b5
[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_NAME_LEN                    72
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 /*
105  * set_usb_iftype
106  *
107  * Set the type based on the USB interface class
108  */
109 static void set_usb_iftype(char *to, const char *from, size_t len)
110 {
111         int type_num;
112         char *eptr;
113         char *type = "generic";
114
115         type_num = strtoul(from, &eptr, 0);
116         if (eptr != from) {
117                 switch (type_num) {
118                 case 1:
119                         type = "audio";
120                         break;
121                 case 3:
122                         type = "hid";
123                         break;
124                 case 7:
125                         type = "printer";
126                         break;
127                 case 8:
128                         type = "disk";
129                         break;
130                 case 2: /* CDC-Control */
131                 case 5: /* Physical */
132                 case 6: /* Image */
133                 case 9: /* HUB */
134                 case 0x0a: /* CDC-Data */
135                 case 0x0b: /* Chip/Smart Card */
136                 case 0x0d: /* Content Security */
137                 case 0x0e: /* Video */
138                 case 0xdc: /* Diagnostic Device */
139                 case 0xe0: /* Wireless Controller */
140                 case 0xf2: /* Application-specific */
141                 case 0xff: /* Vendor-specific */
142                 default:
143                         break;
144                 }
145         }
146         strncpy(to, type, len);
147         to[len-1] = '\0';
148 }
149
150 /*
151  * set_usb_ifsybtype
152  *
153  * Set the type base on the interfaceSubClass.
154  * Valid for Mass-Storage devices (type 8) only.
155  */
156 static int set_usb_ifsubtype(char *to, const char *from, size_t len)
157 {
158         int type_num = 0;
159         char *eptr;
160         char *type = "generic";
161
162         type_num = strtoul(from, &eptr, 0);
163         if (eptr != from) {
164                 switch (type_num) {
165                 case 2:
166                         type = "cd";
167                         break;
168                 case 3:
169                         type = "tape";
170                         break;
171                 case 4: /* UFI */
172                 case 5: /* SFF-8070i */
173                         type = "floppy";
174                         break;
175                 case 1: /* RBC devices */
176                 case 6: /* Transparent SPC-2 devices */
177                         type = "disk";
178                         break;
179                 default:
180                         break;
181                 }
182         }
183         strncpy(to, type, len);
184         to[len-1] = '\0';
185
186         return type_num;
187 }
188
189 static void set_scsi_type(char *to, const char *from, int count)
190 {
191         int type_num;
192         char *eptr;
193
194         type_num = strtoul(from, &eptr, 0);
195         if (eptr != from) {
196                 switch (type_num) {
197                 case 0:
198                         sprintf(to, "disk");
199                         break;
200                 case 1:
201                         sprintf(to, "tape");
202                         break;
203                 case 4:
204                         sprintf(to, "optical");
205                         break;
206                 case 5:
207                         sprintf(to, "cd");
208                         break;
209                 case 7:
210                         sprintf(to, "optical");
211                         break;
212                 case 0xe:
213                         sprintf(to, "disk");
214                         break;
215                 case 0xf:
216                         sprintf(to, "optical");
217                         break;
218                 default:
219                         sprintf(to, "generic");
220                         break;
221                 }
222         } else {
223                 sprintf(to, "generic");
224         }
225 }
226
227 /*
228  * A unique USB identification is generated like this:
229  *
230  * 1.) Get the USB device type from DeviceClass, InterfaceClass
231  *     and InterfaceSubClass
232  * 2.) If the device type is 'Mass-Storage/SPC-2' or 'Mass-Storage/RBC'
233  *     use the SCSI vendor and model as USB-Vendor and USB-model.
234  * 3.) Otherwise use the USB manufacturer and product as
235  *     USB-Vendor and USB-model. Any non-printable characters
236  *     in those strings will be skipped; a slash '/' will be converted
237  *     into a full stop '.'.
238  * 4.) If that fails, too, we will use idVendor and idProduct
239  *     as USB-Vendor and USB-model.
240  * 5.) The USB identification is the USB-vendor and USB-model
241  *     string concatenated with an underscore '_'.
242  * 6.) If the device supplies a serial number, this number
243  *     is concatenated with the identification with an underscore '_'.
244  */
245 static int usb_id(const char *devpath)
246 {
247         struct sysfs_device *dev;
248         struct sysfs_device *dev_scsi;
249         struct sysfs_device *dev_target;
250         struct sysfs_device *dev_host, *dev_interface, *dev_usb;
251         const char *scsi_model, *scsi_vendor, *scsi_type, *scsi_rev;
252         const char *usb_model = NULL, *usb_vendor = NULL, *usb_rev, *usb_serial;
253         const char *if_class, *if_subclass;
254         int if_class_num;
255         int protocol = 0;
256
257         dbg("devpath %s\n", devpath);
258
259         dev = sysfs_device_get(devpath);
260         if (dev == NULL) {
261                 err("unable to access '%s'", devpath);
262                 return 1;
263         }
264
265         /* get scsi parent device */
266         dev_scsi = sysfs_device_get_parent(dev);
267         if (dev_scsi == NULL) {
268                 err("unable to access parent device of '%s'", devpath);
269                 return 1;
270         }
271         /* allow only scsi devices */
272         if (strcmp(dev_scsi->subsystem, "scsi") != 0) {
273                 info("%s is not a scsi device", devpath);
274                 return 1;
275         }
276
277         /* target directory */
278         dev_target = sysfs_device_get_parent(dev_scsi);
279         if (dev_target == NULL) {
280                 err("unable to access parent device of '%s'", devpath);
281                 return 1;
282         }
283
284         /* host directory */
285         dev_host = sysfs_device_get_parent(dev_target);
286         if (dev_host == NULL) {
287                 err("unable to access parent device of '%s'", devpath);
288                 return 1;
289         }
290
291         /* usb interface directory */
292         dev_interface = sysfs_device_get_parent(dev_host);
293         if (dev_interface == NULL) {
294                 err("unable to access parent device of '%s'", devpath);
295                 return 1;
296         }
297
298         /* usb device directory */
299         dev_usb = sysfs_device_get_parent(dev_interface);
300         if (dev_usb == NULL) {
301                 err("unable to access parent device of '%s'", devpath);
302                 return 1;
303         }
304         if (strcmp(dev_interface->subsystem, "usb") != 0) {
305                 info("%s is not an usb device", devpath);
306                 return 1;
307         }
308
309         if_class = sysfs_attr_get_value(dev_interface->devpath, "bInterfaceClass");
310         if (!if_class) {
311                 info("%s: cannot get bInterfaceClass attribute", dev_interface->kernel_name);
312                 return 1;
313         }
314         if_class_num = strtoul(if_class, NULL, 16);
315         if (if_class_num != 8) {
316                 set_usb_iftype(type_str, if_class, sizeof(type_str)-1);
317                 protocol = 0;
318         } else {
319                 if_subclass = sysfs_attr_get_value(dev_interface->devpath, "bInterfaceSubClass");
320                 protocol = set_usb_ifsubtype(type_str, if_subclass, sizeof(type_str)-1);
321         }
322
323         if (!use_usb_info && protocol == 6) {
324                 /* Generic SPC-2 device */
325                 scsi_vendor = sysfs_attr_get_value(dev_scsi->devpath, "vendor");
326                 if (!scsi_vendor) {
327                         info("%s: cannot get SCSI vendor attribute", dev_scsi->kernel_name);
328                         return 1;
329                 }
330                 set_str(vendor_str, scsi_vendor, sizeof(vendor_str)-1);
331
332                 scsi_model = sysfs_attr_get_value(dev_scsi->devpath, "model");
333                 if (!scsi_model) {
334                         info("%s: cannot get SCSI model attribute", dev_scsi->kernel_name);
335                         return 1;
336                 }
337                 set_str(model_str, scsi_model, sizeof(model_str)-1);
338
339                 scsi_type = sysfs_attr_get_value(dev_scsi->devpath, "type");
340                 if (!scsi_type) {
341                         info("%s: cannot get SCSI type attribute", dev_scsi->kernel_name);
342                         return 1;
343                 }
344                 set_scsi_type(type_str, scsi_type, sizeof(type_str)-1);
345
346                 scsi_rev = sysfs_attr_get_value(dev_scsi->devpath, "rev");
347                 if (!scsi_rev) {
348                         info("%s: cannot get SCSI revision attribute", dev_scsi->kernel_name);
349                         return 1;
350                 }
351                 set_str(revision_str, scsi_rev, sizeof(revision_str)-1);
352
353         }
354
355         /* Fallback to USB vendor & device */
356         if (vendor_str[0] == '\0') {
357                 if (!use_num_info)
358                         if (!(usb_vendor = sysfs_attr_get_value(dev_usb->devpath, "manufacturer")))
359                                 dbg("No USB vendor string found, using idVendor");
360
361                 if (!usb_vendor) {
362                         if (!(usb_vendor = sysfs_attr_get_value(dev_usb->devpath, "idVendor"))) {
363                                 dbg("No USB vendor information available\n");
364                                 sprintf(vendor_str,"0000");
365                         }
366                 }
367                 set_str(vendor_str,usb_vendor, sizeof(vendor_str) - 1);
368         }
369         
370         if (model_str[0] == '\0') {
371                 if (!use_num_info)
372                         if (!(usb_model = sysfs_attr_get_value(dev_usb->devpath, "product")))
373                                 dbg("No USB model string found, using idProduct");
374                 
375                 if (!usb_model) {
376                         if (!(usb_model = sysfs_attr_get_value(dev_usb->devpath, "idProduct")))
377                                 dbg("No USB model information available\n"); sprintf(model_str,"0000");
378                 }
379                 set_str(model_str, usb_model, sizeof(model_str) - 1);
380         }
381
382         if (revision_str[0] == '\0') {
383                 usb_rev = sysfs_attr_get_value(dev_usb->devpath, "bcdDevice");
384                 if (usb_rev)
385                         set_str(revision_str, usb_rev, sizeof(revision_str)-1);
386         }
387
388         if (serial_str[0] == '\0') {
389                 usb_serial = sysfs_attr_get_value(dev_usb->devpath, "serial");
390                 if (usb_serial)
391                         set_str(serial_str, usb_serial, sizeof(serial_str)-1);
392         }
393         return 0;
394 }
395
396 int main(int argc, char **argv)
397 {
398         int retval = 0;
399         const char *env;
400         char devpath[MAX_NAME_LEN];
401         int option;
402
403         logging_init("usb_id");
404         sysfs_init();
405
406         dbg("argc is %d", argc);
407
408         /* sysfs path can be overridden for testing */
409         env = getenv("SYSFS_PATH");
410         if (env) {
411                 strlcpy(sysfs_path, env, sizeof(sysfs_path));
412                 remove_trailing_chars(sysfs_path, '/');
413         } else
414                 strcpy(sysfs_path, "/sys");
415
416         while ((option = getopt(argc, argv, "dnux")) != -1 ) {
417                 if (optarg)
418                         dbg("option '%c' arg '%s'", option, optarg);
419                 else
420                         dbg("option '%c'", option);
421
422                 switch (option) {
423                 case 'd':
424                         debug = 1;
425                         break;
426                 case 'n':
427                         use_num_info = 1;
428                         use_usb_info = 1;
429                         break;
430                 case 'u':
431                         use_usb_info = 1;
432                         break;
433                 case 'x':
434                         export = 1;
435                         break;
436                 default:
437                         info("Unknown or bad option '%c' (0x%x)", option, option);
438                         retval = 1;
439                         break;
440                 }
441         }
442
443         env = getenv("DEVPATH");
444         if (env != NULL)
445                 strlcpy(devpath, env, sizeof(devpath));
446         else {
447                 if (optind == argc) {
448                         fprintf(stderr, "No device specified\n");
449                         retval = 1;
450                         goto exit;
451                 }
452                 strlcpy(devpath, argv[optind], sizeof(devpath));
453         }
454
455         retval = usb_id(devpath);
456
457         if (retval == 0) {
458                 if (export) {
459                         printf("ID_VENDOR=%s\n", vendor_str);
460                         printf("ID_MODEL=%s\n", model_str);
461                         printf("ID_REVISION=%s\n", revision_str);
462                         if (serial_str[0] == '\0') {
463                                 printf("ID_SERIAL=%s_%s\n", 
464                                        vendor_str, model_str);
465                         } else {
466                                 printf("ID_SERIAL=%s_%s_%s\n", 
467                                        vendor_str, model_str, serial_str);
468                         }
469                         printf("ID_TYPE=%s\n", type_str);
470                         printf("ID_BUS=usb\n");
471                 } else {
472                         if (serial_str[0] == '\0') {
473                                 printf("%s_%s\n", 
474                                        vendor_str, model_str);
475                         } else {
476                                 printf("%s_%s_%s\n", 
477                                        vendor_str, model_str, serial_str);
478                         }
479                 }
480         }
481
482 exit:
483         sysfs_cleanup();
484         logging_close();
485         return retval;
486 }