.. | .. |
---|
12 | 12 | * Jaswinder Singh (jaswinder.singh@linaro.org) |
---|
13 | 13 | */ |
---|
14 | 14 | |
---|
| 15 | +#include <linux/kernel.h> |
---|
15 | 16 | #include <linux/module.h> |
---|
16 | | -#include <linux/usb/audio.h> |
---|
17 | | -#include <sound/control.h> |
---|
18 | 17 | #include <sound/core.h> |
---|
19 | 18 | #include <sound/pcm.h> |
---|
20 | 19 | #include <sound/pcm_params.h> |
---|
| 20 | +#include <sound/control.h> |
---|
| 21 | +#include <sound/tlv.h> |
---|
| 22 | +#include <linux/usb/audio.h> |
---|
21 | 23 | |
---|
22 | 24 | #include "u_audio.h" |
---|
23 | 25 | |
---|
.. | .. |
---|
25 | 27 | #define PRD_SIZE_MAX PAGE_SIZE |
---|
26 | 28 | #define MIN_PERIODS 4 |
---|
27 | 29 | |
---|
28 | | -#define CLK_PPM_GROUP_SIZE 20 |
---|
29 | | - |
---|
30 | | -static struct class *audio_class; |
---|
31 | | - |
---|
32 | | -struct uac_req { |
---|
33 | | - struct uac_rtd_params *pp; /* parent param */ |
---|
34 | | - struct usb_request *req; |
---|
| 30 | +enum { |
---|
| 31 | + UAC_FBACK_CTRL, |
---|
| 32 | + UAC_P_PITCH_CTRL, |
---|
| 33 | + UAC_MUTE_CTRL, |
---|
| 34 | + UAC_VOLUME_CTRL, |
---|
| 35 | + UAC_RATE_CTRL, |
---|
35 | 36 | }; |
---|
| 37 | + |
---|
| 38 | +#define CLK_PPM_GROUP_SIZE 20 |
---|
36 | 39 | |
---|
37 | 40 | /* Runtime data params for one stream */ |
---|
38 | 41 | struct uac_rtd_params { |
---|
.. | .. |
---|
46 | 49 | |
---|
47 | 50 | void *rbuf; |
---|
48 | 51 | |
---|
49 | | - unsigned max_psize; /* MaxPacketSize of endpoint */ |
---|
50 | | - struct uac_req *ureq; |
---|
| 52 | + unsigned int pitch; /* Stream pitch ratio to 1000000 */ |
---|
| 53 | + unsigned int max_psize; /* MaxPacketSize of endpoint */ |
---|
51 | 54 | |
---|
52 | | - spinlock_t lock; |
---|
| 55 | + struct usb_request **reqs; |
---|
| 56 | + |
---|
| 57 | + struct usb_request *req_fback; /* Feedback endpoint request */ |
---|
| 58 | + bool fb_ep_enabled; /* if the ep is enabled */ |
---|
| 59 | + |
---|
| 60 | + /* Volume/Mute controls and their state */ |
---|
| 61 | + int fu_id; /* Feature Unit ID */ |
---|
| 62 | + struct snd_kcontrol *snd_kctl_volume; |
---|
| 63 | + struct snd_kcontrol *snd_kctl_mute; |
---|
| 64 | + s16 volume_min, volume_max, volume_res; |
---|
| 65 | + s16 volume; |
---|
| 66 | + int mute; |
---|
| 67 | + |
---|
| 68 | + struct snd_kcontrol *snd_kctl_rate; /* read-only current rate */ |
---|
| 69 | + int srate; /* selected samplerate */ |
---|
| 70 | + int active; /* playback/capture running */ |
---|
| 71 | + |
---|
| 72 | + spinlock_t lock; /* lock for control transfers */ |
---|
| 73 | + |
---|
53 | 74 | }; |
---|
54 | 75 | |
---|
55 | 76 | struct snd_uac_chip { |
---|
.. | .. |
---|
61 | 82 | struct snd_card *card; |
---|
62 | 83 | struct snd_pcm *pcm; |
---|
63 | 84 | |
---|
64 | | - /* timekeeping for the playback endpoint */ |
---|
65 | | - unsigned int p_interval; |
---|
66 | | - unsigned int p_residue; |
---|
67 | | - |
---|
68 | 85 | /* pre-calculated values for playback iso completion */ |
---|
69 | | - unsigned int p_pktsize; |
---|
70 | | - unsigned int p_pktsize_residue; |
---|
| 86 | + unsigned long long p_residue_mil; |
---|
| 87 | + unsigned int p_interval; |
---|
71 | 88 | unsigned int p_framesize; |
---|
72 | 89 | }; |
---|
73 | 90 | |
---|
.. | .. |
---|
82 | 99 | .periods_min = MIN_PERIODS, |
---|
83 | 100 | }; |
---|
84 | 101 | |
---|
| 102 | +static struct class *audio_class; |
---|
| 103 | + |
---|
| 104 | +static void u_audio_set_fback_frequency(enum usb_device_speed speed, |
---|
| 105 | + struct usb_ep *out_ep, |
---|
| 106 | + unsigned long long freq, |
---|
| 107 | + unsigned int pitch, |
---|
| 108 | + void *buf) |
---|
| 109 | +{ |
---|
| 110 | + u32 ff = 0; |
---|
| 111 | + const struct usb_endpoint_descriptor *ep_desc; |
---|
| 112 | + |
---|
| 113 | + /* |
---|
| 114 | + * Because the pitch base is 1000000, the final divider here |
---|
| 115 | + * will be 1000 * 1000000 = 1953125 << 9 |
---|
| 116 | + * |
---|
| 117 | + * Instead of dealing with big numbers lets fold this 9 left shift |
---|
| 118 | + */ |
---|
| 119 | + |
---|
| 120 | + if (speed == USB_SPEED_FULL) { |
---|
| 121 | + /* |
---|
| 122 | + * Full-speed feedback endpoints report frequency |
---|
| 123 | + * in samples/frame |
---|
| 124 | + * Format is encoded in Q10.10 left-justified in the 24 bits, |
---|
| 125 | + * so that it has a Q10.14 format. |
---|
| 126 | + * |
---|
| 127 | + * ff = (freq << 14) / 1000 |
---|
| 128 | + */ |
---|
| 129 | + freq <<= 5; |
---|
| 130 | + } else { |
---|
| 131 | + /* |
---|
| 132 | + * High-speed feedback endpoints report frequency |
---|
| 133 | + * in samples/microframe. |
---|
| 134 | + * Format is encoded in Q12.13 fitted into four bytes so that |
---|
| 135 | + * the binary point is located between the second and the third |
---|
| 136 | + * byte fromat (that is Q16.16) |
---|
| 137 | + * |
---|
| 138 | + * ff = (freq << 16) / 8000 |
---|
| 139 | + * |
---|
| 140 | + * Win10 and OSX UAC2 drivers require number of samples per packet |
---|
| 141 | + * in order to honor the feedback value. |
---|
| 142 | + * Linux snd-usb-audio detects the applied bit-shift automatically. |
---|
| 143 | + */ |
---|
| 144 | + ep_desc = out_ep->desc; |
---|
| 145 | + freq <<= 4 + (ep_desc->bInterval - 1); |
---|
| 146 | + } |
---|
| 147 | + |
---|
| 148 | + ff = DIV_ROUND_CLOSEST_ULL((freq * pitch), 1953125); |
---|
| 149 | + |
---|
| 150 | + *(__le32 *)buf = cpu_to_le32(ff); |
---|
| 151 | +} |
---|
| 152 | + |
---|
85 | 153 | static void u_audio_iso_complete(struct usb_ep *ep, struct usb_request *req) |
---|
86 | 154 | { |
---|
87 | | - unsigned pending; |
---|
88 | | - unsigned long flags, flags2; |
---|
| 155 | + unsigned int pending; |
---|
89 | 156 | unsigned int hw_ptr; |
---|
90 | 157 | int status = req->status; |
---|
91 | | - struct uac_req *ur = req->context; |
---|
92 | 158 | struct snd_pcm_substream *substream; |
---|
93 | 159 | struct snd_pcm_runtime *runtime; |
---|
94 | | - struct uac_rtd_params *prm = ur->pp; |
---|
| 160 | + struct uac_rtd_params *prm = req->context; |
---|
95 | 161 | struct snd_uac_chip *uac = prm->uac; |
---|
| 162 | + unsigned int frames, p_pktsize; |
---|
| 163 | + unsigned long long pitched_rate_mil, p_pktsize_residue_mil, |
---|
| 164 | + residue_frames_mil, div_result; |
---|
96 | 165 | |
---|
97 | 166 | /* i/f shutting down */ |
---|
98 | 167 | if (!prm->ep_enabled) { |
---|
.. | .. |
---|
117 | 186 | if (!substream) |
---|
118 | 187 | goto exit; |
---|
119 | 188 | |
---|
120 | | - snd_pcm_stream_lock_irqsave(substream, flags2); |
---|
| 189 | + snd_pcm_stream_lock(substream); |
---|
121 | 190 | |
---|
122 | 191 | runtime = substream->runtime; |
---|
123 | 192 | if (!runtime || !snd_pcm_running(substream)) { |
---|
124 | | - snd_pcm_stream_unlock_irqrestore(substream, flags2); |
---|
| 193 | + snd_pcm_stream_unlock(substream); |
---|
125 | 194 | goto exit; |
---|
126 | 195 | } |
---|
127 | | - |
---|
128 | | - spin_lock_irqsave(&prm->lock, flags); |
---|
129 | 196 | |
---|
130 | 197 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { |
---|
131 | 198 | /* |
---|
.. | .. |
---|
134 | 201 | * If there is a residue from this division, add it to the |
---|
135 | 202 | * residue accumulator. |
---|
136 | 203 | */ |
---|
137 | | - req->length = uac->p_pktsize; |
---|
138 | | - uac->p_residue += uac->p_pktsize_residue; |
---|
| 204 | + unsigned long long p_interval_mil = uac->p_interval * 1000000ULL; |
---|
| 205 | + |
---|
| 206 | + pitched_rate_mil = (unsigned long long) prm->srate * prm->pitch; |
---|
| 207 | + div_result = pitched_rate_mil; |
---|
| 208 | + do_div(div_result, uac->p_interval); |
---|
| 209 | + do_div(div_result, 1000000); |
---|
| 210 | + frames = (unsigned int) div_result; |
---|
| 211 | + |
---|
| 212 | + pr_debug("p_srate %d, pitch %d, interval_mil %llu, frames %d\n", |
---|
| 213 | + prm->srate, prm->pitch, p_interval_mil, frames); |
---|
| 214 | + |
---|
| 215 | + p_pktsize = min_t(unsigned int, |
---|
| 216 | + uac->p_framesize * frames, |
---|
| 217 | + ep->maxpacket); |
---|
| 218 | + |
---|
| 219 | + if (p_pktsize < ep->maxpacket) { |
---|
| 220 | + residue_frames_mil = pitched_rate_mil - frames * p_interval_mil; |
---|
| 221 | + p_pktsize_residue_mil = uac->p_framesize * residue_frames_mil; |
---|
| 222 | + } else |
---|
| 223 | + p_pktsize_residue_mil = 0; |
---|
| 224 | + |
---|
| 225 | + req->length = p_pktsize; |
---|
| 226 | + uac->p_residue_mil += p_pktsize_residue_mil; |
---|
139 | 227 | |
---|
140 | 228 | /* |
---|
141 | | - * Whenever there are more bytes in the accumulator than we |
---|
| 229 | + * Whenever there are more bytes in the accumulator p_residue_mil than we |
---|
142 | 230 | * need to add one more sample frame, increase this packet's |
---|
143 | 231 | * size and decrease the accumulator. |
---|
144 | 232 | */ |
---|
145 | | - if (uac->p_residue / uac->p_interval >= uac->p_framesize) { |
---|
| 233 | + div_result = uac->p_residue_mil; |
---|
| 234 | + do_div(div_result, uac->p_interval); |
---|
| 235 | + do_div(div_result, 1000000); |
---|
| 236 | + if ((unsigned int) div_result >= uac->p_framesize) { |
---|
146 | 237 | req->length += uac->p_framesize; |
---|
147 | | - uac->p_residue -= uac->p_framesize * |
---|
148 | | - uac->p_interval; |
---|
| 238 | + uac->p_residue_mil -= uac->p_framesize * p_interval_mil; |
---|
| 239 | + pr_debug("increased req length to %d\n", req->length); |
---|
149 | 240 | } |
---|
| 241 | + pr_debug("remains uac->p_residue_mil %llu\n", uac->p_residue_mil); |
---|
150 | 242 | |
---|
151 | 243 | req->actual = req->length; |
---|
152 | 244 | } |
---|
153 | 245 | |
---|
154 | 246 | hw_ptr = prm->hw_ptr; |
---|
155 | | - |
---|
156 | | - spin_unlock_irqrestore(&prm->lock, flags); |
---|
157 | 247 | |
---|
158 | 248 | /* Pack USB load in ALSA ring buffer */ |
---|
159 | 249 | pending = runtime->dma_bytes - hw_ptr; |
---|
.. | .. |
---|
178 | 268 | } |
---|
179 | 269 | } |
---|
180 | 270 | |
---|
181 | | - spin_lock_irqsave(&prm->lock, flags); |
---|
182 | 271 | /* update hw_ptr after data is copied to memory */ |
---|
183 | 272 | prm->hw_ptr = (hw_ptr + req->actual) % runtime->dma_bytes; |
---|
184 | 273 | hw_ptr = prm->hw_ptr; |
---|
185 | | - spin_unlock_irqrestore(&prm->lock, flags); |
---|
186 | | - snd_pcm_stream_unlock_irqrestore(substream, flags2); |
---|
| 274 | + snd_pcm_stream_unlock(substream); |
---|
187 | 275 | |
---|
188 | 276 | if ((hw_ptr % snd_pcm_lib_period_bytes(substream)) < req->actual) |
---|
189 | 277 | snd_pcm_period_elapsed(substream); |
---|
190 | 278 | |
---|
191 | 279 | exit: |
---|
| 280 | + if (usb_ep_queue(ep, req, GFP_ATOMIC)) |
---|
| 281 | + dev_err(uac->card->dev, "%d Error!\n", __LINE__); |
---|
| 282 | +} |
---|
| 283 | + |
---|
| 284 | +static void u_audio_iso_fback_complete(struct usb_ep *ep, |
---|
| 285 | + struct usb_request *req) |
---|
| 286 | +{ |
---|
| 287 | + struct uac_rtd_params *prm = req->context; |
---|
| 288 | + struct snd_uac_chip *uac = prm->uac; |
---|
| 289 | + struct g_audio *audio_dev = uac->audio_dev; |
---|
| 290 | + int status = req->status; |
---|
| 291 | + |
---|
| 292 | + /* i/f shutting down */ |
---|
| 293 | + if (!prm->fb_ep_enabled) { |
---|
| 294 | + kfree(req->buf); |
---|
| 295 | + usb_ep_free_request(ep, req); |
---|
| 296 | + return; |
---|
| 297 | + } |
---|
| 298 | + |
---|
| 299 | + if (req->status == -ESHUTDOWN) |
---|
| 300 | + return; |
---|
| 301 | + |
---|
| 302 | + /* |
---|
| 303 | + * We can't really do much about bad xfers. |
---|
| 304 | + * Afterall, the ISOCH xfers could fail legitimately. |
---|
| 305 | + */ |
---|
| 306 | + if (status) |
---|
| 307 | + pr_debug("%s: iso_complete status(%d) %d/%d\n", |
---|
| 308 | + __func__, status, req->actual, req->length); |
---|
| 309 | + |
---|
| 310 | + u_audio_set_fback_frequency(audio_dev->gadget->speed, audio_dev->out_ep, |
---|
| 311 | + prm->srate, prm->pitch, |
---|
| 312 | + req->buf); |
---|
| 313 | + |
---|
192 | 314 | if (usb_ep_queue(ep, req, GFP_ATOMIC)) |
---|
193 | 315 | dev_err(uac->card->dev, "%d Error!\n", __LINE__); |
---|
194 | 316 | } |
---|
.. | .. |
---|
199 | 321 | struct uac_rtd_params *prm; |
---|
200 | 322 | struct g_audio *audio_dev; |
---|
201 | 323 | struct uac_params *params; |
---|
202 | | - unsigned long flags; |
---|
203 | 324 | int err = 0; |
---|
204 | 325 | |
---|
205 | 326 | audio_dev = uac->audio_dev; |
---|
.. | .. |
---|
209 | 330 | prm = &uac->p_prm; |
---|
210 | 331 | else |
---|
211 | 332 | prm = &uac->c_prm; |
---|
212 | | - |
---|
213 | | - spin_lock_irqsave(&prm->lock, flags); |
---|
214 | 333 | |
---|
215 | 334 | /* Reset */ |
---|
216 | 335 | prm->hw_ptr = 0; |
---|
.. | .. |
---|
227 | 346 | default: |
---|
228 | 347 | err = -EINVAL; |
---|
229 | 348 | } |
---|
230 | | - |
---|
231 | | - spin_unlock_irqrestore(&prm->lock, flags); |
---|
232 | 349 | |
---|
233 | 350 | /* Clear buffer after Play stops */ |
---|
234 | 351 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK && !prm->ss) |
---|
.. | .. |
---|
250 | 367 | return bytes_to_frames(substream->runtime, prm->hw_ptr); |
---|
251 | 368 | } |
---|
252 | 369 | |
---|
253 | | -static int uac_pcm_hw_params(struct snd_pcm_substream *substream, |
---|
254 | | - struct snd_pcm_hw_params *hw_params) |
---|
| 370 | +static u64 uac_ssize_to_fmt(int ssize) |
---|
255 | 371 | { |
---|
256 | | - return snd_pcm_lib_malloc_pages(substream, |
---|
257 | | - params_buffer_bytes(hw_params)); |
---|
258 | | -} |
---|
| 372 | + u64 ret; |
---|
259 | 373 | |
---|
260 | | -static int uac_pcm_hw_free(struct snd_pcm_substream *substream) |
---|
261 | | -{ |
---|
262 | | - return snd_pcm_lib_free_pages(substream); |
---|
| 374 | + switch (ssize) { |
---|
| 375 | + case 3: |
---|
| 376 | + ret = SNDRV_PCM_FMTBIT_S24_3LE; |
---|
| 377 | + break; |
---|
| 378 | + case 4: |
---|
| 379 | + ret = SNDRV_PCM_FMTBIT_S32_LE; |
---|
| 380 | + break; |
---|
| 381 | + default: |
---|
| 382 | + ret = SNDRV_PCM_FMTBIT_S16_LE; |
---|
| 383 | + break; |
---|
| 384 | + } |
---|
| 385 | + |
---|
| 386 | + return ret; |
---|
263 | 387 | } |
---|
264 | 388 | |
---|
265 | 389 | static int uac_pcm_open(struct snd_pcm_substream *substream) |
---|
266 | 390 | { |
---|
267 | 391 | struct snd_uac_chip *uac = snd_pcm_substream_chip(substream); |
---|
268 | 392 | struct snd_pcm_runtime *runtime = substream->runtime; |
---|
269 | | - struct g_audio *audio_dev = uac->audio_dev; |
---|
| 393 | + struct g_audio *audio_dev; |
---|
270 | 394 | struct uac_params *params; |
---|
| 395 | + struct uac_rtd_params *prm; |
---|
271 | 396 | int p_ssize, c_ssize; |
---|
272 | | - int p_srate, c_srate; |
---|
273 | 397 | int p_chmask, c_chmask; |
---|
274 | 398 | |
---|
| 399 | + audio_dev = uac->audio_dev; |
---|
275 | 400 | params = &audio_dev->params; |
---|
276 | 401 | p_ssize = params->p_ssize; |
---|
277 | 402 | c_ssize = params->c_ssize; |
---|
278 | | - p_srate = params->p_srate_active; |
---|
279 | | - c_srate = params->c_srate_active; |
---|
280 | 403 | p_chmask = params->p_chmask; |
---|
281 | 404 | c_chmask = params->c_chmask; |
---|
282 | | - uac->p_residue = 0; |
---|
| 405 | + uac->p_residue_mil = 0; |
---|
283 | 406 | |
---|
284 | 407 | runtime->hw = uac_pcm_hardware; |
---|
285 | 408 | |
---|
286 | 409 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { |
---|
287 | | - spin_lock_init(&uac->p_prm.lock); |
---|
288 | | - runtime->hw.rate_min = p_srate; |
---|
289 | | - switch (p_ssize) { |
---|
290 | | - case 3: |
---|
291 | | - runtime->hw.formats = SNDRV_PCM_FMTBIT_S24_3LE; |
---|
292 | | - break; |
---|
293 | | - case 4: |
---|
294 | | - runtime->hw.formats = SNDRV_PCM_FMTBIT_S32_LE; |
---|
295 | | - break; |
---|
296 | | - default: |
---|
297 | | - runtime->hw.formats = SNDRV_PCM_FMTBIT_S16_LE; |
---|
298 | | - break; |
---|
299 | | - } |
---|
| 410 | + runtime->hw.formats = uac_ssize_to_fmt(p_ssize); |
---|
300 | 411 | runtime->hw.channels_min = num_channels(p_chmask); |
---|
301 | | - runtime->hw.period_bytes_min = 2 * uac->p_prm.max_psize |
---|
302 | | - / runtime->hw.periods_min; |
---|
| 412 | + prm = &uac->p_prm; |
---|
303 | 413 | } else { |
---|
304 | | - spin_lock_init(&uac->c_prm.lock); |
---|
305 | | - runtime->hw.rate_min = c_srate; |
---|
306 | | - switch (c_ssize) { |
---|
307 | | - case 3: |
---|
308 | | - runtime->hw.formats = SNDRV_PCM_FMTBIT_S24_3LE; |
---|
309 | | - break; |
---|
310 | | - case 4: |
---|
311 | | - runtime->hw.formats = SNDRV_PCM_FMTBIT_S32_LE; |
---|
312 | | - break; |
---|
313 | | - default: |
---|
314 | | - runtime->hw.formats = SNDRV_PCM_FMTBIT_S16_LE; |
---|
315 | | - break; |
---|
316 | | - } |
---|
| 414 | + runtime->hw.formats = uac_ssize_to_fmt(c_ssize); |
---|
317 | 415 | runtime->hw.channels_min = num_channels(c_chmask); |
---|
318 | | - runtime->hw.period_bytes_min = 2 * uac->c_prm.max_psize |
---|
319 | | - / runtime->hw.periods_min; |
---|
| 416 | + prm = &uac->c_prm; |
---|
320 | 417 | } |
---|
321 | 418 | |
---|
| 419 | + runtime->hw.period_bytes_min = 2 * prm->max_psize |
---|
| 420 | + / runtime->hw.periods_min; |
---|
| 421 | + runtime->hw.rate_min = prm->srate; |
---|
322 | 422 | runtime->hw.rate_max = runtime->hw.rate_min; |
---|
323 | 423 | runtime->hw.channels_max = runtime->hw.channels_min; |
---|
324 | 424 | |
---|
.. | .. |
---|
326 | 426 | |
---|
327 | 427 | return 0; |
---|
328 | 428 | } |
---|
329 | | - |
---|
330 | | -static int uac_pcm_rate_info(struct snd_kcontrol *kcontrol, |
---|
331 | | - struct snd_ctl_elem_info *uinfo) |
---|
332 | | -{ |
---|
333 | | - uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; |
---|
334 | | - uinfo->count = 1; |
---|
335 | | - uinfo->value.integer.min = 0; |
---|
336 | | - uinfo->value.integer.max = 324000; |
---|
337 | | - return 0; |
---|
338 | | -} |
---|
339 | | - |
---|
340 | | -static int uac_pcm_rate_get(struct snd_kcontrol *kcontrol, |
---|
341 | | - struct snd_ctl_elem_value *ucontrol) |
---|
342 | | -{ |
---|
343 | | - struct snd_uac_chip *uac = snd_kcontrol_chip(kcontrol); |
---|
344 | | - struct g_audio *audio_dev = uac->audio_dev; |
---|
345 | | - struct uac_params *params = &audio_dev->params; |
---|
346 | | - |
---|
347 | | - if (kcontrol->private_value == SNDRV_PCM_STREAM_CAPTURE) |
---|
348 | | - ucontrol->value.integer.value[0] = params->c_srate_active; |
---|
349 | | - else if (kcontrol->private_value == SNDRV_PCM_STREAM_PLAYBACK) |
---|
350 | | - ucontrol->value.integer.value[0] = params->p_srate_active; |
---|
351 | | - else |
---|
352 | | - return -EINVAL; |
---|
353 | | - |
---|
354 | | - return 0; |
---|
355 | | -} |
---|
356 | | - |
---|
357 | | -static struct snd_kcontrol_new uac_pcm_controls[] = { |
---|
358 | | -{ |
---|
359 | | - .iface = SNDRV_CTL_ELEM_IFACE_PCM, |
---|
360 | | - .name = "Capture Rate", |
---|
361 | | - .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, |
---|
362 | | - .info = uac_pcm_rate_info, |
---|
363 | | - .get = uac_pcm_rate_get, |
---|
364 | | - .private_value = SNDRV_PCM_STREAM_CAPTURE, |
---|
365 | | -}, |
---|
366 | | -{ |
---|
367 | | - .iface = SNDRV_CTL_ELEM_IFACE_PCM, |
---|
368 | | - .name = "Playback Rate", |
---|
369 | | - .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, |
---|
370 | | - .info = uac_pcm_rate_info, |
---|
371 | | - .get = uac_pcm_rate_get, |
---|
372 | | - .private_value = SNDRV_PCM_STREAM_PLAYBACK, |
---|
373 | | -}, |
---|
374 | | -}; |
---|
375 | 429 | |
---|
376 | 430 | /* ALSA cries without these function pointers */ |
---|
377 | 431 | static int uac_pcm_null(struct snd_pcm_substream *substream) |
---|
.. | .. |
---|
382 | 436 | static const struct snd_pcm_ops uac_pcm_ops = { |
---|
383 | 437 | .open = uac_pcm_open, |
---|
384 | 438 | .close = uac_pcm_null, |
---|
385 | | - .ioctl = snd_pcm_lib_ioctl, |
---|
386 | | - .hw_params = uac_pcm_hw_params, |
---|
387 | | - .hw_free = uac_pcm_hw_free, |
---|
388 | 439 | .trigger = uac_pcm_trigger, |
---|
389 | 440 | .pointer = uac_pcm_pointer, |
---|
390 | 441 | .prepare = uac_pcm_null, |
---|
.. | .. |
---|
404 | 455 | params = &audio_dev->params; |
---|
405 | 456 | |
---|
406 | 457 | for (i = 0; i < params->req_number; i++) { |
---|
407 | | - if (prm->ureq[i].req) { |
---|
408 | | - if (usb_ep_dequeue(ep, prm->ureq[i].req)) |
---|
409 | | - usb_ep_free_request(ep, prm->ureq[i].req); |
---|
| 458 | + if (prm->reqs[i]) { |
---|
| 459 | + if (usb_ep_dequeue(ep, prm->reqs[i])) |
---|
| 460 | + usb_ep_free_request(ep, prm->reqs[i]); |
---|
410 | 461 | /* |
---|
411 | 462 | * If usb_ep_dequeue() cannot successfully dequeue the |
---|
412 | 463 | * request, the request will be freed by the completion |
---|
413 | 464 | * callback. |
---|
414 | 465 | */ |
---|
415 | 466 | |
---|
416 | | - prm->ureq[i].req = NULL; |
---|
| 467 | + prm->reqs[i] = NULL; |
---|
417 | 468 | } |
---|
418 | 469 | } |
---|
419 | 470 | |
---|
.. | .. |
---|
423 | 474 | dev_err(uac->card->dev, "%s:%d Error!\n", __func__, __LINE__); |
---|
424 | 475 | } |
---|
425 | 476 | |
---|
426 | | -static struct snd_kcontrol *u_audio_get_ctl(struct g_audio *audio_dev, |
---|
427 | | - const char *name) |
---|
| 477 | +static inline void free_ep_fback(struct uac_rtd_params *prm, struct usb_ep *ep) |
---|
428 | 478 | { |
---|
429 | | - struct snd_ctl_elem_id elem_id; |
---|
| 479 | + struct snd_uac_chip *uac = prm->uac; |
---|
430 | 480 | |
---|
431 | | - memset(&elem_id, 0, sizeof(elem_id)); |
---|
432 | | - elem_id.iface = SNDRV_CTL_ELEM_IFACE_PCM; |
---|
433 | | - strlcpy(elem_id.name, name, sizeof(elem_id.name)); |
---|
434 | | - return snd_ctl_find_id(audio_dev->uac->card, &elem_id); |
---|
| 481 | + if (!prm->fb_ep_enabled) |
---|
| 482 | + return; |
---|
| 483 | + |
---|
| 484 | + prm->fb_ep_enabled = false; |
---|
| 485 | + |
---|
| 486 | + if (prm->req_fback) { |
---|
| 487 | + if (usb_ep_dequeue(ep, prm->req_fback)) { |
---|
| 488 | + kfree(prm->req_fback->buf); |
---|
| 489 | + usb_ep_free_request(ep, prm->req_fback); |
---|
| 490 | + } |
---|
| 491 | + prm->req_fback = NULL; |
---|
| 492 | + } |
---|
| 493 | + |
---|
| 494 | + if (usb_ep_disable(ep)) |
---|
| 495 | + dev_err(uac->card->dev, "%s:%d Error!\n", __func__, __LINE__); |
---|
| 496 | +} |
---|
| 497 | + |
---|
| 498 | +static void set_active(struct uac_rtd_params *prm, bool active) |
---|
| 499 | +{ |
---|
| 500 | + // notifying through the Rate ctrl |
---|
| 501 | + struct snd_kcontrol *kctl = prm->snd_kctl_rate; |
---|
| 502 | + unsigned long flags; |
---|
| 503 | + |
---|
| 504 | + spin_lock_irqsave(&prm->lock, flags); |
---|
| 505 | + if (prm->active != active) { |
---|
| 506 | + prm->active = active; |
---|
| 507 | + snd_ctl_notify(prm->uac->card, SNDRV_CTL_EVENT_MASK_VALUE, |
---|
| 508 | + &kctl->id); |
---|
| 509 | + } |
---|
| 510 | + spin_unlock_irqrestore(&prm->lock, flags); |
---|
435 | 511 | } |
---|
436 | 512 | |
---|
437 | 513 | int u_audio_set_capture_srate(struct g_audio *audio_dev, int srate) |
---|
438 | 514 | { |
---|
439 | | - struct snd_kcontrol *ctl = u_audio_get_ctl(audio_dev, "Capture Rate"); |
---|
440 | 515 | struct uac_params *params = &audio_dev->params; |
---|
| 516 | + struct snd_uac_chip *uac = audio_dev->uac; |
---|
| 517 | + struct uac_rtd_params *prm; |
---|
441 | 518 | int i; |
---|
| 519 | + unsigned long flags; |
---|
442 | 520 | |
---|
| 521 | + dev_dbg(&audio_dev->gadget->dev, "%s: srate %d\n", __func__, srate); |
---|
| 522 | + prm = &uac->c_prm; |
---|
443 | 523 | for (i = 0; i < UAC_MAX_RATES; i++) { |
---|
444 | | - if (params->c_srate[i] == srate) { |
---|
| 524 | + if (params->c_srates[i] == srate) { |
---|
| 525 | + spin_lock_irqsave(&prm->lock, flags); |
---|
| 526 | + prm->srate = srate; |
---|
445 | 527 | audio_dev->usb_state[SET_SAMPLE_RATE_OUT] = true; |
---|
446 | 528 | schedule_work(&audio_dev->work); |
---|
447 | | - |
---|
448 | | - params->c_srate_active = srate; |
---|
449 | | - snd_ctl_notify(audio_dev->uac->card, |
---|
450 | | - SNDRV_CTL_EVENT_MASK_VALUE, &ctl->id); |
---|
| 529 | + spin_unlock_irqrestore(&prm->lock, flags); |
---|
451 | 530 | return 0; |
---|
452 | 531 | } |
---|
453 | | - if (params->c_srate[i] == 0) |
---|
| 532 | + if (params->c_srates[i] == 0) |
---|
454 | 533 | break; |
---|
455 | 534 | } |
---|
456 | 535 | |
---|
.. | .. |
---|
458 | 537 | } |
---|
459 | 538 | EXPORT_SYMBOL_GPL(u_audio_set_capture_srate); |
---|
460 | 539 | |
---|
461 | | -static void u_audio_set_playback_pktsize(struct g_audio *audio_dev, int srate) |
---|
| 540 | +int u_audio_get_capture_srate(struct g_audio *audio_dev, u32 *val) |
---|
462 | 541 | { |
---|
463 | | - struct uac_params *params = &audio_dev->params; |
---|
464 | 542 | struct snd_uac_chip *uac = audio_dev->uac; |
---|
465 | | - struct usb_gadget *gadget = audio_dev->gadget; |
---|
466 | | - const struct usb_endpoint_descriptor *ep_desc; |
---|
467 | 543 | struct uac_rtd_params *prm; |
---|
468 | | - unsigned int factor; |
---|
| 544 | + unsigned long flags; |
---|
469 | 545 | |
---|
470 | | - prm = &uac->p_prm; |
---|
471 | | - /* set srate before starting playback, epin is not configured */ |
---|
472 | | - if (!prm->ep_enabled) |
---|
473 | | - return; |
---|
474 | | - |
---|
475 | | - ep_desc = audio_dev->in_ep->desc; |
---|
476 | | - |
---|
477 | | - /* pre-calculate the playback endpoint's interval */ |
---|
478 | | - if (gadget->speed == USB_SPEED_FULL) |
---|
479 | | - factor = 1000; |
---|
480 | | - else |
---|
481 | | - factor = 8000; |
---|
482 | | - |
---|
483 | | - /* pre-compute some values for iso_complete() */ |
---|
484 | | - uac->p_framesize = params->p_ssize * |
---|
485 | | - num_channels(params->p_chmask); |
---|
486 | | - uac->p_interval = factor / (1 << (ep_desc->bInterval - 1)); |
---|
487 | | - uac->p_pktsize = min_t(unsigned int, |
---|
488 | | - uac->p_framesize * |
---|
489 | | - (params->p_srate_active / uac->p_interval), |
---|
490 | | - prm->max_psize); |
---|
491 | | - |
---|
492 | | - if (uac->p_pktsize < prm->max_psize) |
---|
493 | | - uac->p_pktsize_residue = uac->p_framesize * |
---|
494 | | - (params->p_srate_active % uac->p_interval); |
---|
495 | | - else |
---|
496 | | - uac->p_pktsize_residue = 0; |
---|
| 546 | + prm = &uac->c_prm; |
---|
| 547 | + spin_lock_irqsave(&prm->lock, flags); |
---|
| 548 | + *val = prm->srate; |
---|
| 549 | + spin_unlock_irqrestore(&prm->lock, flags); |
---|
| 550 | + return 0; |
---|
497 | 551 | } |
---|
| 552 | +EXPORT_SYMBOL_GPL(u_audio_get_capture_srate); |
---|
498 | 553 | |
---|
499 | 554 | int u_audio_set_playback_srate(struct g_audio *audio_dev, int srate) |
---|
500 | 555 | { |
---|
501 | | - struct snd_kcontrol *ctl = u_audio_get_ctl(audio_dev, "Playback Rate"); |
---|
502 | 556 | struct uac_params *params = &audio_dev->params; |
---|
| 557 | + struct snd_uac_chip *uac = audio_dev->uac; |
---|
| 558 | + struct uac_rtd_params *prm; |
---|
503 | 559 | int i; |
---|
| 560 | + unsigned long flags; |
---|
504 | 561 | |
---|
| 562 | + dev_dbg(&audio_dev->gadget->dev, "%s: srate %d\n", __func__, srate); |
---|
| 563 | + prm = &uac->p_prm; |
---|
505 | 564 | for (i = 0; i < UAC_MAX_RATES; i++) { |
---|
506 | | - if (params->p_srate[i] == srate) { |
---|
| 565 | + if (params->p_srates[i] == srate) { |
---|
| 566 | + spin_lock_irqsave(&prm->lock, flags); |
---|
| 567 | + prm->srate = srate; |
---|
507 | 568 | audio_dev->usb_state[SET_SAMPLE_RATE_IN] = true; |
---|
508 | 569 | schedule_work(&audio_dev->work); |
---|
509 | | - |
---|
510 | | - params->p_srate_active = srate; |
---|
511 | | - u_audio_set_playback_pktsize(audio_dev, srate); |
---|
512 | | - snd_ctl_notify(audio_dev->uac->card, |
---|
513 | | - SNDRV_CTL_EVENT_MASK_VALUE, &ctl->id); |
---|
| 570 | + spin_unlock_irqrestore(&prm->lock, flags); |
---|
514 | 571 | return 0; |
---|
515 | 572 | } |
---|
516 | | - if (params->p_srate[i] == 0) |
---|
| 573 | + if (params->p_srates[i] == 0) |
---|
517 | 574 | break; |
---|
518 | 575 | } |
---|
519 | 576 | |
---|
.. | .. |
---|
521 | 578 | } |
---|
522 | 579 | EXPORT_SYMBOL_GPL(u_audio_set_playback_srate); |
---|
523 | 580 | |
---|
| 581 | +int u_audio_get_playback_srate(struct g_audio *audio_dev, u32 *val) |
---|
| 582 | +{ |
---|
| 583 | + struct snd_uac_chip *uac = audio_dev->uac; |
---|
| 584 | + struct uac_rtd_params *prm; |
---|
| 585 | + unsigned long flags; |
---|
| 586 | + |
---|
| 587 | + prm = &uac->p_prm; |
---|
| 588 | + spin_lock_irqsave(&prm->lock, flags); |
---|
| 589 | + *val = prm->srate; |
---|
| 590 | + spin_unlock_irqrestore(&prm->lock, flags); |
---|
| 591 | + return 0; |
---|
| 592 | +} |
---|
| 593 | +EXPORT_SYMBOL_GPL(u_audio_get_playback_srate); |
---|
| 594 | + |
---|
524 | 595 | int u_audio_start_capture(struct g_audio *audio_dev) |
---|
525 | 596 | { |
---|
526 | 597 | struct snd_uac_chip *uac = audio_dev->uac; |
---|
527 | 598 | struct usb_gadget *gadget = audio_dev->gadget; |
---|
528 | 599 | struct device *dev = &gadget->dev; |
---|
529 | | - struct usb_request *req; |
---|
530 | | - struct usb_ep *ep; |
---|
| 600 | + struct usb_request *req, *req_fback; |
---|
| 601 | + struct usb_ep *ep, *ep_fback; |
---|
531 | 602 | struct uac_rtd_params *prm; |
---|
532 | 603 | struct uac_params *params = &audio_dev->params; |
---|
533 | 604 | int req_len, i; |
---|
.. | .. |
---|
545 | 616 | audio_dev->stream_state[STATE_OUT] = true; |
---|
546 | 617 | schedule_work(&audio_dev->work); |
---|
547 | 618 | |
---|
548 | | - ep = audio_dev->out_ep; |
---|
549 | 619 | prm = &uac->c_prm; |
---|
| 620 | + dev_dbg(dev, "start capture with rate %d\n", prm->srate); |
---|
| 621 | + ep = audio_dev->out_ep; |
---|
550 | 622 | config_ep_by_speed(gadget, &audio_dev->func, ep); |
---|
551 | | - req_len = prm->max_psize; |
---|
| 623 | + req_len = ep->maxpacket; |
---|
552 | 624 | |
---|
553 | 625 | prm->ep_enabled = true; |
---|
554 | 626 | usb_ep_enable(ep); |
---|
555 | 627 | |
---|
556 | 628 | for (i = 0; i < params->req_number; i++) { |
---|
557 | | - if (!prm->ureq[i].req) { |
---|
| 629 | + if (!prm->reqs[i]) { |
---|
558 | 630 | req = usb_ep_alloc_request(ep, GFP_ATOMIC); |
---|
559 | 631 | if (req == NULL) |
---|
560 | 632 | return -ENOMEM; |
---|
561 | 633 | |
---|
562 | | - prm->ureq[i].req = req; |
---|
563 | | - prm->ureq[i].pp = prm; |
---|
| 634 | + prm->reqs[i] = req; |
---|
564 | 635 | |
---|
565 | 636 | req->zero = 0; |
---|
566 | | - req->context = &prm->ureq[i]; |
---|
| 637 | + req->context = prm; |
---|
567 | 638 | req->length = req_len; |
---|
568 | 639 | req->complete = u_audio_iso_complete; |
---|
569 | | - req->buf = prm->rbuf + i * prm->max_psize; |
---|
| 640 | + req->buf = prm->rbuf + i * ep->maxpacket; |
---|
570 | 641 | } |
---|
571 | 642 | |
---|
572 | | - if (usb_ep_queue(ep, prm->ureq[i].req, GFP_ATOMIC)) |
---|
| 643 | + if (usb_ep_queue(ep, prm->reqs[i], GFP_ATOMIC)) |
---|
573 | 644 | dev_err(dev, "%s:%d Error!\n", __func__, __LINE__); |
---|
574 | 645 | } |
---|
| 646 | + |
---|
| 647 | + set_active(&uac->c_prm, true); |
---|
| 648 | + |
---|
| 649 | + ep_fback = audio_dev->in_ep_fback; |
---|
| 650 | + if (!ep_fback) |
---|
| 651 | + return 0; |
---|
| 652 | + |
---|
| 653 | + /* Setup feedback endpoint */ |
---|
| 654 | + config_ep_by_speed(gadget, &audio_dev->func, ep_fback); |
---|
| 655 | + prm->fb_ep_enabled = true; |
---|
| 656 | + usb_ep_enable(ep_fback); |
---|
| 657 | + req_len = ep_fback->maxpacket; |
---|
| 658 | + |
---|
| 659 | + req_fback = usb_ep_alloc_request(ep_fback, GFP_ATOMIC); |
---|
| 660 | + if (req_fback == NULL) |
---|
| 661 | + return -ENOMEM; |
---|
| 662 | + |
---|
| 663 | + prm->req_fback = req_fback; |
---|
| 664 | + req_fback->zero = 0; |
---|
| 665 | + req_fback->context = prm; |
---|
| 666 | + req_fback->length = req_len; |
---|
| 667 | + req_fback->complete = u_audio_iso_fback_complete; |
---|
| 668 | + |
---|
| 669 | + req_fback->buf = kzalloc(req_len, GFP_ATOMIC); |
---|
| 670 | + if (!req_fback->buf) |
---|
| 671 | + return -ENOMEM; |
---|
| 672 | + |
---|
| 673 | + /* |
---|
| 674 | + * Configure the feedback endpoint's reported frequency. |
---|
| 675 | + * Always start with original frequency since its deviation can't |
---|
| 676 | + * be meauserd at start of playback |
---|
| 677 | + */ |
---|
| 678 | + prm->pitch = 1000000; |
---|
| 679 | + u_audio_set_fback_frequency(audio_dev->gadget->speed, ep, |
---|
| 680 | + prm->srate, prm->pitch, |
---|
| 681 | + req_fback->buf); |
---|
| 682 | + |
---|
| 683 | + if (usb_ep_queue(ep_fback, req_fback, GFP_ATOMIC)) |
---|
| 684 | + dev_err(dev, "%s:%d Error!\n", __func__, __LINE__); |
---|
575 | 685 | |
---|
576 | 686 | return 0; |
---|
577 | 687 | } |
---|
.. | .. |
---|
581 | 691 | { |
---|
582 | 692 | struct snd_uac_chip *uac = audio_dev->uac; |
---|
583 | 693 | |
---|
| 694 | + set_active(&uac->c_prm, false); |
---|
| 695 | + if (audio_dev->in_ep_fback) |
---|
| 696 | + free_ep_fback(&uac->c_prm, audio_dev->in_ep_fback); |
---|
584 | 697 | free_ep(&uac->c_prm, audio_dev->out_ep); |
---|
585 | 698 | |
---|
586 | 699 | audio_dev->usb_state[SET_INTERFACE_OUT] = true; |
---|
.. | .. |
---|
593 | 706 | { |
---|
594 | 707 | struct snd_uac_chip *uac = audio_dev->uac; |
---|
595 | 708 | struct usb_gadget *gadget = audio_dev->gadget; |
---|
596 | | - struct device *dev = audio_dev->device; |
---|
| 709 | + struct device *dev = &gadget->dev; |
---|
597 | 710 | struct usb_request *req; |
---|
598 | 711 | struct usb_ep *ep; |
---|
599 | 712 | struct uac_rtd_params *prm; |
---|
600 | 713 | struct uac_params *params = &audio_dev->params; |
---|
| 714 | + unsigned int factor; |
---|
| 715 | + const struct usb_endpoint_descriptor *ep_desc; |
---|
601 | 716 | int req_len, i; |
---|
| 717 | + unsigned int p_pktsize; |
---|
602 | 718 | |
---|
603 | 719 | /* |
---|
604 | 720 | * For better compatibility on some PC Hosts which |
---|
.. | .. |
---|
613 | 729 | audio_dev->stream_state[STATE_IN] = true; |
---|
614 | 730 | schedule_work(&audio_dev->work); |
---|
615 | 731 | |
---|
616 | | - dev_dbg(dev, "start playback with rate %d\n", params->p_srate_active); |
---|
617 | | - ep = audio_dev->in_ep; |
---|
618 | 732 | prm = &uac->p_prm; |
---|
| 733 | + dev_dbg(dev, "start playback with rate %d\n", prm->srate); |
---|
| 734 | + ep = audio_dev->in_ep; |
---|
619 | 735 | config_ep_by_speed(gadget, &audio_dev->func, ep); |
---|
| 736 | + |
---|
| 737 | + ep_desc = ep->desc; |
---|
| 738 | + /* |
---|
| 739 | + * Always start with original frequency |
---|
| 740 | + */ |
---|
| 741 | + prm->pitch = 1000000; |
---|
| 742 | + |
---|
| 743 | + /* pre-calculate the playback endpoint's interval */ |
---|
| 744 | + if (gadget->speed == USB_SPEED_FULL) |
---|
| 745 | + factor = 1000; |
---|
| 746 | + else |
---|
| 747 | + factor = 8000; |
---|
| 748 | + |
---|
| 749 | + /* pre-compute some values for iso_complete() */ |
---|
| 750 | + uac->p_framesize = params->p_ssize * |
---|
| 751 | + num_channels(params->p_chmask); |
---|
| 752 | + uac->p_interval = factor / (1 << (ep_desc->bInterval - 1)); |
---|
| 753 | + p_pktsize = min_t(unsigned int, |
---|
| 754 | + uac->p_framesize * |
---|
| 755 | + (prm->srate / uac->p_interval), |
---|
| 756 | + ep->maxpacket); |
---|
| 757 | + |
---|
| 758 | + req_len = p_pktsize; |
---|
| 759 | + uac->p_residue_mil = 0; |
---|
620 | 760 | |
---|
621 | 761 | prm->ep_enabled = true; |
---|
622 | 762 | usb_ep_enable(ep); |
---|
623 | 763 | |
---|
624 | | - u_audio_set_playback_pktsize(audio_dev, params->p_srate_active); |
---|
625 | | - req_len = uac->p_pktsize; |
---|
626 | | - uac->p_residue = 0; |
---|
627 | | - |
---|
628 | 764 | for (i = 0; i < params->req_number; i++) { |
---|
629 | | - if (!prm->ureq[i].req) { |
---|
| 765 | + if (!prm->reqs[i]) { |
---|
630 | 766 | req = usb_ep_alloc_request(ep, GFP_ATOMIC); |
---|
631 | 767 | if (req == NULL) |
---|
632 | 768 | return -ENOMEM; |
---|
633 | 769 | |
---|
634 | | - prm->ureq[i].req = req; |
---|
635 | | - prm->ureq[i].pp = prm; |
---|
| 770 | + prm->reqs[i] = req; |
---|
636 | 771 | |
---|
637 | 772 | req->zero = 0; |
---|
638 | | - req->context = &prm->ureq[i]; |
---|
| 773 | + req->context = prm; |
---|
639 | 774 | req->length = req_len; |
---|
640 | 775 | req->complete = u_audio_iso_complete; |
---|
641 | | - req->buf = prm->rbuf + i * prm->max_psize; |
---|
| 776 | + req->buf = prm->rbuf + i * ep->maxpacket; |
---|
642 | 777 | } |
---|
643 | 778 | |
---|
644 | | - if (usb_ep_queue(ep, prm->ureq[i].req, GFP_ATOMIC)) |
---|
| 779 | + if (usb_ep_queue(ep, prm->reqs[i], GFP_ATOMIC)) |
---|
645 | 780 | dev_err(dev, "%s:%d Error!\n", __func__, __LINE__); |
---|
646 | 781 | } |
---|
| 782 | + |
---|
| 783 | + set_active(&uac->p_prm, true); |
---|
647 | 784 | |
---|
648 | 785 | return 0; |
---|
649 | 786 | } |
---|
.. | .. |
---|
653 | 790 | { |
---|
654 | 791 | struct snd_uac_chip *uac = audio_dev->uac; |
---|
655 | 792 | |
---|
| 793 | + set_active(&uac->p_prm, false); |
---|
656 | 794 | free_ep(&uac->p_prm, audio_dev->in_ep); |
---|
657 | 795 | |
---|
658 | 796 | audio_dev->usb_state[SET_INTERFACE_IN] = true; |
---|
.. | .. |
---|
661 | 799 | } |
---|
662 | 800 | EXPORT_SYMBOL_GPL(u_audio_stop_playback); |
---|
663 | 801 | |
---|
664 | | -int u_audio_fu_set_cmd(struct usb_audio_control *con, u8 cmd, int value) |
---|
| 802 | +void u_audio_suspend(struct g_audio *audio_dev) |
---|
665 | 803 | { |
---|
666 | | - struct g_audio *audio_dev = (struct g_audio *)con->context; |
---|
667 | | - struct uac_params *params = &audio_dev->params; |
---|
| 804 | + struct snd_uac_chip *uac = audio_dev->uac; |
---|
668 | 805 | |
---|
669 | | - switch (cmd) { |
---|
670 | | - case UAC_SET_CUR: |
---|
671 | | - if (!strncmp(con->name, "Capture Mute", 12)) { |
---|
672 | | - params->c_mute = value; |
---|
673 | | - audio_dev->usb_state[SET_MUTE_OUT] = true; |
---|
674 | | - } else if (!strncmp(con->name, "Capture Volume", 14)) { |
---|
675 | | - params->c_volume = value; |
---|
676 | | - audio_dev->usb_state[SET_VOLUME_OUT] = true; |
---|
677 | | - } else if (!strncmp(con->name, "Playback Mute", 13)) { |
---|
678 | | - params->p_mute = value; |
---|
679 | | - audio_dev->usb_state[SET_MUTE_IN] = true; |
---|
680 | | - } else if (!strncmp(con->name, "Playback Volume", 15)) { |
---|
681 | | - params->p_volume = value; |
---|
682 | | - audio_dev->usb_state[SET_VOLUME_IN] = true; |
---|
683 | | - } |
---|
684 | | - break; |
---|
685 | | - case UAC_SET_RES: |
---|
686 | | - /* fall through */ |
---|
687 | | - default: |
---|
688 | | - return 0; |
---|
689 | | - } |
---|
| 806 | + set_active(&uac->p_prm, false); |
---|
| 807 | + set_active(&uac->c_prm, false); |
---|
| 808 | +} |
---|
| 809 | +EXPORT_SYMBOL_GPL(u_audio_suspend); |
---|
690 | 810 | |
---|
691 | | - con->data[cmd] = value; |
---|
692 | | - schedule_work(&audio_dev->work); |
---|
| 811 | +int u_audio_get_volume(struct g_audio *audio_dev, int playback, s16 *val) |
---|
| 812 | +{ |
---|
| 813 | + struct snd_uac_chip *uac = audio_dev->uac; |
---|
| 814 | + struct uac_rtd_params *prm; |
---|
| 815 | + unsigned long flags; |
---|
| 816 | + |
---|
| 817 | + if (playback) |
---|
| 818 | + prm = &uac->p_prm; |
---|
| 819 | + else |
---|
| 820 | + prm = &uac->c_prm; |
---|
| 821 | + |
---|
| 822 | + spin_lock_irqsave(&prm->lock, flags); |
---|
| 823 | + *val = prm->volume; |
---|
| 824 | + spin_unlock_irqrestore(&prm->lock, flags); |
---|
693 | 825 | |
---|
694 | 826 | return 0; |
---|
695 | 827 | } |
---|
696 | | -EXPORT_SYMBOL_GPL(u_audio_fu_set_cmd); |
---|
| 828 | +EXPORT_SYMBOL_GPL(u_audio_get_volume); |
---|
697 | 829 | |
---|
698 | | -int u_audio_fu_get_cmd(struct usb_audio_control *con, u8 cmd) |
---|
| 830 | +int u_audio_set_volume(struct g_audio *audio_dev, int playback, s16 val) |
---|
699 | 831 | { |
---|
700 | | - struct g_audio *audio_dev = (struct g_audio *)con->context; |
---|
| 832 | + struct snd_uac_chip *uac = audio_dev->uac; |
---|
| 833 | + struct uac_rtd_params *prm; |
---|
| 834 | + unsigned long flags; |
---|
| 835 | + int change = 0; |
---|
701 | 836 | |
---|
702 | | - dev_dbg(audio_dev->device, "GET_CMD con %s cmd %d data %d\n", |
---|
703 | | - con->name, cmd, (int16_t)con->data[cmd]); |
---|
704 | | - return con->data[cmd]; |
---|
| 837 | + if (playback) |
---|
| 838 | + prm = &uac->p_prm; |
---|
| 839 | + else |
---|
| 840 | + prm = &uac->c_prm; |
---|
| 841 | + |
---|
| 842 | + spin_lock_irqsave(&prm->lock, flags); |
---|
| 843 | + val = clamp(val, prm->volume_min, prm->volume_max); |
---|
| 844 | + if (prm->volume != val) { |
---|
| 845 | + prm->volume = val; |
---|
| 846 | + change = 1; |
---|
| 847 | + } |
---|
| 848 | + spin_unlock_irqrestore(&prm->lock, flags); |
---|
| 849 | + |
---|
| 850 | + if (change) { |
---|
| 851 | + if (playback) |
---|
| 852 | + audio_dev->usb_state[SET_VOLUME_IN] = true; |
---|
| 853 | + else |
---|
| 854 | + audio_dev->usb_state[SET_VOLUME_OUT] = true; |
---|
| 855 | + schedule_work(&audio_dev->work); |
---|
| 856 | + |
---|
| 857 | + snd_ctl_notify(uac->card, SNDRV_CTL_EVENT_MASK_VALUE, |
---|
| 858 | + &prm->snd_kctl_volume->id); |
---|
| 859 | + } |
---|
| 860 | + |
---|
| 861 | + return 0; |
---|
705 | 862 | } |
---|
706 | | -EXPORT_SYMBOL_GPL(u_audio_fu_get_cmd); |
---|
| 863 | +EXPORT_SYMBOL_GPL(u_audio_set_volume); |
---|
| 864 | + |
---|
| 865 | +int u_audio_get_mute(struct g_audio *audio_dev, int playback, int *val) |
---|
| 866 | +{ |
---|
| 867 | + struct snd_uac_chip *uac = audio_dev->uac; |
---|
| 868 | + struct uac_rtd_params *prm; |
---|
| 869 | + unsigned long flags; |
---|
| 870 | + |
---|
| 871 | + if (playback) |
---|
| 872 | + prm = &uac->p_prm; |
---|
| 873 | + else |
---|
| 874 | + prm = &uac->c_prm; |
---|
| 875 | + |
---|
| 876 | + spin_lock_irqsave(&prm->lock, flags); |
---|
| 877 | + *val = prm->mute; |
---|
| 878 | + spin_unlock_irqrestore(&prm->lock, flags); |
---|
| 879 | + |
---|
| 880 | + return 0; |
---|
| 881 | +} |
---|
| 882 | +EXPORT_SYMBOL_GPL(u_audio_get_mute); |
---|
| 883 | + |
---|
| 884 | +int u_audio_set_mute(struct g_audio *audio_dev, int playback, int val) |
---|
| 885 | +{ |
---|
| 886 | + struct snd_uac_chip *uac = audio_dev->uac; |
---|
| 887 | + struct uac_rtd_params *prm; |
---|
| 888 | + unsigned long flags; |
---|
| 889 | + int change = 0; |
---|
| 890 | + int mute; |
---|
| 891 | + |
---|
| 892 | + if (playback) |
---|
| 893 | + prm = &uac->p_prm; |
---|
| 894 | + else |
---|
| 895 | + prm = &uac->c_prm; |
---|
| 896 | + |
---|
| 897 | + mute = val ? 1 : 0; |
---|
| 898 | + |
---|
| 899 | + spin_lock_irqsave(&prm->lock, flags); |
---|
| 900 | + if (prm->mute != mute) { |
---|
| 901 | + prm->mute = mute; |
---|
| 902 | + change = 1; |
---|
| 903 | + } |
---|
| 904 | + spin_unlock_irqrestore(&prm->lock, flags); |
---|
| 905 | + |
---|
| 906 | + if (change) { |
---|
| 907 | + if (playback) |
---|
| 908 | + audio_dev->usb_state[SET_MUTE_IN] = true; |
---|
| 909 | + else |
---|
| 910 | + audio_dev->usb_state[SET_MUTE_OUT] = true; |
---|
| 911 | + schedule_work(&audio_dev->work); |
---|
| 912 | + |
---|
| 913 | + snd_ctl_notify(uac->card, SNDRV_CTL_EVENT_MASK_VALUE, |
---|
| 914 | + &prm->snd_kctl_mute->id); |
---|
| 915 | + } |
---|
| 916 | + |
---|
| 917 | + return 0; |
---|
| 918 | +} |
---|
| 919 | +EXPORT_SYMBOL_GPL(u_audio_set_mute); |
---|
| 920 | + |
---|
| 921 | + |
---|
| 922 | +static int u_audio_pitch_info(struct snd_kcontrol *kcontrol, |
---|
| 923 | + struct snd_ctl_elem_info *uinfo) |
---|
| 924 | +{ |
---|
| 925 | + struct uac_rtd_params *prm = snd_kcontrol_chip(kcontrol); |
---|
| 926 | + struct snd_uac_chip *uac = prm->uac; |
---|
| 927 | + struct g_audio *audio_dev = uac->audio_dev; |
---|
| 928 | + struct uac_params *params = &audio_dev->params; |
---|
| 929 | + unsigned int pitch_min, pitch_max; |
---|
| 930 | + |
---|
| 931 | + pitch_min = (1000 - FBACK_SLOW_MAX) * 1000; |
---|
| 932 | + pitch_max = (1000 + params->fb_max) * 1000; |
---|
| 933 | + |
---|
| 934 | + uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; |
---|
| 935 | + uinfo->count = 1; |
---|
| 936 | + uinfo->value.integer.min = pitch_min; |
---|
| 937 | + uinfo->value.integer.max = pitch_max; |
---|
| 938 | + uinfo->value.integer.step = 1; |
---|
| 939 | + return 0; |
---|
| 940 | +} |
---|
| 941 | + |
---|
| 942 | +static int u_audio_pitch_get(struct snd_kcontrol *kcontrol, |
---|
| 943 | + struct snd_ctl_elem_value *ucontrol) |
---|
| 944 | +{ |
---|
| 945 | + struct uac_rtd_params *prm = snd_kcontrol_chip(kcontrol); |
---|
| 946 | + |
---|
| 947 | + ucontrol->value.integer.value[0] = prm->pitch; |
---|
| 948 | + |
---|
| 949 | + return 0; |
---|
| 950 | +} |
---|
| 951 | + |
---|
| 952 | +static int u_audio_pitch_put(struct snd_kcontrol *kcontrol, |
---|
| 953 | + struct snd_ctl_elem_value *ucontrol) |
---|
| 954 | +{ |
---|
| 955 | + struct uac_rtd_params *prm = snd_kcontrol_chip(kcontrol); |
---|
| 956 | + struct snd_uac_chip *uac = prm->uac; |
---|
| 957 | + struct g_audio *audio_dev = uac->audio_dev; |
---|
| 958 | + struct uac_params *params = &audio_dev->params; |
---|
| 959 | + unsigned int val; |
---|
| 960 | + unsigned int pitch_min, pitch_max; |
---|
| 961 | + int change = 0; |
---|
| 962 | + |
---|
| 963 | + pitch_min = (1000 - FBACK_SLOW_MAX) * 1000; |
---|
| 964 | + pitch_max = (1000 + params->fb_max) * 1000; |
---|
| 965 | + |
---|
| 966 | + val = ucontrol->value.integer.value[0]; |
---|
| 967 | + |
---|
| 968 | + if (val < pitch_min) |
---|
| 969 | + val = pitch_min; |
---|
| 970 | + if (val > pitch_max) |
---|
| 971 | + val = pitch_max; |
---|
| 972 | + |
---|
| 973 | + if (prm->pitch != val) { |
---|
| 974 | + prm->pitch = val; |
---|
| 975 | + change = 1; |
---|
| 976 | + } |
---|
| 977 | + |
---|
| 978 | + return change; |
---|
| 979 | +} |
---|
| 980 | + |
---|
| 981 | +static int u_audio_mute_info(struct snd_kcontrol *kcontrol, |
---|
| 982 | + struct snd_ctl_elem_info *uinfo) |
---|
| 983 | +{ |
---|
| 984 | + uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN; |
---|
| 985 | + uinfo->count = 1; |
---|
| 986 | + uinfo->value.integer.min = 0; |
---|
| 987 | + uinfo->value.integer.max = 1; |
---|
| 988 | + uinfo->value.integer.step = 1; |
---|
| 989 | + |
---|
| 990 | + return 0; |
---|
| 991 | +} |
---|
| 992 | + |
---|
| 993 | +static int u_audio_mute_get(struct snd_kcontrol *kcontrol, |
---|
| 994 | + struct snd_ctl_elem_value *ucontrol) |
---|
| 995 | +{ |
---|
| 996 | + struct uac_rtd_params *prm = snd_kcontrol_chip(kcontrol); |
---|
| 997 | + unsigned long flags; |
---|
| 998 | + |
---|
| 999 | + spin_lock_irqsave(&prm->lock, flags); |
---|
| 1000 | + ucontrol->value.integer.value[0] = !prm->mute; |
---|
| 1001 | + spin_unlock_irqrestore(&prm->lock, flags); |
---|
| 1002 | + |
---|
| 1003 | + return 0; |
---|
| 1004 | +} |
---|
| 1005 | + |
---|
| 1006 | +static int u_audio_mute_put(struct snd_kcontrol *kcontrol, |
---|
| 1007 | + struct snd_ctl_elem_value *ucontrol) |
---|
| 1008 | +{ |
---|
| 1009 | + struct uac_rtd_params *prm = snd_kcontrol_chip(kcontrol); |
---|
| 1010 | + struct snd_uac_chip *uac = prm->uac; |
---|
| 1011 | + struct g_audio *audio_dev = uac->audio_dev; |
---|
| 1012 | + unsigned int val; |
---|
| 1013 | + unsigned long flags; |
---|
| 1014 | + int change = 0; |
---|
| 1015 | + |
---|
| 1016 | + val = !ucontrol->value.integer.value[0]; |
---|
| 1017 | + |
---|
| 1018 | + spin_lock_irqsave(&prm->lock, flags); |
---|
| 1019 | + if (val != prm->mute) { |
---|
| 1020 | + prm->mute = val; |
---|
| 1021 | + change = 1; |
---|
| 1022 | + } |
---|
| 1023 | + spin_unlock_irqrestore(&prm->lock, flags); |
---|
| 1024 | + |
---|
| 1025 | + if (change && audio_dev->notify) |
---|
| 1026 | + audio_dev->notify(audio_dev, prm->fu_id, UAC_FU_MUTE); |
---|
| 1027 | + |
---|
| 1028 | + return change; |
---|
| 1029 | +} |
---|
| 1030 | + |
---|
| 1031 | +/* |
---|
| 1032 | + * TLV callback for mixer volume controls |
---|
| 1033 | + */ |
---|
| 1034 | +static int u_audio_volume_tlv(struct snd_kcontrol *kcontrol, int op_flag, |
---|
| 1035 | + unsigned int size, unsigned int __user *_tlv) |
---|
| 1036 | +{ |
---|
| 1037 | + struct uac_rtd_params *prm = snd_kcontrol_chip(kcontrol); |
---|
| 1038 | + DECLARE_TLV_DB_MINMAX(scale, 0, 0); |
---|
| 1039 | + |
---|
| 1040 | + if (size < sizeof(scale)) |
---|
| 1041 | + return -ENOMEM; |
---|
| 1042 | + |
---|
| 1043 | + /* UAC volume resolution is 1/256 dB, TLV is 1/100 dB */ |
---|
| 1044 | + scale[2] = (prm->volume_min * 100) / 256; |
---|
| 1045 | + scale[3] = (prm->volume_max * 100) / 256; |
---|
| 1046 | + if (copy_to_user(_tlv, scale, sizeof(scale))) |
---|
| 1047 | + return -EFAULT; |
---|
| 1048 | + |
---|
| 1049 | + return 0; |
---|
| 1050 | +} |
---|
| 1051 | + |
---|
| 1052 | +static int u_audio_volume_info(struct snd_kcontrol *kcontrol, |
---|
| 1053 | + struct snd_ctl_elem_info *uinfo) |
---|
| 1054 | +{ |
---|
| 1055 | + struct uac_rtd_params *prm = snd_kcontrol_chip(kcontrol); |
---|
| 1056 | + |
---|
| 1057 | + uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; |
---|
| 1058 | + uinfo->count = 1; |
---|
| 1059 | + uinfo->value.integer.min = 0; |
---|
| 1060 | + uinfo->value.integer.max = |
---|
| 1061 | + (prm->volume_max - prm->volume_min + prm->volume_res - 1) |
---|
| 1062 | + / prm->volume_res; |
---|
| 1063 | + uinfo->value.integer.step = 1; |
---|
| 1064 | + |
---|
| 1065 | + return 0; |
---|
| 1066 | +} |
---|
| 1067 | + |
---|
| 1068 | +static int u_audio_volume_get(struct snd_kcontrol *kcontrol, |
---|
| 1069 | + struct snd_ctl_elem_value *ucontrol) |
---|
| 1070 | +{ |
---|
| 1071 | + struct uac_rtd_params *prm = snd_kcontrol_chip(kcontrol); |
---|
| 1072 | + unsigned long flags; |
---|
| 1073 | + |
---|
| 1074 | + spin_lock_irqsave(&prm->lock, flags); |
---|
| 1075 | + ucontrol->value.integer.value[0] = |
---|
| 1076 | + (prm->volume - prm->volume_min) / prm->volume_res; |
---|
| 1077 | + spin_unlock_irqrestore(&prm->lock, flags); |
---|
| 1078 | + |
---|
| 1079 | + return 0; |
---|
| 1080 | +} |
---|
| 1081 | + |
---|
| 1082 | +static int u_audio_volume_put(struct snd_kcontrol *kcontrol, |
---|
| 1083 | + struct snd_ctl_elem_value *ucontrol) |
---|
| 1084 | +{ |
---|
| 1085 | + struct uac_rtd_params *prm = snd_kcontrol_chip(kcontrol); |
---|
| 1086 | + struct snd_uac_chip *uac = prm->uac; |
---|
| 1087 | + struct g_audio *audio_dev = uac->audio_dev; |
---|
| 1088 | + unsigned int val; |
---|
| 1089 | + s16 volume; |
---|
| 1090 | + unsigned long flags; |
---|
| 1091 | + int change = 0; |
---|
| 1092 | + |
---|
| 1093 | + val = ucontrol->value.integer.value[0]; |
---|
| 1094 | + |
---|
| 1095 | + spin_lock_irqsave(&prm->lock, flags); |
---|
| 1096 | + volume = (val * prm->volume_res) + prm->volume_min; |
---|
| 1097 | + volume = clamp(volume, prm->volume_min, prm->volume_max); |
---|
| 1098 | + if (volume != prm->volume) { |
---|
| 1099 | + prm->volume = volume; |
---|
| 1100 | + change = 1; |
---|
| 1101 | + } |
---|
| 1102 | + spin_unlock_irqrestore(&prm->lock, flags); |
---|
| 1103 | + |
---|
| 1104 | + if (change && audio_dev->notify) |
---|
| 1105 | + audio_dev->notify(audio_dev, prm->fu_id, UAC_FU_VOLUME); |
---|
| 1106 | + |
---|
| 1107 | + return change; |
---|
| 1108 | +} |
---|
| 1109 | + |
---|
| 1110 | +static int get_max_srate(const int *srates) |
---|
| 1111 | +{ |
---|
| 1112 | + int i, max_srate = 0; |
---|
| 1113 | + |
---|
| 1114 | + for (i = 0; i < UAC_MAX_RATES; i++) { |
---|
| 1115 | + if (srates[i] == 0) |
---|
| 1116 | + break; |
---|
| 1117 | + if (srates[i] > max_srate) |
---|
| 1118 | + max_srate = srates[i]; |
---|
| 1119 | + } |
---|
| 1120 | + return max_srate; |
---|
| 1121 | +} |
---|
| 1122 | + |
---|
| 1123 | +static int get_min_srate(const int *srates) |
---|
| 1124 | +{ |
---|
| 1125 | + int i, min_srate = INT_MAX; |
---|
| 1126 | + |
---|
| 1127 | + for (i = 0; i < UAC_MAX_RATES; i++) { |
---|
| 1128 | + if (srates[i] == 0) |
---|
| 1129 | + break; |
---|
| 1130 | + if (srates[i] < min_srate) |
---|
| 1131 | + min_srate = srates[i]; |
---|
| 1132 | + } |
---|
| 1133 | + return min_srate; |
---|
| 1134 | +} |
---|
| 1135 | + |
---|
| 1136 | +static int u_audio_rate_info(struct snd_kcontrol *kcontrol, |
---|
| 1137 | + struct snd_ctl_elem_info *uinfo) |
---|
| 1138 | +{ |
---|
| 1139 | + const int *srates; |
---|
| 1140 | + struct uac_rtd_params *prm = snd_kcontrol_chip(kcontrol); |
---|
| 1141 | + struct snd_uac_chip *uac = prm->uac; |
---|
| 1142 | + struct g_audio *audio_dev = uac->audio_dev; |
---|
| 1143 | + struct uac_params *params = &audio_dev->params; |
---|
| 1144 | + |
---|
| 1145 | + uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; |
---|
| 1146 | + uinfo->count = 1; |
---|
| 1147 | + |
---|
| 1148 | + if (prm == &uac->c_prm) |
---|
| 1149 | + srates = params->c_srates; |
---|
| 1150 | + else |
---|
| 1151 | + srates = params->p_srates; |
---|
| 1152 | + uinfo->value.integer.min = get_min_srate(srates); |
---|
| 1153 | + uinfo->value.integer.max = get_max_srate(srates); |
---|
| 1154 | + return 0; |
---|
| 1155 | +} |
---|
| 1156 | + |
---|
| 1157 | +static int u_audio_rate_get(struct snd_kcontrol *kcontrol, |
---|
| 1158 | + struct snd_ctl_elem_value *ucontrol) |
---|
| 1159 | +{ |
---|
| 1160 | + struct uac_rtd_params *prm = snd_kcontrol_chip(kcontrol); |
---|
| 1161 | + unsigned long flags; |
---|
| 1162 | + |
---|
| 1163 | + spin_lock_irqsave(&prm->lock, flags); |
---|
| 1164 | + if (prm->active) |
---|
| 1165 | + ucontrol->value.integer.value[0] = prm->srate; |
---|
| 1166 | + else |
---|
| 1167 | + /* not active: reporting zero rate */ |
---|
| 1168 | + ucontrol->value.integer.value[0] = 0; |
---|
| 1169 | + spin_unlock_irqrestore(&prm->lock, flags); |
---|
| 1170 | + return 0; |
---|
| 1171 | +} |
---|
| 1172 | + |
---|
| 1173 | +static struct snd_kcontrol_new u_audio_controls[] = { |
---|
| 1174 | + [UAC_FBACK_CTRL] { |
---|
| 1175 | + .iface = SNDRV_CTL_ELEM_IFACE_PCM, |
---|
| 1176 | + .name = "Capture Pitch 1000000", |
---|
| 1177 | + .info = u_audio_pitch_info, |
---|
| 1178 | + .get = u_audio_pitch_get, |
---|
| 1179 | + .put = u_audio_pitch_put, |
---|
| 1180 | + }, |
---|
| 1181 | + [UAC_P_PITCH_CTRL] { |
---|
| 1182 | + .iface = SNDRV_CTL_ELEM_IFACE_PCM, |
---|
| 1183 | + .name = "Playback Pitch 1000000", |
---|
| 1184 | + .info = u_audio_pitch_info, |
---|
| 1185 | + .get = u_audio_pitch_get, |
---|
| 1186 | + .put = u_audio_pitch_put, |
---|
| 1187 | + }, |
---|
| 1188 | + [UAC_MUTE_CTRL] { |
---|
| 1189 | + .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
---|
| 1190 | + .name = "", /* will be filled later */ |
---|
| 1191 | + .info = u_audio_mute_info, |
---|
| 1192 | + .get = u_audio_mute_get, |
---|
| 1193 | + .put = u_audio_mute_put, |
---|
| 1194 | + }, |
---|
| 1195 | + [UAC_VOLUME_CTRL] { |
---|
| 1196 | + .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
---|
| 1197 | + .name = "", /* will be filled later */ |
---|
| 1198 | + .info = u_audio_volume_info, |
---|
| 1199 | + .get = u_audio_volume_get, |
---|
| 1200 | + .put = u_audio_volume_put, |
---|
| 1201 | + }, |
---|
| 1202 | + [UAC_RATE_CTRL] { |
---|
| 1203 | + .iface = SNDRV_CTL_ELEM_IFACE_PCM, |
---|
| 1204 | + .name = "", /* will be filled later */ |
---|
| 1205 | + .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, |
---|
| 1206 | + .info = u_audio_rate_info, |
---|
| 1207 | + .get = u_audio_rate_get, |
---|
| 1208 | + }, |
---|
| 1209 | +}; |
---|
707 | 1210 | |
---|
708 | 1211 | static void g_audio_work(struct work_struct *data) |
---|
709 | 1212 | { |
---|
710 | 1213 | struct g_audio *audio = container_of(data, struct g_audio, work); |
---|
711 | | - struct uac_params *params = &audio->params; |
---|
| 1214 | + struct usb_gadget *gadget = audio->gadget; |
---|
| 1215 | + struct snd_uac_chip *uac = audio->uac; |
---|
| 1216 | + struct uac_rtd_params *prm; |
---|
| 1217 | + struct device *dev = &gadget->dev; |
---|
712 | 1218 | char *uac_event[4] = { NULL, NULL, NULL, NULL }; |
---|
713 | 1219 | char str[19]; |
---|
714 | | - signed short volume; |
---|
715 | 1220 | int i; |
---|
716 | 1221 | |
---|
717 | 1222 | for (i = 0; i < SET_USB_STATE_MAX; i++) { |
---|
.. | .. |
---|
734 | 1239 | case SET_SAMPLE_RATE_OUT: |
---|
735 | 1240 | uac_event[0] = "USB_STATE=SET_SAMPLE_RATE"; |
---|
736 | 1241 | uac_event[1] = "STREAM_DIRECTION=OUT"; |
---|
| 1242 | + prm = &uac->c_prm; |
---|
737 | 1243 | snprintf(str, sizeof(str), "SAMPLE_RATE=%d", |
---|
738 | | - params->c_srate_active); |
---|
| 1244 | + prm->srate); |
---|
739 | 1245 | uac_event[2] = str; |
---|
740 | 1246 | break; |
---|
741 | 1247 | case SET_SAMPLE_RATE_IN: |
---|
742 | 1248 | uac_event[0] = "USB_STATE=SET_SAMPLE_RATE"; |
---|
743 | 1249 | uac_event[1] = "STREAM_DIRECTION=IN"; |
---|
| 1250 | + prm = &uac->p_prm; |
---|
744 | 1251 | snprintf(str, sizeof(str), "SAMPLE_RATE=%d", |
---|
745 | | - params->p_srate_active); |
---|
| 1252 | + prm->srate); |
---|
746 | 1253 | uac_event[2] = str; |
---|
747 | 1254 | break; |
---|
748 | 1255 | case SET_MUTE_OUT: |
---|
749 | 1256 | uac_event[0] = "USB_STATE=SET_MUTE"; |
---|
750 | 1257 | uac_event[1] = "STREAM_DIRECTION=OUT"; |
---|
751 | | - snprintf(str, sizeof(str), "MUTE=%d", params->c_mute); |
---|
| 1258 | + prm = &uac->c_prm; |
---|
| 1259 | + snprintf(str, sizeof(str), "MUTE=%d", prm->mute); |
---|
752 | 1260 | uac_event[2] = str; |
---|
753 | 1261 | break; |
---|
754 | 1262 | case SET_MUTE_IN: |
---|
755 | 1263 | uac_event[0] = "USB_STATE=SET_MUTE"; |
---|
756 | 1264 | uac_event[1] = "STREAM_DIRECTION=IN"; |
---|
757 | | - snprintf(str, sizeof(str), "MUTE=%d", params->p_mute); |
---|
| 1265 | + prm = &uac->p_prm; |
---|
| 1266 | + snprintf(str, sizeof(str), "MUTE=%d", prm->mute); |
---|
758 | 1267 | uac_event[2] = str; |
---|
759 | 1268 | break; |
---|
760 | 1269 | case SET_VOLUME_OUT: |
---|
761 | 1270 | uac_event[0] = "USB_STATE=SET_VOLUME"; |
---|
762 | 1271 | uac_event[1] = "STREAM_DIRECTION=OUT"; |
---|
763 | | - volume = (signed short)params->c_volume; |
---|
764 | | - volume /= UAC_VOLUME_RES; |
---|
765 | | - snprintf(str, sizeof(str), "VOLUME=%d%%", volume + 50); |
---|
| 1272 | + prm = &uac->c_prm; |
---|
| 1273 | + snprintf(str, sizeof(str), "VOLUME=0x%hx", prm->volume); |
---|
766 | 1274 | uac_event[2] = str; |
---|
767 | 1275 | break; |
---|
768 | 1276 | case SET_VOLUME_IN: |
---|
769 | 1277 | uac_event[0] = "USB_STATE=SET_VOLUME"; |
---|
770 | 1278 | uac_event[1] = "STREAM_DIRECTION=IN"; |
---|
771 | | - volume = (signed short)params->p_volume; |
---|
772 | | - volume /= UAC_VOLUME_RES; |
---|
773 | | - snprintf(str, sizeof(str), "VOLUME=%d%%", volume + 50); |
---|
| 1279 | + prm = &uac->p_prm; |
---|
| 1280 | + snprintf(str, sizeof(str), "VOLUME=0x%hx", prm->volume); |
---|
774 | 1281 | uac_event[2] = str; |
---|
775 | 1282 | break; |
---|
776 | 1283 | case SET_AUDIO_CLK: |
---|
777 | 1284 | uac_event[0] = "USB_STATE=SET_AUDIO_CLK"; |
---|
778 | | - snprintf(str, sizeof(str), "PPM=%d", params->ppm); |
---|
| 1285 | + snprintf(str, sizeof(str), "PPM=%d", audio->params.ppm); |
---|
779 | 1286 | uac_event[1] = str; |
---|
780 | 1287 | default: |
---|
781 | 1288 | break; |
---|
.. | .. |
---|
784 | 1291 | audio->usb_state[i] = false; |
---|
785 | 1292 | kobject_uevent_env(&audio->device->kobj, KOBJ_CHANGE, |
---|
786 | 1293 | uac_event); |
---|
787 | | - dev_dbg(audio->device, "%s: sent uac uevent %s %s %s\n", |
---|
788 | | - __func__, uac_event[0], uac_event[1], uac_event[2]); |
---|
| 1294 | + dev_dbg(dev, "%s: sent uac uevent %s %s %s\n", __func__, |
---|
| 1295 | + uac_event[0], uac_event[1], uac_event[2]); |
---|
789 | 1296 | } |
---|
790 | 1297 | } |
---|
791 | 1298 | |
---|
.. | .. |
---|
896 | 1403 | struct snd_uac_chip *uac; |
---|
897 | 1404 | struct snd_card *card; |
---|
898 | 1405 | struct snd_pcm *pcm; |
---|
| 1406 | + struct snd_kcontrol *kctl; |
---|
899 | 1407 | struct uac_params *params; |
---|
900 | 1408 | int p_chmask, c_chmask; |
---|
901 | | - int err; |
---|
902 | | - int i; |
---|
| 1409 | + int i, err; |
---|
903 | 1410 | |
---|
904 | 1411 | if (!g_audio) |
---|
905 | 1412 | return -EINVAL; |
---|
.. | .. |
---|
923 | 1430 | if (c_chmask) { |
---|
924 | 1431 | struct uac_rtd_params *prm = &uac->c_prm; |
---|
925 | 1432 | |
---|
926 | | - uac->c_prm.uac = uac; |
---|
| 1433 | + spin_lock_init(&prm->lock); |
---|
| 1434 | + uac->c_prm.uac = uac; |
---|
927 | 1435 | prm->max_psize = g_audio->out_ep_maxpsize; |
---|
| 1436 | + prm->srate = params->c_srates[0]; |
---|
928 | 1437 | |
---|
929 | | - prm->ureq = kcalloc(params->req_number, sizeof(struct uac_req), |
---|
930 | | - GFP_KERNEL); |
---|
931 | | - if (!prm->ureq) { |
---|
| 1438 | + prm->reqs = kcalloc(params->req_number, |
---|
| 1439 | + sizeof(struct usb_request *), |
---|
| 1440 | + GFP_KERNEL); |
---|
| 1441 | + if (!prm->reqs) { |
---|
932 | 1442 | err = -ENOMEM; |
---|
933 | 1443 | goto fail; |
---|
934 | 1444 | } |
---|
.. | .. |
---|
945 | 1455 | if (p_chmask) { |
---|
946 | 1456 | struct uac_rtd_params *prm = &uac->p_prm; |
---|
947 | 1457 | |
---|
| 1458 | + spin_lock_init(&prm->lock); |
---|
948 | 1459 | uac->p_prm.uac = uac; |
---|
949 | 1460 | prm->max_psize = g_audio->in_ep_maxpsize; |
---|
| 1461 | + prm->srate = params->p_srates[0]; |
---|
950 | 1462 | |
---|
951 | | - prm->ureq = kcalloc(params->req_number, sizeof(struct uac_req), |
---|
952 | | - GFP_KERNEL); |
---|
953 | | - if (!prm->ureq) { |
---|
| 1463 | + prm->reqs = kcalloc(params->req_number, |
---|
| 1464 | + sizeof(struct usb_request *), |
---|
| 1465 | + GFP_KERNEL); |
---|
| 1466 | + if (!prm->reqs) { |
---|
954 | 1467 | err = -ENOMEM; |
---|
955 | 1468 | goto fail; |
---|
956 | 1469 | } |
---|
.. | .. |
---|
981 | 1494 | if (err < 0) |
---|
982 | 1495 | goto snd_fail; |
---|
983 | 1496 | |
---|
984 | | - strlcpy(pcm->name, pcm_name, sizeof(pcm->name)); |
---|
| 1497 | + strscpy(pcm->name, pcm_name, sizeof(pcm->name)); |
---|
985 | 1498 | pcm->private_data = uac; |
---|
986 | 1499 | uac->pcm = pcm; |
---|
987 | 1500 | |
---|
988 | 1501 | snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &uac_pcm_ops); |
---|
989 | 1502 | snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &uac_pcm_ops); |
---|
990 | 1503 | |
---|
991 | | - strlcpy(card->driver, card_name, sizeof(card->driver)); |
---|
992 | | - strlcpy(card->shortname, card_name, sizeof(card->shortname)); |
---|
993 | | - sprintf(card->longname, "%s %i", card_name, card->dev->id); |
---|
| 1504 | + /* |
---|
| 1505 | + * Create mixer and controls |
---|
| 1506 | + * Create only if it's required on USB side |
---|
| 1507 | + */ |
---|
| 1508 | + if ((c_chmask && g_audio->in_ep_fback) |
---|
| 1509 | + || (p_chmask && params->p_fu.id) |
---|
| 1510 | + || (c_chmask && params->c_fu.id)) |
---|
| 1511 | + strscpy(card->mixername, card_name, sizeof(card->driver)); |
---|
994 | 1512 | |
---|
995 | | - snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS, |
---|
996 | | - snd_dma_continuous_data(GFP_KERNEL), 0, BUFF_SIZE_MAX); |
---|
| 1513 | + if (c_chmask && g_audio->in_ep_fback) { |
---|
| 1514 | + kctl = snd_ctl_new1(&u_audio_controls[UAC_FBACK_CTRL], |
---|
| 1515 | + &uac->c_prm); |
---|
| 1516 | + if (!kctl) { |
---|
| 1517 | + err = -ENOMEM; |
---|
| 1518 | + goto snd_fail; |
---|
| 1519 | + } |
---|
997 | 1520 | |
---|
998 | | - /* Add controls */ |
---|
999 | | - for (i = 0; i < ARRAY_SIZE(uac_pcm_controls); i++) { |
---|
1000 | | - err = snd_ctl_add(card, |
---|
1001 | | - snd_ctl_new1(&uac_pcm_controls[i], uac)); |
---|
| 1521 | + kctl->id.device = pcm->device; |
---|
| 1522 | + kctl->id.subdevice = 0; |
---|
| 1523 | + |
---|
| 1524 | + err = snd_ctl_add(card, kctl); |
---|
1002 | 1525 | if (err < 0) |
---|
1003 | 1526 | goto snd_fail; |
---|
1004 | 1527 | } |
---|
| 1528 | + |
---|
| 1529 | + if (p_chmask) { |
---|
| 1530 | + kctl = snd_ctl_new1(&u_audio_controls[UAC_P_PITCH_CTRL], |
---|
| 1531 | + &uac->p_prm); |
---|
| 1532 | + if (!kctl) { |
---|
| 1533 | + err = -ENOMEM; |
---|
| 1534 | + goto snd_fail; |
---|
| 1535 | + } |
---|
| 1536 | + |
---|
| 1537 | + kctl->id.device = pcm->device; |
---|
| 1538 | + kctl->id.subdevice = 0; |
---|
| 1539 | + |
---|
| 1540 | + err = snd_ctl_add(card, kctl); |
---|
| 1541 | + if (err < 0) |
---|
| 1542 | + goto snd_fail; |
---|
| 1543 | + } |
---|
| 1544 | + |
---|
| 1545 | + for (i = 0; i <= SNDRV_PCM_STREAM_LAST; i++) { |
---|
| 1546 | + struct uac_rtd_params *prm; |
---|
| 1547 | + struct uac_fu_params *fu; |
---|
| 1548 | + char ctrl_name[24]; |
---|
| 1549 | + char *direction; |
---|
| 1550 | + |
---|
| 1551 | + if (!pcm->streams[i].substream_count) |
---|
| 1552 | + continue; |
---|
| 1553 | + |
---|
| 1554 | + if (i == SNDRV_PCM_STREAM_PLAYBACK) { |
---|
| 1555 | + prm = &uac->p_prm; |
---|
| 1556 | + fu = ¶ms->p_fu; |
---|
| 1557 | + direction = "Playback"; |
---|
| 1558 | + } else { |
---|
| 1559 | + prm = &uac->c_prm; |
---|
| 1560 | + fu = ¶ms->c_fu; |
---|
| 1561 | + direction = "Capture"; |
---|
| 1562 | + } |
---|
| 1563 | + |
---|
| 1564 | + prm->fu_id = fu->id; |
---|
| 1565 | + |
---|
| 1566 | + if (fu->mute_present) { |
---|
| 1567 | + snprintf(ctrl_name, sizeof(ctrl_name), |
---|
| 1568 | + "PCM %s Switch", direction); |
---|
| 1569 | + |
---|
| 1570 | + u_audio_controls[UAC_MUTE_CTRL].name = ctrl_name; |
---|
| 1571 | + |
---|
| 1572 | + kctl = snd_ctl_new1(&u_audio_controls[UAC_MUTE_CTRL], |
---|
| 1573 | + prm); |
---|
| 1574 | + if (!kctl) { |
---|
| 1575 | + err = -ENOMEM; |
---|
| 1576 | + goto snd_fail; |
---|
| 1577 | + } |
---|
| 1578 | + |
---|
| 1579 | + kctl->id.device = pcm->device; |
---|
| 1580 | + kctl->id.subdevice = 0; |
---|
| 1581 | + |
---|
| 1582 | + err = snd_ctl_add(card, kctl); |
---|
| 1583 | + if (err < 0) |
---|
| 1584 | + goto snd_fail; |
---|
| 1585 | + prm->snd_kctl_mute = kctl; |
---|
| 1586 | + prm->mute = 0; |
---|
| 1587 | + } |
---|
| 1588 | + |
---|
| 1589 | + if (fu->volume_present) { |
---|
| 1590 | + snprintf(ctrl_name, sizeof(ctrl_name), |
---|
| 1591 | + "PCM %s Volume", direction); |
---|
| 1592 | + |
---|
| 1593 | + u_audio_controls[UAC_VOLUME_CTRL].name = ctrl_name; |
---|
| 1594 | + |
---|
| 1595 | + kctl = snd_ctl_new1(&u_audio_controls[UAC_VOLUME_CTRL], |
---|
| 1596 | + prm); |
---|
| 1597 | + if (!kctl) { |
---|
| 1598 | + err = -ENOMEM; |
---|
| 1599 | + goto snd_fail; |
---|
| 1600 | + } |
---|
| 1601 | + |
---|
| 1602 | + kctl->id.device = pcm->device; |
---|
| 1603 | + kctl->id.subdevice = 0; |
---|
| 1604 | + |
---|
| 1605 | + |
---|
| 1606 | + kctl->tlv.c = u_audio_volume_tlv; |
---|
| 1607 | + kctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_TLV_READ | |
---|
| 1608 | + SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK; |
---|
| 1609 | + |
---|
| 1610 | + err = snd_ctl_add(card, kctl); |
---|
| 1611 | + if (err < 0) |
---|
| 1612 | + goto snd_fail; |
---|
| 1613 | + prm->snd_kctl_volume = kctl; |
---|
| 1614 | + prm->volume = fu->volume_max; |
---|
| 1615 | + prm->volume_max = fu->volume_max; |
---|
| 1616 | + prm->volume_min = fu->volume_min; |
---|
| 1617 | + prm->volume_res = fu->volume_res; |
---|
| 1618 | + } |
---|
| 1619 | + |
---|
| 1620 | + /* Add rate control */ |
---|
| 1621 | + snprintf(ctrl_name, sizeof(ctrl_name), |
---|
| 1622 | + "%s Rate", direction); |
---|
| 1623 | + u_audio_controls[UAC_RATE_CTRL].name = ctrl_name; |
---|
| 1624 | + |
---|
| 1625 | + kctl = snd_ctl_new1(&u_audio_controls[UAC_RATE_CTRL], prm); |
---|
| 1626 | + if (!kctl) { |
---|
| 1627 | + err = -ENOMEM; |
---|
| 1628 | + goto snd_fail; |
---|
| 1629 | + } |
---|
| 1630 | + |
---|
| 1631 | + kctl->id.device = pcm->device; |
---|
| 1632 | + kctl->id.subdevice = 0; |
---|
| 1633 | + |
---|
| 1634 | + err = snd_ctl_add(card, kctl); |
---|
| 1635 | + if (err < 0) |
---|
| 1636 | + goto snd_fail; |
---|
| 1637 | + prm->snd_kctl_rate = kctl; |
---|
| 1638 | + } |
---|
| 1639 | + |
---|
| 1640 | + strscpy(card->driver, card_name, sizeof(card->driver)); |
---|
| 1641 | + strscpy(card->shortname, card_name, sizeof(card->shortname)); |
---|
| 1642 | + sprintf(card->longname, "%s %i", card_name, card->dev->id); |
---|
| 1643 | + |
---|
| 1644 | + snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS, |
---|
| 1645 | + NULL, 0, BUFF_SIZE_MAX); |
---|
1005 | 1646 | |
---|
1006 | 1647 | err = snd_card_register(card); |
---|
1007 | 1648 | if (err < 0) |
---|
.. | .. |
---|
1024 | 1665 | snd_fail: |
---|
1025 | 1666 | snd_card_free(card); |
---|
1026 | 1667 | fail: |
---|
1027 | | - kfree(uac->p_prm.ureq); |
---|
1028 | | - kfree(uac->c_prm.ureq); |
---|
| 1668 | + kfree(uac->p_prm.reqs); |
---|
| 1669 | + kfree(uac->c_prm.reqs); |
---|
1029 | 1670 | kfree(uac->p_prm.rbuf); |
---|
1030 | 1671 | kfree(uac->c_prm.rbuf); |
---|
1031 | 1672 | kfree(uac); |
---|
.. | .. |
---|
1053 | 1694 | if (card) |
---|
1054 | 1695 | snd_card_free(card); |
---|
1055 | 1696 | |
---|
1056 | | - free_ep(&uac->c_prm, g_audio->out_ep); |
---|
1057 | | - free_ep(&uac->p_prm, g_audio->in_ep); |
---|
1058 | | - |
---|
1059 | | - kfree(uac->p_prm.ureq); |
---|
1060 | | - kfree(uac->c_prm.ureq); |
---|
| 1697 | + kfree(uac->p_prm.reqs); |
---|
| 1698 | + kfree(uac->c_prm.reqs); |
---|
1061 | 1699 | kfree(uac->p_prm.rbuf); |
---|
1062 | 1700 | kfree(uac->c_prm.rbuf); |
---|
1063 | 1701 | kfree(uac); |
---|