chiark / gitweb /
scsi_id, usb_id: request device parent by subsystem
[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 /*
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;
251         struct sysfs_device *dev_interface;
252         struct sysfs_device *dev_usb;
253         const char *scsi_model, *scsi_vendor, *scsi_type, *scsi_rev;
254         const char *usb_model = NULL, *usb_vendor = NULL, *usb_rev, *usb_serial;
255         const char *if_class, *if_subclass;
256         int if_class_num;
257         int protocol = 0;
258
259         dbg("devpath %s\n", devpath);
260
261         dev = sysfs_device_get(devpath);
262         if (dev == NULL) {
263                 err("unable to access '%s'", devpath);
264                 return 1;
265         }
266
267         /* get scsi parent device */
268         dev_scsi = sysfs_device_get_parent_with_subsystem(dev, "scsi");
269         if (dev_scsi == NULL) {
270                 err("unable to find parent 'scsi' device of '%s'", devpath);
271                 return 1;
272         }
273
274         /* target directory */
275         dev_target = sysfs_device_get_parent(dev_scsi);
276         if (dev_target == NULL) {
277                 err("unable to access parent device of '%s'", devpath);
278                 return 1;
279         }
280
281         /* host directory */
282         dev_host = sysfs_device_get_parent(dev_target);
283         if (dev_host == NULL) {
284                 err("unable to access parent device of '%s'", devpath);
285                 return 1;
286         }
287
288         /* usb interface directory */
289         dev_interface = sysfs_device_get_parent_with_subsystem(dev_host, "usb");
290         if (dev_interface == NULL) {
291                 err("unable to access parent device of '%s'", devpath);
292                 return 1;
293         }
294
295         /* usb device directory */
296         dev_usb = sysfs_device_get_parent_with_subsystem(dev_interface, "usb");
297         if (dev_usb == NULL) {
298                 err("unable to find parent 'usb' device of '%s'", devpath);
299                 return 1;
300         }
301
302         if_class = sysfs_attr_get_value(dev_interface->devpath, "bInterfaceClass");
303         if (!if_class) {
304                 info("%s: cannot get bInterfaceClass attribute", dev_interface->kernel_name);
305                 return 1;
306         }
307         if_class_num = strtoul(if_class, NULL, 16);
308         if (if_class_num != 8) {
309                 set_usb_iftype(type_str, if_class, sizeof(type_str)-1);
310                 protocol = 0;
311         } else {
312                 if_subclass = sysfs_attr_get_value(dev_interface->devpath, "bInterfaceSubClass");
313                 protocol = set_usb_ifsubtype(type_str, if_subclass, sizeof(type_str)-1);
314         }
315
316         if (!use_usb_info && protocol == 6) {
317                 /* Generic SPC-2 device */
318                 scsi_vendor = sysfs_attr_get_value(dev_scsi->devpath, "vendor");
319                 if (!scsi_vendor) {
320                         info("%s: cannot get SCSI vendor attribute", dev_scsi->kernel_name);
321                         return 1;
322                 }
323                 set_str(vendor_str, scsi_vendor, sizeof(vendor_str)-1);
324
325                 scsi_model = sysfs_attr_get_value(dev_scsi->devpath, "model");
326                 if (!scsi_model) {
327                         info("%s: cannot get SCSI model attribute", dev_scsi->kernel_name);
328                         return 1;
329                 }
330                 set_str(model_str, scsi_model, sizeof(model_str)-1);
331
332                 scsi_type = sysfs_attr_get_value(dev_scsi->devpath, "type");
333                 if (!scsi_type) {
334                         info("%s: cannot get SCSI type attribute", dev_scsi->kernel_name);
335                         return 1;
336                 }
337                 set_scsi_type(type_str, scsi_type, sizeof(type_str)-1);
338
339                 scsi_rev = sysfs_attr_get_value(dev_scsi->devpath, "rev");
340                 if (!scsi_rev) {
341                         info("%s: cannot get SCSI revision attribute", dev_scsi->kernel_name);
342                         return 1;
343                 }
344                 set_str(revision_str, scsi_rev, sizeof(revision_str)-1);
345
346         }
347
348         /* Fallback to USB vendor & device */
349         if (vendor_str[0] == '\0') {
350                 if (!use_num_info)
351                         if (!(usb_vendor = sysfs_attr_get_value(dev_usb->devpath, "manufacturer")))
352                                 dbg("No USB vendor string found, using idVendor");
353
354                 if (!usb_vendor) {
355                         if (!(usb_vendor = sysfs_attr_get_value(dev_usb->devpath, "idVendor"))) {
356                                 dbg("No USB vendor information available\n");
357                                 sprintf(vendor_str,"0000");
358                         }
359                 }
360                 set_str(vendor_str,usb_vendor, sizeof(vendor_str) - 1);
361         }
362         
363         if (model_str[0] == '\0') {
364                 if (!use_num_info)
365                         if (!(usb_model = sysfs_attr_get_value(dev_usb->devpath, "product")))
366                                 dbg("No USB model string found, using idProduct");
367                 
368                 if (!usb_model) {
369                         if (!(usb_model = sysfs_attr_get_value(dev_usb->devpath, "idProduct")))
370                                 dbg("No USB model information available\n"); sprintf(model_str,"0000");
371                 }
372                 set_str(model_str, usb_model, sizeof(model_str) - 1);
373         }
374
375         if (revision_str[0] == '\0') {
376                 usb_rev = sysfs_attr_get_value(dev_usb->devpath, "bcdDevice");
377                 if (usb_rev)
378                         set_str(revision_str, usb_rev, sizeof(revision_str)-1);
379         }
380
381         if (serial_str[0] == '\0') {
382                 usb_serial = sysfs_attr_get_value(dev_usb->devpath, "serial");
383                 if (usb_serial)
384                         set_str(serial_str, usb_serial, sizeof(serial_str)-1);
385         }
386         return 0;
387 }
388
389 int main(int argc, char **argv)
390 {
391         int retval = 0;
392         const char *env;
393         char devpath[MAX_PATH_LEN];
394         int option;
395
396         logging_init("usb_id");
397         sysfs_init();
398
399         dbg("argc is %d", argc);
400
401         /* sysfs path can be overridden for testing */
402         env = getenv("SYSFS_PATH");
403         if (env) {
404                 strlcpy(sysfs_path, env, sizeof(sysfs_path));
405                 remove_trailing_chars(sysfs_path, '/');
406         } else
407                 strcpy(sysfs_path, "/sys");
408
409         while ((option = getopt(argc, argv, "dnux")) != -1 ) {
410                 if (optarg)
411                         dbg("option '%c' arg '%s'", option, optarg);
412                 else
413                         dbg("option '%c'", option);
414
415                 switch (option) {
416                 case 'd':
417                         debug = 1;
418                         break;
419                 case 'n':
420                         use_num_info = 1;
421                         use_usb_info = 1;
422                         break;
423                 case 'u':
424                         use_usb_info = 1;
425                         break;
426                 case 'x':
427                         export = 1;
428                         break;
429                 default:
430                         info("Unknown or bad option '%c' (0x%x)", option, option);
431                         retval = 1;
432                         break;
433                 }
434         }
435
436         env = getenv("DEVPATH");
437         if (env != NULL)
438                 strlcpy(devpath, env, sizeof(devpath));
439         else {
440                 if (optind == argc) {
441                         fprintf(stderr, "No device specified\n");
442                         retval = 1;
443                         goto exit;
444                 }
445                 strlcpy(devpath, argv[optind], sizeof(devpath));
446         }
447
448         retval = usb_id(devpath);
449
450         if (retval == 0) {
451                 if (export) {
452                         printf("ID_VENDOR=%s\n", vendor_str);
453                         printf("ID_MODEL=%s\n", model_str);
454                         printf("ID_REVISION=%s\n", revision_str);
455                         if (serial_str[0] == '\0') {
456                                 printf("ID_SERIAL=%s_%s\n", 
457                                        vendor_str, model_str);
458                         } else {
459                                 printf("ID_SERIAL=%s_%s_%s\n", 
460                                        vendor_str, model_str, serial_str);
461                         }
462                         printf("ID_TYPE=%s\n", type_str);
463                         printf("ID_BUS=usb\n");
464                 } else {
465                         if (serial_str[0] == '\0') {
466                                 printf("%s_%s\n", 
467                                        vendor_str, model_str);
468                         } else {
469                                 printf("%s_%s_%s\n", 
470                                        vendor_str, model_str, serial_str);
471                         }
472                 }
473         }
474
475 exit:
476         sysfs_cleanup();
477         logging_close();
478         return retval;
479 }