18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * USB Role Switch Support
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2018 Intel Corporation
68c2ecf20Sopenharmony_ci * Author: Heikki Krogerus <heikki.krogerus@linux.intel.com>
78c2ecf20Sopenharmony_ci *         Hans de Goede <hdegoede@redhat.com>
88c2ecf20Sopenharmony_ci */
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#include <linux/usb/role.h>
118c2ecf20Sopenharmony_ci#include <linux/property.h>
128c2ecf20Sopenharmony_ci#include <linux/device.h>
138c2ecf20Sopenharmony_ci#include <linux/module.h>
148c2ecf20Sopenharmony_ci#include <linux/mutex.h>
158c2ecf20Sopenharmony_ci#include <linux/slab.h>
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_cistatic struct class *role_class;
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_cistruct usb_role_switch {
208c2ecf20Sopenharmony_ci	struct device dev;
218c2ecf20Sopenharmony_ci	struct mutex lock; /* device lock*/
228c2ecf20Sopenharmony_ci	struct module *module; /* the module this device depends on */
238c2ecf20Sopenharmony_ci	enum usb_role role;
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci	/* From descriptor */
268c2ecf20Sopenharmony_ci	struct device *usb2_port;
278c2ecf20Sopenharmony_ci	struct device *usb3_port;
288c2ecf20Sopenharmony_ci	struct device *udc;
298c2ecf20Sopenharmony_ci	usb_role_switch_set_t set;
308c2ecf20Sopenharmony_ci	usb_role_switch_get_t get;
318c2ecf20Sopenharmony_ci	bool allow_userspace_control;
328c2ecf20Sopenharmony_ci};
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci#define to_role_switch(d)	container_of(d, struct usb_role_switch, dev)
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci/**
378c2ecf20Sopenharmony_ci * usb_role_switch_set_role - Set USB role for a switch
388c2ecf20Sopenharmony_ci * @sw: USB role switch
398c2ecf20Sopenharmony_ci * @role: USB role to be switched to
408c2ecf20Sopenharmony_ci *
418c2ecf20Sopenharmony_ci * Set USB role @role for @sw.
428c2ecf20Sopenharmony_ci */
438c2ecf20Sopenharmony_ciint usb_role_switch_set_role(struct usb_role_switch *sw, enum usb_role role)
448c2ecf20Sopenharmony_ci{
458c2ecf20Sopenharmony_ci	int ret;
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci	if (IS_ERR_OR_NULL(sw))
488c2ecf20Sopenharmony_ci		return 0;
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci	mutex_lock(&sw->lock);
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci	ret = sw->set(sw, role);
538c2ecf20Sopenharmony_ci	if (!ret) {
548c2ecf20Sopenharmony_ci		sw->role = role;
558c2ecf20Sopenharmony_ci		kobject_uevent(&sw->dev.kobj, KOBJ_CHANGE);
568c2ecf20Sopenharmony_ci	}
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci	mutex_unlock(&sw->lock);
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci	return ret;
618c2ecf20Sopenharmony_ci}
628c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_role_switch_set_role);
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci/**
658c2ecf20Sopenharmony_ci * usb_role_switch_get_role - Get the USB role for a switch
668c2ecf20Sopenharmony_ci * @sw: USB role switch
678c2ecf20Sopenharmony_ci *
688c2ecf20Sopenharmony_ci * Depending on the role-switch-driver this function returns either a cached
698c2ecf20Sopenharmony_ci * value of the last set role, or reads back the actual value from the hardware.
708c2ecf20Sopenharmony_ci */
718c2ecf20Sopenharmony_cienum usb_role usb_role_switch_get_role(struct usb_role_switch *sw)
728c2ecf20Sopenharmony_ci{
738c2ecf20Sopenharmony_ci	enum usb_role role;
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci	if (IS_ERR_OR_NULL(sw))
768c2ecf20Sopenharmony_ci		return USB_ROLE_NONE;
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci	mutex_lock(&sw->lock);
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci	if (sw->get)
818c2ecf20Sopenharmony_ci		role = sw->get(sw);
828c2ecf20Sopenharmony_ci	else
838c2ecf20Sopenharmony_ci		role = sw->role;
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci	mutex_unlock(&sw->lock);
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci	return role;
888c2ecf20Sopenharmony_ci}
898c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_role_switch_get_role);
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_cistatic void *usb_role_switch_match(struct fwnode_handle *fwnode, const char *id,
928c2ecf20Sopenharmony_ci				   void *data)
938c2ecf20Sopenharmony_ci{
948c2ecf20Sopenharmony_ci	struct device *dev;
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci	if (id && !fwnode_property_present(fwnode, id))
978c2ecf20Sopenharmony_ci		return NULL;
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ci	dev = class_find_device_by_fwnode(role_class, fwnode);
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci	return dev ? to_role_switch(dev) : ERR_PTR(-EPROBE_DEFER);
1028c2ecf20Sopenharmony_ci}
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_cistatic struct usb_role_switch *
1058c2ecf20Sopenharmony_ciusb_role_switch_is_parent(struct fwnode_handle *fwnode)
1068c2ecf20Sopenharmony_ci{
1078c2ecf20Sopenharmony_ci	struct fwnode_handle *parent = fwnode_get_parent(fwnode);
1088c2ecf20Sopenharmony_ci	struct device *dev;
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci	if (!fwnode_property_present(parent, "usb-role-switch")) {
1118c2ecf20Sopenharmony_ci		fwnode_handle_put(parent);
1128c2ecf20Sopenharmony_ci		return NULL;
1138c2ecf20Sopenharmony_ci	}
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ci	dev = class_find_device_by_fwnode(role_class, parent);
1168c2ecf20Sopenharmony_ci	fwnode_handle_put(parent);
1178c2ecf20Sopenharmony_ci	return dev ? to_role_switch(dev) : ERR_PTR(-EPROBE_DEFER);
1188c2ecf20Sopenharmony_ci}
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci/**
1218c2ecf20Sopenharmony_ci * usb_role_switch_get - Find USB role switch linked with the caller
1228c2ecf20Sopenharmony_ci * @dev: The caller device
1238c2ecf20Sopenharmony_ci *
1248c2ecf20Sopenharmony_ci * Finds and returns role switch linked with @dev. The reference count for the
1258c2ecf20Sopenharmony_ci * found switch is incremented.
1268c2ecf20Sopenharmony_ci */
1278c2ecf20Sopenharmony_cistruct usb_role_switch *usb_role_switch_get(struct device *dev)
1288c2ecf20Sopenharmony_ci{
1298c2ecf20Sopenharmony_ci	struct usb_role_switch *sw;
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ci	sw = usb_role_switch_is_parent(dev_fwnode(dev));
1328c2ecf20Sopenharmony_ci	if (!sw)
1338c2ecf20Sopenharmony_ci		sw = device_connection_find_match(dev, "usb-role-switch", NULL,
1348c2ecf20Sopenharmony_ci						  usb_role_switch_match);
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ci	if (!IS_ERR_OR_NULL(sw))
1378c2ecf20Sopenharmony_ci		WARN_ON(!try_module_get(sw->module));
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci	return sw;
1408c2ecf20Sopenharmony_ci}
1418c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_role_switch_get);
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci/**
1448c2ecf20Sopenharmony_ci * fwnode_usb_role_switch_get - Find USB role switch linked with the caller
1458c2ecf20Sopenharmony_ci * @fwnode: The caller device node
1468c2ecf20Sopenharmony_ci *
1478c2ecf20Sopenharmony_ci * This is similar to the usb_role_switch_get() function above, but it searches
1488c2ecf20Sopenharmony_ci * the switch using fwnode instead of device entry.
1498c2ecf20Sopenharmony_ci */
1508c2ecf20Sopenharmony_cistruct usb_role_switch *fwnode_usb_role_switch_get(struct fwnode_handle *fwnode)
1518c2ecf20Sopenharmony_ci{
1528c2ecf20Sopenharmony_ci	struct usb_role_switch *sw;
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_ci	sw = usb_role_switch_is_parent(fwnode);
1558c2ecf20Sopenharmony_ci	if (!sw)
1568c2ecf20Sopenharmony_ci		sw = fwnode_connection_find_match(fwnode, "usb-role-switch",
1578c2ecf20Sopenharmony_ci						  NULL, usb_role_switch_match);
1588c2ecf20Sopenharmony_ci	if (!IS_ERR_OR_NULL(sw))
1598c2ecf20Sopenharmony_ci		WARN_ON(!try_module_get(sw->module));
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_ci	return sw;
1628c2ecf20Sopenharmony_ci}
1638c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(fwnode_usb_role_switch_get);
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ci/**
1668c2ecf20Sopenharmony_ci * usb_role_switch_put - Release handle to a switch
1678c2ecf20Sopenharmony_ci * @sw: USB Role Switch
1688c2ecf20Sopenharmony_ci *
1698c2ecf20Sopenharmony_ci * Decrement reference count for @sw.
1708c2ecf20Sopenharmony_ci */
1718c2ecf20Sopenharmony_civoid usb_role_switch_put(struct usb_role_switch *sw)
1728c2ecf20Sopenharmony_ci{
1738c2ecf20Sopenharmony_ci	if (!IS_ERR_OR_NULL(sw)) {
1748c2ecf20Sopenharmony_ci		module_put(sw->module);
1758c2ecf20Sopenharmony_ci		put_device(&sw->dev);
1768c2ecf20Sopenharmony_ci	}
1778c2ecf20Sopenharmony_ci}
1788c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_role_switch_put);
1798c2ecf20Sopenharmony_ci
1808c2ecf20Sopenharmony_ci/**
1818c2ecf20Sopenharmony_ci * usb_role_switch_find_by_fwnode - Find USB role switch with its fwnode
1828c2ecf20Sopenharmony_ci * @fwnode: fwnode of the USB Role Switch
1838c2ecf20Sopenharmony_ci *
1848c2ecf20Sopenharmony_ci * Finds and returns role switch with @fwnode. The reference count for the
1858c2ecf20Sopenharmony_ci * found switch is incremented.
1868c2ecf20Sopenharmony_ci */
1878c2ecf20Sopenharmony_cistruct usb_role_switch *
1888c2ecf20Sopenharmony_ciusb_role_switch_find_by_fwnode(const struct fwnode_handle *fwnode)
1898c2ecf20Sopenharmony_ci{
1908c2ecf20Sopenharmony_ci	struct device *dev;
1918c2ecf20Sopenharmony_ci	struct usb_role_switch *sw = NULL;
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_ci	if (!fwnode)
1948c2ecf20Sopenharmony_ci		return NULL;
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_ci	dev = class_find_device_by_fwnode(role_class, fwnode);
1978c2ecf20Sopenharmony_ci	if (dev) {
1988c2ecf20Sopenharmony_ci		sw = to_role_switch(dev);
1998c2ecf20Sopenharmony_ci		WARN_ON(!try_module_get(sw->module));
2008c2ecf20Sopenharmony_ci	}
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_ci	return sw;
2038c2ecf20Sopenharmony_ci}
2048c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_role_switch_find_by_fwnode);
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_cistatic umode_t
2078c2ecf20Sopenharmony_ciusb_role_switch_is_visible(struct kobject *kobj, struct attribute *attr, int n)
2088c2ecf20Sopenharmony_ci{
2098c2ecf20Sopenharmony_ci	struct device *dev = kobj_to_dev(kobj);
2108c2ecf20Sopenharmony_ci	struct usb_role_switch *sw = to_role_switch(dev);
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_ci	if (sw->allow_userspace_control)
2138c2ecf20Sopenharmony_ci		return attr->mode;
2148c2ecf20Sopenharmony_ci
2158c2ecf20Sopenharmony_ci	return 0;
2168c2ecf20Sopenharmony_ci}
2178c2ecf20Sopenharmony_ci
2188c2ecf20Sopenharmony_cistatic const char * const usb_roles[] = {
2198c2ecf20Sopenharmony_ci	[USB_ROLE_NONE]		= "none",
2208c2ecf20Sopenharmony_ci	[USB_ROLE_HOST]		= "host",
2218c2ecf20Sopenharmony_ci	[USB_ROLE_DEVICE]	= "device",
2228c2ecf20Sopenharmony_ci};
2238c2ecf20Sopenharmony_ci
2248c2ecf20Sopenharmony_cistatic ssize_t
2258c2ecf20Sopenharmony_cirole_show(struct device *dev, struct device_attribute *attr, char *buf)
2268c2ecf20Sopenharmony_ci{
2278c2ecf20Sopenharmony_ci	struct usb_role_switch *sw = to_role_switch(dev);
2288c2ecf20Sopenharmony_ci	enum usb_role role = usb_role_switch_get_role(sw);
2298c2ecf20Sopenharmony_ci
2308c2ecf20Sopenharmony_ci	return sprintf(buf, "%s\n", usb_roles[role]);
2318c2ecf20Sopenharmony_ci}
2328c2ecf20Sopenharmony_ci
2338c2ecf20Sopenharmony_cistatic ssize_t role_store(struct device *dev, struct device_attribute *attr,
2348c2ecf20Sopenharmony_ci			  const char *buf, size_t size)
2358c2ecf20Sopenharmony_ci{
2368c2ecf20Sopenharmony_ci	struct usb_role_switch *sw = to_role_switch(dev);
2378c2ecf20Sopenharmony_ci	int ret;
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_ci	ret = sysfs_match_string(usb_roles, buf);
2408c2ecf20Sopenharmony_ci	if (ret < 0) {
2418c2ecf20Sopenharmony_ci		bool res;
2428c2ecf20Sopenharmony_ci
2438c2ecf20Sopenharmony_ci		/* Extra check if the user wants to disable the switch */
2448c2ecf20Sopenharmony_ci		ret = kstrtobool(buf, &res);
2458c2ecf20Sopenharmony_ci		if (ret || res)
2468c2ecf20Sopenharmony_ci			return -EINVAL;
2478c2ecf20Sopenharmony_ci	}
2488c2ecf20Sopenharmony_ci
2498c2ecf20Sopenharmony_ci	ret = usb_role_switch_set_role(sw, ret);
2508c2ecf20Sopenharmony_ci	if (ret)
2518c2ecf20Sopenharmony_ci		return ret;
2528c2ecf20Sopenharmony_ci
2538c2ecf20Sopenharmony_ci	return size;
2548c2ecf20Sopenharmony_ci}
2558c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RW(role);
2568c2ecf20Sopenharmony_ci
2578c2ecf20Sopenharmony_cistatic struct attribute *usb_role_switch_attrs[] = {
2588c2ecf20Sopenharmony_ci	&dev_attr_role.attr,
2598c2ecf20Sopenharmony_ci	NULL,
2608c2ecf20Sopenharmony_ci};
2618c2ecf20Sopenharmony_ci
2628c2ecf20Sopenharmony_cistatic const struct attribute_group usb_role_switch_group = {
2638c2ecf20Sopenharmony_ci	.is_visible = usb_role_switch_is_visible,
2648c2ecf20Sopenharmony_ci	.attrs = usb_role_switch_attrs,
2658c2ecf20Sopenharmony_ci};
2668c2ecf20Sopenharmony_ci
2678c2ecf20Sopenharmony_cistatic const struct attribute_group *usb_role_switch_groups[] = {
2688c2ecf20Sopenharmony_ci	&usb_role_switch_group,
2698c2ecf20Sopenharmony_ci	NULL,
2708c2ecf20Sopenharmony_ci};
2718c2ecf20Sopenharmony_ci
2728c2ecf20Sopenharmony_cistatic int
2738c2ecf20Sopenharmony_ciusb_role_switch_uevent(struct device *dev, struct kobj_uevent_env *env)
2748c2ecf20Sopenharmony_ci{
2758c2ecf20Sopenharmony_ci	int ret;
2768c2ecf20Sopenharmony_ci
2778c2ecf20Sopenharmony_ci	ret = add_uevent_var(env, "USB_ROLE_SWITCH=%s", dev_name(dev));
2788c2ecf20Sopenharmony_ci	if (ret)
2798c2ecf20Sopenharmony_ci		dev_err(dev, "failed to add uevent USB_ROLE_SWITCH\n");
2808c2ecf20Sopenharmony_ci
2818c2ecf20Sopenharmony_ci	return ret;
2828c2ecf20Sopenharmony_ci}
2838c2ecf20Sopenharmony_ci
2848c2ecf20Sopenharmony_cistatic void usb_role_switch_release(struct device *dev)
2858c2ecf20Sopenharmony_ci{
2868c2ecf20Sopenharmony_ci	struct usb_role_switch *sw = to_role_switch(dev);
2878c2ecf20Sopenharmony_ci
2888c2ecf20Sopenharmony_ci	kfree(sw);
2898c2ecf20Sopenharmony_ci}
2908c2ecf20Sopenharmony_ci
2918c2ecf20Sopenharmony_cistatic const struct device_type usb_role_dev_type = {
2928c2ecf20Sopenharmony_ci	.name = "usb_role_switch",
2938c2ecf20Sopenharmony_ci	.groups = usb_role_switch_groups,
2948c2ecf20Sopenharmony_ci	.uevent = usb_role_switch_uevent,
2958c2ecf20Sopenharmony_ci	.release = usb_role_switch_release,
2968c2ecf20Sopenharmony_ci};
2978c2ecf20Sopenharmony_ci
2988c2ecf20Sopenharmony_ci/**
2998c2ecf20Sopenharmony_ci * usb_role_switch_register - Register USB Role Switch
3008c2ecf20Sopenharmony_ci * @parent: Parent device for the switch
3018c2ecf20Sopenharmony_ci * @desc: Description of the switch
3028c2ecf20Sopenharmony_ci *
3038c2ecf20Sopenharmony_ci * USB Role Switch is a device capable or choosing the role for USB connector.
3048c2ecf20Sopenharmony_ci * On platforms where the USB controller is dual-role capable, the controller
3058c2ecf20Sopenharmony_ci * driver will need to register the switch. On platforms where the USB host and
3068c2ecf20Sopenharmony_ci * USB device controllers behind the connector are separate, there will be a
3078c2ecf20Sopenharmony_ci * mux, and the driver for that mux will need to register the switch.
3088c2ecf20Sopenharmony_ci *
3098c2ecf20Sopenharmony_ci * Returns handle to a new role switch or ERR_PTR. The content of @desc is
3108c2ecf20Sopenharmony_ci * copied.
3118c2ecf20Sopenharmony_ci */
3128c2ecf20Sopenharmony_cistruct usb_role_switch *
3138c2ecf20Sopenharmony_ciusb_role_switch_register(struct device *parent,
3148c2ecf20Sopenharmony_ci			 const struct usb_role_switch_desc *desc)
3158c2ecf20Sopenharmony_ci{
3168c2ecf20Sopenharmony_ci	struct usb_role_switch *sw;
3178c2ecf20Sopenharmony_ci	int ret;
3188c2ecf20Sopenharmony_ci
3198c2ecf20Sopenharmony_ci	if (!desc || !desc->set)
3208c2ecf20Sopenharmony_ci		return ERR_PTR(-EINVAL);
3218c2ecf20Sopenharmony_ci
3228c2ecf20Sopenharmony_ci	sw = kzalloc(sizeof(*sw), GFP_KERNEL);
3238c2ecf20Sopenharmony_ci	if (!sw)
3248c2ecf20Sopenharmony_ci		return ERR_PTR(-ENOMEM);
3258c2ecf20Sopenharmony_ci
3268c2ecf20Sopenharmony_ci	mutex_init(&sw->lock);
3278c2ecf20Sopenharmony_ci
3288c2ecf20Sopenharmony_ci	sw->allow_userspace_control = desc->allow_userspace_control;
3298c2ecf20Sopenharmony_ci	sw->usb2_port = desc->usb2_port;
3308c2ecf20Sopenharmony_ci	sw->usb3_port = desc->usb3_port;
3318c2ecf20Sopenharmony_ci	sw->udc = desc->udc;
3328c2ecf20Sopenharmony_ci	sw->set = desc->set;
3338c2ecf20Sopenharmony_ci	sw->get = desc->get;
3348c2ecf20Sopenharmony_ci
3358c2ecf20Sopenharmony_ci	sw->module = parent->driver->owner;
3368c2ecf20Sopenharmony_ci	sw->dev.parent = parent;
3378c2ecf20Sopenharmony_ci	sw->dev.fwnode = desc->fwnode;
3388c2ecf20Sopenharmony_ci	sw->dev.class = role_class;
3398c2ecf20Sopenharmony_ci	sw->dev.type = &usb_role_dev_type;
3408c2ecf20Sopenharmony_ci	dev_set_drvdata(&sw->dev, desc->driver_data);
3418c2ecf20Sopenharmony_ci	dev_set_name(&sw->dev, "%s-role-switch",
3428c2ecf20Sopenharmony_ci		     desc->name ? desc->name : dev_name(parent));
3438c2ecf20Sopenharmony_ci
3448c2ecf20Sopenharmony_ci	ret = device_register(&sw->dev);
3458c2ecf20Sopenharmony_ci	if (ret) {
3468c2ecf20Sopenharmony_ci		put_device(&sw->dev);
3478c2ecf20Sopenharmony_ci		return ERR_PTR(ret);
3488c2ecf20Sopenharmony_ci	}
3498c2ecf20Sopenharmony_ci
3508c2ecf20Sopenharmony_ci	/* TODO: Symlinks for the host port and the device controller. */
3518c2ecf20Sopenharmony_ci
3528c2ecf20Sopenharmony_ci	return sw;
3538c2ecf20Sopenharmony_ci}
3548c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_role_switch_register);
3558c2ecf20Sopenharmony_ci
3568c2ecf20Sopenharmony_ci/**
3578c2ecf20Sopenharmony_ci * usb_role_switch_unregister - Unregsiter USB Role Switch
3588c2ecf20Sopenharmony_ci * @sw: USB Role Switch
3598c2ecf20Sopenharmony_ci *
3608c2ecf20Sopenharmony_ci * Unregister switch that was registered with usb_role_switch_register().
3618c2ecf20Sopenharmony_ci */
3628c2ecf20Sopenharmony_civoid usb_role_switch_unregister(struct usb_role_switch *sw)
3638c2ecf20Sopenharmony_ci{
3648c2ecf20Sopenharmony_ci	if (!IS_ERR_OR_NULL(sw))
3658c2ecf20Sopenharmony_ci		device_unregister(&sw->dev);
3668c2ecf20Sopenharmony_ci}
3678c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_role_switch_unregister);
3688c2ecf20Sopenharmony_ci
3698c2ecf20Sopenharmony_ci/**
3708c2ecf20Sopenharmony_ci * usb_role_switch_set_drvdata - Assign private data pointer to a switch
3718c2ecf20Sopenharmony_ci * @sw: USB Role Switch
3728c2ecf20Sopenharmony_ci * @data: Private data pointer
3738c2ecf20Sopenharmony_ci */
3748c2ecf20Sopenharmony_civoid usb_role_switch_set_drvdata(struct usb_role_switch *sw, void *data)
3758c2ecf20Sopenharmony_ci{
3768c2ecf20Sopenharmony_ci	dev_set_drvdata(&sw->dev, data);
3778c2ecf20Sopenharmony_ci}
3788c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_role_switch_set_drvdata);
3798c2ecf20Sopenharmony_ci
3808c2ecf20Sopenharmony_ci/**
3818c2ecf20Sopenharmony_ci * usb_role_switch_get_drvdata - Get the private data pointer of a switch
3828c2ecf20Sopenharmony_ci * @sw: USB Role Switch
3838c2ecf20Sopenharmony_ci */
3848c2ecf20Sopenharmony_civoid *usb_role_switch_get_drvdata(struct usb_role_switch *sw)
3858c2ecf20Sopenharmony_ci{
3868c2ecf20Sopenharmony_ci	return dev_get_drvdata(&sw->dev);
3878c2ecf20Sopenharmony_ci}
3888c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_role_switch_get_drvdata);
3898c2ecf20Sopenharmony_ci
3908c2ecf20Sopenharmony_cistatic int __init usb_roles_init(void)
3918c2ecf20Sopenharmony_ci{
3928c2ecf20Sopenharmony_ci	role_class = class_create(THIS_MODULE, "usb_role");
3938c2ecf20Sopenharmony_ci	return PTR_ERR_OR_ZERO(role_class);
3948c2ecf20Sopenharmony_ci}
3958c2ecf20Sopenharmony_cisubsys_initcall(usb_roles_init);
3968c2ecf20Sopenharmony_ci
3978c2ecf20Sopenharmony_cistatic void __exit usb_roles_exit(void)
3988c2ecf20Sopenharmony_ci{
3998c2ecf20Sopenharmony_ci	class_destroy(role_class);
4008c2ecf20Sopenharmony_ci}
4018c2ecf20Sopenharmony_cimodule_exit(usb_roles_exit);
4028c2ecf20Sopenharmony_ci
4038c2ecf20Sopenharmony_ciMODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>");
4048c2ecf20Sopenharmony_ciMODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
4058c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
4068c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("USB Role Class");
407