Lines Matching +full:usb +full:- +full:role +full:- +full:switch
1 // SPDX-License-Identifier: GPL-2.0
3 * USB Role Switch Support
11 #include <linux/usb/role.h>
28 enum usb_role role; member
46 ret = sysfs_create_link(&dev->kobj, &connector->kobj, "connector"); in connector_bind()
50 ret = sysfs_create_link(&connector->kobj, &dev->kobj, "usb-role-switch"); in connector_bind()
52 sysfs_remove_link(&dev->kobj, "connector"); in connector_bind()
59 sysfs_remove_link(&connector->kobj, "usb-role-switch"); in connector_unbind()
60 sysfs_remove_link(&dev->kobj, "connector"); in connector_unbind()
69 * usb_role_switch_set_role - Set USB role for a switch
70 * @sw: USB role switch
71 * @role: USB role to be switched to
73 * Set USB role @role for @sw.
75 int usb_role_switch_set_role(struct usb_role_switch *sw, enum usb_role role) in usb_role_switch_set_role() argument
82 if (!sw->registered) in usb_role_switch_set_role()
83 return -EOPNOTSUPP; in usb_role_switch_set_role()
85 mutex_lock(&sw->lock); in usb_role_switch_set_role()
87 ret = sw->set(sw, role); in usb_role_switch_set_role()
89 sw->role = role; in usb_role_switch_set_role()
90 kobject_uevent(&sw->dev.kobj, KOBJ_CHANGE); in usb_role_switch_set_role()
93 mutex_unlock(&sw->lock); in usb_role_switch_set_role()
100 * usb_role_switch_get_role - Get the USB role for a switch
101 * @sw: USB role switch
103 * Depending on the role-switch-driver this function returns either a cached
104 * value of the last set role, or reads back the actual value from the hardware.
108 enum usb_role role; in usb_role_switch_get_role() local
110 if (IS_ERR_OR_NULL(sw) || !sw->registered) in usb_role_switch_get_role()
113 mutex_lock(&sw->lock); in usb_role_switch_get_role()
115 if (sw->get) in usb_role_switch_get_role()
116 role = sw->get(sw); in usb_role_switch_get_role()
118 role = sw->role; in usb_role_switch_get_role()
120 mutex_unlock(&sw->lock); in usb_role_switch_get_role()
122 return role; in usb_role_switch_get_role()
136 return dev ? to_role_switch(dev) : ERR_PTR(-EPROBE_DEFER); in usb_role_switch_match()
145 if (!fwnode_property_present(parent, "usb-role-switch")) { in usb_role_switch_is_parent()
152 return dev ? to_role_switch(dev) : ERR_PTR(-EPROBE_DEFER); in usb_role_switch_is_parent()
156 * usb_role_switch_get - Find USB role switch linked with the caller
159 * Finds and returns role switch linked with @dev. The reference count for the
160 * found switch is incremented.
168 sw = device_connection_find_match(dev, "usb-role-switch", NULL, in usb_role_switch_get()
172 WARN_ON(!try_module_get(sw->module)); in usb_role_switch_get()
179 * fwnode_usb_role_switch_get - Find USB role switch linked with the caller
183 * the switch using fwnode instead of device entry.
191 sw = fwnode_connection_find_match(fwnode, "usb-role-switch", in fwnode_usb_role_switch_get()
194 WARN_ON(!try_module_get(sw->module)); in fwnode_usb_role_switch_get()
201 * usb_role_switch_put - Release handle to a switch
202 * @sw: USB Role Switch
209 module_put(sw->module); in usb_role_switch_put()
210 put_device(&sw->dev); in usb_role_switch_put()
216 * usb_role_switch_find_by_fwnode - Find USB role switch with its fwnode
217 * @fwnode: fwnode of the USB Role Switch
219 * Finds and returns role switch with @fwnode. The reference count for the
220 * found switch is incremented.
234 WARN_ON(!try_module_get(sw->module)); in usb_role_switch_find_by_fwnode()
247 if (sw->allow_userspace_control) in usb_role_switch_is_visible()
248 return attr->mode; in usb_role_switch_is_visible()
259 const char *usb_role_string(enum usb_role role) in usb_role_string() argument
261 if (role < 0 || role >= ARRAY_SIZE(usb_roles)) in usb_role_string()
264 return usb_roles[role]; in usb_role_string()
272 enum usb_role role = usb_role_switch_get_role(sw); in role_show() local
274 return sprintf(buf, "%s\n", usb_roles[role]); in role_show()
287 /* Extra check if the user wants to disable the switch */ in role_store()
290 return -EINVAL; in role_store()
299 static DEVICE_ATTR_RW(role);
331 mutex_destroy(&sw->lock); in usb_role_switch_release()
332 lockdep_unregister_key(&sw->key); in usb_role_switch_release()
344 * usb_role_switch_register - Register USB Role Switch
345 * @parent: Parent device for the switch
346 * @desc: Description of the switch
348 * USB Role Switch is a device capable or choosing the role for USB connector.
349 * On platforms where the USB controller is dual-role capable, the controller
350 * driver will need to register the switch. On platforms where the USB host and
351 * USB device controllers behind the connector are separate, there will be a
352 * mux, and the driver for that mux will need to register the switch.
354 * Returns handle to a new role switch or ERR_PTR. The content of @desc is
364 if (!desc || !desc->set) in usb_role_switch_register()
365 return ERR_PTR(-EINVAL); in usb_role_switch_register()
369 return ERR_PTR(-ENOMEM); in usb_role_switch_register()
371 lockdep_register_key(&sw->key); in usb_role_switch_register()
372 mutex_init_with_key(&sw->lock, &sw->key); in usb_role_switch_register()
374 sw->allow_userspace_control = desc->allow_userspace_control; in usb_role_switch_register()
375 sw->usb2_port = desc->usb2_port; in usb_role_switch_register()
376 sw->usb3_port = desc->usb3_port; in usb_role_switch_register()
377 sw->udc = desc->udc; in usb_role_switch_register()
378 sw->set = desc->set; in usb_role_switch_register()
379 sw->get = desc->get; in usb_role_switch_register()
381 sw->module = parent->driver->owner; in usb_role_switch_register()
382 sw->dev.parent = parent; in usb_role_switch_register()
383 sw->dev.fwnode = desc->fwnode; in usb_role_switch_register()
384 sw->dev.class = &role_class; in usb_role_switch_register()
385 sw->dev.type = &usb_role_dev_type; in usb_role_switch_register()
386 dev_set_drvdata(&sw->dev, desc->driver_data); in usb_role_switch_register()
387 dev_set_name(&sw->dev, "%s-role-switch", in usb_role_switch_register()
388 desc->name ? desc->name : dev_name(parent)); in usb_role_switch_register()
390 sw->registered = true; in usb_role_switch_register()
392 ret = device_register(&sw->dev); in usb_role_switch_register()
394 sw->registered = false; in usb_role_switch_register()
395 put_device(&sw->dev); in usb_role_switch_register()
399 if (dev_fwnode(&sw->dev)) { in usb_role_switch_register()
400 ret = component_add(&sw->dev, &connector_ops); in usb_role_switch_register()
402 dev_warn(&sw->dev, "failed to add component\n"); in usb_role_switch_register()
412 * usb_role_switch_unregister - Unregsiter USB Role Switch
413 * @sw: USB Role Switch
415 * Unregister switch that was registered with usb_role_switch_register().
421 sw->registered = false; in usb_role_switch_unregister()
422 if (dev_fwnode(&sw->dev)) in usb_role_switch_unregister()
423 component_del(&sw->dev, &connector_ops); in usb_role_switch_unregister()
424 device_unregister(&sw->dev); in usb_role_switch_unregister()
429 * usb_role_switch_set_drvdata - Assign private data pointer to a switch
430 * @sw: USB Role Switch
435 dev_set_drvdata(&sw->dev, data); in usb_role_switch_set_drvdata()
440 * usb_role_switch_get_drvdata - Get the private data pointer of a switch
441 * @sw: USB Role Switch
445 return dev_get_drvdata(&sw->dev); in usb_role_switch_get_drvdata()
464 MODULE_DESCRIPTION("USB Role Class");