blob: 1b9671effa122c03f96939116b9feefaec981006 [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;
Andrzej Pietrasiewiczf4087572014-07-22 19:58:33 +0200923 struct usb_string *us;
Jassi Brar132fcb42012-02-02 22:01:34 +0530924 int ret;
925
Andrzej Pietrasiewiczf8f93d22014-07-22 19:58:30 +0200926 uac2_opts = container_of(fn->fi, struct f_uac2_opts, func_inst);
Andrzej Pietrasiewiczf8f93d22014-07-22 19:58:30 +0200927
Andrzej Pietrasiewiczf4087572014-07-22 19:58:33 +0200928 us = usb_gstrings_attach(cdev, fn_strings, ARRAY_SIZE(strings_fn));
929 if (IS_ERR(us))
930 return PTR_ERR(us);
931 iad_desc.iFunction = us[STR_ASSOC].id;
932 std_ac_if_desc.iInterface = us[STR_IF_CTRL].id;
933 in_clk_src_desc.iClockSource = us[STR_CLKSRC_IN].id;
934 out_clk_src_desc.iClockSource = us[STR_CLKSRC_OUT].id;
935 usb_out_it_desc.iTerminal = us[STR_USB_IT].id;
936 io_in_it_desc.iTerminal = us[STR_IO_IT].id;
937 usb_in_ot_desc.iTerminal = us[STR_USB_OT].id;
938 io_out_ot_desc.iTerminal = us[STR_IO_OT].id;
939 std_as_out_if0_desc.iInterface = us[STR_AS_OUT_ALT0].id;
940 std_as_out_if1_desc.iInterface = us[STR_AS_OUT_ALT1].id;
941 std_as_in_if0_desc.iInterface = us[STR_AS_IN_ALT0].id;
942 std_as_in_if1_desc.iInterface = us[STR_AS_IN_ALT1].id;
943
Andrzej Pietrasiewiczf8f93d22014-07-22 19:58:30 +0200944
Andrzej Pietrasiewiczf8f93d22014-07-22 19:58:30 +0200945 /* Initialize the configurable parameters */
946 usb_out_it_desc.bNrChannels = num_channels(uac2_opts->c_chmask);
947 usb_out_it_desc.bmChannelConfig = cpu_to_le32(uac2_opts->c_chmask);
948 io_in_it_desc.bNrChannels = num_channels(uac2_opts->p_chmask);
949 io_in_it_desc.bmChannelConfig = cpu_to_le32(uac2_opts->p_chmask);
950 as_out_hdr_desc.bNrChannels = num_channels(uac2_opts->c_chmask);
951 as_out_hdr_desc.bmChannelConfig = cpu_to_le32(uac2_opts->c_chmask);
952 as_in_hdr_desc.bNrChannels = num_channels(uac2_opts->p_chmask);
953 as_in_hdr_desc.bmChannelConfig = cpu_to_le32(uac2_opts->p_chmask);
954 as_out_fmt1_desc.bSubslotSize = uac2_opts->c_ssize;
955 as_out_fmt1_desc.bBitResolution = uac2_opts->c_ssize * 8;
956 as_in_fmt1_desc.bSubslotSize = uac2_opts->p_ssize;
957 as_in_fmt1_desc.bBitResolution = uac2_opts->p_ssize * 8;
958
959 snprintf(clksrc_in, sizeof(clksrc_in), "%uHz", uac2_opts->p_srate);
960 snprintf(clksrc_out, sizeof(clksrc_out), "%uHz", uac2_opts->c_srate);
Andrzej Pietrasiewiczf8f93d22014-07-22 19:58:30 +0200961
Jassi Brar132fcb42012-02-02 22:01:34 +0530962 ret = usb_interface_id(cfg, fn);
963 if (ret < 0) {
964 dev_err(&uac2->pdev.dev,
965 "%s:%d Error!\n", __func__, __LINE__);
966 return ret;
967 }
968 std_ac_if_desc.bInterfaceNumber = ret;
Sebastian Andrzej Siewior4ce63572012-10-22 22:15:08 +0200969 agdev->ac_intf = ret;
970 agdev->ac_alt = 0;
Jassi Brar132fcb42012-02-02 22:01:34 +0530971
972 ret = usb_interface_id(cfg, fn);
973 if (ret < 0) {
974 dev_err(&uac2->pdev.dev,
975 "%s:%d Error!\n", __func__, __LINE__);
976 return ret;
977 }
978 std_as_out_if0_desc.bInterfaceNumber = ret;
979 std_as_out_if1_desc.bInterfaceNumber = ret;
Sebastian Andrzej Siewior4ce63572012-10-22 22:15:08 +0200980 agdev->as_out_intf = ret;
981 agdev->as_out_alt = 0;
Jassi Brar132fcb42012-02-02 22:01:34 +0530982
983 ret = usb_interface_id(cfg, fn);
984 if (ret < 0) {
985 dev_err(&uac2->pdev.dev,
986 "%s:%d Error!\n", __func__, __LINE__);
987 return ret;
988 }
989 std_as_in_if0_desc.bInterfaceNumber = ret;
990 std_as_in_if1_desc.bInterfaceNumber = ret;
Sebastian Andrzej Siewior4ce63572012-10-22 22:15:08 +0200991 agdev->as_in_intf = ret;
992 agdev->as_in_alt = 0;
Jassi Brar132fcb42012-02-02 22:01:34 +0530993
994 agdev->out_ep = usb_ep_autoconfig(gadget, &fs_epout_desc);
Sebastian Andrzej Siewior391aa852012-10-22 22:14:59 +0200995 if (!agdev->out_ep) {
Jassi Brar132fcb42012-02-02 22:01:34 +0530996 dev_err(&uac2->pdev.dev,
997 "%s:%d Error!\n", __func__, __LINE__);
Sebastian Andrzej Siewior391aa852012-10-22 22:14:59 +0200998 goto err;
999 }
Jassi Brar132fcb42012-02-02 22:01:34 +05301000 agdev->out_ep->driver_data = agdev;
1001
1002 agdev->in_ep = usb_ep_autoconfig(gadget, &fs_epin_desc);
Sebastian Andrzej Siewior391aa852012-10-22 22:14:59 +02001003 if (!agdev->in_ep) {
Jassi Brar132fcb42012-02-02 22:01:34 +05301004 dev_err(&uac2->pdev.dev,
1005 "%s:%d Error!\n", __func__, __LINE__);
Sebastian Andrzej Siewior391aa852012-10-22 22:14:59 +02001006 goto err;
1007 }
Jassi Brar132fcb42012-02-02 22:01:34 +05301008 agdev->in_ep->driver_data = agdev;
1009
Jassi Brareb127cb2013-05-30 18:23:33 +05301010 uac2->p_prm.uac2 = uac2;
1011 uac2->c_prm.uac2 = uac2;
1012
Jassi Brar132fcb42012-02-02 22:01:34 +05301013 hs_epout_desc.bEndpointAddress = fs_epout_desc.bEndpointAddress;
1014 hs_epout_desc.wMaxPacketSize = fs_epout_desc.wMaxPacketSize;
1015 hs_epin_desc.bEndpointAddress = fs_epin_desc.bEndpointAddress;
1016 hs_epin_desc.wMaxPacketSize = fs_epin_desc.wMaxPacketSize;
1017
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +02001018 ret = usb_assign_descriptors(fn, fs_audio_desc, hs_audio_desc, NULL);
1019 if (ret)
1020 goto err;
Jassi Brar132fcb42012-02-02 22:01:34 +05301021
1022 prm = &agdev->uac2.c_prm;
1023 prm->max_psize = hs_epout_desc.wMaxPacketSize;
1024 prm->rbuf = kzalloc(prm->max_psize * USB_XFERS, GFP_KERNEL);
1025 if (!prm->rbuf) {
1026 prm->max_psize = 0;
Sebastian Andrzej Siewior391aa852012-10-22 22:14:59 +02001027 goto err;
Jassi Brar132fcb42012-02-02 22:01:34 +05301028 }
1029
1030 prm = &agdev->uac2.p_prm;
1031 prm->max_psize = hs_epin_desc.wMaxPacketSize;
1032 prm->rbuf = kzalloc(prm->max_psize * USB_XFERS, GFP_KERNEL);
1033 if (!prm->rbuf) {
1034 prm->max_psize = 0;
Sebastian Andrzej Siewior391aa852012-10-22 22:14:59 +02001035 goto err;
Jassi Brar132fcb42012-02-02 22:01:34 +05301036 }
1037
Sebastian Andrzej Siewior391aa852012-10-22 22:14:59 +02001038 ret = alsa_uac2_init(agdev);
1039 if (ret)
1040 goto err;
1041 return 0;
1042err:
1043 kfree(agdev->uac2.p_prm.rbuf);
1044 kfree(agdev->uac2.c_prm.rbuf);
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +02001045 usb_free_all_descriptors(fn);
Sebastian Andrzej Siewior391aa852012-10-22 22:14:59 +02001046 if (agdev->in_ep)
1047 agdev->in_ep->driver_data = NULL;
1048 if (agdev->out_ep)
1049 agdev->out_ep->driver_data = NULL;
1050 return -EINVAL;
Jassi Brar132fcb42012-02-02 22:01:34 +05301051}
1052
Jassi Brar132fcb42012-02-02 22:01:34 +05301053static int
1054afunc_set_alt(struct usb_function *fn, unsigned intf, unsigned alt)
1055{
1056 struct usb_composite_dev *cdev = fn->config->cdev;
1057 struct audio_dev *agdev = func_to_agdev(fn);
1058 struct snd_uac2_chip *uac2 = &agdev->uac2;
1059 struct usb_gadget *gadget = cdev->gadget;
1060 struct usb_request *req;
1061 struct usb_ep *ep;
1062 struct uac2_rtd_params *prm;
1063 int i;
1064
1065 /* No i/f has more than 2 alt settings */
1066 if (alt > 1) {
1067 dev_err(&uac2->pdev.dev,
1068 "%s:%d Error!\n", __func__, __LINE__);
1069 return -EINVAL;
1070 }
1071
Sebastian Andrzej Siewior4ce63572012-10-22 22:15:08 +02001072 if (intf == agdev->ac_intf) {
Jassi Brar132fcb42012-02-02 22:01:34 +05301073 /* Control I/f has only 1 AltSetting - 0 */
1074 if (alt) {
1075 dev_err(&uac2->pdev.dev,
1076 "%s:%d Error!\n", __func__, __LINE__);
1077 return -EINVAL;
1078 }
1079 return 0;
1080 }
1081
Sebastian Andrzej Siewior4ce63572012-10-22 22:15:08 +02001082 if (intf == agdev->as_out_intf) {
Jassi Brar132fcb42012-02-02 22:01:34 +05301083 ep = agdev->out_ep;
1084 prm = &uac2->c_prm;
1085 config_ep_by_speed(gadget, fn, ep);
Sebastian Andrzej Siewior4ce63572012-10-22 22:15:08 +02001086 agdev->as_out_alt = alt;
1087 } else if (intf == agdev->as_in_intf) {
Jassi Brar132fcb42012-02-02 22:01:34 +05301088 ep = agdev->in_ep;
1089 prm = &uac2->p_prm;
1090 config_ep_by_speed(gadget, fn, ep);
Sebastian Andrzej Siewior4ce63572012-10-22 22:15:08 +02001091 agdev->as_in_alt = alt;
Jassi Brar132fcb42012-02-02 22:01:34 +05301092 } else {
1093 dev_err(&uac2->pdev.dev,
1094 "%s:%d Error!\n", __func__, __LINE__);
1095 return -EINVAL;
1096 }
1097
1098 if (alt == 0) {
1099 free_ep(prm, ep);
1100 return 0;
1101 }
1102
1103 prm->ep_enabled = true;
1104 usb_ep_enable(ep);
1105
1106 for (i = 0; i < USB_XFERS; i++) {
1107 if (prm->ureq[i].req) {
1108 if (usb_ep_queue(ep, prm->ureq[i].req, GFP_ATOMIC))
1109 dev_err(&uac2->pdev.dev, "%d Error!\n",
1110 __LINE__);
1111 continue;
1112 }
1113
1114 req = usb_ep_alloc_request(ep, GFP_ATOMIC);
1115 if (req == NULL) {
1116 dev_err(&uac2->pdev.dev,
1117 "%s:%d Error!\n", __func__, __LINE__);
1118 return -EINVAL;
1119 }
1120
1121 prm->ureq[i].req = req;
1122 prm->ureq[i].pp = prm;
1123
1124 req->zero = 0;
Jassi Brar132fcb42012-02-02 22:01:34 +05301125 req->context = &prm->ureq[i];
1126 req->length = prm->max_psize;
1127 req->complete = agdev_iso_complete;
1128 req->buf = prm->rbuf + i * req->length;
1129
1130 if (usb_ep_queue(ep, req, GFP_ATOMIC))
1131 dev_err(&uac2->pdev.dev, "%d Error!\n", __LINE__);
1132 }
1133
1134 return 0;
1135}
1136
1137static int
1138afunc_get_alt(struct usb_function *fn, unsigned intf)
1139{
1140 struct audio_dev *agdev = func_to_agdev(fn);
1141 struct snd_uac2_chip *uac2 = &agdev->uac2;
1142
Sebastian Andrzej Siewior4ce63572012-10-22 22:15:08 +02001143 if (intf == agdev->ac_intf)
1144 return agdev->ac_alt;
1145 else if (intf == agdev->as_out_intf)
1146 return agdev->as_out_alt;
1147 else if (intf == agdev->as_in_intf)
1148 return agdev->as_in_alt;
Jassi Brar132fcb42012-02-02 22:01:34 +05301149 else
1150 dev_err(&uac2->pdev.dev,
1151 "%s:%d Invalid Interface %d!\n",
1152 __func__, __LINE__, intf);
1153
1154 return -EINVAL;
1155}
1156
1157static void
1158afunc_disable(struct usb_function *fn)
1159{
1160 struct audio_dev *agdev = func_to_agdev(fn);
1161 struct snd_uac2_chip *uac2 = &agdev->uac2;
1162
1163 free_ep(&uac2->p_prm, agdev->in_ep);
Sebastian Andrzej Siewior4ce63572012-10-22 22:15:08 +02001164 agdev->as_in_alt = 0;
Jassi Brar132fcb42012-02-02 22:01:34 +05301165
1166 free_ep(&uac2->c_prm, agdev->out_ep);
Sebastian Andrzej Siewior4ce63572012-10-22 22:15:08 +02001167 agdev->as_out_alt = 0;
Jassi Brar132fcb42012-02-02 22:01:34 +05301168}
1169
1170static int
1171in_rq_cur(struct usb_function *fn, const struct usb_ctrlrequest *cr)
1172{
1173 struct usb_request *req = fn->config->cdev->req;
1174 struct audio_dev *agdev = func_to_agdev(fn);
1175 struct snd_uac2_chip *uac2 = &agdev->uac2;
Andrzej Pietrasiewiczf8f93d22014-07-22 19:58:30 +02001176 struct f_uac2_opts *opts;
Jassi Brar132fcb42012-02-02 22:01:34 +05301177 u16 w_length = le16_to_cpu(cr->wLength);
1178 u16 w_index = le16_to_cpu(cr->wIndex);
1179 u16 w_value = le16_to_cpu(cr->wValue);
1180 u8 entity_id = (w_index >> 8) & 0xff;
1181 u8 control_selector = w_value >> 8;
1182 int value = -EOPNOTSUPP;
Andrzej Pietrasiewiczf8f93d22014-07-22 19:58:30 +02001183 int p_srate, c_srate;
1184
1185 opts = container_of(agdev->func.fi, struct f_uac2_opts, func_inst);
1186 p_srate = opts->p_srate;
1187 c_srate = opts->c_srate;
Jassi Brar132fcb42012-02-02 22:01:34 +05301188
1189 if (control_selector == UAC2_CS_CONTROL_SAM_FREQ) {
1190 struct cntrl_cur_lay3 c;
1191
1192 if (entity_id == USB_IN_CLK_ID)
1193 c.dCUR = p_srate;
1194 else if (entity_id == USB_OUT_CLK_ID)
1195 c.dCUR = c_srate;
1196
1197 value = min_t(unsigned, w_length, sizeof c);
1198 memcpy(req->buf, &c, value);
1199 } else if (control_selector == UAC2_CS_CONTROL_CLOCK_VALID) {
1200 *(u8 *)req->buf = 1;
1201 value = min_t(unsigned, w_length, 1);
1202 } else {
1203 dev_err(&uac2->pdev.dev,
1204 "%s:%d control_selector=%d TODO!\n",
1205 __func__, __LINE__, control_selector);
1206 }
1207
1208 return value;
1209}
1210
1211static int
1212in_rq_range(struct usb_function *fn, const struct usb_ctrlrequest *cr)
1213{
1214 struct usb_request *req = fn->config->cdev->req;
1215 struct audio_dev *agdev = func_to_agdev(fn);
1216 struct snd_uac2_chip *uac2 = &agdev->uac2;
Andrzej Pietrasiewiczf8f93d22014-07-22 19:58:30 +02001217 struct f_uac2_opts *opts;
Jassi Brar132fcb42012-02-02 22:01:34 +05301218 u16 w_length = le16_to_cpu(cr->wLength);
1219 u16 w_index = le16_to_cpu(cr->wIndex);
1220 u16 w_value = le16_to_cpu(cr->wValue);
1221 u8 entity_id = (w_index >> 8) & 0xff;
1222 u8 control_selector = w_value >> 8;
1223 struct cntrl_range_lay3 r;
1224 int value = -EOPNOTSUPP;
Andrzej Pietrasiewiczf8f93d22014-07-22 19:58:30 +02001225 int p_srate, c_srate;
1226
1227 opts = container_of(agdev->func.fi, struct f_uac2_opts, func_inst);
1228 p_srate = opts->p_srate;
1229 c_srate = opts->c_srate;
Jassi Brar132fcb42012-02-02 22:01:34 +05301230
1231 if (control_selector == UAC2_CS_CONTROL_SAM_FREQ) {
1232 if (entity_id == USB_IN_CLK_ID)
1233 r.dMIN = p_srate;
1234 else if (entity_id == USB_OUT_CLK_ID)
1235 r.dMIN = c_srate;
1236 else
1237 return -EOPNOTSUPP;
1238
1239 r.dMAX = r.dMIN;
1240 r.dRES = 0;
1241 r.wNumSubRanges = 1;
1242
1243 value = min_t(unsigned, w_length, sizeof r);
1244 memcpy(req->buf, &r, value);
1245 } else {
1246 dev_err(&uac2->pdev.dev,
1247 "%s:%d control_selector=%d TODO!\n",
1248 __func__, __LINE__, control_selector);
1249 }
1250
1251 return value;
1252}
1253
1254static int
1255ac_rq_in(struct usb_function *fn, const struct usb_ctrlrequest *cr)
1256{
1257 if (cr->bRequest == UAC2_CS_CUR)
1258 return in_rq_cur(fn, cr);
1259 else if (cr->bRequest == UAC2_CS_RANGE)
1260 return in_rq_range(fn, cr);
1261 else
1262 return -EOPNOTSUPP;
1263}
1264
1265static int
1266out_rq_cur(struct usb_function *fn, const struct usb_ctrlrequest *cr)
1267{
1268 u16 w_length = le16_to_cpu(cr->wLength);
1269 u16 w_value = le16_to_cpu(cr->wValue);
1270 u8 control_selector = w_value >> 8;
1271
1272 if (control_selector == UAC2_CS_CONTROL_SAM_FREQ)
1273 return w_length;
1274
1275 return -EOPNOTSUPP;
1276}
1277
1278static int
1279setup_rq_inf(struct usb_function *fn, const struct usb_ctrlrequest *cr)
1280{
1281 struct audio_dev *agdev = func_to_agdev(fn);
1282 struct snd_uac2_chip *uac2 = &agdev->uac2;
1283 u16 w_index = le16_to_cpu(cr->wIndex);
1284 u8 intf = w_index & 0xff;
1285
Sebastian Andrzej Siewior4ce63572012-10-22 22:15:08 +02001286 if (intf != agdev->ac_intf) {
Jassi Brar132fcb42012-02-02 22:01:34 +05301287 dev_err(&uac2->pdev.dev,
1288 "%s:%d Error!\n", __func__, __LINE__);
1289 return -EOPNOTSUPP;
1290 }
1291
1292 if (cr->bRequestType & USB_DIR_IN)
1293 return ac_rq_in(fn, cr);
1294 else if (cr->bRequest == UAC2_CS_CUR)
1295 return out_rq_cur(fn, cr);
1296
1297 return -EOPNOTSUPP;
1298}
1299
1300static int
1301afunc_setup(struct usb_function *fn, const struct usb_ctrlrequest *cr)
1302{
1303 struct usb_composite_dev *cdev = fn->config->cdev;
1304 struct audio_dev *agdev = func_to_agdev(fn);
1305 struct snd_uac2_chip *uac2 = &agdev->uac2;
1306 struct usb_request *req = cdev->req;
1307 u16 w_length = le16_to_cpu(cr->wLength);
1308 int value = -EOPNOTSUPP;
1309
1310 /* Only Class specific requests are supposed to reach here */
1311 if ((cr->bRequestType & USB_TYPE_MASK) != USB_TYPE_CLASS)
1312 return -EOPNOTSUPP;
1313
1314 if ((cr->bRequestType & USB_RECIP_MASK) == USB_RECIP_INTERFACE)
1315 value = setup_rq_inf(fn, cr);
1316 else
1317 dev_err(&uac2->pdev.dev, "%s:%d Error!\n", __func__, __LINE__);
1318
1319 if (value >= 0) {
1320 req->length = value;
1321 req->zero = value < w_length;
1322 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
1323 if (value < 0) {
1324 dev_err(&uac2->pdev.dev,
1325 "%s:%d Error!\n", __func__, __LINE__);
1326 req->status = 0;
1327 }
1328 }
1329
1330 return value;
1331}
1332
Andrzej Pietrasiewiczf8f93d22014-07-22 19:58:30 +02001333static void afunc_free_inst(struct usb_function_instance *f)
1334{
1335 struct f_uac2_opts *opts;
1336
1337 opts = container_of(f, struct f_uac2_opts, func_inst);
1338 kfree(opts);
1339}
1340
1341static struct usb_function_instance *afunc_alloc_inst(void)
1342{
1343 struct f_uac2_opts *opts;
1344
1345 opts = kzalloc(sizeof(*opts), GFP_KERNEL);
1346 if (!opts)
1347 return ERR_PTR(-ENOMEM);
1348
1349 opts->func_inst.free_func_inst = afunc_free_inst;
1350
1351 return &opts->func_inst;
1352}
1353
1354static void afunc_free(struct usb_function *f)
1355{
1356 struct audio_dev *agdev;
1357
1358 agdev = func_to_agdev(f);
1359 kfree(agdev);
1360}
1361
1362static void afunc_unbind(struct usb_configuration *c, struct usb_function *f)
1363{
1364 struct audio_dev *agdev = func_to_agdev(f);
1365 struct uac2_rtd_params *prm;
1366
1367 alsa_uac2_exit(agdev);
1368
1369 prm = &agdev->uac2.p_prm;
1370 kfree(prm->rbuf);
1371
1372 prm = &agdev->uac2.c_prm;
1373 kfree(prm->rbuf);
1374 usb_free_all_descriptors(f);
1375
1376 if (agdev->in_ep)
1377 agdev->in_ep->driver_data = NULL;
1378 if (agdev->out_ep)
1379 agdev->out_ep->driver_data = NULL;
1380}
1381
1382struct usb_function *afunc_alloc(struct usb_function_instance *fi)
1383{
1384 struct audio_dev *agdev;
1385 struct f_uac2_opts *opts;
1386
1387 agdev = kzalloc(sizeof(*agdev), GFP_KERNEL);
1388 if (agdev == NULL)
1389 return ERR_PTR(-ENOMEM);
1390
1391 opts = container_of(fi, struct f_uac2_opts, func_inst);
1392
1393 agdev->func.name = "uac2_func";
Andrzej Pietrasiewiczf8f93d22014-07-22 19:58:30 +02001394 agdev->func.bind = afunc_bind;
1395 agdev->func.unbind = afunc_unbind;
1396 agdev->func.set_alt = afunc_set_alt;
1397 agdev->func.get_alt = afunc_get_alt;
1398 agdev->func.disable = afunc_disable;
1399 agdev->func.setup = afunc_setup;
1400 agdev->func.free_func = afunc_free;
1401
1402 return &agdev->func;
1403}
1404
1405DECLARE_USB_FUNCTION_INIT(uac2, afunc_alloc_inst, afunc_alloc);
1406MODULE_LICENSE("GPL");
1407MODULE_AUTHOR("Yadwinder Singh");
1408MODULE_AUTHOR("Jaswinder Singh");