usb-common.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * Provides code common for host and device side USB.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation, version 2.
  7. *
  8. * If either host side (ie. CONFIG_USB=y) or device side USB stack
  9. * (ie. CONFIG_USB_GADGET=y) is compiled in the kernel, this module is
  10. * compiled-in as well. Otherwise, if either of the two stacks is
  11. * compiled as module, this file is compiled as module as well.
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/of.h>
  16. #include <linux/usb/ch9.h>
  17. #include <linux/usb/of.h>
  18. #include <linux/usb/otg.h>
  19. const char *usb_otg_state_string(enum usb_otg_state state)
  20. {
  21. static const char *const names[] = {
  22. [OTG_STATE_A_IDLE] = "a_idle",
  23. [OTG_STATE_A_WAIT_VRISE] = "a_wait_vrise",
  24. [OTG_STATE_A_WAIT_BCON] = "a_wait_bcon",
  25. [OTG_STATE_A_HOST] = "a_host",
  26. [OTG_STATE_A_SUSPEND] = "a_suspend",
  27. [OTG_STATE_A_PERIPHERAL] = "a_peripheral",
  28. [OTG_STATE_A_WAIT_VFALL] = "a_wait_vfall",
  29. [OTG_STATE_A_VBUS_ERR] = "a_vbus_err",
  30. [OTG_STATE_B_IDLE] = "b_idle",
  31. [OTG_STATE_B_SRP_INIT] = "b_srp_init",
  32. [OTG_STATE_B_PERIPHERAL] = "b_peripheral",
  33. [OTG_STATE_B_WAIT_ACON] = "b_wait_acon",
  34. [OTG_STATE_B_HOST] = "b_host",
  35. };
  36. if (state < 0 || state >= ARRAY_SIZE(names))
  37. return "UNDEFINED";
  38. return names[state];
  39. }
  40. EXPORT_SYMBOL_GPL(usb_otg_state_string);
  41. const char *usb_speed_string(enum usb_device_speed speed)
  42. {
  43. static const char *const names[] = {
  44. [USB_SPEED_UNKNOWN] = "UNKNOWN",
  45. [USB_SPEED_LOW] = "low-speed",
  46. [USB_SPEED_FULL] = "full-speed",
  47. [USB_SPEED_HIGH] = "high-speed",
  48. [USB_SPEED_WIRELESS] = "wireless",
  49. [USB_SPEED_SUPER] = "super-speed",
  50. };
  51. if (speed < 0 || speed >= ARRAY_SIZE(names))
  52. speed = USB_SPEED_UNKNOWN;
  53. return names[speed];
  54. }
  55. EXPORT_SYMBOL_GPL(usb_speed_string);
  56. const char *usb_state_string(enum usb_device_state state)
  57. {
  58. static const char *const names[] = {
  59. [USB_STATE_NOTATTACHED] = "not attached",
  60. [USB_STATE_ATTACHED] = "attached",
  61. [USB_STATE_POWERED] = "powered",
  62. [USB_STATE_RECONNECTING] = "reconnecting",
  63. [USB_STATE_UNAUTHENTICATED] = "unauthenticated",
  64. [USB_STATE_DEFAULT] = "default",
  65. [USB_STATE_ADDRESS] = "addresssed",
  66. [USB_STATE_CONFIGURED] = "configured",
  67. [USB_STATE_SUSPENDED] = "suspended",
  68. };
  69. if (state < 0 || state >= ARRAY_SIZE(names))
  70. return "UNKNOWN";
  71. return names[state];
  72. }
  73. EXPORT_SYMBOL_GPL(usb_state_string);
  74. #ifdef CONFIG_OF
  75. static const char *const usb_dr_modes[] = {
  76. [USB_DR_MODE_UNKNOWN] = "",
  77. [USB_DR_MODE_HOST] = "host",
  78. [USB_DR_MODE_PERIPHERAL] = "peripheral",
  79. [USB_DR_MODE_OTG] = "otg",
  80. };
  81. /**
  82. * of_usb_get_dr_mode - Get dual role mode for given device_node
  83. * @np: Pointer to the given device_node
  84. *
  85. * The function gets phy interface string from property 'dr_mode',
  86. * and returns the correspondig enum usb_dr_mode
  87. */
  88. enum usb_dr_mode of_usb_get_dr_mode(struct device_node *np)
  89. {
  90. const char *dr_mode;
  91. int err, i;
  92. err = of_property_read_string(np, "dr_mode", &dr_mode);
  93. if (err < 0)
  94. return USB_DR_MODE_UNKNOWN;
  95. for (i = 0; i < ARRAY_SIZE(usb_dr_modes); i++)
  96. if (!strcmp(dr_mode, usb_dr_modes[i]))
  97. return i;
  98. return USB_DR_MODE_UNKNOWN;
  99. }
  100. EXPORT_SYMBOL_GPL(of_usb_get_dr_mode);
  101. #endif
  102. MODULE_LICENSE("GPL");