^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * USB Role Switch Support
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2018 Intel Corporation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Author: Heikki Krogerus <heikki.krogerus@linux.intel.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Hans de Goede <hdegoede@redhat.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/usb/role.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/property.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) static struct class *role_class;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) struct usb_role_switch {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) struct device dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) struct mutex lock; /* device lock*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) enum usb_role role;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) /* From descriptor */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) struct device *usb2_port;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) struct device *usb3_port;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) struct device *udc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) usb_role_switch_set_t set;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) usb_role_switch_get_t get;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) bool allow_userspace_control;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #define to_role_switch(d) container_of(d, struct usb_role_switch, dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) * usb_role_switch_set_role - Set USB role for a switch
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) * @sw: USB role switch
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) * @role: USB role to be switched to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) * Set USB role @role for @sw.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) int usb_role_switch_set_role(struct usb_role_switch *sw, enum usb_role role)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) if (IS_ERR_OR_NULL(sw))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) mutex_lock(&sw->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) ret = sw->set(sw, role);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) if (!ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) sw->role = role;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) kobject_uevent(&sw->dev.kobj, KOBJ_CHANGE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) mutex_unlock(&sw->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) EXPORT_SYMBOL_GPL(usb_role_switch_set_role);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) * usb_role_switch_get_role - Get the USB role for a switch
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) * @sw: USB role switch
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) * Depending on the role-switch-driver this function returns either a cached
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) * value of the last set role, or reads back the actual value from the hardware.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) enum usb_role usb_role_switch_get_role(struct usb_role_switch *sw)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) enum usb_role role;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) if (IS_ERR_OR_NULL(sw))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) return USB_ROLE_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) mutex_lock(&sw->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) if (sw->get)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) role = sw->get(sw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) role = sw->role;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) mutex_unlock(&sw->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) return role;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) EXPORT_SYMBOL_GPL(usb_role_switch_get_role);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) static void *usb_role_switch_match(struct fwnode_handle *fwnode, const char *id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) if (id && !fwnode_property_present(fwnode, id))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) dev = class_find_device_by_fwnode(role_class, fwnode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) return dev ? to_role_switch(dev) : ERR_PTR(-EPROBE_DEFER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) static struct usb_role_switch *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) usb_role_switch_is_parent(struct fwnode_handle *fwnode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) struct fwnode_handle *parent = fwnode_get_parent(fwnode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) if (!parent || !fwnode_property_present(parent, "usb-role-switch"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) dev = class_find_device_by_fwnode(role_class, parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) return dev ? to_role_switch(dev) : ERR_PTR(-EPROBE_DEFER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) * usb_role_switch_get - Find USB role switch linked with the caller
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) * @dev: The caller device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) * Finds and returns role switch linked with @dev. The reference count for the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) * found switch is incremented.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) struct usb_role_switch *usb_role_switch_get(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) struct usb_role_switch *sw;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) sw = usb_role_switch_is_parent(dev_fwnode(dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) if (!sw)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) sw = device_connection_find_match(dev, "usb-role-switch", NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) usb_role_switch_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) if (!IS_ERR_OR_NULL(sw))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) WARN_ON(!try_module_get(sw->dev.parent->driver->owner));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) return sw;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) EXPORT_SYMBOL_GPL(usb_role_switch_get);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) * fwnode_usb_role_switch_get - Find USB role switch linked with the caller
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) * @fwnode: The caller device node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) * This is similar to the usb_role_switch_get() function above, but it searches
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) * the switch using fwnode instead of device entry.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) struct usb_role_switch *fwnode_usb_role_switch_get(struct fwnode_handle *fwnode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) struct usb_role_switch *sw;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) sw = usb_role_switch_is_parent(fwnode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) if (!sw)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) sw = fwnode_connection_find_match(fwnode, "usb-role-switch",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) NULL, usb_role_switch_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) if (!IS_ERR_OR_NULL(sw))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) WARN_ON(!try_module_get(sw->dev.parent->driver->owner));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) return sw;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) EXPORT_SYMBOL_GPL(fwnode_usb_role_switch_get);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) * usb_role_switch_put - Release handle to a switch
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) * @sw: USB Role Switch
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) * Decrement reference count for @sw.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) void usb_role_switch_put(struct usb_role_switch *sw)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) if (!IS_ERR_OR_NULL(sw)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) module_put(sw->dev.parent->driver->owner);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) put_device(&sw->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) EXPORT_SYMBOL_GPL(usb_role_switch_put);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) * usb_role_switch_find_by_fwnode - Find USB role switch with its fwnode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) * @fwnode: fwnode of the USB Role Switch
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) * Finds and returns role switch with @fwnode. The reference count for the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) * found switch is incremented.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) struct usb_role_switch *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) usb_role_switch_find_by_fwnode(const struct fwnode_handle *fwnode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) if (!fwnode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) dev = class_find_device_by_fwnode(role_class, fwnode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) if (dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) WARN_ON(!try_module_get(dev->parent->driver->owner));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) return dev ? to_role_switch(dev) : NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) EXPORT_SYMBOL_GPL(usb_role_switch_find_by_fwnode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) static umode_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) usb_role_switch_is_visible(struct kobject *kobj, struct attribute *attr, int n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) struct device *dev = kobj_to_dev(kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) struct usb_role_switch *sw = to_role_switch(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) if (sw->allow_userspace_control)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) return attr->mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) static const char * const usb_roles[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) [USB_ROLE_NONE] = "none",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) [USB_ROLE_HOST] = "host",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) [USB_ROLE_DEVICE] = "device",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) role_show(struct device *dev, struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) struct usb_role_switch *sw = to_role_switch(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) enum usb_role role = usb_role_switch_get_role(sw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) return sprintf(buf, "%s\n", usb_roles[role]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) static ssize_t role_store(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) const char *buf, size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) struct usb_role_switch *sw = to_role_switch(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) ret = sysfs_match_string(usb_roles, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) bool res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) /* Extra check if the user wants to disable the switch */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) ret = kstrtobool(buf, &res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) if (ret || res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) ret = usb_role_switch_set_role(sw, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) return size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) static DEVICE_ATTR_RW(role);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) static struct attribute *usb_role_switch_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) &dev_attr_role.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) static const struct attribute_group usb_role_switch_group = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) .is_visible = usb_role_switch_is_visible,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) .attrs = usb_role_switch_attrs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) static const struct attribute_group *usb_role_switch_groups[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) &usb_role_switch_group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) usb_role_switch_uevent(struct device *dev, struct kobj_uevent_env *env)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) ret = add_uevent_var(env, "USB_ROLE_SWITCH=%s", dev_name(dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) dev_err(dev, "failed to add uevent USB_ROLE_SWITCH\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) static void usb_role_switch_release(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) struct usb_role_switch *sw = to_role_switch(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) kfree(sw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) static const struct device_type usb_role_dev_type = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) .name = "usb_role_switch",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) .groups = usb_role_switch_groups,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) .uevent = usb_role_switch_uevent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) .release = usb_role_switch_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) * usb_role_switch_register - Register USB Role Switch
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) * @parent: Parent device for the switch
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) * @desc: Description of the switch
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) * USB Role Switch is a device capable or choosing the role for USB connector.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) * On platforms where the USB controller is dual-role capable, the controller
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) * driver will need to register the switch. On platforms where the USB host and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) * USB device controllers behind the connector are separate, there will be a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) * mux, and the driver for that mux will need to register the switch.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) * Returns handle to a new role switch or ERR_PTR. The content of @desc is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) * copied.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) struct usb_role_switch *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) usb_role_switch_register(struct device *parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) const struct usb_role_switch_desc *desc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) struct usb_role_switch *sw;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) if (!desc || !desc->set)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) sw = kzalloc(sizeof(*sw), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) if (!sw)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) mutex_init(&sw->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) sw->allow_userspace_control = desc->allow_userspace_control;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) sw->usb2_port = desc->usb2_port;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) sw->usb3_port = desc->usb3_port;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) sw->udc = desc->udc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) sw->set = desc->set;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) sw->get = desc->get;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) sw->dev.parent = parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) sw->dev.fwnode = desc->fwnode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) sw->dev.class = role_class;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) sw->dev.type = &usb_role_dev_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) dev_set_drvdata(&sw->dev, desc->driver_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) dev_set_name(&sw->dev, "%s-role-switch",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) desc->name ? desc->name : dev_name(parent));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) ret = device_register(&sw->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) put_device(&sw->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) return ERR_PTR(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) /* TODO: Symlinks for the host port and the device controller. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) return sw;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) EXPORT_SYMBOL_GPL(usb_role_switch_register);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) * usb_role_switch_unregister - Unregsiter USB Role Switch
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) * @sw: USB Role Switch
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) * Unregister switch that was registered with usb_role_switch_register().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) void usb_role_switch_unregister(struct usb_role_switch *sw)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) if (!IS_ERR_OR_NULL(sw))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) device_unregister(&sw->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) EXPORT_SYMBOL_GPL(usb_role_switch_unregister);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) * usb_role_switch_set_drvdata - Assign private data pointer to a switch
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) * @sw: USB Role Switch
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) * @data: Private data pointer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) void usb_role_switch_set_drvdata(struct usb_role_switch *sw, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) dev_set_drvdata(&sw->dev, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) EXPORT_SYMBOL_GPL(usb_role_switch_set_drvdata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) * usb_role_switch_get_drvdata - Get the private data pointer of a switch
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) * @sw: USB Role Switch
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) void *usb_role_switch_get_drvdata(struct usb_role_switch *sw)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) return dev_get_drvdata(&sw->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) EXPORT_SYMBOL_GPL(usb_role_switch_get_drvdata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) static int __init usb_roles_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) role_class = class_create(THIS_MODULE, "usb_role");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) return PTR_ERR_OR_ZERO(role_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) subsys_initcall(usb_roles_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) static void __exit usb_roles_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) class_destroy(role_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) module_exit(usb_roles_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) MODULE_LICENSE("GPL v2");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) MODULE_DESCRIPTION("USB Role Class");