usb_desc_app.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /*
  2. * usb_desc_app.c
  3. *
  4. * Copyright (C) 2020 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_ac_proto.h>
  25. #include <no2usb/usb_cdc_proto.h>
  26. #include <no2usb/usb_dfu_proto.h>
  27. #include <no2usb/usb.h>
  28. usb_ac_ac_hdr_desc_def(1);
  29. usb_ac_ac_feature_desc_def(6);
  30. usb_ac_as_fmt_type1_desc_def(3);
  31. usb_ac_ms_ep_general_desc_def(1);
  32. usb_ac_ms_out_jack_desc_def(1);
  33. static const struct {
  34. /* Configuration */
  35. struct usb_conf_desc conf;
  36. /* DFU Runtime */
  37. struct {
  38. struct usb_intf_desc intf;
  39. struct usb_dfu_func_desc func;
  40. } __attribute__ ((packed)) dfu;
  41. /* Audio Control Interface */
  42. struct {
  43. struct usb_intf_desc intf;
  44. struct usb_ac_ac_hdr_desc__1 hdr;
  45. struct usb_ac_ac_input_desc input;
  46. struct usb_ac_ac_feature_desc__6 feat;
  47. struct usb_ac_ac_output_desc output;
  48. } __attribute__ ((packed)) audio_ctl;
  49. /* Audio Streaming Interface */
  50. struct {
  51. struct usb_intf_desc intf[2];
  52. struct usb_ac_as_general_desc general;
  53. struct usb_ac_as_fmt_type1_desc__3 fmt;
  54. struct usb_cc_ep_desc ep_data;
  55. struct usb_ac_as_ep_general_desc ep_gen;
  56. struct usb_cc_ep_desc ep_sync;
  57. } __attribute__ ((packed)) audio_stream;
  58. /* MIDI Streaming Interface */
  59. struct {
  60. struct usb_intf_desc intf;
  61. struct usb_ac_ms_hdr_desc hdr;
  62. struct usb_ac_ms_in_jack_desc input;
  63. struct usb_ac_ms_out_jack_desc__1 output;
  64. struct usb_cc_ep_desc ep_data;
  65. struct usb_ac_ms_ep_general_desc__1 ep_gen;
  66. } __attribute__ ((packed)) midi_stream;
  67. } __attribute__ ((packed)) _app_conf_desc = {
  68. .conf = {
  69. .bLength = sizeof(struct usb_conf_desc),
  70. .bDescriptorType = USB_DT_CONF,
  71. .wTotalLength = sizeof(_app_conf_desc),
  72. .bNumInterfaces = 4,
  73. .bConfigurationValue = 1,
  74. .iConfiguration = 4,
  75. .bmAttributes = 0x80,
  76. .bMaxPower = 0x32, /* 100 mA */
  77. },
  78. .dfu = {
  79. .intf = {
  80. .bLength = sizeof(struct usb_intf_desc),
  81. .bDescriptorType = USB_DT_INTF,
  82. .bInterfaceNumber = 0,
  83. .bAlternateSetting = 0,
  84. .bNumEndpoints = 0,
  85. .bInterfaceClass = USB_CLS_APP_SPECIFIC,
  86. .bInterfaceSubClass = 0x01,
  87. .bInterfaceProtocol = 0x01,
  88. .iInterface = 5,
  89. },
  90. .func = {
  91. .bLength = sizeof(struct usb_dfu_func_desc),
  92. .bDescriptorType = USB_DFU_DT_FUNC,
  93. .bmAttributes = 0x0d,
  94. .wDetachTimeOut = 0,
  95. .wTransferSize = 4096,
  96. .bcdDFUVersion = 0x0101,
  97. },
  98. },
  99. .audio_ctl = {
  100. .intf = {
  101. .bLength = sizeof(struct usb_intf_desc),
  102. .bDescriptorType = USB_DT_INTF,
  103. .bInterfaceNumber = 1,
  104. .bAlternateSetting = 0,
  105. .bNumEndpoints = 0,
  106. .bInterfaceClass = 0x01,
  107. .bInterfaceSubClass = USB_AC_SCLS_AUDIOCONTROL,
  108. .bInterfaceProtocol = 0x00,
  109. .iInterface = 6,
  110. },
  111. .hdr = {
  112. .bLength = sizeof(struct usb_ac_ac_hdr_desc__1),
  113. .bDescriptorType = USB_CS_DT_INTF,
  114. .bDescriptorSubtype = USB_AC_AC_IDST_HEADER,
  115. .bcdADC = 0x0100,
  116. .wTotalLength = sizeof(_app_conf_desc.audio_ctl) - sizeof(struct usb_intf_desc),
  117. .bInCollection = 1,
  118. .baInterfaceNr = { 0x02 },
  119. },
  120. .input = {
  121. .bLength = sizeof(struct usb_ac_ac_input_desc),
  122. .bDescriptortype = USB_CS_DT_INTF,
  123. .bDescriptorSubtype = USB_AC_AC_IDST_INPUT_TERMINAL,
  124. .bTerminalID = 1,
  125. .wTerminalType = 0x0101,
  126. .bAssocTerminal = 0,
  127. .bNrChannels = 2,
  128. .wChannelConfig = 0x0003,
  129. .iChannelNames = 7,
  130. .iTerminal = 9,
  131. },
  132. .feat = {
  133. .bLength = sizeof(struct usb_ac_ac_feature_desc__6),
  134. .bDescriptortype = USB_CS_DT_INTF,
  135. .bDescriptorSubtype = USB_AC_AC_IDST_FEATURE_UNIT,
  136. .bUnitID = 2,
  137. .bSourceID = 1,
  138. .bControlSize = 2,
  139. .bmaControls = {
  140. U16_TO_U8_LE(0x0001), // Mute
  141. U16_TO_U8_LE(0x0003), // Mute + Volume
  142. U16_TO_U8_LE(0x0003), // Mute + Volume
  143. },
  144. .iFeature = 0,
  145. },
  146. .output = {
  147. .bLength = sizeof(struct usb_ac_ac_output_desc),
  148. .bDescriptortype = USB_CS_DT_INTF,
  149. .bDescriptorSubtype = USB_AC_AC_IDST_OUTPUT_TERMINAL,
  150. .bTerminalID = 3,
  151. .wTerminalType = 0x0302,
  152. .bAssocTerminal = 0,
  153. .bSourceID = 2,
  154. .iTerminal = 10,
  155. },
  156. },
  157. .audio_stream = {
  158. .intf[0] = {
  159. .bLength = sizeof(struct usb_intf_desc),
  160. .bDescriptorType = USB_DT_INTF,
  161. .bInterfaceNumber = 2,
  162. .bAlternateSetting = 0,
  163. .bNumEndpoints = 0,
  164. .bInterfaceClass = 0x01,
  165. .bInterfaceSubClass = USB_AC_SCLS_AUDIOSTREAMING,
  166. .bInterfaceProtocol = 0x00,
  167. .iInterface = 11,
  168. },
  169. .intf[1] = {
  170. .bLength = sizeof(struct usb_intf_desc),
  171. .bDescriptorType = USB_DT_INTF,
  172. .bInterfaceNumber = 2,
  173. .bAlternateSetting = 1,
  174. .bNumEndpoints = 2,
  175. .bInterfaceClass = 0x01,
  176. .bInterfaceSubClass = USB_AC_SCLS_AUDIOSTREAMING,
  177. .bInterfaceProtocol = 0x00,
  178. .iInterface = 12,
  179. },
  180. .general = {
  181. .bLength = sizeof(struct usb_ac_as_general_desc),
  182. .bDescriptortype = USB_CS_DT_INTF,
  183. .bDescriptorSubtype = USB_AC_AS_IDST_GENERAL,
  184. .bTerminalLink = 1,
  185. .bDelay = 0,
  186. .wFormatTag = 0x0001, /* PCM */
  187. },
  188. .fmt = {
  189. .bLength = sizeof(struct usb_ac_as_fmt_type1_desc__3),
  190. .bDescriptortype = USB_CS_DT_INTF,
  191. .bDescriptorSubtype = USB_AC_AS_IDST_FORMAT_TYPE,
  192. .bFormatType = 1,
  193. .bNrChannels = 2,
  194. .bSubframeSize = 2,
  195. .bBitResolution = 16,
  196. .bSamFreqType = 1,
  197. .tSamFreq = {
  198. U24_TO_U8_LE(48000),
  199. },
  200. },
  201. .ep_data = {
  202. .bLength = sizeof(struct usb_cc_ep_desc),
  203. .bDescriptorType = USB_DT_EP,
  204. .bEndpointAddress = 0x01, /* 1 OUT */
  205. .bmAttributes = 0x05, /* Data, Async, Isoc */
  206. .wMaxPacketSize = 288,
  207. .bInterval = 1,
  208. .bRefresh = 0,
  209. .bSynchAddress = 0x81,
  210. },
  211. .ep_gen = {
  212. .bLength = sizeof(struct usb_ac_as_ep_general_desc),
  213. .bDescriptortype = USB_CS_DT_EP,
  214. .bDescriptorSubtype = USB_AC_EDST_GENERAL,
  215. .bmAttributes = 0x00,
  216. .bLockDelayUnits = 0,
  217. .wLockDelay = 0,
  218. },
  219. .ep_sync = {
  220. .bLength = sizeof(struct usb_cc_ep_desc),
  221. .bDescriptorType = USB_DT_EP,
  222. .bEndpointAddress = 0x81, /* 1 IN */
  223. .bmAttributes = 0x11, /* Feedback, Isoc */
  224. .wMaxPacketSize = 8,
  225. .bInterval = 1,
  226. .bRefresh = 1,
  227. .bSynchAddress = 0,
  228. },
  229. },
  230. .midi_stream = {
  231. .intf = {
  232. .bLength = sizeof(struct usb_intf_desc),
  233. .bDescriptorType = USB_DT_INTF,
  234. .bInterfaceNumber = 3,
  235. .bAlternateSetting = 0,
  236. .bNumEndpoints = 1,
  237. .bInterfaceClass = 0x01,
  238. .bInterfaceSubClass = USB_AC_SCLS_MIDISTREAMING,
  239. .bInterfaceProtocol = 0x00,
  240. .iInterface = 0,
  241. },
  242. .hdr = {
  243. .bLength = sizeof(struct usb_ac_ms_hdr_desc),
  244. .bDescriptorType = USB_CS_DT_INTF,
  245. .bDescriptorSubtype = USB_AC_MS_IDST_HEADER,
  246. .bcdADC = 0x0100,
  247. .wTotalLength = sizeof(_app_conf_desc.midi_stream) - sizeof(struct usb_intf_desc) - sizeof(_app_conf_desc.midi_stream.ep_data) - sizeof(_app_conf_desc.midi_stream.ep_gen),
  248. },
  249. .input = {
  250. .bLength = sizeof(struct usb_ac_ms_in_jack_desc),
  251. .bDescriptorType = USB_CS_DT_INTF,
  252. .bDescriptorSubtype = USB_AC_MS_IDST_MIDI_IN_JACK,
  253. .bJackType = USB_AC_MS_JACK_TYPE_EMBEDDED,
  254. .bJackID = 1,
  255. .iJack = 0,
  256. },
  257. .output = {
  258. .bLength = sizeof(struct usb_ac_ms_out_jack_desc__1),
  259. .bDescriptorType = USB_CS_DT_INTF,
  260. .bDescriptorSubtype = USB_AC_MS_IDST_MIDI_OUT_JACK,
  261. .bJackType = USB_AC_MS_JACK_TYPE_EXTERNAL,
  262. .bJackID = 2,
  263. .bNrInputPins = 1,
  264. .sources = {
  265. {
  266. .baSourceID = 1,
  267. .baSourcePin = 1,
  268. },
  269. },
  270. .iJack = 0,
  271. },
  272. .ep_data = {
  273. .bLength = sizeof(struct usb_cc_ep_desc),
  274. .bDescriptorType = USB_DT_EP,
  275. .bEndpointAddress = 0x02, /* 2 OUT */
  276. .bmAttributes = 0x02, /* Bulk */
  277. .wMaxPacketSize = 64,
  278. .bInterval = 0,
  279. .bRefresh = 0,
  280. .bSynchAddress = 0,
  281. },
  282. .ep_gen = {
  283. .bLength = sizeof(struct usb_ac_ms_ep_general_desc__1),
  284. .bDescriptortype = USB_CS_DT_EP,
  285. .bDescriptorSubtype = USB_AC_EDST_GENERAL,
  286. .bNumEmbMIDIJack = 1,
  287. .baAssocJackID = { 1 },
  288. },
  289. },
  290. };
  291. static const struct usb_conf_desc * const _conf_desc_array[] = {
  292. &_app_conf_desc.conf,
  293. };
  294. static const struct usb_dev_desc _dev_desc = {
  295. .bLength = sizeof(struct usb_dev_desc),
  296. .bDescriptorType = USB_DT_DEV,
  297. .bcdUSB = 0x0200,
  298. .bDeviceClass = 0,
  299. .bDeviceSubClass = 0,
  300. .bDeviceProtocol = 0,
  301. .bMaxPacketSize0 = 64,
  302. .idVendor = 0x1d50,
  303. .idProduct = 0x6147,
  304. .bcdDevice = 0x0001, /* v0.1 */
  305. .iManufacturer = 2,
  306. .iProduct = 3,
  307. .iSerialNumber = 1,
  308. .bNumConfigurations = num_elem(_conf_desc_array),
  309. };
  310. #include "usb_str_app.gen.h"
  311. const struct usb_stack_descriptors app_stack_desc = {
  312. .dev = &_dev_desc,
  313. .conf = _conf_desc_array,
  314. .n_conf = num_elem(_conf_desc_array),
  315. .str = _str_desc_array,
  316. .n_str = num_elem(_str_desc_array),
  317. };