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