Greg Kroah-Hartman | 5fd54ac | 2017-11-03 11:28:30 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Jassi Brar | 132fcb4 | 2012-02-02 22:01:34 +0530 | [diff] [blame] | 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) |
Jassi Brar | 132fcb4 | 2012-02-02 22:01:34 +0530 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <linux/usb/audio.h> |
| 11 | #include <linux/usb/audio-v2.h> |
Jassi Brar | 132fcb4 | 2012-02-02 22:01:34 +0530 | [diff] [blame] | 12 | #include <linux/module.h> |
| 13 | |
Ruslan Bilovol | eb9fecb | 2017-06-18 16:23:52 +0300 | [diff] [blame] | 14 | #include "u_audio.h" |
Andrzej Pietrasiewicz | f8f93d2 | 2014-07-22 19:58:30 +0200 | [diff] [blame] | 15 | #include "u_uac2.h" |
| 16 | |
Jassi Brar | 132fcb4 | 2012-02-02 22:01:34 +0530 | [diff] [blame] | 17 | /* |
| 18 | * The driver implements a simple UAC_2 topology. |
| 19 | * USB-OUT -> IT_1 -> OT_3 -> ALSA_Capture |
| 20 | * ALSA_Playback -> IT_2 -> OT_4 -> USB-IN |
| 21 | * Capture and Playback sampling rates are independently |
| 22 | * controlled by two clock sources : |
| 23 | * CLK_5 := c_srate, and CLK_6 := p_srate |
| 24 | */ |
Andreas Pape | 3fa4eaa | 2018-06-21 17:22:51 +0200 | [diff] [blame] | 25 | #define USB_OUT_CLK_ID (out_clk_src_desc.bClockID) |
| 26 | #define USB_IN_CLK_ID (in_clk_src_desc.bClockID) |
Jassi Brar | 132fcb4 | 2012-02-02 22:01:34 +0530 | [diff] [blame] | 27 | |
| 28 | #define CONTROL_ABSENT 0 |
| 29 | #define CONTROL_RDONLY 1 |
| 30 | #define CONTROL_RDWR 3 |
| 31 | |
| 32 | #define CLK_FREQ_CTRL 0 |
| 33 | #define CLK_VLD_CTRL 2 |
| 34 | |
| 35 | #define COPY_CTRL 0 |
| 36 | #define CONN_CTRL 2 |
| 37 | #define OVRLD_CTRL 4 |
| 38 | #define CLSTR_CTRL 6 |
| 39 | #define UNFLW_CTRL 8 |
| 40 | #define OVFLW_CTRL 10 |
| 41 | |
Andreas Pape | 3fa4eaa | 2018-06-21 17:22:51 +0200 | [diff] [blame] | 42 | #define EPIN_EN(_opts) ((_opts)->p_chmask != 0) |
| 43 | #define EPOUT_EN(_opts) ((_opts)->c_chmask != 0) |
| 44 | |
Ruslan Bilovol | eb9fecb | 2017-06-18 16:23:52 +0300 | [diff] [blame] | 45 | struct f_uac2 { |
| 46 | struct g_audio g_audio; |
| 47 | u8 ac_intf, as_in_intf, as_out_intf; |
| 48 | u8 ac_alt, as_in_alt, as_out_alt; /* needed for get_alt() */ |
Jassi Brar | 132fcb4 | 2012-02-02 22:01:34 +0530 | [diff] [blame] | 49 | }; |
| 50 | |
Ruslan Bilovol | eb9fecb | 2017-06-18 16:23:52 +0300 | [diff] [blame] | 51 | static inline struct f_uac2 *func_to_uac2(struct usb_function *f) |
Jassi Brar | 132fcb4 | 2012-02-02 22:01:34 +0530 | [diff] [blame] | 52 | { |
Ruslan Bilovol | eb9fecb | 2017-06-18 16:23:52 +0300 | [diff] [blame] | 53 | return container_of(f, struct f_uac2, g_audio.func); |
Jassi Brar | 132fcb4 | 2012-02-02 22:01:34 +0530 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | static inline |
Ruslan Bilovol | eb9fecb | 2017-06-18 16:23:52 +0300 | [diff] [blame] | 57 | struct f_uac2_opts *g_audio_to_uac2_opts(struct g_audio *agdev) |
Daniel Mack | 254b3bf | 2014-08-27 19:09:05 +0200 | [diff] [blame] | 58 | { |
| 59 | return container_of(agdev->func.fi, struct f_uac2_opts, func_inst); |
| 60 | } |
| 61 | |
Jassi Brar | 132fcb4 | 2012-02-02 22:01:34 +0530 | [diff] [blame] | 62 | /* --------- USB Function Interface ------------- */ |
| 63 | |
| 64 | enum { |
| 65 | STR_ASSOC, |
| 66 | STR_IF_CTRL, |
| 67 | STR_CLKSRC_IN, |
| 68 | STR_CLKSRC_OUT, |
| 69 | STR_USB_IT, |
| 70 | STR_IO_IT, |
| 71 | STR_USB_OT, |
| 72 | STR_IO_OT, |
| 73 | STR_AS_OUT_ALT0, |
| 74 | STR_AS_OUT_ALT1, |
| 75 | STR_AS_IN_ALT0, |
| 76 | STR_AS_IN_ALT1, |
| 77 | }; |
| 78 | |
Jassi Brar | 132fcb4 | 2012-02-02 22:01:34 +0530 | [diff] [blame] | 79 | static char clksrc_in[8]; |
| 80 | static char clksrc_out[8]; |
Jassi Brar | 132fcb4 | 2012-02-02 22:01:34 +0530 | [diff] [blame] | 81 | |
| 82 | static struct usb_string strings_fn[] = { |
Sebastian Andrzej Siewior | b36c347 | 2012-10-22 22:15:09 +0200 | [diff] [blame] | 83 | [STR_ASSOC].s = "Source/Sink", |
| 84 | [STR_IF_CTRL].s = "Topology Control", |
Jassi Brar | 132fcb4 | 2012-02-02 22:01:34 +0530 | [diff] [blame] | 85 | [STR_CLKSRC_IN].s = clksrc_in, |
| 86 | [STR_CLKSRC_OUT].s = clksrc_out, |
Sebastian Andrzej Siewior | b36c347 | 2012-10-22 22:15:09 +0200 | [diff] [blame] | 87 | [STR_USB_IT].s = "USBH Out", |
| 88 | [STR_IO_IT].s = "USBD Out", |
| 89 | [STR_USB_OT].s = "USBH In", |
| 90 | [STR_IO_OT].s = "USBD In", |
| 91 | [STR_AS_OUT_ALT0].s = "Playback Inactive", |
| 92 | [STR_AS_OUT_ALT1].s = "Playback Active", |
| 93 | [STR_AS_IN_ALT0].s = "Capture Inactive", |
| 94 | [STR_AS_IN_ALT1].s = "Capture Active", |
Jassi Brar | 132fcb4 | 2012-02-02 22:01:34 +0530 | [diff] [blame] | 95 | { }, |
| 96 | }; |
| 97 | |
| 98 | static struct usb_gadget_strings str_fn = { |
| 99 | .language = 0x0409, /* en-us */ |
| 100 | .strings = strings_fn, |
| 101 | }; |
| 102 | |
| 103 | static struct usb_gadget_strings *fn_strings[] = { |
| 104 | &str_fn, |
| 105 | NULL, |
| 106 | }; |
| 107 | |
Jassi Brar | 132fcb4 | 2012-02-02 22:01:34 +0530 | [diff] [blame] | 108 | static struct usb_interface_assoc_descriptor iad_desc = { |
| 109 | .bLength = sizeof iad_desc, |
| 110 | .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION, |
| 111 | |
| 112 | .bFirstInterface = 0, |
| 113 | .bInterfaceCount = 3, |
| 114 | .bFunctionClass = USB_CLASS_AUDIO, |
| 115 | .bFunctionSubClass = UAC2_FUNCTION_SUBCLASS_UNDEFINED, |
| 116 | .bFunctionProtocol = UAC_VERSION_2, |
| 117 | }; |
| 118 | |
| 119 | /* Audio Control Interface */ |
| 120 | static struct usb_interface_descriptor std_ac_if_desc = { |
| 121 | .bLength = sizeof std_ac_if_desc, |
| 122 | .bDescriptorType = USB_DT_INTERFACE, |
| 123 | |
| 124 | .bAlternateSetting = 0, |
| 125 | .bNumEndpoints = 0, |
| 126 | .bInterfaceClass = USB_CLASS_AUDIO, |
| 127 | .bInterfaceSubClass = USB_SUBCLASS_AUDIOCONTROL, |
| 128 | .bInterfaceProtocol = UAC_VERSION_2, |
| 129 | }; |
| 130 | |
| 131 | /* Clock source for IN traffic */ |
Lad, Prabhakar | ef16e7c | 2015-02-04 18:01:11 +0000 | [diff] [blame] | 132 | static struct uac_clock_source_descriptor in_clk_src_desc = { |
Jassi Brar | 132fcb4 | 2012-02-02 22:01:34 +0530 | [diff] [blame] | 133 | .bLength = sizeof in_clk_src_desc, |
| 134 | .bDescriptorType = USB_DT_CS_INTERFACE, |
| 135 | |
| 136 | .bDescriptorSubtype = UAC2_CLOCK_SOURCE, |
Andreas Pape | 3fa4eaa | 2018-06-21 17:22:51 +0200 | [diff] [blame] | 137 | /* .bClockID = DYNAMIC */ |
Jassi Brar | 132fcb4 | 2012-02-02 22:01:34 +0530 | [diff] [blame] | 138 | .bmAttributes = UAC_CLOCK_SOURCE_TYPE_INT_FIXED, |
| 139 | .bmControls = (CONTROL_RDONLY << CLK_FREQ_CTRL), |
| 140 | .bAssocTerminal = 0, |
| 141 | }; |
| 142 | |
| 143 | /* Clock source for OUT traffic */ |
Lad, Prabhakar | ef16e7c | 2015-02-04 18:01:11 +0000 | [diff] [blame] | 144 | static struct uac_clock_source_descriptor out_clk_src_desc = { |
Jassi Brar | 132fcb4 | 2012-02-02 22:01:34 +0530 | [diff] [blame] | 145 | .bLength = sizeof out_clk_src_desc, |
| 146 | .bDescriptorType = USB_DT_CS_INTERFACE, |
| 147 | |
| 148 | .bDescriptorSubtype = UAC2_CLOCK_SOURCE, |
Andreas Pape | 3fa4eaa | 2018-06-21 17:22:51 +0200 | [diff] [blame] | 149 | /* .bClockID = DYNAMIC */ |
Jassi Brar | 132fcb4 | 2012-02-02 22:01:34 +0530 | [diff] [blame] | 150 | .bmAttributes = UAC_CLOCK_SOURCE_TYPE_INT_FIXED, |
| 151 | .bmControls = (CONTROL_RDONLY << CLK_FREQ_CTRL), |
| 152 | .bAssocTerminal = 0, |
| 153 | }; |
| 154 | |
| 155 | /* Input Terminal for USB_OUT */ |
Lad, Prabhakar | ef16e7c | 2015-02-04 18:01:11 +0000 | [diff] [blame] | 156 | static struct uac2_input_terminal_descriptor usb_out_it_desc = { |
Jassi Brar | 132fcb4 | 2012-02-02 22:01:34 +0530 | [diff] [blame] | 157 | .bLength = sizeof usb_out_it_desc, |
| 158 | .bDescriptorType = USB_DT_CS_INTERFACE, |
| 159 | |
| 160 | .bDescriptorSubtype = UAC_INPUT_TERMINAL, |
Andreas Pape | 3fa4eaa | 2018-06-21 17:22:51 +0200 | [diff] [blame] | 161 | /* .bTerminalID = DYNAMIC */ |
Jassi Brar | 132fcb4 | 2012-02-02 22:01:34 +0530 | [diff] [blame] | 162 | .wTerminalType = cpu_to_le16(UAC_TERMINAL_STREAMING), |
| 163 | .bAssocTerminal = 0, |
Andreas Pape | 3fa4eaa | 2018-06-21 17:22:51 +0200 | [diff] [blame] | 164 | /* .bCSourceID = DYNAMIC */ |
Jassi Brar | 132fcb4 | 2012-02-02 22:01:34 +0530 | [diff] [blame] | 165 | .iChannelNames = 0, |
Ruslan Bilovol | 14e1d56 | 2017-06-25 16:23:47 +0300 | [diff] [blame] | 166 | .bmControls = cpu_to_le16(CONTROL_RDWR << COPY_CTRL), |
Jassi Brar | 132fcb4 | 2012-02-02 22:01:34 +0530 | [diff] [blame] | 167 | }; |
| 168 | |
| 169 | /* Input Terminal for I/O-In */ |
Lad, Prabhakar | ef16e7c | 2015-02-04 18:01:11 +0000 | [diff] [blame] | 170 | static struct uac2_input_terminal_descriptor io_in_it_desc = { |
Jassi Brar | 132fcb4 | 2012-02-02 22:01:34 +0530 | [diff] [blame] | 171 | .bLength = sizeof io_in_it_desc, |
| 172 | .bDescriptorType = USB_DT_CS_INTERFACE, |
| 173 | |
| 174 | .bDescriptorSubtype = UAC_INPUT_TERMINAL, |
Andreas Pape | 3fa4eaa | 2018-06-21 17:22:51 +0200 | [diff] [blame] | 175 | /* .bTerminalID = DYNAMIC */ |
Jassi Brar | 132fcb4 | 2012-02-02 22:01:34 +0530 | [diff] [blame] | 176 | .wTerminalType = cpu_to_le16(UAC_INPUT_TERMINAL_UNDEFINED), |
| 177 | .bAssocTerminal = 0, |
Andreas Pape | 3fa4eaa | 2018-06-21 17:22:51 +0200 | [diff] [blame] | 178 | /* .bCSourceID = DYNAMIC */ |
Jassi Brar | 132fcb4 | 2012-02-02 22:01:34 +0530 | [diff] [blame] | 179 | .iChannelNames = 0, |
Ruslan Bilovol | 14e1d56 | 2017-06-25 16:23:47 +0300 | [diff] [blame] | 180 | .bmControls = cpu_to_le16(CONTROL_RDWR << COPY_CTRL), |
Jassi Brar | 132fcb4 | 2012-02-02 22:01:34 +0530 | [diff] [blame] | 181 | }; |
| 182 | |
| 183 | /* Ouput Terminal for USB_IN */ |
Lad, Prabhakar | ef16e7c | 2015-02-04 18:01:11 +0000 | [diff] [blame] | 184 | static struct uac2_output_terminal_descriptor usb_in_ot_desc = { |
Jassi Brar | 132fcb4 | 2012-02-02 22:01:34 +0530 | [diff] [blame] | 185 | .bLength = sizeof usb_in_ot_desc, |
| 186 | .bDescriptorType = USB_DT_CS_INTERFACE, |
| 187 | |
| 188 | .bDescriptorSubtype = UAC_OUTPUT_TERMINAL, |
Andreas Pape | 3fa4eaa | 2018-06-21 17:22:51 +0200 | [diff] [blame] | 189 | /* .bTerminalID = DYNAMIC */ |
Jassi Brar | 132fcb4 | 2012-02-02 22:01:34 +0530 | [diff] [blame] | 190 | .wTerminalType = cpu_to_le16(UAC_TERMINAL_STREAMING), |
| 191 | .bAssocTerminal = 0, |
Andreas Pape | 3fa4eaa | 2018-06-21 17:22:51 +0200 | [diff] [blame] | 192 | /* .bSourceID = DYNAMIC */ |
| 193 | /* .bCSourceID = DYNAMIC */ |
Ruslan Bilovol | 14e1d56 | 2017-06-25 16:23:47 +0300 | [diff] [blame] | 194 | .bmControls = cpu_to_le16(CONTROL_RDWR << COPY_CTRL), |
Jassi Brar | 132fcb4 | 2012-02-02 22:01:34 +0530 | [diff] [blame] | 195 | }; |
| 196 | |
| 197 | /* Ouput Terminal for I/O-Out */ |
Lad, Prabhakar | ef16e7c | 2015-02-04 18:01:11 +0000 | [diff] [blame] | 198 | static struct uac2_output_terminal_descriptor io_out_ot_desc = { |
Jassi Brar | 132fcb4 | 2012-02-02 22:01:34 +0530 | [diff] [blame] | 199 | .bLength = sizeof io_out_ot_desc, |
| 200 | .bDescriptorType = USB_DT_CS_INTERFACE, |
| 201 | |
| 202 | .bDescriptorSubtype = UAC_OUTPUT_TERMINAL, |
Andreas Pape | 3fa4eaa | 2018-06-21 17:22:51 +0200 | [diff] [blame] | 203 | /* .bTerminalID = DYNAMIC */ |
Jassi Brar | 132fcb4 | 2012-02-02 22:01:34 +0530 | [diff] [blame] | 204 | .wTerminalType = cpu_to_le16(UAC_OUTPUT_TERMINAL_UNDEFINED), |
| 205 | .bAssocTerminal = 0, |
Andreas Pape | 3fa4eaa | 2018-06-21 17:22:51 +0200 | [diff] [blame] | 206 | /* .bSourceID = DYNAMIC */ |
| 207 | /* .bCSourceID = DYNAMIC */ |
Ruslan Bilovol | 14e1d56 | 2017-06-25 16:23:47 +0300 | [diff] [blame] | 208 | .bmControls = cpu_to_le16(CONTROL_RDWR << COPY_CTRL), |
Jassi Brar | 132fcb4 | 2012-02-02 22:01:34 +0530 | [diff] [blame] | 209 | }; |
| 210 | |
Lad, Prabhakar | ef16e7c | 2015-02-04 18:01:11 +0000 | [diff] [blame] | 211 | static struct uac2_ac_header_descriptor ac_hdr_desc = { |
Jassi Brar | 132fcb4 | 2012-02-02 22:01:34 +0530 | [diff] [blame] | 212 | .bLength = sizeof ac_hdr_desc, |
| 213 | .bDescriptorType = USB_DT_CS_INTERFACE, |
| 214 | |
| 215 | .bDescriptorSubtype = UAC_MS_HEADER, |
| 216 | .bcdADC = cpu_to_le16(0x200), |
| 217 | .bCategory = UAC2_FUNCTION_IO_BOX, |
Ruslan Bilovol | a9cf871 | 2020-07-03 16:49:03 +0300 | [diff] [blame] | 218 | /* .wTotalLength = DYNAMIC */ |
Jassi Brar | 132fcb4 | 2012-02-02 22:01:34 +0530 | [diff] [blame] | 219 | .bmControls = 0, |
| 220 | }; |
| 221 | |
| 222 | /* Audio Streaming OUT Interface - Alt0 */ |
| 223 | static struct usb_interface_descriptor std_as_out_if0_desc = { |
| 224 | .bLength = sizeof std_as_out_if0_desc, |
| 225 | .bDescriptorType = USB_DT_INTERFACE, |
| 226 | |
| 227 | .bAlternateSetting = 0, |
| 228 | .bNumEndpoints = 0, |
| 229 | .bInterfaceClass = USB_CLASS_AUDIO, |
| 230 | .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING, |
| 231 | .bInterfaceProtocol = UAC_VERSION_2, |
| 232 | }; |
| 233 | |
| 234 | /* Audio Streaming OUT Interface - Alt1 */ |
| 235 | static struct usb_interface_descriptor std_as_out_if1_desc = { |
| 236 | .bLength = sizeof std_as_out_if1_desc, |
| 237 | .bDescriptorType = USB_DT_INTERFACE, |
| 238 | |
| 239 | .bAlternateSetting = 1, |
| 240 | .bNumEndpoints = 1, |
| 241 | .bInterfaceClass = USB_CLASS_AUDIO, |
| 242 | .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING, |
| 243 | .bInterfaceProtocol = UAC_VERSION_2, |
| 244 | }; |
| 245 | |
| 246 | /* Audio Stream OUT Intface Desc */ |
Lad, Prabhakar | ef16e7c | 2015-02-04 18:01:11 +0000 | [diff] [blame] | 247 | static struct uac2_as_header_descriptor as_out_hdr_desc = { |
Jassi Brar | 132fcb4 | 2012-02-02 22:01:34 +0530 | [diff] [blame] | 248 | .bLength = sizeof as_out_hdr_desc, |
| 249 | .bDescriptorType = USB_DT_CS_INTERFACE, |
| 250 | |
| 251 | .bDescriptorSubtype = UAC_AS_GENERAL, |
Andreas Pape | 3fa4eaa | 2018-06-21 17:22:51 +0200 | [diff] [blame] | 252 | /* .bTerminalLink = DYNAMIC */ |
Jassi Brar | 132fcb4 | 2012-02-02 22:01:34 +0530 | [diff] [blame] | 253 | .bmControls = 0, |
| 254 | .bFormatType = UAC_FORMAT_TYPE_I, |
| 255 | .bmFormats = cpu_to_le32(UAC_FORMAT_TYPE_I_PCM), |
| 256 | .iChannelNames = 0, |
| 257 | }; |
| 258 | |
| 259 | /* Audio USB_OUT Format */ |
Lad, Prabhakar | ef16e7c | 2015-02-04 18:01:11 +0000 | [diff] [blame] | 260 | static struct uac2_format_type_i_descriptor as_out_fmt1_desc = { |
Jassi Brar | 132fcb4 | 2012-02-02 22:01:34 +0530 | [diff] [blame] | 261 | .bLength = sizeof as_out_fmt1_desc, |
| 262 | .bDescriptorType = USB_DT_CS_INTERFACE, |
| 263 | .bDescriptorSubtype = UAC_FORMAT_TYPE, |
| 264 | .bFormatType = UAC_FORMAT_TYPE_I, |
| 265 | }; |
| 266 | |
| 267 | /* STD AS ISO OUT Endpoint */ |
Lad, Prabhakar | ef16e7c | 2015-02-04 18:01:11 +0000 | [diff] [blame] | 268 | static struct usb_endpoint_descriptor fs_epout_desc = { |
Jassi Brar | 132fcb4 | 2012-02-02 22:01:34 +0530 | [diff] [blame] | 269 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 270 | .bDescriptorType = USB_DT_ENDPOINT, |
| 271 | |
| 272 | .bEndpointAddress = USB_DIR_OUT, |
| 273 | .bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC, |
Jerome Brunet | 9389044 | 2020-12-21 18:35:28 +0100 | [diff] [blame^] | 274 | /* .wMaxPacketSize = DYNAMIC */ |
Jassi Brar | 132fcb4 | 2012-02-02 22:01:34 +0530 | [diff] [blame] | 275 | .bInterval = 1, |
| 276 | }; |
| 277 | |
Lad, Prabhakar | ef16e7c | 2015-02-04 18:01:11 +0000 | [diff] [blame] | 278 | static struct usb_endpoint_descriptor hs_epout_desc = { |
Jassi Brar | 132fcb4 | 2012-02-02 22:01:34 +0530 | [diff] [blame] | 279 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 280 | .bDescriptorType = USB_DT_ENDPOINT, |
| 281 | |
| 282 | .bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC, |
Jerome Brunet | 9389044 | 2020-12-21 18:35:28 +0100 | [diff] [blame^] | 283 | /* .wMaxPacketSize = DYNAMIC */ |
Jassi Brar | 132fcb4 | 2012-02-02 22:01:34 +0530 | [diff] [blame] | 284 | .bInterval = 4, |
| 285 | }; |
| 286 | |
| 287 | /* CS AS ISO OUT Endpoint */ |
| 288 | static struct uac2_iso_endpoint_descriptor as_iso_out_desc = { |
| 289 | .bLength = sizeof as_iso_out_desc, |
| 290 | .bDescriptorType = USB_DT_CS_ENDPOINT, |
| 291 | |
| 292 | .bDescriptorSubtype = UAC_EP_GENERAL, |
| 293 | .bmAttributes = 0, |
| 294 | .bmControls = 0, |
| 295 | .bLockDelayUnits = 0, |
| 296 | .wLockDelay = 0, |
| 297 | }; |
| 298 | |
| 299 | /* Audio Streaming IN Interface - Alt0 */ |
| 300 | static struct usb_interface_descriptor std_as_in_if0_desc = { |
| 301 | .bLength = sizeof std_as_in_if0_desc, |
| 302 | .bDescriptorType = USB_DT_INTERFACE, |
| 303 | |
| 304 | .bAlternateSetting = 0, |
| 305 | .bNumEndpoints = 0, |
| 306 | .bInterfaceClass = USB_CLASS_AUDIO, |
| 307 | .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING, |
| 308 | .bInterfaceProtocol = UAC_VERSION_2, |
| 309 | }; |
| 310 | |
| 311 | /* Audio Streaming IN Interface - Alt1 */ |
| 312 | static struct usb_interface_descriptor std_as_in_if1_desc = { |
| 313 | .bLength = sizeof std_as_in_if1_desc, |
| 314 | .bDescriptorType = USB_DT_INTERFACE, |
| 315 | |
| 316 | .bAlternateSetting = 1, |
| 317 | .bNumEndpoints = 1, |
| 318 | .bInterfaceClass = USB_CLASS_AUDIO, |
| 319 | .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING, |
| 320 | .bInterfaceProtocol = UAC_VERSION_2, |
| 321 | }; |
| 322 | |
| 323 | /* Audio Stream IN Intface Desc */ |
Lad, Prabhakar | ef16e7c | 2015-02-04 18:01:11 +0000 | [diff] [blame] | 324 | static struct uac2_as_header_descriptor as_in_hdr_desc = { |
Jassi Brar | 132fcb4 | 2012-02-02 22:01:34 +0530 | [diff] [blame] | 325 | .bLength = sizeof as_in_hdr_desc, |
| 326 | .bDescriptorType = USB_DT_CS_INTERFACE, |
| 327 | |
| 328 | .bDescriptorSubtype = UAC_AS_GENERAL, |
Andreas Pape | 3fa4eaa | 2018-06-21 17:22:51 +0200 | [diff] [blame] | 329 | /* .bTerminalLink = DYNAMIC */ |
Jassi Brar | 132fcb4 | 2012-02-02 22:01:34 +0530 | [diff] [blame] | 330 | .bmControls = 0, |
| 331 | .bFormatType = UAC_FORMAT_TYPE_I, |
| 332 | .bmFormats = cpu_to_le32(UAC_FORMAT_TYPE_I_PCM), |
| 333 | .iChannelNames = 0, |
| 334 | }; |
| 335 | |
| 336 | /* Audio USB_IN Format */ |
Lad, Prabhakar | ef16e7c | 2015-02-04 18:01:11 +0000 | [diff] [blame] | 337 | static struct uac2_format_type_i_descriptor as_in_fmt1_desc = { |
Jassi Brar | 132fcb4 | 2012-02-02 22:01:34 +0530 | [diff] [blame] | 338 | .bLength = sizeof as_in_fmt1_desc, |
| 339 | .bDescriptorType = USB_DT_CS_INTERFACE, |
| 340 | .bDescriptorSubtype = UAC_FORMAT_TYPE, |
| 341 | .bFormatType = UAC_FORMAT_TYPE_I, |
| 342 | }; |
| 343 | |
| 344 | /* STD AS ISO IN Endpoint */ |
Lad, Prabhakar | ef16e7c | 2015-02-04 18:01:11 +0000 | [diff] [blame] | 345 | static struct usb_endpoint_descriptor fs_epin_desc = { |
Jassi Brar | 132fcb4 | 2012-02-02 22:01:34 +0530 | [diff] [blame] | 346 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 347 | .bDescriptorType = USB_DT_ENDPOINT, |
| 348 | |
| 349 | .bEndpointAddress = USB_DIR_IN, |
| 350 | .bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC, |
Jerome Brunet | 9389044 | 2020-12-21 18:35:28 +0100 | [diff] [blame^] | 351 | /* .wMaxPacketSize = DYNAMIC */ |
Jassi Brar | 132fcb4 | 2012-02-02 22:01:34 +0530 | [diff] [blame] | 352 | .bInterval = 1, |
| 353 | }; |
| 354 | |
Lad, Prabhakar | ef16e7c | 2015-02-04 18:01:11 +0000 | [diff] [blame] | 355 | static struct usb_endpoint_descriptor hs_epin_desc = { |
Jassi Brar | 132fcb4 | 2012-02-02 22:01:34 +0530 | [diff] [blame] | 356 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 357 | .bDescriptorType = USB_DT_ENDPOINT, |
| 358 | |
| 359 | .bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC, |
Jerome Brunet | 9389044 | 2020-12-21 18:35:28 +0100 | [diff] [blame^] | 360 | /* .wMaxPacketSize = DYNAMIC */ |
Jassi Brar | 132fcb4 | 2012-02-02 22:01:34 +0530 | [diff] [blame] | 361 | .bInterval = 4, |
| 362 | }; |
| 363 | |
| 364 | /* CS AS ISO IN Endpoint */ |
| 365 | static struct uac2_iso_endpoint_descriptor as_iso_in_desc = { |
| 366 | .bLength = sizeof as_iso_in_desc, |
| 367 | .bDescriptorType = USB_DT_CS_ENDPOINT, |
| 368 | |
| 369 | .bDescriptorSubtype = UAC_EP_GENERAL, |
| 370 | .bmAttributes = 0, |
| 371 | .bmControls = 0, |
| 372 | .bLockDelayUnits = 0, |
| 373 | .wLockDelay = 0, |
| 374 | }; |
| 375 | |
| 376 | static struct usb_descriptor_header *fs_audio_desc[] = { |
| 377 | (struct usb_descriptor_header *)&iad_desc, |
| 378 | (struct usb_descriptor_header *)&std_ac_if_desc, |
| 379 | |
| 380 | (struct usb_descriptor_header *)&ac_hdr_desc, |
| 381 | (struct usb_descriptor_header *)&in_clk_src_desc, |
| 382 | (struct usb_descriptor_header *)&out_clk_src_desc, |
| 383 | (struct usb_descriptor_header *)&usb_out_it_desc, |
| 384 | (struct usb_descriptor_header *)&io_in_it_desc, |
| 385 | (struct usb_descriptor_header *)&usb_in_ot_desc, |
| 386 | (struct usb_descriptor_header *)&io_out_ot_desc, |
| 387 | |
| 388 | (struct usb_descriptor_header *)&std_as_out_if0_desc, |
| 389 | (struct usb_descriptor_header *)&std_as_out_if1_desc, |
| 390 | |
| 391 | (struct usb_descriptor_header *)&as_out_hdr_desc, |
| 392 | (struct usb_descriptor_header *)&as_out_fmt1_desc, |
| 393 | (struct usb_descriptor_header *)&fs_epout_desc, |
| 394 | (struct usb_descriptor_header *)&as_iso_out_desc, |
| 395 | |
| 396 | (struct usb_descriptor_header *)&std_as_in_if0_desc, |
| 397 | (struct usb_descriptor_header *)&std_as_in_if1_desc, |
| 398 | |
| 399 | (struct usb_descriptor_header *)&as_in_hdr_desc, |
| 400 | (struct usb_descriptor_header *)&as_in_fmt1_desc, |
| 401 | (struct usb_descriptor_header *)&fs_epin_desc, |
| 402 | (struct usb_descriptor_header *)&as_iso_in_desc, |
| 403 | NULL, |
| 404 | }; |
| 405 | |
| 406 | static struct usb_descriptor_header *hs_audio_desc[] = { |
| 407 | (struct usb_descriptor_header *)&iad_desc, |
| 408 | (struct usb_descriptor_header *)&std_ac_if_desc, |
| 409 | |
| 410 | (struct usb_descriptor_header *)&ac_hdr_desc, |
| 411 | (struct usb_descriptor_header *)&in_clk_src_desc, |
| 412 | (struct usb_descriptor_header *)&out_clk_src_desc, |
| 413 | (struct usb_descriptor_header *)&usb_out_it_desc, |
| 414 | (struct usb_descriptor_header *)&io_in_it_desc, |
| 415 | (struct usb_descriptor_header *)&usb_in_ot_desc, |
| 416 | (struct usb_descriptor_header *)&io_out_ot_desc, |
| 417 | |
| 418 | (struct usb_descriptor_header *)&std_as_out_if0_desc, |
| 419 | (struct usb_descriptor_header *)&std_as_out_if1_desc, |
| 420 | |
| 421 | (struct usb_descriptor_header *)&as_out_hdr_desc, |
| 422 | (struct usb_descriptor_header *)&as_out_fmt1_desc, |
| 423 | (struct usb_descriptor_header *)&hs_epout_desc, |
| 424 | (struct usb_descriptor_header *)&as_iso_out_desc, |
| 425 | |
| 426 | (struct usb_descriptor_header *)&std_as_in_if0_desc, |
| 427 | (struct usb_descriptor_header *)&std_as_in_if1_desc, |
| 428 | |
| 429 | (struct usb_descriptor_header *)&as_in_hdr_desc, |
| 430 | (struct usb_descriptor_header *)&as_in_fmt1_desc, |
| 431 | (struct usb_descriptor_header *)&hs_epin_desc, |
| 432 | (struct usb_descriptor_header *)&as_iso_in_desc, |
| 433 | NULL, |
| 434 | }; |
| 435 | |
| 436 | struct cntrl_cur_lay3 { |
Eugeniu Rosca | eec24f2 | 2018-07-02 23:46:47 +0200 | [diff] [blame] | 437 | __le32 dCUR; |
Jassi Brar | 132fcb4 | 2012-02-02 22:01:34 +0530 | [diff] [blame] | 438 | }; |
| 439 | |
| 440 | struct cntrl_range_lay3 { |
Eugeniu Rosca | eec24f2 | 2018-07-02 23:46:47 +0200 | [diff] [blame] | 441 | __le16 wNumSubRanges; |
| 442 | __le32 dMIN; |
| 443 | __le32 dMAX; |
| 444 | __le32 dRES; |
Jassi Brar | 132fcb4 | 2012-02-02 22:01:34 +0530 | [diff] [blame] | 445 | } __packed; |
| 446 | |
Jerome Brunet | 9389044 | 2020-12-21 18:35:28 +0100 | [diff] [blame^] | 447 | static int set_ep_max_packet_size(const struct f_uac2_opts *uac2_opts, |
Peter Chen | 913e4a9 | 2015-07-30 13:13:03 +0800 | [diff] [blame] | 448 | struct usb_endpoint_descriptor *ep_desc, |
Jerome Brunet | 9389044 | 2020-12-21 18:35:28 +0100 | [diff] [blame^] | 449 | enum usb_device_speed speed, bool is_playback) |
Peter Chen | 913e4a9 | 2015-07-30 13:13:03 +0800 | [diff] [blame] | 450 | { |
| 451 | int chmask, srate, ssize; |
Jerome Brunet | 9389044 | 2020-12-21 18:35:28 +0100 | [diff] [blame^] | 452 | u16 max_size_bw, max_size_ep; |
| 453 | unsigned int factor; |
| 454 | |
| 455 | switch (speed) { |
| 456 | case USB_SPEED_FULL: |
| 457 | max_size_ep = 1023; |
| 458 | factor = 1000; |
| 459 | break; |
| 460 | |
| 461 | case USB_SPEED_HIGH: |
| 462 | max_size_ep = 1024; |
| 463 | factor = 8000; |
| 464 | break; |
| 465 | |
| 466 | default: |
| 467 | return -EINVAL; |
| 468 | } |
Peter Chen | 913e4a9 | 2015-07-30 13:13:03 +0800 | [diff] [blame] | 469 | |
| 470 | if (is_playback) { |
| 471 | chmask = uac2_opts->p_chmask; |
| 472 | srate = uac2_opts->p_srate; |
| 473 | ssize = uac2_opts->p_ssize; |
| 474 | } else { |
| 475 | chmask = uac2_opts->c_chmask; |
| 476 | srate = uac2_opts->c_srate; |
| 477 | ssize = uac2_opts->c_ssize; |
| 478 | } |
| 479 | |
Jerome Brunet | 9389044 | 2020-12-21 18:35:28 +0100 | [diff] [blame^] | 480 | max_size_bw = num_channels(chmask) * ssize * |
Peter Chen | 913e4a9 | 2015-07-30 13:13:03 +0800 | [diff] [blame] | 481 | DIV_ROUND_UP(srate, factor / (1 << (ep_desc->bInterval - 1))); |
Jerome Brunet | 9389044 | 2020-12-21 18:35:28 +0100 | [diff] [blame^] | 482 | ep_desc->wMaxPacketSize = cpu_to_le16(min_t(u16, max_size_bw, |
| 483 | max_size_ep)); |
| 484 | |
| 485 | return 0; |
Peter Chen | 913e4a9 | 2015-07-30 13:13:03 +0800 | [diff] [blame] | 486 | } |
| 487 | |
Andreas Pape | 3fa4eaa | 2018-06-21 17:22:51 +0200 | [diff] [blame] | 488 | /* Use macro to overcome line length limitation */ |
| 489 | #define USBDHDR(p) (struct usb_descriptor_header *)(p) |
| 490 | |
| 491 | static void setup_descriptor(struct f_uac2_opts *opts) |
| 492 | { |
| 493 | /* patch descriptors */ |
| 494 | int i = 1; /* ID's start with 1 */ |
| 495 | |
| 496 | if (EPOUT_EN(opts)) |
| 497 | usb_out_it_desc.bTerminalID = i++; |
| 498 | if (EPIN_EN(opts)) |
| 499 | io_in_it_desc.bTerminalID = i++; |
| 500 | if (EPOUT_EN(opts)) |
| 501 | io_out_ot_desc.bTerminalID = i++; |
| 502 | if (EPIN_EN(opts)) |
| 503 | usb_in_ot_desc.bTerminalID = i++; |
| 504 | if (EPOUT_EN(opts)) |
| 505 | out_clk_src_desc.bClockID = i++; |
| 506 | if (EPIN_EN(opts)) |
| 507 | in_clk_src_desc.bClockID = i++; |
| 508 | |
| 509 | usb_out_it_desc.bCSourceID = out_clk_src_desc.bClockID; |
| 510 | usb_in_ot_desc.bSourceID = io_in_it_desc.bTerminalID; |
| 511 | usb_in_ot_desc.bCSourceID = in_clk_src_desc.bClockID; |
| 512 | io_in_it_desc.bCSourceID = in_clk_src_desc.bClockID; |
| 513 | io_out_ot_desc.bCSourceID = out_clk_src_desc.bClockID; |
| 514 | io_out_ot_desc.bSourceID = usb_out_it_desc.bTerminalID; |
| 515 | as_out_hdr_desc.bTerminalLink = usb_out_it_desc.bTerminalID; |
| 516 | as_in_hdr_desc.bTerminalLink = usb_in_ot_desc.bTerminalID; |
| 517 | |
| 518 | iad_desc.bInterfaceCount = 1; |
Ruslan Bilovol | a9cf871 | 2020-07-03 16:49:03 +0300 | [diff] [blame] | 519 | ac_hdr_desc.wTotalLength = cpu_to_le16(sizeof(ac_hdr_desc)); |
Andreas Pape | 3fa4eaa | 2018-06-21 17:22:51 +0200 | [diff] [blame] | 520 | |
| 521 | if (EPIN_EN(opts)) { |
| 522 | u16 len = le16_to_cpu(ac_hdr_desc.wTotalLength); |
| 523 | |
| 524 | len += sizeof(in_clk_src_desc); |
| 525 | len += sizeof(usb_in_ot_desc); |
| 526 | len += sizeof(io_in_it_desc); |
| 527 | ac_hdr_desc.wTotalLength = cpu_to_le16(len); |
| 528 | iad_desc.bInterfaceCount++; |
| 529 | } |
| 530 | if (EPOUT_EN(opts)) { |
| 531 | u16 len = le16_to_cpu(ac_hdr_desc.wTotalLength); |
| 532 | |
| 533 | len += sizeof(out_clk_src_desc); |
| 534 | len += sizeof(usb_out_it_desc); |
| 535 | len += sizeof(io_out_ot_desc); |
| 536 | ac_hdr_desc.wTotalLength = cpu_to_le16(len); |
| 537 | iad_desc.bInterfaceCount++; |
| 538 | } |
| 539 | |
| 540 | i = 0; |
| 541 | fs_audio_desc[i++] = USBDHDR(&iad_desc); |
| 542 | fs_audio_desc[i++] = USBDHDR(&std_ac_if_desc); |
| 543 | fs_audio_desc[i++] = USBDHDR(&ac_hdr_desc); |
| 544 | if (EPIN_EN(opts)) |
| 545 | fs_audio_desc[i++] = USBDHDR(&in_clk_src_desc); |
| 546 | if (EPOUT_EN(opts)) { |
| 547 | fs_audio_desc[i++] = USBDHDR(&out_clk_src_desc); |
| 548 | fs_audio_desc[i++] = USBDHDR(&usb_out_it_desc); |
| 549 | } |
| 550 | if (EPIN_EN(opts)) { |
| 551 | fs_audio_desc[i++] = USBDHDR(&io_in_it_desc); |
| 552 | fs_audio_desc[i++] = USBDHDR(&usb_in_ot_desc); |
| 553 | } |
| 554 | if (EPOUT_EN(opts)) { |
| 555 | fs_audio_desc[i++] = USBDHDR(&io_out_ot_desc); |
| 556 | fs_audio_desc[i++] = USBDHDR(&std_as_out_if0_desc); |
| 557 | fs_audio_desc[i++] = USBDHDR(&std_as_out_if1_desc); |
| 558 | fs_audio_desc[i++] = USBDHDR(&as_out_hdr_desc); |
| 559 | fs_audio_desc[i++] = USBDHDR(&as_out_fmt1_desc); |
| 560 | fs_audio_desc[i++] = USBDHDR(&fs_epout_desc); |
| 561 | fs_audio_desc[i++] = USBDHDR(&as_iso_out_desc); |
| 562 | } |
| 563 | if (EPIN_EN(opts)) { |
| 564 | fs_audio_desc[i++] = USBDHDR(&std_as_in_if0_desc); |
| 565 | fs_audio_desc[i++] = USBDHDR(&std_as_in_if1_desc); |
| 566 | fs_audio_desc[i++] = USBDHDR(&as_in_hdr_desc); |
| 567 | fs_audio_desc[i++] = USBDHDR(&as_in_fmt1_desc); |
| 568 | fs_audio_desc[i++] = USBDHDR(&fs_epin_desc); |
| 569 | fs_audio_desc[i++] = USBDHDR(&as_iso_in_desc); |
| 570 | } |
| 571 | fs_audio_desc[i] = NULL; |
| 572 | |
| 573 | i = 0; |
| 574 | hs_audio_desc[i++] = USBDHDR(&iad_desc); |
| 575 | hs_audio_desc[i++] = USBDHDR(&std_ac_if_desc); |
| 576 | hs_audio_desc[i++] = USBDHDR(&ac_hdr_desc); |
| 577 | if (EPIN_EN(opts)) |
| 578 | hs_audio_desc[i++] = USBDHDR(&in_clk_src_desc); |
| 579 | if (EPOUT_EN(opts)) { |
| 580 | hs_audio_desc[i++] = USBDHDR(&out_clk_src_desc); |
| 581 | hs_audio_desc[i++] = USBDHDR(&usb_out_it_desc); |
| 582 | } |
| 583 | if (EPIN_EN(opts)) { |
| 584 | hs_audio_desc[i++] = USBDHDR(&io_in_it_desc); |
| 585 | hs_audio_desc[i++] = USBDHDR(&usb_in_ot_desc); |
| 586 | } |
| 587 | if (EPOUT_EN(opts)) { |
| 588 | hs_audio_desc[i++] = USBDHDR(&io_out_ot_desc); |
| 589 | hs_audio_desc[i++] = USBDHDR(&std_as_out_if0_desc); |
| 590 | hs_audio_desc[i++] = USBDHDR(&std_as_out_if1_desc); |
| 591 | hs_audio_desc[i++] = USBDHDR(&as_out_hdr_desc); |
| 592 | hs_audio_desc[i++] = USBDHDR(&as_out_fmt1_desc); |
| 593 | hs_audio_desc[i++] = USBDHDR(&hs_epout_desc); |
| 594 | hs_audio_desc[i++] = USBDHDR(&as_iso_out_desc); |
| 595 | } |
| 596 | if (EPIN_EN(opts)) { |
| 597 | hs_audio_desc[i++] = USBDHDR(&std_as_in_if0_desc); |
| 598 | hs_audio_desc[i++] = USBDHDR(&std_as_in_if1_desc); |
| 599 | hs_audio_desc[i++] = USBDHDR(&as_in_hdr_desc); |
| 600 | hs_audio_desc[i++] = USBDHDR(&as_in_fmt1_desc); |
| 601 | hs_audio_desc[i++] = USBDHDR(&hs_epin_desc); |
| 602 | hs_audio_desc[i++] = USBDHDR(&as_iso_in_desc); |
| 603 | } |
| 604 | hs_audio_desc[i] = NULL; |
| 605 | } |
| 606 | |
Andrzej Pietrasiewicz | f8f93d2 | 2014-07-22 19:58:30 +0200 | [diff] [blame] | 607 | static int |
Jassi Brar | 132fcb4 | 2012-02-02 22:01:34 +0530 | [diff] [blame] | 608 | afunc_bind(struct usb_configuration *cfg, struct usb_function *fn) |
| 609 | { |
Ruslan Bilovol | eb9fecb | 2017-06-18 16:23:52 +0300 | [diff] [blame] | 610 | struct f_uac2 *uac2 = func_to_uac2(fn); |
| 611 | struct g_audio *agdev = func_to_g_audio(fn); |
Jassi Brar | 132fcb4 | 2012-02-02 22:01:34 +0530 | [diff] [blame] | 612 | struct usb_composite_dev *cdev = cfg->cdev; |
| 613 | struct usb_gadget *gadget = cdev->gadget; |
Ruslan Bilovol | 7158b57 | 2017-06-18 16:23:51 +0300 | [diff] [blame] | 614 | struct device *dev = &gadget->dev; |
Andrzej Pietrasiewicz | f8f93d2 | 2014-07-22 19:58:30 +0200 | [diff] [blame] | 615 | struct f_uac2_opts *uac2_opts; |
Andrzej Pietrasiewicz | f408757 | 2014-07-22 19:58:33 +0200 | [diff] [blame] | 616 | struct usb_string *us; |
Jassi Brar | 132fcb4 | 2012-02-02 22:01:34 +0530 | [diff] [blame] | 617 | int ret; |
| 618 | |
Andrzej Pietrasiewicz | f8f93d2 | 2014-07-22 19:58:30 +0200 | [diff] [blame] | 619 | uac2_opts = container_of(fn->fi, struct f_uac2_opts, func_inst); |
Andrzej Pietrasiewicz | f8f93d2 | 2014-07-22 19:58:30 +0200 | [diff] [blame] | 620 | |
Andrzej Pietrasiewicz | f408757 | 2014-07-22 19:58:33 +0200 | [diff] [blame] | 621 | us = usb_gstrings_attach(cdev, fn_strings, ARRAY_SIZE(strings_fn)); |
| 622 | if (IS_ERR(us)) |
| 623 | return PTR_ERR(us); |
| 624 | iad_desc.iFunction = us[STR_ASSOC].id; |
| 625 | std_ac_if_desc.iInterface = us[STR_IF_CTRL].id; |
| 626 | in_clk_src_desc.iClockSource = us[STR_CLKSRC_IN].id; |
| 627 | out_clk_src_desc.iClockSource = us[STR_CLKSRC_OUT].id; |
| 628 | usb_out_it_desc.iTerminal = us[STR_USB_IT].id; |
| 629 | io_in_it_desc.iTerminal = us[STR_IO_IT].id; |
| 630 | usb_in_ot_desc.iTerminal = us[STR_USB_OT].id; |
| 631 | io_out_ot_desc.iTerminal = us[STR_IO_OT].id; |
| 632 | std_as_out_if0_desc.iInterface = us[STR_AS_OUT_ALT0].id; |
| 633 | std_as_out_if1_desc.iInterface = us[STR_AS_OUT_ALT1].id; |
| 634 | std_as_in_if0_desc.iInterface = us[STR_AS_IN_ALT0].id; |
| 635 | std_as_in_if1_desc.iInterface = us[STR_AS_IN_ALT1].id; |
| 636 | |
Andrzej Pietrasiewicz | f8f93d2 | 2014-07-22 19:58:30 +0200 | [diff] [blame] | 637 | |
Andrzej Pietrasiewicz | f8f93d2 | 2014-07-22 19:58:30 +0200 | [diff] [blame] | 638 | /* Initialize the configurable parameters */ |
| 639 | usb_out_it_desc.bNrChannels = num_channels(uac2_opts->c_chmask); |
| 640 | usb_out_it_desc.bmChannelConfig = cpu_to_le32(uac2_opts->c_chmask); |
| 641 | io_in_it_desc.bNrChannels = num_channels(uac2_opts->p_chmask); |
| 642 | io_in_it_desc.bmChannelConfig = cpu_to_le32(uac2_opts->p_chmask); |
| 643 | as_out_hdr_desc.bNrChannels = num_channels(uac2_opts->c_chmask); |
| 644 | as_out_hdr_desc.bmChannelConfig = cpu_to_le32(uac2_opts->c_chmask); |
| 645 | as_in_hdr_desc.bNrChannels = num_channels(uac2_opts->p_chmask); |
| 646 | as_in_hdr_desc.bmChannelConfig = cpu_to_le32(uac2_opts->p_chmask); |
| 647 | as_out_fmt1_desc.bSubslotSize = uac2_opts->c_ssize; |
| 648 | as_out_fmt1_desc.bBitResolution = uac2_opts->c_ssize * 8; |
| 649 | as_in_fmt1_desc.bSubslotSize = uac2_opts->p_ssize; |
| 650 | as_in_fmt1_desc.bBitResolution = uac2_opts->p_ssize * 8; |
| 651 | |
| 652 | snprintf(clksrc_in, sizeof(clksrc_in), "%uHz", uac2_opts->p_srate); |
| 653 | snprintf(clksrc_out, sizeof(clksrc_out), "%uHz", uac2_opts->c_srate); |
Andrzej Pietrasiewicz | f8f93d2 | 2014-07-22 19:58:30 +0200 | [diff] [blame] | 654 | |
Jassi Brar | 132fcb4 | 2012-02-02 22:01:34 +0530 | [diff] [blame] | 655 | ret = usb_interface_id(cfg, fn); |
| 656 | if (ret < 0) { |
Daniel Mack | a8147da | 2014-08-27 19:09:04 +0200 | [diff] [blame] | 657 | dev_err(dev, "%s:%d Error!\n", __func__, __LINE__); |
Jassi Brar | 132fcb4 | 2012-02-02 22:01:34 +0530 | [diff] [blame] | 658 | return ret; |
| 659 | } |
John Keeping | 8813a59 | 2018-01-12 18:43:32 +0000 | [diff] [blame] | 660 | iad_desc.bFirstInterface = ret; |
| 661 | |
Jassi Brar | 132fcb4 | 2012-02-02 22:01:34 +0530 | [diff] [blame] | 662 | std_ac_if_desc.bInterfaceNumber = ret; |
Ruslan Bilovol | eb9fecb | 2017-06-18 16:23:52 +0300 | [diff] [blame] | 663 | uac2->ac_intf = ret; |
| 664 | uac2->ac_alt = 0; |
Jassi Brar | 132fcb4 | 2012-02-02 22:01:34 +0530 | [diff] [blame] | 665 | |
Andreas Pape | 3fa4eaa | 2018-06-21 17:22:51 +0200 | [diff] [blame] | 666 | if (EPOUT_EN(uac2_opts)) { |
| 667 | ret = usb_interface_id(cfg, fn); |
| 668 | if (ret < 0) { |
| 669 | dev_err(dev, "%s:%d Error!\n", __func__, __LINE__); |
| 670 | return ret; |
| 671 | } |
| 672 | std_as_out_if0_desc.bInterfaceNumber = ret; |
| 673 | std_as_out_if1_desc.bInterfaceNumber = ret; |
| 674 | uac2->as_out_intf = ret; |
| 675 | uac2->as_out_alt = 0; |
Jassi Brar | 132fcb4 | 2012-02-02 22:01:34 +0530 | [diff] [blame] | 676 | } |
Jassi Brar | 132fcb4 | 2012-02-02 22:01:34 +0530 | [diff] [blame] | 677 | |
Andreas Pape | 3fa4eaa | 2018-06-21 17:22:51 +0200 | [diff] [blame] | 678 | if (EPIN_EN(uac2_opts)) { |
| 679 | ret = usb_interface_id(cfg, fn); |
| 680 | if (ret < 0) { |
| 681 | dev_err(dev, "%s:%d Error!\n", __func__, __LINE__); |
| 682 | return ret; |
| 683 | } |
| 684 | std_as_in_if0_desc.bInterfaceNumber = ret; |
| 685 | std_as_in_if1_desc.bInterfaceNumber = ret; |
| 686 | uac2->as_in_intf = ret; |
| 687 | uac2->as_in_alt = 0; |
Jassi Brar | 132fcb4 | 2012-02-02 22:01:34 +0530 | [diff] [blame] | 688 | } |
Jassi Brar | 132fcb4 | 2012-02-02 22:01:34 +0530 | [diff] [blame] | 689 | |
Sekhar Nori | 0db56e4 | 2017-05-17 13:45:17 +0530 | [diff] [blame] | 690 | /* Calculate wMaxPacketSize according to audio bandwidth */ |
Jerome Brunet | 9389044 | 2020-12-21 18:35:28 +0100 | [diff] [blame^] | 691 | ret = set_ep_max_packet_size(uac2_opts, &fs_epin_desc, USB_SPEED_FULL, |
| 692 | true); |
| 693 | if (ret < 0) { |
| 694 | dev_err(dev, "%s:%d Error!\n", __func__, __LINE__); |
| 695 | return ret; |
| 696 | } |
| 697 | |
| 698 | ret = set_ep_max_packet_size(uac2_opts, &fs_epout_desc, USB_SPEED_FULL, |
| 699 | false); |
| 700 | if (ret < 0) { |
| 701 | dev_err(dev, "%s:%d Error!\n", __func__, __LINE__); |
| 702 | return ret; |
| 703 | } |
| 704 | |
| 705 | ret = set_ep_max_packet_size(uac2_opts, &hs_epin_desc, USB_SPEED_HIGH, |
| 706 | true); |
| 707 | if (ret < 0) { |
| 708 | dev_err(dev, "%s:%d Error!\n", __func__, __LINE__); |
| 709 | return ret; |
| 710 | } |
| 711 | |
| 712 | ret = set_ep_max_packet_size(uac2_opts, &hs_epout_desc, USB_SPEED_HIGH, |
| 713 | false); |
| 714 | if (ret < 0) { |
| 715 | dev_err(dev, "%s:%d Error!\n", __func__, __LINE__); |
| 716 | return ret; |
| 717 | } |
Sekhar Nori | 0db56e4 | 2017-05-17 13:45:17 +0530 | [diff] [blame] | 718 | |
Andreas Pape | 3fa4eaa | 2018-06-21 17:22:51 +0200 | [diff] [blame] | 719 | if (EPOUT_EN(uac2_opts)) { |
| 720 | agdev->out_ep = usb_ep_autoconfig(gadget, &fs_epout_desc); |
| 721 | if (!agdev->out_ep) { |
| 722 | dev_err(dev, "%s:%d Error!\n", __func__, __LINE__); |
| 723 | return -ENODEV; |
| 724 | } |
Sebastian Andrzej Siewior | 391aa85 | 2012-10-22 22:14:59 +0200 | [diff] [blame] | 725 | } |
Jassi Brar | 132fcb4 | 2012-02-02 22:01:34 +0530 | [diff] [blame] | 726 | |
Andreas Pape | 3fa4eaa | 2018-06-21 17:22:51 +0200 | [diff] [blame] | 727 | if (EPIN_EN(uac2_opts)) { |
| 728 | agdev->in_ep = usb_ep_autoconfig(gadget, &fs_epin_desc); |
| 729 | if (!agdev->in_ep) { |
| 730 | dev_err(dev, "%s:%d Error!\n", __func__, __LINE__); |
| 731 | return -ENODEV; |
| 732 | } |
Sebastian Andrzej Siewior | 391aa85 | 2012-10-22 22:14:59 +0200 | [diff] [blame] | 733 | } |
Jassi Brar | 132fcb4 | 2012-02-02 22:01:34 +0530 | [diff] [blame] | 734 | |
Ruslan Bilovol | 14e1d56 | 2017-06-25 16:23:47 +0300 | [diff] [blame] | 735 | agdev->in_ep_maxpsize = max_t(u16, |
| 736 | le16_to_cpu(fs_epin_desc.wMaxPacketSize), |
| 737 | le16_to_cpu(hs_epin_desc.wMaxPacketSize)); |
| 738 | agdev->out_ep_maxpsize = max_t(u16, |
| 739 | le16_to_cpu(fs_epout_desc.wMaxPacketSize), |
| 740 | le16_to_cpu(hs_epout_desc.wMaxPacketSize)); |
Jassi Brar | eb127cb | 2013-05-30 18:23:33 +0530 | [diff] [blame] | 741 | |
Jassi Brar | 132fcb4 | 2012-02-02 22:01:34 +0530 | [diff] [blame] | 742 | hs_epout_desc.bEndpointAddress = fs_epout_desc.bEndpointAddress; |
Jassi Brar | 132fcb4 | 2012-02-02 22:01:34 +0530 | [diff] [blame] | 743 | hs_epin_desc.bEndpointAddress = fs_epin_desc.bEndpointAddress; |
Jassi Brar | 132fcb4 | 2012-02-02 22:01:34 +0530 | [diff] [blame] | 744 | |
Andreas Pape | 3fa4eaa | 2018-06-21 17:22:51 +0200 | [diff] [blame] | 745 | setup_descriptor(uac2_opts); |
| 746 | |
John Youn | eaef50c | 2016-02-05 17:06:07 -0800 | [diff] [blame] | 747 | ret = usb_assign_descriptors(fn, fs_audio_desc, hs_audio_desc, NULL, |
| 748 | NULL); |
Sebastian Andrzej Siewior | 10287ba | 2012-10-22 22:15:06 +0200 | [diff] [blame] | 749 | if (ret) |
Peter Chen | f1d3861 | 2016-11-08 10:10:44 +0800 | [diff] [blame] | 750 | return ret; |
Jassi Brar | 132fcb4 | 2012-02-02 22:01:34 +0530 | [diff] [blame] | 751 | |
Ruslan Bilovol | 7158b57 | 2017-06-18 16:23:51 +0300 | [diff] [blame] | 752 | agdev->gadget = gadget; |
| 753 | |
Ruslan Bilovol | eb9fecb | 2017-06-18 16:23:52 +0300 | [diff] [blame] | 754 | agdev->params.p_chmask = uac2_opts->p_chmask; |
| 755 | agdev->params.p_srate = uac2_opts->p_srate; |
| 756 | agdev->params.p_ssize = uac2_opts->p_ssize; |
| 757 | agdev->params.c_chmask = uac2_opts->c_chmask; |
| 758 | agdev->params.c_srate = uac2_opts->c_srate; |
| 759 | agdev->params.c_ssize = uac2_opts->c_ssize; |
| 760 | agdev->params.req_number = uac2_opts->req_number; |
| 761 | ret = g_audio_setup(agdev, "UAC2 PCM", "UAC2_Gadget"); |
Sebastian Andrzej Siewior | 391aa85 | 2012-10-22 22:14:59 +0200 | [diff] [blame] | 762 | if (ret) |
Ruslan Bilovol | eb9fecb | 2017-06-18 16:23:52 +0300 | [diff] [blame] | 763 | goto err_free_descs; |
Sebastian Andrzej Siewior | 391aa85 | 2012-10-22 22:14:59 +0200 | [diff] [blame] | 764 | return 0; |
Pavitrakumar Managutte | d12a872 | 2014-10-22 19:24:58 +0530 | [diff] [blame] | 765 | |
Peter Chen | f1d3861 | 2016-11-08 10:10:44 +0800 | [diff] [blame] | 766 | err_free_descs: |
| 767 | usb_free_all_descriptors(fn); |
Ruslan Bilovol | 7158b57 | 2017-06-18 16:23:51 +0300 | [diff] [blame] | 768 | agdev->gadget = NULL; |
Peter Chen | 88f950a | 2017-01-04 10:19:22 +0800 | [diff] [blame] | 769 | return ret; |
Jassi Brar | 132fcb4 | 2012-02-02 22:01:34 +0530 | [diff] [blame] | 770 | } |
| 771 | |
Jassi Brar | 132fcb4 | 2012-02-02 22:01:34 +0530 | [diff] [blame] | 772 | static int |
| 773 | afunc_set_alt(struct usb_function *fn, unsigned intf, unsigned alt) |
| 774 | { |
| 775 | struct usb_composite_dev *cdev = fn->config->cdev; |
Ruslan Bilovol | eb9fecb | 2017-06-18 16:23:52 +0300 | [diff] [blame] | 776 | struct f_uac2 *uac2 = func_to_uac2(fn); |
Jassi Brar | 132fcb4 | 2012-02-02 22:01:34 +0530 | [diff] [blame] | 777 | struct usb_gadget *gadget = cdev->gadget; |
Ruslan Bilovol | 7158b57 | 2017-06-18 16:23:51 +0300 | [diff] [blame] | 778 | struct device *dev = &gadget->dev; |
Ruslan Bilovol | eb9fecb | 2017-06-18 16:23:52 +0300 | [diff] [blame] | 779 | int ret = 0; |
Jassi Brar | 132fcb4 | 2012-02-02 22:01:34 +0530 | [diff] [blame] | 780 | |
| 781 | /* No i/f has more than 2 alt settings */ |
| 782 | if (alt > 1) { |
Daniel Mack | a8147da | 2014-08-27 19:09:04 +0200 | [diff] [blame] | 783 | dev_err(dev, "%s:%d Error!\n", __func__, __LINE__); |
Jassi Brar | 132fcb4 | 2012-02-02 22:01:34 +0530 | [diff] [blame] | 784 | return -EINVAL; |
| 785 | } |
| 786 | |
Ruslan Bilovol | eb9fecb | 2017-06-18 16:23:52 +0300 | [diff] [blame] | 787 | if (intf == uac2->ac_intf) { |
Jassi Brar | 132fcb4 | 2012-02-02 22:01:34 +0530 | [diff] [blame] | 788 | /* Control I/f has only 1 AltSetting - 0 */ |
| 789 | if (alt) { |
Daniel Mack | a8147da | 2014-08-27 19:09:04 +0200 | [diff] [blame] | 790 | dev_err(dev, "%s:%d Error!\n", __func__, __LINE__); |
Jassi Brar | 132fcb4 | 2012-02-02 22:01:34 +0530 | [diff] [blame] | 791 | return -EINVAL; |
| 792 | } |
| 793 | return 0; |
| 794 | } |
| 795 | |
Ruslan Bilovol | eb9fecb | 2017-06-18 16:23:52 +0300 | [diff] [blame] | 796 | if (intf == uac2->as_out_intf) { |
| 797 | uac2->as_out_alt = alt; |
Daniel Mack | 9bb87f1 | 2014-08-27 19:09:07 +0200 | [diff] [blame] | 798 | |
Ruslan Bilovol | eb9fecb | 2017-06-18 16:23:52 +0300 | [diff] [blame] | 799 | if (alt) |
| 800 | ret = u_audio_start_capture(&uac2->g_audio); |
Daniel Mack | 9bb87f1 | 2014-08-27 19:09:07 +0200 | [diff] [blame] | 801 | else |
Ruslan Bilovol | eb9fecb | 2017-06-18 16:23:52 +0300 | [diff] [blame] | 802 | u_audio_stop_capture(&uac2->g_audio); |
| 803 | } else if (intf == uac2->as_in_intf) { |
| 804 | uac2->as_in_alt = alt; |
Daniel Mack | 9bb87f1 | 2014-08-27 19:09:07 +0200 | [diff] [blame] | 805 | |
Ruslan Bilovol | eb9fecb | 2017-06-18 16:23:52 +0300 | [diff] [blame] | 806 | if (alt) |
| 807 | ret = u_audio_start_playback(&uac2->g_audio); |
| 808 | else |
| 809 | u_audio_stop_playback(&uac2->g_audio); |
Jassi Brar | 132fcb4 | 2012-02-02 22:01:34 +0530 | [diff] [blame] | 810 | } else { |
Daniel Mack | a8147da | 2014-08-27 19:09:04 +0200 | [diff] [blame] | 811 | dev_err(dev, "%s:%d Error!\n", __func__, __LINE__); |
Jassi Brar | 132fcb4 | 2012-02-02 22:01:34 +0530 | [diff] [blame] | 812 | return -EINVAL; |
| 813 | } |
| 814 | |
Ruslan Bilovol | eb9fecb | 2017-06-18 16:23:52 +0300 | [diff] [blame] | 815 | return ret; |
Jassi Brar | 132fcb4 | 2012-02-02 22:01:34 +0530 | [diff] [blame] | 816 | } |
| 817 | |
| 818 | static int |
| 819 | afunc_get_alt(struct usb_function *fn, unsigned intf) |
| 820 | { |
Ruslan Bilovol | eb9fecb | 2017-06-18 16:23:52 +0300 | [diff] [blame] | 821 | struct f_uac2 *uac2 = func_to_uac2(fn); |
| 822 | struct g_audio *agdev = func_to_g_audio(fn); |
Jassi Brar | 132fcb4 | 2012-02-02 22:01:34 +0530 | [diff] [blame] | 823 | |
Ruslan Bilovol | eb9fecb | 2017-06-18 16:23:52 +0300 | [diff] [blame] | 824 | if (intf == uac2->ac_intf) |
| 825 | return uac2->ac_alt; |
| 826 | else if (intf == uac2->as_out_intf) |
| 827 | return uac2->as_out_alt; |
| 828 | else if (intf == uac2->as_in_intf) |
| 829 | return uac2->as_in_alt; |
Jassi Brar | 132fcb4 | 2012-02-02 22:01:34 +0530 | [diff] [blame] | 830 | else |
Ruslan Bilovol | 7158b57 | 2017-06-18 16:23:51 +0300 | [diff] [blame] | 831 | dev_err(&agdev->gadget->dev, |
Jassi Brar | 132fcb4 | 2012-02-02 22:01:34 +0530 | [diff] [blame] | 832 | "%s:%d Invalid Interface %d!\n", |
| 833 | __func__, __LINE__, intf); |
| 834 | |
| 835 | return -EINVAL; |
| 836 | } |
| 837 | |
| 838 | static void |
| 839 | afunc_disable(struct usb_function *fn) |
| 840 | { |
Ruslan Bilovol | eb9fecb | 2017-06-18 16:23:52 +0300 | [diff] [blame] | 841 | struct f_uac2 *uac2 = func_to_uac2(fn); |
Jassi Brar | 132fcb4 | 2012-02-02 22:01:34 +0530 | [diff] [blame] | 842 | |
Ruslan Bilovol | eb9fecb | 2017-06-18 16:23:52 +0300 | [diff] [blame] | 843 | uac2->as_in_alt = 0; |
| 844 | uac2->as_out_alt = 0; |
| 845 | u_audio_stop_capture(&uac2->g_audio); |
| 846 | u_audio_stop_playback(&uac2->g_audio); |
Jassi Brar | 132fcb4 | 2012-02-02 22:01:34 +0530 | [diff] [blame] | 847 | } |
| 848 | |
| 849 | static int |
| 850 | in_rq_cur(struct usb_function *fn, const struct usb_ctrlrequest *cr) |
| 851 | { |
| 852 | struct usb_request *req = fn->config->cdev->req; |
Ruslan Bilovol | eb9fecb | 2017-06-18 16:23:52 +0300 | [diff] [blame] | 853 | struct g_audio *agdev = func_to_g_audio(fn); |
Andrzej Pietrasiewicz | f8f93d2 | 2014-07-22 19:58:30 +0200 | [diff] [blame] | 854 | struct f_uac2_opts *opts; |
Jassi Brar | 132fcb4 | 2012-02-02 22:01:34 +0530 | [diff] [blame] | 855 | u16 w_length = le16_to_cpu(cr->wLength); |
| 856 | u16 w_index = le16_to_cpu(cr->wIndex); |
| 857 | u16 w_value = le16_to_cpu(cr->wValue); |
| 858 | u8 entity_id = (w_index >> 8) & 0xff; |
| 859 | u8 control_selector = w_value >> 8; |
| 860 | int value = -EOPNOTSUPP; |
Andrzej Pietrasiewicz | f8f93d2 | 2014-07-22 19:58:30 +0200 | [diff] [blame] | 861 | int p_srate, c_srate; |
| 862 | |
Ruslan Bilovol | eb9fecb | 2017-06-18 16:23:52 +0300 | [diff] [blame] | 863 | opts = g_audio_to_uac2_opts(agdev); |
Andrzej Pietrasiewicz | f8f93d2 | 2014-07-22 19:58:30 +0200 | [diff] [blame] | 864 | p_srate = opts->p_srate; |
| 865 | c_srate = opts->c_srate; |
Jassi Brar | 132fcb4 | 2012-02-02 22:01:34 +0530 | [diff] [blame] | 866 | |
| 867 | if (control_selector == UAC2_CS_CONTROL_SAM_FREQ) { |
| 868 | struct cntrl_cur_lay3 c; |
Heinrich Schuchardt | ffeee83 | 2016-05-08 23:20:59 +0200 | [diff] [blame] | 869 | memset(&c, 0, sizeof(struct cntrl_cur_lay3)); |
Jassi Brar | 132fcb4 | 2012-02-02 22:01:34 +0530 | [diff] [blame] | 870 | |
| 871 | if (entity_id == USB_IN_CLK_ID) |
Eugeniu Rosca | eec24f2 | 2018-07-02 23:46:47 +0200 | [diff] [blame] | 872 | c.dCUR = cpu_to_le32(p_srate); |
Jassi Brar | 132fcb4 | 2012-02-02 22:01:34 +0530 | [diff] [blame] | 873 | else if (entity_id == USB_OUT_CLK_ID) |
Eugeniu Rosca | eec24f2 | 2018-07-02 23:46:47 +0200 | [diff] [blame] | 874 | c.dCUR = cpu_to_le32(c_srate); |
Jassi Brar | 132fcb4 | 2012-02-02 22:01:34 +0530 | [diff] [blame] | 875 | |
| 876 | value = min_t(unsigned, w_length, sizeof c); |
| 877 | memcpy(req->buf, &c, value); |
| 878 | } else if (control_selector == UAC2_CS_CONTROL_CLOCK_VALID) { |
| 879 | *(u8 *)req->buf = 1; |
| 880 | value = min_t(unsigned, w_length, 1); |
| 881 | } else { |
Ruslan Bilovol | 7158b57 | 2017-06-18 16:23:51 +0300 | [diff] [blame] | 882 | dev_err(&agdev->gadget->dev, |
Jassi Brar | 132fcb4 | 2012-02-02 22:01:34 +0530 | [diff] [blame] | 883 | "%s:%d control_selector=%d TODO!\n", |
| 884 | __func__, __LINE__, control_selector); |
| 885 | } |
| 886 | |
| 887 | return value; |
| 888 | } |
| 889 | |
| 890 | static int |
| 891 | in_rq_range(struct usb_function *fn, const struct usb_ctrlrequest *cr) |
| 892 | { |
| 893 | struct usb_request *req = fn->config->cdev->req; |
Ruslan Bilovol | eb9fecb | 2017-06-18 16:23:52 +0300 | [diff] [blame] | 894 | struct g_audio *agdev = func_to_g_audio(fn); |
Andrzej Pietrasiewicz | f8f93d2 | 2014-07-22 19:58:30 +0200 | [diff] [blame] | 895 | struct f_uac2_opts *opts; |
Jassi Brar | 132fcb4 | 2012-02-02 22:01:34 +0530 | [diff] [blame] | 896 | u16 w_length = le16_to_cpu(cr->wLength); |
| 897 | u16 w_index = le16_to_cpu(cr->wIndex); |
| 898 | u16 w_value = le16_to_cpu(cr->wValue); |
| 899 | u8 entity_id = (w_index >> 8) & 0xff; |
| 900 | u8 control_selector = w_value >> 8; |
| 901 | struct cntrl_range_lay3 r; |
| 902 | int value = -EOPNOTSUPP; |
Andrzej Pietrasiewicz | f8f93d2 | 2014-07-22 19:58:30 +0200 | [diff] [blame] | 903 | int p_srate, c_srate; |
| 904 | |
Ruslan Bilovol | eb9fecb | 2017-06-18 16:23:52 +0300 | [diff] [blame] | 905 | opts = g_audio_to_uac2_opts(agdev); |
Andrzej Pietrasiewicz | f8f93d2 | 2014-07-22 19:58:30 +0200 | [diff] [blame] | 906 | p_srate = opts->p_srate; |
| 907 | c_srate = opts->c_srate; |
Jassi Brar | 132fcb4 | 2012-02-02 22:01:34 +0530 | [diff] [blame] | 908 | |
| 909 | if (control_selector == UAC2_CS_CONTROL_SAM_FREQ) { |
| 910 | if (entity_id == USB_IN_CLK_ID) |
Eugeniu Rosca | eec24f2 | 2018-07-02 23:46:47 +0200 | [diff] [blame] | 911 | r.dMIN = cpu_to_le32(p_srate); |
Jassi Brar | 132fcb4 | 2012-02-02 22:01:34 +0530 | [diff] [blame] | 912 | else if (entity_id == USB_OUT_CLK_ID) |
Eugeniu Rosca | eec24f2 | 2018-07-02 23:46:47 +0200 | [diff] [blame] | 913 | r.dMIN = cpu_to_le32(c_srate); |
Jassi Brar | 132fcb4 | 2012-02-02 22:01:34 +0530 | [diff] [blame] | 914 | else |
| 915 | return -EOPNOTSUPP; |
| 916 | |
| 917 | r.dMAX = r.dMIN; |
| 918 | r.dRES = 0; |
Eugeniu Rosca | eec24f2 | 2018-07-02 23:46:47 +0200 | [diff] [blame] | 919 | r.wNumSubRanges = cpu_to_le16(1); |
Jassi Brar | 132fcb4 | 2012-02-02 22:01:34 +0530 | [diff] [blame] | 920 | |
| 921 | value = min_t(unsigned, w_length, sizeof r); |
| 922 | memcpy(req->buf, &r, value); |
| 923 | } else { |
Ruslan Bilovol | 7158b57 | 2017-06-18 16:23:51 +0300 | [diff] [blame] | 924 | dev_err(&agdev->gadget->dev, |
Jassi Brar | 132fcb4 | 2012-02-02 22:01:34 +0530 | [diff] [blame] | 925 | "%s:%d control_selector=%d TODO!\n", |
| 926 | __func__, __LINE__, control_selector); |
| 927 | } |
| 928 | |
| 929 | return value; |
| 930 | } |
| 931 | |
| 932 | static int |
| 933 | ac_rq_in(struct usb_function *fn, const struct usb_ctrlrequest *cr) |
| 934 | { |
| 935 | if (cr->bRequest == UAC2_CS_CUR) |
| 936 | return in_rq_cur(fn, cr); |
| 937 | else if (cr->bRequest == UAC2_CS_RANGE) |
| 938 | return in_rq_range(fn, cr); |
| 939 | else |
| 940 | return -EOPNOTSUPP; |
| 941 | } |
| 942 | |
| 943 | static int |
| 944 | out_rq_cur(struct usb_function *fn, const struct usb_ctrlrequest *cr) |
| 945 | { |
| 946 | u16 w_length = le16_to_cpu(cr->wLength); |
| 947 | u16 w_value = le16_to_cpu(cr->wValue); |
| 948 | u8 control_selector = w_value >> 8; |
| 949 | |
| 950 | if (control_selector == UAC2_CS_CONTROL_SAM_FREQ) |
| 951 | return w_length; |
| 952 | |
| 953 | return -EOPNOTSUPP; |
| 954 | } |
| 955 | |
| 956 | static int |
| 957 | setup_rq_inf(struct usb_function *fn, const struct usb_ctrlrequest *cr) |
| 958 | { |
Ruslan Bilovol | eb9fecb | 2017-06-18 16:23:52 +0300 | [diff] [blame] | 959 | struct f_uac2 *uac2 = func_to_uac2(fn); |
| 960 | struct g_audio *agdev = func_to_g_audio(fn); |
Jassi Brar | 132fcb4 | 2012-02-02 22:01:34 +0530 | [diff] [blame] | 961 | u16 w_index = le16_to_cpu(cr->wIndex); |
| 962 | u8 intf = w_index & 0xff; |
| 963 | |
Ruslan Bilovol | eb9fecb | 2017-06-18 16:23:52 +0300 | [diff] [blame] | 964 | if (intf != uac2->ac_intf) { |
Ruslan Bilovol | 7158b57 | 2017-06-18 16:23:51 +0300 | [diff] [blame] | 965 | dev_err(&agdev->gadget->dev, |
Jassi Brar | 132fcb4 | 2012-02-02 22:01:34 +0530 | [diff] [blame] | 966 | "%s:%d Error!\n", __func__, __LINE__); |
| 967 | return -EOPNOTSUPP; |
| 968 | } |
| 969 | |
| 970 | if (cr->bRequestType & USB_DIR_IN) |
| 971 | return ac_rq_in(fn, cr); |
| 972 | else if (cr->bRequest == UAC2_CS_CUR) |
| 973 | return out_rq_cur(fn, cr); |
| 974 | |
| 975 | return -EOPNOTSUPP; |
| 976 | } |
| 977 | |
| 978 | static int |
| 979 | afunc_setup(struct usb_function *fn, const struct usb_ctrlrequest *cr) |
| 980 | { |
| 981 | struct usb_composite_dev *cdev = fn->config->cdev; |
Ruslan Bilovol | eb9fecb | 2017-06-18 16:23:52 +0300 | [diff] [blame] | 982 | struct g_audio *agdev = func_to_g_audio(fn); |
Jassi Brar | 132fcb4 | 2012-02-02 22:01:34 +0530 | [diff] [blame] | 983 | struct usb_request *req = cdev->req; |
| 984 | u16 w_length = le16_to_cpu(cr->wLength); |
| 985 | int value = -EOPNOTSUPP; |
| 986 | |
| 987 | /* Only Class specific requests are supposed to reach here */ |
| 988 | if ((cr->bRequestType & USB_TYPE_MASK) != USB_TYPE_CLASS) |
| 989 | return -EOPNOTSUPP; |
| 990 | |
| 991 | if ((cr->bRequestType & USB_RECIP_MASK) == USB_RECIP_INTERFACE) |
| 992 | value = setup_rq_inf(fn, cr); |
| 993 | else |
Ruslan Bilovol | 7158b57 | 2017-06-18 16:23:51 +0300 | [diff] [blame] | 994 | dev_err(&agdev->gadget->dev, "%s:%d Error!\n", |
| 995 | __func__, __LINE__); |
Jassi Brar | 132fcb4 | 2012-02-02 22:01:34 +0530 | [diff] [blame] | 996 | |
| 997 | if (value >= 0) { |
| 998 | req->length = value; |
| 999 | req->zero = value < w_length; |
| 1000 | value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC); |
| 1001 | if (value < 0) { |
Ruslan Bilovol | 7158b57 | 2017-06-18 16:23:51 +0300 | [diff] [blame] | 1002 | dev_err(&agdev->gadget->dev, |
Jassi Brar | 132fcb4 | 2012-02-02 22:01:34 +0530 | [diff] [blame] | 1003 | "%s:%d Error!\n", __func__, __LINE__); |
| 1004 | req->status = 0; |
| 1005 | } |
| 1006 | } |
| 1007 | |
| 1008 | return value; |
| 1009 | } |
| 1010 | |
Andrzej Pietrasiewicz | 3aeea3c | 2014-07-22 19:58:35 +0200 | [diff] [blame] | 1011 | static inline struct f_uac2_opts *to_f_uac2_opts(struct config_item *item) |
| 1012 | { |
| 1013 | return container_of(to_config_group(item), struct f_uac2_opts, |
| 1014 | func_inst.group); |
| 1015 | } |
| 1016 | |
Andrzej Pietrasiewicz | 3aeea3c | 2014-07-22 19:58:35 +0200 | [diff] [blame] | 1017 | static void f_uac2_attr_release(struct config_item *item) |
| 1018 | { |
| 1019 | struct f_uac2_opts *opts = to_f_uac2_opts(item); |
| 1020 | |
| 1021 | usb_put_function_instance(&opts->func_inst); |
| 1022 | } |
| 1023 | |
| 1024 | static struct configfs_item_operations f_uac2_item_ops = { |
| 1025 | .release = f_uac2_attr_release, |
Andrzej Pietrasiewicz | 3aeea3c | 2014-07-22 19:58:35 +0200 | [diff] [blame] | 1026 | }; |
| 1027 | |
| 1028 | #define UAC2_ATTRIBUTE(name) \ |
Christoph Hellwig | 495702b | 2015-10-03 15:32:49 +0200 | [diff] [blame] | 1029 | static ssize_t f_uac2_opts_##name##_show(struct config_item *item, \ |
Andrzej Pietrasiewicz | 3aeea3c | 2014-07-22 19:58:35 +0200 | [diff] [blame] | 1030 | char *page) \ |
| 1031 | { \ |
Christoph Hellwig | 495702b | 2015-10-03 15:32:49 +0200 | [diff] [blame] | 1032 | struct f_uac2_opts *opts = to_f_uac2_opts(item); \ |
Andrzej Pietrasiewicz | 3aeea3c | 2014-07-22 19:58:35 +0200 | [diff] [blame] | 1033 | int result; \ |
| 1034 | \ |
| 1035 | mutex_lock(&opts->lock); \ |
| 1036 | result = sprintf(page, "%u\n", opts->name); \ |
| 1037 | mutex_unlock(&opts->lock); \ |
| 1038 | \ |
| 1039 | return result; \ |
| 1040 | } \ |
| 1041 | \ |
Christoph Hellwig | 495702b | 2015-10-03 15:32:49 +0200 | [diff] [blame] | 1042 | static ssize_t f_uac2_opts_##name##_store(struct config_item *item, \ |
Andrzej Pietrasiewicz | 3aeea3c | 2014-07-22 19:58:35 +0200 | [diff] [blame] | 1043 | const char *page, size_t len) \ |
| 1044 | { \ |
Christoph Hellwig | 495702b | 2015-10-03 15:32:49 +0200 | [diff] [blame] | 1045 | struct f_uac2_opts *opts = to_f_uac2_opts(item); \ |
Andrzej Pietrasiewicz | 3aeea3c | 2014-07-22 19:58:35 +0200 | [diff] [blame] | 1046 | int ret; \ |
| 1047 | u32 num; \ |
| 1048 | \ |
| 1049 | mutex_lock(&opts->lock); \ |
| 1050 | if (opts->refcnt) { \ |
| 1051 | ret = -EBUSY; \ |
| 1052 | goto end; \ |
| 1053 | } \ |
| 1054 | \ |
| 1055 | ret = kstrtou32(page, 0, &num); \ |
| 1056 | if (ret) \ |
| 1057 | goto end; \ |
| 1058 | \ |
| 1059 | opts->name = num; \ |
| 1060 | ret = len; \ |
| 1061 | \ |
| 1062 | end: \ |
| 1063 | mutex_unlock(&opts->lock); \ |
| 1064 | return ret; \ |
| 1065 | } \ |
| 1066 | \ |
Christoph Hellwig | 495702b | 2015-10-03 15:32:49 +0200 | [diff] [blame] | 1067 | CONFIGFS_ATTR(f_uac2_opts_, name) |
Andrzej Pietrasiewicz | 3aeea3c | 2014-07-22 19:58:35 +0200 | [diff] [blame] | 1068 | |
| 1069 | UAC2_ATTRIBUTE(p_chmask); |
| 1070 | UAC2_ATTRIBUTE(p_srate); |
| 1071 | UAC2_ATTRIBUTE(p_ssize); |
| 1072 | UAC2_ATTRIBUTE(c_chmask); |
| 1073 | UAC2_ATTRIBUTE(c_srate); |
| 1074 | UAC2_ATTRIBUTE(c_ssize); |
Peter Chen | e92b9d4 | 2017-01-04 10:19:23 +0800 | [diff] [blame] | 1075 | UAC2_ATTRIBUTE(req_number); |
Andrzej Pietrasiewicz | 3aeea3c | 2014-07-22 19:58:35 +0200 | [diff] [blame] | 1076 | |
| 1077 | static struct configfs_attribute *f_uac2_attrs[] = { |
Christoph Hellwig | 495702b | 2015-10-03 15:32:49 +0200 | [diff] [blame] | 1078 | &f_uac2_opts_attr_p_chmask, |
| 1079 | &f_uac2_opts_attr_p_srate, |
| 1080 | &f_uac2_opts_attr_p_ssize, |
| 1081 | &f_uac2_opts_attr_c_chmask, |
| 1082 | &f_uac2_opts_attr_c_srate, |
| 1083 | &f_uac2_opts_attr_c_ssize, |
Peter Chen | e92b9d4 | 2017-01-04 10:19:23 +0800 | [diff] [blame] | 1084 | &f_uac2_opts_attr_req_number, |
Andrzej Pietrasiewicz | 3aeea3c | 2014-07-22 19:58:35 +0200 | [diff] [blame] | 1085 | NULL, |
| 1086 | }; |
| 1087 | |
Bhumika Goyal | 9736390 | 2017-10-16 17:18:41 +0200 | [diff] [blame] | 1088 | static const struct config_item_type f_uac2_func_type = { |
Andrzej Pietrasiewicz | 3aeea3c | 2014-07-22 19:58:35 +0200 | [diff] [blame] | 1089 | .ct_item_ops = &f_uac2_item_ops, |
| 1090 | .ct_attrs = f_uac2_attrs, |
| 1091 | .ct_owner = THIS_MODULE, |
| 1092 | }; |
| 1093 | |
Andrzej Pietrasiewicz | f8f93d2 | 2014-07-22 19:58:30 +0200 | [diff] [blame] | 1094 | static void afunc_free_inst(struct usb_function_instance *f) |
| 1095 | { |
| 1096 | struct f_uac2_opts *opts; |
| 1097 | |
| 1098 | opts = container_of(f, struct f_uac2_opts, func_inst); |
| 1099 | kfree(opts); |
| 1100 | } |
| 1101 | |
| 1102 | static struct usb_function_instance *afunc_alloc_inst(void) |
| 1103 | { |
| 1104 | struct f_uac2_opts *opts; |
| 1105 | |
| 1106 | opts = kzalloc(sizeof(*opts), GFP_KERNEL); |
| 1107 | if (!opts) |
| 1108 | return ERR_PTR(-ENOMEM); |
| 1109 | |
Andrzej Pietrasiewicz | 3aeea3c | 2014-07-22 19:58:35 +0200 | [diff] [blame] | 1110 | mutex_init(&opts->lock); |
Andrzej Pietrasiewicz | f8f93d2 | 2014-07-22 19:58:30 +0200 | [diff] [blame] | 1111 | opts->func_inst.free_func_inst = afunc_free_inst; |
| 1112 | |
Andrzej Pietrasiewicz | 3aeea3c | 2014-07-22 19:58:35 +0200 | [diff] [blame] | 1113 | config_group_init_type_name(&opts->func_inst.group, "", |
| 1114 | &f_uac2_func_type); |
| 1115 | |
| 1116 | opts->p_chmask = UAC2_DEF_PCHMASK; |
| 1117 | opts->p_srate = UAC2_DEF_PSRATE; |
| 1118 | opts->p_ssize = UAC2_DEF_PSSIZE; |
| 1119 | opts->c_chmask = UAC2_DEF_CCHMASK; |
| 1120 | opts->c_srate = UAC2_DEF_CSRATE; |
| 1121 | opts->c_ssize = UAC2_DEF_CSSIZE; |
Peter Chen | e92b9d4 | 2017-01-04 10:19:23 +0800 | [diff] [blame] | 1122 | opts->req_number = UAC2_DEF_REQ_NUM; |
Andrzej Pietrasiewicz | f8f93d2 | 2014-07-22 19:58:30 +0200 | [diff] [blame] | 1123 | return &opts->func_inst; |
| 1124 | } |
| 1125 | |
| 1126 | static void afunc_free(struct usb_function *f) |
| 1127 | { |
Ruslan Bilovol | eb9fecb | 2017-06-18 16:23:52 +0300 | [diff] [blame] | 1128 | struct g_audio *agdev; |
Andrzej Pietrasiewicz | 3aeea3c | 2014-07-22 19:58:35 +0200 | [diff] [blame] | 1129 | struct f_uac2_opts *opts; |
Andrzej Pietrasiewicz | f8f93d2 | 2014-07-22 19:58:30 +0200 | [diff] [blame] | 1130 | |
Ruslan Bilovol | eb9fecb | 2017-06-18 16:23:52 +0300 | [diff] [blame] | 1131 | agdev = func_to_g_audio(f); |
Andrzej Pietrasiewicz | 3aeea3c | 2014-07-22 19:58:35 +0200 | [diff] [blame] | 1132 | opts = container_of(f->fi, struct f_uac2_opts, func_inst); |
Andrzej Pietrasiewicz | f8f93d2 | 2014-07-22 19:58:30 +0200 | [diff] [blame] | 1133 | kfree(agdev); |
Andrzej Pietrasiewicz | 3aeea3c | 2014-07-22 19:58:35 +0200 | [diff] [blame] | 1134 | mutex_lock(&opts->lock); |
| 1135 | --opts->refcnt; |
| 1136 | mutex_unlock(&opts->lock); |
Andrzej Pietrasiewicz | f8f93d2 | 2014-07-22 19:58:30 +0200 | [diff] [blame] | 1137 | } |
| 1138 | |
| 1139 | static void afunc_unbind(struct usb_configuration *c, struct usb_function *f) |
| 1140 | { |
Ruslan Bilovol | eb9fecb | 2017-06-18 16:23:52 +0300 | [diff] [blame] | 1141 | struct g_audio *agdev = func_to_g_audio(f); |
Andrzej Pietrasiewicz | f8f93d2 | 2014-07-22 19:58:30 +0200 | [diff] [blame] | 1142 | |
Ruslan Bilovol | eb9fecb | 2017-06-18 16:23:52 +0300 | [diff] [blame] | 1143 | g_audio_cleanup(agdev); |
Andrzej Pietrasiewicz | f8f93d2 | 2014-07-22 19:58:30 +0200 | [diff] [blame] | 1144 | usb_free_all_descriptors(f); |
Ruslan Bilovol | 7158b57 | 2017-06-18 16:23:51 +0300 | [diff] [blame] | 1145 | |
| 1146 | agdev->gadget = NULL; |
Andrzej Pietrasiewicz | f8f93d2 | 2014-07-22 19:58:30 +0200 | [diff] [blame] | 1147 | } |
| 1148 | |
Lad, Prabhakar | ef16e7c | 2015-02-04 18:01:11 +0000 | [diff] [blame] | 1149 | static struct usb_function *afunc_alloc(struct usb_function_instance *fi) |
Andrzej Pietrasiewicz | f8f93d2 | 2014-07-22 19:58:30 +0200 | [diff] [blame] | 1150 | { |
Ruslan Bilovol | eb9fecb | 2017-06-18 16:23:52 +0300 | [diff] [blame] | 1151 | struct f_uac2 *uac2; |
Andrzej Pietrasiewicz | f8f93d2 | 2014-07-22 19:58:30 +0200 | [diff] [blame] | 1152 | struct f_uac2_opts *opts; |
| 1153 | |
Ruslan Bilovol | eb9fecb | 2017-06-18 16:23:52 +0300 | [diff] [blame] | 1154 | uac2 = kzalloc(sizeof(*uac2), GFP_KERNEL); |
| 1155 | if (uac2 == NULL) |
Andrzej Pietrasiewicz | f8f93d2 | 2014-07-22 19:58:30 +0200 | [diff] [blame] | 1156 | return ERR_PTR(-ENOMEM); |
| 1157 | |
| 1158 | opts = container_of(fi, struct f_uac2_opts, func_inst); |
Andrzej Pietrasiewicz | 3aeea3c | 2014-07-22 19:58:35 +0200 | [diff] [blame] | 1159 | mutex_lock(&opts->lock); |
| 1160 | ++opts->refcnt; |
| 1161 | mutex_unlock(&opts->lock); |
Andrzej Pietrasiewicz | f8f93d2 | 2014-07-22 19:58:30 +0200 | [diff] [blame] | 1162 | |
Ruslan Bilovol | eb9fecb | 2017-06-18 16:23:52 +0300 | [diff] [blame] | 1163 | uac2->g_audio.func.name = "uac2_func"; |
| 1164 | uac2->g_audio.func.bind = afunc_bind; |
| 1165 | uac2->g_audio.func.unbind = afunc_unbind; |
| 1166 | uac2->g_audio.func.set_alt = afunc_set_alt; |
| 1167 | uac2->g_audio.func.get_alt = afunc_get_alt; |
| 1168 | uac2->g_audio.func.disable = afunc_disable; |
| 1169 | uac2->g_audio.func.setup = afunc_setup; |
| 1170 | uac2->g_audio.func.free_func = afunc_free; |
Andrzej Pietrasiewicz | f8f93d2 | 2014-07-22 19:58:30 +0200 | [diff] [blame] | 1171 | |
Ruslan Bilovol | eb9fecb | 2017-06-18 16:23:52 +0300 | [diff] [blame] | 1172 | return &uac2->g_audio.func; |
Andrzej Pietrasiewicz | f8f93d2 | 2014-07-22 19:58:30 +0200 | [diff] [blame] | 1173 | } |
| 1174 | |
| 1175 | DECLARE_USB_FUNCTION_INIT(uac2, afunc_alloc_inst, afunc_alloc); |
| 1176 | MODULE_LICENSE("GPL"); |
| 1177 | MODULE_AUTHOR("Yadwinder Singh"); |
| 1178 | MODULE_AUTHOR("Jaswinder Singh"); |