Blame view

sound/usb/pcm.c 47.4 KB
e5779998b   Daniel Mack   ALSA: usb-audio: ...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  /*
   *   This program is free software; you can redistribute it and/or modify
   *   it under the terms of the GNU General Public License as published by
   *   the Free Software Foundation; either version 2 of the License, or
   *   (at your option) any later version.
   *
   *   This program is distributed in the hope that it will be useful,
   *   but WITHOUT ANY WARRANTY; without even the implied warranty of
   *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   *   GNU General Public License for more details.
   *
   *   You should have received a copy of the GNU General Public License
   *   along with this program; if not, write to the Free Software
   *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
   */
  
  #include <linux/init.h>
9966ddafe   Stephen Rothwell   ALSA: usb pcm: us...
18
  #include <linux/slab.h>
44dcbbb1c   Daniel Mack   ALSA: snd-usb: ad...
19
  #include <linux/bitrev.h>
edcd3633e   Daniel Mack   ALSA: snd-usb: sw...
20
  #include <linux/ratelimit.h>
e5779998b   Daniel Mack   ALSA: usb-audio: ...
21
22
  #include <linux/usb.h>
  #include <linux/usb/audio.h>
7e8478940   Daniel Mack   linux/usb/audio.h...
23
  #include <linux/usb/audio-v2.h>
e5779998b   Daniel Mack   ALSA: usb-audio: ...
24
25
26
27
28
29
30
31
32
  
  #include <sound/core.h>
  #include <sound/pcm.h>
  #include <sound/pcm_params.h>
  
  #include "usbaudio.h"
  #include "card.h"
  #include "quirks.h"
  #include "debug.h"
c731bc96a   Daniel Mack   ALSA: snd-usb: mo...
33
  #include "endpoint.h"
e5779998b   Daniel Mack   ALSA: usb-audio: ...
34
35
  #include "helper.h"
  #include "pcm.h"
79f920fbf   Daniel Mack   ALSA: usb-audio: ...
36
  #include "clock.h"
88a8516a2   Oliver Neukum   ALSA: usbaudio: i...
37
  #include "power.h"
e5779998b   Daniel Mack   ALSA: usb-audio: ...
38

edcd3633e   Daniel Mack   ALSA: snd-usb: sw...
39
40
  #define SUBSTREAM_FLAG_DATA_EP_STARTED	0
  #define SUBSTREAM_FLAG_SYNC_EP_STARTED	1
294c4fb8a   Pierre-Louis Bossart   ALSA: usb: refine...
41
42
43
44
45
46
47
  /* return the estimated delay based on USB frame counters */
  snd_pcm_uframes_t snd_usb_pcm_delay(struct snd_usb_substream *subs,
  				    unsigned int rate)
  {
  	int current_frame_number;
  	int frame_diff;
  	int est_delay;
48779a0b8   Takashi Iwai   ALSA: usb-audio: ...
48
49
  	if (!subs->last_delay)
  		return 0; /* short path */
294c4fb8a   Pierre-Louis Bossart   ALSA: usb: refine...
50
51
52
53
54
55
56
57
58
59
  	current_frame_number = usb_get_current_frame_number(subs->dev);
  	/*
  	 * HCD implementations use different widths, use lower 8 bits.
  	 * The delay will be managed up to 256ms, which is more than
  	 * enough
  	 */
  	frame_diff = (current_frame_number - subs->last_frame_number) & 0xff;
  
  	/* Approximation based on number of samples per USB frame (ms),
  	   some truncation for 44.1 but the estimate is good enough */
e4cc61534   Pierre-Louis Bossart   ALSA: usb-audio: ...
60
61
62
63
64
  	est_delay =  frame_diff * rate / 1000;
  	if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK)
  		est_delay = subs->last_delay - est_delay;
  	else
  		est_delay = subs->last_delay + est_delay;
294c4fb8a   Pierre-Louis Bossart   ALSA: usb: refine...
65
66
67
68
  	if (est_delay < 0)
  		est_delay = 0;
  	return est_delay;
  }
e5779998b   Daniel Mack   ALSA: usb-audio: ...
69
70
71
72
73
74
75
76
77
  /*
   * return the current pcm pointer.  just based on the hwptr_done value.
   */
  static snd_pcm_uframes_t snd_usb_pcm_pointer(struct snd_pcm_substream *substream)
  {
  	struct snd_usb_substream *subs;
  	unsigned int hwptr_done;
  
  	subs = (struct snd_usb_substream *)substream->runtime->private_data;
47ab15459   Takashi Iwai   ALSA: usb-audio: ...
78
  	if (atomic_read(&subs->stream->chip->shutdown))
978520b75   Takashi Iwai   ALSA: usb-audio: ...
79
  		return SNDRV_PCM_POS_XRUN;
e5779998b   Daniel Mack   ALSA: usb-audio: ...
80
81
  	spin_lock(&subs->lock);
  	hwptr_done = subs->hwptr_done;
e4cc61534   Pierre-Louis Bossart   ALSA: usb-audio: ...
82
  	substream->runtime->delay = snd_usb_pcm_delay(subs,
294c4fb8a   Pierre-Louis Bossart   ALSA: usb: refine...
83
  						substream->runtime->rate);
e5779998b   Daniel Mack   ALSA: usb-audio: ...
84
85
86
87
88
89
90
  	spin_unlock(&subs->lock);
  	return hwptr_done / (substream->runtime->frame_bits >> 3);
  }
  
  /*
   * find a matching audio format
   */
61a709504   Dylan Reid   ALSA: usb-audio: ...
91
  static struct audioformat *find_format(struct snd_usb_substream *subs)
e5779998b   Daniel Mack   ALSA: usb-audio: ...
92
  {
88766f04c   Eldad Zack   ALSA: usb-audio: ...
93
  	struct audioformat *fp;
e5779998b   Daniel Mack   ALSA: usb-audio: ...
94
95
  	struct audioformat *found = NULL;
  	int cur_attr = 0, attr;
88766f04c   Eldad Zack   ALSA: usb-audio: ...
96
  	list_for_each_entry(fp, &subs->fmt_list, list) {
74c34ca1c   Eldad Zack   ALSA: pcm_format_...
97
  		if (!(fp->formats & pcm_format_to_bits(subs->pcm_format)))
015eb0b08   Clemens Ladisch   ALSA: usb-audio: ...
98
  			continue;
61a709504   Dylan Reid   ALSA: usb-audio: ...
99
  		if (fp->channels != subs->channels)
e5779998b   Daniel Mack   ALSA: usb-audio: ...
100
  			continue;
61a709504   Dylan Reid   ALSA: usb-audio: ...
101
102
  		if (subs->cur_rate < fp->rate_min ||
  		    subs->cur_rate > fp->rate_max)
e5779998b   Daniel Mack   ALSA: usb-audio: ...
103
104
105
106
  			continue;
  		if (! (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)) {
  			unsigned int i;
  			for (i = 0; i < fp->nr_rates; i++)
61a709504   Dylan Reid   ALSA: usb-audio: ...
107
  				if (fp->rate_table[i] == subs->cur_rate)
e5779998b   Daniel Mack   ALSA: usb-audio: ...
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
  					break;
  			if (i >= fp->nr_rates)
  				continue;
  		}
  		attr = fp->ep_attr & USB_ENDPOINT_SYNCTYPE;
  		if (! found) {
  			found = fp;
  			cur_attr = attr;
  			continue;
  		}
  		/* avoid async out and adaptive in if the other method
  		 * supports the same format.
  		 * this is a workaround for the case like
  		 * M-audio audiophile USB.
  		 */
  		if (attr != cur_attr) {
  			if ((attr == USB_ENDPOINT_SYNC_ASYNC &&
  			     subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
  			    (attr == USB_ENDPOINT_SYNC_ADAPTIVE &&
  			     subs->direction == SNDRV_PCM_STREAM_CAPTURE))
  				continue;
  			if ((cur_attr == USB_ENDPOINT_SYNC_ASYNC &&
  			     subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
  			    (cur_attr == USB_ENDPOINT_SYNC_ADAPTIVE &&
  			     subs->direction == SNDRV_PCM_STREAM_CAPTURE)) {
  				found = fp;
  				cur_attr = attr;
  				continue;
  			}
  		}
  		/* find the format with the largest max. packet size */
  		if (fp->maxpacksize > found->maxpacksize) {
  			found = fp;
  			cur_attr = attr;
  		}
  	}
  	return found;
  }
767d75ad1   Daniel Mack   ALSA: usb-audio: ...
146
147
148
149
150
151
152
153
  static int init_pitch_v1(struct snd_usb_audio *chip, int iface,
  			 struct usb_host_interface *alts,
  			 struct audioformat *fmt)
  {
  	struct usb_device *dev = chip->dev;
  	unsigned int ep;
  	unsigned char data[1];
  	int err;
447d6275f   Takashi Iwai   ALSA: usb-audio: ...
154
155
  	if (get_iface_desc(alts)->bNumEndpoints < 1)
  		return -EINVAL;
767d75ad1   Daniel Mack   ALSA: usb-audio: ...
156
  	ep = get_endpoint(alts, 0)->bEndpointAddress;
767d75ad1   Daniel Mack   ALSA: usb-audio: ...
157
158
159
160
  	data[0] = 1;
  	if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC_SET_CUR,
  				   USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT,
  				   UAC_EP_CS_ATTR_PITCH_CONTROL << 8, ep,
17d900c4a   Clemens Ladisch   ALSA: usb-audio: ...
161
  				   data, sizeof(data))) < 0) {
0ba41d917   Takashi Iwai   ALSA: usb-audio: ...
162
163
164
  		usb_audio_err(chip, "%d:%d: cannot set enable PITCH
  ",
  			      iface, ep);
767d75ad1   Daniel Mack   ALSA: usb-audio: ...
165
166
167
168
169
  		return err;
  	}
  
  	return 0;
  }
e5779998b   Daniel Mack   ALSA: usb-audio: ...
170

92c256110   Daniel Mack   ALSA: usb-audio: ...
171
172
173
174
175
176
  static int init_pitch_v2(struct snd_usb_audio *chip, int iface,
  			 struct usb_host_interface *alts,
  			 struct audioformat *fmt)
  {
  	struct usb_device *dev = chip->dev;
  	unsigned char data[1];
92c256110   Daniel Mack   ALSA: usb-audio: ...
177
  	int err;
92c256110   Daniel Mack   ALSA: usb-audio: ...
178
179
180
181
  	data[0] = 1;
  	if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC2_CS_CUR,
  				   USB_TYPE_CLASS | USB_RECIP_ENDPOINT | USB_DIR_OUT,
  				   UAC2_EP_CS_PITCH << 8, 0,
17d900c4a   Clemens Ladisch   ALSA: usb-audio: ...
182
  				   data, sizeof(data))) < 0) {
0ba41d917   Takashi Iwai   ALSA: usb-audio: ...
183
184
185
  		usb_audio_err(chip, "%d:%d: cannot set enable PITCH (v2)
  ",
  			      iface, fmt->altsetting);
92c256110   Daniel Mack   ALSA: usb-audio: ...
186
187
188
189
190
  		return err;
  	}
  
  	return 0;
  }
e5779998b   Daniel Mack   ALSA: usb-audio: ...
191
  /*
92c256110   Daniel Mack   ALSA: usb-audio: ...
192
   * initialize the pitch control and sample rate
e5779998b   Daniel Mack   ALSA: usb-audio: ...
193
   */
767d75ad1   Daniel Mack   ALSA: usb-audio: ...
194
  int snd_usb_init_pitch(struct snd_usb_audio *chip, int iface,
e5779998b   Daniel Mack   ALSA: usb-audio: ...
195
196
197
  		       struct usb_host_interface *alts,
  		       struct audioformat *fmt)
  {
92c256110   Daniel Mack   ALSA: usb-audio: ...
198
199
200
  	/* if endpoint doesn't have pitch control, bail out */
  	if (!(fmt->attributes & UAC_EP_CS_ATTR_PITCH_CONTROL))
  		return 0;
8f898e92a   Clemens Ladisch   ALSA: usb-audio: ...
201
  	switch (fmt->protocol) {
767d75ad1   Daniel Mack   ALSA: usb-audio: ...
202
  	case UAC_VERSION_1:
a2acad829   Clemens Ladisch   ALSA: usb-audio: ...
203
  	default:
767d75ad1   Daniel Mack   ALSA: usb-audio: ...
204
205
206
  		return init_pitch_v1(chip, iface, alts, fmt);
  
  	case UAC_VERSION_2:
92c256110   Daniel Mack   ALSA: usb-audio: ...
207
  		return init_pitch_v2(chip, iface, alts, fmt);
767d75ad1   Daniel Mack   ALSA: usb-audio: ...
208
  	}
767d75ad1   Daniel Mack   ALSA: usb-audio: ...
209
  }
