blob: c263991b3b9d12a724ba434f6d39940bd618d8f0 [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;
Daniel Mack0959f222013-03-17 20:07:26 +0800353
354 /*
355 * "Playback Design" products need a 50ms delay after setting the
356 * USB interface.
357 */
358 if (le16_to_cpu(dev->descriptor.idVendor) == 0x23ba)
359 mdelay(50);
Daniel Mack68e67f42012-07-12 13:08:40 +0200360 }
361
Daniel Mackedcd3632012-04-12 13:51:12 +0200362 subs->data_endpoint = snd_usb_add_endpoint(subs->stream->chip,
363 alts, fmt->endpoint, subs->direction,
364 SND_USB_ENDPOINT_TYPE_DATA);
365 if (!subs->data_endpoint)
366 return -EINVAL;
Daniel Macke5779992010-03-04 19:46:13 +0100367
368 /* we need a sync pipe in async OUT or adaptive IN mode */
369 /* check the number of EP, since some devices have broken
370 * descriptors which fool us. if it has only one EP,
371 * assume it as adaptive-out or sync-in.
372 */
373 attr = fmt->ep_attr & USB_ENDPOINT_SYNCTYPE;
Daniel Mackc75a8a72012-04-12 13:51:14 +0200374
375 switch (subs->stream->chip->usb_id) {
Eldad Zackca10a7e2012-11-28 23:55:41 +0100376 case USB_ID(0x0763, 0x2030): /* M-Audio Fast Track C400 */
Matt Gruskine9a25e02013-02-09 12:56:35 -0500377 case USB_ID(0x0763, 0x2031): /* M-Audio Fast Track C600 */
Eldad Zackca10a7e2012-11-28 23:55:41 +0100378 if (is_playback) {
379 implicit_fb = 1;
380 ep = 0x81;
381 iface = usb_ifnum_to_if(dev, 3);
382
383 if (!iface || iface->num_altsetting == 0)
384 return -EINVAL;
385
386 alts = &iface->altsetting[1];
387 goto add_sync_ep;
388 }
389 break;
Daniel Mackc75a8a72012-04-12 13:51:14 +0200390 case USB_ID(0x0763, 0x2080): /* M-Audio FastTrack Ultra */
391 case USB_ID(0x0763, 0x2081):
392 if (is_playback) {
393 implicit_fb = 1;
Daniel Mackedcd3632012-04-12 13:51:12 +0200394 ep = 0x81;
395 iface = usb_ifnum_to_if(dev, 2);
Daniel Mackc75a8a72012-04-12 13:51:14 +0200396
397 if (!iface || iface->num_altsetting == 0)
398 return -EINVAL;
399
Daniel Mackedcd3632012-04-12 13:51:12 +0200400 alts = &iface->altsetting[1];
401 goto add_sync_ep;
402 }
Daniel Mackc75a8a72012-04-12 13:51:14 +0200403 }
Daniel Mackedcd3632012-04-12 13:51:12 +0200404
Daniel Mackc75a8a72012-04-12 13:51:14 +0200405 if (((is_playback && attr == USB_ENDPOINT_SYNC_ASYNC) ||
406 (!is_playback && attr == USB_ENDPOINT_SYNC_ADAPTIVE)) &&
407 altsd->bNumEndpoints >= 2) {
Daniel Macke5779992010-03-04 19:46:13 +0100408 /* check sync-pipe endpoint */
409 /* ... and check descriptor size before accessing bSynchAddress
410 because there is a version of the SB Audigy 2 NX firmware lacking
411 the audio fields in the endpoint descriptors */
Eldad Zackfde854b2012-11-28 23:55:32 +0100412 if ((get_endpoint(alts, 1)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_ISOC ||
Daniel Macke5779992010-03-04 19:46:13 +0100413 (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
Daniel Mackc75a8a72012-04-12 13:51:14 +0200414 get_endpoint(alts, 1)->bSynchAddress != 0 &&
415 !implicit_fb)) {
Daniel Mack7fb75db2012-06-17 13:44:27 +0200416 snd_printk(KERN_ERR "%d:%d:%d : invalid sync pipe. bmAttributes %02x, bLength %d, bSynchAddress %02x\n",
417 dev->devnum, fmt->iface, fmt->altsetting,
418 get_endpoint(alts, 1)->bmAttributes,
419 get_endpoint(alts, 1)->bLength,
420 get_endpoint(alts, 1)->bSynchAddress);
Daniel Macke5779992010-03-04 19:46:13 +0100421 return -EINVAL;
422 }
423 ep = get_endpoint(alts, 1)->bEndpointAddress;
Daniel Mack7fb75db2012-06-17 13:44:27 +0200424 if (!implicit_fb &&
425 get_endpoint(alts, 0)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
Daniel Macke5779992010-03-04 19:46:13 +0100426 (( is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress | USB_DIR_IN)) ||
Daniel Mack7fb75db2012-06-17 13:44:27 +0200427 (!is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress & ~USB_DIR_IN)))) {
428 snd_printk(KERN_ERR "%d:%d:%d : invalid sync pipe. is_playback %d, ep %02x, bSynchAddress %02x\n",
429 dev->devnum, fmt->iface, fmt->altsetting,
430 is_playback, ep, get_endpoint(alts, 0)->bSynchAddress);
Daniel Macke5779992010-03-04 19:46:13 +0100431 return -EINVAL;
432 }
Daniel Mackc75a8a72012-04-12 13:51:14 +0200433
434 implicit_fb = (get_endpoint(alts, 1)->bmAttributes & USB_ENDPOINT_USAGE_MASK)
435 == USB_ENDPOINT_USAGE_IMPLICIT_FB;
436
Daniel Mackedcd3632012-04-12 13:51:12 +0200437add_sync_ep:
438 subs->sync_endpoint = snd_usb_add_endpoint(subs->stream->chip,
439 alts, ep, !subs->direction,
Daniel Mackc75a8a72012-04-12 13:51:14 +0200440 implicit_fb ?
441 SND_USB_ENDPOINT_TYPE_DATA :
442 SND_USB_ENDPOINT_TYPE_SYNC);
Daniel Mackedcd3632012-04-12 13:51:12 +0200443 if (!subs->sync_endpoint)
444 return -EINVAL;
445
446 subs->data_endpoint->sync_master = subs->sync_endpoint;
447 }
Daniel Macke5779992010-03-04 19:46:13 +0100448
Takashi Iwai9e9b5942012-07-06 08:11:43 +0200449 if ((err = snd_usb_init_pitch(subs->stream->chip, fmt->iface, alts, fmt)) < 0)
Daniel Macke5779992010-03-04 19:46:13 +0100450 return err;
451
452 subs->cur_audiofmt = fmt;
453
454 snd_usb_set_format_quirk(subs, fmt);
455
456#if 0
457 printk(KERN_DEBUG
458 "setting done: format = %d, rate = %d..%d, channels = %d\n",
459 fmt->format, fmt->rate_min, fmt->rate_max, fmt->channels);
460 printk(KERN_DEBUG
461 " datapipe = 0x%0x, syncpipe = 0x%0x\n",
462 subs->datapipe, subs->syncpipe);
463#endif
464
465 return 0;
466}
467
468/*
Eldad Zack0d9741c2012-12-03 20:30:09 +0100469 * Return the score of matching two audioformats.
470 * Veto the audioformat if:
471 * - It has no channels for some reason.
472 * - Requested PCM format is not supported.
473 * - Requested sample rate is not supported.
474 */
475static int match_endpoint_audioformats(struct audioformat *fp,
476 struct audioformat *match, int rate,
477 snd_pcm_format_t pcm_format)
478{
479 int i;
480 int score = 0;
481
482 if (fp->channels < 1) {
483 snd_printdd("%s: (fmt @%p) no channels\n", __func__, fp);
484 return 0;
485 }
486
487 if (!(fp->formats & (1ULL << pcm_format))) {
488 snd_printdd("%s: (fmt @%p) no match for format %d\n", __func__,
489 fp, pcm_format);
490 return 0;
491 }
492
493 for (i = 0; i < fp->nr_rates; i++) {
494 if (fp->rate_table[i] == rate) {
495 score++;
496 break;
497 }
498 }
499 if (!score) {
500 snd_printdd("%s: (fmt @%p) no match for rate %d\n", __func__,
501 fp, rate);
502 return 0;
503 }
504
505 if (fp->channels == match->channels)
506 score++;
507
508 snd_printdd("%s: (fmt @%p) score %d\n", __func__, fp, score);
509
510 return score;
511}
512
513/*
514 * Configure the sync ep using the rate and pcm format of the data ep.
515 */
516static int configure_sync_endpoint(struct snd_usb_substream *subs)
517{
518 int ret;
519 struct audioformat *fp;
520 struct audioformat *sync_fp = NULL;
521 int cur_score = 0;
522 int sync_period_bytes = subs->period_bytes;
523 struct snd_usb_substream *sync_subs =
524 &subs->stream->substream[subs->direction ^ 1];
525
Takashi Iwai31be5422013-01-10 14:06:38 +0100526 if (subs->sync_endpoint->type != SND_USB_ENDPOINT_TYPE_DATA ||
527 !subs->stream)
528 return snd_usb_endpoint_set_params(subs->sync_endpoint,
529 subs->pcm_format,
530 subs->channels,
531 subs->period_bytes,
532 subs->cur_rate,
533 subs->cur_audiofmt,
534 NULL);
535
Eldad Zack0d9741c2012-12-03 20:30:09 +0100536 /* Try to find the best matching audioformat. */
537 list_for_each_entry(fp, &sync_subs->fmt_list, list) {
538 int score = match_endpoint_audioformats(fp, subs->cur_audiofmt,
539 subs->cur_rate, subs->pcm_format);
540
541 if (score > cur_score) {
542 sync_fp = fp;
543 cur_score = score;
544 }
545 }
546
547 if (unlikely(sync_fp == NULL)) {
548 snd_printk(KERN_ERR "%s: no valid audioformat for sync ep %x found\n",
549 __func__, sync_subs->ep_num);
550 return -EINVAL;
551 }
552
553 /*
554 * Recalculate the period bytes if channel number differ between
555 * data and sync ep audioformat.
556 */
557 if (sync_fp->channels != subs->channels) {
558 sync_period_bytes = (subs->period_bytes / subs->channels) *
559 sync_fp->channels;
560 snd_printdd("%s: adjusted sync ep period bytes (%d -> %d)\n",
561 __func__, subs->period_bytes, sync_period_bytes);
562 }
563
564 ret = snd_usb_endpoint_set_params(subs->sync_endpoint,
565 subs->pcm_format,
566 sync_fp->channels,
567 sync_period_bytes,
568 subs->cur_rate,
569 sync_fp,
570 NULL);
571
572 return ret;
573}
574
575/*
Dylan Reid61a70952012-09-18 09:49:48 -0700576 * configure endpoint params
577 *
578 * called during initial setup and upon resume
579 */
580static int configure_endpoint(struct snd_usb_substream *subs)
581{
582 int ret;
583
Dylan Reid61a70952012-09-18 09:49:48 -0700584 /* format changed */
Takashi Iwaib0db6062012-11-21 08:35:42 +0100585 stop_endpoints(subs, true);
Dylan Reid61a70952012-09-18 09:49:48 -0700586 ret = snd_usb_endpoint_set_params(subs->data_endpoint,
587 subs->pcm_format,
588 subs->channels,
589 subs->period_bytes,
590 subs->cur_rate,
591 subs->cur_audiofmt,
592 subs->sync_endpoint);
593 if (ret < 0)
Takashi Iwai978520b2012-10-12 15:12:55 +0200594 return ret;
Dylan Reid61a70952012-09-18 09:49:48 -0700595
596 if (subs->sync_endpoint)
Eldad Zack0d9741c2012-12-03 20:30:09 +0100597 ret = configure_sync_endpoint(subs);
598
Dylan Reid61a70952012-09-18 09:49:48 -0700599 return ret;
600}
601
602/*
Daniel Macke5779992010-03-04 19:46:13 +0100603 * hw_params callback
604 *
605 * allocate a buffer and set the given audio format.
606 *
607 * so far we use a physically linear buffer although packetize transfer
608 * doesn't need a continuous area.
609 * if sg buffer is supported on the later version of alsa, we'll follow
610 * that.
611 */
612static int snd_usb_hw_params(struct snd_pcm_substream *substream,
613 struct snd_pcm_hw_params *hw_params)
614{
615 struct snd_usb_substream *subs = substream->runtime->private_data;
616 struct audioformat *fmt;
Dylan Reid61a70952012-09-18 09:49:48 -0700617 int ret;
Daniel Macke5779992010-03-04 19:46:13 +0100618
619 ret = snd_pcm_lib_alloc_vmalloc_buffer(substream,
620 params_buffer_bytes(hw_params));
621 if (ret < 0)
622 return ret;
623
Dylan Reid61a70952012-09-18 09:49:48 -0700624 subs->pcm_format = params_format(hw_params);
625 subs->period_bytes = params_period_bytes(hw_params);
626 subs->channels = params_channels(hw_params);
627 subs->cur_rate = params_rate(hw_params);
628
629 fmt = find_format(subs);
Daniel Macke5779992010-03-04 19:46:13 +0100630 if (!fmt) {
631 snd_printd(KERN_DEBUG "cannot set format: format = %#x, rate = %d, channels = %d\n",
Dylan Reid61a70952012-09-18 09:49:48 -0700632 subs->pcm_format, subs->cur_rate, subs->channels);
Daniel Macke5779992010-03-04 19:46:13 +0100633 return -EINVAL;
634 }
635
Takashi Iwai34f3c892012-10-15 12:16:02 +0200636 down_read(&subs->stream->chip->shutdown_rwsem);
Takashi Iwai978520b2012-10-12 15:12:55 +0200637 if (subs->stream->chip->shutdown)
638 ret = -ENODEV;
639 else
640 ret = set_format(subs, fmt);
Takashi Iwai34f3c892012-10-15 12:16:02 +0200641 up_read(&subs->stream->chip->shutdown_rwsem);
Takashi Iwai978520b2012-10-12 15:12:55 +0200642 if (ret < 0)
Daniel Macke5779992010-03-04 19:46:13 +0100643 return ret;
644
Dylan Reid61a70952012-09-18 09:49:48 -0700645 subs->interface = fmt->iface;
646 subs->altset_idx = fmt->altset_idx;
Takashi Iwai384dc0852012-09-18 14:49:31 +0200647 subs->need_setup_ep = true;
Daniel Macke5779992010-03-04 19:46:13 +0100648
Dylan Reid61a70952012-09-18 09:49:48 -0700649 return 0;
Daniel Macke5779992010-03-04 19:46:13 +0100650}
651
652/*
653 * hw_free callback
654 *
655 * reset the audio format and release the buffer
656 */
657static int snd_usb_hw_free(struct snd_pcm_substream *substream)
658{
659 struct snd_usb_substream *subs = substream->runtime->private_data;
660
661 subs->cur_audiofmt = NULL;
662 subs->cur_rate = 0;
663 subs->period_bytes = 0;
Takashi Iwai34f3c892012-10-15 12:16:02 +0200664 down_read(&subs->stream->chip->shutdown_rwsem);
Takashi Iwai978520b2012-10-12 15:12:55 +0200665 if (!subs->stream->chip->shutdown) {
Takashi Iwaia9bb3622012-11-20 18:32:06 +0100666 stop_endpoints(subs, true);
Takashi Iwai978520b2012-10-12 15:12:55 +0200667 deactivate_endpoints(subs);
668 }
Takashi Iwai34f3c892012-10-15 12:16:02 +0200669 up_read(&subs->stream->chip->shutdown_rwsem);
Daniel Macke5779992010-03-04 19:46:13 +0100670 return snd_pcm_lib_free_vmalloc_buffer(substream);
671}
672
673/*
674 * prepare callback
675 *
676 * only a few subtle things...
677 */
678static int snd_usb_pcm_prepare(struct snd_pcm_substream *substream)
679{
680 struct snd_pcm_runtime *runtime = substream->runtime;
681 struct snd_usb_substream *subs = runtime->private_data;
Dylan Reid61a70952012-09-18 09:49:48 -0700682 struct usb_host_interface *alts;
683 struct usb_interface *iface;
684 int ret;
Daniel Macke5779992010-03-04 19:46:13 +0100685
686 if (! subs->cur_audiofmt) {
687 snd_printk(KERN_ERR "usbaudio: no format is specified!\n");
688 return -ENXIO;
689 }
690
Takashi Iwai34f3c892012-10-15 12:16:02 +0200691 down_read(&subs->stream->chip->shutdown_rwsem);
Takashi Iwai978520b2012-10-12 15:12:55 +0200692 if (subs->stream->chip->shutdown) {
693 ret = -ENODEV;
694 goto unlock;
695 }
696 if (snd_BUG_ON(!subs->data_endpoint)) {
697 ret = -EIO;
698 goto unlock;
699 }
Daniel Mackedcd3632012-04-12 13:51:12 +0200700
Takashi Iwaif58161b2012-11-08 08:52:45 +0100701 snd_usb_endpoint_sync_pending_stop(subs->sync_endpoint);
702 snd_usb_endpoint_sync_pending_stop(subs->data_endpoint);
703
Dylan Reid61a70952012-09-18 09:49:48 -0700704 ret = set_format(subs, subs->cur_audiofmt);
705 if (ret < 0)
Takashi Iwai978520b2012-10-12 15:12:55 +0200706 goto unlock;
Dylan Reid61a70952012-09-18 09:49:48 -0700707
708 iface = usb_ifnum_to_if(subs->dev, subs->cur_audiofmt->iface);
709 alts = &iface->altsetting[subs->cur_audiofmt->altset_idx];
710 ret = snd_usb_init_sample_rate(subs->stream->chip,
711 subs->cur_audiofmt->iface,
712 alts,
713 subs->cur_audiofmt,
714 subs->cur_rate);
715 if (ret < 0)
Takashi Iwai978520b2012-10-12 15:12:55 +0200716 goto unlock;
Dylan Reid61a70952012-09-18 09:49:48 -0700717
Takashi Iwai384dc0852012-09-18 14:49:31 +0200718 if (subs->need_setup_ep) {
719 ret = configure_endpoint(subs);
720 if (ret < 0)
Takashi Iwai978520b2012-10-12 15:12:55 +0200721 goto unlock;
Takashi Iwai384dc0852012-09-18 14:49:31 +0200722 subs->need_setup_ep = false;
723 }
Dylan Reid61a70952012-09-18 09:49:48 -0700724
Daniel Macke5779992010-03-04 19:46:13 +0100725 /* some unit conversions in runtime */
Daniel Mackedcd3632012-04-12 13:51:12 +0200726 subs->data_endpoint->maxframesize =
727 bytes_to_frames(runtime, subs->data_endpoint->maxpacksize);
728 subs->data_endpoint->curframesize =
729 bytes_to_frames(runtime, subs->data_endpoint->curpacksize);
Daniel Macke5779992010-03-04 19:46:13 +0100730
731 /* reset the pointer */
732 subs->hwptr_done = 0;
733 subs->transfer_done = 0;
Pierre-Louis Bossart294c4fb2011-09-06 19:15:34 -0500734 subs->last_delay = 0;
735 subs->last_frame_number = 0;
Daniel Macke5779992010-03-04 19:46:13 +0100736 runtime->delay = 0;
737
Daniel Mackedcd3632012-04-12 13:51:12 +0200738 /* for playback, submit the URBs now; otherwise, the first hwptr_done
739 * updates for all URBs would happen at the same time when starting */
740 if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK)
Takashi Iwaia9bb3622012-11-20 18:32:06 +0100741 ret = start_endpoints(subs, true);
Daniel Mackedcd3632012-04-12 13:51:12 +0200742
Takashi Iwai978520b2012-10-12 15:12:55 +0200743 unlock:
Takashi Iwai34f3c892012-10-15 12:16:02 +0200744 up_read(&subs->stream->chip->shutdown_rwsem);
Takashi Iwai978520b2012-10-12 15:12:55 +0200745 return ret;
Daniel Macke5779992010-03-04 19:46:13 +0100746}
747
748static struct snd_pcm_hardware snd_usb_hardware =
749{
750 .info = SNDRV_PCM_INFO_MMAP |
751 SNDRV_PCM_INFO_MMAP_VALID |
752 SNDRV_PCM_INFO_BATCH |
753 SNDRV_PCM_INFO_INTERLEAVED |
754 SNDRV_PCM_INFO_BLOCK_TRANSFER |
755 SNDRV_PCM_INFO_PAUSE,
756 .buffer_bytes_max = 1024 * 1024,
757 .period_bytes_min = 64,
758 .period_bytes_max = 512 * 1024,
759 .periods_min = 2,
760 .periods_max = 1024,
761};
762
763static int hw_check_valid_format(struct snd_usb_substream *subs,
764 struct snd_pcm_hw_params *params,
765 struct audioformat *fp)
766{
767 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
768 struct snd_interval *ct = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
769 struct snd_mask *fmts = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
770 struct snd_interval *pt = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
Clemens Ladisch015eb0b2010-03-04 19:46:15 +0100771 struct snd_mask check_fmts;
Daniel Macke5779992010-03-04 19:46:13 +0100772 unsigned int ptime;
773
774 /* check the format */
Clemens Ladisch015eb0b2010-03-04 19:46:15 +0100775 snd_mask_none(&check_fmts);
776 check_fmts.bits[0] = (u32)fp->formats;
777 check_fmts.bits[1] = (u32)(fp->formats >> 32);
778 snd_mask_intersect(&check_fmts, fmts);
779 if (snd_mask_empty(&check_fmts)) {
Daniel Macke5779992010-03-04 19:46:13 +0100780 hwc_debug(" > check: no supported format %d\n", fp->format);
781 return 0;
782 }
783 /* check the channels */
784 if (fp->channels < ct->min || fp->channels > ct->max) {
785 hwc_debug(" > check: no valid channels %d (%d/%d)\n", fp->channels, ct->min, ct->max);
786 return 0;
787 }
788 /* check the rate is within the range */
789 if (fp->rate_min > it->max || (fp->rate_min == it->max && it->openmax)) {
790 hwc_debug(" > check: rate_min %d > max %d\n", fp->rate_min, it->max);
791 return 0;
792 }
793 if (fp->rate_max < it->min || (fp->rate_max == it->min && it->openmin)) {
794 hwc_debug(" > check: rate_max %d < min %d\n", fp->rate_max, it->min);
795 return 0;
796 }
797 /* check whether the period time is >= the data packet interval */
Takashi Iwai978520b2012-10-12 15:12:55 +0200798 if (subs->speed != USB_SPEED_FULL) {
Daniel Macke5779992010-03-04 19:46:13 +0100799 ptime = 125 * (1 << fp->datainterval);
800 if (ptime > pt->max || (ptime == pt->max && pt->openmax)) {
801 hwc_debug(" > check: ptime %u > max %u\n", ptime, pt->max);
802 return 0;
803 }
804 }
805 return 1;
806}
807
808static int hw_rule_rate(struct snd_pcm_hw_params *params,
809 struct snd_pcm_hw_rule *rule)
810{
811 struct snd_usb_substream *subs = rule->private;
812 struct list_head *p;
813 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
814 unsigned int rmin, rmax;
815 int changed;
816
817 hwc_debug("hw_rule_rate: (%d,%d)\n", it->min, it->max);
818 changed = 0;
819 rmin = rmax = 0;
820 list_for_each(p, &subs->fmt_list) {
821 struct audioformat *fp;
822 fp = list_entry(p, struct audioformat, list);
823 if (!hw_check_valid_format(subs, params, fp))
824 continue;
825 if (changed++) {
826 if (rmin > fp->rate_min)
827 rmin = fp->rate_min;
828 if (rmax < fp->rate_max)
829 rmax = fp->rate_max;
830 } else {
831 rmin = fp->rate_min;
832 rmax = fp->rate_max;
833 }
834 }
835
836 if (!changed) {
837 hwc_debug(" --> get empty\n");
838 it->empty = 1;
839 return -EINVAL;
840 }
841
842 changed = 0;
843 if (it->min < rmin) {
844 it->min = rmin;
845 it->openmin = 0;
846 changed = 1;
847 }
848 if (it->max > rmax) {
849 it->max = rmax;
850 it->openmax = 0;
851 changed = 1;
852 }
853 if (snd_interval_checkempty(it)) {
854 it->empty = 1;
855 return -EINVAL;
856 }
857 hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
858 return changed;
859}
860
861
862static int hw_rule_channels(struct snd_pcm_hw_params *params,
863 struct snd_pcm_hw_rule *rule)
864{
865 struct snd_usb_substream *subs = rule->private;
866 struct list_head *p;
867 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
868 unsigned int rmin, rmax;
869 int changed;
870
871 hwc_debug("hw_rule_channels: (%d,%d)\n", it->min, it->max);
872 changed = 0;
873 rmin = rmax = 0;
874 list_for_each(p, &subs->fmt_list) {
875 struct audioformat *fp;
876 fp = list_entry(p, struct audioformat, list);
877 if (!hw_check_valid_format(subs, params, fp))
878 continue;
879 if (changed++) {
880 if (rmin > fp->channels)
881 rmin = fp->channels;
882 if (rmax < fp->channels)
883 rmax = fp->channels;
884 } else {
885 rmin = fp->channels;
886 rmax = fp->channels;
887 }
888 }
889
890 if (!changed) {
891 hwc_debug(" --> get empty\n");
892 it->empty = 1;
893 return -EINVAL;
894 }
895
896 changed = 0;
897 if (it->min < rmin) {
898 it->min = rmin;
899 it->openmin = 0;
900 changed = 1;
901 }
902 if (it->max > rmax) {
903 it->max = rmax;
904 it->openmax = 0;
905 changed = 1;
906 }
907 if (snd_interval_checkempty(it)) {
908 it->empty = 1;
909 return -EINVAL;
910 }
911 hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
912 return changed;
913}
914
915static int hw_rule_format(struct snd_pcm_hw_params *params,
916 struct snd_pcm_hw_rule *rule)
917{
918 struct snd_usb_substream *subs = rule->private;
919 struct list_head *p;
920 struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
921 u64 fbits;
922 u32 oldbits[2];
923 int changed;
924
925 hwc_debug("hw_rule_format: %x:%x\n", fmt->bits[0], fmt->bits[1]);
926 fbits = 0;
927 list_for_each(p, &subs->fmt_list) {
928 struct audioformat *fp;
929 fp = list_entry(p, struct audioformat, list);
930 if (!hw_check_valid_format(subs, params, fp))
931 continue;
Clemens Ladisch015eb0b2010-03-04 19:46:15 +0100932 fbits |= fp->formats;
Daniel Macke5779992010-03-04 19:46:13 +0100933 }
934
935 oldbits[0] = fmt->bits[0];
936 oldbits[1] = fmt->bits[1];
937 fmt->bits[0] &= (u32)fbits;
938 fmt->bits[1] &= (u32)(fbits >> 32);
939 if (!fmt->bits[0] && !fmt->bits[1]) {
940 hwc_debug(" --> get empty\n");
941 return -EINVAL;
942 }
943 changed = (oldbits[0] != fmt->bits[0] || oldbits[1] != fmt->bits[1]);
944 hwc_debug(" --> %x:%x (changed = %d)\n", fmt->bits[0], fmt->bits[1], changed);
945 return changed;
946}
947
948static int hw_rule_period_time(struct snd_pcm_hw_params *params,
949 struct snd_pcm_hw_rule *rule)
950{
951 struct snd_usb_substream *subs = rule->private;
952 struct audioformat *fp;
953 struct snd_interval *it;
954 unsigned char min_datainterval;
955 unsigned int pmin;
956 int changed;
957
958 it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
959 hwc_debug("hw_rule_period_time: (%u,%u)\n", it->min, it->max);
960 min_datainterval = 0xff;
961 list_for_each_entry(fp, &subs->fmt_list, list) {
962 if (!hw_check_valid_format(subs, params, fp))
963 continue;
964 min_datainterval = min(min_datainterval, fp->datainterval);
965 }
966 if (min_datainterval == 0xff) {
Uwe Kleine-Königa7ce2e02010-07-12 17:15:44 +0200967 hwc_debug(" --> get empty\n");
Daniel Macke5779992010-03-04 19:46:13 +0100968 it->empty = 1;
969 return -EINVAL;
970 }
971 pmin = 125 * (1 << min_datainterval);
972 changed = 0;
973 if (it->min < pmin) {
974 it->min = pmin;
975 it->openmin = 0;
976 changed = 1;
977 }
978 if (snd_interval_checkempty(it)) {
979 it->empty = 1;
980 return -EINVAL;
981 }
982 hwc_debug(" --> (%u,%u) (changed = %d)\n", it->min, it->max, changed);
983 return changed;
984}
985
986/*
987 * If the device supports unusual bit rates, does the request meet these?
988 */
989static int snd_usb_pcm_check_knot(struct snd_pcm_runtime *runtime,
990 struct snd_usb_substream *subs)
991{
992 struct audioformat *fp;
Takashi Iwai0717d0f2012-03-15 16:14:38 +0100993 int *rate_list;
Daniel Macke5779992010-03-04 19:46:13 +0100994 int count = 0, needs_knot = 0;
995 int err;
996
Clemens Ladisch5cd5d7c2012-05-18 18:00:43 +0200997 kfree(subs->rate_list.list);
998 subs->rate_list.list = NULL;
999
Daniel Macke5779992010-03-04 19:46:13 +01001000 list_for_each_entry(fp, &subs->fmt_list, list) {
1001 if (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)
1002 return 0;
1003 count += fp->nr_rates;
1004 if (fp->rates & SNDRV_PCM_RATE_KNOT)
1005 needs_knot = 1;
1006 }
1007 if (!needs_knot)
1008 return 0;
1009
Takashi Iwai0717d0f2012-03-15 16:14:38 +01001010 subs->rate_list.list = rate_list =
1011 kmalloc(sizeof(int) * count, GFP_KERNEL);
Jesper Juhl8a8d56b2010-10-29 20:40:23 +02001012 if (!subs->rate_list.list)
1013 return -ENOMEM;
1014 subs->rate_list.count = count;
Daniel Macke5779992010-03-04 19:46:13 +01001015 subs->rate_list.mask = 0;
1016 count = 0;
1017 list_for_each_entry(fp, &subs->fmt_list, list) {
1018 int i;
1019 for (i = 0; i < fp->nr_rates; i++)
Takashi Iwai0717d0f2012-03-15 16:14:38 +01001020 rate_list[count++] = fp->rate_table[i];
Daniel Macke5779992010-03-04 19:46:13 +01001021 }
1022 err = snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1023 &subs->rate_list);
1024 if (err < 0)
1025 return err;
1026
1027 return 0;
1028}
1029
1030
1031/*
1032 * set up the runtime hardware information.
1033 */
1034
1035static int setup_hw_info(struct snd_pcm_runtime *runtime, struct snd_usb_substream *subs)
1036{
1037 struct list_head *p;
1038 unsigned int pt, ptmin;
1039 int param_period_time_if_needed;
1040 int err;
1041
1042 runtime->hw.formats = subs->formats;
1043
1044 runtime->hw.rate_min = 0x7fffffff;
1045 runtime->hw.rate_max = 0;
1046 runtime->hw.channels_min = 256;
1047 runtime->hw.channels_max = 0;
1048 runtime->hw.rates = 0;
1049 ptmin = UINT_MAX;
1050 /* check min/max rates and channels */
1051 list_for_each(p, &subs->fmt_list) {
1052 struct audioformat *fp;
1053 fp = list_entry(p, struct audioformat, list);
1054 runtime->hw.rates |= fp->rates;
1055 if (runtime->hw.rate_min > fp->rate_min)
1056 runtime->hw.rate_min = fp->rate_min;
1057 if (runtime->hw.rate_max < fp->rate_max)
1058 runtime->hw.rate_max = fp->rate_max;
1059 if (runtime->hw.channels_min > fp->channels)
1060 runtime->hw.channels_min = fp->channels;
1061 if (runtime->hw.channels_max < fp->channels)
1062 runtime->hw.channels_max = fp->channels;
1063 if (fp->fmt_type == UAC_FORMAT_TYPE_II && fp->frame_size > 0) {
1064 /* FIXME: there might be more than one audio formats... */
1065 runtime->hw.period_bytes_min = runtime->hw.period_bytes_max =
1066 fp->frame_size;
1067 }
1068 pt = 125 * (1 << fp->datainterval);
1069 ptmin = min(ptmin, pt);
1070 }
Oliver Neukum88a85162011-03-11 14:51:12 +01001071 err = snd_usb_autoresume(subs->stream->chip);
1072 if (err < 0)
1073 return err;
Daniel Macke5779992010-03-04 19:46:13 +01001074
1075 param_period_time_if_needed = SNDRV_PCM_HW_PARAM_PERIOD_TIME;
Takashi Iwai978520b2012-10-12 15:12:55 +02001076 if (subs->speed == USB_SPEED_FULL)
Daniel Macke5779992010-03-04 19:46:13 +01001077 /* full speed devices have fixed data packet interval */
1078 ptmin = 1000;
1079 if (ptmin == 1000)
1080 /* if period time doesn't go below 1 ms, no rules needed */
1081 param_period_time_if_needed = -1;
1082 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1083 ptmin, UINT_MAX);
1084
1085 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1086 hw_rule_rate, subs,
1087 SNDRV_PCM_HW_PARAM_FORMAT,
1088 SNDRV_PCM_HW_PARAM_CHANNELS,
1089 param_period_time_if_needed,
1090 -1)) < 0)
Oliver Neukum88a85162011-03-11 14:51:12 +01001091 goto rep_err;
Daniel Macke5779992010-03-04 19:46:13 +01001092 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
1093 hw_rule_channels, subs,
1094 SNDRV_PCM_HW_PARAM_FORMAT,
1095 SNDRV_PCM_HW_PARAM_RATE,
1096 param_period_time_if_needed,
1097 -1)) < 0)
Oliver Neukum88a85162011-03-11 14:51:12 +01001098 goto rep_err;
Daniel Macke5779992010-03-04 19:46:13 +01001099 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
1100 hw_rule_format, subs,
1101 SNDRV_PCM_HW_PARAM_RATE,
1102 SNDRV_PCM_HW_PARAM_CHANNELS,
1103 param_period_time_if_needed,
1104 -1)) < 0)
Oliver Neukum88a85162011-03-11 14:51:12 +01001105 goto rep_err;
Daniel Macke5779992010-03-04 19:46:13 +01001106 if (param_period_time_if_needed >= 0) {
1107 err = snd_pcm_hw_rule_add(runtime, 0,
1108 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1109 hw_rule_period_time, subs,
1110 SNDRV_PCM_HW_PARAM_FORMAT,
1111 SNDRV_PCM_HW_PARAM_CHANNELS,
1112 SNDRV_PCM_HW_PARAM_RATE,
1113 -1);
1114 if (err < 0)
Oliver Neukum88a85162011-03-11 14:51:12 +01001115 goto rep_err;
Daniel Macke5779992010-03-04 19:46:13 +01001116 }
1117 if ((err = snd_usb_pcm_check_knot(runtime, subs)) < 0)
Oliver Neukum88a85162011-03-11 14:51:12 +01001118 goto rep_err;
Daniel Macke5779992010-03-04 19:46:13 +01001119 return 0;
Oliver Neukum88a85162011-03-11 14:51:12 +01001120
1121rep_err:
1122 snd_usb_autosuspend(subs->stream->chip);
1123 return err;
Daniel Macke5779992010-03-04 19:46:13 +01001124}
1125
1126static int snd_usb_pcm_open(struct snd_pcm_substream *substream, int direction)
1127{
1128 struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
1129 struct snd_pcm_runtime *runtime = substream->runtime;
1130 struct snd_usb_substream *subs = &as->substream[direction];
1131
1132 subs->interface = -1;
Clemens Ladische11b4e02010-03-04 19:46:14 +01001133 subs->altset_idx = 0;
Daniel Macke5779992010-03-04 19:46:13 +01001134 runtime->hw = snd_usb_hardware;
1135 runtime->private_data = subs;
1136 subs->pcm_substream = substream;
Oliver Neukum88a85162011-03-11 14:51:12 +01001137 /* runtime PM is also done there */
Daniel Macke5779992010-03-04 19:46:13 +01001138 return setup_hw_info(runtime, subs);
1139}
1140
1141static int snd_usb_pcm_close(struct snd_pcm_substream *substream, int direction)
1142{
1143 struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
1144 struct snd_usb_substream *subs = &as->substream[direction];
1145
Takashi Iwaib0db6062012-11-21 08:35:42 +01001146 stop_endpoints(subs, true);
Daniel Mack68e67f42012-07-12 13:08:40 +02001147
1148 if (!as->chip->shutdown && subs->interface >= 0) {
1149 usb_set_interface(subs->dev, subs->interface, 0);
1150 subs->interface = -1;
1151 }
1152
Daniel Macke5779992010-03-04 19:46:13 +01001153 subs->pcm_substream = NULL;
Oliver Neukum88a85162011-03-11 14:51:12 +01001154 snd_usb_autosuspend(subs->stream->chip);
Daniel Mackedcd3632012-04-12 13:51:12 +02001155
Daniel Mack68e67f42012-07-12 13:08:40 +02001156 return 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001157}
1158
1159/* Since a URB can handle only a single linear buffer, we must use double
1160 * buffering when the data to be transferred overflows the buffer boundary.
1161 * To avoid inconsistencies when updating hwptr_done, we use double buffering
1162 * for all URBs.
1163 */
1164static void retire_capture_urb(struct snd_usb_substream *subs,
1165 struct urb *urb)
1166{
1167 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1168 unsigned int stride, frames, bytes, oldptr;
1169 int i, period_elapsed = 0;
1170 unsigned long flags;
1171 unsigned char *cp;
Pierre-Louis Bossarte4cc6152012-12-19 11:39:05 -06001172 int current_frame_number;
1173
1174 /* read frame number here, update pointer in critical section */
1175 current_frame_number = usb_get_current_frame_number(subs->dev);
Daniel Mackedcd3632012-04-12 13:51:12 +02001176
1177 stride = runtime->frame_bits >> 3;
1178
1179 for (i = 0; i < urb->number_of_packets; i++) {
1180 cp = (unsigned char *)urb->transfer_buffer + urb->iso_frame_desc[i].offset;
1181 if (urb->iso_frame_desc[i].status && printk_ratelimit()) {
1182 snd_printdd(KERN_ERR "frame %d active: %d\n", i, urb->iso_frame_desc[i].status);
1183 // continue;
1184 }
1185 bytes = urb->iso_frame_desc[i].actual_length;
1186 frames = bytes / stride;
1187 if (!subs->txfr_quirk)
1188 bytes = frames * stride;
1189 if (bytes % (runtime->sample_bits >> 3) != 0) {
Daniel Mackedcd3632012-04-12 13:51:12 +02001190 int oldbytes = bytes;
Daniel Mackedcd3632012-04-12 13:51:12 +02001191 bytes = frames * stride;
1192 snd_printdd(KERN_ERR "Corrected urb data len. %d->%d\n",
1193 oldbytes, bytes);
1194 }
1195 /* update the current pointer */
1196 spin_lock_irqsave(&subs->lock, flags);
1197 oldptr = subs->hwptr_done;
1198 subs->hwptr_done += bytes;
1199 if (subs->hwptr_done >= runtime->buffer_size * stride)
1200 subs->hwptr_done -= runtime->buffer_size * stride;
1201 frames = (bytes + (oldptr % stride)) / stride;
1202 subs->transfer_done += frames;
1203 if (subs->transfer_done >= runtime->period_size) {
1204 subs->transfer_done -= runtime->period_size;
1205 period_elapsed = 1;
1206 }
Pierre-Louis Bossarte4cc6152012-12-19 11:39:05 -06001207 /* capture delay is by construction limited to one URB,
1208 * reset delays here
1209 */
1210 runtime->delay = subs->last_delay = 0;
1211
1212 /* realign last_frame_number */
1213 subs->last_frame_number = current_frame_number;
1214 subs->last_frame_number &= 0xFF; /* keep 8 LSBs */
1215
Daniel Mackedcd3632012-04-12 13:51:12 +02001216 spin_unlock_irqrestore(&subs->lock, flags);
1217 /* copy a data chunk */
1218 if (oldptr + bytes > runtime->buffer_size * stride) {
1219 unsigned int bytes1 =
1220 runtime->buffer_size * stride - oldptr;
1221 memcpy(runtime->dma_area + oldptr, cp, bytes1);
1222 memcpy(runtime->dma_area, cp + bytes1, bytes - bytes1);
1223 } else {
1224 memcpy(runtime->dma_area + oldptr, cp, bytes);
1225 }
1226 }
1227
1228 if (period_elapsed)
1229 snd_pcm_period_elapsed(subs->pcm_substream);
1230}
1231
1232static void prepare_playback_urb(struct snd_usb_substream *subs,
1233 struct urb *urb)
1234{
1235 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
Daniel Mack245baf92012-08-30 18:52:30 +02001236 struct snd_usb_endpoint *ep = subs->data_endpoint;
Daniel Mackedcd3632012-04-12 13:51:12 +02001237 struct snd_urb_ctx *ctx = urb->context;
1238 unsigned int counts, frames, bytes;
1239 int i, stride, period_elapsed = 0;
1240 unsigned long flags;
1241
1242 stride = runtime->frame_bits >> 3;
1243
1244 frames = 0;
1245 urb->number_of_packets = 0;
1246 spin_lock_irqsave(&subs->lock, flags);
1247 for (i = 0; i < ctx->packets; i++) {
Daniel Mack245baf92012-08-30 18:52:30 +02001248 if (ctx->packet_size[i])
1249 counts = ctx->packet_size[i];
1250 else
1251 counts = snd_usb_endpoint_next_packet_size(ep);
1252
Daniel Mackedcd3632012-04-12 13:51:12 +02001253 /* set up descriptor */
1254 urb->iso_frame_desc[i].offset = frames * stride;
1255 urb->iso_frame_desc[i].length = counts * stride;
1256 frames += counts;
1257 urb->number_of_packets++;
1258 subs->transfer_done += counts;
1259 if (subs->transfer_done >= runtime->period_size) {
1260 subs->transfer_done -= runtime->period_size;
1261 period_elapsed = 1;
1262 if (subs->fmt_type == UAC_FORMAT_TYPE_II) {
1263 if (subs->transfer_done > 0) {
1264 /* FIXME: fill-max mode is not
1265 * supported yet */
1266 frames -= subs->transfer_done;
1267 counts -= subs->transfer_done;
1268 urb->iso_frame_desc[i].length =
1269 counts * stride;
1270 subs->transfer_done = 0;
1271 }
1272 i++;
1273 if (i < ctx->packets) {
1274 /* add a transfer delimiter */
1275 urb->iso_frame_desc[i].offset =
1276 frames * stride;
1277 urb->iso_frame_desc[i].length = 0;
1278 urb->number_of_packets++;
1279 }
1280 break;
1281 }
1282 }
1283 if (period_elapsed &&
1284 !snd_usb_endpoint_implict_feedback_sink(subs->data_endpoint)) /* finish at the period boundary */
1285 break;
1286 }
1287 bytes = frames * stride;
1288 if (subs->hwptr_done + bytes > runtime->buffer_size * stride) {
1289 /* err, the transferred area goes over buffer boundary. */
1290 unsigned int bytes1 =
1291 runtime->buffer_size * stride - subs->hwptr_done;
1292 memcpy(urb->transfer_buffer,
1293 runtime->dma_area + subs->hwptr_done, bytes1);
1294 memcpy(urb->transfer_buffer + bytes1,
1295 runtime->dma_area, bytes - bytes1);
1296 } else {
1297 memcpy(urb->transfer_buffer,
1298 runtime->dma_area + subs->hwptr_done, bytes);
1299 }
1300 subs->hwptr_done += bytes;
1301 if (subs->hwptr_done >= runtime->buffer_size * stride)
1302 subs->hwptr_done -= runtime->buffer_size * stride;
Daniel Mackfbcfbf52012-08-30 18:52:29 +02001303
1304 /* update delay with exact number of samples queued */
1305 runtime->delay = subs->last_delay;
Daniel Mackedcd3632012-04-12 13:51:12 +02001306 runtime->delay += frames;
Daniel Mackfbcfbf52012-08-30 18:52:29 +02001307 subs->last_delay = runtime->delay;
1308
1309 /* realign last_frame_number */
1310 subs->last_frame_number = usb_get_current_frame_number(subs->dev);
1311 subs->last_frame_number &= 0xFF; /* keep 8 LSBs */
1312
Daniel Mackedcd3632012-04-12 13:51:12 +02001313 spin_unlock_irqrestore(&subs->lock, flags);
1314 urb->transfer_buffer_length = bytes;
1315 if (period_elapsed)
1316 snd_pcm_period_elapsed(subs->pcm_substream);
1317}
1318
1319/*
1320 * process after playback data complete
1321 * - decrease the delay count again
1322 */
1323static void retire_playback_urb(struct snd_usb_substream *subs,
1324 struct urb *urb)
1325{
1326 unsigned long flags;
1327 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1328 int stride = runtime->frame_bits >> 3;
1329 int processed = urb->transfer_buffer_length / stride;
Daniel Mackfbcfbf52012-08-30 18:52:29 +02001330 int est_delay;
Daniel Mackedcd3632012-04-12 13:51:12 +02001331
Takashi Iwai1213a202012-09-06 14:58:00 +02001332 /* ignore the delay accounting when procssed=0 is given, i.e.
1333 * silent payloads are procssed before handling the actual data
1334 */
1335 if (!processed)
1336 return;
1337
Daniel Mackedcd3632012-04-12 13:51:12 +02001338 spin_lock_irqsave(&subs->lock, flags);
Takashi Iwai48779a02012-11-23 16:00:37 +01001339 if (!subs->last_delay)
1340 goto out; /* short path */
1341
Daniel Mackfbcfbf52012-08-30 18:52:29 +02001342 est_delay = snd_usb_pcm_delay(subs, runtime->rate);
1343 /* update delay with exact number of samples played */
1344 if (processed > subs->last_delay)
1345 subs->last_delay = 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001346 else
Daniel Mackfbcfbf52012-08-30 18:52:29 +02001347 subs->last_delay -= processed;
1348 runtime->delay = subs->last_delay;
1349
1350 /*
1351 * Report when delay estimate is off by more than 2ms.
1352 * The error should be lower than 2ms since the estimate relies
1353 * on two reads of a counter updated every ms.
1354 */
1355 if (abs(est_delay - subs->last_delay) * 1000 > runtime->rate * 2)
1356 snd_printk(KERN_DEBUG "delay: estimated %d, actual %d\n",
1357 est_delay, subs->last_delay);
1358
Takashi Iwai48779a02012-11-23 16:00:37 +01001359 if (!subs->running) {
1360 /* update last_frame_number for delay counting here since
1361 * prepare_playback_urb won't be called during pause
1362 */
1363 subs->last_frame_number =
1364 usb_get_current_frame_number(subs->dev) & 0xff;
1365 }
1366
1367 out:
Daniel Mackedcd3632012-04-12 13:51:12 +02001368 spin_unlock_irqrestore(&subs->lock, flags);
Daniel Macke5779992010-03-04 19:46:13 +01001369}
1370
1371static int snd_usb_playback_open(struct snd_pcm_substream *substream)
1372{
1373 return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_PLAYBACK);
1374}
1375
1376static int snd_usb_playback_close(struct snd_pcm_substream *substream)
1377{
1378 return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_PLAYBACK);
1379}
1380
1381static int snd_usb_capture_open(struct snd_pcm_substream *substream)
1382{
1383 return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_CAPTURE);
1384}
1385
1386static int snd_usb_capture_close(struct snd_pcm_substream *substream)
1387{
1388 return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_CAPTURE);
1389}
1390
Daniel Mackedcd3632012-04-12 13:51:12 +02001391static int snd_usb_substream_playback_trigger(struct snd_pcm_substream *substream,
1392 int cmd)
1393{
1394 struct snd_usb_substream *subs = substream->runtime->private_data;
1395
1396 switch (cmd) {
1397 case SNDRV_PCM_TRIGGER_START:
1398 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1399 subs->data_endpoint->prepare_data_urb = prepare_playback_urb;
1400 subs->data_endpoint->retire_data_urb = retire_playback_urb;
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001401 subs->running = 1;
Daniel Mackedcd3632012-04-12 13:51:12 +02001402 return 0;
1403 case SNDRV_PCM_TRIGGER_STOP:
Takashi Iwaia9bb3622012-11-20 18:32:06 +01001404 stop_endpoints(subs, false);
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001405 subs->running = 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001406 return 0;
1407 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1408 subs->data_endpoint->prepare_data_urb = NULL;
Takashi Iwai48779a02012-11-23 16:00:37 +01001409 /* keep retire_data_urb for delay calculation */
1410 subs->data_endpoint->retire_data_urb = retire_playback_urb;
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001411 subs->running = 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001412 return 0;
1413 }
1414
1415 return -EINVAL;
1416}
1417
Daniel Mackafe25962012-06-16 16:58:04 +02001418static int snd_usb_substream_capture_trigger(struct snd_pcm_substream *substream,
1419 int cmd)
Daniel Mackedcd3632012-04-12 13:51:12 +02001420{
1421 int err;
1422 struct snd_usb_substream *subs = substream->runtime->private_data;
1423
1424 switch (cmd) {
1425 case SNDRV_PCM_TRIGGER_START:
Takashi Iwaia9bb3622012-11-20 18:32:06 +01001426 err = start_endpoints(subs, false);
Daniel Mackedcd3632012-04-12 13:51:12 +02001427 if (err < 0)
1428 return err;
1429
1430 subs->data_endpoint->retire_data_urb = retire_capture_urb;
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001431 subs->running = 1;
Daniel Mackedcd3632012-04-12 13:51:12 +02001432 return 0;
1433 case SNDRV_PCM_TRIGGER_STOP:
Takashi Iwaia9bb3622012-11-20 18:32:06 +01001434 stop_endpoints(subs, false);
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001435 subs->running = 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001436 return 0;
1437 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1438 subs->data_endpoint->retire_data_urb = NULL;
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001439 subs->running = 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001440 return 0;
1441 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1442 subs->data_endpoint->retire_data_urb = retire_capture_urb;
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001443 subs->running = 1;
Daniel Mackedcd3632012-04-12 13:51:12 +02001444 return 0;
1445 }
1446
1447 return -EINVAL;
1448}
1449
Daniel Macke5779992010-03-04 19:46:13 +01001450static struct snd_pcm_ops snd_usb_playback_ops = {
1451 .open = snd_usb_playback_open,
1452 .close = snd_usb_playback_close,
1453 .ioctl = snd_pcm_lib_ioctl,
1454 .hw_params = snd_usb_hw_params,
1455 .hw_free = snd_usb_hw_free,
1456 .prepare = snd_usb_pcm_prepare,
1457 .trigger = snd_usb_substream_playback_trigger,
1458 .pointer = snd_usb_pcm_pointer,
1459 .page = snd_pcm_lib_get_vmalloc_page,
1460 .mmap = snd_pcm_lib_mmap_vmalloc,
1461};
1462
1463static struct snd_pcm_ops snd_usb_capture_ops = {
1464 .open = snd_usb_capture_open,
1465 .close = snd_usb_capture_close,
1466 .ioctl = snd_pcm_lib_ioctl,
1467 .hw_params = snd_usb_hw_params,
1468 .hw_free = snd_usb_hw_free,
1469 .prepare = snd_usb_pcm_prepare,
1470 .trigger = snd_usb_substream_capture_trigger,
1471 .pointer = snd_usb_pcm_pointer,
1472 .page = snd_pcm_lib_get_vmalloc_page,
1473 .mmap = snd_pcm_lib_mmap_vmalloc,
1474};
1475
1476void snd_usb_set_pcm_ops(struct snd_pcm *pcm, int stream)
1477{
1478 snd_pcm_set_ops(pcm, stream,
1479 stream == SNDRV_PCM_STREAM_PLAYBACK ?
1480 &snd_usb_playback_ops : &snd_usb_capture_ops);
1481}