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_UNDEFINED), 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_UNDEFINED), 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 static struct usb_endpoint_descriptor ss_epout_desc = { 291 .bLength = USB_DT_ENDPOINT_SIZE, 292 .bDescriptorType = USB_DT_ENDPOINT, 293 294 .bEndpointAddress = USB_DIR_OUT, 295 .bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC, 296 /* .wMaxPacketSize = DYNAMIC */ 297 .bInterval = 4, 298 }; 299 300 static struct usb_ss_ep_comp_descriptor ss_epout_desc_comp = { 301 .bLength = sizeof(ss_epout_desc_comp), 302 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP, 303 .bMaxBurst = 0, 304 .bmAttributes = 0, 305 /* wBytesPerInterval = DYNAMIC */ 306 }; 307 308 /* CS AS ISO OUT Endpoint */ 309 static struct uac2_iso_endpoint_descriptor as_iso_out_desc = { 310 .bLength = sizeof as_iso_out_desc, 311 .bDescriptorType = USB_DT_CS_ENDPOINT, 312 313 .bDescriptorSubtype = UAC_EP_GENERAL, 314 .bmAttributes = 0, 315 .bmControls = 0, 316 .bLockDelayUnits = 0, 317 .wLockDelay = 0, 318 }; 319 320 /* Audio Streaming IN Interface - Alt0 */ 321 static struct usb_interface_descriptor std_as_in_if0_desc = { 322 .bLength = sizeof std_as_in_if0_desc, 323 .bDescriptorType = USB_DT_INTERFACE, 324 325 .bAlternateSetting = 0, 326 .bNumEndpoints = 0, 327 .bInterfaceClass = USB_CLASS_AUDIO, 328 .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING, 329 .bInterfaceProtocol = UAC_VERSION_2, 330 }; 331 332 /* Audio Streaming IN Interface - Alt1 */ 333 static struct usb_interface_descriptor std_as_in_if1_desc = { 334 .bLength = sizeof std_as_in_if1_desc, 335 .bDescriptorType = USB_DT_INTERFACE, 336 337 .bAlternateSetting = 1, 338 .bNumEndpoints = 1, 339 .bInterfaceClass = USB_CLASS_AUDIO, 340 .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING, 341 .bInterfaceProtocol = UAC_VERSION_2, 342 }; 343 344 /* Audio Stream IN Intface Desc */ 345 static struct uac2_as_header_descriptor as_in_hdr_desc = { 346 .bLength = sizeof as_in_hdr_desc, 347 .bDescriptorType = USB_DT_CS_INTERFACE, 348 349 .bDescriptorSubtype = UAC_AS_GENERAL, 350 /* .bTerminalLink = DYNAMIC */ 351 .bmControls = 0, 352 .bFormatType = UAC_FORMAT_TYPE_I, 353 .bmFormats = cpu_to_le32(UAC_FORMAT_TYPE_I_PCM), 354 .iChannelNames = 0, 355 }; 356 357 /* Audio USB_IN Format */ 358 static struct uac2_format_type_i_descriptor as_in_fmt1_desc = { 359 .bLength = sizeof as_in_fmt1_desc, 360 .bDescriptorType = USB_DT_CS_INTERFACE, 361 .bDescriptorSubtype = UAC_FORMAT_TYPE, 362 .bFormatType = UAC_FORMAT_TYPE_I, 363 }; 364 365 /* STD AS ISO IN Endpoint */ 366 static struct usb_endpoint_descriptor fs_epin_desc = { 367 .bLength = USB_DT_ENDPOINT_SIZE, 368 .bDescriptorType = USB_DT_ENDPOINT, 369 370 .bEndpointAddress = USB_DIR_IN, 371 .bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC, 372 /* .wMaxPacketSize = DYNAMIC */ 373 .bInterval = 1, 374 }; 375 376 static struct usb_endpoint_descriptor hs_epin_desc = { 377 .bLength = USB_DT_ENDPOINT_SIZE, 378 .bDescriptorType = USB_DT_ENDPOINT, 379 380 .bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC, 381 /* .wMaxPacketSize = DYNAMIC */ 382 .bInterval = 4, 383 }; 384 385 static struct usb_endpoint_descriptor ss_epin_desc = { 386 .bLength = USB_DT_ENDPOINT_SIZE, 387 .bDescriptorType = USB_DT_ENDPOINT, 388 389 .bEndpointAddress = USB_DIR_IN, 390 .bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC, 391 /* .wMaxPacketSize = DYNAMIC */ 392 .bInterval = 4, 393 }; 394 395 static struct usb_ss_ep_comp_descriptor ss_epin_desc_comp = { 396 .bLength = sizeof(ss_epin_desc_comp), 397 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP, 398 .bMaxBurst = 0, 399 .bmAttributes = 0, 400 /* wBytesPerInterval = DYNAMIC */ 401 }; 402 403 /* CS AS ISO IN Endpoint */ 404 static struct uac2_iso_endpoint_descriptor as_iso_in_desc = { 405 .bLength = sizeof as_iso_in_desc, 406 .bDescriptorType = USB_DT_CS_ENDPOINT, 407 408 .bDescriptorSubtype = UAC_EP_GENERAL, 409 .bmAttributes = 0, 410 .bmControls = 0, 411 .bLockDelayUnits = 0, 412 .wLockDelay = 0, 413 }; 414 415 static struct usb_descriptor_header *fs_audio_desc[] = { 416 (struct usb_descriptor_header *)&iad_desc, 417 (struct usb_descriptor_header *)&std_ac_if_desc, 418 419 (struct usb_descriptor_header *)&ac_hdr_desc, 420 (struct usb_descriptor_header *)&in_clk_src_desc, 421 (struct usb_descriptor_header *)&out_clk_src_desc, 422 (struct usb_descriptor_header *)&usb_out_it_desc, 423 (struct usb_descriptor_header *)&io_in_it_desc, 424 (struct usb_descriptor_header *)&usb_in_ot_desc, 425 (struct usb_descriptor_header *)&io_out_ot_desc, 426 427 (struct usb_descriptor_header *)&std_as_out_if0_desc, 428 (struct usb_descriptor_header *)&std_as_out_if1_desc, 429 430 (struct usb_descriptor_header *)&as_out_hdr_desc, 431 (struct usb_descriptor_header *)&as_out_fmt1_desc, 432 (struct usb_descriptor_header *)&fs_epout_desc, 433 (struct usb_descriptor_header *)&as_iso_out_desc, 434 435 (struct usb_descriptor_header *)&std_as_in_if0_desc, 436 (struct usb_descriptor_header *)&std_as_in_if1_desc, 437 438 (struct usb_descriptor_header *)&as_in_hdr_desc, 439 (struct usb_descriptor_header *)&as_in_fmt1_desc, 440 (struct usb_descriptor_header *)&fs_epin_desc, 441 (struct usb_descriptor_header *)&as_iso_in_desc, 442 NULL, 443 }; 444 445 static struct usb_descriptor_header *hs_audio_desc[] = { 446 (struct usb_descriptor_header *)&iad_desc, 447 (struct usb_descriptor_header *)&std_ac_if_desc, 448 449 (struct usb_descriptor_header *)&ac_hdr_desc, 450 (struct usb_descriptor_header *)&in_clk_src_desc, 451 (struct usb_descriptor_header *)&out_clk_src_desc, 452 (struct usb_descriptor_header *)&usb_out_it_desc, 453 (struct usb_descriptor_header *)&io_in_it_desc, 454 (struct usb_descriptor_header *)&usb_in_ot_desc, 455 (struct usb_descriptor_header *)&io_out_ot_desc, 456 457 (struct usb_descriptor_header *)&std_as_out_if0_desc, 458 (struct usb_descriptor_header *)&std_as_out_if1_desc, 459 460 (struct usb_descriptor_header *)&as_out_hdr_desc, 461 (struct usb_descriptor_header *)&as_out_fmt1_desc, 462 (struct usb_descriptor_header *)&hs_epout_desc, 463 (struct usb_descriptor_header *)&as_iso_out_desc, 464 465 (struct usb_descriptor_header *)&std_as_in_if0_desc, 466 (struct usb_descriptor_header *)&std_as_in_if1_desc, 467 468 (struct usb_descriptor_header *)&as_in_hdr_desc, 469 (struct usb_descriptor_header *)&as_in_fmt1_desc, 470 (struct usb_descriptor_header *)&hs_epin_desc, 471 (struct usb_descriptor_header *)&as_iso_in_desc, 472 NULL, 473 }; 474 475 static struct usb_descriptor_header *ss_audio_desc[] = { 476 (struct usb_descriptor_header *)&iad_desc, 477 (struct usb_descriptor_header *)&std_ac_if_desc, 478 479 (struct usb_descriptor_header *)&ac_hdr_desc, 480 (struct usb_descriptor_header *)&in_clk_src_desc, 481 (struct usb_descriptor_header *)&out_clk_src_desc, 482 (struct usb_descriptor_header *)&usb_out_it_desc, 483 (struct usb_descriptor_header *)&io_in_it_desc, 484 (struct usb_descriptor_header *)&usb_in_ot_desc, 485 (struct usb_descriptor_header *)&io_out_ot_desc, 486 487 (struct usb_descriptor_header *)&std_as_out_if0_desc, 488 (struct usb_descriptor_header *)&std_as_out_if1_desc, 489 490 (struct usb_descriptor_header *)&as_out_hdr_desc, 491 (struct usb_descriptor_header *)&as_out_fmt1_desc, 492 (struct usb_descriptor_header *)&ss_epout_desc, 493 (struct usb_descriptor_header *)&ss_epout_desc_comp, 494 (struct usb_descriptor_header *)&as_iso_out_desc, 495 496 (struct usb_descriptor_header *)&std_as_in_if0_desc, 497 (struct usb_descriptor_header *)&std_as_in_if1_desc, 498 499 (struct usb_descriptor_header *)&as_in_hdr_desc, 500 (struct usb_descriptor_header *)&as_in_fmt1_desc, 501 (struct usb_descriptor_header *)&ss_epin_desc, 502 (struct usb_descriptor_header *)&ss_epin_desc_comp, 503 (struct usb_descriptor_header *)&as_iso_in_desc, 504 NULL, 505 }; 506 507 struct cntrl_cur_lay3 { 508 __le32 dCUR; 509 }; 510 511 struct cntrl_range_lay3 { 512 __le16 wNumSubRanges; 513 __le32 dMIN; 514 __le32 dMAX; 515 __le32 dRES; 516 } __packed; 517 518 static int set_ep_max_packet_size(const struct f_uac2_opts *uac2_opts, 519 struct usb_endpoint_descriptor *ep_desc, 520 enum usb_device_speed speed, bool is_playback) 521 { 522 int chmask, srate, ssize; 523 u16 max_size_bw, max_size_ep; 524 unsigned int factor; 525 526 switch (speed) { 527 case USB_SPEED_FULL: 528 max_size_ep = 1023; 529 factor = 1000; 530 break; 531 532 case USB_SPEED_HIGH: 533 case USB_SPEED_SUPER: 534 max_size_ep = 1024; 535 factor = 8000; 536 break; 537 538 default: 539 return -EINVAL; 540 } 541 542 if (is_playback) { 543 chmask = uac2_opts->p_chmask; 544 srate = uac2_opts->p_srate; 545 ssize = uac2_opts->p_ssize; 546 } else { 547 chmask = uac2_opts->c_chmask; 548 srate = uac2_opts->c_srate; 549 ssize = uac2_opts->c_ssize; 550 } 551 552 max_size_bw = num_channels(chmask) * ssize * 553 ((srate / (factor / (1 << (ep_desc->bInterval - 1)))) + 1); 554 ep_desc->wMaxPacketSize = cpu_to_le16(min_t(u16, max_size_bw, 555 max_size_ep)); 556 557 return 0; 558 } 559 560 /* Use macro to overcome line length limitation */ 561 #define USBDHDR(p) (struct usb_descriptor_header *)(p) 562 563 static void setup_headers(struct f_uac2_opts *opts, 564 struct usb_descriptor_header **headers, 565 enum usb_device_speed speed) 566 { 567 struct usb_ss_ep_comp_descriptor *epout_desc_comp = NULL; 568 struct usb_ss_ep_comp_descriptor *epin_desc_comp = NULL; 569 struct usb_endpoint_descriptor *epout_desc; 570 struct usb_endpoint_descriptor *epin_desc; 571 int i; 572 573 switch (speed) { 574 case USB_SPEED_FULL: 575 epout_desc = &fs_epout_desc; 576 epin_desc = &fs_epin_desc; 577 break; 578 case USB_SPEED_HIGH: 579 epout_desc = &hs_epout_desc; 580 epin_desc = &hs_epin_desc; 581 break; 582 default: 583 epout_desc = &ss_epout_desc; 584 epin_desc = &ss_epin_desc; 585 epout_desc_comp = &ss_epout_desc_comp; 586 epin_desc_comp = &ss_epin_desc_comp; 587 } 588 589 i = 0; 590 headers[i++] = USBDHDR(&iad_desc); 591 headers[i++] = USBDHDR(&std_ac_if_desc); 592 headers[i++] = USBDHDR(&ac_hdr_desc); 593 if (EPIN_EN(opts)) 594 headers[i++] = USBDHDR(&in_clk_src_desc); 595 if (EPOUT_EN(opts)) { 596 headers[i++] = USBDHDR(&out_clk_src_desc); 597 headers[i++] = USBDHDR(&usb_out_it_desc); 598 } 599 if (EPIN_EN(opts)) { 600 headers[i++] = USBDHDR(&io_in_it_desc); 601 headers[i++] = USBDHDR(&usb_in_ot_desc); 602 } 603 if (EPOUT_EN(opts)) { 604 headers[i++] = USBDHDR(&io_out_ot_desc); 605 headers[i++] = USBDHDR(&std_as_out_if0_desc); 606 headers[i++] = USBDHDR(&std_as_out_if1_desc); 607 headers[i++] = USBDHDR(&as_out_hdr_desc); 608 headers[i++] = USBDHDR(&as_out_fmt1_desc); 609 headers[i++] = USBDHDR(epout_desc); 610 if (epout_desc_comp) 611 headers[i++] = USBDHDR(epout_desc_comp); 612 613 headers[i++] = USBDHDR(&as_iso_out_desc); 614 } 615 if (EPIN_EN(opts)) { 616 headers[i++] = USBDHDR(&std_as_in_if0_desc); 617 headers[i++] = USBDHDR(&std_as_in_if1_desc); 618 headers[i++] = USBDHDR(&as_in_hdr_desc); 619 headers[i++] = USBDHDR(&as_in_fmt1_desc); 620 headers[i++] = USBDHDR(epin_desc); 621 if (epin_desc_comp) 622 headers[i++] = USBDHDR(epin_desc_comp); 623 624 headers[i++] = USBDHDR(&as_iso_in_desc); 625 } 626 headers[i] = NULL; 627 } 628 629 static void setup_descriptor(struct f_uac2_opts *opts) 630 { 631 /* patch descriptors */ 632 int i = 1; /* ID's start with 1 */ 633 634 if (EPOUT_EN(opts)) 635 usb_out_it_desc.bTerminalID = i++; 636 if (EPIN_EN(opts)) 637 io_in_it_desc.bTerminalID = i++; 638 if (EPOUT_EN(opts)) 639 io_out_ot_desc.bTerminalID = i++; 640 if (EPIN_EN(opts)) 641 usb_in_ot_desc.bTerminalID = i++; 642 if (EPOUT_EN(opts)) 643 out_clk_src_desc.bClockID = i++; 644 if (EPIN_EN(opts)) 645 in_clk_src_desc.bClockID = i++; 646 647 usb_out_it_desc.bCSourceID = out_clk_src_desc.bClockID; 648 usb_in_ot_desc.bSourceID = io_in_it_desc.bTerminalID; 649 usb_in_ot_desc.bCSourceID = in_clk_src_desc.bClockID; 650 io_in_it_desc.bCSourceID = in_clk_src_desc.bClockID; 651 io_out_ot_desc.bCSourceID = out_clk_src_desc.bClockID; 652 io_out_ot_desc.bSourceID = usb_out_it_desc.bTerminalID; 653 as_out_hdr_desc.bTerminalLink = usb_out_it_desc.bTerminalID; 654 as_in_hdr_desc.bTerminalLink = usb_in_ot_desc.bTerminalID; 655 656 iad_desc.bInterfaceCount = 1; 657 ac_hdr_desc.wTotalLength = cpu_to_le16(sizeof(ac_hdr_desc)); 658 659 if (EPIN_EN(opts)) { 660 u16 len = le16_to_cpu(ac_hdr_desc.wTotalLength); 661 662 len += sizeof(in_clk_src_desc); 663 len += sizeof(usb_in_ot_desc); 664 len += sizeof(io_in_it_desc); 665 ac_hdr_desc.wTotalLength = cpu_to_le16(len); 666 iad_desc.bInterfaceCount++; 667 } 668 if (EPOUT_EN(opts)) { 669 u16 len = le16_to_cpu(ac_hdr_desc.wTotalLength); 670 671 len += sizeof(out_clk_src_desc); 672 len += sizeof(usb_out_it_desc); 673 len += sizeof(io_out_ot_desc); 674 ac_hdr_desc.wTotalLength = cpu_to_le16(len); 675 iad_desc.bInterfaceCount++; 676 } 677 678 setup_headers(opts, fs_audio_desc, USB_SPEED_FULL); 679 setup_headers(opts, hs_audio_desc, USB_SPEED_HIGH); 680 setup_headers(opts, ss_audio_desc, USB_SPEED_SUPER); 681 } 682 683 static int afunc_validate_opts(struct g_audio *agdev, struct device *dev) 684 { 685 struct f_uac2_opts *opts = g_audio_to_uac2_opts(agdev); 686 687 if (!opts->p_chmask && !opts->c_chmask) { 688 dev_err(dev, "Error: no playback and capture channels\n"); 689 return -EINVAL; 690 } else if (opts->p_chmask & ~UAC2_CHANNEL_MASK) { 691 dev_err(dev, "Error: unsupported playback channels mask\n"); 692 return -EINVAL; 693 } else if (opts->c_chmask & ~UAC2_CHANNEL_MASK) { 694 dev_err(dev, "Error: unsupported capture channels mask\n"); 695 return -EINVAL; 696 } else if ((opts->p_ssize < 1) || (opts->p_ssize > 4)) { 697 dev_err(dev, "Error: incorrect playback sample size\n"); 698 return -EINVAL; 699 } else if ((opts->c_ssize < 1) || (opts->c_ssize > 4)) { 700 dev_err(dev, "Error: incorrect capture sample size\n"); 701 return -EINVAL; 702 } else if (!opts->p_srate) { 703 dev_err(dev, "Error: incorrect playback sampling rate\n"); 704 return -EINVAL; 705 } else if (!opts->c_srate) { 706 dev_err(dev, "Error: incorrect capture sampling rate\n"); 707 return -EINVAL; 708 } 709 710 return 0; 711 } 712 713 static int 714 afunc_bind(struct usb_configuration *cfg, struct usb_function *fn) 715 { 716 struct f_uac2 *uac2 = func_to_uac2(fn); 717 struct g_audio *agdev = func_to_g_audio(fn); 718 struct usb_composite_dev *cdev = cfg->cdev; 719 struct usb_gadget *gadget = cdev->gadget; 720 struct device *dev = &gadget->dev; 721 struct f_uac2_opts *uac2_opts = g_audio_to_uac2_opts(agdev); 722 struct usb_string *us; 723 int ret; 724 725 ret = afunc_validate_opts(agdev, dev); 726 if (ret) 727 return ret; 728 729 us = usb_gstrings_attach(cdev, fn_strings, ARRAY_SIZE(strings_fn)); 730 if (IS_ERR(us)) 731 return PTR_ERR(us); 732 iad_desc.iFunction = us[STR_ASSOC].id; 733 std_ac_if_desc.iInterface = us[STR_IF_CTRL].id; 734 in_clk_src_desc.iClockSource = us[STR_CLKSRC_IN].id; 735 out_clk_src_desc.iClockSource = us[STR_CLKSRC_OUT].id; 736 usb_out_it_desc.iTerminal = us[STR_USB_IT].id; 737 io_in_it_desc.iTerminal = us[STR_IO_IT].id; 738 usb_in_ot_desc.iTerminal = us[STR_USB_OT].id; 739 io_out_ot_desc.iTerminal = us[STR_IO_OT].id; 740 std_as_out_if0_desc.iInterface = us[STR_AS_OUT_ALT0].id; 741 std_as_out_if1_desc.iInterface = us[STR_AS_OUT_ALT1].id; 742 std_as_in_if0_desc.iInterface = us[STR_AS_IN_ALT0].id; 743 std_as_in_if1_desc.iInterface = us[STR_AS_IN_ALT1].id; 744 745 746 /* Initialize the configurable parameters */ 747 usb_out_it_desc.bNrChannels = num_channels(uac2_opts->c_chmask); 748 usb_out_it_desc.bmChannelConfig = cpu_to_le32(uac2_opts->c_chmask); 749 io_in_it_desc.bNrChannels = num_channels(uac2_opts->p_chmask); 750 io_in_it_desc.bmChannelConfig = cpu_to_le32(uac2_opts->p_chmask); 751 as_out_hdr_desc.bNrChannels = num_channels(uac2_opts->c_chmask); 752 as_out_hdr_desc.bmChannelConfig = cpu_to_le32(uac2_opts->c_chmask); 753 as_in_hdr_desc.bNrChannels = num_channels(uac2_opts->p_chmask); 754 as_in_hdr_desc.bmChannelConfig = cpu_to_le32(uac2_opts->p_chmask); 755 as_out_fmt1_desc.bSubslotSize = uac2_opts->c_ssize; 756 as_out_fmt1_desc.bBitResolution = uac2_opts->c_ssize * 8; 757 as_in_fmt1_desc.bSubslotSize = uac2_opts->p_ssize; 758 as_in_fmt1_desc.bBitResolution = uac2_opts->p_ssize * 8; 759 760 snprintf(clksrc_in, sizeof(clksrc_in), "%uHz", uac2_opts->p_srate); 761 snprintf(clksrc_out, sizeof(clksrc_out), "%uHz", uac2_opts->c_srate); 762 763 ret = usb_interface_id(cfg, fn); 764 if (ret < 0) { 765 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__); 766 return ret; 767 } 768 iad_desc.bFirstInterface = ret; 769 770 std_ac_if_desc.bInterfaceNumber = ret; 771 uac2->ac_intf = ret; 772 uac2->ac_alt = 0; 773 774 if (EPOUT_EN(uac2_opts)) { 775 ret = usb_interface_id(cfg, fn); 776 if (ret < 0) { 777 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__); 778 return ret; 779 } 780 std_as_out_if0_desc.bInterfaceNumber = ret; 781 std_as_out_if1_desc.bInterfaceNumber = ret; 782 uac2->as_out_intf = ret; 783 uac2->as_out_alt = 0; 784 } 785 786 if (EPIN_EN(uac2_opts)) { 787 ret = usb_interface_id(cfg, fn); 788 if (ret < 0) { 789 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__); 790 return ret; 791 } 792 std_as_in_if0_desc.bInterfaceNumber = ret; 793 std_as_in_if1_desc.bInterfaceNumber = ret; 794 uac2->as_in_intf = ret; 795 uac2->as_in_alt = 0; 796 } 797 798 /* Calculate wMaxPacketSize according to audio bandwidth */ 799 ret = set_ep_max_packet_size(uac2_opts, &fs_epin_desc, USB_SPEED_FULL, 800 true); 801 if (ret < 0) { 802 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__); 803 return ret; 804 } 805 806 ret = set_ep_max_packet_size(uac2_opts, &fs_epout_desc, USB_SPEED_FULL, 807 false); 808 if (ret < 0) { 809 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__); 810 return ret; 811 } 812 813 ret = set_ep_max_packet_size(uac2_opts, &hs_epin_desc, USB_SPEED_HIGH, 814 true); 815 if (ret < 0) { 816 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__); 817 return ret; 818 } 819 820 ret = set_ep_max_packet_size(uac2_opts, &hs_epout_desc, USB_SPEED_HIGH, 821 false); 822 if (ret < 0) { 823 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__); 824 return ret; 825 } 826 827 ret = set_ep_max_packet_size(uac2_opts, &ss_epin_desc, USB_SPEED_SUPER, 828 true); 829 if (ret < 0) { 830 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__); 831 return ret; 832 } 833 834 ret = set_ep_max_packet_size(uac2_opts, &ss_epout_desc, USB_SPEED_SUPER, 835 false); 836 if (ret < 0) { 837 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__); 838 return ret; 839 } 840 841 if (EPOUT_EN(uac2_opts)) { 842 agdev->out_ep = usb_ep_autoconfig(gadget, &fs_epout_desc); 843 if (!agdev->out_ep) { 844 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__); 845 return -ENODEV; 846 } 847 } 848 849 if (EPIN_EN(uac2_opts)) { 850 agdev->in_ep = usb_ep_autoconfig(gadget, &fs_epin_desc); 851 if (!agdev->in_ep) { 852 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__); 853 return -ENODEV; 854 } 855 } 856 857 agdev->in_ep_maxpsize = max_t(u16, 858 le16_to_cpu(fs_epin_desc.wMaxPacketSize), 859 le16_to_cpu(hs_epin_desc.wMaxPacketSize)); 860 agdev->out_ep_maxpsize = max_t(u16, 861 le16_to_cpu(fs_epout_desc.wMaxPacketSize), 862 le16_to_cpu(hs_epout_desc.wMaxPacketSize)); 863 864 agdev->in_ep_maxpsize = max_t(u16, agdev->in_ep_maxpsize, 865 le16_to_cpu(ss_epin_desc.wMaxPacketSize)); 866 agdev->out_ep_maxpsize = max_t(u16, agdev->out_ep_maxpsize, 867 le16_to_cpu(ss_epout_desc.wMaxPacketSize)); 868 869 hs_epout_desc.bEndpointAddress = fs_epout_desc.bEndpointAddress; 870 hs_epin_desc.bEndpointAddress = fs_epin_desc.bEndpointAddress; 871 ss_epout_desc.bEndpointAddress = fs_epout_desc.bEndpointAddress; 872 ss_epin_desc.bEndpointAddress = fs_epin_desc.bEndpointAddress; 873 874 setup_descriptor(uac2_opts); 875 876 ret = usb_assign_descriptors(fn, fs_audio_desc, hs_audio_desc, ss_audio_desc, 877 ss_audio_desc); 878 if (ret) 879 return ret; 880 881 agdev->gadget = gadget; 882 883 agdev->params.p_chmask = uac2_opts->p_chmask; 884 agdev->params.p_srate = uac2_opts->p_srate; 885 agdev->params.p_ssize = uac2_opts->p_ssize; 886 agdev->params.c_chmask = uac2_opts->c_chmask; 887 agdev->params.c_srate = uac2_opts->c_srate; 888 agdev->params.c_ssize = uac2_opts->c_ssize; 889 agdev->params.req_number = uac2_opts->req_number; 890 ret = g_audio_setup(agdev, "UAC2 PCM", "UAC2_Gadget"); 891 if (ret) 892 goto err_free_descs; 893 return 0; 894 895 err_free_descs: 896 usb_free_all_descriptors(fn); 897 agdev->gadget = NULL; 898 return ret; 899 } 900 901 static int 902 afunc_set_alt(struct usb_function *fn, unsigned intf, unsigned alt) 903 { 904 struct usb_composite_dev *cdev = fn->config->cdev; 905 struct f_uac2 *uac2 = func_to_uac2(fn); 906 struct usb_gadget *gadget = cdev->gadget; 907 struct device *dev = &gadget->dev; 908 int ret = 0; 909 910 /* No i/f has more than 2 alt settings */ 911 if (alt > 1) { 912 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__); 913 return -EINVAL; 914 } 915 916 if (intf == uac2->ac_intf) { 917 /* Control I/f has only 1 AltSetting - 0 */ 918 if (alt) { 919 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__); 920 return -EINVAL; 921 } 922 return 0; 923 } 924 925 if (intf == uac2->as_out_intf) { 926 uac2->as_out_alt = alt; 927 928 if (alt) 929 ret = u_audio_start_capture(&uac2->g_audio); 930 else 931 u_audio_stop_capture(&uac2->g_audio); 932 } else if (intf == uac2->as_in_intf) { 933 uac2->as_in_alt = alt; 934 935 if (alt) 936 ret = u_audio_start_playback(&uac2->g_audio); 937 else 938 u_audio_stop_playback(&uac2->g_audio); 939 } else { 940 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__); 941 return -EINVAL; 942 } 943 944 return ret; 945 } 946 947 static int 948 afunc_get_alt(struct usb_function *fn, unsigned intf) 949 { 950 struct f_uac2 *uac2 = func_to_uac2(fn); 951 struct g_audio *agdev = func_to_g_audio(fn); 952 953 if (intf == uac2->ac_intf) 954 return uac2->ac_alt; 955 else if (intf == uac2->as_out_intf) 956 return uac2->as_out_alt; 957 else if (intf == uac2->as_in_intf) 958 return uac2->as_in_alt; 959 else 960 dev_err(&agdev->gadget->dev, 961 "%s:%d Invalid Interface %d!\n", 962 __func__, __LINE__, intf); 963 964 return -EINVAL; 965 } 966 967 static void 968 afunc_disable(struct usb_function *fn) 969 { 970 struct f_uac2 *uac2 = func_to_uac2(fn); 971 972 uac2->as_in_alt = 0; 973 uac2->as_out_alt = 0; 974 u_audio_stop_capture(&uac2->g_audio); 975 u_audio_stop_playback(&uac2->g_audio); 976 } 977 978 static int 979 in_rq_cur(struct usb_function *fn, const struct usb_ctrlrequest *cr) 980 { 981 struct usb_request *req = fn->config->cdev->req; 982 struct g_audio *agdev = func_to_g_audio(fn); 983 struct f_uac2_opts *opts; 984 u16 w_length = le16_to_cpu(cr->wLength); 985 u16 w_index = le16_to_cpu(cr->wIndex); 986 u16 w_value = le16_to_cpu(cr->wValue); 987 u8 entity_id = (w_index >> 8) & 0xff; 988 u8 control_selector = w_value >> 8; 989 int value = -EOPNOTSUPP; 990 int p_srate, c_srate; 991 992 opts = g_audio_to_uac2_opts(agdev); 993 p_srate = opts->p_srate; 994 c_srate = opts->c_srate; 995 996 if (control_selector == UAC2_CS_CONTROL_SAM_FREQ) { 997 struct cntrl_cur_lay3 c; 998 memset(&c, 0, sizeof(struct cntrl_cur_lay3)); 999 1000 if (entity_id == USB_IN_CLK_ID) 1001 c.dCUR = cpu_to_le32(p_srate); 1002 else if (entity_id == USB_OUT_CLK_ID) 1003 c.dCUR = cpu_to_le32(c_srate); 1004 1005 value = min_t(unsigned, w_length, sizeof c); 1006 memcpy(req->buf, &c, value); 1007 } else if (control_selector == UAC2_CS_CONTROL_CLOCK_VALID) { 1008 *(u8 *)req->buf = 1; 1009 value = min_t(unsigned, w_length, 1); 1010 } else { 1011 dev_err(&agdev->gadget->dev, 1012 "%s:%d control_selector=%d TODO!\n", 1013 __func__, __LINE__, control_selector); 1014 } 1015 1016 return value; 1017 } 1018 1019 static int 1020 in_rq_range(struct usb_function *fn, const struct usb_ctrlrequest *cr) 1021 { 1022 struct usb_request *req = fn->config->cdev->req; 1023 struct g_audio *agdev = func_to_g_audio(fn); 1024 struct f_uac2_opts *opts; 1025 u16 w_length = le16_to_cpu(cr->wLength); 1026 u16 w_index = le16_to_cpu(cr->wIndex); 1027 u16 w_value = le16_to_cpu(cr->wValue); 1028 u8 entity_id = (w_index >> 8) & 0xff; 1029 u8 control_selector = w_value >> 8; 1030 struct cntrl_range_lay3 r; 1031 int value = -EOPNOTSUPP; 1032 int p_srate, c_srate; 1033 1034 opts = g_audio_to_uac2_opts(agdev); 1035 p_srate = opts->p_srate; 1036 c_srate = opts->c_srate; 1037 1038 if (control_selector == UAC2_CS_CONTROL_SAM_FREQ) { 1039 if (entity_id == USB_IN_CLK_ID) 1040 r.dMIN = cpu_to_le32(p_srate); 1041 else if (entity_id == USB_OUT_CLK_ID) 1042 r.dMIN = cpu_to_le32(c_srate); 1043 else 1044 return -EOPNOTSUPP; 1045 1046 r.dMAX = r.dMIN; 1047 r.dRES = 0; 1048 r.wNumSubRanges = cpu_to_le16(1); 1049 1050 value = min_t(unsigned, w_length, sizeof r); 1051 memcpy(req->buf, &r, value); 1052 } else { 1053 dev_err(&agdev->gadget->dev, 1054 "%s:%d control_selector=%d TODO!\n", 1055 __func__, __LINE__, control_selector); 1056 } 1057 1058 return value; 1059 } 1060 1061 static int 1062 ac_rq_in(struct usb_function *fn, const struct usb_ctrlrequest *cr) 1063 { 1064 if (cr->bRequest == UAC2_CS_CUR) 1065 return in_rq_cur(fn, cr); 1066 else if (cr->bRequest == UAC2_CS_RANGE) 1067 return in_rq_range(fn, cr); 1068 else 1069 return -EOPNOTSUPP; 1070 } 1071 1072 static int 1073 out_rq_cur(struct usb_function *fn, const struct usb_ctrlrequest *cr) 1074 { 1075 u16 w_length = le16_to_cpu(cr->wLength); 1076 u16 w_value = le16_to_cpu(cr->wValue); 1077 u8 control_selector = w_value >> 8; 1078 1079 if (control_selector == UAC2_CS_CONTROL_SAM_FREQ) 1080 return w_length; 1081 1082 return -EOPNOTSUPP; 1083 } 1084 1085 static int 1086 setup_rq_inf(struct usb_function *fn, const struct usb_ctrlrequest *cr) 1087 { 1088 struct f_uac2 *uac2 = func_to_uac2(fn); 1089 struct g_audio *agdev = func_to_g_audio(fn); 1090 u16 w_index = le16_to_cpu(cr->wIndex); 1091 u8 intf = w_index & 0xff; 1092 1093 if (intf != uac2->ac_intf) { 1094 dev_err(&agdev->gadget->dev, 1095 "%s:%d Error!\n", __func__, __LINE__); 1096 return -EOPNOTSUPP; 1097 } 1098 1099 if (cr->bRequestType & USB_DIR_IN) 1100 return ac_rq_in(fn, cr); 1101 else if (cr->bRequest == UAC2_CS_CUR) 1102 return out_rq_cur(fn, cr); 1103 1104 return -EOPNOTSUPP; 1105 } 1106 1107 static int 1108 afunc_setup(struct usb_function *fn, const struct usb_ctrlrequest *cr) 1109 { 1110 struct usb_composite_dev *cdev = fn->config->cdev; 1111 struct g_audio *agdev = func_to_g_audio(fn); 1112 struct usb_request *req = cdev->req; 1113 u16 w_length = le16_to_cpu(cr->wLength); 1114 int value = -EOPNOTSUPP; 1115 1116 /* Only Class specific requests are supposed to reach here */ 1117 if ((cr->bRequestType & USB_TYPE_MASK) != USB_TYPE_CLASS) 1118 return -EOPNOTSUPP; 1119 1120 if ((cr->bRequestType & USB_RECIP_MASK) == USB_RECIP_INTERFACE) 1121 value = setup_rq_inf(fn, cr); 1122 else 1123 dev_err(&agdev->gadget->dev, "%s:%d Error!\n", 1124 __func__, __LINE__); 1125 1126 if (value >= 0) { 1127 req->length = value; 1128 req->zero = value < w_length; 1129 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC); 1130 if (value < 0) { 1131 dev_err(&agdev->gadget->dev, 1132 "%s:%d Error!\n", __func__, __LINE__); 1133 req->status = 0; 1134 } 1135 } 1136 1137 return value; 1138 } 1139 1140 static inline struct f_uac2_opts *to_f_uac2_opts(struct config_item *item) 1141 { 1142 return container_of(to_config_group(item), struct f_uac2_opts, 1143 func_inst.group); 1144 } 1145 1146 static void f_uac2_attr_release(struct config_item *item) 1147 { 1148 struct f_uac2_opts *opts = to_f_uac2_opts(item); 1149 1150 usb_put_function_instance(&opts->func_inst); 1151 } 1152 1153 static struct configfs_item_operations f_uac2_item_ops = { 1154 .release = f_uac2_attr_release, 1155 }; 1156 1157 #define UAC2_ATTRIBUTE(name) \ 1158 static ssize_t f_uac2_opts_##name##_show(struct config_item *item, \ 1159 char *page) \ 1160 { \ 1161 struct f_uac2_opts *opts = to_f_uac2_opts(item); \ 1162 int result; \ 1163 \ 1164 mutex_lock(&opts->lock); \ 1165 result = sprintf(page, "%u\n", opts->name); \ 1166 mutex_unlock(&opts->lock); \ 1167 \ 1168 return result; \ 1169 } \ 1170 \ 1171 static ssize_t f_uac2_opts_##name##_store(struct config_item *item, \ 1172 const char *page, size_t len) \ 1173 { \ 1174 struct f_uac2_opts *opts = to_f_uac2_opts(item); \ 1175 int ret; \ 1176 u32 num; \ 1177 \ 1178 mutex_lock(&opts->lock); \ 1179 if (opts->refcnt) { \ 1180 ret = -EBUSY; \ 1181 goto end; \ 1182 } \ 1183 \ 1184 ret = kstrtou32(page, 0, &num); \ 1185 if (ret) \ 1186 goto end; \ 1187 \ 1188 opts->name = num; \ 1189 ret = len; \ 1190 \ 1191 end: \ 1192 mutex_unlock(&opts->lock); \ 1193 return ret; \ 1194 } \ 1195 \ 1196 CONFIGFS_ATTR(f_uac2_opts_, name) 1197 1198 UAC2_ATTRIBUTE(p_chmask); 1199 UAC2_ATTRIBUTE(p_srate); 1200 UAC2_ATTRIBUTE(p_ssize); 1201 UAC2_ATTRIBUTE(c_chmask); 1202 UAC2_ATTRIBUTE(c_srate); 1203 UAC2_ATTRIBUTE(c_ssize); 1204 UAC2_ATTRIBUTE(req_number); 1205 1206 static struct configfs_attribute *f_uac2_attrs[] = { 1207 &f_uac2_opts_attr_p_chmask, 1208 &f_uac2_opts_attr_p_srate, 1209 &f_uac2_opts_attr_p_ssize, 1210 &f_uac2_opts_attr_c_chmask, 1211 &f_uac2_opts_attr_c_srate, 1212 &f_uac2_opts_attr_c_ssize, 1213 &f_uac2_opts_attr_req_number, 1214 NULL, 1215 }; 1216 1217 static const struct config_item_type f_uac2_func_type = { 1218 .ct_item_ops = &f_uac2_item_ops, 1219 .ct_attrs = f_uac2_attrs, 1220 .ct_owner = THIS_MODULE, 1221 }; 1222 1223 static void afunc_free_inst(struct usb_function_instance *f) 1224 { 1225 struct f_uac2_opts *opts; 1226 1227 opts = container_of(f, struct f_uac2_opts, func_inst); 1228 kfree(opts); 1229 } 1230 1231 static struct usb_function_instance *afunc_alloc_inst(void) 1232 { 1233 struct f_uac2_opts *opts; 1234 1235 opts = kzalloc(sizeof(*opts), GFP_KERNEL); 1236 if (!opts) 1237 return ERR_PTR(-ENOMEM); 1238 1239 mutex_init(&opts->lock); 1240 opts->func_inst.free_func_inst = afunc_free_inst; 1241 1242 config_group_init_type_name(&opts->func_inst.group, "", 1243 &f_uac2_func_type); 1244 1245 opts->p_chmask = UAC2_DEF_PCHMASK; 1246 opts->p_srate = UAC2_DEF_PSRATE; 1247 opts->p_ssize = UAC2_DEF_PSSIZE; 1248 opts->c_chmask = UAC2_DEF_CCHMASK; 1249 opts->c_srate = UAC2_DEF_CSRATE; 1250 opts->c_ssize = UAC2_DEF_CSSIZE; 1251 opts->req_number = UAC2_DEF_REQ_NUM; 1252 return &opts->func_inst; 1253 } 1254 1255 static void afunc_free(struct usb_function *f) 1256 { 1257 struct g_audio *agdev; 1258 struct f_uac2_opts *opts; 1259 1260 agdev = func_to_g_audio(f); 1261 opts = container_of(f->fi, struct f_uac2_opts, func_inst); 1262 kfree(agdev); 1263 mutex_lock(&opts->lock); 1264 --opts->refcnt; 1265 mutex_unlock(&opts->lock); 1266 } 1267 1268 static void afunc_unbind(struct usb_configuration *c, struct usb_function *f) 1269 { 1270 struct g_audio *agdev = func_to_g_audio(f); 1271 1272 g_audio_cleanup(agdev); 1273 usb_free_all_descriptors(f); 1274 1275 agdev->gadget = NULL; 1276 } 1277 1278 static struct usb_function *afunc_alloc(struct usb_function_instance *fi) 1279 { 1280 struct f_uac2 *uac2; 1281 struct f_uac2_opts *opts; 1282 1283 uac2 = kzalloc(sizeof(*uac2), GFP_KERNEL); 1284 if (uac2 == NULL) 1285 return ERR_PTR(-ENOMEM); 1286 1287 opts = container_of(fi, struct f_uac2_opts, func_inst); 1288 mutex_lock(&opts->lock); 1289 ++opts->refcnt; 1290 mutex_unlock(&opts->lock); 1291 1292 uac2->g_audio.func.name = "uac2_func"; 1293 uac2->g_audio.func.bind = afunc_bind; 1294 uac2->g_audio.func.unbind = afunc_unbind; 1295 uac2->g_audio.func.set_alt = afunc_set_alt; 1296 uac2->g_audio.func.get_alt = afunc_get_alt; 1297 uac2->g_audio.func.disable = afunc_disable; 1298 uac2->g_audio.func.setup = afunc_setup; 1299 uac2->g_audio.func.free_func = afunc_free; 1300 1301 return &uac2->g_audio.func; 1302 } 1303 1304 DECLARE_USB_FUNCTION_INIT(uac2, afunc_alloc_inst, afunc_alloc); 1305 MODULE_LICENSE("GPL"); 1306 MODULE_AUTHOR("Yadwinder Singh"); 1307 MODULE_AUTHOR("Jaswinder Singh"); 1308