blob: d90604aa5137803d0d1544707a26d570ebeb966c [file] [log] [blame]
Daniel Macke5779992010-03-04 19:46:13 +01001/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15 */
16
17#include <linux/init.h>
Stephen Rothwell9966dda2010-03-29 19:01:48 +110018#include <linux/slab.h>
Daniel Mackedcd3632012-04-12 13:51:12 +020019#include <linux/ratelimit.h>
Daniel Macke5779992010-03-04 19:46:13 +010020#include <linux/usb.h>
21#include <linux/usb/audio.h>
Daniel Mack7e847892010-03-11 21:13:20 +010022#include <linux/usb/audio-v2.h>
Daniel Macke5779992010-03-04 19:46:13 +010023
24#include <sound/core.h>
25#include <sound/pcm.h>
26#include <sound/pcm_params.h>
27
28#include "usbaudio.h"
29#include "card.h"
30#include "quirks.h"
31#include "debug.h"
Daniel Mackc731bc92011-09-14 12:46:57 +020032#include "endpoint.h"
Daniel Macke5779992010-03-04 19:46:13 +010033#include "helper.h"
34#include "pcm.h"
Daniel Mack79f920f2010-05-31 14:51:31 +020035#include "clock.h"
Oliver Neukum88a85162011-03-11 14:51:12 +010036#include "power.h"
Daniel Macke5779992010-03-04 19:46:13 +010037
Daniel Mackedcd3632012-04-12 13:51:12 +020038#define SUBSTREAM_FLAG_DATA_EP_STARTED 0
39#define SUBSTREAM_FLAG_SYNC_EP_STARTED 1
40
Pierre-Louis Bossart294c4fb2011-09-06 19:15:34 -050041/* return the estimated delay based on USB frame counters */
42snd_pcm_uframes_t snd_usb_pcm_delay(struct snd_usb_substream *subs,
43 unsigned int rate)
44{
45 int current_frame_number;
46 int frame_diff;
47 int est_delay;
48
49 current_frame_number = usb_get_current_frame_number(subs->dev);
50 /*
51 * HCD implementations use different widths, use lower 8 bits.
52 * The delay will be managed up to 256ms, which is more than
53 * enough
54 */
55 frame_diff = (current_frame_number - subs->last_frame_number) & 0xff;
56
57 /* Approximation based on number of samples per USB frame (ms),
58 some truncation for 44.1 but the estimate is good enough */
59 est_delay = subs->last_delay - (frame_diff * rate / 1000);
60 if (est_delay < 0)
61 est_delay = 0;
62 return est_delay;
63}
64
Daniel Macke5779992010-03-04 19:46:13 +010065/*
66 * return the current pcm pointer. just based on the hwptr_done value.
67 */
68static snd_pcm_uframes_t snd_usb_pcm_pointer(struct snd_pcm_substream *substream)
69{
70 struct snd_usb_substream *subs;
71 unsigned int hwptr_done;
72
73 subs = (struct snd_usb_substream *)substream->runtime->private_data;
Takashi Iwai978520b2012-10-12 15:12:55 +020074 if (subs->stream->chip->shutdown)
75 return SNDRV_PCM_POS_XRUN;
Daniel Macke5779992010-03-04 19:46:13 +010076 spin_lock(&subs->lock);
77 hwptr_done = subs->hwptr_done;
Pierre-Louis Bossart294c4fb2011-09-06 19:15:34 -050078 substream->runtime->delay = snd_usb_pcm_delay(subs,
79 substream->runtime->rate);
Daniel Macke5779992010-03-04 19:46:13 +010080 spin_unlock(&subs->lock);
81 return hwptr_done / (substream->runtime->frame_bits >> 3);
82}
83
84/*
85 * find a matching audio format
86 */
Dylan Reid61a70952012-09-18 09:49:48 -070087static struct audioformat *find_format(struct snd_usb_substream *subs)
Daniel Macke5779992010-03-04 19:46:13 +010088{
89 struct list_head *p;
90 struct audioformat *found = NULL;
91 int cur_attr = 0, attr;
92
93 list_for_each(p, &subs->fmt_list) {
94 struct audioformat *fp;
95 fp = list_entry(p, struct audioformat, list);
Dylan Reid61a70952012-09-18 09:49:48 -070096 if (!(fp->formats & (1uLL << subs->pcm_format)))
Clemens Ladisch015eb0b2010-03-04 19:46:15 +010097 continue;
Dylan Reid61a70952012-09-18 09:49:48 -070098 if (fp->channels != subs->channels)
Daniel Macke5779992010-03-04 19:46:13 +010099 continue;
Dylan Reid61a70952012-09-18 09:49:48 -0700100 if (subs->cur_rate < fp->rate_min ||
101 subs->cur_rate > fp->rate_max)
Daniel Macke5779992010-03-04 19:46:13 +0100102 continue;
103 if (! (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)) {
104 unsigned int i;
105 for (i = 0; i < fp->nr_rates; i++)
Dylan Reid61a70952012-09-18 09:49:48 -0700106 if (fp->rate_table[i] == subs->cur_rate)
Daniel Macke5779992010-03-04 19:46:13 +0100107 break;
108 if (i >= fp->nr_rates)
109 continue;
110 }
111 attr = fp->ep_attr & USB_ENDPOINT_SYNCTYPE;
112 if (! found) {
113 found = fp;
114 cur_attr = attr;
115 continue;
116 }
117 /* avoid async out and adaptive in if the other method
118 * supports the same format.
119 * this is a workaround for the case like
120 * M-audio audiophile USB.
121 */
122 if (attr != cur_attr) {
123 if ((attr == USB_ENDPOINT_SYNC_ASYNC &&
124 subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
125 (attr == USB_ENDPOINT_SYNC_ADAPTIVE &&
126 subs->direction == SNDRV_PCM_STREAM_CAPTURE))
127 continue;
128 if ((cur_attr == USB_ENDPOINT_SYNC_ASYNC &&
129 subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
130 (cur_attr == USB_ENDPOINT_SYNC_ADAPTIVE &&
131 subs->direction == SNDRV_PCM_STREAM_CAPTURE)) {
132 found = fp;
133 cur_attr = attr;
134 continue;
135 }
136 }
137 /* find the format with the largest max. packet size */
138 if (fp->maxpacksize > found->maxpacksize) {
139 found = fp;
140 cur_attr = attr;
141 }
142 }
143 return found;
144}
145
Daniel Mack767d75a2010-03-04 19:46:17 +0100146static int init_pitch_v1(struct snd_usb_audio *chip, int iface,
147 struct usb_host_interface *alts,
148 struct audioformat *fmt)
Daniel Macke5779992010-03-04 19:46:13 +0100149{
Daniel Mack767d75a2010-03-04 19:46:17 +0100150 struct usb_device *dev = chip->dev;
Daniel Macke5779992010-03-04 19:46:13 +0100151 unsigned int ep;
152 unsigned char data[1];
153 int err;
154
155 ep = get_endpoint(alts, 0)->bEndpointAddress;
Daniel Mack767d75a2010-03-04 19:46:17 +0100156
Daniel Mack767d75a2010-03-04 19:46:17 +0100157 data[0] = 1;
158 if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC_SET_CUR,
159 USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT,
160 UAC_EP_CS_ATTR_PITCH_CONTROL << 8, ep,
Clemens Ladisch17d900c2011-09-26 21:15:27 +0200161 data, sizeof(data))) < 0) {
Daniel Mack767d75a2010-03-04 19:46:17 +0100162 snd_printk(KERN_ERR "%d:%d:%d: cannot set enable PITCH\n",
163 dev->devnum, iface, ep);
164 return err;
Daniel Macke5779992010-03-04 19:46:13 +0100165 }
Daniel Mack767d75a2010-03-04 19:46:17 +0100166
Daniel Macke5779992010-03-04 19:46:13 +0100167 return 0;
168}
169
Daniel Mack92c25612010-05-26 18:11:39 +0200170static int init_pitch_v2(struct snd_usb_audio *chip, int iface,
171 struct usb_host_interface *alts,
172 struct audioformat *fmt)
173{
174 struct usb_device *dev = chip->dev;
175 unsigned char data[1];
Daniel Mack92c25612010-05-26 18:11:39 +0200176 int err;
177
Daniel Mack92c25612010-05-26 18:11:39 +0200178 data[0] = 1;
179 if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC2_CS_CUR,
180 USB_TYPE_CLASS | USB_RECIP_ENDPOINT | USB_DIR_OUT,
181 UAC2_EP_CS_PITCH << 8, 0,
Clemens Ladisch17d900c2011-09-26 21:15:27 +0200182 data, sizeof(data))) < 0) {
Daniel Mack92c25612010-05-26 18:11:39 +0200183 snd_printk(KERN_ERR "%d:%d:%d: cannot set enable PITCH (v2)\n",
184 dev->devnum, iface, fmt->altsetting);
185 return err;
186 }
187
188 return 0;
189}
190
Daniel Mack767d75a2010-03-04 19:46:17 +0100191/*
Daniel Mack92c25612010-05-26 18:11:39 +0200192 * initialize the pitch control and sample rate
Daniel Mack767d75a2010-03-04 19:46:17 +0100193 */
194int snd_usb_init_pitch(struct snd_usb_audio *chip, int iface,
195 struct usb_host_interface *alts,
196 struct audioformat *fmt)
197{
198 struct usb_interface_descriptor *altsd = get_iface_desc(alts);
199
Daniel Mack92c25612010-05-26 18:11:39 +0200200 /* if endpoint doesn't have pitch control, bail out */
201 if (!(fmt->attributes & UAC_EP_CS_ATTR_PITCH_CONTROL))
202 return 0;
203
Daniel Mack767d75a2010-03-04 19:46:17 +0100204 switch (altsd->bInterfaceProtocol) {
205 case UAC_VERSION_1:
Clemens Ladischa2acad82010-09-03 10:53:11 +0200206 default:
Daniel Mack767d75a2010-03-04 19:46:17 +0100207 return init_pitch_v1(chip, iface, alts, fmt);
208
209 case UAC_VERSION_2:
Daniel Mack92c25612010-05-26 18:11:39 +0200210 return init_pitch_v2(chip, iface, alts, fmt);
Daniel Mack767d75a2010-03-04 19:46:17 +0100211 }
Daniel Mack767d75a2010-03-04 19:46:17 +0100212}
213
Takashi Iwaia9bb3622012-11-20 18:32:06 +0100214static int start_endpoints(struct snd_usb_substream *subs, bool can_sleep)
Daniel Mackedcd3632012-04-12 13:51:12 +0200215{
216 int err;
217
218 if (!subs->data_endpoint)
219 return -EINVAL;
220
221 if (!test_and_set_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags)) {
222 struct snd_usb_endpoint *ep = subs->data_endpoint;
223
224 snd_printdd(KERN_DEBUG "Starting data EP @%p\n", ep);
225
226 ep->data_subs = subs;
Daniel Mack015618b2012-08-29 13:17:05 +0200227 err = snd_usb_endpoint_start(ep, can_sleep);
Daniel Mackedcd3632012-04-12 13:51:12 +0200228 if (err < 0) {
229 clear_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags);
230 return err;
231 }
232 }
233
234 if (subs->sync_endpoint &&
235 !test_and_set_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags)) {
236 struct snd_usb_endpoint *ep = subs->sync_endpoint;
237
Daniel Mack2e4a2632012-08-30 18:52:31 +0200238 if (subs->data_endpoint->iface != subs->sync_endpoint->iface ||
239 subs->data_endpoint->alt_idx != subs->sync_endpoint->alt_idx) {
240 err = usb_set_interface(subs->dev,
241 subs->sync_endpoint->iface,
242 subs->sync_endpoint->alt_idx);
243 if (err < 0) {
244 snd_printk(KERN_ERR
245 "%d:%d:%d: cannot set interface (%d)\n",
246 subs->dev->devnum,
247 subs->sync_endpoint->iface,
248 subs->sync_endpoint->alt_idx, err);
249 return -EIO;
250 }
251 }
252
Daniel Mackedcd3632012-04-12 13:51:12 +0200253 snd_printdd(KERN_DEBUG "Starting sync EP @%p\n", ep);
254
255 ep->sync_slave = subs->data_endpoint;
Daniel Mack015618b2012-08-29 13:17:05 +0200256 err = snd_usb_endpoint_start(ep, can_sleep);
Daniel Mackedcd3632012-04-12 13:51:12 +0200257 if (err < 0) {
258 clear_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags);
259 return err;
260 }
261 }
262
263 return 0;
264}
265
Takashi Iwaia9bb3622012-11-20 18:32:06 +0100266static void stop_endpoints(struct snd_usb_substream *subs, bool wait)
Daniel Mackedcd3632012-04-12 13:51:12 +0200267{
268 if (test_and_clear_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags))
Takashi Iwaia9bb3622012-11-20 18:32:06 +0100269 snd_usb_endpoint_stop(subs->sync_endpoint, wait);
Daniel Mackedcd3632012-04-12 13:51:12 +0200270
271 if (test_and_clear_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags))
Takashi Iwaia9bb3622012-11-20 18:32:06 +0100272 snd_usb_endpoint_stop(subs->data_endpoint, wait);
Daniel Mackedcd3632012-04-12 13:51:12 +0200273}
274
Daniel Mackedcd3632012-04-12 13:51:12 +0200275static int deactivate_endpoints(struct snd_usb_substream *subs)
276{
277 int reta, retb;
278
279 reta = snd_usb_endpoint_deactivate(subs->sync_endpoint);
280 retb = snd_usb_endpoint_deactivate(subs->data_endpoint);
281
282 if (reta < 0)
283 return reta;
284
285 if (retb < 0)
286 return retb;
287
288 return 0;
289}
290
Daniel Macke5779992010-03-04 19:46:13 +0100291/*
292 * find a matching format and set up the interface
293 */
294static int set_format(struct snd_usb_substream *subs, struct audioformat *fmt)
295{
296 struct usb_device *dev = subs->dev;
297 struct usb_host_interface *alts;
298 struct usb_interface_descriptor *altsd;
299 struct usb_interface *iface;
300 unsigned int ep, attr;
301 int is_playback = subs->direction == SNDRV_PCM_STREAM_PLAYBACK;
Daniel Mackc75a8a72012-04-12 13:51:14 +0200302 int err, implicit_fb = 0;
Daniel Macke5779992010-03-04 19:46:13 +0100303
304 iface = usb_ifnum_to_if(dev, fmt->iface);
305 if (WARN_ON(!iface))
306 return -EINVAL;
307 alts = &iface->altsetting[fmt->altset_idx];
308 altsd = get_iface_desc(alts);
309 if (WARN_ON(altsd->bAlternateSetting != fmt->altsetting))
310 return -EINVAL;
311
312 if (fmt == subs->cur_audiofmt)
313 return 0;
314
Daniel Mack68e67f42012-07-12 13:08:40 +0200315 /* close the old interface */
316 if (subs->interface >= 0 && subs->interface != fmt->iface) {
317 err = usb_set_interface(subs->dev, subs->interface, 0);
318 if (err < 0) {
319 snd_printk(KERN_ERR "%d:%d:%d: return to setting 0 failed (%d)\n",
320 dev->devnum, fmt->iface, fmt->altsetting, err);
321 return -EIO;
322 }
323 subs->interface = -1;
324 subs->altset_idx = 0;
325 }
326
327 /* set interface */
328 if (subs->interface != fmt->iface ||
329 subs->altset_idx != fmt->altset_idx) {
330 err = usb_set_interface(dev, fmt->iface, fmt->altsetting);
331 if (err < 0) {
332 snd_printk(KERN_ERR "%d:%d:%d: usb_set_interface failed (%d)\n",
333 dev->devnum, fmt->iface, fmt->altsetting, err);
334 return -EIO;
335 }
336 snd_printdd(KERN_INFO "setting usb interface %d:%d\n",
337 fmt->iface, fmt->altsetting);
338 subs->interface = fmt->iface;
339 subs->altset_idx = fmt->altset_idx;
340 }
341
Daniel Mackedcd3632012-04-12 13:51:12 +0200342 subs->data_endpoint = snd_usb_add_endpoint(subs->stream->chip,
343 alts, fmt->endpoint, subs->direction,
344 SND_USB_ENDPOINT_TYPE_DATA);
345 if (!subs->data_endpoint)
346 return -EINVAL;
Daniel Macke5779992010-03-04 19:46:13 +0100347
348 /* we need a sync pipe in async OUT or adaptive IN mode */
349 /* check the number of EP, since some devices have broken
350 * descriptors which fool us. if it has only one EP,
351 * assume it as adaptive-out or sync-in.
352 */
353 attr = fmt->ep_attr & USB_ENDPOINT_SYNCTYPE;
Daniel Mackc75a8a72012-04-12 13:51:14 +0200354
355 switch (subs->stream->chip->usb_id) {
356 case USB_ID(0x0763, 0x2080): /* M-Audio FastTrack Ultra */
357 case USB_ID(0x0763, 0x2081):
358 if (is_playback) {
359 implicit_fb = 1;
Daniel Mackedcd3632012-04-12 13:51:12 +0200360 ep = 0x81;
361 iface = usb_ifnum_to_if(dev, 2);
Daniel Mackc75a8a72012-04-12 13:51:14 +0200362
363 if (!iface || iface->num_altsetting == 0)
364 return -EINVAL;
365
Daniel Mackedcd3632012-04-12 13:51:12 +0200366 alts = &iface->altsetting[1];
367 goto add_sync_ep;
368 }
Daniel Mackc75a8a72012-04-12 13:51:14 +0200369 }
Daniel Mackedcd3632012-04-12 13:51:12 +0200370
Daniel Mackc75a8a72012-04-12 13:51:14 +0200371 if (((is_playback && attr == USB_ENDPOINT_SYNC_ASYNC) ||
372 (!is_playback && attr == USB_ENDPOINT_SYNC_ADAPTIVE)) &&
373 altsd->bNumEndpoints >= 2) {
Daniel Macke5779992010-03-04 19:46:13 +0100374 /* check sync-pipe endpoint */
375 /* ... and check descriptor size before accessing bSynchAddress
376 because there is a version of the SB Audigy 2 NX firmware lacking
377 the audio fields in the endpoint descriptors */
378 if ((get_endpoint(alts, 1)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != 0x01 ||
379 (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
Daniel Mackc75a8a72012-04-12 13:51:14 +0200380 get_endpoint(alts, 1)->bSynchAddress != 0 &&
381 !implicit_fb)) {
Daniel Mack7fb75db2012-06-17 13:44:27 +0200382 snd_printk(KERN_ERR "%d:%d:%d : invalid sync pipe. bmAttributes %02x, bLength %d, bSynchAddress %02x\n",
383 dev->devnum, fmt->iface, fmt->altsetting,
384 get_endpoint(alts, 1)->bmAttributes,
385 get_endpoint(alts, 1)->bLength,
386 get_endpoint(alts, 1)->bSynchAddress);
Daniel Macke5779992010-03-04 19:46:13 +0100387 return -EINVAL;
388 }
389 ep = get_endpoint(alts, 1)->bEndpointAddress;
Daniel Mack7fb75db2012-06-17 13:44:27 +0200390 if (!implicit_fb &&
391 get_endpoint(alts, 0)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
Daniel Macke5779992010-03-04 19:46:13 +0100392 (( is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress | USB_DIR_IN)) ||
Daniel Mack7fb75db2012-06-17 13:44:27 +0200393 (!is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress & ~USB_DIR_IN)))) {
394 snd_printk(KERN_ERR "%d:%d:%d : invalid sync pipe. is_playback %d, ep %02x, bSynchAddress %02x\n",
395 dev->devnum, fmt->iface, fmt->altsetting,
396 is_playback, ep, get_endpoint(alts, 0)->bSynchAddress);
Daniel Macke5779992010-03-04 19:46:13 +0100397 return -EINVAL;
398 }
Daniel Mackc75a8a72012-04-12 13:51:14 +0200399
400 implicit_fb = (get_endpoint(alts, 1)->bmAttributes & USB_ENDPOINT_USAGE_MASK)
401 == USB_ENDPOINT_USAGE_IMPLICIT_FB;
402
Daniel Mackedcd3632012-04-12 13:51:12 +0200403add_sync_ep:
404 subs->sync_endpoint = snd_usb_add_endpoint(subs->stream->chip,
405 alts, ep, !subs->direction,
Daniel Mackc75a8a72012-04-12 13:51:14 +0200406 implicit_fb ?
407 SND_USB_ENDPOINT_TYPE_DATA :
408 SND_USB_ENDPOINT_TYPE_SYNC);
Daniel Mackedcd3632012-04-12 13:51:12 +0200409 if (!subs->sync_endpoint)
410 return -EINVAL;
411
412 subs->data_endpoint->sync_master = subs->sync_endpoint;
413 }
Daniel Macke5779992010-03-04 19:46:13 +0100414
Takashi Iwai9e9b5942012-07-06 08:11:43 +0200415 if ((err = snd_usb_init_pitch(subs->stream->chip, fmt->iface, alts, fmt)) < 0)
Daniel Macke5779992010-03-04 19:46:13 +0100416 return err;
417
418 subs->cur_audiofmt = fmt;
419
420 snd_usb_set_format_quirk(subs, fmt);
421
422#if 0
423 printk(KERN_DEBUG
424 "setting done: format = %d, rate = %d..%d, channels = %d\n",
425 fmt->format, fmt->rate_min, fmt->rate_max, fmt->channels);
426 printk(KERN_DEBUG
427 " datapipe = 0x%0x, syncpipe = 0x%0x\n",
428 subs->datapipe, subs->syncpipe);
429#endif
430
431 return 0;
432}
433
434/*
Dylan Reid61a70952012-09-18 09:49:48 -0700435 * configure endpoint params
436 *
437 * called during initial setup and upon resume
438 */
439static int configure_endpoint(struct snd_usb_substream *subs)
440{
441 int ret;
442
Dylan Reid61a70952012-09-18 09:49:48 -0700443 /* format changed */
Takashi Iwaia9bb3622012-11-20 18:32:06 +0100444 stop_endpoints(subs, false);
Dylan Reid61a70952012-09-18 09:49:48 -0700445 ret = snd_usb_endpoint_set_params(subs->data_endpoint,
446 subs->pcm_format,
447 subs->channels,
448 subs->period_bytes,
449 subs->cur_rate,
450 subs->cur_audiofmt,
451 subs->sync_endpoint);
452 if (ret < 0)
Takashi Iwai978520b2012-10-12 15:12:55 +0200453 return ret;
Dylan Reid61a70952012-09-18 09:49:48 -0700454
455 if (subs->sync_endpoint)
456 ret = snd_usb_endpoint_set_params(subs->data_endpoint,
457 subs->pcm_format,
458 subs->channels,
459 subs->period_bytes,
460 subs->cur_rate,
461 subs->cur_audiofmt,
462 NULL);
Dylan Reid61a70952012-09-18 09:49:48 -0700463 return ret;
464}
465
466/*
Daniel Macke5779992010-03-04 19:46:13 +0100467 * hw_params callback
468 *
469 * allocate a buffer and set the given audio format.
470 *
471 * so far we use a physically linear buffer although packetize transfer
472 * doesn't need a continuous area.
473 * if sg buffer is supported on the later version of alsa, we'll follow
474 * that.
475 */
476static int snd_usb_hw_params(struct snd_pcm_substream *substream,
477 struct snd_pcm_hw_params *hw_params)
478{
479 struct snd_usb_substream *subs = substream->runtime->private_data;
480 struct audioformat *fmt;
Dylan Reid61a70952012-09-18 09:49:48 -0700481 int ret;
Daniel Macke5779992010-03-04 19:46:13 +0100482
483 ret = snd_pcm_lib_alloc_vmalloc_buffer(substream,
484 params_buffer_bytes(hw_params));
485 if (ret < 0)
486 return ret;
487
Dylan Reid61a70952012-09-18 09:49:48 -0700488 subs->pcm_format = params_format(hw_params);
489 subs->period_bytes = params_period_bytes(hw_params);
490 subs->channels = params_channels(hw_params);
491 subs->cur_rate = params_rate(hw_params);
492
493 fmt = find_format(subs);
Daniel Macke5779992010-03-04 19:46:13 +0100494 if (!fmt) {
495 snd_printd(KERN_DEBUG "cannot set format: format = %#x, rate = %d, channels = %d\n",
Dylan Reid61a70952012-09-18 09:49:48 -0700496 subs->pcm_format, subs->cur_rate, subs->channels);
Daniel Macke5779992010-03-04 19:46:13 +0100497 return -EINVAL;
498 }
499
Takashi Iwai34f3c892012-10-15 12:16:02 +0200500 down_read(&subs->stream->chip->shutdown_rwsem);
Takashi Iwai978520b2012-10-12 15:12:55 +0200501 if (subs->stream->chip->shutdown)
502 ret = -ENODEV;
503 else
504 ret = set_format(subs, fmt);
Takashi Iwai34f3c892012-10-15 12:16:02 +0200505 up_read(&subs->stream->chip->shutdown_rwsem);
Takashi Iwai978520b2012-10-12 15:12:55 +0200506 if (ret < 0)
Daniel Macke5779992010-03-04 19:46:13 +0100507 return ret;
508
Dylan Reid61a70952012-09-18 09:49:48 -0700509 subs->interface = fmt->iface;
510 subs->altset_idx = fmt->altset_idx;
Takashi Iwai384dc0852012-09-18 14:49:31 +0200511 subs->need_setup_ep = true;
Daniel Macke5779992010-03-04 19:46:13 +0100512
Dylan Reid61a70952012-09-18 09:49:48 -0700513 return 0;
Daniel Macke5779992010-03-04 19:46:13 +0100514}
515
516/*
517 * hw_free callback
518 *
519 * reset the audio format and release the buffer
520 */
521static int snd_usb_hw_free(struct snd_pcm_substream *substream)
522{
523 struct snd_usb_substream *subs = substream->runtime->private_data;
524
525 subs->cur_audiofmt = NULL;
526 subs->cur_rate = 0;
527 subs->period_bytes = 0;
Takashi Iwai34f3c892012-10-15 12:16:02 +0200528 down_read(&subs->stream->chip->shutdown_rwsem);
Takashi Iwai978520b2012-10-12 15:12:55 +0200529 if (!subs->stream->chip->shutdown) {
Takashi Iwaia9bb3622012-11-20 18:32:06 +0100530 stop_endpoints(subs, true);
Takashi Iwai978520b2012-10-12 15:12:55 +0200531 deactivate_endpoints(subs);
532 }
Takashi Iwai34f3c892012-10-15 12:16:02 +0200533 up_read(&subs->stream->chip->shutdown_rwsem);
Daniel Macke5779992010-03-04 19:46:13 +0100534 return snd_pcm_lib_free_vmalloc_buffer(substream);
535}
536
537/*
538 * prepare callback
539 *
540 * only a few subtle things...
541 */
542static int snd_usb_pcm_prepare(struct snd_pcm_substream *substream)
543{
544 struct snd_pcm_runtime *runtime = substream->runtime;
545 struct snd_usb_substream *subs = runtime->private_data;
Dylan Reid61a70952012-09-18 09:49:48 -0700546 struct usb_host_interface *alts;
547 struct usb_interface *iface;
548 int ret;
Daniel Macke5779992010-03-04 19:46:13 +0100549
550 if (! subs->cur_audiofmt) {
551 snd_printk(KERN_ERR "usbaudio: no format is specified!\n");
552 return -ENXIO;
553 }
554
Takashi Iwai34f3c892012-10-15 12:16:02 +0200555 down_read(&subs->stream->chip->shutdown_rwsem);
Takashi Iwai978520b2012-10-12 15:12:55 +0200556 if (subs->stream->chip->shutdown) {
557 ret = -ENODEV;
558 goto unlock;
559 }
560 if (snd_BUG_ON(!subs->data_endpoint)) {
561 ret = -EIO;
562 goto unlock;
563 }
Daniel Mackedcd3632012-04-12 13:51:12 +0200564
Takashi Iwaif58161b2012-11-08 08:52:45 +0100565 snd_usb_endpoint_sync_pending_stop(subs->sync_endpoint);
566 snd_usb_endpoint_sync_pending_stop(subs->data_endpoint);
567
Dylan Reid61a70952012-09-18 09:49:48 -0700568 ret = set_format(subs, subs->cur_audiofmt);
569 if (ret < 0)
Takashi Iwai978520b2012-10-12 15:12:55 +0200570 goto unlock;
Dylan Reid61a70952012-09-18 09:49:48 -0700571
572 iface = usb_ifnum_to_if(subs->dev, subs->cur_audiofmt->iface);
573 alts = &iface->altsetting[subs->cur_audiofmt->altset_idx];
574 ret = snd_usb_init_sample_rate(subs->stream->chip,
575 subs->cur_audiofmt->iface,
576 alts,
577 subs->cur_audiofmt,
578 subs->cur_rate);
579 if (ret < 0)
Takashi Iwai978520b2012-10-12 15:12:55 +0200580 goto unlock;
Dylan Reid61a70952012-09-18 09:49:48 -0700581
Takashi Iwai384dc0852012-09-18 14:49:31 +0200582 if (subs->need_setup_ep) {
583 ret = configure_endpoint(subs);
584 if (ret < 0)
Takashi Iwai978520b2012-10-12 15:12:55 +0200585 goto unlock;
Takashi Iwai384dc0852012-09-18 14:49:31 +0200586 subs->need_setup_ep = false;
587 }
Dylan Reid61a70952012-09-18 09:49:48 -0700588
Daniel Macke5779992010-03-04 19:46:13 +0100589 /* some unit conversions in runtime */
Daniel Mackedcd3632012-04-12 13:51:12 +0200590 subs->data_endpoint->maxframesize =
591 bytes_to_frames(runtime, subs->data_endpoint->maxpacksize);
592 subs->data_endpoint->curframesize =
593 bytes_to_frames(runtime, subs->data_endpoint->curpacksize);
Daniel Macke5779992010-03-04 19:46:13 +0100594
595 /* reset the pointer */
596 subs->hwptr_done = 0;
597 subs->transfer_done = 0;
Pierre-Louis Bossart294c4fb2011-09-06 19:15:34 -0500598 subs->last_delay = 0;
599 subs->last_frame_number = 0;
Daniel Macke5779992010-03-04 19:46:13 +0100600 runtime->delay = 0;
601
Daniel Mackedcd3632012-04-12 13:51:12 +0200602 /* for playback, submit the URBs now; otherwise, the first hwptr_done
603 * updates for all URBs would happen at the same time when starting */
604 if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK)
Takashi Iwaia9bb3622012-11-20 18:32:06 +0100605 ret = start_endpoints(subs, true);
Daniel Mackedcd3632012-04-12 13:51:12 +0200606
Takashi Iwai978520b2012-10-12 15:12:55 +0200607 unlock:
Takashi Iwai34f3c892012-10-15 12:16:02 +0200608 up_read(&subs->stream->chip->shutdown_rwsem);
Takashi Iwai978520b2012-10-12 15:12:55 +0200609 return ret;
Daniel Macke5779992010-03-04 19:46:13 +0100610}
611
612static struct snd_pcm_hardware snd_usb_hardware =
613{
614 .info = SNDRV_PCM_INFO_MMAP |
615 SNDRV_PCM_INFO_MMAP_VALID |
616 SNDRV_PCM_INFO_BATCH |
617 SNDRV_PCM_INFO_INTERLEAVED |
618 SNDRV_PCM_INFO_BLOCK_TRANSFER |
619 SNDRV_PCM_INFO_PAUSE,
620 .buffer_bytes_max = 1024 * 1024,
621 .period_bytes_min = 64,
622 .period_bytes_max = 512 * 1024,
623 .periods_min = 2,
624 .periods_max = 1024,
625};
626
627static int hw_check_valid_format(struct snd_usb_substream *subs,
628 struct snd_pcm_hw_params *params,
629 struct audioformat *fp)
630{
631 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
632 struct snd_interval *ct = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
633 struct snd_mask *fmts = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
634 struct snd_interval *pt = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
Clemens Ladisch015eb0b2010-03-04 19:46:15 +0100635 struct snd_mask check_fmts;
Daniel Macke5779992010-03-04 19:46:13 +0100636 unsigned int ptime;
637
638 /* check the format */
Clemens Ladisch015eb0b2010-03-04 19:46:15 +0100639 snd_mask_none(&check_fmts);
640 check_fmts.bits[0] = (u32)fp->formats;
641 check_fmts.bits[1] = (u32)(fp->formats >> 32);
642 snd_mask_intersect(&check_fmts, fmts);
643 if (snd_mask_empty(&check_fmts)) {
Daniel Macke5779992010-03-04 19:46:13 +0100644 hwc_debug(" > check: no supported format %d\n", fp->format);
645 return 0;
646 }
647 /* check the channels */
648 if (fp->channels < ct->min || fp->channels > ct->max) {
649 hwc_debug(" > check: no valid channels %d (%d/%d)\n", fp->channels, ct->min, ct->max);
650 return 0;
651 }
652 /* check the rate is within the range */
653 if (fp->rate_min > it->max || (fp->rate_min == it->max && it->openmax)) {
654 hwc_debug(" > check: rate_min %d > max %d\n", fp->rate_min, it->max);
655 return 0;
656 }
657 if (fp->rate_max < it->min || (fp->rate_max == it->min && it->openmin)) {
658 hwc_debug(" > check: rate_max %d < min %d\n", fp->rate_max, it->min);
659 return 0;
660 }
661 /* check whether the period time is >= the data packet interval */
Takashi Iwai978520b2012-10-12 15:12:55 +0200662 if (subs->speed != USB_SPEED_FULL) {
Daniel Macke5779992010-03-04 19:46:13 +0100663 ptime = 125 * (1 << fp->datainterval);
664 if (ptime > pt->max || (ptime == pt->max && pt->openmax)) {
665 hwc_debug(" > check: ptime %u > max %u\n", ptime, pt->max);
666 return 0;
667 }
668 }
669 return 1;
670}
671
672static int hw_rule_rate(struct snd_pcm_hw_params *params,
673 struct snd_pcm_hw_rule *rule)
674{
675 struct snd_usb_substream *subs = rule->private;
676 struct list_head *p;
677 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
678 unsigned int rmin, rmax;
679 int changed;
680
681 hwc_debug("hw_rule_rate: (%d,%d)\n", it->min, it->max);
682 changed = 0;
683 rmin = rmax = 0;
684 list_for_each(p, &subs->fmt_list) {
685 struct audioformat *fp;
686 fp = list_entry(p, struct audioformat, list);
687 if (!hw_check_valid_format(subs, params, fp))
688 continue;
689 if (changed++) {
690 if (rmin > fp->rate_min)
691 rmin = fp->rate_min;
692 if (rmax < fp->rate_max)
693 rmax = fp->rate_max;
694 } else {
695 rmin = fp->rate_min;
696 rmax = fp->rate_max;
697 }
698 }
699
700 if (!changed) {
701 hwc_debug(" --> get empty\n");
702 it->empty = 1;
703 return -EINVAL;
704 }
705
706 changed = 0;
707 if (it->min < rmin) {
708 it->min = rmin;
709 it->openmin = 0;
710 changed = 1;
711 }
712 if (it->max > rmax) {
713 it->max = rmax;
714 it->openmax = 0;
715 changed = 1;
716 }
717 if (snd_interval_checkempty(it)) {
718 it->empty = 1;
719 return -EINVAL;
720 }
721 hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
722 return changed;
723}
724
725
726static int hw_rule_channels(struct snd_pcm_hw_params *params,
727 struct snd_pcm_hw_rule *rule)
728{
729 struct snd_usb_substream *subs = rule->private;
730 struct list_head *p;
731 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
732 unsigned int rmin, rmax;
733 int changed;
734
735 hwc_debug("hw_rule_channels: (%d,%d)\n", it->min, it->max);
736 changed = 0;
737 rmin = rmax = 0;
738 list_for_each(p, &subs->fmt_list) {
739 struct audioformat *fp;
740 fp = list_entry(p, struct audioformat, list);
741 if (!hw_check_valid_format(subs, params, fp))
742 continue;
743 if (changed++) {
744 if (rmin > fp->channels)
745 rmin = fp->channels;
746 if (rmax < fp->channels)
747 rmax = fp->channels;
748 } else {
749 rmin = fp->channels;
750 rmax = fp->channels;
751 }
752 }
753
754 if (!changed) {
755 hwc_debug(" --> get empty\n");
756 it->empty = 1;
757 return -EINVAL;
758 }
759
760 changed = 0;
761 if (it->min < rmin) {
762 it->min = rmin;
763 it->openmin = 0;
764 changed = 1;
765 }
766 if (it->max > rmax) {
767 it->max = rmax;
768 it->openmax = 0;
769 changed = 1;
770 }
771 if (snd_interval_checkempty(it)) {
772 it->empty = 1;
773 return -EINVAL;
774 }
775 hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
776 return changed;
777}
778
779static int hw_rule_format(struct snd_pcm_hw_params *params,
780 struct snd_pcm_hw_rule *rule)
781{
782 struct snd_usb_substream *subs = rule->private;
783 struct list_head *p;
784 struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
785 u64 fbits;
786 u32 oldbits[2];
787 int changed;
788
789 hwc_debug("hw_rule_format: %x:%x\n", fmt->bits[0], fmt->bits[1]);
790 fbits = 0;
791 list_for_each(p, &subs->fmt_list) {
792 struct audioformat *fp;
793 fp = list_entry(p, struct audioformat, list);
794 if (!hw_check_valid_format(subs, params, fp))
795 continue;
Clemens Ladisch015eb0b2010-03-04 19:46:15 +0100796 fbits |= fp->formats;
Daniel Macke5779992010-03-04 19:46:13 +0100797 }
798
799 oldbits[0] = fmt->bits[0];
800 oldbits[1] = fmt->bits[1];
801 fmt->bits[0] &= (u32)fbits;
802 fmt->bits[1] &= (u32)(fbits >> 32);
803 if (!fmt->bits[0] && !fmt->bits[1]) {
804 hwc_debug(" --> get empty\n");
805 return -EINVAL;
806 }
807 changed = (oldbits[0] != fmt->bits[0] || oldbits[1] != fmt->bits[1]);
808 hwc_debug(" --> %x:%x (changed = %d)\n", fmt->bits[0], fmt->bits[1], changed);
809 return changed;
810}
811
812static int hw_rule_period_time(struct snd_pcm_hw_params *params,
813 struct snd_pcm_hw_rule *rule)
814{
815 struct snd_usb_substream *subs = rule->private;
816 struct audioformat *fp;
817 struct snd_interval *it;
818 unsigned char min_datainterval;
819 unsigned int pmin;
820 int changed;
821
822 it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
823 hwc_debug("hw_rule_period_time: (%u,%u)\n", it->min, it->max);
824 min_datainterval = 0xff;
825 list_for_each_entry(fp, &subs->fmt_list, list) {
826 if (!hw_check_valid_format(subs, params, fp))
827 continue;
828 min_datainterval = min(min_datainterval, fp->datainterval);
829 }
830 if (min_datainterval == 0xff) {
Uwe Kleine-Königa7ce2e02010-07-12 17:15:44 +0200831 hwc_debug(" --> get empty\n");
Daniel Macke5779992010-03-04 19:46:13 +0100832 it->empty = 1;
833 return -EINVAL;
834 }
835 pmin = 125 * (1 << min_datainterval);
836 changed = 0;
837 if (it->min < pmin) {
838 it->min = pmin;
839 it->openmin = 0;
840 changed = 1;
841 }
842 if (snd_interval_checkempty(it)) {
843 it->empty = 1;
844 return -EINVAL;
845 }
846 hwc_debug(" --> (%u,%u) (changed = %d)\n", it->min, it->max, changed);
847 return changed;
848}
849
850/*
851 * If the device supports unusual bit rates, does the request meet these?
852 */
853static int snd_usb_pcm_check_knot(struct snd_pcm_runtime *runtime,
854 struct snd_usb_substream *subs)
855{
856 struct audioformat *fp;
Takashi Iwai0717d0f2012-03-15 16:14:38 +0100857 int *rate_list;
Daniel Macke5779992010-03-04 19:46:13 +0100858 int count = 0, needs_knot = 0;
859 int err;
860
Clemens Ladisch5cd5d7c2012-05-18 18:00:43 +0200861 kfree(subs->rate_list.list);
862 subs->rate_list.list = NULL;
863
Daniel Macke5779992010-03-04 19:46:13 +0100864 list_for_each_entry(fp, &subs->fmt_list, list) {
865 if (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)
866 return 0;
867 count += fp->nr_rates;
868 if (fp->rates & SNDRV_PCM_RATE_KNOT)
869 needs_knot = 1;
870 }
871 if (!needs_knot)
872 return 0;
873
Takashi Iwai0717d0f2012-03-15 16:14:38 +0100874 subs->rate_list.list = rate_list =
875 kmalloc(sizeof(int) * count, GFP_KERNEL);
Jesper Juhl8a8d56b2010-10-29 20:40:23 +0200876 if (!subs->rate_list.list)
877 return -ENOMEM;
878 subs->rate_list.count = count;
Daniel Macke5779992010-03-04 19:46:13 +0100879 subs->rate_list.mask = 0;
880 count = 0;
881 list_for_each_entry(fp, &subs->fmt_list, list) {
882 int i;
883 for (i = 0; i < fp->nr_rates; i++)
Takashi Iwai0717d0f2012-03-15 16:14:38 +0100884 rate_list[count++] = fp->rate_table[i];
Daniel Macke5779992010-03-04 19:46:13 +0100885 }
886 err = snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
887 &subs->rate_list);
888 if (err < 0)
889 return err;
890
891 return 0;
892}
893
894
895/*
896 * set up the runtime hardware information.
897 */
898
899static int setup_hw_info(struct snd_pcm_runtime *runtime, struct snd_usb_substream *subs)
900{
901 struct list_head *p;
902 unsigned int pt, ptmin;
903 int param_period_time_if_needed;
904 int err;
905
906 runtime->hw.formats = subs->formats;
907
908 runtime->hw.rate_min = 0x7fffffff;
909 runtime->hw.rate_max = 0;
910 runtime->hw.channels_min = 256;
911 runtime->hw.channels_max = 0;
912 runtime->hw.rates = 0;
913 ptmin = UINT_MAX;
914 /* check min/max rates and channels */
915 list_for_each(p, &subs->fmt_list) {
916 struct audioformat *fp;
917 fp = list_entry(p, struct audioformat, list);
918 runtime->hw.rates |= fp->rates;
919 if (runtime->hw.rate_min > fp->rate_min)
920 runtime->hw.rate_min = fp->rate_min;
921 if (runtime->hw.rate_max < fp->rate_max)
922 runtime->hw.rate_max = fp->rate_max;
923 if (runtime->hw.channels_min > fp->channels)
924 runtime->hw.channels_min = fp->channels;
925 if (runtime->hw.channels_max < fp->channels)
926 runtime->hw.channels_max = fp->channels;
927 if (fp->fmt_type == UAC_FORMAT_TYPE_II && fp->frame_size > 0) {
928 /* FIXME: there might be more than one audio formats... */
929 runtime->hw.period_bytes_min = runtime->hw.period_bytes_max =
930 fp->frame_size;
931 }
932 pt = 125 * (1 << fp->datainterval);
933 ptmin = min(ptmin, pt);
934 }
Oliver Neukum88a85162011-03-11 14:51:12 +0100935 err = snd_usb_autoresume(subs->stream->chip);
936 if (err < 0)
937 return err;
Daniel Macke5779992010-03-04 19:46:13 +0100938
939 param_period_time_if_needed = SNDRV_PCM_HW_PARAM_PERIOD_TIME;
Takashi Iwai978520b2012-10-12 15:12:55 +0200940 if (subs->speed == USB_SPEED_FULL)
Daniel Macke5779992010-03-04 19:46:13 +0100941 /* full speed devices have fixed data packet interval */
942 ptmin = 1000;
943 if (ptmin == 1000)
944 /* if period time doesn't go below 1 ms, no rules needed */
945 param_period_time_if_needed = -1;
946 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
947 ptmin, UINT_MAX);
948
949 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
950 hw_rule_rate, subs,
951 SNDRV_PCM_HW_PARAM_FORMAT,
952 SNDRV_PCM_HW_PARAM_CHANNELS,
953 param_period_time_if_needed,
954 -1)) < 0)
Oliver Neukum88a85162011-03-11 14:51:12 +0100955 goto rep_err;
Daniel Macke5779992010-03-04 19:46:13 +0100956 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
957 hw_rule_channels, subs,
958 SNDRV_PCM_HW_PARAM_FORMAT,
959 SNDRV_PCM_HW_PARAM_RATE,
960 param_period_time_if_needed,
961 -1)) < 0)
Oliver Neukum88a85162011-03-11 14:51:12 +0100962 goto rep_err;
Daniel Macke5779992010-03-04 19:46:13 +0100963 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
964 hw_rule_format, subs,
965 SNDRV_PCM_HW_PARAM_RATE,
966 SNDRV_PCM_HW_PARAM_CHANNELS,
967 param_period_time_if_needed,
968 -1)) < 0)
Oliver Neukum88a85162011-03-11 14:51:12 +0100969 goto rep_err;
Daniel Macke5779992010-03-04 19:46:13 +0100970 if (param_period_time_if_needed >= 0) {
971 err = snd_pcm_hw_rule_add(runtime, 0,
972 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
973 hw_rule_period_time, subs,
974 SNDRV_PCM_HW_PARAM_FORMAT,
975 SNDRV_PCM_HW_PARAM_CHANNELS,
976 SNDRV_PCM_HW_PARAM_RATE,
977 -1);
978 if (err < 0)
Oliver Neukum88a85162011-03-11 14:51:12 +0100979 goto rep_err;
Daniel Macke5779992010-03-04 19:46:13 +0100980 }
981 if ((err = snd_usb_pcm_check_knot(runtime, subs)) < 0)
Oliver Neukum88a85162011-03-11 14:51:12 +0100982 goto rep_err;
Daniel Macke5779992010-03-04 19:46:13 +0100983 return 0;
Oliver Neukum88a85162011-03-11 14:51:12 +0100984
985rep_err:
986 snd_usb_autosuspend(subs->stream->chip);
987 return err;
Daniel Macke5779992010-03-04 19:46:13 +0100988}
989
990static int snd_usb_pcm_open(struct snd_pcm_substream *substream, int direction)
991{
992 struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
993 struct snd_pcm_runtime *runtime = substream->runtime;
994 struct snd_usb_substream *subs = &as->substream[direction];
995
996 subs->interface = -1;
Clemens Ladische11b4e02010-03-04 19:46:14 +0100997 subs->altset_idx = 0;
Daniel Macke5779992010-03-04 19:46:13 +0100998 runtime->hw = snd_usb_hardware;
999 runtime->private_data = subs;
1000 subs->pcm_substream = substream;
Oliver Neukum88a85162011-03-11 14:51:12 +01001001 /* runtime PM is also done there */
Daniel Macke5779992010-03-04 19:46:13 +01001002 return setup_hw_info(runtime, subs);
1003}
1004
1005static int snd_usb_pcm_close(struct snd_pcm_substream *substream, int direction)
1006{
1007 struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
1008 struct snd_usb_substream *subs = &as->substream[direction];
1009
Takashi Iwaia9bb3622012-11-20 18:32:06 +01001010 stop_endpoints(subs, false);
Daniel Mack68e67f42012-07-12 13:08:40 +02001011
1012 if (!as->chip->shutdown && subs->interface >= 0) {
1013 usb_set_interface(subs->dev, subs->interface, 0);
1014 subs->interface = -1;
1015 }
1016
Daniel Macke5779992010-03-04 19:46:13 +01001017 subs->pcm_substream = NULL;
Oliver Neukum88a85162011-03-11 14:51:12 +01001018 snd_usb_autosuspend(subs->stream->chip);
Daniel Mackedcd3632012-04-12 13:51:12 +02001019
Daniel Mack68e67f42012-07-12 13:08:40 +02001020 return 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001021}
1022
1023/* Since a URB can handle only a single linear buffer, we must use double
1024 * buffering when the data to be transferred overflows the buffer boundary.
1025 * To avoid inconsistencies when updating hwptr_done, we use double buffering
1026 * for all URBs.
1027 */
1028static void retire_capture_urb(struct snd_usb_substream *subs,
1029 struct urb *urb)
1030{
1031 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1032 unsigned int stride, frames, bytes, oldptr;
1033 int i, period_elapsed = 0;
1034 unsigned long flags;
1035 unsigned char *cp;
1036
1037 stride = runtime->frame_bits >> 3;
1038
1039 for (i = 0; i < urb->number_of_packets; i++) {
1040 cp = (unsigned char *)urb->transfer_buffer + urb->iso_frame_desc[i].offset;
1041 if (urb->iso_frame_desc[i].status && printk_ratelimit()) {
1042 snd_printdd(KERN_ERR "frame %d active: %d\n", i, urb->iso_frame_desc[i].status);
1043 // continue;
1044 }
1045 bytes = urb->iso_frame_desc[i].actual_length;
1046 frames = bytes / stride;
1047 if (!subs->txfr_quirk)
1048 bytes = frames * stride;
1049 if (bytes % (runtime->sample_bits >> 3) != 0) {
1050#ifdef CONFIG_SND_DEBUG_VERBOSE
1051 int oldbytes = bytes;
1052#endif
1053 bytes = frames * stride;
1054 snd_printdd(KERN_ERR "Corrected urb data len. %d->%d\n",
1055 oldbytes, bytes);
1056 }
1057 /* update the current pointer */
1058 spin_lock_irqsave(&subs->lock, flags);
1059 oldptr = subs->hwptr_done;
1060 subs->hwptr_done += bytes;
1061 if (subs->hwptr_done >= runtime->buffer_size * stride)
1062 subs->hwptr_done -= runtime->buffer_size * stride;
1063 frames = (bytes + (oldptr % stride)) / stride;
1064 subs->transfer_done += frames;
1065 if (subs->transfer_done >= runtime->period_size) {
1066 subs->transfer_done -= runtime->period_size;
1067 period_elapsed = 1;
1068 }
1069 spin_unlock_irqrestore(&subs->lock, flags);
1070 /* copy a data chunk */
1071 if (oldptr + bytes > runtime->buffer_size * stride) {
1072 unsigned int bytes1 =
1073 runtime->buffer_size * stride - oldptr;
1074 memcpy(runtime->dma_area + oldptr, cp, bytes1);
1075 memcpy(runtime->dma_area, cp + bytes1, bytes - bytes1);
1076 } else {
1077 memcpy(runtime->dma_area + oldptr, cp, bytes);
1078 }
1079 }
1080
1081 if (period_elapsed)
1082 snd_pcm_period_elapsed(subs->pcm_substream);
1083}
1084
1085static void prepare_playback_urb(struct snd_usb_substream *subs,
1086 struct urb *urb)
1087{
1088 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
Daniel Mack245baf92012-08-30 18:52:30 +02001089 struct snd_usb_endpoint *ep = subs->data_endpoint;
Daniel Mackedcd3632012-04-12 13:51:12 +02001090 struct snd_urb_ctx *ctx = urb->context;
1091 unsigned int counts, frames, bytes;
1092 int i, stride, period_elapsed = 0;
1093 unsigned long flags;
1094
1095 stride = runtime->frame_bits >> 3;
1096
1097 frames = 0;
1098 urb->number_of_packets = 0;
1099 spin_lock_irqsave(&subs->lock, flags);
1100 for (i = 0; i < ctx->packets; i++) {
Daniel Mack245baf92012-08-30 18:52:30 +02001101 if (ctx->packet_size[i])
1102 counts = ctx->packet_size[i];
1103 else
1104 counts = snd_usb_endpoint_next_packet_size(ep);
1105
Daniel Mackedcd3632012-04-12 13:51:12 +02001106 /* set up descriptor */
1107 urb->iso_frame_desc[i].offset = frames * stride;
1108 urb->iso_frame_desc[i].length = counts * stride;
1109 frames += counts;
1110 urb->number_of_packets++;
1111 subs->transfer_done += counts;
1112 if (subs->transfer_done >= runtime->period_size) {
1113 subs->transfer_done -= runtime->period_size;
1114 period_elapsed = 1;
1115 if (subs->fmt_type == UAC_FORMAT_TYPE_II) {
1116 if (subs->transfer_done > 0) {
1117 /* FIXME: fill-max mode is not
1118 * supported yet */
1119 frames -= subs->transfer_done;
1120 counts -= subs->transfer_done;
1121 urb->iso_frame_desc[i].length =
1122 counts * stride;
1123 subs->transfer_done = 0;
1124 }
1125 i++;
1126 if (i < ctx->packets) {
1127 /* add a transfer delimiter */
1128 urb->iso_frame_desc[i].offset =
1129 frames * stride;
1130 urb->iso_frame_desc[i].length = 0;
1131 urb->number_of_packets++;
1132 }
1133 break;
1134 }
1135 }
1136 if (period_elapsed &&
1137 !snd_usb_endpoint_implict_feedback_sink(subs->data_endpoint)) /* finish at the period boundary */
1138 break;
1139 }
1140 bytes = frames * stride;
1141 if (subs->hwptr_done + bytes > runtime->buffer_size * stride) {
1142 /* err, the transferred area goes over buffer boundary. */
1143 unsigned int bytes1 =
1144 runtime->buffer_size * stride - subs->hwptr_done;
1145 memcpy(urb->transfer_buffer,
1146 runtime->dma_area + subs->hwptr_done, bytes1);
1147 memcpy(urb->transfer_buffer + bytes1,
1148 runtime->dma_area, bytes - bytes1);
1149 } else {
1150 memcpy(urb->transfer_buffer,
1151 runtime->dma_area + subs->hwptr_done, bytes);
1152 }
1153 subs->hwptr_done += bytes;
1154 if (subs->hwptr_done >= runtime->buffer_size * stride)
1155 subs->hwptr_done -= runtime->buffer_size * stride;
Daniel Mackfbcfbf52012-08-30 18:52:29 +02001156
1157 /* update delay with exact number of samples queued */
1158 runtime->delay = subs->last_delay;
Daniel Mackedcd3632012-04-12 13:51:12 +02001159 runtime->delay += frames;
Daniel Mackfbcfbf52012-08-30 18:52:29 +02001160 subs->last_delay = runtime->delay;
1161
1162 /* realign last_frame_number */
1163 subs->last_frame_number = usb_get_current_frame_number(subs->dev);
1164 subs->last_frame_number &= 0xFF; /* keep 8 LSBs */
1165
Daniel Mackedcd3632012-04-12 13:51:12 +02001166 spin_unlock_irqrestore(&subs->lock, flags);
1167 urb->transfer_buffer_length = bytes;
1168 if (period_elapsed)
1169 snd_pcm_period_elapsed(subs->pcm_substream);
1170}
1171
1172/*
1173 * process after playback data complete
1174 * - decrease the delay count again
1175 */
1176static void retire_playback_urb(struct snd_usb_substream *subs,
1177 struct urb *urb)
1178{
1179 unsigned long flags;
1180 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1181 int stride = runtime->frame_bits >> 3;
1182 int processed = urb->transfer_buffer_length / stride;
Daniel Mackfbcfbf52012-08-30 18:52:29 +02001183 int est_delay;
Daniel Mackedcd3632012-04-12 13:51:12 +02001184
Takashi Iwai1213a202012-09-06 14:58:00 +02001185 /* ignore the delay accounting when procssed=0 is given, i.e.
1186 * silent payloads are procssed before handling the actual data
1187 */
1188 if (!processed)
1189 return;
1190
Daniel Mackedcd3632012-04-12 13:51:12 +02001191 spin_lock_irqsave(&subs->lock, flags);
Daniel Mackfbcfbf52012-08-30 18:52:29 +02001192 est_delay = snd_usb_pcm_delay(subs, runtime->rate);
1193 /* update delay with exact number of samples played */
1194 if (processed > subs->last_delay)
1195 subs->last_delay = 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001196 else
Daniel Mackfbcfbf52012-08-30 18:52:29 +02001197 subs->last_delay -= processed;
1198 runtime->delay = subs->last_delay;
1199
1200 /*
1201 * Report when delay estimate is off by more than 2ms.
1202 * The error should be lower than 2ms since the estimate relies
1203 * on two reads of a counter updated every ms.
1204 */
1205 if (abs(est_delay - subs->last_delay) * 1000 > runtime->rate * 2)
1206 snd_printk(KERN_DEBUG "delay: estimated %d, actual %d\n",
1207 est_delay, subs->last_delay);
1208
Daniel Mackedcd3632012-04-12 13:51:12 +02001209 spin_unlock_irqrestore(&subs->lock, flags);
Daniel Macke5779992010-03-04 19:46:13 +01001210}
1211
1212static int snd_usb_playback_open(struct snd_pcm_substream *substream)
1213{
1214 return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_PLAYBACK);
1215}
1216
1217static int snd_usb_playback_close(struct snd_pcm_substream *substream)
1218{
1219 return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_PLAYBACK);
1220}
1221
1222static int snd_usb_capture_open(struct snd_pcm_substream *substream)
1223{
1224 return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_CAPTURE);
1225}
1226
1227static int snd_usb_capture_close(struct snd_pcm_substream *substream)
1228{
1229 return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_CAPTURE);
1230}
1231
Daniel Mackedcd3632012-04-12 13:51:12 +02001232static int snd_usb_substream_playback_trigger(struct snd_pcm_substream *substream,
1233 int cmd)
1234{
1235 struct snd_usb_substream *subs = substream->runtime->private_data;
1236
1237 switch (cmd) {
1238 case SNDRV_PCM_TRIGGER_START:
1239 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1240 subs->data_endpoint->prepare_data_urb = prepare_playback_urb;
1241 subs->data_endpoint->retire_data_urb = retire_playback_urb;
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001242 subs->running = 1;
Daniel Mackedcd3632012-04-12 13:51:12 +02001243 return 0;
1244 case SNDRV_PCM_TRIGGER_STOP:
Takashi Iwaia9bb3622012-11-20 18:32:06 +01001245 stop_endpoints(subs, false);
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001246 subs->running = 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001247 return 0;
1248 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1249 subs->data_endpoint->prepare_data_urb = NULL;
1250 subs->data_endpoint->retire_data_urb = NULL;
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001251 subs->running = 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001252 return 0;
1253 }
1254
1255 return -EINVAL;
1256}
1257
Daniel Mackafe25962012-06-16 16:58:04 +02001258static int snd_usb_substream_capture_trigger(struct snd_pcm_substream *substream,
1259 int cmd)
Daniel Mackedcd3632012-04-12 13:51:12 +02001260{
1261 int err;
1262 struct snd_usb_substream *subs = substream->runtime->private_data;
1263
1264 switch (cmd) {
1265 case SNDRV_PCM_TRIGGER_START:
Takashi Iwaia9bb3622012-11-20 18:32:06 +01001266 err = start_endpoints(subs, false);
Daniel Mackedcd3632012-04-12 13:51:12 +02001267 if (err < 0)
1268 return err;
1269
1270 subs->data_endpoint->retire_data_urb = retire_capture_urb;
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001271 subs->running = 1;
Daniel Mackedcd3632012-04-12 13:51:12 +02001272 return 0;
1273 case SNDRV_PCM_TRIGGER_STOP:
Takashi Iwaia9bb3622012-11-20 18:32:06 +01001274 stop_endpoints(subs, false);
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001275 subs->running = 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001276 return 0;
1277 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1278 subs->data_endpoint->retire_data_urb = NULL;
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001279 subs->running = 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001280 return 0;
1281 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1282 subs->data_endpoint->retire_data_urb = retire_capture_urb;
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001283 subs->running = 1;
Daniel Mackedcd3632012-04-12 13:51:12 +02001284 return 0;
1285 }
1286
1287 return -EINVAL;
1288}
1289
Daniel Macke5779992010-03-04 19:46:13 +01001290static struct snd_pcm_ops snd_usb_playback_ops = {
1291 .open = snd_usb_playback_open,
1292 .close = snd_usb_playback_close,
1293 .ioctl = snd_pcm_lib_ioctl,
1294 .hw_params = snd_usb_hw_params,
1295 .hw_free = snd_usb_hw_free,
1296 .prepare = snd_usb_pcm_prepare,
1297 .trigger = snd_usb_substream_playback_trigger,
1298 .pointer = snd_usb_pcm_pointer,
1299 .page = snd_pcm_lib_get_vmalloc_page,
1300 .mmap = snd_pcm_lib_mmap_vmalloc,
1301};
1302
1303static struct snd_pcm_ops snd_usb_capture_ops = {
1304 .open = snd_usb_capture_open,
1305 .close = snd_usb_capture_close,
1306 .ioctl = snd_pcm_lib_ioctl,
1307 .hw_params = snd_usb_hw_params,
1308 .hw_free = snd_usb_hw_free,
1309 .prepare = snd_usb_pcm_prepare,
1310 .trigger = snd_usb_substream_capture_trigger,
1311 .pointer = snd_usb_pcm_pointer,
1312 .page = snd_pcm_lib_get_vmalloc_page,
1313 .mmap = snd_pcm_lib_mmap_vmalloc,
1314};
1315
1316void snd_usb_set_pcm_ops(struct snd_pcm *pcm, int stream)
1317{
1318 snd_pcm_set_ops(pcm, stream,
1319 stream == SNDRV_PCM_STREAM_PLAYBACK ?
1320 &snd_usb_playback_ops : &snd_usb_capture_ops);
1321}