Lines Matching +full:usb +full:- +full:switch

1 // SPDX-License-Identifier: GPL-2.0
3 * USB Role Switch Support
10 #include <linux/usb/role.h>
36 * usb_role_switch_set_role - Set USB role for a switch
37 * @sw: USB role switch
38 * @role: USB role to be switched to
40 * Set USB role @role for @sw.
49 mutex_lock(&sw->lock); in usb_role_switch_set_role()
51 ret = sw->set(sw, role); in usb_role_switch_set_role()
53 sw->role = role; in usb_role_switch_set_role()
54 kobject_uevent(&sw->dev.kobj, KOBJ_CHANGE); in usb_role_switch_set_role()
57 mutex_unlock(&sw->lock); in usb_role_switch_set_role()
64 * usb_role_switch_get_role - Get the USB role for a switch
65 * @sw: USB role switch
67 * Depending on the role-switch-driver this function returns either a cached
77 mutex_lock(&sw->lock); in usb_role_switch_get_role()
79 if (sw->get) in usb_role_switch_get_role()
80 role = sw->get(sw); in usb_role_switch_get_role()
82 role = sw->role; in usb_role_switch_get_role()
84 mutex_unlock(&sw->lock); in usb_role_switch_get_role()
100 return dev ? to_role_switch(dev) : ERR_PTR(-EPROBE_DEFER); in usb_role_switch_match()
109 if (!parent || !fwnode_property_present(parent, "usb-role-switch")) in usb_role_switch_is_parent()
113 return dev ? to_role_switch(dev) : ERR_PTR(-EPROBE_DEFER); in usb_role_switch_is_parent()
117 * usb_role_switch_get - Find USB role switch linked with the caller
120 * Finds and returns role switch linked with @dev. The reference count for the
121 * found switch is incremented.
129 sw = device_connection_find_match(dev, "usb-role-switch", NULL, in usb_role_switch_get()
133 WARN_ON(!try_module_get(sw->dev.parent->driver->owner)); in usb_role_switch_get()
140 * fwnode_usb_role_switch_get - Find USB role switch linked with the caller
144 * the switch using fwnode instead of device entry.
152 sw = fwnode_connection_find_match(fwnode, "usb-role-switch", in fwnode_usb_role_switch_get()
155 WARN_ON(!try_module_get(sw->dev.parent->driver->owner)); in fwnode_usb_role_switch_get()
162 * usb_role_switch_put - Release handle to a switch
163 * @sw: USB Role Switch
170 module_put(sw->dev.parent->driver->owner); in usb_role_switch_put()
171 put_device(&sw->dev); in usb_role_switch_put()
177 * usb_role_switch_find_by_fwnode - Find USB role switch with its fwnode
178 * @fwnode: fwnode of the USB Role Switch
180 * Finds and returns role switch with @fwnode. The reference count for the
181 * found switch is incremented.
193 WARN_ON(!try_module_get(dev->parent->driver->owner)); in usb_role_switch_find_by_fwnode()
205 if (sw->allow_userspace_control) in usb_role_switch_is_visible()
206 return attr->mode; in usb_role_switch_is_visible()
245 /* Extra check if the user wants to disable the switch */ in role_store()
248 return -EINVAL; in role_store()
301 * usb_role_switch_register - Register USB Role Switch
302 * @parent: Parent device for the switch
303 * @desc: Description of the switch
305 * USB Role Switch is a device capable or choosing the role for USB connector.
306 * On platforms where the USB controller is dual-role capable, the controller
307 * driver will need to register the switch. On platforms where the USB host and
308 * USB device controllers behind the connector are separate, there will be a
309 * mux, and the driver for that mux will need to register the switch.
311 * Returns handle to a new role switch or ERR_PTR. The content of @desc is
321 if (!desc || !desc->set) in usb_role_switch_register()
322 return ERR_PTR(-EINVAL); in usb_role_switch_register()
326 return ERR_PTR(-ENOMEM); in usb_role_switch_register()
328 mutex_init(&sw->lock); in usb_role_switch_register()
330 sw->allow_userspace_control = desc->allow_userspace_control; in usb_role_switch_register()
331 sw->usb2_port = desc->usb2_port; in usb_role_switch_register()
332 sw->usb3_port = desc->usb3_port; in usb_role_switch_register()
333 sw->udc = desc->udc; in usb_role_switch_register()
334 sw->set = desc->set; in usb_role_switch_register()
335 sw->get = desc->get; in usb_role_switch_register()
337 sw->dev.parent = parent; in usb_role_switch_register()
338 sw->dev.fwnode = desc->fwnode; in usb_role_switch_register()
339 sw->dev.class = role_class; in usb_role_switch_register()
340 sw->dev.type = &usb_role_dev_type; in usb_role_switch_register()
341 dev_set_drvdata(&sw->dev, desc->driver_data); in usb_role_switch_register()
342 dev_set_name(&sw->dev, "%s-role-switch", in usb_role_switch_register()
343 desc->name ? desc->name : dev_name(parent)); in usb_role_switch_register()
345 ret = device_register(&sw->dev); in usb_role_switch_register()
347 put_device(&sw->dev); in usb_role_switch_register()
358 * usb_role_switch_unregister - Unregsiter USB Role Switch
359 * @sw: USB Role Switch
361 * Unregister switch that was registered with usb_role_switch_register().
366 device_unregister(&sw->dev); in usb_role_switch_unregister()
371 * usb_role_switch_set_drvdata - Assign private data pointer to a switch
372 * @sw: USB Role Switch
377 dev_set_drvdata(&sw->dev, data); in usb_role_switch_set_drvdata()
382 * usb_role_switch_get_drvdata - Get the private data pointer of a switch
383 * @sw: USB Role Switch
387 return dev_get_drvdata(&sw->dev); in usb_role_switch_get_drvdata()
407 MODULE_DESCRIPTION("USB Role Class");