blob: 36fa6ef0581b5694db7a6bb227951b3b72abf5f1 [file] [log] [blame]
Greg Kroah-Hartman5fd54ac2017-11-03 11:28:30 +01001// SPDX-License-Identifier: GPL-2.0+
Jassi Brar132fcb42012-02-02 22:01:34 +05302/*
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)
Ruslan Bilovoleaf6cbe2021-07-12 14:55:28 +02008 *
9 * Copyright (C) 2020
10 * Ruslan Bilovol (ruslan.bilovol@gmail.com)
Jassi Brar132fcb42012-02-02 22:01:34 +053011 */
12
13#include <linux/usb/audio.h>
14#include <linux/usb/audio-v2.h>
Jassi Brar132fcb42012-02-02 22:01:34 +053015#include <linux/module.h>
16
Ruslan Bilovoleb9fecb2017-06-18 16:23:52 +030017#include "u_audio.h"
Pavel Hofmand9f27342021-10-22 16:03:39 +020018
Andrzej Pietrasiewiczf8f93d22014-07-22 19:58:30 +020019#include "u_uac2.h"
20
Ruslan Bilovol3713d5c2021-03-01 13:49:33 +020021/* UAC2 spec: 4.1 Audio Channel Cluster Descriptor */
22#define UAC2_CHANNEL_MASK 0x07FFFFFF
23
Jassi Brar132fcb42012-02-02 22:01:34 +053024/*
25 * The driver implements a simple UAC_2 topology.
Ruslan Bilovoleaf6cbe2021-07-12 14:55:28 +020026 * USB-OUT -> IT_1 -> FU -> OT_3 -> ALSA_Capture
27 * ALSA_Playback -> IT_2 -> FU -> OT_4 -> USB-IN
Jassi Brar132fcb42012-02-02 22:01:34 +053028 * Capture and Playback sampling rates are independently
29 * controlled by two clock sources :
30 * CLK_5 := c_srate, and CLK_6 := p_srate
31 */
Andreas Pape3fa4eaa2018-06-21 17:22:51 +020032#define USB_OUT_CLK_ID (out_clk_src_desc.bClockID)
33#define USB_IN_CLK_ID (in_clk_src_desc.bClockID)
Ruslan Bilovoleaf6cbe2021-07-12 14:55:28 +020034#define USB_OUT_FU_ID (out_feature_unit_desc->bUnitID)
35#define USB_IN_FU_ID (in_feature_unit_desc->bUnitID)
Jassi Brar132fcb42012-02-02 22:01:34 +053036
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
Ruslan Bilovoleaf6cbe2021-07-12 14:55:28 +020043#define FU_MUTE_CTRL 0
44#define FU_VOL_CTRL 2
Jassi Brar132fcb42012-02-02 22:01:34 +053045
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
Andreas Pape3fa4eaa2018-06-21 17:22:51 +020053#define EPIN_EN(_opts) ((_opts)->p_chmask != 0)
54#define EPOUT_EN(_opts) ((_opts)->c_chmask != 0)
Ruslan Bilovoleaf6cbe2021-07-12 14:55:28 +020055#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))
Ruslan Bilovol40c73b32021-06-04 00:01:03 +020061#define EPOUT_FBACK_IN_EN(_opts) ((_opts)->c_sync == USB_ENDPOINT_SYNC_ASYNC)
Andreas Pape3fa4eaa2018-06-21 17:22:51 +020062
Ruslan Bilovoleb9fecb2017-06-18 16:23:52 +030063struct 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() */
Ruslan Bilovoleaf6cbe2021-07-12 14:55:28 +020067
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;
Jassi Brar132fcb42012-02-02 22:01:34 +053073};
74
Ruslan Bilovoleb9fecb2017-06-18 16:23:52 +030075static inline struct f_uac2 *func_to_uac2(struct usb_function *f)
Jassi Brar132fcb42012-02-02 22:01:34 +053076{
Ruslan Bilovoleb9fecb2017-06-18 16:23:52 +030077 return container_of(f, struct f_uac2, g_audio.func);
Jassi Brar132fcb42012-02-02 22:01:34 +053078}
79
80static inline
Ruslan Bilovoleb9fecb2017-06-18 16:23:52 +030081struct f_uac2_opts *g_audio_to_uac2_opts(struct g_audio *agdev)
Daniel Mack254b3bf2014-08-27 19:09:05 +020082{
83 return container_of(agdev->func.fi, struct f_uac2_opts, func_inst);
84}
85
Ruslan Bilovoleaf6cbe2021-07-12 14:55:28 +020086static int afunc_notify(struct g_audio *agdev, int unit_id, int cs);
87
Jassi Brar132fcb42012-02-02 22:01:34 +053088/* --------- USB Function Interface ------------- */
89
90enum {
91 STR_ASSOC,
92 STR_IF_CTRL,
93 STR_CLKSRC_IN,
94 STR_CLKSRC_OUT,
95 STR_USB_IT,
96 STR_IO_IT,
97 STR_USB_OT,
98 STR_IO_OT,
Ruslan Bilovoleaf6cbe2021-07-12 14:55:28 +020099 STR_FU_IN,
100 STR_FU_OUT,
Jassi Brar132fcb42012-02-02 22:01:34 +0530101 STR_AS_OUT_ALT0,
102 STR_AS_OUT_ALT1,
103 STR_AS_IN_ALT0,
104 STR_AS_IN_ALT1,
105};
106
Jassi Brar132fcb42012-02-02 22:01:34 +0530107static char clksrc_in[8];
108static char clksrc_out[8];
Jassi Brar132fcb42012-02-02 22:01:34 +0530109
110static struct usb_string strings_fn[] = {
Sebastian Andrzej Siewiorb36c3472012-10-22 22:15:09 +0200111 [STR_ASSOC].s = "Source/Sink",
112 [STR_IF_CTRL].s = "Topology Control",
Jassi Brar132fcb42012-02-02 22:01:34 +0530113 [STR_CLKSRC_IN].s = clksrc_in,
114 [STR_CLKSRC_OUT].s = clksrc_out,
Sebastian Andrzej Siewiorb36c3472012-10-22 22:15:09 +0200115 [STR_USB_IT].s = "USBH Out",
116 [STR_IO_IT].s = "USBD Out",
117 [STR_USB_OT].s = "USBH In",
118 [STR_IO_OT].s = "USBD In",
Ruslan Bilovoleaf6cbe2021-07-12 14:55:28 +0200119 [STR_FU_IN].s = "Capture Volume",
120 [STR_FU_OUT].s = "Playback Volume",
Sebastian Andrzej Siewiorb36c3472012-10-22 22:15:09 +0200121 [STR_AS_OUT_ALT0].s = "Playback Inactive",
122 [STR_AS_OUT_ALT1].s = "Playback Active",
123 [STR_AS_IN_ALT0].s = "Capture Inactive",
124 [STR_AS_IN_ALT1].s = "Capture Active",
Jassi Brar132fcb42012-02-02 22:01:34 +0530125 { },
126};
127
128static struct usb_gadget_strings str_fn = {
129 .language = 0x0409, /* en-us */
130 .strings = strings_fn,
131};
132
133static struct usb_gadget_strings *fn_strings[] = {
134 &str_fn,
135 NULL,
136};
137
Jassi Brar132fcb42012-02-02 22:01:34 +0530138static struct usb_interface_assoc_descriptor iad_desc = {
139 .bLength = sizeof iad_desc,
140 .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION,
141
142 .bFirstInterface = 0,
143 .bInterfaceCount = 3,
144 .bFunctionClass = USB_CLASS_AUDIO,
145 .bFunctionSubClass = UAC2_FUNCTION_SUBCLASS_UNDEFINED,
146 .bFunctionProtocol = UAC_VERSION_2,
147};
148
149/* Audio Control Interface */
150static struct usb_interface_descriptor std_ac_if_desc = {
151 .bLength = sizeof std_ac_if_desc,
152 .bDescriptorType = USB_DT_INTERFACE,
153
154 .bAlternateSetting = 0,
Ruslan Bilovoleaf6cbe2021-07-12 14:55:28 +0200155 /* .bNumEndpoints = DYNAMIC */
Jassi Brar132fcb42012-02-02 22:01:34 +0530156 .bInterfaceClass = USB_CLASS_AUDIO,
157 .bInterfaceSubClass = USB_SUBCLASS_AUDIOCONTROL,
158 .bInterfaceProtocol = UAC_VERSION_2,
159};
160
161/* Clock source for IN traffic */
Lad, Prabhakaref16e7c2015-02-04 18:01:11 +0000162static struct uac_clock_source_descriptor in_clk_src_desc = {
Jassi Brar132fcb42012-02-02 22:01:34 +0530163 .bLength = sizeof in_clk_src_desc,
164 .bDescriptorType = USB_DT_CS_INTERFACE,
165
166 .bDescriptorSubtype = UAC2_CLOCK_SOURCE,
Andreas Pape3fa4eaa2018-06-21 17:22:51 +0200167 /* .bClockID = DYNAMIC */
Jassi Brar132fcb42012-02-02 22:01:34 +0530168 .bmAttributes = UAC_CLOCK_SOURCE_TYPE_INT_FIXED,
169 .bmControls = (CONTROL_RDONLY << CLK_FREQ_CTRL),
170 .bAssocTerminal = 0,
171};
172
173/* Clock source for OUT traffic */
Lad, Prabhakaref16e7c2015-02-04 18:01:11 +0000174static struct uac_clock_source_descriptor out_clk_src_desc = {
Jassi Brar132fcb42012-02-02 22:01:34 +0530175 .bLength = sizeof out_clk_src_desc,
176 .bDescriptorType = USB_DT_CS_INTERFACE,
177
178 .bDescriptorSubtype = UAC2_CLOCK_SOURCE,
Andreas Pape3fa4eaa2018-06-21 17:22:51 +0200179 /* .bClockID = DYNAMIC */
Jassi Brar132fcb42012-02-02 22:01:34 +0530180 .bmAttributes = UAC_CLOCK_SOURCE_TYPE_INT_FIXED,
181 .bmControls = (CONTROL_RDONLY << CLK_FREQ_CTRL),
182 .bAssocTerminal = 0,
183};
184
185/* Input Terminal for USB_OUT */
Lad, Prabhakaref16e7c2015-02-04 18:01:11 +0000186static struct uac2_input_terminal_descriptor usb_out_it_desc = {
Jassi Brar132fcb42012-02-02 22:01:34 +0530187 .bLength = sizeof usb_out_it_desc,
188 .bDescriptorType = USB_DT_CS_INTERFACE,
189
190 .bDescriptorSubtype = UAC_INPUT_TERMINAL,
Andreas Pape3fa4eaa2018-06-21 17:22:51 +0200191 /* .bTerminalID = DYNAMIC */
Jassi Brar132fcb42012-02-02 22:01:34 +0530192 .wTerminalType = cpu_to_le16(UAC_TERMINAL_STREAMING),
193 .bAssocTerminal = 0,
Andreas Pape3fa4eaa2018-06-21 17:22:51 +0200194 /* .bCSourceID = DYNAMIC */
Jassi Brar132fcb42012-02-02 22:01:34 +0530195 .iChannelNames = 0,
Ruslan Bilovol14e1d562017-06-25 16:23:47 +0300196 .bmControls = cpu_to_le16(CONTROL_RDWR << COPY_CTRL),
Jassi Brar132fcb42012-02-02 22:01:34 +0530197};
198
199/* Input Terminal for I/O-In */
Lad, Prabhakaref16e7c2015-02-04 18:01:11 +0000200static struct uac2_input_terminal_descriptor io_in_it_desc = {
Jassi Brar132fcb42012-02-02 22:01:34 +0530201 .bLength = sizeof io_in_it_desc,
202 .bDescriptorType = USB_DT_CS_INTERFACE,
203
204 .bDescriptorSubtype = UAC_INPUT_TERMINAL,
Andreas Pape3fa4eaa2018-06-21 17:22:51 +0200205 /* .bTerminalID = DYNAMIC */
Jassi Brar132fcb42012-02-02 22:01:34 +0530206 .wTerminalType = cpu_to_le16(UAC_INPUT_TERMINAL_UNDEFINED),
207 .bAssocTerminal = 0,
Andreas Pape3fa4eaa2018-06-21 17:22:51 +0200208 /* .bCSourceID = DYNAMIC */
Jassi Brar132fcb42012-02-02 22:01:34 +0530209 .iChannelNames = 0,
Ruslan Bilovol14e1d562017-06-25 16:23:47 +0300210 .bmControls = cpu_to_le16(CONTROL_RDWR << COPY_CTRL),
Jassi Brar132fcb42012-02-02 22:01:34 +0530211};
212
213/* Ouput Terminal for USB_IN */
Lad, Prabhakaref16e7c2015-02-04 18:01:11 +0000214static struct uac2_output_terminal_descriptor usb_in_ot_desc = {
Jassi Brar132fcb42012-02-02 22:01:34 +0530215 .bLength = sizeof usb_in_ot_desc,
216 .bDescriptorType = USB_DT_CS_INTERFACE,
217
218 .bDescriptorSubtype = UAC_OUTPUT_TERMINAL,
Andreas Pape3fa4eaa2018-06-21 17:22:51 +0200219 /* .bTerminalID = DYNAMIC */
Jassi Brar132fcb42012-02-02 22:01:34 +0530220 .wTerminalType = cpu_to_le16(UAC_TERMINAL_STREAMING),
221 .bAssocTerminal = 0,
Andreas Pape3fa4eaa2018-06-21 17:22:51 +0200222 /* .bSourceID = DYNAMIC */
223 /* .bCSourceID = DYNAMIC */
Ruslan Bilovol14e1d562017-06-25 16:23:47 +0300224 .bmControls = cpu_to_le16(CONTROL_RDWR << COPY_CTRL),
Jassi Brar132fcb42012-02-02 22:01:34 +0530225};
226
227/* Ouput Terminal for I/O-Out */
Lad, Prabhakaref16e7c2015-02-04 18:01:11 +0000228static struct uac2_output_terminal_descriptor io_out_ot_desc = {
Jassi Brar132fcb42012-02-02 22:01:34 +0530229 .bLength = sizeof io_out_ot_desc,
230 .bDescriptorType = USB_DT_CS_INTERFACE,
231
232 .bDescriptorSubtype = UAC_OUTPUT_TERMINAL,
Andreas Pape3fa4eaa2018-06-21 17:22:51 +0200233 /* .bTerminalID = DYNAMIC */
Jassi Brar132fcb42012-02-02 22:01:34 +0530234 .wTerminalType = cpu_to_le16(UAC_OUTPUT_TERMINAL_UNDEFINED),
235 .bAssocTerminal = 0,
Andreas Pape3fa4eaa2018-06-21 17:22:51 +0200236 /* .bSourceID = DYNAMIC */
237 /* .bCSourceID = DYNAMIC */
Ruslan Bilovol14e1d562017-06-25 16:23:47 +0300238 .bmControls = cpu_to_le16(CONTROL_RDWR << COPY_CTRL),
Jassi Brar132fcb42012-02-02 22:01:34 +0530239};
240
Ruslan Bilovoleaf6cbe2021-07-12 14:55:28 +0200241static struct uac2_feature_unit_descriptor *in_feature_unit_desc;
242static struct uac2_feature_unit_descriptor *out_feature_unit_desc;
243
Lad, Prabhakaref16e7c2015-02-04 18:01:11 +0000244static struct uac2_ac_header_descriptor ac_hdr_desc = {
Jassi Brar132fcb42012-02-02 22:01:34 +0530245 .bLength = sizeof ac_hdr_desc,
246 .bDescriptorType = USB_DT_CS_INTERFACE,
247
248 .bDescriptorSubtype = UAC_MS_HEADER,
249 .bcdADC = cpu_to_le16(0x200),
250 .bCategory = UAC2_FUNCTION_IO_BOX,
Ruslan Bilovola9cf8712020-07-03 16:49:03 +0300251 /* .wTotalLength = DYNAMIC */
Jassi Brar132fcb42012-02-02 22:01:34 +0530252 .bmControls = 0,
253};
254
Ruslan Bilovoleaf6cbe2021-07-12 14:55:28 +0200255/* AC IN Interrupt Endpoint */
256static struct usb_endpoint_descriptor fs_ep_int_desc = {
257 .bLength = USB_DT_ENDPOINT_SIZE,
258 .bDescriptorType = USB_DT_ENDPOINT,
259
260 .bEndpointAddress = USB_DIR_IN,
261 .bmAttributes = USB_ENDPOINT_XFER_INT,
262 .wMaxPacketSize = cpu_to_le16(6),
263 .bInterval = 1,
264};
265
266static struct usb_endpoint_descriptor hs_ep_int_desc = {
267 .bLength = USB_DT_ENDPOINT_SIZE,
268 .bDescriptorType = USB_DT_ENDPOINT,
269
270 .bmAttributes = USB_ENDPOINT_XFER_INT,
271 .wMaxPacketSize = cpu_to_le16(6),
272 .bInterval = 4,
273};
274
275static struct usb_endpoint_descriptor ss_ep_int_desc = {
276 .bLength = USB_DT_ENDPOINT_SIZE,
277 .bDescriptorType = USB_DT_ENDPOINT,
278
279 .bEndpointAddress = USB_DIR_IN,
280 .bmAttributes = USB_ENDPOINT_XFER_INT,
281 .wMaxPacketSize = cpu_to_le16(6),
282 .bInterval = 4,
283};
284
Jassi Brar132fcb42012-02-02 22:01:34 +0530285/* Audio Streaming OUT Interface - Alt0 */
286static struct usb_interface_descriptor std_as_out_if0_desc = {
287 .bLength = sizeof std_as_out_if0_desc,
288 .bDescriptorType = USB_DT_INTERFACE,
289
290 .bAlternateSetting = 0,
291 .bNumEndpoints = 0,
292 .bInterfaceClass = USB_CLASS_AUDIO,
293 .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
294 .bInterfaceProtocol = UAC_VERSION_2,
295};
296
297/* Audio Streaming OUT Interface - Alt1 */
298static struct usb_interface_descriptor std_as_out_if1_desc = {
299 .bLength = sizeof std_as_out_if1_desc,
300 .bDescriptorType = USB_DT_INTERFACE,
301
302 .bAlternateSetting = 1,
Ruslan Bilovol40c73b32021-06-04 00:01:03 +0200303 .bNumEndpoints = 1,
Jassi Brar132fcb42012-02-02 22:01:34 +0530304 .bInterfaceClass = USB_CLASS_AUDIO,
305 .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
306 .bInterfaceProtocol = UAC_VERSION_2,
307};
308
309/* Audio Stream OUT Intface Desc */
Lad, Prabhakaref16e7c2015-02-04 18:01:11 +0000310static struct uac2_as_header_descriptor as_out_hdr_desc = {
Jassi Brar132fcb42012-02-02 22:01:34 +0530311 .bLength = sizeof as_out_hdr_desc,
312 .bDescriptorType = USB_DT_CS_INTERFACE,
313
314 .bDescriptorSubtype = UAC_AS_GENERAL,
Andreas Pape3fa4eaa2018-06-21 17:22:51 +0200315 /* .bTerminalLink = DYNAMIC */
Jassi Brar132fcb42012-02-02 22:01:34 +0530316 .bmControls = 0,
317 .bFormatType = UAC_FORMAT_TYPE_I,
318 .bmFormats = cpu_to_le32(UAC_FORMAT_TYPE_I_PCM),
319 .iChannelNames = 0,
320};
321
322/* Audio USB_OUT Format */
Lad, Prabhakaref16e7c2015-02-04 18:01:11 +0000323static struct uac2_format_type_i_descriptor as_out_fmt1_desc = {
Jassi Brar132fcb42012-02-02 22:01:34 +0530324 .bLength = sizeof as_out_fmt1_desc,
325 .bDescriptorType = USB_DT_CS_INTERFACE,
326 .bDescriptorSubtype = UAC_FORMAT_TYPE,
327 .bFormatType = UAC_FORMAT_TYPE_I,
328};
329
330/* STD AS ISO OUT Endpoint */
Lad, Prabhakaref16e7c2015-02-04 18:01:11 +0000331static struct usb_endpoint_descriptor fs_epout_desc = {
Jassi Brar132fcb42012-02-02 22:01:34 +0530332 .bLength = USB_DT_ENDPOINT_SIZE,
333 .bDescriptorType = USB_DT_ENDPOINT,
334
335 .bEndpointAddress = USB_DIR_OUT,
Ruslan Bilovol40c73b32021-06-04 00:01:03 +0200336 /* .bmAttributes = DYNAMIC */
Jerome Brunet93890442020-12-21 18:35:28 +0100337 /* .wMaxPacketSize = DYNAMIC */
Jassi Brar132fcb42012-02-02 22:01:34 +0530338 .bInterval = 1,
339};
340
Lad, Prabhakaref16e7c2015-02-04 18:01:11 +0000341static struct usb_endpoint_descriptor hs_epout_desc = {
Jassi Brar132fcb42012-02-02 22:01:34 +0530342 .bLength = USB_DT_ENDPOINT_SIZE,
343 .bDescriptorType = USB_DT_ENDPOINT,
344
Ruslan Bilovol40c73b32021-06-04 00:01:03 +0200345 /* .bmAttributes = DYNAMIC */
Jerome Brunet93890442020-12-21 18:35:28 +0100346 /* .wMaxPacketSize = DYNAMIC */
Jassi Brar132fcb42012-02-02 22:01:34 +0530347 .bInterval = 4,
348};
349
Pawel Laszczakf8cb3d52021-03-10 11:52:16 +0100350static struct usb_endpoint_descriptor ss_epout_desc = {
351 .bLength = USB_DT_ENDPOINT_SIZE,
352 .bDescriptorType = USB_DT_ENDPOINT,
353
354 .bEndpointAddress = USB_DIR_OUT,
Ruslan Bilovol40c73b32021-06-04 00:01:03 +0200355 /* .bmAttributes = DYNAMIC */
Pawel Laszczakf8cb3d52021-03-10 11:52:16 +0100356 /* .wMaxPacketSize = DYNAMIC */
357 .bInterval = 4,
358};
359
360static struct usb_ss_ep_comp_descriptor ss_epout_desc_comp = {
361 .bLength = sizeof(ss_epout_desc_comp),
362 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
363 .bMaxBurst = 0,
364 .bmAttributes = 0,
365 /* wBytesPerInterval = DYNAMIC */
366};
367
Jassi Brar132fcb42012-02-02 22:01:34 +0530368/* CS AS ISO OUT Endpoint */
369static struct uac2_iso_endpoint_descriptor as_iso_out_desc = {
370 .bLength = sizeof as_iso_out_desc,
371 .bDescriptorType = USB_DT_CS_ENDPOINT,
372
373 .bDescriptorSubtype = UAC_EP_GENERAL,
374 .bmAttributes = 0,
375 .bmControls = 0,
376 .bLockDelayUnits = 0,
377 .wLockDelay = 0,
378};
379
Ruslan Bilovol24f779d2021-06-04 00:01:02 +0200380/* STD AS ISO IN Feedback Endpoint */
381static struct usb_endpoint_descriptor fs_epin_fback_desc = {
382 .bLength = USB_DT_ENDPOINT_SIZE,
383 .bDescriptorType = USB_DT_ENDPOINT,
384
385 .bEndpointAddress = USB_DIR_IN,
386 .bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_USAGE_FEEDBACK,
387 .wMaxPacketSize = cpu_to_le16(3),
388 .bInterval = 1,
389};
390
391static struct usb_endpoint_descriptor hs_epin_fback_desc = {
392 .bLength = USB_DT_ENDPOINT_SIZE,
393 .bDescriptorType = USB_DT_ENDPOINT,
394
395 .bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_USAGE_FEEDBACK,
396 .wMaxPacketSize = cpu_to_le16(4),
397 .bInterval = 4,
398};
399
400static struct usb_endpoint_descriptor ss_epin_fback_desc = {
401 .bLength = USB_DT_ENDPOINT_SIZE,
402 .bDescriptorType = USB_DT_ENDPOINT,
403
404 .bEndpointAddress = USB_DIR_IN,
405 .bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_USAGE_FEEDBACK,
406 .wMaxPacketSize = cpu_to_le16(4),
407 .bInterval = 4,
408};
409
Jack Pham595091a2021-09-09 10:48:10 -0700410static struct usb_ss_ep_comp_descriptor ss_epin_fback_desc_comp = {
411 .bLength = sizeof(ss_epin_fback_desc_comp),
412 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
413 .bMaxBurst = 0,
414 .bmAttributes = 0,
415 .wBytesPerInterval = cpu_to_le16(4),
416};
417
Ruslan Bilovol24f779d2021-06-04 00:01:02 +0200418
Jassi Brar132fcb42012-02-02 22:01:34 +0530419/* Audio Streaming IN Interface - Alt0 */
420static struct usb_interface_descriptor std_as_in_if0_desc = {
421 .bLength = sizeof std_as_in_if0_desc,
422 .bDescriptorType = USB_DT_INTERFACE,
423
424 .bAlternateSetting = 0,
425 .bNumEndpoints = 0,
426 .bInterfaceClass = USB_CLASS_AUDIO,
427 .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
428 .bInterfaceProtocol = UAC_VERSION_2,
429};
430
431/* Audio Streaming IN Interface - Alt1 */
432static struct usb_interface_descriptor std_as_in_if1_desc = {
433 .bLength = sizeof std_as_in_if1_desc,
434 .bDescriptorType = USB_DT_INTERFACE,
435
436 .bAlternateSetting = 1,
437 .bNumEndpoints = 1,
438 .bInterfaceClass = USB_CLASS_AUDIO,
439 .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
440 .bInterfaceProtocol = UAC_VERSION_2,
441};
442
443/* Audio Stream IN Intface Desc */
Lad, Prabhakaref16e7c2015-02-04 18:01:11 +0000444static struct uac2_as_header_descriptor as_in_hdr_desc = {
Jassi Brar132fcb42012-02-02 22:01:34 +0530445 .bLength = sizeof as_in_hdr_desc,
446 .bDescriptorType = USB_DT_CS_INTERFACE,
447
448 .bDescriptorSubtype = UAC_AS_GENERAL,
Andreas Pape3fa4eaa2018-06-21 17:22:51 +0200449 /* .bTerminalLink = DYNAMIC */
Jassi Brar132fcb42012-02-02 22:01:34 +0530450 .bmControls = 0,
451 .bFormatType = UAC_FORMAT_TYPE_I,
452 .bmFormats = cpu_to_le32(UAC_FORMAT_TYPE_I_PCM),
453 .iChannelNames = 0,
454};
455
456/* Audio USB_IN Format */
Lad, Prabhakaref16e7c2015-02-04 18:01:11 +0000457static struct uac2_format_type_i_descriptor as_in_fmt1_desc = {
Jassi Brar132fcb42012-02-02 22:01:34 +0530458 .bLength = sizeof as_in_fmt1_desc,
459 .bDescriptorType = USB_DT_CS_INTERFACE,
460 .bDescriptorSubtype = UAC_FORMAT_TYPE,
461 .bFormatType = UAC_FORMAT_TYPE_I,
462};
463
464/* STD AS ISO IN Endpoint */
Lad, Prabhakaref16e7c2015-02-04 18:01:11 +0000465static struct usb_endpoint_descriptor fs_epin_desc = {
Jassi Brar132fcb42012-02-02 22:01:34 +0530466 .bLength = USB_DT_ENDPOINT_SIZE,
467 .bDescriptorType = USB_DT_ENDPOINT,
468
469 .bEndpointAddress = USB_DIR_IN,
470 .bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC,
Jerome Brunet93890442020-12-21 18:35:28 +0100471 /* .wMaxPacketSize = DYNAMIC */
Jassi Brar132fcb42012-02-02 22:01:34 +0530472 .bInterval = 1,
473};
474
Lad, Prabhakaref16e7c2015-02-04 18:01:11 +0000475static struct usb_endpoint_descriptor hs_epin_desc = {
Jassi Brar132fcb42012-02-02 22:01:34 +0530476 .bLength = USB_DT_ENDPOINT_SIZE,
477 .bDescriptorType = USB_DT_ENDPOINT,
478
479 .bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC,
Jerome Brunet93890442020-12-21 18:35:28 +0100480 /* .wMaxPacketSize = DYNAMIC */
Jassi Brar132fcb42012-02-02 22:01:34 +0530481 .bInterval = 4,
482};
483
Pawel Laszczakf8cb3d52021-03-10 11:52:16 +0100484static struct usb_endpoint_descriptor ss_epin_desc = {
485 .bLength = USB_DT_ENDPOINT_SIZE,
486 .bDescriptorType = USB_DT_ENDPOINT,
487
488 .bEndpointAddress = USB_DIR_IN,
489 .bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC,
490 /* .wMaxPacketSize = DYNAMIC */
491 .bInterval = 4,
492};
493
494static struct usb_ss_ep_comp_descriptor ss_epin_desc_comp = {
495 .bLength = sizeof(ss_epin_desc_comp),
496 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
497 .bMaxBurst = 0,
498 .bmAttributes = 0,
499 /* wBytesPerInterval = DYNAMIC */
500};
501
Jassi Brar132fcb42012-02-02 22:01:34 +0530502/* CS AS ISO IN Endpoint */
503static struct uac2_iso_endpoint_descriptor as_iso_in_desc = {
504 .bLength = sizeof as_iso_in_desc,
505 .bDescriptorType = USB_DT_CS_ENDPOINT,
506
507 .bDescriptorSubtype = UAC_EP_GENERAL,
508 .bmAttributes = 0,
509 .bmControls = 0,
510 .bLockDelayUnits = 0,
511 .wLockDelay = 0,
512};
513
514static struct usb_descriptor_header *fs_audio_desc[] = {
515 (struct usb_descriptor_header *)&iad_desc,
516 (struct usb_descriptor_header *)&std_ac_if_desc,
517
518 (struct usb_descriptor_header *)&ac_hdr_desc,
519 (struct usb_descriptor_header *)&in_clk_src_desc,
520 (struct usb_descriptor_header *)&out_clk_src_desc,
521 (struct usb_descriptor_header *)&usb_out_it_desc,
Ruslan Bilovoleaf6cbe2021-07-12 14:55:28 +0200522 (struct usb_descriptor_header *)&out_feature_unit_desc,
Jassi Brar132fcb42012-02-02 22:01:34 +0530523 (struct usb_descriptor_header *)&io_in_it_desc,
524 (struct usb_descriptor_header *)&usb_in_ot_desc,
Ruslan Bilovoleaf6cbe2021-07-12 14:55:28 +0200525 (struct usb_descriptor_header *)&in_feature_unit_desc,
Jassi Brar132fcb42012-02-02 22:01:34 +0530526 (struct usb_descriptor_header *)&io_out_ot_desc,
527
Ruslan Bilovoleaf6cbe2021-07-12 14:55:28 +0200528 (struct usb_descriptor_header *)&fs_ep_int_desc,
529
Jassi Brar132fcb42012-02-02 22:01:34 +0530530 (struct usb_descriptor_header *)&std_as_out_if0_desc,
531 (struct usb_descriptor_header *)&std_as_out_if1_desc,
532
533 (struct usb_descriptor_header *)&as_out_hdr_desc,
534 (struct usb_descriptor_header *)&as_out_fmt1_desc,
535 (struct usb_descriptor_header *)&fs_epout_desc,
536 (struct usb_descriptor_header *)&as_iso_out_desc,
Ruslan Bilovol24f779d2021-06-04 00:01:02 +0200537 (struct usb_descriptor_header *)&fs_epin_fback_desc,
Jassi Brar132fcb42012-02-02 22:01:34 +0530538
539 (struct usb_descriptor_header *)&std_as_in_if0_desc,
540 (struct usb_descriptor_header *)&std_as_in_if1_desc,
541
542 (struct usb_descriptor_header *)&as_in_hdr_desc,
543 (struct usb_descriptor_header *)&as_in_fmt1_desc,
544 (struct usb_descriptor_header *)&fs_epin_desc,
545 (struct usb_descriptor_header *)&as_iso_in_desc,
546 NULL,
547};
548
549static struct usb_descriptor_header *hs_audio_desc[] = {
550 (struct usb_descriptor_header *)&iad_desc,
551 (struct usb_descriptor_header *)&std_ac_if_desc,
552
553 (struct usb_descriptor_header *)&ac_hdr_desc,
554 (struct usb_descriptor_header *)&in_clk_src_desc,
555 (struct usb_descriptor_header *)&out_clk_src_desc,
556 (struct usb_descriptor_header *)&usb_out_it_desc,
Ruslan Bilovoleaf6cbe2021-07-12 14:55:28 +0200557 (struct usb_descriptor_header *)&out_feature_unit_desc,
Jassi Brar132fcb42012-02-02 22:01:34 +0530558 (struct usb_descriptor_header *)&io_in_it_desc,
559 (struct usb_descriptor_header *)&usb_in_ot_desc,
Ruslan Bilovoleaf6cbe2021-07-12 14:55:28 +0200560 (struct usb_descriptor_header *)&in_feature_unit_desc,
Jassi Brar132fcb42012-02-02 22:01:34 +0530561 (struct usb_descriptor_header *)&io_out_ot_desc,
562
Ruslan Bilovoleaf6cbe2021-07-12 14:55:28 +0200563 (struct usb_descriptor_header *)&hs_ep_int_desc,
564
Jassi Brar132fcb42012-02-02 22:01:34 +0530565 (struct usb_descriptor_header *)&std_as_out_if0_desc,
566 (struct usb_descriptor_header *)&std_as_out_if1_desc,
567
568 (struct usb_descriptor_header *)&as_out_hdr_desc,
569 (struct usb_descriptor_header *)&as_out_fmt1_desc,
570 (struct usb_descriptor_header *)&hs_epout_desc,
571 (struct usb_descriptor_header *)&as_iso_out_desc,
Ruslan Bilovol24f779d2021-06-04 00:01:02 +0200572 (struct usb_descriptor_header *)&hs_epin_fback_desc,
Jassi Brar132fcb42012-02-02 22:01:34 +0530573
574 (struct usb_descriptor_header *)&std_as_in_if0_desc,
575 (struct usb_descriptor_header *)&std_as_in_if1_desc,
576
577 (struct usb_descriptor_header *)&as_in_hdr_desc,
578 (struct usb_descriptor_header *)&as_in_fmt1_desc,
579 (struct usb_descriptor_header *)&hs_epin_desc,
580 (struct usb_descriptor_header *)&as_iso_in_desc,
581 NULL,
582};
583
Pawel Laszczakf8cb3d52021-03-10 11:52:16 +0100584static struct usb_descriptor_header *ss_audio_desc[] = {
585 (struct usb_descriptor_header *)&iad_desc,
586 (struct usb_descriptor_header *)&std_ac_if_desc,
587
588 (struct usb_descriptor_header *)&ac_hdr_desc,
589 (struct usb_descriptor_header *)&in_clk_src_desc,
590 (struct usb_descriptor_header *)&out_clk_src_desc,
591 (struct usb_descriptor_header *)&usb_out_it_desc,
Ruslan Bilovoleaf6cbe2021-07-12 14:55:28 +0200592 (struct usb_descriptor_header *)&out_feature_unit_desc,
Pawel Laszczakf8cb3d52021-03-10 11:52:16 +0100593 (struct usb_descriptor_header *)&io_in_it_desc,
594 (struct usb_descriptor_header *)&usb_in_ot_desc,
Ruslan Bilovoleaf6cbe2021-07-12 14:55:28 +0200595 (struct usb_descriptor_header *)&in_feature_unit_desc,
Pawel Laszczakf8cb3d52021-03-10 11:52:16 +0100596 (struct usb_descriptor_header *)&io_out_ot_desc,
597
Ruslan Bilovoleaf6cbe2021-07-12 14:55:28 +0200598 (struct usb_descriptor_header *)&ss_ep_int_desc,
599
Pawel Laszczakf8cb3d52021-03-10 11:52:16 +0100600 (struct usb_descriptor_header *)&std_as_out_if0_desc,
601 (struct usb_descriptor_header *)&std_as_out_if1_desc,
602
603 (struct usb_descriptor_header *)&as_out_hdr_desc,
604 (struct usb_descriptor_header *)&as_out_fmt1_desc,
605 (struct usb_descriptor_header *)&ss_epout_desc,
606 (struct usb_descriptor_header *)&ss_epout_desc_comp,
607 (struct usb_descriptor_header *)&as_iso_out_desc,
Ruslan Bilovol24f779d2021-06-04 00:01:02 +0200608 (struct usb_descriptor_header *)&ss_epin_fback_desc,
Jack Pham595091a2021-09-09 10:48:10 -0700609 (struct usb_descriptor_header *)&ss_epin_fback_desc_comp,
Pawel Laszczakf8cb3d52021-03-10 11:52:16 +0100610
611 (struct usb_descriptor_header *)&std_as_in_if0_desc,
612 (struct usb_descriptor_header *)&std_as_in_if1_desc,
613
614 (struct usb_descriptor_header *)&as_in_hdr_desc,
615 (struct usb_descriptor_header *)&as_in_fmt1_desc,
616 (struct usb_descriptor_header *)&ss_epin_desc,
617 (struct usb_descriptor_header *)&ss_epin_desc_comp,
618 (struct usb_descriptor_header *)&as_iso_in_desc,
619 NULL,
620};
621
Ruslan Bilovoleaf6cbe2021-07-12 14:55:28 +0200622struct cntrl_cur_lay2 {
623 __le16 wCUR;
624};
625
626struct cntrl_range_lay2 {
627 __le16 wNumSubRanges;
628 __le16 wMIN;
629 __le16 wMAX;
630 __le16 wRES;
631} __packed;
632
Jassi Brar132fcb42012-02-02 22:01:34 +0530633struct cntrl_cur_lay3 {
Eugeniu Roscaeec24f22018-07-02 23:46:47 +0200634 __le32 dCUR;
Jassi Brar132fcb42012-02-02 22:01:34 +0530635};
636
637struct cntrl_range_lay3 {
Eugeniu Roscaeec24f22018-07-02 23:46:47 +0200638 __le16 wNumSubRanges;
639 __le32 dMIN;
640 __le32 dMAX;
641 __le32 dRES;
Jassi Brar132fcb42012-02-02 22:01:34 +0530642} __packed;
643
Jerome Brunet93890442020-12-21 18:35:28 +0100644static int set_ep_max_packet_size(const struct f_uac2_opts *uac2_opts,
Peter Chen913e4a92015-07-30 13:13:03 +0800645 struct usb_endpoint_descriptor *ep_desc,
Jerome Brunet93890442020-12-21 18:35:28 +0100646 enum usb_device_speed speed, bool is_playback)
Peter Chen913e4a92015-07-30 13:13:03 +0800647{
648 int chmask, srate, ssize;
Jerome Brunet93890442020-12-21 18:35:28 +0100649 u16 max_size_bw, max_size_ep;
650 unsigned int factor;
651
652 switch (speed) {
653 case USB_SPEED_FULL:
654 max_size_ep = 1023;
655 factor = 1000;
656 break;
657
658 case USB_SPEED_HIGH:
Pawel Laszczakf8cb3d52021-03-10 11:52:16 +0100659 case USB_SPEED_SUPER:
Jerome Brunet93890442020-12-21 18:35:28 +0100660 max_size_ep = 1024;
661 factor = 8000;
662 break;
663
664 default:
665 return -EINVAL;
666 }
Peter Chen913e4a92015-07-30 13:13:03 +0800667
668 if (is_playback) {
669 chmask = uac2_opts->p_chmask;
670 srate = uac2_opts->p_srate;
671 ssize = uac2_opts->p_ssize;
672 } else {
673 chmask = uac2_opts->c_chmask;
674 srate = uac2_opts->c_srate;
675 ssize = uac2_opts->c_ssize;
676 }
677
Pavel Hofman6fec0182021-10-13 09:39:34 +0200678 if (is_playback || (uac2_opts->c_sync == USB_ENDPOINT_SYNC_ASYNC)) {
679 // playback is always async, capture only when configured
680 // Win10 requires max packet size + 1 frame
Ruslan Bilovole89bb422021-06-04 00:01:04 +0200681 srate = srate * (1000 + uac2_opts->fb_max) / 1000;
Pavel Hofman0560c9c2021-09-24 10:00:27 +0200682 // updated srate is always bigger, therefore DIV_ROUND_UP always yields +1
683 max_size_bw = num_channels(chmask) * ssize *
684 (DIV_ROUND_UP(srate, factor / (1 << (ep_desc->bInterval - 1))));
685 } else {
686 // adding 1 frame provision for Win10
687 max_size_bw = num_channels(chmask) * ssize *
688 (DIV_ROUND_UP(srate, factor / (1 << (ep_desc->bInterval - 1))) + 1);
689 }
Jerome Brunet93890442020-12-21 18:35:28 +0100690 ep_desc->wMaxPacketSize = cpu_to_le16(min_t(u16, max_size_bw,
691 max_size_ep));
692
693 return 0;
Peter Chen913e4a92015-07-30 13:13:03 +0800694}
695
Ruslan Bilovoleaf6cbe2021-07-12 14:55:28 +0200696static struct uac2_feature_unit_descriptor *build_fu_desc(int chmask)
697{
698 struct uac2_feature_unit_descriptor *fu_desc;
699 int channels = num_channels(chmask);
700 int fu_desc_size = UAC2_DT_FEATURE_UNIT_SIZE(channels);
701
702 fu_desc = kzalloc(fu_desc_size, GFP_KERNEL);
703 if (!fu_desc)
704 return NULL;
705
706 fu_desc->bLength = fu_desc_size;
707 fu_desc->bDescriptorType = USB_DT_CS_INTERFACE;
708
709 fu_desc->bDescriptorSubtype = UAC_FEATURE_UNIT;
710
711 /* bUnitID, bSourceID and bmaControls will be defined later */
712
713 return fu_desc;
714}
715
Andreas Pape3fa4eaa2018-06-21 17:22:51 +0200716/* Use macro to overcome line length limitation */
717#define USBDHDR(p) (struct usb_descriptor_header *)(p)
718
Pawel Laszczakf8cb3d52021-03-10 11:52:16 +0100719static void setup_headers(struct f_uac2_opts *opts,
720 struct usb_descriptor_header **headers,
721 enum usb_device_speed speed)
722{
723 struct usb_ss_ep_comp_descriptor *epout_desc_comp = NULL;
724 struct usb_ss_ep_comp_descriptor *epin_desc_comp = NULL;
Jack Pham595091a2021-09-09 10:48:10 -0700725 struct usb_ss_ep_comp_descriptor *epin_fback_desc_comp = NULL;
Pawel Laszczakf8cb3d52021-03-10 11:52:16 +0100726 struct usb_endpoint_descriptor *epout_desc;
727 struct usb_endpoint_descriptor *epin_desc;
Ruslan Bilovol24f779d2021-06-04 00:01:02 +0200728 struct usb_endpoint_descriptor *epin_fback_desc;
Ruslan Bilovoleaf6cbe2021-07-12 14:55:28 +0200729 struct usb_endpoint_descriptor *ep_int_desc;
Pawel Laszczakf8cb3d52021-03-10 11:52:16 +0100730 int i;
731
732 switch (speed) {
733 case USB_SPEED_FULL:
734 epout_desc = &fs_epout_desc;
735 epin_desc = &fs_epin_desc;
Ruslan Bilovol24f779d2021-06-04 00:01:02 +0200736 epin_fback_desc = &fs_epin_fback_desc;
Ruslan Bilovoleaf6cbe2021-07-12 14:55:28 +0200737 ep_int_desc = &fs_ep_int_desc;
Pawel Laszczakf8cb3d52021-03-10 11:52:16 +0100738 break;
739 case USB_SPEED_HIGH:
740 epout_desc = &hs_epout_desc;
741 epin_desc = &hs_epin_desc;
Ruslan Bilovol24f779d2021-06-04 00:01:02 +0200742 epin_fback_desc = &hs_epin_fback_desc;
Ruslan Bilovoleaf6cbe2021-07-12 14:55:28 +0200743 ep_int_desc = &hs_ep_int_desc;
Pawel Laszczakf8cb3d52021-03-10 11:52:16 +0100744 break;
745 default:
746 epout_desc = &ss_epout_desc;
747 epin_desc = &ss_epin_desc;
748 epout_desc_comp = &ss_epout_desc_comp;
749 epin_desc_comp = &ss_epin_desc_comp;
Ruslan Bilovol24f779d2021-06-04 00:01:02 +0200750 epin_fback_desc = &ss_epin_fback_desc;
Jack Pham595091a2021-09-09 10:48:10 -0700751 epin_fback_desc_comp = &ss_epin_fback_desc_comp;
Ruslan Bilovoleaf6cbe2021-07-12 14:55:28 +0200752 ep_int_desc = &ss_ep_int_desc;
Pawel Laszczakf8cb3d52021-03-10 11:52:16 +0100753 }
754
755 i = 0;
756 headers[i++] = USBDHDR(&iad_desc);
757 headers[i++] = USBDHDR(&std_ac_if_desc);
758 headers[i++] = USBDHDR(&ac_hdr_desc);
759 if (EPIN_EN(opts))
760 headers[i++] = USBDHDR(&in_clk_src_desc);
761 if (EPOUT_EN(opts)) {
762 headers[i++] = USBDHDR(&out_clk_src_desc);
763 headers[i++] = USBDHDR(&usb_out_it_desc);
Ruslan Bilovoleaf6cbe2021-07-12 14:55:28 +0200764
Colin Ian King18d6b392021-09-02 23:47:58 +0100765 if (FUOUT_EN(opts))
766 headers[i++] = USBDHDR(out_feature_unit_desc);
767 }
Ruslan Bilovoleaf6cbe2021-07-12 14:55:28 +0200768
Pawel Laszczakf8cb3d52021-03-10 11:52:16 +0100769 if (EPIN_EN(opts)) {
770 headers[i++] = USBDHDR(&io_in_it_desc);
Ruslan Bilovoleaf6cbe2021-07-12 14:55:28 +0200771
Colin Ian King18d6b392021-09-02 23:47:58 +0100772 if (FUIN_EN(opts))
773 headers[i++] = USBDHDR(in_feature_unit_desc);
Ruslan Bilovoleaf6cbe2021-07-12 14:55:28 +0200774
Pawel Laszczakf8cb3d52021-03-10 11:52:16 +0100775 headers[i++] = USBDHDR(&usb_in_ot_desc);
776 }
Ruslan Bilovoleaf6cbe2021-07-12 14:55:28 +0200777
778 if (EPOUT_EN(opts))
Pawel Laszczakf8cb3d52021-03-10 11:52:16 +0100779 headers[i++] = USBDHDR(&io_out_ot_desc);
Ruslan Bilovoleaf6cbe2021-07-12 14:55:28 +0200780
Colin Ian King18d6b392021-09-02 23:47:58 +0100781 if (FUOUT_EN(opts) || FUIN_EN(opts))
782 headers[i++] = USBDHDR(ep_int_desc);
Ruslan Bilovoleaf6cbe2021-07-12 14:55:28 +0200783
Colin Ian King18d6b392021-09-02 23:47:58 +0100784 if (EPOUT_EN(opts)) {
Pawel Laszczakf8cb3d52021-03-10 11:52:16 +0100785 headers[i++] = USBDHDR(&std_as_out_if0_desc);
786 headers[i++] = USBDHDR(&std_as_out_if1_desc);
787 headers[i++] = USBDHDR(&as_out_hdr_desc);
788 headers[i++] = USBDHDR(&as_out_fmt1_desc);
789 headers[i++] = USBDHDR(epout_desc);
790 if (epout_desc_comp)
791 headers[i++] = USBDHDR(epout_desc_comp);
792
793 headers[i++] = USBDHDR(&as_iso_out_desc);
Ruslan Bilovol40c73b32021-06-04 00:01:03 +0200794
Jack Pham595091a2021-09-09 10:48:10 -0700795 if (EPOUT_FBACK_IN_EN(opts)) {
Ruslan Bilovol40c73b32021-06-04 00:01:03 +0200796 headers[i++] = USBDHDR(epin_fback_desc);
Jack Pham595091a2021-09-09 10:48:10 -0700797 if (epin_fback_desc_comp)
798 headers[i++] = USBDHDR(epin_fback_desc_comp);
799 }
Pawel Laszczakf8cb3d52021-03-10 11:52:16 +0100800 }
Ruslan Bilovoleaf6cbe2021-07-12 14:55:28 +0200801
Pawel Laszczakf8cb3d52021-03-10 11:52:16 +0100802 if (EPIN_EN(opts)) {
803 headers[i++] = USBDHDR(&std_as_in_if0_desc);
804 headers[i++] = USBDHDR(&std_as_in_if1_desc);
805 headers[i++] = USBDHDR(&as_in_hdr_desc);
806 headers[i++] = USBDHDR(&as_in_fmt1_desc);
807 headers[i++] = USBDHDR(epin_desc);
808 if (epin_desc_comp)
809 headers[i++] = USBDHDR(epin_desc_comp);
810
811 headers[i++] = USBDHDR(&as_iso_in_desc);
812 }
813 headers[i] = NULL;
814}
815
Andreas Pape3fa4eaa2018-06-21 17:22:51 +0200816static void setup_descriptor(struct f_uac2_opts *opts)
817{
818 /* patch descriptors */
819 int i = 1; /* ID's start with 1 */
820
821 if (EPOUT_EN(opts))
822 usb_out_it_desc.bTerminalID = i++;
823 if (EPIN_EN(opts))
824 io_in_it_desc.bTerminalID = i++;
825 if (EPOUT_EN(opts))
826 io_out_ot_desc.bTerminalID = i++;
827 if (EPIN_EN(opts))
828 usb_in_ot_desc.bTerminalID = i++;
Ruslan Bilovoleaf6cbe2021-07-12 14:55:28 +0200829 if (FUOUT_EN(opts))
830 out_feature_unit_desc->bUnitID = i++;
831 if (FUIN_EN(opts))
832 in_feature_unit_desc->bUnitID = i++;
Andreas Pape3fa4eaa2018-06-21 17:22:51 +0200833 if (EPOUT_EN(opts))
834 out_clk_src_desc.bClockID = i++;
835 if (EPIN_EN(opts))
836 in_clk_src_desc.bClockID = i++;
837
838 usb_out_it_desc.bCSourceID = out_clk_src_desc.bClockID;
Ruslan Bilovoleaf6cbe2021-07-12 14:55:28 +0200839
840 if (FUIN_EN(opts)) {
841 usb_in_ot_desc.bSourceID = in_feature_unit_desc->bUnitID;
842 in_feature_unit_desc->bSourceID = io_in_it_desc.bTerminalID;
843 } else {
844 usb_in_ot_desc.bSourceID = io_in_it_desc.bTerminalID;
845 }
846
Andreas Pape3fa4eaa2018-06-21 17:22:51 +0200847 usb_in_ot_desc.bCSourceID = in_clk_src_desc.bClockID;
848 io_in_it_desc.bCSourceID = in_clk_src_desc.bClockID;
849 io_out_ot_desc.bCSourceID = out_clk_src_desc.bClockID;
Ruslan Bilovoleaf6cbe2021-07-12 14:55:28 +0200850
851 if (FUOUT_EN(opts)) {
852 io_out_ot_desc.bSourceID = out_feature_unit_desc->bUnitID;
853 out_feature_unit_desc->bSourceID = usb_out_it_desc.bTerminalID;
854 } else {
855 io_out_ot_desc.bSourceID = usb_out_it_desc.bTerminalID;
856 }
857
Andreas Pape3fa4eaa2018-06-21 17:22:51 +0200858 as_out_hdr_desc.bTerminalLink = usb_out_it_desc.bTerminalID;
859 as_in_hdr_desc.bTerminalLink = usb_in_ot_desc.bTerminalID;
860
861 iad_desc.bInterfaceCount = 1;
Ruslan Bilovola9cf8712020-07-03 16:49:03 +0300862 ac_hdr_desc.wTotalLength = cpu_to_le16(sizeof(ac_hdr_desc));
Andreas Pape3fa4eaa2018-06-21 17:22:51 +0200863
864 if (EPIN_EN(opts)) {
865 u16 len = le16_to_cpu(ac_hdr_desc.wTotalLength);
866
867 len += sizeof(in_clk_src_desc);
868 len += sizeof(usb_in_ot_desc);
Ruslan Bilovoleaf6cbe2021-07-12 14:55:28 +0200869
870 if (FUIN_EN(opts))
871 len += in_feature_unit_desc->bLength;
872
Andreas Pape3fa4eaa2018-06-21 17:22:51 +0200873 len += sizeof(io_in_it_desc);
874 ac_hdr_desc.wTotalLength = cpu_to_le16(len);
875 iad_desc.bInterfaceCount++;
876 }
877 if (EPOUT_EN(opts)) {
878 u16 len = le16_to_cpu(ac_hdr_desc.wTotalLength);
879
880 len += sizeof(out_clk_src_desc);
881 len += sizeof(usb_out_it_desc);
Ruslan Bilovoleaf6cbe2021-07-12 14:55:28 +0200882
883 if (FUOUT_EN(opts))
884 len += out_feature_unit_desc->bLength;
885
Andreas Pape3fa4eaa2018-06-21 17:22:51 +0200886 len += sizeof(io_out_ot_desc);
887 ac_hdr_desc.wTotalLength = cpu_to_le16(len);
888 iad_desc.bInterfaceCount++;
889 }
890
Pawel Laszczakf8cb3d52021-03-10 11:52:16 +0100891 setup_headers(opts, fs_audio_desc, USB_SPEED_FULL);
892 setup_headers(opts, hs_audio_desc, USB_SPEED_HIGH);
893 setup_headers(opts, ss_audio_desc, USB_SPEED_SUPER);
Andreas Pape3fa4eaa2018-06-21 17:22:51 +0200894}
895
Ruslan Bilovol3713d5c2021-03-01 13:49:33 +0200896static int afunc_validate_opts(struct g_audio *agdev, struct device *dev)
897{
898 struct f_uac2_opts *opts = g_audio_to_uac2_opts(agdev);
899
900 if (!opts->p_chmask && !opts->c_chmask) {
901 dev_err(dev, "Error: no playback and capture channels\n");
902 return -EINVAL;
903 } else if (opts->p_chmask & ~UAC2_CHANNEL_MASK) {
904 dev_err(dev, "Error: unsupported playback channels mask\n");
905 return -EINVAL;
906 } else if (opts->c_chmask & ~UAC2_CHANNEL_MASK) {
907 dev_err(dev, "Error: unsupported capture channels mask\n");
908 return -EINVAL;
909 } else if ((opts->p_ssize < 1) || (opts->p_ssize > 4)) {
910 dev_err(dev, "Error: incorrect playback sample size\n");
911 return -EINVAL;
912 } else if ((opts->c_ssize < 1) || (opts->c_ssize > 4)) {
913 dev_err(dev, "Error: incorrect capture sample size\n");
914 return -EINVAL;
915 } else if (!opts->p_srate) {
916 dev_err(dev, "Error: incorrect playback sampling rate\n");
917 return -EINVAL;
918 } else if (!opts->c_srate) {
919 dev_err(dev, "Error: incorrect capture sampling rate\n");
920 return -EINVAL;
921 }
922
Ruslan Bilovoleaf6cbe2021-07-12 14:55:28 +0200923 if (opts->p_volume_max <= opts->p_volume_min) {
924 dev_err(dev, "Error: incorrect playback volume max/min\n");
925 return -EINVAL;
926 } else if (opts->c_volume_max <= opts->c_volume_min) {
927 dev_err(dev, "Error: incorrect capture volume max/min\n");
928 return -EINVAL;
929 } else if (opts->p_volume_res <= 0) {
930 dev_err(dev, "Error: negative/zero playback volume resolution\n");
931 return -EINVAL;
932 } else if (opts->c_volume_res <= 0) {
933 dev_err(dev, "Error: negative/zero capture volume resolution\n");
934 return -EINVAL;
935 }
936
937 if ((opts->p_volume_max - opts->p_volume_min) % opts->p_volume_res) {
938 dev_err(dev, "Error: incorrect playback volume resolution\n");
939 return -EINVAL;
940 } else if ((opts->c_volume_max - opts->c_volume_min) % opts->c_volume_res) {
941 dev_err(dev, "Error: incorrect capture volume resolution\n");
942 return -EINVAL;
943 }
944
Ruslan Bilovol3713d5c2021-03-01 13:49:33 +0200945 return 0;
946}
947
Andrzej Pietrasiewiczf8f93d22014-07-22 19:58:30 +0200948static int
Jassi Brar132fcb42012-02-02 22:01:34 +0530949afunc_bind(struct usb_configuration *cfg, struct usb_function *fn)
950{
Ruslan Bilovoleb9fecb2017-06-18 16:23:52 +0300951 struct f_uac2 *uac2 = func_to_uac2(fn);
952 struct g_audio *agdev = func_to_g_audio(fn);
Jassi Brar132fcb42012-02-02 22:01:34 +0530953 struct usb_composite_dev *cdev = cfg->cdev;
954 struct usb_gadget *gadget = cdev->gadget;
Ruslan Bilovol7158b572017-06-18 16:23:51 +0300955 struct device *dev = &gadget->dev;
Ruslan Bilovol3713d5c2021-03-01 13:49:33 +0200956 struct f_uac2_opts *uac2_opts = g_audio_to_uac2_opts(agdev);
Andrzej Pietrasiewiczf4087572014-07-22 19:58:33 +0200957 struct usb_string *us;
Jassi Brar132fcb42012-02-02 22:01:34 +0530958 int ret;
959
Ruslan Bilovol3713d5c2021-03-01 13:49:33 +0200960 ret = afunc_validate_opts(agdev, dev);
961 if (ret)
962 return ret;
Andrzej Pietrasiewiczf8f93d22014-07-22 19:58:30 +0200963
Andrzej Pietrasiewiczf4087572014-07-22 19:58:33 +0200964 us = usb_gstrings_attach(cdev, fn_strings, ARRAY_SIZE(strings_fn));
965 if (IS_ERR(us))
966 return PTR_ERR(us);
Ruslan Bilovoleaf6cbe2021-07-12 14:55:28 +0200967
968 if (FUOUT_EN(uac2_opts)) {
969 out_feature_unit_desc = build_fu_desc(uac2_opts->c_chmask);
970 if (!out_feature_unit_desc)
971 return -ENOMEM;
972 }
973 if (FUIN_EN(uac2_opts)) {
974 in_feature_unit_desc = build_fu_desc(uac2_opts->p_chmask);
975 if (!in_feature_unit_desc) {
976 ret = -ENOMEM;
977 goto err_free_fu;
978 }
979 }
980
Andrzej Pietrasiewiczf4087572014-07-22 19:58:33 +0200981 iad_desc.iFunction = us[STR_ASSOC].id;
982 std_ac_if_desc.iInterface = us[STR_IF_CTRL].id;
983 in_clk_src_desc.iClockSource = us[STR_CLKSRC_IN].id;
984 out_clk_src_desc.iClockSource = us[STR_CLKSRC_OUT].id;
985 usb_out_it_desc.iTerminal = us[STR_USB_IT].id;
986 io_in_it_desc.iTerminal = us[STR_IO_IT].id;
987 usb_in_ot_desc.iTerminal = us[STR_USB_OT].id;
988 io_out_ot_desc.iTerminal = us[STR_IO_OT].id;
989 std_as_out_if0_desc.iInterface = us[STR_AS_OUT_ALT0].id;
990 std_as_out_if1_desc.iInterface = us[STR_AS_OUT_ALT1].id;
991 std_as_in_if0_desc.iInterface = us[STR_AS_IN_ALT0].id;
992 std_as_in_if1_desc.iInterface = us[STR_AS_IN_ALT1].id;
993
Ruslan Bilovoleaf6cbe2021-07-12 14:55:28 +0200994 if (FUOUT_EN(uac2_opts)) {
Colin Ian King59e477a2021-08-04 13:59:07 +0100995 u8 *i_feature = (u8 *)out_feature_unit_desc +
996 out_feature_unit_desc->bLength - 1;
Ruslan Bilovoleaf6cbe2021-07-12 14:55:28 +0200997 *i_feature = us[STR_FU_OUT].id;
998 }
999 if (FUIN_EN(uac2_opts)) {
Colin Ian King59e477a2021-08-04 13:59:07 +01001000 u8 *i_feature = (u8 *)in_feature_unit_desc +
1001 in_feature_unit_desc->bLength - 1;
Ruslan Bilovoleaf6cbe2021-07-12 14:55:28 +02001002 *i_feature = us[STR_FU_IN].id;
1003 }
1004
Andrzej Pietrasiewiczf8f93d22014-07-22 19:58:30 +02001005
Andrzej Pietrasiewiczf8f93d22014-07-22 19:58:30 +02001006 /* Initialize the configurable parameters */
1007 usb_out_it_desc.bNrChannels = num_channels(uac2_opts->c_chmask);
1008 usb_out_it_desc.bmChannelConfig = cpu_to_le32(uac2_opts->c_chmask);
1009 io_in_it_desc.bNrChannels = num_channels(uac2_opts->p_chmask);
1010 io_in_it_desc.bmChannelConfig = cpu_to_le32(uac2_opts->p_chmask);
1011 as_out_hdr_desc.bNrChannels = num_channels(uac2_opts->c_chmask);
1012 as_out_hdr_desc.bmChannelConfig = cpu_to_le32(uac2_opts->c_chmask);
1013 as_in_hdr_desc.bNrChannels = num_channels(uac2_opts->p_chmask);
1014 as_in_hdr_desc.bmChannelConfig = cpu_to_le32(uac2_opts->p_chmask);
1015 as_out_fmt1_desc.bSubslotSize = uac2_opts->c_ssize;
1016 as_out_fmt1_desc.bBitResolution = uac2_opts->c_ssize * 8;
1017 as_in_fmt1_desc.bSubslotSize = uac2_opts->p_ssize;
1018 as_in_fmt1_desc.bBitResolution = uac2_opts->p_ssize * 8;
Ruslan Bilovoleaf6cbe2021-07-12 14:55:28 +02001019 if (FUOUT_EN(uac2_opts)) {
1020 __le32 *bma = (__le32 *)&out_feature_unit_desc->bmaControls[0];
1021 u32 control = 0;
1022
1023 if (uac2_opts->c_mute_present)
1024 control |= CONTROL_RDWR << FU_MUTE_CTRL;
1025 if (uac2_opts->c_volume_present)
1026 control |= CONTROL_RDWR << FU_VOL_CTRL;
1027 *bma = cpu_to_le32(control);
1028 }
1029 if (FUIN_EN(uac2_opts)) {
1030 __le32 *bma = (__le32 *)&in_feature_unit_desc->bmaControls[0];
1031 u32 control = 0;
1032
1033 if (uac2_opts->p_mute_present)
1034 control |= CONTROL_RDWR << FU_MUTE_CTRL;
1035 if (uac2_opts->p_volume_present)
1036 control |= CONTROL_RDWR << FU_VOL_CTRL;
1037 *bma = cpu_to_le32(control);
1038 }
Andrzej Pietrasiewiczf8f93d22014-07-22 19:58:30 +02001039
1040 snprintf(clksrc_in, sizeof(clksrc_in), "%uHz", uac2_opts->p_srate);
1041 snprintf(clksrc_out, sizeof(clksrc_out), "%uHz", uac2_opts->c_srate);
Andrzej Pietrasiewiczf8f93d22014-07-22 19:58:30 +02001042
Jassi Brar132fcb42012-02-02 22:01:34 +05301043 ret = usb_interface_id(cfg, fn);
1044 if (ret < 0) {
Daniel Macka8147da2014-08-27 19:09:04 +02001045 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
Ruslan Bilovoleaf6cbe2021-07-12 14:55:28 +02001046 goto err_free_fu;
Jassi Brar132fcb42012-02-02 22:01:34 +05301047 }
John Keeping8813a592018-01-12 18:43:32 +00001048 iad_desc.bFirstInterface = ret;
1049
Jassi Brar132fcb42012-02-02 22:01:34 +05301050 std_ac_if_desc.bInterfaceNumber = ret;
Ruslan Bilovoleb9fecb2017-06-18 16:23:52 +03001051 uac2->ac_intf = ret;
1052 uac2->ac_alt = 0;
Jassi Brar132fcb42012-02-02 22:01:34 +05301053
Andreas Pape3fa4eaa2018-06-21 17:22:51 +02001054 if (EPOUT_EN(uac2_opts)) {
1055 ret = usb_interface_id(cfg, fn);
1056 if (ret < 0) {
1057 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
Ruslan Bilovoleaf6cbe2021-07-12 14:55:28 +02001058 goto err_free_fu;
Andreas Pape3fa4eaa2018-06-21 17:22:51 +02001059 }
1060 std_as_out_if0_desc.bInterfaceNumber = ret;
1061 std_as_out_if1_desc.bInterfaceNumber = ret;
1062 uac2->as_out_intf = ret;
1063 uac2->as_out_alt = 0;
Ruslan Bilovol40c73b32021-06-04 00:01:03 +02001064
1065 if (EPOUT_FBACK_IN_EN(uac2_opts)) {
1066 fs_epout_desc.bmAttributes =
1067 USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC;
1068 hs_epout_desc.bmAttributes =
1069 USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC;
1070 ss_epout_desc.bmAttributes =
1071 USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC;
1072 std_as_out_if1_desc.bNumEndpoints++;
1073 } else {
1074 fs_epout_desc.bmAttributes =
1075 USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ADAPTIVE;
1076 hs_epout_desc.bmAttributes =
1077 USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ADAPTIVE;
1078 ss_epout_desc.bmAttributes =
1079 USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ADAPTIVE;
1080 }
Jassi Brar132fcb42012-02-02 22:01:34 +05301081 }
Jassi Brar132fcb42012-02-02 22:01:34 +05301082
Andreas Pape3fa4eaa2018-06-21 17:22:51 +02001083 if (EPIN_EN(uac2_opts)) {
1084 ret = usb_interface_id(cfg, fn);
1085 if (ret < 0) {
1086 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
Ruslan Bilovoleaf6cbe2021-07-12 14:55:28 +02001087 goto err_free_fu;
Andreas Pape3fa4eaa2018-06-21 17:22:51 +02001088 }
1089 std_as_in_if0_desc.bInterfaceNumber = ret;
1090 std_as_in_if1_desc.bInterfaceNumber = ret;
1091 uac2->as_in_intf = ret;
1092 uac2->as_in_alt = 0;
Jassi Brar132fcb42012-02-02 22:01:34 +05301093 }
Jassi Brar132fcb42012-02-02 22:01:34 +05301094
Ruslan Bilovoleaf6cbe2021-07-12 14:55:28 +02001095 if (FUOUT_EN(uac2_opts) || FUIN_EN(uac2_opts)) {
1096 uac2->int_ep = usb_ep_autoconfig(gadget, &fs_ep_int_desc);
1097 if (!uac2->int_ep) {
1098 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
1099 ret = -ENODEV;
1100 goto err_free_fu;
1101 }
1102
1103 std_ac_if_desc.bNumEndpoints = 1;
1104 }
1105
Sekhar Nori0db56e42017-05-17 13:45:17 +05301106 /* Calculate wMaxPacketSize according to audio bandwidth */
Jerome Brunet93890442020-12-21 18:35:28 +01001107 ret = set_ep_max_packet_size(uac2_opts, &fs_epin_desc, USB_SPEED_FULL,
1108 true);
1109 if (ret < 0) {
1110 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
1111 return ret;
1112 }
1113
1114 ret = set_ep_max_packet_size(uac2_opts, &fs_epout_desc, USB_SPEED_FULL,
1115 false);
1116 if (ret < 0) {
1117 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
1118 return ret;
1119 }
1120
1121 ret = set_ep_max_packet_size(uac2_opts, &hs_epin_desc, USB_SPEED_HIGH,
1122 true);
1123 if (ret < 0) {
1124 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
1125 return ret;
1126 }
1127
1128 ret = set_ep_max_packet_size(uac2_opts, &hs_epout_desc, USB_SPEED_HIGH,
1129 false);
1130 if (ret < 0) {
1131 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
1132 return ret;
1133 }
Sekhar Nori0db56e42017-05-17 13:45:17 +05301134
Pawel Laszczakf8cb3d52021-03-10 11:52:16 +01001135 ret = set_ep_max_packet_size(uac2_opts, &ss_epin_desc, USB_SPEED_SUPER,
1136 true);
1137 if (ret < 0) {
1138 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
1139 return ret;
1140 }
1141
1142 ret = set_ep_max_packet_size(uac2_opts, &ss_epout_desc, USB_SPEED_SUPER,
1143 false);
1144 if (ret < 0) {
1145 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
1146 return ret;
1147 }
1148
Andreas Pape3fa4eaa2018-06-21 17:22:51 +02001149 if (EPOUT_EN(uac2_opts)) {
1150 agdev->out_ep = usb_ep_autoconfig(gadget, &fs_epout_desc);
1151 if (!agdev->out_ep) {
1152 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
Ruslan Bilovoleaf6cbe2021-07-12 14:55:28 +02001153 ret = -ENODEV;
1154 goto err_free_fu;
Andreas Pape3fa4eaa2018-06-21 17:22:51 +02001155 }
Ruslan Bilovol40c73b32021-06-04 00:01:03 +02001156 if (EPOUT_FBACK_IN_EN(uac2_opts)) {
1157 agdev->in_ep_fback = usb_ep_autoconfig(gadget,
Ruslan Bilovol24f779d2021-06-04 00:01:02 +02001158 &fs_epin_fback_desc);
Ruslan Bilovol40c73b32021-06-04 00:01:03 +02001159 if (!agdev->in_ep_fback) {
1160 dev_err(dev, "%s:%d Error!\n",
1161 __func__, __LINE__);
Ruslan Bilovoleaf6cbe2021-07-12 14:55:28 +02001162 ret = -ENODEV;
1163 goto err_free_fu;
Ruslan Bilovol40c73b32021-06-04 00:01:03 +02001164 }
Ruslan Bilovol24f779d2021-06-04 00:01:02 +02001165 }
Sebastian Andrzej Siewior391aa852012-10-22 22:14:59 +02001166 }
Jassi Brar132fcb42012-02-02 22:01:34 +05301167
Andreas Pape3fa4eaa2018-06-21 17:22:51 +02001168 if (EPIN_EN(uac2_opts)) {
1169 agdev->in_ep = usb_ep_autoconfig(gadget, &fs_epin_desc);
1170 if (!agdev->in_ep) {
1171 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
Ruslan Bilovoleaf6cbe2021-07-12 14:55:28 +02001172 ret = -ENODEV;
1173 goto err_free_fu;
Andreas Pape3fa4eaa2018-06-21 17:22:51 +02001174 }
Sebastian Andrzej Siewior391aa852012-10-22 22:14:59 +02001175 }
Jassi Brar132fcb42012-02-02 22:01:34 +05301176
Ruslan Bilovol14e1d562017-06-25 16:23:47 +03001177 agdev->in_ep_maxpsize = max_t(u16,
1178 le16_to_cpu(fs_epin_desc.wMaxPacketSize),
1179 le16_to_cpu(hs_epin_desc.wMaxPacketSize));
1180 agdev->out_ep_maxpsize = max_t(u16,
1181 le16_to_cpu(fs_epout_desc.wMaxPacketSize),
1182 le16_to_cpu(hs_epout_desc.wMaxPacketSize));
Jassi Brareb127cb2013-05-30 18:23:33 +05301183
Pawel Laszczakf8cb3d52021-03-10 11:52:16 +01001184 agdev->in_ep_maxpsize = max_t(u16, agdev->in_ep_maxpsize,
1185 le16_to_cpu(ss_epin_desc.wMaxPacketSize));
1186 agdev->out_ep_maxpsize = max_t(u16, agdev->out_ep_maxpsize,
1187 le16_to_cpu(ss_epout_desc.wMaxPacketSize));
1188
Jack Phamf0e8a202021-09-09 10:48:11 -07001189 ss_epin_desc_comp.wBytesPerInterval = ss_epin_desc.wMaxPacketSize;
1190 ss_epout_desc_comp.wBytesPerInterval = ss_epout_desc.wMaxPacketSize;
1191
Ruslan Bilovoleaf6cbe2021-07-12 14:55:28 +02001192 // HS and SS endpoint addresses are copied from autoconfigured FS descriptors
1193 hs_ep_int_desc.bEndpointAddress = fs_ep_int_desc.bEndpointAddress;
Jassi Brar132fcb42012-02-02 22:01:34 +05301194 hs_epout_desc.bEndpointAddress = fs_epout_desc.bEndpointAddress;
Ruslan Bilovol24f779d2021-06-04 00:01:02 +02001195 hs_epin_fback_desc.bEndpointAddress = fs_epin_fback_desc.bEndpointAddress;
Jassi Brar132fcb42012-02-02 22:01:34 +05301196 hs_epin_desc.bEndpointAddress = fs_epin_desc.bEndpointAddress;
Pawel Laszczakf8cb3d52021-03-10 11:52:16 +01001197 ss_epout_desc.bEndpointAddress = fs_epout_desc.bEndpointAddress;
Ruslan Bilovol24f779d2021-06-04 00:01:02 +02001198 ss_epin_fback_desc.bEndpointAddress = fs_epin_fback_desc.bEndpointAddress;
Pawel Laszczakf8cb3d52021-03-10 11:52:16 +01001199 ss_epin_desc.bEndpointAddress = fs_epin_desc.bEndpointAddress;
Ruslan Bilovoleaf6cbe2021-07-12 14:55:28 +02001200 ss_ep_int_desc.bEndpointAddress = fs_ep_int_desc.bEndpointAddress;
Jassi Brar132fcb42012-02-02 22:01:34 +05301201
Andreas Pape3fa4eaa2018-06-21 17:22:51 +02001202 setup_descriptor(uac2_opts);
1203
Pawel Laszczakf8cb3d52021-03-10 11:52:16 +01001204 ret = usb_assign_descriptors(fn, fs_audio_desc, hs_audio_desc, ss_audio_desc,
1205 ss_audio_desc);
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +02001206 if (ret)
Ruslan Bilovoleaf6cbe2021-07-12 14:55:28 +02001207 goto err_free_fu;
Jassi Brar132fcb42012-02-02 22:01:34 +05301208
Ruslan Bilovol7158b572017-06-18 16:23:51 +03001209 agdev->gadget = gadget;
1210
Ruslan Bilovoleb9fecb2017-06-18 16:23:52 +03001211 agdev->params.p_chmask = uac2_opts->p_chmask;
1212 agdev->params.p_srate = uac2_opts->p_srate;
1213 agdev->params.p_ssize = uac2_opts->p_ssize;
Ruslan Bilovoleaf6cbe2021-07-12 14:55:28 +02001214 if (FUIN_EN(uac2_opts)) {
1215 agdev->params.p_fu.id = USB_IN_FU_ID;
1216 agdev->params.p_fu.mute_present = uac2_opts->p_mute_present;
1217 agdev->params.p_fu.volume_present = uac2_opts->p_volume_present;
1218 agdev->params.p_fu.volume_min = uac2_opts->p_volume_min;
1219 agdev->params.p_fu.volume_max = uac2_opts->p_volume_max;
1220 agdev->params.p_fu.volume_res = uac2_opts->p_volume_res;
1221 }
Ruslan Bilovoleb9fecb2017-06-18 16:23:52 +03001222 agdev->params.c_chmask = uac2_opts->c_chmask;
1223 agdev->params.c_srate = uac2_opts->c_srate;
1224 agdev->params.c_ssize = uac2_opts->c_ssize;
Ruslan Bilovoleaf6cbe2021-07-12 14:55:28 +02001225 if (FUOUT_EN(uac2_opts)) {
1226 agdev->params.c_fu.id = USB_OUT_FU_ID;
1227 agdev->params.c_fu.mute_present = uac2_opts->c_mute_present;
1228 agdev->params.c_fu.volume_present = uac2_opts->c_volume_present;
1229 agdev->params.c_fu.volume_min = uac2_opts->c_volume_min;
1230 agdev->params.c_fu.volume_max = uac2_opts->c_volume_max;
1231 agdev->params.c_fu.volume_res = uac2_opts->c_volume_res;
1232 }
Ruslan Bilovoleb9fecb2017-06-18 16:23:52 +03001233 agdev->params.req_number = uac2_opts->req_number;
Ruslan Bilovole89bb422021-06-04 00:01:04 +02001234 agdev->params.fb_max = uac2_opts->fb_max;
Ruslan Bilovoleaf6cbe2021-07-12 14:55:28 +02001235
1236 if (FUOUT_EN(uac2_opts) || FUIN_EN(uac2_opts))
1237 agdev->notify = afunc_notify;
1238
Ruslan Bilovoleb9fecb2017-06-18 16:23:52 +03001239 ret = g_audio_setup(agdev, "UAC2 PCM", "UAC2_Gadget");
Sebastian Andrzej Siewior391aa852012-10-22 22:14:59 +02001240 if (ret)
Ruslan Bilovoleb9fecb2017-06-18 16:23:52 +03001241 goto err_free_descs;
Ruslan Bilovoleaf6cbe2021-07-12 14:55:28 +02001242
Sebastian Andrzej Siewior391aa852012-10-22 22:14:59 +02001243 return 0;
Pavitrakumar Managutted12a8722014-10-22 19:24:58 +05301244
Peter Chenf1d38612016-11-08 10:10:44 +08001245err_free_descs:
1246 usb_free_all_descriptors(fn);
Ruslan Bilovol7158b572017-06-18 16:23:51 +03001247 agdev->gadget = NULL;
Ruslan Bilovoleaf6cbe2021-07-12 14:55:28 +02001248err_free_fu:
1249 kfree(out_feature_unit_desc);
1250 out_feature_unit_desc = NULL;
1251 kfree(in_feature_unit_desc);
1252 in_feature_unit_desc = NULL;
1253 return ret;
1254}
1255
1256static void
1257afunc_notify_complete(struct usb_ep *_ep, struct usb_request *req)
1258{
1259 struct g_audio *agdev = req->context;
1260 struct f_uac2 *uac2 = func_to_uac2(&agdev->func);
1261
1262 atomic_dec(&uac2->int_count);
1263 kfree(req->buf);
1264 usb_ep_free_request(_ep, req);
1265}
1266
1267static int
1268afunc_notify(struct g_audio *agdev, int unit_id, int cs)
1269{
1270 struct f_uac2 *uac2 = func_to_uac2(&agdev->func);
1271 struct usb_request *req;
1272 struct uac2_interrupt_data_msg *msg;
1273 u16 w_index, w_value;
1274 int ret;
1275
1276 if (!uac2->int_ep->enabled)
1277 return 0;
1278
1279 if (atomic_inc_return(&uac2->int_count) > UAC2_DEF_INT_REQ_NUM) {
1280 atomic_dec(&uac2->int_count);
1281 return 0;
1282 }
1283
1284 req = usb_ep_alloc_request(uac2->int_ep, GFP_ATOMIC);
1285 if (req == NULL) {
1286 ret = -ENOMEM;
1287 goto err_dec_int_count;
1288 }
1289
1290 msg = kzalloc(sizeof(*msg), GFP_ATOMIC);
1291 if (msg == NULL) {
1292 ret = -ENOMEM;
1293 goto err_free_request;
1294 }
1295
1296 w_index = unit_id << 8 | uac2->ac_intf;
1297 w_value = cs << 8;
1298
1299 msg->bInfo = 0; /* Non-vendor, interface interrupt */
1300 msg->bAttribute = UAC2_CS_CUR;
1301 msg->wIndex = cpu_to_le16(w_index);
1302 msg->wValue = cpu_to_le16(w_value);
1303
1304 req->length = sizeof(*msg);
1305 req->buf = msg;
1306 req->context = agdev;
1307 req->complete = afunc_notify_complete;
1308
1309 ret = usb_ep_queue(uac2->int_ep, req, GFP_ATOMIC);
1310
1311 if (ret)
1312 goto err_free_msg;
1313
1314 return 0;
1315
1316err_free_msg:
1317 kfree(msg);
1318err_free_request:
1319 usb_ep_free_request(uac2->int_ep, req);
1320err_dec_int_count:
1321 atomic_dec(&uac2->int_count);
1322
Peter Chen88f950a2017-01-04 10:19:22 +08001323 return ret;
Jassi Brar132fcb42012-02-02 22:01:34 +05301324}
1325
Jassi Brar132fcb42012-02-02 22:01:34 +05301326static int
1327afunc_set_alt(struct usb_function *fn, unsigned intf, unsigned alt)
1328{
1329 struct usb_composite_dev *cdev = fn->config->cdev;
Ruslan Bilovoleb9fecb2017-06-18 16:23:52 +03001330 struct f_uac2 *uac2 = func_to_uac2(fn);
Ruslan Bilovoleaf6cbe2021-07-12 14:55:28 +02001331 struct g_audio *agdev = func_to_g_audio(fn);
Jassi Brar132fcb42012-02-02 22:01:34 +05301332 struct usb_gadget *gadget = cdev->gadget;
Ruslan Bilovol7158b572017-06-18 16:23:51 +03001333 struct device *dev = &gadget->dev;
Ruslan Bilovoleb9fecb2017-06-18 16:23:52 +03001334 int ret = 0;
Jassi Brar132fcb42012-02-02 22:01:34 +05301335
1336 /* No i/f has more than 2 alt settings */
1337 if (alt > 1) {
Daniel Macka8147da2014-08-27 19:09:04 +02001338 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
Jassi Brar132fcb42012-02-02 22:01:34 +05301339 return -EINVAL;
1340 }
1341
Ruslan Bilovoleb9fecb2017-06-18 16:23:52 +03001342 if (intf == uac2->ac_intf) {
Jassi Brar132fcb42012-02-02 22:01:34 +05301343 /* Control I/f has only 1 AltSetting - 0 */
1344 if (alt) {
Daniel Macka8147da2014-08-27 19:09:04 +02001345 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
Jassi Brar132fcb42012-02-02 22:01:34 +05301346 return -EINVAL;
1347 }
Ruslan Bilovoleaf6cbe2021-07-12 14:55:28 +02001348
1349 /* restart interrupt endpoint */
1350 if (uac2->int_ep) {
1351 usb_ep_disable(uac2->int_ep);
1352 config_ep_by_speed(gadget, &agdev->func, uac2->int_ep);
1353 usb_ep_enable(uac2->int_ep);
1354 }
1355
Jassi Brar132fcb42012-02-02 22:01:34 +05301356 return 0;
1357 }
1358
Ruslan Bilovoleb9fecb2017-06-18 16:23:52 +03001359 if (intf == uac2->as_out_intf) {
1360 uac2->as_out_alt = alt;
Daniel Mack9bb87f12014-08-27 19:09:07 +02001361
Ruslan Bilovoleb9fecb2017-06-18 16:23:52 +03001362 if (alt)
1363 ret = u_audio_start_capture(&uac2->g_audio);
Daniel Mack9bb87f12014-08-27 19:09:07 +02001364 else
Ruslan Bilovoleb9fecb2017-06-18 16:23:52 +03001365 u_audio_stop_capture(&uac2->g_audio);
1366 } else if (intf == uac2->as_in_intf) {
1367 uac2->as_in_alt = alt;
Daniel Mack9bb87f12014-08-27 19:09:07 +02001368
Ruslan Bilovoleb9fecb2017-06-18 16:23:52 +03001369 if (alt)
1370 ret = u_audio_start_playback(&uac2->g_audio);
1371 else
1372 u_audio_stop_playback(&uac2->g_audio);
Jassi Brar132fcb42012-02-02 22:01:34 +05301373 } else {
Daniel Macka8147da2014-08-27 19:09:04 +02001374 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
Jassi Brar132fcb42012-02-02 22:01:34 +05301375 return -EINVAL;
1376 }
1377
Ruslan Bilovoleb9fecb2017-06-18 16:23:52 +03001378 return ret;
Jassi Brar132fcb42012-02-02 22:01:34 +05301379}
1380
1381static int
1382afunc_get_alt(struct usb_function *fn, unsigned intf)
1383{
Ruslan Bilovoleb9fecb2017-06-18 16:23:52 +03001384 struct f_uac2 *uac2 = func_to_uac2(fn);
1385 struct g_audio *agdev = func_to_g_audio(fn);
Jassi Brar132fcb42012-02-02 22:01:34 +05301386
Ruslan Bilovoleb9fecb2017-06-18 16:23:52 +03001387 if (intf == uac2->ac_intf)
1388 return uac2->ac_alt;
1389 else if (intf == uac2->as_out_intf)
1390 return uac2->as_out_alt;
1391 else if (intf == uac2->as_in_intf)
1392 return uac2->as_in_alt;
Jassi Brar132fcb42012-02-02 22:01:34 +05301393 else
Ruslan Bilovol7158b572017-06-18 16:23:51 +03001394 dev_err(&agdev->gadget->dev,
Jassi Brar132fcb42012-02-02 22:01:34 +05301395 "%s:%d Invalid Interface %d!\n",
1396 __func__, __LINE__, intf);
1397
1398 return -EINVAL;
1399}
1400
1401static void
1402afunc_disable(struct usb_function *fn)
1403{
Ruslan Bilovoleb9fecb2017-06-18 16:23:52 +03001404 struct f_uac2 *uac2 = func_to_uac2(fn);
Jassi Brar132fcb42012-02-02 22:01:34 +05301405
Ruslan Bilovoleb9fecb2017-06-18 16:23:52 +03001406 uac2->as_in_alt = 0;
1407 uac2->as_out_alt = 0;
1408 u_audio_stop_capture(&uac2->g_audio);
1409 u_audio_stop_playback(&uac2->g_audio);
Ruslan Bilovoleaf6cbe2021-07-12 14:55:28 +02001410 if (uac2->int_ep)
1411 usb_ep_disable(uac2->int_ep);
Jassi Brar132fcb42012-02-02 22:01:34 +05301412}
1413
1414static int
1415in_rq_cur(struct usb_function *fn, const struct usb_ctrlrequest *cr)
1416{
1417 struct usb_request *req = fn->config->cdev->req;
Ruslan Bilovoleb9fecb2017-06-18 16:23:52 +03001418 struct g_audio *agdev = func_to_g_audio(fn);
Ruslan Bilovoleaf6cbe2021-07-12 14:55:28 +02001419 struct f_uac2_opts *opts = g_audio_to_uac2_opts(agdev);
Jassi Brar132fcb42012-02-02 22:01:34 +05301420 u16 w_length = le16_to_cpu(cr->wLength);
1421 u16 w_index = le16_to_cpu(cr->wIndex);
1422 u16 w_value = le16_to_cpu(cr->wValue);
1423 u8 entity_id = (w_index >> 8) & 0xff;
1424 u8 control_selector = w_value >> 8;
1425 int value = -EOPNOTSUPP;
Andrzej Pietrasiewiczf8f93d22014-07-22 19:58:30 +02001426 int p_srate, c_srate;
1427
Andrzej Pietrasiewiczf8f93d22014-07-22 19:58:30 +02001428 p_srate = opts->p_srate;
1429 c_srate = opts->c_srate;
Jassi Brar132fcb42012-02-02 22:01:34 +05301430
Ruslan Bilovoleaf6cbe2021-07-12 14:55:28 +02001431 if ((entity_id == USB_IN_CLK_ID) || (entity_id == USB_OUT_CLK_ID)) {
1432 if (control_selector == UAC2_CS_CONTROL_SAM_FREQ) {
1433 struct cntrl_cur_lay3 c;
Jassi Brar132fcb42012-02-02 22:01:34 +05301434
Ruslan Bilovoleaf6cbe2021-07-12 14:55:28 +02001435 memset(&c, 0, sizeof(struct cntrl_cur_lay3));
Jassi Brar132fcb42012-02-02 22:01:34 +05301436
Ruslan Bilovoleaf6cbe2021-07-12 14:55:28 +02001437 if (entity_id == USB_IN_CLK_ID)
1438 c.dCUR = cpu_to_le32(p_srate);
1439 else if (entity_id == USB_OUT_CLK_ID)
1440 c.dCUR = cpu_to_le32(c_srate);
1441
1442 value = min_t(unsigned int, w_length, sizeof(c));
1443 memcpy(req->buf, &c, value);
1444 } else if (control_selector == UAC2_CS_CONTROL_CLOCK_VALID) {
1445 *(u8 *)req->buf = 1;
1446 value = min_t(unsigned int, w_length, 1);
1447 } else {
1448 dev_err(&agdev->gadget->dev,
1449 "%s:%d control_selector=%d TODO!\n",
1450 __func__, __LINE__, control_selector);
1451 }
1452 } else if ((FUIN_EN(opts) && (entity_id == USB_IN_FU_ID)) ||
1453 (FUOUT_EN(opts) && (entity_id == USB_OUT_FU_ID))) {
1454 unsigned int is_playback = 0;
1455
1456 if (FUIN_EN(opts) && (entity_id == USB_IN_FU_ID))
1457 is_playback = 1;
1458
1459 if (control_selector == UAC_FU_MUTE) {
1460 unsigned int mute;
1461
1462 u_audio_get_mute(agdev, is_playback, &mute);
1463
1464 *(u8 *)req->buf = mute;
1465 value = min_t(unsigned int, w_length, 1);
1466 } else if (control_selector == UAC_FU_VOLUME) {
1467 struct cntrl_cur_lay2 c;
1468 s16 volume;
1469
1470 memset(&c, 0, sizeof(struct cntrl_cur_lay2));
1471
1472 u_audio_get_volume(agdev, is_playback, &volume);
1473 c.wCUR = cpu_to_le16(volume);
1474
1475 value = min_t(unsigned int, w_length, sizeof(c));
1476 memcpy(req->buf, &c, value);
1477 } else {
1478 dev_err(&agdev->gadget->dev,
1479 "%s:%d control_selector=%d TODO!\n",
1480 __func__, __LINE__, control_selector);
1481 }
Jassi Brar132fcb42012-02-02 22:01:34 +05301482 } else {
Ruslan Bilovol7158b572017-06-18 16:23:51 +03001483 dev_err(&agdev->gadget->dev,
Ruslan Bilovoleaf6cbe2021-07-12 14:55:28 +02001484 "%s:%d entity_id=%d control_selector=%d TODO!\n",
1485 __func__, __LINE__, entity_id, control_selector);
Jassi Brar132fcb42012-02-02 22:01:34 +05301486 }
1487
1488 return value;
1489}
1490
1491static int
1492in_rq_range(struct usb_function *fn, const struct usb_ctrlrequest *cr)
1493{
1494 struct usb_request *req = fn->config->cdev->req;
Ruslan Bilovoleb9fecb2017-06-18 16:23:52 +03001495 struct g_audio *agdev = func_to_g_audio(fn);
Ruslan Bilovoleaf6cbe2021-07-12 14:55:28 +02001496 struct f_uac2_opts *opts = g_audio_to_uac2_opts(agdev);
Jassi Brar132fcb42012-02-02 22:01:34 +05301497 u16 w_length = le16_to_cpu(cr->wLength);
1498 u16 w_index = le16_to_cpu(cr->wIndex);
1499 u16 w_value = le16_to_cpu(cr->wValue);
1500 u8 entity_id = (w_index >> 8) & 0xff;
1501 u8 control_selector = w_value >> 8;
Jassi Brar132fcb42012-02-02 22:01:34 +05301502 int value = -EOPNOTSUPP;
Andrzej Pietrasiewiczf8f93d22014-07-22 19:58:30 +02001503 int p_srate, c_srate;
1504
Andrzej Pietrasiewiczf8f93d22014-07-22 19:58:30 +02001505 p_srate = opts->p_srate;
1506 c_srate = opts->c_srate;
Jassi Brar132fcb42012-02-02 22:01:34 +05301507
Ruslan Bilovoleaf6cbe2021-07-12 14:55:28 +02001508 if ((entity_id == USB_IN_CLK_ID) || (entity_id == USB_OUT_CLK_ID)) {
1509 if (control_selector == UAC2_CS_CONTROL_SAM_FREQ) {
1510 struct cntrl_range_lay3 r;
Jassi Brar132fcb42012-02-02 22:01:34 +05301511
Ruslan Bilovoleaf6cbe2021-07-12 14:55:28 +02001512 if (entity_id == USB_IN_CLK_ID)
1513 r.dMIN = cpu_to_le32(p_srate);
1514 else if (entity_id == USB_OUT_CLK_ID)
1515 r.dMIN = cpu_to_le32(c_srate);
1516 else
1517 return -EOPNOTSUPP;
Jassi Brar132fcb42012-02-02 22:01:34 +05301518
Ruslan Bilovoleaf6cbe2021-07-12 14:55:28 +02001519 r.dMAX = r.dMIN;
1520 r.dRES = 0;
1521 r.wNumSubRanges = cpu_to_le16(1);
1522
1523 value = min_t(unsigned int, w_length, sizeof(r));
1524 memcpy(req->buf, &r, value);
1525 } else {
1526 dev_err(&agdev->gadget->dev,
1527 "%s:%d control_selector=%d TODO!\n",
1528 __func__, __LINE__, control_selector);
1529 }
1530 } else if ((FUIN_EN(opts) && (entity_id == USB_IN_FU_ID)) ||
1531 (FUOUT_EN(opts) && (entity_id == USB_OUT_FU_ID))) {
1532 unsigned int is_playback = 0;
1533
1534 if (FUIN_EN(opts) && (entity_id == USB_IN_FU_ID))
1535 is_playback = 1;
1536
1537 if (control_selector == UAC_FU_VOLUME) {
1538 struct cntrl_range_lay2 r;
1539 s16 max_db, min_db, res_db;
1540
1541 if (is_playback) {
1542 max_db = opts->p_volume_max;
1543 min_db = opts->p_volume_min;
1544 res_db = opts->p_volume_res;
1545 } else {
1546 max_db = opts->c_volume_max;
1547 min_db = opts->c_volume_min;
1548 res_db = opts->c_volume_res;
1549 }
1550
1551 r.wMAX = cpu_to_le16(max_db);
1552 r.wMIN = cpu_to_le16(min_db);
1553 r.wRES = cpu_to_le16(res_db);
1554 r.wNumSubRanges = cpu_to_le16(1);
1555
1556 value = min_t(unsigned int, w_length, sizeof(r));
1557 memcpy(req->buf, &r, value);
1558 } else {
1559 dev_err(&agdev->gadget->dev,
1560 "%s:%d control_selector=%d TODO!\n",
1561 __func__, __LINE__, control_selector);
1562 }
Jassi Brar132fcb42012-02-02 22:01:34 +05301563 } else {
Ruslan Bilovol7158b572017-06-18 16:23:51 +03001564 dev_err(&agdev->gadget->dev,
Ruslan Bilovoleaf6cbe2021-07-12 14:55:28 +02001565 "%s:%d entity_id=%d control_selector=%d TODO!\n",
1566 __func__, __LINE__, entity_id, control_selector);
Jassi Brar132fcb42012-02-02 22:01:34 +05301567 }
1568
1569 return value;
1570}
1571
1572static int
1573ac_rq_in(struct usb_function *fn, const struct usb_ctrlrequest *cr)
1574{
1575 if (cr->bRequest == UAC2_CS_CUR)
1576 return in_rq_cur(fn, cr);
1577 else if (cr->bRequest == UAC2_CS_RANGE)
1578 return in_rq_range(fn, cr);
1579 else
1580 return -EOPNOTSUPP;
1581}
1582
Ruslan Bilovoleaf6cbe2021-07-12 14:55:28 +02001583static void
1584out_rq_cur_complete(struct usb_ep *ep, struct usb_request *req)
1585{
1586 struct g_audio *agdev = req->context;
1587 struct usb_composite_dev *cdev = agdev->func.config->cdev;
1588 struct f_uac2_opts *opts = g_audio_to_uac2_opts(agdev);
1589 struct f_uac2 *uac2 = func_to_uac2(&agdev->func);
1590 struct usb_ctrlrequest *cr = &uac2->setup_cr;
1591 u16 w_index = le16_to_cpu(cr->wIndex);
1592 u16 w_value = le16_to_cpu(cr->wValue);
1593 u8 entity_id = (w_index >> 8) & 0xff;
1594 u8 control_selector = w_value >> 8;
1595
1596 if (req->status != 0) {
1597 dev_dbg(&cdev->gadget->dev, "completion err %d\n", req->status);
1598 return;
1599 }
1600
1601 if ((FUIN_EN(opts) && (entity_id == USB_IN_FU_ID)) ||
1602 (FUOUT_EN(opts) && (entity_id == USB_OUT_FU_ID))) {
1603 unsigned int is_playback = 0;
1604
1605 if (FUIN_EN(opts) && (entity_id == USB_IN_FU_ID))
1606 is_playback = 1;
1607
1608 if (control_selector == UAC_FU_MUTE) {
1609 u8 mute = *(u8 *)req->buf;
1610
1611 u_audio_set_mute(agdev, is_playback, mute);
1612
1613 return;
1614 } else if (control_selector == UAC_FU_VOLUME) {
1615 struct cntrl_cur_lay2 *c = req->buf;
1616 s16 volume;
1617
1618 volume = le16_to_cpu(c->wCUR);
1619 u_audio_set_volume(agdev, is_playback, volume);
1620
1621 return;
1622 } else {
1623 dev_err(&agdev->gadget->dev,
1624 "%s:%d control_selector=%d TODO!\n",
1625 __func__, __LINE__, control_selector);
1626 usb_ep_set_halt(ep);
1627 }
1628 }
1629}
1630
Jassi Brar132fcb42012-02-02 22:01:34 +05301631static int
1632out_rq_cur(struct usb_function *fn, const struct usb_ctrlrequest *cr)
1633{
Ruslan Bilovoleaf6cbe2021-07-12 14:55:28 +02001634 struct usb_request *req = fn->config->cdev->req;
1635 struct g_audio *agdev = func_to_g_audio(fn);
1636 struct f_uac2_opts *opts = g_audio_to_uac2_opts(agdev);
1637 struct f_uac2 *uac2 = func_to_uac2(fn);
Jassi Brar132fcb42012-02-02 22:01:34 +05301638 u16 w_length = le16_to_cpu(cr->wLength);
Ruslan Bilovoleaf6cbe2021-07-12 14:55:28 +02001639 u16 w_index = le16_to_cpu(cr->wIndex);
Jassi Brar132fcb42012-02-02 22:01:34 +05301640 u16 w_value = le16_to_cpu(cr->wValue);
Ruslan Bilovoleaf6cbe2021-07-12 14:55:28 +02001641 u8 entity_id = (w_index >> 8) & 0xff;
Jassi Brar132fcb42012-02-02 22:01:34 +05301642 u8 control_selector = w_value >> 8;
1643
Ruslan Bilovoleaf6cbe2021-07-12 14:55:28 +02001644 if ((entity_id == USB_IN_CLK_ID) || (entity_id == USB_OUT_CLK_ID)) {
1645 if (control_selector == UAC2_CS_CONTROL_SAM_FREQ)
1646 return w_length;
1647 } else if ((FUIN_EN(opts) && (entity_id == USB_IN_FU_ID)) ||
1648 (FUOUT_EN(opts) && (entity_id == USB_OUT_FU_ID))) {
1649 memcpy(&uac2->setup_cr, cr, sizeof(*cr));
1650 req->context = agdev;
1651 req->complete = out_rq_cur_complete;
Jassi Brar132fcb42012-02-02 22:01:34 +05301652
Ruslan Bilovoleaf6cbe2021-07-12 14:55:28 +02001653 return w_length;
1654 } else {
1655 dev_err(&agdev->gadget->dev,
1656 "%s:%d entity_id=%d control_selector=%d TODO!\n",
1657 __func__, __LINE__, entity_id, control_selector);
1658 }
Jassi Brar132fcb42012-02-02 22:01:34 +05301659 return -EOPNOTSUPP;
1660}
1661
1662static int
1663setup_rq_inf(struct usb_function *fn, const struct usb_ctrlrequest *cr)
1664{
Ruslan Bilovoleb9fecb2017-06-18 16:23:52 +03001665 struct f_uac2 *uac2 = func_to_uac2(fn);
1666 struct g_audio *agdev = func_to_g_audio(fn);
Jassi Brar132fcb42012-02-02 22:01:34 +05301667 u16 w_index = le16_to_cpu(cr->wIndex);
1668 u8 intf = w_index & 0xff;
1669
Ruslan Bilovoleb9fecb2017-06-18 16:23:52 +03001670 if (intf != uac2->ac_intf) {
Ruslan Bilovol7158b572017-06-18 16:23:51 +03001671 dev_err(&agdev->gadget->dev,
Jassi Brar132fcb42012-02-02 22:01:34 +05301672 "%s:%d Error!\n", __func__, __LINE__);
1673 return -EOPNOTSUPP;
1674 }
1675
1676 if (cr->bRequestType & USB_DIR_IN)
1677 return ac_rq_in(fn, cr);
1678 else if (cr->bRequest == UAC2_CS_CUR)
1679 return out_rq_cur(fn, cr);
1680
1681 return -EOPNOTSUPP;
1682}
1683
1684static int
1685afunc_setup(struct usb_function *fn, const struct usb_ctrlrequest *cr)
1686{
1687 struct usb_composite_dev *cdev = fn->config->cdev;
Ruslan Bilovoleb9fecb2017-06-18 16:23:52 +03001688 struct g_audio *agdev = func_to_g_audio(fn);
Jassi Brar132fcb42012-02-02 22:01:34 +05301689 struct usb_request *req = cdev->req;
1690 u16 w_length = le16_to_cpu(cr->wLength);
1691 int value = -EOPNOTSUPP;
1692
1693 /* Only Class specific requests are supposed to reach here */
1694 if ((cr->bRequestType & USB_TYPE_MASK) != USB_TYPE_CLASS)
1695 return -EOPNOTSUPP;
1696
1697 if ((cr->bRequestType & USB_RECIP_MASK) == USB_RECIP_INTERFACE)
1698 value = setup_rq_inf(fn, cr);
1699 else
Ruslan Bilovol7158b572017-06-18 16:23:51 +03001700 dev_err(&agdev->gadget->dev, "%s:%d Error!\n",
1701 __func__, __LINE__);
Jassi Brar132fcb42012-02-02 22:01:34 +05301702
1703 if (value >= 0) {
1704 req->length = value;
1705 req->zero = value < w_length;
1706 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
1707 if (value < 0) {
Ruslan Bilovol7158b572017-06-18 16:23:51 +03001708 dev_err(&agdev->gadget->dev,
Jassi Brar132fcb42012-02-02 22:01:34 +05301709 "%s:%d Error!\n", __func__, __LINE__);
1710 req->status = 0;
1711 }
1712 }
1713
1714 return value;
1715}
1716
Andrzej Pietrasiewicz3aeea3c2014-07-22 19:58:35 +02001717static inline struct f_uac2_opts *to_f_uac2_opts(struct config_item *item)
1718{
1719 return container_of(to_config_group(item), struct f_uac2_opts,
1720 func_inst.group);
1721}
1722
Andrzej Pietrasiewicz3aeea3c2014-07-22 19:58:35 +02001723static void f_uac2_attr_release(struct config_item *item)
1724{
1725 struct f_uac2_opts *opts = to_f_uac2_opts(item);
1726
1727 usb_put_function_instance(&opts->func_inst);
1728}
1729
1730static struct configfs_item_operations f_uac2_item_ops = {
1731 .release = f_uac2_attr_release,
Andrzej Pietrasiewicz3aeea3c2014-07-22 19:58:35 +02001732};
1733
Ruslan Bilovoleaf6cbe2021-07-12 14:55:28 +02001734#define uac2_kstrtou32 kstrtou32
1735#define uac2_kstrtos16 kstrtos16
1736#define uac2_kstrtobool(s, base, res) kstrtobool((s), (res))
1737
1738static const char *u32_fmt = "%u\n";
1739static const char *s16_fmt = "%hd\n";
1740static const char *bool_fmt = "%u\n";
1741
1742#define UAC2_ATTRIBUTE(type, name) \
Christoph Hellwig495702b2015-10-03 15:32:49 +02001743static ssize_t f_uac2_opts_##name##_show(struct config_item *item, \
Andrzej Pietrasiewicz3aeea3c2014-07-22 19:58:35 +02001744 char *page) \
1745{ \
Christoph Hellwig495702b2015-10-03 15:32:49 +02001746 struct f_uac2_opts *opts = to_f_uac2_opts(item); \
Andrzej Pietrasiewicz3aeea3c2014-07-22 19:58:35 +02001747 int result; \
1748 \
1749 mutex_lock(&opts->lock); \
Ruslan Bilovoleaf6cbe2021-07-12 14:55:28 +02001750 result = sprintf(page, type##_fmt, opts->name); \
Andrzej Pietrasiewicz3aeea3c2014-07-22 19:58:35 +02001751 mutex_unlock(&opts->lock); \
1752 \
1753 return result; \
1754} \
1755 \
Christoph Hellwig495702b2015-10-03 15:32:49 +02001756static ssize_t f_uac2_opts_##name##_store(struct config_item *item, \
Andrzej Pietrasiewicz3aeea3c2014-07-22 19:58:35 +02001757 const char *page, size_t len) \
1758{ \
Christoph Hellwig495702b2015-10-03 15:32:49 +02001759 struct f_uac2_opts *opts = to_f_uac2_opts(item); \
Andrzej Pietrasiewicz3aeea3c2014-07-22 19:58:35 +02001760 int ret; \
Ruslan Bilovoleaf6cbe2021-07-12 14:55:28 +02001761 type num; \
Andrzej Pietrasiewicz3aeea3c2014-07-22 19:58:35 +02001762 \
1763 mutex_lock(&opts->lock); \
1764 if (opts->refcnt) { \
1765 ret = -EBUSY; \
1766 goto end; \
1767 } \
1768 \
Ruslan Bilovoleaf6cbe2021-07-12 14:55:28 +02001769 ret = uac2_kstrto##type(page, 0, &num); \
Andrzej Pietrasiewicz3aeea3c2014-07-22 19:58:35 +02001770 if (ret) \
1771 goto end; \
1772 \
1773 opts->name = num; \
1774 ret = len; \
1775 \
1776end: \
1777 mutex_unlock(&opts->lock); \
1778 return ret; \
1779} \
1780 \
Christoph Hellwig495702b2015-10-03 15:32:49 +02001781CONFIGFS_ATTR(f_uac2_opts_, name)
Andrzej Pietrasiewicz3aeea3c2014-07-22 19:58:35 +02001782
Ruslan Bilovol40c73b32021-06-04 00:01:03 +02001783#define UAC2_ATTRIBUTE_SYNC(name) \
1784static ssize_t f_uac2_opts_##name##_show(struct config_item *item, \
1785 char *page) \
1786{ \
1787 struct f_uac2_opts *opts = to_f_uac2_opts(item); \
1788 int result; \
1789 char *str; \
1790 \
1791 mutex_lock(&opts->lock); \
1792 switch (opts->name) { \
1793 case USB_ENDPOINT_SYNC_ASYNC: \
1794 str = "async"; \
1795 break; \
1796 case USB_ENDPOINT_SYNC_ADAPTIVE: \
1797 str = "adaptive"; \
1798 break; \
1799 default: \
1800 str = "unknown"; \
1801 break; \
1802 } \
1803 result = sprintf(page, "%s\n", str); \
1804 mutex_unlock(&opts->lock); \
1805 \
1806 return result; \
1807} \
1808 \
1809static ssize_t f_uac2_opts_##name##_store(struct config_item *item, \
1810 const char *page, size_t len) \
1811{ \
1812 struct f_uac2_opts *opts = to_f_uac2_opts(item); \
1813 int ret = 0; \
1814 \
1815 mutex_lock(&opts->lock); \
1816 if (opts->refcnt) { \
1817 ret = -EBUSY; \
1818 goto end; \
1819 } \
1820 \
1821 if (!strncmp(page, "async", 5)) \
1822 opts->name = USB_ENDPOINT_SYNC_ASYNC; \
1823 else if (!strncmp(page, "adaptive", 8)) \
1824 opts->name = USB_ENDPOINT_SYNC_ADAPTIVE; \
1825 else { \
1826 ret = -EINVAL; \
1827 goto end; \
1828 } \
1829 \
1830 ret = len; \
1831 \
1832end: \
1833 mutex_unlock(&opts->lock); \
1834 return ret; \
1835} \
1836 \
1837CONFIGFS_ATTR(f_uac2_opts_, name)
1838
Ruslan Bilovoleaf6cbe2021-07-12 14:55:28 +02001839UAC2_ATTRIBUTE(u32, p_chmask);
1840UAC2_ATTRIBUTE(u32, p_srate);
1841UAC2_ATTRIBUTE(u32, p_ssize);
1842UAC2_ATTRIBUTE(u32, c_chmask);
1843UAC2_ATTRIBUTE(u32, c_srate);
Ruslan Bilovol40c73b32021-06-04 00:01:03 +02001844UAC2_ATTRIBUTE_SYNC(c_sync);
Ruslan Bilovoleaf6cbe2021-07-12 14:55:28 +02001845UAC2_ATTRIBUTE(u32, c_ssize);
1846UAC2_ATTRIBUTE(u32, req_number);
1847
1848UAC2_ATTRIBUTE(bool, p_mute_present);
1849UAC2_ATTRIBUTE(bool, p_volume_present);
1850UAC2_ATTRIBUTE(s16, p_volume_min);
1851UAC2_ATTRIBUTE(s16, p_volume_max);
1852UAC2_ATTRIBUTE(s16, p_volume_res);
1853
1854UAC2_ATTRIBUTE(bool, c_mute_present);
1855UAC2_ATTRIBUTE(bool, c_volume_present);
1856UAC2_ATTRIBUTE(s16, c_volume_min);
1857UAC2_ATTRIBUTE(s16, c_volume_max);
1858UAC2_ATTRIBUTE(s16, c_volume_res);
1859UAC2_ATTRIBUTE(u32, fb_max);
Andrzej Pietrasiewicz3aeea3c2014-07-22 19:58:35 +02001860
1861static struct configfs_attribute *f_uac2_attrs[] = {
Christoph Hellwig495702b2015-10-03 15:32:49 +02001862 &f_uac2_opts_attr_p_chmask,
1863 &f_uac2_opts_attr_p_srate,
1864 &f_uac2_opts_attr_p_ssize,
1865 &f_uac2_opts_attr_c_chmask,
1866 &f_uac2_opts_attr_c_srate,
1867 &f_uac2_opts_attr_c_ssize,
Ruslan Bilovol40c73b32021-06-04 00:01:03 +02001868 &f_uac2_opts_attr_c_sync,
Peter Chene92b9d42017-01-04 10:19:23 +08001869 &f_uac2_opts_attr_req_number,
Ruslan Bilovole89bb422021-06-04 00:01:04 +02001870 &f_uac2_opts_attr_fb_max,
Ruslan Bilovoleaf6cbe2021-07-12 14:55:28 +02001871
1872 &f_uac2_opts_attr_p_mute_present,
1873 &f_uac2_opts_attr_p_volume_present,
1874 &f_uac2_opts_attr_p_volume_min,
1875 &f_uac2_opts_attr_p_volume_max,
1876 &f_uac2_opts_attr_p_volume_res,
1877
1878 &f_uac2_opts_attr_c_mute_present,
1879 &f_uac2_opts_attr_c_volume_present,
1880 &f_uac2_opts_attr_c_volume_min,
1881 &f_uac2_opts_attr_c_volume_max,
1882 &f_uac2_opts_attr_c_volume_res,
1883
Andrzej Pietrasiewicz3aeea3c2014-07-22 19:58:35 +02001884 NULL,
1885};
1886
Bhumika Goyal97363902017-10-16 17:18:41 +02001887static const struct config_item_type f_uac2_func_type = {
Andrzej Pietrasiewicz3aeea3c2014-07-22 19:58:35 +02001888 .ct_item_ops = &f_uac2_item_ops,
1889 .ct_attrs = f_uac2_attrs,
1890 .ct_owner = THIS_MODULE,
1891};
1892
Andrzej Pietrasiewiczf8f93d22014-07-22 19:58:30 +02001893static void afunc_free_inst(struct usb_function_instance *f)
1894{
1895 struct f_uac2_opts *opts;
1896
1897 opts = container_of(f, struct f_uac2_opts, func_inst);
1898 kfree(opts);
1899}
1900
1901static struct usb_function_instance *afunc_alloc_inst(void)
1902{
1903 struct f_uac2_opts *opts;
1904
1905 opts = kzalloc(sizeof(*opts), GFP_KERNEL);
1906 if (!opts)
1907 return ERR_PTR(-ENOMEM);
1908
Andrzej Pietrasiewicz3aeea3c2014-07-22 19:58:35 +02001909 mutex_init(&opts->lock);
Andrzej Pietrasiewiczf8f93d22014-07-22 19:58:30 +02001910 opts->func_inst.free_func_inst = afunc_free_inst;
1911
Andrzej Pietrasiewicz3aeea3c2014-07-22 19:58:35 +02001912 config_group_init_type_name(&opts->func_inst.group, "",
1913 &f_uac2_func_type);
1914
1915 opts->p_chmask = UAC2_DEF_PCHMASK;
1916 opts->p_srate = UAC2_DEF_PSRATE;
1917 opts->p_ssize = UAC2_DEF_PSSIZE;
1918 opts->c_chmask = UAC2_DEF_CCHMASK;
1919 opts->c_srate = UAC2_DEF_CSRATE;
1920 opts->c_ssize = UAC2_DEF_CSSIZE;
Ruslan Bilovol40c73b32021-06-04 00:01:03 +02001921 opts->c_sync = UAC2_DEF_CSYNC;
Ruslan Bilovoleaf6cbe2021-07-12 14:55:28 +02001922
1923 opts->p_mute_present = UAC2_DEF_MUTE_PRESENT;
1924 opts->p_volume_present = UAC2_DEF_VOLUME_PRESENT;
1925 opts->p_volume_min = UAC2_DEF_MIN_DB;
1926 opts->p_volume_max = UAC2_DEF_MAX_DB;
1927 opts->p_volume_res = UAC2_DEF_RES_DB;
1928
1929 opts->c_mute_present = UAC2_DEF_MUTE_PRESENT;
1930 opts->c_volume_present = UAC2_DEF_VOLUME_PRESENT;
1931 opts->c_volume_min = UAC2_DEF_MIN_DB;
1932 opts->c_volume_max = UAC2_DEF_MAX_DB;
1933 opts->c_volume_res = UAC2_DEF_RES_DB;
1934
Peter Chene92b9d42017-01-04 10:19:23 +08001935 opts->req_number = UAC2_DEF_REQ_NUM;
Pavel Hofmand9f27342021-10-22 16:03:39 +02001936 opts->fb_max = FBACK_FAST_MAX;
Andrzej Pietrasiewiczf8f93d22014-07-22 19:58:30 +02001937 return &opts->func_inst;
1938}
1939
1940static void afunc_free(struct usb_function *f)
1941{
Ruslan Bilovoleb9fecb2017-06-18 16:23:52 +03001942 struct g_audio *agdev;
Andrzej Pietrasiewicz3aeea3c2014-07-22 19:58:35 +02001943 struct f_uac2_opts *opts;
Andrzej Pietrasiewiczf8f93d22014-07-22 19:58:30 +02001944
Ruslan Bilovoleb9fecb2017-06-18 16:23:52 +03001945 agdev = func_to_g_audio(f);
Andrzej Pietrasiewicz3aeea3c2014-07-22 19:58:35 +02001946 opts = container_of(f->fi, struct f_uac2_opts, func_inst);
Andrzej Pietrasiewiczf8f93d22014-07-22 19:58:30 +02001947 kfree(agdev);
Andrzej Pietrasiewicz3aeea3c2014-07-22 19:58:35 +02001948 mutex_lock(&opts->lock);
1949 --opts->refcnt;
1950 mutex_unlock(&opts->lock);
Andrzej Pietrasiewiczf8f93d22014-07-22 19:58:30 +02001951}
1952
1953static void afunc_unbind(struct usb_configuration *c, struct usb_function *f)
1954{
Ruslan Bilovoleb9fecb2017-06-18 16:23:52 +03001955 struct g_audio *agdev = func_to_g_audio(f);
Andrzej Pietrasiewiczf8f93d22014-07-22 19:58:30 +02001956
Ruslan Bilovoleb9fecb2017-06-18 16:23:52 +03001957 g_audio_cleanup(agdev);
Andrzej Pietrasiewiczf8f93d22014-07-22 19:58:30 +02001958 usb_free_all_descriptors(f);
Ruslan Bilovol7158b572017-06-18 16:23:51 +03001959
1960 agdev->gadget = NULL;
Ruslan Bilovoleaf6cbe2021-07-12 14:55:28 +02001961
1962 kfree(out_feature_unit_desc);
1963 out_feature_unit_desc = NULL;
1964 kfree(in_feature_unit_desc);
1965 in_feature_unit_desc = NULL;
Andrzej Pietrasiewiczf8f93d22014-07-22 19:58:30 +02001966}
1967
Lad, Prabhakaref16e7c2015-02-04 18:01:11 +00001968static struct usb_function *afunc_alloc(struct usb_function_instance *fi)
Andrzej Pietrasiewiczf8f93d22014-07-22 19:58:30 +02001969{
Ruslan Bilovoleb9fecb2017-06-18 16:23:52 +03001970 struct f_uac2 *uac2;
Andrzej Pietrasiewiczf8f93d22014-07-22 19:58:30 +02001971 struct f_uac2_opts *opts;
1972
Ruslan Bilovoleb9fecb2017-06-18 16:23:52 +03001973 uac2 = kzalloc(sizeof(*uac2), GFP_KERNEL);
1974 if (uac2 == NULL)
Andrzej Pietrasiewiczf8f93d22014-07-22 19:58:30 +02001975 return ERR_PTR(-ENOMEM);
1976
1977 opts = container_of(fi, struct f_uac2_opts, func_inst);
Andrzej Pietrasiewicz3aeea3c2014-07-22 19:58:35 +02001978 mutex_lock(&opts->lock);
1979 ++opts->refcnt;
1980 mutex_unlock(&opts->lock);
Andrzej Pietrasiewiczf8f93d22014-07-22 19:58:30 +02001981
Ruslan Bilovoleb9fecb2017-06-18 16:23:52 +03001982 uac2->g_audio.func.name = "uac2_func";
1983 uac2->g_audio.func.bind = afunc_bind;
1984 uac2->g_audio.func.unbind = afunc_unbind;
1985 uac2->g_audio.func.set_alt = afunc_set_alt;
1986 uac2->g_audio.func.get_alt = afunc_get_alt;
1987 uac2->g_audio.func.disable = afunc_disable;
1988 uac2->g_audio.func.setup = afunc_setup;
1989 uac2->g_audio.func.free_func = afunc_free;
Andrzej Pietrasiewiczf8f93d22014-07-22 19:58:30 +02001990
Ruslan Bilovoleb9fecb2017-06-18 16:23:52 +03001991 return &uac2->g_audio.func;
Andrzej Pietrasiewiczf8f93d22014-07-22 19:58:30 +02001992}
1993
1994DECLARE_USB_FUNCTION_INIT(uac2, afunc_alloc_inst, afunc_alloc);
1995MODULE_LICENSE("GPL");
1996MODULE_AUTHOR("Yadwinder Singh");
1997MODULE_AUTHOR("Jaswinder Singh");
Ruslan Bilovoleaf6cbe2021-07-12 14:55:28 +02001998MODULE_AUTHOR("Ruslan Bilovol");