| .. | .. |
|---|
| 15 | 15 | #include <linux/kernel.h> |
|---|
| 16 | 16 | #include <linux/string.h> |
|---|
| 17 | 17 | #include <linux/usb.h> |
|---|
| 18 | +#include <linux/usb/hcd.h> |
|---|
| 18 | 19 | #include <linux/usb/quirks.h> |
|---|
| 19 | 20 | #include <linux/of.h> |
|---|
| 20 | 21 | #include "usb.h" |
|---|
| .. | .. |
|---|
| 848 | 849 | static umode_t dev_string_attrs_are_visible(struct kobject *kobj, |
|---|
| 849 | 850 | struct attribute *a, int n) |
|---|
| 850 | 851 | { |
|---|
| 851 | | - struct device *dev = container_of(kobj, struct device, kobj); |
|---|
| 852 | + struct device *dev = kobj_to_dev(kobj); |
|---|
| 852 | 853 | struct usb_device *udev = to_usb_device(dev); |
|---|
| 853 | 854 | |
|---|
| 854 | 855 | if (a == &dev_attr_manufacturer.attr) { |
|---|
| .. | .. |
|---|
| 882 | 883 | struct bin_attribute *attr, |
|---|
| 883 | 884 | char *buf, loff_t off, size_t count) |
|---|
| 884 | 885 | { |
|---|
| 885 | | - struct device *dev = container_of(kobj, struct device, kobj); |
|---|
| 886 | + struct device *dev = kobj_to_dev(kobj); |
|---|
| 886 | 887 | struct usb_device *udev = to_usb_device(dev); |
|---|
| 887 | 888 | size_t nleft = count; |
|---|
| 888 | 889 | size_t srclen, n; |
|---|
| .. | .. |
|---|
| 927 | 928 | .size = 18 + 65535, /* dev descr + max-size raw descriptor */ |
|---|
| 928 | 929 | }; |
|---|
| 929 | 930 | |
|---|
| 931 | +/* |
|---|
| 932 | + * Show & store the current value of authorized_default |
|---|
| 933 | + */ |
|---|
| 934 | +static ssize_t authorized_default_show(struct device *dev, |
|---|
| 935 | + struct device_attribute *attr, char *buf) |
|---|
| 936 | +{ |
|---|
| 937 | + struct usb_device *rh_usb_dev = to_usb_device(dev); |
|---|
| 938 | + struct usb_bus *usb_bus = rh_usb_dev->bus; |
|---|
| 939 | + struct usb_hcd *hcd; |
|---|
| 940 | + |
|---|
| 941 | + hcd = bus_to_hcd(usb_bus); |
|---|
| 942 | + return snprintf(buf, PAGE_SIZE, "%u\n", hcd->dev_policy); |
|---|
| 943 | +} |
|---|
| 944 | + |
|---|
| 945 | +static ssize_t authorized_default_store(struct device *dev, |
|---|
| 946 | + struct device_attribute *attr, |
|---|
| 947 | + const char *buf, size_t size) |
|---|
| 948 | +{ |
|---|
| 949 | + ssize_t result; |
|---|
| 950 | + unsigned int val; |
|---|
| 951 | + struct usb_device *rh_usb_dev = to_usb_device(dev); |
|---|
| 952 | + struct usb_bus *usb_bus = rh_usb_dev->bus; |
|---|
| 953 | + struct usb_hcd *hcd; |
|---|
| 954 | + |
|---|
| 955 | + hcd = bus_to_hcd(usb_bus); |
|---|
| 956 | + result = sscanf(buf, "%u\n", &val); |
|---|
| 957 | + if (result == 1) { |
|---|
| 958 | + hcd->dev_policy = val <= USB_DEVICE_AUTHORIZE_INTERNAL ? |
|---|
| 959 | + val : USB_DEVICE_AUTHORIZE_ALL; |
|---|
| 960 | + result = size; |
|---|
| 961 | + } else { |
|---|
| 962 | + result = -EINVAL; |
|---|
| 963 | + } |
|---|
| 964 | + return result; |
|---|
| 965 | +} |
|---|
| 966 | +static DEVICE_ATTR_RW(authorized_default); |
|---|
| 967 | + |
|---|
| 968 | +/* |
|---|
| 969 | + * interface_authorized_default_show - show default authorization status |
|---|
| 970 | + * for USB interfaces |
|---|
| 971 | + * |
|---|
| 972 | + * note: interface_authorized_default is the default value |
|---|
| 973 | + * for initializing the authorized attribute of interfaces |
|---|
| 974 | + */ |
|---|
| 975 | +static ssize_t interface_authorized_default_show(struct device *dev, |
|---|
| 976 | + struct device_attribute *attr, char *buf) |
|---|
| 977 | +{ |
|---|
| 978 | + struct usb_device *usb_dev = to_usb_device(dev); |
|---|
| 979 | + struct usb_hcd *hcd = bus_to_hcd(usb_dev->bus); |
|---|
| 980 | + |
|---|
| 981 | + return sprintf(buf, "%u\n", !!HCD_INTF_AUTHORIZED(hcd)); |
|---|
| 982 | +} |
|---|
| 983 | + |
|---|
| 984 | +/* |
|---|
| 985 | + * interface_authorized_default_store - store default authorization status |
|---|
| 986 | + * for USB interfaces |
|---|
| 987 | + * |
|---|
| 988 | + * note: interface_authorized_default is the default value |
|---|
| 989 | + * for initializing the authorized attribute of interfaces |
|---|
| 990 | + */ |
|---|
| 991 | +static ssize_t interface_authorized_default_store(struct device *dev, |
|---|
| 992 | + struct device_attribute *attr, const char *buf, size_t count) |
|---|
| 993 | +{ |
|---|
| 994 | + struct usb_device *usb_dev = to_usb_device(dev); |
|---|
| 995 | + struct usb_hcd *hcd = bus_to_hcd(usb_dev->bus); |
|---|
| 996 | + int rc = count; |
|---|
| 997 | + bool val; |
|---|
| 998 | + |
|---|
| 999 | + if (strtobool(buf, &val) != 0) |
|---|
| 1000 | + return -EINVAL; |
|---|
| 1001 | + |
|---|
| 1002 | + if (val) |
|---|
| 1003 | + set_bit(HCD_FLAG_INTF_AUTHORIZED, &hcd->flags); |
|---|
| 1004 | + else |
|---|
| 1005 | + clear_bit(HCD_FLAG_INTF_AUTHORIZED, &hcd->flags); |
|---|
| 1006 | + |
|---|
| 1007 | + return rc; |
|---|
| 1008 | +} |
|---|
| 1009 | +static DEVICE_ATTR_RW(interface_authorized_default); |
|---|
| 1010 | + |
|---|
| 1011 | +/* Group all the USB bus attributes */ |
|---|
| 1012 | +static struct attribute *usb_bus_attrs[] = { |
|---|
| 1013 | + &dev_attr_authorized_default.attr, |
|---|
| 1014 | + &dev_attr_interface_authorized_default.attr, |
|---|
| 1015 | + NULL, |
|---|
| 1016 | +}; |
|---|
| 1017 | + |
|---|
| 1018 | +static const struct attribute_group usb_bus_attr_group = { |
|---|
| 1019 | + .name = NULL, /* we want them in the same directory */ |
|---|
| 1020 | + .attrs = usb_bus_attrs, |
|---|
| 1021 | +}; |
|---|
| 1022 | + |
|---|
| 1023 | + |
|---|
| 1024 | +static int add_default_authorized_attributes(struct device *dev) |
|---|
| 1025 | +{ |
|---|
| 1026 | + int rc = 0; |
|---|
| 1027 | + |
|---|
| 1028 | + if (is_usb_device(dev)) |
|---|
| 1029 | + rc = sysfs_create_group(&dev->kobj, &usb_bus_attr_group); |
|---|
| 1030 | + |
|---|
| 1031 | + return rc; |
|---|
| 1032 | +} |
|---|
| 1033 | + |
|---|
| 1034 | +static void remove_default_authorized_attributes(struct device *dev) |
|---|
| 1035 | +{ |
|---|
| 1036 | + if (is_usb_device(dev)) { |
|---|
| 1037 | + sysfs_remove_group(&dev->kobj, &usb_bus_attr_group); |
|---|
| 1038 | + } |
|---|
| 1039 | +} |
|---|
| 1040 | + |
|---|
| 930 | 1041 | int usb_create_sysfs_dev_files(struct usb_device *udev) |
|---|
| 931 | 1042 | { |
|---|
| 932 | 1043 | struct device *dev = &udev->dev; |
|---|
| .. | .. |
|---|
| 943 | 1054 | retval = add_power_attributes(dev); |
|---|
| 944 | 1055 | if (retval) |
|---|
| 945 | 1056 | goto error; |
|---|
| 1057 | + |
|---|
| 1058 | + if (is_root_hub(udev)) { |
|---|
| 1059 | + retval = add_default_authorized_attributes(dev); |
|---|
| 1060 | + if (retval) |
|---|
| 1061 | + goto error; |
|---|
| 1062 | + } |
|---|
| 946 | 1063 | return retval; |
|---|
| 1064 | + |
|---|
| 947 | 1065 | error: |
|---|
| 948 | 1066 | usb_remove_sysfs_dev_files(udev); |
|---|
| 949 | 1067 | return retval; |
|---|
| .. | .. |
|---|
| 952 | 1070 | void usb_remove_sysfs_dev_files(struct usb_device *udev) |
|---|
| 953 | 1071 | { |
|---|
| 954 | 1072 | struct device *dev = &udev->dev; |
|---|
| 1073 | + |
|---|
| 1074 | + if (is_root_hub(udev)) |
|---|
| 1075 | + remove_default_authorized_attributes(dev); |
|---|
| 955 | 1076 | |
|---|
| 956 | 1077 | remove_power_attributes(dev); |
|---|
| 957 | 1078 | remove_persist_attributes(dev); |
|---|
| .. | .. |
|---|
| 1117 | 1238 | static umode_t intf_assoc_attrs_are_visible(struct kobject *kobj, |
|---|
| 1118 | 1239 | struct attribute *a, int n) |
|---|
| 1119 | 1240 | { |
|---|
| 1120 | | - struct device *dev = container_of(kobj, struct device, kobj); |
|---|
| 1241 | + struct device *dev = kobj_to_dev(kobj); |
|---|
| 1121 | 1242 | struct usb_interface *intf = to_usb_interface(dev); |
|---|
| 1122 | 1243 | |
|---|
| 1123 | 1244 | if (intf->intf_assoc == NULL) |
|---|
| .. | .. |
|---|
| 1146 | 1267 | |
|---|
| 1147 | 1268 | if (!alt->string && !(udev->quirks & USB_QUIRK_CONFIG_INTF_STRINGS)) |
|---|
| 1148 | 1269 | alt->string = usb_cache_string(udev, alt->desc.iInterface); |
|---|
| 1149 | | - if (alt->string && device_create_file(&intf->dev, &dev_attr_interface)) |
|---|
| 1150 | | - ; /* We don't actually care if the function fails. */ |
|---|
| 1270 | + if (alt->string && device_create_file(&intf->dev, &dev_attr_interface)) { |
|---|
| 1271 | + /* This is not a serious error */ |
|---|
| 1272 | + dev_dbg(&intf->dev, "interface string descriptor file not created\n"); |
|---|
| 1273 | + } |
|---|
| 1151 | 1274 | intf->sysfs_files_created = 1; |
|---|
| 1152 | 1275 | } |
|---|
| 1153 | 1276 | |
|---|