Debian Codegraph @

src/udev/udev-builtin-usb_id.c

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