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.

MassStorageClassDevice.c 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. #define __INCLUDE_FROM_USB_DRIVER
  27. #include "../../Core/USBMode.h"
  28. #if defined(USB_CAN_BE_DEVICE)
  29. #define __INCLUDE_FROM_MS_DRIVER
  30. #define __INCLUDE_FROM_MASSSTORAGE_DEVICE_C
  31. #include "MassStorageClassDevice.h"
  32. void MS_Device_ProcessControlRequest(USB_ClassInfo_MS_Device_t* const MSInterfaceInfo)
  33. {
  34. if (!(Endpoint_IsSETUPReceived()))
  35. return;
  36. if (USB_ControlRequest.wIndex != MSInterfaceInfo->Config.InterfaceNumber)
  37. return;
  38. switch (USB_ControlRequest.bRequest)
  39. {
  40. case MS_REQ_MassStorageReset:
  41. if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
  42. {
  43. Endpoint_ClearSETUP();
  44. Endpoint_ClearStatusStage();
  45. MSInterfaceInfo->State.IsMassStoreReset = true;
  46. }
  47. break;
  48. case MS_REQ_GetMaxLUN:
  49. if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
  50. {
  51. Endpoint_ClearSETUP();
  52. while (!(Endpoint_IsINReady()));
  53. Endpoint_Write_8(MSInterfaceInfo->Config.TotalLUNs - 1);
  54. Endpoint_ClearIN();
  55. Endpoint_ClearStatusStage();
  56. }
  57. break;
  58. }
  59. }
  60. bool MS_Device_ConfigureEndpoints(USB_ClassInfo_MS_Device_t* const MSInterfaceInfo)
  61. {
  62. memset(&MSInterfaceInfo->State, 0x00, sizeof(MSInterfaceInfo->State));
  63. MSInterfaceInfo->Config.DataINEndpoint.Type = EP_TYPE_BULK;
  64. MSInterfaceInfo->Config.DataOUTEndpoint.Type = EP_TYPE_BULK;
  65. if (!(Endpoint_ConfigureEndpointTable(&MSInterfaceInfo->Config.DataINEndpoint, 1)))
  66. return false;
  67. if (!(Endpoint_ConfigureEndpointTable(&MSInterfaceInfo->Config.DataOUTEndpoint, 1)))
  68. return false;
  69. return true;
  70. }
  71. void MS_Device_USBTask(USB_ClassInfo_MS_Device_t* const MSInterfaceInfo)
  72. {
  73. if (USB_DeviceState != DEVICE_STATE_Configured)
  74. return;
  75. Endpoint_SelectEndpoint(MSInterfaceInfo->Config.DataOUTEndpoint.Address);
  76. if (Endpoint_IsOUTReceived())
  77. {
  78. if (MS_Device_ReadInCommandBlock(MSInterfaceInfo))
  79. {
  80. if (MSInterfaceInfo->State.CommandBlock.Flags & MS_COMMAND_DIR_DATA_IN)
  81. Endpoint_SelectEndpoint(MSInterfaceInfo->Config.DataINEndpoint.Address);
  82. bool SCSICommandResult = CALLBACK_MS_Device_SCSICommandReceived(MSInterfaceInfo);
  83. MSInterfaceInfo->State.CommandStatus.Status = (SCSICommandResult) ? MS_SCSI_COMMAND_Pass : MS_SCSI_COMMAND_Fail;
  84. MSInterfaceInfo->State.CommandStatus.Signature = CPU_TO_LE32(MS_CSW_SIGNATURE);
  85. MSInterfaceInfo->State.CommandStatus.Tag = MSInterfaceInfo->State.CommandBlock.Tag;
  86. MSInterfaceInfo->State.CommandStatus.DataTransferResidue = MSInterfaceInfo->State.CommandBlock.DataTransferLength;
  87. if (!(SCSICommandResult) && (le32_to_cpu(MSInterfaceInfo->State.CommandStatus.DataTransferResidue)))
  88. Endpoint_StallTransaction();
  89. MS_Device_ReturnCommandStatus(MSInterfaceInfo);
  90. }
  91. }
  92. if (MSInterfaceInfo->State.IsMassStoreReset)
  93. {
  94. Endpoint_ResetEndpoint(MSInterfaceInfo->Config.DataOUTEndpoint.Address);
  95. Endpoint_ResetEndpoint(MSInterfaceInfo->Config.DataINEndpoint.Address);
  96. Endpoint_SelectEndpoint(MSInterfaceInfo->Config.DataOUTEndpoint.Address);
  97. Endpoint_ClearStall();
  98. Endpoint_ResetDataToggle();
  99. Endpoint_SelectEndpoint(MSInterfaceInfo->Config.DataINEndpoint.Address);
  100. Endpoint_ClearStall();
  101. Endpoint_ResetDataToggle();
  102. MSInterfaceInfo->State.IsMassStoreReset = false;
  103. }
  104. }
  105. static bool MS_Device_ReadInCommandBlock(USB_ClassInfo_MS_Device_t* const MSInterfaceInfo)
  106. {
  107. uint16_t BytesProcessed;
  108. Endpoint_SelectEndpoint(MSInterfaceInfo->Config.DataOUTEndpoint.Address);
  109. BytesProcessed = 0;
  110. while (Endpoint_Read_Stream_LE(&MSInterfaceInfo->State.CommandBlock,
  111. (sizeof(MS_CommandBlockWrapper_t) - 16), &BytesProcessed) ==
  112. ENDPOINT_RWSTREAM_IncompleteTransfer)
  113. {
  114. if (MSInterfaceInfo->State.IsMassStoreReset)
  115. return false;
  116. }
  117. if ((MSInterfaceInfo->State.CommandBlock.Signature != CPU_TO_LE32(MS_CBW_SIGNATURE)) ||
  118. (MSInterfaceInfo->State.CommandBlock.LUN >= MSInterfaceInfo->Config.TotalLUNs) ||
  119. (MSInterfaceInfo->State.CommandBlock.Flags & 0x1F) ||
  120. (MSInterfaceInfo->State.CommandBlock.SCSICommandLength == 0) ||
  121. (MSInterfaceInfo->State.CommandBlock.SCSICommandLength > 16))
  122. {
  123. Endpoint_StallTransaction();
  124. Endpoint_SelectEndpoint(MSInterfaceInfo->Config.DataINEndpoint.Address);
  125. Endpoint_StallTransaction();
  126. return false;
  127. }
  128. BytesProcessed = 0;
  129. while (Endpoint_Read_Stream_LE(&MSInterfaceInfo->State.CommandBlock.SCSICommandData,
  130. MSInterfaceInfo->State.CommandBlock.SCSICommandLength, &BytesProcessed) ==
  131. ENDPOINT_RWSTREAM_IncompleteTransfer)
  132. {
  133. if (MSInterfaceInfo->State.IsMassStoreReset)
  134. return false;
  135. }
  136. Endpoint_ClearOUT();
  137. return true;
  138. }
  139. static void MS_Device_ReturnCommandStatus(USB_ClassInfo_MS_Device_t* const MSInterfaceInfo)
  140. {
  141. Endpoint_SelectEndpoint(MSInterfaceInfo->Config.DataOUTEndpoint.Address);
  142. while (Endpoint_IsStalled())
  143. {
  144. #if !defined(INTERRUPT_CONTROL_ENDPOINT)
  145. USB_USBTask();
  146. #endif
  147. if (MSInterfaceInfo->State.IsMassStoreReset)
  148. return;
  149. }
  150. Endpoint_SelectEndpoint(MSInterfaceInfo->Config.DataINEndpoint.Address);
  151. while (Endpoint_IsStalled())
  152. {
  153. #if !defined(INTERRUPT_CONTROL_ENDPOINT)
  154. USB_USBTask();
  155. #endif
  156. if (MSInterfaceInfo->State.IsMassStoreReset)
  157. return;
  158. }
  159. uint16_t BytesProcessed = 0;
  160. while (Endpoint_Write_Stream_LE(&MSInterfaceInfo->State.CommandStatus,
  161. sizeof(MS_CommandStatusWrapper_t), &BytesProcessed) ==
  162. ENDPOINT_RWSTREAM_IncompleteTransfer)
  163. {
  164. if (MSInterfaceInfo->State.IsMassStoreReset)
  165. return;
  166. }
  167. Endpoint_ClearIN();
  168. }
  169. #endif