usb_desc_app.c 12 KB

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