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