blob: d82e378d37cbb7202aaa487b6003b6b171b79eab [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
Takashi Iwai48779a02012-11-23 16:00:37 +010049 if (!subs->last_delay)
50 return 0; /* short path */
51
Pierre-Louis Bossart294c4fb2011-09-06 19:15:34 -050052 current_frame_number = usb_get_current_frame_number(subs->dev);
53 /*
54 * HCD implementations use different widths, use lower 8 bits.
55 * The delay will be managed up to 256ms, which is more than
56 * enough
57 */
58 frame_diff = (current_frame_number - subs->last_frame_number) & 0xff;
59
60 /* Approximation based on number of samples per USB frame (ms),
61 some truncation for 44.1 but the estimate is good enough */
62 est_delay = subs->last_delay - (frame_diff * rate / 1000);
63 if (est_delay < 0)
64 est_delay = 0;
65 return est_delay;
66}
67
Daniel Macke5779992010-03-04 19:46:13 +010068/*
69 * return the current pcm pointer. just based on the hwptr_done value.
70 */
71static snd_pcm_uframes_t snd_usb_pcm_pointer(struct snd_pcm_substream *substream)
72{
73 struct snd_usb_substream *subs;
74 unsigned int hwptr_done;
75
76 subs = (struct snd_usb_substream *)substream->runtime->private_data;
Takashi Iwai978520b2012-10-12 15:12:55 +020077 if (subs->stream->chip->shutdown)
78 return SNDRV_PCM_POS_XRUN;
Daniel Macke5779992010-03-04 19:46:13 +010079 spin_lock(&subs->lock);
80 hwptr_done = subs->hwptr_done;
Takashi Iwai3f94fad2012-11-23 14:28:42 +010081 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
82 substream->runtime->delay = snd_usb_pcm_delay(subs,
Pierre-Louis Bossart294c4fb2011-09-06 19:15:34 -050083 substream->runtime->rate);
Daniel Macke5779992010-03-04 19:46:13 +010084 spin_unlock(&subs->lock);
85 return hwptr_done / (substream->runtime->frame_bits >> 3);
86}
87
88/*
89 * find a matching audio format
90 */
Dylan Reid61a70952012-09-18 09:49:48 -070091static struct audioformat *find_format(struct snd_usb_substream *subs)
Daniel Macke5779992010-03-04 19:46:13 +010092{
93 struct list_head *p;
94 struct audioformat *found = NULL;
95 int cur_attr = 0, attr;
96
97 list_for_each(p, &subs->fmt_list) {
98 struct audioformat *fp;
99 fp = list_entry(p, struct audioformat, list);
Dylan Reid61a70952012-09-18 09:49:48 -0700100 if (!(fp->formats & (1uLL << subs->pcm_format)))
Clemens Ladisch015eb0b2010-03-04 19:46:15 +0100101 continue;
Dylan Reid61a70952012-09-18 09:49:48 -0700102 if (fp->channels != subs->channels)
Daniel Macke5779992010-03-04 19:46:13 +0100103 continue;
Dylan Reid61a70952012-09-18 09:49:48 -0700104 if (subs->cur_rate < fp->rate_min ||
105 subs->cur_rate > fp->rate_max)
Daniel Macke5779992010-03-04 19:46:13 +0100106 continue;
107 if (! (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)) {
108 unsigned int i;
109 for (i = 0; i < fp->nr_rates; i++)
Dylan Reid61a70952012-09-18 09:49:48 -0700110 if (fp->rate_table[i] == subs->cur_rate)
Daniel Macke5779992010-03-04 19:46:13 +0100111 break;
112 if (i >= fp->nr_rates)
113 continue;
114 }
115 attr = fp->ep_attr & USB_ENDPOINT_SYNCTYPE;
116 if (! found) {
117 found = fp;
118 cur_attr = attr;
119 continue;
120 }
121 /* avoid async out and adaptive in if the other method
122 * supports the same format.
123 * this is a workaround for the case like
124 * M-audio audiophile USB.
125 */
126 if (attr != cur_attr) {
127 if ((attr == USB_ENDPOINT_SYNC_ASYNC &&
128 subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
129 (attr == USB_ENDPOINT_SYNC_ADAPTIVE &&
130 subs->direction == SNDRV_PCM_STREAM_CAPTURE))
131 continue;
132 if ((cur_attr == USB_ENDPOINT_SYNC_ASYNC &&
133 subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
134 (cur_attr == USB_ENDPOINT_SYNC_ADAPTIVE &&
135 subs->direction == SNDRV_PCM_STREAM_CAPTURE)) {
136 found = fp;
137 cur_attr = attr;
138 continue;
139 }
140 }
141 /* find the format with the largest max. packet size */
142 if (fp->maxpacksize > found->maxpacksize) {
143 found = fp;
144 cur_attr = attr;
145 }
146 }
147 return found;
148}
149
Daniel Mack767d75a2010-03-04 19:46:17 +0100150static int init_pitch_v1(struct snd_usb_audio *chip, int iface,
151 struct usb_host_interface *alts,
152 struct audioformat *fmt)
Daniel Macke5779992010-03-04 19:46:13 +0100153{
Daniel Mack767d75a2010-03-04 19:46:17 +0100154 struct usb_device *dev = chip->dev;
Daniel Macke5779992010-03-04 19:46:13 +0100155 unsigned int ep;
156 unsigned char data[1];
157 int err;
158
159 ep = get_endpoint(alts, 0)->bEndpointAddress;
Daniel Mack767d75a2010-03-04 19:46:17 +0100160
Daniel Mack767d75a2010-03-04 19:46:17 +0100161 data[0] = 1;
162 if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC_SET_CUR,
163 USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT,
164 UAC_EP_CS_ATTR_PITCH_CONTROL << 8, ep,
Clemens Ladisch17d900c2011-09-26 21:15:27 +0200165 data, sizeof(data))) < 0) {
Daniel Mack767d75a2010-03-04 19:46:17 +0100166 snd_printk(KERN_ERR "%d:%d:%d: cannot set enable PITCH\n",
167 dev->devnum, iface, ep);
168 return err;
Daniel Macke5779992010-03-04 19:46:13 +0100169 }
Daniel Mack767d75a2010-03-04 19:46:17 +0100170
Daniel Macke5779992010-03-04 19:46:13 +0100171 return 0;
172}
173
Daniel Mack92c25612010-05-26 18:11:39 +0200174static int init_pitch_v2(struct snd_usb_audio *chip, int iface,
175 struct usb_host_interface *alts,
176 struct audioformat *fmt)
177{
178 struct usb_device *dev = chip->dev;
179 unsigned char data[1];
Daniel Mack92c25612010-05-26 18:11:39 +0200180 int err;
181
Daniel Mack92c25612010-05-26 18:11:39 +0200182 data[0] = 1;
183 if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC2_CS_CUR,
184 USB_TYPE_CLASS | USB_RECIP_ENDPOINT | USB_DIR_OUT,
185 UAC2_EP_CS_PITCH << 8, 0,
Clemens Ladisch17d900c2011-09-26 21:15:27 +0200186 data, sizeof(data))) < 0) {
Daniel Mack92c25612010-05-26 18:11:39 +0200187 snd_printk(KERN_ERR "%d:%d:%d: cannot set enable PITCH (v2)\n",
188 dev->devnum, iface, fmt->altsetting);
189 return err;
190 }
191
192 return 0;
193}
194
Daniel Mack767d75a2010-03-04 19:46:17 +0100195/*
Daniel Mack92c25612010-05-26 18:11:39 +0200196 * initialize the pitch control and sample rate
Daniel Mack767d75a2010-03-04 19:46:17 +0100197 */
198int snd_usb_init_pitch(struct snd_usb_audio *chip, int iface,
199 struct usb_host_interface *alts,
200 struct audioformat *fmt)
201{
202 struct usb_interface_descriptor *altsd = get_iface_desc(alts);
203
Daniel Mack92c25612010-05-26 18:11:39 +0200204 /* if endpoint doesn't have pitch control, bail out */
205 if (!(fmt->attributes & UAC_EP_CS_ATTR_PITCH_CONTROL))
206 return 0;
207
Daniel Mack767d75a2010-03-04 19:46:17 +0100208 switch (altsd->bInterfaceProtocol) {
209 case UAC_VERSION_1:
Clemens Ladischa2acad82010-09-03 10:53:11 +0200210 default:
Daniel Mack767d75a2010-03-04 19:46:17 +0100211 return init_pitch_v1(chip, iface, alts, fmt);
212
213 case UAC_VERSION_2:
Daniel Mack92c25612010-05-26 18:11:39 +0200214 return init_pitch_v2(chip, iface, alts, fmt);
Daniel Mack767d75a2010-03-04 19:46:17 +0100215 }
Daniel Mack767d75a2010-03-04 19:46:17 +0100216}
217
Takashi Iwaia9bb3622012-11-20 18:32:06 +0100218static int start_endpoints(struct snd_usb_substream *subs, bool can_sleep)
Daniel Mackedcd3632012-04-12 13:51:12 +0200219{
220 int err;
221
222 if (!subs->data_endpoint)
223 return -EINVAL;
224
225 if (!test_and_set_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags)) {
226 struct snd_usb_endpoint *ep = subs->data_endpoint;
227
228 snd_printdd(KERN_DEBUG "Starting data EP @%p\n", ep);
229
230 ep->data_subs = subs;
Daniel Mack015618b2012-08-29 13:17:05 +0200231 err = snd_usb_endpoint_start(ep, can_sleep);
Daniel Mackedcd3632012-04-12 13:51:12 +0200232 if (err < 0) {
233 clear_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags);
234 return err;
235 }
236 }
237
238 if (subs->sync_endpoint &&
239 !test_and_set_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags)) {
240 struct snd_usb_endpoint *ep = subs->sync_endpoint;
241
Daniel Mack2e4a2632012-08-30 18:52:31 +0200242 if (subs->data_endpoint->iface != subs->sync_endpoint->iface ||
243 subs->data_endpoint->alt_idx != subs->sync_endpoint->alt_idx) {
244 err = usb_set_interface(subs->dev,
245 subs->sync_endpoint->iface,
246 subs->sync_endpoint->alt_idx);
247 if (err < 0) {
248 snd_printk(KERN_ERR
249 "%d:%d:%d: cannot set interface (%d)\n",
250 subs->dev->devnum,
251 subs->sync_endpoint->iface,
252 subs->sync_endpoint->alt_idx, err);
253 return -EIO;
254 }
255 }
256
Daniel Mackedcd3632012-04-12 13:51:12 +0200257 snd_printdd(KERN_DEBUG "Starting sync EP @%p\n", ep);
258
259 ep->sync_slave = subs->data_endpoint;
Daniel Mack015618b2012-08-29 13:17:05 +0200260 err = snd_usb_endpoint_start(ep, can_sleep);
Daniel Mackedcd3632012-04-12 13:51:12 +0200261 if (err < 0) {
262 clear_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags);
263 return err;
264 }
265 }
266
267 return 0;
268}
269
Takashi Iwaia9bb3622012-11-20 18:32:06 +0100270static void stop_endpoints(struct snd_usb_substream *subs, bool wait)
Daniel Mackedcd3632012-04-12 13:51:12 +0200271{
272 if (test_and_clear_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags))
Takashi Iwaib2eb9502012-11-21 08:30:48 +0100273 snd_usb_endpoint_stop(subs->sync_endpoint);
Daniel Mackedcd3632012-04-12 13:51:12 +0200274
275 if (test_and_clear_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags))
Takashi Iwaib2eb9502012-11-21 08:30:48 +0100276 snd_usb_endpoint_stop(subs->data_endpoint);
277
278 if (wait) {
279 snd_usb_endpoint_sync_pending_stop(subs->sync_endpoint);
280 snd_usb_endpoint_sync_pending_stop(subs->data_endpoint);
281 }
Daniel Mackedcd3632012-04-12 13:51:12 +0200282}
283
Daniel Mackedcd3632012-04-12 13:51:12 +0200284static int deactivate_endpoints(struct snd_usb_substream *subs)
285{
286 int reta, retb;
287
288 reta = snd_usb_endpoint_deactivate(subs->sync_endpoint);
289 retb = snd_usb_endpoint_deactivate(subs->data_endpoint);
290
291 if (reta < 0)
292 return reta;
293
294 if (retb < 0)
295 return retb;
296
297 return 0;
298}
299
Daniel Macke5779992010-03-04 19:46:13 +0100300/*
301 * find a matching format and set up the interface
302 */
303static int set_format(struct snd_usb_substream *subs, struct audioformat *fmt)
304{
305 struct usb_device *dev = subs->dev;
306 struct usb_host_interface *alts;
307 struct usb_interface_descriptor *altsd;
308 struct usb_interface *iface;
309 unsigned int ep, attr;
310 int is_playback = subs->direction == SNDRV_PCM_STREAM_PLAYBACK;
Daniel Mackc75a8a72012-04-12 13:51:14 +0200311 int err, implicit_fb = 0;
Daniel Macke5779992010-03-04 19:46:13 +0100312
313 iface = usb_ifnum_to_if(dev, fmt->iface);
314 if (WARN_ON(!iface))
315 return -EINVAL;
316 alts = &iface->altsetting[fmt->altset_idx];
317 altsd = get_iface_desc(alts);
318 if (WARN_ON(altsd->bAlternateSetting != fmt->altsetting))
319 return -EINVAL;
320
321 if (fmt == subs->cur_audiofmt)
322 return 0;
323
Daniel Mack68e67f42012-07-12 13:08:40 +0200324 /* close the old interface */
325 if (subs->interface >= 0 && subs->interface != fmt->iface) {
326 err = usb_set_interface(subs->dev, subs->interface, 0);
327 if (err < 0) {
328 snd_printk(KERN_ERR "%d:%d:%d: return to setting 0 failed (%d)\n",
329 dev->devnum, fmt->iface, fmt->altsetting, err);
330 return -EIO;
331 }
332 subs->interface = -1;
333 subs->altset_idx = 0;
334 }
335
336 /* set interface */
337 if (subs->interface != fmt->iface ||
338 subs->altset_idx != fmt->altset_idx) {
339 err = usb_set_interface(dev, fmt->iface, fmt->altsetting);
340 if (err < 0) {
341 snd_printk(KERN_ERR "%d:%d:%d: usb_set_interface failed (%d)\n",
342 dev->devnum, fmt->iface, fmt->altsetting, err);
343 return -EIO;
344 }
345 snd_printdd(KERN_INFO "setting usb interface %d:%d\n",
346 fmt->iface, fmt->altsetting);
347 subs->interface = fmt->iface;
348 subs->altset_idx = fmt->altset_idx;
349 }
350
Daniel Mackedcd3632012-04-12 13:51:12 +0200351 subs->data_endpoint = snd_usb_add_endpoint(subs->stream->chip,
352 alts, fmt->endpoint, subs->direction,
353 SND_USB_ENDPOINT_TYPE_DATA);
354 if (!subs->data_endpoint)
355 return -EINVAL;
Daniel Macke5779992010-03-04 19:46:13 +0100356
357 /* we need a sync pipe in async OUT or adaptive IN mode */
358 /* check the number of EP, since some devices have broken
359 * descriptors which fool us. if it has only one EP,
360 * assume it as adaptive-out or sync-in.
361 */
362 attr = fmt->ep_attr & USB_ENDPOINT_SYNCTYPE;
Daniel Mackc75a8a72012-04-12 13:51:14 +0200363
364 switch (subs->stream->chip->usb_id) {
Eldad Zackca10a7e2012-11-28 23:55:41 +0100365 case USB_ID(0x0763, 0x2030): /* M-Audio Fast Track C400 */
366 if (is_playback) {
367 implicit_fb = 1;
368 ep = 0x81;
369 iface = usb_ifnum_to_if(dev, 3);
370
371 if (!iface || iface->num_altsetting == 0)
372 return -EINVAL;
373
374 alts = &iface->altsetting[1];
375 goto add_sync_ep;
376 }
377 break;
Daniel Mackc75a8a72012-04-12 13:51:14 +0200378 case USB_ID(0x0763, 0x2080): /* M-Audio FastTrack Ultra */
379 case USB_ID(0x0763, 0x2081):
380 if (is_playback) {
381 implicit_fb = 1;
Daniel Mackedcd3632012-04-12 13:51:12 +0200382 ep = 0x81;
383 iface = usb_ifnum_to_if(dev, 2);
Daniel Mackc75a8a72012-04-12 13:51:14 +0200384
385 if (!iface || iface->num_altsetting == 0)
386 return -EINVAL;
387
Daniel Mackedcd3632012-04-12 13:51:12 +0200388 alts = &iface->altsetting[1];
389 goto add_sync_ep;
390 }
Daniel Mackc75a8a72012-04-12 13:51:14 +0200391 }
Daniel Mackedcd3632012-04-12 13:51:12 +0200392
Daniel Mackc75a8a72012-04-12 13:51:14 +0200393 if (((is_playback && attr == USB_ENDPOINT_SYNC_ASYNC) ||
394 (!is_playback && attr == USB_ENDPOINT_SYNC_ADAPTIVE)) &&
395 altsd->bNumEndpoints >= 2) {
Daniel Macke5779992010-03-04 19:46:13 +0100396 /* check sync-pipe endpoint */
397 /* ... and check descriptor size before accessing bSynchAddress
398 because there is a version of the SB Audigy 2 NX firmware lacking
399 the audio fields in the endpoint descriptors */
Eldad Zackfde854b2012-11-28 23:55:32 +0100400 if ((get_endpoint(alts, 1)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_ISOC ||
Daniel Macke5779992010-03-04 19:46:13 +0100401 (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
Daniel Mackc75a8a72012-04-12 13:51:14 +0200402 get_endpoint(alts, 1)->bSynchAddress != 0 &&
403 !implicit_fb)) {
Daniel Mack7fb75db2012-06-17 13:44:27 +0200404 snd_printk(KERN_ERR "%d:%d:%d : invalid sync pipe. bmAttributes %02x, bLength %d, bSynchAddress %02x\n",
405 dev->devnum, fmt->iface, fmt->altsetting,
406 get_endpoint(alts, 1)->bmAttributes,
407 get_endpoint(alts, 1)->bLength,
408 get_endpoint(alts, 1)->bSynchAddress);
Daniel Macke5779992010-03-04 19:46:13 +0100409 return -EINVAL;
410 }
411 ep = get_endpoint(alts, 1)->bEndpointAddress;
Daniel Mack7fb75db2012-06-17 13:44:27 +0200412 if (!implicit_fb &&
413 get_endpoint(alts, 0)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
Daniel Macke5779992010-03-04 19:46:13 +0100414 (( is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress | USB_DIR_IN)) ||
Daniel Mack7fb75db2012-06-17 13:44:27 +0200415 (!is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress & ~USB_DIR_IN)))) {
416 snd_printk(KERN_ERR "%d:%d:%d : invalid sync pipe. is_playback %d, ep %02x, bSynchAddress %02x\n",
417 dev->devnum, fmt->iface, fmt->altsetting,
418 is_playback, ep, get_endpoint(alts, 0)->bSynchAddress);
Daniel Macke5779992010-03-04 19:46:13 +0100419 return -EINVAL;
420 }
Daniel Mackc75a8a72012-04-12 13:51:14 +0200421
422 implicit_fb = (get_endpoint(alts, 1)->bmAttributes & USB_ENDPOINT_USAGE_MASK)
423 == USB_ENDPOINT_USAGE_IMPLICIT_FB;
424
Daniel Mackedcd3632012-04-12 13:51:12 +0200425add_sync_ep:
426 subs->sync_endpoint = snd_usb_add_endpoint(subs->stream->chip,
427 alts, ep, !subs->direction,
Daniel Mackc75a8a72012-04-12 13:51:14 +0200428 implicit_fb ?
429 SND_USB_ENDPOINT_TYPE_DATA :
430 SND_USB_ENDPOINT_TYPE_SYNC);
Daniel Mackedcd3632012-04-12 13:51:12 +0200431 if (!subs->sync_endpoint)
432 return -EINVAL;
433
434 subs->data_endpoint->sync_master = subs->sync_endpoint;
435 }
Daniel Macke5779992010-03-04 19:46:13 +0100436
Takashi Iwai9e9b5942012-07-06 08:11:43 +0200437 if ((err = snd_usb_init_pitch(subs->stream->chip, fmt->iface, alts, fmt)) < 0)
Daniel Macke5779992010-03-04 19:46:13 +0100438 return err;
439
440 subs->cur_audiofmt = fmt;
441
442 snd_usb_set_format_quirk(subs, fmt);
443
444#if 0
445 printk(KERN_DEBUG
446 "setting done: format = %d, rate = %d..%d, channels = %d\n",
447 fmt->format, fmt->rate_min, fmt->rate_max, fmt->channels);
448 printk(KERN_DEBUG
449 " datapipe = 0x%0x, syncpipe = 0x%0x\n",
450 subs->datapipe, subs->syncpipe);
451#endif
452
453 return 0;
454}
455
456/*
Eldad Zack0d9741c2012-12-03 20:30:09 +0100457 * Return the score of matching two audioformats.
458 * Veto the audioformat if:
459 * - It has no channels for some reason.
460 * - Requested PCM format is not supported.
461 * - Requested sample rate is not supported.
462 */
463static int match_endpoint_audioformats(struct audioformat *fp,
464 struct audioformat *match, int rate,
465 snd_pcm_format_t pcm_format)
466{
467 int i;
468 int score = 0;
469
470 if (fp->channels < 1) {
471 snd_printdd("%s: (fmt @%p) no channels\n", __func__, fp);
472 return 0;
473 }
474
475 if (!(fp->formats & (1ULL << pcm_format))) {
476 snd_printdd("%s: (fmt @%p) no match for format %d\n", __func__,
477 fp, pcm_format);
478 return 0;
479 }
480
481 for (i = 0; i < fp->nr_rates; i++) {
482 if (fp->rate_table[i] == rate) {
483 score++;
484 break;
485 }
486 }
487 if (!score) {
488 snd_printdd("%s: (fmt @%p) no match for rate %d\n", __func__,
489 fp, rate);
490 return 0;
491 }
492
493 if (fp->channels == match->channels)
494 score++;
495
496 snd_printdd("%s: (fmt @%p) score %d\n", __func__, fp, score);
497
498 return score;
499}
500
501/*
502 * Configure the sync ep using the rate and pcm format of the data ep.
503 */
504static int configure_sync_endpoint(struct snd_usb_substream *subs)
505{
506 int ret;
507 struct audioformat *fp;
508 struct audioformat *sync_fp = NULL;
509 int cur_score = 0;
510 int sync_period_bytes = subs->period_bytes;
511 struct snd_usb_substream *sync_subs =
512 &subs->stream->substream[subs->direction ^ 1];
513
Takashi Iwai31be5422013-01-10 14:06:38 +0100514 if (subs->sync_endpoint->type != SND_USB_ENDPOINT_TYPE_DATA ||
515 !subs->stream)
516 return snd_usb_endpoint_set_params(subs->sync_endpoint,
517 subs->pcm_format,
518 subs->channels,
519 subs->period_bytes,
520 subs->cur_rate,
521 subs->cur_audiofmt,
522 NULL);
523
Eldad Zack0d9741c2012-12-03 20:30:09 +0100524 /* Try to find the best matching audioformat. */
525 list_for_each_entry(fp, &sync_subs->fmt_list, list) {
526 int score = match_endpoint_audioformats(fp, subs->cur_audiofmt,
527 subs->cur_rate, subs->pcm_format);
528
529 if (score > cur_score) {
530 sync_fp = fp;
531 cur_score = score;
532 }
533 }
534
535 if (unlikely(sync_fp == NULL)) {
536 snd_printk(KERN_ERR "%s: no valid audioformat for sync ep %x found\n",
537 __func__, sync_subs->ep_num);
538 return -EINVAL;
539 }
540
541 /*
542 * Recalculate the period bytes if channel number differ between
543 * data and sync ep audioformat.
544 */
545 if (sync_fp->channels != subs->channels) {
546 sync_period_bytes = (subs->period_bytes / subs->channels) *
547 sync_fp->channels;
548 snd_printdd("%s: adjusted sync ep period bytes (%d -> %d)\n",
549 __func__, subs->period_bytes, sync_period_bytes);
550 }
551
552 ret = snd_usb_endpoint_set_params(subs->sync_endpoint,
553 subs->pcm_format,
554 sync_fp->channels,
555 sync_period_bytes,
556 subs->cur_rate,
557 sync_fp,
558 NULL);
559
560 return ret;
561}
562
563/*
Dylan Reid61a70952012-09-18 09:49:48 -0700564 * configure endpoint params
565 *
566 * called during initial setup and upon resume
567 */
568static int configure_endpoint(struct snd_usb_substream *subs)
569{
570 int ret;
571
Dylan Reid61a70952012-09-18 09:49:48 -0700572 /* format changed */
Takashi Iwaib0db6062012-11-21 08:35:42 +0100573 stop_endpoints(subs, true);
Dylan Reid61a70952012-09-18 09:49:48 -0700574 ret = snd_usb_endpoint_set_params(subs->data_endpoint,
575 subs->pcm_format,
576 subs->channels,
577 subs->period_bytes,
578 subs->cur_rate,
579 subs->cur_audiofmt,
580 subs->sync_endpoint);
581 if (ret < 0)
Takashi Iwai978520b2012-10-12 15:12:55 +0200582 return ret;
Dylan Reid61a70952012-09-18 09:49:48 -0700583
584 if (subs->sync_endpoint)
Eldad Zack0d9741c2012-12-03 20:30:09 +0100585 ret = configure_sync_endpoint(subs);
586
Dylan Reid61a70952012-09-18 09:49:48 -0700587 return ret;
588}
589
590/*
Daniel Macke5779992010-03-04 19:46:13 +0100591 * hw_params callback
592 *
593 * allocate a buffer and set the given audio format.
594 *
595 * so far we use a physically linear buffer although packetize transfer
596 * doesn't need a continuous area.
597 * if sg buffer is supported on the later version of alsa, we'll follow
598 * that.
599 */
600static int snd_usb_hw_params(struct snd_pcm_substream *substream,
601 struct snd_pcm_hw_params *hw_params)
602{
603 struct snd_usb_substream *subs = substream->runtime->private_data;
604 struct audioformat *fmt;
Dylan Reid61a70952012-09-18 09:49:48 -0700605 int ret;
Daniel Macke5779992010-03-04 19:46:13 +0100606
607 ret = snd_pcm_lib_alloc_vmalloc_buffer(substream,
608 params_buffer_bytes(hw_params));
609 if (ret < 0)
610 return ret;
611
Dylan Reid61a70952012-09-18 09:49:48 -0700612 subs->pcm_format = params_format(hw_params);
613 subs->period_bytes = params_period_bytes(hw_params);
614 subs->channels = params_channels(hw_params);
615 subs->cur_rate = params_rate(hw_params);
616
617 fmt = find_format(subs);
Daniel Macke5779992010-03-04 19:46:13 +0100618 if (!fmt) {
619 snd_printd(KERN_DEBUG "cannot set format: format = %#x, rate = %d, channels = %d\n",
Dylan Reid61a70952012-09-18 09:49:48 -0700620 subs->pcm_format, subs->cur_rate, subs->channels);
Daniel Macke5779992010-03-04 19:46:13 +0100621 return -EINVAL;
622 }
623
Takashi Iwai34f3c892012-10-15 12:16:02 +0200624 down_read(&subs->stream->chip->shutdown_rwsem);
Takashi Iwai978520b2012-10-12 15:12:55 +0200625 if (subs->stream->chip->shutdown)
626 ret = -ENODEV;
627 else
628 ret = set_format(subs, fmt);
Takashi Iwai34f3c892012-10-15 12:16:02 +0200629 up_read(&subs->stream->chip->shutdown_rwsem);
Takashi Iwai978520b2012-10-12 15:12:55 +0200630 if (ret < 0)
Daniel Macke5779992010-03-04 19:46:13 +0100631 return ret;
632
Dylan Reid61a70952012-09-18 09:49:48 -0700633 subs->interface = fmt->iface;
634 subs->altset_idx = fmt->altset_idx;
Takashi Iwai384dc0852012-09-18 14:49:31 +0200635 subs->need_setup_ep = true;
Daniel Macke5779992010-03-04 19:46:13 +0100636
Dylan Reid61a70952012-09-18 09:49:48 -0700637 return 0;
Daniel Macke5779992010-03-04 19:46:13 +0100638}
639
640/*
641 * hw_free callback
642 *
643 * reset the audio format and release the buffer
644 */
645static int snd_usb_hw_free(struct snd_pcm_substream *substream)
646{
647 struct snd_usb_substream *subs = substream->runtime->private_data;
648
649 subs->cur_audiofmt = NULL;
650 subs->cur_rate = 0;
651 subs->period_bytes = 0;
Takashi Iwai34f3c892012-10-15 12:16:02 +0200652 down_read(&subs->stream->chip->shutdown_rwsem);
Takashi Iwai978520b2012-10-12 15:12:55 +0200653 if (!subs->stream->chip->shutdown) {
Takashi Iwaia9bb3622012-11-20 18:32:06 +0100654 stop_endpoints(subs, true);
Takashi Iwai978520b2012-10-12 15:12:55 +0200655 deactivate_endpoints(subs);
656 }
Takashi Iwai34f3c892012-10-15 12:16:02 +0200657 up_read(&subs->stream->chip->shutdown_rwsem);
Daniel Macke5779992010-03-04 19:46:13 +0100658 return snd_pcm_lib_free_vmalloc_buffer(substream);
659}
660
661/*
662 * prepare callback
663 *
664 * only a few subtle things...
665 */
666static int snd_usb_pcm_prepare(struct snd_pcm_substream *substream)
667{
668 struct snd_pcm_runtime *runtime = substream->runtime;
669 struct snd_usb_substream *subs = runtime->private_data;
Dylan Reid61a70952012-09-18 09:49:48 -0700670 struct usb_host_interface *alts;
671 struct usb_interface *iface;
672 int ret;
Daniel Macke5779992010-03-04 19:46:13 +0100673
674 if (! subs->cur_audiofmt) {
675 snd_printk(KERN_ERR "usbaudio: no format is specified!\n");
676 return -ENXIO;
677 }
678
Takashi Iwai34f3c892012-10-15 12:16:02 +0200679 down_read(&subs->stream->chip->shutdown_rwsem);
Takashi Iwai978520b2012-10-12 15:12:55 +0200680 if (subs->stream->chip->shutdown) {
681 ret = -ENODEV;
682 goto unlock;
683 }
684 if (snd_BUG_ON(!subs->data_endpoint)) {
685 ret = -EIO;
686 goto unlock;
687 }
Daniel Mackedcd3632012-04-12 13:51:12 +0200688
Takashi Iwaif58161b2012-11-08 08:52:45 +0100689 snd_usb_endpoint_sync_pending_stop(subs->sync_endpoint);
690 snd_usb_endpoint_sync_pending_stop(subs->data_endpoint);
691
Dylan Reid61a70952012-09-18 09:49:48 -0700692 ret = set_format(subs, subs->cur_audiofmt);
693 if (ret < 0)
Takashi Iwai978520b2012-10-12 15:12:55 +0200694 goto unlock;
Dylan Reid61a70952012-09-18 09:49:48 -0700695
696 iface = usb_ifnum_to_if(subs->dev, subs->cur_audiofmt->iface);
697 alts = &iface->altsetting[subs->cur_audiofmt->altset_idx];
698 ret = snd_usb_init_sample_rate(subs->stream->chip,
699 subs->cur_audiofmt->iface,
700 alts,
701 subs->cur_audiofmt,
702 subs->cur_rate);
703 if (ret < 0)
Takashi Iwai978520b2012-10-12 15:12:55 +0200704 goto unlock;
Dylan Reid61a70952012-09-18 09:49:48 -0700705
Takashi Iwai384dc0852012-09-18 14:49:31 +0200706 if (subs->need_setup_ep) {
707 ret = configure_endpoint(subs);
708 if (ret < 0)
Takashi Iwai978520b2012-10-12 15:12:55 +0200709 goto unlock;
Takashi Iwai384dc0852012-09-18 14:49:31 +0200710 subs->need_setup_ep = false;
711 }
Dylan Reid61a70952012-09-18 09:49:48 -0700712
Daniel Macke5779992010-03-04 19:46:13 +0100713 /* some unit conversions in runtime */
Daniel Mackedcd3632012-04-12 13:51:12 +0200714 subs->data_endpoint->maxframesize =
715 bytes_to_frames(runtime, subs->data_endpoint->maxpacksize);
716 subs->data_endpoint->curframesize =
717 bytes_to_frames(runtime, subs->data_endpoint->curpacksize);
Daniel Macke5779992010-03-04 19:46:13 +0100718
719 /* reset the pointer */
720 subs->hwptr_done = 0;
721 subs->transfer_done = 0;
Pierre-Louis Bossart294c4fb2011-09-06 19:15:34 -0500722 subs->last_delay = 0;
723 subs->last_frame_number = 0;
Daniel Macke5779992010-03-04 19:46:13 +0100724 runtime->delay = 0;
725
Daniel Mackedcd3632012-04-12 13:51:12 +0200726 /* for playback, submit the URBs now; otherwise, the first hwptr_done
727 * updates for all URBs would happen at the same time when starting */
728 if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK)
Takashi Iwaia9bb3622012-11-20 18:32:06 +0100729 ret = start_endpoints(subs, true);
Daniel Mackedcd3632012-04-12 13:51:12 +0200730
Takashi Iwai978520b2012-10-12 15:12:55 +0200731 unlock:
Takashi Iwai34f3c892012-10-15 12:16:02 +0200732 up_read(&subs->stream->chip->shutdown_rwsem);
Takashi Iwai978520b2012-10-12 15:12:55 +0200733 return ret;
Daniel Macke5779992010-03-04 19:46:13 +0100734}
735
736static struct snd_pcm_hardware snd_usb_hardware =
737{
738 .info = SNDRV_PCM_INFO_MMAP |
739 SNDRV_PCM_INFO_MMAP_VALID |
740 SNDRV_PCM_INFO_BATCH |
741 SNDRV_PCM_INFO_INTERLEAVED |
742 SNDRV_PCM_INFO_BLOCK_TRANSFER |
743 SNDRV_PCM_INFO_PAUSE,
744 .buffer_bytes_max = 1024 * 1024,
745 .period_bytes_min = 64,
746 .period_bytes_max = 512 * 1024,
747 .periods_min = 2,
748 .periods_max = 1024,
749};
750
751static int hw_check_valid_format(struct snd_usb_substream *subs,
752 struct snd_pcm_hw_params *params,
753 struct audioformat *fp)
754{
755 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
756 struct snd_interval *ct = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
757 struct snd_mask *fmts = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
758 struct snd_interval *pt = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
Clemens Ladisch015eb0b2010-03-04 19:46:15 +0100759 struct snd_mask check_fmts;
Daniel Macke5779992010-03-04 19:46:13 +0100760 unsigned int ptime;
761
762 /* check the format */
Clemens Ladisch015eb0b2010-03-04 19:46:15 +0100763 snd_mask_none(&check_fmts);
764 check_fmts.bits[0] = (u32)fp->formats;
765 check_fmts.bits[1] = (u32)(fp->formats >> 32);
766 snd_mask_intersect(&check_fmts, fmts);
767 if (snd_mask_empty(&check_fmts)) {
Daniel Macke5779992010-03-04 19:46:13 +0100768 hwc_debug(" > check: no supported format %d\n", fp->format);
769 return 0;
770 }
771 /* check the channels */
772 if (fp->channels < ct->min || fp->channels > ct->max) {
773 hwc_debug(" > check: no valid channels %d (%d/%d)\n", fp->channels, ct->min, ct->max);
774 return 0;
775 }
776 /* check the rate is within the range */
777 if (fp->rate_min > it->max || (fp->rate_min == it->max && it->openmax)) {
778 hwc_debug(" > check: rate_min %d > max %d\n", fp->rate_min, it->max);
779 return 0;
780 }
781 if (fp->rate_max < it->min || (fp->rate_max == it->min && it->openmin)) {
782 hwc_debug(" > check: rate_max %d < min %d\n", fp->rate_max, it->min);
783 return 0;
784 }
785 /* check whether the period time is >= the data packet interval */
Takashi Iwai978520b2012-10-12 15:12:55 +0200786 if (subs->speed != USB_SPEED_FULL) {
Daniel Macke5779992010-03-04 19:46:13 +0100787 ptime = 125 * (1 << fp->datainterval);
788 if (ptime > pt->max || (ptime == pt->max && pt->openmax)) {
789 hwc_debug(" > check: ptime %u > max %u\n", ptime, pt->max);
790 return 0;
791 }
792 }
793 return 1;
794}
795
796static int hw_rule_rate(struct snd_pcm_hw_params *params,
797 struct snd_pcm_hw_rule *rule)
798{
799 struct snd_usb_substream *subs = rule->private;
800 struct list_head *p;
801 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
802 unsigned int rmin, rmax;
803 int changed;
804
805 hwc_debug("hw_rule_rate: (%d,%d)\n", it->min, it->max);
806 changed = 0;
807 rmin = rmax = 0;
808 list_for_each(p, &subs->fmt_list) {
809 struct audioformat *fp;
810 fp = list_entry(p, struct audioformat, list);
811 if (!hw_check_valid_format(subs, params, fp))
812 continue;
813 if (changed++) {
814 if (rmin > fp->rate_min)
815 rmin = fp->rate_min;
816 if (rmax < fp->rate_max)
817 rmax = fp->rate_max;
818 } else {
819 rmin = fp->rate_min;
820 rmax = fp->rate_max;
821 }
822 }
823
824 if (!changed) {
825 hwc_debug(" --> get empty\n");
826 it->empty = 1;
827 return -EINVAL;
828 }
829
830 changed = 0;
831 if (it->min < rmin) {
832 it->min = rmin;
833 it->openmin = 0;
834 changed = 1;
835 }
836 if (it->max > rmax) {
837 it->max = rmax;
838 it->openmax = 0;
839 changed = 1;
840 }
841 if (snd_interval_checkempty(it)) {
842 it->empty = 1;
843 return -EINVAL;
844 }
845 hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
846 return changed;
847}
848
849
850static int hw_rule_channels(struct snd_pcm_hw_params *params,
851 struct snd_pcm_hw_rule *rule)
852{
853 struct snd_usb_substream *subs = rule->private;
854 struct list_head *p;
855 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
856 unsigned int rmin, rmax;
857 int changed;
858
859 hwc_debug("hw_rule_channels: (%d,%d)\n", it->min, it->max);
860 changed = 0;
861 rmin = rmax = 0;
862 list_for_each(p, &subs->fmt_list) {
863 struct audioformat *fp;
864 fp = list_entry(p, struct audioformat, list);
865 if (!hw_check_valid_format(subs, params, fp))
866 continue;
867 if (changed++) {
868 if (rmin > fp->channels)
869 rmin = fp->channels;
870 if (rmax < fp->channels)
871 rmax = fp->channels;
872 } else {
873 rmin = fp->channels;
874 rmax = fp->channels;
875 }
876 }
877
878 if (!changed) {
879 hwc_debug(" --> get empty\n");
880 it->empty = 1;
881 return -EINVAL;
882 }
883
884 changed = 0;
885 if (it->min < rmin) {
886 it->min = rmin;
887 it->openmin = 0;
888 changed = 1;
889 }
890 if (it->max > rmax) {
891 it->max = rmax;
892 it->openmax = 0;
893 changed = 1;
894 }
895 if (snd_interval_checkempty(it)) {
896 it->empty = 1;
897 return -EINVAL;
898 }
899 hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
900 return changed;
901}
902
903static int hw_rule_format(struct snd_pcm_hw_params *params,
904 struct snd_pcm_hw_rule *rule)
905{
906 struct snd_usb_substream *subs = rule->private;
907 struct list_head *p;
908 struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
909 u64 fbits;
910 u32 oldbits[2];
911 int changed;
912
913 hwc_debug("hw_rule_format: %x:%x\n", fmt->bits[0], fmt->bits[1]);
914 fbits = 0;
915 list_for_each(p, &subs->fmt_list) {
916 struct audioformat *fp;
917 fp = list_entry(p, struct audioformat, list);
918 if (!hw_check_valid_format(subs, params, fp))
919 continue;
Clemens Ladisch015eb0b2010-03-04 19:46:15 +0100920 fbits |= fp->formats;
Daniel Macke5779992010-03-04 19:46:13 +0100921 }
922
923 oldbits[0] = fmt->bits[0];
924 oldbits[1] = fmt->bits[1];
925 fmt->bits[0] &= (u32)fbits;
926 fmt->bits[1] &= (u32)(fbits >> 32);
927 if (!fmt->bits[0] && !fmt->bits[1]) {
928 hwc_debug(" --> get empty\n");
929 return -EINVAL;
930 }
931 changed = (oldbits[0] != fmt->bits[0] || oldbits[1] != fmt->bits[1]);
932 hwc_debug(" --> %x:%x (changed = %d)\n", fmt->bits[0], fmt->bits[1], changed);
933 return changed;
934}
935
936static int hw_rule_period_time(struct snd_pcm_hw_params *params,
937 struct snd_pcm_hw_rule *rule)
938{
939 struct snd_usb_substream *subs = rule->private;
940 struct audioformat *fp;
941 struct snd_interval *it;
942 unsigned char min_datainterval;
943 unsigned int pmin;
944 int changed;
945
946 it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
947 hwc_debug("hw_rule_period_time: (%u,%u)\n", it->min, it->max);
948 min_datainterval = 0xff;
949 list_for_each_entry(fp, &subs->fmt_list, list) {
950 if (!hw_check_valid_format(subs, params, fp))
951 continue;
952 min_datainterval = min(min_datainterval, fp->datainterval);
953 }
954 if (min_datainterval == 0xff) {
Uwe Kleine-Königa7ce2e02010-07-12 17:15:44 +0200955 hwc_debug(" --> get empty\n");
Daniel Macke5779992010-03-04 19:46:13 +0100956 it->empty = 1;
957 return -EINVAL;
958 }
959 pmin = 125 * (1 << min_datainterval);
960 changed = 0;
961 if (it->min < pmin) {
962 it->min = pmin;
963 it->openmin = 0;
964 changed = 1;
965 }
966 if (snd_interval_checkempty(it)) {
967 it->empty = 1;
968 return -EINVAL;
969 }
970 hwc_debug(" --> (%u,%u) (changed = %d)\n", it->min, it->max, changed);
971 return changed;
972}
973
974/*
975 * If the device supports unusual bit rates, does the request meet these?
976 */
977static int snd_usb_pcm_check_knot(struct snd_pcm_runtime *runtime,
978 struct snd_usb_substream *subs)
979{
980 struct audioformat *fp;
Takashi Iwai0717d0f2012-03-15 16:14:38 +0100981 int *rate_list;
Daniel Macke5779992010-03-04 19:46:13 +0100982 int count = 0, needs_knot = 0;
983 int err;
984
Clemens Ladisch5cd5d7c2012-05-18 18:00:43 +0200985 kfree(subs->rate_list.list);
986 subs->rate_list.list = NULL;
987
Daniel Macke5779992010-03-04 19:46:13 +0100988 list_for_each_entry(fp, &subs->fmt_list, list) {
989 if (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)
990 return 0;
991 count += fp->nr_rates;
992 if (fp->rates & SNDRV_PCM_RATE_KNOT)
993 needs_knot = 1;
994 }
995 if (!needs_knot)
996 return 0;
997
Takashi Iwai0717d0f2012-03-15 16:14:38 +0100998 subs->rate_list.list = rate_list =
999 kmalloc(sizeof(int) * count, GFP_KERNEL);
Jesper Juhl8a8d56b2010-10-29 20:40:23 +02001000 if (!subs->rate_list.list)
1001 return -ENOMEM;
1002 subs->rate_list.count = count;
Daniel Macke5779992010-03-04 19:46:13 +01001003 subs->rate_list.mask = 0;
1004 count = 0;
1005 list_for_each_entry(fp, &subs->fmt_list, list) {
1006 int i;
1007 for (i = 0; i < fp->nr_rates; i++)
Takashi Iwai0717d0f2012-03-15 16:14:38 +01001008 rate_list[count++] = fp->rate_table[i];
Daniel Macke5779992010-03-04 19:46:13 +01001009 }
1010 err = snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1011 &subs->rate_list);
1012 if (err < 0)
1013 return err;
1014
1015 return 0;
1016}
1017
1018
1019/*
1020 * set up the runtime hardware information.
1021 */
1022
1023static int setup_hw_info(struct snd_pcm_runtime *runtime, struct snd_usb_substream *subs)
1024{
1025 struct list_head *p;
1026 unsigned int pt, ptmin;
1027 int param_period_time_if_needed;
1028 int err;
1029
1030 runtime->hw.formats = subs->formats;
1031
1032 runtime->hw.rate_min = 0x7fffffff;
1033 runtime->hw.rate_max = 0;
1034 runtime->hw.channels_min = 256;
1035 runtime->hw.channels_max = 0;
1036 runtime->hw.rates = 0;
1037 ptmin = UINT_MAX;
1038 /* check min/max rates and channels */
1039 list_for_each(p, &subs->fmt_list) {
1040 struct audioformat *fp;
1041 fp = list_entry(p, struct audioformat, list);
1042 runtime->hw.rates |= fp->rates;
1043 if (runtime->hw.rate_min > fp->rate_min)
1044 runtime->hw.rate_min = fp->rate_min;
1045 if (runtime->hw.rate_max < fp->rate_max)
1046 runtime->hw.rate_max = fp->rate_max;
1047 if (runtime->hw.channels_min > fp->channels)
1048 runtime->hw.channels_min = fp->channels;
1049 if (runtime->hw.channels_max < fp->channels)
1050 runtime->hw.channels_max = fp->channels;
1051 if (fp->fmt_type == UAC_FORMAT_TYPE_II && fp->frame_size > 0) {
1052 /* FIXME: there might be more than one audio formats... */
1053 runtime->hw.period_bytes_min = runtime->hw.period_bytes_max =
1054 fp->frame_size;
1055 }
1056 pt = 125 * (1 << fp->datainterval);
1057 ptmin = min(ptmin, pt);
1058 }
Oliver Neukum88a85162011-03-11 14:51:12 +01001059 err = snd_usb_autoresume(subs->stream->chip);
1060 if (err < 0)
1061 return err;
Daniel Macke5779992010-03-04 19:46:13 +01001062
1063 param_period_time_if_needed = SNDRV_PCM_HW_PARAM_PERIOD_TIME;
Takashi Iwai978520b2012-10-12 15:12:55 +02001064 if (subs->speed == USB_SPEED_FULL)
Daniel Macke5779992010-03-04 19:46:13 +01001065 /* full speed devices have fixed data packet interval */
1066 ptmin = 1000;
1067 if (ptmin == 1000)
1068 /* if period time doesn't go below 1 ms, no rules needed */
1069 param_period_time_if_needed = -1;
1070 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1071 ptmin, UINT_MAX);
1072
1073 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1074 hw_rule_rate, subs,
1075 SNDRV_PCM_HW_PARAM_FORMAT,
1076 SNDRV_PCM_HW_PARAM_CHANNELS,
1077 param_period_time_if_needed,
1078 -1)) < 0)
Oliver Neukum88a85162011-03-11 14:51:12 +01001079 goto rep_err;
Daniel Macke5779992010-03-04 19:46:13 +01001080 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
1081 hw_rule_channels, subs,
1082 SNDRV_PCM_HW_PARAM_FORMAT,
1083 SNDRV_PCM_HW_PARAM_RATE,
1084 param_period_time_if_needed,
1085 -1)) < 0)
Oliver Neukum88a85162011-03-11 14:51:12 +01001086 goto rep_err;
Daniel Macke5779992010-03-04 19:46:13 +01001087 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
1088 hw_rule_format, subs,
1089 SNDRV_PCM_HW_PARAM_RATE,
1090 SNDRV_PCM_HW_PARAM_CHANNELS,
1091 param_period_time_if_needed,
1092 -1)) < 0)
Oliver Neukum88a85162011-03-11 14:51:12 +01001093 goto rep_err;
Daniel Macke5779992010-03-04 19:46:13 +01001094 if (param_period_time_if_needed >= 0) {
1095 err = snd_pcm_hw_rule_add(runtime, 0,
1096 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1097 hw_rule_period_time, subs,
1098 SNDRV_PCM_HW_PARAM_FORMAT,
1099 SNDRV_PCM_HW_PARAM_CHANNELS,
1100 SNDRV_PCM_HW_PARAM_RATE,
1101 -1);
1102 if (err < 0)
Oliver Neukum88a85162011-03-11 14:51:12 +01001103 goto rep_err;
Daniel Macke5779992010-03-04 19:46:13 +01001104 }
1105 if ((err = snd_usb_pcm_check_knot(runtime, subs)) < 0)
Oliver Neukum88a85162011-03-11 14:51:12 +01001106 goto rep_err;
Daniel Macke5779992010-03-04 19:46:13 +01001107 return 0;
Oliver Neukum88a85162011-03-11 14:51:12 +01001108
1109rep_err:
1110 snd_usb_autosuspend(subs->stream->chip);
1111 return err;
Daniel Macke5779992010-03-04 19:46:13 +01001112}
1113
1114static int snd_usb_pcm_open(struct snd_pcm_substream *substream, int direction)
1115{
1116 struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
1117 struct snd_pcm_runtime *runtime = substream->runtime;
1118 struct snd_usb_substream *subs = &as->substream[direction];
1119
1120 subs->interface = -1;
Clemens Ladische11b4e02010-03-04 19:46:14 +01001121 subs->altset_idx = 0;
Daniel Macke5779992010-03-04 19:46:13 +01001122 runtime->hw = snd_usb_hardware;
1123 runtime->private_data = subs;
1124 subs->pcm_substream = substream;
Oliver Neukum88a85162011-03-11 14:51:12 +01001125 /* runtime PM is also done there */
Daniel Macke5779992010-03-04 19:46:13 +01001126 return setup_hw_info(runtime, subs);
1127}
1128
1129static int snd_usb_pcm_close(struct snd_pcm_substream *substream, int direction)
1130{
1131 struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
1132 struct snd_usb_substream *subs = &as->substream[direction];
1133
Takashi Iwaib0db6062012-11-21 08:35:42 +01001134 stop_endpoints(subs, true);
Daniel Mack68e67f42012-07-12 13:08:40 +02001135
1136 if (!as->chip->shutdown && subs->interface >= 0) {
1137 usb_set_interface(subs->dev, subs->interface, 0);
1138 subs->interface = -1;
1139 }
1140
Daniel Macke5779992010-03-04 19:46:13 +01001141 subs->pcm_substream = NULL;
Oliver Neukum88a85162011-03-11 14:51:12 +01001142 snd_usb_autosuspend(subs->stream->chip);
Daniel Mackedcd3632012-04-12 13:51:12 +02001143
Daniel Mack68e67f42012-07-12 13:08:40 +02001144 return 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001145}
1146
1147/* Since a URB can handle only a single linear buffer, we must use double
1148 * buffering when the data to be transferred overflows the buffer boundary.
1149 * To avoid inconsistencies when updating hwptr_done, we use double buffering
1150 * for all URBs.
1151 */
1152static void retire_capture_urb(struct snd_usb_substream *subs,
1153 struct urb *urb)
1154{
1155 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1156 unsigned int stride, frames, bytes, oldptr;
1157 int i, period_elapsed = 0;
1158 unsigned long flags;
1159 unsigned char *cp;
1160
1161 stride = runtime->frame_bits >> 3;
1162
1163 for (i = 0; i < urb->number_of_packets; i++) {
1164 cp = (unsigned char *)urb->transfer_buffer + urb->iso_frame_desc[i].offset;
1165 if (urb->iso_frame_desc[i].status && printk_ratelimit()) {
1166 snd_printdd(KERN_ERR "frame %d active: %d\n", i, urb->iso_frame_desc[i].status);
1167 // continue;
1168 }
1169 bytes = urb->iso_frame_desc[i].actual_length;
1170 frames = bytes / stride;
1171 if (!subs->txfr_quirk)
1172 bytes = frames * stride;
1173 if (bytes % (runtime->sample_bits >> 3) != 0) {
1174#ifdef CONFIG_SND_DEBUG_VERBOSE
1175 int oldbytes = bytes;
1176#endif
1177 bytes = frames * stride;
1178 snd_printdd(KERN_ERR "Corrected urb data len. %d->%d\n",
1179 oldbytes, bytes);
1180 }
1181 /* update the current pointer */
1182 spin_lock_irqsave(&subs->lock, flags);
1183 oldptr = subs->hwptr_done;
1184 subs->hwptr_done += bytes;
1185 if (subs->hwptr_done >= runtime->buffer_size * stride)
1186 subs->hwptr_done -= runtime->buffer_size * stride;
1187 frames = (bytes + (oldptr % stride)) / stride;
1188 subs->transfer_done += frames;
1189 if (subs->transfer_done >= runtime->period_size) {
1190 subs->transfer_done -= runtime->period_size;
1191 period_elapsed = 1;
1192 }
1193 spin_unlock_irqrestore(&subs->lock, flags);
1194 /* copy a data chunk */
1195 if (oldptr + bytes > runtime->buffer_size * stride) {
1196 unsigned int bytes1 =
1197 runtime->buffer_size * stride - oldptr;
1198 memcpy(runtime->dma_area + oldptr, cp, bytes1);
1199 memcpy(runtime->dma_area, cp + bytes1, bytes - bytes1);
1200 } else {
1201 memcpy(runtime->dma_area + oldptr, cp, bytes);
1202 }
1203 }
1204
1205 if (period_elapsed)
1206 snd_pcm_period_elapsed(subs->pcm_substream);
1207}
1208
1209static void prepare_playback_urb(struct snd_usb_substream *subs,
1210 struct urb *urb)
1211{
1212 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
Daniel Mack245baf92012-08-30 18:52:30 +02001213 struct snd_usb_endpoint *ep = subs->data_endpoint;
Daniel Mackedcd3632012-04-12 13:51:12 +02001214 struct snd_urb_ctx *ctx = urb->context;
1215 unsigned int counts, frames, bytes;
1216 int i, stride, period_elapsed = 0;
1217 unsigned long flags;
1218
1219 stride = runtime->frame_bits >> 3;
1220
1221 frames = 0;
1222 urb->number_of_packets = 0;
1223 spin_lock_irqsave(&subs->lock, flags);
1224 for (i = 0; i < ctx->packets; i++) {
Daniel Mack245baf92012-08-30 18:52:30 +02001225 if (ctx->packet_size[i])
1226 counts = ctx->packet_size[i];
1227 else
1228 counts = snd_usb_endpoint_next_packet_size(ep);
1229
Daniel Mackedcd3632012-04-12 13:51:12 +02001230 /* set up descriptor */
1231 urb->iso_frame_desc[i].offset = frames * stride;
1232 urb->iso_frame_desc[i].length = counts * stride;
1233 frames += counts;
1234 urb->number_of_packets++;
1235 subs->transfer_done += counts;
1236 if (subs->transfer_done >= runtime->period_size) {
1237 subs->transfer_done -= runtime->period_size;
1238 period_elapsed = 1;
1239 if (subs->fmt_type == UAC_FORMAT_TYPE_II) {
1240 if (subs->transfer_done > 0) {
1241 /* FIXME: fill-max mode is not
1242 * supported yet */
1243 frames -= subs->transfer_done;
1244 counts -= subs->transfer_done;
1245 urb->iso_frame_desc[i].length =
1246 counts * stride;
1247 subs->transfer_done = 0;
1248 }
1249 i++;
1250 if (i < ctx->packets) {
1251 /* add a transfer delimiter */
1252 urb->iso_frame_desc[i].offset =
1253 frames * stride;
1254 urb->iso_frame_desc[i].length = 0;
1255 urb->number_of_packets++;
1256 }
1257 break;
1258 }
1259 }
1260 if (period_elapsed &&
1261 !snd_usb_endpoint_implict_feedback_sink(subs->data_endpoint)) /* finish at the period boundary */
1262 break;
1263 }
1264 bytes = frames * stride;
1265 if (subs->hwptr_done + bytes > runtime->buffer_size * stride) {
1266 /* err, the transferred area goes over buffer boundary. */
1267 unsigned int bytes1 =
1268 runtime->buffer_size * stride - subs->hwptr_done;
1269 memcpy(urb->transfer_buffer,
1270 runtime->dma_area + subs->hwptr_done, bytes1);
1271 memcpy(urb->transfer_buffer + bytes1,
1272 runtime->dma_area, bytes - bytes1);
1273 } else {
1274 memcpy(urb->transfer_buffer,
1275 runtime->dma_area + subs->hwptr_done, bytes);
1276 }
1277 subs->hwptr_done += bytes;
1278 if (subs->hwptr_done >= runtime->buffer_size * stride)
1279 subs->hwptr_done -= runtime->buffer_size * stride;
Daniel Mackfbcfbf52012-08-30 18:52:29 +02001280
1281 /* update delay with exact number of samples queued */
1282 runtime->delay = subs->last_delay;
Daniel Mackedcd3632012-04-12 13:51:12 +02001283 runtime->delay += frames;
Daniel Mackfbcfbf52012-08-30 18:52:29 +02001284 subs->last_delay = runtime->delay;
1285
1286 /* realign last_frame_number */
1287 subs->last_frame_number = usb_get_current_frame_number(subs->dev);
1288 subs->last_frame_number &= 0xFF; /* keep 8 LSBs */
1289
Daniel Mackedcd3632012-04-12 13:51:12 +02001290 spin_unlock_irqrestore(&subs->lock, flags);
1291 urb->transfer_buffer_length = bytes;
1292 if (period_elapsed)
1293 snd_pcm_period_elapsed(subs->pcm_substream);
1294}
1295
1296/*
1297 * process after playback data complete
1298 * - decrease the delay count again
1299 */
1300static void retire_playback_urb(struct snd_usb_substream *subs,
1301 struct urb *urb)
1302{
1303 unsigned long flags;
1304 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1305 int stride = runtime->frame_bits >> 3;
1306 int processed = urb->transfer_buffer_length / stride;
Daniel Mackfbcfbf52012-08-30 18:52:29 +02001307 int est_delay;
Daniel Mackedcd3632012-04-12 13:51:12 +02001308
Takashi Iwai1213a202012-09-06 14:58:00 +02001309 /* ignore the delay accounting when procssed=0 is given, i.e.
1310 * silent payloads are procssed before handling the actual data
1311 */
1312 if (!processed)
1313 return;
1314
Daniel Mackedcd3632012-04-12 13:51:12 +02001315 spin_lock_irqsave(&subs->lock, flags);
Takashi Iwai48779a02012-11-23 16:00:37 +01001316 if (!subs->last_delay)
1317 goto out; /* short path */
1318
Daniel Mackfbcfbf52012-08-30 18:52:29 +02001319 est_delay = snd_usb_pcm_delay(subs, runtime->rate);
1320 /* update delay with exact number of samples played */
1321 if (processed > subs->last_delay)
1322 subs->last_delay = 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001323 else
Daniel Mackfbcfbf52012-08-30 18:52:29 +02001324 subs->last_delay -= processed;
1325 runtime->delay = subs->last_delay;
1326
1327 /*
1328 * Report when delay estimate is off by more than 2ms.
1329 * The error should be lower than 2ms since the estimate relies
1330 * on two reads of a counter updated every ms.
1331 */
1332 if (abs(est_delay - subs->last_delay) * 1000 > runtime->rate * 2)
1333 snd_printk(KERN_DEBUG "delay: estimated %d, actual %d\n",
1334 est_delay, subs->last_delay);
1335
Takashi Iwai48779a02012-11-23 16:00:37 +01001336 if (!subs->running) {
1337 /* update last_frame_number for delay counting here since
1338 * prepare_playback_urb won't be called during pause
1339 */
1340 subs->last_frame_number =
1341 usb_get_current_frame_number(subs->dev) & 0xff;
1342 }
1343
1344 out:
Daniel Mackedcd3632012-04-12 13:51:12 +02001345 spin_unlock_irqrestore(&subs->lock, flags);
Daniel Macke5779992010-03-04 19:46:13 +01001346}
1347
1348static int snd_usb_playback_open(struct snd_pcm_substream *substream)
1349{
1350 return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_PLAYBACK);
1351}
1352
1353static int snd_usb_playback_close(struct snd_pcm_substream *substream)
1354{
1355 return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_PLAYBACK);
1356}
1357
1358static int snd_usb_capture_open(struct snd_pcm_substream *substream)
1359{
1360 return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_CAPTURE);
1361}
1362
1363static int snd_usb_capture_close(struct snd_pcm_substream *substream)
1364{
1365 return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_CAPTURE);
1366}
1367
Daniel Mackedcd3632012-04-12 13:51:12 +02001368static int snd_usb_substream_playback_trigger(struct snd_pcm_substream *substream,
1369 int cmd)
1370{
1371 struct snd_usb_substream *subs = substream->runtime->private_data;
1372
1373 switch (cmd) {
1374 case SNDRV_PCM_TRIGGER_START:
1375 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1376 subs->data_endpoint->prepare_data_urb = prepare_playback_urb;
1377 subs->data_endpoint->retire_data_urb = retire_playback_urb;
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001378 subs->running = 1;
Daniel Mackedcd3632012-04-12 13:51:12 +02001379 return 0;
1380 case SNDRV_PCM_TRIGGER_STOP:
Takashi Iwaia9bb3622012-11-20 18:32:06 +01001381 stop_endpoints(subs, false);
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001382 subs->running = 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001383 return 0;
1384 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1385 subs->data_endpoint->prepare_data_urb = NULL;
Takashi Iwai48779a02012-11-23 16:00:37 +01001386 /* keep retire_data_urb for delay calculation */
1387 subs->data_endpoint->retire_data_urb = retire_playback_urb;
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001388 subs->running = 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001389 return 0;
1390 }
1391
1392 return -EINVAL;
1393}
1394
Daniel Mackafe25962012-06-16 16:58:04 +02001395static int snd_usb_substream_capture_trigger(struct snd_pcm_substream *substream,
1396 int cmd)
Daniel Mackedcd3632012-04-12 13:51:12 +02001397{
1398 int err;
1399 struct snd_usb_substream *subs = substream->runtime->private_data;
1400
1401 switch (cmd) {
1402 case SNDRV_PCM_TRIGGER_START:
Takashi Iwaia9bb3622012-11-20 18:32:06 +01001403 err = start_endpoints(subs, false);
Daniel Mackedcd3632012-04-12 13:51:12 +02001404 if (err < 0)
1405 return err;
1406
1407 subs->data_endpoint->retire_data_urb = retire_capture_urb;
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001408 subs->running = 1;
Daniel Mackedcd3632012-04-12 13:51:12 +02001409 return 0;
1410 case SNDRV_PCM_TRIGGER_STOP:
Takashi Iwaia9bb3622012-11-20 18:32:06 +01001411 stop_endpoints(subs, false);
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001412 subs->running = 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001413 return 0;
1414 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1415 subs->data_endpoint->retire_data_urb = NULL;
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001416 subs->running = 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001417 return 0;
1418 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1419 subs->data_endpoint->retire_data_urb = retire_capture_urb;
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001420 subs->running = 1;
Daniel Mackedcd3632012-04-12 13:51:12 +02001421 return 0;
1422 }
1423
1424 return -EINVAL;
1425}
1426
Daniel Macke5779992010-03-04 19:46:13 +01001427static struct snd_pcm_ops snd_usb_playback_ops = {
1428 .open = snd_usb_playback_open,
1429 .close = snd_usb_playback_close,
1430 .ioctl = snd_pcm_lib_ioctl,
1431 .hw_params = snd_usb_hw_params,
1432 .hw_free = snd_usb_hw_free,
1433 .prepare = snd_usb_pcm_prepare,
1434 .trigger = snd_usb_substream_playback_trigger,
1435 .pointer = snd_usb_pcm_pointer,
1436 .page = snd_pcm_lib_get_vmalloc_page,
1437 .mmap = snd_pcm_lib_mmap_vmalloc,
1438};
1439
1440static struct snd_pcm_ops snd_usb_capture_ops = {
1441 .open = snd_usb_capture_open,
1442 .close = snd_usb_capture_close,
1443 .ioctl = snd_pcm_lib_ioctl,
1444 .hw_params = snd_usb_hw_params,
1445 .hw_free = snd_usb_hw_free,
1446 .prepare = snd_usb_pcm_prepare,
1447 .trigger = snd_usb_substream_capture_trigger,
1448 .pointer = snd_usb_pcm_pointer,
1449 .page = snd_pcm_lib_get_vmalloc_page,
1450 .mmap = snd_pcm_lib_mmap_vmalloc,
1451};
1452
1453void snd_usb_set_pcm_ops(struct snd_pcm *pcm, int stream)
1454{
1455 snd_pcm_set_ops(pcm, stream,
1456 stream == SNDRV_PCM_STREAM_PLAYBACK ?
1457 &snd_usb_playback_ops : &snd_usb_capture_ops);
1458}