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