f_uac2.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053
  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 = DYNAMIC */
  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 = DYNAMIC */
  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 = DYNAMIC */
  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 = DYNAMIC */
  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 int set_ep_max_packet_size(const struct f_uac2_opts *uac2_opts,
  378. struct usb_endpoint_descriptor *ep_desc,
  379. enum usb_device_speed speed, bool is_playback)
  380. {
  381. int chmask, srate, ssize;
  382. u16 max_size_bw, max_size_ep;
  383. unsigned int factor;
  384. switch (speed) {
  385. case USB_SPEED_FULL:
  386. max_size_ep = 1023;
  387. factor = 1000;
  388. break;
  389. case USB_SPEED_HIGH:
  390. max_size_ep = 1024;
  391. factor = 8000;
  392. break;
  393. default:
  394. return -EINVAL;
  395. }
  396. if (is_playback) {
  397. chmask = uac2_opts->p_chmask;
  398. srate = uac2_opts->p_srate;
  399. ssize = uac2_opts->p_ssize;
  400. } else {
  401. chmask = uac2_opts->c_chmask;
  402. srate = uac2_opts->c_srate;
  403. ssize = uac2_opts->c_ssize;
  404. }
  405. max_size_bw = num_channels(chmask) * ssize *
  406. ((srate / (factor / (1 << (ep_desc->bInterval - 1)))) + 1);
  407. ep_desc->wMaxPacketSize = cpu_to_le16(min_t(u16, max_size_bw,
  408. max_size_ep));
  409. return 0;
  410. }
  411. static int
  412. afunc_bind(struct usb_configuration *cfg, struct usb_function *fn)
  413. {
  414. struct f_uac2 *uac2 = func_to_uac2(fn);
  415. struct g_audio *agdev = func_to_g_audio(fn);
  416. struct usb_composite_dev *cdev = cfg->cdev;
  417. struct usb_gadget *gadget = cdev->gadget;
  418. struct device *dev = &gadget->dev;
  419. struct f_uac2_opts *uac2_opts;
  420. struct usb_string *us;
  421. int ret;
  422. uac2_opts = container_of(fn->fi, struct f_uac2_opts, func_inst);
  423. us = usb_gstrings_attach(cdev, fn_strings, ARRAY_SIZE(strings_fn));
  424. if (IS_ERR(us))
  425. return PTR_ERR(us);
  426. iad_desc.iFunction = us[STR_ASSOC].id;
  427. std_ac_if_desc.iInterface = us[STR_IF_CTRL].id;
  428. in_clk_src_desc.iClockSource = us[STR_CLKSRC_IN].id;
  429. out_clk_src_desc.iClockSource = us[STR_CLKSRC_OUT].id;
  430. usb_out_it_desc.iTerminal = us[STR_USB_IT].id;
  431. io_in_it_desc.iTerminal = us[STR_IO_IT].id;
  432. usb_in_ot_desc.iTerminal = us[STR_USB_OT].id;
  433. io_out_ot_desc.iTerminal = us[STR_IO_OT].id;
  434. std_as_out_if0_desc.iInterface = us[STR_AS_OUT_ALT0].id;
  435. std_as_out_if1_desc.iInterface = us[STR_AS_OUT_ALT1].id;
  436. std_as_in_if0_desc.iInterface = us[STR_AS_IN_ALT0].id;
  437. std_as_in_if1_desc.iInterface = us[STR_AS_IN_ALT1].id;
  438. /* Initialize the configurable parameters */
  439. usb_out_it_desc.bNrChannels = num_channels(uac2_opts->c_chmask);
  440. usb_out_it_desc.bmChannelConfig = cpu_to_le32(uac2_opts->c_chmask);
  441. io_in_it_desc.bNrChannels = num_channels(uac2_opts->p_chmask);
  442. io_in_it_desc.bmChannelConfig = cpu_to_le32(uac2_opts->p_chmask);
  443. as_out_hdr_desc.bNrChannels = num_channels(uac2_opts->c_chmask);
  444. as_out_hdr_desc.bmChannelConfig = cpu_to_le32(uac2_opts->c_chmask);
  445. as_in_hdr_desc.bNrChannels = num_channels(uac2_opts->p_chmask);
  446. as_in_hdr_desc.bmChannelConfig = cpu_to_le32(uac2_opts->p_chmask);
  447. as_out_fmt1_desc.bSubslotSize = uac2_opts->c_ssize;
  448. as_out_fmt1_desc.bBitResolution = uac2_opts->c_ssize * 8;
  449. as_in_fmt1_desc.bSubslotSize = uac2_opts->p_ssize;
  450. as_in_fmt1_desc.bBitResolution = uac2_opts->p_ssize * 8;
  451. snprintf(clksrc_in, sizeof(clksrc_in), "%uHz", uac2_opts->p_srate);
  452. snprintf(clksrc_out, sizeof(clksrc_out), "%uHz", uac2_opts->c_srate);
  453. ret = usb_interface_id(cfg, fn);
  454. if (ret < 0) {
  455. dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
  456. return ret;
  457. }
  458. iad_desc.bFirstInterface = ret;
  459. std_ac_if_desc.bInterfaceNumber = ret;
  460. uac2->ac_intf = ret;
  461. uac2->ac_alt = 0;
  462. ret = usb_interface_id(cfg, fn);
  463. if (ret < 0) {
  464. dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
  465. return ret;
  466. }
  467. std_as_out_if0_desc.bInterfaceNumber = ret;
  468. std_as_out_if1_desc.bInterfaceNumber = ret;
  469. uac2->as_out_intf = ret;
  470. uac2->as_out_alt = 0;
  471. ret = usb_interface_id(cfg, fn);
  472. if (ret < 0) {
  473. dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
  474. return ret;
  475. }
  476. std_as_in_if0_desc.bInterfaceNumber = ret;
  477. std_as_in_if1_desc.bInterfaceNumber = ret;
  478. uac2->as_in_intf = ret;
  479. uac2->as_in_alt = 0;
  480. /* Calculate wMaxPacketSize according to audio bandwidth */
  481. ret = set_ep_max_packet_size(uac2_opts, &fs_epin_desc, USB_SPEED_FULL,
  482. true);
  483. if (ret < 0) {
  484. dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
  485. return ret;
  486. }
  487. ret = set_ep_max_packet_size(uac2_opts, &fs_epout_desc, USB_SPEED_FULL,
  488. false);
  489. if (ret < 0) {
  490. dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
  491. return ret;
  492. }
  493. ret = set_ep_max_packet_size(uac2_opts, &hs_epin_desc, USB_SPEED_HIGH,
  494. true);
  495. if (ret < 0) {
  496. dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
  497. return ret;
  498. }
  499. ret = set_ep_max_packet_size(uac2_opts, &hs_epout_desc, USB_SPEED_HIGH,
  500. false);
  501. if (ret < 0) {
  502. dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
  503. return ret;
  504. }
  505. agdev->out_ep = usb_ep_autoconfig(gadget, &fs_epout_desc);
  506. if (!agdev->out_ep) {
  507. dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
  508. return -ENODEV;
  509. }
  510. agdev->in_ep = usb_ep_autoconfig(gadget, &fs_epin_desc);
  511. if (!agdev->in_ep) {
  512. dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
  513. return -ENODEV;
  514. }
  515. agdev->in_ep_maxpsize = max_t(u16,
  516. le16_to_cpu(fs_epin_desc.wMaxPacketSize),
  517. le16_to_cpu(hs_epin_desc.wMaxPacketSize));
  518. agdev->out_ep_maxpsize = max_t(u16,
  519. le16_to_cpu(fs_epout_desc.wMaxPacketSize),
  520. le16_to_cpu(hs_epout_desc.wMaxPacketSize));
  521. hs_epout_desc.bEndpointAddress = fs_epout_desc.bEndpointAddress;
  522. hs_epin_desc.bEndpointAddress = fs_epin_desc.bEndpointAddress;
  523. ret = usb_assign_descriptors(fn, fs_audio_desc, hs_audio_desc, NULL,
  524. NULL);
  525. if (ret)
  526. return ret;
  527. agdev->gadget = gadget;
  528. agdev->params.p_chmask = uac2_opts->p_chmask;
  529. agdev->params.p_srate = uac2_opts->p_srate;
  530. agdev->params.p_ssize = uac2_opts->p_ssize;
  531. agdev->params.c_chmask = uac2_opts->c_chmask;
  532. agdev->params.c_srate = uac2_opts->c_srate;
  533. agdev->params.c_ssize = uac2_opts->c_ssize;
  534. agdev->params.req_number = uac2_opts->req_number;
  535. ret = g_audio_setup(agdev, "UAC2 PCM", "UAC2_Gadget");
  536. if (ret)
  537. goto err_free_descs;
  538. return 0;
  539. err_free_descs:
  540. usb_free_all_descriptors(fn);
  541. agdev->gadget = NULL;
  542. return ret;
  543. }
  544. static int
  545. afunc_set_alt(struct usb_function *fn, unsigned intf, unsigned alt)
  546. {
  547. struct usb_composite_dev *cdev = fn->config->cdev;
  548. struct f_uac2 *uac2 = func_to_uac2(fn);
  549. struct usb_gadget *gadget = cdev->gadget;
  550. struct device *dev = &gadget->dev;
  551. int ret = 0;
  552. /* No i/f has more than 2 alt settings */
  553. if (alt > 1) {
  554. dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
  555. return -EINVAL;
  556. }
  557. if (intf == uac2->ac_intf) {
  558. /* Control I/f has only 1 AltSetting - 0 */
  559. if (alt) {
  560. dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
  561. return -EINVAL;
  562. }
  563. return 0;
  564. }
  565. if (intf == uac2->as_out_intf) {
  566. uac2->as_out_alt = alt;
  567. if (alt)
  568. ret = u_audio_start_capture(&uac2->g_audio);
  569. else
  570. u_audio_stop_capture(&uac2->g_audio);
  571. } else if (intf == uac2->as_in_intf) {
  572. uac2->as_in_alt = alt;
  573. if (alt)
  574. ret = u_audio_start_playback(&uac2->g_audio);
  575. else
  576. u_audio_stop_playback(&uac2->g_audio);
  577. } else {
  578. dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
  579. return -EINVAL;
  580. }
  581. return ret;
  582. }
  583. static int
  584. afunc_get_alt(struct usb_function *fn, unsigned intf)
  585. {
  586. struct f_uac2 *uac2 = func_to_uac2(fn);
  587. struct g_audio *agdev = func_to_g_audio(fn);
  588. if (intf == uac2->ac_intf)
  589. return uac2->ac_alt;
  590. else if (intf == uac2->as_out_intf)
  591. return uac2->as_out_alt;
  592. else if (intf == uac2->as_in_intf)
  593. return uac2->as_in_alt;
  594. else
  595. dev_err(&agdev->gadget->dev,
  596. "%s:%d Invalid Interface %d!\n",
  597. __func__, __LINE__, intf);
  598. return -EINVAL;
  599. }
  600. static void
  601. afunc_disable(struct usb_function *fn)
  602. {
  603. struct f_uac2 *uac2 = func_to_uac2(fn);
  604. uac2->as_in_alt = 0;
  605. uac2->as_out_alt = 0;
  606. u_audio_stop_capture(&uac2->g_audio);
  607. u_audio_stop_playback(&uac2->g_audio);
  608. }
  609. static int
  610. in_rq_cur(struct usb_function *fn, const struct usb_ctrlrequest *cr)
  611. {
  612. struct usb_request *req = fn->config->cdev->req;
  613. struct g_audio *agdev = func_to_g_audio(fn);
  614. struct f_uac2_opts *opts;
  615. u16 w_length = le16_to_cpu(cr->wLength);
  616. u16 w_index = le16_to_cpu(cr->wIndex);
  617. u16 w_value = le16_to_cpu(cr->wValue);
  618. u8 entity_id = (w_index >> 8) & 0xff;
  619. u8 control_selector = w_value >> 8;
  620. int value = -EOPNOTSUPP;
  621. int p_srate, c_srate;
  622. opts = g_audio_to_uac2_opts(agdev);
  623. p_srate = opts->p_srate;
  624. c_srate = opts->c_srate;
  625. if (control_selector == UAC2_CS_CONTROL_SAM_FREQ) {
  626. struct cntrl_cur_lay3 c;
  627. memset(&c, 0, sizeof(struct cntrl_cur_lay3));
  628. if (entity_id == USB_IN_CLK_ID)
  629. c.dCUR = cpu_to_le32(p_srate);
  630. else if (entity_id == USB_OUT_CLK_ID)
  631. c.dCUR = cpu_to_le32(c_srate);
  632. value = min_t(unsigned, w_length, sizeof c);
  633. memcpy(req->buf, &c, value);
  634. } else if (control_selector == UAC2_CS_CONTROL_CLOCK_VALID) {
  635. *(u8 *)req->buf = 1;
  636. value = min_t(unsigned, w_length, 1);
  637. } else {
  638. dev_err(&agdev->gadget->dev,
  639. "%s:%d control_selector=%d TODO!\n",
  640. __func__, __LINE__, control_selector);
  641. }
  642. return value;
  643. }
  644. static int
  645. in_rq_range(struct usb_function *fn, const struct usb_ctrlrequest *cr)
  646. {
  647. struct usb_request *req = fn->config->cdev->req;
  648. struct g_audio *agdev = func_to_g_audio(fn);
  649. struct f_uac2_opts *opts;
  650. u16 w_length = le16_to_cpu(cr->wLength);
  651. u16 w_index = le16_to_cpu(cr->wIndex);
  652. u16 w_value = le16_to_cpu(cr->wValue);
  653. u8 entity_id = (w_index >> 8) & 0xff;
  654. u8 control_selector = w_value >> 8;
  655. struct cntrl_range_lay3 r;
  656. int value = -EOPNOTSUPP;
  657. int p_srate, c_srate;
  658. opts = g_audio_to_uac2_opts(agdev);
  659. p_srate = opts->p_srate;
  660. c_srate = opts->c_srate;
  661. if (control_selector == UAC2_CS_CONTROL_SAM_FREQ) {
  662. if (entity_id == USB_IN_CLK_ID)
  663. r.dMIN = cpu_to_le32(p_srate);
  664. else if (entity_id == USB_OUT_CLK_ID)
  665. r.dMIN = cpu_to_le32(c_srate);
  666. else
  667. return -EOPNOTSUPP;
  668. r.dMAX = r.dMIN;
  669. r.dRES = 0;
  670. r.wNumSubRanges = cpu_to_le16(1);
  671. value = min_t(unsigned, w_length, sizeof r);
  672. memcpy(req->buf, &r, value);
  673. } else {
  674. dev_err(&agdev->gadget->dev,
  675. "%s:%d control_selector=%d TODO!\n",
  676. __func__, __LINE__, control_selector);
  677. }
  678. return value;
  679. }
  680. static int
  681. ac_rq_in(struct usb_function *fn, const struct usb_ctrlrequest *cr)
  682. {
  683. if (cr->bRequest == UAC2_CS_CUR)
  684. return in_rq_cur(fn, cr);
  685. else if (cr->bRequest == UAC2_CS_RANGE)
  686. return in_rq_range(fn, cr);
  687. else
  688. return -EOPNOTSUPP;
  689. }
  690. static int
  691. out_rq_cur(struct usb_function *fn, const struct usb_ctrlrequest *cr)
  692. {
  693. u16 w_length = le16_to_cpu(cr->wLength);
  694. u16 w_value = le16_to_cpu(cr->wValue);
  695. u8 control_selector = w_value >> 8;
  696. if (control_selector == UAC2_CS_CONTROL_SAM_FREQ)
  697. return w_length;
  698. return -EOPNOTSUPP;
  699. }
  700. static int
  701. setup_rq_inf(struct usb_function *fn, const struct usb_ctrlrequest *cr)
  702. {
  703. struct f_uac2 *uac2 = func_to_uac2(fn);
  704. struct g_audio *agdev = func_to_g_audio(fn);
  705. u16 w_index = le16_to_cpu(cr->wIndex);
  706. u8 intf = w_index & 0xff;
  707. if (intf != uac2->ac_intf) {
  708. dev_err(&agdev->gadget->dev,
  709. "%s:%d Error!\n", __func__, __LINE__);
  710. return -EOPNOTSUPP;
  711. }
  712. if (cr->bRequestType & USB_DIR_IN)
  713. return ac_rq_in(fn, cr);
  714. else if (cr->bRequest == UAC2_CS_CUR)
  715. return out_rq_cur(fn, cr);
  716. return -EOPNOTSUPP;
  717. }
  718. static int
  719. afunc_setup(struct usb_function *fn, const struct usb_ctrlrequest *cr)
  720. {
  721. struct usb_composite_dev *cdev = fn->config->cdev;
  722. struct g_audio *agdev = func_to_g_audio(fn);
  723. struct usb_request *req = cdev->req;
  724. u16 w_length = le16_to_cpu(cr->wLength);
  725. int value = -EOPNOTSUPP;
  726. /* Only Class specific requests are supposed to reach here */
  727. if ((cr->bRequestType & USB_TYPE_MASK) != USB_TYPE_CLASS)
  728. return -EOPNOTSUPP;
  729. if ((cr->bRequestType & USB_RECIP_MASK) == USB_RECIP_INTERFACE)
  730. value = setup_rq_inf(fn, cr);
  731. else
  732. dev_err(&agdev->gadget->dev, "%s:%d Error!\n",
  733. __func__, __LINE__);
  734. if (value >= 0) {
  735. req->length = value;
  736. req->zero = value < w_length;
  737. value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
  738. if (value < 0) {
  739. dev_err(&agdev->gadget->dev,
  740. "%s:%d Error!\n", __func__, __LINE__);
  741. req->status = 0;
  742. }
  743. }
  744. return value;
  745. }
  746. static inline struct f_uac2_opts *to_f_uac2_opts(struct config_item *item)
  747. {
  748. return container_of(to_config_group(item), struct f_uac2_opts,
  749. func_inst.group);
  750. }
  751. static void f_uac2_attr_release(struct config_item *item)
  752. {
  753. struct f_uac2_opts *opts = to_f_uac2_opts(item);
  754. usb_put_function_instance(&opts->func_inst);
  755. }
  756. static struct configfs_item_operations f_uac2_item_ops = {
  757. .release = f_uac2_attr_release,
  758. };
  759. #define UAC2_ATTRIBUTE(name) \
  760. static ssize_t f_uac2_opts_##name##_show(struct config_item *item, \
  761. char *page) \
  762. { \
  763. struct f_uac2_opts *opts = to_f_uac2_opts(item); \
  764. int result; \
  765. \
  766. mutex_lock(&opts->lock); \
  767. result = sprintf(page, "%u\n", opts->name); \
  768. mutex_unlock(&opts->lock); \
  769. \
  770. return result; \
  771. } \
  772. \
  773. static ssize_t f_uac2_opts_##name##_store(struct config_item *item, \
  774. const char *page, size_t len) \
  775. { \
  776. struct f_uac2_opts *opts = to_f_uac2_opts(item); \
  777. int ret; \
  778. u32 num; \
  779. \
  780. mutex_lock(&opts->lock); \
  781. if (opts->refcnt) { \
  782. ret = -EBUSY; \
  783. goto end; \
  784. } \
  785. \
  786. ret = kstrtou32(page, 0, &num); \
  787. if (ret) \
  788. goto end; \
  789. \
  790. opts->name = num; \
  791. ret = len; \
  792. \
  793. end: \
  794. mutex_unlock(&opts->lock); \
  795. return ret; \
  796. } \
  797. \
  798. CONFIGFS_ATTR(f_uac2_opts_, name)
  799. UAC2_ATTRIBUTE(p_chmask);
  800. UAC2_ATTRIBUTE(p_srate);
  801. UAC2_ATTRIBUTE(p_ssize);
  802. UAC2_ATTRIBUTE(c_chmask);
  803. UAC2_ATTRIBUTE(c_srate);
  804. UAC2_ATTRIBUTE(c_ssize);
  805. UAC2_ATTRIBUTE(req_number);
  806. static struct configfs_attribute *f_uac2_attrs[] = {
  807. &f_uac2_opts_attr_p_chmask,
  808. &f_uac2_opts_attr_p_srate,
  809. &f_uac2_opts_attr_p_ssize,
  810. &f_uac2_opts_attr_c_chmask,
  811. &f_uac2_opts_attr_c_srate,
  812. &f_uac2_opts_attr_c_ssize,
  813. &f_uac2_opts_attr_req_number,
  814. NULL,
  815. };
  816. static const struct config_item_type f_uac2_func_type = {
  817. .ct_item_ops = &f_uac2_item_ops,
  818. .ct_attrs = f_uac2_attrs,
  819. .ct_owner = THIS_MODULE,
  820. };
  821. static void afunc_free_inst(struct usb_function_instance *f)
  822. {
  823. struct f_uac2_opts *opts;
  824. opts = container_of(f, struct f_uac2_opts, func_inst);
  825. kfree(opts);
  826. }
  827. static struct usb_function_instance *afunc_alloc_inst(void)
  828. {
  829. struct f_uac2_opts *opts;
  830. opts = kzalloc(sizeof(*opts), GFP_KERNEL);
  831. if (!opts)
  832. return ERR_PTR(-ENOMEM);
  833. mutex_init(&opts->lock);
  834. opts->func_inst.free_func_inst = afunc_free_inst;
  835. config_group_init_type_name(&opts->func_inst.group, "",
  836. &f_uac2_func_type);
  837. opts->p_chmask = UAC2_DEF_PCHMASK;
  838. opts->p_srate = UAC2_DEF_PSRATE;
  839. opts->p_ssize = UAC2_DEF_PSSIZE;
  840. opts->c_chmask = UAC2_DEF_CCHMASK;
  841. opts->c_srate = UAC2_DEF_CSRATE;
  842. opts->c_ssize = UAC2_DEF_CSSIZE;
  843. opts->req_number = UAC2_DEF_REQ_NUM;
  844. return &opts->func_inst;
  845. }
  846. static void afunc_free(struct usb_function *f)
  847. {
  848. struct g_audio *agdev;
  849. struct f_uac2_opts *opts;
  850. agdev = func_to_g_audio(f);
  851. opts = container_of(f->fi, struct f_uac2_opts, func_inst);
  852. kfree(agdev);
  853. mutex_lock(&opts->lock);
  854. --opts->refcnt;
  855. mutex_unlock(&opts->lock);
  856. }
  857. static void afunc_unbind(struct usb_configuration *c, struct usb_function *f)
  858. {
  859. struct g_audio *agdev = func_to_g_audio(f);
  860. g_audio_cleanup(agdev);
  861. usb_free_all_descriptors(f);
  862. agdev->gadget = NULL;
  863. }
  864. static struct usb_function *afunc_alloc(struct usb_function_instance *fi)
  865. {
  866. struct f_uac2 *uac2;
  867. struct f_uac2_opts *opts;
  868. uac2 = kzalloc(sizeof(*uac2), GFP_KERNEL);
  869. if (uac2 == NULL)
  870. return ERR_PTR(-ENOMEM);
  871. opts = container_of(fi, struct f_uac2_opts, func_inst);
  872. mutex_lock(&opts->lock);
  873. ++opts->refcnt;
  874. mutex_unlock(&opts->lock);
  875. uac2->g_audio.func.name = "uac2_func";
  876. uac2->g_audio.func.bind = afunc_bind;
  877. uac2->g_audio.func.unbind = afunc_unbind;
  878. uac2->g_audio.func.set_alt = afunc_set_alt;
  879. uac2->g_audio.func.get_alt = afunc_get_alt;
  880. uac2->g_audio.func.disable = afunc_disable;
  881. uac2->g_audio.func.setup = afunc_setup;
  882. uac2->g_audio.func.free_func = afunc_free;
  883. return &uac2->g_audio.func;
  884. }
  885. DECLARE_USB_FUNCTION_INIT(uac2, afunc_alloc_inst, afunc_alloc);
  886. MODULE_LICENSE("GPL");
  887. MODULE_AUTHOR("Yadwinder Singh");
  888. MODULE_AUTHOR("Jaswinder Singh");