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