1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * f_uac1.c -- USB Audio Class 1.0 Function (using u_audio API) 4 * 5 * Copyright (C) 2016 Ruslan Bilovol <ruslan.bilovol@gmail.com> 6 * 7 * This driver doesn't expect any real Audio codec to be present 8 * on the device - the audio streams are simply sinked to and 9 * sourced from a virtual ALSA sound card created. 10 * 11 * This file is based on f_uac1.c which is 12 * Copyright (C) 2008 Bryan Wu <cooloney@kernel.org> 13 * Copyright (C) 2008 Analog Devices, Inc 14 */ 15 16 #include <linux/usb/audio.h> 17 #include <linux/module.h> 18 19 #include "u_audio.h" 20 #include "u_uac1.h" 21 22 /* UAC1 spec: 3.7.2.3 Audio Channel Cluster Format */ 23 #define UAC1_CHANNEL_MASK 0x0FFF 24 25 #define USB_OUT_FU_ID (out_feature_unit_desc->bUnitID) 26 #define USB_IN_FU_ID (in_feature_unit_desc->bUnitID) 27 28 #define EPIN_EN(_opts) ((_opts)->p_chmask != 0) 29 #define EPOUT_EN(_opts) ((_opts)->c_chmask != 0) 30 #define FUIN_EN(_opts) ((_opts)->p_mute_present \ 31 || (_opts)->p_volume_present) 32 #define FUOUT_EN(_opts) ((_opts)->c_mute_present \ 33 || (_opts)->c_volume_present) 34 35 struct f_uac1 { 36 struct g_audio g_audio; 37 u8 ac_intf, as_in_intf, as_out_intf; 38 u8 ac_alt, as_in_alt, as_out_alt; /* needed for get_alt() */ 39 40 struct usb_ctrlrequest setup_cr; /* will be used in data stage */ 41 42 /* Interrupt IN endpoint of AC interface */ 43 struct usb_ep *int_ep; 44 atomic_t int_count; 45 }; 46 47 static inline struct f_uac1 *func_to_uac1(struct usb_function *f) 48 { 49 return container_of(f, struct f_uac1, g_audio.func); 50 } 51 52 static inline struct f_uac1_opts *g_audio_to_uac1_opts(struct g_audio *audio) 53 { 54 return container_of(audio->func.fi, struct f_uac1_opts, func_inst); 55 } 56 57 /* 58 * DESCRIPTORS ... most are static, but strings and full 59 * configuration descriptors are built on demand. 60 */ 61 62 /* 63 * We have three interfaces - one AudioControl and two AudioStreaming 64 * 65 * The driver implements a simple UAC_1 topology. 66 * USB-OUT -> IT_1 -> OT_2 -> ALSA_Capture 67 * ALSA_Playback -> IT_3 -> OT_4 -> USB-IN 68 */ 69 70 /* B.3.1 Standard AC Interface Descriptor */ 71 static struct usb_interface_descriptor ac_interface_desc = { 72 .bLength = USB_DT_INTERFACE_SIZE, 73 .bDescriptorType = USB_DT_INTERFACE, 74 /* .bNumEndpoints = DYNAMIC */ 75 .bInterfaceClass = USB_CLASS_AUDIO, 76 .bInterfaceSubClass = USB_SUBCLASS_AUDIOCONTROL, 77 }; 78 79 /* B.3.2 Class-Specific AC Interface Descriptor */ 80 static struct uac1_ac_header_descriptor *ac_header_desc; 81 82 static struct uac_input_terminal_descriptor usb_out_it_desc = { 83 .bLength = UAC_DT_INPUT_TERMINAL_SIZE, 84 .bDescriptorType = USB_DT_CS_INTERFACE, 85 .bDescriptorSubtype = UAC_INPUT_TERMINAL, 86 /* .bTerminalID = DYNAMIC */ 87 .wTerminalType = cpu_to_le16(UAC_TERMINAL_STREAMING), 88 .bAssocTerminal = 0, 89 .wChannelConfig = cpu_to_le16(0x3), 90 }; 91 92 static struct uac1_output_terminal_descriptor io_out_ot_desc = { 93 .bLength = UAC_DT_OUTPUT_TERMINAL_SIZE, 94 .bDescriptorType = USB_DT_CS_INTERFACE, 95 .bDescriptorSubtype = UAC_OUTPUT_TERMINAL, 96 /* .bTerminalID = DYNAMIC */ 97 .wTerminalType = cpu_to_le16(UAC_OUTPUT_TERMINAL_SPEAKER), 98 .bAssocTerminal = 0, 99 /* .bSourceID = DYNAMIC */ 100 }; 101 102 static struct uac_input_terminal_descriptor io_in_it_desc = { 103 .bLength = UAC_DT_INPUT_TERMINAL_SIZE, 104 .bDescriptorType = USB_DT_CS_INTERFACE, 105 .bDescriptorSubtype = UAC_INPUT_TERMINAL, 106 /* .bTerminalID = DYNAMIC */ 107 .wTerminalType = cpu_to_le16(UAC_INPUT_TERMINAL_MICROPHONE), 108 .bAssocTerminal = 0, 109 .wChannelConfig = cpu_to_le16(0x3), 110 }; 111 112 static struct uac1_output_terminal_descriptor usb_in_ot_desc = { 113 .bLength = UAC_DT_OUTPUT_TERMINAL_SIZE, 114 .bDescriptorType = USB_DT_CS_INTERFACE, 115 .bDescriptorSubtype = UAC_OUTPUT_TERMINAL, 116 /* .bTerminalID = DYNAMIC */ 117 .wTerminalType = cpu_to_le16(UAC_TERMINAL_STREAMING), 118 .bAssocTerminal = 0, 119 /* .bSourceID = DYNAMIC */ 120 }; 121 122 static struct uac_feature_unit_descriptor *in_feature_unit_desc; 123 static struct uac_feature_unit_descriptor *out_feature_unit_desc; 124 125 /* AC IN Interrupt Endpoint */ 126 static struct usb_endpoint_descriptor ac_int_ep_desc = { 127 .bLength = USB_DT_ENDPOINT_SIZE, 128 .bDescriptorType = USB_DT_ENDPOINT, 129 .bEndpointAddress = USB_DIR_IN, 130 .bmAttributes = USB_ENDPOINT_XFER_INT, 131 .wMaxPacketSize = cpu_to_le16(2), 132 .bInterval = 4, 133 }; 134 135 /* B.4.1 Standard AS Interface Descriptor */ 136 static struct usb_interface_descriptor as_out_interface_alt_0_desc = { 137 .bLength = USB_DT_INTERFACE_SIZE, 138 .bDescriptorType = USB_DT_INTERFACE, 139 .bAlternateSetting = 0, 140 .bNumEndpoints = 0, 141 .bInterfaceClass = USB_CLASS_AUDIO, 142 .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING, 143 }; 144 145 static struct usb_interface_descriptor as_out_interface_alt_1_desc = { 146 .bLength = USB_DT_INTERFACE_SIZE, 147 .bDescriptorType = USB_DT_INTERFACE, 148 .bAlternateSetting = 1, 149 .bNumEndpoints = 1, 150 .bInterfaceClass = USB_CLASS_AUDIO, 151 .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING, 152 }; 153 154 static struct usb_interface_descriptor as_in_interface_alt_0_desc = { 155 .bLength = USB_DT_INTERFACE_SIZE, 156 .bDescriptorType = USB_DT_INTERFACE, 157 .bAlternateSetting = 0, 158 .bNumEndpoints = 0, 159 .bInterfaceClass = USB_CLASS_AUDIO, 160 .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING, 161 }; 162 163 static struct usb_interface_descriptor as_in_interface_alt_1_desc = { 164 .bLength = USB_DT_INTERFACE_SIZE, 165 .bDescriptorType = USB_DT_INTERFACE, 166 .bAlternateSetting = 1, 167 .bNumEndpoints = 1, 168 .bInterfaceClass = USB_CLASS_AUDIO, 169 .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING, 170 }; 171 172 /* B.4.2 Class-Specific AS Interface Descriptor */ 173 static struct uac1_as_header_descriptor as_out_header_desc = { 174 .bLength = UAC_DT_AS_HEADER_SIZE, 175 .bDescriptorType = USB_DT_CS_INTERFACE, 176 .bDescriptorSubtype = UAC_AS_GENERAL, 177 /* .bTerminalLink = DYNAMIC */ 178 .bDelay = 1, 179 .wFormatTag = cpu_to_le16(UAC_FORMAT_TYPE_I_PCM), 180 }; 181 182 static struct uac1_as_header_descriptor as_in_header_desc = { 183 .bLength = UAC_DT_AS_HEADER_SIZE, 184 .bDescriptorType = USB_DT_CS_INTERFACE, 185 .bDescriptorSubtype = UAC_AS_GENERAL, 186 /* .bTerminalLink = DYNAMIC */ 187 .bDelay = 1, 188 .wFormatTag = cpu_to_le16(UAC_FORMAT_TYPE_I_PCM), 189 }; 190 191 DECLARE_UAC_FORMAT_TYPE_I_DISCRETE_DESC(1); 192 193 static struct uac_format_type_i_discrete_descriptor_1 as_out_type_i_desc = { 194 .bLength = UAC_FORMAT_TYPE_I_DISCRETE_DESC_SIZE(1), 195 .bDescriptorType = USB_DT_CS_INTERFACE, 196 .bDescriptorSubtype = UAC_FORMAT_TYPE, 197 .bFormatType = UAC_FORMAT_TYPE_I, 198 .bSubframeSize = 2, 199 .bBitResolution = 16, 200 .bSamFreqType = 1, 201 }; 202 203 /* Standard ISO OUT Endpoint Descriptor */ 204 static struct usb_endpoint_descriptor as_out_ep_desc = { 205 .bLength = USB_DT_ENDPOINT_AUDIO_SIZE, 206 .bDescriptorType = USB_DT_ENDPOINT, 207 .bEndpointAddress = USB_DIR_OUT, 208 .bmAttributes = USB_ENDPOINT_SYNC_ADAPTIVE 209 | USB_ENDPOINT_XFER_ISOC, 210 .wMaxPacketSize = cpu_to_le16(UAC1_OUT_EP_MAX_PACKET_SIZE), 211 .bInterval = 4, 212 }; 213 214 /* Class-specific AS ISO OUT Endpoint Descriptor */ 215 static struct uac_iso_endpoint_descriptor as_iso_out_desc = { 216 .bLength = UAC_ISO_ENDPOINT_DESC_SIZE, 217 .bDescriptorType = USB_DT_CS_ENDPOINT, 218 .bDescriptorSubtype = UAC_EP_GENERAL, 219 .bmAttributes = 1, 220 .bLockDelayUnits = 1, 221 .wLockDelay = cpu_to_le16(1), 222 }; 223 224 static struct uac_format_type_i_discrete_descriptor_1 as_in_type_i_desc = { 225 .bLength = UAC_FORMAT_TYPE_I_DISCRETE_DESC_SIZE(1), 226 .bDescriptorType = USB_DT_CS_INTERFACE, 227 .bDescriptorSubtype = UAC_FORMAT_TYPE, 228 .bFormatType = UAC_FORMAT_TYPE_I, 229 .bSubframeSize = 2, 230 .bBitResolution = 16, 231 .bSamFreqType = 1, 232 }; 233 234 /* Standard ISO OUT Endpoint Descriptor */ 235 static struct usb_endpoint_descriptor as_in_ep_desc = { 236 .bLength = USB_DT_ENDPOINT_AUDIO_SIZE, 237 .bDescriptorType = USB_DT_ENDPOINT, 238 .bEndpointAddress = USB_DIR_IN, 239 .bmAttributes = USB_ENDPOINT_SYNC_ASYNC 240 | USB_ENDPOINT_XFER_ISOC, 241 .wMaxPacketSize = cpu_to_le16(UAC1_OUT_EP_MAX_PACKET_SIZE), 242 .bInterval = 4, 243 }; 244 245 /* Class-specific AS ISO OUT Endpoint Descriptor */ 246 static struct uac_iso_endpoint_descriptor as_iso_in_desc = { 247 .bLength = UAC_ISO_ENDPOINT_DESC_SIZE, 248 .bDescriptorType = USB_DT_CS_ENDPOINT, 249 .bDescriptorSubtype = UAC_EP_GENERAL, 250 .bmAttributes = 1, 251 .bLockDelayUnits = 0, 252 .wLockDelay = 0, 253 }; 254 255 static struct usb_descriptor_header *f_audio_desc[] = { 256 (struct usb_descriptor_header *)&ac_interface_desc, 257 (struct usb_descriptor_header *)&ac_header_desc, 258 259 (struct usb_descriptor_header *)&usb_out_it_desc, 260 (struct usb_descriptor_header *)&io_out_ot_desc, 261 (struct usb_descriptor_header *)&out_feature_unit_desc, 262 263 (struct usb_descriptor_header *)&io_in_it_desc, 264 (struct usb_descriptor_header *)&usb_in_ot_desc, 265 (struct usb_descriptor_header *)&in_feature_unit_desc, 266 267 (struct usb_descriptor_header *)&ac_int_ep_desc, 268 269 (struct usb_descriptor_header *)&as_out_interface_alt_0_desc, 270 (struct usb_descriptor_header *)&as_out_interface_alt_1_desc, 271 (struct usb_descriptor_header *)&as_out_header_desc, 272 273 (struct usb_descriptor_header *)&as_out_type_i_desc, 274 275 (struct usb_descriptor_header *)&as_out_ep_desc, 276 (struct usb_descriptor_header *)&as_iso_out_desc, 277 278 (struct usb_descriptor_header *)&as_in_interface_alt_0_desc, 279 (struct usb_descriptor_header *)&as_in_interface_alt_1_desc, 280 (struct usb_descriptor_header *)&as_in_header_desc, 281 282 (struct usb_descriptor_header *)&as_in_type_i_desc, 283 284 (struct usb_descriptor_header *)&as_in_ep_desc, 285 (struct usb_descriptor_header *)&as_iso_in_desc, 286 NULL, 287 }; 288 289 enum { 290 STR_AC_IF, 291 STR_USB_OUT_IT, 292 STR_USB_OUT_IT_CH_NAMES, 293 STR_IO_OUT_OT, 294 STR_IO_IN_IT, 295 STR_IO_IN_IT_CH_NAMES, 296 STR_USB_IN_OT, 297 STR_FU_IN, 298 STR_FU_OUT, 299 STR_AS_OUT_IF_ALT0, 300 STR_AS_OUT_IF_ALT1, 301 STR_AS_IN_IF_ALT0, 302 STR_AS_IN_IF_ALT1, 303 }; 304 305 static struct usb_string strings_uac1[] = { 306 [STR_AC_IF].s = "AC Interface", 307 [STR_USB_OUT_IT].s = "Playback Input terminal", 308 [STR_USB_OUT_IT_CH_NAMES].s = "Playback Channels", 309 [STR_IO_OUT_OT].s = "Playback Output terminal", 310 [STR_IO_IN_IT].s = "Capture Input terminal", 311 [STR_IO_IN_IT_CH_NAMES].s = "Capture Channels", 312 [STR_USB_IN_OT].s = "Capture Output terminal", 313 [STR_FU_IN].s = "Capture Volume", 314 [STR_FU_OUT].s = "Playback Volume", 315 [STR_AS_OUT_IF_ALT0].s = "Playback Inactive", 316 [STR_AS_OUT_IF_ALT1].s = "Playback Active", 317 [STR_AS_IN_IF_ALT0].s = "Capture Inactive", 318 [STR_AS_IN_IF_ALT1].s = "Capture Active", 319 { }, 320 }; 321 322 static struct usb_gadget_strings str_uac1 = { 323 .language = 0x0409, /* en-us */ 324 .strings = strings_uac1, 325 }; 326 327 static struct usb_gadget_strings *uac1_strings[] = { 328 &str_uac1, 329 NULL, 330 }; 331 332 /* 333 * This function is an ALSA sound card following USB Audio Class Spec 1.0. 334 */ 335 336 static void audio_notify_complete(struct usb_ep *_ep, struct usb_request *req) 337 { 338 struct g_audio *audio = req->context; 339 struct f_uac1 *uac1 = func_to_uac1(&audio->func); 340 341 atomic_dec(&uac1->int_count); 342 kfree(req->buf); 343 usb_ep_free_request(_ep, req); 344 } 345 346 static int audio_notify(struct g_audio *audio, int unit_id, int cs) 347 { 348 struct f_uac1 *uac1 = func_to_uac1(&audio->func); 349 struct usb_request *req; 350 struct uac1_status_word *msg; 351 int ret; 352 353 if (!uac1->int_ep->enabled) 354 return 0; 355 356 if (atomic_inc_return(&uac1->int_count) > UAC1_DEF_INT_REQ_NUM) { 357 atomic_dec(&uac1->int_count); 358 return 0; 359 } 360 361 req = usb_ep_alloc_request(uac1->int_ep, GFP_ATOMIC); 362 if (req == NULL) { 363 ret = -ENOMEM; 364 goto err_dec_int_count; 365 } 366 367 msg = kmalloc(sizeof(*msg), GFP_ATOMIC); 368 if (msg == NULL) { 369 ret = -ENOMEM; 370 goto err_free_request; 371 } 372 373 msg->bStatusType = UAC1_STATUS_TYPE_IRQ_PENDING 374 | UAC1_STATUS_TYPE_ORIG_AUDIO_CONTROL_IF; 375 msg->bOriginator = unit_id; 376 377 req->length = sizeof(*msg); 378 req->buf = msg; 379 req->context = audio; 380 req->complete = audio_notify_complete; 381 382 ret = usb_ep_queue(uac1->int_ep, req, GFP_ATOMIC); 383 384 if (ret) 385 goto err_free_msg; 386 387 return 0; 388 389 err_free_msg: 390 kfree(msg); 391 err_free_request: 392 usb_ep_free_request(uac1->int_ep, req); 393 err_dec_int_count: 394 atomic_dec(&uac1->int_count); 395 396 return ret; 397 } 398 399 static int 400 in_rq_cur(struct usb_function *fn, const struct usb_ctrlrequest *cr) 401 { 402 struct usb_request *req = fn->config->cdev->req; 403 struct g_audio *audio = func_to_g_audio(fn); 404 struct f_uac1_opts *opts = g_audio_to_uac1_opts(audio); 405 u16 w_length = le16_to_cpu(cr->wLength); 406 u16 w_index = le16_to_cpu(cr->wIndex); 407 u16 w_value = le16_to_cpu(cr->wValue); 408 u8 entity_id = (w_index >> 8) & 0xff; 409 u8 control_selector = w_value >> 8; 410 int value = -EOPNOTSUPP; 411 412 if ((FUIN_EN(opts) && (entity_id == USB_IN_FU_ID)) || 413 (FUOUT_EN(opts) && (entity_id == USB_OUT_FU_ID))) { 414 unsigned int is_playback = 0; 415 416 if (FUIN_EN(opts) && (entity_id == USB_IN_FU_ID)) 417 is_playback = 1; 418 419 if (control_selector == UAC_FU_MUTE) { 420 unsigned int mute; 421 422 u_audio_get_mute(audio, is_playback, &mute); 423 424 *(u8 *)req->buf = mute; 425 value = min_t(unsigned int, w_length, 1); 426 } else if (control_selector == UAC_FU_VOLUME) { 427 __le16 c; 428 s16 volume; 429 430 u_audio_get_volume(audio, is_playback, &volume); 431 432 c = cpu_to_le16(volume); 433 434 value = min_t(unsigned int, w_length, sizeof(c)); 435 memcpy(req->buf, &c, value); 436 } else { 437 dev_err(&audio->gadget->dev, 438 "%s:%d control_selector=%d TODO!\n", 439 __func__, __LINE__, control_selector); 440 } 441 } else { 442 dev_err(&audio->gadget->dev, 443 "%s:%d entity_id=%d control_selector=%d TODO!\n", 444 __func__, __LINE__, entity_id, control_selector); 445 } 446 447 return value; 448 } 449 450 static int 451 in_rq_min(struct usb_function *fn, const struct usb_ctrlrequest *cr) 452 { 453 struct usb_request *req = fn->config->cdev->req; 454 struct g_audio *audio = func_to_g_audio(fn); 455 struct f_uac1_opts *opts = g_audio_to_uac1_opts(audio); 456 u16 w_length = le16_to_cpu(cr->wLength); 457 u16 w_index = le16_to_cpu(cr->wIndex); 458 u16 w_value = le16_to_cpu(cr->wValue); 459 u8 entity_id = (w_index >> 8) & 0xff; 460 u8 control_selector = w_value >> 8; 461 int value = -EOPNOTSUPP; 462 463 if ((FUIN_EN(opts) && (entity_id == USB_IN_FU_ID)) || 464 (FUOUT_EN(opts) && (entity_id == USB_OUT_FU_ID))) { 465 unsigned int is_playback = 0; 466 467 if (FUIN_EN(opts) && (entity_id == USB_IN_FU_ID)) 468 is_playback = 1; 469 470 if (control_selector == UAC_FU_VOLUME) { 471 __le16 r; 472 s16 min_db; 473 474 if (is_playback) 475 min_db = opts->p_volume_min; 476 else 477 min_db = opts->c_volume_min; 478 479 r = cpu_to_le16(min_db); 480 481 value = min_t(unsigned int, w_length, sizeof(r)); 482 memcpy(req->buf, &r, value); 483 } else { 484 dev_err(&audio->gadget->dev, 485 "%s:%d control_selector=%d TODO!\n", 486 __func__, __LINE__, control_selector); 487 } 488 } else { 489 dev_err(&audio->gadget->dev, 490 "%s:%d entity_id=%d control_selector=%d TODO!\n", 491 __func__, __LINE__, entity_id, control_selector); 492 } 493 494 return value; 495 } 496 497 static int 498 in_rq_max(struct usb_function *fn, const struct usb_ctrlrequest *cr) 499 { 500 struct usb_request *req = fn->config->cdev->req; 501 struct g_audio *audio = func_to_g_audio(fn); 502 struct f_uac1_opts *opts = g_audio_to_uac1_opts(audio); 503 u16 w_length = le16_to_cpu(cr->wLength); 504 u16 w_index = le16_to_cpu(cr->wIndex); 505 u16 w_value = le16_to_cpu(cr->wValue); 506 u8 entity_id = (w_index >> 8) & 0xff; 507 u8 control_selector = w_value >> 8; 508 int value = -EOPNOTSUPP; 509 510 if ((FUIN_EN(opts) && (entity_id == USB_IN_FU_ID)) || 511 (FUOUT_EN(opts) && (entity_id == USB_OUT_FU_ID))) { 512 unsigned int is_playback = 0; 513 514 if (FUIN_EN(opts) && (entity_id == USB_IN_FU_ID)) 515 is_playback = 1; 516 517 if (control_selector == UAC_FU_VOLUME) { 518 __le16 r; 519 s16 max_db; 520 521 if (is_playback) 522 max_db = opts->p_volume_max; 523 else 524 max_db = opts->c_volume_max; 525 526 r = cpu_to_le16(max_db); 527 528 value = min_t(unsigned int, w_length, sizeof(r)); 529 memcpy(req->buf, &r, value); 530 } else { 531 dev_err(&audio->gadget->dev, 532 "%s:%d control_selector=%d TODO!\n", 533 __func__, __LINE__, control_selector); 534 } 535 } else { 536 dev_err(&audio->gadget->dev, 537 "%s:%d entity_id=%d control_selector=%d TODO!\n", 538 __func__, __LINE__, entity_id, control_selector); 539 } 540 541 return value; 542 } 543 544 static int 545 in_rq_res(struct usb_function *fn, const struct usb_ctrlrequest *cr) 546 { 547 struct usb_request *req = fn->config->cdev->req; 548 struct g_audio *audio = func_to_g_audio(fn); 549 struct f_uac1_opts *opts = g_audio_to_uac1_opts(audio); 550 u16 w_length = le16_to_cpu(cr->wLength); 551 u16 w_index = le16_to_cpu(cr->wIndex); 552 u16 w_value = le16_to_cpu(cr->wValue); 553 u8 entity_id = (w_index >> 8) & 0xff; 554 u8 control_selector = w_value >> 8; 555 int value = -EOPNOTSUPP; 556 557 if ((FUIN_EN(opts) && (entity_id == USB_IN_FU_ID)) || 558 (FUOUT_EN(opts) && (entity_id == USB_OUT_FU_ID))) { 559 unsigned int is_playback = 0; 560 561 if (FUIN_EN(opts) && (entity_id == USB_IN_FU_ID)) 562 is_playback = 1; 563 564 if (control_selector == UAC_FU_VOLUME) { 565 __le16 r; 566 s16 res_db; 567 568 if (is_playback) 569 res_db = opts->p_volume_res; 570 else 571 res_db = opts->c_volume_res; 572 573 r = cpu_to_le16(res_db); 574 575 value = min_t(unsigned int, w_length, sizeof(r)); 576 memcpy(req->buf, &r, value); 577 } else { 578 dev_err(&audio->gadget->dev, 579 "%s:%d control_selector=%d TODO!\n", 580 __func__, __LINE__, control_selector); 581 } 582 } else { 583 dev_err(&audio->gadget->dev, 584 "%s:%d entity_id=%d control_selector=%d TODO!\n", 585 __func__, __LINE__, entity_id, control_selector); 586 } 587 588 return value; 589 } 590 591 static void 592 out_rq_cur_complete(struct usb_ep *ep, struct usb_request *req) 593 { 594 struct g_audio *audio = req->context; 595 struct usb_composite_dev *cdev = audio->func.config->cdev; 596 struct f_uac1_opts *opts = g_audio_to_uac1_opts(audio); 597 struct f_uac1 *uac1 = func_to_uac1(&audio->func); 598 struct usb_ctrlrequest *cr = &uac1->setup_cr; 599 u16 w_index = le16_to_cpu(cr->wIndex); 600 u16 w_value = le16_to_cpu(cr->wValue); 601 u8 entity_id = (w_index >> 8) & 0xff; 602 u8 control_selector = w_value >> 8; 603 604 if (req->status != 0) { 605 dev_dbg(&cdev->gadget->dev, "completion err %d\n", req->status); 606 return; 607 } 608 609 if ((FUIN_EN(opts) && (entity_id == USB_IN_FU_ID)) || 610 (FUOUT_EN(opts) && (entity_id == USB_OUT_FU_ID))) { 611 unsigned int is_playback = 0; 612 613 if (FUIN_EN(opts) && (entity_id == USB_IN_FU_ID)) 614 is_playback = 1; 615 616 if (control_selector == UAC_FU_MUTE) { 617 u8 mute = *(u8 *)req->buf; 618 619 u_audio_set_mute(audio, is_playback, mute); 620 621 return; 622 } else if (control_selector == UAC_FU_VOLUME) { 623 __le16 *c = req->buf; 624 s16 volume; 625 626 volume = le16_to_cpu(*c); 627 u_audio_set_volume(audio, is_playback, volume); 628 629 return; 630 } else { 631 dev_err(&audio->gadget->dev, 632 "%s:%d control_selector=%d TODO!\n", 633 __func__, __LINE__, control_selector); 634 usb_ep_set_halt(ep); 635 } 636 } else { 637 dev_err(&audio->gadget->dev, 638 "%s:%d entity_id=%d control_selector=%d TODO!\n", 639 __func__, __LINE__, entity_id, control_selector); 640 usb_ep_set_halt(ep); 641 642 } 643 } 644 645 static int 646 out_rq_cur(struct usb_function *fn, const struct usb_ctrlrequest *cr) 647 { 648 struct usb_request *req = fn->config->cdev->req; 649 struct g_audio *audio = func_to_g_audio(fn); 650 struct f_uac1_opts *opts = g_audio_to_uac1_opts(audio); 651 struct f_uac1 *uac1 = func_to_uac1(&audio->func); 652 u16 w_length = le16_to_cpu(cr->wLength); 653 u16 w_index = le16_to_cpu(cr->wIndex); 654 u16 w_value = le16_to_cpu(cr->wValue); 655 u8 entity_id = (w_index >> 8) & 0xff; 656 u8 control_selector = w_value >> 8; 657 658 if ((FUIN_EN(opts) && (entity_id == USB_IN_FU_ID)) || 659 (FUOUT_EN(opts) && (entity_id == USB_OUT_FU_ID))) { 660 memcpy(&uac1->setup_cr, cr, sizeof(*cr)); 661 req->context = audio; 662 req->complete = out_rq_cur_complete; 663 664 return w_length; 665 } else { 666 dev_err(&audio->gadget->dev, 667 "%s:%d entity_id=%d control_selector=%d TODO!\n", 668 __func__, __LINE__, entity_id, control_selector); 669 } 670 return -EOPNOTSUPP; 671 } 672 673 static int ac_rq_in(struct usb_function *f, 674 const struct usb_ctrlrequest *ctrl) 675 { 676 struct usb_composite_dev *cdev = f->config->cdev; 677 int value = -EOPNOTSUPP; 678 u8 ep = ((le16_to_cpu(ctrl->wIndex) >> 8) & 0xFF); 679 u16 len = le16_to_cpu(ctrl->wLength); 680 u16 w_value = le16_to_cpu(ctrl->wValue); 681 682 DBG(cdev, "bRequest 0x%x, w_value 0x%04x, len %d, endpoint %d\n", 683 ctrl->bRequest, w_value, len, ep); 684 685 switch (ctrl->bRequest) { 686 case UAC_GET_CUR: 687 return in_rq_cur(f, ctrl); 688 case UAC_GET_MIN: 689 return in_rq_min(f, ctrl); 690 case UAC_GET_MAX: 691 return in_rq_max(f, ctrl); 692 case UAC_GET_RES: 693 return in_rq_res(f, ctrl); 694 case UAC_GET_MEM: 695 break; 696 case UAC_GET_STAT: 697 value = len; 698 break; 699 default: 700 break; 701 } 702 703 return value; 704 } 705 706 static int audio_set_endpoint_req(struct usb_function *f, 707 const struct usb_ctrlrequest *ctrl) 708 { 709 struct usb_composite_dev *cdev = f->config->cdev; 710 int value = -EOPNOTSUPP; 711 u16 ep = le16_to_cpu(ctrl->wIndex); 712 u16 len = le16_to_cpu(ctrl->wLength); 713 u16 w_value = le16_to_cpu(ctrl->wValue); 714 715 DBG(cdev, "bRequest 0x%x, w_value 0x%04x, len %d, endpoint %d\n", 716 ctrl->bRequest, w_value, len, ep); 717 718 switch (ctrl->bRequest) { 719 case UAC_SET_CUR: 720 value = len; 721 break; 722 723 case UAC_SET_MIN: 724 break; 725 726 case UAC_SET_MAX: 727 break; 728 729 case UAC_SET_RES: 730 break; 731 732 case UAC_SET_MEM: 733 break; 734 735 default: 736 break; 737 } 738 739 return value; 740 } 741 742 static int audio_get_endpoint_req(struct usb_function *f, 743 const struct usb_ctrlrequest *ctrl) 744 { 745 struct usb_composite_dev *cdev = f->config->cdev; 746 int value = -EOPNOTSUPP; 747 u8 ep = ((le16_to_cpu(ctrl->wIndex) >> 8) & 0xFF); 748 u16 len = le16_to_cpu(ctrl->wLength); 749 u16 w_value = le16_to_cpu(ctrl->wValue); 750 751 DBG(cdev, "bRequest 0x%x, w_value 0x%04x, len %d, endpoint %d\n", 752 ctrl->bRequest, w_value, len, ep); 753 754 switch (ctrl->bRequest) { 755 case UAC_GET_CUR: 756 case UAC_GET_MIN: 757 case UAC_GET_MAX: 758 case UAC_GET_RES: 759 value = len; 760 break; 761 case UAC_GET_MEM: 762 break; 763 default: 764 break; 765 } 766 767 return value; 768 } 769 770 static int 771 f_audio_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl) 772 { 773 struct usb_composite_dev *cdev = f->config->cdev; 774 struct usb_request *req = cdev->req; 775 int value = -EOPNOTSUPP; 776 u16 w_index = le16_to_cpu(ctrl->wIndex); 777 u16 w_value = le16_to_cpu(ctrl->wValue); 778 u16 w_length = le16_to_cpu(ctrl->wLength); 779 780 /* composite driver infrastructure handles everything; interface 781 * activation uses set_alt(). 782 */ 783 switch (ctrl->bRequestType) { 784 case USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_ENDPOINT: 785 value = audio_set_endpoint_req(f, ctrl); 786 break; 787 788 case USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT: 789 value = audio_get_endpoint_req(f, ctrl); 790 break; 791 case USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE: 792 if (ctrl->bRequest == UAC_SET_CUR) 793 value = out_rq_cur(f, ctrl); 794 break; 795 case USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE: 796 value = ac_rq_in(f, ctrl); 797 break; 798 default: 799 ERROR(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n", 800 ctrl->bRequestType, ctrl->bRequest, 801 w_value, w_index, w_length); 802 } 803 804 /* respond with data transfer or status phase? */ 805 if (value >= 0) { 806 DBG(cdev, "audio req%02x.%02x v%04x i%04x l%d\n", 807 ctrl->bRequestType, ctrl->bRequest, 808 w_value, w_index, w_length); 809 req->zero = 0; 810 req->length = value; 811 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC); 812 if (value < 0) 813 ERROR(cdev, "audio response on err %d\n", value); 814 } 815 816 /* device either stalls (value < 0) or reports success */ 817 return value; 818 } 819 820 static int f_audio_set_alt(struct usb_function *f, unsigned intf, unsigned alt) 821 { 822 struct usb_composite_dev *cdev = f->config->cdev; 823 struct usb_gadget *gadget = cdev->gadget; 824 struct device *dev = &gadget->dev; 825 struct g_audio *audio = func_to_g_audio(f); 826 struct f_uac1 *uac1 = func_to_uac1(f); 827 int ret = 0; 828 829 /* No i/f has more than 2 alt settings */ 830 if (alt > 1) { 831 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__); 832 return -EINVAL; 833 } 834 835 if (intf == uac1->ac_intf) { 836 /* Control I/f has only 1 AltSetting - 0 */ 837 if (alt) { 838 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__); 839 return -EINVAL; 840 } 841 842 /* restart interrupt endpoint */ 843 if (uac1->int_ep) { 844 usb_ep_disable(uac1->int_ep); 845 config_ep_by_speed(gadget, &audio->func, uac1->int_ep); 846 usb_ep_enable(uac1->int_ep); 847 } 848 849 return 0; 850 } 851 852 if (intf == uac1->as_out_intf) { 853 uac1->as_out_alt = alt; 854 855 if (alt) 856 ret = u_audio_start_capture(&uac1->g_audio); 857 else 858 u_audio_stop_capture(&uac1->g_audio); 859 } else if (intf == uac1->as_in_intf) { 860 uac1->as_in_alt = alt; 861 862 if (alt) 863 ret = u_audio_start_playback(&uac1->g_audio); 864 else 865 u_audio_stop_playback(&uac1->g_audio); 866 } else { 867 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__); 868 return -EINVAL; 869 } 870 871 return ret; 872 } 873 874 static int f_audio_get_alt(struct usb_function *f, unsigned intf) 875 { 876 struct usb_composite_dev *cdev = f->config->cdev; 877 struct usb_gadget *gadget = cdev->gadget; 878 struct device *dev = &gadget->dev; 879 struct f_uac1 *uac1 = func_to_uac1(f); 880 881 if (intf == uac1->ac_intf) 882 return uac1->ac_alt; 883 else if (intf == uac1->as_out_intf) 884 return uac1->as_out_alt; 885 else if (intf == uac1->as_in_intf) 886 return uac1->as_in_alt; 887 else 888 dev_err(dev, "%s:%d Invalid Interface %d!\n", 889 __func__, __LINE__, intf); 890 891 return -EINVAL; 892 } 893 894 895 static void f_audio_disable(struct usb_function *f) 896 { 897 struct f_uac1 *uac1 = func_to_uac1(f); 898 899 uac1->as_out_alt = 0; 900 uac1->as_in_alt = 0; 901 902 u_audio_stop_playback(&uac1->g_audio); 903 u_audio_stop_capture(&uac1->g_audio); 904 if (uac1->int_ep) 905 usb_ep_disable(uac1->int_ep); 906 } 907 908 /*-------------------------------------------------------------------------*/ 909 static struct uac_feature_unit_descriptor *build_fu_desc(int chmask) 910 { 911 struct uac_feature_unit_descriptor *fu_desc; 912 int channels = num_channels(chmask); 913 int fu_desc_size = UAC_DT_FEATURE_UNIT_SIZE(channels); 914 915 fu_desc = kzalloc(fu_desc_size, GFP_KERNEL); 916 if (!fu_desc) 917 return NULL; 918 919 fu_desc->bLength = fu_desc_size; 920 fu_desc->bDescriptorType = USB_DT_CS_INTERFACE; 921 922 fu_desc->bDescriptorSubtype = UAC_FEATURE_UNIT; 923 fu_desc->bControlSize = 2; 924 925 /* bUnitID, bSourceID and bmaControls will be defined later */ 926 927 return fu_desc; 928 } 929 930 /* B.3.2 Class-Specific AC Interface Descriptor */ 931 static struct 932 uac1_ac_header_descriptor *build_ac_header_desc(struct f_uac1_opts *opts) 933 { 934 struct uac1_ac_header_descriptor *ac_desc; 935 int ac_header_desc_size; 936 int num_ifaces = 0; 937 938 if (EPOUT_EN(opts)) 939 num_ifaces++; 940 if (EPIN_EN(opts)) 941 num_ifaces++; 942 943 ac_header_desc_size = UAC_DT_AC_HEADER_SIZE(num_ifaces); 944 945 ac_desc = kzalloc(ac_header_desc_size, GFP_KERNEL); 946 if (!ac_desc) 947 return NULL; 948 949 ac_desc->bLength = ac_header_desc_size; 950 ac_desc->bDescriptorType = USB_DT_CS_INTERFACE; 951 ac_desc->bDescriptorSubtype = UAC_HEADER; 952 ac_desc->bcdADC = cpu_to_le16(0x0100); 953 ac_desc->bInCollection = num_ifaces; 954 955 /* wTotalLength and baInterfaceNr will be defined later */ 956 957 return ac_desc; 958 } 959 960 /* Use macro to overcome line length limitation */ 961 #define USBDHDR(p) (struct usb_descriptor_header *)(p) 962 963 static void setup_descriptor(struct f_uac1_opts *opts) 964 { 965 /* patch descriptors */ 966 int i = 1; /* ID's start with 1 */ 967 968 if (EPOUT_EN(opts)) 969 usb_out_it_desc.bTerminalID = i++; 970 if (EPIN_EN(opts)) 971 io_in_it_desc.bTerminalID = i++; 972 if (EPOUT_EN(opts)) 973 io_out_ot_desc.bTerminalID = i++; 974 if (EPIN_EN(opts)) 975 usb_in_ot_desc.bTerminalID = i++; 976 if (FUOUT_EN(opts)) 977 out_feature_unit_desc->bUnitID = i++; 978 if (FUIN_EN(opts)) 979 in_feature_unit_desc->bUnitID = i++; 980 981 if (FUIN_EN(opts)) { 982 usb_in_ot_desc.bSourceID = in_feature_unit_desc->bUnitID; 983 in_feature_unit_desc->bSourceID = io_in_it_desc.bTerminalID; 984 } else { 985 usb_in_ot_desc.bSourceID = io_in_it_desc.bTerminalID; 986 } 987 if (FUOUT_EN(opts)) { 988 io_out_ot_desc.bSourceID = out_feature_unit_desc->bUnitID; 989 out_feature_unit_desc->bSourceID = usb_out_it_desc.bTerminalID; 990 } else { 991 io_out_ot_desc.bSourceID = usb_out_it_desc.bTerminalID; 992 } 993 994 as_out_header_desc.bTerminalLink = usb_out_it_desc.bTerminalID; 995 as_in_header_desc.bTerminalLink = usb_in_ot_desc.bTerminalID; 996 997 ac_header_desc->wTotalLength = cpu_to_le16(ac_header_desc->bLength); 998 999 if (EPIN_EN(opts)) { 1000 u16 len = le16_to_cpu(ac_header_desc->wTotalLength); 1001 1002 len += sizeof(usb_in_ot_desc); 1003 len += sizeof(io_in_it_desc); 1004 if (FUIN_EN(opts)) 1005 len += in_feature_unit_desc->bLength; 1006 ac_header_desc->wTotalLength = cpu_to_le16(len); 1007 } 1008 if (EPOUT_EN(opts)) { 1009 u16 len = le16_to_cpu(ac_header_desc->wTotalLength); 1010 1011 len += sizeof(usb_out_it_desc); 1012 len += sizeof(io_out_ot_desc); 1013 if (FUOUT_EN(opts)) 1014 len += out_feature_unit_desc->bLength; 1015 ac_header_desc->wTotalLength = cpu_to_le16(len); 1016 } 1017 1018 i = 0; 1019 f_audio_desc[i++] = USBDHDR(&ac_interface_desc); 1020 f_audio_desc[i++] = USBDHDR(ac_header_desc); 1021 1022 if (EPOUT_EN(opts)) { 1023 f_audio_desc[i++] = USBDHDR(&usb_out_it_desc); 1024 f_audio_desc[i++] = USBDHDR(&io_out_ot_desc); 1025 if (FUOUT_EN(opts)) 1026 f_audio_desc[i++] = USBDHDR(out_feature_unit_desc); 1027 } 1028 1029 if (EPIN_EN(opts)) { 1030 f_audio_desc[i++] = USBDHDR(&io_in_it_desc); 1031 f_audio_desc[i++] = USBDHDR(&usb_in_ot_desc); 1032 if (FUIN_EN(opts)) 1033 f_audio_desc[i++] = USBDHDR(in_feature_unit_desc); 1034 } 1035 1036 if (FUOUT_EN(opts) || FUIN_EN(opts)) 1037 f_audio_desc[i++] = USBDHDR(&ac_int_ep_desc); 1038 1039 if (EPOUT_EN(opts)) { 1040 f_audio_desc[i++] = USBDHDR(&as_out_interface_alt_0_desc); 1041 f_audio_desc[i++] = USBDHDR(&as_out_interface_alt_1_desc); 1042 f_audio_desc[i++] = USBDHDR(&as_out_header_desc); 1043 f_audio_desc[i++] = USBDHDR(&as_out_type_i_desc); 1044 f_audio_desc[i++] = USBDHDR(&as_out_ep_desc); 1045 f_audio_desc[i++] = USBDHDR(&as_iso_out_desc); 1046 } 1047 if (EPIN_EN(opts)) { 1048 f_audio_desc[i++] = USBDHDR(&as_in_interface_alt_0_desc); 1049 f_audio_desc[i++] = USBDHDR(&as_in_interface_alt_1_desc); 1050 f_audio_desc[i++] = USBDHDR(&as_in_header_desc); 1051 f_audio_desc[i++] = USBDHDR(&as_in_type_i_desc); 1052 f_audio_desc[i++] = USBDHDR(&as_in_ep_desc); 1053 f_audio_desc[i++] = USBDHDR(&as_iso_in_desc); 1054 } 1055 f_audio_desc[i] = NULL; 1056 } 1057 1058 static int f_audio_validate_opts(struct g_audio *audio, struct device *dev) 1059 { 1060 struct f_uac1_opts *opts = g_audio_to_uac1_opts(audio); 1061 1062 if (!opts->p_chmask && !opts->c_chmask) { 1063 dev_err(dev, "Error: no playback and capture channels\n"); 1064 return -EINVAL; 1065 } else if (opts->p_chmask & ~UAC1_CHANNEL_MASK) { 1066 dev_err(dev, "Error: unsupported playback channels mask\n"); 1067 return -EINVAL; 1068 } else if (opts->c_chmask & ~UAC1_CHANNEL_MASK) { 1069 dev_err(dev, "Error: unsupported capture channels mask\n"); 1070 return -EINVAL; 1071 } else if ((opts->p_ssize < 1) || (opts->p_ssize > 4)) { 1072 dev_err(dev, "Error: incorrect playback sample size\n"); 1073 return -EINVAL; 1074 } else if ((opts->c_ssize < 1) || (opts->c_ssize > 4)) { 1075 dev_err(dev, "Error: incorrect capture sample size\n"); 1076 return -EINVAL; 1077 } else if (!opts->p_srate) { 1078 dev_err(dev, "Error: incorrect playback sampling rate\n"); 1079 return -EINVAL; 1080 } else if (!opts->c_srate) { 1081 dev_err(dev, "Error: incorrect capture sampling rate\n"); 1082 return -EINVAL; 1083 } 1084 1085 if (opts->p_volume_max <= opts->p_volume_min) { 1086 dev_err(dev, "Error: incorrect playback volume max/min\n"); 1087 return -EINVAL; 1088 } else if (opts->c_volume_max <= opts->c_volume_min) { 1089 dev_err(dev, "Error: incorrect capture volume max/min\n"); 1090 return -EINVAL; 1091 } else if (opts->p_volume_res <= 0) { 1092 dev_err(dev, "Error: negative/zero playback volume resolution\n"); 1093 return -EINVAL; 1094 } else if (opts->c_volume_res <= 0) { 1095 dev_err(dev, "Error: negative/zero capture volume resolution\n"); 1096 return -EINVAL; 1097 } 1098 1099 if ((opts->p_volume_max - opts->p_volume_min) % opts->p_volume_res) { 1100 dev_err(dev, "Error: incorrect playback volume resolution\n"); 1101 return -EINVAL; 1102 } else if ((opts->c_volume_max - opts->c_volume_min) % opts->c_volume_res) { 1103 dev_err(dev, "Error: incorrect capture volume resolution\n"); 1104 return -EINVAL; 1105 } 1106 1107 return 0; 1108 } 1109 1110 /* audio function driver setup/binding */ 1111 static int f_audio_bind(struct usb_configuration *c, struct usb_function *f) 1112 { 1113 struct usb_composite_dev *cdev = c->cdev; 1114 struct usb_gadget *gadget = cdev->gadget; 1115 struct device *dev = &gadget->dev; 1116 struct f_uac1 *uac1 = func_to_uac1(f); 1117 struct g_audio *audio = func_to_g_audio(f); 1118 struct f_uac1_opts *audio_opts; 1119 struct usb_ep *ep = NULL; 1120 struct usb_string *us; 1121 u8 *sam_freq; 1122 int rate; 1123 int ba_iface_id; 1124 int status; 1125 1126 status = f_audio_validate_opts(audio, dev); 1127 if (status) 1128 return status; 1129 1130 audio_opts = container_of(f->fi, struct f_uac1_opts, func_inst); 1131 1132 us = usb_gstrings_attach(cdev, uac1_strings, ARRAY_SIZE(strings_uac1)); 1133 if (IS_ERR(us)) 1134 return PTR_ERR(us); 1135 1136 ac_header_desc = build_ac_header_desc(audio_opts); 1137 if (!ac_header_desc) 1138 return -ENOMEM; 1139 1140 if (FUOUT_EN(audio_opts)) { 1141 out_feature_unit_desc = build_fu_desc(audio_opts->c_chmask); 1142 if (!out_feature_unit_desc) { 1143 status = -ENOMEM; 1144 goto fail; 1145 } 1146 } 1147 if (FUIN_EN(audio_opts)) { 1148 in_feature_unit_desc = build_fu_desc(audio_opts->p_chmask); 1149 if (!in_feature_unit_desc) { 1150 status = -ENOMEM; 1151 goto err_free_fu; 1152 } 1153 } 1154 1155 ac_interface_desc.iInterface = us[STR_AC_IF].id; 1156 usb_out_it_desc.iTerminal = us[STR_USB_OUT_IT].id; 1157 usb_out_it_desc.iChannelNames = us[STR_USB_OUT_IT_CH_NAMES].id; 1158 io_out_ot_desc.iTerminal = us[STR_IO_OUT_OT].id; 1159 as_out_interface_alt_0_desc.iInterface = us[STR_AS_OUT_IF_ALT0].id; 1160 as_out_interface_alt_1_desc.iInterface = us[STR_AS_OUT_IF_ALT1].id; 1161 io_in_it_desc.iTerminal = us[STR_IO_IN_IT].id; 1162 io_in_it_desc.iChannelNames = us[STR_IO_IN_IT_CH_NAMES].id; 1163 usb_in_ot_desc.iTerminal = us[STR_USB_IN_OT].id; 1164 as_in_interface_alt_0_desc.iInterface = us[STR_AS_IN_IF_ALT0].id; 1165 as_in_interface_alt_1_desc.iInterface = us[STR_AS_IN_IF_ALT1].id; 1166 1167 if (FUOUT_EN(audio_opts)) { 1168 u8 *i_feature; 1169 1170 i_feature = (u8 *)out_feature_unit_desc + 1171 out_feature_unit_desc->bLength - 1; 1172 *i_feature = us[STR_FU_OUT].id; 1173 } 1174 if (FUIN_EN(audio_opts)) { 1175 u8 *i_feature; 1176 1177 i_feature = (u8 *)in_feature_unit_desc + 1178 in_feature_unit_desc->bLength - 1; 1179 *i_feature = us[STR_FU_IN].id; 1180 } 1181 1182 /* Set channel numbers */ 1183 usb_out_it_desc.bNrChannels = num_channels(audio_opts->c_chmask); 1184 usb_out_it_desc.wChannelConfig = cpu_to_le16(audio_opts->c_chmask); 1185 as_out_type_i_desc.bNrChannels = num_channels(audio_opts->c_chmask); 1186 as_out_type_i_desc.bSubframeSize = audio_opts->c_ssize; 1187 as_out_type_i_desc.bBitResolution = audio_opts->c_ssize * 8; 1188 io_in_it_desc.bNrChannels = num_channels(audio_opts->p_chmask); 1189 io_in_it_desc.wChannelConfig = cpu_to_le16(audio_opts->p_chmask); 1190 as_in_type_i_desc.bNrChannels = num_channels(audio_opts->p_chmask); 1191 as_in_type_i_desc.bSubframeSize = audio_opts->p_ssize; 1192 as_in_type_i_desc.bBitResolution = audio_opts->p_ssize * 8; 1193 1194 if (FUOUT_EN(audio_opts)) { 1195 __le16 *bma = (__le16 *)&out_feature_unit_desc->bmaControls[0]; 1196 u32 control = 0; 1197 1198 if (audio_opts->c_mute_present) 1199 control |= UAC_FU_MUTE; 1200 if (audio_opts->c_volume_present) 1201 control |= UAC_FU_VOLUME; 1202 *bma = cpu_to_le16(control); 1203 } 1204 if (FUIN_EN(audio_opts)) { 1205 __le16 *bma = (__le16 *)&in_feature_unit_desc->bmaControls[0]; 1206 u32 control = 0; 1207 1208 if (audio_opts->p_mute_present) 1209 control |= UAC_FU_MUTE; 1210 if (audio_opts->p_volume_present) 1211 control |= UAC_FU_VOLUME; 1212 *bma = cpu_to_le16(control); 1213 } 1214 1215 /* Set sample rates */ 1216 rate = audio_opts->c_srate; 1217 sam_freq = as_out_type_i_desc.tSamFreq[0]; 1218 memcpy(sam_freq, &rate, 3); 1219 rate = audio_opts->p_srate; 1220 sam_freq = as_in_type_i_desc.tSamFreq[0]; 1221 memcpy(sam_freq, &rate, 3); 1222 1223 /* allocate instance-specific interface IDs, and patch descriptors */ 1224 status = usb_interface_id(c, f); 1225 if (status < 0) 1226 goto err_free_fu; 1227 ac_interface_desc.bInterfaceNumber = status; 1228 uac1->ac_intf = status; 1229 uac1->ac_alt = 0; 1230 1231 ba_iface_id = 0; 1232 1233 if (EPOUT_EN(audio_opts)) { 1234 status = usb_interface_id(c, f); 1235 if (status < 0) 1236 goto err_free_fu; 1237 as_out_interface_alt_0_desc.bInterfaceNumber = status; 1238 as_out_interface_alt_1_desc.bInterfaceNumber = status; 1239 ac_header_desc->baInterfaceNr[ba_iface_id++] = status; 1240 uac1->as_out_intf = status; 1241 uac1->as_out_alt = 0; 1242 } 1243 1244 if (EPIN_EN(audio_opts)) { 1245 status = usb_interface_id(c, f); 1246 if (status < 0) 1247 goto err_free_fu; 1248 as_in_interface_alt_0_desc.bInterfaceNumber = status; 1249 as_in_interface_alt_1_desc.bInterfaceNumber = status; 1250 ac_header_desc->baInterfaceNr[ba_iface_id++] = status; 1251 uac1->as_in_intf = status; 1252 uac1->as_in_alt = 0; 1253 } 1254 1255 audio->gadget = gadget; 1256 1257 status = -ENODEV; 1258 1259 ac_interface_desc.bNumEndpoints = 0; 1260 1261 /* allocate AC interrupt endpoint */ 1262 if (FUOUT_EN(audio_opts) || FUIN_EN(audio_opts)) { 1263 ep = usb_ep_autoconfig(cdev->gadget, &ac_int_ep_desc); 1264 if (!ep) 1265 goto err_free_fu; 1266 uac1->int_ep = ep; 1267 uac1->int_ep->desc = &ac_int_ep_desc; 1268 1269 ac_interface_desc.bNumEndpoints = 1; 1270 } 1271 1272 /* allocate instance-specific endpoints */ 1273 if (EPOUT_EN(audio_opts)) { 1274 ep = usb_ep_autoconfig(cdev->gadget, &as_out_ep_desc); 1275 if (!ep) 1276 goto err_free_fu; 1277 audio->out_ep = ep; 1278 audio->out_ep->desc = &as_out_ep_desc; 1279 } 1280 1281 if (EPIN_EN(audio_opts)) { 1282 ep = usb_ep_autoconfig(cdev->gadget, &as_in_ep_desc); 1283 if (!ep) 1284 goto err_free_fu; 1285 audio->in_ep = ep; 1286 audio->in_ep->desc = &as_in_ep_desc; 1287 } 1288 1289 setup_descriptor(audio_opts); 1290 1291 /* copy descriptors, and track endpoint copies */ 1292 status = usb_assign_descriptors(f, f_audio_desc, f_audio_desc, NULL, 1293 NULL); 1294 if (status) 1295 goto err_free_fu; 1296 1297 audio->out_ep_maxpsize = le16_to_cpu(as_out_ep_desc.wMaxPacketSize); 1298 audio->in_ep_maxpsize = le16_to_cpu(as_in_ep_desc.wMaxPacketSize); 1299 audio->params.c_chmask = audio_opts->c_chmask; 1300 audio->params.c_srate = audio_opts->c_srate; 1301 audio->params.c_ssize = audio_opts->c_ssize; 1302 if (FUIN_EN(audio_opts)) { 1303 audio->params.p_fu.id = USB_IN_FU_ID; 1304 audio->params.p_fu.mute_present = audio_opts->p_mute_present; 1305 audio->params.p_fu.volume_present = 1306 audio_opts->p_volume_present; 1307 audio->params.p_fu.volume_min = audio_opts->p_volume_min; 1308 audio->params.p_fu.volume_max = audio_opts->p_volume_max; 1309 audio->params.p_fu.volume_res = audio_opts->p_volume_res; 1310 } 1311 audio->params.p_chmask = audio_opts->p_chmask; 1312 audio->params.p_srate = audio_opts->p_srate; 1313 audio->params.p_ssize = audio_opts->p_ssize; 1314 if (FUOUT_EN(audio_opts)) { 1315 audio->params.c_fu.id = USB_OUT_FU_ID; 1316 audio->params.c_fu.mute_present = audio_opts->c_mute_present; 1317 audio->params.c_fu.volume_present = 1318 audio_opts->c_volume_present; 1319 audio->params.c_fu.volume_min = audio_opts->c_volume_min; 1320 audio->params.c_fu.volume_max = audio_opts->c_volume_max; 1321 audio->params.c_fu.volume_res = audio_opts->c_volume_res; 1322 } 1323 audio->params.req_number = audio_opts->req_number; 1324 audio->params.fb_max = FBACK_FAST_MAX; 1325 if (FUOUT_EN(audio_opts) || FUIN_EN(audio_opts)) 1326 audio->notify = audio_notify; 1327 1328 status = g_audio_setup(audio, "UAC1_PCM", "UAC1_Gadget"); 1329 if (status) 1330 goto err_card_register; 1331 1332 return 0; 1333 1334 err_card_register: 1335 usb_free_all_descriptors(f); 1336 err_free_fu: 1337 kfree(out_feature_unit_desc); 1338 out_feature_unit_desc = NULL; 1339 kfree(in_feature_unit_desc); 1340 in_feature_unit_desc = NULL; 1341 fail: 1342 kfree(ac_header_desc); 1343 ac_header_desc = NULL; 1344 return status; 1345 } 1346 1347 /*-------------------------------------------------------------------------*/ 1348 1349 static inline struct f_uac1_opts *to_f_uac1_opts(struct config_item *item) 1350 { 1351 return container_of(to_config_group(item), struct f_uac1_opts, 1352 func_inst.group); 1353 } 1354 1355 static void f_uac1_attr_release(struct config_item *item) 1356 { 1357 struct f_uac1_opts *opts = to_f_uac1_opts(item); 1358 1359 usb_put_function_instance(&opts->func_inst); 1360 } 1361 1362 static struct configfs_item_operations f_uac1_item_ops = { 1363 .release = f_uac1_attr_release, 1364 }; 1365 1366 #define uac1_kstrtou32 kstrtou32 1367 #define uac1_kstrtos16 kstrtos16 1368 #define uac1_kstrtobool(s, base, res) kstrtobool((s), (res)) 1369 1370 static const char *u32_fmt = "%u\n"; 1371 static const char *s16_fmt = "%hd\n"; 1372 static const char *bool_fmt = "%u\n"; 1373 1374 #define UAC1_ATTRIBUTE(type, name) \ 1375 static ssize_t f_uac1_opts_##name##_show( \ 1376 struct config_item *item, \ 1377 char *page) \ 1378 { \ 1379 struct f_uac1_opts *opts = to_f_uac1_opts(item); \ 1380 int result; \ 1381 \ 1382 mutex_lock(&opts->lock); \ 1383 result = sprintf(page, type##_fmt, opts->name); \ 1384 mutex_unlock(&opts->lock); \ 1385 \ 1386 return result; \ 1387 } \ 1388 \ 1389 static ssize_t f_uac1_opts_##name##_store( \ 1390 struct config_item *item, \ 1391 const char *page, size_t len) \ 1392 { \ 1393 struct f_uac1_opts *opts = to_f_uac1_opts(item); \ 1394 int ret; \ 1395 type num; \ 1396 \ 1397 mutex_lock(&opts->lock); \ 1398 if (opts->refcnt) { \ 1399 ret = -EBUSY; \ 1400 goto end; \ 1401 } \ 1402 \ 1403 ret = uac1_kstrto##type(page, 0, &num); \ 1404 if (ret) \ 1405 goto end; \ 1406 \ 1407 opts->name = num; \ 1408 ret = len; \ 1409 \ 1410 end: \ 1411 mutex_unlock(&opts->lock); \ 1412 return ret; \ 1413 } \ 1414 \ 1415 CONFIGFS_ATTR(f_uac1_opts_, name) 1416 1417 UAC1_ATTRIBUTE(u32, c_chmask); 1418 UAC1_ATTRIBUTE(u32, c_srate); 1419 UAC1_ATTRIBUTE(u32, c_ssize); 1420 UAC1_ATTRIBUTE(u32, p_chmask); 1421 UAC1_ATTRIBUTE(u32, p_srate); 1422 UAC1_ATTRIBUTE(u32, p_ssize); 1423 UAC1_ATTRIBUTE(u32, req_number); 1424 1425 UAC1_ATTRIBUTE(bool, p_mute_present); 1426 UAC1_ATTRIBUTE(bool, p_volume_present); 1427 UAC1_ATTRIBUTE(s16, p_volume_min); 1428 UAC1_ATTRIBUTE(s16, p_volume_max); 1429 UAC1_ATTRIBUTE(s16, p_volume_res); 1430 1431 UAC1_ATTRIBUTE(bool, c_mute_present); 1432 UAC1_ATTRIBUTE(bool, c_volume_present); 1433 UAC1_ATTRIBUTE(s16, c_volume_min); 1434 UAC1_ATTRIBUTE(s16, c_volume_max); 1435 UAC1_ATTRIBUTE(s16, c_volume_res); 1436 1437 static struct configfs_attribute *f_uac1_attrs[] = { 1438 &f_uac1_opts_attr_c_chmask, 1439 &f_uac1_opts_attr_c_srate, 1440 &f_uac1_opts_attr_c_ssize, 1441 &f_uac1_opts_attr_p_chmask, 1442 &f_uac1_opts_attr_p_srate, 1443 &f_uac1_opts_attr_p_ssize, 1444 &f_uac1_opts_attr_req_number, 1445 1446 &f_uac1_opts_attr_p_mute_present, 1447 &f_uac1_opts_attr_p_volume_present, 1448 &f_uac1_opts_attr_p_volume_min, 1449 &f_uac1_opts_attr_p_volume_max, 1450 &f_uac1_opts_attr_p_volume_res, 1451 1452 &f_uac1_opts_attr_c_mute_present, 1453 &f_uac1_opts_attr_c_volume_present, 1454 &f_uac1_opts_attr_c_volume_min, 1455 &f_uac1_opts_attr_c_volume_max, 1456 &f_uac1_opts_attr_c_volume_res, 1457 1458 NULL, 1459 }; 1460 1461 static const struct config_item_type f_uac1_func_type = { 1462 .ct_item_ops = &f_uac1_item_ops, 1463 .ct_attrs = f_uac1_attrs, 1464 .ct_owner = THIS_MODULE, 1465 }; 1466 1467 static void f_audio_free_inst(struct usb_function_instance *f) 1468 { 1469 struct f_uac1_opts *opts; 1470 1471 opts = container_of(f, struct f_uac1_opts, func_inst); 1472 kfree(opts); 1473 } 1474 1475 static struct usb_function_instance *f_audio_alloc_inst(void) 1476 { 1477 struct f_uac1_opts *opts; 1478 1479 opts = kzalloc(sizeof(*opts), GFP_KERNEL); 1480 if (!opts) 1481 return ERR_PTR(-ENOMEM); 1482 1483 mutex_init(&opts->lock); 1484 opts->func_inst.free_func_inst = f_audio_free_inst; 1485 1486 config_group_init_type_name(&opts->func_inst.group, "", 1487 &f_uac1_func_type); 1488 1489 opts->c_chmask = UAC1_DEF_CCHMASK; 1490 opts->c_srate = UAC1_DEF_CSRATE; 1491 opts->c_ssize = UAC1_DEF_CSSIZE; 1492 opts->p_chmask = UAC1_DEF_PCHMASK; 1493 opts->p_srate = UAC1_DEF_PSRATE; 1494 opts->p_ssize = UAC1_DEF_PSSIZE; 1495 1496 opts->p_mute_present = UAC1_DEF_MUTE_PRESENT; 1497 opts->p_volume_present = UAC1_DEF_VOLUME_PRESENT; 1498 opts->p_volume_min = UAC1_DEF_MIN_DB; 1499 opts->p_volume_max = UAC1_DEF_MAX_DB; 1500 opts->p_volume_res = UAC1_DEF_RES_DB; 1501 1502 opts->c_mute_present = UAC1_DEF_MUTE_PRESENT; 1503 opts->c_volume_present = UAC1_DEF_VOLUME_PRESENT; 1504 opts->c_volume_min = UAC1_DEF_MIN_DB; 1505 opts->c_volume_max = UAC1_DEF_MAX_DB; 1506 opts->c_volume_res = UAC1_DEF_RES_DB; 1507 1508 opts->req_number = UAC1_DEF_REQ_NUM; 1509 return &opts->func_inst; 1510 } 1511 1512 static void f_audio_free(struct usb_function *f) 1513 { 1514 struct g_audio *audio; 1515 struct f_uac1_opts *opts; 1516 1517 audio = func_to_g_audio(f); 1518 opts = container_of(f->fi, struct f_uac1_opts, func_inst); 1519 kfree(audio); 1520 mutex_lock(&opts->lock); 1521 --opts->refcnt; 1522 mutex_unlock(&opts->lock); 1523 } 1524 1525 static void f_audio_unbind(struct usb_configuration *c, struct usb_function *f) 1526 { 1527 struct g_audio *audio = func_to_g_audio(f); 1528 1529 g_audio_cleanup(audio); 1530 usb_free_all_descriptors(f); 1531 1532 kfree(out_feature_unit_desc); 1533 out_feature_unit_desc = NULL; 1534 kfree(in_feature_unit_desc); 1535 in_feature_unit_desc = NULL; 1536 1537 kfree(ac_header_desc); 1538 ac_header_desc = NULL; 1539 1540 audio->gadget = NULL; 1541 } 1542 1543 static struct usb_function *f_audio_alloc(struct usb_function_instance *fi) 1544 { 1545 struct f_uac1 *uac1; 1546 struct f_uac1_opts *opts; 1547 1548 /* allocate and initialize one new instance */ 1549 uac1 = kzalloc(sizeof(*uac1), GFP_KERNEL); 1550 if (!uac1) 1551 return ERR_PTR(-ENOMEM); 1552 1553 opts = container_of(fi, struct f_uac1_opts, func_inst); 1554 mutex_lock(&opts->lock); 1555 ++opts->refcnt; 1556 mutex_unlock(&opts->lock); 1557 1558 uac1->g_audio.func.name = "uac1_func"; 1559 uac1->g_audio.func.bind = f_audio_bind; 1560 uac1->g_audio.func.unbind = f_audio_unbind; 1561 uac1->g_audio.func.set_alt = f_audio_set_alt; 1562 uac1->g_audio.func.get_alt = f_audio_get_alt; 1563 uac1->g_audio.func.setup = f_audio_setup; 1564 uac1->g_audio.func.disable = f_audio_disable; 1565 uac1->g_audio.func.free_func = f_audio_free; 1566 1567 return &uac1->g_audio.func; 1568 } 1569 1570 DECLARE_USB_FUNCTION_INIT(uac1, f_audio_alloc_inst, f_audio_alloc); 1571 MODULE_LICENSE("GPL"); 1572 MODULE_AUTHOR("Ruslan Bilovol"); 1573