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