2 * f_audio.c -- USB Audio class function driver
4 * Copyright (C) 2008 Bryan Wu <cooloney@kernel.org>
5 * Copyright (C) 2008 Analog Devices, Inc
7 * Enter bugs at http://blackfin.uclinux.org/
9 * Licensed under the GPL-2 or later.
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>
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
);
24 * DESCRIPTORS ... most are static, but strings and full
25 * configuration descriptors are built on demand.
29 * We have two interfaces- AudioControl and AudioStreaming
30 * TODO: only supcard playback currently
32 #define F_AUDIO_AC_INTERFACE 0
33 #define F_AUDIO_AS_INTERFACE 1
34 #define F_AUDIO_NUM_INTERFACES 2
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
,
41 .bInterfaceClass
= USB_CLASS_AUDIO
,
42 .bInterfaceSubClass
= USB_SUBCLASS_AUDIOCONTROL
,
45 DECLARE_UAC_AC_HEADER_DESCRIPTOR(2);
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 */
52 static 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
,
60 [0] = F_AUDIO_AC_INTERFACE
,
61 [1] = F_AUDIO_AS_INTERFACE
,
65 #define INPUT_TERMINAL_ID 1
66 static 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
,
73 .wChannelConfig
= 0x3,
76 DECLARE_UAC_FEATURE_UNIT_DESCRIPTOR(0);
78 #define FEATURE_UNIT_ID 2
79 static 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
,
86 .bmaControls
[0] = (UAC_FU_MUTE
| UAC_FU_VOLUME
),
89 static struct usb_audio_control mute_control
= {
90 .list
= LIST_HEAD_INIT(mute_control
.list
),
91 .name
= "Mute Control",
93 /* Todo: add real Mute control code */
94 .set
= generic_set_cmd
,
95 .get
= generic_get_cmd
,
98 static 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
,
107 static 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
,
115 #define OUTPUT_TERMINAL_ID 3
116 static 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
,
126 /* B.4.1 Standard AS Interface Descriptor */
127 static struct usb_interface_descriptor as_interface_alt_0_desc
= {
128 .bLength
= USB_DT_INTERFACE_SIZE
,
129 .bDescriptorType
= USB_DT_INTERFACE
,
130 .bAlternateSetting
= 0,
132 .bInterfaceClass
= USB_CLASS_AUDIO
,
133 .bInterfaceSubClass
= USB_SUBCLASS_AUDIOSTREAMING
,
136 static struct usb_interface_descriptor as_interface_alt_1_desc
= {
137 .bLength
= USB_DT_INTERFACE_SIZE
,
138 .bDescriptorType
= USB_DT_INTERFACE
,
139 .bAlternateSetting
= 1,
141 .bInterfaceClass
= USB_CLASS_AUDIO
,
142 .bInterfaceSubClass
= USB_SUBCLASS_AUDIOSTREAMING
,
145 /* B.4.2 Class-Specific AS Interface Descriptor */
146 static 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
,
152 .wFormatTag
= UAC_FORMAT_TYPE_I_PCM
,
155 DECLARE_UAC_FORMAT_TYPE_I_DISCRETE_DESC(1);
157 static 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
,
163 .bBitResolution
= 16,
167 /* Standard ISO OUT Endpoint Descriptor */
168 static 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
),
178 /* Class-specific AS ISO OUT Endpoint Descriptor */
179 static 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
,
184 .bLockDelayUnits
= 1,
185 .wLockDelay
= __constant_cpu_to_le16(1),
188 static struct usb_descriptor_header
*f_audio_desc
[] = {
189 (struct usb_descriptor_header
*)&ac_interface_desc
,
190 (struct usb_descriptor_header
*)&ac_header_desc
,
192 (struct usb_descriptor_header
*)&input_terminal_desc
,
193 (struct usb_descriptor_header
*)&output_terminal_desc
,
194 (struct usb_descriptor_header
*)&feature_unit_desc
,
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
,
200 (struct usb_descriptor_header
*)&as_type_i_desc
,
202 (struct usb_descriptor_header
*)&as_out_ep_desc
,
203 (struct usb_descriptor_header
*)&as_iso_out_desc
,
210 STR_INPUT_TERMINAL_CH_NAMES
,
217 static 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",
228 static struct usb_gadget_strings str_uac1
= {
229 .language
= 0x0409, /* en-us */
230 .strings
= strings_uac1
,
233 static struct usb_gadget_strings
*uac1_strings
[] = {
239 * This function is an ALSA sound card following USB Audio Class Spec 1.0.
242 /*-------------------------------------------------------------------------*/
246 struct list_head list
;
249 static struct f_audio_buf
*f_audio_buffer_alloc(int buf_size
)
251 struct f_audio_buf
*copy_buf
;
253 copy_buf
= kzalloc(sizeof *copy_buf
, GFP_ATOMIC
);
255 return ERR_PTR(-ENOMEM
);
257 copy_buf
->buf
= kzalloc(buf_size
, GFP_ATOMIC
);
258 if (!copy_buf
->buf
) {
260 return ERR_PTR(-ENOMEM
);
266 static void f_audio_buffer_free(struct f_audio_buf
*audio_buf
)
268 kfree(audio_buf
->buf
);
271 /*-------------------------------------------------------------------------*/
276 /* endpoints handle full and/or high speeds */
277 struct usb_ep
*out_ep
;
280 struct f_audio_buf
*copy_buf
;
281 struct work_struct playback_work
;
282 struct list_head play_queue
;
284 /* Control Set command */
287 struct usb_audio_control
*set_con
;
290 static inline struct f_audio
*func_to_audio(struct usb_function
*f
)
292 return container_of(f
, struct f_audio
, card
.func
);
295 /*-------------------------------------------------------------------------*/
297 static void f_audio_playback_work(struct work_struct
*data
)
299 struct f_audio
*audio
= container_of(data
, struct f_audio
,
301 struct f_audio_buf
*play_buf
;
303 spin_lock_irq(&audio
->lock
);
304 if (list_empty(&audio
->play_queue
)) {
305 spin_unlock_irq(&audio
->lock
);
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
);
313 u_audio_playback(&audio
->card
, play_buf
->buf
, play_buf
->actual
);
314 f_audio_buffer_free(play_buf
);
317 static int f_audio_out_ep_complete(struct usb_ep
*ep
, struct usb_request
*req
)
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
;
326 opts
= container_of(audio
->card
.func
.fi
, struct f_uac1_opts
,
328 audio_buf_size
= opts
->audio_buf_size
;
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
))
342 memcpy(copy_buf
->buf
+ copy_buf
->actual
, req
->buf
, req
->actual
);
343 copy_buf
->actual
+= req
->actual
;
344 audio
->copy_buf
= copy_buf
;
346 err
= usb_ep_queue(ep
, req
, GFP_ATOMIC
);
348 ERROR(cdev
, "%s queue req: %d\n", ep
->name
, err
);
354 static void f_audio_complete(struct usb_ep
*ep
, struct usb_request
*req
)
356 struct f_audio
*audio
= req
->context
;
357 int status
= req
->status
;
359 struct usb_ep
*out_ep
= audio
->out_ep
;
363 case 0: /* normal completion? */
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
,
370 audio
->set_con
= NULL
;
378 static int audio_set_intf_req(struct usb_function
*f
,
379 const struct usb_ctrlrequest
*ctrl
)
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
;
392 DBG(cdev
, "bRequest 0x%x, w_value 0x%04x, len %d, entity %d\n",
393 ctrl
->bRequest
, w_value
, len
, id
);
395 list_for_each_entry(cs
, &audio
->cs
, list
) {
397 list_for_each_entry(con
, &cs
->control
, list
) {
398 if (con
->type
== con_sel
) {
399 audio
->set_con
= con
;
407 audio
->set_cmd
= cmd
;
408 req
->context
= audio
;
409 req
->complete
= f_audio_complete
;
414 static int audio_get_intf_req(struct usb_function
*f
,
415 const struct usb_ctrlrequest
*ctrl
)
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
;
429 DBG(cdev
, "bRequest 0x%x, w_value 0x%04x, len %d, entity %d\n",
430 ctrl
->bRequest
, w_value
, len
, id
);
432 list_for_each_entry(cs
, &audio
->cs
, list
) {
434 list_for_each_entry(con
, &cs
->control
, list
) {
435 if (con
->type
== con_sel
&& con
->get
) {
436 value
= con
->get(con
, cmd
);
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
);
452 static int audio_set_endpoint_req(struct usb_function
*f
,
453 const struct usb_ctrlrequest
*ctrl
)
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
);
461 DBG(cdev
, "bRequest 0x%x, w_value 0x%04x, len %d, endpoint %d\n",
462 ctrl
->bRequest
, w_value
, len
, ep
);
464 switch (ctrl
->bRequest
) {
488 static int audio_get_endpoint_req(struct usb_function
*f
,
489 const struct usb_ctrlrequest
*ctrl
)
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
);
497 DBG(cdev
, "bRequest 0x%x, w_value 0x%04x, len %d, endpoint %d\n",
498 ctrl
->bRequest
, w_value
, len
, ep
);
500 switch (ctrl
->bRequest
) {
517 f_audio_setup(struct usb_function
*f
, const struct usb_ctrlrequest
*ctrl
)
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
);
526 /* composite driver infrastructure handles everything; interface
527 * activation uses set_alt().
529 switch (ctrl
->bRequestType
) {
530 case USB_DIR_OUT
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
:
531 value
= audio_set_intf_req(f
, ctrl
);
534 case USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
:
535 value
= audio_get_intf_req(f
, ctrl
);
538 case USB_DIR_OUT
| USB_TYPE_CLASS
| USB_RECIP_ENDPOINT
:
539 value
= audio_set_endpoint_req(f
, ctrl
);
542 case USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_ENDPOINT
:
543 value
= audio_get_endpoint_req(f
, ctrl
);
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
);
552 /* respond with data transfer or status phase? */
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
);
559 value
= usb_ep_queue(cdev
->gadget
->ep0
, req
, GFP_ATOMIC
);
561 ERROR(cdev
, "audio response on err %d\n", value
);
564 /* device either stalls (value < 0) or reports success */
568 static int f_audio_set_alt(struct usb_function
*f
, unsigned intf
, unsigned alt
)
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
;
578 DBG(cdev
, "intf %d, alt %d\n", intf
, alt
);
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
;
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
))
594 * allocate a bunch of read buffers
595 * and queue them all at once.
597 for (i
= 0; i
< req_count
&& err
== 0; i
++) {
598 req
= usb_ep_alloc_request(out_ep
, GFP_ATOMIC
);
600 req
->buf
= kzalloc(req_buf_size
,
603 req
->length
= req_buf_size
;
604 req
->context
= audio
;
607 err
= usb_ep_queue(out_ep
,
611 "%s queue req: %d\n",
620 struct f_audio_buf
*copy_buf
= audio
->copy_buf
;
622 list_add_tail(©_buf
->list
,
624 schedule_work(&audio
->playback_work
);
632 static void f_audio_disable(struct usb_function
*f
)
637 /*-------------------------------------------------------------------------*/
639 static void f_audio_build_desc(struct f_audio
*audio
)
641 struct gaudio
*card
= &audio
->card
;
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
);
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);
654 /* Todo: Set Sample bits and other parameters */
659 /* audio function driver setup/binding */
661 f_audio_bind(struct usb_configuration
*c
, struct usb_function
*f
)
663 struct usb_composite_dev
*cdev
= c
->cdev
;
664 struct f_audio
*audio
= func_to_audio(f
);
665 struct usb_string
*us
;
667 struct usb_ep
*ep
= NULL
;
668 struct f_uac1_opts
*audio_opts
;
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
);
678 audio_opts
->bound
= true;
680 us
= usb_gstrings_attach(cdev
, uac1_strings
, ARRAY_SIZE(strings_uac1
));
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
;
692 f_audio_build_desc(audio
);
694 /* allocate instance-specific interface IDs, and patch descriptors */
695 status
= usb_interface_id(c
, f
);
698 ac_interface_desc
.bInterfaceNumber
= status
;
700 status
= usb_interface_id(c
, f
);
703 as_interface_alt_0_desc
.bInterfaceNumber
= status
;
704 as_interface_alt_1_desc
.bInterfaceNumber
= status
;
708 /* allocate instance-specific endpoints */
709 ep
= usb_ep_autoconfig(cdev
->gadget
, &as_out_ep_desc
);
713 audio
->out_ep
->desc
= &as_out_ep_desc
;
714 ep
->driver_data
= cdev
; /* claim */
718 /* copy descriptors, and track endpoint copies */
719 status
= usb_assign_descriptors(f
, f_audio_desc
, f_audio_desc
, NULL
);
725 gaudio_cleanup(&audio
->card
);
727 ep
->driver_data
= NULL
;
731 /*-------------------------------------------------------------------------*/
733 static int generic_set_cmd(struct usb_audio_control
*con
, u8 cmd
, int value
)
735 con
->data
[cmd
] = value
;
740 static int generic_get_cmd(struct usb_audio_control
*con
, u8 cmd
)
742 return con
->data
[cmd
];
745 /* Todo: add more control selecotor dynamically */
746 static int control_selector_init(struct f_audio
*audio
)
748 INIT_LIST_HEAD(&audio
->cs
);
749 list_add(&feature_unit
.list
, &audio
->cs
);
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
);
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;
763 static inline struct f_uac1_opts
*to_f_uac1_opts(struct config_item
*item
)
765 return container_of(to_config_group(item
), struct f_uac1_opts
,
769 CONFIGFS_ATTR_STRUCT(f_uac1_opts
);
770 CONFIGFS_ATTR_OPS(f_uac1_opts
);
772 static void f_uac1_attr_release(struct config_item
*item
)
774 struct f_uac1_opts
*opts
= to_f_uac1_opts(item
);
776 usb_put_function_instance(&opts
->func_inst
);
779 static 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
,
785 #define UAC1_INT_ATTRIBUTE(name) \
786 static ssize_t f_uac1_opts_##name##_show(struct f_uac1_opts *opts, \
791 mutex_lock(&opts->lock); \
792 result = sprintf(page, "%u\n", opts->name); \
793 mutex_unlock(&opts->lock); \
798 static ssize_t f_uac1_opts_##name##_store(struct f_uac1_opts *opts, \
799 const char *page, size_t len) \
804 mutex_lock(&opts->lock); \
805 if (opts->refcnt) { \
810 ret = kstrtou32(page, 0, &num); \
818 mutex_unlock(&opts->lock); \
822 static 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)
827 UAC1_INT_ATTRIBUTE(req_buf_size
);
828 UAC1_INT_ATTRIBUTE(req_count
);
829 UAC1_INT_ATTRIBUTE(audio_buf_size
);
831 #define UAC1_STR_ATTRIBUTE(name) \
832 static ssize_t f_uac1_opts_##name##_show(struct f_uac1_opts *opts, \
837 mutex_lock(&opts->lock); \
838 result = sprintf(page, "%s\n", opts->name); \
839 mutex_unlock(&opts->lock); \
844 static ssize_t f_uac1_opts_##name##_store(struct f_uac1_opts *opts, \
845 const char *page, size_t len) \
850 mutex_lock(&opts->lock); \
854 tmp = kstrndup(page, len, GFP_KERNEL); \
859 if (opts->name##_alloc) \
861 opts->name##_alloc = true; \
866 mutex_unlock(&opts->lock); \
870 static 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)
875 UAC1_STR_ATTRIBUTE(fn_play
);
876 UAC1_STR_ATTRIBUTE(fn_cap
);
877 UAC1_STR_ATTRIBUTE(fn_cntl
);
879 static 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
,
889 static 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
,
895 static void f_audio_free_inst(struct usb_function_instance
*f
)
897 struct f_uac1_opts
*opts
;
899 opts
= container_of(f
, struct f_uac1_opts
, func_inst
);
900 if (opts
->fn_play_alloc
)
901 kfree(opts
->fn_play
);
902 if (opts
->fn_cap_alloc
)
904 if (opts
->fn_cntl_alloc
)
905 kfree(opts
->fn_cntl
);
909 static struct usb_function_instance
*f_audio_alloc_inst(void)
911 struct f_uac1_opts
*opts
;
913 opts
= kzalloc(sizeof(*opts
), GFP_KERNEL
);
915 return ERR_PTR(-ENOMEM
);
917 mutex_init(&opts
->lock
);
918 opts
->func_inst
.free_func_inst
= f_audio_free_inst
;
920 config_group_init_type_name(&opts
->func_inst
.group
, "",
923 opts
->req_buf_size
= UAC1_OUT_EP_MAX_PACKET_SIZE
;
924 opts
->req_count
= UAC1_REQ_COUNT
;
925 opts
->audio_buf_size
= UAC1_AUDIO_BUF_SIZE
;
926 opts
->fn_play
= FILE_PCM_PLAYBACK
;
927 opts
->fn_cap
= FILE_PCM_CAPTURE
;
928 opts
->fn_cntl
= FILE_CONTROL
;
929 return &opts
->func_inst
;
932 static void f_audio_free(struct usb_function
*f
)
934 struct f_audio
*audio
= func_to_audio(f
);
935 struct f_uac1_opts
*opts
;
937 gaudio_cleanup(&audio
->card
);
938 opts
= container_of(f
->fi
, struct f_uac1_opts
, func_inst
);
940 mutex_lock(&opts
->lock
);
942 mutex_unlock(&opts
->lock
);
945 static void f_audio_unbind(struct usb_configuration
*c
, struct usb_function
*f
)
947 usb_free_all_descriptors(f
);
950 static struct usb_function
*f_audio_alloc(struct usb_function_instance
*fi
)
952 struct f_audio
*audio
;
953 struct f_uac1_opts
*opts
;
955 /* allocate and initialize one new instance */
956 audio
= kzalloc(sizeof(*audio
), GFP_KERNEL
);
958 return ERR_PTR(-ENOMEM
);
960 audio
->card
.func
.name
= "g_audio";
962 opts
= container_of(fi
, struct f_uac1_opts
, func_inst
);
963 mutex_lock(&opts
->lock
);
965 mutex_unlock(&opts
->lock
);
966 INIT_LIST_HEAD(&audio
->play_queue
);
967 spin_lock_init(&audio
->lock
);
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
;
976 control_selector_init(audio
);
978 INIT_WORK(&audio
->playback_work
, f_audio_playback_work
);
980 return &audio
->card
.func
;
983 DECLARE_USB_FUNCTION_INIT(uac1
, f_audio_alloc_inst
, f_audio_alloc
);
984 MODULE_LICENSE("GPL");
985 MODULE_AUTHOR("Bryan Wu");