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