hc
2024-02-20 102a0743326a03cd1a1202ceda21e175b7d3575c
kernel/drivers/usb/core/sysfs.c
....@@ -15,6 +15,7 @@
1515 #include <linux/kernel.h>
1616 #include <linux/string.h>
1717 #include <linux/usb.h>
18
+#include <linux/usb/hcd.h>
1819 #include <linux/usb/quirks.h>
1920 #include <linux/of.h>
2021 #include "usb.h"
....@@ -848,7 +849,7 @@
848849 static umode_t dev_string_attrs_are_visible(struct kobject *kobj,
849850 struct attribute *a, int n)
850851 {
851
- struct device *dev = container_of(kobj, struct device, kobj);
852
+ struct device *dev = kobj_to_dev(kobj);
852853 struct usb_device *udev = to_usb_device(dev);
853854
854855 if (a == &dev_attr_manufacturer.attr) {
....@@ -882,17 +883,13 @@
882883 struct bin_attribute *attr,
883884 char *buf, loff_t off, size_t count)
884885 {
885
- struct device *dev = container_of(kobj, struct device, kobj);
886
+ struct device *dev = kobj_to_dev(kobj);
886887 struct usb_device *udev = to_usb_device(dev);
887888 size_t nleft = count;
888889 size_t srclen, n;
889890 int cfgno;
890891 void *src;
891
- int retval;
892892
893
- retval = usb_lock_device_interruptible(udev);
894
- if (retval < 0)
895
- return -EINTR;
896893 /* The binary attribute begins with the device descriptor.
897894 * Following that are the raw descriptor entries for all the
898895 * configurations (config plus subsidiary descriptors).
....@@ -917,7 +914,6 @@
917914 off -= srclen;
918915 }
919916 }
920
- usb_unlock_device(udev);
921917 return count - nleft;
922918 }
923919
....@@ -926,6 +922,116 @@
926922 .read = read_descriptors,
927923 .size = 18 + 65535, /* dev descr + max-size raw descriptor */
928924 };
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
+}
9291035
9301036 int usb_create_sysfs_dev_files(struct usb_device *udev)
9311037 {
....@@ -943,7 +1049,14 @@
9431049 retval = add_power_attributes(dev);
9441050 if (retval)
9451051 goto error;
1052
+
1053
+ if (is_root_hub(udev)) {
1054
+ retval = add_default_authorized_attributes(dev);
1055
+ if (retval)
1056
+ goto error;
1057
+ }
9461058 return retval;
1059
+
9471060 error:
9481061 usb_remove_sysfs_dev_files(udev);
9491062 return retval;
....@@ -952,6 +1065,9 @@
9521065 void usb_remove_sysfs_dev_files(struct usb_device *udev)
9531066 {
9541067 struct device *dev = &udev->dev;
1068
+
1069
+ if (is_root_hub(udev))
1070
+ remove_default_authorized_attributes(dev);
9551071
9561072 remove_power_attributes(dev);
9571073 remove_persist_attributes(dev);
....@@ -1117,7 +1233,7 @@
11171233 static umode_t intf_assoc_attrs_are_visible(struct kobject *kobj,
11181234 struct attribute *a, int n)
11191235 {
1120
- struct device *dev = container_of(kobj, struct device, kobj);
1236
+ struct device *dev = kobj_to_dev(kobj);
11211237 struct usb_interface *intf = to_usb_interface(dev);
11221238
11231239 if (intf->intf_assoc == NULL)
....@@ -1146,8 +1262,10 @@
11461262
11471263 if (!alt->string && !(udev->quirks & USB_QUIRK_CONFIG_INTF_STRINGS))
11481264 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
+ }
11511269 intf->sysfs_files_created = 1;
11521270 }
11531271