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