blob: 98899244860ecce46d86bd7592b779e9534afe94 [file] [log] [blame]
Bryan Wuc6994e62009-06-03 09:17:58 -04001/*
2 * audio.c -- Audio gadget driver
3 *
4 * Copyright (C) 2008 Bryan Wu <cooloney@kernel.org>
5 * Copyright (C) 2008 Analog Devices, Inc
6 *
7 * Enter bugs at http://blackfin.uclinux.org/
8 *
9 * Licensed under the GPL-2 or later.
10 */
11
12/* #define VERBOSE_DEBUG */
13
14#include <linux/kernel.h>
15#include <linux/utsname.h>
16
Bryan Wuc6994e62009-06-03 09:17:58 -040017#define DRIVER_DESC "Linux USB Audio Gadget"
Jassi Brar132fcb42012-02-02 22:01:34 +053018#define DRIVER_VERSION "Feb 2, 2012"
Bryan Wuc6994e62009-06-03 09:17:58 -040019
20/*-------------------------------------------------------------------------*/
21
22/*
23 * Kbuild is not very cooperative with respect to linking separately
24 * compiled library objects into one module. So for now we won't use
25 * separate compilation ... ensuring init/exit sections work to shrink
26 * the runtime footprint, and giving us at least some parts of what
27 * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
28 */
29#include "composite.c"
30#include "usbstring.c"
31#include "config.c"
32#include "epautoconf.c"
33
Jassi Brard747a912012-02-02 22:01:12 +053034/* string IDs are assigned dynamically */
35
36#define STRING_MANUFACTURER_IDX 0
37#define STRING_PRODUCT_IDX 1
38
39static char manufacturer[50];
40
41static struct usb_string strings_dev[] = {
42 [STRING_MANUFACTURER_IDX].s = manufacturer,
43 [STRING_PRODUCT_IDX].s = DRIVER_DESC,
44 { } /* end of list */
45};
46
47static struct usb_gadget_strings stringtab_dev = {
48 .language = 0x0409, /* en-us */
49 .strings = strings_dev,
50};
51
52static struct usb_gadget_strings *audio_strings[] = {
53 &stringtab_dev,
54 NULL,
55};
56
Jassi Brar132fcb42012-02-02 22:01:34 +053057#ifdef CONFIG_GADGET_UAC1
58#include "u_uac1.h"
Jassi Brar18b5b3b2012-02-02 21:59:53 +053059#include "u_uac1.c"
60#include "f_uac1.c"
Jassi Brar132fcb42012-02-02 22:01:34 +053061#else
62#include "f_uac2.c"
63#endif
Bryan Wuc6994e62009-06-03 09:17:58 -040064
65/*-------------------------------------------------------------------------*/
66
67/* DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
68 * Instead: allocate your own, using normal USB-IF procedures.
69 */
70
Greg Kroah-Hartman05cbc2d2009-06-23 16:01:06 -070071/* Thanks to Linux Foundation for donating this product ID. */
72#define AUDIO_VENDOR_NUM 0x1d6b /* Linux Foundation */
73#define AUDIO_PRODUCT_NUM 0x0101 /* Linux-USB Audio Gadget */
Bryan Wuc6994e62009-06-03 09:17:58 -040074
75/*-------------------------------------------------------------------------*/
76
77static struct usb_device_descriptor device_desc = {
78 .bLength = sizeof device_desc,
79 .bDescriptorType = USB_DT_DEVICE,
80
81 .bcdUSB = __constant_cpu_to_le16(0x200),
82
Jassi Brar132fcb42012-02-02 22:01:34 +053083#ifdef CONFIG_GADGET_UAC1
Bryan Wuc6994e62009-06-03 09:17:58 -040084 .bDeviceClass = USB_CLASS_PER_INTERFACE,
85 .bDeviceSubClass = 0,
86 .bDeviceProtocol = 0,
Jassi Brar132fcb42012-02-02 22:01:34 +053087#else
88 .bDeviceClass = USB_CLASS_MISC,
89 .bDeviceSubClass = 0x02,
90 .bDeviceProtocol = 0x01,
91#endif
Bryan Wuc6994e62009-06-03 09:17:58 -040092 /* .bMaxPacketSize0 = f(hardware) */
93
94 /* Vendor and product id defaults change according to what configs
95 * we support. (As does bNumConfigurations.) These values can
96 * also be overridden by module parameters.
97 */
98 .idVendor = __constant_cpu_to_le16(AUDIO_VENDOR_NUM),
99 .idProduct = __constant_cpu_to_le16(AUDIO_PRODUCT_NUM),
100 /* .bcdDevice = f(hardware) */
101 /* .iManufacturer = DYNAMIC */
102 /* .iProduct = DYNAMIC */
103 /* NO SERIAL NUMBER */
104 .bNumConfigurations = 1,
105};
106
107static struct usb_otg_descriptor otg_descriptor = {
108 .bLength = sizeof otg_descriptor,
109 .bDescriptorType = USB_DT_OTG,
110
111 /* REVISIT SRP-only hardware is possible, although
112 * it would not be called "OTG" ...
113 */
114 .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
115};
116
117static const struct usb_descriptor_header *otg_desc[] = {
118 (struct usb_descriptor_header *) &otg_descriptor,
119 NULL,
120};
121
122/*-------------------------------------------------------------------------*/
123
Michal Nazarewicze12995e2010-08-12 17:43:52 +0200124static int __init audio_do_config(struct usb_configuration *c)
Bryan Wuc6994e62009-06-03 09:17:58 -0400125{
126 /* FIXME alloc iConfiguration string, set it in c->strings */
127
128 if (gadget_is_otg(c->cdev->gadget)) {
129 c->descriptors = otg_desc;
130 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
131 }
132
133 audio_bind_config(c);
134
135 return 0;
136}
137
138static struct usb_configuration audio_config_driver = {
139 .label = DRIVER_DESC,
Bryan Wuc6994e62009-06-03 09:17:58 -0400140 .bConfigurationValue = 1,
141 /* .iConfiguration = DYNAMIC */
142 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
Jassi Brar132fcb42012-02-02 22:01:34 +0530143#ifndef CONFIG_GADGET_UAC1
144 .unbind = uac2_unbind_config,
145#endif
Bryan Wuc6994e62009-06-03 09:17:58 -0400146};
147
148/*-------------------------------------------------------------------------*/
149
Michal Nazarewicze12995e2010-08-12 17:43:52 +0200150static int __init audio_bind(struct usb_composite_dev *cdev)
Bryan Wuc6994e62009-06-03 09:17:58 -0400151{
152 int gcnum;
153 int status;
154
155 gcnum = usb_gadget_controller_number(cdev->gadget);
156 if (gcnum >= 0)
157 device_desc.bcdDevice = cpu_to_le16(0x0300 | gcnum);
158 else {
159 ERROR(cdev, "controller '%s' not recognized; trying %s\n",
160 cdev->gadget->name,
161 audio_config_driver.label);
162 device_desc.bcdDevice =
163 __constant_cpu_to_le16(0x0300 | 0x0099);
164 }
165
166 /* device descriptor strings: manufacturer, product */
167 snprintf(manufacturer, sizeof manufacturer, "%s %s with %s",
168 init_utsname()->sysname, init_utsname()->release,
169 cdev->gadget->name);
170 status = usb_string_id(cdev);
171 if (status < 0)
172 goto fail;
173 strings_dev[STRING_MANUFACTURER_IDX].id = status;
174 device_desc.iManufacturer = status;
175
176 status = usb_string_id(cdev);
177 if (status < 0)
178 goto fail;
179 strings_dev[STRING_PRODUCT_IDX].id = status;
180 device_desc.iProduct = status;
181
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200182 status = usb_add_config(cdev, &audio_config_driver, audio_do_config);
Bryan Wuc6994e62009-06-03 09:17:58 -0400183 if (status < 0)
184 goto fail;
185
186 INFO(cdev, "%s, version: %s\n", DRIVER_DESC, DRIVER_VERSION);
187 return 0;
188
189fail:
190 return status;
191}
192
193static int __exit audio_unbind(struct usb_composite_dev *cdev)
194{
Jassi Brar132fcb42012-02-02 22:01:34 +0530195#ifdef CONFIG_GADGET_UAC1
Cliff Caifeef1d92009-12-09 22:28:39 -0500196 gaudio_cleanup();
Jassi Brar132fcb42012-02-02 22:01:34 +0530197#endif
Bryan Wuc6994e62009-06-03 09:17:58 -0400198 return 0;
199}
200
201static struct usb_composite_driver audio_driver = {
202 .name = "g_audio",
203 .dev = &device_desc,
204 .strings = audio_strings,
Tatyana Brokhman35a0e0b2011-06-29 16:41:49 +0300205 .max_speed = USB_SPEED_HIGH,
Bryan Wuc6994e62009-06-03 09:17:58 -0400206 .unbind = __exit_p(audio_unbind),
207};
208
209static int __init init(void)
210{
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +0200211 return usb_composite_probe(&audio_driver, audio_bind);
Bryan Wuc6994e62009-06-03 09:17:58 -0400212}
213module_init(init);
214
215static void __exit cleanup(void)
216{
217 usb_composite_unregister(&audio_driver);
218}
219module_exit(cleanup);
220
221MODULE_DESCRIPTION(DRIVER_DESC);
222MODULE_AUTHOR("Bryan Wu <cooloney@kernel.org>");
223MODULE_LICENSE("GPL");
224