1
2
3
4
5
6
7
8
9
10
11
12
13
14#include <linux/kernel.h>
15#include <linux/module.h>
16#include <linux/usb/composite.h>
17
18#define DRIVER_DESC "Linux USB Audio Gadget"
19#define DRIVER_VERSION "Feb 2, 2012"
20
21USB_GADGET_COMPOSITE_OPTIONS();
22
23#ifndef CONFIG_GADGET_UAC1
24#include "u_uac2.h"
25
26
27static int p_chmask = UAC2_DEF_PCHMASK;
28module_param(p_chmask, uint, S_IRUGO);
29MODULE_PARM_DESC(p_chmask, "Playback Channel Mask");
30
31
32static int p_srate = UAC2_DEF_PSRATE;
33module_param(p_srate, uint, S_IRUGO);
34MODULE_PARM_DESC(p_srate, "Playback Sampling Rate");
35
36
37static int p_ssize = UAC2_DEF_PSSIZE;
38module_param(p_ssize, uint, S_IRUGO);
39MODULE_PARM_DESC(p_ssize, "Playback Sample Size(bytes)");
40
41
42static int c_chmask = UAC2_DEF_CCHMASK;
43module_param(c_chmask, uint, S_IRUGO);
44MODULE_PARM_DESC(c_chmask, "Capture Channel Mask");
45
46
47static int c_srate = UAC2_DEF_CSRATE;
48module_param(c_srate, uint, S_IRUGO);
49MODULE_PARM_DESC(c_srate, "Capture Sampling Rate");
50
51
52static int c_ssize = UAC2_DEF_CSSIZE;
53module_param(c_ssize, uint, S_IRUGO);
54MODULE_PARM_DESC(c_ssize, "Capture Sample Size(bytes)");
55#else
56#ifndef CONFIG_GADGET_UAC1_LEGACY
57#include "u_uac1.h"
58
59
60static int p_chmask = UAC1_DEF_PCHMASK;
61module_param(p_chmask, uint, S_IRUGO);
62MODULE_PARM_DESC(p_chmask, "Playback Channel Mask");
63
64
65static int p_srate = UAC1_DEF_PSRATE;
66module_param(p_srate, uint, S_IRUGO);
67MODULE_PARM_DESC(p_srate, "Playback Sampling Rate");
68
69
70static int p_ssize = UAC1_DEF_PSSIZE;
71module_param(p_ssize, uint, S_IRUGO);
72MODULE_PARM_DESC(p_ssize, "Playback Sample Size(bytes)");
73
74
75static int c_chmask = UAC1_DEF_CCHMASK;
76module_param(c_chmask, uint, S_IRUGO);
77MODULE_PARM_DESC(c_chmask, "Capture Channel Mask");
78
79
80static int c_srate = UAC1_DEF_CSRATE;
81module_param(c_srate, uint, S_IRUGO);
82MODULE_PARM_DESC(c_srate, "Capture Sampling Rate");
83
84
85static int c_ssize = UAC1_DEF_CSSIZE;
86module_param(c_ssize, uint, S_IRUGO);
87MODULE_PARM_DESC(c_ssize, "Capture Sample Size(bytes)");
88#else
89#include "u_uac1_legacy.h"
90
91static char *fn_play = FILE_PCM_PLAYBACK;
92module_param(fn_play, charp, S_IRUGO);
93MODULE_PARM_DESC(fn_play, "Playback PCM device file name");
94
95static char *fn_cap = FILE_PCM_CAPTURE;
96module_param(fn_cap, charp, S_IRUGO);
97MODULE_PARM_DESC(fn_cap, "Capture PCM device file name");
98
99static char *fn_cntl = FILE_CONTROL;
100module_param(fn_cntl, charp, S_IRUGO);
101MODULE_PARM_DESC(fn_cntl, "Control device file name");
102
103static int req_buf_size = UAC1_OUT_EP_MAX_PACKET_SIZE;
104module_param(req_buf_size, int, S_IRUGO);
105MODULE_PARM_DESC(req_buf_size, "ISO OUT endpoint request buffer size");
106
107static int req_count = UAC1_REQ_COUNT;
108module_param(req_count, int, S_IRUGO);
109MODULE_PARM_DESC(req_count, "ISO OUT endpoint request count");
110
111static int audio_buf_size = UAC1_AUDIO_BUF_SIZE;
112module_param(audio_buf_size, int, S_IRUGO);
113MODULE_PARM_DESC(audio_buf_size, "Audio buffer size");
114#endif
115#endif
116
117
118
119static struct usb_string strings_dev[] = {
120 [USB_GADGET_MANUFACTURER_IDX].s = "",
121 [USB_GADGET_PRODUCT_IDX].s = DRIVER_DESC,
122 [USB_GADGET_SERIAL_IDX].s = "",
123 { }
124};
125
126static struct usb_gadget_strings stringtab_dev = {
127 .language = 0x0409,
128 .strings = strings_dev,
129};
130
131static struct usb_gadget_strings *audio_strings[] = {
132 &stringtab_dev,
133 NULL,
134};
135
136#ifndef CONFIG_GADGET_UAC1
137static struct usb_function_instance *fi_uac2;
138static struct usb_function *f_uac2;
139#else
140static struct usb_function_instance *fi_uac1;
141static struct usb_function *f_uac1;
142#endif
143
144
145
146
147
148
149
150
151#define AUDIO_VENDOR_NUM 0x1d6b
152#define AUDIO_PRODUCT_NUM 0x0101
153
154
155
156static struct usb_device_descriptor device_desc = {
157 .bLength = sizeof device_desc,
158 .bDescriptorType = USB_DT_DEVICE,
159
160
161
162#ifdef CONFIG_GADGET_UAC1_LEGACY
163 .bDeviceClass = USB_CLASS_PER_INTERFACE,
164 .bDeviceSubClass = 0,
165 .bDeviceProtocol = 0,
166#else
167 .bDeviceClass = USB_CLASS_MISC,
168 .bDeviceSubClass = 0x02,
169 .bDeviceProtocol = 0x01,
170#endif
171
172
173
174
175
176
177 .idVendor = cpu_to_le16(AUDIO_VENDOR_NUM),
178 .idProduct = cpu_to_le16(AUDIO_PRODUCT_NUM),
179
180
181
182
183 .bNumConfigurations = 1,
184};
185
186static const struct usb_descriptor_header *otg_desc[2];
187
188
189
190static int audio_do_config(struct usb_configuration *c)
191{
192 int status;
193
194
195
196 if (gadget_is_otg(c->cdev->gadget)) {
197 c->descriptors = otg_desc;
198 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
199 }
200
201#ifdef CONFIG_GADGET_UAC1
202 f_uac1 = usb_get_function(fi_uac1);
203 if (IS_ERR(f_uac1)) {
204 status = PTR_ERR(f_uac1);
205 return status;
206 }
207
208 status = usb_add_function(c, f_uac1);
209 if (status < 0) {
210 usb_put_function(f_uac1);
211 return status;
212 }
213#else
214 f_uac2 = usb_get_function(fi_uac2);
215 if (IS_ERR(f_uac2)) {
216 status = PTR_ERR(f_uac2);
217 return status;
218 }
219
220 status = usb_add_function(c, f_uac2);
221 if (status < 0) {
222 usb_put_function(f_uac2);
223 return status;
224 }
225#endif
226
227 return 0;
228}
229
230static struct usb_configuration audio_config_driver = {
231 .label = DRIVER_DESC,
232 .bConfigurationValue = 1,
233
234 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
235};
236
237
238
239static int audio_bind(struct usb_composite_dev *cdev)
240{
241#ifndef CONFIG_GADGET_UAC1
242 struct f_uac2_opts *uac2_opts;
243#else
244#ifndef CONFIG_GADGET_UAC1_LEGACY
245 struct f_uac1_opts *uac1_opts;
246#else
247 struct f_uac1_legacy_opts *uac1_opts;
248#endif
249#endif
250 int status;
251
252#ifndef CONFIG_GADGET_UAC1
253 fi_uac2 = usb_get_function_instance("uac2");
254 if (IS_ERR(fi_uac2))
255 return PTR_ERR(fi_uac2);
256#else
257#ifndef CONFIG_GADGET_UAC1_LEGACY
258 fi_uac1 = usb_get_function_instance("uac1");
259#else
260 fi_uac1 = usb_get_function_instance("uac1_legacy");
261#endif
262 if (IS_ERR(fi_uac1))
263 return PTR_ERR(fi_uac1);
264#endif
265
266#ifndef CONFIG_GADGET_UAC1
267 uac2_opts = container_of(fi_uac2, struct f_uac2_opts, func_inst);
268 uac2_opts->p_chmask = p_chmask;
269 uac2_opts->p_srate = p_srate;
270 uac2_opts->p_ssize = p_ssize;
271 uac2_opts->c_chmask = c_chmask;
272 uac2_opts->c_srate = c_srate;
273 uac2_opts->c_ssize = c_ssize;
274 uac2_opts->req_number = UAC2_DEF_REQ_NUM;
275#else
276#ifndef CONFIG_GADGET_UAC1_LEGACY
277 uac1_opts = container_of(fi_uac1, struct f_uac1_opts, func_inst);
278 uac1_opts->p_chmask = p_chmask;
279 uac1_opts->p_srate = p_srate;
280 uac1_opts->p_ssize = p_ssize;
281 uac1_opts->c_chmask = c_chmask;
282 uac1_opts->c_srate = c_srate;
283 uac1_opts->c_ssize = c_ssize;
284 uac1_opts->req_number = UAC1_DEF_REQ_NUM;
285#else
286 uac1_opts = container_of(fi_uac1, struct f_uac1_legacy_opts, func_inst);
287 uac1_opts->fn_play = fn_play;
288 uac1_opts->fn_cap = fn_cap;
289 uac1_opts->fn_cntl = fn_cntl;
290 uac1_opts->req_buf_size = req_buf_size;
291 uac1_opts->req_count = req_count;
292 uac1_opts->audio_buf_size = audio_buf_size;
293#endif
294#endif
295
296 status = usb_string_ids_tab(cdev, strings_dev);
297 if (status < 0)
298 goto fail;
299 device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id;
300 device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
301
302 if (gadget_is_otg(cdev->gadget) && !otg_desc[0]) {
303 struct usb_descriptor_header *usb_desc;
304
305 usb_desc = usb_otg_descriptor_alloc(cdev->gadget);
306 if (!usb_desc)
307 goto fail;
308 usb_otg_descriptor_init(cdev->gadget, usb_desc);
309 otg_desc[0] = usb_desc;
310 otg_desc[1] = NULL;
311 }
312
313 status = usb_add_config(cdev, &audio_config_driver, audio_do_config);
314 if (status < 0)
315 goto fail_otg_desc;
316 usb_composite_overwrite_options(cdev, &coverwrite);
317
318 INFO(cdev, "%s, version: %s\n", DRIVER_DESC, DRIVER_VERSION);
319 return 0;
320
321fail_otg_desc:
322 kfree(otg_desc[0]);
323 otg_desc[0] = NULL;
324fail:
325#ifndef CONFIG_GADGET_UAC1
326 usb_put_function_instance(fi_uac2);
327#else
328 usb_put_function_instance(fi_uac1);
329#endif
330 return status;
331}
332
333static int audio_unbind(struct usb_composite_dev *cdev)
334{
335#ifdef CONFIG_GADGET_UAC1
336 if (!IS_ERR_OR_NULL(f_uac1))
337 usb_put_function(f_uac1);
338 if (!IS_ERR_OR_NULL(fi_uac1))
339 usb_put_function_instance(fi_uac1);
340#else
341 if (!IS_ERR_OR_NULL(f_uac2))
342 usb_put_function(f_uac2);
343 if (!IS_ERR_OR_NULL(fi_uac2))
344 usb_put_function_instance(fi_uac2);
345#endif
346 kfree(otg_desc[0]);
347 otg_desc[0] = NULL;
348
349 return 0;
350}
351
352static struct usb_composite_driver audio_driver = {
353 .name = "g_audio",
354 .dev = &device_desc,
355 .strings = audio_strings,
356 .max_speed = USB_SPEED_HIGH,
357 .bind = audio_bind,
358 .unbind = audio_unbind,
359};
360
361module_usb_composite_driver(audio_driver);
362
363MODULE_DESCRIPTION(DRIVER_DESC);
364MODULE_AUTHOR("Bryan Wu <cooloney@kernel.org>");
365MODULE_LICENSE("GPL");
366
367