cachepc-linux

Fork of AMDESE/linux with modifications for CachePC side-channel attack
git clone https://git.sinitax.com/sinitax/cachepc-linux
Log | Files | Refs | README | LICENSE | sfeed.txt

f_uac2.c (63392B)


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