blob: 3a384479e92376840f234c8c7accf810c12b49c7 [file] [log] [blame]
Daniel Macke5779992010-03-04 19:46:13 +01001/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15 */
16
17#include <linux/init.h>
Stephen Rothwell9966dda2010-03-29 19:01:48 +110018#include <linux/slab.h>
Daniel Mackedcd3632012-04-12 13:51:12 +020019#include <linux/ratelimit.h>
Daniel Macke5779992010-03-04 19:46:13 +010020#include <linux/usb.h>
21#include <linux/usb/audio.h>
Daniel Mack7e847892010-03-11 21:13:20 +010022#include <linux/usb/audio-v2.h>
Daniel Macke5779992010-03-04 19:46:13 +010023
24#include <sound/core.h>
25#include <sound/pcm.h>
26#include <sound/pcm_params.h>
27
28#include "usbaudio.h"
29#include "card.h"
30#include "quirks.h"
31#include "debug.h"
Daniel Mackc731bc92011-09-14 12:46:57 +020032#include "endpoint.h"
Daniel Macke5779992010-03-04 19:46:13 +010033#include "helper.h"
34#include "pcm.h"
Daniel Mack79f920f2010-05-31 14:51:31 +020035#include "clock.h"
Oliver Neukum88a85162011-03-11 14:51:12 +010036#include "power.h"
Daniel Macke5779992010-03-04 19:46:13 +010037
Daniel Mackedcd3632012-04-12 13:51:12 +020038#define SUBSTREAM_FLAG_DATA_EP_STARTED 0
39#define SUBSTREAM_FLAG_SYNC_EP_STARTED 1
40
Pierre-Louis Bossart294c4fb2011-09-06 19:15:34 -050041/* return the estimated delay based on USB frame counters */
42snd_pcm_uframes_t snd_usb_pcm_delay(struct snd_usb_substream *subs,
43 unsigned int rate)
44{
45 int current_frame_number;
46 int frame_diff;
47 int est_delay;
48
Takashi Iwai48779a02012-11-23 16:00:37 +010049 if (!subs->last_delay)
50 return 0; /* short path */
51
Pierre-Louis Bossart294c4fb2011-09-06 19:15:34 -050052 current_frame_number = usb_get_current_frame_number(subs->dev);
53 /*
54 * HCD implementations use different widths, use lower 8 bits.
55 * The delay will be managed up to 256ms, which is more than
56 * enough
57 */
58 frame_diff = (current_frame_number - subs->last_frame_number) & 0xff;
59
60 /* Approximation based on number of samples per USB frame (ms),
61 some truncation for 44.1 but the estimate is good enough */
Pierre-Louis Bossarte4cc6152012-12-19 11:39:05 -060062 est_delay = frame_diff * rate / 1000;
63 if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK)
64 est_delay = subs->last_delay - est_delay;
65 else
66 est_delay = subs->last_delay + est_delay;
67
Pierre-Louis Bossart294c4fb2011-09-06 19:15:34 -050068 if (est_delay < 0)
69 est_delay = 0;
70 return est_delay;
71}
72
Daniel Macke5779992010-03-04 19:46:13 +010073/*
74 * return the current pcm pointer. just based on the hwptr_done value.
75 */
76static snd_pcm_uframes_t snd_usb_pcm_pointer(struct snd_pcm_substream *substream)
77{
78 struct snd_usb_substream *subs;
79 unsigned int hwptr_done;
80
81 subs = (struct snd_usb_substream *)substream->runtime->private_data;
Takashi Iwai978520b2012-10-12 15:12:55 +020082 if (subs->stream->chip->shutdown)
83 return SNDRV_PCM_POS_XRUN;
Daniel Macke5779992010-03-04 19:46:13 +010084 spin_lock(&subs->lock);
85 hwptr_done = subs->hwptr_done;
Pierre-Louis Bossarte4cc6152012-12-19 11:39:05 -060086 substream->runtime->delay = snd_usb_pcm_delay(subs,
Pierre-Louis Bossart294c4fb2011-09-06 19:15:34 -050087 substream->runtime->rate);
Daniel Macke5779992010-03-04 19:46:13 +010088 spin_unlock(&subs->lock);
89 return hwptr_done / (substream->runtime->frame_bits >> 3);
90}
91
92/*
93 * find a matching audio format
94 */
Dylan Reid61a70952012-09-18 09:49:48 -070095static struct audioformat *find_format(struct snd_usb_substream *subs)
Daniel Macke5779992010-03-04 19:46:13 +010096{
97 struct list_head *p;
98 struct audioformat *found = NULL;
99 int cur_attr = 0, attr;
100
101 list_for_each(p, &subs->fmt_list) {
102 struct audioformat *fp;
103 fp = list_entry(p, struct audioformat, list);
Dylan Reid61a70952012-09-18 09:49:48 -0700104 if (!(fp->formats & (1uLL << subs->pcm_format)))
Clemens Ladisch015eb0b2010-03-04 19:46:15 +0100105 continue;
Dylan Reid61a70952012-09-18 09:49:48 -0700106 if (fp->channels != subs->channels)
Daniel Macke5779992010-03-04 19:46:13 +0100107 continue;
Dylan Reid61a70952012-09-18 09:49:48 -0700108 if (subs->cur_rate < fp->rate_min ||
109 subs->cur_rate > fp->rate_max)
Daniel Macke5779992010-03-04 19:46:13 +0100110 continue;
111 if (! (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)) {
112 unsigned int i;
113 for (i = 0; i < fp->nr_rates; i++)
Dylan Reid61a70952012-09-18 09:49:48 -0700114 if (fp->rate_table[i] == subs->cur_rate)
Daniel Macke5779992010-03-04 19:46:13 +0100115 break;
116 if (i >= fp->nr_rates)
117 continue;
118 }
119 attr = fp->ep_attr & USB_ENDPOINT_SYNCTYPE;
120 if (! found) {
121 found = fp;
122 cur_attr = attr;
123 continue;
124 }
125 /* avoid async out and adaptive in if the other method
126 * supports the same format.
127 * this is a workaround for the case like
128 * M-audio audiophile USB.
129 */
130 if (attr != cur_attr) {
131 if ((attr == USB_ENDPOINT_SYNC_ASYNC &&
132 subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
133 (attr == USB_ENDPOINT_SYNC_ADAPTIVE &&
134 subs->direction == SNDRV_PCM_STREAM_CAPTURE))
135 continue;
136 if ((cur_attr == USB_ENDPOINT_SYNC_ASYNC &&
137 subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
138 (cur_attr == USB_ENDPOINT_SYNC_ADAPTIVE &&
139 subs->direction == SNDRV_PCM_STREAM_CAPTURE)) {
140 found = fp;
141 cur_attr = attr;
142 continue;
143 }
144 }
145 /* find the format with the largest max. packet size */
146 if (fp->maxpacksize > found->maxpacksize) {
147 found = fp;
148 cur_attr = attr;
149 }
150 }
151 return found;
152}
153
Daniel Mack767d75a2010-03-04 19:46:17 +0100154static int init_pitch_v1(struct snd_usb_audio *chip, int iface,
155 struct usb_host_interface *alts,
156 struct audioformat *fmt)
Daniel Macke5779992010-03-04 19:46:13 +0100157{
Daniel Mack767d75a2010-03-04 19:46:17 +0100158 struct usb_device *dev = chip->dev;
Daniel Macke5779992010-03-04 19:46:13 +0100159 unsigned int ep;
160 unsigned char data[1];
161 int err;
162
163 ep = get_endpoint(alts, 0)->bEndpointAddress;
Daniel Mack767d75a2010-03-04 19:46:17 +0100164
Daniel Mack767d75a2010-03-04 19:46:17 +0100165 data[0] = 1;
166 if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC_SET_CUR,
167 USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT,
168 UAC_EP_CS_ATTR_PITCH_CONTROL << 8, ep,
Clemens Ladisch17d900c2011-09-26 21:15:27 +0200169 data, sizeof(data))) < 0) {
Daniel Mack767d75a2010-03-04 19:46:17 +0100170 snd_printk(KERN_ERR "%d:%d:%d: cannot set enable PITCH\n",
171 dev->devnum, iface, ep);
172 return err;
Daniel Macke5779992010-03-04 19:46:13 +0100173 }
Daniel Mack767d75a2010-03-04 19:46:17 +0100174
Daniel Macke5779992010-03-04 19:46:13 +0100175 return 0;
176}
177
Daniel Mack92c25612010-05-26 18:11:39 +0200178static int init_pitch_v2(struct snd_usb_audio *chip, int iface,
179 struct usb_host_interface *alts,
180 struct audioformat *fmt)
181{
182 struct usb_device *dev = chip->dev;
183 unsigned char data[1];
Daniel Mack92c25612010-05-26 18:11:39 +0200184 int err;
185
Daniel Mack92c25612010-05-26 18:11:39 +0200186 data[0] = 1;
187 if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC2_CS_CUR,
188 USB_TYPE_CLASS | USB_RECIP_ENDPOINT | USB_DIR_OUT,
189 UAC2_EP_CS_PITCH << 8, 0,
Clemens Ladisch17d900c2011-09-26 21:15:27 +0200190 data, sizeof(data))) < 0) {
Daniel Mack92c25612010-05-26 18:11:39 +0200191 snd_printk(KERN_ERR "%d:%d:%d: cannot set enable PITCH (v2)\n",
192 dev->devnum, iface, fmt->altsetting);
193 return err;
194 }
195
196 return 0;
197}
198
Daniel Mack767d75a2010-03-04 19:46:17 +0100199/*
Daniel Mack92c25612010-05-26 18:11:39 +0200200 * initialize the pitch control and sample rate
Daniel Mack767d75a2010-03-04 19:46:17 +0100201 */
202int snd_usb_init_pitch(struct snd_usb_audio *chip, int iface,
203 struct usb_host_interface *alts,
204 struct audioformat *fmt)
205{
206 struct usb_interface_descriptor *altsd = get_iface_desc(alts);
207
Daniel Mack92c25612010-05-26 18:11:39 +0200208 /* if endpoint doesn't have pitch control, bail out */
209 if (!(fmt->attributes & UAC_EP_CS_ATTR_PITCH_CONTROL))
210 return 0;
211
Daniel Mack767d75a2010-03-04 19:46:17 +0100212 switch (altsd->bInterfaceProtocol) {
213 case UAC_VERSION_1:
Clemens Ladischa2acad82010-09-03 10:53:11 +0200214 default:
Daniel Mack767d75a2010-03-04 19:46:17 +0100215 return init_pitch_v1(chip, iface, alts, fmt);
216
217 case UAC_VERSION_2:
Daniel Mack92c25612010-05-26 18:11:39 +0200218 return init_pitch_v2(chip, iface, alts, fmt);
Daniel Mack767d75a2010-03-04 19:46:17 +0100219 }
Daniel Mack767d75a2010-03-04 19:46:17 +0100220}
221
Takashi Iwaia9bb3622012-11-20 18:32:06 +0100222static int start_endpoints(struct snd_usb_substream *subs, bool can_sleep)
Daniel Mackedcd3632012-04-12 13:51:12 +0200223{
224 int err;
225
226 if (!subs->data_endpoint)
227 return -EINVAL;
228
229 if (!test_and_set_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags)) {
230 struct snd_usb_endpoint *ep = subs->data_endpoint;
231
232 snd_printdd(KERN_DEBUG "Starting data EP @%p\n", ep);
233
234 ep->data_subs = subs;
Daniel Mack015618b2012-08-29 13:17:05 +0200235 err = snd_usb_endpoint_start(ep, can_sleep);
Daniel Mackedcd3632012-04-12 13:51:12 +0200236 if (err < 0) {
237 clear_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags);
238 return err;
239 }
240 }
241
242 if (subs->sync_endpoint &&
243 !test_and_set_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags)) {
244 struct snd_usb_endpoint *ep = subs->sync_endpoint;
245
Daniel Mack2e4a2632012-08-30 18:52:31 +0200246 if (subs->data_endpoint->iface != subs->sync_endpoint->iface ||
247 subs->data_endpoint->alt_idx != subs->sync_endpoint->alt_idx) {
248 err = usb_set_interface(subs->dev,
249 subs->sync_endpoint->iface,
250 subs->sync_endpoint->alt_idx);
251 if (err < 0) {
252 snd_printk(KERN_ERR
253 "%d:%d:%d: cannot set interface (%d)\n",
254 subs->dev->devnum,
255 subs->sync_endpoint->iface,
256 subs->sync_endpoint->alt_idx, err);
257 return -EIO;
258 }
259 }
260
Daniel Mackedcd3632012-04-12 13:51:12 +0200261 snd_printdd(KERN_DEBUG "Starting sync EP @%p\n", ep);
262
263 ep->sync_slave = subs->data_endpoint;
Daniel Mack015618b2012-08-29 13:17:05 +0200264 err = snd_usb_endpoint_start(ep, can_sleep);
Daniel Mackedcd3632012-04-12 13:51:12 +0200265 if (err < 0) {
266 clear_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags);
267 return err;
268 }
269 }
270
271 return 0;
272}
273
Takashi Iwaia9bb3622012-11-20 18:32:06 +0100274static void stop_endpoints(struct snd_usb_substream *subs, bool wait)
Daniel Mackedcd3632012-04-12 13:51:12 +0200275{
276 if (test_and_clear_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags))
Takashi Iwaib2eb9502012-11-21 08:30:48 +0100277 snd_usb_endpoint_stop(subs->sync_endpoint);
Daniel Mackedcd3632012-04-12 13:51:12 +0200278
279 if (test_and_clear_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags))
Takashi Iwaib2eb9502012-11-21 08:30:48 +0100280 snd_usb_endpoint_stop(subs->data_endpoint);
281
282 if (wait) {
283 snd_usb_endpoint_sync_pending_stop(subs->sync_endpoint);
284 snd_usb_endpoint_sync_pending_stop(subs->data_endpoint);
285 }
Daniel Mackedcd3632012-04-12 13:51:12 +0200286}
287
Daniel Mackedcd3632012-04-12 13:51:12 +0200288static int deactivate_endpoints(struct snd_usb_substream *subs)
289{
290 int reta, retb;
291
292 reta = snd_usb_endpoint_deactivate(subs->sync_endpoint);
293 retb = snd_usb_endpoint_deactivate(subs->data_endpoint);
294
295 if (reta < 0)
296 return reta;
297
298 if (retb < 0)
299 return retb;
300
301 return 0;
302}
303
Daniel Macke5779992010-03-04 19:46:13 +0100304/*
305 * find a matching format and set up the interface
306 */
307static int set_format(struct snd_usb_substream *subs, struct audioformat *fmt)
308{
309 struct usb_device *dev = subs->dev;
310 struct usb_host_interface *alts;
311 struct usb_interface_descriptor *altsd;
312 struct usb_interface *iface;
313 unsigned int ep, attr;
314 int is_playback = subs->direction == SNDRV_PCM_STREAM_PLAYBACK;
Daniel Mackc75a8a72012-04-12 13:51:14 +0200315 int err, implicit_fb = 0;
Daniel Macke5779992010-03-04 19:46:13 +0100316
317 iface = usb_ifnum_to_if(dev, fmt->iface);
318 if (WARN_ON(!iface))
319 return -EINVAL;
320 alts = &iface->altsetting[fmt->altset_idx];
321 altsd = get_iface_desc(alts);
322 if (WARN_ON(altsd->bAlternateSetting != fmt->altsetting))
323 return -EINVAL;
324
325 if (fmt == subs->cur_audiofmt)
326 return 0;
327
Daniel Mack68e67f42012-07-12 13:08:40 +0200328 /* close the old interface */
329 if (subs->interface >= 0 && subs->interface != fmt->iface) {
330 err = usb_set_interface(subs->dev, subs->interface, 0);
331 if (err < 0) {
332 snd_printk(KERN_ERR "%d:%d:%d: return to setting 0 failed (%d)\n",
333 dev->devnum, fmt->iface, fmt->altsetting, err);
334 return -EIO;
335 }
336 subs->interface = -1;
337 subs->altset_idx = 0;
338 }
339
340 /* set interface */
341 if (subs->interface != fmt->iface ||
342 subs->altset_idx != fmt->altset_idx) {
343 err = usb_set_interface(dev, fmt->iface, fmt->altsetting);
344 if (err < 0) {
345 snd_printk(KERN_ERR "%d:%d:%d: usb_set_interface failed (%d)\n",
346 dev->devnum, fmt->iface, fmt->altsetting, err);
347 return -EIO;
348 }
349 snd_printdd(KERN_INFO "setting usb interface %d:%d\n",
350 fmt->iface, fmt->altsetting);
351 subs->interface = fmt->iface;
352 subs->altset_idx = fmt->altset_idx;
353 }
354
Daniel Mackedcd3632012-04-12 13:51:12 +0200355 subs->data_endpoint = snd_usb_add_endpoint(subs->stream->chip,
356 alts, fmt->endpoint, subs->direction,
357 SND_USB_ENDPOINT_TYPE_DATA);
358 if (!subs->data_endpoint)
359 return -EINVAL;
Daniel Macke5779992010-03-04 19:46:13 +0100360
361 /* we need a sync pipe in async OUT or adaptive IN mode */
362 /* check the number of EP, since some devices have broken
363 * descriptors which fool us. if it has only one EP,
364 * assume it as adaptive-out or sync-in.
365 */
366 attr = fmt->ep_attr & USB_ENDPOINT_SYNCTYPE;
Daniel Mackc75a8a72012-04-12 13:51:14 +0200367
368 switch (subs->stream->chip->usb_id) {
Eldad Zackca10a7e2012-11-28 23:55:41 +0100369 case USB_ID(0x0763, 0x2030): /* M-Audio Fast Track C400 */
370 if (is_playback) {
371 implicit_fb = 1;
372 ep = 0x81;
373 iface = usb_ifnum_to_if(dev, 3);
374
375 if (!iface || iface->num_altsetting == 0)
376 return -EINVAL;
377
378 alts = &iface->altsetting[1];
379 goto add_sync_ep;
380 }
381 break;
Daniel Mackc75a8a72012-04-12 13:51:14 +0200382 case USB_ID(0x0763, 0x2080): /* M-Audio FastTrack Ultra */
383 case USB_ID(0x0763, 0x2081):
384 if (is_playback) {
385 implicit_fb = 1;
Daniel Mackedcd3632012-04-12 13:51:12 +0200386 ep = 0x81;
387 iface = usb_ifnum_to_if(dev, 2);
Daniel Mackc75a8a72012-04-12 13:51:14 +0200388
389 if (!iface || iface->num_altsetting == 0)
390 return -EINVAL;
391
Daniel Mackedcd3632012-04-12 13:51:12 +0200392 alts = &iface->altsetting[1];
393 goto add_sync_ep;
394 }
Daniel Mackc75a8a72012-04-12 13:51:14 +0200395 }
Daniel Mackedcd3632012-04-12 13:51:12 +0200396
Daniel Mackc75a8a72012-04-12 13:51:14 +0200397 if (((is_playback && attr == USB_ENDPOINT_SYNC_ASYNC) ||
398 (!is_playback && attr == USB_ENDPOINT_SYNC_ADAPTIVE)) &&
399 altsd->bNumEndpoints >= 2) {
Daniel Macke5779992010-03-04 19:46:13 +0100400 /* check sync-pipe endpoint */
401 /* ... and check descriptor size before accessing bSynchAddress
402 because there is a version of the SB Audigy 2 NX firmware lacking
403 the audio fields in the endpoint descriptors */
Eldad Zackfde854b2012-11-28 23:55:32 +0100404 if ((get_endpoint(alts, 1)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_ISOC ||
Daniel Macke5779992010-03-04 19:46:13 +0100405 (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
Daniel Mackc75a8a72012-04-12 13:51:14 +0200406 get_endpoint(alts, 1)->bSynchAddress != 0 &&
407 !implicit_fb)) {
Daniel Mack7fb75db2012-06-17 13:44:27 +0200408 snd_printk(KERN_ERR "%d:%d:%d : invalid sync pipe. bmAttributes %02x, bLength %d, bSynchAddress %02x\n",
409 dev->devnum, fmt->iface, fmt->altsetting,
410 get_endpoint(alts, 1)->bmAttributes,
411 get_endpoint(alts, 1)->bLength,
412 get_endpoint(alts, 1)->bSynchAddress);
Daniel Macke5779992010-03-04 19:46:13 +0100413 return -EINVAL;
414 }
415 ep = get_endpoint(alts, 1)->bEndpointAddress;
Daniel Mack7fb75db2012-06-17 13:44:27 +0200416 if (!implicit_fb &&
417 get_endpoint(alts, 0)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
Daniel Macke5779992010-03-04 19:46:13 +0100418 (( is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress | USB_DIR_IN)) ||
Daniel Mack7fb75db2012-06-17 13:44:27 +0200419 (!is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress & ~USB_DIR_IN)))) {
420 snd_printk(KERN_ERR "%d:%d:%d : invalid sync pipe. is_playback %d, ep %02x, bSynchAddress %02x\n",
421 dev->devnum, fmt->iface, fmt->altsetting,
422 is_playback, ep, get_endpoint(alts, 0)->bSynchAddress);
Daniel Macke5779992010-03-04 19:46:13 +0100423 return -EINVAL;
424 }
Daniel Mackc75a8a72012-04-12 13:51:14 +0200425
426 implicit_fb = (get_endpoint(alts, 1)->bmAttributes & USB_ENDPOINT_USAGE_MASK)
427 == USB_ENDPOINT_USAGE_IMPLICIT_FB;
428
Daniel Mackedcd3632012-04-12 13:51:12 +0200429add_sync_ep:
430 subs->sync_endpoint = snd_usb_add_endpoint(subs->stream->chip,
431 alts, ep, !subs->direction,
Daniel Mackc75a8a72012-04-12 13:51:14 +0200432 implicit_fb ?
433 SND_USB_ENDPOINT_TYPE_DATA :
434 SND_USB_ENDPOINT_TYPE_SYNC);
Daniel Mackedcd3632012-04-12 13:51:12 +0200435 if (!subs->sync_endpoint)
436 return -EINVAL;
437
438 subs->data_endpoint->sync_master = subs->sync_endpoint;
439 }
Daniel Macke5779992010-03-04 19:46:13 +0100440
Takashi Iwai9e9b5942012-07-06 08:11:43 +0200441 if ((err = snd_usb_init_pitch(subs->stream->chip, fmt->iface, alts, fmt)) < 0)
Daniel Macke5779992010-03-04 19:46:13 +0100442 return err;
443
444 subs->cur_audiofmt = fmt;
445
446 snd_usb_set_format_quirk(subs, fmt);
447
448#if 0
449 printk(KERN_DEBUG
450 "setting done: format = %d, rate = %d..%d, channels = %d\n",
451 fmt->format, fmt->rate_min, fmt->rate_max, fmt->channels);
452 printk(KERN_DEBUG
453 " datapipe = 0x%0x, syncpipe = 0x%0x\n",
454 subs->datapipe, subs->syncpipe);
455#endif
456
457 return 0;
458}
459
460/*
Eldad Zack0d9741c2012-12-03 20:30:09 +0100461 * Return the score of matching two audioformats.
462 * Veto the audioformat if:
463 * - It has no channels for some reason.
464 * - Requested PCM format is not supported.
465 * - Requested sample rate is not supported.
466 */
467static int match_endpoint_audioformats(struct audioformat *fp,
468 struct audioformat *match, int rate,
469 snd_pcm_format_t pcm_format)
470{
471 int i;
472 int score = 0;
473
474 if (fp->channels < 1) {
475 snd_printdd("%s: (fmt @%p) no channels\n", __func__, fp);
476 return 0;
477 }
478
479 if (!(fp->formats & (1ULL << pcm_format))) {
480 snd_printdd("%s: (fmt @%p) no match for format %d\n", __func__,
481 fp, pcm_format);
482 return 0;
483 }
484
485 for (i = 0; i < fp->nr_rates; i++) {
486 if (fp->rate_table[i] == rate) {
487 score++;
488 break;
489 }
490 }
491 if (!score) {
492 snd_printdd("%s: (fmt @%p) no match for rate %d\n", __func__,
493 fp, rate);
494 return 0;
495 }
496
497 if (fp->channels == match->channels)
498 score++;
499
500 snd_printdd("%s: (fmt @%p) score %d\n", __func__, fp, score);
501
502 return score;
503}
504
505/*
506 * Configure the sync ep using the rate and pcm format of the data ep.
507 */
508static int configure_sync_endpoint(struct snd_usb_substream *subs)
509{
510 int ret;
511 struct audioformat *fp;
512 struct audioformat *sync_fp = NULL;
513 int cur_score = 0;
514 int sync_period_bytes = subs->period_bytes;
515 struct snd_usb_substream *sync_subs =
516 &subs->stream->substream[subs->direction ^ 1];
517
518 /* Try to find the best matching audioformat. */
519 list_for_each_entry(fp, &sync_subs->fmt_list, list) {
520 int score = match_endpoint_audioformats(fp, subs->cur_audiofmt,
521 subs->cur_rate, subs->pcm_format);
522
523 if (score > cur_score) {
524 sync_fp = fp;
525 cur_score = score;
526 }
527 }
528
529 if (unlikely(sync_fp == NULL)) {
530 snd_printk(KERN_ERR "%s: no valid audioformat for sync ep %x found\n",
531 __func__, sync_subs->ep_num);
532 return -EINVAL;
533 }
534
535 /*
536 * Recalculate the period bytes if channel number differ between
537 * data and sync ep audioformat.
538 */
539 if (sync_fp->channels != subs->channels) {
540 sync_period_bytes = (subs->period_bytes / subs->channels) *
541 sync_fp->channels;
542 snd_printdd("%s: adjusted sync ep period bytes (%d -> %d)\n",
543 __func__, subs->period_bytes, sync_period_bytes);
544 }
545
546 ret = snd_usb_endpoint_set_params(subs->sync_endpoint,
547 subs->pcm_format,
548 sync_fp->channels,
549 sync_period_bytes,
550 subs->cur_rate,
551 sync_fp,
552 NULL);
553
554 return ret;
555}
556
557/*
Dylan Reid61a70952012-09-18 09:49:48 -0700558 * configure endpoint params
559 *
560 * called during initial setup and upon resume
561 */
562static int configure_endpoint(struct snd_usb_substream *subs)
563{
564 int ret;
565
Dylan Reid61a70952012-09-18 09:49:48 -0700566 /* format changed */
Takashi Iwaib0db6062012-11-21 08:35:42 +0100567 stop_endpoints(subs, true);
Dylan Reid61a70952012-09-18 09:49:48 -0700568 ret = snd_usb_endpoint_set_params(subs->data_endpoint,
569 subs->pcm_format,
570 subs->channels,
571 subs->period_bytes,
572 subs->cur_rate,
573 subs->cur_audiofmt,
574 subs->sync_endpoint);
575 if (ret < 0)
Takashi Iwai978520b2012-10-12 15:12:55 +0200576 return ret;
Dylan Reid61a70952012-09-18 09:49:48 -0700577
578 if (subs->sync_endpoint)
Eldad Zack0d9741c2012-12-03 20:30:09 +0100579 ret = configure_sync_endpoint(subs);
580
Dylan Reid61a70952012-09-18 09:49:48 -0700581 return ret;
582}
583
584/*
Daniel Macke5779992010-03-04 19:46:13 +0100585 * hw_params callback
586 *
587 * allocate a buffer and set the given audio format.
588 *
589 * so far we use a physically linear buffer although packetize transfer
590 * doesn't need a continuous area.
591 * if sg buffer is supported on the later version of alsa, we'll follow
592 * that.
593 */
594static int snd_usb_hw_params(struct snd_pcm_substream *substream,
595 struct snd_pcm_hw_params *hw_params)
596{
597 struct snd_usb_substream *subs = substream->runtime->private_data;
598 struct audioformat *fmt;
Dylan Reid61a70952012-09-18 09:49:48 -0700599 int ret;
Daniel Macke5779992010-03-04 19:46:13 +0100600
601 ret = snd_pcm_lib_alloc_vmalloc_buffer(substream,
602 params_buffer_bytes(hw_params));
603 if (ret < 0)
604 return ret;
605
Dylan Reid61a70952012-09-18 09:49:48 -0700606 subs->pcm_format = params_format(hw_params);
607 subs->period_bytes = params_period_bytes(hw_params);
608 subs->channels = params_channels(hw_params);
609 subs->cur_rate = params_rate(hw_params);
610
611 fmt = find_format(subs);
Daniel Macke5779992010-03-04 19:46:13 +0100612 if (!fmt) {
613 snd_printd(KERN_DEBUG "cannot set format: format = %#x, rate = %d, channels = %d\n",
Dylan Reid61a70952012-09-18 09:49:48 -0700614 subs->pcm_format, subs->cur_rate, subs->channels);
Daniel Macke5779992010-03-04 19:46:13 +0100615 return -EINVAL;
616 }
617
Takashi Iwai34f3c892012-10-15 12:16:02 +0200618 down_read(&subs->stream->chip->shutdown_rwsem);
Takashi Iwai978520b2012-10-12 15:12:55 +0200619 if (subs->stream->chip->shutdown)
620 ret = -ENODEV;
621 else
622 ret = set_format(subs, fmt);
Takashi Iwai34f3c892012-10-15 12:16:02 +0200623 up_read(&subs->stream->chip->shutdown_rwsem);
Takashi Iwai978520b2012-10-12 15:12:55 +0200624 if (ret < 0)
Daniel Macke5779992010-03-04 19:46:13 +0100625 return ret;
626
Dylan Reid61a70952012-09-18 09:49:48 -0700627 subs->interface = fmt->iface;
628 subs->altset_idx = fmt->altset_idx;
Takashi Iwai384dc0852012-09-18 14:49:31 +0200629 subs->need_setup_ep = true;
Daniel Macke5779992010-03-04 19:46:13 +0100630
Dylan Reid61a70952012-09-18 09:49:48 -0700631 return 0;
Daniel Macke5779992010-03-04 19:46:13 +0100632}
633
634/*
635 * hw_free callback
636 *
637 * reset the audio format and release the buffer
638 */
639static int snd_usb_hw_free(struct snd_pcm_substream *substream)
640{
641 struct snd_usb_substream *subs = substream->runtime->private_data;
642
643 subs->cur_audiofmt = NULL;
644 subs->cur_rate = 0;
645 subs->period_bytes = 0;
Takashi Iwai34f3c892012-10-15 12:16:02 +0200646 down_read(&subs->stream->chip->shutdown_rwsem);
Takashi Iwai978520b2012-10-12 15:12:55 +0200647 if (!subs->stream->chip->shutdown) {
Takashi Iwaia9bb3622012-11-20 18:32:06 +0100648 stop_endpoints(subs, true);
Takashi Iwai978520b2012-10-12 15:12:55 +0200649 deactivate_endpoints(subs);
650 }
Takashi Iwai34f3c892012-10-15 12:16:02 +0200651 up_read(&subs->stream->chip->shutdown_rwsem);
Daniel Macke5779992010-03-04 19:46:13 +0100652 return snd_pcm_lib_free_vmalloc_buffer(substream);
653}
654
655/*
656 * prepare callback
657 *
658 * only a few subtle things...
659 */
660static int snd_usb_pcm_prepare(struct snd_pcm_substream *substream)
661{
662 struct snd_pcm_runtime *runtime = substream->runtime;
663 struct snd_usb_substream *subs = runtime->private_data;
Dylan Reid61a70952012-09-18 09:49:48 -0700664 struct usb_host_interface *alts;
665 struct usb_interface *iface;
666 int ret;
Daniel Macke5779992010-03-04 19:46:13 +0100667
668 if (! subs->cur_audiofmt) {
669 snd_printk(KERN_ERR "usbaudio: no format is specified!\n");
670 return -ENXIO;
671 }
672
Takashi Iwai34f3c892012-10-15 12:16:02 +0200673 down_read(&subs->stream->chip->shutdown_rwsem);
Takashi Iwai978520b2012-10-12 15:12:55 +0200674 if (subs->stream->chip->shutdown) {
675 ret = -ENODEV;
676 goto unlock;
677 }
678 if (snd_BUG_ON(!subs->data_endpoint)) {
679 ret = -EIO;
680 goto unlock;
681 }
Daniel Mackedcd3632012-04-12 13:51:12 +0200682
Takashi Iwaif58161b2012-11-08 08:52:45 +0100683 snd_usb_endpoint_sync_pending_stop(subs->sync_endpoint);
684 snd_usb_endpoint_sync_pending_stop(subs->data_endpoint);
685
Dylan Reid61a70952012-09-18 09:49:48 -0700686 ret = set_format(subs, subs->cur_audiofmt);
687 if (ret < 0)
Takashi Iwai978520b2012-10-12 15:12:55 +0200688 goto unlock;
Dylan Reid61a70952012-09-18 09:49:48 -0700689
690 iface = usb_ifnum_to_if(subs->dev, subs->cur_audiofmt->iface);
691 alts = &iface->altsetting[subs->cur_audiofmt->altset_idx];
692 ret = snd_usb_init_sample_rate(subs->stream->chip,
693 subs->cur_audiofmt->iface,
694 alts,
695 subs->cur_audiofmt,
696 subs->cur_rate);
697 if (ret < 0)
Takashi Iwai978520b2012-10-12 15:12:55 +0200698 goto unlock;
Dylan Reid61a70952012-09-18 09:49:48 -0700699
Takashi Iwai384dc0852012-09-18 14:49:31 +0200700 if (subs->need_setup_ep) {
701 ret = configure_endpoint(subs);
702 if (ret < 0)
Takashi Iwai978520b2012-10-12 15:12:55 +0200703 goto unlock;
Takashi Iwai384dc0852012-09-18 14:49:31 +0200704 subs->need_setup_ep = false;
705 }
Dylan Reid61a70952012-09-18 09:49:48 -0700706
Daniel Macke5779992010-03-04 19:46:13 +0100707 /* some unit conversions in runtime */
Daniel Mackedcd3632012-04-12 13:51:12 +0200708 subs->data_endpoint->maxframesize =
709 bytes_to_frames(runtime, subs->data_endpoint->maxpacksize);
710 subs->data_endpoint->curframesize =
711 bytes_to_frames(runtime, subs->data_endpoint->curpacksize);
Daniel Macke5779992010-03-04 19:46:13 +0100712
713 /* reset the pointer */
714 subs->hwptr_done = 0;
715 subs->transfer_done = 0;
Pierre-Louis Bossart294c4fb2011-09-06 19:15:34 -0500716 subs->last_delay = 0;
717 subs->last_frame_number = 0;
Daniel Macke5779992010-03-04 19:46:13 +0100718 runtime->delay = 0;
719
Daniel Mackedcd3632012-04-12 13:51:12 +0200720 /* for playback, submit the URBs now; otherwise, the first hwptr_done
721 * updates for all URBs would happen at the same time when starting */
722 if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK)
Takashi Iwaia9bb3622012-11-20 18:32:06 +0100723 ret = start_endpoints(subs, true);
Daniel Mackedcd3632012-04-12 13:51:12 +0200724
Takashi Iwai978520b2012-10-12 15:12:55 +0200725 unlock:
Takashi Iwai34f3c892012-10-15 12:16:02 +0200726 up_read(&subs->stream->chip->shutdown_rwsem);
Takashi Iwai978520b2012-10-12 15:12:55 +0200727 return ret;
Daniel Macke5779992010-03-04 19:46:13 +0100728}
729
730static struct snd_pcm_hardware snd_usb_hardware =
731{
732 .info = SNDRV_PCM_INFO_MMAP |
733 SNDRV_PCM_INFO_MMAP_VALID |
734 SNDRV_PCM_INFO_BATCH |
735 SNDRV_PCM_INFO_INTERLEAVED |
736 SNDRV_PCM_INFO_BLOCK_TRANSFER |
737 SNDRV_PCM_INFO_PAUSE,
738 .buffer_bytes_max = 1024 * 1024,
739 .period_bytes_min = 64,
740 .period_bytes_max = 512 * 1024,
741 .periods_min = 2,
742 .periods_max = 1024,
743};
744
745static int hw_check_valid_format(struct snd_usb_substream *subs,
746 struct snd_pcm_hw_params *params,
747 struct audioformat *fp)
748{
749 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
750 struct snd_interval *ct = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
751 struct snd_mask *fmts = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
752 struct snd_interval *pt = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
Clemens Ladisch015eb0b2010-03-04 19:46:15 +0100753 struct snd_mask check_fmts;
Daniel Macke5779992010-03-04 19:46:13 +0100754 unsigned int ptime;
755
756 /* check the format */
Clemens Ladisch015eb0b2010-03-04 19:46:15 +0100757 snd_mask_none(&check_fmts);
758 check_fmts.bits[0] = (u32)fp->formats;
759 check_fmts.bits[1] = (u32)(fp->formats >> 32);
760 snd_mask_intersect(&check_fmts, fmts);
761 if (snd_mask_empty(&check_fmts)) {
Daniel Macke5779992010-03-04 19:46:13 +0100762 hwc_debug(" > check: no supported format %d\n", fp->format);
763 return 0;
764 }
765 /* check the channels */
766 if (fp->channels < ct->min || fp->channels > ct->max) {
767 hwc_debug(" > check: no valid channels %d (%d/%d)\n", fp->channels, ct->min, ct->max);
768 return 0;
769 }
770 /* check the rate is within the range */
771 if (fp->rate_min > it->max || (fp->rate_min == it->max && it->openmax)) {
772 hwc_debug(" > check: rate_min %d > max %d\n", fp->rate_min, it->max);
773 return 0;
774 }
775 if (fp->rate_max < it->min || (fp->rate_max == it->min && it->openmin)) {
776 hwc_debug(" > check: rate_max %d < min %d\n", fp->rate_max, it->min);
777 return 0;
778 }
779 /* check whether the period time is >= the data packet interval */
Takashi Iwai978520b2012-10-12 15:12:55 +0200780 if (subs->speed != USB_SPEED_FULL) {
Daniel Macke5779992010-03-04 19:46:13 +0100781 ptime = 125 * (1 << fp->datainterval);
782 if (ptime > pt->max || (ptime == pt->max && pt->openmax)) {
783 hwc_debug(" > check: ptime %u > max %u\n", ptime, pt->max);
784 return 0;
785 }
786 }
787 return 1;
788}
789
790static int hw_rule_rate(struct snd_pcm_hw_params *params,
791 struct snd_pcm_hw_rule *rule)
792{
793 struct snd_usb_substream *subs = rule->private;
794 struct list_head *p;
795 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
796 unsigned int rmin, rmax;
797 int changed;
798
799 hwc_debug("hw_rule_rate: (%d,%d)\n", it->min, it->max);
800 changed = 0;
801 rmin = rmax = 0;
802 list_for_each(p, &subs->fmt_list) {
803 struct audioformat *fp;
804 fp = list_entry(p, struct audioformat, list);
805 if (!hw_check_valid_format(subs, params, fp))
806 continue;
807 if (changed++) {
808 if (rmin > fp->rate_min)
809 rmin = fp->rate_min;
810 if (rmax < fp->rate_max)
811 rmax = fp->rate_max;
812 } else {
813 rmin = fp->rate_min;
814 rmax = fp->rate_max;
815 }
816 }
817
818 if (!changed) {
819 hwc_debug(" --> get empty\n");
820 it->empty = 1;
821 return -EINVAL;
822 }
823
824 changed = 0;
825 if (it->min < rmin) {
826 it->min = rmin;
827 it->openmin = 0;
828 changed = 1;
829 }
830 if (it->max > rmax) {
831 it->max = rmax;
832 it->openmax = 0;
833 changed = 1;
834 }
835 if (snd_interval_checkempty(it)) {
836 it->empty = 1;
837 return -EINVAL;
838 }
839 hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
840 return changed;
841}
842
843
844static int hw_rule_channels(struct snd_pcm_hw_params *params,
845 struct snd_pcm_hw_rule *rule)
846{
847 struct snd_usb_substream *subs = rule->private;
848 struct list_head *p;
849 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
850 unsigned int rmin, rmax;
851 int changed;
852
853 hwc_debug("hw_rule_channels: (%d,%d)\n", it->min, it->max);
854 changed = 0;
855 rmin = rmax = 0;
856 list_for_each(p, &subs->fmt_list) {
857 struct audioformat *fp;
858 fp = list_entry(p, struct audioformat, list);
859 if (!hw_check_valid_format(subs, params, fp))
860 continue;
861 if (changed++) {
862 if (rmin > fp->channels)
863 rmin = fp->channels;
864 if (rmax < fp->channels)
865 rmax = fp->channels;
866 } else {
867 rmin = fp->channels;
868 rmax = fp->channels;
869 }
870 }
871
872 if (!changed) {
873 hwc_debug(" --> get empty\n");
874 it->empty = 1;
875 return -EINVAL;
876 }
877
878 changed = 0;
879 if (it->min < rmin) {
880 it->min = rmin;
881 it->openmin = 0;
882 changed = 1;
883 }
884 if (it->max > rmax) {
885 it->max = rmax;
886 it->openmax = 0;
887 changed = 1;
888 }
889 if (snd_interval_checkempty(it)) {
890 it->empty = 1;
891 return -EINVAL;
892 }
893 hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
894 return changed;
895}
896
897static int hw_rule_format(struct snd_pcm_hw_params *params,
898 struct snd_pcm_hw_rule *rule)
899{
900 struct snd_usb_substream *subs = rule->private;
901 struct list_head *p;
902 struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
903 u64 fbits;
904 u32 oldbits[2];
905 int changed;
906
907 hwc_debug("hw_rule_format: %x:%x\n", fmt->bits[0], fmt->bits[1]);
908 fbits = 0;
909 list_for_each(p, &subs->fmt_list) {
910 struct audioformat *fp;
911 fp = list_entry(p, struct audioformat, list);
912 if (!hw_check_valid_format(subs, params, fp))
913 continue;
Clemens Ladisch015eb0b2010-03-04 19:46:15 +0100914 fbits |= fp->formats;
Daniel Macke5779992010-03-04 19:46:13 +0100915 }
916
917 oldbits[0] = fmt->bits[0];
918 oldbits[1] = fmt->bits[1];
919 fmt->bits[0] &= (u32)fbits;
920 fmt->bits[1] &= (u32)(fbits >> 32);
921 if (!fmt->bits[0] && !fmt->bits[1]) {
922 hwc_debug(" --> get empty\n");
923 return -EINVAL;
924 }
925 changed = (oldbits[0] != fmt->bits[0] || oldbits[1] != fmt->bits[1]);
926 hwc_debug(" --> %x:%x (changed = %d)\n", fmt->bits[0], fmt->bits[1], changed);
927 return changed;
928}
929
930static int hw_rule_period_time(struct snd_pcm_hw_params *params,
931 struct snd_pcm_hw_rule *rule)
932{
933 struct snd_usb_substream *subs = rule->private;
934 struct audioformat *fp;
935 struct snd_interval *it;
936 unsigned char min_datainterval;
937 unsigned int pmin;
938 int changed;
939
940 it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
941 hwc_debug("hw_rule_period_time: (%u,%u)\n", it->min, it->max);
942 min_datainterval = 0xff;
943 list_for_each_entry(fp, &subs->fmt_list, list) {
944 if (!hw_check_valid_format(subs, params, fp))
945 continue;
946 min_datainterval = min(min_datainterval, fp->datainterval);
947 }
948 if (min_datainterval == 0xff) {
Uwe Kleine-Königa7ce2e02010-07-12 17:15:44 +0200949 hwc_debug(" --> get empty\n");
Daniel Macke5779992010-03-04 19:46:13 +0100950 it->empty = 1;
951 return -EINVAL;
952 }
953 pmin = 125 * (1 << min_datainterval);
954 changed = 0;
955 if (it->min < pmin) {
956 it->min = pmin;
957 it->openmin = 0;
958 changed = 1;
959 }
960 if (snd_interval_checkempty(it)) {
961 it->empty = 1;
962 return -EINVAL;
963 }
964 hwc_debug(" --> (%u,%u) (changed = %d)\n", it->min, it->max, changed);
965 return changed;
966}
967
968/*
969 * If the device supports unusual bit rates, does the request meet these?
970 */
971static int snd_usb_pcm_check_knot(struct snd_pcm_runtime *runtime,
972 struct snd_usb_substream *subs)
973{
974 struct audioformat *fp;
Takashi Iwai0717d0f2012-03-15 16:14:38 +0100975 int *rate_list;
Daniel Macke5779992010-03-04 19:46:13 +0100976 int count = 0, needs_knot = 0;
977 int err;
978
Clemens Ladisch5cd5d7c2012-05-18 18:00:43 +0200979 kfree(subs->rate_list.list);
980 subs->rate_list.list = NULL;
981
Daniel Macke5779992010-03-04 19:46:13 +0100982 list_for_each_entry(fp, &subs->fmt_list, list) {
983 if (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)
984 return 0;
985 count += fp->nr_rates;
986 if (fp->rates & SNDRV_PCM_RATE_KNOT)
987 needs_knot = 1;
988 }
989 if (!needs_knot)
990 return 0;
991
Takashi Iwai0717d0f2012-03-15 16:14:38 +0100992 subs->rate_list.list = rate_list =
993 kmalloc(sizeof(int) * count, GFP_KERNEL);
Jesper Juhl8a8d56b2010-10-29 20:40:23 +0200994 if (!subs->rate_list.list)
995 return -ENOMEM;
996 subs->rate_list.count = count;
Daniel Macke5779992010-03-04 19:46:13 +0100997 subs->rate_list.mask = 0;
998 count = 0;
999 list_for_each_entry(fp, &subs->fmt_list, list) {
1000 int i;
1001 for (i = 0; i < fp->nr_rates; i++)
Takashi Iwai0717d0f2012-03-15 16:14:38 +01001002 rate_list[count++] = fp->rate_table[i];
Daniel Macke5779992010-03-04 19:46:13 +01001003 }
1004 err = snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1005 &subs->rate_list);
1006 if (err < 0)
1007 return err;
1008
1009 return 0;
1010}
1011
1012
1013/*
1014 * set up the runtime hardware information.
1015 */
1016
1017static int setup_hw_info(struct snd_pcm_runtime *runtime, struct snd_usb_substream *subs)
1018{
1019 struct list_head *p;
1020 unsigned int pt, ptmin;
1021 int param_period_time_if_needed;
1022 int err;
1023
1024 runtime->hw.formats = subs->formats;
1025
1026 runtime->hw.rate_min = 0x7fffffff;
1027 runtime->hw.rate_max = 0;
1028 runtime->hw.channels_min = 256;
1029 runtime->hw.channels_max = 0;
1030 runtime->hw.rates = 0;
1031 ptmin = UINT_MAX;
1032 /* check min/max rates and channels */
1033 list_for_each(p, &subs->fmt_list) {
1034 struct audioformat *fp;
1035 fp = list_entry(p, struct audioformat, list);
1036 runtime->hw.rates |= fp->rates;
1037 if (runtime->hw.rate_min > fp->rate_min)
1038 runtime->hw.rate_min = fp->rate_min;
1039 if (runtime->hw.rate_max < fp->rate_max)
1040 runtime->hw.rate_max = fp->rate_max;
1041 if (runtime->hw.channels_min > fp->channels)
1042 runtime->hw.channels_min = fp->channels;
1043 if (runtime->hw.channels_max < fp->channels)
1044 runtime->hw.channels_max = fp->channels;
1045 if (fp->fmt_type == UAC_FORMAT_TYPE_II && fp->frame_size > 0) {
1046 /* FIXME: there might be more than one audio formats... */
1047 runtime->hw.period_bytes_min = runtime->hw.period_bytes_max =
1048 fp->frame_size;
1049 }
1050 pt = 125 * (1 << fp->datainterval);
1051 ptmin = min(ptmin, pt);
1052 }
Oliver Neukum88a85162011-03-11 14:51:12 +01001053 err = snd_usb_autoresume(subs->stream->chip);
1054 if (err < 0)
1055 return err;
Daniel Macke5779992010-03-04 19:46:13 +01001056
1057 param_period_time_if_needed = SNDRV_PCM_HW_PARAM_PERIOD_TIME;
Takashi Iwai978520b2012-10-12 15:12:55 +02001058 if (subs->speed == USB_SPEED_FULL)
Daniel Macke5779992010-03-04 19:46:13 +01001059 /* full speed devices have fixed data packet interval */
1060 ptmin = 1000;
1061 if (ptmin == 1000)
1062 /* if period time doesn't go below 1 ms, no rules needed */
1063 param_period_time_if_needed = -1;
1064 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1065 ptmin, UINT_MAX);
1066
1067 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1068 hw_rule_rate, subs,
1069 SNDRV_PCM_HW_PARAM_FORMAT,
1070 SNDRV_PCM_HW_PARAM_CHANNELS,
1071 param_period_time_if_needed,
1072 -1)) < 0)
Oliver Neukum88a85162011-03-11 14:51:12 +01001073 goto rep_err;
Daniel Macke5779992010-03-04 19:46:13 +01001074 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
1075 hw_rule_channels, subs,
1076 SNDRV_PCM_HW_PARAM_FORMAT,
1077 SNDRV_PCM_HW_PARAM_RATE,
1078 param_period_time_if_needed,
1079 -1)) < 0)
Oliver Neukum88a85162011-03-11 14:51:12 +01001080 goto rep_err;
Daniel Macke5779992010-03-04 19:46:13 +01001081 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
1082 hw_rule_format, subs,
1083 SNDRV_PCM_HW_PARAM_RATE,
1084 SNDRV_PCM_HW_PARAM_CHANNELS,
1085 param_period_time_if_needed,
1086 -1)) < 0)
Oliver Neukum88a85162011-03-11 14:51:12 +01001087 goto rep_err;
Daniel Macke5779992010-03-04 19:46:13 +01001088 if (param_period_time_if_needed >= 0) {
1089 err = snd_pcm_hw_rule_add(runtime, 0,
1090 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1091 hw_rule_period_time, subs,
1092 SNDRV_PCM_HW_PARAM_FORMAT,
1093 SNDRV_PCM_HW_PARAM_CHANNELS,
1094 SNDRV_PCM_HW_PARAM_RATE,
1095 -1);
1096 if (err < 0)
Oliver Neukum88a85162011-03-11 14:51:12 +01001097 goto rep_err;
Daniel Macke5779992010-03-04 19:46:13 +01001098 }
1099 if ((err = snd_usb_pcm_check_knot(runtime, subs)) < 0)
Oliver Neukum88a85162011-03-11 14:51:12 +01001100 goto rep_err;
Daniel Macke5779992010-03-04 19:46:13 +01001101 return 0;
Oliver Neukum88a85162011-03-11 14:51:12 +01001102
1103rep_err:
1104 snd_usb_autosuspend(subs->stream->chip);
1105 return err;
Daniel Macke5779992010-03-04 19:46:13 +01001106}
1107
1108static int snd_usb_pcm_open(struct snd_pcm_substream *substream, int direction)
1109{
1110 struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
1111 struct snd_pcm_runtime *runtime = substream->runtime;
1112 struct snd_usb_substream *subs = &as->substream[direction];
1113
1114 subs->interface = -1;
Clemens Ladische11b4e02010-03-04 19:46:14 +01001115 subs->altset_idx = 0;
Daniel Macke5779992010-03-04 19:46:13 +01001116 runtime->hw = snd_usb_hardware;
1117 runtime->private_data = subs;
1118 subs->pcm_substream = substream;
Oliver Neukum88a85162011-03-11 14:51:12 +01001119 /* runtime PM is also done there */
Daniel Macke5779992010-03-04 19:46:13 +01001120 return setup_hw_info(runtime, subs);
1121}
1122
1123static int snd_usb_pcm_close(struct snd_pcm_substream *substream, int direction)
1124{
1125 struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
1126 struct snd_usb_substream *subs = &as->substream[direction];
1127
Takashi Iwaib0db6062012-11-21 08:35:42 +01001128 stop_endpoints(subs, true);
Daniel Mack68e67f42012-07-12 13:08:40 +02001129
1130 if (!as->chip->shutdown && subs->interface >= 0) {
1131 usb_set_interface(subs->dev, subs->interface, 0);
1132 subs->interface = -1;
1133 }
1134
Daniel Macke5779992010-03-04 19:46:13 +01001135 subs->pcm_substream = NULL;
Oliver Neukum88a85162011-03-11 14:51:12 +01001136 snd_usb_autosuspend(subs->stream->chip);
Daniel Mackedcd3632012-04-12 13:51:12 +02001137
Daniel Mack68e67f42012-07-12 13:08:40 +02001138 return 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001139}
1140
1141/* Since a URB can handle only a single linear buffer, we must use double
1142 * buffering when the data to be transferred overflows the buffer boundary.
1143 * To avoid inconsistencies when updating hwptr_done, we use double buffering
1144 * for all URBs.
1145 */
1146static void retire_capture_urb(struct snd_usb_substream *subs,
1147 struct urb *urb)
1148{
1149 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1150 unsigned int stride, frames, bytes, oldptr;
1151 int i, period_elapsed = 0;
1152 unsigned long flags;
1153 unsigned char *cp;
Pierre-Louis Bossarte4cc6152012-12-19 11:39:05 -06001154 int current_frame_number;
1155
1156 /* read frame number here, update pointer in critical section */
1157 current_frame_number = usb_get_current_frame_number(subs->dev);
Daniel Mackedcd3632012-04-12 13:51:12 +02001158
1159 stride = runtime->frame_bits >> 3;
1160
1161 for (i = 0; i < urb->number_of_packets; i++) {
1162 cp = (unsigned char *)urb->transfer_buffer + urb->iso_frame_desc[i].offset;
1163 if (urb->iso_frame_desc[i].status && printk_ratelimit()) {
1164 snd_printdd(KERN_ERR "frame %d active: %d\n", i, urb->iso_frame_desc[i].status);
1165 // continue;
1166 }
1167 bytes = urb->iso_frame_desc[i].actual_length;
1168 frames = bytes / stride;
1169 if (!subs->txfr_quirk)
1170 bytes = frames * stride;
1171 if (bytes % (runtime->sample_bits >> 3) != 0) {
1172#ifdef CONFIG_SND_DEBUG_VERBOSE
1173 int oldbytes = bytes;
1174#endif
1175 bytes = frames * stride;
1176 snd_printdd(KERN_ERR "Corrected urb data len. %d->%d\n",
1177 oldbytes, bytes);
1178 }
1179 /* update the current pointer */
1180 spin_lock_irqsave(&subs->lock, flags);
1181 oldptr = subs->hwptr_done;
1182 subs->hwptr_done += bytes;
1183 if (subs->hwptr_done >= runtime->buffer_size * stride)
1184 subs->hwptr_done -= runtime->buffer_size * stride;
1185 frames = (bytes + (oldptr % stride)) / stride;
1186 subs->transfer_done += frames;
1187 if (subs->transfer_done >= runtime->period_size) {
1188 subs->transfer_done -= runtime->period_size;
1189 period_elapsed = 1;
1190 }
Pierre-Louis Bossarte4cc6152012-12-19 11:39:05 -06001191 /* capture delay is by construction limited to one URB,
1192 * reset delays here
1193 */
1194 runtime->delay = subs->last_delay = 0;
1195
1196 /* realign last_frame_number */
1197 subs->last_frame_number = current_frame_number;
1198 subs->last_frame_number &= 0xFF; /* keep 8 LSBs */
1199
Daniel Mackedcd3632012-04-12 13:51:12 +02001200 spin_unlock_irqrestore(&subs->lock, flags);
1201 /* copy a data chunk */
1202 if (oldptr + bytes > runtime->buffer_size * stride) {
1203 unsigned int bytes1 =
1204 runtime->buffer_size * stride - oldptr;
1205 memcpy(runtime->dma_area + oldptr, cp, bytes1);
1206 memcpy(runtime->dma_area, cp + bytes1, bytes - bytes1);
1207 } else {
1208 memcpy(runtime->dma_area + oldptr, cp, bytes);
1209 }
1210 }
1211
1212 if (period_elapsed)
1213 snd_pcm_period_elapsed(subs->pcm_substream);
1214}
1215
1216static void prepare_playback_urb(struct snd_usb_substream *subs,
1217 struct urb *urb)
1218{
1219 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
Daniel Mack245baf92012-08-30 18:52:30 +02001220 struct snd_usb_endpoint *ep = subs->data_endpoint;
Daniel Mackedcd3632012-04-12 13:51:12 +02001221 struct snd_urb_ctx *ctx = urb->context;
1222 unsigned int counts, frames, bytes;
1223 int i, stride, period_elapsed = 0;
1224 unsigned long flags;
1225
1226 stride = runtime->frame_bits >> 3;
1227
1228 frames = 0;
1229 urb->number_of_packets = 0;
1230 spin_lock_irqsave(&subs->lock, flags);
1231 for (i = 0; i < ctx->packets; i++) {
Daniel Mack245baf92012-08-30 18:52:30 +02001232 if (ctx->packet_size[i])
1233 counts = ctx->packet_size[i];
1234 else
1235 counts = snd_usb_endpoint_next_packet_size(ep);
1236
Daniel Mackedcd3632012-04-12 13:51:12 +02001237 /* set up descriptor */
1238 urb->iso_frame_desc[i].offset = frames * stride;
1239 urb->iso_frame_desc[i].length = counts * stride;
1240 frames += counts;
1241 urb->number_of_packets++;
1242 subs->transfer_done += counts;
1243 if (subs->transfer_done >= runtime->period_size) {
1244 subs->transfer_done -= runtime->period_size;
1245 period_elapsed = 1;
1246 if (subs->fmt_type == UAC_FORMAT_TYPE_II) {
1247 if (subs->transfer_done > 0) {
1248 /* FIXME: fill-max mode is not
1249 * supported yet */
1250 frames -= subs->transfer_done;
1251 counts -= subs->transfer_done;
1252 urb->iso_frame_desc[i].length =
1253 counts * stride;
1254 subs->transfer_done = 0;
1255 }
1256 i++;
1257 if (i < ctx->packets) {
1258 /* add a transfer delimiter */
1259 urb->iso_frame_desc[i].offset =
1260 frames * stride;
1261 urb->iso_frame_desc[i].length = 0;
1262 urb->number_of_packets++;
1263 }
1264 break;
1265 }
1266 }
1267 if (period_elapsed &&
1268 !snd_usb_endpoint_implict_feedback_sink(subs->data_endpoint)) /* finish at the period boundary */
1269 break;
1270 }
1271 bytes = frames * stride;
1272 if (subs->hwptr_done + bytes > runtime->buffer_size * stride) {
1273 /* err, the transferred area goes over buffer boundary. */
1274 unsigned int bytes1 =
1275 runtime->buffer_size * stride - subs->hwptr_done;
1276 memcpy(urb->transfer_buffer,
1277 runtime->dma_area + subs->hwptr_done, bytes1);
1278 memcpy(urb->transfer_buffer + bytes1,
1279 runtime->dma_area, bytes - bytes1);
1280 } else {
1281 memcpy(urb->transfer_buffer,
1282 runtime->dma_area + subs->hwptr_done, bytes);
1283 }
1284 subs->hwptr_done += bytes;
1285 if (subs->hwptr_done >= runtime->buffer_size * stride)
1286 subs->hwptr_done -= runtime->buffer_size * stride;
Daniel Mackfbcfbf52012-08-30 18:52:29 +02001287
1288 /* update delay with exact number of samples queued */
1289 runtime->delay = subs->last_delay;
Daniel Mackedcd3632012-04-12 13:51:12 +02001290 runtime->delay += frames;
Daniel Mackfbcfbf52012-08-30 18:52:29 +02001291 subs->last_delay = runtime->delay;
1292
1293 /* realign last_frame_number */
1294 subs->last_frame_number = usb_get_current_frame_number(subs->dev);
1295 subs->last_frame_number &= 0xFF; /* keep 8 LSBs */
1296
Daniel Mackedcd3632012-04-12 13:51:12 +02001297 spin_unlock_irqrestore(&subs->lock, flags);
1298 urb->transfer_buffer_length = bytes;
1299 if (period_elapsed)
1300 snd_pcm_period_elapsed(subs->pcm_substream);
1301}
1302
1303/*
1304 * process after playback data complete
1305 * - decrease the delay count again
1306 */
1307static void retire_playback_urb(struct snd_usb_substream *subs,
1308 struct urb *urb)
1309{
1310 unsigned long flags;
1311 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1312 int stride = runtime->frame_bits >> 3;
1313 int processed = urb->transfer_buffer_length / stride;
Daniel Mackfbcfbf52012-08-30 18:52:29 +02001314 int est_delay;
Daniel Mackedcd3632012-04-12 13:51:12 +02001315
Takashi Iwai1213a202012-09-06 14:58:00 +02001316 /* ignore the delay accounting when procssed=0 is given, i.e.
1317 * silent payloads are procssed before handling the actual data
1318 */
1319 if (!processed)
1320 return;
1321
Daniel Mackedcd3632012-04-12 13:51:12 +02001322 spin_lock_irqsave(&subs->lock, flags);
Takashi Iwai48779a02012-11-23 16:00:37 +01001323 if (!subs->last_delay)
1324 goto out; /* short path */
1325
Daniel Mackfbcfbf52012-08-30 18:52:29 +02001326 est_delay = snd_usb_pcm_delay(subs, runtime->rate);
1327 /* update delay with exact number of samples played */
1328 if (processed > subs->last_delay)
1329 subs->last_delay = 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001330 else
Daniel Mackfbcfbf52012-08-30 18:52:29 +02001331 subs->last_delay -= processed;
1332 runtime->delay = subs->last_delay;
1333
1334 /*
1335 * Report when delay estimate is off by more than 2ms.
1336 * The error should be lower than 2ms since the estimate relies
1337 * on two reads of a counter updated every ms.
1338 */
1339 if (abs(est_delay - subs->last_delay) * 1000 > runtime->rate * 2)
1340 snd_printk(KERN_DEBUG "delay: estimated %d, actual %d\n",
1341 est_delay, subs->last_delay);
1342
Takashi Iwai48779a02012-11-23 16:00:37 +01001343 if (!subs->running) {
1344 /* update last_frame_number for delay counting here since
1345 * prepare_playback_urb won't be called during pause
1346 */
1347 subs->last_frame_number =
1348 usb_get_current_frame_number(subs->dev) & 0xff;
1349 }
1350
1351 out:
Daniel Mackedcd3632012-04-12 13:51:12 +02001352 spin_unlock_irqrestore(&subs->lock, flags);
Daniel Macke5779992010-03-04 19:46:13 +01001353}
1354
1355static int snd_usb_playback_open(struct snd_pcm_substream *substream)
1356{
1357 return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_PLAYBACK);
1358}
1359
1360static int snd_usb_playback_close(struct snd_pcm_substream *substream)
1361{
1362 return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_PLAYBACK);
1363}
1364
1365static int snd_usb_capture_open(struct snd_pcm_substream *substream)
1366{
1367 return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_CAPTURE);
1368}
1369
1370static int snd_usb_capture_close(struct snd_pcm_substream *substream)
1371{
1372 return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_CAPTURE);
1373}
1374
Daniel Mackedcd3632012-04-12 13:51:12 +02001375static int snd_usb_substream_playback_trigger(struct snd_pcm_substream *substream,
1376 int cmd)
1377{
1378 struct snd_usb_substream *subs = substream->runtime->private_data;
1379
1380 switch (cmd) {
1381 case SNDRV_PCM_TRIGGER_START:
1382 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1383 subs->data_endpoint->prepare_data_urb = prepare_playback_urb;
1384 subs->data_endpoint->retire_data_urb = retire_playback_urb;
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001385 subs->running = 1;
Daniel Mackedcd3632012-04-12 13:51:12 +02001386 return 0;
1387 case SNDRV_PCM_TRIGGER_STOP:
Takashi Iwaia9bb3622012-11-20 18:32:06 +01001388 stop_endpoints(subs, false);
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001389 subs->running = 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001390 return 0;
1391 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1392 subs->data_endpoint->prepare_data_urb = NULL;
Takashi Iwai48779a02012-11-23 16:00:37 +01001393 /* keep retire_data_urb for delay calculation */
1394 subs->data_endpoint->retire_data_urb = retire_playback_urb;
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001395 subs->running = 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001396 return 0;
1397 }
1398
1399 return -EINVAL;
1400}
1401
Daniel Mackafe25962012-06-16 16:58:04 +02001402static int snd_usb_substream_capture_trigger(struct snd_pcm_substream *substream,
1403 int cmd)
Daniel Mackedcd3632012-04-12 13:51:12 +02001404{
1405 int err;
1406 struct snd_usb_substream *subs = substream->runtime->private_data;
1407
1408 switch (cmd) {
1409 case SNDRV_PCM_TRIGGER_START:
Takashi Iwaia9bb3622012-11-20 18:32:06 +01001410 err = start_endpoints(subs, false);
Daniel Mackedcd3632012-04-12 13:51:12 +02001411 if (err < 0)
1412 return err;
1413
1414 subs->data_endpoint->retire_data_urb = retire_capture_urb;
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001415 subs->running = 1;
Daniel Mackedcd3632012-04-12 13:51:12 +02001416 return 0;
1417 case SNDRV_PCM_TRIGGER_STOP:
Takashi Iwaia9bb3622012-11-20 18:32:06 +01001418 stop_endpoints(subs, false);
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001419 subs->running = 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001420 return 0;
1421 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1422 subs->data_endpoint->retire_data_urb = NULL;
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001423 subs->running = 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001424 return 0;
1425 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1426 subs->data_endpoint->retire_data_urb = retire_capture_urb;
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001427 subs->running = 1;
Daniel Mackedcd3632012-04-12 13:51:12 +02001428 return 0;
1429 }
1430
1431 return -EINVAL;
1432}
1433
Daniel Macke5779992010-03-04 19:46:13 +01001434static struct snd_pcm_ops snd_usb_playback_ops = {
1435 .open = snd_usb_playback_open,
1436 .close = snd_usb_playback_close,
1437 .ioctl = snd_pcm_lib_ioctl,
1438 .hw_params = snd_usb_hw_params,
1439 .hw_free = snd_usb_hw_free,
1440 .prepare = snd_usb_pcm_prepare,
1441 .trigger = snd_usb_substream_playback_trigger,
1442 .pointer = snd_usb_pcm_pointer,
1443 .page = snd_pcm_lib_get_vmalloc_page,
1444 .mmap = snd_pcm_lib_mmap_vmalloc,
1445};
1446
1447static struct snd_pcm_ops snd_usb_capture_ops = {
1448 .open = snd_usb_capture_open,
1449 .close = snd_usb_capture_close,
1450 .ioctl = snd_pcm_lib_ioctl,
1451 .hw_params = snd_usb_hw_params,
1452 .hw_free = snd_usb_hw_free,
1453 .prepare = snd_usb_pcm_prepare,
1454 .trigger = snd_usb_substream_capture_trigger,
1455 .pointer = snd_usb_pcm_pointer,
1456 .page = snd_pcm_lib_get_vmalloc_page,
1457 .mmap = snd_pcm_lib_mmap_vmalloc,
1458};
1459
1460void snd_usb_set_pcm_ops(struct snd_pcm *pcm, int stream)
1461{
1462 snd_pcm_set_ops(pcm, stream,
1463 stream == SNDRV_PCM_STREAM_PLAYBACK ?
1464 &snd_usb_playback_ops : &snd_usb_capture_ops);
1465}