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