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