blob: 2d77f97df72d6422a2445eeb4b3fba6d63a26d59 [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
16typedef int (*usb_role_switch_set_t)(struct device *dev, enum usb_role role);
17typedef enum usb_role (*usb_role_switch_get_t)(struct device *dev);
18
19/**
20 * struct usb_role_switch_desc - USB Role Switch Descriptor
David Brazdil0f672f62019-12-10 10:32:29 +000021 * @fwnode: The device node to be associated with the role switch
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000022 * @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 *
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.
34 */
35struct usb_role_switch_desc {
David Brazdil0f672f62019-12-10 10:32:29 +000036 struct fwnode_handle *fwnode;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000037 struct device *usb2_port;
38 struct device *usb3_port;
39 struct device *udc;
40 usb_role_switch_set_t set;
41 usb_role_switch_get_t get;
42 bool allow_userspace_control;
43};
44
David Brazdil0f672f62019-12-10 10:32:29 +000045
46#if IS_ENABLED(CONFIG_USB_ROLE_SWITCH)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000047int usb_role_switch_set_role(struct usb_role_switch *sw, enum usb_role role);
48enum usb_role usb_role_switch_get_role(struct usb_role_switch *sw);
49struct usb_role_switch *usb_role_switch_get(struct device *dev);
David Brazdil0f672f62019-12-10 10:32:29 +000050struct usb_role_switch *fwnode_usb_role_switch_get(struct fwnode_handle *node);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000051void usb_role_switch_put(struct usb_role_switch *sw);
52
53struct usb_role_switch *
54usb_role_switch_register(struct device *parent,
55 const struct usb_role_switch_desc *desc);
56void usb_role_switch_unregister(struct usb_role_switch *sw);
David Brazdil0f672f62019-12-10 10:32:29 +000057#else
58static inline int usb_role_switch_set_role(struct usb_role_switch *sw,
59 enum usb_role role)
60{
61 return 0;
62}
63
64static inline enum usb_role usb_role_switch_get_role(struct usb_role_switch *sw)
65{
66 return USB_ROLE_NONE;
67}
68
69static inline struct usb_role_switch *usb_role_switch_get(struct device *dev)
70{
71 return ERR_PTR(-ENODEV);
72}
73
74static inline struct usb_role_switch *
75fwnode_usb_role_switch_get(struct fwnode_handle *node)
76{
77 return ERR_PTR(-ENODEV);
78}
79
80static inline void usb_role_switch_put(struct usb_role_switch *sw) { }
81
82static inline struct usb_role_switch *
83usb_role_switch_register(struct device *parent,
84 const struct usb_role_switch_desc *desc)
85{
86 return ERR_PTR(-ENODEV);
87}
88
89static inline void usb_role_switch_unregister(struct usb_role_switch *sw) { }
90#endif
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000091
92#endif /* __LINUX_USB_ROLE_H */