blob: 8c74381f6de301a10ae75c28ad727d4f600e4ebb [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
466 /* Control Set command */
Anna Perel432367a2012-09-20 10:55:32 +0300467 struct list_head fu_cs;
468 struct list_head ep_cs;
469 u8 set_cmd;
470 struct usb_audio_control *set_con;
Bryan Wuc6994e62009-06-03 09:17:58 -0400471};
472
473static inline struct f_audio *func_to_audio(struct usb_function *f)
474{
475 return container_of(f, struct f_audio, card.func);
476}
477
478/*-------------------------------------------------------------------------*/
479
480static void f_audio_playback_work(struct work_struct *data)
481{
482 struct f_audio *audio = container_of(data, struct f_audio,
483 playback_work);
484 struct f_audio_buf *play_buf;
Anna Perel432367a2012-09-20 10:55:32 +0300485 unsigned long flags;
486 int res = 0;
Bryan Wuc6994e62009-06-03 09:17:58 -0400487
Anna Perel432367a2012-09-20 10:55:32 +0300488 spin_lock_irqsave(&audio->playback_lock, flags);
Bryan Wuc6994e62009-06-03 09:17:58 -0400489 if (list_empty(&audio->play_queue)) {
Anna Perel432367a2012-09-20 10:55:32 +0300490 pr_err("playback_buf is empty");
491 spin_unlock_irqrestore(&audio->playback_lock, flags);
Bryan Wuc6994e62009-06-03 09:17:58 -0400492 return;
493 }
494 play_buf = list_first_entry(&audio->play_queue,
495 struct f_audio_buf, list);
496 list_del(&play_buf->list);
Anna Perel432367a2012-09-20 10:55:32 +0300497 spin_unlock_irqrestore(&audio->playback_lock, flags);
Bryan Wuc6994e62009-06-03 09:17:58 -0400498
Anna Perel432367a2012-09-20 10:55:32 +0300499 pr_debug("play_buf->actual = %d", play_buf->actual);
500
501 res = u_audio_playback(&audio->card, play_buf->buf, play_buf->actual);
502 if (res)
503 pr_err("copying failed");
504
Bryan Wuc6994e62009-06-03 09:17:58 -0400505 f_audio_buffer_free(play_buf);
Bryan Wuc6994e62009-06-03 09:17:58 -0400506}
507
Anna Perel432367a2012-09-20 10:55:32 +0300508static int
509f_audio_playback_ep_complete(struct usb_ep *ep, struct usb_request *req)
Bryan Wuc6994e62009-06-03 09:17:58 -0400510{
511 struct f_audio *audio = req->context;
Anna Perel432367a2012-09-20 10:55:32 +0300512 struct f_audio_buf *copy_buf = audio->playback_copy_buf;
Bryan Wuc6994e62009-06-03 09:17:58 -0400513 int err;
514
515 if (!copy_buf)
516 return -EINVAL;
517
518 /* Copy buffer is full, add it to the play_queue */
Anna Perel432367a2012-09-20 10:55:32 +0300519 if (audio_playback_buf_size - copy_buf->actual < req->actual) {
520 pr_debug("audio_playback_buf_size %d - copy_buf->actual %d, req->actual %d",
521 audio_playback_buf_size, copy_buf->actual, req->actual);
Bryan Wuc6994e62009-06-03 09:17:58 -0400522 list_add_tail(&copy_buf->list, &audio->play_queue);
523 schedule_work(&audio->playback_work);
Anna Perel432367a2012-09-20 10:55:32 +0300524 copy_buf = f_audio_buffer_alloc(audio_playback_buf_size);
525 if (IS_ERR(copy_buf)) {
526 pr_err("Failed to allocate playback_copy_buf");
Bryan Wuc6994e62009-06-03 09:17:58 -0400527 return -ENOMEM;
Anna Perel432367a2012-09-20 10:55:32 +0300528 }
Bryan Wuc6994e62009-06-03 09:17:58 -0400529 }
530
531 memcpy(copy_buf->buf + copy_buf->actual, req->buf, req->actual);
532 copy_buf->actual += req->actual;
Anna Perel432367a2012-09-20 10:55:32 +0300533 audio->playback_copy_buf = copy_buf;
Bryan Wuc6994e62009-06-03 09:17:58 -0400534
535 err = usb_ep_queue(ep, req, GFP_ATOMIC);
536 if (err)
Anna Perel432367a2012-09-20 10:55:32 +0300537 pr_err("Failed to queue %s req: err - %d\n", ep->name, err);
Bryan Wuc6994e62009-06-03 09:17:58 -0400538
Anna Perel432367a2012-09-20 10:55:32 +0300539 return err;
540}
Bryan Wuc6994e62009-06-03 09:17:58 -0400541
Anna Perel432367a2012-09-20 10:55:32 +0300542static void f_audio_capture_work(struct work_struct *data)
543{
544 struct f_audio *audio =
545 container_of(data, struct f_audio, capture_work);
546 struct f_audio_buf *capture_buf;
547 unsigned long flags;
548 int res = 0;
549
550 capture_buf = f_audio_buffer_alloc(audio_capture_buf_size);
551 if (capture_buf <= 0) {
552 pr_err("%s: buffer alloc failed\n", __func__);
553 return;
554 }
555
556 res = u_audio_capture(&audio->card, capture_buf->buf,
557 audio_capture_buf_size);
558 if (res)
559 pr_err("copying failed");
560
561 pr_debug("Queue capture packet: size %d", audio_capture_buf_size);
562 spin_lock_irqsave(&audio->capture_lock, flags);
563 list_add_tail(&capture_buf->list, &audio->capture_queue);
564 spin_unlock_irqrestore(&audio->capture_lock, flags);
565}
566
567static int
568f_audio_capture_ep_complete(struct usb_ep *ep, struct usb_request *req)
569{
570 struct f_audio *audio = req->context;
571 struct f_audio_buf *copy_buf = audio->capture_copy_buf;
572 unsigned long flags;
573 int err = 0;
574
575 if (copy_buf == 0) {
576 pr_debug("copy_buf == 0");
577 spin_lock_irqsave(&audio->capture_lock, flags);
578 if (list_empty(&audio->capture_queue)) {
579 spin_unlock_irqrestore(&audio->capture_lock, flags);
580 schedule_work(&audio->capture_work);
581 goto done;
582 }
583 copy_buf = list_first_entry(&audio->capture_queue,
584 struct f_audio_buf, list);
585 list_del(&copy_buf->list);
586 audio->capture_copy_buf = copy_buf;
587 spin_unlock_irqrestore(&audio->capture_lock, flags);
588 }
589
590 pr_debug("Copy %d bytes", req->actual);
591 memcpy(req->buf, copy_buf->buf + copy_buf->actual, req->actual);
592 copy_buf->actual += req->actual;
593
594 if (audio_capture_buf_size - copy_buf->actual < req->actual) {
595 f_audio_buffer_free(copy_buf);
596 audio->capture_copy_buf = 0;
597 schedule_work(&audio->capture_work);
598 }
599done:
600 err = usb_ep_queue(ep, req, GFP_ATOMIC);
601 if (err)
602 pr_err("Failed to queue %s req: err - %d\n", ep->name, err);
603
604 return err;
Bryan Wuc6994e62009-06-03 09:17:58 -0400605}
606
607static void f_audio_complete(struct usb_ep *ep, struct usb_request *req)
608{
609 struct f_audio *audio = req->context;
610 int status = req->status;
611 u32 data = 0;
Bryan Wuc6994e62009-06-03 09:17:58 -0400612
613 switch (status) {
Anna Perel432367a2012-09-20 10:55:32 +0300614 case 0: /* normal completion? */
615 if (ep == audio->out_ep) {
616 f_audio_playback_ep_complete(ep, req);
617 } else if (ep == audio->in_ep) {
618 f_audio_capture_ep_complete(ep, req);
619 } else if (audio->set_con) {
Bryan Wuc6994e62009-06-03 09:17:58 -0400620 memcpy(&data, req->buf, req->length);
621 audio->set_con->set(audio->set_con, audio->set_cmd,
622 le16_to_cpu(data));
623 audio->set_con = NULL;
624 }
625 break;
626 default:
Anna Perel432367a2012-09-20 10:55:32 +0300627 pr_err("Failed completion: status %d", status);
Bryan Wuc6994e62009-06-03 09:17:58 -0400628 break;
629 }
630}
631
632static int audio_set_intf_req(struct usb_function *f,
633 const struct usb_ctrlrequest *ctrl)
634{
635 struct f_audio *audio = func_to_audio(f);
636 struct usb_composite_dev *cdev = f->config->cdev;
637 struct usb_request *req = cdev->req;
638 u8 id = ((le16_to_cpu(ctrl->wIndex) >> 8) & 0xFF);
639 u16 len = le16_to_cpu(ctrl->wLength);
640 u16 w_value = le16_to_cpu(ctrl->wValue);
641 u8 con_sel = (w_value >> 8) & 0xFF;
642 u8 cmd = (ctrl->bRequest & 0x0F);
643 struct usb_audio_control_selector *cs;
644 struct usb_audio_control *con;
645
Anna Perel432367a2012-09-20 10:55:32 +0300646 pr_debug("bRequest 0x%x, w_value 0x%04x, len %d, entity %d\n",
Bryan Wuc6994e62009-06-03 09:17:58 -0400647 ctrl->bRequest, w_value, len, id);
648
Anna Perel432367a2012-09-20 10:55:32 +0300649 list_for_each_entry(cs, &audio->fu_cs, list) {
Bryan Wuc6994e62009-06-03 09:17:58 -0400650 if (cs->id == id) {
651 list_for_each_entry(con, &cs->control, list) {
652 if (con->type == con_sel) {
653 audio->set_con = con;
654 break;
655 }
656 }
657 break;
658 }
659 }
660
661 audio->set_cmd = cmd;
662 req->context = audio;
663 req->complete = f_audio_complete;
664
665 return len;
666}
667
668static int audio_get_intf_req(struct usb_function *f,
669 const struct usb_ctrlrequest *ctrl)
670{
671 struct f_audio *audio = func_to_audio(f);
672 struct usb_composite_dev *cdev = f->config->cdev;
673 struct usb_request *req = cdev->req;
674 int value = -EOPNOTSUPP;
675 u8 id = ((le16_to_cpu(ctrl->wIndex) >> 8) & 0xFF);
676 u16 len = le16_to_cpu(ctrl->wLength);
677 u16 w_value = le16_to_cpu(ctrl->wValue);
678 u8 con_sel = (w_value >> 8) & 0xFF;
679 u8 cmd = (ctrl->bRequest & 0x0F);
680 struct usb_audio_control_selector *cs;
681 struct usb_audio_control *con;
682
Anna Perel432367a2012-09-20 10:55:32 +0300683 pr_debug("bRequest 0x%x, w_value 0x%04x, len %d, entity %d\n",
Bryan Wuc6994e62009-06-03 09:17:58 -0400684 ctrl->bRequest, w_value, len, id);
685
Anna Perel432367a2012-09-20 10:55:32 +0300686 list_for_each_entry(cs, &audio->fu_cs, list) {
Bryan Wuc6994e62009-06-03 09:17:58 -0400687 if (cs->id == id) {
688 list_for_each_entry(con, &cs->control, list) {
689 if (con->type == con_sel && con->get) {
690 value = con->get(con, cmd);
691 break;
692 }
693 }
694 break;
695 }
696 }
697
698 req->context = audio;
699 req->complete = f_audio_complete;
700 memcpy(req->buf, &value, len);
701
702 return len;
703}
704
Anna Perel432367a2012-09-20 10:55:32 +0300705static void audio_set_endpoint_complete(struct usb_ep *ep,
706 struct usb_request *req)
707{
708 struct f_audio *audio = req->context;
709 u32 data = 0;
710
711 if (req->status == 0 && audio->set_con) {
712 memcpy(&data, req->buf, req->length);
713 audio->set_con->set(audio->set_con, audio->set_cmd,
714 le32_to_cpu(data));
715 audio->set_con = NULL;
716 }
717}
718
Laurent Pinchart0ad72522009-10-21 00:03:39 +0200719static int audio_set_endpoint_req(struct usb_function *f,
720 const struct usb_ctrlrequest *ctrl)
721{
Anna Perel432367a2012-09-20 10:55:32 +0300722 int value = -EOPNOTSUPP;
723 u16 ep = le16_to_cpu(ctrl->wIndex);
724 u16 len = le16_to_cpu(ctrl->wLength);
725 u16 w_value = le16_to_cpu(ctrl->wValue);
726
727 struct f_audio *audio = func_to_audio(f);
Laurent Pinchart0ad72522009-10-21 00:03:39 +0200728 struct usb_composite_dev *cdev = f->config->cdev;
Anna Perel432367a2012-09-20 10:55:32 +0300729 struct usb_request *req = cdev->req;
730 struct usb_audio_control_selector *cs;
731 struct usb_audio_control *con;
Laurent Pinchart0ad72522009-10-21 00:03:39 +0200732
Anna Perel432367a2012-09-20 10:55:32 +0300733 u8 epnum = ep & ~0x80;
734 u8 con_sel = (w_value >> 8) & 0xFF;
735 u8 cmd = (ctrl->bRequest & 0x0F);
Laurent Pinchart0ad72522009-10-21 00:03:39 +0200736
Anna Perel432367a2012-09-20 10:55:32 +0300737 pr_debug("bRequest 0x%x, w_value 0x%04x, len %d, endp %d, epnum %d\n",
738 ctrl->bRequest, w_value, len, ep, epnum);
Laurent Pinchart0ad72522009-10-21 00:03:39 +0200739
Anna Perel432367a2012-09-20 10:55:32 +0300740 list_for_each_entry(cs, &audio->ep_cs, list) {
741 if (cs->id != epnum)
742 continue;
Laurent Pinchart0ad72522009-10-21 00:03:39 +0200743
Anna Perel432367a2012-09-20 10:55:32 +0300744 list_for_each_entry(con, &cs->control, list) {
745 if (con->type != con_sel)
746 continue;
Laurent Pinchart0ad72522009-10-21 00:03:39 +0200747
Anna Perel432367a2012-09-20 10:55:32 +0300748 switch (cmd) {
749 case UAC__CUR:
750 case UAC__MIN:
751 case UAC__MAX:
752 case UAC__RES:
753 audio->set_con = con;
754 audio->set_cmd = cmd;
755 req->context = audio;
756 req->complete = audio_set_endpoint_complete;
757 value = len;
758 break;
759 case UAC__MEM:
760 break;
761 default:
762 pr_err("Unknown command");
763 break;
764 }
765 break;
766 }
Laurent Pinchart0ad72522009-10-21 00:03:39 +0200767 break;
768 }
769
770 return value;
771}
772
773static int audio_get_endpoint_req(struct usb_function *f,
774 const struct usb_ctrlrequest *ctrl)
775{
Anna Perel432367a2012-09-20 10:55:32 +0300776 struct f_audio *audio = func_to_audio(f);
Laurent Pinchart0ad72522009-10-21 00:03:39 +0200777 struct usb_composite_dev *cdev = f->config->cdev;
Anna Perel432367a2012-09-20 10:55:32 +0300778 struct usb_request *req = cdev->req;
779 struct usb_audio_control_selector *cs;
780 struct usb_audio_control *con;
781 int data;
Laurent Pinchart0ad72522009-10-21 00:03:39 +0200782
Anna Perel432367a2012-09-20 10:55:32 +0300783 int value = -EOPNOTSUPP;
784 u8 ep = (le16_to_cpu(ctrl->wIndex) & 0x7F);
785 u8 epnum = ep & ~0x80;
786 u16 len = le16_to_cpu(ctrl->wLength);
787 u16 w_value = le16_to_cpu(ctrl->wValue);
788 u8 con_sel = (w_value >> 8) & 0xFF;
789 u8 cmd = (ctrl->bRequest & 0x0F);
790
791 pr_debug("bRequest 0x%x, w_value 0x%04x, len %d, ep %d\n",
Laurent Pinchart0ad72522009-10-21 00:03:39 +0200792 ctrl->bRequest, w_value, len, ep);
793
Anna Perel432367a2012-09-20 10:55:32 +0300794 list_for_each_entry(cs, &audio->ep_cs, list) {
795 if (cs->id != epnum)
796 continue;
797
798 list_for_each_entry(con, &cs->control, list) {
799 if (con->type != con_sel)
800 continue;
801
802 switch (cmd) {
803 case UAC__CUR:
804 case UAC__MIN:
805 case UAC__MAX:
806 case UAC__RES:
807 data = cpu_to_le32(generic_get_cmd(con, cmd));
808 memcpy(req->buf, &data, len);
809 value = len;
810 break;
811 case UAC__MEM:
812 break;
813 default:
814 break;
815 }
816 break;
817 }
Laurent Pinchart0ad72522009-10-21 00:03:39 +0200818 break;
819 }
820
821 return value;
822}
823
Bryan Wuc6994e62009-06-03 09:17:58 -0400824static int
825f_audio_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
826{
827 struct usb_composite_dev *cdev = f->config->cdev;
828 struct usb_request *req = cdev->req;
829 int value = -EOPNOTSUPP;
830 u16 w_index = le16_to_cpu(ctrl->wIndex);
831 u16 w_value = le16_to_cpu(ctrl->wValue);
832 u16 w_length = le16_to_cpu(ctrl->wLength);
833
Laurent Pinchart0ad72522009-10-21 00:03:39 +0200834 /* composite driver infrastructure handles everything; interface
835 * activation uses set_alt().
Bryan Wuc6994e62009-06-03 09:17:58 -0400836 */
Anna Perel432367a2012-09-20 10:55:32 +0300837
Bryan Wuc6994e62009-06-03 09:17:58 -0400838 switch (ctrl->bRequestType) {
Laurent Pinchart512ad272009-06-21 23:23:05 +0200839 case USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE:
Anna Perel432367a2012-09-20 10:55:32 +0300840 pr_debug("USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE");
Bryan Wuc6994e62009-06-03 09:17:58 -0400841 value = audio_set_intf_req(f, ctrl);
842 break;
843
Laurent Pinchart512ad272009-06-21 23:23:05 +0200844 case USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE:
Anna Perel432367a2012-09-20 10:55:32 +0300845 pr_debug("USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE");
Bryan Wuc6994e62009-06-03 09:17:58 -0400846 value = audio_get_intf_req(f, ctrl);
847 break;
848
Laurent Pinchart0ad72522009-10-21 00:03:39 +0200849 case USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_ENDPOINT:
Anna Perel432367a2012-09-20 10:55:32 +0300850 pr_debug("USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_ENDPOINT");
Laurent Pinchart0ad72522009-10-21 00:03:39 +0200851 value = audio_set_endpoint_req(f, ctrl);
852 break;
853
854 case USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT:
Anna Perel432367a2012-09-20 10:55:32 +0300855 pr_debug("USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT");
Laurent Pinchart0ad72522009-10-21 00:03:39 +0200856 value = audio_get_endpoint_req(f, ctrl);
857 break;
858
Bryan Wuc6994e62009-06-03 09:17:58 -0400859 default:
Anna Perel432367a2012-09-20 10:55:32 +0300860 pr_err("Unknown control request %02x.%02x v%04x i%04x l%d\n",
Bryan Wuc6994e62009-06-03 09:17:58 -0400861 ctrl->bRequestType, ctrl->bRequest,
862 w_value, w_index, w_length);
Anna Perel432367a2012-09-20 10:55:32 +0300863
Bryan Wuc6994e62009-06-03 09:17:58 -0400864 }
865
866 /* respond with data transfer or status phase? */
867 if (value >= 0) {
Anna Perel432367a2012-09-20 10:55:32 +0300868 pr_debug("audio req %02x.%02x v%04x i%04x l%d\n",
Bryan Wuc6994e62009-06-03 09:17:58 -0400869 ctrl->bRequestType, ctrl->bRequest,
870 w_value, w_index, w_length);
Anna Perel432367a2012-09-20 10:55:32 +0300871 req->zero = 1;
Bryan Wuc6994e62009-06-03 09:17:58 -0400872 req->length = value;
873 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
874 if (value < 0)
Anna Perel432367a2012-09-20 10:55:32 +0300875 pr_err("audio response failed on err %d\n", value);
876 } else {
877 pr_err("STALL\n");
Bryan Wuc6994e62009-06-03 09:17:58 -0400878 }
879
880 /* device either stalls (value < 0) or reports success */
881 return value;
882}
883
884static int f_audio_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
885{
886 struct f_audio *audio = func_to_audio(f);
Anna Perel432367a2012-09-20 10:55:32 +0300887 struct usb_ep *out_ep = audio->out_ep;
888 struct usb_ep *in_ep = audio->in_ep;
889 struct usb_request *req;
890 unsigned long flags;
Bryan Wuc6994e62009-06-03 09:17:58 -0400891 int i = 0, err = 0;
892
Anna Perel432367a2012-09-20 10:55:32 +0300893 pr_debug("intf %d, alt %d\n", intf, alt);
Bryan Wuc6994e62009-06-03 09:17:58 -0400894
Anna Perel432367a2012-09-20 10:55:32 +0300895 if (intf == ac_header_desc.baInterfaceNr[0]) {
Bryan Wuc6994e62009-06-03 09:17:58 -0400896 if (alt == 1) {
Anna Perel432367a2012-09-20 10:55:32 +0300897 err = usb_ep_enable(in_ep);
898 if (err) {
899 pr_err("Failed to enable capture ep");
900 return err;
901 }
902 in_ep->driver_data = audio;
903 audio->capture_copy_buf = 0;
904
905 /* Allocate a write buffer */
906 req = usb_ep_alloc_request(in_ep, GFP_ATOMIC);
907 if (!req) {
908 pr_err("request allocation failed\n");
Julia Lawallff3b9682009-12-09 14:23:32 +0100909 return -ENOMEM;
Anna Perel432367a2012-09-20 10:55:32 +0300910 }
911 req->buf = kzalloc(req_capture_buf_size,
912 GFP_ATOMIC);
913 if (!req->buf) {
914 pr_err("request buffer allocation failed\n");
915 return -ENOMEM;
916 }
917
918 req->length = req_capture_buf_size;
919 req->context = audio;
920 req->complete = f_audio_complete;
921 audio->capture_req = req;
922 err = usb_ep_queue(in_ep, req, GFP_ATOMIC);
923 if (err)
924 pr_err("Failed to queue %s req: err %d\n",
925 in_ep->name, err);
926 schedule_work(&audio->capture_work);
927 } else {
928 struct f_audio_buf *capture_buf;
929 spin_lock_irqsave(&audio->capture_lock, flags);
930 while (!list_empty(&audio->capture_queue)) {
931 capture_buf =
932 list_first_entry(
933 &audio->capture_queue,
934 struct f_audio_buf,
935 list);
936 list_del(&capture_buf->list);
937 f_audio_buffer_free(capture_buf);
938 }
939 spin_unlock_irqrestore(&audio->capture_lock, flags);
940 }
941 } else if (intf == ac_header_desc.baInterfaceNr[1]) {
942 if (alt == 1) {
943 err = usb_ep_enable(out_ep);
944 if (err) {
945 pr_err("Failed to enable playback ep");
946 return err;
947 }
948 out_ep->driver_data = audio;
949 audio->playback_copy_buf =
950 f_audio_buffer_alloc(audio_playback_buf_size);
951 if (IS_ERR(audio->playback_copy_buf)) {
952 pr_err("Failed to allocate playback_copy_buf");
953 return -ENOMEM;
954 }
Bryan Wuc6994e62009-06-03 09:17:58 -0400955
956 /*
957 * allocate a bunch of read buffers
958 * and queue them all at once.
959 */
Anna Perel432367a2012-09-20 10:55:32 +0300960 for (i = 0; i < req_playback_count && err == 0; i++) {
Bryan Wuc6994e62009-06-03 09:17:58 -0400961 req = usb_ep_alloc_request(out_ep, GFP_ATOMIC);
Anna Perel432367a2012-09-20 10:55:32 +0300962 if (!req) {
963 pr_err("request allocation failed\n");
964 return -ENOMEM;
965 }
966 req->buf = kzalloc(req_playback_buf_size,
967 GFP_ATOMIC);
968 if (!req->buf) {
969 pr_err("request buffer allocation failed\n");
970 return -ENOMEM;
971 }
972 req->length = req_playback_buf_size;
973 req->context = audio;
974 req->complete = f_audio_complete;
975 err = usb_ep_queue(out_ep, req, GFP_ATOMIC);
976 if (err)
977 pr_err("Failed to queue %s queue req: err %d\n",
978 out_ep->name, err);
Bryan Wuc6994e62009-06-03 09:17:58 -0400979 }
Anna Perel432367a2012-09-20 10:55:32 +0300980 pr_debug("Allocated %d requests\n", req_playback_count);
Bryan Wuc6994e62009-06-03 09:17:58 -0400981 } else {
Anna Perel432367a2012-09-20 10:55:32 +0300982 struct f_audio_buf *playback_copy_buf =
983 audio->playback_copy_buf;
984 if (playback_copy_buf) {
985 pr_err("Schedule playback_work");
986 list_add_tail(&playback_copy_buf->list,
Bryan Wuc6994e62009-06-03 09:17:58 -0400987 &audio->play_queue);
988 schedule_work(&audio->playback_work);
Anna Perel432367a2012-09-20 10:55:32 +0300989 } else {
990 pr_err("playback_buf is empty. Stop.");
Bryan Wuc6994e62009-06-03 09:17:58 -0400991 }
992 }
Anna Perel432367a2012-09-20 10:55:32 +0300993 } else {
994 pr_err("Interface %d. Do nothing. Return %d\n", intf, err);
Bryan Wuc6994e62009-06-03 09:17:58 -0400995 }
996
997 return err;
998}
999
1000static void f_audio_disable(struct usb_function *f)
1001{
Anna Perel432367a2012-09-20 10:55:32 +03001002 u_audio_clear();
Bryan Wuc6994e62009-06-03 09:17:58 -04001003}
1004
1005/*-------------------------------------------------------------------------*/
1006
1007static void f_audio_build_desc(struct f_audio *audio)
1008{
1009 struct gaudio *card = &audio->card;
1010 u8 *sam_freq;
1011 int rate;
1012
1013 /* Set channel numbers */
Anna Perel432367a2012-09-20 10:55:32 +03001014 speaker_input_terminal_desc.bNrChannels =
1015 u_audio_get_playback_channels(card);
1016 speaker_as_type_i_desc.bNrChannels =
1017 u_audio_get_playback_channels(card);
1018
1019 microphone_input_terminal_desc.bNrChannels =
1020 u_audio_get_capture_channels(card);
1021 microphone_as_type_i_desc.bNrChannels =
1022 u_audio_get_capture_channels(card);
Bryan Wuc6994e62009-06-03 09:17:58 -04001023
1024 /* Set sample rates */
1025 rate = u_audio_get_playback_rate(card);
Anna Perel432367a2012-09-20 10:55:32 +03001026 sam_freq = speaker_as_type_i_desc.tSamFreq[0];
1027 memcpy(sam_freq, &rate, 3);
1028
1029 rate = u_audio_get_capture_rate(card);
1030 sam_freq = microphone_as_type_i_desc.tSamFreq[0];
Bryan Wuc6994e62009-06-03 09:17:58 -04001031 memcpy(sam_freq, &rate, 3);
1032
1033 /* Todo: Set Sample bits and other parameters */
1034
1035 return;
1036}
1037
1038/* audio function driver setup/binding */
Anna Perel432367a2012-09-20 10:55:32 +03001039static int
Bryan Wuc6994e62009-06-03 09:17:58 -04001040f_audio_bind(struct usb_configuration *c, struct usb_function *f)
1041{
1042 struct usb_composite_dev *cdev = c->cdev;
1043 struct f_audio *audio = func_to_audio(f);
1044 int status;
1045 struct usb_ep *ep;
Anna Perel432367a2012-09-20 10:55:32 +03001046 u8 epaddr;
1047
Bryan Wuc6994e62009-06-03 09:17:58 -04001048
1049 f_audio_build_desc(audio);
1050
1051 /* allocate instance-specific interface IDs, and patch descriptors */
1052 status = usb_interface_id(c, f);
Anna Perel432367a2012-09-20 10:55:32 +03001053 if (status < 0) {
1054 pr_err("%s: failed to allocate desc interface", __func__);
Bryan Wuc6994e62009-06-03 09:17:58 -04001055 goto fail;
Anna Perel432367a2012-09-20 10:55:32 +03001056 }
Bryan Wuc6994e62009-06-03 09:17:58 -04001057 ac_interface_desc.bInterfaceNumber = status;
1058
Anna Perel432367a2012-09-20 10:55:32 +03001059 status = -ENOMEM;
1060
Bryan Wuc6994e62009-06-03 09:17:58 -04001061 status = usb_interface_id(c, f);
Anna Perel432367a2012-09-20 10:55:32 +03001062 if (status < 0) {
1063 pr_err("%s: failed to allocate alt interface", __func__);
Bryan Wuc6994e62009-06-03 09:17:58 -04001064 goto fail;
Anna Perel432367a2012-09-20 10:55:32 +03001065 }
1066 microphone_as_interface_alt_0_desc.bInterfaceNumber = status;
1067 microphone_as_interface_alt_1_desc.bInterfaceNumber = status;
1068 ac_header_desc.baInterfaceNr[0] = status;
1069
1070 status = -ENODEV;
1071
1072 status = usb_interface_id(c, f);
1073 if (status < 0) {
1074 pr_err("%s: failed to allocate alt interface", __func__);
1075 goto fail;
1076 }
1077 speaker_as_interface_alt_0_desc.bInterfaceNumber = status;
1078 speaker_as_interface_alt_1_desc.bInterfaceNumber = status;
1079 ac_header_desc.baInterfaceNr[1] = status;
Bryan Wuc6994e62009-06-03 09:17:58 -04001080
1081 status = -ENODEV;
1082
1083 /* allocate instance-specific endpoints */
Anna Perel432367a2012-09-20 10:55:32 +03001084 ep = usb_ep_autoconfig(cdev->gadget, &microphone_as_ep_in_desc);
1085 if (!ep) {
1086 pr_err("%s: failed to autoconfig in endpoint", __func__);
Bryan Wuc6994e62009-06-03 09:17:58 -04001087 goto fail;
Anna Perel432367a2012-09-20 10:55:32 +03001088 }
1089 audio->in_ep = ep;
1090 ep->desc = &microphone_as_ep_in_desc;
1091 ep->driver_data = cdev;
Bryan Wuc6994e62009-06-03 09:17:58 -04001092
1093 status = -ENOMEM;
1094
Anna Perel432367a2012-09-20 10:55:32 +03001095 ep = usb_ep_autoconfig(cdev->gadget, &speaker_as_ep_out_desc);
1096 if (!ep) {
1097 pr_err("%s: failed to autoconfig out endpoint", __func__);
1098 goto fail;
1099 }
1100 audio->out_ep = ep;
1101 ep->desc = &speaker_as_ep_out_desc;
1102 ep->driver_data = cdev;
Felipe Balbief7f5842011-08-26 12:48:15 +03001103
Anna Perel432367a2012-09-20 10:55:32 +03001104 /* associate bEndpointAddress with usb_function */
1105 epaddr = microphone_as_ep_in_desc.bEndpointAddress & ~USB_DIR_IN;
1106 microphone_as_iso_in.id = epaddr;
1107
1108 epaddr = speaker_as_ep_out_desc.bEndpointAddress & ~USB_DIR_IN;
1109 speaker_as_iso_out.id = epaddr;
1110
1111 /* support all relevant hardware speeds. we expect that when
Bryan Wuc6994e62009-06-03 09:17:58 -04001112 * hardware is dual speed, all bulk-capable endpoints work at
1113 * both speeds
1114 */
Anna Perel432367a2012-09-20 10:55:32 +03001115
1116 /* copy descriptors, and track endpoint copies */
Bryan Wuc6994e62009-06-03 09:17:58 -04001117 if (gadget_is_dualspeed(c->cdev->gadget)) {
1118 c->highspeed = true;
1119 f->hs_descriptors = usb_copy_descriptors(f_audio_desc);
Anna Perel432367a2012-09-20 10:55:32 +03001120 } else {
1121 f->descriptors = usb_copy_descriptors(f_audio_desc);
Felipe Balbief7f5842011-08-26 12:48:15 +03001122 }
Bryan Wuc6994e62009-06-03 09:17:58 -04001123
1124 return 0;
1125
1126fail:
Bryan Wuc6994e62009-06-03 09:17:58 -04001127 return status;
1128}
1129
1130static void
1131f_audio_unbind(struct usb_configuration *c, struct usb_function *f)
1132{
Anna Perel432367a2012-09-20 10:55:32 +03001133 struct f_audio *audio = func_to_audio(f);
Bryan Wuc6994e62009-06-03 09:17:58 -04001134
1135 usb_free_descriptors(f->descriptors);
Sebastian Andrzej Siewior16a2f972011-04-11 20:44:30 +02001136 usb_free_descriptors(f->hs_descriptors);
Bryan Wuc6994e62009-06-03 09:17:58 -04001137 kfree(audio);
1138}
1139
1140/*-------------------------------------------------------------------------*/
1141
Laurent Pinchartb95cd7e2009-06-21 23:21:55 +02001142static int generic_set_cmd(struct usb_audio_control *con, u8 cmd, int value)
1143{
1144 con->data[cmd] = value;
1145
1146 return 0;
1147}
1148
1149static int generic_get_cmd(struct usb_audio_control *con, u8 cmd)
1150{
1151 return con->data[cmd];
1152}
1153
Bryan Wuc6994e62009-06-03 09:17:58 -04001154/* Todo: add more control selecotor dynamically */
Anna Perel432367a2012-09-20 10:55:32 +03001155int control_selector_init(struct f_audio *audio)
Bryan Wuc6994e62009-06-03 09:17:58 -04001156{
Anna Perel432367a2012-09-20 10:55:32 +03001157 INIT_LIST_HEAD(&audio->fu_cs);
1158 list_add(&microphone_fu_controls.list, &audio->fu_cs);
1159 list_add(&speaker_fu_controls.list, &audio->fu_cs);
Bryan Wuc6994e62009-06-03 09:17:58 -04001160
Anna Perel432367a2012-09-20 10:55:32 +03001161 INIT_LIST_HEAD(&microphone_fu_controls.control);
1162 list_add(&microphone_mute_control.list,
1163 &microphone_fu_controls.control);
1164 list_add(&microphone_volume_control.list,
1165 &microphone_fu_controls.control);
Bryan Wuc6994e62009-06-03 09:17:58 -04001166
Anna Perel432367a2012-09-20 10:55:32 +03001167 INIT_LIST_HEAD(&speaker_fu_controls.control);
1168 list_add(&speaker_mute_control.list,
1169 &speaker_fu_controls.control);
1170 list_add(&speaker_volume_control.list,
1171 &speaker_fu_controls.control);
1172
1173 microphone_volume_control.data[UAC__CUR] = 0xffc0;
1174 microphone_volume_control.data[UAC__MIN] = 0xe3a0;
1175 microphone_volume_control.data[UAC__MAX] = 0xfff0;
1176 microphone_volume_control.data[UAC__RES] = 0x0030;
1177
1178 speaker_volume_control.data[UAC__CUR] = 0xffc0;
1179 speaker_volume_control.data[UAC__MIN] = 0xe3a0;
1180 speaker_volume_control.data[UAC__MAX] = 0xfff0;
1181 speaker_volume_control.data[UAC__RES] = 0x0030;
1182
1183 INIT_LIST_HEAD(&audio->ep_cs);
1184 list_add(&speaker_as_iso_out.list, &audio->ep_cs);
1185 list_add(&microphone_as_iso_in.list, &audio->ep_cs);
1186
1187 INIT_LIST_HEAD(&microphone_as_iso_in.control);
1188 list_add(&microphone_sample_freq_control.list,
1189 &microphone_as_iso_in.control);
1190
1191 INIT_LIST_HEAD(&speaker_as_iso_out.control);
1192 list_add(&speaker_sample_freq_control.list,
1193 &speaker_as_iso_out.control);
Bryan Wuc6994e62009-06-03 09:17:58 -04001194
1195 return 0;
Anna Perel432367a2012-09-20 10:55:32 +03001196
Bryan Wuc6994e62009-06-03 09:17:58 -04001197}
1198
1199/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001200 * audio_bind_config - add USB audio function to a configuration
Anna Perel432367a2012-09-20 10:55:32 +03001201 * @c: the configuration to support the USB audio function
Bryan Wuc6994e62009-06-03 09:17:58 -04001202 * Context: single threaded during gadget setup
1203 *
1204 * Returns zero on success, else negative errno.
1205 */
Anna Perel432367a2012-09-20 10:55:32 +03001206int audio_bind_config(struct usb_configuration *c)
Bryan Wuc6994e62009-06-03 09:17:58 -04001207{
1208 struct f_audio *audio;
1209 int status;
1210
1211 /* allocate and initialize one new instance */
1212 audio = kzalloc(sizeof *audio, GFP_KERNEL);
1213 if (!audio)
1214 return -ENOMEM;
1215
Bryan Wuc6994e62009-06-03 09:17:58 -04001216 audio->card.gadget = c->cdev->gadget;
1217
1218 INIT_LIST_HEAD(&audio->play_queue);
Anna Perel432367a2012-09-20 10:55:32 +03001219 spin_lock_init(&audio->playback_lock);
Bryan Wuc6994e62009-06-03 09:17:58 -04001220
Anna Perel432367a2012-09-20 10:55:32 +03001221 INIT_LIST_HEAD(&audio->capture_queue);
1222 spin_lock_init(&audio->capture_lock);
Bryan Wuc6994e62009-06-03 09:17:58 -04001223
Anna Perel432367a2012-09-20 10:55:32 +03001224 audio->card.func.name = "audio";
Bryan Wuc6994e62009-06-03 09:17:58 -04001225 audio->card.func.strings = audio_strings;
1226 audio->card.func.bind = f_audio_bind;
1227 audio->card.func.unbind = f_audio_unbind;
1228 audio->card.func.set_alt = f_audio_set_alt;
1229 audio->card.func.setup = f_audio_setup;
1230 audio->card.func.disable = f_audio_disable;
Bryan Wuc6994e62009-06-03 09:17:58 -04001231
1232 control_selector_init(audio);
Bryan Wuc6994e62009-06-03 09:17:58 -04001233 INIT_WORK(&audio->playback_work, f_audio_playback_work);
Anna Perel432367a2012-09-20 10:55:32 +03001234 INIT_WORK(&audio->capture_work, f_audio_capture_work);
Bryan Wuc6994e62009-06-03 09:17:58 -04001235
Anna Perel432367a2012-09-20 10:55:32 +03001236 /* set up ASLA audio devices */
1237 status = gaudio_setup(&audio->card);
1238 if (status < 0)
Bryan Wuc6994e62009-06-03 09:17:58 -04001239 goto add_fail;
1240
Anna Perel432367a2012-09-20 10:55:32 +03001241 status = usb_add_function(c, &audio->card.func);
1242 if (status) {
1243 pr_err("%s: Failed to add usb audio function, err = %d",
1244 __func__, status);
1245 goto setup_fail;
1246 }
Bryan Wuc6994e62009-06-03 09:17:58 -04001247
1248 return status;
1249
1250add_fail:
Cliff Caifeef1d92009-12-09 22:28:39 -05001251 gaudio_cleanup();
Bryan Wuc6994e62009-06-03 09:17:58 -04001252setup_fail:
1253 kfree(audio);
1254 return status;
1255}