.. | .. |
---|
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; |
---|
889 | 890 | int cfgno; |
---|
890 | 891 | void *src; |
---|
891 | | - int retval; |
---|
892 | 892 | |
---|
893 | | - retval = usb_lock_device_interruptible(udev); |
---|
894 | | - if (retval < 0) |
---|
895 | | - return -EINTR; |
---|
896 | 893 | /* The binary attribute begins with the device descriptor. |
---|
897 | 894 | * Following that are the raw descriptor entries for all the |
---|
898 | 895 | * configurations (config plus subsidiary descriptors). |
---|
.. | .. |
---|
917 | 914 | off -= srclen; |
---|
918 | 915 | } |
---|
919 | 916 | } |
---|
920 | | - usb_unlock_device(udev); |
---|
921 | 917 | return count - nleft; |
---|
922 | 918 | } |
---|
923 | 919 | |
---|
.. | .. |
---|
926 | 922 | .read = read_descriptors, |
---|
927 | 923 | .size = 18 + 65535, /* dev descr + max-size raw descriptor */ |
---|
928 | 924 | }; |
---|
| 925 | + |
---|
| 926 | +/* |
---|
| 927 | + * Show & store the current value of authorized_default |
---|
| 928 | + */ |
---|
| 929 | +static ssize_t authorized_default_show(struct device *dev, |
---|
| 930 | + struct device_attribute *attr, char *buf) |
---|
| 931 | +{ |
---|
| 932 | + struct usb_device *rh_usb_dev = to_usb_device(dev); |
---|
| 933 | + struct usb_bus *usb_bus = rh_usb_dev->bus; |
---|
| 934 | + struct usb_hcd *hcd; |
---|
| 935 | + |
---|
| 936 | + hcd = bus_to_hcd(usb_bus); |
---|
| 937 | + return snprintf(buf, PAGE_SIZE, "%u\n", hcd->dev_policy); |
---|
| 938 | +} |
---|
| 939 | + |
---|
| 940 | +static ssize_t authorized_default_store(struct device *dev, |
---|
| 941 | + struct device_attribute *attr, |
---|
| 942 | + const char *buf, size_t size) |
---|
| 943 | +{ |
---|
| 944 | + ssize_t result; |
---|
| 945 | + unsigned int val; |
---|
| 946 | + struct usb_device *rh_usb_dev = to_usb_device(dev); |
---|
| 947 | + struct usb_bus *usb_bus = rh_usb_dev->bus; |
---|
| 948 | + struct usb_hcd *hcd; |
---|
| 949 | + |
---|
| 950 | + hcd = bus_to_hcd(usb_bus); |
---|
| 951 | + result = sscanf(buf, "%u\n", &val); |
---|
| 952 | + if (result == 1) { |
---|
| 953 | + hcd->dev_policy = val <= USB_DEVICE_AUTHORIZE_INTERNAL ? |
---|
| 954 | + val : USB_DEVICE_AUTHORIZE_ALL; |
---|
| 955 | + result = size; |
---|
| 956 | + } else { |
---|
| 957 | + result = -EINVAL; |
---|
| 958 | + } |
---|
| 959 | + return result; |
---|
| 960 | +} |
---|
| 961 | +static DEVICE_ATTR_RW(authorized_default); |
---|
| 962 | + |
---|
| 963 | +/* |
---|
| 964 | + * interface_authorized_default_show - show default authorization status |
---|
| 965 | + * for USB interfaces |
---|
| 966 | + * |
---|
| 967 | + * note: interface_authorized_default is the default value |
---|
| 968 | + * for initializing the authorized attribute of interfaces |
---|
| 969 | + */ |
---|
| 970 | +static ssize_t interface_authorized_default_show(struct device *dev, |
---|
| 971 | + struct device_attribute *attr, char *buf) |
---|
| 972 | +{ |
---|
| 973 | + struct usb_device *usb_dev = to_usb_device(dev); |
---|
| 974 | + struct usb_hcd *hcd = bus_to_hcd(usb_dev->bus); |
---|
| 975 | + |
---|
| 976 | + return sprintf(buf, "%u\n", !!HCD_INTF_AUTHORIZED(hcd)); |
---|
| 977 | +} |
---|
| 978 | + |
---|
| 979 | +/* |
---|
| 980 | + * interface_authorized_default_store - store default authorization status |
---|
| 981 | + * for USB interfaces |
---|
| 982 | + * |
---|
| 983 | + * note: interface_authorized_default is the default value |
---|
| 984 | + * for initializing the authorized attribute of interfaces |
---|
| 985 | + */ |
---|
| 986 | +static ssize_t interface_authorized_default_store(struct device *dev, |
---|
| 987 | + struct device_attribute *attr, const char *buf, size_t count) |
---|
| 988 | +{ |
---|
| 989 | + struct usb_device *usb_dev = to_usb_device(dev); |
---|
| 990 | + struct usb_hcd *hcd = bus_to_hcd(usb_dev->bus); |
---|
| 991 | + int rc = count; |
---|
| 992 | + bool val; |
---|
| 993 | + |
---|
| 994 | + if (strtobool(buf, &val) != 0) |
---|
| 995 | + return -EINVAL; |
---|
| 996 | + |
---|
| 997 | + if (val) |
---|
| 998 | + set_bit(HCD_FLAG_INTF_AUTHORIZED, &hcd->flags); |
---|
| 999 | + else |
---|
| 1000 | + clear_bit(HCD_FLAG_INTF_AUTHORIZED, &hcd->flags); |
---|
| 1001 | + |
---|
| 1002 | + return rc; |
---|
| 1003 | +} |
---|
| 1004 | +static DEVICE_ATTR_RW(interface_authorized_default); |
---|
| 1005 | + |
---|
| 1006 | +/* Group all the USB bus attributes */ |
---|
| 1007 | +static struct attribute *usb_bus_attrs[] = { |
---|
| 1008 | + &dev_attr_authorized_default.attr, |
---|
| 1009 | + &dev_attr_interface_authorized_default.attr, |
---|
| 1010 | + NULL, |
---|
| 1011 | +}; |
---|
| 1012 | + |
---|
| 1013 | +static const struct attribute_group usb_bus_attr_group = { |
---|
| 1014 | + .name = NULL, /* we want them in the same directory */ |
---|
| 1015 | + .attrs = usb_bus_attrs, |
---|
| 1016 | +}; |
---|
| 1017 | + |
---|
| 1018 | + |
---|
| 1019 | +static int add_default_authorized_attributes(struct device *dev) |
---|
| 1020 | +{ |
---|
| 1021 | + int rc = 0; |
---|
| 1022 | + |
---|
| 1023 | + if (is_usb_device(dev)) |
---|
| 1024 | + rc = sysfs_create_group(&dev->kobj, &usb_bus_attr_group); |
---|
| 1025 | + |
---|
| 1026 | + return rc; |
---|
| 1027 | +} |
---|
| 1028 | + |
---|
| 1029 | +static void remove_default_authorized_attributes(struct device *dev) |
---|
| 1030 | +{ |
---|
| 1031 | + if (is_usb_device(dev)) { |
---|
| 1032 | + sysfs_remove_group(&dev->kobj, &usb_bus_attr_group); |
---|
| 1033 | + } |
---|
| 1034 | +} |
---|
929 | 1035 | |
---|
930 | 1036 | int usb_create_sysfs_dev_files(struct usb_device *udev) |
---|
931 | 1037 | { |
---|
.. | .. |
---|
943 | 1049 | retval = add_power_attributes(dev); |
---|
944 | 1050 | if (retval) |
---|
945 | 1051 | goto error; |
---|
| 1052 | + |
---|
| 1053 | + if (is_root_hub(udev)) { |
---|
| 1054 | + retval = add_default_authorized_attributes(dev); |
---|
| 1055 | + if (retval) |
---|
| 1056 | + goto error; |
---|
| 1057 | + } |
---|
946 | 1058 | return retval; |
---|
| 1059 | + |
---|
947 | 1060 | error: |
---|
948 | 1061 | usb_remove_sysfs_dev_files(udev); |
---|
949 | 1062 | return retval; |
---|
.. | .. |
---|
952 | 1065 | void usb_remove_sysfs_dev_files(struct usb_device *udev) |
---|
953 | 1066 | { |
---|
954 | 1067 | struct device *dev = &udev->dev; |
---|
| 1068 | + |
---|
| 1069 | + if (is_root_hub(udev)) |
---|
| 1070 | + remove_default_authorized_attributes(dev); |
---|
955 | 1071 | |
---|
956 | 1072 | remove_power_attributes(dev); |
---|
957 | 1073 | remove_persist_attributes(dev); |
---|
.. | .. |
---|
1117 | 1233 | static umode_t intf_assoc_attrs_are_visible(struct kobject *kobj, |
---|
1118 | 1234 | struct attribute *a, int n) |
---|
1119 | 1235 | { |
---|
1120 | | - struct device *dev = container_of(kobj, struct device, kobj); |
---|
| 1236 | + struct device *dev = kobj_to_dev(kobj); |
---|
1121 | 1237 | struct usb_interface *intf = to_usb_interface(dev); |
---|
1122 | 1238 | |
---|
1123 | 1239 | if (intf->intf_assoc == NULL) |
---|
.. | .. |
---|
1146 | 1262 | |
---|
1147 | 1263 | if (!alt->string && !(udev->quirks & USB_QUIRK_CONFIG_INTF_STRINGS)) |
---|
1148 | 1264 | 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. */ |
---|
| 1265 | + if (alt->string && device_create_file(&intf->dev, &dev_attr_interface)) { |
---|
| 1266 | + /* This is not a serious error */ |
---|
| 1267 | + dev_dbg(&intf->dev, "interface string descriptor file not created\n"); |
---|
| 1268 | + } |
---|
1151 | 1269 | intf->sysfs_files_created = 1; |
---|
1152 | 1270 | } |
---|
1153 | 1271 | |
---|