blob: 5ceb8f1d63fbe00ecf8c643cc35d06419c45f2c9 [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 Mack015618b2012-08-29 13:17:05 +0200215static int start_endpoints(struct snd_usb_substream *subs, int can_sleep)
Daniel Mackedcd3632012-04-12 13:51:12 +0200216{
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;
Daniel Mack015618b2012-08-29 13:17:05 +0200228 err = snd_usb_endpoint_start(ep, can_sleep);
Daniel Mackedcd3632012-04-12 13:51:12 +0200229 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;
Daniel Mack015618b2012-08-29 13:17:05 +0200242 err = snd_usb_endpoint_start(ep, can_sleep);
Daniel Mackedcd3632012-04-12 13:51:12 +0200243 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
Daniel Mackedcd3632012-04-12 13:51:12 +0200264static int deactivate_endpoints(struct snd_usb_substream *subs)
265{
266 int reta, retb;
267
268 reta = snd_usb_endpoint_deactivate(subs->sync_endpoint);
269 retb = snd_usb_endpoint_deactivate(subs->data_endpoint);
270
271 if (reta < 0)
272 return reta;
273
274 if (retb < 0)
275 return retb;
276
277 return 0;
278}
279
Daniel Macke5779992010-03-04 19:46:13 +0100280/*
281 * find a matching format and set up the interface
282 */
283static int set_format(struct snd_usb_substream *subs, struct audioformat *fmt)
284{
285 struct usb_device *dev = subs->dev;
286 struct usb_host_interface *alts;
287 struct usb_interface_descriptor *altsd;
288 struct usb_interface *iface;
289 unsigned int ep, attr;
290 int is_playback = subs->direction == SNDRV_PCM_STREAM_PLAYBACK;
Daniel Mackc75a8a72012-04-12 13:51:14 +0200291 int err, implicit_fb = 0;
Daniel Macke5779992010-03-04 19:46:13 +0100292
293 iface = usb_ifnum_to_if(dev, fmt->iface);
294 if (WARN_ON(!iface))
295 return -EINVAL;
296 alts = &iface->altsetting[fmt->altset_idx];
297 altsd = get_iface_desc(alts);
298 if (WARN_ON(altsd->bAlternateSetting != fmt->altsetting))
299 return -EINVAL;
300
301 if (fmt == subs->cur_audiofmt)
302 return 0;
303
Daniel Mack68e67f42012-07-12 13:08:40 +0200304 /* close the old interface */
305 if (subs->interface >= 0 && subs->interface != fmt->iface) {
306 err = usb_set_interface(subs->dev, subs->interface, 0);
307 if (err < 0) {
308 snd_printk(KERN_ERR "%d:%d:%d: return to setting 0 failed (%d)\n",
309 dev->devnum, fmt->iface, fmt->altsetting, err);
310 return -EIO;
311 }
312 subs->interface = -1;
313 subs->altset_idx = 0;
314 }
315
316 /* set interface */
317 if (subs->interface != fmt->iface ||
318 subs->altset_idx != fmt->altset_idx) {
319 err = usb_set_interface(dev, fmt->iface, fmt->altsetting);
320 if (err < 0) {
321 snd_printk(KERN_ERR "%d:%d:%d: usb_set_interface failed (%d)\n",
322 dev->devnum, fmt->iface, fmt->altsetting, err);
323 return -EIO;
324 }
325 snd_printdd(KERN_INFO "setting usb interface %d:%d\n",
326 fmt->iface, fmt->altsetting);
327 subs->interface = fmt->iface;
328 subs->altset_idx = fmt->altset_idx;
329 }
330
Daniel Mackedcd3632012-04-12 13:51:12 +0200331 subs->data_endpoint = snd_usb_add_endpoint(subs->stream->chip,
332 alts, fmt->endpoint, subs->direction,
333 SND_USB_ENDPOINT_TYPE_DATA);
334 if (!subs->data_endpoint)
335 return -EINVAL;
Daniel Macke5779992010-03-04 19:46:13 +0100336
337 /* we need a sync pipe in async OUT or adaptive IN mode */
338 /* check the number of EP, since some devices have broken
339 * descriptors which fool us. if it has only one EP,
340 * assume it as adaptive-out or sync-in.
341 */
342 attr = fmt->ep_attr & USB_ENDPOINT_SYNCTYPE;
Daniel Mackc75a8a72012-04-12 13:51:14 +0200343
344 switch (subs->stream->chip->usb_id) {
345 case USB_ID(0x0763, 0x2080): /* M-Audio FastTrack Ultra */
346 case USB_ID(0x0763, 0x2081):
347 if (is_playback) {
348 implicit_fb = 1;
Daniel Mackedcd3632012-04-12 13:51:12 +0200349 ep = 0x81;
350 iface = usb_ifnum_to_if(dev, 2);
Daniel Mackc75a8a72012-04-12 13:51:14 +0200351
352 if (!iface || iface->num_altsetting == 0)
353 return -EINVAL;
354
Daniel Mackedcd3632012-04-12 13:51:12 +0200355 alts = &iface->altsetting[1];
356 goto add_sync_ep;
357 }
Daniel Mackc75a8a72012-04-12 13:51:14 +0200358 }
Daniel Mackedcd3632012-04-12 13:51:12 +0200359
Daniel Mackc75a8a72012-04-12 13:51:14 +0200360 if (((is_playback && attr == USB_ENDPOINT_SYNC_ASYNC) ||
361 (!is_playback && attr == USB_ENDPOINT_SYNC_ADAPTIVE)) &&
362 altsd->bNumEndpoints >= 2) {
Daniel Macke5779992010-03-04 19:46:13 +0100363 /* check sync-pipe endpoint */
364 /* ... and check descriptor size before accessing bSynchAddress
365 because there is a version of the SB Audigy 2 NX firmware lacking
366 the audio fields in the endpoint descriptors */
367 if ((get_endpoint(alts, 1)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != 0x01 ||
368 (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
Daniel Mackc75a8a72012-04-12 13:51:14 +0200369 get_endpoint(alts, 1)->bSynchAddress != 0 &&
370 !implicit_fb)) {
Daniel Mack7fb75db2012-06-17 13:44:27 +0200371 snd_printk(KERN_ERR "%d:%d:%d : invalid sync pipe. bmAttributes %02x, bLength %d, bSynchAddress %02x\n",
372 dev->devnum, fmt->iface, fmt->altsetting,
373 get_endpoint(alts, 1)->bmAttributes,
374 get_endpoint(alts, 1)->bLength,
375 get_endpoint(alts, 1)->bSynchAddress);
Daniel Macke5779992010-03-04 19:46:13 +0100376 return -EINVAL;
377 }
378 ep = get_endpoint(alts, 1)->bEndpointAddress;
Daniel Mack7fb75db2012-06-17 13:44:27 +0200379 if (!implicit_fb &&
380 get_endpoint(alts, 0)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
Daniel Macke5779992010-03-04 19:46:13 +0100381 (( is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress | USB_DIR_IN)) ||
Daniel Mack7fb75db2012-06-17 13:44:27 +0200382 (!is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress & ~USB_DIR_IN)))) {
383 snd_printk(KERN_ERR "%d:%d:%d : invalid sync pipe. is_playback %d, ep %02x, bSynchAddress %02x\n",
384 dev->devnum, fmt->iface, fmt->altsetting,
385 is_playback, ep, get_endpoint(alts, 0)->bSynchAddress);
Daniel Macke5779992010-03-04 19:46:13 +0100386 return -EINVAL;
387 }
Daniel Mackc75a8a72012-04-12 13:51:14 +0200388
389 implicit_fb = (get_endpoint(alts, 1)->bmAttributes & USB_ENDPOINT_USAGE_MASK)
390 == USB_ENDPOINT_USAGE_IMPLICIT_FB;
391
Daniel Mackedcd3632012-04-12 13:51:12 +0200392add_sync_ep:
393 subs->sync_endpoint = snd_usb_add_endpoint(subs->stream->chip,
394 alts, ep, !subs->direction,
Daniel Mackc75a8a72012-04-12 13:51:14 +0200395 implicit_fb ?
396 SND_USB_ENDPOINT_TYPE_DATA :
397 SND_USB_ENDPOINT_TYPE_SYNC);
Daniel Mackedcd3632012-04-12 13:51:12 +0200398 if (!subs->sync_endpoint)
399 return -EINVAL;
400
401 subs->data_endpoint->sync_master = subs->sync_endpoint;
402 }
Daniel Macke5779992010-03-04 19:46:13 +0100403
Takashi Iwai9e9b5942012-07-06 08:11:43 +0200404 if ((err = snd_usb_init_pitch(subs->stream->chip, fmt->iface, alts, fmt)) < 0)
Daniel Macke5779992010-03-04 19:46:13 +0100405 return err;
406
407 subs->cur_audiofmt = fmt;
408
409 snd_usb_set_format_quirk(subs, fmt);
410
411#if 0
412 printk(KERN_DEBUG
413 "setting done: format = %d, rate = %d..%d, channels = %d\n",
414 fmt->format, fmt->rate_min, fmt->rate_max, fmt->channels);
415 printk(KERN_DEBUG
416 " datapipe = 0x%0x, syncpipe = 0x%0x\n",
417 subs->datapipe, subs->syncpipe);
418#endif
419
420 return 0;
421}
422
423/*
424 * hw_params callback
425 *
426 * allocate a buffer and set the given audio format.
427 *
428 * so far we use a physically linear buffer although packetize transfer
429 * doesn't need a continuous area.
430 * if sg buffer is supported on the later version of alsa, we'll follow
431 * that.
432 */
433static int snd_usb_hw_params(struct snd_pcm_substream *substream,
434 struct snd_pcm_hw_params *hw_params)
435{
436 struct snd_usb_substream *subs = substream->runtime->private_data;
437 struct audioformat *fmt;
438 unsigned int channels, rate, format;
439 int ret, changed;
440
441 ret = snd_pcm_lib_alloc_vmalloc_buffer(substream,
442 params_buffer_bytes(hw_params));
443 if (ret < 0)
444 return ret;
445
446 format = params_format(hw_params);
447 rate = params_rate(hw_params);
448 channels = params_channels(hw_params);
449 fmt = find_format(subs, format, rate, channels);
450 if (!fmt) {
451 snd_printd(KERN_DEBUG "cannot set format: format = %#x, rate = %d, channels = %d\n",
452 format, rate, channels);
453 return -EINVAL;
454 }
455
456 changed = subs->cur_audiofmt != fmt ||
457 subs->period_bytes != params_period_bytes(hw_params) ||
458 subs->cur_rate != rate;
459 if ((ret = set_format(subs, fmt)) < 0)
460 return ret;
461
462 if (subs->cur_rate != rate) {
463 struct usb_host_interface *alts;
464 struct usb_interface *iface;
465 iface = usb_ifnum_to_if(subs->dev, fmt->iface);
466 alts = &iface->altsetting[fmt->altset_idx];
Takashi Iwai9e9b5942012-07-06 08:11:43 +0200467 ret = snd_usb_init_sample_rate(subs->stream->chip, fmt->iface, alts, fmt, rate);
Daniel Macke5779992010-03-04 19:46:13 +0100468 if (ret < 0)
469 return ret;
470 subs->cur_rate = rate;
471 }
472
473 if (changed) {
Takashi Iwai382225e2011-02-22 10:21:18 +0100474 mutex_lock(&subs->stream->chip->shutdown_mutex);
Daniel Macke5779992010-03-04 19:46:13 +0100475 /* format changed */
Daniel Mackedcd3632012-04-12 13:51:12 +0200476 stop_endpoints(subs, 0, 0, 0);
Daniel Mackedcd3632012-04-12 13:51:12 +0200477 ret = snd_usb_endpoint_set_params(subs->data_endpoint, hw_params, fmt,
478 subs->sync_endpoint);
479 if (ret < 0)
480 goto unlock;
481
482 if (subs->sync_endpoint)
483 ret = snd_usb_endpoint_set_params(subs->sync_endpoint,
484 hw_params, fmt, NULL);
485unlock:
Takashi Iwai382225e2011-02-22 10:21:18 +0100486 mutex_unlock(&subs->stream->chip->shutdown_mutex);
Daniel Macke5779992010-03-04 19:46:13 +0100487 }
488
Daniel Mack97f8d3b2012-05-21 12:47:36 +0200489 if (ret == 0) {
490 subs->interface = fmt->iface;
491 subs->altset_idx = fmt->altset_idx;
492 }
493
Daniel Macke5779992010-03-04 19:46:13 +0100494 return ret;
495}
496
497/*
498 * hw_free callback
499 *
500 * reset the audio format and release the buffer
501 */
502static int snd_usb_hw_free(struct snd_pcm_substream *substream)
503{
504 struct snd_usb_substream *subs = substream->runtime->private_data;
505
506 subs->cur_audiofmt = NULL;
507 subs->cur_rate = 0;
508 subs->period_bytes = 0;
Takashi Iwai382225e2011-02-22 10:21:18 +0100509 mutex_lock(&subs->stream->chip->shutdown_mutex);
Daniel Mackedcd3632012-04-12 13:51:12 +0200510 stop_endpoints(subs, 0, 1, 1);
Daniel Mack68e67f42012-07-12 13:08:40 +0200511 deactivate_endpoints(subs);
Takashi Iwai382225e2011-02-22 10:21:18 +0100512 mutex_unlock(&subs->stream->chip->shutdown_mutex);
Daniel Macke5779992010-03-04 19:46:13 +0100513 return snd_pcm_lib_free_vmalloc_buffer(substream);
514}
515
516/*
517 * prepare callback
518 *
519 * only a few subtle things...
520 */
521static int snd_usb_pcm_prepare(struct snd_pcm_substream *substream)
522{
523 struct snd_pcm_runtime *runtime = substream->runtime;
524 struct snd_usb_substream *subs = runtime->private_data;
525
526 if (! subs->cur_audiofmt) {
527 snd_printk(KERN_ERR "usbaudio: no format is specified!\n");
528 return -ENXIO;
529 }
530
Daniel Mackedcd3632012-04-12 13:51:12 +0200531 if (snd_BUG_ON(!subs->data_endpoint))
532 return -EIO;
533
Daniel Macke5779992010-03-04 19:46:13 +0100534 /* some unit conversions in runtime */
Daniel Mackedcd3632012-04-12 13:51:12 +0200535 subs->data_endpoint->maxframesize =
536 bytes_to_frames(runtime, subs->data_endpoint->maxpacksize);
537 subs->data_endpoint->curframesize =
538 bytes_to_frames(runtime, subs->data_endpoint->curpacksize);
Daniel Macke5779992010-03-04 19:46:13 +0100539
540 /* reset the pointer */
541 subs->hwptr_done = 0;
542 subs->transfer_done = 0;
Pierre-Louis Bossart294c4fb2011-09-06 19:15:34 -0500543 subs->last_delay = 0;
544 subs->last_frame_number = 0;
Daniel Macke5779992010-03-04 19:46:13 +0100545 runtime->delay = 0;
546
Daniel Mackedcd3632012-04-12 13:51:12 +0200547 /* for playback, submit the URBs now; otherwise, the first hwptr_done
548 * updates for all URBs would happen at the same time when starting */
549 if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK)
Daniel Mack015618b2012-08-29 13:17:05 +0200550 return start_endpoints(subs, 1);
Daniel Mackedcd3632012-04-12 13:51:12 +0200551
552 return 0;
Daniel Macke5779992010-03-04 19:46:13 +0100553}
554
555static struct snd_pcm_hardware snd_usb_hardware =
556{
557 .info = SNDRV_PCM_INFO_MMAP |
558 SNDRV_PCM_INFO_MMAP_VALID |
559 SNDRV_PCM_INFO_BATCH |
560 SNDRV_PCM_INFO_INTERLEAVED |
561 SNDRV_PCM_INFO_BLOCK_TRANSFER |
562 SNDRV_PCM_INFO_PAUSE,
563 .buffer_bytes_max = 1024 * 1024,
564 .period_bytes_min = 64,
565 .period_bytes_max = 512 * 1024,
566 .periods_min = 2,
567 .periods_max = 1024,
568};
569
570static int hw_check_valid_format(struct snd_usb_substream *subs,
571 struct snd_pcm_hw_params *params,
572 struct audioformat *fp)
573{
574 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
575 struct snd_interval *ct = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
576 struct snd_mask *fmts = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
577 struct snd_interval *pt = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
Clemens Ladisch015eb0b2010-03-04 19:46:15 +0100578 struct snd_mask check_fmts;
Daniel Macke5779992010-03-04 19:46:13 +0100579 unsigned int ptime;
580
581 /* check the format */
Clemens Ladisch015eb0b2010-03-04 19:46:15 +0100582 snd_mask_none(&check_fmts);
583 check_fmts.bits[0] = (u32)fp->formats;
584 check_fmts.bits[1] = (u32)(fp->formats >> 32);
585 snd_mask_intersect(&check_fmts, fmts);
586 if (snd_mask_empty(&check_fmts)) {
Daniel Macke5779992010-03-04 19:46:13 +0100587 hwc_debug(" > check: no supported format %d\n", fp->format);
588 return 0;
589 }
590 /* check the channels */
591 if (fp->channels < ct->min || fp->channels > ct->max) {
592 hwc_debug(" > check: no valid channels %d (%d/%d)\n", fp->channels, ct->min, ct->max);
593 return 0;
594 }
595 /* check the rate is within the range */
596 if (fp->rate_min > it->max || (fp->rate_min == it->max && it->openmax)) {
597 hwc_debug(" > check: rate_min %d > max %d\n", fp->rate_min, it->max);
598 return 0;
599 }
600 if (fp->rate_max < it->min || (fp->rate_max == it->min && it->openmin)) {
601 hwc_debug(" > check: rate_max %d < min %d\n", fp->rate_max, it->min);
602 return 0;
603 }
604 /* check whether the period time is >= the data packet interval */
Paul Zimmerman4f4e8f62010-08-13 12:42:07 -0700605 if (snd_usb_get_speed(subs->dev) != USB_SPEED_FULL) {
Daniel Macke5779992010-03-04 19:46:13 +0100606 ptime = 125 * (1 << fp->datainterval);
607 if (ptime > pt->max || (ptime == pt->max && pt->openmax)) {
608 hwc_debug(" > check: ptime %u > max %u\n", ptime, pt->max);
609 return 0;
610 }
611 }
612 return 1;
613}
614
615static int hw_rule_rate(struct snd_pcm_hw_params *params,
616 struct snd_pcm_hw_rule *rule)
617{
618 struct snd_usb_substream *subs = rule->private;
619 struct list_head *p;
620 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
621 unsigned int rmin, rmax;
622 int changed;
623
624 hwc_debug("hw_rule_rate: (%d,%d)\n", it->min, it->max);
625 changed = 0;
626 rmin = rmax = 0;
627 list_for_each(p, &subs->fmt_list) {
628 struct audioformat *fp;
629 fp = list_entry(p, struct audioformat, list);
630 if (!hw_check_valid_format(subs, params, fp))
631 continue;
632 if (changed++) {
633 if (rmin > fp->rate_min)
634 rmin = fp->rate_min;
635 if (rmax < fp->rate_max)
636 rmax = fp->rate_max;
637 } else {
638 rmin = fp->rate_min;
639 rmax = fp->rate_max;
640 }
641 }
642
643 if (!changed) {
644 hwc_debug(" --> get empty\n");
645 it->empty = 1;
646 return -EINVAL;
647 }
648
649 changed = 0;
650 if (it->min < rmin) {
651 it->min = rmin;
652 it->openmin = 0;
653 changed = 1;
654 }
655 if (it->max > rmax) {
656 it->max = rmax;
657 it->openmax = 0;
658 changed = 1;
659 }
660 if (snd_interval_checkempty(it)) {
661 it->empty = 1;
662 return -EINVAL;
663 }
664 hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
665 return changed;
666}
667
668
669static int hw_rule_channels(struct snd_pcm_hw_params *params,
670 struct snd_pcm_hw_rule *rule)
671{
672 struct snd_usb_substream *subs = rule->private;
673 struct list_head *p;
674 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
675 unsigned int rmin, rmax;
676 int changed;
677
678 hwc_debug("hw_rule_channels: (%d,%d)\n", it->min, it->max);
679 changed = 0;
680 rmin = rmax = 0;
681 list_for_each(p, &subs->fmt_list) {
682 struct audioformat *fp;
683 fp = list_entry(p, struct audioformat, list);
684 if (!hw_check_valid_format(subs, params, fp))
685 continue;
686 if (changed++) {
687 if (rmin > fp->channels)
688 rmin = fp->channels;
689 if (rmax < fp->channels)
690 rmax = fp->channels;
691 } else {
692 rmin = fp->channels;
693 rmax = fp->channels;
694 }
695 }
696
697 if (!changed) {
698 hwc_debug(" --> get empty\n");
699 it->empty = 1;
700 return -EINVAL;
701 }
702
703 changed = 0;
704 if (it->min < rmin) {
705 it->min = rmin;
706 it->openmin = 0;
707 changed = 1;
708 }
709 if (it->max > rmax) {
710 it->max = rmax;
711 it->openmax = 0;
712 changed = 1;
713 }
714 if (snd_interval_checkempty(it)) {
715 it->empty = 1;
716 return -EINVAL;
717 }
718 hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
719 return changed;
720}
721
722static int hw_rule_format(struct snd_pcm_hw_params *params,
723 struct snd_pcm_hw_rule *rule)
724{
725 struct snd_usb_substream *subs = rule->private;
726 struct list_head *p;
727 struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
728 u64 fbits;
729 u32 oldbits[2];
730 int changed;
731
732 hwc_debug("hw_rule_format: %x:%x\n", fmt->bits[0], fmt->bits[1]);
733 fbits = 0;
734 list_for_each(p, &subs->fmt_list) {
735 struct audioformat *fp;
736 fp = list_entry(p, struct audioformat, list);
737 if (!hw_check_valid_format(subs, params, fp))
738 continue;
Clemens Ladisch015eb0b2010-03-04 19:46:15 +0100739 fbits |= fp->formats;
Daniel Macke5779992010-03-04 19:46:13 +0100740 }
741
742 oldbits[0] = fmt->bits[0];
743 oldbits[1] = fmt->bits[1];
744 fmt->bits[0] &= (u32)fbits;
745 fmt->bits[1] &= (u32)(fbits >> 32);
746 if (!fmt->bits[0] && !fmt->bits[1]) {
747 hwc_debug(" --> get empty\n");
748 return -EINVAL;
749 }
750 changed = (oldbits[0] != fmt->bits[0] || oldbits[1] != fmt->bits[1]);
751 hwc_debug(" --> %x:%x (changed = %d)\n", fmt->bits[0], fmt->bits[1], changed);
752 return changed;
753}
754
755static int hw_rule_period_time(struct snd_pcm_hw_params *params,
756 struct snd_pcm_hw_rule *rule)
757{
758 struct snd_usb_substream *subs = rule->private;
759 struct audioformat *fp;
760 struct snd_interval *it;
761 unsigned char min_datainterval;
762 unsigned int pmin;
763 int changed;
764
765 it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
766 hwc_debug("hw_rule_period_time: (%u,%u)\n", it->min, it->max);
767 min_datainterval = 0xff;
768 list_for_each_entry(fp, &subs->fmt_list, list) {
769 if (!hw_check_valid_format(subs, params, fp))
770 continue;
771 min_datainterval = min(min_datainterval, fp->datainterval);
772 }
773 if (min_datainterval == 0xff) {
Uwe Kleine-Königa7ce2e02010-07-12 17:15:44 +0200774 hwc_debug(" --> get empty\n");
Daniel Macke5779992010-03-04 19:46:13 +0100775 it->empty = 1;
776 return -EINVAL;
777 }
778 pmin = 125 * (1 << min_datainterval);
779 changed = 0;
780 if (it->min < pmin) {
781 it->min = pmin;
782 it->openmin = 0;
783 changed = 1;
784 }
785 if (snd_interval_checkempty(it)) {
786 it->empty = 1;
787 return -EINVAL;
788 }
789 hwc_debug(" --> (%u,%u) (changed = %d)\n", it->min, it->max, changed);
790 return changed;
791}
792
793/*
794 * If the device supports unusual bit rates, does the request meet these?
795 */
796static int snd_usb_pcm_check_knot(struct snd_pcm_runtime *runtime,
797 struct snd_usb_substream *subs)
798{
799 struct audioformat *fp;
Takashi Iwai0717d0f2012-03-15 16:14:38 +0100800 int *rate_list;
Daniel Macke5779992010-03-04 19:46:13 +0100801 int count = 0, needs_knot = 0;
802 int err;
803
Clemens Ladisch5cd5d7c2012-05-18 18:00:43 +0200804 kfree(subs->rate_list.list);
805 subs->rate_list.list = NULL;
806
Daniel Macke5779992010-03-04 19:46:13 +0100807 list_for_each_entry(fp, &subs->fmt_list, list) {
808 if (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)
809 return 0;
810 count += fp->nr_rates;
811 if (fp->rates & SNDRV_PCM_RATE_KNOT)
812 needs_knot = 1;
813 }
814 if (!needs_knot)
815 return 0;
816
Takashi Iwai0717d0f2012-03-15 16:14:38 +0100817 subs->rate_list.list = rate_list =
818 kmalloc(sizeof(int) * count, GFP_KERNEL);
Jesper Juhl8a8d56b2010-10-29 20:40:23 +0200819 if (!subs->rate_list.list)
820 return -ENOMEM;
821 subs->rate_list.count = count;
Daniel Macke5779992010-03-04 19:46:13 +0100822 subs->rate_list.mask = 0;
823 count = 0;
824 list_for_each_entry(fp, &subs->fmt_list, list) {
825 int i;
826 for (i = 0; i < fp->nr_rates; i++)
Takashi Iwai0717d0f2012-03-15 16:14:38 +0100827 rate_list[count++] = fp->rate_table[i];
Daniel Macke5779992010-03-04 19:46:13 +0100828 }
829 err = snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
830 &subs->rate_list);
831 if (err < 0)
832 return err;
833
834 return 0;
835}
836
837
838/*
839 * set up the runtime hardware information.
840 */
841
842static int setup_hw_info(struct snd_pcm_runtime *runtime, struct snd_usb_substream *subs)
843{
844 struct list_head *p;
845 unsigned int pt, ptmin;
846 int param_period_time_if_needed;
847 int err;
848
849 runtime->hw.formats = subs->formats;
850
851 runtime->hw.rate_min = 0x7fffffff;
852 runtime->hw.rate_max = 0;
853 runtime->hw.channels_min = 256;
854 runtime->hw.channels_max = 0;
855 runtime->hw.rates = 0;
856 ptmin = UINT_MAX;
857 /* check min/max rates and channels */
858 list_for_each(p, &subs->fmt_list) {
859 struct audioformat *fp;
860 fp = list_entry(p, struct audioformat, list);
861 runtime->hw.rates |= fp->rates;
862 if (runtime->hw.rate_min > fp->rate_min)
863 runtime->hw.rate_min = fp->rate_min;
864 if (runtime->hw.rate_max < fp->rate_max)
865 runtime->hw.rate_max = fp->rate_max;
866 if (runtime->hw.channels_min > fp->channels)
867 runtime->hw.channels_min = fp->channels;
868 if (runtime->hw.channels_max < fp->channels)
869 runtime->hw.channels_max = fp->channels;
870 if (fp->fmt_type == UAC_FORMAT_TYPE_II && fp->frame_size > 0) {
871 /* FIXME: there might be more than one audio formats... */
872 runtime->hw.period_bytes_min = runtime->hw.period_bytes_max =
873 fp->frame_size;
874 }
875 pt = 125 * (1 << fp->datainterval);
876 ptmin = min(ptmin, pt);
877 }
Oliver Neukum88a85162011-03-11 14:51:12 +0100878 err = snd_usb_autoresume(subs->stream->chip);
879 if (err < 0)
880 return err;
Daniel Macke5779992010-03-04 19:46:13 +0100881
882 param_period_time_if_needed = SNDRV_PCM_HW_PARAM_PERIOD_TIME;
Paul Zimmerman4f4e8f62010-08-13 12:42:07 -0700883 if (snd_usb_get_speed(subs->dev) == USB_SPEED_FULL)
Daniel Macke5779992010-03-04 19:46:13 +0100884 /* full speed devices have fixed data packet interval */
885 ptmin = 1000;
886 if (ptmin == 1000)
887 /* if period time doesn't go below 1 ms, no rules needed */
888 param_period_time_if_needed = -1;
889 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
890 ptmin, UINT_MAX);
891
892 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
893 hw_rule_rate, subs,
894 SNDRV_PCM_HW_PARAM_FORMAT,
895 SNDRV_PCM_HW_PARAM_CHANNELS,
896 param_period_time_if_needed,
897 -1)) < 0)
Oliver Neukum88a85162011-03-11 14:51:12 +0100898 goto rep_err;
Daniel Macke5779992010-03-04 19:46:13 +0100899 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
900 hw_rule_channels, subs,
901 SNDRV_PCM_HW_PARAM_FORMAT,
902 SNDRV_PCM_HW_PARAM_RATE,
903 param_period_time_if_needed,
904 -1)) < 0)
Oliver Neukum88a85162011-03-11 14:51:12 +0100905 goto rep_err;
Daniel Macke5779992010-03-04 19:46:13 +0100906 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
907 hw_rule_format, subs,
908 SNDRV_PCM_HW_PARAM_RATE,
909 SNDRV_PCM_HW_PARAM_CHANNELS,
910 param_period_time_if_needed,
911 -1)) < 0)
Oliver Neukum88a85162011-03-11 14:51:12 +0100912 goto rep_err;
Daniel Macke5779992010-03-04 19:46:13 +0100913 if (param_period_time_if_needed >= 0) {
914 err = snd_pcm_hw_rule_add(runtime, 0,
915 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
916 hw_rule_period_time, subs,
917 SNDRV_PCM_HW_PARAM_FORMAT,
918 SNDRV_PCM_HW_PARAM_CHANNELS,
919 SNDRV_PCM_HW_PARAM_RATE,
920 -1);
921 if (err < 0)
Oliver Neukum88a85162011-03-11 14:51:12 +0100922 goto rep_err;
Daniel Macke5779992010-03-04 19:46:13 +0100923 }
924 if ((err = snd_usb_pcm_check_knot(runtime, subs)) < 0)
Oliver Neukum88a85162011-03-11 14:51:12 +0100925 goto rep_err;
Daniel Macke5779992010-03-04 19:46:13 +0100926 return 0;
Oliver Neukum88a85162011-03-11 14:51:12 +0100927
928rep_err:
929 snd_usb_autosuspend(subs->stream->chip);
930 return err;
Daniel Macke5779992010-03-04 19:46:13 +0100931}
932
933static int snd_usb_pcm_open(struct snd_pcm_substream *substream, int direction)
934{
935 struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
936 struct snd_pcm_runtime *runtime = substream->runtime;
937 struct snd_usb_substream *subs = &as->substream[direction];
938
939 subs->interface = -1;
Clemens Ladische11b4e02010-03-04 19:46:14 +0100940 subs->altset_idx = 0;
Daniel Macke5779992010-03-04 19:46:13 +0100941 runtime->hw = snd_usb_hardware;
942 runtime->private_data = subs;
943 subs->pcm_substream = substream;
Oliver Neukum88a85162011-03-11 14:51:12 +0100944 /* runtime PM is also done there */
Daniel Macke5779992010-03-04 19:46:13 +0100945 return setup_hw_info(runtime, subs);
946}
947
948static int snd_usb_pcm_close(struct snd_pcm_substream *substream, int direction)
949{
950 struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
951 struct snd_usb_substream *subs = &as->substream[direction];
952
Daniel Mackedcd3632012-04-12 13:51:12 +0200953 stop_endpoints(subs, 0, 0, 0);
Daniel Mack68e67f42012-07-12 13:08:40 +0200954
955 if (!as->chip->shutdown && subs->interface >= 0) {
956 usb_set_interface(subs->dev, subs->interface, 0);
957 subs->interface = -1;
958 }
959
Daniel Macke5779992010-03-04 19:46:13 +0100960 subs->pcm_substream = NULL;
Oliver Neukum88a85162011-03-11 14:51:12 +0100961 snd_usb_autosuspend(subs->stream->chip);
Daniel Mackedcd3632012-04-12 13:51:12 +0200962
Daniel Mack68e67f42012-07-12 13:08:40 +0200963 return 0;
Daniel Mackedcd3632012-04-12 13:51:12 +0200964}
965
966/* Since a URB can handle only a single linear buffer, we must use double
967 * buffering when the data to be transferred overflows the buffer boundary.
968 * To avoid inconsistencies when updating hwptr_done, we use double buffering
969 * for all URBs.
970 */
971static void retire_capture_urb(struct snd_usb_substream *subs,
972 struct urb *urb)
973{
974 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
975 unsigned int stride, frames, bytes, oldptr;
976 int i, period_elapsed = 0;
977 unsigned long flags;
978 unsigned char *cp;
979
980 stride = runtime->frame_bits >> 3;
981
982 for (i = 0; i < urb->number_of_packets; i++) {
983 cp = (unsigned char *)urb->transfer_buffer + urb->iso_frame_desc[i].offset;
984 if (urb->iso_frame_desc[i].status && printk_ratelimit()) {
985 snd_printdd(KERN_ERR "frame %d active: %d\n", i, urb->iso_frame_desc[i].status);
986 // continue;
987 }
988 bytes = urb->iso_frame_desc[i].actual_length;
989 frames = bytes / stride;
990 if (!subs->txfr_quirk)
991 bytes = frames * stride;
992 if (bytes % (runtime->sample_bits >> 3) != 0) {
993#ifdef CONFIG_SND_DEBUG_VERBOSE
994 int oldbytes = bytes;
995#endif
996 bytes = frames * stride;
997 snd_printdd(KERN_ERR "Corrected urb data len. %d->%d\n",
998 oldbytes, bytes);
999 }
1000 /* update the current pointer */
1001 spin_lock_irqsave(&subs->lock, flags);
1002 oldptr = subs->hwptr_done;
1003 subs->hwptr_done += bytes;
1004 if (subs->hwptr_done >= runtime->buffer_size * stride)
1005 subs->hwptr_done -= runtime->buffer_size * stride;
1006 frames = (bytes + (oldptr % stride)) / stride;
1007 subs->transfer_done += frames;
1008 if (subs->transfer_done >= runtime->period_size) {
1009 subs->transfer_done -= runtime->period_size;
1010 period_elapsed = 1;
1011 }
1012 spin_unlock_irqrestore(&subs->lock, flags);
1013 /* copy a data chunk */
1014 if (oldptr + bytes > runtime->buffer_size * stride) {
1015 unsigned int bytes1 =
1016 runtime->buffer_size * stride - oldptr;
1017 memcpy(runtime->dma_area + oldptr, cp, bytes1);
1018 memcpy(runtime->dma_area, cp + bytes1, bytes - bytes1);
1019 } else {
1020 memcpy(runtime->dma_area + oldptr, cp, bytes);
1021 }
1022 }
1023
1024 if (period_elapsed)
1025 snd_pcm_period_elapsed(subs->pcm_substream);
1026}
1027
1028static void prepare_playback_urb(struct snd_usb_substream *subs,
1029 struct urb *urb)
1030{
1031 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1032 struct snd_urb_ctx *ctx = urb->context;
1033 unsigned int counts, frames, bytes;
1034 int i, stride, period_elapsed = 0;
1035 unsigned long flags;
1036
1037 stride = runtime->frame_bits >> 3;
1038
1039 frames = 0;
1040 urb->number_of_packets = 0;
1041 spin_lock_irqsave(&subs->lock, flags);
1042 for (i = 0; i < ctx->packets; i++) {
1043 counts = ctx->packet_size[i];
1044 /* set up descriptor */
1045 urb->iso_frame_desc[i].offset = frames * stride;
1046 urb->iso_frame_desc[i].length = counts * stride;
1047 frames += counts;
1048 urb->number_of_packets++;
1049 subs->transfer_done += counts;
1050 if (subs->transfer_done >= runtime->period_size) {
1051 subs->transfer_done -= runtime->period_size;
1052 period_elapsed = 1;
1053 if (subs->fmt_type == UAC_FORMAT_TYPE_II) {
1054 if (subs->transfer_done > 0) {
1055 /* FIXME: fill-max mode is not
1056 * supported yet */
1057 frames -= subs->transfer_done;
1058 counts -= subs->transfer_done;
1059 urb->iso_frame_desc[i].length =
1060 counts * stride;
1061 subs->transfer_done = 0;
1062 }
1063 i++;
1064 if (i < ctx->packets) {
1065 /* add a transfer delimiter */
1066 urb->iso_frame_desc[i].offset =
1067 frames * stride;
1068 urb->iso_frame_desc[i].length = 0;
1069 urb->number_of_packets++;
1070 }
1071 break;
1072 }
1073 }
1074 if (period_elapsed &&
1075 !snd_usb_endpoint_implict_feedback_sink(subs->data_endpoint)) /* finish at the period boundary */
1076 break;
1077 }
1078 bytes = frames * stride;
1079 if (subs->hwptr_done + bytes > runtime->buffer_size * stride) {
1080 /* err, the transferred area goes over buffer boundary. */
1081 unsigned int bytes1 =
1082 runtime->buffer_size * stride - subs->hwptr_done;
1083 memcpy(urb->transfer_buffer,
1084 runtime->dma_area + subs->hwptr_done, bytes1);
1085 memcpy(urb->transfer_buffer + bytes1,
1086 runtime->dma_area, bytes - bytes1);
1087 } else {
1088 memcpy(urb->transfer_buffer,
1089 runtime->dma_area + subs->hwptr_done, bytes);
1090 }
1091 subs->hwptr_done += bytes;
1092 if (subs->hwptr_done >= runtime->buffer_size * stride)
1093 subs->hwptr_done -= runtime->buffer_size * stride;
Daniel Mackfbcfbf52012-08-30 18:52:29 +02001094
1095 /* update delay with exact number of samples queued */
1096 runtime->delay = subs->last_delay;
Daniel Mackedcd3632012-04-12 13:51:12 +02001097 runtime->delay += frames;
Daniel Mackfbcfbf52012-08-30 18:52:29 +02001098 subs->last_delay = runtime->delay;
1099
1100 /* realign last_frame_number */
1101 subs->last_frame_number = usb_get_current_frame_number(subs->dev);
1102 subs->last_frame_number &= 0xFF; /* keep 8 LSBs */
1103
Daniel Mackedcd3632012-04-12 13:51:12 +02001104 spin_unlock_irqrestore(&subs->lock, flags);
1105 urb->transfer_buffer_length = bytes;
1106 if (period_elapsed)
1107 snd_pcm_period_elapsed(subs->pcm_substream);
1108}
1109
1110/*
1111 * process after playback data complete
1112 * - decrease the delay count again
1113 */
1114static void retire_playback_urb(struct snd_usb_substream *subs,
1115 struct urb *urb)
1116{
1117 unsigned long flags;
1118 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1119 int stride = runtime->frame_bits >> 3;
1120 int processed = urb->transfer_buffer_length / stride;
Daniel Mackfbcfbf52012-08-30 18:52:29 +02001121 int est_delay;
Daniel Mackedcd3632012-04-12 13:51:12 +02001122
1123 spin_lock_irqsave(&subs->lock, flags);
Daniel Mackfbcfbf52012-08-30 18:52:29 +02001124 est_delay = snd_usb_pcm_delay(subs, runtime->rate);
1125 /* update delay with exact number of samples played */
1126 if (processed > subs->last_delay)
1127 subs->last_delay = 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001128 else
Daniel Mackfbcfbf52012-08-30 18:52:29 +02001129 subs->last_delay -= processed;
1130 runtime->delay = subs->last_delay;
1131
1132 /*
1133 * Report when delay estimate is off by more than 2ms.
1134 * The error should be lower than 2ms since the estimate relies
1135 * on two reads of a counter updated every ms.
1136 */
1137 if (abs(est_delay - subs->last_delay) * 1000 > runtime->rate * 2)
1138 snd_printk(KERN_DEBUG "delay: estimated %d, actual %d\n",
1139 est_delay, subs->last_delay);
1140
Daniel Mackedcd3632012-04-12 13:51:12 +02001141 spin_unlock_irqrestore(&subs->lock, flags);
Daniel Macke5779992010-03-04 19:46:13 +01001142}
1143
1144static int snd_usb_playback_open(struct snd_pcm_substream *substream)
1145{
1146 return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_PLAYBACK);
1147}
1148
1149static int snd_usb_playback_close(struct snd_pcm_substream *substream)
1150{
1151 return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_PLAYBACK);
1152}
1153
1154static int snd_usb_capture_open(struct snd_pcm_substream *substream)
1155{
1156 return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_CAPTURE);
1157}
1158
1159static int snd_usb_capture_close(struct snd_pcm_substream *substream)
1160{
1161 return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_CAPTURE);
1162}
1163
Daniel Mackedcd3632012-04-12 13:51:12 +02001164static int snd_usb_substream_playback_trigger(struct snd_pcm_substream *substream,
1165 int cmd)
1166{
1167 struct snd_usb_substream *subs = substream->runtime->private_data;
1168
1169 switch (cmd) {
1170 case SNDRV_PCM_TRIGGER_START:
1171 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1172 subs->data_endpoint->prepare_data_urb = prepare_playback_urb;
1173 subs->data_endpoint->retire_data_urb = retire_playback_urb;
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001174 subs->running = 1;
Daniel Mackedcd3632012-04-12 13:51:12 +02001175 return 0;
1176 case SNDRV_PCM_TRIGGER_STOP:
1177 stop_endpoints(subs, 0, 0, 0);
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001178 subs->running = 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001179 return 0;
1180 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1181 subs->data_endpoint->prepare_data_urb = NULL;
1182 subs->data_endpoint->retire_data_urb = NULL;
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001183 subs->running = 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001184 return 0;
1185 }
1186
1187 return -EINVAL;
1188}
1189
Daniel Mackafe25962012-06-16 16:58:04 +02001190static int snd_usb_substream_capture_trigger(struct snd_pcm_substream *substream,
1191 int cmd)
Daniel Mackedcd3632012-04-12 13:51:12 +02001192{
1193 int err;
1194 struct snd_usb_substream *subs = substream->runtime->private_data;
1195
1196 switch (cmd) {
1197 case SNDRV_PCM_TRIGGER_START:
Daniel Mack015618b2012-08-29 13:17:05 +02001198 err = start_endpoints(subs, 0);
Daniel Mackedcd3632012-04-12 13:51:12 +02001199 if (err < 0)
1200 return err;
1201
1202 subs->data_endpoint->retire_data_urb = retire_capture_urb;
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001203 subs->running = 1;
Daniel Mackedcd3632012-04-12 13:51:12 +02001204 return 0;
1205 case SNDRV_PCM_TRIGGER_STOP:
1206 stop_endpoints(subs, 0, 0, 0);
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001207 subs->running = 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001208 return 0;
1209 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1210 subs->data_endpoint->retire_data_urb = NULL;
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001211 subs->running = 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001212 return 0;
1213 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1214 subs->data_endpoint->retire_data_urb = retire_capture_urb;
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001215 subs->running = 1;
Daniel Mackedcd3632012-04-12 13:51:12 +02001216 return 0;
1217 }
1218
1219 return -EINVAL;
1220}
1221
Daniel Macke5779992010-03-04 19:46:13 +01001222static struct snd_pcm_ops snd_usb_playback_ops = {
1223 .open = snd_usb_playback_open,
1224 .close = snd_usb_playback_close,
1225 .ioctl = snd_pcm_lib_ioctl,
1226 .hw_params = snd_usb_hw_params,
1227 .hw_free = snd_usb_hw_free,
1228 .prepare = snd_usb_pcm_prepare,
1229 .trigger = snd_usb_substream_playback_trigger,
1230 .pointer = snd_usb_pcm_pointer,
1231 .page = snd_pcm_lib_get_vmalloc_page,
1232 .mmap = snd_pcm_lib_mmap_vmalloc,
1233};
1234
1235static struct snd_pcm_ops snd_usb_capture_ops = {
1236 .open = snd_usb_capture_open,
1237 .close = snd_usb_capture_close,
1238 .ioctl = snd_pcm_lib_ioctl,
1239 .hw_params = snd_usb_hw_params,
1240 .hw_free = snd_usb_hw_free,
1241 .prepare = snd_usb_pcm_prepare,
1242 .trigger = snd_usb_substream_capture_trigger,
1243 .pointer = snd_usb_pcm_pointer,
1244 .page = snd_pcm_lib_get_vmalloc_page,
1245 .mmap = snd_pcm_lib_mmap_vmalloc,
1246};
1247
1248void snd_usb_set_pcm_ops(struct snd_pcm *pcm, int stream)
1249{
1250 snd_pcm_set_ops(pcm, stream,
1251 stream == SNDRV_PCM_STREAM_PLAYBACK ?
1252 &snd_usb_playback_ops : &snd_usb_capture_ops);
1253}