f_uac2.c 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245
  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 ([email protected])
  7. * Jaswinder Singh ([email protected])
  8. *
  9. * Copyright (C) 2020
  10. * Ruslan Bilovol ([email protected])
  11. */
  12. #include <linux/usb/audio.h>
  13. #include <linux/usb/audio-v2.h>
  14. #include <linux/module.h>
  15. #include "u_audio.h"
  16. #include "u_uac2.h"
  17. /* UAC2 spec: 4.1 Audio Channel Cluster Descriptor */
  18. #define UAC2_CHANNEL_MASK 0x07FFFFFF
  19. /*
  20. * The driver implements a simple UAC_2 topology.
  21. * USB-OUT -> IT_1 -> FU -> OT_3 -> ALSA_Capture
  22. * ALSA_Playback -> IT_2 -> FU -> OT_4 -> USB-IN
  23. * Capture and Playback sampling rates are independently
  24. * controlled by two clock sources :
  25. * CLK_5 := c_srate, and CLK_6 := p_srate
  26. */
  27. #define USB_OUT_CLK_ID (out_clk_src_desc.bClockID)
  28. #define USB_IN_CLK_ID (in_clk_src_desc.bClockID)
  29. #define USB_OUT_FU_ID (out_feature_unit_desc->bUnitID)
  30. #define USB_IN_FU_ID (in_feature_unit_desc->bUnitID)
  31. #define CONTROL_ABSENT 0
  32. #define CONTROL_RDONLY 1
  33. #define CONTROL_RDWR 3
  34. #define CLK_FREQ_CTRL 0
  35. #define CLK_VLD_CTRL 2
  36. #define FU_MUTE_CTRL 0
  37. #define FU_VOL_CTRL 2
  38. #define COPY_CTRL 0
  39. #define CONN_CTRL 2
  40. #define OVRLD_CTRL 4
  41. #define CLSTR_CTRL 6
  42. #define UNFLW_CTRL 8
  43. #define OVFLW_CTRL 10
  44. #define EPIN_EN(_opts) ((_opts)->p_chmask != 0)
  45. #define EPOUT_EN(_opts) ((_opts)->c_chmask != 0)
  46. #define FUIN_EN(_opts) (EPIN_EN(_opts) \
  47. && ((_opts)->p_mute_present \
  48. || (_opts)->p_volume_present))
  49. #define FUOUT_EN(_opts) (EPOUT_EN(_opts) \
  50. && ((_opts)->c_mute_present \
  51. || (_opts)->c_volume_present))
  52. #define EPOUT_FBACK_IN_EN(_opts) ((_opts)->c_sync == USB_ENDPOINT_SYNC_ASYNC)
  53. struct f_uac2 {
  54. struct g_audio g_audio;
  55. u8 ac_intf, as_in_intf, as_out_intf;
  56. u8 ac_alt, as_in_alt, as_out_alt; /* needed for get_alt() */
  57. struct usb_ctrlrequest setup_cr; /* will be used in data stage */
  58. /* Interrupt IN endpoint of AC interface */
  59. struct usb_ep *int_ep;
  60. atomic_t int_count;
  61. /* transient state, only valid during handling of a single control request */
  62. int clock_id;
  63. };
  64. static inline struct f_uac2 *func_to_uac2(struct usb_function *f)
  65. {
  66. return container_of(f, struct f_uac2, g_audio.func);
  67. }
  68. static inline
  69. struct f_uac2_opts *g_audio_to_uac2_opts(struct g_audio *agdev)
  70. {
  71. return container_of(agdev->func.fi, struct f_uac2_opts, func_inst);
  72. }
  73. static int afunc_notify(struct g_audio *agdev, int unit_id, int cs);
  74. /* --------- USB Function Interface ------------- */
  75. enum {
  76. STR_ASSOC,
  77. STR_IF_CTRL,
  78. STR_CLKSRC_IN,
  79. STR_CLKSRC_OUT,
  80. STR_USB_IT,
  81. STR_IO_IT,
  82. STR_USB_OT,
  83. STR_IO_OT,
  84. STR_FU_IN,
  85. STR_FU_OUT,
  86. STR_AS_OUT_ALT0,
  87. STR_AS_OUT_ALT1,
  88. STR_AS_IN_ALT0,
  89. STR_AS_IN_ALT1,
  90. };
  91. static struct usb_string strings_fn[] = {
  92. /* [STR_ASSOC].s = DYNAMIC, */
  93. [STR_IF_CTRL].s = "Topology Control",
  94. [STR_CLKSRC_IN].s = "Input Clock",
  95. [STR_CLKSRC_OUT].s = "Output Clock",
  96. [STR_USB_IT].s = "USBH Out",
  97. [STR_IO_IT].s = "USBD Out",
  98. [STR_USB_OT].s = "USBH In",
  99. [STR_IO_OT].s = "USBD In",
  100. [STR_FU_IN].s = "Capture Volume",
  101. [STR_FU_OUT].s = "Playback Volume",
  102. [STR_AS_OUT_ALT0].s = "Playback Inactive",
  103. [STR_AS_OUT_ALT1].s = "Playback Active",
  104. [STR_AS_IN_ALT0].s = "Capture Inactive",
  105. [STR_AS_IN_ALT1].s = "Capture Active",
  106. { },
  107. };
  108. static const char *const speed_names[] = {
  109. [USB_SPEED_UNKNOWN] = "UNKNOWN",
  110. [USB_SPEED_LOW] = "LS",
  111. [USB_SPEED_FULL] = "FS",
  112. [USB_SPEED_HIGH] = "HS",
  113. [USB_SPEED_WIRELESS] = "W",
  114. [USB_SPEED_SUPER] = "SS",
  115. [USB_SPEED_SUPER_PLUS] = "SS+",
  116. };
  117. static struct usb_gadget_strings str_fn = {
  118. .language = 0x0409, /* en-us */
  119. .strings = strings_fn,
  120. };
  121. static struct usb_gadget_strings *fn_strings[] = {
  122. &str_fn,
  123. NULL,
  124. };
  125. static struct usb_interface_assoc_descriptor iad_desc = {
  126. .bLength = sizeof iad_desc,
  127. .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION,
  128. .bFirstInterface = 0,
  129. .bInterfaceCount = 3,
  130. .bFunctionClass = USB_CLASS_AUDIO,
  131. .bFunctionSubClass = UAC2_FUNCTION_SUBCLASS_UNDEFINED,
  132. .bFunctionProtocol = UAC_VERSION_2,
  133. };
  134. /* Audio Control Interface */
  135. static struct usb_interface_descriptor std_ac_if_desc = {
  136. .bLength = sizeof std_ac_if_desc,
  137. .bDescriptorType = USB_DT_INTERFACE,
  138. .bAlternateSetting = 0,
  139. /* .bNumEndpoints = DYNAMIC */
  140. .bInterfaceClass = USB_CLASS_AUDIO,
  141. .bInterfaceSubClass = USB_SUBCLASS_AUDIOCONTROL,
  142. .bInterfaceProtocol = UAC_VERSION_2,
  143. };
  144. /* Clock source for IN traffic */
  145. static struct uac_clock_source_descriptor in_clk_src_desc = {
  146. .bLength = sizeof in_clk_src_desc,
  147. .bDescriptorType = USB_DT_CS_INTERFACE,
  148. .bDescriptorSubtype = UAC2_CLOCK_SOURCE,
  149. /* .bClockID = DYNAMIC */
  150. .bmAttributes = UAC_CLOCK_SOURCE_TYPE_INT_FIXED,
  151. .bmControls = (CONTROL_RDWR << CLK_FREQ_CTRL),
  152. .bAssocTerminal = 0,
  153. };
  154. /* Clock source for OUT traffic */
  155. static struct uac_clock_source_descriptor out_clk_src_desc = {
  156. .bLength = sizeof out_clk_src_desc,
  157. .bDescriptorType = USB_DT_CS_INTERFACE,
  158. .bDescriptorSubtype = UAC2_CLOCK_SOURCE,
  159. /* .bClockID = DYNAMIC */
  160. .bmAttributes = UAC_CLOCK_SOURCE_TYPE_INT_FIXED,
  161. .bmControls = (CONTROL_RDWR << CLK_FREQ_CTRL),
  162. .bAssocTerminal = 0,
  163. };
  164. /* Input Terminal for USB_OUT */
  165. static struct uac2_input_terminal_descriptor usb_out_it_desc = {
  166. .bLength = sizeof usb_out_it_desc,
  167. .bDescriptorType = USB_DT_CS_INTERFACE,
  168. .bDescriptorSubtype = UAC_INPUT_TERMINAL,
  169. /* .bTerminalID = DYNAMIC */
  170. .wTerminalType = cpu_to_le16(UAC_TERMINAL_STREAMING),
  171. .bAssocTerminal = 0,
  172. /* .bCSourceID = DYNAMIC */
  173. .iChannelNames = 0,
  174. .bmControls = cpu_to_le16(CONTROL_RDWR << COPY_CTRL),
  175. };
  176. /* Input Terminal for I/O-In */
  177. static struct uac2_input_terminal_descriptor io_in_it_desc = {
  178. .bLength = sizeof io_in_it_desc,
  179. .bDescriptorType = USB_DT_CS_INTERFACE,
  180. .bDescriptorSubtype = UAC_INPUT_TERMINAL,
  181. /* .bTerminalID = DYNAMIC */
  182. .wTerminalType = cpu_to_le16(UAC_INPUT_TERMINAL_MICROPHONE),
  183. .bAssocTerminal = 0,
  184. /* .bCSourceID = DYNAMIC */
  185. .iChannelNames = 0,
  186. .bmControls = cpu_to_le16(CONTROL_RDWR << COPY_CTRL),
  187. };
  188. /* Ouput Terminal for USB_IN */
  189. static struct uac2_output_terminal_descriptor usb_in_ot_desc = {
  190. .bLength = sizeof usb_in_ot_desc,
  191. .bDescriptorType = USB_DT_CS_INTERFACE,
  192. .bDescriptorSubtype = UAC_OUTPUT_TERMINAL,
  193. /* .bTerminalID = DYNAMIC */
  194. .wTerminalType = cpu_to_le16(UAC_TERMINAL_STREAMING),
  195. .bAssocTerminal = 0,
  196. /* .bSourceID = DYNAMIC */
  197. /* .bCSourceID = DYNAMIC */
  198. .bmControls = cpu_to_le16(CONTROL_RDWR << COPY_CTRL),
  199. };
  200. /* Ouput Terminal for I/O-Out */
  201. static struct uac2_output_terminal_descriptor io_out_ot_desc = {
  202. .bLength = sizeof io_out_ot_desc,
  203. .bDescriptorType = USB_DT_CS_INTERFACE,
  204. .bDescriptorSubtype = UAC_OUTPUT_TERMINAL,
  205. /* .bTerminalID = DYNAMIC */
  206. .wTerminalType = cpu_to_le16(UAC_OUTPUT_TERMINAL_SPEAKER),
  207. .bAssocTerminal = 0,
  208. /* .bSourceID = DYNAMIC */
  209. /* .bCSourceID = DYNAMIC */
  210. .bmControls = cpu_to_le16(CONTROL_RDWR << COPY_CTRL),
  211. };
  212. static struct uac2_feature_unit_descriptor *in_feature_unit_desc;
  213. static struct uac2_feature_unit_descriptor *out_feature_unit_desc;
  214. static struct uac2_ac_header_descriptor ac_hdr_desc = {
  215. .bLength = sizeof ac_hdr_desc,
  216. .bDescriptorType = USB_DT_CS_INTERFACE,
  217. .bDescriptorSubtype = UAC_MS_HEADER,
  218. .bcdADC = cpu_to_le16(0x200),
  219. .bCategory = UAC2_FUNCTION_IO_BOX,
  220. /* .wTotalLength = DYNAMIC */
  221. .bmControls = 0,
  222. };
  223. /* AC IN Interrupt Endpoint */
  224. static struct usb_endpoint_descriptor fs_ep_int_desc = {
  225. .bLength = USB_DT_ENDPOINT_SIZE,
  226. .bDescriptorType = USB_DT_ENDPOINT,
  227. .bEndpointAddress = USB_DIR_IN,
  228. .bmAttributes = USB_ENDPOINT_XFER_INT,
  229. .wMaxPacketSize = cpu_to_le16(6),
  230. .bInterval = 1,
  231. };
  232. static struct usb_endpoint_descriptor hs_ep_int_desc = {
  233. .bLength = USB_DT_ENDPOINT_SIZE,
  234. .bDescriptorType = USB_DT_ENDPOINT,
  235. .bmAttributes = USB_ENDPOINT_XFER_INT,
  236. .wMaxPacketSize = cpu_to_le16(6),
  237. .bInterval = 4,
  238. };
  239. static struct usb_endpoint_descriptor ss_ep_int_desc = {
  240. .bLength = USB_DT_ENDPOINT_SIZE,
  241. .bDescriptorType = USB_DT_ENDPOINT,
  242. .bEndpointAddress = USB_DIR_IN,
  243. .bmAttributes = USB_ENDPOINT_XFER_INT,
  244. .wMaxPacketSize = cpu_to_le16(6),
  245. .bInterval = 4,
  246. };
  247. static struct usb_ss_ep_comp_descriptor ss_ep_int_desc_comp = {
  248. .bLength = sizeof(ss_ep_int_desc_comp),
  249. .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
  250. .wBytesPerInterval = cpu_to_le16(6),
  251. };
  252. /* Audio Streaming OUT Interface - Alt0 */
  253. static struct usb_interface_descriptor std_as_out_if0_desc = {
  254. .bLength = sizeof std_as_out_if0_desc,
  255. .bDescriptorType = USB_DT_INTERFACE,
  256. .bAlternateSetting = 0,
  257. .bNumEndpoints = 0,
  258. .bInterfaceClass = USB_CLASS_AUDIO,
  259. .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
  260. .bInterfaceProtocol = UAC_VERSION_2,
  261. };
  262. /* Audio Streaming OUT Interface - Alt1 */
  263. static struct usb_interface_descriptor std_as_out_if1_desc = {
  264. .bLength = sizeof std_as_out_if1_desc,
  265. .bDescriptorType = USB_DT_INTERFACE,
  266. .bAlternateSetting = 1,
  267. .bNumEndpoints = 1,
  268. .bInterfaceClass = USB_CLASS_AUDIO,
  269. .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
  270. .bInterfaceProtocol = UAC_VERSION_2,
  271. };
  272. /* Audio Stream OUT Intface Desc */
  273. static struct uac2_as_header_descriptor as_out_hdr_desc = {
  274. .bLength = sizeof as_out_hdr_desc,
  275. .bDescriptorType = USB_DT_CS_INTERFACE,
  276. .bDescriptorSubtype = UAC_AS_GENERAL,
  277. /* .bTerminalLink = DYNAMIC */
  278. .bmControls = 0,
  279. .bFormatType = UAC_FORMAT_TYPE_I,
  280. .bmFormats = cpu_to_le32(UAC_FORMAT_TYPE_I_PCM),
  281. .iChannelNames = 0,
  282. };
  283. /* Audio USB_OUT Format */
  284. static struct uac2_format_type_i_descriptor as_out_fmt1_desc = {
  285. .bLength = sizeof as_out_fmt1_desc,
  286. .bDescriptorType = USB_DT_CS_INTERFACE,
  287. .bDescriptorSubtype = UAC_FORMAT_TYPE,
  288. .bFormatType = UAC_FORMAT_TYPE_I,
  289. };
  290. /* STD AS ISO OUT Endpoint */
  291. static struct usb_endpoint_descriptor fs_epout_desc = {
  292. .bLength = USB_DT_ENDPOINT_SIZE,
  293. .bDescriptorType = USB_DT_ENDPOINT,
  294. .bEndpointAddress = USB_DIR_OUT,
  295. /* .bmAttributes = DYNAMIC */
  296. /* .wMaxPacketSize = DYNAMIC */
  297. .bInterval = 1,
  298. };
  299. static struct usb_endpoint_descriptor hs_epout_desc = {
  300. .bLength = USB_DT_ENDPOINT_SIZE,
  301. .bDescriptorType = USB_DT_ENDPOINT,
  302. /* .bmAttributes = DYNAMIC */
  303. /* .wMaxPacketSize = DYNAMIC */
  304. /* .bInterval = DYNAMIC */
  305. };
  306. static struct usb_endpoint_descriptor ss_epout_desc = {
  307. .bLength = USB_DT_ENDPOINT_SIZE,
  308. .bDescriptorType = USB_DT_ENDPOINT,
  309. .bEndpointAddress = USB_DIR_OUT,
  310. /* .bmAttributes = DYNAMIC */
  311. /* .wMaxPacketSize = DYNAMIC */
  312. /* .bInterval = DYNAMIC */
  313. };
  314. static struct usb_ss_ep_comp_descriptor ss_epout_desc_comp = {
  315. .bLength = sizeof(ss_epout_desc_comp),
  316. .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
  317. .bMaxBurst = 0,
  318. .bmAttributes = 0,
  319. /* wBytesPerInterval = DYNAMIC */
  320. };
  321. /* CS AS ISO OUT Endpoint */
  322. static struct uac2_iso_endpoint_descriptor as_iso_out_desc = {
  323. .bLength = sizeof as_iso_out_desc,
  324. .bDescriptorType = USB_DT_CS_ENDPOINT,
  325. .bDescriptorSubtype = UAC_EP_GENERAL,
  326. .bmAttributes = 0,
  327. .bmControls = 0,
  328. .bLockDelayUnits = 0,
  329. .wLockDelay = 0,
  330. };
  331. /* STD AS ISO IN Feedback Endpoint */
  332. static struct usb_endpoint_descriptor fs_epin_fback_desc = {
  333. .bLength = USB_DT_ENDPOINT_SIZE,
  334. .bDescriptorType = USB_DT_ENDPOINT,
  335. .bEndpointAddress = USB_DIR_IN,
  336. .bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_USAGE_FEEDBACK,
  337. .wMaxPacketSize = cpu_to_le16(3),
  338. .bInterval = 1,
  339. };
  340. static struct usb_endpoint_descriptor hs_epin_fback_desc = {
  341. .bLength = USB_DT_ENDPOINT_SIZE,
  342. .bDescriptorType = USB_DT_ENDPOINT,
  343. .bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_USAGE_FEEDBACK,
  344. .wMaxPacketSize = cpu_to_le16(4),
  345. .bInterval = 4,
  346. };
  347. static struct usb_endpoint_descriptor ss_epin_fback_desc = {
  348. .bLength = USB_DT_ENDPOINT_SIZE,
  349. .bDescriptorType = USB_DT_ENDPOINT,
  350. .bEndpointAddress = USB_DIR_IN,
  351. .bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_USAGE_FEEDBACK,
  352. .wMaxPacketSize = cpu_to_le16(4),
  353. .bInterval = 4,
  354. };
  355. static struct usb_ss_ep_comp_descriptor ss_epin_fback_desc_comp = {
  356. .bLength = sizeof(ss_epin_fback_desc_comp),
  357. .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
  358. .bMaxBurst = 0,
  359. .bmAttributes = 0,
  360. .wBytesPerInterval = cpu_to_le16(4),
  361. };
  362. /* Audio Streaming IN Interface - Alt0 */
  363. static struct usb_interface_descriptor std_as_in_if0_desc = {
  364. .bLength = sizeof std_as_in_if0_desc,
  365. .bDescriptorType = USB_DT_INTERFACE,
  366. .bAlternateSetting = 0,
  367. .bNumEndpoints = 0,
  368. .bInterfaceClass = USB_CLASS_AUDIO,
  369. .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
  370. .bInterfaceProtocol = UAC_VERSION_2,
  371. };
  372. /* Audio Streaming IN Interface - Alt1 */
  373. static struct usb_interface_descriptor std_as_in_if1_desc = {
  374. .bLength = sizeof std_as_in_if1_desc,
  375. .bDescriptorType = USB_DT_INTERFACE,
  376. .bAlternateSetting = 1,
  377. .bNumEndpoints = 1,
  378. .bInterfaceClass = USB_CLASS_AUDIO,
  379. .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
  380. .bInterfaceProtocol = UAC_VERSION_2,
  381. };
  382. /* Audio Stream IN Intface Desc */
  383. static struct uac2_as_header_descriptor as_in_hdr_desc = {
  384. .bLength = sizeof as_in_hdr_desc,
  385. .bDescriptorType = USB_DT_CS_INTERFACE,
  386. .bDescriptorSubtype = UAC_AS_GENERAL,
  387. /* .bTerminalLink = DYNAMIC */
  388. .bmControls = 0,
  389. .bFormatType = UAC_FORMAT_TYPE_I,
  390. .bmFormats = cpu_to_le32(UAC_FORMAT_TYPE_I_PCM),
  391. .iChannelNames = 0,
  392. };
  393. /* Audio USB_IN Format */
  394. static struct uac2_format_type_i_descriptor as_in_fmt1_desc = {
  395. .bLength = sizeof as_in_fmt1_desc,
  396. .bDescriptorType = USB_DT_CS_INTERFACE,
  397. .bDescriptorSubtype = UAC_FORMAT_TYPE,
  398. .bFormatType = UAC_FORMAT_TYPE_I,
  399. };
  400. /* STD AS ISO IN Endpoint */
  401. static struct usb_endpoint_descriptor fs_epin_desc = {
  402. .bLength = USB_DT_ENDPOINT_SIZE,
  403. .bDescriptorType = USB_DT_ENDPOINT,
  404. .bEndpointAddress = USB_DIR_IN,
  405. .bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC,
  406. /* .wMaxPacketSize = DYNAMIC */
  407. .bInterval = 1,
  408. };
  409. static struct usb_endpoint_descriptor hs_epin_desc = {
  410. .bLength = USB_DT_ENDPOINT_SIZE,
  411. .bDescriptorType = USB_DT_ENDPOINT,
  412. .bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC,
  413. /* .wMaxPacketSize = DYNAMIC */
  414. /* .bInterval = DYNAMIC */
  415. };
  416. static struct usb_endpoint_descriptor ss_epin_desc = {
  417. .bLength = USB_DT_ENDPOINT_SIZE,
  418. .bDescriptorType = USB_DT_ENDPOINT,
  419. .bEndpointAddress = USB_DIR_IN,
  420. .bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC,
  421. /* .wMaxPacketSize = DYNAMIC */
  422. /* .bInterval = DYNAMIC */
  423. };
  424. static struct usb_ss_ep_comp_descriptor ss_epin_desc_comp = {
  425. .bLength = sizeof(ss_epin_desc_comp),
  426. .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
  427. .bMaxBurst = 0,
  428. .bmAttributes = 0,
  429. /* wBytesPerInterval = DYNAMIC */
  430. };
  431. /* CS AS ISO IN Endpoint */
  432. static struct uac2_iso_endpoint_descriptor as_iso_in_desc = {
  433. .bLength = sizeof as_iso_in_desc,
  434. .bDescriptorType = USB_DT_CS_ENDPOINT,
  435. .bDescriptorSubtype = UAC_EP_GENERAL,
  436. .bmAttributes = 0,
  437. .bmControls = 0,
  438. .bLockDelayUnits = 0,
  439. .wLockDelay = 0,
  440. };
  441. static struct usb_descriptor_header *fs_audio_desc[] = {
  442. (struct usb_descriptor_header *)&iad_desc,
  443. (struct usb_descriptor_header *)&std_ac_if_desc,
  444. (struct usb_descriptor_header *)&ac_hdr_desc,
  445. (struct usb_descriptor_header *)&in_clk_src_desc,
  446. (struct usb_descriptor_header *)&out_clk_src_desc,
  447. (struct usb_descriptor_header *)&usb_out_it_desc,
  448. (struct usb_descriptor_header *)&out_feature_unit_desc,
  449. (struct usb_descriptor_header *)&io_in_it_desc,
  450. (struct usb_descriptor_header *)&usb_in_ot_desc,
  451. (struct usb_descriptor_header *)&in_feature_unit_desc,
  452. (struct usb_descriptor_header *)&io_out_ot_desc,
  453. (struct usb_descriptor_header *)&fs_ep_int_desc,
  454. (struct usb_descriptor_header *)&std_as_out_if0_desc,
  455. (struct usb_descriptor_header *)&std_as_out_if1_desc,
  456. (struct usb_descriptor_header *)&as_out_hdr_desc,
  457. (struct usb_descriptor_header *)&as_out_fmt1_desc,
  458. (struct usb_descriptor_header *)&fs_epout_desc,
  459. (struct usb_descriptor_header *)&as_iso_out_desc,
  460. (struct usb_descriptor_header *)&fs_epin_fback_desc,
  461. (struct usb_descriptor_header *)&std_as_in_if0_desc,
  462. (struct usb_descriptor_header *)&std_as_in_if1_desc,
  463. (struct usb_descriptor_header *)&as_in_hdr_desc,
  464. (struct usb_descriptor_header *)&as_in_fmt1_desc,
  465. (struct usb_descriptor_header *)&fs_epin_desc,
  466. (struct usb_descriptor_header *)&as_iso_in_desc,
  467. NULL,
  468. };
  469. static struct usb_descriptor_header *hs_audio_desc[] = {
  470. (struct usb_descriptor_header *)&iad_desc,
  471. (struct usb_descriptor_header *)&std_ac_if_desc,
  472. (struct usb_descriptor_header *)&ac_hdr_desc,
  473. (struct usb_descriptor_header *)&in_clk_src_desc,
  474. (struct usb_descriptor_header *)&out_clk_src_desc,
  475. (struct usb_descriptor_header *)&usb_out_it_desc,
  476. (struct usb_descriptor_header *)&out_feature_unit_desc,
  477. (struct usb_descriptor_header *)&io_in_it_desc,
  478. (struct usb_descriptor_header *)&usb_in_ot_desc,
  479. (struct usb_descriptor_header *)&in_feature_unit_desc,
  480. (struct usb_descriptor_header *)&io_out_ot_desc,
  481. (struct usb_descriptor_header *)&hs_ep_int_desc,
  482. (struct usb_descriptor_header *)&std_as_out_if0_desc,
  483. (struct usb_descriptor_header *)&std_as_out_if1_desc,
  484. (struct usb_descriptor_header *)&as_out_hdr_desc,
  485. (struct usb_descriptor_header *)&as_out_fmt1_desc,
  486. (struct usb_descriptor_header *)&hs_epout_desc,
  487. (struct usb_descriptor_header *)&as_iso_out_desc,
  488. (struct usb_descriptor_header *)&hs_epin_fback_desc,
  489. (struct usb_descriptor_header *)&std_as_in_if0_desc,
  490. (struct usb_descriptor_header *)&std_as_in_if1_desc,
  491. (struct usb_descriptor_header *)&as_in_hdr_desc,
  492. (struct usb_descriptor_header *)&as_in_fmt1_desc,
  493. (struct usb_descriptor_header *)&hs_epin_desc,
  494. (struct usb_descriptor_header *)&as_iso_in_desc,
  495. NULL,
  496. };
  497. static struct usb_descriptor_header *ss_audio_desc[] = {
  498. (struct usb_descriptor_header *)&iad_desc,
  499. (struct usb_descriptor_header *)&std_ac_if_desc,
  500. (struct usb_descriptor_header *)&ac_hdr_desc,
  501. (struct usb_descriptor_header *)&in_clk_src_desc,
  502. (struct usb_descriptor_header *)&out_clk_src_desc,
  503. (struct usb_descriptor_header *)&usb_out_it_desc,
  504. (struct usb_descriptor_header *)&out_feature_unit_desc,
  505. (struct usb_descriptor_header *)&io_in_it_desc,
  506. (struct usb_descriptor_header *)&usb_in_ot_desc,
  507. (struct usb_descriptor_header *)&in_feature_unit_desc,
  508. (struct usb_descriptor_header *)&io_out_ot_desc,
  509. (struct usb_descriptor_header *)&ss_ep_int_desc,
  510. (struct usb_descriptor_header *)&ss_ep_int_desc_comp,
  511. (struct usb_descriptor_header *)&std_as_out_if0_desc,
  512. (struct usb_descriptor_header *)&std_as_out_if1_desc,
  513. (struct usb_descriptor_header *)&as_out_hdr_desc,
  514. (struct usb_descriptor_header *)&as_out_fmt1_desc,
  515. (struct usb_descriptor_header *)&ss_epout_desc,
  516. (struct usb_descriptor_header *)&ss_epout_desc_comp,
  517. (struct usb_descriptor_header *)&as_iso_out_desc,
  518. (struct usb_descriptor_header *)&ss_epin_fback_desc,
  519. (struct usb_descriptor_header *)&ss_epin_fback_desc_comp,
  520. (struct usb_descriptor_header *)&std_as_in_if0_desc,
  521. (struct usb_descriptor_header *)&std_as_in_if1_desc,
  522. (struct usb_descriptor_header *)&as_in_hdr_desc,
  523. (struct usb_descriptor_header *)&as_in_fmt1_desc,
  524. (struct usb_descriptor_header *)&ss_epin_desc,
  525. (struct usb_descriptor_header *)&ss_epin_desc_comp,
  526. (struct usb_descriptor_header *)&as_iso_in_desc,
  527. NULL,
  528. };
  529. struct cntrl_cur_lay2 {
  530. __le16 wCUR;
  531. };
  532. struct cntrl_range_lay2 {
  533. __le16 wNumSubRanges;
  534. __le16 wMIN;
  535. __le16 wMAX;
  536. __le16 wRES;
  537. } __packed;
  538. struct cntrl_cur_lay3 {
  539. __le32 dCUR;
  540. };
  541. struct cntrl_subrange_lay3 {
  542. __le32 dMIN;
  543. __le32 dMAX;
  544. __le32 dRES;
  545. } __packed;
  546. #define ranges_lay3_size(c) (sizeof(c.wNumSubRanges) \
  547. + le16_to_cpu(c.wNumSubRanges) \
  548. * sizeof(struct cntrl_subrange_lay3))
  549. #define DECLARE_UAC2_CNTRL_RANGES_LAY3(k, n) \
  550. struct cntrl_ranges_lay3_##k { \
  551. __le16 wNumSubRanges; \
  552. struct cntrl_subrange_lay3 r[n]; \
  553. } __packed
  554. DECLARE_UAC2_CNTRL_RANGES_LAY3(srates, UAC_MAX_RATES);
  555. static int get_max_srate(const int *srates)
  556. {
  557. int i, max_srate = 0;
  558. for (i = 0; i < UAC_MAX_RATES; i++) {
  559. if (srates[i] == 0)
  560. break;
  561. if (srates[i] > max_srate)
  562. max_srate = srates[i];
  563. }
  564. return max_srate;
  565. }
  566. static int get_max_bw_for_bint(const struct f_uac2_opts *uac2_opts,
  567. u8 bint, unsigned int factor, bool is_playback)
  568. {
  569. int chmask, srate, ssize;
  570. u16 max_size_bw;
  571. if (is_playback) {
  572. chmask = uac2_opts->p_chmask;
  573. srate = get_max_srate(uac2_opts->p_srates);
  574. ssize = uac2_opts->p_ssize;
  575. } else {
  576. chmask = uac2_opts->c_chmask;
  577. srate = get_max_srate(uac2_opts->c_srates);
  578. ssize = uac2_opts->c_ssize;
  579. }
  580. if (is_playback || (uac2_opts->c_sync == USB_ENDPOINT_SYNC_ASYNC)) {
  581. // playback is always async, capture only when configured
  582. // Win10 requires max packet size + 1 frame
  583. srate = srate * (1000 + uac2_opts->fb_max) / 1000;
  584. // updated srate is always bigger, therefore DIV_ROUND_UP always yields +1
  585. max_size_bw = num_channels(chmask) * ssize *
  586. (DIV_ROUND_UP(srate, factor / (1 << (bint - 1))));
  587. } else {
  588. // adding 1 frame provision for Win10
  589. max_size_bw = num_channels(chmask) * ssize *
  590. (DIV_ROUND_UP(srate, factor / (1 << (bint - 1))) + 1);
  591. }
  592. return max_size_bw;
  593. }
  594. static int set_ep_max_packet_size_bint(struct device *dev, const struct f_uac2_opts *uac2_opts,
  595. struct usb_endpoint_descriptor *ep_desc,
  596. enum usb_device_speed speed, bool is_playback)
  597. {
  598. u16 max_size_bw, max_size_ep;
  599. u8 bint, opts_bint;
  600. char *dir;
  601. switch (speed) {
  602. case USB_SPEED_FULL:
  603. max_size_ep = 1023;
  604. // fixed
  605. bint = ep_desc->bInterval;
  606. max_size_bw = get_max_bw_for_bint(uac2_opts, bint, 1000, is_playback);
  607. break;
  608. case USB_SPEED_HIGH:
  609. case USB_SPEED_SUPER:
  610. max_size_ep = 1024;
  611. if (is_playback)
  612. opts_bint = uac2_opts->p_hs_bint;
  613. else
  614. opts_bint = uac2_opts->c_hs_bint;
  615. if (opts_bint > 0) {
  616. /* fixed bint */
  617. bint = opts_bint;
  618. max_size_bw = get_max_bw_for_bint(uac2_opts, bint, 8000, is_playback);
  619. } else {
  620. /* checking bInterval from 4 to 1 whether the required bandwidth fits */
  621. for (bint = 4; bint > 0; --bint) {
  622. max_size_bw = get_max_bw_for_bint(
  623. uac2_opts, bint, 8000, is_playback);
  624. if (max_size_bw <= max_size_ep)
  625. break;
  626. }
  627. }
  628. break;
  629. default:
  630. return -EINVAL;
  631. }
  632. if (is_playback)
  633. dir = "Playback";
  634. else
  635. dir = "Capture";
  636. if (max_size_bw <= max_size_ep)
  637. dev_dbg(dev,
  638. "%s %s: Would use wMaxPacketSize %d and bInterval %d\n",
  639. speed_names[speed], dir, max_size_bw, bint);
  640. else {
  641. dev_warn(dev,
  642. "%s %s: Req. wMaxPacketSize %d at bInterval %d > max ISOC %d, may drop data!\n",
  643. speed_names[speed], dir, max_size_bw, bint, max_size_ep);
  644. max_size_bw = max_size_ep;
  645. }
  646. ep_desc->wMaxPacketSize = cpu_to_le16(max_size_bw);
  647. ep_desc->bInterval = bint;
  648. return 0;
  649. }
  650. static struct uac2_feature_unit_descriptor *build_fu_desc(int chmask)
  651. {
  652. struct uac2_feature_unit_descriptor *fu_desc;
  653. int channels = num_channels(chmask);
  654. int fu_desc_size = UAC2_DT_FEATURE_UNIT_SIZE(channels);
  655. fu_desc = kzalloc(fu_desc_size, GFP_KERNEL);
  656. if (!fu_desc)
  657. return NULL;
  658. fu_desc->bLength = fu_desc_size;
  659. fu_desc->bDescriptorType = USB_DT_CS_INTERFACE;
  660. fu_desc->bDescriptorSubtype = UAC_FEATURE_UNIT;
  661. /* bUnitID, bSourceID and bmaControls will be defined later */
  662. return fu_desc;
  663. }
  664. /* Use macro to overcome line length limitation */
  665. #define USBDHDR(p) (struct usb_descriptor_header *)(p)
  666. static void setup_headers(struct f_uac2_opts *opts,
  667. struct usb_descriptor_header **headers,
  668. enum usb_device_speed speed)
  669. {
  670. struct usb_ss_ep_comp_descriptor *epout_desc_comp = NULL;
  671. struct usb_ss_ep_comp_descriptor *epin_desc_comp = NULL;
  672. struct usb_ss_ep_comp_descriptor *epin_fback_desc_comp = NULL;
  673. struct usb_ss_ep_comp_descriptor *ep_int_desc_comp = NULL;
  674. struct usb_endpoint_descriptor *epout_desc;
  675. struct usb_endpoint_descriptor *epin_desc;
  676. struct usb_endpoint_descriptor *epin_fback_desc;
  677. struct usb_endpoint_descriptor *ep_int_desc;
  678. int i;
  679. switch (speed) {
  680. case USB_SPEED_FULL:
  681. epout_desc = &fs_epout_desc;
  682. epin_desc = &fs_epin_desc;
  683. epin_fback_desc = &fs_epin_fback_desc;
  684. ep_int_desc = &fs_ep_int_desc;
  685. break;
  686. case USB_SPEED_HIGH:
  687. epout_desc = &hs_epout_desc;
  688. epin_desc = &hs_epin_desc;
  689. epin_fback_desc = &hs_epin_fback_desc;
  690. ep_int_desc = &hs_ep_int_desc;
  691. break;
  692. default:
  693. epout_desc = &ss_epout_desc;
  694. epin_desc = &ss_epin_desc;
  695. epout_desc_comp = &ss_epout_desc_comp;
  696. epin_desc_comp = &ss_epin_desc_comp;
  697. epin_fback_desc = &ss_epin_fback_desc;
  698. epin_fback_desc_comp = &ss_epin_fback_desc_comp;
  699. ep_int_desc = &ss_ep_int_desc;
  700. ep_int_desc_comp = &ss_ep_int_desc_comp;
  701. }
  702. i = 0;
  703. headers[i++] = USBDHDR(&iad_desc);
  704. headers[i++] = USBDHDR(&std_ac_if_desc);
  705. headers[i++] = USBDHDR(&ac_hdr_desc);
  706. if (EPIN_EN(opts))
  707. headers[i++] = USBDHDR(&in_clk_src_desc);
  708. if (EPOUT_EN(opts)) {
  709. headers[i++] = USBDHDR(&out_clk_src_desc);
  710. headers[i++] = USBDHDR(&usb_out_it_desc);
  711. if (FUOUT_EN(opts))
  712. headers[i++] = USBDHDR(out_feature_unit_desc);
  713. }
  714. if (EPIN_EN(opts)) {
  715. headers[i++] = USBDHDR(&io_in_it_desc);
  716. if (FUIN_EN(opts))
  717. headers[i++] = USBDHDR(in_feature_unit_desc);
  718. headers[i++] = USBDHDR(&usb_in_ot_desc);
  719. }
  720. if (EPOUT_EN(opts))
  721. headers[i++] = USBDHDR(&io_out_ot_desc);
  722. if (FUOUT_EN(opts) || FUIN_EN(opts)) {
  723. headers[i++] = USBDHDR(ep_int_desc);
  724. if (ep_int_desc_comp)
  725. headers[i++] = USBDHDR(ep_int_desc_comp);
  726. }
  727. if (EPOUT_EN(opts)) {
  728. headers[i++] = USBDHDR(&std_as_out_if0_desc);
  729. headers[i++] = USBDHDR(&std_as_out_if1_desc);
  730. headers[i++] = USBDHDR(&as_out_hdr_desc);
  731. headers[i++] = USBDHDR(&as_out_fmt1_desc);
  732. headers[i++] = USBDHDR(epout_desc);
  733. if (epout_desc_comp)
  734. headers[i++] = USBDHDR(epout_desc_comp);
  735. headers[i++] = USBDHDR(&as_iso_out_desc);
  736. if (EPOUT_FBACK_IN_EN(opts)) {
  737. headers[i++] = USBDHDR(epin_fback_desc);
  738. if (epin_fback_desc_comp)
  739. headers[i++] = USBDHDR(epin_fback_desc_comp);
  740. }
  741. }
  742. if (EPIN_EN(opts)) {
  743. headers[i++] = USBDHDR(&std_as_in_if0_desc);
  744. headers[i++] = USBDHDR(&std_as_in_if1_desc);
  745. headers[i++] = USBDHDR(&as_in_hdr_desc);
  746. headers[i++] = USBDHDR(&as_in_fmt1_desc);
  747. headers[i++] = USBDHDR(epin_desc);
  748. if (epin_desc_comp)
  749. headers[i++] = USBDHDR(epin_desc_comp);
  750. headers[i++] = USBDHDR(&as_iso_in_desc);
  751. }
  752. headers[i] = NULL;
  753. }
  754. static void setup_descriptor(struct f_uac2_opts *opts)
  755. {
  756. /* patch descriptors */
  757. int i = 1; /* ID's start with 1 */
  758. if (EPOUT_EN(opts))
  759. usb_out_it_desc.bTerminalID = i++;
  760. if (EPIN_EN(opts))
  761. io_in_it_desc.bTerminalID = i++;
  762. if (EPOUT_EN(opts))
  763. io_out_ot_desc.bTerminalID = i++;
  764. if (EPIN_EN(opts))
  765. usb_in_ot_desc.bTerminalID = i++;
  766. if (FUOUT_EN(opts))
  767. out_feature_unit_desc->bUnitID = i++;
  768. if (FUIN_EN(opts))
  769. in_feature_unit_desc->bUnitID = i++;
  770. if (EPOUT_EN(opts))
  771. out_clk_src_desc.bClockID = i++;
  772. if (EPIN_EN(opts))
  773. in_clk_src_desc.bClockID = i++;
  774. usb_out_it_desc.bCSourceID = out_clk_src_desc.bClockID;
  775. if (FUIN_EN(opts)) {
  776. usb_in_ot_desc.bSourceID = in_feature_unit_desc->bUnitID;
  777. in_feature_unit_desc->bSourceID = io_in_it_desc.bTerminalID;
  778. } else {
  779. usb_in_ot_desc.bSourceID = io_in_it_desc.bTerminalID;
  780. }
  781. usb_in_ot_desc.bCSourceID = in_clk_src_desc.bClockID;
  782. io_in_it_desc.bCSourceID = in_clk_src_desc.bClockID;
  783. io_out_ot_desc.bCSourceID = out_clk_src_desc.bClockID;
  784. if (FUOUT_EN(opts)) {
  785. io_out_ot_desc.bSourceID = out_feature_unit_desc->bUnitID;
  786. out_feature_unit_desc->bSourceID = usb_out_it_desc.bTerminalID;
  787. } else {
  788. io_out_ot_desc.bSourceID = usb_out_it_desc.bTerminalID;
  789. }
  790. as_out_hdr_desc.bTerminalLink = usb_out_it_desc.bTerminalID;
  791. as_in_hdr_desc.bTerminalLink = usb_in_ot_desc.bTerminalID;
  792. iad_desc.bInterfaceCount = 1;
  793. ac_hdr_desc.wTotalLength = cpu_to_le16(sizeof(ac_hdr_desc));
  794. if (EPIN_EN(opts)) {
  795. u16 len = le16_to_cpu(ac_hdr_desc.wTotalLength);
  796. len += sizeof(in_clk_src_desc);
  797. len += sizeof(usb_in_ot_desc);
  798. if (FUIN_EN(opts))
  799. len += in_feature_unit_desc->bLength;
  800. len += sizeof(io_in_it_desc);
  801. ac_hdr_desc.wTotalLength = cpu_to_le16(len);
  802. iad_desc.bInterfaceCount++;
  803. }
  804. if (EPOUT_EN(opts)) {
  805. u16 len = le16_to_cpu(ac_hdr_desc.wTotalLength);
  806. len += sizeof(out_clk_src_desc);
  807. len += sizeof(usb_out_it_desc);
  808. if (FUOUT_EN(opts))
  809. len += out_feature_unit_desc->bLength;
  810. len += sizeof(io_out_ot_desc);
  811. ac_hdr_desc.wTotalLength = cpu_to_le16(len);
  812. iad_desc.bInterfaceCount++;
  813. }
  814. setup_headers(opts, fs_audio_desc, USB_SPEED_FULL);
  815. setup_headers(opts, hs_audio_desc, USB_SPEED_HIGH);
  816. setup_headers(opts, ss_audio_desc, USB_SPEED_SUPER);
  817. }
  818. static int afunc_validate_opts(struct g_audio *agdev, struct device *dev)
  819. {
  820. struct f_uac2_opts *opts = g_audio_to_uac2_opts(agdev);
  821. const char *msg = NULL;
  822. if (!opts->p_chmask && !opts->c_chmask)
  823. msg = "no playback and capture channels";
  824. else if (opts->p_chmask & ~UAC2_CHANNEL_MASK)
  825. msg = "unsupported playback channels mask";
  826. else if (opts->c_chmask & ~UAC2_CHANNEL_MASK)
  827. msg = "unsupported capture channels mask";
  828. else if ((opts->p_ssize < 1) || (opts->p_ssize > 4))
  829. msg = "incorrect playback sample size";
  830. else if ((opts->c_ssize < 1) || (opts->c_ssize > 4))
  831. msg = "incorrect capture sample size";
  832. else if (!opts->p_srates[0])
  833. msg = "incorrect playback sampling rate";
  834. else if (!opts->c_srates[0])
  835. msg = "incorrect capture sampling rate";
  836. else if (opts->p_volume_max <= opts->p_volume_min)
  837. msg = "incorrect playback volume max/min";
  838. else if (opts->c_volume_max <= opts->c_volume_min)
  839. msg = "incorrect capture volume max/min";
  840. else if (opts->p_volume_res <= 0)
  841. msg = "negative/zero playback volume resolution";
  842. else if (opts->c_volume_res <= 0)
  843. msg = "negative/zero capture volume resolution";
  844. else if ((opts->p_volume_max - opts->p_volume_min) % opts->p_volume_res)
  845. msg = "incorrect playback volume resolution";
  846. else if ((opts->c_volume_max - opts->c_volume_min) % opts->c_volume_res)
  847. msg = "incorrect capture volume resolution";
  848. else if ((opts->p_hs_bint < 0) || (opts->p_hs_bint > 4))
  849. msg = "incorrect playback HS/SS bInterval (1-4: fixed, 0: auto)";
  850. else if ((opts->c_hs_bint < 0) || (opts->c_hs_bint > 4))
  851. msg = "incorrect capture HS/SS bInterval (1-4: fixed, 0: auto)";
  852. if (msg) {
  853. dev_err(dev, "Error: %s\n", msg);
  854. return -EINVAL;
  855. }
  856. return 0;
  857. }
  858. static int
  859. afunc_bind(struct usb_configuration *cfg, struct usb_function *fn)
  860. {
  861. struct f_uac2 *uac2 = func_to_uac2(fn);
  862. struct g_audio *agdev = func_to_g_audio(fn);
  863. struct usb_composite_dev *cdev = cfg->cdev;
  864. struct usb_gadget *gadget = cdev->gadget;
  865. struct device *dev = &gadget->dev;
  866. struct f_uac2_opts *uac2_opts = g_audio_to_uac2_opts(agdev);
  867. struct usb_string *us;
  868. int ret;
  869. ret = afunc_validate_opts(agdev, dev);
  870. if (ret)
  871. return ret;
  872. strings_fn[STR_ASSOC].s = uac2_opts->function_name;
  873. us = usb_gstrings_attach(cdev, fn_strings, ARRAY_SIZE(strings_fn));
  874. if (IS_ERR(us))
  875. return PTR_ERR(us);
  876. if (FUOUT_EN(uac2_opts)) {
  877. out_feature_unit_desc = build_fu_desc(uac2_opts->c_chmask);
  878. if (!out_feature_unit_desc)
  879. return -ENOMEM;
  880. }
  881. if (FUIN_EN(uac2_opts)) {
  882. in_feature_unit_desc = build_fu_desc(uac2_opts->p_chmask);
  883. if (!in_feature_unit_desc) {
  884. ret = -ENOMEM;
  885. goto err_free_fu;
  886. }
  887. }
  888. iad_desc.iFunction = us[STR_ASSOC].id;
  889. std_ac_if_desc.iInterface = us[STR_IF_CTRL].id;
  890. in_clk_src_desc.iClockSource = us[STR_CLKSRC_IN].id;
  891. out_clk_src_desc.iClockSource = us[STR_CLKSRC_OUT].id;
  892. usb_out_it_desc.iTerminal = us[STR_USB_IT].id;
  893. io_in_it_desc.iTerminal = us[STR_IO_IT].id;
  894. usb_in_ot_desc.iTerminal = us[STR_USB_OT].id;
  895. io_out_ot_desc.iTerminal = us[STR_IO_OT].id;
  896. std_as_out_if0_desc.iInterface = us[STR_AS_OUT_ALT0].id;
  897. std_as_out_if1_desc.iInterface = us[STR_AS_OUT_ALT1].id;
  898. std_as_in_if0_desc.iInterface = us[STR_AS_IN_ALT0].id;
  899. std_as_in_if1_desc.iInterface = us[STR_AS_IN_ALT1].id;
  900. if (FUOUT_EN(uac2_opts)) {
  901. u8 *i_feature = (u8 *)out_feature_unit_desc +
  902. out_feature_unit_desc->bLength - 1;
  903. *i_feature = us[STR_FU_OUT].id;
  904. }
  905. if (FUIN_EN(uac2_opts)) {
  906. u8 *i_feature = (u8 *)in_feature_unit_desc +
  907. in_feature_unit_desc->bLength - 1;
  908. *i_feature = us[STR_FU_IN].id;
  909. }
  910. /* Initialize the configurable parameters */
  911. usb_out_it_desc.bNrChannels = num_channels(uac2_opts->c_chmask);
  912. usb_out_it_desc.bmChannelConfig = cpu_to_le32(uac2_opts->c_chmask);
  913. io_in_it_desc.bNrChannels = num_channels(uac2_opts->p_chmask);
  914. io_in_it_desc.bmChannelConfig = cpu_to_le32(uac2_opts->p_chmask);
  915. as_out_hdr_desc.bNrChannels = num_channels(uac2_opts->c_chmask);
  916. as_out_hdr_desc.bmChannelConfig = cpu_to_le32(uac2_opts->c_chmask);
  917. as_in_hdr_desc.bNrChannels = num_channels(uac2_opts->p_chmask);
  918. as_in_hdr_desc.bmChannelConfig = cpu_to_le32(uac2_opts->p_chmask);
  919. as_out_fmt1_desc.bSubslotSize = uac2_opts->c_ssize;
  920. as_out_fmt1_desc.bBitResolution = uac2_opts->c_ssize * 8;
  921. as_in_fmt1_desc.bSubslotSize = uac2_opts->p_ssize;
  922. as_in_fmt1_desc.bBitResolution = uac2_opts->p_ssize * 8;
  923. if (FUOUT_EN(uac2_opts)) {
  924. __le32 *bma = (__le32 *)&out_feature_unit_desc->bmaControls[0];
  925. u32 control = 0;
  926. if (uac2_opts->c_mute_present)
  927. control |= CONTROL_RDWR << FU_MUTE_CTRL;
  928. if (uac2_opts->c_volume_present)
  929. control |= CONTROL_RDWR << FU_VOL_CTRL;
  930. *bma = cpu_to_le32(control);
  931. }
  932. if (FUIN_EN(uac2_opts)) {
  933. __le32 *bma = (__le32 *)&in_feature_unit_desc->bmaControls[0];
  934. u32 control = 0;
  935. if (uac2_opts->p_mute_present)
  936. control |= CONTROL_RDWR << FU_MUTE_CTRL;
  937. if (uac2_opts->p_volume_present)
  938. control |= CONTROL_RDWR << FU_VOL_CTRL;
  939. *bma = cpu_to_le32(control);
  940. }
  941. ret = usb_interface_id(cfg, fn);
  942. if (ret < 0) {
  943. dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
  944. goto err_free_fu;
  945. }
  946. iad_desc.bFirstInterface = ret;
  947. std_ac_if_desc.bInterfaceNumber = ret;
  948. uac2->ac_intf = ret;
  949. uac2->ac_alt = 0;
  950. if (EPOUT_EN(uac2_opts)) {
  951. ret = usb_interface_id(cfg, fn);
  952. if (ret < 0) {
  953. dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
  954. goto err_free_fu;
  955. }
  956. std_as_out_if0_desc.bInterfaceNumber = ret;
  957. std_as_out_if1_desc.bInterfaceNumber = ret;
  958. std_as_out_if1_desc.bNumEndpoints = 1;
  959. uac2->as_out_intf = ret;
  960. uac2->as_out_alt = 0;
  961. if (EPOUT_FBACK_IN_EN(uac2_opts)) {
  962. fs_epout_desc.bmAttributes =
  963. USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC;
  964. hs_epout_desc.bmAttributes =
  965. USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC;
  966. ss_epout_desc.bmAttributes =
  967. USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC;
  968. std_as_out_if1_desc.bNumEndpoints++;
  969. } else {
  970. fs_epout_desc.bmAttributes =
  971. USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ADAPTIVE;
  972. hs_epout_desc.bmAttributes =
  973. USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ADAPTIVE;
  974. ss_epout_desc.bmAttributes =
  975. USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ADAPTIVE;
  976. }
  977. }
  978. if (EPIN_EN(uac2_opts)) {
  979. ret = usb_interface_id(cfg, fn);
  980. if (ret < 0) {
  981. dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
  982. goto err_free_fu;
  983. }
  984. std_as_in_if0_desc.bInterfaceNumber = ret;
  985. std_as_in_if1_desc.bInterfaceNumber = ret;
  986. uac2->as_in_intf = ret;
  987. uac2->as_in_alt = 0;
  988. }
  989. if (FUOUT_EN(uac2_opts) || FUIN_EN(uac2_opts)) {
  990. uac2->int_ep = usb_ep_autoconfig(gadget, &fs_ep_int_desc);
  991. if (!uac2->int_ep) {
  992. dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
  993. ret = -ENODEV;
  994. goto err_free_fu;
  995. }
  996. std_ac_if_desc.bNumEndpoints = 1;
  997. }
  998. hs_epin_desc.bInterval = uac2_opts->p_hs_bint;
  999. ss_epin_desc.bInterval = uac2_opts->p_hs_bint;
  1000. hs_epout_desc.bInterval = uac2_opts->c_hs_bint;
  1001. ss_epout_desc.bInterval = uac2_opts->c_hs_bint;
  1002. /* Calculate wMaxPacketSize according to audio bandwidth */
  1003. ret = set_ep_max_packet_size_bint(dev, uac2_opts, &fs_epin_desc,
  1004. USB_SPEED_FULL, true);
  1005. if (ret < 0) {
  1006. dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
  1007. return ret;
  1008. }
  1009. ret = set_ep_max_packet_size_bint(dev, uac2_opts, &fs_epout_desc,
  1010. USB_SPEED_FULL, false);
  1011. if (ret < 0) {
  1012. dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
  1013. return ret;
  1014. }
  1015. ret = set_ep_max_packet_size_bint(dev, uac2_opts, &hs_epin_desc,
  1016. USB_SPEED_HIGH, true);
  1017. if (ret < 0) {
  1018. dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
  1019. return ret;
  1020. }
  1021. ret = set_ep_max_packet_size_bint(dev, uac2_opts, &hs_epout_desc,
  1022. USB_SPEED_HIGH, false);
  1023. if (ret < 0) {
  1024. dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
  1025. return ret;
  1026. }
  1027. ret = set_ep_max_packet_size_bint(dev, uac2_opts, &ss_epin_desc,
  1028. USB_SPEED_SUPER, true);
  1029. if (ret < 0) {
  1030. dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
  1031. return ret;
  1032. }
  1033. ret = set_ep_max_packet_size_bint(dev, uac2_opts, &ss_epout_desc,
  1034. USB_SPEED_SUPER, false);
  1035. if (ret < 0) {
  1036. dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
  1037. return ret;
  1038. }
  1039. if (EPOUT_EN(uac2_opts)) {
  1040. agdev->out_ep = usb_ep_autoconfig(gadget, &fs_epout_desc);
  1041. if (!agdev->out_ep) {
  1042. dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
  1043. ret = -ENODEV;
  1044. goto err_free_fu;
  1045. }
  1046. if (EPOUT_FBACK_IN_EN(uac2_opts)) {
  1047. agdev->in_ep_fback = usb_ep_autoconfig(gadget,
  1048. &fs_epin_fback_desc);
  1049. if (!agdev->in_ep_fback) {
  1050. dev_err(dev, "%s:%d Error!\n",
  1051. __func__, __LINE__);
  1052. ret = -ENODEV;
  1053. goto err_free_fu;
  1054. }
  1055. }
  1056. }
  1057. if (EPIN_EN(uac2_opts)) {
  1058. agdev->in_ep = usb_ep_autoconfig(gadget, &fs_epin_desc);
  1059. if (!agdev->in_ep) {
  1060. dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
  1061. ret = -ENODEV;
  1062. goto err_free_fu;
  1063. }
  1064. }
  1065. agdev->in_ep_maxpsize = max_t(u16,
  1066. le16_to_cpu(fs_epin_desc.wMaxPacketSize),
  1067. le16_to_cpu(hs_epin_desc.wMaxPacketSize));
  1068. agdev->out_ep_maxpsize = max_t(u16,
  1069. le16_to_cpu(fs_epout_desc.wMaxPacketSize),
  1070. le16_to_cpu(hs_epout_desc.wMaxPacketSize));
  1071. agdev->in_ep_maxpsize = max_t(u16, agdev->in_ep_maxpsize,
  1072. le16_to_cpu(ss_epin_desc.wMaxPacketSize));
  1073. agdev->out_ep_maxpsize = max_t(u16, agdev->out_ep_maxpsize,
  1074. le16_to_cpu(ss_epout_desc.wMaxPacketSize));
  1075. ss_epin_desc_comp.wBytesPerInterval = ss_epin_desc.wMaxPacketSize;
  1076. ss_epout_desc_comp.wBytesPerInterval = ss_epout_desc.wMaxPacketSize;
  1077. // HS and SS endpoint addresses are copied from autoconfigured FS descriptors
  1078. hs_ep_int_desc.bEndpointAddress = fs_ep_int_desc.bEndpointAddress;
  1079. hs_epout_desc.bEndpointAddress = fs_epout_desc.bEndpointAddress;
  1080. hs_epin_fback_desc.bEndpointAddress = fs_epin_fback_desc.bEndpointAddress;
  1081. hs_epin_desc.bEndpointAddress = fs_epin_desc.bEndpointAddress;
  1082. ss_epout_desc.bEndpointAddress = fs_epout_desc.bEndpointAddress;
  1083. ss_epin_fback_desc.bEndpointAddress = fs_epin_fback_desc.bEndpointAddress;
  1084. ss_epin_desc.bEndpointAddress = fs_epin_desc.bEndpointAddress;
  1085. ss_ep_int_desc.bEndpointAddress = fs_ep_int_desc.bEndpointAddress;
  1086. setup_descriptor(uac2_opts);
  1087. ret = usb_assign_descriptors(fn, fs_audio_desc, hs_audio_desc, ss_audio_desc,
  1088. ss_audio_desc);
  1089. if (ret)
  1090. goto err_free_fu;
  1091. agdev->gadget = gadget;
  1092. agdev->params.p_chmask = uac2_opts->p_chmask;
  1093. memcpy(agdev->params.p_srates, uac2_opts->p_srates,
  1094. sizeof(agdev->params.p_srates));
  1095. agdev->params.p_ssize = uac2_opts->p_ssize;
  1096. if (FUIN_EN(uac2_opts)) {
  1097. agdev->params.p_fu.id = USB_IN_FU_ID;
  1098. agdev->params.p_fu.mute_present = uac2_opts->p_mute_present;
  1099. agdev->params.p_fu.volume_present = uac2_opts->p_volume_present;
  1100. agdev->params.p_fu.volume_min = uac2_opts->p_volume_min;
  1101. agdev->params.p_fu.volume_max = uac2_opts->p_volume_max;
  1102. agdev->params.p_fu.volume_res = uac2_opts->p_volume_res;
  1103. }
  1104. agdev->params.c_chmask = uac2_opts->c_chmask;
  1105. memcpy(agdev->params.c_srates, uac2_opts->c_srates,
  1106. sizeof(agdev->params.c_srates));
  1107. agdev->params.c_ssize = uac2_opts->c_ssize;
  1108. if (FUOUT_EN(uac2_opts)) {
  1109. agdev->params.c_fu.id = USB_OUT_FU_ID;
  1110. agdev->params.c_fu.mute_present = uac2_opts->c_mute_present;
  1111. agdev->params.c_fu.volume_present = uac2_opts->c_volume_present;
  1112. agdev->params.c_fu.volume_min = uac2_opts->c_volume_min;
  1113. agdev->params.c_fu.volume_max = uac2_opts->c_volume_max;
  1114. agdev->params.c_fu.volume_res = uac2_opts->c_volume_res;
  1115. }
  1116. agdev->params.req_number = uac2_opts->req_number;
  1117. agdev->params.fb_max = uac2_opts->fb_max;
  1118. if (FUOUT_EN(uac2_opts) || FUIN_EN(uac2_opts))
  1119. agdev->notify = afunc_notify;
  1120. ret = g_audio_setup(agdev, "UAC2 PCM", "UAC2_Gadget");
  1121. if (ret)
  1122. goto err_free_descs;
  1123. return 0;
  1124. err_free_descs:
  1125. usb_free_all_descriptors(fn);
  1126. agdev->gadget = NULL;
  1127. err_free_fu:
  1128. kfree(out_feature_unit_desc);
  1129. out_feature_unit_desc = NULL;
  1130. kfree(in_feature_unit_desc);
  1131. in_feature_unit_desc = NULL;
  1132. return ret;
  1133. }
  1134. static void
  1135. afunc_notify_complete(struct usb_ep *_ep, struct usb_request *req)
  1136. {
  1137. struct g_audio *agdev = req->context;
  1138. struct f_uac2 *uac2 = func_to_uac2(&agdev->func);
  1139. atomic_dec(&uac2->int_count);
  1140. kfree(req->buf);
  1141. usb_ep_free_request(_ep, req);
  1142. }
  1143. static int
  1144. afunc_notify(struct g_audio *agdev, int unit_id, int cs)
  1145. {
  1146. struct f_uac2 *uac2 = func_to_uac2(&agdev->func);
  1147. struct usb_request *req;
  1148. struct uac2_interrupt_data_msg *msg;
  1149. u16 w_index, w_value;
  1150. int ret;
  1151. if (!uac2->int_ep->enabled)
  1152. return 0;
  1153. if (atomic_inc_return(&uac2->int_count) > UAC2_DEF_INT_REQ_NUM) {
  1154. atomic_dec(&uac2->int_count);
  1155. return 0;
  1156. }
  1157. req = usb_ep_alloc_request(uac2->int_ep, GFP_ATOMIC);
  1158. if (req == NULL) {
  1159. ret = -ENOMEM;
  1160. goto err_dec_int_count;
  1161. }
  1162. msg = kzalloc(sizeof(*msg), GFP_ATOMIC);
  1163. if (msg == NULL) {
  1164. ret = -ENOMEM;
  1165. goto err_free_request;
  1166. }
  1167. w_index = unit_id << 8 | uac2->ac_intf;
  1168. w_value = cs << 8;
  1169. msg->bInfo = 0; /* Non-vendor, interface interrupt */
  1170. msg->bAttribute = UAC2_CS_CUR;
  1171. msg->wIndex = cpu_to_le16(w_index);
  1172. msg->wValue = cpu_to_le16(w_value);
  1173. req->length = sizeof(*msg);
  1174. req->buf = msg;
  1175. req->context = agdev;
  1176. req->complete = afunc_notify_complete;
  1177. ret = usb_ep_queue(uac2->int_ep, req, GFP_ATOMIC);
  1178. if (ret)
  1179. goto err_free_msg;
  1180. return 0;
  1181. err_free_msg:
  1182. kfree(msg);
  1183. err_free_request:
  1184. usb_ep_free_request(uac2->int_ep, req);
  1185. err_dec_int_count:
  1186. atomic_dec(&uac2->int_count);
  1187. return ret;
  1188. }
  1189. static int
  1190. afunc_set_alt(struct usb_function *fn, unsigned intf, unsigned alt)
  1191. {
  1192. struct usb_composite_dev *cdev = fn->config->cdev;
  1193. struct f_uac2 *uac2 = func_to_uac2(fn);
  1194. struct g_audio *agdev = func_to_g_audio(fn);
  1195. struct usb_gadget *gadget = cdev->gadget;
  1196. struct device *dev = &gadget->dev;
  1197. int ret = 0;
  1198. /* No i/f has more than 2 alt settings */
  1199. if (alt > 1) {
  1200. dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
  1201. return -EINVAL;
  1202. }
  1203. if (intf == uac2->ac_intf) {
  1204. /* Control I/f has only 1 AltSetting - 0 */
  1205. if (alt) {
  1206. dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
  1207. return -EINVAL;
  1208. }
  1209. /* restart interrupt endpoint */
  1210. if (uac2->int_ep) {
  1211. usb_ep_disable(uac2->int_ep);
  1212. config_ep_by_speed(gadget, &agdev->func, uac2->int_ep);
  1213. usb_ep_enable(uac2->int_ep);
  1214. }
  1215. return 0;
  1216. }
  1217. if (intf == uac2->as_out_intf) {
  1218. uac2->as_out_alt = alt;
  1219. if (alt)
  1220. ret = u_audio_start_capture(&uac2->g_audio);
  1221. else
  1222. u_audio_stop_capture(&uac2->g_audio);
  1223. } else if (intf == uac2->as_in_intf) {
  1224. uac2->as_in_alt = alt;
  1225. if (alt)
  1226. ret = u_audio_start_playback(&uac2->g_audio);
  1227. else
  1228. u_audio_stop_playback(&uac2->g_audio);
  1229. } else {
  1230. dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
  1231. return -EINVAL;
  1232. }
  1233. return ret;
  1234. }
  1235. static int
  1236. afunc_get_alt(struct usb_function *fn, unsigned intf)
  1237. {
  1238. struct f_uac2 *uac2 = func_to_uac2(fn);
  1239. struct g_audio *agdev = func_to_g_audio(fn);
  1240. if (intf == uac2->ac_intf)
  1241. return uac2->ac_alt;
  1242. else if (intf == uac2->as_out_intf)
  1243. return uac2->as_out_alt;
  1244. else if (intf == uac2->as_in_intf)
  1245. return uac2->as_in_alt;
  1246. else
  1247. dev_err(&agdev->gadget->dev,
  1248. "%s:%d Invalid Interface %d!\n",
  1249. __func__, __LINE__, intf);
  1250. return -EINVAL;
  1251. }
  1252. static void
  1253. afunc_disable(struct usb_function *fn)
  1254. {
  1255. struct f_uac2 *uac2 = func_to_uac2(fn);
  1256. uac2->as_in_alt = 0;
  1257. uac2->as_out_alt = 0;
  1258. u_audio_stop_capture(&uac2->g_audio);
  1259. u_audio_stop_playback(&uac2->g_audio);
  1260. if (uac2->int_ep)
  1261. usb_ep_disable(uac2->int_ep);
  1262. }
  1263. static void
  1264. afunc_suspend(struct usb_function *fn)
  1265. {
  1266. struct f_uac2 *uac2 = func_to_uac2(fn);
  1267. u_audio_suspend(&uac2->g_audio);
  1268. }
  1269. static int
  1270. in_rq_cur(struct usb_function *fn, const struct usb_ctrlrequest *cr)
  1271. {
  1272. struct usb_request *req = fn->config->cdev->req;
  1273. struct g_audio *agdev = func_to_g_audio(fn);
  1274. struct f_uac2_opts *opts = g_audio_to_uac2_opts(agdev);
  1275. u16 w_length = le16_to_cpu(cr->wLength);
  1276. u16 w_index = le16_to_cpu(cr->wIndex);
  1277. u16 w_value = le16_to_cpu(cr->wValue);
  1278. u8 entity_id = (w_index >> 8) & 0xff;
  1279. u8 control_selector = w_value >> 8;
  1280. int value = -EOPNOTSUPP;
  1281. u32 p_srate, c_srate;
  1282. u_audio_get_playback_srate(agdev, &p_srate);
  1283. u_audio_get_capture_srate(agdev, &c_srate);
  1284. if ((entity_id == USB_IN_CLK_ID) || (entity_id == USB_OUT_CLK_ID)) {
  1285. if (control_selector == UAC2_CS_CONTROL_SAM_FREQ) {
  1286. struct cntrl_cur_lay3 c;
  1287. memset(&c, 0, sizeof(struct cntrl_cur_lay3));
  1288. if (entity_id == USB_IN_CLK_ID)
  1289. c.dCUR = cpu_to_le32(p_srate);
  1290. else if (entity_id == USB_OUT_CLK_ID)
  1291. c.dCUR = cpu_to_le32(c_srate);
  1292. value = min_t(unsigned int, w_length, sizeof(c));
  1293. memcpy(req->buf, &c, value);
  1294. } else if (control_selector == UAC2_CS_CONTROL_CLOCK_VALID) {
  1295. *(u8 *)req->buf = 1;
  1296. value = min_t(unsigned int, w_length, 1);
  1297. } else {
  1298. dev_err(&agdev->gadget->dev,
  1299. "%s:%d control_selector=%d TODO!\n",
  1300. __func__, __LINE__, control_selector);
  1301. }
  1302. } else if ((FUIN_EN(opts) && (entity_id == USB_IN_FU_ID)) ||
  1303. (FUOUT_EN(opts) && (entity_id == USB_OUT_FU_ID))) {
  1304. unsigned int is_playback = 0;
  1305. if (FUIN_EN(opts) && (entity_id == USB_IN_FU_ID))
  1306. is_playback = 1;
  1307. if (control_selector == UAC_FU_MUTE) {
  1308. unsigned int mute;
  1309. u_audio_get_mute(agdev, is_playback, &mute);
  1310. *(u8 *)req->buf = mute;
  1311. value = min_t(unsigned int, w_length, 1);
  1312. } else if (control_selector == UAC_FU_VOLUME) {
  1313. struct cntrl_cur_lay2 c;
  1314. s16 volume;
  1315. memset(&c, 0, sizeof(struct cntrl_cur_lay2));
  1316. u_audio_get_volume(agdev, is_playback, &volume);
  1317. c.wCUR = cpu_to_le16(volume);
  1318. value = min_t(unsigned int, w_length, sizeof(c));
  1319. memcpy(req->buf, &c, value);
  1320. } else {
  1321. dev_err(&agdev->gadget->dev,
  1322. "%s:%d control_selector=%d TODO!\n",
  1323. __func__, __LINE__, control_selector);
  1324. }
  1325. } else {
  1326. dev_err(&agdev->gadget->dev,
  1327. "%s:%d entity_id=%d control_selector=%d TODO!\n",
  1328. __func__, __LINE__, entity_id, control_selector);
  1329. }
  1330. return value;
  1331. }
  1332. static int
  1333. in_rq_range(struct usb_function *fn, const struct usb_ctrlrequest *cr)
  1334. {
  1335. struct usb_request *req = fn->config->cdev->req;
  1336. struct g_audio *agdev = func_to_g_audio(fn);
  1337. struct f_uac2_opts *opts = g_audio_to_uac2_opts(agdev);
  1338. u16 w_length = le16_to_cpu(cr->wLength);
  1339. u16 w_index = le16_to_cpu(cr->wIndex);
  1340. u16 w_value = le16_to_cpu(cr->wValue);
  1341. u8 entity_id = (w_index >> 8) & 0xff;
  1342. u8 control_selector = w_value >> 8;
  1343. int value = -EOPNOTSUPP;
  1344. if ((entity_id == USB_IN_CLK_ID) || (entity_id == USB_OUT_CLK_ID)) {
  1345. if (control_selector == UAC2_CS_CONTROL_SAM_FREQ) {
  1346. struct cntrl_ranges_lay3_srates rs;
  1347. int i;
  1348. int wNumSubRanges = 0;
  1349. int srate;
  1350. int *srates;
  1351. if (entity_id == USB_IN_CLK_ID)
  1352. srates = opts->p_srates;
  1353. else if (entity_id == USB_OUT_CLK_ID)
  1354. srates = opts->c_srates;
  1355. else
  1356. return -EOPNOTSUPP;
  1357. for (i = 0; i < UAC_MAX_RATES; i++) {
  1358. srate = srates[i];
  1359. if (srate == 0)
  1360. break;
  1361. rs.r[wNumSubRanges].dMIN = cpu_to_le32(srate);
  1362. rs.r[wNumSubRanges].dMAX = cpu_to_le32(srate);
  1363. rs.r[wNumSubRanges].dRES = 0;
  1364. wNumSubRanges++;
  1365. dev_dbg(&agdev->gadget->dev,
  1366. "%s(): clk %d: rate ID %d: %d\n",
  1367. __func__, entity_id, wNumSubRanges, srate);
  1368. }
  1369. rs.wNumSubRanges = cpu_to_le16(wNumSubRanges);
  1370. value = min_t(unsigned int, w_length, ranges_lay3_size(rs));
  1371. dev_dbg(&agdev->gadget->dev, "%s(): sending %d rates, size %d\n",
  1372. __func__, rs.wNumSubRanges, value);
  1373. memcpy(req->buf, &rs, value);
  1374. } else {
  1375. dev_err(&agdev->gadget->dev,
  1376. "%s:%d control_selector=%d TODO!\n",
  1377. __func__, __LINE__, control_selector);
  1378. }
  1379. } else if ((FUIN_EN(opts) && (entity_id == USB_IN_FU_ID)) ||
  1380. (FUOUT_EN(opts) && (entity_id == USB_OUT_FU_ID))) {
  1381. unsigned int is_playback = 0;
  1382. if (FUIN_EN(opts) && (entity_id == USB_IN_FU_ID))
  1383. is_playback = 1;
  1384. if (control_selector == UAC_FU_VOLUME) {
  1385. struct cntrl_range_lay2 r;
  1386. s16 max_db, min_db, res_db;
  1387. if (is_playback) {
  1388. max_db = opts->p_volume_max;
  1389. min_db = opts->p_volume_min;
  1390. res_db = opts->p_volume_res;
  1391. } else {
  1392. max_db = opts->c_volume_max;
  1393. min_db = opts->c_volume_min;
  1394. res_db = opts->c_volume_res;
  1395. }
  1396. r.wMAX = cpu_to_le16(max_db);
  1397. r.wMIN = cpu_to_le16(min_db);
  1398. r.wRES = cpu_to_le16(res_db);
  1399. r.wNumSubRanges = cpu_to_le16(1);
  1400. value = min_t(unsigned int, w_length, sizeof(r));
  1401. memcpy(req->buf, &r, value);
  1402. } else {
  1403. dev_err(&agdev->gadget->dev,
  1404. "%s:%d control_selector=%d TODO!\n",
  1405. __func__, __LINE__, control_selector);
  1406. }
  1407. } else {
  1408. dev_err(&agdev->gadget->dev,
  1409. "%s:%d entity_id=%d control_selector=%d TODO!\n",
  1410. __func__, __LINE__, entity_id, control_selector);
  1411. }
  1412. return value;
  1413. }
  1414. static int
  1415. ac_rq_in(struct usb_function *fn, const struct usb_ctrlrequest *cr)
  1416. {
  1417. if (cr->bRequest == UAC2_CS_CUR)
  1418. return in_rq_cur(fn, cr);
  1419. else if (cr->bRequest == UAC2_CS_RANGE)
  1420. return in_rq_range(fn, cr);
  1421. else
  1422. return -EOPNOTSUPP;
  1423. }
  1424. static void uac2_cs_control_sam_freq(struct usb_ep *ep, struct usb_request *req)
  1425. {
  1426. struct usb_function *fn = ep->driver_data;
  1427. struct g_audio *agdev = func_to_g_audio(fn);
  1428. struct f_uac2 *uac2 = func_to_uac2(fn);
  1429. u32 val;
  1430. if (req->actual != 4)
  1431. return;
  1432. val = le32_to_cpu(*((__le32 *)req->buf));
  1433. dev_dbg(&agdev->gadget->dev, "%s val: %d.\n", __func__, val);
  1434. if (uac2->clock_id == USB_IN_CLK_ID) {
  1435. u_audio_set_playback_srate(agdev, val);
  1436. } else if (uac2->clock_id == USB_OUT_CLK_ID) {
  1437. u_audio_set_capture_srate(agdev, val);
  1438. }
  1439. }
  1440. static void
  1441. out_rq_cur_complete(struct usb_ep *ep, struct usb_request *req)
  1442. {
  1443. struct g_audio *agdev = req->context;
  1444. struct usb_composite_dev *cdev = agdev->func.config->cdev;
  1445. struct f_uac2_opts *opts = g_audio_to_uac2_opts(agdev);
  1446. struct f_uac2 *uac2 = func_to_uac2(&agdev->func);
  1447. struct usb_ctrlrequest *cr = &uac2->setup_cr;
  1448. u16 w_index = le16_to_cpu(cr->wIndex);
  1449. u16 w_value = le16_to_cpu(cr->wValue);
  1450. u8 entity_id = (w_index >> 8) & 0xff;
  1451. u8 control_selector = w_value >> 8;
  1452. if (req->status != 0) {
  1453. dev_dbg(&cdev->gadget->dev, "completion err %d\n", req->status);
  1454. return;
  1455. }
  1456. if ((FUIN_EN(opts) && (entity_id == USB_IN_FU_ID)) ||
  1457. (FUOUT_EN(opts) && (entity_id == USB_OUT_FU_ID))) {
  1458. unsigned int is_playback = 0;
  1459. if (FUIN_EN(opts) && (entity_id == USB_IN_FU_ID))
  1460. is_playback = 1;
  1461. if (control_selector == UAC_FU_MUTE) {
  1462. u8 mute = *(u8 *)req->buf;
  1463. u_audio_set_mute(agdev, is_playback, mute);
  1464. return;
  1465. } else if (control_selector == UAC_FU_VOLUME) {
  1466. struct cntrl_cur_lay2 *c = req->buf;
  1467. s16 volume;
  1468. volume = le16_to_cpu(c->wCUR);
  1469. u_audio_set_volume(agdev, is_playback, volume);
  1470. return;
  1471. } else {
  1472. dev_err(&agdev->gadget->dev,
  1473. "%s:%d control_selector=%d TODO!\n",
  1474. __func__, __LINE__, control_selector);
  1475. usb_ep_set_halt(ep);
  1476. }
  1477. }
  1478. }
  1479. static int
  1480. out_rq_cur(struct usb_function *fn, const struct usb_ctrlrequest *cr)
  1481. {
  1482. struct usb_composite_dev *cdev = fn->config->cdev;
  1483. struct usb_request *req = fn->config->cdev->req;
  1484. struct g_audio *agdev = func_to_g_audio(fn);
  1485. struct f_uac2_opts *opts = g_audio_to_uac2_opts(agdev);
  1486. struct f_uac2 *uac2 = func_to_uac2(fn);
  1487. u16 w_length = le16_to_cpu(cr->wLength);
  1488. u16 w_index = le16_to_cpu(cr->wIndex);
  1489. u16 w_value = le16_to_cpu(cr->wValue);
  1490. u8 entity_id = (w_index >> 8) & 0xff;
  1491. u8 control_selector = w_value >> 8;
  1492. u8 clock_id = w_index >> 8;
  1493. if ((entity_id == USB_IN_CLK_ID) || (entity_id == USB_OUT_CLK_ID)) {
  1494. if (control_selector == UAC2_CS_CONTROL_SAM_FREQ) {
  1495. dev_dbg(&agdev->gadget->dev,
  1496. "control_selector UAC2_CS_CONTROL_SAM_FREQ, clock: %d\n", clock_id);
  1497. cdev->gadget->ep0->driver_data = fn;
  1498. uac2->clock_id = clock_id;
  1499. req->complete = uac2_cs_control_sam_freq;
  1500. return w_length;
  1501. }
  1502. } else if ((FUIN_EN(opts) && (entity_id == USB_IN_FU_ID)) ||
  1503. (FUOUT_EN(opts) && (entity_id == USB_OUT_FU_ID))) {
  1504. memcpy(&uac2->setup_cr, cr, sizeof(*cr));
  1505. req->context = agdev;
  1506. req->complete = out_rq_cur_complete;
  1507. return w_length;
  1508. } else {
  1509. dev_err(&agdev->gadget->dev,
  1510. "%s:%d entity_id=%d control_selector=%d TODO!\n",
  1511. __func__, __LINE__, entity_id, control_selector);
  1512. }
  1513. return -EOPNOTSUPP;
  1514. }
  1515. static int
  1516. setup_rq_inf(struct usb_function *fn, const struct usb_ctrlrequest *cr)
  1517. {
  1518. struct f_uac2 *uac2 = func_to_uac2(fn);
  1519. struct g_audio *agdev = func_to_g_audio(fn);
  1520. u16 w_index = le16_to_cpu(cr->wIndex);
  1521. u8 intf = w_index & 0xff;
  1522. if (intf != uac2->ac_intf) {
  1523. dev_err(&agdev->gadget->dev,
  1524. "%s:%d Error!\n", __func__, __LINE__);
  1525. return -EOPNOTSUPP;
  1526. }
  1527. if (cr->bRequestType & USB_DIR_IN)
  1528. return ac_rq_in(fn, cr);
  1529. else if (cr->bRequest == UAC2_CS_CUR)
  1530. return out_rq_cur(fn, cr);
  1531. return -EOPNOTSUPP;
  1532. }
  1533. static int
  1534. afunc_setup(struct usb_function *fn, const struct usb_ctrlrequest *cr)
  1535. {
  1536. struct usb_composite_dev *cdev = fn->config->cdev;
  1537. struct g_audio *agdev = func_to_g_audio(fn);
  1538. struct usb_request *req = cdev->req;
  1539. u16 w_length = le16_to_cpu(cr->wLength);
  1540. int value = -EOPNOTSUPP;
  1541. /* Only Class specific requests are supposed to reach here */
  1542. if ((cr->bRequestType & USB_TYPE_MASK) != USB_TYPE_CLASS)
  1543. return -EOPNOTSUPP;
  1544. if ((cr->bRequestType & USB_RECIP_MASK) == USB_RECIP_INTERFACE)
  1545. value = setup_rq_inf(fn, cr);
  1546. else
  1547. dev_err(&agdev->gadget->dev, "%s:%d Error!\n",
  1548. __func__, __LINE__);
  1549. if (value >= 0) {
  1550. req->length = value;
  1551. req->zero = value < w_length;
  1552. value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
  1553. if (value < 0) {
  1554. dev_err(&agdev->gadget->dev,
  1555. "%s:%d Error!\n", __func__, __LINE__);
  1556. req->status = 0;
  1557. }
  1558. }
  1559. return value;
  1560. }
  1561. static inline struct f_uac2_opts *to_f_uac2_opts(struct config_item *item)
  1562. {
  1563. return container_of(to_config_group(item), struct f_uac2_opts,
  1564. func_inst.group);
  1565. }
  1566. static void f_uac2_attr_release(struct config_item *item)
  1567. {
  1568. struct f_uac2_opts *opts = to_f_uac2_opts(item);
  1569. usb_put_function_instance(&opts->func_inst);
  1570. }
  1571. static struct configfs_item_operations f_uac2_item_ops = {
  1572. .release = f_uac2_attr_release,
  1573. };
  1574. #define uac2_kstrtou8 kstrtou8
  1575. #define uac2_kstrtou32 kstrtou32
  1576. #define uac2_kstrtos16 kstrtos16
  1577. #define uac2_kstrtobool(s, base, res) kstrtobool((s), (res))
  1578. static const char *u8_fmt = "%u\n";
  1579. static const char *u32_fmt = "%u\n";
  1580. static const char *s16_fmt = "%hd\n";
  1581. static const char *bool_fmt = "%u\n";
  1582. #define UAC2_ATTRIBUTE(type, name) \
  1583. static ssize_t f_uac2_opts_##name##_show(struct config_item *item, \
  1584. char *page) \
  1585. { \
  1586. struct f_uac2_opts *opts = to_f_uac2_opts(item); \
  1587. int result; \
  1588. \
  1589. mutex_lock(&opts->lock); \
  1590. result = sprintf(page, type##_fmt, opts->name); \
  1591. mutex_unlock(&opts->lock); \
  1592. \
  1593. return result; \
  1594. } \
  1595. \
  1596. static ssize_t f_uac2_opts_##name##_store(struct config_item *item, \
  1597. const char *page, size_t len) \
  1598. { \
  1599. struct f_uac2_opts *opts = to_f_uac2_opts(item); \
  1600. int ret; \
  1601. type num; \
  1602. \
  1603. mutex_lock(&opts->lock); \
  1604. if (opts->refcnt) { \
  1605. ret = -EBUSY; \
  1606. goto end; \
  1607. } \
  1608. \
  1609. ret = uac2_kstrto##type(page, 0, &num); \
  1610. if (ret) \
  1611. goto end; \
  1612. \
  1613. opts->name = num; \
  1614. ret = len; \
  1615. \
  1616. end: \
  1617. mutex_unlock(&opts->lock); \
  1618. return ret; \
  1619. } \
  1620. \
  1621. CONFIGFS_ATTR(f_uac2_opts_, name)
  1622. #define UAC2_ATTRIBUTE_SYNC(name) \
  1623. static ssize_t f_uac2_opts_##name##_show(struct config_item *item, \
  1624. char *page) \
  1625. { \
  1626. struct f_uac2_opts *opts = to_f_uac2_opts(item); \
  1627. int result; \
  1628. char *str; \
  1629. \
  1630. mutex_lock(&opts->lock); \
  1631. switch (opts->name) { \
  1632. case USB_ENDPOINT_SYNC_ASYNC: \
  1633. str = "async"; \
  1634. break; \
  1635. case USB_ENDPOINT_SYNC_ADAPTIVE: \
  1636. str = "adaptive"; \
  1637. break; \
  1638. default: \
  1639. str = "unknown"; \
  1640. break; \
  1641. } \
  1642. result = sprintf(page, "%s\n", str); \
  1643. mutex_unlock(&opts->lock); \
  1644. \
  1645. return result; \
  1646. } \
  1647. \
  1648. static ssize_t f_uac2_opts_##name##_store(struct config_item *item, \
  1649. const char *page, size_t len) \
  1650. { \
  1651. struct f_uac2_opts *opts = to_f_uac2_opts(item); \
  1652. int ret = 0; \
  1653. \
  1654. mutex_lock(&opts->lock); \
  1655. if (opts->refcnt) { \
  1656. ret = -EBUSY; \
  1657. goto end; \
  1658. } \
  1659. \
  1660. if (!strncmp(page, "async", 5)) \
  1661. opts->name = USB_ENDPOINT_SYNC_ASYNC; \
  1662. else if (!strncmp(page, "adaptive", 8)) \
  1663. opts->name = USB_ENDPOINT_SYNC_ADAPTIVE; \
  1664. else { \
  1665. ret = -EINVAL; \
  1666. goto end; \
  1667. } \
  1668. \
  1669. ret = len; \
  1670. \
  1671. end: \
  1672. mutex_unlock(&opts->lock); \
  1673. return ret; \
  1674. } \
  1675. \
  1676. CONFIGFS_ATTR(f_uac2_opts_, name)
  1677. #define UAC2_RATE_ATTRIBUTE(name) \
  1678. static ssize_t f_uac2_opts_##name##_show(struct config_item *item, \
  1679. char *page) \
  1680. { \
  1681. struct f_uac2_opts *opts = to_f_uac2_opts(item); \
  1682. int result = 0; \
  1683. int i; \
  1684. \
  1685. mutex_lock(&opts->lock); \
  1686. page[0] = '\0'; \
  1687. for (i = 0; i < UAC_MAX_RATES; i++) { \
  1688. if (opts->name##s[i] == 0) \
  1689. break; \
  1690. result += sprintf(page + strlen(page), "%u,", \
  1691. opts->name##s[i]); \
  1692. } \
  1693. if (strlen(page) > 0) \
  1694. page[strlen(page) - 1] = '\n'; \
  1695. mutex_unlock(&opts->lock); \
  1696. \
  1697. return result; \
  1698. } \
  1699. \
  1700. static ssize_t f_uac2_opts_##name##_store(struct config_item *item, \
  1701. const char *page, size_t len) \
  1702. { \
  1703. struct f_uac2_opts *opts = to_f_uac2_opts(item); \
  1704. char *split_page = NULL; \
  1705. int ret = -EINVAL; \
  1706. char *token; \
  1707. u32 num; \
  1708. int i; \
  1709. \
  1710. mutex_lock(&opts->lock); \
  1711. if (opts->refcnt) { \
  1712. ret = -EBUSY; \
  1713. goto end; \
  1714. } \
  1715. \
  1716. i = 0; \
  1717. memset(opts->name##s, 0x00, sizeof(opts->name##s)); \
  1718. split_page = kstrdup(page, GFP_KERNEL); \
  1719. while ((token = strsep(&split_page, ",")) != NULL) { \
  1720. ret = kstrtou32(token, 0, &num); \
  1721. if (ret) \
  1722. goto end; \
  1723. \
  1724. opts->name##s[i++] = num; \
  1725. ret = len; \
  1726. }; \
  1727. \
  1728. end: \
  1729. kfree(split_page); \
  1730. mutex_unlock(&opts->lock); \
  1731. return ret; \
  1732. } \
  1733. \
  1734. CONFIGFS_ATTR(f_uac2_opts_, name)
  1735. #define UAC2_ATTRIBUTE_STRING(name) \
  1736. static ssize_t f_uac2_opts_##name##_show(struct config_item *item, \
  1737. char *page) \
  1738. { \
  1739. struct f_uac2_opts *opts = to_f_uac2_opts(item); \
  1740. int result; \
  1741. \
  1742. mutex_lock(&opts->lock); \
  1743. result = snprintf(page, sizeof(opts->name), "%s", opts->name); \
  1744. mutex_unlock(&opts->lock); \
  1745. \
  1746. return result; \
  1747. } \
  1748. \
  1749. static ssize_t f_uac2_opts_##name##_store(struct config_item *item, \
  1750. const char *page, size_t len) \
  1751. { \
  1752. struct f_uac2_opts *opts = to_f_uac2_opts(item); \
  1753. int ret = 0; \
  1754. \
  1755. mutex_lock(&opts->lock); \
  1756. if (opts->refcnt) { \
  1757. ret = -EBUSY; \
  1758. goto end; \
  1759. } \
  1760. \
  1761. ret = snprintf(opts->name, min(sizeof(opts->name), len), \
  1762. "%s", page); \
  1763. \
  1764. end: \
  1765. mutex_unlock(&opts->lock); \
  1766. return ret; \
  1767. } \
  1768. \
  1769. CONFIGFS_ATTR(f_uac2_opts_, name)
  1770. UAC2_ATTRIBUTE(u32, p_chmask);
  1771. UAC2_RATE_ATTRIBUTE(p_srate);
  1772. UAC2_ATTRIBUTE(u32, p_ssize);
  1773. UAC2_ATTRIBUTE(u8, p_hs_bint);
  1774. UAC2_ATTRIBUTE(u32, c_chmask);
  1775. UAC2_RATE_ATTRIBUTE(c_srate);
  1776. UAC2_ATTRIBUTE_SYNC(c_sync);
  1777. UAC2_ATTRIBUTE(u32, c_ssize);
  1778. UAC2_ATTRIBUTE(u8, c_hs_bint);
  1779. UAC2_ATTRIBUTE(u32, req_number);
  1780. UAC2_ATTRIBUTE(bool, p_mute_present);
  1781. UAC2_ATTRIBUTE(bool, p_volume_present);
  1782. UAC2_ATTRIBUTE(s16, p_volume_min);
  1783. UAC2_ATTRIBUTE(s16, p_volume_max);
  1784. UAC2_ATTRIBUTE(s16, p_volume_res);
  1785. UAC2_ATTRIBUTE(bool, c_mute_present);
  1786. UAC2_ATTRIBUTE(bool, c_volume_present);
  1787. UAC2_ATTRIBUTE(s16, c_volume_min);
  1788. UAC2_ATTRIBUTE(s16, c_volume_max);
  1789. UAC2_ATTRIBUTE(s16, c_volume_res);
  1790. UAC2_ATTRIBUTE(u32, fb_max);
  1791. UAC2_ATTRIBUTE_STRING(function_name);
  1792. static struct configfs_attribute *f_uac2_attrs[] = {
  1793. &f_uac2_opts_attr_p_chmask,
  1794. &f_uac2_opts_attr_p_srate,
  1795. &f_uac2_opts_attr_p_ssize,
  1796. &f_uac2_opts_attr_p_hs_bint,
  1797. &f_uac2_opts_attr_c_chmask,
  1798. &f_uac2_opts_attr_c_srate,
  1799. &f_uac2_opts_attr_c_ssize,
  1800. &f_uac2_opts_attr_c_hs_bint,
  1801. &f_uac2_opts_attr_c_sync,
  1802. &f_uac2_opts_attr_req_number,
  1803. &f_uac2_opts_attr_fb_max,
  1804. &f_uac2_opts_attr_p_mute_present,
  1805. &f_uac2_opts_attr_p_volume_present,
  1806. &f_uac2_opts_attr_p_volume_min,
  1807. &f_uac2_opts_attr_p_volume_max,
  1808. &f_uac2_opts_attr_p_volume_res,
  1809. &f_uac2_opts_attr_c_mute_present,
  1810. &f_uac2_opts_attr_c_volume_present,
  1811. &f_uac2_opts_attr_c_volume_min,
  1812. &f_uac2_opts_attr_c_volume_max,
  1813. &f_uac2_opts_attr_c_volume_res,
  1814. &f_uac2_opts_attr_function_name,
  1815. NULL,
  1816. };
  1817. static const struct config_item_type f_uac2_func_type = {
  1818. .ct_item_ops = &f_uac2_item_ops,
  1819. .ct_attrs = f_uac2_attrs,
  1820. .ct_owner = THIS_MODULE,
  1821. };
  1822. static void afunc_free_inst(struct usb_function_instance *f)
  1823. {
  1824. struct f_uac2_opts *opts;
  1825. opts = container_of(f, struct f_uac2_opts, func_inst);
  1826. kfree(opts);
  1827. }
  1828. static struct usb_function_instance *afunc_alloc_inst(void)
  1829. {
  1830. struct f_uac2_opts *opts;
  1831. opts = kzalloc(sizeof(*opts), GFP_KERNEL);
  1832. if (!opts)
  1833. return ERR_PTR(-ENOMEM);
  1834. mutex_init(&opts->lock);
  1835. opts->func_inst.free_func_inst = afunc_free_inst;
  1836. config_group_init_type_name(&opts->func_inst.group, "",
  1837. &f_uac2_func_type);
  1838. opts->p_chmask = UAC2_DEF_PCHMASK;
  1839. opts->p_srates[0] = UAC2_DEF_PSRATE;
  1840. opts->p_ssize = UAC2_DEF_PSSIZE;
  1841. opts->p_hs_bint = UAC2_DEF_PHSBINT;
  1842. opts->c_chmask = UAC2_DEF_CCHMASK;
  1843. opts->c_srates[0] = UAC2_DEF_CSRATE;
  1844. opts->c_ssize = UAC2_DEF_CSSIZE;
  1845. opts->c_hs_bint = UAC2_DEF_CHSBINT;
  1846. opts->c_sync = UAC2_DEF_CSYNC;
  1847. opts->p_mute_present = UAC2_DEF_MUTE_PRESENT;
  1848. opts->p_volume_present = UAC2_DEF_VOLUME_PRESENT;
  1849. opts->p_volume_min = UAC2_DEF_MIN_DB;
  1850. opts->p_volume_max = UAC2_DEF_MAX_DB;
  1851. opts->p_volume_res = UAC2_DEF_RES_DB;
  1852. opts->c_mute_present = UAC2_DEF_MUTE_PRESENT;
  1853. opts->c_volume_present = UAC2_DEF_VOLUME_PRESENT;
  1854. opts->c_volume_min = UAC2_DEF_MIN_DB;
  1855. opts->c_volume_max = UAC2_DEF_MAX_DB;
  1856. opts->c_volume_res = UAC2_DEF_RES_DB;
  1857. opts->req_number = UAC2_DEF_REQ_NUM;
  1858. opts->fb_max = FBACK_FAST_MAX;
  1859. snprintf(opts->function_name, sizeof(opts->function_name), "Source/Sink");
  1860. return &opts->func_inst;
  1861. }
  1862. static void afunc_free(struct usb_function *f)
  1863. {
  1864. struct g_audio *agdev;
  1865. struct f_uac2_opts *opts;
  1866. agdev = func_to_g_audio(f);
  1867. opts = container_of(f->fi, struct f_uac2_opts, func_inst);
  1868. kfree(agdev);
  1869. mutex_lock(&opts->lock);
  1870. --opts->refcnt;
  1871. mutex_unlock(&opts->lock);
  1872. }
  1873. static void afunc_unbind(struct usb_configuration *c, struct usb_function *f)
  1874. {
  1875. struct g_audio *agdev = func_to_g_audio(f);
  1876. g_audio_cleanup(agdev);
  1877. usb_free_all_descriptors(f);
  1878. agdev->gadget = NULL;
  1879. kfree(out_feature_unit_desc);
  1880. out_feature_unit_desc = NULL;
  1881. kfree(in_feature_unit_desc);
  1882. in_feature_unit_desc = NULL;
  1883. }
  1884. static struct usb_function *afunc_alloc(struct usb_function_instance *fi)
  1885. {
  1886. struct f_uac2 *uac2;
  1887. struct f_uac2_opts *opts;
  1888. uac2 = kzalloc(sizeof(*uac2), GFP_KERNEL);
  1889. if (uac2 == NULL)
  1890. return ERR_PTR(-ENOMEM);
  1891. opts = container_of(fi, struct f_uac2_opts, func_inst);
  1892. mutex_lock(&opts->lock);
  1893. ++opts->refcnt;
  1894. mutex_unlock(&opts->lock);
  1895. uac2->g_audio.func.name = "uac2_func";
  1896. uac2->g_audio.func.bind = afunc_bind;
  1897. uac2->g_audio.func.unbind = afunc_unbind;
  1898. uac2->g_audio.func.set_alt = afunc_set_alt;
  1899. uac2->g_audio.func.get_alt = afunc_get_alt;
  1900. uac2->g_audio.func.disable = afunc_disable;
  1901. uac2->g_audio.func.suspend = afunc_suspend;
  1902. uac2->g_audio.func.setup = afunc_setup;
  1903. uac2->g_audio.func.free_func = afunc_free;
  1904. return &uac2->g_audio.func;
  1905. }
  1906. DECLARE_USB_FUNCTION_INIT(uac2, afunc_alloc_inst, afunc_alloc);
  1907. MODULE_LICENSE("GPL");
  1908. MODULE_AUTHOR("Yadwinder Singh");
  1909. MODULE_AUTHOR("Jaswinder Singh");
  1910. MODULE_AUTHOR("Ruslan Bilovol");