ehci_controller.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * ehci_controller.c
  3. *
  4. * Created on: Mar 9, 2013
  5. * Author: hathach
  6. */
  7. /*
  8. * Software License Agreement (BSD License)
  9. * Copyright (c) 2012, hathach (tinyusb.org)
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or without modification,
  13. * are permitted provided that the following conditions are met:
  14. *
  15. * 1. Redistributions of source code must retain the above copyright notice,
  16. * this list of conditions and the following disclaimer.
  17. * 2. Redistributions in binary form must reproduce the above copyright notice,
  18. * this list of conditions and the following disclaimer in the documentation
  19. * and/or other materials provided with the distribution.
  20. * 3. The name of the author may not be used to endorse or promote products
  21. * derived from this software without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  24. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  25. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
  26. * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  27. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
  28. * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  31. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  32. * OF SUCH DAMAGE.
  33. *
  34. * This file is part of the tiny usb stack.
  35. */
  36. //--------------------------------------------------------------------+
  37. // INCLUDE
  38. //--------------------------------------------------------------------+
  39. #include "unity.h"
  40. #include "tusb_option.h"
  41. #include "errors.h"
  42. #include "binary.h"
  43. #include "hal.h"
  44. #include "ehci.h"
  45. #include "usbh_hcd.h"
  46. //--------------------------------------------------------------------+
  47. // MACRO CONSTANT TYPEDEF
  48. //--------------------------------------------------------------------+
  49. ehci_data_t ehci_data;
  50. LPC_USB0_Type lpc_usb0;
  51. LPC_USB1_Type lpc_usb1;
  52. extern usbh_device_info_t usbh_devices[TUSB_CFG_HOST_DEVICE_MAX+1];
  53. //--------------------------------------------------------------------+
  54. // IMPLEMENTATION
  55. //--------------------------------------------------------------------+
  56. void ehci_controller_control_xfer_proceed(uint8_t dev_addr, uint8_t p_data[])
  57. {
  58. ehci_registers_t* const regs = get_operational_register( usbh_devices[dev_addr].core_id );
  59. ehci_qhd_t * p_qhd = get_control_qhd(dev_addr);
  60. ehci_qtd_t * p_qtd_setup = get_control_qtds(dev_addr);
  61. ehci_qtd_t * p_qtd_data = p_qtd_setup + 1;
  62. ehci_qtd_t * p_qtd_status = p_qtd_setup + 2;
  63. tusb_std_request_t const *p_request = (tusb_std_request_t *) p_qtd_setup->buffer[0];
  64. if (p_request->wLength > 0 && p_request->bmRequestType.direction == TUSB_DIR_DEV_TO_HOST)
  65. {
  66. memcpy(p_qtd_data, p_data, p_request->wLength);
  67. }
  68. //------------- retire all QTDs -------------//
  69. p_qtd_setup->active = p_qtd_data->active = p_qtd_status->active = 0;
  70. p_qhd->qtd_overlay = *p_qtd_status;
  71. regs->usb_sts = EHCI_INT_MASK_NXP_ASYNC | EHCI_INT_MASK_NXP_PERIODIC;
  72. hcd_isr( usbh_devices[dev_addr].core_id );
  73. }
  74. bool complete_all_qtd_in_list(ehci_qhd_t *head)
  75. {
  76. ehci_qhd_t *p_qhd = head;
  77. do
  78. {
  79. if ( !p_qhd->qtd_overlay.halted )
  80. {
  81. while(!p_qhd->qtd_overlay.next.terminate)
  82. {
  83. ehci_qtd_t* p_qtd = (ehci_qtd_t*) align32(p_qhd->qtd_overlay.next.address);
  84. p_qtd->active = 0;
  85. p_qhd->qtd_overlay = *p_qtd;
  86. }
  87. }
  88. if (!p_qhd->next.terminate)
  89. {
  90. p_qhd = (ehci_qhd_t*) align32(p_qhd->next.address);
  91. }
  92. else
  93. {
  94. break;
  95. }
  96. }while(p_qhd != head); // stop if loop around
  97. return true;
  98. }
  99. void ehci_controller_run(uint8_t hostid)
  100. {
  101. //------------- Async List -------------//
  102. ehci_registers_t* const regs = get_operational_register(hostid);
  103. complete_all_qtd_in_list((ehci_qhd_t*) regs->async_list_base);
  104. //------------- Period List -------------//
  105. complete_all_qtd_in_list( get_period_head(hostid) );
  106. regs->usb_sts = EHCI_INT_MASK_NXP_ASYNC | EHCI_INT_MASK_NXP_PERIODIC;
  107. hcd_isr(hostid);
  108. }
  109. void ehci_controller_run_error(uint8_t hostid)
  110. {
  111. //------------- Async List -------------//
  112. ehci_registers_t* const regs = get_operational_register(hostid);
  113. ehci_qhd_t *p_qhd = (ehci_qhd_t*) regs->async_list_base;
  114. do
  115. {
  116. if ( !p_qhd->qtd_overlay.halted )
  117. {
  118. if(!p_qhd->qtd_overlay.next.terminate)
  119. {
  120. ehci_qtd_t* p_qtd = (ehci_qtd_t*) align32(p_qhd->qtd_overlay.next.address);
  121. p_qtd->active = 0;
  122. p_qtd->babble_err = p_qtd->buffer_err = p_qtd->xact_err = 1;
  123. p_qhd->qtd_overlay = *p_qtd;
  124. }
  125. }
  126. p_qhd = (ehci_qhd_t*) align32(p_qhd->next.address);
  127. }while(p_qhd != get_async_head(hostid)); // stop if loop around
  128. //------------- Period List -------------//
  129. regs->usb_sts = EHCI_INT_MASK_ERROR;
  130. hcd_isr(hostid);
  131. }
  132. void ehci_controller_device_plug(uint8_t hostid, tusb_speed_t speed)
  133. {
  134. ehci_registers_t* const regs = get_operational_register(hostid);
  135. regs->usb_sts_bit.port_change_detect = 1;
  136. regs->portsc_bit.connect_status_change = 1;
  137. regs->portsc_bit.current_connect_status = 1;
  138. regs->portsc_bit.nxp_port_speed = speed;
  139. hcd_isr(hostid);
  140. }
  141. void ehci_controller_device_unplug(uint8_t hostid)
  142. {
  143. ehci_registers_t* const regs = get_operational_register(hostid);
  144. regs->usb_sts_bit.port_change_detect = 1;
  145. regs->portsc_bit.connect_status_change = 1;
  146. regs->portsc_bit.current_connect_status = 0;
  147. }