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.

RNDISCommands.c 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. /** \file
  27. *
  28. * RNDIS Device commands, to issue RNDIS commands to the device for
  29. * the control and data transfer between the host and RNDIS device.
  30. */
  31. #include "RNDISCommands.h"
  32. /** Current RNDIS Request ID, for associating sent commands with received data */
  33. uint32_t RequestID = 0;
  34. /** Function to send the given encapsulated RNDIS command to the device.
  35. *
  36. * \param[in] Buffer Source command data buffer to send to the device
  37. * \param[in] Length Number of bytes to send
  38. *
  39. * \return A value from the USB_Host_SendControlErrorCodes_t enum
  40. */
  41. uint8_t RNDIS_SendEncapsulatedCommand(void* const Buffer,
  42. const uint16_t Length)
  43. {
  44. USB_ControlRequest = (USB_Request_Header_t)
  45. {
  46. .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE),
  47. .bRequest = RNDIS_REQ_SendEncapsulatedCommand,
  48. .wValue = 0,
  49. .wIndex = 0,
  50. .wLength = Length,
  51. };
  52. /* Select the control pipe for the request transfer */
  53. Pipe_SelectPipe(PIPE_CONTROLPIPE);
  54. return USB_Host_SendControlRequest(Buffer);
  55. }
  56. /** Function to receive the given encapsulated RNDIS response from the device.
  57. *
  58. * \param[out] Buffer Destination command data buffer to write read data from the device to
  59. * \param[in] Length Number of bytes to read
  60. *
  61. * \return A value from the USB_Host_SendControlErrorCodes_t enum
  62. */
  63. uint8_t RNDIS_GetEncapsulatedResponse(void* const Buffer,
  64. const uint16_t Length)
  65. {
  66. USB_ControlRequest = (USB_Request_Header_t)
  67. {
  68. .bmRequestType = (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE),
  69. .bRequest = RNDIS_REQ_GetEncapsulatedResponse,
  70. .wValue = 0,
  71. .wIndex = 0,
  72. .wLength = Length,
  73. };
  74. /* Select the control pipe for the request transfer */
  75. Pipe_SelectPipe(PIPE_CONTROLPIPE);
  76. return USB_Host_SendControlRequest(Buffer);
  77. }
  78. /** Sends a RNDIS KEEPALIVE command to the device, to ensure that it does not enter standby mode after periods
  79. * of long inactivity.
  80. *
  81. * \return A value from the USB_Host_SendControlErrorCodes_t enum or RNDIS_COMMAND_FAILED if the device returned a
  82. * logical command failure
  83. */
  84. uint8_t RNDIS_SendKeepAlive(void)
  85. {
  86. uint8_t ErrorCode;
  87. RNDIS_KeepAlive_Message_t KeepAliveMessage;
  88. RNDIS_KeepAlive_Complete_t KeepAliveMessageResponse;
  89. KeepAliveMessage.MessageType = REMOTE_NDIS_KEEPALIVE_MSG;
  90. KeepAliveMessage.MessageLength = sizeof(RNDIS_KeepAlive_Message_t);
  91. KeepAliveMessage.RequestId = RequestID++;
  92. if ((ErrorCode = RNDIS_SendEncapsulatedCommand(&KeepAliveMessage,
  93. sizeof(RNDIS_KeepAlive_Message_t))) != HOST_SENDCONTROL_Successful)
  94. {
  95. return ErrorCode;
  96. }
  97. if ((ErrorCode = RNDIS_GetEncapsulatedResponse(&KeepAliveMessageResponse,
  98. sizeof(RNDIS_KeepAlive_Complete_t))) != HOST_SENDCONTROL_Successful)
  99. {
  100. return ErrorCode;
  101. }
  102. return HOST_SENDCONTROL_Successful;
  103. }
  104. /** Initializes the attached RNDIS device's RNDIS interface.
  105. *
  106. * \param[in] HostMaxPacketSize Size of the packet buffer on the host
  107. * \param[out] DeviceMaxPacketSize Pointer to where the packet buffer size of the device is to be stored
  108. *
  109. * \return A value from the USB_Host_SendControlErrorCodes_t enum or RNDIS_COMMAND_FAILED if the device returned a
  110. * logical command failure
  111. */
  112. uint8_t RNDIS_InitializeDevice(const uint16_t HostMaxPacketSize,
  113. uint16_t* const DeviceMaxPacketSize)
  114. {
  115. uint8_t ErrorCode;
  116. RNDIS_Initialize_Message_t InitMessage;
  117. RNDIS_Initialize_Complete_t InitMessageResponse;
  118. InitMessage.MessageType = REMOTE_NDIS_INITIALIZE_MSG;
  119. InitMessage.MessageLength = sizeof(RNDIS_Initialize_Message_t);
  120. InitMessage.RequestId = RequestID++;
  121. InitMessage.MajorVersion = REMOTE_NDIS_VERSION_MAJOR;
  122. InitMessage.MinorVersion = REMOTE_NDIS_VERSION_MINOR;
  123. InitMessage.MaxTransferSize = HostMaxPacketSize;
  124. if ((ErrorCode = RNDIS_SendEncapsulatedCommand(&InitMessage,
  125. sizeof(RNDIS_Initialize_Message_t))) != HOST_SENDCONTROL_Successful)
  126. {
  127. return ErrorCode;
  128. }
  129. if ((ErrorCode = RNDIS_GetEncapsulatedResponse(&InitMessageResponse,
  130. sizeof(RNDIS_Initialize_Complete_t))) != HOST_SENDCONTROL_Successful)
  131. {
  132. return ErrorCode;
  133. }
  134. if (InitMessageResponse.Status != REMOTE_NDIS_STATUS_SUCCESS)
  135. return RNDIS_COMMAND_FAILED;
  136. *DeviceMaxPacketSize = InitMessageResponse.MaxTransferSize;
  137. return HOST_SENDCONTROL_Successful;
  138. }
  139. /** Sets a given RNDIS property of an attached RNDIS device.
  140. *
  141. * \param[in] Oid OID number of the parameter to set
  142. * \param[in] Buffer Pointer to where the property data is to be sourced from
  143. * \param[in] Length Length in bytes of the property data to sent to the device
  144. *
  145. * \return A value from the USB_Host_SendControlErrorCodes_t enum or RNDIS_COMMAND_FAILED if the device returned a
  146. * logical command failure
  147. */
  148. uint8_t RNDIS_SetRNDISProperty(const uint32_t Oid,
  149. void* Buffer,
  150. const uint16_t Length)
  151. {
  152. uint8_t ErrorCode;
  153. struct
  154. {
  155. RNDIS_Set_Message_t SetMessage;
  156. uint8_t ContiguousBuffer[Length];
  157. } SetMessageData;
  158. RNDIS_Set_Complete_t SetMessageResponse;
  159. SetMessageData.SetMessage.MessageType = REMOTE_NDIS_SET_MSG;
  160. SetMessageData.SetMessage.MessageLength = sizeof(RNDIS_Set_Message_t) + Length;
  161. SetMessageData.SetMessage.RequestId = RequestID++;
  162. SetMessageData.SetMessage.Oid = Oid;
  163. SetMessageData.SetMessage.InformationBufferLength = Length;
  164. SetMessageData.SetMessage.InformationBufferOffset = (sizeof(RNDIS_Set_Message_t) - sizeof(RNDIS_Message_Header_t));
  165. SetMessageData.SetMessage.DeviceVcHandle = 0;
  166. memcpy(SetMessageData.ContiguousBuffer, Buffer, Length);
  167. if ((ErrorCode = RNDIS_SendEncapsulatedCommand(&SetMessageData,
  168. SetMessageData.SetMessage.MessageLength)) != HOST_SENDCONTROL_Successful)
  169. {
  170. return ErrorCode;
  171. }
  172. if ((ErrorCode = RNDIS_GetEncapsulatedResponse(&SetMessageResponse,
  173. sizeof(RNDIS_Set_Complete_t))) != HOST_SENDCONTROL_Successful)
  174. {
  175. return ErrorCode;
  176. }
  177. if (SetMessageResponse.Status != REMOTE_NDIS_STATUS_SUCCESS)
  178. return RNDIS_COMMAND_FAILED;
  179. return HOST_SENDCONTROL_Successful;
  180. }
  181. /** Gets a given RNDIS property of an attached RNDIS device.
  182. *
  183. * \param[in] Oid OID number of the parameter to get
  184. * \param[in] Buffer Pointer to where the property data is to be written to
  185. * \param[in] MaxLength Length in bytes of the destination buffer size
  186. *
  187. * \return A value from the USB_Host_SendControlErrorCodes_t enum or RNDIS_COMMAND_FAILED if the device returned a
  188. * logical command failure
  189. */
  190. uint8_t RNDIS_QueryRNDISProperty(const uint32_t Oid,
  191. void* Buffer,
  192. const uint16_t MaxLength)
  193. {
  194. uint8_t ErrorCode;
  195. RNDIS_Query_Message_t QueryMessage;
  196. struct
  197. {
  198. RNDIS_Query_Complete_t QueryMessageResponse;
  199. uint8_t ContiguousBuffer[MaxLength];
  200. } QueryMessageResponseData;
  201. QueryMessage.MessageType = REMOTE_NDIS_QUERY_MSG;
  202. QueryMessage.MessageLength = sizeof(RNDIS_Query_Message_t);
  203. QueryMessage.RequestId = RequestID++;
  204. QueryMessage.Oid = Oid;
  205. QueryMessage.InformationBufferLength = 0;
  206. QueryMessage.InformationBufferOffset = 0;
  207. QueryMessage.DeviceVcHandle = 0;
  208. if ((ErrorCode = RNDIS_SendEncapsulatedCommand(&QueryMessage,
  209. sizeof(RNDIS_Query_Message_t))) != HOST_SENDCONTROL_Successful)
  210. {
  211. return ErrorCode;
  212. }
  213. if ((ErrorCode = RNDIS_GetEncapsulatedResponse(&QueryMessageResponseData,
  214. sizeof(QueryMessageResponseData))) != HOST_SENDCONTROL_Successful)
  215. {
  216. return ErrorCode;
  217. }
  218. if (QueryMessageResponseData.QueryMessageResponse.Status != REMOTE_NDIS_STATUS_SUCCESS)
  219. return RNDIS_COMMAND_FAILED;
  220. memcpy(Buffer, &QueryMessageResponseData.ContiguousBuffer, MaxLength);
  221. return HOST_SENDCONTROL_Successful;
  222. }
  223. /** Retrieves the size of a received packet, discarding the remainder of the RNDIS packet header to leave only the
  224. * packet contents for processing by the host.
  225. *
  226. * \param[out] PacketLength Size of the packet currently in the pipe
  227. *
  228. * \return A value from the Pipe_Stream_RW_ErrorCodes_t enum
  229. */
  230. uint8_t RNDIS_GetPacketLength(uint16_t* const PacketLength)
  231. {
  232. uint8_t ErrorCode;
  233. Pipe_SelectPipe(RNDIS_DATA_IN_PIPE);
  234. Pipe_SetPipeToken(PIPE_TOKEN_IN);
  235. Pipe_Unfreeze();
  236. if (!(Pipe_IsReadWriteAllowed()))
  237. {
  238. *PacketLength = 0;
  239. Pipe_Freeze();
  240. return PIPE_RWSTREAM_NoError;
  241. }
  242. RNDIS_Packet_Message_t DeviceMessage;
  243. if ((ErrorCode = Pipe_Read_Stream_LE(&DeviceMessage, sizeof(RNDIS_Packet_Message_t), NULL)) != PIPE_RWSTREAM_NoError)
  244. {
  245. return ErrorCode;
  246. }
  247. *PacketLength = (uint16_t)DeviceMessage.DataLength;
  248. Pipe_Discard_Stream(DeviceMessage.DataOffset - (sizeof(RNDIS_Packet_Message_t) - sizeof(RNDIS_Message_Header_t)),
  249. NULL);
  250. Pipe_Freeze();
  251. return PIPE_RWSTREAM_NoError;
  252. }