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