Linux Audio

Check our new training course

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