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