otg.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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 (!IS_ERR(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. * -ENODEV 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. goto err0;
  85. }
  86. get_device(phy->dev);
  87. err0:
  88. spin_unlock_irqrestore(&phy_lock, flags);
  89. return phy;
  90. }
  91. EXPORT_SYMBOL(usb_get_phy);
  92. /**
  93. * devm_usb_put_phy - release the USB PHY
  94. * @dev - device that wants to release this phy
  95. * @phy - the phy returned by devm_usb_get_phy()
  96. *
  97. * destroys the devres associated with this phy and invokes usb_put_phy
  98. * to release the phy.
  99. *
  100. * For use by USB host and peripheral drivers.
  101. */
  102. void devm_usb_put_phy(struct device *dev, struct usb_phy *phy)
  103. {
  104. int r;
  105. r = devres_destroy(dev, devm_usb_phy_release, devm_usb_phy_match, phy);
  106. dev_WARN_ONCE(dev, r, "couldn't find PHY resource\n");
  107. }
  108. EXPORT_SYMBOL(devm_usb_put_phy);
  109. /**
  110. * usb_put_phy - release the USB PHY
  111. * @x: the phy returned by usb_get_phy()
  112. *
  113. * Releases a refcount the caller received from usb_get_phy().
  114. *
  115. * For use by USB host and peripheral drivers.
  116. */
  117. void usb_put_phy(struct usb_phy *x)
  118. {
  119. if (x)
  120. put_device(x->dev);
  121. }
  122. EXPORT_SYMBOL(usb_put_phy);
  123. /**
  124. * usb_add_phy - declare the USB PHY
  125. * @x: the USB phy to be used; or NULL
  126. * @type - the type of this PHY
  127. *
  128. * This call is exclusively for use by phy drivers, which
  129. * coordinate the activities of drivers for host and peripheral
  130. * controllers, and in some cases for VBUS current regulation.
  131. */
  132. int usb_add_phy(struct usb_phy *x, enum usb_phy_type type)
  133. {
  134. int ret = 0;
  135. unsigned long flags;
  136. struct usb_phy *phy;
  137. if (x->type != USB_PHY_TYPE_UNDEFINED) {
  138. dev_err(x->dev, "not accepting initialized PHY %s\n", x->label);
  139. return -EINVAL;
  140. }
  141. spin_lock_irqsave(&phy_lock, flags);
  142. list_for_each_entry(phy, &phy_list, head) {
  143. if (phy->type == type) {
  144. ret = -EBUSY;
  145. dev_err(x->dev, "transceiver type %s already exists\n",
  146. usb_phy_type_string(type));
  147. goto out;
  148. }
  149. }
  150. x->type = type;
  151. list_add_tail(&x->head, &phy_list);
  152. out:
  153. spin_unlock_irqrestore(&phy_lock, flags);
  154. return ret;
  155. }
  156. EXPORT_SYMBOL(usb_add_phy);
  157. /**
  158. * usb_remove_phy - remove the OTG PHY
  159. * @x: the USB OTG PHY to be removed;
  160. *
  161. * This reverts the effects of usb_add_phy
  162. */
  163. void usb_remove_phy(struct usb_phy *x)
  164. {
  165. unsigned long flags;
  166. spin_lock_irqsave(&phy_lock, flags);
  167. if (x)
  168. list_del(&x->head);
  169. spin_unlock_irqrestore(&phy_lock, flags);
  170. }
  171. EXPORT_SYMBOL(usb_remove_phy);
  172. const char *otg_state_string(enum usb_otg_state state)
  173. {
  174. switch (state) {
  175. case OTG_STATE_A_IDLE:
  176. return "a_idle";
  177. case OTG_STATE_A_WAIT_VRISE:
  178. return "a_wait_vrise";
  179. case OTG_STATE_A_WAIT_BCON:
  180. return "a_wait_bcon";
  181. case OTG_STATE_A_HOST:
  182. return "a_host";
  183. case OTG_STATE_A_SUSPEND:
  184. return "a_suspend";
  185. case OTG_STATE_A_PERIPHERAL:
  186. return "a_peripheral";
  187. case OTG_STATE_A_WAIT_VFALL:
  188. return "a_wait_vfall";
  189. case OTG_STATE_A_VBUS_ERR:
  190. return "a_vbus_err";
  191. case OTG_STATE_B_IDLE:
  192. return "b_idle";
  193. case OTG_STATE_B_SRP_INIT:
  194. return "b_srp_init";
  195. case OTG_STATE_B_PERIPHERAL:
  196. return "b_peripheral";
  197. case OTG_STATE_B_WAIT_ACON:
  198. return "b_wait_acon";
  199. case OTG_STATE_B_HOST:
  200. return "b_host";
  201. default:
  202. return "UNDEFINED";
  203. }
  204. }
  205. EXPORT_SYMBOL(otg_state_string);