12 #include <linux/slab.h>
13 #include <linux/kernel.h>
14 #include <linux/device.h>
19 #define OUT_EP_MAX_PACKET_SIZE 200
28 static int audio_buf_size = 48000;
44 #define F_AUDIO_AC_INTERFACE 0
45 #define F_AUDIO_AS_INTERFACE 1
46 #define F_AUDIO_NUM_INTERFACES 2
59 #define UAC_DT_AC_HEADER_LENGTH UAC_DT_AC_HEADER_SIZE(F_AUDIO_NUM_INTERFACES)
61 #define UAC_DT_TOTAL_LENGTH (UAC_DT_AC_HEADER_LENGTH + UAC_DT_INPUT_TERMINAL_SIZE \
62 + UAC_DT_OUTPUT_TERMINAL_SIZE + UAC_DT_FEATURE_UNIT_SIZE(0))
64 static struct uac1_ac_header_descriptor_2 ac_header_desc = {
77 #define INPUT_TERMINAL_ID 1
85 .wChannelConfig = 0x3,
90 #define FEATURE_UNIT_ID 2
91 static struct uac_feature_unit_descriptor_0 feature_unit_desc = {
103 .name =
"Mute Control",
106 .set = generic_set_cmd,
107 .get = generic_get_cmd,
112 .name =
"Volume Control",
115 .set = generic_set_cmd,
116 .get = generic_get_cmd,
122 .name =
"Mute & Volume Control",
127 #define OUTPUT_TERMINAL_ID 3
142 .bAlternateSetting = 0,
151 .bAlternateSetting = 1,
169 static struct uac_format_type_i_discrete_descriptor_1 as_type_i_desc = {
175 .bBitResolution = 16,
196 .bLockDelayUnits = 1,
234 copy_buf = kzalloc(
sizeof *copy_buf,
GFP_ATOMIC);
239 if (!copy_buf->
buf) {
247 static void f_audio_buffer_free(
struct f_audio_buf *audio_buf)
284 spin_lock_irq(&audio->
lock);
286 spin_unlock_irq(&audio->
lock);
292 spin_unlock_irq(&audio->
lock);
294 u_audio_playback(&audio->
card, play_buf->
buf, play_buf->
actual);
295 f_audio_buffer_free(play_buf);
312 copy_buf = f_audio_buffer_alloc(audio_buf_size);
313 if (IS_ERR(copy_buf))
323 ERROR(cdev,
"%s queue req: %d\n", ep->
name, err);
340 f_audio_out_ep_complete(ep, req);
356 struct f_audio *audio = func_to_audio(f);
362 u8 con_sel = (w_value >> 8) & 0xFF;
367 DBG(cdev,
"bRequest 0x%x, w_value 0x%04x, len %d, entity %d\n",
373 if (con->
type == con_sel) {
392 struct f_audio *audio = func_to_audio(f);
399 u8 con_sel = (w_value >> 8) & 0xFF;
404 DBG(cdev,
"bRequest 0x%x, w_value 0x%04x, len %d, entity %d\n",
410 if (con->
type == con_sel && con->
get) {
411 value = con->
get(con, cmd);
426 static int audio_set_endpoint_req(
struct usb_function *f,
435 DBG(cdev,
"bRequest 0x%x, w_value 0x%04x, len %d, endpoint %d\n",
462 static int audio_get_endpoint_req(
struct usb_function *f,
471 DBG(cdev,
"bRequest 0x%x, w_value 0x%04x, len %d, endpoint %d\n",
505 value = audio_set_intf_req(f, ctrl);
509 value = audio_get_intf_req(f, ctrl);
513 value = audio_set_endpoint_req(f, ctrl);
517 value = audio_get_endpoint_req(f, ctrl);
521 ERROR(cdev,
"invalid control req%02x.%02x v%04x i%04x l%d\n",
523 w_value, w_index, w_length);
528 DBG(cdev,
"audio req%02x.%02x v%04x i%04x l%d\n",
530 w_value, w_index, w_length);
535 ERROR(cdev,
"audio response on err %d\n", value);
542 static int f_audio_set_alt(
struct usb_function *f,
unsigned intf,
unsigned alt)
544 struct f_audio *audio = func_to_audio(f);
550 DBG(cdev,
"intf %d, alt %d\n", intf, alt);
554 usb_ep_enable(out_ep);
556 audio->
copy_buf = f_audio_buffer_alloc(audio_buf_size);
564 for (i = 0; i <
req_count && err == 0; i++) {
565 req = usb_ep_alloc_request(out_ep,
GFP_ATOMIC);
567 req->
buf = kzalloc(req_buf_size,
570 req->
length = req_buf_size;
574 err = usb_ep_queue(out_ep,
578 "%s queue req: %d\n",
606 static void f_audio_build_desc(
struct f_audio *audio)
613 input_terminal_desc.
bNrChannels = u_audio_get_playback_channels(card);
614 as_type_i_desc.bNrChannels = u_audio_get_playback_channels(card);
617 rate = u_audio_get_playback_rate(card);
618 sam_freq = as_type_i_desc.tSamFreq[0];
619 memcpy(sam_freq, &rate, 3);
631 struct f_audio *audio = func_to_audio(f);
635 f_audio_build_desc(audio);
641 ac_interface_desc.bInterfaceNumber =
status;
656 audio->
out_ep->desc = &as_out_ep_desc;
669 if (gadget_is_dualspeed(c->
cdev->gadget)) {
684 struct f_audio *audio = func_to_audio(f);
708 INIT_LIST_HEAD(&audio->
cs);
709 list_add(&feature_unit.
list, &audio->
cs);
711 INIT_LIST_HEAD(&feature_unit.
control);
712 list_add(&mute_control.
list, &feature_unit.
control);
713 list_add(&volume_control.
list, &feature_unit.
control);
740 audio->
card.func.name =
"g_audio";
741 audio->
card.gadget = c->
cdev->gadget;
751 audio->
card.func.strings = audio_strings;
752 audio->
card.func.bind = f_audio_bind;
753 audio->
card.func.unbind = f_audio_unbind;
754 audio->
card.func.set_alt = f_audio_set_alt;
755 audio->
card.func.setup = f_audio_setup;
756 audio->
card.func.disable = f_audio_disable;
766 INFO(c->
cdev,
"audio_buf_size %d, req_buf_size %d, req_count %d\n",
767 audio_buf_size, req_buf_size,
req_count);