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