Lines Matching +full:usb +full:- +full:a +full:- +full:connector

1 // SPDX-License-Identifier: GPL-2.0
3 * USB Role Switch Support
11 #include <linux/usb/role.h>
42 static int connector_bind(struct device *dev, struct device *connector, void *data) in connector_bind() argument
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()
57 static void connector_unbind(struct device *dev, struct device *connector, void *data) in connector_unbind() argument
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.
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
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()
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
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
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
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()
290 return -EINVAL; in role_store()
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
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
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 ret = device_register(&sw->dev); in usb_role_switch_register()
392 put_device(&sw->dev); in usb_role_switch_register()
396 if (dev_fwnode(&sw->dev)) { in usb_role_switch_register()
397 ret = component_add(&sw->dev, &connector_ops); in usb_role_switch_register()
399 dev_warn(&sw->dev, "failed to add component\n"); in usb_role_switch_register()
402 sw->registered = true; in usb_role_switch_register()
411 * usb_role_switch_unregister - Unregsiter USB Role Switch
412 * @sw: USB Role Switch
420 sw->registered = false; in usb_role_switch_unregister()
421 if (dev_fwnode(&sw->dev)) in usb_role_switch_unregister()
422 component_del(&sw->dev, &connector_ops); in usb_role_switch_unregister()
423 device_unregister(&sw->dev); in usb_role_switch_unregister()
428 * usb_role_switch_set_drvdata - Assign private data pointer to a switch
429 * @sw: USB Role Switch
434 dev_set_drvdata(&sw->dev, data); in usb_role_switch_set_drvdata()
439 * usb_role_switch_get_drvdata - Get the private data pointer of a switch
440 * @sw: USB Role Switch
444 return dev_get_drvdata(&sw->dev); in usb_role_switch_get_drvdata()
463 MODULE_DESCRIPTION("USB Role Class");