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