blob: 2f23566e53d88bf7fd618efcc100b49447c0cf27 [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 */
397 err = snd_card_create(-1, NULL, THIS_MODULE, 0, &card);
398 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
424 snd_card_set_dev(card, &pdev->dev);
425
426 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS,
427 snd_dma_continuous_data(GFP_KERNEL), 0, BUFF_SIZE_MAX);
428
429 err = snd_card_register(card);
430 if (!err) {
431 platform_set_drvdata(pdev, card);
432 return 0;
433 }
434
435snd_fail:
436 snd_card_free(card);
437
438 uac2->pcm = NULL;
439 uac2->card = NULL;
440
441 return err;
442}
443
Sebastian Andrzej Siewior1e1a27c2012-09-06 20:11:02 +0200444static int snd_uac2_remove(struct platform_device *pdev)
Jassi Brar132fcb42012-02-02 22:01:34 +0530445{
446 struct snd_card *card = platform_get_drvdata(pdev);
447
Jassi Brar132fcb42012-02-02 22:01:34 +0530448 if (card)
449 return snd_card_free(card);
450
451 return 0;
452}
453
454static int alsa_uac2_init(struct audio_dev *agdev)
455{
456 struct snd_uac2_chip *uac2 = &agdev->uac2;
457 int err;
458
459 uac2->pdrv.probe = snd_uac2_probe;
460 uac2->pdrv.remove = snd_uac2_remove;
461 uac2->pdrv.driver.name = uac2_name;
462
463 uac2->pdev.id = 0;
464 uac2->pdev.name = uac2_name;
465
466 /* Register snd_uac2 driver */
467 err = platform_driver_register(&uac2->pdrv);
468 if (err)
469 return err;
470
471 /* Register snd_uac2 device */
472 err = platform_device_register(&uac2->pdev);
473 if (err)
474 platform_driver_unregister(&uac2->pdrv);
475
476 return err;
477}
478
479static void alsa_uac2_exit(struct audio_dev *agdev)
480{
481 struct snd_uac2_chip *uac2 = &agdev->uac2;
482
483 platform_driver_unregister(&uac2->pdrv);
484 platform_device_unregister(&uac2->pdev);
485}
486
487
488/* --------- USB Function Interface ------------- */
489
490enum {
491 STR_ASSOC,
492 STR_IF_CTRL,
493 STR_CLKSRC_IN,
494 STR_CLKSRC_OUT,
495 STR_USB_IT,
496 STR_IO_IT,
497 STR_USB_OT,
498 STR_IO_OT,
499 STR_AS_OUT_ALT0,
500 STR_AS_OUT_ALT1,
501 STR_AS_IN_ALT0,
502 STR_AS_IN_ALT1,
503};
504
Jassi Brar132fcb42012-02-02 22:01:34 +0530505static char clksrc_in[8];
506static char clksrc_out[8];
Jassi Brar132fcb42012-02-02 22:01:34 +0530507
508static struct usb_string strings_fn[] = {
Sebastian Andrzej Siewiorb36c3472012-10-22 22:15:09 +0200509 [STR_ASSOC].s = "Source/Sink",
510 [STR_IF_CTRL].s = "Topology Control",
Jassi Brar132fcb42012-02-02 22:01:34 +0530511 [STR_CLKSRC_IN].s = clksrc_in,
512 [STR_CLKSRC_OUT].s = clksrc_out,
Sebastian Andrzej Siewiorb36c3472012-10-22 22:15:09 +0200513 [STR_USB_IT].s = "USBH Out",
514 [STR_IO_IT].s = "USBD Out",
515 [STR_USB_OT].s = "USBH In",
516 [STR_IO_OT].s = "USBD In",
517 [STR_AS_OUT_ALT0].s = "Playback Inactive",
518 [STR_AS_OUT_ALT1].s = "Playback Active",
519 [STR_AS_IN_ALT0].s = "Capture Inactive",
520 [STR_AS_IN_ALT1].s = "Capture Active",
Jassi Brar132fcb42012-02-02 22:01:34 +0530521 { },
522};
523
524static struct usb_gadget_strings str_fn = {
525 .language = 0x0409, /* en-us */
526 .strings = strings_fn,
527};
528
529static struct usb_gadget_strings *fn_strings[] = {
530 &str_fn,
531 NULL,
532};
533
534static struct usb_qualifier_descriptor devqual_desc = {
535 .bLength = sizeof devqual_desc,
536 .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
537
538 .bcdUSB = cpu_to_le16(0x200),
539 .bDeviceClass = USB_CLASS_MISC,
540 .bDeviceSubClass = 0x02,
541 .bDeviceProtocol = 0x01,
542 .bNumConfigurations = 1,
543 .bRESERVED = 0,
544};
545
546static struct usb_interface_assoc_descriptor iad_desc = {
547 .bLength = sizeof iad_desc,
548 .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION,
549
550 .bFirstInterface = 0,
551 .bInterfaceCount = 3,
552 .bFunctionClass = USB_CLASS_AUDIO,
553 .bFunctionSubClass = UAC2_FUNCTION_SUBCLASS_UNDEFINED,
554 .bFunctionProtocol = UAC_VERSION_2,
555};
556
557/* Audio Control Interface */
558static struct usb_interface_descriptor std_ac_if_desc = {
559 .bLength = sizeof std_ac_if_desc,
560 .bDescriptorType = USB_DT_INTERFACE,
561
562 .bAlternateSetting = 0,
563 .bNumEndpoints = 0,
564 .bInterfaceClass = USB_CLASS_AUDIO,
565 .bInterfaceSubClass = USB_SUBCLASS_AUDIOCONTROL,
566 .bInterfaceProtocol = UAC_VERSION_2,
567};
568
569/* Clock source for IN traffic */
570struct uac_clock_source_descriptor in_clk_src_desc = {
571 .bLength = sizeof in_clk_src_desc,
572 .bDescriptorType = USB_DT_CS_INTERFACE,
573
574 .bDescriptorSubtype = UAC2_CLOCK_SOURCE,
575 .bClockID = USB_IN_CLK_ID,
576 .bmAttributes = UAC_CLOCK_SOURCE_TYPE_INT_FIXED,
577 .bmControls = (CONTROL_RDONLY << CLK_FREQ_CTRL),
578 .bAssocTerminal = 0,
579};
580
581/* Clock source for OUT traffic */
582struct uac_clock_source_descriptor out_clk_src_desc = {
583 .bLength = sizeof out_clk_src_desc,
584 .bDescriptorType = USB_DT_CS_INTERFACE,
585
586 .bDescriptorSubtype = UAC2_CLOCK_SOURCE,
587 .bClockID = USB_OUT_CLK_ID,
588 .bmAttributes = UAC_CLOCK_SOURCE_TYPE_INT_FIXED,
589 .bmControls = (CONTROL_RDONLY << CLK_FREQ_CTRL),
590 .bAssocTerminal = 0,
591};
592
593/* Input Terminal for USB_OUT */
594struct uac2_input_terminal_descriptor usb_out_it_desc = {
595 .bLength = sizeof usb_out_it_desc,
596 .bDescriptorType = USB_DT_CS_INTERFACE,
597
598 .bDescriptorSubtype = UAC_INPUT_TERMINAL,
599 .bTerminalID = USB_OUT_IT_ID,
600 .wTerminalType = cpu_to_le16(UAC_TERMINAL_STREAMING),
601 .bAssocTerminal = 0,
602 .bCSourceID = USB_OUT_CLK_ID,
603 .iChannelNames = 0,
604 .bmControls = (CONTROL_RDWR << COPY_CTRL),
605};
606
607/* Input Terminal for I/O-In */
608struct uac2_input_terminal_descriptor io_in_it_desc = {
609 .bLength = sizeof io_in_it_desc,
610 .bDescriptorType = USB_DT_CS_INTERFACE,
611
612 .bDescriptorSubtype = UAC_INPUT_TERMINAL,
613 .bTerminalID = IO_IN_IT_ID,
614 .wTerminalType = cpu_to_le16(UAC_INPUT_TERMINAL_UNDEFINED),
615 .bAssocTerminal = 0,
616 .bCSourceID = USB_IN_CLK_ID,
617 .iChannelNames = 0,
618 .bmControls = (CONTROL_RDWR << COPY_CTRL),
619};
620
621/* Ouput Terminal for USB_IN */
622struct uac2_output_terminal_descriptor usb_in_ot_desc = {
623 .bLength = sizeof usb_in_ot_desc,
624 .bDescriptorType = USB_DT_CS_INTERFACE,
625
626 .bDescriptorSubtype = UAC_OUTPUT_TERMINAL,
627 .bTerminalID = USB_IN_OT_ID,
628 .wTerminalType = cpu_to_le16(UAC_TERMINAL_STREAMING),
629 .bAssocTerminal = 0,
630 .bSourceID = IO_IN_IT_ID,
631 .bCSourceID = USB_IN_CLK_ID,
632 .bmControls = (CONTROL_RDWR << COPY_CTRL),
633};
634
635/* Ouput Terminal for I/O-Out */
636struct uac2_output_terminal_descriptor io_out_ot_desc = {
637 .bLength = sizeof io_out_ot_desc,
638 .bDescriptorType = USB_DT_CS_INTERFACE,
639
640 .bDescriptorSubtype = UAC_OUTPUT_TERMINAL,
641 .bTerminalID = IO_OUT_OT_ID,
642 .wTerminalType = cpu_to_le16(UAC_OUTPUT_TERMINAL_UNDEFINED),
643 .bAssocTerminal = 0,
644 .bSourceID = USB_OUT_IT_ID,
645 .bCSourceID = USB_OUT_CLK_ID,
646 .bmControls = (CONTROL_RDWR << COPY_CTRL),
647};
648
649struct uac2_ac_header_descriptor ac_hdr_desc = {
650 .bLength = sizeof ac_hdr_desc,
651 .bDescriptorType = USB_DT_CS_INTERFACE,
652
653 .bDescriptorSubtype = UAC_MS_HEADER,
654 .bcdADC = cpu_to_le16(0x200),
655 .bCategory = UAC2_FUNCTION_IO_BOX,
656 .wTotalLength = sizeof in_clk_src_desc + sizeof out_clk_src_desc
657 + sizeof usb_out_it_desc + sizeof io_in_it_desc
658 + sizeof usb_in_ot_desc + sizeof io_out_ot_desc,
659 .bmControls = 0,
660};
661
662/* Audio Streaming OUT Interface - Alt0 */
663static struct usb_interface_descriptor std_as_out_if0_desc = {
664 .bLength = sizeof std_as_out_if0_desc,
665 .bDescriptorType = USB_DT_INTERFACE,
666
667 .bAlternateSetting = 0,
668 .bNumEndpoints = 0,
669 .bInterfaceClass = USB_CLASS_AUDIO,
670 .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
671 .bInterfaceProtocol = UAC_VERSION_2,
672};
673
674/* Audio Streaming OUT Interface - Alt1 */
675static struct usb_interface_descriptor std_as_out_if1_desc = {
676 .bLength = sizeof std_as_out_if1_desc,
677 .bDescriptorType = USB_DT_INTERFACE,
678
679 .bAlternateSetting = 1,
680 .bNumEndpoints = 1,
681 .bInterfaceClass = USB_CLASS_AUDIO,
682 .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
683 .bInterfaceProtocol = UAC_VERSION_2,
684};
685
686/* Audio Stream OUT Intface Desc */
687struct uac2_as_header_descriptor as_out_hdr_desc = {
688 .bLength = sizeof as_out_hdr_desc,
689 .bDescriptorType = USB_DT_CS_INTERFACE,
690
691 .bDescriptorSubtype = UAC_AS_GENERAL,
692 .bTerminalLink = USB_OUT_IT_ID,
693 .bmControls = 0,
694 .bFormatType = UAC_FORMAT_TYPE_I,
695 .bmFormats = cpu_to_le32(UAC_FORMAT_TYPE_I_PCM),
696 .iChannelNames = 0,
697};
698
699/* Audio USB_OUT Format */
700struct uac2_format_type_i_descriptor as_out_fmt1_desc = {
701 .bLength = sizeof as_out_fmt1_desc,
702 .bDescriptorType = USB_DT_CS_INTERFACE,
703 .bDescriptorSubtype = UAC_FORMAT_TYPE,
704 .bFormatType = UAC_FORMAT_TYPE_I,
705};
706
707/* STD AS ISO OUT Endpoint */
708struct usb_endpoint_descriptor fs_epout_desc = {
709 .bLength = USB_DT_ENDPOINT_SIZE,
710 .bDescriptorType = USB_DT_ENDPOINT,
711
712 .bEndpointAddress = USB_DIR_OUT,
713 .bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC,
714 .bInterval = 1,
715};
716
717struct usb_endpoint_descriptor hs_epout_desc = {
718 .bLength = USB_DT_ENDPOINT_SIZE,
719 .bDescriptorType = USB_DT_ENDPOINT,
720
721 .bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC,
722 .bInterval = 4,
723};
724
725/* CS AS ISO OUT Endpoint */
726static struct uac2_iso_endpoint_descriptor as_iso_out_desc = {
727 .bLength = sizeof as_iso_out_desc,
728 .bDescriptorType = USB_DT_CS_ENDPOINT,
729
730 .bDescriptorSubtype = UAC_EP_GENERAL,
731 .bmAttributes = 0,
732 .bmControls = 0,
733 .bLockDelayUnits = 0,
734 .wLockDelay = 0,
735};
736
737/* Audio Streaming IN Interface - Alt0 */
738static struct usb_interface_descriptor std_as_in_if0_desc = {
739 .bLength = sizeof std_as_in_if0_desc,
740 .bDescriptorType = USB_DT_INTERFACE,
741
742 .bAlternateSetting = 0,
743 .bNumEndpoints = 0,
744 .bInterfaceClass = USB_CLASS_AUDIO,
745 .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
746 .bInterfaceProtocol = UAC_VERSION_2,
747};
748
749/* Audio Streaming IN Interface - Alt1 */
750static struct usb_interface_descriptor std_as_in_if1_desc = {
751 .bLength = sizeof std_as_in_if1_desc,
752 .bDescriptorType = USB_DT_INTERFACE,
753
754 .bAlternateSetting = 1,
755 .bNumEndpoints = 1,
756 .bInterfaceClass = USB_CLASS_AUDIO,
757 .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
758 .bInterfaceProtocol = UAC_VERSION_2,
759};
760
761/* Audio Stream IN Intface Desc */
762struct uac2_as_header_descriptor as_in_hdr_desc = {
763 .bLength = sizeof as_in_hdr_desc,
764 .bDescriptorType = USB_DT_CS_INTERFACE,
765
766 .bDescriptorSubtype = UAC_AS_GENERAL,
767 .bTerminalLink = USB_IN_OT_ID,
768 .bmControls = 0,
769 .bFormatType = UAC_FORMAT_TYPE_I,
770 .bmFormats = cpu_to_le32(UAC_FORMAT_TYPE_I_PCM),
771 .iChannelNames = 0,
772};
773
774/* Audio USB_IN Format */
775struct uac2_format_type_i_descriptor as_in_fmt1_desc = {
776 .bLength = sizeof as_in_fmt1_desc,
777 .bDescriptorType = USB_DT_CS_INTERFACE,
778 .bDescriptorSubtype = UAC_FORMAT_TYPE,
779 .bFormatType = UAC_FORMAT_TYPE_I,
780};
781
782/* STD AS ISO IN Endpoint */
783struct usb_endpoint_descriptor fs_epin_desc = {
784 .bLength = USB_DT_ENDPOINT_SIZE,
785 .bDescriptorType = USB_DT_ENDPOINT,
786
787 .bEndpointAddress = USB_DIR_IN,
788 .bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC,
789 .bInterval = 1,
790};
791
792struct usb_endpoint_descriptor hs_epin_desc = {
793 .bLength = USB_DT_ENDPOINT_SIZE,
794 .bDescriptorType = USB_DT_ENDPOINT,
795
796 .bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC,
797 .bInterval = 4,
798};
799
800/* CS AS ISO IN Endpoint */
801static struct uac2_iso_endpoint_descriptor as_iso_in_desc = {
802 .bLength = sizeof as_iso_in_desc,
803 .bDescriptorType = USB_DT_CS_ENDPOINT,
804
805 .bDescriptorSubtype = UAC_EP_GENERAL,
806 .bmAttributes = 0,
807 .bmControls = 0,
808 .bLockDelayUnits = 0,
809 .wLockDelay = 0,
810};
811
812static struct usb_descriptor_header *fs_audio_desc[] = {
813 (struct usb_descriptor_header *)&iad_desc,
814 (struct usb_descriptor_header *)&std_ac_if_desc,
815
816 (struct usb_descriptor_header *)&ac_hdr_desc,
817 (struct usb_descriptor_header *)&in_clk_src_desc,
818 (struct usb_descriptor_header *)&out_clk_src_desc,
819 (struct usb_descriptor_header *)&usb_out_it_desc,
820 (struct usb_descriptor_header *)&io_in_it_desc,
821 (struct usb_descriptor_header *)&usb_in_ot_desc,
822 (struct usb_descriptor_header *)&io_out_ot_desc,
823
824 (struct usb_descriptor_header *)&std_as_out_if0_desc,
825 (struct usb_descriptor_header *)&std_as_out_if1_desc,
826
827 (struct usb_descriptor_header *)&as_out_hdr_desc,
828 (struct usb_descriptor_header *)&as_out_fmt1_desc,
829 (struct usb_descriptor_header *)&fs_epout_desc,
830 (struct usb_descriptor_header *)&as_iso_out_desc,
831
832 (struct usb_descriptor_header *)&std_as_in_if0_desc,
833 (struct usb_descriptor_header *)&std_as_in_if1_desc,
834
835 (struct usb_descriptor_header *)&as_in_hdr_desc,
836 (struct usb_descriptor_header *)&as_in_fmt1_desc,
837 (struct usb_descriptor_header *)&fs_epin_desc,
838 (struct usb_descriptor_header *)&as_iso_in_desc,
839 NULL,
840};
841
842static struct usb_descriptor_header *hs_audio_desc[] = {
843 (struct usb_descriptor_header *)&iad_desc,
844 (struct usb_descriptor_header *)&std_ac_if_desc,
845
846 (struct usb_descriptor_header *)&ac_hdr_desc,
847 (struct usb_descriptor_header *)&in_clk_src_desc,
848 (struct usb_descriptor_header *)&out_clk_src_desc,
849 (struct usb_descriptor_header *)&usb_out_it_desc,
850 (struct usb_descriptor_header *)&io_in_it_desc,
851 (struct usb_descriptor_header *)&usb_in_ot_desc,
852 (struct usb_descriptor_header *)&io_out_ot_desc,
853
854 (struct usb_descriptor_header *)&std_as_out_if0_desc,
855 (struct usb_descriptor_header *)&std_as_out_if1_desc,
856
857 (struct usb_descriptor_header *)&as_out_hdr_desc,
858 (struct usb_descriptor_header *)&as_out_fmt1_desc,
859 (struct usb_descriptor_header *)&hs_epout_desc,
860 (struct usb_descriptor_header *)&as_iso_out_desc,
861
862 (struct usb_descriptor_header *)&std_as_in_if0_desc,
863 (struct usb_descriptor_header *)&std_as_in_if1_desc,
864
865 (struct usb_descriptor_header *)&as_in_hdr_desc,
866 (struct usb_descriptor_header *)&as_in_fmt1_desc,
867 (struct usb_descriptor_header *)&hs_epin_desc,
868 (struct usb_descriptor_header *)&as_iso_in_desc,
869 NULL,
870};
871
872struct cntrl_cur_lay3 {
873 __u32 dCUR;
874};
875
876struct cntrl_range_lay3 {
877 __u16 wNumSubRanges;
878 __u32 dMIN;
879 __u32 dMAX;
880 __u32 dRES;
881} __packed;
882
883static inline void
884free_ep(struct uac2_rtd_params *prm, struct usb_ep *ep)
885{
Jassi Brareb127cb2013-05-30 18:23:33 +0530886 struct snd_uac2_chip *uac2 = prm->uac2;
Jassi Brar132fcb42012-02-02 22:01:34 +0530887 int i;
888
889 prm->ep_enabled = false;
890
891 for (i = 0; i < USB_XFERS; i++) {
892 if (prm->ureq[i].req) {
893 usb_ep_dequeue(ep, prm->ureq[i].req);
894 usb_ep_free_request(ep, prm->ureq[i].req);
895 prm->ureq[i].req = NULL;
896 }
897 }
898
899 if (usb_ep_disable(ep))
900 dev_err(&uac2->pdev.dev,
901 "%s:%d Error!\n", __func__, __LINE__);
902}
903
904static int __init
905afunc_bind(struct usb_configuration *cfg, struct usb_function *fn)
906{
907 struct audio_dev *agdev = func_to_agdev(fn);
908 struct snd_uac2_chip *uac2 = &agdev->uac2;
909 struct usb_composite_dev *cdev = cfg->cdev;
910 struct usb_gadget *gadget = cdev->gadget;
911 struct uac2_rtd_params *prm;
912 int ret;
913
914 ret = usb_interface_id(cfg, fn);
915 if (ret < 0) {
916 dev_err(&uac2->pdev.dev,
917 "%s:%d Error!\n", __func__, __LINE__);
918 return ret;
919 }
920 std_ac_if_desc.bInterfaceNumber = ret;
Sebastian Andrzej Siewior4ce63572012-10-22 22:15:08 +0200921 agdev->ac_intf = ret;
922 agdev->ac_alt = 0;
Jassi Brar132fcb42012-02-02 22:01:34 +0530923
924 ret = usb_interface_id(cfg, fn);
925 if (ret < 0) {
926 dev_err(&uac2->pdev.dev,
927 "%s:%d Error!\n", __func__, __LINE__);
928 return ret;
929 }
930 std_as_out_if0_desc.bInterfaceNumber = ret;
931 std_as_out_if1_desc.bInterfaceNumber = ret;
Sebastian Andrzej Siewior4ce63572012-10-22 22:15:08 +0200932 agdev->as_out_intf = ret;
933 agdev->as_out_alt = 0;
Jassi Brar132fcb42012-02-02 22:01:34 +0530934
935 ret = usb_interface_id(cfg, fn);
936 if (ret < 0) {
937 dev_err(&uac2->pdev.dev,
938 "%s:%d Error!\n", __func__, __LINE__);
939 return ret;
940 }
941 std_as_in_if0_desc.bInterfaceNumber = ret;
942 std_as_in_if1_desc.bInterfaceNumber = ret;
Sebastian Andrzej Siewior4ce63572012-10-22 22:15:08 +0200943 agdev->as_in_intf = ret;
944 agdev->as_in_alt = 0;
Jassi Brar132fcb42012-02-02 22:01:34 +0530945
946 agdev->out_ep = usb_ep_autoconfig(gadget, &fs_epout_desc);
Sebastian Andrzej Siewior391aa852012-10-22 22:14:59 +0200947 if (!agdev->out_ep) {
Jassi Brar132fcb42012-02-02 22:01:34 +0530948 dev_err(&uac2->pdev.dev,
949 "%s:%d Error!\n", __func__, __LINE__);
Sebastian Andrzej Siewior391aa852012-10-22 22:14:59 +0200950 goto err;
951 }
Jassi Brar132fcb42012-02-02 22:01:34 +0530952 agdev->out_ep->driver_data = agdev;
953
954 agdev->in_ep = usb_ep_autoconfig(gadget, &fs_epin_desc);
Sebastian Andrzej Siewior391aa852012-10-22 22:14:59 +0200955 if (!agdev->in_ep) {
Jassi Brar132fcb42012-02-02 22:01:34 +0530956 dev_err(&uac2->pdev.dev,
957 "%s:%d Error!\n", __func__, __LINE__);
Sebastian Andrzej Siewior391aa852012-10-22 22:14:59 +0200958 goto err;
959 }
Jassi Brar132fcb42012-02-02 22:01:34 +0530960 agdev->in_ep->driver_data = agdev;
961
Jassi Brareb127cb2013-05-30 18:23:33 +0530962 uac2->p_prm.uac2 = uac2;
963 uac2->c_prm.uac2 = uac2;
964
Jassi Brar132fcb42012-02-02 22:01:34 +0530965 hs_epout_desc.bEndpointAddress = fs_epout_desc.bEndpointAddress;
966 hs_epout_desc.wMaxPacketSize = fs_epout_desc.wMaxPacketSize;
967 hs_epin_desc.bEndpointAddress = fs_epin_desc.bEndpointAddress;
968 hs_epin_desc.wMaxPacketSize = fs_epin_desc.wMaxPacketSize;
969
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200970 ret = usb_assign_descriptors(fn, fs_audio_desc, hs_audio_desc, NULL);
971 if (ret)
972 goto err;
Jassi Brar132fcb42012-02-02 22:01:34 +0530973
974 prm = &agdev->uac2.c_prm;
975 prm->max_psize = hs_epout_desc.wMaxPacketSize;
976 prm->rbuf = kzalloc(prm->max_psize * USB_XFERS, GFP_KERNEL);
977 if (!prm->rbuf) {
978 prm->max_psize = 0;
979 dev_err(&uac2->pdev.dev,
980 "%s:%d Error!\n", __func__, __LINE__);
Sebastian Andrzej Siewior391aa852012-10-22 22:14:59 +0200981 goto err;
Jassi Brar132fcb42012-02-02 22:01:34 +0530982 }
983
984 prm = &agdev->uac2.p_prm;
985 prm->max_psize = hs_epin_desc.wMaxPacketSize;
986 prm->rbuf = kzalloc(prm->max_psize * USB_XFERS, GFP_KERNEL);
987 if (!prm->rbuf) {
988 prm->max_psize = 0;
989 dev_err(&uac2->pdev.dev,
990 "%s:%d Error!\n", __func__, __LINE__);
Sebastian Andrzej Siewior391aa852012-10-22 22:14:59 +0200991 goto err;
Jassi Brar132fcb42012-02-02 22:01:34 +0530992 }
993
Sebastian Andrzej Siewior391aa852012-10-22 22:14:59 +0200994 ret = alsa_uac2_init(agdev);
995 if (ret)
996 goto err;
997 return 0;
998err:
999 kfree(agdev->uac2.p_prm.rbuf);
1000 kfree(agdev->uac2.c_prm.rbuf);
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +02001001 usb_free_all_descriptors(fn);
Sebastian Andrzej Siewior391aa852012-10-22 22:14:59 +02001002 if (agdev->in_ep)
1003 agdev->in_ep->driver_data = NULL;
1004 if (agdev->out_ep)
1005 agdev->out_ep->driver_data = NULL;
1006 return -EINVAL;
Jassi Brar132fcb42012-02-02 22:01:34 +05301007}
1008
1009static void
1010afunc_unbind(struct usb_configuration *cfg, struct usb_function *fn)
1011{
1012 struct audio_dev *agdev = func_to_agdev(fn);
Jassi Brar132fcb42012-02-02 22:01:34 +05301013 struct uac2_rtd_params *prm;
1014
1015 alsa_uac2_exit(agdev);
1016
1017 prm = &agdev->uac2.p_prm;
1018 kfree(prm->rbuf);
1019
1020 prm = &agdev->uac2.c_prm;
1021 kfree(prm->rbuf);
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +02001022 usb_free_all_descriptors(fn);
Jassi Brar132fcb42012-02-02 22:01:34 +05301023
1024 if (agdev->in_ep)
1025 agdev->in_ep->driver_data = NULL;
1026 if (agdev->out_ep)
1027 agdev->out_ep->driver_data = NULL;
1028}
1029
1030static int
1031afunc_set_alt(struct usb_function *fn, unsigned intf, unsigned alt)
1032{
1033 struct usb_composite_dev *cdev = fn->config->cdev;
1034 struct audio_dev *agdev = func_to_agdev(fn);
1035 struct snd_uac2_chip *uac2 = &agdev->uac2;
1036 struct usb_gadget *gadget = cdev->gadget;
1037 struct usb_request *req;
1038 struct usb_ep *ep;
1039 struct uac2_rtd_params *prm;
1040 int i;
1041
1042 /* No i/f has more than 2 alt settings */
1043 if (alt > 1) {
1044 dev_err(&uac2->pdev.dev,
1045 "%s:%d Error!\n", __func__, __LINE__);
1046 return -EINVAL;
1047 }
1048
Sebastian Andrzej Siewior4ce63572012-10-22 22:15:08 +02001049 if (intf == agdev->ac_intf) {
Jassi Brar132fcb42012-02-02 22:01:34 +05301050 /* Control I/f has only 1 AltSetting - 0 */
1051 if (alt) {
1052 dev_err(&uac2->pdev.dev,
1053 "%s:%d Error!\n", __func__, __LINE__);
1054 return -EINVAL;
1055 }
1056 return 0;
1057 }
1058
Sebastian Andrzej Siewior4ce63572012-10-22 22:15:08 +02001059 if (intf == agdev->as_out_intf) {
Jassi Brar132fcb42012-02-02 22:01:34 +05301060 ep = agdev->out_ep;
1061 prm = &uac2->c_prm;
1062 config_ep_by_speed(gadget, fn, ep);
Sebastian Andrzej Siewior4ce63572012-10-22 22:15:08 +02001063 agdev->as_out_alt = alt;
1064 } else if (intf == agdev->as_in_intf) {
Jassi Brar132fcb42012-02-02 22:01:34 +05301065 ep = agdev->in_ep;
1066 prm = &uac2->p_prm;
1067 config_ep_by_speed(gadget, fn, ep);
Sebastian Andrzej Siewior4ce63572012-10-22 22:15:08 +02001068 agdev->as_in_alt = alt;
Jassi Brar132fcb42012-02-02 22:01:34 +05301069 } else {
1070 dev_err(&uac2->pdev.dev,
1071 "%s:%d Error!\n", __func__, __LINE__);
1072 return -EINVAL;
1073 }
1074
1075 if (alt == 0) {
1076 free_ep(prm, ep);
1077 return 0;
1078 }
1079
1080 prm->ep_enabled = true;
1081 usb_ep_enable(ep);
1082
1083 for (i = 0; i < USB_XFERS; i++) {
1084 if (prm->ureq[i].req) {
1085 if (usb_ep_queue(ep, prm->ureq[i].req, GFP_ATOMIC))
1086 dev_err(&uac2->pdev.dev, "%d Error!\n",
1087 __LINE__);
1088 continue;
1089 }
1090
1091 req = usb_ep_alloc_request(ep, GFP_ATOMIC);
1092 if (req == NULL) {
1093 dev_err(&uac2->pdev.dev,
1094 "%s:%d Error!\n", __func__, __LINE__);
1095 return -EINVAL;
1096 }
1097
1098 prm->ureq[i].req = req;
1099 prm->ureq[i].pp = prm;
1100
1101 req->zero = 0;
Jassi Brar132fcb42012-02-02 22:01:34 +05301102 req->context = &prm->ureq[i];
1103 req->length = prm->max_psize;
1104 req->complete = agdev_iso_complete;
1105 req->buf = prm->rbuf + i * req->length;
1106
1107 if (usb_ep_queue(ep, req, GFP_ATOMIC))
1108 dev_err(&uac2->pdev.dev, "%d Error!\n", __LINE__);
1109 }
1110
1111 return 0;
1112}
1113
1114static int
1115afunc_get_alt(struct usb_function *fn, unsigned intf)
1116{
1117 struct audio_dev *agdev = func_to_agdev(fn);
1118 struct snd_uac2_chip *uac2 = &agdev->uac2;
1119
Sebastian Andrzej Siewior4ce63572012-10-22 22:15:08 +02001120 if (intf == agdev->ac_intf)
1121 return agdev->ac_alt;
1122 else if (intf == agdev->as_out_intf)
1123 return agdev->as_out_alt;
1124 else if (intf == agdev->as_in_intf)
1125 return agdev->as_in_alt;
Jassi Brar132fcb42012-02-02 22:01:34 +05301126 else
1127 dev_err(&uac2->pdev.dev,
1128 "%s:%d Invalid Interface %d!\n",
1129 __func__, __LINE__, intf);
1130
1131 return -EINVAL;
1132}
1133
1134static void
1135afunc_disable(struct usb_function *fn)
1136{
1137 struct audio_dev *agdev = func_to_agdev(fn);
1138 struct snd_uac2_chip *uac2 = &agdev->uac2;
1139
1140 free_ep(&uac2->p_prm, agdev->in_ep);
Sebastian Andrzej Siewior4ce63572012-10-22 22:15:08 +02001141 agdev->as_in_alt = 0;
Jassi Brar132fcb42012-02-02 22:01:34 +05301142
1143 free_ep(&uac2->c_prm, agdev->out_ep);
Sebastian Andrzej Siewior4ce63572012-10-22 22:15:08 +02001144 agdev->as_out_alt = 0;
Jassi Brar132fcb42012-02-02 22:01:34 +05301145}
1146
1147static int
1148in_rq_cur(struct usb_function *fn, const struct usb_ctrlrequest *cr)
1149{
1150 struct usb_request *req = fn->config->cdev->req;
1151 struct audio_dev *agdev = func_to_agdev(fn);
1152 struct snd_uac2_chip *uac2 = &agdev->uac2;
1153 u16 w_length = le16_to_cpu(cr->wLength);
1154 u16 w_index = le16_to_cpu(cr->wIndex);
1155 u16 w_value = le16_to_cpu(cr->wValue);
1156 u8 entity_id = (w_index >> 8) & 0xff;
1157 u8 control_selector = w_value >> 8;
1158 int value = -EOPNOTSUPP;
1159
1160 if (control_selector == UAC2_CS_CONTROL_SAM_FREQ) {
1161 struct cntrl_cur_lay3 c;
1162
1163 if (entity_id == USB_IN_CLK_ID)
1164 c.dCUR = p_srate;
1165 else if (entity_id == USB_OUT_CLK_ID)
1166 c.dCUR = c_srate;
1167
1168 value = min_t(unsigned, w_length, sizeof c);
1169 memcpy(req->buf, &c, value);
1170 } else if (control_selector == UAC2_CS_CONTROL_CLOCK_VALID) {
1171 *(u8 *)req->buf = 1;
1172 value = min_t(unsigned, w_length, 1);
1173 } else {
1174 dev_err(&uac2->pdev.dev,
1175 "%s:%d control_selector=%d TODO!\n",
1176 __func__, __LINE__, control_selector);
1177 }
1178
1179 return value;
1180}
1181
1182static int
1183in_rq_range(struct usb_function *fn, const struct usb_ctrlrequest *cr)
1184{
1185 struct usb_request *req = fn->config->cdev->req;
1186 struct audio_dev *agdev = func_to_agdev(fn);
1187 struct snd_uac2_chip *uac2 = &agdev->uac2;
1188 u16 w_length = le16_to_cpu(cr->wLength);
1189 u16 w_index = le16_to_cpu(cr->wIndex);
1190 u16 w_value = le16_to_cpu(cr->wValue);
1191 u8 entity_id = (w_index >> 8) & 0xff;
1192 u8 control_selector = w_value >> 8;
1193 struct cntrl_range_lay3 r;
1194 int value = -EOPNOTSUPP;
1195
1196 if (control_selector == UAC2_CS_CONTROL_SAM_FREQ) {
1197 if (entity_id == USB_IN_CLK_ID)
1198 r.dMIN = p_srate;
1199 else if (entity_id == USB_OUT_CLK_ID)
1200 r.dMIN = c_srate;
1201 else
1202 return -EOPNOTSUPP;
1203
1204 r.dMAX = r.dMIN;
1205 r.dRES = 0;
1206 r.wNumSubRanges = 1;
1207
1208 value = min_t(unsigned, w_length, sizeof r);
1209 memcpy(req->buf, &r, value);
1210 } else {
1211 dev_err(&uac2->pdev.dev,
1212 "%s:%d control_selector=%d TODO!\n",
1213 __func__, __LINE__, control_selector);
1214 }
1215
1216 return value;
1217}
1218
1219static int
1220ac_rq_in(struct usb_function *fn, const struct usb_ctrlrequest *cr)
1221{
1222 if (cr->bRequest == UAC2_CS_CUR)
1223 return in_rq_cur(fn, cr);
1224 else if (cr->bRequest == UAC2_CS_RANGE)
1225 return in_rq_range(fn, cr);
1226 else
1227 return -EOPNOTSUPP;
1228}
1229
1230static int
1231out_rq_cur(struct usb_function *fn, const struct usb_ctrlrequest *cr)
1232{
1233 u16 w_length = le16_to_cpu(cr->wLength);
1234 u16 w_value = le16_to_cpu(cr->wValue);
1235 u8 control_selector = w_value >> 8;
1236
1237 if (control_selector == UAC2_CS_CONTROL_SAM_FREQ)
1238 return w_length;
1239
1240 return -EOPNOTSUPP;
1241}
1242
1243static int
1244setup_rq_inf(struct usb_function *fn, const struct usb_ctrlrequest *cr)
1245{
1246 struct audio_dev *agdev = func_to_agdev(fn);
1247 struct snd_uac2_chip *uac2 = &agdev->uac2;
1248 u16 w_index = le16_to_cpu(cr->wIndex);
1249 u8 intf = w_index & 0xff;
1250
Sebastian Andrzej Siewior4ce63572012-10-22 22:15:08 +02001251 if (intf != agdev->ac_intf) {
Jassi Brar132fcb42012-02-02 22:01:34 +05301252 dev_err(&uac2->pdev.dev,
1253 "%s:%d Error!\n", __func__, __LINE__);
1254 return -EOPNOTSUPP;
1255 }
1256
1257 if (cr->bRequestType & USB_DIR_IN)
1258 return ac_rq_in(fn, cr);
1259 else if (cr->bRequest == UAC2_CS_CUR)
1260 return out_rq_cur(fn, cr);
1261
1262 return -EOPNOTSUPP;
1263}
1264
1265static int
1266afunc_setup(struct usb_function *fn, const struct usb_ctrlrequest *cr)
1267{
1268 struct usb_composite_dev *cdev = fn->config->cdev;
1269 struct audio_dev *agdev = func_to_agdev(fn);
1270 struct snd_uac2_chip *uac2 = &agdev->uac2;
1271 struct usb_request *req = cdev->req;
1272 u16 w_length = le16_to_cpu(cr->wLength);
1273 int value = -EOPNOTSUPP;
1274
1275 /* Only Class specific requests are supposed to reach here */
1276 if ((cr->bRequestType & USB_TYPE_MASK) != USB_TYPE_CLASS)
1277 return -EOPNOTSUPP;
1278
1279 if ((cr->bRequestType & USB_RECIP_MASK) == USB_RECIP_INTERFACE)
1280 value = setup_rq_inf(fn, cr);
1281 else
1282 dev_err(&uac2->pdev.dev, "%s:%d Error!\n", __func__, __LINE__);
1283
1284 if (value >= 0) {
1285 req->length = value;
1286 req->zero = value < w_length;
1287 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
1288 if (value < 0) {
1289 dev_err(&uac2->pdev.dev,
1290 "%s:%d Error!\n", __func__, __LINE__);
1291 req->status = 0;
1292 }
1293 }
1294
1295 return value;
1296}
1297
1298static int audio_bind_config(struct usb_configuration *cfg)
1299{
Sebastian Andrzej Siewior1616e992012-10-22 22:15:10 +02001300 int res;
Jassi Brar132fcb42012-02-02 22:01:34 +05301301
1302 agdev_g = kzalloc(sizeof *agdev_g, GFP_KERNEL);
1303 if (agdev_g == NULL) {
1304 printk(KERN_ERR "Unable to allocate audio gadget\n");
1305 return -ENOMEM;
1306 }
1307
Sebastian Andrzej Siewior1616e992012-10-22 22:15:10 +02001308 res = usb_string_ids_tab(cfg->cdev, strings_fn);
1309 if (res)
1310 return res;
1311 iad_desc.iFunction = strings_fn[STR_ASSOC].id;
1312 std_ac_if_desc.iInterface = strings_fn[STR_IF_CTRL].id;
1313 in_clk_src_desc.iClockSource = strings_fn[STR_CLKSRC_IN].id;
1314 out_clk_src_desc.iClockSource = strings_fn[STR_CLKSRC_OUT].id;
1315 usb_out_it_desc.iTerminal = strings_fn[STR_USB_IT].id;
1316 io_in_it_desc.iTerminal = strings_fn[STR_IO_IT].id;
1317 usb_in_ot_desc.iTerminal = strings_fn[STR_USB_OT].id;
1318 io_out_ot_desc.iTerminal = strings_fn[STR_IO_OT].id;
1319 std_as_out_if0_desc.iInterface = strings_fn[STR_AS_OUT_ALT0].id;
1320 std_as_out_if1_desc.iInterface = strings_fn[STR_AS_OUT_ALT1].id;
1321 std_as_in_if0_desc.iInterface = strings_fn[STR_AS_IN_ALT0].id;
1322 std_as_in_if1_desc.iInterface = strings_fn[STR_AS_IN_ALT1].id;
Jassi Brar132fcb42012-02-02 22:01:34 +05301323
1324 agdev_g->func.name = "uac2_func";
1325 agdev_g->func.strings = fn_strings;
1326 agdev_g->func.bind = afunc_bind;
1327 agdev_g->func.unbind = afunc_unbind;
1328 agdev_g->func.set_alt = afunc_set_alt;
1329 agdev_g->func.get_alt = afunc_get_alt;
1330 agdev_g->func.disable = afunc_disable;
1331 agdev_g->func.setup = afunc_setup;
1332
1333 /* Initialize the configurable parameters */
1334 usb_out_it_desc.bNrChannels = num_channels(c_chmask);
1335 usb_out_it_desc.bmChannelConfig = cpu_to_le32(c_chmask);
1336 io_in_it_desc.bNrChannels = num_channels(p_chmask);
1337 io_in_it_desc.bmChannelConfig = cpu_to_le32(p_chmask);
1338 as_out_hdr_desc.bNrChannels = num_channels(c_chmask);
1339 as_out_hdr_desc.bmChannelConfig = cpu_to_le32(c_chmask);
1340 as_in_hdr_desc.bNrChannels = num_channels(p_chmask);
1341 as_in_hdr_desc.bmChannelConfig = cpu_to_le32(p_chmask);
1342 as_out_fmt1_desc.bSubslotSize = c_ssize;
1343 as_out_fmt1_desc.bBitResolution = c_ssize * 8;
1344 as_in_fmt1_desc.bSubslotSize = p_ssize;
1345 as_in_fmt1_desc.bBitResolution = p_ssize * 8;
1346
1347 snprintf(clksrc_in, sizeof(clksrc_in), "%uHz", p_srate);
1348 snprintf(clksrc_out, sizeof(clksrc_out), "%uHz", c_srate);
1349
1350 res = usb_add_function(cfg, &agdev_g->func);
1351 if (res < 0)
1352 kfree(agdev_g);
1353
1354 return res;
1355}
1356
1357static void
1358uac2_unbind_config(struct usb_configuration *cfg)
1359{
1360 kfree(agdev_g);
1361 agdev_g = NULL;
1362}