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.

USBHID.cpp 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /* Copyright (c) 2010-2011 mbed.org, MIT License
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
  4. * and associated documentation files (the "Software"), to deal in the Software without
  5. * restriction, including without limitation the rights to use, copy, modify, merge, publish,
  6. * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
  7. * Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or
  10. * substantial portions of the Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
  13. * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  14. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  15. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  17. */
  18. #include "stdint.h"
  19. #include "USBHAL.h"
  20. #include "USBHID.h"
  21. USBHID::USBHID(uint8_t output_report_length, uint8_t input_report_length, uint16_t vendor_id, uint16_t product_id, uint16_t product_release, bool connect): USBDevice(vendor_id, product_id, product_release)
  22. {
  23. output_length = output_report_length;
  24. input_length = input_report_length;
  25. if(connect) {
  26. USBDevice::connect();
  27. }
  28. }
  29. bool USBHID::send(HID_REPORT *report)
  30. {
  31. return write(EPINT_IN, report->data, report->length, MAX_HID_REPORT_SIZE);
  32. }
  33. bool USBHID::sendNB(HID_REPORT *report)
  34. {
  35. return writeNB(EPINT_IN, report->data, report->length, MAX_HID_REPORT_SIZE);
  36. }
  37. bool USBHID::read(HID_REPORT *report)
  38. {
  39. uint32_t bytesRead = 0;
  40. bool result;
  41. result = USBDevice::readEP(EPINT_OUT, report->data, &bytesRead, MAX_HID_REPORT_SIZE);
  42. if(!readStart(EPINT_OUT, MAX_HID_REPORT_SIZE))
  43. return false;
  44. report->length = bytesRead;
  45. return result;
  46. }
  47. bool USBHID::readNB(HID_REPORT *report)
  48. {
  49. uint32_t bytesRead = 0;
  50. bool result;
  51. result = USBDevice::readEP_NB(EPINT_OUT, report->data, &bytesRead, MAX_HID_REPORT_SIZE);
  52. // if readEP_NB did not succeed, does not issue a readStart
  53. if (!result)
  54. return false;
  55. report->length = bytesRead;
  56. if(!readStart(EPINT_OUT, MAX_HID_REPORT_SIZE))
  57. return false;
  58. return result;
  59. }
  60. uint16_t USBHID::reportDescLength() {
  61. reportDesc();
  62. return reportLength;
  63. }
  64. //
  65. // Route callbacks from lower layers to class(es)
  66. //
  67. // Called in ISR context
  68. // Called by USBDevice on Endpoint0 request
  69. // This is used to handle extensions to standard requests
  70. // and class specific requests
  71. // Return true if class handles this request
  72. bool USBHID::USBCallback_request() {
  73. bool success = false;
  74. CONTROL_TRANSFER * transfer = getTransferPtr();
  75. uint8_t *hidDescriptor;
  76. // Process additional standard requests
  77. if ((transfer->setup.bmRequestType.Type == STANDARD_TYPE))
  78. {
  79. switch (transfer->setup.bRequest)
  80. {
  81. case GET_DESCRIPTOR:
  82. switch (DESCRIPTOR_TYPE(transfer->setup.wValue))
  83. {
  84. case REPORT_DESCRIPTOR:
  85. if ((reportDesc() != NULL) \
  86. && (reportDescLength() != 0))
  87. {
  88. transfer->remaining = reportDescLength();
  89. transfer->ptr = reportDesc();
  90. transfer->direction = DEVICE_TO_HOST;
  91. success = true;
  92. }
  93. break;
  94. case HID_DESCRIPTOR:
  95. // Find the HID descriptor, after the configuration descriptor
  96. hidDescriptor = findDescriptor(HID_DESCRIPTOR);
  97. if (hidDescriptor != NULL)
  98. {
  99. transfer->remaining = HID_DESCRIPTOR_LENGTH;
  100. transfer->ptr = hidDescriptor;
  101. transfer->direction = DEVICE_TO_HOST;
  102. success = true;
  103. }
  104. break;
  105. default:
  106. break;
  107. }
  108. break;
  109. default:
  110. break;
  111. }
  112. }
  113. // Process class-specific requests
  114. if (transfer->setup.bmRequestType.Type == CLASS_TYPE)
  115. {
  116. switch (transfer->setup.bRequest)
  117. {
  118. case SET_REPORT:
  119. // First byte will be used for report ID
  120. outputReport.data[0] = transfer->setup.wValue & 0xff;
  121. outputReport.length = transfer->setup.wLength + 1;
  122. transfer->remaining = sizeof(outputReport.data) - 1;
  123. transfer->ptr = &outputReport.data[1];
  124. transfer->direction = HOST_TO_DEVICE;
  125. transfer->notify = true;
  126. success = true;
  127. default:
  128. break;
  129. }
  130. }
  131. return success;
  132. }
  133. #define DEFAULT_CONFIGURATION (1)
  134. // Called in ISR context
  135. // Set configuration. Return false if the
  136. // configuration is not supported
  137. bool USBHID::USBCallback_setConfiguration(uint8_t configuration) {
  138. if (configuration != DEFAULT_CONFIGURATION) {
  139. return false;
  140. }
  141. // Configure endpoints > 0
  142. addEndpoint(EPINT_IN, MAX_PACKET_SIZE_EPINT);
  143. addEndpoint(EPINT_OUT, MAX_PACKET_SIZE_EPINT);
  144. // We activate the endpoint to be able to recceive data
  145. readStart(EPINT_OUT, MAX_PACKET_SIZE_EPINT);
  146. return true;
  147. }
  148. uint8_t * USBHID::stringIinterfaceDesc() {
  149. static uint8_t stringIinterfaceDescriptor[] = {
  150. 0x08, //bLength
  151. STRING_DESCRIPTOR, //bDescriptorType 0x03
  152. 'H',0,'I',0,'D',0, //bString iInterface - HID
  153. };
  154. return stringIinterfaceDescriptor;
  155. }
  156. uint8_t * USBHID::stringIproductDesc() {
  157. static uint8_t stringIproductDescriptor[] = {
  158. 0x16, //bLength
  159. STRING_DESCRIPTOR, //bDescriptorType 0x03
  160. 'H',0,'I',0,'D',0,' ',0,'D',0,'E',0,'V',0,'I',0,'C',0,'E',0 //bString iProduct - HID device
  161. };
  162. return stringIproductDescriptor;
  163. }
  164. uint8_t * USBHID::reportDesc() {
  165. static uint8_t reportDescriptor[] = {
  166. 0x06, LSB(0xFFAB), MSB(0xFFAB),
  167. 0x0A, LSB(0x0200), MSB(0x0200),
  168. 0xA1, 0x01, // Collection 0x01
  169. 0x75, 0x08, // report size = 8 bits
  170. 0x15, 0x00, // logical minimum = 0
  171. 0x26, 0xFF, 0x00, // logical maximum = 255
  172. 0x95, input_length, // report count
  173. 0x09, 0x01, // usage
  174. 0x81, 0x02, // Input (array)
  175. 0x95, output_length,// report count
  176. 0x09, 0x02, // usage
  177. 0x91, 0x02, // Output (array)
  178. 0xC0 // end collection
  179. };
  180. reportLength = sizeof(reportDescriptor);
  181. return reportDescriptor;
  182. }
  183. #define DEFAULT_CONFIGURATION (1)
  184. #define TOTAL_DESCRIPTOR_LENGTH ((1 * CONFIGURATION_DESCRIPTOR_LENGTH) \
  185. + (1 * INTERFACE_DESCRIPTOR_LENGTH) \
  186. + (1 * HID_DESCRIPTOR_LENGTH) \
  187. + (2 * ENDPOINT_DESCRIPTOR_LENGTH))
  188. uint8_t * USBHID::configurationDesc() {
  189. static uint8_t configurationDescriptor[] = {
  190. CONFIGURATION_DESCRIPTOR_LENGTH,// bLength
  191. CONFIGURATION_DESCRIPTOR, // bDescriptorType
  192. LSB(TOTAL_DESCRIPTOR_LENGTH), // wTotalLength (LSB)
  193. MSB(TOTAL_DESCRIPTOR_LENGTH), // wTotalLength (MSB)
  194. 0x01, // bNumInterfaces
  195. DEFAULT_CONFIGURATION, // bConfigurationValue
  196. 0x00, // iConfiguration
  197. C_RESERVED | C_SELF_POWERED, // bmAttributes
  198. C_POWER(0), // bMaxPower
  199. INTERFACE_DESCRIPTOR_LENGTH, // bLength
  200. INTERFACE_DESCRIPTOR, // bDescriptorType
  201. 0x00, // bInterfaceNumber
  202. 0x00, // bAlternateSetting
  203. 0x02, // bNumEndpoints
  204. HID_CLASS, // bInterfaceClass
  205. HID_SUBCLASS_NONE, // bInterfaceSubClass
  206. HID_PROTOCOL_NONE, // bInterfaceProtocol
  207. 0x00, // iInterface
  208. HID_DESCRIPTOR_LENGTH, // bLength
  209. HID_DESCRIPTOR, // bDescriptorType
  210. LSB(HID_VERSION_1_11), // bcdHID (LSB)
  211. MSB(HID_VERSION_1_11), // bcdHID (MSB)
  212. 0x00, // bCountryCode
  213. 0x01, // bNumDescriptors
  214. REPORT_DESCRIPTOR, // bDescriptorType
  215. (uint8_t)(LSB(this->reportDescLength())), // wDescriptorLength (LSB)
  216. (uint8_t)(MSB(this->reportDescLength())), // wDescriptorLength (MSB)
  217. ENDPOINT_DESCRIPTOR_LENGTH, // bLength
  218. ENDPOINT_DESCRIPTOR, // bDescriptorType
  219. PHY_TO_DESC(EPINT_IN), // bEndpointAddress
  220. E_INTERRUPT, // bmAttributes
  221. LSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (LSB)
  222. MSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (MSB)
  223. 1, // bInterval (milliseconds)
  224. ENDPOINT_DESCRIPTOR_LENGTH, // bLength
  225. ENDPOINT_DESCRIPTOR, // bDescriptorType
  226. PHY_TO_DESC(EPINT_OUT), // bEndpointAddress
  227. E_INTERRUPT, // bmAttributes
  228. LSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (LSB)
  229. MSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (MSB)
  230. 1, // bInterval (milliseconds)
  231. };
  232. return configurationDescriptor;
  233. }