Merge tag 'firewire-update2' of git://git.kernel.org/pub/scm/linux/kernel/git/ieee139...
[deliverable/linux.git] / sound / usb / pcm.c
CommitLineData
e5779998
DM
1/*
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>
9966ddaf 18#include <linux/slab.h>
44dcbbb1 19#include <linux/bitrev.h>
edcd3633 20#include <linux/ratelimit.h>
e5779998
DM
21#include <linux/usb.h>
22#include <linux/usb/audio.h>
7e847894 23#include <linux/usb/audio-v2.h>
e5779998
DM
24
25#include <sound/core.h>
26#include <sound/pcm.h>
27#include <sound/pcm_params.h>
28
29#include "usbaudio.h"
30#include "card.h"
31#include "quirks.h"
32#include "debug.h"
c731bc96 33#include "endpoint.h"
e5779998
DM
34#include "helper.h"
35#include "pcm.h"
79f920fb 36#include "clock.h"
88a8516a 37#include "power.h"
aebb2b89 38#include "media.h"
e5779998 39
edcd3633
DM
40#define SUBSTREAM_FLAG_DATA_EP_STARTED 0
41#define SUBSTREAM_FLAG_SYNC_EP_STARTED 1
42
294c4fb8
PLB
43/* return the estimated delay based on USB frame counters */
44snd_pcm_uframes_t snd_usb_pcm_delay(struct snd_usb_substream *subs,
45 unsigned int rate)
46{
47 int current_frame_number;
48 int frame_diff;
49 int est_delay;
50
48779a0b
TI
51 if (!subs->last_delay)
52 return 0; /* short path */
53
294c4fb8
PLB
54 current_frame_number = usb_get_current_frame_number(subs->dev);
55 /*
56 * HCD implementations use different widths, use lower 8 bits.
57 * The delay will be managed up to 256ms, which is more than
58 * enough
59 */
60 frame_diff = (current_frame_number - subs->last_frame_number) & 0xff;
61
62 /* Approximation based on number of samples per USB frame (ms),
63 some truncation for 44.1 but the estimate is good enough */
e4cc6153
PLB
64 est_delay = frame_diff * rate / 1000;
65 if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK)
66 est_delay = subs->last_delay - est_delay;
67 else
68 est_delay = subs->last_delay + est_delay;
69
294c4fb8
PLB
70 if (est_delay < 0)
71 est_delay = 0;
72 return est_delay;
73}
74
e5779998
DM
75/*
76 * return the current pcm pointer. just based on the hwptr_done value.
77 */
78static snd_pcm_uframes_t snd_usb_pcm_pointer(struct snd_pcm_substream *substream)
79{
80 struct snd_usb_substream *subs;
81 unsigned int hwptr_done;
82
83 subs = (struct snd_usb_substream *)substream->runtime->private_data;
47ab1545 84 if (atomic_read(&subs->stream->chip->shutdown))
978520b7 85 return SNDRV_PCM_POS_XRUN;
e5779998
DM
86 spin_lock(&subs->lock);
87 hwptr_done = subs->hwptr_done;
e4cc6153 88 substream->runtime->delay = snd_usb_pcm_delay(subs,
294c4fb8 89 substream->runtime->rate);
e5779998
DM
90 spin_unlock(&subs->lock);
91 return hwptr_done / (substream->runtime->frame_bits >> 3);
92}
93
94/*
95 * find a matching audio format
96 */
61a70950 97static struct audioformat *find_format(struct snd_usb_substream *subs)
e5779998 98{
88766f04 99 struct audioformat *fp;
e5779998
DM
100 struct audioformat *found = NULL;
101 int cur_attr = 0, attr;
102
88766f04 103 list_for_each_entry(fp, &subs->fmt_list, list) {
74c34ca1 104 if (!(fp->formats & pcm_format_to_bits(subs->pcm_format)))
015eb0b0 105 continue;
61a70950 106 if (fp->channels != subs->channels)
e5779998 107 continue;
61a70950
DR
108 if (subs->cur_rate < fp->rate_min ||
109 subs->cur_rate > fp->rate_max)
e5779998
DM
110 continue;
111 if (! (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)) {
112 unsigned int i;
113 for (i = 0; i < fp->nr_rates; i++)
61a70950 114 if (fp->rate_table[i] == subs->cur_rate)
e5779998
DM
115 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
767d75ad
DM
154static int init_pitch_v1(struct snd_usb_audio *chip, int iface,
155 struct usb_host_interface *alts,
156 struct audioformat *fmt)
157{
158 struct usb_device *dev = chip->dev;
159 unsigned int ep;
160 unsigned char data[1];
161 int err;
162
447d6275
TI
163 if (get_iface_desc(alts)->bNumEndpoints < 1)
164 return -EINVAL;
767d75ad
DM
165 ep = get_endpoint(alts, 0)->bEndpointAddress;
166
767d75ad
DM
167 data[0] = 1;
168 if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC_SET_CUR,
169 USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT,
170 UAC_EP_CS_ATTR_PITCH_CONTROL << 8, ep,
17d900c4 171 data, sizeof(data))) < 0) {
0ba41d91
TI
172 usb_audio_err(chip, "%d:%d: cannot set enable PITCH\n",
173 iface, ep);
767d75ad
DM
174 return err;
175 }
176
177 return 0;
178}
e5779998 179
92c25611
DM
180static int init_pitch_v2(struct snd_usb_audio *chip, int iface,
181 struct usb_host_interface *alts,
182 struct audioformat *fmt)
183{
184 struct usb_device *dev = chip->dev;
185 unsigned char data[1];
92c25611
DM
186 int err;
187
92c25611
DM
188 data[0] = 1;
189 if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC2_CS_CUR,
190 USB_TYPE_CLASS | USB_RECIP_ENDPOINT | USB_DIR_OUT,
191 UAC2_EP_CS_PITCH << 8, 0,
17d900c4 192 data, sizeof(data))) < 0) {
0ba41d91
TI
193 usb_audio_err(chip, "%d:%d: cannot set enable PITCH (v2)\n",
194 iface, fmt->altsetting);
92c25611
DM
195 return err;
196 }
197
198 return 0;
199}
200
e5779998 201/*
92c25611 202 * initialize the pitch control and sample rate
e5779998 203 */
767d75ad 204int snd_usb_init_pitch(struct snd_usb_audio *chip, int iface,
e5779998
DM
205 struct usb_host_interface *alts,
206 struct audioformat *fmt)
207{
92c25611
DM
208 /* if endpoint doesn't have pitch control, bail out */
209 if (!(fmt->attributes & UAC_EP_CS_ATTR_PITCH_CONTROL))
210 return 0;
211
8f898e92 212 switch (fmt->protocol) {
767d75ad 213 case UAC_VERSION_1:
a2acad82 214 default:
767d75ad
DM
215 return init_pitch_v1(chip, iface, alts, fmt);
216
217 case UAC_VERSION_2:
92c25611 218 return init_pitch_v2(chip, iface, alts, fmt);
767d75ad 219 }
767d75ad
DM
220}
221
a9bb3626 222static int start_endpoints(struct snd_usb_substream *subs, bool can_sleep)
edcd3633
DM
223{
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
0ba41d91 232 dev_dbg(&subs->dev->dev, "Starting data EP @%p\n", ep);
edcd3633
DM
233
234 ep->data_subs = subs;
015618b9 235 err = snd_usb_endpoint_start(ep, can_sleep);
edcd3633
DM
236 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
2e4a263c 246 if (subs->data_endpoint->iface != subs->sync_endpoint->iface ||
df23a246 247 subs->data_endpoint->altsetting != subs->sync_endpoint->altsetting) {
2e4a263c
DM
248 err = usb_set_interface(subs->dev,
249 subs->sync_endpoint->iface,
df23a246 250 subs->sync_endpoint->altsetting);
2e4a263c 251 if (err < 0) {
06613f54 252 clear_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags);
0ba41d91
TI
253 dev_err(&subs->dev->dev,
254 "%d:%d: cannot set interface (%d)\n",
2e4a263c 255 subs->sync_endpoint->iface,
df23a246 256 subs->sync_endpoint->altsetting, err);
2e4a263c
DM
257 return -EIO;
258 }
259 }
260
0ba41d91 261 dev_dbg(&subs->dev->dev, "Starting sync EP @%p\n", ep);
edcd3633
DM
262
263 ep->sync_slave = subs->data_endpoint;
015618b9 264 err = snd_usb_endpoint_start(ep, can_sleep);
edcd3633
DM
265 if (err < 0) {
266 clear_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags);
267 return err;
268 }
269 }
270
271 return 0;
272}
273
a9bb3626 274static void stop_endpoints(struct snd_usb_substream *subs, bool wait)
edcd3633
DM
275{
276 if (test_and_clear_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags))
b2eb950d 277 snd_usb_endpoint_stop(subs->sync_endpoint);
edcd3633
DM
278
279 if (test_and_clear_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags))
b2eb950d
TI
280 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 }
edcd3633
DM
286}
287
ba7c2be1
CL
288static int search_roland_implicit_fb(struct usb_device *dev, int ifnum,
289 unsigned int altsetting,
290 struct usb_host_interface **alts,
291 unsigned int *ep)
292{
293 struct usb_interface *iface;
294 struct usb_interface_descriptor *altsd;
295 struct usb_endpoint_descriptor *epd;
296
297 iface = usb_ifnum_to_if(dev, ifnum);
298 if (!iface || iface->num_altsetting < altsetting + 1)
299 return -ENOENT;
300 *alts = &iface->altsetting[altsetting];
301 altsd = get_iface_desc(*alts);
302 if (altsd->bAlternateSetting != altsetting ||
303 altsd->bInterfaceClass != USB_CLASS_VENDOR_SPEC ||
304 (altsd->bInterfaceSubClass != 2 &&
305 altsd->bInterfaceProtocol != 2 ) ||
306 altsd->bNumEndpoints < 1)
307 return -ENOENT;
308 epd = get_endpoint(*alts, 0);
309 if (!usb_endpoint_is_isoc_in(epd) ||
310 (epd->bmAttributes & USB_ENDPOINT_USAGE_MASK) !=
311 USB_ENDPOINT_USAGE_IMPLICIT_FB)
312 return -ENOENT;
313 *ep = epd->bEndpointAddress;
314 return 0;
315}
316
a60945fd
EZ
317static int set_sync_ep_implicit_fb_quirk(struct snd_usb_substream *subs,
318 struct usb_device *dev,
319 struct usb_interface_descriptor *altsd,
320 unsigned int attr)
e5779998 321{
a60945fd 322 struct usb_host_interface *alts;
e5779998 323 struct usb_interface *iface;
a60945fd 324 unsigned int ep;
c75a8a7a 325
914273c7
EZ
326 /* Implicit feedback sync EPs consumers are always playback EPs */
327 if (subs->direction != SNDRV_PCM_STREAM_PLAYBACK)
328 return 0;
329
c75a8a7a 330 switch (subs->stream->chip->usb_id) {
ca10a7eb 331 case USB_ID(0x0763, 0x2030): /* M-Audio Fast Track C400 */
e9a25e04 332 case USB_ID(0x0763, 0x2031): /* M-Audio Fast Track C600 */
914273c7
EZ
333 ep = 0x81;
334 iface = usb_ifnum_to_if(dev, 3);
ca10a7eb 335
914273c7
EZ
336 if (!iface || iface->num_altsetting == 0)
337 return -EINVAL;
ca10a7eb 338
914273c7
EZ
339 alts = &iface->altsetting[1];
340 goto add_sync_ep;
ca10a7eb 341 break;
c75a8a7a
DM
342 case USB_ID(0x0763, 0x2080): /* M-Audio FastTrack Ultra */
343 case USB_ID(0x0763, 0x2081):
914273c7
EZ
344 ep = 0x81;
345 iface = usb_ifnum_to_if(dev, 2);
c75a8a7a 346
914273c7
EZ
347 if (!iface || iface->num_altsetting == 0)
348 return -EINVAL;
c75a8a7a 349
914273c7
EZ
350 alts = &iface->altsetting[1];
351 goto add_sync_ep;
c75a8a7a 352 }
914273c7 353 if (attr == USB_ENDPOINT_SYNC_ASYNC &&
ba7c2be1
CL
354 altsd->bInterfaceClass == USB_CLASS_VENDOR_SPEC &&
355 altsd->bInterfaceProtocol == 2 &&
356 altsd->bNumEndpoints == 1 &&
357 USB_ID_VENDOR(subs->stream->chip->usb_id) == 0x0582 /* Roland */ &&
358 search_roland_implicit_fb(dev, altsd->bInterfaceNumber + 1,
359 altsd->bAlternateSetting,
360 &alts, &ep) >= 0) {
ba7c2be1
CL
361 goto add_sync_ep;
362 }
edcd3633 363
a60945fd
EZ
364 /* No quirk */
365 return 0;
366
367add_sync_ep:
368 subs->sync_endpoint = snd_usb_add_endpoint(subs->stream->chip,
369 alts, ep, !subs->direction,
88abb8ef 370 SND_USB_ENDPOINT_TYPE_DATA);
a60945fd
EZ
371 if (!subs->sync_endpoint)
372 return -EINVAL;
373
374 subs->data_endpoint->sync_master = subs->sync_endpoint;
375
376 return 0;
377}
378
379static int set_sync_endpoint(struct snd_usb_substream *subs,
380 struct audioformat *fmt,
381 struct usb_device *dev,
382 struct usb_host_interface *alts,
383 struct usb_interface_descriptor *altsd)
384{
385 int is_playback = subs->direction == SNDRV_PCM_STREAM_PLAYBACK;
386 unsigned int ep, attr;
95fec883 387 bool implicit_fb;
a60945fd
EZ
388 int err;
389
390 /* we need a sync pipe in async OUT or adaptive IN mode */
391 /* check the number of EP, since some devices have broken
392 * descriptors which fool us. if it has only one EP,
393 * assume it as adaptive-out or sync-in.
394 */
395 attr = fmt->ep_attr & USB_ENDPOINT_SYNCTYPE;
396
63018447
PLB
397 if ((is_playback && (attr != USB_ENDPOINT_SYNC_ASYNC)) ||
398 (!is_playback && (attr != USB_ENDPOINT_SYNC_ADAPTIVE))) {
399
400 /*
401 * In these modes the notion of sync_endpoint is irrelevant.
402 * Reset pointers to avoid using stale data from previously
403 * used settings, e.g. when configuration and endpoints were
404 * changed
405 */
406
407 subs->sync_endpoint = NULL;
408 subs->data_endpoint->sync_master = NULL;
409 }
410
a60945fd
EZ
411 err = set_sync_ep_implicit_fb_quirk(subs, dev, altsd, attr);
412 if (err < 0)
413 return err;
414
f34d0650
EZ
415 if (altsd->bNumEndpoints < 2)
416 return 0;
c75a8a7a 417
395ae54b
PLB
418 if ((is_playback && (attr == USB_ENDPOINT_SYNC_SYNC ||
419 attr == USB_ENDPOINT_SYNC_ADAPTIVE)) ||
f34d0650
EZ
420 (!is_playback && attr != USB_ENDPOINT_SYNC_ADAPTIVE))
421 return 0;
edcd3633 422
395ae54b
PLB
423 /*
424 * In case of illegal SYNC_NONE for OUT endpoint, we keep going to see
425 * if we don't find a sync endpoint, as on M-Audio Transit. In case of
426 * error fall back to SYNC mode and don't create sync endpoint
427 */
428
f34d0650
EZ
429 /* check sync-pipe endpoint */
430 /* ... and check descriptor size before accessing bSynchAddress
431 because there is a version of the SB Audigy 2 NX firmware lacking
432 the audio fields in the endpoint descriptors */
433 if ((get_endpoint(alts, 1)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_ISOC ||
434 (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
95fec883 435 get_endpoint(alts, 1)->bSynchAddress != 0)) {
0ba41d91
TI
436 dev_err(&dev->dev,
437 "%d:%d : invalid sync pipe. bmAttributes %02x, bLength %d, bSynchAddress %02x\n",
438 fmt->iface, fmt->altsetting,
f34d0650
EZ
439 get_endpoint(alts, 1)->bmAttributes,
440 get_endpoint(alts, 1)->bLength,
441 get_endpoint(alts, 1)->bSynchAddress);
395ae54b
PLB
442 if (is_playback && attr == USB_ENDPOINT_SYNC_NONE)
443 return 0;
f34d0650
EZ
444 return -EINVAL;
445 }
446 ep = get_endpoint(alts, 1)->bEndpointAddress;
95fec883 447 if (get_endpoint(alts, 0)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
f34d0650
EZ
448 ((is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress | USB_DIR_IN)) ||
449 (!is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress & ~USB_DIR_IN)))) {
0ba41d91
TI
450 dev_err(&dev->dev,
451 "%d:%d : invalid sync pipe. is_playback %d, ep %02x, bSynchAddress %02x\n",
452 fmt->iface, fmt->altsetting,
f34d0650 453 is_playback, ep, get_endpoint(alts, 0)->bSynchAddress);
395ae54b
PLB
454 if (is_playback && attr == USB_ENDPOINT_SYNC_NONE)
455 return 0;
f34d0650 456 return -EINVAL;
edcd3633 457 }
e5779998 458
f34d0650
EZ
459 implicit_fb = (get_endpoint(alts, 1)->bmAttributes & USB_ENDPOINT_USAGE_MASK)
460 == USB_ENDPOINT_USAGE_IMPLICIT_FB;
461
462 subs->sync_endpoint = snd_usb_add_endpoint(subs->stream->chip,
463 alts, ep, !subs->direction,
464 implicit_fb ?
465 SND_USB_ENDPOINT_TYPE_DATA :
466 SND_USB_ENDPOINT_TYPE_SYNC);
395ae54b
PLB
467 if (!subs->sync_endpoint) {
468 if (is_playback && attr == USB_ENDPOINT_SYNC_NONE)
469 return 0;
f34d0650 470 return -EINVAL;
395ae54b 471 }
f34d0650
EZ
472
473 subs->data_endpoint->sync_master = subs->sync_endpoint;
474
71bb64c5
EZ
475 return 0;
476}
477
478/*
479 * find a matching format and set up the interface
480 */
481static int set_format(struct snd_usb_substream *subs, struct audioformat *fmt)
482{
483 struct usb_device *dev = subs->dev;
484 struct usb_host_interface *alts;
485 struct usb_interface_descriptor *altsd;
486 struct usb_interface *iface;
487 int err;
488
489 iface = usb_ifnum_to_if(dev, fmt->iface);
490 if (WARN_ON(!iface))
491 return -EINVAL;
492 alts = &iface->altsetting[fmt->altset_idx];
493 altsd = get_iface_desc(alts);
494 if (WARN_ON(altsd->bAlternateSetting != fmt->altsetting))
495 return -EINVAL;
496
497 if (fmt == subs->cur_audiofmt)
498 return 0;
499
500 /* close the old interface */
501 if (subs->interface >= 0 && subs->interface != fmt->iface) {
502 err = usb_set_interface(subs->dev, subs->interface, 0);
503 if (err < 0) {
0ba41d91
TI
504 dev_err(&dev->dev,
505 "%d:%d: return to setting 0 failed (%d)\n",
506 fmt->iface, fmt->altsetting, err);
71bb64c5
EZ
507 return -EIO;
508 }
509 subs->interface = -1;
510 subs->altset_idx = 0;
511 }
512
513 /* set interface */
514 if (subs->interface != fmt->iface ||
515 subs->altset_idx != fmt->altset_idx) {
6874daad
JK
516
517 err = snd_usb_select_mode_quirk(subs, fmt);
518 if (err < 0)
519 return -EIO;
520
71bb64c5
EZ
521 err = usb_set_interface(dev, fmt->iface, fmt->altsetting);
522 if (err < 0) {
0ba41d91
TI
523 dev_err(&dev->dev,
524 "%d:%d: usb_set_interface failed (%d)\n",
525 fmt->iface, fmt->altsetting, err);
71bb64c5
EZ
526 return -EIO;
527 }
0ba41d91
TI
528 dev_dbg(&dev->dev, "setting usb interface %d:%d\n",
529 fmt->iface, fmt->altsetting);
71bb64c5
EZ
530 subs->interface = fmt->iface;
531 subs->altset_idx = fmt->altset_idx;
532
533 snd_usb_set_interface_quirk(dev);
534 }
535
536 subs->data_endpoint = snd_usb_add_endpoint(subs->stream->chip,
537 alts, fmt->endpoint, subs->direction,
538 SND_USB_ENDPOINT_TYPE_DATA);
539
540 if (!subs->data_endpoint)
541 return -EINVAL;
542
543 err = set_sync_endpoint(subs, fmt, dev, alts, altsd);
544 if (err < 0)
545 return err;
546
d133f2c2
EZ
547 err = snd_usb_init_pitch(subs->stream->chip, fmt->iface, alts, fmt);
548 if (err < 0)
e5779998
DM
549 return err;
550
551 subs->cur_audiofmt = fmt;
552
553 snd_usb_set_format_quirk(subs, fmt);
554
e5779998
DM
555 return 0;
556}
557
0d9741c0
EZ
558/*
559 * Return the score of matching two audioformats.
560 * Veto the audioformat if:
561 * - It has no channels for some reason.
562 * - Requested PCM format is not supported.
563 * - Requested sample rate is not supported.
564 */
0ba41d91
TI
565static int match_endpoint_audioformats(struct snd_usb_substream *subs,
566 struct audioformat *fp,
567 struct audioformat *match, int rate,
568 snd_pcm_format_t pcm_format)
0d9741c0
EZ
569{
570 int i;
571 int score = 0;
572
573 if (fp->channels < 1) {
0ba41d91
TI
574 dev_dbg(&subs->dev->dev,
575 "%s: (fmt @%p) no channels\n", __func__, fp);
0d9741c0
EZ
576 return 0;
577 }
578
74c34ca1 579 if (!(fp->formats & pcm_format_to_bits(pcm_format))) {
0ba41d91
TI
580 dev_dbg(&subs->dev->dev,
581 "%s: (fmt @%p) no match for format %d\n", __func__,
0d9741c0
EZ
582 fp, pcm_format);
583 return 0;
584 }
585
586 for (i = 0; i < fp->nr_rates; i++) {
587 if (fp->rate_table[i] == rate) {
588 score++;
589 break;
590 }
591 }
592 if (!score) {
0ba41d91
TI
593 dev_dbg(&subs->dev->dev,
594 "%s: (fmt @%p) no match for rate %d\n", __func__,
0d9741c0
EZ
595 fp, rate);
596 return 0;
597 }
598
599 if (fp->channels == match->channels)
600 score++;
601
0ba41d91
TI
602 dev_dbg(&subs->dev->dev,
603 "%s: (fmt @%p) score %d\n", __func__, fp, score);
0d9741c0
EZ
604
605 return score;
606}
607
608/*
609 * Configure the sync ep using the rate and pcm format of the data ep.
610 */
611static int configure_sync_endpoint(struct snd_usb_substream *subs)
612{
613 int ret;
614 struct audioformat *fp;
615 struct audioformat *sync_fp = NULL;
616 int cur_score = 0;
617 int sync_period_bytes = subs->period_bytes;
618 struct snd_usb_substream *sync_subs =
619 &subs->stream->substream[subs->direction ^ 1];
620
31be5425
TI
621 if (subs->sync_endpoint->type != SND_USB_ENDPOINT_TYPE_DATA ||
622 !subs->stream)
623 return snd_usb_endpoint_set_params(subs->sync_endpoint,
624 subs->pcm_format,
625 subs->channels,
626 subs->period_bytes,
976b6c06 627 0, 0,
31be5425
TI
628 subs->cur_rate,
629 subs->cur_audiofmt,
630 NULL);
631
0d9741c0
EZ
632 /* Try to find the best matching audioformat. */
633 list_for_each_entry(fp, &sync_subs->fmt_list, list) {
0ba41d91
TI
634 int score = match_endpoint_audioformats(subs,
635 fp, subs->cur_audiofmt,
0d9741c0
EZ
636 subs->cur_rate, subs->pcm_format);
637
638 if (score > cur_score) {
639 sync_fp = fp;
640 cur_score = score;
641 }
642 }
643
644 if (unlikely(sync_fp == NULL)) {
0ba41d91
TI
645 dev_err(&subs->dev->dev,
646 "%s: no valid audioformat for sync ep %x found\n",
0d9741c0
EZ
647 __func__, sync_subs->ep_num);
648 return -EINVAL;
649 }
650
651 /*
652 * Recalculate the period bytes if channel number differ between
653 * data and sync ep audioformat.
654 */
655 if (sync_fp->channels != subs->channels) {
656 sync_period_bytes = (subs->period_bytes / subs->channels) *
657 sync_fp->channels;
0ba41d91
TI
658 dev_dbg(&subs->dev->dev,
659 "%s: adjusted sync ep period bytes (%d -> %d)\n",
0d9741c0
EZ
660 __func__, subs->period_bytes, sync_period_bytes);
661 }
662
663 ret = snd_usb_endpoint_set_params(subs->sync_endpoint,
664 subs->pcm_format,
665 sync_fp->channels,
666 sync_period_bytes,
976b6c06 667 0, 0,
0d9741c0
EZ
668 subs->cur_rate,
669 sync_fp,
670 NULL);
671
672 return ret;
673}
674
61a70950
DR
675/*
676 * configure endpoint params
677 *
678 * called during initial setup and upon resume
679 */
680static int configure_endpoint(struct snd_usb_substream *subs)
681{
682 int ret;
683
61a70950 684 /* format changed */
b0db6063 685 stop_endpoints(subs, true);
61a70950
DR
686 ret = snd_usb_endpoint_set_params(subs->data_endpoint,
687 subs->pcm_format,
688 subs->channels,
689 subs->period_bytes,
976b6c06
AS
690 subs->period_frames,
691 subs->buffer_periods,
61a70950
DR
692 subs->cur_rate,
693 subs->cur_audiofmt,
694 subs->sync_endpoint);
695 if (ret < 0)
978520b7 696 return ret;
61a70950
DR
697
698 if (subs->sync_endpoint)
0d9741c0
EZ
699 ret = configure_sync_endpoint(subs);
700
61a70950
DR
701 return ret;
702}
703
e5779998
DM
704/*
705 * hw_params callback
706 *
707 * allocate a buffer and set the given audio format.
708 *
709 * so far we use a physically linear buffer although packetize transfer
710 * doesn't need a continuous area.
711 * if sg buffer is supported on the later version of alsa, we'll follow
712 * that.
713 */
714static int snd_usb_hw_params(struct snd_pcm_substream *substream,
715 struct snd_pcm_hw_params *hw_params)
716{
717 struct snd_usb_substream *subs = substream->runtime->private_data;
718 struct audioformat *fmt;
61a70950 719 int ret;
e5779998 720
aebb2b89
SK
721 ret = media_snd_start_pipeline(subs);
722 if (ret)
723 return ret;
724
e5779998
DM
725 ret = snd_pcm_lib_alloc_vmalloc_buffer(substream,
726 params_buffer_bytes(hw_params));
727 if (ret < 0)
aebb2b89 728 goto err_ret;
e5779998 729
61a70950
DR
730 subs->pcm_format = params_format(hw_params);
731 subs->period_bytes = params_period_bytes(hw_params);
976b6c06
AS
732 subs->period_frames = params_period_size(hw_params);
733 subs->buffer_periods = params_periods(hw_params);
61a70950
DR
734 subs->channels = params_channels(hw_params);
735 subs->cur_rate = params_rate(hw_params);
736
737 fmt = find_format(subs);
e5779998 738 if (!fmt) {
0ba41d91
TI
739 dev_dbg(&subs->dev->dev,
740 "cannot set format: format = %#x, rate = %d, channels = %d\n",
61a70950 741 subs->pcm_format, subs->cur_rate, subs->channels);
aebb2b89
SK
742 ret = -EINVAL;
743 goto err_ret;
e5779998
DM
744 }
745
47ab1545
TI
746 ret = snd_usb_lock_shutdown(subs->stream->chip);
747 if (ret < 0)
aebb2b89 748 goto err_ret;
47ab1545
TI
749 ret = set_format(subs, fmt);
750 snd_usb_unlock_shutdown(subs->stream->chip);
978520b7 751 if (ret < 0)
aebb2b89 752 goto err_ret;
e5779998 753
61a70950
DR
754 subs->interface = fmt->iface;
755 subs->altset_idx = fmt->altset_idx;
384dc085 756 subs->need_setup_ep = true;
715a1705 757
61a70950 758 return 0;
aebb2b89
SK
759
760err_ret:
761 media_snd_stop_pipeline(subs);
762 return ret;
e5779998
DM
763}
764
765/*
766 * hw_free callback
767 *
768 * reset the audio format and release the buffer
769 */
770static int snd_usb_hw_free(struct snd_pcm_substream *substream)
771{
772 struct snd_usb_substream *subs = substream->runtime->private_data;
773
aebb2b89 774 media_snd_stop_pipeline(subs);
e5779998
DM
775 subs->cur_audiofmt = NULL;
776 subs->cur_rate = 0;
777 subs->period_bytes = 0;
47ab1545 778 if (!snd_usb_lock_shutdown(subs->stream->chip)) {
a9bb3626 779 stop_endpoints(subs, true);
26de5d0a
EZ
780 snd_usb_endpoint_deactivate(subs->sync_endpoint);
781 snd_usb_endpoint_deactivate(subs->data_endpoint);
47ab1545 782 snd_usb_unlock_shutdown(subs->stream->chip);
978520b7 783 }
e5779998
DM
784 return snd_pcm_lib_free_vmalloc_buffer(substream);
785}
786
787/*
788 * prepare callback
789 *
790 * only a few subtle things...
791 */
792static int snd_usb_pcm_prepare(struct snd_pcm_substream *substream)
793{
794 struct snd_pcm_runtime *runtime = substream->runtime;
795 struct snd_usb_substream *subs = runtime->private_data;
61a70950
DR
796 struct usb_host_interface *alts;
797 struct usb_interface *iface;
798 int ret;
e5779998
DM
799
800 if (! subs->cur_audiofmt) {
0ba41d91 801 dev_err(&subs->dev->dev, "no format is specified!\n");
e5779998
DM
802 return -ENXIO;
803 }
804
47ab1545
TI
805 ret = snd_usb_lock_shutdown(subs->stream->chip);
806 if (ret < 0)
807 return ret;
978520b7
TI
808 if (snd_BUG_ON(!subs->data_endpoint)) {
809 ret = -EIO;
810 goto unlock;
811 }
edcd3633 812
f58161ba
TI
813 snd_usb_endpoint_sync_pending_stop(subs->sync_endpoint);
814 snd_usb_endpoint_sync_pending_stop(subs->data_endpoint);
815
61a70950
DR
816 ret = set_format(subs, subs->cur_audiofmt);
817 if (ret < 0)
978520b7 818 goto unlock;
61a70950
DR
819
820 iface = usb_ifnum_to_if(subs->dev, subs->cur_audiofmt->iface);
821 alts = &iface->altsetting[subs->cur_audiofmt->altset_idx];
822 ret = snd_usb_init_sample_rate(subs->stream->chip,
823 subs->cur_audiofmt->iface,
824 alts,
825 subs->cur_audiofmt,
826 subs->cur_rate);
827 if (ret < 0)
978520b7 828 goto unlock;
61a70950 829
384dc085
TI
830 if (subs->need_setup_ep) {
831 ret = configure_endpoint(subs);
832 if (ret < 0)
978520b7 833 goto unlock;
384dc085
TI
834 subs->need_setup_ep = false;
835 }
61a70950 836
e5779998 837 /* some unit conversions in runtime */
edcd3633
DM
838 subs->data_endpoint->maxframesize =
839 bytes_to_frames(runtime, subs->data_endpoint->maxpacksize);
840 subs->data_endpoint->curframesize =
841 bytes_to_frames(runtime, subs->data_endpoint->curpacksize);
e5779998
DM
842
843 /* reset the pointer */
844 subs->hwptr_done = 0;
845 subs->transfer_done = 0;
294c4fb8
PLB
846 subs->last_delay = 0;
847 subs->last_frame_number = 0;
e5779998
DM
848 runtime->delay = 0;
849
edcd3633
DM
850 /* for playback, submit the URBs now; otherwise, the first hwptr_done
851 * updates for all URBs would happen at the same time when starting */
852 if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK)
a9bb3626 853 ret = start_endpoints(subs, true);
edcd3633 854
978520b7 855 unlock:
47ab1545 856 snd_usb_unlock_shutdown(subs->stream->chip);
978520b7 857 return ret;
e5779998
DM
858}
859
860static struct snd_pcm_hardware snd_usb_hardware =
861{
862 .info = SNDRV_PCM_INFO_MMAP |
863 SNDRV_PCM_INFO_MMAP_VALID |
864 SNDRV_PCM_INFO_BATCH |
865 SNDRV_PCM_INFO_INTERLEAVED |
866 SNDRV_PCM_INFO_BLOCK_TRANSFER |
867 SNDRV_PCM_INFO_PAUSE,
868 .buffer_bytes_max = 1024 * 1024,
869 .period_bytes_min = 64,
870 .period_bytes_max = 512 * 1024,
871 .periods_min = 2,
872 .periods_max = 1024,
873};
874
875static int hw_check_valid_format(struct snd_usb_substream *subs,
876 struct snd_pcm_hw_params *params,
877 struct audioformat *fp)
878{
879 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
880 struct snd_interval *ct = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
881 struct snd_mask *fmts = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
882 struct snd_interval *pt = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
015eb0b0 883 struct snd_mask check_fmts;
e5779998
DM
884 unsigned int ptime;
885
886 /* check the format */
015eb0b0
CL
887 snd_mask_none(&check_fmts);
888 check_fmts.bits[0] = (u32)fp->formats;
889 check_fmts.bits[1] = (u32)(fp->formats >> 32);
890 snd_mask_intersect(&check_fmts, fmts);
891 if (snd_mask_empty(&check_fmts)) {
e5779998
DM
892 hwc_debug(" > check: no supported format %d\n", fp->format);
893 return 0;
894 }
895 /* check the channels */
896 if (fp->channels < ct->min || fp->channels > ct->max) {
897 hwc_debug(" > check: no valid channels %d (%d/%d)\n", fp->channels, ct->min, ct->max);
898 return 0;
899 }
900 /* check the rate is within the range */
901 if (fp->rate_min > it->max || (fp->rate_min == it->max && it->openmax)) {
902 hwc_debug(" > check: rate_min %d > max %d\n", fp->rate_min, it->max);
903 return 0;
904 }
905 if (fp->rate_max < it->min || (fp->rate_max == it->min && it->openmin)) {
906 hwc_debug(" > check: rate_max %d < min %d\n", fp->rate_max, it->min);
907 return 0;
908 }
909 /* check whether the period time is >= the data packet interval */
978520b7 910 if (subs->speed != USB_SPEED_FULL) {
e5779998
DM
911 ptime = 125 * (1 << fp->datainterval);
912 if (ptime > pt->max || (ptime == pt->max && pt->openmax)) {
913 hwc_debug(" > check: ptime %u > max %u\n", ptime, pt->max);
914 return 0;
915 }
916 }
917 return 1;
918}
919
920static int hw_rule_rate(struct snd_pcm_hw_params *params,
921 struct snd_pcm_hw_rule *rule)
922{
923 struct snd_usb_substream *subs = rule->private;
88766f04 924 struct audioformat *fp;
e5779998
DM
925 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
926 unsigned int rmin, rmax;
927 int changed;
928
929 hwc_debug("hw_rule_rate: (%d,%d)\n", it->min, it->max);
930 changed = 0;
931 rmin = rmax = 0;
88766f04 932 list_for_each_entry(fp, &subs->fmt_list, list) {
e5779998
DM
933 if (!hw_check_valid_format(subs, params, fp))
934 continue;
935 if (changed++) {
936 if (rmin > fp->rate_min)
937 rmin = fp->rate_min;
938 if (rmax < fp->rate_max)
939 rmax = fp->rate_max;
940 } else {
941 rmin = fp->rate_min;
942 rmax = fp->rate_max;
943 }
944 }
945
946 if (!changed) {
947 hwc_debug(" --> get empty\n");
948 it->empty = 1;
949 return -EINVAL;
950 }
951
952 changed = 0;
953 if (it->min < rmin) {
954 it->min = rmin;
955 it->openmin = 0;
956 changed = 1;
957 }
958 if (it->max > rmax) {
959 it->max = rmax;
960 it->openmax = 0;
961 changed = 1;
962 }
963 if (snd_interval_checkempty(it)) {
964 it->empty = 1;
965 return -EINVAL;
966 }
967 hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
968 return changed;
969}
970
971
972static int hw_rule_channels(struct snd_pcm_hw_params *params,
973 struct snd_pcm_hw_rule *rule)
974{
975 struct snd_usb_substream *subs = rule->private;
88766f04 976 struct audioformat *fp;
e5779998
DM
977 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
978 unsigned int rmin, rmax;
979 int changed;
980
981 hwc_debug("hw_rule_channels: (%d,%d)\n", it->min, it->max);
982 changed = 0;
983 rmin = rmax = 0;
88766f04 984 list_for_each_entry(fp, &subs->fmt_list, list) {
e5779998
DM
985 if (!hw_check_valid_format(subs, params, fp))
986 continue;
987 if (changed++) {
988 if (rmin > fp->channels)
989 rmin = fp->channels;
990 if (rmax < fp->channels)
991 rmax = fp->channels;
992 } else {
993 rmin = fp->channels;
994 rmax = fp->channels;
995 }
996 }
997
998 if (!changed) {
999 hwc_debug(" --> get empty\n");
1000 it->empty = 1;
1001 return -EINVAL;
1002 }
1003
1004 changed = 0;
1005 if (it->min < rmin) {
1006 it->min = rmin;
1007 it->openmin = 0;
1008 changed = 1;
1009 }
1010 if (it->max > rmax) {
1011 it->max = rmax;
1012 it->openmax = 0;
1013 changed = 1;
1014 }
1015 if (snd_interval_checkempty(it)) {
1016 it->empty = 1;
1017 return -EINVAL;
1018 }
1019 hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
1020 return changed;
1021}
1022
1023static int hw_rule_format(struct snd_pcm_hw_params *params,
1024 struct snd_pcm_hw_rule *rule)
1025{
1026 struct snd_usb_substream *subs = rule->private;
88766f04 1027 struct audioformat *fp;
e5779998
DM
1028 struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
1029 u64 fbits;
1030 u32 oldbits[2];
1031 int changed;
1032
1033 hwc_debug("hw_rule_format: %x:%x\n", fmt->bits[0], fmt->bits[1]);
1034 fbits = 0;
88766f04 1035 list_for_each_entry(fp, &subs->fmt_list, list) {
e5779998
DM
1036 if (!hw_check_valid_format(subs, params, fp))
1037 continue;
015eb0b0 1038 fbits |= fp->formats;
e5779998
DM
1039 }
1040
1041 oldbits[0] = fmt->bits[0];
1042 oldbits[1] = fmt->bits[1];
1043 fmt->bits[0] &= (u32)fbits;
1044 fmt->bits[1] &= (u32)(fbits >> 32);
1045 if (!fmt->bits[0] && !fmt->bits[1]) {
1046 hwc_debug(" --> get empty\n");
1047 return -EINVAL;
1048 }
1049 changed = (oldbits[0] != fmt->bits[0] || oldbits[1] != fmt->bits[1]);
1050 hwc_debug(" --> %x:%x (changed = %d)\n", fmt->bits[0], fmt->bits[1], changed);
1051 return changed;
1052}
1053
1054static int hw_rule_period_time(struct snd_pcm_hw_params *params,
1055 struct snd_pcm_hw_rule *rule)
1056{
1057 struct snd_usb_substream *subs = rule->private;
1058 struct audioformat *fp;
1059 struct snd_interval *it;
1060 unsigned char min_datainterval;
1061 unsigned int pmin;
1062 int changed;
1063
1064 it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
1065 hwc_debug("hw_rule_period_time: (%u,%u)\n", it->min, it->max);
1066 min_datainterval = 0xff;
1067 list_for_each_entry(fp, &subs->fmt_list, list) {
1068 if (!hw_check_valid_format(subs, params, fp))
1069 continue;
1070 min_datainterval = min(min_datainterval, fp->datainterval);
1071 }
1072 if (min_datainterval == 0xff) {
a7ce2e0d 1073 hwc_debug(" --> get empty\n");
e5779998
DM
1074 it->empty = 1;
1075 return -EINVAL;
1076 }
1077 pmin = 125 * (1 << min_datainterval);
1078 changed = 0;
1079 if (it->min < pmin) {
1080 it->min = pmin;
1081 it->openmin = 0;
1082 changed = 1;
1083 }
1084 if (snd_interval_checkempty(it)) {
1085 it->empty = 1;
1086 return -EINVAL;
1087 }
1088 hwc_debug(" --> (%u,%u) (changed = %d)\n", it->min, it->max, changed);
1089 return changed;
1090}
1091
1092/*
1093 * If the device supports unusual bit rates, does the request meet these?
1094 */
1095static int snd_usb_pcm_check_knot(struct snd_pcm_runtime *runtime,
1096 struct snd_usb_substream *subs)
1097{
1098 struct audioformat *fp;
0717d0f5 1099 int *rate_list;
e5779998
DM
1100 int count = 0, needs_knot = 0;
1101 int err;
1102
5cd5d7c4
CL
1103 kfree(subs->rate_list.list);
1104 subs->rate_list.list = NULL;
1105
e5779998
DM
1106 list_for_each_entry(fp, &subs->fmt_list, list) {
1107 if (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)
1108 return 0;
1109 count += fp->nr_rates;
1110 if (fp->rates & SNDRV_PCM_RATE_KNOT)
1111 needs_knot = 1;
1112 }
1113 if (!needs_knot)
1114 return 0;
1115
0717d0f5
TI
1116 subs->rate_list.list = rate_list =
1117 kmalloc(sizeof(int) * count, GFP_KERNEL);
8a8d56b2
JJ
1118 if (!subs->rate_list.list)
1119 return -ENOMEM;
1120 subs->rate_list.count = count;
e5779998
DM
1121 subs->rate_list.mask = 0;
1122 count = 0;
1123 list_for_each_entry(fp, &subs->fmt_list, list) {
1124 int i;
1125 for (i = 0; i < fp->nr_rates; i++)
0717d0f5 1126 rate_list[count++] = fp->rate_table[i];
e5779998
DM
1127 }
1128 err = snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1129 &subs->rate_list);
1130 if (err < 0)
1131 return err;
1132
1133 return 0;
1134}
1135
1136
1137/*
1138 * set up the runtime hardware information.
1139 */
1140
1141static int setup_hw_info(struct snd_pcm_runtime *runtime, struct snd_usb_substream *subs)
1142{
88766f04 1143 struct audioformat *fp;
e5779998
DM
1144 unsigned int pt, ptmin;
1145 int param_period_time_if_needed;
1146 int err;
1147
1148 runtime->hw.formats = subs->formats;
1149
1150 runtime->hw.rate_min = 0x7fffffff;
1151 runtime->hw.rate_max = 0;
1152 runtime->hw.channels_min = 256;
1153 runtime->hw.channels_max = 0;
1154 runtime->hw.rates = 0;
1155 ptmin = UINT_MAX;
1156 /* check min/max rates and channels */
88766f04 1157 list_for_each_entry(fp, &subs->fmt_list, list) {
e5779998
DM
1158 runtime->hw.rates |= fp->rates;
1159 if (runtime->hw.rate_min > fp->rate_min)
1160 runtime->hw.rate_min = fp->rate_min;
1161 if (runtime->hw.rate_max < fp->rate_max)
1162 runtime->hw.rate_max = fp->rate_max;
1163 if (runtime->hw.channels_min > fp->channels)
1164 runtime->hw.channels_min = fp->channels;
1165 if (runtime->hw.channels_max < fp->channels)
1166 runtime->hw.channels_max = fp->channels;
1167 if (fp->fmt_type == UAC_FORMAT_TYPE_II && fp->frame_size > 0) {
1168 /* FIXME: there might be more than one audio formats... */
1169 runtime->hw.period_bytes_min = runtime->hw.period_bytes_max =
1170 fp->frame_size;
1171 }
1172 pt = 125 * (1 << fp->datainterval);
1173 ptmin = min(ptmin, pt);
1174 }
88a8516a
ON
1175 err = snd_usb_autoresume(subs->stream->chip);
1176 if (err < 0)
1177 return err;
e5779998
DM
1178
1179 param_period_time_if_needed = SNDRV_PCM_HW_PARAM_PERIOD_TIME;
978520b7 1180 if (subs->speed == USB_SPEED_FULL)
e5779998
DM
1181 /* full speed devices have fixed data packet interval */
1182 ptmin = 1000;
1183 if (ptmin == 1000)
1184 /* if period time doesn't go below 1 ms, no rules needed */
1185 param_period_time_if_needed = -1;
1186 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1187 ptmin, UINT_MAX);
1188
1189 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1190 hw_rule_rate, subs,
1191 SNDRV_PCM_HW_PARAM_FORMAT,
1192 SNDRV_PCM_HW_PARAM_CHANNELS,
1193 param_period_time_if_needed,
1194 -1)) < 0)
88a8516a 1195 goto rep_err;
e5779998
DM
1196 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
1197 hw_rule_channels, subs,
1198 SNDRV_PCM_HW_PARAM_FORMAT,
1199 SNDRV_PCM_HW_PARAM_RATE,
1200 param_period_time_if_needed,
1201 -1)) < 0)
88a8516a 1202 goto rep_err;
e5779998
DM
1203 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
1204 hw_rule_format, subs,
1205 SNDRV_PCM_HW_PARAM_RATE,
1206 SNDRV_PCM_HW_PARAM_CHANNELS,
1207 param_period_time_if_needed,
1208 -1)) < 0)
88a8516a 1209 goto rep_err;
e5779998
DM
1210 if (param_period_time_if_needed >= 0) {
1211 err = snd_pcm_hw_rule_add(runtime, 0,
1212 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1213 hw_rule_period_time, subs,
1214 SNDRV_PCM_HW_PARAM_FORMAT,
1215 SNDRV_PCM_HW_PARAM_CHANNELS,
1216 SNDRV_PCM_HW_PARAM_RATE,
1217 -1);
1218 if (err < 0)
88a8516a 1219 goto rep_err;
e5779998
DM
1220 }
1221 if ((err = snd_usb_pcm_check_knot(runtime, subs)) < 0)
88a8516a 1222 goto rep_err;
e5779998 1223 return 0;
88a8516a
ON
1224
1225rep_err:
1226 snd_usb_autosuspend(subs->stream->chip);
1227 return err;
e5779998
DM
1228}
1229
1230static int snd_usb_pcm_open(struct snd_pcm_substream *substream, int direction)
1231{
1232 struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
1233 struct snd_pcm_runtime *runtime = substream->runtime;
1234 struct snd_usb_substream *subs = &as->substream[direction];
aebb2b89 1235 int ret;
e5779998
DM
1236
1237 subs->interface = -1;
e11b4e0e 1238 subs->altset_idx = 0;
e5779998
DM
1239 runtime->hw = snd_usb_hardware;
1240 runtime->private_data = subs;
1241 subs->pcm_substream = substream;
88a8516a 1242 /* runtime PM is also done there */
d24f5061
DM
1243
1244 /* initialize DSD/DOP context */
1245 subs->dsd_dop.byte_idx = 0;
1246 subs->dsd_dop.channel = 0;
1247 subs->dsd_dop.marker = 1;
1248
aebb2b89
SK
1249 ret = setup_hw_info(runtime, subs);
1250 if (ret == 0)
1251 ret = media_snd_stream_init(subs, as->pcm, direction);
1252 if (ret)
1253 snd_usb_autosuspend(subs->stream->chip);
1254 return ret;
e5779998
DM
1255}
1256
1257static int snd_usb_pcm_close(struct snd_pcm_substream *substream, int direction)
1258{
1259 struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
1260 struct snd_usb_substream *subs = &as->substream[direction];
1261
b0db6063 1262 stop_endpoints(subs, true);
aebb2b89 1263 media_snd_stop_pipeline(subs);
68e67f40 1264
47ab1545
TI
1265 if (subs->interface >= 0 &&
1266 !snd_usb_lock_shutdown(subs->stream->chip)) {
68e67f40
DM
1267 usb_set_interface(subs->dev, subs->interface, 0);
1268 subs->interface = -1;
47ab1545 1269 snd_usb_unlock_shutdown(subs->stream->chip);
68e67f40
DM
1270 }
1271
e5779998 1272 subs->pcm_substream = NULL;
88a8516a 1273 snd_usb_autosuspend(subs->stream->chip);
edcd3633 1274
68e67f40 1275 return 0;
edcd3633
DM
1276}
1277
1278/* Since a URB can handle only a single linear buffer, we must use double
1279 * buffering when the data to be transferred overflows the buffer boundary.
1280 * To avoid inconsistencies when updating hwptr_done, we use double buffering
1281 * for all URBs.
1282 */
1283static void retire_capture_urb(struct snd_usb_substream *subs,
1284 struct urb *urb)
1285{
1286 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1287 unsigned int stride, frames, bytes, oldptr;
1288 int i, period_elapsed = 0;
1289 unsigned long flags;
1290 unsigned char *cp;
e4cc6153
PLB
1291 int current_frame_number;
1292
1293 /* read frame number here, update pointer in critical section */
1294 current_frame_number = usb_get_current_frame_number(subs->dev);
edcd3633
DM
1295
1296 stride = runtime->frame_bits >> 3;
1297
1298 for (i = 0; i < urb->number_of_packets; i++) {
1539d4f8 1299 cp = (unsigned char *)urb->transfer_buffer + urb->iso_frame_desc[i].offset + subs->pkt_offset_adj;
edcd3633 1300 if (urb->iso_frame_desc[i].status && printk_ratelimit()) {
0ba41d91
TI
1301 dev_dbg(&subs->dev->dev, "frame %d active: %d\n",
1302 i, urb->iso_frame_desc[i].status);
edcd3633
DM
1303 // continue;
1304 }
1305 bytes = urb->iso_frame_desc[i].actual_length;
1306 frames = bytes / stride;
1307 if (!subs->txfr_quirk)
1308 bytes = frames * stride;
1309 if (bytes % (runtime->sample_bits >> 3) != 0) {
edcd3633 1310 int oldbytes = bytes;
edcd3633 1311 bytes = frames * stride;
0ba41d91
TI
1312 dev_warn(&subs->dev->dev,
1313 "Corrected urb data len. %d->%d\n",
edcd3633
DM
1314 oldbytes, bytes);
1315 }
1316 /* update the current pointer */
1317 spin_lock_irqsave(&subs->lock, flags);
1318 oldptr = subs->hwptr_done;
1319 subs->hwptr_done += bytes;
1320 if (subs->hwptr_done >= runtime->buffer_size * stride)
1321 subs->hwptr_done -= runtime->buffer_size * stride;
1322 frames = (bytes + (oldptr % stride)) / stride;
1323 subs->transfer_done += frames;
1324 if (subs->transfer_done >= runtime->period_size) {
1325 subs->transfer_done -= runtime->period_size;
1326 period_elapsed = 1;
1327 }
e4cc6153
PLB
1328 /* capture delay is by construction limited to one URB,
1329 * reset delays here
1330 */
1331 runtime->delay = subs->last_delay = 0;
1332
1333 /* realign last_frame_number */
1334 subs->last_frame_number = current_frame_number;
1335 subs->last_frame_number &= 0xFF; /* keep 8 LSBs */
1336
edcd3633
DM
1337 spin_unlock_irqrestore(&subs->lock, flags);
1338 /* copy a data chunk */
1339 if (oldptr + bytes > runtime->buffer_size * stride) {
1340 unsigned int bytes1 =
1341 runtime->buffer_size * stride - oldptr;
1342 memcpy(runtime->dma_area + oldptr, cp, bytes1);
1343 memcpy(runtime->dma_area, cp + bytes1, bytes - bytes1);
1344 } else {
1345 memcpy(runtime->dma_area + oldptr, cp, bytes);
1346 }
1347 }
1348
1349 if (period_elapsed)
1350 snd_pcm_period_elapsed(subs->pcm_substream);
1351}
1352
d24f5061
DM
1353static inline void fill_playback_urb_dsd_dop(struct snd_usb_substream *subs,
1354 struct urb *urb, unsigned int bytes)
1355{
1356 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1357 unsigned int stride = runtime->frame_bits >> 3;
1358 unsigned int dst_idx = 0;
1359 unsigned int src_idx = subs->hwptr_done;
1360 unsigned int wrap = runtime->buffer_size * stride;
1361 u8 *dst = urb->transfer_buffer;
1362 u8 *src = runtime->dma_area;
1363 u8 marker[] = { 0x05, 0xfa };
1364
1365 /*
1366 * The DSP DOP format defines a way to transport DSD samples over
1367 * normal PCM data endpoints. It requires stuffing of marker bytes
1368 * (0x05 and 0xfa, alternating per sample frame), and then expects
1369 * 2 additional bytes of actual payload. The whole frame is stored
1370 * LSB.
1371 *
1372 * Hence, for a stereo transport, the buffer layout looks like this,
1373 * where L refers to left channel samples and R to right.
1374 *
1375 * L1 L2 0x05 R1 R2 0x05 L3 L4 0xfa R3 R4 0xfa
1376 * L5 L6 0x05 R5 R6 0x05 L7 L8 0xfa R7 R8 0xfa
1377 * .....
1378 *
1379 */
1380
1381 while (bytes--) {
1382 if (++subs->dsd_dop.byte_idx == 3) {
1383 /* frame boundary? */
1384 dst[dst_idx++] = marker[subs->dsd_dop.marker];
1385 src_idx += 2;
1386 subs->dsd_dop.byte_idx = 0;
1387
1388 if (++subs->dsd_dop.channel % runtime->channels == 0) {
1389 /* alternate the marker */
1390 subs->dsd_dop.marker++;
1391 subs->dsd_dop.marker %= ARRAY_SIZE(marker);
1392 subs->dsd_dop.channel = 0;
1393 }
1394 } else {
1395 /* stuff the DSD payload */
1396 int idx = (src_idx + subs->dsd_dop.byte_idx - 1) % wrap;
44dcbbb1
DM
1397
1398 if (subs->cur_audiofmt->dsd_bitrev)
1399 dst[dst_idx++] = bitrev8(src[idx]);
1400 else
1401 dst[dst_idx++] = src[idx];
1402
d24f5061
DM
1403 subs->hwptr_done++;
1404 }
1405 }
4c4e4391
RW
1406 if (subs->hwptr_done >= runtime->buffer_size * stride)
1407 subs->hwptr_done -= runtime->buffer_size * stride;
d24f5061
DM
1408}
1409
b97a9369
RW
1410static void copy_to_urb(struct snd_usb_substream *subs, struct urb *urb,
1411 int offset, int stride, unsigned int bytes)
07a40c2f
RW
1412{
1413 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1414
1415 if (subs->hwptr_done + bytes > runtime->buffer_size * stride) {
1416 /* err, the transferred area goes over buffer boundary. */
1417 unsigned int bytes1 =
1418 runtime->buffer_size * stride - subs->hwptr_done;
b97a9369 1419 memcpy(urb->transfer_buffer + offset,
07a40c2f 1420 runtime->dma_area + subs->hwptr_done, bytes1);
b97a9369 1421 memcpy(urb->transfer_buffer + offset + bytes1,
07a40c2f
RW
1422 runtime->dma_area, bytes - bytes1);
1423 } else {
b97a9369 1424 memcpy(urb->transfer_buffer + offset,
07a40c2f
RW
1425 runtime->dma_area + subs->hwptr_done, bytes);
1426 }
1427 subs->hwptr_done += bytes;
4c4e4391
RW
1428 if (subs->hwptr_done >= runtime->buffer_size * stride)
1429 subs->hwptr_done -= runtime->buffer_size * stride;
07a40c2f
RW
1430}
1431
e0570446
RW
1432static unsigned int copy_to_urb_quirk(struct snd_usb_substream *subs,
1433 struct urb *urb, int stride,
1434 unsigned int bytes)
1435{
1436 __le32 packet_length;
1437 int i;
1438
1439 /* Put __le32 length descriptor at start of each packet. */
1440 for (i = 0; i < urb->number_of_packets; i++) {
1441 unsigned int length = urb->iso_frame_desc[i].length;
1442 unsigned int offset = urb->iso_frame_desc[i].offset;
1443
1444 packet_length = cpu_to_le32(length);
1445 offset += i * sizeof(packet_length);
1446 urb->iso_frame_desc[i].offset = offset;
1447 urb->iso_frame_desc[i].length += sizeof(packet_length);
1448 memcpy(urb->transfer_buffer + offset,
1449 &packet_length, sizeof(packet_length));
1450 copy_to_urb(subs, urb, offset + sizeof(packet_length),
1451 stride, length);
1452 }
1453 /* Adjust transfer size accordingly. */
1454 bytes += urb->number_of_packets * sizeof(packet_length);
1455 return bytes;
1456}
1457
edcd3633
DM
1458static void prepare_playback_urb(struct snd_usb_substream *subs,
1459 struct urb *urb)
1460{
1461 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
245baf98 1462 struct snd_usb_endpoint *ep = subs->data_endpoint;
edcd3633
DM
1463 struct snd_urb_ctx *ctx = urb->context;
1464 unsigned int counts, frames, bytes;
1465 int i, stride, period_elapsed = 0;
1466 unsigned long flags;
1467
1468 stride = runtime->frame_bits >> 3;
1469
1470 frames = 0;
1471 urb->number_of_packets = 0;
1472 spin_lock_irqsave(&subs->lock, flags);
976b6c06 1473 subs->frame_limit += ep->max_urb_frames;
edcd3633 1474 for (i = 0; i < ctx->packets; i++) {
245baf98
DM
1475 if (ctx->packet_size[i])
1476 counts = ctx->packet_size[i];
1477 else
1478 counts = snd_usb_endpoint_next_packet_size(ep);
1479
edcd3633 1480 /* set up descriptor */
8a2a74d2
DM
1481 urb->iso_frame_desc[i].offset = frames * ep->stride;
1482 urb->iso_frame_desc[i].length = counts * ep->stride;
edcd3633
DM
1483 frames += counts;
1484 urb->number_of_packets++;
1485 subs->transfer_done += counts;
1486 if (subs->transfer_done >= runtime->period_size) {
1487 subs->transfer_done -= runtime->period_size;
976b6c06 1488 subs->frame_limit = 0;
edcd3633
DM
1489 period_elapsed = 1;
1490 if (subs->fmt_type == UAC_FORMAT_TYPE_II) {
1491 if (subs->transfer_done > 0) {
1492 /* FIXME: fill-max mode is not
1493 * supported yet */
1494 frames -= subs->transfer_done;
1495 counts -= subs->transfer_done;
1496 urb->iso_frame_desc[i].length =
8a2a74d2 1497 counts * ep->stride;
edcd3633
DM
1498 subs->transfer_done = 0;
1499 }
1500 i++;
1501 if (i < ctx->packets) {
1502 /* add a transfer delimiter */
1503 urb->iso_frame_desc[i].offset =
8a2a74d2 1504 frames * ep->stride;
edcd3633
DM
1505 urb->iso_frame_desc[i].length = 0;
1506 urb->number_of_packets++;
1507 }
1508 break;
1509 }
1510 }
976b6c06
AS
1511 /* finish at the period boundary or after enough frames */
1512 if ((period_elapsed ||
1513 subs->transfer_done >= subs->frame_limit) &&
1514 !snd_usb_endpoint_implicit_feedback_sink(ep))
edcd3633
DM
1515 break;
1516 }
8a2a74d2 1517 bytes = frames * ep->stride;
d24f5061
DM
1518
1519 if (unlikely(subs->pcm_format == SNDRV_PCM_FORMAT_DSD_U16_LE &&
1520 subs->cur_audiofmt->dsd_dop)) {
1521 fill_playback_urb_dsd_dop(subs, urb, bytes);
44dcbbb1
DM
1522 } else if (unlikely(subs->pcm_format == SNDRV_PCM_FORMAT_DSD_U8 &&
1523 subs->cur_audiofmt->dsd_bitrev)) {
1524 /* bit-reverse the bytes */
1525 u8 *buf = urb->transfer_buffer;
1526 for (i = 0; i < bytes; i++) {
1527 int idx = (subs->hwptr_done + i)
1528 % (runtime->buffer_size * stride);
1529 buf[i] = bitrev8(runtime->dma_area[idx]);
1530 }
1531
1532 subs->hwptr_done += bytes;
4c4e4391
RW
1533 if (subs->hwptr_done >= runtime->buffer_size * stride)
1534 subs->hwptr_done -= runtime->buffer_size * stride;
edcd3633 1535 } else {
d24f5061 1536 /* usual PCM */
e0570446
RW
1537 if (!subs->tx_length_quirk)
1538 copy_to_urb(subs, urb, 0, stride, bytes);
1539 else
1540 bytes = copy_to_urb_quirk(subs, urb, stride, bytes);
1541 /* bytes is now amount of outgoing data */
edcd3633 1542 }
d24f5061 1543
fbcfbf5f
DM
1544 /* update delay with exact number of samples queued */
1545 runtime->delay = subs->last_delay;
edcd3633 1546 runtime->delay += frames;
fbcfbf5f
DM
1547 subs->last_delay = runtime->delay;
1548
1549 /* realign last_frame_number */
1550 subs->last_frame_number = usb_get_current_frame_number(subs->dev);
1551 subs->last_frame_number &= 0xFF; /* keep 8 LSBs */
1552
ea33d359
PLB
1553 if (subs->trigger_tstamp_pending_update) {
1554 /* this is the first actual URB submitted,
1555 * update trigger timestamp to reflect actual start time
1556 */
1557 snd_pcm_gettime(runtime, &runtime->trigger_tstamp);
1558 subs->trigger_tstamp_pending_update = false;
1559 }
1560
edcd3633
DM
1561 spin_unlock_irqrestore(&subs->lock, flags);
1562 urb->transfer_buffer_length = bytes;
1563 if (period_elapsed)
1564 snd_pcm_period_elapsed(subs->pcm_substream);
1565}
1566
1567/*
1568 * process after playback data complete
1569 * - decrease the delay count again
1570 */
1571static void retire_playback_urb(struct snd_usb_substream *subs,
1572 struct urb *urb)
1573{
1574 unsigned long flags;
1575 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
8a2a74d2
DM
1576 struct snd_usb_endpoint *ep = subs->data_endpoint;
1577 int processed = urb->transfer_buffer_length / ep->stride;
fbcfbf5f 1578 int est_delay;
edcd3633 1579
1213a205
TI
1580 /* ignore the delay accounting when procssed=0 is given, i.e.
1581 * silent payloads are procssed before handling the actual data
1582 */
1583 if (!processed)
1584 return;
1585
edcd3633 1586 spin_lock_irqsave(&subs->lock, flags);
48779a0b
TI
1587 if (!subs->last_delay)
1588 goto out; /* short path */
1589
fbcfbf5f
DM
1590 est_delay = snd_usb_pcm_delay(subs, runtime->rate);
1591 /* update delay with exact number of samples played */
1592 if (processed > subs->last_delay)
1593 subs->last_delay = 0;
edcd3633 1594 else
fbcfbf5f
DM
1595 subs->last_delay -= processed;
1596 runtime->delay = subs->last_delay;
1597
1598 /*
1599 * Report when delay estimate is off by more than 2ms.
1600 * The error should be lower than 2ms since the estimate relies
1601 * on two reads of a counter updated every ms.
1602 */
b7a77235
SE
1603 if (abs(est_delay - subs->last_delay) * 1000 > runtime->rate * 2)
1604 dev_dbg_ratelimited(&subs->dev->dev,
0ba41d91 1605 "delay: estimated %d, actual %d\n",
fbcfbf5f
DM
1606 est_delay, subs->last_delay);
1607
48779a0b
TI
1608 if (!subs->running) {
1609 /* update last_frame_number for delay counting here since
1610 * prepare_playback_urb won't be called during pause
1611 */
1612 subs->last_frame_number =
1613 usb_get_current_frame_number(subs->dev) & 0xff;
1614 }
1615
1616 out:
edcd3633 1617 spin_unlock_irqrestore(&subs->lock, flags);
e5779998
DM
1618}
1619
1620static int snd_usb_playback_open(struct snd_pcm_substream *substream)
1621{
1622 return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_PLAYBACK);
1623}
1624
1625static int snd_usb_playback_close(struct snd_pcm_substream *substream)
1626{
1627 return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_PLAYBACK);
1628}
1629
1630static int snd_usb_capture_open(struct snd_pcm_substream *substream)
1631{
1632 return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_CAPTURE);
1633}
1634
1635static int snd_usb_capture_close(struct snd_pcm_substream *substream)
1636{
1637 return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_CAPTURE);
1638}
1639
edcd3633
DM
1640static int snd_usb_substream_playback_trigger(struct snd_pcm_substream *substream,
1641 int cmd)
1642{
1643 struct snd_usb_substream *subs = substream->runtime->private_data;
1644
1645 switch (cmd) {
1646 case SNDRV_PCM_TRIGGER_START:
ea33d359 1647 subs->trigger_tstamp_pending_update = true;
edcd3633
DM
1648 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1649 subs->data_endpoint->prepare_data_urb = prepare_playback_urb;
1650 subs->data_endpoint->retire_data_urb = retire_playback_urb;
97f8d3b6 1651 subs->running = 1;
edcd3633
DM
1652 return 0;
1653 case SNDRV_PCM_TRIGGER_STOP:
a9bb3626 1654 stop_endpoints(subs, false);
97f8d3b6 1655 subs->running = 0;
edcd3633
DM
1656 return 0;
1657 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1658 subs->data_endpoint->prepare_data_urb = NULL;
48779a0b
TI
1659 /* keep retire_data_urb for delay calculation */
1660 subs->data_endpoint->retire_data_urb = retire_playback_urb;
97f8d3b6 1661 subs->running = 0;
edcd3633
DM
1662 return 0;
1663 }
1664
1665 return -EINVAL;
1666}
1667
afe25967
DM
1668static int snd_usb_substream_capture_trigger(struct snd_pcm_substream *substream,
1669 int cmd)
edcd3633
DM
1670{
1671 int err;
1672 struct snd_usb_substream *subs = substream->runtime->private_data;
1673
1674 switch (cmd) {
1675 case SNDRV_PCM_TRIGGER_START:
a9bb3626 1676 err = start_endpoints(subs, false);
edcd3633
DM
1677 if (err < 0)
1678 return err;
1679
1680 subs->data_endpoint->retire_data_urb = retire_capture_urb;
97f8d3b6 1681 subs->running = 1;
edcd3633
DM
1682 return 0;
1683 case SNDRV_PCM_TRIGGER_STOP:
a9bb3626 1684 stop_endpoints(subs, false);
97f8d3b6 1685 subs->running = 0;
edcd3633
DM
1686 return 0;
1687 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1688 subs->data_endpoint->retire_data_urb = NULL;
97f8d3b6 1689 subs->running = 0;
edcd3633
DM
1690 return 0;
1691 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1692 subs->data_endpoint->retire_data_urb = retire_capture_urb;
97f8d3b6 1693 subs->running = 1;
edcd3633
DM
1694 return 0;
1695 }
1696
1697 return -EINVAL;
1698}
1699
e5779998
DM
1700static struct snd_pcm_ops snd_usb_playback_ops = {
1701 .open = snd_usb_playback_open,
1702 .close = snd_usb_playback_close,
1703 .ioctl = snd_pcm_lib_ioctl,
1704 .hw_params = snd_usb_hw_params,
1705 .hw_free = snd_usb_hw_free,
1706 .prepare = snd_usb_pcm_prepare,
1707 .trigger = snd_usb_substream_playback_trigger,
1708 .pointer = snd_usb_pcm_pointer,
1709 .page = snd_pcm_lib_get_vmalloc_page,
1710 .mmap = snd_pcm_lib_mmap_vmalloc,
1711};
1712
1713static struct snd_pcm_ops snd_usb_capture_ops = {
1714 .open = snd_usb_capture_open,
1715 .close = snd_usb_capture_close,
1716 .ioctl = snd_pcm_lib_ioctl,
1717 .hw_params = snd_usb_hw_params,
1718 .hw_free = snd_usb_hw_free,
1719 .prepare = snd_usb_pcm_prepare,
1720 .trigger = snd_usb_substream_capture_trigger,
1721 .pointer = snd_usb_pcm_pointer,
1722 .page = snd_pcm_lib_get_vmalloc_page,
1723 .mmap = snd_pcm_lib_mmap_vmalloc,
1724};
1725
1726void snd_usb_set_pcm_ops(struct snd_pcm *pcm, int stream)
1727{
1728 snd_pcm_set_ops(pcm, stream,
1729 stream == SNDRV_PCM_STREAM_PLAYBACK ?
1730 &snd_usb_playback_ops : &snd_usb_capture_ops);
1731}
This page took 0.42886 seconds and 5 git commands to generate.