blob: 54607f8c4f66ca91e5502808aa9fc94ceebc4d76 [file] [log] [blame]
Daniel Macke5779992010-03-04 19:46:13 +01001/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15 */
16
17#include <linux/init.h>
Stephen Rothwell9966dda2010-03-29 19:01:48 +110018#include <linux/slab.h>
Daniel Mackedcd3632012-04-12 13:51:12 +020019#include <linux/ratelimit.h>
Daniel Macke5779992010-03-04 19:46:13 +010020#include <linux/usb.h>
21#include <linux/usb/audio.h>
Daniel Mack7e847892010-03-11 21:13:20 +010022#include <linux/usb/audio-v2.h>
Daniel Macke5779992010-03-04 19:46:13 +010023
24#include <sound/core.h>
25#include <sound/pcm.h>
26#include <sound/pcm_params.h>
27
28#include "usbaudio.h"
29#include "card.h"
30#include "quirks.h"
31#include "debug.h"
Daniel Mackc731bc92011-09-14 12:46:57 +020032#include "endpoint.h"
Daniel Macke5779992010-03-04 19:46:13 +010033#include "helper.h"
34#include "pcm.h"
Daniel Mack79f920f2010-05-31 14:51:31 +020035#include "clock.h"
Oliver Neukum88a85162011-03-11 14:51:12 +010036#include "power.h"
Daniel Macke5779992010-03-04 19:46:13 +010037
Daniel Mackedcd3632012-04-12 13:51:12 +020038#define SUBSTREAM_FLAG_DATA_EP_STARTED 0
39#define SUBSTREAM_FLAG_SYNC_EP_STARTED 1
40
Pierre-Louis Bossart294c4fb2011-09-06 19:15:34 -050041/* return the estimated delay based on USB frame counters */
42snd_pcm_uframes_t snd_usb_pcm_delay(struct snd_usb_substream *subs,
43 unsigned int rate)
44{
45 int current_frame_number;
46 int frame_diff;
47 int est_delay;
48
49 current_frame_number = usb_get_current_frame_number(subs->dev);
50 /*
51 * HCD implementations use different widths, use lower 8 bits.
52 * The delay will be managed up to 256ms, which is more than
53 * enough
54 */
55 frame_diff = (current_frame_number - subs->last_frame_number) & 0xff;
56
57 /* Approximation based on number of samples per USB frame (ms),
58 some truncation for 44.1 but the estimate is good enough */
59 est_delay = subs->last_delay - (frame_diff * rate / 1000);
60 if (est_delay < 0)
61 est_delay = 0;
62 return est_delay;
63}
64
Daniel Macke5779992010-03-04 19:46:13 +010065/*
66 * return the current pcm pointer. just based on the hwptr_done value.
67 */
68static snd_pcm_uframes_t snd_usb_pcm_pointer(struct snd_pcm_substream *substream)
69{
70 struct snd_usb_substream *subs;
71 unsigned int hwptr_done;
72
73 subs = (struct snd_usb_substream *)substream->runtime->private_data;
74 spin_lock(&subs->lock);
75 hwptr_done = subs->hwptr_done;
Pierre-Louis Bossart294c4fb2011-09-06 19:15:34 -050076 substream->runtime->delay = snd_usb_pcm_delay(subs,
77 substream->runtime->rate);
Daniel Macke5779992010-03-04 19:46:13 +010078 spin_unlock(&subs->lock);
79 return hwptr_done / (substream->runtime->frame_bits >> 3);
80}
81
82/*
83 * find a matching audio format
84 */
85static struct audioformat *find_format(struct snd_usb_substream *subs, unsigned int format,
86 unsigned int rate, unsigned int channels)
87{
88 struct list_head *p;
89 struct audioformat *found = NULL;
90 int cur_attr = 0, attr;
91
92 list_for_each(p, &subs->fmt_list) {
93 struct audioformat *fp;
94 fp = list_entry(p, struct audioformat, list);
Clemens Ladisch015eb0b2010-03-04 19:46:15 +010095 if (!(fp->formats & (1uLL << format)))
96 continue;
97 if (fp->channels != channels)
Daniel Macke5779992010-03-04 19:46:13 +010098 continue;
99 if (rate < fp->rate_min || rate > fp->rate_max)
100 continue;
101 if (! (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)) {
102 unsigned int i;
103 for (i = 0; i < fp->nr_rates; i++)
104 if (fp->rate_table[i] == rate)
105 break;
106 if (i >= fp->nr_rates)
107 continue;
108 }
109 attr = fp->ep_attr & USB_ENDPOINT_SYNCTYPE;
110 if (! found) {
111 found = fp;
112 cur_attr = attr;
113 continue;
114 }
115 /* avoid async out and adaptive in if the other method
116 * supports the same format.
117 * this is a workaround for the case like
118 * M-audio audiophile USB.
119 */
120 if (attr != cur_attr) {
121 if ((attr == USB_ENDPOINT_SYNC_ASYNC &&
122 subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
123 (attr == USB_ENDPOINT_SYNC_ADAPTIVE &&
124 subs->direction == SNDRV_PCM_STREAM_CAPTURE))
125 continue;
126 if ((cur_attr == USB_ENDPOINT_SYNC_ASYNC &&
127 subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
128 (cur_attr == USB_ENDPOINT_SYNC_ADAPTIVE &&
129 subs->direction == SNDRV_PCM_STREAM_CAPTURE)) {
130 found = fp;
131 cur_attr = attr;
132 continue;
133 }
134 }
135 /* find the format with the largest max. packet size */
136 if (fp->maxpacksize > found->maxpacksize) {
137 found = fp;
138 cur_attr = attr;
139 }
140 }
141 return found;
142}
143
Daniel Mack767d75a2010-03-04 19:46:17 +0100144static int init_pitch_v1(struct snd_usb_audio *chip, int iface,
145 struct usb_host_interface *alts,
146 struct audioformat *fmt)
Daniel Macke5779992010-03-04 19:46:13 +0100147{
Daniel Mack767d75a2010-03-04 19:46:17 +0100148 struct usb_device *dev = chip->dev;
Daniel Macke5779992010-03-04 19:46:13 +0100149 unsigned int ep;
150 unsigned char data[1];
151 int err;
152
153 ep = get_endpoint(alts, 0)->bEndpointAddress;
Daniel Mack767d75a2010-03-04 19:46:17 +0100154
Daniel Mack767d75a2010-03-04 19:46:17 +0100155 data[0] = 1;
156 if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC_SET_CUR,
157 USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT,
158 UAC_EP_CS_ATTR_PITCH_CONTROL << 8, ep,
Clemens Ladisch17d900c2011-09-26 21:15:27 +0200159 data, sizeof(data))) < 0) {
Daniel Mack767d75a2010-03-04 19:46:17 +0100160 snd_printk(KERN_ERR "%d:%d:%d: cannot set enable PITCH\n",
161 dev->devnum, iface, ep);
162 return err;
Daniel Macke5779992010-03-04 19:46:13 +0100163 }
Daniel Mack767d75a2010-03-04 19:46:17 +0100164
Daniel Macke5779992010-03-04 19:46:13 +0100165 return 0;
166}
167
Daniel Mack92c25612010-05-26 18:11:39 +0200168static int init_pitch_v2(struct snd_usb_audio *chip, int iface,
169 struct usb_host_interface *alts,
170 struct audioformat *fmt)
171{
172 struct usb_device *dev = chip->dev;
173 unsigned char data[1];
174 unsigned int ep;
175 int err;
176
177 ep = get_endpoint(alts, 0)->bEndpointAddress;
178
179 data[0] = 1;
180 if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC2_CS_CUR,
181 USB_TYPE_CLASS | USB_RECIP_ENDPOINT | USB_DIR_OUT,
182 UAC2_EP_CS_PITCH << 8, 0,
Clemens Ladisch17d900c2011-09-26 21:15:27 +0200183 data, sizeof(data))) < 0) {
Daniel Mack92c25612010-05-26 18:11:39 +0200184 snd_printk(KERN_ERR "%d:%d:%d: cannot set enable PITCH (v2)\n",
185 dev->devnum, iface, fmt->altsetting);
186 return err;
187 }
188
189 return 0;
190}
191
Daniel Mack767d75a2010-03-04 19:46:17 +0100192/*
Daniel Mack92c25612010-05-26 18:11:39 +0200193 * initialize the pitch control and sample rate
Daniel Mack767d75a2010-03-04 19:46:17 +0100194 */
195int snd_usb_init_pitch(struct snd_usb_audio *chip, int iface,
196 struct usb_host_interface *alts,
197 struct audioformat *fmt)
198{
199 struct usb_interface_descriptor *altsd = get_iface_desc(alts);
200
Daniel Mack92c25612010-05-26 18:11:39 +0200201 /* if endpoint doesn't have pitch control, bail out */
202 if (!(fmt->attributes & UAC_EP_CS_ATTR_PITCH_CONTROL))
203 return 0;
204
Daniel Mack767d75a2010-03-04 19:46:17 +0100205 switch (altsd->bInterfaceProtocol) {
206 case UAC_VERSION_1:
Clemens Ladischa2acad82010-09-03 10:53:11 +0200207 default:
Daniel Mack767d75a2010-03-04 19:46:17 +0100208 return init_pitch_v1(chip, iface, alts, fmt);
209
210 case UAC_VERSION_2:
Daniel Mack92c25612010-05-26 18:11:39 +0200211 return init_pitch_v2(chip, iface, alts, fmt);
Daniel Mack767d75a2010-03-04 19:46:17 +0100212 }
Daniel Mack767d75a2010-03-04 19:46:17 +0100213}
214
Daniel Mackedcd3632012-04-12 13:51:12 +0200215static 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
Daniel Macke5779992010-03-04 19:46:13 +0100293/*
294 * find a matching format and set up the interface
295 */
296static int set_format(struct snd_usb_substream *subs, struct audioformat *fmt)
297{
298 struct usb_device *dev = subs->dev;
299 struct usb_host_interface *alts;
300 struct usb_interface_descriptor *altsd;
301 struct usb_interface *iface;
302 unsigned int ep, attr;
303 int is_playback = subs->direction == SNDRV_PCM_STREAM_PLAYBACK;
Daniel Mackc75a8a72012-04-12 13:51:14 +0200304 int err, implicit_fb = 0;
Daniel Macke5779992010-03-04 19:46:13 +0100305
306 iface = usb_ifnum_to_if(dev, fmt->iface);
307 if (WARN_ON(!iface))
308 return -EINVAL;
309 alts = &iface->altsetting[fmt->altset_idx];
310 altsd = get_iface_desc(alts);
311 if (WARN_ON(altsd->bAlternateSetting != fmt->altsetting))
312 return -EINVAL;
313
314 if (fmt == subs->cur_audiofmt)
315 return 0;
316
Daniel Mackedcd3632012-04-12 13:51:12 +0200317 subs->data_endpoint = snd_usb_add_endpoint(subs->stream->chip,
318 alts, fmt->endpoint, subs->direction,
319 SND_USB_ENDPOINT_TYPE_DATA);
320 if (!subs->data_endpoint)
321 return -EINVAL;
Daniel Macke5779992010-03-04 19:46:13 +0100322
323 /* we need a sync pipe in async OUT or adaptive IN mode */
324 /* check the number of EP, since some devices have broken
325 * descriptors which fool us. if it has only one EP,
326 * assume it as adaptive-out or sync-in.
327 */
328 attr = fmt->ep_attr & USB_ENDPOINT_SYNCTYPE;
Daniel Mackc75a8a72012-04-12 13:51:14 +0200329
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;
Daniel Mackedcd3632012-04-12 13:51:12 +0200335 ep = 0x81;
336 iface = usb_ifnum_to_if(dev, 2);
Daniel Mackc75a8a72012-04-12 13:51:14 +0200337
338 if (!iface || iface->num_altsetting == 0)
339 return -EINVAL;
340
Daniel Mackedcd3632012-04-12 13:51:12 +0200341 alts = &iface->altsetting[1];
342 goto add_sync_ep;
343 }
Daniel Mackc75a8a72012-04-12 13:51:14 +0200344 }
Daniel Mackedcd3632012-04-12 13:51:12 +0200345
Daniel Mackc75a8a72012-04-12 13:51:14 +0200346 if (((is_playback && attr == USB_ENDPOINT_SYNC_ASYNC) ||
347 (!is_playback && attr == USB_ENDPOINT_SYNC_ADAPTIVE)) &&
348 altsd->bNumEndpoints >= 2) {
Daniel Macke5779992010-03-04 19:46:13 +0100349 /* check sync-pipe endpoint */
350 /* ... and check descriptor size before accessing bSynchAddress
351 because there is a version of the SB Audigy 2 NX firmware lacking
352 the audio fields in the endpoint descriptors */
353 if ((get_endpoint(alts, 1)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != 0x01 ||
354 (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
Daniel Mackc75a8a72012-04-12 13:51:14 +0200355 get_endpoint(alts, 1)->bSynchAddress != 0 &&
356 !implicit_fb)) {
Daniel Mack7fb75db2012-06-17 13:44:27 +0200357 snd_printk(KERN_ERR "%d:%d:%d : invalid sync pipe. bmAttributes %02x, bLength %d, bSynchAddress %02x\n",
358 dev->devnum, fmt->iface, fmt->altsetting,
359 get_endpoint(alts, 1)->bmAttributes,
360 get_endpoint(alts, 1)->bLength,
361 get_endpoint(alts, 1)->bSynchAddress);
Daniel Macke5779992010-03-04 19:46:13 +0100362 return -EINVAL;
363 }
364 ep = get_endpoint(alts, 1)->bEndpointAddress;
Daniel Mack7fb75db2012-06-17 13:44:27 +0200365 if (!implicit_fb &&
366 get_endpoint(alts, 0)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
Daniel Macke5779992010-03-04 19:46:13 +0100367 (( is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress | USB_DIR_IN)) ||
Daniel Mack7fb75db2012-06-17 13:44:27 +0200368 (!is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress & ~USB_DIR_IN)))) {
369 snd_printk(KERN_ERR "%d:%d:%d : invalid sync pipe. is_playback %d, ep %02x, bSynchAddress %02x\n",
370 dev->devnum, fmt->iface, fmt->altsetting,
371 is_playback, ep, get_endpoint(alts, 0)->bSynchAddress);
Daniel Macke5779992010-03-04 19:46:13 +0100372 return -EINVAL;
373 }
Daniel Mackc75a8a72012-04-12 13:51:14 +0200374
375 implicit_fb = (get_endpoint(alts, 1)->bmAttributes & USB_ENDPOINT_USAGE_MASK)
376 == USB_ENDPOINT_USAGE_IMPLICIT_FB;
377
Daniel Mackedcd3632012-04-12 13:51:12 +0200378add_sync_ep:
379 subs->sync_endpoint = snd_usb_add_endpoint(subs->stream->chip,
380 alts, ep, !subs->direction,
Daniel Mackc75a8a72012-04-12 13:51:14 +0200381 implicit_fb ?
382 SND_USB_ENDPOINT_TYPE_DATA :
383 SND_USB_ENDPOINT_TYPE_SYNC);
Daniel Mackedcd3632012-04-12 13:51:12 +0200384 if (!subs->sync_endpoint)
385 return -EINVAL;
386
387 subs->data_endpoint->sync_master = subs->sync_endpoint;
388 }
Daniel Macke5779992010-03-04 19:46:13 +0100389
Daniel Mack767d75a2010-03-04 19:46:17 +0100390 if ((err = snd_usb_init_pitch(subs->stream->chip, subs->interface, alts, fmt)) < 0)
Daniel Macke5779992010-03-04 19:46:13 +0100391 return err;
392
393 subs->cur_audiofmt = fmt;
394
395 snd_usb_set_format_quirk(subs, fmt);
396
397#if 0
398 printk(KERN_DEBUG
399 "setting done: format = %d, rate = %d..%d, channels = %d\n",
400 fmt->format, fmt->rate_min, fmt->rate_max, fmt->channels);
401 printk(KERN_DEBUG
402 " datapipe = 0x%0x, syncpipe = 0x%0x\n",
403 subs->datapipe, subs->syncpipe);
404#endif
405
406 return 0;
407}
408
409/*
410 * hw_params callback
411 *
412 * allocate a buffer and set the given audio format.
413 *
414 * so far we use a physically linear buffer although packetize transfer
415 * doesn't need a continuous area.
416 * if sg buffer is supported on the later version of alsa, we'll follow
417 * that.
418 */
419static int snd_usb_hw_params(struct snd_pcm_substream *substream,
420 struct snd_pcm_hw_params *hw_params)
421{
422 struct snd_usb_substream *subs = substream->runtime->private_data;
423 struct audioformat *fmt;
424 unsigned int channels, rate, format;
425 int ret, changed;
426
427 ret = snd_pcm_lib_alloc_vmalloc_buffer(substream,
428 params_buffer_bytes(hw_params));
429 if (ret < 0)
430 return ret;
431
432 format = params_format(hw_params);
433 rate = params_rate(hw_params);
434 channels = params_channels(hw_params);
435 fmt = find_format(subs, format, rate, channels);
436 if (!fmt) {
437 snd_printd(KERN_DEBUG "cannot set format: format = %#x, rate = %d, channels = %d\n",
438 format, rate, channels);
439 return -EINVAL;
440 }
441
442 changed = subs->cur_audiofmt != fmt ||
443 subs->period_bytes != params_period_bytes(hw_params) ||
444 subs->cur_rate != rate;
445 if ((ret = set_format(subs, fmt)) < 0)
446 return ret;
447
448 if (subs->cur_rate != rate) {
449 struct usb_host_interface *alts;
450 struct usb_interface *iface;
451 iface = usb_ifnum_to_if(subs->dev, fmt->iface);
452 alts = &iface->altsetting[fmt->altset_idx];
Daniel Mack767d75a2010-03-04 19:46:17 +0100453 ret = snd_usb_init_sample_rate(subs->stream->chip, subs->interface, alts, fmt, rate);
Daniel Macke5779992010-03-04 19:46:13 +0100454 if (ret < 0)
455 return ret;
456 subs->cur_rate = rate;
457 }
458
459 if (changed) {
Takashi Iwai382225e2011-02-22 10:21:18 +0100460 mutex_lock(&subs->stream->chip->shutdown_mutex);
Daniel Macke5779992010-03-04 19:46:13 +0100461 /* format changed */
Daniel Mackedcd3632012-04-12 13:51:12 +0200462 stop_endpoints(subs, 0, 0, 0);
463 deactivate_endpoints(subs);
464
465 ret = activate_endpoints(subs);
466 if (ret < 0)
467 goto unlock;
468
469 ret = snd_usb_endpoint_set_params(subs->data_endpoint, hw_params, fmt,
470 subs->sync_endpoint);
471 if (ret < 0)
472 goto unlock;
473
474 if (subs->sync_endpoint)
475 ret = snd_usb_endpoint_set_params(subs->sync_endpoint,
476 hw_params, fmt, NULL);
477unlock:
Takashi Iwai382225e2011-02-22 10:21:18 +0100478 mutex_unlock(&subs->stream->chip->shutdown_mutex);
Daniel Macke5779992010-03-04 19:46:13 +0100479 }
480
Daniel Mack97f8d3b2012-05-21 12:47:36 +0200481 if (ret == 0) {
482 subs->interface = fmt->iface;
483 subs->altset_idx = fmt->altset_idx;
484 }
485
Daniel Macke5779992010-03-04 19:46:13 +0100486 return ret;
487}
488
489/*
490 * hw_free callback
491 *
492 * reset the audio format and release the buffer
493 */
494static int snd_usb_hw_free(struct snd_pcm_substream *substream)
495{
496 struct snd_usb_substream *subs = substream->runtime->private_data;
497
498 subs->cur_audiofmt = NULL;
499 subs->cur_rate = 0;
500 subs->period_bytes = 0;
Takashi Iwai382225e2011-02-22 10:21:18 +0100501 mutex_lock(&subs->stream->chip->shutdown_mutex);
Daniel Mackedcd3632012-04-12 13:51:12 +0200502 stop_endpoints(subs, 0, 1, 1);
Takashi Iwai382225e2011-02-22 10:21:18 +0100503 mutex_unlock(&subs->stream->chip->shutdown_mutex);
Daniel Macke5779992010-03-04 19:46:13 +0100504 return snd_pcm_lib_free_vmalloc_buffer(substream);
505}
506
507/*
508 * prepare callback
509 *
510 * only a few subtle things...
511 */
512static int snd_usb_pcm_prepare(struct snd_pcm_substream *substream)
513{
514 struct snd_pcm_runtime *runtime = substream->runtime;
515 struct snd_usb_substream *subs = runtime->private_data;
516
517 if (! subs->cur_audiofmt) {
518 snd_printk(KERN_ERR "usbaudio: no format is specified!\n");
519 return -ENXIO;
520 }
521
Daniel Mackedcd3632012-04-12 13:51:12 +0200522 if (snd_BUG_ON(!subs->data_endpoint))
523 return -EIO;
524
Daniel Macke5779992010-03-04 19:46:13 +0100525 /* some unit conversions in runtime */
Daniel Mackedcd3632012-04-12 13:51:12 +0200526 subs->data_endpoint->maxframesize =
527 bytes_to_frames(runtime, subs->data_endpoint->maxpacksize);
528 subs->data_endpoint->curframesize =
529 bytes_to_frames(runtime, subs->data_endpoint->curpacksize);
Daniel Macke5779992010-03-04 19:46:13 +0100530
531 /* reset the pointer */
532 subs->hwptr_done = 0;
533 subs->transfer_done = 0;
Pierre-Louis Bossart294c4fb2011-09-06 19:15:34 -0500534 subs->last_delay = 0;
535 subs->last_frame_number = 0;
Daniel Macke5779992010-03-04 19:46:13 +0100536 runtime->delay = 0;
537
Daniel Mackedcd3632012-04-12 13:51:12 +0200538 /* for playback, submit the URBs now; otherwise, the first hwptr_done
539 * updates for all URBs would happen at the same time when starting */
540 if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK)
541 return start_endpoints(subs);
542
543 return 0;
Daniel Macke5779992010-03-04 19:46:13 +0100544}
545
546static struct snd_pcm_hardware snd_usb_hardware =
547{
548 .info = SNDRV_PCM_INFO_MMAP |
549 SNDRV_PCM_INFO_MMAP_VALID |
550 SNDRV_PCM_INFO_BATCH |
551 SNDRV_PCM_INFO_INTERLEAVED |
552 SNDRV_PCM_INFO_BLOCK_TRANSFER |
553 SNDRV_PCM_INFO_PAUSE,
554 .buffer_bytes_max = 1024 * 1024,
555 .period_bytes_min = 64,
556 .period_bytes_max = 512 * 1024,
557 .periods_min = 2,
558 .periods_max = 1024,
559};
560
561static int hw_check_valid_format(struct snd_usb_substream *subs,
562 struct snd_pcm_hw_params *params,
563 struct audioformat *fp)
564{
565 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
566 struct snd_interval *ct = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
567 struct snd_mask *fmts = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
568 struct snd_interval *pt = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
Clemens Ladisch015eb0b2010-03-04 19:46:15 +0100569 struct snd_mask check_fmts;
Daniel Macke5779992010-03-04 19:46:13 +0100570 unsigned int ptime;
571
572 /* check the format */
Clemens Ladisch015eb0b2010-03-04 19:46:15 +0100573 snd_mask_none(&check_fmts);
574 check_fmts.bits[0] = (u32)fp->formats;
575 check_fmts.bits[1] = (u32)(fp->formats >> 32);
576 snd_mask_intersect(&check_fmts, fmts);
577 if (snd_mask_empty(&check_fmts)) {
Daniel Macke5779992010-03-04 19:46:13 +0100578 hwc_debug(" > check: no supported format %d\n", fp->format);
579 return 0;
580 }
581 /* check the channels */
582 if (fp->channels < ct->min || fp->channels > ct->max) {
583 hwc_debug(" > check: no valid channels %d (%d/%d)\n", fp->channels, ct->min, ct->max);
584 return 0;
585 }
586 /* check the rate is within the range */
587 if (fp->rate_min > it->max || (fp->rate_min == it->max && it->openmax)) {
588 hwc_debug(" > check: rate_min %d > max %d\n", fp->rate_min, it->max);
589 return 0;
590 }
591 if (fp->rate_max < it->min || (fp->rate_max == it->min && it->openmin)) {
592 hwc_debug(" > check: rate_max %d < min %d\n", fp->rate_max, it->min);
593 return 0;
594 }
595 /* check whether the period time is >= the data packet interval */
Paul Zimmerman4f4e8f62010-08-13 12:42:07 -0700596 if (snd_usb_get_speed(subs->dev) != USB_SPEED_FULL) {
Daniel Macke5779992010-03-04 19:46:13 +0100597 ptime = 125 * (1 << fp->datainterval);
598 if (ptime > pt->max || (ptime == pt->max && pt->openmax)) {
599 hwc_debug(" > check: ptime %u > max %u\n", ptime, pt->max);
600 return 0;
601 }
602 }
603 return 1;
604}
605
606static int hw_rule_rate(struct snd_pcm_hw_params *params,
607 struct snd_pcm_hw_rule *rule)
608{
609 struct snd_usb_substream *subs = rule->private;
610 struct list_head *p;
611 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
612 unsigned int rmin, rmax;
613 int changed;
614
615 hwc_debug("hw_rule_rate: (%d,%d)\n", it->min, it->max);
616 changed = 0;
617 rmin = rmax = 0;
618 list_for_each(p, &subs->fmt_list) {
619 struct audioformat *fp;
620 fp = list_entry(p, struct audioformat, list);
621 if (!hw_check_valid_format(subs, params, fp))
622 continue;
623 if (changed++) {
624 if (rmin > fp->rate_min)
625 rmin = fp->rate_min;
626 if (rmax < fp->rate_max)
627 rmax = fp->rate_max;
628 } else {
629 rmin = fp->rate_min;
630 rmax = fp->rate_max;
631 }
632 }
633
634 if (!changed) {
635 hwc_debug(" --> get empty\n");
636 it->empty = 1;
637 return -EINVAL;
638 }
639
640 changed = 0;
641 if (it->min < rmin) {
642 it->min = rmin;
643 it->openmin = 0;
644 changed = 1;
645 }
646 if (it->max > rmax) {
647 it->max = rmax;
648 it->openmax = 0;
649 changed = 1;
650 }
651 if (snd_interval_checkempty(it)) {
652 it->empty = 1;
653 return -EINVAL;
654 }
655 hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
656 return changed;
657}
658
659
660static int hw_rule_channels(struct snd_pcm_hw_params *params,
661 struct snd_pcm_hw_rule *rule)
662{
663 struct snd_usb_substream *subs = rule->private;
664 struct list_head *p;
665 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
666 unsigned int rmin, rmax;
667 int changed;
668
669 hwc_debug("hw_rule_channels: (%d,%d)\n", it->min, it->max);
670 changed = 0;
671 rmin = rmax = 0;
672 list_for_each(p, &subs->fmt_list) {
673 struct audioformat *fp;
674 fp = list_entry(p, struct audioformat, list);
675 if (!hw_check_valid_format(subs, params, fp))
676 continue;
677 if (changed++) {
678 if (rmin > fp->channels)
679 rmin = fp->channels;
680 if (rmax < fp->channels)
681 rmax = fp->channels;
682 } else {
683 rmin = fp->channels;
684 rmax = fp->channels;
685 }
686 }
687
688 if (!changed) {
689 hwc_debug(" --> get empty\n");
690 it->empty = 1;
691 return -EINVAL;
692 }
693
694 changed = 0;
695 if (it->min < rmin) {
696 it->min = rmin;
697 it->openmin = 0;
698 changed = 1;
699 }
700 if (it->max > rmax) {
701 it->max = rmax;
702 it->openmax = 0;
703 changed = 1;
704 }
705 if (snd_interval_checkempty(it)) {
706 it->empty = 1;
707 return -EINVAL;
708 }
709 hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
710 return changed;
711}
712
713static int hw_rule_format(struct snd_pcm_hw_params *params,
714 struct snd_pcm_hw_rule *rule)
715{
716 struct snd_usb_substream *subs = rule->private;
717 struct list_head *p;
718 struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
719 u64 fbits;
720 u32 oldbits[2];
721 int changed;
722
723 hwc_debug("hw_rule_format: %x:%x\n", fmt->bits[0], fmt->bits[1]);
724 fbits = 0;
725 list_for_each(p, &subs->fmt_list) {
726 struct audioformat *fp;
727 fp = list_entry(p, struct audioformat, list);
728 if (!hw_check_valid_format(subs, params, fp))
729 continue;
Clemens Ladisch015eb0b2010-03-04 19:46:15 +0100730 fbits |= fp->formats;
Daniel Macke5779992010-03-04 19:46:13 +0100731 }
732
733 oldbits[0] = fmt->bits[0];
734 oldbits[1] = fmt->bits[1];
735 fmt->bits[0] &= (u32)fbits;
736 fmt->bits[1] &= (u32)(fbits >> 32);
737 if (!fmt->bits[0] && !fmt->bits[1]) {
738 hwc_debug(" --> get empty\n");
739 return -EINVAL;
740 }
741 changed = (oldbits[0] != fmt->bits[0] || oldbits[1] != fmt->bits[1]);
742 hwc_debug(" --> %x:%x (changed = %d)\n", fmt->bits[0], fmt->bits[1], changed);
743 return changed;
744}
745
746static int hw_rule_period_time(struct snd_pcm_hw_params *params,
747 struct snd_pcm_hw_rule *rule)
748{
749 struct snd_usb_substream *subs = rule->private;
750 struct audioformat *fp;
751 struct snd_interval *it;
752 unsigned char min_datainterval;
753 unsigned int pmin;
754 int changed;
755
756 it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
757 hwc_debug("hw_rule_period_time: (%u,%u)\n", it->min, it->max);
758 min_datainterval = 0xff;
759 list_for_each_entry(fp, &subs->fmt_list, list) {
760 if (!hw_check_valid_format(subs, params, fp))
761 continue;
762 min_datainterval = min(min_datainterval, fp->datainterval);
763 }
764 if (min_datainterval == 0xff) {
Uwe Kleine-Königa7ce2e02010-07-12 17:15:44 +0200765 hwc_debug(" --> get empty\n");
Daniel Macke5779992010-03-04 19:46:13 +0100766 it->empty = 1;
767 return -EINVAL;
768 }
769 pmin = 125 * (1 << min_datainterval);
770 changed = 0;
771 if (it->min < pmin) {
772 it->min = pmin;
773 it->openmin = 0;
774 changed = 1;
775 }
776 if (snd_interval_checkempty(it)) {
777 it->empty = 1;
778 return -EINVAL;
779 }
780 hwc_debug(" --> (%u,%u) (changed = %d)\n", it->min, it->max, changed);
781 return changed;
782}
783
784/*
785 * If the device supports unusual bit rates, does the request meet these?
786 */
787static int snd_usb_pcm_check_knot(struct snd_pcm_runtime *runtime,
788 struct snd_usb_substream *subs)
789{
790 struct audioformat *fp;
Takashi Iwai0717d0f2012-03-15 16:14:38 +0100791 int *rate_list;
Daniel Macke5779992010-03-04 19:46:13 +0100792 int count = 0, needs_knot = 0;
793 int err;
794
Clemens Ladisch5cd5d7c2012-05-18 18:00:43 +0200795 kfree(subs->rate_list.list);
796 subs->rate_list.list = NULL;
797
Daniel Macke5779992010-03-04 19:46:13 +0100798 list_for_each_entry(fp, &subs->fmt_list, list) {
799 if (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)
800 return 0;
801 count += fp->nr_rates;
802 if (fp->rates & SNDRV_PCM_RATE_KNOT)
803 needs_knot = 1;
804 }
805 if (!needs_knot)
806 return 0;
807
Takashi Iwai0717d0f2012-03-15 16:14:38 +0100808 subs->rate_list.list = rate_list =
809 kmalloc(sizeof(int) * count, GFP_KERNEL);
Jesper Juhl8a8d56b2010-10-29 20:40:23 +0200810 if (!subs->rate_list.list)
811 return -ENOMEM;
812 subs->rate_list.count = count;
Daniel Macke5779992010-03-04 19:46:13 +0100813 subs->rate_list.mask = 0;
814 count = 0;
815 list_for_each_entry(fp, &subs->fmt_list, list) {
816 int i;
817 for (i = 0; i < fp->nr_rates; i++)
Takashi Iwai0717d0f2012-03-15 16:14:38 +0100818 rate_list[count++] = fp->rate_table[i];
Daniel Macke5779992010-03-04 19:46:13 +0100819 }
820 err = snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
821 &subs->rate_list);
822 if (err < 0)
823 return err;
824
825 return 0;
826}
827
828
829/*
830 * set up the runtime hardware information.
831 */
832
833static int setup_hw_info(struct snd_pcm_runtime *runtime, struct snd_usb_substream *subs)
834{
835 struct list_head *p;
836 unsigned int pt, ptmin;
837 int param_period_time_if_needed;
838 int err;
839
840 runtime->hw.formats = subs->formats;
841
842 runtime->hw.rate_min = 0x7fffffff;
843 runtime->hw.rate_max = 0;
844 runtime->hw.channels_min = 256;
845 runtime->hw.channels_max = 0;
846 runtime->hw.rates = 0;
847 ptmin = UINT_MAX;
848 /* check min/max rates and channels */
849 list_for_each(p, &subs->fmt_list) {
850 struct audioformat *fp;
851 fp = list_entry(p, struct audioformat, list);
852 runtime->hw.rates |= fp->rates;
853 if (runtime->hw.rate_min > fp->rate_min)
854 runtime->hw.rate_min = fp->rate_min;
855 if (runtime->hw.rate_max < fp->rate_max)
856 runtime->hw.rate_max = fp->rate_max;
857 if (runtime->hw.channels_min > fp->channels)
858 runtime->hw.channels_min = fp->channels;
859 if (runtime->hw.channels_max < fp->channels)
860 runtime->hw.channels_max = fp->channels;
861 if (fp->fmt_type == UAC_FORMAT_TYPE_II && fp->frame_size > 0) {
862 /* FIXME: there might be more than one audio formats... */
863 runtime->hw.period_bytes_min = runtime->hw.period_bytes_max =
864 fp->frame_size;
865 }
866 pt = 125 * (1 << fp->datainterval);
867 ptmin = min(ptmin, pt);
868 }
Oliver Neukum88a85162011-03-11 14:51:12 +0100869 err = snd_usb_autoresume(subs->stream->chip);
870 if (err < 0)
871 return err;
Daniel Macke5779992010-03-04 19:46:13 +0100872
873 param_period_time_if_needed = SNDRV_PCM_HW_PARAM_PERIOD_TIME;
Paul Zimmerman4f4e8f62010-08-13 12:42:07 -0700874 if (snd_usb_get_speed(subs->dev) == USB_SPEED_FULL)
Daniel Macke5779992010-03-04 19:46:13 +0100875 /* full speed devices have fixed data packet interval */
876 ptmin = 1000;
877 if (ptmin == 1000)
878 /* if period time doesn't go below 1 ms, no rules needed */
879 param_period_time_if_needed = -1;
880 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
881 ptmin, UINT_MAX);
882
883 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
884 hw_rule_rate, subs,
885 SNDRV_PCM_HW_PARAM_FORMAT,
886 SNDRV_PCM_HW_PARAM_CHANNELS,
887 param_period_time_if_needed,
888 -1)) < 0)
Oliver Neukum88a85162011-03-11 14:51:12 +0100889 goto rep_err;
Daniel Macke5779992010-03-04 19:46:13 +0100890 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
891 hw_rule_channels, subs,
892 SNDRV_PCM_HW_PARAM_FORMAT,
893 SNDRV_PCM_HW_PARAM_RATE,
894 param_period_time_if_needed,
895 -1)) < 0)
Oliver Neukum88a85162011-03-11 14:51:12 +0100896 goto rep_err;
Daniel Macke5779992010-03-04 19:46:13 +0100897 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
898 hw_rule_format, subs,
899 SNDRV_PCM_HW_PARAM_RATE,
900 SNDRV_PCM_HW_PARAM_CHANNELS,
901 param_period_time_if_needed,
902 -1)) < 0)
Oliver Neukum88a85162011-03-11 14:51:12 +0100903 goto rep_err;
Daniel Macke5779992010-03-04 19:46:13 +0100904 if (param_period_time_if_needed >= 0) {
905 err = snd_pcm_hw_rule_add(runtime, 0,
906 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
907 hw_rule_period_time, subs,
908 SNDRV_PCM_HW_PARAM_FORMAT,
909 SNDRV_PCM_HW_PARAM_CHANNELS,
910 SNDRV_PCM_HW_PARAM_RATE,
911 -1);
912 if (err < 0)
Oliver Neukum88a85162011-03-11 14:51:12 +0100913 goto rep_err;
Daniel Macke5779992010-03-04 19:46:13 +0100914 }
915 if ((err = snd_usb_pcm_check_knot(runtime, subs)) < 0)
Oliver Neukum88a85162011-03-11 14:51:12 +0100916 goto rep_err;
Daniel Macke5779992010-03-04 19:46:13 +0100917 return 0;
Oliver Neukum88a85162011-03-11 14:51:12 +0100918
919rep_err:
920 snd_usb_autosuspend(subs->stream->chip);
921 return err;
Daniel Macke5779992010-03-04 19:46:13 +0100922}
923
924static int snd_usb_pcm_open(struct snd_pcm_substream *substream, int direction)
925{
926 struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
927 struct snd_pcm_runtime *runtime = substream->runtime;
928 struct snd_usb_substream *subs = &as->substream[direction];
929
930 subs->interface = -1;
Clemens Ladische11b4e02010-03-04 19:46:14 +0100931 subs->altset_idx = 0;
Daniel Macke5779992010-03-04 19:46:13 +0100932 runtime->hw = snd_usb_hardware;
933 runtime->private_data = subs;
934 subs->pcm_substream = substream;
Oliver Neukum88a85162011-03-11 14:51:12 +0100935 /* runtime PM is also done there */
Daniel Macke5779992010-03-04 19:46:13 +0100936 return setup_hw_info(runtime, subs);
937}
938
939static int snd_usb_pcm_close(struct snd_pcm_substream *substream, int direction)
940{
Daniel Mackedcd3632012-04-12 13:51:12 +0200941 int ret;
Daniel Macke5779992010-03-04 19:46:13 +0100942 struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
943 struct snd_usb_substream *subs = &as->substream[direction];
944
Daniel Mackedcd3632012-04-12 13:51:12 +0200945 stop_endpoints(subs, 0, 0, 0);
946 ret = deactivate_endpoints(subs);
Daniel Macke5779992010-03-04 19:46:13 +0100947 subs->pcm_substream = NULL;
Oliver Neukum88a85162011-03-11 14:51:12 +0100948 snd_usb_autosuspend(subs->stream->chip);
Daniel Mackedcd3632012-04-12 13:51:12 +0200949
950 return ret;
951}
952
953/* Since a URB can handle only a single linear buffer, we must use double
954 * buffering when the data to be transferred overflows the buffer boundary.
955 * To avoid inconsistencies when updating hwptr_done, we use double buffering
956 * for all URBs.
957 */
958static void retire_capture_urb(struct snd_usb_substream *subs,
959 struct urb *urb)
960{
961 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
962 unsigned int stride, frames, bytes, oldptr;
963 int i, period_elapsed = 0;
964 unsigned long flags;
965 unsigned char *cp;
966
967 stride = runtime->frame_bits >> 3;
968
969 for (i = 0; i < urb->number_of_packets; i++) {
970 cp = (unsigned char *)urb->transfer_buffer + urb->iso_frame_desc[i].offset;
971 if (urb->iso_frame_desc[i].status && printk_ratelimit()) {
972 snd_printdd(KERN_ERR "frame %d active: %d\n", i, urb->iso_frame_desc[i].status);
973 // continue;
974 }
975 bytes = urb->iso_frame_desc[i].actual_length;
976 frames = bytes / stride;
977 if (!subs->txfr_quirk)
978 bytes = frames * stride;
979 if (bytes % (runtime->sample_bits >> 3) != 0) {
980#ifdef CONFIG_SND_DEBUG_VERBOSE
981 int oldbytes = bytes;
982#endif
983 bytes = frames * stride;
984 snd_printdd(KERN_ERR "Corrected urb data len. %d->%d\n",
985 oldbytes, bytes);
986 }
987 /* update the current pointer */
988 spin_lock_irqsave(&subs->lock, flags);
989 oldptr = subs->hwptr_done;
990 subs->hwptr_done += bytes;
991 if (subs->hwptr_done >= runtime->buffer_size * stride)
992 subs->hwptr_done -= runtime->buffer_size * stride;
993 frames = (bytes + (oldptr % stride)) / stride;
994 subs->transfer_done += frames;
995 if (subs->transfer_done >= runtime->period_size) {
996 subs->transfer_done -= runtime->period_size;
997 period_elapsed = 1;
998 }
999 spin_unlock_irqrestore(&subs->lock, flags);
1000 /* copy a data chunk */
1001 if (oldptr + bytes > runtime->buffer_size * stride) {
1002 unsigned int bytes1 =
1003 runtime->buffer_size * stride - oldptr;
1004 memcpy(runtime->dma_area + oldptr, cp, bytes1);
1005 memcpy(runtime->dma_area, cp + bytes1, bytes - bytes1);
1006 } else {
1007 memcpy(runtime->dma_area + oldptr, cp, bytes);
1008 }
1009 }
1010
1011 if (period_elapsed)
1012 snd_pcm_period_elapsed(subs->pcm_substream);
1013}
1014
1015static void prepare_playback_urb(struct snd_usb_substream *subs,
1016 struct urb *urb)
1017{
1018 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1019 struct snd_urb_ctx *ctx = urb->context;
1020 unsigned int counts, frames, bytes;
1021 int i, stride, period_elapsed = 0;
1022 unsigned long flags;
1023
1024 stride = runtime->frame_bits >> 3;
1025
1026 frames = 0;
1027 urb->number_of_packets = 0;
1028 spin_lock_irqsave(&subs->lock, flags);
1029 for (i = 0; i < ctx->packets; i++) {
1030 counts = ctx->packet_size[i];
1031 /* set up descriptor */
1032 urb->iso_frame_desc[i].offset = frames * stride;
1033 urb->iso_frame_desc[i].length = counts * stride;
1034 frames += counts;
1035 urb->number_of_packets++;
1036 subs->transfer_done += counts;
1037 if (subs->transfer_done >= runtime->period_size) {
1038 subs->transfer_done -= runtime->period_size;
1039 period_elapsed = 1;
1040 if (subs->fmt_type == UAC_FORMAT_TYPE_II) {
1041 if (subs->transfer_done > 0) {
1042 /* FIXME: fill-max mode is not
1043 * supported yet */
1044 frames -= subs->transfer_done;
1045 counts -= subs->transfer_done;
1046 urb->iso_frame_desc[i].length =
1047 counts * stride;
1048 subs->transfer_done = 0;
1049 }
1050 i++;
1051 if (i < ctx->packets) {
1052 /* add a transfer delimiter */
1053 urb->iso_frame_desc[i].offset =
1054 frames * stride;
1055 urb->iso_frame_desc[i].length = 0;
1056 urb->number_of_packets++;
1057 }
1058 break;
1059 }
1060 }
1061 if (period_elapsed &&
1062 !snd_usb_endpoint_implict_feedback_sink(subs->data_endpoint)) /* finish at the period boundary */
1063 break;
1064 }
1065 bytes = frames * stride;
1066 if (subs->hwptr_done + bytes > runtime->buffer_size * stride) {
1067 /* err, the transferred area goes over buffer boundary. */
1068 unsigned int bytes1 =
1069 runtime->buffer_size * stride - subs->hwptr_done;
1070 memcpy(urb->transfer_buffer,
1071 runtime->dma_area + subs->hwptr_done, bytes1);
1072 memcpy(urb->transfer_buffer + bytes1,
1073 runtime->dma_area, bytes - bytes1);
1074 } else {
1075 memcpy(urb->transfer_buffer,
1076 runtime->dma_area + subs->hwptr_done, bytes);
1077 }
1078 subs->hwptr_done += bytes;
1079 if (subs->hwptr_done >= runtime->buffer_size * stride)
1080 subs->hwptr_done -= runtime->buffer_size * stride;
1081 runtime->delay += frames;
1082 spin_unlock_irqrestore(&subs->lock, flags);
1083 urb->transfer_buffer_length = bytes;
1084 if (period_elapsed)
1085 snd_pcm_period_elapsed(subs->pcm_substream);
1086}
1087
1088/*
1089 * process after playback data complete
1090 * - decrease the delay count again
1091 */
1092static void retire_playback_urb(struct snd_usb_substream *subs,
1093 struct urb *urb)
1094{
1095 unsigned long flags;
1096 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1097 int stride = runtime->frame_bits >> 3;
1098 int processed = urb->transfer_buffer_length / stride;
1099
1100 spin_lock_irqsave(&subs->lock, flags);
1101 if (processed > runtime->delay)
1102 runtime->delay = 0;
1103 else
1104 runtime->delay -= processed;
1105 spin_unlock_irqrestore(&subs->lock, flags);
Daniel Macke5779992010-03-04 19:46:13 +01001106}
1107
1108static int snd_usb_playback_open(struct snd_pcm_substream *substream)
1109{
1110 return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_PLAYBACK);
1111}
1112
1113static int snd_usb_playback_close(struct snd_pcm_substream *substream)
1114{
1115 return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_PLAYBACK);
1116}
1117
1118static int snd_usb_capture_open(struct snd_pcm_substream *substream)
1119{
1120 return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_CAPTURE);
1121}
1122
1123static int snd_usb_capture_close(struct snd_pcm_substream *substream)
1124{
1125 return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_CAPTURE);
1126}
1127
Daniel Mackedcd3632012-04-12 13:51:12 +02001128static int snd_usb_substream_playback_trigger(struct snd_pcm_substream *substream,
1129 int cmd)
1130{
1131 struct snd_usb_substream *subs = substream->runtime->private_data;
1132
1133 switch (cmd) {
1134 case SNDRV_PCM_TRIGGER_START:
1135 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1136 subs->data_endpoint->prepare_data_urb = prepare_playback_urb;
1137 subs->data_endpoint->retire_data_urb = retire_playback_urb;
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001138 subs->running = 1;
Daniel Mackedcd3632012-04-12 13:51:12 +02001139 return 0;
1140 case SNDRV_PCM_TRIGGER_STOP:
1141 stop_endpoints(subs, 0, 0, 0);
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001142 subs->running = 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001143 return 0;
1144 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1145 subs->data_endpoint->prepare_data_urb = NULL;
1146 subs->data_endpoint->retire_data_urb = NULL;
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001147 subs->running = 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001148 return 0;
1149 }
1150
1151 return -EINVAL;
1152}
1153
Daniel Mackafe25962012-06-16 16:58:04 +02001154static int snd_usb_substream_capture_trigger(struct snd_pcm_substream *substream,
1155 int cmd)
Daniel Mackedcd3632012-04-12 13:51:12 +02001156{
1157 int err;
1158 struct snd_usb_substream *subs = substream->runtime->private_data;
1159
1160 switch (cmd) {
1161 case SNDRV_PCM_TRIGGER_START:
1162 err = start_endpoints(subs);
1163 if (err < 0)
1164 return err;
1165
1166 subs->data_endpoint->retire_data_urb = retire_capture_urb;
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001167 subs->running = 1;
Daniel Mackedcd3632012-04-12 13:51:12 +02001168 return 0;
1169 case SNDRV_PCM_TRIGGER_STOP:
1170 stop_endpoints(subs, 0, 0, 0);
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001171 subs->running = 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001172 return 0;
1173 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1174 subs->data_endpoint->retire_data_urb = NULL;
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001175 subs->running = 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001176 return 0;
1177 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1178 subs->data_endpoint->retire_data_urb = retire_capture_urb;
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001179 subs->running = 1;
Daniel Mackedcd3632012-04-12 13:51:12 +02001180 return 0;
1181 }
1182
1183 return -EINVAL;
1184}
1185
Daniel Macke5779992010-03-04 19:46:13 +01001186static struct snd_pcm_ops snd_usb_playback_ops = {
1187 .open = snd_usb_playback_open,
1188 .close = snd_usb_playback_close,
1189 .ioctl = snd_pcm_lib_ioctl,
1190 .hw_params = snd_usb_hw_params,
1191 .hw_free = snd_usb_hw_free,
1192 .prepare = snd_usb_pcm_prepare,
1193 .trigger = snd_usb_substream_playback_trigger,
1194 .pointer = snd_usb_pcm_pointer,
1195 .page = snd_pcm_lib_get_vmalloc_page,
1196 .mmap = snd_pcm_lib_mmap_vmalloc,
1197};
1198
1199static struct snd_pcm_ops snd_usb_capture_ops = {
1200 .open = snd_usb_capture_open,
1201 .close = snd_usb_capture_close,
1202 .ioctl = snd_pcm_lib_ioctl,
1203 .hw_params = snd_usb_hw_params,
1204 .hw_free = snd_usb_hw_free,
1205 .prepare = snd_usb_pcm_prepare,
1206 .trigger = snd_usb_substream_capture_trigger,
1207 .pointer = snd_usb_pcm_pointer,
1208 .page = snd_pcm_lib_get_vmalloc_page,
1209 .mmap = snd_pcm_lib_mmap_vmalloc,
1210};
1211
1212void snd_usb_set_pcm_ops(struct snd_pcm *pcm, int stream)
1213{
1214 snd_pcm_set_ops(pcm, stream,
1215 stream == SNDRV_PCM_STREAM_PLAYBACK ?
1216 &snd_usb_playback_ops : &snd_usb_capture_ops);
1217}