chiark / gitweb /
fix NAME= and OPTION+="string_escape=..." logic
[elogind.git] / extras / usb_id / usb_id.c
index 59cdd40b3101017070fdf751d0374a5683ccad48..3d007f9ee5a66f62e7cd277c3fc9b2e137b51ec6 100644 (file)
@@ -46,44 +46,6 @@ static char instance_str[64];
 static int use_usb_info;
 static int use_num_info;
 
-static void set_str(char *to, const char *from, size_t count)
-{
-       size_t i, j, len;
-
-       /* strip trailing whitespace */
-       len = strnlen(from, count);
-       while (len && isspace(from[len-1]))
-               len--;
-
-       /* strip leading whitespace */
-       i = 0;
-       while (isspace(from[i]) && (i < len))
-               i++;
-
-       j = 0;
-       while (i < len) {
-               /* substitute multiple whitespace */
-               if (isspace(from[i])) {
-                       while (isspace(from[i]))
-                               i++;
-                       to[j++] = '_';
-               }
-               /* Replace '/' with '.' */
-               if (from[i] == '/') {
-                       to[j++] = '.';
-                       i++;
-                       continue;
-               }
-               /* skip non-printable chars */
-               if (!isalnum(from[i]) && !ispunct(from[i])) {
-                       i++;
-                       continue;
-               }
-               to[j++] = from[i++];
-       }
-       to[j] = '\0';
-}
-
 static void set_usb_iftype(char *to, int if_class_num, size_t len)
 {
        char *type = "generic";
@@ -92,26 +54,40 @@ static void set_usb_iftype(char *to, int if_class_num, size_t len)
        case 1:
                type = "audio";
                break;
+       case 2: /* CDC-Control */
+               break;
        case 3:
                type = "hid";
                break;
+       case 5: /* Physical */
+               break;
+       case 6:
+               type = "media";
+               break;
        case 7:
                type = "printer";
                break;
        case 8:
                type = "storage";
                break;
-       case 2: /* CDC-Control */
-       case 5: /* Physical */
-       case 6: /* Image */
-       case 9: /* HUB */
+       case 9:
+               type = "hub";
+               break;
        case 0x0a: /* CDC-Data */
+               break;
        case 0x0b: /* Chip/Smart Card */
+               break;
        case 0x0d: /* Content Security */
-       case 0x0e: /* Video */
+               break;
+       case 0x0e:
+               type = "video";
+               break;
        case 0xdc: /* Diagnostic Device */
+               break;
        case 0xe0: /* Wireless Controller */
-       case 0xf2: /* Application-specific */
+               break;
+       case 0xfe: /* Application-specific */
+               break;
        case 0xff: /* Vendor-specific */
                break;
        default:
@@ -131,7 +107,7 @@ static int set_usb_mass_storage_ifsubtype(char *to, const char *from, size_t len
        if (eptr != from) {
                switch (type_num) {
                case 2:
-                       type = "cd";
+                       type = "atapi";
                        break;
                case 3:
                        type = "tape";
@@ -141,15 +117,16 @@ static int set_usb_mass_storage_ifsubtype(char *to, const char *from, size_t len
                        type = "floppy";
                        break;
                case 1: /* RBC devices */
+                       type = "rbc";
+                       break;
                case 6: /* Transparent SPC-2 devices */
-                       type = "disk";
+                       type = "scsi";
                        break;
                default:
                        break;
                }
        }
        util_strlcpy(to, type, len);
-
        return type_num;
 }
 
@@ -214,7 +191,7 @@ static int usb_id(struct udev_device *dev)
        dbg(udev, "syspath %s\n", udev_device_get_syspath(dev));
 
        /* usb interface directory */
-       dev_interface = udev_device_get_parent_with_subsystem(dev, "usb");
+       dev_interface = udev_device_get_parent_with_subsystem_devtype(dev, "usb", "usb_interface");
        if (dev_interface == NULL) {
                info(udev, "unable to access usb_interface device of '%s'\n",
                     udev_device_get_syspath(dev));
@@ -227,8 +204,10 @@ static int usb_id(struct udev_device *dev)
                     udev_device_get_sysname(dev));
                return 1;
        }
+
        if_class_num = strtoul(if_class, NULL, 16);
        if (if_class_num == 8) {
+               /* mass storage */
                if_subclass = udev_device_get_sysattr_value(dev_interface, "bInterfaceSubClass");
                if (if_subclass != NULL)
                        protocol = set_usb_mass_storage_ifsubtype(type_str, if_subclass, sizeof(type_str)-1);
@@ -240,21 +219,21 @@ static int usb_id(struct udev_device *dev)
             udev_device_get_syspath(dev_interface), if_class_num, protocol);
 
        /* usb device directory */
-       dev_usb = udev_device_get_parent_with_subsystem(dev_interface, "usb");
+       dev_usb = udev_device_get_parent_with_subsystem_devtype(dev_interface, "usb", "usb_device");
        if (!dev_usb) {
                info(udev, "unable to find parent 'usb' device of '%s'\n",
                     udev_device_get_syspath(dev));
                return 1;
        }
 
-       /* mass storage */
-       if (protocol == 6 && !use_usb_info) {
+       /* mass storage : SCSI or ATAPI */
+       if ((protocol == 6 || protocol == 2) && !use_usb_info) {
                struct udev_device *dev_scsi;
                const char *scsi_model, *scsi_vendor, *scsi_type, *scsi_rev;
                int host, bus, target, lun;
 
                /* get scsi device */
-               dev_scsi = udev_device_get_parent_with_subsystem(dev, "scsi");
+               dev_scsi = udev_device_get_parent_with_subsystem_devtype(dev, "scsi", "scsi_device");
                if (dev_scsi == NULL) {
                        info(udev, "unable to find parent 'scsi' device of '%s'\n",
                             udev_device_get_syspath(dev));
@@ -272,7 +251,8 @@ static int usb_id(struct udev_device *dev)
                             udev_device_get_sysname(dev_scsi));
                        goto fallback;
                }
-               set_str(vendor_str, scsi_vendor, sizeof(vendor_str)-1);
+               udev_util_replace_whitespace(scsi_vendor, vendor_str, sizeof(vendor_str)-1);
+               udev_util_replace_chars(vendor_str, NULL);
 
                scsi_model = udev_device_get_sysattr_value(dev_scsi, "model");
                if (!scsi_model) {
@@ -280,7 +260,8 @@ static int usb_id(struct udev_device *dev)
                             udev_device_get_sysname(dev_scsi));
                        goto fallback;
                }
-               set_str(model_str, scsi_model, sizeof(model_str)-1);
+               udev_util_replace_whitespace(scsi_model, model_str, sizeof(model_str)-1);
+               udev_util_replace_chars(model_str, NULL);
 
                scsi_type = udev_device_get_sysattr_value(dev_scsi, "type");
                if (!scsi_type) {
@@ -296,7 +277,8 @@ static int usb_id(struct udev_device *dev)
                             udev_device_get_sysname(dev_scsi));
                        goto fallback;
                }
