]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - sound/usb/pcm.c
ALSA: usb-audio: simplify snd_usb_endpoint_start/stop arguments
[karo-tx-linux.git] / sound / usb / pcm.c
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>
18 #include <linux/slab.h>
19 #include <linux/ratelimit.h>
20 #include <linux/usb.h>
21 #include <linux/usb/audio.h>
22 #include <linux/usb/audio-v2.h>
23
24 #include <sound/core.h>
25 #include <sound/pcm.h>
26 #include <sound/pcm_params.h>
27
28 #include "usbaudio.h"
29 #include "card.h"
30 #include "quirks.h"
31 #include "debug.h"
32 #include "endpoint.h"
33 #include "helper.h"
34 #include "pcm.h"
35 #include "clock.h"
36 #include "power.h"
37
38 #define SUBSTREAM_FLAG_DATA_EP_STARTED  0
39 #define SUBSTREAM_FLAG_SYNC_EP_STARTED  1
40
41 /* return the estimated delay based on USB frame counters */
42 snd_pcm_uframes_t snd_usb_pcm_delay(struct snd_usb_substream *subs,
43                                     unsigned int rate)
44 {
45         int current_frame_number;
46         int frame_diff;
47         int est_delay;
48
49         current_frame_number = usb_get_current_frame_number(subs->dev);
50         /*
51          * HCD implementations use different widths, use lower 8 bits.
52          * The delay will be managed up to 256ms, which is more than
53          * enough
54          */
55         frame_diff = (current_frame_number - subs->last_frame_number) & 0xff;
56
57         /* Approximation based on number of samples per USB frame (ms),
58            some truncation for 44.1 but the estimate is good enough */
59         est_delay =  subs->last_delay - (frame_diff * rate / 1000);
60         if (est_delay < 0)
61                 est_delay = 0;
62         return est_delay;
63 }
64
65 /*
66  * return the current pcm pointer.  just based on the hwptr_done value.
67  */
68 static snd_pcm_uframes_t snd_usb_pcm_pointer(struct snd_pcm_substream *substream)
69 {
70         struct snd_usb_substream *subs;
71         unsigned int hwptr_done;
72
73         subs = (struct snd_usb_substream *)substream->runtime->private_data;
74         if (subs->stream->chip->shutdown)
75                 return SNDRV_PCM_POS_XRUN;
76         spin_lock(&subs->lock);
77         hwptr_done = subs->hwptr_done;
78         substream->runtime->delay = snd_usb_pcm_delay(subs,
79                                                 substream->runtime->rate);
80         spin_unlock(&subs->lock);
81         return hwptr_done / (substream->runtime->frame_bits >> 3);
82 }
83
84 /*
85  * find a matching audio format
86  */
87 static struct audioformat *find_format(struct snd_usb_substream *subs)
88 {
89         struct list_head *p;
90         struct audioformat *found = NULL;
91         int cur_attr = 0, attr;
92
93         list_for_each(p, &subs->fmt_list) {
94                 struct audioformat *fp;
95                 fp = list_entry(p, struct audioformat, list);
96                 if (!(fp->formats & (1uLL << subs->pcm_format)))
97                         continue;
98                 if (fp->channels != subs->channels)
99                         continue;
100                 if (subs->cur_rate < fp->rate_min ||
101                     subs->cur_rate > fp->rate_max)
102                         continue;
103                 if (! (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)) {
104                         unsigned int i;
105                         for (i = 0; i < fp->nr_rates; i++)
106                                 if (fp->rate_table[i] == subs->cur_rate)
107                                         break;
108                         if (i >= fp->nr_rates)
109                                 continue;
110                 }
111                 attr = fp->ep_attr & USB_ENDPOINT_SYNCTYPE;
112                 if (! found) {
113                         found = fp;
114                         cur_attr = attr;
115                         continue;
116                 }
117                 /* avoid async out and adaptive in if the other method
118                  * supports the same format.
119                  * this is a workaround for the case like
120                  * M-audio audiophile USB.
121                  */
122                 if (attr != cur_attr) {
123                         if ((attr == USB_ENDPOINT_SYNC_ASYNC &&
124                              subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
125                             (attr == USB_ENDPOINT_SYNC_ADAPTIVE &&
126                              subs->direction == SNDRV_PCM_STREAM_CAPTURE))
127                                 continue;
128                         if ((cur_attr == USB_ENDPOINT_SYNC_ASYNC &&
129                              subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
130                             (cur_attr == USB_ENDPOINT_SYNC_ADAPTIVE &&
131                              subs->direction == SNDRV_PCM_STREAM_CAPTURE)) {
132                                 found = fp;
133                                 cur_attr = attr;
134                                 continue;
135                         }
136                 }
137                 /* find the format with the largest max. packet size */
138                 if (fp->maxpacksize > found->maxpacksize) {
139                         found = fp;
140                         cur_attr = attr;
141                 }
142         }
143         return found;
144 }
145
146 static int init_pitch_v1(struct snd_usb_audio *chip, int iface,
147                          struct usb_host_interface *alts,
148                          struct audioformat *fmt)
149 {
150         struct usb_device *dev = chip->dev;
151         unsigned int ep;
152         unsigned char data[1];
153         int err;
154
155         ep = get_endpoint(alts, 0)->bEndpointAddress;
156
157         data[0] = 1;
158         if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC_SET_CUR,
159                                    USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT,
160                                    UAC_EP_CS_ATTR_PITCH_CONTROL << 8, ep,
161                                    data, sizeof(data))) < 0) {
162                 snd_printk(KERN_ERR "%d:%d:%d: cannot set enable PITCH\n",
163                            dev->devnum, iface, ep);
164                 return err;
165         }
166
167         return 0;
168 }
169
170 static int init_pitch_v2(struct snd_usb_audio *chip, int iface,
171                          struct usb_host_interface *alts,
172                          struct audioformat *fmt)
173 {
174         struct usb_device *dev = chip->dev;
175         unsigned char data[1];
176         int err;
177
178         data[0] = 1;
179         if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC2_CS_CUR,
180                                    USB_TYPE_CLASS | USB_RECIP_ENDPOINT | USB_DIR_OUT,
181                                    UAC2_EP_CS_PITCH << 8, 0,
182                                    data, sizeof(data))) < 0) {
183                 snd_printk(KERN_ERR "%d:%d:%d: cannot set enable PITCH (v2)\n",
184                            dev->devnum, iface, fmt->altsetting);
185                 return err;
186         }
187
188         return 0;
189 }
190
191 /*
192  * initialize the pitch control and sample rate
193  */
194 int snd_usb_init_pitch(struct snd_usb_audio *chip, int iface,
195                        struct usb_host_interface *alts,
196                        struct audioformat *fmt)
197 {
198         struct usb_interface_descriptor *altsd = get_iface_desc(alts);
199
200         /* if endpoint doesn't have pitch control, bail out */
201         if (!(fmt->attributes & UAC_EP_CS_ATTR_PITCH_CONTROL))
202                 return 0;
203
204         switch (altsd->bInterfaceProtocol) {
205         case UAC_VERSION_1:
206         default:
207                 return init_pitch_v1(chip, iface, alts, fmt);
208
209         case UAC_VERSION_2:
210                 return init_pitch_v2(chip, iface, alts, fmt);
211         }
212 }
213
214 static int start_endpoints(struct snd_usb_substream *subs, bool can_sleep)
215 {
216         int err;
217
218         if (!subs->data_endpoint)
219                 return -EINVAL;
220
221         if (!test_and_set_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags)) {
222                 struct snd_usb_endpoint *ep = subs->data_endpoint;
223
224                 snd_printdd(KERN_DEBUG "Starting data EP @%p\n", ep);
225
226                 ep->data_subs = subs;
227                 err = snd_usb_endpoint_start(ep, can_sleep);
228                 if (err < 0) {
229                         clear_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags);
230                         return err;
231                 }
232         }
233
234         if (subs->sync_endpoint &&
235             !test_and_set_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags)) {
236                 struct snd_usb_endpoint *ep = subs->sync_endpoint;
237
238                 if (subs->data_endpoint->iface != subs->sync_endpoint->iface ||
239                     subs->data_endpoint->alt_idx != subs->sync_endpoint->alt_idx) {
240                         err = usb_set_interface(subs->dev,
241                                                 subs->sync_endpoint->iface,
242                                                 subs->sync_endpoint->alt_idx);
243                         if (err < 0) {
244                                 snd_printk(KERN_ERR
245                                            "%d:%d:%d: cannot set interface (%d)\n",
246                                            subs->dev->devnum,
247                                            subs->sync_endpoint->iface,
248                                            subs->sync_endpoint->alt_idx, err);
249                                 return -EIO;
250                         }
251                 }
252
253                 snd_printdd(KERN_DEBUG "Starting sync EP @%p\n", ep);
254
255                 ep->sync_slave = subs->data_endpoint;
256                 err = snd_usb_endpoint_start(ep, can_sleep);
257                 if (err < 0) {
258                         clear_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags);
259                         return err;
260                 }
261         }
262
263         return 0;
264 }
265
266 static void stop_endpoints(struct snd_usb_substream *subs, bool wait)
267 {
268         if (test_and_clear_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags))
269                 snd_usb_endpoint_stop(subs->sync_endpoint, wait);
270
271         if (test_and_clear_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags))
272                 snd_usb_endpoint_stop(subs->data_endpoint, wait);
273 }
274
275 static int deactivate_endpoints(struct snd_usb_substream *subs)
276 {
277         int reta, retb;
278
279         reta = snd_usb_endpoint_deactivate(subs->sync_endpoint);
280         retb = snd_usb_endpoint_deactivate(subs->data_endpoint);
281
282         if (reta < 0)
283                 return reta;
284
285         if (retb < 0)
286                 return retb;
287
288         return 0;
289 }
290
291 /*
292  * find a matching format and set up the interface
293  */
294 static int set_format(struct snd_usb_substream *subs, struct audioformat *fmt)
295 {
296         struct usb_device *dev = subs->dev;
297         struct usb_host_interface *alts;
298         struct usb_interface_descriptor *altsd;
299         struct usb_interface *iface;
300         unsigned int ep, attr;
301         int is_playback = subs->direction == SNDRV_PCM_STREAM_PLAYBACK;
302         int err, implicit_fb = 0;
303
304         iface = usb_ifnum_to_if(dev, fmt->iface);
305         if (WARN_ON(!iface))
306                 return -EINVAL;
307         alts = &iface->altsetting[fmt->altset_idx];
308         altsd = get_iface_desc(alts);
309         if (WARN_ON(altsd->bAlternateSetting != fmt->altsetting))
310                 return -EINVAL;
311
312         if (fmt == subs->cur_audiofmt)
313                 return 0;
314
315         /* close the old interface */
316         if (subs->interface >= 0 && subs->interface != fmt->iface) {
317                 err = usb_set_interface(subs->dev, subs->interface, 0);
318                 if (err < 0) {
319                         snd_printk(KERN_ERR "%d:%d:%d: return to setting 0 failed (%d)\n",
320                                 dev->devnum, fmt->iface, fmt->altsetting, err);
321                         return -EIO;
322                 }
323                 subs->interface = -1;
324                 subs->altset_idx = 0;
325         }
326
327         /* set interface */
328         if (subs->interface != fmt->iface ||
329             subs->altset_idx != fmt->altset_idx) {
330                 err = usb_set_interface(dev, fmt->iface, fmt->altsetting);
331                 if (err < 0) {
332                         snd_printk(KERN_ERR "%d:%d:%d: usb_set_interface failed (%d)\n",
333                                    dev->devnum, fmt->iface, fmt->altsetting, err);
334                         return -EIO;
335                 }
336                 snd_printdd(KERN_INFO "setting usb interface %d:%d\n",
337                                 fmt->iface, fmt->altsetting);
338                 subs->interface = fmt->iface;
339                 subs->altset_idx = fmt->altset_idx;
340         }
341
342         subs->data_endpoint = snd_usb_add_endpoint(subs->stream->chip,
343                                                    alts, fmt->endpoint, subs->direction,
344                                                    SND_USB_ENDPOINT_TYPE_DATA);
345         if (!subs->data_endpoint)
346                 return -EINVAL;
347
348         /* we need a sync pipe in async OUT or adaptive IN mode */
349         /* check the number of EP, since some devices have broken
350          * descriptors which fool us.  if it has only one EP,
351          * assume it as adaptive-out or sync-in.
352          */
353         attr = fmt->ep_attr & USB_ENDPOINT_SYNCTYPE;
354
355         switch (subs->stream->chip->usb_id) {
356         case USB_ID(0x0763, 0x2080): /* M-Audio FastTrack Ultra */
357         case USB_ID(0x0763, 0x2081):
358                 if (is_playback) {
359                         implicit_fb = 1;
360                         ep = 0x81;
361                         iface = usb_ifnum_to_if(dev, 2);
362
363                         if (!iface || iface->num_altsetting == 0)
364                                 return -EINVAL;
365
366                         alts = &iface->altsetting[1];
367                         goto add_sync_ep;
368                 }
369         }
370
371         if (((is_playback && attr == USB_ENDPOINT_SYNC_ASYNC) ||
372              (!is_playback && attr == USB_ENDPOINT_SYNC_ADAPTIVE)) &&
373             altsd->bNumEndpoints >= 2) {
374                 /* check sync-pipe endpoint */
375                 /* ... and check descriptor size before accessing bSynchAddress
376                    because there is a version of the SB Audigy 2 NX firmware lacking
377                    the audio fields in the endpoint descriptors */
378                 if ((get_endpoint(alts, 1)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != 0x01 ||
379                     (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
380                      get_endpoint(alts, 1)->bSynchAddress != 0 &&
381                      !implicit_fb)) {
382                         snd_printk(KERN_ERR "%d:%d:%d : invalid sync pipe. bmAttributes %02x, bLength %d, bSynchAddress %02x\n",
383                                    dev->devnum, fmt->iface, fmt->altsetting,
384                                    get_endpoint(alts, 1)->bmAttributes,
385                                    get_endpoint(alts, 1)->bLength,
386                                    get_endpoint(alts, 1)->bSynchAddress);
387                         return -EINVAL;
388                 }
389                 ep = get_endpoint(alts, 1)->bEndpointAddress;
390                 if (!implicit_fb &&
391                     get_endpoint(alts, 0)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
392                     (( is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress | USB_DIR_IN)) ||
393                      (!is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress & ~USB_DIR_IN)))) {
394                         snd_printk(KERN_ERR "%d:%d:%d : invalid sync pipe. is_playback %d, ep %02x, bSynchAddress %02x\n",
395                                    dev->devnum, fmt->iface, fmt->altsetting,
396                                    is_playback, ep, get_endpoint(alts, 0)->bSynchAddress);
397                         return -EINVAL;
398                 }
399
400                 implicit_fb = (get_endpoint(alts, 1)->bmAttributes & USB_ENDPOINT_USAGE_MASK)
401                                 == USB_ENDPOINT_USAGE_IMPLICIT_FB;
402
403 add_sync_ep:
404                 subs->sync_endpoint = snd_usb_add_endpoint(subs->stream->chip,
405                                                            alts, ep, !subs->direction,
406                                                            implicit_fb ?
407                                                                 SND_USB_ENDPOINT_TYPE_DATA :
408                                                                 SND_USB_ENDPOINT_TYPE_SYNC);
409                 if (!subs->sync_endpoint)
410                         return -EINVAL;
411
412                 subs->data_endpoint->sync_master = subs->sync_endpoint;
413         }
414
415         if ((err = snd_usb_init_pitch(subs->stream->chip, fmt->iface, alts, fmt)) < 0)
416                 return err;
417
418         subs->cur_audiofmt = fmt;
419
420         snd_usb_set_format_quirk(subs, fmt);
421
422 #if 0
423         printk(KERN_DEBUG
424                "setting done: format = %d, rate = %d..%d, channels = %d\n",
425                fmt->format, fmt->rate_min, fmt->rate_max, fmt->channels);
426         printk(KERN_DEBUG
427                "  datapipe = 0x%0x, syncpipe = 0x%0x\n",
428                subs->datapipe, subs->syncpipe);
429 #endif
430
431         return 0;
432 }
433
434 /*
435  * configure endpoint params
436  *
437  * called  during initial setup and upon resume
438  */
439 static int configure_endpoint(struct snd_usb_substream *subs)
440 {
441         int ret;
442
443         /* format changed */
444         stop_endpoints(subs, false);
445         ret = snd_usb_endpoint_set_params(subs->data_endpoint,
446                                           subs->pcm_format,
447                                           subs->channels,
448                                           subs->period_bytes,
449                                           subs->cur_rate,
450                                           subs->cur_audiofmt,
451                                           subs->sync_endpoint);
452         if (ret < 0)
453                 return ret;
454
455         if (subs->sync_endpoint)
456                 ret = snd_usb_endpoint_set_params(subs->data_endpoint,
457                                                   subs->pcm_format,
458                                                   subs->channels,
459                                                   subs->period_bytes,
460                                                   subs->cur_rate,
461                                                   subs->cur_audiofmt,
462                                                   NULL);
463         return ret;
464 }
465
466 /*
467  * hw_params callback
468  *
469  * allocate a buffer and set the given audio format.
470  *
471  * so far we use a physically linear buffer although packetize transfer
472  * doesn't need a continuous area.
473  * if sg buffer is supported on the later version of alsa, we'll follow
474  * that.
475  */
476 static int snd_usb_hw_params(struct snd_pcm_substream *substream,
477                              struct snd_pcm_hw_params *hw_params)
478 {
479         struct snd_usb_substream *subs = substream->runtime->private_data;
480         struct audioformat *fmt;
481         int ret;
482
483         ret = snd_pcm_lib_alloc_vmalloc_buffer(substream,
484                                                params_buffer_bytes(hw_params));
485         if (ret < 0)
486                 return ret;
487
488         subs->pcm_format = params_format(hw_params);
489         subs->period_bytes = params_period_bytes(hw_params);
490         subs->channels = params_channels(hw_params);
491         subs->cur_rate = params_rate(hw_params);
492
493         fmt = find_format(subs);
494         if (!fmt) {
495                 snd_printd(KERN_DEBUG "cannot set format: format = %#x, rate = %d, channels = %d\n",
496                            subs->pcm_format, subs->cur_rate, subs->channels);
497                 return -EINVAL;
498         }
499
500         down_read(&subs->stream->chip->shutdown_rwsem);
501         if (subs->stream->chip->shutdown)
502                 ret = -ENODEV;
503         else
504                 ret = set_format(subs, fmt);
505         up_read(&subs->stream->chip->shutdown_rwsem);
506         if (ret < 0)
507                 return ret;
508
509         subs->interface = fmt->iface;
510         subs->altset_idx = fmt->altset_idx;
511         subs->need_setup_ep = true;
512
513         return 0;
514 }
515
516 /*
517  * hw_free callback
518  *
519  * reset the audio format and release the buffer
520  */
521 static int snd_usb_hw_free(struct snd_pcm_substream *substream)
522 {
523         struct snd_usb_substream *subs = substream->runtime->private_data;
524
525         subs->cur_audiofmt = NULL;
526         subs->cur_rate = 0;
527         subs->period_bytes = 0;
528         down_read(&subs->stream->chip->shutdown_rwsem);
529         if (!subs->stream->chip->shutdown) {
530                 stop_endpoints(subs, true);
531                 deactivate_endpoints(subs);
532         }
533         up_read(&subs->stream->chip->shutdown_rwsem);
534         return snd_pcm_lib_free_vmalloc_buffer(substream);
535 }
536
537 /*
538  * prepare callback
539  *
540  * only a few subtle things...
541  */
542 static int snd_usb_pcm_prepare(struct snd_pcm_substream *substream)
543 {
544         struct snd_pcm_runtime *runtime = substream->runtime;
545         struct snd_usb_substream *subs = runtime->private_data;
546         struct usb_host_interface *alts;
547         struct usb_interface *iface;
548         int ret;
549
550         if (! subs->cur_audiofmt) {
551                 snd_printk(KERN_ERR "usbaudio: no format is specified!\n");
552                 return -ENXIO;
553         }
554
555         down_read(&subs->stream->chip->shutdown_rwsem);
556         if (subs->stream->chip->shutdown) {
557                 ret = -ENODEV;
558                 goto unlock;
559         }
560         if (snd_BUG_ON(!subs->data_endpoint)) {
561                 ret = -EIO;
562                 goto unlock;
563         }
564
565         snd_usb_endpoint_sync_pending_stop(subs->sync_endpoint);
566         snd_usb_endpoint_sync_pending_stop(subs->data_endpoint);
567
568         ret = set_format(subs, subs->cur_audiofmt);
569         if (ret < 0)
570                 goto unlock;
571
572         iface = usb_ifnum_to_if(subs->dev, subs->cur_audiofmt->iface);
573         alts = &iface->altsetting[subs->cur_audiofmt->altset_idx];
574         ret = snd_usb_init_sample_rate(subs->stream->chip,
575                                        subs->cur_audiofmt->iface,
576                                        alts,
577                                        subs->cur_audiofmt,
578                                        subs->cur_rate);
579         if (ret < 0)
580                 goto unlock;
581
582         if (subs->need_setup_ep) {
583                 ret = configure_endpoint(subs);
584                 if (ret < 0)
585                         goto unlock;
586                 subs->need_setup_ep = false;
587         }
588
589         /* some unit conversions in runtime */
590         subs->data_endpoint->maxframesize =
591                 bytes_to_frames(runtime, subs->data_endpoint->maxpacksize);
592         subs->data_endpoint->curframesize =
593                 bytes_to_frames(runtime, subs->data_endpoint->curpacksize);
594
595         /* reset the pointer */
596         subs->hwptr_done = 0;
597         subs->transfer_done = 0;
598         subs->last_delay = 0;
599         subs->last_frame_number = 0;
600         runtime->delay = 0;
601
602         /* for playback, submit the URBs now; otherwise, the first hwptr_done
603          * updates for all URBs would happen at the same time when starting */
604         if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK)
605                 ret = start_endpoints(subs, true);
606
607  unlock:
608         up_read(&subs->stream->chip->shutdown_rwsem);
609         return ret;
610 }
611
612 static struct snd_pcm_hardware snd_usb_hardware =
613 {
614         .info =                 SNDRV_PCM_INFO_MMAP |
615                                 SNDRV_PCM_INFO_MMAP_VALID |
616                                 SNDRV_PCM_INFO_BATCH |
617                                 SNDRV_PCM_INFO_INTERLEAVED |
618                                 SNDRV_PCM_INFO_BLOCK_TRANSFER |
619                                 SNDRV_PCM_INFO_PAUSE,
620         .buffer_bytes_max =     1024 * 1024,
621         .period_bytes_min =     64,
622         .period_bytes_max =     512 * 1024,
623         .periods_min =          2,
624         .periods_max =          1024,
625 };
626
627 static int hw_check_valid_format(struct snd_usb_substream *subs,
628                                  struct snd_pcm_hw_params *params,
629                                  struct audioformat *fp)
630 {
631         struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
632         struct snd_interval *ct = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
633         struct snd_mask *fmts = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
634         struct snd_interval *pt = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
635         struct snd_mask check_fmts;
636         unsigned int ptime;
637
638         /* check the format */
639         snd_mask_none(&check_fmts);
640         check_fmts.bits[0] = (u32)fp->formats;
641         check_fmts.bits[1] = (u32)(fp->formats >> 32);
642         snd_mask_intersect(&check_fmts, fmts);
643         if (snd_mask_empty(&check_fmts)) {
644                 hwc_debug("   > check: no supported format %d\n", fp->format);
645                 return 0;
646         }
647         /* check the channels */
648         if (fp->channels < ct->min || fp->channels > ct->max) {
649                 hwc_debug("   > check: no valid channels %d (%d/%d)\n", fp->channels, ct->min, ct->max);
650                 return 0;
651         }
652         /* check the rate is within the range */
653         if (fp->rate_min > it->max || (fp->rate_min == it->max && it->openmax)) {
654                 hwc_debug("   > check: rate_min %d > max %d\n", fp->rate_min, it->max);
655                 return 0;
656         }
657         if (fp->rate_max < it->min || (fp->rate_max == it->min && it->openmin)) {
658                 hwc_debug("   > check: rate_max %d < min %d\n", fp->rate_max, it->min);
659                 return 0;
660         }
661         /* check whether the period time is >= the data packet interval */
662         if (subs->speed != USB_SPEED_FULL) {
663                 ptime = 125 * (1 << fp->datainterval);
664                 if (ptime > pt->max || (ptime == pt->max && pt->openmax)) {
665                         hwc_debug("   > check: ptime %u > max %u\n", ptime, pt->max);
666                         return 0;
667                 }
668         }
669         return 1;
670 }
671
672 static int hw_rule_rate(struct snd_pcm_hw_params *params,
673                         struct snd_pcm_hw_rule *rule)
674 {
675         struct snd_usb_substream *subs = rule->private;
676         struct list_head *p;
677         struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
678         unsigned int rmin, rmax;
679         int changed;
680
681         hwc_debug("hw_rule_rate: (%d,%d)\n", it->min, it->max);
682         changed = 0;
683         rmin = rmax = 0;
684         list_for_each(p, &subs->fmt_list) {
685                 struct audioformat *fp;
686                 fp = list_entry(p, struct audioformat, list);
687                 if (!hw_check_valid_format(subs, params, fp))
688                         continue;
689                 if (changed++) {
690                         if (rmin > fp->rate_min)
691                                 rmin = fp->rate_min;
692                         if (rmax < fp->rate_max)
693                                 rmax = fp->rate_max;
694                 } else {
695                         rmin = fp->rate_min;
696                         rmax = fp->rate_max;
697                 }
698         }
699
700         if (!changed) {
701                 hwc_debug("  --> get empty\n");
702                 it->empty = 1;
703                 return -EINVAL;
704         }
705
706         changed = 0;
707         if (it->min < rmin) {
708                 it->min = rmin;
709                 it->openmin = 0;
710                 changed = 1;
711         }
712         if (it->max > rmax) {
713                 it->max = rmax;
714                 it->openmax = 0;
715                 changed = 1;
716         }
717         if (snd_interval_checkempty(it)) {
718                 it->empty = 1;
719                 return -EINVAL;
720         }
721         hwc_debug("  --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
722         return changed;
723 }
724
725
726 static int hw_rule_channels(struct snd_pcm_hw_params *params,
727                             struct snd_pcm_hw_rule *rule)
728 {
729         struct snd_usb_substream *subs = rule->private;
730         struct list_head *p;
731         struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
732         unsigned int rmin, rmax;
733         int changed;
734
735         hwc_debug("hw_rule_channels: (%d,%d)\n", it->min, it->max);
736         changed = 0;
737         rmin = rmax = 0;
738         list_for_each(p, &subs->fmt_list) {
739                 struct audioformat *fp;
740                 fp = list_entry(p, struct audioformat, list);
741                 if (!hw_check_valid_format(subs, params, fp))
742                         continue;
743                 if (changed++) {
744                         if (rmin > fp->channels)
745                                 rmin = fp->channels;
746                         if (rmax < fp->channels)
747                                 rmax = fp->channels;
748                 } else {
749                         rmin = fp->channels;
750                         rmax = fp->channels;
751                 }
752         }
753
754         if (!changed) {
755                 hwc_debug("  --> get empty\n");
756                 it->empty = 1;
757                 return -EINVAL;
758         }
759
760         changed = 0;
761         if (it->min < rmin) {
762                 it->min = rmin;
763                 it->openmin = 0;
764                 changed = 1;
765         }
766         if (it->max > rmax) {
767                 it->max = rmax;
768                 it->openmax = 0;
769                 changed = 1;
770         }
771         if (snd_interval_checkempty(it)) {
772                 it->empty = 1;
773                 return -EINVAL;
774         }
775         hwc_debug("  --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
776         return changed;
777 }
778
779 static int hw_rule_format(struct snd_pcm_hw_params *params,
780                           struct snd_pcm_hw_rule *rule)
781 {
782         struct snd_usb_substream *subs = rule->private;
783         struct list_head *p;
784         struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
785         u64 fbits;
786         u32 oldbits[2];
787         int changed;
788
789         hwc_debug("hw_rule_format: %x:%x\n", fmt->bits[0], fmt->bits[1]);
790         fbits = 0;
791         list_for_each(p, &subs->fmt_list) {
792                 struct audioformat *fp;
793                 fp = list_entry(p, struct audioformat, list);
794                 if (!hw_check_valid_format(subs, params, fp))
795                         continue;
796                 fbits |= fp->formats;
797         }
798
799         oldbits[0] = fmt->bits[0];
800         oldbits[1] = fmt->bits[1];
801         fmt->bits[0] &= (u32)fbits;
802         fmt->bits[1] &= (u32)(fbits >> 32);
803         if (!fmt->bits[0] && !fmt->bits[1]) {
804                 hwc_debug("  --> get empty\n");
805                 return -EINVAL;
806         }
807         changed = (oldbits[0] != fmt->bits[0] || oldbits[1] != fmt->bits[1]);
808         hwc_debug("  --> %x:%x (changed = %d)\n", fmt->bits[0], fmt->bits[1], changed);
809         return changed;
810 }
811
812 static int hw_rule_period_time(struct snd_pcm_hw_params *params,
813                                struct snd_pcm_hw_rule *rule)
814 {
815         struct snd_usb_substream *subs = rule->private;
816         struct audioformat *fp;
817         struct snd_interval *it;
818         unsigned char min_datainterval;
819         unsigned int pmin;
820         int changed;
821
822         it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
823         hwc_debug("hw_rule_period_time: (%u,%u)\n", it->min, it->max);
824         min_datainterval = 0xff;
825         list_for_each_entry(fp, &subs->fmt_list, list) {
826                 if (!hw_check_valid_format(subs, params, fp))
827                         continue;
828                 min_datainterval = min(min_datainterval, fp->datainterval);
829         }
830         if (min_datainterval == 0xff) {
831                 hwc_debug("  --> get empty\n");
832                 it->empty = 1;
833                 return -EINVAL;
834         }
835         pmin = 125 * (1 << min_datainterval);
836         changed = 0;
837         if (it->min < pmin) {
838                 it->min = pmin;
839                 it->openmin = 0;
840                 changed = 1;
841         }
842         if (snd_interval_checkempty(it)) {
843                 it->empty = 1;
844                 return -EINVAL;
845         }
846         hwc_debug("  --> (%u,%u) (changed = %d)\n", it->min, it->max, changed);
847         return changed;
848 }
849
850 /*
851  *  If the device supports unusual bit rates, does the request meet these?
852  */
853 static int snd_usb_pcm_check_knot(struct snd_pcm_runtime *runtime,
854                                   struct snd_usb_substream *subs)
855 {
856         struct audioformat *fp;
857         int *rate_list;
858         int count = 0, needs_knot = 0;
859         int err;
860
861         kfree(subs->rate_list.list);
862         subs->rate_list.list = NULL;
863
864         list_for_each_entry(fp, &subs->fmt_list, list) {
865                 if (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)
866                         return 0;
867                 count += fp->nr_rates;
868                 if (fp->rates & SNDRV_PCM_RATE_KNOT)
869                         needs_knot = 1;
870         }
871         if (!needs_knot)
872                 return 0;
873
874         subs->rate_list.list = rate_list =
875                 kmalloc(sizeof(int) * count, GFP_KERNEL);
876         if (!subs->rate_list.list)
877                 return -ENOMEM;
878         subs->rate_list.count = count;
879         subs->rate_list.mask = 0;
880         count = 0;
881         list_for_each_entry(fp, &subs->fmt_list, list) {
882                 int i;
883                 for (i = 0; i < fp->nr_rates; i++)
884                         rate_list[count++] = fp->rate_table[i];
885         }
886         err = snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
887                                          &subs->rate_list);
888         if (err < 0)
889                 return err;
890
891         return 0;
892 }
893
894
895 /*
896  * set up the runtime hardware information.
897  */
898
899 static int setup_hw_info(struct snd_pcm_runtime *runtime, struct snd_usb_substream *subs)
900 {
901         struct list_head *p;
902         unsigned int pt, ptmin;
903         int param_period_time_if_needed;
904         int err;
905
906         runtime->hw.formats = subs->formats;
907
908         runtime->hw.rate_min = 0x7fffffff;
909         runtime->hw.rate_max = 0;
910         runtime->hw.channels_min = 256;
911         runtime->hw.channels_max = 0;
912         runtime->hw.rates = 0;
913         ptmin = UINT_MAX;
914         /* check min/max rates and channels */
915         list_for_each(p, &subs->fmt_list) {
916                 struct audioformat *fp;
917                 fp = list_entry(p, struct audioformat, list);
918                 runtime->hw.rates |= fp->rates;
919                 if (runtime->hw.rate_min > fp->rate_min)
920                         runtime->hw.rate_min = fp->rate_min;
921                 if (runtime->hw.rate_max < fp->rate_max)
922                         runtime->hw.rate_max = fp->rate_max;
923                 if (runtime->hw.channels_min > fp->channels)
924                         runtime->hw.channels_min = fp->channels;
925                 if (runtime->hw.channels_max < fp->channels)
926                         runtime->hw.channels_max = fp->channels;
927                 if (fp->fmt_type == UAC_FORMAT_TYPE_II && fp->frame_size > 0) {
928                         /* FIXME: there might be more than one audio formats... */
929                         runtime->hw.period_bytes_min = runtime->hw.period_bytes_max =
930                                 fp->frame_size;
931                 }
932                 pt = 125 * (1 << fp->datainterval);
933                 ptmin = min(ptmin, pt);
934         }
935         err = snd_usb_autoresume(subs->stream->chip);
936         if (err < 0)
937                 return err;
938
939         param_period_time_if_needed = SNDRV_PCM_HW_PARAM_PERIOD_TIME;
940         if (subs->speed == USB_SPEED_FULL)
941                 /* full speed devices have fixed data packet interval */
942                 ptmin = 1000;
943         if (ptmin == 1000)
944                 /* if period time doesn't go below 1 ms, no rules needed */
945                 param_period_time_if_needed = -1;
946         snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
947                                      ptmin, UINT_MAX);
948
949         if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
950                                        hw_rule_rate, subs,
951                                        SNDRV_PCM_HW_PARAM_FORMAT,
952                                        SNDRV_PCM_HW_PARAM_CHANNELS,
953                                        param_period_time_if_needed,
954                                        -1)) < 0)
955                 goto rep_err;
956         if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
957                                        hw_rule_channels, subs,
958                                        SNDRV_PCM_HW_PARAM_FORMAT,
959                                        SNDRV_PCM_HW_PARAM_RATE,
960                                        param_period_time_if_needed,
961                                        -1)) < 0)
962                 goto rep_err;
963         if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
964                                        hw_rule_format, subs,
965                                        SNDRV_PCM_HW_PARAM_RATE,
966                                        SNDRV_PCM_HW_PARAM_CHANNELS,
967                                        param_period_time_if_needed,
968                                        -1)) < 0)
969                 goto rep_err;
970         if (param_period_time_if_needed >= 0) {
971                 err = snd_pcm_hw_rule_add(runtime, 0,
972                                           SNDRV_PCM_HW_PARAM_PERIOD_TIME,
973                                           hw_rule_period_time, subs,
974                                           SNDRV_PCM_HW_PARAM_FORMAT,
975                                           SNDRV_PCM_HW_PARAM_CHANNELS,
976                                           SNDRV_PCM_HW_PARAM_RATE,
977                                           -1);
978                 if (err < 0)
979                         goto rep_err;
980         }
981         if ((err = snd_usb_pcm_check_knot(runtime, subs)) < 0)
982                 goto rep_err;
983         return 0;
984
985 rep_err:
986         snd_usb_autosuspend(subs->stream->chip);
987         return err;
988 }
989
990 static int snd_usb_pcm_open(struct snd_pcm_substream *substream, int direction)
991 {
992         struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
993         struct snd_pcm_runtime *runtime = substream->runtime;
994         struct snd_usb_substream *subs = &as->substream[direction];
995
996         subs->interface = -1;
997         subs->altset_idx = 0;
998         runtime->hw = snd_usb_hardware;
999         runtime->private_data = subs;
1000         subs->pcm_substream = substream;
1001         /* runtime PM is also done there */
1002         return setup_hw_info(runtime, subs);
1003 }
1004
1005 static int snd_usb_pcm_close(struct snd_pcm_substream *substream, int direction)
1006 {
1007         struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
1008         struct snd_usb_substream *subs = &as->substream[direction];
1009
1010         stop_endpoints(subs, false);
1011
1012         if (!as->chip->shutdown && subs->interface >= 0) {
1013                 usb_set_interface(subs->dev, subs->interface, 0);
1014                 subs->interface = -1;
1015         }
1016
1017         subs->pcm_substream = NULL;
1018         snd_usb_autosuspend(subs->stream->chip);
1019
1020         return 0;
1021 }
1022
1023 /* Since a URB can handle only a single linear buffer, we must use double
1024  * buffering when the data to be transferred overflows the buffer boundary.
1025  * To avoid inconsistencies when updating hwptr_done, we use double buffering
1026  * for all URBs.
1027  */
1028 static void retire_capture_urb(struct snd_usb_substream *subs,
1029                                struct urb *urb)
1030 {
1031         struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1032         unsigned int stride, frames, bytes, oldptr;
1033         int i, period_elapsed = 0;
1034         unsigned long flags;
1035         unsigned char *cp;
1036
1037         stride = runtime->frame_bits >> 3;
1038
1039         for (i = 0; i < urb->number_of_packets; i++) {
1040                 cp = (unsigned char *)urb->transfer_buffer + urb->iso_frame_desc[i].offset;
1041                 if (urb->iso_frame_desc[i].status && printk_ratelimit()) {
1042                         snd_printdd(KERN_ERR "frame %d active: %d\n", i, urb->iso_frame_desc[i].status);
1043                         // continue;
1044                 }
1045                 bytes = urb->iso_frame_desc[i].actual_length;
1046                 frames = bytes / stride;
1047                 if (!subs->txfr_quirk)
1048                         bytes = frames * stride;
1049                 if (bytes % (runtime->sample_bits >> 3) != 0) {
1050 #ifdef CONFIG_SND_DEBUG_VERBOSE
1051                         int oldbytes = bytes;
1052 #endif
1053                         bytes = frames * stride;
1054                         snd_printdd(KERN_ERR "Corrected urb data len. %d->%d\n",
1055                                                         oldbytes, bytes);
1056                 }
1057                 /* update the current pointer */
1058                 spin_lock_irqsave(&subs->lock, flags);
1059                 oldptr = subs->hwptr_done;
1060                 subs->hwptr_done += bytes;
1061                 if (subs->hwptr_done >= runtime->buffer_size * stride)
1062                         subs->hwptr_done -= runtime->buffer_size * stride;
1063                 frames = (bytes + (oldptr % stride)) / stride;
1064                 subs->transfer_done += frames;
1065                 if (subs->transfer_done >= runtime->period_size) {
1066                         subs->transfer_done -= runtime->period_size;
1067                         period_elapsed = 1;
1068                 }
1069                 spin_unlock_irqrestore(&subs->lock, flags);
1070                 /* copy a data chunk */
1071                 if (oldptr + bytes > runtime->buffer_size * stride) {
1072                         unsigned int bytes1 =
1073                                         runtime->buffer_size * stride - oldptr;
1074                         memcpy(runtime->dma_area + oldptr, cp, bytes1);
1075                         memcpy(runtime->dma_area, cp + bytes1, bytes - bytes1);
1076                 } else {
1077                         memcpy(runtime->dma_area + oldptr, cp, bytes);
1078                 }
1079         }
1080
1081         if (period_elapsed)
1082                 snd_pcm_period_elapsed(subs->pcm_substream);
1083 }
1084
1085 static void prepare_playback_urb(struct snd_usb_substream *subs,
1086                                  struct urb *urb)
1087 {
1088         struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1089         struct snd_usb_endpoint *ep = subs->data_endpoint;
1090         struct snd_urb_ctx *ctx = urb->context;
1091         unsigned int counts, frames, bytes;
1092         int i, stride, period_elapsed = 0;
1093         unsigned long flags;
1094
1095         stride = runtime->frame_bits >> 3;
1096
1097         frames = 0;
1098         urb->number_of_packets = 0;
1099         spin_lock_irqsave(&subs->lock, flags);
1100         for (i = 0; i < ctx->packets; i++) {
1101                 if (ctx->packet_size[i])
1102                         counts = ctx->packet_size[i];
1103                 else
1104                         counts = snd_usb_endpoint_next_packet_size(ep);
1105
1106                 /* set up descriptor */
1107                 urb->iso_frame_desc[i].offset = frames * stride;
1108                 urb->iso_frame_desc[i].length = counts * stride;
1109                 frames += counts;
1110                 urb->number_of_packets++;
1111                 subs->transfer_done += counts;
1112                 if (subs->transfer_done >= runtime->period_size) {
1113                         subs->transfer_done -= runtime->period_size;
1114                         period_elapsed = 1;
1115                         if (subs->fmt_type == UAC_FORMAT_TYPE_II) {
1116                                 if (subs->transfer_done > 0) {
1117                                         /* FIXME: fill-max mode is not
1118                                          * supported yet */
1119                                         frames -= subs->transfer_done;
1120                                         counts -= subs->transfer_done;
1121                                         urb->iso_frame_desc[i].length =
1122                                                 counts * stride;
1123                                         subs->transfer_done = 0;
1124                                 }
1125                                 i++;
1126                                 if (i < ctx->packets) {
1127                                         /* add a transfer delimiter */
1128                                         urb->iso_frame_desc[i].offset =
1129                                                 frames * stride;
1130                                         urb->iso_frame_desc[i].length = 0;
1131                                         urb->number_of_packets++;
1132                                 }
1133                                 break;
1134                         }
1135                 }
1136                 if (period_elapsed &&
1137                     !snd_usb_endpoint_implict_feedback_sink(subs->data_endpoint)) /* finish at the period boundary */
1138                         break;
1139         }
1140         bytes = frames * stride;
1141         if (subs->hwptr_done + bytes > runtime->buffer_size * stride) {
1142                 /* err, the transferred area goes over buffer boundary. */
1143                 unsigned int bytes1 =
1144                         runtime->buffer_size * stride - subs->hwptr_done;
1145                 memcpy(urb->transfer_buffer,
1146                        runtime->dma_area + subs->hwptr_done, bytes1);
1147                 memcpy(urb->transfer_buffer + bytes1,
1148                        runtime->dma_area, bytes - bytes1);
1149         } else {
1150                 memcpy(urb->transfer_buffer,
1151                        runtime->dma_area + subs->hwptr_done, bytes);
1152         }
1153         subs->hwptr_done += bytes;
1154         if (subs->hwptr_done >= runtime->buffer_size * stride)
1155                 subs->hwptr_done -= runtime->buffer_size * stride;
1156
1157         /* update delay with exact number of samples queued */
1158         runtime->delay = subs->last_delay;
1159         runtime->delay += frames;
1160         subs->last_delay = runtime->delay;
1161
1162         /* realign last_frame_number */
1163         subs->last_frame_number = usb_get_current_frame_number(subs->dev);
1164         subs->last_frame_number &= 0xFF; /* keep 8 LSBs */
1165
1166         spin_unlock_irqrestore(&subs->lock, flags);
1167         urb->transfer_buffer_length = bytes;
1168         if (period_elapsed)
1169                 snd_pcm_period_elapsed(subs->pcm_substream);
1170 }
1171
1172 /*
1173  * process after playback data complete
1174  * - decrease the delay count again
1175  */
1176 static void retire_playback_urb(struct snd_usb_substream *subs,
1177                                struct urb *urb)
1178 {
1179         unsigned long flags;
1180         struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1181         int stride = runtime->frame_bits >> 3;
1182         int processed = urb->transfer_buffer_length / stride;
1183         int est_delay;
1184
1185         /* ignore the delay accounting when procssed=0 is given, i.e.
1186          * silent payloads are procssed before handling the actual data
1187          */
1188         if (!processed)
1189                 return;
1190
1191         spin_lock_irqsave(&subs->lock, flags);
1192         est_delay = snd_usb_pcm_delay(subs, runtime->rate);
1193         /* update delay with exact number of samples played */
1194         if (processed > subs->last_delay)
1195                 subs->last_delay = 0;
1196         else
1197                 subs->last_delay -= processed;
1198         runtime->delay = subs->last_delay;
1199
1200         /*
1201          * Report when delay estimate is off by more than 2ms.
1202          * The error should be lower than 2ms since the estimate relies
1203          * on two reads of a counter updated every ms.
1204          */
1205         if (abs(est_delay - subs->last_delay) * 1000 > runtime->rate * 2)
1206                 snd_printk(KERN_DEBUG "delay: estimated %d, actual %d\n",
1207                         est_delay, subs->last_delay);
1208
1209         spin_unlock_irqrestore(&subs->lock, flags);
1210 }
1211
1212 static int snd_usb_playback_open(struct snd_pcm_substream *substream)
1213 {
1214         return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_PLAYBACK);
1215 }
1216
1217 static int snd_usb_playback_close(struct snd_pcm_substream *substream)
1218 {
1219         return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_PLAYBACK);
1220 }
1221
1222 static int snd_usb_capture_open(struct snd_pcm_substream *substream)
1223 {
1224         return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_CAPTURE);
1225 }
1226
1227 static int snd_usb_capture_close(struct snd_pcm_substream *substream)
1228 {
1229         return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_CAPTURE);
1230 }
1231
1232 static int snd_usb_substream_playback_trigger(struct snd_pcm_substream *substream,
1233                                               int cmd)
1234 {
1235         struct snd_usb_substream *subs = substream->runtime->private_data;
1236
1237         switch (cmd) {
1238         case SNDRV_PCM_TRIGGER_START:
1239         case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1240                 subs->data_endpoint->prepare_data_urb = prepare_playback_urb;
1241                 subs->data_endpoint->retire_data_urb = retire_playback_urb;
1242                 subs->running = 1;
1243                 return 0;
1244         case SNDRV_PCM_TRIGGER_STOP:
1245                 stop_endpoints(subs, false);
1246                 subs->running = 0;
1247                 return 0;
1248         case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1249                 subs->data_endpoint->prepare_data_urb = NULL;
1250                 subs->data_endpoint->retire_data_urb = NULL;
1251                 subs->running = 0;
1252                 return 0;
1253         }
1254
1255         return -EINVAL;
1256 }
1257
1258 static int snd_usb_substream_capture_trigger(struct snd_pcm_substream *substream,
1259                                              int cmd)
1260 {
1261         int err;
1262         struct snd_usb_substream *subs = substream->runtime->private_data;
1263
1264         switch (cmd) {
1265         case SNDRV_PCM_TRIGGER_START:
1266                 err = start_endpoints(subs, false);
1267                 if (err < 0)
1268                         return err;
1269
1270                 subs->data_endpoint->retire_data_urb = retire_capture_urb;
1271                 subs->running = 1;
1272                 return 0;
1273         case SNDRV_PCM_TRIGGER_STOP:
1274                 stop_endpoints(subs, false);
1275                 subs->running = 0;
1276                 return 0;
1277         case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1278                 subs->data_endpoint->retire_data_urb = NULL;
1279                 subs->running = 0;
1280                 return 0;
1281         case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1282                 subs->data_endpoint->retire_data_urb = retire_capture_urb;
1283                 subs->running = 1;
1284                 return 0;
1285         }
1286
1287         return -EINVAL;
1288 }
1289
1290 static struct snd_pcm_ops snd_usb_playback_ops = {
1291         .open =         snd_usb_playback_open,
1292         .close =        snd_usb_playback_close,
1293         .ioctl =        snd_pcm_lib_ioctl,
1294         .hw_params =    snd_usb_hw_params,
1295         .hw_free =      snd_usb_hw_free,
1296         .prepare =      snd_usb_pcm_prepare,
1297         .trigger =      snd_usb_substream_playback_trigger,
1298         .pointer =      snd_usb_pcm_pointer,
1299         .page =         snd_pcm_lib_get_vmalloc_page,
1300         .mmap =         snd_pcm_lib_mmap_vmalloc,
1301 };
1302
1303 static struct snd_pcm_ops snd_usb_capture_ops = {
1304         .open =         snd_usb_capture_open,
1305         .close =        snd_usb_capture_close,
1306         .ioctl =        snd_pcm_lib_ioctl,
1307         .hw_params =    snd_usb_hw_params,
1308         .hw_free =      snd_usb_hw_free,
1309         .prepare =      snd_usb_pcm_prepare,
1310         .trigger =      snd_usb_substream_capture_trigger,
1311         .pointer =      snd_usb_pcm_pointer,
1312         .page =         snd_pcm_lib_get_vmalloc_page,
1313         .mmap =         snd_pcm_lib_mmap_vmalloc,
1314 };
1315
1316 void snd_usb_set_pcm_ops(struct snd_pcm *pcm, int stream)
1317 {
1318         snd_pcm_set_ops(pcm, stream,
1319                         stream == SNDRV_PCM_STREAM_PLAYBACK ?
1320                         &snd_usb_playback_ops : &snd_usb_capture_ops);
1321 }