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