diff options
author | Daniel Mack <zonque@gmail.com> | 2012-04-12 07:51:13 -0400 |
---|---|---|
committer | Takashi Iwai <tiwai@suse.de> | 2012-04-13 04:24:23 -0400 |
commit | d399ff9593e088d33fb38f5206c6427825892baa (patch) | |
tree | 2d4423a2ab71bce37e4df82a379f1908e0e0602f | |
parent | edcd3633e72a1590c4cf46befe5e6cd03b5aec3e (diff) |
ALSA: snd-usb: remove old streaming logic
Signed-off-by: Daniel Mack <zonque@gmail.com>
Signed-off-by: Takashi Iwai <tiwai@suse.de>
-rw-r--r-- | sound/usb/endpoint.c | 850 | ||||
-rw-r--r-- | sound/usb/endpoint.h | 15 |
2 files changed, 6 insertions, 859 deletions
diff --git a/sound/usb/endpoint.c b/sound/usb/endpoint.c index 1b0ed22cff73..64853f76c10c 100644 --- a/sound/usb/endpoint.c +++ b/sound/usb/endpoint.c | |||
@@ -54,727 +54,16 @@ static inline unsigned get_usb_high_speed_rate(unsigned int rate) | |||
54 | } | 54 | } |
55 | 55 | ||
56 | /* | 56 | /* |
57 | * unlink active urbs. | ||
58 | */ | ||
59 | static int deactivate_urbs_old(struct snd_usb_substream *subs, int force, int can_sleep) | ||
60 | { | ||
61 | struct snd_usb_audio *chip = subs->stream->chip; | ||
62 | unsigned int i; | ||
63 | int async; | ||
64 | |||
65 | subs->running = 0; | ||
66 | |||
67 | if (!force && subs->stream->chip->shutdown) /* to be sure... */ | ||
68 | return -EBADFD; | ||
69 | |||
70 | async = !can_sleep && chip->async_unlink; | ||
71 | |||
72 | if (!async && in_interrupt()) | ||
73 | return 0; | ||
74 | |||
75 | for (i = 0; i < subs->nurbs; i++) { | ||
76 | if (test_bit(i, &subs->active_mask)) { | ||
77 | if (!test_and_set_bit(i, &subs->unlink_mask)) { | ||
78 | struct urb *u = subs->dataurb[i].urb; | ||
79 | if (async) | ||
80 | usb_unlink_urb(u); | ||
81 | else | ||
82 | usb_kill_urb(u); | ||
83 | } | ||
84 | } | ||
85 | } | ||
86 | if (subs->syncpipe) { | ||
87 | for (i = 0; i < SYNC_URBS; i++) { | ||
88 | if (test_bit(i+16, &subs->active_mask)) { | ||
89 | if (!test_and_set_bit(i+16, &subs->unlink_mask)) { | ||
90 | struct urb *u = subs->syncurb[i].urb; | ||
91 | if (async) | ||
92 | usb_unlink_urb(u); | ||
93 | else | ||
94 | usb_kill_urb(u); | ||
95 | } | ||
96 | } | ||
97 | } | ||
98 | } | ||
99 | return 0; | ||
100 | } | ||
101 | |||
102 | |||
103 | /* | ||
104 | * release a urb data | 57 | * release a urb data |
105 | */ | 58 | */ |
106 | static void release_urb_ctx(struct snd_urb_ctx *u) | 59 | static void release_urb_ctx(struct snd_urb_ctx *u) |
107 | { | 60 | { |
108 | if (u->urb) { | 61 | if (u->buffer_size) |
109 | if (u->buffer_size) | 62 | usb_free_coherent(u->ep->chip->dev, u->buffer_size, |
110 | usb_free_coherent(u->subs->dev, u->buffer_size, | 63 | u->urb->transfer_buffer, |
111 | u->urb->transfer_buffer, | 64 | u->urb->transfer_dma); |
112 | u->urb->transfer_dma); | 65 | usb_free_urb(u->urb); |
113 | usb_free_urb(u->urb); | 66 | u->urb = NULL; |
114 | u->urb = NULL; | ||
115 | } | ||
116 | } | ||
117 | |||
118 | /* | ||
119 | * wait until all urbs are processed. | ||
120 | */ | ||
121 | static int wait_clear_urbs_old(struct snd_usb_substream *subs) | ||
122 | { | ||
123 | unsigned long end_time = jiffies + msecs_to_jiffies(1000); | ||
124 | unsigned int i; | ||
125 | int alive; | ||
126 | |||
127 | do { | ||
128 | alive = 0; | ||
129 | for (i = 0; i < subs->nurbs; i++) { | ||
130 | if (test_bit(i, &subs->active_mask)) | ||
131 | alive++; | ||
132 | } | ||
133 | if (subs->syncpipe) { | ||
134 | for (i = 0; i < SYNC_URBS; i++) { | ||
135 | if (test_bit(i + 16, &subs->active_mask)) | ||
136 | alive++; | ||
137 | } | ||
138 | } | ||
139 | if (! alive) | ||
140 | break; | ||
141 | schedule_timeout_uninterruptible(1); | ||
142 | } while (time_before(jiffies, end_time)); | ||
143 | if (alive) | ||
144 | snd_printk(KERN_ERR "timeout: still %d active urbs..\n", alive); | ||
145 | return 0; | ||
146 | } | ||
147 | |||
148 | /* | ||
149 | * release a substream | ||
150 | */ | ||
151 | void snd_usb_release_substream_urbs(struct snd_usb_substream *subs, int force) | ||
152 | { | ||
153 | int i; | ||
154 | |||
155 | /* stop urbs (to be sure) */ | ||
156 | deactivate_urbs_old(subs, force, 1); | ||
157 | wait_clear_urbs_old(subs); | ||
158 | |||
159 | for (i = 0; i < MAX_URBS; i++) | ||
160 | release_urb_ctx(&subs->dataurb[i]); | ||
161 | for (i = 0; i < SYNC_URBS; i++) | ||
162 | release_urb_ctx(&subs->syncurb[i]); | ||
163 | usb_free_coherent(subs->dev, SYNC_URBS * 4, | ||
164 | subs->syncbuf, subs->sync_dma); | ||
165 | subs->syncbuf = NULL; | ||
166 | subs->nurbs = 0; | ||
167 | } | ||
168 | |||
169 | /* | ||
170 | * complete callback from data urb | ||
171 | */ | ||
172 | static void snd_complete_urb_old(struct urb *urb) | ||
173 | { | ||
174 | struct snd_urb_ctx *ctx = urb->context; | ||
175 | struct snd_usb_substream *subs = ctx->subs; | ||
176 | struct snd_pcm_substream *substream = ctx->subs->pcm_substream; | ||
177 | int err = 0; | ||
178 | |||
179 | if ((subs->running && subs->ops.retire(subs, substream->runtime, urb)) || | ||
180 | !subs->running || /* can be stopped during retire callback */ | ||
181 | (err = subs->ops.prepare(subs, substream->runtime, urb)) < 0 || | ||
182 | (err = usb_submit_urb(urb, GFP_ATOMIC)) < 0) { | ||
183 | clear_bit(ctx->index, &subs->active_mask); | ||
184 | if (err < 0) { | ||
185 | snd_printd(KERN_ERR "cannot submit urb (err = %d)\n", err); | ||
186 | snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN); | ||
187 | } | ||
188 | } | ||
189 | } | ||
190 | |||
191 | |||
192 | /* | ||
193 | * complete callback from sync urb | ||
194 | */ | ||
195 | static void snd_complete_sync_urb(struct urb *urb) | ||
196 | { | ||
197 | struct snd_urb_ctx *ctx = urb->context; | ||
198 | struct snd_usb_substream *subs = ctx->subs; | ||
199 | struct snd_pcm_substream *substream = ctx->subs->pcm_substream; | ||
200 | int err = 0; | ||
201 | |||
202 | if ((subs->running && subs->ops.retire_sync(subs, substream->runtime, urb)) || | ||
203 | !subs->running || /* can be stopped during retire callback */ | ||
204 | (err = subs->ops.prepare_sync(subs, substream->runtime, urb)) < 0 || | ||
205 | (err = usb_submit_urb(urb, GFP_ATOMIC)) < 0) { | ||
206 | clear_bit(ctx->index + 16, &subs->active_mask); | ||
207 | if (err < 0) { | ||
208 | snd_printd(KERN_ERR "cannot submit sync urb (err = %d)\n", err); | ||
209 | snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN); | ||
210 | } | ||
211 | } | ||
212 | } | ||
213 | |||
214 | |||
215 | /* | ||
216 | * initialize a substream for plaback/capture | ||
217 | */ | ||
218 | int snd_usb_init_substream_urbs(struct snd_usb_substream *subs, | ||
219 | unsigned int period_bytes, | ||
220 | unsigned int rate, | ||
221 | unsigned int frame_bits) | ||
222 | { | ||
223 | unsigned int maxsize, i; | ||
224 | int is_playback = subs->direction == SNDRV_PCM_STREAM_PLAYBACK; | ||
225 | unsigned int urb_packs, total_packs, packs_per_ms; | ||
226 | struct snd_usb_audio *chip = subs->stream->chip; | ||
227 | |||
228 | /* calculate the frequency in 16.16 format */ | ||
229 | if (snd_usb_get_speed(subs->dev) == USB_SPEED_FULL) | ||
230 | subs->freqn = get_usb_full_speed_rate(rate); | ||
231 | else | ||
232 | subs->freqn = get_usb_high_speed_rate(rate); | ||
233 | subs->freqm = subs->freqn; | ||
234 | subs->freqshift = INT_MIN; | ||
235 | /* calculate max. frequency */ | ||
236 | if (subs->maxpacksize) { | ||
237 | /* whatever fits into a max. size packet */ | ||
238 | maxsize = subs->maxpacksize; | ||
239 | subs->freqmax = (maxsize / (frame_bits >> 3)) | ||
240 | << (16 - subs->datainterval); | ||
241 | } else { | ||
242 | /* no max. packet size: just take 25% higher than nominal */ | ||
243 | subs->freqmax = subs->freqn + (subs->freqn >> 2); | ||
244 | maxsize = ((subs->freqmax + 0xffff) * (frame_bits >> 3)) | ||
245 | >> (16 - subs->datainterval); | ||
246 | } | ||
247 | subs->phase = 0; | ||
248 | |||
249 | if (subs->fill_max) | ||
250 | subs->curpacksize = subs->maxpacksize; | ||
251 | else | ||
252 | subs->curpacksize = maxsize; | ||
253 | |||
254 | if (snd_usb_get_speed(subs->dev) != USB_SPEED_FULL) | ||
255 | packs_per_ms = 8 >> subs->datainterval; | ||
256 | else | ||
257 | packs_per_ms = 1; | ||
258 | |||
259 | if (is_playback) { | ||
260 | urb_packs = max(chip->nrpacks, 1); | ||
261 | urb_packs = min(urb_packs, (unsigned int)MAX_PACKS); | ||
262 | } else | ||
263 | urb_packs = 1; | ||
264 | urb_packs *= packs_per_ms; | ||
265 | if (subs->syncpipe) | ||
266 | urb_packs = min(urb_packs, 1U << subs->syncinterval); | ||
267 | |||
268 | /* decide how many packets to be used */ | ||
269 | if (is_playback) { | ||
270 | unsigned int minsize, maxpacks; | ||
271 | /* determine how small a packet can be */ | ||
272 | minsize = (subs->freqn >> (16 - subs->datainterval)) | ||
273 | * (frame_bits >> 3); | ||
274 | /* with sync from device, assume it can be 12% lower */ | ||
275 | if (subs->syncpipe) | ||
276 | minsize -= minsize >> 3; | ||
277 | minsize = max(minsize, 1u); | ||
278 | total_packs = (period_bytes + minsize - 1) / minsize; | ||
279 | /* we need at least two URBs for queueing */ | ||
280 | if (total_packs < 2) { | ||
281 | total_packs = 2; | ||
282 | } else { | ||
283 | /* and we don't want too long a queue either */ | ||
284 | maxpacks = max(MAX_QUEUE * packs_per_ms, urb_packs * 2); | ||
285 | total_packs = min(total_packs, maxpacks); | ||
286 | } | ||
287 | } else { | ||
288 | while (urb_packs > 1 && urb_packs * maxsize >= period_bytes) | ||
289 | urb_packs >>= 1; | ||
290 | total_packs = MAX_URBS * urb_packs; | ||
291 | } | ||
292 | subs->nurbs = (total_packs + urb_packs - 1) / urb_packs; | ||
293 | if (subs->nurbs > MAX_URBS) { | ||
294 | /* too much... */ | ||
295 | subs->nurbs = MAX_URBS; | ||
296 | total_packs = MAX_URBS * urb_packs; | ||
297 | } else if (subs->nurbs < 2) { | ||
298 | /* too little - we need at least two packets | ||
299 | * to ensure contiguous playback/capture | ||
300 | */ | ||
301 | subs->nurbs = 2; | ||
302 | } | ||
303 | |||
304 | /* allocate and initialize data urbs */ | ||
305 | for (i = 0; i < subs->nurbs; i++) { | ||
306 | struct snd_urb_ctx *u = &subs->dataurb[i]; | ||
307 | u->index = i; | ||
308 | u->subs = subs; | ||
309 | u->packets = (i + 1) * total_packs / subs->nurbs | ||
310 | - i * total_packs / subs->nurbs; | ||
311 | u->buffer_size = maxsize * u->packets; | ||
312 | if (subs->fmt_type == UAC_FORMAT_TYPE_II) | ||
313 | u->packets++; /* for transfer delimiter */ | ||
314 | u->urb = usb_alloc_urb(u->packets, GFP_KERNEL); | ||
315 | if (!u->urb) | ||
316 | goto out_of_memory; | ||
317 | u->urb->transfer_buffer = | ||
318 | usb_alloc_coherent(subs->dev, u->buffer_size, | ||
319 | GFP_KERNEL, &u->urb->transfer_dma); | ||
320 | if (!u->urb->transfer_buffer) | ||
321 | goto out_of_memory; | ||
322 | u->urb->pipe = subs->datapipe; | ||
323 | u->urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP; | ||
324 | u->urb->interval = 1 << subs->datainterval; | ||
325 | u->urb->context = u; | ||
326 | u->urb->complete = snd_complete_urb_old; | ||
327 | } | ||
328 | |||
329 | if (subs->syncpipe) { | ||
330 | /* allocate and initialize sync urbs */ | ||
331 | subs->syncbuf = usb_alloc_coherent(subs->dev, SYNC_URBS * 4, | ||
332 | GFP_KERNEL, &subs->sync_dma); | ||
333 | if (!subs->syncbuf) | ||
334 | goto out_of_memory; | ||
335 | for (i = 0; i < SYNC_URBS; i++) { | ||
336 | struct snd_urb_ctx *u = &subs->syncurb[i]; | ||
337 | u->index = i; | ||
338 | u->subs = subs; | ||
339 | u->packets = 1; | ||
340 | u->urb = usb_alloc_urb(1, GFP_KERNEL); | ||
341 | if (!u->urb) | ||
342 | goto out_of_memory; | ||
343 | u->urb->transfer_buffer = subs->syncbuf + i * 4; | ||
344 | u->urb->transfer_dma = subs->sync_dma + i * 4; | ||
345 | u->urb->transfer_buffer_length = 4; | ||
346 | u->urb->pipe = subs->syncpipe; | ||
347 | u->urb->transfer_flags = URB_ISO_ASAP | | ||
348 | URB_NO_TRANSFER_DMA_MAP; | ||
349 | u->urb->number_of_packets = 1; | ||
350 | u->urb->interval = 1 << subs->syncinterval; | ||
351 | u->urb->context = u; | ||
352 | u->urb->complete = snd_complete_sync_urb; | ||
353 | } | ||
354 | } | ||
355 | return 0; | ||
356 | |||
357 | out_of_memory: | ||
358 | snd_usb_release_substream_urbs(subs, 0); | ||
359 | return -ENOMEM; | ||
360 | } | ||
361 | |||
362 | /* | ||
363 | * prepare urb for full speed capture sync pipe | ||
364 | * | ||
365 | * fill the length and offset of each urb descriptor. | ||
366 | * the fixed 10.14 frequency is passed through the pipe. | ||
367 | */ | ||
368 | static int prepare_capture_sync_urb(struct snd_usb_substream *subs, | ||
369 | struct snd_pcm_runtime *runtime, | ||
370 | struct urb *urb) | ||
371 | { | ||
372 | unsigned char *cp = urb->transfer_buffer; | ||
373 | struct snd_urb_ctx *ctx = urb->context; | ||
374 | |||
375 | urb->dev = ctx->subs->dev; /* we need to set this at each time */ | ||
376 | urb->iso_frame_desc[0].length = 3; | ||
377 | urb->iso_frame_desc[0].offset = 0; | ||
378 | cp[0] = subs->freqn >> 2; | ||
379 | cp[1] = subs->freqn >> 10; | ||
380 | cp[2] = subs->freqn >> 18; | ||
381 | return 0; | ||
382 | } | ||
383 | |||
384 | /* | ||
385 | * prepare urb for high speed capture sync pipe | ||
386 | * | ||
387 | * fill the length and offset of each urb descriptor. | ||
388 | * the fixed 12.13 frequency is passed as 16.16 through the pipe. | ||
389 | */ | ||
390 | static int prepare_capture_sync_urb_hs(struct snd_usb_substream *subs, | ||
391 | struct snd_pcm_runtime *runtime, | ||
392 | struct urb *urb) | ||
393 | { | ||
394 | unsigned char *cp = urb->transfer_buffer; | ||
395 | struct snd_urb_ctx *ctx = urb->context; | ||
396 | |||
397 | urb->dev = ctx->subs->dev; /* we need to set this at each time */ | ||
398 | urb->iso_frame_desc[0].length = 4; | ||
399 | urb->iso_frame_desc[0].offset = 0; | ||
400 | cp[0] = subs->freqn; | ||
401 | cp[1] = subs->freqn >> 8; | ||
402 | cp[2] = subs->freqn >> 16; | ||
403 | cp[3] = subs->freqn >> 24; | ||
404 | return 0; | ||
405 | } | ||
406 | |||
407 | /* | ||
408 | * process after capture sync complete | ||
409 | * - nothing to do | ||
410 | */ | ||
411 | static int retire_capture_sync_urb(struct snd_usb_substream *subs, | ||
412 | struct snd_pcm_runtime *runtime, | ||
413 | struct urb *urb) | ||
414 | { | ||
415 | return 0; | ||
416 | } | ||
417 | |||
418 | /* | ||
419 | * prepare urb for capture data pipe | ||
420 | * | ||
421 | * fill the offset and length of each descriptor. | ||
422 | * | ||
423 | * we use a temporary buffer to write the captured data. | ||
424 | * since the length of written data is determined by host, we cannot | ||
425 | * write onto the pcm buffer directly... the data is thus copied | ||
426 | * later at complete callback to the global buffer. | ||
427 | */ | ||
428 | static int prepare_capture_urb(struct snd_usb_substream *subs, | ||
429 | struct snd_pcm_runtime *runtime, | ||
430 | struct urb *urb) | ||
431 | { | ||
432 | int i, offs; | ||
433 | struct snd_urb_ctx *ctx = urb->context; | ||
434 | |||
435 | offs = 0; | ||
436 | urb->dev = ctx->subs->dev; /* we need to set this at each time */ | ||
437 | for (i = 0; i < ctx->packets; i++) { | ||
438 | urb->iso_frame_desc[i].offset = offs; | ||
439 | urb->iso_frame_desc[i].length = subs->curpacksize; | ||
440 | offs += subs->curpacksize; | ||
441 | } | ||
442 | urb->transfer_buffer_length = offs; | ||
443 | urb->number_of_packets = ctx->packets; | ||
444 | return 0; | ||
445 | } | ||
446 | |||
447 | /* | ||
448 | * process after capture complete | ||
449 | * | ||
450 | * copy the data from each desctiptor to the pcm buffer, and | ||
451 | * update the current position. | ||
452 | */ | ||
453 | static int retire_capture_urb(struct snd_usb_substream *subs, | ||
454 | struct snd_pcm_runtime *runtime, | ||
455 | struct urb *urb) | ||
456 | { | ||
457 | unsigned long flags; | ||
458 | unsigned char *cp; | ||
459 | int i; | ||
460 | unsigned int stride, frames, bytes, oldptr; | ||
461 | int period_elapsed = 0; | ||
462 | |||
463 | stride = runtime->frame_bits >> 3; | ||
464 | |||
465 | for (i = 0; i < urb->number_of_packets; i++) { | ||
466 | cp = (unsigned char *)urb->transfer_buffer + urb->iso_frame_desc[i].offset; | ||
467 | if (urb->iso_frame_desc[i].status && printk_ratelimit()) { | ||
468 | snd_printdd("frame %d active: %d\n", i, urb->iso_frame_desc[i].status); | ||
469 | // continue; | ||
470 | } | ||
471 | bytes = urb->iso_frame_desc[i].actual_length; | ||
472 | frames = bytes / stride; | ||
473 | if (!subs->txfr_quirk) | ||
474 | bytes = frames * stride; | ||
475 | if (bytes % (runtime->sample_bits >> 3) != 0) { | ||
476 | #ifdef CONFIG_SND_DEBUG_VERBOSE | ||
477 | int oldbytes = bytes; | ||
478 | #endif | ||
479 | bytes = frames * stride; | ||
480 | snd_printdd(KERN_ERR "Corrected urb data len. %d->%d\n", | ||
481 | oldbytes, bytes); | ||
482 | } | ||
483 | /* update the current pointer */ | ||
484 | spin_lock_irqsave(&subs->lock, flags); | ||
485 | oldptr = subs->hwptr_done; | ||
486 | subs->hwptr_done += bytes; | ||
487 | if (subs->hwptr_done >= runtime->buffer_size * stride) | ||
488 | subs->hwptr_done -= runtime->buffer_size * stride; | ||
489 | frames = (bytes + (oldptr % stride)) / stride; | ||
490 | subs->transfer_done += frames; | ||
491 | if (subs->transfer_done >= runtime->period_size) { | ||
492 | subs->transfer_done -= runtime->period_size; | ||
493 | period_elapsed = 1; | ||
494 | } | ||
495 | spin_unlock_irqrestore(&subs->lock, flags); | ||
496 | /* copy a data chunk */ | ||
497 | if (oldptr + bytes > runtime->buffer_size * stride) { | ||
498 | unsigned int bytes1 = | ||
499 | runtime->buffer_size * stride - oldptr; | ||
500 | memcpy(runtime->dma_area + oldptr, cp, bytes1); | ||
501 | memcpy(runtime->dma_area, cp + bytes1, bytes - bytes1); | ||
502 | } else { | ||
503 | memcpy(runtime->dma_area + oldptr, cp, bytes); | ||
504 | } | ||
505 | } | ||
506 | if (period_elapsed) | ||
507 | snd_pcm_period_elapsed(subs->pcm_substream); | ||
508 | return 0; | ||
509 | } | ||
510 | |||
511 | /* | ||
512 | * Process after capture complete when paused. Nothing to do. | ||
513 | */ | ||
514 | static int retire_paused_capture_urb(struct snd_usb_substream *subs, | ||
515 | struct snd_pcm_runtime *runtime, | ||
516 | struct urb *urb) | ||
517 | { | ||
518 | return 0; | ||
519 | } | ||
520 | |||
521 | |||
522 | /* | ||
523 | * prepare urb for playback sync pipe | ||
524 | * | ||
525 | * set up the offset and length to receive the current frequency. | ||
526 | */ | ||
527 | static int prepare_playback_sync_urb(struct snd_usb_substream *subs, | ||
528 | struct snd_pcm_runtime *runtime, | ||
529 | struct urb *urb) | ||
530 | { | ||
531 | struct snd_urb_ctx *ctx = urb->context; | ||
532 | |||
533 | urb->dev = ctx->subs->dev; /* we need to set this at each time */ | ||
534 | urb->iso_frame_desc[0].length = min(4u, ctx->subs->syncmaxsize); | ||
535 | urb->iso_frame_desc[0].offset = 0; | ||
536 | return 0; | ||
537 | } | ||
538 | |||
539 | /* | ||
540 | * process after playback sync complete | ||
541 | * | ||
542 | * Full speed devices report feedback values in 10.14 format as samples per | ||
543 | * frame, high speed devices in 16.16 format as samples per microframe. | ||
544 | * Because the Audio Class 1 spec was written before USB 2.0, many high speed | ||
545 | * devices use a wrong interpretation, some others use an entirely different | ||
546 | * format. Therefore, we cannot predict what format any particular device uses | ||
547 | * and must detect it automatically. | ||
548 | */ | ||
549 | static int retire_playback_sync_urb(struct snd_usb_substream *subs, | ||
550 | struct snd_pcm_runtime *runtime, | ||
551 | struct urb *urb) | ||
552 | { | ||
553 | unsigned int f; | ||
554 | int shift; | ||
555 | unsigned long flags; | ||
556 | |||
557 | if (urb->iso_frame_desc[0].status != 0 || | ||
558 | urb->iso_frame_desc[0].actual_length < 3) | ||
559 | return 0; | ||
560 | |||
561 | f = le32_to_cpup(urb->transfer_buffer); | ||
562 | if (urb->iso_frame_desc[0].actual_length == 3) | ||
563 | f &= 0x00ffffff; | ||
564 | else | ||
565 | f &= 0x0fffffff; | ||
566 | if (f == 0) | ||
567 | return 0; | ||
568 | |||
569 | if (unlikely(subs->freqshift == INT_MIN)) { | ||
570 | /* | ||
571 | * The first time we see a feedback value, determine its format | ||
572 | * by shifting it left or right until it matches the nominal | ||
573 | * frequency value. This assumes that the feedback does not | ||
574 | * differ from the nominal value more than +50% or -25%. | ||
575 | */ | ||
576 | shift = 0; | ||
577 | while (f < subs->freqn - subs->freqn / 4) { | ||
578 | f <<= 1; | ||
579 | shift++; | ||
580 | } | ||
581 | while (f > subs->freqn + subs->freqn / 2) { | ||
582 | f >>= 1; | ||
583 | shift--; | ||
584 | } | ||
585 | subs->freqshift = shift; | ||
586 | } | ||
587 | else if (subs->freqshift >= 0) | ||
588 | f <<= subs->freqshift; | ||
589 | else | ||
590 | f >>= -subs->freqshift; | ||
591 | |||
592 | if (likely(f >= subs->freqn - subs->freqn / 8 && f <= subs->freqmax)) { | ||
593 | /* | ||
594 | * If the frequency looks valid, set it. | ||
595 | * This value is referred to in prepare_playback_urb(). | ||
596 | */ | ||
597 | spin_lock_irqsave(&subs->lock, flags); | ||
598 | subs->freqm = f; | ||
599 | spin_unlock_irqrestore(&subs->lock, flags); | ||
600 | } else { | ||
601 | /* | ||
602 | * Out of range; maybe the shift value is wrong. | ||
603 | * Reset it so that we autodetect again the next time. | ||
604 | */ | ||
605 | subs->freqshift = INT_MIN; | ||
606 | } | ||
607 | |||
608 | return 0; | ||
609 | } | ||
610 | |||
611 | /* determine the number of frames in the next packet */ | ||
612 | static int snd_usb_audio_next_packet_size(struct snd_usb_substream *subs) | ||
613 | { | ||
614 | if (subs->fill_max) | ||
615 | return subs->maxframesize; | ||
616 | else { | ||
617 | subs->phase = (subs->phase & 0xffff) | ||
618 | + (subs->freqm << subs->datainterval); | ||
619 | return min(subs->phase >> 16, subs->maxframesize); | ||
620 | } | ||
621 | } | ||
622 | |||
623 | /* | ||
624 | * Prepare urb for streaming before playback starts or when paused. | ||
625 | * | ||
626 | * We don't have any data, so we send silence. | ||
627 | */ | ||
628 | static int prepare_nodata_playback_urb(struct snd_usb_substream *subs, | ||
629 | struct snd_pcm_runtime *runtime, | ||
630 | struct urb *urb) | ||
631 | { | ||
632 | unsigned int i, offs, counts; | ||
633 | struct snd_urb_ctx *ctx = urb->context; | ||
634 | int stride = runtime->frame_bits >> 3; | ||
635 | |||
636 | offs = 0; | ||
637 | urb->dev = ctx->subs->dev; | ||
638 | for (i = 0; i < ctx->packets; ++i) { | ||
639 | counts = snd_usb_audio_next_packet_size(subs); | ||
640 | urb->iso_frame_desc[i].offset = offs * stride; | ||
641 | urb->iso_frame_desc[i].length = counts * stride; | ||
642 | offs += counts; | ||
643 | } | ||
644 | urb->number_of_packets = ctx->packets; | ||
645 | urb->transfer_buffer_length = offs * stride; | ||
646 | memset(urb->transfer_buffer, | ||
647 | runtime->format == SNDRV_PCM_FORMAT_U8 ? 0x80 : 0, | ||
648 | offs * stride); | ||
649 | return 0; | ||
650 | } | ||
651 | |||
652 | /* | ||
653 | * prepare urb for playback data pipe | ||
654 | * | ||
655 | * Since a URB can handle only a single linear buffer, we must use double | ||
656 | * buffering when the data to be transferred overflows the buffer boundary. | ||
657 | * To avoid inconsistencies when updating hwptr_done, we use double buffering | ||
658 | * for all URBs. | ||
659 | */ | ||
660 | static int prepare_playback_urb(struct snd_usb_substream *subs, | ||
661 | struct snd_pcm_runtime *runtime, | ||
662 | struct urb *urb) | ||
663 | { | ||
664 | int i, stride; | ||
665 | unsigned int counts, frames, bytes; | ||
666 | unsigned long flags; | ||
667 | int period_elapsed = 0; | ||
668 | struct snd_urb_ctx *ctx = urb->context; | ||
669 | |||
670 | stride = runtime->frame_bits >> 3; | ||
671 | |||
672 | frames = 0; | ||
673 | urb->dev = ctx->subs->dev; /* we need to set this at each time */ | ||
674 | urb->number_of_packets = 0; | ||
675 | spin_lock_irqsave(&subs->lock, flags); | ||
676 | for (i = 0; i < ctx->packets; i++) { | ||
677 | counts = snd_usb_audio_next_packet_size(subs); | ||
678 | /* set up descriptor */ | ||
679 | urb->iso_frame_desc[i].offset = frames * stride; | ||
680 | urb->iso_frame_desc[i].length = counts * stride; | ||
681 | frames += counts; | ||
682 | urb->number_of_packets++; | ||
683 | subs->transfer_done += counts; | ||
684 | if (subs->transfer_done >= runtime->period_size) { | ||
685 | subs->transfer_done -= runtime->period_size; | ||
686 | period_elapsed = 1; | ||
687 | if (subs->fmt_type == UAC_FORMAT_TYPE_II) { | ||
688 | if (subs->transfer_done > 0) { | ||
689 | /* FIXME: fill-max mode is not | ||
690 | * supported yet */ | ||
691 | frames -= subs->transfer_done; | ||
692 | counts -= subs->transfer_done; | ||
693 | urb->iso_frame_desc[i].length = | ||
694 | counts * stride; | ||
695 | subs->transfer_done = 0; | ||
696 | } | ||
697 | i++; | ||
698 | if (i < ctx->packets) { | ||
699 | /* add a transfer delimiter */ | ||
700 | urb->iso_frame_desc[i].offset = | ||
701 | frames * stride; | ||
702 | urb->iso_frame_desc[i].length = 0; | ||
703 | urb->number_of_packets++; | ||
704 | } | ||
705 | break; | ||
706 | } | ||
707 | } | ||
708 | if (period_elapsed) /* finish at the period boundary */ | ||
709 | break; | ||
710 | } | ||
711 | bytes = frames * stride; | ||
712 | if (subs->hwptr_done + bytes > runtime->buffer_size * stride) { | ||
713 | /* err, the transferred area goes over buffer boundary. */ | ||
714 | unsigned int bytes1 = | ||
715 | runtime->buffer_size * stride - subs->hwptr_done; | ||
716 | memcpy(urb->transfer_buffer, | ||
717 | runtime->dma_area + subs->hwptr_done, bytes1); | ||
718 | memcpy(urb->transfer_buffer + bytes1, | ||
719 | runtime->dma_area, bytes - bytes1); | ||
720 | } else { | ||
721 | memcpy(urb->transfer_buffer, | ||
722 | runtime->dma_area + subs->hwptr_done, bytes); | ||
723 | } | ||
724 | subs->hwptr_done += bytes; | ||
725 | if (subs->hwptr_done >= runtime->buffer_size * stride) | ||
726 | subs->hwptr_done -= runtime->buffer_size * stride; | ||
727 | |||
728 | /* update delay with exact number of samples queued */ | ||
729 | runtime->delay = subs->last_delay; | ||
730 | runtime->delay += frames; | ||
731 | subs->last_delay = runtime->delay; | ||
732 | |||
733 | /* realign last_frame_number */ | ||
734 | subs->last_frame_number = usb_get_current_frame_number(subs->dev); | ||
735 | subs->last_frame_number &= 0xFF; /* keep 8 LSBs */ | ||
736 | |||
737 | spin_unlock_irqrestore(&subs->lock, flags); | ||
738 | urb->transfer_buffer_length = bytes; | ||
739 | if (period_elapsed) | ||
740 | snd_pcm_period_elapsed(subs->pcm_substream); | ||
741 | return 0; | ||
742 | } | ||
743 | |||
744 | /* | ||
745 | * process after playback data complete | ||
746 | * - decrease the delay count again | ||
747 | */ | ||
748 | static int retire_playback_urb(struct snd_usb_substream *subs, | ||
749 | struct snd_pcm_runtime *runtime, | ||
750 | struct urb *urb) | ||
751 | { | ||
752 | unsigned long flags; | ||
753 | int stride = runtime->frame_bits >> 3; | ||
754 | int processed = urb->transfer_buffer_length / stride; | ||
755 | int est_delay; | ||
756 | |||
757 | spin_lock_irqsave(&subs->lock, flags); | ||
758 | |||
759 | est_delay = snd_usb_pcm_delay(subs, runtime->rate); | ||
760 | /* update delay with exact number of samples played */ | ||
761 | if (processed > subs->last_delay) | ||
762 | subs->last_delay = 0; | ||
763 | else | ||
764 | subs->last_delay -= processed; | ||
765 | runtime->delay = subs->last_delay; | ||
766 | |||
767 | /* | ||
768 | * Report when delay estimate is off by more than 2ms. | ||
769 | * The error should be lower than 2ms since the estimate relies | ||
770 | * on two reads of a counter updated every ms. | ||
771 | */ | ||
772 | if (abs(est_delay - subs->last_delay) * 1000 > runtime->rate * 2) | ||
773 | snd_printk(KERN_DEBUG "delay: estimated %d, actual %d\n", | ||
774 | est_delay, subs->last_delay); | ||
775 | |||
776 | spin_unlock_irqrestore(&subs->lock, flags); | ||
777 | return 0; | ||
778 | } | 67 | } |
779 | 68 | ||
780 | static const char *usb_error_string(int err) | 69 | static const char *usb_error_string(int err) |
@@ -802,133 +91,6 @@ static const char *usb_error_string(int err) | |||
802 | } | 91 | } |
803 | } | 92 | } |
804 | 93 | ||
805 | /* | ||
806 | * set up and start data/sync urbs | ||
807 | */ | ||
808 | static int start_urbs(struct snd_usb_substream *subs, struct snd_pcm_runtime *runtime) | ||
809 | { | ||
810 | unsigned int i; | ||
811 | int err; | ||
812 | |||
813 | if (subs->stream->chip->shutdown) | ||
814 | return -EBADFD; | ||
815 | |||
816 | for (i = 0; i < subs->nurbs; i++) { | ||
817 | if (snd_BUG_ON(!subs->dataurb[i].urb)) | ||
818 | return -EINVAL; | ||
819 | if (subs->ops.prepare(subs, runtime, subs->dataurb[i].urb) < 0) { | ||
820 | snd_printk(KERN_ERR "cannot prepare datapipe for urb %d\n", i); | ||
821 | goto __error; | ||
822 | } | ||
823 | } | ||
824 | if (subs->syncpipe) { | ||
825 | for (i = 0; i < SYNC_URBS; i++) { | ||
826 | if (snd_BUG_ON(!subs->syncurb[i].urb)) | ||
827 | return -EINVAL; | ||
828 | if (subs->ops.prepare_sync(subs, runtime, subs->syncurb[i].urb) < 0) { | ||
829 | snd_printk(KERN_ERR "cannot prepare syncpipe for urb %d\n", i); | ||
830 | goto __error; | ||
831 | } | ||
832 | } | ||
833 | } | ||
834 | |||
835 | subs->active_mask = 0; | ||
836 | subs->unlink_mask = 0; | ||
837 | subs->running = 1; | ||
838 | for (i = 0; i < subs->nurbs; i++) { | ||
839 | err = usb_submit_urb(subs->dataurb[i].urb, GFP_ATOMIC); | ||
840 | if (err < 0) { | ||
841 | snd_printk(KERN_ERR "cannot submit datapipe " | ||
842 | "for urb %d, error %d: %s\n", | ||
843 | i, err, usb_error_string(err)); | ||
844 | goto __error; | ||
845 | } | ||
846 | set_bit(i, &subs->active_mask); | ||
847 | } | ||
848 | if (subs->syncpipe) { | ||
849 | for (i = 0; i < SYNC_URBS; i++) { | ||
850 | err = usb_submit_urb(subs->syncurb[i].urb, GFP_ATOMIC); | ||
851 | if (err < 0) { | ||
852 | snd_printk(KERN_ERR "cannot submit syncpipe " | ||
853 | "for urb %d, error %d: %s\n", | ||
854 | i, err, usb_error_string(err)); | ||
855 | goto __error; | ||
856 | } | ||
857 | set_bit(i + 16, &subs->active_mask); | ||
858 | } | ||
859 | } | ||
860 | return 0; | ||
861 | |||
862 | __error: | ||
863 | // snd_pcm_stop(subs->pcm_substream, SNDRV_PCM_STATE_XRUN); | ||
864 | deactivate_urbs_old(subs, 0, 0); | ||
865 | return -EPIPE; | ||
866 | } | ||
867 | |||
868 | |||
869 | /* | ||
870 | */ | ||
871 | static struct snd_urb_ops audio_urb_ops[2] = { | ||
872 | { | ||
873 | .prepare = prepare_nodata_playback_urb, | ||
874 | .retire = retire_playback_urb, | ||
875 | .prepare_sync = prepare_playback_sync_urb, | ||
876 | .retire_sync = retire_playback_sync_urb, | ||
877 | }, | ||
878 | { | ||
879 | .prepare = prepare_capture_urb, | ||
880 | .retire = retire_capture_urb, | ||
881 | .prepare_sync = prepare_capture_sync_urb, | ||
882 | .retire_sync = retire_capture_sync_urb, | ||
883 | }, | ||
884 | }; | ||
885 | |||
886 | /* | ||
887 | * initialize the substream instance. | ||
888 | */ | ||
889 | |||
890 | void snd_usb_init_substream(struct snd_usb_stream *as, | ||
891 | int stream, struct audioformat *fp) | ||
892 | { | ||
893 | struct snd_usb_substream *subs = &as->substream[stream]; | ||
894 | |||
895 | INIT_LIST_HEAD(&subs->fmt_list); | ||
896 | spin_lock_init(&subs->lock); | ||
897 | |||
898 | subs->stream = as; | ||
899 | subs->direction = stream; | ||
900 | subs->dev = as->chip->dev; | ||
901 | subs->txfr_quirk = as->chip->txfr_quirk; | ||
902 | subs->ops = audio_urb_ops[stream]; | ||
903 | if (snd_usb_get_speed(subs->dev) >= USB_SPEED_HIGH) | ||
904 | subs->ops.prepare_sync = prepare_capture_sync_urb_hs; | ||
905 | |||
906 | snd_usb_set_pcm_ops(as->pcm, stream); | ||
907 | |||
908 | list_add_tail(&fp->list, &subs->fmt_list); | ||
909 | subs->formats |= fp->formats; | ||
910 | subs->endpoint = fp->endpoint; | ||
911 | subs->num_formats++; | ||
912 | subs->fmt_type = fp->fmt_type; | ||
913 | } | ||
914 | |||
915 | int snd_usb_substream_prepare(struct snd_usb_substream *subs, | ||
916 | struct snd_pcm_runtime *runtime) | ||
917 | { | ||
918 | /* clear urbs (to be sure) */ | ||
919 | deactivate_urbs_old(subs, 0, 1); | ||
920 | wait_clear_urbs_old(subs); | ||
921 | |||
922 | /* for playback, submit the URBs now; otherwise, the first hwptr_done | ||
923 | * updates for all URBs would happen at the same time when starting */ | ||
924 | if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK) { | ||
925 | subs->ops.prepare = prepare_nodata_playback_urb; | ||
926 | return start_urbs(subs, runtime); | ||
927 | } | ||
928 | |||
929 | return 0; | ||
930 | } | ||
931 | |||
932 | int snd_usb_endpoint_implict_feedback_sink(struct snd_usb_endpoint *ep) | 94 | int snd_usb_endpoint_implict_feedback_sink(struct snd_usb_endpoint *ep) |
933 | { | 95 | { |
934 | return ep->sync_master && | 96 | return ep->sync_master && |
diff --git a/sound/usb/endpoint.h b/sound/usb/endpoint.h index e540768de3d5..ee2723fb174f 100644 --- a/sound/usb/endpoint.h +++ b/sound/usb/endpoint.h | |||
@@ -1,21 +1,6 @@ | |||
1 | #ifndef __USBAUDIO_ENDPOINT_H | 1 | #ifndef __USBAUDIO_ENDPOINT_H |
2 | #define __USBAUDIO_ENDPOINT_H | 2 | #define __USBAUDIO_ENDPOINT_H |
3 | 3 | ||
4 | void snd_usb_init_substream(struct snd_usb_stream *as, | ||
5 | int stream, | ||
6 | struct audioformat *fp); | ||
7 | |||
8 | int snd_usb_init_substream_urbs(struct snd_usb_substream *subs, | ||
9 | unsigned int period_bytes, | ||
10 | unsigned int rate, | ||
11 | unsigned int frame_bits); | ||
12 | |||
13 | void snd_usb_release_substream_urbs(struct snd_usb_substream *subs, int force); | ||
14 | |||
15 | int snd_usb_substream_prepare(struct snd_usb_substream *subs, | ||
16 | struct snd_pcm_runtime *runtime); | ||
17 | |||
18 | |||
19 | #define SND_USB_ENDPOINT_TYPE_DATA 0 | 4 | #define SND_USB_ENDPOINT_TYPE_DATA 0 |
20 | #define SND_USB_ENDPOINT_TYPE_SYNC 1 | 5 | #define SND_USB_ENDPOINT_TYPE_SYNC 1 |
21 | 6 | ||