otg.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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/usb/otg.h>
  16. static LIST_HEAD(phy_list);
  17. static DEFINE_SPINLOCK(phy_lock);
  18. static struct usb_phy *__usb_find_phy(struct list_head *list,
  19. enum usb_phy_type type)
  20. {
  21. struct usb_phy *phy = NULL;
  22. list_for_each_entry(phy, list, head) {
  23. if (phy->type != type)
  24. continue;
  25. return phy;
  26. }
  27. return ERR_PTR(-ENODEV);
  28. }
  29. /**
  30. * usb_get_phy - find the USB PHY
  31. * @type - the type of the phy the controller requires
  32. *
  33. * Returns the phy driver, after getting a refcount to it; or
  34. * null if there is no such phy. The caller is responsible for
  35. * calling usb_put_phy() to release that count.
  36. *
  37. * For use by USB host and peripheral drivers.
  38. */
  39. struct usb_phy *usb_get_phy(enum usb_phy_type type)
  40. {
  41. struct usb_phy *phy = NULL;
  42. unsigned long flags;
  43. spin_lock_irqsave(&phy_lock, flags);
  44. phy = __usb_find_phy(&phy_list, type);
  45. if (IS_ERR(phy)) {
  46. pr_err("unable to find transceiver of type %s\n",
  47. usb_phy_type_string(type));
  48. return phy;
  49. }
  50. get_device(phy->dev);
  51. spin_unlock_irqrestore(&phy_lock, flags);
  52. return phy;
  53. }
  54. EXPORT_SYMBOL(usb_get_phy);
  55. /**
  56. * usb_put_phy - release the USB PHY
  57. * @x: the phy returned by usb_get_phy()
  58. *
  59. * Releases a refcount the caller received from usb_get_phy().
  60. *
  61. * For use by USB host and peripheral drivers.
  62. */
  63. void usb_put_phy(struct usb_phy *x)
  64. {
  65. if (x)
  66. put_device(x->dev);
  67. }
  68. EXPORT_SYMBOL(usb_put_phy);
  69. /**
  70. * usb_add_phy - declare the USB PHY
  71. * @x: the USB phy to be used; or NULL
  72. * @type - the type of this PHY
  73. *
  74. * This call is exclusively for use by phy drivers, which
  75. * coordinate the activities of drivers for host and peripheral
  76. * controllers, and in some cases for VBUS current regulation.
  77. */
  78. int usb_add_phy(struct usb_phy *x, enum usb_phy_type type)
  79. {
  80. int ret = 0;
  81. unsigned long flags;
  82. struct usb_phy *phy;
  83. if (x && x->type != USB_PHY_TYPE_UNDEFINED) {
  84. dev_err(x->dev, "not accepting initialized PHY %s\n", x->label);
  85. return -EINVAL;
  86. }
  87. spin_lock_irqsave(&phy_lock, flags);
  88. list_for_each_entry(phy, &phy_list, head) {
  89. if (phy->type == type) {
  90. ret = -EBUSY;
  91. dev_err(x->dev, "transceiver type %s already exists\n",
  92. usb_phy_type_string(type));
  93. goto out;
  94. }
  95. }
  96. x->type = type;
  97. list_add_tail(&x->head, &phy_list);
  98. out:
  99. spin_unlock_irqrestore(&phy_lock, flags);
  100. return ret;
  101. }
  102. EXPORT_SYMBOL(usb_add_phy);
  103. /**
  104. * usb_remove_phy - remove the OTG PHY
  105. * @x: the USB OTG PHY to be removed;
  106. *
  107. * This reverts the effects of usb_add_phy
  108. */
  109. void usb_remove_phy(struct usb_phy *x)
  110. {
  111. unsigned long flags;
  112. spin_lock_irqsave(&phy_lock, flags);
  113. if (x)
  114. list_del(&x->head);
  115. spin_unlock_irqrestore(&phy_lock, flags);
  116. }
  117. EXPORT_SYMBOL(usb_remove_phy);
  118. const char *otg_state_string(enum usb_otg_state state)
  119. {
  120. switch (state) {
  121. case OTG_STATE_A_IDLE:
  122. return "a_idle";
  123. case OTG_STATE_A_WAIT_VRISE:
  124. return "a_wait_vrise";
  125. case OTG_STATE_A_WAIT_BCON:
  126. return "a_wait_bcon";
  127. case OTG_STATE_A_HOST:
  128. return "a_host";
  129. case OTG_STATE_A_SUSPEND:
  130. return "a_suspend";
  131. case OTG_STATE_A_PERIPHERAL:
  132. return "a_peripheral";
  133. case OTG_STATE_A_WAIT_VFALL:
  134. return "a_wait_vfall";
  135. case OTG_STATE_A_VBUS_ERR:
  136. return "a_vbus_err";
  137. case OTG_STATE_B_IDLE:
  138. return "b_idle";
  139. case OTG_STATE_B_SRP_INIT:
  140. return "b_srp_init";
  141. case OTG_STATE_B_PERIPHERAL:
  142. return "b_peripheral";
  143. case OTG_STATE_B_WAIT_ACON:
  144. return "b_wait_acon";
  145. case OTG_STATE_B_HOST:
  146. return "b_host";
  147. default:
  148. return "UNDEFINED";
  149. }
  150. }
  151. EXPORT_SYMBOL(otg_state_string);