Linux Audio

Check our new training course

Loading...
v6.2
   1// SPDX-License-Identifier: GPL-2.0+
   2/*
   3 * f_uac1.c -- USB Audio Class 1.0 Function (using u_audio API)
   4 *
   5 * Copyright (C) 2016 Ruslan Bilovol <ruslan.bilovol@gmail.com>
   6 * Copyright (C) 2021 Julian Scheel <julian@jusst.de>
   7 *
   8 * This driver doesn't expect any real Audio codec to be present
   9 * on the device - the audio streams are simply sinked to and
  10 * sourced from a virtual ALSA sound card created.
  11 *
  12 * This file is based on f_uac1.c which is
  13 *   Copyright (C) 2008 Bryan Wu <cooloney@kernel.org>
  14 *   Copyright (C) 2008 Analog Devices, Inc
  15 */
  16
  17#include <linux/usb/audio.h>
  18#include <linux/module.h>
  19
  20#include "u_audio.h"
  21#include "u_uac1.h"
  22
  23/* UAC1 spec: 3.7.2.3 Audio Channel Cluster Format */
  24#define UAC1_CHANNEL_MASK 0x0FFF
  25
  26#define USB_OUT_FU_ID	(out_feature_unit_desc->bUnitID)
  27#define USB_IN_FU_ID	(in_feature_unit_desc->bUnitID)
  28
  29#define EPIN_EN(_opts) ((_opts)->p_chmask != 0)
  30#define EPOUT_EN(_opts) ((_opts)->c_chmask != 0)
  31#define FUIN_EN(_opts) ((_opts)->p_mute_present \
  32			|| (_opts)->p_volume_present)
  33#define FUOUT_EN(_opts) ((_opts)->c_mute_present \
  34			|| (_opts)->c_volume_present)
  35
  36struct f_uac1 {
  37	struct g_audio g_audio;
  38	u8 ac_intf, as_in_intf, as_out_intf;
  39	u8 ac_alt, as_in_alt, as_out_alt;	/* needed for get_alt() */
  40
  41	struct usb_ctrlrequest setup_cr;	/* will be used in data stage */
  42
  43	/* Interrupt IN endpoint of AC interface */
  44	struct usb_ep	*int_ep;
  45	atomic_t	int_count;
  46	int ctl_id;		/* EP id */
  47	int c_srate;	/* current capture srate */
  48	int p_srate;	/* current playback prate */
  49};
  50
  51static inline struct f_uac1 *func_to_uac1(struct usb_function *f)
  52{
  53	return container_of(f, struct f_uac1, g_audio.func);
  54}
  55
  56static inline struct f_uac1_opts *g_audio_to_uac1_opts(struct g_audio *audio)
  57{
  58	return container_of(audio->func.fi, struct f_uac1_opts, func_inst);
  59}
  60
  61/*
  62 * DESCRIPTORS ... most are static, but strings and full
  63 * configuration descriptors are built on demand.
  64 */
  65
  66/*
  67 * We have three interfaces - one AudioControl and two AudioStreaming
  68 *
  69 * The driver implements a simple UAC_1 topology.
  70 * USB-OUT -> IT_1 -> OT_2 -> ALSA_Capture
  71 * ALSA_Playback -> IT_3 -> OT_4 -> USB-IN
  72 */
 
 
 
 
 
  73
  74/* B.3.1  Standard AC Interface Descriptor */
  75static struct usb_interface_descriptor ac_interface_desc = {
  76	.bLength =		USB_DT_INTERFACE_SIZE,
  77	.bDescriptorType =	USB_DT_INTERFACE,
  78	/* .bNumEndpoints =	DYNAMIC */
  79	.bInterfaceClass =	USB_CLASS_AUDIO,
  80	.bInterfaceSubClass =	USB_SUBCLASS_AUDIOCONTROL,
  81};
  82
 
 
 
 
 
 
 
 
 
 
  83/* B.3.2  Class-Specific AC Interface Descriptor */
  84static struct uac1_ac_header_descriptor *ac_header_desc;
 
 
 
 
 
 
 
 
 
 
 
 
  85
 
  86static struct uac_input_terminal_descriptor usb_out_it_desc = {
  87	.bLength =		UAC_DT_INPUT_TERMINAL_SIZE,
  88	.bDescriptorType =	USB_DT_CS_INTERFACE,
  89	.bDescriptorSubtype =	UAC_INPUT_TERMINAL,
  90	/* .bTerminalID =	DYNAMIC */
  91	.wTerminalType =	cpu_to_le16(UAC_TERMINAL_STREAMING),
  92	.bAssocTerminal =	0,
  93	.wChannelConfig =	cpu_to_le16(0x3),
  94};
  95
 
  96static struct uac1_output_terminal_descriptor io_out_ot_desc = {
  97	.bLength		= UAC_DT_OUTPUT_TERMINAL_SIZE,
  98	.bDescriptorType	= USB_DT_CS_INTERFACE,
  99	.bDescriptorSubtype	= UAC_OUTPUT_TERMINAL,
 100	/* .bTerminalID =	DYNAMIC */
 101	.wTerminalType		= cpu_to_le16(UAC_OUTPUT_TERMINAL_SPEAKER),
 102	.bAssocTerminal		= 0,
 103	/* .bSourceID =		DYNAMIC */
 104};
 105
 
 106static struct uac_input_terminal_descriptor io_in_it_desc = {
 107	.bLength		= UAC_DT_INPUT_TERMINAL_SIZE,
 108	.bDescriptorType	= USB_DT_CS_INTERFACE,
 109	.bDescriptorSubtype	= UAC_INPUT_TERMINAL,
 110	/* .bTerminalID		= DYNAMIC */
 111	.wTerminalType		= cpu_to_le16(UAC_INPUT_TERMINAL_MICROPHONE),
 112	.bAssocTerminal		= 0,
 113	.wChannelConfig		= cpu_to_le16(0x3),
 114};
 115
 
 116static struct uac1_output_terminal_descriptor usb_in_ot_desc = {
 117	.bLength =		UAC_DT_OUTPUT_TERMINAL_SIZE,
 118	.bDescriptorType =	USB_DT_CS_INTERFACE,
 119	.bDescriptorSubtype =	UAC_OUTPUT_TERMINAL,
 120	/* .bTerminalID =	DYNAMIC */
 121	.wTerminalType =	cpu_to_le16(UAC_TERMINAL_STREAMING),
 122	.bAssocTerminal =	0,
 123	/* .bSourceID =		DYNAMIC */
 124};
 125
 126static struct uac_feature_unit_descriptor *in_feature_unit_desc;
 127static struct uac_feature_unit_descriptor *out_feature_unit_desc;
 128
 129/* AC IN Interrupt Endpoint */
 130static struct usb_endpoint_descriptor ac_int_ep_desc = {
 131	.bLength = USB_DT_ENDPOINT_SIZE,
 132	.bDescriptorType = USB_DT_ENDPOINT,
 133	.bEndpointAddress = USB_DIR_IN,
 134	.bmAttributes = USB_ENDPOINT_XFER_INT,
 135	.wMaxPacketSize = cpu_to_le16(2),
 136	.bInterval = 4,
 137};
 138
 139/* B.4.1  Standard AS Interface Descriptor */
 140static struct usb_interface_descriptor as_out_interface_alt_0_desc = {
 141	.bLength =		USB_DT_INTERFACE_SIZE,
 142	.bDescriptorType =	USB_DT_INTERFACE,
 143	.bAlternateSetting =	0,
 144	.bNumEndpoints =	0,
 145	.bInterfaceClass =	USB_CLASS_AUDIO,
 146	.bInterfaceSubClass =	USB_SUBCLASS_AUDIOSTREAMING,
 147};
 148
 149static struct usb_interface_descriptor as_out_interface_alt_1_desc = {
 150	.bLength =		USB_DT_INTERFACE_SIZE,
 151	.bDescriptorType =	USB_DT_INTERFACE,
 152	.bAlternateSetting =	1,
 153	.bNumEndpoints =	1,
 154	.bInterfaceClass =	USB_CLASS_AUDIO,
 155	.bInterfaceSubClass =	USB_SUBCLASS_AUDIOSTREAMING,
 156};
 157
 158static struct usb_interface_descriptor as_in_interface_alt_0_desc = {
 159	.bLength =		USB_DT_INTERFACE_SIZE,
 160	.bDescriptorType =	USB_DT_INTERFACE,
 161	.bAlternateSetting =	0,
 162	.bNumEndpoints =	0,
 163	.bInterfaceClass =	USB_CLASS_AUDIO,
 164	.bInterfaceSubClass =	USB_SUBCLASS_AUDIOSTREAMING,
 165};
 166
 167static struct usb_interface_descriptor as_in_interface_alt_1_desc = {
 168	.bLength =		USB_DT_INTERFACE_SIZE,
 169	.bDescriptorType =	USB_DT_INTERFACE,
 170	.bAlternateSetting =	1,
 171	.bNumEndpoints =	1,
 172	.bInterfaceClass =	USB_CLASS_AUDIO,
 173	.bInterfaceSubClass =	USB_SUBCLASS_AUDIOSTREAMING,
 174};
 175
 176/* B.4.2  Class-Specific AS Interface Descriptor */
 177static struct uac1_as_header_descriptor as_out_header_desc = {
 178	.bLength =		UAC_DT_AS_HEADER_SIZE,
 179	.bDescriptorType =	USB_DT_CS_INTERFACE,
 180	.bDescriptorSubtype =	UAC_AS_GENERAL,
 181	/* .bTerminalLink =	DYNAMIC */
 182	.bDelay =		1,
 183	.wFormatTag =		cpu_to_le16(UAC_FORMAT_TYPE_I_PCM),
 184};
 185
 186static struct uac1_as_header_descriptor as_in_header_desc = {
 187	.bLength =		UAC_DT_AS_HEADER_SIZE,
 188	.bDescriptorType =	USB_DT_CS_INTERFACE,
 189	.bDescriptorSubtype =	UAC_AS_GENERAL,
 190	/* .bTerminalLink =	DYNAMIC */
 191	.bDelay =		1,
 192	.wFormatTag =		cpu_to_le16(UAC_FORMAT_TYPE_I_PCM),
 193};
 194
 195DECLARE_UAC_FORMAT_TYPE_I_DISCRETE_DESC(UAC_MAX_RATES);
 196#define uac_format_type_i_discrete_descriptor			\
 197	uac_format_type_i_discrete_descriptor_##UAC_MAX_RATES
 198
 199static struct uac_format_type_i_discrete_descriptor as_out_type_i_desc = {
 200	.bLength =		0, /* filled on rate setup */
 201	.bDescriptorType =	USB_DT_CS_INTERFACE,
 202	.bDescriptorSubtype =	UAC_FORMAT_TYPE,
 203	.bFormatType =		UAC_FORMAT_TYPE_I,
 204	.bSubframeSize =	2,
 205	.bBitResolution =	16,
 206	.bSamFreqType =		0, /* filled on rate setup */
 207};
 208
 209/* Standard ISO OUT Endpoint Descriptor */
 210static struct usb_endpoint_descriptor as_out_ep_desc  = {
 211	.bLength =		USB_DT_ENDPOINT_AUDIO_SIZE,
 212	.bDescriptorType =	USB_DT_ENDPOINT,
 213	.bEndpointAddress =	USB_DIR_OUT,
 214	.bmAttributes =		USB_ENDPOINT_SYNC_ADAPTIVE
 215				| USB_ENDPOINT_XFER_ISOC,
 216	.wMaxPacketSize	=	cpu_to_le16(UAC1_OUT_EP_MAX_PACKET_SIZE),
 217	.bInterval =		4,
 218};
 219
 220/* Class-specific AS ISO OUT Endpoint Descriptor */
 221static struct uac_iso_endpoint_descriptor as_iso_out_desc = {
 222	.bLength =		UAC_ISO_ENDPOINT_DESC_SIZE,
 223	.bDescriptorType =	USB_DT_CS_ENDPOINT,
 224	.bDescriptorSubtype =	UAC_EP_GENERAL,
 225	.bmAttributes =		1,
 226	.bLockDelayUnits =	1,
 227	.wLockDelay =		cpu_to_le16(1),
 228};
 229
 230static struct uac_format_type_i_discrete_descriptor as_in_type_i_desc = {
 231	.bLength =		0, /* filled on rate setup */
 232	.bDescriptorType =	USB_DT_CS_INTERFACE,
 233	.bDescriptorSubtype =	UAC_FORMAT_TYPE,
 234	.bFormatType =		UAC_FORMAT_TYPE_I,
 235	.bSubframeSize =	2,
 236	.bBitResolution =	16,
 237	.bSamFreqType =		0, /* filled on rate setup */
 238};
 239
 240/* Standard ISO OUT Endpoint Descriptor */
 241static struct usb_endpoint_descriptor as_in_ep_desc  = {
 242	.bLength =		USB_DT_ENDPOINT_AUDIO_SIZE,
 243	.bDescriptorType =	USB_DT_ENDPOINT,
 244	.bEndpointAddress =	USB_DIR_IN,
 245	.bmAttributes =		USB_ENDPOINT_SYNC_ASYNC
 246				| USB_ENDPOINT_XFER_ISOC,
 247	.wMaxPacketSize	=	cpu_to_le16(UAC1_OUT_EP_MAX_PACKET_SIZE),
 248	.bInterval =		4,
 249};
 250
 251/* Class-specific AS ISO OUT Endpoint Descriptor */
 252static struct uac_iso_endpoint_descriptor as_iso_in_desc = {
 253	.bLength =		UAC_ISO_ENDPOINT_DESC_SIZE,
 254	.bDescriptorType =	USB_DT_CS_ENDPOINT,
 255	.bDescriptorSubtype =	UAC_EP_GENERAL,
 256	.bmAttributes =		1,
 257	.bLockDelayUnits =	0,
 258	.wLockDelay =		0,
 259};
 260
 261static struct usb_descriptor_header *f_audio_desc[] = {
 262	(struct usb_descriptor_header *)&ac_interface_desc,
 263	(struct usb_descriptor_header *)&ac_header_desc,
 264
 265	(struct usb_descriptor_header *)&usb_out_it_desc,
 266	(struct usb_descriptor_header *)&io_out_ot_desc,
 267	(struct usb_descriptor_header *)&out_feature_unit_desc,
 268
 269	(struct usb_descriptor_header *)&io_in_it_desc,
 270	(struct usb_descriptor_header *)&usb_in_ot_desc,
 271	(struct usb_descriptor_header *)&in_feature_unit_desc,
 272
 273	(struct usb_descriptor_header *)&ac_int_ep_desc,
 274
 275	(struct usb_descriptor_header *)&as_out_interface_alt_0_desc,
 276	(struct usb_descriptor_header *)&as_out_interface_alt_1_desc,
 277	(struct usb_descriptor_header *)&as_out_header_desc,
 278
 279	(struct usb_descriptor_header *)&as_out_type_i_desc,
 280
 281	(struct usb_descriptor_header *)&as_out_ep_desc,
 282	(struct usb_descriptor_header *)&as_iso_out_desc,
 283
 284	(struct usb_descriptor_header *)&as_in_interface_alt_0_desc,
 285	(struct usb_descriptor_header *)&as_in_interface_alt_1_desc,
 286	(struct usb_descriptor_header *)&as_in_header_desc,
 287
 288	(struct usb_descriptor_header *)&as_in_type_i_desc,
 289
 290	(struct usb_descriptor_header *)&as_in_ep_desc,
 291	(struct usb_descriptor_header *)&as_iso_in_desc,
 292	NULL,
 293};
 294
 295enum {
 296	STR_AC_IF,
 297	STR_USB_OUT_IT,
 298	STR_USB_OUT_IT_CH_NAMES,
 299	STR_IO_OUT_OT,
 300	STR_IO_IN_IT,
 301	STR_IO_IN_IT_CH_NAMES,
 302	STR_USB_IN_OT,
 303	STR_FU_IN,
 304	STR_FU_OUT,
 305	STR_AS_OUT_IF_ALT0,
 306	STR_AS_OUT_IF_ALT1,
 307	STR_AS_IN_IF_ALT0,
 308	STR_AS_IN_IF_ALT1,
 309};
 310
 311static struct usb_string strings_uac1[] = {
 312	/* [STR_AC_IF].s = DYNAMIC, */
 313	[STR_USB_OUT_IT].s = "Playback Input terminal",
 314	[STR_USB_OUT_IT_CH_NAMES].s = "Playback Channels",
 315	[STR_IO_OUT_OT].s = "Playback Output terminal",
 316	[STR_IO_IN_IT].s = "Capture Input terminal",
 317	[STR_IO_IN_IT_CH_NAMES].s = "Capture Channels",
 318	[STR_USB_IN_OT].s = "Capture Output terminal",
 319	[STR_FU_IN].s = "Capture Volume",
 320	[STR_FU_OUT].s = "Playback Volume",
 321	[STR_AS_OUT_IF_ALT0].s = "Playback Inactive",
 322	[STR_AS_OUT_IF_ALT1].s = "Playback Active",
 323	[STR_AS_IN_IF_ALT0].s = "Capture Inactive",
 324	[STR_AS_IN_IF_ALT1].s = "Capture Active",
 325	{ },
 326};
 327
 328static struct usb_gadget_strings str_uac1 = {
 329	.language = 0x0409,	/* en-us */
 330	.strings = strings_uac1,
 331};
 332
 333static struct usb_gadget_strings *uac1_strings[] = {
 334	&str_uac1,
 335	NULL,
 336};
 337
 338/*
 339 * This function is an ALSA sound card following USB Audio Class Spec 1.0.
 340 */
 341
 342static void uac_cs_attr_sample_rate(struct usb_ep *ep, struct usb_request *req)
 343{
 344	struct usb_function *fn = ep->driver_data;
 345	struct usb_composite_dev *cdev = fn->config->cdev;
 346	struct g_audio *agdev = func_to_g_audio(fn);
 347	struct f_uac1 *uac1 = func_to_uac1(fn);
 348	u8 *buf = (u8 *)req->buf;
 349	u32 val = 0;
 350
 351	if (req->actual != 3) {
 352		WARN(cdev, "Invalid data size for UAC_EP_CS_ATTR_SAMPLE_RATE.\n");
 353		return;
 354	}
 355
 356	val = buf[0] | (buf[1] << 8) | (buf[2] << 16);
 357	if (uac1->ctl_id == (USB_DIR_IN | 2)) {
 358		uac1->p_srate = val;
 359		u_audio_set_playback_srate(agdev, uac1->p_srate);
 360	} else if (uac1->ctl_id == (USB_DIR_OUT | 1)) {
 361		uac1->c_srate = val;
 362		u_audio_set_capture_srate(agdev, uac1->c_srate);
 363	}
 364}
 365
 366static void audio_notify_complete(struct usb_ep *_ep, struct usb_request *req)
 367{
 368	struct g_audio *audio = req->context;
 369	struct f_uac1 *uac1 = func_to_uac1(&audio->func);
 370
 371	atomic_dec(&uac1->int_count);
 372	kfree(req->buf);
 373	usb_ep_free_request(_ep, req);
 374}
 375
 376static int audio_notify(struct g_audio *audio, int unit_id, int cs)
 377{
 378	struct f_uac1 *uac1 = func_to_uac1(&audio->func);
 379	struct usb_request *req;
 380	struct uac1_status_word *msg;
 381	int ret;
 382
 383	if (!uac1->int_ep->enabled)
 384		return 0;
 385
 386	if (atomic_inc_return(&uac1->int_count) > UAC1_DEF_INT_REQ_NUM) {
 387		atomic_dec(&uac1->int_count);
 388		return 0;
 389	}
 390
 391	req = usb_ep_alloc_request(uac1->int_ep, GFP_ATOMIC);
 392	if (req == NULL) {
 393		ret = -ENOMEM;
 394		goto err_dec_int_count;
 395	}
 396
 397	msg = kmalloc(sizeof(*msg), GFP_ATOMIC);
 398	if (msg == NULL) {
 399		ret = -ENOMEM;
 400		goto err_free_request;
 401	}
 402
 403	msg->bStatusType = UAC1_STATUS_TYPE_IRQ_PENDING
 404				| UAC1_STATUS_TYPE_ORIG_AUDIO_CONTROL_IF;
 405	msg->bOriginator = unit_id;
 406
 407	req->length = sizeof(*msg);
 408	req->buf = msg;
 409	req->context = audio;
 410	req->complete = audio_notify_complete;
 411
 412	ret = usb_ep_queue(uac1->int_ep, req, GFP_ATOMIC);
 413
 414	if (ret)
 415		goto err_free_msg;
 416
 417	return 0;
 418
 419err_free_msg:
 420	kfree(msg);
 421err_free_request:
 422	usb_ep_free_request(uac1->int_ep, req);
 423err_dec_int_count:
 424	atomic_dec(&uac1->int_count);
 425
 426	return ret;
 427}
 428
 429static int
 430in_rq_cur(struct usb_function *fn, const struct usb_ctrlrequest *cr)
 431{
 432	struct usb_request *req = fn->config->cdev->req;
 433	struct g_audio *audio = func_to_g_audio(fn);
 434	struct f_uac1_opts *opts = g_audio_to_uac1_opts(audio);
 435	u16 w_length = le16_to_cpu(cr->wLength);
 436	u16 w_index = le16_to_cpu(cr->wIndex);
 437	u16 w_value = le16_to_cpu(cr->wValue);
 438	u8 entity_id = (w_index >> 8) & 0xff;
 439	u8 control_selector = w_value >> 8;
 440	int value = -EOPNOTSUPP;
 441
 442	if ((FUIN_EN(opts) && (entity_id == USB_IN_FU_ID)) ||
 443			(FUOUT_EN(opts) && (entity_id == USB_OUT_FU_ID))) {
 444		unsigned int is_playback = 0;
 445
 446		if (FUIN_EN(opts) && (entity_id == USB_IN_FU_ID))
 447			is_playback = 1;
 448
 449		if (control_selector == UAC_FU_MUTE) {
 450			unsigned int mute;
 451
 452			u_audio_get_mute(audio, is_playback, &mute);
 453
 454			*(u8 *)req->buf = mute;
 455			value = min_t(unsigned int, w_length, 1);
 456		} else if (control_selector == UAC_FU_VOLUME) {
 457			__le16 c;
 458			s16 volume;
 459
 460			u_audio_get_volume(audio, is_playback, &volume);
 461
 462			c = cpu_to_le16(volume);
 463
 464			value = min_t(unsigned int, w_length, sizeof(c));
 465			memcpy(req->buf, &c, value);
 466		} else {
 467			dev_err(&audio->gadget->dev,
 468				"%s:%d control_selector=%d TODO!\n",
 469				__func__, __LINE__, control_selector);
 470		}
 471	} else {
 472		dev_err(&audio->gadget->dev,
 473			"%s:%d entity_id=%d control_selector=%d TODO!\n",
 474			__func__, __LINE__, entity_id, control_selector);
 475	}
 476
 477	return value;
 478}
 479
 480static int
 481in_rq_min(struct usb_function *fn, const struct usb_ctrlrequest *cr)
 482{
 483	struct usb_request *req = fn->config->cdev->req;
 484	struct g_audio *audio = func_to_g_audio(fn);
 485	struct f_uac1_opts *opts = g_audio_to_uac1_opts(audio);
 486	u16 w_length = le16_to_cpu(cr->wLength);
 487	u16 w_index = le16_to_cpu(cr->wIndex);
 488	u16 w_value = le16_to_cpu(cr->wValue);
 489	u8 entity_id = (w_index >> 8) & 0xff;
 490	u8 control_selector = w_value >> 8;
 491	int value = -EOPNOTSUPP;
 492
 493	if ((FUIN_EN(opts) && (entity_id == USB_IN_FU_ID)) ||
 494			(FUOUT_EN(opts) && (entity_id == USB_OUT_FU_ID))) {
 495		unsigned int is_playback = 0;
 496
 497		if (FUIN_EN(opts) && (entity_id == USB_IN_FU_ID))
 498			is_playback = 1;
 499
 500		if (control_selector == UAC_FU_VOLUME) {
 501			__le16 r;
 502			s16 min_db;
 503
 504			if (is_playback)
 505				min_db = opts->p_volume_min;
 506			else
 507				min_db = opts->c_volume_min;
 508
 509			r = cpu_to_le16(min_db);
 510
 511			value = min_t(unsigned int, w_length, sizeof(r));
 512			memcpy(req->buf, &r, value);
 513		} else {
 514			dev_err(&audio->gadget->dev,
 515				"%s:%d control_selector=%d TODO!\n",
 516				__func__, __LINE__, control_selector);
 517		}
 518	} else {
 519		dev_err(&audio->gadget->dev,
 520			"%s:%d entity_id=%d control_selector=%d TODO!\n",
 521			__func__, __LINE__, entity_id, control_selector);
 522	}
 523
 524	return value;
 525}
 526
 527static int
 528in_rq_max(struct usb_function *fn, const struct usb_ctrlrequest *cr)
 529{
 530	struct usb_request *req = fn->config->cdev->req;
 531	struct g_audio *audio = func_to_g_audio(fn);
 532	struct f_uac1_opts *opts = g_audio_to_uac1_opts(audio);
 533	u16 w_length = le16_to_cpu(cr->wLength);
 534	u16 w_index = le16_to_cpu(cr->wIndex);
 535	u16 w_value = le16_to_cpu(cr->wValue);
 536	u8 entity_id = (w_index >> 8) & 0xff;
 537	u8 control_selector = w_value >> 8;
 538	int value = -EOPNOTSUPP;
 539
 540	if ((FUIN_EN(opts) && (entity_id == USB_IN_FU_ID)) ||
 541			(FUOUT_EN(opts) && (entity_id == USB_OUT_FU_ID))) {
 542		unsigned int is_playback = 0;
 543
 544		if (FUIN_EN(opts) && (entity_id == USB_IN_FU_ID))
 545			is_playback = 1;
 546
 547		if (control_selector == UAC_FU_VOLUME) {
 548			__le16 r;
 549			s16 max_db;
 550
 551			if (is_playback)
 552				max_db = opts->p_volume_max;
 553			else
 554				max_db = opts->c_volume_max;
 555
 556			r = cpu_to_le16(max_db);
 557
 558			value = min_t(unsigned int, w_length, sizeof(r));
 559			memcpy(req->buf, &r, value);
 560		} else {
 561			dev_err(&audio->gadget->dev,
 562				"%s:%d control_selector=%d TODO!\n",
 563				__func__, __LINE__, control_selector);
 564		}
 565	} else {
 566		dev_err(&audio->gadget->dev,
 567			"%s:%d entity_id=%d control_selector=%d TODO!\n",
 568			__func__, __LINE__, entity_id, control_selector);
 569	}
 570
 571	return value;
 572}
 573
 574static int
 575in_rq_res(struct usb_function *fn, const struct usb_ctrlrequest *cr)
 576{
 577	struct usb_request *req = fn->config->cdev->req;
 578	struct g_audio *audio = func_to_g_audio(fn);
 579	struct f_uac1_opts *opts = g_audio_to_uac1_opts(audio);
 580	u16 w_length = le16_to_cpu(cr->wLength);
 581	u16 w_index = le16_to_cpu(cr->wIndex);
 582	u16 w_value = le16_to_cpu(cr->wValue);
 583	u8 entity_id = (w_index >> 8) & 0xff;
 584	u8 control_selector = w_value >> 8;
 585	int value = -EOPNOTSUPP;
 586
 587	if ((FUIN_EN(opts) && (entity_id == USB_IN_FU_ID)) ||
 588			(FUOUT_EN(opts) && (entity_id == USB_OUT_FU_ID))) {
 589		unsigned int is_playback = 0;
 590
 591		if (FUIN_EN(opts) && (entity_id == USB_IN_FU_ID))
 592			is_playback = 1;
 593
 594		if (control_selector == UAC_FU_VOLUME) {
 595			__le16 r;
 596			s16 res_db;
 597
 598			if (is_playback)
 599				res_db = opts->p_volume_res;
 600			else
 601				res_db = opts->c_volume_res;
 602
 603			r = cpu_to_le16(res_db);
 604
 605			value = min_t(unsigned int, w_length, sizeof(r));
 606			memcpy(req->buf, &r, value);
 607		} else {
 608			dev_err(&audio->gadget->dev,
 609				"%s:%d control_selector=%d TODO!\n",
 610				__func__, __LINE__, control_selector);
 611		}
 612	} else {
 613		dev_err(&audio->gadget->dev,
 614			"%s:%d entity_id=%d control_selector=%d TODO!\n",
 615			__func__, __LINE__, entity_id, control_selector);
 616	}
 617
 618	return value;
 619}
 620
 621static void
 622out_rq_cur_complete(struct usb_ep *ep, struct usb_request *req)
 623{
 624	struct g_audio *audio = req->context;
 625	struct usb_composite_dev *cdev = audio->func.config->cdev;
 626	struct f_uac1_opts *opts = g_audio_to_uac1_opts(audio);
 627	struct f_uac1 *uac1 = func_to_uac1(&audio->func);
 628	struct usb_ctrlrequest *cr = &uac1->setup_cr;
 629	u16 w_index = le16_to_cpu(cr->wIndex);
 630	u16 w_value = le16_to_cpu(cr->wValue);
 631	u8 entity_id = (w_index >> 8) & 0xff;
 632	u8 control_selector = w_value >> 8;
 633
 634	if (req->status != 0) {
 635		dev_dbg(&cdev->gadget->dev, "completion err %d\n", req->status);
 636		return;
 637	}
 638
 639	if ((FUIN_EN(opts) && (entity_id == USB_IN_FU_ID)) ||
 640			(FUOUT_EN(opts) && (entity_id == USB_OUT_FU_ID))) {
 641		unsigned int is_playback = 0;
 642
 643		if (FUIN_EN(opts) && (entity_id == USB_IN_FU_ID))
 644			is_playback = 1;
 645
 646		if (control_selector == UAC_FU_MUTE) {
 647			u8 mute = *(u8 *)req->buf;
 648
 649			u_audio_set_mute(audio, is_playback, mute);
 650
 651			return;
 652		} else if (control_selector == UAC_FU_VOLUME) {
 653			__le16 *c = req->buf;
 654			s16 volume;
 655
 656			volume = le16_to_cpu(*c);
 657			u_audio_set_volume(audio, is_playback, volume);
 658
 659			return;
 660		} else {
 661			dev_err(&audio->gadget->dev,
 662				"%s:%d control_selector=%d TODO!\n",
 663				__func__, __LINE__, control_selector);
 664			usb_ep_set_halt(ep);
 665		}
 666	} else {
 667		dev_err(&audio->gadget->dev,
 668			"%s:%d entity_id=%d control_selector=%d TODO!\n",
 669			__func__, __LINE__, entity_id, control_selector);
 670		usb_ep_set_halt(ep);
 671
 672	}
 673}
 674
 675static int
 676out_rq_cur(struct usb_function *fn, const struct usb_ctrlrequest *cr)
 677{
 678	struct usb_request *req = fn->config->cdev->req;
 679	struct g_audio *audio = func_to_g_audio(fn);
 680	struct f_uac1_opts *opts = g_audio_to_uac1_opts(audio);
 681	struct f_uac1 *uac1 = func_to_uac1(&audio->func);
 682	u16 w_length = le16_to_cpu(cr->wLength);
 683	u16 w_index = le16_to_cpu(cr->wIndex);
 684	u16 w_value = le16_to_cpu(cr->wValue);
 685	u8 entity_id = (w_index >> 8) & 0xff;
 686	u8 control_selector = w_value >> 8;
 687
 688	if ((FUIN_EN(opts) && (entity_id == USB_IN_FU_ID)) ||
 689			(FUOUT_EN(opts) && (entity_id == USB_OUT_FU_ID))) {
 690		memcpy(&uac1->setup_cr, cr, sizeof(*cr));
 691		req->context = audio;
 692		req->complete = out_rq_cur_complete;
 693
 694		return w_length;
 695	} else {
 696		dev_err(&audio->gadget->dev,
 697			"%s:%d entity_id=%d control_selector=%d TODO!\n",
 698			__func__, __LINE__, entity_id, control_selector);
 699	}
 700	return -EOPNOTSUPP;
 701}
 702
 703static int ac_rq_in(struct usb_function *f,
 704		const struct usb_ctrlrequest *ctrl)
 705{
 706	struct usb_composite_dev *cdev = f->config->cdev;
 707	int value = -EOPNOTSUPP;
 708	u8 ep = ((le16_to_cpu(ctrl->wIndex) >> 8) & 0xFF);
 709	u16 len = le16_to_cpu(ctrl->wLength);
 710	u16 w_value = le16_to_cpu(ctrl->wValue);
 711
 712	DBG(cdev, "bRequest 0x%x, w_value 0x%04x, len %d, endpoint %d\n",
 713			ctrl->bRequest, w_value, len, ep);
 714
 715	switch (ctrl->bRequest) {
 716	case UAC_GET_CUR:
 717		return in_rq_cur(f, ctrl);
 718	case UAC_GET_MIN:
 719		return in_rq_min(f, ctrl);
 720	case UAC_GET_MAX:
 721		return in_rq_max(f, ctrl);
 722	case UAC_GET_RES:
 723		return in_rq_res(f, ctrl);
 724	case UAC_GET_MEM:
 725		break;
 726	case UAC_GET_STAT:
 727		value = len;
 728		break;
 729	default:
 730		break;
 731	}
 732
 733	return value;
 734}
 735
 736static int audio_set_endpoint_req(struct usb_function *f,
 737		const struct usb_ctrlrequest *ctrl)
 738{
 739	struct usb_composite_dev *cdev = f->config->cdev;
 740	struct usb_request	*req = f->config->cdev->req;
 741	struct f_uac1		*uac1 = func_to_uac1(f);
 742	int			value = -EOPNOTSUPP;
 743	u16			ep = le16_to_cpu(ctrl->wIndex);
 744	u16			len = le16_to_cpu(ctrl->wLength);
 745	u16			w_value = le16_to_cpu(ctrl->wValue);
 746	u8			cs = w_value >> 8;
 747
 748	DBG(cdev, "bRequest 0x%x, w_value 0x%04x, len %d, endpoint %d\n",
 749			ctrl->bRequest, w_value, len, ep);
 750
 751	switch (ctrl->bRequest) {
 752	case UAC_SET_CUR: {
 753		if (cs == UAC_EP_CS_ATTR_SAMPLE_RATE) {
 754			cdev->gadget->ep0->driver_data = f;
 755			uac1->ctl_id = ep;
 756			req->complete = uac_cs_attr_sample_rate;
 757		}
 758		value = len;
 759		break;
 760	}
 761
 762	case UAC_SET_MIN:
 763		break;
 764
 765	case UAC_SET_MAX:
 766		break;
 767
 768	case UAC_SET_RES:
 769		break;
 770
 771	case UAC_SET_MEM:
 772		break;
 773
 774	default:
 775		break;
 776	}
 777
 778	return value;
 779}
 780
 781static int audio_get_endpoint_req(struct usb_function *f,
 782		const struct usb_ctrlrequest *ctrl)
 783{
 784	struct usb_composite_dev *cdev = f->config->cdev;
 785	struct usb_request *req = f->config->cdev->req;
 786	struct f_uac1 *uac1 = func_to_uac1(f);
 787	u8 *buf = (u8 *)req->buf;
 788	int value = -EOPNOTSUPP;
 789	u8 ep = le16_to_cpu(ctrl->wIndex);
 790	u16 len = le16_to_cpu(ctrl->wLength);
 791	u16 w_value = le16_to_cpu(ctrl->wValue);
 792	u8 cs = w_value >> 8;
 793	u32 val = 0;
 794
 795	DBG(cdev, "bRequest 0x%x, w_value 0x%04x, len %d, endpoint %d\n",
 796			ctrl->bRequest, w_value, len, ep);
 797
 798	switch (ctrl->bRequest) {
 799	case UAC_GET_CUR: {
 800		if (cs == UAC_EP_CS_ATTR_SAMPLE_RATE) {
 801			if (ep == (USB_DIR_IN | 2))
 802				val = uac1->p_srate;
 803			else if (ep == (USB_DIR_OUT | 1))
 804				val = uac1->c_srate;
 805			buf[2] = (val >> 16) & 0xff;
 806			buf[1] = (val >> 8) & 0xff;
 807			buf[0] = val & 0xff;
 808		}
 809		value = len;
 810		break;
 811	}
 812	case UAC_GET_MIN:
 813	case UAC_GET_MAX:
 814	case UAC_GET_RES:
 815		value = len;
 816		break;
 817	case UAC_GET_MEM:
 818		break;
 819	default:
 820		break;
 821	}
 822
 823	return value;
 824}
 825
 826static int
 827f_audio_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
 828{
 829	struct usb_composite_dev *cdev = f->config->cdev;
 830	struct usb_request	*req = cdev->req;
 831	int			value = -EOPNOTSUPP;
 832	u16			w_index = le16_to_cpu(ctrl->wIndex);
 833	u16			w_value = le16_to_cpu(ctrl->wValue);
 834	u16			w_length = le16_to_cpu(ctrl->wLength);
 835
 836	/* composite driver infrastructure handles everything; interface
 837	 * activation uses set_alt().
 838	 */
 839	switch (ctrl->bRequestType) {
 840	case USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_ENDPOINT:
 841		value = audio_set_endpoint_req(f, ctrl);
 842		break;
 843
 844	case USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT:
 845		value = audio_get_endpoint_req(f, ctrl);
 846		break;
 847	case USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE:
 848		if (ctrl->bRequest == UAC_SET_CUR)
 849			value = out_rq_cur(f, ctrl);
 850		break;
 851	case USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE:
 852		value = ac_rq_in(f, ctrl);
 853		break;
 854	default:
 855		ERROR(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
 856			ctrl->bRequestType, ctrl->bRequest,
 857			w_value, w_index, w_length);
 858	}
 859
 860	/* respond with data transfer or status phase? */
 861	if (value >= 0) {
 862		DBG(cdev, "audio req%02x.%02x v%04x i%04x l%d\n",
 863			ctrl->bRequestType, ctrl->bRequest,
 864			w_value, w_index, w_length);
 865		req->zero = 0;
 866		req->length = value;
 867		value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
 868		if (value < 0)
 869			ERROR(cdev, "audio response on err %d\n", value);
 870	}
 871
 872	/* device either stalls (value < 0) or reports success */
 873	return value;
 874}
 875
 876static int f_audio_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
 877{
 878	struct usb_composite_dev *cdev = f->config->cdev;
 879	struct usb_gadget *gadget = cdev->gadget;
 880	struct device *dev = &gadget->dev;
 881	struct g_audio *audio = func_to_g_audio(f);
 882	struct f_uac1 *uac1 = func_to_uac1(f);
 883	int ret = 0;
 884
 885	/* No i/f has more than 2 alt settings */
 886	if (alt > 1) {
 887		dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
 888		return -EINVAL;
 889	}
 890
 891	if (intf == uac1->ac_intf) {
 892		/* Control I/f has only 1 AltSetting - 0 */
 893		if (alt) {
 894			dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
 895			return -EINVAL;
 896		}
 897
 898		/* restart interrupt endpoint */
 899		if (uac1->int_ep) {
 900			usb_ep_disable(uac1->int_ep);
 901			config_ep_by_speed(gadget, &audio->func, uac1->int_ep);
 902			usb_ep_enable(uac1->int_ep);
 903		}
 904
 905		return 0;
 906	}
 907
 908	if (intf == uac1->as_out_intf) {
 909		uac1->as_out_alt = alt;
 910
 911		if (alt)
 912			ret = u_audio_start_capture(&uac1->g_audio);
 913		else
 914			u_audio_stop_capture(&uac1->g_audio);
 915	} else if (intf == uac1->as_in_intf) {
 916		uac1->as_in_alt = alt;
 917
 918		if (alt)
 919			ret = u_audio_start_playback(&uac1->g_audio);
 920		else
 921			u_audio_stop_playback(&uac1->g_audio);
 922	} else {
 923		dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
 924		return -EINVAL;
 925	}
 926
 927	return ret;
 928}
 929
 930static int f_audio_get_alt(struct usb_function *f, unsigned intf)
 931{
 932	struct usb_composite_dev *cdev = f->config->cdev;
 933	struct usb_gadget *gadget = cdev->gadget;
 934	struct device *dev = &gadget->dev;
 935	struct f_uac1 *uac1 = func_to_uac1(f);
 936
 937	if (intf == uac1->ac_intf)
 938		return uac1->ac_alt;
 939	else if (intf == uac1->as_out_intf)
 940		return uac1->as_out_alt;
 941	else if (intf == uac1->as_in_intf)
 942		return uac1->as_in_alt;
 943	else
 944		dev_err(dev, "%s:%d Invalid Interface %d!\n",
 945			__func__, __LINE__, intf);
 946
 947	return -EINVAL;
 948}
 949
 950
 951static void f_audio_disable(struct usb_function *f)
 952{
 953	struct f_uac1 *uac1 = func_to_uac1(f);
 954
 955	uac1->as_out_alt = 0;
 956	uac1->as_in_alt = 0;
 957
 958	u_audio_stop_playback(&uac1->g_audio);
 959	u_audio_stop_capture(&uac1->g_audio);
 960	if (uac1->int_ep)
 961		usb_ep_disable(uac1->int_ep);
 962}
 963
 964static void
 965f_audio_suspend(struct usb_function *f)
 966{
 967	struct f_uac1 *uac1 = func_to_uac1(f);
 968
 969	u_audio_suspend(&uac1->g_audio);
 970}
 971
 972/*-------------------------------------------------------------------------*/
 973static struct uac_feature_unit_descriptor *build_fu_desc(int chmask)
 974{
 975	struct uac_feature_unit_descriptor *fu_desc;
 976	int channels = num_channels(chmask);
 977	int fu_desc_size = UAC_DT_FEATURE_UNIT_SIZE(channels);
 978
 979	fu_desc = kzalloc(fu_desc_size, GFP_KERNEL);
 980	if (!fu_desc)
 981		return NULL;
 982
 983	fu_desc->bLength = fu_desc_size;
 984	fu_desc->bDescriptorType = USB_DT_CS_INTERFACE;
 985
 986	fu_desc->bDescriptorSubtype = UAC_FEATURE_UNIT;
 987	fu_desc->bControlSize  = 2;
 988
 989	/* bUnitID, bSourceID and bmaControls will be defined later */
 990
 991	return fu_desc;
 992}
 993
 994/* B.3.2  Class-Specific AC Interface Descriptor */
 995static struct
 996uac1_ac_header_descriptor *build_ac_header_desc(struct f_uac1_opts *opts)
 997{
 998	struct uac1_ac_header_descriptor *ac_desc;
 999	int ac_header_desc_size;
1000	int num_ifaces = 0;
1001
1002	if (EPOUT_EN(opts))
1003		num_ifaces++;
1004	if (EPIN_EN(opts))
1005		num_ifaces++;
1006
1007	ac_header_desc_size = UAC_DT_AC_HEADER_SIZE(num_ifaces);
1008
1009	ac_desc = kzalloc(ac_header_desc_size, GFP_KERNEL);
1010	if (!ac_desc)
1011		return NULL;
1012
1013	ac_desc->bLength = ac_header_desc_size;
1014	ac_desc->bDescriptorType = USB_DT_CS_INTERFACE;
1015	ac_desc->bDescriptorSubtype = UAC_HEADER;
1016	ac_desc->bcdADC = cpu_to_le16(0x0100);
1017	ac_desc->bInCollection = num_ifaces;
1018
1019	/* wTotalLength and baInterfaceNr will be defined later */
1020
1021	return ac_desc;
1022}
1023
1024/* Use macro to overcome line length limitation */
1025#define USBDHDR(p) (struct usb_descriptor_header *)(p)
1026
1027static void setup_descriptor(struct f_uac1_opts *opts)
1028{
1029	/* patch descriptors */
1030	int i = 1; /* ID's start with 1 */
1031
1032	if (EPOUT_EN(opts))
1033		usb_out_it_desc.bTerminalID = i++;
1034	if (EPIN_EN(opts))
1035		io_in_it_desc.bTerminalID = i++;
1036	if (EPOUT_EN(opts))
1037		io_out_ot_desc.bTerminalID = i++;
1038	if (EPIN_EN(opts))
1039		usb_in_ot_desc.bTerminalID = i++;
1040	if (FUOUT_EN(opts))
1041		out_feature_unit_desc->bUnitID = i++;
1042	if (FUIN_EN(opts))
1043		in_feature_unit_desc->bUnitID = i++;
1044
1045	if (FUIN_EN(opts)) {
1046		usb_in_ot_desc.bSourceID = in_feature_unit_desc->bUnitID;
1047		in_feature_unit_desc->bSourceID = io_in_it_desc.bTerminalID;
1048	} else {
1049		usb_in_ot_desc.bSourceID = io_in_it_desc.bTerminalID;
1050	}
1051	if (FUOUT_EN(opts)) {
1052		io_out_ot_desc.bSourceID = out_feature_unit_desc->bUnitID;
1053		out_feature_unit_desc->bSourceID = usb_out_it_desc.bTerminalID;
1054	} else {
1055		io_out_ot_desc.bSourceID = usb_out_it_desc.bTerminalID;
1056	}
1057
1058	as_out_header_desc.bTerminalLink = usb_out_it_desc.bTerminalID;
1059	as_in_header_desc.bTerminalLink = usb_in_ot_desc.bTerminalID;
1060
1061	ac_header_desc->wTotalLength = cpu_to_le16(ac_header_desc->bLength);
1062
1063	if (EPIN_EN(opts)) {
1064		u16 len = le16_to_cpu(ac_header_desc->wTotalLength);
1065
1066		len += sizeof(usb_in_ot_desc);
1067		len += sizeof(io_in_it_desc);
1068		if (FUIN_EN(opts))
1069			len += in_feature_unit_desc->bLength;
1070		ac_header_desc->wTotalLength = cpu_to_le16(len);
1071	}
1072	if (EPOUT_EN(opts)) {
1073		u16 len = le16_to_cpu(ac_header_desc->wTotalLength);
1074
1075		len += sizeof(usb_out_it_desc);
1076		len += sizeof(io_out_ot_desc);
1077		if (FUOUT_EN(opts))
1078			len += out_feature_unit_desc->bLength;
1079		ac_header_desc->wTotalLength = cpu_to_le16(len);
1080	}
1081
1082	i = 0;
1083	f_audio_desc[i++] = USBDHDR(&ac_interface_desc);
1084	f_audio_desc[i++] = USBDHDR(ac_header_desc);
1085
1086	if (EPOUT_EN(opts)) {
1087		f_audio_desc[i++] = USBDHDR(&usb_out_it_desc);
1088		f_audio_desc[i++] = USBDHDR(&io_out_ot_desc);
1089		if (FUOUT_EN(opts))
1090			f_audio_desc[i++] = USBDHDR(out_feature_unit_desc);
1091	}
1092
1093	if (EPIN_EN(opts)) {
1094		f_audio_desc[i++] = USBDHDR(&io_in_it_desc);
1095		f_audio_desc[i++] = USBDHDR(&usb_in_ot_desc);
1096		if (FUIN_EN(opts))
1097			f_audio_desc[i++] = USBDHDR(in_feature_unit_desc);
1098	}
1099
1100	if (FUOUT_EN(opts) || FUIN_EN(opts))
1101		f_audio_desc[i++] = USBDHDR(&ac_int_ep_desc);
1102
1103	if (EPOUT_EN(opts)) {
1104		f_audio_desc[i++] = USBDHDR(&as_out_interface_alt_0_desc);
1105		f_audio_desc[i++] = USBDHDR(&as_out_interface_alt_1_desc);
1106		f_audio_desc[i++] = USBDHDR(&as_out_header_desc);
1107		f_audio_desc[i++] = USBDHDR(&as_out_type_i_desc);
1108		f_audio_desc[i++] = USBDHDR(&as_out_ep_desc);
1109		f_audio_desc[i++] = USBDHDR(&as_iso_out_desc);
1110	}
1111	if (EPIN_EN(opts)) {
1112		f_audio_desc[i++] = USBDHDR(&as_in_interface_alt_0_desc);
1113		f_audio_desc[i++] = USBDHDR(&as_in_interface_alt_1_desc);
1114		f_audio_desc[i++] = USBDHDR(&as_in_header_desc);
1115		f_audio_desc[i++] = USBDHDR(&as_in_type_i_desc);
1116		f_audio_desc[i++] = USBDHDR(&as_in_ep_desc);
1117		f_audio_desc[i++] = USBDHDR(&as_iso_in_desc);
1118	}
1119	f_audio_desc[i] = NULL;
1120}
1121
1122static int f_audio_validate_opts(struct g_audio *audio, struct device *dev)
1123{
1124	struct f_uac1_opts *opts = g_audio_to_uac1_opts(audio);
1125
1126	if (!opts->p_chmask && !opts->c_chmask) {
1127		dev_err(dev, "Error: no playback and capture channels\n");
1128		return -EINVAL;
1129	} else if (opts->p_chmask & ~UAC1_CHANNEL_MASK) {
1130		dev_err(dev, "Error: unsupported playback channels mask\n");
1131		return -EINVAL;
1132	} else if (opts->c_chmask & ~UAC1_CHANNEL_MASK) {
1133		dev_err(dev, "Error: unsupported capture channels mask\n");
1134		return -EINVAL;
1135	} else if ((opts->p_ssize < 1) || (opts->p_ssize > 4)) {
1136		dev_err(dev, "Error: incorrect playback sample size\n");
1137		return -EINVAL;
1138	} else if ((opts->c_ssize < 1) || (opts->c_ssize > 4)) {
1139		dev_err(dev, "Error: incorrect capture sample size\n");
1140		return -EINVAL;
1141	} else if (!opts->p_srates[0]) {
1142		dev_err(dev, "Error: incorrect playback sampling rate\n");
1143		return -EINVAL;
1144	} else if (!opts->c_srates[0]) {
1145		dev_err(dev, "Error: incorrect capture sampling rate\n");
1146		return -EINVAL;
1147	}
1148
1149	if (opts->p_volume_max <= opts->p_volume_min) {
1150		dev_err(dev, "Error: incorrect playback volume max/min\n");
1151		return -EINVAL;
1152	} else if (opts->c_volume_max <= opts->c_volume_min) {
1153		dev_err(dev, "Error: incorrect capture volume max/min\n");
1154		return -EINVAL;
1155	} else if (opts->p_volume_res <= 0) {
1156		dev_err(dev, "Error: negative/zero playback volume resolution\n");
1157		return -EINVAL;
1158	} else if (opts->c_volume_res <= 0) {
1159		dev_err(dev, "Error: negative/zero capture volume resolution\n");
1160		return -EINVAL;
1161	}
1162
1163	if ((opts->p_volume_max - opts->p_volume_min) % opts->p_volume_res) {
1164		dev_err(dev, "Error: incorrect playback volume resolution\n");
1165		return -EINVAL;
1166	} else if ((opts->c_volume_max - opts->c_volume_min) % opts->c_volume_res) {
1167		dev_err(dev, "Error: incorrect capture volume resolution\n");
1168		return -EINVAL;
1169	}
1170
1171	return 0;
1172}
1173
1174/* audio function driver setup/binding */
1175static int f_audio_bind(struct usb_configuration *c, struct usb_function *f)
1176{
1177	struct usb_composite_dev	*cdev = c->cdev;
1178	struct usb_gadget		*gadget = cdev->gadget;
1179	struct device			*dev = &gadget->dev;
1180	struct f_uac1			*uac1 = func_to_uac1(f);
1181	struct g_audio			*audio = func_to_g_audio(f);
1182	struct f_uac1_opts		*audio_opts;
1183	struct usb_ep			*ep = NULL;
1184	struct usb_string		*us;
1185	int				ba_iface_id;
 
1186	int				status;
1187	int				idx, i;
1188
1189	status = f_audio_validate_opts(audio, dev);
1190	if (status)
1191		return status;
1192
1193	audio_opts = container_of(f->fi, struct f_uac1_opts, func_inst);
1194
1195	strings_uac1[STR_AC_IF].s = audio_opts->function_name;
1196
1197	us = usb_gstrings_attach(cdev, uac1_strings, ARRAY_SIZE(strings_uac1));
1198	if (IS_ERR(us))
1199		return PTR_ERR(us);
1200
1201	ac_header_desc = build_ac_header_desc(audio_opts);
1202	if (!ac_header_desc)
1203		return -ENOMEM;
1204
1205	if (FUOUT_EN(audio_opts)) {
1206		out_feature_unit_desc = build_fu_desc(audio_opts->c_chmask);
1207		if (!out_feature_unit_desc) {
1208			status = -ENOMEM;
1209			goto fail;
1210		}
1211	}
1212	if (FUIN_EN(audio_opts)) {
1213		in_feature_unit_desc = build_fu_desc(audio_opts->p_chmask);
1214		if (!in_feature_unit_desc) {
1215			status = -ENOMEM;
1216			goto err_free_fu;
1217		}
1218	}
1219
1220	ac_interface_desc.iInterface = us[STR_AC_IF].id;
1221	usb_out_it_desc.iTerminal = us[STR_USB_OUT_IT].id;
1222	usb_out_it_desc.iChannelNames = us[STR_USB_OUT_IT_CH_NAMES].id;
1223	io_out_ot_desc.iTerminal = us[STR_IO_OUT_OT].id;
1224	as_out_interface_alt_0_desc.iInterface = us[STR_AS_OUT_IF_ALT0].id;
1225	as_out_interface_alt_1_desc.iInterface = us[STR_AS_OUT_IF_ALT1].id;
1226	io_in_it_desc.iTerminal = us[STR_IO_IN_IT].id;
1227	io_in_it_desc.iChannelNames = us[STR_IO_IN_IT_CH_NAMES].id;
1228	usb_in_ot_desc.iTerminal = us[STR_USB_IN_OT].id;
1229	as_in_interface_alt_0_desc.iInterface = us[STR_AS_IN_IF_ALT0].id;
1230	as_in_interface_alt_1_desc.iInterface = us[STR_AS_IN_IF_ALT1].id;
1231
1232	if (FUOUT_EN(audio_opts)) {
1233		u8 *i_feature;
1234
1235		i_feature = (u8 *)out_feature_unit_desc +
1236					out_feature_unit_desc->bLength - 1;
1237		*i_feature = us[STR_FU_OUT].id;
1238	}
1239	if (FUIN_EN(audio_opts)) {
1240		u8 *i_feature;
1241
1242		i_feature = (u8 *)in_feature_unit_desc +
1243					in_feature_unit_desc->bLength - 1;
1244		*i_feature = us[STR_FU_IN].id;
1245	}
1246
1247	/* Set channel numbers */
1248	usb_out_it_desc.bNrChannels = num_channels(audio_opts->c_chmask);
1249	usb_out_it_desc.wChannelConfig = cpu_to_le16(audio_opts->c_chmask);
1250	as_out_type_i_desc.bNrChannels = num_channels(audio_opts->c_chmask);
1251	as_out_type_i_desc.bSubframeSize = audio_opts->c_ssize;
1252	as_out_type_i_desc.bBitResolution = audio_opts->c_ssize * 8;
1253	io_in_it_desc.bNrChannels = num_channels(audio_opts->p_chmask);
1254	io_in_it_desc.wChannelConfig = cpu_to_le16(audio_opts->p_chmask);
1255	as_in_type_i_desc.bNrChannels = num_channels(audio_opts->p_chmask);
1256	as_in_type_i_desc.bSubframeSize = audio_opts->p_ssize;
1257	as_in_type_i_desc.bBitResolution = audio_opts->p_ssize * 8;
1258
1259	if (FUOUT_EN(audio_opts)) {
1260		__le16 *bma = (__le16 *)&out_feature_unit_desc->bmaControls[0];
1261		u32 control = 0;
1262
1263		if (audio_opts->c_mute_present)
1264			control |= UAC_FU_MUTE;
1265		if (audio_opts->c_volume_present)
1266			control |= UAC_FU_VOLUME;
1267		*bma = cpu_to_le16(control);
1268	}
1269	if (FUIN_EN(audio_opts)) {
1270		__le16 *bma = (__le16 *)&in_feature_unit_desc->bmaControls[0];
1271		u32 control = 0;
1272
1273		if (audio_opts->p_mute_present)
1274			control |= UAC_FU_MUTE;
1275		if (audio_opts->p_volume_present)
1276			control |= UAC_FU_VOLUME;
1277		*bma = cpu_to_le16(control);
1278	}
1279
1280	/* Set sample rates */
1281	for (i = 0, idx = 0; i < UAC_MAX_RATES; i++) {
1282		if (audio_opts->c_srates[i] == 0)
1283			break;
1284		memcpy(as_out_type_i_desc.tSamFreq[idx++],
1285				&audio_opts->c_srates[i], 3);
1286	}
1287	as_out_type_i_desc.bLength = UAC_FORMAT_TYPE_I_DISCRETE_DESC_SIZE(idx);
1288	as_out_type_i_desc.bSamFreqType = idx;
1289
1290	for (i = 0, idx = 0; i < UAC_MAX_RATES; i++) {
1291		if (audio_opts->p_srates[i] == 0)
1292			break;
1293		memcpy(as_in_type_i_desc.tSamFreq[idx++],
1294				&audio_opts->p_srates[i], 3);
1295	}
1296	as_in_type_i_desc.bLength = UAC_FORMAT_TYPE_I_DISCRETE_DESC_SIZE(idx);
1297	as_in_type_i_desc.bSamFreqType = idx;
1298	uac1->p_srate = audio_opts->p_srates[0];
1299	uac1->c_srate = audio_opts->c_srates[0];
1300
1301	/* allocate instance-specific interface IDs, and patch descriptors */
1302	status = usb_interface_id(c, f);
1303	if (status < 0)
1304		goto err_free_fu;
1305	ac_interface_desc.bInterfaceNumber = status;
1306	uac1->ac_intf = status;
1307	uac1->ac_alt = 0;
1308
1309	ba_iface_id = 0;
1310
1311	if (EPOUT_EN(audio_opts)) {
1312		status = usb_interface_id(c, f);
1313		if (status < 0)
1314			goto err_free_fu;
1315		as_out_interface_alt_0_desc.bInterfaceNumber = status;
1316		as_out_interface_alt_1_desc.bInterfaceNumber = status;
1317		ac_header_desc->baInterfaceNr[ba_iface_id++] = status;
1318		uac1->as_out_intf = status;
1319		uac1->as_out_alt = 0;
1320	}
1321
1322	if (EPIN_EN(audio_opts)) {
1323		status = usb_interface_id(c, f);
1324		if (status < 0)
1325			goto err_free_fu;
1326		as_in_interface_alt_0_desc.bInterfaceNumber = status;
1327		as_in_interface_alt_1_desc.bInterfaceNumber = status;
1328		ac_header_desc->baInterfaceNr[ba_iface_id++] = status;
1329		uac1->as_in_intf = status;
1330		uac1->as_in_alt = 0;
1331	}
1332
1333	audio->gadget = gadget;
1334
1335	status = -ENODEV;
1336
1337	ac_interface_desc.bNumEndpoints = 0;
1338
1339	/* allocate AC interrupt endpoint */
1340	if (FUOUT_EN(audio_opts) || FUIN_EN(audio_opts)) {
1341		ep = usb_ep_autoconfig(cdev->gadget, &ac_int_ep_desc);
1342		if (!ep)
1343			goto err_free_fu;
1344		uac1->int_ep = ep;
1345		uac1->int_ep->desc = &ac_int_ep_desc;
1346
1347		ac_interface_desc.bNumEndpoints = 1;
1348	}
1349
1350	/* allocate instance-specific endpoints */
1351	if (EPOUT_EN(audio_opts)) {
1352		ep = usb_ep_autoconfig(cdev->gadget, &as_out_ep_desc);
1353		if (!ep)
1354			goto err_free_fu;
1355		audio->out_ep = ep;
1356		audio->out_ep->desc = &as_out_ep_desc;
1357	}
1358
1359	if (EPIN_EN(audio_opts)) {
1360		ep = usb_ep_autoconfig(cdev->gadget, &as_in_ep_desc);
1361		if (!ep)
1362			goto err_free_fu;
1363		audio->in_ep = ep;
1364		audio->in_ep->desc = &as_in_ep_desc;
1365	}
1366
1367	setup_descriptor(audio_opts);
1368
1369	/* copy descriptors, and track endpoint copies */
1370	status = usb_assign_descriptors(f, f_audio_desc, f_audio_desc, NULL,
1371					NULL);
1372	if (status)
1373		goto err_free_fu;
1374
1375	audio->out_ep_maxpsize = le16_to_cpu(as_out_ep_desc.wMaxPacketSize);
1376	audio->in_ep_maxpsize = le16_to_cpu(as_in_ep_desc.wMaxPacketSize);
1377	audio->params.c_chmask = audio_opts->c_chmask;
1378	memcpy(audio->params.c_srates, audio_opts->c_srates,
1379			sizeof(audio->params.c_srates));
1380	audio->params.c_ssize = audio_opts->c_ssize;
1381	if (FUIN_EN(audio_opts)) {
1382		audio->params.p_fu.id = USB_IN_FU_ID;
1383		audio->params.p_fu.mute_present = audio_opts->p_mute_present;
1384		audio->params.p_fu.volume_present =
1385				audio_opts->p_volume_present;
1386		audio->params.p_fu.volume_min = audio_opts->p_volume_min;
1387		audio->params.p_fu.volume_max = audio_opts->p_volume_max;
1388		audio->params.p_fu.volume_res = audio_opts->p_volume_res;
1389	}
1390	audio->params.p_chmask = audio_opts->p_chmask;
1391	memcpy(audio->params.p_srates, audio_opts->p_srates,
1392			sizeof(audio->params.p_srates));
1393	audio->params.p_ssize = audio_opts->p_ssize;
1394	if (FUOUT_EN(audio_opts)) {
1395		audio->params.c_fu.id = USB_OUT_FU_ID;
1396		audio->params.c_fu.mute_present = audio_opts->c_mute_present;
1397		audio->params.c_fu.volume_present =
1398				audio_opts->c_volume_present;
1399		audio->params.c_fu.volume_min = audio_opts->c_volume_min;
1400		audio->params.c_fu.volume_max = audio_opts->c_volume_max;
1401		audio->params.c_fu.volume_res = audio_opts->c_volume_res;
1402	}
1403	audio->params.req_number = audio_opts->req_number;
1404	audio->params.fb_max = FBACK_FAST_MAX;
1405	if (FUOUT_EN(audio_opts) || FUIN_EN(audio_opts))
1406		audio->notify = audio_notify;
1407
1408	status = g_audio_setup(audio, "UAC1_PCM", "UAC1_Gadget");
1409	if (status)
1410		goto err_card_register;
1411
1412	return 0;
1413
1414err_card_register:
1415	usb_free_all_descriptors(f);
1416err_free_fu:
1417	kfree(out_feature_unit_desc);
1418	out_feature_unit_desc = NULL;
1419	kfree(in_feature_unit_desc);
1420	in_feature_unit_desc = NULL;
1421fail:
1422	kfree(ac_header_desc);
1423	ac_header_desc = NULL;
1424	return status;
1425}
1426
1427/*-------------------------------------------------------------------------*/
1428
1429static inline struct f_uac1_opts *to_f_uac1_opts(struct config_item *item)
1430{
1431	return container_of(to_config_group(item), struct f_uac1_opts,
1432			    func_inst.group);
1433}
1434
1435static void f_uac1_attr_release(struct config_item *item)
1436{
1437	struct f_uac1_opts *opts = to_f_uac1_opts(item);
1438
1439	usb_put_function_instance(&opts->func_inst);
1440}
1441
1442static struct configfs_item_operations f_uac1_item_ops = {
1443	.release	= f_uac1_attr_release,
1444};
1445
1446#define uac1_kstrtou32			kstrtou32
1447#define uac1_kstrtos16			kstrtos16
1448#define uac1_kstrtobool(s, base, res)	kstrtobool((s), (res))
1449
1450static const char *u32_fmt = "%u\n";
1451static const char *s16_fmt = "%hd\n";
1452static const char *bool_fmt = "%u\n";
1453
1454#define UAC1_ATTRIBUTE(type, name)					\
1455static ssize_t f_uac1_opts_##name##_show(				\
1456					  struct config_item *item,	\
1457					  char *page)			\
1458{									\
1459	struct f_uac1_opts *opts = to_f_uac1_opts(item);		\
1460	int result;							\
1461									\
1462	mutex_lock(&opts->lock);					\
1463	result = sprintf(page, type##_fmt, opts->name);			\
1464	mutex_unlock(&opts->lock);					\
1465									\
1466	return result;							\
1467}									\
1468									\
1469static ssize_t f_uac1_opts_##name##_store(				\
1470					  struct config_item *item,	\
1471					  const char *page, size_t len)	\
1472{									\
1473	struct f_uac1_opts *opts = to_f_uac1_opts(item);		\
1474	int ret;							\
1475	type num;							\
1476									\
1477	mutex_lock(&opts->lock);					\
1478	if (opts->refcnt) {						\
1479		ret = -EBUSY;						\
1480		goto end;						\
1481	}								\
1482									\
1483	ret = uac1_kstrto##type(page, 0, &num);				\
1484	if (ret)							\
1485		goto end;						\
1486									\
1487	opts->name = num;						\
1488	ret = len;							\
1489									\
1490end:									\
1491	mutex_unlock(&opts->lock);					\
1492	return ret;							\
1493}									\
1494									\
1495CONFIGFS_ATTR(f_uac1_opts_, name)
1496
1497#define UAC1_RATE_ATTRIBUTE(name)					\
1498static ssize_t f_uac1_opts_##name##_show(struct config_item *item,	\
1499					 char *page)			\
1500{									\
1501	struct f_uac1_opts *opts = to_f_uac1_opts(item);		\
1502	int result = 0;							\
1503	int i;								\
1504									\
1505	mutex_lock(&opts->lock);					\
1506	page[0] = '\0';							\
1507	for (i = 0; i < UAC_MAX_RATES; i++) {				\
1508		if (opts->name##s[i] == 0)				\
1509			break;						\
1510		result += sprintf(page + strlen(page), "%u,",		\
1511				opts->name##s[i]);			\
1512	}								\
1513	if (strlen(page) > 0)						\
1514		page[strlen(page) - 1] = '\n';				\
1515	mutex_unlock(&opts->lock);					\
1516									\
1517	return result;							\
1518}									\
1519									\
1520static ssize_t f_uac1_opts_##name##_store(struct config_item *item,	\
1521					  const char *page, size_t len)	\
1522{									\
1523	struct f_uac1_opts *opts = to_f_uac1_opts(item);		\
1524	char *split_page = NULL;					\
1525	int ret = -EINVAL;						\
1526	char *token;							\
1527	u32 num;							\
1528	int i;								\
1529									\
1530	mutex_lock(&opts->lock);					\
1531	if (opts->refcnt) {						\
1532		ret = -EBUSY;						\
1533		goto end;						\
1534	}								\
1535									\
1536	i = 0;								\
1537	memset(opts->name##s, 0x00, sizeof(opts->name##s));		\
1538	split_page = kstrdup(page, GFP_KERNEL);				\
1539	while ((token = strsep(&split_page, ",")) != NULL) {		\
1540		ret = kstrtou32(token, 0, &num);			\
1541		if (ret)						\
1542			goto end;					\
1543									\
1544		opts->name##s[i++] = num;				\
1545		ret = len;						\
1546	};								\
1547									\
1548end:									\
1549	kfree(split_page);						\
1550	mutex_unlock(&opts->lock);					\
1551	return ret;							\
1552}									\
1553									\
1554CONFIGFS_ATTR(f_uac1_opts_, name)
1555
1556#define UAC1_ATTRIBUTE_STRING(name)					\
1557static ssize_t f_uac1_opts_##name##_show(struct config_item *item,	\
1558					 char *page)			\
1559{									\
1560	struct f_uac1_opts *opts = to_f_uac1_opts(item);		\
1561	int result;							\
1562									\
1563	mutex_lock(&opts->lock);					\
1564	result = snprintf(page, sizeof(opts->name), "%s", opts->name);	\
1565	mutex_unlock(&opts->lock);					\
1566									\
1567	return result;							\
1568}									\
1569									\
1570static ssize_t f_uac1_opts_##name##_store(struct config_item *item,	\
1571					  const char *page, size_t len)	\
1572{									\
1573	struct f_uac1_opts *opts = to_f_uac1_opts(item);		\
1574	int ret = 0;							\
1575									\
1576	mutex_lock(&opts->lock);					\
1577	if (opts->refcnt) {						\
1578		ret = -EBUSY;						\
1579		goto end;						\
1580	}								\
1581									\
1582	ret = snprintf(opts->name, min(sizeof(opts->name), len),	\
1583			"%s", page);					\
1584									\
1585end:									\
1586	mutex_unlock(&opts->lock);					\
1587	return ret;							\
1588}									\
1589									\
1590CONFIGFS_ATTR(f_uac1_opts_, name)
1591
1592UAC1_ATTRIBUTE(u32, c_chmask);
1593UAC1_RATE_ATTRIBUTE(c_srate);
1594UAC1_ATTRIBUTE(u32, c_ssize);
1595UAC1_ATTRIBUTE(u32, p_chmask);
1596UAC1_RATE_ATTRIBUTE(p_srate);
1597UAC1_ATTRIBUTE(u32, p_ssize);
1598UAC1_ATTRIBUTE(u32, req_number);
1599
1600UAC1_ATTRIBUTE(bool, p_mute_present);
1601UAC1_ATTRIBUTE(bool, p_volume_present);
1602UAC1_ATTRIBUTE(s16, p_volume_min);
1603UAC1_ATTRIBUTE(s16, p_volume_max);
1604UAC1_ATTRIBUTE(s16, p_volume_res);
1605
1606UAC1_ATTRIBUTE(bool, c_mute_present);
1607UAC1_ATTRIBUTE(bool, c_volume_present);
1608UAC1_ATTRIBUTE(s16, c_volume_min);
1609UAC1_ATTRIBUTE(s16, c_volume_max);
1610UAC1_ATTRIBUTE(s16, c_volume_res);
1611UAC1_ATTRIBUTE_STRING(function_name);
1612
1613static struct configfs_attribute *f_uac1_attrs[] = {
1614	&f_uac1_opts_attr_c_chmask,
1615	&f_uac1_opts_attr_c_srate,
1616	&f_uac1_opts_attr_c_ssize,
1617	&f_uac1_opts_attr_p_chmask,
1618	&f_uac1_opts_attr_p_srate,
1619	&f_uac1_opts_attr_p_ssize,
1620	&f_uac1_opts_attr_req_number,
1621
1622	&f_uac1_opts_attr_p_mute_present,
1623	&f_uac1_opts_attr_p_volume_present,
1624	&f_uac1_opts_attr_p_volume_min,
1625	&f_uac1_opts_attr_p_volume_max,
1626	&f_uac1_opts_attr_p_volume_res,
1627
1628	&f_uac1_opts_attr_c_mute_present,
1629	&f_uac1_opts_attr_c_volume_present,
1630	&f_uac1_opts_attr_c_volume_min,
1631	&f_uac1_opts_attr_c_volume_max,
1632	&f_uac1_opts_attr_c_volume_res,
1633
1634	&f_uac1_opts_attr_function_name,
1635
1636	NULL,
1637};
1638
1639static const struct config_item_type f_uac1_func_type = {
1640	.ct_item_ops	= &f_uac1_item_ops,
1641	.ct_attrs	= f_uac1_attrs,
1642	.ct_owner	= THIS_MODULE,
1643};
1644
1645static void f_audio_free_inst(struct usb_function_instance *f)
1646{
1647	struct f_uac1_opts *opts;
1648
1649	opts = container_of(f, struct f_uac1_opts, func_inst);
1650	kfree(opts);
1651}
1652
1653static struct usb_function_instance *f_audio_alloc_inst(void)
1654{
1655	struct f_uac1_opts *opts;
1656
1657	opts = kzalloc(sizeof(*opts), GFP_KERNEL);
1658	if (!opts)
1659		return ERR_PTR(-ENOMEM);
1660
1661	mutex_init(&opts->lock);
1662	opts->func_inst.free_func_inst = f_audio_free_inst;
1663
1664	config_group_init_type_name(&opts->func_inst.group, "",
1665				    &f_uac1_func_type);
1666
1667	opts->c_chmask = UAC1_DEF_CCHMASK;
1668	opts->c_srates[0] = UAC1_DEF_CSRATE;
1669	opts->c_ssize = UAC1_DEF_CSSIZE;
1670	opts->p_chmask = UAC1_DEF_PCHMASK;
1671	opts->p_srates[0] = UAC1_DEF_PSRATE;
1672	opts->p_ssize = UAC1_DEF_PSSIZE;
1673
1674	opts->p_mute_present = UAC1_DEF_MUTE_PRESENT;
1675	opts->p_volume_present = UAC1_DEF_VOLUME_PRESENT;
1676	opts->p_volume_min = UAC1_DEF_MIN_DB;
1677	opts->p_volume_max = UAC1_DEF_MAX_DB;
1678	opts->p_volume_res = UAC1_DEF_RES_DB;
1679
1680	opts->c_mute_present = UAC1_DEF_MUTE_PRESENT;
1681	opts->c_volume_present = UAC1_DEF_VOLUME_PRESENT;
1682	opts->c_volume_min = UAC1_DEF_MIN_DB;
1683	opts->c_volume_max = UAC1_DEF_MAX_DB;
1684	opts->c_volume_res = UAC1_DEF_RES_DB;
1685
1686	opts->req_number = UAC1_DEF_REQ_NUM;
1687
1688	snprintf(opts->function_name, sizeof(opts->function_name), "AC Interface");
1689
1690	return &opts->func_inst;
1691}
1692
1693static void f_audio_free(struct usb_function *f)
1694{
1695	struct g_audio *audio;
1696	struct f_uac1_opts *opts;
1697
1698	audio = func_to_g_audio(f);
1699	opts = container_of(f->fi, struct f_uac1_opts, func_inst);
1700	kfree(audio);
1701	mutex_lock(&opts->lock);
1702	--opts->refcnt;
1703	mutex_unlock(&opts->lock);
1704}
1705
1706static void f_audio_unbind(struct usb_configuration *c, struct usb_function *f)
1707{
1708	struct g_audio *audio = func_to_g_audio(f);
1709
1710	g_audio_cleanup(audio);
1711	usb_free_all_descriptors(f);
1712
1713	kfree(out_feature_unit_desc);
1714	out_feature_unit_desc = NULL;
1715	kfree(in_feature_unit_desc);
1716	in_feature_unit_desc = NULL;
1717
1718	kfree(ac_header_desc);
1719	ac_header_desc = NULL;
1720
1721	audio->gadget = NULL;
1722}
1723
1724static struct usb_function *f_audio_alloc(struct usb_function_instance *fi)
1725{
1726	struct f_uac1 *uac1;
1727	struct f_uac1_opts *opts;
1728
1729	/* allocate and initialize one new instance */
1730	uac1 = kzalloc(sizeof(*uac1), GFP_KERNEL);
1731	if (!uac1)
1732		return ERR_PTR(-ENOMEM);
1733
1734	opts = container_of(fi, struct f_uac1_opts, func_inst);
1735	mutex_lock(&opts->lock);
1736	++opts->refcnt;
1737	mutex_unlock(&opts->lock);
1738
1739	uac1->g_audio.func.name = "uac1_func";
1740	uac1->g_audio.func.bind = f_audio_bind;
1741	uac1->g_audio.func.unbind = f_audio_unbind;
1742	uac1->g_audio.func.set_alt = f_audio_set_alt;
1743	uac1->g_audio.func.get_alt = f_audio_get_alt;
1744	uac1->g_audio.func.setup = f_audio_setup;
1745	uac1->g_audio.func.disable = f_audio_disable;
1746	uac1->g_audio.func.suspend = f_audio_suspend;
1747	uac1->g_audio.func.free_func = f_audio_free;
1748
1749	return &uac1->g_audio.func;
1750}
1751
1752DECLARE_USB_FUNCTION_INIT(uac1, f_audio_alloc_inst, f_audio_alloc);
1753MODULE_LICENSE("GPL");
1754MODULE_AUTHOR("Ruslan Bilovol");
v5.4
  1// SPDX-License-Identifier: GPL-2.0+
  2/*
  3 * f_uac1.c -- USB Audio Class 1.0 Function (using u_audio API)
  4 *
  5 * Copyright (C) 2016 Ruslan Bilovol <ruslan.bilovol@gmail.com>
 
  6 *
  7 * This driver doesn't expect any real Audio codec to be present
  8 * on the device - the audio streams are simply sinked to and
  9 * sourced from a virtual ALSA sound card created.
 10 *
 11 * This file is based on f_uac1.c which is
 12 *   Copyright (C) 2008 Bryan Wu <cooloney@kernel.org>
 13 *   Copyright (C) 2008 Analog Devices, Inc
 14 */
 15
 16#include <linux/usb/audio.h>
 17#include <linux/module.h>
 18
 19#include "u_audio.h"
 20#include "u_uac1.h"
 21
 
 
 
 
 
 
 
 
 
 
 
 
 
 22struct f_uac1 {
 23	struct g_audio g_audio;
 24	u8 ac_intf, as_in_intf, as_out_intf;
 25	u8 ac_alt, as_in_alt, as_out_alt;	/* needed for get_alt() */
 
 
 
 
 
 
 
 
 
 26};
 27
 28static inline struct f_uac1 *func_to_uac1(struct usb_function *f)
 29{
 30	return container_of(f, struct f_uac1, g_audio.func);
 31}
 32
 
 
 
 
 
 33/*
 34 * DESCRIPTORS ... most are static, but strings and full
 35 * configuration descriptors are built on demand.
 36 */
 37
 38/*
 39 * We have three interfaces - one AudioControl and two AudioStreaming
 40 *
 41 * The driver implements a simple UAC_1 topology.
 42 * USB-OUT -> IT_1 -> OT_2 -> ALSA_Capture
 43 * ALSA_Playback -> IT_3 -> OT_4 -> USB-IN
 44 */
 45#define F_AUDIO_AC_INTERFACE		0
 46#define F_AUDIO_AS_OUT_INTERFACE	1
 47#define F_AUDIO_AS_IN_INTERFACE		2
 48/* Number of streaming interfaces */
 49#define F_AUDIO_NUM_INTERFACES		2
 50
 51/* B.3.1  Standard AC Interface Descriptor */
 52static struct usb_interface_descriptor ac_interface_desc = {
 53	.bLength =		USB_DT_INTERFACE_SIZE,
 54	.bDescriptorType =	USB_DT_INTERFACE,
 55	.bNumEndpoints =	0,
 56	.bInterfaceClass =	USB_CLASS_AUDIO,
 57	.bInterfaceSubClass =	USB_SUBCLASS_AUDIOCONTROL,
 58};
 59
 60/*
 61 * The number of AudioStreaming and MIDIStreaming interfaces
 62 * in the Audio Interface Collection
 63 */
 64DECLARE_UAC_AC_HEADER_DESCRIPTOR(2);
 65
 66#define UAC_DT_AC_HEADER_LENGTH	UAC_DT_AC_HEADER_SIZE(F_AUDIO_NUM_INTERFACES)
 67/* 2 input terminals and 2 output terminals */
 68#define UAC_DT_TOTAL_LENGTH (UAC_DT_AC_HEADER_LENGTH \
 69	+ 2*UAC_DT_INPUT_TERMINAL_SIZE + 2*UAC_DT_OUTPUT_TERMINAL_SIZE)
 70/* B.3.2  Class-Specific AC Interface Descriptor */
 71static struct uac1_ac_header_descriptor_2 ac_header_desc = {
 72	.bLength =		UAC_DT_AC_HEADER_LENGTH,
 73	.bDescriptorType =	USB_DT_CS_INTERFACE,
 74	.bDescriptorSubtype =	UAC_HEADER,
 75	.bcdADC =		cpu_to_le16(0x0100),
 76	.wTotalLength =		cpu_to_le16(UAC_DT_TOTAL_LENGTH),
 77	.bInCollection =	F_AUDIO_NUM_INTERFACES,
 78	.baInterfaceNr = {
 79	/* Interface number of the AudioStream interfaces */
 80		[0] =		1,
 81		[1] =		2,
 82	}
 83};
 84
 85#define USB_OUT_IT_ID	1
 86static struct uac_input_terminal_descriptor usb_out_it_desc = {
 87	.bLength =		UAC_DT_INPUT_TERMINAL_SIZE,
 88	.bDescriptorType =	USB_DT_CS_INTERFACE,
 89	.bDescriptorSubtype =	UAC_INPUT_TERMINAL,
 90	.bTerminalID =		USB_OUT_IT_ID,
 91	.wTerminalType =	cpu_to_le16(UAC_TERMINAL_STREAMING),
 92	.bAssocTerminal =	0,
 93	.wChannelConfig =	cpu_to_le16(0x3),
 94};
 95
 96#define IO_OUT_OT_ID	2
 97static struct uac1_output_terminal_descriptor io_out_ot_desc = {
 98	.bLength		= UAC_DT_OUTPUT_TERMINAL_SIZE,
 99	.bDescriptorType	= USB_DT_CS_INTERFACE,
100	.bDescriptorSubtype	= UAC_OUTPUT_TERMINAL,
101	.bTerminalID		= IO_OUT_OT_ID,
102	.wTerminalType		= cpu_to_le16(UAC_OUTPUT_TERMINAL_SPEAKER),
103	.bAssocTerminal		= 0,
104	.bSourceID		= USB_OUT_IT_ID,
105};
106
107#define IO_IN_IT_ID	3
108static struct uac_input_terminal_descriptor io_in_it_desc = {
109	.bLength		= UAC_DT_INPUT_TERMINAL_SIZE,
110	.bDescriptorType	= USB_DT_CS_INTERFACE,
111	.bDescriptorSubtype	= UAC_INPUT_TERMINAL,
112	.bTerminalID		= IO_IN_IT_ID,
113	.wTerminalType		= cpu_to_le16(UAC_INPUT_TERMINAL_MICROPHONE),
114	.bAssocTerminal		= 0,
115	.wChannelConfig		= cpu_to_le16(0x3),
116};
117
118#define USB_IN_OT_ID	4
119static struct uac1_output_terminal_descriptor usb_in_ot_desc = {
120	.bLength =		UAC_DT_OUTPUT_TERMINAL_SIZE,
121	.bDescriptorType =	USB_DT_CS_INTERFACE,
122	.bDescriptorSubtype =	UAC_OUTPUT_TERMINAL,
123	.bTerminalID =		USB_IN_OT_ID,
124	.wTerminalType =	cpu_to_le16(UAC_TERMINAL_STREAMING),
125	.bAssocTerminal =	0,
126	.bSourceID =		IO_IN_IT_ID,
 
 
 
 
 
 
 
 
 
 
 
 
 
127};
128
129/* B.4.1  Standard AS Interface Descriptor */
130static struct usb_interface_descriptor as_out_interface_alt_0_desc = {
131	.bLength =		USB_DT_INTERFACE_SIZE,
132	.bDescriptorType =	USB_DT_INTERFACE,
133	.bAlternateSetting =	0,
134	.bNumEndpoints =	0,
135	.bInterfaceClass =	USB_CLASS_AUDIO,
136	.bInterfaceSubClass =	USB_SUBCLASS_AUDIOSTREAMING,
137};
138
139static struct usb_interface_descriptor as_out_interface_alt_1_desc = {
140	.bLength =		USB_DT_INTERFACE_SIZE,
141	.bDescriptorType =	USB_DT_INTERFACE,
142	.bAlternateSetting =	1,
143	.bNumEndpoints =	1,
144	.bInterfaceClass =	USB_CLASS_AUDIO,
145	.bInterfaceSubClass =	USB_SUBCLASS_AUDIOSTREAMING,
146};
147
148static struct usb_interface_descriptor as_in_interface_alt_0_desc = {
149	.bLength =		USB_DT_INTERFACE_SIZE,
150	.bDescriptorType =	USB_DT_INTERFACE,
151	.bAlternateSetting =	0,
152	.bNumEndpoints =	0,
153	.bInterfaceClass =	USB_CLASS_AUDIO,
154	.bInterfaceSubClass =	USB_SUBCLASS_AUDIOSTREAMING,
155};
156
157static struct usb_interface_descriptor as_in_interface_alt_1_desc = {
158	.bLength =		USB_DT_INTERFACE_SIZE,
159	.bDescriptorType =	USB_DT_INTERFACE,
160	.bAlternateSetting =	1,
161	.bNumEndpoints =	1,
162	.bInterfaceClass =	USB_CLASS_AUDIO,
163	.bInterfaceSubClass =	USB_SUBCLASS_AUDIOSTREAMING,
164};
165
166/* B.4.2  Class-Specific AS Interface Descriptor */
167static struct uac1_as_header_descriptor as_out_header_desc = {
168	.bLength =		UAC_DT_AS_HEADER_SIZE,
169	.bDescriptorType =	USB_DT_CS_INTERFACE,
170	.bDescriptorSubtype =	UAC_AS_GENERAL,
171	.bTerminalLink =	USB_OUT_IT_ID,
172	.bDelay =		1,
173	.wFormatTag =		cpu_to_le16(UAC_FORMAT_TYPE_I_PCM),
174};
175
176static struct uac1_as_header_descriptor as_in_header_desc = {
177	.bLength =		UAC_DT_AS_HEADER_SIZE,
178	.bDescriptorType =	USB_DT_CS_INTERFACE,
179	.bDescriptorSubtype =	UAC_AS_GENERAL,
180	.bTerminalLink =	USB_IN_OT_ID,
181	.bDelay =		1,
182	.wFormatTag =		cpu_to_le16(UAC_FORMAT_TYPE_I_PCM),
183};
184
185DECLARE_UAC_FORMAT_TYPE_I_DISCRETE_DESC(1);
 
 
186
187static struct uac_format_type_i_discrete_descriptor_1 as_out_type_i_desc = {
188	.bLength =		UAC_FORMAT_TYPE_I_DISCRETE_DESC_SIZE(1),
189	.bDescriptorType =	USB_DT_CS_INTERFACE,
190	.bDescriptorSubtype =	UAC_FORMAT_TYPE,
191	.bFormatType =		UAC_FORMAT_TYPE_I,
192	.bSubframeSize =	2,
193	.bBitResolution =	16,
194	.bSamFreqType =		1,
195};
196
197/* Standard ISO OUT Endpoint Descriptor */
198static struct usb_endpoint_descriptor as_out_ep_desc  = {
199	.bLength =		USB_DT_ENDPOINT_AUDIO_SIZE,
200	.bDescriptorType =	USB_DT_ENDPOINT,
201	.bEndpointAddress =	USB_DIR_OUT,
202	.bmAttributes =		USB_ENDPOINT_SYNC_ADAPTIVE
203				| USB_ENDPOINT_XFER_ISOC,
204	.wMaxPacketSize	=	cpu_to_le16(UAC1_OUT_EP_MAX_PACKET_SIZE),
205	.bInterval =		4,
206};
207
208/* Class-specific AS ISO OUT Endpoint Descriptor */
209static struct uac_iso_endpoint_descriptor as_iso_out_desc = {
210	.bLength =		UAC_ISO_ENDPOINT_DESC_SIZE,
211	.bDescriptorType =	USB_DT_CS_ENDPOINT,
212	.bDescriptorSubtype =	UAC_EP_GENERAL,
213	.bmAttributes =		1,
214	.bLockDelayUnits =	1,
215	.wLockDelay =		cpu_to_le16(1),
216};
217
218static struct uac_format_type_i_discrete_descriptor_1 as_in_type_i_desc = {
219	.bLength =		UAC_FORMAT_TYPE_I_DISCRETE_DESC_SIZE(1),
220	.bDescriptorType =	USB_DT_CS_INTERFACE,
221	.bDescriptorSubtype =	UAC_FORMAT_TYPE,
222	.bFormatType =		UAC_FORMAT_TYPE_I,
223	.bSubframeSize =	2,
224	.bBitResolution =	16,
225	.bSamFreqType =		1,
226};
227
228/* Standard ISO OUT Endpoint Descriptor */
229static struct usb_endpoint_descriptor as_in_ep_desc  = {
230	.bLength =		USB_DT_ENDPOINT_AUDIO_SIZE,
231	.bDescriptorType =	USB_DT_ENDPOINT,
232	.bEndpointAddress =	USB_DIR_IN,
233	.bmAttributes =		USB_ENDPOINT_SYNC_ASYNC
234				| USB_ENDPOINT_XFER_ISOC,
235	.wMaxPacketSize	=	cpu_to_le16(UAC1_OUT_EP_MAX_PACKET_SIZE),
236	.bInterval =		4,
237};
238
239/* Class-specific AS ISO OUT Endpoint Descriptor */
240static struct uac_iso_endpoint_descriptor as_iso_in_desc = {
241	.bLength =		UAC_ISO_ENDPOINT_DESC_SIZE,
242	.bDescriptorType =	USB_DT_CS_ENDPOINT,
243	.bDescriptorSubtype =	UAC_EP_GENERAL,
244	.bmAttributes =		1,
245	.bLockDelayUnits =	0,
246	.wLockDelay =		0,
247};
248
249static struct usb_descriptor_header *f_audio_desc[] = {
250	(struct usb_descriptor_header *)&ac_interface_desc,
251	(struct usb_descriptor_header *)&ac_header_desc,
252
253	(struct usb_descriptor_header *)&usb_out_it_desc,
254	(struct usb_descriptor_header *)&io_out_ot_desc,
 
 
255	(struct usb_descriptor_header *)&io_in_it_desc,
256	(struct usb_descriptor_header *)&usb_in_ot_desc,
 
 
 
257
258	(struct usb_descriptor_header *)&as_out_interface_alt_0_desc,
259	(struct usb_descriptor_header *)&as_out_interface_alt_1_desc,
260	(struct usb_descriptor_header *)&as_out_header_desc,
261
262	(struct usb_descriptor_header *)&as_out_type_i_desc,
263
264	(struct usb_descriptor_header *)&as_out_ep_desc,
265	(struct usb_descriptor_header *)&as_iso_out_desc,
266
267	(struct usb_descriptor_header *)&as_in_interface_alt_0_desc,
268	(struct usb_descriptor_header *)&as_in_interface_alt_1_desc,
269	(struct usb_descriptor_header *)&as_in_header_desc,
270
271	(struct usb_descriptor_header *)&as_in_type_i_desc,
272
273	(struct usb_descriptor_header *)&as_in_ep_desc,
274	(struct usb_descriptor_header *)&as_iso_in_desc,
275	NULL,
276};
277
278enum {
279	STR_AC_IF,
280	STR_USB_OUT_IT,
281	STR_USB_OUT_IT_CH_NAMES,
282	STR_IO_OUT_OT,
283	STR_IO_IN_IT,
284	STR_IO_IN_IT_CH_NAMES,
285	STR_USB_IN_OT,
 
 
286	STR_AS_OUT_IF_ALT0,
287	STR_AS_OUT_IF_ALT1,
288	STR_AS_IN_IF_ALT0,
289	STR_AS_IN_IF_ALT1,
290};
291
292static struct usb_string strings_uac1[] = {
293	[STR_AC_IF].s = "AC Interface",
294	[STR_USB_OUT_IT].s = "Playback Input terminal",
295	[STR_USB_OUT_IT_CH_NAMES].s = "Playback Channels",
296	[STR_IO_OUT_OT].s = "Playback Output terminal",
297	[STR_IO_IN_IT].s = "Capture Input terminal",
298	[STR_IO_IN_IT_CH_NAMES].s = "Capture Channels",
299	[STR_USB_IN_OT].s = "Capture Output terminal",
 
 
300	[STR_AS_OUT_IF_ALT0].s = "Playback Inactive",
301	[STR_AS_OUT_IF_ALT1].s = "Playback Active",
302	[STR_AS_IN_IF_ALT0].s = "Capture Inactive",
303	[STR_AS_IN_IF_ALT1].s = "Capture Active",
304	{ },
305};
306
307static struct usb_gadget_strings str_uac1 = {
308	.language = 0x0409,	/* en-us */
309	.strings = strings_uac1,
310};
311
312static struct usb_gadget_strings *uac1_strings[] = {
313	&str_uac1,
314	NULL,
315};
316
317/*
318 * This function is an ALSA sound card following USB Audio Class Spec 1.0.
319 */
320
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
321static int audio_set_endpoint_req(struct usb_function *f,
322		const struct usb_ctrlrequest *ctrl)
323{
324	struct usb_composite_dev *cdev = f->config->cdev;
 
 
325	int			value = -EOPNOTSUPP;
326	u16			ep = le16_to_cpu(ctrl->wIndex);
327	u16			len = le16_to_cpu(ctrl->wLength);
328	u16			w_value = le16_to_cpu(ctrl->wValue);
 
329
330	DBG(cdev, "bRequest 0x%x, w_value 0x%04x, len %d, endpoint %d\n",
331			ctrl->bRequest, w_value, len, ep);
332
333	switch (ctrl->bRequest) {
334	case UAC_SET_CUR:
 
 
 
 
 
335		value = len;
336		break;
 
337
338	case UAC_SET_MIN:
339		break;
340
341	case UAC_SET_MAX:
342		break;
343
344	case UAC_SET_RES:
345		break;
346
347	case UAC_SET_MEM:
348		break;
349
350	default:
351		break;
352	}
353
354	return value;
355}
356
357static int audio_get_endpoint_req(struct usb_function *f,
358		const struct usb_ctrlrequest *ctrl)
359{
360	struct usb_composite_dev *cdev = f->config->cdev;
 
 
 
361	int value = -EOPNOTSUPP;
362	u8 ep = ((le16_to_cpu(ctrl->wIndex) >> 8) & 0xFF);
363	u16 len = le16_to_cpu(ctrl->wLength);
364	u16 w_value = le16_to_cpu(ctrl->wValue);
 
 
365
366	DBG(cdev, "bRequest 0x%x, w_value 0x%04x, len %d, endpoint %d\n",
367			ctrl->bRequest, w_value, len, ep);
368
369	switch (ctrl->bRequest) {
370	case UAC_GET_CUR:
 
 
 
 
 
 
 
 
 
 
 
 
371	case UAC_GET_MIN:
372	case UAC_GET_MAX:
373	case UAC_GET_RES:
374		value = len;
375		break;
376	case UAC_GET_MEM:
377		break;
378	default:
379		break;
380	}
381
382	return value;
383}
384
385static int
386f_audio_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
387{
388	struct usb_composite_dev *cdev = f->config->cdev;
389	struct usb_request	*req = cdev->req;
390	int			value = -EOPNOTSUPP;
391	u16			w_index = le16_to_cpu(ctrl->wIndex);
392	u16			w_value = le16_to_cpu(ctrl->wValue);
393	u16			w_length = le16_to_cpu(ctrl->wLength);
394
395	/* composite driver infrastructure handles everything; interface
396	 * activation uses set_alt().
397	 */
398	switch (ctrl->bRequestType) {
399	case USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_ENDPOINT:
400		value = audio_set_endpoint_req(f, ctrl);
401		break;
402
403	case USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT:
404		value = audio_get_endpoint_req(f, ctrl);
405		break;
406
 
 
 
 
 
 
407	default:
408		ERROR(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
409			ctrl->bRequestType, ctrl->bRequest,
410			w_value, w_index, w_length);
411	}
412
413	/* respond with data transfer or status phase? */
414	if (value >= 0) {
415		DBG(cdev, "audio req%02x.%02x v%04x i%04x l%d\n",
416			ctrl->bRequestType, ctrl->bRequest,
417			w_value, w_index, w_length);
418		req->zero = 0;
419		req->length = value;
420		value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
421		if (value < 0)
422			ERROR(cdev, "audio response on err %d\n", value);
423	}
424
425	/* device either stalls (value < 0) or reports success */
426	return value;
427}
428
429static int f_audio_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
430{
431	struct usb_composite_dev *cdev = f->config->cdev;
432	struct usb_gadget *gadget = cdev->gadget;
433	struct device *dev = &gadget->dev;
 
434	struct f_uac1 *uac1 = func_to_uac1(f);
435	int ret = 0;
436
437	/* No i/f has more than 2 alt settings */
438	if (alt > 1) {
439		dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
440		return -EINVAL;
441	}
442
443	if (intf == uac1->ac_intf) {
444		/* Control I/f has only 1 AltSetting - 0 */
445		if (alt) {
446			dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
447			return -EINVAL;
448		}
 
 
 
 
 
 
 
 
449		return 0;
450	}
451
452	if (intf == uac1->as_out_intf) {
453		uac1->as_out_alt = alt;
454
455		if (alt)
456			ret = u_audio_start_capture(&uac1->g_audio);
457		else
458			u_audio_stop_capture(&uac1->g_audio);
459	} else if (intf == uac1->as_in_intf) {
460		uac1->as_in_alt = alt;
461
462		if (alt)
463			ret = u_audio_start_playback(&uac1->g_audio);
464		else
465			u_audio_stop_playback(&uac1->g_audio);
466	} else {
467		dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
468		return -EINVAL;
469	}
470
471	return ret;
472}
473
474static int f_audio_get_alt(struct usb_function *f, unsigned intf)
475{
476	struct usb_composite_dev *cdev = f->config->cdev;
477	struct usb_gadget *gadget = cdev->gadget;
478	struct device *dev = &gadget->dev;
479	struct f_uac1 *uac1 = func_to_uac1(f);
480
481	if (intf == uac1->ac_intf)
482		return uac1->ac_alt;
483	else if (intf == uac1->as_out_intf)
484		return uac1->as_out_alt;
485	else if (intf == uac1->as_in_intf)
486		return uac1->as_in_alt;
487	else
488		dev_err(dev, "%s:%d Invalid Interface %d!\n",
489			__func__, __LINE__, intf);
490
491	return -EINVAL;
492}
493
494
495static void f_audio_disable(struct usb_function *f)
496{
497	struct f_uac1 *uac1 = func_to_uac1(f);
498
499	uac1->as_out_alt = 0;
500	uac1->as_in_alt = 0;
501
 
502	u_audio_stop_capture(&uac1->g_audio);
 
 
 
 
 
 
 
 
 
 
503}
504
505/*-------------------------------------------------------------------------*/
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
506
507/* audio function driver setup/binding */
508static int f_audio_bind(struct usb_configuration *c, struct usb_function *f)
509{
510	struct usb_composite_dev	*cdev = c->cdev;
511	struct usb_gadget		*gadget = cdev->gadget;
 
512	struct f_uac1			*uac1 = func_to_uac1(f);
513	struct g_audio			*audio = func_to_g_audio(f);
514	struct f_uac1_opts		*audio_opts;
515	struct usb_ep			*ep = NULL;
516	struct usb_string		*us;
517	u8				*sam_freq;
518	int				rate;
519	int				status;
 
 
 
 
 
520
521	audio_opts = container_of(f->fi, struct f_uac1_opts, func_inst);
522
 
 
523	us = usb_gstrings_attach(cdev, uac1_strings, ARRAY_SIZE(strings_uac1));
524	if (IS_ERR(us))
525		return PTR_ERR(us);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
526	ac_interface_desc.iInterface = us[STR_AC_IF].id;
527	usb_out_it_desc.iTerminal = us[STR_USB_OUT_IT].id;
528	usb_out_it_desc.iChannelNames = us[STR_USB_OUT_IT_CH_NAMES].id;
529	io_out_ot_desc.iTerminal = us[STR_IO_OUT_OT].id;
530	as_out_interface_alt_0_desc.iInterface = us[STR_AS_OUT_IF_ALT0].id;
531	as_out_interface_alt_1_desc.iInterface = us[STR_AS_OUT_IF_ALT1].id;
532	io_in_it_desc.iTerminal = us[STR_IO_IN_IT].id;
533	io_in_it_desc.iChannelNames = us[STR_IO_IN_IT_CH_NAMES].id;
534	usb_in_ot_desc.iTerminal = us[STR_USB_IN_OT].id;
535	as_in_interface_alt_0_desc.iInterface = us[STR_AS_IN_IF_ALT0].id;
536	as_in_interface_alt_1_desc.iInterface = us[STR_AS_IN_IF_ALT1].id;
537
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
538	/* Set channel numbers */
539	usb_out_it_desc.bNrChannels = num_channels(audio_opts->c_chmask);
540	usb_out_it_desc.wChannelConfig = cpu_to_le16(audio_opts->c_chmask);
541	as_out_type_i_desc.bNrChannels = num_channels(audio_opts->c_chmask);
542	as_out_type_i_desc.bSubframeSize = audio_opts->c_ssize;
543	as_out_type_i_desc.bBitResolution = audio_opts->c_ssize * 8;
544	io_in_it_desc.bNrChannels = num_channels(audio_opts->p_chmask);
545	io_in_it_desc.wChannelConfig = cpu_to_le16(audio_opts->p_chmask);
546	as_in_type_i_desc.bNrChannels = num_channels(audio_opts->p_chmask);
547	as_in_type_i_desc.bSubframeSize = audio_opts->p_ssize;
548	as_in_type_i_desc.bBitResolution = audio_opts->p_ssize * 8;
549
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
550	/* Set sample rates */
551	rate = audio_opts->c_srate;
552	sam_freq = as_out_type_i_desc.tSamFreq[0];
553	memcpy(sam_freq, &rate, 3);
554	rate = audio_opts->p_srate;
555	sam_freq = as_in_type_i_desc.tSamFreq[0];
556	memcpy(sam_freq, &rate, 3);
 
 
 
 
 
 
 
 
 
 
 
 
 
557
558	/* allocate instance-specific interface IDs, and patch descriptors */
559	status = usb_interface_id(c, f);
560	if (status < 0)
561		goto fail;
562	ac_interface_desc.bInterfaceNumber = status;
563	uac1->ac_intf = status;
564	uac1->ac_alt = 0;
565
566	status = usb_interface_id(c, f);
567	if (status < 0)
568		goto fail;
569	as_out_interface_alt_0_desc.bInterfaceNumber = status;
570	as_out_interface_alt_1_desc.bInterfaceNumber = status;
571	ac_header_desc.baInterfaceNr[0] = status;
572	uac1->as_out_intf = status;
573	uac1->as_out_alt = 0;
 
 
 
 
574
575	status = usb_interface_id(c, f);
576	if (status < 0)
577		goto fail;
578	as_in_interface_alt_0_desc.bInterfaceNumber = status;
579	as_in_interface_alt_1_desc.bInterfaceNumber = status;
580	ac_header_desc.baInterfaceNr[1] = status;
581	uac1->as_in_intf = status;
582	uac1->as_in_alt = 0;
 
 
583
584	audio->gadget = gadget;
585
586	status = -ENODEV;
587
 
 
 
 
 
 
 
 
 
 
 
 
 
588	/* allocate instance-specific endpoints */
589	ep = usb_ep_autoconfig(cdev->gadget, &as_out_ep_desc);
590	if (!ep)
591		goto fail;
592	audio->out_ep = ep;
593	audio->out_ep->desc = &as_out_ep_desc;
594
595	ep = usb_ep_autoconfig(cdev->gadget, &as_in_ep_desc);
596	if (!ep)
597		goto fail;
598	audio->in_ep = ep;
599	audio->in_ep->desc = &as_in_ep_desc;
 
 
 
 
 
 
600
601	/* copy descriptors, and track endpoint copies */
602	status = usb_assign_descriptors(f, f_audio_desc, f_audio_desc, NULL,
603					NULL);
604	if (status)
605		goto fail;
606
607	audio->out_ep_maxpsize = le16_to_cpu(as_out_ep_desc.wMaxPacketSize);
608	audio->in_ep_maxpsize = le16_to_cpu(as_in_ep_desc.wMaxPacketSize);
609	audio->params.c_chmask = audio_opts->c_chmask;
610	audio->params.c_srate = audio_opts->c_srate;
 
611	audio->params.c_ssize = audio_opts->c_ssize;
 
 
 
 
 
 
 
 
 
612	audio->params.p_chmask = audio_opts->p_chmask;
613	audio->params.p_srate = audio_opts->p_srate;
 
614	audio->params.p_ssize = audio_opts->p_ssize;
 
 
 
 
 
 
 
 
 
615	audio->params.req_number = audio_opts->req_number;
 
 
 
616
617	status = g_audio_setup(audio, "UAC1_PCM", "UAC1_Gadget");
618	if (status)
619		goto err_card_register;
620
621	return 0;
622
623err_card_register:
624	usb_free_all_descriptors(f);
 
 
 
 
 
625fail:
 
 
626	return status;
627}
628
629/*-------------------------------------------------------------------------*/
630
631static inline struct f_uac1_opts *to_f_uac1_opts(struct config_item *item)
632{
633	return container_of(to_config_group(item), struct f_uac1_opts,
634			    func_inst.group);
635}
636
637static void f_uac1_attr_release(struct config_item *item)
638{
639	struct f_uac1_opts *opts = to_f_uac1_opts(item);
640
641	usb_put_function_instance(&opts->func_inst);
642}
643
644static struct configfs_item_operations f_uac1_item_ops = {
645	.release	= f_uac1_attr_release,
646};
647
648#define UAC1_ATTRIBUTE(name)						\
 
 
 
 
 
 
 
 
649static ssize_t f_uac1_opts_##name##_show(				\
650					  struct config_item *item,	\
651					  char *page)			\
652{									\
653	struct f_uac1_opts *opts = to_f_uac1_opts(item);		\
654	int result;							\
655									\
656	mutex_lock(&opts->lock);					\
657	result = sprintf(page, "%u\n", opts->name);			\
658	mutex_unlock(&opts->lock);					\
659									\
660	return result;							\
661}									\
662									\
663static ssize_t f_uac1_opts_##name##_store(				\
664					  struct config_item *item,	\
665					  const char *page, size_t len)	\
666{									\
667	struct f_uac1_opts *opts = to_f_uac1_opts(item);		\
668	int ret;							\
669	u32 num;							\
670									\
671	mutex_lock(&opts->lock);					\
672	if (opts->refcnt) {						\
673		ret = -EBUSY;						\
674		goto end;						\
675	}								\
676									\
677	ret = kstrtou32(page, 0, &num);					\
678	if (ret)							\
679		goto end;						\
680									\
681	opts->name = num;						\
682	ret = len;							\
683									\
684end:									\
685	mutex_unlock(&opts->lock);					\
686	return ret;							\
687}									\
688									\
689CONFIGFS_ATTR(f_uac1_opts_, name)
690
691UAC1_ATTRIBUTE(c_chmask);
692UAC1_ATTRIBUTE(c_srate);
693UAC1_ATTRIBUTE(c_ssize);
694UAC1_ATTRIBUTE(p_chmask);
695UAC1_ATTRIBUTE(p_srate);
696UAC1_ATTRIBUTE(p_ssize);
697UAC1_ATTRIBUTE(req_number);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
698
699static struct configfs_attribute *f_uac1_attrs[] = {
700	&f_uac1_opts_attr_c_chmask,
701	&f_uac1_opts_attr_c_srate,
702	&f_uac1_opts_attr_c_ssize,
703	&f_uac1_opts_attr_p_chmask,
704	&f_uac1_opts_attr_p_srate,
705	&f_uac1_opts_attr_p_ssize,
706	&f_uac1_opts_attr_req_number,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
707	NULL,
708};
709
710static const struct config_item_type f_uac1_func_type = {
711	.ct_item_ops	= &f_uac1_item_ops,
712	.ct_attrs	= f_uac1_attrs,
713	.ct_owner	= THIS_MODULE,
714};
715
716static void f_audio_free_inst(struct usb_function_instance *f)
717{
718	struct f_uac1_opts *opts;
719
720	opts = container_of(f, struct f_uac1_opts, func_inst);
721	kfree(opts);
722}
723
724static struct usb_function_instance *f_audio_alloc_inst(void)
725{
726	struct f_uac1_opts *opts;
727
728	opts = kzalloc(sizeof(*opts), GFP_KERNEL);
729	if (!opts)
730		return ERR_PTR(-ENOMEM);
731
732	mutex_init(&opts->lock);
733	opts->func_inst.free_func_inst = f_audio_free_inst;
734
735	config_group_init_type_name(&opts->func_inst.group, "",
736				    &f_uac1_func_type);
737
738	opts->c_chmask = UAC1_DEF_CCHMASK;
739	opts->c_srate = UAC1_DEF_CSRATE;
740	opts->c_ssize = UAC1_DEF_CSSIZE;
741	opts->p_chmask = UAC1_DEF_PCHMASK;
742	opts->p_srate = UAC1_DEF_PSRATE;
743	opts->p_ssize = UAC1_DEF_PSSIZE;
 
 
 
 
 
 
 
 
 
 
 
 
 
744	opts->req_number = UAC1_DEF_REQ_NUM;
 
 
 
745	return &opts->func_inst;
746}
747
748static void f_audio_free(struct usb_function *f)
749{
750	struct g_audio *audio;
751	struct f_uac1_opts *opts;
752
753	audio = func_to_g_audio(f);
754	opts = container_of(f->fi, struct f_uac1_opts, func_inst);
755	kfree(audio);
756	mutex_lock(&opts->lock);
757	--opts->refcnt;
758	mutex_unlock(&opts->lock);
759}
760
761static void f_audio_unbind(struct usb_configuration *c, struct usb_function *f)
762{
763	struct g_audio *audio = func_to_g_audio(f);
764
765	g_audio_cleanup(audio);
766	usb_free_all_descriptors(f);
767
 
 
 
 
 
 
 
 
768	audio->gadget = NULL;
769}
770
771static struct usb_function *f_audio_alloc(struct usb_function_instance *fi)
772{
773	struct f_uac1 *uac1;
774	struct f_uac1_opts *opts;
775
776	/* allocate and initialize one new instance */
777	uac1 = kzalloc(sizeof(*uac1), GFP_KERNEL);
778	if (!uac1)
779		return ERR_PTR(-ENOMEM);
780
781	opts = container_of(fi, struct f_uac1_opts, func_inst);
782	mutex_lock(&opts->lock);
783	++opts->refcnt;
784	mutex_unlock(&opts->lock);
785
786	uac1->g_audio.func.name = "uac1_func";
787	uac1->g_audio.func.bind = f_audio_bind;
788	uac1->g_audio.func.unbind = f_audio_unbind;
789	uac1->g_audio.func.set_alt = f_audio_set_alt;
790	uac1->g_audio.func.get_alt = f_audio_get_alt;
791	uac1->g_audio.func.setup = f_audio_setup;
792	uac1->g_audio.func.disable = f_audio_disable;
 
793	uac1->g_audio.func.free_func = f_audio_free;
794
795	return &uac1->g_audio.func;
796}
797
798DECLARE_USB_FUNCTION_INIT(uac1, f_audio_alloc_inst, f_audio_alloc);
799MODULE_LICENSE("GPL");
800MODULE_AUTHOR("Ruslan Bilovol");