diff options
Diffstat (limited to 'sound/usb/endpoint.c')
-rw-r--r-- | sound/usb/endpoint.c | 1609 |
1 files changed, 902 insertions, 707 deletions
diff --git a/sound/usb/endpoint.c b/sound/usb/endpoint.c index 08dcce53720b..e6906901debb 100644 --- a/sound/usb/endpoint.c +++ b/sound/usb/endpoint.c | |||
@@ -20,9 +20,11 @@ | |||
20 | #include <linux/ratelimit.h> | 20 | #include <linux/ratelimit.h> |
21 | #include <linux/usb.h> | 21 | #include <linux/usb.h> |
22 | #include <linux/usb/audio.h> | 22 | #include <linux/usb/audio.h> |
23 | #include <linux/slab.h> | ||
23 | 24 | ||
24 | #include <sound/core.h> | 25 | #include <sound/core.h> |
25 | #include <sound/pcm.h> | 26 | #include <sound/pcm.h> |
27 | #include <sound/pcm_params.h> | ||
26 | 28 | ||
27 | #include "usbaudio.h" | 29 | #include "usbaudio.h" |
28 | #include "helper.h" | 30 | #include "helper.h" |
@@ -30,6 +32,36 @@ | |||
30 | #include "endpoint.h" | 32 | #include "endpoint.h" |
31 | #include "pcm.h" | 33 | #include "pcm.h" |
32 | 34 | ||
35 | #define EP_FLAG_ACTIVATED 0 | ||
36 | #define EP_FLAG_RUNNING 1 | ||
37 | |||
38 | /* | ||
39 | * snd_usb_endpoint is a model that abstracts everything related to an | ||
40 | * USB endpoint and its streaming. | ||
41 | * | ||
42 | * There are functions to activate and deactivate the streaming URBs and | ||
43 | * optional callbacks to let the pcm logic handle the actual content of the | ||
44 | * packets for playback and record. Thus, the bus streaming and the audio | ||
45 | * handlers are fully decoupled. | ||
46 | * | ||
47 | * There are two different types of endpoints in audio applications. | ||
48 | * | ||
49 | * SND_USB_ENDPOINT_TYPE_DATA handles full audio data payload for both | ||
50 | * inbound and outbound traffic. | ||
51 | * | ||
52 | * SND_USB_ENDPOINT_TYPE_SYNC endpoints are for inbound traffic only and | ||
53 | * expect the payload to carry Q10.14 / Q16.16 formatted sync information | ||
54 | * (3 or 4 bytes). | ||
55 | * | ||
56 | * Each endpoint has to be configured prior to being used by calling | ||
57 | * snd_usb_endpoint_set_params(). | ||
58 | * | ||
59 | * The model incorporates a reference counting, so that multiple users | ||
60 | * can call snd_usb_endpoint_start() and snd_usb_endpoint_stop(), and | ||
61 | * only the first user will effectively start the URBs, and only the last | ||
62 | * one to stop it will tear the URBs down again. | ||
63 | */ | ||
64 | |||
33 | /* | 65 | /* |
34 | * convert a sampling rate into our full speed format (fs/1000 in Q16.16) | 66 | * convert a sampling rate into our full speed format (fs/1000 in Q16.16) |
35 | * this will overflow at approx 524 kHz | 67 | * this will overflow at approx 524 kHz |
@@ -49,71 +81,415 @@ static inline unsigned get_usb_high_speed_rate(unsigned int rate) | |||
49 | } | 81 | } |
50 | 82 | ||
51 | /* | 83 | /* |
52 | * unlink active urbs. | 84 | * release a urb data |
53 | */ | 85 | */ |
54 | static int deactivate_urbs(struct snd_usb_substream *subs, int force, int can_sleep) | 86 | static void release_urb_ctx(struct snd_urb_ctx *u) |
55 | { | 87 | { |
56 | struct snd_usb_audio *chip = subs->stream->chip; | 88 | if (u->buffer_size) |
57 | unsigned int i; | 89 | usb_free_coherent(u->ep->chip->dev, u->buffer_size, |
58 | int async; | 90 | u->urb->transfer_buffer, |
91 | u->urb->transfer_dma); | ||
92 | usb_free_urb(u->urb); | ||
93 | u->urb = NULL; | ||
94 | } | ||
95 | |||
96 | static const char *usb_error_string(int err) | ||
97 | { | ||
98 | switch (err) { | ||
99 | case -ENODEV: | ||
100 | return "no device"; | ||
101 | case -ENOENT: | ||
102 | return "endpoint not enabled"; | ||
103 | case -EPIPE: | ||
104 | return "endpoint stalled"; | ||
105 | case -ENOSPC: | ||
106 | return "not enough bandwidth"; | ||
107 | case -ESHUTDOWN: | ||
108 | return "device disabled"; | ||
109 | case -EHOSTUNREACH: | ||
110 | return "device suspended"; | ||
111 | case -EINVAL: | ||
112 | case -EAGAIN: | ||
113 | case -EFBIG: | ||
114 | case -EMSGSIZE: | ||
115 | return "internal error"; | ||
116 | default: | ||
117 | return "unknown error"; | ||
118 | } | ||
119 | } | ||
120 | |||
121 | /** | ||
122 | * snd_usb_endpoint_implicit_feedback_sink: Report endpoint usage type | ||
123 | * | ||
124 | * @ep: The snd_usb_endpoint | ||
125 | * | ||
126 | * Determine whether an endpoint is driven by an implicit feedback | ||
127 | * data endpoint source. | ||
128 | */ | ||
129 | int snd_usb_endpoint_implict_feedback_sink(struct snd_usb_endpoint *ep) | ||
130 | { | ||
131 | return ep->sync_master && | ||
132 | ep->sync_master->type == SND_USB_ENDPOINT_TYPE_DATA && | ||
133 | ep->type == SND_USB_ENDPOINT_TYPE_DATA && | ||
134 | usb_pipeout(ep->pipe); | ||
135 | } | ||
59 | 136 | ||
60 | subs->running = 0; | 137 | /* |
138 | * For streaming based on information derived from sync endpoints, | ||
139 | * prepare_outbound_urb_sizes() will call next_packet_size() to | ||
140 | * determine the number of samples to be sent in the next packet. | ||
141 | * | ||
142 | * For implicit feedback, next_packet_size() is unused. | ||
143 | */ | ||
144 | static int next_packet_size(struct snd_usb_endpoint *ep) | ||
145 | { | ||
146 | unsigned long flags; | ||
147 | int ret; | ||
61 | 148 | ||
62 | if (!force && subs->stream->chip->shutdown) /* to be sure... */ | 149 | if (ep->fill_max) |
63 | return -EBADFD; | 150 | return ep->maxframesize; |
64 | 151 | ||
65 | async = !can_sleep && chip->async_unlink; | 152 | spin_lock_irqsave(&ep->lock, flags); |
153 | ep->phase = (ep->phase & 0xffff) | ||
154 | + (ep->freqm << ep->datainterval); | ||
155 | ret = min(ep->phase >> 16, ep->maxframesize); | ||
156 | spin_unlock_irqrestore(&ep->lock, flags); | ||
66 | 157 | ||
67 | if (!async && in_interrupt()) | 158 | return ret; |
68 | return 0; | 159 | } |
69 | 160 | ||
70 | for (i = 0; i < subs->nurbs; i++) { | 161 | static void retire_outbound_urb(struct snd_usb_endpoint *ep, |
71 | if (test_bit(i, &subs->active_mask)) { | 162 | struct snd_urb_ctx *urb_ctx) |
72 | if (!test_and_set_bit(i, &subs->unlink_mask)) { | 163 | { |
73 | struct urb *u = subs->dataurb[i].urb; | 164 | if (ep->retire_data_urb) |
74 | if (async) | 165 | ep->retire_data_urb(ep->data_subs, urb_ctx->urb); |
75 | usb_unlink_urb(u); | 166 | } |
76 | else | 167 | |
77 | usb_kill_urb(u); | 168 | static void retire_inbound_urb(struct snd_usb_endpoint *ep, |
169 | struct snd_urb_ctx *urb_ctx) | ||
170 | { | ||
171 | struct urb *urb = urb_ctx->urb; | ||
172 | |||
173 | if (ep->sync_slave) | ||
174 | snd_usb_handle_sync_urb(ep->sync_slave, ep, urb); | ||
175 | |||
176 | if (ep->retire_data_urb) | ||
177 | ep->retire_data_urb(ep->data_subs, urb); | ||
178 | } | ||
179 | |||
180 | static void prepare_outbound_urb_sizes(struct snd_usb_endpoint *ep, | ||
181 | struct snd_urb_ctx *ctx) | ||
182 | { | ||
183 | int i; | ||
184 | |||
185 | for (i = 0; i < ctx->packets; ++i) | ||
186 | ctx->packet_size[i] = next_packet_size(ep); | ||
187 | } | ||
188 | |||
189 | /* | ||
190 | * Prepare a PLAYBACK urb for submission to the bus. | ||
191 | */ | ||
192 | static void prepare_outbound_urb(struct snd_usb_endpoint *ep, | ||
193 | struct snd_urb_ctx *ctx) | ||
194 | { | ||
195 | int i; | ||
196 | struct urb *urb = ctx->urb; | ||
197 | unsigned char *cp = urb->transfer_buffer; | ||
198 | |||
199 | urb->dev = ep->chip->dev; /* we need to set this at each time */ | ||
200 | |||
201 | switch (ep->type) { | ||
202 | case SND_USB_ENDPOINT_TYPE_DATA: | ||
203 | if (ep->prepare_data_urb) { | ||
204 | ep->prepare_data_urb(ep->data_subs, urb); | ||
205 | } else { | ||
206 | /* no data provider, so send silence */ | ||
207 | unsigned int offs = 0; | ||
208 | for (i = 0; i < ctx->packets; ++i) { | ||
209 | int counts = ctx->packet_size[i]; | ||
210 | urb->iso_frame_desc[i].offset = offs * ep->stride; | ||
211 | urb->iso_frame_desc[i].length = counts * ep->stride; | ||
212 | offs += counts; | ||
78 | } | 213 | } |
214 | |||
215 | urb->number_of_packets = ctx->packets; | ||
216 | urb->transfer_buffer_length = offs * ep->stride; | ||
217 | memset(urb->transfer_buffer, ep->silence_value, | ||
218 | offs * ep->stride); | ||
79 | } | 219 | } |
220 | break; | ||
221 | |||
222 | case SND_USB_ENDPOINT_TYPE_SYNC: | ||
223 | if (snd_usb_get_speed(ep->chip->dev) >= USB_SPEED_HIGH) { | ||
224 | /* | ||
225 | * fill the length and offset of each urb descriptor. | ||
226 | * the fixed 12.13 frequency is passed as 16.16 through the pipe. | ||
227 | */ | ||
228 | urb->iso_frame_desc[0].length = 4; | ||
229 | urb->iso_frame_desc[0].offset = 0; | ||
230 | cp[0] = ep->freqn; | ||
231 | cp[1] = ep->freqn >> 8; | ||
232 | cp[2] = ep->freqn >> 16; | ||
233 | cp[3] = ep->freqn >> 24; | ||
234 | } else { | ||
235 | /* | ||
236 | * fill the length and offset of each urb descriptor. | ||
237 | * the fixed 10.14 frequency is passed through the pipe. | ||
238 | */ | ||
239 | urb->iso_frame_desc[0].length = 3; | ||
240 | urb->iso_frame_desc[0].offset = 0; | ||
241 | cp[0] = ep->freqn >> 2; | ||
242 | cp[1] = ep->freqn >> 10; | ||
243 | cp[2] = ep->freqn >> 18; | ||
244 | } | ||
245 | |||
246 | break; | ||
80 | } | 247 | } |
81 | if (subs->syncpipe) { | 248 | } |
82 | for (i = 0; i < SYNC_URBS; i++) { | 249 | |
83 | if (test_bit(i+16, &subs->active_mask)) { | 250 | /* |
84 | if (!test_and_set_bit(i+16, &subs->unlink_mask)) { | 251 | * Prepare a CAPTURE or SYNC urb for submission to the bus. |
85 | struct urb *u = subs->syncurb[i].urb; | 252 | */ |
86 | if (async) | 253 | static inline void prepare_inbound_urb(struct snd_usb_endpoint *ep, |
87 | usb_unlink_urb(u); | 254 | struct snd_urb_ctx *urb_ctx) |
88 | else | 255 | { |
89 | usb_kill_urb(u); | 256 | int i, offs; |
90 | } | 257 | struct urb *urb = urb_ctx->urb; |
91 | } | 258 | |
259 | urb->dev = ep->chip->dev; /* we need to set this at each time */ | ||
260 | |||
261 | switch (ep->type) { | ||
262 | case SND_USB_ENDPOINT_TYPE_DATA: | ||
263 | offs = 0; | ||
264 | for (i = 0; i < urb_ctx->packets; i++) { | ||
265 | urb->iso_frame_desc[i].offset = offs; | ||
266 | urb->iso_frame_desc[i].length = ep->curpacksize; | ||
267 | offs += ep->curpacksize; | ||
92 | } | 268 | } |
269 | |||
270 | urb->transfer_buffer_length = offs; | ||
271 | urb->number_of_packets = urb_ctx->packets; | ||
272 | break; | ||
273 | |||
274 | case SND_USB_ENDPOINT_TYPE_SYNC: | ||
275 | urb->iso_frame_desc[0].length = min(4u, ep->syncmaxsize); | ||
276 | urb->iso_frame_desc[0].offset = 0; | ||
277 | break; | ||
93 | } | 278 | } |
94 | return 0; | ||
95 | } | 279 | } |
96 | 280 | ||
281 | /* | ||
282 | * Send output urbs that have been prepared previously. URBs are dequeued | ||
283 | * from ep->ready_playback_urbs and in case there there aren't any available | ||
284 | * or there are no packets that have been prepared, this function does | ||
285 | * nothing. | ||
286 | * | ||
287 | * The reason why the functionality of sending and preparing URBs is separated | ||
288 | * is that host controllers don't guarantee the order in which they return | ||
289 | * inbound and outbound packets to their submitters. | ||
290 | * | ||
291 | * This function is only used for implicit feedback endpoints. For endpoints | ||
292 | * driven by dedicated sync endpoints, URBs are immediately re-submitted | ||
293 | * from their completion handler. | ||
294 | */ | ||
295 | static void queue_pending_output_urbs(struct snd_usb_endpoint *ep) | ||
296 | { | ||
297 | while (test_bit(EP_FLAG_RUNNING, &ep->flags)) { | ||
298 | |||
299 | unsigned long flags; | ||
300 | struct snd_usb_packet_info *uninitialized_var(packet); | ||
301 | struct snd_urb_ctx *ctx = NULL; | ||
302 | struct urb *urb; | ||
303 | int err, i; | ||
304 | |||
305 | spin_lock_irqsave(&ep->lock, flags); | ||
306 | if (ep->next_packet_read_pos != ep->next_packet_write_pos) { | ||
307 | packet = ep->next_packet + ep->next_packet_read_pos; | ||
308 | ep->next_packet_read_pos++; | ||
309 | ep->next_packet_read_pos %= MAX_URBS; | ||
310 | |||
311 | /* take URB out of FIFO */ | ||
312 | if (!list_empty(&ep->ready_playback_urbs)) | ||
313 | ctx = list_first_entry(&ep->ready_playback_urbs, | ||
314 | struct snd_urb_ctx, ready_list); | ||
315 | } | ||
316 | spin_unlock_irqrestore(&ep->lock, flags); | ||
317 | |||
318 | if (ctx == NULL) | ||
319 | return; | ||
320 | |||
321 | list_del_init(&ctx->ready_list); | ||
322 | urb = ctx->urb; | ||
323 | |||
324 | /* copy over the length information */ | ||
325 | for (i = 0; i < packet->packets; i++) | ||
326 | ctx->packet_size[i] = packet->packet_size[i]; | ||
327 | |||
328 | /* call the data handler to fill in playback data */ | ||
329 | prepare_outbound_urb(ep, ctx); | ||
330 | |||
331 | err = usb_submit_urb(ctx->urb, GFP_ATOMIC); | ||
332 | if (err < 0) | ||
333 | snd_printk(KERN_ERR "Unable to submit urb #%d: %d (urb %p)\n", | ||
334 | ctx->index, err, ctx->urb); | ||
335 | else | ||
336 | set_bit(ctx->index, &ep->active_mask); | ||
337 | } | ||
338 | } | ||
97 | 339 | ||
98 | /* | 340 | /* |
99 | * release a urb data | 341 | * complete callback for urbs |
100 | */ | 342 | */ |
101 | static void release_urb_ctx(struct snd_urb_ctx *u) | 343 | static void snd_complete_urb(struct urb *urb) |
344 | { | ||
345 | struct snd_urb_ctx *ctx = urb->context; | ||
346 | struct snd_usb_endpoint *ep = ctx->ep; | ||
347 | int err; | ||
348 | |||
349 | if (unlikely(urb->status == -ENOENT || /* unlinked */ | ||
350 | urb->status == -ENODEV || /* device removed */ | ||
351 | urb->status == -ECONNRESET || /* unlinked */ | ||
352 | urb->status == -ESHUTDOWN || /* device disabled */ | ||
353 | ep->chip->shutdown)) /* device disconnected */ | ||
354 | goto exit_clear; | ||
355 | |||
356 | if (usb_pipeout(ep->pipe)) { | ||
357 | retire_outbound_urb(ep, ctx); | ||
358 | /* can be stopped during retire callback */ | ||
359 | if (unlikely(!test_bit(EP_FLAG_RUNNING, &ep->flags))) | ||
360 | goto exit_clear; | ||
361 | |||
362 | if (snd_usb_endpoint_implict_feedback_sink(ep)) { | ||
363 | unsigned long flags; | ||
364 | |||
365 | spin_lock_irqsave(&ep->lock, flags); | ||
366 | list_add_tail(&ctx->ready_list, &ep->ready_playback_urbs); | ||
367 | spin_unlock_irqrestore(&ep->lock, flags); | ||
368 | queue_pending_output_urbs(ep); | ||
369 | |||
370 | goto exit_clear; | ||
371 | } | ||
372 | |||
373 | prepare_outbound_urb_sizes(ep, ctx); | ||
374 | prepare_outbound_urb(ep, ctx); | ||
375 | } else { | ||
376 | retire_inbound_urb(ep, ctx); | ||
377 | /* can be stopped during retire callback */ | ||
378 | if (unlikely(!test_bit(EP_FLAG_RUNNING, &ep->flags))) | ||
379 | goto exit_clear; | ||
380 | |||
381 | prepare_inbound_urb(ep, ctx); | ||
382 | } | ||
383 | |||
384 | err = usb_submit_urb(urb, GFP_ATOMIC); | ||
385 | if (err == 0) | ||
386 | return; | ||
387 | |||
388 | snd_printk(KERN_ERR "cannot submit urb (err = %d)\n", err); | ||
389 | //snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN); | ||
390 | |||
391 | exit_clear: | ||
392 | clear_bit(ctx->index, &ep->active_mask); | ||
393 | } | ||
394 | |||
395 | /** | ||
396 | * snd_usb_add_endpoint: Add an endpoint to an USB audio chip | ||
397 | * | ||
398 | * @chip: The chip | ||
399 | * @alts: The USB host interface | ||
400 | * @ep_num: The number of the endpoint to use | ||
401 | * @direction: SNDRV_PCM_STREAM_PLAYBACK or SNDRV_PCM_STREAM_CAPTURE | ||
402 | * @type: SND_USB_ENDPOINT_TYPE_DATA or SND_USB_ENDPOINT_TYPE_SYNC | ||
403 | * | ||
404 | * If the requested endpoint has not been added to the given chip before, | ||
405 | * a new instance is created. Otherwise, a pointer to the previoulsy | ||
406 | * created instance is returned. In case of any error, NULL is returned. | ||
407 | * | ||
408 | * New endpoints will be added to chip->ep_list and must be freed by | ||
409 | * calling snd_usb_endpoint_free(). | ||
410 | */ | ||
411 | struct snd_usb_endpoint *snd_usb_add_endpoint(struct snd_usb_audio *chip, | ||
412 | struct usb_host_interface *alts, | ||
413 | int ep_num, int direction, int type) | ||
102 | { | 414 | { |
103 | if (u->urb) { | 415 | struct list_head *p; |
104 | if (u->buffer_size) | 416 | struct snd_usb_endpoint *ep; |
105 | usb_free_coherent(u->subs->dev, u->buffer_size, | 417 | int ret, is_playback = direction == SNDRV_PCM_STREAM_PLAYBACK; |
106 | u->urb->transfer_buffer, | 418 | |
107 | u->urb->transfer_dma); | 419 | mutex_lock(&chip->mutex); |
108 | usb_free_urb(u->urb); | 420 | |
109 | u->urb = NULL; | 421 | list_for_each(p, &chip->ep_list) { |
422 | ep = list_entry(p, struct snd_usb_endpoint, list); | ||
423 | if (ep->ep_num == ep_num && | ||
424 | ep->iface == alts->desc.bInterfaceNumber && | ||
425 | ep->alt_idx == alts->desc.bAlternateSetting) { | ||
426 | snd_printdd(KERN_DEBUG "Re-using EP %x in iface %d,%d @%p\n", | ||
427 | ep_num, ep->iface, ep->alt_idx, ep); | ||
428 | goto __exit_unlock; | ||
429 | } | ||
430 | } | ||
431 | |||
432 | snd_printdd(KERN_DEBUG "Creating new %s %s endpoint #%x\n", | ||
433 | is_playback ? "playback" : "capture", | ||
434 | type == SND_USB_ENDPOINT_TYPE_DATA ? "data" : "sync", | ||
435 | ep_num); | ||
436 | |||
437 | /* select the alt setting once so the endpoints become valid */ | ||
438 | ret = usb_set_interface(chip->dev, alts->desc.bInterfaceNumber, | ||
439 | alts->desc.bAlternateSetting); | ||
440 | if (ret < 0) { | ||
441 | snd_printk(KERN_ERR "%s(): usb_set_interface() failed, ret = %d\n", | ||
442 | __func__, ret); | ||
443 | ep = NULL; | ||
444 | goto __exit_unlock; | ||
110 | } | 445 | } |
446 | |||
447 | ep = kzalloc(sizeof(*ep), GFP_KERNEL); | ||
448 | if (!ep) | ||
449 | goto __exit_unlock; | ||
450 | |||
451 | ep->chip = chip; | ||
452 | spin_lock_init(&ep->lock); | ||
453 | ep->type = type; | ||
454 | ep->ep_num = ep_num; | ||
455 | ep->iface = alts->desc.bInterfaceNumber; | ||
456 | ep->alt_idx = alts->desc.bAlternateSetting; | ||
457 | INIT_LIST_HEAD(&ep->ready_playback_urbs); | ||
458 | ep_num &= USB_ENDPOINT_NUMBER_MASK; | ||
459 | |||
460 | if (is_playback) | ||
461 | ep->pipe = usb_sndisocpipe(chip->dev, ep_num); | ||
462 | else | ||
463 | ep->pipe = usb_rcvisocpipe(chip->dev, ep_num); | ||
464 | |||
465 | if (type == SND_USB_ENDPOINT_TYPE_SYNC) { | ||
466 | if (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE && | ||
467 | get_endpoint(alts, 1)->bRefresh >= 1 && | ||
468 | get_endpoint(alts, 1)->bRefresh <= 9) | ||
469 | ep->syncinterval = get_endpoint(alts, 1)->bRefresh; | ||
470 | else if (snd_usb_get_speed(chip->dev) == USB_SPEED_FULL) | ||
471 | ep->syncinterval = 1; | ||
472 | else if (get_endpoint(alts, 1)->bInterval >= 1 && | ||
473 | get_endpoint(alts, 1)->bInterval <= 16) | ||
474 | ep->syncinterval = get_endpoint(alts, 1)->bInterval - 1; | ||
475 | else | ||
476 | ep->syncinterval = 3; | ||
477 | |||
478 | ep->syncmaxsize = le16_to_cpu(get_endpoint(alts, 1)->wMaxPacketSize); | ||
479 | } | ||
480 | |||
481 | list_add_tail(&ep->list, &chip->ep_list); | ||
482 | |||
483 | __exit_unlock: | ||
484 | mutex_unlock(&chip->mutex); | ||
485 | |||
486 | return ep; | ||
111 | } | 487 | } |
112 | 488 | ||
113 | /* | 489 | /* |
114 | * wait until all urbs are processed. | 490 | * wait until all urbs are processed. |
115 | */ | 491 | */ |
116 | static int wait_clear_urbs(struct snd_usb_substream *subs) | 492 | static int wait_clear_urbs(struct snd_usb_endpoint *ep) |
117 | { | 493 | { |
118 | unsigned long end_time = jiffies + msecs_to_jiffies(1000); | 494 | unsigned long end_time = jiffies + msecs_to_jiffies(1000); |
119 | unsigned int i; | 495 | unsigned int i; |
@@ -121,153 +497,148 @@ static int wait_clear_urbs(struct snd_usb_substream *subs) | |||
121 | 497 | ||
122 | do { | 498 | do { |
123 | alive = 0; | 499 | alive = 0; |
124 | for (i = 0; i < subs->nurbs; i++) { | 500 | for (i = 0; i < ep->nurbs; i++) |
125 | if (test_bit(i, &subs->active_mask)) | 501 | if (test_bit(i, &ep->active_mask)) |
126 | alive++; | 502 | alive++; |
127 | } | 503 | |
128 | if (subs->syncpipe) { | 504 | if (!alive) |
129 | for (i = 0; i < SYNC_URBS; i++) { | ||
130 | if (test_bit(i + 16, &subs->active_mask)) | ||
131 | alive++; | ||
132 | } | ||
133 | } | ||
134 | if (! alive) | ||
135 | break; | 505 | break; |
506 | |||
136 | schedule_timeout_uninterruptible(1); | 507 | schedule_timeout_uninterruptible(1); |
137 | } while (time_before(jiffies, end_time)); | 508 | } while (time_before(jiffies, end_time)); |
509 | |||
138 | if (alive) | 510 | if (alive) |
139 | snd_printk(KERN_ERR "timeout: still %d active urbs..\n", alive); | 511 | snd_printk(KERN_ERR "timeout: still %d active urbs on EP #%x\n", |
512 | alive, ep->ep_num); | ||
513 | |||
140 | return 0; | 514 | return 0; |
141 | } | 515 | } |
142 | 516 | ||
143 | /* | 517 | /* |
144 | * release a substream | 518 | * unlink active urbs. |
145 | */ | 519 | */ |
146 | void snd_usb_release_substream_urbs(struct snd_usb_substream *subs, int force) | 520 | static int deactivate_urbs(struct snd_usb_endpoint *ep, int force, int can_sleep) |
147 | { | 521 | { |
148 | int i; | 522 | unsigned int i; |
523 | int async; | ||
149 | 524 | ||
150 | /* stop urbs (to be sure) */ | 525 | if (!force && ep->chip->shutdown) /* to be sure... */ |
151 | deactivate_urbs(subs, force, 1); | 526 | return -EBADFD; |
152 | wait_clear_urbs(subs); | ||
153 | |||
154 | for (i = 0; i < MAX_URBS; i++) | ||
155 | release_urb_ctx(&subs->dataurb[i]); | ||
156 | for (i = 0; i < SYNC_URBS; i++) | ||
157 | release_urb_ctx(&subs->syncurb[i]); | ||
158 | usb_free_coherent(subs->dev, SYNC_URBS * 4, | ||
159 | subs->syncbuf, subs->sync_dma); | ||
160 | subs->syncbuf = NULL; | ||
161 | subs->nurbs = 0; | ||
162 | } | ||
163 | 527 | ||
164 | /* | 528 | async = !can_sleep && ep->chip->async_unlink; |
165 | * complete callback from data urb | 529 | |
166 | */ | 530 | clear_bit(EP_FLAG_RUNNING, &ep->flags); |
167 | static void snd_complete_urb(struct urb *urb) | 531 | |
168 | { | 532 | INIT_LIST_HEAD(&ep->ready_playback_urbs); |
169 | struct snd_urb_ctx *ctx = urb->context; | 533 | ep->next_packet_read_pos = 0; |
170 | struct snd_usb_substream *subs = ctx->subs; | 534 | ep->next_packet_write_pos = 0; |
171 | struct snd_pcm_substream *substream = ctx->subs->pcm_substream; | 535 | |
172 | int err = 0; | 536 | if (!async && in_interrupt()) |
173 | 537 | return 0; | |
174 | if ((subs->running && subs->ops.retire(subs, substream->runtime, urb)) || | 538 | |
175 | !subs->running || /* can be stopped during retire callback */ | 539 | for (i = 0; i < ep->nurbs; i++) { |
176 | (err = subs->ops.prepare(subs, substream->runtime, urb)) < 0 || | 540 | if (test_bit(i, &ep->active_mask)) { |
177 | (err = usb_submit_urb(urb, GFP_ATOMIC)) < 0) { | 541 | if (!test_and_set_bit(i, &ep->unlink_mask)) { |
178 | clear_bit(ctx->index, &subs->active_mask); | 542 | struct urb *u = ep->urb[i].urb; |
179 | if (err < 0) { | 543 | if (async) |
180 | snd_printd(KERN_ERR "cannot submit urb (err = %d)\n", err); | 544 | usb_unlink_urb(u); |
181 | snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN); | 545 | else |
546 | usb_kill_urb(u); | ||
547 | } | ||
182 | } | 548 | } |
183 | } | 549 | } |
184 | } | ||
185 | 550 | ||
551 | return 0; | ||
552 | } | ||
186 | 553 | ||
187 | /* | 554 | /* |
188 | * complete callback from sync urb | 555 | * release an endpoint's urbs |
189 | */ | 556 | */ |
190 | static void snd_complete_sync_urb(struct urb *urb) | 557 | static void release_urbs(struct snd_usb_endpoint *ep, int force) |
191 | { | 558 | { |
192 | struct snd_urb_ctx *ctx = urb->context; | 559 | int i; |
193 | struct snd_usb_substream *subs = ctx->subs; | ||
194 | struct snd_pcm_substream *substream = ctx->subs->pcm_substream; | ||
195 | int err = 0; | ||
196 | |||
197 | if ((subs->running && subs->ops.retire_sync(subs, substream->runtime, urb)) || | ||
198 | !subs->running || /* can be stopped during retire callback */ | ||
199 | (err = subs->ops.prepare_sync(subs, substream->runtime, urb)) < 0 || | ||
200 | (err = usb_submit_urb(urb, GFP_ATOMIC)) < 0) { | ||
201 | clear_bit(ctx->index + 16, &subs->active_mask); | ||
202 | if (err < 0) { | ||
203 | snd_printd(KERN_ERR "cannot submit sync urb (err = %d)\n", err); | ||
204 | snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN); | ||
205 | } | ||
206 | } | ||
207 | } | ||
208 | 560 | ||
561 | /* route incoming urbs to nirvana */ | ||
562 | ep->retire_data_urb = NULL; | ||
563 | ep->prepare_data_urb = NULL; | ||
564 | |||
565 | /* stop urbs */ | ||
566 | deactivate_urbs(ep, force, 1); | ||
567 | wait_clear_urbs(ep); | ||
568 | |||
569 | for (i = 0; i < ep->nurbs; i++) | ||
570 | release_urb_ctx(&ep->urb[i]); | ||
571 | |||
572 | if (ep->syncbuf) | ||
573 | usb_free_coherent(ep->chip->dev, SYNC_URBS * 4, | ||
574 | ep->syncbuf, ep->sync_dma); | ||
575 | |||
576 | ep->syncbuf = NULL; | ||
577 | ep->nurbs = 0; | ||
578 | } | ||
209 | 579 | ||
210 | /* | 580 | /* |
211 | * initialize a substream for plaback/capture | 581 | * configure a data endpoint |
212 | */ | 582 | */ |
213 | int snd_usb_init_substream_urbs(struct snd_usb_substream *subs, | 583 | static int data_ep_set_params(struct snd_usb_endpoint *ep, |
214 | unsigned int period_bytes, | 584 | struct snd_pcm_hw_params *hw_params, |
215 | unsigned int rate, | 585 | struct audioformat *fmt, |
216 | unsigned int frame_bits) | 586 | struct snd_usb_endpoint *sync_ep) |
217 | { | 587 | { |
218 | unsigned int maxsize, i; | 588 | unsigned int maxsize, i, urb_packs, total_packs, packs_per_ms; |
219 | int is_playback = subs->direction == SNDRV_PCM_STREAM_PLAYBACK; | 589 | int period_bytes = params_period_bytes(hw_params); |
220 | unsigned int urb_packs, total_packs, packs_per_ms; | 590 | int format = params_format(hw_params); |
221 | struct snd_usb_audio *chip = subs->stream->chip; | 591 | int is_playback = usb_pipeout(ep->pipe); |
592 | int frame_bits = snd_pcm_format_physical_width(params_format(hw_params)) * | ||
593 | params_channels(hw_params); | ||
594 | |||
595 | ep->datainterval = fmt->datainterval; | ||
596 | ep->stride = frame_bits >> 3; | ||
597 | ep->silence_value = format == SNDRV_PCM_FORMAT_U8 ? 0x80 : 0; | ||
222 | 598 | ||
223 | /* calculate the frequency in 16.16 format */ | ||
224 | if (snd_usb_get_speed(subs->dev) == USB_SPEED_FULL) | ||
225 | subs->freqn = get_usb_full_speed_rate(rate); | ||
226 | else | ||
227 | subs->freqn = get_usb_high_speed_rate(rate); | ||
228 | subs->freqm = subs->freqn; | ||
229 | subs->freqshift = INT_MIN; | ||
230 | /* calculate max. frequency */ | 599 | /* calculate max. frequency */ |
231 | if (subs->maxpacksize) { | 600 | if (ep->maxpacksize) { |
232 | /* whatever fits into a max. size packet */ | 601 | /* whatever fits into a max. size packet */ |
233 | maxsize = subs->maxpacksize; | 602 | maxsize = ep->maxpacksize; |
234 | subs->freqmax = (maxsize / (frame_bits >> 3)) | 603 | ep->freqmax = (maxsize / (frame_bits >> 3)) |
235 | << (16 - subs->datainterval); | 604 | << (16 - ep->datainterval); |
236 | } else { | 605 | } else { |
237 | /* no max. packet size: just take 25% higher than nominal */ | 606 | /* no max. packet size: just take 25% higher than nominal */ |
238 | subs->freqmax = subs->freqn + (subs->freqn >> 2); | 607 | ep->freqmax = ep->freqn + (ep->freqn >> 2); |
239 | maxsize = ((subs->freqmax + 0xffff) * (frame_bits >> 3)) | 608 | maxsize = ((ep->freqmax + 0xffff) * (frame_bits >> 3)) |
240 | >> (16 - subs->datainterval); | 609 | >> (16 - ep->datainterval); |
241 | } | 610 | } |
242 | subs->phase = 0; | ||
243 | 611 | ||
244 | if (subs->fill_max) | 612 | if (ep->fill_max) |
245 | subs->curpacksize = subs->maxpacksize; | 613 | ep->curpacksize = ep->maxpacksize; |
246 | else | 614 | else |
247 | subs->curpacksize = maxsize; | 615 | ep->curpacksize = maxsize; |
248 | 616 | ||
249 | if (snd_usb_get_speed(subs->dev) != USB_SPEED_FULL) | 617 | if (snd_usb_get_speed(ep->chip->dev) != USB_SPEED_FULL) |
250 | packs_per_ms = 8 >> subs->datainterval; | 618 | packs_per_ms = 8 >> ep->datainterval; |
251 | else | 619 | else |
252 | packs_per_ms = 1; | 620 | packs_per_ms = 1; |
253 | 621 | ||
254 | if (is_playback) { | 622 | if (is_playback && !snd_usb_endpoint_implict_feedback_sink(ep)) { |
255 | urb_packs = max(chip->nrpacks, 1); | 623 | urb_packs = max(ep->chip->nrpacks, 1); |
256 | urb_packs = min(urb_packs, (unsigned int)MAX_PACKS); | 624 | urb_packs = min(urb_packs, (unsigned int) MAX_PACKS); |
257 | } else | 625 | } else { |
258 | urb_packs = 1; | 626 | urb_packs = 1; |
627 | } | ||
628 | |||
259 | urb_packs *= packs_per_ms; | 629 | urb_packs *= packs_per_ms; |
260 | if (subs->syncpipe) | 630 | |
261 | urb_packs = min(urb_packs, 1U << subs->syncinterval); | 631 | if (sync_ep && !snd_usb_endpoint_implict_feedback_sink(ep)) |
632 | urb_packs = min(urb_packs, 1U << sync_ep->syncinterval); | ||
262 | 633 | ||
263 | /* decide how many packets to be used */ | 634 | /* decide how many packets to be used */ |
264 | if (is_playback) { | 635 | if (is_playback && !snd_usb_endpoint_implict_feedback_sink(ep)) { |
265 | unsigned int minsize, maxpacks; | 636 | unsigned int minsize, maxpacks; |
266 | /* determine how small a packet can be */ | 637 | /* determine how small a packet can be */ |
267 | minsize = (subs->freqn >> (16 - subs->datainterval)) | 638 | minsize = (ep->freqn >> (16 - ep->datainterval)) |
268 | * (frame_bits >> 3); | 639 | * (frame_bits >> 3); |
269 | /* with sync from device, assume it can be 12% lower */ | 640 | /* with sync from device, assume it can be 12% lower */ |
270 | if (subs->syncpipe) | 641 | if (sync_ep) |
271 | minsize -= minsize >> 3; | 642 | minsize -= minsize >> 3; |
272 | minsize = max(minsize, 1u); | 643 | minsize = max(minsize, 1u); |
273 | total_packs = (period_bytes + minsize - 1) / minsize; | 644 | total_packs = (period_bytes + minsize - 1) / minsize; |
@@ -284,284 +655,472 @@ int snd_usb_init_substream_urbs(struct snd_usb_substream *subs, | |||
284 | urb_packs >>= 1; | 655 | urb_packs >>= 1; |
285 | total_packs = MAX_URBS * urb_packs; | 656 | total_packs = MAX_URBS * urb_packs; |
286 | } | 657 | } |
287 | subs->nurbs = (total_packs + urb_packs - 1) / urb_packs; | 658 | |
288 | if (subs->nurbs > MAX_URBS) { | 659 | ep->nurbs = (total_packs + urb_packs - 1) / urb_packs; |
660 | if (ep->nurbs > MAX_URBS) { | ||
289 | /* too much... */ | 661 | /* too much... */ |
290 | subs->nurbs = MAX_URBS; | 662 | ep->nurbs = MAX_URBS; |
291 | total_packs = MAX_URBS * urb_packs; | 663 | total_packs = MAX_URBS * urb_packs; |
292 | } else if (subs->nurbs < 2) { | 664 | } else if (ep->nurbs < 2) { |
293 | /* too little - we need at least two packets | 665 | /* too little - we need at least two packets |
294 | * to ensure contiguous playback/capture | 666 | * to ensure contiguous playback/capture |
295 | */ | 667 | */ |
296 | subs->nurbs = 2; | 668 | ep->nurbs = 2; |
297 | } | 669 | } |
298 | 670 | ||
299 | /* allocate and initialize data urbs */ | 671 | /* allocate and initialize data urbs */ |
300 | for (i = 0; i < subs->nurbs; i++) { | 672 | for (i = 0; i < ep->nurbs; i++) { |
301 | struct snd_urb_ctx *u = &subs->dataurb[i]; | 673 | struct snd_urb_ctx *u = &ep->urb[i]; |
302 | u->index = i; | 674 | u->index = i; |
303 | u->subs = subs; | 675 | u->ep = ep; |
304 | u->packets = (i + 1) * total_packs / subs->nurbs | 676 | u->packets = (i + 1) * total_packs / ep->nurbs |
305 | - i * total_packs / subs->nurbs; | 677 | - i * total_packs / ep->nurbs; |
306 | u->buffer_size = maxsize * u->packets; | 678 | u->buffer_size = maxsize * u->packets; |
307 | if (subs->fmt_type == UAC_FORMAT_TYPE_II) | 679 | |
680 | if (fmt->fmt_type == UAC_FORMAT_TYPE_II) | ||
308 | u->packets++; /* for transfer delimiter */ | 681 | u->packets++; /* for transfer delimiter */ |
309 | u->urb = usb_alloc_urb(u->packets, GFP_KERNEL); | 682 | u->urb = usb_alloc_urb(u->packets, GFP_KERNEL); |
310 | if (!u->urb) | 683 | if (!u->urb) |
311 | goto out_of_memory; | 684 | goto out_of_memory; |
685 | |||
312 | u->urb->transfer_buffer = | 686 | u->urb->transfer_buffer = |
313 | usb_alloc_coherent(subs->dev, u->buffer_size, | 687 | usb_alloc_coherent(ep->chip->dev, u->buffer_size, |
314 | GFP_KERNEL, &u->urb->transfer_dma); | 688 | GFP_KERNEL, &u->urb->transfer_dma); |
315 | if (!u->urb->transfer_buffer) | 689 | if (!u->urb->transfer_buffer) |
316 | goto out_of_memory; | 690 | goto out_of_memory; |
317 | u->urb->pipe = subs->datapipe; | 691 | u->urb->pipe = ep->pipe; |
318 | u->urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP; | 692 | u->urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP; |
319 | u->urb->interval = 1 << subs->datainterval; | 693 | u->urb->interval = 1 << ep->datainterval; |
320 | u->urb->context = u; | 694 | u->urb->context = u; |
321 | u->urb->complete = snd_complete_urb; | 695 | u->urb->complete = snd_complete_urb; |
696 | INIT_LIST_HEAD(&u->ready_list); | ||
322 | } | 697 | } |
323 | 698 | ||
324 | if (subs->syncpipe) { | ||
325 | /* allocate and initialize sync urbs */ | ||
326 | subs->syncbuf = usb_alloc_coherent(subs->dev, SYNC_URBS * 4, | ||
327 | GFP_KERNEL, &subs->sync_dma); | ||
328 | if (!subs->syncbuf) | ||
329 | goto out_of_memory; | ||
330 | for (i = 0; i < SYNC_URBS; i++) { | ||
331 | struct snd_urb_ctx *u = &subs->syncurb[i]; | ||
332 | u->index = i; | ||
333 | u->subs = subs; | ||
334 | u->packets = 1; | ||
335 | u->urb = usb_alloc_urb(1, GFP_KERNEL); | ||
336 | if (!u->urb) | ||
337 | goto out_of_memory; | ||
338 | u->urb->transfer_buffer = subs->syncbuf + i * 4; | ||
339 | u->urb->transfer_dma = subs->sync_dma + i * 4; | ||
340 | u->urb->transfer_buffer_length = 4; | ||
341 | u->urb->pipe = subs->syncpipe; | ||
342 | u->urb->transfer_flags = URB_ISO_ASAP | | ||
343 | URB_NO_TRANSFER_DMA_MAP; | ||
344 | u->urb->number_of_packets = 1; | ||
345 | u->urb->interval = 1 << subs->syncinterval; | ||
346 | u->urb->context = u; | ||
347 | u->urb->complete = snd_complete_sync_urb; | ||
348 | } | ||
349 | } | ||
350 | return 0; | 699 | return 0; |
351 | 700 | ||
352 | out_of_memory: | 701 | out_of_memory: |
353 | snd_usb_release_substream_urbs(subs, 0); | 702 | release_urbs(ep, 0); |
354 | return -ENOMEM; | 703 | return -ENOMEM; |
355 | } | 704 | } |
356 | 705 | ||
357 | /* | 706 | /* |
358 | * prepare urb for full speed capture sync pipe | 707 | * configure a sync endpoint |
359 | * | ||
360 | * fill the length and offset of each urb descriptor. | ||
361 | * the fixed 10.14 frequency is passed through the pipe. | ||
362 | */ | 708 | */ |
363 | static int prepare_capture_sync_urb(struct snd_usb_substream *subs, | 709 | static int sync_ep_set_params(struct snd_usb_endpoint *ep, |
364 | struct snd_pcm_runtime *runtime, | 710 | struct snd_pcm_hw_params *hw_params, |
365 | struct urb *urb) | 711 | struct audioformat *fmt) |
366 | { | 712 | { |
367 | unsigned char *cp = urb->transfer_buffer; | 713 | int i; |
368 | struct snd_urb_ctx *ctx = urb->context; | 714 | |
715 | ep->syncbuf = usb_alloc_coherent(ep->chip->dev, SYNC_URBS * 4, | ||
716 | GFP_KERNEL, &ep->sync_dma); | ||
717 | if (!ep->syncbuf) | ||
718 | return -ENOMEM; | ||
719 | |||
720 | for (i = 0; i < SYNC_URBS; i++) { | ||
721 | struct snd_urb_ctx *u = &ep->urb[i]; | ||
722 | u->index = i; | ||
723 | u->ep = ep; | ||
724 | u->packets = 1; | ||
725 | u->urb = usb_alloc_urb(1, GFP_KERNEL); | ||
726 | if (!u->urb) | ||
727 | goto out_of_memory; | ||
728 | u->urb->transfer_buffer = ep->syncbuf + i * 4; | ||
729 | u->urb->transfer_dma = ep->sync_dma + i * 4; | ||
730 | u->urb->transfer_buffer_length = 4; | ||
731 | u->urb->pipe = ep->pipe; | ||
732 | u->urb->transfer_flags = URB_ISO_ASAP | | ||
733 | URB_NO_TRANSFER_DMA_MAP; | ||
734 | u->urb->number_of_packets = 1; | ||
735 | u->urb->interval = 1 << ep->syncinterval; | ||
736 | u->urb->context = u; | ||
737 | u->urb->complete = snd_complete_urb; | ||
738 | } | ||
739 | |||
740 | ep->nurbs = SYNC_URBS; | ||
369 | 741 | ||
370 | urb->dev = ctx->subs->dev; /* we need to set this at each time */ | ||
371 | urb->iso_frame_desc[0].length = 3; | ||
372 | urb->iso_frame_desc[0].offset = 0; | ||
373 | cp[0] = subs->freqn >> 2; | ||
374 | cp[1] = subs->freqn >> 10; | ||
375 | cp[2] = subs->freqn >> 18; | ||
376 | return 0; | 742 | return 0; |
743 | |||
744 | out_of_memory: | ||
745 | release_urbs(ep, 0); | ||
746 | return -ENOMEM; | ||
377 | } | 747 | } |
378 | 748 | ||
379 | /* | 749 | /** |
380 | * prepare urb for high speed capture sync pipe | 750 | * snd_usb_endpoint_set_params: configure an snd_usb_endpoint |
751 | * | ||
752 | * @ep: the snd_usb_endpoint to configure | ||
753 | * @hw_params: the hardware parameters | ||
754 | * @fmt: the USB audio format information | ||
755 | * @sync_ep: the sync endpoint to use, if any | ||
381 | * | 756 | * |
382 | * fill the length and offset of each urb descriptor. | 757 | * Determine the number of URBs to be used on this endpoint. |
383 | * the fixed 12.13 frequency is passed as 16.16 through the pipe. | 758 | * An endpoint must be configured before it can be started. |
759 | * An endpoint that is already running can not be reconfigured. | ||
384 | */ | 760 | */ |
385 | static int prepare_capture_sync_urb_hs(struct snd_usb_substream *subs, | 761 | int snd_usb_endpoint_set_params(struct snd_usb_endpoint *ep, |
386 | struct snd_pcm_runtime *runtime, | 762 | struct snd_pcm_hw_params *hw_params, |
387 | struct urb *urb) | 763 | struct audioformat *fmt, |
764 | struct snd_usb_endpoint *sync_ep) | ||
388 | { | 765 | { |
389 | unsigned char *cp = urb->transfer_buffer; | 766 | int err; |
390 | struct snd_urb_ctx *ctx = urb->context; | ||
391 | 767 | ||
392 | urb->dev = ctx->subs->dev; /* we need to set this at each time */ | 768 | if (ep->use_count != 0) { |
393 | urb->iso_frame_desc[0].length = 4; | 769 | snd_printk(KERN_WARNING "Unable to change format on ep #%x: already in use\n", |
394 | urb->iso_frame_desc[0].offset = 0; | 770 | ep->ep_num); |
395 | cp[0] = subs->freqn; | 771 | return -EBUSY; |
396 | cp[1] = subs->freqn >> 8; | 772 | } |
397 | cp[2] = subs->freqn >> 16; | 773 | |
398 | cp[3] = subs->freqn >> 24; | 774 | /* release old buffers, if any */ |
399 | return 0; | 775 | release_urbs(ep, 0); |
776 | |||
777 | ep->datainterval = fmt->datainterval; | ||
778 | ep->maxpacksize = fmt->maxpacksize; | ||
779 | ep->fill_max = !!(fmt->attributes & UAC_EP_CS_ATTR_FILL_MAX); | ||
780 | |||
781 | if (snd_usb_get_speed(ep->chip->dev) == USB_SPEED_FULL) | ||
782 | ep->freqn = get_usb_full_speed_rate(params_rate(hw_params)); | ||
783 | else | ||
784 | ep->freqn = get_usb_high_speed_rate(params_rate(hw_params)); | ||
785 | |||
786 | /* calculate the frequency in 16.16 format */ | ||
787 | ep->freqm = ep->freqn; | ||
788 | ep->freqshift = INT_MIN; | ||
789 | |||
790 | ep->phase = 0; | ||
791 | |||
792 | switch (ep->type) { | ||
793 | case SND_USB_ENDPOINT_TYPE_DATA: | ||
794 | err = data_ep_set_params(ep, hw_params, fmt, sync_ep); | ||
795 | break; | ||
796 | case SND_USB_ENDPOINT_TYPE_SYNC: | ||
797 | err = sync_ep_set_params(ep, hw_params, fmt); | ||
798 | break; | ||
799 | default: | ||
800 | err = -EINVAL; | ||
801 | } | ||
802 | |||
803 | snd_printdd(KERN_DEBUG "Setting params for ep #%x (type %d, %d urbs), ret=%d\n", | ||
804 | ep->ep_num, ep->type, ep->nurbs, err); | ||
805 | |||
806 | return err; | ||
400 | } | 807 | } |
401 | 808 | ||
402 | /* | 809 | /** |
403 | * process after capture sync complete | 810 | * snd_usb_endpoint_start: start an snd_usb_endpoint |
404 | * - nothing to do | 811 | * |
812 | * @ep: the endpoint to start | ||
813 | * | ||
814 | * A call to this function will increment the use count of the endpoint. | ||
815 | * In case it is not already running, the URBs for this endpoint will be | ||
816 | * submitted. Otherwise, this function does nothing. | ||
817 | * | ||
818 | * Must be balanced to calls of snd_usb_endpoint_stop(). | ||
819 | * | ||
820 | * Returns an error if the URB submission failed, 0 in all other cases. | ||
405 | */ | 821 | */ |
406 | static int retire_capture_sync_urb(struct snd_usb_substream *subs, | 822 | int snd_usb_endpoint_start(struct snd_usb_endpoint *ep) |
407 | struct snd_pcm_runtime *runtime, | ||
408 | struct urb *urb) | ||
409 | { | 823 | { |
824 | int err; | ||
825 | unsigned int i; | ||
826 | |||
827 | if (ep->chip->shutdown) | ||
828 | return -EBADFD; | ||
829 | |||
830 | /* already running? */ | ||
831 | if (++ep->use_count != 1) | ||
832 | return 0; | ||
833 | |||
834 | if (snd_BUG_ON(!test_bit(EP_FLAG_ACTIVATED, &ep->flags))) | ||
835 | return -EINVAL; | ||
836 | |||
837 | /* just to be sure */ | ||
838 | deactivate_urbs(ep, 0, 1); | ||
839 | wait_clear_urbs(ep); | ||
840 | |||
841 | ep->active_mask = 0; | ||
842 | ep->unlink_mask = 0; | ||
843 | ep->phase = 0; | ||
844 | |||
845 | /* | ||
846 | * If this endpoint has a data endpoint as implicit feedback source, | ||
847 | * don't start the urbs here. Instead, mark them all as available, | ||
848 | * wait for the record urbs to return and queue the playback urbs | ||
849 | * from that context. | ||
850 | */ | ||
851 | |||
852 | set_bit(EP_FLAG_RUNNING, &ep->flags); | ||
853 | |||
854 | if (snd_usb_endpoint_implict_feedback_sink(ep)) { | ||
855 | for (i = 0; i < ep->nurbs; i++) { | ||
856 | struct snd_urb_ctx *ctx = ep->urb + i; | ||
857 | list_add_tail(&ctx->ready_list, &ep->ready_playback_urbs); | ||
858 | } | ||
859 | |||
860 | return 0; | ||
861 | } | ||
862 | |||
863 | for (i = 0; i < ep->nurbs; i++) { | ||
864 | struct urb *urb = ep->urb[i].urb; | ||
865 | |||
866 | if (snd_BUG_ON(!urb)) | ||
867 | goto __error; | ||
868 | |||
869 | if (usb_pipeout(ep->pipe)) { | ||
870 | prepare_outbound_urb_sizes(ep, urb->context); | ||
871 | prepare_outbound_urb(ep, urb->context); | ||
872 | } else { | ||
873 | prepare_inbound_urb(ep, urb->context); | ||
874 | } | ||
875 | |||
876 | err = usb_submit_urb(urb, GFP_ATOMIC); | ||
877 | if (err < 0) { | ||
878 | snd_printk(KERN_ERR "cannot submit urb %d, error %d: %s\n", | ||
879 | i, err, usb_error_string(err)); | ||
880 | goto __error; | ||
881 | } | ||
882 | set_bit(i, &ep->active_mask); | ||
883 | } | ||
884 | |||
410 | return 0; | 885 | return 0; |
886 | |||
887 | __error: | ||
888 | clear_bit(EP_FLAG_RUNNING, &ep->flags); | ||
889 | ep->use_count--; | ||
890 | deactivate_urbs(ep, 0, 0); | ||
891 | return -EPIPE; | ||
411 | } | 892 | } |
412 | 893 | ||
413 | /* | 894 | /** |
414 | * prepare urb for capture data pipe | 895 | * snd_usb_endpoint_stop: stop an snd_usb_endpoint |
896 | * | ||
897 | * @ep: the endpoint to stop (may be NULL) | ||
415 | * | 898 | * |
416 | * fill the offset and length of each descriptor. | 899 | * A call to this function will decrement the use count of the endpoint. |
900 | * In case the last user has requested the endpoint stop, the URBs will | ||
901 | * actually be deactivated. | ||
417 | * | 902 | * |
418 | * we use a temporary buffer to write the captured data. | 903 | * Must be balanced to calls of snd_usb_endpoint_start(). |
419 | * since the length of written data is determined by host, we cannot | ||
420 | * write onto the pcm buffer directly... the data is thus copied | ||
421 | * later at complete callback to the global buffer. | ||
422 | */ | 904 | */ |
423 | static int prepare_capture_urb(struct snd_usb_substream *subs, | 905 | void snd_usb_endpoint_stop(struct snd_usb_endpoint *ep, |
424 | struct snd_pcm_runtime *runtime, | 906 | int force, int can_sleep, int wait) |
425 | struct urb *urb) | ||
426 | { | 907 | { |
427 | int i, offs; | 908 | if (!ep) |
428 | struct snd_urb_ctx *ctx = urb->context; | 909 | return; |
429 | 910 | ||
430 | offs = 0; | 911 | if (snd_BUG_ON(ep->use_count == 0)) |
431 | urb->dev = ctx->subs->dev; /* we need to set this at each time */ | 912 | return; |
432 | for (i = 0; i < ctx->packets; i++) { | 913 | |
433 | urb->iso_frame_desc[i].offset = offs; | 914 | if (snd_BUG_ON(!test_bit(EP_FLAG_ACTIVATED, &ep->flags))) |
434 | urb->iso_frame_desc[i].length = subs->curpacksize; | 915 | return; |
435 | offs += subs->curpacksize; | 916 | |
917 | if (--ep->use_count == 0) { | ||
918 | deactivate_urbs(ep, force, can_sleep); | ||
919 | ep->data_subs = NULL; | ||
920 | ep->sync_slave = NULL; | ||
921 | ep->retire_data_urb = NULL; | ||
922 | ep->prepare_data_urb = NULL; | ||
923 | |||
924 | if (wait) | ||
925 | wait_clear_urbs(ep); | ||
436 | } | 926 | } |
437 | urb->transfer_buffer_length = offs; | ||
438 | urb->number_of_packets = ctx->packets; | ||
439 | return 0; | ||
440 | } | 927 | } |
441 | 928 | ||
442 | /* | 929 | /** |
443 | * process after capture complete | 930 | * snd_usb_endpoint_activate: activate an snd_usb_endpoint |
931 | * | ||
932 | * @ep: the endpoint to activate | ||
933 | * | ||
934 | * If the endpoint is not currently in use, this functions will select the | ||
935 | * correct alternate interface setting for the interface of this endpoint. | ||
444 | * | 936 | * |
445 | * copy the data from each desctiptor to the pcm buffer, and | 937 | * In case of any active users, this functions does nothing. |
446 | * update the current position. | 938 | * |
939 | * Returns an error if usb_set_interface() failed, 0 in all other | ||
940 | * cases. | ||
447 | */ | 941 | */ |
448 | static int retire_capture_urb(struct snd_usb_substream *subs, | 942 | int snd_usb_endpoint_activate(struct snd_usb_endpoint *ep) |
449 | struct snd_pcm_runtime *runtime, | ||
450 | struct urb *urb) | ||
451 | { | 943 | { |
452 | unsigned long flags; | 944 | if (ep->use_count != 0) |
453 | unsigned char *cp; | 945 | return 0; |
454 | int i; | ||
455 | unsigned int stride, frames, bytes, oldptr; | ||
456 | int period_elapsed = 0; | ||
457 | 946 | ||
458 | stride = runtime->frame_bits >> 3; | 947 | if (!ep->chip->shutdown && |
948 | !test_and_set_bit(EP_FLAG_ACTIVATED, &ep->flags)) { | ||
949 | int ret; | ||
459 | 950 | ||
460 | for (i = 0; i < urb->number_of_packets; i++) { | 951 | ret = usb_set_interface(ep->chip->dev, ep->iface, ep->alt_idx); |
461 | cp = (unsigned char *)urb->transfer_buffer + urb->iso_frame_desc[i].offset; | 952 | if (ret < 0) { |
462 | if (urb->iso_frame_desc[i].status && printk_ratelimit()) { | 953 | snd_printk(KERN_ERR "%s() usb_set_interface() failed, ret = %d\n", |
463 | snd_printdd("frame %d active: %d\n", i, urb->iso_frame_desc[i].status); | 954 | __func__, ret); |
464 | // continue; | 955 | clear_bit(EP_FLAG_ACTIVATED, &ep->flags); |
465 | } | 956 | return ret; |
466 | bytes = urb->iso_frame_desc[i].actual_length; | ||
467 | frames = bytes / stride; | ||
468 | if (!subs->txfr_quirk) | ||
469 | bytes = frames * stride; | ||
470 | if (bytes % (runtime->sample_bits >> 3) != 0) { | ||
471 | #ifdef CONFIG_SND_DEBUG_VERBOSE | ||
472 | int oldbytes = bytes; | ||
473 | #endif | ||
474 | bytes = frames * stride; | ||
475 | snd_printdd(KERN_ERR "Corrected urb data len. %d->%d\n", | ||
476 | oldbytes, bytes); | ||
477 | } | ||
478 | /* update the current pointer */ | ||
479 | spin_lock_irqsave(&subs->lock, flags); | ||
480 | oldptr = subs->hwptr_done; | ||
481 | subs->hwptr_done += bytes; | ||
482 | if (subs->hwptr_done >= runtime->buffer_size * stride) | ||
483 | subs->hwptr_done -= runtime->buffer_size * stride; | ||
484 | frames = (bytes + (oldptr % stride)) / stride; | ||
485 | subs->transfer_done += frames; | ||
486 | if (subs->transfer_done >= runtime->period_size) { | ||
487 | subs->transfer_done -= runtime->period_size; | ||
488 | period_elapsed = 1; | ||
489 | } | ||
490 | spin_unlock_irqrestore(&subs->lock, flags); | ||
491 | /* copy a data chunk */ | ||
492 | if (oldptr + bytes > runtime->buffer_size * stride) { | ||
493 | unsigned int bytes1 = | ||
494 | runtime->buffer_size * stride - oldptr; | ||
495 | memcpy(runtime->dma_area + oldptr, cp, bytes1); | ||
496 | memcpy(runtime->dma_area, cp + bytes1, bytes - bytes1); | ||
497 | } else { | ||
498 | memcpy(runtime->dma_area + oldptr, cp, bytes); | ||
499 | } | 957 | } |
958 | |||
959 | return 0; | ||
500 | } | 960 | } |
501 | if (period_elapsed) | 961 | |
502 | snd_pcm_period_elapsed(subs->pcm_substream); | 962 | return -EBUSY; |
503 | return 0; | ||
504 | } | 963 | } |
505 | 964 | ||
506 | /* | 965 | /** |
507 | * Process after capture complete when paused. Nothing to do. | 966 | * snd_usb_endpoint_deactivate: deactivate an snd_usb_endpoint |
967 | * | ||
968 | * @ep: the endpoint to deactivate | ||
969 | * | ||
970 | * If the endpoint is not currently in use, this functions will select the | ||
971 | * alternate interface setting 0 for the interface of this endpoint. | ||
972 | * | ||
973 | * In case of any active users, this functions does nothing. | ||
974 | * | ||
975 | * Returns an error if usb_set_interface() failed, 0 in all other | ||
976 | * cases. | ||
508 | */ | 977 | */ |
509 | static int retire_paused_capture_urb(struct snd_usb_substream *subs, | 978 | int snd_usb_endpoint_deactivate(struct snd_usb_endpoint *ep) |
510 | struct snd_pcm_runtime *runtime, | ||
511 | struct urb *urb) | ||
512 | { | 979 | { |
513 | return 0; | 980 | if (!ep) |
514 | } | 981 | return -EINVAL; |
515 | 982 | ||
983 | if (ep->use_count != 0) | ||
984 | return 0; | ||
516 | 985 | ||
517 | /* | 986 | if (!ep->chip->shutdown && |
518 | * prepare urb for playback sync pipe | 987 | test_and_clear_bit(EP_FLAG_ACTIVATED, &ep->flags)) { |
988 | int ret; | ||
989 | |||
990 | ret = usb_set_interface(ep->chip->dev, ep->iface, 0); | ||
991 | if (ret < 0) { | ||
992 | snd_printk(KERN_ERR "%s(): usb_set_interface() failed, ret = %d\n", | ||
993 | __func__, ret); | ||
994 | return ret; | ||
995 | } | ||
996 | |||
997 | return 0; | ||
998 | } | ||
999 | |||
1000 | return -EBUSY; | ||
1001 | } | ||
1002 | |||
1003 | /** | ||
1004 | * snd_usb_endpoint_free: Free the resources of an snd_usb_endpoint | ||
1005 | * | ||
1006 | * @ep: the list header of the endpoint to free | ||
519 | * | 1007 | * |
520 | * set up the offset and length to receive the current frequency. | 1008 | * This function does not care for the endpoint's use count but will tear |
1009 | * down all the streaming URBs immediately and free all resources. | ||
521 | */ | 1010 | */ |
522 | static int prepare_playback_sync_urb(struct snd_usb_substream *subs, | 1011 | void snd_usb_endpoint_free(struct list_head *head) |
523 | struct snd_pcm_runtime *runtime, | ||
524 | struct urb *urb) | ||
525 | { | 1012 | { |
526 | struct snd_urb_ctx *ctx = urb->context; | 1013 | struct snd_usb_endpoint *ep; |
527 | 1014 | ||
528 | urb->dev = ctx->subs->dev; /* we need to set this at each time */ | 1015 | ep = list_entry(head, struct snd_usb_endpoint, list); |
529 | urb->iso_frame_desc[0].length = min(4u, ctx->subs->syncmaxsize); | 1016 | release_urbs(ep, 1); |
530 | urb->iso_frame_desc[0].offset = 0; | 1017 | kfree(ep); |
531 | return 0; | ||
532 | } | 1018 | } |
533 | 1019 | ||
534 | /* | 1020 | /** |
535 | * process after playback sync complete | 1021 | * snd_usb_handle_sync_urb: parse an USB sync packet |
536 | * | 1022 | * |
537 | * Full speed devices report feedback values in 10.14 format as samples per | 1023 | * @ep: the endpoint to handle the packet |
538 | * frame, high speed devices in 16.16 format as samples per microframe. | 1024 | * @sender: the sending endpoint |
539 | * Because the Audio Class 1 spec was written before USB 2.0, many high speed | 1025 | * @urb: the received packet |
540 | * devices use a wrong interpretation, some others use an entirely different | 1026 | * |
541 | * format. Therefore, we cannot predict what format any particular device uses | 1027 | * This function is called from the context of an endpoint that received |
542 | * and must detect it automatically. | 1028 | * the packet and is used to let another endpoint object handle the payload. |
543 | */ | 1029 | */ |
544 | static int retire_playback_sync_urb(struct snd_usb_substream *subs, | 1030 | void snd_usb_handle_sync_urb(struct snd_usb_endpoint *ep, |
545 | struct snd_pcm_runtime *runtime, | 1031 | struct snd_usb_endpoint *sender, |
546 | struct urb *urb) | 1032 | const struct urb *urb) |
547 | { | 1033 | { |
548 | unsigned int f; | ||
549 | int shift; | 1034 | int shift; |
1035 | unsigned int f; | ||
550 | unsigned long flags; | 1036 | unsigned long flags; |
551 | 1037 | ||
1038 | snd_BUG_ON(ep == sender); | ||
1039 | |||
1040 | /* | ||
1041 | * In case the endpoint is operating in implicit feedback mode, prepare | ||
1042 | * a new outbound URB that has the same layout as the received packet | ||
1043 | * and add it to the list of pending urbs. queue_pending_output_urbs() | ||
1044 | * will take care of them later. | ||
1045 | */ | ||
1046 | if (snd_usb_endpoint_implict_feedback_sink(ep) && | ||
1047 | ep->use_count != 0) { | ||
1048 | |||
1049 | /* implicit feedback case */ | ||
1050 | int i, bytes = 0; | ||
1051 | struct snd_urb_ctx *in_ctx; | ||
1052 | struct snd_usb_packet_info *out_packet; | ||
1053 | |||
1054 | in_ctx = urb->context; | ||
1055 | |||
1056 | /* Count overall packet size */ | ||
1057 | for (i = 0; i < in_ctx->packets; i++) | ||
1058 | if (urb->iso_frame_desc[i].status == 0) | ||
1059 | bytes += urb->iso_frame_desc[i].actual_length; | ||
1060 | |||
1061 | /* | ||
1062 | * skip empty packets. At least M-Audio's Fast Track Ultra stops | ||
1063 | * streaming once it received a 0-byte OUT URB | ||
1064 | */ | ||
1065 | if (bytes == 0) | ||
1066 | return; | ||
1067 | |||
1068 | spin_lock_irqsave(&ep->lock, flags); | ||
1069 | out_packet = ep->next_packet + ep->next_packet_write_pos; | ||
1070 | |||
1071 | /* | ||
1072 | * Iterate through the inbound packet and prepare the lengths | ||
1073 | * for the output packet. The OUT packet we are about to send | ||
1074 | * will have the same amount of payload bytes than the IN | ||
1075 | * packet we just received. | ||
1076 | */ | ||
1077 | |||
1078 | out_packet->packets = in_ctx->packets; | ||
1079 | for (i = 0; i < in_ctx->packets; i++) { | ||
1080 | if (urb->iso_frame_desc[i].status == 0) | ||
1081 | out_packet->packet_size[i] = | ||
1082 | urb->iso_frame_desc[i].actual_length / ep->stride; | ||
1083 | else | ||
1084 | out_packet->packet_size[i] = 0; | ||
1085 | } | ||
1086 | |||
1087 | ep->next_packet_write_pos++; | ||
1088 | ep->next_packet_write_pos %= MAX_URBS; | ||
1089 | spin_unlock_irqrestore(&ep->lock, flags); | ||
1090 | queue_pending_output_urbs(ep); | ||
1091 | |||
1092 | return; | ||
1093 | } | ||
1094 | |||
1095 | /* | ||
1096 | * process after playback sync complete | ||
1097 | * | ||
1098 | * Full speed devices report feedback values in 10.14 format as samples | ||
1099 | * per frame, high speed devices in 16.16 format as samples per | ||
1100 | * microframe. | ||
1101 | * | ||
1102 | * Because the Audio Class 1 spec was written before USB 2.0, many high | ||
1103 | * speed devices use a wrong interpretation, some others use an | ||
1104 | * entirely different format. | ||
1105 | * | ||
1106 | * Therefore, we cannot predict what format any particular device uses | ||
1107 | * and must detect it automatically. | ||
1108 | */ | ||
1109 | |||
552 | if (urb->iso_frame_desc[0].status != 0 || | 1110 | if (urb->iso_frame_desc[0].status != 0 || |
553 | urb->iso_frame_desc[0].actual_length < 3) | 1111 | urb->iso_frame_desc[0].actual_length < 3) |
554 | return 0; | 1112 | return; |
555 | 1113 | ||
556 | f = le32_to_cpup(urb->transfer_buffer); | 1114 | f = le32_to_cpup(urb->transfer_buffer); |
557 | if (urb->iso_frame_desc[0].actual_length == 3) | 1115 | if (urb->iso_frame_desc[0].actual_length == 3) |
558 | f &= 0x00ffffff; | 1116 | f &= 0x00ffffff; |
559 | else | 1117 | else |
560 | f &= 0x0fffffff; | 1118 | f &= 0x0fffffff; |
1119 | |||
561 | if (f == 0) | 1120 | if (f == 0) |
562 | return 0; | 1121 | return; |
563 | 1122 | ||
564 | if (unlikely(subs->freqshift == INT_MIN)) { | 1123 | if (unlikely(ep->freqshift == INT_MIN)) { |
565 | /* | 1124 | /* |
566 | * The first time we see a feedback value, determine its format | 1125 | * The first time we see a feedback value, determine its format |
567 | * by shifting it left or right until it matches the nominal | 1126 | * by shifting it left or right until it matches the nominal |
@@ -569,398 +1128,34 @@ static int retire_playback_sync_urb(struct snd_usb_substream *subs, | |||
569 | * differ from the nominal value more than +50% or -25%. | 1128 | * differ from the nominal value more than +50% or -25%. |
570 | */ | 1129 | */ |
571 | shift = 0; | 1130 | shift = 0; |
572 | while (f < subs->freqn - subs->freqn / 4) { | 1131 | while (f < ep->freqn - ep->freqn / 4) { |
573 | f <<= 1; | 1132 | f <<= 1; |
574 | shift++; | 1133 | shift++; |
575 | } | 1134 | } |
576 | while (f > subs->freqn + subs->freqn / 2) { | 1135 | while (f > ep->freqn + ep->freqn / 2) { |
577 | f >>= 1; | 1136 | f >>= 1; |
578 | shift--; | 1137 | shift--; |
579 | } | 1138 | } |
580 | subs->freqshift = shift; | 1139 | ep->freqshift = shift; |
581 | } | 1140 | } else if (ep->freqshift >= 0) |
582 | else if (subs->freqshift >= 0) | 1141 | f <<= ep->freqshift; |
583 | f <<= subs->freqshift; | ||
584 | else | 1142 | else |
585 | f >>= -subs->freqshift; | 1143 | f >>= -ep->freqshift; |
586 | 1144 | ||
587 | if (likely(f >= subs->freqn - subs->freqn / 8 && f <= subs->freqmax)) { | 1145 | if (likely(f >= ep->freqn - ep->freqn / 8 && f <= ep->freqmax)) { |
588 | /* | 1146 | /* |
589 | * If the frequency looks valid, set it. | 1147 | * If the frequency looks valid, set it. |
590 | * This value is referred to in prepare_playback_urb(). | 1148 | * This value is referred to in prepare_playback_urb(). |
591 | */ | 1149 | */ |
592 | spin_lock_irqsave(&subs->lock, flags); | 1150 | spin_lock_irqsave(&ep->lock, flags); |
593 | subs->freqm = f; | 1151 | ep->freqm = f; |
594 | spin_unlock_irqrestore(&subs->lock, flags); | 1152 | spin_unlock_irqrestore(&ep->lock, flags); |
595 | } else { | 1153 | } else { |
596 | /* | 1154 | /* |
597 | * Out of range; maybe the shift value is wrong. | 1155 | * Out of range; maybe the shift value is wrong. |
598 | * Reset it so that we autodetect again the next time. | 1156 | * Reset it so that we autodetect again the next time. |
599 | */ | 1157 | */ |
600 | subs->freqshift = INT_MIN; | 1158 | ep->freqshift = INT_MIN; |
601 | } | ||
602 | |||
603 | return 0; | ||
604 | } | ||
605 | |||
606 | /* determine the number of frames in the next packet */ | ||
607 | static int snd_usb_audio_next_packet_size(struct snd_usb_substream *subs) | ||
608 | { | ||
609 | if (subs->fill_max) | ||
610 | return subs->maxframesize; | ||
611 | else { | ||
612 | subs->phase = (subs->phase & 0xffff) | ||
613 | + (subs->freqm << subs->datainterval); | ||
614 | return min(subs->phase >> 16, subs->maxframesize); | ||
615 | } | 1159 | } |
616 | } | 1160 | } |
617 | 1161 | ||
618 | /* | ||
619 | * Prepare urb for streaming before playback starts or when paused. | ||
620 | * | ||
621 | * We don't have any data, so we send silence. | ||
622 | */ | ||
623 | static int prepare_nodata_playback_urb(struct snd_usb_substream *subs, | ||
624 | struct snd_pcm_runtime *runtime, | ||
625 | struct urb *urb) | ||
626 | { | ||
627 | unsigned int i, offs, counts; | ||
628 | struct snd_urb_ctx *ctx = urb->context; | ||
629 | int stride = runtime->frame_bits >> 3; | ||
630 | |||
631 | offs = 0; | ||
632 | urb->dev = ctx->subs->dev; | ||
633 | for (i = 0; i < ctx->packets; ++i) { | ||
634 | counts = snd_usb_audio_next_packet_size(subs); | ||
635 | urb->iso_frame_desc[i].offset = offs * stride; | ||
636 | urb->iso_frame_desc[i].length = counts * stride; | ||
637 | offs += counts; | ||
638 | } | ||
639 | urb->number_of_packets = ctx->packets; | ||
640 | urb->transfer_buffer_length = offs * stride; | ||
641 | memset(urb->transfer_buffer, | ||
642 | runtime->format == SNDRV_PCM_FORMAT_U8 ? 0x80 : 0, | ||
643 | offs * stride); | ||
644 | return 0; | ||
645 | } | ||
646 | |||
647 | /* | ||
648 | * prepare urb for playback data pipe | ||
649 | * | ||
650 | * Since a URB can handle only a single linear buffer, we must use double | ||
651 | * buffering when the data to be transferred overflows the buffer boundary. | ||
652 | * To avoid inconsistencies when updating hwptr_done, we use double buffering | ||
653 | * for all URBs. | ||
654 | */ | ||
655 | static int prepare_playback_urb(struct snd_usb_substream *subs, | ||
656 | struct snd_pcm_runtime *runtime, | ||
657 | struct urb *urb) | ||
658 | { | ||
659 | int i, stride; | ||
660 | unsigned int counts, frames, bytes; | ||
661 | unsigned long flags; | ||
662 | int period_elapsed = 0; | ||
663 | struct snd_urb_ctx *ctx = urb->context; | ||
664 | |||
665 | stride = runtime->frame_bits >> 3; | ||
666 | |||
667 | frames = 0; | ||
668 | urb->dev = ctx->subs->dev; /* we need to set this at each time */ | ||
669 | urb->number_of_packets = 0; | ||
670 | spin_lock_irqsave(&subs->lock, flags); | ||
671 | for (i = 0; i < ctx->packets; i++) { | ||
672 | counts = snd_usb_audio_next_packet_size(subs); | ||
673 | /* set up descriptor */ | ||
674 | urb->iso_frame_desc[i].offset = frames * stride; | ||
675 | urb->iso_frame_desc[i].length = counts * stride; | ||
676 | frames += counts; | ||
677 | urb->number_of_packets++; | ||
678 | subs->transfer_done += counts; | ||
679 | if (subs->transfer_done >= runtime->period_size) { | ||
680 | subs->transfer_done -= runtime->period_size; | ||
681 | period_elapsed = 1; | ||
682 | if (subs->fmt_type == UAC_FORMAT_TYPE_II) { | ||
683 | if (subs->transfer_done > 0) { | ||
684 | /* FIXME: fill-max mode is not | ||
685 | * supported yet */ | ||
686 | frames -= subs->transfer_done; | ||
687 | counts -= subs->transfer_done; | ||
688 | urb->iso_frame_desc[i].length = | ||
689 | counts * stride; | ||
690 | subs->transfer_done = 0; | ||
691 | } | ||
692 | i++; | ||
693 | if (i < ctx->packets) { | ||
694 | /* add a transfer delimiter */ | ||
695 | urb->iso_frame_desc[i].offset = | ||
696 | frames * stride; | ||
697 | urb->iso_frame_desc[i].length = 0; | ||
698 | urb->number_of_packets++; | ||
699 | } | ||
700 | break; | ||
701 | } | ||
702 | } | ||
703 | if (period_elapsed) /* finish at the period boundary */ | ||
704 | break; | ||
705 | } | ||
706 | bytes = frames * stride; | ||
707 | if (subs->hwptr_done + bytes > runtime->buffer_size * stride) { | ||
708 | /* err, the transferred area goes over buffer boundary. */ | ||
709 | unsigned int bytes1 = | ||
710 | runtime->buffer_size * stride - subs->hwptr_done; | ||
711 | memcpy(urb->transfer_buffer, | ||
712 | runtime->dma_area + subs->hwptr_done, bytes1); | ||
713 | memcpy(urb->transfer_buffer + bytes1, | ||
714 | runtime->dma_area, bytes - bytes1); | ||
715 | } else { | ||
716 | memcpy(urb->transfer_buffer, | ||
717 | runtime->dma_area + subs->hwptr_done, bytes); | ||
718 | } | ||
719 | subs->hwptr_done += bytes; | ||
720 | if (subs->hwptr_done >= runtime->buffer_size * stride) | ||
721 | subs->hwptr_done -= runtime->buffer_size * stride; | ||
722 | |||
723 | /* update delay with exact number of samples queued */ | ||
724 | runtime->delay = subs->last_delay; | ||
725 | runtime->delay += frames; | ||
726 | subs->last_delay = runtime->delay; | ||
727 | |||
728 | /* realign last_frame_number */ | ||
729 | subs->last_frame_number = usb_get_current_frame_number(subs->dev); | ||
730 | subs->last_frame_number &= 0xFF; /* keep 8 LSBs */ | ||
731 | |||
732 | spin_unlock_irqrestore(&subs->lock, flags); | ||
733 | urb->transfer_buffer_length = bytes; | ||
734 | if (period_elapsed) | ||
735 | snd_pcm_period_elapsed(subs->pcm_substream); | ||
736 | return 0; | ||
737 | } | ||
738 | |||
739 | /* | ||
740 | * process after playback data complete | ||
741 | * - decrease the delay count again | ||
742 | */ | ||
743 | static int retire_playback_urb(struct snd_usb_substream *subs, | ||
744 | struct snd_pcm_runtime *runtime, | ||
745 | struct urb *urb) | ||
746 | { | ||
747 | unsigned long flags; | ||
748 | int stride = runtime->frame_bits >> 3; | ||
749 | int processed = urb->transfer_buffer_length / stride; | ||
750 | int est_delay; | ||
751 | |||
752 | spin_lock_irqsave(&subs->lock, flags); | ||
753 | |||
754 | est_delay = snd_usb_pcm_delay(subs, runtime->rate); | ||
755 | /* update delay with exact number of samples played */ | ||
756 | if (processed > subs->last_delay) | ||
757 | subs->last_delay = 0; | ||
758 | else | ||
759 | subs->last_delay -= processed; | ||
760 | runtime->delay = subs->last_delay; | ||
761 | |||
762 | /* | ||
763 | * Report when delay estimate is off by more than 2ms. | ||
764 | * The error should be lower than 2ms since the estimate relies | ||
765 | * on two reads of a counter updated every ms. | ||
766 | */ | ||
767 | if (abs(est_delay - subs->last_delay) * 1000 > runtime->rate * 2) | ||
768 | snd_printk(KERN_DEBUG "delay: estimated %d, actual %d\n", | ||
769 | est_delay, subs->last_delay); | ||
770 | |||
771 | spin_unlock_irqrestore(&subs->lock, flags); | ||
772 | return 0; | ||
773 | } | ||
774 | |||
775 | static const char *usb_error_string(int err) | ||
776 | { | ||
777 | switch (err) { | ||
778 | case -ENODEV: | ||
779 | return "no device"; | ||
780 | case -ENOENT: | ||
781 | return "endpoint not enabled"; | ||
782 | case -EPIPE: | ||
783 | return "endpoint stalled"; | ||
784 | case -ENOSPC: | ||
785 | return "not enough bandwidth"; | ||
786 | case -ESHUTDOWN: | ||
787 | return "device disabled"; | ||
788 | case -EHOSTUNREACH: | ||
789 | return "device suspended"; | ||
790 | case -EINVAL: | ||
791 | case -EAGAIN: | ||
792 | case -EFBIG: | ||
793 | case -EMSGSIZE: | ||
794 | return "internal error"; | ||
795 | default: | ||
796 | return "unknown error"; | ||
797 | } | ||
798 | } | ||
799 | |||
800 | /* | ||
801 | * set up and start data/sync urbs | ||
802 | */ | ||
803 | static int start_urbs(struct snd_usb_substream *subs, struct snd_pcm_runtime *runtime) | ||
804 | { | ||
805 | unsigned int i; | ||
806 | int err; | ||
807 | |||
808 | if (subs->stream->chip->shutdown) | ||
809 | return -EBADFD; | ||
810 | |||
811 | for (i = 0; i < subs->nurbs; i++) { | ||
812 | if (snd_BUG_ON(!subs->dataurb[i].urb)) | ||
813 | return -EINVAL; | ||
814 | if (subs->ops.prepare(subs, runtime, subs->dataurb[i].urb) < 0) { | ||
815 | snd_printk(KERN_ERR "cannot prepare datapipe for urb %d\n", i); | ||
816 | goto __error; | ||
817 | } | ||
818 | } | ||
819 | if (subs->syncpipe) { | ||
820 | for (i = 0; i < SYNC_URBS; i++) { | ||
821 | if (snd_BUG_ON(!subs->syncurb[i].urb)) | ||
822 | return -EINVAL; | ||
823 | if (subs->ops.prepare_sync(subs, runtime, subs->syncurb[i].urb) < 0) { | ||
824 | snd_printk(KERN_ERR "cannot prepare syncpipe for urb %d\n", i); | ||
825 | goto __error; | ||
826 | } | ||
827 | } | ||
828 | } | ||
829 | |||
830 | subs->active_mask = 0; | ||
831 | subs->unlink_mask = 0; | ||
832 | subs->running = 1; | ||
833 | for (i = 0; i < subs->nurbs; i++) { | ||
834 | err = usb_submit_urb(subs->dataurb[i].urb, GFP_ATOMIC); | ||
835 | if (err < 0) { | ||
836 | snd_printk(KERN_ERR "cannot submit datapipe " | ||
837 | "for urb %d, error %d: %s\n", | ||
838 | i, err, usb_error_string(err)); | ||
839 | goto __error; | ||
840 | } | ||
841 | set_bit(i, &subs->active_mask); | ||
842 | } | ||
843 | if (subs->syncpipe) { | ||
844 | for (i = 0; i < SYNC_URBS; i++) { | ||
845 | err = usb_submit_urb(subs->syncurb[i].urb, GFP_ATOMIC); | ||
846 | if (err < 0) { | ||
847 | snd_printk(KERN_ERR "cannot submit syncpipe " | ||
848 | "for urb %d, error %d: %s\n", | ||
849 | i, err, usb_error_string(err)); | ||
850 | goto __error; | ||
851 | } | ||
852 | set_bit(i + 16, &subs->active_mask); | ||
853 | } | ||
854 | } | ||
855 | return 0; | ||
856 | |||
857 | __error: | ||
858 | // snd_pcm_stop(subs->pcm_substream, SNDRV_PCM_STATE_XRUN); | ||
859 | deactivate_urbs(subs, 0, 0); | ||
860 | return -EPIPE; | ||
861 | } | ||
862 | |||
863 | |||
864 | /* | ||
865 | */ | ||
866 | static struct snd_urb_ops audio_urb_ops[2] = { | ||
867 | { | ||
868 | .prepare = prepare_nodata_playback_urb, | ||
869 | .retire = retire_playback_urb, | ||
870 | .prepare_sync = prepare_playback_sync_urb, | ||
871 | .retire_sync = retire_playback_sync_urb, | ||
872 | }, | ||
873 | { | ||
874 | .prepare = prepare_capture_urb, | ||
875 | .retire = retire_capture_urb, | ||
876 | .prepare_sync = prepare_capture_sync_urb, | ||
877 | .retire_sync = retire_capture_sync_urb, | ||
878 | }, | ||
879 | }; | ||
880 | |||
881 | /* | ||
882 | * initialize the substream instance. | ||
883 | */ | ||
884 | |||
885 | void snd_usb_init_substream(struct snd_usb_stream *as, | ||
886 | int stream, struct audioformat *fp) | ||
887 | { | ||
888 | struct snd_usb_substream *subs = &as->substream[stream]; | ||
889 | |||
890 | INIT_LIST_HEAD(&subs->fmt_list); | ||
891 | spin_lock_init(&subs->lock); | ||
892 | |||
893 | subs->stream = as; | ||
894 | subs->direction = stream; | ||
895 | subs->dev = as->chip->dev; | ||
896 | subs->txfr_quirk = as->chip->txfr_quirk; | ||
897 | subs->ops = audio_urb_ops[stream]; | ||
898 | if (snd_usb_get_speed(subs->dev) >= USB_SPEED_HIGH) | ||
899 | subs->ops.prepare_sync = prepare_capture_sync_urb_hs; | ||
900 | |||
901 | snd_usb_set_pcm_ops(as->pcm, stream); | ||
902 | |||
903 | list_add_tail(&fp->list, &subs->fmt_list); | ||
904 | subs->formats |= fp->formats; | ||
905 | subs->endpoint = fp->endpoint; | ||
906 | subs->num_formats++; | ||
907 | subs->fmt_type = fp->fmt_type; | ||
908 | } | ||
909 | |||
910 | int snd_usb_substream_playback_trigger(struct snd_pcm_substream *substream, int cmd) | ||
911 | { | ||
912 | struct snd_usb_substream *subs = substream->runtime->private_data; | ||
913 | |||
914 | switch (cmd) { | ||
915 | case SNDRV_PCM_TRIGGER_START: | ||
916 | case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: | ||
917 | subs->ops.prepare = prepare_playback_urb; | ||
918 | return 0; | ||
919 | case SNDRV_PCM_TRIGGER_STOP: | ||
920 | return deactivate_urbs(subs, 0, 0); | ||
921 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: | ||
922 | subs->ops.prepare = prepare_nodata_playback_urb; | ||
923 | return 0; | ||
924 | } | ||
925 | |||
926 | return -EINVAL; | ||
927 | } | ||
928 | |||
929 | int snd_usb_substream_capture_trigger(struct snd_pcm_substream *substream, int cmd) | ||
930 | { | ||
931 | struct snd_usb_substream *subs = substream->runtime->private_data; | ||
932 | |||
933 | switch (cmd) { | ||
934 | case SNDRV_PCM_TRIGGER_START: | ||
935 | subs->ops.retire = retire_capture_urb; | ||
936 | return start_urbs(subs, substream->runtime); | ||
937 | case SNDRV_PCM_TRIGGER_STOP: | ||
938 | return deactivate_urbs(subs, 0, 0); | ||
939 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: | ||
940 | subs->ops.retire = retire_paused_capture_urb; | ||
941 | return 0; | ||
942 | case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: | ||
943 | subs->ops.retire = retire_capture_urb; | ||
944 | return 0; | ||
945 | } | ||
946 | |||
947 | return -EINVAL; | ||
948 | } | ||
949 | |||
950 | int snd_usb_substream_prepare(struct snd_usb_substream *subs, | ||
951 | struct snd_pcm_runtime *runtime) | ||
952 | { | ||
953 | /* clear urbs (to be sure) */ | ||
954 | deactivate_urbs(subs, 0, 1); | ||
955 | wait_clear_urbs(subs); | ||
956 | |||
957 | /* for playback, submit the URBs now; otherwise, the first hwptr_done | ||
958 | * updates for all URBs would happen at the same time when starting */ | ||
959 | if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK) { | ||
960 | subs->ops.prepare = prepare_nodata_playback_urb; | ||
961 | return start_urbs(subs, runtime); | ||
962 | } | ||
963 | |||
964 | return 0; | ||
965 | } | ||
966 | |||