common.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. static const char *const speed_names[] = {
  42. [USB_SPEED_UNKNOWN] = "UNKNOWN",
  43. [USB_SPEED_LOW] = "low-speed",
  44. [USB_SPEED_FULL] = "full-speed",
  45. [USB_SPEED_HIGH] = "high-speed",
  46. [USB_SPEED_WIRELESS] = "wireless",
  47. [USB_SPEED_SUPER] = "super-speed",
  48. };
  49. const char *usb_speed_string(enum usb_device_speed speed)
  50. {
  51. if (speed < 0 || speed >= ARRAY_SIZE(speed_names))
  52. speed = USB_SPEED_UNKNOWN;
  53. return speed_names[speed];
  54. }
  55. EXPORT_SYMBOL_GPL(usb_speed_string);
  56. enum usb_device_speed usb_get_maximum_speed(struct device *dev)
  57. {
  58. const char *maximum_speed;
  59. int err;
  60. int i;
  61. err = device_property_read_string(dev, "maximum-speed", &maximum_speed);
  62. if (err < 0)
  63. return USB_SPEED_UNKNOWN;
  64. for (i = 0; i < ARRAY_SIZE(speed_names); i++)
  65. if (strcmp(maximum_speed, speed_names[i]) == 0)
  66. return i;
  67. return USB_SPEED_UNKNOWN;
  68. }
  69. EXPORT_SYMBOL_GPL(usb_get_maximum_speed);
  70. const char *usb_state_string(enum usb_device_state state)
  71. {
  72. static const char *const names[] = {
  73. [USB_STATE_NOTATTACHED] = "not attached",
  74. [USB_STATE_ATTACHED] = "attached",
  75. [USB_STATE_POWERED] = "powered",
  76. [USB_STATE_RECONNECTING] = "reconnecting",
  77. [USB_STATE_UNAUTHENTICATED] = "unauthenticated",
  78. [USB_STATE_DEFAULT] = "default",
  79. [USB_STATE_ADDRESS] = "addressed",
  80. [USB_STATE_CONFIGURED] = "configured",
  81. [USB_STATE_SUSPENDED] = "suspended",
  82. };
  83. if (state < 0 || state >= ARRAY_SIZE(names))
  84. return "UNKNOWN";
  85. return names[state];
  86. }
  87. EXPORT_SYMBOL_GPL(usb_state_string);
  88. static const char *const usb_dr_modes[] = {
  89. [USB_DR_MODE_UNKNOWN] = "",
  90. [USB_DR_MODE_HOST] = "host",
  91. [USB_DR_MODE_PERIPHERAL] = "peripheral",
  92. [USB_DR_MODE_OTG] = "otg",
  93. };
  94. enum usb_dr_mode usb_get_dr_mode(struct device *dev)
  95. {
  96. const char *dr_mode;
  97. int err, i;
  98. err = device_property_read_string(dev, "dr_mode", &dr_mode);
  99. if (err < 0)
  100. return USB_DR_MODE_UNKNOWN;
  101. for (i = 0; i < ARRAY_SIZE(usb_dr_modes); i++)
  102. if (!strcmp(dr_mode, usb_dr_modes[i]))
  103. return i;
  104. return USB_DR_MODE_UNKNOWN;
  105. }
  106. EXPORT_SYMBOL_GPL(usb_get_dr_mode);
  107. #ifdef CONFIG_OF
  108. /**
  109. * of_usb_host_tpl_support - to get if Targeted Peripheral List is supported
  110. * for given targeted hosts (non-PC hosts)
  111. * @np: Pointer to the given device_node
  112. *
  113. * The function gets if the targeted hosts support TPL or not
  114. */
  115. bool of_usb_host_tpl_support(struct device_node *np)
  116. {
  117. if (of_find_property(np, "tpl-support", NULL))
  118. return true;
  119. return false;
  120. }
  121. EXPORT_SYMBOL_GPL(of_usb_host_tpl_support);
  122. /**
  123. * of_usb_update_otg_caps - to update usb otg capabilities according to
  124. * the passed properties in DT.
  125. * @np: Pointer to the given device_node
  126. * @otg_caps: Pointer to the target usb_otg_caps to be set
  127. *
  128. * The function updates the otg capabilities
  129. */
  130. int of_usb_update_otg_caps(struct device_node *np,
  131. struct usb_otg_caps *otg_caps)
  132. {
  133. u32 otg_rev;
  134. if (!otg_caps)
  135. return -EINVAL;
  136. if (!of_property_read_u32(np, "otg-rev", &otg_rev)) {
  137. switch (otg_rev) {
  138. case 0x0100:
  139. case 0x0120:
  140. case 0x0130:
  141. case 0x0200:
  142. /* Choose the lesser one if it's already been set */
  143. if (otg_caps->otg_rev)
  144. otg_caps->otg_rev = min_t(u16, otg_rev,
  145. otg_caps->otg_rev);
  146. else
  147. otg_caps->otg_rev = otg_rev;
  148. break;
  149. default:
  150. pr_err("%s: unsupported otg-rev: 0x%x\n",
  151. np->full_name, otg_rev);
  152. return -EINVAL;
  153. }
  154. } else {
  155. /*
  156. * otg-rev is mandatory for otg properties, if not passed
  157. * we set it to be 0 and assume it's a legacy otg device.
  158. * Non-dt platform can set it afterwards.
  159. */
  160. otg_caps->otg_rev = 0;
  161. }
  162. if (of_find_property(np, "hnp-disable", NULL))
  163. otg_caps->hnp_support = false;
  164. if (of_find_property(np, "srp-disable", NULL))
  165. otg_caps->srp_support = false;
  166. if (of_find_property(np, "adp-disable", NULL) ||
  167. (otg_caps->otg_rev < 0x0200))
  168. otg_caps->adp_support = false;
  169. return 0;
  170. }
  171. EXPORT_SYMBOL_GPL(of_usb_update_otg_caps);
  172. #endif
  173. MODULE_LICENSE("GPL");