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