Line data Source code
1 : /* SPDX-License-Identifier: GPL-2.0+ */
2 : /*
3 : * USB device properties and persistent device path
4 : *
5 : * Copyright (c) 2005 SUSE Linux Products GmbH, Germany
6 : * Author: Hannes Reinecke <hare@suse.de>
7 : */
8 :
9 : #include <ctype.h>
10 : #include <errno.h>
11 : #include <fcntl.h>
12 : #include <stdarg.h>
13 : #include <stdio.h>
14 : #include <stdlib.h>
15 : #include <string.h>
16 : #include <unistd.h>
17 :
18 : #include "alloc-util.h"
19 : #include "device-util.h"
20 : #include "fd-util.h"
21 : #include "libudev-util.h"
22 : #include "string-util.h"
23 : #include "strxcpyx.h"
24 : #include "udev-builtin.h"
25 :
26 0 : static void set_usb_iftype(char *to, int if_class_num, size_t len) {
27 0 : const char *type = "generic";
28 :
29 0 : switch (if_class_num) {
30 0 : case 1:
31 0 : type = "audio";
32 0 : break;
33 0 : case 2: /* CDC-Control */
34 0 : break;
35 0 : case 3:
36 0 : type = "hid";
37 0 : break;
38 0 : case 5: /* Physical */
39 0 : break;
40 0 : case 6:
41 0 : type = "media";
42 0 : break;
43 0 : case 7:
44 0 : type = "printer";
45 0 : break;
46 0 : case 8:
47 0 : type = "storage";
48 0 : break;
49 0 : case 9:
50 0 : type = "hub";
51 0 : break;
52 0 : case 0x0a: /* CDC-Data */
53 0 : break;
54 0 : case 0x0b: /* Chip/Smart Card */
55 0 : break;
56 0 : case 0x0d: /* Content Security */
57 0 : break;
58 0 : case 0x0e:
59 0 : type = "video";
60 0 : break;
61 0 : case 0xdc: /* Diagnostic Device */
62 0 : break;
63 0 : case 0xe0: /* Wireless Controller */
64 0 : break;
65 0 : case 0xfe: /* Application-specific */
66 0 : break;
67 0 : case 0xff: /* Vendor-specific */
68 0 : break;
69 0 : default:
70 0 : break;
71 : }
72 0 : strncpy(to, type, len);
73 0 : to[len-1] = '\0';
74 0 : }
75 :
76 0 : static int set_usb_mass_storage_ifsubtype(char *to, const char *from, size_t len) {
77 0 : int type_num = 0;
78 : char *eptr;
79 0 : const char *type = "generic";
80 :
81 0 : type_num = strtoul(from, &eptr, 0);
82 0 : if (eptr != from) {
83 0 : switch (type_num) {
84 0 : case 1: /* RBC devices */
85 0 : type = "rbc";
86 0 : break;
87 0 : case 2:
88 0 : type = "atapi";
89 0 : break;
90 0 : case 3:
91 0 : type = "tape";
92 0 : break;
93 0 : case 4: /* UFI */
94 0 : type = "floppy";
95 0 : break;
96 0 : case 6: /* Transparent SPC-2 devices */
97 0 : type = "scsi";
98 0 : break;
99 0 : default:
100 0 : break;
101 : }
102 0 : }
103 0 : strscpy(to, len, type);
104 0 : return type_num;
105 : }
106 :
107 0 : static void set_scsi_type(char *to, const char *from, size_t len) {
108 : int type_num;
109 : char *eptr;
110 0 : const char *type = "generic";
111 :
112 0 : type_num = strtoul(from, &eptr, 0);
113 0 : if (eptr != from) {
114 0 : switch (type_num) {
115 0 : case 0:
116 : case 0xe:
117 0 : type = "disk";
118 0 : break;
119 0 : case 1:
120 0 : type = "tape";
121 0 : break;
122 0 : case 4:
123 : case 7:
124 : case 0xf:
125 0 : type = "optical";
126 0 : break;
127 0 : case 5:
128 0 : type = "cd";
129 0 : break;
130 0 : default:
131 0 : break;
132 : }
133 0 : }
134 0 : strscpy(to, len, type);
135 0 : }
136 :
137 : #define USB_DT_DEVICE 0x01
138 : #define USB_DT_INTERFACE 0x04
139 :
140 0 : static int dev_if_packed_info(sd_device *dev, char *ifs_str, size_t len) {
141 0 : _cleanup_close_ int fd = -1;
142 : ssize_t size;
143 : unsigned char buf[18 + 65535];
144 0 : size_t pos = 0;
145 0 : unsigned strpos = 0;
146 : const char *filename, *syspath;
147 : int r;
148 : struct usb_interface_descriptor {
149 : uint8_t bLength;
150 : uint8_t bDescriptorType;
151 : uint8_t bInterfaceNumber;
152 : uint8_t bAlternateSetting;
153 : uint8_t bNumEndpoints;
154 : uint8_t bInterfaceClass;
155 : uint8_t bInterfaceSubClass;
156 : uint8_t bInterfaceProtocol;
157 : uint8_t iInterface;
158 : } _packed_;
159 :
160 0 : r = sd_device_get_syspath(dev, &syspath);
161 0 : if (r < 0)
162 0 : return r;
163 :
164 0 : filename = strjoina(syspath, "/descriptors");
165 0 : fd = open(filename, O_RDONLY|O_CLOEXEC);
166 0 : if (fd < 0)
167 0 : return log_device_debug_errno(dev, errno, "Failed to open \"%s\": %m", filename);
168 :
169 0 : size = read(fd, buf, sizeof(buf));
170 0 : if (size < 18)
171 0 : return log_device_warning_errno(dev, SYNTHETIC_ERRNO(EIO),
172 : "Short read from \"%s\"", filename);
173 0 : assert((size_t) size <= sizeof buf);
174 :
175 0 : ifs_str[0] = '\0';
176 0 : while (pos + sizeof(struct usb_interface_descriptor) < (size_t) size &&
177 0 : strpos + 7 < len - 2) {
178 :
179 : struct usb_interface_descriptor *desc;
180 : char if_str[8];
181 :
182 0 : desc = (struct usb_interface_descriptor *) (buf + pos);
183 0 : if (desc->bLength < 3)
184 0 : break;
185 0 : if (desc->bLength > size - sizeof(struct usb_interface_descriptor))
186 0 : return log_device_debug_errno(dev, SYNTHETIC_ERRNO(EIO),
187 : "Corrupt data read from \"%s\"", filename);
188 0 : pos += desc->bLength;
189 :
190 0 : if (desc->bDescriptorType != USB_DT_INTERFACE)
191 0 : continue;
192 :
193 0 : if (snprintf(if_str, 8, ":%02x%02x%02x",
194 0 : desc->bInterfaceClass,
195 0 : desc->bInterfaceSubClass,
196 0 : desc->bInterfaceProtocol) != 7)
197 0 : continue;
198 :
199 0 : if (strstr(ifs_str, if_str))
200 0 : continue;
201 :
202 0 : memcpy(&ifs_str[strpos], if_str, 8),
203 0 : strpos += 7;
204 : }
205 :
206 0 : if (strpos > 0) {
207 0 : ifs_str[strpos++] = ':';
208 0 : ifs_str[strpos++] = '\0';
209 : }
210 :
211 0 : 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 0 : static int builtin_usb_id(sd_device *dev, int argc, char *argv[], bool test) {
232 0 : char vendor_str[64] = "";
233 : char vendor_str_enc[256];
234 : const char *vendor_id;
235 0 : char model_str[64] = "";
236 : char model_str_enc[256];
237 : const char *product_id;
238 0 : char serial_str[UTIL_NAME_SIZE] = "";
239 0 : char packed_if_str[UTIL_NAME_SIZE] = "";
240 0 : char revision_str[64] = "";
241 0 : char type_str[64] = "";
242 0 : char instance_str[64] = "";
243 0 : const char *ifnum = NULL;
244 0 : const char *driver = NULL;
245 : char serial[256];
246 :
247 : sd_device *dev_interface, *dev_usb;
248 : const char *if_class, *if_subclass;
249 : int if_class_num;
250 0 : int protocol = 0;
251 : size_t l;
252 : char *s;
253 :
254 : const char *syspath, *sysname, *devtype, *interface_syspath;
255 : int r;
256 :
257 0 : assert(dev);
258 :
259 0 : r = sd_device_get_syspath(dev, &syspath);
260 0 : if (r < 0)
261 0 : return r;
262 :
263 0 : r = sd_device_get_sysname(dev, &sysname);
264 0 : if (r < 0)
265 0 : return r;
266 :
267 : /* shortcut, if we are called directly for a "usb_device" type */
268 0 : if (sd_device_get_devtype(dev, &devtype) >= 0 && streq(devtype, "usb_device")) {
269 0 : dev_if_packed_info(dev, packed_if_str, sizeof(packed_if_str));
270 0 : dev_usb = dev;
271 0 : goto fallback;
272 : }
273 :
274 : /* usb interface directory */
275 0 : r = sd_device_get_parent_with_subsystem_devtype(dev, "usb", "usb_interface", &dev_interface);
276 0 : if (r < 0)
277 0 : return log_device_debug_errno(dev, r, "Failed to access usb_interface: %m");
278 :
279 0 : r = sd_device_get_syspath(dev_interface, &interface_syspath);
280 0 : if (r < 0)
281 0 : return log_device_debug_errno(dev_interface, r, "Failed to get syspath: %m");
282 0 : (void) sd_device_get_sysattr_value(dev_interface, "bInterfaceNumber", &ifnum);
283 0 : (void) sd_device_get_sysattr_value(dev_interface, "driver", &driver);
284 :
285 0 : r = sd_device_get_sysattr_value(dev_interface, "bInterfaceClass", &if_class);
286 0 : if (r < 0)
287 0 : return log_device_debug_errno(dev_interface, r, "Failed to get bInterfaceClass attribute: %m");
288 :
289 0 : if_class_num = strtoul(if_class, NULL, 16);
290 0 : if (if_class_num == 8) {
291 : /* mass storage */
292 0 : if (sd_device_get_sysattr_value(dev_interface, "bInterfaceSubClass", &if_subclass) >= 0)
293 0 : protocol = set_usb_mass_storage_ifsubtype(type_str, if_subclass, sizeof(type_str)-1);
294 : } else
295 0 : set_usb_iftype(type_str, if_class_num, sizeof(type_str)-1);
296 :
297 0 : log_device_debug(dev_interface, "if_class:%d protocol:%d", if_class_num, protocol);
298 :
299 : /* usb device directory */
300 0 : r = sd_device_get_parent_with_subsystem_devtype(dev_interface, "usb", "usb_device", &dev_usb);
301 0 : if (r < 0)
302 0 : return log_device_debug_errno(dev_interface, r, "Failed to find parent 'usb' device");
303 :
304 : /* all interfaces of the device in a single string */
305 0 : dev_if_packed_info(dev_usb, packed_if_str, sizeof(packed_if_str));
306 :
307 : /* mass storage : SCSI or ATAPI */
308 0 : if (IN_SET(protocol, 6, 2)) {
309 : sd_device *dev_scsi;
310 : const char *scsi_sysname, *scsi_model, *scsi_vendor, *scsi_type, *scsi_rev;
311 : int host, bus, target, lun;
312 :
313 : /* get scsi device */
314 0 : r = sd_device_get_parent_with_subsystem_devtype(dev, "scsi", "scsi_device", &dev_scsi);
315 0 : if (r < 0) {
316 0 : log_device_debug_errno(dev, r, "Unable to find parent SCSI device");
317 0 : goto fallback;
318 : }
319 0 : if (sd_device_get_sysname(dev_scsi, &scsi_sysname) < 0)
320 0 : goto fallback;
321 0 : if (sscanf(scsi_sysname, "%d:%d:%d:%d", &host, &bus, &target, &lun) != 4) {
322 0 : log_device_debug(dev_scsi, "Invalid SCSI device");
323 0 : goto fallback;
324 : }
325 :
326 : /* Generic SPC-2 device */
327 0 : r = sd_device_get_sysattr_value(dev_scsi, "vendor", &scsi_vendor);
328 0 : if (r < 0) {
329 0 : log_device_debug_errno(dev_scsi, r, "Failed to get SCSI vendor attribute: %m");
330 0 : goto fallback;
331 : }
332 0 : udev_util_encode_string(scsi_vendor, vendor_str_enc, sizeof(vendor_str_enc));
333 0 : util_replace_whitespace(scsi_vendor, vendor_str, sizeof(vendor_str)-1);
334 0 : util_replace_chars(vendor_str, NULL);
335 :
336 0 : r = sd_device_get_sysattr_value(dev_scsi, "model", &scsi_model);
337 0 : if (r < 0) {
338 0 : log_device_debug_errno(dev_scsi, r, "Failed to get SCSI model attribute: %m");
339 0 : goto fallback;
340 : }
341 0 : udev_util_encode_string(scsi_model, model_str_enc, sizeof(model_str_enc));
342 0 : util_replace_whitespace(scsi_model, model_str, sizeof(model_str)-1);
343 0 : util_replace_chars(model_str, NULL);
344 :
345 0 : r = sd_device_get_sysattr_value(dev_scsi, "type", &scsi_type);
346 0 : if (r < 0) {
347 0 : log_device_debug_errno(dev_scsi, r, "Failed to get SCSI type attribute: %m");
348 0 : goto fallback;
349 : }
350 0 : set_scsi_type(type_str, scsi_type, sizeof(type_str)-1);
351 :
352 0 : r = sd_device_get_sysattr_value(dev_scsi, "rev", &scsi_rev);
353 0 : if (r < 0) {
354 0 : log_device_debug_errno(dev_scsi, r, "Failed to get SCSI revision attribute: %m");
355 0 : goto fallback;
356 : }
357 0 : util_replace_whitespace(scsi_rev, revision_str, sizeof(revision_str)-1);
358 0 : util_replace_chars(revision_str, NULL);
359 :
360 : /*
361 : * some broken devices have the same identifiers
362 : * for all luns, export the target:lun number
363 : */
364 0 : sprintf(instance_str, "%d:%d", target, lun);
365 : }
366 :
367 0 : fallback:
368 0 : r = sd_device_get_sysattr_value(dev_usb, "idVendor", &vendor_id);
369 0 : if (r < 0)
370 0 : return log_device_debug_errno(dev_usb, r, "Failed to get idVendor attribute: %m");
371 :
372 0 : r = sd_device_get_sysattr_value(dev_usb, "idProduct", &product_id);
373 0 : if (r < 0)
374 0 : return log_device_debug_errno(dev_usb, r, "Failed to get idProduct attribute: %m");
375 :
376 : /* fallback to USB vendor & device */
377 0 : if (vendor_str[0] == '\0') {
378 : const char *usb_vendor;
379 :
380 0 : if (sd_device_get_sysattr_value(dev_usb, "manufacturer", &usb_vendor) < 0)
381 0 : usb_vendor = vendor_id;
382 0 : udev_util_encode_string(usb_vendor, vendor_str_enc, sizeof(vendor_str_enc));
383 0 : util_replace_whitespace(usb_vendor, vendor_str, sizeof(vendor_str)-1);
384 0 : util_replace_chars(vendor_str, NULL);
385 : }
386 :
387 0 : if (model_str[0] == '\0') {
388 : const char *usb_model;
389 :
390 0 : if (sd_device_get_sysattr_value(dev_usb, "product", &usb_model) < 0)
391 0 : usb_model = product_id;
392 0 : udev_util_encode_string(usb_model, model_str_enc, sizeof(model_str_enc));
393 0 : util_replace_whitespace(usb_model, model_str, sizeof(model_str)-1);
394 0 : util_replace_chars(model_str, NULL);
395 : }
396 :
397 0 : if (revision_str[0] == '\0') {
398 : const char *usb_rev;
399 :
400 0 : if (sd_device_get_sysattr_value(dev_usb, "bcdDevice", &usb_rev) >= 0) {
401 0 : util_replace_whitespace(usb_rev, revision_str, sizeof(revision_str)-1);
402 0 : util_replace_chars(revision_str, NULL);
403 : }
404 : }
405 :
406 0 : if (serial_str[0] == '\0') {
407 : const char *usb_serial;
408 :
409 0 : if (sd_device_get_sysattr_value(dev_usb, "serial", &usb_serial) >= 0) {
410 : const unsigned char *p;
411 :
412 : /* http://msdn.microsoft.com/en-us/library/windows/hardware/gg487321.aspx */
413 0 : for (p = (unsigned char *) usb_serial; *p != '\0'; p++)
414 0 : if (*p < 0x20 || *p > 0x7f || *p == ',') {
415 0 : usb_serial = NULL;
416 0 : break;
417 : }
418 :
419 0 : if (usb_serial) {
420 0 : util_replace_whitespace(usb_serial, serial_str, sizeof(serial_str)-1);
421 0 : util_replace_chars(serial_str, NULL);
422 : }
423 : }
424 : }
425 :
426 0 : s = serial;
427 0 : l = strpcpyl(&s, sizeof(serial), vendor_str, "_", model_str, NULL);
428 0 : if (!isempty(serial_str))
429 0 : l = strpcpyl(&s, l, "_", serial_str, NULL);
430 :
431 0 : if (!isempty(instance_str))
432 0 : strpcpyl(&s, l, "-", instance_str, NULL);
433 :
434 0 : udev_builtin_add_property(dev, test, "ID_VENDOR", vendor_str);
435 0 : udev_builtin_add_property(dev, test, "ID_VENDOR_ENC", vendor_str_enc);
436 0 : udev_builtin_add_property(dev, test, "ID_VENDOR_ID", vendor_id);
437 0 : udev_builtin_add_property(dev, test, "ID_MODEL", model_str);
438 0 : udev_builtin_add_property(dev, test, "ID_MODEL_ENC", model_str_enc);
439 0 : udev_builtin_add_property(dev, test, "ID_MODEL_ID", product_id);
440 0 : udev_builtin_add_property(dev, test, "ID_REVISION", revision_str);
441 0 : udev_builtin_add_property(dev, test, "ID_SERIAL", serial);
442 0 : if (!isempty(serial_str))
443 0 : udev_builtin_add_property(dev, test, "ID_SERIAL_SHORT", serial_str);
444 0 : if (!isempty(type_str))
445 0 : udev_builtin_add_property(dev, test, "ID_TYPE", type_str);
446 0 : if (!isempty(instance_str))
447 0 : udev_builtin_add_property(dev, test, "ID_INSTANCE", instance_str);
448 0 : udev_builtin_add_property(dev, test, "ID_BUS", "usb");
449 0 : if (!isempty(packed_if_str))
450 0 : udev_builtin_add_property(dev, test, "ID_USB_INTERFACES", packed_if_str);
451 0 : if (ifnum)
452 0 : udev_builtin_add_property(dev, test, "ID_USB_INTERFACE_NUM", ifnum);
453 0 : if (driver)
454 0 : udev_builtin_add_property(dev, test, "ID_USB_DRIVER", driver);
455 0 : return 0;
456 : }
457 :
458 : const UdevBuiltin udev_builtin_usb_id = {
459 : .name = "usb_id",
460 : .cmd = builtin_usb_id,
461 : .help = "USB device properties",
462 : .run_once = true,
463 : };
|