blob: e8c966713fc5264ebd3521c9e5cabe48462c7771 [file] [log] [blame]
Bryan Wuc6994e62009-06-03 09:17:58 -04001/*
2 * f_audio.c -- USB Audio class function driver
Anna Perel432367a2012-09-20 10:55:32 +03003 *
4 * Copyright (c) 2012, The Linux Foundation. All rights reserved.
Bryan Wuc6994e62009-06-03 09:17:58 -04005 * Copyright (C) 2008 Bryan Wu <cooloney@kernel.org>
6 * Copyright (C) 2008 Analog Devices, Inc
7 *
8 * Enter bugs at http://blackfin.uclinux.org/
9 *
10 * Licensed under the GPL-2 or later.
11 */
12
Anna Perel432367a2012-09-20 10:55:32 +030013#ifdef pr_fmt
14#undef pr_fmt
15#endif
16#define pr_fmt(fmt) "%s: " fmt, __func__
17
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
Bryan Wuc6994e62009-06-03 09:17:58 -040019#include <linux/kernel.h>
20#include <linux/device.h>
Arun Sharma60063492011-07-26 16:09:06 -070021#include <linux/atomic.h>
Bryan Wuc6994e62009-06-03 09:17:58 -040022
Anna Perel432367a2012-09-20 10:55:32 +030023#include <sound/core.h>
24#include <sound/initval.h>
25
Jassi Brar18b5b3b2012-02-02 21:59:53 +053026#include "u_uac1.h"
Bryan Wuc6994e62009-06-03 09:17:58 -040027
Bryan Wuc6994e62009-06-03 09:17:58 -040028/*
29 * DESCRIPTORS ... most are static, but strings and full
30 * configuration descriptors are built on demand.
31 */
32
33/*
34 * We have two interfaces- AudioControl and AudioStreaming
Bryan Wuc6994e62009-06-03 09:17:58 -040035 */
Anna Perel432367a2012-09-20 10:55:32 +030036#define PLAYBACK_EP_MAX_PACKET_SIZE 32
37static int req_playback_buf_size = PLAYBACK_EP_MAX_PACKET_SIZE;
38module_param(req_playback_buf_size, int, S_IRUGO);
39MODULE_PARM_DESC(req_playback_buf_size, "ISO OUT endpoint (playback) request buffer size");
Bryan Wuc6994e62009-06-03 09:17:58 -040040
Anna Perel432367a2012-09-20 10:55:32 +030041static int req_playback_count = 48;
42module_param(req_playback_count, int, S_IRUGO);
43MODULE_PARM_DESC(req_playback_count, "ISO OUT endpoint (playback) request count");
44
45static int audio_playback_buf_size = 256*32;
46module_param(audio_playback_buf_size, int, S_IRUGO);
47MODULE_PARM_DESC(audio_playback_buf_size, "Audio buffer size");
48
49#define CAPTURE_EP_MAX_PACKET_SIZE 32
50static int req_capture_buf_size = CAPTURE_EP_MAX_PACKET_SIZE;
51module_param(req_capture_buf_size, int, S_IRUGO);
52MODULE_PARM_DESC(req_capture_buf_size, "ISO IN endpoint (capture) request buffer size");
53
54static int req_capture_count = 48;
55module_param(req_capture_count, int, S_IRUGO);
56MODULE_PARM_DESC(req_capture_count, "ISO IN endpoint (capture) request count");
57
58static int audio_capture_buf_size = 256*32;
59module_param(audio_capture_buf_size, int, S_IRUGO);
60MODULE_PARM_DESC(audio_capture_buf_size, "Microphone Audio buffer size");
61
62static int generic_set_cmd(struct usb_audio_control *con, u8 cmd, int value);
63static int generic_get_cmd(struct usb_audio_control *con, u8 cmd);
Bryan Wuc6994e62009-06-03 09:17:58 -040064
Bryan Wuc6994e62009-06-03 09:17:58 -040065
Anna Perel432367a2012-09-20 10:55:32 +030066#define SPEAKER_INPUT_TERMINAL_ID 3
67#define SPEAKER_OUTPUT_TERMINAL_ID 4
68
69#define MICROPHONE_INPUT_TERMINAL_ID 1
70#define MICROPHONE_OUTPUT_TERMINAL_ID 2
71
72
73 /*
74 * We have two interfaces- AudioControl and AudioStreaming
75 */
76
77#define F_AUDIO_INTERFACE_MICROPHONE 2
78#define F_AUDIO_INTERFACE_SPEAKER 3
79#define F_AUDIO_NUM_INTERFACES 2
80
81 /* B.3.1 Standard AC Interface Descriptor */
82struct usb_interface_descriptor ac_interface_desc = {
83 .bLength = USB_DT_INTERFACE_SIZE,
84 .bDescriptorType = USB_DT_INTERFACE,
85 .bNumEndpoints = 0,
86 .bInterfaceClass = USB_CLASS_AUDIO,
87 .bInterfaceSubClass = USB_SUBCLASS_AUDIOCONTROL,
Bryan Wuc6994e62009-06-03 09:17:58 -040088};
89
Anna Perel432367a2012-09-20 10:55:32 +030090#define TOTAL_LENGTH ( \
91 UAC_DT_AC_HEADER_SIZE(2) + \
92 UAC_DT_INPUT_TERMINAL_SIZE + \
93 UAC_DT_OUTPUT_TERMINAL_SIZE + \
94 UAC_DT_INPUT_TERMINAL_SIZE + \
95 UAC_DT_OUTPUT_TERMINAL_SIZE \
96 )
Bryan Wuc6994e62009-06-03 09:17:58 -040097
Anna Perel432367a2012-09-20 10:55:32 +030098 /* B.3.2 Class-Specific AC Interface Descriptor */
99struct uac1_ac_header_descriptor_2 ac_header_desc = {
100 .bLength = UAC_DT_AC_HEADER_SIZE(2),
Bryan Wuc6994e62009-06-03 09:17:58 -0400101 .bDescriptorType = USB_DT_CS_INTERFACE,
Anna Perel432367a2012-09-20 10:55:32 +0300102 .bDescriptorSubtype = UAC_HEADER,
103 .bcdADC = __constant_cpu_to_le16(0x0100),
104 .wTotalLength = __constant_cpu_to_le16(TOTAL_LENGTH),
105 .bInCollection = F_AUDIO_NUM_INTERFACES,
106 /*.baInterfaceNr = {
107 [0] = F_AUDIO_INTERFACE_MICROPHONE,
108 [1] = F_AUDIO_INTERFACE_SPEAKER,
109 }
110 */
Bryan Wuc6994e62009-06-03 09:17:58 -0400111};
112
Anna Perel432367a2012-09-20 10:55:32 +0300113/*---------------------------------*/
114
115struct uac_input_terminal_descriptor speaker_input_terminal_desc = {
116 .bLength = UAC_DT_INPUT_TERMINAL_SIZE,
117 .bDescriptorType = USB_DT_CS_INTERFACE,
118 .bDescriptorSubtype = UAC_INPUT_TERMINAL,
119 .bTerminalID = SPEAKER_INPUT_TERMINAL_ID,
120 .wTerminalType = UAC_TERMINAL_STREAMING,
121 .bAssocTerminal = SPEAKER_OUTPUT_TERMINAL_ID,
122 .wChannelConfig = 0x3,
123};
124
125struct uac1_output_terminal_descriptor speaker_output_terminal_desc = {
126 .bLength = UAC_DT_OUTPUT_TERMINAL_SIZE,
127 .bDescriptorType = USB_DT_CS_INTERFACE,
128 .bDescriptorSubtype = UAC_OUTPUT_TERMINAL,
129 .bTerminalID = SPEAKER_OUTPUT_TERMINAL_ID,
130 .wTerminalType = UAC_OUTPUT_TERMINAL_SPEAKER,
131 .bAssocTerminal = SPEAKER_INPUT_TERMINAL_ID,
132 .bSourceID = SPEAKER_INPUT_TERMINAL_ID,
133};
134
135static struct usb_audio_control speaker_mute_control = {
136 .list = LIST_HEAD_INIT(speaker_mute_control.list),
137 .name = "Speaker Mute Control",
Takashi Iwaif07ff972010-06-01 07:42:03 +0200138 .type = UAC_FU_MUTE,
Bryan Wuc6994e62009-06-03 09:17:58 -0400139 /* Todo: add real Mute control code */
140 .set = generic_set_cmd,
141 .get = generic_get_cmd,
142};
143
Anna Perel432367a2012-09-20 10:55:32 +0300144static struct usb_audio_control speaker_volume_control = {
145 .list = LIST_HEAD_INIT(speaker_volume_control.list),
146 .name = "Speaker Volume Control",
Takashi Iwaif07ff972010-06-01 07:42:03 +0200147 .type = UAC_FU_VOLUME,
Bryan Wuc6994e62009-06-03 09:17:58 -0400148 /* Todo: add real Volume control code */
149 .set = generic_set_cmd,
150 .get = generic_get_cmd,
151};
152
Anna Perel432367a2012-09-20 10:55:32 +0300153static struct usb_audio_control_selector speaker_fu_controls = {
154 .list = LIST_HEAD_INIT(speaker_fu_controls.list),
155 .name = "Speaker Function Unit Controls",
Bryan Wuc6994e62009-06-03 09:17:58 -0400156};
157
Anna Perel432367a2012-09-20 10:55:32 +0300158static struct uac_input_terminal_descriptor microphone_input_terminal_desc = {
159 .bLength = UAC_DT_INPUT_TERMINAL_SIZE,
160 .bDescriptorType = USB_DT_CS_INTERFACE,
161 .bDescriptorSubtype = UAC_INPUT_TERMINAL,
162 .bTerminalID = MICROPHONE_INPUT_TERMINAL_ID,
163 .wTerminalType = UAC_INPUT_TERMINAL_MICROPHONE,
164 .bAssocTerminal = MICROPHONE_OUTPUT_TERMINAL_ID,
165 .bNrChannels = 1,
166 .wChannelConfig = 0x3,
167};
168
169static struct
170uac1_output_terminal_descriptor microphone_output_terminal_desc = {
Laurent Pinchart512ad272009-06-21 23:23:05 +0200171 .bLength = UAC_DT_OUTPUT_TERMINAL_SIZE,
Bryan Wuc6994e62009-06-03 09:17:58 -0400172 .bDescriptorType = USB_DT_CS_INTERFACE,
Laurent Pinchart512ad272009-06-21 23:23:05 +0200173 .bDescriptorSubtype = UAC_OUTPUT_TERMINAL,
Anna Perel432367a2012-09-20 10:55:32 +0300174 .bTerminalID = MICROPHONE_OUTPUT_TERMINAL_ID,
175 .wTerminalType = UAC_TERMINAL_STREAMING,
176 .bAssocTerminal = MICROPHONE_INPUT_TERMINAL_ID,
177 .bSourceID = MICROPHONE_INPUT_TERMINAL_ID,
Bryan Wuc6994e62009-06-03 09:17:58 -0400178};
179
Anna Perel432367a2012-09-20 10:55:32 +0300180static struct usb_audio_control microphone_mute_control = {
181 .list = LIST_HEAD_INIT(microphone_mute_control.list),
182 .name = "Microphone Mute Control",
183 .type = UAC_FU_MUTE,
184 /* Todo: add real Mute control code */
185 .set = generic_set_cmd,
186 .get = generic_get_cmd,
Bryan Wuc6994e62009-06-03 09:17:58 -0400187};
188
Anna Perel432367a2012-09-20 10:55:32 +0300189static struct usb_audio_control microphone_volume_control = {
190 .list = LIST_HEAD_INIT(microphone_volume_control.list),
191 .name = "Microphone Volume Control",
192 .type = UAC_FU_VOLUME,
193 /* Todo: add real Volume control code */
194 .set = generic_set_cmd,
195 .get = generic_get_cmd,
Bryan Wuc6994e62009-06-03 09:17:58 -0400196};
197
Anna Perel432367a2012-09-20 10:55:32 +0300198static struct usb_audio_control_selector microphone_fu_controls = {
199 .list = LIST_HEAD_INIT(microphone_fu_controls.list),
200 .name = "Microphone Feature Unit Controls",
Bryan Wuc6994e62009-06-03 09:17:58 -0400201};
202
Anna Perel432367a2012-09-20 10:55:32 +0300203/*---------------------------------*/
Bryan Wuc6994e62009-06-03 09:17:58 -0400204
Anna Perel432367a2012-09-20 10:55:32 +0300205 /* B.4.1 Standard AS Interface Descriptor */
206static struct usb_interface_descriptor speaker_as_interface_alt_0_desc = {
207 .bLength = USB_DT_INTERFACE_SIZE,
208 .bDescriptorType = USB_DT_INTERFACE,
209 .bAlternateSetting = 0,
210 .bNumEndpoints = 0,
211 .bInterfaceClass = USB_CLASS_AUDIO,
212 .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
Bryan Wuc6994e62009-06-03 09:17:58 -0400213};
214
Anna Perel432367a2012-09-20 10:55:32 +0300215static struct usb_interface_descriptor speaker_as_interface_alt_1_desc = {
216 .bLength = USB_DT_INTERFACE_SIZE,
217 .bDescriptorType = USB_DT_INTERFACE,
218 .bAlternateSetting = 1,
219 .bNumEndpoints = 1,
220 .bInterfaceClass = USB_CLASS_AUDIO,
221 .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
222};
223
224 /* B.4.2 Class-Specific AS Interface Descriptor */
225static struct uac1_as_header_descriptor speaker_as_header_desc = {
226 .bLength = UAC_DT_AS_HEADER_SIZE,
227 .bDescriptorType = USB_DT_CS_INTERFACE,
228 .bDescriptorSubtype = UAC_AS_GENERAL,
229 .bTerminalLink = SPEAKER_INPUT_TERMINAL_ID,
230 .bDelay = 1,
231 .wFormatTag = UAC_FORMAT_TYPE_I_PCM,
232};
233
234static struct uac_format_type_i_discrete_descriptor_1 speaker_as_type_i_desc = {
235 .bLength = UAC_FORMAT_TYPE_I_DISCRETE_DESC_SIZE(1),
236 .bDescriptorType = USB_DT_CS_INTERFACE,
237 .bDescriptorSubtype = UAC_FORMAT_TYPE,
238 .bFormatType = UAC_FORMAT_TYPE_I,
239 .bSubframeSize = 2,
240 .bBitResolution = 16,
241 .bSamFreqType = 1,
242};
243
244 /* Standard ISO OUT Endpoint Descriptor */
245static struct usb_endpoint_descriptor speaker_as_ep_out_desc = {
246 .bLength = USB_DT_ENDPOINT_AUDIO_SIZE,
247 .bDescriptorType = USB_DT_ENDPOINT,
248 .bEndpointAddress = USB_DIR_OUT,
249 .bmAttributes = USB_ENDPOINT_SYNC_ADAPTIVE |
250 USB_ENDPOINT_XFER_ISOC,
251 .wMaxPacketSize =
252 __constant_cpu_to_le16(PLAYBACK_EP_MAX_PACKET_SIZE),
253 .bInterval = 4,
Bryan Wuc6994e62009-06-03 09:17:58 -0400254};
255
256/* Class-specific AS ISO OUT Endpoint Descriptor */
Anna Perel432367a2012-09-20 10:55:32 +0300257static struct uac_iso_endpoint_descriptor speaker_as_iso_out_desc = {
258 .bLength = UAC_ISO_ENDPOINT_DESC_SIZE,
259 .bDescriptorType = USB_DT_CS_ENDPOINT,
260 .bDescriptorSubtype = UAC_EP_GENERAL,
261 .bmAttributes = 1,
262 .bLockDelayUnits = 1,
263 .wLockDelay = __constant_cpu_to_le16(1),
Bryan Wuc6994e62009-06-03 09:17:58 -0400264};
265
Anna Perel432367a2012-09-20 10:55:32 +0300266static struct usb_audio_control speaker_sample_freq_control = {
267 .list = LIST_HEAD_INIT(speaker_sample_freq_control.list),
268 .name = "Speaker Sampling Frequency Control",
269 .type = UAC_EP_CS_ATTR_SAMPLE_RATE,
270 .set = generic_set_cmd,
271 .get = generic_get_cmd,
272};
273
274static struct usb_audio_control_selector speaker_as_iso_out = {
275 .list = LIST_HEAD_INIT(speaker_as_iso_out.list),
276 .name = "Speaker Iso-out Endpoint Control",
277 .type = UAC_EP_GENERAL,
278 .desc = (struct usb_descriptor_header *)&speaker_as_iso_out_desc,
279};
280
281/*---------------------------------*/
282
283/* B.4.1 Standard AS Interface Descriptor */
284static struct usb_interface_descriptor microphone_as_interface_alt_0_desc = {
285 .bLength = USB_DT_INTERFACE_SIZE,
286 .bDescriptorType = USB_DT_INTERFACE,
287 .bAlternateSetting = 0,
288 .bNumEndpoints = 0,
289 .bInterfaceClass = USB_CLASS_AUDIO,
290 .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
291};
292
293static struct usb_interface_descriptor microphone_as_interface_alt_1_desc = {
294 .bLength = USB_DT_INTERFACE_SIZE,
295 .bDescriptorType = USB_DT_INTERFACE,
296 .bAlternateSetting = 1,
297 .bNumEndpoints = 1,
298 .bInterfaceClass = USB_CLASS_AUDIO,
299 .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
300};
301
302/* B.4.2 Class-Specific AS Interface Descriptor */
303static struct uac1_as_header_descriptor microphone_as_header_desc = {
304 .bLength = UAC_DT_AS_HEADER_SIZE,
305 .bDescriptorType = USB_DT_CS_INTERFACE,
306 .bDescriptorSubtype = UAC_AS_GENERAL,
307 .bTerminalLink = MICROPHONE_OUTPUT_TERMINAL_ID,
308 .bDelay = 1,
309 .wFormatTag = UAC_FORMAT_TYPE_I_PCM,
310};
311
312static struct
313uac_format_type_i_discrete_descriptor_1 microphone_as_type_i_desc = {
314 .bLength = UAC_FORMAT_TYPE_I_DISCRETE_DESC_SIZE(1),
315 .bDescriptorType = USB_DT_CS_INTERFACE,
316 .bDescriptorSubtype = UAC_FORMAT_TYPE,
317 .bFormatType = UAC_FORMAT_TYPE_I,
318 .bNrChannels = 1,
319 .bSubframeSize = 2,
320 .bBitResolution = 16,
321 .bSamFreqType = 1,
322};
323
324/* Standard ISO IN Endpoint Descriptor */
325static struct usb_endpoint_descriptor microphone_as_ep_in_desc = {
326 .bLength = USB_DT_ENDPOINT_AUDIO_SIZE,
327 .bDescriptorType = USB_DT_ENDPOINT,
328 .bEndpointAddress = USB_DIR_IN,
329 .bmAttributes =
330 USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC,
331 .wMaxPacketSize =
332 __constant_cpu_to_le16(CAPTURE_EP_MAX_PACKET_SIZE),
333 .bInterval = 4,
334};
335
336 /* Class-specific AS ISO OUT Endpoint Descriptor */
337static struct uac_iso_endpoint_descriptor microphone_as_iso_in_desc = {
338 .bLength = UAC_ISO_ENDPOINT_DESC_SIZE,
339 .bDescriptorType = USB_DT_CS_ENDPOINT,
340 .bDescriptorSubtype = UAC_EP_GENERAL,
341 .bmAttributes = 1,
342 .bLockDelayUnits = 1,
343 .wLockDelay = __constant_cpu_to_le16(1),
344};
345
346static struct usb_audio_control microphone_sample_freq_control = {
347 .list = LIST_HEAD_INIT(microphone_sample_freq_control.list),
348 .name = "Microphone Sampling Frequency Control",
349 .type = UAC_EP_CS_ATTR_SAMPLE_RATE,
350 .set = generic_set_cmd,
351 .get = generic_get_cmd,
352};
353
354static struct usb_audio_control_selector microphone_as_iso_in = {
355 .list = LIST_HEAD_INIT(microphone_as_iso_in.list),
356 .name = "Microphone Iso-IN Endpoint Control",
357 .type = UAC_EP_GENERAL,
358 .desc = (struct usb_descriptor_header *)&microphone_as_iso_in_desc,
359};
360
361/*--------------------------------- */
362
363static struct usb_descriptor_header *f_audio_desc[] = {
Bryan Wuc6994e62009-06-03 09:17:58 -0400364 (struct usb_descriptor_header *)&ac_interface_desc,
365 (struct usb_descriptor_header *)&ac_header_desc,
366
Anna Perel432367a2012-09-20 10:55:32 +0300367 (struct usb_descriptor_header *)&microphone_input_terminal_desc,
368 (struct usb_descriptor_header *)&microphone_output_terminal_desc,
Bryan Wuc6994e62009-06-03 09:17:58 -0400369
Anna Perel432367a2012-09-20 10:55:32 +0300370 (struct usb_descriptor_header *)&speaker_input_terminal_desc,
371 (struct usb_descriptor_header *)&speaker_output_terminal_desc,
Bryan Wuc6994e62009-06-03 09:17:58 -0400372
Anna Perel432367a2012-09-20 10:55:32 +0300373 (struct usb_descriptor_header *)&microphone_as_interface_alt_0_desc,
374 (struct usb_descriptor_header *)&microphone_as_interface_alt_1_desc,
375 (struct usb_descriptor_header *)&microphone_as_header_desc,
376 (struct usb_descriptor_header *)&microphone_as_type_i_desc,
377 (struct usb_descriptor_header *)&microphone_as_ep_in_desc,
378 (struct usb_descriptor_header *)&microphone_as_iso_in_desc,
Bryan Wuc6994e62009-06-03 09:17:58 -0400379
Anna Perel432367a2012-09-20 10:55:32 +0300380 (struct usb_descriptor_header *)&speaker_as_interface_alt_0_desc,
381 (struct usb_descriptor_header *)&speaker_as_interface_alt_1_desc,
382 (struct usb_descriptor_header *)&speaker_as_header_desc,
383 (struct usb_descriptor_header *)&speaker_as_type_i_desc,
384 (struct usb_descriptor_header *)&speaker_as_ep_out_desc,
385 (struct usb_descriptor_header *)&speaker_as_iso_out_desc,
386
387 NULL,
388};
389
390/* string IDs are assigned dynamically */
391
392static struct usb_string audio_string_defs[] = {
393 { } /* end of list */
394};
395
396static struct usb_gadget_strings audio_stringtab_dev = {
397 .language = 0x0409, /* en-us */
398 .strings = audio_string_defs,
399};
400
401static struct usb_gadget_strings *audio_strings[] = {
402 &audio_stringtab_dev,
Bryan Wuc6994e62009-06-03 09:17:58 -0400403 NULL,
404};
405
Bryan Wuc6994e62009-06-03 09:17:58 -0400406/*
407 * This function is an ALSA sound card following USB Audio Class Spec 1.0.
408 */
409
410/*-------------------------------------------------------------------------*/
411struct f_audio_buf {
412 u8 *buf;
413 int actual;
414 struct list_head list;
415};
416
417static struct f_audio_buf *f_audio_buffer_alloc(int buf_size)
418{
Anna Perel432367a2012-09-20 10:55:32 +0300419 struct f_audio_buf *playback_copy_buf;
Bryan Wuc6994e62009-06-03 09:17:58 -0400420
Anna Perel432367a2012-09-20 10:55:32 +0300421 playback_copy_buf = kzalloc(sizeof *playback_copy_buf, GFP_ATOMIC);
422 if (!playback_copy_buf) {
423 pr_err("Failed to allocate playback_copy_buf");
Julia Lawallff3b9682009-12-09 14:23:32 +0100424 return ERR_PTR(-ENOMEM);
Bryan Wuc6994e62009-06-03 09:17:58 -0400425 }
426
Anna Perel432367a2012-09-20 10:55:32 +0300427 playback_copy_buf->buf = kzalloc(buf_size, GFP_ATOMIC);
428 if (!playback_copy_buf->buf) {
429 pr_err("Failed to allocate playback_copy_buf buffer");
430 kfree(playback_copy_buf);
431 return ERR_PTR(-ENOMEM);
432 }
433
434 return playback_copy_buf;
Bryan Wuc6994e62009-06-03 09:17:58 -0400435}
436
437static void f_audio_buffer_free(struct f_audio_buf *audio_buf)
438{
Anna Perel432367a2012-09-20 10:55:32 +0300439 if (audio_buf) {
440 kfree(audio_buf->buf);
441 kfree(audio_buf);
442 audio_buf->buf = NULL;
443 audio_buf = NULL;
444 }
Bryan Wuc6994e62009-06-03 09:17:58 -0400445}
446/*-------------------------------------------------------------------------*/
447
448struct f_audio {
449 struct gaudio card;
450
451 /* endpoints handle full and/or high speeds */
452 struct usb_ep *out_ep;
Anna Perel432367a2012-09-20 10:55:32 +0300453 struct usb_ep *in_ep;
Bryan Wuc6994e62009-06-03 09:17:58 -0400454
Anna Perel432367a2012-09-20 10:55:32 +0300455 spinlock_t playback_lock;
456 struct f_audio_buf *playback_copy_buf;
457 struct work_struct playback_work;
458 struct list_head play_queue;
459
460 spinlock_t capture_lock;
461 struct f_audio_buf *capture_copy_buf;
462 struct work_struct capture_work;
463 struct list_head capture_queue;
464 struct usb_request *capture_req;
Bryan Wuc6994e62009-06-03 09:17:58 -0400465
Bar Weiner33e0dbe2013-03-05 10:30:52 +0200466 u8 alt_intf[F_AUDIO_NUM_INTERFACES];
467
Bryan Wuc6994e62009-06-03 09:17:58 -0400468 /* Control Set command */
Anna Perel432367a2012-09-20 10:55:32 +0300469 struct list_head fu_cs;
470 struct list_head ep_cs;
471 u8 set_cmd;
472 struct usb_audio_control *set_con;
Bryan Wuc6994e62009-06-03 09:17:58 -0400473};
474
475static inline struct f_audio *func_to_audio(struct usb_function *f)
476{
477 return container_of(f, struct f_audio, card.func);
478}
479
480/*-------------------------------------------------------------------------*/
481
482static void f_audio_playback_work(struct work_struct *data)
483{
484 struct f_audio *audio = container_of(data, struct f_audio,
485 playback_work);
486 struct f_audio_buf *play_buf;
Anna Perel432367a2012-09-20 10:55:32 +0300487 unsigned long flags;
488 int res = 0;
Bryan Wuc6994e62009-06-03 09:17:58 -0400489
Anna Perel432367a2012-09-20 10:55:32 +0300490 spin_lock_irqsave(&audio->playback_lock, flags);
Bryan Wuc6994e62009-06-03 09:17:58 -0400491 if (list_empty(&audio->play_queue)) {
Anna Perel432367a2012-09-20 10:55:32 +0300492 pr_err("playback_buf is empty");
493 spin_unlock_irqrestore(&audio->playback_lock, flags);
Bryan Wuc6994e62009-06-03 09:17:58 -0400494 return;
495 }
496 play_buf = list_first_entry(&audio->play_queue,
497 struct f_audio_buf, list);
498 list_del(&play_buf->list);
Anna Perel432367a2012-09-20 10:55:32 +0300499 spin_unlock_irqrestore(&audio->playback_lock, flags);
Bryan Wuc6994e62009-06-03 09:17:58 -0400500
Anna Perel432367a2012-09-20 10:55:32 +0300501 pr_debug("play_buf->actual = %d", play_buf->actual);
502
503 res = u_audio_playback(&audio->card, play_buf->buf, play_buf->actual);
504 if (res)
505 pr_err("copying failed");
506
Bryan Wuc6994e62009-06-03 09:17:58 -0400507 f_audio_buffer_free(play_buf);
Bryan Wuc6994e62009-06-03 09:17:58 -0400508}
509
Anna Perel432367a2012-09-20 10:55:32 +0300510static int
511f_audio_playback_ep_complete(struct usb_ep *ep, struct usb_request *req)
Bryan Wuc6994e62009-06-03 09:17:58 -0400512{
513 struct f_audio *audio = req->context;
Anna Perel432367a2012-09-20 10:55:32 +0300514 struct f_audio_buf *copy_buf = audio->playback_copy_buf;
Bryan Wuc6994e62009-06-03 09:17:58 -0400515 int err;
516
517 if (!copy_buf)
518 return -EINVAL;
519
520 /* Copy buffer is full, add it to the play_queue */
Anna Perel432367a2012-09-20 10:55:32 +0300521 if (audio_playback_buf_size - copy_buf->actual < req->actual) {
522 pr_debug("audio_playback_buf_size %d - copy_buf->actual %d, req->actual %d",
523 audio_playback_buf_size, copy_buf->actual, req->actual);
Bryan Wuc6994e62009-06-03 09:17:58 -0400524 list_add_tail(&copy_buf->list, &audio->play_queue);
525 schedule_work(&audio->playback_work);
Anna Perel432367a2012-09-20 10:55:32 +0300526 copy_buf = f_audio_buffer_alloc(audio_playback_buf_size);
527 if (IS_ERR(copy_buf)) {
528 pr_err("Failed to allocate playback_copy_buf");
Bryan Wuc6994e62009-06-03 09:17:58 -0400529 return -ENOMEM;
Anna Perel432367a2012-09-20 10:55:32 +0300530 }
Bryan Wuc6994e62009-06-03 09:17:58 -0400531 }
532
533 memcpy(copy_buf->buf + copy_buf->actual, req->buf, req->actual);
534 copy_buf->actual += req->actual;
Anna Perel432367a2012-09-20 10:55:32 +0300535 audio->playback_copy_buf = copy_buf;
Bryan Wuc6994e62009-06-03 09:17:58 -0400536
537 err = usb_ep_queue(ep, req, GFP_ATOMIC);
538 if (err)
Anna Perel432367a2012-09-20 10:55:32 +0300539 pr_err("Failed to queue %s req: err - %d\n", ep->name, err);
Bryan Wuc6994e62009-06-03 09:17:58 -0400540
Anna Perel432367a2012-09-20 10:55:32 +0300541 return err;
542}
Bryan Wuc6994e62009-06-03 09:17:58 -0400543
Anna Perel432367a2012-09-20 10:55:32 +0300544static void f_audio_capture_work(struct work_struct *data)
545{
546 struct f_audio *audio =
547 container_of(data, struct f_audio, capture_work);
548 struct f_audio_buf *capture_buf;
549 unsigned long flags;
550 int res = 0;
551
552 capture_buf = f_audio_buffer_alloc(audio_capture_buf_size);
553 if (capture_buf <= 0) {
554 pr_err("%s: buffer alloc failed\n", __func__);
555 return;
556 }
557
558 res = u_audio_capture(&audio->card, capture_buf->buf,
559 audio_capture_buf_size);
560 if (res)
561 pr_err("copying failed");
562
563 pr_debug("Queue capture packet: size %d", audio_capture_buf_size);
564 spin_lock_irqsave(&audio->capture_lock, flags);
565 list_add_tail(&capture_buf->list, &audio->capture_queue);
566 spin_unlock_irqrestore(&audio->capture_lock, flags);
567}
568
569static int
570f_audio_capture_ep_complete(struct usb_ep *ep, struct usb_request *req)
571{
572 struct f_audio *audio = req->context;
573 struct f_audio_buf *copy_buf = audio->capture_copy_buf;
574 unsigned long flags;
575 int err = 0;
576
577 if (copy_buf == 0) {
578 pr_debug("copy_buf == 0");
579 spin_lock_irqsave(&audio->capture_lock, flags);
580 if (list_empty(&audio->capture_queue)) {
581 spin_unlock_irqrestore(&audio->capture_lock, flags);
582 schedule_work(&audio->capture_work);
583 goto done;
584 }
585 copy_buf = list_first_entry(&audio->capture_queue,
586 struct f_audio_buf, list);
587 list_del(&copy_buf->list);
588 audio->capture_copy_buf = copy_buf;
589 spin_unlock_irqrestore(&audio->capture_lock, flags);
590 }
591
592 pr_debug("Copy %d bytes", req->actual);
593 memcpy(req->buf, copy_buf->buf + copy_buf->actual, req->actual);
594 copy_buf->actual += req->actual;
595
596 if (audio_capture_buf_size - copy_buf->actual < req->actual) {
597 f_audio_buffer_free(copy_buf);
598 audio->capture_copy_buf = 0;
599 schedule_work(&audio->capture_work);
600 }
601done:
602 err = usb_ep_queue(ep, req, GFP_ATOMIC);
603 if (err)
604 pr_err("Failed to queue %s req: err - %d\n", ep->name, err);
605
606 return err;
Bryan Wuc6994e62009-06-03 09:17:58 -0400607}
608
609static void f_audio_complete(struct usb_ep *ep, struct usb_request *req)
610{
611 struct f_audio *audio = req->context;
612 int status = req->status;
613 u32 data = 0;
Bryan Wuc6994e62009-06-03 09:17:58 -0400614
615 switch (status) {
Anna Perel432367a2012-09-20 10:55:32 +0300616 case 0: /* normal completion? */
617 if (ep == audio->out_ep) {
618 f_audio_playback_ep_complete(ep, req);
619 } else if (ep == audio->in_ep) {
620 f_audio_capture_ep_complete(ep, req);
621 } else if (audio->set_con) {
Bryan Wuc6994e62009-06-03 09:17:58 -0400622 memcpy(&data, req->buf, req->length);
623 audio->set_con->set(audio->set_con, audio->set_cmd,
624 le16_to_cpu(data));
625 audio->set_con = NULL;
626 }
627 break;
628 default:
Anna Perel432367a2012-09-20 10:55:32 +0300629 pr_err("Failed completion: status %d", status);
Bryan Wuc6994e62009-06-03 09:17:58 -0400630 break;
631 }
632}
633
634static int audio_set_intf_req(struct usb_function *f,
635 const struct usb_ctrlrequest *ctrl)
636{
637 struct f_audio *audio = func_to_audio(f);
638 struct usb_composite_dev *cdev = f->config->cdev;
639 struct usb_request *req = cdev->req;
640 u8 id = ((le16_to_cpu(ctrl->wIndex) >> 8) & 0xFF);
641 u16 len = le16_to_cpu(ctrl->wLength);
642 u16 w_value = le16_to_cpu(ctrl->wValue);
643 u8 con_sel = (w_value >> 8) & 0xFF;
644 u8 cmd = (ctrl->bRequest & 0x0F);
645 struct usb_audio_control_selector *cs;
646 struct usb_audio_control *con;
647
Anna Perel432367a2012-09-20 10:55:32 +0300648 pr_debug("bRequest 0x%x, w_value 0x%04x, len %d, entity %d\n",
Bryan Wuc6994e62009-06-03 09:17:58 -0400649 ctrl->bRequest, w_value, len, id);
650
Anna Perel432367a2012-09-20 10:55:32 +0300651 list_for_each_entry(cs, &audio->fu_cs, list) {
Bryan Wuc6994e62009-06-03 09:17:58 -0400652 if (cs->id == id) {
653 list_for_each_entry(con, &cs->control, list) {
654 if (con->type == con_sel) {
655 audio->set_con = con;
656 break;
657 }
658 }
659 break;
660 }
661 }
662
663 audio->set_cmd = cmd;
664 req->context = audio;
665 req->complete = f_audio_complete;
666
667 return len;
668}
669
670static int audio_get_intf_req(struct usb_function *f,
671 const struct usb_ctrlrequest *ctrl)
672{
673 struct f_audio *audio = func_to_audio(f);
674 struct usb_composite_dev *cdev = f->config->cdev;
675 struct usb_request *req = cdev->req;
676 int value = -EOPNOTSUPP;
677 u8 id = ((le16_to_cpu(ctrl->wIndex) >> 8) & 0xFF);
678 u16 len = le16_to_cpu(ctrl->wLength);
679 u16 w_value = le16_to_cpu(ctrl->wValue);
680 u8 con_sel = (w_value >> 8) & 0xFF;
681 u8 cmd = (ctrl->bRequest & 0x0F);
682 struct usb_audio_control_selector *cs;
683 struct usb_audio_control *con;
684
Anna Perel432367a2012-09-20 10:55:32 +0300685 pr_debug("bRequest 0x%x, w_value 0x%04x, len %d, entity %d\n",
Bryan Wuc6994e62009-06-03 09:17:58 -0400686 ctrl->bRequest, w_value, len, id);
687
Anna Perel432367a2012-09-20 10:55:32 +0300688 list_for_each_entry(cs, &audio->fu_cs, list) {
Bryan Wuc6994e62009-06-03 09:17:58 -0400689 if (cs->id == id) {
690 list_for_each_entry(con, &cs->control, list) {
691 if (con->type == con_sel && con->get) {
692 value = con->get(con, cmd);
693 break;
694 }
695 }
696 break;
697 }
698 }
699
700 req->context = audio;
701 req->complete = f_audio_complete;
702 memcpy(req->buf, &value, len);
703
704 return len;
705}
706
Anna Perel432367a2012-09-20 10:55:32 +0300707static void audio_set_endpoint_complete(struct usb_ep *ep,
708 struct usb_request *req)
709{
710 struct f_audio *audio = req->context;
711 u32 data = 0;
712
713 if (req->status == 0 && audio->set_con) {
714 memcpy(&data, req->buf, req->length);
715 audio->set_con->set(audio->set_con, audio->set_cmd,
716 le32_to_cpu(data));
717 audio->set_con = NULL;
718 }
719}
720
Laurent Pinchart0ad72522009-10-21 00:03:39 +0200721static int audio_set_endpoint_req(struct usb_function *f,
722 const struct usb_ctrlrequest *ctrl)
723{
Anna Perel432367a2012-09-20 10:55:32 +0300724 int value = -EOPNOTSUPP;
725 u16 ep = le16_to_cpu(ctrl->wIndex);
726 u16 len = le16_to_cpu(ctrl->wLength);
727 u16 w_value = le16_to_cpu(ctrl->wValue);
728
729 struct f_audio *audio = func_to_audio(f);
Laurent Pinchart0ad72522009-10-21 00:03:39 +0200730 struct usb_composite_dev *cdev = f->config->cdev;
Anna Perel432367a2012-09-20 10:55:32 +0300731 struct usb_request *req = cdev->req;
732 struct usb_audio_control_selector *cs;
733 struct usb_audio_control *con;
Laurent Pinchart0ad72522009-10-21 00:03:39 +0200734
Anna Perel432367a2012-09-20 10:55:32 +0300735 u8 epnum = ep & ~0x80;
736 u8 con_sel = (w_value >> 8) & 0xFF;
737 u8 cmd = (ctrl->bRequest & 0x0F);
Laurent Pinchart0ad72522009-10-21 00:03:39 +0200738
Anna Perel432367a2012-09-20 10:55:32 +0300739 pr_debug("bRequest 0x%x, w_value 0x%04x, len %d, endp %d, epnum %d\n",
740 ctrl->bRequest, w_value, len, ep, epnum);
Laurent Pinchart0ad72522009-10-21 00:03:39 +0200741
Anna Perel432367a2012-09-20 10:55:32 +0300742 list_for_each_entry(cs, &audio->ep_cs, list) {
743 if (cs->id != epnum)
744 continue;
Laurent Pinchart0ad72522009-10-21 00:03:39 +0200745
Anna Perel432367a2012-09-20 10:55:32 +0300746 list_for_each_entry(con, &cs->control, list) {
747 if (con->type != con_sel)
748 continue;
Laurent Pinchart0ad72522009-10-21 00:03:39 +0200749
Anna Perel432367a2012-09-20 10:55:32 +0300750 switch (cmd) {
751 case UAC__CUR:
752 case UAC__MIN:
753 case UAC__MAX:
754 case UAC__RES:
755 audio->set_con = con;
756 audio->set_cmd = cmd;
757 req->context = audio;
758 req->complete = audio_set_endpoint_complete;
759 value = len;
760 break;
761 case UAC__MEM:
762 break;
763 default:
764 pr_err("Unknown command");
765 break;
766 }
767 break;
768 }
Laurent Pinchart0ad72522009-10-21 00:03:39 +0200769 break;
770 }
771
772 return value;
773}
774
775static int audio_get_endpoint_req(struct usb_function *f,
776 const struct usb_ctrlrequest *ctrl)
777{
Anna Perel432367a2012-09-20 10:55:32 +0300778 struct f_audio *audio = func_to_audio(f);
Laurent Pinchart0ad72522009-10-21 00:03:39 +0200779 struct usb_composite_dev *cdev = f->config->cdev;
Anna Perel432367a2012-09-20 10:55:32 +0300780 struct usb_request *req = cdev->req;
781 struct usb_audio_control_selector *cs;
782 struct usb_audio_control *con;
783 int data;
Laurent Pinchart0ad72522009-10-21 00:03:39 +0200784
Anna Perel432367a2012-09-20 10:55:32 +0300785 int value = -EOPNOTSUPP;
786 u8 ep = (le16_to_cpu(ctrl->wIndex) & 0x7F);
787 u8 epnum = ep & ~0x80;
788 u16 len = le16_to_cpu(ctrl->wLength);
789 u16 w_value = le16_to_cpu(ctrl->wValue);
790 u8 con_sel = (w_value >> 8) & 0xFF;
791 u8 cmd = (ctrl->bRequest & 0x0F);
792
793 pr_debug("bRequest 0x%x, w_value 0x%04x, len %d, ep %d\n",
Laurent Pinchart0ad72522009-10-21 00:03:39 +0200794 ctrl->bRequest, w_value, len, ep);
795
Anna Perel432367a2012-09-20 10:55:32 +0300796 list_for_each_entry(cs, &audio->ep_cs, list) {
797 if (cs->id != epnum)
798 continue;
799
800 list_for_each_entry(con, &cs->control, list) {
801 if (con->type != con_sel)
802 continue;
803
804 switch (cmd) {
805 case UAC__CUR:
806 case UAC__MIN:
807 case UAC__MAX:
808 case UAC__RES:
809 data = cpu_to_le32(generic_get_cmd(con, cmd));
810 memcpy(req->buf, &data, len);
811 value = len;
812 break;
813 case UAC__MEM:
814 break;
815 default:
816 break;
817 }
818 break;
819 }
Laurent Pinchart0ad72522009-10-21 00:03:39 +0200820 break;
821 }
822
823 return value;
824}
825
Bryan Wuc6994e62009-06-03 09:17:58 -0400826static int
827f_audio_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
828{
829 struct usb_composite_dev *cdev = f->config->cdev;
830 struct usb_request *req = cdev->req;
831 int value = -EOPNOTSUPP;
832 u16 w_index = le16_to_cpu(ctrl->wIndex);
833 u16 w_value = le16_to_cpu(ctrl->wValue);
834 u16 w_length = le16_to_cpu(ctrl->wLength);
835
Laurent Pinchart0ad72522009-10-21 00:03:39 +0200836 /* composite driver infrastructure handles everything; interface
837 * activation uses set_alt().
Bryan Wuc6994e62009-06-03 09:17:58 -0400838 */
Anna Perel432367a2012-09-20 10:55:32 +0300839
Bryan Wuc6994e62009-06-03 09:17:58 -0400840 switch (ctrl->bRequestType) {
Laurent Pinchart512ad272009-06-21 23:23:05 +0200841 case USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE:
Anna Perel432367a2012-09-20 10:55:32 +0300842 pr_debug("USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE");
Bryan Wuc6994e62009-06-03 09:17:58 -0400843 value = audio_set_intf_req(f, ctrl);
844 break;
845
Laurent Pinchart512ad272009-06-21 23:23:05 +0200846 case USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE:
Anna Perel432367a2012-09-20 10:55:32 +0300847 pr_debug("USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE");
Bryan Wuc6994e62009-06-03 09:17:58 -0400848 value = audio_get_intf_req(f, ctrl);
849 break;
850
Laurent Pinchart0ad72522009-10-21 00:03:39 +0200851 case USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_ENDPOINT:
Anna Perel432367a2012-09-20 10:55:32 +0300852 pr_debug("USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_ENDPOINT");
Laurent Pinchart0ad72522009-10-21 00:03:39 +0200853 value = audio_set_endpoint_req(f, ctrl);
854 break;
855
856 case USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT:
Anna Perel432367a2012-09-20 10:55:32 +0300857 pr_debug("USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT");
Laurent Pinchart0ad72522009-10-21 00:03:39 +0200858 value = audio_get_endpoint_req(f, ctrl);
859 break;
860
Bryan Wuc6994e62009-06-03 09:17:58 -0400861 default:
Anna Perel432367a2012-09-20 10:55:32 +0300862 pr_err("Unknown control request %02x.%02x v%04x i%04x l%d\n",
Bryan Wuc6994e62009-06-03 09:17:58 -0400863 ctrl->bRequestType, ctrl->bRequest,
864 w_value, w_index, w_length);
Anna Perel432367a2012-09-20 10:55:32 +0300865
Bryan Wuc6994e62009-06-03 09:17:58 -0400866 }
867
868 /* respond with data transfer or status phase? */
869 if (value >= 0) {
Anna Perel432367a2012-09-20 10:55:32 +0300870 pr_debug("audio req %02x.%02x v%04x i%04x l%d\n",
Bryan Wuc6994e62009-06-03 09:17:58 -0400871 ctrl->bRequestType, ctrl->bRequest,
872 w_value, w_index, w_length);
Anna Perel432367a2012-09-20 10:55:32 +0300873 req->zero = 1;
Bryan Wuc6994e62009-06-03 09:17:58 -0400874 req->length = value;
875 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
876 if (value < 0)
Anna Perel432367a2012-09-20 10:55:32 +0300877 pr_err("audio response failed on err %d\n", value);
878 } else {
879 pr_err("STALL\n");
Bryan Wuc6994e62009-06-03 09:17:58 -0400880 }
881
882 /* device either stalls (value < 0) or reports success */
883 return value;
884}
885
Bar Weiner33e0dbe2013-03-05 10:30:52 +0200886static int f_audio_get_alt(struct usb_function *f, unsigned intf)
887{
888 struct f_audio *audio = func_to_audio(f);
889
890 if (intf == ac_header_desc.baInterfaceNr[0])
891 return audio->alt_intf[0];
892 if (intf == ac_header_desc.baInterfaceNr[1])
893 return audio->alt_intf[1];
894
895 return 0;
896}
897
Bryan Wuc6994e62009-06-03 09:17:58 -0400898static int f_audio_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
899{
900 struct f_audio *audio = func_to_audio(f);
Anna Perel432367a2012-09-20 10:55:32 +0300901 struct usb_ep *out_ep = audio->out_ep;
902 struct usb_ep *in_ep = audio->in_ep;
903 struct usb_request *req;
904 unsigned long flags;
Bryan Wuc6994e62009-06-03 09:17:58 -0400905 int i = 0, err = 0;
906
Anna Perel432367a2012-09-20 10:55:32 +0300907 pr_debug("intf %d, alt %d\n", intf, alt);
Bryan Wuc6994e62009-06-03 09:17:58 -0400908
Anna Perel432367a2012-09-20 10:55:32 +0300909 if (intf == ac_header_desc.baInterfaceNr[0]) {
Bryan Wuc6994e62009-06-03 09:17:58 -0400910 if (alt == 1) {
Anna Perel432367a2012-09-20 10:55:32 +0300911 err = usb_ep_enable(in_ep);
912 if (err) {
913 pr_err("Failed to enable capture ep");
914 return err;
915 }
916 in_ep->driver_data = audio;
917 audio->capture_copy_buf = 0;
918
919 /* Allocate a write buffer */
920 req = usb_ep_alloc_request(in_ep, GFP_ATOMIC);
921 if (!req) {
922 pr_err("request allocation failed\n");
Julia Lawallff3b9682009-12-09 14:23:32 +0100923 return -ENOMEM;
Anna Perel432367a2012-09-20 10:55:32 +0300924 }
925 req->buf = kzalloc(req_capture_buf_size,
926 GFP_ATOMIC);
927 if (!req->buf) {
928 pr_err("request buffer allocation failed\n");
929 return -ENOMEM;
930 }
931
932 req->length = req_capture_buf_size;
933 req->context = audio;
934 req->complete = f_audio_complete;
935 audio->capture_req = req;
936 err = usb_ep_queue(in_ep, req, GFP_ATOMIC);
937 if (err)
938 pr_err("Failed to queue %s req: err %d\n",
939 in_ep->name, err);
940 schedule_work(&audio->capture_work);
941 } else {
942 struct f_audio_buf *capture_buf;
943 spin_lock_irqsave(&audio->capture_lock, flags);
944 while (!list_empty(&audio->capture_queue)) {
945 capture_buf =
946 list_first_entry(
947 &audio->capture_queue,
948 struct f_audio_buf,
949 list);
950 list_del(&capture_buf->list);
951 f_audio_buffer_free(capture_buf);
952 }
953 spin_unlock_irqrestore(&audio->capture_lock, flags);
954 }
Bar Weiner33e0dbe2013-03-05 10:30:52 +0200955 audio->alt_intf[0] = alt;
Anna Perel432367a2012-09-20 10:55:32 +0300956 } else if (intf == ac_header_desc.baInterfaceNr[1]) {
957 if (alt == 1) {
958 err = usb_ep_enable(out_ep);
959 if (err) {
960 pr_err("Failed to enable playback ep");
961 return err;
962 }
963 out_ep->driver_data = audio;
964 audio->playback_copy_buf =
965 f_audio_buffer_alloc(audio_playback_buf_size);
966 if (IS_ERR(audio->playback_copy_buf)) {
967 pr_err("Failed to allocate playback_copy_buf");
968 return -ENOMEM;
969 }
Bryan Wuc6994e62009-06-03 09:17:58 -0400970
971 /*
972 * allocate a bunch of read buffers
973 * and queue them all at once.
974 */
Anna Perel432367a2012-09-20 10:55:32 +0300975 for (i = 0; i < req_playback_count && err == 0; i++) {
Bryan Wuc6994e62009-06-03 09:17:58 -0400976 req = usb_ep_alloc_request(out_ep, GFP_ATOMIC);
Anna Perel432367a2012-09-20 10:55:32 +0300977 if (!req) {
978 pr_err("request allocation failed\n");
979 return -ENOMEM;
980 }
981 req->buf = kzalloc(req_playback_buf_size,
982 GFP_ATOMIC);
983 if (!req->buf) {
984 pr_err("request buffer allocation failed\n");
985 return -ENOMEM;
986 }
987 req->length = req_playback_buf_size;
988 req->context = audio;
989 req->complete = f_audio_complete;
990 err = usb_ep_queue(out_ep, req, GFP_ATOMIC);
991 if (err)
992 pr_err("Failed to queue %s queue req: err %d\n",
993 out_ep->name, err);
Bryan Wuc6994e62009-06-03 09:17:58 -0400994 }
Anna Perel432367a2012-09-20 10:55:32 +0300995 pr_debug("Allocated %d requests\n", req_playback_count);
Bryan Wuc6994e62009-06-03 09:17:58 -0400996 } else {
Anna Perel432367a2012-09-20 10:55:32 +0300997 struct f_audio_buf *playback_copy_buf =
998 audio->playback_copy_buf;
999 if (playback_copy_buf) {
1000 pr_err("Schedule playback_work");
1001 list_add_tail(&playback_copy_buf->list,
Bryan Wuc6994e62009-06-03 09:17:58 -04001002 &audio->play_queue);
1003 schedule_work(&audio->playback_work);
Bar Weineraf8ef112013-03-05 10:40:26 +02001004 audio->playback_copy_buf = NULL;
Anna Perel432367a2012-09-20 10:55:32 +03001005 } else {
1006 pr_err("playback_buf is empty. Stop.");
Bryan Wuc6994e62009-06-03 09:17:58 -04001007 }
1008 }
Bar Weiner33e0dbe2013-03-05 10:30:52 +02001009 audio->alt_intf[1] = alt;
Anna Perel432367a2012-09-20 10:55:32 +03001010 } else {
1011 pr_err("Interface %d. Do nothing. Return %d\n", intf, err);
Bryan Wuc6994e62009-06-03 09:17:58 -04001012 }
1013
1014 return err;
1015}
1016
1017static void f_audio_disable(struct usb_function *f)
1018{
Anna Perel432367a2012-09-20 10:55:32 +03001019 u_audio_clear();
Bryan Wuc6994e62009-06-03 09:17:58 -04001020}
1021
1022/*-------------------------------------------------------------------------*/
1023
1024static void f_audio_build_desc(struct f_audio *audio)
1025{
1026 struct gaudio *card = &audio->card;
1027 u8 *sam_freq;
1028 int rate;
1029
1030 /* Set channel numbers */
Anna Perel432367a2012-09-20 10:55:32 +03001031 speaker_input_terminal_desc.bNrChannels =
1032 u_audio_get_playback_channels(card);
1033 speaker_as_type_i_desc.bNrChannels =
1034 u_audio_get_playback_channels(card);
1035
1036 microphone_input_terminal_desc.bNrChannels =
1037 u_audio_get_capture_channels(card);
1038 microphone_as_type_i_desc.bNrChannels =
1039 u_audio_get_capture_channels(card);
Bryan Wuc6994e62009-06-03 09:17:58 -04001040
1041 /* Set sample rates */
1042 rate = u_audio_get_playback_rate(card);
Anna Perel432367a2012-09-20 10:55:32 +03001043 sam_freq = speaker_as_type_i_desc.tSamFreq[0];
1044 memcpy(sam_freq, &rate, 3);
1045
1046 rate = u_audio_get_capture_rate(card);
1047 sam_freq = microphone_as_type_i_desc.tSamFreq[0];
Bryan Wuc6994e62009-06-03 09:17:58 -04001048 memcpy(sam_freq, &rate, 3);
1049
1050 /* Todo: Set Sample bits and other parameters */
1051
1052 return;
1053}
1054
1055/* audio function driver setup/binding */
Anna Perel432367a2012-09-20 10:55:32 +03001056static int
Bryan Wuc6994e62009-06-03 09:17:58 -04001057f_audio_bind(struct usb_configuration *c, struct usb_function *f)
1058{
1059 struct usb_composite_dev *cdev = c->cdev;
1060 struct f_audio *audio = func_to_audio(f);
1061 int status;
1062 struct usb_ep *ep;
Anna Perel432367a2012-09-20 10:55:32 +03001063 u8 epaddr;
1064
Bryan Wuc6994e62009-06-03 09:17:58 -04001065
1066 f_audio_build_desc(audio);
1067
1068 /* allocate instance-specific interface IDs, and patch descriptors */
1069 status = usb_interface_id(c, f);
Anna Perel432367a2012-09-20 10:55:32 +03001070 if (status < 0) {
1071 pr_err("%s: failed to allocate desc interface", __func__);
Bryan Wuc6994e62009-06-03 09:17:58 -04001072 goto fail;
Anna Perel432367a2012-09-20 10:55:32 +03001073 }
Bryan Wuc6994e62009-06-03 09:17:58 -04001074 ac_interface_desc.bInterfaceNumber = status;
1075
Anna Perel432367a2012-09-20 10:55:32 +03001076 status = -ENOMEM;
1077
Bryan Wuc6994e62009-06-03 09:17:58 -04001078 status = usb_interface_id(c, f);
Anna Perel432367a2012-09-20 10:55:32 +03001079 if (status < 0) {
1080 pr_err("%s: failed to allocate alt interface", __func__);
Bryan Wuc6994e62009-06-03 09:17:58 -04001081 goto fail;
Anna Perel432367a2012-09-20 10:55:32 +03001082 }
1083 microphone_as_interface_alt_0_desc.bInterfaceNumber = status;
1084 microphone_as_interface_alt_1_desc.bInterfaceNumber = status;
1085 ac_header_desc.baInterfaceNr[0] = status;
Bar Weiner33e0dbe2013-03-05 10:30:52 +02001086 audio->alt_intf[0] = 0;
Anna Perel432367a2012-09-20 10:55:32 +03001087
1088 status = -ENODEV;
1089
1090 status = usb_interface_id(c, f);
1091 if (status < 0) {
1092 pr_err("%s: failed to allocate alt interface", __func__);
1093 goto fail;
1094 }
1095 speaker_as_interface_alt_0_desc.bInterfaceNumber = status;
1096 speaker_as_interface_alt_1_desc.bInterfaceNumber = status;
1097 ac_header_desc.baInterfaceNr[1] = status;
Bar Weiner33e0dbe2013-03-05 10:30:52 +02001098 audio->alt_intf[1] = 0;
Bryan Wuc6994e62009-06-03 09:17:58 -04001099
1100 status = -ENODEV;
1101
1102 /* allocate instance-specific endpoints */
Anna Perel432367a2012-09-20 10:55:32 +03001103 ep = usb_ep_autoconfig(cdev->gadget, &microphone_as_ep_in_desc);
1104 if (!ep) {
1105 pr_err("%s: failed to autoconfig in endpoint", __func__);
Bryan Wuc6994e62009-06-03 09:17:58 -04001106 goto fail;
Anna Perel432367a2012-09-20 10:55:32 +03001107 }
1108 audio->in_ep = ep;
1109 ep->desc = &microphone_as_ep_in_desc;
1110 ep->driver_data = cdev;
Bryan Wuc6994e62009-06-03 09:17:58 -04001111
1112 status = -ENOMEM;
1113
Anna Perel432367a2012-09-20 10:55:32 +03001114 ep = usb_ep_autoconfig(cdev->gadget, &speaker_as_ep_out_desc);
1115 if (!ep) {
1116 pr_err("%s: failed to autoconfig out endpoint", __func__);
1117 goto fail;
1118 }
1119 audio->out_ep = ep;
1120 ep->desc = &speaker_as_ep_out_desc;
1121 ep->driver_data = cdev;
Felipe Balbief7f5842011-08-26 12:48:15 +03001122
Anna Perel432367a2012-09-20 10:55:32 +03001123 /* associate bEndpointAddress with usb_function */
1124 epaddr = microphone_as_ep_in_desc.bEndpointAddress & ~USB_DIR_IN;
1125 microphone_as_iso_in.id = epaddr;
1126
1127 epaddr = speaker_as_ep_out_desc.bEndpointAddress & ~USB_DIR_IN;
1128 speaker_as_iso_out.id = epaddr;
1129
1130 /* support all relevant hardware speeds. we expect that when
Bryan Wuc6994e62009-06-03 09:17:58 -04001131 * hardware is dual speed, all bulk-capable endpoints work at
1132 * both speeds
1133 */
Anna Perel432367a2012-09-20 10:55:32 +03001134
1135 /* copy descriptors, and track endpoint copies */
Bryan Wuc6994e62009-06-03 09:17:58 -04001136 if (gadget_is_dualspeed(c->cdev->gadget)) {
1137 c->highspeed = true;
1138 f->hs_descriptors = usb_copy_descriptors(f_audio_desc);
Anna Perel432367a2012-09-20 10:55:32 +03001139 } else {
1140 f->descriptors = usb_copy_descriptors(f_audio_desc);
Felipe Balbief7f5842011-08-26 12:48:15 +03001141 }
Bryan Wuc6994e62009-06-03 09:17:58 -04001142
1143 return 0;
1144
1145fail:
Bryan Wuc6994e62009-06-03 09:17:58 -04001146 return status;
1147}
1148
1149static void
1150f_audio_unbind(struct usb_configuration *c, struct usb_function *f)
1151{
Anna Perel432367a2012-09-20 10:55:32 +03001152 struct f_audio *audio = func_to_audio(f);
Bryan Wuc6994e62009-06-03 09:17:58 -04001153
1154 usb_free_descriptors(f->descriptors);
Sebastian Andrzej Siewior16a2f972011-04-11 20:44:30 +02001155 usb_free_descriptors(f->hs_descriptors);
Bryan Wuc6994e62009-06-03 09:17:58 -04001156 kfree(audio);
1157}
1158
1159/*-------------------------------------------------------------------------*/
1160
Laurent Pinchartb95cd7e2009-06-21 23:21:55 +02001161static int generic_set_cmd(struct usb_audio_control *con, u8 cmd, int value)
1162{
1163 con->data[cmd] = value;
1164
1165 return 0;
1166}
1167
1168static int generic_get_cmd(struct usb_audio_control *con, u8 cmd)
1169{
1170 return con->data[cmd];
1171}
1172
Bryan Wuc6994e62009-06-03 09:17:58 -04001173/* Todo: add more control selecotor dynamically */
Anna Perel432367a2012-09-20 10:55:32 +03001174int control_selector_init(struct f_audio *audio)
Bryan Wuc6994e62009-06-03 09:17:58 -04001175{
Anna Perel432367a2012-09-20 10:55:32 +03001176 INIT_LIST_HEAD(&audio->fu_cs);
1177 list_add(&microphone_fu_controls.list, &audio->fu_cs);
1178 list_add(&speaker_fu_controls.list, &audio->fu_cs);
Bryan Wuc6994e62009-06-03 09:17:58 -04001179
Anna Perel432367a2012-09-20 10:55:32 +03001180 INIT_LIST_HEAD(&microphone_fu_controls.control);
1181 list_add(&microphone_mute_control.list,
1182 &microphone_fu_controls.control);
1183 list_add(&microphone_volume_control.list,
1184 &microphone_fu_controls.control);
Bryan Wuc6994e62009-06-03 09:17:58 -04001185
Anna Perel432367a2012-09-20 10:55:32 +03001186 INIT_LIST_HEAD(&speaker_fu_controls.control);
1187 list_add(&speaker_mute_control.list,
1188 &speaker_fu_controls.control);
1189 list_add(&speaker_volume_control.list,
1190 &speaker_fu_controls.control);
1191
1192 microphone_volume_control.data[UAC__CUR] = 0xffc0;
1193 microphone_volume_control.data[UAC__MIN] = 0xe3a0;
1194 microphone_volume_control.data[UAC__MAX] = 0xfff0;
1195 microphone_volume_control.data[UAC__RES] = 0x0030;
1196
1197 speaker_volume_control.data[UAC__CUR] = 0xffc0;
1198 speaker_volume_control.data[UAC__MIN] = 0xe3a0;
1199 speaker_volume_control.data[UAC__MAX] = 0xfff0;
1200 speaker_volume_control.data[UAC__RES] = 0x0030;
1201
1202 INIT_LIST_HEAD(&audio->ep_cs);
1203 list_add(&speaker_as_iso_out.list, &audio->ep_cs);
1204 list_add(&microphone_as_iso_in.list, &audio->ep_cs);
1205
1206 INIT_LIST_HEAD(&microphone_as_iso_in.control);
1207 list_add(&microphone_sample_freq_control.list,
1208 &microphone_as_iso_in.control);
1209
1210 INIT_LIST_HEAD(&speaker_as_iso_out.control);
1211 list_add(&speaker_sample_freq_control.list,
1212 &speaker_as_iso_out.control);
Bryan Wuc6994e62009-06-03 09:17:58 -04001213
1214 return 0;
Anna Perel432367a2012-09-20 10:55:32 +03001215
Bryan Wuc6994e62009-06-03 09:17:58 -04001216}
1217
1218/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001219 * audio_bind_config - add USB audio function to a configuration
Anna Perel432367a2012-09-20 10:55:32 +03001220 * @c: the configuration to support the USB audio function
Bryan Wuc6994e62009-06-03 09:17:58 -04001221 * Context: single threaded during gadget setup
1222 *
1223 * Returns zero on success, else negative errno.
1224 */
Anna Perel432367a2012-09-20 10:55:32 +03001225int audio_bind_config(struct usb_configuration *c)
Bryan Wuc6994e62009-06-03 09:17:58 -04001226{
1227 struct f_audio *audio;
1228 int status;
1229
1230 /* allocate and initialize one new instance */
1231 audio = kzalloc(sizeof *audio, GFP_KERNEL);
1232 if (!audio)
1233 return -ENOMEM;
1234
Bryan Wuc6994e62009-06-03 09:17:58 -04001235 audio->card.gadget = c->cdev->gadget;
1236
1237 INIT_LIST_HEAD(&audio->play_queue);
Anna Perel432367a2012-09-20 10:55:32 +03001238 spin_lock_init(&audio->playback_lock);
Bryan Wuc6994e62009-06-03 09:17:58 -04001239
Anna Perel432367a2012-09-20 10:55:32 +03001240 INIT_LIST_HEAD(&audio->capture_queue);
1241 spin_lock_init(&audio->capture_lock);
Bryan Wuc6994e62009-06-03 09:17:58 -04001242
Anna Perel432367a2012-09-20 10:55:32 +03001243 audio->card.func.name = "audio";
Bryan Wuc6994e62009-06-03 09:17:58 -04001244 audio->card.func.strings = audio_strings;
1245 audio->card.func.bind = f_audio_bind;
1246 audio->card.func.unbind = f_audio_unbind;
Bar Weiner33e0dbe2013-03-05 10:30:52 +02001247 audio->card.func.get_alt = f_audio_get_alt;
Bryan Wuc6994e62009-06-03 09:17:58 -04001248 audio->card.func.set_alt = f_audio_set_alt;
1249 audio->card.func.setup = f_audio_setup;
1250 audio->card.func.disable = f_audio_disable;
Bryan Wuc6994e62009-06-03 09:17:58 -04001251
1252 control_selector_init(audio);
Bryan Wuc6994e62009-06-03 09:17:58 -04001253 INIT_WORK(&audio->playback_work, f_audio_playback_work);
Anna Perel432367a2012-09-20 10:55:32 +03001254 INIT_WORK(&audio->capture_work, f_audio_capture_work);
Bryan Wuc6994e62009-06-03 09:17:58 -04001255
Anna Perel432367a2012-09-20 10:55:32 +03001256 /* set up ASLA audio devices */
1257 status = gaudio_setup(&audio->card);
1258 if (status < 0)
Bryan Wuc6994e62009-06-03 09:17:58 -04001259 goto add_fail;
1260
Anna Perel432367a2012-09-20 10:55:32 +03001261 status = usb_add_function(c, &audio->card.func);
1262 if (status) {
1263 pr_err("%s: Failed to add usb audio function, err = %d",
1264 __func__, status);
1265 goto setup_fail;
1266 }
Bryan Wuc6994e62009-06-03 09:17:58 -04001267
1268 return status;
1269
1270add_fail:
Cliff Caifeef1d92009-12-09 22:28:39 -05001271 gaudio_cleanup();
Bryan Wuc6994e62009-06-03 09:17:58 -04001272setup_fail:
1273 kfree(audio);
1274 return status;
1275}