f_uac2.c 32 KB

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