1 // SPDX-License-Identifier: GPL-2.0+
3 * f_audio.c -- USB Audio class function driver
5 * Copyright (C) 2008 Bryan Wu <cooloney@kernel.org>
6 * Copyright (C) 2008 Analog Devices, Inc
9 #include <linux/slab.h>
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/device.h>
13 #include <linux/atomic.h>
15 #include "u_uac1_legacy.h"
17 static int generic_set_cmd(struct usb_audio_control
*con
, u8 cmd
, int value
);
18 static int generic_get_cmd(struct usb_audio_control
*con
, u8 cmd
);
21 * DESCRIPTORS ... most are static, but strings and full
22 * configuration descriptors are built on demand.
26 * We have two interfaces- AudioControl and AudioStreaming
27 * TODO: only supcard playback currently
29 #define F_AUDIO_AC_INTERFACE 0
30 #define F_AUDIO_AS_INTERFACE 1
31 #define F_AUDIO_NUM_INTERFACES 1
33 /* B.3.1 Standard AC Interface Descriptor */
34 static struct usb_interface_descriptor ac_interface_desc
= {
35 .bLength
= USB_DT_INTERFACE_SIZE
,
36 .bDescriptorType
= USB_DT_INTERFACE
,
38 .bInterfaceClass
= USB_CLASS_AUDIO
,
39 .bInterfaceSubClass
= USB_SUBCLASS_AUDIOCONTROL
,
43 * The number of AudioStreaming and MIDIStreaming interfaces
44 * in the Audio Interface Collection
46 DECLARE_UAC_AC_HEADER_DESCRIPTOR(1);
48 #define UAC_DT_AC_HEADER_LENGTH UAC_DT_AC_HEADER_SIZE(F_AUDIO_NUM_INTERFACES)
49 /* 1 input terminal, 1 output terminal and 1 feature unit */
50 #define UAC_DT_TOTAL_LENGTH (UAC_DT_AC_HEADER_LENGTH + UAC_DT_INPUT_TERMINAL_SIZE \
51 + UAC_DT_OUTPUT_TERMINAL_SIZE + UAC_DT_FEATURE_UNIT_SIZE(0))
52 /* B.3.2 Class-Specific AC Interface Descriptor */
53 static struct uac1_ac_header_descriptor_1 ac_header_desc
= {
54 .bLength
= UAC_DT_AC_HEADER_LENGTH
,
55 .bDescriptorType
= USB_DT_CS_INTERFACE
,
56 .bDescriptorSubtype
= UAC_HEADER
,
57 .bcdADC
= cpu_to_le16(0x0100),
58 .wTotalLength
= cpu_to_le16(UAC_DT_TOTAL_LENGTH
),
59 .bInCollection
= F_AUDIO_NUM_INTERFACES
,
61 /* Interface number of the first AudioStream interface */
66 #define INPUT_TERMINAL_ID 1
67 static struct uac_input_terminal_descriptor input_terminal_desc
= {
68 .bLength
= UAC_DT_INPUT_TERMINAL_SIZE
,
69 .bDescriptorType
= USB_DT_CS_INTERFACE
,
70 .bDescriptorSubtype
= UAC_INPUT_TERMINAL
,
71 .bTerminalID
= INPUT_TERMINAL_ID
,
72 .wTerminalType
= UAC_TERMINAL_STREAMING
,
74 .wChannelConfig
= 0x3,
77 DECLARE_UAC_FEATURE_UNIT_DESCRIPTOR(0);
79 #define FEATURE_UNIT_ID 2
80 static struct uac_feature_unit_descriptor_0 feature_unit_desc
= {
81 .bLength
= UAC_DT_FEATURE_UNIT_SIZE(0),
82 .bDescriptorType
= USB_DT_CS_INTERFACE
,
83 .bDescriptorSubtype
= UAC_FEATURE_UNIT
,
84 .bUnitID
= FEATURE_UNIT_ID
,
85 .bSourceID
= INPUT_TERMINAL_ID
,
87 .bmaControls
[0] = (UAC_FU_MUTE
| UAC_FU_VOLUME
),
90 static struct usb_audio_control mute_control
= {
91 .list
= LIST_HEAD_INIT(mute_control
.list
),
92 .name
= "Mute Control",
94 /* Todo: add real Mute control code */
95 .set
= generic_set_cmd
,
96 .get
= generic_get_cmd
,
99 static struct usb_audio_control volume_control
= {
100 .list
= LIST_HEAD_INIT(volume_control
.list
),
101 .name
= "Volume Control",
102 .type
= UAC_FU_VOLUME
,
103 /* Todo: add real Volume control code */
104 .set
= generic_set_cmd
,
105 .get
= generic_get_cmd
,
108 static struct usb_audio_control_selector feature_unit
= {
109 .list
= LIST_HEAD_INIT(feature_unit
.list
),
110 .id
= FEATURE_UNIT_ID
,
111 .name
= "Mute & Volume Control",
112 .type
= UAC_FEATURE_UNIT
,
113 .desc
= (struct usb_descriptor_header
*)&feature_unit_desc
,
116 #define OUTPUT_TERMINAL_ID 3
117 static struct uac1_output_terminal_descriptor output_terminal_desc
= {
118 .bLength
= UAC_DT_OUTPUT_TERMINAL_SIZE
,
119 .bDescriptorType
= USB_DT_CS_INTERFACE
,
120 .bDescriptorSubtype
= UAC_OUTPUT_TERMINAL
,
121 .bTerminalID
= OUTPUT_TERMINAL_ID
,
122 .wTerminalType
= UAC_OUTPUT_TERMINAL_SPEAKER
,
123 .bAssocTerminal
= FEATURE_UNIT_ID
,
124 .bSourceID
= FEATURE_UNIT_ID
,
127 /* B.4.1 Standard AS Interface Descriptor */
128 static struct usb_interface_descriptor as_interface_alt_0_desc
= {
129 .bLength
= USB_DT_INTERFACE_SIZE
,
130 .bDescriptorType
= USB_DT_INTERFACE
,
131 .bAlternateSetting
= 0,
133 .bInterfaceClass
= USB_CLASS_AUDIO
,
134 .bInterfaceSubClass
= USB_SUBCLASS_AUDIOSTREAMING
,
137 static struct usb_interface_descriptor as_interface_alt_1_desc
= {
138 .bLength
= USB_DT_INTERFACE_SIZE
,
139 .bDescriptorType
= USB_DT_INTERFACE
,
140 .bAlternateSetting
= 1,
142 .bInterfaceClass
= USB_CLASS_AUDIO
,
143 .bInterfaceSubClass
= USB_SUBCLASS_AUDIOSTREAMING
,
146 /* B.4.2 Class-Specific AS Interface Descriptor */
147 static struct uac1_as_header_descriptor as_header_desc
= {
148 .bLength
= UAC_DT_AS_HEADER_SIZE
,
149 .bDescriptorType
= USB_DT_CS_INTERFACE
,
150 .bDescriptorSubtype
= UAC_AS_GENERAL
,
151 .bTerminalLink
= INPUT_TERMINAL_ID
,
153 .wFormatTag
= UAC_FORMAT_TYPE_I_PCM
,
156 DECLARE_UAC_FORMAT_TYPE_I_DISCRETE_DESC(1);
158 static struct uac_format_type_i_discrete_descriptor_1 as_type_i_desc
= {
159 .bLength
= UAC_FORMAT_TYPE_I_DISCRETE_DESC_SIZE(1),
160 .bDescriptorType
= USB_DT_CS_INTERFACE
,
161 .bDescriptorSubtype
= UAC_FORMAT_TYPE
,
162 .bFormatType
= UAC_FORMAT_TYPE_I
,
164 .bBitResolution
= 16,
168 /* Standard ISO OUT Endpoint Descriptor */
169 static struct usb_endpoint_descriptor as_out_ep_desc
= {
170 .bLength
= USB_DT_ENDPOINT_AUDIO_SIZE
,
171 .bDescriptorType
= USB_DT_ENDPOINT
,
172 .bEndpointAddress
= USB_DIR_OUT
,
173 .bmAttributes
= USB_ENDPOINT_SYNC_ADAPTIVE
174 | USB_ENDPOINT_XFER_ISOC
,
175 .wMaxPacketSize
= cpu_to_le16(UAC1_OUT_EP_MAX_PACKET_SIZE
),
179 /* Class-specific AS ISO OUT Endpoint Descriptor */
180 static struct uac_iso_endpoint_descriptor as_iso_out_desc
= {
181 .bLength
= UAC_ISO_ENDPOINT_DESC_SIZE
,
182 .bDescriptorType
= USB_DT_CS_ENDPOINT
,
183 .bDescriptorSubtype
= UAC_EP_GENERAL
,
185 .bLockDelayUnits
= 1,
186 .wLockDelay
= cpu_to_le16(1),
189 static struct usb_descriptor_header
*f_audio_desc
[] = {
190 (struct usb_descriptor_header
*)&ac_interface_desc
,
191 (struct usb_descriptor_header
*)&ac_header_desc
,
193 (struct usb_descriptor_header
*)&input_terminal_desc
,
194 (struct usb_descriptor_header
*)&output_terminal_desc
,
195 (struct usb_descriptor_header
*)&feature_unit_desc
,
197 (struct usb_descriptor_header
*)&as_interface_alt_0_desc
,
198 (struct usb_descriptor_header
*)&as_interface_alt_1_desc
,
199 (struct usb_descriptor_header
*)&as_header_desc
,
201 (struct usb_descriptor_header
*)&as_type_i_desc
,
203 (struct usb_descriptor_header
*)&as_out_ep_desc
,
204 (struct usb_descriptor_header
*)&as_iso_out_desc
,
211 STR_INPUT_TERMINAL_CH_NAMES
,
218 static struct usb_string strings_uac1
[] = {
219 [STR_AC_IF
].s
= "AC Interface",
220 [STR_INPUT_TERMINAL
].s
= "Input terminal",
221 [STR_INPUT_TERMINAL_CH_NAMES
].s
= "Channels",
222 [STR_FEAT_DESC_0
].s
= "Volume control & mute",
223 [STR_OUTPUT_TERMINAL
].s
= "Output terminal",
224 [STR_AS_IF_ALT0
].s
= "AS Interface",
225 [STR_AS_IF_ALT1
].s
= "AS Interface",
229 static struct usb_gadget_strings str_uac1
= {
230 .language
= 0x0409, /* en-us */
231 .strings
= strings_uac1
,
234 static struct usb_gadget_strings
*uac1_strings
[] = {
240 * This function is an ALSA sound card following USB Audio Class Spec 1.0.
243 /*-------------------------------------------------------------------------*/
247 struct list_head list
;
250 static struct f_audio_buf
*f_audio_buffer_alloc(int buf_size
)
252 struct f_audio_buf
*copy_buf
;
254 copy_buf
= kzalloc(sizeof *copy_buf
, GFP_ATOMIC
);
256 return ERR_PTR(-ENOMEM
);
258 copy_buf
->buf
= kzalloc(buf_size
, GFP_ATOMIC
);
259 if (!copy_buf
->buf
) {
261 return ERR_PTR(-ENOMEM
);
267 static void f_audio_buffer_free(struct f_audio_buf
*audio_buf
)
269 kfree(audio_buf
->buf
);
272 /*-------------------------------------------------------------------------*/
280 /* endpoints handle full and/or high speeds */
281 struct usb_ep
*out_ep
;
284 struct f_audio_buf
*copy_buf
;
285 struct work_struct playback_work
;
286 struct list_head play_queue
;
288 /* Control Set command */
291 struct usb_audio_control
*set_con
;
294 static inline struct f_audio
*func_to_audio(struct usb_function
*f
)
296 return container_of(f
, struct f_audio
, card
.func
);
299 /*-------------------------------------------------------------------------*/
301 static void f_audio_playback_work(struct work_struct
*data
)
303 struct f_audio
*audio
= container_of(data
, struct f_audio
,
305 struct f_audio_buf
*play_buf
;
307 spin_lock_irq(&audio
->lock
);
308 if (list_empty(&audio
->play_queue
)) {
309 spin_unlock_irq(&audio
->lock
);
312 play_buf
= list_first_entry(&audio
->play_queue
,
313 struct f_audio_buf
, list
);
314 list_del(&play_buf
->list
);
315 spin_unlock_irq(&audio
->lock
);
317 u_audio_playback(&audio
->card
, play_buf
->buf
, play_buf
->actual
);
318 f_audio_buffer_free(play_buf
);
321 static int f_audio_out_ep_complete(struct usb_ep
*ep
, struct usb_request
*req
)
323 struct f_audio
*audio
= req
->context
;
324 struct usb_composite_dev
*cdev
= audio
->card
.func
.config
->cdev
;
325 struct f_audio_buf
*copy_buf
= audio
->copy_buf
;
326 struct f_uac1_legacy_opts
*opts
;
330 opts
= container_of(audio
->card
.func
.fi
, struct f_uac1_legacy_opts
,
332 audio_buf_size
= opts
->audio_buf_size
;
337 /* Copy buffer is full, add it to the play_queue */
338 if (audio_buf_size
- copy_buf
->actual
< req
->actual
) {
339 spin_lock_irq(&audio
->lock
);
340 list_add_tail(©_buf
->list
, &audio
->play_queue
);
341 spin_unlock_irq(&audio
->lock
);
342 schedule_work(&audio
->playback_work
);
343 copy_buf
= f_audio_buffer_alloc(audio_buf_size
);
344 if (IS_ERR(copy_buf
))
348 memcpy(copy_buf
->buf
+ copy_buf
->actual
, req
->buf
, req
->actual
);
349 copy_buf
->actual
+= req
->actual
;
350 audio
->copy_buf
= copy_buf
;
352 err
= usb_ep_queue(ep
, req
, GFP_ATOMIC
);
354 ERROR(cdev
, "%s queue req: %d\n", ep
->name
, err
);
360 static void f_audio_complete(struct usb_ep
*ep
, struct usb_request
*req
)
362 struct f_audio
*audio
= req
->context
;
363 int status
= req
->status
;
365 struct usb_ep
*out_ep
= audio
->out_ep
;
369 case 0: /* normal completion? */
371 f_audio_out_ep_complete(ep
, req
);
372 else if (audio
->set_con
) {
373 memcpy(&data
, req
->buf
, req
->length
);
374 audio
->set_con
->set(audio
->set_con
, audio
->set_cmd
,
376 audio
->set_con
= NULL
;
384 static int audio_set_intf_req(struct usb_function
*f
,
385 const struct usb_ctrlrequest
*ctrl
)
387 struct f_audio
*audio
= func_to_audio(f
);
388 struct usb_composite_dev
*cdev
= f
->config
->cdev
;
389 struct usb_request
*req
= cdev
->req
;
390 u8 id
= ((le16_to_cpu(ctrl
->wIndex
) >> 8) & 0xFF);
391 u16 len
= le16_to_cpu(ctrl
->wLength
);
392 u16 w_value
= le16_to_cpu(ctrl
->wValue
);
393 u8 con_sel
= (w_value
>> 8) & 0xFF;
394 u8 cmd
= (ctrl
->bRequest
& 0x0F);
395 struct usb_audio_control_selector
*cs
;
396 struct usb_audio_control
*con
;
398 DBG(cdev
, "bRequest 0x%x, w_value 0x%04x, len %d, entity %d\n",
399 ctrl
->bRequest
, w_value
, len
, id
);
401 list_for_each_entry(cs
, &audio
->cs
, list
) {
403 list_for_each_entry(con
, &cs
->control
, list
) {
404 if (con
->type
== con_sel
) {
405 audio
->set_con
= con
;
413 audio
->set_cmd
= cmd
;
414 req
->context
= audio
;
415 req
->complete
= f_audio_complete
;
420 static int audio_get_intf_req(struct usb_function
*f
,
421 const struct usb_ctrlrequest
*ctrl
)
423 struct f_audio
*audio
= func_to_audio(f
);
424 struct usb_composite_dev
*cdev
= f
->config
->cdev
;
425 struct usb_request
*req
= cdev
->req
;
426 int value
= -EOPNOTSUPP
;
427 u8 id
= ((le16_to_cpu(ctrl
->wIndex
) >> 8) & 0xFF);
428 u16 len
= le16_to_cpu(ctrl
->wLength
);
429 u16 w_value
= le16_to_cpu(ctrl
->wValue
);
430 u8 con_sel
= (w_value
>> 8) & 0xFF;
431 u8 cmd
= (ctrl
->bRequest
& 0x0F);
432 struct usb_audio_control_selector
*cs
;
433 struct usb_audio_control
*con
;
435 DBG(cdev
, "bRequest 0x%x, w_value 0x%04x, len %d, entity %d\n",
436 ctrl
->bRequest
, w_value
, len
, id
);
438 list_for_each_entry(cs
, &audio
->cs
, list
) {
440 list_for_each_entry(con
, &cs
->control
, list
) {
441 if (con
->type
== con_sel
&& con
->get
) {
442 value
= con
->get(con
, cmd
);
450 req
->context
= audio
;
451 req
->complete
= f_audio_complete
;
452 len
= min_t(size_t, sizeof(value
), len
);
453 memcpy(req
->buf
, &value
, len
);
458 static int audio_set_endpoint_req(struct usb_function
*f
,
459 const struct usb_ctrlrequest
*ctrl
)
461 struct usb_composite_dev
*cdev
= f
->config
->cdev
;
462 int value
= -EOPNOTSUPP
;
463 u16 ep
= le16_to_cpu(ctrl
->wIndex
);
464 u16 len
= le16_to_cpu(ctrl
->wLength
);
465 u16 w_value
= le16_to_cpu(ctrl
->wValue
);
467 DBG(cdev
, "bRequest 0x%x, w_value 0x%04x, len %d, endpoint %d\n",
468 ctrl
->bRequest
, w_value
, len
, ep
);
470 switch (ctrl
->bRequest
) {
494 static int audio_get_endpoint_req(struct usb_function
*f
,
495 const struct usb_ctrlrequest
*ctrl
)
497 struct usb_composite_dev
*cdev
= f
->config
->cdev
;
498 int value
= -EOPNOTSUPP
;
499 u8 ep
= ((le16_to_cpu(ctrl
->wIndex
) >> 8) & 0xFF);
500 u16 len
= le16_to_cpu(ctrl
->wLength
);
501 u16 w_value
= le16_to_cpu(ctrl
->wValue
);
503 DBG(cdev
, "bRequest 0x%x, w_value 0x%04x, len %d, endpoint %d\n",
504 ctrl
->bRequest
, w_value
, len
, ep
);
506 switch (ctrl
->bRequest
) {
523 f_audio_setup(struct usb_function
*f
, const struct usb_ctrlrequest
*ctrl
)
525 struct usb_composite_dev
*cdev
= f
->config
->cdev
;
526 struct usb_request
*req
= cdev
->req
;
527 int value
= -EOPNOTSUPP
;
528 u16 w_index
= le16_to_cpu(ctrl
->wIndex
);
529 u16 w_value
= le16_to_cpu(ctrl
->wValue
);
530 u16 w_length
= le16_to_cpu(ctrl
->wLength
);
532 /* composite driver infrastructure handles everything; interface
533 * activation uses set_alt().
535 switch (ctrl
->bRequestType
) {
536 case USB_DIR_OUT
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
:
537 value
= audio_set_intf_req(f
, ctrl
);
540 case USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
:
541 value
= audio_get_intf_req(f
, ctrl
);
544 case USB_DIR_OUT
| USB_TYPE_CLASS
| USB_RECIP_ENDPOINT
:
545 value
= audio_set_endpoint_req(f
, ctrl
);
548 case USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_ENDPOINT
:
549 value
= audio_get_endpoint_req(f
, ctrl
);
553 ERROR(cdev
, "invalid control req%02x.%02x v%04x i%04x l%d\n",
554 ctrl
->bRequestType
, ctrl
->bRequest
,
555 w_value
, w_index
, w_length
);
558 /* respond with data transfer or status phase? */
560 DBG(cdev
, "audio req%02x.%02x v%04x i%04x l%d\n",
561 ctrl
->bRequestType
, ctrl
->bRequest
,
562 w_value
, w_index
, w_length
);
565 value
= usb_ep_queue(cdev
->gadget
->ep0
, req
, GFP_ATOMIC
);
567 ERROR(cdev
, "audio response on err %d\n", value
);
570 /* device either stalls (value < 0) or reports success */
574 static int f_audio_set_alt(struct usb_function
*f
, unsigned intf
, unsigned alt
)
576 struct f_audio
*audio
= func_to_audio(f
);
577 struct usb_composite_dev
*cdev
= f
->config
->cdev
;
578 struct usb_ep
*out_ep
= audio
->out_ep
;
579 struct usb_request
*req
;
580 struct f_uac1_legacy_opts
*opts
;
581 int req_buf_size
, req_count
, audio_buf_size
;
584 DBG(cdev
, "intf %d, alt %d\n", intf
, alt
);
586 opts
= container_of(f
->fi
, struct f_uac1_legacy_opts
, func_inst
);
587 req_buf_size
= opts
->req_buf_size
;
588 req_count
= opts
->req_count
;
589 audio_buf_size
= opts
->audio_buf_size
;
591 /* No i/f has more than 2 alt settings */
593 ERROR(cdev
, "%s:%d Error!\n", __func__
, __LINE__
);
597 if (intf
== audio
->ac_intf
) {
598 /* Control I/f has only 1 AltSetting - 0 */
600 ERROR(cdev
, "%s:%d Error!\n", __func__
, __LINE__
);
604 } else if (intf
== audio
->as_intf
) {
606 err
= config_ep_by_speed(cdev
->gadget
, f
, out_ep
);
610 usb_ep_enable(out_ep
);
611 audio
->copy_buf
= f_audio_buffer_alloc(audio_buf_size
);
612 if (IS_ERR(audio
->copy_buf
))
616 * allocate a bunch of read buffers
617 * and queue them all at once.
619 for (i
= 0; i
< req_count
&& err
== 0; i
++) {
620 req
= usb_ep_alloc_request(out_ep
, GFP_ATOMIC
);
622 req
->buf
= kzalloc(req_buf_size
,
625 req
->length
= req_buf_size
;
626 req
->context
= audio
;
629 err
= usb_ep_queue(out_ep
,
633 "%s queue req: %d\n",
642 struct f_audio_buf
*copy_buf
= audio
->copy_buf
;
644 list_add_tail(©_buf
->list
,
646 schedule_work(&audio
->playback_work
);
655 static int f_audio_get_alt(struct usb_function
*f
, unsigned intf
)
657 struct f_audio
*audio
= func_to_audio(f
);
658 struct usb_composite_dev
*cdev
= f
->config
->cdev
;
660 if (intf
== audio
->ac_intf
)
661 return audio
->ac_alt
;
662 else if (intf
== audio
->as_intf
)
663 return audio
->as_alt
;
665 ERROR(cdev
, "%s:%d Invalid Interface %d!\n",
666 __func__
, __LINE__
, intf
);
671 static void f_audio_disable(struct usb_function
*f
)
676 /*-------------------------------------------------------------------------*/
678 static void f_audio_build_desc(struct f_audio
*audio
)
680 struct gaudio
*card
= &audio
->card
;
684 /* Set channel numbers */
685 input_terminal_desc
.bNrChannels
= u_audio_get_playback_channels(card
);
686 as_type_i_desc
.bNrChannels
= u_audio_get_playback_channels(card
);
688 /* Set sample rates */
689 rate
= u_audio_get_playback_rate(card
);
690 sam_freq
= as_type_i_desc
.tSamFreq
[0];
691 memcpy(sam_freq
, &rate
, 3);
693 /* Todo: Set Sample bits and other parameters */
698 /* audio function driver setup/binding */
700 f_audio_bind(struct usb_configuration
*c
, struct usb_function
*f
)
702 struct usb_composite_dev
*cdev
= c
->cdev
;
703 struct f_audio
*audio
= func_to_audio(f
);
704 struct usb_string
*us
;
706 struct usb_ep
*ep
= NULL
;
707 struct f_uac1_legacy_opts
*audio_opts
;
709 audio_opts
= container_of(f
->fi
, struct f_uac1_legacy_opts
, func_inst
);
710 audio
->card
.gadget
= c
->cdev
->gadget
;
711 /* set up ASLA audio devices */
712 if (!audio_opts
->bound
) {
713 status
= gaudio_setup(&audio
->card
);
716 audio_opts
->bound
= true;
718 us
= usb_gstrings_attach(cdev
, uac1_strings
, ARRAY_SIZE(strings_uac1
));
721 ac_interface_desc
.iInterface
= us
[STR_AC_IF
].id
;
722 input_terminal_desc
.iTerminal
= us
[STR_INPUT_TERMINAL
].id
;
723 input_terminal_desc
.iChannelNames
= us
[STR_INPUT_TERMINAL_CH_NAMES
].id
;
724 feature_unit_desc
.iFeature
= us
[STR_FEAT_DESC_0
].id
;
725 output_terminal_desc
.iTerminal
= us
[STR_OUTPUT_TERMINAL
].id
;
726 as_interface_alt_0_desc
.iInterface
= us
[STR_AS_IF_ALT0
].id
;
727 as_interface_alt_1_desc
.iInterface
= us
[STR_AS_IF_ALT1
].id
;
730 f_audio_build_desc(audio
);
732 /* allocate instance-specific interface IDs, and patch descriptors */
733 status
= usb_interface_id(c
, f
);
736 ac_interface_desc
.bInterfaceNumber
= status
;
737 audio
->ac_intf
= status
;
740 status
= usb_interface_id(c
, f
);
743 as_interface_alt_0_desc
.bInterfaceNumber
= status
;
744 as_interface_alt_1_desc
.bInterfaceNumber
= status
;
745 audio
->as_intf
= status
;
750 /* allocate instance-specific endpoints */
751 ep
= usb_ep_autoconfig(cdev
->gadget
, &as_out_ep_desc
);
755 audio
->out_ep
->desc
= &as_out_ep_desc
;
757 /* copy descriptors, and track endpoint copies */
758 status
= usb_assign_descriptors(f
, f_audio_desc
, f_audio_desc
, NULL
,
765 gaudio_cleanup(&audio
->card
);
769 /*-------------------------------------------------------------------------*/
771 static int generic_set_cmd(struct usb_audio_control
*con
, u8 cmd
, int value
)
773 con
->data
[cmd
] = value
;
778 static int generic_get_cmd(struct usb_audio_control
*con
, u8 cmd
)
780 return con
->data
[cmd
];
783 /* Todo: add more control selecotor dynamically */
784 static int control_selector_init(struct f_audio
*audio
)
786 INIT_LIST_HEAD(&audio
->cs
);
787 list_add(&feature_unit
.list
, &audio
->cs
);
789 INIT_LIST_HEAD(&feature_unit
.control
);
790 list_add(&mute_control
.list
, &feature_unit
.control
);
791 list_add(&volume_control
.list
, &feature_unit
.control
);
793 volume_control
.data
[UAC__CUR
] = 0xffc0;
794 volume_control
.data
[UAC__MIN
] = 0xe3a0;
795 volume_control
.data
[UAC__MAX
] = 0xfff0;
796 volume_control
.data
[UAC__RES
] = 0x0030;
802 struct f_uac1_legacy_opts
*to_f_uac1_opts(struct config_item
*item
)
804 return container_of(to_config_group(item
), struct f_uac1_legacy_opts
,
808 static void f_uac1_attr_release(struct config_item
*item
)
810 struct f_uac1_legacy_opts
*opts
= to_f_uac1_opts(item
);
812 usb_put_function_instance(&opts
->func_inst
);
815 static struct configfs_item_operations f_uac1_item_ops
= {
816 .release
= f_uac1_attr_release
,
819 #define UAC1_INT_ATTRIBUTE(name) \
820 static ssize_t f_uac1_opts_##name##_show(struct config_item *item, \
823 struct f_uac1_legacy_opts *opts = to_f_uac1_opts(item); \
826 mutex_lock(&opts->lock); \
827 result = sprintf(page, "%u\n", opts->name); \
828 mutex_unlock(&opts->lock); \
833 static ssize_t f_uac1_opts_##name##_store(struct config_item *item, \
834 const char *page, size_t len) \
836 struct f_uac1_legacy_opts *opts = to_f_uac1_opts(item); \
840 mutex_lock(&opts->lock); \
841 if (opts->refcnt) { \
846 ret = kstrtou32(page, 0, &num); \
854 mutex_unlock(&opts->lock); \
858 CONFIGFS_ATTR(f_uac1_opts_, name)
860 UAC1_INT_ATTRIBUTE(req_buf_size
);
861 UAC1_INT_ATTRIBUTE(req_count
);
862 UAC1_INT_ATTRIBUTE(audio_buf_size
);
864 #define UAC1_STR_ATTRIBUTE(name) \
865 static ssize_t f_uac1_opts_##name##_show(struct config_item *item, \
868 struct f_uac1_legacy_opts *opts = to_f_uac1_opts(item); \
871 mutex_lock(&opts->lock); \
872 result = sprintf(page, "%s\n", opts->name); \
873 mutex_unlock(&opts->lock); \
878 static ssize_t f_uac1_opts_##name##_store(struct config_item *item, \
879 const char *page, size_t len) \
881 struct f_uac1_legacy_opts *opts = to_f_uac1_opts(item); \
885 mutex_lock(&opts->lock); \
889 tmp = kstrndup(page, len, GFP_KERNEL); \
894 if (opts->name##_alloc) \
896 opts->name##_alloc = true; \
901 mutex_unlock(&opts->lock); \
905 CONFIGFS_ATTR(f_uac1_opts_, name)
907 UAC1_STR_ATTRIBUTE(fn_play
);
908 UAC1_STR_ATTRIBUTE(fn_cap
);
909 UAC1_STR_ATTRIBUTE(fn_cntl
);
911 static struct configfs_attribute
*f_uac1_attrs
[] = {
912 &f_uac1_opts_attr_req_buf_size
,
913 &f_uac1_opts_attr_req_count
,
914 &f_uac1_opts_attr_audio_buf_size
,
915 &f_uac1_opts_attr_fn_play
,
916 &f_uac1_opts_attr_fn_cap
,
917 &f_uac1_opts_attr_fn_cntl
,
921 static const struct config_item_type f_uac1_func_type
= {
922 .ct_item_ops
= &f_uac1_item_ops
,
923 .ct_attrs
= f_uac1_attrs
,
924 .ct_owner
= THIS_MODULE
,
927 static void f_audio_free_inst(struct usb_function_instance
*f
)
929 struct f_uac1_legacy_opts
*opts
;
931 opts
= container_of(f
, struct f_uac1_legacy_opts
, func_inst
);
932 if (opts
->fn_play_alloc
)
933 kfree(opts
->fn_play
);
934 if (opts
->fn_cap_alloc
)
936 if (opts
->fn_cntl_alloc
)
937 kfree(opts
->fn_cntl
);
941 static struct usb_function_instance
*f_audio_alloc_inst(void)
943 struct f_uac1_legacy_opts
*opts
;
945 opts
= kzalloc(sizeof(*opts
), GFP_KERNEL
);
947 return ERR_PTR(-ENOMEM
);
949 mutex_init(&opts
->lock
);
950 opts
->func_inst
.free_func_inst
= f_audio_free_inst
;
952 config_group_init_type_name(&opts
->func_inst
.group
, "",
955 opts
->req_buf_size
= UAC1_OUT_EP_MAX_PACKET_SIZE
;
956 opts
->req_count
= UAC1_REQ_COUNT
;
957 opts
->audio_buf_size
= UAC1_AUDIO_BUF_SIZE
;
958 opts
->fn_play
= FILE_PCM_PLAYBACK
;
959 opts
->fn_cap
= FILE_PCM_CAPTURE
;
960 opts
->fn_cntl
= FILE_CONTROL
;
961 return &opts
->func_inst
;
964 static void f_audio_free(struct usb_function
*f
)
966 struct f_audio
*audio
= func_to_audio(f
);
967 struct f_uac1_legacy_opts
*opts
;
969 gaudio_cleanup(&audio
->card
);
970 opts
= container_of(f
->fi
, struct f_uac1_legacy_opts
, func_inst
);
972 mutex_lock(&opts
->lock
);
974 mutex_unlock(&opts
->lock
);
977 static void f_audio_unbind(struct usb_configuration
*c
, struct usb_function
*f
)
979 usb_free_all_descriptors(f
);
982 static struct usb_function
*f_audio_alloc(struct usb_function_instance
*fi
)
984 struct f_audio
*audio
;
985 struct f_uac1_legacy_opts
*opts
;
987 /* allocate and initialize one new instance */
988 audio
= kzalloc(sizeof(*audio
), GFP_KERNEL
);
990 return ERR_PTR(-ENOMEM
);
992 audio
->card
.func
.name
= "g_audio";
994 opts
= container_of(fi
, struct f_uac1_legacy_opts
, func_inst
);
995 mutex_lock(&opts
->lock
);
997 mutex_unlock(&opts
->lock
);
998 INIT_LIST_HEAD(&audio
->play_queue
);
999 spin_lock_init(&audio
->lock
);
1001 audio
->card
.func
.bind
= f_audio_bind
;
1002 audio
->card
.func
.unbind
= f_audio_unbind
;
1003 audio
->card
.func
.set_alt
= f_audio_set_alt
;
1004 audio
->card
.func
.get_alt
= f_audio_get_alt
;
1005 audio
->card
.func
.setup
= f_audio_setup
;
1006 audio
->card
.func
.disable
= f_audio_disable
;
1007 audio
->card
.func
.free_func
= f_audio_free
;
1009 control_selector_init(audio
);
1011 INIT_WORK(&audio
->playback_work
, f_audio_playback_work
);
1013 return &audio
->card
.func
;
1016 DECLARE_USB_FUNCTION_INIT(uac1_legacy
, f_audio_alloc_inst
, f_audio_alloc
);
1017 MODULE_LICENSE("GPL");
1018 MODULE_AUTHOR("Bryan Wu");