otg.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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/device.h>
  14. #include <linux/usb/otg.h>
  15. static struct usb_phy *phy;
  16. /**
  17. * usb_get_transceiver - find the (single) USB transceiver
  18. *
  19. * Returns the transceiver driver, after getting a refcount to it; or
  20. * null if there is no such transceiver. The caller is responsible for
  21. * calling usb_put_transceiver() to release that count.
  22. *
  23. * For use by USB host and peripheral drivers.
  24. */
  25. struct usb_phy *usb_get_transceiver(void)
  26. {
  27. if (phy)
  28. get_device(phy->dev);
  29. return phy;
  30. }
  31. EXPORT_SYMBOL(usb_get_transceiver);
  32. /**
  33. * usb_put_transceiver - release the (single) USB transceiver
  34. * @x: the transceiver returned by usb_get_transceiver()
  35. *
  36. * Releases a refcount the caller received from usb_get_transceiver().
  37. *
  38. * For use by USB host and peripheral drivers.
  39. */
  40. void usb_put_transceiver(struct usb_phy *x)
  41. {
  42. if (x)
  43. put_device(x->dev);
  44. }
  45. EXPORT_SYMBOL(usb_put_transceiver);
  46. /**
  47. * usb_set_transceiver - declare the (single) USB transceiver
  48. * @x: the USB transceiver to be used; or NULL
  49. *
  50. * This call is exclusively for use by transceiver drivers, which
  51. * coordinate the activities of drivers for host and peripheral
  52. * controllers, and in some cases for VBUS current regulation.
  53. */
  54. int usb_set_transceiver(struct usb_phy *x)
  55. {
  56. if (phy && x)
  57. return -EBUSY;
  58. phy = x;
  59. return 0;
  60. }
  61. EXPORT_SYMBOL(usb_set_transceiver);
  62. const char *otg_state_string(enum usb_otg_state state)
  63. {
  64. switch (state) {
  65. case OTG_STATE_A_IDLE:
  66. return "a_idle";
  67. case OTG_STATE_A_WAIT_VRISE:
  68. return "a_wait_vrise";
  69. case OTG_STATE_A_WAIT_BCON:
  70. return "a_wait_bcon";
  71. case OTG_STATE_A_HOST:
  72. return "a_host";
  73. case OTG_STATE_A_SUSPEND:
  74. return "a_suspend";
  75. case OTG_STATE_A_PERIPHERAL:
  76. return "a_peripheral";
  77. case OTG_STATE_A_WAIT_VFALL:
  78. return "a_wait_vfall";
  79. case OTG_STATE_A_VBUS_ERR:
  80. return "a_vbus_err";
  81. case OTG_STATE_B_IDLE:
  82. return "b_idle";
  83. case OTG_STATE_B_SRP_INIT:
  84. return "b_srp_init";
  85. case OTG_STATE_B_PERIPHERAL:
  86. return "b_peripheral";
  87. case OTG_STATE_B_WAIT_ACON:
  88. return "b_wait_acon";
  89. case OTG_STATE_B_HOST:
  90. return "b_host";
  91. default:
  92. return "UNDEFINED";
  93. }
  94. }
  95. EXPORT_SYMBOL(otg_state_string);
  96. int otg_send_event(enum usb_otg_event event)
  97. {
  98. struct usb_phy *phy = usb_get_transceiver();
  99. int ret = -ENOTSUPP;
  100. if (phy && phy->otg && phy->otg->send_event)
  101. ret = phy->otg->send_event(phy->otg, event);
  102. if (phy)
  103. usb_put_transceiver(phy);
  104. return ret;
  105. }
  106. EXPORT_SYMBOL(otg_send_event);