9b2c1c68746bc1328b39bd49b1e3bca6f867cdd3
[deliverable/linux.git] / drivers / usb / gadget / legacy / 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/module.h>
16 #include <linux/usb/composite.h>
17
18 #include "gadget_chips.h"
19 #define DRIVER_DESC "Linux USB Audio Gadget"
20 #define DRIVER_VERSION "Feb 2, 2012"
21
22 USB_GADGET_COMPOSITE_OPTIONS();
23
24 #ifndef CONFIG_GADGET_UAC1
25 #include "u_uac2.h"
26
27 /* Playback(USB-IN) Default Stereo - Fl/Fr */
28 static int p_chmask = UAC2_DEF_PCHMASK;
29 module_param(p_chmask, uint, S_IRUGO);
30 MODULE_PARM_DESC(p_chmask, "Playback Channel Mask");
31
32 /* Playback Default 48 KHz */
33 static int p_srate = UAC2_DEF_PSRATE;
34 module_param(p_srate, uint, S_IRUGO);
35 MODULE_PARM_DESC(p_srate, "Playback Sampling Rate");
36
37 /* Playback Default 16bits/sample */
38 static int p_ssize = UAC2_DEF_PSSIZE;
39 module_param(p_ssize, uint, S_IRUGO);
40 MODULE_PARM_DESC(p_ssize, "Playback Sample Size(bytes)");
41
42 /* Capture(USB-OUT) Default Stereo - Fl/Fr */
43 static int c_chmask = UAC2_DEF_CCHMASK;
44 module_param(c_chmask, uint, S_IRUGO);
45 MODULE_PARM_DESC(c_chmask, "Capture Channel Mask");
46
47 /* Capture Default 64 KHz */
48 static int c_srate = UAC2_DEF_CSRATE;
49 module_param(c_srate, uint, S_IRUGO);
50 MODULE_PARM_DESC(c_srate, "Capture Sampling Rate");
51
52 /* Capture Default 16bits/sample */
53 static int c_ssize = UAC2_DEF_CSSIZE;
54 module_param(c_ssize, uint, S_IRUGO);
55 MODULE_PARM_DESC(c_ssize, "Capture Sample Size(bytes)");
56 #else
57 #include "u_uac1.h"
58
59 static char *fn_play = FILE_PCM_PLAYBACK;
60 module_param(fn_play, charp, S_IRUGO);
61 MODULE_PARM_DESC(fn_play, "Playback PCM device file name");
62
63 static char *fn_cap = FILE_PCM_CAPTURE;
64 module_param(fn_cap, charp, S_IRUGO);
65 MODULE_PARM_DESC(fn_cap, "Capture PCM device file name");
66
67 static char *fn_cntl = FILE_CONTROL;
68 module_param(fn_cntl, charp, S_IRUGO);
69 MODULE_PARM_DESC(fn_cntl, "Control device file name");
70
71 static int req_buf_size = UAC1_OUT_EP_MAX_PACKET_SIZE;
72 module_param(req_buf_size, int, S_IRUGO);
73 MODULE_PARM_DESC(req_buf_size, "ISO OUT endpoint request buffer size");
74
75 static int req_count = UAC1_REQ_COUNT;
76 module_param(req_count, int, S_IRUGO);
77 MODULE_PARM_DESC(req_count, "ISO OUT endpoint request count");
78
79 static int audio_buf_size = UAC1_AUDIO_BUF_SIZE;
80 module_param(audio_buf_size, int, S_IRUGO);
81 MODULE_PARM_DESC(audio_buf_size, "Audio buffer size");
82 #endif
83
84 /* string IDs are assigned dynamically */
85
86 static struct usb_string strings_dev[] = {
87 [USB_GADGET_MANUFACTURER_IDX].s = "",
88 [USB_GADGET_PRODUCT_IDX].s = DRIVER_DESC,
89 [USB_GADGET_SERIAL_IDX].s = "",
90 { } /* end of list */
91 };
92
93 static struct usb_gadget_strings stringtab_dev = {
94 .language = 0x0409, /* en-us */
95 .strings = strings_dev,
96 };
97
98 static struct usb_gadget_strings *audio_strings[] = {
99 &stringtab_dev,
100 NULL,
101 };
102
103 #ifndef CONFIG_GADGET_UAC1
104 static struct usb_function_instance *fi_uac2;
105 static struct usb_function *f_uac2;
106 #else
107 static struct usb_function_instance *fi_uac1;
108 static struct usb_function *f_uac1;
109 #endif
110
111 /*-------------------------------------------------------------------------*/
112
113 /* DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
114 * Instead: allocate your own, using normal USB-IF procedures.
115 */
116
117 /* Thanks to Linux Foundation for donating this product ID. */
118 #define AUDIO_VENDOR_NUM 0x1d6b /* Linux Foundation */
119 #define AUDIO_PRODUCT_NUM 0x0101 /* Linux-USB Audio Gadget */
120
121 /*-------------------------------------------------------------------------*/
122
123 static struct usb_device_descriptor device_desc = {
124 .bLength = sizeof device_desc,
125 .bDescriptorType = USB_DT_DEVICE,
126
127 .bcdUSB = cpu_to_le16(0x200),
128
129 #ifdef CONFIG_GADGET_UAC1
130 .bDeviceClass = USB_CLASS_PER_INTERFACE,
131 .bDeviceSubClass = 0,
132 .bDeviceProtocol = 0,
133 #else
134 .bDeviceClass = USB_CLASS_MISC,
135 .bDeviceSubClass = 0x02,
136 .bDeviceProtocol = 0x01,
137 #endif
138 /* .bMaxPacketSize0 = f(hardware) */
139
140 /* Vendor and product id defaults change according to what configs
141 * we support. (As does bNumConfigurations.) These values can
142 * also be overridden by module parameters.
143 */
144 .idVendor = cpu_to_le16(AUDIO_VENDOR_NUM),
145 .idProduct = cpu_to_le16(AUDIO_PRODUCT_NUM),
146 /* .bcdDevice = f(hardware) */
147 /* .iManufacturer = DYNAMIC */
148 /* .iProduct = DYNAMIC */
149 /* NO SERIAL NUMBER */
150 .bNumConfigurations = 1,
151 };
152
153 static const struct usb_descriptor_header *otg_desc[2];
154
155 /*-------------------------------------------------------------------------*/
156
157 static int audio_do_config(struct usb_configuration *c)
158 {
159 int status;
160
161 /* FIXME alloc iConfiguration string, set it in c->strings */
162
163 if (gadget_is_otg(c->cdev->gadget)) {
164 c->descriptors = otg_desc;
165 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
166 }
167
168 #ifdef CONFIG_GADGET_UAC1
169 f_uac1 = usb_get_function(fi_uac1);
170 if (IS_ERR(f_uac1)) {
171 status = PTR_ERR(f_uac1);
172 return status;
173 }
174
175 status = usb_add_function(c, f_uac1);
176 if (status < 0) {
177 usb_put_function(f_uac1);
178 return status;
179 }
180 #else
181 f_uac2 = usb_get_function(fi_uac2);
182 if (IS_ERR(f_uac2)) {
183 status = PTR_ERR(f_uac2);
184 return status;
185 }
186
187 status = usb_add_function(c, f_uac2);
188 if (status < 0) {
189 usb_put_function(f_uac2);
190 return status;
191 }
192 #endif
193
194 return 0;
195 }
196
197 static struct usb_configuration audio_config_driver = {
198 .label = DRIVER_DESC,
199 .bConfigurationValue = 1,
200 /* .iConfiguration = DYNAMIC */
201 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
202 };
203
204 /*-------------------------------------------------------------------------*/
205
206 static int audio_bind(struct usb_composite_dev *cdev)
207 {
208 #ifndef CONFIG_GADGET_UAC1
209 struct f_uac2_opts *uac2_opts;
210 #else
211 struct f_uac1_opts *uac1_opts;
212 #endif
213 int status;
214
215 #ifndef CONFIG_GADGET_UAC1
216 fi_uac2 = usb_get_function_instance("uac2");
217 if (IS_ERR(fi_uac2))
218 return PTR_ERR(fi_uac2);
219 #else
220 fi_uac1 = usb_get_function_instance("uac1");
221 if (IS_ERR(fi_uac1))
222 return PTR_ERR(fi_uac1);
223 #endif
224
225 #ifndef CONFIG_GADGET_UAC1
226 uac2_opts = container_of(fi_uac2, struct f_uac2_opts, func_inst);
227 uac2_opts->p_chmask = p_chmask;
228 uac2_opts->p_srate = p_srate;
229 uac2_opts->p_ssize = p_ssize;
230 uac2_opts->c_chmask = c_chmask;
231 uac2_opts->c_srate = c_srate;
232 uac2_opts->c_ssize = c_ssize;
233 #else
234 uac1_opts = container_of(fi_uac1, struct f_uac1_opts, func_inst);
235 uac1_opts->fn_play = fn_play;
236 uac1_opts->fn_cap = fn_cap;
237 uac1_opts->fn_cntl = fn_cntl;
238 uac1_opts->req_buf_size = req_buf_size;
239 uac1_opts->req_count = req_count;
240 uac1_opts->audio_buf_size = audio_buf_size;
241 #endif
242
243 status = usb_string_ids_tab(cdev, strings_dev);
244 if (status < 0)
245 goto fail;
246 device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id;
247 device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
248
249 if (gadget_is_otg(cdev->gadget) && !otg_desc[0]) {
250 struct usb_descriptor_header *usb_desc;
251
252 usb_desc = usb_otg_descriptor_alloc(cdev->gadget);
253 if (!usb_desc)
254 goto fail;
255 usb_otg_descriptor_init(cdev->gadget, usb_desc);
256 otg_desc[0] = usb_desc;
257 otg_desc[1] = NULL;
258 }
259
260 status = usb_add_config(cdev, &audio_config_driver, audio_do_config);
261 if (status < 0)
262 goto fail_otg_desc;
263 usb_composite_overwrite_options(cdev, &coverwrite);
264
265 INFO(cdev, "%s, version: %s\n", DRIVER_DESC, DRIVER_VERSION);
266 return 0;
267
268 fail_otg_desc:
269 kfree(otg_desc[0]);
270 otg_desc[0] = NULL;
271 fail:
272 #ifndef CONFIG_GADGET_UAC1
273 usb_put_function_instance(fi_uac2);
274 #else
275 usb_put_function_instance(fi_uac1);
276 #endif
277 return status;
278 }
279
280 static int audio_unbind(struct usb_composite_dev *cdev)
281 {
282 #ifdef CONFIG_GADGET_UAC1
283 if (!IS_ERR_OR_NULL(f_uac1))
284 usb_put_function(f_uac1);
285 if (!IS_ERR_OR_NULL(fi_uac1))
286 usb_put_function_instance(fi_uac1);
287 #else
288 if (!IS_ERR_OR_NULL(f_uac2))
289 usb_put_function(f_uac2);
290 if (!IS_ERR_OR_NULL(fi_uac2))
291 usb_put_function_instance(fi_uac2);
292 #endif
293 kfree(otg_desc[0]);
294 otg_desc[0] = NULL;
295
296 return 0;
297 }
298
299 static struct usb_composite_driver audio_driver = {
300 .name = "g_audio",
301 .dev = &device_desc,
302 .strings = audio_strings,
303 .max_speed = USB_SPEED_HIGH,
304 .bind = audio_bind,
305 .unbind = audio_unbind,
306 };
307
308 module_usb_composite_driver(audio_driver);
309
310 MODULE_DESCRIPTION(DRIVER_DESC);
311 MODULE_AUTHOR("Bryan Wu <cooloney@kernel.org>");
312 MODULE_LICENSE("GPL");
313
This page took 0.03567 seconds and 4 git commands to generate.