blob: bc23040c7790387b4a206b2814de5860f61a3b1c [file] [log] [blame]
Jassi Brar132fcb42012-02-02 22:01:34 +05301/*
2 * f_uac2.c -- USB Audio Class 2.0 Function
3 *
4 * Copyright (C) 2011
5 * Yadwinder Singh (yadi.brar01@gmail.com)
6 * Jaswinder Singh (jaswinder.singh@linaro.org)
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 */
13
14#include <linux/usb/audio.h>
15#include <linux/usb/audio-v2.h>
16#include <linux/platform_device.h>
17#include <linux/module.h>
18
19#include <sound/core.h>
20#include <sound/pcm.h>
21#include <sound/pcm_params.h>
22
23/* Playback(USB-IN) Default Stereo - Fl/Fr */
24static int p_chmask = 0x3;
25module_param(p_chmask, uint, S_IRUGO);
26MODULE_PARM_DESC(p_chmask, "Playback Channel Mask");
27
28/* Playback Default 48 KHz */
29static int p_srate = 48000;
30module_param(p_srate, uint, S_IRUGO);
31MODULE_PARM_DESC(p_srate, "Playback Sampling Rate");
32
33/* Playback Default 16bits/sample */
34static int p_ssize = 2;
35module_param(p_ssize, uint, S_IRUGO);
36MODULE_PARM_DESC(p_ssize, "Playback Sample Size(bytes)");
37
38/* Capture(USB-OUT) Default Stereo - Fl/Fr */
39static int c_chmask = 0x3;
40module_param(c_chmask, uint, S_IRUGO);
41MODULE_PARM_DESC(c_chmask, "Capture Channel Mask");
42
43/* Capture Default 64 KHz */
44static int c_srate = 64000;
45module_param(c_srate, uint, S_IRUGO);
46MODULE_PARM_DESC(c_srate, "Capture Sampling Rate");
47
48/* Capture Default 16bits/sample */
49static int c_ssize = 2;
50module_param(c_ssize, uint, S_IRUGO);
51MODULE_PARM_DESC(c_ssize, "Capture Sample Size(bytes)");
52
Jassi Brar132fcb42012-02-02 22:01:34 +053053/* Keep everyone on toes */
54#define USB_XFERS 2
55
56/*
57 * The driver implements a simple UAC_2 topology.
58 * USB-OUT -> IT_1 -> OT_3 -> ALSA_Capture
59 * ALSA_Playback -> IT_2 -> OT_4 -> USB-IN
60 * Capture and Playback sampling rates are independently
61 * controlled by two clock sources :
62 * CLK_5 := c_srate, and CLK_6 := p_srate
63 */
64#define USB_OUT_IT_ID 1
65#define IO_IN_IT_ID 2
66#define IO_OUT_OT_ID 3
67#define USB_IN_OT_ID 4
68#define USB_OUT_CLK_ID 5
69#define USB_IN_CLK_ID 6
70
71#define CONTROL_ABSENT 0
72#define CONTROL_RDONLY 1
73#define CONTROL_RDWR 3
74
75#define CLK_FREQ_CTRL 0
76#define CLK_VLD_CTRL 2
77
78#define COPY_CTRL 0
79#define CONN_CTRL 2
80#define OVRLD_CTRL 4
81#define CLSTR_CTRL 6
82#define UNFLW_CTRL 8
83#define OVFLW_CTRL 10
84
85const char *uac2_name = "snd_uac2";
86
87struct uac2_req {
88 struct uac2_rtd_params *pp; /* parent param */
89 struct usb_request *req;
90};
91
92struct uac2_rtd_params {
Jassi Brareb127cb2013-05-30 18:23:33 +053093 struct snd_uac2_chip *uac2; /* parent chip */
Jassi Brar132fcb42012-02-02 22:01:34 +053094 bool ep_enabled; /* if the ep is enabled */
95 /* Size of the ring buffer */
96 size_t dma_bytes;
97 unsigned char *dma_area;
98
99 struct snd_pcm_substream *ss;
100
101 /* Ring buffer */
102 ssize_t hw_ptr;
103
104 void *rbuf;
105
106 size_t period_size;
107
108 unsigned max_psize;
109 struct uac2_req ureq[USB_XFERS];
110
111 spinlock_t lock;
112};
113
114struct snd_uac2_chip {
115 struct platform_device pdev;
116 struct platform_driver pdrv;
117
118 struct uac2_rtd_params p_prm;
119 struct uac2_rtd_params c_prm;
120
121 struct snd_card *card;
122 struct snd_pcm *pcm;
123};
124
125#define BUFF_SIZE_MAX (PAGE_SIZE * 16)
126#define PRD_SIZE_MAX PAGE_SIZE
127#define MIN_PERIODS 4
128
129static struct snd_pcm_hardware uac2_pcm_hardware = {
130 .info = SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BLOCK_TRANSFER
131 | SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID
132 | SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME,
133 .rates = SNDRV_PCM_RATE_CONTINUOUS,
134 .periods_max = BUFF_SIZE_MAX / PRD_SIZE_MAX,
135 .buffer_bytes_max = BUFF_SIZE_MAX,
136 .period_bytes_max = PRD_SIZE_MAX,
137 .periods_min = MIN_PERIODS,
138};
139
140struct audio_dev {
Sebastian Andrzej Siewior4ce63572012-10-22 22:15:08 +0200141 u8 ac_intf, ac_alt;
142 u8 as_out_intf, as_out_alt;
143 u8 as_in_intf, as_in_alt;
Jassi Brar132fcb42012-02-02 22:01:34 +0530144
145 struct usb_ep *in_ep, *out_ep;
146 struct usb_function func;
147
148 /* The ALSA Sound Card it represents on the USB-Client side */
149 struct snd_uac2_chip uac2;
150};
151
152static struct audio_dev *agdev_g;
153
154static inline
155struct audio_dev *func_to_agdev(struct usb_function *f)
156{
157 return container_of(f, struct audio_dev, func);
158}
159
160static inline
161struct audio_dev *uac2_to_agdev(struct snd_uac2_chip *u)
162{
163 return container_of(u, struct audio_dev, uac2);
164}
165
166static inline
167struct snd_uac2_chip *pdev_to_uac2(struct platform_device *p)
168{
169 return container_of(p, struct snd_uac2_chip, pdev);
170}
171
172static inline
Jassi Brar132fcb42012-02-02 22:01:34 +0530173uint num_channels(uint chanmask)
174{
175 uint num = 0;
176
177 while (chanmask) {
178 num += (chanmask & 1);
179 chanmask >>= 1;
180 }
181
182 return num;
183}
184
185static void
186agdev_iso_complete(struct usb_ep *ep, struct usb_request *req)
187{
188 unsigned pending;
189 unsigned long flags;
190 bool update_alsa = false;
191 unsigned char *src, *dst;
192 int status = req->status;
193 struct uac2_req *ur = req->context;
194 struct snd_pcm_substream *substream;
195 struct uac2_rtd_params *prm = ur->pp;
Jassi Brareb127cb2013-05-30 18:23:33 +0530196 struct snd_uac2_chip *uac2 = prm->uac2;
Jassi Brar132fcb42012-02-02 22:01:34 +0530197
198 /* i/f shutting down */
199 if (!prm->ep_enabled)
200 return;
201
202 /*
203 * We can't really do much about bad xfers.
204 * Afterall, the ISOCH xfers could fail legitimately.
205 */
206 if (status)
207 pr_debug("%s: iso_complete status(%d) %d/%d\n",
208 __func__, status, req->actual, req->length);
209
210 substream = prm->ss;
211
212 /* Do nothing if ALSA isn't active */
213 if (!substream)
214 goto exit;
215
216 spin_lock_irqsave(&prm->lock, flags);
217
218 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
219 src = prm->dma_area + prm->hw_ptr;
220 req->actual = req->length;
221 dst = req->buf;
222 } else {
223 dst = prm->dma_area + prm->hw_ptr;
224 src = req->buf;
225 }
226
227 pending = prm->hw_ptr % prm->period_size;
228 pending += req->actual;
229 if (pending >= prm->period_size)
230 update_alsa = true;
231
232 prm->hw_ptr = (prm->hw_ptr + req->actual) % prm->dma_bytes;
233
234 spin_unlock_irqrestore(&prm->lock, flags);
235
236 /* Pack USB load in ALSA ring buffer */
237 memcpy(dst, src, req->actual);
238exit:
239 if (usb_ep_queue(ep, req, GFP_ATOMIC))
240 dev_err(&uac2->pdev.dev, "%d Error!\n", __LINE__);
241
242 if (update_alsa)
243 snd_pcm_period_elapsed(substream);
244
245 return;
246}
247
248static int
249uac2_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
250{
251 struct snd_uac2_chip *uac2 = snd_pcm_substream_chip(substream);
Jassi Brar132fcb42012-02-02 22:01:34 +0530252 struct uac2_rtd_params *prm;
253 unsigned long flags;
Jassi Brar132fcb42012-02-02 22:01:34 +0530254 int err = 0;
255
Wei Yongjunb6424352012-12-02 05:33:08 -0500256 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
Jassi Brar132fcb42012-02-02 22:01:34 +0530257 prm = &uac2->p_prm;
Wei Yongjunb6424352012-12-02 05:33:08 -0500258 else
Jassi Brar132fcb42012-02-02 22:01:34 +0530259 prm = &uac2->c_prm;
Jassi Brar132fcb42012-02-02 22:01:34 +0530260
261 spin_lock_irqsave(&prm->lock, flags);
262
263 /* Reset */
264 prm->hw_ptr = 0;
265
266 switch (cmd) {
267 case SNDRV_PCM_TRIGGER_START:
268 case SNDRV_PCM_TRIGGER_RESUME:
269 prm->ss = substream;
270 break;
271 case SNDRV_PCM_TRIGGER_STOP:
272 case SNDRV_PCM_TRIGGER_SUSPEND:
273 prm->ss = NULL;
274 break;
275 default:
276 err = -EINVAL;
277 }
278
279 spin_unlock_irqrestore(&prm->lock, flags);
280
281 /* Clear buffer after Play stops */
282 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK && !prm->ss)
283 memset(prm->rbuf, 0, prm->max_psize * USB_XFERS);
284
285 return err;
286}
287
288static snd_pcm_uframes_t uac2_pcm_pointer(struct snd_pcm_substream *substream)
289{
290 struct snd_uac2_chip *uac2 = snd_pcm_substream_chip(substream);
291 struct uac2_rtd_params *prm;
292
293 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
294 prm = &uac2->p_prm;
295 else
296 prm = &uac2->c_prm;
297
298 return bytes_to_frames(substream->runtime, prm->hw_ptr);
299}
300
301static int uac2_pcm_hw_params(struct snd_pcm_substream *substream,
302 struct snd_pcm_hw_params *hw_params)
303{
304 struct snd_uac2_chip *uac2 = snd_pcm_substream_chip(substream);
305 struct uac2_rtd_params *prm;
306 int err;
307
308 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
309 prm = &uac2->p_prm;
310 else
311 prm = &uac2->c_prm;
312
313 err = snd_pcm_lib_malloc_pages(substream,
314 params_buffer_bytes(hw_params));
315 if (err >= 0) {
316 prm->dma_bytes = substream->runtime->dma_bytes;
317 prm->dma_area = substream->runtime->dma_area;
318 prm->period_size = params_period_bytes(hw_params);
319 }
320
321 return err;
322}
323
324static int uac2_pcm_hw_free(struct snd_pcm_substream *substream)
325{
326 struct snd_uac2_chip *uac2 = snd_pcm_substream_chip(substream);
327 struct uac2_rtd_params *prm;
328
329 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
330 prm = &uac2->p_prm;
331 else
332 prm = &uac2->c_prm;
333
334 prm->dma_area = NULL;
335 prm->dma_bytes = 0;
336 prm->period_size = 0;
337
338 return snd_pcm_lib_free_pages(substream);
339}
340
341static int uac2_pcm_open(struct snd_pcm_substream *substream)
342{
343 struct snd_uac2_chip *uac2 = snd_pcm_substream_chip(substream);
344 struct snd_pcm_runtime *runtime = substream->runtime;
345
346 runtime->hw = uac2_pcm_hardware;
347
348 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
349 spin_lock_init(&uac2->p_prm.lock);
350 runtime->hw.rate_min = p_srate;
351 runtime->hw.formats = SNDRV_PCM_FMTBIT_S16_LE; /* ! p_ssize ! */
352 runtime->hw.channels_min = num_channels(p_chmask);
353 runtime->hw.period_bytes_min = 2 * uac2->p_prm.max_psize
354 / runtime->hw.periods_min;
355 } else {
356 spin_lock_init(&uac2->c_prm.lock);
357 runtime->hw.rate_min = c_srate;
358 runtime->hw.formats = SNDRV_PCM_FMTBIT_S16_LE; /* ! c_ssize ! */
359 runtime->hw.channels_min = num_channels(c_chmask);
360 runtime->hw.period_bytes_min = 2 * uac2->c_prm.max_psize
361 / runtime->hw.periods_min;
362 }
363
364 runtime->hw.rate_max = runtime->hw.rate_min;
365 runtime->hw.channels_max = runtime->hw.channels_min;
366
367 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
368
369 return 0;
370}
371
372/* ALSA cries without these function pointers */
373static int uac2_pcm_null(struct snd_pcm_substream *substream)
374{
375 return 0;
376}
377
378static struct snd_pcm_ops uac2_pcm_ops = {
379 .open = uac2_pcm_open,
380 .close = uac2_pcm_null,
381 .ioctl = snd_pcm_lib_ioctl,
382 .hw_params = uac2_pcm_hw_params,
383 .hw_free = uac2_pcm_hw_free,
384 .trigger = uac2_pcm_trigger,
385 .pointer = uac2_pcm_pointer,
386 .prepare = uac2_pcm_null,
387};
388
Bill Pemberton41ac7b32012-11-19 13:21:48 -0500389static int snd_uac2_probe(struct platform_device *pdev)
Jassi Brar132fcb42012-02-02 22:01:34 +0530390{
391 struct snd_uac2_chip *uac2 = pdev_to_uac2(pdev);
392 struct snd_card *card;
393 struct snd_pcm *pcm;
394 int err;
395
396 /* Choose any slot, with no id */
Takashi Iwai13345092014-01-29 15:04:31 +0100397 err = snd_card_new(&pdev->dev, -1, NULL, THIS_MODULE, 0, &card);
Jassi Brar132fcb42012-02-02 22:01:34 +0530398 if (err < 0)
399 return err;
400
401 uac2->card = card;
402
403 /*
404 * Create first PCM device
405 * Create a substream only for non-zero channel streams
406 */
407 err = snd_pcm_new(uac2->card, "UAC2 PCM", 0,
408 p_chmask ? 1 : 0, c_chmask ? 1 : 0, &pcm);
409 if (err < 0)
410 goto snd_fail;
411
412 strcpy(pcm->name, "UAC2 PCM");
413 pcm->private_data = uac2;
414
415 uac2->pcm = pcm;
416
417 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &uac2_pcm_ops);
418 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &uac2_pcm_ops);
419
420 strcpy(card->driver, "UAC2_Gadget");
421 strcpy(card->shortname, "UAC2_Gadget");
422 sprintf(card->longname, "UAC2_Gadget %i", pdev->id);
423
Jassi Brar132fcb42012-02-02 22:01:34 +0530424 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS,
425 snd_dma_continuous_data(GFP_KERNEL), 0, BUFF_SIZE_MAX);
426
427 err = snd_card_register(card);
428 if (!err) {
429 platform_set_drvdata(pdev, card);
430 return 0;
431 }
432
433snd_fail:
434 snd_card_free(card);
435
436 uac2->pcm = NULL;
437 uac2->card = NULL;
438
439 return err;
440}
441
Sebastian Andrzej Siewior1e1a27c2012-09-06 20:11:02 +0200442static int snd_uac2_remove(struct platform_device *pdev)
Jassi Brar132fcb42012-02-02 22:01:34 +0530443{
444 struct snd_card *card = platform_get_drvdata(pdev);
445
Jassi Brar132fcb42012-02-02 22:01:34 +0530446 if (card)
447 return snd_card_free(card);
448
449 return 0;
450}
451
452static int alsa_uac2_init(struct audio_dev *agdev)
453{
454 struct snd_uac2_chip *uac2 = &agdev->uac2;
455 int err;
456
457 uac2->pdrv.probe = snd_uac2_probe;
458 uac2->pdrv.remove = snd_uac2_remove;
459 uac2->pdrv.driver.name = uac2_name;
460
461 uac2->pdev.id = 0;
462 uac2->pdev.name = uac2_name;
463
464 /* Register snd_uac2 driver */
465 err = platform_driver_register(&uac2->pdrv);
466 if (err)
467 return err;
468
469 /* Register snd_uac2 device */
470 err = platform_device_register(&uac2->pdev);
471 if (err)
472 platform_driver_unregister(&uac2->pdrv);
473
474 return err;
475}
476
477static void alsa_uac2_exit(struct audio_dev *agdev)
478{
479 struct snd_uac2_chip *uac2 = &agdev->uac2;
480
481 platform_driver_unregister(&uac2->pdrv);
482 platform_device_unregister(&uac2->pdev);
483}
484
485
486/* --------- USB Function Interface ------------- */
487
488enum {
489 STR_ASSOC,
490 STR_IF_CTRL,
491 STR_CLKSRC_IN,
492 STR_CLKSRC_OUT,
493 STR_USB_IT,
494 STR_IO_IT,
495 STR_USB_OT,
496 STR_IO_OT,
497 STR_AS_OUT_ALT0,
498 STR_AS_OUT_ALT1,
499 STR_AS_IN_ALT0,
500 STR_AS_IN_ALT1,
501};
502
Jassi Brar132fcb42012-02-02 22:01:34 +0530503static char clksrc_in[8];
504static char clksrc_out[8];
Jassi Brar132fcb42012-02-02 22:01:34 +0530505
506static struct usb_string strings_fn[] = {
Sebastian Andrzej Siewiorb36c3472012-10-22 22:15:09 +0200507 [STR_ASSOC].s = "Source/Sink",
508 [STR_IF_CTRL].s = "Topology Control",
Jassi Brar132fcb42012-02-02 22:01:34 +0530509 [STR_CLKSRC_IN].s = clksrc_in,
510 [STR_CLKSRC_OUT].s = clksrc_out,
Sebastian Andrzej Siewiorb36c3472012-10-22 22:15:09 +0200511 [STR_USB_IT].s = "USBH Out",
512 [STR_IO_IT].s = "USBD Out",
513 [STR_USB_OT].s = "USBH In",
514 [STR_IO_OT].s = "USBD In",
515 [STR_AS_OUT_ALT0].s = "Playback Inactive",
516 [STR_AS_OUT_ALT1].s = "Playback Active",
517 [STR_AS_IN_ALT0].s = "Capture Inactive",
518 [STR_AS_IN_ALT1].s = "Capture Active",
Jassi Brar132fcb42012-02-02 22:01:34 +0530519 { },
520};
521
522static struct usb_gadget_strings str_fn = {
523 .language = 0x0409, /* en-us */
524 .strings = strings_fn,
525};
526
527static struct usb_gadget_strings *fn_strings[] = {
528 &str_fn,
529 NULL,
530};
531
532static struct usb_qualifier_descriptor devqual_desc = {
533 .bLength = sizeof devqual_desc,
534 .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
535
536 .bcdUSB = cpu_to_le16(0x200),
537 .bDeviceClass = USB_CLASS_MISC,
538 .bDeviceSubClass = 0x02,
539 .bDeviceProtocol = 0x01,
540 .bNumConfigurations = 1,
541 .bRESERVED = 0,
542};
543
544static struct usb_interface_assoc_descriptor iad_desc = {
545 .bLength = sizeof iad_desc,
546 .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION,
547
548 .bFirstInterface = 0,
549 .bInterfaceCount = 3,
550 .bFunctionClass = USB_CLASS_AUDIO,
551 .bFunctionSubClass = UAC2_FUNCTION_SUBCLASS_UNDEFINED,
552 .bFunctionProtocol = UAC_VERSION_2,
553};
554
555/* Audio Control Interface */
556static struct usb_interface_descriptor std_ac_if_desc = {
557 .bLength = sizeof std_ac_if_desc,
558 .bDescriptorType = USB_DT_INTERFACE,
559
560 .bAlternateSetting = 0,
561 .bNumEndpoints = 0,
562 .bInterfaceClass = USB_CLASS_AUDIO,
563 .bInterfaceSubClass = USB_SUBCLASS_AUDIOCONTROL,
564 .bInterfaceProtocol = UAC_VERSION_2,
565};
566
567/* Clock source for IN traffic */
568struct uac_clock_source_descriptor in_clk_src_desc = {
569 .bLength = sizeof in_clk_src_desc,
570 .bDescriptorType = USB_DT_CS_INTERFACE,
571
572 .bDescriptorSubtype = UAC2_CLOCK_SOURCE,
573 .bClockID = USB_IN_CLK_ID,
574 .bmAttributes = UAC_CLOCK_SOURCE_TYPE_INT_FIXED,
575 .bmControls = (CONTROL_RDONLY << CLK_FREQ_CTRL),
576 .bAssocTerminal = 0,
577};
578
579/* Clock source for OUT traffic */
580struct uac_clock_source_descriptor out_clk_src_desc = {
581 .bLength = sizeof out_clk_src_desc,
582 .bDescriptorType = USB_DT_CS_INTERFACE,
583
584 .bDescriptorSubtype = UAC2_CLOCK_SOURCE,
585 .bClockID = USB_OUT_CLK_ID,
586 .bmAttributes = UAC_CLOCK_SOURCE_TYPE_INT_FIXED,
587 .bmControls = (CONTROL_RDONLY << CLK_FREQ_CTRL),
588 .bAssocTerminal = 0,
589};
590
591/* Input Terminal for USB_OUT */
592struct uac2_input_terminal_descriptor usb_out_it_desc = {
593 .bLength = sizeof usb_out_it_desc,
594 .bDescriptorType = USB_DT_CS_INTERFACE,
595
596 .bDescriptorSubtype = UAC_INPUT_TERMINAL,
597 .bTerminalID = USB_OUT_IT_ID,
598 .wTerminalType = cpu_to_le16(UAC_TERMINAL_STREAMING),
599 .bAssocTerminal = 0,
600 .bCSourceID = USB_OUT_CLK_ID,
601 .iChannelNames = 0,
602 .bmControls = (CONTROL_RDWR << COPY_CTRL),
603};
604
605/* Input Terminal for I/O-In */
606struct uac2_input_terminal_descriptor io_in_it_desc = {
607 .bLength = sizeof io_in_it_desc,
608 .bDescriptorType = USB_DT_CS_INTERFACE,
609
610 .bDescriptorSubtype = UAC_INPUT_TERMINAL,
611 .bTerminalID = IO_IN_IT_ID,
612 .wTerminalType = cpu_to_le16(UAC_INPUT_TERMINAL_UNDEFINED),
613 .bAssocTerminal = 0,
614 .bCSourceID = USB_IN_CLK_ID,
615 .iChannelNames = 0,
616 .bmControls = (CONTROL_RDWR << COPY_CTRL),
617};
618
619/* Ouput Terminal for USB_IN */
620struct uac2_output_terminal_descriptor usb_in_ot_desc = {
621 .bLength = sizeof usb_in_ot_desc,
622 .bDescriptorType = USB_DT_CS_INTERFACE,
623
624 .bDescriptorSubtype = UAC_OUTPUT_TERMINAL,
625 .bTerminalID = USB_IN_OT_ID,
626 .wTerminalType = cpu_to_le16(UAC_TERMINAL_STREAMING),
627 .bAssocTerminal = 0,
628 .bSourceID = IO_IN_IT_ID,
629 .bCSourceID = USB_IN_CLK_ID,
630 .bmControls = (CONTROL_RDWR << COPY_CTRL),
631};
632
633/* Ouput Terminal for I/O-Out */
634struct uac2_output_terminal_descriptor io_out_ot_desc = {
635 .bLength = sizeof io_out_ot_desc,
636 .bDescriptorType = USB_DT_CS_INTERFACE,
637
638 .bDescriptorSubtype = UAC_OUTPUT_TERMINAL,
639 .bTerminalID = IO_OUT_OT_ID,
640 .wTerminalType = cpu_to_le16(UAC_OUTPUT_TERMINAL_UNDEFINED),
641 .bAssocTerminal = 0,
642 .bSourceID = USB_OUT_IT_ID,
643 .bCSourceID = USB_OUT_CLK_ID,
644 .bmControls = (CONTROL_RDWR << COPY_CTRL),
645};
646
647struct uac2_ac_header_descriptor ac_hdr_desc = {
648 .bLength = sizeof ac_hdr_desc,
649 .bDescriptorType = USB_DT_CS_INTERFACE,
650
651 .bDescriptorSubtype = UAC_MS_HEADER,
652 .bcdADC = cpu_to_le16(0x200),
653 .bCategory = UAC2_FUNCTION_IO_BOX,
654 .wTotalLength = sizeof in_clk_src_desc + sizeof out_clk_src_desc
655 + sizeof usb_out_it_desc + sizeof io_in_it_desc
656 + sizeof usb_in_ot_desc + sizeof io_out_ot_desc,
657 .bmControls = 0,
658};
659
660/* Audio Streaming OUT Interface - Alt0 */
661static struct usb_interface_descriptor std_as_out_if0_desc = {
662 .bLength = sizeof std_as_out_if0_desc,
663 .bDescriptorType = USB_DT_INTERFACE,
664
665 .bAlternateSetting = 0,
666 .bNumEndpoints = 0,
667 .bInterfaceClass = USB_CLASS_AUDIO,
668 .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
669 .bInterfaceProtocol = UAC_VERSION_2,
670};
671
672/* Audio Streaming OUT Interface - Alt1 */
673static struct usb_interface_descriptor std_as_out_if1_desc = {
674 .bLength = sizeof std_as_out_if1_desc,
675 .bDescriptorType = USB_DT_INTERFACE,
676
677 .bAlternateSetting = 1,
678 .bNumEndpoints = 1,
679 .bInterfaceClass = USB_CLASS_AUDIO,
680 .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
681 .bInterfaceProtocol = UAC_VERSION_2,
682};
683
684/* Audio Stream OUT Intface Desc */
685struct uac2_as_header_descriptor as_out_hdr_desc = {
686 .bLength = sizeof as_out_hdr_desc,
687 .bDescriptorType = USB_DT_CS_INTERFACE,
688
689 .bDescriptorSubtype = UAC_AS_GENERAL,
690 .bTerminalLink = USB_OUT_IT_ID,
691 .bmControls = 0,
692 .bFormatType = UAC_FORMAT_TYPE_I,
693 .bmFormats = cpu_to_le32(UAC_FORMAT_TYPE_I_PCM),
694 .iChannelNames = 0,
695};
696
697/* Audio USB_OUT Format */
698struct uac2_format_type_i_descriptor as_out_fmt1_desc = {
699 .bLength = sizeof as_out_fmt1_desc,
700 .bDescriptorType = USB_DT_CS_INTERFACE,
701 .bDescriptorSubtype = UAC_FORMAT_TYPE,
702 .bFormatType = UAC_FORMAT_TYPE_I,
703};
704
705/* STD AS ISO OUT Endpoint */
706struct usb_endpoint_descriptor fs_epout_desc = {
707 .bLength = USB_DT_ENDPOINT_SIZE,
708 .bDescriptorType = USB_DT_ENDPOINT,
709
710 .bEndpointAddress = USB_DIR_OUT,
711 .bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC,
712 .bInterval = 1,
713};
714
715struct usb_endpoint_descriptor hs_epout_desc = {
716 .bLength = USB_DT_ENDPOINT_SIZE,
717 .bDescriptorType = USB_DT_ENDPOINT,
718
719 .bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC,
720 .bInterval = 4,
721};
722
723/* CS AS ISO OUT Endpoint */
724static struct uac2_iso_endpoint_descriptor as_iso_out_desc = {
725 .bLength = sizeof as_iso_out_desc,
726 .bDescriptorType = USB_DT_CS_ENDPOINT,
727
728 .bDescriptorSubtype = UAC_EP_GENERAL,
729 .bmAttributes = 0,
730 .bmControls = 0,
731 .bLockDelayUnits = 0,
732 .wLockDelay = 0,
733};
734
735/* Audio Streaming IN Interface - Alt0 */
736static struct usb_interface_descriptor std_as_in_if0_desc = {
737 .bLength = sizeof std_as_in_if0_desc,
738 .bDescriptorType = USB_DT_INTERFACE,
739
740 .bAlternateSetting = 0,
741 .bNumEndpoints = 0,
742 .bInterfaceClass = USB_CLASS_AUDIO,
743 .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
744 .bInterfaceProtocol = UAC_VERSION_2,
745};
746
747/* Audio Streaming IN Interface - Alt1 */
748static struct usb_interface_descriptor std_as_in_if1_desc = {
749 .bLength = sizeof std_as_in_if1_desc,
750 .bDescriptorType = USB_DT_INTERFACE,
751
752 .bAlternateSetting = 1,
753 .bNumEndpoints = 1,
754 .bInterfaceClass = USB_CLASS_AUDIO,
755 .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
756 .bInterfaceProtocol = UAC_VERSION_2,
757};
758
759/* Audio Stream IN Intface Desc */
760struct uac2_as_header_descriptor as_in_hdr_desc = {
761 .bLength = sizeof as_in_hdr_desc,
762 .bDescriptorType = USB_DT_CS_INTERFACE,
763
764 .bDescriptorSubtype = UAC_AS_GENERAL,
765 .bTerminalLink = USB_IN_OT_ID,
766 .bmControls = 0,
767 .bFormatType = UAC_FORMAT_TYPE_I,
768 .bmFormats = cpu_to_le32(UAC_FORMAT_TYPE_I_PCM),
769 .iChannelNames = 0,
770};
771
772/* Audio USB_IN Format */
773struct uac2_format_type_i_descriptor as_in_fmt1_desc = {
774 .bLength = sizeof as_in_fmt1_desc,
775 .bDescriptorType = USB_DT_CS_INTERFACE,
776 .bDescriptorSubtype = UAC_FORMAT_TYPE,
777 .bFormatType = UAC_FORMAT_TYPE_I,
778};
779
780/* STD AS ISO IN Endpoint */
781struct usb_endpoint_descriptor fs_epin_desc = {
782 .bLength = USB_DT_ENDPOINT_SIZE,
783 .bDescriptorType = USB_DT_ENDPOINT,
784
785 .bEndpointAddress = USB_DIR_IN,
786 .bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC,
787 .bInterval = 1,
788};
789
790struct usb_endpoint_descriptor hs_epin_desc = {
791 .bLength = USB_DT_ENDPOINT_SIZE,
792 .bDescriptorType = USB_DT_ENDPOINT,
793
794 .bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC,
795 .bInterval = 4,
796};
797
798/* CS AS ISO IN Endpoint */
799static struct uac2_iso_endpoint_descriptor as_iso_in_desc = {
800 .bLength = sizeof as_iso_in_desc,
801 .bDescriptorType = USB_DT_CS_ENDPOINT,
802
803 .bDescriptorSubtype = UAC_EP_GENERAL,
804 .bmAttributes = 0,
805 .bmControls = 0,
806 .bLockDelayUnits = 0,
807 .wLockDelay = 0,
808};
809
810static struct usb_descriptor_header *fs_audio_desc[] = {
811 (struct usb_descriptor_header *)&iad_desc,
812 (struct usb_descriptor_header *)&std_ac_if_desc,
813
814 (struct usb_descriptor_header *)&ac_hdr_desc,
815 (struct usb_descriptor_header *)&in_clk_src_desc,
816 (struct usb_descriptor_header *)&out_clk_src_desc,
817 (struct usb_descriptor_header *)&usb_out_it_desc,
818 (struct usb_descriptor_header *)&io_in_it_desc,
819 (struct usb_descriptor_header *)&usb_in_ot_desc,
820 (struct usb_descriptor_header *)&io_out_ot_desc,
821
822 (struct usb_descriptor_header *)&std_as_out_if0_desc,
823 (struct usb_descriptor_header *)&std_as_out_if1_desc,
824
825 (struct usb_descriptor_header *)&as_out_hdr_desc,
826 (struct usb_descriptor_header *)&as_out_fmt1_desc,
827 (struct usb_descriptor_header *)&fs_epout_desc,
828 (struct usb_descriptor_header *)&as_iso_out_desc,
829
830 (struct usb_descriptor_header *)&std_as_in_if0_desc,
831 (struct usb_descriptor_header *)&std_as_in_if1_desc,
832
833 (struct usb_descriptor_header *)&as_in_hdr_desc,
834 (struct usb_descriptor_header *)&as_in_fmt1_desc,
835 (struct usb_descriptor_header *)&fs_epin_desc,
836 (struct usb_descriptor_header *)&as_iso_in_desc,
837 NULL,
838};
839
840static struct usb_descriptor_header *hs_audio_desc[] = {
841 (struct usb_descriptor_header *)&iad_desc,
842 (struct usb_descriptor_header *)&std_ac_if_desc,
843
844 (struct usb_descriptor_header *)&ac_hdr_desc,
845 (struct usb_descriptor_header *)&in_clk_src_desc,
846 (struct usb_descriptor_header *)&out_clk_src_desc,
847 (struct usb_descriptor_header *)&usb_out_it_desc,
848 (struct usb_descriptor_header *)&io_in_it_desc,
849 (struct usb_descriptor_header *)&usb_in_ot_desc,
850 (struct usb_descriptor_header *)&io_out_ot_desc,
851
852 (struct usb_descriptor_header *)&std_as_out_if0_desc,
853 (struct usb_descriptor_header *)&std_as_out_if1_desc,
854
855 (struct usb_descriptor_header *)&as_out_hdr_desc,
856 (struct usb_descriptor_header *)&as_out_fmt1_desc,
857 (struct usb_descriptor_header *)&hs_epout_desc,
858 (struct usb_descriptor_header *)&as_iso_out_desc,
859
860 (struct usb_descriptor_header *)&std_as_in_if0_desc,
861 (struct usb_descriptor_header *)&std_as_in_if1_desc,
862
863 (struct usb_descriptor_header *)&as_in_hdr_desc,
864 (struct usb_descriptor_header *)&as_in_fmt1_desc,
865 (struct usb_descriptor_header *)&hs_epin_desc,
866 (struct usb_descriptor_header *)&as_iso_in_desc,
867 NULL,
868};
869
870struct cntrl_cur_lay3 {
871 __u32 dCUR;
872};
873
874struct cntrl_range_lay3 {
875 __u16 wNumSubRanges;
876 __u32 dMIN;
877 __u32 dMAX;
878 __u32 dRES;
879} __packed;
880
881static inline void
882free_ep(struct uac2_rtd_params *prm, struct usb_ep *ep)
883{
Jassi Brareb127cb2013-05-30 18:23:33 +0530884 struct snd_uac2_chip *uac2 = prm->uac2;
Jassi Brar132fcb42012-02-02 22:01:34 +0530885 int i;
886
887 prm->ep_enabled = false;
888
889 for (i = 0; i < USB_XFERS; i++) {
890 if (prm->ureq[i].req) {
891 usb_ep_dequeue(ep, prm->ureq[i].req);
892 usb_ep_free_request(ep, prm->ureq[i].req);
893 prm->ureq[i].req = NULL;
894 }
895 }
896
897 if (usb_ep_disable(ep))
898 dev_err(&uac2->pdev.dev,
899 "%s:%d Error!\n", __func__, __LINE__);
900}
901
902static int __init
903afunc_bind(struct usb_configuration *cfg, struct usb_function *fn)
904{
905 struct audio_dev *agdev = func_to_agdev(fn);
906 struct snd_uac2_chip *uac2 = &agdev->uac2;
907 struct usb_composite_dev *cdev = cfg->cdev;
908 struct usb_gadget *gadget = cdev->gadget;
909 struct uac2_rtd_params *prm;
910 int ret;
911
912 ret = usb_interface_id(cfg, fn);
913 if (ret < 0) {
914 dev_err(&uac2->pdev.dev,
915 "%s:%d Error!\n", __func__, __LINE__);
916 return ret;
917 }
918 std_ac_if_desc.bInterfaceNumber = ret;
Sebastian Andrzej Siewior4ce63572012-10-22 22:15:08 +0200919 agdev->ac_intf = ret;
920 agdev->ac_alt = 0;
Jassi Brar132fcb42012-02-02 22:01:34 +0530921
922 ret = usb_interface_id(cfg, fn);
923 if (ret < 0) {
924 dev_err(&uac2->pdev.dev,
925 "%s:%d Error!\n", __func__, __LINE__);
926 return ret;
927 }
928 std_as_out_if0_desc.bInterfaceNumber = ret;
929 std_as_out_if1_desc.bInterfaceNumber = ret;
Sebastian Andrzej Siewior4ce63572012-10-22 22:15:08 +0200930 agdev->as_out_intf = ret;
931 agdev->as_out_alt = 0;
Jassi Brar132fcb42012-02-02 22:01:34 +0530932
933 ret = usb_interface_id(cfg, fn);
934 if (ret < 0) {
935 dev_err(&uac2->pdev.dev,
936 "%s:%d Error!\n", __func__, __LINE__);
937 return ret;
938 }
939 std_as_in_if0_desc.bInterfaceNumber = ret;
940 std_as_in_if1_desc.bInterfaceNumber = ret;
Sebastian Andrzej Siewior4ce63572012-10-22 22:15:08 +0200941 agdev->as_in_intf = ret;
942 agdev->as_in_alt = 0;
Jassi Brar132fcb42012-02-02 22:01:34 +0530943
944 agdev->out_ep = usb_ep_autoconfig(gadget, &fs_epout_desc);
Sebastian Andrzej Siewior391aa852012-10-22 22:14:59 +0200945 if (!agdev->out_ep) {
Jassi Brar132fcb42012-02-02 22:01:34 +0530946 dev_err(&uac2->pdev.dev,
947 "%s:%d Error!\n", __func__, __LINE__);
Sebastian Andrzej Siewior391aa852012-10-22 22:14:59 +0200948 goto err;
949 }
Jassi Brar132fcb42012-02-02 22:01:34 +0530950 agdev->out_ep->driver_data = agdev;
951
952 agdev->in_ep = usb_ep_autoconfig(gadget, &fs_epin_desc);
Sebastian Andrzej Siewior391aa852012-10-22 22:14:59 +0200953 if (!agdev->in_ep) {
Jassi Brar132fcb42012-02-02 22:01:34 +0530954 dev_err(&uac2->pdev.dev,
955 "%s:%d Error!\n", __func__, __LINE__);
Sebastian Andrzej Siewior391aa852012-10-22 22:14:59 +0200956 goto err;
957 }
Jassi Brar132fcb42012-02-02 22:01:34 +0530958 agdev->in_ep->driver_data = agdev;
959
Jassi Brareb127cb2013-05-30 18:23:33 +0530960 uac2->p_prm.uac2 = uac2;
961 uac2->c_prm.uac2 = uac2;
962
Jassi Brar132fcb42012-02-02 22:01:34 +0530963 hs_epout_desc.bEndpointAddress = fs_epout_desc.bEndpointAddress;
964 hs_epout_desc.wMaxPacketSize = fs_epout_desc.wMaxPacketSize;
965 hs_epin_desc.bEndpointAddress = fs_epin_desc.bEndpointAddress;
966 hs_epin_desc.wMaxPacketSize = fs_epin_desc.wMaxPacketSize;
967
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200968 ret = usb_assign_descriptors(fn, fs_audio_desc, hs_audio_desc, NULL);
969 if (ret)
970 goto err;
Jassi Brar132fcb42012-02-02 22:01:34 +0530971
972 prm = &agdev->uac2.c_prm;
973 prm->max_psize = hs_epout_desc.wMaxPacketSize;
974 prm->rbuf = kzalloc(prm->max_psize * USB_XFERS, GFP_KERNEL);
975 if (!prm->rbuf) {
976 prm->max_psize = 0;
977 dev_err(&uac2->pdev.dev,
978 "%s:%d Error!\n", __func__, __LINE__);
Sebastian Andrzej Siewior391aa852012-10-22 22:14:59 +0200979 goto err;
Jassi Brar132fcb42012-02-02 22:01:34 +0530980 }
981
982 prm = &agdev->uac2.p_prm;
983 prm->max_psize = hs_epin_desc.wMaxPacketSize;
984 prm->rbuf = kzalloc(prm->max_psize * USB_XFERS, GFP_KERNEL);
985 if (!prm->rbuf) {
986 prm->max_psize = 0;
987 dev_err(&uac2->pdev.dev,
988 "%s:%d Error!\n", __func__, __LINE__);
Sebastian Andrzej Siewior391aa852012-10-22 22:14:59 +0200989 goto err;
Jassi Brar132fcb42012-02-02 22:01:34 +0530990 }
991
Sebastian Andrzej Siewior391aa852012-10-22 22:14:59 +0200992 ret = alsa_uac2_init(agdev);
993 if (ret)
994 goto err;
995 return 0;
996err:
997 kfree(agdev->uac2.p_prm.rbuf);
998 kfree(agdev->uac2.c_prm.rbuf);
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200999 usb_free_all_descriptors(fn);
Sebastian Andrzej Siewior391aa852012-10-22 22:14:59 +02001000 if (agdev->in_ep)
1001 agdev->in_ep->driver_data = NULL;
1002 if (agdev->out_ep)
1003 agdev->out_ep->driver_data = NULL;
1004 return -EINVAL;
Jassi Brar132fcb42012-02-02 22:01:34 +05301005}
1006
1007static void
1008afunc_unbind(struct usb_configuration *cfg, struct usb_function *fn)
1009{
1010 struct audio_dev *agdev = func_to_agdev(fn);
Jassi Brar132fcb42012-02-02 22:01:34 +05301011 struct uac2_rtd_params *prm;
1012
1013 alsa_uac2_exit(agdev);
1014
1015 prm = &agdev->uac2.p_prm;
1016 kfree(prm->rbuf);
1017
1018 prm = &agdev->uac2.c_prm;
1019 kfree(prm->rbuf);
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +02001020 usb_free_all_descriptors(fn);
Jassi Brar132fcb42012-02-02 22:01:34 +05301021
1022 if (agdev->in_ep)
1023 agdev->in_ep->driver_data = NULL;
1024 if (agdev->out_ep)
1025 agdev->out_ep->driver_data = NULL;
1026}
1027
1028static int
1029afunc_set_alt(struct usb_function *fn, unsigned intf, unsigned alt)
1030{
1031 struct usb_composite_dev *cdev = fn->config->cdev;
1032 struct audio_dev *agdev = func_to_agdev(fn);
1033 struct snd_uac2_chip *uac2 = &agdev->uac2;
1034 struct usb_gadget *gadget = cdev->gadget;
1035 struct usb_request *req;
1036 struct usb_ep *ep;
1037 struct uac2_rtd_params *prm;
1038 int i;
1039
1040 /* No i/f has more than 2 alt settings */
1041 if (alt > 1) {
1042 dev_err(&uac2->pdev.dev,
1043 "%s:%d Error!\n", __func__, __LINE__);
1044 return -EINVAL;
1045 }
1046
Sebastian Andrzej Siewior4ce63572012-10-22 22:15:08 +02001047 if (intf == agdev->ac_intf) {
Jassi Brar132fcb42012-02-02 22:01:34 +05301048 /* Control I/f has only 1 AltSetting - 0 */
1049 if (alt) {
1050 dev_err(&uac2->pdev.dev,
1051 "%s:%d Error!\n", __func__, __LINE__);
1052 return -EINVAL;
1053 }
1054 return 0;
1055 }
1056
Sebastian Andrzej Siewior4ce63572012-10-22 22:15:08 +02001057 if (intf == agdev->as_out_intf) {
Jassi Brar132fcb42012-02-02 22:01:34 +05301058 ep = agdev->out_ep;
1059 prm = &uac2->c_prm;
1060 config_ep_by_speed(gadget, fn, ep);
Sebastian Andrzej Siewior4ce63572012-10-22 22:15:08 +02001061 agdev->as_out_alt = alt;
1062 } else if (intf == agdev->as_in_intf) {
Jassi Brar132fcb42012-02-02 22:01:34 +05301063 ep = agdev->in_ep;
1064 prm = &uac2->p_prm;
1065 config_ep_by_speed(gadget, fn, ep);
Sebastian Andrzej Siewior4ce63572012-10-22 22:15:08 +02001066 agdev->as_in_alt = alt;
Jassi Brar132fcb42012-02-02 22:01:34 +05301067 } else {
1068 dev_err(&uac2->pdev.dev,
1069 "%s:%d Error!\n", __func__, __LINE__);
1070 return -EINVAL;
1071 }
1072
1073 if (alt == 0) {
1074 free_ep(prm, ep);
1075 return 0;
1076 }
1077
1078 prm->ep_enabled = true;
1079 usb_ep_enable(ep);
1080
1081 for (i = 0; i < USB_XFERS; i++) {
1082 if (prm->ureq[i].req) {
1083 if (usb_ep_queue(ep, prm->ureq[i].req, GFP_ATOMIC))
1084 dev_err(&uac2->pdev.dev, "%d Error!\n",
1085 __LINE__);
1086 continue;
1087 }
1088
1089 req = usb_ep_alloc_request(ep, GFP_ATOMIC);
1090 if (req == NULL) {
1091 dev_err(&uac2->pdev.dev,
1092 "%s:%d Error!\n", __func__, __LINE__);
1093 return -EINVAL;
1094 }
1095
1096 prm->ureq[i].req = req;
1097 prm->ureq[i].pp = prm;
1098
1099 req->zero = 0;
Jassi Brar132fcb42012-02-02 22:01:34 +05301100 req->context = &prm->ureq[i];
1101 req->length = prm->max_psize;
1102 req->complete = agdev_iso_complete;
1103 req->buf = prm->rbuf + i * req->length;
1104
1105 if (usb_ep_queue(ep, req, GFP_ATOMIC))
1106 dev_err(&uac2->pdev.dev, "%d Error!\n", __LINE__);
1107 }
1108
1109 return 0;
1110}
1111
1112static int
1113afunc_get_alt(struct usb_function *fn, unsigned intf)
1114{
1115 struct audio_dev *agdev = func_to_agdev(fn);
1116 struct snd_uac2_chip *uac2 = &agdev->uac2;
1117
Sebastian Andrzej Siewior4ce63572012-10-22 22:15:08 +02001118 if (intf == agdev->ac_intf)
1119 return agdev->ac_alt;
1120 else if (intf == agdev->as_out_intf)
1121 return agdev->as_out_alt;
1122 else if (intf == agdev->as_in_intf)
1123 return agdev->as_in_alt;
Jassi Brar132fcb42012-02-02 22:01:34 +05301124 else
1125 dev_err(&uac2->pdev.dev,
1126 "%s:%d Invalid Interface %d!\n",
1127 __func__, __LINE__, intf);
1128
1129 return -EINVAL;
1130}
1131
1132static void
1133afunc_disable(struct usb_function *fn)
1134{
1135 struct audio_dev *agdev = func_to_agdev(fn);
1136 struct snd_uac2_chip *uac2 = &agdev->uac2;
1137
1138 free_ep(&uac2->p_prm, agdev->in_ep);
Sebastian Andrzej Siewior4ce63572012-10-22 22:15:08 +02001139 agdev->as_in_alt = 0;
Jassi Brar132fcb42012-02-02 22:01:34 +05301140
1141 free_ep(&uac2->c_prm, agdev->out_ep);
Sebastian Andrzej Siewior4ce63572012-10-22 22:15:08 +02001142 agdev->as_out_alt = 0;
Jassi Brar132fcb42012-02-02 22:01:34 +05301143}
1144
1145static int
1146in_rq_cur(struct usb_function *fn, const struct usb_ctrlrequest *cr)
1147{
1148 struct usb_request *req = fn->config->cdev->req;
1149 struct audio_dev *agdev = func_to_agdev(fn);
1150 struct snd_uac2_chip *uac2 = &agdev->uac2;
1151 u16 w_length = le16_to_cpu(cr->wLength);
1152 u16 w_index = le16_to_cpu(cr->wIndex);
1153 u16 w_value = le16_to_cpu(cr->wValue);
1154 u8 entity_id = (w_index >> 8) & 0xff;
1155 u8 control_selector = w_value >> 8;
1156 int value = -EOPNOTSUPP;
1157
1158 if (control_selector == UAC2_CS_CONTROL_SAM_FREQ) {
1159 struct cntrl_cur_lay3 c;
1160
1161 if (entity_id == USB_IN_CLK_ID)
1162 c.dCUR = p_srate;
1163 else if (entity_id == USB_OUT_CLK_ID)
1164 c.dCUR = c_srate;
1165
1166 value = min_t(unsigned, w_length, sizeof c);
1167 memcpy(req->buf, &c, value);
1168 } else if (control_selector == UAC2_CS_CONTROL_CLOCK_VALID) {
1169 *(u8 *)req->buf = 1;
1170 value = min_t(unsigned, w_length, 1);
1171 } else {
1172 dev_err(&uac2->pdev.dev,
1173 "%s:%d control_selector=%d TODO!\n",
1174 __func__, __LINE__, control_selector);
1175 }
1176
1177 return value;
1178}
1179
1180static int
1181in_rq_range(struct usb_function *fn, const struct usb_ctrlrequest *cr)
1182{
1183 struct usb_request *req = fn->config->cdev->req;
1184 struct audio_dev *agdev = func_to_agdev(fn);
1185 struct snd_uac2_chip *uac2 = &agdev->uac2;
1186 u16 w_length = le16_to_cpu(cr->wLength);
1187 u16 w_index = le16_to_cpu(cr->wIndex);
1188 u16 w_value = le16_to_cpu(cr->wValue);
1189 u8 entity_id = (w_index >> 8) & 0xff;
1190 u8 control_selector = w_value >> 8;
1191 struct cntrl_range_lay3 r;
1192 int value = -EOPNOTSUPP;
1193
1194 if (control_selector == UAC2_CS_CONTROL_SAM_FREQ) {
1195 if (entity_id == USB_IN_CLK_ID)
1196 r.dMIN = p_srate;
1197 else if (entity_id == USB_OUT_CLK_ID)
1198 r.dMIN = c_srate;
1199 else
1200 return -EOPNOTSUPP;
1201
1202 r.dMAX = r.dMIN;
1203 r.dRES = 0;
1204 r.wNumSubRanges = 1;
1205
1206 value = min_t(unsigned, w_length, sizeof r);
1207 memcpy(req->buf, &r, value);
1208 } else {
1209 dev_err(&uac2->pdev.dev,
1210 "%s:%d control_selector=%d TODO!\n",
1211 __func__, __LINE__, control_selector);
1212 }
1213
1214 return value;
1215}
1216
1217static int
1218ac_rq_in(struct usb_function *fn, const struct usb_ctrlrequest *cr)
1219{
1220 if (cr->bRequest == UAC2_CS_CUR)
1221 return in_rq_cur(fn, cr);
1222 else if (cr->bRequest == UAC2_CS_RANGE)
1223 return in_rq_range(fn, cr);
1224 else
1225 return -EOPNOTSUPP;
1226}
1227
1228static int
1229out_rq_cur(struct usb_function *fn, const struct usb_ctrlrequest *cr)
1230{
1231 u16 w_length = le16_to_cpu(cr->wLength);
1232 u16 w_value = le16_to_cpu(cr->wValue);
1233 u8 control_selector = w_value >> 8;
1234
1235 if (control_selector == UAC2_CS_CONTROL_SAM_FREQ)
1236 return w_length;
1237
1238 return -EOPNOTSUPP;
1239}
1240
1241static int
1242setup_rq_inf(struct usb_function *fn, const struct usb_ctrlrequest *cr)
1243{
1244 struct audio_dev *agdev = func_to_agdev(fn);
1245 struct snd_uac2_chip *uac2 = &agdev->uac2;
1246 u16 w_index = le16_to_cpu(cr->wIndex);
1247 u8 intf = w_index & 0xff;
1248
Sebastian Andrzej Siewior4ce63572012-10-22 22:15:08 +02001249 if (intf != agdev->ac_intf) {
Jassi Brar132fcb42012-02-02 22:01:34 +05301250 dev_err(&uac2->pdev.dev,
1251 "%s:%d Error!\n", __func__, __LINE__);
1252 return -EOPNOTSUPP;
1253 }
1254
1255 if (cr->bRequestType & USB_DIR_IN)
1256 return ac_rq_in(fn, cr);
1257 else if (cr->bRequest == UAC2_CS_CUR)
1258 return out_rq_cur(fn, cr);
1259
1260 return -EOPNOTSUPP;
1261}
1262
1263static int
1264afunc_setup(struct usb_function *fn, const struct usb_ctrlrequest *cr)
1265{
1266 struct usb_composite_dev *cdev = fn->config->cdev;
1267 struct audio_dev *agdev = func_to_agdev(fn);
1268 struct snd_uac2_chip *uac2 = &agdev->uac2;
1269 struct usb_request *req = cdev->req;
1270 u16 w_length = le16_to_cpu(cr->wLength);
1271 int value = -EOPNOTSUPP;
1272
1273 /* Only Class specific requests are supposed to reach here */
1274 if ((cr->bRequestType & USB_TYPE_MASK) != USB_TYPE_CLASS)
1275 return -EOPNOTSUPP;
1276
1277 if ((cr->bRequestType & USB_RECIP_MASK) == USB_RECIP_INTERFACE)
1278 value = setup_rq_inf(fn, cr);
1279 else
1280 dev_err(&uac2->pdev.dev, "%s:%d Error!\n", __func__, __LINE__);
1281
1282 if (value >= 0) {
1283 req->length = value;
1284 req->zero = value < w_length;
1285 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
1286 if (value < 0) {
1287 dev_err(&uac2->pdev.dev,
1288 "%s:%d Error!\n", __func__, __LINE__);
1289 req->status = 0;
1290 }
1291 }
1292
1293 return value;
1294}
1295
1296static int audio_bind_config(struct usb_configuration *cfg)
1297{
Sebastian Andrzej Siewior1616e99d2012-10-22 22:15:10 +02001298 int res;
Jassi Brar132fcb42012-02-02 22:01:34 +05301299
1300 agdev_g = kzalloc(sizeof *agdev_g, GFP_KERNEL);
1301 if (agdev_g == NULL) {
1302 printk(KERN_ERR "Unable to allocate audio gadget\n");
1303 return -ENOMEM;
1304 }
1305
Sebastian Andrzej Siewior1616e99d2012-10-22 22:15:10 +02001306 res = usb_string_ids_tab(cfg->cdev, strings_fn);
1307 if (res)
1308 return res;
1309 iad_desc.iFunction = strings_fn[STR_ASSOC].id;
1310 std_ac_if_desc.iInterface = strings_fn[STR_IF_CTRL].id;
1311 in_clk_src_desc.iClockSource = strings_fn[STR_CLKSRC_IN].id;
1312 out_clk_src_desc.iClockSource = strings_fn[STR_CLKSRC_OUT].id;
1313 usb_out_it_desc.iTerminal = strings_fn[STR_USB_IT].id;
1314 io_in_it_desc.iTerminal = strings_fn[STR_IO_IT].id;
1315 usb_in_ot_desc.iTerminal = strings_fn[STR_USB_OT].id;
1316 io_out_ot_desc.iTerminal = strings_fn[STR_IO_OT].id;
1317 std_as_out_if0_desc.iInterface = strings_fn[STR_AS_OUT_ALT0].id;
1318 std_as_out_if1_desc.iInterface = strings_fn[STR_AS_OUT_ALT1].id;
1319 std_as_in_if0_desc.iInterface = strings_fn[STR_AS_IN_ALT0].id;
1320 std_as_in_if1_desc.iInterface = strings_fn[STR_AS_IN_ALT1].id;
Jassi Brar132fcb42012-02-02 22:01:34 +05301321
1322 agdev_g->func.name = "uac2_func";
1323 agdev_g->func.strings = fn_strings;
1324 agdev_g->func.bind = afunc_bind;
1325 agdev_g->func.unbind = afunc_unbind;
1326 agdev_g->func.set_alt = afunc_set_alt;
1327 agdev_g->func.get_alt = afunc_get_alt;
1328 agdev_g->func.disable = afunc_disable;
1329 agdev_g->func.setup = afunc_setup;
1330
1331 /* Initialize the configurable parameters */
1332 usb_out_it_desc.bNrChannels = num_channels(c_chmask);
1333 usb_out_it_desc.bmChannelConfig = cpu_to_le32(c_chmask);
1334 io_in_it_desc.bNrChannels = num_channels(p_chmask);
1335 io_in_it_desc.bmChannelConfig = cpu_to_le32(p_chmask);
1336 as_out_hdr_desc.bNrChannels = num_channels(c_chmask);
1337 as_out_hdr_desc.bmChannelConfig = cpu_to_le32(c_chmask);
1338 as_in_hdr_desc.bNrChannels = num_channels(p_chmask);
1339 as_in_hdr_desc.bmChannelConfig = cpu_to_le32(p_chmask);
1340 as_out_fmt1_desc.bSubslotSize = c_ssize;
1341 as_out_fmt1_desc.bBitResolution = c_ssize * 8;
1342 as_in_fmt1_desc.bSubslotSize = p_ssize;
1343 as_in_fmt1_desc.bBitResolution = p_ssize * 8;
1344
1345 snprintf(clksrc_in, sizeof(clksrc_in), "%uHz", p_srate);
1346 snprintf(clksrc_out, sizeof(clksrc_out), "%uHz", c_srate);
1347
1348 res = usb_add_function(cfg, &agdev_g->func);
1349 if (res < 0)
1350 kfree(agdev_g);
1351
1352 return res;
1353}
1354
1355static void
1356uac2_unbind_config(struct usb_configuration *cfg)
1357{
1358 kfree(agdev_g);
1359 agdev_g = NULL;
1360}