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/kernel.h>
13 #include <linux/device.h>
14 #include <asm/atomic.h>
18 #define OUT_EP_MAX_PACKET_SIZE 200
19 static int req_buf_size
= OUT_EP_MAX_PACKET_SIZE
;
20 module_param(req_buf_size
, int, S_IRUGO
);
21 MODULE_PARM_DESC(req_buf_size
, "ISO OUT endpoint request buffer size");
23 static int req_count
= 256;
24 module_param(req_count
, int, S_IRUGO
);
25 MODULE_PARM_DESC(req_count
, "ISO OUT endpoint request count");
27 static int audio_buf_size
= 48000;
28 module_param(audio_buf_size
, int, S_IRUGO
);
29 MODULE_PARM_DESC(audio_buf_size
, "Audio buffer size");
32 * DESCRIPTORS ... most are static, but strings and full
33 * configuration descriptors are built on demand.
37 * We have two interfaces- AudioControl and AudioStreaming
38 * TODO: only supcard playback currently
40 #define F_AUDIO_AC_INTERFACE 0
41 #define F_AUDIO_AS_INTERFACE 1
42 #define F_AUDIO_NUM_INTERFACES 2
44 /* B.3.1 Standard AC Interface Descriptor */
45 static struct usb_interface_descriptor ac_interface_desc __initdata
= {
46 .bLength
= USB_DT_INTERFACE_SIZE
,
47 .bDescriptorType
= USB_DT_INTERFACE
,
49 .bInterfaceClass
= USB_CLASS_AUDIO
,
50 .bInterfaceSubClass
= USB_SUBCLASS_AUDIOCONTROL
,
53 DECLARE_USB_AC_HEADER_DESCRIPTOR(2);
55 #define USB_DT_AC_HEADER_LENGH USB_DT_AC_HEADER_SIZE(F_AUDIO_NUM_INTERFACES)
56 /* B.3.2 Class-Specific AC Interface Descriptor */
57 static struct usb_ac_header_descriptor_2 ac_header_desc
= {
58 .bLength
= USB_DT_AC_HEADER_LENGH
,
59 .bDescriptorType
= USB_DT_CS_INTERFACE
,
60 .bDescriptorSubtype
= HEADER
,
61 .bcdADC
= __constant_cpu_to_le16(0x0100),
62 .wTotalLength
= __constant_cpu_to_le16(USB_DT_AC_HEADER_LENGH
),
63 .bInCollection
= F_AUDIO_NUM_INTERFACES
,
65 [0] = F_AUDIO_AC_INTERFACE
,
66 [1] = F_AUDIO_AS_INTERFACE
,
70 #define INPUT_TERMINAL_ID 1
71 static struct usb_input_terminal_descriptor input_terminal_desc
= {
72 .bLength
= USB_DT_AC_INPUT_TERMINAL_SIZE
,
73 .bDescriptorType
= USB_DT_CS_INTERFACE
,
74 .bDescriptorSubtype
= INPUT_TERMINAL
,
75 .bTerminalID
= INPUT_TERMINAL_ID
,
76 .wTerminalType
= USB_AC_TERMINAL_STREAMING
,
78 .wChannelConfig
= 0x3,
81 DECLARE_USB_AC_FEATURE_UNIT_DESCRIPTOR(0);
83 #define FEATURE_UNIT_ID 2
84 static struct usb_ac_feature_unit_descriptor_0 feature_unit_desc
= {
85 .bLength
= USB_DT_AC_FEATURE_UNIT_SIZE(0),
86 .bDescriptorType
= USB_DT_CS_INTERFACE
,
87 .bDescriptorSubtype
= FEATURE_UNIT
,
88 .bUnitID
= FEATURE_UNIT_ID
,
89 .bSourceID
= INPUT_TERMINAL_ID
,
91 .bmaControls
[0] = (FU_MUTE
| FU_VOLUME
),
94 static struct usb_audio_control mute_control
= {
95 .list
= LIST_HEAD_INIT(mute_control
.list
),
96 .name
= "Mute Control",
98 /* Todo: add real Mute control code */
99 .set
= generic_set_cmd
,
100 .get
= generic_get_cmd
,
103 static struct usb_audio_control volume_control
= {
104 .list
= LIST_HEAD_INIT(volume_control
.list
),
105 .name
= "Volume Control",
106 .type
= VOLUME_CONTROL
,
107 /* Todo: add real Volume control code */
108 .set
= generic_set_cmd
,
109 .get
= generic_get_cmd
,
112 static struct usb_audio_control_selector feature_unit
= {
113 .list
= LIST_HEAD_INIT(feature_unit
.list
),
114 .id
= FEATURE_UNIT_ID
,
115 .name
= "Mute & Volume Control",
116 .type
= FEATURE_UNIT
,
117 .desc
= (struct usb_descriptor_header
*)&feature_unit_desc
,
120 #define OUTPUT_TERMINAL_ID 3
121 static struct usb_output_terminal_descriptor output_terminal_desc
= {
122 .bLength
= USB_DT_AC_OUTPUT_TERMINAL_SIZE
,
123 .bDescriptorType
= USB_DT_CS_INTERFACE
,
124 .bDescriptorSubtype
= OUTPUT_TERMINAL
,
125 .bTerminalID
= OUTPUT_TERMINAL_ID
,
126 .wTerminalType
= USB_AC_OUTPUT_TERMINAL_SPEAKER
,
127 .bAssocTerminal
= FEATURE_UNIT_ID
,
128 .bSourceID
= FEATURE_UNIT_ID
,
131 /* B.4.1 Standard AS Interface Descriptor */
132 static struct usb_interface_descriptor as_interface_alt_0_desc
= {
133 .bLength
= USB_DT_INTERFACE_SIZE
,
134 .bDescriptorType
= USB_DT_INTERFACE
,
135 .bAlternateSetting
= 0,
137 .bInterfaceClass
= USB_CLASS_AUDIO
,
138 .bInterfaceSubClass
= USB_SUBCLASS_AUDIOSTREAMING
,
141 static struct usb_interface_descriptor as_interface_alt_1_desc
= {
142 .bLength
= USB_DT_INTERFACE_SIZE
,
143 .bDescriptorType
= USB_DT_INTERFACE
,
144 .bAlternateSetting
= 1,
146 .bInterfaceClass
= USB_CLASS_AUDIO
,
147 .bInterfaceSubClass
= USB_SUBCLASS_AUDIOSTREAMING
,
150 /* B.4.2 Class-Specific AS Interface Descriptor */
151 static struct usb_as_header_descriptor as_header_desc
= {
152 .bLength
= USB_DT_AS_HEADER_SIZE
,
153 .bDescriptorType
= USB_DT_CS_INTERFACE
,
154 .bDescriptorSubtype
= AS_GENERAL
,
155 .bTerminalLink
= INPUT_TERMINAL_ID
,
157 .wFormatTag
= USB_AS_AUDIO_FORMAT_TYPE_I_PCM
,
160 DECLARE_USB_AS_FORMAT_TYPE_I_DISCRETE_DESC(1);
162 static struct usb_as_formate_type_i_discrete_descriptor_1 as_type_i_desc
= {
163 .bLength
= USB_AS_FORMAT_TYPE_I_DISCRETE_DESC_SIZE(1),
164 .bDescriptorType
= USB_DT_CS_INTERFACE
,
165 .bDescriptorSubtype
= FORMAT_TYPE
,
166 .bFormatType
= USB_AS_FORMAT_TYPE_I
,
168 .bBitResolution
= 16,
172 /* Standard ISO OUT Endpoint Descriptor */
173 static struct usb_endpoint_descriptor as_out_ep_desc __initdata
= {
174 .bLength
= USB_DT_ENDPOINT_AUDIO_SIZE
,
175 .bDescriptorType
= USB_DT_ENDPOINT
,
176 .bEndpointAddress
= USB_DIR_OUT
,
177 .bmAttributes
= USB_AS_ENDPOINT_ADAPTIVE
178 | USB_ENDPOINT_XFER_ISOC
,
179 .wMaxPacketSize
= __constant_cpu_to_le16(OUT_EP_MAX_PACKET_SIZE
),
183 /* Class-specific AS ISO OUT Endpoint Descriptor */
184 static struct usb_as_iso_endpoint_descriptor as_iso_out_desc __initdata
= {
185 .bLength
= USB_AS_ISO_ENDPOINT_DESC_SIZE
,
186 .bDescriptorType
= USB_DT_CS_ENDPOINT
,
187 .bDescriptorSubtype
= EP_GENERAL
,
189 .bLockDelayUnits
= 1,
190 .wLockDelay
= __constant_cpu_to_le16(1),
193 static struct usb_descriptor_header
*f_audio_desc
[] __initdata
= {
194 (struct usb_descriptor_header
*)&ac_interface_desc
,
195 (struct usb_descriptor_header
*)&ac_header_desc
,
197 (struct usb_descriptor_header
*)&input_terminal_desc
,
198 (struct usb_descriptor_header
*)&output_terminal_desc
,
199 (struct usb_descriptor_header
*)&feature_unit_desc
,
201 (struct usb_descriptor_header
*)&as_interface_alt_0_desc
,
202 (struct usb_descriptor_header
*)&as_interface_alt_1_desc
,
203 (struct usb_descriptor_header
*)&as_header_desc
,
205 (struct usb_descriptor_header
*)&as_type_i_desc
,
207 (struct usb_descriptor_header
*)&as_out_ep_desc
,
208 (struct usb_descriptor_header
*)&as_iso_out_desc
,
212 /* string IDs are assigned dynamically */
214 #define STRING_MANUFACTURER_IDX 0
215 #define STRING_PRODUCT_IDX 1
217 static char manufacturer
[50];
219 static struct usb_string strings_dev
[] = {
220 [STRING_MANUFACTURER_IDX
].s
= manufacturer
,
221 [STRING_PRODUCT_IDX
].s
= DRIVER_DESC
,
222 { } /* end of list */
225 static struct usb_gadget_strings stringtab_dev
= {
226 .language
= 0x0409, /* en-us */
227 .strings
= strings_dev
,
230 static struct usb_gadget_strings
*audio_strings
[] = {
236 * This function is an ALSA sound card following USB Audio Class Spec 1.0.
239 /*-------------------------------------------------------------------------*/
243 struct list_head list
;
246 static struct f_audio_buf
*f_audio_buffer_alloc(int buf_size
)
248 struct f_audio_buf
*copy_buf
;
250 copy_buf
= kzalloc(sizeof *copy_buf
, GFP_ATOMIC
);
252 return (struct f_audio_buf
*)-ENOMEM
;
254 copy_buf
->buf
= kzalloc(buf_size
, GFP_ATOMIC
);
255 if (!copy_buf
->buf
) {
257 return (struct f_audio_buf
*)-ENOMEM
;
263 static void f_audio_buffer_free(struct f_audio_buf
*audio_buf
)
265 kfree(audio_buf
->buf
);
268 /*-------------------------------------------------------------------------*/
273 /* endpoints handle full and/or high speeds */
274 struct usb_ep
*out_ep
;
275 struct usb_endpoint_descriptor
*out_desc
;
278 struct f_audio_buf
*copy_buf
;
279 struct work_struct playback_work
;
280 struct list_head play_queue
;
282 /* Control Set command */
285 struct usb_audio_control
*set_con
;
288 static inline struct f_audio
*func_to_audio(struct usb_function
*f
)
290 return container_of(f
, struct f_audio
, card
.func
);
293 /*-------------------------------------------------------------------------*/
295 static void f_audio_playback_work(struct work_struct
*data
)
297 struct f_audio
*audio
= container_of(data
, struct f_audio
,
299 struct f_audio_buf
*play_buf
;
301 spin_lock_irq(&audio
->lock
);
302 if (list_empty(&audio
->play_queue
)) {
303 spin_unlock_irq(&audio
->lock
);
306 play_buf
= list_first_entry(&audio
->play_queue
,
307 struct f_audio_buf
, list
);
308 list_del(&play_buf
->list
);
309 spin_unlock_irq(&audio
->lock
);
311 u_audio_playback(&audio
->card
, play_buf
->buf
, play_buf
->actual
);
312 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
;
327 /* Copy buffer is full, add it to the play_queue */
328 if (audio_buf_size
- copy_buf
->actual
< req
->actual
) {
329 list_add_tail(©_buf
->list
, &audio
->play_queue
);
330 schedule_work(&audio
->playback_work
);
331 copy_buf
= f_audio_buffer_alloc(audio_buf_size
);
336 memcpy(copy_buf
->buf
+ copy_buf
->actual
, req
->buf
, req
->actual
);
337 copy_buf
->actual
+= req
->actual
;
338 audio
->copy_buf
= copy_buf
;
340 err
= usb_ep_queue(ep
, req
, GFP_ATOMIC
);
342 ERROR(cdev
, "%s queue req: %d\n", ep
->name
, err
);
348 static void f_audio_complete(struct usb_ep
*ep
, struct usb_request
*req
)
350 struct f_audio
*audio
= req
->context
;
351 int status
= req
->status
;
353 struct usb_ep
*out_ep
= audio
->out_ep
;
357 case 0: /* normal completion? */
359 f_audio_out_ep_complete(ep
, req
);
360 else if (audio
->set_con
) {
361 memcpy(&data
, req
->buf
, req
->length
);
362 audio
->set_con
->set(audio
->set_con
, audio
->set_cmd
,
364 audio
->set_con
= NULL
;
372 static int audio_set_intf_req(struct usb_function
*f
,
373 const struct usb_ctrlrequest
*ctrl
)
375 struct f_audio
*audio
= func_to_audio(f
);
376 struct usb_composite_dev
*cdev
= f
->config
->cdev
;
377 struct usb_request
*req
= cdev
->req
;
378 u8 id
= ((le16_to_cpu(ctrl
->wIndex
) >> 8) & 0xFF);
379 u16 len
= le16_to_cpu(ctrl
->wLength
);
380 u16 w_value
= le16_to_cpu(ctrl
->wValue
);
381 u8 con_sel
= (w_value
>> 8) & 0xFF;
382 u8 cmd
= (ctrl
->bRequest
& 0x0F);
383 struct usb_audio_control_selector
*cs
;
384 struct usb_audio_control
*con
;
386 DBG(cdev
, "bRequest 0x%x, w_value 0x%04x, len %d, entity %d\n",
387 ctrl
->bRequest
, w_value
, len
, id
);
389 list_for_each_entry(cs
, &audio
->cs
, list
) {
391 list_for_each_entry(con
, &cs
->control
, list
) {
392 if (con
->type
== con_sel
) {
393 audio
->set_con
= con
;
401 audio
->set_cmd
= cmd
;
402 req
->context
= audio
;
403 req
->complete
= f_audio_complete
;
408 static int audio_get_intf_req(struct usb_function
*f
,
409 const struct usb_ctrlrequest
*ctrl
)
411 struct f_audio
*audio
= func_to_audio(f
);
412 struct usb_composite_dev
*cdev
= f
->config
->cdev
;
413 struct usb_request
*req
= cdev
->req
;
414 int value
= -EOPNOTSUPP
;
415 u8 id
= ((le16_to_cpu(ctrl
->wIndex
) >> 8) & 0xFF);
416 u16 len
= le16_to_cpu(ctrl
->wLength
);
417 u16 w_value
= le16_to_cpu(ctrl
->wValue
);
418 u8 con_sel
= (w_value
>> 8) & 0xFF;
419 u8 cmd
= (ctrl
->bRequest
& 0x0F);
420 struct usb_audio_control_selector
*cs
;
421 struct usb_audio_control
*con
;
423 DBG(cdev
, "bRequest 0x%x, w_value 0x%04x, len %d, entity %d\n",
424 ctrl
->bRequest
, w_value
, len
, id
);
426 list_for_each_entry(cs
, &audio
->cs
, list
) {
428 list_for_each_entry(con
, &cs
->control
, list
) {
429 if (con
->type
== con_sel
&& con
->get
) {
430 value
= con
->get(con
, cmd
);
438 req
->context
= audio
;
439 req
->complete
= f_audio_complete
;
440 memcpy(req
->buf
, &value
, len
);
446 f_audio_setup(struct usb_function
*f
, const struct usb_ctrlrequest
*ctrl
)
448 struct usb_composite_dev
*cdev
= f
->config
->cdev
;
449 struct usb_request
*req
= cdev
->req
;
450 int value
= -EOPNOTSUPP
;
451 u16 w_index
= le16_to_cpu(ctrl
->wIndex
);
452 u16 w_value
= le16_to_cpu(ctrl
->wValue
);
453 u16 w_length
= le16_to_cpu(ctrl
->wLength
);
455 /* composite driver infrastructure handles everything except
456 * Audio class messages; interface activation uses set_alt().
458 switch (ctrl
->bRequestType
) {
459 case USB_AUDIO_SET_INTF
:
460 value
= audio_set_intf_req(f
, ctrl
);
463 case USB_AUDIO_GET_INTF
:
464 value
= audio_get_intf_req(f
, ctrl
);
468 ERROR(cdev
, "invalid control req%02x.%02x v%04x i%04x l%d\n",
469 ctrl
->bRequestType
, ctrl
->bRequest
,
470 w_value
, w_index
, w_length
);
473 /* respond with data transfer or status phase? */
475 DBG(cdev
, "audio req%02x.%02x v%04x i%04x l%d\n",
476 ctrl
->bRequestType
, ctrl
->bRequest
,
477 w_value
, w_index
, w_length
);
480 value
= usb_ep_queue(cdev
->gadget
->ep0
, req
, GFP_ATOMIC
);
482 ERROR(cdev
, "audio response on err %d\n", value
);
485 /* device either stalls (value < 0) or reports success */
489 static int f_audio_set_alt(struct usb_function
*f
, unsigned intf
, unsigned alt
)
491 struct f_audio
*audio
= func_to_audio(f
);
492 struct usb_composite_dev
*cdev
= f
->config
->cdev
;
493 struct usb_ep
*out_ep
= audio
->out_ep
;
494 struct usb_request
*req
;
497 DBG(cdev
, "intf %d, alt %d\n", intf
, alt
);
501 usb_ep_enable(out_ep
, audio
->out_desc
);
502 out_ep
->driver_data
= audio
;
503 audio
->copy_buf
= f_audio_buffer_alloc(audio_buf_size
);
506 * allocate a bunch of read buffers
507 * and queue them all at once.
509 for (i
= 0; i
< req_count
&& err
== 0; i
++) {
510 req
= usb_ep_alloc_request(out_ep
, GFP_ATOMIC
);
512 req
->buf
= kzalloc(req_buf_size
,
515 req
->length
= req_buf_size
;
516 req
->context
= audio
;
519 err
= usb_ep_queue(out_ep
,
523 "%s queue req: %d\n",
532 struct f_audio_buf
*copy_buf
= audio
->copy_buf
;
534 list_add_tail(©_buf
->list
,
536 schedule_work(&audio
->playback_work
);
544 static void f_audio_disable(struct usb_function
*f
)
549 /*-------------------------------------------------------------------------*/
551 static void f_audio_build_desc(struct f_audio
*audio
)
553 struct gaudio
*card
= &audio
->card
;
557 /* Set channel numbers */
558 input_terminal_desc
.bNrChannels
= u_audio_get_playback_channels(card
);
559 as_type_i_desc
.bNrChannels
= u_audio_get_playback_channels(card
);
561 /* Set sample rates */
562 rate
= u_audio_get_playback_rate(card
);
563 sam_freq
= as_type_i_desc
.tSamFreq
[0];
564 memcpy(sam_freq
, &rate
, 3);
566 /* Todo: Set Sample bits and other parameters */
571 /* audio function driver setup/binding */
573 f_audio_bind(struct usb_configuration
*c
, struct usb_function
*f
)
575 struct usb_composite_dev
*cdev
= c
->cdev
;
576 struct f_audio
*audio
= func_to_audio(f
);
580 f_audio_build_desc(audio
);
582 /* allocate instance-specific interface IDs, and patch descriptors */
583 status
= usb_interface_id(c
, f
);
586 ac_interface_desc
.bInterfaceNumber
= status
;
588 status
= usb_interface_id(c
, f
);
591 as_interface_alt_0_desc
.bInterfaceNumber
= status
;
592 as_interface_alt_1_desc
.bInterfaceNumber
= status
;
596 /* allocate instance-specific endpoints */
597 ep
= usb_ep_autoconfig(cdev
->gadget
, &as_out_ep_desc
);
601 ep
->driver_data
= cdev
; /* claim */
605 /* supcard all relevant hardware speeds... we expect that when
606 * hardware is dual speed, all bulk-capable endpoints work at
610 /* copy descriptors, and track endpoint copies */
611 if (gadget_is_dualspeed(c
->cdev
->gadget
)) {
613 f
->hs_descriptors
= usb_copy_descriptors(f_audio_desc
);
615 f
->descriptors
= usb_copy_descriptors(f_audio_desc
);
625 f_audio_unbind(struct usb_configuration
*c
, struct usb_function
*f
)
627 struct f_audio
*audio
= func_to_audio(f
);
629 usb_free_descriptors(f
->descriptors
);
633 /*-------------------------------------------------------------------------*/
635 /* Todo: add more control selecotor dynamically */
636 int __init
control_selector_init(struct f_audio
*audio
)
638 INIT_LIST_HEAD(&audio
->cs
);
639 list_add(&feature_unit
.list
, &audio
->cs
);
641 INIT_LIST_HEAD(&feature_unit
.control
);
642 list_add(&mute_control
.list
, &feature_unit
.control
);
643 list_add(&volume_control
.list
, &feature_unit
.control
);
645 volume_control
.data
[_CUR
] = 0xffc0;
646 volume_control
.data
[_MIN
] = 0xe3a0;
647 volume_control
.data
[_MAX
] = 0xfff0;
648 volume_control
.data
[_RES
] = 0x0030;
654 * audio_bind_config - add USB audio fucntion to a configuration
655 * @c: the configuration to supcard the USB audio function
656 * Context: single threaded during gadget setup
658 * Returns zero on success, else negative errno.
660 int __init
audio_bind_config(struct usb_configuration
*c
)
662 struct f_audio
*audio
;
665 /* allocate and initialize one new instance */
666 audio
= kzalloc(sizeof *audio
, GFP_KERNEL
);
670 audio
->card
.func
.name
= "g_audio";
671 audio
->card
.gadget
= c
->cdev
->gadget
;
673 INIT_LIST_HEAD(&audio
->play_queue
);
674 spin_lock_init(&audio
->lock
);
676 /* set up ASLA audio devices */
677 status
= gaudio_setup(&audio
->card
);
681 audio
->card
.func
.strings
= audio_strings
;
682 audio
->card
.func
.bind
= f_audio_bind
;
683 audio
->card
.func
.unbind
= f_audio_unbind
;
684 audio
->card
.func
.set_alt
= f_audio_set_alt
;
685 audio
->card
.func
.setup
= f_audio_setup
;
686 audio
->card
.func
.disable
= f_audio_disable
;
687 audio
->out_desc
= &as_out_ep_desc
;
689 control_selector_init(audio
);
691 INIT_WORK(&audio
->playback_work
, f_audio_playback_work
);
693 status
= usb_add_function(c
, &audio
->card
.func
);
697 INFO(c
->cdev
, "audio_buf_size %d, req_buf_size %d, req_count %d\n",
698 audio_buf_size
, req_buf_size
, req_count
);
703 gaudio_cleanup(&audio
->card
);