Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 1 | /* |
Daniel Mack | 8d04884 | 2008-04-14 15:39:14 +0200 | [diff] [blame] | 2 | * Copyright (c) 2006-2008 Daniel Mack, Karsten Wiese |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 | */ |
| 18 | |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 19 | #include <linux/spinlock.h> |
Daniel Mack | e431cf4 | 2009-03-28 21:19:49 +0100 | [diff] [blame] | 20 | #include <linux/init.h> |
| 21 | #include <linux/usb.h> |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 22 | #include <sound/core.h> |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 23 | #include <sound/pcm.h> |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 24 | |
Daniel Mack | 936e7d0 | 2009-04-01 19:05:39 +0200 | [diff] [blame] | 25 | #include "device.h" |
| 26 | #include "audio.h" |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 27 | |
| 28 | #define N_URBS 32 |
| 29 | #define CLOCK_DRIFT_TOLERANCE 5 |
| 30 | #define FRAMES_PER_URB 8 |
| 31 | #define BYTES_PER_FRAME 512 |
| 32 | #define CHANNELS_PER_STREAM 2 |
| 33 | #define BYTES_PER_SAMPLE 3 |
| 34 | #define BYTES_PER_SAMPLE_USB 4 |
| 35 | #define MAX_BUFFER_SIZE (128*1024) |
Daniel Mack | 6e9fc6b | 2008-04-14 15:40:31 +0200 | [diff] [blame] | 36 | #define MAX_ENDPOINT_SIZE 512 |
| 37 | |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 38 | #define ENDPOINT_CAPTURE 2 |
| 39 | #define ENDPOINT_PLAYBACK 6 |
| 40 | |
| 41 | #define MAKE_CHECKBYTE(dev,stream,i) \ |
| 42 | (stream << 1) | (~(i / (dev->n_streams * BYTES_PER_SAMPLE_USB)) & 1) |
| 43 | |
| 44 | static struct snd_pcm_hardware snd_usb_caiaq_pcm_hardware = { |
Daniel Mack | 9318dce | 2009-06-01 21:36:23 +0200 | [diff] [blame] | 45 | .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED | |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 46 | SNDRV_PCM_INFO_BLOCK_TRANSFER), |
| 47 | .formats = SNDRV_PCM_FMTBIT_S24_3BE, |
Daniel Mack | 9318dce | 2009-06-01 21:36:23 +0200 | [diff] [blame] | 48 | .rates = (SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 | |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 49 | SNDRV_PCM_RATE_96000), |
| 50 | .rate_min = 44100, |
| 51 | .rate_max = 0, /* will overwrite later */ |
| 52 | .channels_min = CHANNELS_PER_STREAM, |
| 53 | .channels_max = CHANNELS_PER_STREAM, |
| 54 | .buffer_bytes_max = MAX_BUFFER_SIZE, |
Daniel Mack | 09189ac | 2008-01-24 18:46:42 +0100 | [diff] [blame] | 55 | .period_bytes_min = 128, |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 56 | .period_bytes_max = MAX_BUFFER_SIZE, |
| 57 | .periods_min = 1, |
| 58 | .periods_max = 1024, |
| 59 | }; |
| 60 | |
| 61 | static void |
| 62 | activate_substream(struct snd_usb_caiaqdev *dev, |
| 63 | struct snd_pcm_substream *sub) |
| 64 | { |
| 65 | if (sub->stream == SNDRV_PCM_STREAM_PLAYBACK) |
| 66 | dev->sub_playback[sub->number] = sub; |
| 67 | else |
| 68 | dev->sub_capture[sub->number] = sub; |
| 69 | } |
| 70 | |
Daniel Mack | 9318dce | 2009-06-01 21:36:23 +0200 | [diff] [blame] | 71 | static void |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 72 | deactivate_substream(struct snd_usb_caiaqdev *dev, |
| 73 | struct snd_pcm_substream *sub) |
| 74 | { |
Daniel Mack | 8d04884 | 2008-04-14 15:39:14 +0200 | [diff] [blame] | 75 | unsigned long flags; |
| 76 | spin_lock_irqsave(&dev->spinlock, flags); |
| 77 | |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 78 | if (sub->stream == SNDRV_PCM_STREAM_PLAYBACK) |
| 79 | dev->sub_playback[sub->number] = NULL; |
| 80 | else |
| 81 | dev->sub_capture[sub->number] = NULL; |
Daniel Mack | 8d04884 | 2008-04-14 15:39:14 +0200 | [diff] [blame] | 82 | |
| 83 | spin_unlock_irqrestore(&dev->spinlock, flags); |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | static int |
| 87 | all_substreams_zero(struct snd_pcm_substream **subs) |
| 88 | { |
| 89 | int i; |
| 90 | for (i = 0; i < MAX_STREAMS; i++) |
| 91 | if (subs[i] != NULL) |
| 92 | return 0; |
| 93 | return 1; |
| 94 | } |
| 95 | |
| 96 | static int stream_start(struct snd_usb_caiaqdev *dev) |
| 97 | { |
| 98 | int i, ret; |
| 99 | |
Daniel Mack | 8d04884 | 2008-04-14 15:39:14 +0200 | [diff] [blame] | 100 | debug("%s(%p)\n", __func__, dev); |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 101 | |
Daniel Mack | 8d04884 | 2008-04-14 15:39:14 +0200 | [diff] [blame] | 102 | if (dev->streaming) |
| 103 | return -EINVAL; |
| 104 | |
| 105 | memset(dev->sub_playback, 0, sizeof(dev->sub_playback)); |
| 106 | memset(dev->sub_capture, 0, sizeof(dev->sub_capture)); |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 107 | dev->input_panic = 0; |
| 108 | dev->output_panic = 0; |
| 109 | dev->first_packet = 1; |
| 110 | dev->streaming = 1; |
Daniel Mack | 1313e70 | 2009-03-18 11:03:53 +0100 | [diff] [blame] | 111 | dev->warned = 0; |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 112 | |
| 113 | for (i = 0; i < N_URBS; i++) { |
| 114 | ret = usb_submit_urb(dev->data_urbs_in[i], GFP_ATOMIC); |
| 115 | if (ret) { |
Daniel Mack | 8d04884 | 2008-04-14 15:39:14 +0200 | [diff] [blame] | 116 | log("unable to trigger read #%d! (ret %d)\n", i, ret); |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 117 | dev->streaming = 0; |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 118 | return -EPIPE; |
| 119 | } |
| 120 | } |
Daniel Mack | 9318dce | 2009-06-01 21:36:23 +0200 | [diff] [blame] | 121 | |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 122 | return 0; |
| 123 | } |
| 124 | |
| 125 | static void stream_stop(struct snd_usb_caiaqdev *dev) |
| 126 | { |
| 127 | int i; |
Daniel Mack | 8d04884 | 2008-04-14 15:39:14 +0200 | [diff] [blame] | 128 | |
| 129 | debug("%s(%p)\n", __func__, dev); |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 130 | if (!dev->streaming) |
| 131 | return; |
Daniel Mack | 9318dce | 2009-06-01 21:36:23 +0200 | [diff] [blame] | 132 | |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 133 | dev->streaming = 0; |
Daniel Mack | 8d04884 | 2008-04-14 15:39:14 +0200 | [diff] [blame] | 134 | |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 135 | for (i = 0; i < N_URBS; i++) { |
Daniel Mack | 8d04884 | 2008-04-14 15:39:14 +0200 | [diff] [blame] | 136 | usb_kill_urb(dev->data_urbs_in[i]); |
| 137 | usb_kill_urb(dev->data_urbs_out[i]); |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 138 | } |
| 139 | } |
| 140 | |
| 141 | static int snd_usb_caiaq_substream_open(struct snd_pcm_substream *substream) |
| 142 | { |
| 143 | struct snd_usb_caiaqdev *dev = snd_pcm_substream_chip(substream); |
Daniel Mack | 8d04884 | 2008-04-14 15:39:14 +0200 | [diff] [blame] | 144 | debug("%s(%p)\n", __func__, substream); |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 145 | substream->runtime->hw = dev->pcm_info; |
| 146 | snd_pcm_limit_hw_rates(substream->runtime); |
| 147 | return 0; |
| 148 | } |
| 149 | |
| 150 | static int snd_usb_caiaq_substream_close(struct snd_pcm_substream *substream) |
| 151 | { |
| 152 | struct snd_usb_caiaqdev *dev = snd_pcm_substream_chip(substream); |
| 153 | |
Daniel Mack | 8d04884 | 2008-04-14 15:39:14 +0200 | [diff] [blame] | 154 | debug("%s(%p)\n", __func__, substream); |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 155 | if (all_substreams_zero(dev->sub_playback) && |
| 156 | all_substreams_zero(dev->sub_capture)) { |
Daniel Mack | 9318dce | 2009-06-01 21:36:23 +0200 | [diff] [blame] | 157 | /* when the last client has stopped streaming, |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 158 | * all sample rates are allowed again */ |
| 159 | stream_stop(dev); |
| 160 | dev->pcm_info.rates = dev->samplerates; |
| 161 | } |
Daniel Mack | 8d04884 | 2008-04-14 15:39:14 +0200 | [diff] [blame] | 162 | |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 163 | return 0; |
| 164 | } |
| 165 | |
| 166 | static int snd_usb_caiaq_pcm_hw_params(struct snd_pcm_substream *sub, |
| 167 | struct snd_pcm_hw_params *hw_params) |
| 168 | { |
Daniel Mack | 8d04884 | 2008-04-14 15:39:14 +0200 | [diff] [blame] | 169 | debug("%s(%p)\n", __func__, sub); |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 170 | return snd_pcm_lib_malloc_pages(sub, params_buffer_bytes(hw_params)); |
| 171 | } |
| 172 | |
| 173 | static int snd_usb_caiaq_pcm_hw_free(struct snd_pcm_substream *sub) |
| 174 | { |
| 175 | struct snd_usb_caiaqdev *dev = snd_pcm_substream_chip(sub); |
Daniel Mack | 8d04884 | 2008-04-14 15:39:14 +0200 | [diff] [blame] | 176 | debug("%s(%p)\n", __func__, sub); |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 177 | deactivate_substream(dev, sub); |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 178 | return snd_pcm_lib_free_pages(sub); |
| 179 | } |
| 180 | |
| 181 | /* this should probably go upstream */ |
| 182 | #if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12 |
| 183 | #error "Change this table" |
| 184 | #endif |
| 185 | |
| 186 | static unsigned int rates[] = { 5512, 8000, 11025, 16000, 22050, 32000, 44100, |
| 187 | 48000, 64000, 88200, 96000, 176400, 192000 }; |
| 188 | |
| 189 | static int snd_usb_caiaq_pcm_prepare(struct snd_pcm_substream *substream) |
| 190 | { |
| 191 | int bytes_per_sample, bpp, ret, i; |
| 192 | int index = substream->number; |
| 193 | struct snd_usb_caiaqdev *dev = snd_pcm_substream_chip(substream); |
| 194 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 195 | |
Daniel Mack | 8d04884 | 2008-04-14 15:39:14 +0200 | [diff] [blame] | 196 | debug("%s(%p)\n", __func__, substream); |
Daniel Mack | 9318dce | 2009-06-01 21:36:23 +0200 | [diff] [blame] | 197 | |
Daniel Mack | a9b487f | 2009-04-27 12:18:05 +0200 | [diff] [blame] | 198 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { |
| 199 | dev->period_out_count[index] = BYTES_PER_SAMPLE + 1; |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 200 | dev->audio_out_buf_pos[index] = BYTES_PER_SAMPLE + 1; |
Daniel Mack | a9b487f | 2009-04-27 12:18:05 +0200 | [diff] [blame] | 201 | } else { |
Daniel Mack | 0a842c8 | 2009-06-17 17:45:11 +0200 | [diff] [blame] | 202 | int in_pos = (dev->spec.data_alignment == 2) ? 0 : 2; |
| 203 | dev->period_in_count[index] = BYTES_PER_SAMPLE + in_pos; |
| 204 | dev->audio_in_buf_pos[index] = BYTES_PER_SAMPLE + in_pos; |
Daniel Mack | a9b487f | 2009-04-27 12:18:05 +0200 | [diff] [blame] | 205 | } |
| 206 | |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 207 | if (dev->streaming) |
| 208 | return 0; |
Daniel Mack | 9318dce | 2009-06-01 21:36:23 +0200 | [diff] [blame] | 209 | |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 210 | /* the first client that opens a stream defines the sample rate |
| 211 | * setting for all subsequent calls, until the last client closed. */ |
| 212 | for (i=0; i < ARRAY_SIZE(rates); i++) |
| 213 | if (runtime->rate == rates[i]) |
| 214 | dev->pcm_info.rates = 1 << i; |
Daniel Mack | 9318dce | 2009-06-01 21:36:23 +0200 | [diff] [blame] | 215 | |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 216 | snd_pcm_limit_hw_rates(runtime); |
| 217 | |
| 218 | bytes_per_sample = BYTES_PER_SAMPLE; |
| 219 | if (dev->spec.data_alignment == 2) |
| 220 | bytes_per_sample++; |
Daniel Mack | 9318dce | 2009-06-01 21:36:23 +0200 | [diff] [blame] | 221 | |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 222 | bpp = ((runtime->rate / 8000) + CLOCK_DRIFT_TOLERANCE) |
| 223 | * bytes_per_sample * CHANNELS_PER_STREAM * dev->n_streams; |
Daniel Mack | 6e9fc6b | 2008-04-14 15:40:31 +0200 | [diff] [blame] | 224 | |
| 225 | if (bpp > MAX_ENDPOINT_SIZE) |
| 226 | bpp = MAX_ENDPOINT_SIZE; |
| 227 | |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 228 | ret = snd_usb_caiaq_set_audio_params(dev, runtime->rate, |
| 229 | runtime->sample_bits, bpp); |
| 230 | if (ret) |
| 231 | return ret; |
| 232 | |
| 233 | ret = stream_start(dev); |
| 234 | if (ret) |
| 235 | return ret; |
Daniel Mack | 9318dce | 2009-06-01 21:36:23 +0200 | [diff] [blame] | 236 | |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 237 | dev->output_running = 0; |
| 238 | wait_event_timeout(dev->prepare_wait_queue, dev->output_running, HZ); |
| 239 | if (!dev->output_running) { |
| 240 | stream_stop(dev); |
| 241 | return -EPIPE; |
| 242 | } |
| 243 | |
| 244 | return 0; |
| 245 | } |
| 246 | |
| 247 | static int snd_usb_caiaq_pcm_trigger(struct snd_pcm_substream *sub, int cmd) |
| 248 | { |
| 249 | struct snd_usb_caiaqdev *dev = snd_pcm_substream_chip(sub); |
| 250 | |
| 251 | switch (cmd) { |
| 252 | case SNDRV_PCM_TRIGGER_START: |
| 253 | case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 254 | activate_substream(dev, sub); |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 255 | break; |
| 256 | case SNDRV_PCM_TRIGGER_STOP: |
| 257 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 258 | deactivate_substream(dev, sub); |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 259 | break; |
| 260 | default: |
| 261 | return -EINVAL; |
| 262 | } |
| 263 | |
| 264 | return 0; |
| 265 | } |
| 266 | |
| 267 | static snd_pcm_uframes_t |
| 268 | snd_usb_caiaq_pcm_pointer(struct snd_pcm_substream *sub) |
| 269 | { |
| 270 | int index = sub->number; |
| 271 | struct snd_usb_caiaqdev *dev = snd_pcm_substream_chip(sub); |
Mark Hills | 3702b08 | 2009-10-24 12:59:35 +0100 | [diff] [blame^] | 272 | snd_pcm_uframes_t ptr; |
| 273 | |
| 274 | spin_lock(&dev->spinlock); |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 275 | |
| 276 | if (dev->input_panic || dev->output_panic) |
Mark Hills | 3702b08 | 2009-10-24 12:59:35 +0100 | [diff] [blame^] | 277 | ptr = SNDRV_PCM_POS_XRUN; |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 278 | |
| 279 | if (sub->stream == SNDRV_PCM_STREAM_PLAYBACK) |
Mark Hills | 3702b08 | 2009-10-24 12:59:35 +0100 | [diff] [blame^] | 280 | ptr = bytes_to_frames(sub->runtime, |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 281 | dev->audio_out_buf_pos[index]); |
| 282 | else |
Mark Hills | 3702b08 | 2009-10-24 12:59:35 +0100 | [diff] [blame^] | 283 | ptr = bytes_to_frames(sub->runtime, |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 284 | dev->audio_in_buf_pos[index]); |
Mark Hills | 3702b08 | 2009-10-24 12:59:35 +0100 | [diff] [blame^] | 285 | |
| 286 | spin_unlock(&dev->spinlock); |
| 287 | return ptr; |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | /* operators for both playback and capture */ |
| 291 | static struct snd_pcm_ops snd_usb_caiaq_ops = { |
| 292 | .open = snd_usb_caiaq_substream_open, |
| 293 | .close = snd_usb_caiaq_substream_close, |
| 294 | .ioctl = snd_pcm_lib_ioctl, |
| 295 | .hw_params = snd_usb_caiaq_pcm_hw_params, |
| 296 | .hw_free = snd_usb_caiaq_pcm_hw_free, |
| 297 | .prepare = snd_usb_caiaq_pcm_prepare, |
| 298 | .trigger = snd_usb_caiaq_pcm_trigger, |
| 299 | .pointer = snd_usb_caiaq_pcm_pointer |
| 300 | }; |
Daniel Mack | 9318dce | 2009-06-01 21:36:23 +0200 | [diff] [blame] | 301 | |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 302 | static void check_for_elapsed_periods(struct snd_usb_caiaqdev *dev, |
| 303 | struct snd_pcm_substream **subs) |
| 304 | { |
| 305 | int stream, pb, *cnt; |
| 306 | struct snd_pcm_substream *sub; |
| 307 | |
| 308 | for (stream = 0; stream < dev->n_streams; stream++) { |
| 309 | sub = subs[stream]; |
| 310 | if (!sub) |
| 311 | continue; |
| 312 | |
Daniel Mack | a9b487f | 2009-04-27 12:18:05 +0200 | [diff] [blame] | 313 | pb = snd_pcm_lib_period_bytes(sub); |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 314 | cnt = (sub->stream == SNDRV_PCM_STREAM_PLAYBACK) ? |
| 315 | &dev->period_out_count[stream] : |
| 316 | &dev->period_in_count[stream]; |
| 317 | |
| 318 | if (*cnt >= pb) { |
| 319 | snd_pcm_period_elapsed(sub); |
| 320 | *cnt %= pb; |
| 321 | } |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | static void read_in_urb_mode0(struct snd_usb_caiaqdev *dev, |
| 326 | const struct urb *urb, |
| 327 | const struct usb_iso_packet_descriptor *iso) |
| 328 | { |
| 329 | unsigned char *usb_buf = urb->transfer_buffer + iso->offset; |
| 330 | struct snd_pcm_substream *sub; |
| 331 | int stream, i; |
| 332 | |
| 333 | if (all_substreams_zero(dev->sub_capture)) |
| 334 | return; |
| 335 | |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 336 | for (i = 0; i < iso->actual_length;) { |
| 337 | for (stream = 0; stream < dev->n_streams; stream++, i++) { |
| 338 | sub = dev->sub_capture[stream]; |
| 339 | if (sub) { |
| 340 | struct snd_pcm_runtime *rt = sub->runtime; |
| 341 | char *audio_buf = rt->dma_area; |
| 342 | int sz = frames_to_bytes(rt, rt->buffer_size); |
Daniel Mack | 9318dce | 2009-06-01 21:36:23 +0200 | [diff] [blame] | 343 | audio_buf[dev->audio_in_buf_pos[stream]++] |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 344 | = usb_buf[i]; |
| 345 | dev->period_in_count[stream]++; |
| 346 | if (dev->audio_in_buf_pos[stream] == sz) |
| 347 | dev->audio_in_buf_pos[stream] = 0; |
| 348 | } |
| 349 | } |
| 350 | } |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | static void read_in_urb_mode2(struct snd_usb_caiaqdev *dev, |
| 354 | const struct urb *urb, |
| 355 | const struct usb_iso_packet_descriptor *iso) |
| 356 | { |
| 357 | unsigned char *usb_buf = urb->transfer_buffer + iso->offset; |
| 358 | unsigned char check_byte; |
| 359 | struct snd_pcm_substream *sub; |
| 360 | int stream, i; |
| 361 | |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 362 | for (i = 0; i < iso->actual_length;) { |
| 363 | if (i % (dev->n_streams * BYTES_PER_SAMPLE_USB) == 0) { |
Daniel Mack | 9318dce | 2009-06-01 21:36:23 +0200 | [diff] [blame] | 364 | for (stream = 0; |
| 365 | stream < dev->n_streams; |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 366 | stream++, i++) { |
| 367 | if (dev->first_packet) |
| 368 | continue; |
| 369 | |
| 370 | check_byte = MAKE_CHECKBYTE(dev, stream, i); |
Daniel Mack | 9318dce | 2009-06-01 21:36:23 +0200 | [diff] [blame] | 371 | |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 372 | if ((usb_buf[i] & 0x3f) != check_byte) |
| 373 | dev->input_panic = 1; |
| 374 | |
| 375 | if (usb_buf[i] & 0x80) |
| 376 | dev->output_panic = 1; |
| 377 | } |
| 378 | } |
| 379 | dev->first_packet = 0; |
| 380 | |
| 381 | for (stream = 0; stream < dev->n_streams; stream++, i++) { |
| 382 | sub = dev->sub_capture[stream]; |
Daniel Mack | 9311c9b | 2009-03-18 11:03:54 +0100 | [diff] [blame] | 383 | if (dev->input_panic) |
| 384 | usb_buf[i] = 0; |
| 385 | |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 386 | if (sub) { |
| 387 | struct snd_pcm_runtime *rt = sub->runtime; |
| 388 | char *audio_buf = rt->dma_area; |
| 389 | int sz = frames_to_bytes(rt, rt->buffer_size); |
Karsten Wiese | a971c3d4 | 2007-03-29 17:02:45 +0200 | [diff] [blame] | 390 | audio_buf[dev->audio_in_buf_pos[stream]++] = |
| 391 | usb_buf[i]; |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 392 | dev->period_in_count[stream]++; |
| 393 | if (dev->audio_in_buf_pos[stream] == sz) |
| 394 | dev->audio_in_buf_pos[stream] = 0; |
| 395 | } |
| 396 | } |
| 397 | } |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | static void read_in_urb(struct snd_usb_caiaqdev *dev, |
| 401 | const struct urb *urb, |
| 402 | const struct usb_iso_packet_descriptor *iso) |
| 403 | { |
| 404 | if (!dev->streaming) |
| 405 | return; |
| 406 | |
Daniel Mack | 9311c9b | 2009-03-18 11:03:54 +0100 | [diff] [blame] | 407 | if (iso->actual_length < dev->bpp) |
| 408 | return; |
| 409 | |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 410 | switch (dev->spec.data_alignment) { |
| 411 | case 0: |
| 412 | read_in_urb_mode0(dev, urb, iso); |
| 413 | break; |
| 414 | case 2: |
| 415 | read_in_urb_mode2(dev, urb, iso); |
| 416 | break; |
| 417 | } |
| 418 | |
Daniel Mack | 1313e70 | 2009-03-18 11:03:53 +0100 | [diff] [blame] | 419 | if ((dev->input_panic || dev->output_panic) && !dev->warned) { |
Daniel Mack | 9318dce | 2009-06-01 21:36:23 +0200 | [diff] [blame] | 420 | debug("streaming error detected %s %s\n", |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 421 | dev->input_panic ? "(input)" : "", |
| 422 | dev->output_panic ? "(output)" : ""); |
Daniel Mack | 1313e70 | 2009-03-18 11:03:53 +0100 | [diff] [blame] | 423 | dev->warned = 1; |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 424 | } |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 425 | } |
| 426 | |
Daniel Mack | 9318dce | 2009-06-01 21:36:23 +0200 | [diff] [blame] | 427 | static void fill_out_urb(struct snd_usb_caiaqdev *dev, |
| 428 | struct urb *urb, |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 429 | const struct usb_iso_packet_descriptor *iso) |
| 430 | { |
| 431 | unsigned char *usb_buf = urb->transfer_buffer + iso->offset; |
| 432 | struct snd_pcm_substream *sub; |
| 433 | int stream, i; |
Daniel Mack | 9318dce | 2009-06-01 21:36:23 +0200 | [diff] [blame] | 434 | |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 435 | for (i = 0; i < iso->length;) { |
Karsten Wiese | a971c3d4 | 2007-03-29 17:02:45 +0200 | [diff] [blame] | 436 | for (stream = 0; stream < dev->n_streams; stream++, i++) { |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 437 | sub = dev->sub_playback[stream]; |
| 438 | if (sub) { |
| 439 | struct snd_pcm_runtime *rt = sub->runtime; |
| 440 | char *audio_buf = rt->dma_area; |
| 441 | int sz = frames_to_bytes(rt, rt->buffer_size); |
Karsten Wiese | a971c3d4 | 2007-03-29 17:02:45 +0200 | [diff] [blame] | 442 | usb_buf[i] = |
| 443 | audio_buf[dev->audio_out_buf_pos[stream]]; |
| 444 | dev->period_out_count[stream]++; |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 445 | dev->audio_out_buf_pos[stream]++; |
| 446 | if (dev->audio_out_buf_pos[stream] == sz) |
| 447 | dev->audio_out_buf_pos[stream] = 0; |
| 448 | } else |
Karsten Wiese | a971c3d4 | 2007-03-29 17:02:45 +0200 | [diff] [blame] | 449 | usb_buf[i] = 0; |
| 450 | } |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 451 | |
| 452 | /* fill in the check bytes */ |
| 453 | if (dev->spec.data_alignment == 2 && |
Daniel Mack | 9318dce | 2009-06-01 21:36:23 +0200 | [diff] [blame] | 454 | i % (dev->n_streams * BYTES_PER_SAMPLE_USB) == |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 455 | (dev->n_streams * CHANNELS_PER_STREAM)) |
| 456 | for (stream = 0; stream < dev->n_streams; stream++, i++) |
| 457 | usb_buf[i] = MAKE_CHECKBYTE(dev, stream, i); |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 458 | } |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | static void read_completed(struct urb *urb) |
| 462 | { |
Daniel Mack | 9318dce | 2009-06-01 21:36:23 +0200 | [diff] [blame] | 463 | struct snd_usb_caiaq_cb_info *info = urb->context; |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 464 | struct snd_usb_caiaqdev *dev; |
| 465 | struct urb *out; |
| 466 | int frame, len, send_it = 0, outframe = 0; |
| 467 | |
| 468 | if (urb->status || !info) |
| 469 | return; |
| 470 | |
| 471 | dev = info->dev; |
Daniel Mack | 8d04884 | 2008-04-14 15:39:14 +0200 | [diff] [blame] | 472 | |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 473 | if (!dev->streaming) |
| 474 | return; |
| 475 | |
| 476 | out = dev->data_urbs_out[info->index]; |
| 477 | |
| 478 | /* read the recently received packet and send back one which has |
| 479 | * the same layout */ |
| 480 | for (frame = 0; frame < FRAMES_PER_URB; frame++) { |
| 481 | if (urb->iso_frame_desc[frame].status) |
| 482 | continue; |
| 483 | |
| 484 | len = urb->iso_frame_desc[outframe].actual_length; |
| 485 | out->iso_frame_desc[outframe].length = len; |
| 486 | out->iso_frame_desc[outframe].actual_length = 0; |
| 487 | out->iso_frame_desc[outframe].offset = BYTES_PER_FRAME * frame; |
Daniel Mack | 9318dce | 2009-06-01 21:36:23 +0200 | [diff] [blame] | 488 | |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 489 | if (len > 0) { |
Daniel Mack | 8d04884 | 2008-04-14 15:39:14 +0200 | [diff] [blame] | 490 | spin_lock(&dev->spinlock); |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 491 | fill_out_urb(dev, out, &out->iso_frame_desc[outframe]); |
| 492 | read_in_urb(dev, urb, &urb->iso_frame_desc[frame]); |
Daniel Mack | 8d04884 | 2008-04-14 15:39:14 +0200 | [diff] [blame] | 493 | spin_unlock(&dev->spinlock); |
| 494 | check_for_elapsed_periods(dev, dev->sub_playback); |
| 495 | check_for_elapsed_periods(dev, dev->sub_capture); |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 496 | send_it = 1; |
| 497 | } |
| 498 | |
| 499 | outframe++; |
| 500 | } |
| 501 | |
| 502 | if (send_it) { |
| 503 | out->number_of_packets = FRAMES_PER_URB; |
| 504 | out->transfer_flags = URB_ISO_ASAP; |
| 505 | usb_submit_urb(out, GFP_ATOMIC); |
| 506 | } |
Daniel Mack | 9318dce | 2009-06-01 21:36:23 +0200 | [diff] [blame] | 507 | |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 508 | /* re-submit inbound urb */ |
| 509 | for (frame = 0; frame < FRAMES_PER_URB; frame++) { |
| 510 | urb->iso_frame_desc[frame].offset = BYTES_PER_FRAME * frame; |
| 511 | urb->iso_frame_desc[frame].length = BYTES_PER_FRAME; |
| 512 | urb->iso_frame_desc[frame].actual_length = 0; |
| 513 | } |
Daniel Mack | 9318dce | 2009-06-01 21:36:23 +0200 | [diff] [blame] | 514 | |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 515 | urb->number_of_packets = FRAMES_PER_URB; |
| 516 | urb->transfer_flags = URB_ISO_ASAP; |
| 517 | usb_submit_urb(urb, GFP_ATOMIC); |
| 518 | } |
| 519 | |
| 520 | static void write_completed(struct urb *urb) |
| 521 | { |
| 522 | struct snd_usb_caiaq_cb_info *info = urb->context; |
| 523 | struct snd_usb_caiaqdev *dev = info->dev; |
| 524 | |
| 525 | if (!dev->output_running) { |
| 526 | dev->output_running = 1; |
| 527 | wake_up(&dev->prepare_wait_queue); |
| 528 | } |
| 529 | } |
| 530 | |
| 531 | static struct urb **alloc_urbs(struct snd_usb_caiaqdev *dev, int dir, int *ret) |
| 532 | { |
| 533 | int i, frame; |
| 534 | struct urb **urbs; |
| 535 | struct usb_device *usb_dev = dev->chip.dev; |
| 536 | unsigned int pipe; |
| 537 | |
Daniel Mack | 9318dce | 2009-06-01 21:36:23 +0200 | [diff] [blame] | 538 | pipe = (dir == SNDRV_PCM_STREAM_PLAYBACK) ? |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 539 | usb_sndisocpipe(usb_dev, ENDPOINT_PLAYBACK) : |
| 540 | usb_rcvisocpipe(usb_dev, ENDPOINT_CAPTURE); |
| 541 | |
| 542 | urbs = kmalloc(N_URBS * sizeof(*urbs), GFP_KERNEL); |
| 543 | if (!urbs) { |
| 544 | log("unable to kmalloc() urbs, OOM!?\n"); |
| 545 | *ret = -ENOMEM; |
| 546 | return NULL; |
| 547 | } |
| 548 | |
| 549 | for (i = 0; i < N_URBS; i++) { |
| 550 | urbs[i] = usb_alloc_urb(FRAMES_PER_URB, GFP_KERNEL); |
| 551 | if (!urbs[i]) { |
| 552 | log("unable to usb_alloc_urb(), OOM!?\n"); |
| 553 | *ret = -ENOMEM; |
| 554 | return urbs; |
| 555 | } |
| 556 | |
Daniel Mack | 9318dce | 2009-06-01 21:36:23 +0200 | [diff] [blame] | 557 | urbs[i]->transfer_buffer = |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 558 | kmalloc(FRAMES_PER_URB * BYTES_PER_FRAME, GFP_KERNEL); |
| 559 | if (!urbs[i]->transfer_buffer) { |
| 560 | log("unable to kmalloc() transfer buffer, OOM!?\n"); |
| 561 | *ret = -ENOMEM; |
| 562 | return urbs; |
| 563 | } |
Daniel Mack | 9318dce | 2009-06-01 21:36:23 +0200 | [diff] [blame] | 564 | |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 565 | for (frame = 0; frame < FRAMES_PER_URB; frame++) { |
Daniel Mack | 9318dce | 2009-06-01 21:36:23 +0200 | [diff] [blame] | 566 | struct usb_iso_packet_descriptor *iso = |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 567 | &urbs[i]->iso_frame_desc[frame]; |
Daniel Mack | 9318dce | 2009-06-01 21:36:23 +0200 | [diff] [blame] | 568 | |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 569 | iso->offset = BYTES_PER_FRAME * frame; |
| 570 | iso->length = BYTES_PER_FRAME; |
| 571 | } |
Daniel Mack | 9318dce | 2009-06-01 21:36:23 +0200 | [diff] [blame] | 572 | |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 573 | urbs[i]->dev = usb_dev; |
| 574 | urbs[i]->pipe = pipe; |
Daniel Mack | 9318dce | 2009-06-01 21:36:23 +0200 | [diff] [blame] | 575 | urbs[i]->transfer_buffer_length = FRAMES_PER_URB |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 576 | * BYTES_PER_FRAME; |
| 577 | urbs[i]->context = &dev->data_cb_info[i]; |
| 578 | urbs[i]->interval = 1; |
| 579 | urbs[i]->transfer_flags = URB_ISO_ASAP; |
| 580 | urbs[i]->number_of_packets = FRAMES_PER_URB; |
| 581 | urbs[i]->complete = (dir == SNDRV_PCM_STREAM_CAPTURE) ? |
| 582 | read_completed : write_completed; |
| 583 | } |
| 584 | |
| 585 | *ret = 0; |
| 586 | return urbs; |
| 587 | } |
| 588 | |
| 589 | static void free_urbs(struct urb **urbs) |
| 590 | { |
| 591 | int i; |
| 592 | |
| 593 | if (!urbs) |
| 594 | return; |
| 595 | |
| 596 | for (i = 0; i < N_URBS; i++) { |
| 597 | if (!urbs[i]) |
| 598 | continue; |
Daniel Mack | 9318dce | 2009-06-01 21:36:23 +0200 | [diff] [blame] | 599 | |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 600 | usb_kill_urb(urbs[i]); |
| 601 | kfree(urbs[i]->transfer_buffer); |
| 602 | usb_free_urb(urbs[i]); |
| 603 | } |
| 604 | |
| 605 | kfree(urbs); |
| 606 | } |
| 607 | |
Randy Dunlap | 599c3e7 | 2008-01-16 14:56:04 +0100 | [diff] [blame] | 608 | int snd_usb_caiaq_audio_init(struct snd_usb_caiaqdev *dev) |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 609 | { |
| 610 | int i, ret; |
| 611 | |
Daniel Mack | 9318dce | 2009-06-01 21:36:23 +0200 | [diff] [blame] | 612 | dev->n_audio_in = max(dev->spec.num_analog_audio_in, |
| 613 | dev->spec.num_digital_audio_in) / |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 614 | CHANNELS_PER_STREAM; |
| 615 | dev->n_audio_out = max(dev->spec.num_analog_audio_out, |
Daniel Mack | 9318dce | 2009-06-01 21:36:23 +0200 | [diff] [blame] | 616 | dev->spec.num_digital_audio_out) / |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 617 | CHANNELS_PER_STREAM; |
| 618 | dev->n_streams = max(dev->n_audio_in, dev->n_audio_out); |
| 619 | |
| 620 | debug("dev->n_audio_in = %d\n", dev->n_audio_in); |
| 621 | debug("dev->n_audio_out = %d\n", dev->n_audio_out); |
| 622 | debug("dev->n_streams = %d\n", dev->n_streams); |
| 623 | |
| 624 | if (dev->n_streams > MAX_STREAMS) { |
| 625 | log("unable to initialize device, too many streams.\n"); |
| 626 | return -EINVAL; |
| 627 | } |
| 628 | |
Daniel Mack | 9318dce | 2009-06-01 21:36:23 +0200 | [diff] [blame] | 629 | ret = snd_pcm_new(dev->chip.card, dev->product_name, 0, |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 630 | dev->n_audio_out, dev->n_audio_in, &dev->pcm); |
| 631 | |
| 632 | if (ret < 0) { |
| 633 | log("snd_pcm_new() returned %d\n", ret); |
| 634 | return ret; |
| 635 | } |
| 636 | |
| 637 | dev->pcm->private_data = dev; |
| 638 | strcpy(dev->pcm->name, dev->product_name); |
| 639 | |
| 640 | memset(dev->sub_playback, 0, sizeof(dev->sub_playback)); |
| 641 | memset(dev->sub_capture, 0, sizeof(dev->sub_capture)); |
Daniel Mack | 9318dce | 2009-06-01 21:36:23 +0200 | [diff] [blame] | 642 | |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 643 | memcpy(&dev->pcm_info, &snd_usb_caiaq_pcm_hardware, |
| 644 | sizeof(snd_usb_caiaq_pcm_hardware)); |
| 645 | |
| 646 | /* setup samplerates */ |
| 647 | dev->samplerates = dev->pcm_info.rates; |
| 648 | switch (dev->chip.usb_id) { |
| 649 | case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AK1): |
Daniel Mack | ad1e34b | 2007-09-17 14:45:14 +0200 | [diff] [blame] | 650 | case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_RIGKONTROL3): |
Daniel Mack | f3e9d5d | 2008-05-08 15:42:15 +0200 | [diff] [blame] | 651 | case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_SESSIONIO): |
Daniel Mack | 2165592 | 2009-01-16 11:03:19 +0100 | [diff] [blame] | 652 | case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_GUITARRIGMOBILE): |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 653 | dev->samplerates |= SNDRV_PCM_RATE_192000; |
Daniel Mack | 2165592 | 2009-01-16 11:03:19 +0100 | [diff] [blame] | 654 | /* fall thru */ |
Daniel Mack | b30c494 | 2009-07-22 14:13:35 +0200 | [diff] [blame] | 655 | case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AUDIO2DJ): |
Daniel Mack | 2165592 | 2009-01-16 11:03:19 +0100 | [diff] [blame] | 656 | case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AUDIO4DJ): |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 657 | case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AUDIO8DJ): |
| 658 | dev->samplerates |= SNDRV_PCM_RATE_88200; |
| 659 | break; |
| 660 | } |
| 661 | |
Daniel Mack | 9318dce | 2009-06-01 21:36:23 +0200 | [diff] [blame] | 662 | snd_pcm_set_ops(dev->pcm, SNDRV_PCM_STREAM_PLAYBACK, |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 663 | &snd_usb_caiaq_ops); |
Daniel Mack | 9318dce | 2009-06-01 21:36:23 +0200 | [diff] [blame] | 664 | snd_pcm_set_ops(dev->pcm, SNDRV_PCM_STREAM_CAPTURE, |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 665 | &snd_usb_caiaq_ops); |
| 666 | |
| 667 | snd_pcm_lib_preallocate_pages_for_all(dev->pcm, |
| 668 | SNDRV_DMA_TYPE_CONTINUOUS, |
| 669 | snd_dma_continuous_data(GFP_KERNEL), |
| 670 | MAX_BUFFER_SIZE, MAX_BUFFER_SIZE); |
| 671 | |
| 672 | dev->data_cb_info = |
Daniel Mack | 9318dce | 2009-06-01 21:36:23 +0200 | [diff] [blame] | 673 | kmalloc(sizeof(struct snd_usb_caiaq_cb_info) * N_URBS, |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 674 | GFP_KERNEL); |
| 675 | |
| 676 | if (!dev->data_cb_info) |
| 677 | return -ENOMEM; |
| 678 | |
| 679 | for (i = 0; i < N_URBS; i++) { |
| 680 | dev->data_cb_info[i].dev = dev; |
| 681 | dev->data_cb_info[i].index = i; |
| 682 | } |
Daniel Mack | 9318dce | 2009-06-01 21:36:23 +0200 | [diff] [blame] | 683 | |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 684 | dev->data_urbs_in = alloc_urbs(dev, SNDRV_PCM_STREAM_CAPTURE, &ret); |
| 685 | if (ret < 0) { |
| 686 | kfree(dev->data_cb_info); |
| 687 | free_urbs(dev->data_urbs_in); |
| 688 | return ret; |
| 689 | } |
Daniel Mack | 9318dce | 2009-06-01 21:36:23 +0200 | [diff] [blame] | 690 | |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 691 | dev->data_urbs_out = alloc_urbs(dev, SNDRV_PCM_STREAM_PLAYBACK, &ret); |
| 692 | if (ret < 0) { |
| 693 | kfree(dev->data_cb_info); |
| 694 | free_urbs(dev->data_urbs_in); |
| 695 | free_urbs(dev->data_urbs_out); |
| 696 | return ret; |
| 697 | } |
| 698 | |
| 699 | return 0; |
| 700 | } |
| 701 | |
| 702 | void snd_usb_caiaq_audio_free(struct snd_usb_caiaqdev *dev) |
| 703 | { |
Daniel Mack | 8d04884 | 2008-04-14 15:39:14 +0200 | [diff] [blame] | 704 | debug("%s(%p)\n", __func__, dev); |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 705 | stream_stop(dev); |
| 706 | free_urbs(dev->data_urbs_in); |
| 707 | free_urbs(dev->data_urbs_out); |
| 708 | kfree(dev->data_cb_info); |
| 709 | } |
| 710 | |