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