Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * (Tentative) USB Audio Driver for ALSA |
| 3 | * |
| 4 | * Main and PCM part |
| 5 | * |
| 6 | * Copyright (c) 2002 by Takashi Iwai <tiwai@suse.de> |
| 7 | * |
| 8 | * Many codes borrowed from audio.c by |
| 9 | * Alan Cox (alan@lxorguk.ukuu.org.uk) |
| 10 | * Thomas Sailer (sailer@ife.ee.ethz.ch) |
| 11 | * |
| 12 | * |
| 13 | * This program is free software; you can redistribute it and/or modify |
| 14 | * it under the terms of the GNU General Public License as published by |
| 15 | * the Free Software Foundation; either version 2 of the License, or |
| 16 | * (at your option) any later version. |
| 17 | * |
| 18 | * This program is distributed in the hope that it will be useful, |
| 19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 | * GNU General Public License for more details. |
| 22 | * |
| 23 | * You should have received a copy of the GNU General Public License |
| 24 | * along with this program; if not, write to the Free Software |
| 25 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 26 | * |
| 27 | * |
| 28 | * NOTES: |
| 29 | * |
| 30 | * - async unlink should be used for avoiding the sleep inside lock. |
| 31 | * 2.4.22 usb-uhci seems buggy for async unlinking and results in |
| 32 | * oops. in such a cse, pass async_unlink=0 option. |
| 33 | * - the linked URBs would be preferred but not used so far because of |
| 34 | * the instability of unlinking. |
| 35 | * - type II is not supported properly. there is no device which supports |
| 36 | * this type *correctly*. SB extigy looks as if it supports, but it's |
| 37 | * indeed an AC3 stream packed in SPDIF frames (i.e. no real AC3 stream). |
| 38 | */ |
| 39 | |
| 40 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | #include <linux/bitops.h> |
| 42 | #include <linux/init.h> |
| 43 | #include <linux/list.h> |
| 44 | #include <linux/slab.h> |
| 45 | #include <linux/string.h> |
| 46 | #include <linux/usb.h> |
| 47 | #include <linux/moduleparam.h> |
Ingo Molnar | 12aa757 | 2006-01-16 16:36:05 +0100 | [diff] [blame] | 48 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | #include <sound/core.h> |
| 50 | #include <sound/info.h> |
| 51 | #include <sound/pcm.h> |
| 52 | #include <sound/pcm_params.h> |
| 53 | #include <sound/initval.h> |
| 54 | |
| 55 | #include "usbaudio.h" |
| 56 | |
| 57 | |
| 58 | MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>"); |
| 59 | MODULE_DESCRIPTION("USB Audio"); |
| 60 | MODULE_LICENSE("GPL"); |
| 61 | MODULE_SUPPORTED_DEVICE("{{Generic,USB Audio}}"); |
| 62 | |
| 63 | |
| 64 | static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */ |
| 65 | static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */ |
Pavel Machek | 07f51a7 | 2008-04-14 13:15:56 +0200 | [diff] [blame] | 66 | static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;/* Enable this card */ |
| 67 | /* Vendor/product IDs for this card */ |
| 68 | static int vid[SNDRV_CARDS] = { [0 ... (SNDRV_CARDS-1)] = -1 }; |
| 69 | static int pid[SNDRV_CARDS] = { [0 ... (SNDRV_CARDS-1)] = -1 }; |
Clemens Ladisch | 92b9ac7 | 2006-09-22 10:57:36 +0200 | [diff] [blame] | 70 | static int nrpacks = 8; /* max. number of packets per urb */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | static int async_unlink = 1; |
Thibault LE MEUR | e311334 | 2006-03-14 11:44:53 +0100 | [diff] [blame] | 72 | static int device_setup[SNDRV_CARDS]; /* device parameter for this card*/ |
Takashi Iwai | 7a9b806 | 2008-08-13 15:40:53 +0200 | [diff] [blame] | 73 | static int ignore_ctl_error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | |
| 75 | module_param_array(index, int, NULL, 0444); |
| 76 | MODULE_PARM_DESC(index, "Index value for the USB audio adapter."); |
| 77 | module_param_array(id, charp, NULL, 0444); |
| 78 | MODULE_PARM_DESC(id, "ID string for the USB audio adapter."); |
| 79 | module_param_array(enable, bool, NULL, 0444); |
| 80 | MODULE_PARM_DESC(enable, "Enable USB audio adapter."); |
| 81 | module_param_array(vid, int, NULL, 0444); |
| 82 | MODULE_PARM_DESC(vid, "Vendor ID for the USB audio device."); |
| 83 | module_param_array(pid, int, NULL, 0444); |
| 84 | MODULE_PARM_DESC(pid, "Product ID for the USB audio device."); |
Clemens Ladisch | 71d848c | 2005-08-12 15:18:00 +0200 | [diff] [blame] | 85 | module_param(nrpacks, int, 0644); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | MODULE_PARM_DESC(nrpacks, "Max. number of packets per URB."); |
| 87 | module_param(async_unlink, bool, 0444); |
| 88 | MODULE_PARM_DESC(async_unlink, "Use async unlink mode."); |
Thibault LE MEUR | e311334 | 2006-03-14 11:44:53 +0100 | [diff] [blame] | 89 | module_param_array(device_setup, int, NULL, 0444); |
| 90 | MODULE_PARM_DESC(device_setup, "Specific device setup (if needed)."); |
Takashi Iwai | 7a9b806 | 2008-08-13 15:40:53 +0200 | [diff] [blame] | 91 | module_param(ignore_ctl_error, bool, 0444); |
| 92 | MODULE_PARM_DESC(ignore_ctl_error, |
| 93 | "Ignore errors from USB controller for mixer interfaces."); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | |
| 95 | /* |
| 96 | * debug the h/w constraints |
| 97 | */ |
| 98 | /* #define HW_CONST_DEBUG */ |
| 99 | |
| 100 | |
| 101 | /* |
| 102 | * |
| 103 | */ |
| 104 | |
Clemens Ladisch | 92b9ac7 | 2006-09-22 10:57:36 +0200 | [diff] [blame] | 105 | #define MAX_PACKS 20 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | #define MAX_PACKS_HS (MAX_PACKS * 8) /* in high speed mode */ |
Clemens Ladisch | 15a24c0 | 2005-08-12 08:25:26 +0200 | [diff] [blame] | 107 | #define MAX_URBS 8 |
Clemens Ladisch | 604cf49 | 2005-05-17 09:15:27 +0200 | [diff] [blame] | 108 | #define SYNC_URBS 4 /* always four urbs for sync */ |
Clemens Ladisch | 4d788e0 | 2009-01-26 08:09:28 +0100 | [diff] [blame] | 109 | #define MAX_QUEUE 24 /* try not to exceed this queue length, in ms */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | struct audioformat { |
| 112 | struct list_head list; |
| 113 | snd_pcm_format_t format; /* format type */ |
| 114 | unsigned int channels; /* # channels */ |
| 115 | unsigned int fmt_type; /* USB audio format type (1-3) */ |
| 116 | unsigned int frame_size; /* samples per frame for non-audio */ |
| 117 | int iface; /* interface number */ |
| 118 | unsigned char altsetting; /* corresponding alternate setting */ |
| 119 | unsigned char altset_idx; /* array index of altenate setting */ |
| 120 | unsigned char attributes; /* corresponding attributes of cs endpoint */ |
| 121 | unsigned char endpoint; /* endpoint */ |
| 122 | unsigned char ep_attr; /* endpoint attributes */ |
Clemens Ladisch | 744b89e | 2009-04-03 09:45:01 +0200 | [diff] [blame] | 123 | unsigned char datainterval; /* log_2 of data packet interval */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | unsigned int maxpacksize; /* max. packet size */ |
| 125 | unsigned int rates; /* rate bitmasks */ |
| 126 | unsigned int rate_min, rate_max; /* min/max rates */ |
| 127 | unsigned int nr_rates; /* number of rate table entries */ |
| 128 | unsigned int *rate_table; /* rate table */ |
| 129 | }; |
| 130 | |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 131 | struct snd_usb_substream; |
| 132 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | struct snd_urb_ctx { |
| 134 | struct urb *urb; |
Clemens Ladisch | 55851f7 | 2005-08-15 08:34:16 +0200 | [diff] [blame] | 135 | unsigned int buffer_size; /* size of data buffer, if data URB */ |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 136 | struct snd_usb_substream *subs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | int index; /* index for urb array */ |
| 138 | int packets; /* number of packets per urb */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | }; |
| 140 | |
| 141 | struct snd_urb_ops { |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 142 | int (*prepare)(struct snd_usb_substream *subs, struct snd_pcm_runtime *runtime, struct urb *u); |
| 143 | int (*retire)(struct snd_usb_substream *subs, struct snd_pcm_runtime *runtime, struct urb *u); |
| 144 | int (*prepare_sync)(struct snd_usb_substream *subs, struct snd_pcm_runtime *runtime, struct urb *u); |
| 145 | int (*retire_sync)(struct snd_usb_substream *subs, struct snd_pcm_runtime *runtime, struct urb *u); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | }; |
| 147 | |
| 148 | struct snd_usb_substream { |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 149 | struct snd_usb_stream *stream; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | struct usb_device *dev; |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 151 | struct snd_pcm_substream *pcm_substream; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | int direction; /* playback or capture */ |
| 153 | int interface; /* current interface */ |
| 154 | int endpoint; /* assigned endpoint */ |
| 155 | struct audioformat *cur_audiofmt; /* current audioformat pointer (for hw_params callback) */ |
| 156 | unsigned int cur_rate; /* current rate (for hw_params callback) */ |
| 157 | unsigned int period_bytes; /* current period bytes (for hw_params callback) */ |
| 158 | unsigned int format; /* USB data format */ |
| 159 | unsigned int datapipe; /* the data i/o pipe */ |
| 160 | unsigned int syncpipe; /* 1 - async out or adaptive in */ |
Clemens Ladisch | 573567e | 2005-06-27 08:17:30 +0200 | [diff] [blame] | 161 | unsigned int datainterval; /* log_2 of data packet interval */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | unsigned int syncinterval; /* P for adaptive mode, 0 otherwise */ |
| 163 | unsigned int freqn; /* nominal sampling rate in fs/fps in Q16.16 format */ |
| 164 | unsigned int freqm; /* momentary sampling rate in fs/fps in Q16.16 format */ |
| 165 | unsigned int freqmax; /* maximum sampling rate, used for buffer management */ |
| 166 | unsigned int phase; /* phase accumulator */ |
| 167 | unsigned int maxpacksize; /* max packet size in bytes */ |
| 168 | unsigned int maxframesize; /* max packet size in frames */ |
| 169 | unsigned int curpacksize; /* current packet size in bytes (for capture) */ |
| 170 | unsigned int curframesize; /* current packet size in frames (for capture) */ |
| 171 | unsigned int fill_max: 1; /* fill max packet size always */ |
| 172 | unsigned int fmt_type; /* USB audio format type (1-3) */ |
| 173 | |
| 174 | unsigned int running: 1; /* running status */ |
| 175 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | unsigned int hwptr_done; /* processed frame position in the buffer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | unsigned int transfer_done; /* processed frames since last period update */ |
| 178 | unsigned long active_mask; /* bitmask of active urbs */ |
| 179 | unsigned long unlink_mask; /* bitmask of unlinked urbs */ |
| 180 | |
| 181 | unsigned int nurbs; /* # urbs */ |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 182 | struct snd_urb_ctx dataurb[MAX_URBS]; /* data urb table */ |
| 183 | struct snd_urb_ctx syncurb[SYNC_URBS]; /* sync urb table */ |
Clemens Ladisch | 55851f7 | 2005-08-15 08:34:16 +0200 | [diff] [blame] | 184 | char *syncbuf; /* sync buffer for all sync URBs */ |
| 185 | dma_addr_t sync_dma; /* DMA address of syncbuf */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | |
| 187 | u64 formats; /* format bitmasks (all or'ed) */ |
| 188 | unsigned int num_formats; /* number of supported audio formats (list) */ |
| 189 | struct list_head fmt_list; /* format list */ |
Takashi Iwai | 8fec560 | 2007-02-01 11:50:56 +0100 | [diff] [blame] | 190 | struct snd_pcm_hw_constraint_list rate_list; /* limited rates */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | spinlock_t lock; |
| 192 | |
| 193 | struct snd_urb_ops ops; /* callbacks (must be filled at init) */ |
| 194 | }; |
| 195 | |
| 196 | |
| 197 | struct snd_usb_stream { |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 198 | struct snd_usb_audio *chip; |
| 199 | struct snd_pcm *pcm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | int pcm_index; |
| 201 | unsigned int fmt_type; /* USB audio format type (1-3) */ |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 202 | struct snd_usb_substream substream[2]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | struct list_head list; |
| 204 | }; |
| 205 | |
| 206 | |
| 207 | /* |
| 208 | * we keep the snd_usb_audio_t instances by ourselves for merging |
| 209 | * the all interfaces on the same card as one sound device. |
| 210 | */ |
| 211 | |
Ingo Molnar | 12aa757 | 2006-01-16 16:36:05 +0100 | [diff] [blame] | 212 | static DEFINE_MUTEX(register_mutex); |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 213 | static struct snd_usb_audio *usb_chip[SNDRV_CARDS]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | |
| 215 | |
| 216 | /* |
| 217 | * convert a sampling rate into our full speed format (fs/1000 in Q16.16) |
| 218 | * this will overflow at approx 524 kHz |
| 219 | */ |
Jesper Juhl | 77933d7 | 2005-07-27 11:46:09 -0700 | [diff] [blame] | 220 | static inline unsigned get_usb_full_speed_rate(unsigned int rate) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | { |
| 222 | return ((rate << 13) + 62) / 125; |
| 223 | } |
| 224 | |
| 225 | /* |
| 226 | * convert a sampling rate into USB high speed format (fs/8000 in Q16.16) |
| 227 | * this will overflow at approx 4 MHz |
| 228 | */ |
Jesper Juhl | 77933d7 | 2005-07-27 11:46:09 -0700 | [diff] [blame] | 229 | static inline unsigned get_usb_high_speed_rate(unsigned int rate) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | { |
| 231 | return ((rate << 10) + 62) / 125; |
| 232 | } |
| 233 | |
| 234 | /* convert our full speed USB rate into sampling rate in Hz */ |
Jesper Juhl | 77933d7 | 2005-07-27 11:46:09 -0700 | [diff] [blame] | 235 | static inline unsigned get_full_speed_hz(unsigned int usb_rate) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | { |
| 237 | return (usb_rate * 125 + (1 << 12)) >> 13; |
| 238 | } |
| 239 | |
| 240 | /* convert our high speed USB rate into sampling rate in Hz */ |
Jesper Juhl | 77933d7 | 2005-07-27 11:46:09 -0700 | [diff] [blame] | 241 | static inline unsigned get_high_speed_hz(unsigned int usb_rate) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | { |
| 243 | return (usb_rate * 125 + (1 << 9)) >> 10; |
| 244 | } |
| 245 | |
| 246 | |
| 247 | /* |
| 248 | * prepare urb for full speed capture sync pipe |
| 249 | * |
| 250 | * fill the length and offset of each urb descriptor. |
| 251 | * the fixed 10.14 frequency is passed through the pipe. |
| 252 | */ |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 253 | static int prepare_capture_sync_urb(struct snd_usb_substream *subs, |
| 254 | struct snd_pcm_runtime *runtime, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | struct urb *urb) |
| 256 | { |
| 257 | unsigned char *cp = urb->transfer_buffer; |
John Daiker | 8851827 | 2006-12-28 13:55:05 +0100 | [diff] [blame] | 258 | struct snd_urb_ctx *ctx = urb->context; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | urb->dev = ctx->subs->dev; /* we need to set this at each time */ |
Clemens Ladisch | ca81090 | 2005-05-02 08:55:54 +0200 | [diff] [blame] | 261 | urb->iso_frame_desc[0].length = 3; |
| 262 | urb->iso_frame_desc[0].offset = 0; |
| 263 | cp[0] = subs->freqn >> 2; |
| 264 | cp[1] = subs->freqn >> 10; |
| 265 | cp[2] = subs->freqn >> 18; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | return 0; |
| 267 | } |
| 268 | |
| 269 | /* |
| 270 | * prepare urb for high speed capture sync pipe |
| 271 | * |
| 272 | * fill the length and offset of each urb descriptor. |
| 273 | * the fixed 12.13 frequency is passed as 16.16 through the pipe. |
| 274 | */ |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 275 | static int prepare_capture_sync_urb_hs(struct snd_usb_substream *subs, |
| 276 | struct snd_pcm_runtime *runtime, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | struct urb *urb) |
| 278 | { |
| 279 | unsigned char *cp = urb->transfer_buffer; |
John Daiker | 8851827 | 2006-12-28 13:55:05 +0100 | [diff] [blame] | 280 | struct snd_urb_ctx *ctx = urb->context; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | urb->dev = ctx->subs->dev; /* we need to set this at each time */ |
Clemens Ladisch | ca81090 | 2005-05-02 08:55:54 +0200 | [diff] [blame] | 283 | urb->iso_frame_desc[0].length = 4; |
| 284 | urb->iso_frame_desc[0].offset = 0; |
| 285 | cp[0] = subs->freqn; |
| 286 | cp[1] = subs->freqn >> 8; |
| 287 | cp[2] = subs->freqn >> 16; |
| 288 | cp[3] = subs->freqn >> 24; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | return 0; |
| 290 | } |
| 291 | |
| 292 | /* |
| 293 | * process after capture sync complete |
| 294 | * - nothing to do |
| 295 | */ |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 296 | static int retire_capture_sync_urb(struct snd_usb_substream *subs, |
| 297 | struct snd_pcm_runtime *runtime, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | struct urb *urb) |
| 299 | { |
| 300 | return 0; |
| 301 | } |
| 302 | |
| 303 | /* |
| 304 | * prepare urb for capture data pipe |
| 305 | * |
| 306 | * fill the offset and length of each descriptor. |
| 307 | * |
| 308 | * we use a temporary buffer to write the captured data. |
| 309 | * since the length of written data is determined by host, we cannot |
| 310 | * write onto the pcm buffer directly... the data is thus copied |
| 311 | * later at complete callback to the global buffer. |
| 312 | */ |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 313 | static int prepare_capture_urb(struct snd_usb_substream *subs, |
| 314 | struct snd_pcm_runtime *runtime, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | struct urb *urb) |
| 316 | { |
| 317 | int i, offs; |
John Daiker | 8851827 | 2006-12-28 13:55:05 +0100 | [diff] [blame] | 318 | struct snd_urb_ctx *ctx = urb->context; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | |
| 320 | offs = 0; |
| 321 | urb->dev = ctx->subs->dev; /* we need to set this at each time */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | for (i = 0; i < ctx->packets; i++) { |
| 323 | urb->iso_frame_desc[i].offset = offs; |
| 324 | urb->iso_frame_desc[i].length = subs->curpacksize; |
| 325 | offs += subs->curpacksize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | urb->transfer_buffer_length = offs; |
Clemens Ladisch | b263a9b | 2005-08-15 08:22:39 +0200 | [diff] [blame] | 328 | urb->number_of_packets = ctx->packets; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | return 0; |
| 330 | } |
| 331 | |
| 332 | /* |
| 333 | * process after capture complete |
| 334 | * |
| 335 | * copy the data from each desctiptor to the pcm buffer, and |
| 336 | * update the current position. |
| 337 | */ |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 338 | static int retire_capture_urb(struct snd_usb_substream *subs, |
| 339 | struct snd_pcm_runtime *runtime, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | struct urb *urb) |
| 341 | { |
| 342 | unsigned long flags; |
| 343 | unsigned char *cp; |
| 344 | int i; |
| 345 | unsigned int stride, len, oldptr; |
Clemens Ladisch | b263a9b | 2005-08-15 08:22:39 +0200 | [diff] [blame] | 346 | int period_elapsed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | |
| 348 | stride = runtime->frame_bits >> 3; |
| 349 | |
| 350 | for (i = 0; i < urb->number_of_packets; i++) { |
| 351 | cp = (unsigned char *)urb->transfer_buffer + urb->iso_frame_desc[i].offset; |
| 352 | if (urb->iso_frame_desc[i].status) { |
| 353 | snd_printd(KERN_ERR "frame %d active: %d\n", i, urb->iso_frame_desc[i].status); |
| 354 | // continue; |
| 355 | } |
| 356 | len = urb->iso_frame_desc[i].actual_length / stride; |
| 357 | if (! len) |
| 358 | continue; |
| 359 | /* update the current pointer */ |
| 360 | spin_lock_irqsave(&subs->lock, flags); |
| 361 | oldptr = subs->hwptr_done; |
| 362 | subs->hwptr_done += len; |
| 363 | if (subs->hwptr_done >= runtime->buffer_size) |
| 364 | subs->hwptr_done -= runtime->buffer_size; |
| 365 | subs->transfer_done += len; |
Clemens Ladisch | b263a9b | 2005-08-15 08:22:39 +0200 | [diff] [blame] | 366 | if (subs->transfer_done >= runtime->period_size) { |
| 367 | subs->transfer_done -= runtime->period_size; |
| 368 | period_elapsed = 1; |
| 369 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | spin_unlock_irqrestore(&subs->lock, flags); |
| 371 | /* copy a data chunk */ |
| 372 | if (oldptr + len > runtime->buffer_size) { |
| 373 | unsigned int cnt = runtime->buffer_size - oldptr; |
| 374 | unsigned int blen = cnt * stride; |
| 375 | memcpy(runtime->dma_area + oldptr * stride, cp, blen); |
| 376 | memcpy(runtime->dma_area, cp + blen, len * stride - blen); |
| 377 | } else { |
| 378 | memcpy(runtime->dma_area + oldptr * stride, cp, len * stride); |
| 379 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | } |
Clemens Ladisch | b263a9b | 2005-08-15 08:22:39 +0200 | [diff] [blame] | 381 | if (period_elapsed) |
| 382 | snd_pcm_period_elapsed(subs->pcm_substream); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | return 0; |
| 384 | } |
| 385 | |
Clemens Ladisch | e4f8e65 | 2006-10-04 13:42:57 +0200 | [diff] [blame] | 386 | /* |
| 387 | * Process after capture complete when paused. Nothing to do. |
| 388 | */ |
| 389 | static int retire_paused_capture_urb(struct snd_usb_substream *subs, |
| 390 | struct snd_pcm_runtime *runtime, |
| 391 | struct urb *urb) |
| 392 | { |
| 393 | return 0; |
| 394 | } |
| 395 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | |
| 397 | /* |
| 398 | * prepare urb for full speed playback sync pipe |
| 399 | * |
| 400 | * set up the offset and length to receive the current frequency. |
| 401 | */ |
| 402 | |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 403 | static int prepare_playback_sync_urb(struct snd_usb_substream *subs, |
| 404 | struct snd_pcm_runtime *runtime, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | struct urb *urb) |
| 406 | { |
John Daiker | 8851827 | 2006-12-28 13:55:05 +0100 | [diff] [blame] | 407 | struct snd_urb_ctx *ctx = urb->context; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | urb->dev = ctx->subs->dev; /* we need to set this at each time */ |
Clemens Ladisch | ca81090 | 2005-05-02 08:55:54 +0200 | [diff] [blame] | 410 | urb->iso_frame_desc[0].length = 3; |
| 411 | urb->iso_frame_desc[0].offset = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | return 0; |
| 413 | } |
| 414 | |
| 415 | /* |
| 416 | * prepare urb for high speed playback sync pipe |
| 417 | * |
| 418 | * set up the offset and length to receive the current frequency. |
| 419 | */ |
| 420 | |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 421 | static int prepare_playback_sync_urb_hs(struct snd_usb_substream *subs, |
| 422 | struct snd_pcm_runtime *runtime, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | struct urb *urb) |
| 424 | { |
John Daiker | 8851827 | 2006-12-28 13:55:05 +0100 | [diff] [blame] | 425 | struct snd_urb_ctx *ctx = urb->context; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | urb->dev = ctx->subs->dev; /* we need to set this at each time */ |
Clemens Ladisch | ca81090 | 2005-05-02 08:55:54 +0200 | [diff] [blame] | 428 | urb->iso_frame_desc[0].length = 4; |
| 429 | urb->iso_frame_desc[0].offset = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | return 0; |
| 431 | } |
| 432 | |
| 433 | /* |
| 434 | * process after full speed playback sync complete |
| 435 | * |
| 436 | * retrieve the current 10.14 frequency from pipe, and set it. |
| 437 | * the value is referred in prepare_playback_urb(). |
| 438 | */ |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 439 | static int retire_playback_sync_urb(struct snd_usb_substream *subs, |
| 440 | struct snd_pcm_runtime *runtime, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | struct urb *urb) |
| 442 | { |
Clemens Ladisch | ca81090 | 2005-05-02 08:55:54 +0200 | [diff] [blame] | 443 | unsigned int f; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | unsigned long flags; |
| 445 | |
Clemens Ladisch | ca81090 | 2005-05-02 08:55:54 +0200 | [diff] [blame] | 446 | if (urb->iso_frame_desc[0].status == 0 && |
| 447 | urb->iso_frame_desc[0].actual_length == 3) { |
| 448 | f = combine_triple((u8*)urb->transfer_buffer) << 2; |
Clemens Ladisch | 50cdbf1 | 2005-05-13 07:44:13 +0200 | [diff] [blame] | 449 | if (f >= subs->freqn - subs->freqn / 8 && f <= subs->freqmax) { |
| 450 | spin_lock_irqsave(&subs->lock, flags); |
| 451 | subs->freqm = f; |
| 452 | spin_unlock_irqrestore(&subs->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | return 0; |
| 457 | } |
| 458 | |
| 459 | /* |
| 460 | * process after high speed playback sync complete |
| 461 | * |
| 462 | * retrieve the current 12.13 frequency from pipe, and set it. |
| 463 | * the value is referred in prepare_playback_urb(). |
| 464 | */ |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 465 | static int retire_playback_sync_urb_hs(struct snd_usb_substream *subs, |
| 466 | struct snd_pcm_runtime *runtime, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | struct urb *urb) |
| 468 | { |
Clemens Ladisch | ca81090 | 2005-05-02 08:55:54 +0200 | [diff] [blame] | 469 | unsigned int f; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | unsigned long flags; |
| 471 | |
Clemens Ladisch | ca81090 | 2005-05-02 08:55:54 +0200 | [diff] [blame] | 472 | if (urb->iso_frame_desc[0].status == 0 && |
| 473 | urb->iso_frame_desc[0].actual_length == 4) { |
| 474 | f = combine_quad((u8*)urb->transfer_buffer) & 0x0fffffff; |
Clemens Ladisch | 50cdbf1 | 2005-05-13 07:44:13 +0200 | [diff] [blame] | 475 | if (f >= subs->freqn - subs->freqn / 8 && f <= subs->freqmax) { |
| 476 | spin_lock_irqsave(&subs->lock, flags); |
| 477 | subs->freqm = f; |
| 478 | spin_unlock_irqrestore(&subs->lock, flags); |
| 479 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | return 0; |
| 483 | } |
| 484 | |
Clemens Ladisch | d513202 | 2008-02-25 11:01:00 +0100 | [diff] [blame] | 485 | /* |
Eran Tromer | 97c889a | 2008-09-26 01:07:03 -0400 | [diff] [blame] | 486 | * process after E-Mu 0202/0404/Tracker Pre high speed playback sync complete |
Clemens Ladisch | d513202 | 2008-02-25 11:01:00 +0100 | [diff] [blame] | 487 | * |
| 488 | * These devices return the number of samples per packet instead of the number |
| 489 | * of samples per microframe. |
| 490 | */ |
| 491 | static int retire_playback_sync_urb_hs_emu(struct snd_usb_substream *subs, |
| 492 | struct snd_pcm_runtime *runtime, |
| 493 | struct urb *urb) |
| 494 | { |
| 495 | unsigned int f; |
| 496 | unsigned long flags; |
| 497 | |
| 498 | if (urb->iso_frame_desc[0].status == 0 && |
| 499 | urb->iso_frame_desc[0].actual_length == 4) { |
| 500 | f = combine_quad((u8*)urb->transfer_buffer) & 0x0fffffff; |
| 501 | f >>= subs->datainterval; |
| 502 | if (f >= subs->freqn - subs->freqn / 8 && f <= subs->freqmax) { |
| 503 | spin_lock_irqsave(&subs->lock, flags); |
| 504 | subs->freqm = f; |
| 505 | spin_unlock_irqrestore(&subs->lock, flags); |
| 506 | } |
| 507 | } |
| 508 | |
| 509 | return 0; |
| 510 | } |
| 511 | |
Clemens Ladisch | 9568f46 | 2006-01-12 08:19:21 +0100 | [diff] [blame] | 512 | /* determine the number of frames in the next packet */ |
| 513 | static int snd_usb_audio_next_packet_size(struct snd_usb_substream *subs) |
| 514 | { |
| 515 | if (subs->fill_max) |
| 516 | return subs->maxframesize; |
| 517 | else { |
| 518 | subs->phase = (subs->phase & 0xffff) |
| 519 | + (subs->freqm << subs->datainterval); |
| 520 | return min(subs->phase >> 16, subs->maxframesize); |
| 521 | } |
| 522 | } |
| 523 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | /* |
Clemens Ladisch | e4f8e65 | 2006-10-04 13:42:57 +0200 | [diff] [blame] | 525 | * Prepare urb for streaming before playback starts or when paused. |
Clemens Ladisch | b55bbf0 | 2005-11-02 11:32:52 +0100 | [diff] [blame] | 526 | * |
Clemens Ladisch | b7eb4a0 | 2009-01-26 08:08:34 +0100 | [diff] [blame] | 527 | * We don't have any data, so we send silence. |
Clemens Ladisch | b55bbf0 | 2005-11-02 11:32:52 +0100 | [diff] [blame] | 528 | */ |
Clemens Ladisch | e4f8e65 | 2006-10-04 13:42:57 +0200 | [diff] [blame] | 529 | static int prepare_nodata_playback_urb(struct snd_usb_substream *subs, |
| 530 | struct snd_pcm_runtime *runtime, |
| 531 | struct urb *urb) |
Clemens Ladisch | b55bbf0 | 2005-11-02 11:32:52 +0100 | [diff] [blame] | 532 | { |
Clemens Ladisch | 4b28492 | 2006-01-12 08:17:49 +0100 | [diff] [blame] | 533 | unsigned int i, offs, counts; |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 534 | struct snd_urb_ctx *ctx = urb->context; |
Clemens Ladisch | 4b28492 | 2006-01-12 08:17:49 +0100 | [diff] [blame] | 535 | int stride = runtime->frame_bits >> 3; |
Clemens Ladisch | b55bbf0 | 2005-11-02 11:32:52 +0100 | [diff] [blame] | 536 | |
Clemens Ladisch | 4b28492 | 2006-01-12 08:17:49 +0100 | [diff] [blame] | 537 | offs = 0; |
Clemens Ladisch | b55bbf0 | 2005-11-02 11:32:52 +0100 | [diff] [blame] | 538 | urb->dev = ctx->subs->dev; |
Clemens Ladisch | b7eb4a0 | 2009-01-26 08:08:34 +0100 | [diff] [blame] | 539 | for (i = 0; i < ctx->packets; ++i) { |
Clemens Ladisch | 9568f46 | 2006-01-12 08:19:21 +0100 | [diff] [blame] | 540 | counts = snd_usb_audio_next_packet_size(subs); |
Clemens Ladisch | 4b28492 | 2006-01-12 08:17:49 +0100 | [diff] [blame] | 541 | urb->iso_frame_desc[i].offset = offs * stride; |
| 542 | urb->iso_frame_desc[i].length = counts * stride; |
| 543 | offs += counts; |
Clemens Ladisch | b55bbf0 | 2005-11-02 11:32:52 +0100 | [diff] [blame] | 544 | } |
Clemens Ladisch | b7eb4a0 | 2009-01-26 08:08:34 +0100 | [diff] [blame] | 545 | urb->number_of_packets = ctx->packets; |
Clemens Ladisch | 4b28492 | 2006-01-12 08:17:49 +0100 | [diff] [blame] | 546 | urb->transfer_buffer_length = offs * stride; |
| 547 | memset(urb->transfer_buffer, |
| 548 | subs->cur_audiofmt->format == SNDRV_PCM_FORMAT_U8 ? 0x80 : 0, |
| 549 | offs * stride); |
Clemens Ladisch | b55bbf0 | 2005-11-02 11:32:52 +0100 | [diff] [blame] | 550 | return 0; |
| 551 | } |
| 552 | |
| 553 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 554 | * prepare urb for playback data pipe |
| 555 | * |
Clemens Ladisch | 7efd8bc | 2005-08-15 08:24:44 +0200 | [diff] [blame] | 556 | * Since a URB can handle only a single linear buffer, we must use double |
| 557 | * buffering when the data to be transferred overflows the buffer boundary. |
| 558 | * To avoid inconsistencies when updating hwptr_done, we use double buffering |
| 559 | * for all URBs. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 560 | */ |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 561 | static int prepare_playback_urb(struct snd_usb_substream *subs, |
| 562 | struct snd_pcm_runtime *runtime, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | struct urb *urb) |
| 564 | { |
| 565 | int i, stride, offs; |
| 566 | unsigned int counts; |
| 567 | unsigned long flags; |
Clemens Ladisch | 7efd8bc | 2005-08-15 08:24:44 +0200 | [diff] [blame] | 568 | int period_elapsed = 0; |
John Daiker | 8851827 | 2006-12-28 13:55:05 +0100 | [diff] [blame] | 569 | struct snd_urb_ctx *ctx = urb->context; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 570 | |
| 571 | stride = runtime->frame_bits >> 3; |
| 572 | |
| 573 | offs = 0; |
| 574 | urb->dev = ctx->subs->dev; /* we need to set this at each time */ |
| 575 | urb->number_of_packets = 0; |
| 576 | spin_lock_irqsave(&subs->lock, flags); |
| 577 | for (i = 0; i < ctx->packets; i++) { |
Clemens Ladisch | 9568f46 | 2006-01-12 08:19:21 +0100 | [diff] [blame] | 578 | counts = snd_usb_audio_next_packet_size(subs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 579 | /* set up descriptor */ |
| 580 | urb->iso_frame_desc[i].offset = offs * stride; |
| 581 | urb->iso_frame_desc[i].length = counts * stride; |
| 582 | offs += counts; |
| 583 | urb->number_of_packets++; |
Clemens Ladisch | 7efd8bc | 2005-08-15 08:24:44 +0200 | [diff] [blame] | 584 | subs->transfer_done += counts; |
| 585 | if (subs->transfer_done >= runtime->period_size) { |
| 586 | subs->transfer_done -= runtime->period_size; |
| 587 | period_elapsed = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 588 | if (subs->fmt_type == USB_FORMAT_TYPE_II) { |
Clemens Ladisch | 7efd8bc | 2005-08-15 08:24:44 +0200 | [diff] [blame] | 589 | if (subs->transfer_done > 0) { |
| 590 | /* FIXME: fill-max mode is not |
| 591 | * supported yet */ |
| 592 | offs -= subs->transfer_done; |
| 593 | counts -= subs->transfer_done; |
| 594 | urb->iso_frame_desc[i].length = |
| 595 | counts * stride; |
| 596 | subs->transfer_done = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 597 | } |
| 598 | i++; |
| 599 | if (i < ctx->packets) { |
| 600 | /* add a transfer delimiter */ |
Clemens Ladisch | 7efd8bc | 2005-08-15 08:24:44 +0200 | [diff] [blame] | 601 | urb->iso_frame_desc[i].offset = |
| 602 | offs * stride; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 603 | urb->iso_frame_desc[i].length = 0; |
| 604 | urb->number_of_packets++; |
| 605 | } |
Clemens Ladisch | 9624ea8 | 2005-08-15 08:25:24 +0200 | [diff] [blame] | 606 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 608 | } |
Clemens Ladisch | a7d9c09 | 2009-04-03 09:48:26 +0200 | [diff] [blame] | 609 | if (period_elapsed) /* finish at the period boundary */ |
Clemens Ladisch | 9624ea8 | 2005-08-15 08:25:24 +0200 | [diff] [blame] | 610 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 611 | } |
Clemens Ladisch | 7efd8bc | 2005-08-15 08:24:44 +0200 | [diff] [blame] | 612 | if (subs->hwptr_done + offs > runtime->buffer_size) { |
| 613 | /* err, the transferred area goes over buffer boundary. */ |
| 614 | unsigned int len = runtime->buffer_size - subs->hwptr_done; |
| 615 | memcpy(urb->transfer_buffer, |
| 616 | runtime->dma_area + subs->hwptr_done * stride, |
| 617 | len * stride); |
| 618 | memcpy(urb->transfer_buffer + len * stride, |
| 619 | runtime->dma_area, |
| 620 | (offs - len) * stride); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 | } else { |
Clemens Ladisch | 7efd8bc | 2005-08-15 08:24:44 +0200 | [diff] [blame] | 622 | memcpy(urb->transfer_buffer, |
| 623 | runtime->dma_area + subs->hwptr_done * stride, |
| 624 | offs * stride); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | } |
Clemens Ladisch | 7efd8bc | 2005-08-15 08:24:44 +0200 | [diff] [blame] | 626 | subs->hwptr_done += offs; |
| 627 | if (subs->hwptr_done >= runtime->buffer_size) |
| 628 | subs->hwptr_done -= runtime->buffer_size; |
Takashi Iwai | ae1ec5e | 2008-10-13 03:08:53 +0200 | [diff] [blame] | 629 | runtime->delay += offs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 630 | spin_unlock_irqrestore(&subs->lock, flags); |
| 631 | urb->transfer_buffer_length = offs * stride; |
Clemens Ladisch | b55bbf0 | 2005-11-02 11:32:52 +0100 | [diff] [blame] | 632 | if (period_elapsed) |
| 633 | snd_pcm_period_elapsed(subs->pcm_substream); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 634 | return 0; |
| 635 | } |
| 636 | |
| 637 | /* |
| 638 | * process after playback data complete |
Takashi Iwai | ae1ec5e | 2008-10-13 03:08:53 +0200 | [diff] [blame] | 639 | * - decrease the delay count again |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 640 | */ |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 641 | static int retire_playback_urb(struct snd_usb_substream *subs, |
| 642 | struct snd_pcm_runtime *runtime, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | struct urb *urb) |
| 644 | { |
Takashi Iwai | ae1ec5e | 2008-10-13 03:08:53 +0200 | [diff] [blame] | 645 | unsigned long flags; |
| 646 | int stride = runtime->frame_bits >> 3; |
| 647 | int processed = urb->transfer_buffer_length / stride; |
| 648 | |
| 649 | spin_lock_irqsave(&subs->lock, flags); |
| 650 | if (processed > runtime->delay) |
| 651 | runtime->delay = 0; |
| 652 | else |
| 653 | runtime->delay -= processed; |
| 654 | spin_unlock_irqrestore(&subs->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 655 | return 0; |
| 656 | } |
| 657 | |
| 658 | |
| 659 | /* |
| 660 | */ |
| 661 | static struct snd_urb_ops audio_urb_ops[2] = { |
| 662 | { |
Clemens Ladisch | e4f8e65 | 2006-10-04 13:42:57 +0200 | [diff] [blame] | 663 | .prepare = prepare_nodata_playback_urb, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | .retire = retire_playback_urb, |
| 665 | .prepare_sync = prepare_playback_sync_urb, |
| 666 | .retire_sync = retire_playback_sync_urb, |
| 667 | }, |
| 668 | { |
| 669 | .prepare = prepare_capture_urb, |
| 670 | .retire = retire_capture_urb, |
| 671 | .prepare_sync = prepare_capture_sync_urb, |
| 672 | .retire_sync = retire_capture_sync_urb, |
| 673 | }, |
| 674 | }; |
| 675 | |
| 676 | static struct snd_urb_ops audio_urb_ops_high_speed[2] = { |
| 677 | { |
Clemens Ladisch | e4f8e65 | 2006-10-04 13:42:57 +0200 | [diff] [blame] | 678 | .prepare = prepare_nodata_playback_urb, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 679 | .retire = retire_playback_urb, |
| 680 | .prepare_sync = prepare_playback_sync_urb_hs, |
| 681 | .retire_sync = retire_playback_sync_urb_hs, |
| 682 | }, |
| 683 | { |
| 684 | .prepare = prepare_capture_urb, |
| 685 | .retire = retire_capture_urb, |
| 686 | .prepare_sync = prepare_capture_sync_urb_hs, |
| 687 | .retire_sync = retire_capture_sync_urb, |
| 688 | }, |
| 689 | }; |
| 690 | |
| 691 | /* |
| 692 | * complete callback from data urb |
| 693 | */ |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 694 | static void snd_complete_urb(struct urb *urb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 695 | { |
John Daiker | 8851827 | 2006-12-28 13:55:05 +0100 | [diff] [blame] | 696 | struct snd_urb_ctx *ctx = urb->context; |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 697 | struct snd_usb_substream *subs = ctx->subs; |
| 698 | struct snd_pcm_substream *substream = ctx->subs->pcm_substream; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 699 | int err = 0; |
| 700 | |
| 701 | if ((subs->running && subs->ops.retire(subs, substream->runtime, urb)) || |
Pavel Machek | 07f51a7 | 2008-04-14 13:15:56 +0200 | [diff] [blame] | 702 | !subs->running || /* can be stopped during retire callback */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 | (err = subs->ops.prepare(subs, substream->runtime, urb)) < 0 || |
| 704 | (err = usb_submit_urb(urb, GFP_ATOMIC)) < 0) { |
| 705 | clear_bit(ctx->index, &subs->active_mask); |
| 706 | if (err < 0) { |
| 707 | snd_printd(KERN_ERR "cannot submit urb (err = %d)\n", err); |
| 708 | snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN); |
| 709 | } |
| 710 | } |
| 711 | } |
| 712 | |
| 713 | |
| 714 | /* |
| 715 | * complete callback from sync urb |
| 716 | */ |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 717 | static void snd_complete_sync_urb(struct urb *urb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 718 | { |
John Daiker | 8851827 | 2006-12-28 13:55:05 +0100 | [diff] [blame] | 719 | struct snd_urb_ctx *ctx = urb->context; |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 720 | struct snd_usb_substream *subs = ctx->subs; |
| 721 | struct snd_pcm_substream *substream = ctx->subs->pcm_substream; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 722 | int err = 0; |
| 723 | |
| 724 | if ((subs->running && subs->ops.retire_sync(subs, substream->runtime, urb)) || |
Pavel Machek | 07f51a7 | 2008-04-14 13:15:56 +0200 | [diff] [blame] | 725 | !subs->running || /* can be stopped during retire callback */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 726 | (err = subs->ops.prepare_sync(subs, substream->runtime, urb)) < 0 || |
| 727 | (err = usb_submit_urb(urb, GFP_ATOMIC)) < 0) { |
| 728 | clear_bit(ctx->index + 16, &subs->active_mask); |
| 729 | if (err < 0) { |
| 730 | snd_printd(KERN_ERR "cannot submit sync urb (err = %d)\n", err); |
| 731 | snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN); |
| 732 | } |
| 733 | } |
| 734 | } |
| 735 | |
| 736 | |
| 737 | /* |
| 738 | * unlink active urbs. |
| 739 | */ |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 740 | static int deactivate_urbs(struct snd_usb_substream *subs, int force, int can_sleep) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 741 | { |
| 742 | unsigned int i; |
| 743 | int async; |
| 744 | |
| 745 | subs->running = 0; |
| 746 | |
| 747 | if (!force && subs->stream->chip->shutdown) /* to be sure... */ |
| 748 | return -EBADFD; |
| 749 | |
| 750 | async = !can_sleep && async_unlink; |
| 751 | |
Pavel Machek | 07f51a7 | 2008-04-14 13:15:56 +0200 | [diff] [blame] | 752 | if (!async && in_interrupt()) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 753 | return 0; |
| 754 | |
| 755 | for (i = 0; i < subs->nurbs; i++) { |
| 756 | if (test_bit(i, &subs->active_mask)) { |
Pavel Machek | 07f51a7 | 2008-04-14 13:15:56 +0200 | [diff] [blame] | 757 | if (!test_and_set_bit(i, &subs->unlink_mask)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 758 | struct urb *u = subs->dataurb[i].urb; |
Alan Stern | b375a04 | 2005-07-29 16:11:07 -0400 | [diff] [blame] | 759 | if (async) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 760 | usb_unlink_urb(u); |
Alan Stern | b375a04 | 2005-07-29 16:11:07 -0400 | [diff] [blame] | 761 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 | usb_kill_urb(u); |
| 763 | } |
| 764 | } |
| 765 | } |
| 766 | if (subs->syncpipe) { |
| 767 | for (i = 0; i < SYNC_URBS; i++) { |
| 768 | if (test_bit(i+16, &subs->active_mask)) { |
Pavel Machek | 07f51a7 | 2008-04-14 13:15:56 +0200 | [diff] [blame] | 769 | if (!test_and_set_bit(i+16, &subs->unlink_mask)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 770 | struct urb *u = subs->syncurb[i].urb; |
Alan Stern | b375a04 | 2005-07-29 16:11:07 -0400 | [diff] [blame] | 771 | if (async) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 772 | usb_unlink_urb(u); |
Alan Stern | b375a04 | 2005-07-29 16:11:07 -0400 | [diff] [blame] | 773 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 774 | usb_kill_urb(u); |
| 775 | } |
| 776 | } |
| 777 | } |
| 778 | } |
| 779 | return 0; |
| 780 | } |
| 781 | |
| 782 | |
Clemens Ladisch | 32e19e8 | 2006-03-09 07:58:39 +0100 | [diff] [blame] | 783 | static const char *usb_error_string(int err) |
| 784 | { |
| 785 | switch (err) { |
| 786 | case -ENODEV: |
| 787 | return "no device"; |
| 788 | case -ENOENT: |
| 789 | return "endpoint not enabled"; |
| 790 | case -EPIPE: |
| 791 | return "endpoint stalled"; |
| 792 | case -ENOSPC: |
| 793 | return "not enough bandwidth"; |
| 794 | case -ESHUTDOWN: |
| 795 | return "device disabled"; |
| 796 | case -EHOSTUNREACH: |
| 797 | return "device suspended"; |
| 798 | case -EINVAL: |
| 799 | case -EAGAIN: |
| 800 | case -EFBIG: |
| 801 | case -EMSGSIZE: |
| 802 | return "internal error"; |
| 803 | default: |
| 804 | return "unknown error"; |
| 805 | } |
| 806 | } |
| 807 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 808 | /* |
| 809 | * set up and start data/sync urbs |
| 810 | */ |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 811 | static int start_urbs(struct snd_usb_substream *subs, struct snd_pcm_runtime *runtime) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 812 | { |
| 813 | unsigned int i; |
| 814 | int err; |
| 815 | |
| 816 | if (subs->stream->chip->shutdown) |
| 817 | return -EBADFD; |
| 818 | |
| 819 | for (i = 0; i < subs->nurbs; i++) { |
Takashi Iwai | 5e246b8 | 2008-08-08 17:12:47 +0200 | [diff] [blame] | 820 | if (snd_BUG_ON(!subs->dataurb[i].urb)) |
| 821 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 822 | if (subs->ops.prepare(subs, runtime, subs->dataurb[i].urb) < 0) { |
| 823 | snd_printk(KERN_ERR "cannot prepare datapipe for urb %d\n", i); |
| 824 | goto __error; |
| 825 | } |
| 826 | } |
| 827 | if (subs->syncpipe) { |
| 828 | for (i = 0; i < SYNC_URBS; i++) { |
Takashi Iwai | 5e246b8 | 2008-08-08 17:12:47 +0200 | [diff] [blame] | 829 | if (snd_BUG_ON(!subs->syncurb[i].urb)) |
| 830 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 831 | if (subs->ops.prepare_sync(subs, runtime, subs->syncurb[i].urb) < 0) { |
| 832 | snd_printk(KERN_ERR "cannot prepare syncpipe for urb %d\n", i); |
| 833 | goto __error; |
| 834 | } |
| 835 | } |
| 836 | } |
| 837 | |
| 838 | subs->active_mask = 0; |
| 839 | subs->unlink_mask = 0; |
| 840 | subs->running = 1; |
| 841 | for (i = 0; i < subs->nurbs; i++) { |
Clemens Ladisch | 32e19e8 | 2006-03-09 07:58:39 +0100 | [diff] [blame] | 842 | err = usb_submit_urb(subs->dataurb[i].urb, GFP_ATOMIC); |
| 843 | if (err < 0) { |
| 844 | snd_printk(KERN_ERR "cannot submit datapipe " |
| 845 | "for urb %d, error %d: %s\n", |
| 846 | i, err, usb_error_string(err)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 847 | goto __error; |
| 848 | } |
| 849 | set_bit(i, &subs->active_mask); |
| 850 | } |
| 851 | if (subs->syncpipe) { |
| 852 | for (i = 0; i < SYNC_URBS; i++) { |
Clemens Ladisch | 32e19e8 | 2006-03-09 07:58:39 +0100 | [diff] [blame] | 853 | err = usb_submit_urb(subs->syncurb[i].urb, GFP_ATOMIC); |
| 854 | if (err < 0) { |
| 855 | snd_printk(KERN_ERR "cannot submit syncpipe " |
| 856 | "for urb %d, error %d: %s\n", |
| 857 | i, err, usb_error_string(err)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 858 | goto __error; |
| 859 | } |
| 860 | set_bit(i + 16, &subs->active_mask); |
| 861 | } |
| 862 | } |
| 863 | return 0; |
| 864 | |
| 865 | __error: |
| 866 | // snd_pcm_stop(subs->pcm_substream, SNDRV_PCM_STATE_XRUN); |
| 867 | deactivate_urbs(subs, 0, 0); |
| 868 | return -EPIPE; |
| 869 | } |
| 870 | |
| 871 | |
| 872 | /* |
| 873 | * wait until all urbs are processed. |
| 874 | */ |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 875 | static int wait_clear_urbs(struct snd_usb_substream *subs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 876 | { |
Nishanth Aravamudan | b27c187 | 2005-07-09 10:54:37 +0200 | [diff] [blame] | 877 | unsigned long end_time = jiffies + msecs_to_jiffies(1000); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 878 | unsigned int i; |
| 879 | int alive; |
| 880 | |
| 881 | do { |
| 882 | alive = 0; |
| 883 | for (i = 0; i < subs->nurbs; i++) { |
| 884 | if (test_bit(i, &subs->active_mask)) |
| 885 | alive++; |
| 886 | } |
| 887 | if (subs->syncpipe) { |
| 888 | for (i = 0; i < SYNC_URBS; i++) { |
| 889 | if (test_bit(i + 16, &subs->active_mask)) |
| 890 | alive++; |
| 891 | } |
| 892 | } |
| 893 | if (! alive) |
| 894 | break; |
Nishanth Aravamudan | 8433a50 | 2005-10-24 15:02:37 +0200 | [diff] [blame] | 895 | schedule_timeout_uninterruptible(1); |
Nishanth Aravamudan | b27c187 | 2005-07-09 10:54:37 +0200 | [diff] [blame] | 896 | } while (time_before(jiffies, end_time)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 897 | if (alive) |
| 898 | snd_printk(KERN_ERR "timeout: still %d active urbs..\n", alive); |
| 899 | return 0; |
| 900 | } |
| 901 | |
| 902 | |
| 903 | /* |
| 904 | * return the current pcm pointer. just return the hwptr_done value. |
| 905 | */ |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 906 | static snd_pcm_uframes_t snd_usb_pcm_pointer(struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 907 | { |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 908 | struct snd_usb_substream *subs; |
Clemens Ladisch | daa150e | 2005-08-15 08:25:50 +0200 | [diff] [blame] | 909 | snd_pcm_uframes_t hwptr_done; |
| 910 | |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 911 | subs = (struct snd_usb_substream *)substream->runtime->private_data; |
Clemens Ladisch | daa150e | 2005-08-15 08:25:50 +0200 | [diff] [blame] | 912 | spin_lock(&subs->lock); |
| 913 | hwptr_done = subs->hwptr_done; |
| 914 | spin_unlock(&subs->lock); |
| 915 | return hwptr_done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 916 | } |
| 917 | |
| 918 | |
| 919 | /* |
Clemens Ladisch | b55bbf0 | 2005-11-02 11:32:52 +0100 | [diff] [blame] | 920 | * start/stop playback substream |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 921 | */ |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 922 | static int snd_usb_pcm_playback_trigger(struct snd_pcm_substream *substream, |
Clemens Ladisch | b55bbf0 | 2005-11-02 11:32:52 +0100 | [diff] [blame] | 923 | int cmd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 924 | { |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 925 | struct snd_usb_substream *subs = substream->runtime->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 926 | |
| 927 | switch (cmd) { |
| 928 | case SNDRV_PCM_TRIGGER_START: |
Clemens Ladisch | e4f8e65 | 2006-10-04 13:42:57 +0200 | [diff] [blame] | 929 | case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: |
Clemens Ladisch | b55bbf0 | 2005-11-02 11:32:52 +0100 | [diff] [blame] | 930 | subs->ops.prepare = prepare_playback_urb; |
| 931 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 932 | case SNDRV_PCM_TRIGGER_STOP: |
Clemens Ladisch | b55bbf0 | 2005-11-02 11:32:52 +0100 | [diff] [blame] | 933 | return deactivate_urbs(subs, 0, 0); |
Clemens Ladisch | e4f8e65 | 2006-10-04 13:42:57 +0200 | [diff] [blame] | 934 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: |
| 935 | subs->ops.prepare = prepare_nodata_playback_urb; |
| 936 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 937 | default: |
Clemens Ladisch | b55bbf0 | 2005-11-02 11:32:52 +0100 | [diff] [blame] | 938 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 939 | } |
Clemens Ladisch | b55bbf0 | 2005-11-02 11:32:52 +0100 | [diff] [blame] | 940 | } |
| 941 | |
| 942 | /* |
| 943 | * start/stop capture substream |
| 944 | */ |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 945 | static int snd_usb_pcm_capture_trigger(struct snd_pcm_substream *substream, |
Clemens Ladisch | b55bbf0 | 2005-11-02 11:32:52 +0100 | [diff] [blame] | 946 | int cmd) |
| 947 | { |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 948 | struct snd_usb_substream *subs = substream->runtime->private_data; |
Clemens Ladisch | b55bbf0 | 2005-11-02 11:32:52 +0100 | [diff] [blame] | 949 | |
| 950 | switch (cmd) { |
| 951 | case SNDRV_PCM_TRIGGER_START: |
Clemens Ladisch | e4f8e65 | 2006-10-04 13:42:57 +0200 | [diff] [blame] | 952 | subs->ops.retire = retire_capture_urb; |
Clemens Ladisch | b55bbf0 | 2005-11-02 11:32:52 +0100 | [diff] [blame] | 953 | return start_urbs(subs, substream->runtime); |
| 954 | case SNDRV_PCM_TRIGGER_STOP: |
| 955 | return deactivate_urbs(subs, 0, 0); |
Clemens Ladisch | e4f8e65 | 2006-10-04 13:42:57 +0200 | [diff] [blame] | 956 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: |
| 957 | subs->ops.retire = retire_paused_capture_urb; |
| 958 | return 0; |
| 959 | case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: |
| 960 | subs->ops.retire = retire_capture_urb; |
| 961 | return 0; |
Clemens Ladisch | b55bbf0 | 2005-11-02 11:32:52 +0100 | [diff] [blame] | 962 | default: |
| 963 | return -EINVAL; |
| 964 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 965 | } |
| 966 | |
| 967 | |
| 968 | /* |
| 969 | * release a urb data |
| 970 | */ |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 971 | static void release_urb_ctx(struct snd_urb_ctx *u) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 972 | { |
| 973 | if (u->urb) { |
Clemens Ladisch | 55851f7 | 2005-08-15 08:34:16 +0200 | [diff] [blame] | 974 | if (u->buffer_size) |
| 975 | usb_buffer_free(u->subs->dev, u->buffer_size, |
| 976 | u->urb->transfer_buffer, |
| 977 | u->urb->transfer_dma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 978 | usb_free_urb(u->urb); |
| 979 | u->urb = NULL; |
| 980 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 981 | } |
| 982 | |
| 983 | /* |
| 984 | * release a substream |
| 985 | */ |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 986 | static void release_substream_urbs(struct snd_usb_substream *subs, int force) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 987 | { |
| 988 | int i; |
| 989 | |
| 990 | /* stop urbs (to be sure) */ |
| 991 | deactivate_urbs(subs, force, 1); |
| 992 | wait_clear_urbs(subs); |
| 993 | |
| 994 | for (i = 0; i < MAX_URBS; i++) |
| 995 | release_urb_ctx(&subs->dataurb[i]); |
| 996 | for (i = 0; i < SYNC_URBS; i++) |
| 997 | release_urb_ctx(&subs->syncurb[i]); |
Clemens Ladisch | 55851f7 | 2005-08-15 08:34:16 +0200 | [diff] [blame] | 998 | usb_buffer_free(subs->dev, SYNC_URBS * 4, |
| 999 | subs->syncbuf, subs->sync_dma); |
| 1000 | subs->syncbuf = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1001 | subs->nurbs = 0; |
| 1002 | } |
| 1003 | |
| 1004 | /* |
| 1005 | * initialize a substream for plaback/capture |
| 1006 | */ |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 1007 | static int init_substream_urbs(struct snd_usb_substream *subs, unsigned int period_bytes, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1008 | unsigned int rate, unsigned int frame_bits) |
| 1009 | { |
Clemens Ladisch | 160389c | 2009-01-26 08:10:19 +0100 | [diff] [blame] | 1010 | unsigned int maxsize, i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1011 | int is_playback = subs->direction == SNDRV_PCM_STREAM_PLAYBACK; |
Clemens Ladisch | 160389c | 2009-01-26 08:10:19 +0100 | [diff] [blame] | 1012 | unsigned int urb_packs, total_packs, packs_per_ms; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1013 | |
| 1014 | /* calculate the frequency in 16.16 format */ |
| 1015 | if (snd_usb_get_speed(subs->dev) == USB_SPEED_FULL) |
| 1016 | subs->freqn = get_usb_full_speed_rate(rate); |
| 1017 | else |
| 1018 | subs->freqn = get_usb_high_speed_rate(rate); |
| 1019 | subs->freqm = subs->freqn; |
Clemens Ladisch | 573567e | 2005-06-27 08:17:30 +0200 | [diff] [blame] | 1020 | /* calculate max. frequency */ |
| 1021 | if (subs->maxpacksize) { |
| 1022 | /* whatever fits into a max. size packet */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1023 | maxsize = subs->maxpacksize; |
Clemens Ladisch | 573567e | 2005-06-27 08:17:30 +0200 | [diff] [blame] | 1024 | subs->freqmax = (maxsize / (frame_bits >> 3)) |
| 1025 | << (16 - subs->datainterval); |
| 1026 | } else { |
| 1027 | /* no max. packet size: just take 25% higher than nominal */ |
| 1028 | subs->freqmax = subs->freqn + (subs->freqn >> 2); |
| 1029 | maxsize = ((subs->freqmax + 0xffff) * (frame_bits >> 3)) |
| 1030 | >> (16 - subs->datainterval); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1031 | } |
Clemens Ladisch | 573567e | 2005-06-27 08:17:30 +0200 | [diff] [blame] | 1032 | subs->phase = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1033 | |
| 1034 | if (subs->fill_max) |
| 1035 | subs->curpacksize = subs->maxpacksize; |
| 1036 | else |
| 1037 | subs->curpacksize = maxsize; |
| 1038 | |
Clemens Ladisch | a93bf99 | 2005-08-12 15:19:39 +0200 | [diff] [blame] | 1039 | if (snd_usb_get_speed(subs->dev) == USB_SPEED_HIGH) |
| 1040 | packs_per_ms = 8 >> subs->datainterval; |
| 1041 | else |
| 1042 | packs_per_ms = 1; |
| 1043 | |
Clemens Ladisch | 71d848c | 2005-08-12 15:18:00 +0200 | [diff] [blame] | 1044 | if (is_playback) { |
Clemens Ladisch | f3990e6 | 2009-02-20 09:32:40 +0100 | [diff] [blame] | 1045 | urb_packs = max(nrpacks, 1); |
Clemens Ladisch | 71d848c | 2005-08-12 15:18:00 +0200 | [diff] [blame] | 1046 | urb_packs = min(urb_packs, (unsigned int)MAX_PACKS); |
| 1047 | } else |
Clemens Ladisch | 15a24c0 | 2005-08-12 08:25:26 +0200 | [diff] [blame] | 1048 | urb_packs = 1; |
Clemens Ladisch | a93bf99 | 2005-08-12 15:19:39 +0200 | [diff] [blame] | 1049 | urb_packs *= packs_per_ms; |
Clemens Ladisch | 765e8db | 2009-08-10 10:07:35 +0200 | [diff] [blame] | 1050 | if (subs->syncpipe) |
Takashi Iwai | f1e6d3c | 2009-08-11 08:15:04 +0200 | [diff] [blame] | 1051 | urb_packs = min(urb_packs, 1U << subs->syncinterval); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1052 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1053 | /* decide how many packets to be used */ |
Clemens Ladisch | 15a24c0 | 2005-08-12 08:25:26 +0200 | [diff] [blame] | 1054 | if (is_playback) { |
Clemens Ladisch | 4d788e0 | 2009-01-26 08:09:28 +0100 | [diff] [blame] | 1055 | unsigned int minsize, maxpacks; |
Clemens Ladisch | d6db392 | 2005-08-12 08:28:27 +0200 | [diff] [blame] | 1056 | /* determine how small a packet can be */ |
| 1057 | minsize = (subs->freqn >> (16 - subs->datainterval)) |
| 1058 | * (frame_bits >> 3); |
Clemens Ladisch | 7efd8bc | 2005-08-15 08:24:44 +0200 | [diff] [blame] | 1059 | /* with sync from device, assume it can be 12% lower */ |
Clemens Ladisch | d6db392 | 2005-08-12 08:28:27 +0200 | [diff] [blame] | 1060 | if (subs->syncpipe) |
Clemens Ladisch | 7efd8bc | 2005-08-15 08:24:44 +0200 | [diff] [blame] | 1061 | minsize -= minsize >> 3; |
Clemens Ladisch | d6db392 | 2005-08-12 08:28:27 +0200 | [diff] [blame] | 1062 | minsize = max(minsize, 1u); |
| 1063 | total_packs = (period_bytes + minsize - 1) / minsize; |
Clemens Ladisch | a93bf99 | 2005-08-12 15:19:39 +0200 | [diff] [blame] | 1064 | /* we need at least two URBs for queueing */ |
Clemens Ladisch | a7d9c09 | 2009-04-03 09:48:26 +0200 | [diff] [blame] | 1065 | if (total_packs < 2) { |
| 1066 | total_packs = 2; |
Clemens Ladisch | f3990e6 | 2009-02-20 09:32:40 +0100 | [diff] [blame] | 1067 | } else { |
Clemens Ladisch | 4d788e0 | 2009-01-26 08:09:28 +0100 | [diff] [blame] | 1068 | /* and we don't want too long a queue either */ |
Clemens Ladisch | b1c86bb | 2009-03-02 12:06:28 +0100 | [diff] [blame] | 1069 | maxpacks = max(MAX_QUEUE * packs_per_ms, urb_packs * 2); |
| 1070 | total_packs = min(total_packs, maxpacks); |
Clemens Ladisch | 4d788e0 | 2009-01-26 08:09:28 +0100 | [diff] [blame] | 1071 | } |
Clemens Ladisch | 15a24c0 | 2005-08-12 08:25:26 +0200 | [diff] [blame] | 1072 | } else { |
Clemens Ladisch | a7d9c09 | 2009-04-03 09:48:26 +0200 | [diff] [blame] | 1073 | while (urb_packs > 1 && urb_packs * maxsize >= period_bytes) |
| 1074 | urb_packs >>= 1; |
Clemens Ladisch | 15a24c0 | 2005-08-12 08:25:26 +0200 | [diff] [blame] | 1075 | total_packs = MAX_URBS * urb_packs; |
| 1076 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1077 | subs->nurbs = (total_packs + urb_packs - 1) / urb_packs; |
| 1078 | if (subs->nurbs > MAX_URBS) { |
| 1079 | /* too much... */ |
| 1080 | subs->nurbs = MAX_URBS; |
| 1081 | total_packs = MAX_URBS * urb_packs; |
Clemens Ladisch | 160389c | 2009-01-26 08:10:19 +0100 | [diff] [blame] | 1082 | } else if (subs->nurbs < 2) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1083 | /* too little - we need at least two packets |
| 1084 | * to ensure contiguous playback/capture |
| 1085 | */ |
| 1086 | subs->nurbs = 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1087 | } |
| 1088 | |
| 1089 | /* allocate and initialize data urbs */ |
| 1090 | for (i = 0; i < subs->nurbs; i++) { |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 1091 | struct snd_urb_ctx *u = &subs->dataurb[i]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1092 | u->index = i; |
| 1093 | u->subs = subs; |
Clemens Ladisch | 160389c | 2009-01-26 08:10:19 +0100 | [diff] [blame] | 1094 | u->packets = (i + 1) * total_packs / subs->nurbs |
| 1095 | - i * total_packs / subs->nurbs; |
Clemens Ladisch | 55851f7 | 2005-08-15 08:34:16 +0200 | [diff] [blame] | 1096 | u->buffer_size = maxsize * u->packets; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1097 | if (subs->fmt_type == USB_FORMAT_TYPE_II) |
| 1098 | u->packets++; /* for transfer delimiter */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1099 | u->urb = usb_alloc_urb(u->packets, GFP_KERNEL); |
Pavel Machek | 07f51a7 | 2008-04-14 13:15:56 +0200 | [diff] [blame] | 1100 | if (!u->urb) |
Clemens Ladisch | 55851f7 | 2005-08-15 08:34:16 +0200 | [diff] [blame] | 1101 | goto out_of_memory; |
| 1102 | u->urb->transfer_buffer = |
| 1103 | usb_buffer_alloc(subs->dev, u->buffer_size, GFP_KERNEL, |
| 1104 | &u->urb->transfer_dma); |
Pavel Machek | 07f51a7 | 2008-04-14 13:15:56 +0200 | [diff] [blame] | 1105 | if (!u->urb->transfer_buffer) |
Clemens Ladisch | 55851f7 | 2005-08-15 08:34:16 +0200 | [diff] [blame] | 1106 | goto out_of_memory; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1107 | u->urb->pipe = subs->datapipe; |
Clemens Ladisch | 55851f7 | 2005-08-15 08:34:16 +0200 | [diff] [blame] | 1108 | u->urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP; |
Clemens Ladisch | 573567e | 2005-06-27 08:17:30 +0200 | [diff] [blame] | 1109 | u->urb->interval = 1 << subs->datainterval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1110 | u->urb->context = u; |
Clemens Ladisch | 3527a00 | 2005-09-26 10:03:09 +0200 | [diff] [blame] | 1111 | u->urb->complete = snd_complete_urb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1112 | } |
| 1113 | |
| 1114 | if (subs->syncpipe) { |
| 1115 | /* allocate and initialize sync urbs */ |
Clemens Ladisch | 55851f7 | 2005-08-15 08:34:16 +0200 | [diff] [blame] | 1116 | subs->syncbuf = usb_buffer_alloc(subs->dev, SYNC_URBS * 4, |
| 1117 | GFP_KERNEL, &subs->sync_dma); |
Pavel Machek | 07f51a7 | 2008-04-14 13:15:56 +0200 | [diff] [blame] | 1118 | if (!subs->syncbuf) |
Clemens Ladisch | 55851f7 | 2005-08-15 08:34:16 +0200 | [diff] [blame] | 1119 | goto out_of_memory; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1120 | for (i = 0; i < SYNC_URBS; i++) { |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 1121 | struct snd_urb_ctx *u = &subs->syncurb[i]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1122 | u->index = i; |
| 1123 | u->subs = subs; |
Clemens Ladisch | ca81090 | 2005-05-02 08:55:54 +0200 | [diff] [blame] | 1124 | u->packets = 1; |
| 1125 | u->urb = usb_alloc_urb(1, GFP_KERNEL); |
Pavel Machek | 07f51a7 | 2008-04-14 13:15:56 +0200 | [diff] [blame] | 1126 | if (!u->urb) |
Clemens Ladisch | 55851f7 | 2005-08-15 08:34:16 +0200 | [diff] [blame] | 1127 | goto out_of_memory; |
Clemens Ladisch | ca81090 | 2005-05-02 08:55:54 +0200 | [diff] [blame] | 1128 | u->urb->transfer_buffer = subs->syncbuf + i * 4; |
Clemens Ladisch | 55851f7 | 2005-08-15 08:34:16 +0200 | [diff] [blame] | 1129 | u->urb->transfer_dma = subs->sync_dma + i * 4; |
Clemens Ladisch | ca81090 | 2005-05-02 08:55:54 +0200 | [diff] [blame] | 1130 | u->urb->transfer_buffer_length = 4; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1131 | u->urb->pipe = subs->syncpipe; |
Clemens Ladisch | 55851f7 | 2005-08-15 08:34:16 +0200 | [diff] [blame] | 1132 | u->urb->transfer_flags = URB_ISO_ASAP | |
| 1133 | URB_NO_TRANSFER_DMA_MAP; |
Clemens Ladisch | ca81090 | 2005-05-02 08:55:54 +0200 | [diff] [blame] | 1134 | u->urb->number_of_packets = 1; |
Clemens Ladisch | 1149a64 | 2005-05-02 08:53:46 +0200 | [diff] [blame] | 1135 | u->urb->interval = 1 << subs->syncinterval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1136 | u->urb->context = u; |
Clemens Ladisch | 3527a00 | 2005-09-26 10:03:09 +0200 | [diff] [blame] | 1137 | u->urb->complete = snd_complete_sync_urb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1138 | } |
| 1139 | } |
| 1140 | return 0; |
Clemens Ladisch | 55851f7 | 2005-08-15 08:34:16 +0200 | [diff] [blame] | 1141 | |
| 1142 | out_of_memory: |
| 1143 | release_substream_urbs(subs, 0); |
| 1144 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1145 | } |
| 1146 | |
| 1147 | |
| 1148 | /* |
| 1149 | * find a matching audio format |
| 1150 | */ |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 1151 | static struct audioformat *find_format(struct snd_usb_substream *subs, unsigned int format, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1152 | unsigned int rate, unsigned int channels) |
| 1153 | { |
| 1154 | struct list_head *p; |
| 1155 | struct audioformat *found = NULL; |
| 1156 | int cur_attr = 0, attr; |
| 1157 | |
| 1158 | list_for_each(p, &subs->fmt_list) { |
| 1159 | struct audioformat *fp; |
| 1160 | fp = list_entry(p, struct audioformat, list); |
| 1161 | if (fp->format != format || fp->channels != channels) |
| 1162 | continue; |
| 1163 | if (rate < fp->rate_min || rate > fp->rate_max) |
| 1164 | continue; |
| 1165 | if (! (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)) { |
| 1166 | unsigned int i; |
| 1167 | for (i = 0; i < fp->nr_rates; i++) |
| 1168 | if (fp->rate_table[i] == rate) |
| 1169 | break; |
| 1170 | if (i >= fp->nr_rates) |
| 1171 | continue; |
| 1172 | } |
| 1173 | attr = fp->ep_attr & EP_ATTR_MASK; |
| 1174 | if (! found) { |
| 1175 | found = fp; |
| 1176 | cur_attr = attr; |
| 1177 | continue; |
| 1178 | } |
| 1179 | /* avoid async out and adaptive in if the other method |
| 1180 | * supports the same format. |
| 1181 | * this is a workaround for the case like |
| 1182 | * M-audio audiophile USB. |
| 1183 | */ |
| 1184 | if (attr != cur_attr) { |
| 1185 | if ((attr == EP_ATTR_ASYNC && |
| 1186 | subs->direction == SNDRV_PCM_STREAM_PLAYBACK) || |
| 1187 | (attr == EP_ATTR_ADAPTIVE && |
| 1188 | subs->direction == SNDRV_PCM_STREAM_CAPTURE)) |
| 1189 | continue; |
| 1190 | if ((cur_attr == EP_ATTR_ASYNC && |
| 1191 | subs->direction == SNDRV_PCM_STREAM_PLAYBACK) || |
| 1192 | (cur_attr == EP_ATTR_ADAPTIVE && |
| 1193 | subs->direction == SNDRV_PCM_STREAM_CAPTURE)) { |
| 1194 | found = fp; |
| 1195 | cur_attr = attr; |
| 1196 | continue; |
| 1197 | } |
| 1198 | } |
| 1199 | /* find the format with the largest max. packet size */ |
| 1200 | if (fp->maxpacksize > found->maxpacksize) { |
| 1201 | found = fp; |
| 1202 | cur_attr = attr; |
| 1203 | } |
| 1204 | } |
| 1205 | return found; |
| 1206 | } |
| 1207 | |
| 1208 | |
| 1209 | /* |
| 1210 | * initialize the picth control and sample rate |
| 1211 | */ |
| 1212 | static int init_usb_pitch(struct usb_device *dev, int iface, |
| 1213 | struct usb_host_interface *alts, |
| 1214 | struct audioformat *fmt) |
| 1215 | { |
| 1216 | unsigned int ep; |
| 1217 | unsigned char data[1]; |
| 1218 | int err; |
| 1219 | |
| 1220 | ep = get_endpoint(alts, 0)->bEndpointAddress; |
| 1221 | /* if endpoint has pitch control, enable it */ |
| 1222 | if (fmt->attributes & EP_CS_ATTR_PITCH_CONTROL) { |
| 1223 | data[0] = 1; |
| 1224 | if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), SET_CUR, |
| 1225 | USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT, |
| 1226 | PITCH_CONTROL << 8, ep, data, 1, 1000)) < 0) { |
| 1227 | snd_printk(KERN_ERR "%d:%d:%d: cannot set enable PITCH\n", |
| 1228 | dev->devnum, iface, ep); |
| 1229 | return err; |
| 1230 | } |
| 1231 | } |
| 1232 | return 0; |
| 1233 | } |
| 1234 | |
| 1235 | static int init_usb_sample_rate(struct usb_device *dev, int iface, |
| 1236 | struct usb_host_interface *alts, |
| 1237 | struct audioformat *fmt, int rate) |
| 1238 | { |
| 1239 | unsigned int ep; |
| 1240 | unsigned char data[3]; |
| 1241 | int err; |
| 1242 | |
| 1243 | ep = get_endpoint(alts, 0)->bEndpointAddress; |
| 1244 | /* if endpoint has sampling rate control, set it */ |
| 1245 | if (fmt->attributes & EP_CS_ATTR_SAMPLE_RATE) { |
| 1246 | int crate; |
| 1247 | data[0] = rate; |
| 1248 | data[1] = rate >> 8; |
| 1249 | data[2] = rate >> 16; |
| 1250 | if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), SET_CUR, |
| 1251 | USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT, |
| 1252 | SAMPLING_FREQ_CONTROL << 8, ep, data, 3, 1000)) < 0) { |
Andreas Bergmeier | b9d710b | 2009-01-24 12:15:14 +0100 | [diff] [blame] | 1253 | snd_printk(KERN_ERR "%d:%d:%d: cannot set freq %d to ep %#x\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1254 | dev->devnum, iface, fmt->altsetting, rate, ep); |
| 1255 | return err; |
| 1256 | } |
| 1257 | if ((err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), GET_CUR, |
| 1258 | USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_IN, |
| 1259 | SAMPLING_FREQ_CONTROL << 8, ep, data, 3, 1000)) < 0) { |
Andreas Bergmeier | b9d710b | 2009-01-24 12:15:14 +0100 | [diff] [blame] | 1260 | snd_printk(KERN_WARNING "%d:%d:%d: cannot get freq at ep %#x\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1261 | dev->devnum, iface, fmt->altsetting, ep); |
| 1262 | return 0; /* some devices don't support reading */ |
| 1263 | } |
| 1264 | crate = data[0] | (data[1] << 8) | (data[2] << 16); |
| 1265 | if (crate != rate) { |
| 1266 | snd_printd(KERN_WARNING "current rate %d is different from the runtime rate %d\n", crate, rate); |
| 1267 | // runtime->rate = crate; |
| 1268 | } |
| 1269 | } |
| 1270 | return 0; |
| 1271 | } |
| 1272 | |
| 1273 | /* |
Sergiy Kovalchuk | 7d2b451 | 2009-12-27 09:13:41 -0800 | [diff] [blame^] | 1274 | * For E-Mu 0404USB/0202USB/TrackerPre sample rate should be set for device, |
| 1275 | * not for interface. |
| 1276 | */ |
| 1277 | static void set_format_emu_quirk(struct snd_usb_substream *subs, |
| 1278 | struct audioformat *fmt) |
| 1279 | { |
| 1280 | unsigned char emu_samplerate_id = 0; |
| 1281 | |
| 1282 | /* When capture is active |
| 1283 | * sample rate shouldn't be changed |
| 1284 | * by playback substream |
| 1285 | */ |
| 1286 | if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK) { |
| 1287 | if (subs->stream->substream[SNDRV_PCM_STREAM_CAPTURE].interface != -1) |
| 1288 | return; |
| 1289 | } |
| 1290 | |
| 1291 | switch (fmt->rate_min) { |
| 1292 | case 48000: |
| 1293 | emu_samplerate_id = EMU_QUIRK_SR_48000HZ; |
| 1294 | break; |
| 1295 | case 88200: |
| 1296 | emu_samplerate_id = EMU_QUIRK_SR_88200HZ; |
| 1297 | break; |
| 1298 | case 96000: |
| 1299 | emu_samplerate_id = EMU_QUIRK_SR_96000HZ; |
| 1300 | break; |
| 1301 | case 176400: |
| 1302 | emu_samplerate_id = EMU_QUIRK_SR_176400HZ; |
| 1303 | break; |
| 1304 | case 192000: |
| 1305 | emu_samplerate_id = EMU_QUIRK_SR_192000HZ; |
| 1306 | break; |
| 1307 | default: |
| 1308 | emu_samplerate_id = EMU_QUIRK_SR_44100HZ; |
| 1309 | break; |
| 1310 | } |
| 1311 | snd_emuusb_set_samplerate(subs->stream->chip, emu_samplerate_id); |
| 1312 | } |
| 1313 | |
| 1314 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1315 | * find a matching format and set up the interface |
| 1316 | */ |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 1317 | static int set_format(struct snd_usb_substream *subs, struct audioformat *fmt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1318 | { |
| 1319 | struct usb_device *dev = subs->dev; |
| 1320 | struct usb_host_interface *alts; |
| 1321 | struct usb_interface_descriptor *altsd; |
| 1322 | struct usb_interface *iface; |
| 1323 | unsigned int ep, attr; |
| 1324 | int is_playback = subs->direction == SNDRV_PCM_STREAM_PLAYBACK; |
| 1325 | int err; |
| 1326 | |
| 1327 | iface = usb_ifnum_to_if(dev, fmt->iface); |
Takashi Iwai | 5e246b8 | 2008-08-08 17:12:47 +0200 | [diff] [blame] | 1328 | if (WARN_ON(!iface)) |
| 1329 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1330 | alts = &iface->altsetting[fmt->altset_idx]; |
| 1331 | altsd = get_iface_desc(alts); |
Takashi Iwai | 5e246b8 | 2008-08-08 17:12:47 +0200 | [diff] [blame] | 1332 | if (WARN_ON(altsd->bAlternateSetting != fmt->altsetting)) |
| 1333 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1334 | |
| 1335 | if (fmt == subs->cur_audiofmt) |
| 1336 | return 0; |
| 1337 | |
| 1338 | /* close the old interface */ |
| 1339 | if (subs->interface >= 0 && subs->interface != fmt->iface) { |
Oliver Neukum | 5149fe2 | 2007-08-31 12:15:27 +0200 | [diff] [blame] | 1340 | if (usb_set_interface(subs->dev, subs->interface, 0) < 0) { |
| 1341 | snd_printk(KERN_ERR "%d:%d:%d: return to setting 0 failed\n", |
| 1342 | dev->devnum, fmt->iface, fmt->altsetting); |
| 1343 | return -EIO; |
| 1344 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1345 | subs->interface = -1; |
| 1346 | subs->format = 0; |
| 1347 | } |
| 1348 | |
| 1349 | /* set interface */ |
| 1350 | if (subs->interface != fmt->iface || subs->format != fmt->altset_idx) { |
| 1351 | if (usb_set_interface(dev, fmt->iface, fmt->altsetting) < 0) { |
| 1352 | snd_printk(KERN_ERR "%d:%d:%d: usb_set_interface failed\n", |
| 1353 | dev->devnum, fmt->iface, fmt->altsetting); |
| 1354 | return -EIO; |
| 1355 | } |
| 1356 | snd_printdd(KERN_INFO "setting usb interface %d:%d\n", fmt->iface, fmt->altsetting); |
| 1357 | subs->interface = fmt->iface; |
| 1358 | subs->format = fmt->altset_idx; |
| 1359 | } |
| 1360 | |
| 1361 | /* create a data pipe */ |
| 1362 | ep = fmt->endpoint & USB_ENDPOINT_NUMBER_MASK; |
| 1363 | if (is_playback) |
| 1364 | subs->datapipe = usb_sndisocpipe(dev, ep); |
| 1365 | else |
| 1366 | subs->datapipe = usb_rcvisocpipe(dev, ep); |
Clemens Ladisch | 744b89e | 2009-04-03 09:45:01 +0200 | [diff] [blame] | 1367 | subs->datainterval = fmt->datainterval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1368 | subs->syncpipe = subs->syncinterval = 0; |
| 1369 | subs->maxpacksize = fmt->maxpacksize; |
| 1370 | subs->fill_max = 0; |
| 1371 | |
| 1372 | /* we need a sync pipe in async OUT or adaptive IN mode */ |
| 1373 | /* check the number of EP, since some devices have broken |
| 1374 | * descriptors which fool us. if it has only one EP, |
| 1375 | * assume it as adaptive-out or sync-in. |
| 1376 | */ |
| 1377 | attr = fmt->ep_attr & EP_ATTR_MASK; |
| 1378 | if (((is_playback && attr == EP_ATTR_ASYNC) || |
| 1379 | (! is_playback && attr == EP_ATTR_ADAPTIVE)) && |
| 1380 | altsd->bNumEndpoints >= 2) { |
| 1381 | /* check sync-pipe endpoint */ |
| 1382 | /* ... and check descriptor size before accessing bSynchAddress |
| 1383 | because there is a version of the SB Audigy 2 NX firmware lacking |
| 1384 | the audio fields in the endpoint descriptors */ |
| 1385 | if ((get_endpoint(alts, 1)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != 0x01 || |
| 1386 | (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE && |
| 1387 | get_endpoint(alts, 1)->bSynchAddress != 0)) { |
| 1388 | snd_printk(KERN_ERR "%d:%d:%d : invalid synch pipe\n", |
| 1389 | dev->devnum, fmt->iface, fmt->altsetting); |
| 1390 | return -EINVAL; |
| 1391 | } |
| 1392 | ep = get_endpoint(alts, 1)->bEndpointAddress; |
| 1393 | if (get_endpoint(alts, 0)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE && |
| 1394 | (( is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress | USB_DIR_IN)) || |
| 1395 | (!is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress & ~USB_DIR_IN)))) { |
| 1396 | snd_printk(KERN_ERR "%d:%d:%d : invalid synch pipe\n", |
| 1397 | dev->devnum, fmt->iface, fmt->altsetting); |
| 1398 | return -EINVAL; |
| 1399 | } |
| 1400 | ep &= USB_ENDPOINT_NUMBER_MASK; |
| 1401 | if (is_playback) |
| 1402 | subs->syncpipe = usb_rcvisocpipe(dev, ep); |
| 1403 | else |
| 1404 | subs->syncpipe = usb_sndisocpipe(dev, ep); |
Clemens Ladisch | 1149a64 | 2005-05-02 08:53:46 +0200 | [diff] [blame] | 1405 | if (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE && |
| 1406 | get_endpoint(alts, 1)->bRefresh >= 1 && |
| 1407 | get_endpoint(alts, 1)->bRefresh <= 9) |
| 1408 | subs->syncinterval = get_endpoint(alts, 1)->bRefresh; |
Clemens Ladisch | 604cf49 | 2005-05-17 09:15:27 +0200 | [diff] [blame] | 1409 | else if (snd_usb_get_speed(subs->dev) == USB_SPEED_FULL) |
Clemens Ladisch | 1149a64 | 2005-05-02 08:53:46 +0200 | [diff] [blame] | 1410 | subs->syncinterval = 1; |
Clemens Ladisch | 604cf49 | 2005-05-17 09:15:27 +0200 | [diff] [blame] | 1411 | else if (get_endpoint(alts, 1)->bInterval >= 1 && |
| 1412 | get_endpoint(alts, 1)->bInterval <= 16) |
| 1413 | subs->syncinterval = get_endpoint(alts, 1)->bInterval - 1; |
| 1414 | else |
| 1415 | subs->syncinterval = 3; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1416 | } |
| 1417 | |
| 1418 | /* always fill max packet size */ |
| 1419 | if (fmt->attributes & EP_CS_ATTR_FILL_MAX) |
| 1420 | subs->fill_max = 1; |
| 1421 | |
| 1422 | if ((err = init_usb_pitch(dev, subs->interface, alts, fmt)) < 0) |
| 1423 | return err; |
| 1424 | |
| 1425 | subs->cur_audiofmt = fmt; |
| 1426 | |
Sergiy Kovalchuk | 7d2b451 | 2009-12-27 09:13:41 -0800 | [diff] [blame^] | 1427 | switch (subs->stream->chip->usb_id) { |
| 1428 | case USB_ID(0x041e, 0x3f02): /* E-Mu 0202 USB */ |
| 1429 | case USB_ID(0x041e, 0x3f04): /* E-Mu 0404 USB */ |
| 1430 | case USB_ID(0x041e, 0x3f0a): /* E-Mu Tracker Pre */ |
| 1431 | set_format_emu_quirk(subs, fmt); |
| 1432 | break; |
| 1433 | } |
| 1434 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1435 | #if 0 |
Takashi Iwai | 54530bd | 2009-02-05 15:55:18 +0100 | [diff] [blame] | 1436 | printk(KERN_DEBUG |
| 1437 | "setting done: format = %d, rate = %d..%d, channels = %d\n", |
Pavel Machek | 2a56f51 | 2008-04-14 13:14:22 +0200 | [diff] [blame] | 1438 | fmt->format, fmt->rate_min, fmt->rate_max, fmt->channels); |
Takashi Iwai | 54530bd | 2009-02-05 15:55:18 +0100 | [diff] [blame] | 1439 | printk(KERN_DEBUG |
| 1440 | " datapipe = 0x%0x, syncpipe = 0x%0x\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1441 | subs->datapipe, subs->syncpipe); |
| 1442 | #endif |
| 1443 | |
| 1444 | return 0; |
| 1445 | } |
| 1446 | |
| 1447 | /* |
| 1448 | * hw_params callback |
| 1449 | * |
| 1450 | * allocate a buffer and set the given audio format. |
| 1451 | * |
| 1452 | * so far we use a physically linear buffer although packetize transfer |
| 1453 | * doesn't need a continuous area. |
| 1454 | * if sg buffer is supported on the later version of alsa, we'll follow |
| 1455 | * that. |
| 1456 | */ |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 1457 | static int snd_usb_hw_params(struct snd_pcm_substream *substream, |
| 1458 | struct snd_pcm_hw_params *hw_params) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1459 | { |
John Daiker | 8851827 | 2006-12-28 13:55:05 +0100 | [diff] [blame] | 1460 | struct snd_usb_substream *subs = substream->runtime->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1461 | struct audioformat *fmt; |
| 1462 | unsigned int channels, rate, format; |
| 1463 | int ret, changed; |
| 1464 | |
Clemens Ladisch | c55675e | 2009-12-18 09:31:31 +0100 | [diff] [blame] | 1465 | ret = snd_pcm_lib_alloc_vmalloc_buffer(substream, |
| 1466 | params_buffer_bytes(hw_params)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1467 | if (ret < 0) |
| 1468 | return ret; |
| 1469 | |
| 1470 | format = params_format(hw_params); |
| 1471 | rate = params_rate(hw_params); |
| 1472 | channels = params_channels(hw_params); |
| 1473 | fmt = find_format(subs, format, rate, channels); |
Pavel Machek | 07f51a7 | 2008-04-14 13:15:56 +0200 | [diff] [blame] | 1474 | if (!fmt) { |
Andreas Bergmeier | b9d710b | 2009-01-24 12:15:14 +0100 | [diff] [blame] | 1475 | snd_printd(KERN_DEBUG "cannot set format: format = %#x, rate = %d, channels = %d\n", |
Jaroslav Kysela | 21a3479 | 2006-01-13 09:12:11 +0100 | [diff] [blame] | 1476 | format, rate, channels); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1477 | return -EINVAL; |
| 1478 | } |
| 1479 | |
| 1480 | changed = subs->cur_audiofmt != fmt || |
| 1481 | subs->period_bytes != params_period_bytes(hw_params) || |
| 1482 | subs->cur_rate != rate; |
| 1483 | if ((ret = set_format(subs, fmt)) < 0) |
| 1484 | return ret; |
| 1485 | |
| 1486 | if (subs->cur_rate != rate) { |
| 1487 | struct usb_host_interface *alts; |
| 1488 | struct usb_interface *iface; |
| 1489 | iface = usb_ifnum_to_if(subs->dev, fmt->iface); |
| 1490 | alts = &iface->altsetting[fmt->altset_idx]; |
| 1491 | ret = init_usb_sample_rate(subs->dev, subs->interface, alts, fmt, rate); |
| 1492 | if (ret < 0) |
| 1493 | return ret; |
| 1494 | subs->cur_rate = rate; |
| 1495 | } |
| 1496 | |
| 1497 | if (changed) { |
| 1498 | /* format changed */ |
| 1499 | release_substream_urbs(subs, 0); |
| 1500 | /* influenced: period_bytes, channels, rate, format, */ |
| 1501 | ret = init_substream_urbs(subs, params_period_bytes(hw_params), |
| 1502 | params_rate(hw_params), |
| 1503 | snd_pcm_format_physical_width(params_format(hw_params)) * params_channels(hw_params)); |
| 1504 | } |
| 1505 | |
| 1506 | return ret; |
| 1507 | } |
| 1508 | |
| 1509 | /* |
| 1510 | * hw_free callback |
| 1511 | * |
| 1512 | * reset the audio format and release the buffer |
| 1513 | */ |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 1514 | static int snd_usb_hw_free(struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1515 | { |
John Daiker | 8851827 | 2006-12-28 13:55:05 +0100 | [diff] [blame] | 1516 | struct snd_usb_substream *subs = substream->runtime->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1517 | |
| 1518 | subs->cur_audiofmt = NULL; |
| 1519 | subs->cur_rate = 0; |
| 1520 | subs->period_bytes = 0; |
Takashi Iwai | de1b8b9 | 2006-11-08 15:41:29 +0100 | [diff] [blame] | 1521 | if (!subs->stream->chip->shutdown) |
| 1522 | release_substream_urbs(subs, 0); |
Clemens Ladisch | c55675e | 2009-12-18 09:31:31 +0100 | [diff] [blame] | 1523 | return snd_pcm_lib_free_vmalloc_buffer(substream); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1524 | } |
| 1525 | |
| 1526 | /* |
| 1527 | * prepare callback |
| 1528 | * |
| 1529 | * only a few subtle things... |
| 1530 | */ |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 1531 | static int snd_usb_pcm_prepare(struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1532 | { |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 1533 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 1534 | struct snd_usb_substream *subs = runtime->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1535 | |
| 1536 | if (! subs->cur_audiofmt) { |
| 1537 | snd_printk(KERN_ERR "usbaudio: no format is specified!\n"); |
| 1538 | return -ENXIO; |
| 1539 | } |
| 1540 | |
| 1541 | /* some unit conversions in runtime */ |
| 1542 | subs->maxframesize = bytes_to_frames(runtime, subs->maxpacksize); |
| 1543 | subs->curframesize = bytes_to_frames(runtime, subs->curpacksize); |
| 1544 | |
| 1545 | /* reset the pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1546 | subs->hwptr_done = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1547 | subs->transfer_done = 0; |
| 1548 | subs->phase = 0; |
Takashi Iwai | ae1ec5e | 2008-10-13 03:08:53 +0200 | [diff] [blame] | 1549 | runtime->delay = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1550 | |
| 1551 | /* clear urbs (to be sure) */ |
| 1552 | deactivate_urbs(subs, 0, 1); |
| 1553 | wait_clear_urbs(subs); |
| 1554 | |
Clemens Ladisch | b55bbf0 | 2005-11-02 11:32:52 +0100 | [diff] [blame] | 1555 | /* for playback, submit the URBs now; otherwise, the first hwptr_done |
| 1556 | * updates for all URBs would happen at the same time when starting */ |
| 1557 | if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK) { |
Clemens Ladisch | e4f8e65 | 2006-10-04 13:42:57 +0200 | [diff] [blame] | 1558 | subs->ops.prepare = prepare_nodata_playback_urb; |
Clemens Ladisch | b55bbf0 | 2005-11-02 11:32:52 +0100 | [diff] [blame] | 1559 | return start_urbs(subs, runtime); |
| 1560 | } else |
| 1561 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1562 | } |
| 1563 | |
Clemens Ladisch | 1700f30 | 2006-10-04 13:41:25 +0200 | [diff] [blame] | 1564 | static struct snd_pcm_hardware snd_usb_hardware = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1565 | { |
Clemens Ladisch | 49045d3 | 2005-09-05 10:31:05 +0200 | [diff] [blame] | 1566 | .info = SNDRV_PCM_INFO_MMAP | |
| 1567 | SNDRV_PCM_INFO_MMAP_VALID | |
| 1568 | SNDRV_PCM_INFO_BATCH | |
| 1569 | SNDRV_PCM_INFO_INTERLEAVED | |
Clemens Ladisch | e4f8e65 | 2006-10-04 13:42:57 +0200 | [diff] [blame] | 1570 | SNDRV_PCM_INFO_BLOCK_TRANSFER | |
| 1571 | SNDRV_PCM_INFO_PAUSE, |
Clemens Ladisch | d31cbbf | 2005-09-26 09:59:57 +0200 | [diff] [blame] | 1572 | .buffer_bytes_max = 1024 * 1024, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1573 | .period_bytes_min = 64, |
Clemens Ladisch | d31cbbf | 2005-09-26 09:59:57 +0200 | [diff] [blame] | 1574 | .period_bytes_max = 512 * 1024, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1575 | .periods_min = 2, |
| 1576 | .periods_max = 1024, |
| 1577 | }; |
| 1578 | |
| 1579 | /* |
| 1580 | * h/w constraints |
| 1581 | */ |
| 1582 | |
| 1583 | #ifdef HW_CONST_DEBUG |
| 1584 | #define hwc_debug(fmt, args...) printk(KERN_DEBUG fmt, ##args) |
| 1585 | #else |
| 1586 | #define hwc_debug(fmt, args...) /**/ |
| 1587 | #endif |
| 1588 | |
Clemens Ladisch | a7d9c09 | 2009-04-03 09:48:26 +0200 | [diff] [blame] | 1589 | static int hw_check_valid_format(struct snd_usb_substream *subs, |
| 1590 | struct snd_pcm_hw_params *params, |
| 1591 | struct audioformat *fp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1592 | { |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 1593 | struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE); |
| 1594 | struct snd_interval *ct = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS); |
| 1595 | struct snd_mask *fmts = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT); |
Clemens Ladisch | a7d9c09 | 2009-04-03 09:48:26 +0200 | [diff] [blame] | 1596 | struct snd_interval *pt = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME); |
| 1597 | unsigned int ptime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1598 | |
| 1599 | /* check the format */ |
Pavel Machek | 07f51a7 | 2008-04-14 13:15:56 +0200 | [diff] [blame] | 1600 | if (!snd_mask_test(fmts, fp->format)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1601 | hwc_debug(" > check: no supported format %d\n", fp->format); |
| 1602 | return 0; |
| 1603 | } |
| 1604 | /* check the channels */ |
| 1605 | if (fp->channels < ct->min || fp->channels > ct->max) { |
| 1606 | hwc_debug(" > check: no valid channels %d (%d/%d)\n", fp->channels, ct->min, ct->max); |
| 1607 | return 0; |
| 1608 | } |
| 1609 | /* check the rate is within the range */ |
| 1610 | if (fp->rate_min > it->max || (fp->rate_min == it->max && it->openmax)) { |
| 1611 | hwc_debug(" > check: rate_min %d > max %d\n", fp->rate_min, it->max); |
| 1612 | return 0; |
| 1613 | } |
| 1614 | if (fp->rate_max < it->min || (fp->rate_max == it->min && it->openmin)) { |
| 1615 | hwc_debug(" > check: rate_max %d < min %d\n", fp->rate_max, it->min); |
| 1616 | return 0; |
| 1617 | } |
Clemens Ladisch | a7d9c09 | 2009-04-03 09:48:26 +0200 | [diff] [blame] | 1618 | /* check whether the period time is >= the data packet interval */ |
| 1619 | if (snd_usb_get_speed(subs->dev) == USB_SPEED_HIGH) { |
| 1620 | ptime = 125 * (1 << fp->datainterval); |
| 1621 | if (ptime > pt->max || (ptime == pt->max && pt->openmax)) { |
| 1622 | hwc_debug(" > check: ptime %u > max %u\n", ptime, pt->max); |
| 1623 | return 0; |
| 1624 | } |
| 1625 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1626 | return 1; |
| 1627 | } |
| 1628 | |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 1629 | static int hw_rule_rate(struct snd_pcm_hw_params *params, |
| 1630 | struct snd_pcm_hw_rule *rule) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1631 | { |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 1632 | struct snd_usb_substream *subs = rule->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1633 | struct list_head *p; |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 1634 | struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1635 | unsigned int rmin, rmax; |
| 1636 | int changed; |
| 1637 | |
| 1638 | hwc_debug("hw_rule_rate: (%d,%d)\n", it->min, it->max); |
| 1639 | changed = 0; |
| 1640 | rmin = rmax = 0; |
| 1641 | list_for_each(p, &subs->fmt_list) { |
| 1642 | struct audioformat *fp; |
| 1643 | fp = list_entry(p, struct audioformat, list); |
Clemens Ladisch | a7d9c09 | 2009-04-03 09:48:26 +0200 | [diff] [blame] | 1644 | if (!hw_check_valid_format(subs, params, fp)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1645 | continue; |
| 1646 | if (changed++) { |
| 1647 | if (rmin > fp->rate_min) |
| 1648 | rmin = fp->rate_min; |
| 1649 | if (rmax < fp->rate_max) |
| 1650 | rmax = fp->rate_max; |
| 1651 | } else { |
| 1652 | rmin = fp->rate_min; |
| 1653 | rmax = fp->rate_max; |
| 1654 | } |
| 1655 | } |
| 1656 | |
Pavel Machek | 07f51a7 | 2008-04-14 13:15:56 +0200 | [diff] [blame] | 1657 | if (!changed) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1658 | hwc_debug(" --> get empty\n"); |
| 1659 | it->empty = 1; |
| 1660 | return -EINVAL; |
| 1661 | } |
| 1662 | |
| 1663 | changed = 0; |
| 1664 | if (it->min < rmin) { |
| 1665 | it->min = rmin; |
| 1666 | it->openmin = 0; |
| 1667 | changed = 1; |
| 1668 | } |
| 1669 | if (it->max > rmax) { |
| 1670 | it->max = rmax; |
| 1671 | it->openmax = 0; |
| 1672 | changed = 1; |
| 1673 | } |
| 1674 | if (snd_interval_checkempty(it)) { |
| 1675 | it->empty = 1; |
| 1676 | return -EINVAL; |
| 1677 | } |
| 1678 | hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed); |
| 1679 | return changed; |
| 1680 | } |
| 1681 | |
| 1682 | |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 1683 | static int hw_rule_channels(struct snd_pcm_hw_params *params, |
| 1684 | struct snd_pcm_hw_rule *rule) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1685 | { |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 1686 | struct snd_usb_substream *subs = rule->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1687 | struct list_head *p; |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 1688 | struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1689 | unsigned int rmin, rmax; |
| 1690 | int changed; |
| 1691 | |
| 1692 | hwc_debug("hw_rule_channels: (%d,%d)\n", it->min, it->max); |
| 1693 | changed = 0; |
| 1694 | rmin = rmax = 0; |
| 1695 | list_for_each(p, &subs->fmt_list) { |
| 1696 | struct audioformat *fp; |
| 1697 | fp = list_entry(p, struct audioformat, list); |
Clemens Ladisch | a7d9c09 | 2009-04-03 09:48:26 +0200 | [diff] [blame] | 1698 | if (!hw_check_valid_format(subs, params, fp)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1699 | continue; |
| 1700 | if (changed++) { |
| 1701 | if (rmin > fp->channels) |
| 1702 | rmin = fp->channels; |
| 1703 | if (rmax < fp->channels) |
| 1704 | rmax = fp->channels; |
| 1705 | } else { |
| 1706 | rmin = fp->channels; |
| 1707 | rmax = fp->channels; |
| 1708 | } |
| 1709 | } |
| 1710 | |
Pavel Machek | 07f51a7 | 2008-04-14 13:15:56 +0200 | [diff] [blame] | 1711 | if (!changed) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1712 | hwc_debug(" --> get empty\n"); |
| 1713 | it->empty = 1; |
| 1714 | return -EINVAL; |
| 1715 | } |
| 1716 | |
| 1717 | changed = 0; |
| 1718 | if (it->min < rmin) { |
| 1719 | it->min = rmin; |
| 1720 | it->openmin = 0; |
| 1721 | changed = 1; |
| 1722 | } |
| 1723 | if (it->max > rmax) { |
| 1724 | it->max = rmax; |
| 1725 | it->openmax = 0; |
| 1726 | changed = 1; |
| 1727 | } |
| 1728 | if (snd_interval_checkempty(it)) { |
| 1729 | it->empty = 1; |
| 1730 | return -EINVAL; |
| 1731 | } |
| 1732 | hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed); |
| 1733 | return changed; |
| 1734 | } |
| 1735 | |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 1736 | static int hw_rule_format(struct snd_pcm_hw_params *params, |
| 1737 | struct snd_pcm_hw_rule *rule) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1738 | { |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 1739 | struct snd_usb_substream *subs = rule->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1740 | struct list_head *p; |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 1741 | struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1742 | u64 fbits; |
| 1743 | u32 oldbits[2]; |
| 1744 | int changed; |
| 1745 | |
| 1746 | hwc_debug("hw_rule_format: %x:%x\n", fmt->bits[0], fmt->bits[1]); |
| 1747 | fbits = 0; |
| 1748 | list_for_each(p, &subs->fmt_list) { |
| 1749 | struct audioformat *fp; |
| 1750 | fp = list_entry(p, struct audioformat, list); |
Clemens Ladisch | a7d9c09 | 2009-04-03 09:48:26 +0200 | [diff] [blame] | 1751 | if (!hw_check_valid_format(subs, params, fp)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1752 | continue; |
| 1753 | fbits |= (1ULL << fp->format); |
| 1754 | } |
| 1755 | |
| 1756 | oldbits[0] = fmt->bits[0]; |
| 1757 | oldbits[1] = fmt->bits[1]; |
| 1758 | fmt->bits[0] &= (u32)fbits; |
| 1759 | fmt->bits[1] &= (u32)(fbits >> 32); |
Pavel Machek | 07f51a7 | 2008-04-14 13:15:56 +0200 | [diff] [blame] | 1760 | if (!fmt->bits[0] && !fmt->bits[1]) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1761 | hwc_debug(" --> get empty\n"); |
| 1762 | return -EINVAL; |
| 1763 | } |
| 1764 | changed = (oldbits[0] != fmt->bits[0] || oldbits[1] != fmt->bits[1]); |
| 1765 | hwc_debug(" --> %x:%x (changed = %d)\n", fmt->bits[0], fmt->bits[1], changed); |
| 1766 | return changed; |
| 1767 | } |
| 1768 | |
Clemens Ladisch | a7d9c09 | 2009-04-03 09:48:26 +0200 | [diff] [blame] | 1769 | static int hw_rule_period_time(struct snd_pcm_hw_params *params, |
| 1770 | struct snd_pcm_hw_rule *rule) |
| 1771 | { |
| 1772 | struct snd_usb_substream *subs = rule->private; |
| 1773 | struct audioformat *fp; |
| 1774 | struct snd_interval *it; |
| 1775 | unsigned char min_datainterval; |
| 1776 | unsigned int pmin; |
| 1777 | int changed; |
| 1778 | |
| 1779 | it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME); |
| 1780 | hwc_debug("hw_rule_period_time: (%u,%u)\n", it->min, it->max); |
| 1781 | min_datainterval = 0xff; |
| 1782 | list_for_each_entry(fp, &subs->fmt_list, list) { |
| 1783 | if (!hw_check_valid_format(subs, params, fp)) |
| 1784 | continue; |
| 1785 | min_datainterval = min(min_datainterval, fp->datainterval); |
| 1786 | } |
| 1787 | if (min_datainterval == 0xff) { |
| 1788 | hwc_debug(" --> get emtpy\n"); |
| 1789 | it->empty = 1; |
| 1790 | return -EINVAL; |
| 1791 | } |
| 1792 | pmin = 125 * (1 << min_datainterval); |
| 1793 | changed = 0; |
| 1794 | if (it->min < pmin) { |
| 1795 | it->min = pmin; |
| 1796 | it->openmin = 0; |
| 1797 | changed = 1; |
| 1798 | } |
| 1799 | if (snd_interval_checkempty(it)) { |
| 1800 | it->empty = 1; |
| 1801 | return -EINVAL; |
| 1802 | } |
| 1803 | hwc_debug(" --> (%u,%u) (changed = %d)\n", it->min, it->max, changed); |
| 1804 | return changed; |
| 1805 | } |
| 1806 | |
Luke Ross | a79eee8 | 2006-08-29 10:46:32 +0200 | [diff] [blame] | 1807 | /* |
| 1808 | * If the device supports unusual bit rates, does the request meet these? |
| 1809 | */ |
| 1810 | static int snd_usb_pcm_check_knot(struct snd_pcm_runtime *runtime, |
| 1811 | struct snd_usb_substream *subs) |
| 1812 | { |
Takashi Iwai | 8fec560 | 2007-02-01 11:50:56 +0100 | [diff] [blame] | 1813 | struct audioformat *fp; |
| 1814 | int count = 0, needs_knot = 0; |
Luke Ross | a79eee8 | 2006-08-29 10:46:32 +0200 | [diff] [blame] | 1815 | int err; |
| 1816 | |
Takashi Iwai | 8fec560 | 2007-02-01 11:50:56 +0100 | [diff] [blame] | 1817 | list_for_each_entry(fp, &subs->fmt_list, list) { |
| 1818 | if (fp->rates & SNDRV_PCM_RATE_CONTINUOUS) |
| 1819 | return 0; |
| 1820 | count += fp->nr_rates; |
Clemens Ladisch | 918f3a0 | 2007-08-13 17:40:54 +0200 | [diff] [blame] | 1821 | if (fp->rates & SNDRV_PCM_RATE_KNOT) |
Takashi Iwai | 8fec560 | 2007-02-01 11:50:56 +0100 | [diff] [blame] | 1822 | needs_knot = 1; |
Luke Ross | a79eee8 | 2006-08-29 10:46:32 +0200 | [diff] [blame] | 1823 | } |
Takashi Iwai | 8fec560 | 2007-02-01 11:50:56 +0100 | [diff] [blame] | 1824 | if (!needs_knot) |
| 1825 | return 0; |
| 1826 | |
| 1827 | subs->rate_list.count = count; |
| 1828 | subs->rate_list.list = kmalloc(sizeof(int) * count, GFP_KERNEL); |
| 1829 | subs->rate_list.mask = 0; |
| 1830 | count = 0; |
| 1831 | list_for_each_entry(fp, &subs->fmt_list, list) { |
| 1832 | int i; |
| 1833 | for (i = 0; i < fp->nr_rates; i++) |
| 1834 | subs->rate_list.list[count++] = fp->rate_table[i]; |
| 1835 | } |
| 1836 | err = snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, |
| 1837 | &subs->rate_list); |
| 1838 | if (err < 0) |
| 1839 | return err; |
Luke Ross | a79eee8 | 2006-08-29 10:46:32 +0200 | [diff] [blame] | 1840 | |
| 1841 | return 0; |
| 1842 | } |
| 1843 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1844 | |
| 1845 | /* |
| 1846 | * set up the runtime hardware information. |
| 1847 | */ |
| 1848 | |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 1849 | static int setup_hw_info(struct snd_pcm_runtime *runtime, struct snd_usb_substream *subs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1850 | { |
| 1851 | struct list_head *p; |
Clemens Ladisch | a7d9c09 | 2009-04-03 09:48:26 +0200 | [diff] [blame] | 1852 | unsigned int pt, ptmin; |
| 1853 | int param_period_time_if_needed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1854 | int err; |
| 1855 | |
| 1856 | runtime->hw.formats = subs->formats; |
| 1857 | |
| 1858 | runtime->hw.rate_min = 0x7fffffff; |
| 1859 | runtime->hw.rate_max = 0; |
| 1860 | runtime->hw.channels_min = 256; |
| 1861 | runtime->hw.channels_max = 0; |
| 1862 | runtime->hw.rates = 0; |
Clemens Ladisch | a7d9c09 | 2009-04-03 09:48:26 +0200 | [diff] [blame] | 1863 | ptmin = UINT_MAX; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1864 | /* check min/max rates and channels */ |
| 1865 | list_for_each(p, &subs->fmt_list) { |
| 1866 | struct audioformat *fp; |
| 1867 | fp = list_entry(p, struct audioformat, list); |
| 1868 | runtime->hw.rates |= fp->rates; |
| 1869 | if (runtime->hw.rate_min > fp->rate_min) |
| 1870 | runtime->hw.rate_min = fp->rate_min; |
| 1871 | if (runtime->hw.rate_max < fp->rate_max) |
| 1872 | runtime->hw.rate_max = fp->rate_max; |
| 1873 | if (runtime->hw.channels_min > fp->channels) |
| 1874 | runtime->hw.channels_min = fp->channels; |
| 1875 | if (runtime->hw.channels_max < fp->channels) |
| 1876 | runtime->hw.channels_max = fp->channels; |
| 1877 | if (fp->fmt_type == USB_FORMAT_TYPE_II && fp->frame_size > 0) { |
| 1878 | /* FIXME: there might be more than one audio formats... */ |
| 1879 | runtime->hw.period_bytes_min = runtime->hw.period_bytes_max = |
| 1880 | fp->frame_size; |
| 1881 | } |
Clemens Ladisch | a7d9c09 | 2009-04-03 09:48:26 +0200 | [diff] [blame] | 1882 | pt = 125 * (1 << fp->datainterval); |
| 1883 | ptmin = min(ptmin, pt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1884 | } |
| 1885 | |
Clemens Ladisch | a7d9c09 | 2009-04-03 09:48:26 +0200 | [diff] [blame] | 1886 | param_period_time_if_needed = SNDRV_PCM_HW_PARAM_PERIOD_TIME; |
| 1887 | if (snd_usb_get_speed(subs->dev) != USB_SPEED_HIGH) |
| 1888 | /* full speed devices have fixed data packet interval */ |
| 1889 | ptmin = 1000; |
| 1890 | if (ptmin == 1000) |
| 1891 | /* if period time doesn't go below 1 ms, no rules needed */ |
| 1892 | param_period_time_if_needed = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1893 | snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME, |
Clemens Ladisch | a7d9c09 | 2009-04-03 09:48:26 +0200 | [diff] [blame] | 1894 | ptmin, UINT_MAX); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1895 | |
Clemens Ladisch | 4608eb0 | 2009-04-03 09:42:42 +0200 | [diff] [blame] | 1896 | if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, |
| 1897 | hw_rule_rate, subs, |
| 1898 | SNDRV_PCM_HW_PARAM_FORMAT, |
| 1899 | SNDRV_PCM_HW_PARAM_CHANNELS, |
Clemens Ladisch | a7d9c09 | 2009-04-03 09:48:26 +0200 | [diff] [blame] | 1900 | param_period_time_if_needed, |
Clemens Ladisch | 4608eb0 | 2009-04-03 09:42:42 +0200 | [diff] [blame] | 1901 | -1)) < 0) |
Takashi Iwai | 5a220c8 | 2008-03-17 09:59:32 +0100 | [diff] [blame] | 1902 | return err; |
Clemens Ladisch | 4608eb0 | 2009-04-03 09:42:42 +0200 | [diff] [blame] | 1903 | if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS, |
| 1904 | hw_rule_channels, subs, |
| 1905 | SNDRV_PCM_HW_PARAM_FORMAT, |
| 1906 | SNDRV_PCM_HW_PARAM_RATE, |
Clemens Ladisch | a7d9c09 | 2009-04-03 09:48:26 +0200 | [diff] [blame] | 1907 | param_period_time_if_needed, |
Clemens Ladisch | 4608eb0 | 2009-04-03 09:42:42 +0200 | [diff] [blame] | 1908 | -1)) < 0) |
| 1909 | return err; |
| 1910 | if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT, |
| 1911 | hw_rule_format, subs, |
| 1912 | SNDRV_PCM_HW_PARAM_RATE, |
| 1913 | SNDRV_PCM_HW_PARAM_CHANNELS, |
Clemens Ladisch | a7d9c09 | 2009-04-03 09:48:26 +0200 | [diff] [blame] | 1914 | param_period_time_if_needed, |
Clemens Ladisch | 4608eb0 | 2009-04-03 09:42:42 +0200 | [diff] [blame] | 1915 | -1)) < 0) |
| 1916 | return err; |
Clemens Ladisch | a7d9c09 | 2009-04-03 09:48:26 +0200 | [diff] [blame] | 1917 | if (param_period_time_if_needed >= 0) { |
| 1918 | err = snd_pcm_hw_rule_add(runtime, 0, |
| 1919 | SNDRV_PCM_HW_PARAM_PERIOD_TIME, |
| 1920 | hw_rule_period_time, subs, |
| 1921 | SNDRV_PCM_HW_PARAM_FORMAT, |
| 1922 | SNDRV_PCM_HW_PARAM_CHANNELS, |
| 1923 | SNDRV_PCM_HW_PARAM_RATE, |
| 1924 | -1); |
| 1925 | if (err < 0) |
| 1926 | return err; |
| 1927 | } |
Clemens Ladisch | 4608eb0 | 2009-04-03 09:42:42 +0200 | [diff] [blame] | 1928 | if ((err = snd_usb_pcm_check_knot(runtime, subs)) < 0) |
| 1929 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1930 | return 0; |
| 1931 | } |
| 1932 | |
Clemens Ladisch | 1700f30 | 2006-10-04 13:41:25 +0200 | [diff] [blame] | 1933 | static int snd_usb_pcm_open(struct snd_pcm_substream *substream, int direction) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1934 | { |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 1935 | struct snd_usb_stream *as = snd_pcm_substream_chip(substream); |
| 1936 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 1937 | struct snd_usb_substream *subs = &as->substream[direction]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1938 | |
| 1939 | subs->interface = -1; |
| 1940 | subs->format = 0; |
Clemens Ladisch | 1700f30 | 2006-10-04 13:41:25 +0200 | [diff] [blame] | 1941 | runtime->hw = snd_usb_hardware; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1942 | runtime->private_data = subs; |
| 1943 | subs->pcm_substream = substream; |
| 1944 | return setup_hw_info(runtime, subs); |
| 1945 | } |
| 1946 | |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 1947 | static int snd_usb_pcm_close(struct snd_pcm_substream *substream, int direction) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1948 | { |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 1949 | struct snd_usb_stream *as = snd_pcm_substream_chip(substream); |
| 1950 | struct snd_usb_substream *subs = &as->substream[direction]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1951 | |
| 1952 | if (subs->interface >= 0) { |
| 1953 | usb_set_interface(subs->dev, subs->interface, 0); |
| 1954 | subs->interface = -1; |
| 1955 | } |
| 1956 | subs->pcm_substream = NULL; |
| 1957 | return 0; |
| 1958 | } |
| 1959 | |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 1960 | static int snd_usb_playback_open(struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1961 | { |
Clemens Ladisch | 1700f30 | 2006-10-04 13:41:25 +0200 | [diff] [blame] | 1962 | return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_PLAYBACK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1963 | } |
| 1964 | |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 1965 | static int snd_usb_playback_close(struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1966 | { |
| 1967 | return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_PLAYBACK); |
| 1968 | } |
| 1969 | |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 1970 | static int snd_usb_capture_open(struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1971 | { |
Clemens Ladisch | 1700f30 | 2006-10-04 13:41:25 +0200 | [diff] [blame] | 1972 | return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_CAPTURE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1973 | } |
| 1974 | |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 1975 | static int snd_usb_capture_close(struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1976 | { |
| 1977 | return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_CAPTURE); |
| 1978 | } |
| 1979 | |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 1980 | static struct snd_pcm_ops snd_usb_playback_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1981 | .open = snd_usb_playback_open, |
| 1982 | .close = snd_usb_playback_close, |
| 1983 | .ioctl = snd_pcm_lib_ioctl, |
| 1984 | .hw_params = snd_usb_hw_params, |
| 1985 | .hw_free = snd_usb_hw_free, |
| 1986 | .prepare = snd_usb_pcm_prepare, |
Clemens Ladisch | b55bbf0 | 2005-11-02 11:32:52 +0100 | [diff] [blame] | 1987 | .trigger = snd_usb_pcm_playback_trigger, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1988 | .pointer = snd_usb_pcm_pointer, |
Clemens Ladisch | c55675e | 2009-12-18 09:31:31 +0100 | [diff] [blame] | 1989 | .page = snd_pcm_lib_get_vmalloc_page, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1990 | }; |
| 1991 | |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 1992 | static struct snd_pcm_ops snd_usb_capture_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1993 | .open = snd_usb_capture_open, |
| 1994 | .close = snd_usb_capture_close, |
| 1995 | .ioctl = snd_pcm_lib_ioctl, |
| 1996 | .hw_params = snd_usb_hw_params, |
| 1997 | .hw_free = snd_usb_hw_free, |
| 1998 | .prepare = snd_usb_pcm_prepare, |
Clemens Ladisch | b55bbf0 | 2005-11-02 11:32:52 +0100 | [diff] [blame] | 1999 | .trigger = snd_usb_pcm_capture_trigger, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2000 | .pointer = snd_usb_pcm_pointer, |
Clemens Ladisch | c55675e | 2009-12-18 09:31:31 +0100 | [diff] [blame] | 2001 | .page = snd_pcm_lib_get_vmalloc_page, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2002 | }; |
| 2003 | |
| 2004 | |
| 2005 | |
| 2006 | /* |
| 2007 | * helper functions |
| 2008 | */ |
| 2009 | |
| 2010 | /* |
| 2011 | * combine bytes and get an integer value |
| 2012 | */ |
| 2013 | unsigned int snd_usb_combine_bytes(unsigned char *bytes, int size) |
| 2014 | { |
| 2015 | switch (size) { |
| 2016 | case 1: return *bytes; |
| 2017 | case 2: return combine_word(bytes); |
| 2018 | case 3: return combine_triple(bytes); |
| 2019 | case 4: return combine_quad(bytes); |
| 2020 | default: return 0; |
| 2021 | } |
| 2022 | } |
| 2023 | |
| 2024 | /* |
| 2025 | * parse descriptor buffer and return the pointer starting the given |
| 2026 | * descriptor type. |
| 2027 | */ |
| 2028 | void *snd_usb_find_desc(void *descstart, int desclen, void *after, u8 dtype) |
| 2029 | { |
| 2030 | u8 *p, *end, *next; |
| 2031 | |
| 2032 | p = descstart; |
| 2033 | end = p + desclen; |
| 2034 | for (; p < end;) { |
| 2035 | if (p[0] < 2) |
| 2036 | return NULL; |
| 2037 | next = p + p[0]; |
| 2038 | if (next > end) |
| 2039 | return NULL; |
| 2040 | if (p[1] == dtype && (!after || (void *)p > after)) { |
| 2041 | return p; |
| 2042 | } |
| 2043 | p = next; |
| 2044 | } |
| 2045 | return NULL; |
| 2046 | } |
| 2047 | |
| 2048 | /* |
| 2049 | * find a class-specified interface descriptor with the given subtype. |
| 2050 | */ |
| 2051 | void *snd_usb_find_csint_desc(void *buffer, int buflen, void *after, u8 dsubtype) |
| 2052 | { |
| 2053 | unsigned char *p = after; |
| 2054 | |
| 2055 | while ((p = snd_usb_find_desc(buffer, buflen, p, |
| 2056 | USB_DT_CS_INTERFACE)) != NULL) { |
| 2057 | if (p[0] >= 3 && p[2] == dsubtype) |
| 2058 | return p; |
| 2059 | } |
| 2060 | return NULL; |
| 2061 | } |
| 2062 | |
| 2063 | /* |
| 2064 | * Wrapper for usb_control_msg(). |
| 2065 | * Allocates a temp buffer to prevent dmaing from/to the stack. |
| 2066 | */ |
| 2067 | int snd_usb_ctl_msg(struct usb_device *dev, unsigned int pipe, __u8 request, |
| 2068 | __u8 requesttype, __u16 value, __u16 index, void *data, |
| 2069 | __u16 size, int timeout) |
| 2070 | { |
| 2071 | int err; |
| 2072 | void *buf = NULL; |
| 2073 | |
| 2074 | if (size > 0) { |
Alexey Dobriyan | 52978be | 2006-09-30 23:27:21 -0700 | [diff] [blame] | 2075 | buf = kmemdup(data, size, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2076 | if (!buf) |
| 2077 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2078 | } |
| 2079 | err = usb_control_msg(dev, pipe, request, requesttype, |
| 2080 | value, index, buf, size, timeout); |
| 2081 | if (size > 0) { |
| 2082 | memcpy(data, buf, size); |
| 2083 | kfree(buf); |
| 2084 | } |
| 2085 | return err; |
| 2086 | } |
| 2087 | |
| 2088 | |
| 2089 | /* |
| 2090 | * entry point for linux usb interface |
| 2091 | */ |
| 2092 | |
| 2093 | static int usb_audio_probe(struct usb_interface *intf, |
| 2094 | const struct usb_device_id *id); |
| 2095 | static void usb_audio_disconnect(struct usb_interface *intf); |
Andrew Morton | 93521d2 | 2007-12-24 14:40:56 +0100 | [diff] [blame] | 2096 | |
| 2097 | #ifdef CONFIG_PM |
Oliver Neukum | f85bf29 | 2007-12-14 14:42:41 +0100 | [diff] [blame] | 2098 | static int usb_audio_suspend(struct usb_interface *intf, pm_message_t message); |
| 2099 | static int usb_audio_resume(struct usb_interface *intf); |
Andrew Morton | 93521d2 | 2007-12-24 14:40:56 +0100 | [diff] [blame] | 2100 | #else |
| 2101 | #define usb_audio_suspend NULL |
| 2102 | #define usb_audio_resume NULL |
| 2103 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2104 | |
| 2105 | static struct usb_device_id usb_audio_ids [] = { |
| 2106 | #include "usbquirks.h" |
| 2107 | { .match_flags = (USB_DEVICE_ID_MATCH_INT_CLASS | USB_DEVICE_ID_MATCH_INT_SUBCLASS), |
| 2108 | .bInterfaceClass = USB_CLASS_AUDIO, |
| 2109 | .bInterfaceSubClass = USB_SUBCLASS_AUDIO_CONTROL }, |
| 2110 | { } /* Terminating entry */ |
| 2111 | }; |
| 2112 | |
| 2113 | MODULE_DEVICE_TABLE (usb, usb_audio_ids); |
| 2114 | |
| 2115 | static struct usb_driver usb_audio_driver = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2116 | .name = "snd-usb-audio", |
| 2117 | .probe = usb_audio_probe, |
| 2118 | .disconnect = usb_audio_disconnect, |
Oliver Neukum | f85bf29 | 2007-12-14 14:42:41 +0100 | [diff] [blame] | 2119 | .suspend = usb_audio_suspend, |
| 2120 | .resume = usb_audio_resume, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2121 | .id_table = usb_audio_ids, |
| 2122 | }; |
| 2123 | |
| 2124 | |
Takashi Iwai | 727f317 | 2006-08-04 19:08:03 +0200 | [diff] [blame] | 2125 | #if defined(CONFIG_PROC_FS) && defined(CONFIG_SND_VERBOSE_PROCFS) |
Jaroslav Kysela | 21a3479 | 2006-01-13 09:12:11 +0100 | [diff] [blame] | 2126 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2127 | /* |
| 2128 | * proc interface for list the supported pcm formats |
| 2129 | */ |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 2130 | static void proc_dump_substream_formats(struct snd_usb_substream *subs, struct snd_info_buffer *buffer) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2131 | { |
| 2132 | struct list_head *p; |
| 2133 | static char *sync_types[4] = { |
| 2134 | "NONE", "ASYNC", "ADAPTIVE", "SYNC" |
| 2135 | }; |
| 2136 | |
| 2137 | list_for_each(p, &subs->fmt_list) { |
| 2138 | struct audioformat *fp; |
| 2139 | fp = list_entry(p, struct audioformat, list); |
| 2140 | snd_iprintf(buffer, " Interface %d\n", fp->iface); |
| 2141 | snd_iprintf(buffer, " Altset %d\n", fp->altsetting); |
Takashi Iwai | 6e5265e | 2009-09-08 14:26:51 +0200 | [diff] [blame] | 2142 | snd_iprintf(buffer, " Format: %s\n", |
| 2143 | snd_pcm_format_name(fp->format)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2144 | snd_iprintf(buffer, " Channels: %d\n", fp->channels); |
| 2145 | snd_iprintf(buffer, " Endpoint: %d %s (%s)\n", |
| 2146 | fp->endpoint & USB_ENDPOINT_NUMBER_MASK, |
| 2147 | fp->endpoint & USB_DIR_IN ? "IN" : "OUT", |
| 2148 | sync_types[(fp->ep_attr & EP_ATTR_MASK) >> 2]); |
| 2149 | if (fp->rates & SNDRV_PCM_RATE_CONTINUOUS) { |
| 2150 | snd_iprintf(buffer, " Rates: %d - %d (continuous)\n", |
| 2151 | fp->rate_min, fp->rate_max); |
| 2152 | } else { |
| 2153 | unsigned int i; |
| 2154 | snd_iprintf(buffer, " Rates: "); |
| 2155 | for (i = 0; i < fp->nr_rates; i++) { |
| 2156 | if (i > 0) |
| 2157 | snd_iprintf(buffer, ", "); |
| 2158 | snd_iprintf(buffer, "%d", fp->rate_table[i]); |
| 2159 | } |
| 2160 | snd_iprintf(buffer, "\n"); |
| 2161 | } |
Clemens Ladisch | 744b89e | 2009-04-03 09:45:01 +0200 | [diff] [blame] | 2162 | if (snd_usb_get_speed(subs->dev) == USB_SPEED_HIGH) |
| 2163 | snd_iprintf(buffer, " Data packet interval: %d us\n", |
| 2164 | 125 * (1 << fp->datainterval)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2165 | // snd_iprintf(buffer, " Max Packet Size = %d\n", fp->maxpacksize); |
Andreas Bergmeier | b9d710b | 2009-01-24 12:15:14 +0100 | [diff] [blame] | 2166 | // snd_iprintf(buffer, " EP Attribute = %#x\n", fp->attributes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2167 | } |
| 2168 | } |
| 2169 | |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 2170 | static void proc_dump_substream_status(struct snd_usb_substream *subs, struct snd_info_buffer *buffer) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2171 | { |
| 2172 | if (subs->running) { |
| 2173 | unsigned int i; |
| 2174 | snd_iprintf(buffer, " Status: Running\n"); |
| 2175 | snd_iprintf(buffer, " Interface = %d\n", subs->interface); |
| 2176 | snd_iprintf(buffer, " Altset = %d\n", subs->format); |
| 2177 | snd_iprintf(buffer, " URBs = %d [ ", subs->nurbs); |
| 2178 | for (i = 0; i < subs->nurbs; i++) |
| 2179 | snd_iprintf(buffer, "%d ", subs->dataurb[i].packets); |
| 2180 | snd_iprintf(buffer, "]\n"); |
| 2181 | snd_iprintf(buffer, " Packet Size = %d\n", subs->curpacksize); |
Clemens Ladisch | 08fe158 | 2005-04-22 15:33:01 +0200 | [diff] [blame] | 2182 | snd_iprintf(buffer, " Momentary freq = %u Hz (%#x.%04x)\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2183 | snd_usb_get_speed(subs->dev) == USB_SPEED_FULL |
| 2184 | ? get_full_speed_hz(subs->freqm) |
Clemens Ladisch | 08fe158 | 2005-04-22 15:33:01 +0200 | [diff] [blame] | 2185 | : get_high_speed_hz(subs->freqm), |
| 2186 | subs->freqm >> 16, subs->freqm & 0xffff); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2187 | } else { |
| 2188 | snd_iprintf(buffer, " Status: Stop\n"); |
| 2189 | } |
| 2190 | } |
| 2191 | |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 2192 | static void proc_pcm_format_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2193 | { |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 2194 | struct snd_usb_stream *stream = entry->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2195 | |
| 2196 | snd_iprintf(buffer, "%s : %s\n", stream->chip->card->longname, stream->pcm->name); |
| 2197 | |
| 2198 | if (stream->substream[SNDRV_PCM_STREAM_PLAYBACK].num_formats) { |
| 2199 | snd_iprintf(buffer, "\nPlayback:\n"); |
| 2200 | proc_dump_substream_status(&stream->substream[SNDRV_PCM_STREAM_PLAYBACK], buffer); |
| 2201 | proc_dump_substream_formats(&stream->substream[SNDRV_PCM_STREAM_PLAYBACK], buffer); |
| 2202 | } |
| 2203 | if (stream->substream[SNDRV_PCM_STREAM_CAPTURE].num_formats) { |
| 2204 | snd_iprintf(buffer, "\nCapture:\n"); |
| 2205 | proc_dump_substream_status(&stream->substream[SNDRV_PCM_STREAM_CAPTURE], buffer); |
| 2206 | proc_dump_substream_formats(&stream->substream[SNDRV_PCM_STREAM_CAPTURE], buffer); |
| 2207 | } |
| 2208 | } |
| 2209 | |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 2210 | static void proc_pcm_format_add(struct snd_usb_stream *stream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2211 | { |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 2212 | struct snd_info_entry *entry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2213 | char name[32]; |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 2214 | struct snd_card *card = stream->chip->card; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2215 | |
| 2216 | sprintf(name, "stream%d", stream->pcm_index); |
Pavel Machek | 07f51a7 | 2008-04-14 13:15:56 +0200 | [diff] [blame] | 2217 | if (!snd_card_proc_new(card, name, &entry)) |
Takashi Iwai | bf85020 | 2006-04-28 15:13:41 +0200 | [diff] [blame] | 2218 | snd_info_set_text_ops(entry, stream, proc_pcm_format_read); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2219 | } |
| 2220 | |
Jaroslav Kysela | 21a3479 | 2006-01-13 09:12:11 +0100 | [diff] [blame] | 2221 | #else |
| 2222 | |
| 2223 | static inline void proc_pcm_format_add(struct snd_usb_stream *stream) |
| 2224 | { |
| 2225 | } |
| 2226 | |
| 2227 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2228 | |
| 2229 | /* |
| 2230 | * initialize the substream instance. |
| 2231 | */ |
| 2232 | |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 2233 | static void init_substream(struct snd_usb_stream *as, int stream, struct audioformat *fp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2234 | { |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 2235 | struct snd_usb_substream *subs = &as->substream[stream]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2236 | |
| 2237 | INIT_LIST_HEAD(&subs->fmt_list); |
| 2238 | spin_lock_init(&subs->lock); |
| 2239 | |
| 2240 | subs->stream = as; |
| 2241 | subs->direction = stream; |
| 2242 | subs->dev = as->chip->dev; |
Clemens Ladisch | d513202 | 2008-02-25 11:01:00 +0100 | [diff] [blame] | 2243 | if (snd_usb_get_speed(subs->dev) == USB_SPEED_FULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2244 | subs->ops = audio_urb_ops[stream]; |
Clemens Ladisch | d513202 | 2008-02-25 11:01:00 +0100 | [diff] [blame] | 2245 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2246 | subs->ops = audio_urb_ops_high_speed[stream]; |
Clemens Ladisch | d513202 | 2008-02-25 11:01:00 +0100 | [diff] [blame] | 2247 | switch (as->chip->usb_id) { |
| 2248 | case USB_ID(0x041e, 0x3f02): /* E-Mu 0202 USB */ |
| 2249 | case USB_ID(0x041e, 0x3f04): /* E-Mu 0404 USB */ |
Eran Tromer | 97c889a | 2008-09-26 01:07:03 -0400 | [diff] [blame] | 2250 | case USB_ID(0x041e, 0x3f0a): /* E-Mu Tracker Pre */ |
Clemens Ladisch | d513202 | 2008-02-25 11:01:00 +0100 | [diff] [blame] | 2251 | subs->ops.retire_sync = retire_playback_sync_urb_hs_emu; |
| 2252 | break; |
| 2253 | } |
| 2254 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2255 | snd_pcm_set_ops(as->pcm, stream, |
| 2256 | stream == SNDRV_PCM_STREAM_PLAYBACK ? |
| 2257 | &snd_usb_playback_ops : &snd_usb_capture_ops); |
| 2258 | |
| 2259 | list_add_tail(&fp->list, &subs->fmt_list); |
| 2260 | subs->formats |= 1ULL << fp->format; |
| 2261 | subs->endpoint = fp->endpoint; |
| 2262 | subs->num_formats++; |
| 2263 | subs->fmt_type = fp->fmt_type; |
| 2264 | } |
| 2265 | |
| 2266 | |
| 2267 | /* |
| 2268 | * free a substream |
| 2269 | */ |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 2270 | static void free_substream(struct snd_usb_substream *subs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2271 | { |
| 2272 | struct list_head *p, *n; |
| 2273 | |
Pavel Machek | 07f51a7 | 2008-04-14 13:15:56 +0200 | [diff] [blame] | 2274 | if (!subs->num_formats) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2275 | return; /* not initialized */ |
| 2276 | list_for_each_safe(p, n, &subs->fmt_list) { |
| 2277 | struct audioformat *fp = list_entry(p, struct audioformat, list); |
| 2278 | kfree(fp->rate_table); |
| 2279 | kfree(fp); |
| 2280 | } |
Takashi Iwai | 8fec560 | 2007-02-01 11:50:56 +0100 | [diff] [blame] | 2281 | kfree(subs->rate_list.list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2282 | } |
| 2283 | |
| 2284 | |
| 2285 | /* |
| 2286 | * free a usb stream instance |
| 2287 | */ |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 2288 | static void snd_usb_audio_stream_free(struct snd_usb_stream *stream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2289 | { |
| 2290 | free_substream(&stream->substream[0]); |
| 2291 | free_substream(&stream->substream[1]); |
| 2292 | list_del(&stream->list); |
| 2293 | kfree(stream); |
| 2294 | } |
| 2295 | |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 2296 | static void snd_usb_audio_pcm_free(struct snd_pcm *pcm) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2297 | { |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 2298 | struct snd_usb_stream *stream = pcm->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2299 | if (stream) { |
| 2300 | stream->pcm = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2301 | snd_usb_audio_stream_free(stream); |
| 2302 | } |
| 2303 | } |
| 2304 | |
| 2305 | |
| 2306 | /* |
| 2307 | * add this endpoint to the chip instance. |
| 2308 | * if a stream with the same endpoint already exists, append to it. |
| 2309 | * if not, create a new pcm stream. |
| 2310 | */ |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 2311 | static int add_audio_endpoint(struct snd_usb_audio *chip, int stream, struct audioformat *fp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2312 | { |
| 2313 | struct list_head *p; |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 2314 | struct snd_usb_stream *as; |
| 2315 | struct snd_usb_substream *subs; |
| 2316 | struct snd_pcm *pcm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2317 | int err; |
| 2318 | |
| 2319 | list_for_each(p, &chip->pcm_list) { |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 2320 | as = list_entry(p, struct snd_usb_stream, list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2321 | if (as->fmt_type != fp->fmt_type) |
| 2322 | continue; |
| 2323 | subs = &as->substream[stream]; |
Pavel Machek | 07f51a7 | 2008-04-14 13:15:56 +0200 | [diff] [blame] | 2324 | if (!subs->endpoint) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2325 | continue; |
| 2326 | if (subs->endpoint == fp->endpoint) { |
| 2327 | list_add_tail(&fp->list, &subs->fmt_list); |
| 2328 | subs->num_formats++; |
| 2329 | subs->formats |= 1ULL << fp->format; |
| 2330 | return 0; |
| 2331 | } |
| 2332 | } |
| 2333 | /* look for an empty stream */ |
| 2334 | list_for_each(p, &chip->pcm_list) { |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 2335 | as = list_entry(p, struct snd_usb_stream, list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2336 | if (as->fmt_type != fp->fmt_type) |
| 2337 | continue; |
| 2338 | subs = &as->substream[stream]; |
| 2339 | if (subs->endpoint) |
| 2340 | continue; |
| 2341 | err = snd_pcm_new_stream(as->pcm, stream, 1); |
| 2342 | if (err < 0) |
| 2343 | return err; |
| 2344 | init_substream(as, stream, fp); |
| 2345 | return 0; |
| 2346 | } |
| 2347 | |
| 2348 | /* create a new pcm */ |
Panagiotis Issaris | 59feddb | 2006-07-25 15:28:03 +0200 | [diff] [blame] | 2349 | as = kzalloc(sizeof(*as), GFP_KERNEL); |
Pavel Machek | 07f51a7 | 2008-04-14 13:15:56 +0200 | [diff] [blame] | 2350 | if (!as) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2351 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2352 | as->pcm_index = chip->pcm_devs; |
| 2353 | as->chip = chip; |
| 2354 | as->fmt_type = fp->fmt_type; |
| 2355 | err = snd_pcm_new(chip->card, "USB Audio", chip->pcm_devs, |
| 2356 | stream == SNDRV_PCM_STREAM_PLAYBACK ? 1 : 0, |
| 2357 | stream == SNDRV_PCM_STREAM_PLAYBACK ? 0 : 1, |
| 2358 | &pcm); |
| 2359 | if (err < 0) { |
| 2360 | kfree(as); |
| 2361 | return err; |
| 2362 | } |
| 2363 | as->pcm = pcm; |
| 2364 | pcm->private_data = as; |
| 2365 | pcm->private_free = snd_usb_audio_pcm_free; |
| 2366 | pcm->info_flags = 0; |
| 2367 | if (chip->pcm_devs > 0) |
| 2368 | sprintf(pcm->name, "USB Audio #%d", chip->pcm_devs); |
| 2369 | else |
| 2370 | strcpy(pcm->name, "USB Audio"); |
| 2371 | |
| 2372 | init_substream(as, stream, fp); |
| 2373 | |
| 2374 | list_add(&as->list, &chip->pcm_list); |
| 2375 | chip->pcm_devs++; |
| 2376 | |
| 2377 | proc_pcm_format_add(as); |
| 2378 | |
| 2379 | return 0; |
| 2380 | } |
| 2381 | |
| 2382 | |
| 2383 | /* |
| 2384 | * check if the device uses big-endian samples |
| 2385 | */ |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 2386 | static int is_big_endian_format(struct snd_usb_audio *chip, struct audioformat *fp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2387 | { |
Clemens Ladisch | 27d10f5 | 2005-05-02 08:51:26 +0200 | [diff] [blame] | 2388 | switch (chip->usb_id) { |
| 2389 | case USB_ID(0x0763, 0x2001): /* M-Audio Quattro: captured data only */ |
| 2390 | if (fp->endpoint & USB_DIR_IN) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2391 | return 1; |
Clemens Ladisch | 27d10f5 | 2005-05-02 08:51:26 +0200 | [diff] [blame] | 2392 | break; |
| 2393 | case USB_ID(0x0763, 0x2003): /* M-Audio Audiophile USB */ |
Thibault Le Meur | f8c78b8 | 2007-07-12 11:26:35 +0200 | [diff] [blame] | 2394 | if (device_setup[chip->index] == 0x00 || |
| 2395 | fp->altsetting==1 || fp->altsetting==2 || fp->altsetting==3) |
| 2396 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2397 | } |
| 2398 | return 0; |
| 2399 | } |
| 2400 | |
| 2401 | /* |
| 2402 | * parse the audio format type I descriptor |
| 2403 | * and returns the corresponding pcm format |
| 2404 | * |
| 2405 | * @dev: usb device |
| 2406 | * @fp: audioformat record |
| 2407 | * @format: the format tag (wFormatTag) |
| 2408 | * @fmt: the format type descriptor |
| 2409 | */ |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 2410 | static int parse_audio_format_i_type(struct snd_usb_audio *chip, struct audioformat *fp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2411 | int format, unsigned char *fmt) |
| 2412 | { |
| 2413 | int pcm_format; |
| 2414 | int sample_width, sample_bytes; |
| 2415 | |
| 2416 | /* FIXME: correct endianess and sign? */ |
| 2417 | pcm_format = -1; |
| 2418 | sample_width = fmt[6]; |
| 2419 | sample_bytes = fmt[5]; |
| 2420 | switch (format) { |
| 2421 | case 0: /* some devices don't define this correctly... */ |
| 2422 | snd_printdd(KERN_INFO "%d:%u:%d : format type 0 is detected, processed as PCM\n", |
Clemens Ladisch | 27d10f5 | 2005-05-02 08:51:26 +0200 | [diff] [blame] | 2423 | chip->dev->devnum, fp->iface, fp->altsetting); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2424 | /* fall-through */ |
| 2425 | case USB_AUDIO_FORMAT_PCM: |
| 2426 | if (sample_width > sample_bytes * 8) { |
| 2427 | snd_printk(KERN_INFO "%d:%u:%d : sample bitwidth %d in over sample bytes %d\n", |
Clemens Ladisch | 27d10f5 | 2005-05-02 08:51:26 +0200 | [diff] [blame] | 2428 | chip->dev->devnum, fp->iface, fp->altsetting, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2429 | sample_width, sample_bytes); |
| 2430 | } |
| 2431 | /* check the format byte size */ |
| 2432 | switch (fmt[5]) { |
| 2433 | case 1: |
| 2434 | pcm_format = SNDRV_PCM_FORMAT_S8; |
| 2435 | break; |
| 2436 | case 2: |
Clemens Ladisch | 27d10f5 | 2005-05-02 08:51:26 +0200 | [diff] [blame] | 2437 | if (is_big_endian_format(chip, fp)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2438 | pcm_format = SNDRV_PCM_FORMAT_S16_BE; /* grrr, big endian!! */ |
| 2439 | else |
| 2440 | pcm_format = SNDRV_PCM_FORMAT_S16_LE; |
| 2441 | break; |
| 2442 | case 3: |
Clemens Ladisch | 27d10f5 | 2005-05-02 08:51:26 +0200 | [diff] [blame] | 2443 | if (is_big_endian_format(chip, fp)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2444 | pcm_format = SNDRV_PCM_FORMAT_S24_3BE; /* grrr, big endian!! */ |
| 2445 | else |
| 2446 | pcm_format = SNDRV_PCM_FORMAT_S24_3LE; |
| 2447 | break; |
| 2448 | case 4: |
| 2449 | pcm_format = SNDRV_PCM_FORMAT_S32_LE; |
| 2450 | break; |
| 2451 | default: |
| 2452 | snd_printk(KERN_INFO "%d:%u:%d : unsupported sample bitwidth %d in %d bytes\n", |
Clemens Ladisch | 27d10f5 | 2005-05-02 08:51:26 +0200 | [diff] [blame] | 2453 | chip->dev->devnum, fp->iface, |
| 2454 | fp->altsetting, sample_width, sample_bytes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2455 | break; |
| 2456 | } |
| 2457 | break; |
| 2458 | case USB_AUDIO_FORMAT_PCM8: |
Pavel Machek | 2a56f51 | 2008-04-14 13:14:22 +0200 | [diff] [blame] | 2459 | pcm_format = SNDRV_PCM_FORMAT_U8; |
| 2460 | |
| 2461 | /* Dallas DS4201 workaround: it advertises U8 format, but really |
| 2462 | supports S8. */ |
Clemens Ladisch | 27d10f5 | 2005-05-02 08:51:26 +0200 | [diff] [blame] | 2463 | if (chip->usb_id == USB_ID(0x04fa, 0x4201)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2464 | pcm_format = SNDRV_PCM_FORMAT_S8; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2465 | break; |
| 2466 | case USB_AUDIO_FORMAT_IEEE_FLOAT: |
| 2467 | pcm_format = SNDRV_PCM_FORMAT_FLOAT_LE; |
| 2468 | break; |
| 2469 | case USB_AUDIO_FORMAT_ALAW: |
| 2470 | pcm_format = SNDRV_PCM_FORMAT_A_LAW; |
| 2471 | break; |
| 2472 | case USB_AUDIO_FORMAT_MU_LAW: |
| 2473 | pcm_format = SNDRV_PCM_FORMAT_MU_LAW; |
| 2474 | break; |
| 2475 | default: |
| 2476 | snd_printk(KERN_INFO "%d:%u:%d : unsupported format type %d\n", |
Clemens Ladisch | 27d10f5 | 2005-05-02 08:51:26 +0200 | [diff] [blame] | 2477 | chip->dev->devnum, fp->iface, fp->altsetting, format); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2478 | break; |
| 2479 | } |
| 2480 | return pcm_format; |
| 2481 | } |
| 2482 | |
| 2483 | |
| 2484 | /* |
| 2485 | * parse the format descriptor and stores the possible sample rates |
| 2486 | * on the audioformat table. |
| 2487 | * |
| 2488 | * @dev: usb device |
| 2489 | * @fp: audioformat record |
| 2490 | * @fmt: the format descriptor |
| 2491 | * @offset: the start offset of descriptor pointing the rate type |
| 2492 | * (7 for type I and II, 8 for type II) |
| 2493 | */ |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 2494 | static int parse_audio_format_rates(struct snd_usb_audio *chip, struct audioformat *fp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2495 | unsigned char *fmt, int offset) |
| 2496 | { |
| 2497 | int nr_rates = fmt[offset]; |
Clemens Ladisch | 918f3a0 | 2007-08-13 17:40:54 +0200 | [diff] [blame] | 2498 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2499 | if (fmt[0] < offset + 1 + 3 * (nr_rates ? nr_rates : 2)) { |
| 2500 | snd_printk(KERN_ERR "%d:%u:%d : invalid FORMAT_TYPE desc\n", |
Clemens Ladisch | 27d10f5 | 2005-05-02 08:51:26 +0200 | [diff] [blame] | 2501 | chip->dev->devnum, fp->iface, fp->altsetting); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2502 | return -1; |
| 2503 | } |
| 2504 | |
| 2505 | if (nr_rates) { |
| 2506 | /* |
| 2507 | * build the rate table and bitmap flags |
| 2508 | */ |
Clemens Ladisch | 918f3a0 | 2007-08-13 17:40:54 +0200 | [diff] [blame] | 2509 | int r, idx; |
Clemens Ladisch | 918f3a0 | 2007-08-13 17:40:54 +0200 | [diff] [blame] | 2510 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2511 | fp->rate_table = kmalloc(sizeof(int) * nr_rates, GFP_KERNEL); |
| 2512 | if (fp->rate_table == NULL) { |
| 2513 | snd_printk(KERN_ERR "cannot malloc\n"); |
| 2514 | return -1; |
| 2515 | } |
| 2516 | |
Takashi Iwai | 0412558 | 2009-02-16 22:48:12 +0100 | [diff] [blame] | 2517 | fp->nr_rates = 0; |
| 2518 | fp->rate_min = fp->rate_max = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2519 | for (r = 0, idx = offset + 1; r < nr_rates; r++, idx += 3) { |
Clemens Ladisch | 987411b | 2006-11-20 14:14:39 +0100 | [diff] [blame] | 2520 | unsigned int rate = combine_triple(&fmt[idx]); |
Takashi Iwai | 0412558 | 2009-02-16 22:48:12 +0100 | [diff] [blame] | 2521 | if (!rate) |
| 2522 | continue; |
Clemens Ladisch | 987411b | 2006-11-20 14:14:39 +0100 | [diff] [blame] | 2523 | /* C-Media CM6501 mislabels its 96 kHz altsetting */ |
| 2524 | if (rate == 48000 && nr_rates == 1 && |
Joris van Rantwijk | 3b03cc5 | 2009-02-16 22:58:23 +0100 | [diff] [blame] | 2525 | (chip->usb_id == USB_ID(0x0d8c, 0x0201) || |
| 2526 | chip->usb_id == USB_ID(0x0d8c, 0x0102)) && |
Clemens Ladisch | 987411b | 2006-11-20 14:14:39 +0100 | [diff] [blame] | 2527 | fp->altsetting == 5 && fp->maxpacksize == 392) |
| 2528 | rate = 96000; |
Takashi Iwai | 0412558 | 2009-02-16 22:48:12 +0100 | [diff] [blame] | 2529 | fp->rate_table[fp->nr_rates] = rate; |
| 2530 | if (!fp->rate_min || rate < fp->rate_min) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2531 | fp->rate_min = rate; |
Takashi Iwai | 0412558 | 2009-02-16 22:48:12 +0100 | [diff] [blame] | 2532 | if (!fp->rate_max || rate > fp->rate_max) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2533 | fp->rate_max = rate; |
Clemens Ladisch | 918f3a0 | 2007-08-13 17:40:54 +0200 | [diff] [blame] | 2534 | fp->rates |= snd_pcm_rate_to_rate_bit(rate); |
Takashi Iwai | 0412558 | 2009-02-16 22:48:12 +0100 | [diff] [blame] | 2535 | fp->nr_rates++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2536 | } |
Takashi Iwai | 0412558 | 2009-02-16 22:48:12 +0100 | [diff] [blame] | 2537 | if (!fp->nr_rates) { |
Gregor Jasny | beb6011 | 2007-01-31 12:27:39 +0100 | [diff] [blame] | 2538 | hwc_debug("All rates were zero. Skipping format!\n"); |
| 2539 | return -1; |
| 2540 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2541 | } else { |
| 2542 | /* continuous rates */ |
| 2543 | fp->rates = SNDRV_PCM_RATE_CONTINUOUS; |
| 2544 | fp->rate_min = combine_triple(&fmt[offset + 1]); |
| 2545 | fp->rate_max = combine_triple(&fmt[offset + 4]); |
| 2546 | } |
| 2547 | return 0; |
| 2548 | } |
| 2549 | |
| 2550 | /* |
| 2551 | * parse the format type I and III descriptors |
| 2552 | */ |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 2553 | static int parse_audio_format_i(struct snd_usb_audio *chip, struct audioformat *fp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2554 | int format, unsigned char *fmt) |
| 2555 | { |
| 2556 | int pcm_format; |
| 2557 | |
| 2558 | if (fmt[3] == USB_FORMAT_TYPE_III) { |
| 2559 | /* FIXME: the format type is really IECxxx |
| 2560 | * but we give normal PCM format to get the existing |
| 2561 | * apps working... |
| 2562 | */ |
Thibault Le Meur | cac19c3 | 2007-07-13 11:50:23 +0200 | [diff] [blame] | 2563 | switch (chip->usb_id) { |
| 2564 | |
| 2565 | case USB_ID(0x0763, 0x2003): /* M-Audio Audiophile USB */ |
| 2566 | if (device_setup[chip->index] == 0x00 && |
| 2567 | fp->altsetting == 6) |
| 2568 | pcm_format = SNDRV_PCM_FORMAT_S16_BE; |
| 2569 | else |
| 2570 | pcm_format = SNDRV_PCM_FORMAT_S16_LE; |
| 2571 | break; |
| 2572 | default: |
| 2573 | pcm_format = SNDRV_PCM_FORMAT_S16_LE; |
| 2574 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2575 | } else { |
Clemens Ladisch | 27d10f5 | 2005-05-02 08:51:26 +0200 | [diff] [blame] | 2576 | pcm_format = parse_audio_format_i_type(chip, fp, format, fmt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2577 | if (pcm_format < 0) |
| 2578 | return -1; |
| 2579 | } |
| 2580 | fp->format = pcm_format; |
| 2581 | fp->channels = fmt[4]; |
| 2582 | if (fp->channels < 1) { |
| 2583 | snd_printk(KERN_ERR "%d:%u:%d : invalid channels %d\n", |
Clemens Ladisch | 27d10f5 | 2005-05-02 08:51:26 +0200 | [diff] [blame] | 2584 | chip->dev->devnum, fp->iface, fp->altsetting, fp->channels); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2585 | return -1; |
| 2586 | } |
Clemens Ladisch | 27d10f5 | 2005-05-02 08:51:26 +0200 | [diff] [blame] | 2587 | return parse_audio_format_rates(chip, fp, fmt, 7); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2588 | } |
| 2589 | |
| 2590 | /* |
| 2591 | * prase the format type II descriptor |
| 2592 | */ |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 2593 | static int parse_audio_format_ii(struct snd_usb_audio *chip, struct audioformat *fp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2594 | int format, unsigned char *fmt) |
| 2595 | { |
| 2596 | int brate, framesize; |
| 2597 | switch (format) { |
| 2598 | case USB_AUDIO_FORMAT_AC3: |
| 2599 | /* FIXME: there is no AC3 format defined yet */ |
| 2600 | // fp->format = SNDRV_PCM_FORMAT_AC3; |
| 2601 | fp->format = SNDRV_PCM_FORMAT_U8; /* temporarily hack to receive byte streams */ |
| 2602 | break; |
| 2603 | case USB_AUDIO_FORMAT_MPEG: |
| 2604 | fp->format = SNDRV_PCM_FORMAT_MPEG; |
| 2605 | break; |
| 2606 | default: |
Andreas Bergmeier | b9d710b | 2009-01-24 12:15:14 +0100 | [diff] [blame] | 2607 | snd_printd(KERN_INFO "%d:%u:%d : unknown format tag %#x is detected. processed as MPEG.\n", |
Clemens Ladisch | 27d10f5 | 2005-05-02 08:51:26 +0200 | [diff] [blame] | 2608 | chip->dev->devnum, fp->iface, fp->altsetting, format); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2609 | fp->format = SNDRV_PCM_FORMAT_MPEG; |
| 2610 | break; |
| 2611 | } |
| 2612 | fp->channels = 1; |
| 2613 | brate = combine_word(&fmt[4]); /* fmt[4,5] : wMaxBitRate (in kbps) */ |
| 2614 | framesize = combine_word(&fmt[6]); /* fmt[6,7]: wSamplesPerFrame */ |
| 2615 | snd_printd(KERN_INFO "found format II with max.bitrate = %d, frame size=%d\n", brate, framesize); |
| 2616 | fp->frame_size = framesize; |
Clemens Ladisch | 27d10f5 | 2005-05-02 08:51:26 +0200 | [diff] [blame] | 2617 | return parse_audio_format_rates(chip, fp, fmt, 8); /* fmt[8..] sample rates */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2618 | } |
| 2619 | |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 2620 | static int parse_audio_format(struct snd_usb_audio *chip, struct audioformat *fp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2621 | int format, unsigned char *fmt, int stream) |
| 2622 | { |
| 2623 | int err; |
| 2624 | |
| 2625 | switch (fmt[3]) { |
| 2626 | case USB_FORMAT_TYPE_I: |
| 2627 | case USB_FORMAT_TYPE_III: |
Clemens Ladisch | 27d10f5 | 2005-05-02 08:51:26 +0200 | [diff] [blame] | 2628 | err = parse_audio_format_i(chip, fp, format, fmt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2629 | break; |
| 2630 | case USB_FORMAT_TYPE_II: |
Clemens Ladisch | 27d10f5 | 2005-05-02 08:51:26 +0200 | [diff] [blame] | 2631 | err = parse_audio_format_ii(chip, fp, format, fmt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2632 | break; |
| 2633 | default: |
| 2634 | snd_printd(KERN_INFO "%d:%u:%d : format type %d is not supported yet\n", |
Clemens Ladisch | 27d10f5 | 2005-05-02 08:51:26 +0200 | [diff] [blame] | 2635 | chip->dev->devnum, fp->iface, fp->altsetting, fmt[3]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2636 | return -1; |
| 2637 | } |
| 2638 | fp->fmt_type = fmt[3]; |
| 2639 | if (err < 0) |
| 2640 | return err; |
| 2641 | #if 1 |
Clemens Ladisch | 3315937 | 2006-01-13 08:11:22 +0100 | [diff] [blame] | 2642 | /* FIXME: temporary hack for extigy/audigy 2 nx/zs */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2643 | /* extigy apparently supports sample rates other than 48k |
| 2644 | * but not in ordinary way. so we enable only 48k atm. |
| 2645 | */ |
Clemens Ladisch | 27d10f5 | 2005-05-02 08:51:26 +0200 | [diff] [blame] | 2646 | if (chip->usb_id == USB_ID(0x041e, 0x3000) || |
Clemens Ladisch | 3315937 | 2006-01-13 08:11:22 +0100 | [diff] [blame] | 2647 | chip->usb_id == USB_ID(0x041e, 0x3020) || |
| 2648 | chip->usb_id == USB_ID(0x041e, 0x3061)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2649 | if (fmt[3] == USB_FORMAT_TYPE_I && |
Clemens Ladisch | 8c1872d | 2005-04-28 09:31:53 +0200 | [diff] [blame] | 2650 | fp->rates != SNDRV_PCM_RATE_48000 && |
| 2651 | fp->rates != SNDRV_PCM_RATE_96000) |
Clemens Ladisch | b4d3f9d | 2005-06-27 08:18:27 +0200 | [diff] [blame] | 2652 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2653 | } |
| 2654 | #endif |
| 2655 | return 0; |
| 2656 | } |
| 2657 | |
Clemens Ladisch | 744b89e | 2009-04-03 09:45:01 +0200 | [diff] [blame] | 2658 | static unsigned char parse_datainterval(struct snd_usb_audio *chip, |
| 2659 | struct usb_host_interface *alts) |
| 2660 | { |
| 2661 | if (snd_usb_get_speed(chip->dev) == USB_SPEED_HIGH && |
| 2662 | get_endpoint(alts, 0)->bInterval >= 1 && |
| 2663 | get_endpoint(alts, 0)->bInterval <= 4) |
| 2664 | return get_endpoint(alts, 0)->bInterval - 1; |
| 2665 | else |
| 2666 | return 0; |
| 2667 | } |
| 2668 | |
Thibault LE MEUR | e311334 | 2006-03-14 11:44:53 +0100 | [diff] [blame] | 2669 | static int audiophile_skip_setting_quirk(struct snd_usb_audio *chip, |
| 2670 | int iface, int altno); |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 2671 | static int parse_audio_endpoints(struct snd_usb_audio *chip, int iface_no) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2672 | { |
| 2673 | struct usb_device *dev; |
| 2674 | struct usb_interface *iface; |
| 2675 | struct usb_host_interface *alts; |
| 2676 | struct usb_interface_descriptor *altsd; |
| 2677 | int i, altno, err, stream; |
| 2678 | int format; |
Clemens Ladisch | 8886f33 | 2009-07-13 13:21:58 +0200 | [diff] [blame] | 2679 | struct audioformat *fp = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2680 | unsigned char *fmt, *csep; |
Pavel Machek | b9d43bc | 2008-04-14 13:12:47 +0200 | [diff] [blame] | 2681 | int num; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2682 | |
| 2683 | dev = chip->dev; |
| 2684 | |
| 2685 | /* parse the interface's altsettings */ |
| 2686 | iface = usb_ifnum_to_if(dev, iface_no); |
Pavel Machek | b9d43bc | 2008-04-14 13:12:47 +0200 | [diff] [blame] | 2687 | |
| 2688 | num = iface->num_altsetting; |
| 2689 | |
| 2690 | /* |
| 2691 | * Dallas DS4201 workaround: It presents 5 altsettings, but the last |
| 2692 | * one misses syncpipe, and does not produce any sound. |
| 2693 | */ |
| 2694 | if (chip->usb_id == USB_ID(0x04fa, 0x4201)) |
| 2695 | num = 4; |
| 2696 | |
| 2697 | for (i = 0; i < num; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2698 | alts = &iface->altsetting[i]; |
| 2699 | altsd = get_iface_desc(alts); |
| 2700 | /* skip invalid one */ |
| 2701 | if ((altsd->bInterfaceClass != USB_CLASS_AUDIO && |
| 2702 | altsd->bInterfaceClass != USB_CLASS_VENDOR_SPEC) || |
| 2703 | (altsd->bInterfaceSubClass != USB_SUBCLASS_AUDIO_STREAMING && |
| 2704 | altsd->bInterfaceSubClass != USB_SUBCLASS_VENDOR_SPEC) || |
| 2705 | altsd->bNumEndpoints < 1 || |
| 2706 | le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize) == 0) |
| 2707 | continue; |
| 2708 | /* must be isochronous */ |
| 2709 | if ((get_endpoint(alts, 0)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != |
| 2710 | USB_ENDPOINT_XFER_ISOC) |
| 2711 | continue; |
| 2712 | /* check direction */ |
| 2713 | stream = (get_endpoint(alts, 0)->bEndpointAddress & USB_DIR_IN) ? |
| 2714 | SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK; |
| 2715 | altno = altsd->bAlternateSetting; |
Thibault LE MEUR | e311334 | 2006-03-14 11:44:53 +0100 | [diff] [blame] | 2716 | |
| 2717 | /* audiophile usb: skip altsets incompatible with device_setup |
| 2718 | */ |
| 2719 | if (chip->usb_id == USB_ID(0x0763, 0x2003) && |
| 2720 | audiophile_skip_setting_quirk(chip, iface_no, altno)) |
| 2721 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2722 | |
| 2723 | /* get audio formats */ |
| 2724 | fmt = snd_usb_find_csint_desc(alts->extra, alts->extralen, NULL, AS_GENERAL); |
| 2725 | if (!fmt) { |
| 2726 | snd_printk(KERN_ERR "%d:%u:%d : AS_GENERAL descriptor not found\n", |
| 2727 | dev->devnum, iface_no, altno); |
| 2728 | continue; |
| 2729 | } |
| 2730 | |
| 2731 | if (fmt[0] < 7) { |
| 2732 | snd_printk(KERN_ERR "%d:%u:%d : invalid AS_GENERAL desc\n", |
| 2733 | dev->devnum, iface_no, altno); |
| 2734 | continue; |
| 2735 | } |
| 2736 | |
| 2737 | format = (fmt[6] << 8) | fmt[5]; /* remember the format value */ |
| 2738 | |
| 2739 | /* get format type */ |
| 2740 | fmt = snd_usb_find_csint_desc(alts->extra, alts->extralen, NULL, FORMAT_TYPE); |
| 2741 | if (!fmt) { |
| 2742 | snd_printk(KERN_ERR "%d:%u:%d : no FORMAT_TYPE desc\n", |
| 2743 | dev->devnum, iface_no, altno); |
| 2744 | continue; |
| 2745 | } |
| 2746 | if (fmt[0] < 8) { |
| 2747 | snd_printk(KERN_ERR "%d:%u:%d : invalid FORMAT_TYPE desc\n", |
| 2748 | dev->devnum, iface_no, altno); |
| 2749 | continue; |
| 2750 | } |
| 2751 | |
Clemens Ladisch | 8886f33 | 2009-07-13 13:21:58 +0200 | [diff] [blame] | 2752 | /* |
| 2753 | * Blue Microphones workaround: The last altsetting is identical |
| 2754 | * with the previous one, except for a larger packet size, but |
| 2755 | * is actually a mislabeled two-channel setting; ignore it. |
| 2756 | */ |
| 2757 | if (fmt[4] == 1 && fmt[5] == 2 && altno == 2 && num == 3 && |
| 2758 | fp && fp->altsetting == 1 && fp->channels == 1 && |
| 2759 | fp->format == SNDRV_PCM_FORMAT_S16_LE && |
| 2760 | le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize) == |
| 2761 | fp->maxpacksize * 2) |
| 2762 | continue; |
| 2763 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2764 | csep = snd_usb_find_desc(alts->endpoint[0].extra, alts->endpoint[0].extralen, NULL, USB_DT_CS_ENDPOINT); |
| 2765 | /* Creamware Noah has this descriptor after the 2nd endpoint */ |
| 2766 | if (!csep && altsd->bNumEndpoints >= 2) |
| 2767 | csep = snd_usb_find_desc(alts->endpoint[1].extra, alts->endpoint[1].extralen, NULL, USB_DT_CS_ENDPOINT); |
| 2768 | if (!csep || csep[0] < 7 || csep[2] != EP_GENERAL) { |
Takashi Iwai | f8c7579 | 2006-05-18 14:47:03 +0200 | [diff] [blame] | 2769 | snd_printk(KERN_WARNING "%d:%u:%d : no or invalid" |
Clemens Ladisch | faf8d11 | 2006-05-18 09:35:15 +0200 | [diff] [blame] | 2770 | " class specific endpoint descriptor\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2771 | dev->devnum, iface_no, altno); |
Clemens Ladisch | faf8d11 | 2006-05-18 09:35:15 +0200 | [diff] [blame] | 2772 | csep = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2773 | } |
| 2774 | |
Panagiotis Issaris | 59feddb | 2006-07-25 15:28:03 +0200 | [diff] [blame] | 2775 | fp = kzalloc(sizeof(*fp), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2776 | if (! fp) { |
| 2777 | snd_printk(KERN_ERR "cannot malloc\n"); |
| 2778 | return -ENOMEM; |
| 2779 | } |
| 2780 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2781 | fp->iface = iface_no; |
| 2782 | fp->altsetting = altno; |
| 2783 | fp->altset_idx = i; |
| 2784 | fp->endpoint = get_endpoint(alts, 0)->bEndpointAddress; |
| 2785 | fp->ep_attr = get_endpoint(alts, 0)->bmAttributes; |
Clemens Ladisch | 744b89e | 2009-04-03 09:45:01 +0200 | [diff] [blame] | 2786 | fp->datainterval = parse_datainterval(chip, alts); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2787 | fp->maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize); |
Clemens Ladisch | 573567e | 2005-06-27 08:17:30 +0200 | [diff] [blame] | 2788 | if (snd_usb_get_speed(dev) == USB_SPEED_HIGH) |
| 2789 | fp->maxpacksize = (((fp->maxpacksize >> 11) & 3) + 1) |
| 2790 | * (fp->maxpacksize & 0x7ff); |
Clemens Ladisch | faf8d11 | 2006-05-18 09:35:15 +0200 | [diff] [blame] | 2791 | fp->attributes = csep ? csep[3] : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2792 | |
| 2793 | /* some quirks for attributes here */ |
| 2794 | |
Clemens Ladisch | c3f9329 | 2005-05-04 14:56:04 +0200 | [diff] [blame] | 2795 | switch (chip->usb_id) { |
| 2796 | case USB_ID(0x0a92, 0x0053): /* AudioTrak Optoplay */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2797 | /* Optoplay sets the sample rate attribute although |
| 2798 | * it seems not supporting it in fact. |
| 2799 | */ |
| 2800 | fp->attributes &= ~EP_CS_ATTR_SAMPLE_RATE; |
Clemens Ladisch | c3f9329 | 2005-05-04 14:56:04 +0200 | [diff] [blame] | 2801 | break; |
| 2802 | case USB_ID(0x041e, 0x3020): /* Creative SB Audigy 2 NX */ |
| 2803 | case USB_ID(0x0763, 0x2003): /* M-Audio Audiophile USB */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2804 | /* doesn't set the sample rate attribute, but supports it */ |
| 2805 | fp->attributes |= EP_CS_ATTR_SAMPLE_RATE; |
Clemens Ladisch | c3f9329 | 2005-05-04 14:56:04 +0200 | [diff] [blame] | 2806 | break; |
| 2807 | case USB_ID(0x047f, 0x0ca1): /* plantronics headset */ |
| 2808 | case USB_ID(0x077d, 0x07af): /* Griffin iMic (note that there is |
| 2809 | an older model 77d:223) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2810 | /* |
| 2811 | * plantronics headset and Griffin iMic have set adaptive-in |
| 2812 | * although it's really not... |
| 2813 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2814 | fp->ep_attr &= ~EP_ATTR_MASK; |
| 2815 | if (stream == SNDRV_PCM_STREAM_PLAYBACK) |
| 2816 | fp->ep_attr |= EP_ATTR_ADAPTIVE; |
| 2817 | else |
| 2818 | fp->ep_attr |= EP_ATTR_SYNC; |
Clemens Ladisch | c3f9329 | 2005-05-04 14:56:04 +0200 | [diff] [blame] | 2819 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2820 | } |
| 2821 | |
| 2822 | /* ok, let's parse further... */ |
Clemens Ladisch | 27d10f5 | 2005-05-02 08:51:26 +0200 | [diff] [blame] | 2823 | if (parse_audio_format(chip, fp, format, fmt, stream) < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2824 | kfree(fp->rate_table); |
| 2825 | kfree(fp); |
| 2826 | continue; |
| 2827 | } |
| 2828 | |
Andreas Bergmeier | b9d710b | 2009-01-24 12:15:14 +0100 | [diff] [blame] | 2829 | snd_printdd(KERN_INFO "%d:%u:%d: add audio endpoint %#x\n", dev->devnum, iface_no, altno, fp->endpoint); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2830 | err = add_audio_endpoint(chip, stream, fp); |
| 2831 | if (err < 0) { |
| 2832 | kfree(fp->rate_table); |
| 2833 | kfree(fp); |
| 2834 | return err; |
| 2835 | } |
| 2836 | /* try to set the interface... */ |
| 2837 | usb_set_interface(chip->dev, iface_no, altno); |
| 2838 | init_usb_pitch(chip->dev, iface_no, alts, fp); |
| 2839 | init_usb_sample_rate(chip->dev, iface_no, alts, fp, fp->rate_max); |
| 2840 | } |
| 2841 | return 0; |
| 2842 | } |
| 2843 | |
| 2844 | |
| 2845 | /* |
| 2846 | * disconnect streams |
| 2847 | * called from snd_usb_audio_disconnect() |
| 2848 | */ |
Clemens Ladisch | ee73339 | 2005-04-25 10:34:13 +0200 | [diff] [blame] | 2849 | static void snd_usb_stream_disconnect(struct list_head *head) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2850 | { |
| 2851 | int idx; |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 2852 | struct snd_usb_stream *as; |
| 2853 | struct snd_usb_substream *subs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2854 | |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 2855 | as = list_entry(head, struct snd_usb_stream, list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2856 | for (idx = 0; idx < 2; idx++) { |
| 2857 | subs = &as->substream[idx]; |
| 2858 | if (!subs->num_formats) |
| 2859 | return; |
| 2860 | release_substream_urbs(subs, 1); |
| 2861 | subs->interface = -1; |
| 2862 | } |
| 2863 | } |
| 2864 | |
| 2865 | /* |
| 2866 | * parse audio control descriptor and create pcm/midi streams |
| 2867 | */ |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 2868 | static int snd_usb_create_streams(struct snd_usb_audio *chip, int ctrlif) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2869 | { |
| 2870 | struct usb_device *dev = chip->dev; |
| 2871 | struct usb_host_interface *host_iface; |
| 2872 | struct usb_interface *iface; |
| 2873 | unsigned char *p1; |
| 2874 | int i, j; |
| 2875 | |
| 2876 | /* find audiocontrol interface */ |
| 2877 | host_iface = &usb_ifnum_to_if(dev, ctrlif)->altsetting[0]; |
| 2878 | if (!(p1 = snd_usb_find_csint_desc(host_iface->extra, host_iface->extralen, NULL, HEADER))) { |
| 2879 | snd_printk(KERN_ERR "cannot find HEADER\n"); |
| 2880 | return -EINVAL; |
| 2881 | } |
| 2882 | if (! p1[7] || p1[0] < 8 + p1[7]) { |
| 2883 | snd_printk(KERN_ERR "invalid HEADER\n"); |
| 2884 | return -EINVAL; |
| 2885 | } |
| 2886 | |
| 2887 | /* |
| 2888 | * parse all USB audio streaming interfaces |
| 2889 | */ |
| 2890 | for (i = 0; i < p1[7]; i++) { |
| 2891 | struct usb_host_interface *alts; |
| 2892 | struct usb_interface_descriptor *altsd; |
| 2893 | j = p1[8 + i]; |
| 2894 | iface = usb_ifnum_to_if(dev, j); |
| 2895 | if (!iface) { |
| 2896 | snd_printk(KERN_ERR "%d:%u:%d : does not exist\n", |
| 2897 | dev->devnum, ctrlif, j); |
| 2898 | continue; |
| 2899 | } |
| 2900 | if (usb_interface_claimed(iface)) { |
| 2901 | snd_printdd(KERN_INFO "%d:%d:%d: skipping, already claimed\n", dev->devnum, ctrlif, j); |
| 2902 | continue; |
| 2903 | } |
| 2904 | alts = &iface->altsetting[0]; |
| 2905 | altsd = get_iface_desc(alts); |
| 2906 | if ((altsd->bInterfaceClass == USB_CLASS_AUDIO || |
| 2907 | altsd->bInterfaceClass == USB_CLASS_VENDOR_SPEC) && |
| 2908 | altsd->bInterfaceSubClass == USB_SUBCLASS_MIDI_STREAMING) { |
Clemens Ladisch | d82af9f | 2009-11-16 12:23:46 +0100 | [diff] [blame] | 2909 | int err = snd_usbmidi_create(chip->card, iface, |
| 2910 | &chip->midi_list, NULL); |
| 2911 | if (err < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2912 | snd_printk(KERN_ERR "%d:%u:%d: cannot create sequencer device\n", dev->devnum, ctrlif, j); |
| 2913 | continue; |
| 2914 | } |
| 2915 | usb_driver_claim_interface(&usb_audio_driver, iface, (void *)-1L); |
| 2916 | continue; |
| 2917 | } |
| 2918 | if ((altsd->bInterfaceClass != USB_CLASS_AUDIO && |
| 2919 | altsd->bInterfaceClass != USB_CLASS_VENDOR_SPEC) || |
| 2920 | altsd->bInterfaceSubClass != USB_SUBCLASS_AUDIO_STREAMING) { |
| 2921 | snd_printdd(KERN_ERR "%d:%u:%d: skipping non-supported interface %d\n", dev->devnum, ctrlif, j, altsd->bInterfaceClass); |
| 2922 | /* skip non-supported classes */ |
| 2923 | continue; |
| 2924 | } |
Clemens Ladisch | 076639f | 2007-08-21 08:56:54 +0200 | [diff] [blame] | 2925 | if (snd_usb_get_speed(dev) == USB_SPEED_LOW) { |
| 2926 | snd_printk(KERN_ERR "low speed audio streaming not supported\n"); |
| 2927 | continue; |
| 2928 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2929 | if (! parse_audio_endpoints(chip, j)) { |
| 2930 | usb_set_interface(dev, j, 0); /* reset the current interface */ |
| 2931 | usb_driver_claim_interface(&usb_audio_driver, iface, (void *)-1L); |
| 2932 | } |
| 2933 | } |
| 2934 | |
| 2935 | return 0; |
| 2936 | } |
| 2937 | |
| 2938 | /* |
| 2939 | * create a stream for an endpoint/altsetting without proper descriptors |
| 2940 | */ |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 2941 | static int create_fixed_stream_quirk(struct snd_usb_audio *chip, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2942 | struct usb_interface *iface, |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 2943 | const struct snd_usb_audio_quirk *quirk) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2944 | { |
| 2945 | struct audioformat *fp; |
| 2946 | struct usb_host_interface *alts; |
| 2947 | int stream, err; |
Al Viro | b4482a4 | 2007-10-14 19:35:40 +0100 | [diff] [blame] | 2948 | unsigned *rate_table = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2949 | |
Alexey Dobriyan | 52978be | 2006-09-30 23:27:21 -0700 | [diff] [blame] | 2950 | fp = kmemdup(quirk->data, sizeof(*fp), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2951 | if (! fp) { |
Alexey Dobriyan | 52978be | 2006-09-30 23:27:21 -0700 | [diff] [blame] | 2952 | snd_printk(KERN_ERR "cannot memdup\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2953 | return -ENOMEM; |
| 2954 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2955 | if (fp->nr_rates > 0) { |
| 2956 | rate_table = kmalloc(sizeof(int) * fp->nr_rates, GFP_KERNEL); |
| 2957 | if (!rate_table) { |
| 2958 | kfree(fp); |
| 2959 | return -ENOMEM; |
| 2960 | } |
| 2961 | memcpy(rate_table, fp->rate_table, sizeof(int) * fp->nr_rates); |
| 2962 | fp->rate_table = rate_table; |
| 2963 | } |
| 2964 | |
| 2965 | stream = (fp->endpoint & USB_DIR_IN) |
| 2966 | ? SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK; |
| 2967 | err = add_audio_endpoint(chip, stream, fp); |
| 2968 | if (err < 0) { |
| 2969 | kfree(fp); |
| 2970 | kfree(rate_table); |
| 2971 | return err; |
| 2972 | } |
| 2973 | if (fp->iface != get_iface_desc(&iface->altsetting[0])->bInterfaceNumber || |
| 2974 | fp->altset_idx >= iface->num_altsetting) { |
| 2975 | kfree(fp); |
| 2976 | kfree(rate_table); |
| 2977 | return -EINVAL; |
| 2978 | } |
| 2979 | alts = &iface->altsetting[fp->altset_idx]; |
Clemens Ladisch | 744b89e | 2009-04-03 09:45:01 +0200 | [diff] [blame] | 2980 | fp->datainterval = parse_datainterval(chip, alts); |
Clemens Ladisch | 894dcd7 | 2009-02-06 08:13:07 +0100 | [diff] [blame] | 2981 | fp->maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2982 | usb_set_interface(chip->dev, fp->iface, 0); |
| 2983 | init_usb_pitch(chip->dev, fp->iface, alts, fp); |
| 2984 | init_usb_sample_rate(chip->dev, fp->iface, alts, fp, fp->rate_max); |
| 2985 | return 0; |
| 2986 | } |
| 2987 | |
| 2988 | /* |
| 2989 | * create a stream for an interface with proper descriptors |
| 2990 | */ |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 2991 | static int create_standard_audio_quirk(struct snd_usb_audio *chip, |
Clemens Ladisch | d1bda04 | 2005-09-14 08:36:03 +0200 | [diff] [blame] | 2992 | struct usb_interface *iface, |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 2993 | const struct snd_usb_audio_quirk *quirk) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2994 | { |
| 2995 | struct usb_host_interface *alts; |
| 2996 | struct usb_interface_descriptor *altsd; |
| 2997 | int err; |
| 2998 | |
| 2999 | alts = &iface->altsetting[0]; |
| 3000 | altsd = get_iface_desc(alts); |
Clemens Ladisch | d1bda04 | 2005-09-14 08:36:03 +0200 | [diff] [blame] | 3001 | err = parse_audio_endpoints(chip, altsd->bInterfaceNumber); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3002 | if (err < 0) { |
| 3003 | snd_printk(KERN_ERR "cannot setup if %d: error %d\n", |
| 3004 | altsd->bInterfaceNumber, err); |
| 3005 | return err; |
| 3006 | } |
Clemens Ladisch | d1bda04 | 2005-09-14 08:36:03 +0200 | [diff] [blame] | 3007 | /* reset the current interface */ |
| 3008 | usb_set_interface(chip->dev, altsd->bInterfaceNumber, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3009 | return 0; |
| 3010 | } |
| 3011 | |
| 3012 | /* |
Pedro Lopez-Cabanillas | 310e0dc | 2008-10-04 16:27:36 +0200 | [diff] [blame] | 3013 | * Create a stream for an Edirol UA-700/UA-25/UA-4FX interface. |
| 3014 | * The only way to detect the sample rate is by looking at wMaxPacketSize. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3015 | */ |
Pedro Lopez-Cabanillas | 310e0dc | 2008-10-04 16:27:36 +0200 | [diff] [blame] | 3016 | static int create_uaxx_quirk(struct snd_usb_audio *chip, |
| 3017 | struct usb_interface *iface, |
| 3018 | const struct snd_usb_audio_quirk *quirk) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3019 | { |
| 3020 | static const struct audioformat ua_format = { |
| 3021 | .format = SNDRV_PCM_FORMAT_S24_3LE, |
| 3022 | .channels = 2, |
| 3023 | .fmt_type = USB_FORMAT_TYPE_I, |
| 3024 | .altsetting = 1, |
| 3025 | .altset_idx = 1, |
| 3026 | .rates = SNDRV_PCM_RATE_CONTINUOUS, |
| 3027 | }; |
| 3028 | struct usb_host_interface *alts; |
| 3029 | struct usb_interface_descriptor *altsd; |
| 3030 | struct audioformat *fp; |
| 3031 | int stream, err; |
| 3032 | |
Pedro Lopez-Cabanillas | 310e0dc | 2008-10-04 16:27:36 +0200 | [diff] [blame] | 3033 | /* both PCM and MIDI interfaces have 2 or more altsettings */ |
| 3034 | if (iface->num_altsetting < 2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3035 | return -ENXIO; |
| 3036 | alts = &iface->altsetting[1]; |
| 3037 | altsd = get_iface_desc(alts); |
| 3038 | |
Pedro Lopez-Cabanillas | 59b3db6 | 2008-10-07 20:54:18 +0200 | [diff] [blame] | 3039 | if (altsd->bNumEndpoints == 2) { |
| 3040 | static const struct snd_usb_midi_endpoint_info ua700_ep = { |
| 3041 | .out_cables = 0x0003, |
| 3042 | .in_cables = 0x0003 |
| 3043 | }; |
| 3044 | static const struct snd_usb_audio_quirk ua700_quirk = { |
| 3045 | .type = QUIRK_MIDI_FIXED_ENDPOINT, |
| 3046 | .data = &ua700_ep |
| 3047 | }; |
| 3048 | static const struct snd_usb_midi_endpoint_info uaxx_ep = { |
| 3049 | .out_cables = 0x0001, |
| 3050 | .in_cables = 0x0001 |
| 3051 | }; |
| 3052 | static const struct snd_usb_audio_quirk uaxx_quirk = { |
| 3053 | .type = QUIRK_MIDI_FIXED_ENDPOINT, |
| 3054 | .data = &uaxx_ep |
| 3055 | }; |
Clemens Ladisch | d82af9f | 2009-11-16 12:23:46 +0100 | [diff] [blame] | 3056 | const struct snd_usb_audio_quirk *quirk = |
| 3057 | chip->usb_id == USB_ID(0x0582, 0x002b) |
| 3058 | ? &ua700_quirk : &uaxx_quirk; |
| 3059 | return snd_usbmidi_create(chip->card, iface, |
| 3060 | &chip->midi_list, quirk); |
Pedro Lopez-Cabanillas | 59b3db6 | 2008-10-07 20:54:18 +0200 | [diff] [blame] | 3061 | } |
| 3062 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3063 | if (altsd->bNumEndpoints != 1) |
| 3064 | return -ENXIO; |
| 3065 | |
| 3066 | fp = kmalloc(sizeof(*fp), GFP_KERNEL); |
| 3067 | if (!fp) |
| 3068 | return -ENOMEM; |
| 3069 | memcpy(fp, &ua_format, sizeof(*fp)); |
| 3070 | |
| 3071 | fp->iface = altsd->bInterfaceNumber; |
| 3072 | fp->endpoint = get_endpoint(alts, 0)->bEndpointAddress; |
| 3073 | fp->ep_attr = get_endpoint(alts, 0)->bmAttributes; |
Clemens Ladisch | 744b89e | 2009-04-03 09:45:01 +0200 | [diff] [blame] | 3074 | fp->datainterval = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3075 | fp->maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize); |
| 3076 | |
| 3077 | switch (fp->maxpacksize) { |
| 3078 | case 0x120: |
| 3079 | fp->rate_max = fp->rate_min = 44100; |
| 3080 | break; |
| 3081 | case 0x138: |
| 3082 | case 0x140: |
| 3083 | fp->rate_max = fp->rate_min = 48000; |
| 3084 | break; |
| 3085 | case 0x258: |
| 3086 | case 0x260: |
| 3087 | fp->rate_max = fp->rate_min = 96000; |
| 3088 | break; |
| 3089 | default: |
| 3090 | snd_printk(KERN_ERR "unknown sample rate\n"); |
| 3091 | kfree(fp); |
| 3092 | return -ENXIO; |
| 3093 | } |
| 3094 | |
| 3095 | stream = (fp->endpoint & USB_DIR_IN) |
| 3096 | ? SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK; |
| 3097 | err = add_audio_endpoint(chip, stream, fp); |
| 3098 | if (err < 0) { |
| 3099 | kfree(fp); |
| 3100 | return err; |
| 3101 | } |
| 3102 | usb_set_interface(chip->dev, fp->iface, 0); |
| 3103 | return 0; |
| 3104 | } |
| 3105 | |
| 3106 | /* |
| 3107 | * Create a stream for an Edirol UA-1000 interface. |
| 3108 | */ |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 3109 | static int create_ua1000_quirk(struct snd_usb_audio *chip, |
Clemens Ladisch | 854af95 | 2005-07-25 16:19:10 +0200 | [diff] [blame] | 3110 | struct usb_interface *iface, |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 3111 | const struct snd_usb_audio_quirk *quirk) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3112 | { |
| 3113 | static const struct audioformat ua1000_format = { |
| 3114 | .format = SNDRV_PCM_FORMAT_S32_LE, |
| 3115 | .fmt_type = USB_FORMAT_TYPE_I, |
| 3116 | .altsetting = 1, |
| 3117 | .altset_idx = 1, |
| 3118 | .attributes = 0, |
| 3119 | .rates = SNDRV_PCM_RATE_CONTINUOUS, |
| 3120 | }; |
| 3121 | struct usb_host_interface *alts; |
| 3122 | struct usb_interface_descriptor *altsd; |
| 3123 | struct audioformat *fp; |
| 3124 | int stream, err; |
| 3125 | |
| 3126 | if (iface->num_altsetting != 2) |
| 3127 | return -ENXIO; |
| 3128 | alts = &iface->altsetting[1]; |
| 3129 | altsd = get_iface_desc(alts); |
Ben Williamson | c4a87ef | 2006-06-19 17:20:09 +0200 | [diff] [blame] | 3130 | if (alts->extralen != 11 || alts->extra[1] != USB_DT_CS_INTERFACE || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3131 | altsd->bNumEndpoints != 1) |
| 3132 | return -ENXIO; |
| 3133 | |
Alexey Dobriyan | 52978be | 2006-09-30 23:27:21 -0700 | [diff] [blame] | 3134 | fp = kmemdup(&ua1000_format, sizeof(*fp), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3135 | if (!fp) |
| 3136 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3137 | |
| 3138 | fp->channels = alts->extra[4]; |
| 3139 | fp->iface = altsd->bInterfaceNumber; |
| 3140 | fp->endpoint = get_endpoint(alts, 0)->bEndpointAddress; |
| 3141 | fp->ep_attr = get_endpoint(alts, 0)->bmAttributes; |
Clemens Ladisch | 744b89e | 2009-04-03 09:45:01 +0200 | [diff] [blame] | 3142 | fp->datainterval = parse_datainterval(chip, alts); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3143 | fp->maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize); |
| 3144 | fp->rate_max = fp->rate_min = combine_triple(&alts->extra[8]); |
| 3145 | |
| 3146 | stream = (fp->endpoint & USB_DIR_IN) |
| 3147 | ? SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK; |
| 3148 | err = add_audio_endpoint(chip, stream, fp); |
| 3149 | if (err < 0) { |
| 3150 | kfree(fp); |
| 3151 | return err; |
| 3152 | } |
| 3153 | /* FIXME: playback must be synchronized to capture */ |
| 3154 | usb_set_interface(chip->dev, fp->iface, 0); |
| 3155 | return 0; |
| 3156 | } |
| 3157 | |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 3158 | static int snd_usb_create_quirk(struct snd_usb_audio *chip, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3159 | struct usb_interface *iface, |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 3160 | const struct snd_usb_audio_quirk *quirk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3161 | |
| 3162 | /* |
| 3163 | * handle the quirks for the contained interfaces |
| 3164 | */ |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 3165 | static int create_composite_quirk(struct snd_usb_audio *chip, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3166 | struct usb_interface *iface, |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 3167 | const struct snd_usb_audio_quirk *quirk) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3168 | { |
| 3169 | int probed_ifnum = get_iface_desc(iface->altsetting)->bInterfaceNumber; |
| 3170 | int err; |
| 3171 | |
| 3172 | for (quirk = quirk->data; quirk->ifnum >= 0; ++quirk) { |
| 3173 | iface = usb_ifnum_to_if(chip->dev, quirk->ifnum); |
| 3174 | if (!iface) |
| 3175 | continue; |
| 3176 | if (quirk->ifnum != probed_ifnum && |
| 3177 | usb_interface_claimed(iface)) |
| 3178 | continue; |
| 3179 | err = snd_usb_create_quirk(chip, iface, quirk); |
| 3180 | if (err < 0) |
| 3181 | return err; |
| 3182 | if (quirk->ifnum != probed_ifnum) |
| 3183 | usb_driver_claim_interface(&usb_audio_driver, iface, (void *)-1L); |
| 3184 | } |
| 3185 | return 0; |
| 3186 | } |
| 3187 | |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 3188 | static int ignore_interface_quirk(struct snd_usb_audio *chip, |
Clemens Ladisch | 854af95 | 2005-07-25 16:19:10 +0200 | [diff] [blame] | 3189 | struct usb_interface *iface, |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 3190 | const struct snd_usb_audio_quirk *quirk) |
Clemens Ladisch | 854af95 | 2005-07-25 16:19:10 +0200 | [diff] [blame] | 3191 | { |
| 3192 | return 0; |
| 3193 | } |
| 3194 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3195 | |
| 3196 | /* |
| 3197 | * boot quirks |
| 3198 | */ |
| 3199 | |
| 3200 | #define EXTIGY_FIRMWARE_SIZE_OLD 794 |
| 3201 | #define EXTIGY_FIRMWARE_SIZE_NEW 483 |
| 3202 | |
| 3203 | static int snd_usb_extigy_boot_quirk(struct usb_device *dev, struct usb_interface *intf) |
| 3204 | { |
| 3205 | struct usb_host_config *config = dev->actconfig; |
| 3206 | int err; |
| 3207 | |
| 3208 | if (le16_to_cpu(get_cfg_desc(config)->wTotalLength) == EXTIGY_FIRMWARE_SIZE_OLD || |
| 3209 | le16_to_cpu(get_cfg_desc(config)->wTotalLength) == EXTIGY_FIRMWARE_SIZE_NEW) { |
| 3210 | snd_printdd("sending Extigy boot sequence...\n"); |
| 3211 | /* Send message to force it to reconnect with full interface. */ |
| 3212 | err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev,0), |
| 3213 | 0x10, 0x43, 0x0001, 0x000a, NULL, 0, 1000); |
| 3214 | if (err < 0) snd_printdd("error sending boot message: %d\n", err); |
| 3215 | err = usb_get_descriptor(dev, USB_DT_DEVICE, 0, |
| 3216 | &dev->descriptor, sizeof(dev->descriptor)); |
| 3217 | config = dev->actconfig; |
| 3218 | if (err < 0) snd_printdd("error usb_get_descriptor: %d\n", err); |
| 3219 | err = usb_reset_configuration(dev); |
| 3220 | if (err < 0) snd_printdd("error usb_reset_configuration: %d\n", err); |
| 3221 | snd_printdd("extigy_boot: new boot length = %d\n", |
| 3222 | le16_to_cpu(get_cfg_desc(config)->wTotalLength)); |
| 3223 | return -ENODEV; /* quit this anyway */ |
| 3224 | } |
| 3225 | return 0; |
| 3226 | } |
| 3227 | |
Clemens Ladisch | 3a2f085 | 2005-05-09 09:20:31 +0200 | [diff] [blame] | 3228 | static int snd_usb_audigy2nx_boot_quirk(struct usb_device *dev) |
| 3229 | { |
Clemens Ladisch | 3a2f085 | 2005-05-09 09:20:31 +0200 | [diff] [blame] | 3230 | u8 buf = 1; |
| 3231 | |
| 3232 | snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), 0x2a, |
| 3233 | USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_OTHER, |
| 3234 | 0, 0, &buf, 1, 1000); |
| 3235 | if (buf == 0) { |
| 3236 | snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), 0x29, |
| 3237 | USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER, |
| 3238 | 1, 2000, NULL, 0, 1000); |
| 3239 | return -ENODEV; |
| 3240 | } |
Clemens Ladisch | 3a2f085 | 2005-05-09 09:20:31 +0200 | [diff] [blame] | 3241 | return 0; |
| 3242 | } |
| 3243 | |
Thibault LE MEUR | e311334 | 2006-03-14 11:44:53 +0100 | [diff] [blame] | 3244 | /* |
Sam Revitch | e217e30 | 2006-06-23 15:10:18 +0200 | [diff] [blame] | 3245 | * C-Media CM106/CM106+ have four 16-bit internal registers that are nicely |
| 3246 | * documented in the device's data sheet. |
| 3247 | */ |
| 3248 | static int snd_usb_cm106_write_int_reg(struct usb_device *dev, int reg, u16 value) |
| 3249 | { |
| 3250 | u8 buf[4]; |
| 3251 | buf[0] = 0x20; |
| 3252 | buf[1] = value & 0xff; |
| 3253 | buf[2] = (value >> 8) & 0xff; |
| 3254 | buf[3] = reg; |
| 3255 | return snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), USB_REQ_SET_CONFIGURATION, |
| 3256 | USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_ENDPOINT, |
| 3257 | 0, 0, &buf, 4, 1000); |
| 3258 | } |
| 3259 | |
| 3260 | static int snd_usb_cm106_boot_quirk(struct usb_device *dev) |
| 3261 | { |
| 3262 | /* |
| 3263 | * Enable line-out driver mode, set headphone source to front |
| 3264 | * channels, enable stereo mic. |
| 3265 | */ |
| 3266 | return snd_usb_cm106_write_int_reg(dev, 2, 0x8004); |
| 3267 | } |
| 3268 | |
Dan Allongo | 92a4379 | 2009-06-08 11:21:52 -0400 | [diff] [blame] | 3269 | /* |
| 3270 | * C-Media CM6206 is based on CM106 with two additional |
| 3271 | * registers that are not documented in the data sheet. |
| 3272 | * Values here are chosen based on sniffing USB traffic |
| 3273 | * under Windows. |
| 3274 | */ |
| 3275 | static int snd_usb_cm6206_boot_quirk(struct usb_device *dev) |
| 3276 | { |
| 3277 | int err, reg; |
| 3278 | int val[] = {0x200c, 0x3000, 0xf800, 0x143f, 0x0000, 0x3000}; |
| 3279 | |
| 3280 | for (reg = 0; reg < ARRAY_SIZE(val); reg++) { |
| 3281 | err = snd_usb_cm106_write_int_reg(dev, reg, val[reg]); |
| 3282 | if (err < 0) |
| 3283 | return err; |
| 3284 | } |
| 3285 | |
| 3286 | return err; |
| 3287 | } |
Sam Revitch | e217e30 | 2006-06-23 15:10:18 +0200 | [diff] [blame] | 3288 | |
| 3289 | /* |
Thibault LE MEUR | e311334 | 2006-03-14 11:44:53 +0100 | [diff] [blame] | 3290 | * Setup quirks |
| 3291 | */ |
| 3292 | #define AUDIOPHILE_SET 0x01 /* if set, parse device_setup */ |
| 3293 | #define AUDIOPHILE_SET_DTS 0x02 /* if set, enable DTS Digital Output */ |
| 3294 | #define AUDIOPHILE_SET_96K 0x04 /* 48-96KHz rate if set, 8-48KHz otherwise */ |
| 3295 | #define AUDIOPHILE_SET_24B 0x08 /* 24bits sample if set, 16bits otherwise */ |
| 3296 | #define AUDIOPHILE_SET_DI 0x10 /* if set, enable Digital Input */ |
| 3297 | #define AUDIOPHILE_SET_MASK 0x1F /* bit mask for setup value */ |
| 3298 | #define AUDIOPHILE_SET_24B_48K_DI 0x19 /* value for 24bits+48KHz+Digital Input */ |
| 3299 | #define AUDIOPHILE_SET_24B_48K_NOTDI 0x09 /* value for 24bits+48KHz+No Digital Input */ |
| 3300 | #define AUDIOPHILE_SET_16B_48K_DI 0x11 /* value for 16bits+48KHz+Digital Input */ |
| 3301 | #define AUDIOPHILE_SET_16B_48K_NOTDI 0x01 /* value for 16bits+48KHz+No Digital Input */ |
| 3302 | |
| 3303 | static int audiophile_skip_setting_quirk(struct snd_usb_audio *chip, |
| 3304 | int iface, int altno) |
| 3305 | { |
Thibault Le Meur | f8c78b8 | 2007-07-12 11:26:35 +0200 | [diff] [blame] | 3306 | /* Reset ALL ifaces to 0 altsetting. |
| 3307 | * Call it for every possible altsetting of every interface. |
| 3308 | */ |
| 3309 | usb_set_interface(chip->dev, iface, 0); |
| 3310 | |
Thibault LE MEUR | e311334 | 2006-03-14 11:44:53 +0100 | [diff] [blame] | 3311 | if (device_setup[chip->index] & AUDIOPHILE_SET) { |
| 3312 | if ((device_setup[chip->index] & AUDIOPHILE_SET_DTS) |
| 3313 | && altno != 6) |
| 3314 | return 1; /* skip this altsetting */ |
| 3315 | if ((device_setup[chip->index] & AUDIOPHILE_SET_96K) |
| 3316 | && altno != 1) |
| 3317 | return 1; /* skip this altsetting */ |
| 3318 | if ((device_setup[chip->index] & AUDIOPHILE_SET_MASK) == |
| 3319 | AUDIOPHILE_SET_24B_48K_DI && altno != 2) |
| 3320 | return 1; /* skip this altsetting */ |
| 3321 | if ((device_setup[chip->index] & AUDIOPHILE_SET_MASK) == |
| 3322 | AUDIOPHILE_SET_24B_48K_NOTDI && altno != 3) |
| 3323 | return 1; /* skip this altsetting */ |
| 3324 | if ((device_setup[chip->index] & AUDIOPHILE_SET_MASK) == |
| 3325 | AUDIOPHILE_SET_16B_48K_DI && altno != 4) |
| 3326 | return 1; /* skip this altsetting */ |
| 3327 | if ((device_setup[chip->index] & AUDIOPHILE_SET_MASK) == |
| 3328 | AUDIOPHILE_SET_16B_48K_NOTDI && altno != 5) |
| 3329 | return 1; /* skip this altsetting */ |
| 3330 | } |
| 3331 | return 0; /* keep this altsetting */ |
| 3332 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3333 | |
Clemens Ladisch | d82af9f | 2009-11-16 12:23:46 +0100 | [diff] [blame] | 3334 | static int create_any_midi_quirk(struct snd_usb_audio *chip, |
| 3335 | struct usb_interface *intf, |
| 3336 | const struct snd_usb_audio_quirk *quirk) |
| 3337 | { |
| 3338 | return snd_usbmidi_create(chip->card, intf, &chip->midi_list, quirk); |
| 3339 | } |
| 3340 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3341 | /* |
| 3342 | * audio-interface quirks |
| 3343 | * |
| 3344 | * returns zero if no standard audio/MIDI parsing is needed. |
| 3345 | * returns a postive value if standard audio/midi interfaces are parsed |
| 3346 | * after this. |
| 3347 | * returns a negative value at error. |
| 3348 | */ |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 3349 | static int snd_usb_create_quirk(struct snd_usb_audio *chip, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3350 | struct usb_interface *iface, |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 3351 | const struct snd_usb_audio_quirk *quirk) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3352 | { |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 3353 | typedef int (*quirk_func_t)(struct snd_usb_audio *, struct usb_interface *, |
| 3354 | const struct snd_usb_audio_quirk *); |
Clemens Ladisch | 854af95 | 2005-07-25 16:19:10 +0200 | [diff] [blame] | 3355 | static const quirk_func_t quirk_funcs[] = { |
| 3356 | [QUIRK_IGNORE_INTERFACE] = ignore_interface_quirk, |
| 3357 | [QUIRK_COMPOSITE] = create_composite_quirk, |
Clemens Ladisch | d82af9f | 2009-11-16 12:23:46 +0100 | [diff] [blame] | 3358 | [QUIRK_MIDI_STANDARD_INTERFACE] = create_any_midi_quirk, |
| 3359 | [QUIRK_MIDI_FIXED_ENDPOINT] = create_any_midi_quirk, |
| 3360 | [QUIRK_MIDI_YAMAHA] = create_any_midi_quirk, |
| 3361 | [QUIRK_MIDI_MIDIMAN] = create_any_midi_quirk, |
| 3362 | [QUIRK_MIDI_NOVATION] = create_any_midi_quirk, |
| 3363 | [QUIRK_MIDI_FASTLANE] = create_any_midi_quirk, |
| 3364 | [QUIRK_MIDI_EMAGIC] = create_any_midi_quirk, |
| 3365 | [QUIRK_MIDI_CME] = create_any_midi_quirk, |
Clemens Ladisch | d1bda04 | 2005-09-14 08:36:03 +0200 | [diff] [blame] | 3366 | [QUIRK_AUDIO_STANDARD_INTERFACE] = create_standard_audio_quirk, |
Clemens Ladisch | 854af95 | 2005-07-25 16:19:10 +0200 | [diff] [blame] | 3367 | [QUIRK_AUDIO_FIXED_ENDPOINT] = create_fixed_stream_quirk, |
Clemens Ladisch | 854af95 | 2005-07-25 16:19:10 +0200 | [diff] [blame] | 3368 | [QUIRK_AUDIO_EDIROL_UA1000] = create_ua1000_quirk, |
Pedro Lopez-Cabanillas | 310e0dc | 2008-10-04 16:27:36 +0200 | [diff] [blame] | 3369 | [QUIRK_AUDIO_EDIROL_UAXX] = create_uaxx_quirk |
Clemens Ladisch | 854af95 | 2005-07-25 16:19:10 +0200 | [diff] [blame] | 3370 | }; |
| 3371 | |
| 3372 | if (quirk->type < QUIRK_TYPE_COUNT) { |
| 3373 | return quirk_funcs[quirk->type](chip, iface, quirk); |
| 3374 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3375 | snd_printd(KERN_ERR "invalid quirk type %d\n", quirk->type); |
| 3376 | return -ENXIO; |
| 3377 | } |
| 3378 | } |
| 3379 | |
| 3380 | |
| 3381 | /* |
| 3382 | * common proc files to show the usb device info |
| 3383 | */ |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 3384 | static void proc_audio_usbbus_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3385 | { |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 3386 | struct snd_usb_audio *chip = entry->private_data; |
Pavel Machek | 07f51a7 | 2008-04-14 13:15:56 +0200 | [diff] [blame] | 3387 | if (!chip->shutdown) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3388 | snd_iprintf(buffer, "%03d/%03d\n", chip->dev->bus->busnum, chip->dev->devnum); |
| 3389 | } |
| 3390 | |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 3391 | static void proc_audio_usbid_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3392 | { |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 3393 | struct snd_usb_audio *chip = entry->private_data; |
Pavel Machek | 07f51a7 | 2008-04-14 13:15:56 +0200 | [diff] [blame] | 3394 | if (!chip->shutdown) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3395 | snd_iprintf(buffer, "%04x:%04x\n", |
Clemens Ladisch | 27d10f5 | 2005-05-02 08:51:26 +0200 | [diff] [blame] | 3396 | USB_ID_VENDOR(chip->usb_id), |
| 3397 | USB_ID_PRODUCT(chip->usb_id)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3398 | } |
| 3399 | |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 3400 | static void snd_usb_audio_create_proc(struct snd_usb_audio *chip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3401 | { |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 3402 | struct snd_info_entry *entry; |
Pavel Machek | 07f51a7 | 2008-04-14 13:15:56 +0200 | [diff] [blame] | 3403 | if (!snd_card_proc_new(chip->card, "usbbus", &entry)) |
Takashi Iwai | bf85020 | 2006-04-28 15:13:41 +0200 | [diff] [blame] | 3404 | snd_info_set_text_ops(entry, chip, proc_audio_usbbus_read); |
Pavel Machek | 07f51a7 | 2008-04-14 13:15:56 +0200 | [diff] [blame] | 3405 | if (!snd_card_proc_new(chip->card, "usbid", &entry)) |
Takashi Iwai | bf85020 | 2006-04-28 15:13:41 +0200 | [diff] [blame] | 3406 | snd_info_set_text_ops(entry, chip, proc_audio_usbid_read); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3407 | } |
| 3408 | |
| 3409 | /* |
| 3410 | * free the chip instance |
| 3411 | * |
| 3412 | * here we have to do not much, since pcm and controls are already freed |
| 3413 | * |
| 3414 | */ |
| 3415 | |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 3416 | static int snd_usb_audio_free(struct snd_usb_audio *chip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3417 | { |
| 3418 | kfree(chip); |
| 3419 | return 0; |
| 3420 | } |
| 3421 | |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 3422 | static int snd_usb_audio_dev_free(struct snd_device *device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3423 | { |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 3424 | struct snd_usb_audio *chip = device->device_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3425 | return snd_usb_audio_free(chip); |
| 3426 | } |
| 3427 | |
| 3428 | |
| 3429 | /* |
| 3430 | * create a chip instance and set its names. |
| 3431 | */ |
| 3432 | static int snd_usb_audio_create(struct usb_device *dev, int idx, |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 3433 | const struct snd_usb_audio_quirk *quirk, |
| 3434 | struct snd_usb_audio **rchip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3435 | { |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 3436 | struct snd_card *card; |
| 3437 | struct snd_usb_audio *chip; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3438 | int err, len; |
| 3439 | char component[14]; |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 3440 | static struct snd_device_ops ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3441 | .dev_free = snd_usb_audio_dev_free, |
| 3442 | }; |
| 3443 | |
| 3444 | *rchip = NULL; |
| 3445 | |
Clemens Ladisch | 076639f | 2007-08-21 08:56:54 +0200 | [diff] [blame] | 3446 | if (snd_usb_get_speed(dev) != USB_SPEED_LOW && |
| 3447 | snd_usb_get_speed(dev) != USB_SPEED_FULL && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3448 | snd_usb_get_speed(dev) != USB_SPEED_HIGH) { |
| 3449 | snd_printk(KERN_ERR "unknown device speed %d\n", snd_usb_get_speed(dev)); |
| 3450 | return -ENXIO; |
| 3451 | } |
| 3452 | |
Takashi Iwai | bd7dd77 | 2008-12-28 16:45:02 +0100 | [diff] [blame] | 3453 | err = snd_card_create(index[idx], id[idx], THIS_MODULE, 0, &card); |
| 3454 | if (err < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3455 | snd_printk(KERN_ERR "cannot create card instance %d\n", idx); |
Takashi Iwai | bd7dd77 | 2008-12-28 16:45:02 +0100 | [diff] [blame] | 3456 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3457 | } |
| 3458 | |
Takashi Iwai | 561b220 | 2005-09-09 14:22:34 +0200 | [diff] [blame] | 3459 | chip = kzalloc(sizeof(*chip), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3460 | if (! chip) { |
| 3461 | snd_card_free(card); |
| 3462 | return -ENOMEM; |
| 3463 | } |
| 3464 | |
| 3465 | chip->index = idx; |
| 3466 | chip->dev = dev; |
| 3467 | chip->card = card; |
Clemens Ladisch | 27d10f5 | 2005-05-02 08:51:26 +0200 | [diff] [blame] | 3468 | chip->usb_id = USB_ID(le16_to_cpu(dev->descriptor.idVendor), |
| 3469 | le16_to_cpu(dev->descriptor.idProduct)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3470 | INIT_LIST_HEAD(&chip->pcm_list); |
| 3471 | INIT_LIST_HEAD(&chip->midi_list); |
Clemens Ladisch | 84957a8 | 2005-04-29 16:23:13 +0200 | [diff] [blame] | 3472 | INIT_LIST_HEAD(&chip->mixer_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3473 | |
| 3474 | if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) { |
| 3475 | snd_usb_audio_free(chip); |
| 3476 | snd_card_free(card); |
| 3477 | return err; |
| 3478 | } |
| 3479 | |
| 3480 | strcpy(card->driver, "USB-Audio"); |
| 3481 | sprintf(component, "USB%04x:%04x", |
Clemens Ladisch | 27d10f5 | 2005-05-02 08:51:26 +0200 | [diff] [blame] | 3482 | USB_ID_VENDOR(chip->usb_id), USB_ID_PRODUCT(chip->usb_id)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3483 | snd_component_add(card, component); |
| 3484 | |
| 3485 | /* retrieve the device string as shortname */ |
| 3486 | if (quirk && quirk->product_name) { |
| 3487 | strlcpy(card->shortname, quirk->product_name, sizeof(card->shortname)); |
| 3488 | } else { |
| 3489 | if (!dev->descriptor.iProduct || |
| 3490 | usb_string(dev, dev->descriptor.iProduct, |
| 3491 | card->shortname, sizeof(card->shortname)) <= 0) { |
| 3492 | /* no name available from anywhere, so use ID */ |
| 3493 | sprintf(card->shortname, "USB Device %#04x:%#04x", |
Clemens Ladisch | 27d10f5 | 2005-05-02 08:51:26 +0200 | [diff] [blame] | 3494 | USB_ID_VENDOR(chip->usb_id), |
| 3495 | USB_ID_PRODUCT(chip->usb_id)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3496 | } |
| 3497 | } |
| 3498 | |
| 3499 | /* retrieve the vendor and device strings as longname */ |
| 3500 | if (quirk && quirk->vendor_name) { |
| 3501 | len = strlcpy(card->longname, quirk->vendor_name, sizeof(card->longname)); |
| 3502 | } else { |
| 3503 | if (dev->descriptor.iManufacturer) |
| 3504 | len = usb_string(dev, dev->descriptor.iManufacturer, |
| 3505 | card->longname, sizeof(card->longname)); |
| 3506 | else |
| 3507 | len = 0; |
| 3508 | /* we don't really care if there isn't any vendor string */ |
| 3509 | } |
| 3510 | if (len > 0) |
| 3511 | strlcat(card->longname, " ", sizeof(card->longname)); |
| 3512 | |
| 3513 | strlcat(card->longname, card->shortname, sizeof(card->longname)); |
| 3514 | |
| 3515 | len = strlcat(card->longname, " at ", sizeof(card->longname)); |
| 3516 | |
| 3517 | if (len < sizeof(card->longname)) |
| 3518 | usb_make_path(dev, card->longname + len, sizeof(card->longname) - len); |
| 3519 | |
| 3520 | strlcat(card->longname, |
Clemens Ladisch | 076639f | 2007-08-21 08:56:54 +0200 | [diff] [blame] | 3521 | snd_usb_get_speed(dev) == USB_SPEED_LOW ? ", low speed" : |
| 3522 | snd_usb_get_speed(dev) == USB_SPEED_FULL ? ", full speed" : |
| 3523 | ", high speed", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3524 | sizeof(card->longname)); |
| 3525 | |
| 3526 | snd_usb_audio_create_proc(chip); |
| 3527 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3528 | *rchip = chip; |
| 3529 | return 0; |
| 3530 | } |
| 3531 | |
| 3532 | |
| 3533 | /* |
| 3534 | * probe the active usb device |
| 3535 | * |
| 3536 | * note that this can be called multiple times per a device, when it |
| 3537 | * includes multiple audio control interfaces. |
| 3538 | * |
| 3539 | * thus we check the usb device pointer and creates the card instance |
| 3540 | * only at the first time. the successive calls of this function will |
| 3541 | * append the pcm interface to the corresponding card. |
| 3542 | */ |
| 3543 | static void *snd_usb_audio_probe(struct usb_device *dev, |
| 3544 | struct usb_interface *intf, |
| 3545 | const struct usb_device_id *usb_id) |
| 3546 | { |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 3547 | const struct snd_usb_audio_quirk *quirk = (const struct snd_usb_audio_quirk *)usb_id->driver_info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3548 | int i, err; |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 3549 | struct snd_usb_audio *chip; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3550 | struct usb_host_interface *alts; |
| 3551 | int ifnum; |
Clemens Ladisch | 27d10f5 | 2005-05-02 08:51:26 +0200 | [diff] [blame] | 3552 | u32 id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3553 | |
| 3554 | alts = &intf->altsetting[0]; |
| 3555 | ifnum = get_iface_desc(alts)->bInterfaceNumber; |
Clemens Ladisch | 27d10f5 | 2005-05-02 08:51:26 +0200 | [diff] [blame] | 3556 | id = USB_ID(le16_to_cpu(dev->descriptor.idVendor), |
| 3557 | le16_to_cpu(dev->descriptor.idProduct)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3558 | |
| 3559 | if (quirk && quirk->ifnum >= 0 && ifnum != quirk->ifnum) |
| 3560 | goto __err_val; |
| 3561 | |
| 3562 | /* SB Extigy needs special boot-up sequence */ |
| 3563 | /* if more models come, this will go to the quirk list. */ |
Clemens Ladisch | 27d10f5 | 2005-05-02 08:51:26 +0200 | [diff] [blame] | 3564 | if (id == USB_ID(0x041e, 0x3000)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3565 | if (snd_usb_extigy_boot_quirk(dev, intf) < 0) |
| 3566 | goto __err_val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3567 | } |
Clemens Ladisch | 3a2f085 | 2005-05-09 09:20:31 +0200 | [diff] [blame] | 3568 | /* SB Audigy 2 NX needs its own boot-up magic, too */ |
| 3569 | if (id == USB_ID(0x041e, 0x3020)) { |
| 3570 | if (snd_usb_audigy2nx_boot_quirk(dev) < 0) |
| 3571 | goto __err_val; |
| 3572 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3573 | |
Sam Revitch | e217e30 | 2006-06-23 15:10:18 +0200 | [diff] [blame] | 3574 | /* C-Media CM106 / Turtle Beach Audio Advantage Roadie */ |
| 3575 | if (id == USB_ID(0x10f5, 0x0200)) { |
| 3576 | if (snd_usb_cm106_boot_quirk(dev) < 0) |
| 3577 | goto __err_val; |
| 3578 | } |
| 3579 | |
Dan Allongo | 92a4379 | 2009-06-08 11:21:52 -0400 | [diff] [blame] | 3580 | /* C-Media CM6206 / CM106-Like Sound Device */ |
| 3581 | if (id == USB_ID(0x0d8c, 0x0102)) { |
| 3582 | if (snd_usb_cm6206_boot_quirk(dev) < 0) |
| 3583 | goto __err_val; |
| 3584 | } |
| 3585 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3586 | /* |
| 3587 | * found a config. now register to ALSA |
| 3588 | */ |
| 3589 | |
| 3590 | /* check whether it's already registered */ |
| 3591 | chip = NULL; |
Ingo Molnar | 12aa757 | 2006-01-16 16:36:05 +0100 | [diff] [blame] | 3592 | mutex_lock(®ister_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3593 | for (i = 0; i < SNDRV_CARDS; i++) { |
| 3594 | if (usb_chip[i] && usb_chip[i]->dev == dev) { |
| 3595 | if (usb_chip[i]->shutdown) { |
| 3596 | snd_printk(KERN_ERR "USB device is in the shutdown state, cannot create a card instance\n"); |
| 3597 | goto __error; |
| 3598 | } |
| 3599 | chip = usb_chip[i]; |
| 3600 | break; |
| 3601 | } |
| 3602 | } |
| 3603 | if (! chip) { |
| 3604 | /* it's a fresh one. |
| 3605 | * now look for an empty slot and create a new card instance |
| 3606 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3607 | for (i = 0; i < SNDRV_CARDS; i++) |
| 3608 | if (enable[i] && ! usb_chip[i] && |
Clemens Ladisch | 27d10f5 | 2005-05-02 08:51:26 +0200 | [diff] [blame] | 3609 | (vid[i] == -1 || vid[i] == USB_ID_VENDOR(id)) && |
| 3610 | (pid[i] == -1 || pid[i] == USB_ID_PRODUCT(id))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3611 | if (snd_usb_audio_create(dev, i, quirk, &chip) < 0) { |
| 3612 | goto __error; |
| 3613 | } |
Clemens Ladisch | 1dcd3ec | 2005-05-10 14:51:40 +0200 | [diff] [blame] | 3614 | snd_card_set_dev(chip->card, &intf->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3615 | break; |
| 3616 | } |
Pavel Machek | 07f51a7 | 2008-04-14 13:15:56 +0200 | [diff] [blame] | 3617 | if (!chip) { |
| 3618 | printk(KERN_ERR "no available usb audio device\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3619 | goto __error; |
| 3620 | } |
| 3621 | } |
| 3622 | |
| 3623 | err = 1; /* continue */ |
| 3624 | if (quirk && quirk->ifnum != QUIRK_NO_INTERFACE) { |
| 3625 | /* need some special handlings */ |
| 3626 | if ((err = snd_usb_create_quirk(chip, intf, quirk)) < 0) |
| 3627 | goto __error; |
| 3628 | } |
| 3629 | |
| 3630 | if (err > 0) { |
| 3631 | /* create normal USB audio interfaces */ |
| 3632 | if (snd_usb_create_streams(chip, ifnum) < 0 || |
Takashi Iwai | 7a9b806 | 2008-08-13 15:40:53 +0200 | [diff] [blame] | 3633 | snd_usb_create_mixer(chip, ifnum, ignore_ctl_error) < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3634 | goto __error; |
| 3635 | } |
| 3636 | } |
| 3637 | |
| 3638 | /* we are allowed to call snd_card_register() many times */ |
| 3639 | if (snd_card_register(chip->card) < 0) { |
| 3640 | goto __error; |
| 3641 | } |
| 3642 | |
| 3643 | usb_chip[chip->index] = chip; |
| 3644 | chip->num_interfaces++; |
Ingo Molnar | 12aa757 | 2006-01-16 16:36:05 +0100 | [diff] [blame] | 3645 | mutex_unlock(®ister_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3646 | return chip; |
| 3647 | |
| 3648 | __error: |
| 3649 | if (chip && !chip->num_interfaces) |
| 3650 | snd_card_free(chip->card); |
Ingo Molnar | 12aa757 | 2006-01-16 16:36:05 +0100 | [diff] [blame] | 3651 | mutex_unlock(®ister_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3652 | __err_val: |
| 3653 | return NULL; |
| 3654 | } |
| 3655 | |
| 3656 | /* |
| 3657 | * we need to take care of counter, since disconnection can be called also |
| 3658 | * many times as well as usb_audio_probe(). |
| 3659 | */ |
| 3660 | static void snd_usb_audio_disconnect(struct usb_device *dev, void *ptr) |
| 3661 | { |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 3662 | struct snd_usb_audio *chip; |
| 3663 | struct snd_card *card; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3664 | struct list_head *p; |
| 3665 | |
| 3666 | if (ptr == (void *)-1L) |
| 3667 | return; |
| 3668 | |
| 3669 | chip = ptr; |
| 3670 | card = chip->card; |
Ingo Molnar | 12aa757 | 2006-01-16 16:36:05 +0100 | [diff] [blame] | 3671 | mutex_lock(®ister_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3672 | chip->shutdown = 1; |
| 3673 | chip->num_interfaces--; |
| 3674 | if (chip->num_interfaces <= 0) { |
| 3675 | snd_card_disconnect(card); |
| 3676 | /* release the pcm resources */ |
| 3677 | list_for_each(p, &chip->pcm_list) { |
Clemens Ladisch | ee73339 | 2005-04-25 10:34:13 +0200 | [diff] [blame] | 3678 | snd_usb_stream_disconnect(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3679 | } |
| 3680 | /* release the midi resources */ |
| 3681 | list_for_each(p, &chip->midi_list) { |
Clemens Ladisch | ee73339 | 2005-04-25 10:34:13 +0200 | [diff] [blame] | 3682 | snd_usbmidi_disconnect(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3683 | } |
Clemens Ladisch | 84957a8 | 2005-04-29 16:23:13 +0200 | [diff] [blame] | 3684 | /* release mixer resources */ |
| 3685 | list_for_each(p, &chip->mixer_list) { |
| 3686 | snd_usb_mixer_disconnect(p); |
| 3687 | } |
Takashi Iwai | 9eb70e6 | 2008-04-17 12:53:26 +0200 | [diff] [blame] | 3688 | usb_chip[chip->index] = NULL; |
Ingo Molnar | 12aa757 | 2006-01-16 16:36:05 +0100 | [diff] [blame] | 3689 | mutex_unlock(®ister_mutex); |
Takashi Iwai | c461482 | 2006-06-23 14:38:23 +0200 | [diff] [blame] | 3690 | snd_card_free_when_closed(card); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3691 | } else { |
Ingo Molnar | 12aa757 | 2006-01-16 16:36:05 +0100 | [diff] [blame] | 3692 | mutex_unlock(®ister_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3693 | } |
| 3694 | } |
| 3695 | |
| 3696 | /* |
| 3697 | * new 2.5 USB kernel API |
| 3698 | */ |
| 3699 | static int usb_audio_probe(struct usb_interface *intf, |
| 3700 | const struct usb_device_id *id) |
| 3701 | { |
| 3702 | void *chip; |
| 3703 | chip = snd_usb_audio_probe(interface_to_usbdev(intf), intf, id); |
| 3704 | if (chip) { |
Julia Lawall | f4e9749 | 2009-01-01 18:14:35 +0100 | [diff] [blame] | 3705 | usb_set_intfdata(intf, chip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3706 | return 0; |
| 3707 | } else |
| 3708 | return -EIO; |
| 3709 | } |
| 3710 | |
| 3711 | static void usb_audio_disconnect(struct usb_interface *intf) |
| 3712 | { |
| 3713 | snd_usb_audio_disconnect(interface_to_usbdev(intf), |
Julia Lawall | f4e9749 | 2009-01-01 18:14:35 +0100 | [diff] [blame] | 3714 | usb_get_intfdata(intf)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3715 | } |
| 3716 | |
Andrew Morton | 93521d2 | 2007-12-24 14:40:56 +0100 | [diff] [blame] | 3717 | #ifdef CONFIG_PM |
Oliver Neukum | f85bf29 | 2007-12-14 14:42:41 +0100 | [diff] [blame] | 3718 | static int usb_audio_suspend(struct usb_interface *intf, pm_message_t message) |
| 3719 | { |
Julia Lawall | f4e9749 | 2009-01-01 18:14:35 +0100 | [diff] [blame] | 3720 | struct snd_usb_audio *chip = usb_get_intfdata(intf); |
Oliver Neukum | f85bf29 | 2007-12-14 14:42:41 +0100 | [diff] [blame] | 3721 | struct list_head *p; |
| 3722 | struct snd_usb_stream *as; |
| 3723 | |
| 3724 | if (chip == (void *)-1L) |
| 3725 | return 0; |
| 3726 | |
| 3727 | snd_power_change_state(chip->card, SNDRV_CTL_POWER_D3hot); |
| 3728 | if (!chip->num_suspended_intf++) { |
| 3729 | list_for_each(p, &chip->pcm_list) { |
| 3730 | as = list_entry(p, struct snd_usb_stream, list); |
| 3731 | snd_pcm_suspend_all(as->pcm); |
| 3732 | } |
| 3733 | } |
| 3734 | |
| 3735 | return 0; |
| 3736 | } |
| 3737 | |
| 3738 | static int usb_audio_resume(struct usb_interface *intf) |
| 3739 | { |
Julia Lawall | f4e9749 | 2009-01-01 18:14:35 +0100 | [diff] [blame] | 3740 | struct snd_usb_audio *chip = usb_get_intfdata(intf); |
Oliver Neukum | f85bf29 | 2007-12-14 14:42:41 +0100 | [diff] [blame] | 3741 | |
| 3742 | if (chip == (void *)-1L) |
| 3743 | return 0; |
| 3744 | if (--chip->num_suspended_intf) |
| 3745 | return 0; |
| 3746 | /* |
| 3747 | * ALSA leaves material resumption to user space |
| 3748 | * we just notify |
| 3749 | */ |
| 3750 | |
| 3751 | snd_power_change_state(chip->card, SNDRV_CTL_POWER_D0); |
| 3752 | |
| 3753 | return 0; |
| 3754 | } |
Andrew Morton | 93521d2 | 2007-12-24 14:40:56 +0100 | [diff] [blame] | 3755 | #endif /* CONFIG_PM */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3756 | |
| 3757 | static int __init snd_usb_audio_init(void) |
| 3758 | { |
Clemens Ladisch | f3990e6 | 2009-02-20 09:32:40 +0100 | [diff] [blame] | 3759 | if (nrpacks < 1 || nrpacks > MAX_PACKS) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3760 | printk(KERN_WARNING "invalid nrpacks value.\n"); |
| 3761 | return -EINVAL; |
| 3762 | } |
Tobias Klauser | cf78bbc | 2006-10-04 18:12:43 +0200 | [diff] [blame] | 3763 | return usb_register(&usb_audio_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3764 | } |
| 3765 | |
| 3766 | |
| 3767 | static void __exit snd_usb_audio_cleanup(void) |
| 3768 | { |
| 3769 | usb_deregister(&usb_audio_driver); |
| 3770 | } |
| 3771 | |
| 3772 | module_init(snd_usb_audio_init); |
| 3773 | module_exit(snd_usb_audio_cleanup); |