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