usb: gadget: f_uac2: add short-hand for 'dev'
[linux-2.6-block.git] / drivers / usb / gadget / function / f_uac2.c
CommitLineData
132fcb46
JB
1/*
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
f8f93d24
AP
23#include "u_uac2.h"
24
132fcb46
JB
25/* 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 {
eb127cb5 65 struct snd_uac2_chip *uac2; /* parent chip */
132fcb46
JB
66 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 {
4ce63571
SAS
113 u8 ac_intf, ac_alt;
114 u8 as_out_intf, as_out_alt;
115 u8 as_in_intf, as_in_alt;
132fcb46
JB
116
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
132fcb46
JB
124static 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
132fcb46
JB
142static inline
143uint 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;
eb127cb5 166 struct snd_uac2_chip *uac2 = prm->uac2;
132fcb46
JB
167
168 /* i/f shutting down */
1ade5d7e 169 if (!prm->ep_enabled || req->status == -ESHUTDOWN)
132fcb46
JB
170 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);
132fcb46
JB
222 struct uac2_rtd_params *prm;
223 unsigned long flags;
132fcb46
JB
224 int err = 0;
225
b6424353 226 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
132fcb46 227 prm = &uac2->p_prm;
b6424353 228 else
132fcb46 229 prm = &uac2->c_prm;
132fcb46
JB
230
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;
f8f93d24
AP
315 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;
132fcb46
JB
329
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;
55f7840a
SR
335 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 }
132fcb46
JB
346 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;
55f7840a
SR
352 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 }
132fcb46
JB
363 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
41ac7b3a 393static int snd_uac2_probe(struct platform_device *pdev)
132fcb46
JB
394{
395 struct snd_uac2_chip *uac2 = pdev_to_uac2(pdev);
396 struct snd_card *card;
397 struct snd_pcm *pcm;
f8f93d24
AP
398 struct audio_dev *audio_dev;
399 struct f_uac2_opts *opts;
132fcb46 400 int err;
f8f93d24
AP
401 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;
132fcb46
JB
407
408 /* Choose any slot, with no id */
13345095 409 err = snd_card_new(&pdev->dev, -1, NULL, THIS_MODULE, 0, &card);
132fcb46
JB
410 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
132fcb46
JB
436 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
1e1a27c3 454static int snd_uac2_remove(struct platform_device *pdev)
132fcb46
JB
455{
456 struct snd_card *card = platform_get_drvdata(pdev);
457
132fcb46
JB
458 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
132fcb46
JB
515static char clksrc_in[8];
516static char clksrc_out[8];
132fcb46
JB
517
518static struct usb_string strings_fn[] = {
b36c3479
SAS
519 [STR_ASSOC].s = "Source/Sink",
520 [STR_IF_CTRL].s = "Topology Control",
132fcb46
JB
521 [STR_CLKSRC_IN].s = clksrc_in,
522 [STR_CLKSRC_OUT].s = clksrc_out,
b36c3479
SAS
523 [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",
132fcb46
JB
531 { },
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{
eb127cb5 896 struct snd_uac2_chip *uac2 = prm->uac2;
132fcb46
JB
897 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
f8f93d24 914static int
132fcb46
JB
915afunc_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;
a8147dab 921 struct device *dev = &uac2->pdev.dev;
132fcb46 922 struct uac2_rtd_params *prm;
f8f93d24 923 struct f_uac2_opts *uac2_opts;
f408757f 924 struct usb_string *us;
132fcb46
JB
925 int ret;
926
f8f93d24 927 uac2_opts = container_of(fn->fi, struct f_uac2_opts, func_inst);
f8f93d24 928
f408757f
AP
929 us = usb_gstrings_attach(cdev, fn_strings, ARRAY_SIZE(strings_fn));
930 if (IS_ERR(us))
931 return PTR_ERR(us);
932 iad_desc.iFunction = us[STR_ASSOC].id;
933 std_ac_if_desc.iInterface = us[STR_IF_CTRL].id;
934 in_clk_src_desc.iClockSource = us[STR_CLKSRC_IN].id;
935 out_clk_src_desc.iClockSource = us[STR_CLKSRC_OUT].id;
936 usb_out_it_desc.iTerminal = us[STR_USB_IT].id;
937 io_in_it_desc.iTerminal = us[STR_IO_IT].id;
938 usb_in_ot_desc.iTerminal = us[STR_USB_OT].id;
939 io_out_ot_desc.iTerminal = us[STR_IO_OT].id;
940 std_as_out_if0_desc.iInterface = us[STR_AS_OUT_ALT0].id;
941 std_as_out_if1_desc.iInterface = us[STR_AS_OUT_ALT1].id;
942 std_as_in_if0_desc.iInterface = us[STR_AS_IN_ALT0].id;
943 std_as_in_if1_desc.iInterface = us[STR_AS_IN_ALT1].id;
944
f8f93d24 945
f8f93d24
AP
946 /* Initialize the configurable parameters */
947 usb_out_it_desc.bNrChannels = num_channels(uac2_opts->c_chmask);
948 usb_out_it_desc.bmChannelConfig = cpu_to_le32(uac2_opts->c_chmask);
949 io_in_it_desc.bNrChannels = num_channels(uac2_opts->p_chmask);
950 io_in_it_desc.bmChannelConfig = cpu_to_le32(uac2_opts->p_chmask);
951 as_out_hdr_desc.bNrChannels = num_channels(uac2_opts->c_chmask);
952 as_out_hdr_desc.bmChannelConfig = cpu_to_le32(uac2_opts->c_chmask);
953 as_in_hdr_desc.bNrChannels = num_channels(uac2_opts->p_chmask);
954 as_in_hdr_desc.bmChannelConfig = cpu_to_le32(uac2_opts->p_chmask);
955 as_out_fmt1_desc.bSubslotSize = uac2_opts->c_ssize;
956 as_out_fmt1_desc.bBitResolution = uac2_opts->c_ssize * 8;
957 as_in_fmt1_desc.bSubslotSize = uac2_opts->p_ssize;
958 as_in_fmt1_desc.bBitResolution = uac2_opts->p_ssize * 8;
959
960 snprintf(clksrc_in, sizeof(clksrc_in), "%uHz", uac2_opts->p_srate);
961 snprintf(clksrc_out, sizeof(clksrc_out), "%uHz", uac2_opts->c_srate);
f8f93d24 962
132fcb46
JB
963 ret = usb_interface_id(cfg, fn);
964 if (ret < 0) {
a8147dab 965 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
132fcb46
JB
966 return ret;
967 }
968 std_ac_if_desc.bInterfaceNumber = ret;
4ce63571
SAS
969 agdev->ac_intf = ret;
970 agdev->ac_alt = 0;
132fcb46
JB
971
972 ret = usb_interface_id(cfg, fn);
973 if (ret < 0) {
a8147dab 974 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
132fcb46
JB
975 return ret;
976 }
977 std_as_out_if0_desc.bInterfaceNumber = ret;
978 std_as_out_if1_desc.bInterfaceNumber = ret;
4ce63571
SAS
979 agdev->as_out_intf = ret;
980 agdev->as_out_alt = 0;
132fcb46
JB
981
982 ret = usb_interface_id(cfg, fn);
983 if (ret < 0) {
a8147dab 984 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
132fcb46
JB
985 return ret;
986 }
987 std_as_in_if0_desc.bInterfaceNumber = ret;
988 std_as_in_if1_desc.bInterfaceNumber = ret;
4ce63571
SAS
989 agdev->as_in_intf = ret;
990 agdev->as_in_alt = 0;
132fcb46
JB
991
992 agdev->out_ep = usb_ep_autoconfig(gadget, &fs_epout_desc);
391aa852 993 if (!agdev->out_ep) {
a8147dab 994 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
391aa852
SAS
995 goto err;
996 }
132fcb46
JB
997 agdev->out_ep->driver_data = agdev;
998
999 agdev->in_ep = usb_ep_autoconfig(gadget, &fs_epin_desc);
391aa852 1000 if (!agdev->in_ep) {
a8147dab 1001 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
391aa852
SAS
1002 goto err;
1003 }
132fcb46
JB
1004 agdev->in_ep->driver_data = agdev;
1005
eb127cb5
JB
1006 uac2->p_prm.uac2 = uac2;
1007 uac2->c_prm.uac2 = uac2;
1008
132fcb46
JB
1009 hs_epout_desc.bEndpointAddress = fs_epout_desc.bEndpointAddress;
1010 hs_epout_desc.wMaxPacketSize = fs_epout_desc.wMaxPacketSize;
1011 hs_epin_desc.bEndpointAddress = fs_epin_desc.bEndpointAddress;
1012 hs_epin_desc.wMaxPacketSize = fs_epin_desc.wMaxPacketSize;
1013
10287bae
SAS
1014 ret = usb_assign_descriptors(fn, fs_audio_desc, hs_audio_desc, NULL);
1015 if (ret)
1016 goto err;
132fcb46
JB
1017
1018 prm = &agdev->uac2.c_prm;
1019 prm->max_psize = hs_epout_desc.wMaxPacketSize;
1020 prm->rbuf = kzalloc(prm->max_psize * USB_XFERS, GFP_KERNEL);
1021 if (!prm->rbuf) {
1022 prm->max_psize = 0;
391aa852 1023 goto err;
132fcb46
JB
1024 }
1025
1026 prm = &agdev->uac2.p_prm;
1027 prm->max_psize = hs_epin_desc.wMaxPacketSize;
1028 prm->rbuf = kzalloc(prm->max_psize * USB_XFERS, GFP_KERNEL);
1029 if (!prm->rbuf) {
1030 prm->max_psize = 0;
391aa852 1031 goto err;
132fcb46
JB
1032 }
1033
391aa852
SAS
1034 ret = alsa_uac2_init(agdev);
1035 if (ret)
1036 goto err;
1037 return 0;
1038err:
1039 kfree(agdev->uac2.p_prm.rbuf);
1040 kfree(agdev->uac2.c_prm.rbuf);
10287bae 1041 usb_free_all_descriptors(fn);
391aa852
SAS
1042 if (agdev->in_ep)
1043 agdev->in_ep->driver_data = NULL;
1044 if (agdev->out_ep)
1045 agdev->out_ep->driver_data = NULL;
1046 return -EINVAL;
132fcb46
JB
1047}
1048
132fcb46
JB
1049static int
1050afunc_set_alt(struct usb_function *fn, unsigned intf, unsigned alt)
1051{
1052 struct usb_composite_dev *cdev = fn->config->cdev;
1053 struct audio_dev *agdev = func_to_agdev(fn);
1054 struct snd_uac2_chip *uac2 = &agdev->uac2;
1055 struct usb_gadget *gadget = cdev->gadget;
a8147dab 1056 struct device *dev = &uac2->pdev.dev;
132fcb46
JB
1057 struct usb_request *req;
1058 struct usb_ep *ep;
1059 struct uac2_rtd_params *prm;
1060 int i;
1061
1062 /* No i/f has more than 2 alt settings */
1063 if (alt > 1) {
a8147dab 1064 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
132fcb46
JB
1065 return -EINVAL;
1066 }
1067
4ce63571 1068 if (intf == agdev->ac_intf) {
132fcb46
JB
1069 /* Control I/f has only 1 AltSetting - 0 */
1070 if (alt) {
a8147dab 1071 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
132fcb46
JB
1072 return -EINVAL;
1073 }
1074 return 0;
1075 }
1076
4ce63571 1077 if (intf == agdev->as_out_intf) {
132fcb46
JB
1078 ep = agdev->out_ep;
1079 prm = &uac2->c_prm;
1080 config_ep_by_speed(gadget, fn, ep);
4ce63571
SAS
1081 agdev->as_out_alt = alt;
1082 } else if (intf == agdev->as_in_intf) {
132fcb46
JB
1083 ep = agdev->in_ep;
1084 prm = &uac2->p_prm;
1085 config_ep_by_speed(gadget, fn, ep);
4ce63571 1086 agdev->as_in_alt = alt;
132fcb46 1087 } else {
a8147dab 1088 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
132fcb46
JB
1089 return -EINVAL;
1090 }
1091
1092 if (alt == 0) {
1093 free_ep(prm, ep);
1094 return 0;
1095 }
1096
1097 prm->ep_enabled = true;
1098 usb_ep_enable(ep);
1099
1100 for (i = 0; i < USB_XFERS; i++) {
430fdbd3
DM
1101 if (!prm->ureq[i].req) {
1102 req = usb_ep_alloc_request(ep, GFP_ATOMIC);
1103 if (req == NULL)
1104 return -ENOMEM;
1105
1106 prm->ureq[i].req = req;
1107 prm->ureq[i].pp = prm;
1108
1109 req->zero = 0;
1110 req->context = &prm->ureq[i];
1111 req->length = prm->max_psize;
1112 req->complete = agdev_iso_complete;
1113 req->buf = prm->rbuf + i * req->length;
132fcb46
JB
1114 }
1115
430fdbd3 1116 if (usb_ep_queue(ep, prm->ureq[i].req, GFP_ATOMIC))
a8147dab 1117 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
132fcb46
JB
1118 }
1119
1120 return 0;
1121}
1122
1123static int
1124afunc_get_alt(struct usb_function *fn, unsigned intf)
1125{
1126 struct audio_dev *agdev = func_to_agdev(fn);
1127 struct snd_uac2_chip *uac2 = &agdev->uac2;
1128
4ce63571
SAS
1129 if (intf == agdev->ac_intf)
1130 return agdev->ac_alt;
1131 else if (intf == agdev->as_out_intf)
1132 return agdev->as_out_alt;
1133 else if (intf == agdev->as_in_intf)
1134 return agdev->as_in_alt;
132fcb46
JB
1135 else
1136 dev_err(&uac2->pdev.dev,
1137 "%s:%d Invalid Interface %d!\n",
1138 __func__, __LINE__, intf);
1139
1140 return -EINVAL;
1141}
1142
1143static void
1144afunc_disable(struct usb_function *fn)
1145{
1146 struct audio_dev *agdev = func_to_agdev(fn);
1147 struct snd_uac2_chip *uac2 = &agdev->uac2;
1148
1149 free_ep(&uac2->p_prm, agdev->in_ep);
4ce63571 1150 agdev->as_in_alt = 0;
132fcb46
JB
1151
1152 free_ep(&uac2->c_prm, agdev->out_ep);
4ce63571 1153 agdev->as_out_alt = 0;
132fcb46
JB
1154}
1155
1156static int
1157in_rq_cur(struct usb_function *fn, const struct usb_ctrlrequest *cr)
1158{
1159 struct usb_request *req = fn->config->cdev->req;
1160 struct audio_dev *agdev = func_to_agdev(fn);
1161 struct snd_uac2_chip *uac2 = &agdev->uac2;
f8f93d24 1162 struct f_uac2_opts *opts;
132fcb46
JB
1163 u16 w_length = le16_to_cpu(cr->wLength);
1164 u16 w_index = le16_to_cpu(cr->wIndex);
1165 u16 w_value = le16_to_cpu(cr->wValue);
1166 u8 entity_id = (w_index >> 8) & 0xff;
1167 u8 control_selector = w_value >> 8;
1168 int value = -EOPNOTSUPP;
f8f93d24
AP
1169 int p_srate, c_srate;
1170
1171 opts = container_of(agdev->func.fi, struct f_uac2_opts, func_inst);
1172 p_srate = opts->p_srate;
1173 c_srate = opts->c_srate;
132fcb46
JB
1174
1175 if (control_selector == UAC2_CS_CONTROL_SAM_FREQ) {
1176 struct cntrl_cur_lay3 c;
1177
1178 if (entity_id == USB_IN_CLK_ID)
1179 c.dCUR = p_srate;
1180 else if (entity_id == USB_OUT_CLK_ID)
1181 c.dCUR = c_srate;
1182
1183 value = min_t(unsigned, w_length, sizeof c);
1184 memcpy(req->buf, &c, value);
1185 } else if (control_selector == UAC2_CS_CONTROL_CLOCK_VALID) {
1186 *(u8 *)req->buf = 1;
1187 value = min_t(unsigned, w_length, 1);
1188 } else {
1189 dev_err(&uac2->pdev.dev,
1190 "%s:%d control_selector=%d TODO!\n",
1191 __func__, __LINE__, control_selector);
1192 }
1193
1194 return value;
1195}
1196
1197static int
1198in_rq_range(struct usb_function *fn, const struct usb_ctrlrequest *cr)
1199{
1200 struct usb_request *req = fn->config->cdev->req;
1201 struct audio_dev *agdev = func_to_agdev(fn);
1202 struct snd_uac2_chip *uac2 = &agdev->uac2;
f8f93d24 1203 struct f_uac2_opts *opts;
132fcb46
JB
1204 u16 w_length = le16_to_cpu(cr->wLength);
1205 u16 w_index = le16_to_cpu(cr->wIndex);
1206 u16 w_value = le16_to_cpu(cr->wValue);
1207 u8 entity_id = (w_index >> 8) & 0xff;
1208 u8 control_selector = w_value >> 8;
1209 struct cntrl_range_lay3 r;
1210 int value = -EOPNOTSUPP;
f8f93d24
AP
1211 int p_srate, c_srate;
1212
1213 opts = container_of(agdev->func.fi, struct f_uac2_opts, func_inst);
1214 p_srate = opts->p_srate;
1215 c_srate = opts->c_srate;
132fcb46
JB
1216
1217 if (control_selector == UAC2_CS_CONTROL_SAM_FREQ) {
1218 if (entity_id == USB_IN_CLK_ID)
1219 r.dMIN = p_srate;
1220 else if (entity_id == USB_OUT_CLK_ID)
1221 r.dMIN = c_srate;
1222 else
1223 return -EOPNOTSUPP;
1224
1225 r.dMAX = r.dMIN;
1226 r.dRES = 0;
1227 r.wNumSubRanges = 1;
1228
1229 value = min_t(unsigned, w_length, sizeof r);
1230 memcpy(req->buf, &r, value);
1231 } else {
1232 dev_err(&uac2->pdev.dev,
1233 "%s:%d control_selector=%d TODO!\n",
1234 __func__, __LINE__, control_selector);
1235 }
1236
1237 return value;
1238}
1239
1240static int
1241ac_rq_in(struct usb_function *fn, const struct usb_ctrlrequest *cr)
1242{
1243 if (cr->bRequest == UAC2_CS_CUR)
1244 return in_rq_cur(fn, cr);
1245 else if (cr->bRequest == UAC2_CS_RANGE)
1246 return in_rq_range(fn, cr);
1247 else
1248 return -EOPNOTSUPP;
1249}
1250
1251static int
1252out_rq_cur(struct usb_function *fn, const struct usb_ctrlrequest *cr)
1253{
1254 u16 w_length = le16_to_cpu(cr->wLength);
1255 u16 w_value = le16_to_cpu(cr->wValue);
1256 u8 control_selector = w_value >> 8;
1257
1258 if (control_selector == UAC2_CS_CONTROL_SAM_FREQ)
1259 return w_length;
1260
1261 return -EOPNOTSUPP;
1262}
1263
1264static int
1265setup_rq_inf(struct usb_function *fn, const struct usb_ctrlrequest *cr)
1266{
1267 struct audio_dev *agdev = func_to_agdev(fn);
1268 struct snd_uac2_chip *uac2 = &agdev->uac2;
1269 u16 w_index = le16_to_cpu(cr->wIndex);
1270 u8 intf = w_index & 0xff;
1271
4ce63571 1272 if (intf != agdev->ac_intf) {
132fcb46
JB
1273 dev_err(&uac2->pdev.dev,
1274 "%s:%d Error!\n", __func__, __LINE__);
1275 return -EOPNOTSUPP;
1276 }
1277
1278 if (cr->bRequestType & USB_DIR_IN)
1279 return ac_rq_in(fn, cr);
1280 else if (cr->bRequest == UAC2_CS_CUR)
1281 return out_rq_cur(fn, cr);
1282
1283 return -EOPNOTSUPP;
1284}
1285
1286static int
1287afunc_setup(struct usb_function *fn, const struct usb_ctrlrequest *cr)
1288{
1289 struct usb_composite_dev *cdev = fn->config->cdev;
1290 struct audio_dev *agdev = func_to_agdev(fn);
1291 struct snd_uac2_chip *uac2 = &agdev->uac2;
1292 struct usb_request *req = cdev->req;
1293 u16 w_length = le16_to_cpu(cr->wLength);
1294 int value = -EOPNOTSUPP;
1295
1296 /* Only Class specific requests are supposed to reach here */
1297 if ((cr->bRequestType & USB_TYPE_MASK) != USB_TYPE_CLASS)
1298 return -EOPNOTSUPP;
1299
1300 if ((cr->bRequestType & USB_RECIP_MASK) == USB_RECIP_INTERFACE)
1301 value = setup_rq_inf(fn, cr);
1302 else
1303 dev_err(&uac2->pdev.dev, "%s:%d Error!\n", __func__, __LINE__);
1304
1305 if (value >= 0) {
1306 req->length = value;
1307 req->zero = value < w_length;
1308 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
1309 if (value < 0) {
1310 dev_err(&uac2->pdev.dev,
1311 "%s:%d Error!\n", __func__, __LINE__);
1312 req->status = 0;
1313 }
1314 }
1315
1316 return value;
1317}
1318
3aeea3c5
AP
1319static inline struct f_uac2_opts *to_f_uac2_opts(struct config_item *item)
1320{
1321 return container_of(to_config_group(item), struct f_uac2_opts,
1322 func_inst.group);
1323}
1324
1325CONFIGFS_ATTR_STRUCT(f_uac2_opts);
1326CONFIGFS_ATTR_OPS(f_uac2_opts);
1327
1328static void f_uac2_attr_release(struct config_item *item)
1329{
1330 struct f_uac2_opts *opts = to_f_uac2_opts(item);
1331
1332 usb_put_function_instance(&opts->func_inst);
1333}
1334
1335static struct configfs_item_operations f_uac2_item_ops = {
1336 .release = f_uac2_attr_release,
1337 .show_attribute = f_uac2_opts_attr_show,
1338 .store_attribute = f_uac2_opts_attr_store,
1339};
1340
1341#define UAC2_ATTRIBUTE(name) \
1342static ssize_t f_uac2_opts_##name##_show(struct f_uac2_opts *opts, \
1343 char *page) \
1344{ \
1345 int result; \
1346 \
1347 mutex_lock(&opts->lock); \
1348 result = sprintf(page, "%u\n", opts->name); \
1349 mutex_unlock(&opts->lock); \
1350 \
1351 return result; \
1352} \
1353 \
1354static ssize_t f_uac2_opts_##name##_store(struct f_uac2_opts *opts, \
1355 const char *page, size_t len) \
1356{ \
1357 int ret; \
1358 u32 num; \
1359 \
1360 mutex_lock(&opts->lock); \
1361 if (opts->refcnt) { \
1362 ret = -EBUSY; \
1363 goto end; \
1364 } \
1365 \
1366 ret = kstrtou32(page, 0, &num); \
1367 if (ret) \
1368 goto end; \
1369 \
1370 opts->name = num; \
1371 ret = len; \
1372 \
1373end: \
1374 mutex_unlock(&opts->lock); \
1375 return ret; \
1376} \
1377 \
1378static struct f_uac2_opts_attribute f_uac2_opts_##name = \
1379 __CONFIGFS_ATTR(name, S_IRUGO | S_IWUSR, \
1380 f_uac2_opts_##name##_show, \
1381 f_uac2_opts_##name##_store)
1382
1383UAC2_ATTRIBUTE(p_chmask);
1384UAC2_ATTRIBUTE(p_srate);
1385UAC2_ATTRIBUTE(p_ssize);
1386UAC2_ATTRIBUTE(c_chmask);
1387UAC2_ATTRIBUTE(c_srate);
1388UAC2_ATTRIBUTE(c_ssize);
1389
1390static struct configfs_attribute *f_uac2_attrs[] = {
1391 &f_uac2_opts_p_chmask.attr,
1392 &f_uac2_opts_p_srate.attr,
1393 &f_uac2_opts_p_ssize.attr,
1394 &f_uac2_opts_c_chmask.attr,
1395 &f_uac2_opts_c_srate.attr,
1396 &f_uac2_opts_c_ssize.attr,
1397 NULL,
1398};
1399
1400static struct config_item_type f_uac2_func_type = {
1401 .ct_item_ops = &f_uac2_item_ops,
1402 .ct_attrs = f_uac2_attrs,
1403 .ct_owner = THIS_MODULE,
1404};
1405
f8f93d24
AP
1406static void afunc_free_inst(struct usb_function_instance *f)
1407{
1408 struct f_uac2_opts *opts;
1409
1410 opts = container_of(f, struct f_uac2_opts, func_inst);
1411 kfree(opts);
1412}
1413
1414static struct usb_function_instance *afunc_alloc_inst(void)
1415{
1416 struct f_uac2_opts *opts;
1417
1418 opts = kzalloc(sizeof(*opts), GFP_KERNEL);
1419 if (!opts)
1420 return ERR_PTR(-ENOMEM);
1421
3aeea3c5 1422 mutex_init(&opts->lock);
f8f93d24
AP
1423 opts->func_inst.free_func_inst = afunc_free_inst;
1424
3aeea3c5
AP
1425 config_group_init_type_name(&opts->func_inst.group, "",
1426 &f_uac2_func_type);
1427
1428 opts->p_chmask = UAC2_DEF_PCHMASK;
1429 opts->p_srate = UAC2_DEF_PSRATE;
1430 opts->p_ssize = UAC2_DEF_PSSIZE;
1431 opts->c_chmask = UAC2_DEF_CCHMASK;
1432 opts->c_srate = UAC2_DEF_CSRATE;
1433 opts->c_ssize = UAC2_DEF_CSSIZE;
f8f93d24
AP
1434 return &opts->func_inst;
1435}
1436
1437static void afunc_free(struct usb_function *f)
1438{
1439 struct audio_dev *agdev;
3aeea3c5 1440 struct f_uac2_opts *opts;
f8f93d24
AP
1441
1442 agdev = func_to_agdev(f);
3aeea3c5 1443 opts = container_of(f->fi, struct f_uac2_opts, func_inst);
f8f93d24 1444 kfree(agdev);
3aeea3c5
AP
1445 mutex_lock(&opts->lock);
1446 --opts->refcnt;
1447 mutex_unlock(&opts->lock);
f8f93d24
AP
1448}
1449
1450static void afunc_unbind(struct usb_configuration *c, struct usb_function *f)
1451{
1452 struct audio_dev *agdev = func_to_agdev(f);
1453 struct uac2_rtd_params *prm;
1454
1455 alsa_uac2_exit(agdev);
1456
1457 prm = &agdev->uac2.p_prm;
1458 kfree(prm->rbuf);
1459
1460 prm = &agdev->uac2.c_prm;
1461 kfree(prm->rbuf);
1462 usb_free_all_descriptors(f);
1463
1464 if (agdev->in_ep)
1465 agdev->in_ep->driver_data = NULL;
1466 if (agdev->out_ep)
1467 agdev->out_ep->driver_data = NULL;
1468}
1469
1470struct usb_function *afunc_alloc(struct usb_function_instance *fi)
1471{
1472 struct audio_dev *agdev;
1473 struct f_uac2_opts *opts;
1474
1475 agdev = kzalloc(sizeof(*agdev), GFP_KERNEL);
1476 if (agdev == NULL)
1477 return ERR_PTR(-ENOMEM);
1478
1479 opts = container_of(fi, struct f_uac2_opts, func_inst);
3aeea3c5
AP
1480 mutex_lock(&opts->lock);
1481 ++opts->refcnt;
1482 mutex_unlock(&opts->lock);
f8f93d24
AP
1483
1484 agdev->func.name = "uac2_func";
f8f93d24
AP
1485 agdev->func.bind = afunc_bind;
1486 agdev->func.unbind = afunc_unbind;
1487 agdev->func.set_alt = afunc_set_alt;
1488 agdev->func.get_alt = afunc_get_alt;
1489 agdev->func.disable = afunc_disable;
1490 agdev->func.setup = afunc_setup;
1491 agdev->func.free_func = afunc_free;
1492
1493 return &agdev->func;
1494}
1495
1496DECLARE_USB_FUNCTION_INIT(uac2, afunc_alloc_inst, afunc_alloc);
1497MODULE_LICENSE("GPL");
1498MODULE_AUTHOR("Yadwinder Singh");
1499MODULE_AUTHOR("Jaswinder Singh");