usb: gadget: start with libcomposite
[deliverable/linux.git] / drivers / usb / gadget / audio.c
1 /*
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
17 #define DRIVER_DESC "Linux USB Audio Gadget"
18 #define DRIVER_VERSION "Feb 2, 2012"
19
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 "config.c"
31 #include "epautoconf.c"
32
33 /* string IDs are assigned dynamically */
34
35 #define STRING_MANUFACTURER_IDX 0
36 #define STRING_PRODUCT_IDX 1
37
38 static char manufacturer[50];
39
40 static struct usb_string strings_dev[] = {
41 [STRING_MANUFACTURER_IDX].s = manufacturer,
42 [STRING_PRODUCT_IDX].s = DRIVER_DESC,
43 { } /* end of list */
44 };
45
46 static struct usb_gadget_strings stringtab_dev = {
47 .language = 0x0409, /* en-us */
48 .strings = strings_dev,
49 };
50
51 static struct usb_gadget_strings *audio_strings[] = {
52 &stringtab_dev,
53 NULL,
54 };
55
56 #ifdef CONFIG_GADGET_UAC1
57 #include "u_uac1.h"
58 #include "u_uac1.c"
59 #include "f_uac1.c"
60 #else
61 #include "f_uac2.c"
62 #endif
63
64 /*-------------------------------------------------------------------------*/
65
66 /* DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
67 * Instead: allocate your own, using normal USB-IF procedures.
68 */
69
70 /* Thanks to Linux Foundation for donating this product ID. */
71 #define AUDIO_VENDOR_NUM 0x1d6b /* Linux Foundation */
72 #define AUDIO_PRODUCT_NUM 0x0101 /* Linux-USB Audio Gadget */
73
74 /*-------------------------------------------------------------------------*/
75
76 static struct usb_device_descriptor device_desc = {
77 .bLength = sizeof device_desc,
78 .bDescriptorType = USB_DT_DEVICE,
79
80 .bcdUSB = __constant_cpu_to_le16(0x200),
81
82 #ifdef CONFIG_GADGET_UAC1
83 .bDeviceClass = USB_CLASS_PER_INTERFACE,
84 .bDeviceSubClass = 0,
85 .bDeviceProtocol = 0,
86 #else
87 .bDeviceClass = USB_CLASS_MISC,
88 .bDeviceSubClass = 0x02,
89 .bDeviceProtocol = 0x01,
90 #endif
91 /* .bMaxPacketSize0 = f(hardware) */
92
93 /* Vendor and product id defaults change according to what configs
94 * we support. (As does bNumConfigurations.) These values can
95 * also be overridden by module parameters.
96 */
97 .idVendor = __constant_cpu_to_le16(AUDIO_VENDOR_NUM),
98 .idProduct = __constant_cpu_to_le16(AUDIO_PRODUCT_NUM),
99 /* .bcdDevice = f(hardware) */
100 /* .iManufacturer = DYNAMIC */
101 /* .iProduct = DYNAMIC */
102 /* NO SERIAL NUMBER */
103 .bNumConfigurations = 1,
104 };
105
106 static struct usb_otg_descriptor otg_descriptor = {
107 .bLength = sizeof otg_descriptor,
108 .bDescriptorType = USB_DT_OTG,
109
110 /* REVISIT SRP-only hardware is possible, although
111 * it would not be called "OTG" ...
112 */
113 .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
114 };
115
116 static const struct usb_descriptor_header *otg_desc[] = {
117 (struct usb_descriptor_header *) &otg_descriptor,
118 NULL,
119 };
120
121 /*-------------------------------------------------------------------------*/
122
123 static int __init audio_do_config(struct usb_configuration *c)
124 {
125 /* FIXME alloc iConfiguration string, set it in c->strings */
126
127 if (gadget_is_otg(c->cdev->gadget)) {
128 c->descriptors = otg_desc;
129 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
130 }
131
132 audio_bind_config(c);
133
134 return 0;
135 }
136
137 static struct usb_configuration audio_config_driver = {
138 .label = DRIVER_DESC,
139 .bConfigurationValue = 1,
140 /* .iConfiguration = DYNAMIC */
141 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
142 #ifndef CONFIG_GADGET_UAC1
143 .unbind = uac2_unbind_config,
144 #endif
145 };
146
147 /*-------------------------------------------------------------------------*/
148
149 static int __init audio_bind(struct usb_composite_dev *cdev)
150 {
151 int gcnum;
152 int status;
153
154 gcnum = usb_gadget_controller_number(cdev->gadget);
155 if (gcnum >= 0)
156 device_desc.bcdDevice = cpu_to_le16(0x0300 | gcnum);
157 else {
158 ERROR(cdev, "controller '%s' not recognized; trying %s\n",
159 cdev->gadget->name,
160 audio_config_driver.label);
161 device_desc.bcdDevice =
162 __constant_cpu_to_le16(0x0300 | 0x0099);
163 }
164
165 /* device descriptor strings: manufacturer, product */
166 snprintf(manufacturer, sizeof manufacturer, "%s %s with %s",
167 init_utsname()->sysname, init_utsname()->release,
168 cdev->gadget->name);
169 status = usb_string_id(cdev);
170 if (status < 0)
171 goto fail;
172 strings_dev[STRING_MANUFACTURER_IDX].id = status;
173 device_desc.iManufacturer = status;
174
175 status = usb_string_id(cdev);
176 if (status < 0)
177 goto fail;
178 strings_dev[STRING_PRODUCT_IDX].id = status;
179 device_desc.iProduct = status;
180
181 status = usb_add_config(cdev, &audio_config_driver, audio_do_config);
182 if (status < 0)
183 goto fail;
184
185 INFO(cdev, "%s, version: %s\n", DRIVER_DESC, DRIVER_VERSION);
186 return 0;
187
188 fail:
189 return status;
190 }
191
192 static int __exit audio_unbind(struct usb_composite_dev *cdev)
193 {
194 #ifdef CONFIG_GADGET_UAC1
195 gaudio_cleanup();
196 #endif
197 return 0;
198 }
199
200 static __refdata struct usb_composite_driver audio_driver = {
201 .name = "g_audio",
202 .dev = &device_desc,
203 .strings = audio_strings,
204 .max_speed = USB_SPEED_HIGH,
205 .bind = audio_bind,
206 .unbind = __exit_p(audio_unbind),
207 };
208
209 static int __init init(void)
210 {
211 return usb_composite_probe(&audio_driver);
212 }
213 module_init(init);
214
215 static void __exit cleanup(void)
216 {
217 usb_composite_unregister(&audio_driver);
218 }
219 module_exit(cleanup);
220
221 MODULE_DESCRIPTION(DRIVER_DESC);
222 MODULE_AUTHOR("Bryan Wu <cooloney@kernel.org>");
223 MODULE_LICENSE("GPL");
224
This page took 0.036394 seconds and 6 git commands to generate.