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 device *dev, enum usb_role role);
17 typedef enum usb_role (*usb_role_switch_get_t)(struct device *dev);
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
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.
36 struct usb_role_switch_desc {
37 struct fwnode_handle *fwnode;
38 struct device *usb2_port;
39 struct device *usb3_port;
41 usb_role_switch_set_t set;
42 usb_role_switch_get_t get;
43 bool allow_userspace_control;
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);
55 struct usb_role_switch *
56 usb_role_switch_find_by_fwnode(const struct fwnode_handle *fwnode);
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);
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);
66 static inline int usb_role_switch_set_role(struct usb_role_switch *sw,
72 static inline enum usb_role usb_role_switch_get_role(struct usb_role_switch *sw)
77 static inline struct usb_role_switch *usb_role_switch_get(struct device *dev)
79 return ERR_PTR(-ENODEV);
82 static inline struct usb_role_switch *
83 fwnode_usb_role_switch_get(struct fwnode_handle *node)
85 return ERR_PTR(-ENODEV);
88 static inline void usb_role_switch_put(struct usb_role_switch *sw) { }
90 static inline struct usb_role_switch *
91 usb_role_switch_register(struct device *parent,
92 const struct usb_role_switch_desc *desc)
94 return ERR_PTR(-ENODEV);
97 static inline void usb_role_switch_unregister(struct usb_role_switch *sw) { }
100 usb_role_switch_set_drvdata(struct usb_role_switch *sw, void *data)
104 static inline void *usb_role_switch_get_drvdata(struct usb_role_switch *sw)
111 #endif /* __LINUX_USB_ROLE_H */