be4e3aec5   Ioan-Adrian Ratiu   ALSA: usb-audio: ...
210
  static int start_endpoints(struct snd_usb_substream *subs)
edcd3633e   Daniel Mack   ALSA: snd-usb: sw...
211
212
213
214
215
216
217
218
  {
  	int err;
  
  	if (!subs->data_endpoint)
  		return -EINVAL;
  
  	if (!test_and_set_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags)) {
  		struct snd_usb_endpoint *ep = subs->data_endpoint;
0ba41d917   Takashi Iwai   ALSA: usb-audio: ...
219
220
  		dev_dbg(&subs->dev->dev, "Starting data EP @%p
  ", ep);
edcd3633e   Daniel Mack   ALSA: snd-usb: sw...
221
222
  
  		ep->data_subs = subs;
be4e3aec5   Ioan-Adrian Ratiu   ALSA: usb-audio: ...
223
  		err = snd_usb_endpoint_start(ep);
edcd3633e   Daniel Mack   ALSA: snd-usb: sw...
224
225
226
227
228
229
230
231
232
  		if (err < 0) {
  			clear_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags);
  			return err;
  		}
  	}
  
  	if (subs->sync_endpoint &&
  	    !test_and_set_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags)) {
  		struct snd_usb_endpoint *ep = subs->sync_endpoint;
2e4a263ca   Daniel Mack   ALSA: snd-usb: fi...
233
  		if (subs->data_endpoint->iface != subs->sync_endpoint->iface ||
df23a2466   Eldad Zack   ALSA: usb-audio: ...
234
  		    subs->data_endpoint->altsetting != subs->sync_endpoint->altsetting) {
2e4a263ca   Daniel Mack   ALSA: snd-usb: fi...
235
236
  			err = usb_set_interface(subs->dev,
  						subs->sync_endpoint->iface,
df23a2466   Eldad Zack   ALSA: usb-audio: ...
237
  						subs->sync_endpoint->altsetting);
2e4a263ca   Daniel Mack   ALSA: snd-usb: fi...
238
  			if (err < 0) {
06613f547   Eldad Zack   ALSA: usb-audio: ...
239
  				clear_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags);
0ba41d917   Takashi Iwai   ALSA: usb-audio: ...
240
241
242
  				dev_err(&subs->dev->dev,
  					   "%d:%d: cannot set interface (%d)
  ",
2e4a263ca   Daniel Mack   ALSA: snd-usb: fi...
243
  					   subs->sync_endpoint->iface,
df23a2466   Eldad Zack   ALSA: usb-audio: ...
244
  					   subs->sync_endpoint->altsetting, err);
2e4a263ca   Daniel Mack   ALSA: snd-usb: fi...
245
246
247
  				return -EIO;
  			}
  		}
0ba41d917   Takashi Iwai   ALSA: usb-audio: ...
248
249
  		dev_dbg(&subs->dev->dev, "Starting sync EP @%p
  ", ep);
edcd3633e   Daniel Mack   ALSA: snd-usb: sw...
250
251
  
  		ep->sync_slave = subs->data_endpoint;
be4e3aec5   Ioan-Adrian Ratiu   ALSA: usb-audio: ...
252
  		err = snd_usb_endpoint_start(ep);
edcd3633e   Daniel Mack   ALSA: snd-usb: sw...
253
254
255
256
257
258
259
260
  		if (err < 0) {
  			clear_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags);
  			return err;
  		}
  	}
  
  	return 0;
  }
a9bb36261   Takashi Iwai   ALSA: usb-audio: ...
261
  static void stop_endpoints(struct snd_usb_substream *subs, bool wait)
edcd3633e   Daniel Mack   ALSA: snd-usb: sw...
262
263
  {
  	if (test_and_clear_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags))
b2eb950de   Takashi Iwai   ALSA: usb-audio: ...
264
  		snd_usb_endpoint_stop(subs->sync_endpoint);
edcd3633e   Daniel Mack   ALSA: snd-usb: sw...
265
266
  
  	if (test_and_clear_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags))
b2eb950de   Takashi Iwai   ALSA: usb-audio: ...
267
268
269
270
271
272
  		snd_usb_endpoint_stop(subs->data_endpoint);
  
  	if (wait) {
  		snd_usb_endpoint_sync_pending_stop(subs->sync_endpoint);
  		snd_usb_endpoint_sync_pending_stop(subs->data_endpoint);
  	}
edcd3633e   Daniel Mack   ALSA: snd-usb: sw...
273
  }
ba7c2be11   Clemens Ladisch   ALSA: usb-audio: ...
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
  static int search_roland_implicit_fb(struct usb_device *dev, int ifnum,
  				     unsigned int altsetting,
  				     struct usb_host_interface **alts,
  				     unsigned int *ep)
  {
  	struct usb_interface *iface;
  	struct usb_interface_descriptor *altsd;
  	struct usb_endpoint_descriptor *epd;
  
  	iface = usb_ifnum_to_if(dev, ifnum);
  	if (!iface || iface->num_altsetting < altsetting + 1)
  		return -ENOENT;
  	*alts = &iface->altsetting[altsetting];
  	altsd = get_iface_desc(*alts);
  	if (altsd->bAlternateSetting != altsetting ||
  	    altsd->bInterfaceClass != USB_CLASS_VENDOR_SPEC ||
  	    (altsd->bInterfaceSubClass != 2 &&
  	     altsd->bInterfaceProtocol != 2   ) ||
  	    altsd->bNumEndpoints < 1)
  		return -ENOENT;
  	epd = get_endpoint(*alts, 0);
  	if (!usb_endpoint_is_isoc_in(epd) ||
  	    (epd->bmAttributes & USB_ENDPOINT_USAGE_MASK) !=
  					USB_ENDPOINT_USAGE_IMPLICIT_FB)
  		return -ENOENT;
  	*ep = epd->bEndpointAddress;
  	return 0;
  }
a60945fd0   Eldad Zack   ALSA: usb-audio: ...
302
303
304
305
  static int set_sync_ep_implicit_fb_quirk(struct snd_usb_substream *subs,
  					 struct usb_device *dev,
  					 struct usb_interface_descriptor *altsd,
  					 unsigned int attr)
e5779998b   Daniel Mack   ALSA: usb-audio: ...
306
  {
a60945fd0   Eldad Zack   ALSA: usb-audio: ...
307
  	struct usb_host_interface *alts;
e5779998b   Daniel Mack   ALSA: usb-audio: ...
308
  	struct usb_interface *iface;
a60945fd0   Eldad Zack   ALSA: usb-audio: ...
309
  	unsigned int ep;
c75a8a7ae   Daniel Mack   ALSA: snd-usb: ad...
310

914273c71   Eldad Zack   ALSA: usb-audio: ...
311
312
313
  	/* Implicit feedback sync EPs consumers are always playback EPs */
  	if (subs->direction != SNDRV_PCM_STREAM_PLAYBACK)
  		return 0;
c75a8a7ae   Daniel Mack   ALSA: snd-usb: ad...
314
  	switch (subs->stream->chip->usb_id) {
ca10a7ebd   Eldad Zack   ALSA: usb-audio: ...
315
  	case USB_ID(0x0763, 0x2030): /* M-Audio Fast Track C400 */
e9a25e04b   Matt Gruskin   ALSA: usb-audio: ...
316
  	case USB_ID(0x0763, 0x2031): /* M-Audio Fast Track C600 */
914273c71   Eldad Zack   ALSA: usb-audio: ...
317
318
  		ep = 0x81;
  		iface = usb_ifnum_to_if(dev, 3);
ca10a7ebd   Eldad Zack   ALSA: usb-audio: ...
319

914273c71   Eldad Zack   ALSA: usb-audio: ...
320
321
  		if (!iface || iface->num_altsetting == 0)
  			return -EINVAL;
ca10a7ebd   Eldad Zack   ALSA: usb-audio: ...
322

914273c71   Eldad Zack   ALSA: usb-audio: ...
323
324
  		alts = &iface->altsetting[1];
  		goto add_sync_ep;
ca10a7ebd   Eldad Zack   ALSA: usb-audio: ...
325
  		break;
c75a8a7ae   Daniel Mack   ALSA: snd-usb: ad...
326
327
  	case USB_ID(0x0763, 0x2080): /* M-Audio FastTrack Ultra */
  	case USB_ID(0x0763, 0x2081):
914273c71   Eldad Zack   ALSA: usb-audio: ...
328
329
  		ep = 0x81;
  		iface = usb_ifnum_to_if(dev, 2);
c75a8a7ae   Daniel Mack   ALSA: snd-usb: ad...
330

914273c71   Eldad Zack   ALSA: usb-audio: ...
331
332
  		if (!iface || iface->num_altsetting == 0)
  			return -EINVAL;
c75a8a7ae   Daniel Mack   ALSA: snd-usb: ad...
333

914273c71   Eldad Zack   ALSA: usb-audio: ...
334
335
  		alts = &iface->altsetting[1];
  		goto add_sync_ep;
c75a8a7ae   Daniel Mack   ALSA: snd-usb: ad...
336
  	}
914273c71   Eldad Zack   ALSA: usb-audio: ...
337
  	if (attr == USB_ENDPOINT_SYNC_ASYNC &&
ba7c2be11   Clemens Ladisch   ALSA: usb-audio: ...
338
339
340
341
342
343
344
  	    altsd->bInterfaceClass == USB_CLASS_VENDOR_SPEC &&
  	    altsd->bInterfaceProtocol == 2 &&
  	    altsd->bNumEndpoints == 1 &&
  	    USB_ID_VENDOR(subs->stream->chip->usb_id) == 0x0582 /* Roland */ &&
  	    search_roland_implicit_fb(dev, altsd->bInterfaceNumber + 1,
  				      altsd->bAlternateSetting,
  				      &alts, &ep) >= 0) {
ba7c2be11   Clemens Ladisch   ALSA: usb-audio: ...
345
346
  		goto add_sync_ep;
  	}
edcd3633e   Daniel Mack   ALSA: snd-usb: sw...
347

a60945fd0   Eldad Zack   ALSA: usb-audio: ...
348
349
350
351
352
353
  	/* No quirk */
  	return 0;
  
  add_sync_ep:
  	subs->sync_endpoint = snd_usb_add_endpoint(subs->stream->chip,
  						   alts, ep, !subs->direction,
88abb8eff   Eldad Zack   ALSA: usb-audio: ...
354
  						   SND_USB_ENDPOINT_TYPE_DATA);
a60945fd0   Eldad Zack   ALSA: usb-audio: ...
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
  	if (!subs->sync_endpoint)
  		return -EINVAL;
  
  	subs->data_endpoint->sync_master = subs->sync_endpoint;
  
  	return 0;
  }
  
  static int set_sync_endpoint(struct snd_usb_substream *subs,
  			     struct audioformat *fmt,
  			     struct usb_device *dev,
  			     struct usb_host_interface *alts,
  			     struct usb_interface_descriptor *altsd)
  {
  	int is_playback = subs->direction == SNDRV_PCM_STREAM_PLAYBACK;
  	unsigned int ep, attr;
95fec8833   Eldad Zack   ALSA: usb-audio: ...
371
  	bool implicit_fb;
a60945fd0   Eldad Zack   ALSA: usb-audio: ...
372
373
374
375
376
377
378
379
  	int err;
  
  	/* we need a sync pipe in async OUT or adaptive IN mode */
  	/* check the number of EP, since some devices have broken
  	 * descriptors which fool us.  if it has only one EP,
  	 * assume it as adaptive-out or sync-in.
  	 */
  	attr = fmt->ep_attr & USB_ENDPOINT_SYNCTYPE;
630184477   Pierre-Louis Bossart   ALSA: usb: fix co...
380
381
382
383
384
385
386
387
388
389
390
391
392
  	if ((is_playback && (attr != USB_ENDPOINT_SYNC_ASYNC)) ||
  		(!is_playback && (attr != USB_ENDPOINT_SYNC_ADAPTIVE))) {
  
  		/*
  		 * In these modes the notion of sync_endpoint is irrelevant.
  		 * Reset pointers to avoid using stale data from previously
  		 * used settings, e.g. when configuration and endpoints were
  		 * changed
  		 */
  
  		subs->sync_endpoint = NULL;
  		subs->data_endpoint->sync_master = NULL;
  	}
a60945fd0   Eldad Zack   ALSA: usb-audio: ...
393
394
395
  	err = set_sync_ep_implicit_fb_quirk(subs, dev, altsd, attr);
  	if (err < 0)
  		return err;
f34d06501   Eldad Zack   ALSA: usb-audio: ...
396
397
  	if (altsd->bNumEndpoints < 2)
  		return 0;
c75a8a7ae   Daniel Mack   ALSA: snd-usb: ad...
398

395ae54bd   Pierre-Louis Bossart   ALSA: usb: handle...
399
400
  	if ((is_playback && (attr == USB_ENDPOINT_SYNC_SYNC ||
  			     attr == USB_ENDPOINT_SYNC_ADAPTIVE)) ||
f34d06501   Eldad Zack   ALSA: usb-audio: ...
401
402
  	    (!is_playback && attr != USB_ENDPOINT_SYNC_ADAPTIVE))
  		return 0;
edcd3633e   Daniel Mack   ALSA: snd-usb: sw...
403

395ae54bd   Pierre-Louis Bossart   ALSA: usb: handle...
404
405
406
407
408
  	/*
  	 * In case of illegal SYNC_NONE for OUT endpoint, we keep going to see
  	 * if we don't find a sync endpoint, as on M-Audio Transit. In case of
  	 * error fall back to SYNC mode and don't create sync endpoint
  	 */
f34d06501   Eldad Zack   ALSA: usb-audio: ...
409
410
411
412
413
414
  	/* check sync-pipe endpoint */
  	/* ... and check descriptor size before accessing bSynchAddress
  	   because there is a version of the SB Audigy 2 NX firmware lacking
  	   the audio fields in the endpoint descriptors */
  	if ((get_endpoint(alts, 1)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_ISOC ||
  	    (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
95fec8833   Eldad Zack   ALSA: usb-audio: ...
415
  	     get_endpoint(alts, 1)->bSynchAddress != 0)) {
0ba41d917   Takashi Iwai   ALSA: usb-audio: ...
416
417
418
419
  		dev_err(&dev->dev,
  			"%d:%d : invalid sync pipe. bmAttributes %02x, bLength %d, bSynchAddress %02x
  ",
  			   fmt->iface, fmt->altsetting,
f34d06501   Eldad Zack   ALSA: usb-audio: ...
420
421
422
  			   get_endpoint(alts, 1)->bmAttributes,
  			   get_endpoint(alts, 1)->bLength,
  			   get_endpoint(alts, 1)->bSynchAddress);
395ae54bd   Pierre-Louis Bossart   ALSA: usb: handle...
423
424
  		if (is_playback && attr == USB_ENDPOINT_SYNC_NONE)
  			return 0;
f34d06501   Eldad Zack   ALSA: usb-audio: ...
425
426
427
  		return -EINVAL;
  	}
  	ep = get_endpoint(alts, 1)->bEndpointAddress;
95fec8833   Eldad Zack   ALSA: usb-audio: ...
428
  	if (get_endpoint(alts, 0)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
f34d06501   Eldad Zack   ALSA: usb-audio: ...
429
430
  	    ((is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress | USB_DIR_IN)) ||
  	     (!is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress & ~USB_DIR_IN)))) {
0ba41d917   Takashi Iwai   ALSA: usb-audio: ...
431
432
433
434
  		dev_err(&dev->dev,
  			"%d:%d : invalid sync pipe. is_playback %d, ep %02x, bSynchAddress %02x
  ",
  			   fmt->iface, fmt->altsetting,
f34d06501   Eldad Zack   ALSA: usb-audio: ...
435
  			   is_playback, ep, get_endpoint(alts, 0)->bSynchAddress);
395ae54bd   Pierre-Louis Bossart   ALSA: usb: handle...
436
437
  		if (is_playback && attr == USB_ENDPOINT_SYNC_NONE)
  			return 0;
f34d06501   Eldad Zack   ALSA: usb-audio: ...
438
  		return -EINVAL;
edcd3633e   Daniel Mack   ALSA: snd-usb: sw...
439
  	}
