usb_desc_app.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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_cdc_union_desc_def(1);
  29. usb_cdc_coutry_sel_desc_def(71);
  30. usb_ac_ac_hdr_desc_def(2);
  31. usb_ac_ac_feature_desc_def(4);
  32. usb_ac_as_fmt_type1_desc_def(3);
  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__2 hdr;
  45. struct usb_ac_ac_input_desc it_phone;
  46. struct usb_ac_ac_feature_desc__4 feat_in;
  47. struct usb_ac_ac_output_desc ot_usb;
  48. struct usb_ac_ac_input_desc it_usb;
  49. struct usb_ac_ac_feature_desc__4 feat_out;
  50. struct usb_ac_ac_output_desc ot_phone;
  51. } __attribute__ ((packed)) audio_ctl;
  52. /* Audio Inpput Streaming Interface */
  53. struct {
  54. struct usb_intf_desc intf[2];
  55. struct usb_ac_as_general_desc general;
  56. struct usb_ac_as_fmt_type1_desc__3 fmt;
  57. struct usb_cc_ep_desc ep_data;
  58. struct usb_ac_as_ep_general_desc ep_gen;
  59. } __attribute__ ((packed)) audio_stream_in;
  60. /* Audio Output Streaming Interface */
  61. struct {
  62. struct usb_intf_desc intf[2];
  63. struct usb_ac_as_general_desc general;
  64. struct usb_ac_as_fmt_type1_desc__3 fmt;
  65. struct usb_cc_ep_desc ep_data;
  66. struct usb_ac_as_ep_general_desc ep_gen;
  67. struct usb_cc_ep_desc ep_sync;
  68. } __attribute__ ((packed)) audio_stream_out;
  69. /* USB CDC DLM */
  70. struct {
  71. struct usb_intf_desc intf;
  72. struct usb_cdc_hdr_desc hdr;
  73. struct usb_cdc_dlm_desc dlm;
  74. struct usb_cdc_union_desc__1 ud;
  75. struct usb_cdc_country_sel_desc__71 csd;
  76. struct usb_ep_desc ep;
  77. } __attribute__ ((packed)) cdc_dlm;
  78. } __attribute__ ((packed)) _app_conf_desc = {
  79. .conf = {
  80. .bLength = sizeof(struct usb_conf_desc),
  81. .bDescriptorType = USB_DT_CONF,
  82. .wTotalLength = sizeof(_app_conf_desc),
  83. .bNumInterfaces = 5,
  84. .bConfigurationValue = 1,
  85. .iConfiguration = 4,
  86. .bmAttributes = 0x80,
  87. .bMaxPower = 0x32, /* 100 mA */
  88. },
  89. .dfu = {
  90. .intf = {
  91. .bLength = sizeof(struct usb_intf_desc),
  92. .bDescriptorType = USB_DT_INTF,
  93. .bInterfaceNumber = 0,
  94. .bAlternateSetting = 0,
  95. .bNumEndpoints = 0,
  96. .bInterfaceClass = 0xfe,
  97. .bInterfaceSubClass = 0x01,
  98. .bInterfaceProtocol = 0x01,
  99. .iInterface = 5,
  100. },
  101. .func = {
  102. .bLength = sizeof(struct usb_dfu_func_desc),
  103. .bDescriptorType = USB_DFU_DT_FUNC,
  104. .bmAttributes = 0x0d,
  105. .wDetachTimeOut = 0,
  106. .wTransferSize = 4096,
  107. .bcdDFUVersion = 0x0101,
  108. },
  109. },
  110. .audio_ctl = {
  111. .intf = {
  112. .bLength = sizeof(struct usb_intf_desc),
  113. .bDescriptorType = USB_DT_INTF,
  114. .bInterfaceNumber = 1,
  115. .bAlternateSetting = 0,
  116. .bNumEndpoints = 0,
  117. .bInterfaceClass = USB_CLS_AUDIO,
  118. .bInterfaceSubClass = USB_AC_SCLS_AUDIOCONTROL,
  119. .bInterfaceProtocol = 0x00,
  120. .iInterface = 0,
  121. },
  122. .hdr = {
  123. .bLength = sizeof(struct usb_ac_ac_hdr_desc__2),
  124. .bDescriptorType = USB_CS_DT_INTF,
  125. .bDescriptorSubtype = USB_AC_AC_IDST_HEADER,
  126. .bcdADC = 0x0100,
  127. .wTotalLength = sizeof(_app_conf_desc.audio_ctl) - sizeof(struct usb_intf_desc),
  128. .bInCollection = 2,
  129. .baInterfaceNr = { 0x02, 0x03 },
  130. },
  131. .it_phone = { /* ID 1 */
  132. .bLength = sizeof(struct usb_ac_ac_input_desc),
  133. .bDescriptortype = USB_CS_DT_INTF,
  134. .bDescriptorSubtype = USB_AC_AC_IDST_INPUT_TERMINAL,
  135. .bTerminalID = 1,
  136. .wTerminalType = 0x0501, /* Phone Line */
  137. .bAssocTerminal = 6,
  138. .bNrChannels = 1,
  139. .wChannelConfig = 0x0000,
  140. .iChannelNames = 0,
  141. .iTerminal = 0,
  142. },
  143. .feat_in = { /* ID 2 */
  144. .bLength = sizeof(struct usb_ac_ac_feature_desc__4),
  145. .bDescriptortype = USB_CS_DT_INTF,
  146. .bDescriptorSubtype = USB_AC_AC_IDST_FEATURE_UNIT,
  147. .bUnitID = 2,
  148. .bSourceID = 1,
  149. .bControlSize = 2,
  150. .bmaControls = {
  151. U16_TO_U8_LE(0x0003), // Mute + Volume
  152. U16_TO_U8_LE(0x0000), // n/a
  153. },
  154. .iFeature = 6,
  155. },
  156. .ot_usb = { /* ID 3 */
  157. .bLength = sizeof(struct usb_ac_ac_output_desc),
  158. .bDescriptortype = USB_CS_DT_INTF,
  159. .bDescriptorSubtype = USB_AC_AC_IDST_OUTPUT_TERMINAL,
  160. .bTerminalID = 3,
  161. .wTerminalType = 0x0101, /* USB Streaming */
  162. .bAssocTerminal = 4,
  163. .bSourceID = 2,
  164. .iTerminal = 0,
  165. },
  166. .it_usb = { /* ID 4 */
  167. .bLength = sizeof(struct usb_ac_ac_input_desc),
  168. .bDescriptortype = USB_CS_DT_INTF,
  169. .bDescriptorSubtype = USB_AC_AC_IDST_INPUT_TERMINAL,
  170. .bTerminalID = 4,
  171. .wTerminalType = 0x0101, /* USB Streaming */
  172. .bAssocTerminal = 3,
  173. .bNrChannels = 1,
  174. .wChannelConfig = 0x0000,
  175. .iChannelNames = 0,
  176. .iTerminal = 0,
  177. },
  178. .feat_out = { /* ID 5 */
  179. .bLength = sizeof(struct usb_ac_ac_feature_desc__4),
  180. .bDescriptortype = USB_CS_DT_INTF,
  181. .bDescriptorSubtype = USB_AC_AC_IDST_FEATURE_UNIT,
  182. .bUnitID = 5,
  183. .bSourceID = 4,
  184. .bControlSize = 2,
  185. .bmaControls = {
  186. U16_TO_U8_LE(0x0003), // Mute + Volume
  187. U16_TO_U8_LE(0x0000), // n/a
  188. },
  189. .iFeature = 7,
  190. },
  191. .ot_phone = { /* ID 6 */
  192. .bLength = sizeof(struct usb_ac_ac_output_desc),
  193. .bDescriptortype = USB_CS_DT_INTF,
  194. .bDescriptorSubtype = USB_AC_AC_IDST_OUTPUT_TERMINAL,
  195. .bTerminalID = 6,
  196. .wTerminalType = 0x0501, /* Phone Line */
  197. .bAssocTerminal = 1,
  198. .bSourceID = 5,
  199. .iTerminal = 0,
  200. },
  201. },
  202. .audio_stream_in = {
  203. .intf[0] = {
  204. .bLength = sizeof(struct usb_intf_desc),
  205. .bDescriptorType = USB_DT_INTF,
  206. .bInterfaceNumber = 2,
  207. .bAlternateSetting = 0,
  208. .bNumEndpoints = 0,
  209. .bInterfaceClass = USB_CLS_AUDIO,
  210. .bInterfaceSubClass = USB_AC_SCLS_AUDIOSTREAMING,
  211. .bInterfaceProtocol = 0x00,
  212. .iInterface = 0,
  213. },
  214. .intf[1] = {
  215. .bLength = sizeof(struct usb_intf_desc),
  216. .bDescriptorType = USB_DT_INTF,
  217. .bInterfaceNumber = 2,
  218. .bAlternateSetting = 1,
  219. .bNumEndpoints = 1,
  220. .bInterfaceClass = USB_CLS_AUDIO,
  221. .bInterfaceSubClass = USB_AC_SCLS_AUDIOSTREAMING,
  222. .bInterfaceProtocol = 0x00,
  223. .iInterface = 0,
  224. },
  225. .general = {
  226. .bLength = sizeof(struct usb_ac_as_general_desc),
  227. .bDescriptortype = USB_CS_DT_INTF,
  228. .bDescriptorSubtype = USB_AC_AS_IDST_GENERAL,
  229. .bTerminalLink = 3,
  230. .bDelay = 0,
  231. .wFormatTag = 0x0001, /* PCM */
  232. },
  233. .fmt = {
  234. .bLength = sizeof(struct usb_ac_as_fmt_type1_desc__3),
  235. .bDescriptortype = USB_CS_DT_INTF,
  236. .bDescriptorSubtype = USB_AC_AS_IDST_FORMAT_TYPE,
  237. .bFormatType = 1,
  238. .bNrChannels = 1,
  239. .bSubframeSize = 2,
  240. .bBitResolution = 16,
  241. .bSamFreqType = 1,
  242. .tSamFreq = {
  243. U24_TO_U8_LE(8000),
  244. },
  245. },
  246. .ep_data = {
  247. .bLength = sizeof(struct usb_cc_ep_desc),
  248. .bDescriptorType = USB_DT_EP,
  249. .bEndpointAddress = 0x81, /* 1 IN */
  250. .bmAttributes = 0x05, /* Data, Async, Isoc */
  251. .wMaxPacketSize = 120,
  252. .bInterval = 1,
  253. .bRefresh = 0,
  254. .bSynchAddress = 0,
  255. },
  256. .ep_gen = {
  257. .bLength = sizeof(struct usb_ac_as_ep_general_desc),
  258. .bDescriptortype = USB_CS_DT_EP,
  259. .bDescriptorSubtype = USB_AC_EDST_GENERAL,
  260. .bmAttributes = 0x00,
  261. .bLockDelayUnits = 0,
  262. .wLockDelay = 0,
  263. },
  264. },
  265. .audio_stream_out = {
  266. .intf[0] = {
  267. .bLength = sizeof(struct usb_intf_desc),
  268. .bDescriptorType = USB_DT_INTF,
  269. .bInterfaceNumber = 3,
  270. .bAlternateSetting = 0,
  271. .bNumEndpoints = 0,
  272. .bInterfaceClass = USB_CLS_AUDIO,
  273. .bInterfaceSubClass = USB_AC_SCLS_AUDIOSTREAMING,
  274. .bInterfaceProtocol = 0x00,
  275. .iInterface = 0,
  276. },
  277. .intf[1] = {
  278. .bLength = sizeof(struct usb_intf_desc),
  279. .bDescriptorType = USB_DT_INTF,
  280. .bInterfaceNumber = 3,
  281. .bAlternateSetting = 1,
  282. .bNumEndpoints = 2,
  283. .bInterfaceClass = USB_CLS_AUDIO,
  284. .bInterfaceSubClass = USB_AC_SCLS_AUDIOSTREAMING,
  285. .bInterfaceProtocol = 0x00,
  286. .iInterface = 0,
  287. },
  288. .general = {
  289. .bLength = sizeof(struct usb_ac_as_general_desc),
  290. .bDescriptortype = USB_CS_DT_INTF,
  291. .bDescriptorSubtype = USB_AC_AS_IDST_GENERAL,
  292. .bTerminalLink = 4,
  293. .bDelay = 0,
  294. .wFormatTag = 0x0001, /* PCM */
  295. },
  296. .fmt = {
  297. .bLength = sizeof(struct usb_ac_as_fmt_type1_desc__3),
  298. .bDescriptortype = USB_CS_DT_INTF,
  299. .bDescriptorSubtype = USB_AC_AS_IDST_FORMAT_TYPE,
  300. .bFormatType = 1,
  301. .bNrChannels = 1,
  302. .bSubframeSize = 2,
  303. .bBitResolution = 16,
  304. .bSamFreqType = 1,
  305. .tSamFreq = {
  306. U24_TO_U8_LE(8000),
  307. },
  308. },
  309. .ep_data = {
  310. .bLength = sizeof(struct usb_cc_ep_desc),
  311. .bDescriptorType = USB_DT_EP,
  312. .bEndpointAddress = 0x01, /* 1 OUT */
  313. .bmAttributes = 0x05, /* Data, Async, Isoc */
  314. .wMaxPacketSize = 120,
  315. .bInterval = 1,
  316. .bRefresh = 0,
  317. .bSynchAddress = 0x82,
  318. },
  319. .ep_gen = {
  320. .bLength = sizeof(struct usb_ac_as_ep_general_desc),
  321. .bDescriptortype = USB_CS_DT_EP,
  322. .bDescriptorSubtype = USB_AC_EDST_GENERAL,
  323. .bmAttributes = 0x00,
  324. .bLockDelayUnits = 0,
  325. .wLockDelay = 0,
  326. },
  327. .ep_sync = {
  328. .bLength = sizeof(struct usb_cc_ep_desc),
  329. .bDescriptorType = USB_DT_EP,
  330. .bEndpointAddress = 0x82, /* 2 IN */
  331. .bmAttributes = 0x11, /* Feedback, Isoc */
  332. .wMaxPacketSize = 8,
  333. .bInterval = 1,
  334. .bRefresh = 1,
  335. .bSynchAddress = 0,
  336. },
  337. },
  338. .cdc_dlm = {
  339. .intf = {
  340. .bLength = sizeof(struct usb_intf_desc),
  341. .bDescriptorType = USB_DT_INTF,
  342. .bInterfaceNumber = 4,
  343. .bAlternateSetting = 0,
  344. .bNumEndpoints = 1,
  345. .bInterfaceClass = USB_CLS_COMMUNICATIONS,
  346. .bInterfaceSubClass = USB_CDC_SCLS_DLCM,
  347. .bInterfaceProtocol = 0x00,
  348. .iInterface = 0,
  349. },
  350. .hdr = {
  351. .bLength = sizeof(struct usb_cdc_hdr_desc),
  352. .bDescriptorType = USB_CS_DT_INTF,
  353. .bDescriptorsubtype = USB_CDC_DST_HEADER,
  354. .bcdCDC = 0x0110,
  355. },
  356. .dlm = {
  357. .bLength = sizeof(struct usb_cdc_dlm_desc),
  358. .bDescriptorType = USB_CS_DT_INTF,
  359. .bDescriptorsubtype = USB_CDC_DST_DLM,
  360. .bmCapabilities = 0x02,
  361. },
  362. .ud = {
  363. .bLength = sizeof(struct usb_cdc_union_desc__1),
  364. .bDescriptorType = USB_CS_DT_INTF,
  365. .bDescriptorsubtype = USB_CDC_DST_UNION,
  366. .bMasterInterface = 4,
  367. .bSlaveInterface = { 1 },
  368. },
  369. .csd = {
  370. .bLength = sizeof(struct usb_cdc_country_sel_desc__71),
  371. .bDescriptorType = USB_CS_DT_INTF,
  372. .bDescriptorsubtype = USB_CDC_DST_COUNTRY_SEL,
  373. .iCountryCodeRelDate = 9,
  374. .wCountryCode = {
  375. 32, 36, 40, 48, 56, 76, 100, 124, 152, 156,
  376. 158, 170, 191, 196, 203, 208, 218, 222, 246, 250,
  377. 276, 300, 316, 344, 348, 352, 356, 360, 372, 376,
  378. 380, 392, 398, 400, 410, 414, 422, 428, 442, 446,
  379. 458, 470, 484, 504, 512, 528, 554, 566, 578, 586,
  380. 604, 608, 616, 620, 642, 643, 682, 702, 703, 705,
  381. 710, 724, 752, 756, 760, 764, 784, 818, 826, 840,
  382. 887,
  383. },
  384. },
  385. .ep = {
  386. .bLength = sizeof(struct usb_ep_desc),
  387. .bDescriptorType = USB_DT_EP,
  388. .bEndpointAddress = 0x83, /* 3 IN */
  389. .bmAttributes = 0x03, /* Interupt */
  390. .wMaxPacketSize = 8,
  391. .bInterval = 32,
  392. },
  393. },
  394. };
  395. static const struct usb_conf_desc * const _conf_desc_array[] = {
  396. &_app_conf_desc.conf,
  397. };
  398. static const struct usb_dev_desc _dev_desc = {
  399. .bLength = sizeof(struct usb_dev_desc),
  400. .bDescriptorType = USB_DT_DEV,
  401. .bcdUSB = 0x0200,
  402. .bDeviceClass = 0,
  403. .bDeviceSubClass = 0,
  404. .bDeviceProtocol = 0,
  405. .bMaxPacketSize0 = 64,
  406. .idVendor = 0x1d50,
  407. .idProduct = 0x6175,
  408. .bcdDevice = 0x0001, /* v0.1 */
  409. .iManufacturer = 2,
  410. .iProduct = 3,
  411. .iSerialNumber = 1,
  412. .bNumConfigurations = num_elem(_conf_desc_array),
  413. };
  414. #include "usb_str_app.gen.h"
  415. const struct usb_stack_descriptors app_stack_desc = {
  416. .dev = &_dev_desc,
  417. .conf = _conf_desc_array,
  418. .n_conf = num_elem(_conf_desc_array),
  419. .str = _str_desc_array,
  420. .n_str = num_elem(_str_desc_array),
  421. };