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/device.h>
15 #include <linux/atomic.h>
19 #define OUT_EP_MAX_PACKET_SIZE 200
20 static int req_buf_size = OUT_EP_MAX_PACKET_SIZE;
21 module_param(req_buf_size, int, S_IRUGO);
22 MODULE_PARM_DESC(req_buf_size, "ISO OUT endpoint request buffer size");
24 static int req_count = 256;
25 module_param(req_count, int, S_IRUGO);
26 MODULE_PARM_DESC(req_count, "ISO OUT endpoint request count");
28 static int audio_buf_size = 48000;
29 module_param(audio_buf_size, int, S_IRUGO);
30 MODULE_PARM_DESC(audio_buf_size, "Audio buffer size");
32 static int generic_set_cmd(struct usb_audio_control *con, u8 cmd, int value);
33 static int generic_get_cmd(struct usb_audio_control *con, u8 cmd);
36 * DESCRIPTORS ... most are static, but strings and full
37 * configuration descriptors are built on demand.
41 * We have two interfaces- AudioControl and AudioStreaming
42 * TODO: only supcard playback currently
44 #define F_AUDIO_AC_INTERFACE 0
45 #define F_AUDIO_AS_INTERFACE 1
46 #define F_AUDIO_NUM_INTERFACES 2
48 /* B.3.1 Standard AC Interface Descriptor */
49 static struct usb_interface_descriptor ac_interface_desc __initdata = {
50 .bLength = USB_DT_INTERFACE_SIZE,
51 .bDescriptorType = USB_DT_INTERFACE,
53 .bInterfaceClass = USB_CLASS_AUDIO,
54 .bInterfaceSubClass = USB_SUBCLASS_AUDIOCONTROL,
57 DECLARE_UAC_AC_HEADER_DESCRIPTOR(2);
59 #define UAC_DT_AC_HEADER_LENGTH UAC_DT_AC_HEADER_SIZE(F_AUDIO_NUM_INTERFACES)
60 /* 1 input terminal, 1 output terminal and 1 feature unit */
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))
63 /* B.3.2 Class-Specific AC Interface Descriptor */
64 static struct uac1_ac_header_descriptor_2 ac_header_desc = {
65 .bLength = UAC_DT_AC_HEADER_LENGTH,
66 .bDescriptorType = USB_DT_CS_INTERFACE,
67 .bDescriptorSubtype = UAC_HEADER,
68 .bcdADC = __constant_cpu_to_le16(0x0100),
69 .wTotalLength = __constant_cpu_to_le16(UAC_DT_TOTAL_LENGTH),
70 .bInCollection = F_AUDIO_NUM_INTERFACES,
72 [0] = F_AUDIO_AC_INTERFACE,
73 [1] = F_AUDIO_AS_INTERFACE,
77 #define INPUT_TERMINAL_ID 1
78 static struct uac_input_terminal_descriptor input_terminal_desc = {
79 .bLength = UAC_DT_INPUT_TERMINAL_SIZE,
80 .bDescriptorType = USB_DT_CS_INTERFACE,
81 .bDescriptorSubtype = UAC_INPUT_TERMINAL,
82 .bTerminalID = INPUT_TERMINAL_ID,
83 .wTerminalType = UAC_TERMINAL_STREAMING,
85 .wChannelConfig = 0x3,
88 DECLARE_UAC_FEATURE_UNIT_DESCRIPTOR(0);
90 #define FEATURE_UNIT_ID 2
91 static struct uac_feature_unit_descriptor_0 feature_unit_desc = {
92 .bLength = UAC_DT_FEATURE_UNIT_SIZE(0),
93 .bDescriptorType = USB_DT_CS_INTERFACE,
94 .bDescriptorSubtype = UAC_FEATURE_UNIT,
95 .bUnitID = FEATURE_UNIT_ID,
96 .bSourceID = INPUT_TERMINAL_ID,
98 .bmaControls[0] = (UAC_FU_MUTE | UAC_FU_VOLUME),
101 static struct usb_audio_control mute_control = {
102 .list = LIST_HEAD_INIT(mute_control.list),
103 .name = "Mute Control",
105 /* Todo: add real Mute control code */
106 .set = generic_set_cmd,
107 .get = generic_get_cmd,
110 static struct usb_audio_control volume_control = {
111 .list = LIST_HEAD_INIT(volume_control.list),
112 .name = "Volume Control",
113 .type = UAC_FU_VOLUME,
114 /* Todo: add real Volume control code */
115 .set = generic_set_cmd,
116 .get = generic_get_cmd,
119 static struct usb_audio_control_selector feature_unit = {
120 .list = LIST_HEAD_INIT(feature_unit.list),
121 .id = FEATURE_UNIT_ID,
122 .name = "Mute & Volume Control",
123 .type = UAC_FEATURE_UNIT,
124 .desc = (struct usb_descriptor_header *)&feature_unit_desc,
127 #define OUTPUT_TERMINAL_ID 3
128 static struct uac1_output_terminal_descriptor output_terminal_desc = {
129 .bLength = UAC_DT_OUTPUT_TERMINAL_SIZE,
130 .bDescriptorType = USB_DT_CS_INTERFACE,
131 .bDescriptorSubtype = UAC_OUTPUT_TERMINAL,
132 .bTerminalID = OUTPUT_TERMINAL_ID,
133 .wTerminalType = UAC_OUTPUT_TERMINAL_SPEAKER,
134 .bAssocTerminal = FEATURE_UNIT_ID,
135 .bSourceID = FEATURE_UNIT_ID,
138 /* B.4.1 Standard AS Interface Descriptor */
139 static struct usb_interface_descriptor as_interface_alt_0_desc = {
140 .bLength = USB_DT_INTERFACE_SIZE,
141 .bDescriptorType = USB_DT_INTERFACE,
142 .bAlternateSetting = 0,
144 .bInterfaceClass = USB_CLASS_AUDIO,
145 .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
148 static struct usb_interface_descriptor as_interface_alt_1_desc = {
149 .bLength = USB_DT_INTERFACE_SIZE,
150 .bDescriptorType = USB_DT_INTERFACE,
151 .bAlternateSetting = 1,
153 .bInterfaceClass = USB_CLASS_AUDIO,
154 .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
157 /* B.4.2 Class-Specific AS Interface Descriptor */
158 static struct uac1_as_header_descriptor as_header_desc = {
159 .bLength = UAC_DT_AS_HEADER_SIZE,
160 .bDescriptorType = USB_DT_CS_INTERFACE,
161 .bDescriptorSubtype = UAC_AS_GENERAL,
162 .bTerminalLink = INPUT_TERMINAL_ID,
164 .wFormatTag = UAC_FORMAT_TYPE_I_PCM,
167 DECLARE_UAC_FORMAT_TYPE_I_DISCRETE_DESC(1);
169 static struct uac_format_type_i_discrete_descriptor_1 as_type_i_desc = {
170 .bLength = UAC_FORMAT_TYPE_I_DISCRETE_DESC_SIZE(1),
171 .bDescriptorType = USB_DT_CS_INTERFACE,
172 .bDescriptorSubtype = UAC_FORMAT_TYPE,
173 .bFormatType = UAC_FORMAT_TYPE_I,
175 .bBitResolution = 16,
179 /* Standard ISO OUT Endpoint Descriptor */
180 static struct usb_endpoint_descriptor as_out_ep_desc = {
181 .bLength = USB_DT_ENDPOINT_AUDIO_SIZE,
182 .bDescriptorType = USB_DT_ENDPOINT,
183 .bEndpointAddress = USB_DIR_OUT,
184 .bmAttributes = USB_ENDPOINT_SYNC_ADAPTIVE
185 | USB_ENDPOINT_XFER_ISOC,
186 .wMaxPacketSize = __constant_cpu_to_le16(OUT_EP_MAX_PACKET_SIZE),
190 /* Class-specific AS ISO OUT Endpoint Descriptor */
191 static struct uac_iso_endpoint_descriptor as_iso_out_desc __initdata = {
192 .bLength = UAC_ISO_ENDPOINT_DESC_SIZE,
193 .bDescriptorType = USB_DT_CS_ENDPOINT,
194 .bDescriptorSubtype = UAC_EP_GENERAL,
196 .bLockDelayUnits = 1,
197 .wLockDelay = __constant_cpu_to_le16(1),
200 static struct usb_descriptor_header *f_audio_desc[] __initdata = {
201 (struct usb_descriptor_header *)&ac_interface_desc,
202 (struct usb_descriptor_header *)&ac_header_desc,
204 (struct usb_descriptor_header *)&input_terminal_desc,
205 (struct usb_descriptor_header *)&output_terminal_desc,
206 (struct usb_descriptor_header *)&feature_unit_desc,
208 (struct usb_descriptor_header *)&as_interface_alt_0_desc,
209 (struct usb_descriptor_header *)&as_interface_alt_1_desc,
210 (struct usb_descriptor_header *)&as_header_desc,
212 (struct usb_descriptor_header *)&as_type_i_desc,
214 (struct usb_descriptor_header *)&as_out_ep_desc,
215 (struct usb_descriptor_header *)&as_iso_out_desc,
220 * This function is an ALSA sound card following USB Audio Class Spec 1.0.
223 /*-------------------------------------------------------------------------*/
227 struct list_head list;
230 static struct f_audio_buf *f_audio_buffer_alloc(int buf_size)
232 struct f_audio_buf *copy_buf;
234 copy_buf = kzalloc(sizeof *copy_buf, GFP_ATOMIC);
236 return ERR_PTR(-ENOMEM);
238 copy_buf->buf = kzalloc(buf_size, GFP_ATOMIC);
239 if (!copy_buf->buf) {
241 return ERR_PTR(-ENOMEM);
247 static void f_audio_buffer_free(struct f_audio_buf *audio_buf)
249 kfree(audio_buf->buf);
252 /*-------------------------------------------------------------------------*/
257 /* endpoints handle full and/or high speeds */
258 struct usb_ep *out_ep;
261 struct f_audio_buf *copy_buf;
262 struct work_struct playback_work;
263 struct list_head play_queue;
265 /* Control Set command */
268 struct usb_audio_control *set_con;
271 static inline struct f_audio *func_to_audio(struct usb_function *f)
273 return container_of(f, struct f_audio, card.func);
276 /*-------------------------------------------------------------------------*/
278 static void f_audio_playback_work(struct work_struct *data)
280 struct f_audio *audio = container_of(data, struct f_audio,
282 struct f_audio_buf *play_buf;
284 spin_lock_irq(&audio->lock);
285 if (list_empty(&audio->play_queue)) {
286 spin_unlock_irq(&audio->lock);
289 play_buf = list_first_entry(&audio->play_queue,
290 struct f_audio_buf, list);
291 list_del(&play_buf->list);
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);
298 static int f_audio_out_ep_complete(struct usb_ep *ep, struct usb_request *req)
300 struct f_audio *audio = req->context;
301 struct usb_composite_dev *cdev = audio->card.func.config->cdev;
302 struct f_audio_buf *copy_buf = audio->copy_buf;
308 /* Copy buffer is full, add it to the play_queue */
309 if (audio_buf_size - copy_buf->actual < req->actual) {
310 list_add_tail(©_buf->list, &audio->play_queue);
311 schedule_work(&audio->playback_work);
312 copy_buf = f_audio_buffer_alloc(audio_buf_size);
313 if (IS_ERR(copy_buf))
317 memcpy(copy_buf->buf + copy_buf->actual, req->buf, req->actual);
318 copy_buf->actual += req->actual;
319 audio->copy_buf = copy_buf;
321 err = usb_ep_queue(ep, req, GFP_ATOMIC);
323 ERROR(cdev, "%s queue req: %d\n", ep->name, err);
329 static void f_audio_complete(struct usb_ep *ep, struct usb_request *req)
331 struct f_audio *audio = req->context;
332 int status = req->status;
334 struct usb_ep *out_ep = audio->out_ep;
338 case 0: /* normal completion? */
340 f_audio_out_ep_complete(ep, req);
341 else if (audio->set_con) {
342 memcpy(&data, req->buf, req->length);
343 audio->set_con->set(audio->set_con, audio->set_cmd,
345 audio->set_con = NULL;
353 static int audio_set_intf_req(struct usb_function *f,
354 const struct usb_ctrlrequest *ctrl)
356 struct f_audio *audio = func_to_audio(f);
357 struct usb_composite_dev *cdev = f->config->cdev;
358 struct usb_request *req = cdev->req;
359 u8 id = ((le16_to_cpu(ctrl->wIndex) >> 8) & 0xFF);
360 u16 len = le16_to_cpu(ctrl->wLength);
361 u16 w_value = le16_to_cpu(ctrl->wValue);
362 u8 con_sel = (w_value >> 8) & 0xFF;
363 u8 cmd = (ctrl->bRequest & 0x0F);
364 struct usb_audio_control_selector *cs;
365 struct usb_audio_control *con;
367 DBG(cdev, "bRequest 0x%x, w_value 0x%04x, len %d, entity %d\n",
368 ctrl->bRequest, w_value, len, id);
370 list_for_each_entry(cs, &audio->cs, list) {
372 list_for_each_entry(con, &cs->control, list) {
373 if (con->type == con_sel) {
374 audio->set_con = con;
382 audio->set_cmd = cmd;
383 req->context = audio;
384 req->complete = f_audio_complete;
389 static int audio_get_intf_req(struct usb_function *f,
390 const struct usb_ctrlrequest *ctrl)
392 struct f_audio *audio = func_to_audio(f);
393 struct usb_composite_dev *cdev = f->config->cdev;
394 struct usb_request *req = cdev->req;
395 int value = -EOPNOTSUPP;
396 u8 id = ((le16_to_cpu(ctrl->wIndex) >> 8) & 0xFF);
397 u16 len = le16_to_cpu(ctrl->wLength);
398 u16 w_value = le16_to_cpu(ctrl->wValue);
399 u8 con_sel = (w_value >> 8) & 0xFF;
400 u8 cmd = (ctrl->bRequest & 0x0F);
401 struct usb_audio_control_selector *cs;
402 struct usb_audio_control *con;
404 DBG(cdev, "bRequest 0x%x, w_value 0x%04x, len %d, entity %d\n",
405 ctrl->bRequest, w_value, len, id);
407 list_for_each_entry(cs, &audio->cs, list) {
409 list_for_each_entry(con, &cs->control, list) {
410 if (con->type == con_sel && con->get) {
411 value = con->get(con, cmd);
419 req->context = audio;
420 req->complete = f_audio_complete;
421 memcpy(req->buf, &value, len);
426 static int audio_set_endpoint_req(struct usb_function *f,
427 const struct usb_ctrlrequest *ctrl)
429 struct usb_composite_dev *cdev = f->config->cdev;
430 int value = -EOPNOTSUPP;
431 u16 ep = le16_to_cpu(ctrl->wIndex);
432 u16 len = le16_to_cpu(ctrl->wLength);
433 u16 w_value = le16_to_cpu(ctrl->wValue);
435 DBG(cdev, "bRequest 0x%x, w_value 0x%04x, len %d, endpoint %d\n",
436 ctrl->bRequest, w_value, len, ep);
438 switch (ctrl->bRequest) {
462 static int audio_get_endpoint_req(struct usb_function *f,
463 const struct usb_ctrlrequest *ctrl)
465 struct usb_composite_dev *cdev = f->config->cdev;
466 int value = -EOPNOTSUPP;
467 u8 ep = ((le16_to_cpu(ctrl->wIndex) >> 8) & 0xFF);
468 u16 len = le16_to_cpu(ctrl->wLength);
469 u16 w_value = le16_to_cpu(ctrl->wValue);
471 DBG(cdev, "bRequest 0x%x, w_value 0x%04x, len %d, endpoint %d\n",
472 ctrl->bRequest, w_value, len, ep);
474 switch (ctrl->bRequest) {
491 f_audio_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
493 struct usb_composite_dev *cdev = f->config->cdev;
494 struct usb_request *req = cdev->req;
495 int value = -EOPNOTSUPP;
496 u16 w_index = le16_to_cpu(ctrl->wIndex);
497 u16 w_value = le16_to_cpu(ctrl->wValue);
498 u16 w_length = le16_to_cpu(ctrl->wLength);
500 /* composite driver infrastructure handles everything; interface
501 * activation uses set_alt().
503 switch (ctrl->bRequestType) {
504 case USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE:
505 value = audio_set_intf_req(f, ctrl);
508 case USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE:
509 value = audio_get_intf_req(f, ctrl);
512 case USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_ENDPOINT:
513 value = audio_set_endpoint_req(f, ctrl);
516 case USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT:
517 value = audio_get_endpoint_req(f, ctrl);
521 ERROR(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
522 ctrl->bRequestType, ctrl->bRequest,
523 w_value, w_index, w_length);
526 /* respond with data transfer or status phase? */
528 DBG(cdev, "audio req%02x.%02x v%04x i%04x l%d\n",
529 ctrl->bRequestType, ctrl->bRequest,
530 w_value, w_index, w_length);
533 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
535 ERROR(cdev, "audio response on err %d\n", value);
538 /* device either stalls (value < 0) or reports success */
542 static int f_audio_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
544 struct f_audio *audio = func_to_audio(f);
545 struct usb_composite_dev *cdev = f->config->cdev;
546 struct usb_ep *out_ep = audio->out_ep;
547 struct usb_request *req;
550 DBG(cdev, "intf %d, alt %d\n", intf, alt);
554 usb_ep_enable(out_ep);
555 out_ep->driver_data = audio;
556 audio->copy_buf = f_audio_buffer_alloc(audio_buf_size);
557 if (IS_ERR(audio->copy_buf))
561 * allocate a bunch of read buffers
562 * and queue them all at once.
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;
571 req->context = audio;
574 err = usb_ep_queue(out_ep,
578 "%s queue req: %d\n",
587 struct f_audio_buf *copy_buf = audio->copy_buf;
589 list_add_tail(©_buf->list,
591 schedule_work(&audio->playback_work);
599 static void f_audio_disable(struct usb_function *f)
604 /*-------------------------------------------------------------------------*/
606 static void f_audio_build_desc(struct f_audio *audio)
608 struct gaudio *card = &audio->card;
612 /* Set channel numbers */
613 input_terminal_desc.bNrChannels = u_audio_get_playback_channels(card);
614 as_type_i_desc.bNrChannels = u_audio_get_playback_channels(card);
616 /* Set sample rates */
617 rate = u_audio_get_playback_rate(card);
618 sam_freq = as_type_i_desc.tSamFreq[0];
619 memcpy(sam_freq, &rate, 3);
621 /* Todo: Set Sample bits and other parameters */
626 /* audio function driver setup/binding */
628 f_audio_bind(struct usb_configuration *c, struct usb_function *f)
630 struct usb_composite_dev *cdev = c->cdev;
631 struct f_audio *audio = func_to_audio(f);
635 f_audio_build_desc(audio);
637 /* allocate instance-specific interface IDs, and patch descriptors */
638 status = usb_interface_id(c, f);
641 ac_interface_desc.bInterfaceNumber = status;
643 status = usb_interface_id(c, f);
646 as_interface_alt_0_desc.bInterfaceNumber = status;
647 as_interface_alt_1_desc.bInterfaceNumber = status;
651 /* allocate instance-specific endpoints */
652 ep = usb_ep_autoconfig(cdev->gadget, &as_out_ep_desc);
656 audio->out_ep->desc = &as_out_ep_desc;
657 ep->driver_data = cdev; /* claim */
661 /* copy descriptors, and track endpoint copies */
662 f->descriptors = usb_copy_descriptors(f_audio_desc);
665 * support all relevant hardware speeds... we expect that when
666 * hardware is dual speed, all bulk-capable endpoints work at
669 if (gadget_is_dualspeed(c->cdev->gadget)) {
671 f->hs_descriptors = usb_copy_descriptors(f_audio_desc);
682 f_audio_unbind(struct usb_configuration *c, struct usb_function *f)
684 struct f_audio *audio = func_to_audio(f);
686 usb_free_descriptors(f->descriptors);
687 usb_free_descriptors(f->hs_descriptors);
691 /*-------------------------------------------------------------------------*/
693 static int generic_set_cmd(struct usb_audio_control *con, u8 cmd, int value)
695 con->data[cmd] = value;
700 static int generic_get_cmd(struct usb_audio_control *con, u8 cmd)
702 return con->data[cmd];
705 /* Todo: add more control selecotor dynamically */
706 int __init control_selector_init(struct f_audio *audio)
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);
715 volume_control.data[UAC__CUR] = 0xffc0;
716 volume_control.data[UAC__MIN] = 0xe3a0;
717 volume_control.data[UAC__MAX] = 0xfff0;
718 volume_control.data[UAC__RES] = 0x0030;
724 * audio_bind_config - add USB audio function to a configuration
725 * @c: the configuration to supcard the USB audio function
726 * Context: single threaded during gadget setup
728 * Returns zero on success, else negative errno.
730 int __init audio_bind_config(struct usb_configuration *c)
732 struct f_audio *audio;
735 /* allocate and initialize one new instance */
736 audio = kzalloc(sizeof *audio, GFP_KERNEL);
740 audio->card.func.name = "g_audio";
741 audio->card.gadget = c->cdev->gadget;
743 INIT_LIST_HEAD(&audio->play_queue);
744 spin_lock_init(&audio->lock);
746 /* set up ASLA audio devices */
747 status = gaudio_setup(&audio->card);
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;
758 control_selector_init(audio);
760 INIT_WORK(&audio->playback_work, f_audio_playback_work);
762 status = usb_add_function(c, &audio->card.func);
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);