-               set_str(revision_str, scsi_rev, sizeof(revision_str)-1);
+               udev_util_replace_whitespace(scsi_rev, revision_str, sizeof(revision_str)-1);
+               udev_util_replace_chars(revision_str, NULL);
 
                /*
                 * some broken devices have the same identifiers
@@ -320,7 +302,8 @@ fallback:
                        info(udev, "No USB vendor information available\n");
                        return 1;
                }
-               set_str(vendor_str, usb_vendor, sizeof(vendor_str)-1);
+               udev_util_replace_whitespace(usb_vendor, vendor_str, sizeof(vendor_str)-1);
+               udev_util_replace_chars(vendor_str, NULL);
        }
 
        if (model_str[0] == '\0') {
@@ -336,23 +319,28 @@ fallback:
                        dbg(udev, "No USB model information available\n");
                        return 1;
                }
-               set_str(model_str, usb_model, sizeof(model_str)-1);
+               udev_util_replace_whitespace(usb_model, model_str, sizeof(model_str)-1);
+               udev_util_replace_chars(model_str, NULL);
        }
 
        if (revision_str[0] == '\0') {
                const char *usb_rev;
 
                usb_rev = udev_device_get_sysattr_value(dev_usb, "bcdDevice");
-               if (usb_rev)
-                       set_str(revision_str, usb_rev, sizeof(revision_str)-1);
+               if (usb_rev) {
+                       udev_util_replace_whitespace(usb_rev, revision_str, sizeof(revision_str)-1);
+                       udev_util_replace_chars(revision_str, NULL);
+               }
        }
 
        if (serial_str[0] == '\0') {
                const char *usb_serial;
 
                usb_serial = udev_device_get_sysattr_value(dev_usb, "serial");
-               if (usb_serial)
-                       set_str(serial_str, usb_serial, sizeof(serial_str)-1);
+               if (usb_serial) {
+                       udev_util_replace_whitespace(usb_serial, serial_str, sizeof(serial_str)-1);
+                       udev_util_replace_chars(serial_str, NULL);
+               }
        }
        return 0;
 }
@@ -368,7 +356,7 @@ int main(int argc, char **argv)
                {}
        };
        struct udev *udev;
-       struct udev_device *dev;
+       struct udev_device *dev = NULL;
        char syspath[UTIL_PATH_SIZE];
        const char *devpath;
        static int export;
@@ -408,7 +396,7 @@ int main(int argc, char **argv)
                        printf("Usage: usb_id [--usb-info] [--num-info] [--export] [--help] <devpath>\n"
                               "  --usb-info  use usb strings instead\n"
                               "  --num-info  use numerical values\n"
-                              "  --export    print values as environemt keys\n"
+                              "  --export    print values as environment keys\n"
                               "  --help      print this help text\n\n");
                default:
                        retval = 1;