forked from ~ljy/RK356X_SDK_RELEASE

hc
2024-05-11 04dd17822334871b23ea2862f7798fb0e0007777
kernel/drivers/usb/roles/class.c
....@@ -8,6 +8,7 @@
88 */
99
1010 #include <linux/usb/role.h>
11
+#include <linux/property.h>
1112 #include <linux/device.h>
1213 #include <linux/module.h>
1314 #include <linux/mutex.h>
....@@ -47,9 +48,11 @@
4748
4849 mutex_lock(&sw->lock);
4950
50
- ret = sw->set(sw->dev.parent, role);
51
- if (!ret)
51
+ ret = sw->set(sw, role);
52
+ if (!ret) {
5253 sw->role = role;
54
+ kobject_uevent(&sw->dev.kobj, KOBJ_CHANGE);
55
+ }
5356
5457 mutex_unlock(&sw->lock);
5558
....@@ -74,7 +77,7 @@
7477 mutex_lock(&sw->lock);
7578
7679 if (sw->get)
77
- role = sw->get(sw->dev.parent);
80
+ role = sw->get(sw);
7881 else
7982 role = sw->role;
8083
....@@ -84,19 +87,32 @@
8487 }
8588 EXPORT_SYMBOL_GPL(usb_role_switch_get_role);
8689
87
-static int __switch_match(struct device *dev, const void *name)
88
-{
89
- return !strcmp((const char *)name, dev_name(dev));
90
-}
91
-
92
-static void *usb_role_switch_match(struct device_connection *con, int ep,
90
+static void *usb_role_switch_match(struct fwnode_handle *fwnode, const char *id,
9391 void *data)
9492 {
9593 struct device *dev;
9694
97
- dev = class_find_device(role_class, NULL, con->endpoint[ep],
98
- __switch_match);
95
+ if (id && !fwnode_property_present(fwnode, id))
96
+ return NULL;
9997
98
+ dev = class_find_device_by_fwnode(role_class, fwnode);
99
+
100
+ return dev ? to_role_switch(dev) : ERR_PTR(-EPROBE_DEFER);
101
+}
102
+
103
+static struct usb_role_switch *
104
+usb_role_switch_is_parent(struct fwnode_handle *fwnode)
105
+{
106
+ struct fwnode_handle *parent = fwnode_get_parent(fwnode);
107
+ struct device *dev;
108
+
109
+ if (!fwnode_property_present(parent, "usb-role-switch")) {
110
+ fwnode_handle_put(parent);
111
+ return NULL;
112
+ }
113
+
114
+ dev = class_find_device_by_fwnode(role_class, parent);
115
+ fwnode_handle_put(parent);
100116 return dev ? to_role_switch(dev) : ERR_PTR(-EPROBE_DEFER);
101117 }
102118
....@@ -111,8 +127,10 @@
111127 {
112128 struct usb_role_switch *sw;
113129
114
- sw = device_connection_find_match(dev, "usb-role-switch", NULL,
115
- usb_role_switch_match);
130
+ sw = usb_role_switch_is_parent(dev_fwnode(dev));
131
+ if (!sw)
132
+ sw = device_connection_find_match(dev, "usb-role-switch", NULL,
133
+ usb_role_switch_match);
116134
117135 if (!IS_ERR_OR_NULL(sw))
118136 WARN_ON(!try_module_get(sw->dev.parent->driver->owner));
....@@ -120,6 +138,28 @@
120138 return sw;
121139 }
122140 EXPORT_SYMBOL_GPL(usb_role_switch_get);
141
+
142
+/**
143
+ * fwnode_usb_role_switch_get - Find USB role switch linked with the caller
144
+ * @fwnode: The caller device node
145
+ *
146
+ * This is similar to the usb_role_switch_get() function above, but it searches
147
+ * the switch using fwnode instead of device entry.
148
+ */
149
+struct usb_role_switch *fwnode_usb_role_switch_get(struct fwnode_handle *fwnode)
150
+{
151
+ struct usb_role_switch *sw;
152
+
153
+ sw = usb_role_switch_is_parent(fwnode);
154
+ if (!sw)
155
+ sw = fwnode_connection_find_match(fwnode, "usb-role-switch",
156
+ NULL, usb_role_switch_match);
157
+ if (!IS_ERR_OR_NULL(sw))
158
+ WARN_ON(!try_module_get(sw->dev.parent->driver->owner));
159
+
160
+ return sw;
161
+}
162
+EXPORT_SYMBOL_GPL(fwnode_usb_role_switch_get);
123163
124164 /**
125165 * usb_role_switch_put - Release handle to a switch
....@@ -136,10 +176,33 @@
136176 }
137177 EXPORT_SYMBOL_GPL(usb_role_switch_put);
138178
179
+/**
180
+ * usb_role_switch_find_by_fwnode - Find USB role switch with its fwnode
181
+ * @fwnode: fwnode of the USB Role Switch
182
+ *
183
+ * Finds and returns role switch with @fwnode. The reference count for the
184
+ * found switch is incremented.
185
+ */
186
+struct usb_role_switch *
187
+usb_role_switch_find_by_fwnode(const struct fwnode_handle *fwnode)
188
+{
189
+ struct device *dev;
190
+
191
+ if (!fwnode)
192
+ return NULL;
193
+
194
+ dev = class_find_device_by_fwnode(role_class, fwnode);
195
+ if (dev)
196
+ WARN_ON(!try_module_get(dev->parent->driver->owner));
197
+
198
+ return dev ? to_role_switch(dev) : NULL;
199
+}
200
+EXPORT_SYMBOL_GPL(usb_role_switch_find_by_fwnode);
201
+
139202 static umode_t
140203 usb_role_switch_is_visible(struct kobject *kobj, struct attribute *attr, int n)
141204 {
142
- struct device *dev = container_of(kobj, typeof(*dev), kobj);
205
+ struct device *dev = kobj_to_dev(kobj);
143206 struct usb_role_switch *sw = to_role_switch(dev);
144207
145208 if (sw->allow_userspace_control)
....@@ -266,9 +329,12 @@
266329 sw->get = desc->get;
267330
268331 sw->dev.parent = parent;
332
+ sw->dev.fwnode = desc->fwnode;
269333 sw->dev.class = role_class;
270334 sw->dev.type = &usb_role_dev_type;
271
- dev_set_name(&sw->dev, "%s-role-switch", dev_name(parent));
335
+ dev_set_drvdata(&sw->dev, desc->driver_data);
336
+ dev_set_name(&sw->dev, "%s-role-switch",
337
+ desc->name ? desc->name : dev_name(parent));
272338
273339 ret = device_register(&sw->dev);
274340 if (ret) {
....@@ -295,6 +361,27 @@
295361 }
296362 EXPORT_SYMBOL_GPL(usb_role_switch_unregister);
297363
364
+/**
365
+ * usb_role_switch_set_drvdata - Assign private data pointer to a switch
366
+ * @sw: USB Role Switch
367
+ * @data: Private data pointer
368
+ */
369
+void usb_role_switch_set_drvdata(struct usb_role_switch *sw, void *data)
370
+{
371
+ dev_set_drvdata(&sw->dev, data);
372
+}
373
+EXPORT_SYMBOL_GPL(usb_role_switch_set_drvdata);
374
+
375
+/**
376
+ * usb_role_switch_get_drvdata - Get the private data pointer of a switch
377
+ * @sw: USB Role Switch
378
+ */
379
+void *usb_role_switch_get_drvdata(struct usb_role_switch *sw)
380
+{
381
+ return dev_get_drvdata(&sw->dev);
382
+}
383
+EXPORT_SYMBOL_GPL(usb_role_switch_get_drvdata);
384
+
298385 static int __init usb_roles_init(void)
299386 {
300387 role_class = class_create(THIS_MODULE, "usb_role");