Commit | Line | Data |
---|---|---|
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 | ||
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 | ||
132fcb46 JB |
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 { | |
4ce63571 SAS |
140 | u8 ac_intf, ac_alt; |
141 | u8 as_out_intf, as_out_alt; | |
142 | u8 as_in_intf, as_in_alt; | |
132fcb46 JB |
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 audio_dev *agdev = uac2_to_agdev(uac2); | |
264 | struct uac2_rtd_params *prm; | |
265 | unsigned long flags; | |
266 | struct usb_ep *ep; | |
267 | int err = 0; | |
268 | ||
269 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { | |
270 | ep = agdev->in_ep; | |
271 | prm = &uac2->p_prm; | |
272 | } else { | |
273 | ep = agdev->out_ep; | |
274 | prm = &uac2->c_prm; | |
275 | } | |
276 | ||
277 | spin_lock_irqsave(&prm->lock, flags); | |
278 | ||
279 | /* Reset */ | |
280 | prm->hw_ptr = 0; | |
281 | ||
282 | switch (cmd) { | |
283 | case SNDRV_PCM_TRIGGER_START: | |
284 | case SNDRV_PCM_TRIGGER_RESUME: | |
285 | prm->ss = substream; | |
286 | break; | |
287 | case SNDRV_PCM_TRIGGER_STOP: | |
288 | case SNDRV_PCM_TRIGGER_SUSPEND: | |
289 | prm->ss = NULL; | |
290 | break; | |
291 | default: | |
292 | err = -EINVAL; | |
293 | } | |
294 | ||
295 | spin_unlock_irqrestore(&prm->lock, flags); | |
296 | ||
297 | /* Clear buffer after Play stops */ | |
298 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK && !prm->ss) | |
299 | memset(prm->rbuf, 0, prm->max_psize * USB_XFERS); | |
300 | ||
301 | return err; | |
302 | } | |
303 | ||
304 | static snd_pcm_uframes_t uac2_pcm_pointer(struct snd_pcm_substream *substream) | |
305 | { | |
306 | struct snd_uac2_chip *uac2 = snd_pcm_substream_chip(substream); | |
307 | struct uac2_rtd_params *prm; | |
308 | ||
309 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) | |
310 | prm = &uac2->p_prm; | |
311 | else | |
312 | prm = &uac2->c_prm; | |
313 | ||
314 | return bytes_to_frames(substream->runtime, prm->hw_ptr); | |
315 | } | |
316 | ||
317 | static int uac2_pcm_hw_params(struct snd_pcm_substream *substream, | |
318 | struct snd_pcm_hw_params *hw_params) | |
319 | { | |
320 | struct snd_uac2_chip *uac2 = snd_pcm_substream_chip(substream); | |
321 | struct uac2_rtd_params *prm; | |
322 | int err; | |
323 | ||
324 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) | |
325 | prm = &uac2->p_prm; | |
326 | else | |
327 | prm = &uac2->c_prm; | |
328 | ||
329 | err = snd_pcm_lib_malloc_pages(substream, | |
330 | params_buffer_bytes(hw_params)); | |
331 | if (err >= 0) { | |
332 | prm->dma_bytes = substream->runtime->dma_bytes; | |
333 | prm->dma_area = substream->runtime->dma_area; | |
334 | prm->period_size = params_period_bytes(hw_params); | |
335 | } | |
336 | ||
337 | return err; | |
338 | } | |
339 | ||
340 | static int uac2_pcm_hw_free(struct snd_pcm_substream *substream) | |
341 | { | |
342 | struct snd_uac2_chip *uac2 = snd_pcm_substream_chip(substream); | |
343 | struct uac2_rtd_params *prm; | |
344 | ||
345 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) | |
346 | prm = &uac2->p_prm; | |
347 | else | |
348 | prm = &uac2->c_prm; | |
349 | ||
350 | prm->dma_area = NULL; | |
351 | prm->dma_bytes = 0; | |
352 | prm->period_size = 0; | |
353 | ||
354 | return snd_pcm_lib_free_pages(substream); | |
355 | } | |
356 | ||
357 | static int uac2_pcm_open(struct snd_pcm_substream *substream) | |
358 | { | |
359 | struct snd_uac2_chip *uac2 = snd_pcm_substream_chip(substream); | |
360 | struct snd_pcm_runtime *runtime = substream->runtime; | |
361 | ||
362 | runtime->hw = uac2_pcm_hardware; | |
363 | ||
364 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { | |
365 | spin_lock_init(&uac2->p_prm.lock); | |
366 | runtime->hw.rate_min = p_srate; | |
367 | runtime->hw.formats = SNDRV_PCM_FMTBIT_S16_LE; /* ! p_ssize ! */ | |
368 | runtime->hw.channels_min = num_channels(p_chmask); | |
369 | runtime->hw.period_bytes_min = 2 * uac2->p_prm.max_psize | |
370 | / runtime->hw.periods_min; | |
371 | } else { | |
372 | spin_lock_init(&uac2->c_prm.lock); | |
373 | runtime->hw.rate_min = c_srate; | |
374 | runtime->hw.formats = SNDRV_PCM_FMTBIT_S16_LE; /* ! c_ssize ! */ | |
375 | runtime->hw.channels_min = num_channels(c_chmask); | |
376 | runtime->hw.period_bytes_min = 2 * uac2->c_prm.max_psize | |
377 | / runtime->hw.periods_min; | |
378 | } | |
379 | ||
380 | runtime->hw.rate_max = runtime->hw.rate_min; | |
381 | runtime->hw.channels_max = runtime->hw.channels_min; | |
382 | ||
383 | snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS); | |
384 | ||
385 | return 0; | |
386 | } | |
387 | ||
388 | /* ALSA cries without these function pointers */ | |
389 | static int uac2_pcm_null(struct snd_pcm_substream *substream) | |
390 | { | |
391 | return 0; | |
392 | } | |
393 | ||
394 | static struct snd_pcm_ops uac2_pcm_ops = { | |
395 | .open = uac2_pcm_open, | |
396 | .close = uac2_pcm_null, | |
397 | .ioctl = snd_pcm_lib_ioctl, | |
398 | .hw_params = uac2_pcm_hw_params, | |
399 | .hw_free = uac2_pcm_hw_free, | |
400 | .trigger = uac2_pcm_trigger, | |
401 | .pointer = uac2_pcm_pointer, | |
402 | .prepare = uac2_pcm_null, | |
403 | }; | |
404 | ||
405 | static int __devinit snd_uac2_probe(struct platform_device *pdev) | |
406 | { | |
407 | struct snd_uac2_chip *uac2 = pdev_to_uac2(pdev); | |
408 | struct snd_card *card; | |
409 | struct snd_pcm *pcm; | |
410 | int err; | |
411 | ||
412 | /* Choose any slot, with no id */ | |
413 | err = snd_card_create(-1, NULL, THIS_MODULE, 0, &card); | |
414 | if (err < 0) | |
415 | return err; | |
416 | ||
417 | uac2->card = card; | |
418 | ||
419 | /* | |
420 | * Create first PCM device | |
421 | * Create a substream only for non-zero channel streams | |
422 | */ | |
423 | err = snd_pcm_new(uac2->card, "UAC2 PCM", 0, | |
424 | p_chmask ? 1 : 0, c_chmask ? 1 : 0, &pcm); | |
425 | if (err < 0) | |
426 | goto snd_fail; | |
427 | ||
428 | strcpy(pcm->name, "UAC2 PCM"); | |
429 | pcm->private_data = uac2; | |
430 | ||
431 | uac2->pcm = pcm; | |
432 | ||
433 | snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &uac2_pcm_ops); | |
434 | snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &uac2_pcm_ops); | |
435 | ||
436 | strcpy(card->driver, "UAC2_Gadget"); | |
437 | strcpy(card->shortname, "UAC2_Gadget"); | |
438 | sprintf(card->longname, "UAC2_Gadget %i", pdev->id); | |
439 | ||
440 | snd_card_set_dev(card, &pdev->dev); | |
441 | ||
442 | snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS, | |
443 | snd_dma_continuous_data(GFP_KERNEL), 0, BUFF_SIZE_MAX); | |
444 | ||
445 | err = snd_card_register(card); | |
446 | if (!err) { | |
447 | platform_set_drvdata(pdev, card); | |
448 | return 0; | |
449 | } | |
450 | ||
451 | snd_fail: | |
452 | snd_card_free(card); | |
453 | ||
454 | uac2->pcm = NULL; | |
455 | uac2->card = NULL; | |
456 | ||
457 | return err; | |
458 | } | |
459 | ||
1e1a27c3 | 460 | static int snd_uac2_remove(struct platform_device *pdev) |
132fcb46 JB |
461 | { |
462 | struct snd_card *card = platform_get_drvdata(pdev); | |
463 | ||
464 | platform_set_drvdata(pdev, NULL); | |
465 | ||
466 | if (card) | |
467 | return snd_card_free(card); | |
468 | ||
469 | return 0; | |
470 | } | |
471 | ||
472 | static int alsa_uac2_init(struct audio_dev *agdev) | |
473 | { | |
474 | struct snd_uac2_chip *uac2 = &agdev->uac2; | |
475 | int err; | |
476 | ||
477 | uac2->pdrv.probe = snd_uac2_probe; | |
478 | uac2->pdrv.remove = snd_uac2_remove; | |
479 | uac2->pdrv.driver.name = uac2_name; | |
480 | ||
481 | uac2->pdev.id = 0; | |
482 | uac2->pdev.name = uac2_name; | |
483 | ||
484 | /* Register snd_uac2 driver */ | |
485 | err = platform_driver_register(&uac2->pdrv); | |
486 | if (err) | |
487 | return err; | |
488 | ||
489 | /* Register snd_uac2 device */ | |
490 | err = platform_device_register(&uac2->pdev); | |
491 | if (err) | |
492 | platform_driver_unregister(&uac2->pdrv); | |
493 | ||
494 | return err; | |
495 | } | |
496 | ||
497 | static void alsa_uac2_exit(struct audio_dev *agdev) | |
498 | { | |
499 | struct snd_uac2_chip *uac2 = &agdev->uac2; | |
500 | ||
501 | platform_driver_unregister(&uac2->pdrv); | |
502 | platform_device_unregister(&uac2->pdev); | |
503 | } | |
504 | ||
505 | ||
506 | /* --------- USB Function Interface ------------- */ | |
507 | ||
508 | enum { | |
509 | STR_ASSOC, | |
510 | STR_IF_CTRL, | |
511 | STR_CLKSRC_IN, | |
512 | STR_CLKSRC_OUT, | |
513 | STR_USB_IT, | |
514 | STR_IO_IT, | |
515 | STR_USB_OT, | |
516 | STR_IO_OT, | |
517 | STR_AS_OUT_ALT0, | |
518 | STR_AS_OUT_ALT1, | |
519 | STR_AS_IN_ALT0, | |
520 | STR_AS_IN_ALT1, | |
521 | }; | |
522 | ||
132fcb46 JB |
523 | static char clksrc_in[8]; |
524 | static char clksrc_out[8]; | |
132fcb46 JB |
525 | |
526 | static struct usb_string strings_fn[] = { | |
b36c3479 SAS |
527 | [STR_ASSOC].s = "Source/Sink", |
528 | [STR_IF_CTRL].s = "Topology Control", | |
132fcb46 JB |
529 | [STR_CLKSRC_IN].s = clksrc_in, |
530 | [STR_CLKSRC_OUT].s = clksrc_out, | |
b36c3479 SAS |
531 | [STR_USB_IT].s = "USBH Out", |
532 | [STR_IO_IT].s = "USBD Out", | |
533 | [STR_USB_OT].s = "USBH In", | |
534 | [STR_IO_OT].s = "USBD In", | |
535 | [STR_AS_OUT_ALT0].s = "Playback Inactive", | |
536 | [STR_AS_OUT_ALT1].s = "Playback Active", | |
537 | [STR_AS_IN_ALT0].s = "Capture Inactive", | |
538 | [STR_AS_IN_ALT1].s = "Capture Active", | |
132fcb46 JB |
539 | { }, |
540 | }; | |
541 | ||
542 | static struct usb_gadget_strings str_fn = { | |
543 | .language = 0x0409, /* en-us */ | |
544 | .strings = strings_fn, | |
545 | }; | |
546 | ||
547 | static struct usb_gadget_strings *fn_strings[] = { | |
548 | &str_fn, | |
549 | NULL, | |
550 | }; | |
551 | ||
552 | static struct usb_qualifier_descriptor devqual_desc = { | |
553 | .bLength = sizeof devqual_desc, | |
554 | .bDescriptorType = USB_DT_DEVICE_QUALIFIER, | |
555 | ||
556 | .bcdUSB = cpu_to_le16(0x200), | |
557 | .bDeviceClass = USB_CLASS_MISC, | |
558 | .bDeviceSubClass = 0x02, | |
559 | .bDeviceProtocol = 0x01, | |
560 | .bNumConfigurations = 1, | |
561 | .bRESERVED = 0, | |
562 | }; | |
563 | ||
564 | static struct usb_interface_assoc_descriptor iad_desc = { | |
565 | .bLength = sizeof iad_desc, | |
566 | .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION, | |
567 | ||
568 | .bFirstInterface = 0, | |
569 | .bInterfaceCount = 3, | |
570 | .bFunctionClass = USB_CLASS_AUDIO, | |
571 | .bFunctionSubClass = UAC2_FUNCTION_SUBCLASS_UNDEFINED, | |
572 | .bFunctionProtocol = UAC_VERSION_2, | |
573 | }; | |
574 | ||
575 | /* Audio Control Interface */ | |
576 | static struct usb_interface_descriptor std_ac_if_desc = { | |
577 | .bLength = sizeof std_ac_if_desc, | |
578 | .bDescriptorType = USB_DT_INTERFACE, | |
579 | ||
580 | .bAlternateSetting = 0, | |
581 | .bNumEndpoints = 0, | |
582 | .bInterfaceClass = USB_CLASS_AUDIO, | |
583 | .bInterfaceSubClass = USB_SUBCLASS_AUDIOCONTROL, | |
584 | .bInterfaceProtocol = UAC_VERSION_2, | |
585 | }; | |
586 | ||
587 | /* Clock source for IN traffic */ | |
588 | struct uac_clock_source_descriptor in_clk_src_desc = { | |
589 | .bLength = sizeof in_clk_src_desc, | |
590 | .bDescriptorType = USB_DT_CS_INTERFACE, | |
591 | ||
592 | .bDescriptorSubtype = UAC2_CLOCK_SOURCE, | |
593 | .bClockID = USB_IN_CLK_ID, | |
594 | .bmAttributes = UAC_CLOCK_SOURCE_TYPE_INT_FIXED, | |
595 | .bmControls = (CONTROL_RDONLY << CLK_FREQ_CTRL), | |
596 | .bAssocTerminal = 0, | |
597 | }; | |
598 | ||
599 | /* Clock source for OUT traffic */ | |
600 | struct uac_clock_source_descriptor out_clk_src_desc = { | |
601 | .bLength = sizeof out_clk_src_desc, | |
602 | .bDescriptorType = USB_DT_CS_INTERFACE, | |
603 | ||
604 | .bDescriptorSubtype = UAC2_CLOCK_SOURCE, | |
605 | .bClockID = USB_OUT_CLK_ID, | |
606 | .bmAttributes = UAC_CLOCK_SOURCE_TYPE_INT_FIXED, | |
607 | .bmControls = (CONTROL_RDONLY << CLK_FREQ_CTRL), | |
608 | .bAssocTerminal = 0, | |
609 | }; | |
610 | ||
611 | /* Input Terminal for USB_OUT */ | |
612 | struct uac2_input_terminal_descriptor usb_out_it_desc = { | |
613 | .bLength = sizeof usb_out_it_desc, | |
614 | .bDescriptorType = USB_DT_CS_INTERFACE, | |
615 | ||
616 | .bDescriptorSubtype = UAC_INPUT_TERMINAL, | |
617 | .bTerminalID = USB_OUT_IT_ID, | |
618 | .wTerminalType = cpu_to_le16(UAC_TERMINAL_STREAMING), | |
619 | .bAssocTerminal = 0, | |
620 | .bCSourceID = USB_OUT_CLK_ID, | |
621 | .iChannelNames = 0, | |
622 | .bmControls = (CONTROL_RDWR << COPY_CTRL), | |
623 | }; | |
624 | ||
625 | /* Input Terminal for I/O-In */ | |
626 | struct uac2_input_terminal_descriptor io_in_it_desc = { | |
627 | .bLength = sizeof io_in_it_desc, | |
628 | .bDescriptorType = USB_DT_CS_INTERFACE, | |
629 | ||
630 | .bDescriptorSubtype = UAC_INPUT_TERMINAL, | |
631 | .bTerminalID = IO_IN_IT_ID, | |
632 | .wTerminalType = cpu_to_le16(UAC_INPUT_TERMINAL_UNDEFINED), | |
633 | .bAssocTerminal = 0, | |
634 | .bCSourceID = USB_IN_CLK_ID, | |
635 | .iChannelNames = 0, | |
636 | .bmControls = (CONTROL_RDWR << COPY_CTRL), | |
637 | }; | |
638 | ||
639 | /* Ouput Terminal for USB_IN */ | |
640 | struct uac2_output_terminal_descriptor usb_in_ot_desc = { | |
641 | .bLength = sizeof usb_in_ot_desc, | |
642 | .bDescriptorType = USB_DT_CS_INTERFACE, | |
643 | ||
644 | .bDescriptorSubtype = UAC_OUTPUT_TERMINAL, | |
645 | .bTerminalID = USB_IN_OT_ID, | |
646 | .wTerminalType = cpu_to_le16(UAC_TERMINAL_STREAMING), | |
647 | .bAssocTerminal = 0, | |
648 | .bSourceID = IO_IN_IT_ID, | |
649 | .bCSourceID = USB_IN_CLK_ID, | |
650 | .bmControls = (CONTROL_RDWR << COPY_CTRL), | |
651 | }; | |
652 | ||
653 | /* Ouput Terminal for I/O-Out */ | |
654 | struct uac2_output_terminal_descriptor io_out_ot_desc = { | |
655 | .bLength = sizeof io_out_ot_desc, | |
656 | .bDescriptorType = USB_DT_CS_INTERFACE, | |
657 | ||
658 | .bDescriptorSubtype = UAC_OUTPUT_TERMINAL, | |
659 | .bTerminalID = IO_OUT_OT_ID, | |
660 | .wTerminalType = cpu_to_le16(UAC_OUTPUT_TERMINAL_UNDEFINED), | |
661 | .bAssocTerminal = 0, | |
662 | .bSourceID = USB_OUT_IT_ID, | |
663 | .bCSourceID = USB_OUT_CLK_ID, | |
664 | .bmControls = (CONTROL_RDWR << COPY_CTRL), | |
665 | }; | |
666 | ||
667 | struct uac2_ac_header_descriptor ac_hdr_desc = { | |
668 | .bLength = sizeof ac_hdr_desc, | |
669 | .bDescriptorType = USB_DT_CS_INTERFACE, | |
670 | ||
671 | .bDescriptorSubtype = UAC_MS_HEADER, | |
672 | .bcdADC = cpu_to_le16(0x200), | |
673 | .bCategory = UAC2_FUNCTION_IO_BOX, | |
674 | .wTotalLength = sizeof in_clk_src_desc + sizeof out_clk_src_desc | |
675 | + sizeof usb_out_it_desc + sizeof io_in_it_desc | |
676 | + sizeof usb_in_ot_desc + sizeof io_out_ot_desc, | |
677 | .bmControls = 0, | |
678 | }; | |
679 | ||
680 | /* Audio Streaming OUT Interface - Alt0 */ | |
681 | static struct usb_interface_descriptor std_as_out_if0_desc = { | |
682 | .bLength = sizeof std_as_out_if0_desc, | |
683 | .bDescriptorType = USB_DT_INTERFACE, | |
684 | ||
685 | .bAlternateSetting = 0, | |
686 | .bNumEndpoints = 0, | |
687 | .bInterfaceClass = USB_CLASS_AUDIO, | |
688 | .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING, | |
689 | .bInterfaceProtocol = UAC_VERSION_2, | |
690 | }; | |
691 | ||
692 | /* Audio Streaming OUT Interface - Alt1 */ | |
693 | static struct usb_interface_descriptor std_as_out_if1_desc = { | |
694 | .bLength = sizeof std_as_out_if1_desc, | |
695 | .bDescriptorType = USB_DT_INTERFACE, | |
696 | ||
697 | .bAlternateSetting = 1, | |
698 | .bNumEndpoints = 1, | |
699 | .bInterfaceClass = USB_CLASS_AUDIO, | |
700 | .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING, | |
701 | .bInterfaceProtocol = UAC_VERSION_2, | |
702 | }; | |
703 | ||
704 | /* Audio Stream OUT Intface Desc */ | |
705 | struct uac2_as_header_descriptor as_out_hdr_desc = { | |
706 | .bLength = sizeof as_out_hdr_desc, | |
707 | .bDescriptorType = USB_DT_CS_INTERFACE, | |
708 | ||
709 | .bDescriptorSubtype = UAC_AS_GENERAL, | |
710 | .bTerminalLink = USB_OUT_IT_ID, | |
711 | .bmControls = 0, | |
712 | .bFormatType = UAC_FORMAT_TYPE_I, | |
713 | .bmFormats = cpu_to_le32(UAC_FORMAT_TYPE_I_PCM), | |
714 | .iChannelNames = 0, | |
715 | }; | |
716 | ||
717 | /* Audio USB_OUT Format */ | |
718 | struct uac2_format_type_i_descriptor as_out_fmt1_desc = { | |
719 | .bLength = sizeof as_out_fmt1_desc, | |
720 | .bDescriptorType = USB_DT_CS_INTERFACE, | |
721 | .bDescriptorSubtype = UAC_FORMAT_TYPE, | |
722 | .bFormatType = UAC_FORMAT_TYPE_I, | |
723 | }; | |
724 | ||
725 | /* STD AS ISO OUT Endpoint */ | |
726 | struct usb_endpoint_descriptor fs_epout_desc = { | |
727 | .bLength = USB_DT_ENDPOINT_SIZE, | |
728 | .bDescriptorType = USB_DT_ENDPOINT, | |
729 | ||
730 | .bEndpointAddress = USB_DIR_OUT, | |
731 | .bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC, | |
732 | .bInterval = 1, | |
733 | }; | |
734 | ||
735 | struct usb_endpoint_descriptor hs_epout_desc = { | |
736 | .bLength = USB_DT_ENDPOINT_SIZE, | |
737 | .bDescriptorType = USB_DT_ENDPOINT, | |
738 | ||
739 | .bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC, | |
740 | .bInterval = 4, | |
741 | }; | |
742 | ||
743 | /* CS AS ISO OUT Endpoint */ | |
744 | static struct uac2_iso_endpoint_descriptor as_iso_out_desc = { | |
745 | .bLength = sizeof as_iso_out_desc, | |
746 | .bDescriptorType = USB_DT_CS_ENDPOINT, | |
747 | ||
748 | .bDescriptorSubtype = UAC_EP_GENERAL, | |
749 | .bmAttributes = 0, | |
750 | .bmControls = 0, | |
751 | .bLockDelayUnits = 0, | |
752 | .wLockDelay = 0, | |
753 | }; | |
754 | ||
755 | /* Audio Streaming IN Interface - Alt0 */ | |
756 | static struct usb_interface_descriptor std_as_in_if0_desc = { | |
757 | .bLength = sizeof std_as_in_if0_desc, | |
758 | .bDescriptorType = USB_DT_INTERFACE, | |
759 | ||
760 | .bAlternateSetting = 0, | |
761 | .bNumEndpoints = 0, | |
762 | .bInterfaceClass = USB_CLASS_AUDIO, | |
763 | .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING, | |
764 | .bInterfaceProtocol = UAC_VERSION_2, | |
765 | }; | |
766 | ||
767 | /* Audio Streaming IN Interface - Alt1 */ | |
768 | static struct usb_interface_descriptor std_as_in_if1_desc = { | |
769 | .bLength = sizeof std_as_in_if1_desc, | |
770 | .bDescriptorType = USB_DT_INTERFACE, | |
771 | ||
772 | .bAlternateSetting = 1, | |
773 | .bNumEndpoints = 1, | |
774 | .bInterfaceClass = USB_CLASS_AUDIO, | |
775 | .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING, | |
776 | .bInterfaceProtocol = UAC_VERSION_2, | |
777 | }; | |
778 | ||
779 | /* Audio Stream IN Intface Desc */ | |
780 | struct uac2_as_header_descriptor as_in_hdr_desc = { | |
781 | .bLength = sizeof as_in_hdr_desc, | |
782 | .bDescriptorType = USB_DT_CS_INTERFACE, | |
783 | ||
784 | .bDescriptorSubtype = UAC_AS_GENERAL, | |
785 | .bTerminalLink = USB_IN_OT_ID, | |
786 | .bmControls = 0, | |
787 | .bFormatType = UAC_FORMAT_TYPE_I, | |
788 | .bmFormats = cpu_to_le32(UAC_FORMAT_TYPE_I_PCM), | |
789 | .iChannelNames = 0, | |
790 | }; | |
791 | ||
792 | /* Audio USB_IN Format */ | |
793 | struct uac2_format_type_i_descriptor as_in_fmt1_desc = { | |
794 | .bLength = sizeof as_in_fmt1_desc, | |
795 | .bDescriptorType = USB_DT_CS_INTERFACE, | |
796 | .bDescriptorSubtype = UAC_FORMAT_TYPE, | |
797 | .bFormatType = UAC_FORMAT_TYPE_I, | |
798 | }; | |
799 | ||
800 | /* STD AS ISO IN Endpoint */ | |
801 | struct usb_endpoint_descriptor fs_epin_desc = { | |
802 | .bLength = USB_DT_ENDPOINT_SIZE, | |
803 | .bDescriptorType = USB_DT_ENDPOINT, | |
804 | ||
805 | .bEndpointAddress = USB_DIR_IN, | |
806 | .bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC, | |
807 | .bInterval = 1, | |
808 | }; | |
809 | ||
810 | struct usb_endpoint_descriptor hs_epin_desc = { | |
811 | .bLength = USB_DT_ENDPOINT_SIZE, | |
812 | .bDescriptorType = USB_DT_ENDPOINT, | |
813 | ||
814 | .bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC, | |
815 | .bInterval = 4, | |
816 | }; | |
817 | ||
818 | /* CS AS ISO IN Endpoint */ | |
819 | static struct uac2_iso_endpoint_descriptor as_iso_in_desc = { | |
820 | .bLength = sizeof as_iso_in_desc, | |
821 | .bDescriptorType = USB_DT_CS_ENDPOINT, | |
822 | ||
823 | .bDescriptorSubtype = UAC_EP_GENERAL, | |
824 | .bmAttributes = 0, | |
825 | .bmControls = 0, | |
826 | .bLockDelayUnits = 0, | |
827 | .wLockDelay = 0, | |
828 | }; | |
829 | ||
830 | static struct usb_descriptor_header *fs_audio_desc[] = { | |
831 | (struct usb_descriptor_header *)&iad_desc, | |
832 | (struct usb_descriptor_header *)&std_ac_if_desc, | |
833 | ||
834 | (struct usb_descriptor_header *)&ac_hdr_desc, | |
835 | (struct usb_descriptor_header *)&in_clk_src_desc, | |
836 | (struct usb_descriptor_header *)&out_clk_src_desc, | |
837 | (struct usb_descriptor_header *)&usb_out_it_desc, | |
838 | (struct usb_descriptor_header *)&io_in_it_desc, | |
839 | (struct usb_descriptor_header *)&usb_in_ot_desc, | |
840 | (struct usb_descriptor_header *)&io_out_ot_desc, | |
841 | ||
842 | (struct usb_descriptor_header *)&std_as_out_if0_desc, | |
843 | (struct usb_descriptor_header *)&std_as_out_if1_desc, | |
844 | ||
845 | (struct usb_descriptor_header *)&as_out_hdr_desc, | |
846 | (struct usb_descriptor_header *)&as_out_fmt1_desc, | |
847 | (struct usb_descriptor_header *)&fs_epout_desc, | |
848 | (struct usb_descriptor_header *)&as_iso_out_desc, | |
849 | ||
850 | (struct usb_descriptor_header *)&std_as_in_if0_desc, | |
851 | (struct usb_descriptor_header *)&std_as_in_if1_desc, | |
852 | ||
853 | (struct usb_descriptor_header *)&as_in_hdr_desc, | |
854 | (struct usb_descriptor_header *)&as_in_fmt1_desc, | |
855 | (struct usb_descriptor_header *)&fs_epin_desc, | |
856 | (struct usb_descriptor_header *)&as_iso_in_desc, | |
857 | NULL, | |
858 | }; | |
859 | ||
860 | static struct usb_descriptor_header *hs_audio_desc[] = { | |
861 | (struct usb_descriptor_header *)&iad_desc, | |
862 | (struct usb_descriptor_header *)&std_ac_if_desc, | |
863 | ||
864 | (struct usb_descriptor_header *)&ac_hdr_desc, | |
865 | (struct usb_descriptor_header *)&in_clk_src_desc, | |
866 | (struct usb_descriptor_header *)&out_clk_src_desc, | |
867 | (struct usb_descriptor_header *)&usb_out_it_desc, | |
868 | (struct usb_descriptor_header *)&io_in_it_desc, | |
869 | (struct usb_descriptor_header *)&usb_in_ot_desc, | |
870 | (struct usb_descriptor_header *)&io_out_ot_desc, | |
871 | ||
872 | (struct usb_descriptor_header *)&std_as_out_if0_desc, | |
873 | (struct usb_descriptor_header *)&std_as_out_if1_desc, | |
874 | ||
875 | (struct usb_descriptor_header *)&as_out_hdr_desc, | |
876 | (struct usb_descriptor_header *)&as_out_fmt1_desc, | |
877 | (struct usb_descriptor_header *)&hs_epout_desc, | |
878 | (struct usb_descriptor_header *)&as_iso_out_desc, | |
879 | ||
880 | (struct usb_descriptor_header *)&std_as_in_if0_desc, | |
881 | (struct usb_descriptor_header *)&std_as_in_if1_desc, | |
882 | ||
883 | (struct usb_descriptor_header *)&as_in_hdr_desc, | |
884 | (struct usb_descriptor_header *)&as_in_fmt1_desc, | |
885 | (struct usb_descriptor_header *)&hs_epin_desc, | |
886 | (struct usb_descriptor_header *)&as_iso_in_desc, | |
887 | NULL, | |
888 | }; | |
889 | ||
890 | struct cntrl_cur_lay3 { | |
891 | __u32 dCUR; | |
892 | }; | |
893 | ||
894 | struct cntrl_range_lay3 { | |
895 | __u16 wNumSubRanges; | |
896 | __u32 dMIN; | |
897 | __u32 dMAX; | |
898 | __u32 dRES; | |
899 | } __packed; | |
900 | ||
901 | static inline void | |
902 | free_ep(struct uac2_rtd_params *prm, struct usb_ep *ep) | |
903 | { | |
904 | struct snd_uac2_chip *uac2 = prm_to_uac2(prm); | |
905 | int i; | |
906 | ||
907 | prm->ep_enabled = false; | |
908 | ||
909 | for (i = 0; i < USB_XFERS; i++) { | |
910 | if (prm->ureq[i].req) { | |
911 | usb_ep_dequeue(ep, prm->ureq[i].req); | |
912 | usb_ep_free_request(ep, prm->ureq[i].req); | |
913 | prm->ureq[i].req = NULL; | |
914 | } | |
915 | } | |
916 | ||
917 | if (usb_ep_disable(ep)) | |
918 | dev_err(&uac2->pdev.dev, | |
919 | "%s:%d Error!\n", __func__, __LINE__); | |
920 | } | |
921 | ||
922 | static int __init | |
923 | afunc_bind(struct usb_configuration *cfg, struct usb_function *fn) | |
924 | { | |
925 | struct audio_dev *agdev = func_to_agdev(fn); | |
926 | struct snd_uac2_chip *uac2 = &agdev->uac2; | |
927 | struct usb_composite_dev *cdev = cfg->cdev; | |
928 | struct usb_gadget *gadget = cdev->gadget; | |
929 | struct uac2_rtd_params *prm; | |
930 | int ret; | |
931 | ||
932 | ret = usb_interface_id(cfg, fn); | |
933 | if (ret < 0) { | |
934 | dev_err(&uac2->pdev.dev, | |
935 | "%s:%d Error!\n", __func__, __LINE__); | |
936 | return ret; | |
937 | } | |
938 | std_ac_if_desc.bInterfaceNumber = ret; | |
4ce63571 SAS |
939 | agdev->ac_intf = ret; |
940 | agdev->ac_alt = 0; | |
132fcb46 JB |
941 | |
942 | ret = usb_interface_id(cfg, fn); | |
943 | if (ret < 0) { | |
944 | dev_err(&uac2->pdev.dev, | |
945 | "%s:%d Error!\n", __func__, __LINE__); | |
946 | return ret; | |
947 | } | |
948 | std_as_out_if0_desc.bInterfaceNumber = ret; | |
949 | std_as_out_if1_desc.bInterfaceNumber = ret; | |
4ce63571 SAS |
950 | agdev->as_out_intf = ret; |
951 | agdev->as_out_alt = 0; | |
132fcb46 JB |
952 | |
953 | ret = usb_interface_id(cfg, fn); | |
954 | if (ret < 0) { | |
955 | dev_err(&uac2->pdev.dev, | |
956 | "%s:%d Error!\n", __func__, __LINE__); | |
957 | return ret; | |
958 | } | |
959 | std_as_in_if0_desc.bInterfaceNumber = ret; | |
960 | std_as_in_if1_desc.bInterfaceNumber = ret; | |
4ce63571 SAS |
961 | agdev->as_in_intf = ret; |
962 | agdev->as_in_alt = 0; | |
132fcb46 JB |
963 | |
964 | agdev->out_ep = usb_ep_autoconfig(gadget, &fs_epout_desc); | |
391aa852 | 965 | if (!agdev->out_ep) { |
132fcb46 JB |
966 | dev_err(&uac2->pdev.dev, |
967 | "%s:%d Error!\n", __func__, __LINE__); | |
391aa852 SAS |
968 | goto err; |
969 | } | |
132fcb46 JB |
970 | agdev->out_ep->driver_data = agdev; |
971 | ||
972 | agdev->in_ep = usb_ep_autoconfig(gadget, &fs_epin_desc); | |
391aa852 | 973 | if (!agdev->in_ep) { |
132fcb46 JB |
974 | dev_err(&uac2->pdev.dev, |
975 | "%s:%d Error!\n", __func__, __LINE__); | |
391aa852 SAS |
976 | goto err; |
977 | } | |
132fcb46 JB |
978 | agdev->in_ep->driver_data = agdev; |
979 | ||
980 | hs_epout_desc.bEndpointAddress = fs_epout_desc.bEndpointAddress; | |
981 | hs_epout_desc.wMaxPacketSize = fs_epout_desc.wMaxPacketSize; | |
982 | hs_epin_desc.bEndpointAddress = fs_epin_desc.bEndpointAddress; | |
983 | hs_epin_desc.wMaxPacketSize = fs_epin_desc.wMaxPacketSize; | |
984 | ||
10287bae SAS |
985 | ret = usb_assign_descriptors(fn, fs_audio_desc, hs_audio_desc, NULL); |
986 | if (ret) | |
987 | goto err; | |
132fcb46 JB |
988 | |
989 | prm = &agdev->uac2.c_prm; | |
990 | prm->max_psize = hs_epout_desc.wMaxPacketSize; | |
991 | prm->rbuf = kzalloc(prm->max_psize * USB_XFERS, GFP_KERNEL); | |
992 | if (!prm->rbuf) { | |
993 | prm->max_psize = 0; | |
994 | dev_err(&uac2->pdev.dev, | |
995 | "%s:%d Error!\n", __func__, __LINE__); | |
391aa852 | 996 | goto err; |
132fcb46 JB |
997 | } |
998 | ||
999 | prm = &agdev->uac2.p_prm; | |
1000 | prm->max_psize = hs_epin_desc.wMaxPacketSize; | |
1001 | prm->rbuf = kzalloc(prm->max_psize * USB_XFERS, GFP_KERNEL); | |
1002 | if (!prm->rbuf) { | |
1003 | prm->max_psize = 0; | |
1004 | dev_err(&uac2->pdev.dev, | |
1005 | "%s:%d Error!\n", __func__, __LINE__); | |
391aa852 | 1006 | goto err; |
132fcb46 JB |
1007 | } |
1008 | ||
391aa852 SAS |
1009 | ret = alsa_uac2_init(agdev); |
1010 | if (ret) | |
1011 | goto err; | |
1012 | return 0; | |
1013 | err: | |
1014 | kfree(agdev->uac2.p_prm.rbuf); | |
1015 | kfree(agdev->uac2.c_prm.rbuf); | |
10287bae | 1016 | usb_free_all_descriptors(fn); |
391aa852 SAS |
1017 | if (agdev->in_ep) |
1018 | agdev->in_ep->driver_data = NULL; | |
1019 | if (agdev->out_ep) | |
1020 | agdev->out_ep->driver_data = NULL; | |
1021 | return -EINVAL; | |
132fcb46 JB |
1022 | } |
1023 | ||
1024 | static void | |
1025 | afunc_unbind(struct usb_configuration *cfg, struct usb_function *fn) | |
1026 | { | |
1027 | struct audio_dev *agdev = func_to_agdev(fn); | |
132fcb46 JB |
1028 | struct uac2_rtd_params *prm; |
1029 | ||
1030 | alsa_uac2_exit(agdev); | |
1031 | ||
1032 | prm = &agdev->uac2.p_prm; | |
1033 | kfree(prm->rbuf); | |
1034 | ||
1035 | prm = &agdev->uac2.c_prm; | |
1036 | kfree(prm->rbuf); | |
10287bae | 1037 | usb_free_all_descriptors(fn); |
132fcb46 JB |
1038 | |
1039 | if (agdev->in_ep) | |
1040 | agdev->in_ep->driver_data = NULL; | |
1041 | if (agdev->out_ep) | |
1042 | agdev->out_ep->driver_data = NULL; | |
1043 | } | |
1044 | ||
1045 | static int | |
1046 | afunc_set_alt(struct usb_function *fn, unsigned intf, unsigned alt) | |
1047 | { | |
1048 | struct usb_composite_dev *cdev = fn->config->cdev; | |
1049 | struct audio_dev *agdev = func_to_agdev(fn); | |
1050 | struct snd_uac2_chip *uac2 = &agdev->uac2; | |
1051 | struct usb_gadget *gadget = cdev->gadget; | |
1052 | struct usb_request *req; | |
1053 | struct usb_ep *ep; | |
1054 | struct uac2_rtd_params *prm; | |
1055 | int i; | |
1056 | ||
1057 | /* No i/f has more than 2 alt settings */ | |
1058 | if (alt > 1) { | |
1059 | dev_err(&uac2->pdev.dev, | |
1060 | "%s:%d Error!\n", __func__, __LINE__); | |
1061 | return -EINVAL; | |
1062 | } | |
1063 | ||
4ce63571 | 1064 | if (intf == agdev->ac_intf) { |
132fcb46 JB |
1065 | /* Control I/f has only 1 AltSetting - 0 */ |
1066 | if (alt) { | |
1067 | dev_err(&uac2->pdev.dev, | |
1068 | "%s:%d Error!\n", __func__, __LINE__); | |
1069 | return -EINVAL; | |
1070 | } | |
1071 | return 0; | |
1072 | } | |
1073 | ||
4ce63571 | 1074 | if (intf == agdev->as_out_intf) { |
132fcb46 JB |
1075 | ep = agdev->out_ep; |
1076 | prm = &uac2->c_prm; | |
1077 | config_ep_by_speed(gadget, fn, ep); | |
4ce63571 SAS |
1078 | agdev->as_out_alt = alt; |
1079 | } else if (intf == agdev->as_in_intf) { | |
132fcb46 JB |
1080 | ep = agdev->in_ep; |
1081 | prm = &uac2->p_prm; | |
1082 | config_ep_by_speed(gadget, fn, ep); | |
4ce63571 | 1083 | agdev->as_in_alt = alt; |
132fcb46 JB |
1084 | } else { |
1085 | dev_err(&uac2->pdev.dev, | |
1086 | "%s:%d Error!\n", __func__, __LINE__); | |
1087 | return -EINVAL; | |
1088 | } | |
1089 | ||
1090 | if (alt == 0) { | |
1091 | free_ep(prm, ep); | |
1092 | return 0; | |
1093 | } | |
1094 | ||
1095 | prm->ep_enabled = true; | |
1096 | usb_ep_enable(ep); | |
1097 | ||
1098 | for (i = 0; i < USB_XFERS; i++) { | |
1099 | if (prm->ureq[i].req) { | |
1100 | if (usb_ep_queue(ep, prm->ureq[i].req, GFP_ATOMIC)) | |
1101 | dev_err(&uac2->pdev.dev, "%d Error!\n", | |
1102 | __LINE__); | |
1103 | continue; | |
1104 | } | |
1105 | ||
1106 | req = usb_ep_alloc_request(ep, GFP_ATOMIC); | |
1107 | if (req == NULL) { | |
1108 | dev_err(&uac2->pdev.dev, | |
1109 | "%s:%d Error!\n", __func__, __LINE__); | |
1110 | return -EINVAL; | |
1111 | } | |
1112 | ||
1113 | prm->ureq[i].req = req; | |
1114 | prm->ureq[i].pp = prm; | |
1115 | ||
1116 | req->zero = 0; | |
132fcb46 JB |
1117 | req->context = &prm->ureq[i]; |
1118 | req->length = prm->max_psize; | |
1119 | req->complete = agdev_iso_complete; | |
1120 | req->buf = prm->rbuf + i * req->length; | |
1121 | ||
1122 | if (usb_ep_queue(ep, req, GFP_ATOMIC)) | |
1123 | dev_err(&uac2->pdev.dev, "%d Error!\n", __LINE__); | |
1124 | } | |
1125 | ||
1126 | return 0; | |
1127 | } | |
1128 | ||
1129 | static int | |
1130 | afunc_get_alt(struct usb_function *fn, unsigned intf) | |
1131 | { | |
1132 | struct audio_dev *agdev = func_to_agdev(fn); | |
1133 | struct snd_uac2_chip *uac2 = &agdev->uac2; | |
1134 | ||
4ce63571 SAS |
1135 | if (intf == agdev->ac_intf) |
1136 | return agdev->ac_alt; | |
1137 | else if (intf == agdev->as_out_intf) | |
1138 | return agdev->as_out_alt; | |
1139 | else if (intf == agdev->as_in_intf) | |
1140 | return agdev->as_in_alt; | |
132fcb46 JB |
1141 | else |
1142 | dev_err(&uac2->pdev.dev, | |
1143 | "%s:%d Invalid Interface %d!\n", | |
1144 | __func__, __LINE__, intf); | |
1145 | ||
1146 | return -EINVAL; | |
1147 | } | |
1148 | ||
1149 | static void | |
1150 | afunc_disable(struct usb_function *fn) | |
1151 | { | |
1152 | struct audio_dev *agdev = func_to_agdev(fn); | |
1153 | struct snd_uac2_chip *uac2 = &agdev->uac2; | |
1154 | ||
1155 | free_ep(&uac2->p_prm, agdev->in_ep); | |
4ce63571 | 1156 | agdev->as_in_alt = 0; |
132fcb46 JB |
1157 | |
1158 | free_ep(&uac2->c_prm, agdev->out_ep); | |
4ce63571 | 1159 | agdev->as_out_alt = 0; |
132fcb46 JB |
1160 | } |
1161 | ||
1162 | static int | |
1163 | in_rq_cur(struct usb_function *fn, const struct usb_ctrlrequest *cr) | |
1164 | { | |
1165 | struct usb_request *req = fn->config->cdev->req; | |
1166 | struct audio_dev *agdev = func_to_agdev(fn); | |
1167 | struct snd_uac2_chip *uac2 = &agdev->uac2; | |
1168 | u16 w_length = le16_to_cpu(cr->wLength); | |
1169 | u16 w_index = le16_to_cpu(cr->wIndex); | |
1170 | u16 w_value = le16_to_cpu(cr->wValue); | |
1171 | u8 entity_id = (w_index >> 8) & 0xff; | |
1172 | u8 control_selector = w_value >> 8; | |
1173 | int value = -EOPNOTSUPP; | |
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 | ||
1197 | static int | |
1198 | in_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; | |
1203 | u16 w_length = le16_to_cpu(cr->wLength); | |
1204 | u16 w_index = le16_to_cpu(cr->wIndex); | |
1205 | u16 w_value = le16_to_cpu(cr->wValue); | |
1206 | u8 entity_id = (w_index >> 8) & 0xff; | |
1207 | u8 control_selector = w_value >> 8; | |
1208 | struct cntrl_range_lay3 r; | |
1209 | int value = -EOPNOTSUPP; | |
1210 | ||
1211 | if (control_selector == UAC2_CS_CONTROL_SAM_FREQ) { | |
1212 | if (entity_id == USB_IN_CLK_ID) | |
1213 | r.dMIN = p_srate; | |
1214 | else if (entity_id == USB_OUT_CLK_ID) | |
1215 | r.dMIN = c_srate; | |
1216 | else | |
1217 | return -EOPNOTSUPP; | |
1218 | ||
1219 | r.dMAX = r.dMIN; | |
1220 | r.dRES = 0; | |
1221 | r.wNumSubRanges = 1; | |
1222 | ||
1223 | value = min_t(unsigned, w_length, sizeof r); | |
1224 | memcpy(req->buf, &r, value); | |
1225 | } else { | |
1226 | dev_err(&uac2->pdev.dev, | |
1227 | "%s:%d control_selector=%d TODO!\n", | |
1228 | __func__, __LINE__, control_selector); | |
1229 | } | |
1230 | ||
1231 | return value; | |
1232 | } | |
1233 | ||
1234 | static int | |
1235 | ac_rq_in(struct usb_function *fn, const struct usb_ctrlrequest *cr) | |
1236 | { | |
1237 | if (cr->bRequest == UAC2_CS_CUR) | |
1238 | return in_rq_cur(fn, cr); | |
1239 | else if (cr->bRequest == UAC2_CS_RANGE) | |
1240 | return in_rq_range(fn, cr); | |
1241 | else | |
1242 | return -EOPNOTSUPP; | |
1243 | } | |
1244 | ||
1245 | static int | |
1246 | out_rq_cur(struct usb_function *fn, const struct usb_ctrlrequest *cr) | |
1247 | { | |
1248 | u16 w_length = le16_to_cpu(cr->wLength); | |
1249 | u16 w_value = le16_to_cpu(cr->wValue); | |
1250 | u8 control_selector = w_value >> 8; | |
1251 | ||
1252 | if (control_selector == UAC2_CS_CONTROL_SAM_FREQ) | |
1253 | return w_length; | |
1254 | ||
1255 | return -EOPNOTSUPP; | |
1256 | } | |
1257 | ||
1258 | static int | |
1259 | setup_rq_inf(struct usb_function *fn, const struct usb_ctrlrequest *cr) | |
1260 | { | |
1261 | struct audio_dev *agdev = func_to_agdev(fn); | |
1262 | struct snd_uac2_chip *uac2 = &agdev->uac2; | |
1263 | u16 w_index = le16_to_cpu(cr->wIndex); | |
1264 | u8 intf = w_index & 0xff; | |
1265 | ||
4ce63571 | 1266 | if (intf != agdev->ac_intf) { |
132fcb46 JB |
1267 | dev_err(&uac2->pdev.dev, |
1268 | "%s:%d Error!\n", __func__, __LINE__); | |
1269 | return -EOPNOTSUPP; | |
1270 | } | |
1271 | ||
1272 | if (cr->bRequestType & USB_DIR_IN) | |
1273 | return ac_rq_in(fn, cr); | |
1274 | else if (cr->bRequest == UAC2_CS_CUR) | |
1275 | return out_rq_cur(fn, cr); | |
1276 | ||
1277 | return -EOPNOTSUPP; | |
1278 | } | |
1279 | ||
1280 | static int | |
1281 | afunc_setup(struct usb_function *fn, const struct usb_ctrlrequest *cr) | |
1282 | { | |
1283 | struct usb_composite_dev *cdev = fn->config->cdev; | |
1284 | struct audio_dev *agdev = func_to_agdev(fn); | |
1285 | struct snd_uac2_chip *uac2 = &agdev->uac2; | |
1286 | struct usb_request *req = cdev->req; | |
1287 | u16 w_length = le16_to_cpu(cr->wLength); | |
1288 | int value = -EOPNOTSUPP; | |
1289 | ||
1290 | /* Only Class specific requests are supposed to reach here */ | |
1291 | if ((cr->bRequestType & USB_TYPE_MASK) != USB_TYPE_CLASS) | |
1292 | return -EOPNOTSUPP; | |
1293 | ||
1294 | if ((cr->bRequestType & USB_RECIP_MASK) == USB_RECIP_INTERFACE) | |
1295 | value = setup_rq_inf(fn, cr); | |
1296 | else | |
1297 | dev_err(&uac2->pdev.dev, "%s:%d Error!\n", __func__, __LINE__); | |
1298 | ||
1299 | if (value >= 0) { | |
1300 | req->length = value; | |
1301 | req->zero = value < w_length; | |
1302 | value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC); | |
1303 | if (value < 0) { | |
1304 | dev_err(&uac2->pdev.dev, | |
1305 | "%s:%d Error!\n", __func__, __LINE__); | |
1306 | req->status = 0; | |
1307 | } | |
1308 | } | |
1309 | ||
1310 | return value; | |
1311 | } | |
1312 | ||
1313 | static int audio_bind_config(struct usb_configuration *cfg) | |
1314 | { | |
1616e99d | 1315 | int res; |
132fcb46 JB |
1316 | |
1317 | agdev_g = kzalloc(sizeof *agdev_g, GFP_KERNEL); | |
1318 | if (agdev_g == NULL) { | |
1319 | printk(KERN_ERR "Unable to allocate audio gadget\n"); | |
1320 | return -ENOMEM; | |
1321 | } | |
1322 | ||
1616e99d SAS |
1323 | res = usb_string_ids_tab(cfg->cdev, strings_fn); |
1324 | if (res) | |
1325 | return res; | |
1326 | iad_desc.iFunction = strings_fn[STR_ASSOC].id; | |
1327 | std_ac_if_desc.iInterface = strings_fn[STR_IF_CTRL].id; | |
1328 | in_clk_src_desc.iClockSource = strings_fn[STR_CLKSRC_IN].id; | |
1329 | out_clk_src_desc.iClockSource = strings_fn[STR_CLKSRC_OUT].id; | |
1330 | usb_out_it_desc.iTerminal = strings_fn[STR_USB_IT].id; | |
1331 | io_in_it_desc.iTerminal = strings_fn[STR_IO_IT].id; | |
1332 | usb_in_ot_desc.iTerminal = strings_fn[STR_USB_OT].id; | |
1333 | io_out_ot_desc.iTerminal = strings_fn[STR_IO_OT].id; | |
1334 | std_as_out_if0_desc.iInterface = strings_fn[STR_AS_OUT_ALT0].id; | |
1335 | std_as_out_if1_desc.iInterface = strings_fn[STR_AS_OUT_ALT1].id; | |
1336 | std_as_in_if0_desc.iInterface = strings_fn[STR_AS_IN_ALT0].id; | |
1337 | std_as_in_if1_desc.iInterface = strings_fn[STR_AS_IN_ALT1].id; | |
132fcb46 JB |
1338 | |
1339 | agdev_g->func.name = "uac2_func"; | |
1340 | agdev_g->func.strings = fn_strings; | |
1341 | agdev_g->func.bind = afunc_bind; | |
1342 | agdev_g->func.unbind = afunc_unbind; | |
1343 | agdev_g->func.set_alt = afunc_set_alt; | |
1344 | agdev_g->func.get_alt = afunc_get_alt; | |
1345 | agdev_g->func.disable = afunc_disable; | |
1346 | agdev_g->func.setup = afunc_setup; | |
1347 | ||
1348 | /* Initialize the configurable parameters */ | |
1349 | usb_out_it_desc.bNrChannels = num_channels(c_chmask); | |
1350 | usb_out_it_desc.bmChannelConfig = cpu_to_le32(c_chmask); | |
1351 | io_in_it_desc.bNrChannels = num_channels(p_chmask); | |
1352 | io_in_it_desc.bmChannelConfig = cpu_to_le32(p_chmask); | |
1353 | as_out_hdr_desc.bNrChannels = num_channels(c_chmask); | |
1354 | as_out_hdr_desc.bmChannelConfig = cpu_to_le32(c_chmask); | |
1355 | as_in_hdr_desc.bNrChannels = num_channels(p_chmask); | |
1356 | as_in_hdr_desc.bmChannelConfig = cpu_to_le32(p_chmask); | |
1357 | as_out_fmt1_desc.bSubslotSize = c_ssize; | |
1358 | as_out_fmt1_desc.bBitResolution = c_ssize * 8; | |
1359 | as_in_fmt1_desc.bSubslotSize = p_ssize; | |
1360 | as_in_fmt1_desc.bBitResolution = p_ssize * 8; | |
1361 | ||
1362 | snprintf(clksrc_in, sizeof(clksrc_in), "%uHz", p_srate); | |
1363 | snprintf(clksrc_out, sizeof(clksrc_out), "%uHz", c_srate); | |
1364 | ||
1365 | res = usb_add_function(cfg, &agdev_g->func); | |
1366 | if (res < 0) | |
1367 | kfree(agdev_g); | |
1368 | ||
1369 | return res; | |
1370 | } | |
1371 | ||
1372 | static void | |
1373 | uac2_unbind_config(struct usb_configuration *cfg) | |
1374 | { | |
1375 | kfree(agdev_g); | |
1376 | agdev_g = NULL; | |
1377 | } |