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