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