f_uac2.c 65 KB

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