f_uac2.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014
  1. /*
  2. * f_uac2.c -- USB Audio Class 2.0 Function
  3. *
  4. * Copyright (C) 2011
  5. * Yadwinder Singh (yadi.brar01@gmail.com)
  6. * Jaswinder Singh (jaswinder.singh@linaro.org)
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. */
  13. #include <linux/usb/audio.h>
  14. #include <linux/usb/audio-v2.h>
  15. #include <linux/module.h>
  16. #include "u_audio.h"
  17. #include "u_uac2.h"
  18. /*
  19. * The driver implements a simple UAC_2 topology.
  20. * USB-OUT -> IT_1 -> OT_3 -> ALSA_Capture
  21. * ALSA_Playback -> IT_2 -> OT_4 -> USB-IN
  22. * Capture and Playback sampling rates are independently
  23. * controlled by two clock sources :
  24. * CLK_5 := c_srate, and CLK_6 := p_srate
  25. */
  26. #define USB_OUT_IT_ID 1
  27. #define IO_IN_IT_ID 2
  28. #define IO_OUT_OT_ID 3
  29. #define USB_IN_OT_ID 4
  30. #define USB_OUT_CLK_ID 5
  31. #define USB_IN_CLK_ID 6
  32. #define CONTROL_ABSENT 0
  33. #define CONTROL_RDONLY 1
  34. #define CONTROL_RDWR 3
  35. #define CLK_FREQ_CTRL 0
  36. #define CLK_VLD_CTRL 2
  37. #define COPY_CTRL 0
  38. #define CONN_CTRL 2
  39. #define OVRLD_CTRL 4
  40. #define CLSTR_CTRL 6
  41. #define UNFLW_CTRL 8
  42. #define OVFLW_CTRL 10
  43. struct f_uac2 {
  44. struct g_audio g_audio;
  45. u8 ac_intf, as_in_intf, as_out_intf;
  46. u8 ac_alt, as_in_alt, as_out_alt; /* needed for get_alt() */
  47. };
  48. static inline struct f_uac2 *func_to_uac2(struct usb_function *f)
  49. {
  50. return container_of(f, struct f_uac2, g_audio.func);
  51. }
  52. static inline
  53. struct f_uac2_opts *g_audio_to_uac2_opts(struct g_audio *agdev)
  54. {
  55. return container_of(agdev->func.fi, struct f_uac2_opts, func_inst);
  56. }
  57. /* --------- USB Function Interface ------------- */
  58. enum {
  59. STR_ASSOC,
  60. STR_IF_CTRL,
  61. STR_CLKSRC_IN,
  62. STR_CLKSRC_OUT,
  63. STR_USB_IT,
  64. STR_IO_IT,
  65. STR_USB_OT,
  66. STR_IO_OT,
  67. STR_AS_OUT_ALT0,
  68. STR_AS_OUT_ALT1,
  69. STR_AS_IN_ALT0,
  70. STR_AS_IN_ALT1,
  71. };
  72. static char clksrc_in[8];
  73. static char clksrc_out[8];
  74. static struct usb_string strings_fn[] = {
  75. [STR_ASSOC].s = "Source/Sink",
  76. [STR_IF_CTRL].s = "Topology Control",
  77. [STR_CLKSRC_IN].s = clksrc_in,
  78. [STR_CLKSRC_OUT].s = clksrc_out,
  79. [STR_USB_IT].s = "USBH Out",
  80. [STR_IO_IT].s = "USBD Out",
  81. [STR_USB_OT].s = "USBH In",
  82. [STR_IO_OT].s = "USBD In",
  83. [STR_AS_OUT_ALT0].s = "Playback Inactive",
  84. [STR_AS_OUT_ALT1].s = "Playback Active",
  85. [STR_AS_IN_ALT0].s = "Capture Inactive",
  86. [STR_AS_IN_ALT1].s = "Capture Active",
  87. { },
  88. };
  89. static struct usb_gadget_strings str_fn = {
  90. .language = 0x0409, /* en-us */
  91. .strings = strings_fn,
  92. };
  93. static struct usb_gadget_strings *fn_strings[] = {
  94. &str_fn,
  95. NULL,
  96. };
  97. static struct usb_interface_assoc_descriptor iad_desc = {
  98. .bLength = sizeof iad_desc,
  99. .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION,
  100. .bFirstInterface = 0,
  101. .bInterfaceCount = 3,
  102. .bFunctionClass = USB_CLASS_AUDIO,
  103. .bFunctionSubClass = UAC2_FUNCTION_SUBCLASS_UNDEFINED,
  104. .bFunctionProtocol = UAC_VERSION_2,
  105. };
  106. /* Audio Control Interface */
  107. static struct usb_interface_descriptor std_ac_if_desc = {
  108. .bLength = sizeof std_ac_if_desc,
  109. .bDescriptorType = USB_DT_INTERFACE,
  110. .bAlternateSetting = 0,
  111. .bNumEndpoints = 0,
  112. .bInterfaceClass = USB_CLASS_AUDIO,
  113. .bInterfaceSubClass = USB_SUBCLASS_AUDIOCONTROL,
  114. .bInterfaceProtocol = UAC_VERSION_2,
  115. };
  116. /* Clock source for IN traffic */
  117. static struct uac_clock_source_descriptor in_clk_src_desc = {
  118. .bLength = sizeof in_clk_src_desc,
  119. .bDescriptorType = USB_DT_CS_INTERFACE,
  120. .bDescriptorSubtype = UAC2_CLOCK_SOURCE,
  121. .bClockID = USB_IN_CLK_ID,
  122. .bmAttributes = UAC_CLOCK_SOURCE_TYPE_INT_FIXED,
  123. .bmControls = (CONTROL_RDONLY << CLK_FREQ_CTRL),
  124. .bAssocTerminal = 0,
  125. };
  126. /* Clock source for OUT traffic */
  127. static struct uac_clock_source_descriptor out_clk_src_desc = {
  128. .bLength = sizeof out_clk_src_desc,
  129. .bDescriptorType = USB_DT_CS_INTERFACE,
  130. .bDescriptorSubtype = UAC2_CLOCK_SOURCE,
  131. .bClockID = USB_OUT_CLK_ID,
  132. .bmAttributes = UAC_CLOCK_SOURCE_TYPE_INT_FIXED,
  133. .bmControls = (CONTROL_RDONLY << CLK_FREQ_CTRL),
  134. .bAssocTerminal = 0,
  135. };
  136. /* Input Terminal for USB_OUT */
  137. static struct uac2_input_terminal_descriptor usb_out_it_desc = {
  138. .bLength = sizeof usb_out_it_desc,
  139. .bDescriptorType = USB_DT_CS_INTERFACE,
  140. .bDescriptorSubtype = UAC_INPUT_TERMINAL,
  141. .bTerminalID = USB_OUT_IT_ID,
  142. .wTerminalType = cpu_to_le16(UAC_TERMINAL_STREAMING),
  143. .bAssocTerminal = 0,
  144. .bCSourceID = USB_OUT_CLK_ID,
  145. .iChannelNames = 0,
  146. .bmControls = cpu_to_le16(CONTROL_RDWR << COPY_CTRL),
  147. };
  148. /* Input Terminal for I/O-In */
  149. static struct uac2_input_terminal_descriptor io_in_it_desc = {
  150. .bLength = sizeof io_in_it_desc,
  151. .bDescriptorType = USB_DT_CS_INTERFACE,
  152. .bDescriptorSubtype = UAC_INPUT_TERMINAL,
  153. .bTerminalID = IO_IN_IT_ID,
  154. .wTerminalType = cpu_to_le16(UAC_INPUT_TERMINAL_UNDEFINED),
  155. .bAssocTerminal = 0,
  156. .bCSourceID = USB_IN_CLK_ID,
  157. .iChannelNames = 0,
  158. .bmControls = cpu_to_le16(CONTROL_RDWR << COPY_CTRL),
  159. };
  160. /* Ouput Terminal for USB_IN */
  161. static struct uac2_output_terminal_descriptor usb_in_ot_desc = {
  162. .bLength = sizeof usb_in_ot_desc,
  163. .bDescriptorType = USB_DT_CS_INTERFACE,
  164. .bDescriptorSubtype = UAC_OUTPUT_TERMINAL,
  165. .bTerminalID = USB_IN_OT_ID,
  166. .wTerminalType = cpu_to_le16(UAC_TERMINAL_STREAMING),
  167. .bAssocTerminal = 0,
  168. .bSourceID = IO_IN_IT_ID,
  169. .bCSourceID = USB_IN_CLK_ID,
  170. .bmControls = cpu_to_le16(CONTROL_RDWR << COPY_CTRL),
  171. };
  172. /* Ouput Terminal for I/O-Out */
  173. static struct uac2_output_terminal_descriptor io_out_ot_desc = {
  174. .bLength = sizeof io_out_ot_desc,
  175. .bDescriptorType = USB_DT_CS_INTERFACE,
  176. .bDescriptorSubtype = UAC_OUTPUT_TERMINAL,
  177. .bTerminalID = IO_OUT_OT_ID,
  178. .wTerminalType = cpu_to_le16(UAC_OUTPUT_TERMINAL_UNDEFINED),
  179. .bAssocTerminal = 0,
  180. .bSourceID = USB_OUT_IT_ID,
  181. .bCSourceID = USB_OUT_CLK_ID,
  182. .bmControls = cpu_to_le16(CONTROL_RDWR << COPY_CTRL),
  183. };
  184. static struct uac2_ac_header_descriptor ac_hdr_desc = {
  185. .bLength = sizeof ac_hdr_desc,
  186. .bDescriptorType = USB_DT_CS_INTERFACE,
  187. .bDescriptorSubtype = UAC_MS_HEADER,
  188. .bcdADC = cpu_to_le16(0x200),
  189. .bCategory = UAC2_FUNCTION_IO_BOX,
  190. .wTotalLength = cpu_to_le16(sizeof in_clk_src_desc
  191. + sizeof out_clk_src_desc + sizeof usb_out_it_desc
  192. + sizeof io_in_it_desc + sizeof usb_in_ot_desc
  193. + sizeof io_out_ot_desc),
  194. .bmControls = 0,
  195. };
  196. /* Audio Streaming OUT Interface - Alt0 */
  197. static struct usb_interface_descriptor std_as_out_if0_desc = {
  198. .bLength = sizeof std_as_out_if0_desc,
  199. .bDescriptorType = USB_DT_INTERFACE,
  200. .bAlternateSetting = 0,
  201. .bNumEndpoints = 0,
  202. .bInterfaceClass = USB_CLASS_AUDIO,
  203. .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
  204. .bInterfaceProtocol = UAC_VERSION_2,
  205. };
  206. /* Audio Streaming OUT Interface - Alt1 */
  207. static struct usb_interface_descriptor std_as_out_if1_desc = {
  208. .bLength = sizeof std_as_out_if1_desc,
  209. .bDescriptorType = USB_DT_INTERFACE,
  210. .bAlternateSetting = 1,
  211. .bNumEndpoints = 1,
  212. .bInterfaceClass = USB_CLASS_AUDIO,
  213. .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
  214. .bInterfaceProtocol = UAC_VERSION_2,
  215. };
  216. /* Audio Stream OUT Intface Desc */
  217. static struct uac2_as_header_descriptor as_out_hdr_desc = {
  218. .bLength = sizeof as_out_hdr_desc,
  219. .bDescriptorType = USB_DT_CS_INTERFACE,
  220. .bDescriptorSubtype = UAC_AS_GENERAL,
  221. .bTerminalLink = USB_OUT_IT_ID,
  222. .bmControls = 0,
  223. .bFormatType = UAC_FORMAT_TYPE_I,
  224. .bmFormats = cpu_to_le32(UAC_FORMAT_TYPE_I_PCM),
  225. .iChannelNames = 0,
  226. };
  227. /* Audio USB_OUT Format */
  228. static struct uac2_format_type_i_descriptor as_out_fmt1_desc = {
  229. .bLength = sizeof as_out_fmt1_desc,
  230. .bDescriptorType = USB_DT_CS_INTERFACE,
  231. .bDescriptorSubtype = UAC_FORMAT_TYPE,
  232. .bFormatType = UAC_FORMAT_TYPE_I,
  233. };
  234. /* STD AS ISO OUT Endpoint */
  235. static struct usb_endpoint_descriptor fs_epout_desc = {
  236. .bLength = USB_DT_ENDPOINT_SIZE,
  237. .bDescriptorType = USB_DT_ENDPOINT,
  238. .bEndpointAddress = USB_DIR_OUT,
  239. .bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC,
  240. .wMaxPacketSize = cpu_to_le16(1023),
  241. .bInterval = 1,
  242. };
  243. static struct usb_endpoint_descriptor hs_epout_desc = {
  244. .bLength = USB_DT_ENDPOINT_SIZE,
  245. .bDescriptorType = USB_DT_ENDPOINT,
  246. .bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC,
  247. .wMaxPacketSize = cpu_to_le16(1024),
  248. .bInterval = 4,
  249. };
  250. /* CS AS ISO OUT Endpoint */
  251. static struct uac2_iso_endpoint_descriptor as_iso_out_desc = {
  252. .bLength = sizeof as_iso_out_desc,
  253. .bDescriptorType = USB_DT_CS_ENDPOINT,
  254. .bDescriptorSubtype = UAC_EP_GENERAL,
  255. .bmAttributes = 0,
  256. .bmControls = 0,
  257. .bLockDelayUnits = 0,
  258. .wLockDelay = 0,
  259. };
  260. /* Audio Streaming IN Interface - Alt0 */
  261. static struct usb_interface_descriptor std_as_in_if0_desc = {
  262. .bLength = sizeof std_as_in_if0_desc,
  263. .bDescriptorType = USB_DT_INTERFACE,
  264. .bAlternateSetting = 0,
  265. .bNumEndpoints = 0,
  266. .bInterfaceClass = USB_CLASS_AUDIO,
  267. .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
  268. .bInterfaceProtocol = UAC_VERSION_2,
  269. };
  270. /* Audio Streaming IN Interface - Alt1 */
  271. static struct usb_interface_descriptor std_as_in_if1_desc = {
  272. .bLength = sizeof std_as_in_if1_desc,
  273. .bDescriptorType = USB_DT_INTERFACE,
  274. .bAlternateSetting = 1,
  275. .bNumEndpoints = 1,
  276. .bInterfaceClass = USB_CLASS_AUDIO,
  277. .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
  278. .bInterfaceProtocol = UAC_VERSION_2,
  279. };
  280. /* Audio Stream IN Intface Desc */
  281. static struct uac2_as_header_descriptor as_in_hdr_desc = {
  282. .bLength = sizeof as_in_hdr_desc,
  283. .bDescriptorType = USB_DT_CS_INTERFACE,
  284. .bDescriptorSubtype = UAC_AS_GENERAL,
  285. .bTerminalLink = USB_IN_OT_ID,
  286. .bmControls = 0,
  287. .bFormatType = UAC_FORMAT_TYPE_I,
  288. .bmFormats = cpu_to_le32(UAC_FORMAT_TYPE_I_PCM),
  289. .iChannelNames = 0,
  290. };
  291. /* Audio USB_IN Format */
  292. static struct uac2_format_type_i_descriptor as_in_fmt1_desc = {
  293. .bLength = sizeof as_in_fmt1_desc,
  294. .bDescriptorType = USB_DT_CS_INTERFACE,
  295. .bDescriptorSubtype = UAC_FORMAT_TYPE,
  296. .bFormatType = UAC_FORMAT_TYPE_I,
  297. };
  298. /* STD AS ISO IN Endpoint */
  299. static struct usb_endpoint_descriptor fs_epin_desc = {
  300. .bLength = USB_DT_ENDPOINT_SIZE,
  301. .bDescriptorType = USB_DT_ENDPOINT,
  302. .bEndpointAddress = USB_DIR_IN,
  303. .bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC,
  304. .wMaxPacketSize = cpu_to_le16(1023),
  305. .bInterval = 1,
  306. };
  307. static struct usb_endpoint_descriptor hs_epin_desc = {
  308. .bLength = USB_DT_ENDPOINT_SIZE,
  309. .bDescriptorType = USB_DT_ENDPOINT,
  310. .bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC,
  311. .wMaxPacketSize = cpu_to_le16(1024),
  312. .bInterval = 4,
  313. };
  314. /* CS AS ISO IN Endpoint */
  315. static struct uac2_iso_endpoint_descriptor as_iso_in_desc = {
  316. .bLength = sizeof as_iso_in_desc,
  317. .bDescriptorType = USB_DT_CS_ENDPOINT,
  318. .bDescriptorSubtype = UAC_EP_GENERAL,
  319. .bmAttributes = 0,
  320. .bmControls = 0,
  321. .bLockDelayUnits = 0,
  322. .wLockDelay = 0,
  323. };
  324. static struct usb_descriptor_header *fs_audio_desc[] = {
  325. (struct usb_descriptor_header *)&iad_desc,
  326. (struct usb_descriptor_header *)&std_ac_if_desc,
  327. (struct usb_descriptor_header *)&ac_hdr_desc,
  328. (struct usb_descriptor_header *)&in_clk_src_desc,
  329. (struct usb_descriptor_header *)&out_clk_src_desc,
  330. (struct usb_descriptor_header *)&usb_out_it_desc,
  331. (struct usb_descriptor_header *)&io_in_it_desc,
  332. (struct usb_descriptor_header *)&usb_in_ot_desc,
  333. (struct usb_descriptor_header *)&io_out_ot_desc,
  334. (struct usb_descriptor_header *)&std_as_out_if0_desc,
  335. (struct usb_descriptor_header *)&std_as_out_if1_desc,
  336. (struct usb_descriptor_header *)&as_out_hdr_desc,
  337. (struct usb_descriptor_header *)&as_out_fmt1_desc,
  338. (struct usb_descriptor_header *)&fs_epout_desc,
  339. (struct usb_descriptor_header *)&as_iso_out_desc,
  340. (struct usb_descriptor_header *)&std_as_in_if0_desc,
  341. (struct usb_descriptor_header *)&std_as_in_if1_desc,
  342. (struct usb_descriptor_header *)&as_in_hdr_desc,
  343. (struct usb_descriptor_header *)&as_in_fmt1_desc,
  344. (struct usb_descriptor_header *)&fs_epin_desc,
  345. (struct usb_descriptor_header *)&as_iso_in_desc,
  346. NULL,
  347. };
  348. static struct usb_descriptor_header *hs_audio_desc[] = {
  349. (struct usb_descriptor_header *)&iad_desc,
  350. (struct usb_descriptor_header *)&std_ac_if_desc,
  351. (struct usb_descriptor_header *)&ac_hdr_desc,
  352. (struct usb_descriptor_header *)&in_clk_src_desc,
  353. (struct usb_descriptor_header *)&out_clk_src_desc,
  354. (struct usb_descriptor_header *)&usb_out_it_desc,
  355. (struct usb_descriptor_header *)&io_in_it_desc,
  356. (struct usb_descriptor_header *)&usb_in_ot_desc,
  357. (struct usb_descriptor_header *)&io_out_ot_desc,
  358. (struct usb_descriptor_header *)&std_as_out_if0_desc,
  359. (struct usb_descriptor_header *)&std_as_out_if1_desc,
  360. (struct usb_descriptor_header *)&as_out_hdr_desc,
  361. (struct usb_descriptor_header *)&as_out_fmt1_desc,
  362. (struct usb_descriptor_header *)&hs_epout_desc,
  363. (struct usb_descriptor_header *)&as_iso_out_desc,
  364. (struct usb_descriptor_header *)&std_as_in_if0_desc,
  365. (struct usb_descriptor_header *)&std_as_in_if1_desc,
  366. (struct usb_descriptor_header *)&as_in_hdr_desc,
  367. (struct usb_descriptor_header *)&as_in_fmt1_desc,
  368. (struct usb_descriptor_header *)&hs_epin_desc,
  369. (struct usb_descriptor_header *)&as_iso_in_desc,
  370. NULL,
  371. };
  372. struct cntrl_cur_lay3 {
  373. __u32 dCUR;
  374. };
  375. struct cntrl_range_lay3 {
  376. __u16 wNumSubRanges;
  377. __u32 dMIN;
  378. __u32 dMAX;
  379. __u32 dRES;
  380. } __packed;
  381. static void set_ep_max_packet_size(const struct f_uac2_opts *uac2_opts,
  382. struct usb_endpoint_descriptor *ep_desc,
  383. unsigned int factor, bool is_playback)
  384. {
  385. int chmask, srate, ssize;
  386. u16 max_packet_size;
  387. if (is_playback) {
  388. chmask = uac2_opts->p_chmask;
  389. srate = uac2_opts->p_srate;
  390. ssize = uac2_opts->p_ssize;
  391. } else {
  392. chmask = uac2_opts->c_chmask;
  393. srate = uac2_opts->c_srate;
  394. ssize = uac2_opts->c_ssize;
  395. }
  396. max_packet_size = num_channels(chmask) * ssize *
  397. DIV_ROUND_UP(srate, factor / (1 << (ep_desc->bInterval - 1)));
  398. ep_desc->wMaxPacketSize = cpu_to_le16(min_t(u16, max_packet_size,
  399. le16_to_cpu(ep_desc->wMaxPacketSize)));
  400. }
  401. static int
  402. afunc_bind(struct usb_configuration *cfg, struct usb_function *fn)
  403. {
  404. struct f_uac2 *uac2 = func_to_uac2(fn);
  405. struct g_audio *agdev = func_to_g_audio(fn);
  406. struct usb_composite_dev *cdev = cfg->cdev;
  407. struct usb_gadget *gadget = cdev->gadget;
  408. struct device *dev = &gadget->dev;
  409. struct f_uac2_opts *uac2_opts;
  410. struct usb_string *us;
  411. int ret;
  412. uac2_opts = container_of(fn->fi, struct f_uac2_opts, func_inst);
  413. us = usb_gstrings_attach(cdev, fn_strings, ARRAY_SIZE(strings_fn));
  414. if (IS_ERR(us))
  415. return PTR_ERR(us);
  416. iad_desc.iFunction = us[STR_ASSOC].id;
  417. std_ac_if_desc.iInterface = us[STR_IF_CTRL].id;
  418. in_clk_src_desc.iClockSource = us[STR_CLKSRC_IN].id;
  419. out_clk_src_desc.iClockSource = us[STR_CLKSRC_OUT].id;
  420. usb_out_it_desc.iTerminal = us[STR_USB_IT].id;
  421. io_in_it_desc.iTerminal = us[STR_IO_IT].id;
  422. usb_in_ot_desc.iTerminal = us[STR_USB_OT].id;
  423. io_out_ot_desc.iTerminal = us[STR_IO_OT].id;
  424. std_as_out_if0_desc.iInterface = us[STR_AS_OUT_ALT0].id;
  425. std_as_out_if1_desc.iInterface = us[STR_AS_OUT_ALT1].id;
  426. std_as_in_if0_desc.iInterface = us[STR_AS_IN_ALT0].id;
  427. std_as_in_if1_desc.iInterface = us[STR_AS_IN_ALT1].id;
  428. /* Initialize the configurable parameters */
  429. usb_out_it_desc.bNrChannels = num_channels(uac2_opts->c_chmask);
  430. usb_out_it_desc.bmChannelConfig = cpu_to_le32(uac2_opts->c_chmask);
  431. io_in_it_desc.bNrChannels = num_channels(uac2_opts->p_chmask);
  432. io_in_it_desc.bmChannelConfig = cpu_to_le32(uac2_opts->p_chmask);
  433. as_out_hdr_desc.bNrChannels = num_channels(uac2_opts->c_chmask);
  434. as_out_hdr_desc.bmChannelConfig = cpu_to_le32(uac2_opts->c_chmask);
  435. as_in_hdr_desc.bNrChannels = num_channels(uac2_opts->p_chmask);
  436. as_in_hdr_desc.bmChannelConfig = cpu_to_le32(uac2_opts->p_chmask);
  437. as_out_fmt1_desc.bSubslotSize = uac2_opts->c_ssize;
  438. as_out_fmt1_desc.bBitResolution = uac2_opts->c_ssize * 8;
  439. as_in_fmt1_desc.bSubslotSize = uac2_opts->p_ssize;
  440. as_in_fmt1_desc.bBitResolution = uac2_opts->p_ssize * 8;
  441. snprintf(clksrc_in, sizeof(clksrc_in), "%uHz", uac2_opts->p_srate);
  442. snprintf(clksrc_out, sizeof(clksrc_out), "%uHz", uac2_opts->c_srate);
  443. ret = usb_interface_id(cfg, fn);
  444. if (ret < 0) {
  445. dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
  446. return ret;
  447. }
  448. std_ac_if_desc.bInterfaceNumber = ret;
  449. uac2->ac_intf = ret;
  450. uac2->ac_alt = 0;
  451. ret = usb_interface_id(cfg, fn);
  452. if (ret < 0) {
  453. dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
  454. return ret;
  455. }
  456. std_as_out_if0_desc.bInterfaceNumber = ret;
  457. std_as_out_if1_desc.bInterfaceNumber = ret;
  458. uac2->as_out_intf = ret;
  459. uac2->as_out_alt = 0;
  460. ret = usb_interface_id(cfg, fn);
  461. if (ret < 0) {
  462. dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
  463. return ret;
  464. }
  465. std_as_in_if0_desc.bInterfaceNumber = ret;
  466. std_as_in_if1_desc.bInterfaceNumber = ret;
  467. uac2->as_in_intf = ret;
  468. uac2->as_in_alt = 0;
  469. /* Calculate wMaxPacketSize according to audio bandwidth */
  470. set_ep_max_packet_size(uac2_opts, &fs_epin_desc, 1000, true);
  471. set_ep_max_packet_size(uac2_opts, &fs_epout_desc, 1000, false);
  472. set_ep_max_packet_size(uac2_opts, &hs_epin_desc, 8000, true);
  473. set_ep_max_packet_size(uac2_opts, &hs_epout_desc, 8000, false);
  474. agdev->out_ep = usb_ep_autoconfig(gadget, &fs_epout_desc);
  475. if (!agdev->out_ep) {
  476. dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
  477. return ret;
  478. }
  479. agdev->in_ep = usb_ep_autoconfig(gadget, &fs_epin_desc);
  480. if (!agdev->in_ep) {
  481. dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
  482. return ret;
  483. }
  484. agdev->in_ep_maxpsize = max_t(u16,
  485. le16_to_cpu(fs_epin_desc.wMaxPacketSize),
  486. le16_to_cpu(hs_epin_desc.wMaxPacketSize));
  487. agdev->out_ep_maxpsize = max_t(u16,
  488. le16_to_cpu(fs_epout_desc.wMaxPacketSize),
  489. le16_to_cpu(hs_epout_desc.wMaxPacketSize));
  490. hs_epout_desc.bEndpointAddress = fs_epout_desc.bEndpointAddress;
  491. hs_epin_desc.bEndpointAddress = fs_epin_desc.bEndpointAddress;
  492. ret = usb_assign_descriptors(fn, fs_audio_desc, hs_audio_desc, NULL,
  493. NULL);
  494. if (ret)
  495. return ret;
  496. agdev->gadget = gadget;
  497. agdev->params.p_chmask = uac2_opts->p_chmask;
  498. agdev->params.p_srate = uac2_opts->p_srate;
  499. agdev->params.p_ssize = uac2_opts->p_ssize;
  500. agdev->params.c_chmask = uac2_opts->c_chmask;
  501. agdev->params.c_srate = uac2_opts->c_srate;
  502. agdev->params.c_ssize = uac2_opts->c_ssize;
  503. agdev->params.req_number = uac2_opts->req_number;
  504. ret = g_audio_setup(agdev, "UAC2 PCM", "UAC2_Gadget");
  505. if (ret)
  506. goto err_free_descs;
  507. return 0;
  508. err_free_descs:
  509. usb_free_all_descriptors(fn);
  510. agdev->gadget = NULL;
  511. return ret;
  512. }
  513. static int
  514. afunc_set_alt(struct usb_function *fn, unsigned intf, unsigned alt)
  515. {
  516. struct usb_composite_dev *cdev = fn->config->cdev;
  517. struct f_uac2 *uac2 = func_to_uac2(fn);
  518. struct usb_gadget *gadget = cdev->gadget;
  519. struct device *dev = &gadget->dev;
  520. int ret = 0;
  521. /* No i/f has more than 2 alt settings */
  522. if (alt > 1) {
  523. dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
  524. return -EINVAL;
  525. }
  526. if (intf == uac2->ac_intf) {
  527. /* Control I/f has only 1 AltSetting - 0 */
  528. if (alt) {
  529. dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
  530. return -EINVAL;
  531. }
  532. return 0;
  533. }
  534. if (intf == uac2->as_out_intf) {
  535. uac2->as_out_alt = alt;
  536. if (alt)
  537. ret = u_audio_start_capture(&uac2->g_audio);
  538. else
  539. u_audio_stop_capture(&uac2->g_audio);
  540. } else if (intf == uac2->as_in_intf) {
  541. uac2->as_in_alt = alt;
  542. if (alt)
  543. ret = u_audio_start_playback(&uac2->g_audio);
  544. else
  545. u_audio_stop_playback(&uac2->g_audio);
  546. } else {
  547. dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
  548. return -EINVAL;
  549. }
  550. return ret;
  551. }
  552. static int
  553. afunc_get_alt(struct usb_function *fn, unsigned intf)
  554. {
  555. struct f_uac2 *uac2 = func_to_uac2(fn);
  556. struct g_audio *agdev = func_to_g_audio(fn);
  557. if (intf == uac2->ac_intf)
  558. return uac2->ac_alt;
  559. else if (intf == uac2->as_out_intf)
  560. return uac2->as_out_alt;
  561. else if (intf == uac2->as_in_intf)
  562. return uac2->as_in_alt;
  563. else
  564. dev_err(&agdev->gadget->dev,
  565. "%s:%d Invalid Interface %d!\n",
  566. __func__, __LINE__, intf);
  567. return -EINVAL;
  568. }
  569. static void
  570. afunc_disable(struct usb_function *fn)
  571. {
  572. struct f_uac2 *uac2 = func_to_uac2(fn);
  573. uac2->as_in_alt = 0;
  574. uac2->as_out_alt = 0;
  575. u_audio_stop_capture(&uac2->g_audio);
  576. u_audio_stop_playback(&uac2->g_audio);
  577. }
  578. static int
  579. in_rq_cur(struct usb_function *fn, const struct usb_ctrlrequest *cr)
  580. {
  581. struct usb_request *req = fn->config->cdev->req;
  582. struct g_audio *agdev = func_to_g_audio(fn);
  583. struct f_uac2_opts *opts;
  584. u16 w_length = le16_to_cpu(cr->wLength);
  585. u16 w_index = le16_to_cpu(cr->wIndex);
  586. u16 w_value = le16_to_cpu(cr->wValue);
  587. u8 entity_id = (w_index >> 8) & 0xff;
  588. u8 control_selector = w_value >> 8;
  589. int value = -EOPNOTSUPP;
  590. int p_srate, c_srate;
  591. opts = g_audio_to_uac2_opts(agdev);
  592. p_srate = opts->p_srate;
  593. c_srate = opts->c_srate;
  594. if (control_selector == UAC2_CS_CONTROL_SAM_FREQ) {
  595. struct cntrl_cur_lay3 c;
  596. memset(&c, 0, sizeof(struct cntrl_cur_lay3));
  597. if (entity_id == USB_IN_CLK_ID)
  598. c.dCUR = p_srate;
  599. else if (entity_id == USB_OUT_CLK_ID)
  600. c.dCUR = c_srate;
  601. value = min_t(unsigned, w_length, sizeof c);
  602. memcpy(req->buf, &c, value);
  603. } else if (control_selector == UAC2_CS_CONTROL_CLOCK_VALID) {
  604. *(u8 *)req->buf = 1;
  605. value = min_t(unsigned, w_length, 1);
  606. } else {
  607. dev_err(&agdev->gadget->dev,
  608. "%s:%d control_selector=%d TODO!\n",
  609. __func__, __LINE__, control_selector);
  610. }
  611. return value;
  612. }
  613. static int
  614. in_rq_range(struct usb_function *fn, const struct usb_ctrlrequest *cr)
  615. {
  616. struct usb_request *req = fn->config->cdev->req;
  617. struct g_audio *agdev = func_to_g_audio(fn);
  618. struct f_uac2_opts *opts;
  619. u16 w_length = le16_to_cpu(cr->wLength);
  620. u16 w_index = le16_to_cpu(cr->wIndex);
  621. u16 w_value = le16_to_cpu(cr->wValue);
  622. u8 entity_id = (w_index >> 8) & 0xff;
  623. u8 control_selector = w_value >> 8;
  624. struct cntrl_range_lay3 r;
  625. int value = -EOPNOTSUPP;
  626. int p_srate, c_srate;
  627. opts = g_audio_to_uac2_opts(agdev);
  628. p_srate = opts->p_srate;
  629. c_srate = opts->c_srate;
  630. if (control_selector == UAC2_CS_CONTROL_SAM_FREQ) {
  631. if (entity_id == USB_IN_CLK_ID)
  632. r.dMIN = p_srate;
  633. else if (entity_id == USB_OUT_CLK_ID)
  634. r.dMIN = c_srate;
  635. else
  636. return -EOPNOTSUPP;
  637. r.dMAX = r.dMIN;
  638. r.dRES = 0;
  639. r.wNumSubRanges = 1;
  640. value = min_t(unsigned, w_length, sizeof r);
  641. memcpy(req->buf, &r, value);
  642. } else {
  643. dev_err(&agdev->gadget->dev,
  644. "%s:%d control_selector=%d TODO!\n",
  645. __func__, __LINE__, control_selector);
  646. }
  647. return value;
  648. }
  649. static int
  650. ac_rq_in(struct usb_function *fn, const struct usb_ctrlrequest *cr)
  651. {
  652. if (cr->bRequest == UAC2_CS_CUR)
  653. return in_rq_cur(fn, cr);
  654. else if (cr->bRequest == UAC2_CS_RANGE)
  655. return in_rq_range(fn, cr);
  656. else
  657. return -EOPNOTSUPP;
  658. }
  659. static int
  660. out_rq_cur(struct usb_function *fn, const struct usb_ctrlrequest *cr)
  661. {
  662. u16 w_length = le16_to_cpu(cr->wLength);
  663. u16 w_value = le16_to_cpu(cr->wValue);
  664. u8 control_selector = w_value >> 8;
  665. if (control_selector == UAC2_CS_CONTROL_SAM_FREQ)
  666. return w_length;
  667. return -EOPNOTSUPP;
  668. }
  669. static int
  670. setup_rq_inf(struct usb_function *fn, const struct usb_ctrlrequest *cr)
  671. {
  672. struct f_uac2 *uac2 = func_to_uac2(fn);
  673. struct g_audio *agdev = func_to_g_audio(fn);
  674. u16 w_index = le16_to_cpu(cr->wIndex);
  675. u8 intf = w_index & 0xff;
  676. if (intf != uac2->ac_intf) {
  677. dev_err(&agdev->gadget->dev,
  678. "%s:%d Error!\n", __func__, __LINE__);
  679. return -EOPNOTSUPP;
  680. }
  681. if (cr->bRequestType & USB_DIR_IN)
  682. return ac_rq_in(fn, cr);
  683. else if (cr->bRequest == UAC2_CS_CUR)
  684. return out_rq_cur(fn, cr);
  685. return -EOPNOTSUPP;
  686. }
  687. static int
  688. afunc_setup(struct usb_function *fn, const struct usb_ctrlrequest *cr)
  689. {
  690. struct usb_composite_dev *cdev = fn->config->cdev;
  691. struct g_audio *agdev = func_to_g_audio(fn);
  692. struct usb_request *req = cdev->req;
  693. u16 w_length = le16_to_cpu(cr->wLength);
  694. int value = -EOPNOTSUPP;
  695. /* Only Class specific requests are supposed to reach here */
  696. if ((cr->bRequestType & USB_TYPE_MASK) != USB_TYPE_CLASS)
  697. return -EOPNOTSUPP;
  698. if ((cr->bRequestType & USB_RECIP_MASK) == USB_RECIP_INTERFACE)
  699. value = setup_rq_inf(fn, cr);
  700. else
  701. dev_err(&agdev->gadget->dev, "%s:%d Error!\n",
  702. __func__, __LINE__);
  703. if (value >= 0) {
  704. req->length = value;
  705. req->zero = value < w_length;
  706. value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
  707. if (value < 0) {
  708. dev_err(&agdev->gadget->dev,
  709. "%s:%d Error!\n", __func__, __LINE__);
  710. req->status = 0;
  711. }
  712. }
  713. return value;
  714. }
  715. static inline struct f_uac2_opts *to_f_uac2_opts(struct config_item *item)
  716. {
  717. return container_of(to_config_group(item), struct f_uac2_opts,
  718. func_inst.group);
  719. }
  720. static void f_uac2_attr_release(struct config_item *item)
  721. {
  722. struct f_uac2_opts *opts = to_f_uac2_opts(item);
  723. usb_put_function_instance(&opts->func_inst);
  724. }
  725. static struct configfs_item_operations f_uac2_item_ops = {
  726. .release = f_uac2_attr_release,
  727. };
  728. #define UAC2_ATTRIBUTE(name) \
  729. static ssize_t f_uac2_opts_##name##_show(struct config_item *item, \
  730. char *page) \
  731. { \
  732. struct f_uac2_opts *opts = to_f_uac2_opts(item); \
  733. int result; \
  734. \
  735. mutex_lock(&opts->lock); \
  736. result = sprintf(page, "%u\n", opts->name); \
  737. mutex_unlock(&opts->lock); \
  738. \
  739. return result; \
  740. } \
  741. \
  742. static ssize_t f_uac2_opts_##name##_store(struct config_item *item, \
  743. const char *page, size_t len) \
  744. { \
  745. struct f_uac2_opts *opts = to_f_uac2_opts(item); \
  746. int ret; \
  747. u32 num; \
  748. \
  749. mutex_lock(&opts->lock); \
  750. if (opts->refcnt) { \
  751. ret = -EBUSY; \
  752. goto end; \
  753. } \
  754. \
  755. ret = kstrtou32(page, 0, &num); \
  756. if (ret) \
  757. goto end; \
  758. \
  759. opts->name = num; \
  760. ret = len; \
  761. \
  762. end: \
  763. mutex_unlock(&opts->lock); \
  764. return ret; \
  765. } \
  766. \
  767. CONFIGFS_ATTR(f_uac2_opts_, name)
  768. UAC2_ATTRIBUTE(p_chmask);
  769. UAC2_ATTRIBUTE(p_srate);
  770. UAC2_ATTRIBUTE(p_ssize);
  771. UAC2_ATTRIBUTE(c_chmask);
  772. UAC2_ATTRIBUTE(c_srate);
  773. UAC2_ATTRIBUTE(c_ssize);
  774. UAC2_ATTRIBUTE(req_number);
  775. static struct configfs_attribute *f_uac2_attrs[] = {
  776. &f_uac2_opts_attr_p_chmask,
  777. &f_uac2_opts_attr_p_srate,
  778. &f_uac2_opts_attr_p_ssize,
  779. &f_uac2_opts_attr_c_chmask,
  780. &f_uac2_opts_attr_c_srate,
  781. &f_uac2_opts_attr_c_ssize,
  782. &f_uac2_opts_attr_req_number,
  783. NULL,
  784. };
  785. static struct config_item_type f_uac2_func_type = {
  786. .ct_item_ops = &f_uac2_item_ops,
  787. .ct_attrs = f_uac2_attrs,
  788. .ct_owner = THIS_MODULE,
  789. };
  790. static void afunc_free_inst(struct usb_function_instance *f)
  791. {
  792. struct f_uac2_opts *opts;
  793. opts = container_of(f, struct f_uac2_opts, func_inst);
  794. kfree(opts);
  795. }
  796. static struct usb_function_instance *afunc_alloc_inst(void)
  797. {
  798. struct f_uac2_opts *opts;
  799. opts = kzalloc(sizeof(*opts), GFP_KERNEL);
  800. if (!opts)
  801. return ERR_PTR(-ENOMEM);
  802. mutex_init(&opts->lock);
  803. opts->func_inst.free_func_inst = afunc_free_inst;
  804. config_group_init_type_name(&opts->func_inst.group, "",
  805. &f_uac2_func_type);
  806. opts->p_chmask = UAC2_DEF_PCHMASK;
  807. opts->p_srate = UAC2_DEF_PSRATE;
  808. opts->p_ssize = UAC2_DEF_PSSIZE;
  809. opts->c_chmask = UAC2_DEF_CCHMASK;
  810. opts->c_srate = UAC2_DEF_CSRATE;
  811. opts->c_ssize = UAC2_DEF_CSSIZE;
  812. opts->req_number = UAC2_DEF_REQ_NUM;
  813. return &opts->func_inst;
  814. }
  815. static void afunc_free(struct usb_function *f)
  816. {
  817. struct g_audio *agdev;
  818. struct f_uac2_opts *opts;
  819. agdev = func_to_g_audio(f);
  820. opts = container_of(f->fi, struct f_uac2_opts, func_inst);
  821. kfree(agdev);
  822. mutex_lock(&opts->lock);
  823. --opts->refcnt;
  824. mutex_unlock(&opts->lock);
  825. }
  826. static void afunc_unbind(struct usb_configuration *c, struct usb_function *f)
  827. {
  828. struct g_audio *agdev = func_to_g_audio(f);
  829. g_audio_cleanup(agdev);
  830. usb_free_all_descriptors(f);
  831. agdev->gadget = NULL;
  832. }
  833. static struct usb_function *afunc_alloc(struct usb_function_instance *fi)
  834. {
  835. struct f_uac2 *uac2;
  836. struct f_uac2_opts *opts;
  837. uac2 = kzalloc(sizeof(*uac2), GFP_KERNEL);
  838. if (uac2 == NULL)
  839. return ERR_PTR(-ENOMEM);
  840. opts = container_of(fi, struct f_uac2_opts, func_inst);
  841. mutex_lock(&opts->lock);
  842. ++opts->refcnt;
  843. mutex_unlock(&opts->lock);
  844. uac2->g_audio.func.name = "uac2_func";
  845. uac2->g_audio.func.bind = afunc_bind;
  846. uac2->g_audio.func.unbind = afunc_unbind;
  847. uac2->g_audio.func.set_alt = afunc_set_alt;
  848. uac2->g_audio.func.get_alt = afunc_get_alt;
  849. uac2->g_audio.func.disable = afunc_disable;
  850. uac2->g_audio.func.setup = afunc_setup;
  851. uac2->g_audio.func.free_func = afunc_free;
  852. return &uac2->g_audio.func;
  853. }
  854. DECLARE_USB_FUNCTION_INIT(uac2, afunc_alloc_inst, afunc_alloc);
  855. MODULE_LICENSE("GPL");
  856. MODULE_AUTHOR("Yadwinder Singh");
  857. MODULE_AUTHOR("Jaswinder Singh");