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