e5779998b   Daniel Mack   ALSA: usb-audio: ...
440

f34d06501   Eldad Zack   ALSA: usb-audio: ...
441
442
443
444
445
446
447
448
  	implicit_fb = (get_endpoint(alts, 1)->bmAttributes & USB_ENDPOINT_USAGE_MASK)
  			== USB_ENDPOINT_USAGE_IMPLICIT_FB;
  
  	subs->sync_endpoint = snd_usb_add_endpoint(subs->stream->chip,
  						   alts, ep, !subs->direction,
  						   implicit_fb ?
  							SND_USB_ENDPOINT_TYPE_DATA :
  							SND_USB_ENDPOINT_TYPE_SYNC);
395ae54bd   Pierre-Louis Bossart   ALSA: usb: handle...
449
450
451
  	if (!subs->sync_endpoint) {
  		if (is_playback && attr == USB_ENDPOINT_SYNC_NONE)
  			return 0;
f34d06501   Eldad Zack   ALSA: usb-audio: ...
452
  		return -EINVAL;
395ae54bd   Pierre-Louis Bossart   ALSA: usb: handle...
453
  	}
f34d06501   Eldad Zack   ALSA: usb-audio: ...
454
455
  
  	subs->data_endpoint->sync_master = subs->sync_endpoint;
71bb64c56   Eldad Zack   ALSA: usb-audio: ...
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
  	return 0;
  }
  
  /*
   * find a matching format and set up the interface
   */
  static int set_format(struct snd_usb_substream *subs, struct audioformat *fmt)
  {
  	struct usb_device *dev = subs->dev;
  	struct usb_host_interface *alts;
  	struct usb_interface_descriptor *altsd;
  	struct usb_interface *iface;
  	int err;
  
  	iface = usb_ifnum_to_if(dev, fmt->iface);
  	if (WARN_ON(!iface))
  		return -EINVAL;
  	alts = &iface->altsetting[fmt->altset_idx];
  	altsd = get_iface_desc(alts);
  	if (WARN_ON(altsd->bAlternateSetting != fmt->altsetting))
  		return -EINVAL;
  
  	if (fmt == subs->cur_audiofmt)
  		return 0;
  
  	/* close the old interface */
  	if (subs->interface >= 0 && subs->interface != fmt->iface) {
  		err = usb_set_interface(subs->dev, subs->interface, 0);
  		if (err < 0) {
0ba41d917   Takashi Iwai   ALSA: usb-audio: ...
485
486
487
488
  			dev_err(&dev->dev,
  				"%d:%d: return to setting 0 failed (%d)
  ",
  				fmt->iface, fmt->altsetting, err);
71bb64c56   Eldad Zack   ALSA: usb-audio: ...
489
490
491
492
493
494
495
496
497
  			return -EIO;
  		}
  		subs->interface = -1;
  		subs->altset_idx = 0;
  	}
  
  	/* set interface */
  	if (subs->interface != fmt->iface ||
  	    subs->altset_idx != fmt->altset_idx) {
6874daad4   Jurgen Kramer   ALSA: usb-audio: ...
498
499
500
501
  
  		err = snd_usb_select_mode_quirk(subs, fmt);
  		if (err < 0)
  			return -EIO;
71bb64c56   Eldad Zack   ALSA: usb-audio: ...
502
503
  		err = usb_set_interface(dev, fmt->iface, fmt->altsetting);
  		if (err < 0) {
0ba41d917   Takashi Iwai   ALSA: usb-audio: ...
504
505
506
507
  			dev_err(&dev->dev,
  				"%d:%d: usb_set_interface failed (%d)
  ",
  				fmt->iface, fmt->altsetting, err);
71bb64c56   Eldad Zack   ALSA: usb-audio: ...
508
509
  			return -EIO;
  		}
0ba41d917   Takashi Iwai   ALSA: usb-audio: ...
510
511
512
  		dev_dbg(&dev->dev, "setting usb interface %d:%d
  ",
  			fmt->iface, fmt->altsetting);
71bb64c56   Eldad Zack   ALSA: usb-audio: ...
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
  		subs->interface = fmt->iface;
  		subs->altset_idx = fmt->altset_idx;
  
  		snd_usb_set_interface_quirk(dev);
  	}
  
  	subs->data_endpoint = snd_usb_add_endpoint(subs->stream->chip,
  						   alts, fmt->endpoint, subs->direction,
  						   SND_USB_ENDPOINT_TYPE_DATA);
  
  	if (!subs->data_endpoint)
  		return -EINVAL;
  
  	err = set_sync_endpoint(subs, fmt, dev, alts, altsd);
  	if (err < 0)
  		return err;
d133f2c22   Eldad Zack   ALSA: usb-audio: ...
529
530
  	err = snd_usb_init_pitch(subs->stream->chip, fmt->iface, alts, fmt);
  	if (err < 0)
e5779998b   Daniel Mack   ALSA: usb-audio: ...
531
532
533
534
535
  		return err;
  
  	subs->cur_audiofmt = fmt;
  
  	snd_usb_set_format_quirk(subs, fmt);
e5779998b   Daniel Mack   ALSA: usb-audio: ...
536
537
538
539
  	return 0;
  }
  
  /*
0d9741c0e   Eldad Zack   ALSA: usb-audio: ...
540
541
542
543
544
545
   * Return the score of matching two audioformats.
   * Veto the audioformat if:
   * - It has no channels for some reason.
   * - Requested PCM format is not supported.
   * - Requested sample rate is not supported.
   */
0ba41d917   Takashi Iwai   ALSA: usb-audio: ...
546
547
548
549
  static int match_endpoint_audioformats(struct snd_usb_substream *subs,
  				       struct audioformat *fp,
  				       struct audioformat *match, int rate,
  				       snd_pcm_format_t pcm_format)
