GNU Linux-libre 4.14.328-gnu1
[releases.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 #define DRIVER_DESC             "Linux USB Audio Gadget"
19 #define DRIVER_VERSION          "Feb 2, 2012"
20
21 USB_GADGET_COMPOSITE_OPTIONS();
22
23 #ifndef CONFIG_GADGET_UAC1
24 #include "u_uac2.h"
25
26 /* Playback(USB-IN) Default Stereo - Fl/Fr */
27 static int p_chmask = UAC2_DEF_PCHMASK;
28 module_param(p_chmask, uint, S_IRUGO);
29 MODULE_PARM_DESC(p_chmask, "Playback Channel Mask");
30
31 /* Playback Default 48 KHz */
32 static int p_srate = UAC2_DEF_PSRATE;
33 module_param(p_srate, uint, S_IRUGO);
34 MODULE_PARM_DESC(p_srate, "Playback Sampling Rate");
35
36 /* Playback Default 16bits/sample */
37 static int p_ssize = UAC2_DEF_PSSIZE;
38 module_param(p_ssize, uint, S_IRUGO);
39 MODULE_PARM_DESC(p_ssize, "Playback Sample Size(bytes)");
40
41 /* Capture(USB-OUT) Default Stereo - Fl/Fr */
42 static int c_chmask = UAC2_DEF_CCHMASK;
43 module_param(c_chmask, uint, S_IRUGO);
44 MODULE_PARM_DESC(c_chmask, "Capture Channel Mask");
45
46 /* Capture Default 64 KHz */
47 static int c_srate = UAC2_DEF_CSRATE;
48 module_param(c_srate, uint, S_IRUGO);
49 MODULE_PARM_DESC(c_srate, "Capture Sampling Rate");
50
51 /* Capture Default 16bits/sample */
52 static int c_ssize = UAC2_DEF_CSSIZE;
53 module_param(c_ssize, uint, S_IRUGO);
54 MODULE_PARM_DESC(c_ssize, "Capture Sample Size(bytes)");
55 #else
56 #ifndef CONFIG_GADGET_UAC1_LEGACY
57 #include "u_uac1.h"
58
59 /* Playback(USB-IN) Default Stereo - Fl/Fr */
60 static int p_chmask = UAC1_DEF_PCHMASK;
61 module_param(p_chmask, uint, S_IRUGO);
62 MODULE_PARM_DESC(p_chmask, "Playback Channel Mask");
63
64 /* Playback Default 48 KHz */
65 static int p_srate = UAC1_DEF_PSRATE;
66 module_param(p_srate, uint, S_IRUGO);
67 MODULE_PARM_DESC(p_srate, "Playback Sampling Rate");
68
69 /* Playback Default 16bits/sample */
70 static int p_ssize = UAC1_DEF_PSSIZE;
71 module_param(p_ssize, uint, S_IRUGO);
72 MODULE_PARM_DESC(p_ssize, "Playback Sample Size(bytes)");
73
74 /* Capture(USB-OUT) Default Stereo - Fl/Fr */
75 static int c_chmask = UAC1_DEF_CCHMASK;
76 module_param(c_chmask, uint, S_IRUGO);
77 MODULE_PARM_DESC(c_chmask, "Capture Channel Mask");
78
79 /* Capture Default 48 KHz */
80 static int c_srate = UAC1_DEF_CSRATE;
81 module_param(c_srate, uint, S_IRUGO);
82 MODULE_PARM_DESC(c_srate, "Capture Sampling Rate");
83
84 /* Capture Default 16bits/sample */
85 static int c_ssize = UAC1_DEF_CSSIZE;
86 module_param(c_ssize, uint, S_IRUGO);
87 MODULE_PARM_DESC(c_ssize, "Capture Sample Size(bytes)");
88 #else /* CONFIG_GADGET_UAC1_LEGACY */
89 #include "u_uac1_legacy.h"
90
91 static char *fn_play = FILE_PCM_PLAYBACK;
92 module_param(fn_play, charp, S_IRUGO);
93 MODULE_PARM_DESC(fn_play, "Playback PCM device file name");
94
95 static char *fn_cap = FILE_PCM_CAPTURE;
96 module_param(fn_cap, charp, S_IRUGO);
97 MODULE_PARM_DESC(fn_cap, "Capture PCM device file name");
98
99 static char *fn_cntl = FILE_CONTROL;
100 module_param(fn_cntl, charp, S_IRUGO);
101 MODULE_PARM_DESC(fn_cntl, "Control device file name");
102
103 static int req_buf_size = UAC1_OUT_EP_MAX_PACKET_SIZE;
104 module_param(req_buf_size, int, S_IRUGO);
105 MODULE_PARM_DESC(req_buf_size, "ISO OUT endpoint request buffer size");
106
107 static int req_count = UAC1_REQ_COUNT;
108 module_param(req_count, int, S_IRUGO);
109 MODULE_PARM_DESC(req_count, "ISO OUT endpoint request count");
110
111 static int audio_buf_size = UAC1_AUDIO_BUF_SIZE;
112 module_param(audio_buf_size, int, S_IRUGO);
113 MODULE_PARM_DESC(audio_buf_size, "Audio buffer size");
114 #endif /* CONFIG_GADGET_UAC1_LEGACY */
115 #endif
116
117 /* string IDs are assigned dynamically */
118
119 static 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         {  } /* end of list */
124 };
125
126 static struct usb_gadget_strings stringtab_dev = {
127         .language = 0x0409,     /* en-us */
128         .strings = strings_dev,
129 };
130
131 static struct usb_gadget_strings *audio_strings[] = {
132         &stringtab_dev,
133         NULL,
134 };
135
136 #ifndef CONFIG_GADGET_UAC1
137 static struct usb_function_instance *fi_uac2;
138 static struct usb_function *f_uac2;
139 #else
140 static struct usb_function_instance *fi_uac1;
141 static struct usb_function *f_uac1;
142 #endif
143
144 /*-------------------------------------------------------------------------*/
145
146 /* DO NOT REUSE THESE IDs with a protocol-incompatible driver!!  Ever!!
147  * Instead:  allocate your own, using normal USB-IF procedures.
148  */
149
150 /* Thanks to Linux Foundation for donating this product ID. */
151 #define AUDIO_VENDOR_NUM                0x1d6b  /* Linux Foundation */
152 #define AUDIO_PRODUCT_NUM               0x0101  /* Linux-USB Audio Gadget */
153
154 /*-------------------------------------------------------------------------*/
155
156 static struct usb_device_descriptor device_desc = {
157         .bLength =              sizeof device_desc,
158         .bDescriptorType =      USB_DT_DEVICE,
159
160         /* .bcdUSB = DYNAMIC */
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         /* .bMaxPacketSize0 = f(hardware) */
172
173         /* Vendor and product id defaults change according to what configs
174          * we support.  (As does bNumConfigurations.)  These values can
175          * also be overridden by module parameters.
176          */
177         .idVendor =             cpu_to_le16(AUDIO_VENDOR_NUM),
178         .idProduct =            cpu_to_le16(AUDIO_PRODUCT_NUM),
179         /* .bcdDevice = f(hardware) */
180         /* .iManufacturer = DYNAMIC */
181         /* .iProduct = DYNAMIC */
182         /* NO SERIAL NUMBER */
183         .bNumConfigurations =   1,
184 };
185
186 static const struct usb_descriptor_header *otg_desc[2];
187
188 /*-------------------------------------------------------------------------*/
189
190 static int audio_do_config(struct usb_configuration *c)
191 {
192         int status;
193
194         /* FIXME alloc iConfiguration string, set it in c->strings */
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
230 static struct usb_configuration audio_config_driver = {
231         .label                  = DRIVER_DESC,
232         .bConfigurationValue    = 1,
233         /* .iConfiguration = DYNAMIC */
234         .bmAttributes           = USB_CONFIG_ATT_SELFPOWER,
235 };
236
237 /*-------------------------------------------------------------------------*/
238
239 static 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 /* CONFIG_GADGET_UAC1_LEGACY */
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 /* CONFIG_GADGET_UAC1_LEGACY */
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                         status = -ENOMEM;
308                         goto fail;
309                 }
310                 usb_otg_descriptor_init(cdev->gadget, usb_desc);
311                 otg_desc[0] = usb_desc;
312                 otg_desc[1] = NULL;
313         }
314
315         status = usb_add_config(cdev, &audio_config_driver, audio_do_config);
316         if (status < 0)
317                 goto fail_otg_desc;
318         usb_composite_overwrite_options(cdev, &coverwrite);
319
320         INFO(cdev, "%s, version: %s\n", DRIVER_DESC, DRIVER_VERSION);
321         return 0;
322
323 fail_otg_desc:
324         kfree(otg_desc[0]);
325         otg_desc[0] = NULL;
326 fail:
327 #ifndef CONFIG_GADGET_UAC1
328         usb_put_function_instance(fi_uac2);
329 #else
330         usb_put_function_instance(fi_uac1);
331 #endif
332         return status;
333 }
334
335 static int audio_unbind(struct usb_composite_dev *cdev)
336 {
337 #ifdef CONFIG_GADGET_UAC1
338         if (!IS_ERR_OR_NULL(f_uac1))
339                 usb_put_function(f_uac1);
340         if (!IS_ERR_OR_NULL(fi_uac1))
341                 usb_put_function_instance(fi_uac1);
342 #else
343         if (!IS_ERR_OR_NULL(f_uac2))
344                 usb_put_function(f_uac2);
345         if (!IS_ERR_OR_NULL(fi_uac2))
346                 usb_put_function_instance(fi_uac2);
347 #endif
348         kfree(otg_desc[0]);
349         otg_desc[0] = NULL;
350
351         return 0;
352 }
353
354 static struct usb_composite_driver audio_driver = {
355         .name           = "g_audio",
356         .dev            = &device_desc,
357         .strings        = audio_strings,
358         .max_speed      = USB_SPEED_HIGH,
359         .bind           = audio_bind,
360         .unbind         = audio_unbind,
361 };
362
363 module_usb_composite_driver(audio_driver);
364
365 MODULE_DESCRIPTION(DRIVER_DESC);
366 MODULE_AUTHOR("Bryan Wu <cooloney@kernel.org>");
367 MODULE_LICENSE("GPL");
368