blob: b9ccaeb8a4aef14e08e092a4da405c4f22764c67 [file] [log] [blame]
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001// 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
8struct usb_role_switch;
9
10enum usb_role {
11 USB_ROLE_NONE,
12 USB_ROLE_HOST,
13 USB_ROLE_DEVICE,
14};
15
Olivier Deprez157378f2022-04-04 15:47:50 +020016typedef int (*usb_role_switch_set_t)(struct usb_role_switch *sw,
17 enum usb_role role);
18typedef enum usb_role (*usb_role_switch_get_t)(struct usb_role_switch *sw);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000019
20/**
21 * struct usb_role_switch_desc - USB Role Switch Descriptor
David Brazdil0f672f62019-12-10 10:32:29 +000022 * @fwnode: The device node to be associated with the role switch
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000023 * @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
Olivier Deprez157378f2022-04-04 15:47:50 +020029 * @driver_data: Private data pointer
30 * @name: Name for the switch (optional)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000031 *
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.
37 */
38struct usb_role_switch_desc {
David Brazdil0f672f62019-12-10 10:32:29 +000039 struct fwnode_handle *fwnode;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000040 struct device *usb2_port;
41 struct device *usb3_port;
42 struct device *udc;
43 usb_role_switch_set_t set;
44 usb_role_switch_get_t get;
45 bool allow_userspace_control;
Olivier Deprez157378f2022-04-04 15:47:50 +020046 void *driver_data;
47 const char *name;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000048};
49
David Brazdil0f672f62019-12-10 10:32:29 +000050
51#if IS_ENABLED(CONFIG_USB_ROLE_SWITCH)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000052int usb_role_switch_set_role(struct usb_role_switch *sw, enum usb_role role);
53enum usb_role usb_role_switch_get_role(struct usb_role_switch *sw);
54struct usb_role_switch *usb_role_switch_get(struct device *dev);
David Brazdil0f672f62019-12-10 10:32:29 +000055struct usb_role_switch *fwnode_usb_role_switch_get(struct fwnode_handle *node);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000056void usb_role_switch_put(struct usb_role_switch *sw);
57
58struct usb_role_switch *
Olivier Deprez157378f2022-04-04 15:47:50 +020059usb_role_switch_find_by_fwnode(const struct fwnode_handle *fwnode);
60
61struct usb_role_switch *
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000062usb_role_switch_register(struct device *parent,
63 const struct usb_role_switch_desc *desc);
64void usb_role_switch_unregister(struct usb_role_switch *sw);
Olivier Deprez157378f2022-04-04 15:47:50 +020065
66void usb_role_switch_set_drvdata(struct usb_role_switch *sw, void *data);
67void *usb_role_switch_get_drvdata(struct usb_role_switch *sw);
David Brazdil0f672f62019-12-10 10:32:29 +000068#else
69static inline int usb_role_switch_set_role(struct usb_role_switch *sw,
70 enum usb_role role)
71{
72 return 0;
73}
74
75static inline enum usb_role usb_role_switch_get_role(struct usb_role_switch *sw)
76{
77 return USB_ROLE_NONE;
78}
79
80static inline struct usb_role_switch *usb_role_switch_get(struct device *dev)
81{
82 return ERR_PTR(-ENODEV);
83}
84
85static inline struct usb_role_switch *
86fwnode_usb_role_switch_get(struct fwnode_handle *node)
87{
88 return ERR_PTR(-ENODEV);
89}
90
91static inline void usb_role_switch_put(struct usb_role_switch *sw) { }
92
93static inline struct usb_role_switch *
Olivier Deprez157378f2022-04-04 15:47:50 +020094usb_role_switch_find_by_fwnode(const struct fwnode_handle *fwnode)
95{
96 return NULL;
97}
98
99static inline struct usb_role_switch *
David Brazdil0f672f62019-12-10 10:32:29 +0000100usb_role_switch_register(struct device *parent,
101 const struct usb_role_switch_desc *desc)
102{
103 return ERR_PTR(-ENODEV);
104}
105
106static inline void usb_role_switch_unregister(struct usb_role_switch *sw) { }
Olivier Deprez157378f2022-04-04 15:47:50 +0200107
108static inline void
109usb_role_switch_set_drvdata(struct usb_role_switch *sw, void *data)
110{
111}
112
113static inline void *usb_role_switch_get_drvdata(struct usb_role_switch *sw)
114{
115 return NULL;
116}
117
David Brazdil0f672f62019-12-10 10:32:29 +0000118#endif
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000119
120#endif /* __LINUX_USB_ROLE_H */