PageRenderTime 214ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/avr/LUFA-130303/Demos/Device/LowLevel/KeyboardMouse/Descriptors.c

https://bitbucket.org/idnorton/raspberry-pi
C | 359 lines | 238 code | 49 blank | 72 comment | 4 complexity | dcff7d6c87eddbf02b4f88470dafdde1 MD5 | raw file
  1. /*
  2. LUFA Library
  3. Copyright (C) Dean Camera, 2013.
  4. dean [at] fourwalledcubicle [dot] com
  5. www.lufa-lib.org
  6. */
  7. /*
  8. Copyright 2013 Dean Camera (dean [at] fourwalledcubicle [dot] com)
  9. Copyright 2010 Denver Gingerich (denver [at] ossguy [dot] com)
  10. Permission to use, copy, modify, distribute, and sell this
  11. software and its documentation for any purpose is hereby granted
  12. without fee, provided that the above copyright notice appear in
  13. all copies and that both that the copyright notice and this
  14. permission notice and warranty disclaimer appear in supporting
  15. documentation, and that the name of the author not be used in
  16. advertising or publicity pertaining to distribution of the
  17. software without specific, written prior permission.
  18. The author disclaims all warranties with regard to this
  19. software, including all implied warranties of merchantability
  20. and fitness. In no event shall the author be liable for any
  21. special, indirect or consequential damages or any damages
  22. whatsoever resulting from loss of use, data or profits, whether
  23. in an action of contract, negligence or other tortious action,
  24. arising out of or in connection with the use or performance of
  25. this software.
  26. */
  27. /** \file
  28. *
  29. * USB Device Descriptors, for library use when in USB device mode. Descriptors are special
  30. * computer-readable structures which the host requests upon device enumeration, to determine
  31. * the device's capabilities and functions.
  32. */
  33. #include "Descriptors.h"
  34. /** HID class report descriptor. This is a special descriptor constructed with values from the
  35. * USBIF HID class specification to describe the reports and capabilities of the HID device. This
  36. * descriptor is parsed by the host and its contents used to determine what data (and in what encoding)
  37. * the device will send, and what it may be sent back from the host. Refer to the HID specification for
  38. * more details on HID report descriptors.
  39. *
  40. * This descriptor describes the mouse HID interface's report structure.
  41. */
  42. const USB_Descriptor_HIDReport_Datatype_t PROGMEM MouseReport[] =
  43. {
  44. HID_RI_USAGE_PAGE(8, 0x01), /* Generic Desktop */
  45. HID_RI_USAGE(8, 0x02), /* Mouse */
  46. HID_RI_COLLECTION(8, 0x01), /* Application */
  47. HID_RI_USAGE(8, 0x01), /* Pointer */
  48. HID_RI_COLLECTION(8, 0x00), /* Physical */
  49. HID_RI_USAGE_PAGE(8, 0x09), /* Button */
  50. HID_RI_USAGE_MINIMUM(8, 0x01),
  51. HID_RI_USAGE_MAXIMUM(8, 0x03),
  52. HID_RI_LOGICAL_MINIMUM(8, 0x00),
  53. HID_RI_LOGICAL_MAXIMUM(8, 0x01),
  54. HID_RI_REPORT_COUNT(8, 0x03),
  55. HID_RI_REPORT_SIZE(8, 0x01),
  56. HID_RI_INPUT(8, HID_IOF_DATA | HID_IOF_VARIABLE | HID_IOF_ABSOLUTE),
  57. HID_RI_REPORT_COUNT(8, 0x01),
  58. HID_RI_REPORT_SIZE(8, 0x05),
  59. HID_RI_INPUT(8, HID_IOF_CONSTANT),
  60. HID_RI_USAGE_PAGE(8, 0x01), /* Generic Desktop */
  61. HID_RI_USAGE(8, 0x30), /* Usage X */
  62. HID_RI_USAGE(8, 0x31), /* Usage Y */
  63. HID_RI_LOGICAL_MINIMUM(8, -1),
  64. HID_RI_LOGICAL_MAXIMUM(8, 1),
  65. HID_RI_PHYSICAL_MINIMUM(8, -1),
  66. HID_RI_PHYSICAL_MAXIMUM(8, 1),
  67. HID_RI_REPORT_COUNT(8, 0x02),
  68. HID_RI_REPORT_SIZE(8, 0x08),
  69. HID_RI_INPUT(8, HID_IOF_DATA | HID_IOF_VARIABLE | HID_IOF_RELATIVE),
  70. HID_RI_END_COLLECTION(0),
  71. HID_RI_END_COLLECTION(0),
  72. };
  73. /** Same as the MouseReport structure, but defines the keyboard HID interface's report structure. */
  74. const USB_Descriptor_HIDReport_Datatype_t PROGMEM KeyboardReport[] =
  75. {
  76. HID_RI_USAGE_PAGE(8, 0x01), /* Generic Desktop */
  77. HID_RI_USAGE(8, 0x06), /* Keyboard */
  78. HID_RI_COLLECTION(8, 0x01), /* Application */
  79. HID_RI_USAGE_PAGE(8, 0x07), /* Key Codes */
  80. HID_RI_USAGE_MINIMUM(8, 0xE0), /* Keyboard Left Control */
  81. HID_RI_USAGE_MAXIMUM(8, 0xE7), /* Keyboard Right GUI */
  82. HID_RI_LOGICAL_MINIMUM(8, 0x00),
  83. HID_RI_LOGICAL_MAXIMUM(8, 0x01),
  84. HID_RI_REPORT_SIZE(8, 0x01),
  85. HID_RI_REPORT_COUNT(8, 0x08),
  86. HID_RI_INPUT(8, HID_IOF_DATA | HID_IOF_VARIABLE | HID_IOF_ABSOLUTE),
  87. HID_RI_REPORT_COUNT(8, 0x01),
  88. HID_RI_REPORT_SIZE(8, 0x08),
  89. HID_RI_INPUT(8, HID_IOF_CONSTANT),
  90. HID_RI_USAGE_PAGE(8, 0x08), /* LEDs */
  91. HID_RI_USAGE_MINIMUM(8, 0x01), /* Num Lock */
  92. HID_RI_USAGE_MAXIMUM(8, 0x05), /* Kana */
  93. HID_RI_REPORT_COUNT(8, 0x05),
  94. HID_RI_REPORT_SIZE(8, 0x01),
  95. HID_RI_OUTPUT(8, HID_IOF_DATA | HID_IOF_VARIABLE | HID_IOF_ABSOLUTE | HID_IOF_NON_VOLATILE),
  96. HID_RI_REPORT_COUNT(8, 0x01),
  97. HID_RI_REPORT_SIZE(8, 0x03),
  98. HID_RI_OUTPUT(8, HID_IOF_CONSTANT),
  99. HID_RI_LOGICAL_MINIMUM(8, 0x00),
  100. HID_RI_LOGICAL_MAXIMUM(8, 0x65),
  101. HID_RI_USAGE_PAGE(8, 0x07), /* Keyboard */
  102. HID_RI_USAGE_MINIMUM(8, 0x00), /* Reserved (no event indicated) */
  103. HID_RI_USAGE_MAXIMUM(8, 0x65), /* Keyboard Application */
  104. HID_RI_REPORT_COUNT(8, 0x06),
  105. HID_RI_REPORT_SIZE(8, 0x08),
  106. HID_RI_INPUT(8, HID_IOF_DATA | HID_IOF_ARRAY | HID_IOF_ABSOLUTE),
  107. HID_RI_END_COLLECTION(0),
  108. };
  109. /** Device descriptor structure. This descriptor, located in FLASH memory, describes the overall
  110. * device characteristics, including the supported USB version, control endpoint size and the
  111. * number of device configurations. The descriptor is read out by the USB host when the enumeration
  112. * process begins.
  113. */
  114. const USB_Descriptor_Device_t PROGMEM DeviceDescriptor =
  115. {
  116. .Header = {.Size = sizeof(USB_Descriptor_Device_t), .Type = DTYPE_Device},
  117. .USBSpecification = VERSION_BCD(01.10),
  118. .Class = USB_CSCP_NoDeviceClass,
  119. .SubClass = USB_CSCP_NoDeviceSubclass,
  120. .Protocol = USB_CSCP_NoDeviceProtocol,
  121. .Endpoint0Size = FIXED_CONTROL_ENDPOINT_SIZE,
  122. .VendorID = 0x03EB,
  123. .ProductID = 0x204D,
  124. .ReleaseNumber = VERSION_BCD(00.01),
  125. .ManufacturerStrIndex = 0x01,
  126. .ProductStrIndex = 0x02,
  127. .SerialNumStrIndex = NO_DESCRIPTOR,
  128. .NumberOfConfigurations = FIXED_NUM_CONFIGURATIONS
  129. };
  130. /** Configuration descriptor structure. This descriptor, located in FLASH memory, describes the usage
  131. * of the device in one of its supported configurations, including information about any device interfaces
  132. * and endpoints. The descriptor is read out by the USB host during the enumeration process when selecting
  133. * a configuration so that the host may correctly communicate with the USB device.
  134. */
  135. const USB_Descriptor_Configuration_t PROGMEM ConfigurationDescriptor =
  136. {
  137. .Config =
  138. {
  139. .Header = {.Size = sizeof(USB_Descriptor_Configuration_Header_t), .Type = DTYPE_Configuration},
  140. .TotalConfigurationSize = sizeof(USB_Descriptor_Configuration_t),
  141. .TotalInterfaces = 2,
  142. .ConfigurationNumber = 1,
  143. .ConfigurationStrIndex = NO_DESCRIPTOR,
  144. .ConfigAttributes = (USB_CONFIG_ATTR_RESERVED | USB_CONFIG_ATTR_SELFPOWERED),
  145. .MaxPowerConsumption = USB_CONFIG_POWER_MA(100)
  146. },
  147. .HID1_KeyboardInterface =
  148. {
  149. .Header = {.Size = sizeof(USB_Descriptor_Interface_t), .Type = DTYPE_Interface},
  150. .InterfaceNumber = 0x00,
  151. .AlternateSetting = 0x00,
  152. .TotalEndpoints = 2,
  153. .Class = HID_CSCP_HIDClass,
  154. .SubClass = HID_CSCP_BootSubclass,
  155. .Protocol = HID_CSCP_KeyboardBootProtocol,
  156. .InterfaceStrIndex = NO_DESCRIPTOR
  157. },
  158. .HID1_KeyboardHID =
  159. {
  160. .Header = {.Size = sizeof(USB_HID_Descriptor_HID_t), .Type = HID_DTYPE_HID},
  161. .HIDSpec = VERSION_BCD(01.11),
  162. .CountryCode = 0x00,
  163. .TotalReportDescriptors = 1,
  164. .HIDReportType = HID_DTYPE_Report,
  165. .HIDReportLength = sizeof(KeyboardReport)
  166. },
  167. .HID1_ReportINEndpoint =
  168. {
  169. .Header = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint},
  170. .EndpointAddress = KEYBOARD_IN_EPADDR,
  171. .Attributes = (EP_TYPE_INTERRUPT | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA),
  172. .EndpointSize = HID_EPSIZE,
  173. .PollingIntervalMS = 0x05
  174. },
  175. .HID1_ReportOUTEndpoint =
  176. {
  177. .Header = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint},
  178. .EndpointAddress = KEYBOARD_OUT_EPADDR,
  179. .Attributes = (EP_TYPE_INTERRUPT | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA),
  180. .EndpointSize = HID_EPSIZE,
  181. .PollingIntervalMS = 0x05
  182. },
  183. .HID2_MouseInterface =
  184. {
  185. .Header = {.Size = sizeof(USB_Descriptor_Interface_t), .Type = DTYPE_Interface},
  186. .InterfaceNumber = 0x01,
  187. .AlternateSetting = 0x00,
  188. .TotalEndpoints = 1,
  189. .Class = HID_CSCP_HIDClass,
  190. .SubClass = HID_CSCP_BootSubclass,
  191. .Protocol = HID_CSCP_MouseBootProtocol,
  192. .InterfaceStrIndex = NO_DESCRIPTOR
  193. },
  194. .HID2_MouseHID =
  195. {
  196. .Header = {.Size = sizeof(USB_HID_Descriptor_HID_t), .Type = HID_DTYPE_HID},
  197. .HIDSpec = VERSION_BCD(01.11),
  198. .CountryCode = 0x00,
  199. .TotalReportDescriptors = 1,
  200. .HIDReportType = HID_DTYPE_Report,
  201. .HIDReportLength = sizeof(MouseReport)
  202. },
  203. .HID2_ReportINEndpoint =
  204. {
  205. .Header = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint},
  206. .EndpointAddress = MOUSE_IN_EPADDR,
  207. .Attributes = (EP_TYPE_INTERRUPT | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA),
  208. .EndpointSize = HID_EPSIZE,
  209. .PollingIntervalMS = 0x05
  210. }
  211. };
  212. /** Language descriptor structure. This descriptor, located in FLASH memory, is returned when the host requests
  213. * the string descriptor with index 0 (the first index). It is actually an array of 16-bit integers, which indicate
  214. * via the language ID table available at USB.org what languages the device supports for its string descriptors.
  215. */
  216. const USB_Descriptor_String_t PROGMEM LanguageString =
  217. {
  218. .Header = {.Size = USB_STRING_LEN(1), .Type = DTYPE_String},
  219. .UnicodeString = {LANGUAGE_ID_ENG}
  220. };
  221. /** Manufacturer descriptor string. This is a Unicode string containing the manufacturer's details in human readable
  222. * form, and is read out upon request by the host when the appropriate string ID is requested, listed in the Device
  223. * Descriptor.
  224. */
  225. const USB_Descriptor_String_t PROGMEM ManufacturerString =
  226. {
  227. .Header = {.Size = USB_STRING_LEN(11), .Type = DTYPE_String},
  228. .UnicodeString = L"Dean Camera"
  229. };
  230. /** Product descriptor string. This is a Unicode string containing the product's details in human readable form,
  231. * and is read out upon request by the host when the appropriate string ID is requested, listed in the Device
  232. * Descriptor.
  233. */
  234. const USB_Descriptor_String_t PROGMEM ProductString =
  235. {
  236. .Header = {.Size = USB_STRING_LEN(28), .Type = DTYPE_String},
  237. .UnicodeString = L"LUFA Mouse and Keyboard Demo"
  238. };
  239. /** This function is called by the library when in device mode, and must be overridden (see library "USB Descriptors"
  240. * documentation) by the application code so that the address and size of a requested descriptor can be given
  241. * to the USB library. When the device receives a Get Descriptor request on the control endpoint, this function
  242. * is called so that the descriptor details can be passed back and the appropriate descriptor sent back to the
  243. * USB host.
  244. */
  245. uint16_t CALLBACK_USB_GetDescriptor(const uint16_t wValue,
  246. const uint8_t wIndex,
  247. const void** const DescriptorAddress)
  248. {
  249. const uint8_t DescriptorType = (wValue >> 8);
  250. const uint8_t DescriptorNumber = (wValue & 0xFF);
  251. const void* Address = NULL;
  252. uint16_t Size = NO_DESCRIPTOR;
  253. switch (DescriptorType)
  254. {
  255. case DTYPE_Device:
  256. Address = &DeviceDescriptor;
  257. Size = sizeof(USB_Descriptor_Device_t);
  258. break;
  259. case DTYPE_Configuration:
  260. Address = &ConfigurationDescriptor;
  261. Size = sizeof(USB_Descriptor_Configuration_t);
  262. break;
  263. case DTYPE_String:
  264. switch (DescriptorNumber)
  265. {
  266. case 0x00:
  267. Address = &LanguageString;
  268. Size = pgm_read_byte(&LanguageString.Header.Size);
  269. break;
  270. case 0x01:
  271. Address = &ManufacturerString;
  272. Size = pgm_read_byte(&ManufacturerString.Header.Size);
  273. break;
  274. case 0x02:
  275. Address = &ProductString;
  276. Size = pgm_read_byte(&ProductString.Header.Size);
  277. break;
  278. }
  279. break;
  280. case HID_DTYPE_HID:
  281. if (!(wIndex))
  282. {
  283. Address = &ConfigurationDescriptor.HID1_KeyboardHID;
  284. Size = sizeof(USB_HID_Descriptor_HID_t);
  285. }
  286. else
  287. {
  288. Address = &ConfigurationDescriptor.HID2_MouseHID;
  289. Size = sizeof(USB_HID_Descriptor_HID_t);
  290. }
  291. break;
  292. case HID_DTYPE_Report:
  293. if (!(wIndex))
  294. {
  295. Address = &KeyboardReport;
  296. Size = sizeof(KeyboardReport);
  297. }
  298. else
  299. {
  300. Address = &MouseReport;
  301. Size = sizeof(MouseReport);
  302. }
  303. break;
  304. }
  305. *DescriptorAddress = Address;
  306. return Size;
  307. }