Commit 8866f405efd4171f9d9c91901d2dd02f01bacb60

Authored by Xi Wang
Committed by Takashi Iwai
1 parent 27c3afe6e1

ALSA: usb-audio: avoid integer overflow in create_fixed_stream_quirk()

A malicious USB device could feed in a large nr_rates value.  This would
cause the subsequent call to kmemdup() to allocate a smaller buffer than
expected, leading to out-of-bounds access.

This patch validates the nr_rates value and reuses the limit introduced
in commit 4fa0e81b ("ALSA: usb-audio: fix possible hang and overflow
in parse_uac2_sample_rate_range()").

Signed-off-by: Xi Wang <xi.wang@gmail.com>
Signed-off-by: Takashi Iwai <tiwai@suse.de>

Showing 3 changed files with 7 additions and 4 deletions Inline Diff

1 #ifndef __USBAUDIO_CARD_H 1 #ifndef __USBAUDIO_CARD_H
2 #define __USBAUDIO_CARD_H 2 #define __USBAUDIO_CARD_H
3 3
4 #define MAX_NR_RATES 1024
4 #define MAX_PACKS 20 5 #define MAX_PACKS 20
5 #define MAX_PACKS_HS (MAX_PACKS * 8) /* in high speed mode */ 6 #define MAX_PACKS_HS (MAX_PACKS * 8) /* in high speed mode */
6 #define MAX_URBS 8 7 #define MAX_URBS 8
7 #define SYNC_URBS 4 /* always four urbs for sync */ 8 #define SYNC_URBS 4 /* always four urbs for sync */
8 #define MAX_QUEUE 24 /* try not to exceed this queue length, in ms */ 9 #define MAX_QUEUE 24 /* try not to exceed this queue length, in ms */
9 10
10 struct audioformat { 11 struct audioformat {
11 struct list_head list; 12 struct list_head list;
12 u64 formats; /* ALSA format bits */ 13 u64 formats; /* ALSA format bits */
13 unsigned int channels; /* # channels */ 14 unsigned int channels; /* # channels */
14 unsigned int fmt_type; /* USB audio format type (1-3) */ 15 unsigned int fmt_type; /* USB audio format type (1-3) */
15 unsigned int frame_size; /* samples per frame for non-audio */ 16 unsigned int frame_size; /* samples per frame for non-audio */
16 int iface; /* interface number */ 17 int iface; /* interface number */
17 unsigned char altsetting; /* corresponding alternate setting */ 18 unsigned char altsetting; /* corresponding alternate setting */
18 unsigned char altset_idx; /* array index of altenate setting */ 19 unsigned char altset_idx; /* array index of altenate setting */
19 unsigned char attributes; /* corresponding attributes of cs endpoint */ 20 unsigned char attributes; /* corresponding attributes of cs endpoint */
20 unsigned char endpoint; /* endpoint */ 21 unsigned char endpoint; /* endpoint */
21 unsigned char ep_attr; /* endpoint attributes */ 22 unsigned char ep_attr; /* endpoint attributes */
22 unsigned char datainterval; /* log_2 of data packet interval */ 23 unsigned char datainterval; /* log_2 of data packet interval */
23 unsigned int maxpacksize; /* max. packet size */ 24 unsigned int maxpacksize; /* max. packet size */
24 unsigned int rates; /* rate bitmasks */ 25 unsigned int rates; /* rate bitmasks */
25 unsigned int rate_min, rate_max; /* min/max rates */ 26 unsigned int rate_min, rate_max; /* min/max rates */
26 unsigned int nr_rates; /* number of rate table entries */ 27 unsigned int nr_rates; /* number of rate table entries */
27 unsigned int *rate_table; /* rate table */ 28 unsigned int *rate_table; /* rate table */
28 unsigned char clock; /* associated clock */ 29 unsigned char clock; /* associated clock */
29 }; 30 };
30 31
31 struct snd_usb_substream; 32 struct snd_usb_substream;
32 33
33 struct snd_urb_ctx { 34 struct snd_urb_ctx {
34 struct urb *urb; 35 struct urb *urb;
35 unsigned int buffer_size; /* size of data buffer, if data URB */ 36 unsigned int buffer_size; /* size of data buffer, if data URB */
36 struct snd_usb_substream *subs; 37 struct snd_usb_substream *subs;
37 int index; /* index for urb array */ 38 int index; /* index for urb array */
38 int packets; /* number of packets per urb */ 39 int packets; /* number of packets per urb */
39 }; 40 };
40 41
41 struct snd_urb_ops { 42 struct snd_urb_ops {
42 int (*prepare)(struct snd_usb_substream *subs, struct snd_pcm_runtime *runtime, struct urb *u); 43 int (*prepare)(struct snd_usb_substream *subs, struct snd_pcm_runtime *runtime, struct urb *u);
43 int (*retire)(struct snd_usb_substream *subs, struct snd_pcm_runtime *runtime, struct urb *u); 44 int (*retire)(struct snd_usb_substream *subs, struct snd_pcm_runtime *runtime, struct urb *u);
44 int (*prepare_sync)(struct snd_usb_substream *subs, struct snd_pcm_runtime *runtime, struct urb *u); 45 int (*prepare_sync)(struct snd_usb_substream *subs, struct snd_pcm_runtime *runtime, struct urb *u);
45 int (*retire_sync)(struct snd_usb_substream *subs, struct snd_pcm_runtime *runtime, struct urb *u); 46 int (*retire_sync)(struct snd_usb_substream *subs, struct snd_pcm_runtime *runtime, struct urb *u);
46 }; 47 };
47 48
48 struct snd_usb_substream { 49 struct snd_usb_substream {
49 struct snd_usb_stream *stream; 50 struct snd_usb_stream *stream;
50 struct usb_device *dev; 51 struct usb_device *dev;
51 struct snd_pcm_substream *pcm_substream; 52 struct snd_pcm_substream *pcm_substream;
52 int direction; /* playback or capture */ 53 int direction; /* playback or capture */
53 int interface; /* current interface */ 54 int interface; /* current interface */
54 int endpoint; /* assigned endpoint */ 55 int endpoint; /* assigned endpoint */
55 struct audioformat *cur_audiofmt; /* current audioformat pointer (for hw_params callback) */ 56 struct audioformat *cur_audiofmt; /* current audioformat pointer (for hw_params callback) */
56 unsigned int cur_rate; /* current rate (for hw_params callback) */ 57 unsigned int cur_rate; /* current rate (for hw_params callback) */
57 unsigned int period_bytes; /* current period bytes (for hw_params callback) */ 58 unsigned int period_bytes; /* current period bytes (for hw_params callback) */
58 unsigned int altset_idx; /* USB data format: index of alternate setting */ 59 unsigned int altset_idx; /* USB data format: index of alternate setting */
59 unsigned int datapipe; /* the data i/o pipe */ 60 unsigned int datapipe; /* the data i/o pipe */
60 unsigned int syncpipe; /* 1 - async out or adaptive in */ 61 unsigned int syncpipe; /* 1 - async out or adaptive in */
61 unsigned int datainterval; /* log_2 of data packet interval */ 62 unsigned int datainterval; /* log_2 of data packet interval */
62 unsigned int syncinterval; /* P for adaptive mode, 0 otherwise */ 63 unsigned int syncinterval; /* P for adaptive mode, 0 otherwise */
63 unsigned int freqn; /* nominal sampling rate in fs/fps in Q16.16 format */ 64 unsigned int freqn; /* nominal sampling rate in fs/fps in Q16.16 format */
64 unsigned int freqm; /* momentary sampling rate in fs/fps in Q16.16 format */ 65 unsigned int freqm; /* momentary sampling rate in fs/fps in Q16.16 format */
65 int freqshift; /* how much to shift the feedback value to get Q16.16 */ 66 int freqshift; /* how much to shift the feedback value to get Q16.16 */
66 unsigned int freqmax; /* maximum sampling rate, used for buffer management */ 67 unsigned int freqmax; /* maximum sampling rate, used for buffer management */
67 unsigned int phase; /* phase accumulator */ 68 unsigned int phase; /* phase accumulator */
68 unsigned int maxpacksize; /* max packet size in bytes */ 69 unsigned int maxpacksize; /* max packet size in bytes */
69 unsigned int maxframesize; /* max packet size in frames */ 70 unsigned int maxframesize; /* max packet size in frames */
70 unsigned int curpacksize; /* current packet size in bytes (for capture) */ 71 unsigned int curpacksize; /* current packet size in bytes (for capture) */
71 unsigned int curframesize; /* current packet size in frames (for capture) */ 72 unsigned int curframesize; /* current packet size in frames (for capture) */
72 unsigned int syncmaxsize; /* sync endpoint packet size */ 73 unsigned int syncmaxsize; /* sync endpoint packet size */
73 unsigned int fill_max: 1; /* fill max packet size always */ 74 unsigned int fill_max: 1; /* fill max packet size always */
74 unsigned int txfr_quirk:1; /* allow sub-frame alignment */ 75 unsigned int txfr_quirk:1; /* allow sub-frame alignment */
75 unsigned int fmt_type; /* USB audio format type (1-3) */ 76 unsigned int fmt_type; /* USB audio format type (1-3) */
76 77
77 unsigned int running: 1; /* running status */ 78 unsigned int running: 1; /* running status */
78 79
79 unsigned int hwptr_done; /* processed byte position in the buffer */ 80 unsigned int hwptr_done; /* processed byte position in the buffer */
80 unsigned int transfer_done; /* processed frames since last period update */ 81 unsigned int transfer_done; /* processed frames since last period update */
81 unsigned long active_mask; /* bitmask of active urbs */ 82 unsigned long active_mask; /* bitmask of active urbs */
82 unsigned long unlink_mask; /* bitmask of unlinked urbs */ 83 unsigned long unlink_mask; /* bitmask of unlinked urbs */
83 84
84 unsigned int nurbs; /* # urbs */ 85 unsigned int nurbs; /* # urbs */
85 struct snd_urb_ctx dataurb[MAX_URBS]; /* data urb table */ 86 struct snd_urb_ctx dataurb[MAX_URBS]; /* data urb table */
86 struct snd_urb_ctx syncurb[SYNC_URBS]; /* sync urb table */ 87 struct snd_urb_ctx syncurb[SYNC_URBS]; /* sync urb table */
87 char *syncbuf; /* sync buffer for all sync URBs */ 88 char *syncbuf; /* sync buffer for all sync URBs */
88 dma_addr_t sync_dma; /* DMA address of syncbuf */ 89 dma_addr_t sync_dma; /* DMA address of syncbuf */
89 90
90 u64 formats; /* format bitmasks (all or'ed) */ 91 u64 formats; /* format bitmasks (all or'ed) */
91 unsigned int num_formats; /* number of supported audio formats (list) */ 92 unsigned int num_formats; /* number of supported audio formats (list) */
92 struct list_head fmt_list; /* format list */ 93 struct list_head fmt_list; /* format list */
93 struct snd_pcm_hw_constraint_list rate_list; /* limited rates */ 94 struct snd_pcm_hw_constraint_list rate_list; /* limited rates */
94 spinlock_t lock; 95 spinlock_t lock;
95 96
96 struct snd_urb_ops ops; /* callbacks (must be filled at init) */ 97 struct snd_urb_ops ops; /* callbacks (must be filled at init) */
97 int last_frame_number; /* stored frame number */ 98 int last_frame_number; /* stored frame number */
98 int last_delay; /* stored delay */ 99 int last_delay; /* stored delay */
99 }; 100 };
100 101
101 struct snd_usb_stream { 102 struct snd_usb_stream {
102 struct snd_usb_audio *chip; 103 struct snd_usb_audio *chip;
103 struct snd_pcm *pcm; 104 struct snd_pcm *pcm;
104 int pcm_index; 105 int pcm_index;
105 unsigned int fmt_type; /* USB audio format type (1-3) */ 106 unsigned int fmt_type; /* USB audio format type (1-3) */
106 struct snd_usb_substream substream[2]; 107 struct snd_usb_substream substream[2];
107 struct list_head list; 108 struct list_head list;
108 }; 109 };
109 110
110 #endif /* __USBAUDIO_CARD_H */ 111 #endif /* __USBAUDIO_CARD_H */
111 112
1 /* 1 /*
2 * This program is free software; you can redistribute it and/or modify 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 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 4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version. 5 * (at your option) any later version.
6 * 6 *
7 * This program is distributed in the hope that it will be useful, 7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of 8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details. 10 * GNU General Public License for more details.
11 * 11 *
12 * You should have received a copy of the GNU General Public License 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 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 14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15 * 15 *
16 */ 16 */
17 17
18 #include <linux/init.h> 18 #include <linux/init.h>
19 #include <linux/slab.h> 19 #include <linux/slab.h>
20 #include <linux/usb.h> 20 #include <linux/usb.h>
21 #include <linux/usb/audio.h> 21 #include <linux/usb/audio.h>
22 #include <linux/usb/audio-v2.h> 22 #include <linux/usb/audio-v2.h>
23 23
24 #include <sound/core.h> 24 #include <sound/core.h>
25 #include <sound/pcm.h> 25 #include <sound/pcm.h>
26 26
27 #include "usbaudio.h" 27 #include "usbaudio.h"
28 #include "card.h" 28 #include "card.h"
29 #include "quirks.h" 29 #include "quirks.h"
30 #include "helper.h" 30 #include "helper.h"
31 #include "debug.h" 31 #include "debug.h"
32 #include "clock.h" 32 #include "clock.h"
33 #include "format.h" 33 #include "format.h"
34 34
35 /* 35 /*
36 * parse the audio format type I descriptor 36 * parse the audio format type I descriptor
37 * and returns the corresponding pcm format 37 * and returns the corresponding pcm format
38 * 38 *
39 * @dev: usb device 39 * @dev: usb device
40 * @fp: audioformat record 40 * @fp: audioformat record
41 * @format: the format tag (wFormatTag) 41 * @format: the format tag (wFormatTag)
42 * @fmt: the format type descriptor 42 * @fmt: the format type descriptor
43 */ 43 */
44 static u64 parse_audio_format_i_type(struct snd_usb_audio *chip, 44 static u64 parse_audio_format_i_type(struct snd_usb_audio *chip,
45 struct audioformat *fp, 45 struct audioformat *fp,
46 int format, void *_fmt, 46 int format, void *_fmt,
47 int protocol) 47 int protocol)
48 { 48 {
49 int sample_width, sample_bytes; 49 int sample_width, sample_bytes;
50 u64 pcm_formats; 50 u64 pcm_formats;
51 51
52 switch (protocol) { 52 switch (protocol) {
53 case UAC_VERSION_1: 53 case UAC_VERSION_1:
54 default: { 54 default: {
55 struct uac_format_type_i_discrete_descriptor *fmt = _fmt; 55 struct uac_format_type_i_discrete_descriptor *fmt = _fmt;
56 sample_width = fmt->bBitResolution; 56 sample_width = fmt->bBitResolution;
57 sample_bytes = fmt->bSubframeSize; 57 sample_bytes = fmt->bSubframeSize;
58 format = 1 << format; 58 format = 1 << format;
59 break; 59 break;
60 } 60 }
61 61
62 case UAC_VERSION_2: { 62 case UAC_VERSION_2: {
63 struct uac_format_type_i_ext_descriptor *fmt = _fmt; 63 struct uac_format_type_i_ext_descriptor *fmt = _fmt;
64 sample_width = fmt->bBitResolution; 64 sample_width = fmt->bBitResolution;
65 sample_bytes = fmt->bSubslotSize; 65 sample_bytes = fmt->bSubslotSize;
66 format <<= 1; 66 format <<= 1;
67 break; 67 break;
68 } 68 }
69 } 69 }
70 70
71 pcm_formats = 0; 71 pcm_formats = 0;
72 72
73 if (format == 0 || format == (1 << UAC_FORMAT_TYPE_I_UNDEFINED)) { 73 if (format == 0 || format == (1 << UAC_FORMAT_TYPE_I_UNDEFINED)) {
74 /* some devices don't define this correctly... */ 74 /* some devices don't define this correctly... */
75 snd_printdd(KERN_INFO "%d:%u:%d : format type 0 is detected, processed as PCM\n", 75 snd_printdd(KERN_INFO "%d:%u:%d : format type 0 is detected, processed as PCM\n",
76 chip->dev->devnum, fp->iface, fp->altsetting); 76 chip->dev->devnum, fp->iface, fp->altsetting);
77 format = 1 << UAC_FORMAT_TYPE_I_PCM; 77 format = 1 << UAC_FORMAT_TYPE_I_PCM;
78 } 78 }
79 if (format & (1 << UAC_FORMAT_TYPE_I_PCM)) { 79 if (format & (1 << UAC_FORMAT_TYPE_I_PCM)) {
80 if (chip->usb_id == USB_ID(0x0582, 0x0016) /* Edirol SD-90 */ && 80 if (chip->usb_id == USB_ID(0x0582, 0x0016) /* Edirol SD-90 */ &&
81 sample_width == 24 && sample_bytes == 2) 81 sample_width == 24 && sample_bytes == 2)
82 sample_bytes = 3; 82 sample_bytes = 3;
83 else if (sample_width > sample_bytes * 8) { 83 else if (sample_width > sample_bytes * 8) {
84 snd_printk(KERN_INFO "%d:%u:%d : sample bitwidth %d in over sample bytes %d\n", 84 snd_printk(KERN_INFO "%d:%u:%d : sample bitwidth %d in over sample bytes %d\n",
85 chip->dev->devnum, fp->iface, fp->altsetting, 85 chip->dev->devnum, fp->iface, fp->altsetting,
86 sample_width, sample_bytes); 86 sample_width, sample_bytes);
87 } 87 }
88 /* check the format byte size */ 88 /* check the format byte size */
89 switch (sample_bytes) { 89 switch (sample_bytes) {
90 case 1: 90 case 1:
91 pcm_formats |= SNDRV_PCM_FMTBIT_S8; 91 pcm_formats |= SNDRV_PCM_FMTBIT_S8;
92 break; 92 break;
93 case 2: 93 case 2:
94 if (snd_usb_is_big_endian_format(chip, fp)) 94 if (snd_usb_is_big_endian_format(chip, fp))
95 pcm_formats |= SNDRV_PCM_FMTBIT_S16_BE; /* grrr, big endian!! */ 95 pcm_formats |= SNDRV_PCM_FMTBIT_S16_BE; /* grrr, big endian!! */
96 else 96 else
97 pcm_formats |= SNDRV_PCM_FMTBIT_S16_LE; 97 pcm_formats |= SNDRV_PCM_FMTBIT_S16_LE;
98 break; 98 break;
99 case 3: 99 case 3:
100 if (snd_usb_is_big_endian_format(chip, fp)) 100 if (snd_usb_is_big_endian_format(chip, fp))
101 pcm_formats |= SNDRV_PCM_FMTBIT_S24_3BE; /* grrr, big endian!! */ 101 pcm_formats |= SNDRV_PCM_FMTBIT_S24_3BE; /* grrr, big endian!! */
102 else 102 else
103 pcm_formats |= SNDRV_PCM_FMTBIT_S24_3LE; 103 pcm_formats |= SNDRV_PCM_FMTBIT_S24_3LE;
104 break; 104 break;
105 case 4: 105 case 4:
106 pcm_formats |= SNDRV_PCM_FMTBIT_S32_LE; 106 pcm_formats |= SNDRV_PCM_FMTBIT_S32_LE;
107 break; 107 break;
108 default: 108 default:
109 snd_printk(KERN_INFO "%d:%u:%d : unsupported sample bitwidth %d in %d bytes\n", 109 snd_printk(KERN_INFO "%d:%u:%d : unsupported sample bitwidth %d in %d bytes\n",
110 chip->dev->devnum, fp->iface, fp->altsetting, 110 chip->dev->devnum, fp->iface, fp->altsetting,
111 sample_width, sample_bytes); 111 sample_width, sample_bytes);
112 break; 112 break;
113 } 113 }
114 } 114 }
115 if (format & (1 << UAC_FORMAT_TYPE_I_PCM8)) { 115 if (format & (1 << UAC_FORMAT_TYPE_I_PCM8)) {
116 /* Dallas DS4201 workaround: it advertises U8 format, but really 116 /* Dallas DS4201 workaround: it advertises U8 format, but really
117 supports S8. */ 117 supports S8. */
118 if (chip->usb_id == USB_ID(0x04fa, 0x4201)) 118 if (chip->usb_id == USB_ID(0x04fa, 0x4201))
119 pcm_formats |= SNDRV_PCM_FMTBIT_S8; 119 pcm_formats |= SNDRV_PCM_FMTBIT_S8;
120 else 120 else
121 pcm_formats |= SNDRV_PCM_FMTBIT_U8; 121 pcm_formats |= SNDRV_PCM_FMTBIT_U8;
122 } 122 }
123 if (format & (1 << UAC_FORMAT_TYPE_I_IEEE_FLOAT)) { 123 if (format & (1 << UAC_FORMAT_TYPE_I_IEEE_FLOAT)) {
124 pcm_formats |= SNDRV_PCM_FMTBIT_FLOAT_LE; 124 pcm_formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
125 } 125 }
126 if (format & (1 << UAC_FORMAT_TYPE_I_ALAW)) { 126 if (format & (1 << UAC_FORMAT_TYPE_I_ALAW)) {
127 pcm_formats |= SNDRV_PCM_FMTBIT_A_LAW; 127 pcm_formats |= SNDRV_PCM_FMTBIT_A_LAW;
128 } 128 }
129 if (format & (1 << UAC_FORMAT_TYPE_I_MULAW)) { 129 if (format & (1 << UAC_FORMAT_TYPE_I_MULAW)) {
130 pcm_formats |= SNDRV_PCM_FMTBIT_MU_LAW; 130 pcm_formats |= SNDRV_PCM_FMTBIT_MU_LAW;
131 } 131 }
132 if (format & ~0x3f) { 132 if (format & ~0x3f) {
133 snd_printk(KERN_INFO "%d:%u:%d : unsupported format bits %#x\n", 133 snd_printk(KERN_INFO "%d:%u:%d : unsupported format bits %#x\n",
134 chip->dev->devnum, fp->iface, fp->altsetting, format); 134 chip->dev->devnum, fp->iface, fp->altsetting, format);
135 } 135 }
136 return pcm_formats; 136 return pcm_formats;
137 } 137 }
138 138
139 139
140 /* 140 /*
141 * parse the format descriptor and stores the possible sample rates 141 * parse the format descriptor and stores the possible sample rates
142 * on the audioformat table (audio class v1). 142 * on the audioformat table (audio class v1).
143 * 143 *
144 * @dev: usb device 144 * @dev: usb device
145 * @fp: audioformat record 145 * @fp: audioformat record
146 * @fmt: the format descriptor 146 * @fmt: the format descriptor
147 * @offset: the start offset of descriptor pointing the rate type 147 * @offset: the start offset of descriptor pointing the rate type
148 * (7 for type I and II, 8 for type II) 148 * (7 for type I and II, 8 for type II)
149 */ 149 */
150 static int parse_audio_format_rates_v1(struct snd_usb_audio *chip, struct audioformat *fp, 150 static int parse_audio_format_rates_v1(struct snd_usb_audio *chip, struct audioformat *fp,
151 unsigned char *fmt, int offset) 151 unsigned char *fmt, int offset)
152 { 152 {
153 int nr_rates = fmt[offset]; 153 int nr_rates = fmt[offset];
154 154
155 if (fmt[0] < offset + 1 + 3 * (nr_rates ? nr_rates : 2)) { 155 if (fmt[0] < offset + 1 + 3 * (nr_rates ? nr_rates : 2)) {
156 snd_printk(KERN_ERR "%d:%u:%d : invalid UAC_FORMAT_TYPE desc\n", 156 snd_printk(KERN_ERR "%d:%u:%d : invalid UAC_FORMAT_TYPE desc\n",
157 chip->dev->devnum, fp->iface, fp->altsetting); 157 chip->dev->devnum, fp->iface, fp->altsetting);
158 return -1; 158 return -1;
159 } 159 }
160 160
161 if (nr_rates) { 161 if (nr_rates) {
162 /* 162 /*
163 * build the rate table and bitmap flags 163 * build the rate table and bitmap flags
164 */ 164 */
165 int r, idx; 165 int r, idx;
166 166
167 fp->rate_table = kmalloc(sizeof(int) * nr_rates, GFP_KERNEL); 167 fp->rate_table = kmalloc(sizeof(int) * nr_rates, GFP_KERNEL);
168 if (fp->rate_table == NULL) { 168 if (fp->rate_table == NULL) {
169 snd_printk(KERN_ERR "cannot malloc\n"); 169 snd_printk(KERN_ERR "cannot malloc\n");
170 return -1; 170 return -1;
171 } 171 }
172 172
173 fp->nr_rates = 0; 173 fp->nr_rates = 0;
174 fp->rate_min = fp->rate_max = 0; 174 fp->rate_min = fp->rate_max = 0;
175 for (r = 0, idx = offset + 1; r < nr_rates; r++, idx += 3) { 175 for (r = 0, idx = offset + 1; r < nr_rates; r++, idx += 3) {
176 unsigned int rate = combine_triple(&fmt[idx]); 176 unsigned int rate = combine_triple(&fmt[idx]);
177 if (!rate) 177 if (!rate)
178 continue; 178 continue;
179 /* C-Media CM6501 mislabels its 96 kHz altsetting */ 179 /* C-Media CM6501 mislabels its 96 kHz altsetting */
180 /* Terratec Aureon 7.1 USB C-Media 6206, too */ 180 /* Terratec Aureon 7.1 USB C-Media 6206, too */
181 if (rate == 48000 && nr_rates == 1 && 181 if (rate == 48000 && nr_rates == 1 &&
182 (chip->usb_id == USB_ID(0x0d8c, 0x0201) || 182 (chip->usb_id == USB_ID(0x0d8c, 0x0201) ||
183 chip->usb_id == USB_ID(0x0d8c, 0x0102) || 183 chip->usb_id == USB_ID(0x0d8c, 0x0102) ||
184 chip->usb_id == USB_ID(0x0ccd, 0x00b1)) && 184 chip->usb_id == USB_ID(0x0ccd, 0x00b1)) &&
185 fp->altsetting == 5 && fp->maxpacksize == 392) 185 fp->altsetting == 5 && fp->maxpacksize == 392)
186 rate = 96000; 186 rate = 96000;
187 /* Creative VF0470 Live Cam reports 16 kHz instead of 8kHz */ 187 /* Creative VF0470 Live Cam reports 16 kHz instead of 8kHz */
188 if (rate == 16000 && chip->usb_id == USB_ID(0x041e, 0x4068)) 188 if (rate == 16000 && chip->usb_id == USB_ID(0x041e, 0x4068))
189 rate = 8000; 189 rate = 8000;
190 190
191 fp->rate_table[fp->nr_rates] = rate; 191 fp->rate_table[fp->nr_rates] = rate;
192 if (!fp->rate_min || rate < fp->rate_min) 192 if (!fp->rate_min || rate < fp->rate_min)
193 fp->rate_min = rate; 193 fp->rate_min = rate;
194 if (!fp->rate_max || rate > fp->rate_max) 194 if (!fp->rate_max || rate > fp->rate_max)
195 fp->rate_max = rate; 195 fp->rate_max = rate;
196 fp->rates |= snd_pcm_rate_to_rate_bit(rate); 196 fp->rates |= snd_pcm_rate_to_rate_bit(rate);
197 fp->nr_rates++; 197 fp->nr_rates++;
198 } 198 }
199 if (!fp->nr_rates) { 199 if (!fp->nr_rates) {
200 hwc_debug("All rates were zero. Skipping format!\n"); 200 hwc_debug("All rates were zero. Skipping format!\n");
201 return -1; 201 return -1;
202 } 202 }
203 } else { 203 } else {
204 /* continuous rates */ 204 /* continuous rates */
205 fp->rates = SNDRV_PCM_RATE_CONTINUOUS; 205 fp->rates = SNDRV_PCM_RATE_CONTINUOUS;
206 fp->rate_min = combine_triple(&fmt[offset + 1]); 206 fp->rate_min = combine_triple(&fmt[offset + 1]);
207 fp->rate_max = combine_triple(&fmt[offset + 4]); 207 fp->rate_max = combine_triple(&fmt[offset + 4]);
208 } 208 }
209 return 0; 209 return 0;
210 } 210 }
211 211
212 #define MAX_UAC2_NR_RATES 1024
213
214 /* 212 /*
215 * Helper function to walk the array of sample rate triplets reported by 213 * Helper function to walk the array of sample rate triplets reported by
216 * the device. The problem is that we need to parse whole array first to 214 * the device. The problem is that we need to parse whole array first to
217 * get to know how many sample rates we have to expect. 215 * get to know how many sample rates we have to expect.
218 * Then fp->rate_table can be allocated and filled. 216 * Then fp->rate_table can be allocated and filled.
219 */ 217 */
220 static int parse_uac2_sample_rate_range(struct audioformat *fp, int nr_triplets, 218 static int parse_uac2_sample_rate_range(struct audioformat *fp, int nr_triplets,
221 const unsigned char *data) 219 const unsigned char *data)
222 { 220 {
223 int i, nr_rates = 0; 221 int i, nr_rates = 0;
224 222
225 fp->rates = fp->rate_min = fp->rate_max = 0; 223 fp->rates = fp->rate_min = fp->rate_max = 0;
226 224
227 for (i = 0; i < nr_triplets; i++) { 225 for (i = 0; i < nr_triplets; i++) {
228 int min = combine_quad(&data[2 + 12 * i]); 226 int min = combine_quad(&data[2 + 12 * i]);
229 int max = combine_quad(&data[6 + 12 * i]); 227 int max = combine_quad(&data[6 + 12 * i]);
230 int res = combine_quad(&data[10 + 12 * i]); 228 int res = combine_quad(&data[10 + 12 * i]);
231 unsigned int rate; 229 unsigned int rate;
232 230
233 if ((max < 0) || (min < 0) || (res < 0) || (max < min)) 231 if ((max < 0) || (min < 0) || (res < 0) || (max < min))
234 continue; 232 continue;
235 233
236 /* 234 /*
237 * for ranges with res == 1, we announce a continuous sample 235 * for ranges with res == 1, we announce a continuous sample
238 * rate range, and this function should return 0 for no further 236 * rate range, and this function should return 0 for no further
239 * parsing. 237 * parsing.
240 */ 238 */
241 if (res == 1) { 239 if (res == 1) {
242 fp->rate_min = min; 240 fp->rate_min = min;
243 fp->rate_max = max; 241 fp->rate_max = max;
244 fp->rates = SNDRV_PCM_RATE_CONTINUOUS; 242 fp->rates = SNDRV_PCM_RATE_CONTINUOUS;
245 return 0; 243 return 0;
246 } 244 }
247 245
248 for (rate = min; rate <= max; rate += res) { 246 for (rate = min; rate <= max; rate += res) {
249 if (fp->rate_table) 247 if (fp->rate_table)
250 fp->rate_table[nr_rates] = rate; 248 fp->rate_table[nr_rates] = rate;
251 if (!fp->rate_min || rate < fp->rate_min) 249 if (!fp->rate_min || rate < fp->rate_min)
252 fp->rate_min = rate; 250 fp->rate_min = rate;
253 if (!fp->rate_max || rate > fp->rate_max) 251 if (!fp->rate_max || rate > fp->rate_max)
254 fp->rate_max = rate; 252 fp->rate_max = rate;
255 fp->rates |= snd_pcm_rate_to_rate_bit(rate); 253 fp->rates |= snd_pcm_rate_to_rate_bit(rate);
256 254
257 nr_rates++; 255 nr_rates++;
258 if (nr_rates >= MAX_UAC2_NR_RATES) { 256 if (nr_rates >= MAX_NR_RATES) {
259 snd_printk(KERN_ERR "invalid uac2 rates\n"); 257 snd_printk(KERN_ERR "invalid uac2 rates\n");
260 break; 258 break;
261 } 259 }
262 260
263 /* avoid endless loop */ 261 /* avoid endless loop */
264 if (res == 0) 262 if (res == 0)
265 break; 263 break;
266 } 264 }
267 } 265 }
268 266
269 return nr_rates; 267 return nr_rates;
270 } 268 }
271 269
272 /* 270 /*
273 * parse the format descriptor and stores the possible sample rates 271 * parse the format descriptor and stores the possible sample rates
274 * on the audioformat table (audio class v2). 272 * on the audioformat table (audio class v2).
275 */ 273 */
276 static int parse_audio_format_rates_v2(struct snd_usb_audio *chip, 274 static int parse_audio_format_rates_v2(struct snd_usb_audio *chip,
277 struct audioformat *fp) 275 struct audioformat *fp)
278 { 276 {
279 struct usb_device *dev = chip->dev; 277 struct usb_device *dev = chip->dev;
280 unsigned char tmp[2], *data; 278 unsigned char tmp[2], *data;
281 int nr_triplets, data_size, ret = 0; 279 int nr_triplets, data_size, ret = 0;
282 int clock = snd_usb_clock_find_source(chip, fp->clock); 280 int clock = snd_usb_clock_find_source(chip, fp->clock);
283 281
284 if (clock < 0) { 282 if (clock < 0) {
285 snd_printk(KERN_ERR "%s(): unable to find clock source (clock %d)\n", 283 snd_printk(KERN_ERR "%s(): unable to find clock source (clock %d)\n",
286 __func__, clock); 284 __func__, clock);
287 goto err; 285 goto err;
288 } 286 }
289 287
290 /* get the number of sample rates first by only fetching 2 bytes */ 288 /* get the number of sample rates first by only fetching 2 bytes */
291 ret = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_RANGE, 289 ret = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_RANGE,
292 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN, 290 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
293 UAC2_CS_CONTROL_SAM_FREQ << 8, 291 UAC2_CS_CONTROL_SAM_FREQ << 8,
294 snd_usb_ctrl_intf(chip) | (clock << 8), 292 snd_usb_ctrl_intf(chip) | (clock << 8),
295 tmp, sizeof(tmp)); 293 tmp, sizeof(tmp));
296 294
297 if (ret < 0) { 295 if (ret < 0) {
298 snd_printk(KERN_ERR "%s(): unable to retrieve number of sample rates (clock %d)\n", 296 snd_printk(KERN_ERR "%s(): unable to retrieve number of sample rates (clock %d)\n",
299 __func__, clock); 297 __func__, clock);
300 goto err; 298 goto err;
301 } 299 }
302 300
303 nr_triplets = (tmp[1] << 8) | tmp[0]; 301 nr_triplets = (tmp[1] << 8) | tmp[0];
304 data_size = 2 + 12 * nr_triplets; 302 data_size = 2 + 12 * nr_triplets;
305 data = kzalloc(data_size, GFP_KERNEL); 303 data = kzalloc(data_size, GFP_KERNEL);
306 if (!data) { 304 if (!data) {
307 ret = -ENOMEM; 305 ret = -ENOMEM;
308 goto err; 306 goto err;
309 } 307 }
310 308
311 /* now get the full information */ 309 /* now get the full information */
312 ret = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_RANGE, 310 ret = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_RANGE,
313 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN, 311 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
314 UAC2_CS_CONTROL_SAM_FREQ << 8, 312 UAC2_CS_CONTROL_SAM_FREQ << 8,
315 snd_usb_ctrl_intf(chip) | (clock << 8), 313 snd_usb_ctrl_intf(chip) | (clock << 8),
316 data, data_size); 314 data, data_size);
317 315
318 if (ret < 0) { 316 if (ret < 0) {
319 snd_printk(KERN_ERR "%s(): unable to retrieve sample rate range (clock %d)\n", 317 snd_printk(KERN_ERR "%s(): unable to retrieve sample rate range (clock %d)\n",
320 __func__, clock); 318 __func__, clock);
321 ret = -EINVAL; 319 ret = -EINVAL;
322 goto err_free; 320 goto err_free;
323 } 321 }
324 322
325 /* Call the triplet parser, and make sure fp->rate_table is NULL. 323 /* Call the triplet parser, and make sure fp->rate_table is NULL.
326 * We just use the return value to know how many sample rates we 324 * We just use the return value to know how many sample rates we
327 * will have to deal with. */ 325 * will have to deal with. */
328 kfree(fp->rate_table); 326 kfree(fp->rate_table);
329 fp->rate_table = NULL; 327 fp->rate_table = NULL;
330 fp->nr_rates = parse_uac2_sample_rate_range(fp, nr_triplets, data); 328 fp->nr_rates = parse_uac2_sample_rate_range(fp, nr_triplets, data);
331 329
332 if (fp->nr_rates == 0) { 330 if (fp->nr_rates == 0) {
333 /* SNDRV_PCM_RATE_CONTINUOUS */ 331 /* SNDRV_PCM_RATE_CONTINUOUS */
334 ret = 0; 332 ret = 0;
335 goto err_free; 333 goto err_free;
336 } 334 }
337 335
338 fp->rate_table = kmalloc(sizeof(int) * fp->nr_rates, GFP_KERNEL); 336 fp->rate_table = kmalloc(sizeof(int) * fp->nr_rates, GFP_KERNEL);
339 if (!fp->rate_table) { 337 if (!fp->rate_table) {
340 ret = -ENOMEM; 338 ret = -ENOMEM;
341 goto err_free; 339 goto err_free;
342 } 340 }
343 341
344 /* Call the triplet parser again, but this time, fp->rate_table is 342 /* Call the triplet parser again, but this time, fp->rate_table is
345 * allocated, so the rates will be stored */ 343 * allocated, so the rates will be stored */
346 parse_uac2_sample_rate_range(fp, nr_triplets, data); 344 parse_uac2_sample_rate_range(fp, nr_triplets, data);
347 345
348 err_free: 346 err_free:
349 kfree(data); 347 kfree(data);
350 err: 348 err:
351 return ret; 349 return ret;
352 } 350 }
353 351
354 /* 352 /*
355 * parse the format type I and III descriptors 353 * parse the format type I and III descriptors
356 */ 354 */
357 static int parse_audio_format_i(struct snd_usb_audio *chip, 355 static int parse_audio_format_i(struct snd_usb_audio *chip,
358 struct audioformat *fp, int format, 356 struct audioformat *fp, int format,
359 struct uac_format_type_i_continuous_descriptor *fmt, 357 struct uac_format_type_i_continuous_descriptor *fmt,
360 struct usb_host_interface *iface) 358 struct usb_host_interface *iface)
361 { 359 {
362 struct usb_interface_descriptor *altsd = get_iface_desc(iface); 360 struct usb_interface_descriptor *altsd = get_iface_desc(iface);
363 int protocol = altsd->bInterfaceProtocol; 361 int protocol = altsd->bInterfaceProtocol;
364 int pcm_format, ret; 362 int pcm_format, ret;
365 363
366 if (fmt->bFormatType == UAC_FORMAT_TYPE_III) { 364 if (fmt->bFormatType == UAC_FORMAT_TYPE_III) {
367 /* FIXME: the format type is really IECxxx 365 /* FIXME: the format type is really IECxxx
368 * but we give normal PCM format to get the existing 366 * but we give normal PCM format to get the existing
369 * apps working... 367 * apps working...
370 */ 368 */
371 switch (chip->usb_id) { 369 switch (chip->usb_id) {
372 370
373 case USB_ID(0x0763, 0x2003): /* M-Audio Audiophile USB */ 371 case USB_ID(0x0763, 0x2003): /* M-Audio Audiophile USB */
374 if (chip->setup == 0x00 && 372 if (chip->setup == 0x00 &&
375 fp->altsetting == 6) 373 fp->altsetting == 6)
376 pcm_format = SNDRV_PCM_FORMAT_S16_BE; 374 pcm_format = SNDRV_PCM_FORMAT_S16_BE;
377 else 375 else
378 pcm_format = SNDRV_PCM_FORMAT_S16_LE; 376 pcm_format = SNDRV_PCM_FORMAT_S16_LE;
379 break; 377 break;
380 default: 378 default:
381 pcm_format = SNDRV_PCM_FORMAT_S16_LE; 379 pcm_format = SNDRV_PCM_FORMAT_S16_LE;
382 } 380 }
383 fp->formats = 1uLL << pcm_format; 381 fp->formats = 1uLL << pcm_format;
384 } else { 382 } else {
385 fp->formats = parse_audio_format_i_type(chip, fp, format, 383 fp->formats = parse_audio_format_i_type(chip, fp, format,
386 fmt, protocol); 384 fmt, protocol);
387 if (!fp->formats) 385 if (!fp->formats)
388 return -1; 386 return -1;
389 } 387 }
390 388
391 /* gather possible sample rates */ 389 /* gather possible sample rates */
392 /* audio class v1 reports possible sample rates as part of the 390 /* audio class v1 reports possible sample rates as part of the
393 * proprietary class specific descriptor. 391 * proprietary class specific descriptor.
394 * audio class v2 uses class specific EP0 range requests for that. 392 * audio class v2 uses class specific EP0 range requests for that.
395 */ 393 */
396 switch (protocol) { 394 switch (protocol) {
397 default: 395 default:
398 snd_printdd(KERN_WARNING "%d:%u:%d : invalid protocol version %d, assuming v1\n", 396 snd_printdd(KERN_WARNING "%d:%u:%d : invalid protocol version %d, assuming v1\n",
399 chip->dev->devnum, fp->iface, fp->altsetting, protocol); 397 chip->dev->devnum, fp->iface, fp->altsetting, protocol);
400 /* fall through */ 398 /* fall through */
401 case UAC_VERSION_1: 399 case UAC_VERSION_1:
402 fp->channels = fmt->bNrChannels; 400 fp->channels = fmt->bNrChannels;
403 ret = parse_audio_format_rates_v1(chip, fp, (unsigned char *) fmt, 7); 401 ret = parse_audio_format_rates_v1(chip, fp, (unsigned char *) fmt, 7);
404 break; 402 break;
405 case UAC_VERSION_2: 403 case UAC_VERSION_2:
406 /* fp->channels is already set in this case */ 404 /* fp->channels is already set in this case */
407 ret = parse_audio_format_rates_v2(chip, fp); 405 ret = parse_audio_format_rates_v2(chip, fp);
408 break; 406 break;
409 } 407 }
410 408
411 if (fp->channels < 1) { 409 if (fp->channels < 1) {
412 snd_printk(KERN_ERR "%d:%u:%d : invalid channels %d\n", 410 snd_printk(KERN_ERR "%d:%u:%d : invalid channels %d\n",
413 chip->dev->devnum, fp->iface, fp->altsetting, fp->channels); 411 chip->dev->devnum, fp->iface, fp->altsetting, fp->channels);
414 return -1; 412 return -1;
415 } 413 }
416 414
417 return ret; 415 return ret;
418 } 416 }
419 417
420 /* 418 /*
421 * parse the format type II descriptor 419 * parse the format type II descriptor
422 */ 420 */
423 static int parse_audio_format_ii(struct snd_usb_audio *chip, 421 static int parse_audio_format_ii(struct snd_usb_audio *chip,
424 struct audioformat *fp, 422 struct audioformat *fp,
425 int format, void *_fmt, 423 int format, void *_fmt,
426 struct usb_host_interface *iface) 424 struct usb_host_interface *iface)
427 { 425 {
428 int brate, framesize, ret; 426 int brate, framesize, ret;
429 struct usb_interface_descriptor *altsd = get_iface_desc(iface); 427 struct usb_interface_descriptor *altsd = get_iface_desc(iface);
430 int protocol = altsd->bInterfaceProtocol; 428 int protocol = altsd->bInterfaceProtocol;
431 429
432 switch (format) { 430 switch (format) {
433 case UAC_FORMAT_TYPE_II_AC3: 431 case UAC_FORMAT_TYPE_II_AC3:
434 /* FIXME: there is no AC3 format defined yet */ 432 /* FIXME: there is no AC3 format defined yet */
435 // fp->formats = SNDRV_PCM_FMTBIT_AC3; 433 // fp->formats = SNDRV_PCM_FMTBIT_AC3;
436 fp->formats = SNDRV_PCM_FMTBIT_U8; /* temporary hack to receive byte streams */ 434 fp->formats = SNDRV_PCM_FMTBIT_U8; /* temporary hack to receive byte streams */
437 break; 435 break;
438 case UAC_FORMAT_TYPE_II_MPEG: 436 case UAC_FORMAT_TYPE_II_MPEG:
439 fp->formats = SNDRV_PCM_FMTBIT_MPEG; 437 fp->formats = SNDRV_PCM_FMTBIT_MPEG;
440 break; 438 break;
441 default: 439 default:
442 snd_printd(KERN_INFO "%d:%u:%d : unknown format tag %#x is detected. processed as MPEG.\n", 440 snd_printd(KERN_INFO "%d:%u:%d : unknown format tag %#x is detected. processed as MPEG.\n",
443 chip->dev->devnum, fp->iface, fp->altsetting, format); 441 chip->dev->devnum, fp->iface, fp->altsetting, format);
444 fp->formats = SNDRV_PCM_FMTBIT_MPEG; 442 fp->formats = SNDRV_PCM_FMTBIT_MPEG;
445 break; 443 break;
446 } 444 }
447 445
448 fp->channels = 1; 446 fp->channels = 1;
449 447
450 switch (protocol) { 448 switch (protocol) {
451 default: 449 default:
452 snd_printdd(KERN_WARNING "%d:%u:%d : invalid protocol version %d, assuming v1\n", 450 snd_printdd(KERN_WARNING "%d:%u:%d : invalid protocol version %d, assuming v1\n",
453 chip->dev->devnum, fp->iface, fp->altsetting, protocol); 451 chip->dev->devnum, fp->iface, fp->altsetting, protocol);
454 /* fall through */ 452 /* fall through */
455 case UAC_VERSION_1: { 453 case UAC_VERSION_1: {
456 struct uac_format_type_ii_discrete_descriptor *fmt = _fmt; 454 struct uac_format_type_ii_discrete_descriptor *fmt = _fmt;
457 brate = le16_to_cpu(fmt->wMaxBitRate); 455 brate = le16_to_cpu(fmt->wMaxBitRate);
458 framesize = le16_to_cpu(fmt->wSamplesPerFrame); 456 framesize = le16_to_cpu(fmt->wSamplesPerFrame);
459 snd_printd(KERN_INFO "found format II with max.bitrate = %d, frame size=%d\n", brate, framesize); 457 snd_printd(KERN_INFO "found format II with max.bitrate = %d, frame size=%d\n", brate, framesize);
460 fp->frame_size = framesize; 458 fp->frame_size = framesize;
461 ret = parse_audio_format_rates_v1(chip, fp, _fmt, 8); /* fmt[8..] sample rates */ 459 ret = parse_audio_format_rates_v1(chip, fp, _fmt, 8); /* fmt[8..] sample rates */
462 break; 460 break;
463 } 461 }
464 case UAC_VERSION_2: { 462 case UAC_VERSION_2: {
465 struct uac_format_type_ii_ext_descriptor *fmt = _fmt; 463 struct uac_format_type_ii_ext_descriptor *fmt = _fmt;
466 brate = le16_to_cpu(fmt->wMaxBitRate); 464 brate = le16_to_cpu(fmt->wMaxBitRate);
467 framesize = le16_to_cpu(fmt->wSamplesPerFrame); 465 framesize = le16_to_cpu(fmt->wSamplesPerFrame);
468 snd_printd(KERN_INFO "found format II with max.bitrate = %d, frame size=%d\n", brate, framesize); 466 snd_printd(KERN_INFO "found format II with max.bitrate = %d, frame size=%d\n", brate, framesize);
469 fp->frame_size = framesize; 467 fp->frame_size = framesize;
470 ret = parse_audio_format_rates_v2(chip, fp); 468 ret = parse_audio_format_rates_v2(chip, fp);
471 break; 469 break;
472 } 470 }
473 } 471 }
474 472
475 return ret; 473 return ret;
476 } 474 }
477 475
478 int snd_usb_parse_audio_format(struct snd_usb_audio *chip, struct audioformat *fp, 476 int snd_usb_parse_audio_format(struct snd_usb_audio *chip, struct audioformat *fp,
479 int format, struct uac_format_type_i_continuous_descriptor *fmt, 477 int format, struct uac_format_type_i_continuous_descriptor *fmt,
480 int stream, struct usb_host_interface *iface) 478 int stream, struct usb_host_interface *iface)
481 { 479 {
482 int err; 480 int err;
483 481
484 switch (fmt->bFormatType) { 482 switch (fmt->bFormatType) {
485 case UAC_FORMAT_TYPE_I: 483 case UAC_FORMAT_TYPE_I:
486 case UAC_FORMAT_TYPE_III: 484 case UAC_FORMAT_TYPE_III:
487 err = parse_audio_format_i(chip, fp, format, fmt, iface); 485 err = parse_audio_format_i(chip, fp, format, fmt, iface);
488 break; 486 break;
489 case UAC_FORMAT_TYPE_II: 487 case UAC_FORMAT_TYPE_II:
490 err = parse_audio_format_ii(chip, fp, format, fmt, iface); 488 err = parse_audio_format_ii(chip, fp, format, fmt, iface);
491 break; 489 break;
492 default: 490 default:
493 snd_printd(KERN_INFO "%d:%u:%d : format type %d is not supported yet\n", 491 snd_printd(KERN_INFO "%d:%u:%d : format type %d is not supported yet\n",
494 chip->dev->devnum, fp->iface, fp->altsetting, 492 chip->dev->devnum, fp->iface, fp->altsetting,
495 fmt->bFormatType); 493 fmt->bFormatType);
496 return -ENOTSUPP; 494 return -ENOTSUPP;
497 } 495 }
498 fp->fmt_type = fmt->bFormatType; 496 fp->fmt_type = fmt->bFormatType;
499 if (err < 0) 497 if (err < 0)
500 return err; 498 return err;
501 #if 1 499 #if 1
502 /* FIXME: temporary hack for extigy/audigy 2 nx/zs */ 500 /* FIXME: temporary hack for extigy/audigy 2 nx/zs */
503 /* extigy apparently supports sample rates other than 48k 501 /* extigy apparently supports sample rates other than 48k
504 * but not in ordinary way. so we enable only 48k atm. 502 * but not in ordinary way. so we enable only 48k atm.
505 */ 503 */
506 if (chip->usb_id == USB_ID(0x041e, 0x3000) || 504 if (chip->usb_id == USB_ID(0x041e, 0x3000) ||
507 chip->usb_id == USB_ID(0x041e, 0x3020) || 505 chip->usb_id == USB_ID(0x041e, 0x3020) ||
508 chip->usb_id == USB_ID(0x041e, 0x3061)) { 506 chip->usb_id == USB_ID(0x041e, 0x3061)) {
509 if (fmt->bFormatType == UAC_FORMAT_TYPE_I && 507 if (fmt->bFormatType == UAC_FORMAT_TYPE_I &&
510 fp->rates != SNDRV_PCM_RATE_48000 && 508 fp->rates != SNDRV_PCM_RATE_48000 &&
511 fp->rates != SNDRV_PCM_RATE_96000) 509 fp->rates != SNDRV_PCM_RATE_96000)
512 return -ENOTSUPP; 510 return -ENOTSUPP;
513 } 511 }
514 #endif 512 #endif
515 return 0; 513 return 0;
516 } 514 }
517 515
518 516
1 /* 1 /*
2 * This program is free software; you can redistribute it and/or modify 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 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 4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version. 5 * (at your option) any later version.
6 * 6 *
7 * This program is distributed in the hope that it will be useful, 7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of 8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details. 10 * GNU General Public License for more details.
11 * 11 *
12 * You should have received a copy of the GNU General Public License 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 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 14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15 */ 15 */
16 16
17 #include <linux/init.h> 17 #include <linux/init.h>
18 #include <linux/slab.h> 18 #include <linux/slab.h>
19 #include <linux/usb.h> 19 #include <linux/usb.h>
20 #include <linux/usb/audio.h> 20 #include <linux/usb/audio.h>
21 21
22 #include <sound/control.h> 22 #include <sound/control.h>
23 #include <sound/core.h> 23 #include <sound/core.h>
24 #include <sound/info.h> 24 #include <sound/info.h>
25 #include <sound/pcm.h> 25 #include <sound/pcm.h>
26 26
27 #include "usbaudio.h" 27 #include "usbaudio.h"
28 #include "card.h" 28 #include "card.h"
29 #include "mixer.h" 29 #include "mixer.h"
30 #include "mixer_quirks.h" 30 #include "mixer_quirks.h"
31 #include "midi.h" 31 #include "midi.h"
32 #include "quirks.h" 32 #include "quirks.h"
33 #include "helper.h" 33 #include "helper.h"
34 #include "endpoint.h" 34 #include "endpoint.h"
35 #include "pcm.h" 35 #include "pcm.h"
36 #include "clock.h" 36 #include "clock.h"
37 #include "stream.h" 37 #include "stream.h"
38 38
39 /* 39 /*
40 * handle the quirks for the contained interfaces 40 * handle the quirks for the contained interfaces
41 */ 41 */
42 static int create_composite_quirk(struct snd_usb_audio *chip, 42 static int create_composite_quirk(struct snd_usb_audio *chip,
43 struct usb_interface *iface, 43 struct usb_interface *iface,
44 struct usb_driver *driver, 44 struct usb_driver *driver,
45 const struct snd_usb_audio_quirk *quirk) 45 const struct snd_usb_audio_quirk *quirk)
46 { 46 {
47 int probed_ifnum = get_iface_desc(iface->altsetting)->bInterfaceNumber; 47 int probed_ifnum = get_iface_desc(iface->altsetting)->bInterfaceNumber;
48 int err; 48 int err;
49 49
50 for (quirk = quirk->data; quirk->ifnum >= 0; ++quirk) { 50 for (quirk = quirk->data; quirk->ifnum >= 0; ++quirk) {
51 iface = usb_ifnum_to_if(chip->dev, quirk->ifnum); 51 iface = usb_ifnum_to_if(chip->dev, quirk->ifnum);
52 if (!iface) 52 if (!iface)
53 continue; 53 continue;
54 if (quirk->ifnum != probed_ifnum && 54 if (quirk->ifnum != probed_ifnum &&
55 usb_interface_claimed(iface)) 55 usb_interface_claimed(iface))
56 continue; 56 continue;
57 err = snd_usb_create_quirk(chip, iface, driver, quirk); 57 err = snd_usb_create_quirk(chip, iface, driver, quirk);
58 if (err < 0) 58 if (err < 0)
59 return err; 59 return err;
60 if (quirk->ifnum != probed_ifnum) 60 if (quirk->ifnum != probed_ifnum)
61 usb_driver_claim_interface(driver, iface, (void *)-1L); 61 usb_driver_claim_interface(driver, iface, (void *)-1L);
62 } 62 }
63 return 0; 63 return 0;
64 } 64 }
65 65
66 static int ignore_interface_quirk(struct snd_usb_audio *chip, 66 static int ignore_interface_quirk(struct snd_usb_audio *chip,
67 struct usb_interface *iface, 67 struct usb_interface *iface,
68 struct usb_driver *driver, 68 struct usb_driver *driver,
69 const struct snd_usb_audio_quirk *quirk) 69 const struct snd_usb_audio_quirk *quirk)
70 { 70 {
71 return 0; 71 return 0;
72 } 72 }
73 73
74 74
75 /* 75 /*
76 * Allow alignment on audio sub-slot (channel samples) rather than 76 * Allow alignment on audio sub-slot (channel samples) rather than
77 * on audio slots (audio frames) 77 * on audio slots (audio frames)
78 */ 78 */
79 static int create_align_transfer_quirk(struct snd_usb_audio *chip, 79 static int create_align_transfer_quirk(struct snd_usb_audio *chip,
80 struct usb_interface *iface, 80 struct usb_interface *iface,
81 struct usb_driver *driver, 81 struct usb_driver *driver,
82 const struct snd_usb_audio_quirk *quirk) 82 const struct snd_usb_audio_quirk *quirk)
83 { 83 {
84 chip->txfr_quirk = 1; 84 chip->txfr_quirk = 1;
85 return 1; /* Continue with creating streams and mixer */ 85 return 1; /* Continue with creating streams and mixer */
86 } 86 }
87 87
88 static int create_any_midi_quirk(struct snd_usb_audio *chip, 88 static int create_any_midi_quirk(struct snd_usb_audio *chip,
89 struct usb_interface *intf, 89 struct usb_interface *intf,
90 struct usb_driver *driver, 90 struct usb_driver *driver,
91 const struct snd_usb_audio_quirk *quirk) 91 const struct snd_usb_audio_quirk *quirk)
92 { 92 {
93 return snd_usbmidi_create(chip->card, intf, &chip->midi_list, quirk); 93 return snd_usbmidi_create(chip->card, intf, &chip->midi_list, quirk);
94 } 94 }
95 95
96 /* 96 /*
97 * create a stream for an interface with proper descriptors 97 * create a stream for an interface with proper descriptors
98 */ 98 */
99 static int create_standard_audio_quirk(struct snd_usb_audio *chip, 99 static int create_standard_audio_quirk(struct snd_usb_audio *chip,
100 struct usb_interface *iface, 100 struct usb_interface *iface,
101 struct usb_driver *driver, 101 struct usb_driver *driver,
102 const struct snd_usb_audio_quirk *quirk) 102 const struct snd_usb_audio_quirk *quirk)
103 { 103 {
104 struct usb_host_interface *alts; 104 struct usb_host_interface *alts;
105 struct usb_interface_descriptor *altsd; 105 struct usb_interface_descriptor *altsd;
106 int err; 106 int err;
107 107
108 alts = &iface->altsetting[0]; 108 alts = &iface->altsetting[0];
109 altsd = get_iface_desc(alts); 109 altsd = get_iface_desc(alts);
110 err = snd_usb_parse_audio_interface(chip, altsd->bInterfaceNumber); 110 err = snd_usb_parse_audio_interface(chip, altsd->bInterfaceNumber);
111 if (err < 0) { 111 if (err < 0) {
112 snd_printk(KERN_ERR "cannot setup if %d: error %d\n", 112 snd_printk(KERN_ERR "cannot setup if %d: error %d\n",
113 altsd->bInterfaceNumber, err); 113 altsd->bInterfaceNumber, err);
114 return err; 114 return err;
115 } 115 }
116 /* reset the current interface */ 116 /* reset the current interface */
117 usb_set_interface(chip->dev, altsd->bInterfaceNumber, 0); 117 usb_set_interface(chip->dev, altsd->bInterfaceNumber, 0);
118 return 0; 118 return 0;
119 } 119 }
120 120
121 /* 121 /*
122 * create a stream for an endpoint/altsetting without proper descriptors 122 * create a stream for an endpoint/altsetting without proper descriptors
123 */ 123 */
124 static int create_fixed_stream_quirk(struct snd_usb_audio *chip, 124 static int create_fixed_stream_quirk(struct snd_usb_audio *chip,
125 struct usb_interface *iface, 125 struct usb_interface *iface,
126 struct usb_driver *driver, 126 struct usb_driver *driver,
127 const struct snd_usb_audio_quirk *quirk) 127 const struct snd_usb_audio_quirk *quirk)
128 { 128 {
129 struct audioformat *fp; 129 struct audioformat *fp;
130 struct usb_host_interface *alts; 130 struct usb_host_interface *alts;
131 int stream, err; 131 int stream, err;
132 unsigned *rate_table = NULL; 132 unsigned *rate_table = NULL;
133 133
134 fp = kmemdup(quirk->data, sizeof(*fp), GFP_KERNEL); 134 fp = kmemdup(quirk->data, sizeof(*fp), GFP_KERNEL);
135 if (! fp) { 135 if (!fp) {
136 snd_printk(KERN_ERR "cannot memdup\n"); 136 snd_printk(KERN_ERR "cannot memdup\n");
137 return -ENOMEM; 137 return -ENOMEM;
138 }
139 if (fp->nr_rates > MAX_NR_RATES) {
140 kfree(fp);
141 return -EINVAL;
138 } 142 }
139 if (fp->nr_rates > 0) { 143 if (fp->nr_rates > 0) {
140 rate_table = kmemdup(fp->rate_table, 144 rate_table = kmemdup(fp->rate_table,
141 sizeof(int) * fp->nr_rates, GFP_KERNEL); 145 sizeof(int) * fp->nr_rates, GFP_KERNEL);
142 if (!rate_table) { 146 if (!rate_table) {
143 kfree(fp); 147 kfree(fp);
144 return -ENOMEM; 148 return -ENOMEM;
145 } 149 }
146 fp->rate_table = rate_table; 150 fp->rate_table = rate_table;
147 } 151 }
148 152
149 stream = (fp->endpoint & USB_DIR_IN) 153 stream = (fp->endpoint & USB_DIR_IN)
150 ? SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK; 154 ? SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
151 err = snd_usb_add_audio_stream(chip, stream, fp); 155 err = snd_usb_add_audio_stream(chip, stream, fp);
152 if (err < 0) { 156 if (err < 0) {
153 kfree(fp); 157 kfree(fp);
154 kfree(rate_table); 158 kfree(rate_table);
155 return err; 159 return err;
156 } 160 }
157 if (fp->iface != get_iface_desc(&iface->altsetting[0])->bInterfaceNumber || 161 if (fp->iface != get_iface_desc(&iface->altsetting[0])->bInterfaceNumber ||
158 fp->altset_idx >= iface->num_altsetting) { 162 fp->altset_idx >= iface->num_altsetting) {
159 kfree(fp); 163 kfree(fp);
160 kfree(rate_table); 164 kfree(rate_table);
161 return -EINVAL; 165 return -EINVAL;
162 } 166 }
163 alts = &iface->altsetting[fp->altset_idx]; 167 alts = &iface->altsetting[fp->altset_idx];
164 fp->datainterval = snd_usb_parse_datainterval(chip, alts); 168 fp->datainterval = snd_usb_parse_datainterval(chip, alts);
165 fp->maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize); 169 fp->maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize);
166 usb_set_interface(chip->dev, fp->iface, 0); 170 usb_set_interface(chip->dev, fp->iface, 0);
167 snd_usb_init_pitch(chip, fp->iface, alts, fp); 171 snd_usb_init_pitch(chip, fp->iface, alts, fp);
168 snd_usb_init_sample_rate(chip, fp->iface, alts, fp, fp->rate_max); 172 snd_usb_init_sample_rate(chip, fp->iface, alts, fp, fp->rate_max);
169 return 0; 173 return 0;
170 } 174 }
171 175
172 /* 176 /*
173 * Create a stream for an Edirol UA-700/UA-25/UA-4FX interface. 177 * Create a stream for an Edirol UA-700/UA-25/UA-4FX interface.
174 * The only way to detect the sample rate is by looking at wMaxPacketSize. 178 * The only way to detect the sample rate is by looking at wMaxPacketSize.
175 */ 179 */
176 static int create_uaxx_quirk(struct snd_usb_audio *chip, 180 static int create_uaxx_quirk(struct snd_usb_audio *chip,
177 struct usb_interface *iface, 181 struct usb_interface *iface,
178 struct usb_driver *driver, 182 struct usb_driver *driver,
179 const struct snd_usb_audio_quirk *quirk) 183 const struct snd_usb_audio_quirk *quirk)
180 { 184 {
181 static const struct audioformat ua_format = { 185 static const struct audioformat ua_format = {
182 .formats = SNDRV_PCM_FMTBIT_S24_3LE, 186 .formats = SNDRV_PCM_FMTBIT_S24_3LE,
183 .channels = 2, 187 .channels = 2,
184 .fmt_type = UAC_FORMAT_TYPE_I, 188 .fmt_type = UAC_FORMAT_TYPE_I,
185 .altsetting = 1, 189 .altsetting = 1,
186 .altset_idx = 1, 190 .altset_idx = 1,
187 .rates = SNDRV_PCM_RATE_CONTINUOUS, 191 .rates = SNDRV_PCM_RATE_CONTINUOUS,
188 }; 192 };
189 struct usb_host_interface *alts; 193 struct usb_host_interface *alts;
190 struct usb_interface_descriptor *altsd; 194 struct usb_interface_descriptor *altsd;
191 struct audioformat *fp; 195 struct audioformat *fp;
192 int stream, err; 196 int stream, err;
193 197
194 /* both PCM and MIDI interfaces have 2 or more altsettings */ 198 /* both PCM and MIDI interfaces have 2 or more altsettings */
195 if (iface->num_altsetting < 2) 199 if (iface->num_altsetting < 2)
196 return -ENXIO; 200 return -ENXIO;
197 alts = &iface->altsetting[1]; 201 alts = &iface->altsetting[1];
198 altsd = get_iface_desc(alts); 202 altsd = get_iface_desc(alts);
199 203
200 if (altsd->bNumEndpoints == 2) { 204 if (altsd->bNumEndpoints == 2) {
201 static const struct snd_usb_midi_endpoint_info ua700_ep = { 205 static const struct snd_usb_midi_endpoint_info ua700_ep = {
202 .out_cables = 0x0003, 206 .out_cables = 0x0003,
203 .in_cables = 0x0003 207 .in_cables = 0x0003
204 }; 208 };
205 static const struct snd_usb_audio_quirk ua700_quirk = { 209 static const struct snd_usb_audio_quirk ua700_quirk = {
206 .type = QUIRK_MIDI_FIXED_ENDPOINT, 210 .type = QUIRK_MIDI_FIXED_ENDPOINT,
207 .data = &ua700_ep 211 .data = &ua700_ep
208 }; 212 };
209 static const struct snd_usb_midi_endpoint_info uaxx_ep = { 213 static const struct snd_usb_midi_endpoint_info uaxx_ep = {
210 .out_cables = 0x0001, 214 .out_cables = 0x0001,
211 .in_cables = 0x0001 215 .in_cables = 0x0001
212 }; 216 };
213 static const struct snd_usb_audio_quirk uaxx_quirk = { 217 static const struct snd_usb_audio_quirk uaxx_quirk = {
214 .type = QUIRK_MIDI_FIXED_ENDPOINT, 218 .type = QUIRK_MIDI_FIXED_ENDPOINT,
215 .data = &uaxx_ep 219 .data = &uaxx_ep
216 }; 220 };
217 const struct snd_usb_audio_quirk *quirk = 221 const struct snd_usb_audio_quirk *quirk =
218 chip->usb_id == USB_ID(0x0582, 0x002b) 222 chip->usb_id == USB_ID(0x0582, 0x002b)
219 ? &ua700_quirk : &uaxx_quirk; 223 ? &ua700_quirk : &uaxx_quirk;
220 return snd_usbmidi_create(chip->card, iface, 224 return snd_usbmidi_create(chip->card, iface,
221 &chip->midi_list, quirk); 225 &chip->midi_list, quirk);
222 } 226 }
223 227
224 if (altsd->bNumEndpoints != 1) 228 if (altsd->bNumEndpoints != 1)
225 return -ENXIO; 229 return -ENXIO;
226 230
227 fp = kmemdup(&ua_format, sizeof(*fp), GFP_KERNEL); 231 fp = kmemdup(&ua_format, sizeof(*fp), GFP_KERNEL);
228 if (!fp) 232 if (!fp)
229 return -ENOMEM; 233 return -ENOMEM;
230 234
231 fp->iface = altsd->bInterfaceNumber; 235 fp->iface = altsd->bInterfaceNumber;
232 fp->endpoint = get_endpoint(alts, 0)->bEndpointAddress; 236 fp->endpoint = get_endpoint(alts, 0)->bEndpointAddress;
233 fp->ep_attr = get_endpoint(alts, 0)->bmAttributes; 237 fp->ep_attr = get_endpoint(alts, 0)->bmAttributes;
234 fp->datainterval = 0; 238 fp->datainterval = 0;
235 fp->maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize); 239 fp->maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize);
236 240
237 switch (fp->maxpacksize) { 241 switch (fp->maxpacksize) {
238 case 0x120: 242 case 0x120:
239 fp->rate_max = fp->rate_min = 44100; 243 fp->rate_max = fp->rate_min = 44100;
240 break; 244 break;
241 case 0x138: 245 case 0x138:
242 case 0x140: 246 case 0x140:
243 fp->rate_max = fp->rate_min = 48000; 247 fp->rate_max = fp->rate_min = 48000;
244 break; 248 break;
245 case 0x258: 249 case 0x258:
246 case 0x260: 250 case 0x260:
247 fp->rate_max = fp->rate_min = 96000; 251 fp->rate_max = fp->rate_min = 96000;
248 break; 252 break;
249 default: 253 default:
250 snd_printk(KERN_ERR "unknown sample rate\n"); 254 snd_printk(KERN_ERR "unknown sample rate\n");
251 kfree(fp); 255 kfree(fp);
252 return -ENXIO; 256 return -ENXIO;
253 } 257 }
254 258
255 stream = (fp->endpoint & USB_DIR_IN) 259 stream = (fp->endpoint & USB_DIR_IN)
256 ? SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK; 260 ? SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
257 err = snd_usb_add_audio_stream(chip, stream, fp); 261 err = snd_usb_add_audio_stream(chip, stream, fp);
258 if (err < 0) { 262 if (err < 0) {
259 kfree(fp); 263 kfree(fp);
260 return err; 264 return err;
261 } 265 }
262 usb_set_interface(chip->dev, fp->iface, 0); 266 usb_set_interface(chip->dev, fp->iface, 0);
263 return 0; 267 return 0;
264 } 268 }
265 269
266 /* 270 /*
267 * Create a standard mixer for the specified interface. 271 * Create a standard mixer for the specified interface.
268 */ 272 */
269 static int create_standard_mixer_quirk(struct snd_usb_audio *chip, 273 static int create_standard_mixer_quirk(struct snd_usb_audio *chip,
270 struct usb_interface *iface, 274 struct usb_interface *iface,
271 struct usb_driver *driver, 275 struct usb_driver *driver,
272 const struct snd_usb_audio_quirk *quirk) 276 const struct snd_usb_audio_quirk *quirk)
273 { 277 {
274 if (quirk->ifnum < 0) 278 if (quirk->ifnum < 0)
275 return 0; 279 return 0;
276 280
277 return snd_usb_create_mixer(chip, quirk->ifnum, 0); 281 return snd_usb_create_mixer(chip, quirk->ifnum, 0);
278 } 282 }
279 283
280 /* 284 /*
281 * audio-interface quirks 285 * audio-interface quirks
282 * 286 *
283 * returns zero if no standard audio/MIDI parsing is needed. 287 * returns zero if no standard audio/MIDI parsing is needed.
284 * returns a positive value if standard audio/midi interfaces are parsed 288 * returns a positive value if standard audio/midi interfaces are parsed
285 * after this. 289 * after this.
286 * returns a negative value at error. 290 * returns a negative value at error.
287 */ 291 */
288 int snd_usb_create_quirk(struct snd_usb_audio *chip, 292 int snd_usb_create_quirk(struct snd_usb_audio *chip,
289 struct usb_interface *iface, 293 struct usb_interface *iface,
290 struct usb_driver *driver, 294 struct usb_driver *driver,
291 const struct snd_usb_audio_quirk *quirk) 295 const struct snd_usb_audio_quirk *quirk)
292 { 296 {
293 typedef int (*quirk_func_t)(struct snd_usb_audio *, 297 typedef int (*quirk_func_t)(struct snd_usb_audio *,
294 struct usb_interface *, 298 struct usb_interface *,
295 struct usb_driver *, 299 struct usb_driver *,
296 const struct snd_usb_audio_quirk *); 300 const struct snd_usb_audio_quirk *);
297 static const quirk_func_t quirk_funcs[] = { 301 static const quirk_func_t quirk_funcs[] = {
298 [QUIRK_IGNORE_INTERFACE] = ignore_interface_quirk, 302 [QUIRK_IGNORE_INTERFACE] = ignore_interface_quirk,
299 [QUIRK_COMPOSITE] = create_composite_quirk, 303 [QUIRK_COMPOSITE] = create_composite_quirk,
300 [QUIRK_MIDI_STANDARD_INTERFACE] = create_any_midi_quirk, 304 [QUIRK_MIDI_STANDARD_INTERFACE] = create_any_midi_quirk,
301 [QUIRK_MIDI_FIXED_ENDPOINT] = create_any_midi_quirk, 305 [QUIRK_MIDI_FIXED_ENDPOINT] = create_any_midi_quirk,
302 [QUIRK_MIDI_YAMAHA] = create_any_midi_quirk, 306 [QUIRK_MIDI_YAMAHA] = create_any_midi_quirk,
303 [QUIRK_MIDI_MIDIMAN] = create_any_midi_quirk, 307 [QUIRK_MIDI_MIDIMAN] = create_any_midi_quirk,
304 [QUIRK_MIDI_NOVATION] = create_any_midi_quirk, 308 [QUIRK_MIDI_NOVATION] = create_any_midi_quirk,
305 [QUIRK_MIDI_RAW_BYTES] = create_any_midi_quirk, 309 [QUIRK_MIDI_RAW_BYTES] = create_any_midi_quirk,
306 [QUIRK_MIDI_EMAGIC] = create_any_midi_quirk, 310 [QUIRK_MIDI_EMAGIC] = create_any_midi_quirk,
307 [QUIRK_MIDI_CME] = create_any_midi_quirk, 311 [QUIRK_MIDI_CME] = create_any_midi_quirk,
308 [QUIRK_MIDI_AKAI] = create_any_midi_quirk, 312 [QUIRK_MIDI_AKAI] = create_any_midi_quirk,
309 [QUIRK_MIDI_FTDI] = create_any_midi_quirk, 313 [QUIRK_MIDI_FTDI] = create_any_midi_quirk,
310 [QUIRK_AUDIO_STANDARD_INTERFACE] = create_standard_audio_quirk, 314 [QUIRK_AUDIO_STANDARD_INTERFACE] = create_standard_audio_quirk,
311 [QUIRK_AUDIO_FIXED_ENDPOINT] = create_fixed_stream_quirk, 315 [QUIRK_AUDIO_FIXED_ENDPOINT] = create_fixed_stream_quirk,
312 [QUIRK_AUDIO_EDIROL_UAXX] = create_uaxx_quirk, 316 [QUIRK_AUDIO_EDIROL_UAXX] = create_uaxx_quirk,
313 [QUIRK_AUDIO_ALIGN_TRANSFER] = create_align_transfer_quirk, 317 [QUIRK_AUDIO_ALIGN_TRANSFER] = create_align_transfer_quirk,
314 [QUIRK_AUDIO_STANDARD_MIXER] = create_standard_mixer_quirk, 318 [QUIRK_AUDIO_STANDARD_MIXER] = create_standard_mixer_quirk,
315 }; 319 };
316 320
317 if (quirk->type < QUIRK_TYPE_COUNT) { 321 if (quirk->type < QUIRK_TYPE_COUNT) {
318 return quirk_funcs[quirk->type](chip, iface, driver, quirk); 322 return quirk_funcs[quirk->type](chip, iface, driver, quirk);
319 } else { 323 } else {
320 snd_printd(KERN_ERR "invalid quirk type %d\n", quirk->type); 324 snd_printd(KERN_ERR "invalid quirk type %d\n", quirk->type);
321 return -ENXIO; 325 return -ENXIO;
322 } 326 }
323 } 327 }
324 328
325 /* 329 /*
326 * boot quirks 330 * boot quirks
327 */ 331 */
328 332
329 #define EXTIGY_FIRMWARE_SIZE_OLD 794 333 #define EXTIGY_FIRMWARE_SIZE_OLD 794
330 #define EXTIGY_FIRMWARE_SIZE_NEW 483 334 #define EXTIGY_FIRMWARE_SIZE_NEW 483
331 335
332 static int snd_usb_extigy_boot_quirk(struct usb_device *dev, struct usb_interface *intf) 336 static int snd_usb_extigy_boot_quirk(struct usb_device *dev, struct usb_interface *intf)
333 { 337 {
334 struct usb_host_config *config = dev->actconfig; 338 struct usb_host_config *config = dev->actconfig;
335 int err; 339 int err;
336 340
337 if (le16_to_cpu(get_cfg_desc(config)->wTotalLength) == EXTIGY_FIRMWARE_SIZE_OLD || 341 if (le16_to_cpu(get_cfg_desc(config)->wTotalLength) == EXTIGY_FIRMWARE_SIZE_OLD ||
338 le16_to_cpu(get_cfg_desc(config)->wTotalLength) == EXTIGY_FIRMWARE_SIZE_NEW) { 342 le16_to_cpu(get_cfg_desc(config)->wTotalLength) == EXTIGY_FIRMWARE_SIZE_NEW) {
339 snd_printdd("sending Extigy boot sequence...\n"); 343 snd_printdd("sending Extigy boot sequence...\n");
340 /* Send message to force it to reconnect with full interface. */ 344 /* Send message to force it to reconnect with full interface. */
341 err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev,0), 345 err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev,0),
342 0x10, 0x43, 0x0001, 0x000a, NULL, 0); 346 0x10, 0x43, 0x0001, 0x000a, NULL, 0);
343 if (err < 0) snd_printdd("error sending boot message: %d\n", err); 347 if (err < 0) snd_printdd("error sending boot message: %d\n", err);
344 err = usb_get_descriptor(dev, USB_DT_DEVICE, 0, 348 err = usb_get_descriptor(dev, USB_DT_DEVICE, 0,
345 &dev->descriptor, sizeof(dev->descriptor)); 349 &dev->descriptor, sizeof(dev->descriptor));
346 config = dev->actconfig; 350 config = dev->actconfig;
347 if (err < 0) snd_printdd("error usb_get_descriptor: %d\n", err); 351 if (err < 0) snd_printdd("error usb_get_descriptor: %d\n", err);
348 err = usb_reset_configuration(dev); 352 err = usb_reset_configuration(dev);
349 if (err < 0) snd_printdd("error usb_reset_configuration: %d\n", err); 353 if (err < 0) snd_printdd("error usb_reset_configuration: %d\n", err);
350 snd_printdd("extigy_boot: new boot length = %d\n", 354 snd_printdd("extigy_boot: new boot length = %d\n",
351 le16_to_cpu(get_cfg_desc(config)->wTotalLength)); 355 le16_to_cpu(get_cfg_desc(config)->wTotalLength));
352 return -ENODEV; /* quit this anyway */ 356 return -ENODEV; /* quit this anyway */
353 } 357 }
354 return 0; 358 return 0;
355 } 359 }
356 360
357 static int snd_usb_audigy2nx_boot_quirk(struct usb_device *dev) 361 static int snd_usb_audigy2nx_boot_quirk(struct usb_device *dev)
358 { 362 {
359 u8 buf = 1; 363 u8 buf = 1;
360 364
361 snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), 0x2a, 365 snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), 0x2a,
362 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_OTHER, 366 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_OTHER,
363 0, 0, &buf, 1); 367 0, 0, &buf, 1);
364 if (buf == 0) { 368 if (buf == 0) {
365 snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), 0x29, 369 snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), 0x29,
366 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER, 370 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
367 1, 2000, NULL, 0); 371 1, 2000, NULL, 0);
368 return -ENODEV; 372 return -ENODEV;
369 } 373 }
370 return 0; 374 return 0;
371 } 375 }
372 376
373 static int snd_usb_fasttrackpro_boot_quirk(struct usb_device *dev) 377 static int snd_usb_fasttrackpro_boot_quirk(struct usb_device *dev)
374 { 378 {
375 int err; 379 int err;
376 380
377 if (dev->actconfig->desc.bConfigurationValue == 1) { 381 if (dev->actconfig->desc.bConfigurationValue == 1) {
378 snd_printk(KERN_INFO "usb-audio: " 382 snd_printk(KERN_INFO "usb-audio: "
379 "Fast Track Pro switching to config #2\n"); 383 "Fast Track Pro switching to config #2\n");
380 /* This function has to be available by the usb core module. 384 /* This function has to be available by the usb core module.
381 * if it is not avialable the boot quirk has to be left out 385 * if it is not avialable the boot quirk has to be left out
382 * and the configuration has to be set by udev or hotplug 386 * and the configuration has to be set by udev or hotplug
383 * rules 387 * rules
384 */ 388 */
385 err = usb_driver_set_configuration(dev, 2); 389 err = usb_driver_set_configuration(dev, 2);
386 if (err < 0) { 390 if (err < 0) {
387 snd_printdd("error usb_driver_set_configuration: %d\n", 391 snd_printdd("error usb_driver_set_configuration: %d\n",
388 err); 392 err);
389 return -ENODEV; 393 return -ENODEV;
390 } 394 }
391 } else 395 } else
392 snd_printk(KERN_INFO "usb-audio: Fast Track Pro config OK\n"); 396 snd_printk(KERN_INFO "usb-audio: Fast Track Pro config OK\n");
393 397
394 return 0; 398 return 0;
395 } 399 }
396 400
397 /* 401 /*
398 * C-Media CM106/CM106+ have four 16-bit internal registers that are nicely 402 * C-Media CM106/CM106+ have four 16-bit internal registers that are nicely
399 * documented in the device's data sheet. 403 * documented in the device's data sheet.
400 */ 404 */
401 static int snd_usb_cm106_write_int_reg(struct usb_device *dev, int reg, u16 value) 405 static int snd_usb_cm106_write_int_reg(struct usb_device *dev, int reg, u16 value)
402 { 406 {
403 u8 buf[4]; 407 u8 buf[4];
404 buf[0] = 0x20; 408 buf[0] = 0x20;
405 buf[1] = value & 0xff; 409 buf[1] = value & 0xff;
406 buf[2] = (value >> 8) & 0xff; 410 buf[2] = (value >> 8) & 0xff;
407 buf[3] = reg; 411 buf[3] = reg;
408 return snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), USB_REQ_SET_CONFIGURATION, 412 return snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), USB_REQ_SET_CONFIGURATION,
409 USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_ENDPOINT, 413 USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
410 0, 0, &buf, 4); 414 0, 0, &buf, 4);
411 } 415 }
412 416
413 static int snd_usb_cm106_boot_quirk(struct usb_device *dev) 417 static int snd_usb_cm106_boot_quirk(struct usb_device *dev)
414 { 418 {
415 /* 419 /*
416 * Enable line-out driver mode, set headphone source to front 420 * Enable line-out driver mode, set headphone source to front
417 * channels, enable stereo mic. 421 * channels, enable stereo mic.
418 */ 422 */
419 return snd_usb_cm106_write_int_reg(dev, 2, 0x8004); 423 return snd_usb_cm106_write_int_reg(dev, 2, 0x8004);
420 } 424 }
421 425
422 /* 426 /*
423 * C-Media CM6206 is based on CM106 with two additional 427 * C-Media CM6206 is based on CM106 with two additional
424 * registers that are not documented in the data sheet. 428 * registers that are not documented in the data sheet.
425 * Values here are chosen based on sniffing USB traffic 429 * Values here are chosen based on sniffing USB traffic
426 * under Windows. 430 * under Windows.
427 */ 431 */
428 static int snd_usb_cm6206_boot_quirk(struct usb_device *dev) 432 static int snd_usb_cm6206_boot_quirk(struct usb_device *dev)
429 { 433 {
430 int err = 0, reg; 434 int err = 0, reg;
431 int val[] = {0x2004, 0x3000, 0xf800, 0x143f, 0x0000, 0x3000}; 435 int val[] = {0x2004, 0x3000, 0xf800, 0x143f, 0x0000, 0x3000};
432 436
433 for (reg = 0; reg < ARRAY_SIZE(val); reg++) { 437 for (reg = 0; reg < ARRAY_SIZE(val); reg++) {
434 err = snd_usb_cm106_write_int_reg(dev, reg, val[reg]); 438 err = snd_usb_cm106_write_int_reg(dev, reg, val[reg]);
435 if (err < 0) 439 if (err < 0)
436 return err; 440 return err;
437 } 441 }
438 442
439 return err; 443 return err;
440 } 444 }
441 445
442 /* 446 /*
443 * This call will put the synth in "USB send" mode, i.e it will send MIDI 447 * This call will put the synth in "USB send" mode, i.e it will send MIDI
444 * messages through USB (this is disabled at startup). The synth will 448 * messages through USB (this is disabled at startup). The synth will
445 * acknowledge by sending a sysex on endpoint 0x85 and by displaying a USB 449 * acknowledge by sending a sysex on endpoint 0x85 and by displaying a USB
446 * sign on its LCD. Values here are chosen based on sniffing USB traffic 450 * sign on its LCD. Values here are chosen based on sniffing USB traffic
447 * under Windows. 451 * under Windows.
448 */ 452 */
449 static int snd_usb_accessmusic_boot_quirk(struct usb_device *dev) 453 static int snd_usb_accessmusic_boot_quirk(struct usb_device *dev)
450 { 454 {
451 int err, actual_length; 455 int err, actual_length;
452 456
453 /* "midi send" enable */ 457 /* "midi send" enable */
454 static const u8 seq[] = { 0x4e, 0x73, 0x52, 0x01 }; 458 static const u8 seq[] = { 0x4e, 0x73, 0x52, 0x01 };
455 459
456 void *buf = kmemdup(seq, ARRAY_SIZE(seq), GFP_KERNEL); 460 void *buf = kmemdup(seq, ARRAY_SIZE(seq), GFP_KERNEL);
457 if (!buf) 461 if (!buf)
458 return -ENOMEM; 462 return -ENOMEM;
459 err = usb_interrupt_msg(dev, usb_sndintpipe(dev, 0x05), buf, 463 err = usb_interrupt_msg(dev, usb_sndintpipe(dev, 0x05), buf,
460 ARRAY_SIZE(seq), &actual_length, 1000); 464 ARRAY_SIZE(seq), &actual_length, 1000);
461 kfree(buf); 465 kfree(buf);
462 if (err < 0) 466 if (err < 0)
463 return err; 467 return err;
464 468
465 return 0; 469 return 0;
466 } 470 }
467 471
468 /* 472 /*
469 * Some sound cards from Native Instruments are in fact compliant to the USB 473 * Some sound cards from Native Instruments are in fact compliant to the USB
470 * audio standard of version 2 and other approved USB standards, even though 474 * audio standard of version 2 and other approved USB standards, even though
471 * they come up as vendor-specific device when first connected. 475 * they come up as vendor-specific device when first connected.
472 * 476 *
473 * However, they can be told to come up with a new set of descriptors 477 * However, they can be told to come up with a new set of descriptors
474 * upon their next enumeration, and the interfaces announced by the new 478 * upon their next enumeration, and the interfaces announced by the new
475 * descriptors will then be handled by the kernel's class drivers. As the 479 * descriptors will then be handled by the kernel's class drivers. As the
476 * product ID will also change, no further checks are required. 480 * product ID will also change, no further checks are required.
477 */ 481 */
478 482
479 static int snd_usb_nativeinstruments_boot_quirk(struct usb_device *dev) 483 static int snd_usb_nativeinstruments_boot_quirk(struct usb_device *dev)
480 { 484 {
481 int ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), 485 int ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
482 0xaf, USB_TYPE_VENDOR | USB_RECIP_DEVICE, 486 0xaf, USB_TYPE_VENDOR | USB_RECIP_DEVICE,
483 cpu_to_le16(1), 0, NULL, 0, 1000); 487 cpu_to_le16(1), 0, NULL, 0, 1000);
484 488
485 if (ret < 0) 489 if (ret < 0)
486 return ret; 490 return ret;
487 491
488 usb_reset_device(dev); 492 usb_reset_device(dev);
489 493
490 /* return -EAGAIN, so the creation of an audio interface for this 494 /* return -EAGAIN, so the creation of an audio interface for this
491 * temporary device is aborted. The device will reconnect with a 495 * temporary device is aborted. The device will reconnect with a
492 * new product ID */ 496 * new product ID */
493 return -EAGAIN; 497 return -EAGAIN;
494 } 498 }
495 499
496 /* 500 /*
497 * Setup quirks 501 * Setup quirks
498 */ 502 */
499 #define MAUDIO_SET 0x01 /* parse device_setup */ 503 #define MAUDIO_SET 0x01 /* parse device_setup */
500 #define MAUDIO_SET_COMPATIBLE 0x80 /* use only "win-compatible" interfaces */ 504 #define MAUDIO_SET_COMPATIBLE 0x80 /* use only "win-compatible" interfaces */
501 #define MAUDIO_SET_DTS 0x02 /* enable DTS Digital Output */ 505 #define MAUDIO_SET_DTS 0x02 /* enable DTS Digital Output */
502 #define MAUDIO_SET_96K 0x04 /* 48-96KHz rate if set, 8-48KHz otherwise */ 506 #define MAUDIO_SET_96K 0x04 /* 48-96KHz rate if set, 8-48KHz otherwise */
503 #define MAUDIO_SET_24B 0x08 /* 24bits sample if set, 16bits otherwise */ 507 #define MAUDIO_SET_24B 0x08 /* 24bits sample if set, 16bits otherwise */
504 #define MAUDIO_SET_DI 0x10 /* enable Digital Input */ 508 #define MAUDIO_SET_DI 0x10 /* enable Digital Input */
505 #define MAUDIO_SET_MASK 0x1f /* bit mask for setup value */ 509 #define MAUDIO_SET_MASK 0x1f /* bit mask for setup value */
506 #define MAUDIO_SET_24B_48K_DI 0x19 /* 24bits+48KHz+Digital Input */ 510 #define MAUDIO_SET_24B_48K_DI 0x19 /* 24bits+48KHz+Digital Input */
507 #define MAUDIO_SET_24B_48K_NOTDI 0x09 /* 24bits+48KHz+No Digital Input */ 511 #define MAUDIO_SET_24B_48K_NOTDI 0x09 /* 24bits+48KHz+No Digital Input */
508 #define MAUDIO_SET_16B_48K_DI 0x11 /* 16bits+48KHz+Digital Input */ 512 #define MAUDIO_SET_16B_48K_DI 0x11 /* 16bits+48KHz+Digital Input */
509 #define MAUDIO_SET_16B_48K_NOTDI 0x01 /* 16bits+48KHz+No Digital Input */ 513 #define MAUDIO_SET_16B_48K_NOTDI 0x01 /* 16bits+48KHz+No Digital Input */
510 514
511 static int quattro_skip_setting_quirk(struct snd_usb_audio *chip, 515 static int quattro_skip_setting_quirk(struct snd_usb_audio *chip,
512 int iface, int altno) 516 int iface, int altno)
513 { 517 {
514 /* Reset ALL ifaces to 0 altsetting. 518 /* Reset ALL ifaces to 0 altsetting.
515 * Call it for every possible altsetting of every interface. 519 * Call it for every possible altsetting of every interface.
516 */ 520 */
517 usb_set_interface(chip->dev, iface, 0); 521 usb_set_interface(chip->dev, iface, 0);
518 if (chip->setup & MAUDIO_SET) { 522 if (chip->setup & MAUDIO_SET) {
519 if (chip->setup & MAUDIO_SET_COMPATIBLE) { 523 if (chip->setup & MAUDIO_SET_COMPATIBLE) {
520 if (iface != 1 && iface != 2) 524 if (iface != 1 && iface != 2)
521 return 1; /* skip all interfaces but 1 and 2 */ 525 return 1; /* skip all interfaces but 1 and 2 */
522 } else { 526 } else {
523 unsigned int mask; 527 unsigned int mask;
524 if (iface == 1 || iface == 2) 528 if (iface == 1 || iface == 2)
525 return 1; /* skip interfaces 1 and 2 */ 529 return 1; /* skip interfaces 1 and 2 */
526 if ((chip->setup & MAUDIO_SET_96K) && altno != 1) 530 if ((chip->setup & MAUDIO_SET_96K) && altno != 1)
527 return 1; /* skip this altsetting */ 531 return 1; /* skip this altsetting */
528 mask = chip->setup & MAUDIO_SET_MASK; 532 mask = chip->setup & MAUDIO_SET_MASK;
529 if (mask == MAUDIO_SET_24B_48K_DI && altno != 2) 533 if (mask == MAUDIO_SET_24B_48K_DI && altno != 2)
530 return 1; /* skip this altsetting */ 534 return 1; /* skip this altsetting */
531 if (mask == MAUDIO_SET_24B_48K_NOTDI && altno != 3) 535 if (mask == MAUDIO_SET_24B_48K_NOTDI && altno != 3)
532 return 1; /* skip this altsetting */ 536 return 1; /* skip this altsetting */
533 if (mask == MAUDIO_SET_16B_48K_NOTDI && altno != 4) 537 if (mask == MAUDIO_SET_16B_48K_NOTDI && altno != 4)
534 return 1; /* skip this altsetting */ 538 return 1; /* skip this altsetting */
535 } 539 }
536 } 540 }
537 snd_printdd(KERN_INFO 541 snd_printdd(KERN_INFO
538 "using altsetting %d for interface %d config %d\n", 542 "using altsetting %d for interface %d config %d\n",
539 altno, iface, chip->setup); 543 altno, iface, chip->setup);
540 return 0; /* keep this altsetting */ 544 return 0; /* keep this altsetting */
541 } 545 }
542 546
543 static int audiophile_skip_setting_quirk(struct snd_usb_audio *chip, 547 static int audiophile_skip_setting_quirk(struct snd_usb_audio *chip,
544 int iface, 548 int iface,
545 int altno) 549 int altno)
546 { 550 {
547 /* Reset ALL ifaces to 0 altsetting. 551 /* Reset ALL ifaces to 0 altsetting.
548 * Call it for every possible altsetting of every interface. 552 * Call it for every possible altsetting of every interface.
549 */ 553 */
550 usb_set_interface(chip->dev, iface, 0); 554 usb_set_interface(chip->dev, iface, 0);
551 555
552 if (chip->setup & MAUDIO_SET) { 556 if (chip->setup & MAUDIO_SET) {
553 unsigned int mask; 557 unsigned int mask;
554 if ((chip->setup & MAUDIO_SET_DTS) && altno != 6) 558 if ((chip->setup & MAUDIO_SET_DTS) && altno != 6)
555 return 1; /* skip this altsetting */ 559 return 1; /* skip this altsetting */
556 if ((chip->setup & MAUDIO_SET_96K) && altno != 1) 560 if ((chip->setup & MAUDIO_SET_96K) && altno != 1)
557 return 1; /* skip this altsetting */ 561 return 1; /* skip this altsetting */
558 mask = chip->setup & MAUDIO_SET_MASK; 562 mask = chip->setup & MAUDIO_SET_MASK;
559 if (mask == MAUDIO_SET_24B_48K_DI && altno != 2) 563 if (mask == MAUDIO_SET_24B_48K_DI && altno != 2)
560 return 1; /* skip this altsetting */ 564 return 1; /* skip this altsetting */
561 if (mask == MAUDIO_SET_24B_48K_NOTDI && altno != 3) 565 if (mask == MAUDIO_SET_24B_48K_NOTDI && altno != 3)
562 return 1; /* skip this altsetting */ 566 return 1; /* skip this altsetting */
563 if (mask == MAUDIO_SET_16B_48K_DI && altno != 4) 567 if (mask == MAUDIO_SET_16B_48K_DI && altno != 4)
564 return 1; /* skip this altsetting */ 568 return 1; /* skip this altsetting */
565 if (mask == MAUDIO_SET_16B_48K_NOTDI && altno != 5) 569 if (mask == MAUDIO_SET_16B_48K_NOTDI && altno != 5)
566 return 1; /* skip this altsetting */ 570 return 1; /* skip this altsetting */
567 } 571 }
568 572
569 return 0; /* keep this altsetting */ 573 return 0; /* keep this altsetting */
570 } 574 }
571 575
572 576
573 static int fasttrackpro_skip_setting_quirk(struct snd_usb_audio *chip, 577 static int fasttrackpro_skip_setting_quirk(struct snd_usb_audio *chip,
574 int iface, int altno) 578 int iface, int altno)
575 { 579 {
576 /* Reset ALL ifaces to 0 altsetting. 580 /* Reset ALL ifaces to 0 altsetting.
577 * Call it for every possible altsetting of every interface. 581 * Call it for every possible altsetting of every interface.
578 */ 582 */
579 usb_set_interface(chip->dev, iface, 0); 583 usb_set_interface(chip->dev, iface, 0);
580 584
581 /* possible configuration where both inputs and only one output is 585 /* possible configuration where both inputs and only one output is
582 *used is not supported by the current setup 586 *used is not supported by the current setup
583 */ 587 */
584 if (chip->setup & (MAUDIO_SET | MAUDIO_SET_24B)) { 588 if (chip->setup & (MAUDIO_SET | MAUDIO_SET_24B)) {
585 if (chip->setup & MAUDIO_SET_96K) { 589 if (chip->setup & MAUDIO_SET_96K) {
586 if (altno != 3 && altno != 6) 590 if (altno != 3 && altno != 6)
587 return 1; 591 return 1;
588 } else if (chip->setup & MAUDIO_SET_DI) { 592 } else if (chip->setup & MAUDIO_SET_DI) {
589 if (iface == 4) 593 if (iface == 4)
590 return 1; /* no analog input */ 594 return 1; /* no analog input */
591 if (altno != 2 && altno != 5) 595 if (altno != 2 && altno != 5)
592 return 1; /* enable only altsets 2 and 5 */ 596 return 1; /* enable only altsets 2 and 5 */
593 } else { 597 } else {
594 if (iface == 5) 598 if (iface == 5)
595 return 1; /* disable digialt input */ 599 return 1; /* disable digialt input */
596 if (altno != 2 && altno != 5) 600 if (altno != 2 && altno != 5)
597 return 1; /* enalbe only altsets 2 and 5 */ 601 return 1; /* enalbe only altsets 2 and 5 */
598 } 602 }
599 } else { 603 } else {
600 /* keep only 16-Bit mode */ 604 /* keep only 16-Bit mode */
601 if (altno != 1) 605 if (altno != 1)
602 return 1; 606 return 1;
603 } 607 }
604 608
605 snd_printdd(KERN_INFO 609 snd_printdd(KERN_INFO
606 "using altsetting %d for interface %d config %d\n", 610 "using altsetting %d for interface %d config %d\n",
607 altno, iface, chip->setup); 611 altno, iface, chip->setup);
608 return 0; /* keep this altsetting */ 612 return 0; /* keep this altsetting */
609 } 613 }
610 614
611 int snd_usb_apply_interface_quirk(struct snd_usb_audio *chip, 615 int snd_usb_apply_interface_quirk(struct snd_usb_audio *chip,
612 int iface, 616 int iface,
613 int altno) 617 int altno)
614 { 618 {
615 /* audiophile usb: skip altsets incompatible with device_setup */ 619 /* audiophile usb: skip altsets incompatible with device_setup */
616 if (chip->usb_id == USB_ID(0x0763, 0x2003)) 620 if (chip->usb_id == USB_ID(0x0763, 0x2003))
617 return audiophile_skip_setting_quirk(chip, iface, altno); 621 return audiophile_skip_setting_quirk(chip, iface, altno);
618 /* quattro usb: skip altsets incompatible with device_setup */ 622 /* quattro usb: skip altsets incompatible with device_setup */
619 if (chip->usb_id == USB_ID(0x0763, 0x2001)) 623 if (chip->usb_id == USB_ID(0x0763, 0x2001))
620 return quattro_skip_setting_quirk(chip, iface, altno); 624 return quattro_skip_setting_quirk(chip, iface, altno);
621 /* fasttrackpro usb: skip altsets incompatible with device_setup */ 625 /* fasttrackpro usb: skip altsets incompatible with device_setup */
622 if (chip->usb_id == USB_ID(0x0763, 0x2012)) 626 if (chip->usb_id == USB_ID(0x0763, 0x2012))
623 return fasttrackpro_skip_setting_quirk(chip, iface, altno); 627 return fasttrackpro_skip_setting_quirk(chip, iface, altno);
624 628
625 return 0; 629 return 0;
626 } 630 }
627 631
628 int snd_usb_apply_boot_quirk(struct usb_device *dev, 632 int snd_usb_apply_boot_quirk(struct usb_device *dev,
629 struct usb_interface *intf, 633 struct usb_interface *intf,
630 const struct snd_usb_audio_quirk *quirk) 634 const struct snd_usb_audio_quirk *quirk)
631 { 635 {
632 u32 id = USB_ID(le16_to_cpu(dev->descriptor.idVendor), 636 u32 id = USB_ID(le16_to_cpu(dev->descriptor.idVendor),
633 le16_to_cpu(dev->descriptor.idProduct)); 637 le16_to_cpu(dev->descriptor.idProduct));
634 638
635 switch (id) { 639 switch (id) {
636 case USB_ID(0x041e, 0x3000): 640 case USB_ID(0x041e, 0x3000):
637 /* SB Extigy needs special boot-up sequence */ 641 /* SB Extigy needs special boot-up sequence */
638 /* if more models come, this will go to the quirk list. */ 642 /* if more models come, this will go to the quirk list. */
639 return snd_usb_extigy_boot_quirk(dev, intf); 643 return snd_usb_extigy_boot_quirk(dev, intf);
640 644
641 case USB_ID(0x041e, 0x3020): 645 case USB_ID(0x041e, 0x3020):
642 /* SB Audigy 2 NX needs its own boot-up magic, too */ 646 /* SB Audigy 2 NX needs its own boot-up magic, too */
643 return snd_usb_audigy2nx_boot_quirk(dev); 647 return snd_usb_audigy2nx_boot_quirk(dev);
644 648
645 case USB_ID(0x10f5, 0x0200): 649 case USB_ID(0x10f5, 0x0200):
646 /* C-Media CM106 / Turtle Beach Audio Advantage Roadie */ 650 /* C-Media CM106 / Turtle Beach Audio Advantage Roadie */
647 return snd_usb_cm106_boot_quirk(dev); 651 return snd_usb_cm106_boot_quirk(dev);
648 652
649 case USB_ID(0x0d8c, 0x0102): 653 case USB_ID(0x0d8c, 0x0102):
650 /* C-Media CM6206 / CM106-Like Sound Device */ 654 /* C-Media CM6206 / CM106-Like Sound Device */
651 case USB_ID(0x0ccd, 0x00b1): /* Terratec Aureon 7.1 USB */ 655 case USB_ID(0x0ccd, 0x00b1): /* Terratec Aureon 7.1 USB */
652 return snd_usb_cm6206_boot_quirk(dev); 656 return snd_usb_cm6206_boot_quirk(dev);
653 657
654 case USB_ID(0x133e, 0x0815): 658 case USB_ID(0x133e, 0x0815):
655 /* Access Music VirusTI Desktop */ 659 /* Access Music VirusTI Desktop */
656 return snd_usb_accessmusic_boot_quirk(dev); 660 return snd_usb_accessmusic_boot_quirk(dev);
657 661
658 case USB_ID(0x17cc, 0x1000): /* Komplete Audio 6 */ 662 case USB_ID(0x17cc, 0x1000): /* Komplete Audio 6 */
659 case USB_ID(0x17cc, 0x1010): /* Traktor Audio 6 */ 663 case USB_ID(0x17cc, 0x1010): /* Traktor Audio 6 */
660 case USB_ID(0x17cc, 0x1020): /* Traktor Audio 10 */ 664 case USB_ID(0x17cc, 0x1020): /* Traktor Audio 10 */
661 return snd_usb_nativeinstruments_boot_quirk(dev); 665 return snd_usb_nativeinstruments_boot_quirk(dev);
662 case USB_ID(0x0763, 0x2012): /* M-Audio Fast Track Pro USB */ 666 case USB_ID(0x0763, 0x2012): /* M-Audio Fast Track Pro USB */
663 return snd_usb_fasttrackpro_boot_quirk(dev); 667 return snd_usb_fasttrackpro_boot_quirk(dev);
664 } 668 }
665 669
666 return 0; 670 return 0;
667 } 671 }
668 672
669 /* 673 /*
670 * check if the device uses big-endian samples 674 * check if the device uses big-endian samples
671 */ 675 */
672 int snd_usb_is_big_endian_format(struct snd_usb_audio *chip, struct audioformat *fp) 676 int snd_usb_is_big_endian_format(struct snd_usb_audio *chip, struct audioformat *fp)
673 { 677 {
674 /* it depends on altsetting wether the device is big-endian or not */ 678 /* it depends on altsetting wether the device is big-endian or not */
675 switch (chip->usb_id) { 679 switch (chip->usb_id) {
676 case USB_ID(0x0763, 0x2001): /* M-Audio Quattro: captured data only */ 680 case USB_ID(0x0763, 0x2001): /* M-Audio Quattro: captured data only */
677 if (fp->altsetting == 2 || fp->altsetting == 3 || 681 if (fp->altsetting == 2 || fp->altsetting == 3 ||
678 fp->altsetting == 5 || fp->altsetting == 6) 682 fp->altsetting == 5 || fp->altsetting == 6)
679 return 1; 683 return 1;
680 break; 684 break;
681 case USB_ID(0x0763, 0x2003): /* M-Audio Audiophile USB */ 685 case USB_ID(0x0763, 0x2003): /* M-Audio Audiophile USB */
682 if (chip->setup == 0x00 || 686 if (chip->setup == 0x00 ||
683 fp->altsetting == 1 || fp->altsetting == 2 || 687 fp->altsetting == 1 || fp->altsetting == 2 ||
684 fp->altsetting == 3) 688 fp->altsetting == 3)
685 return 1; 689 return 1;
686 break; 690 break;
687 case USB_ID(0x0763, 0x2012): /* M-Audio Fast Track Pro */ 691 case USB_ID(0x0763, 0x2012): /* M-Audio Fast Track Pro */
688 if (fp->altsetting == 2 || fp->altsetting == 3 || 692 if (fp->altsetting == 2 || fp->altsetting == 3 ||
689 fp->altsetting == 5 || fp->altsetting == 6) 693 fp->altsetting == 5 || fp->altsetting == 6)
690 return 1; 694 return 1;
691 break; 695 break;
692 } 696 }
693 return 0; 697 return 0;
694 } 698 }
695 699
696 /* 700 /*
697 * For E-Mu 0404USB/0202USB/TrackerPre/0204 sample rate should be set for device, 701 * For E-Mu 0404USB/0202USB/TrackerPre/0204 sample rate should be set for device,
698 * not for interface. 702 * not for interface.
699 */ 703 */
700 704
701 enum { 705 enum {
702 EMU_QUIRK_SR_44100HZ = 0, 706 EMU_QUIRK_SR_44100HZ = 0,
703 EMU_QUIRK_SR_48000HZ, 707 EMU_QUIRK_SR_48000HZ,
704 EMU_QUIRK_SR_88200HZ, 708 EMU_QUIRK_SR_88200HZ,
705 EMU_QUIRK_SR_96000HZ, 709 EMU_QUIRK_SR_96000HZ,
706 EMU_QUIRK_SR_176400HZ, 710 EMU_QUIRK_SR_176400HZ,
707 EMU_QUIRK_SR_192000HZ 711 EMU_QUIRK_SR_192000HZ
708 }; 712 };
709 713
710 static void set_format_emu_quirk(struct snd_usb_substream *subs, 714 static void set_format_emu_quirk(struct snd_usb_substream *subs,
711 struct audioformat *fmt) 715 struct audioformat *fmt)
712 { 716 {
713 unsigned char emu_samplerate_id = 0; 717 unsigned char emu_samplerate_id = 0;
714 718
715 /* When capture is active 719 /* When capture is active
716 * sample rate shouldn't be changed 720 * sample rate shouldn't be changed
717 * by playback substream 721 * by playback substream
718 */ 722 */
719 if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK) { 723 if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK) {
720 if (subs->stream->substream[SNDRV_PCM_STREAM_CAPTURE].interface != -1) 724 if (subs->stream->substream[SNDRV_PCM_STREAM_CAPTURE].interface != -1)
721 return; 725 return;
722 } 726 }
723 727
724 switch (fmt->rate_min) { 728 switch (fmt->rate_min) {
725 case 48000: 729 case 48000:
726 emu_samplerate_id = EMU_QUIRK_SR_48000HZ; 730 emu_samplerate_id = EMU_QUIRK_SR_48000HZ;
727 break; 731 break;
728 case 88200: 732 case 88200:
729 emu_samplerate_id = EMU_QUIRK_SR_88200HZ; 733 emu_samplerate_id = EMU_QUIRK_SR_88200HZ;
730 break; 734 break;
731 case 96000: 735 case 96000:
732 emu_samplerate_id = EMU_QUIRK_SR_96000HZ; 736 emu_samplerate_id = EMU_QUIRK_SR_96000HZ;
733 break; 737 break;
734 case 176400: 738 case 176400:
735 emu_samplerate_id = EMU_QUIRK_SR_176400HZ; 739 emu_samplerate_id = EMU_QUIRK_SR_176400HZ;
736 break; 740 break;
737 case 192000: 741 case 192000:
738 emu_samplerate_id = EMU_QUIRK_SR_192000HZ; 742 emu_samplerate_id = EMU_QUIRK_SR_192000HZ;
739 break; 743 break;
740 default: 744 default:
741 emu_samplerate_id = EMU_QUIRK_SR_44100HZ; 745 emu_samplerate_id = EMU_QUIRK_SR_44100HZ;
742 break; 746 break;
743 } 747 }
744 snd_emuusb_set_samplerate(subs->stream->chip, emu_samplerate_id); 748 snd_emuusb_set_samplerate(subs->stream->chip, emu_samplerate_id);
745 } 749 }
746 750
747 void snd_usb_set_format_quirk(struct snd_usb_substream *subs, 751 void snd_usb_set_format_quirk(struct snd_usb_substream *subs,
748 struct audioformat *fmt) 752 struct audioformat *fmt)
749 { 753 {
750 switch (subs->stream->chip->usb_id) { 754 switch (subs->stream->chip->usb_id) {
751 case USB_ID(0x041e, 0x3f02): /* E-Mu 0202 USB */ 755 case USB_ID(0x041e, 0x3f02): /* E-Mu 0202 USB */
752 case USB_ID(0x041e, 0x3f04): /* E-Mu 0404 USB */ 756 case USB_ID(0x041e, 0x3f04): /* E-Mu 0404 USB */
753 case USB_ID(0x041e, 0x3f0a): /* E-Mu Tracker Pre */ 757 case USB_ID(0x041e, 0x3f0a): /* E-Mu Tracker Pre */
754 case USB_ID(0x041e, 0x3f19): /* E-Mu 0204 USB */ 758 case USB_ID(0x041e, 0x3f19): /* E-Mu 0204 USB */
755 set_format_emu_quirk(subs, fmt); 759 set_format_emu_quirk(subs, fmt);
756 break; 760 break;
757 } 761 }
758 } 762 }
759 763
760 764