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