role.h 3.6 KB

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