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