usb_descriptors.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * Copyright (c) 2020, Alex Taradov <alex@taradov.com>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  20. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  21. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  22. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  23. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  24. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  25. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  26. * POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. /*- Includes ----------------------------------------------------------------*/
  29. #include <stdalign.h>
  30. #include "usb.h"
  31. #include "usb_descriptors.h"
  32. /*- Variables ---------------------------------------------------------------*/
  33. const alignas(4) usb_device_descriptor_t usb_device_descriptor =
  34. {
  35. .bLength = sizeof(usb_device_descriptor_t),
  36. .bDescriptorType = USB_DEVICE_DESCRIPTOR,
  37. .bcdUSB = 0x0200,
  38. .bDeviceClass = 0x00,
  39. .bDeviceSubClass = 0x00,
  40. .bDeviceProtocol = 0x00,
  41. .bMaxPacketSize0 = 64,
  42. .idVendor = 0x6666,
  43. .idProduct = 0x1111,
  44. .bcdDevice = 0x0100,
  45. .iManufacturer = USB_STR_MANUFACTURER,
  46. .iProduct = USB_STR_PRODUCT,
  47. .iSerialNumber = USB_STR_SERIAL_NUMBER,
  48. .bNumConfigurations = 1,
  49. };
  50. const alignas(4) usb_configuration_hierarchy_t usb_configuration_hierarchy =
  51. {
  52. .configuration =
  53. {
  54. .bLength = sizeof(usb_configuration_descriptor_t),
  55. .bDescriptorType = USB_CONFIGURATION_DESCRIPTOR,
  56. .wTotalLength = sizeof(usb_configuration_hierarchy_t),
  57. .bNumInterfaces = 1,
  58. .bConfigurationValue = 1,
  59. .iConfiguration = 0,
  60. .bmAttributes = 0x80,
  61. .bMaxPower = 250, // 500 mA
  62. },
  63. .hid_interface =
  64. {
  65. .bLength = sizeof(usb_interface_descriptor_t),
  66. .bDescriptorType = USB_INTERFACE_DESCRIPTOR,
  67. .bInterfaceNumber = 0,
  68. .bAlternateSetting = 0,
  69. .bNumEndpoints = 2,
  70. .bInterfaceClass = USB_HID_DEVICE_CLASS,
  71. .bInterfaceSubClass = 0,
  72. .bInterfaceProtocol = 0,
  73. .iInterface = 0,
  74. },
  75. .hid =
  76. {
  77. .bLength = sizeof(usb_hid_descriptor_t),
  78. .bDescriptorType = USB_HID_DESCRIPTOR,
  79. .bcdHID = 0x0111,
  80. .bCountryCode = 0,
  81. .bNumDescriptors = 1,
  82. .bDescriptorType1 = USB_HID_REPORT_DESCRIPTOR,
  83. .wDescriptorLength = sizeof(usb_hid_report_descriptor),
  84. },
  85. .hid_ep_in =
  86. {
  87. .bLength = sizeof(usb_endpoint_descriptor_t),
  88. .bDescriptorType = USB_ENDPOINT_DESCRIPTOR,
  89. .bEndpointAddress = USB_IN_ENDPOINT | USB_HID_EP_SEND,
  90. .bmAttributes = USB_INTERRUPT_ENDPOINT,
  91. .wMaxPacketSize = 1024,
  92. .bInterval = 1,
  93. },
  94. .hid_ep_out =
  95. {
  96. .bLength = sizeof(usb_endpoint_descriptor_t),
  97. .bDescriptorType = USB_ENDPOINT_DESCRIPTOR,
  98. .bEndpointAddress = USB_OUT_ENDPOINT | USB_HID_EP_RECV,
  99. .bmAttributes = USB_INTERRUPT_ENDPOINT,
  100. .wMaxPacketSize = 1024,
  101. .bInterval = 1,
  102. },
  103. };
  104. const alignas(4) uint8_t usb_hid_report_descriptor[30] =
  105. {
  106. 0x05, 0x01, // Usage Page (Generic Desktop Ctrls)
  107. 0x09, 0x00, // Usage (Undefined)
  108. 0xa1, 0x01, // Collection (Application)
  109. 0x15, 0x00, // Logical Minimum (0)
  110. 0x26, 0xff, 0x00, // Logical Maximum (255)
  111. 0x75, 0x08, // Report Size (8)
  112. 0x96, 0x00, 0x04, // Report Count (1024)
  113. 0x09, 0x00, // Usage (Undefined)
  114. 0x81, 0x82, // Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position)
  115. 0x75, 0x08, // Report Size (8)
  116. 0x96, 0x00, 0x04, // Report Count (1024)
  117. 0x09, 0x00, // Usage (Undefined)
  118. 0x91, 0x82, // Output (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position,Volatile)
  119. 0xc0, // End Collection
  120. };
  121. const alignas(4) usb_string_descriptor_zero_t usb_string_descriptor_zero =
  122. {
  123. .bLength = sizeof(usb_string_descriptor_zero_t),
  124. .bDescriptorType = USB_STRING_DESCRIPTOR,
  125. .wLANGID = 0x0409, // English (United States)
  126. };
  127. char usb_serial_number[16];
  128. const char *usb_strings[] =
  129. {
  130. [USB_STR_MANUFACTURER] = "Alex Taradov",
  131. [USB_STR_PRODUCT] = "Generic CMSIS-DAP Adapter",
  132. [USB_STR_SERIAL_NUMBER] = usb_serial_number,
  133. };