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 usb_role_switch *sw,
18 typedef enum usb_role (*usb_role_switch_get_t)(struct usb_role_switch *sw);
21 * struct usb_role_switch_desc - USB Role Switch Descriptor
22 * @fwnode: The device node to be associated with the role switch
23 * @usb2_port: Optional reference to the host controller port device (USB2)
24 * @usb3_port: Optional reference to the host controller port device (USB3)
25 * @udc: Optional reference to the peripheral controller device
26 * @set: Callback for setting the role
27 * @get: Callback for getting the role (optional)
28 * @allow_userspace_control: If true userspace may change the role through sysfs
29 * @driver_data: Private data pointer
30 * @name: Name for the switch (optional)
32 * @usb2_port and @usb3_port will point to the USB host port and @udc to the USB
33 * device controller behind the USB connector with the role switch. If
34 * @usb2_port, @usb3_port and @udc are included in the description, the
35 * reference count for them should be incremented by the caller of
36 * usb_role_switch_register() before registering the switch.
38 struct usb_role_switch_desc {
39 struct fwnode_handle *fwnode;
40 struct device *usb2_port;
41 struct device *usb3_port;
43 usb_role_switch_set_t set;
44 usb_role_switch_get_t get;
45 bool allow_userspace_control;
51 #if IS_ENABLED(CONFIG_USB_ROLE_SWITCH)
52 int usb_role_switch_set_role(struct usb_role_switch *sw, enum usb_role role);
53 enum usb_role usb_role_switch_get_role(struct usb_role_switch *sw);
54 struct usb_role_switch *usb_role_switch_get(struct device *dev);
55 struct usb_role_switch *fwnode_usb_role_switch_get(struct fwnode_handle *node);
56 void usb_role_switch_put(struct usb_role_switch *sw);
58 struct usb_role_switch *
59 usb_role_switch_find_by_fwnode(const struct fwnode_handle *fwnode);
61 struct usb_role_switch *
62 usb_role_switch_register(struct device *parent,
63 const struct usb_role_switch_desc *desc);
64 void usb_role_switch_unregister(struct usb_role_switch *sw);
66 void usb_role_switch_set_drvdata(struct usb_role_switch *sw, void *data);
67 void *usb_role_switch_get_drvdata(struct usb_role_switch *sw);
68 const char *usb_role_string(enum usb_role role);
70 static inline int usb_role_switch_set_role(struct usb_role_switch *sw,
76 static inline enum usb_role usb_role_switch_get_role(struct usb_role_switch *sw)
81 static inline struct usb_role_switch *usb_role_switch_get(struct device *dev)
83 return ERR_PTR(-ENODEV);
86 static inline struct usb_role_switch *
87 fwnode_usb_role_switch_get(struct fwnode_handle *node)
89 return ERR_PTR(-ENODEV);
92 static inline void usb_role_switch_put(struct usb_role_switch *sw) { }
94 static inline struct usb_role_switch *
95 usb_role_switch_register(struct device *parent,
96 const struct usb_role_switch_desc *desc)
98 return ERR_PTR(-ENODEV);
101 static inline void usb_role_switch_unregister(struct usb_role_switch *sw) { }
104 usb_role_switch_set_drvdata(struct usb_role_switch *sw, void *data)
108 static inline void *usb_role_switch_get_drvdata(struct usb_role_switch *sw)
113 static inline const char *usb_role_string(enum usb_role role)
120 #endif /* __LINUX_USB_ROLE_H */