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.

DeviceStandardReq.c 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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 "USBMode.h"
  28. #if defined(USB_CAN_BE_DEVICE)
  29. #define __INCLUDE_FROM_DEVICESTDREQ_C
  30. #include "DeviceStandardReq.h"
  31. uint8_t USB_Device_ConfigurationNumber;
  32. #if !defined(NO_DEVICE_SELF_POWER)
  33. bool USB_Device_CurrentlySelfPowered;
  34. #endif
  35. #if !defined(NO_DEVICE_REMOTE_WAKEUP)
  36. bool USB_Device_RemoteWakeupEnabled;
  37. #endif
  38. void USB_Device_ProcessControlRequest(void)
  39. {
  40. #if defined(ARCH_BIG_ENDIAN)
  41. USB_ControlRequest.bmRequestType = Endpoint_Read_8();
  42. USB_ControlRequest.bRequest = Endpoint_Read_8();
  43. USB_ControlRequest.wValue = Endpoint_Read_16_LE();
  44. USB_ControlRequest.wIndex = Endpoint_Read_16_LE();
  45. USB_ControlRequest.wLength = Endpoint_Read_16_LE();
  46. #else
  47. uint8_t* RequestHeader = (uint8_t*)&USB_ControlRequest;
  48. for (uint8_t RequestHeaderByte = 0; RequestHeaderByte < sizeof(USB_Request_Header_t); RequestHeaderByte++)
  49. *(RequestHeader++) = Endpoint_Read_8();
  50. #endif
  51. EVENT_USB_Device_ControlRequest();
  52. if (Endpoint_IsSETUPReceived())
  53. {
  54. uint8_t bmRequestType = USB_ControlRequest.bmRequestType;
  55. switch (USB_ControlRequest.bRequest)
  56. {
  57. case REQ_GetStatus:
  58. if ((bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_STANDARD | REQREC_DEVICE)) ||
  59. (bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_STANDARD | REQREC_ENDPOINT)))
  60. {
  61. USB_Device_GetStatus();
  62. }
  63. break;
  64. case REQ_ClearFeature:
  65. case REQ_SetFeature:
  66. if ((bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_STANDARD | REQREC_DEVICE)) ||
  67. (bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_STANDARD | REQREC_ENDPOINT)))
  68. {
  69. USB_Device_ClearSetFeature();
  70. }
  71. break;
  72. case REQ_SetAddress:
  73. if (bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_STANDARD | REQREC_DEVICE))
  74. USB_Device_SetAddress();
  75. break;
  76. case REQ_GetDescriptor:
  77. if ((bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_STANDARD | REQREC_DEVICE)) ||
  78. (bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_STANDARD | REQREC_INTERFACE)))
  79. {
  80. USB_Device_GetDescriptor();
  81. }
  82. break;
  83. case REQ_GetConfiguration:
  84. if (bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_STANDARD | REQREC_DEVICE))
  85. USB_Device_GetConfiguration();
  86. break;
  87. case REQ_SetConfiguration:
  88. if (bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_STANDARD | REQREC_DEVICE))
  89. USB_Device_SetConfiguration();
  90. break;
  91. default:
  92. break;
  93. }
  94. }
  95. if (Endpoint_IsSETUPReceived())
  96. {
  97. Endpoint_ClearSETUP();
  98. Endpoint_StallTransaction();
  99. }
  100. }
  101. static void USB_Device_SetAddress(void)
  102. {
  103. uint8_t DeviceAddress = (USB_ControlRequest.wValue & 0x7F);
  104. USB_Device_SetDeviceAddress(DeviceAddress);
  105. Endpoint_ClearSETUP();
  106. Endpoint_ClearStatusStage();
  107. while (!(Endpoint_IsINReady()));
  108. USB_Device_EnableDeviceAddress(DeviceAddress);
  109. USB_DeviceState = (DeviceAddress) ? DEVICE_STATE_Addressed : DEVICE_STATE_Default;
  110. }
  111. static void USB_Device_SetConfiguration(void)
  112. {
  113. #if defined(FIXED_NUM_CONFIGURATIONS)
  114. if ((uint8_t)USB_ControlRequest.wValue > FIXED_NUM_CONFIGURATIONS)
  115. return;
  116. #else
  117. USB_Descriptor_Device_t* DevDescriptorPtr;
  118. #if defined(ARCH_HAS_MULTI_ADDRESS_SPACE)
  119. #if defined(USE_FLASH_DESCRIPTORS)
  120. #define MemoryAddressSpace MEMSPACE_FLASH
  121. #elif defined(USE_EEPROM_DESCRIPTORS)
  122. #define MemoryAddressSpace MEMSPACE_EEPROM
  123. #elif defined(USE_SRAM_DESCRIPTORS)
  124. #define MemoryAddressSpace MEMSPACE_SRAM
  125. #else
  126. uint8_t MemoryAddressSpace;
  127. #endif
  128. #endif
  129. if (CALLBACK_USB_GetDescriptor((DTYPE_Device << 8), 0, (void*)&DevDescriptorPtr
  130. #if defined(ARCH_HAS_MULTI_ADDRESS_SPACE) && \
  131. !(defined(USE_FLASH_DESCRIPTORS) || defined(USE_EEPROM_DESCRIPTORS) || defined(USE_RAM_DESCRIPTORS))
  132. , &MemoryAddressSpace
  133. #endif
  134. ) == NO_DESCRIPTOR)
  135. {
  136. return;
  137. }
  138. #if defined(ARCH_HAS_MULTI_ADDRESS_SPACE)
  139. if (MemoryAddressSpace == MEMSPACE_FLASH)
  140. {
  141. if (((uint8_t)USB_ControlRequest.wValue > pgm_read_byte(&DevDescriptorPtr->NumberOfConfigurations)))
  142. return;
  143. }
  144. else if (MemoryAddressSpace == MEMSPACE_EEPROM)
  145. {
  146. if (((uint8_t)USB_ControlRequest.wValue > eeprom_read_byte(&DevDescriptorPtr->NumberOfConfigurations)))
  147. return;
  148. }
  149. else
  150. {
  151. if ((uint8_t)USB_ControlRequest.wValue > DevDescriptorPtr->NumberOfConfigurations)
  152. return;
  153. }
  154. #else
  155. if ((uint8_t)USB_ControlRequest.wValue > DevDescriptorPtr->NumberOfConfigurations)
  156. return;
  157. #endif
  158. #endif
  159. Endpoint_ClearSETUP();
  160. USB_Device_ConfigurationNumber = (uint8_t)USB_ControlRequest.wValue;
  161. Endpoint_ClearStatusStage();
  162. if (USB_Device_ConfigurationNumber)
  163. USB_DeviceState = DEVICE_STATE_Configured;
  164. else
  165. USB_DeviceState = (USB_Device_IsAddressSet()) ? DEVICE_STATE_Configured : DEVICE_STATE_Powered;
  166. EVENT_USB_Device_ConfigurationChanged();
  167. }
  168. static void USB_Device_GetConfiguration(void)
  169. {
  170. Endpoint_ClearSETUP();
  171. Endpoint_Write_8(USB_Device_ConfigurationNumber);
  172. Endpoint_ClearIN();
  173. Endpoint_ClearStatusStage();
  174. }
  175. #if !defined(NO_INTERNAL_SERIAL) && (USE_INTERNAL_SERIAL != NO_DESCRIPTOR)
  176. static void USB_Device_GetInternalSerialDescriptor(void)
  177. {
  178. struct
  179. {
  180. USB_Descriptor_Header_t Header;
  181. uint16_t UnicodeString[INTERNAL_SERIAL_LENGTH_BITS / 4];
  182. } SignatureDescriptor;
  183. SignatureDescriptor.Header.Type = DTYPE_String;
  184. SignatureDescriptor.Header.Size = USB_STRING_LEN(INTERNAL_SERIAL_LENGTH_BITS / 4);
  185. USB_Device_GetSerialString(SignatureDescriptor.UnicodeString);
  186. Endpoint_ClearSETUP();
  187. Endpoint_Write_Control_Stream_LE(&SignatureDescriptor, sizeof(SignatureDescriptor));
  188. Endpoint_ClearOUT();
  189. }
  190. #endif
  191. static void USB_Device_GetDescriptor(void)
  192. {
  193. const void* DescriptorPointer;
  194. uint16_t DescriptorSize;
  195. #if defined(ARCH_HAS_MULTI_ADDRESS_SPACE) && \
  196. !(defined(USE_FLASH_DESCRIPTORS) || defined(USE_EEPROM_DESCRIPTORS) || defined(USE_RAM_DESCRIPTORS))
  197. uint8_t DescriptorAddressSpace;
  198. #endif
  199. #if !defined(NO_INTERNAL_SERIAL) && (USE_INTERNAL_SERIAL != NO_DESCRIPTOR)
  200. if (USB_ControlRequest.wValue == ((DTYPE_String << 8) | USE_INTERNAL_SERIAL))
  201. {
  202. USB_Device_GetInternalSerialDescriptor();
  203. return;
  204. }
  205. #endif
  206. if ((DescriptorSize = CALLBACK_USB_GetDescriptor(USB_ControlRequest.wValue, USB_ControlRequest.wIndex,
  207. &DescriptorPointer
  208. #if defined(ARCH_HAS_MULTI_ADDRESS_SPACE) && \
  209. !(defined(USE_FLASH_DESCRIPTORS) || defined(USE_EEPROM_DESCRIPTORS) || defined(USE_RAM_DESCRIPTORS))
  210. , &DescriptorAddressSpace
  211. #endif
  212. )) == NO_DESCRIPTOR)
  213. {
  214. return;
  215. }
  216. Endpoint_ClearSETUP();
  217. #if defined(USE_RAM_DESCRIPTORS) || !defined(ARCH_HAS_MULTI_ADDRESS_SPACE)
  218. Endpoint_Write_Control_Stream_LE(DescriptorPointer, DescriptorSize);
  219. #elif defined(USE_EEPROM_DESCRIPTORS)
  220. Endpoint_Write_Control_EStream_LE(DescriptorPointer, DescriptorSize);
  221. #elif defined(USE_FLASH_DESCRIPTORS)
  222. Endpoint_Write_Control_PStream_LE(DescriptorPointer, DescriptorSize);
  223. #else
  224. if (DescriptorAddressSpace == MEMSPACE_FLASH)
  225. Endpoint_Write_Control_PStream_LE(DescriptorPointer, DescriptorSize);
  226. else if (DescriptorAddressSpace == MEMSPACE_EEPROM)
  227. Endpoint_Write_Control_EStream_LE(DescriptorPointer, DescriptorSize);
  228. else
  229. Endpoint_Write_Control_Stream_LE(DescriptorPointer, DescriptorSize);
  230. #endif
  231. Endpoint_ClearOUT();
  232. }
  233. static void USB_Device_GetStatus(void)
  234. {
  235. uint8_t CurrentStatus = 0;
  236. switch (USB_ControlRequest.bmRequestType)
  237. {
  238. case (REQDIR_DEVICETOHOST | REQTYPE_STANDARD | REQREC_DEVICE):
  239. #if !defined(NO_DEVICE_SELF_POWER)
  240. if (USB_Device_CurrentlySelfPowered)
  241. CurrentStatus |= FEATURE_SELFPOWERED_ENABLED;
  242. #endif
  243. #if !defined(NO_DEVICE_REMOTE_WAKEUP)
  244. if (USB_Device_RemoteWakeupEnabled)
  245. CurrentStatus |= FEATURE_REMOTE_WAKEUP_ENABLED;
  246. #endif
  247. break;
  248. case (REQDIR_DEVICETOHOST | REQTYPE_STANDARD | REQREC_ENDPOINT):
  249. #if !defined(CONTROL_ONLY_DEVICE)
  250. Endpoint_SelectEndpoint((uint8_t)USB_ControlRequest.wIndex & ENDPOINT_EPNUM_MASK);
  251. CurrentStatus = Endpoint_IsStalled();
  252. Endpoint_SelectEndpoint(ENDPOINT_CONTROLEP);
  253. #endif
  254. break;
  255. default:
  256. return;
  257. }
  258. Endpoint_ClearSETUP();
  259. Endpoint_Write_16_LE(CurrentStatus);
  260. Endpoint_ClearIN();
  261. Endpoint_ClearStatusStage();
  262. }
  263. static void USB_Device_ClearSetFeature(void)
  264. {
  265. switch (USB_ControlRequest.bmRequestType & CONTROL_REQTYPE_RECIPIENT)
  266. {
  267. #if !defined(NO_DEVICE_REMOTE_WAKEUP)
  268. case REQREC_DEVICE:
  269. if ((uint8_t)USB_ControlRequest.wValue == FEATURE_SEL_DeviceRemoteWakeup)
  270. USB_Device_RemoteWakeupEnabled = (USB_ControlRequest.bRequest == REQ_SetFeature);
  271. else
  272. return;
  273. break;
  274. #endif
  275. #if !defined(CONTROL_ONLY_DEVICE)
  276. case REQREC_ENDPOINT:
  277. if ((uint8_t)USB_ControlRequest.wValue == FEATURE_SEL_EndpointHalt)
  278. {
  279. uint8_t EndpointIndex = ((uint8_t)USB_ControlRequest.wIndex & ENDPOINT_EPNUM_MASK);
  280. if (EndpointIndex == ENDPOINT_CONTROLEP)
  281. return;
  282. Endpoint_SelectEndpoint(EndpointIndex);
  283. if (Endpoint_IsEnabled())
  284. {
  285. if (USB_ControlRequest.bRequest == REQ_SetFeature)
  286. {
  287. Endpoint_StallTransaction();
  288. }
  289. else
  290. {
  291. Endpoint_ClearStall();
  292. Endpoint_ResetEndpoint(EndpointIndex);
  293. Endpoint_ResetDataToggle();
  294. }
  295. }
  296. }
  297. break;
  298. #endif
  299. default:
  300. return;
  301. }
  302. Endpoint_SelectEndpoint(ENDPOINT_CONTROLEP);
  303. Endpoint_ClearSETUP();
  304. Endpoint_ClearStatusStage();
  305. }
  306. #endif