blob: f488a493a98ee09e14b4da4294db612cf1014571 [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) {
365 case USB_ID(0x0763, 0x2080): /* M-Audio FastTrack Ultra */
366 case USB_ID(0x0763, 0x2081):
367 if (is_playback) {
368 implicit_fb = 1;
Daniel Mackedcd3632012-04-12 13:51:12 +0200369 ep = 0x81;
370 iface = usb_ifnum_to_if(dev, 2);
Daniel Mackc75a8a72012-04-12 13:51:14 +0200371
372 if (!iface || iface->num_altsetting == 0)
373 return -EINVAL;
374
Daniel Mackedcd3632012-04-12 13:51:12 +0200375 alts = &iface->altsetting[1];
376 goto add_sync_ep;
377 }
Daniel Mackc75a8a72012-04-12 13:51:14 +0200378 }
Daniel Mackedcd3632012-04-12 13:51:12 +0200379
Daniel Mackc75a8a72012-04-12 13:51:14 +0200380 if (((is_playback && attr == USB_ENDPOINT_SYNC_ASYNC) ||
381 (!is_playback && attr == USB_ENDPOINT_SYNC_ADAPTIVE)) &&
382 altsd->bNumEndpoints >= 2) {
Daniel Macke5779992010-03-04 19:46:13 +0100383 /* check sync-pipe endpoint */
384 /* ... and check descriptor size before accessing bSynchAddress
385 because there is a version of the SB Audigy 2 NX firmware lacking
386 the audio fields in the endpoint descriptors */
Eldad Zackfde854b2012-11-28 23:55:32 +0100387 if ((get_endpoint(alts, 1)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_ISOC ||
Daniel Macke5779992010-03-04 19:46:13 +0100388 (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
Daniel Mackc75a8a72012-04-12 13:51:14 +0200389 get_endpoint(alts, 1)->bSynchAddress != 0 &&
390 !implicit_fb)) {
Daniel Mack7fb75db2012-06-17 13:44:27 +0200391 snd_printk(KERN_ERR "%d:%d:%d : invalid sync pipe. bmAttributes %02x, bLength %d, bSynchAddress %02x\n",
392 dev->devnum, fmt->iface, fmt->altsetting,
393 get_endpoint(alts, 1)->bmAttributes,
394 get_endpoint(alts, 1)->bLength,
395 get_endpoint(alts, 1)->bSynchAddress);
Daniel Macke5779992010-03-04 19:46:13 +0100396 return -EINVAL;
397 }
398 ep = get_endpoint(alts, 1)->bEndpointAddress;
Daniel Mack7fb75db2012-06-17 13:44:27 +0200399 if (!implicit_fb &&
400 get_endpoint(alts, 0)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
Daniel Macke5779992010-03-04 19:46:13 +0100401 (( is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress | USB_DIR_IN)) ||
Daniel Mack7fb75db2012-06-17 13:44:27 +0200402 (!is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress & ~USB_DIR_IN)))) {
403 snd_printk(KERN_ERR "%d:%d:%d : invalid sync pipe. is_playback %d, ep %02x, bSynchAddress %02x\n",
404 dev->devnum, fmt->iface, fmt->altsetting,
405 is_playback, ep, get_endpoint(alts, 0)->bSynchAddress);
Daniel Macke5779992010-03-04 19:46:13 +0100406 return -EINVAL;
407 }
Daniel Mackc75a8a72012-04-12 13:51:14 +0200408
409 implicit_fb = (get_endpoint(alts, 1)->bmAttributes & USB_ENDPOINT_USAGE_MASK)
410 == USB_ENDPOINT_USAGE_IMPLICIT_FB;
411
Daniel Mackedcd3632012-04-12 13:51:12 +0200412add_sync_ep:
413 subs->sync_endpoint = snd_usb_add_endpoint(subs->stream->chip,
414 alts, ep, !subs->direction,
Daniel Mackc75a8a72012-04-12 13:51:14 +0200415 implicit_fb ?
416 SND_USB_ENDPOINT_TYPE_DATA :
417 SND_USB_ENDPOINT_TYPE_SYNC);
Daniel Mackedcd3632012-04-12 13:51:12 +0200418 if (!subs->sync_endpoint)
419 return -EINVAL;
420
421 subs->data_endpoint->sync_master = subs->sync_endpoint;
422 }
Daniel Macke5779992010-03-04 19:46:13 +0100423
Takashi Iwai9e9b5942012-07-06 08:11:43 +0200424 if ((err = snd_usb_init_pitch(subs->stream->chip, fmt->iface, alts, fmt)) < 0)
Daniel Macke5779992010-03-04 19:46:13 +0100425 return err;
426
427 subs->cur_audiofmt = fmt;
428
429 snd_usb_set_format_quirk(subs, fmt);
430
431#if 0
432 printk(KERN_DEBUG
433 "setting done: format = %d, rate = %d..%d, channels = %d\n",
434 fmt->format, fmt->rate_min, fmt->rate_max, fmt->channels);
435 printk(KERN_DEBUG
436 " datapipe = 0x%0x, syncpipe = 0x%0x\n",
437 subs->datapipe, subs->syncpipe);
438#endif
439
440 return 0;
441}
442
443/*
Dylan Reid61a70952012-09-18 09:49:48 -0700444 * configure endpoint params
445 *
446 * called during initial setup and upon resume
447 */
448static int configure_endpoint(struct snd_usb_substream *subs)
449{
450 int ret;
451
Dylan Reid61a70952012-09-18 09:49:48 -0700452 /* format changed */
Takashi Iwaib0db6062012-11-21 08:35:42 +0100453 stop_endpoints(subs, true);
Dylan Reid61a70952012-09-18 09:49:48 -0700454 ret = snd_usb_endpoint_set_params(subs->data_endpoint,
455 subs->pcm_format,
456 subs->channels,
457 subs->period_bytes,
458 subs->cur_rate,
459 subs->cur_audiofmt,
460 subs->sync_endpoint);
461 if (ret < 0)
Takashi Iwai978520b2012-10-12 15:12:55 +0200462 return ret;
Dylan Reid61a70952012-09-18 09:49:48 -0700463
464 if (subs->sync_endpoint)
Daniel Mack947d2992012-11-22 20:27:59 +0100465 ret = snd_usb_endpoint_set_params(subs->sync_endpoint,
Dylan Reid61a70952012-09-18 09:49:48 -0700466 subs->pcm_format,
467 subs->channels,
468 subs->period_bytes,
469 subs->cur_rate,
470 subs->cur_audiofmt,
471 NULL);
Dylan Reid61a70952012-09-18 09:49:48 -0700472 return ret;
473}
474
475/*
Daniel Macke5779992010-03-04 19:46:13 +0100476 * hw_params callback
477 *
478 * allocate a buffer and set the given audio format.
479 *
480 * so far we use a physically linear buffer although packetize transfer
481 * doesn't need a continuous area.
482 * if sg buffer is supported on the later version of alsa, we'll follow
483 * that.
484 */
485static int snd_usb_hw_params(struct snd_pcm_substream *substream,
486 struct snd_pcm_hw_params *hw_params)
487{
488 struct snd_usb_substream *subs = substream->runtime->private_data;
489 struct audioformat *fmt;
Dylan Reid61a70952012-09-18 09:49:48 -0700490 int ret;
Daniel Macke5779992010-03-04 19:46:13 +0100491
492 ret = snd_pcm_lib_alloc_vmalloc_buffer(substream,
493 params_buffer_bytes(hw_params));
494 if (ret < 0)
495 return ret;
496
Dylan Reid61a70952012-09-18 09:49:48 -0700497 subs->pcm_format = params_format(hw_params);
498 subs->period_bytes = params_period_bytes(hw_params);
499 subs->channels = params_channels(hw_params);
500 subs->cur_rate = params_rate(hw_params);
501
502 fmt = find_format(subs);
Daniel Macke5779992010-03-04 19:46:13 +0100503 if (!fmt) {
504 snd_printd(KERN_DEBUG "cannot set format: format = %#x, rate = %d, channels = %d\n",
Dylan Reid61a70952012-09-18 09:49:48 -0700505 subs->pcm_format, subs->cur_rate, subs->channels);
Daniel Macke5779992010-03-04 19:46:13 +0100506 return -EINVAL;
507 }
508
Takashi Iwai34f3c892012-10-15 12:16:02 +0200509 down_read(&subs->stream->chip->shutdown_rwsem);
Takashi Iwai978520b2012-10-12 15:12:55 +0200510 if (subs->stream->chip->shutdown)
511 ret = -ENODEV;
512 else
513 ret = set_format(subs, fmt);
Takashi Iwai34f3c892012-10-15 12:16:02 +0200514 up_read(&subs->stream->chip->shutdown_rwsem);
Takashi Iwai978520b2012-10-12 15:12:55 +0200515 if (ret < 0)
Daniel Macke5779992010-03-04 19:46:13 +0100516 return ret;
517
Dylan Reid61a70952012-09-18 09:49:48 -0700518 subs->interface = fmt->iface;
519 subs->altset_idx = fmt->altset_idx;
Takashi Iwai384dc0852012-09-18 14:49:31 +0200520 subs->need_setup_ep = true;
Daniel Macke5779992010-03-04 19:46:13 +0100521
Dylan Reid61a70952012-09-18 09:49:48 -0700522 return 0;
Daniel Macke5779992010-03-04 19:46:13 +0100523}
524
525/*
526 * hw_free callback
527 *
528 * reset the audio format and release the buffer
529 */
530static int snd_usb_hw_free(struct snd_pcm_substream *substream)
531{
532 struct snd_usb_substream *subs = substream->runtime->private_data;
533
534 subs->cur_audiofmt = NULL;
535 subs->cur_rate = 0;
536 subs->period_bytes = 0;
Takashi Iwai34f3c892012-10-15 12:16:02 +0200537 down_read(&subs->stream->chip->shutdown_rwsem);
Takashi Iwai978520b2012-10-12 15:12:55 +0200538 if (!subs->stream->chip->shutdown) {
Takashi Iwaia9bb3622012-11-20 18:32:06 +0100539 stop_endpoints(subs, true);
Takashi Iwai978520b2012-10-12 15:12:55 +0200540 deactivate_endpoints(subs);
541 }
Takashi Iwai34f3c892012-10-15 12:16:02 +0200542 up_read(&subs->stream->chip->shutdown_rwsem);
Daniel Macke5779992010-03-04 19:46:13 +0100543 return snd_pcm_lib_free_vmalloc_buffer(substream);
544}
545
546/*
547 * prepare callback
548 *
549 * only a few subtle things...
550 */
551static int snd_usb_pcm_prepare(struct snd_pcm_substream *substream)
552{
553 struct snd_pcm_runtime *runtime = substream->runtime;
554 struct snd_usb_substream *subs = runtime->private_data;
Dylan Reid61a70952012-09-18 09:49:48 -0700555 struct usb_host_interface *alts;
556 struct usb_interface *iface;
557 int ret;
Daniel Macke5779992010-03-04 19:46:13 +0100558
559 if (! subs->cur_audiofmt) {
560 snd_printk(KERN_ERR "usbaudio: no format is specified!\n");
561 return -ENXIO;
562 }
563
Takashi Iwai34f3c892012-10-15 12:16:02 +0200564 down_read(&subs->stream->chip->shutdown_rwsem);
Takashi Iwai978520b2012-10-12 15:12:55 +0200565 if (subs->stream->chip->shutdown) {
566 ret = -ENODEV;
567 goto unlock;
568 }
569 if (snd_BUG_ON(!subs->data_endpoint)) {
570 ret = -EIO;
571 goto unlock;
572 }
Daniel Mackedcd3632012-04-12 13:51:12 +0200573
Takashi Iwaif58161b2012-11-08 08:52:45 +0100574 snd_usb_endpoint_sync_pending_stop(subs->sync_endpoint);
575 snd_usb_endpoint_sync_pending_stop(subs->data_endpoint);
576
Dylan Reid61a70952012-09-18 09:49:48 -0700577 ret = set_format(subs, subs->cur_audiofmt);
578 if (ret < 0)
Takashi Iwai978520b2012-10-12 15:12:55 +0200579 goto unlock;
Dylan Reid61a70952012-09-18 09:49:48 -0700580
581 iface = usb_ifnum_to_if(subs->dev, subs->cur_audiofmt->iface);
582 alts = &iface->altsetting[subs->cur_audiofmt->altset_idx];
583 ret = snd_usb_init_sample_rate(subs->stream->chip,
584 subs->cur_audiofmt->iface,
585 alts,
586 subs->cur_audiofmt,
587 subs->cur_rate);
588 if (ret < 0)
Takashi Iwai978520b2012-10-12 15:12:55 +0200589 goto unlock;
Dylan Reid61a70952012-09-18 09:49:48 -0700590
Takashi Iwai384dc0852012-09-18 14:49:31 +0200591 if (subs->need_setup_ep) {
592 ret = configure_endpoint(subs);
593 if (ret < 0)
Takashi Iwai978520b2012-10-12 15:12:55 +0200594 goto unlock;
Takashi Iwai384dc0852012-09-18 14:49:31 +0200595 subs->need_setup_ep = false;
596 }
Dylan Reid61a70952012-09-18 09:49:48 -0700597
Daniel Macke5779992010-03-04 19:46:13 +0100598 /* some unit conversions in runtime */
Daniel Mackedcd3632012-04-12 13:51:12 +0200599 subs->data_endpoint->maxframesize =
600 bytes_to_frames(runtime, subs->data_endpoint->maxpacksize);
601 subs->data_endpoint->curframesize =
602 bytes_to_frames(runtime, subs->data_endpoint->curpacksize);
Daniel Macke5779992010-03-04 19:46:13 +0100603
604 /* reset the pointer */
605 subs->hwptr_done = 0;
606 subs->transfer_done = 0;
Pierre-Louis Bossart294c4fb2011-09-06 19:15:34 -0500607 subs->last_delay = 0;
608 subs->last_frame_number = 0;
Daniel Macke5779992010-03-04 19:46:13 +0100609 runtime->delay = 0;
610
Daniel Mackedcd3632012-04-12 13:51:12 +0200611 /* for playback, submit the URBs now; otherwise, the first hwptr_done
612 * updates for all URBs would happen at the same time when starting */
613 if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK)
Takashi Iwaia9bb3622012-11-20 18:32:06 +0100614 ret = start_endpoints(subs, true);
Daniel Mackedcd3632012-04-12 13:51:12 +0200615
Takashi Iwai978520b2012-10-12 15:12:55 +0200616 unlock:
Takashi Iwai34f3c892012-10-15 12:16:02 +0200617 up_read(&subs->stream->chip->shutdown_rwsem);
Takashi Iwai978520b2012-10-12 15:12:55 +0200618 return ret;
Daniel Macke5779992010-03-04 19:46:13 +0100619}
620
621static struct snd_pcm_hardware snd_usb_hardware =
622{
623 .info = SNDRV_PCM_INFO_MMAP |
624 SNDRV_PCM_INFO_MMAP_VALID |
625 SNDRV_PCM_INFO_BATCH |
626 SNDRV_PCM_INFO_INTERLEAVED |
627 SNDRV_PCM_INFO_BLOCK_TRANSFER |
628 SNDRV_PCM_INFO_PAUSE,
629 .buffer_bytes_max = 1024 * 1024,
630 .period_bytes_min = 64,
631 .period_bytes_max = 512 * 1024,
632 .periods_min = 2,
633 .periods_max = 1024,
634};
635
636static int hw_check_valid_format(struct snd_usb_substream *subs,
637 struct snd_pcm_hw_params *params,
638 struct audioformat *fp)
639{
640 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
641 struct snd_interval *ct = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
642 struct snd_mask *fmts = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
643 struct snd_interval *pt = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
Clemens Ladisch015eb0b2010-03-04 19:46:15 +0100644 struct snd_mask check_fmts;
Daniel Macke5779992010-03-04 19:46:13 +0100645 unsigned int ptime;
646
647 /* check the format */
Clemens Ladisch015eb0b2010-03-04 19:46:15 +0100648 snd_mask_none(&check_fmts);
649 check_fmts.bits[0] = (u32)fp->formats;
650 check_fmts.bits[1] = (u32)(fp->formats >> 32);
651 snd_mask_intersect(&check_fmts, fmts);
652 if (snd_mask_empty(&check_fmts)) {
Daniel Macke5779992010-03-04 19:46:13 +0100653 hwc_debug(" > check: no supported format %d\n", fp->format);
654 return 0;
655 }
656 /* check the channels */
657 if (fp->channels < ct->min || fp->channels > ct->max) {
658 hwc_debug(" > check: no valid channels %d (%d/%d)\n", fp->channels, ct->min, ct->max);
659 return 0;
660 }
661 /* check the rate is within the range */
662 if (fp->rate_min > it->max || (fp->rate_min == it->max && it->openmax)) {
663 hwc_debug(" > check: rate_min %d > max %d\n", fp->rate_min, it->max);
664 return 0;
665 }
666 if (fp->rate_max < it->min || (fp->rate_max == it->min && it->openmin)) {
667 hwc_debug(" > check: rate_max %d < min %d\n", fp->rate_max, it->min);
668 return 0;
669 }
670 /* check whether the period time is >= the data packet interval */
Takashi Iwai978520b2012-10-12 15:12:55 +0200671 if (subs->speed != USB_SPEED_FULL) {
Daniel Macke5779992010-03-04 19:46:13 +0100672 ptime = 125 * (1 << fp->datainterval);
673 if (ptime > pt->max || (ptime == pt->max && pt->openmax)) {
674 hwc_debug(" > check: ptime %u > max %u\n", ptime, pt->max);
675 return 0;
676 }
677 }
678 return 1;
679}
680
681static int hw_rule_rate(struct snd_pcm_hw_params *params,
682 struct snd_pcm_hw_rule *rule)
683{
684 struct snd_usb_substream *subs = rule->private;
685 struct list_head *p;
686 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
687 unsigned int rmin, rmax;
688 int changed;
689
690 hwc_debug("hw_rule_rate: (%d,%d)\n", it->min, it->max);
691 changed = 0;
692 rmin = rmax = 0;
693 list_for_each(p, &subs->fmt_list) {
694 struct audioformat *fp;
695 fp = list_entry(p, struct audioformat, list);
696 if (!hw_check_valid_format(subs, params, fp))
697 continue;
698 if (changed++) {
699 if (rmin > fp->rate_min)
700 rmin = fp->rate_min;
701 if (rmax < fp->rate_max)
702 rmax = fp->rate_max;
703 } else {
704 rmin = fp->rate_min;
705 rmax = fp->rate_max;
706 }
707 }
708
709 if (!changed) {
710 hwc_debug(" --> get empty\n");
711 it->empty = 1;
712 return -EINVAL;
713 }
714
715 changed = 0;
716 if (it->min < rmin) {
717 it->min = rmin;
718 it->openmin = 0;
719 changed = 1;
720 }
721 if (it->max > rmax) {
722 it->max = rmax;
723 it->openmax = 0;
724 changed = 1;
725 }
726 if (snd_interval_checkempty(it)) {
727 it->empty = 1;
728 return -EINVAL;
729 }
730 hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
731 return changed;
732}
733
734
735static int hw_rule_channels(struct snd_pcm_hw_params *params,
736 struct snd_pcm_hw_rule *rule)
737{
738 struct snd_usb_substream *subs = rule->private;
739 struct list_head *p;
740 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
741 unsigned int rmin, rmax;
742 int changed;
743
744 hwc_debug("hw_rule_channels: (%d,%d)\n", it->min, it->max);
745 changed = 0;
746 rmin = rmax = 0;
747 list_for_each(p, &subs->fmt_list) {
748 struct audioformat *fp;
749 fp = list_entry(p, struct audioformat, list);
750 if (!hw_check_valid_format(subs, params, fp))
751 continue;
752 if (changed++) {
753 if (rmin > fp->channels)
754 rmin = fp->channels;
755 if (rmax < fp->channels)
756 rmax = fp->channels;
757 } else {
758 rmin = fp->channels;
759 rmax = fp->channels;
760 }
761 }
762
763 if (!changed) {
764 hwc_debug(" --> get empty\n");
765 it->empty = 1;
766 return -EINVAL;
767 }
768
769 changed = 0;
770 if (it->min < rmin) {
771 it->min = rmin;
772 it->openmin = 0;
773 changed = 1;
774 }
775 if (it->max > rmax) {
776 it->max = rmax;
777 it->openmax = 0;
778 changed = 1;
779 }
780 if (snd_interval_checkempty(it)) {
781 it->empty = 1;
782 return -EINVAL;
783 }
784 hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
785 return changed;
786}
787
788static int hw_rule_format(struct snd_pcm_hw_params *params,
789 struct snd_pcm_hw_rule *rule)
790{
791 struct snd_usb_substream *subs = rule->private;
792 struct list_head *p;
793 struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
794 u64 fbits;
795 u32 oldbits[2];
796 int changed;
797
798 hwc_debug("hw_rule_format: %x:%x\n", fmt->bits[0], fmt->bits[1]);
799 fbits = 0;
800 list_for_each(p, &subs->fmt_list) {
801 struct audioformat *fp;
802 fp = list_entry(p, struct audioformat, list);
803 if (!hw_check_valid_format(subs, params, fp))
804 continue;
Clemens Ladisch015eb0b2010-03-04 19:46:15 +0100805 fbits |= fp->formats;
Daniel Macke5779992010-03-04 19:46:13 +0100806 }
807
808 oldbits[0] = fmt->bits[0];
809 oldbits[1] = fmt->bits[1];
810 fmt->bits[0] &= (u32)fbits;
811 fmt->bits[1] &= (u32)(fbits >> 32);
812 if (!fmt->bits[0] && !fmt->bits[1]) {
813 hwc_debug(" --> get empty\n");
814 return -EINVAL;
815 }
816 changed = (oldbits[0] != fmt->bits[0] || oldbits[1] != fmt->bits[1]);
817 hwc_debug(" --> %x:%x (changed = %d)\n", fmt->bits[0], fmt->bits[1], changed);
818 return changed;
819}
820
821static int hw_rule_period_time(struct snd_pcm_hw_params *params,
822 struct snd_pcm_hw_rule *rule)
823{
824 struct snd_usb_substream *subs = rule->private;
825 struct audioformat *fp;
826 struct snd_interval *it;
827 unsigned char min_datainterval;
828 unsigned int pmin;
829 int changed;
830
831 it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
832 hwc_debug("hw_rule_period_time: (%u,%u)\n", it->min, it->max);
833 min_datainterval = 0xff;
834 list_for_each_entry(fp, &subs->fmt_list, list) {
835 if (!hw_check_valid_format(subs, params, fp))
836 continue;
837 min_datainterval = min(min_datainterval, fp->datainterval);
838 }
839 if (min_datainterval == 0xff) {
Uwe Kleine-Königa7ce2e02010-07-12 17:15:44 +0200840 hwc_debug(" --> get empty\n");
Daniel Macke5779992010-03-04 19:46:13 +0100841 it->empty = 1;
842 return -EINVAL;
843 }
844 pmin = 125 * (1 << min_datainterval);
845 changed = 0;
846 if (it->min < pmin) {
847 it->min = pmin;
848 it->openmin = 0;
849 changed = 1;
850 }
851 if (snd_interval_checkempty(it)) {
852 it->empty = 1;
853 return -EINVAL;
854 }
855 hwc_debug(" --> (%u,%u) (changed = %d)\n", it->min, it->max, changed);
856 return changed;
857}
858
859/*
860 * If the device supports unusual bit rates, does the request meet these?
861 */
862static int snd_usb_pcm_check_knot(struct snd_pcm_runtime *runtime,
863 struct snd_usb_substream *subs)
864{
865 struct audioformat *fp;
Takashi Iwai0717d0f2012-03-15 16:14:38 +0100866 int *rate_list;
Daniel Macke5779992010-03-04 19:46:13 +0100867 int count = 0, needs_knot = 0;
868 int err;
869
Clemens Ladisch5cd5d7c2012-05-18 18:00:43 +0200870 kfree(subs->rate_list.list);
871 subs->rate_list.list = NULL;
872
Daniel Macke5779992010-03-04 19:46:13 +0100873 list_for_each_entry(fp, &subs->fmt_list, list) {
874 if (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)
875 return 0;
876 count += fp->nr_rates;
877 if (fp->rates & SNDRV_PCM_RATE_KNOT)
878 needs_knot = 1;
879 }
880 if (!needs_knot)
881 return 0;
882
Takashi Iwai0717d0f2012-03-15 16:14:38 +0100883 subs->rate_list.list = rate_list =
884 kmalloc(sizeof(int) * count, GFP_KERNEL);
Jesper Juhl8a8d56b2010-10-29 20:40:23 +0200885 if (!subs->rate_list.list)
886 return -ENOMEM;
887 subs->rate_list.count = count;
Daniel Macke5779992010-03-04 19:46:13 +0100888 subs->rate_list.mask = 0;
889 count = 0;
890 list_for_each_entry(fp, &subs->fmt_list, list) {
891 int i;
892 for (i = 0; i < fp->nr_rates; i++)
Takashi Iwai0717d0f2012-03-15 16:14:38 +0100893 rate_list[count++] = fp->rate_table[i];
Daniel Macke5779992010-03-04 19:46:13 +0100894 }
895 err = snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
896 &subs->rate_list);
897 if (err < 0)
898 return err;
899
900 return 0;
901}
902
903
904/*
905 * set up the runtime hardware information.
906 */
907
908static int setup_hw_info(struct snd_pcm_runtime *runtime, struct snd_usb_substream *subs)
909{
910 struct list_head *p;
911 unsigned int pt, ptmin;
912 int param_period_time_if_needed;
913 int err;
914
915 runtime->hw.formats = subs->formats;
916
917 runtime->hw.rate_min = 0x7fffffff;
918 runtime->hw.rate_max = 0;
919 runtime->hw.channels_min = 256;
920 runtime->hw.channels_max = 0;
921 runtime->hw.rates = 0;
922 ptmin = UINT_MAX;
923 /* check min/max rates and channels */
924 list_for_each(p, &subs->fmt_list) {
925 struct audioformat *fp;
926 fp = list_entry(p, struct audioformat, list);
927 runtime->hw.rates |= fp->rates;
928 if (runtime->hw.rate_min > fp->rate_min)
929 runtime->hw.rate_min = fp->rate_min;
930 if (runtime->hw.rate_max < fp->rate_max)
931 runtime->hw.rate_max = fp->rate_max;
932 if (runtime->hw.channels_min > fp->channels)
933 runtime->hw.channels_min = fp->channels;
934 if (runtime->hw.channels_max < fp->channels)
935 runtime->hw.channels_max = fp->channels;
936 if (fp->fmt_type == UAC_FORMAT_TYPE_II && fp->frame_size > 0) {
937 /* FIXME: there might be more than one audio formats... */
938 runtime->hw.period_bytes_min = runtime->hw.period_bytes_max =
939 fp->frame_size;
940 }
941 pt = 125 * (1 << fp->datainterval);
942 ptmin = min(ptmin, pt);
943 }
Oliver Neukum88a85162011-03-11 14:51:12 +0100944 err = snd_usb_autoresume(subs->stream->chip);
945 if (err < 0)
946 return err;
Daniel Macke5779992010-03-04 19:46:13 +0100947
948 param_period_time_if_needed = SNDRV_PCM_HW_PARAM_PERIOD_TIME;
Takashi Iwai978520b2012-10-12 15:12:55 +0200949 if (subs->speed == USB_SPEED_FULL)
Daniel Macke5779992010-03-04 19:46:13 +0100950 /* full speed devices have fixed data packet interval */
951 ptmin = 1000;
952 if (ptmin == 1000)
953 /* if period time doesn't go below 1 ms, no rules needed */
954 param_period_time_if_needed = -1;
955 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
956 ptmin, UINT_MAX);
957
958 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
959 hw_rule_rate, subs,
960 SNDRV_PCM_HW_PARAM_FORMAT,
961 SNDRV_PCM_HW_PARAM_CHANNELS,
962 param_period_time_if_needed,
963 -1)) < 0)
Oliver Neukum88a85162011-03-11 14:51:12 +0100964 goto rep_err;
Daniel Macke5779992010-03-04 19:46:13 +0100965 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
966 hw_rule_channels, subs,
967 SNDRV_PCM_HW_PARAM_FORMAT,
968 SNDRV_PCM_HW_PARAM_RATE,
969 param_period_time_if_needed,
970 -1)) < 0)
Oliver Neukum88a85162011-03-11 14:51:12 +0100971 goto rep_err;
Daniel Macke5779992010-03-04 19:46:13 +0100972 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
973 hw_rule_format, subs,
974 SNDRV_PCM_HW_PARAM_RATE,
975 SNDRV_PCM_HW_PARAM_CHANNELS,
976 param_period_time_if_needed,
977 -1)) < 0)
Oliver Neukum88a85162011-03-11 14:51:12 +0100978 goto rep_err;
Daniel Macke5779992010-03-04 19:46:13 +0100979 if (param_period_time_if_needed >= 0) {
980 err = snd_pcm_hw_rule_add(runtime, 0,
981 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
982 hw_rule_period_time, subs,
983 SNDRV_PCM_HW_PARAM_FORMAT,
984 SNDRV_PCM_HW_PARAM_CHANNELS,
985 SNDRV_PCM_HW_PARAM_RATE,
986 -1);
987 if (err < 0)
Oliver Neukum88a85162011-03-11 14:51:12 +0100988 goto rep_err;
Daniel Macke5779992010-03-04 19:46:13 +0100989 }
990 if ((err = snd_usb_pcm_check_knot(runtime, subs)) < 0)
Oliver Neukum88a85162011-03-11 14:51:12 +0100991 goto rep_err;
Daniel Macke5779992010-03-04 19:46:13 +0100992 return 0;
Oliver Neukum88a85162011-03-11 14:51:12 +0100993
994rep_err:
995 snd_usb_autosuspend(subs->stream->chip);
996 return err;
Daniel Macke5779992010-03-04 19:46:13 +0100997}
998
999static int snd_usb_pcm_open(struct snd_pcm_substream *substream, int direction)
1000{
1001 struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
1002 struct snd_pcm_runtime *runtime = substream->runtime;
1003 struct snd_usb_substream *subs = &as->substream[direction];
1004
1005 subs->interface = -1;
Clemens Ladische11b4e02010-03-04 19:46:14 +01001006 subs->altset_idx = 0;
Daniel Macke5779992010-03-04 19:46:13 +01001007 runtime->hw = snd_usb_hardware;
1008 runtime->private_data = subs;
1009 subs->pcm_substream = substream;
Oliver Neukum88a85162011-03-11 14:51:12 +01001010 /* runtime PM is also done there */
Daniel Macke5779992010-03-04 19:46:13 +01001011 return setup_hw_info(runtime, subs);
1012}
1013
1014static int snd_usb_pcm_close(struct snd_pcm_substream *substream, int direction)
1015{
1016 struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
1017 struct snd_usb_substream *subs = &as->substream[direction];
1018
Takashi Iwaib0db6062012-11-21 08:35:42 +01001019 stop_endpoints(subs, true);
Daniel Mack68e67f42012-07-12 13:08:40 +02001020
1021 if (!as->chip->shutdown && subs->interface >= 0) {
1022 usb_set_interface(subs->dev, subs->interface, 0);
1023 subs->interface = -1;
1024 }
1025
Daniel Macke5779992010-03-04 19:46:13 +01001026 subs->pcm_substream = NULL;
Oliver Neukum88a85162011-03-11 14:51:12 +01001027 snd_usb_autosuspend(subs->stream->chip);
Daniel Mackedcd3632012-04-12 13:51:12 +02001028
Daniel Mack68e67f42012-07-12 13:08:40 +02001029 return 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001030}
1031
1032/* Since a URB can handle only a single linear buffer, we must use double
1033 * buffering when the data to be transferred overflows the buffer boundary.
1034 * To avoid inconsistencies when updating hwptr_done, we use double buffering
1035 * for all URBs.
1036 */
1037static void retire_capture_urb(struct snd_usb_substream *subs,
1038 struct urb *urb)
1039{
1040 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1041 unsigned int stride, frames, bytes, oldptr;
1042 int i, period_elapsed = 0;
1043 unsigned long flags;
1044 unsigned char *cp;
1045
1046 stride = runtime->frame_bits >> 3;
1047
1048 for (i = 0; i < urb->number_of_packets; i++) {
1049 cp = (unsigned char *)urb->transfer_buffer + urb->iso_frame_desc[i].offset;
1050 if (urb->iso_frame_desc[i].status && printk_ratelimit()) {
1051 snd_printdd(KERN_ERR "frame %d active: %d\n", i, urb->iso_frame_desc[i].status);
1052 // continue;
1053 }
1054 bytes = urb->iso_frame_desc[i].actual_length;
1055 frames = bytes / stride;
1056 if (!subs->txfr_quirk)
1057 bytes = frames * stride;
1058 if (bytes % (runtime->sample_bits >> 3) != 0) {
1059#ifdef CONFIG_SND_DEBUG_VERBOSE
1060 int oldbytes = bytes;
1061#endif
1062 bytes = frames * stride;
1063 snd_printdd(KERN_ERR "Corrected urb data len. %d->%d\n",
1064 oldbytes, bytes);
1065 }
1066 /* update the current pointer */
1067 spin_lock_irqsave(&subs->lock, flags);
1068 oldptr = subs->hwptr_done;
1069 subs->hwptr_done += bytes;
1070 if (subs->hwptr_done >= runtime->buffer_size * stride)
1071 subs->hwptr_done -= runtime->buffer_size * stride;
1072 frames = (bytes + (oldptr % stride)) / stride;
1073 subs->transfer_done += frames;
1074 if (subs->transfer_done >= runtime->period_size) {
1075 subs->transfer_done -= runtime->period_size;
1076 period_elapsed = 1;
1077 }
1078 spin_unlock_irqrestore(&subs->lock, flags);
1079 /* copy a data chunk */
1080 if (oldptr + bytes > runtime->buffer_size * stride) {
1081 unsigned int bytes1 =
1082 runtime->buffer_size * stride - oldptr;
1083 memcpy(runtime->dma_area + oldptr, cp, bytes1);
1084 memcpy(runtime->dma_area, cp + bytes1, bytes - bytes1);
1085 } else {
1086 memcpy(runtime->dma_area + oldptr, cp, bytes);
1087 }
1088 }
1089
1090 if (period_elapsed)
1091 snd_pcm_period_elapsed(subs->pcm_substream);
1092}
1093
1094static void prepare_playback_urb(struct snd_usb_substream *subs,
1095 struct urb *urb)
1096{
1097 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
Daniel Mack245baf92012-08-30 18:52:30 +02001098 struct snd_usb_endpoint *ep = subs->data_endpoint;
Daniel Mackedcd3632012-04-12 13:51:12 +02001099 struct snd_urb_ctx *ctx = urb->context;
1100 unsigned int counts, frames, bytes;
1101 int i, stride, period_elapsed = 0;
1102 unsigned long flags;
1103
1104 stride = runtime->frame_bits >> 3;
1105
1106 frames = 0;
1107 urb->number_of_packets = 0;
1108 spin_lock_irqsave(&subs->lock, flags);
1109 for (i = 0; i < ctx->packets; i++) {
Daniel Mack245baf92012-08-30 18:52:30 +02001110 if (ctx->packet_size[i])
1111 counts = ctx->packet_size[i];
1112 else
1113 counts = snd_usb_endpoint_next_packet_size(ep);
1114
Daniel Mackedcd3632012-04-12 13:51:12 +02001115 /* set up descriptor */
1116 urb->iso_frame_desc[i].offset = frames * stride;
1117 urb->iso_frame_desc[i].length = counts * stride;
1118 frames += counts;
1119 urb->number_of_packets++;
1120 subs->transfer_done += counts;
1121 if (subs->transfer_done >= runtime->period_size) {
1122 subs->transfer_done -= runtime->period_size;
1123 period_elapsed = 1;
1124 if (subs->fmt_type == UAC_FORMAT_TYPE_II) {
1125 if (subs->transfer_done > 0) {
1126 /* FIXME: fill-max mode is not
1127 * supported yet */
1128 frames -= subs->transfer_done;
1129 counts -= subs->transfer_done;
1130 urb->iso_frame_desc[i].length =
1131 counts * stride;
1132 subs->transfer_done = 0;
1133 }
1134 i++;
1135 if (i < ctx->packets) {
1136 /* add a transfer delimiter */
1137 urb->iso_frame_desc[i].offset =
1138 frames * stride;
1139 urb->iso_frame_desc[i].length = 0;
1140 urb->number_of_packets++;
1141 }
1142 break;
1143 }
1144 }
1145 if (period_elapsed &&
1146 !snd_usb_endpoint_implict_feedback_sink(subs->data_endpoint)) /* finish at the period boundary */
1147 break;
1148 }
1149 bytes = frames * stride;
1150 if (subs->hwptr_done + bytes > runtime->buffer_size * stride) {
1151 /* err, the transferred area goes over buffer boundary. */
1152 unsigned int bytes1 =
1153 runtime->buffer_size * stride - subs->hwptr_done;
1154 memcpy(urb->transfer_buffer,
1155 runtime->dma_area + subs->hwptr_done, bytes1);
1156 memcpy(urb->transfer_buffer + bytes1,
1157 runtime->dma_area, bytes - bytes1);
1158 } else {
1159 memcpy(urb->transfer_buffer,
1160 runtime->dma_area + subs->hwptr_done, bytes);
1161 }
1162 subs->hwptr_done += bytes;
1163 if (subs->hwptr_done >= runtime->buffer_size * stride)
1164 subs->hwptr_done -= runtime->buffer_size * stride;
Daniel Mackfbcfbf52012-08-30 18:52:29 +02001165
1166 /* update delay with exact number of samples queued */
1167 runtime->delay = subs->last_delay;
Daniel Mackedcd3632012-04-12 13:51:12 +02001168 runtime->delay += frames;
Daniel Mackfbcfbf52012-08-30 18:52:29 +02001169 subs->last_delay = runtime->delay;
1170
1171 /* realign last_frame_number */
1172 subs->last_frame_number = usb_get_current_frame_number(subs->dev);
1173 subs->last_frame_number &= 0xFF; /* keep 8 LSBs */
1174
Daniel Mackedcd3632012-04-12 13:51:12 +02001175 spin_unlock_irqrestore(&subs->lock, flags);
1176 urb->transfer_buffer_length = bytes;
1177 if (period_elapsed)
1178 snd_pcm_period_elapsed(subs->pcm_substream);
1179}
1180
1181/*
1182 * process after playback data complete
1183 * - decrease the delay count again
1184 */
1185static void retire_playback_urb(struct snd_usb_substream *subs,
1186 struct urb *urb)
1187{
1188 unsigned long flags;
1189 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1190 int stride = runtime->frame_bits >> 3;
1191 int processed = urb->transfer_buffer_length / stride;
Daniel Mackfbcfbf52012-08-30 18:52:29 +02001192 int est_delay;
Daniel Mackedcd3632012-04-12 13:51:12 +02001193
Takashi Iwai1213a202012-09-06 14:58:00 +02001194 /* ignore the delay accounting when procssed=0 is given, i.e.
1195 * silent payloads are procssed before handling the actual data
1196 */
1197 if (!processed)
1198 return;
1199
Daniel Mackedcd3632012-04-12 13:51:12 +02001200 spin_lock_irqsave(&subs->lock, flags);
Takashi Iwai48779a02012-11-23 16:00:37 +01001201 if (!subs->last_delay)
1202 goto out; /* short path */
1203
Daniel Mackfbcfbf52012-08-30 18:52:29 +02001204 est_delay = snd_usb_pcm_delay(subs, runtime->rate);
1205 /* update delay with exact number of samples played */
1206 if (processed > subs->last_delay)
1207 subs->last_delay = 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001208 else
Daniel Mackfbcfbf52012-08-30 18:52:29 +02001209 subs->last_delay -= processed;
1210 runtime->delay = subs->last_delay;
1211
1212 /*
1213 * Report when delay estimate is off by more than 2ms.
1214 * The error should be lower than 2ms since the estimate relies
1215 * on two reads of a counter updated every ms.
1216 */
1217 if (abs(est_delay - subs->last_delay) * 1000 > runtime->rate * 2)
1218 snd_printk(KERN_DEBUG "delay: estimated %d, actual %d\n",
1219 est_delay, subs->last_delay);
1220
Takashi Iwai48779a02012-11-23 16:00:37 +01001221 if (!subs->running) {
1222 /* update last_frame_number for delay counting here since
1223 * prepare_playback_urb won't be called during pause
1224 */
1225 subs->last_frame_number =
1226 usb_get_current_frame_number(subs->dev) & 0xff;
1227 }
1228
1229 out:
Daniel Mackedcd3632012-04-12 13:51:12 +02001230 spin_unlock_irqrestore(&subs->lock, flags);
Daniel Macke5779992010-03-04 19:46:13 +01001231}
1232
1233static int snd_usb_playback_open(struct snd_pcm_substream *substream)
1234{
1235 return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_PLAYBACK);
1236}
1237
1238static int snd_usb_playback_close(struct snd_pcm_substream *substream)
1239{
1240 return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_PLAYBACK);
1241}
1242
1243static int snd_usb_capture_open(struct snd_pcm_substream *substream)
1244{
1245 return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_CAPTURE);
1246}
1247
1248static int snd_usb_capture_close(struct snd_pcm_substream *substream)
1249{
1250 return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_CAPTURE);
1251}
1252
Daniel Mackedcd3632012-04-12 13:51:12 +02001253static int snd_usb_substream_playback_trigger(struct snd_pcm_substream *substream,
1254 int cmd)
1255{
1256 struct snd_usb_substream *subs = substream->runtime->private_data;
1257
1258 switch (cmd) {
1259 case SNDRV_PCM_TRIGGER_START:
1260 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1261 subs->data_endpoint->prepare_data_urb = prepare_playback_urb;
1262 subs->data_endpoint->retire_data_urb = retire_playback_urb;
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001263 subs->running = 1;
Daniel Mackedcd3632012-04-12 13:51:12 +02001264 return 0;
1265 case SNDRV_PCM_TRIGGER_STOP:
Takashi Iwaia9bb3622012-11-20 18:32:06 +01001266 stop_endpoints(subs, false);
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001267 subs->running = 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001268 return 0;
1269 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1270 subs->data_endpoint->prepare_data_urb = NULL;
Takashi Iwai48779a02012-11-23 16:00:37 +01001271 /* keep retire_data_urb for delay calculation */
1272 subs->data_endpoint->retire_data_urb = retire_playback_urb;
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001273 subs->running = 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001274 return 0;
1275 }
1276
1277 return -EINVAL;
1278}
1279
Daniel Mackafe25962012-06-16 16:58:04 +02001280static int snd_usb_substream_capture_trigger(struct snd_pcm_substream *substream,
1281 int cmd)
Daniel Mackedcd3632012-04-12 13:51:12 +02001282{
1283 int err;
1284 struct snd_usb_substream *subs = substream->runtime->private_data;
1285
1286 switch (cmd) {
1287 case SNDRV_PCM_TRIGGER_START:
Takashi Iwaia9bb3622012-11-20 18:32:06 +01001288 err = start_endpoints(subs, false);
Daniel Mackedcd3632012-04-12 13:51:12 +02001289 if (err < 0)
1290 return err;
1291
1292 subs->data_endpoint->retire_data_urb = retire_capture_urb;
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001293 subs->running = 1;
Daniel Mackedcd3632012-04-12 13:51:12 +02001294 return 0;
1295 case SNDRV_PCM_TRIGGER_STOP:
Takashi Iwaia9bb3622012-11-20 18:32:06 +01001296 stop_endpoints(subs, false);
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001297 subs->running = 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001298 return 0;
1299 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1300 subs->data_endpoint->retire_data_urb = NULL;
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001301 subs->running = 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001302 return 0;
1303 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1304 subs->data_endpoint->retire_data_urb = retire_capture_urb;
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001305 subs->running = 1;
Daniel Mackedcd3632012-04-12 13:51:12 +02001306 return 0;
1307 }
1308
1309 return -EINVAL;
1310}
1311
Daniel Macke5779992010-03-04 19:46:13 +01001312static struct snd_pcm_ops snd_usb_playback_ops = {
1313 .open = snd_usb_playback_open,
1314 .close = snd_usb_playback_close,
1315 .ioctl = snd_pcm_lib_ioctl,
1316 .hw_params = snd_usb_hw_params,
1317 .hw_free = snd_usb_hw_free,
1318 .prepare = snd_usb_pcm_prepare,
1319 .trigger = snd_usb_substream_playback_trigger,
1320 .pointer = snd_usb_pcm_pointer,
1321 .page = snd_pcm_lib_get_vmalloc_page,
1322 .mmap = snd_pcm_lib_mmap_vmalloc,
1323};
1324
1325static struct snd_pcm_ops snd_usb_capture_ops = {
1326 .open = snd_usb_capture_open,
1327 .close = snd_usb_capture_close,
1328 .ioctl = snd_pcm_lib_ioctl,
1329 .hw_params = snd_usb_hw_params,
1330 .hw_free = snd_usb_hw_free,
1331 .prepare = snd_usb_pcm_prepare,
1332 .trigger = snd_usb_substream_capture_trigger,
1333 .pointer = snd_usb_pcm_pointer,
1334 .page = snd_pcm_lib_get_vmalloc_page,
1335 .mmap = snd_pcm_lib_mmap_vmalloc,
1336};
1337
1338void snd_usb_set_pcm_ops(struct snd_pcm *pcm, int stream)
1339{
1340 snd_pcm_set_ops(pcm, stream,
1341 stream == SNDRV_PCM_STREAM_PLAYBACK ?
1342 &snd_usb_playback_ops : &snd_usb_capture_ops);
1343}