usb: roles: Leave the private driver data pointer to the drivers
[platform/kernel/linux-rpi.git] / include / linux / usb / role.h
1 // SPDX-License-Identifier: GPL-2.0
2
3 #ifndef __LINUX_USB_ROLE_H
4 #define __LINUX_USB_ROLE_H
5
6 #include <linux/device.h>
7
8 struct usb_role_switch;
9
10 enum usb_role {
11         USB_ROLE_NONE,
12         USB_ROLE_HOST,
13         USB_ROLE_DEVICE,
14 };
15
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);
18
19 /**
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
28  * @driver_data: Private data pointer
29  *
30  * @usb2_port and @usb3_port will point to the USB host port and @udc to the USB
31  * device controller behind the USB connector with the role switch. If
32  * @usb2_port, @usb3_port and @udc are included in the description, the
33  * reference count for them should be incremented by the caller of
34  * usb_role_switch_register() before registering the switch.
35  */
36 struct usb_role_switch_desc {
37         struct fwnode_handle *fwnode;
38         struct device *usb2_port;
39         struct device *usb3_port;
40         struct device *udc;
41         usb_role_switch_set_t set;
42         usb_role_switch_get_t get;
43         bool allow_userspace_control;
44         void *driver_data;
45 };
46
47
48 #if IS_ENABLED(CONFIG_USB_ROLE_SWITCH)
49 int usb_role_switch_set_role(struct usb_role_switch *sw, enum usb_role role);
50 enum usb_role usb_role_switch_get_role(struct usb_role_switch *sw);
51 struct usb_role_switch *usb_role_switch_get(struct device *dev);
52 struct usb_role_switch *fwnode_usb_role_switch_get(struct fwnode_handle *node);
53 void usb_role_switch_put(struct usb_role_switch *sw);
54
55 struct usb_role_switch *
56 usb_role_switch_find_by_fwnode(const struct fwnode_handle *fwnode);
57
58 struct usb_role_switch *
59 usb_role_switch_register(struct device *parent,
60                          const struct usb_role_switch_desc *desc);
61 void usb_role_switch_unregister(struct usb_role_switch *sw);
62
63 void usb_role_switch_set_drvdata(struct usb_role_switch *sw, void *data);
64 void *usb_role_switch_get_drvdata(struct usb_role_switch *sw);
65 #else
66 static inline int usb_role_switch_set_role(struct usb_role_switch *sw,
67                 enum usb_role role)
68 {
69         return 0;
70 }
71
72 static inline enum usb_role usb_role_switch_get_role(struct usb_role_switch *sw)
73 {
74         return USB_ROLE_NONE;
75 }
76
77 static inline struct usb_role_switch *usb_role_switch_get(struct device *dev)
78 {
79         return ERR_PTR(-ENODEV);
80 }
81
82 static inline struct usb_role_switch *
83 fwnode_usb_role_switch_get(struct fwnode_handle *node)
84 {
85         return ERR_PTR(-ENODEV);
86 }
87
88 static inline void usb_role_switch_put(struct usb_role_switch *sw) { }
89
90 static inline struct usb_role_switch *
91 usb_role_switch_register(struct device *parent,
92                          const struct usb_role_switch_desc *desc)
93 {
94         return ERR_PTR(-ENODEV);
95 }
96
97 static inline void usb_role_switch_unregister(struct usb_role_switch *sw) { }
98
99 static inline void
100 usb_role_switch_set_drvdata(struct usb_role_switch *sw, void *data)
101 {
102 }
103
104 static inline void *usb_role_switch_get_drvdata(struct usb_role_switch *sw)
105 {
106         return NULL;
107 }
108
109 #endif
110
111 #endif /* __LINUX_USB_ROLE_H */