1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * f_audio.c -- USB Audio class function driver 4 * 5 * Copyright (C) 2008 Bryan Wu <cooloney@kernel.org> 6 * Copyright (C) 2008 Analog Devices, Inc 7 * 8 * Enter bugs at http://blackfin.uclinux.org/ 9 * 10 * Licensed under the GPL-2 or later. 11 */ 12 13 #include <linux/slab.h> 14 #include <linux/kernel.h> 15 #include <linux/module.h> 16 #include <linux/device.h> 17 #include <linux/atomic.h> 18 19 #include "u_uac1_legacy.h" 20 21 static int generic_set_cmd(struct usb_audio_control *con, u8 cmd, int value); 22 static int generic_get_cmd(struct usb_audio_control *con, u8 cmd); 23 24 /* 25 * DESCRIPTORS ... most are static, but strings and full 26 * configuration descriptors are built on demand. 27 */ 28 29 /* 30 * We have two interfaces- AudioControl and AudioStreaming 31 * TODO: only supcard playback currently 32 */ 33 #define F_AUDIO_AC_INTERFACE 0 34 #define F_AUDIO_AS_INTERFACE 1 35 #define F_AUDIO_NUM_INTERFACES 1 36 37 /* B.3.1 Standard AC Interface Descriptor */ 38 static struct usb_interface_descriptor ac_interface_desc = { 39 .bLength = USB_DT_INTERFACE_SIZE, 40 .bDescriptorType = USB_DT_INTERFACE, 41 .bNumEndpoints = 0, 42 .bInterfaceClass = USB_CLASS_AUDIO, 43 .bInterfaceSubClass = USB_SUBCLASS_AUDIOCONTROL, 44 }; 45 46 /* 47 * The number of AudioStreaming and MIDIStreaming interfaces 48 * in the Audio Interface Collection 49 */ 50 DECLARE_UAC_AC_HEADER_DESCRIPTOR(1); 51 52 #define UAC_DT_AC_HEADER_LENGTH UAC_DT_AC_HEADER_SIZE(F_AUDIO_NUM_INTERFACES) 53 /* 1 input terminal, 1 output terminal and 1 feature unit */ 54 #define UAC_DT_TOTAL_LENGTH (UAC_DT_AC_HEADER_LENGTH + UAC_DT_INPUT_TERMINAL_SIZE \ 55 + UAC_DT_OUTPUT_TERMINAL_SIZE + UAC_DT_FEATURE_UNIT_SIZE(0)) 56 /* B.3.2 Class-Specific AC Interface Descriptor */ 57 static struct uac1_ac_header_descriptor_1 ac_header_desc = { 58 .bLength = UAC_DT_AC_HEADER_LENGTH, 59 .bDescriptorType = USB_DT_CS_INTERFACE, 60 .bDescriptorSubtype = UAC_HEADER, 61 .bcdADC = __constant_cpu_to_le16(0x0100), 62 .wTotalLength = __constant_cpu_to_le16(UAC_DT_TOTAL_LENGTH), 63 .bInCollection = F_AUDIO_NUM_INTERFACES, 64 .baInterfaceNr = { 65 /* Interface number of the first AudioStream interface */ 66 [0] = 1, 67 } 68 }; 69 70 #define INPUT_TERMINAL_ID 1 71 static struct uac_input_terminal_descriptor input_terminal_desc = { 72 .bLength = UAC_DT_INPUT_TERMINAL_SIZE, 73 .bDescriptorType = USB_DT_CS_INTERFACE, 74 .bDescriptorSubtype = UAC_INPUT_TERMINAL, 75 .bTerminalID = INPUT_TERMINAL_ID, 76 .wTerminalType = UAC_TERMINAL_STREAMING, 77 .bAssocTerminal = 0, 78 .wChannelConfig = 0x3, 79 }; 80 81 DECLARE_UAC_FEATURE_UNIT_DESCRIPTOR(0); 82 83 #define FEATURE_UNIT_ID 2 84 static struct uac_feature_unit_descriptor_0 feature_unit_desc = { 85 .bLength = UAC_DT_FEATURE_UNIT_SIZE(0), 86 .bDescriptorType = USB_DT_CS_INTERFACE, 87 .bDescriptorSubtype = UAC_FEATURE_UNIT, 88 .bUnitID = FEATURE_UNIT_ID, 89 .bSourceID = INPUT_TERMINAL_ID, 90 .bControlSize = 2, 91 .bmaControls[0] = (UAC_FU_MUTE | UAC_FU_VOLUME), 92 }; 93 94 static struct usb_audio_control mute_control = { 95 .list = LIST_HEAD_INIT(mute_control.list), 96 .name = "Mute Control", 97 .type = UAC_FU_MUTE, 98 /* Todo: add real Mute control code */ 99 .set = generic_set_cmd, 100 .get = generic_get_cmd, 101 }; 102 103 static struct usb_audio_control volume_control = { 104 .list = LIST_HEAD_INIT(volume_control.list), 105 .name = "Volume Control", 106 .type = UAC_FU_VOLUME, 107 /* Todo: add real Volume control code */ 108 .set = generic_set_cmd, 109 .get = generic_get_cmd, 110 }; 111 112 static struct usb_audio_control_selector feature_unit = { 113 .list = LIST_HEAD_INIT(feature_unit.list), 114 .id = FEATURE_UNIT_ID, 115 .name = "Mute & Volume Control", 116 .type = UAC_FEATURE_UNIT, 117 .desc = (struct usb_descriptor_header *)&feature_unit_desc, 118 }; 119 120 #define OUTPUT_TERMINAL_ID 3 121 static struct uac1_output_terminal_descriptor output_terminal_desc = { 122 .bLength = UAC_DT_OUTPUT_TERMINAL_SIZE, 123 .bDescriptorType = USB_DT_CS_INTERFACE, 124 .bDescriptorSubtype = UAC_OUTPUT_TERMINAL, 125 .bTerminalID = OUTPUT_TERMINAL_ID, 126 .wTerminalType = UAC_OUTPUT_TERMINAL_SPEAKER, 127 .bAssocTerminal = FEATURE_UNIT_ID, 128 .bSourceID = FEATURE_UNIT_ID, 129 }; 130 131 /* B.4.1 Standard AS Interface Descriptor */ 132 static struct usb_interface_descriptor as_interface_alt_0_desc = { 133 .bLength = USB_DT_INTERFACE_SIZE, 134 .bDescriptorType = USB_DT_INTERFACE, 135 .bAlternateSetting = 0, 136 .bNumEndpoints = 0, 137 .bInterfaceClass = USB_CLASS_AUDIO, 138 .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING, 139 }; 140 141 static struct usb_interface_descriptor as_interface_alt_1_desc = { 142 .bLength = USB_DT_INTERFACE_SIZE, 143 .bDescriptorType = USB_DT_INTERFACE, 144 .bAlternateSetting = 1, 145 .bNumEndpoints = 1, 146 .bInterfaceClass = USB_CLASS_AUDIO, 147 .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING, 148 }; 149 150 /* B.4.2 Class-Specific AS Interface Descriptor */ 151 static struct uac1_as_header_descriptor as_header_desc = { 152 .bLength = UAC_DT_AS_HEADER_SIZE, 153 .bDescriptorType = USB_DT_CS_INTERFACE, 154 .bDescriptorSubtype = UAC_AS_GENERAL, 155 .bTerminalLink = INPUT_TERMINAL_ID, 156 .bDelay = 1, 157 .wFormatTag = UAC_FORMAT_TYPE_I_PCM, 158 }; 159 160 DECLARE_UAC_FORMAT_TYPE_I_DISCRETE_DESC(1); 161 162 static struct uac_format_type_i_discrete_descriptor_1 as_type_i_desc = { 163 .bLength = UAC_FORMAT_TYPE_I_DISCRETE_DESC_SIZE(1), 164 .bDescriptorType = USB_DT_CS_INTERFACE, 165 .bDescriptorSubtype = UAC_FORMAT_TYPE, 166 .bFormatType = UAC_FORMAT_TYPE_I, 167 .bSubframeSize = 2, 168 .bBitResolution = 16, 169 .bSamFreqType = 1, 170 }; 171 172 /* Standard ISO OUT Endpoint Descriptor */ 173 static struct usb_endpoint_descriptor as_out_ep_desc = { 174 .bLength = USB_DT_ENDPOINT_AUDIO_SIZE, 175 .bDescriptorType = USB_DT_ENDPOINT, 176 .bEndpointAddress = USB_DIR_OUT, 177 .bmAttributes = USB_ENDPOINT_SYNC_ADAPTIVE 178 | USB_ENDPOINT_XFER_ISOC, 179 .wMaxPacketSize = cpu_to_le16(UAC1_OUT_EP_MAX_PACKET_SIZE), 180 .bInterval = 4, 181 }; 182 183 /* Class-specific AS ISO OUT Endpoint Descriptor */ 184 static struct uac_iso_endpoint_descriptor as_iso_out_desc = { 185 .bLength = UAC_ISO_ENDPOINT_DESC_SIZE, 186 .bDescriptorType = USB_DT_CS_ENDPOINT, 187 .bDescriptorSubtype = UAC_EP_GENERAL, 188 .bmAttributes = 1, 189 .bLockDelayUnits = 1, 190 .wLockDelay = __constant_cpu_to_le16(1), 191 }; 192 193 static struct usb_descriptor_header *f_audio_desc[] = { 194 (struct usb_descriptor_header *)&ac_interface_desc, 195 (struct usb_descriptor_header *)&ac_header_desc, 196 197 (struct usb_descriptor_header *)&input_terminal_desc, 198 (struct usb_descriptor_header *)&output_terminal_desc, 199 (struct usb_descriptor_header *)&feature_unit_desc, 200 201 (struct usb_descriptor_header *)&as_interface_alt_0_desc, 202 (struct usb_descriptor_header *)&as_interface_alt_1_desc, 203 (struct usb_descriptor_header *)&as_header_desc, 204 205 (struct usb_descriptor_header *)&as_type_i_desc, 206 207 (struct usb_descriptor_header *)&as_out_ep_desc, 208 (struct usb_descriptor_header *)&as_iso_out_desc, 209 NULL, 210 }; 211 212 enum { 213 STR_AC_IF, 214 STR_INPUT_TERMINAL, 215 STR_INPUT_TERMINAL_CH_NAMES, 216 STR_FEAT_DESC_0, 217 STR_OUTPUT_TERMINAL, 218 STR_AS_IF_ALT0, 219 STR_AS_IF_ALT1, 220 }; 221 222 static struct usb_string strings_uac1[] = { 223 [STR_AC_IF].s = "AC Interface", 224 [STR_INPUT_TERMINAL].s = "Input terminal", 225 [STR_INPUT_TERMINAL_CH_NAMES].s = "Channels", 226 [STR_FEAT_DESC_0].s = "Volume control & mute", 227 [STR_OUTPUT_TERMINAL].s = "Output terminal", 228 [STR_AS_IF_ALT0].s = "AS Interface", 229 [STR_AS_IF_ALT1].s = "AS Interface", 230 { }, 231 }; 232 233 static struct usb_gadget_strings str_uac1 = { 234 .language = 0x0409, /* en-us */ 235 .strings = strings_uac1, 236 }; 237 238 static struct usb_gadget_strings *uac1_strings[] = { 239 &str_uac1, 240 NULL, 241 }; 242 243 /* 244 * This function is an ALSA sound card following USB Audio Class Spec 1.0. 245 */ 246 247 /*-------------------------------------------------------------------------*/ 248 struct f_audio_buf { 249 u8 *buf; 250 int actual; 251 struct list_head list; 252 }; 253 254 static struct f_audio_buf *f_audio_buffer_alloc(int buf_size) 255 { 256 struct f_audio_buf *copy_buf; 257 258 copy_buf = kzalloc(sizeof *copy_buf, GFP_ATOMIC); 259 if (!copy_buf) 260 return ERR_PTR(-ENOMEM); 261 262 copy_buf->buf = kzalloc(buf_size, GFP_ATOMIC); 263 if (!copy_buf->buf) { 264 kfree(copy_buf); 265 return ERR_PTR(-ENOMEM); 266 } 267 268 return copy_buf; 269 } 270 271 static void f_audio_buffer_free(struct f_audio_buf *audio_buf) 272 { 273 kfree(audio_buf->buf); 274 kfree(audio_buf); 275 } 276 /*-------------------------------------------------------------------------*/ 277 278 struct f_audio { 279 struct gaudio card; 280 281 u8 ac_intf, ac_alt; 282 u8 as_intf, as_alt; 283 284 /* endpoints handle full and/or high speeds */ 285 struct usb_ep *out_ep; 286 287 spinlock_t lock; 288 struct f_audio_buf *copy_buf; 289 struct work_struct playback_work; 290 struct list_head play_queue; 291 292 /* Control Set command */ 293 struct list_head cs; 294 u8 set_cmd; 295 struct usb_audio_control *set_con; 296 }; 297 298 static inline struct f_audio *func_to_audio(struct usb_function *f) 299 { 300 return container_of(f, struct f_audio, card.func); 301 } 302 303 /*-------------------------------------------------------------------------*/ 304 305 static void f_audio_playback_work(struct work_struct *data) 306 { 307 struct f_audio *audio = container_of(data, struct f_audio, 308 playback_work); 309 struct f_audio_buf *play_buf; 310 311 spin_lock_irq(&audio->lock); 312 if (list_empty(&audio->play_queue)) { 313 spin_unlock_irq(&audio->lock); 314 return; 315 } 316 play_buf = list_first_entry(&audio->play_queue, 317 struct f_audio_buf, list); 318 list_del(&play_buf->list); 319 spin_unlock_irq(&audio->lock); 320 321 u_audio_playback(&audio->card, play_buf->buf, play_buf->actual); 322 f_audio_buffer_free(play_buf); 323 } 324 325 static int f_audio_out_ep_complete(struct usb_ep *ep, struct usb_request *req) 326 { 327 struct f_audio *audio = req->context; 328 struct usb_composite_dev *cdev = audio->card.func.config->cdev; 329 struct f_audio_buf *copy_buf = audio->copy_buf; 330 struct f_uac1_legacy_opts *opts; 331 int audio_buf_size; 332 int err; 333 334 opts = container_of(audio->card.func.fi, struct f_uac1_legacy_opts, 335 func_inst); 336 audio_buf_size = opts->audio_buf_size; 337 338 if (!copy_buf) 339 return -EINVAL; 340 341 /* Copy buffer is full, add it to the play_queue */ 342 if (audio_buf_size - copy_buf->actual < req->actual) { 343 list_add_tail(©_buf->list, &audio->play_queue); 344 schedule_work(&audio->playback_work); 345 copy_buf = f_audio_buffer_alloc(audio_buf_size); 346 if (IS_ERR(copy_buf)) 347 return -ENOMEM; 348 } 349 350 memcpy(copy_buf->buf + copy_buf->actual, req->buf, req->actual); 351 copy_buf->actual += req->actual; 352 audio->copy_buf = copy_buf; 353 354 err = usb_ep_queue(ep, req, GFP_ATOMIC); 355 if (err) 356 ERROR(cdev, "%s queue req: %d\n", ep->name, err); 357 358 return 0; 359 360 } 361 362 static void f_audio_complete(struct usb_ep *ep, struct usb_request *req) 363 { 364 struct f_audio *audio = req->context; 365 int status = req->status; 366 u32 data = 0; 367 struct usb_ep *out_ep = audio->out_ep; 368 369 switch (status) { 370 371 case 0: /* normal completion? */ 372 if (ep == out_ep) 373 f_audio_out_ep_complete(ep, req); 374 else if (audio->set_con) { 375 memcpy(&data, req->buf, req->length); 376 audio->set_con->set(audio->set_con, audio->set_cmd, 377 le16_to_cpu(data)); 378 audio->set_con = NULL; 379 } 380 break; 381 default: 382 break; 383 } 384 } 385 386 static int audio_set_intf_req(struct usb_function *f, 387 const struct usb_ctrlrequest *ctrl) 388 { 389 struct f_audio *audio = func_to_audio(f); 390 struct usb_composite_dev *cdev = f->config->cdev; 391 struct usb_request *req = cdev->req; 392 u8 id = ((le16_to_cpu(ctrl->wIndex) >> 8) & 0xFF); 393 u16 len = le16_to_cpu(ctrl->wLength); 394 u16 w_value = le16_to_cpu(ctrl->wValue); 395 u8 con_sel = (w_value >> 8) & 0xFF; 396 u8 cmd = (ctrl->bRequest & 0x0F); 397 struct usb_audio_control_selector *cs; 398 struct usb_audio_control *con; 399 400 DBG(cdev, "bRequest 0x%x, w_value 0x%04x, len %d, entity %d\n", 401 ctrl->bRequest, w_value, len, id); 402 403 list_for_each_entry(cs, &audio->cs, list) { 404 if (cs->id == id) { 405 list_for_each_entry(con, &cs->control, list) { 406 if (con->type == con_sel) { 407 audio->set_con = con; 408 break; 409 } 410 } 411 break; 412 } 413 } 414 415 audio->set_cmd = cmd; 416 req->context = audio; 417 req->complete = f_audio_complete; 418 419 return len; 420 } 421 422 static int audio_get_intf_req(struct usb_function *f, 423 const struct usb_ctrlrequest *ctrl) 424 { 425 struct f_audio *audio = func_to_audio(f); 426 struct usb_composite_dev *cdev = f->config->cdev; 427 struct usb_request *req = cdev->req; 428 int value = -EOPNOTSUPP; 429 u8 id = ((le16_to_cpu(ctrl->wIndex) >> 8) & 0xFF); 430 u16 len = le16_to_cpu(ctrl->wLength); 431 u16 w_value = le16_to_cpu(ctrl->wValue); 432 u8 con_sel = (w_value >> 8) & 0xFF; 433 u8 cmd = (ctrl->bRequest & 0x0F); 434 struct usb_audio_control_selector *cs; 435 struct usb_audio_control *con; 436 437 DBG(cdev, "bRequest 0x%x, w_value 0x%04x, len %d, entity %d\n", 438 ctrl->bRequest, w_value, len, id); 439 440 list_for_each_entry(cs, &audio->cs, list) { 441 if (cs->id == id) { 442 list_for_each_entry(con, &cs->control, list) { 443 if (con->type == con_sel && con->get) { 444 value = con->get(con, cmd); 445 break; 446 } 447 } 448 break; 449 } 450 } 451 452 req->context = audio; 453 req->complete = f_audio_complete; 454 len = min_t(size_t, sizeof(value), len); 455 memcpy(req->buf, &value, len); 456 457 return len; 458 } 459 460 static int audio_set_endpoint_req(struct usb_function *f, 461 const struct usb_ctrlrequest *ctrl) 462 { 463 struct usb_composite_dev *cdev = f->config->cdev; 464 int value = -EOPNOTSUPP; 465 u16 ep = le16_to_cpu(ctrl->wIndex); 466 u16 len = le16_to_cpu(ctrl->wLength); 467 u16 w_value = le16_to_cpu(ctrl->wValue); 468 469 DBG(cdev, "bRequest 0x%x, w_value 0x%04x, len %d, endpoint %d\n", 470 ctrl->bRequest, w_value, len, ep); 471 472 switch (ctrl->bRequest) { 473 case UAC_SET_CUR: 474 value = len; 475 break; 476 477 case UAC_SET_MIN: 478 break; 479 480 case UAC_SET_MAX: 481 break; 482 483 case UAC_SET_RES: 484 break; 485 486 case UAC_SET_MEM: 487 break; 488 489 default: 490 break; 491 } 492 493 return value; 494 } 495 496 static int audio_get_endpoint_req(struct usb_function *f, 497 const struct usb_ctrlrequest *ctrl) 498 { 499 struct usb_composite_dev *cdev = f->config->cdev; 500 int value = -EOPNOTSUPP; 501 u8 ep = ((le16_to_cpu(ctrl->wIndex) >> 8) & 0xFF); 502 u16 len = le16_to_cpu(ctrl->wLength); 503 u16 w_value = le16_to_cpu(ctrl->wValue); 504 505 DBG(cdev, "bRequest 0x%x, w_value 0x%04x, len %d, endpoint %d\n", 506 ctrl->bRequest, w_value, len, ep); 507 508 switch (ctrl->bRequest) { 509 case UAC_GET_CUR: 510 case UAC_GET_MIN: 511 case UAC_GET_MAX: 512 case UAC_GET_RES: 513 value = len; 514 break; 515 case UAC_GET_MEM: 516 break; 517 default: 518 break; 519 } 520 521 return value; 522 } 523 524 static int 525 f_audio_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl) 526 { 527 struct usb_composite_dev *cdev = f->config->cdev; 528 struct usb_request *req = cdev->req; 529 int value = -EOPNOTSUPP; 530 u16 w_index = le16_to_cpu(ctrl->wIndex); 531 u16 w_value = le16_to_cpu(ctrl->wValue); 532 u16 w_length = le16_to_cpu(ctrl->wLength); 533 534 /* composite driver infrastructure handles everything; interface 535 * activation uses set_alt(). 536 */ 537 switch (ctrl->bRequestType) { 538 case USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE: 539 value = audio_set_intf_req(f, ctrl); 540 break; 541 542 case USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE: 543 value = audio_get_intf_req(f, ctrl); 544 break; 545 546 case USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_ENDPOINT: 547 value = audio_set_endpoint_req(f, ctrl); 548 break; 549 550 case USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT: 551 value = audio_get_endpoint_req(f, ctrl); 552 break; 553 554 default: 555 ERROR(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n", 556 ctrl->bRequestType, ctrl->bRequest, 557 w_value, w_index, w_length); 558 } 559 560 /* respond with data transfer or status phase? */ 561 if (value >= 0) { 562 DBG(cdev, "audio req%02x.%02x v%04x i%04x l%d\n", 563 ctrl->bRequestType, ctrl->bRequest, 564 w_value, w_index, w_length); 565 req->zero = 0; 566 req->length = value; 567 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC); 568 if (value < 0) 569 ERROR(cdev, "audio response on err %d\n", value); 570 } 571 572 /* device either stalls (value < 0) or reports success */ 573 return value; 574 } 575 576 static int f_audio_set_alt(struct usb_function *f, unsigned intf, unsigned alt) 577 { 578 struct f_audio *audio = func_to_audio(f); 579 struct usb_composite_dev *cdev = f->config->cdev; 580 struct usb_ep *out_ep = audio->out_ep; 581 struct usb_request *req; 582 struct f_uac1_legacy_opts *opts; 583 int req_buf_size, req_count, audio_buf_size; 584 int i = 0, err = 0; 585 586 DBG(cdev, "intf %d, alt %d\n", intf, alt); 587 588 opts = container_of(f->fi, struct f_uac1_legacy_opts, func_inst); 589 req_buf_size = opts->req_buf_size; 590 req_count = opts->req_count; 591 audio_buf_size = opts->audio_buf_size; 592 593 /* No i/f has more than 2 alt settings */ 594 if (alt > 1) { 595 ERROR(cdev, "%s:%d Error!\n", __func__, __LINE__); 596 return -EINVAL; 597 } 598 599 if (intf == audio->ac_intf) { 600 /* Control I/f has only 1 AltSetting - 0 */ 601 if (alt) { 602 ERROR(cdev, "%s:%d Error!\n", __func__, __LINE__); 603 return -EINVAL; 604 } 605 return 0; 606 } else if (intf == audio->as_intf) { 607 if (alt == 1) { 608 err = config_ep_by_speed(cdev->gadget, f, out_ep); 609 if (err) 610 return err; 611 612 usb_ep_enable(out_ep); 613 audio->copy_buf = f_audio_buffer_alloc(audio_buf_size); 614 if (IS_ERR(audio->copy_buf)) 615 return -ENOMEM; 616 617 /* 618 * allocate a bunch of read buffers 619 * and queue them all at once. 620 */ 621 for (i = 0; i < req_count && err == 0; i++) { 622 req = usb_ep_alloc_request(out_ep, GFP_ATOMIC); 623 if (req) { 624 req->buf = kzalloc(req_buf_size, 625 GFP_ATOMIC); 626 if (req->buf) { 627 req->length = req_buf_size; 628 req->context = audio; 629 req->complete = 630 f_audio_complete; 631 err = usb_ep_queue(out_ep, 632 req, GFP_ATOMIC); 633 if (err) 634 ERROR(cdev, 635 "%s queue req: %d\n", 636 out_ep->name, err); 637 } else 638 err = -ENOMEM; 639 } else 640 err = -ENOMEM; 641 } 642 643 } else { 644 struct f_audio_buf *copy_buf = audio->copy_buf; 645 if (copy_buf) { 646 list_add_tail(©_buf->list, 647 &audio->play_queue); 648 schedule_work(&audio->playback_work); 649 } 650 } 651 audio->as_alt = alt; 652 } 653 654 return err; 655 } 656 657 static int f_audio_get_alt(struct usb_function *f, unsigned intf) 658 { 659 struct f_audio *audio = func_to_audio(f); 660 struct usb_composite_dev *cdev = f->config->cdev; 661 662 if (intf == audio->ac_intf) 663 return audio->ac_alt; 664 else if (intf == audio->as_intf) 665 return audio->as_alt; 666 else 667 ERROR(cdev, "%s:%d Invalid Interface %d!\n", 668 __func__, __LINE__, intf); 669 670 return -EINVAL; 671 } 672 673 static void f_audio_disable(struct usb_function *f) 674 { 675 return; 676 } 677 678 /*-------------------------------------------------------------------------*/ 679 680 static void f_audio_build_desc(struct f_audio *audio) 681 { 682 struct gaudio *card = &audio->card; 683 u8 *sam_freq; 684 int rate; 685 686 /* Set channel numbers */ 687 input_terminal_desc.bNrChannels = u_audio_get_playback_channels(card); 688 as_type_i_desc.bNrChannels = u_audio_get_playback_channels(card); 689 690 /* Set sample rates */ 691 rate = u_audio_get_playback_rate(card); 692 sam_freq = as_type_i_desc.tSamFreq[0]; 693 memcpy(sam_freq, &rate, 3); 694 695 /* Todo: Set Sample bits and other parameters */ 696 697 return; 698 } 699 700 /* audio function driver setup/binding */ 701 static int 702 f_audio_bind(struct usb_configuration *c, struct usb_function *f) 703 { 704 struct usb_composite_dev *cdev = c->cdev; 705 struct f_audio *audio = func_to_audio(f); 706 struct usb_string *us; 707 int status; 708 struct usb_ep *ep = NULL; 709 struct f_uac1_legacy_opts *audio_opts; 710 711 audio_opts = container_of(f->fi, struct f_uac1_legacy_opts, func_inst); 712 audio->card.gadget = c->cdev->gadget; 713 /* set up ASLA audio devices */ 714 if (!audio_opts->bound) { 715 status = gaudio_setup(&audio->card); 716 if (status < 0) 717 return status; 718 audio_opts->bound = true; 719 } 720 us = usb_gstrings_attach(cdev, uac1_strings, ARRAY_SIZE(strings_uac1)); 721 if (IS_ERR(us)) 722 return PTR_ERR(us); 723 ac_interface_desc.iInterface = us[STR_AC_IF].id; 724 input_terminal_desc.iTerminal = us[STR_INPUT_TERMINAL].id; 725 input_terminal_desc.iChannelNames = us[STR_INPUT_TERMINAL_CH_NAMES].id; 726 feature_unit_desc.iFeature = us[STR_FEAT_DESC_0].id; 727 output_terminal_desc.iTerminal = us[STR_OUTPUT_TERMINAL].id; 728 as_interface_alt_0_desc.iInterface = us[STR_AS_IF_ALT0].id; 729 as_interface_alt_1_desc.iInterface = us[STR_AS_IF_ALT1].id; 730 731 732 f_audio_build_desc(audio); 733 734 /* allocate instance-specific interface IDs, and patch descriptors */ 735 status = usb_interface_id(c, f); 736 if (status < 0) 737 goto fail; 738 ac_interface_desc.bInterfaceNumber = status; 739 audio->ac_intf = status; 740 audio->ac_alt = 0; 741 742 status = usb_interface_id(c, f); 743 if (status < 0) 744 goto fail; 745 as_interface_alt_0_desc.bInterfaceNumber = status; 746 as_interface_alt_1_desc.bInterfaceNumber = status; 747 audio->as_intf = status; 748 audio->as_alt = 0; 749 750 status = -ENODEV; 751 752 /* allocate instance-specific endpoints */ 753 ep = usb_ep_autoconfig(cdev->gadget, &as_out_ep_desc); 754 if (!ep) 755 goto fail; 756 audio->out_ep = ep; 757 audio->out_ep->desc = &as_out_ep_desc; 758 759 status = -ENOMEM; 760 761 /* copy descriptors, and track endpoint copies */ 762 status = usb_assign_descriptors(f, f_audio_desc, f_audio_desc, NULL, 763 NULL); 764 if (status) 765 goto fail; 766 return 0; 767 768 fail: 769 gaudio_cleanup(&audio->card); 770 return status; 771 } 772 773 /*-------------------------------------------------------------------------*/ 774 775 static int generic_set_cmd(struct usb_audio_control *con, u8 cmd, int value) 776 { 777 con->data[cmd] = value; 778 779 return 0; 780 } 781 782 static int generic_get_cmd(struct usb_audio_control *con, u8 cmd) 783 { 784 return con->data[cmd]; 785 } 786 787 /* Todo: add more control selecotor dynamically */ 788 static int control_selector_init(struct f_audio *audio) 789 { 790 INIT_LIST_HEAD(&audio->cs); 791 list_add(&feature_unit.list, &audio->cs); 792 793 INIT_LIST_HEAD(&feature_unit.control); 794 list_add(&mute_control.list, &feature_unit.control); 795 list_add(&volume_control.list, &feature_unit.control); 796 797 volume_control.data[UAC__CUR] = 0xffc0; 798 volume_control.data[UAC__MIN] = 0xe3a0; 799 volume_control.data[UAC__MAX] = 0xfff0; 800 volume_control.data[UAC__RES] = 0x0030; 801 802 return 0; 803 } 804 805 static inline 806 struct f_uac1_legacy_opts *to_f_uac1_opts(struct config_item *item) 807 { 808 return container_of(to_config_group(item), struct f_uac1_legacy_opts, 809 func_inst.group); 810 } 811 812 static void f_uac1_attr_release(struct config_item *item) 813 { 814 struct f_uac1_legacy_opts *opts = to_f_uac1_opts(item); 815 816 usb_put_function_instance(&opts->func_inst); 817 } 818 819 static struct configfs_item_operations f_uac1_item_ops = { 820 .release = f_uac1_attr_release, 821 }; 822 823 #define UAC1_INT_ATTRIBUTE(name) \ 824 static ssize_t f_uac1_opts_##name##_show(struct config_item *item, \ 825 char *page) \ 826 { \ 827 struct f_uac1_legacy_opts *opts = to_f_uac1_opts(item); \ 828 int result; \ 829 \ 830 mutex_lock(&opts->lock); \ 831 result = sprintf(page, "%u\n", opts->name); \ 832 mutex_unlock(&opts->lock); \ 833 \ 834 return result; \ 835 } \ 836 \ 837 static ssize_t f_uac1_opts_##name##_store(struct config_item *item, \ 838 const char *page, size_t len) \ 839 { \ 840 struct f_uac1_legacy_opts *opts = to_f_uac1_opts(item); \ 841 int ret; \ 842 u32 num; \ 843 \ 844 mutex_lock(&opts->lock); \ 845 if (opts->refcnt) { \ 846 ret = -EBUSY; \ 847 goto end; \ 848 } \ 849 \ 850 ret = kstrtou32(page, 0, &num); \ 851 if (ret) \ 852 goto end; \ 853 \ 854 opts->name = num; \ 855 ret = len; \ 856 \ 857 end: \ 858 mutex_unlock(&opts->lock); \ 859 return ret; \ 860 } \ 861 \ 862 CONFIGFS_ATTR(f_uac1_opts_, name) 863 864 UAC1_INT_ATTRIBUTE(req_buf_size); 865 UAC1_INT_ATTRIBUTE(req_count); 866 UAC1_INT_ATTRIBUTE(audio_buf_size); 867 868 #define UAC1_STR_ATTRIBUTE(name) \ 869 static ssize_t f_uac1_opts_##name##_show(struct config_item *item, \ 870 char *page) \ 871 { \ 872 struct f_uac1_legacy_opts *opts = to_f_uac1_opts(item); \ 873 int result; \ 874 \ 875 mutex_lock(&opts->lock); \ 876 result = sprintf(page, "%s\n", opts->name); \ 877 mutex_unlock(&opts->lock); \ 878 \ 879 return result; \ 880 } \ 881 \ 882 static ssize_t f_uac1_opts_##name##_store(struct config_item *item, \ 883 const char *page, size_t len) \ 884 { \ 885 struct f_uac1_legacy_opts *opts = to_f_uac1_opts(item); \ 886 int ret = -EBUSY; \ 887 char *tmp; \ 888 \ 889 mutex_lock(&opts->lock); \ 890 if (opts->refcnt) \ 891 goto end; \ 892 \ 893 tmp = kstrndup(page, len, GFP_KERNEL); \ 894 if (tmp) { \ 895 ret = -ENOMEM; \ 896 goto end; \ 897 } \ 898 if (opts->name##_alloc) \ 899 kfree(opts->name); \ 900 opts->name##_alloc = true; \ 901 opts->name = tmp; \ 902 ret = len; \ 903 \ 904 end: \ 905 mutex_unlock(&opts->lock); \ 906 return ret; \ 907 } \ 908 \ 909 CONFIGFS_ATTR(f_uac1_opts_, name) 910 911 UAC1_STR_ATTRIBUTE(fn_play); 912 UAC1_STR_ATTRIBUTE(fn_cap); 913 UAC1_STR_ATTRIBUTE(fn_cntl); 914 915 static struct configfs_attribute *f_uac1_attrs[] = { 916 &f_uac1_opts_attr_req_buf_size, 917 &f_uac1_opts_attr_req_count, 918 &f_uac1_opts_attr_audio_buf_size, 919 &f_uac1_opts_attr_fn_play, 920 &f_uac1_opts_attr_fn_cap, 921 &f_uac1_opts_attr_fn_cntl, 922 NULL, 923 }; 924 925 static struct config_item_type f_uac1_func_type = { 926 .ct_item_ops = &f_uac1_item_ops, 927 .ct_attrs = f_uac1_attrs, 928 .ct_owner = THIS_MODULE, 929 }; 930 931 static void f_audio_free_inst(struct usb_function_instance *f) 932 { 933 struct f_uac1_legacy_opts *opts; 934 935 opts = container_of(f, struct f_uac1_legacy_opts, func_inst); 936 if (opts->fn_play_alloc) 937 kfree(opts->fn_play); 938 if (opts->fn_cap_alloc) 939 kfree(opts->fn_cap); 940 if (opts->fn_cntl_alloc) 941 kfree(opts->fn_cntl); 942 kfree(opts); 943 } 944 945 static struct usb_function_instance *f_audio_alloc_inst(void) 946 { 947 struct f_uac1_legacy_opts *opts; 948 949 opts = kzalloc(sizeof(*opts), GFP_KERNEL); 950 if (!opts) 951 return ERR_PTR(-ENOMEM); 952 953 mutex_init(&opts->lock); 954 opts->func_inst.free_func_inst = f_audio_free_inst; 955 956 config_group_init_type_name(&opts->func_inst.group, "", 957 &f_uac1_func_type); 958 959 opts->req_buf_size = UAC1_OUT_EP_MAX_PACKET_SIZE; 960 opts->req_count = UAC1_REQ_COUNT; 961 opts->audio_buf_size = UAC1_AUDIO_BUF_SIZE; 962 opts->fn_play = FILE_PCM_PLAYBACK; 963 opts->fn_cap = FILE_PCM_CAPTURE; 964 opts->fn_cntl = FILE_CONTROL; 965 return &opts->func_inst; 966 } 967 968 static void f_audio_free(struct usb_function *f) 969 { 970 struct f_audio *audio = func_to_audio(f); 971 struct f_uac1_legacy_opts *opts; 972 973 gaudio_cleanup(&audio->card); 974 opts = container_of(f->fi, struct f_uac1_legacy_opts, func_inst); 975 kfree(audio); 976 mutex_lock(&opts->lock); 977 --opts->refcnt; 978 mutex_unlock(&opts->lock); 979 } 980 981 static void f_audio_unbind(struct usb_configuration *c, struct usb_function *f) 982 { 983 usb_free_all_descriptors(f); 984 } 985 986 static struct usb_function *f_audio_alloc(struct usb_function_instance *fi) 987 { 988 struct f_audio *audio; 989 struct f_uac1_legacy_opts *opts; 990 991 /* allocate and initialize one new instance */ 992 audio = kzalloc(sizeof(*audio), GFP_KERNEL); 993 if (!audio) 994 return ERR_PTR(-ENOMEM); 995 996 audio->card.func.name = "g_audio"; 997 998 opts = container_of(fi, struct f_uac1_legacy_opts, func_inst); 999 mutex_lock(&opts->lock); 1000 ++opts->refcnt; 1001 mutex_unlock(&opts->lock); 1002 INIT_LIST_HEAD(&audio->play_queue); 1003 spin_lock_init(&audio->lock); 1004 1005 audio->card.func.bind = f_audio_bind; 1006 audio->card.func.unbind = f_audio_unbind; 1007 audio->card.func.set_alt = f_audio_set_alt; 1008 audio->card.func.get_alt = f_audio_get_alt; 1009 audio->card.func.setup = f_audio_setup; 1010 audio->card.func.disable = f_audio_disable; 1011 audio->card.func.free_func = f_audio_free; 1012 1013 control_selector_init(audio); 1014 1015 INIT_WORK(&audio->playback_work, f_audio_playback_work); 1016 1017 return &audio->card.func; 1018 } 1019 1020 DECLARE_USB_FUNCTION_INIT(uac1_legacy, f_audio_alloc_inst, f_audio_alloc); 1021 MODULE_LICENSE("GPL"); 1022 MODULE_AUTHOR("Bryan Wu"); 1023