Commit | Line | Data |
---|---|---|
e5779998 DM |
1 | /* |
2 | * This program is free software; you can redistribute it and/or modify | |
3 | * it under the terms of the GNU General Public License as published by | |
4 | * the Free Software Foundation; either version 2 of the License, or | |
5 | * (at your option) any later version. | |
6 | * | |
7 | * This program is distributed in the hope that it will be useful, | |
8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
10 | * GNU General Public License for more details. | |
11 | * | |
12 | * You should have received a copy of the GNU General Public License | |
13 | * along with this program; if not, write to the Free Software | |
14 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
15 | * | |
16 | */ | |
17 | ||
c731bc96 DM |
18 | #include <linux/gfp.h> |
19 | #include <linux/init.h> | |
80c8a2a3 | 20 | #include <linux/ratelimit.h> |
c731bc96 DM |
21 | #include <linux/usb.h> |
22 | #include <linux/usb/audio.h> | |
8fdff6a3 | 23 | #include <linux/slab.h> |
c731bc96 DM |
24 | |
25 | #include <sound/core.h> | |
26 | #include <sound/pcm.h> | |
8fdff6a3 | 27 | #include <sound/pcm_params.h> |
c731bc96 DM |
28 | |
29 | #include "usbaudio.h" | |
30 | #include "helper.h" | |
31 | #include "card.h" | |
32 | #include "endpoint.h" | |
33 | #include "pcm.h" | |
34 | ||
8fdff6a3 DM |
35 | #define EP_FLAG_ACTIVATED 0 |
36 | #define EP_FLAG_RUNNING 1 | |
37 | ||
c731bc96 DM |
38 | /* |
39 | * convert a sampling rate into our full speed format (fs/1000 in Q16.16) | |
40 | * this will overflow at approx 524 kHz | |
41 | */ | |
42 | static inline unsigned get_usb_full_speed_rate(unsigned int rate) | |
43 | { | |
44 | return ((rate << 13) + 62) / 125; | |
45 | } | |
46 | ||
47 | /* | |
48 | * convert a sampling rate into USB high speed format (fs/8000 in Q16.16) | |
49 | * this will overflow at approx 4 MHz | |
50 | */ | |
51 | static inline unsigned get_usb_high_speed_rate(unsigned int rate) | |
52 | { | |
53 | return ((rate << 10) + 62) / 125; | |
54 | } | |
55 | ||
c731bc96 DM |
56 | /* |
57 | * release a urb data | |
58 | */ | |
59 | static void release_urb_ctx(struct snd_urb_ctx *u) | |
60 | { | |
d399ff95 DM |
61 | if (u->buffer_size) |
62 | usb_free_coherent(u->ep->chip->dev, u->buffer_size, | |
63 | u->urb->transfer_buffer, | |
64 | u->urb->transfer_dma); | |
65 | usb_free_urb(u->urb); | |
66 | u->urb = NULL; | |
c731bc96 DM |
67 | } |
68 | ||
69 | static const char *usb_error_string(int err) | |
70 | { | |
71 | switch (err) { | |
72 | case -ENODEV: | |
73 | return "no device"; | |
74 | case -ENOENT: | |
75 | return "endpoint not enabled"; | |
76 | case -EPIPE: | |
77 | return "endpoint stalled"; | |
78 | case -ENOSPC: | |
79 | return "not enough bandwidth"; | |
80 | case -ESHUTDOWN: | |
81 | return "device disabled"; | |
82 | case -EHOSTUNREACH: | |
83 | return "device suspended"; | |
84 | case -EINVAL: | |
85 | case -EAGAIN: | |
86 | case -EFBIG: | |
87 | case -EMSGSIZE: | |
88 | return "internal error"; | |
89 | default: | |
90 | return "unknown error"; | |
91 | } | |
92 | } | |
93 | ||
8fdff6a3 DM |
94 | int snd_usb_endpoint_implict_feedback_sink(struct snd_usb_endpoint *ep) |
95 | { | |
96 | return ep->sync_master && | |
97 | ep->sync_master->type == SND_USB_ENDPOINT_TYPE_DATA && | |
98 | ep->type == SND_USB_ENDPOINT_TYPE_DATA && | |
99 | usb_pipeout(ep->pipe); | |
100 | } | |
101 | ||
102 | /* determine the number of frames in the next packet */ | |
103 | static int next_packet_size(struct snd_usb_endpoint *ep) | |
104 | { | |
105 | unsigned long flags; | |
106 | int ret; | |
107 | ||
108 | if (ep->fill_max) | |
109 | return ep->maxframesize; | |
110 | ||
111 | spin_lock_irqsave(&ep->lock, flags); | |
112 | ep->phase = (ep->phase & 0xffff) | |
113 | + (ep->freqm << ep->datainterval); | |
114 | ret = min(ep->phase >> 16, ep->maxframesize); | |
115 | spin_unlock_irqrestore(&ep->lock, flags); | |
116 | ||
117 | return ret; | |
118 | } | |
119 | ||
120 | static void retire_outbound_urb(struct snd_usb_endpoint *ep, | |
121 | struct snd_urb_ctx *urb_ctx) | |
122 | { | |
123 | if (ep->retire_data_urb) | |
124 | ep->retire_data_urb(ep->data_subs, urb_ctx->urb); | |
125 | } | |
126 | ||
127 | static void retire_inbound_urb(struct snd_usb_endpoint *ep, | |
128 | struct snd_urb_ctx *urb_ctx) | |
129 | { | |
130 | struct urb *urb = urb_ctx->urb; | |
131 | ||
132 | if (ep->sync_slave) | |
133 | snd_usb_handle_sync_urb(ep->sync_slave, ep, urb); | |
134 | ||
135 | if (ep->retire_data_urb) | |
136 | ep->retire_data_urb(ep->data_subs, urb); | |
137 | } | |
138 | ||
139 | static void prepare_outbound_urb_sizes(struct snd_usb_endpoint *ep, | |
140 | struct snd_urb_ctx *ctx) | |
141 | { | |
142 | int i; | |
143 | ||
144 | for (i = 0; i < ctx->packets; ++i) | |
145 | ctx->packet_size[i] = next_packet_size(ep); | |
146 | } | |
147 | ||
148 | /* | |
149 | * Prepare a PLAYBACK urb for submission to the bus. | |
150 | */ | |
151 | static void prepare_outbound_urb(struct snd_usb_endpoint *ep, | |
152 | struct snd_urb_ctx *ctx) | |
153 | { | |
154 | int i; | |
155 | struct urb *urb = ctx->urb; | |
156 | unsigned char *cp = urb->transfer_buffer; | |
157 | ||
158 | urb->dev = ep->chip->dev; /* we need to set this at each time */ | |
159 | ||
160 | switch (ep->type) { | |
161 | case SND_USB_ENDPOINT_TYPE_DATA: | |
162 | if (ep->prepare_data_urb) { | |
163 | ep->prepare_data_urb(ep->data_subs, urb); | |
164 | } else { | |
165 | /* no data provider, so send silence */ | |
166 | unsigned int offs = 0; | |
167 | for (i = 0; i < ctx->packets; ++i) { | |
168 | int counts = ctx->packet_size[i]; | |
169 | urb->iso_frame_desc[i].offset = offs * ep->stride; | |
170 | urb->iso_frame_desc[i].length = counts * ep->stride; | |
171 | offs += counts; | |
172 | } | |
173 | ||
174 | urb->number_of_packets = ctx->packets; | |
175 | urb->transfer_buffer_length = offs * ep->stride; | |
176 | memset(urb->transfer_buffer, ep->silence_value, | |
177 | offs * ep->stride); | |
178 | } | |
179 | break; | |
180 | ||
181 | case SND_USB_ENDPOINT_TYPE_SYNC: | |
182 | if (snd_usb_get_speed(ep->chip->dev) >= USB_SPEED_HIGH) { | |
183 | /* | |
184 | * fill the length and offset of each urb descriptor. | |
185 | * the fixed 12.13 frequency is passed as 16.16 through the pipe. | |
186 | */ | |
187 | urb->iso_frame_desc[0].length = 4; | |
188 | urb->iso_frame_desc[0].offset = 0; | |
189 | cp[0] = ep->freqn; | |
190 | cp[1] = ep->freqn >> 8; | |
191 | cp[2] = ep->freqn >> 16; | |
192 | cp[3] = ep->freqn >> 24; | |
193 | } else { | |
194 | /* | |
195 | * fill the length and offset of each urb descriptor. | |
196 | * the fixed 10.14 frequency is passed through the pipe. | |
197 | */ | |
198 | urb->iso_frame_desc[0].length = 3; | |
199 | urb->iso_frame_desc[0].offset = 0; | |
200 | cp[0] = ep->freqn >> 2; | |
201 | cp[1] = ep->freqn >> 10; | |
202 | cp[2] = ep->freqn >> 18; | |
203 | } | |
204 | ||
205 | break; | |
206 | } | |
207 | } | |
208 | ||
209 | /* | |
210 | * Prepare a CAPTURE or SYNC urb for submission to the bus. | |
211 | */ | |
212 | static inline void prepare_inbound_urb(struct snd_usb_endpoint *ep, | |
213 | struct snd_urb_ctx *urb_ctx) | |
214 | { | |
215 | int i, offs; | |
216 | struct urb *urb = urb_ctx->urb; | |
217 | ||
218 | urb->dev = ep->chip->dev; /* we need to set this at each time */ | |
219 | ||
220 | switch (ep->type) { | |
221 | case SND_USB_ENDPOINT_TYPE_DATA: | |
222 | offs = 0; | |
223 | for (i = 0; i < urb_ctx->packets; i++) { | |
224 | urb->iso_frame_desc[i].offset = offs; | |
225 | urb->iso_frame_desc[i].length = ep->curpacksize; | |
226 | offs += ep->curpacksize; | |
227 | } | |
228 | ||
229 | urb->transfer_buffer_length = offs; | |
230 | urb->number_of_packets = urb_ctx->packets; | |
231 | break; | |
232 | ||
233 | case SND_USB_ENDPOINT_TYPE_SYNC: | |
234 | urb->iso_frame_desc[0].length = min(4u, ep->syncmaxsize); | |
235 | urb->iso_frame_desc[0].offset = 0; | |
236 | break; | |
237 | } | |
238 | } | |
239 | ||
240 | static void queue_pending_output_urbs(struct snd_usb_endpoint *ep) | |
241 | { | |
242 | while (test_bit(EP_FLAG_RUNNING, &ep->flags)) { | |
243 | ||
244 | unsigned long flags; | |
245 | struct snd_usb_packet_info *packet; | |
246 | struct snd_urb_ctx *ctx = NULL; | |
247 | struct urb *urb; | |
248 | int err, i; | |
249 | ||
250 | spin_lock_irqsave(&ep->lock, flags); | |
251 | if (ep->next_packet_read_pos != ep->next_packet_write_pos) { | |
252 | packet = ep->next_packet + ep->next_packet_read_pos; | |
253 | ep->next_packet_read_pos++; | |
254 | ep->next_packet_read_pos %= MAX_URBS; | |
255 | ||
256 | /* take URB out of FIFO */ | |
257 | if (!list_empty(&ep->ready_playback_urbs)) | |
258 | ctx = list_first_entry(&ep->ready_playback_urbs, | |
259 | struct snd_urb_ctx, ready_list); | |
260 | } | |
261 | spin_unlock_irqrestore(&ep->lock, flags); | |
262 | ||
263 | if (ctx == NULL) | |
264 | return; | |
265 | ||
266 | list_del_init(&ctx->ready_list); | |
267 | urb = ctx->urb; | |
268 | ||
269 | /* copy over the length information */ | |
270 | for (i = 0; i < packet->packets; i++) | |
271 | ctx->packet_size[i] = packet->packet_size[i]; | |
272 | ||
273 | prepare_outbound_urb(ep, ctx); | |
274 | ||
275 | err = usb_submit_urb(ctx->urb, GFP_ATOMIC); | |
276 | if (err < 0) | |
277 | snd_printk(KERN_ERR "Unable to submit urb #%d: %d (urb %p)\n", | |
278 | ctx->index, err, ctx->urb); | |
279 | else | |
280 | set_bit(ctx->index, &ep->active_mask); | |
281 | } | |
282 | } | |
283 | ||
284 | /* | |
285 | * complete callback for urbs | |
286 | */ | |
287 | static void snd_complete_urb(struct urb *urb) | |
288 | { | |
289 | struct snd_urb_ctx *ctx = urb->context; | |
290 | struct snd_usb_endpoint *ep = ctx->ep; | |
291 | int err; | |
292 | ||
293 | if (unlikely(urb->status == -ENOENT || /* unlinked */ | |
294 | urb->status == -ENODEV || /* device removed */ | |
295 | urb->status == -ECONNRESET || /* unlinked */ | |
296 | urb->status == -ESHUTDOWN || /* device disabled */ | |
297 | ep->chip->shutdown)) /* device disconnected */ | |
298 | goto exit_clear; | |
299 | ||
300 | if (usb_pipeout(ep->pipe)) { | |
301 | retire_outbound_urb(ep, ctx); | |
302 | /* can be stopped during retire callback */ | |
303 | if (unlikely(!test_bit(EP_FLAG_RUNNING, &ep->flags))) | |
304 | goto exit_clear; | |
305 | ||
306 | if (snd_usb_endpoint_implict_feedback_sink(ep)) { | |
307 | unsigned long flags; | |
308 | ||
309 | spin_lock_irqsave(&ep->lock, flags); | |
310 | list_add_tail(&ctx->ready_list, &ep->ready_playback_urbs); | |
311 | spin_unlock_irqrestore(&ep->lock, flags); | |
312 | queue_pending_output_urbs(ep); | |
313 | ||
314 | goto exit_clear; | |
315 | } | |
316 | ||
317 | prepare_outbound_urb_sizes(ep, ctx); | |
318 | prepare_outbound_urb(ep, ctx); | |
319 | } else { | |
320 | retire_inbound_urb(ep, ctx); | |
321 | /* can be stopped during retire callback */ | |
322 | if (unlikely(!test_bit(EP_FLAG_RUNNING, &ep->flags))) | |
323 | goto exit_clear; | |
324 | ||
325 | prepare_inbound_urb(ep, ctx); | |
326 | } | |
327 | ||
328 | err = usb_submit_urb(urb, GFP_ATOMIC); | |
329 | if (err == 0) | |
330 | return; | |
331 | ||
332 | snd_printk(KERN_ERR "cannot submit urb (err = %d)\n", err); | |
333 | //snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN); | |
334 | ||
335 | exit_clear: | |
336 | clear_bit(ctx->index, &ep->active_mask); | |
337 | } | |
338 | ||
339 | struct snd_usb_endpoint *snd_usb_add_endpoint(struct snd_usb_audio *chip, | |
340 | struct usb_host_interface *alts, | |
341 | int ep_num, int direction, int type) | |
342 | { | |
343 | struct list_head *p; | |
344 | struct snd_usb_endpoint *ep; | |
345 | int ret, is_playback = direction == SNDRV_PCM_STREAM_PLAYBACK; | |
346 | ||
347 | mutex_lock(&chip->mutex); | |
348 | ||
349 | list_for_each(p, &chip->ep_list) { | |
350 | ep = list_entry(p, struct snd_usb_endpoint, list); | |
351 | if (ep->ep_num == ep_num && | |
352 | ep->iface == alts->desc.bInterfaceNumber && | |
353 | ep->alt_idx == alts->desc.bAlternateSetting) { | |
354 | snd_printdd(KERN_DEBUG "Re-using EP %x in iface %d,%d @%p\n", | |
355 | ep_num, ep->iface, ep->alt_idx, ep); | |
356 | goto __exit_unlock; | |
357 | } | |
358 | } | |
359 | ||
360 | snd_printdd(KERN_DEBUG "Creating new %s %s endpoint #%x\n", | |
361 | is_playback ? "playback" : "capture", | |
362 | type == SND_USB_ENDPOINT_TYPE_DATA ? "data" : "sync", | |
363 | ep_num); | |
364 | ||
365 | /* select the alt setting once so the endpoints become valid */ | |
366 | ret = usb_set_interface(chip->dev, alts->desc.bInterfaceNumber, | |
367 | alts->desc.bAlternateSetting); | |
368 | if (ret < 0) { | |
369 | snd_printk(KERN_ERR "%s(): usb_set_interface() failed, ret = %d\n", | |
370 | __func__, ret); | |
371 | ep = NULL; | |
372 | goto __exit_unlock; | |
373 | } | |
374 | ||
375 | ep = kzalloc(sizeof(*ep), GFP_KERNEL); | |
376 | if (!ep) | |
377 | goto __exit_unlock; | |
378 | ||
379 | ep->chip = chip; | |
380 | spin_lock_init(&ep->lock); | |
381 | ep->type = type; | |
382 | ep->ep_num = ep_num; | |
383 | ep->iface = alts->desc.bInterfaceNumber; | |
384 | ep->alt_idx = alts->desc.bAlternateSetting; | |
385 | INIT_LIST_HEAD(&ep->ready_playback_urbs); | |
386 | ep_num &= USB_ENDPOINT_NUMBER_MASK; | |
387 | ||
388 | if (is_playback) | |
389 | ep->pipe = usb_sndisocpipe(chip->dev, ep_num); | |
390 | else | |
391 | ep->pipe = usb_rcvisocpipe(chip->dev, ep_num); | |
392 | ||
393 | if (type == SND_USB_ENDPOINT_TYPE_SYNC) { | |
394 | if (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE && | |
395 | get_endpoint(alts, 1)->bRefresh >= 1 && | |
396 | get_endpoint(alts, 1)->bRefresh <= 9) | |
397 | ep->syncinterval = get_endpoint(alts, 1)->bRefresh; | |
398 | else if (snd_usb_get_speed(chip->dev) == USB_SPEED_FULL) | |
399 | ep->syncinterval = 1; | |
400 | else if (get_endpoint(alts, 1)->bInterval >= 1 && | |
401 | get_endpoint(alts, 1)->bInterval <= 16) | |
402 | ep->syncinterval = get_endpoint(alts, 1)->bInterval - 1; | |
403 | else | |
404 | ep->syncinterval = 3; | |
405 | ||
406 | ep->syncmaxsize = le16_to_cpu(get_endpoint(alts, 1)->wMaxPacketSize); | |
407 | } | |
408 | ||
409 | list_add_tail(&ep->list, &chip->ep_list); | |
410 | ||
411 | __exit_unlock: | |
412 | mutex_unlock(&chip->mutex); | |
413 | ||
414 | return ep; | |
415 | } | |
416 | ||
417 | /* | |
418 | * wait until all urbs are processed. | |
419 | */ | |
420 | static int wait_clear_urbs(struct snd_usb_endpoint *ep) | |
421 | { | |
422 | unsigned long end_time = jiffies + msecs_to_jiffies(1000); | |
423 | unsigned int i; | |
424 | int alive; | |
425 | ||
426 | do { | |
427 | alive = 0; | |
428 | for (i = 0; i < ep->nurbs; i++) | |
429 | if (test_bit(i, &ep->active_mask)) | |
430 | alive++; | |
431 | ||
432 | if (!alive) | |
433 | break; | |
434 | ||
435 | schedule_timeout_uninterruptible(1); | |
436 | } while (time_before(jiffies, end_time)); | |
437 | ||
438 | if (alive) | |
439 | snd_printk(KERN_ERR "timeout: still %d active urbs on EP #%x\n", | |
440 | alive, ep->ep_num); | |
441 | ||
442 | return 0; | |
443 | } | |
444 | ||
445 | /* | |
446 | * unlink active urbs. | |
447 | */ | |
448 | static int deactivate_urbs(struct snd_usb_endpoint *ep, int force, int can_sleep) | |
449 | { | |
450 | unsigned long flags; | |
451 | unsigned int i; | |
452 | int async; | |
453 | ||
454 | if (!force && ep->chip->shutdown) /* to be sure... */ | |
455 | return -EBADFD; | |
456 | ||
457 | async = !can_sleep && ep->chip->async_unlink; | |
458 | ||
459 | clear_bit(EP_FLAG_RUNNING, &ep->flags); | |
460 | ||
461 | INIT_LIST_HEAD(&ep->ready_playback_urbs); | |
462 | ep->next_packet_read_pos = 0; | |
463 | ep->next_packet_write_pos = 0; | |
464 | ||
465 | if (!async && in_interrupt()) | |
466 | return 0; | |
467 | ||
468 | for (i = 0; i < ep->nurbs; i++) { | |
469 | if (test_bit(i, &ep->active_mask)) { | |
470 | if (!test_and_set_bit(i, &ep->unlink_mask)) { | |
471 | struct urb *u = ep->urb[i].urb; | |
472 | if (async) | |
473 | usb_unlink_urb(u); | |
474 | else | |
475 | usb_kill_urb(u); | |
476 | } | |
477 | } | |
478 | } | |
479 | ||
480 | return 0; | |
481 | } | |
482 | ||
483 | /* | |
484 | * release an endpoint's urbs | |
485 | */ | |
486 | static void release_urbs(struct snd_usb_endpoint *ep, int force) | |
487 | { | |
488 | int i; | |
489 | ||
490 | /* route incoming urbs to nirvana */ | |
491 | ep->retire_data_urb = NULL; | |
492 | ep->prepare_data_urb = NULL; | |
493 | ||
494 | /* stop urbs */ | |
495 | deactivate_urbs(ep, force, 1); | |
496 | wait_clear_urbs(ep); | |
497 | ||
498 | for (i = 0; i < ep->nurbs; i++) | |
499 | release_urb_ctx(&ep->urb[i]); | |
500 | ||
501 | if (ep->syncbuf) | |
502 | usb_free_coherent(ep->chip->dev, SYNC_URBS * 4, | |
503 | ep->syncbuf, ep->sync_dma); | |
504 | ||
505 | ep->syncbuf = NULL; | |
506 | ep->nurbs = 0; | |
507 | } | |
508 | ||
509 | static int data_ep_set_params(struct snd_usb_endpoint *ep, | |
510 | struct snd_pcm_hw_params *hw_params, | |
511 | struct audioformat *fmt, | |
512 | struct snd_usb_endpoint *sync_ep) | |
513 | { | |
514 | unsigned int maxsize, i, urb_packs, total_packs, packs_per_ms; | |
515 | int period_bytes = params_period_bytes(hw_params); | |
516 | int format = params_format(hw_params); | |
517 | int is_playback = usb_pipeout(ep->pipe); | |
518 | int frame_bits = snd_pcm_format_physical_width(params_format(hw_params)) * | |
519 | params_channels(hw_params); | |
520 | ||
521 | ep->datainterval = fmt->datainterval; | |
522 | ep->stride = frame_bits >> 3; | |
523 | ep->silence_value = format == SNDRV_PCM_FORMAT_U8 ? 0x80 : 0; | |
524 | ||
525 | /* calculate max. frequency */ | |
526 | if (ep->maxpacksize) { | |
527 | /* whatever fits into a max. size packet */ | |
528 | maxsize = ep->maxpacksize; | |
529 | ep->freqmax = (maxsize / (frame_bits >> 3)) | |
530 | << (16 - ep->datainterval); | |
531 | } else { | |
532 | /* no max. packet size: just take 25% higher than nominal */ | |
533 | ep->freqmax = ep->freqn + (ep->freqn >> 2); | |
534 | maxsize = ((ep->freqmax + 0xffff) * (frame_bits >> 3)) | |
535 | >> (16 - ep->datainterval); | |
536 | } | |
537 | ||
538 | if (ep->fill_max) | |
539 | ep->curpacksize = ep->maxpacksize; | |
540 | else | |
541 | ep->curpacksize = maxsize; | |
542 | ||
543 | if (snd_usb_get_speed(ep->chip->dev) != USB_SPEED_FULL) | |
544 | packs_per_ms = 8 >> ep->datainterval; | |
545 | else | |
546 | packs_per_ms = 1; | |
547 | ||
548 | if (is_playback && !snd_usb_endpoint_implict_feedback_sink(ep)) { | |
549 | urb_packs = max(ep->chip->nrpacks, 1); | |
550 | urb_packs = min(urb_packs, (unsigned int) MAX_PACKS); | |
551 | } else { | |
552 | urb_packs = 1; | |
553 | } | |
554 | ||
555 | urb_packs *= packs_per_ms; | |
556 | ||
557 | if (sync_ep && !snd_usb_endpoint_implict_feedback_sink(ep)) | |
558 | urb_packs = min(urb_packs, 1U << sync_ep->syncinterval); | |
559 | ||
560 | /* decide how many packets to be used */ | |
561 | if (is_playback && !snd_usb_endpoint_implict_feedback_sink(ep)) { | |
562 | unsigned int minsize, maxpacks; | |
563 | /* determine how small a packet can be */ | |
564 | minsize = (ep->freqn >> (16 - ep->datainterval)) | |
565 | * (frame_bits >> 3); | |
566 | /* with sync from device, assume it can be 12% lower */ | |
567 | if (sync_ep) | |
568 | minsize -= minsize >> 3; | |
569 | minsize = max(minsize, 1u); | |
570 | total_packs = (period_bytes + minsize - 1) / minsize; | |
571 | /* we need at least two URBs for queueing */ | |
572 | if (total_packs < 2) { | |
573 | total_packs = 2; | |
574 | } else { | |
575 | /* and we don't want too long a queue either */ | |
576 | maxpacks = max(MAX_QUEUE * packs_per_ms, urb_packs * 2); | |
577 | total_packs = min(total_packs, maxpacks); | |
578 | } | |
579 | } else { | |
580 | while (urb_packs > 1 && urb_packs * maxsize >= period_bytes) | |
581 | urb_packs >>= 1; | |
582 | total_packs = MAX_URBS * urb_packs; | |
583 | } | |
584 | ||
585 | ep->nurbs = (total_packs + urb_packs - 1) / urb_packs; | |
586 | if (ep->nurbs > MAX_URBS) { | |
587 | /* too much... */ | |
588 | ep->nurbs = MAX_URBS; | |
589 | total_packs = MAX_URBS * urb_packs; | |
590 | } else if (ep->nurbs < 2) { | |
591 | /* too little - we need at least two packets | |
592 | * to ensure contiguous playback/capture | |
593 | */ | |
594 | ep->nurbs = 2; | |
595 | } | |
596 | ||
597 | /* allocate and initialize data urbs */ | |
598 | for (i = 0; i < ep->nurbs; i++) { | |
599 | struct snd_urb_ctx *u = &ep->urb[i]; | |
600 | u->index = i; | |
601 | u->ep = ep; | |
602 | u->packets = (i + 1) * total_packs / ep->nurbs | |
603 | - i * total_packs / ep->nurbs; | |
604 | u->buffer_size = maxsize * u->packets; | |
605 | ||
606 | if (fmt->fmt_type == UAC_FORMAT_TYPE_II) | |
607 | u->packets++; /* for transfer delimiter */ | |
608 | u->urb = usb_alloc_urb(u->packets, GFP_KERNEL); | |
609 | if (!u->urb) | |
610 | goto out_of_memory; | |
611 | ||
612 | u->urb->transfer_buffer = | |
613 | usb_alloc_coherent(ep->chip->dev, u->buffer_size, | |
614 | GFP_KERNEL, &u->urb->transfer_dma); | |
615 | if (!u->urb->transfer_buffer) | |
616 | goto out_of_memory; | |
617 | u->urb->pipe = ep->pipe; | |
618 | u->urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP; | |
619 | u->urb->interval = 1 << ep->datainterval; | |
620 | u->urb->context = u; | |
621 | u->urb->complete = snd_complete_urb; | |
622 | INIT_LIST_HEAD(&u->ready_list); | |
623 | } | |
624 | ||
625 | return 0; | |
626 | ||
627 | out_of_memory: | |
628 | release_urbs(ep, 0); | |
629 | return -ENOMEM; | |
630 | } | |
631 | ||
632 | static int sync_ep_set_params(struct snd_usb_endpoint *ep, | |
633 | struct snd_pcm_hw_params *hw_params, | |
634 | struct audioformat *fmt) | |
635 | { | |
636 | int i; | |
637 | ||
638 | ep->syncbuf = usb_alloc_coherent(ep->chip->dev, SYNC_URBS * 4, | |
639 | GFP_KERNEL, &ep->sync_dma); | |
640 | if (!ep->syncbuf) | |
641 | return -ENOMEM; | |
642 | ||
643 | for (i = 0; i < SYNC_URBS; i++) { | |
644 | struct snd_urb_ctx *u = &ep->urb[i]; | |
645 | u->index = i; | |
646 | u->ep = ep; | |
647 | u->packets = 1; | |
648 | u->urb = usb_alloc_urb(1, GFP_KERNEL); | |
649 | if (!u->urb) | |
650 | goto out_of_memory; | |
651 | u->urb->transfer_buffer = ep->syncbuf + i * 4; | |
652 | u->urb->transfer_dma = ep->sync_dma + i * 4; | |
653 | u->urb->transfer_buffer_length = 4; | |
654 | u->urb->pipe = ep->pipe; | |
655 | u->urb->transfer_flags = URB_ISO_ASAP | | |
656 | URB_NO_TRANSFER_DMA_MAP; | |
657 | u->urb->number_of_packets = 1; | |
658 | u->urb->interval = 1 << ep->syncinterval; | |
659 | u->urb->context = u; | |
660 | u->urb->complete = snd_complete_urb; | |
661 | } | |
662 | ||
663 | ep->nurbs = SYNC_URBS; | |
664 | ||
665 | return 0; | |
666 | ||
667 | out_of_memory: | |
668 | release_urbs(ep, 0); | |
669 | return -ENOMEM; | |
670 | } | |
671 | ||
672 | int snd_usb_endpoint_set_params(struct snd_usb_endpoint *ep, | |
673 | struct snd_pcm_hw_params *hw_params, | |
674 | struct audioformat *fmt, | |
675 | struct snd_usb_endpoint *sync_ep) | |
676 | { | |
677 | int err; | |
678 | ||
679 | if (ep->use_count != 0) { | |
680 | snd_printk(KERN_WARNING "Unable to change format on ep #%x: already in use\n", | |
681 | ep->ep_num); | |
682 | return -EBUSY; | |
683 | } | |
684 | ||
685 | /* release old buffers, if any */ | |
686 | release_urbs(ep, 0); | |
687 | ||
688 | ep->datainterval = fmt->datainterval; | |
689 | ep->maxpacksize = fmt->maxpacksize; | |
690 | ep->fill_max = fmt->attributes & UAC_EP_CS_ATTR_FILL_MAX; | |
691 | ||
692 | if (snd_usb_get_speed(ep->chip->dev) == USB_SPEED_FULL) | |
693 | ep->freqn = get_usb_full_speed_rate(params_rate(hw_params)); | |
694 | else | |
695 | ep->freqn = get_usb_high_speed_rate(params_rate(hw_params)); | |
696 | ||
697 | /* calculate the frequency in 16.16 format */ | |
698 | ep->freqm = ep->freqn; | |
699 | ep->freqshift = INT_MIN; | |
700 | ||
701 | ep->phase = 0; | |
702 | ||
703 | switch (ep->type) { | |
704 | case SND_USB_ENDPOINT_TYPE_DATA: | |
705 | err = data_ep_set_params(ep, hw_params, fmt, sync_ep); | |
706 | break; | |
707 | case SND_USB_ENDPOINT_TYPE_SYNC: | |
708 | err = sync_ep_set_params(ep, hw_params, fmt); | |
709 | break; | |
710 | default: | |
711 | err = -EINVAL; | |
712 | } | |
713 | ||
714 | snd_printdd(KERN_DEBUG "Setting params for ep #%x (type %d, %d urbs), ret=%d\n", | |
715 | ep->ep_num, ep->type, ep->nurbs, err); | |
716 | ||
717 | return err; | |
718 | } | |
719 | ||
720 | int snd_usb_endpoint_start(struct snd_usb_endpoint *ep) | |
721 | { | |
722 | int err; | |
723 | unsigned int i; | |
724 | ||
725 | if (ep->chip->shutdown) | |
726 | return -EBADFD; | |
727 | ||
728 | /* already running? */ | |
729 | if (++ep->use_count != 1) | |
730 | return 0; | |
731 | ||
732 | if (snd_BUG_ON(!test_bit(EP_FLAG_ACTIVATED, &ep->flags))) | |
733 | return -EINVAL; | |
734 | ||
735 | /* just to be sure */ | |
736 | deactivate_urbs(ep, 0, 1); | |
737 | wait_clear_urbs(ep); | |
738 | ||
739 | ep->active_mask = 0; | |
740 | ep->unlink_mask = 0; | |
741 | ep->phase = 0; | |
742 | ||
743 | /* | |
744 | * If this endpoint has a data endpoint as implicit feedback source, | |
745 | * don't start the urbs here. Instead, mark them all as available, | |
746 | * wait for the record urbs to arrive and queue from that context. | |
747 | */ | |
748 | ||
749 | set_bit(EP_FLAG_RUNNING, &ep->flags); | |
750 | ||
751 | if (snd_usb_endpoint_implict_feedback_sink(ep)) { | |
752 | for (i = 0; i < ep->nurbs; i++) { | |
753 | struct snd_urb_ctx *ctx = ep->urb + i; | |
754 | list_add_tail(&ctx->ready_list, &ep->ready_playback_urbs); | |
755 | } | |
756 | ||
757 | return 0; | |
758 | } | |
759 | ||
760 | for (i = 0; i < ep->nurbs; i++) { | |
761 | struct urb *urb = ep->urb[i].urb; | |
762 | ||
763 | if (snd_BUG_ON(!urb)) | |
764 | goto __error; | |
765 | ||
766 | if (usb_pipeout(ep->pipe)) { | |
767 | prepare_outbound_urb_sizes(ep, urb->context); | |
768 | prepare_outbound_urb(ep, urb->context); | |
769 | } else { | |
770 | prepare_inbound_urb(ep, urb->context); | |
771 | } | |
772 | ||
773 | err = usb_submit_urb(urb, GFP_ATOMIC); | |
774 | if (err < 0) { | |
775 | snd_printk(KERN_ERR "cannot submit urb %d, error %d: %s\n", | |
776 | i, err, usb_error_string(err)); | |
777 | goto __error; | |
778 | } | |
779 | set_bit(i, &ep->active_mask); | |
780 | } | |
781 | ||
782 | return 0; | |
783 | ||
784 | __error: | |
785 | clear_bit(EP_FLAG_RUNNING, &ep->flags); | |
786 | ep->use_count--; | |
787 | deactivate_urbs(ep, 0, 0); | |
788 | return -EPIPE; | |
789 | } | |
790 | ||
791 | void snd_usb_endpoint_stop(struct snd_usb_endpoint *ep, | |
792 | int force, int can_sleep, int wait) | |
793 | { | |
794 | if (!ep) | |
795 | return; | |
796 | ||
797 | if (snd_BUG_ON(ep->use_count == 0)) | |
798 | return; | |
799 | ||
800 | if (snd_BUG_ON(!test_bit(EP_FLAG_ACTIVATED, &ep->flags))) | |
801 | return; | |
802 | ||
803 | if (--ep->use_count == 0) { | |
804 | deactivate_urbs(ep, force, can_sleep); | |
805 | ep->data_subs = NULL; | |
806 | ep->sync_slave = NULL; | |
807 | ep->retire_data_urb = NULL; | |
808 | ep->prepare_data_urb = NULL; | |
809 | ||
810 | if (wait) | |
811 | wait_clear_urbs(ep); | |
812 | } | |
813 | } | |
814 | ||
815 | int snd_usb_endpoint_activate(struct snd_usb_endpoint *ep) | |
816 | { | |
817 | if (ep->use_count != 0) | |
818 | return 0; | |
819 | ||
820 | if (!ep->chip->shutdown && | |
821 | !test_and_set_bit(EP_FLAG_ACTIVATED, &ep->flags)) { | |
822 | int ret; | |
823 | ||
824 | ret = usb_set_interface(ep->chip->dev, ep->iface, ep->alt_idx); | |
825 | if (ret < 0) { | |
826 | snd_printk(KERN_ERR "%s() usb_set_interface() failed, ret = %d\n", | |
827 | __func__, ret); | |
828 | clear_bit(EP_FLAG_ACTIVATED, &ep->flags); | |
829 | return ret; | |
830 | } | |
831 | ||
832 | return 0; | |
833 | } | |
834 | ||
835 | return -EBUSY; | |
836 | } | |
837 | ||
838 | int snd_usb_endpoint_deactivate(struct snd_usb_endpoint *ep) | |
839 | { | |
840 | if (!ep) | |
841 | return -EINVAL; | |
842 | ||
843 | if (ep->use_count != 0) | |
844 | return 0; | |
845 | ||
846 | if (!ep->chip->shutdown && | |
847 | test_and_clear_bit(EP_FLAG_ACTIVATED, &ep->flags)) { | |
848 | int ret; | |
849 | ||
850 | ret = usb_set_interface(ep->chip->dev, ep->iface, 0); | |
851 | if (ret < 0) { | |
852 | snd_printk(KERN_ERR "%s(): usb_set_interface() failed, ret = %d\n", | |
853 | __func__, ret); | |
854 | return ret; | |
855 | } | |
856 | ||
857 | return 0; | |
858 | } | |
859 | ||
860 | return -EBUSY; | |
861 | } | |
862 | ||
863 | void snd_usb_endpoint_free(struct list_head *head) | |
864 | { | |
865 | struct snd_usb_endpoint *ep; | |
866 | ||
867 | ep = list_entry(head, struct snd_usb_endpoint, list); | |
868 | release_urbs(ep, 1); | |
869 | kfree(ep); | |
870 | } | |
871 | ||
872 | /* | |
873 | * process after playback sync complete | |
874 | * | |
875 | * Full speed devices report feedback values in 10.14 format as samples per | |
876 | * frame, high speed devices in 16.16 format as samples per microframe. | |
877 | * Because the Audio Class 1 spec was written before USB 2.0, many high speed | |
878 | * devices use a wrong interpretation, some others use an entirely different | |
879 | * format. Therefore, we cannot predict what format any particular device uses | |
880 | * and must detect it automatically. | |
881 | */ | |
882 | void snd_usb_handle_sync_urb(struct snd_usb_endpoint *ep, | |
883 | struct snd_usb_endpoint *sender, | |
884 | const struct urb *urb) | |
885 | { | |
886 | int shift; | |
887 | unsigned int f; | |
888 | unsigned long flags; | |
889 | ||
890 | snd_BUG_ON(ep == sender); | |
891 | ||
892 | if (snd_usb_endpoint_implict_feedback_sink(ep) && | |
893 | ep->use_count != 0) { | |
894 | ||
895 | /* implicit feedback case */ | |
896 | int i, bytes = 0; | |
897 | struct snd_urb_ctx *in_ctx; | |
898 | struct snd_usb_packet_info *out_packet; | |
899 | ||
900 | in_ctx = urb->context; | |
901 | ||
902 | /* Count overall packet size */ | |
903 | for (i = 0; i < in_ctx->packets; i++) | |
904 | if (urb->iso_frame_desc[i].status == 0) | |
905 | bytes += urb->iso_frame_desc[i].actual_length; | |
906 | ||
907 | /* | |
908 | * skip empty packets. At least M-Audio's Fast Track Ultra stops | |
909 | * streaming once it received a 0-byte OUT URB | |
910 | */ | |
911 | if (bytes == 0) | |
912 | return; | |
913 | ||
914 | spin_lock_irqsave(&ep->lock, flags); | |
915 | out_packet = ep->next_packet + ep->next_packet_write_pos; | |
916 | ||
917 | /* | |
918 | * Iterate through the inbound packet and prepare the lengths | |
919 | * for the output packet. The OUT packet we are about to send | |
920 | * will have the same amount of payload than the IN packet we | |
921 | * just received. | |
922 | */ | |
923 | ||
924 | out_packet->packets = in_ctx->packets; | |
925 | for (i = 0; i < in_ctx->packets; i++) { | |
926 | if (urb->iso_frame_desc[i].status == 0) | |
927 | out_packet->packet_size[i] = | |
928 | urb->iso_frame_desc[i].actual_length / ep->stride; | |
929 | else | |
930 | out_packet->packet_size[i] = 0; | |
931 | } | |
932 | ||
933 | ep->next_packet_write_pos++; | |
934 | ep->next_packet_write_pos %= MAX_URBS; | |
935 | spin_unlock_irqrestore(&ep->lock, flags); | |
936 | queue_pending_output_urbs(ep); | |
937 | ||
938 | return; | |
939 | } | |
940 | ||
941 | /* parse sync endpoint packet */ | |
942 | ||
943 | if (urb->iso_frame_desc[0].status != 0 || | |
944 | urb->iso_frame_desc[0].actual_length < 3) | |
945 | return; | |
946 | ||
947 | f = le32_to_cpup(urb->transfer_buffer); | |
948 | if (urb->iso_frame_desc[0].actual_length == 3) | |
949 | f &= 0x00ffffff; | |
950 | else | |
951 | f &= 0x0fffffff; | |
952 | ||
953 | if (f == 0) | |
954 | return; | |
955 | ||
956 | if (unlikely(ep->freqshift == INT_MIN)) { | |
957 | /* | |
958 | * The first time we see a feedback value, determine its format | |
959 | * by shifting it left or right until it matches the nominal | |
960 | * frequency value. This assumes that the feedback does not | |
961 | * differ from the nominal value more than +50% or -25%. | |
962 | */ | |
963 | shift = 0; | |
964 | while (f < ep->freqn - ep->freqn / 4) { | |
965 | f <<= 1; | |
966 | shift++; | |
967 | } | |
968 | while (f > ep->freqn + ep->freqn / 2) { | |
969 | f >>= 1; | |
970 | shift--; | |
971 | } | |
972 | ep->freqshift = shift; | |
973 | } else if (ep->freqshift >= 0) | |
974 | f <<= ep->freqshift; | |
975 | else | |
976 | f >>= -ep->freqshift; | |
977 | ||
978 | if (likely(f >= ep->freqn - ep->freqn / 8 && f <= ep->freqmax)) { | |
979 | /* | |
980 | * If the frequency looks valid, set it. | |
981 | * This value is referred to in prepare_playback_urb(). | |
982 | */ | |
983 | spin_lock_irqsave(&ep->lock, flags); | |
984 | ep->freqm = f; | |
985 | spin_unlock_irqrestore(&ep->lock, flags); | |
986 | } else { | |
987 | /* | |
988 | * Out of range; maybe the shift value is wrong. | |
989 | * Reset it so that we autodetect again the next time. | |
990 | */ | |
991 | ep->freqshift = INT_MIN; | |
992 | } | |
993 | } | |
994 |