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); | |
132fcb46 JB |
263 | struct uac2_rtd_params *prm; |
264 | unsigned long flags; | |
132fcb46 JB |
265 | int err = 0; |
266 | ||
b6424353 | 267 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
132fcb46 | 268 | prm = &uac2->p_prm; |
b6424353 | 269 | else |
132fcb46 | 270 | prm = &uac2->c_prm; |
132fcb46 JB |
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 | ||
41ac7b3a | 400 | static int snd_uac2_probe(struct platform_device *pdev) |
132fcb46 JB |
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 | ||
1e1a27c3 | 455 | static int snd_uac2_remove(struct platform_device *pdev) |
132fcb46 JB |
456 | { |
457 | struct snd_card *card = platform_get_drvdata(pdev); | |
458 | ||
132fcb46 JB |
459 | if (card) |
460 | return snd_card_free(card); | |
461 | ||
462 | return 0; | |
463 | } | |
464 | ||
465 | static int alsa_uac2_init(struct audio_dev *agdev) | |
466 | { | |
467 | struct snd_uac2_chip *uac2 = &agdev->uac2; | |
468 | int err; | |
469 | ||
470 | uac2->pdrv.probe = snd_uac2_probe; | |
471 | uac2->pdrv.remove = snd_uac2_remove; | |
472 | uac2->pdrv.driver.name = uac2_name; | |
473 | ||
474 | uac2->pdev.id = 0; | |
475 | uac2->pdev.name = uac2_name; | |
476 | ||
477 | /* Register snd_uac2 driver */ | |
478 | err = platform_driver_register(&uac2->pdrv); | |
479 | if (err) | |
480 | return err; | |
481 | ||
482 | /* Register snd_uac2 device */ | |
483 | err = platform_device_register(&uac2->pdev); | |
484 | if (err) | |
485 | platform_driver_unregister(&uac2->pdrv); | |
486 | ||
487 | return err; | |
488 | } | |
489 | ||
490 | static void alsa_uac2_exit(struct audio_dev *agdev) | |
491 | { | |
492 | struct snd_uac2_chip *uac2 = &agdev->uac2; | |
493 | ||
494 | platform_driver_unregister(&uac2->pdrv); | |
495 | platform_device_unregister(&uac2->pdev); | |
496 | } | |
497 | ||
498 | ||
499 | /* --------- USB Function Interface ------------- */ | |
500 | ||
501 | enum { | |
502 | STR_ASSOC, | |
503 | STR_IF_CTRL, | |
504 | STR_CLKSRC_IN, | |
505 | STR_CLKSRC_OUT, | |
506 | STR_USB_IT, | |
507 | STR_IO_IT, | |
508 | STR_USB_OT, | |
509 | STR_IO_OT, | |
510 | STR_AS_OUT_ALT0, | |
511 | STR_AS_OUT_ALT1, | |
512 | STR_AS_IN_ALT0, | |
513 | STR_AS_IN_ALT1, | |
514 | }; | |
515 | ||
132fcb46 JB |
516 | static char clksrc_in[8]; |
517 | static char clksrc_out[8]; | |
132fcb46 JB |
518 | |
519 | static struct usb_string strings_fn[] = { | |
b36c3479 SAS |
520 | [STR_ASSOC].s = "Source/Sink", |
521 | [STR_IF_CTRL].s = "Topology Control", | |
132fcb46 JB |
522 | [STR_CLKSRC_IN].s = clksrc_in, |
523 | [STR_CLKSRC_OUT].s = clksrc_out, | |
b36c3479 SAS |
524 | [STR_USB_IT].s = "USBH Out", |
525 | [STR_IO_IT].s = "USBD Out", | |
526 | [STR_USB_OT].s = "USBH In", | |
527 | [STR_IO_OT].s = "USBD In", | |
528 | [STR_AS_OUT_ALT0].s = "Playback Inactive", | |
529 | [STR_AS_OUT_ALT1].s = "Playback Active", | |
530 | [STR_AS_IN_ALT0].s = "Capture Inactive", | |
531 | [STR_AS_IN_ALT1].s = "Capture Active", | |
132fcb46 JB |
532 | { }, |
533 | }; | |
534 | ||
535 | static struct usb_gadget_strings str_fn = { | |
536 | .language = 0x0409, /* en-us */ | |
537 | .strings = strings_fn, | |
538 | }; | |
539 | ||
540 | static struct usb_gadget_strings *fn_strings[] = { | |
541 | &str_fn, | |
542 | NULL, | |
543 | }; | |
544 | ||
545 | static struct usb_qualifier_descriptor devqual_desc = { | |
546 | .bLength = sizeof devqual_desc, | |
547 | .bDescriptorType = USB_DT_DEVICE_QUALIFIER, | |
548 | ||
549 | .bcdUSB = cpu_to_le16(0x200), | |
550 | .bDeviceClass = USB_CLASS_MISC, | |
551 | .bDeviceSubClass = 0x02, | |
552 | .bDeviceProtocol = 0x01, | |
553 | .bNumConfigurations = 1, | |
554 | .bRESERVED = 0, | |
555 | }; | |
556 | ||
557 | static struct usb_interface_assoc_descriptor iad_desc = { | |
558 | .bLength = sizeof iad_desc, | |
559 | .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION, | |
560 | ||
561 | .bFirstInterface = 0, | |
562 | .bInterfaceCount = 3, | |
563 | .bFunctionClass = USB_CLASS_AUDIO, | |
564 | .bFunctionSubClass = UAC2_FUNCTION_SUBCLASS_UNDEFINED, | |
565 | .bFunctionProtocol = UAC_VERSION_2, | |
566 | }; | |
567 | ||
568 | /* Audio Control Interface */ | |
569 | static struct usb_interface_descriptor std_ac_if_desc = { | |
570 | .bLength = sizeof std_ac_if_desc, | |
571 | .bDescriptorType = USB_DT_INTERFACE, | |
572 | ||
573 | .bAlternateSetting = 0, | |
574 | .bNumEndpoints = 0, | |
575 | .bInterfaceClass = USB_CLASS_AUDIO, | |
576 | .bInterfaceSubClass = USB_SUBCLASS_AUDIOCONTROL, | |
577 | .bInterfaceProtocol = UAC_VERSION_2, | |
578 | }; | |
579 | ||
580 | /* Clock source for IN traffic */ | |
581 | struct uac_clock_source_descriptor in_clk_src_desc = { | |
582 | .bLength = sizeof in_clk_src_desc, | |
583 | .bDescriptorType = USB_DT_CS_INTERFACE, | |
584 | ||
585 | .bDescriptorSubtype = UAC2_CLOCK_SOURCE, | |
586 | .bClockID = USB_IN_CLK_ID, | |
587 | .bmAttributes = UAC_CLOCK_SOURCE_TYPE_INT_FIXED, | |
588 | .bmControls = (CONTROL_RDONLY << CLK_FREQ_CTRL), | |
589 | .bAssocTerminal = 0, | |
590 | }; | |
591 | ||
592 | /* Clock source for OUT traffic */ | |
593 | struct uac_clock_source_descriptor out_clk_src_desc = { | |
594 | .bLength = sizeof out_clk_src_desc, | |
595 | .bDescriptorType = USB_DT_CS_INTERFACE, | |
596 | ||
597 | .bDescriptorSubtype = UAC2_CLOCK_SOURCE, | |
598 | .bClockID = USB_OUT_CLK_ID, | |
599 | .bmAttributes = UAC_CLOCK_SOURCE_TYPE_INT_FIXED, | |
600 | .bmControls = (CONTROL_RDONLY << CLK_FREQ_CTRL), | |
601 | .bAssocTerminal = 0, | |
602 | }; | |
603 | ||
604 | /* Input Terminal for USB_OUT */ | |
605 | struct uac2_input_terminal_descriptor usb_out_it_desc = { | |
606 | .bLength = sizeof usb_out_it_desc, | |
607 | .bDescriptorType = USB_DT_CS_INTERFACE, | |
608 | ||
609 | .bDescriptorSubtype = UAC_INPUT_TERMINAL, | |
610 | .bTerminalID = USB_OUT_IT_ID, | |
611 | .wTerminalType = cpu_to_le16(UAC_TERMINAL_STREAMING), | |
612 | .bAssocTerminal = 0, | |
613 | .bCSourceID = USB_OUT_CLK_ID, | |
614 | .iChannelNames = 0, | |
615 | .bmControls = (CONTROL_RDWR << COPY_CTRL), | |
616 | }; | |
617 | ||
618 | /* Input Terminal for I/O-In */ | |
619 | struct uac2_input_terminal_descriptor io_in_it_desc = { | |
620 | .bLength = sizeof io_in_it_desc, | |
621 | .bDescriptorType = USB_DT_CS_INTERFACE, | |
622 | ||
623 | .bDescriptorSubtype = UAC_INPUT_TERMINAL, | |
624 | .bTerminalID = IO_IN_IT_ID, | |
625 | .wTerminalType = cpu_to_le16(UAC_INPUT_TERMINAL_UNDEFINED), | |
626 | .bAssocTerminal = 0, | |
627 | .bCSourceID = USB_IN_CLK_ID, | |
628 | .iChannelNames = 0, | |
629 | .bmControls = (CONTROL_RDWR << COPY_CTRL), | |
630 | }; | |
631 | ||
632 | /* Ouput Terminal for USB_IN */ | |
633 | struct uac2_output_terminal_descriptor usb_in_ot_desc = { | |
634 | .bLength = sizeof usb_in_ot_desc, | |
635 | .bDescriptorType = USB_DT_CS_INTERFACE, | |
636 | ||
637 | .bDescriptorSubtype = UAC_OUTPUT_TERMINAL, | |
638 | .bTerminalID = USB_IN_OT_ID, | |
639 | .wTerminalType = cpu_to_le16(UAC_TERMINAL_STREAMING), | |
640 | .bAssocTerminal = 0, | |
641 | .bSourceID = IO_IN_IT_ID, | |
642 | .bCSourceID = USB_IN_CLK_ID, | |
643 | .bmControls = (CONTROL_RDWR << COPY_CTRL), | |
644 | }; | |
645 | ||
646 | /* Ouput Terminal for I/O-Out */ | |
647 | struct uac2_output_terminal_descriptor io_out_ot_desc = { | |
648 | .bLength = sizeof io_out_ot_desc, | |
649 | .bDescriptorType = USB_DT_CS_INTERFACE, | |
650 | ||
651 | .bDescriptorSubtype = UAC_OUTPUT_TERMINAL, | |
652 | .bTerminalID = IO_OUT_OT_ID, | |
653 | .wTerminalType = cpu_to_le16(UAC_OUTPUT_TERMINAL_UNDEFINED), | |
654 | .bAssocTerminal = 0, | |
655 | .bSourceID = USB_OUT_IT_ID, | |
656 | .bCSourceID = USB_OUT_CLK_ID, | |
657 | .bmControls = (CONTROL_RDWR << COPY_CTRL), | |
658 | }; | |
659 | ||
660 | struct uac2_ac_header_descriptor ac_hdr_desc = { | |
661 | .bLength = sizeof ac_hdr_desc, | |
662 | .bDescriptorType = USB_DT_CS_INTERFACE, | |
663 | ||
664 | .bDescriptorSubtype = UAC_MS_HEADER, | |
665 | .bcdADC = cpu_to_le16(0x200), | |
666 | .bCategory = UAC2_FUNCTION_IO_BOX, | |
667 | .wTotalLength = sizeof in_clk_src_desc + sizeof out_clk_src_desc | |
668 | + sizeof usb_out_it_desc + sizeof io_in_it_desc | |
669 | + sizeof usb_in_ot_desc + sizeof io_out_ot_desc, | |
670 | .bmControls = 0, | |
671 | }; | |
672 | ||
673 | /* Audio Streaming OUT Interface - Alt0 */ | |
674 | static struct usb_interface_descriptor std_as_out_if0_desc = { | |
675 | .bLength = sizeof std_as_out_if0_desc, | |
676 | .bDescriptorType = USB_DT_INTERFACE, | |
677 | ||
678 | .bAlternateSetting = 0, | |
679 | .bNumEndpoints = 0, | |
680 | .bInterfaceClass = USB_CLASS_AUDIO, | |
681 | .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING, | |
682 | .bInterfaceProtocol = UAC_VERSION_2, | |
683 | }; | |
684 | ||
685 | /* Audio Streaming OUT Interface - Alt1 */ | |
686 | static struct usb_interface_descriptor std_as_out_if1_desc = { | |
687 | .bLength = sizeof std_as_out_if1_desc, | |
688 | .bDescriptorType = USB_DT_INTERFACE, | |
689 | ||
690 | .bAlternateSetting = 1, | |
691 | .bNumEndpoints = 1, | |
692 | .bInterfaceClass = USB_CLASS_AUDIO, | |
693 | .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING, | |
694 | .bInterfaceProtocol = UAC_VERSION_2, | |
695 | }; | |
696 | ||
697 | /* Audio Stream OUT Intface Desc */ | |
698 | struct uac2_as_header_descriptor as_out_hdr_desc = { | |
699 | .bLength = sizeof as_out_hdr_desc, | |
700 | .bDescriptorType = USB_DT_CS_INTERFACE, | |
701 | ||
702 | .bDescriptorSubtype = UAC_AS_GENERAL, | |
703 | .bTerminalLink = USB_OUT_IT_ID, | |
704 | .bmControls = 0, | |
705 | .bFormatType = UAC_FORMAT_TYPE_I, | |
706 | .bmFormats = cpu_to_le32(UAC_FORMAT_TYPE_I_PCM), | |
707 | .iChannelNames = 0, | |
708 | }; | |
709 | ||
710 | /* Audio USB_OUT Format */ | |
711 | struct uac2_format_type_i_descriptor as_out_fmt1_desc = { | |
712 | .bLength = sizeof as_out_fmt1_desc, | |
713 | .bDescriptorType = USB_DT_CS_INTERFACE, | |
714 | .bDescriptorSubtype = UAC_FORMAT_TYPE, | |
715 | .bFormatType = UAC_FORMAT_TYPE_I, | |
716 | }; | |
717 | ||
718 | /* STD AS ISO OUT Endpoint */ | |
719 | struct usb_endpoint_descriptor fs_epout_desc = { | |
720 | .bLength = USB_DT_ENDPOINT_SIZE, | |
721 | .bDescriptorType = USB_DT_ENDPOINT, | |
722 | ||
723 | .bEndpointAddress = USB_DIR_OUT, | |
724 | .bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC, | |
725 | .bInterval = 1, | |
726 | }; | |
727 | ||
728 | struct usb_endpoint_descriptor hs_epout_desc = { | |
729 | .bLength = USB_DT_ENDPOINT_SIZE, | |
730 | .bDescriptorType = USB_DT_ENDPOINT, | |
731 | ||
732 | .bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC, | |
733 | .bInterval = 4, | |
734 | }; | |
735 | ||
736 | /* CS AS ISO OUT Endpoint */ | |
737 | static struct uac2_iso_endpoint_descriptor as_iso_out_desc = { | |
738 | .bLength = sizeof as_iso_out_desc, | |
739 | .bDescriptorType = USB_DT_CS_ENDPOINT, | |
740 | ||
741 | .bDescriptorSubtype = UAC_EP_GENERAL, | |
742 | .bmAttributes = 0, | |
743 | .bmControls = 0, | |
744 | .bLockDelayUnits = 0, | |
745 | .wLockDelay = 0, | |
746 | }; | |
747 | ||
748 | /* Audio Streaming IN Interface - Alt0 */ | |
749 | static struct usb_interface_descriptor std_as_in_if0_desc = { | |
750 | .bLength = sizeof std_as_in_if0_desc, | |
751 | .bDescriptorType = USB_DT_INTERFACE, | |
752 | ||
753 | .bAlternateSetting = 0, | |
754 | .bNumEndpoints = 0, | |
755 | .bInterfaceClass = USB_CLASS_AUDIO, | |
756 | .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING, | |
757 | .bInterfaceProtocol = UAC_VERSION_2, | |
758 | }; | |
759 | ||
760 | /* Audio Streaming IN Interface - Alt1 */ | |
761 | static struct usb_interface_descriptor std_as_in_if1_desc = { | |
762 | .bLength = sizeof std_as_in_if1_desc, | |
763 | .bDescriptorType = USB_DT_INTERFACE, | |
764 | ||
765 | .bAlternateSetting = 1, | |
766 | .bNumEndpoints = 1, | |
767 | .bInterfaceClass = USB_CLASS_AUDIO, | |
768 | .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING, | |
769 | .bInterfaceProtocol = UAC_VERSION_2, | |
770 | }; | |
771 | ||
772 | /* Audio Stream IN Intface Desc */ | |
773 | struct uac2_as_header_descriptor as_in_hdr_desc = { | |
774 | .bLength = sizeof as_in_hdr_desc, | |
775 | .bDescriptorType = USB_DT_CS_INTERFACE, | |
776 | ||
777 | .bDescriptorSubtype = UAC_AS_GENERAL, | |
778 | .bTerminalLink = USB_IN_OT_ID, | |
779 | .bmControls = 0, | |
780 | .bFormatType = UAC_FORMAT_TYPE_I, | |
781 | .bmFormats = cpu_to_le32(UAC_FORMAT_TYPE_I_PCM), | |
782 | .iChannelNames = 0, | |
783 | }; | |
784 | ||
785 | /* Audio USB_IN Format */ | |
786 | struct uac2_format_type_i_descriptor as_in_fmt1_desc = { | |
787 | .bLength = sizeof as_in_fmt1_desc, | |
788 | .bDescriptorType = USB_DT_CS_INTERFACE, | |
789 | .bDescriptorSubtype = UAC_FORMAT_TYPE, | |
790 | .bFormatType = UAC_FORMAT_TYPE_I, | |
791 | }; | |
792 | ||
793 | /* STD AS ISO IN Endpoint */ | |
794 | struct usb_endpoint_descriptor fs_epin_desc = { | |
795 | .bLength = USB_DT_ENDPOINT_SIZE, | |
796 | .bDescriptorType = USB_DT_ENDPOINT, | |
797 | ||
798 | .bEndpointAddress = USB_DIR_IN, | |
799 | .bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC, | |
800 | .bInterval = 1, | |
801 | }; | |
802 | ||
803 | struct usb_endpoint_descriptor hs_epin_desc = { | |
804 | .bLength = USB_DT_ENDPOINT_SIZE, | |
805 | .bDescriptorType = USB_DT_ENDPOINT, | |
806 | ||
807 | .bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC, | |
808 | .bInterval = 4, | |
809 | }; | |
810 | ||
811 | /* CS AS ISO IN Endpoint */ | |
812 | static struct uac2_iso_endpoint_descriptor as_iso_in_desc = { | |
813 | .bLength = sizeof as_iso_in_desc, | |
814 | .bDescriptorType = USB_DT_CS_ENDPOINT, | |
815 | ||
816 | .bDescriptorSubtype = UAC_EP_GENERAL, | |
817 | .bmAttributes = 0, | |
818 | .bmControls = 0, | |
819 | .bLockDelayUnits = 0, | |
820 | .wLockDelay = 0, | |
821 | }; | |
822 | ||
823 | static struct usb_descriptor_header *fs_audio_desc[] = { | |
824 | (struct usb_descriptor_header *)&iad_desc, | |
825 | (struct usb_descriptor_header *)&std_ac_if_desc, | |
826 | ||
827 | (struct usb_descriptor_header *)&ac_hdr_desc, | |
828 | (struct usb_descriptor_header *)&in_clk_src_desc, | |
829 | (struct usb_descriptor_header *)&out_clk_src_desc, | |
830 | (struct usb_descriptor_header *)&usb_out_it_desc, | |
831 | (struct usb_descriptor_header *)&io_in_it_desc, | |
832 | (struct usb_descriptor_header *)&usb_in_ot_desc, | |
833 | (struct usb_descriptor_header *)&io_out_ot_desc, | |
834 | ||
835 | (struct usb_descriptor_header *)&std_as_out_if0_desc, | |
836 | (struct usb_descriptor_header *)&std_as_out_if1_desc, | |
837 | ||
838 | (struct usb_descriptor_header *)&as_out_hdr_desc, | |
839 | (struct usb_descriptor_header *)&as_out_fmt1_desc, | |
840 | (struct usb_descriptor_header *)&fs_epout_desc, | |
841 | (struct usb_descriptor_header *)&as_iso_out_desc, | |
842 | ||
843 | (struct usb_descriptor_header *)&std_as_in_if0_desc, | |
844 | (struct usb_descriptor_header *)&std_as_in_if1_desc, | |
845 | ||
846 | (struct usb_descriptor_header *)&as_in_hdr_desc, | |
847 | (struct usb_descriptor_header *)&as_in_fmt1_desc, | |
848 | (struct usb_descriptor_header *)&fs_epin_desc, | |
849 | (struct usb_descriptor_header *)&as_iso_in_desc, | |
850 | NULL, | |
851 | }; | |
852 | ||
853 | static struct usb_descriptor_header *hs_audio_desc[] = { | |
854 | (struct usb_descriptor_header *)&iad_desc, | |
855 | (struct usb_descriptor_header *)&std_ac_if_desc, | |
856 | ||
857 | (struct usb_descriptor_header *)&ac_hdr_desc, | |
858 | (struct usb_descriptor_header *)&in_clk_src_desc, | |
859 | (struct usb_descriptor_header *)&out_clk_src_desc, | |
860 | (struct usb_descriptor_header *)&usb_out_it_desc, | |
861 | (struct usb_descriptor_header *)&io_in_it_desc, | |
862 | (struct usb_descriptor_header *)&usb_in_ot_desc, | |
863 | (struct usb_descriptor_header *)&io_out_ot_desc, | |
864 | ||
865 | (struct usb_descriptor_header *)&std_as_out_if0_desc, | |
866 | (struct usb_descriptor_header *)&std_as_out_if1_desc, | |
867 | ||
868 | (struct usb_descriptor_header *)&as_out_hdr_desc, | |
869 | (struct usb_descriptor_header *)&as_out_fmt1_desc, | |
870 | (struct usb_descriptor_header *)&hs_epout_desc, | |
871 | (struct usb_descriptor_header *)&as_iso_out_desc, | |
872 | ||
873 | (struct usb_descriptor_header *)&std_as_in_if0_desc, | |
874 | (struct usb_descriptor_header *)&std_as_in_if1_desc, | |
875 | ||
876 | (struct usb_descriptor_header *)&as_in_hdr_desc, | |
877 | (struct usb_descriptor_header *)&as_in_fmt1_desc, | |
878 | (struct usb_descriptor_header *)&hs_epin_desc, | |
879 | (struct usb_descriptor_header *)&as_iso_in_desc, | |
880 | NULL, | |
881 | }; | |
882 | ||
883 | struct cntrl_cur_lay3 { | |
884 | __u32 dCUR; | |
885 | }; | |
886 | ||
887 | struct cntrl_range_lay3 { | |
888 | __u16 wNumSubRanges; | |
889 | __u32 dMIN; | |
890 | __u32 dMAX; | |
891 | __u32 dRES; | |
892 | } __packed; | |
893 | ||
894 | static inline void | |
895 | free_ep(struct uac2_rtd_params *prm, struct usb_ep *ep) | |
896 | { | |
897 | struct snd_uac2_chip *uac2 = prm_to_uac2(prm); | |
898 | int i; | |
899 | ||
900 | prm->ep_enabled = false; | |
901 | ||
902 | for (i = 0; i < USB_XFERS; i++) { | |
903 | if (prm->ureq[i].req) { | |
904 | usb_ep_dequeue(ep, prm->ureq[i].req); | |
905 | usb_ep_free_request(ep, prm->ureq[i].req); | |
906 | prm->ureq[i].req = NULL; | |
907 | } | |
908 | } | |
909 | ||
910 | if (usb_ep_disable(ep)) | |
911 | dev_err(&uac2->pdev.dev, | |
912 | "%s:%d Error!\n", __func__, __LINE__); | |
913 | } | |
914 | ||
915 | static int __init | |
916 | afunc_bind(struct usb_configuration *cfg, struct usb_function *fn) | |
917 | { | |
918 | struct audio_dev *agdev = func_to_agdev(fn); | |
919 | struct snd_uac2_chip *uac2 = &agdev->uac2; | |
920 | struct usb_composite_dev *cdev = cfg->cdev; | |
921 | struct usb_gadget *gadget = cdev->gadget; | |
922 | struct uac2_rtd_params *prm; | |
923 | int ret; | |
924 | ||
925 | ret = usb_interface_id(cfg, fn); | |
926 | if (ret < 0) { | |
927 | dev_err(&uac2->pdev.dev, | |
928 | "%s:%d Error!\n", __func__, __LINE__); | |
929 | return ret; | |
930 | } | |
931 | std_ac_if_desc.bInterfaceNumber = ret; | |
4ce63571 SAS |
932 | agdev->ac_intf = ret; |
933 | agdev->ac_alt = 0; | |
132fcb46 JB |
934 | |
935 | ret = usb_interface_id(cfg, fn); | |
936 | if (ret < 0) { | |
937 | dev_err(&uac2->pdev.dev, | |
938 | "%s:%d Error!\n", __func__, __LINE__); | |
939 | return ret; | |
940 | } | |
941 | std_as_out_if0_desc.bInterfaceNumber = ret; | |
942 | std_as_out_if1_desc.bInterfaceNumber = ret; | |
4ce63571 SAS |
943 | agdev->as_out_intf = ret; |
944 | agdev->as_out_alt = 0; | |
132fcb46 JB |
945 | |
946 | ret = usb_interface_id(cfg, fn); | |
947 | if (ret < 0) { | |
948 | dev_err(&uac2->pdev.dev, | |
949 | "%s:%d Error!\n", __func__, __LINE__); | |
950 | return ret; | |
951 | } | |
952 | std_as_in_if0_desc.bInterfaceNumber = ret; | |
953 | std_as_in_if1_desc.bInterfaceNumber = ret; | |
4ce63571 SAS |
954 | agdev->as_in_intf = ret; |
955 | agdev->as_in_alt = 0; | |
132fcb46 JB |
956 | |
957 | agdev->out_ep = usb_ep_autoconfig(gadget, &fs_epout_desc); | |
391aa852 | 958 | if (!agdev->out_ep) { |
132fcb46 JB |
959 | dev_err(&uac2->pdev.dev, |
960 | "%s:%d Error!\n", __func__, __LINE__); | |
391aa852 SAS |
961 | goto err; |
962 | } | |
132fcb46 JB |
963 | agdev->out_ep->driver_data = agdev; |
964 | ||
965 | agdev->in_ep = usb_ep_autoconfig(gadget, &fs_epin_desc); | |
391aa852 | 966 | if (!agdev->in_ep) { |
132fcb46 JB |
967 | dev_err(&uac2->pdev.dev, |
968 | "%s:%d Error!\n", __func__, __LINE__); | |
391aa852 SAS |
969 | goto err; |
970 | } | |
132fcb46 JB |
971 | agdev->in_ep->driver_data = agdev; |
972 | ||
973 | hs_epout_desc.bEndpointAddress = fs_epout_desc.bEndpointAddress; | |
974 | hs_epout_desc.wMaxPacketSize = fs_epout_desc.wMaxPacketSize; | |
975 | hs_epin_desc.bEndpointAddress = fs_epin_desc.bEndpointAddress; | |
976 | hs_epin_desc.wMaxPacketSize = fs_epin_desc.wMaxPacketSize; | |
977 | ||
10287bae SAS |
978 | ret = usb_assign_descriptors(fn, fs_audio_desc, hs_audio_desc, NULL); |
979 | if (ret) | |
980 | goto err; | |
132fcb46 JB |
981 | |
982 | prm = &agdev->uac2.c_prm; | |
983 | prm->max_psize = hs_epout_desc.wMaxPacketSize; | |
984 | prm->rbuf = kzalloc(prm->max_psize * USB_XFERS, GFP_KERNEL); | |
985 | if (!prm->rbuf) { | |
986 | prm->max_psize = 0; | |
987 | dev_err(&uac2->pdev.dev, | |
988 | "%s:%d Error!\n", __func__, __LINE__); | |
391aa852 | 989 | goto err; |
132fcb46 JB |
990 | } |
991 | ||
992 | prm = &agdev->uac2.p_prm; | |
993 | prm->max_psize = hs_epin_desc.wMaxPacketSize; | |
994 | prm->rbuf = kzalloc(prm->max_psize * USB_XFERS, GFP_KERNEL); | |
995 | if (!prm->rbuf) { | |
996 | prm->max_psize = 0; | |
997 | dev_err(&uac2->pdev.dev, | |
998 | "%s:%d Error!\n", __func__, __LINE__); | |
391aa852 | 999 | goto err; |
132fcb46 JB |
1000 | } |
1001 | ||
391aa852 SAS |
1002 | ret = alsa_uac2_init(agdev); |
1003 | if (ret) | |
1004 | goto err; | |
1005 | return 0; | |
1006 | err: | |
1007 | kfree(agdev->uac2.p_prm.rbuf); | |
1008 | kfree(agdev->uac2.c_prm.rbuf); | |
10287bae | 1009 | usb_free_all_descriptors(fn); |
391aa852 SAS |
1010 | if (agdev->in_ep) |
1011 | agdev->in_ep->driver_data = NULL; | |
1012 | if (agdev->out_ep) | |
1013 | agdev->out_ep->driver_data = NULL; | |
1014 | return -EINVAL; | |
132fcb46 JB |
1015 | } |
1016 | ||
1017 | static void | |
1018 | afunc_unbind(struct usb_configuration *cfg, struct usb_function *fn) | |
1019 | { | |
1020 | struct audio_dev *agdev = func_to_agdev(fn); | |
132fcb46 JB |
1021 | struct uac2_rtd_params *prm; |
1022 | ||
1023 | alsa_uac2_exit(agdev); | |
1024 | ||
1025 | prm = &agdev->uac2.p_prm; | |
1026 | kfree(prm->rbuf); | |
1027 | ||
1028 | prm = &agdev->uac2.c_prm; | |
1029 | kfree(prm->rbuf); | |
10287bae | 1030 | usb_free_all_descriptors(fn); |
132fcb46 JB |
1031 | |
1032 | if (agdev->in_ep) | |
1033 | agdev->in_ep->driver_data = NULL; | |
1034 | if (agdev->out_ep) | |
1035 | agdev->out_ep->driver_data = NULL; | |
1036 | } | |
1037 | ||
1038 | static int | |
1039 | afunc_set_alt(struct usb_function *fn, unsigned intf, unsigned alt) | |
1040 | { | |
1041 | struct usb_composite_dev *cdev = fn->config->cdev; | |
1042 | struct audio_dev *agdev = func_to_agdev(fn); | |
1043 | struct snd_uac2_chip *uac2 = &agdev->uac2; | |
1044 | struct usb_gadget *gadget = cdev->gadget; | |
1045 | struct usb_request *req; | |
1046 | struct usb_ep *ep; | |
1047 | struct uac2_rtd_params *prm; | |
1048 | int i; | |
1049 | ||
1050 | /* No i/f has more than 2 alt settings */ | |
1051 | if (alt > 1) { | |
1052 | dev_err(&uac2->pdev.dev, | |
1053 | "%s:%d Error!\n", __func__, __LINE__); | |
1054 | return -EINVAL; | |
1055 | } | |
1056 | ||
4ce63571 | 1057 | if (intf == agdev->ac_intf) { |
132fcb46 JB |
1058 | /* Control I/f has only 1 AltSetting - 0 */ |
1059 | if (alt) { | |
1060 | dev_err(&uac2->pdev.dev, | |
1061 | "%s:%d Error!\n", __func__, __LINE__); | |
1062 | return -EINVAL; | |
1063 | } | |
1064 | return 0; | |
1065 | } | |
1066 | ||
4ce63571 | 1067 | if (intf == agdev->as_out_intf) { |
132fcb46 JB |
1068 | ep = agdev->out_ep; |
1069 | prm = &uac2->c_prm; | |
1070 | config_ep_by_speed(gadget, fn, ep); | |
4ce63571 SAS |
1071 | agdev->as_out_alt = alt; |
1072 | } else if (intf == agdev->as_in_intf) { | |
132fcb46 JB |
1073 | ep = agdev->in_ep; |
1074 | prm = &uac2->p_prm; | |
1075 | config_ep_by_speed(gadget, fn, ep); | |
4ce63571 | 1076 | agdev->as_in_alt = alt; |
132fcb46 JB |
1077 | } else { |
1078 | dev_err(&uac2->pdev.dev, | |
1079 | "%s:%d Error!\n", __func__, __LINE__); | |
1080 | return -EINVAL; | |
1081 | } | |
1082 | ||
1083 | if (alt == 0) { | |
1084 | free_ep(prm, ep); | |
1085 | return 0; | |
1086 | } | |
1087 | ||
1088 | prm->ep_enabled = true; | |
1089 | usb_ep_enable(ep); | |
1090 | ||
1091 | for (i = 0; i < USB_XFERS; i++) { | |
1092 | if (prm->ureq[i].req) { | |
1093 | if (usb_ep_queue(ep, prm->ureq[i].req, GFP_ATOMIC)) | |
1094 | dev_err(&uac2->pdev.dev, "%d Error!\n", | |
1095 | __LINE__); | |
1096 | continue; | |
1097 | } | |
1098 | ||
1099 | req = usb_ep_alloc_request(ep, GFP_ATOMIC); | |
1100 | if (req == NULL) { | |
1101 | dev_err(&uac2->pdev.dev, | |
1102 | "%s:%d Error!\n", __func__, __LINE__); | |
1103 | return -EINVAL; | |
1104 | } | |
1105 | ||
1106 | prm->ureq[i].req = req; | |
1107 | prm->ureq[i].pp = prm; | |
1108 | ||
1109 | req->zero = 0; | |
132fcb46 JB |
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; | |
1114 | ||
1115 | if (usb_ep_queue(ep, req, GFP_ATOMIC)) | |
1116 | dev_err(&uac2->pdev.dev, "%d Error!\n", __LINE__); | |
1117 | } | |
1118 | ||
1119 | return 0; | |
1120 | } | |
1121 | ||
1122 | static int | |
1123 | afunc_get_alt(struct usb_function *fn, unsigned intf) | |
1124 | { | |
1125 | struct audio_dev *agdev = func_to_agdev(fn); | |
1126 | struct snd_uac2_chip *uac2 = &agdev->uac2; | |
1127 | ||
4ce63571 SAS |
1128 | if (intf == agdev->ac_intf) |
1129 | return agdev->ac_alt; | |
1130 | else if (intf == agdev->as_out_intf) | |
1131 | return agdev->as_out_alt; | |
1132 | else if (intf == agdev->as_in_intf) | |
1133 | return agdev->as_in_alt; | |
132fcb46 JB |
1134 | else |
1135 | dev_err(&uac2->pdev.dev, | |
1136 | "%s:%d Invalid Interface %d!\n", | |
1137 | __func__, __LINE__, intf); | |
1138 | ||
1139 | return -EINVAL; | |
1140 | } | |
1141 | ||
1142 | static void | |
1143 | afunc_disable(struct usb_function *fn) | |
1144 | { | |
1145 | struct audio_dev *agdev = func_to_agdev(fn); | |
1146 | struct snd_uac2_chip *uac2 = &agdev->uac2; | |
1147 | ||
1148 | free_ep(&uac2->p_prm, agdev->in_ep); | |
4ce63571 | 1149 | agdev->as_in_alt = 0; |
132fcb46 JB |
1150 | |
1151 | free_ep(&uac2->c_prm, agdev->out_ep); | |
4ce63571 | 1152 | agdev->as_out_alt = 0; |
132fcb46 JB |
1153 | } |
1154 | ||
1155 | static int | |
1156 | in_rq_cur(struct usb_function *fn, const struct usb_ctrlrequest *cr) | |
1157 | { | |
1158 | struct usb_request *req = fn->config->cdev->req; | |
1159 | struct audio_dev *agdev = func_to_agdev(fn); | |
1160 | struct snd_uac2_chip *uac2 = &agdev->uac2; | |
1161 | u16 w_length = le16_to_cpu(cr->wLength); | |
1162 | u16 w_index = le16_to_cpu(cr->wIndex); | |
1163 | u16 w_value = le16_to_cpu(cr->wValue); | |
1164 | u8 entity_id = (w_index >> 8) & 0xff; | |
1165 | u8 control_selector = w_value >> 8; | |
1166 | int value = -EOPNOTSUPP; | |
1167 | ||
1168 | if (control_selector == UAC2_CS_CONTROL_SAM_FREQ) { | |
1169 | struct cntrl_cur_lay3 c; | |
1170 | ||
1171 | if (entity_id == USB_IN_CLK_ID) | |
1172 | c.dCUR = p_srate; | |
1173 | else if (entity_id == USB_OUT_CLK_ID) | |
1174 | c.dCUR = c_srate; | |
1175 | ||
1176 | value = min_t(unsigned, w_length, sizeof c); | |
1177 | memcpy(req->buf, &c, value); | |
1178 | } else if (control_selector == UAC2_CS_CONTROL_CLOCK_VALID) { | |
1179 | *(u8 *)req->buf = 1; | |
1180 | value = min_t(unsigned, w_length, 1); | |
1181 | } else { | |
1182 | dev_err(&uac2->pdev.dev, | |
1183 | "%s:%d control_selector=%d TODO!\n", | |
1184 | __func__, __LINE__, control_selector); | |
1185 | } | |
1186 | ||
1187 | return value; | |
1188 | } | |
1189 | ||
1190 | static int | |
1191 | in_rq_range(struct usb_function *fn, const struct usb_ctrlrequest *cr) | |
1192 | { | |
1193 | struct usb_request *req = fn->config->cdev->req; | |
1194 | struct audio_dev *agdev = func_to_agdev(fn); | |
1195 | struct snd_uac2_chip *uac2 = &agdev->uac2; | |
1196 | u16 w_length = le16_to_cpu(cr->wLength); | |
1197 | u16 w_index = le16_to_cpu(cr->wIndex); | |
1198 | u16 w_value = le16_to_cpu(cr->wValue); | |
1199 | u8 entity_id = (w_index >> 8) & 0xff; | |
1200 | u8 control_selector = w_value >> 8; | |
1201 | struct cntrl_range_lay3 r; | |
1202 | int value = -EOPNOTSUPP; | |
1203 | ||
1204 | if (control_selector == UAC2_CS_CONTROL_SAM_FREQ) { | |
1205 | if (entity_id == USB_IN_CLK_ID) | |
1206 | r.dMIN = p_srate; | |
1207 | else if (entity_id == USB_OUT_CLK_ID) | |
1208 | r.dMIN = c_srate; | |
1209 | else | |
1210 | return -EOPNOTSUPP; | |
1211 | ||
1212 | r.dMAX = r.dMIN; | |
1213 | r.dRES = 0; | |
1214 | r.wNumSubRanges = 1; | |
1215 | ||
1216 | value = min_t(unsigned, w_length, sizeof r); | |
1217 | memcpy(req->buf, &r, value); | |
1218 | } else { | |
1219 | dev_err(&uac2->pdev.dev, | |
1220 | "%s:%d control_selector=%d TODO!\n", | |
1221 | __func__, __LINE__, control_selector); | |
1222 | } | |
1223 | ||
1224 | return value; | |
1225 | } | |
1226 | ||
1227 | static int | |
1228 | ac_rq_in(struct usb_function *fn, const struct usb_ctrlrequest *cr) | |
1229 | { | |
1230 | if (cr->bRequest == UAC2_CS_CUR) | |
1231 | return in_rq_cur(fn, cr); | |
1232 | else if (cr->bRequest == UAC2_CS_RANGE) | |
1233 | return in_rq_range(fn, cr); | |
1234 | else | |
1235 | return -EOPNOTSUPP; | |
1236 | } | |
1237 | ||
1238 | static int | |
1239 | out_rq_cur(struct usb_function *fn, const struct usb_ctrlrequest *cr) | |
1240 | { | |
1241 | u16 w_length = le16_to_cpu(cr->wLength); | |
1242 | u16 w_value = le16_to_cpu(cr->wValue); | |
1243 | u8 control_selector = w_value >> 8; | |
1244 | ||
1245 | if (control_selector == UAC2_CS_CONTROL_SAM_FREQ) | |
1246 | return w_length; | |
1247 | ||
1248 | return -EOPNOTSUPP; | |
1249 | } | |
1250 | ||
1251 | static int | |
1252 | setup_rq_inf(struct usb_function *fn, const struct usb_ctrlrequest *cr) | |
1253 | { | |
1254 | struct audio_dev *agdev = func_to_agdev(fn); | |
1255 | struct snd_uac2_chip *uac2 = &agdev->uac2; | |
1256 | u16 w_index = le16_to_cpu(cr->wIndex); | |
1257 | u8 intf = w_index & 0xff; | |
1258 | ||
4ce63571 | 1259 | if (intf != agdev->ac_intf) { |
132fcb46 JB |
1260 | dev_err(&uac2->pdev.dev, |
1261 | "%s:%d Error!\n", __func__, __LINE__); | |
1262 | return -EOPNOTSUPP; | |
1263 | } | |
1264 | ||
1265 | if (cr->bRequestType & USB_DIR_IN) | |
1266 | return ac_rq_in(fn, cr); | |
1267 | else if (cr->bRequest == UAC2_CS_CUR) | |
1268 | return out_rq_cur(fn, cr); | |
1269 | ||
1270 | return -EOPNOTSUPP; | |
1271 | } | |
1272 | ||
1273 | static int | |
1274 | afunc_setup(struct usb_function *fn, const struct usb_ctrlrequest *cr) | |
1275 | { | |
1276 | struct usb_composite_dev *cdev = fn->config->cdev; | |
1277 | struct audio_dev *agdev = func_to_agdev(fn); | |
1278 | struct snd_uac2_chip *uac2 = &agdev->uac2; | |
1279 | struct usb_request *req = cdev->req; | |
1280 | u16 w_length = le16_to_cpu(cr->wLength); | |
1281 | int value = -EOPNOTSUPP; | |
1282 | ||
1283 | /* Only Class specific requests are supposed to reach here */ | |
1284 | if ((cr->bRequestType & USB_TYPE_MASK) != USB_TYPE_CLASS) | |
1285 | return -EOPNOTSUPP; | |
1286 | ||
1287 | if ((cr->bRequestType & USB_RECIP_MASK) == USB_RECIP_INTERFACE) | |
1288 | value = setup_rq_inf(fn, cr); | |
1289 | else | |
1290 | dev_err(&uac2->pdev.dev, "%s:%d Error!\n", __func__, __LINE__); | |
1291 | ||
1292 | if (value >= 0) { | |
1293 | req->length = value; | |
1294 | req->zero = value < w_length; | |
1295 | value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC); | |
1296 | if (value < 0) { | |
1297 | dev_err(&uac2->pdev.dev, | |
1298 | "%s:%d Error!\n", __func__, __LINE__); | |
1299 | req->status = 0; | |
1300 | } | |
1301 | } | |
1302 | ||
1303 | return value; | |
1304 | } | |
1305 | ||
1306 | static int audio_bind_config(struct usb_configuration *cfg) | |
1307 | { | |
1616e99d | 1308 | int res; |
132fcb46 JB |
1309 | |
1310 | agdev_g = kzalloc(sizeof *agdev_g, GFP_KERNEL); | |
1311 | if (agdev_g == NULL) { | |
1312 | printk(KERN_ERR "Unable to allocate audio gadget\n"); | |
1313 | return -ENOMEM; | |
1314 | } | |
1315 | ||
1616e99d SAS |
1316 | res = usb_string_ids_tab(cfg->cdev, strings_fn); |
1317 | if (res) | |
1318 | return res; | |
1319 | iad_desc.iFunction = strings_fn[STR_ASSOC].id; | |
1320 | std_ac_if_desc.iInterface = strings_fn[STR_IF_CTRL].id; | |
1321 | in_clk_src_desc.iClockSource = strings_fn[STR_CLKSRC_IN].id; | |
1322 | out_clk_src_desc.iClockSource = strings_fn[STR_CLKSRC_OUT].id; | |
1323 | usb_out_it_desc.iTerminal = strings_fn[STR_USB_IT].id; | |
1324 | io_in_it_desc.iTerminal = strings_fn[STR_IO_IT].id; | |
1325 | usb_in_ot_desc.iTerminal = strings_fn[STR_USB_OT].id; | |
1326 | io_out_ot_desc.iTerminal = strings_fn[STR_IO_OT].id; | |
1327 | std_as_out_if0_desc.iInterface = strings_fn[STR_AS_OUT_ALT0].id; | |
1328 | std_as_out_if1_desc.iInterface = strings_fn[STR_AS_OUT_ALT1].id; | |
1329 | std_as_in_if0_desc.iInterface = strings_fn[STR_AS_IN_ALT0].id; | |
1330 | std_as_in_if1_desc.iInterface = strings_fn[STR_AS_IN_ALT1].id; | |
132fcb46 JB |
1331 | |
1332 | agdev_g->func.name = "uac2_func"; | |
1333 | agdev_g->func.strings = fn_strings; | |
1334 | agdev_g->func.bind = afunc_bind; | |
1335 | agdev_g->func.unbind = afunc_unbind; | |
1336 | agdev_g->func.set_alt = afunc_set_alt; | |
1337 | agdev_g->func.get_alt = afunc_get_alt; | |
1338 | agdev_g->func.disable = afunc_disable; | |
1339 | agdev_g->func.setup = afunc_setup; | |
1340 | ||
1341 | /* Initialize the configurable parameters */ | |
1342 | usb_out_it_desc.bNrChannels = num_channels(c_chmask); | |
1343 | usb_out_it_desc.bmChannelConfig = cpu_to_le32(c_chmask); | |
1344 | io_in_it_desc.bNrChannels = num_channels(p_chmask); | |
1345 | io_in_it_desc.bmChannelConfig = cpu_to_le32(p_chmask); | |
1346 | as_out_hdr_desc.bNrChannels = num_channels(c_chmask); | |
1347 | as_out_hdr_desc.bmChannelConfig = cpu_to_le32(c_chmask); | |
1348 | as_in_hdr_desc.bNrChannels = num_channels(p_chmask); | |
1349 | as_in_hdr_desc.bmChannelConfig = cpu_to_le32(p_chmask); | |
1350 | as_out_fmt1_desc.bSubslotSize = c_ssize; | |
1351 | as_out_fmt1_desc.bBitResolution = c_ssize * 8; | |
1352 | as_in_fmt1_desc.bSubslotSize = p_ssize; | |
1353 | as_in_fmt1_desc.bBitResolution = p_ssize * 8; | |
1354 | ||
1355 | snprintf(clksrc_in, sizeof(clksrc_in), "%uHz", p_srate); | |
1356 | snprintf(clksrc_out, sizeof(clksrc_out), "%uHz", c_srate); | |
1357 | ||
1358 | res = usb_add_function(cfg, &agdev_g->func); | |
1359 | if (res < 0) | |
1360 | kfree(agdev_g); | |
1361 | ||
1362 | return res; | |
1363 | } | |
1364 | ||
1365 | static void | |
1366 | uac2_unbind_config(struct usb_configuration *cfg) | |
1367 | { | |
1368 | kfree(agdev_g); | |
1369 | agdev_g = NULL; | |
1370 | } |