1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun
3*4882a593Smuzhiyun #ifndef __LINUX_USB_ROLE_H
4*4882a593Smuzhiyun #define __LINUX_USB_ROLE_H
5*4882a593Smuzhiyun
6*4882a593Smuzhiyun #include <linux/device.h>
7*4882a593Smuzhiyun
8*4882a593Smuzhiyun struct usb_role_switch;
9*4882a593Smuzhiyun
10*4882a593Smuzhiyun enum usb_role {
11*4882a593Smuzhiyun USB_ROLE_NONE,
12*4882a593Smuzhiyun USB_ROLE_HOST,
13*4882a593Smuzhiyun USB_ROLE_DEVICE,
14*4882a593Smuzhiyun };
15*4882a593Smuzhiyun
16*4882a593Smuzhiyun typedef int (*usb_role_switch_set_t)(struct usb_role_switch *sw,
17*4882a593Smuzhiyun enum usb_role role);
18*4882a593Smuzhiyun typedef enum usb_role (*usb_role_switch_get_t)(struct usb_role_switch *sw);
19*4882a593Smuzhiyun
20*4882a593Smuzhiyun /**
21*4882a593Smuzhiyun * struct usb_role_switch_desc - USB Role Switch Descriptor
22*4882a593Smuzhiyun * @fwnode: The device node to be associated with the role switch
23*4882a593Smuzhiyun * @usb2_port: Optional reference to the host controller port device (USB2)
24*4882a593Smuzhiyun * @usb3_port: Optional reference to the host controller port device (USB3)
25*4882a593Smuzhiyun * @udc: Optional reference to the peripheral controller device
26*4882a593Smuzhiyun * @set: Callback for setting the role
27*4882a593Smuzhiyun * @get: Callback for getting the role (optional)
28*4882a593Smuzhiyun * @allow_userspace_control: If true userspace may change the role through sysfs
29*4882a593Smuzhiyun * @driver_data: Private data pointer
30*4882a593Smuzhiyun * @name: Name for the switch (optional)
31*4882a593Smuzhiyun *
32*4882a593Smuzhiyun * @usb2_port and @usb3_port will point to the USB host port and @udc to the USB
33*4882a593Smuzhiyun * device controller behind the USB connector with the role switch. If
34*4882a593Smuzhiyun * @usb2_port, @usb3_port and @udc are included in the description, the
35*4882a593Smuzhiyun * reference count for them should be incremented by the caller of
36*4882a593Smuzhiyun * usb_role_switch_register() before registering the switch.
37*4882a593Smuzhiyun */
38*4882a593Smuzhiyun struct usb_role_switch_desc {
39*4882a593Smuzhiyun struct fwnode_handle *fwnode;
40*4882a593Smuzhiyun struct device *usb2_port;
41*4882a593Smuzhiyun struct device *usb3_port;
42*4882a593Smuzhiyun struct device *udc;
43*4882a593Smuzhiyun usb_role_switch_set_t set;
44*4882a593Smuzhiyun usb_role_switch_get_t get;
45*4882a593Smuzhiyun bool allow_userspace_control;
46*4882a593Smuzhiyun void *driver_data;
47*4882a593Smuzhiyun const char *name;
48*4882a593Smuzhiyun };
49*4882a593Smuzhiyun
50*4882a593Smuzhiyun
51*4882a593Smuzhiyun #if IS_ENABLED(CONFIG_USB_ROLE_SWITCH)
52*4882a593Smuzhiyun int usb_role_switch_set_role(struct usb_role_switch *sw, enum usb_role role);
53*4882a593Smuzhiyun enum usb_role usb_role_switch_get_role(struct usb_role_switch *sw);
54*4882a593Smuzhiyun struct usb_role_switch *usb_role_switch_get(struct device *dev);
55*4882a593Smuzhiyun struct usb_role_switch *fwnode_usb_role_switch_get(struct fwnode_handle *node);
56*4882a593Smuzhiyun void usb_role_switch_put(struct usb_role_switch *sw);
57*4882a593Smuzhiyun
58*4882a593Smuzhiyun struct usb_role_switch *
59*4882a593Smuzhiyun usb_role_switch_find_by_fwnode(const struct fwnode_handle *fwnode);
60*4882a593Smuzhiyun
61*4882a593Smuzhiyun struct usb_role_switch *
62*4882a593Smuzhiyun usb_role_switch_register(struct device *parent,
63*4882a593Smuzhiyun const struct usb_role_switch_desc *desc);
64*4882a593Smuzhiyun void usb_role_switch_unregister(struct usb_role_switch *sw);
65*4882a593Smuzhiyun
66*4882a593Smuzhiyun void usb_role_switch_set_drvdata(struct usb_role_switch *sw, void *data);
67*4882a593Smuzhiyun void *usb_role_switch_get_drvdata(struct usb_role_switch *sw);
68*4882a593Smuzhiyun #else
usb_role_switch_set_role(struct usb_role_switch * sw,enum usb_role role)69*4882a593Smuzhiyun static inline int usb_role_switch_set_role(struct usb_role_switch *sw,
70*4882a593Smuzhiyun enum usb_role role)
71*4882a593Smuzhiyun {
72*4882a593Smuzhiyun return 0;
73*4882a593Smuzhiyun }
74*4882a593Smuzhiyun
usb_role_switch_get_role(struct usb_role_switch * sw)75*4882a593Smuzhiyun static inline enum usb_role usb_role_switch_get_role(struct usb_role_switch *sw)
76*4882a593Smuzhiyun {
77*4882a593Smuzhiyun return USB_ROLE_NONE;
78*4882a593Smuzhiyun }
79*4882a593Smuzhiyun
usb_role_switch_get(struct device * dev)80*4882a593Smuzhiyun static inline struct usb_role_switch *usb_role_switch_get(struct device *dev)
81*4882a593Smuzhiyun {
82*4882a593Smuzhiyun return ERR_PTR(-ENODEV);
83*4882a593Smuzhiyun }
84*4882a593Smuzhiyun
85*4882a593Smuzhiyun static inline struct usb_role_switch *
fwnode_usb_role_switch_get(struct fwnode_handle * node)86*4882a593Smuzhiyun fwnode_usb_role_switch_get(struct fwnode_handle *node)
87*4882a593Smuzhiyun {
88*4882a593Smuzhiyun return ERR_PTR(-ENODEV);
89*4882a593Smuzhiyun }
90*4882a593Smuzhiyun
usb_role_switch_put(struct usb_role_switch * sw)91*4882a593Smuzhiyun static inline void usb_role_switch_put(struct usb_role_switch *sw) { }
92*4882a593Smuzhiyun
93*4882a593Smuzhiyun static inline struct usb_role_switch *
usb_role_switch_find_by_fwnode(const struct fwnode_handle * fwnode)94*4882a593Smuzhiyun usb_role_switch_find_by_fwnode(const struct fwnode_handle *fwnode)
95*4882a593Smuzhiyun {
96*4882a593Smuzhiyun return NULL;
97*4882a593Smuzhiyun }
98*4882a593Smuzhiyun
99*4882a593Smuzhiyun static inline struct usb_role_switch *
usb_role_switch_register(struct device * parent,const struct usb_role_switch_desc * desc)100*4882a593Smuzhiyun usb_role_switch_register(struct device *parent,
101*4882a593Smuzhiyun const struct usb_role_switch_desc *desc)
102*4882a593Smuzhiyun {
103*4882a593Smuzhiyun return ERR_PTR(-ENODEV);
104*4882a593Smuzhiyun }
105*4882a593Smuzhiyun
usb_role_switch_unregister(struct usb_role_switch * sw)106*4882a593Smuzhiyun static inline void usb_role_switch_unregister(struct usb_role_switch *sw) { }
107*4882a593Smuzhiyun
108*4882a593Smuzhiyun static inline void
usb_role_switch_set_drvdata(struct usb_role_switch * sw,void * data)109*4882a593Smuzhiyun usb_role_switch_set_drvdata(struct usb_role_switch *sw, void *data)
110*4882a593Smuzhiyun {
111*4882a593Smuzhiyun }
112*4882a593Smuzhiyun
usb_role_switch_get_drvdata(struct usb_role_switch * sw)113*4882a593Smuzhiyun static inline void *usb_role_switch_get_drvdata(struct usb_role_switch *sw)
114*4882a593Smuzhiyun {
115*4882a593Smuzhiyun return NULL;
116*4882a593Smuzhiyun }
117*4882a593Smuzhiyun
118*4882a593Smuzhiyun #endif
119*4882a593Smuzhiyun
120*4882a593Smuzhiyun #endif /* __LINUX_USB_ROLE_H */
121