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