blob: ca3256d6fde3d1206ffe3fc7313305561438f611 [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 Mack44dcbbb2013-04-17 00:01:39 +080019#include <linux/bitrev.h>
Daniel Mackedcd3632012-04-12 13:51:12 +020020#include <linux/ratelimit.h>
Daniel Macke5779992010-03-04 19:46:13 +010021#include <linux/usb.h>
22#include <linux/usb/audio.h>
Daniel Mack7e847892010-03-11 21:13:20 +010023#include <linux/usb/audio-v2.h>
Daniel Macke5779992010-03-04 19:46:13 +010024
25#include <sound/core.h>
26#include <sound/pcm.h>
27#include <sound/pcm_params.h>
28
29#include "usbaudio.h"
30#include "card.h"
31#include "quirks.h"
32#include "debug.h"
Daniel Mackc731bc92011-09-14 12:46:57 +020033#include "endpoint.h"
Daniel Macke5779992010-03-04 19:46:13 +010034#include "helper.h"
35#include "pcm.h"
Daniel Mack79f920f2010-05-31 14:51:31 +020036#include "clock.h"
Oliver Neukum88a85162011-03-11 14:51:12 +010037#include "power.h"
Daniel Macke5779992010-03-04 19:46:13 +010038
Daniel Mackedcd3632012-04-12 13:51:12 +020039#define SUBSTREAM_FLAG_DATA_EP_STARTED 0
40#define SUBSTREAM_FLAG_SYNC_EP_STARTED 1
41
Pierre-Louis Bossart294c4fb2011-09-06 19:15:34 -050042/* return the estimated delay based on USB frame counters */
43snd_pcm_uframes_t snd_usb_pcm_delay(struct snd_usb_substream *subs,
44 unsigned int rate)
45{
46 int current_frame_number;
47 int frame_diff;
48 int est_delay;
49
Takashi Iwai48779a02012-11-23 16:00:37 +010050 if (!subs->last_delay)
51 return 0; /* short path */
52
Pierre-Louis Bossart294c4fb2011-09-06 19:15:34 -050053 current_frame_number = usb_get_current_frame_number(subs->dev);
54 /*
55 * HCD implementations use different widths, use lower 8 bits.
56 * The delay will be managed up to 256ms, which is more than
57 * enough
58 */
59 frame_diff = (current_frame_number - subs->last_frame_number) & 0xff;
60
61 /* Approximation based on number of samples per USB frame (ms),
62 some truncation for 44.1 but the estimate is good enough */
Pierre-Louis Bossarte4cc6152012-12-19 11:39:05 -060063 est_delay = frame_diff * rate / 1000;
64 if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK)
65 est_delay = subs->last_delay - est_delay;
66 else
67 est_delay = subs->last_delay + est_delay;
68
Pierre-Louis Bossart294c4fb2011-09-06 19:15:34 -050069 if (est_delay < 0)
70 est_delay = 0;
71 return est_delay;
72}
73
Daniel Macke5779992010-03-04 19:46:13 +010074/*
75 * return the current pcm pointer. just based on the hwptr_done value.
76 */
77static snd_pcm_uframes_t snd_usb_pcm_pointer(struct snd_pcm_substream *substream)
78{
79 struct snd_usb_substream *subs;
80 unsigned int hwptr_done;
81
82 subs = (struct snd_usb_substream *)substream->runtime->private_data;
Takashi Iwai978520b2012-10-12 15:12:55 +020083 if (subs->stream->chip->shutdown)
84 return SNDRV_PCM_POS_XRUN;
Daniel Macke5779992010-03-04 19:46:13 +010085 spin_lock(&subs->lock);
86 hwptr_done = subs->hwptr_done;
Pierre-Louis Bossarte4cc6152012-12-19 11:39:05 -060087 substream->runtime->delay = snd_usb_pcm_delay(subs,
Pierre-Louis Bossart294c4fb2011-09-06 19:15:34 -050088 substream->runtime->rate);
Daniel Macke5779992010-03-04 19:46:13 +010089 spin_unlock(&subs->lock);
90 return hwptr_done / (substream->runtime->frame_bits >> 3);
91}
92
93/*
94 * find a matching audio format
95 */
Dylan Reid61a70952012-09-18 09:49:48 -070096static struct audioformat *find_format(struct snd_usb_substream *subs)
Daniel Macke5779992010-03-04 19:46:13 +010097{
Eldad Zack88766f02013-04-03 23:18:49 +020098 struct audioformat *fp;
Daniel Macke5779992010-03-04 19:46:13 +010099 struct audioformat *found = NULL;
100 int cur_attr = 0, attr;
101
Eldad Zack88766f02013-04-03 23:18:49 +0200102 list_for_each_entry(fp, &subs->fmt_list, list) {
Eldad Zack74c34ca2013-04-23 01:00:41 +0200103 if (!(fp->formats & pcm_format_to_bits(subs->pcm_format)))
Clemens Ladisch015eb0b2010-03-04 19:46:15 +0100104 continue;
Dylan Reid61a70952012-09-18 09:49:48 -0700105 if (fp->channels != subs->channels)
Daniel Macke5779992010-03-04 19:46:13 +0100106 continue;
Dylan Reid61a70952012-09-18 09:49:48 -0700107 if (subs->cur_rate < fp->rate_min ||
108 subs->cur_rate > fp->rate_max)
Daniel Macke5779992010-03-04 19:46:13 +0100109 continue;
110 if (! (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)) {
111 unsigned int i;
112 for (i = 0; i < fp->nr_rates; i++)
Dylan Reid61a70952012-09-18 09:49:48 -0700113 if (fp->rate_table[i] == subs->cur_rate)
Daniel Macke5779992010-03-04 19:46:13 +0100114 break;
115 if (i >= fp->nr_rates)
116 continue;
117 }
118 attr = fp->ep_attr & USB_ENDPOINT_SYNCTYPE;
119 if (! found) {
120 found = fp;
121 cur_attr = attr;
122 continue;
123 }
124 /* avoid async out and adaptive in if the other method
125 * supports the same format.
126 * this is a workaround for the case like
127 * M-audio audiophile USB.
128 */
129 if (attr != cur_attr) {
130 if ((attr == USB_ENDPOINT_SYNC_ASYNC &&
131 subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
132 (attr == USB_ENDPOINT_SYNC_ADAPTIVE &&
133 subs->direction == SNDRV_PCM_STREAM_CAPTURE))
134 continue;
135 if ((cur_attr == USB_ENDPOINT_SYNC_ASYNC &&
136 subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
137 (cur_attr == USB_ENDPOINT_SYNC_ADAPTIVE &&
138 subs->direction == SNDRV_PCM_STREAM_CAPTURE)) {
139 found = fp;
140 cur_attr = attr;
141 continue;
142 }
143 }
144 /* find the format with the largest max. packet size */
145 if (fp->maxpacksize > found->maxpacksize) {
146 found = fp;
147 cur_attr = attr;
148 }
149 }
150 return found;
151}
152
Daniel Mack767d75a2010-03-04 19:46:17 +0100153static int init_pitch_v1(struct snd_usb_audio *chip, int iface,
154 struct usb_host_interface *alts,
155 struct audioformat *fmt)
Daniel Macke5779992010-03-04 19:46:13 +0100156{
Daniel Mack767d75a2010-03-04 19:46:17 +0100157 struct usb_device *dev = chip->dev;
Daniel Macke5779992010-03-04 19:46:13 +0100158 unsigned int ep;
159 unsigned char data[1];
160 int err;
161
162 ep = get_endpoint(alts, 0)->bEndpointAddress;
Daniel Mack767d75a2010-03-04 19:46:17 +0100163
Daniel Mack767d75a2010-03-04 19:46:17 +0100164 data[0] = 1;
165 if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC_SET_CUR,
166 USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT,
167 UAC_EP_CS_ATTR_PITCH_CONTROL << 8, ep,
Clemens Ladisch17d900c2011-09-26 21:15:27 +0200168 data, sizeof(data))) < 0) {
Daniel Mack767d75a2010-03-04 19:46:17 +0100169 snd_printk(KERN_ERR "%d:%d:%d: cannot set enable PITCH\n",
170 dev->devnum, iface, ep);
171 return err;
Daniel Macke5779992010-03-04 19:46:13 +0100172 }
Daniel Mack767d75a2010-03-04 19:46:17 +0100173
Daniel Macke5779992010-03-04 19:46:13 +0100174 return 0;
175}
176
Daniel Mack92c25612010-05-26 18:11:39 +0200177static int init_pitch_v2(struct snd_usb_audio *chip, int iface,
178 struct usb_host_interface *alts,
179 struct audioformat *fmt)
180{
181 struct usb_device *dev = chip->dev;
182 unsigned char data[1];
Daniel Mack92c25612010-05-26 18:11:39 +0200183 int err;
184
Daniel Mack92c25612010-05-26 18:11:39 +0200185 data[0] = 1;
186 if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC2_CS_CUR,
187 USB_TYPE_CLASS | USB_RECIP_ENDPOINT | USB_DIR_OUT,
188 UAC2_EP_CS_PITCH << 8, 0,
Clemens Ladisch17d900c2011-09-26 21:15:27 +0200189 data, sizeof(data))) < 0) {
Daniel Mack92c25612010-05-26 18:11:39 +0200190 snd_printk(KERN_ERR "%d:%d:%d: cannot set enable PITCH (v2)\n",
191 dev->devnum, iface, fmt->altsetting);
192 return err;
193 }
194
195 return 0;
196}
197
Daniel Mack767d75a2010-03-04 19:46:17 +0100198/*
Daniel Mack92c25612010-05-26 18:11:39 +0200199 * initialize the pitch control and sample rate
Daniel Mack767d75a2010-03-04 19:46:17 +0100200 */
201int snd_usb_init_pitch(struct snd_usb_audio *chip, int iface,
202 struct usb_host_interface *alts,
203 struct audioformat *fmt)
204{
Daniel Mack92c25612010-05-26 18:11:39 +0200205 /* if endpoint doesn't have pitch control, bail out */
206 if (!(fmt->attributes & UAC_EP_CS_ATTR_PITCH_CONTROL))
207 return 0;
208
Clemens Ladisch8f898e92013-01-31 21:39:17 +0100209 switch (fmt->protocol) {
Daniel Mack767d75a2010-03-04 19:46:17 +0100210 case UAC_VERSION_1:
Clemens Ladischa2acad82010-09-03 10:53:11 +0200211 default:
Daniel Mack767d75a2010-03-04 19:46:17 +0100212 return init_pitch_v1(chip, iface, alts, fmt);
213
214 case UAC_VERSION_2:
Daniel Mack92c25612010-05-26 18:11:39 +0200215 return init_pitch_v2(chip, iface, alts, fmt);
Daniel Mack767d75a2010-03-04 19:46:17 +0100216 }
Daniel Mack767d75a2010-03-04 19:46:17 +0100217}
218
Takashi Iwaia9bb3622012-11-20 18:32:06 +0100219static int start_endpoints(struct snd_usb_substream *subs, bool can_sleep)
Daniel Mackedcd3632012-04-12 13:51:12 +0200220{
221 int err;
222
223 if (!subs->data_endpoint)
224 return -EINVAL;
225
226 if (!test_and_set_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags)) {
227 struct snd_usb_endpoint *ep = subs->data_endpoint;
228
229 snd_printdd(KERN_DEBUG "Starting data EP @%p\n", ep);
230
231 ep->data_subs = subs;
Daniel Mack015618b2012-08-29 13:17:05 +0200232 err = snd_usb_endpoint_start(ep, can_sleep);
Daniel Mackedcd3632012-04-12 13:51:12 +0200233 if (err < 0) {
234 clear_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags);
235 return err;
236 }
237 }
238
239 if (subs->sync_endpoint &&
240 !test_and_set_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags)) {
241 struct snd_usb_endpoint *ep = subs->sync_endpoint;
242
Daniel Mack2e4a2632012-08-30 18:52:31 +0200243 if (subs->data_endpoint->iface != subs->sync_endpoint->iface ||
Eldad Zackdf23a242013-10-06 22:31:13 +0200244 subs->data_endpoint->altsetting != subs->sync_endpoint->altsetting) {
Daniel Mack2e4a2632012-08-30 18:52:31 +0200245 err = usb_set_interface(subs->dev,
246 subs->sync_endpoint->iface,
Eldad Zackdf23a242013-10-06 22:31:13 +0200247 subs->sync_endpoint->altsetting);
Daniel Mack2e4a2632012-08-30 18:52:31 +0200248 if (err < 0) {
Eldad Zack06613f52013-10-06 22:31:11 +0200249 clear_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags);
Daniel Mack2e4a2632012-08-30 18:52:31 +0200250 snd_printk(KERN_ERR
251 "%d:%d:%d: cannot set interface (%d)\n",
252 subs->dev->devnum,
253 subs->sync_endpoint->iface,
Eldad Zackdf23a242013-10-06 22:31:13 +0200254 subs->sync_endpoint->altsetting, err);
Daniel Mack2e4a2632012-08-30 18:52:31 +0200255 return -EIO;
256 }
257 }
258
Daniel Mackedcd3632012-04-12 13:51:12 +0200259 snd_printdd(KERN_DEBUG "Starting sync EP @%p\n", ep);
260
261 ep->sync_slave = subs->data_endpoint;
Daniel Mack015618b2012-08-29 13:17:05 +0200262 err = snd_usb_endpoint_start(ep, can_sleep);
Daniel Mackedcd3632012-04-12 13:51:12 +0200263 if (err < 0) {
264 clear_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags);
265 return err;
266 }
267 }
268
269 return 0;
270}
271
Takashi Iwaia9bb3622012-11-20 18:32:06 +0100272static void stop_endpoints(struct snd_usb_substream *subs, bool wait)
Daniel Mackedcd3632012-04-12 13:51:12 +0200273{
274 if (test_and_clear_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags))
Takashi Iwaib2eb9502012-11-21 08:30:48 +0100275 snd_usb_endpoint_stop(subs->sync_endpoint);
Daniel Mackedcd3632012-04-12 13:51:12 +0200276
277 if (test_and_clear_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags))
Takashi Iwaib2eb9502012-11-21 08:30:48 +0100278 snd_usb_endpoint_stop(subs->data_endpoint);
279
280 if (wait) {
281 snd_usb_endpoint_sync_pending_stop(subs->sync_endpoint);
282 snd_usb_endpoint_sync_pending_stop(subs->data_endpoint);
283 }
Daniel Mackedcd3632012-04-12 13:51:12 +0200284}
285
Clemens Ladischba7c2be2013-02-03 22:31:20 +0100286static int search_roland_implicit_fb(struct usb_device *dev, int ifnum,
287 unsigned int altsetting,
288 struct usb_host_interface **alts,
289 unsigned int *ep)
290{
291 struct usb_interface *iface;
292 struct usb_interface_descriptor *altsd;
293 struct usb_endpoint_descriptor *epd;
294
295 iface = usb_ifnum_to_if(dev, ifnum);
296 if (!iface || iface->num_altsetting < altsetting + 1)
297 return -ENOENT;
298 *alts = &iface->altsetting[altsetting];
299 altsd = get_iface_desc(*alts);
300 if (altsd->bAlternateSetting != altsetting ||
301 altsd->bInterfaceClass != USB_CLASS_VENDOR_SPEC ||
302 (altsd->bInterfaceSubClass != 2 &&
303 altsd->bInterfaceProtocol != 2 ) ||
304 altsd->bNumEndpoints < 1)
305 return -ENOENT;
306 epd = get_endpoint(*alts, 0);
307 if (!usb_endpoint_is_isoc_in(epd) ||
308 (epd->bmAttributes & USB_ENDPOINT_USAGE_MASK) !=
309 USB_ENDPOINT_USAGE_IMPLICIT_FB)
310 return -ENOENT;
311 *ep = epd->bEndpointAddress;
312 return 0;
313}
314
Eldad Zacka60945f2013-08-03 10:50:18 +0200315static int set_sync_ep_implicit_fb_quirk(struct snd_usb_substream *subs,
316 struct usb_device *dev,
317 struct usb_interface_descriptor *altsd,
318 unsigned int attr)
Daniel Macke5779992010-03-04 19:46:13 +0100319{
Eldad Zacka60945f2013-08-03 10:50:18 +0200320 struct usb_host_interface *alts;
Daniel Macke5779992010-03-04 19:46:13 +0100321 struct usb_interface *iface;
Eldad Zacka60945f2013-08-03 10:50:18 +0200322 unsigned int ep;
Daniel Mackc75a8a72012-04-12 13:51:14 +0200323
Eldad Zack914273c2013-08-03 10:50:21 +0200324 /* Implicit feedback sync EPs consumers are always playback EPs */
325 if (subs->direction != SNDRV_PCM_STREAM_PLAYBACK)
326 return 0;
327
Daniel Mackc75a8a72012-04-12 13:51:14 +0200328 switch (subs->stream->chip->usb_id) {
Eldad Zackca10a7e2012-11-28 23:55:41 +0100329 case USB_ID(0x0763, 0x2030): /* M-Audio Fast Track C400 */
Matt Gruskine9a25e02013-02-09 12:56:35 -0500330 case USB_ID(0x0763, 0x2031): /* M-Audio Fast Track C600 */
Eldad Zack914273c2013-08-03 10:50:21 +0200331 ep = 0x81;
332 iface = usb_ifnum_to_if(dev, 3);
Eldad Zackca10a7e2012-11-28 23:55:41 +0100333
Eldad Zack914273c2013-08-03 10:50:21 +0200334 if (!iface || iface->num_altsetting == 0)
335 return -EINVAL;
Eldad Zackca10a7e2012-11-28 23:55:41 +0100336
Eldad Zack914273c2013-08-03 10:50:21 +0200337 alts = &iface->altsetting[1];
338 goto add_sync_ep;
Eldad Zackca10a7e2012-11-28 23:55:41 +0100339 break;
Daniel Mackc75a8a72012-04-12 13:51:14 +0200340 case USB_ID(0x0763, 0x2080): /* M-Audio FastTrack Ultra */
341 case USB_ID(0x0763, 0x2081):
Eldad Zack914273c2013-08-03 10:50:21 +0200342 ep = 0x81;
343 iface = usb_ifnum_to_if(dev, 2);
Daniel Mackc75a8a72012-04-12 13:51:14 +0200344
Eldad Zack914273c2013-08-03 10:50:21 +0200345 if (!iface || iface->num_altsetting == 0)
346 return -EINVAL;
Daniel Mackc75a8a72012-04-12 13:51:14 +0200347
Eldad Zack914273c2013-08-03 10:50:21 +0200348 alts = &iface->altsetting[1];
349 goto add_sync_ep;
Daniel Mackc75a8a72012-04-12 13:51:14 +0200350 }
Eldad Zack914273c2013-08-03 10:50:21 +0200351 if (attr == USB_ENDPOINT_SYNC_ASYNC &&
Clemens Ladischba7c2be2013-02-03 22:31:20 +0100352 altsd->bInterfaceClass == USB_CLASS_VENDOR_SPEC &&
353 altsd->bInterfaceProtocol == 2 &&
354 altsd->bNumEndpoints == 1 &&
355 USB_ID_VENDOR(subs->stream->chip->usb_id) == 0x0582 /* Roland */ &&
356 search_roland_implicit_fb(dev, altsd->bInterfaceNumber + 1,
357 altsd->bAlternateSetting,
358 &alts, &ep) >= 0) {
Clemens Ladischba7c2be2013-02-03 22:31:20 +0100359 goto add_sync_ep;
360 }
Daniel Mackedcd3632012-04-12 13:51:12 +0200361
Eldad Zacka60945f2013-08-03 10:50:18 +0200362 /* No quirk */
363 return 0;
364
365add_sync_ep:
366 subs->sync_endpoint = snd_usb_add_endpoint(subs->stream->chip,
367 alts, ep, !subs->direction,
Eldad Zack88abb8e2013-08-03 10:51:14 +0200368 SND_USB_ENDPOINT_TYPE_DATA);
Eldad Zacka60945f2013-08-03 10:50:18 +0200369 if (!subs->sync_endpoint)
370 return -EINVAL;
371
372 subs->data_endpoint->sync_master = subs->sync_endpoint;
373
374 return 0;
375}
376
377static int set_sync_endpoint(struct snd_usb_substream *subs,
378 struct audioformat *fmt,
379 struct usb_device *dev,
380 struct usb_host_interface *alts,
381 struct usb_interface_descriptor *altsd)
382{
383 int is_playback = subs->direction == SNDRV_PCM_STREAM_PLAYBACK;
384 unsigned int ep, attr;
Eldad Zack95fec882013-08-03 10:50:20 +0200385 bool implicit_fb;
Eldad Zacka60945f2013-08-03 10:50:18 +0200386 int err;
387
388 /* we need a sync pipe in async OUT or adaptive IN mode */
389 /* check the number of EP, since some devices have broken
390 * descriptors which fool us. if it has only one EP,
391 * assume it as adaptive-out or sync-in.
392 */
393 attr = fmt->ep_attr & USB_ENDPOINT_SYNCTYPE;
394
395 err = set_sync_ep_implicit_fb_quirk(subs, dev, altsd, attr);
396 if (err < 0)
397 return err;
398
Eldad Zackf34d0652013-08-03 10:50:19 +0200399 if (altsd->bNumEndpoints < 2)
400 return 0;
Daniel Mackc75a8a72012-04-12 13:51:14 +0200401
Eldad Zackf34d0652013-08-03 10:50:19 +0200402 if ((is_playback && attr != USB_ENDPOINT_SYNC_ASYNC) ||
403 (!is_playback && attr != USB_ENDPOINT_SYNC_ADAPTIVE))
404 return 0;
Daniel Mackc75a8a72012-04-12 13:51:14 +0200405
Eldad Zackf34d0652013-08-03 10:50:19 +0200406 /* check sync-pipe endpoint */
407 /* ... and check descriptor size before accessing bSynchAddress
408 because there is a version of the SB Audigy 2 NX firmware lacking
409 the audio fields in the endpoint descriptors */
410 if ((get_endpoint(alts, 1)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_ISOC ||
411 (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
Eldad Zack95fec882013-08-03 10:50:20 +0200412 get_endpoint(alts, 1)->bSynchAddress != 0)) {
Eldad Zackf34d0652013-08-03 10:50:19 +0200413 snd_printk(KERN_ERR "%d:%d:%d : invalid sync pipe. bmAttributes %02x, bLength %d, bSynchAddress %02x\n",
414 dev->devnum, fmt->iface, fmt->altsetting,
415 get_endpoint(alts, 1)->bmAttributes,
416 get_endpoint(alts, 1)->bLength,
417 get_endpoint(alts, 1)->bSynchAddress);
418 return -EINVAL;
Daniel Mackedcd3632012-04-12 13:51:12 +0200419 }
Eldad Zackf34d0652013-08-03 10:50:19 +0200420 ep = get_endpoint(alts, 1)->bEndpointAddress;
Eldad Zack95fec882013-08-03 10:50:20 +0200421 if (get_endpoint(alts, 0)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
Eldad Zackf34d0652013-08-03 10:50:19 +0200422 ((is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress | USB_DIR_IN)) ||
423 (!is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress & ~USB_DIR_IN)))) {
424 snd_printk(KERN_ERR "%d:%d:%d : invalid sync pipe. is_playback %d, ep %02x, bSynchAddress %02x\n",
425 dev->devnum, fmt->iface, fmt->altsetting,
426 is_playback, ep, get_endpoint(alts, 0)->bSynchAddress);
427 return -EINVAL;
428 }
429
430 implicit_fb = (get_endpoint(alts, 1)->bmAttributes & USB_ENDPOINT_USAGE_MASK)
431 == USB_ENDPOINT_USAGE_IMPLICIT_FB;
432
433 subs->sync_endpoint = snd_usb_add_endpoint(subs->stream->chip,
434 alts, ep, !subs->direction,
435 implicit_fb ?
436 SND_USB_ENDPOINT_TYPE_DATA :
437 SND_USB_ENDPOINT_TYPE_SYNC);
438 if (!subs->sync_endpoint)
439 return -EINVAL;
440
441 subs->data_endpoint->sync_master = subs->sync_endpoint;
Daniel Macke5779992010-03-04 19:46:13 +0100442
Eldad Zack71bb64c2013-08-03 10:50:17 +0200443 return 0;
444}
445
446/*
447 * find a matching format and set up the interface
448 */
449static int set_format(struct snd_usb_substream *subs, struct audioformat *fmt)
450{
451 struct usb_device *dev = subs->dev;
452 struct usb_host_interface *alts;
453 struct usb_interface_descriptor *altsd;
454 struct usb_interface *iface;
455 int err;
456
457 iface = usb_ifnum_to_if(dev, fmt->iface);
458 if (WARN_ON(!iface))
459 return -EINVAL;
460 alts = &iface->altsetting[fmt->altset_idx];
461 altsd = get_iface_desc(alts);
462 if (WARN_ON(altsd->bAlternateSetting != fmt->altsetting))
463 return -EINVAL;
464
465 if (fmt == subs->cur_audiofmt)
466 return 0;
467
468 /* close the old interface */
469 if (subs->interface >= 0 && subs->interface != fmt->iface) {
470 err = usb_set_interface(subs->dev, subs->interface, 0);
471 if (err < 0) {
472 snd_printk(KERN_ERR "%d:%d:%d: return to setting 0 failed (%d)\n",
473 dev->devnum, fmt->iface, fmt->altsetting, err);
474 return -EIO;
475 }
476 subs->interface = -1;
477 subs->altset_idx = 0;
478 }
479
480 /* set interface */
481 if (subs->interface != fmt->iface ||
482 subs->altset_idx != fmt->altset_idx) {
483 err = usb_set_interface(dev, fmt->iface, fmt->altsetting);
484 if (err < 0) {
485 snd_printk(KERN_ERR "%d:%d:%d: usb_set_interface failed (%d)\n",
486 dev->devnum, fmt->iface, fmt->altsetting, err);
487 return -EIO;
488 }
489 snd_printdd(KERN_INFO "setting usb interface %d:%d\n",
490 fmt->iface, fmt->altsetting);
491 subs->interface = fmt->iface;
492 subs->altset_idx = fmt->altset_idx;
493
494 snd_usb_set_interface_quirk(dev);
495 }
496
497 subs->data_endpoint = snd_usb_add_endpoint(subs->stream->chip,
498 alts, fmt->endpoint, subs->direction,
499 SND_USB_ENDPOINT_TYPE_DATA);
500
501 if (!subs->data_endpoint)
502 return -EINVAL;
503
504 err = set_sync_endpoint(subs, fmt, dev, alts, altsd);
505 if (err < 0)
506 return err;
507
Eldad Zackd133f2c2013-08-03 10:50:16 +0200508 err = snd_usb_init_pitch(subs->stream->chip, fmt->iface, alts, fmt);
509 if (err < 0)
Daniel Macke5779992010-03-04 19:46:13 +0100510 return err;
511
512 subs->cur_audiofmt = fmt;
513
514 snd_usb_set_format_quirk(subs, fmt);
515
Daniel Macke5779992010-03-04 19:46:13 +0100516 return 0;
517}
518
519/*
Eldad Zack0d9741c2012-12-03 20:30:09 +0100520 * Return the score of matching two audioformats.
521 * Veto the audioformat if:
522 * - It has no channels for some reason.
523 * - Requested PCM format is not supported.
524 * - Requested sample rate is not supported.
525 */
526static int match_endpoint_audioformats(struct audioformat *fp,
527 struct audioformat *match, int rate,
528 snd_pcm_format_t pcm_format)
529{
530 int i;
531 int score = 0;
532
533 if (fp->channels < 1) {
534 snd_printdd("%s: (fmt @%p) no channels\n", __func__, fp);
535 return 0;
536 }
537
Eldad Zack74c34ca2013-04-23 01:00:41 +0200538 if (!(fp->formats & pcm_format_to_bits(pcm_format))) {
Eldad Zack0d9741c2012-12-03 20:30:09 +0100539 snd_printdd("%s: (fmt @%p) no match for format %d\n", __func__,
540 fp, pcm_format);
541 return 0;
542 }
543
544 for (i = 0; i < fp->nr_rates; i++) {
545 if (fp->rate_table[i] == rate) {
546 score++;
547 break;
548 }
549 }
550 if (!score) {
551 snd_printdd("%s: (fmt @%p) no match for rate %d\n", __func__,
552 fp, rate);
553 return 0;
554 }
555
556 if (fp->channels == match->channels)
557 score++;
558
559 snd_printdd("%s: (fmt @%p) score %d\n", __func__, fp, score);
560
561 return score;
562}
563
564/*
565 * Configure the sync ep using the rate and pcm format of the data ep.
566 */
567static int configure_sync_endpoint(struct snd_usb_substream *subs)
568{
569 int ret;
570 struct audioformat *fp;
571 struct audioformat *sync_fp = NULL;
572 int cur_score = 0;
573 int sync_period_bytes = subs->period_bytes;
574 struct snd_usb_substream *sync_subs =
575 &subs->stream->substream[subs->direction ^ 1];
576
Takashi Iwai31be5422013-01-10 14:06:38 +0100577 if (subs->sync_endpoint->type != SND_USB_ENDPOINT_TYPE_DATA ||
578 !subs->stream)
579 return snd_usb_endpoint_set_params(subs->sync_endpoint,
580 subs->pcm_format,
581 subs->channels,
582 subs->period_bytes,
Alan Stern976b6c02013-09-24 15:51:58 -0400583 0, 0,
Takashi Iwai31be5422013-01-10 14:06:38 +0100584 subs->cur_rate,
585 subs->cur_audiofmt,
586 NULL);
587
Eldad Zack0d9741c2012-12-03 20:30:09 +0100588 /* Try to find the best matching audioformat. */
589 list_for_each_entry(fp, &sync_subs->fmt_list, list) {
590 int score = match_endpoint_audioformats(fp, subs->cur_audiofmt,
591 subs->cur_rate, subs->pcm_format);
592
593 if (score > cur_score) {
594 sync_fp = fp;
595 cur_score = score;
596 }
597 }
598
599 if (unlikely(sync_fp == NULL)) {
600 snd_printk(KERN_ERR "%s: no valid audioformat for sync ep %x found\n",
601 __func__, sync_subs->ep_num);
602 return -EINVAL;
603 }
604
605 /*
606 * Recalculate the period bytes if channel number differ between
607 * data and sync ep audioformat.
608 */
609 if (sync_fp->channels != subs->channels) {
610 sync_period_bytes = (subs->period_bytes / subs->channels) *
611 sync_fp->channels;
612 snd_printdd("%s: adjusted sync ep period bytes (%d -> %d)\n",
613 __func__, subs->period_bytes, sync_period_bytes);
614 }
615
616 ret = snd_usb_endpoint_set_params(subs->sync_endpoint,
617 subs->pcm_format,
618 sync_fp->channels,
619 sync_period_bytes,
Alan Stern976b6c02013-09-24 15:51:58 -0400620 0, 0,
Eldad Zack0d9741c2012-12-03 20:30:09 +0100621 subs->cur_rate,
622 sync_fp,
623 NULL);
624
625 return ret;
626}
627
628/*
Dylan Reid61a70952012-09-18 09:49:48 -0700629 * configure endpoint params
630 *
631 * called during initial setup and upon resume
632 */
633static int configure_endpoint(struct snd_usb_substream *subs)
634{
635 int ret;
636
Dylan Reid61a70952012-09-18 09:49:48 -0700637 /* format changed */
Takashi Iwaib0db6062012-11-21 08:35:42 +0100638 stop_endpoints(subs, true);
Dylan Reid61a70952012-09-18 09:49:48 -0700639 ret = snd_usb_endpoint_set_params(subs->data_endpoint,
640 subs->pcm_format,
641 subs->channels,
642 subs->period_bytes,
Alan Stern976b6c02013-09-24 15:51:58 -0400643 subs->period_frames,
644 subs->buffer_periods,
Dylan Reid61a70952012-09-18 09:49:48 -0700645 subs->cur_rate,
646 subs->cur_audiofmt,
647 subs->sync_endpoint);
648 if (ret < 0)
Takashi Iwai978520b2012-10-12 15:12:55 +0200649 return ret;
Dylan Reid61a70952012-09-18 09:49:48 -0700650
651 if (subs->sync_endpoint)
Eldad Zack0d9741c2012-12-03 20:30:09 +0100652 ret = configure_sync_endpoint(subs);
653
Dylan Reid61a70952012-09-18 09:49:48 -0700654 return ret;
655}
656
657/*
Daniel Macke5779992010-03-04 19:46:13 +0100658 * hw_params callback
659 *
660 * allocate a buffer and set the given audio format.
661 *
662 * so far we use a physically linear buffer although packetize transfer
663 * doesn't need a continuous area.
664 * if sg buffer is supported on the later version of alsa, we'll follow
665 * that.
666 */
667static int snd_usb_hw_params(struct snd_pcm_substream *substream,
668 struct snd_pcm_hw_params *hw_params)
669{
670 struct snd_usb_substream *subs = substream->runtime->private_data;
671 struct audioformat *fmt;
Dylan Reid61a70952012-09-18 09:49:48 -0700672 int ret;
Daniel Macke5779992010-03-04 19:46:13 +0100673
674 ret = snd_pcm_lib_alloc_vmalloc_buffer(substream,
675 params_buffer_bytes(hw_params));
676 if (ret < 0)
677 return ret;
678
Dylan Reid61a70952012-09-18 09:49:48 -0700679 subs->pcm_format = params_format(hw_params);
680 subs->period_bytes = params_period_bytes(hw_params);
Alan Stern976b6c02013-09-24 15:51:58 -0400681 subs->period_frames = params_period_size(hw_params);
682 subs->buffer_periods = params_periods(hw_params);
Dylan Reid61a70952012-09-18 09:49:48 -0700683 subs->channels = params_channels(hw_params);
684 subs->cur_rate = params_rate(hw_params);
685
686 fmt = find_format(subs);
Daniel Macke5779992010-03-04 19:46:13 +0100687 if (!fmt) {
688 snd_printd(KERN_DEBUG "cannot set format: format = %#x, rate = %d, channels = %d\n",
Dylan Reid61a70952012-09-18 09:49:48 -0700689 subs->pcm_format, subs->cur_rate, subs->channels);
Daniel Macke5779992010-03-04 19:46:13 +0100690 return -EINVAL;
691 }
692
Takashi Iwai34f3c892012-10-15 12:16:02 +0200693 down_read(&subs->stream->chip->shutdown_rwsem);
Takashi Iwai978520b2012-10-12 15:12:55 +0200694 if (subs->stream->chip->shutdown)
695 ret = -ENODEV;
696 else
697 ret = set_format(subs, fmt);
Takashi Iwai34f3c892012-10-15 12:16:02 +0200698 up_read(&subs->stream->chip->shutdown_rwsem);
Takashi Iwai978520b2012-10-12 15:12:55 +0200699 if (ret < 0)
Daniel Macke5779992010-03-04 19:46:13 +0100700 return ret;
701
Dylan Reid61a70952012-09-18 09:49:48 -0700702 subs->interface = fmt->iface;
703 subs->altset_idx = fmt->altset_idx;
Takashi Iwai384dc0852012-09-18 14:49:31 +0200704 subs->need_setup_ep = true;
Daniel Macke5779992010-03-04 19:46:13 +0100705
Dylan Reid61a70952012-09-18 09:49:48 -0700706 return 0;
Daniel Macke5779992010-03-04 19:46:13 +0100707}
708
709/*
710 * hw_free callback
711 *
712 * reset the audio format and release the buffer
713 */
714static int snd_usb_hw_free(struct snd_pcm_substream *substream)
715{
716 struct snd_usb_substream *subs = substream->runtime->private_data;
717
718 subs->cur_audiofmt = NULL;
719 subs->cur_rate = 0;
720 subs->period_bytes = 0;
Takashi Iwai34f3c892012-10-15 12:16:02 +0200721 down_read(&subs->stream->chip->shutdown_rwsem);
Takashi Iwai978520b2012-10-12 15:12:55 +0200722 if (!subs->stream->chip->shutdown) {
Takashi Iwaia9bb3622012-11-20 18:32:06 +0100723 stop_endpoints(subs, true);
Eldad Zack26de5d02013-10-06 22:31:07 +0200724 snd_usb_endpoint_deactivate(subs->sync_endpoint);
725 snd_usb_endpoint_deactivate(subs->data_endpoint);
Takashi Iwai978520b2012-10-12 15:12:55 +0200726 }
Takashi Iwai34f3c892012-10-15 12:16:02 +0200727 up_read(&subs->stream->chip->shutdown_rwsem);
Daniel Macke5779992010-03-04 19:46:13 +0100728 return snd_pcm_lib_free_vmalloc_buffer(substream);
729}
730
731/*
732 * prepare callback
733 *
734 * only a few subtle things...
735 */
736static int snd_usb_pcm_prepare(struct snd_pcm_substream *substream)
737{
738 struct snd_pcm_runtime *runtime = substream->runtime;
739 struct snd_usb_substream *subs = runtime->private_data;
Dylan Reid61a70952012-09-18 09:49:48 -0700740 struct usb_host_interface *alts;
741 struct usb_interface *iface;
742 int ret;
Daniel Macke5779992010-03-04 19:46:13 +0100743
744 if (! subs->cur_audiofmt) {
745 snd_printk(KERN_ERR "usbaudio: no format is specified!\n");
746 return -ENXIO;
747 }
748
Takashi Iwai34f3c892012-10-15 12:16:02 +0200749 down_read(&subs->stream->chip->shutdown_rwsem);
Takashi Iwai978520b2012-10-12 15:12:55 +0200750 if (subs->stream->chip->shutdown) {
751 ret = -ENODEV;
752 goto unlock;
753 }
754 if (snd_BUG_ON(!subs->data_endpoint)) {
755 ret = -EIO;
756 goto unlock;
757 }
Daniel Mackedcd3632012-04-12 13:51:12 +0200758
Takashi Iwaif58161b2012-11-08 08:52:45 +0100759 snd_usb_endpoint_sync_pending_stop(subs->sync_endpoint);
760 snd_usb_endpoint_sync_pending_stop(subs->data_endpoint);
761
Dylan Reid61a70952012-09-18 09:49:48 -0700762 ret = set_format(subs, subs->cur_audiofmt);
763 if (ret < 0)
Takashi Iwai978520b2012-10-12 15:12:55 +0200764 goto unlock;
Dylan Reid61a70952012-09-18 09:49:48 -0700765
766 iface = usb_ifnum_to_if(subs->dev, subs->cur_audiofmt->iface);
767 alts = &iface->altsetting[subs->cur_audiofmt->altset_idx];
768 ret = snd_usb_init_sample_rate(subs->stream->chip,
769 subs->cur_audiofmt->iface,
770 alts,
771 subs->cur_audiofmt,
772 subs->cur_rate);
773 if (ret < 0)
Takashi Iwai978520b2012-10-12 15:12:55 +0200774 goto unlock;
Dylan Reid61a70952012-09-18 09:49:48 -0700775
Takashi Iwai384dc0852012-09-18 14:49:31 +0200776 if (subs->need_setup_ep) {
777 ret = configure_endpoint(subs);
778 if (ret < 0)
Takashi Iwai978520b2012-10-12 15:12:55 +0200779 goto unlock;
Takashi Iwai384dc0852012-09-18 14:49:31 +0200780 subs->need_setup_ep = false;
781 }
Dylan Reid61a70952012-09-18 09:49:48 -0700782
Daniel Macke5779992010-03-04 19:46:13 +0100783 /* some unit conversions in runtime */
Daniel Mackedcd3632012-04-12 13:51:12 +0200784 subs->data_endpoint->maxframesize =
785 bytes_to_frames(runtime, subs->data_endpoint->maxpacksize);
786 subs->data_endpoint->curframesize =
787 bytes_to_frames(runtime, subs->data_endpoint->curpacksize);
Daniel Macke5779992010-03-04 19:46:13 +0100788
789 /* reset the pointer */
790 subs->hwptr_done = 0;
791 subs->transfer_done = 0;
Pierre-Louis Bossart294c4fb2011-09-06 19:15:34 -0500792 subs->last_delay = 0;
793 subs->last_frame_number = 0;
Daniel Macke5779992010-03-04 19:46:13 +0100794 runtime->delay = 0;
795
Daniel Mackedcd3632012-04-12 13:51:12 +0200796 /* for playback, submit the URBs now; otherwise, the first hwptr_done
797 * updates for all URBs would happen at the same time when starting */
798 if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK)
Takashi Iwaia9bb3622012-11-20 18:32:06 +0100799 ret = start_endpoints(subs, true);
Daniel Mackedcd3632012-04-12 13:51:12 +0200800
Takashi Iwai978520b2012-10-12 15:12:55 +0200801 unlock:
Takashi Iwai34f3c892012-10-15 12:16:02 +0200802 up_read(&subs->stream->chip->shutdown_rwsem);
Takashi Iwai978520b2012-10-12 15:12:55 +0200803 return ret;
Daniel Macke5779992010-03-04 19:46:13 +0100804}
805
806static struct snd_pcm_hardware snd_usb_hardware =
807{
808 .info = SNDRV_PCM_INFO_MMAP |
809 SNDRV_PCM_INFO_MMAP_VALID |
810 SNDRV_PCM_INFO_BATCH |
811 SNDRV_PCM_INFO_INTERLEAVED |
812 SNDRV_PCM_INFO_BLOCK_TRANSFER |
813 SNDRV_PCM_INFO_PAUSE,
814 .buffer_bytes_max = 1024 * 1024,
815 .period_bytes_min = 64,
816 .period_bytes_max = 512 * 1024,
817 .periods_min = 2,
818 .periods_max = 1024,
819};
820
821static int hw_check_valid_format(struct snd_usb_substream *subs,
822 struct snd_pcm_hw_params *params,
823 struct audioformat *fp)
824{
825 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
826 struct snd_interval *ct = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
827 struct snd_mask *fmts = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
828 struct snd_interval *pt = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
Clemens Ladisch015eb0b2010-03-04 19:46:15 +0100829 struct snd_mask check_fmts;
Daniel Macke5779992010-03-04 19:46:13 +0100830 unsigned int ptime;
831
832 /* check the format */
Clemens Ladisch015eb0b2010-03-04 19:46:15 +0100833 snd_mask_none(&check_fmts);
834 check_fmts.bits[0] = (u32)fp->formats;
835 check_fmts.bits[1] = (u32)(fp->formats >> 32);
836 snd_mask_intersect(&check_fmts, fmts);
837 if (snd_mask_empty(&check_fmts)) {
Daniel Macke5779992010-03-04 19:46:13 +0100838 hwc_debug(" > check: no supported format %d\n", fp->format);
839 return 0;
840 }
841 /* check the channels */
842 if (fp->channels < ct->min || fp->channels > ct->max) {
843 hwc_debug(" > check: no valid channels %d (%d/%d)\n", fp->channels, ct->min, ct->max);
844 return 0;
845 }
846 /* check the rate is within the range */
847 if (fp->rate_min > it->max || (fp->rate_min == it->max && it->openmax)) {
848 hwc_debug(" > check: rate_min %d > max %d\n", fp->rate_min, it->max);
849 return 0;
850 }
851 if (fp->rate_max < it->min || (fp->rate_max == it->min && it->openmin)) {
852 hwc_debug(" > check: rate_max %d < min %d\n", fp->rate_max, it->min);
853 return 0;
854 }
855 /* check whether the period time is >= the data packet interval */
Takashi Iwai978520b2012-10-12 15:12:55 +0200856 if (subs->speed != USB_SPEED_FULL) {
Daniel Macke5779992010-03-04 19:46:13 +0100857 ptime = 125 * (1 << fp->datainterval);
858 if (ptime > pt->max || (ptime == pt->max && pt->openmax)) {
859 hwc_debug(" > check: ptime %u > max %u\n", ptime, pt->max);
860 return 0;
861 }
862 }
863 return 1;
864}
865
866static int hw_rule_rate(struct snd_pcm_hw_params *params,
867 struct snd_pcm_hw_rule *rule)
868{
869 struct snd_usb_substream *subs = rule->private;
Eldad Zack88766f02013-04-03 23:18:49 +0200870 struct audioformat *fp;
Daniel Macke5779992010-03-04 19:46:13 +0100871 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
872 unsigned int rmin, rmax;
873 int changed;
874
875 hwc_debug("hw_rule_rate: (%d,%d)\n", it->min, it->max);
876 changed = 0;
877 rmin = rmax = 0;
Eldad Zack88766f02013-04-03 23:18:49 +0200878 list_for_each_entry(fp, &subs->fmt_list, list) {
Daniel Macke5779992010-03-04 19:46:13 +0100879 if (!hw_check_valid_format(subs, params, fp))
880 continue;
881 if (changed++) {
882 if (rmin > fp->rate_min)
883 rmin = fp->rate_min;
884 if (rmax < fp->rate_max)
885 rmax = fp->rate_max;
886 } else {
887 rmin = fp->rate_min;
888 rmax = fp->rate_max;
889 }
890 }
891
892 if (!changed) {
893 hwc_debug(" --> get empty\n");
894 it->empty = 1;
895 return -EINVAL;
896 }
897
898 changed = 0;
899 if (it->min < rmin) {
900 it->min = rmin;
901 it->openmin = 0;
902 changed = 1;
903 }
904 if (it->max > rmax) {
905 it->max = rmax;
906 it->openmax = 0;
907 changed = 1;
908 }
909 if (snd_interval_checkempty(it)) {
910 it->empty = 1;
911 return -EINVAL;
912 }
913 hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
914 return changed;
915}
916
917
918static int hw_rule_channels(struct snd_pcm_hw_params *params,
919 struct snd_pcm_hw_rule *rule)
920{
921 struct snd_usb_substream *subs = rule->private;
Eldad Zack88766f02013-04-03 23:18:49 +0200922 struct audioformat *fp;
Daniel Macke5779992010-03-04 19:46:13 +0100923 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
924 unsigned int rmin, rmax;
925 int changed;
926
927 hwc_debug("hw_rule_channels: (%d,%d)\n", it->min, it->max);
928 changed = 0;
929 rmin = rmax = 0;
Eldad Zack88766f02013-04-03 23:18:49 +0200930 list_for_each_entry(fp, &subs->fmt_list, list) {
Daniel Macke5779992010-03-04 19:46:13 +0100931 if (!hw_check_valid_format(subs, params, fp))
932 continue;
933 if (changed++) {
934 if (rmin > fp->channels)
935 rmin = fp->channels;
936 if (rmax < fp->channels)
937 rmax = fp->channels;
938 } else {
939 rmin = fp->channels;
940 rmax = fp->channels;
941 }
942 }
943
944 if (!changed) {
945 hwc_debug(" --> get empty\n");
946 it->empty = 1;
947 return -EINVAL;
948 }
949
950 changed = 0;
951 if (it->min < rmin) {
952 it->min = rmin;
953 it->openmin = 0;
954 changed = 1;
955 }
956 if (it->max > rmax) {
957 it->max = rmax;
958 it->openmax = 0;
959 changed = 1;
960 }
961 if (snd_interval_checkempty(it)) {
962 it->empty = 1;
963 return -EINVAL;
964 }
965 hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
966 return changed;
967}
968
969static int hw_rule_format(struct snd_pcm_hw_params *params,
970 struct snd_pcm_hw_rule *rule)
971{
972 struct snd_usb_substream *subs = rule->private;
Eldad Zack88766f02013-04-03 23:18:49 +0200973 struct audioformat *fp;
Daniel Macke5779992010-03-04 19:46:13 +0100974 struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
975 u64 fbits;
976 u32 oldbits[2];
977 int changed;
978
979 hwc_debug("hw_rule_format: %x:%x\n", fmt->bits[0], fmt->bits[1]);
980 fbits = 0;
Eldad Zack88766f02013-04-03 23:18:49 +0200981 list_for_each_entry(fp, &subs->fmt_list, list) {
Daniel Macke5779992010-03-04 19:46:13 +0100982 if (!hw_check_valid_format(subs, params, fp))
983 continue;
Clemens Ladisch015eb0b2010-03-04 19:46:15 +0100984 fbits |= fp->formats;
Daniel Macke5779992010-03-04 19:46:13 +0100985 }
986
987 oldbits[0] = fmt->bits[0];
988 oldbits[1] = fmt->bits[1];
989 fmt->bits[0] &= (u32)fbits;
990 fmt->bits[1] &= (u32)(fbits >> 32);
991 if (!fmt->bits[0] && !fmt->bits[1]) {
992 hwc_debug(" --> get empty\n");
993 return -EINVAL;
994 }
995 changed = (oldbits[0] != fmt->bits[0] || oldbits[1] != fmt->bits[1]);
996 hwc_debug(" --> %x:%x (changed = %d)\n", fmt->bits[0], fmt->bits[1], changed);
997 return changed;
998}
999
1000static int hw_rule_period_time(struct snd_pcm_hw_params *params,
1001 struct snd_pcm_hw_rule *rule)
1002{
1003 struct snd_usb_substream *subs = rule->private;
1004 struct audioformat *fp;
1005 struct snd_interval *it;
1006 unsigned char min_datainterval;
1007 unsigned int pmin;
1008 int changed;
1009
1010 it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
1011 hwc_debug("hw_rule_period_time: (%u,%u)\n", it->min, it->max);
1012 min_datainterval = 0xff;
1013 list_for_each_entry(fp, &subs->fmt_list, list) {
1014 if (!hw_check_valid_format(subs, params, fp))
1015 continue;
1016 min_datainterval = min(min_datainterval, fp->datainterval);
1017 }
1018 if (min_datainterval == 0xff) {
Uwe Kleine-Königa7ce2e02010-07-12 17:15:44 +02001019 hwc_debug(" --> get empty\n");
Daniel Macke5779992010-03-04 19:46:13 +01001020 it->empty = 1;
1021 return -EINVAL;
1022 }
1023 pmin = 125 * (1 << min_datainterval);
1024 changed = 0;
1025 if (it->min < pmin) {
1026 it->min = pmin;
1027 it->openmin = 0;
1028 changed = 1;
1029 }
1030 if (snd_interval_checkempty(it)) {
1031 it->empty = 1;
1032 return -EINVAL;
1033 }
1034 hwc_debug(" --> (%u,%u) (changed = %d)\n", it->min, it->max, changed);
1035 return changed;
1036}
1037
1038/*
1039 * If the device supports unusual bit rates, does the request meet these?
1040 */
1041static int snd_usb_pcm_check_knot(struct snd_pcm_runtime *runtime,
1042 struct snd_usb_substream *subs)
1043{
1044 struct audioformat *fp;
Takashi Iwai0717d0f2012-03-15 16:14:38 +01001045 int *rate_list;
Daniel Macke5779992010-03-04 19:46:13 +01001046 int count = 0, needs_knot = 0;
1047 int err;
1048
Clemens Ladisch5cd5d7c2012-05-18 18:00:43 +02001049 kfree(subs->rate_list.list);
1050 subs->rate_list.list = NULL;
1051
Daniel Macke5779992010-03-04 19:46:13 +01001052 list_for_each_entry(fp, &subs->fmt_list, list) {
1053 if (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)
1054 return 0;
1055 count += fp->nr_rates;
1056 if (fp->rates & SNDRV_PCM_RATE_KNOT)
1057 needs_knot = 1;
1058 }
1059 if (!needs_knot)
1060 return 0;
1061
Takashi Iwai0717d0f2012-03-15 16:14:38 +01001062 subs->rate_list.list = rate_list =
1063 kmalloc(sizeof(int) * count, GFP_KERNEL);
Jesper Juhl8a8d56b2010-10-29 20:40:23 +02001064 if (!subs->rate_list.list)
1065 return -ENOMEM;
1066 subs->rate_list.count = count;
Daniel Macke5779992010-03-04 19:46:13 +01001067 subs->rate_list.mask = 0;
1068 count = 0;
1069 list_for_each_entry(fp, &subs->fmt_list, list) {
1070 int i;
1071 for (i = 0; i < fp->nr_rates; i++)
Takashi Iwai0717d0f2012-03-15 16:14:38 +01001072 rate_list[count++] = fp->rate_table[i];
Daniel Macke5779992010-03-04 19:46:13 +01001073 }
1074 err = snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1075 &subs->rate_list);
1076 if (err < 0)
1077 return err;
1078
1079 return 0;
1080}
1081
1082
1083/*
1084 * set up the runtime hardware information.
1085 */
1086
1087static int setup_hw_info(struct snd_pcm_runtime *runtime, struct snd_usb_substream *subs)
1088{
Eldad Zack88766f02013-04-03 23:18:49 +02001089 struct audioformat *fp;
Daniel Macke5779992010-03-04 19:46:13 +01001090 unsigned int pt, ptmin;
1091 int param_period_time_if_needed;
1092 int err;
1093
1094 runtime->hw.formats = subs->formats;
1095
1096 runtime->hw.rate_min = 0x7fffffff;
1097 runtime->hw.rate_max = 0;
1098 runtime->hw.channels_min = 256;
1099 runtime->hw.channels_max = 0;
1100 runtime->hw.rates = 0;
1101 ptmin = UINT_MAX;
1102 /* check min/max rates and channels */
Eldad Zack88766f02013-04-03 23:18:49 +02001103 list_for_each_entry(fp, &subs->fmt_list, list) {
Daniel Macke5779992010-03-04 19:46:13 +01001104 runtime->hw.rates |= fp->rates;
1105 if (runtime->hw.rate_min > fp->rate_min)
1106 runtime->hw.rate_min = fp->rate_min;
1107 if (runtime->hw.rate_max < fp->rate_max)
1108 runtime->hw.rate_max = fp->rate_max;
1109 if (runtime->hw.channels_min > fp->channels)
1110 runtime->hw.channels_min = fp->channels;
1111 if (runtime->hw.channels_max < fp->channels)
1112 runtime->hw.channels_max = fp->channels;
1113 if (fp->fmt_type == UAC_FORMAT_TYPE_II && fp->frame_size > 0) {
1114 /* FIXME: there might be more than one audio formats... */
1115 runtime->hw.period_bytes_min = runtime->hw.period_bytes_max =
1116 fp->frame_size;
1117 }
1118 pt = 125 * (1 << fp->datainterval);
1119 ptmin = min(ptmin, pt);
1120 }
Oliver Neukum88a85162011-03-11 14:51:12 +01001121 err = snd_usb_autoresume(subs->stream->chip);
1122 if (err < 0)
1123 return err;
Daniel Macke5779992010-03-04 19:46:13 +01001124
1125 param_period_time_if_needed = SNDRV_PCM_HW_PARAM_PERIOD_TIME;
Takashi Iwai978520b2012-10-12 15:12:55 +02001126 if (subs->speed == USB_SPEED_FULL)
Daniel Macke5779992010-03-04 19:46:13 +01001127 /* full speed devices have fixed data packet interval */
1128 ptmin = 1000;
1129 if (ptmin == 1000)
1130 /* if period time doesn't go below 1 ms, no rules needed */
1131 param_period_time_if_needed = -1;
1132 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1133 ptmin, UINT_MAX);
1134
1135 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1136 hw_rule_rate, subs,
1137 SNDRV_PCM_HW_PARAM_FORMAT,
1138 SNDRV_PCM_HW_PARAM_CHANNELS,
1139 param_period_time_if_needed,
1140 -1)) < 0)
Oliver Neukum88a85162011-03-11 14:51:12 +01001141 goto rep_err;
Daniel Macke5779992010-03-04 19:46:13 +01001142 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
1143 hw_rule_channels, subs,
1144 SNDRV_PCM_HW_PARAM_FORMAT,
1145 SNDRV_PCM_HW_PARAM_RATE,
1146 param_period_time_if_needed,
1147 -1)) < 0)
Oliver Neukum88a85162011-03-11 14:51:12 +01001148 goto rep_err;
Daniel Macke5779992010-03-04 19:46:13 +01001149 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
1150 hw_rule_format, subs,
1151 SNDRV_PCM_HW_PARAM_RATE,
1152 SNDRV_PCM_HW_PARAM_CHANNELS,
1153 param_period_time_if_needed,
1154 -1)) < 0)
Oliver Neukum88a85162011-03-11 14:51:12 +01001155 goto rep_err;
Daniel Macke5779992010-03-04 19:46:13 +01001156 if (param_period_time_if_needed >= 0) {
1157 err = snd_pcm_hw_rule_add(runtime, 0,
1158 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1159 hw_rule_period_time, subs,
1160 SNDRV_PCM_HW_PARAM_FORMAT,
1161 SNDRV_PCM_HW_PARAM_CHANNELS,
1162 SNDRV_PCM_HW_PARAM_RATE,
1163 -1);
1164 if (err < 0)
Oliver Neukum88a85162011-03-11 14:51:12 +01001165 goto rep_err;
Daniel Macke5779992010-03-04 19:46:13 +01001166 }
1167 if ((err = snd_usb_pcm_check_knot(runtime, subs)) < 0)
Oliver Neukum88a85162011-03-11 14:51:12 +01001168 goto rep_err;
Daniel Macke5779992010-03-04 19:46:13 +01001169 return 0;
Oliver Neukum88a85162011-03-11 14:51:12 +01001170
1171rep_err:
1172 snd_usb_autosuspend(subs->stream->chip);
1173 return err;
Daniel Macke5779992010-03-04 19:46:13 +01001174}
1175
1176static int snd_usb_pcm_open(struct snd_pcm_substream *substream, int direction)
1177{
1178 struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
1179 struct snd_pcm_runtime *runtime = substream->runtime;
1180 struct snd_usb_substream *subs = &as->substream[direction];
1181
1182 subs->interface = -1;
Clemens Ladische11b4e02010-03-04 19:46:14 +01001183 subs->altset_idx = 0;
Daniel Macke5779992010-03-04 19:46:13 +01001184 runtime->hw = snd_usb_hardware;
1185 runtime->private_data = subs;
1186 subs->pcm_substream = substream;
Oliver Neukum88a85162011-03-11 14:51:12 +01001187 /* runtime PM is also done there */
Daniel Mackd24f5062013-04-17 00:01:38 +08001188
1189 /* initialize DSD/DOP context */
1190 subs->dsd_dop.byte_idx = 0;
1191 subs->dsd_dop.channel = 0;
1192 subs->dsd_dop.marker = 1;
1193
Daniel Macke5779992010-03-04 19:46:13 +01001194 return setup_hw_info(runtime, subs);
1195}
1196
1197static int snd_usb_pcm_close(struct snd_pcm_substream *substream, int direction)
1198{
1199 struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
1200 struct snd_usb_substream *subs = &as->substream[direction];
1201
Takashi Iwaib0db6062012-11-21 08:35:42 +01001202 stop_endpoints(subs, true);
Daniel Mack68e67f42012-07-12 13:08:40 +02001203
1204 if (!as->chip->shutdown && subs->interface >= 0) {
1205 usb_set_interface(subs->dev, subs->interface, 0);
1206 subs->interface = -1;
1207 }
1208
Daniel Macke5779992010-03-04 19:46:13 +01001209 subs->pcm_substream = NULL;
Oliver Neukum88a85162011-03-11 14:51:12 +01001210 snd_usb_autosuspend(subs->stream->chip);
Daniel Mackedcd3632012-04-12 13:51:12 +02001211
Daniel Mack68e67f42012-07-12 13:08:40 +02001212 return 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001213}
1214
1215/* Since a URB can handle only a single linear buffer, we must use double
1216 * buffering when the data to be transferred overflows the buffer boundary.
1217 * To avoid inconsistencies when updating hwptr_done, we use double buffering
1218 * for all URBs.
1219 */
1220static void retire_capture_urb(struct snd_usb_substream *subs,
1221 struct urb *urb)
1222{
1223 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1224 unsigned int stride, frames, bytes, oldptr;
1225 int i, period_elapsed = 0;
1226 unsigned long flags;
1227 unsigned char *cp;
Pierre-Louis Bossarte4cc6152012-12-19 11:39:05 -06001228 int current_frame_number;
1229
1230 /* read frame number here, update pointer in critical section */
1231 current_frame_number = usb_get_current_frame_number(subs->dev);
Daniel Mackedcd3632012-04-12 13:51:12 +02001232
1233 stride = runtime->frame_bits >> 3;
1234
1235 for (i = 0; i < urb->number_of_packets; i++) {
Calvin Owens1539d4f2013-04-12 22:33:59 -05001236 cp = (unsigned char *)urb->transfer_buffer + urb->iso_frame_desc[i].offset + subs->pkt_offset_adj;
Daniel Mackedcd3632012-04-12 13:51:12 +02001237 if (urb->iso_frame_desc[i].status && printk_ratelimit()) {
1238 snd_printdd(KERN_ERR "frame %d active: %d\n", i, urb->iso_frame_desc[i].status);
1239 // continue;
1240 }
1241 bytes = urb->iso_frame_desc[i].actual_length;
1242 frames = bytes / stride;
1243 if (!subs->txfr_quirk)
1244 bytes = frames * stride;
1245 if (bytes % (runtime->sample_bits >> 3) != 0) {
Daniel Mackedcd3632012-04-12 13:51:12 +02001246 int oldbytes = bytes;
Daniel Mackedcd3632012-04-12 13:51:12 +02001247 bytes = frames * stride;
1248 snd_printdd(KERN_ERR "Corrected urb data len. %d->%d\n",
1249 oldbytes, bytes);
1250 }
1251 /* update the current pointer */
1252 spin_lock_irqsave(&subs->lock, flags);
1253 oldptr = subs->hwptr_done;
1254 subs->hwptr_done += bytes;
1255 if (subs->hwptr_done >= runtime->buffer_size * stride)
1256 subs->hwptr_done -= runtime->buffer_size * stride;
1257 frames = (bytes + (oldptr % stride)) / stride;
1258 subs->transfer_done += frames;
1259 if (subs->transfer_done >= runtime->period_size) {
1260 subs->transfer_done -= runtime->period_size;
1261 period_elapsed = 1;
1262 }
Pierre-Louis Bossarte4cc6152012-12-19 11:39:05 -06001263 /* capture delay is by construction limited to one URB,
1264 * reset delays here
1265 */
1266 runtime->delay = subs->last_delay = 0;
1267
1268 /* realign last_frame_number */
1269 subs->last_frame_number = current_frame_number;
1270 subs->last_frame_number &= 0xFF; /* keep 8 LSBs */
1271
Daniel Mackedcd3632012-04-12 13:51:12 +02001272 spin_unlock_irqrestore(&subs->lock, flags);
1273 /* copy a data chunk */
1274 if (oldptr + bytes > runtime->buffer_size * stride) {
1275 unsigned int bytes1 =
1276 runtime->buffer_size * stride - oldptr;
1277 memcpy(runtime->dma_area + oldptr, cp, bytes1);
1278 memcpy(runtime->dma_area, cp + bytes1, bytes - bytes1);
1279 } else {
1280 memcpy(runtime->dma_area + oldptr, cp, bytes);
1281 }
1282 }
1283
1284 if (period_elapsed)
1285 snd_pcm_period_elapsed(subs->pcm_substream);
1286}
1287
Daniel Mackd24f5062013-04-17 00:01:38 +08001288static inline void fill_playback_urb_dsd_dop(struct snd_usb_substream *subs,
1289 struct urb *urb, unsigned int bytes)
1290{
1291 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1292 unsigned int stride = runtime->frame_bits >> 3;
1293 unsigned int dst_idx = 0;
1294 unsigned int src_idx = subs->hwptr_done;
1295 unsigned int wrap = runtime->buffer_size * stride;
1296 u8 *dst = urb->transfer_buffer;
1297 u8 *src = runtime->dma_area;
1298 u8 marker[] = { 0x05, 0xfa };
1299
1300 /*
1301 * The DSP DOP format defines a way to transport DSD samples over
1302 * normal PCM data endpoints. It requires stuffing of marker bytes
1303 * (0x05 and 0xfa, alternating per sample frame), and then expects
1304 * 2 additional bytes of actual payload. The whole frame is stored
1305 * LSB.
1306 *
1307 * Hence, for a stereo transport, the buffer layout looks like this,
1308 * where L refers to left channel samples and R to right.
1309 *
1310 * L1 L2 0x05 R1 R2 0x05 L3 L4 0xfa R3 R4 0xfa
1311 * L5 L6 0x05 R5 R6 0x05 L7 L8 0xfa R7 R8 0xfa
1312 * .....
1313 *
1314 */
1315
1316 while (bytes--) {
1317 if (++subs->dsd_dop.byte_idx == 3) {
1318 /* frame boundary? */
1319 dst[dst_idx++] = marker[subs->dsd_dop.marker];
1320 src_idx += 2;
1321 subs->dsd_dop.byte_idx = 0;
1322
1323 if (++subs->dsd_dop.channel % runtime->channels == 0) {
1324 /* alternate the marker */
1325 subs->dsd_dop.marker++;
1326 subs->dsd_dop.marker %= ARRAY_SIZE(marker);
1327 subs->dsd_dop.channel = 0;
1328 }
1329 } else {
1330 /* stuff the DSD payload */
1331 int idx = (src_idx + subs->dsd_dop.byte_idx - 1) % wrap;
Daniel Mack44dcbbb2013-04-17 00:01:39 +08001332
1333 if (subs->cur_audiofmt->dsd_bitrev)
1334 dst[dst_idx++] = bitrev8(src[idx]);
1335 else
1336 dst[dst_idx++] = src[idx];
1337
Daniel Mackd24f5062013-04-17 00:01:38 +08001338 subs->hwptr_done++;
1339 }
1340 }
1341}
1342
Daniel Mackedcd3632012-04-12 13:51:12 +02001343static void prepare_playback_urb(struct snd_usb_substream *subs,
1344 struct urb *urb)
1345{
1346 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
Daniel Mack245baf92012-08-30 18:52:30 +02001347 struct snd_usb_endpoint *ep = subs->data_endpoint;
Daniel Mackedcd3632012-04-12 13:51:12 +02001348 struct snd_urb_ctx *ctx = urb->context;
1349 unsigned int counts, frames, bytes;
1350 int i, stride, period_elapsed = 0;
1351 unsigned long flags;
1352
1353 stride = runtime->frame_bits >> 3;
1354
1355 frames = 0;
1356 urb->number_of_packets = 0;
1357 spin_lock_irqsave(&subs->lock, flags);
Alan Stern976b6c02013-09-24 15:51:58 -04001358 subs->frame_limit += ep->max_urb_frames;
Daniel Mackedcd3632012-04-12 13:51:12 +02001359 for (i = 0; i < ctx->packets; i++) {
Daniel Mack245baf92012-08-30 18:52:30 +02001360 if (ctx->packet_size[i])
1361 counts = ctx->packet_size[i];
1362 else
1363 counts = snd_usb_endpoint_next_packet_size(ep);
1364
Daniel Mackedcd3632012-04-12 13:51:12 +02001365 /* set up descriptor */
Daniel Mack8a2a74d2013-04-17 00:01:37 +08001366 urb->iso_frame_desc[i].offset = frames * ep->stride;
1367 urb->iso_frame_desc[i].length = counts * ep->stride;
Daniel Mackedcd3632012-04-12 13:51:12 +02001368 frames += counts;
1369 urb->number_of_packets++;
1370 subs->transfer_done += counts;
1371 if (subs->transfer_done >= runtime->period_size) {
1372 subs->transfer_done -= runtime->period_size;
Alan Stern976b6c02013-09-24 15:51:58 -04001373 subs->frame_limit = 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001374 period_elapsed = 1;
1375 if (subs->fmt_type == UAC_FORMAT_TYPE_II) {
1376 if (subs->transfer_done > 0) {
1377 /* FIXME: fill-max mode is not
1378 * supported yet */
1379 frames -= subs->transfer_done;
1380 counts -= subs->transfer_done;
1381 urb->iso_frame_desc[i].length =
Daniel Mack8a2a74d2013-04-17 00:01:37 +08001382 counts * ep->stride;
Daniel Mackedcd3632012-04-12 13:51:12 +02001383 subs->transfer_done = 0;
1384 }
1385 i++;
1386 if (i < ctx->packets) {
1387 /* add a transfer delimiter */
1388 urb->iso_frame_desc[i].offset =
Daniel Mack8a2a74d2013-04-17 00:01:37 +08001389 frames * ep->stride;
Daniel Mackedcd3632012-04-12 13:51:12 +02001390 urb->iso_frame_desc[i].length = 0;
1391 urb->number_of_packets++;
1392 }
1393 break;
1394 }
1395 }
Alan Stern976b6c02013-09-24 15:51:58 -04001396 /* finish at the period boundary or after enough frames */
1397 if ((period_elapsed ||
1398 subs->transfer_done >= subs->frame_limit) &&
1399 !snd_usb_endpoint_implicit_feedback_sink(ep))
Daniel Mackedcd3632012-04-12 13:51:12 +02001400 break;
1401 }
Daniel Mack8a2a74d2013-04-17 00:01:37 +08001402 bytes = frames * ep->stride;
Daniel Mackd24f5062013-04-17 00:01:38 +08001403
1404 if (unlikely(subs->pcm_format == SNDRV_PCM_FORMAT_DSD_U16_LE &&
1405 subs->cur_audiofmt->dsd_dop)) {
1406 fill_playback_urb_dsd_dop(subs, urb, bytes);
Daniel Mack44dcbbb2013-04-17 00:01:39 +08001407 } else if (unlikely(subs->pcm_format == SNDRV_PCM_FORMAT_DSD_U8 &&
1408 subs->cur_audiofmt->dsd_bitrev)) {
1409 /* bit-reverse the bytes */
1410 u8 *buf = urb->transfer_buffer;
1411 for (i = 0; i < bytes; i++) {
1412 int idx = (subs->hwptr_done + i)
1413 % (runtime->buffer_size * stride);
1414 buf[i] = bitrev8(runtime->dma_area[idx]);
1415 }
1416
1417 subs->hwptr_done += bytes;
Daniel Mackedcd3632012-04-12 13:51:12 +02001418 } else {
Daniel Mackd24f5062013-04-17 00:01:38 +08001419 /* usual PCM */
1420 if (subs->hwptr_done + bytes > runtime->buffer_size * stride) {
1421 /* err, the transferred area goes over buffer boundary. */
1422 unsigned int bytes1 =
1423 runtime->buffer_size * stride - subs->hwptr_done;
1424 memcpy(urb->transfer_buffer,
1425 runtime->dma_area + subs->hwptr_done, bytes1);
1426 memcpy(urb->transfer_buffer + bytes1,
1427 runtime->dma_area, bytes - bytes1);
1428 } else {
1429 memcpy(urb->transfer_buffer,
1430 runtime->dma_area + subs->hwptr_done, bytes);
1431 }
1432
1433 subs->hwptr_done += bytes;
Daniel Mackedcd3632012-04-12 13:51:12 +02001434 }
Daniel Mackd24f5062013-04-17 00:01:38 +08001435
Daniel Mackedcd3632012-04-12 13:51:12 +02001436 if (subs->hwptr_done >= runtime->buffer_size * stride)
1437 subs->hwptr_done -= runtime->buffer_size * stride;
Daniel Mackfbcfbf52012-08-30 18:52:29 +02001438
1439 /* update delay with exact number of samples queued */
1440 runtime->delay = subs->last_delay;
Daniel Mackedcd3632012-04-12 13:51:12 +02001441 runtime->delay += frames;
Daniel Mackfbcfbf52012-08-30 18:52:29 +02001442 subs->last_delay = runtime->delay;
1443
1444 /* realign last_frame_number */
1445 subs->last_frame_number = usb_get_current_frame_number(subs->dev);
1446 subs->last_frame_number &= 0xFF; /* keep 8 LSBs */
1447
Daniel Mackedcd3632012-04-12 13:51:12 +02001448 spin_unlock_irqrestore(&subs->lock, flags);
1449 urb->transfer_buffer_length = bytes;
1450 if (period_elapsed)
1451 snd_pcm_period_elapsed(subs->pcm_substream);
1452}
1453
1454/*
1455 * process after playback data complete
1456 * - decrease the delay count again
1457 */
1458static void retire_playback_urb(struct snd_usb_substream *subs,
1459 struct urb *urb)
1460{
1461 unsigned long flags;
1462 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
Daniel Mack8a2a74d2013-04-17 00:01:37 +08001463 struct snd_usb_endpoint *ep = subs->data_endpoint;
1464 int processed = urb->transfer_buffer_length / ep->stride;
Daniel Mackfbcfbf52012-08-30 18:52:29 +02001465 int est_delay;
Daniel Mackedcd3632012-04-12 13:51:12 +02001466
Takashi Iwai1213a202012-09-06 14:58:00 +02001467 /* ignore the delay accounting when procssed=0 is given, i.e.
1468 * silent payloads are procssed before handling the actual data
1469 */
1470 if (!processed)
1471 return;
1472
Daniel Mackedcd3632012-04-12 13:51:12 +02001473 spin_lock_irqsave(&subs->lock, flags);
Takashi Iwai48779a02012-11-23 16:00:37 +01001474 if (!subs->last_delay)
1475 goto out; /* short path */
1476
Daniel Mackfbcfbf52012-08-30 18:52:29 +02001477 est_delay = snd_usb_pcm_delay(subs, runtime->rate);
1478 /* update delay with exact number of samples played */
1479 if (processed > subs->last_delay)
1480 subs->last_delay = 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001481 else
Daniel Mackfbcfbf52012-08-30 18:52:29 +02001482 subs->last_delay -= processed;
1483 runtime->delay = subs->last_delay;
1484
1485 /*
1486 * Report when delay estimate is off by more than 2ms.
1487 * The error should be lower than 2ms since the estimate relies
1488 * on two reads of a counter updated every ms.
1489 */
1490 if (abs(est_delay - subs->last_delay) * 1000 > runtime->rate * 2)
1491 snd_printk(KERN_DEBUG "delay: estimated %d, actual %d\n",
1492 est_delay, subs->last_delay);
1493
Takashi Iwai48779a02012-11-23 16:00:37 +01001494 if (!subs->running) {
1495 /* update last_frame_number for delay counting here since
1496 * prepare_playback_urb won't be called during pause
1497 */
1498 subs->last_frame_number =
1499 usb_get_current_frame_number(subs->dev) & 0xff;
1500 }
1501
1502 out:
Daniel Mackedcd3632012-04-12 13:51:12 +02001503 spin_unlock_irqrestore(&subs->lock, flags);
Daniel Macke5779992010-03-04 19:46:13 +01001504}
1505
1506static int snd_usb_playback_open(struct snd_pcm_substream *substream)
1507{
1508 return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_PLAYBACK);
1509}
1510
1511static int snd_usb_playback_close(struct snd_pcm_substream *substream)
1512{
1513 return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_PLAYBACK);
1514}
1515
1516static int snd_usb_capture_open(struct snd_pcm_substream *substream)
1517{
1518 return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_CAPTURE);
1519}
1520
1521static int snd_usb_capture_close(struct snd_pcm_substream *substream)
1522{
1523 return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_CAPTURE);
1524}
1525
Daniel Mackedcd3632012-04-12 13:51:12 +02001526static int snd_usb_substream_playback_trigger(struct snd_pcm_substream *substream,
1527 int cmd)
1528{
1529 struct snd_usb_substream *subs = substream->runtime->private_data;
1530
1531 switch (cmd) {
1532 case SNDRV_PCM_TRIGGER_START:
1533 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1534 subs->data_endpoint->prepare_data_urb = prepare_playback_urb;
1535 subs->data_endpoint->retire_data_urb = retire_playback_urb;
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001536 subs->running = 1;
Daniel Mackedcd3632012-04-12 13:51:12 +02001537 return 0;
1538 case SNDRV_PCM_TRIGGER_STOP:
Takashi Iwaia9bb3622012-11-20 18:32:06 +01001539 stop_endpoints(subs, false);
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001540 subs->running = 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001541 return 0;
1542 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1543 subs->data_endpoint->prepare_data_urb = NULL;
Takashi Iwai48779a02012-11-23 16:00:37 +01001544 /* keep retire_data_urb for delay calculation */
1545 subs->data_endpoint->retire_data_urb = retire_playback_urb;
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001546 subs->running = 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001547 return 0;
1548 }
1549
1550 return -EINVAL;
1551}
1552
Daniel Mackafe25962012-06-16 16:58:04 +02001553static int snd_usb_substream_capture_trigger(struct snd_pcm_substream *substream,
1554 int cmd)
Daniel Mackedcd3632012-04-12 13:51:12 +02001555{
1556 int err;
1557 struct snd_usb_substream *subs = substream->runtime->private_data;
1558
1559 switch (cmd) {
1560 case SNDRV_PCM_TRIGGER_START:
Takashi Iwaia9bb3622012-11-20 18:32:06 +01001561 err = start_endpoints(subs, false);
Daniel Mackedcd3632012-04-12 13:51:12 +02001562 if (err < 0)
1563 return err;
1564
1565 subs->data_endpoint->retire_data_urb = retire_capture_urb;
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001566 subs->running = 1;
Daniel Mackedcd3632012-04-12 13:51:12 +02001567 return 0;
1568 case SNDRV_PCM_TRIGGER_STOP:
Takashi Iwaia9bb3622012-11-20 18:32:06 +01001569 stop_endpoints(subs, false);
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001570 subs->running = 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001571 return 0;
1572 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1573 subs->data_endpoint->retire_data_urb = NULL;
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001574 subs->running = 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001575 return 0;
1576 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1577 subs->data_endpoint->retire_data_urb = retire_capture_urb;
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001578 subs->running = 1;
Daniel Mackedcd3632012-04-12 13:51:12 +02001579 return 0;
1580 }
1581
1582 return -EINVAL;
1583}
1584
Daniel Macke5779992010-03-04 19:46:13 +01001585static struct snd_pcm_ops snd_usb_playback_ops = {
1586 .open = snd_usb_playback_open,
1587 .close = snd_usb_playback_close,
1588 .ioctl = snd_pcm_lib_ioctl,
1589 .hw_params = snd_usb_hw_params,
1590 .hw_free = snd_usb_hw_free,
1591 .prepare = snd_usb_pcm_prepare,
1592 .trigger = snd_usb_substream_playback_trigger,
1593 .pointer = snd_usb_pcm_pointer,
1594 .page = snd_pcm_lib_get_vmalloc_page,
1595 .mmap = snd_pcm_lib_mmap_vmalloc,
1596};
1597
1598static struct snd_pcm_ops snd_usb_capture_ops = {
1599 .open = snd_usb_capture_open,
1600 .close = snd_usb_capture_close,
1601 .ioctl = snd_pcm_lib_ioctl,
1602 .hw_params = snd_usb_hw_params,
1603 .hw_free = snd_usb_hw_free,
1604 .prepare = snd_usb_pcm_prepare,
1605 .trigger = snd_usb_substream_capture_trigger,
1606 .pointer = snd_usb_pcm_pointer,
1607 .page = snd_pcm_lib_get_vmalloc_page,
1608 .mmap = snd_pcm_lib_mmap_vmalloc,
1609};
1610
1611void snd_usb_set_pcm_ops(struct snd_pcm *pcm, int stream)
1612{
1613 snd_pcm_set_ops(pcm, stream,
1614 stream == SNDRV_PCM_STREAM_PLAYBACK ?
1615 &snd_usb_playback_ops : &snd_usb_capture_ops);
1616}