Keyboard firmwares for Atmel AVR and Cortex-M
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Host_UC3.c 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /*
  2. LUFA Library
  3. Copyright (C) Dean Camera, 2014.
  4. dean [at] fourwalledcubicle [dot] com
  5. www.lufa-lib.org
  6. */
  7. /*
  8. Copyright 2014 Dean Camera (dean [at] fourwalledcubicle [dot] com)
  9. Permission to use, copy, modify, distribute, and sell this
  10. software and its documentation for any purpose is hereby granted
  11. without fee, provided that the above copyright notice appear in
  12. all copies and that both that the copyright notice and this
  13. permission notice and warranty disclaimer appear in supporting
  14. documentation, and that the name of the author not be used in
  15. advertising or publicity pertaining to distribution of the
  16. software without specific, written prior permission.
  17. The author disclaims all warranties with regard to this
  18. software, including all implied warranties of merchantability
  19. and fitness. In no event shall the author be liable for any
  20. special, indirect or consequential damages or any damages
  21. whatsoever resulting from loss of use, data or profits, whether
  22. in an action of contract, negligence or other tortious action,
  23. arising out of or in connection with the use or performance of
  24. this software.
  25. */
  26. #include "../../../../Common/Common.h"
  27. #if (ARCH == ARCH_UC3)
  28. #define __INCLUDE_FROM_USB_DRIVER
  29. #include "../USBMode.h"
  30. #if defined(USB_CAN_BE_HOST)
  31. #define __INCLUDE_FROM_HOST_C
  32. #include "../Host.h"
  33. void USB_Host_ProcessNextHostState(void)
  34. {
  35. uint8_t ErrorCode = HOST_ENUMERROR_NoError;
  36. uint8_t SubErrorCode = HOST_ENUMERROR_NoError;
  37. static uint16_t WaitMSRemaining;
  38. static uint8_t PostWaitState;
  39. switch (USB_HostState)
  40. {
  41. case HOST_STATE_WaitForDevice:
  42. if (WaitMSRemaining)
  43. {
  44. if ((SubErrorCode = USB_Host_WaitMS(1)) != HOST_WAITERROR_Successful)
  45. {
  46. USB_HostState = PostWaitState;
  47. ErrorCode = HOST_ENUMERROR_WaitStage;
  48. break;
  49. }
  50. if (!(--WaitMSRemaining))
  51. USB_HostState = PostWaitState;
  52. }
  53. break;
  54. case HOST_STATE_Powered:
  55. WaitMSRemaining = HOST_DEVICE_SETTLE_DELAY_MS;
  56. USB_HostState = HOST_STATE_Powered_WaitForDeviceSettle;
  57. break;
  58. case HOST_STATE_Powered_WaitForDeviceSettle:
  59. if (WaitMSRemaining--)
  60. {
  61. Delay_MS(1);
  62. break;
  63. }
  64. else
  65. {
  66. USB_Host_VBUS_Manual_Off();
  67. USB_OTGPAD_On();
  68. USB_Host_VBUS_Auto_Enable();
  69. USB_Host_VBUS_Auto_On();
  70. #if defined(NO_AUTO_VBUS_MANAGEMENT)
  71. USB_Host_VBUS_Manual_Enable();
  72. USB_Host_VBUS_Manual_On();
  73. #endif
  74. USB_HostState = HOST_STATE_Powered_WaitForConnect;
  75. }
  76. break;
  77. case HOST_STATE_Powered_WaitForConnect:
  78. if (USB_INT_HasOccurred(USB_INT_DCONNI))
  79. {
  80. USB_INT_Clear(USB_INT_DCONNI);
  81. USB_INT_Clear(USB_INT_DDISCI);
  82. USB_INT_Clear(USB_INT_VBERRI);
  83. USB_INT_Enable(USB_INT_VBERRI);
  84. USB_Host_ResumeBus();
  85. Pipe_ClearPipes();
  86. HOST_TASK_NONBLOCK_WAIT(100, HOST_STATE_Powered_DoReset);
  87. }
  88. break;
  89. case HOST_STATE_Powered_DoReset:
  90. USB_Host_ResetDevice();
  91. HOST_TASK_NONBLOCK_WAIT(200, HOST_STATE_Powered_ConfigPipe);
  92. break;
  93. case HOST_STATE_Powered_ConfigPipe:
  94. if (!(Pipe_ConfigurePipe(PIPE_CONTROLPIPE, EP_TYPE_CONTROL, ENDPOINT_CONTROLEP, PIPE_CONTROLPIPE_DEFAULT_SIZE, 1)))
  95. {
  96. ErrorCode = HOST_ENUMERROR_PipeConfigError;
  97. SubErrorCode = 0;
  98. break;
  99. }
  100. USB_HostState = HOST_STATE_Default;
  101. break;
  102. case HOST_STATE_Default:
  103. USB_ControlRequest = (USB_Request_Header_t)
  104. {
  105. .bmRequestType = (REQDIR_DEVICETOHOST | REQTYPE_STANDARD | REQREC_DEVICE),
  106. .bRequest = REQ_GetDescriptor,
  107. .wValue = (DTYPE_Device << 8),
  108. .wIndex = 0,
  109. .wLength = 8,
  110. };
  111. uint8_t DataBuffer[8];
  112. Pipe_SelectPipe(PIPE_CONTROLPIPE);
  113. if ((SubErrorCode = USB_Host_SendControlRequest(DataBuffer)) != HOST_SENDCONTROL_Successful)
  114. {
  115. ErrorCode = HOST_ENUMERROR_ControlError;
  116. break;
  117. }
  118. USB_Host_ControlPipeSize = DataBuffer[offsetof(USB_Descriptor_Device_t, Endpoint0Size)];
  119. USB_Host_ResetDevice();
  120. HOST_TASK_NONBLOCK_WAIT(200, HOST_STATE_Default_PostReset);
  121. break;
  122. case HOST_STATE_Default_PostReset:
  123. if (!(Pipe_ConfigurePipe(PIPE_CONTROLPIPE, EP_TYPE_CONTROL, ENDPOINT_CONTROLEP, USB_Host_ControlPipeSize, 1)))
  124. {
  125. ErrorCode = HOST_ENUMERROR_PipeConfigError;
  126. SubErrorCode = 0;
  127. break;
  128. }
  129. USB_ControlRequest = (USB_Request_Header_t)
  130. {
  131. .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_STANDARD | REQREC_DEVICE),
  132. .bRequest = REQ_SetAddress,
  133. .wValue = USB_HOST_DEVICEADDRESS,
  134. .wIndex = 0,
  135. .wLength = 0,
  136. };
  137. if ((SubErrorCode = USB_Host_SendControlRequest(NULL)) != HOST_SENDCONTROL_Successful)
  138. {
  139. ErrorCode = HOST_ENUMERROR_ControlError;
  140. break;
  141. }
  142. HOST_TASK_NONBLOCK_WAIT(100, HOST_STATE_Default_PostAddressSet);
  143. break;
  144. case HOST_STATE_Default_PostAddressSet:
  145. USB_Host_SetDeviceAddress(USB_HOST_DEVICEADDRESS);
  146. USB_HostState = HOST_STATE_Addressed;
  147. EVENT_USB_Host_DeviceEnumerationComplete();
  148. break;
  149. default:
  150. break;
  151. }
  152. if ((ErrorCode != HOST_ENUMERROR_NoError) && (USB_HostState != HOST_STATE_Unattached))
  153. {
  154. EVENT_USB_Host_DeviceEnumerationFailed(ErrorCode, SubErrorCode);
  155. USB_Host_VBUS_Auto_Off();
  156. EVENT_USB_Host_DeviceUnattached();
  157. USB_ResetInterface();
  158. }
  159. }
  160. uint8_t USB_Host_WaitMS(uint8_t MS)
  161. {
  162. bool BusSuspended = USB_Host_IsBusSuspended();
  163. uint8_t ErrorCode = HOST_WAITERROR_Successful;
  164. bool HSOFIEnabled = USB_INT_IsEnabled(USB_INT_HSOFI);
  165. USB_INT_Disable(USB_INT_HSOFI);
  166. USB_INT_Clear(USB_INT_HSOFI);
  167. USB_Host_ResumeBus();
  168. while (MS)
  169. {
  170. if (USB_INT_HasOccurred(USB_INT_HSOFI))
  171. {
  172. USB_INT_Clear(USB_INT_HSOFI);
  173. MS--;
  174. }
  175. if ((USB_HostState == HOST_STATE_Unattached) || (USB_CurrentMode != USB_MODE_Host))
  176. {
  177. ErrorCode = HOST_WAITERROR_DeviceDisconnect;
  178. break;
  179. }
  180. if (Pipe_IsError())
  181. {
  182. Pipe_ClearError();
  183. ErrorCode = HOST_WAITERROR_PipeError;
  184. break;
  185. }
  186. if (Pipe_IsStalled())
  187. {
  188. Pipe_ClearStall();
  189. ErrorCode = HOST_WAITERROR_SetupStalled;
  190. break;
  191. }
  192. }
  193. if (BusSuspended)
  194. USB_Host_SuspendBus();
  195. if (HSOFIEnabled)
  196. USB_INT_Enable(USB_INT_HSOFI);
  197. return ErrorCode;
  198. }
  199. static void USB_Host_ResetDevice(void)
  200. {
  201. bool BusSuspended = USB_Host_IsBusSuspended();
  202. USB_INT_Disable(USB_INT_DDISCI);
  203. USB_Host_ResetBus();
  204. while (!(USB_Host_IsBusResetComplete()));
  205. USB_Host_ResumeBus();
  206. USB_Host_ConfigurationNumber = 0;
  207. bool HSOFIEnabled = USB_INT_IsEnabled(USB_INT_HSOFI);
  208. USB_INT_Disable(USB_INT_HSOFI);
  209. USB_INT_Clear(USB_INT_HSOFI);
  210. for (uint8_t MSRem = 10; MSRem != 0; MSRem--)
  211. {
  212. /* Workaround for powerless-pull-up devices. After a USB bus reset,
  213. all disconnection interrupts are suppressed while a USB frame is
  214. looked for - if it is found within 10ms, the device is still
  215. present. */
  216. if (USB_INT_HasOccurred(USB_INT_HSOFI))
  217. {
  218. USB_INT_Clear(USB_INT_HSOFI);
  219. USB_INT_Clear(USB_INT_DDISCI);
  220. break;
  221. }
  222. Delay_MS(1);
  223. }
  224. if (HSOFIEnabled)
  225. USB_INT_Enable(USB_INT_HSOFI);
  226. if (BusSuspended)
  227. USB_Host_SuspendBus();
  228. USB_INT_Enable(USB_INT_DDISCI);
  229. }
  230. #endif
  231. #endif