blob: 22910373510fcf439310443599015e698bb1b8e8 [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
Andrzej Pietrasiewiczf8f93d22014-07-22 19:58:30 +020023#include "u_uac2.h"
24
Jassi Brar132fcb42012-02-02 22:01:34 +053025/* Keep everyone on toes */
26#define USB_XFERS 2
27
28/*
29 * The driver implements a simple UAC_2 topology.
30 * USB-OUT -> IT_1 -> OT_3 -> ALSA_Capture
31 * ALSA_Playback -> IT_2 -> OT_4 -> USB-IN
32 * Capture and Playback sampling rates are independently
33 * controlled by two clock sources :
34 * CLK_5 := c_srate, and CLK_6 := p_srate
35 */
36#define USB_OUT_IT_ID 1
37#define IO_IN_IT_ID 2
38#define IO_OUT_OT_ID 3
39#define USB_IN_OT_ID 4
40#define USB_OUT_CLK_ID 5
41#define USB_IN_CLK_ID 6
42
43#define CONTROL_ABSENT 0
44#define CONTROL_RDONLY 1
45#define CONTROL_RDWR 3
46
47#define CLK_FREQ_CTRL 0
48#define CLK_VLD_CTRL 2
49
50#define COPY_CTRL 0
51#define CONN_CTRL 2
52#define OVRLD_CTRL 4
53#define CLSTR_CTRL 6
54#define UNFLW_CTRL 8
55#define OVFLW_CTRL 10
56
57const char *uac2_name = "snd_uac2";
58
59struct uac2_req {
60 struct uac2_rtd_params *pp; /* parent param */
61 struct usb_request *req;
62};
63
64struct uac2_rtd_params {
Jassi Brareb127cb2013-05-30 18:23:33 +053065 struct snd_uac2_chip *uac2; /* parent chip */
Jassi Brar132fcb42012-02-02 22:01:34 +053066 bool ep_enabled; /* if the ep is enabled */
67 /* Size of the ring buffer */
68 size_t dma_bytes;
69 unsigned char *dma_area;
70
71 struct snd_pcm_substream *ss;
72
73 /* Ring buffer */
74 ssize_t hw_ptr;
75
76 void *rbuf;
77
78 size_t period_size;
79
80 unsigned max_psize;
81 struct uac2_req ureq[USB_XFERS];
82
83 spinlock_t lock;
84};
85
86struct snd_uac2_chip {
87 struct platform_device pdev;
88 struct platform_driver pdrv;
89
90 struct uac2_rtd_params p_prm;
91 struct uac2_rtd_params c_prm;
92
93 struct snd_card *card;
94 struct snd_pcm *pcm;
95};
96
97#define BUFF_SIZE_MAX (PAGE_SIZE * 16)
98#define PRD_SIZE_MAX PAGE_SIZE
99#define MIN_PERIODS 4
100
101static struct snd_pcm_hardware uac2_pcm_hardware = {
102 .info = SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BLOCK_TRANSFER
103 | SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID
104 | SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME,
105 .rates = SNDRV_PCM_RATE_CONTINUOUS,
106 .periods_max = BUFF_SIZE_MAX / PRD_SIZE_MAX,
107 .buffer_bytes_max = BUFF_SIZE_MAX,
108 .period_bytes_max = PRD_SIZE_MAX,
109 .periods_min = MIN_PERIODS,
110};
111
112struct audio_dev {
Sebastian Andrzej Siewior4ce63572012-10-22 22:15:08 +0200113 u8 ac_intf, ac_alt;
114 u8 as_out_intf, as_out_alt;
115 u8 as_in_intf, as_in_alt;
Jassi Brar132fcb42012-02-02 22:01:34 +0530116
117 struct usb_ep *in_ep, *out_ep;
118 struct usb_function func;
119
120 /* The ALSA Sound Card it represents on the USB-Client side */
121 struct snd_uac2_chip uac2;
122};
123
Jassi Brar132fcb42012-02-02 22:01:34 +0530124static inline
125struct audio_dev *func_to_agdev(struct usb_function *f)
126{
127 return container_of(f, struct audio_dev, func);
128}
129
130static inline
131struct audio_dev *uac2_to_agdev(struct snd_uac2_chip *u)
132{
133 return container_of(u, struct audio_dev, uac2);
134}
135
136static inline
137struct snd_uac2_chip *pdev_to_uac2(struct platform_device *p)
138{
139 return container_of(p, struct snd_uac2_chip, pdev);
140}
141
142static inline
Jassi Brar132fcb42012-02-02 22:01:34 +0530143uint num_channels(uint chanmask)
144{
145 uint num = 0;
146
147 while (chanmask) {
148 num += (chanmask & 1);
149 chanmask >>= 1;
150 }
151
152 return num;
153}
154
155static void
156agdev_iso_complete(struct usb_ep *ep, struct usb_request *req)
157{
158 unsigned pending;
159 unsigned long flags;
160 bool update_alsa = false;
161 unsigned char *src, *dst;
162 int status = req->status;
163 struct uac2_req *ur = req->context;
164 struct snd_pcm_substream *substream;
165 struct uac2_rtd_params *prm = ur->pp;
Jassi Brareb127cb2013-05-30 18:23:33 +0530166 struct snd_uac2_chip *uac2 = prm->uac2;
Jassi Brar132fcb42012-02-02 22:01:34 +0530167
168 /* i/f shutting down */
Andrzej Pietrasiewicz1ade5d72014-05-15 13:43:50 +0200169 if (!prm->ep_enabled || req->status == -ESHUTDOWN)
Jassi Brar132fcb42012-02-02 22:01:34 +0530170 return;
171
172 /*
173 * We can't really do much about bad xfers.
174 * Afterall, the ISOCH xfers could fail legitimately.
175 */
176 if (status)
177 pr_debug("%s: iso_complete status(%d) %d/%d\n",
178 __func__, status, req->actual, req->length);
179
180 substream = prm->ss;
181
182 /* Do nothing if ALSA isn't active */
183 if (!substream)
184 goto exit;
185
186 spin_lock_irqsave(&prm->lock, flags);
187
188 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
189 src = prm->dma_area + prm->hw_ptr;
190 req->actual = req->length;
191 dst = req->buf;
192 } else {
193 dst = prm->dma_area + prm->hw_ptr;
194 src = req->buf;
195 }
196
197 pending = prm->hw_ptr % prm->period_size;
198 pending += req->actual;
199 if (pending >= prm->period_size)
200 update_alsa = true;
201
202 prm->hw_ptr = (prm->hw_ptr + req->actual) % prm->dma_bytes;
203
204 spin_unlock_irqrestore(&prm->lock, flags);
205
206 /* Pack USB load in ALSA ring buffer */
207 memcpy(dst, src, req->actual);
208exit:
209 if (usb_ep_queue(ep, req, GFP_ATOMIC))
210 dev_err(&uac2->pdev.dev, "%d Error!\n", __LINE__);
211
212 if (update_alsa)
213 snd_pcm_period_elapsed(substream);
214
215 return;
216}
217
218static int
219uac2_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
220{
221 struct snd_uac2_chip *uac2 = snd_pcm_substream_chip(substream);
Jassi Brar132fcb42012-02-02 22:01:34 +0530222 struct uac2_rtd_params *prm;
223 unsigned long flags;
Jassi Brar132fcb42012-02-02 22:01:34 +0530224 int err = 0;
225
Wei Yongjunb6424352012-12-02 05:33:08 -0500226 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
Jassi Brar132fcb42012-02-02 22:01:34 +0530227 prm = &uac2->p_prm;
Wei Yongjunb6424352012-12-02 05:33:08 -0500228 else
Jassi Brar132fcb42012-02-02 22:01:34 +0530229 prm = &uac2->c_prm;
Jassi Brar132fcb42012-02-02 22:01:34 +0530230
231 spin_lock_irqsave(&prm->lock, flags);
232
233 /* Reset */
234 prm->hw_ptr = 0;
235
236 switch (cmd) {
237 case SNDRV_PCM_TRIGGER_START:
238 case SNDRV_PCM_TRIGGER_RESUME:
239 prm->ss = substream;
240 break;
241 case SNDRV_PCM_TRIGGER_STOP:
242 case SNDRV_PCM_TRIGGER_SUSPEND:
243 prm->ss = NULL;
244 break;
245 default:
246 err = -EINVAL;
247 }
248
249 spin_unlock_irqrestore(&prm->lock, flags);
250
251 /* Clear buffer after Play stops */
252 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK && !prm->ss)
253 memset(prm->rbuf, 0, prm->max_psize * USB_XFERS);
254
255 return err;
256}
257
258static snd_pcm_uframes_t uac2_pcm_pointer(struct snd_pcm_substream *substream)
259{
260 struct snd_uac2_chip *uac2 = snd_pcm_substream_chip(substream);
261 struct uac2_rtd_params *prm;
262
263 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
264 prm = &uac2->p_prm;
265 else
266 prm = &uac2->c_prm;
267
268 return bytes_to_frames(substream->runtime, prm->hw_ptr);
269}
270
271static int uac2_pcm_hw_params(struct snd_pcm_substream *substream,
272 struct snd_pcm_hw_params *hw_params)
273{
274 struct snd_uac2_chip *uac2 = snd_pcm_substream_chip(substream);
275 struct uac2_rtd_params *prm;
276 int err;
277
278 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
279 prm = &uac2->p_prm;
280 else
281 prm = &uac2->c_prm;
282
283 err = snd_pcm_lib_malloc_pages(substream,
284 params_buffer_bytes(hw_params));
285 if (err >= 0) {
286 prm->dma_bytes = substream->runtime->dma_bytes;
287 prm->dma_area = substream->runtime->dma_area;
288 prm->period_size = params_period_bytes(hw_params);
289 }
290
291 return err;
292}
293
294static int uac2_pcm_hw_free(struct snd_pcm_substream *substream)
295{
296 struct snd_uac2_chip *uac2 = snd_pcm_substream_chip(substream);
297 struct uac2_rtd_params *prm;
298
299 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
300 prm = &uac2->p_prm;
301 else
302 prm = &uac2->c_prm;
303
304 prm->dma_area = NULL;
305 prm->dma_bytes = 0;
306 prm->period_size = 0;
307
308 return snd_pcm_lib_free_pages(substream);
309}
310
311static int uac2_pcm_open(struct snd_pcm_substream *substream)
312{
313 struct snd_uac2_chip *uac2 = snd_pcm_substream_chip(substream);
314 struct snd_pcm_runtime *runtime = substream->runtime;
Andrzej Pietrasiewiczf8f93d22014-07-22 19:58:30 +0200315 struct audio_dev *audio_dev;
316 struct f_uac2_opts *opts;
317 int p_ssize, c_ssize;
318 int p_srate, c_srate;
319 int p_chmask, c_chmask;
320
321 audio_dev = uac2_to_agdev(uac2);
322 opts = container_of(audio_dev->func.fi, struct f_uac2_opts, func_inst);
323 p_ssize = opts->p_ssize;
324 c_ssize = opts->c_ssize;
325 p_srate = opts->p_srate;
326 c_srate = opts->c_srate;
327 p_chmask = opts->p_chmask;
328 c_chmask = opts->c_chmask;
Jassi Brar132fcb42012-02-02 22:01:34 +0530329
330 runtime->hw = uac2_pcm_hardware;
331
332 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
333 spin_lock_init(&uac2->p_prm.lock);
334 runtime->hw.rate_min = p_srate;
Sebastian Reimers55f78402014-07-03 20:15:28 +0200335 switch (p_ssize) {
336 case 3:
337 runtime->hw.formats = SNDRV_PCM_FMTBIT_S24_3LE;
338 break;
339 case 4:
340 runtime->hw.formats = SNDRV_PCM_FMTBIT_S32_LE;
341 break;
342 default:
343 runtime->hw.formats = SNDRV_PCM_FMTBIT_S16_LE;
344 break;
345 }
Jassi Brar132fcb42012-02-02 22:01:34 +0530346 runtime->hw.channels_min = num_channels(p_chmask);
347 runtime->hw.period_bytes_min = 2 * uac2->p_prm.max_psize
348 / runtime->hw.periods_min;
349 } else {
350 spin_lock_init(&uac2->c_prm.lock);
351 runtime->hw.rate_min = c_srate;
Sebastian Reimers55f78402014-07-03 20:15:28 +0200352 switch (c_ssize) {
353 case 3:
354 runtime->hw.formats = SNDRV_PCM_FMTBIT_S24_3LE;
355 break;
356 case 4:
357 runtime->hw.formats = SNDRV_PCM_FMTBIT_S32_LE;
358 break;
359 default:
360 runtime->hw.formats = SNDRV_PCM_FMTBIT_S16_LE;
361 break;
362 }
Jassi Brar132fcb42012-02-02 22:01:34 +0530363 runtime->hw.channels_min = num_channels(c_chmask);
364 runtime->hw.period_bytes_min = 2 * uac2->c_prm.max_psize
365 / runtime->hw.periods_min;
366 }
367
368 runtime->hw.rate_max = runtime->hw.rate_min;
369 runtime->hw.channels_max = runtime->hw.channels_min;
370
371 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
372
373 return 0;
374}
375
376/* ALSA cries without these function pointers */
377static int uac2_pcm_null(struct snd_pcm_substream *substream)
378{
379 return 0;
380}
381
382static struct snd_pcm_ops uac2_pcm_ops = {
383 .open = uac2_pcm_open,
384 .close = uac2_pcm_null,
385 .ioctl = snd_pcm_lib_ioctl,
386 .hw_params = uac2_pcm_hw_params,
387 .hw_free = uac2_pcm_hw_free,
388 .trigger = uac2_pcm_trigger,
389 .pointer = uac2_pcm_pointer,
390 .prepare = uac2_pcm_null,
391};
392
Bill Pemberton41ac7b32012-11-19 13:21:48 -0500393static int snd_uac2_probe(struct platform_device *pdev)
Jassi Brar132fcb42012-02-02 22:01:34 +0530394{
395 struct snd_uac2_chip *uac2 = pdev_to_uac2(pdev);
396 struct snd_card *card;
397 struct snd_pcm *pcm;
Andrzej Pietrasiewiczf8f93d22014-07-22 19:58:30 +0200398 struct audio_dev *audio_dev;
399 struct f_uac2_opts *opts;
Jassi Brar132fcb42012-02-02 22:01:34 +0530400 int err;
Andrzej Pietrasiewiczf8f93d22014-07-22 19:58:30 +0200401 int p_chmask, c_chmask;
402
403 audio_dev = uac2_to_agdev(uac2);
404 opts = container_of(audio_dev->func.fi, struct f_uac2_opts, func_inst);
405 p_chmask = opts->p_chmask;
406 c_chmask = opts->c_chmask;
Jassi Brar132fcb42012-02-02 22:01:34 +0530407
408 /* Choose any slot, with no id */
Takashi Iwai13345092014-01-29 15:04:31 +0100409 err = snd_card_new(&pdev->dev, -1, NULL, THIS_MODULE, 0, &card);
Jassi Brar132fcb42012-02-02 22:01:34 +0530410 if (err < 0)
411 return err;
412
413 uac2->card = card;
414
415 /*
416 * Create first PCM device
417 * Create a substream only for non-zero channel streams
418 */
419 err = snd_pcm_new(uac2->card, "UAC2 PCM", 0,
420 p_chmask ? 1 : 0, c_chmask ? 1 : 0, &pcm);
421 if (err < 0)
422 goto snd_fail;
423
424 strcpy(pcm->name, "UAC2 PCM");
425 pcm->private_data = uac2;
426
427 uac2->pcm = pcm;
428
429 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &uac2_pcm_ops);
430 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &uac2_pcm_ops);
431
432 strcpy(card->driver, "UAC2_Gadget");
433 strcpy(card->shortname, "UAC2_Gadget");
434 sprintf(card->longname, "UAC2_Gadget %i", pdev->id);
435
Jassi Brar132fcb42012-02-02 22:01:34 +0530436 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS,
437 snd_dma_continuous_data(GFP_KERNEL), 0, BUFF_SIZE_MAX);
438
439 err = snd_card_register(card);
440 if (!err) {
441 platform_set_drvdata(pdev, card);
442 return 0;
443 }
444
445snd_fail:
446 snd_card_free(card);
447
448 uac2->pcm = NULL;
449 uac2->card = NULL;
450
451 return err;
452}
453
Sebastian Andrzej Siewior1e1a27c2012-09-06 20:11:02 +0200454static int snd_uac2_remove(struct platform_device *pdev)
Jassi Brar132fcb42012-02-02 22:01:34 +0530455{
456 struct snd_card *card = platform_get_drvdata(pdev);
457
Jassi Brar132fcb42012-02-02 22:01:34 +0530458 if (card)
459 return snd_card_free(card);
460
461 return 0;
462}
463
464static int alsa_uac2_init(struct audio_dev *agdev)
465{
466 struct snd_uac2_chip *uac2 = &agdev->uac2;
467 int err;
468
469 uac2->pdrv.probe = snd_uac2_probe;
470 uac2->pdrv.remove = snd_uac2_remove;
471 uac2->pdrv.driver.name = uac2_name;
472
473 uac2->pdev.id = 0;
474 uac2->pdev.name = uac2_name;
475
476 /* Register snd_uac2 driver */
477 err = platform_driver_register(&uac2->pdrv);
478 if (err)
479 return err;
480
481 /* Register snd_uac2 device */
482 err = platform_device_register(&uac2->pdev);
483 if (err)
484 platform_driver_unregister(&uac2->pdrv);
485
486 return err;
487}
488
489static void alsa_uac2_exit(struct audio_dev *agdev)
490{
491 struct snd_uac2_chip *uac2 = &agdev->uac2;
492
493 platform_driver_unregister(&uac2->pdrv);
494 platform_device_unregister(&uac2->pdev);
495}
496
497
498/* --------- USB Function Interface ------------- */
499
500enum {
501 STR_ASSOC,
502 STR_IF_CTRL,
503 STR_CLKSRC_IN,
504 STR_CLKSRC_OUT,
505 STR_USB_IT,
506 STR_IO_IT,
507 STR_USB_OT,
508 STR_IO_OT,
509 STR_AS_OUT_ALT0,
510 STR_AS_OUT_ALT1,
511 STR_AS_IN_ALT0,
512 STR_AS_IN_ALT1,
513};
514
Jassi Brar132fcb42012-02-02 22:01:34 +0530515static char clksrc_in[8];
516static char clksrc_out[8];
Jassi Brar132fcb42012-02-02 22:01:34 +0530517
518static struct usb_string strings_fn[] = {
Sebastian Andrzej Siewiorb36c3472012-10-22 22:15:09 +0200519 [STR_ASSOC].s = "Source/Sink",
520 [STR_IF_CTRL].s = "Topology Control",
Jassi Brar132fcb42012-02-02 22:01:34 +0530521 [STR_CLKSRC_IN].s = clksrc_in,
522 [STR_CLKSRC_OUT].s = clksrc_out,
Sebastian Andrzej Siewiorb36c3472012-10-22 22:15:09 +0200523 [STR_USB_IT].s = "USBH Out",
524 [STR_IO_IT].s = "USBD Out",
525 [STR_USB_OT].s = "USBH In",
526 [STR_IO_OT].s = "USBD In",
527 [STR_AS_OUT_ALT0].s = "Playback Inactive",
528 [STR_AS_OUT_ALT1].s = "Playback Active",
529 [STR_AS_IN_ALT0].s = "Capture Inactive",
530 [STR_AS_IN_ALT1].s = "Capture Active",
Jassi Brar132fcb42012-02-02 22:01:34 +0530531 { },
532};
533
534static struct usb_gadget_strings str_fn = {
535 .language = 0x0409, /* en-us */
536 .strings = strings_fn,
537};
538
539static struct usb_gadget_strings *fn_strings[] = {
540 &str_fn,
541 NULL,
542};
543
544static struct usb_qualifier_descriptor devqual_desc = {
545 .bLength = sizeof devqual_desc,
546 .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
547
548 .bcdUSB = cpu_to_le16(0x200),
549 .bDeviceClass = USB_CLASS_MISC,
550 .bDeviceSubClass = 0x02,
551 .bDeviceProtocol = 0x01,
552 .bNumConfigurations = 1,
553 .bRESERVED = 0,
554};
555
556static struct usb_interface_assoc_descriptor iad_desc = {
557 .bLength = sizeof iad_desc,
558 .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION,
559
560 .bFirstInterface = 0,
561 .bInterfaceCount = 3,
562 .bFunctionClass = USB_CLASS_AUDIO,
563 .bFunctionSubClass = UAC2_FUNCTION_SUBCLASS_UNDEFINED,
564 .bFunctionProtocol = UAC_VERSION_2,
565};
566
567/* Audio Control Interface */
568static struct usb_interface_descriptor std_ac_if_desc = {
569 .bLength = sizeof std_ac_if_desc,
570 .bDescriptorType = USB_DT_INTERFACE,
571
572 .bAlternateSetting = 0,
573 .bNumEndpoints = 0,
574 .bInterfaceClass = USB_CLASS_AUDIO,
575 .bInterfaceSubClass = USB_SUBCLASS_AUDIOCONTROL,
576 .bInterfaceProtocol = UAC_VERSION_2,
577};
578
579/* Clock source for IN traffic */
580struct uac_clock_source_descriptor in_clk_src_desc = {
581 .bLength = sizeof in_clk_src_desc,
582 .bDescriptorType = USB_DT_CS_INTERFACE,
583
584 .bDescriptorSubtype = UAC2_CLOCK_SOURCE,
585 .bClockID = USB_IN_CLK_ID,
586 .bmAttributes = UAC_CLOCK_SOURCE_TYPE_INT_FIXED,
587 .bmControls = (CONTROL_RDONLY << CLK_FREQ_CTRL),
588 .bAssocTerminal = 0,
589};
590
591/* Clock source for OUT traffic */
592struct uac_clock_source_descriptor out_clk_src_desc = {
593 .bLength = sizeof out_clk_src_desc,
594 .bDescriptorType = USB_DT_CS_INTERFACE,
595
596 .bDescriptorSubtype = UAC2_CLOCK_SOURCE,
597 .bClockID = USB_OUT_CLK_ID,
598 .bmAttributes = UAC_CLOCK_SOURCE_TYPE_INT_FIXED,
599 .bmControls = (CONTROL_RDONLY << CLK_FREQ_CTRL),
600 .bAssocTerminal = 0,
601};
602
603/* Input Terminal for USB_OUT */
604struct uac2_input_terminal_descriptor usb_out_it_desc = {
605 .bLength = sizeof usb_out_it_desc,
606 .bDescriptorType = USB_DT_CS_INTERFACE,
607
608 .bDescriptorSubtype = UAC_INPUT_TERMINAL,
609 .bTerminalID = USB_OUT_IT_ID,
610 .wTerminalType = cpu_to_le16(UAC_TERMINAL_STREAMING),
611 .bAssocTerminal = 0,
612 .bCSourceID = USB_OUT_CLK_ID,
613 .iChannelNames = 0,
614 .bmControls = (CONTROL_RDWR << COPY_CTRL),
615};
616
617/* Input Terminal for I/O-In */
618struct uac2_input_terminal_descriptor io_in_it_desc = {
619 .bLength = sizeof io_in_it_desc,
620 .bDescriptorType = USB_DT_CS_INTERFACE,
621
622 .bDescriptorSubtype = UAC_INPUT_TERMINAL,
623 .bTerminalID = IO_IN_IT_ID,
624 .wTerminalType = cpu_to_le16(UAC_INPUT_TERMINAL_UNDEFINED),
625 .bAssocTerminal = 0,
626 .bCSourceID = USB_IN_CLK_ID,
627 .iChannelNames = 0,
628 .bmControls = (CONTROL_RDWR << COPY_CTRL),
629};
630
631/* Ouput Terminal for USB_IN */
632struct uac2_output_terminal_descriptor usb_in_ot_desc = {
633 .bLength = sizeof usb_in_ot_desc,
634 .bDescriptorType = USB_DT_CS_INTERFACE,
635
636 .bDescriptorSubtype = UAC_OUTPUT_TERMINAL,
637 .bTerminalID = USB_IN_OT_ID,
638 .wTerminalType = cpu_to_le16(UAC_TERMINAL_STREAMING),
639 .bAssocTerminal = 0,
640 .bSourceID = IO_IN_IT_ID,
641 .bCSourceID = USB_IN_CLK_ID,
642 .bmControls = (CONTROL_RDWR << COPY_CTRL),
643};
644
645/* Ouput Terminal for I/O-Out */
646struct uac2_output_terminal_descriptor io_out_ot_desc = {
647 .bLength = sizeof io_out_ot_desc,
648 .bDescriptorType = USB_DT_CS_INTERFACE,
649
650 .bDescriptorSubtype = UAC_OUTPUT_TERMINAL,
651 .bTerminalID = IO_OUT_OT_ID,
652 .wTerminalType = cpu_to_le16(UAC_OUTPUT_TERMINAL_UNDEFINED),
653 .bAssocTerminal = 0,
654 .bSourceID = USB_OUT_IT_ID,
655 .bCSourceID = USB_OUT_CLK_ID,
656 .bmControls = (CONTROL_RDWR << COPY_CTRL),
657};
658
659struct uac2_ac_header_descriptor ac_hdr_desc = {
660 .bLength = sizeof ac_hdr_desc,
661 .bDescriptorType = USB_DT_CS_INTERFACE,
662
663 .bDescriptorSubtype = UAC_MS_HEADER,
664 .bcdADC = cpu_to_le16(0x200),
665 .bCategory = UAC2_FUNCTION_IO_BOX,
666 .wTotalLength = sizeof in_clk_src_desc + sizeof out_clk_src_desc
667 + sizeof usb_out_it_desc + sizeof io_in_it_desc
668 + sizeof usb_in_ot_desc + sizeof io_out_ot_desc,
669 .bmControls = 0,
670};
671
672/* Audio Streaming OUT Interface - Alt0 */
673static struct usb_interface_descriptor std_as_out_if0_desc = {
674 .bLength = sizeof std_as_out_if0_desc,
675 .bDescriptorType = USB_DT_INTERFACE,
676
677 .bAlternateSetting = 0,
678 .bNumEndpoints = 0,
679 .bInterfaceClass = USB_CLASS_AUDIO,
680 .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
681 .bInterfaceProtocol = UAC_VERSION_2,
682};
683
684/* Audio Streaming OUT Interface - Alt1 */
685static struct usb_interface_descriptor std_as_out_if1_desc = {
686 .bLength = sizeof std_as_out_if1_desc,
687 .bDescriptorType = USB_DT_INTERFACE,
688
689 .bAlternateSetting = 1,
690 .bNumEndpoints = 1,
691 .bInterfaceClass = USB_CLASS_AUDIO,
692 .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
693 .bInterfaceProtocol = UAC_VERSION_2,
694};
695
696/* Audio Stream OUT Intface Desc */
697struct uac2_as_header_descriptor as_out_hdr_desc = {
698 .bLength = sizeof as_out_hdr_desc,
699 .bDescriptorType = USB_DT_CS_INTERFACE,
700
701 .bDescriptorSubtype = UAC_AS_GENERAL,
702 .bTerminalLink = USB_OUT_IT_ID,
703 .bmControls = 0,
704 .bFormatType = UAC_FORMAT_TYPE_I,
705 .bmFormats = cpu_to_le32(UAC_FORMAT_TYPE_I_PCM),
706 .iChannelNames = 0,
707};
708
709/* Audio USB_OUT Format */
710struct uac2_format_type_i_descriptor as_out_fmt1_desc = {
711 .bLength = sizeof as_out_fmt1_desc,
712 .bDescriptorType = USB_DT_CS_INTERFACE,
713 .bDescriptorSubtype = UAC_FORMAT_TYPE,
714 .bFormatType = UAC_FORMAT_TYPE_I,
715};
716
717/* STD AS ISO OUT Endpoint */
718struct usb_endpoint_descriptor fs_epout_desc = {
719 .bLength = USB_DT_ENDPOINT_SIZE,
720 .bDescriptorType = USB_DT_ENDPOINT,
721
722 .bEndpointAddress = USB_DIR_OUT,
723 .bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC,
724 .bInterval = 1,
725};
726
727struct usb_endpoint_descriptor hs_epout_desc = {
728 .bLength = USB_DT_ENDPOINT_SIZE,
729 .bDescriptorType = USB_DT_ENDPOINT,
730
731 .bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC,
732 .bInterval = 4,
733};
734
735/* CS AS ISO OUT Endpoint */
736static struct uac2_iso_endpoint_descriptor as_iso_out_desc = {
737 .bLength = sizeof as_iso_out_desc,
738 .bDescriptorType = USB_DT_CS_ENDPOINT,
739
740 .bDescriptorSubtype = UAC_EP_GENERAL,
741 .bmAttributes = 0,
742 .bmControls = 0,
743 .bLockDelayUnits = 0,
744 .wLockDelay = 0,
745};
746
747/* Audio Streaming IN Interface - Alt0 */
748static struct usb_interface_descriptor std_as_in_if0_desc = {
749 .bLength = sizeof std_as_in_if0_desc,
750 .bDescriptorType = USB_DT_INTERFACE,
751
752 .bAlternateSetting = 0,
753 .bNumEndpoints = 0,
754 .bInterfaceClass = USB_CLASS_AUDIO,
755 .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
756 .bInterfaceProtocol = UAC_VERSION_2,
757};
758
759/* Audio Streaming IN Interface - Alt1 */
760static struct usb_interface_descriptor std_as_in_if1_desc = {
761 .bLength = sizeof std_as_in_if1_desc,
762 .bDescriptorType = USB_DT_INTERFACE,
763
764 .bAlternateSetting = 1,
765 .bNumEndpoints = 1,
766 .bInterfaceClass = USB_CLASS_AUDIO,
767 .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
768 .bInterfaceProtocol = UAC_VERSION_2,
769};
770
771/* Audio Stream IN Intface Desc */
772struct uac2_as_header_descriptor as_in_hdr_desc = {
773 .bLength = sizeof as_in_hdr_desc,
774 .bDescriptorType = USB_DT_CS_INTERFACE,
775
776 .bDescriptorSubtype = UAC_AS_GENERAL,
777 .bTerminalLink = USB_IN_OT_ID,
778 .bmControls = 0,
779 .bFormatType = UAC_FORMAT_TYPE_I,
780 .bmFormats = cpu_to_le32(UAC_FORMAT_TYPE_I_PCM),
781 .iChannelNames = 0,
782};
783
784/* Audio USB_IN Format */
785struct uac2_format_type_i_descriptor as_in_fmt1_desc = {
786 .bLength = sizeof as_in_fmt1_desc,
787 .bDescriptorType = USB_DT_CS_INTERFACE,
788 .bDescriptorSubtype = UAC_FORMAT_TYPE,
789 .bFormatType = UAC_FORMAT_TYPE_I,
790};
791
792/* STD AS ISO IN Endpoint */
793struct usb_endpoint_descriptor fs_epin_desc = {
794 .bLength = USB_DT_ENDPOINT_SIZE,
795 .bDescriptorType = USB_DT_ENDPOINT,
796
797 .bEndpointAddress = USB_DIR_IN,
798 .bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC,
799 .bInterval = 1,
800};
801
802struct usb_endpoint_descriptor hs_epin_desc = {
803 .bLength = USB_DT_ENDPOINT_SIZE,
804 .bDescriptorType = USB_DT_ENDPOINT,
805
806 .bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC,
807 .bInterval = 4,
808};
809
810/* CS AS ISO IN Endpoint */
811static struct uac2_iso_endpoint_descriptor as_iso_in_desc = {
812 .bLength = sizeof as_iso_in_desc,
813 .bDescriptorType = USB_DT_CS_ENDPOINT,
814
815 .bDescriptorSubtype = UAC_EP_GENERAL,
816 .bmAttributes = 0,
817 .bmControls = 0,
818 .bLockDelayUnits = 0,
819 .wLockDelay = 0,
820};
821
822static struct usb_descriptor_header *fs_audio_desc[] = {
823 (struct usb_descriptor_header *)&iad_desc,
824 (struct usb_descriptor_header *)&std_ac_if_desc,
825
826 (struct usb_descriptor_header *)&ac_hdr_desc,
827 (struct usb_descriptor_header *)&in_clk_src_desc,
828 (struct usb_descriptor_header *)&out_clk_src_desc,
829 (struct usb_descriptor_header *)&usb_out_it_desc,
830 (struct usb_descriptor_header *)&io_in_it_desc,
831 (struct usb_descriptor_header *)&usb_in_ot_desc,
832 (struct usb_descriptor_header *)&io_out_ot_desc,
833
834 (struct usb_descriptor_header *)&std_as_out_if0_desc,
835 (struct usb_descriptor_header *)&std_as_out_if1_desc,
836
837 (struct usb_descriptor_header *)&as_out_hdr_desc,
838 (struct usb_descriptor_header *)&as_out_fmt1_desc,
839 (struct usb_descriptor_header *)&fs_epout_desc,
840 (struct usb_descriptor_header *)&as_iso_out_desc,
841
842 (struct usb_descriptor_header *)&std_as_in_if0_desc,
843 (struct usb_descriptor_header *)&std_as_in_if1_desc,
844
845 (struct usb_descriptor_header *)&as_in_hdr_desc,
846 (struct usb_descriptor_header *)&as_in_fmt1_desc,
847 (struct usb_descriptor_header *)&fs_epin_desc,
848 (struct usb_descriptor_header *)&as_iso_in_desc,
849 NULL,
850};
851
852static struct usb_descriptor_header *hs_audio_desc[] = {
853 (struct usb_descriptor_header *)&iad_desc,
854 (struct usb_descriptor_header *)&std_ac_if_desc,
855
856 (struct usb_descriptor_header *)&ac_hdr_desc,
857 (struct usb_descriptor_header *)&in_clk_src_desc,
858 (struct usb_descriptor_header *)&out_clk_src_desc,
859 (struct usb_descriptor_header *)&usb_out_it_desc,
860 (struct usb_descriptor_header *)&io_in_it_desc,
861 (struct usb_descriptor_header *)&usb_in_ot_desc,
862 (struct usb_descriptor_header *)&io_out_ot_desc,
863
864 (struct usb_descriptor_header *)&std_as_out_if0_desc,
865 (struct usb_descriptor_header *)&std_as_out_if1_desc,
866
867 (struct usb_descriptor_header *)&as_out_hdr_desc,
868 (struct usb_descriptor_header *)&as_out_fmt1_desc,
869 (struct usb_descriptor_header *)&hs_epout_desc,
870 (struct usb_descriptor_header *)&as_iso_out_desc,
871
872 (struct usb_descriptor_header *)&std_as_in_if0_desc,
873 (struct usb_descriptor_header *)&std_as_in_if1_desc,
874
875 (struct usb_descriptor_header *)&as_in_hdr_desc,
876 (struct usb_descriptor_header *)&as_in_fmt1_desc,
877 (struct usb_descriptor_header *)&hs_epin_desc,
878 (struct usb_descriptor_header *)&as_iso_in_desc,
879 NULL,
880};
881
882struct cntrl_cur_lay3 {
883 __u32 dCUR;
884};
885
886struct cntrl_range_lay3 {
887 __u16 wNumSubRanges;
888 __u32 dMIN;
889 __u32 dMAX;
890 __u32 dRES;
891} __packed;
892
893static inline void
894free_ep(struct uac2_rtd_params *prm, struct usb_ep *ep)
895{
Jassi Brareb127cb2013-05-30 18:23:33 +0530896 struct snd_uac2_chip *uac2 = prm->uac2;
Jassi Brar132fcb42012-02-02 22:01:34 +0530897 int i;
898
899 prm->ep_enabled = false;
900
901 for (i = 0; i < USB_XFERS; i++) {
902 if (prm->ureq[i].req) {
903 usb_ep_dequeue(ep, prm->ureq[i].req);
904 usb_ep_free_request(ep, prm->ureq[i].req);
905 prm->ureq[i].req = NULL;
906 }
907 }
908
909 if (usb_ep_disable(ep))
910 dev_err(&uac2->pdev.dev,
911 "%s:%d Error!\n", __func__, __LINE__);
912}
913
Andrzej Pietrasiewiczf8f93d22014-07-22 19:58:30 +0200914static int
Jassi Brar132fcb42012-02-02 22:01:34 +0530915afunc_bind(struct usb_configuration *cfg, struct usb_function *fn)
916{
917 struct audio_dev *agdev = func_to_agdev(fn);
918 struct snd_uac2_chip *uac2 = &agdev->uac2;
919 struct usb_composite_dev *cdev = cfg->cdev;
920 struct usb_gadget *gadget = cdev->gadget;
921 struct uac2_rtd_params *prm;
Andrzej Pietrasiewiczf8f93d22014-07-22 19:58:30 +0200922 struct f_uac2_opts *uac2_opts;
Jassi Brar132fcb42012-02-02 22:01:34 +0530923 int ret;
924
Andrzej Pietrasiewiczf8f93d22014-07-22 19:58:30 +0200925 uac2_opts = container_of(fn->fi, struct f_uac2_opts, func_inst);
Andrzej Pietrasiewiczf8f93d22014-07-22 19:58:30 +0200926
927 ret = usb_string_ids_tab(cfg->cdev, strings_fn);
928 if (ret)
929 return ret;
930 iad_desc.iFunction = strings_fn[STR_ASSOC].id;
931 std_ac_if_desc.iInterface = strings_fn[STR_IF_CTRL].id;
932 in_clk_src_desc.iClockSource = strings_fn[STR_CLKSRC_IN].id;
933 out_clk_src_desc.iClockSource = strings_fn[STR_CLKSRC_OUT].id;
934 usb_out_it_desc.iTerminal = strings_fn[STR_USB_IT].id;
935 io_in_it_desc.iTerminal = strings_fn[STR_IO_IT].id;
936 usb_in_ot_desc.iTerminal = strings_fn[STR_USB_OT].id;
937 io_out_ot_desc.iTerminal = strings_fn[STR_IO_OT].id;
938 std_as_out_if0_desc.iInterface = strings_fn[STR_AS_OUT_ALT0].id;
939 std_as_out_if1_desc.iInterface = strings_fn[STR_AS_OUT_ALT1].id;
940 std_as_in_if0_desc.iInterface = strings_fn[STR_AS_IN_ALT0].id;
941 std_as_in_if1_desc.iInterface = strings_fn[STR_AS_IN_ALT1].id;
942
Andrzej Pietrasiewiczf8f93d22014-07-22 19:58:30 +0200943 /* Initialize the configurable parameters */
944 usb_out_it_desc.bNrChannels = num_channels(uac2_opts->c_chmask);
945 usb_out_it_desc.bmChannelConfig = cpu_to_le32(uac2_opts->c_chmask);
946 io_in_it_desc.bNrChannels = num_channels(uac2_opts->p_chmask);
947 io_in_it_desc.bmChannelConfig = cpu_to_le32(uac2_opts->p_chmask);
948 as_out_hdr_desc.bNrChannels = num_channels(uac2_opts->c_chmask);
949 as_out_hdr_desc.bmChannelConfig = cpu_to_le32(uac2_opts->c_chmask);
950 as_in_hdr_desc.bNrChannels = num_channels(uac2_opts->p_chmask);
951 as_in_hdr_desc.bmChannelConfig = cpu_to_le32(uac2_opts->p_chmask);
952 as_out_fmt1_desc.bSubslotSize = uac2_opts->c_ssize;
953 as_out_fmt1_desc.bBitResolution = uac2_opts->c_ssize * 8;
954 as_in_fmt1_desc.bSubslotSize = uac2_opts->p_ssize;
955 as_in_fmt1_desc.bBitResolution = uac2_opts->p_ssize * 8;
956
957 snprintf(clksrc_in, sizeof(clksrc_in), "%uHz", uac2_opts->p_srate);
958 snprintf(clksrc_out, sizeof(clksrc_out), "%uHz", uac2_opts->c_srate);
Andrzej Pietrasiewiczf8f93d22014-07-22 19:58:30 +0200959
Jassi Brar132fcb42012-02-02 22:01:34 +0530960 ret = usb_interface_id(cfg, fn);
961 if (ret < 0) {
962 dev_err(&uac2->pdev.dev,
963 "%s:%d Error!\n", __func__, __LINE__);
964 return ret;
965 }
966 std_ac_if_desc.bInterfaceNumber = ret;
Sebastian Andrzej Siewior4ce63572012-10-22 22:15:08 +0200967 agdev->ac_intf = ret;
968 agdev->ac_alt = 0;
Jassi Brar132fcb42012-02-02 22:01:34 +0530969
970 ret = usb_interface_id(cfg, fn);
971 if (ret < 0) {
972 dev_err(&uac2->pdev.dev,
973 "%s:%d Error!\n", __func__, __LINE__);
974 return ret;
975 }
976 std_as_out_if0_desc.bInterfaceNumber = ret;
977 std_as_out_if1_desc.bInterfaceNumber = ret;
Sebastian Andrzej Siewior4ce63572012-10-22 22:15:08 +0200978 agdev->as_out_intf = ret;
979 agdev->as_out_alt = 0;
Jassi Brar132fcb42012-02-02 22:01:34 +0530980
981 ret = usb_interface_id(cfg, fn);
982 if (ret < 0) {
983 dev_err(&uac2->pdev.dev,
984 "%s:%d Error!\n", __func__, __LINE__);
985 return ret;
986 }
987 std_as_in_if0_desc.bInterfaceNumber = ret;
988 std_as_in_if1_desc.bInterfaceNumber = ret;
Sebastian Andrzej Siewior4ce63572012-10-22 22:15:08 +0200989 agdev->as_in_intf = ret;
990 agdev->as_in_alt = 0;
Jassi Brar132fcb42012-02-02 22:01:34 +0530991
992 agdev->out_ep = usb_ep_autoconfig(gadget, &fs_epout_desc);
Sebastian Andrzej Siewior391aa852012-10-22 22:14:59 +0200993 if (!agdev->out_ep) {
Jassi Brar132fcb42012-02-02 22:01:34 +0530994 dev_err(&uac2->pdev.dev,
995 "%s:%d Error!\n", __func__, __LINE__);
Sebastian Andrzej Siewior391aa852012-10-22 22:14:59 +0200996 goto err;
997 }
Jassi Brar132fcb42012-02-02 22:01:34 +0530998 agdev->out_ep->driver_data = agdev;
999
1000 agdev->in_ep = usb_ep_autoconfig(gadget, &fs_epin_desc);
Sebastian Andrzej Siewior391aa852012-10-22 22:14:59 +02001001 if (!agdev->in_ep) {
Jassi Brar132fcb42012-02-02 22:01:34 +05301002 dev_err(&uac2->pdev.dev,
1003 "%s:%d Error!\n", __func__, __LINE__);
Sebastian Andrzej Siewior391aa852012-10-22 22:14:59 +02001004 goto err;
1005 }
Jassi Brar132fcb42012-02-02 22:01:34 +05301006 agdev->in_ep->driver_data = agdev;
1007
Jassi Brareb127cb2013-05-30 18:23:33 +05301008 uac2->p_prm.uac2 = uac2;
1009 uac2->c_prm.uac2 = uac2;
1010
Jassi Brar132fcb42012-02-02 22:01:34 +05301011 hs_epout_desc.bEndpointAddress = fs_epout_desc.bEndpointAddress;
1012 hs_epout_desc.wMaxPacketSize = fs_epout_desc.wMaxPacketSize;
1013 hs_epin_desc.bEndpointAddress = fs_epin_desc.bEndpointAddress;
1014 hs_epin_desc.wMaxPacketSize = fs_epin_desc.wMaxPacketSize;
1015
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +02001016 ret = usb_assign_descriptors(fn, fs_audio_desc, hs_audio_desc, NULL);
1017 if (ret)
1018 goto err;
Jassi Brar132fcb42012-02-02 22:01:34 +05301019
1020 prm = &agdev->uac2.c_prm;
1021 prm->max_psize = hs_epout_desc.wMaxPacketSize;
1022 prm->rbuf = kzalloc(prm->max_psize * USB_XFERS, GFP_KERNEL);
1023 if (!prm->rbuf) {
1024 prm->max_psize = 0;
Sebastian Andrzej Siewior391aa852012-10-22 22:14:59 +02001025 goto err;
Jassi Brar132fcb42012-02-02 22:01:34 +05301026 }
1027
1028 prm = &agdev->uac2.p_prm;
1029 prm->max_psize = hs_epin_desc.wMaxPacketSize;
1030 prm->rbuf = kzalloc(prm->max_psize * USB_XFERS, GFP_KERNEL);
1031 if (!prm->rbuf) {
1032 prm->max_psize = 0;
Sebastian Andrzej Siewior391aa852012-10-22 22:14:59 +02001033 goto err;
Jassi Brar132fcb42012-02-02 22:01:34 +05301034 }
1035
Sebastian Andrzej Siewior391aa852012-10-22 22:14:59 +02001036 ret = alsa_uac2_init(agdev);
1037 if (ret)
1038 goto err;
1039 return 0;
1040err:
1041 kfree(agdev->uac2.p_prm.rbuf);
1042 kfree(agdev->uac2.c_prm.rbuf);
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +02001043 usb_free_all_descriptors(fn);
Sebastian Andrzej Siewior391aa852012-10-22 22:14:59 +02001044 if (agdev->in_ep)
1045 agdev->in_ep->driver_data = NULL;
1046 if (agdev->out_ep)
1047 agdev->out_ep->driver_data = NULL;
1048 return -EINVAL;
Jassi Brar132fcb42012-02-02 22:01:34 +05301049}
1050
Jassi Brar132fcb42012-02-02 22:01:34 +05301051static int
1052afunc_set_alt(struct usb_function *fn, unsigned intf, unsigned alt)
1053{
1054 struct usb_composite_dev *cdev = fn->config->cdev;
1055 struct audio_dev *agdev = func_to_agdev(fn);
1056 struct snd_uac2_chip *uac2 = &agdev->uac2;
1057 struct usb_gadget *gadget = cdev->gadget;
1058 struct usb_request *req;
1059 struct usb_ep *ep;
1060 struct uac2_rtd_params *prm;
1061 int i;
1062
1063 /* No i/f has more than 2 alt settings */
1064 if (alt > 1) {
1065 dev_err(&uac2->pdev.dev,
1066 "%s:%d Error!\n", __func__, __LINE__);
1067 return -EINVAL;
1068 }
1069
Sebastian Andrzej Siewior4ce63572012-10-22 22:15:08 +02001070 if (intf == agdev->ac_intf) {
Jassi Brar132fcb42012-02-02 22:01:34 +05301071 /* Control I/f has only 1 AltSetting - 0 */
1072 if (alt) {
1073 dev_err(&uac2->pdev.dev,
1074 "%s:%d Error!\n", __func__, __LINE__);
1075 return -EINVAL;
1076 }
1077 return 0;
1078 }
1079
Sebastian Andrzej Siewior4ce63572012-10-22 22:15:08 +02001080 if (intf == agdev->as_out_intf) {
Jassi Brar132fcb42012-02-02 22:01:34 +05301081 ep = agdev->out_ep;
1082 prm = &uac2->c_prm;
1083 config_ep_by_speed(gadget, fn, ep);
Sebastian Andrzej Siewior4ce63572012-10-22 22:15:08 +02001084 agdev->as_out_alt = alt;
1085 } else if (intf == agdev->as_in_intf) {
Jassi Brar132fcb42012-02-02 22:01:34 +05301086 ep = agdev->in_ep;
1087 prm = &uac2->p_prm;
1088 config_ep_by_speed(gadget, fn, ep);
Sebastian Andrzej Siewior4ce63572012-10-22 22:15:08 +02001089 agdev->as_in_alt = alt;
Jassi Brar132fcb42012-02-02 22:01:34 +05301090 } else {
1091 dev_err(&uac2->pdev.dev,
1092 "%s:%d Error!\n", __func__, __LINE__);
1093 return -EINVAL;
1094 }
1095
1096 if (alt == 0) {
1097 free_ep(prm, ep);
1098 return 0;
1099 }
1100
1101 prm->ep_enabled = true;
1102 usb_ep_enable(ep);
1103
1104 for (i = 0; i < USB_XFERS; i++) {
1105 if (prm->ureq[i].req) {
1106 if (usb_ep_queue(ep, prm->ureq[i].req, GFP_ATOMIC))
1107 dev_err(&uac2->pdev.dev, "%d Error!\n",
1108 __LINE__);
1109 continue;
1110 }
1111
1112 req = usb_ep_alloc_request(ep, GFP_ATOMIC);
1113 if (req == NULL) {
1114 dev_err(&uac2->pdev.dev,
1115 "%s:%d Error!\n", __func__, __LINE__);
1116 return -EINVAL;
1117 }
1118
1119 prm->ureq[i].req = req;
1120 prm->ureq[i].pp = prm;
1121
1122 req->zero = 0;
Jassi Brar132fcb42012-02-02 22:01:34 +05301123 req->context = &prm->ureq[i];
1124 req->length = prm->max_psize;
1125 req->complete = agdev_iso_complete;
1126 req->buf = prm->rbuf + i * req->length;
1127
1128 if (usb_ep_queue(ep, req, GFP_ATOMIC))
1129 dev_err(&uac2->pdev.dev, "%d Error!\n", __LINE__);
1130 }
1131
1132 return 0;
1133}
1134
1135static int
1136afunc_get_alt(struct usb_function *fn, unsigned intf)
1137{
1138 struct audio_dev *agdev = func_to_agdev(fn);
1139 struct snd_uac2_chip *uac2 = &agdev->uac2;
1140
Sebastian Andrzej Siewior4ce63572012-10-22 22:15:08 +02001141 if (intf == agdev->ac_intf)
1142 return agdev->ac_alt;
1143 else if (intf == agdev->as_out_intf)
1144 return agdev->as_out_alt;
1145 else if (intf == agdev->as_in_intf)
1146 return agdev->as_in_alt;
Jassi Brar132fcb42012-02-02 22:01:34 +05301147 else
1148 dev_err(&uac2->pdev.dev,
1149 "%s:%d Invalid Interface %d!\n",
1150 __func__, __LINE__, intf);
1151
1152 return -EINVAL;
1153}
1154
1155static void
1156afunc_disable(struct usb_function *fn)
1157{
1158 struct audio_dev *agdev = func_to_agdev(fn);
1159 struct snd_uac2_chip *uac2 = &agdev->uac2;
1160
1161 free_ep(&uac2->p_prm, agdev->in_ep);
Sebastian Andrzej Siewior4ce63572012-10-22 22:15:08 +02001162 agdev->as_in_alt = 0;
Jassi Brar132fcb42012-02-02 22:01:34 +05301163
1164 free_ep(&uac2->c_prm, agdev->out_ep);
Sebastian Andrzej Siewior4ce63572012-10-22 22:15:08 +02001165 agdev->as_out_alt = 0;
Jassi Brar132fcb42012-02-02 22:01:34 +05301166}
1167
1168static int
1169in_rq_cur(struct usb_function *fn, const struct usb_ctrlrequest *cr)
1170{
1171 struct usb_request *req = fn->config->cdev->req;
1172 struct audio_dev *agdev = func_to_agdev(fn);
1173 struct snd_uac2_chip *uac2 = &agdev->uac2;
Andrzej Pietrasiewiczf8f93d22014-07-22 19:58:30 +02001174 struct f_uac2_opts *opts;
Jassi Brar132fcb42012-02-02 22:01:34 +05301175 u16 w_length = le16_to_cpu(cr->wLength);
1176 u16 w_index = le16_to_cpu(cr->wIndex);
1177 u16 w_value = le16_to_cpu(cr->wValue);
1178 u8 entity_id = (w_index >> 8) & 0xff;
1179 u8 control_selector = w_value >> 8;
1180 int value = -EOPNOTSUPP;
Andrzej Pietrasiewiczf8f93d22014-07-22 19:58:30 +02001181 int p_srate, c_srate;
1182
1183 opts = container_of(agdev->func.fi, struct f_uac2_opts, func_inst);
1184 p_srate = opts->p_srate;
1185 c_srate = opts->c_srate;
Jassi Brar132fcb42012-02-02 22:01:34 +05301186
1187 if (control_selector == UAC2_CS_CONTROL_SAM_FREQ) {
1188 struct cntrl_cur_lay3 c;
1189
1190 if (entity_id == USB_IN_CLK_ID)
1191 c.dCUR = p_srate;
1192 else if (entity_id == USB_OUT_CLK_ID)
1193 c.dCUR = c_srate;
1194
1195 value = min_t(unsigned, w_length, sizeof c);
1196 memcpy(req->buf, &c, value);
1197 } else if (control_selector == UAC2_CS_CONTROL_CLOCK_VALID) {
1198 *(u8 *)req->buf = 1;
1199 value = min_t(unsigned, w_length, 1);
1200 } else {
1201 dev_err(&uac2->pdev.dev,
1202 "%s:%d control_selector=%d TODO!\n",
1203 __func__, __LINE__, control_selector);
1204 }
1205
1206 return value;
1207}
1208
1209static int
1210in_rq_range(struct usb_function *fn, const struct usb_ctrlrequest *cr)
1211{
1212 struct usb_request *req = fn->config->cdev->req;
1213 struct audio_dev *agdev = func_to_agdev(fn);
1214 struct snd_uac2_chip *uac2 = &agdev->uac2;
Andrzej Pietrasiewiczf8f93d22014-07-22 19:58:30 +02001215 struct f_uac2_opts *opts;
Jassi Brar132fcb42012-02-02 22:01:34 +05301216 u16 w_length = le16_to_cpu(cr->wLength);
1217 u16 w_index = le16_to_cpu(cr->wIndex);
1218 u16 w_value = le16_to_cpu(cr->wValue);
1219 u8 entity_id = (w_index >> 8) & 0xff;
1220 u8 control_selector = w_value >> 8;
1221 struct cntrl_range_lay3 r;
1222 int value = -EOPNOTSUPP;
Andrzej Pietrasiewiczf8f93d22014-07-22 19:58:30 +02001223 int p_srate, c_srate;
1224
1225 opts = container_of(agdev->func.fi, struct f_uac2_opts, func_inst);
1226 p_srate = opts->p_srate;
1227 c_srate = opts->c_srate;
Jassi Brar132fcb42012-02-02 22:01:34 +05301228
1229 if (control_selector == UAC2_CS_CONTROL_SAM_FREQ) {
1230 if (entity_id == USB_IN_CLK_ID)
1231 r.dMIN = p_srate;
1232 else if (entity_id == USB_OUT_CLK_ID)
1233 r.dMIN = c_srate;
1234 else
1235 return -EOPNOTSUPP;
1236
1237 r.dMAX = r.dMIN;
1238 r.dRES = 0;
1239 r.wNumSubRanges = 1;
1240
1241 value = min_t(unsigned, w_length, sizeof r);
1242 memcpy(req->buf, &r, value);
1243 } else {
1244 dev_err(&uac2->pdev.dev,
1245 "%s:%d control_selector=%d TODO!\n",
1246 __func__, __LINE__, control_selector);
1247 }
1248
1249 return value;
1250}
1251
1252static int
1253ac_rq_in(struct usb_function *fn, const struct usb_ctrlrequest *cr)
1254{
1255 if (cr->bRequest == UAC2_CS_CUR)
1256 return in_rq_cur(fn, cr);
1257 else if (cr->bRequest == UAC2_CS_RANGE)
1258 return in_rq_range(fn, cr);
1259 else
1260 return -EOPNOTSUPP;
1261}
1262
1263static int
1264out_rq_cur(struct usb_function *fn, const struct usb_ctrlrequest *cr)
1265{
1266 u16 w_length = le16_to_cpu(cr->wLength);
1267 u16 w_value = le16_to_cpu(cr->wValue);
1268 u8 control_selector = w_value >> 8;
1269
1270 if (control_selector == UAC2_CS_CONTROL_SAM_FREQ)
1271 return w_length;
1272
1273 return -EOPNOTSUPP;
1274}
1275
1276static int
1277setup_rq_inf(struct usb_function *fn, const struct usb_ctrlrequest *cr)
1278{
1279 struct audio_dev *agdev = func_to_agdev(fn);
1280 struct snd_uac2_chip *uac2 = &agdev->uac2;
1281 u16 w_index = le16_to_cpu(cr->wIndex);
1282 u8 intf = w_index & 0xff;
1283
Sebastian Andrzej Siewior4ce63572012-10-22 22:15:08 +02001284 if (intf != agdev->ac_intf) {
Jassi Brar132fcb42012-02-02 22:01:34 +05301285 dev_err(&uac2->pdev.dev,
1286 "%s:%d Error!\n", __func__, __LINE__);
1287 return -EOPNOTSUPP;
1288 }
1289
1290 if (cr->bRequestType & USB_DIR_IN)
1291 return ac_rq_in(fn, cr);
1292 else if (cr->bRequest == UAC2_CS_CUR)
1293 return out_rq_cur(fn, cr);
1294
1295 return -EOPNOTSUPP;
1296}
1297
1298static int
1299afunc_setup(struct usb_function *fn, const struct usb_ctrlrequest *cr)
1300{
1301 struct usb_composite_dev *cdev = fn->config->cdev;
1302 struct audio_dev *agdev = func_to_agdev(fn);
1303 struct snd_uac2_chip *uac2 = &agdev->uac2;
1304 struct usb_request *req = cdev->req;
1305 u16 w_length = le16_to_cpu(cr->wLength);
1306 int value = -EOPNOTSUPP;
1307
1308 /* Only Class specific requests are supposed to reach here */
1309 if ((cr->bRequestType & USB_TYPE_MASK) != USB_TYPE_CLASS)
1310 return -EOPNOTSUPP;
1311
1312 if ((cr->bRequestType & USB_RECIP_MASK) == USB_RECIP_INTERFACE)
1313 value = setup_rq_inf(fn, cr);
1314 else
1315 dev_err(&uac2->pdev.dev, "%s:%d Error!\n", __func__, __LINE__);
1316
1317 if (value >= 0) {
1318 req->length = value;
1319 req->zero = value < w_length;
1320 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
1321 if (value < 0) {
1322 dev_err(&uac2->pdev.dev,
1323 "%s:%d Error!\n", __func__, __LINE__);
1324 req->status = 0;
1325 }
1326 }
1327
1328 return value;
1329}
1330
Andrzej Pietrasiewiczf8f93d22014-07-22 19:58:30 +02001331static void afunc_free_inst(struct usb_function_instance *f)
1332{
1333 struct f_uac2_opts *opts;
1334
1335 opts = container_of(f, struct f_uac2_opts, func_inst);
1336 kfree(opts);
1337}
1338
1339static struct usb_function_instance *afunc_alloc_inst(void)
1340{
1341 struct f_uac2_opts *opts;
1342
1343 opts = kzalloc(sizeof(*opts), GFP_KERNEL);
1344 if (!opts)
1345 return ERR_PTR(-ENOMEM);
1346
1347 opts->func_inst.free_func_inst = afunc_free_inst;
1348
1349 return &opts->func_inst;
1350}
1351
1352static void afunc_free(struct usb_function *f)
1353{
1354 struct audio_dev *agdev;
1355
1356 agdev = func_to_agdev(f);
1357 kfree(agdev);
1358}
1359
1360static void afunc_unbind(struct usb_configuration *c, struct usb_function *f)
1361{
1362 struct audio_dev *agdev = func_to_agdev(f);
1363 struct uac2_rtd_params *prm;
1364
1365 alsa_uac2_exit(agdev);
1366
1367 prm = &agdev->uac2.p_prm;
1368 kfree(prm->rbuf);
1369
1370 prm = &agdev->uac2.c_prm;
1371 kfree(prm->rbuf);
1372 usb_free_all_descriptors(f);
1373
1374 if (agdev->in_ep)
1375 agdev->in_ep->driver_data = NULL;
1376 if (agdev->out_ep)
1377 agdev->out_ep->driver_data = NULL;
1378}
1379
1380struct usb_function *afunc_alloc(struct usb_function_instance *fi)
1381{
1382 struct audio_dev *agdev;
1383 struct f_uac2_opts *opts;
1384
1385 agdev = kzalloc(sizeof(*agdev), GFP_KERNEL);
1386 if (agdev == NULL)
1387 return ERR_PTR(-ENOMEM);
1388
1389 opts = container_of(fi, struct f_uac2_opts, func_inst);
1390
1391 agdev->func.name = "uac2_func";
1392 agdev->func.strings = fn_strings;
1393 agdev->func.bind = afunc_bind;
1394 agdev->func.unbind = afunc_unbind;
1395 agdev->func.set_alt = afunc_set_alt;
1396 agdev->func.get_alt = afunc_get_alt;
1397 agdev->func.disable = afunc_disable;
1398 agdev->func.setup = afunc_setup;
1399 agdev->func.free_func = afunc_free;
1400
1401 return &agdev->func;
1402}
1403
1404DECLARE_USB_FUNCTION_INIT(uac2, afunc_alloc_inst, afunc_alloc);
1405MODULE_LICENSE("GPL");
1406MODULE_AUTHOR("Yadwinder Singh");
1407MODULE_AUTHOR("Jaswinder Singh");