0d9741c0e   Eldad Zack   ALSA: usb-audio: ...
550
551
552
553
554
  {
  	int i;
  	int score = 0;
  
  	if (fp->channels < 1) {
0ba41d917   Takashi Iwai   ALSA: usb-audio: ...
555
556
557
  		dev_dbg(&subs->dev->dev,
  			"%s: (fmt @%p) no channels
  ", __func__, fp);
0d9741c0e   Eldad Zack   ALSA: usb-audio: ...
558
559
  		return 0;
  	}
74c34ca1c   Eldad Zack   ALSA: pcm_format_...
560
  	if (!(fp->formats & pcm_format_to_bits(pcm_format))) {
0ba41d917   Takashi Iwai   ALSA: usb-audio: ...
561
562
563
  		dev_dbg(&subs->dev->dev,
  			"%s: (fmt @%p) no match for format %d
  ", __func__,
0d9741c0e   Eldad Zack   ALSA: usb-audio: ...
564
565
566
567
568
569
570
571
572
573
574
  			fp, pcm_format);
  		return 0;
  	}
  
  	for (i = 0; i < fp->nr_rates; i++) {
  		if (fp->rate_table[i] == rate) {
  			score++;
  			break;
  		}
  	}
  	if (!score) {
0ba41d917   Takashi Iwai   ALSA: usb-audio: ...
575
576
577
  		dev_dbg(&subs->dev->dev,
  			"%s: (fmt @%p) no match for rate %d
  ", __func__,
0d9741c0e   Eldad Zack   ALSA: usb-audio: ...
578
579
580
581
582
583
  			fp, rate);
  		return 0;
  	}
  
  	if (fp->channels == match->channels)
  		score++;
0ba41d917   Takashi Iwai   ALSA: usb-audio: ...
584
585
586
  	dev_dbg(&subs->dev->dev,
  		"%s: (fmt @%p) score %d
  ", __func__, fp, score);
0d9741c0e   Eldad Zack   ALSA: usb-audio: ...
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
  
  	return score;
  }
  
  /*
   * Configure the sync ep using the rate and pcm format of the data ep.
   */
  static int configure_sync_endpoint(struct snd_usb_substream *subs)
  {
  	int ret;
  	struct audioformat *fp;
  	struct audioformat *sync_fp = NULL;
  	int cur_score = 0;
  	int sync_period_bytes = subs->period_bytes;
  	struct snd_usb_substream *sync_subs =
  		&subs->stream->substream[subs->direction ^ 1];
31be5425d   Takashi Iwai   ALSA: usb-audio: ...
603
604
605
606
607
608
  	if (subs->sync_endpoint->type != SND_USB_ENDPOINT_TYPE_DATA ||
  	    !subs->stream)
  		return snd_usb_endpoint_set_params(subs->sync_endpoint,
  						   subs->pcm_format,
  						   subs->channels,
  						   subs->period_bytes,
976b6c064   Alan Stern   ALSA: improve buf...
609
  						   0, 0,
31be5425d   Takashi Iwai   ALSA: usb-audio: ...
610
611
612
  						   subs->cur_rate,
  						   subs->cur_audiofmt,
  						   NULL);
0d9741c0e   Eldad Zack   ALSA: usb-audio: ...
613
614
  	/* Try to find the best matching audioformat. */
  	list_for_each_entry(fp, &sync_subs->fmt_list, list) {
0ba41d917   Takashi Iwai   ALSA: usb-audio: ...
615
616
  		int score = match_endpoint_audioformats(subs,
  							fp, subs->cur_audiofmt,
0d9741c0e   Eldad Zack   ALSA: usb-audio: ...
617
618
619
620
621
622
623
624
625
  			subs->cur_rate, subs->pcm_format);
  
  		if (score > cur_score) {
  			sync_fp = fp;
  			cur_score = score;
  		}
  	}
  
  	if (unlikely(sync_fp == NULL)) {
0ba41d917   Takashi Iwai   ALSA: usb-audio: ...
626
627
628
  		dev_err(&subs->dev->dev,
  			"%s: no valid audioformat for sync ep %x found
  ",
0d9741c0e   Eldad Zack   ALSA: usb-audio: ...
629
630
631
632
633
634
635
636
637
638
639
  			__func__, sync_subs->ep_num);
  		return -EINVAL;
  	}
  
  	/*
  	 * Recalculate the period bytes if channel number differ between
  	 * data and sync ep audioformat.
  	 */
  	if (sync_fp->channels != subs->channels) {
  		sync_period_bytes = (subs->period_bytes / subs->channels) *
  			sync_fp->channels;
0ba41d917   Takashi Iwai   ALSA: usb-audio: ...
640
641
642
  		dev_dbg(&subs->dev->dev,
  			"%s: adjusted sync ep period bytes (%d -> %d)
  ",
0d9741c0e   Eldad Zack   ALSA: usb-audio: ...
643
644
645
646
647
648
649
  			__func__, subs->period_bytes, sync_period_bytes);
  	}
  
  	ret = snd_usb_endpoint_set_params(subs->sync_endpoint,
  					  subs->pcm_format,
  					  sync_fp->channels,
  					  sync_period_bytes,
976b6c064   Alan Stern   ALSA: improve buf...
650
  					  0, 0,
0d9741c0e   Eldad Zack   ALSA: usb-audio: ...
651
652
653
654
655
656
657
658
  					  subs->cur_rate,
  					  sync_fp,
  					  NULL);
  
  	return ret;
  }
  
  /*
61a709504   Dylan Reid   ALSA: usb-audio: ...
659
660
661
662
663
664
665
   * configure endpoint params
   *
   * called  during initial setup and upon resume
   */
  static int configure_endpoint(struct snd_usb_substream *subs)
  {
  	int ret;
61a709504   Dylan Reid   ALSA: usb-audio: ...
666
  	/* format changed */
b0db6063d   Takashi Iwai   ALSA: usb-audio: ...
667
  	stop_endpoints(subs, true);
61a709504   Dylan Reid   ALSA: usb-audio: ...
668
669
670
671
  	ret = snd_usb_endpoint_set_params(subs->data_endpoint,
  					  subs->pcm_format,
  					  subs->channels,
  					  subs->period_bytes,
976b6c064   Alan Stern   ALSA: improve buf...
672
673
  					  subs->period_frames,
  					  subs->buffer_periods,
61a709504   Dylan Reid   ALSA: usb-audio: ...
674
675
676
677
  					  subs->cur_rate,
  					  subs->cur_audiofmt,
  					  subs->sync_endpoint);
  	if (ret < 0)
978520b75   Takashi Iwai   ALSA: usb-audio: ...
678
  		return ret;
61a709504   Dylan Reid   ALSA: usb-audio: ...
679
680
  
  	if (subs->sync_endpoint)
0d9741c0e   Eldad Zack   ALSA: usb-audio: ...
681
  		ret = configure_sync_endpoint(subs);
61a709504   Dylan Reid   ALSA: usb-audio: ...
682
683
684
685
  	return ret;
  }
  
  /*
e5779998b   Daniel Mack   ALSA: usb-audio: ...
686
687
688
689
690
691
692
693
694
695
696
697
698
699
   * hw_params callback
   *
   * allocate a buffer and set the given audio format.
   *
   * so far we use a physically linear buffer although packetize transfer
   * doesn't need a continuous area.
   * if sg buffer is supported on the later version of alsa, we'll follow
   * that.
   */
  static int snd_usb_hw_params(struct snd_pcm_substream *substream,
  			     struct snd_pcm_hw_params *hw_params)
  {
  	struct snd_usb_substream *subs = substream->runtime->private_data;
  	struct audioformat *fmt;
61a709504   Dylan Reid   ALSA: usb-audio: ...
700
  	int ret;
e5779998b   Daniel Mack   ALSA: usb-audio: ...
701
702
703
704
  
  	ret = snd_pcm_lib_alloc_vmalloc_buffer(substream,
  					       params_buffer_bytes(hw_params));
  	if (ret < 0)
c89178f57   Mauro Carvalho Chehab   [media] Revert "[...
705
  		return ret;
e5779998b   Daniel Mack   ALSA: usb-audio: ...
706

61a709504   Dylan Reid   ALSA: usb-audio: ...
707
708
  	subs->pcm_format = params_format(hw_params);
  	subs->period_bytes = params_period_bytes(hw_params);
976b6c064   Alan Stern   ALSA: improve buf...
709
710
  	subs->period_frames = params_period_size(hw_params);
  	subs->buffer_periods = params_periods(hw_params);
61a709504   Dylan Reid   ALSA: usb-audio: ...
711
712
713
714
  	subs->channels = params_channels(hw_params);
  	subs->cur_rate = params_rate(hw_params);
  
  	fmt = find_format(subs);
e5779998b   Daniel Mack   ALSA: usb-audio: ...
715
  	if (!fmt) {
0ba41d917   Takashi Iwai   ALSA: usb-audio: ...
716
717
718
  		dev_dbg(&subs->dev->dev,
  			"cannot set format: format = %#x, rate = %d, channels = %d
  ",
61a709504   Dylan Reid   ALSA: usb-audio: ...
719
  			   subs->pcm_format, subs->cur_rate, subs->channels);
c89178f57   Mauro Carvalho Chehab   [media] Revert "[...
720
  		return -EINVAL;
e5779998b   Daniel Mack   ALSA: usb-audio: ...
721
  	}
47ab15459   Takashi Iwai   ALSA: usb-audio: ...
722
723
  	ret = snd_usb_lock_shutdown(subs->stream->chip);
  	if (ret < 0)
c89178f57   Mauro Carvalho Chehab   [media] Revert "[...
724
  		return ret;
47ab15459   Takashi Iwai   ALSA: usb-audio: ...
725
726
  	ret = set_format(subs, fmt);
  	snd_usb_unlock_shutdown(subs->stream->chip);
978520b75   Takashi Iwai   ALSA: usb-audio: ...
727
  	if (ret < 0)
c89178f57   Mauro Carvalho Chehab   [media] Revert "[...
728
  		return ret;
e5779998b   Daniel Mack   ALSA: usb-audio: ...
729

61a709504   Dylan Reid   ALSA: usb-audio: ...
730
731
  	subs->interface = fmt->iface;
  	subs->altset_idx = fmt->altset_idx;
384dc085c   Takashi Iwai   ALSA: usb-audio: ...
732
  	subs->need_setup_ep = true;
715a17056   Dylan Reid   ALSA: usb-audio: ...
733

61a709504   Dylan Reid   ALSA: usb-audio: ...
734
  	return 0;
e5779998b   Daniel Mack   ALSA: usb-audio: ...
735
736
737
738
739
740
741
742
743
744
745
746
747
748
  }
  
  /*
   * hw_free callback
   *
   * reset the audio format and release the buffer
   */
  static int snd_usb_hw_free(struct snd_pcm_substream *substream)
  {
  	struct snd_usb_substream *subs = substream->runtime->private_data;
  
  	subs->cur_audiofmt = NULL;
  	subs->cur_rate = 0;
  	subs->period_bytes = 0;
47ab15459   Takashi Iwai   ALSA: usb-audio: ...
749
  	if (!snd_usb_lock_shutdown(subs->stream->chip)) {
a9bb36261   Takashi Iwai   ALSA: usb-audio: ...
750
  		stop_endpoints(subs, true);
26de5d0a8   Eldad Zack   ALSA: usb-audio: ...
751
752
  		snd_usb_endpoint_deactivate(subs->sync_endpoint);
  		snd_usb_endpoint_deactivate(subs->data_endpoint);
47ab15459   Takashi Iwai   ALSA: usb-audio: ...
753
  		snd_usb_unlock_shutdown(subs->stream->chip);
978520b75   Takashi Iwai   ALSA: usb-audio: ...
754
  	}
e5779998b   Daniel Mack   ALSA: usb-audio: ...
755
756
757
758
759
760
761
762
763
764
765
766
  	return snd_pcm_lib_free_vmalloc_buffer(substream);
  }
  
  /*
   * prepare callback
   *
   * only a few subtle things...
   */
  static int snd_usb_pcm_prepare(struct snd_pcm_substream *substream)
  {
  	struct snd_pcm_runtime *runtime = substream->runtime;
  	struct snd_usb_substream *subs = runtime->private_data;
61a709504   Dylan Reid   ALSA: usb-audio: ...
767
768
769
  	struct usb_host_interface *alts;
  	struct usb_interface *iface;
  	int ret;
e5779998b   Daniel Mack   ALSA: usb-audio: ...
770
771
  
  	if (! subs->cur_audiofmt) {
0ba41d917   Takashi Iwai   ALSA: usb-audio: ...
772
773
  		dev_err(&subs->dev->dev, "no format is specified!
  ");
e5779998b   Daniel Mack   ALSA: usb-audio: ...
774
775
  		return -ENXIO;
  	}
47ab15459   Takashi Iwai   ALSA: usb-audio: ...
776
777
778
  	ret = snd_usb_lock_shutdown(subs->stream->chip);
  	if (ret < 0)
  		return ret;
978520b75   Takashi Iwai   ALSA: usb-audio: ...
779
780
781
782
  	if (snd_BUG_ON(!subs->data_endpoint)) {
  		ret = -EIO;
  		goto unlock;
  	}
edcd3633e   Daniel Mack   ALSA: snd-usb: sw...
783

f58161ba1   Takashi Iwai   ALSA: usb-audio: ...
784
785
  	snd_usb_endpoint_sync_pending_stop(subs->sync_endpoint);
  	snd_usb_endpoint_sync_pending_stop(subs->data_endpoint);
61a709504   Dylan Reid   ALSA: usb-audio: ...
786
787
  	ret = set_format(subs, subs->cur_audiofmt);
  	if (ret < 0)
978520b75   Takashi Iwai   ALSA: usb-audio: ...
788
  		goto unlock;
61a709504   Dylan Reid   ALSA: usb-audio: ...
789
790
791
792
793
794
795
796
797
  
  	iface = usb_ifnum_to_if(subs->dev, subs->cur_audiofmt->iface);
  	alts = &iface->altsetting[subs->cur_audiofmt->altset_idx];
  	ret = snd_usb_init_sample_rate(subs->stream->chip,
  				       subs->cur_audiofmt->iface,
  				       alts,
  				       subs->cur_audiofmt,
  				       subs->cur_rate);
  	if (ret < 0)
978520b75   Takashi Iwai   ALSA: usb-audio: ...
798
  		goto unlock;
61a709504   Dylan Reid   ALSA: usb-audio: ...
799

384dc085c   Takashi Iwai   ALSA: usb-audio: ...
800
801
802
  	if (subs->need_setup_ep) {
  		ret = configure_endpoint(subs);
  		if (ret < 0)
978520b75   Takashi Iwai   ALSA: usb-audio: ...
803
  			goto unlock;
384dc085c   Takashi Iwai   ALSA: usb-audio: ...
804
805
  		subs->need_setup_ep = false;
  	}
61a709504   Dylan Reid   ALSA: usb-audio: ...
806

e5779998b   Daniel Mack   ALSA: usb-audio: ...
807
  	/* some unit conversions in runtime */
edcd3633e   Daniel Mack   ALSA: snd-usb: sw...
808
809
810
811
  	subs->data_endpoint->maxframesize =
  		bytes_to_frames(runtime, subs->data_endpoint->maxpacksize);
  	subs->data_endpoint->curframesize =
  		bytes_to_frames(runtime, subs->data_endpoint->curpacksize);
e5779998b   Daniel Mack   ALSA: usb-audio: ...
812
813
814
815
  
  	/* reset the pointer */
  	subs->hwptr_done = 0;
  	subs->transfer_done = 0;
294c4fb8a   Pierre-Louis Bossart   ALSA: usb: refine...
816
817
  	subs->last_delay = 0;
  	subs->last_frame_number = 0;
e5779998b   Daniel Mack   ALSA: usb-audio: ...
818
  	runtime->delay = 0;
edcd3633e   Daniel Mack   ALSA: snd-usb: sw...
819
820
821
  	/* for playback, submit the URBs now; otherwise, the first hwptr_done
  	 * updates for all URBs would happen at the same time when starting */
  	if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK)
be4e3aec5   Ioan-Adrian Ratiu   ALSA: usb-audio: ...
822
  		ret = start_endpoints(subs);
edcd3633e   Daniel Mack   ALSA: snd-usb: sw...
823

978520b75   Takashi Iwai   ALSA: usb-audio: ...
824
   unlock:
47ab15459   Takashi Iwai   ALSA: usb-audio: ...
825
  	snd_usb_unlock_shutdown(subs->stream->chip);
978520b75   Takashi Iwai   ALSA: usb-audio: ...
826
  	return ret;
e5779998b   Daniel Mack   ALSA: usb-audio: ...
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
  }
  
  static struct snd_pcm_hardware snd_usb_hardware =
  {
  	.info =			SNDRV_PCM_INFO_MMAP |
  				SNDRV_PCM_INFO_MMAP_VALID |
  				SNDRV_PCM_INFO_BATCH |
  				SNDRV_PCM_INFO_INTERLEAVED |
  				SNDRV_PCM_INFO_BLOCK_TRANSFER |
  				SNDRV_PCM_INFO_PAUSE,
  	.buffer_bytes_max =	1024 * 1024,
  	.period_bytes_min =	64,
  	.period_bytes_max =	512 * 1024,
  	.periods_min =		2,
  	.periods_max =		1024,
  };
  
  static int hw_check_valid_format(struct snd_usb_substream *subs,
  				 struct snd_pcm_hw_params *params,
  				 struct audioformat *fp)
  {
  	struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
  	struct snd_interval *ct = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
  	struct snd_mask *fmts = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
  	struct snd_interval *pt = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
015eb0b08   Clemens Ladisch   ALSA: usb-audio: ...
852
  	struct snd_mask check_fmts;
e5779998b   Daniel Mack   ALSA: usb-audio: ...
853
854
855
  	unsigned int ptime;
  
  	/* check the format */
015eb0b08   Clemens Ladisch   ALSA: usb-audio: ...
856
857
858
859
860
  	snd_mask_none(&check_fmts);
  	check_fmts.bits[0] = (u32)fp->formats;
  	check_fmts.bits[1] = (u32)(fp->formats >> 32);
  	snd_mask_intersect(&check_fmts, fmts);
  	if (snd_mask_empty(&check_fmts)) {
e5779998b   Daniel Mack   ALSA: usb-audio: ...
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
  		hwc_debug("   > check: no supported format %d
  ", fp->format);
  		return 0;
  	}
  	/* check the channels */
  	if (fp->channels < ct->min || fp->channels > ct->max) {
  		hwc_debug("   > check: no valid channels %d (%d/%d)
  ", fp->channels, ct->min, ct->max);
  		return 0;
  	}
  	/* check the rate is within the range */
  	if (fp->rate_min > it->max || (fp->rate_min == it->max && it->openmax)) {
  		hwc_debug("   > check: rate_min %d > max %d
  ", fp->rate_min, it->max);
  		return 0;
  	}
  	if (fp->rate_max < it->min || (fp->rate_max == it->min && it->openmin)) {
  		hwc_debug("   > check: rate_max %d < min %d
  ", fp->rate_max, it->min);
  		return 0;
  	}
  	/* check whether the period time is >= the data packet interval */
978520b75   Takashi Iwai   ALSA: usb-audio: ...
883
  	if (subs->speed != USB_SPEED_FULL) {
e5779998b   Daniel Mack   ALSA: usb-audio: ...
884
885
886
887
888
889
890
891
892
893
894
895
896
897
  		ptime = 125 * (1 << fp->datainterval);
  		if (ptime > pt->max || (ptime == pt->max && pt->openmax)) {
  			hwc_debug("   > check: ptime %u > max %u
  ", ptime, pt->max);
  			return 0;
  		}
  	}
  	return 1;
  }
  
  static int hw_rule_rate(struct snd_pcm_hw_params *params,
  			struct snd_pcm_hw_rule *rule)
  {
  	struct snd_usb_substream *subs = rule->private;
88766f04c   Eldad Zack   ALSA: usb-audio: ...
898
  	struct audioformat *fp;
e5779998b   Daniel Mack   ALSA: usb-audio: ...
899
900
901
902
903
904
905
906
  	struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
  	unsigned int rmin, rmax;
  	int changed;
  
  	hwc_debug("hw_rule_rate: (%d,%d)
  ", it->min, it->max);
  	changed = 0;
  	rmin = rmax = 0;
88766f04c   Eldad Zack   ALSA: usb-audio: ...
907
  	list_for_each_entry(fp, &subs->fmt_list, list) {
e5779998b   Daniel Mack   ALSA: usb-audio: ...
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
  		if (!hw_check_valid_format(subs, params, fp))
  			continue;
  		if (changed++) {
  			if (rmin > fp->rate_min)
  				rmin = fp->rate_min;
  			if (rmax < fp->rate_max)
  				rmax = fp->rate_max;
  		} else {
  			rmin = fp->rate_min;
  			rmax = fp->rate_max;
  		}
  	}
  
  	if (!changed) {
  		hwc_debug("  --> get empty
  ");
  		it->empty = 1;
  		return -EINVAL;
  	}
  
  	changed = 0;
  	if (it->min < rmin) {
  		it->min = rmin;
  		it->openmin = 0;
  		changed = 1;
  	}
  	if (it->max > rmax) {
  		it->max = rmax;
  		it->openmax = 0;
  		changed = 1;
  	}
  	if (snd_interval_checkempty(it)) {
  		it->empty = 1;
  		return -EINVAL;
  	}
  	hwc_debug("  --> (%d, %d) (changed = %d)
  ", it->min, it->max, changed);
  	return changed;
  }
  
  
  static int hw_rule_channels(struct snd_pcm_hw_params *params,
  			    struct snd_pcm_hw_rule *rule)
  {
  	struct snd_usb_substream *subs = rule->private;
88766f04c   Eldad Zack   ALSA: usb-audio: ...
953
  	struct audioformat *fp;
e5779998b   Daniel Mack   ALSA: usb-audio: ...
954
955
956
957
958
959
960
961
  	struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
  	unsigned int rmin, rmax;
  	int changed;
  
  	hwc_debug("hw_rule_channels: (%d,%d)
  ", it->min, it->max);
  	changed = 0;
  	rmin = rmax = 0;
88766f04c   Eldad Zack   ALSA: usb-audio: ...
962
  	list_for_each_entry(fp, &subs->fmt_list, list) {
e5779998b   Daniel Mack   ALSA: usb-audio: ...
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
  		if (!hw_check_valid_format(subs, params, fp))
  			continue;
  		if (changed++) {
  			if (rmin > fp->channels)
  				rmin = fp->channels;
  			if (rmax < fp->channels)
  				rmax = fp->channels;
  		} else {
  			rmin = fp->channels;
  			rmax = fp->channels;
  		}
  	}
  
  	if (!changed) {
  		hwc_debug("  --> get empty
  ");
  		it->empty = 1;
  		return -EINVAL;
  	}
  
  	changed = 0;
  	if (it->min < rmin) {
  		it->min = rmin;
  		it->openmin = 0;
  		changed = 1;
  	}
  	if (it->max > rmax) {
  		it->max = rmax;
  		it->openmax = 0;
  		changed = 1;
  	}
  	if (snd_interval_checkempty(it)) {
  		it->empty = 1;
  		return -EINVAL;
  	}
  	hwc_debug("  --> (%d, %d) (changed = %d)
  ", it->min, it->max, changed);
  	return changed;
  }
  
  static int hw_rule_format(struct snd_pcm_hw_params *params,
  			  struct snd_pcm_hw_rule *rule)
  {
  	struct snd_usb_substream *subs = rule->private;
88766f04c   Eldad Zack   ALSA: usb-audio: ...
1007
  	struct audioformat *fp;
e5779998b   Daniel Mack   ALSA: usb-audio: ...
1008
1009
1010
1011
1012
1013
1014
1015
  	struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
  	u64 fbits;
  	u32 oldbits[2];
  	int changed;
  
  	hwc_debug("hw_rule_format: %x:%x
  ", fmt->bits[0], fmt->bits[1]);
  	fbits = 0;
88766f04c   Eldad Zack   ALSA: usb-audio: ...
1016
  	list_for_each_entry(fp, &subs->fmt_list, list) {
e5779998b   Daniel Mack   ALSA: usb-audio: ...
1017
1018
  		if (!hw_check_valid_format(subs, params, fp))
  			continue;
015eb0b08   Clemens Ladisch   ALSA: usb-audio: ...
1019
  		fbits |= fp->formats;
e5779998b   Daniel Mack   ALSA: usb-audio: ...
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
  	}
  
  	oldbits[0] = fmt->bits[0];
  	oldbits[1] = fmt->bits[1];
  	fmt->bits[0] &= (u32)fbits;
  	fmt->bits[1] &= (u32)(fbits >> 32);
  	if (!fmt->bits[0] && !fmt->bits[1]) {
  		hwc_debug("  --> get empty
  ");
  		return -EINVAL;
  	}
  	changed = (oldbits[0] != fmt->bits[0] || oldbits[1] != fmt->bits[1]);
  	hwc_debug("  --> %x:%x (changed = %d)
  ", fmt->bits[0], fmt->bits[1], changed);
  	return changed;
  }
  
  static int hw_rule_period_time(struct snd_pcm_hw_params *params,
  			       struct snd_pcm_hw_rule *rule)
  {
  	struct snd_usb_substream *subs = rule->private;
  	struct audioformat *fp;
  	struct snd_interval *it;
  	unsigned char min_datainterval;
  	unsigned int pmin;
  	int changed;
  
  	it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
  	hwc_debug("hw_rule_period_time: (%u,%u)
  ", it->min, it->max);
  	min_datainterval = 0xff;
  	list_for_each_entry(fp, &subs->fmt_list, list) {
  		if (!hw_check_valid_format(subs, params, fp))
  			continue;
  		min_datainterval = min(min_datainterval, fp->datainterval);
  	}
  	if (min_datainterval == 0xff) {
a7ce2e0d0   Uwe Kleine-König   fix comnment/prin...
1057
1058
  		hwc_debug("  --> get empty
  ");
e5779998b   Daniel Mack   ALSA: usb-audio: ...
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
  		it->empty = 1;
  		return -EINVAL;
  	}
  	pmin = 125 * (1 << min_datainterval);
  	changed = 0;
  	if (it->min < pmin) {
  		it->min = pmin;
  		it->openmin = 0;
  		changed = 1;
  	}
  	if (snd_interval_checkempty(it)) {
  		it->empty = 1;
  		return -EINVAL;
  	}
  	hwc_debug("  --> (%u,%u) (changed = %d)
  ", it->min, it->max, changed);
  	return changed;
  }
  
  /*
   *  If the device supports unusual bit rates, does the request meet these?
   */
  static int snd_usb_pcm_check_knot(struct snd_pcm_runtime *runtime,
  				  struct snd_usb_substream *subs)
  {
  	struct audioformat *fp;
0717d0f5d   Takashi Iwai   ALSA: usb-audio -...
1085
  	int *rate_list;
e5779998b   Daniel Mack   ALSA: usb-audio: ...
1086
1087
  	int count = 0, needs_knot = 0;
  	int err;
5cd5d7c44   Clemens Ladisch   ALSA: usb-audio: ...
1088
1089
  	kfree(subs->rate_list.list);
  	subs->rate_list.list = NULL;
e5779998b   Daniel Mack   ALSA: usb-audio: ...
1090
1091
1092
1093
1094
1095
1096
1097
1098
  	list_for_each_entry(fp, &subs->fmt_list, list) {
  		if (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)
  			return 0;
  		count += fp->nr_rates;
  		if (fp->rates & SNDRV_PCM_RATE_KNOT)
  			needs_knot = 1;
  	}
  	if (!needs_knot)
  		return 0;
0717d0f5d   Takashi Iwai   ALSA: usb-audio -...
1099
1100
  	subs->rate_list.list = rate_list =
  		kmalloc(sizeof(int) * count, GFP_KERNEL);
8a8d56b2a   Jesper Juhl   ALSA: usb - drive...
1101
1102
1103
  	if (!subs->rate_list.list)
  		return -ENOMEM;
  	subs->rate_list.count = count;
e5779998b   Daniel Mack   ALSA: usb-audio: ...
1104
1105
1106
1107
1108
  	subs->rate_list.mask = 0;
  	count = 0;
  	list_for_each_entry(fp, &subs->fmt_list, list) {
  		int i;
  		for (i = 0; i < fp->nr_rates; i++)
0717d0f5d   Takashi Iwai   ALSA: usb-audio -...
1109
  			rate_list[count++] = fp->rate_table[i];
e5779998b   Daniel Mack   ALSA: usb-audio: ...
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
  	}
  	err = snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
  					 &subs->rate_list);
  	if (err < 0)
  		return err;
  
  	return 0;
  }
  
  
  /*
   * set up the runtime hardware information.
   */
  
  static int setup_hw_info(struct snd_pcm_runtime *runtime, struct snd_usb_substream *subs)
  {
88766f04c   Eldad Zack   ALSA: usb-audio: ...
1126
  	struct audioformat *fp;
e5779998b   Daniel Mack   ALSA: usb-audio: ...
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
  	unsigned int pt, ptmin;
  	int param_period_time_if_needed;
  	int err;
  
  	runtime->hw.formats = subs->formats;
  
  	runtime->hw.rate_min = 0x7fffffff;
  	runtime->hw.rate_max = 0;
  	runtime->hw.channels_min = 256;
  	runtime->hw.channels_max = 0;
  	runtime->hw.rates = 0;
  	ptmin = UINT_MAX;
  	/* check min/max rates and channels */
88766f04c   Eldad Zack   ALSA: usb-audio: ...
1140
  	list_for_each_entry(fp, &subs->fmt_list, list) {
e5779998b   Daniel Mack   ALSA: usb-audio: ...
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
  		runtime->hw.rates |= fp->rates;
  		if (runtime->hw.rate_min > fp->rate_min)
  			runtime->hw.rate_min = fp->rate_min;
  		if (runtime->hw.rate_max < fp->rate_max)
  			runtime->hw.rate_max = fp->rate_max;
  		if (runtime->hw.channels_min > fp->channels)
  			runtime->hw.channels_min = fp->channels;
  		if (runtime->hw.channels_max < fp->channels)
  			runtime->hw.channels_max = fp->channels;
  		if (fp->fmt_type == UAC_FORMAT_TYPE_II && fp->frame_size > 0) {
  			/* FIXME: there might be more than one audio formats... */
  			runtime->hw.period_bytes_min = runtime->hw.period_bytes_max =
  				fp->frame_size;
  		}
  		pt = 125 * (1 << fp->datainterval);
  		ptmin = min(ptmin, pt);
  	}
88a8516a2   Oliver Neukum   ALSA: usbaudio: i...
1158
1159
1160
  	err = snd_usb_autoresume(subs->stream->chip);
  	if (err < 0)
  		return err;
e5779998b   Daniel Mack   ALSA: usb-audio: ...
1161
1162
  
  	param_period_time_if_needed = SNDRV_PCM_HW_PARAM_PERIOD_TIME;
978520b75   Takashi Iwai   ALSA: usb-audio: ...
1163
  	if (subs->speed == USB_SPEED_FULL)
e5779998b   Daniel Mack   ALSA: usb-audio: ...
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
  		/* full speed devices have fixed data packet interval */
  		ptmin = 1000;
  	if (ptmin == 1000)
  		/* if period time doesn't go below 1 ms, no rules needed */
  		param_period_time_if_needed = -1;
  	snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
  				     ptmin, UINT_MAX);
  
  	if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
  				       hw_rule_rate, subs,
  				       SNDRV_PCM_HW_PARAM_FORMAT,
  				       SNDRV_PCM_HW_PARAM_CHANNELS,
  				       param_period_time_if_needed,
  				       -1)) < 0)
88a8516a2   Oliver Neukum   ALSA: usbaudio: i...
1178
  		goto rep_err;
e5779998b   Daniel Mack   ALSA: usb-audio: ...
1179
1180
1181
1182
1183
1184
  	if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
  				       hw_rule_channels, subs,
  				       SNDRV_PCM_HW_PARAM_FORMAT,
  				       SNDRV_PCM_HW_PARAM_RATE,
  				       param_period_time_if_needed,
  				       -1)) < 0)
88a8516a2   Oliver Neukum   ALSA: usbaudio: i...
1185
  		goto rep_err;
e5779998b   Daniel Mack   ALSA: usb-audio: ...
1186
1187
1188
1189
1190
1191
  	if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
  				       hw_rule_format, subs,
  				       SNDRV_PCM_HW_PARAM_RATE,
  				       SNDRV_PCM_HW_PARAM_CHANNELS,
  				       param_period_time_if_needed,
  				       -1)) < 0)
88a8516a2   Oliver Neukum   ALSA: usbaudio: i...
1192
  		goto rep_err;
e5779998b   Daniel Mack   ALSA: usb-audio: ...
1193
1194
1195
1196
1197
1198
1199
1200
1201
  	if (param_period_time_if_needed >= 0) {
  		err = snd_pcm_hw_rule_add(runtime, 0,
  					  SNDRV_PCM_HW_PARAM_PERIOD_TIME,
  					  hw_rule_period_time, subs,
  					  SNDRV_PCM_HW_PARAM_FORMAT,
  					  SNDRV_PCM_HW_PARAM_CHANNELS,
  					  SNDRV_PCM_HW_PARAM_RATE,
  					  -1);
  		if (err < 0)
88a8516a2   Oliver Neukum   ALSA: usbaudio: i...
1202
  			goto rep_err;
e5779998b   Daniel Mack   ALSA: usb-audio: ...
1203
1204
  	}
  	if ((err = snd_usb_pcm_check_knot(runtime, subs)) < 0)
88a8516a2   Oliver Neukum   ALSA: usbaudio: i...
1205
  		goto rep_err;
e5779998b   Daniel Mack   ALSA: usb-audio: ...
1206
  	return 0;
88a8516a2   Oliver Neukum   ALSA: usbaudio: i...
1207
1208
1209
1210
  
  rep_err:
  	snd_usb_autosuspend(subs->stream->chip);
  	return err;
e5779998b   Daniel Mack   ALSA: usb-audio: ...
1211
1212
1213
1214
1215
1216
1217
1218
1219
  }
  
  static int snd_usb_pcm_open(struct snd_pcm_substream *substream, int direction)
  {
  	struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
  	struct snd_pcm_runtime *runtime = substream->runtime;
  	struct snd_usb_substream *subs = &as->substream[direction];
  
  	subs->interface = -1;
e11b4e0e4   Clemens Ladisch   ALSA: usb-audio: ...
1220
  	subs->altset_idx = 0;
e5779998b   Daniel Mack   ALSA: usb-audio: ...
1221
1222
1223
  	runtime->hw = snd_usb_hardware;
  	runtime->private_data = subs;
  	subs->pcm_substream = substream;
88a8516a2   Oliver Neukum   ALSA: usbaudio: i...
1224
  	/* runtime PM is also done there */
d24f5061e   Daniel Mack   ALSA: snd-usb: ad...
1225
1226
1227
1228
1229
  
  	/* initialize DSD/DOP context */
  	subs->dsd_dop.byte_idx = 0;
  	subs->dsd_dop.channel = 0;
  	subs->dsd_dop.marker = 1;
c89178f57   Mauro Carvalho Chehab   [media] Revert "[...
1230
  	return setup_hw_info(runtime, subs);
e5779998b   Daniel Mack   ALSA: usb-audio: ...
1231
1232
1233
1234
1235
1236
  }
  
  static int snd_usb_pcm_close(struct snd_pcm_substream *substream, int direction)
  {
  	struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
  	struct snd_usb_substream *subs = &as->substream[direction];
b0db6063d   Takashi Iwai   ALSA: usb-audio: ...
1237
  	stop_endpoints(subs, true);
68e67f40b   Daniel Mack   ALSA: snd-usb: mo...
1238

47ab15459   Takashi Iwai   ALSA: usb-audio: ...
1239
1240
  	if (subs->interface >= 0 &&
  	    !snd_usb_lock_shutdown(subs->stream->chip)) {
68e67f40b   Daniel Mack   ALSA: snd-usb: mo...
1241
1242
  		usb_set_interface(subs->dev, subs->interface, 0);
  		subs->interface = -1;
47ab15459   Takashi Iwai   ALSA: usb-audio: ...
1243
  		snd_usb_unlock_shutdown(subs->stream->chip);
68e67f40b   Daniel Mack   ALSA: snd-usb: mo...
1244
  	}
e5779998b   Daniel Mack   ALSA: usb-audio: ...
1245
  	subs->pcm_substream = NULL;
88a8516a2   Oliver Neukum   ALSA: usbaudio: i...
1246
  	snd_usb_autosuspend(subs->stream->chip);
edcd3633e   Daniel Mack   ALSA: snd-usb: sw...
1247

68e67f40b   Daniel Mack   ALSA: snd-usb: mo...
1248
  	return 0;
edcd3633e   Daniel Mack   ALSA: snd-usb: sw...
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
  }
  
  /* Since a URB can handle only a single linear buffer, we must use double
   * buffering when the data to be transferred overflows the buffer boundary.
   * To avoid inconsistencies when updating hwptr_done, we use double buffering
   * for all URBs.
   */
  static void retire_capture_urb(struct snd_usb_substream *subs,
  			       struct urb *urb)
  {
  	struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
  	unsigned int stride, frames, bytes, oldptr;
  	int i, period_elapsed = 0;
  	unsigned long flags;
  	unsigned char *cp;
e4cc61534   Pierre-Louis Bossart   ALSA: usb-audio: ...
1264
1265
1266
1267
  	int current_frame_number;
  
  	/* read frame number here, update pointer in critical section */
  	current_frame_number = usb_get_current_frame_number(subs->dev);
edcd3633e   Daniel Mack   ALSA: snd-usb: sw...
1268
1269
1270
1271
  
  	stride = runtime->frame_bits >> 3;
  
  	for (i = 0; i < urb->number_of_packets; i++) {
1539d4f82   Calvin Owens   ALSA: usb: Add qu...
1272
  		cp = (unsigned char *)urb->transfer_buffer + urb->iso_frame_desc[i].offset + subs->pkt_offset_adj;
edcd3633e   Daniel Mack   ALSA: snd-usb: sw...
1273
  		if (urb->iso_frame_desc[i].status && printk_ratelimit()) {
0ba41d917   Takashi Iwai   ALSA: usb-audio: ...
1274
1275
1276
  			dev_dbg(&subs->dev->dev, "frame %d active: %d
  ",
  				i, urb->iso_frame_desc[i].status);
edcd3633e   Daniel Mack   ALSA: snd-usb: sw...
1277
1278
1279
1280
1281
1282
1283
  			// continue;
  		}
  		bytes = urb->iso_frame_desc[i].actual_length;
  		frames = bytes / stride;
  		if (!subs->txfr_quirk)
  			bytes = frames * stride;
  		if (bytes % (runtime->sample_bits >> 3) != 0) {
edcd3633e   Daniel Mack   ALSA: snd-usb: sw...
1284
  			int oldbytes = bytes;
edcd3633e   Daniel Mack   ALSA: snd-usb: sw...
1285
  			bytes = frames * stride;
0ba41d917   Takashi Iwai   ALSA: usb-audio: ...
1286
1287
1288
  			dev_warn(&subs->dev->dev,
  				 "Corrected urb data len. %d->%d
  ",
edcd3633e   Daniel Mack   ALSA: snd-usb: sw...
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
  							oldbytes, bytes);
  		}
  		/* update the current pointer */
  		spin_lock_irqsave(&subs->lock, flags);
  		oldptr = subs->hwptr_done;
  		subs->hwptr_done += bytes;
  		if (subs->hwptr_done >= runtime->buffer_size * stride)
  			subs->hwptr_done -= runtime->buffer_size * stride;
  		frames = (bytes + (oldptr % stride)) / stride;
  		subs->transfer_done += frames;
  		if (subs->transfer_done >= runtime->period_size) {
  			subs->transfer_done -= runtime->period_size;
  			period_elapsed = 1;
  		}
e4cc61534   Pierre-Louis Bossart   ALSA: usb-audio: ...
1303
1304
1305
1306
1307
1308
1309
1310
  		/* capture delay is by construction limited to one URB,
  		 * reset delays here
  		 */
  		runtime->delay = subs->last_delay = 0;
  
  		/* realign last_frame_number */
  		subs->last_frame_number = current_frame_number;
  		subs->last_frame_number &= 0xFF; /* keep 8 LSBs */
edcd3633e   Daniel Mack   ALSA: snd-usb: sw...
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
  		spin_unlock_irqrestore(&subs->lock, flags);
  		/* copy a data chunk */
  		if (oldptr + bytes > runtime->buffer_size * stride) {
  			unsigned int bytes1 =
  					runtime->buffer_size * stride - oldptr;
  			memcpy(runtime->dma_area + oldptr, cp, bytes1);
  			memcpy(runtime->dma_area, cp + bytes1, bytes - bytes1);
  		} else {
  			memcpy(runtime->dma_area + oldptr, cp, bytes);
  		}
  	}
  
  	if (period_elapsed)
  		snd_pcm_period_elapsed(subs->pcm_substream);
  }
d24f5061e   Daniel Mack   ALSA: snd-usb: ad...
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
  static inline void fill_playback_urb_dsd_dop(struct snd_usb_substream *subs,
  					     struct urb *urb, unsigned int bytes)
  {
  	struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
  	unsigned int stride = runtime->frame_bits >> 3;
  	unsigned int dst_idx = 0;
  	unsigned int src_idx = subs->hwptr_done;
  	unsigned int wrap = runtime->buffer_size * stride;
  	u8 *dst = urb->transfer_buffer;
  	u8 *src = runtime->dma_area;
  	u8 marker[] = { 0x05, 0xfa };
  
  	/*
  	 * The DSP DOP format defines a way to transport DSD samples over
  	 * normal PCM data endpoints. It requires stuffing of marker bytes
  	 * (0x05 and 0xfa, alternating per sample frame), and then expects
  	 * 2 additional bytes of actual payload. The whole frame is stored
  	 * LSB.
  	 *
  	 * Hence, for a stereo transport, the buffer layout looks like this,
  	 * where L refers to left channel samples and R to right.
  	 *
  	 *   L1 L2 0x05   R1 R2 0x05   L3 L4 0xfa  R3 R4 0xfa
  	 *   L5 L6 0x05   R5 R6 0x05   L7 L8 0xfa  R7 R8 0xfa
  	 *   .....
  	 *
  	 */
  
  	while (bytes--) {
  		if (++subs->dsd_dop.byte_idx == 3) {
  			/* frame boundary? */
  			dst[dst_idx++] = marker[subs->dsd_dop.marker];
  			src_idx += 2;
  			subs->dsd_dop.byte_idx = 0;
  
  			if (++subs->dsd_dop.channel % runtime->channels == 0) {
  				/* alternate the marker */
  				subs->dsd_dop.marker++;
  				subs->dsd_dop.marker %= ARRAY_SIZE(marker);
  				subs->dsd_dop.channel = 0;
  			}
  		} else {
  			/* stuff the DSD payload */
  			int idx = (src_idx + subs->dsd_dop.byte_idx - 1) % wrap;
44dcbbb1c   Daniel Mack   ALSA: snd-usb: ad...
1370
1371
1372
1373
1374
  
  			if (subs->cur_audiofmt->dsd_bitrev)
  				dst[dst_idx++] = bitrev8(src[idx]);
  			else
  				dst[dst_idx++] = src[idx];
d24f5061e   Daniel Mack   ALSA: snd-usb: ad...
1375
1376
1377
  			subs->hwptr_done++;
  		}
  	}
4c4e4391b   Ricard Wanderlof   ALSA: USB-audio: ...
1378
1379
  	if (subs->hwptr_done >= runtime->buffer_size * stride)
  		subs->hwptr_done -= runtime->buffer_size * stride;
d24f5061e   Daniel Mack   ALSA: snd-usb: ad...
1380
  }
b97a93691   Ricard Wanderlof   ALSA: USB-audio: ...
1381
1382
  static void copy_to_urb(struct snd_usb_substream *subs, struct urb *urb,
  			int offset, int stride, unsigned int bytes)
07a40c2fc   Ricard Wanderlof   ALSA: USB-audio: ...
1383
1384
1385
1386
1387
1388
1389
  {
  	struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
  
  	if (subs->hwptr_done + bytes > runtime->buffer_size * stride) {
  		/* err, the transferred area goes over buffer boundary. */
  		unsigned int bytes1 =
  			runtime->buffer_size * stride - subs->hwptr_done;
b97a93691   Ricard Wanderlof   ALSA: USB-audio: ...
1390
  		memcpy(urb->transfer_buffer + offset,
07a40c2fc   Ricard Wanderlof   ALSA: USB-audio: ...
1391
  		       runtime->dma_area + subs->hwptr_done, bytes1);
b97a93691   Ricard Wanderlof   ALSA: USB-audio: ...
1392
  		memcpy(urb->transfer_buffer + offset + bytes1,
07a40c2fc   Ricard Wanderlof   ALSA: USB-audio: ...
1393
1394
  		       runtime->dma_area, bytes - bytes1);
  	} else {
b97a93691   Ricard Wanderlof   ALSA: USB-audio: ...
1395
  		memcpy(urb->transfer_buffer + offset,
07a40c2fc   Ricard Wanderlof   ALSA: USB-audio: ...
1396
1397
1398
  		       runtime->dma_area + subs->hwptr_done, bytes);
  	}
  	subs->hwptr_done += bytes;
4c4e4391b   Ricard Wanderlof   ALSA: USB-audio: ...
1399
1400
  	if (subs->hwptr_done >= runtime->buffer_size * stride)
  		subs->hwptr_done -= runtime->buffer_size * stride;
07a40c2fc   Ricard Wanderlof   ALSA: USB-audio: ...
1401
  }
e05704467   Ricard Wanderlof   ALSA: USB-audio: ...
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
  static unsigned int copy_to_urb_quirk(struct snd_usb_substream *subs,
  				      struct urb *urb, int stride,
  				      unsigned int bytes)
  {
  	__le32 packet_length;
  	int i;
  
  	/* Put __le32 length descriptor at start of each packet. */
  	for (i = 0; i < urb->number_of_packets; i++) {
  		unsigned int length = urb->iso_frame_desc[i].length;
  		unsigned int offset = urb->iso_frame_desc[i].offset;
  
  		packet_length = cpu_to_le32(length);
  		offset += i * sizeof(packet_length);
  		urb->iso_frame_desc[i].offset = offset;
  		urb->iso_frame_desc[i].length += sizeof(packet_length);
  		memcpy(urb->transfer_buffer + offset,
  		       &packet_length, sizeof(packet_length));
  		copy_to_urb(subs, urb, offset + sizeof(packet_length),
  			    stride, length);
  	}
  	/* Adjust transfer size accordingly. */
  	bytes += urb->number_of_packets * sizeof(packet_length);
  	return bytes;
  }
edcd3633e   Daniel Mack   ALSA: snd-usb: sw...
1427
1428
1429
1430
  static void prepare_playback_urb(struct snd_usb_substream *subs,
  				 struct urb *urb)
  {
  	struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
245baf983   Daniel Mack   ALSA: snd-usb: fi...
1431
  	struct snd_usb_endpoint *ep = subs->data_endpoint;
edcd3633e   Daniel Mack   ALSA: snd-usb: sw...
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
  	struct snd_urb_ctx *ctx = urb->context;
  	unsigned int counts, frames, bytes;
  	int i, stride, period_elapsed = 0;
  	unsigned long flags;
  
  	stride = runtime->frame_bits >> 3;
  
  	frames = 0;
  	urb->number_of_packets = 0;
  	spin_lock_irqsave(&subs->lock, flags);
976b6c064   Alan Stern   ALSA: improve buf...
1442
  	subs->frame_limit += ep->max_urb_frames;
edcd3633e   Daniel Mack   ALSA: snd-usb: sw...
1443
  	for (i = 0; i < ctx->packets; i++) {
245baf983   Daniel Mack   ALSA: snd-usb: fi...
1444
1445
1446
1447
  		if (ctx->packet_size[i])
  			counts = ctx->packet_size[i];
  		else
  			counts = snd_usb_endpoint_next_packet_size(ep);
edcd3633e   Daniel Mack   ALSA: snd-usb: sw...
1448
  		/* set up descriptor */
8a2a74d2b   Daniel Mack   ALSA: snd-usb: us...
1449
1450
  		urb->iso_frame_desc[i].offset = frames * ep->stride;
  		urb->iso_frame_desc[i].length = counts * ep->stride;
edcd3633e   Daniel Mack   ALSA: snd-usb: sw...
1451
1452
1453
1454
1455
  		frames += counts;
  		urb->number_of_packets++;
  		subs->transfer_done += counts;
  		if (subs->transfer_done >= runtime->period_size) {
  			subs->transfer_done -= runtime->period_size;
976b6c064   Alan Stern   ALSA: improve buf...
1456
  			subs->frame_limit = 0;
edcd3633e   Daniel Mack   ALSA: snd-usb: sw...
1457
1458
1459
1460
1461
1462
1463
1464
  			period_elapsed = 1;
  			if (subs->fmt_type == UAC_FORMAT_TYPE_II) {
  				if (subs->transfer_done > 0) {
  					/* FIXME: fill-max mode is not
  					 * supported yet */
  					frames -= subs->transfer_done;
  					counts -= subs->transfer_done;
  					urb->iso_frame_desc[i].length =
8a2a74d2b   Daniel Mack   ALSA: snd-usb: us...
1465
  						counts * ep->stride;
edcd3633e   Daniel Mack   ALSA: snd-usb: sw...
1466
1467
1468
1469
1470
1471
  					subs->transfer_done = 0;
  				}
  				i++;
  				if (i < ctx->packets) {
  					/* add a transfer delimiter */
  					urb->iso_frame_desc[i].offset =
8a2a74d2b   Daniel Mack   ALSA: snd-usb: us...
1472
  						frames * ep->stride;
edcd3633e   Daniel Mack   ALSA: snd-usb: sw...
1473
1474
1475
1476
1477
1478
  					urb->iso_frame_desc[i].length = 0;
  					urb->number_of_packets++;
  				}
  				break;
  			}
  		}
976b6c064   Alan Stern   ALSA: improve buf...
1479
1480
1481
1482
  		/* finish at the period boundary or after enough frames */
  		if ((period_elapsed ||
  				subs->transfer_done >= subs->frame_limit) &&
  		    !snd_usb_endpoint_implicit_feedback_sink(ep))
edcd3633e   Daniel Mack   ALSA: snd-usb: sw...
1483
1484
  			break;
  	}
8a2a74d2b   Daniel Mack   ALSA: snd-usb: us...
1485
  	bytes = frames * ep->stride;
d24f5061e   Daniel Mack   ALSA: snd-usb: ad...
1486
1487
1488
1489
  
  	if (unlikely(subs->pcm_format == SNDRV_PCM_FORMAT_DSD_U16_LE &&
  		     subs->cur_audiofmt->dsd_dop)) {
  		fill_playback_urb_dsd_dop(subs, urb, bytes);
44dcbbb1c   Daniel Mack   ALSA: snd-usb: ad...
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
  	} else if (unlikely(subs->pcm_format == SNDRV_PCM_FORMAT_DSD_U8 &&
  			   subs->cur_audiofmt->dsd_bitrev)) {
  		/* bit-reverse the bytes */
  		u8 *buf = urb->transfer_buffer;
  		for (i = 0; i < bytes; i++) {
  			int idx = (subs->hwptr_done + i)
  				% (runtime->buffer_size * stride);
  			buf[i] = bitrev8(runtime->dma_area[idx]);
  		}
  
  		subs->hwptr_done += bytes;
4c4e4391b   Ricard Wanderlof   ALSA: USB-audio: ...
1501
1502
  		if (subs->hwptr_done >= runtime->buffer_size * stride)
  			subs->hwptr_done -= runtime->buffer_size * stride;
edcd3633e   Daniel Mack   ALSA: snd-usb: sw...
1503
  	} else {
d24f5061e   Daniel Mack   ALSA: snd-usb: ad...
1504
  		/* usual PCM */
e05704467   Ricard Wanderlof   ALSA: USB-audio: ...
1505
1506
1507
1508
1509
  		if (!subs->tx_length_quirk)
  			copy_to_urb(subs, urb, 0, stride, bytes);
  		else
  			bytes = copy_to_urb_quirk(subs, urb, stride, bytes);
  			/* bytes is now amount of outgoing data */
edcd3633e   Daniel Mack   ALSA: snd-usb: sw...
1510
  	}
d24f5061e   Daniel Mack   ALSA: snd-usb: ad...
1511

fbcfbf5f6   Daniel Mack   ALSA: snd-usb: re...
1512
1513
  	/* update delay with exact number of samples queued */
  	runtime->delay = subs->last_delay;
edcd3633e   Daniel Mack   ALSA: snd-usb: sw...
1514
  	runtime->delay += frames;
fbcfbf5f6   Daniel Mack   ALSA: snd-usb: re...
1515
1516
1517
1518
1519
  	subs->last_delay = runtime->delay;
  
  	/* realign last_frame_number */
  	subs->last_frame_number = usb_get_current_frame_number(subs->dev);
  	subs->last_frame_number &= 0xFF; /* keep 8 LSBs */
ea33d359c   Pierre-Louis Bossart   ALSA: usb: update...
1520
1521
1522
1523
1524
1525
1526
  	if (subs->trigger_tstamp_pending_update) {
  		/* this is the first actual URB submitted,
  		 * update trigger timestamp to reflect actual start time
  		 */
  		snd_pcm_gettime(runtime, &runtime->trigger_tstamp);
  		subs->trigger_tstamp_pending_update = false;
  	}
edcd3633e   Daniel Mack   ALSA: snd-usb: sw...
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
  	spin_unlock_irqrestore(&subs->lock, flags);
  	urb->transfer_buffer_length = bytes;
  	if (period_elapsed)
  		snd_pcm_period_elapsed(subs->pcm_substream);
  }
  
  /*
   * process after playback data complete
   * - decrease the delay count again
   */
  static void retire_playback_urb(struct snd_usb_substream *subs,
  			       struct urb *urb)
  {
  	unsigned long flags;
  	struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
8a2a74d2b   Daniel Mack   ALSA: snd-usb: us...
1542
1543
  	struct snd_usb_endpoint *ep = subs->data_endpoint;
  	int processed = urb->transfer_buffer_length / ep->stride;
fbcfbf5f6   Daniel Mack   ALSA: snd-usb: re...
1544
  	int est_delay;
edcd3633e   Daniel Mack   ALSA: snd-usb: sw...
1545

1213a205f   Takashi Iwai   ALSA: usb-audio: ...
1546
1547
1548
1549
1550
  	/* ignore the delay accounting when procssed=0 is given, i.e.
  	 * silent payloads are procssed before handling the actual data
  	 */
  	if (!processed)
  		return;
edcd3633e   Daniel Mack   ALSA: snd-usb: sw...
1551
  	spin_lock_irqsave(&subs->lock, flags);
48779a0b8   Takashi Iwai   ALSA: usb-audio: ...
1552
1553
  	if (!subs->last_delay)
  		goto out; /* short path */
fbcfbf5f6   Daniel Mack   ALSA: snd-usb: re...
1554
1555
1556
1557
  	est_delay = snd_usb_pcm_delay(subs, runtime->rate);
  	/* update delay with exact number of samples played */
  	if (processed > subs->last_delay)
  		subs->last_delay = 0;
edcd3633e   Daniel Mack   ALSA: snd-usb: sw...
1558
  	else
fbcfbf5f6   Daniel Mack   ALSA: snd-usb: re...
1559
1560
1561
1562
1563
1564
1565
1566
  		subs->last_delay -= processed;
  	runtime->delay = subs->last_delay;
  
  	/*
  	 * Report when delay estimate is off by more than 2ms.
  	 * The error should be lower than 2ms since the estimate relies
  	 * on two reads of a counter updated every ms.
  	 */
b7a772351   Sander Eikelenboom   ALSA: usb-audio: ...
1567
1568
  	if (abs(est_delay - subs->last_delay) * 1000 > runtime->rate * 2)
  		dev_dbg_ratelimited(&subs->dev->dev,
0ba41d917   Takashi Iwai   ALSA: usb-audio: ...
1569
1570
  			"delay: estimated %d, actual %d
  ",
fbcfbf5f6   Daniel Mack   ALSA: snd-usb: re...
1571
  			est_delay, subs->last_delay);
48779a0b8   Takashi Iwai   ALSA: usb-audio: ...
1572
1573
1574
1575
1576
1577
1578
1579
1580
  	if (!subs->running) {
  		/* update last_frame_number for delay counting here since
  		 * prepare_playback_urb won't be called during pause
  		 */
  		subs->last_frame_number =
  			usb_get_current_frame_number(subs->dev) & 0xff;
  	}
  
   out:
edcd3633e   Daniel Mack   ALSA: snd-usb: sw...
1581
  	spin_unlock_irqrestore(&subs->lock, flags);
e5779998b   Daniel Mack   ALSA: usb-audio: ...
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
  }
  
  static int snd_usb_playback_open(struct snd_pcm_substream *substream)
  {
  	return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_PLAYBACK);
  }
  
  static int snd_usb_playback_close(struct snd_pcm_substream *substream)
  {
  	return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_PLAYBACK);
  }
  
  static int snd_usb_capture_open(struct snd_pcm_substream *substream)
  {
  	return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_CAPTURE);
  }
  
  static int snd_usb_capture_close(struct snd_pcm_substream *substream)
  {
  	return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_CAPTURE);
  }
edcd3633e   Daniel Mack   ALSA: snd-usb: sw...
1603
1604
1605
1606
1607
1608
1609
  static int snd_usb_substream_playback_trigger(struct snd_pcm_substream *substream,
  					      int cmd)
  {
  	struct snd_usb_substream *subs = substream->runtime->private_data;
  
  	switch (cmd) {
  	case SNDRV_PCM_TRIGGER_START:
ea33d359c   Pierre-Louis Bossart   ALSA: usb: update...
1610
  		subs->trigger_tstamp_pending_update = true;
edcd3633e   Daniel Mack   ALSA: snd-usb: sw...
1611
1612
1613
  	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  		subs->data_endpoint->prepare_data_urb = prepare_playback_urb;
  		subs->data_endpoint->retire_data_urb = retire_playback_urb;
97f8d3b65   Daniel Mack   ALSA: snd-usb: fi...
1614
  		subs->running = 1;
edcd3633e   Daniel Mack   ALSA: snd-usb: sw...
1615
1616
  		return 0;
  	case SNDRV_PCM_TRIGGER_STOP:
a9bb36261   Takashi Iwai   ALSA: usb-audio: ...
1617
  		stop_endpoints(subs, false);
97f8d3b65   Daniel Mack   ALSA: snd-usb: fi...
1618
  		subs->running = 0;
edcd3633e   Daniel Mack   ALSA: snd-usb: sw...
1619
1620
1621
  		return 0;
  	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  		subs->data_endpoint->prepare_data_urb = NULL;
48779a0b8   Takashi Iwai   ALSA: usb-audio: ...
1622
1623
  		/* keep retire_data_urb for delay calculation */
  		subs->data_endpoint->retire_data_urb = retire_playback_urb;
97f8d3b65   Daniel Mack   ALSA: snd-usb: fi...
1624
  		subs->running = 0;
edcd3633e   Daniel Mack   ALSA: snd-usb: sw...
1625
1626
1627
1628
1629
  		return 0;
  	}
  
  	return -EINVAL;
  }
afe25967e   Daniel Mack   ALSA: snd-usb: ma...
1630
1631
  static int snd_usb_substream_capture_trigger(struct snd_pcm_substream *substream,
  					     int cmd)
edcd3633e   Daniel Mack   ALSA: snd-usb: sw...
1632
1633
1634
1635
1636
1637
  {
  	int err;
  	struct snd_usb_substream *subs = substream->runtime->private_data;
  
  	switch (cmd) {
  	case SNDRV_PCM_TRIGGER_START:
be4e3aec5   Ioan-Adrian Ratiu   ALSA: usb-audio: ...
1638
  		err = start_endpoints(subs);
edcd3633e   Daniel Mack   ALSA: snd-usb: sw...
1639
1640
1641
1642
  		if (err < 0)
  			return err;
  
  		subs->data_endpoint->retire_data_urb = retire_capture_urb;
97f8d3b65   Daniel Mack   ALSA: snd-usb: fi...
1643
  		subs->running = 1;
edcd3633e   Daniel Mack   ALSA: snd-usb: sw...
1644
1645
  		return 0;
  	case SNDRV_PCM_TRIGGER_STOP:
a9bb36261   Takashi Iwai   ALSA: usb-audio: ...
1646
  		stop_endpoints(subs, false);
97f8d3b65   Daniel Mack   ALSA: snd-usb: fi...
1647
  		subs->running = 0;
edcd3633e   Daniel Mack   ALSA: snd-usb: sw...
1648
1649
1650
  		return 0;
  	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  		subs->data_endpoint->retire_data_urb = NULL;
97f8d3b65   Daniel Mack   ALSA: snd-usb: fi...
1651
  		subs->running = 0;
edcd3633e   Daniel Mack   ALSA: snd-usb: sw...
1652
1653
1654
  		return 0;
  	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  		subs->data_endpoint->retire_data_urb = retire_capture_urb;
97f8d3b65   Daniel Mack   ALSA: snd-usb: fi...
1655
  		subs->running = 1;
edcd3633e   Daniel Mack   ALSA: snd-usb: sw...
1656
1657
1658
1659
1660
  		return 0;
  	}
  
  	return -EINVAL;
  }
e5779998b   Daniel Mack   ALSA: usb-audio: ...
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
  static struct snd_pcm_ops snd_usb_playback_ops = {
  	.open =		snd_usb_playback_open,
  	.close =	snd_usb_playback_close,
  	.ioctl =	snd_pcm_lib_ioctl,
  	.hw_params =	snd_usb_hw_params,
  	.hw_free =	snd_usb_hw_free,
  	.prepare =	snd_usb_pcm_prepare,
  	.trigger =	snd_usb_substream_playback_trigger,
  	.pointer =	snd_usb_pcm_pointer,
  	.page =		snd_pcm_lib_get_vmalloc_page,
  	.mmap =		snd_pcm_lib_mmap_vmalloc,
  };
  
  static struct snd_pcm_ops snd_usb_capture_ops = {
  	.open =		snd_usb_capture_open,
  	.close =	snd_usb_capture_close,
  	.ioctl =	snd_pcm_lib_ioctl,
  	.hw_params =	snd_usb_hw_params,
  	.hw_free =	snd_usb_hw_free,
  	.prepare =	snd_usb_pcm_prepare,
  	.trigger =	snd_usb_substream_capture_trigger,
  	.pointer =	snd_usb_pcm_pointer,
  	.page =		snd_pcm_lib_get_vmalloc_page,
  	.mmap =		snd_pcm_lib_mmap_vmalloc,
  };
  
  void snd_usb_set_pcm_ops(struct snd_pcm *pcm, int stream)
  {
  	snd_pcm_set_ops(pcm, stream,
  			stream == SNDRV_PCM_STREAM_PLAYBACK ?
  			&snd_usb_playback_ops : &snd_usb_capture_ops);
  }