chiark / gitweb /
ab0d96e3772f80db91f839bf04e647e80da9dbde
[elogind.git] / src / udev / udev-builtin-usb_id.c
1 /*
2  * USB device properties and persistent device path
3  *
4  * Copyright (c) 2005 SUSE Linux Products GmbH, Germany
5  *   Author: Hannes Reinecke <hare@suse.de>
6  *
7  * Copyright (C) 2005-2011 Kay Sievers <kay@vrfy.org>
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  */
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <stdarg.h>
26 #include <unistd.h>
27 #include <string.h>
28 #include <ctype.h>
29 #include <fcntl.h>
30 #include <errno.h>
31
32 #include "udev.h"
33
34 static void set_usb_iftype(char *to, int if_class_num, size_t len) {
35         const char *type = "generic";
36
37         switch (if_class_num) {
38         case 1:
39                 type = "audio";
40                 break;
41         case 2: /* CDC-Control */
42                 break;
43         case 3:
44                 type = "hid";
45                 break;
46         case 5: /* Physical */
47                 break;
48         case 6:
49                 type = "media";
50                 break;
51         case 7:
52                 type = "printer";
53                 break;
54         case 8:
55                 type = "storage";
56                 break;
57         case 9:
58                 type = "hub";
59                 break;
60         case 0x0a: /* CDC-Data */
61                 break;
62         case 0x0b: /* Chip/Smart Card */
63                 break;
64         case 0x0d: /* Content Security */
65                 break;
66         case 0x0e:
67                 type = "video";
68                 break;
69         case 0xdc: /* Diagnostic Device */
70                 break;
71         case 0xe0: /* Wireless Controller */
72                 break;
73         case 0xfe: /* Application-specific */
74                 break;
75         case 0xff: /* Vendor-specific */
76                 break;
77         default:
78                 break;
79         }
80         strncpy(to, type, len);
81         to[len-1] = '\0';
82 }
83
84 static int set_usb_mass_storage_ifsubtype(char *to, const char *from, size_t len) {
85         int type_num = 0;
86         char *eptr;
87         const char *type = "generic";
88
89         type_num = strtoul(from, &eptr, 0);
90         if (eptr != from) {
91                 switch (type_num) {
92                 case 1: /* RBC devices */
93                         type = "rbc";
94                         break;
95                 case 2:
96                         type = "atapi";
97                         break;
98                 case 3:
99                         type = "tape";
100                         break;
101                 case 4: /* UFI */
102                         type = "floppy";
103                         break;
104                 case 6: /* Transparent SPC-2 devices */
105                         type = "scsi";
106                         break;
107                 default:
108                         break;
109                 }
110         }
111         strscpy(to, len, type);
112         return type_num;
113 }
114
115 static void set_scsi_type(char *to, const char *from, size_t len) {
116         int type_num;
117         char *eptr;
118         const char *type = "generic";
119
120         type_num = strtoul(from, &eptr, 0);
121         if (eptr != from) {
122                 switch (type_num) {
123                 case 0:
124                 case 0xe:
125                         type = "disk";
126                         break;
127                 case 1:
128                         type = "tape";
129                         break;
130                 case 4:
131                 case 7:
132                 case 0xf:
133                         type = "optical";
134                         break;
135                 case 5:
136                         type = "cd";
137                         break;
138                 default:
139                         break;
140                 }
141         }
142         strscpy(to, len, type);
143 }
144
145 #define USB_DT_DEVICE                        0x01
146 #define USB_DT_INTERFACE                0x04
147
148 static int dev_if_packed_info(struct udev_device *dev, char *ifs_str, size_t len) {
149         _cleanup_free_ char *filename = NULL;
150         _cleanup_close_ int fd = -1;
151         ssize_t size;
152         unsigned char buf[18 + 65535];
153         int pos = 0;
154         unsigned strpos = 0;
155         struct usb_interface_descriptor {
156                 u_int8_t        bLength;
157                 u_int8_t        bDescriptorType;
158                 u_int8_t        bInterfaceNumber;
159                 u_int8_t        bAlternateSetting;
160                 u_int8_t        bNumEndpoints;
161                 u_int8_t        bInterfaceClass;
162                 u_int8_t        bInterfaceSubClass;
163                 u_int8_t        bInterfaceProtocol;
164                 u_int8_t        iInterface;
165         } __attribute__((packed));
166
167         if (asprintf(&filename, "%s/descriptors", udev_device_get_syspath(dev)) < 0)
168                 return log_oom();
169
170         fd = open(filename, O_RDONLY|O_CLOEXEC);
171         if (fd < 0) {
172                 fprintf(stderr, "error opening USB device 'descriptors' file\n");
173                 return -errno;
174         }
175
176         size = read(fd, buf, sizeof(buf));
177         if (size < 18 || size == sizeof(buf))
178                 return -EIO;
179
180         ifs_str[0] = '\0';
181         while (pos < size && strpos+7 < len-2) {
182                 struct usb_interface_descriptor *desc;
183                 char if_str[8];
184
185                 desc = (struct usb_interface_descriptor *) &buf[pos];
186                 if (desc->bLength < 3)
187                         break;
188                 pos += desc->bLength;
189
190                 if (desc->bDescriptorType != USB_DT_INTERFACE)
191                         continue;
192
193                 if (snprintf(if_str, 8, ":%02x%02x%02x",
194                              desc->bInterfaceClass,
195                              desc->bInterfaceSubClass,
196                              desc->bInterfaceProtocol) != 7)
197                         continue;
198
199                 if (strstr(ifs_str, if_str) != NULL)
200                         continue;
201
202                 memcpy(&ifs_str[strpos], if_str, 8),
203                 strpos += 7;
204         }
205
206         if (strpos > 0) {
207                 ifs_str[strpos++] = ':';
208                 ifs_str[strpos++] = '\0';
209         }
210
211         return 0;
212 }
213
214 /*
215  * A unique USB identification is generated like this:
216  *
217  * 1.) Get the USB device type from InterfaceClass and InterfaceSubClass
218  * 2.) If the device type is 'Mass-Storage/SPC-2' or 'Mass-Storage/RBC',
219  *     use the SCSI vendor and model as USB-Vendor and USB-model.
220  * 3.) Otherwise, use the USB manufacturer and product as
221  *     USB-Vendor and USB-model. Any non-printable characters
222  *     in those strings will be skipped; a slash '/' will be converted
223  *     into a full stop '.'.
224  * 4.) If that fails, too, we will use idVendor and idProduct
225  *     as USB-Vendor and USB-model.
226  * 5.) The USB identification is the USB-vendor and USB-model
227  *     string concatenated with an underscore '_'.
228  * 6.) If the device supplies a serial number, this number
229  *     is concatenated with the identification with an underscore '_'.
230  */
231 static int builtin_usb_id(struct udev_device *dev, int argc, char *argv[], bool test) {
232         char vendor_str[64];
233         char vendor_str_enc[256];
234         const char *vendor_id;
235         char model_str[64];
236         char model_str_enc[256];
237         const char *product_id;
238         char serial_str[UTIL_NAME_SIZE];
239         char packed_if_str[UTIL_NAME_SIZE];
240         char revision_str[64];
241         char type_str[64];
242         char instance_str[64];
243         const char *ifnum = NULL;
244         const char *driver = NULL;
245         char serial[256];
246
247         struct udev_device *dev_interface = NULL;
248         struct udev_device *dev_usb = NULL;
249         const char *if_class, *if_subclass;
250         int if_class_num;
251         int protocol = 0;
252         size_t l;
253         char *s;
254
255         vendor_str[0] = '\0';
256         model_str[0] = '\0';
257         serial_str[0] = '\0';
258         packed_if_str[0] = '\0';
259         revision_str[0] = '\0';
260         type_str[0] = '\0';
261         instance_str[0] = '\0';
262
263         /* shortcut, if we are called directly for a "usb_device" type */
264         if (udev_device_get_devtype(dev) != NULL && streq(udev_device_get_devtype(dev), "usb_device")) {
265                 dev_if_packed_info(dev, packed_if_str, sizeof(packed_if_str));
266                 dev_usb = dev;
267                 goto fallback;
268         }
269
270         /* usb interface directory */
271         dev_interface = udev_device_get_parent_with_subsystem_devtype(dev, "usb", "usb_interface");
272         if (dev_interface == NULL) {
273                 log_debug("unable to access usb_interface device of '%s'",
274                      udev_device_get_syspath(dev));
275                 return EXIT_FAILURE;
276         }
277
278         ifnum = udev_device_get_sysattr_value(dev_interface, "bInterfaceNumber");
279         driver = udev_device_get_sysattr_value(dev_interface, "driver");
280
281         if_class = udev_device_get_sysattr_value(dev_interface, "bInterfaceClass");
282         if (!if_class) {
283                 log_debug("%s: cannot get bInterfaceClass attribute",
284                      udev_device_get_sysname(dev));
285                 return EXIT_FAILURE;
286         }
287
288         if_class_num = strtoul(if_class, NULL, 16);
289         if (if_class_num == 8) {
290                 /* mass storage */
291                 if_subclass = udev_device_get_sysattr_value(dev_interface, "bInterfaceSubClass");
292                 if (if_subclass != NULL)
293                         protocol = set_usb_mass_storage_ifsubtype(type_str, if_subclass, sizeof(type_str)-1);
294         } else {
295                 set_usb_iftype(type_str, if_class_num, sizeof(type_str)-1);
296         }
297
298         log_debug("%s: if_class %d protocol %d",
299              udev_device_get_syspath(dev_interface), if_class_num, protocol);
300
301         /* usb device directory */
302         dev_usb = udev_device_get_parent_with_subsystem_devtype(dev_interface, "usb", "usb_device");
303         if (!dev_usb) {
304                 log_debug("unable to find parent 'usb' device of '%s'",
305                      udev_device_get_syspath(dev));
306                 return EXIT_FAILURE;
307         }
308
309         /* all interfaces of the device in a single string */
310         dev_if_packed_info(dev_usb, packed_if_str, sizeof(packed_if_str));
311
312         /* mass storage : SCSI or ATAPI */
313         if ((protocol == 6 || protocol == 2)) {
314                 struct udev_device *dev_scsi;
315                 const char *scsi_model, *scsi_vendor, *scsi_type, *scsi_rev;
316                 int host, bus, target, lun;
317
318                 /* get scsi device */
319                 dev_scsi = udev_device_get_parent_with_subsystem_devtype(dev, "scsi", "scsi_device");
320                 if (dev_scsi == NULL) {
321                         log_debug("unable to find parent 'scsi' device of '%s'",
322                              udev_device_get_syspath(dev));
323                         goto fallback;
324                 }
325                 if (sscanf(udev_device_get_sysname(dev_scsi), "%d:%d:%d:%d", &host, &bus, &target, &lun) != 4) {
326                         log_debug("invalid scsi device '%s'", udev_device_get_sysname(dev_scsi));
327                         goto fallback;
328                 }
329
330                 /* Generic SPC-2 device */
331                 scsi_vendor = udev_device_get_sysattr_value(dev_scsi, "vendor");
332                 if (!scsi_vendor) {
333                         log_debug("%s: cannot get SCSI vendor attribute",
334                              udev_device_get_sysname(dev_scsi));
335                         goto fallback;
336                 }
337                 udev_util_encode_string(scsi_vendor, vendor_str_enc, sizeof(vendor_str_enc));
338                 util_replace_whitespace(scsi_vendor, vendor_str, sizeof(vendor_str)-1);
339                 util_replace_chars(vendor_str, NULL);
340
341                 scsi_model = udev_device_get_sysattr_value(dev_scsi, "model");
342                 if (!scsi_model) {
343                         log_debug("%s: cannot get SCSI model attribute",
344                              udev_device_get_sysname(dev_scsi));
345                         goto fallback;
346                 }
347                 udev_util_encode_string(scsi_model, model_str_enc, sizeof(model_str_enc));
348                 util_replace_whitespace(scsi_model, model_str, sizeof(model_str)-1);
349                 util_replace_chars(model_str, NULL);
350
351                 scsi_type = udev_device_get_sysattr_value(dev_scsi, "type");
352                 if (!scsi_type) {
353                         log_debug("%s: cannot get SCSI type attribute",
354                              udev_device_get_sysname(dev_scsi));
355                         goto fallback;
356                 }
357                 set_scsi_type(type_str, scsi_type, sizeof(type_str)-1);
358
359                 scsi_rev = udev_device_get_sysattr_value(dev_scsi, "rev");
360                 if (!scsi_rev) {
361                         log_debug("%s: cannot get SCSI revision attribute",
362                              udev_device_get_sysname(dev_scsi));
363                         goto fallback;
364                 }
365                 util_replace_whitespace(scsi_rev, revision_str, sizeof(revision_str)-1);
366                 util_replace_chars(revision_str, NULL);
367
368                 /*
369                  * some broken devices have the same identifiers
370                  * for all luns, export the target:lun number
371                  */
372                 sprintf(instance_str, "%d:%d", target, lun);
373         }
374
375 fallback:
376         vendor_id = udev_device_get_sysattr_value(dev_usb, "idVendor");
377         product_id = udev_device_get_sysattr_value(dev_usb, "idProduct");
378
379         /* fallback to USB vendor & device */
380         if (vendor_str[0] == '\0') {
381                 const char *usb_vendor = NULL;
382
383                 usb_vendor = udev_device_get_sysattr_value(dev_usb, "manufacturer");
384                 if (!usb_vendor)
385                         usb_vendor = vendor_id;
386                 if (!usb_vendor) {
387                         log_debug("No USB vendor information available");
388                         return EXIT_FAILURE;
389                 }
390                 udev_util_encode_string(usb_vendor, vendor_str_enc, sizeof(vendor_str_enc));
391                 util_replace_whitespace(usb_vendor, vendor_str, sizeof(vendor_str)-1);
392                 util_replace_chars(vendor_str, NULL);
393         }
394
395         if (model_str[0] == '\0') {
396                 const char *usb_model = NULL;
397
398                 usb_model = udev_device_get_sysattr_value(dev_usb, "product");
399                 if (!usb_model)
400                         usb_model = product_id;
401                 if (!usb_model)
402                         return EXIT_FAILURE;
403                 udev_util_encode_string(usb_model, model_str_enc, sizeof(model_str_enc));
404                 util_replace_whitespace(usb_model, model_str, sizeof(model_str)-1);
405                 util_replace_chars(model_str, NULL);
406         }
407
408         if (revision_str[0] == '\0') {
409                 const char *usb_rev;
410
411                 usb_rev = udev_device_get_sysattr_value(dev_usb, "bcdDevice");
412                 if (usb_rev) {
413                         util_replace_whitespace(usb_rev, revision_str, sizeof(revision_str)-1);
414                         util_replace_chars(revision_str, NULL);
415                 }
416         }
417
418         if (serial_str[0] == '\0') {
419                 const char *usb_serial;
420
421                 usb_serial = udev_device_get_sysattr_value(dev_usb, "serial");
422                 if (usb_serial) {
423                         const unsigned char *p;
424
425                         /* http://msdn.microsoft.com/en-us/library/windows/hardware/gg487321.aspx */
426                         for (p = (unsigned char *)usb_serial; *p != '\0'; p++)
427                                 if (*p < 0x20 || *p > 0x7f || *p == ',') {
428                                         usb_serial = NULL;
429                                         break;
430                                 }
431                 }
432
433                 if (usb_serial) {
434                         util_replace_whitespace(usb_serial, serial_str, sizeof(serial_str)-1);
435                         util_replace_chars(serial_str, NULL);
436                 }
437         }
438
439         s = serial;
440         l = strpcpyl(&s, sizeof(serial), vendor_str, "_", model_str, NULL);
441         if (serial_str[0] != '\0')
442                 l = strpcpyl(&s, l, "_", serial_str, NULL);
443
444         if (instance_str[0] != '\0')
445                 strpcpyl(&s, l, "-", instance_str, NULL);
446
447         udev_builtin_add_property(dev, test, "ID_VENDOR", vendor_str);
448         udev_builtin_add_property(dev, test, "ID_VENDOR_ENC", vendor_str_enc);
449         udev_builtin_add_property(dev, test, "ID_VENDOR_ID", vendor_id);
450         udev_builtin_add_property(dev, test, "ID_MODEL", model_str);
451         udev_builtin_add_property(dev, test, "ID_MODEL_ENC", model_str_enc);
452         udev_builtin_add_property(dev, test, "ID_MODEL_ID", product_id);
453         udev_builtin_add_property(dev, test, "ID_REVISION", revision_str);
454         udev_builtin_add_property(dev, test, "ID_SERIAL", serial);
455         if (serial_str[0] != '\0')
456                 udev_builtin_add_property(dev, test, "ID_SERIAL_SHORT", serial_str);
457         if (type_str[0] != '\0')
458                 udev_builtin_add_property(dev, test, "ID_TYPE", type_str);
459         if (instance_str[0] != '\0')
460                 udev_builtin_add_property(dev, test, "ID_INSTANCE", instance_str);
461         udev_builtin_add_property(dev, test, "ID_BUS", "usb");
462         if (packed_if_str[0] != '\0')
463                 udev_builtin_add_property(dev, test, "ID_USB_INTERFACES", packed_if_str);
464         if (ifnum != NULL)
465                 udev_builtin_add_property(dev, test, "ID_USB_INTERFACE_NUM", ifnum);
466         if (driver != NULL)
467                 udev_builtin_add_property(dev, test, "ID_USB_DRIVER", driver);
468         return EXIT_SUCCESS;
469 }
470
471 const struct udev_builtin udev_builtin_usb_id = {
472         .name = "usb_id",
473         .cmd = builtin_usb_id,
474         .help = "USB device properties",
475         .run_once = true,
476 };