tcp: enforce tcp_min_snd_mss in tcp_mtu_probing()
[pandora-kernel.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/usb.h>
20 #include <linux/usb/audio.h>
21 #include <linux/usb/audio-v2.h>
22
23 #include <sound/core.h>
24 #include <sound/pcm.h>
25 #include <sound/pcm_params.h>
26
27 #include "usbaudio.h"
28 #include "card.h"
29 #include "quirks.h"
30 #include "debug.h"
31 #include "endpoint.h"
32 #include "helper.h"
33 #include "pcm.h"
34 #include "clock.h"
35 #include "power.h"
36
37 /* return the estimated delay based on USB frame counters */
38 snd_pcm_uframes_t snd_usb_pcm_delay(struct snd_usb_substream *subs,
39                                     unsigned int rate)
40 {
41         int current_frame_number;
42         int frame_diff;
43         int est_delay;
44
45         current_frame_number = usb_get_current_frame_number(subs->dev);
46         /*
47          * HCD implementations use different widths, use lower 8 bits.
48          * The delay will be managed up to 256ms, which is more than
49          * enough
50          */
51         frame_diff = (current_frame_number - subs->last_frame_number) & 0xff;
52
53         /* Approximation based on number of samples per USB frame (ms),
54            some truncation for 44.1 but the estimate is good enough */
55         est_delay =  subs->last_delay - (frame_diff * rate / 1000);
56         if (est_delay < 0)
57                 est_delay = 0;
58         return est_delay;
59 }
60
61 /*
62  * return the current pcm pointer.  just based on the hwptr_done value.
63  */
64 static snd_pcm_uframes_t snd_usb_pcm_pointer(struct snd_pcm_substream *substream)
65 {
66         struct snd_usb_substream *subs;
67         unsigned int hwptr_done;
68
69         subs = (struct snd_usb_substream *)substream->runtime->private_data;
70         if (subs->stream->chip->shutdown)
71                 return SNDRV_PCM_POS_XRUN;
72         spin_lock(&subs->lock);
73         hwptr_done = subs->hwptr_done;
74         substream->runtime->delay = snd_usb_pcm_delay(subs,
75                                                 substream->runtime->rate);
76         spin_unlock(&subs->lock);
77         return hwptr_done / (substream->runtime->frame_bits >> 3);
78 }
79
80 /*
81  * find a matching audio format
82  */
83 static struct audioformat *find_format(struct snd_usb_substream *subs, unsigned int format,
84                                        unsigned int rate, unsigned int channels)
85 {
86         struct list_head *p;
87         struct audioformat *found = NULL;
88         int cur_attr = 0, attr;
89
90         list_for_each(p, &subs->fmt_list) {
91                 struct audioformat *fp;
92                 fp = list_entry(p, struct audioformat, list);
93                 if (!(fp->formats & (1uLL << format)))
94                         continue;
95                 if (fp->channels != channels)
96                         continue;
97                 if (rate < fp->rate_min || rate > fp->rate_max)
98                         continue;
99                 if (! (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)) {
100                         unsigned int i;
101                         for (i = 0; i < fp->nr_rates; i++)
102                                 if (fp->rate_table[i] == rate)
103                                         break;
104                         if (i >= fp->nr_rates)
105                                 continue;
106                 }
107                 attr = fp->ep_attr & USB_ENDPOINT_SYNCTYPE;
108                 if (! found) {
109                         found = fp;
110                         cur_attr = attr;
111                         continue;
112                 }
113                 /* avoid async out and adaptive in if the other method
114                  * supports the same format.
115                  * this is a workaround for the case like
116                  * M-audio audiophile USB.
117                  */
118                 if (attr != cur_attr) {
119                         if ((attr == USB_ENDPOINT_SYNC_ASYNC &&
120                              subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
121                             (attr == USB_ENDPOINT_SYNC_ADAPTIVE &&
122                              subs->direction == SNDRV_PCM_STREAM_CAPTURE))
123                                 continue;
124                         if ((cur_attr == USB_ENDPOINT_SYNC_ASYNC &&
125                              subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
126                             (cur_attr == USB_ENDPOINT_SYNC_ADAPTIVE &&
127                              subs->direction == SNDRV_PCM_STREAM_CAPTURE)) {
128                                 found = fp;
129                                 cur_attr = attr;
130                                 continue;
131                         }
132                 }
133                 /* find the format with the largest max. packet size */
134                 if (fp->maxpacksize > found->maxpacksize) {
135                         found = fp;
136                         cur_attr = attr;
137                 }
138         }
139         return found;
140 }
141
142 static int init_pitch_v1(struct snd_usb_audio *chip, int iface,
143                          struct usb_host_interface *alts,
144                          struct audioformat *fmt)
145 {
146         struct usb_device *dev = chip->dev;
147         unsigned int ep;
148         unsigned char data[1];
149         int err;
150
151         if (get_iface_desc(alts)->bNumEndpoints < 1)
152                 return -EINVAL;
153         ep = get_endpoint(alts, 0)->bEndpointAddress;
154
155         data[0] = 1;
156         if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC_SET_CUR,
157                                    USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT,
158                                    UAC_EP_CS_ATTR_PITCH_CONTROL << 8, ep,
159                                    data, sizeof(data))) < 0) {
160                 snd_printk(KERN_ERR "%d:%d:%d: cannot set enable PITCH\n",
161                            dev->devnum, iface, ep);
162                 return err;
163         }
164
165         return 0;
166 }
167
168 static int init_pitch_v2(struct snd_usb_audio *chip, int iface,
169                          struct usb_host_interface *alts,
170                          struct audioformat *fmt)
171 {
172         struct usb_device *dev = chip->dev;
173         unsigned char data[1];
174         unsigned int ep;
175         int err;
176
177         ep = get_endpoint(alts, 0)->bEndpointAddress;
178
179         data[0] = 1;
180         if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC2_CS_CUR,
181                                    USB_TYPE_CLASS | USB_RECIP_ENDPOINT | USB_DIR_OUT,
182                                    UAC2_EP_CS_PITCH << 8, 0,
183                                    data, sizeof(data))) < 0) {
184                 snd_printk(KERN_ERR "%d:%d:%d: cannot set enable PITCH (v2)\n",
185                            dev->devnum, iface, fmt->altsetting);
186                 return err;
187         }
188
189         return 0;
190 }
191
192 /*
193  * initialize the pitch control and sample rate
194  */
195 int snd_usb_init_pitch(struct snd_usb_audio *chip, int iface,
196                        struct usb_host_interface *alts,
197                        struct audioformat *fmt)
198 {
199         struct usb_interface_descriptor *altsd = get_iface_desc(alts);
200
201         /* if endpoint doesn't have pitch control, bail out */
202         if (!(fmt->attributes & UAC_EP_CS_ATTR_PITCH_CONTROL))
203                 return 0;
204
205         switch (altsd->bInterfaceProtocol) {
206         case UAC_VERSION_1:
207         default:
208                 return init_pitch_v1(chip, iface, alts, fmt);
209
210         case UAC_VERSION_2:
211                 return init_pitch_v2(chip, iface, alts, fmt);
212         }
213 }
214
215 /*
216  * find a matching format and set up the interface
217  */
218 static int set_format(struct snd_usb_substream *subs, struct audioformat *fmt)
219 {
220         struct usb_device *dev = subs->dev;
221         struct usb_host_interface *alts;
222         struct usb_interface_descriptor *altsd;
223         struct usb_interface *iface;
224         unsigned int ep, attr;
225         int is_playback = subs->direction == SNDRV_PCM_STREAM_PLAYBACK;
226         int err;
227
228         iface = usb_ifnum_to_if(dev, fmt->iface);
229         if (WARN_ON(!iface))
230                 return -EINVAL;
231         alts = &iface->altsetting[fmt->altset_idx];
232         altsd = get_iface_desc(alts);
233         if (WARN_ON(altsd->bAlternateSetting != fmt->altsetting))
234                 return -EINVAL;
235
236         if (fmt == subs->cur_audiofmt)
237                 return 0;
238
239         /* close the old interface */
240         if (subs->interface >= 0 && subs->interface != fmt->iface) {
241                 if (usb_set_interface(subs->dev, subs->interface, 0) < 0) {
242                         snd_printk(KERN_ERR "%d:%d:%d: return to setting 0 failed\n",
243                                 dev->devnum, fmt->iface, fmt->altsetting);
244                         return -EIO;
245                 }
246                 subs->interface = -1;
247                 subs->altset_idx = 0;
248         }
249
250         /* set interface */
251         if (subs->interface != fmt->iface || subs->altset_idx != fmt->altset_idx) {
252                 if (usb_set_interface(dev, fmt->iface, fmt->altsetting) < 0) {
253                         snd_printk(KERN_ERR "%d:%d:%d: usb_set_interface failed\n",
254                                    dev->devnum, fmt->iface, fmt->altsetting);
255                         return -EIO;
256                 }
257                 snd_printdd(KERN_INFO "setting usb interface %d:%d\n", fmt->iface, fmt->altsetting);
258                 subs->interface = fmt->iface;
259                 subs->altset_idx = fmt->altset_idx;
260         }
261
262         /* create a data pipe */
263         ep = fmt->endpoint & USB_ENDPOINT_NUMBER_MASK;
264         if (is_playback)
265                 subs->datapipe = usb_sndisocpipe(dev, ep);
266         else
267                 subs->datapipe = usb_rcvisocpipe(dev, ep);
268         subs->datainterval = fmt->datainterval;
269         subs->syncpipe = subs->syncinterval = 0;
270         subs->maxpacksize = fmt->maxpacksize;
271         subs->syncmaxsize = 0;
272         subs->fill_max = 0;
273
274         /* we need a sync pipe in async OUT or adaptive IN mode */
275         /* check the number of EP, since some devices have broken
276          * descriptors which fool us.  if it has only one EP,
277          * assume it as adaptive-out or sync-in.
278          */
279         attr = fmt->ep_attr & USB_ENDPOINT_SYNCTYPE;
280         if (((is_playback && attr == USB_ENDPOINT_SYNC_ASYNC) ||
281              (! is_playback && attr == USB_ENDPOINT_SYNC_ADAPTIVE)) &&
282             altsd->bNumEndpoints >= 2) {
283                 /* check sync-pipe endpoint */
284                 /* ... and check descriptor size before accessing bSynchAddress
285                    because there is a version of the SB Audigy 2 NX firmware lacking
286                    the audio fields in the endpoint descriptors */
287                 if ((get_endpoint(alts, 1)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != 0x01 ||
288                     (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
289                      get_endpoint(alts, 1)->bSynchAddress != 0)) {
290                         snd_printk(KERN_ERR "%d:%d:%d : invalid synch pipe\n",
291                                    dev->devnum, fmt->iface, fmt->altsetting);
292                         return -EINVAL;
293                 }
294                 ep = get_endpoint(alts, 1)->bEndpointAddress;
295                 if (get_endpoint(alts, 0)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
296                     (( is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress | USB_DIR_IN)) ||
297                      (!is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress & ~USB_DIR_IN)))) {
298                         snd_printk(KERN_ERR "%d:%d:%d : invalid synch pipe\n",
299                                    dev->devnum, fmt->iface, fmt->altsetting);
300                         return -EINVAL;
301                 }
302                 ep &= USB_ENDPOINT_NUMBER_MASK;
303                 if (is_playback)
304                         subs->syncpipe = usb_rcvisocpipe(dev, ep);
305                 else
306                         subs->syncpipe = usb_sndisocpipe(dev, ep);
307                 if (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
308                     get_endpoint(alts, 1)->bRefresh >= 1 &&
309                     get_endpoint(alts, 1)->bRefresh <= 9)
310                         subs->syncinterval = get_endpoint(alts, 1)->bRefresh;
311                 else if (snd_usb_get_speed(subs->dev) == USB_SPEED_FULL)
312                         subs->syncinterval = 1;
313                 else if (get_endpoint(alts, 1)->bInterval >= 1 &&
314                          get_endpoint(alts, 1)->bInterval <= 16)
315                         subs->syncinterval = get_endpoint(alts, 1)->bInterval - 1;
316                 else
317                         subs->syncinterval = 3;
318                 subs->syncmaxsize = le16_to_cpu(get_endpoint(alts, 1)->wMaxPacketSize);
319         }
320
321         /* always fill max packet size */
322         if (fmt->attributes & UAC_EP_CS_ATTR_FILL_MAX)
323                 subs->fill_max = 1;
324
325         if ((err = snd_usb_init_pitch(subs->stream->chip, subs->interface, alts, fmt)) < 0)
326                 return err;
327
328         subs->cur_audiofmt = fmt;
329
330         snd_usb_set_format_quirk(subs, fmt);
331
332 #if 0
333         printk(KERN_DEBUG
334                "setting done: format = %d, rate = %d..%d, channels = %d\n",
335                fmt->format, fmt->rate_min, fmt->rate_max, fmt->channels);
336         printk(KERN_DEBUG
337                "  datapipe = 0x%0x, syncpipe = 0x%0x\n",
338                subs->datapipe, subs->syncpipe);
339 #endif
340
341         return 0;
342 }
343
344 /*
345  * hw_params callback
346  *
347  * allocate a buffer and set the given audio format.
348  *
349  * so far we use a physically linear buffer although packetize transfer
350  * doesn't need a continuous area.
351  * if sg buffer is supported on the later version of alsa, we'll follow
352  * that.
353  */
354 static int snd_usb_hw_params(struct snd_pcm_substream *substream,
355                              struct snd_pcm_hw_params *hw_params)
356 {
357         struct snd_usb_substream *subs = substream->runtime->private_data;
358         struct audioformat *fmt;
359         unsigned int channels, rate, format;
360         int ret, changed;
361
362         ret = snd_pcm_lib_alloc_vmalloc_buffer(substream,
363                                                params_buffer_bytes(hw_params));
364         if (ret < 0)
365                 return ret;
366
367         format = params_format(hw_params);
368         rate = params_rate(hw_params);
369         channels = params_channels(hw_params);
370         fmt = find_format(subs, format, rate, channels);
371         if (!fmt) {
372                 snd_printd(KERN_DEBUG "cannot set format: format = %#x, rate = %d, channels = %d\n",
373                            format, rate, channels);
374                 return -EINVAL;
375         }
376
377         changed = subs->cur_audiofmt != fmt ||
378                 subs->period_bytes != params_period_bytes(hw_params) ||
379                 subs->cur_rate != rate;
380
381         down_read(&subs->stream->chip->shutdown_rwsem);
382         if (subs->stream->chip->shutdown) {
383                 ret = -ENODEV;
384                 goto unlock;
385         }
386         if ((ret = set_format(subs, fmt)) < 0)
387                 goto unlock;
388
389         if (subs->cur_rate != rate) {
390                 struct usb_host_interface *alts;
391                 struct usb_interface *iface;
392                 iface = usb_ifnum_to_if(subs->dev, fmt->iface);
393                 alts = &iface->altsetting[fmt->altset_idx];
394                 ret = snd_usb_init_sample_rate(subs->stream->chip, subs->interface, alts, fmt, rate);
395                 if (ret < 0)
396                         goto unlock;
397                 subs->cur_rate = rate;
398         }
399
400         if (changed) {
401                 /* format changed */
402                 snd_usb_release_substream_urbs(subs, 0);
403                 /* influenced: period_bytes, channels, rate, format, */
404                 ret = snd_usb_init_substream_urbs(subs, params_period_bytes(hw_params),
405                                                   params_rate(hw_params),
406                                                   snd_pcm_format_physical_width(params_format(hw_params)) *
407                                                         params_channels(hw_params));
408         }
409
410 unlock:
411         up_read(&subs->stream->chip->shutdown_rwsem);
412         return ret;
413 }
414
415 /*
416  * hw_free callback
417  *
418  * reset the audio format and release the buffer
419  */
420 static int snd_usb_hw_free(struct snd_pcm_substream *substream)
421 {
422         struct snd_usb_substream *subs = substream->runtime->private_data;
423
424         subs->cur_audiofmt = NULL;
425         subs->cur_rate = 0;
426         subs->period_bytes = 0;
427         down_read(&subs->stream->chip->shutdown_rwsem);
428         snd_usb_release_substream_urbs(subs, 0);
429         up_read(&subs->stream->chip->shutdown_rwsem);
430         return snd_pcm_lib_free_vmalloc_buffer(substream);
431 }
432
433 /*
434  * prepare callback
435  *
436  * only a few subtle things...
437  */
438 static int snd_usb_pcm_prepare(struct snd_pcm_substream *substream)
439 {
440         struct snd_pcm_runtime *runtime = substream->runtime;
441         struct snd_usb_substream *subs = runtime->private_data;
442         int ret = 0;
443
444         if (! subs->cur_audiofmt) {
445                 snd_printk(KERN_ERR "usbaudio: no format is specified!\n");
446                 return -ENXIO;
447         }
448
449         down_read(&subs->stream->chip->shutdown_rwsem);
450         if (subs->stream->chip->shutdown) {
451                 ret = -ENODEV;
452                 goto unlock;
453         }
454         /* some unit conversions in runtime */
455         subs->maxframesize = bytes_to_frames(runtime, subs->maxpacksize);
456         subs->curframesize = bytes_to_frames(runtime, subs->curpacksize);
457
458         /* reset the pointer */
459         subs->hwptr_done = 0;
460         subs->transfer_done = 0;
461         subs->phase = 0;
462         subs->last_delay = 0;
463         subs->last_frame_number = 0;
464         runtime->delay = 0;
465
466         ret = snd_usb_substream_prepare(subs, runtime);
467  unlock:
468         up_read(&subs->stream->chip->shutdown_rwsem);
469         return ret;
470 }
471
472 static struct snd_pcm_hardware snd_usb_hardware =
473 {
474         .info =                 SNDRV_PCM_INFO_MMAP |
475                                 SNDRV_PCM_INFO_MMAP_VALID |
476                                 SNDRV_PCM_INFO_BATCH |
477                                 SNDRV_PCM_INFO_INTERLEAVED |
478                                 SNDRV_PCM_INFO_BLOCK_TRANSFER |
479                                 SNDRV_PCM_INFO_PAUSE,
480         .buffer_bytes_max =     1024 * 1024,
481         .period_bytes_min =     64,
482         .period_bytes_max =     512 * 1024,
483         .periods_min =          2,
484         .periods_max =          1024,
485 };
486
487 static int hw_check_valid_format(struct snd_usb_substream *subs,
488                                  struct snd_pcm_hw_params *params,
489                                  struct audioformat *fp)
490 {
491         struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
492         struct snd_interval *ct = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
493         struct snd_mask *fmts = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
494         struct snd_interval *pt = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
495         struct snd_mask check_fmts;
496         unsigned int ptime;
497
498         /* check the format */
499         snd_mask_none(&check_fmts);
500         check_fmts.bits[0] = (u32)fp->formats;
501         check_fmts.bits[1] = (u32)(fp->formats >> 32);
502         snd_mask_intersect(&check_fmts, fmts);
503         if (snd_mask_empty(&check_fmts)) {
504                 hwc_debug("   > check: no supported format %d\n", fp->format);
505                 return 0;
506         }
507         /* check the channels */
508         if (fp->channels < ct->min || fp->channels > ct->max) {
509                 hwc_debug("   > check: no valid channels %d (%d/%d)\n", fp->channels, ct->min, ct->max);
510                 return 0;
511         }
512         /* check the rate is within the range */
513         if (fp->rate_min > it->max || (fp->rate_min == it->max && it->openmax)) {
514                 hwc_debug("   > check: rate_min %d > max %d\n", fp->rate_min, it->max);
515                 return 0;
516         }
517         if (fp->rate_max < it->min || (fp->rate_max == it->min && it->openmin)) {
518                 hwc_debug("   > check: rate_max %d < min %d\n", fp->rate_max, it->min);
519                 return 0;
520         }
521         /* check whether the period time is >= the data packet interval */
522         if (subs->speed != USB_SPEED_FULL) {
523                 ptime = 125 * (1 << fp->datainterval);
524                 if (ptime > pt->max || (ptime == pt->max && pt->openmax)) {
525                         hwc_debug("   > check: ptime %u > max %u\n", ptime, pt->max);
526                         return 0;
527                 }
528         }
529         return 1;
530 }
531
532 static int hw_rule_rate(struct snd_pcm_hw_params *params,
533                         struct snd_pcm_hw_rule *rule)
534 {
535         struct snd_usb_substream *subs = rule->private;
536         struct list_head *p;
537         struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
538         unsigned int rmin, rmax;
539         int changed;
540
541         hwc_debug("hw_rule_rate: (%d,%d)\n", it->min, it->max);
542         changed = 0;
543         rmin = rmax = 0;
544         list_for_each(p, &subs->fmt_list) {
545                 struct audioformat *fp;
546                 fp = list_entry(p, struct audioformat, list);
547                 if (!hw_check_valid_format(subs, params, fp))
548                         continue;
549                 if (changed++) {
550                         if (rmin > fp->rate_min)
551                                 rmin = fp->rate_min;
552                         if (rmax < fp->rate_max)
553                                 rmax = fp->rate_max;
554                 } else {
555                         rmin = fp->rate_min;
556                         rmax = fp->rate_max;
557                 }
558         }
559
560         if (!changed) {
561                 hwc_debug("  --> get empty\n");
562                 it->empty = 1;
563                 return -EINVAL;
564         }
565
566         changed = 0;
567         if (it->min < rmin) {
568                 it->min = rmin;
569                 it->openmin = 0;
570                 changed = 1;
571         }
572         if (it->max > rmax) {
573                 it->max = rmax;
574                 it->openmax = 0;
575                 changed = 1;
576         }
577         if (snd_interval_checkempty(it)) {
578                 it->empty = 1;
579                 return -EINVAL;
580         }
581         hwc_debug("  --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
582         return changed;
583 }
584
585
586 static int hw_rule_channels(struct snd_pcm_hw_params *params,
587                             struct snd_pcm_hw_rule *rule)
588 {
589         struct snd_usb_substream *subs = rule->private;
590         struct list_head *p;
591         struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
592         unsigned int rmin, rmax;
593         int changed;
594
595         hwc_debug("hw_rule_channels: (%d,%d)\n", it->min, it->max);
596         changed = 0;
597         rmin = rmax = 0;
598         list_for_each(p, &subs->fmt_list) {
599                 struct audioformat *fp;
600                 fp = list_entry(p, struct audioformat, list);
601                 if (!hw_check_valid_format(subs, params, fp))
602                         continue;
603                 if (changed++) {
604                         if (rmin > fp->channels)
605                                 rmin = fp->channels;
606                         if (rmax < fp->channels)
607                                 rmax = fp->channels;
608                 } else {
609                         rmin = fp->channels;
610                         rmax = fp->channels;
611                 }
612         }
613
614         if (!changed) {
615                 hwc_debug("  --> get empty\n");
616                 it->empty = 1;
617                 return -EINVAL;
618         }
619
620         changed = 0;
621         if (it->min < rmin) {
622                 it->min = rmin;
623                 it->openmin = 0;
624                 changed = 1;
625         }
626         if (it->max > rmax) {
627                 it->max = rmax;
628                 it->openmax = 0;
629                 changed = 1;
630         }
631         if (snd_interval_checkempty(it)) {
632                 it->empty = 1;
633                 return -EINVAL;
634         }
635         hwc_debug("  --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
636         return changed;
637 }
638
639 static int hw_rule_format(struct snd_pcm_hw_params *params,
640                           struct snd_pcm_hw_rule *rule)
641 {
642         struct snd_usb_substream *subs = rule->private;
643         struct list_head *p;
644         struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
645         u64 fbits;
646         u32 oldbits[2];
647         int changed;
648
649         hwc_debug("hw_rule_format: %x:%x\n", fmt->bits[0], fmt->bits[1]);
650         fbits = 0;
651         list_for_each(p, &subs->fmt_list) {
652                 struct audioformat *fp;
653                 fp = list_entry(p, struct audioformat, list);
654                 if (!hw_check_valid_format(subs, params, fp))
655                         continue;
656                 fbits |= fp->formats;
657         }
658
659         oldbits[0] = fmt->bits[0];
660         oldbits[1] = fmt->bits[1];
661         fmt->bits[0] &= (u32)fbits;
662         fmt->bits[1] &= (u32)(fbits >> 32);
663         if (!fmt->bits[0] && !fmt->bits[1]) {
664                 hwc_debug("  --> get empty\n");
665                 return -EINVAL;
666         }
667         changed = (oldbits[0] != fmt->bits[0] || oldbits[1] != fmt->bits[1]);
668         hwc_debug("  --> %x:%x (changed = %d)\n", fmt->bits[0], fmt->bits[1], changed);
669         return changed;
670 }
671
672 static int hw_rule_period_time(struct snd_pcm_hw_params *params,
673                                struct snd_pcm_hw_rule *rule)
674 {
675         struct snd_usb_substream *subs = rule->private;
676         struct audioformat *fp;
677         struct snd_interval *it;
678         unsigned char min_datainterval;
679         unsigned int pmin;
680         int changed;
681
682         it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
683         hwc_debug("hw_rule_period_time: (%u,%u)\n", it->min, it->max);
684         min_datainterval = 0xff;
685         list_for_each_entry(fp, &subs->fmt_list, list) {
686                 if (!hw_check_valid_format(subs, params, fp))
687                         continue;
688                 min_datainterval = min(min_datainterval, fp->datainterval);
689         }
690         if (min_datainterval == 0xff) {
691                 hwc_debug("  --> get empty\n");
692                 it->empty = 1;
693                 return -EINVAL;
694         }
695         pmin = 125 * (1 << min_datainterval);
696         changed = 0;
697         if (it->min < pmin) {
698                 it->min = pmin;
699                 it->openmin = 0;
700                 changed = 1;
701         }
702         if (snd_interval_checkempty(it)) {
703                 it->empty = 1;
704                 return -EINVAL;
705         }
706         hwc_debug("  --> (%u,%u) (changed = %d)\n", it->min, it->max, changed);
707         return changed;
708 }
709
710 /*
711  *  If the device supports unusual bit rates, does the request meet these?
712  */
713 static int snd_usb_pcm_check_knot(struct snd_pcm_runtime *runtime,
714                                   struct snd_usb_substream *subs)
715 {
716         struct audioformat *fp;
717         int count = 0, needs_knot = 0;
718         int err;
719
720         kfree(subs->rate_list.list);
721         subs->rate_list.list = NULL;
722
723         list_for_each_entry(fp, &subs->fmt_list, list) {
724                 if (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)
725                         return 0;
726                 count += fp->nr_rates;
727                 if (fp->rates & SNDRV_PCM_RATE_KNOT)
728                         needs_knot = 1;
729         }
730         if (!needs_knot)
731                 return 0;
732
733         subs->rate_list.list = kmalloc(sizeof(int) * count, GFP_KERNEL);
734         if (!subs->rate_list.list)
735                 return -ENOMEM;
736         subs->rate_list.count = count;
737         subs->rate_list.mask = 0;
738         count = 0;
739         list_for_each_entry(fp, &subs->fmt_list, list) {
740                 int i;
741                 for (i = 0; i < fp->nr_rates; i++)
742                         subs->rate_list.list[count++] = fp->rate_table[i];
743         }
744         err = snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
745                                          &subs->rate_list);
746         if (err < 0)
747                 return err;
748
749         return 0;
750 }
751
752
753 /*
754  * set up the runtime hardware information.
755  */
756
757 static int setup_hw_info(struct snd_pcm_runtime *runtime, struct snd_usb_substream *subs)
758 {
759         struct list_head *p;
760         unsigned int pt, ptmin;
761         int param_period_time_if_needed;
762         int err;
763
764         runtime->hw.formats = subs->formats;
765
766         runtime->hw.rate_min = 0x7fffffff;
767         runtime->hw.rate_max = 0;
768         runtime->hw.channels_min = 256;
769         runtime->hw.channels_max = 0;
770         runtime->hw.rates = 0;
771         ptmin = UINT_MAX;
772         /* check min/max rates and channels */
773         list_for_each(p, &subs->fmt_list) {
774                 struct audioformat *fp;
775                 fp = list_entry(p, struct audioformat, list);
776                 runtime->hw.rates |= fp->rates;
777                 if (runtime->hw.rate_min > fp->rate_min)
778                         runtime->hw.rate_min = fp->rate_min;
779                 if (runtime->hw.rate_max < fp->rate_max)
780                         runtime->hw.rate_max = fp->rate_max;
781                 if (runtime->hw.channels_min > fp->channels)
782                         runtime->hw.channels_min = fp->channels;
783                 if (runtime->hw.channels_max < fp->channels)
784                         runtime->hw.channels_max = fp->channels;
785                 if (fp->fmt_type == UAC_FORMAT_TYPE_II && fp->frame_size > 0) {
786                         /* FIXME: there might be more than one audio formats... */
787                         runtime->hw.period_bytes_min = runtime->hw.period_bytes_max =
788                                 fp->frame_size;
789                 }
790                 pt = 125 * (1 << fp->datainterval);
791                 ptmin = min(ptmin, pt);
792         }
793         err = snd_usb_autoresume(subs->stream->chip);
794         if (err < 0)
795                 return err;
796
797         param_period_time_if_needed = SNDRV_PCM_HW_PARAM_PERIOD_TIME;
798         if (subs->speed == USB_SPEED_FULL)
799                 /* full speed devices have fixed data packet interval */
800                 ptmin = 1000;
801         if (ptmin == 1000)
802                 /* if period time doesn't go below 1 ms, no rules needed */
803                 param_period_time_if_needed = -1;
804         snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
805                                      ptmin, UINT_MAX);
806
807         if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
808                                        hw_rule_rate, subs,
809                                        SNDRV_PCM_HW_PARAM_FORMAT,
810                                        SNDRV_PCM_HW_PARAM_CHANNELS,
811                                        param_period_time_if_needed,
812                                        -1)) < 0)
813                 goto rep_err;
814         if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
815                                        hw_rule_channels, subs,
816                                        SNDRV_PCM_HW_PARAM_FORMAT,
817                                        SNDRV_PCM_HW_PARAM_RATE,
818                                        param_period_time_if_needed,
819                                        -1)) < 0)
820                 goto rep_err;
821         if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
822                                        hw_rule_format, subs,
823                                        SNDRV_PCM_HW_PARAM_RATE,
824                                        SNDRV_PCM_HW_PARAM_CHANNELS,
825                                        param_period_time_if_needed,
826                                        -1)) < 0)
827                 goto rep_err;
828         if (param_period_time_if_needed >= 0) {
829                 err = snd_pcm_hw_rule_add(runtime, 0,
830                                           SNDRV_PCM_HW_PARAM_PERIOD_TIME,
831                                           hw_rule_period_time, subs,
832                                           SNDRV_PCM_HW_PARAM_FORMAT,
833                                           SNDRV_PCM_HW_PARAM_CHANNELS,
834                                           SNDRV_PCM_HW_PARAM_RATE,
835                                           -1);
836                 if (err < 0)
837                         goto rep_err;
838         }
839         if ((err = snd_usb_pcm_check_knot(runtime, subs)) < 0)
840                 goto rep_err;
841         return 0;
842
843 rep_err:
844         snd_usb_autosuspend(subs->stream->chip);
845         return err;
846 }
847
848 static int snd_usb_pcm_open(struct snd_pcm_substream *substream, int direction)
849 {
850         struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
851         struct snd_pcm_runtime *runtime = substream->runtime;
852         struct snd_usb_substream *subs = &as->substream[direction];
853
854         subs->interface = -1;
855         subs->altset_idx = 0;
856         runtime->hw = snd_usb_hardware;
857         runtime->private_data = subs;
858         subs->pcm_substream = substream;
859         /* runtime PM is also done there */
860         return setup_hw_info(runtime, subs);
861 }
862
863 static int snd_usb_pcm_close(struct snd_pcm_substream *substream, int direction)
864 {
865         struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
866         struct snd_usb_substream *subs = &as->substream[direction];
867
868         if (!as->chip->shutdown && subs->interface >= 0) {
869                 usb_set_interface(subs->dev, subs->interface, 0);
870                 subs->interface = -1;
871         }
872         subs->pcm_substream = NULL;
873         snd_usb_autosuspend(subs->stream->chip);
874         return 0;
875 }
876
877 static int snd_usb_playback_open(struct snd_pcm_substream *substream)
878 {
879         return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_PLAYBACK);
880 }
881
882 static int snd_usb_playback_close(struct snd_pcm_substream *substream)
883 {
884         return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_PLAYBACK);
885 }
886
887 static int snd_usb_capture_open(struct snd_pcm_substream *substream)
888 {
889         return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_CAPTURE);
890 }
891
892 static int snd_usb_capture_close(struct snd_pcm_substream *substream)
893 {
894         return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_CAPTURE);
895 }
896
897 static struct snd_pcm_ops snd_usb_playback_ops = {
898         .open =         snd_usb_playback_open,
899         .close =        snd_usb_playback_close,
900         .ioctl =        snd_pcm_lib_ioctl,
901         .hw_params =    snd_usb_hw_params,
902         .hw_free =      snd_usb_hw_free,
903         .prepare =      snd_usb_pcm_prepare,
904         .trigger =      snd_usb_substream_playback_trigger,
905         .pointer =      snd_usb_pcm_pointer,
906         .page =         snd_pcm_lib_get_vmalloc_page,
907         .mmap =         snd_pcm_lib_mmap_vmalloc,
908 };
909
910 static struct snd_pcm_ops snd_usb_capture_ops = {
911         .open =         snd_usb_capture_open,
912         .close =        snd_usb_capture_close,
913         .ioctl =        snd_pcm_lib_ioctl,
914         .hw_params =    snd_usb_hw_params,
915         .hw_free =      snd_usb_hw_free,
916         .prepare =      snd_usb_pcm_prepare,
917         .trigger =      snd_usb_substream_capture_trigger,
918         .pointer =      snd_usb_pcm_pointer,
919         .page =         snd_pcm_lib_get_vmalloc_page,
920         .mmap =         snd_pcm_lib_mmap_vmalloc,
921 };
922
923 void snd_usb_set_pcm_ops(struct snd_pcm *pcm, int stream)
924 {
925         snd_pcm_set_ops(pcm, stream,
926                         stream == SNDRV_PCM_STREAM_PLAYBACK ?
927                         &snd_usb_playback_ops : &snd_usb_capture_ops);
928 }