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