f_uac2.c 27 KB

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