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