forked from ~ljy/RK356X_SDK_RELEASE

hc
2023-12-11 072de836f53be56a70cecf70b43ae43b7ce17376
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,7 +883,7 @@
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;
....@@ -927,6 +928,116 @@
927928 .size = 18 + 65535, /* dev descr + max-size raw descriptor */
928929 };
929930
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
+
9301041 int usb_create_sysfs_dev_files(struct usb_device *udev)
9311042 {
9321043 struct device *dev = &udev->dev;
....@@ -943,7 +1054,14 @@
9431054 retval = add_power_attributes(dev);
9441055 if (retval)
9451056 goto error;
1057
+
1058
+ if (is_root_hub(udev)) {
1059
+ retval = add_default_authorized_attributes(dev);
1060
+ if (retval)
1061
+ goto error;
1062
+ }
9461063 return retval;
1064
+
9471065 error:
9481066 usb_remove_sysfs_dev_files(udev);
9491067 return retval;
....@@ -952,6 +1070,9 @@
9521070 void usb_remove_sysfs_dev_files(struct usb_device *udev)
9531071 {
9541072 struct device *dev = &udev->dev;
1073
+
1074
+ if (is_root_hub(udev))
1075
+ remove_default_authorized_attributes(dev);
9551076
9561077 remove_power_attributes(dev);
9571078 remove_persist_attributes(dev);
....@@ -1117,7 +1238,7 @@
11171238 static umode_t intf_assoc_attrs_are_visible(struct kobject *kobj,
11181239 struct attribute *a, int n)
11191240 {
1120
- struct device *dev = container_of(kobj, struct device, kobj);
1241
+ struct device *dev = kobj_to_dev(kobj);
11211242 struct usb_interface *intf = to_usb_interface(dev);
11221243
11231244 if (intf->intf_assoc == NULL)
....@@ -1146,8 +1267,10 @@
11461267
11471268 if (!alt->string && !(udev->quirks & USB_QUIRK_CONFIG_INTF_STRINGS))
11481269 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
+ }
11511274 intf->sysfs_files_created = 1;
11521275 }
11531276