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