usb_desc_app.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * usb_desc_app.c
  3. *
  4. * Copyright (C) 2019 Sylvain Munaut
  5. * All rights reserved.
  6. *
  7. * LGPL v3+, see LICENSE.lgpl3
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 3 of the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public License
  20. * along with this program; if not, write to the Free Software Foundation,
  21. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  22. */
  23. #include <no2usb/usb_proto.h>
  24. #include <no2usb/usb_cdc_proto.h>
  25. #include <no2usb/usb_dfu_proto.h>
  26. #include <no2usb/usb.h>
  27. static const struct {
  28. /* Configuration */
  29. struct usb_conf_desc conf;
  30. /* CDC */
  31. struct {
  32. struct usb_intf_desc intf_ctl;
  33. struct usb_cdc_hdr_desc cdc_hdr;
  34. struct usb_cdc_acm_desc cdc_acm;
  35. struct usb_cdc_union_desc cdc_union;
  36. uint8_t cdc_union_slave;
  37. struct usb_ep_desc ep_ctl;
  38. struct usb_intf_desc intf_data;
  39. struct usb_ep_desc ep_data_out;
  40. struct usb_ep_desc ep_data_in;
  41. } __attribute__ ((packed)) cdc;
  42. /* DFU Runtime */
  43. struct {
  44. struct usb_intf_desc intf;
  45. struct usb_dfu_func_desc func;
  46. } __attribute__ ((packed)) dfu;
  47. } __attribute__ ((packed)) _app_conf_desc = {
  48. .conf = {
  49. .bLength = sizeof(struct usb_conf_desc),
  50. .bDescriptorType = USB_DT_CONF,
  51. .wTotalLength = sizeof(_app_conf_desc),
  52. .bNumInterfaces = 3,
  53. .bConfigurationValue = 1,
  54. .iConfiguration = 4,
  55. .bmAttributes = 0x80,
  56. .bMaxPower = 0x32, /* 100 mA */
  57. },
  58. .cdc = {
  59. .intf_ctl = {
  60. .bLength = sizeof(struct usb_intf_desc),
  61. .bDescriptorType = USB_DT_INTF,
  62. .bInterfaceNumber = 0,
  63. .bAlternateSetting = 0,
  64. .bNumEndpoints = 1,
  65. .bInterfaceClass = USB_CLS_CDC_CONTROL,
  66. .bInterfaceSubClass = USB_CDC_SCLS_ACM,
  67. .bInterfaceProtocol = 0x00,
  68. .iInterface = 5,
  69. },
  70. .cdc_hdr = {
  71. .bLength = sizeof(struct usb_cdc_hdr_desc),
  72. .bDescriptorType = USB_CS_DT_INTF,
  73. .bDescriptorsubtype = USB_CDC_DST_HEADER,
  74. .bcdCDC = 0x0110,
  75. },
  76. .cdc_acm = {
  77. .bLength = sizeof(struct usb_cdc_acm_desc),
  78. .bDescriptorType = USB_CS_DT_INTF,
  79. .bDescriptorsubtype = USB_CDC_DST_ACM,
  80. .bmCapabilities = 0x02,
  81. },
  82. .cdc_union = {
  83. .bLength = sizeof(struct usb_cdc_union_desc) + 1,
  84. .bDescriptorType = USB_CS_DT_INTF,
  85. .bDescriptorsubtype = USB_CDC_DST_UNION,
  86. .bMasterInterface = 0,
  87. },
  88. .cdc_union_slave = 1,
  89. .ep_ctl = {
  90. .bLength = sizeof(struct usb_ep_desc),
  91. .bDescriptorType = USB_DT_EP,
  92. .bEndpointAddress = 0x84,
  93. .bmAttributes = 0x03,
  94. .wMaxPacketSize = 64,
  95. .bInterval = 0x40,
  96. },
  97. .intf_data = {
  98. .bLength = sizeof(struct usb_intf_desc),
  99. .bDescriptorType = USB_DT_INTF,
  100. .bInterfaceNumber = 1,
  101. .bAlternateSetting = 0,
  102. .bNumEndpoints = 2,
  103. .bInterfaceClass = USB_CLS_CDC_DATA,
  104. .bInterfaceSubClass = 0x00,
  105. .bInterfaceProtocol = 0x00,
  106. .iInterface = 6,
  107. },
  108. .ep_data_out = {
  109. .bLength = sizeof(struct usb_ep_desc),
  110. .bDescriptorType = USB_DT_EP,
  111. .bEndpointAddress = 0x05,
  112. .bmAttributes = 0x02,
  113. .wMaxPacketSize = 64,
  114. .bInterval = 0x00,
  115. },
  116. .ep_data_in = {
  117. .bLength = sizeof(struct usb_ep_desc),
  118. .bDescriptorType = USB_DT_EP,
  119. .bEndpointAddress = 0x85,
  120. .bmAttributes = 0x02,
  121. .wMaxPacketSize = 64,
  122. .bInterval = 0x00,
  123. },
  124. },
  125. .dfu = {
  126. .intf = {
  127. .bLength = sizeof(struct usb_intf_desc),
  128. .bDescriptorType = USB_DT_INTF,
  129. .bInterfaceNumber = 2,
  130. .bAlternateSetting = 0,
  131. .bNumEndpoints = 0,
  132. .bInterfaceClass = USB_CLS_APP_SPECIFIC,
  133. .bInterfaceSubClass = 0x01,
  134. .bInterfaceProtocol = 0x01,
  135. .iInterface = 7,
  136. },
  137. .func = {
  138. .bLength = sizeof(struct usb_dfu_func_desc),
  139. .bDescriptorType = USB_DFU_DT_FUNC,
  140. .bmAttributes = 0x0d,
  141. .wDetachTimeOut = 0,
  142. .wTransferSize = 4096,
  143. .bcdDFUVersion = 0x0101,
  144. },
  145. },
  146. };
  147. static const struct usb_conf_desc * const _conf_desc_array[] = {
  148. &_app_conf_desc.conf,
  149. };
  150. static const struct usb_dev_desc _dev_desc = {
  151. .bLength = sizeof(struct usb_dev_desc),
  152. .bDescriptorType = USB_DT_DEV,
  153. .bcdUSB = 0x0200,
  154. .bDeviceClass = 0,
  155. .bDeviceSubClass = 0,
  156. .bDeviceProtocol = 0,
  157. .bMaxPacketSize0 = 64,
  158. .idVendor = 0x1d50,
  159. .idProduct = 0x6147,
  160. .bcdDevice = 0x0001, /* v0.1 */
  161. .iManufacturer = 2,
  162. .iProduct = 3,
  163. .iSerialNumber = 1,
  164. .bNumConfigurations = num_elem(_conf_desc_array),
  165. };
  166. #include "usb_str_app.gen.h"
  167. const struct usb_stack_descriptors app_stack_desc = {
  168. .dev = &_dev_desc,
  169. .conf = _conf_desc_array,
  170. .n_conf = num_elem(_conf_desc_array),
  171. .str = _str_desc_array,
  172. .n_str = num_elem(_str_desc_array),
  173. };