1 // SPDX-License-Identifier: GPL-2.0
3 #ifndef __LINUX_USB_ROLE_H
4 #define __LINUX_USB_ROLE_H
6 #include <linux/device.h>
8 struct usb_role_switch;
16 typedef int (*usb_role_switch_set_t)(struct device *dev, enum usb_role role);
17 typedef enum usb_role (*usb_role_switch_get_t)(struct device *dev);
20 * struct usb_role_switch_desc - USB Role Switch Descriptor
21 * @fwnode: The device node to be associated with the role switch
22 * @usb2_port: Optional reference to the host controller port device (USB2)
23 * @usb3_port: Optional reference to the host controller port device (USB3)
24 * @udc: Optional reference to the peripheral controller device
25 * @set: Callback for setting the role
26 * @get: Callback for getting the role (optional)
27 * @allow_userspace_control: If true userspace may change the role through sysfs
29 * @usb2_port and @usb3_port will point to the USB host port and @udc to the USB
30 * device controller behind the USB connector with the role switch. If
31 * @usb2_port, @usb3_port and @udc are included in the description, the
32 * reference count for them should be incremented by the caller of
33 * usb_role_switch_register() before registering the switch.
35 struct usb_role_switch_desc {
36 struct fwnode_handle *fwnode;
37 struct device *usb2_port;
38 struct device *usb3_port;
40 usb_role_switch_set_t set;
41 usb_role_switch_get_t get;
42 bool allow_userspace_control;
46 #if IS_ENABLED(CONFIG_USB_ROLE_SWITCH)
47 int usb_role_switch_set_role(struct usb_role_switch *sw, enum usb_role role);
48 enum usb_role usb_role_switch_get_role(struct usb_role_switch *sw);
49 struct usb_role_switch *usb_role_switch_get(struct device *dev);
50 struct usb_role_switch *fwnode_usb_role_switch_get(struct fwnode_handle *node);
51 void usb_role_switch_put(struct usb_role_switch *sw);
53 struct usb_role_switch *
54 usb_role_switch_find_by_fwnode(const struct fwnode_handle *fwnode);
56 struct usb_role_switch *
57 usb_role_switch_register(struct device *parent,
58 const struct usb_role_switch_desc *desc);
59 void usb_role_switch_unregister(struct usb_role_switch *sw);
61 static inline int usb_role_switch_set_role(struct usb_role_switch *sw,
67 static inline enum usb_role usb_role_switch_get_role(struct usb_role_switch *sw)
72 static inline struct usb_role_switch *usb_role_switch_get(struct device *dev)
74 return ERR_PTR(-ENODEV);
77 static inline struct usb_role_switch *
78 fwnode_usb_role_switch_get(struct fwnode_handle *node)
80 return ERR_PTR(-ENODEV);
83 static inline void usb_role_switch_put(struct usb_role_switch *sw) { }
85 static inline struct usb_role_switch *
86 usb_role_switch_register(struct device *parent,
87 const struct usb_role_switch_desc *desc)
89 return ERR_PTR(-ENODEV);
92 static inline void usb_role_switch_unregister(struct usb_role_switch *sw) { }
95 #endif /* __LINUX_USB_ROLE_H */