otg.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*
  2. * otg.c -- USB OTG utility code
  3. *
  4. * Copyright (C) 2004 Texas Instruments
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/export.h>
  13. #include <linux/err.h>
  14. #include <linux/device.h>
  15. #include <linux/slab.h>
  16. #include <linux/usb/otg.h>
  17. static LIST_HEAD(phy_list);
  18. static DEFINE_SPINLOCK(phy_lock);
  19. static struct usb_phy *__usb_find_phy(struct list_head *list,
  20. enum usb_phy_type type)
  21. {
  22. struct usb_phy *phy = NULL;
  23. list_for_each_entry(phy, list, head) {
  24. if (phy->type != type)
  25. continue;
  26. return phy;
  27. }
  28. return ERR_PTR(-ENODEV);
  29. }
  30. static void devm_usb_phy_release(struct device *dev, void *res)
  31. {
  32. struct usb_phy *phy = *(struct usb_phy **)res;
  33. usb_put_phy(phy);
  34. }
  35. static int devm_usb_phy_match(struct device *dev, void *res, void *match_data)
  36. {
  37. return res == match_data;
  38. }
  39. /**
  40. * devm_usb_get_phy - find the USB PHY
  41. * @dev - device that requests this phy
  42. * @type - the type of the phy the controller requires
  43. *
  44. * Gets the phy using usb_get_phy(), and associates a device with it using
  45. * devres. On driver detach, release function is invoked on the devres data,
  46. * then, devres data is freed.
  47. *
  48. * For use by USB host and peripheral drivers.
  49. */
  50. struct usb_phy *devm_usb_get_phy(struct device *dev, enum usb_phy_type type)
  51. {
  52. struct usb_phy **ptr, *phy;
  53. ptr = devres_alloc(devm_usb_phy_release, sizeof(*ptr), GFP_KERNEL);
  54. if (!ptr)
  55. return NULL;
  56. phy = usb_get_phy(type);
  57. if (phy) {
  58. *ptr = phy;
  59. devres_add(dev, ptr);
  60. } else
  61. devres_free(ptr);
  62. return phy;
  63. }
  64. EXPORT_SYMBOL(devm_usb_get_phy);
  65. /**
  66. * usb_get_phy - find the USB PHY
  67. * @type - the type of the phy the controller requires
  68. *
  69. * Returns the phy driver, after getting a refcount to it; or
  70. * null if there is no such phy. The caller is responsible for
  71. * calling usb_put_phy() to release that count.
  72. *
  73. * For use by USB host and peripheral drivers.
  74. */
  75. struct usb_phy *usb_get_phy(enum usb_phy_type type)
  76. {
  77. struct usb_phy *phy = NULL;
  78. unsigned long flags;
  79. spin_lock_irqsave(&phy_lock, flags);
  80. phy = __usb_find_phy(&phy_list, type);
  81. if (IS_ERR(phy)) {
  82. pr_err("unable to find transceiver of type %s\n",
  83. usb_phy_type_string(type));
  84. return phy;
  85. }
  86. get_device(phy->dev);
  87. spin_unlock_irqrestore(&phy_lock, flags);
  88. return phy;
  89. }
  90. EXPORT_SYMBOL(usb_get_phy);
  91. /**
  92. * devm_usb_put_phy - release the USB PHY
  93. * @dev - device that wants to release this phy
  94. * @phy - the phy returned by devm_usb_get_phy()
  95. *
  96. * destroys the devres associated with this phy and invokes usb_put_phy
  97. * to release the phy.
  98. *
  99. * For use by USB host and peripheral drivers.
  100. */
  101. void devm_usb_put_phy(struct device *dev, struct usb_phy *phy)
  102. {
  103. int r;
  104. r = devres_destroy(dev, devm_usb_phy_release, devm_usb_phy_match, phy);
  105. dev_WARN_ONCE(dev, r, "couldn't find PHY resource\n");
  106. }
  107. EXPORT_SYMBOL(devm_usb_put_phy);
  108. /**
  109. * usb_put_phy - release the USB PHY
  110. * @x: the phy returned by usb_get_phy()
  111. *
  112. * Releases a refcount the caller received from usb_get_phy().
  113. *
  114. * For use by USB host and peripheral drivers.
  115. */
  116. void usb_put_phy(struct usb_phy *x)
  117. {
  118. if (x)
  119. put_device(x->dev);
  120. }
  121. EXPORT_SYMBOL(usb_put_phy);
  122. /**
  123. * usb_add_phy - declare the USB PHY
  124. * @x: the USB phy to be used; or NULL
  125. * @type - the type of this PHY
  126. *
  127. * This call is exclusively for use by phy drivers, which
  128. * coordinate the activities of drivers for host and peripheral
  129. * controllers, and in some cases for VBUS current regulation.
  130. */
  131. int usb_add_phy(struct usb_phy *x, enum usb_phy_type type)
  132. {
  133. int ret = 0;
  134. unsigned long flags;
  135. struct usb_phy *phy;
  136. if (x && x->type != USB_PHY_TYPE_UNDEFINED) {
  137. dev_err(x->dev, "not accepting initialized PHY %s\n", x->label);
  138. return -EINVAL;
  139. }
  140. spin_lock_irqsave(&phy_lock, flags);
  141. list_for_each_entry(phy, &phy_list, head) {
  142. if (phy->type == type) {
  143. ret = -EBUSY;
  144. dev_err(x->dev, "transceiver type %s already exists\n",
  145. usb_phy_type_string(type));
  146. goto out;
  147. }
  148. }
  149. x->type = type;
  150. list_add_tail(&x->head, &phy_list);
  151. out:
  152. spin_unlock_irqrestore(&phy_lock, flags);
  153. return ret;
  154. }
  155. EXPORT_SYMBOL(usb_add_phy);
  156. /**
  157. * usb_remove_phy - remove the OTG PHY
  158. * @x: the USB OTG PHY to be removed;
  159. *
  160. * This reverts the effects of usb_add_phy
  161. */
  162. void usb_remove_phy(struct usb_phy *x)
  163. {
  164. unsigned long flags;
  165. spin_lock_irqsave(&phy_lock, flags);
  166. if (x)
  167. list_del(&x->head);
  168. spin_unlock_irqrestore(&phy_lock, flags);
  169. }
  170. EXPORT_SYMBOL(usb_remove_phy);
  171. const char *otg_state_string(enum usb_otg_state state)
  172. {
  173. switch (state) {
  174. case OTG_STATE_A_IDLE:
  175. return "a_idle";
  176. case OTG_STATE_A_WAIT_VRISE:
  177. return "a_wait_vrise";
  178. case OTG_STATE_A_WAIT_BCON:
  179. return "a_wait_bcon";
  180. case OTG_STATE_A_HOST:
  181. return "a_host";
  182. case OTG_STATE_A_SUSPEND:
  183. return "a_suspend";
  184. case OTG_STATE_A_PERIPHERAL:
  185. return "a_peripheral";
  186. case OTG_STATE_A_WAIT_VFALL:
  187. return "a_wait_vfall";
  188. case OTG_STATE_A_VBUS_ERR:
  189. return "a_vbus_err";
  190. case OTG_STATE_B_IDLE:
  191. return "b_idle";
  192. case OTG_STATE_B_SRP_INIT:
  193. return "b_srp_init";
  194. case OTG_STATE_B_PERIPHERAL:
  195. return "b_peripheral";
  196. case OTG_STATE_B_WAIT_ACON:
  197. return "b_wait_acon";
  198. case OTG_STATE_B_HOST:
  199. return "b_host";
  200. default:
  201. return "UNDEFINED";
  202. }
  203. }
  204. EXPORT_SYMBOL(otg_state_string);