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