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