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