blob: 67330af21b0e09e86adae106cad40c266a7fb96e [file] [log] [blame]
Daniel Mack523f1dc2007-03-26 19:11:24 +02001/*
Daniel Mack8d048842008-04-14 15:39:14 +02002 * Copyright (c) 2006-2008 Daniel Mack, Karsten Wiese
Daniel Mack523f1dc2007-03-26 19:11:24 +02003 *
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 Mackf1f6b8f2013-03-03 20:46:22 +010019#include <linux/device.h>
Daniel Mack523f1dc2007-03-26 19:11:24 +020020#include <linux/spinlock.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/slab.h>
Daniel Macke431cf42009-03-28 21:19:49 +010022#include <linux/init.h>
23#include <linux/usb.h>
Daniel Mack523f1dc2007-03-26 19:11:24 +020024#include <sound/core.h>
Daniel Mack523f1dc2007-03-26 19:11:24 +020025#include <sound/pcm.h>
Daniel Mack523f1dc2007-03-26 19:11:24 +020026
Daniel Mack936e7d02009-04-01 19:05:39 +020027#include "device.h"
28#include "audio.h"
Daniel Mack523f1dc2007-03-26 19:11:24 +020029
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 Mack6e9fc6b2008-04-14 15:40:31 +020038#define MAX_ENDPOINT_SIZE 512
39
Daniel Mack523f1dc2007-03-26 19:11:24 +020040#define ENDPOINT_CAPTURE 2
41#define ENDPOINT_PLAYBACK 6
42
Daniel Mack1c8470c2013-03-03 20:46:21 +010043#define MAKE_CHECKBYTE(cdev,stream,i) \
44 (stream << 1) | (~(i / (cdev->n_streams * BYTES_PER_SAMPLE_USB)) & 1)
Daniel Mack523f1dc2007-03-26 19:11:24 +020045
46static struct snd_pcm_hardware snd_usb_caiaq_pcm_hardware = {
Daniel Mack9318dce2009-06-01 21:36:23 +020047 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
Daniel Mack523f1dc2007-03-26 19:11:24 +020048 SNDRV_PCM_INFO_BLOCK_TRANSFER),
49 .formats = SNDRV_PCM_FMTBIT_S24_3BE,
Daniel Mack9318dce2009-06-01 21:36:23 +020050 .rates = (SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |
Daniel Mack523f1dc2007-03-26 19:11:24 +020051 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 Mack09189ac2008-01-24 18:46:42 +010057 .period_bytes_min = 128,
Daniel Mack523f1dc2007-03-26 19:11:24 +020058 .period_bytes_max = MAX_BUFFER_SIZE,
59 .periods_min = 1,
60 .periods_max = 1024,
61};
62
63static void
Daniel Mack1c8470c2013-03-03 20:46:21 +010064activate_substream(struct snd_usb_caiaqdev *cdev,
Daniel Mack523f1dc2007-03-26 19:11:24 +020065 struct snd_pcm_substream *sub)
66{
Daniel Mack1c8470c2013-03-03 20:46:21 +010067 spin_lock(&cdev->spinlock);
Mark Hillsac9dd9d2009-10-24 12:59:36 +010068
Daniel Mack523f1dc2007-03-26 19:11:24 +020069 if (sub->stream == SNDRV_PCM_STREAM_PLAYBACK)
Daniel Mack1c8470c2013-03-03 20:46:21 +010070 cdev->sub_playback[sub->number] = sub;
Daniel Mack523f1dc2007-03-26 19:11:24 +020071 else
Daniel Mack1c8470c2013-03-03 20:46:21 +010072 cdev->sub_capture[sub->number] = sub;
Mark Hillsac9dd9d2009-10-24 12:59:36 +010073
Daniel Mack1c8470c2013-03-03 20:46:21 +010074 spin_unlock(&cdev->spinlock);
Daniel Mack523f1dc2007-03-26 19:11:24 +020075}
76
Daniel Mack9318dce2009-06-01 21:36:23 +020077static void
Daniel Mack1c8470c2013-03-03 20:46:21 +010078deactivate_substream(struct snd_usb_caiaqdev *cdev,
Daniel Mack523f1dc2007-03-26 19:11:24 +020079 struct snd_pcm_substream *sub)
80{
Daniel Mack8d048842008-04-14 15:39:14 +020081 unsigned long flags;
Daniel Mack1c8470c2013-03-03 20:46:21 +010082 spin_lock_irqsave(&cdev->spinlock, flags);
Daniel Mack8d048842008-04-14 15:39:14 +020083
Daniel Mack523f1dc2007-03-26 19:11:24 +020084 if (sub->stream == SNDRV_PCM_STREAM_PLAYBACK)
Daniel Mack1c8470c2013-03-03 20:46:21 +010085 cdev->sub_playback[sub->number] = NULL;
Daniel Mack523f1dc2007-03-26 19:11:24 +020086 else
Daniel Mack1c8470c2013-03-03 20:46:21 +010087 cdev->sub_capture[sub->number] = NULL;
Daniel Mack8d048842008-04-14 15:39:14 +020088
Daniel Mack1c8470c2013-03-03 20:46:21 +010089 spin_unlock_irqrestore(&cdev->spinlock, flags);
Daniel Mack523f1dc2007-03-26 19:11:24 +020090}
91
92static int
93all_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 Mack1c8470c2013-03-03 20:46:21 +0100102static int stream_start(struct snd_usb_caiaqdev *cdev)
Daniel Mack523f1dc2007-03-26 19:11:24 +0200103{
104 int i, ret;
Daniel Mackf1f6b8f2013-03-03 20:46:22 +0100105 struct device *dev = caiaqdev_to_dev(cdev);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200106
Daniel Mackf1f6b8f2013-03-03 20:46:22 +0100107 dev_dbg(dev, "%s(%p)\n", __func__, cdev);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200108
Daniel Mack1c8470c2013-03-03 20:46:21 +0100109 if (cdev->streaming)
Daniel Mack8d048842008-04-14 15:39:14 +0200110 return -EINVAL;
111
Daniel Mack1c8470c2013-03-03 20:46:21 +0100112 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 Mack523f1dc2007-03-26 19:11:24 +0200119
120 for (i = 0; i < N_URBS; i++) {
Daniel Mack1c8470c2013-03-03 20:46:21 +0100121 ret = usb_submit_urb(cdev->data_urbs_in[i], GFP_ATOMIC);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200122 if (ret) {
Daniel Mackf1f6b8f2013-03-03 20:46:22 +0100123 dev_err(dev, "unable to trigger read #%d! (ret %d)\n",
124 i, ret);
Daniel Mack1c8470c2013-03-03 20:46:21 +0100125 cdev->streaming = 0;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200126 return -EPIPE;
127 }
128 }
Daniel Mack9318dce2009-06-01 21:36:23 +0200129
Daniel Mack523f1dc2007-03-26 19:11:24 +0200130 return 0;
131}
132
Daniel Mack1c8470c2013-03-03 20:46:21 +0100133static void stream_stop(struct snd_usb_caiaqdev *cdev)
Daniel Mack523f1dc2007-03-26 19:11:24 +0200134{
135 int i;
Daniel Mackf1f6b8f2013-03-03 20:46:22 +0100136 struct device *dev = caiaqdev_to_dev(cdev);
Daniel Mack8d048842008-04-14 15:39:14 +0200137
Daniel Mackf1f6b8f2013-03-03 20:46:22 +0100138 dev_dbg(dev, "%s(%p)\n", __func__, cdev);
Daniel Mack1c8470c2013-03-03 20:46:21 +0100139 if (!cdev->streaming)
Daniel Mack523f1dc2007-03-26 19:11:24 +0200140 return;
Daniel Mack9318dce2009-06-01 21:36:23 +0200141
Daniel Mack1c8470c2013-03-03 20:46:21 +0100142 cdev->streaming = 0;
Daniel Mack8d048842008-04-14 15:39:14 +0200143
Daniel Mack523f1dc2007-03-26 19:11:24 +0200144 for (i = 0; i < N_URBS; i++) {
Daniel Mack1c8470c2013-03-03 20:46:21 +0100145 usb_kill_urb(cdev->data_urbs_in[i]);
Daniel Mackda6094e2011-08-14 11:31:16 +0200146
Daniel Mack1c8470c2013-03-03 20:46:21 +0100147 if (test_bit(i, &cdev->outurb_active_mask))
148 usb_kill_urb(cdev->data_urbs_out[i]);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200149 }
Daniel Mackda6094e2011-08-14 11:31:16 +0200150
Daniel Mack1c8470c2013-03-03 20:46:21 +0100151 cdev->outurb_active_mask = 0;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200152}
153
154static int snd_usb_caiaq_substream_open(struct snd_pcm_substream *substream)
155{
Daniel Mack1c8470c2013-03-03 20:46:21 +0100156 struct snd_usb_caiaqdev *cdev = snd_pcm_substream_chip(substream);
Daniel Mackf1f6b8f2013-03-03 20:46:22 +0100157 struct device *dev = caiaqdev_to_dev(cdev);
158
159 dev_dbg(dev, "%s(%p)\n", __func__, substream);
Daniel Mack1c8470c2013-03-03 20:46:21 +0100160 substream->runtime->hw = cdev->pcm_info;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200161 snd_pcm_limit_hw_rates(substream->runtime);
Daniel Mackf1f6b8f2013-03-03 20:46:22 +0100162
Daniel Mack523f1dc2007-03-26 19:11:24 +0200163 return 0;
164}
165
166static int snd_usb_caiaq_substream_close(struct snd_pcm_substream *substream)
167{
Daniel Mack1c8470c2013-03-03 20:46:21 +0100168 struct snd_usb_caiaqdev *cdev = snd_pcm_substream_chip(substream);
Daniel Mackf1f6b8f2013-03-03 20:46:22 +0100169 struct device *dev = caiaqdev_to_dev(cdev);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200170
Daniel Mackf1f6b8f2013-03-03 20:46:22 +0100171 dev_dbg(dev, "%s(%p)\n", __func__, substream);
Daniel Mack1c8470c2013-03-03 20:46:21 +0100172 if (all_substreams_zero(cdev->sub_playback) &&
173 all_substreams_zero(cdev->sub_capture)) {
Daniel Mack9318dce2009-06-01 21:36:23 +0200174 /* when the last client has stopped streaming,
Daniel Mack523f1dc2007-03-26 19:11:24 +0200175 * all sample rates are allowed again */
Daniel Mack1c8470c2013-03-03 20:46:21 +0100176 stream_stop(cdev);
177 cdev->pcm_info.rates = cdev->samplerates;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200178 }
Daniel Mack8d048842008-04-14 15:39:14 +0200179
Daniel Mack523f1dc2007-03-26 19:11:24 +0200180 return 0;
181}
182
183static int snd_usb_caiaq_pcm_hw_params(struct snd_pcm_substream *sub,
Daniel Mack15c5ab62010-09-10 17:04:57 +0800184 struct snd_pcm_hw_params *hw_params)
Daniel Mack523f1dc2007-03-26 19:11:24 +0200185{
Daniel Mack523f1dc2007-03-26 19:11:24 +0200186 return snd_pcm_lib_malloc_pages(sub, params_buffer_bytes(hw_params));
187}
188
189static int snd_usb_caiaq_pcm_hw_free(struct snd_pcm_substream *sub)
190{
Daniel Mack1c8470c2013-03-03 20:46:21 +0100191 struct snd_usb_caiaqdev *cdev = snd_pcm_substream_chip(sub);
Daniel Mack1c8470c2013-03-03 20:46:21 +0100192 deactivate_substream(cdev, sub);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200193 return snd_pcm_lib_free_pages(sub);
194}
195
196/* this should probably go upstream */
197#if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
198#error "Change this table"
199#endif
200
201static unsigned int rates[] = { 5512, 8000, 11025, 16000, 22050, 32000, 44100,
Daniel Mack15c5ab62010-09-10 17:04:57 +0800202 48000, 64000, 88200, 96000, 176400, 192000 };
Daniel Mack523f1dc2007-03-26 19:11:24 +0200203
204static int snd_usb_caiaq_pcm_prepare(struct snd_pcm_substream *substream)
205{
206 int bytes_per_sample, bpp, ret, i;
207 int index = substream->number;
Daniel Mack1c8470c2013-03-03 20:46:21 +0100208 struct snd_usb_caiaqdev *cdev = snd_pcm_substream_chip(substream);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200209 struct snd_pcm_runtime *runtime = substream->runtime;
Daniel Mackf1f6b8f2013-03-03 20:46:22 +0100210 struct device *dev = caiaqdev_to_dev(cdev);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200211
Daniel Mackf1f6b8f2013-03-03 20:46:22 +0100212 dev_dbg(dev, "%s(%p)\n", __func__, substream);
Daniel Mack9318dce2009-06-01 21:36:23 +0200213
Daniel Macka9b487f2009-04-27 12:18:05 +0200214 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Daniel Mack15c5ab62010-09-10 17:04:57 +0800215 int out_pos;
216
Daniel Mack1c8470c2013-03-03 20:46:21 +0100217 switch (cdev->spec.data_alignment) {
Daniel Mack15c5ab62010-09-10 17:04:57 +0800218 case 0:
219 case 2:
220 out_pos = BYTES_PER_SAMPLE + 1;
221 break;
222 case 3:
223 default:
224 out_pos = 0;
225 break;
226 }
227
Daniel Mack1c8470c2013-03-03 20:46:21 +0100228 cdev->period_out_count[index] = out_pos;
229 cdev->audio_out_buf_pos[index] = out_pos;
Daniel Macka9b487f2009-04-27 12:18:05 +0200230 } else {
Daniel Mack15c5ab62010-09-10 17:04:57 +0800231 int in_pos;
232
Daniel Mack1c8470c2013-03-03 20:46:21 +0100233 switch (cdev->spec.data_alignment) {
Daniel Mack15c5ab62010-09-10 17:04:57 +0800234 case 0:
235 in_pos = BYTES_PER_SAMPLE + 2;
236 break;
237 case 2:
238 in_pos = BYTES_PER_SAMPLE;
239 break;
240 case 3:
241 default:
242 in_pos = 0;
243 break;
244 }
245
Daniel Mack1c8470c2013-03-03 20:46:21 +0100246 cdev->period_in_count[index] = in_pos;
247 cdev->audio_in_buf_pos[index] = in_pos;
Daniel Macka9b487f2009-04-27 12:18:05 +0200248 }
249
Daniel Mack1c8470c2013-03-03 20:46:21 +0100250 if (cdev->streaming)
Daniel Mack523f1dc2007-03-26 19:11:24 +0200251 return 0;
Daniel Mack9318dce2009-06-01 21:36:23 +0200252
Daniel Mack523f1dc2007-03-26 19:11:24 +0200253 /* the first client that opens a stream defines the sample rate
254 * setting for all subsequent calls, until the last client closed. */
255 for (i=0; i < ARRAY_SIZE(rates); i++)
256 if (runtime->rate == rates[i])
Daniel Mack1c8470c2013-03-03 20:46:21 +0100257 cdev->pcm_info.rates = 1 << i;
Daniel Mack9318dce2009-06-01 21:36:23 +0200258
Daniel Mack523f1dc2007-03-26 19:11:24 +0200259 snd_pcm_limit_hw_rates(runtime);
260
261 bytes_per_sample = BYTES_PER_SAMPLE;
Daniel Mack1c8470c2013-03-03 20:46:21 +0100262 if (cdev->spec.data_alignment >= 2)
Daniel Mack523f1dc2007-03-26 19:11:24 +0200263 bytes_per_sample++;
Daniel Mack9318dce2009-06-01 21:36:23 +0200264
Daniel Mack523f1dc2007-03-26 19:11:24 +0200265 bpp = ((runtime->rate / 8000) + CLOCK_DRIFT_TOLERANCE)
Daniel Mack1c8470c2013-03-03 20:46:21 +0100266 * bytes_per_sample * CHANNELS_PER_STREAM * cdev->n_streams;
Daniel Mack6e9fc6b2008-04-14 15:40:31 +0200267
268 if (bpp > MAX_ENDPOINT_SIZE)
269 bpp = MAX_ENDPOINT_SIZE;
270
Daniel Mack1c8470c2013-03-03 20:46:21 +0100271 ret = snd_usb_caiaq_set_audio_params(cdev, runtime->rate,
Daniel Mack523f1dc2007-03-26 19:11:24 +0200272 runtime->sample_bits, bpp);
273 if (ret)
274 return ret;
275
Daniel Mack1c8470c2013-03-03 20:46:21 +0100276 ret = stream_start(cdev);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200277 if (ret)
278 return ret;
Daniel Mack9318dce2009-06-01 21:36:23 +0200279
Daniel Mack1c8470c2013-03-03 20:46:21 +0100280 cdev->output_running = 0;
281 wait_event_timeout(cdev->prepare_wait_queue, cdev->output_running, HZ);
282 if (!cdev->output_running) {
283 stream_stop(cdev);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200284 return -EPIPE;
285 }
286
287 return 0;
288}
289
290static int snd_usb_caiaq_pcm_trigger(struct snd_pcm_substream *sub, int cmd)
291{
Daniel Mack1c8470c2013-03-03 20:46:21 +0100292 struct snd_usb_caiaqdev *cdev = snd_pcm_substream_chip(sub);
Daniel Mackf1f6b8f2013-03-03 20:46:22 +0100293 struct device *dev = caiaqdev_to_dev(cdev);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200294
Daniel Mackf1f6b8f2013-03-03 20:46:22 +0100295 dev_dbg(dev, "%s(%p) cmd %d\n", __func__, sub, cmd);
Daniel Mack15c5ab62010-09-10 17:04:57 +0800296
Daniel Mack523f1dc2007-03-26 19:11:24 +0200297 switch (cmd) {
298 case SNDRV_PCM_TRIGGER_START:
299 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Daniel Mack1c8470c2013-03-03 20:46:21 +0100300 activate_substream(cdev, sub);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200301 break;
302 case SNDRV_PCM_TRIGGER_STOP:
303 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
Daniel Mack1c8470c2013-03-03 20:46:21 +0100304 deactivate_substream(cdev, sub);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200305 break;
306 default:
307 return -EINVAL;
308 }
309
310 return 0;
311}
312
313static snd_pcm_uframes_t
314snd_usb_caiaq_pcm_pointer(struct snd_pcm_substream *sub)
315{
316 int index = sub->number;
Daniel Mack1c8470c2013-03-03 20:46:21 +0100317 struct snd_usb_caiaqdev *cdev = snd_pcm_substream_chip(sub);
Mark Hills3702b082009-10-24 12:59:35 +0100318 snd_pcm_uframes_t ptr;
319
Daniel Mack1c8470c2013-03-03 20:46:21 +0100320 spin_lock(&cdev->spinlock);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200321
Daniel Mack1c8470c2013-03-03 20:46:21 +0100322 if (cdev->input_panic || cdev->output_panic) {
Mark Hills3702b082009-10-24 12:59:35 +0100323 ptr = SNDRV_PCM_POS_XRUN;
Mark Hillscb74eb12012-02-21 21:26:31 +0000324 goto unlock;
325 }
Daniel Mack523f1dc2007-03-26 19:11:24 +0200326
327 if (sub->stream == SNDRV_PCM_STREAM_PLAYBACK)
Mark Hills3702b082009-10-24 12:59:35 +0100328 ptr = bytes_to_frames(sub->runtime,
Daniel Mack1c8470c2013-03-03 20:46:21 +0100329 cdev->audio_out_buf_pos[index]);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200330 else
Mark Hills3702b082009-10-24 12:59:35 +0100331 ptr = bytes_to_frames(sub->runtime,
Daniel Mack1c8470c2013-03-03 20:46:21 +0100332 cdev->audio_in_buf_pos[index]);
Mark Hills3702b082009-10-24 12:59:35 +0100333
Mark Hillscb74eb12012-02-21 21:26:31 +0000334unlock:
Daniel Mack1c8470c2013-03-03 20:46:21 +0100335 spin_unlock(&cdev->spinlock);
Mark Hills3702b082009-10-24 12:59:35 +0100336 return ptr;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200337}
338
339/* operators for both playback and capture */
340static struct snd_pcm_ops snd_usb_caiaq_ops = {
341 .open = snd_usb_caiaq_substream_open,
342 .close = snd_usb_caiaq_substream_close,
343 .ioctl = snd_pcm_lib_ioctl,
344 .hw_params = snd_usb_caiaq_pcm_hw_params,
345 .hw_free = snd_usb_caiaq_pcm_hw_free,
346 .prepare = snd_usb_caiaq_pcm_prepare,
347 .trigger = snd_usb_caiaq_pcm_trigger,
348 .pointer = snd_usb_caiaq_pcm_pointer
349};
Daniel Mack9318dce2009-06-01 21:36:23 +0200350
Daniel Mack1c8470c2013-03-03 20:46:21 +0100351static void check_for_elapsed_periods(struct snd_usb_caiaqdev *cdev,
Daniel Mack523f1dc2007-03-26 19:11:24 +0200352 struct snd_pcm_substream **subs)
353{
354 int stream, pb, *cnt;
355 struct snd_pcm_substream *sub;
356
Daniel Mack1c8470c2013-03-03 20:46:21 +0100357 for (stream = 0; stream < cdev->n_streams; stream++) {
Daniel Mack523f1dc2007-03-26 19:11:24 +0200358 sub = subs[stream];
359 if (!sub)
360 continue;
361
Daniel Macka9b487f2009-04-27 12:18:05 +0200362 pb = snd_pcm_lib_period_bytes(sub);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200363 cnt = (sub->stream == SNDRV_PCM_STREAM_PLAYBACK) ?
Daniel Mack1c8470c2013-03-03 20:46:21 +0100364 &cdev->period_out_count[stream] :
365 &cdev->period_in_count[stream];
Daniel Mack523f1dc2007-03-26 19:11:24 +0200366
367 if (*cnt >= pb) {
368 snd_pcm_period_elapsed(sub);
369 *cnt %= pb;
370 }
371 }
372}
373
Daniel Mack1c8470c2013-03-03 20:46:21 +0100374static void read_in_urb_mode0(struct snd_usb_caiaqdev *cdev,
Daniel Mack523f1dc2007-03-26 19:11:24 +0200375 const struct urb *urb,
376 const struct usb_iso_packet_descriptor *iso)
377{
378 unsigned char *usb_buf = urb->transfer_buffer + iso->offset;
379 struct snd_pcm_substream *sub;
380 int stream, i;
381
Daniel Mack1c8470c2013-03-03 20:46:21 +0100382 if (all_substreams_zero(cdev->sub_capture))
Daniel Mack523f1dc2007-03-26 19:11:24 +0200383 return;
384
Daniel Mack523f1dc2007-03-26 19:11:24 +0200385 for (i = 0; i < iso->actual_length;) {
Daniel Mack1c8470c2013-03-03 20:46:21 +0100386 for (stream = 0; stream < cdev->n_streams; stream++, i++) {
387 sub = cdev->sub_capture[stream];
Daniel Mack523f1dc2007-03-26 19:11:24 +0200388 if (sub) {
389 struct snd_pcm_runtime *rt = sub->runtime;
390 char *audio_buf = rt->dma_area;
391 int sz = frames_to_bytes(rt, rt->buffer_size);
Daniel Mack1c8470c2013-03-03 20:46:21 +0100392 audio_buf[cdev->audio_in_buf_pos[stream]++]
Daniel Mack523f1dc2007-03-26 19:11:24 +0200393 = usb_buf[i];
Daniel Mack1c8470c2013-03-03 20:46:21 +0100394 cdev->period_in_count[stream]++;
395 if (cdev->audio_in_buf_pos[stream] == sz)
396 cdev->audio_in_buf_pos[stream] = 0;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200397 }
398 }
399 }
Daniel Mack523f1dc2007-03-26 19:11:24 +0200400}
401
Daniel Mack1c8470c2013-03-03 20:46:21 +0100402static void read_in_urb_mode2(struct snd_usb_caiaqdev *cdev,
Daniel Mack523f1dc2007-03-26 19:11:24 +0200403 const struct urb *urb,
404 const struct usb_iso_packet_descriptor *iso)
405{
406 unsigned char *usb_buf = urb->transfer_buffer + iso->offset;
407 unsigned char check_byte;
408 struct snd_pcm_substream *sub;
409 int stream, i;
410
Daniel Mack523f1dc2007-03-26 19:11:24 +0200411 for (i = 0; i < iso->actual_length;) {
Daniel Mack1c8470c2013-03-03 20:46:21 +0100412 if (i % (cdev->n_streams * BYTES_PER_SAMPLE_USB) == 0) {
Daniel Mack9318dce2009-06-01 21:36:23 +0200413 for (stream = 0;
Daniel Mack1c8470c2013-03-03 20:46:21 +0100414 stream < cdev->n_streams;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200415 stream++, i++) {
Daniel Mack1c8470c2013-03-03 20:46:21 +0100416 if (cdev->first_packet)
Daniel Mack523f1dc2007-03-26 19:11:24 +0200417 continue;
418
Daniel Mack1c8470c2013-03-03 20:46:21 +0100419 check_byte = MAKE_CHECKBYTE(cdev, stream, i);
Daniel Mack9318dce2009-06-01 21:36:23 +0200420
Daniel Mack523f1dc2007-03-26 19:11:24 +0200421 if ((usb_buf[i] & 0x3f) != check_byte)
Daniel Mack1c8470c2013-03-03 20:46:21 +0100422 cdev->input_panic = 1;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200423
424 if (usb_buf[i] & 0x80)
Daniel Mack1c8470c2013-03-03 20:46:21 +0100425 cdev->output_panic = 1;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200426 }
427 }
Daniel Mack1c8470c2013-03-03 20:46:21 +0100428 cdev->first_packet = 0;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200429
Daniel Mack1c8470c2013-03-03 20:46:21 +0100430 for (stream = 0; stream < cdev->n_streams; stream++, i++) {
431 sub = cdev->sub_capture[stream];
432 if (cdev->input_panic)
Daniel Mack9311c9b2009-03-18 11:03:54 +0100433 usb_buf[i] = 0;
434
Daniel Mack523f1dc2007-03-26 19:11:24 +0200435 if (sub) {
436 struct snd_pcm_runtime *rt = sub->runtime;
437 char *audio_buf = rt->dma_area;
438 int sz = frames_to_bytes(rt, rt->buffer_size);
Daniel Mack1c8470c2013-03-03 20:46:21 +0100439 audio_buf[cdev->audio_in_buf_pos[stream]++] =
Karsten Wiesea971c3d42007-03-29 17:02:45 +0200440 usb_buf[i];
Daniel Mack1c8470c2013-03-03 20:46:21 +0100441 cdev->period_in_count[stream]++;
442 if (cdev->audio_in_buf_pos[stream] == sz)
443 cdev->audio_in_buf_pos[stream] = 0;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200444 }
445 }
446 }
Daniel Mack523f1dc2007-03-26 19:11:24 +0200447}
448
Daniel Mack1c8470c2013-03-03 20:46:21 +0100449static void read_in_urb_mode3(struct snd_usb_caiaqdev *cdev,
Daniel Mack15c5ab62010-09-10 17:04:57 +0800450 const struct urb *urb,
451 const struct usb_iso_packet_descriptor *iso)
452{
453 unsigned char *usb_buf = urb->transfer_buffer + iso->offset;
Daniel Mackf1f6b8f2013-03-03 20:46:22 +0100454 struct device *dev = caiaqdev_to_dev(cdev);
Daniel Mack15c5ab62010-09-10 17:04:57 +0800455 int stream, i;
456
457 /* paranoia check */
458 if (iso->actual_length % (BYTES_PER_SAMPLE_USB * CHANNELS_PER_STREAM))
459 return;
460
461 for (i = 0; i < iso->actual_length;) {
Daniel Mack1c8470c2013-03-03 20:46:21 +0100462 for (stream = 0; stream < cdev->n_streams; stream++) {
463 struct snd_pcm_substream *sub = cdev->sub_capture[stream];
Daniel Mack15c5ab62010-09-10 17:04:57 +0800464 char *audio_buf = NULL;
465 int c, n, sz = 0;
466
Daniel Mack1c8470c2013-03-03 20:46:21 +0100467 if (sub && !cdev->input_panic) {
Daniel Mack15c5ab62010-09-10 17:04:57 +0800468 struct snd_pcm_runtime *rt = sub->runtime;
469 audio_buf = rt->dma_area;
470 sz = frames_to_bytes(rt, rt->buffer_size);
471 }
472
473 for (c = 0; c < CHANNELS_PER_STREAM; c++) {
474 /* 3 audio data bytes, followed by 1 check byte */
475 if (audio_buf) {
476 for (n = 0; n < BYTES_PER_SAMPLE; n++) {
Daniel Mack1c8470c2013-03-03 20:46:21 +0100477 audio_buf[cdev->audio_in_buf_pos[stream]++] = usb_buf[i+n];
Daniel Mack15c5ab62010-09-10 17:04:57 +0800478
Daniel Mack1c8470c2013-03-03 20:46:21 +0100479 if (cdev->audio_in_buf_pos[stream] == sz)
480 cdev->audio_in_buf_pos[stream] = 0;
Daniel Mack15c5ab62010-09-10 17:04:57 +0800481 }
482
Daniel Mack1c8470c2013-03-03 20:46:21 +0100483 cdev->period_in_count[stream] += BYTES_PER_SAMPLE;
Daniel Mack15c5ab62010-09-10 17:04:57 +0800484 }
485
486 i += BYTES_PER_SAMPLE;
487
488 if (usb_buf[i] != ((stream << 1) | c) &&
Daniel Mack1c8470c2013-03-03 20:46:21 +0100489 !cdev->first_packet) {
490 if (!cdev->input_panic)
Daniel Mackf1f6b8f2013-03-03 20:46:22 +0100491 dev_warn(dev, " EXPECTED: %02x got %02x, c %d, stream %d, i %d\n",
492 ((stream << 1) | c), usb_buf[i], c, stream, i);
Daniel Mack1c8470c2013-03-03 20:46:21 +0100493 cdev->input_panic = 1;
Daniel Mack15c5ab62010-09-10 17:04:57 +0800494 }
495
496 i++;
497 }
498 }
499 }
500
Daniel Mack1c8470c2013-03-03 20:46:21 +0100501 if (cdev->first_packet > 0)
502 cdev->first_packet--;
Daniel Mack15c5ab62010-09-10 17:04:57 +0800503}
504
Daniel Mack1c8470c2013-03-03 20:46:21 +0100505static void read_in_urb(struct snd_usb_caiaqdev *cdev,
Daniel Mack523f1dc2007-03-26 19:11:24 +0200506 const struct urb *urb,
507 const struct usb_iso_packet_descriptor *iso)
508{
Daniel Mackf1f6b8f2013-03-03 20:46:22 +0100509 struct device *dev = caiaqdev_to_dev(cdev);
510
Daniel Mack1c8470c2013-03-03 20:46:21 +0100511 if (!cdev->streaming)
Daniel Mack523f1dc2007-03-26 19:11:24 +0200512 return;
513
Daniel Mack1c8470c2013-03-03 20:46:21 +0100514 if (iso->actual_length < cdev->bpp)
Daniel Mack9311c9b2009-03-18 11:03:54 +0100515 return;
516
Daniel Mack1c8470c2013-03-03 20:46:21 +0100517 switch (cdev->spec.data_alignment) {
Daniel Mack523f1dc2007-03-26 19:11:24 +0200518 case 0:
Daniel Mack1c8470c2013-03-03 20:46:21 +0100519 read_in_urb_mode0(cdev, urb, iso);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200520 break;
521 case 2:
Daniel Mack1c8470c2013-03-03 20:46:21 +0100522 read_in_urb_mode2(cdev, urb, iso);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200523 break;
Daniel Mack15c5ab62010-09-10 17:04:57 +0800524 case 3:
Daniel Mack1c8470c2013-03-03 20:46:21 +0100525 read_in_urb_mode3(cdev, urb, iso);
Daniel Mack15c5ab62010-09-10 17:04:57 +0800526 break;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200527 }
528
Daniel Mack1c8470c2013-03-03 20:46:21 +0100529 if ((cdev->input_panic || cdev->output_panic) && !cdev->warned) {
Daniel Mackf1f6b8f2013-03-03 20:46:22 +0100530 dev_warn(dev, "streaming error detected %s %s\n",
Daniel Mack1c8470c2013-03-03 20:46:21 +0100531 cdev->input_panic ? "(input)" : "",
532 cdev->output_panic ? "(output)" : "");
533 cdev->warned = 1;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200534 }
Daniel Mack523f1dc2007-03-26 19:11:24 +0200535}
536
Daniel Mack1c8470c2013-03-03 20:46:21 +0100537static void fill_out_urb_mode_0(struct snd_usb_caiaqdev *cdev,
Daniel Mack15c5ab62010-09-10 17:04:57 +0800538 struct urb *urb,
539 const struct usb_iso_packet_descriptor *iso)
Daniel Mack523f1dc2007-03-26 19:11:24 +0200540{
541 unsigned char *usb_buf = urb->transfer_buffer + iso->offset;
542 struct snd_pcm_substream *sub;
543 int stream, i;
Daniel Mack9318dce2009-06-01 21:36:23 +0200544
Daniel Mack523f1dc2007-03-26 19:11:24 +0200545 for (i = 0; i < iso->length;) {
Daniel Mack1c8470c2013-03-03 20:46:21 +0100546 for (stream = 0; stream < cdev->n_streams; stream++, i++) {
547 sub = cdev->sub_playback[stream];
Daniel Mack523f1dc2007-03-26 19:11:24 +0200548 if (sub) {
549 struct snd_pcm_runtime *rt = sub->runtime;
550 char *audio_buf = rt->dma_area;
551 int sz = frames_to_bytes(rt, rt->buffer_size);
Karsten Wiesea971c3d42007-03-29 17:02:45 +0200552 usb_buf[i] =
Daniel Mack1c8470c2013-03-03 20:46:21 +0100553 audio_buf[cdev->audio_out_buf_pos[stream]];
554 cdev->period_out_count[stream]++;
555 cdev->audio_out_buf_pos[stream]++;
556 if (cdev->audio_out_buf_pos[stream] == sz)
557 cdev->audio_out_buf_pos[stream] = 0;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200558 } else
Karsten Wiesea971c3d42007-03-29 17:02:45 +0200559 usb_buf[i] = 0;
560 }
Daniel Mack523f1dc2007-03-26 19:11:24 +0200561
562 /* fill in the check bytes */
Daniel Mack1c8470c2013-03-03 20:46:21 +0100563 if (cdev->spec.data_alignment == 2 &&
564 i % (cdev->n_streams * BYTES_PER_SAMPLE_USB) ==
565 (cdev->n_streams * CHANNELS_PER_STREAM))
566 for (stream = 0; stream < cdev->n_streams; stream++, i++)
567 usb_buf[i] = MAKE_CHECKBYTE(cdev, stream, i);
Daniel Mack15c5ab62010-09-10 17:04:57 +0800568 }
569}
570
Daniel Mack1c8470c2013-03-03 20:46:21 +0100571static void fill_out_urb_mode_3(struct snd_usb_caiaqdev *cdev,
Daniel Mack15c5ab62010-09-10 17:04:57 +0800572 struct urb *urb,
573 const struct usb_iso_packet_descriptor *iso)
574{
575 unsigned char *usb_buf = urb->transfer_buffer + iso->offset;
576 int stream, i;
577
578 for (i = 0; i < iso->length;) {
Daniel Mack1c8470c2013-03-03 20:46:21 +0100579 for (stream = 0; stream < cdev->n_streams; stream++) {
580 struct snd_pcm_substream *sub = cdev->sub_playback[stream];
Daniel Mack15c5ab62010-09-10 17:04:57 +0800581 char *audio_buf = NULL;
582 int c, n, sz = 0;
583
584 if (sub) {
585 struct snd_pcm_runtime *rt = sub->runtime;
586 audio_buf = rt->dma_area;
587 sz = frames_to_bytes(rt, rt->buffer_size);
588 }
589
590 for (c = 0; c < CHANNELS_PER_STREAM; c++) {
591 for (n = 0; n < BYTES_PER_SAMPLE; n++) {
592 if (audio_buf) {
Daniel Mack1c8470c2013-03-03 20:46:21 +0100593 usb_buf[i+n] = audio_buf[cdev->audio_out_buf_pos[stream]++];
Daniel Mack15c5ab62010-09-10 17:04:57 +0800594
Daniel Mack1c8470c2013-03-03 20:46:21 +0100595 if (cdev->audio_out_buf_pos[stream] == sz)
596 cdev->audio_out_buf_pos[stream] = 0;
Daniel Mack15c5ab62010-09-10 17:04:57 +0800597 } else {
598 usb_buf[i+n] = 0;
599 }
600 }
601
602 if (audio_buf)
Daniel Mack1c8470c2013-03-03 20:46:21 +0100603 cdev->period_out_count[stream] += BYTES_PER_SAMPLE;
Daniel Mack15c5ab62010-09-10 17:04:57 +0800604
605 i += BYTES_PER_SAMPLE;
606
607 /* fill in the check byte pattern */
608 usb_buf[i++] = (stream << 1) | c;
609 }
610 }
611 }
612}
613
Daniel Mack1c8470c2013-03-03 20:46:21 +0100614static inline void fill_out_urb(struct snd_usb_caiaqdev *cdev,
Daniel Mack15c5ab62010-09-10 17:04:57 +0800615 struct urb *urb,
616 const struct usb_iso_packet_descriptor *iso)
617{
Daniel Mack1c8470c2013-03-03 20:46:21 +0100618 switch (cdev->spec.data_alignment) {
Daniel Mack15c5ab62010-09-10 17:04:57 +0800619 case 0:
620 case 2:
Daniel Mack1c8470c2013-03-03 20:46:21 +0100621 fill_out_urb_mode_0(cdev, urb, iso);
Daniel Mack15c5ab62010-09-10 17:04:57 +0800622 break;
623 case 3:
Daniel Mack1c8470c2013-03-03 20:46:21 +0100624 fill_out_urb_mode_3(cdev, urb, iso);
Daniel Mack15c5ab62010-09-10 17:04:57 +0800625 break;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200626 }
Daniel Mack523f1dc2007-03-26 19:11:24 +0200627}
628
629static void read_completed(struct urb *urb)
630{
Daniel Mack9318dce2009-06-01 21:36:23 +0200631 struct snd_usb_caiaq_cb_info *info = urb->context;
Daniel Mack1c8470c2013-03-03 20:46:21 +0100632 struct snd_usb_caiaqdev *cdev;
Daniel Mackf1f6b8f2013-03-03 20:46:22 +0100633 struct device *dev;
Daniel Mackda6094e2011-08-14 11:31:16 +0200634 struct urb *out = NULL;
635 int i, frame, len, send_it = 0, outframe = 0;
Daniel Mack15439bd2011-08-05 13:49:52 +0200636 size_t offset = 0;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200637
638 if (urb->status || !info)
639 return;
640
Daniel Mack1c8470c2013-03-03 20:46:21 +0100641 cdev = info->cdev;
Daniel Mackf1f6b8f2013-03-03 20:46:22 +0100642 dev = caiaqdev_to_dev(cdev);
Daniel Mack8d048842008-04-14 15:39:14 +0200643
Daniel Mack1c8470c2013-03-03 20:46:21 +0100644 if (!cdev->streaming)
Daniel Mack523f1dc2007-03-26 19:11:24 +0200645 return;
646
Daniel Mackda6094e2011-08-14 11:31:16 +0200647 /* find an unused output urb that is unused */
648 for (i = 0; i < N_URBS; i++)
Daniel Mack1c8470c2013-03-03 20:46:21 +0100649 if (test_and_set_bit(i, &cdev->outurb_active_mask) == 0) {
650 out = cdev->data_urbs_out[i];
Daniel Mackda6094e2011-08-14 11:31:16 +0200651 break;
652 }
653
654 if (!out) {
Daniel Mackf1f6b8f2013-03-03 20:46:22 +0100655 dev_err(dev, "Unable to find an output urb to use\n");
Daniel Mackda6094e2011-08-14 11:31:16 +0200656 goto requeue;
657 }
Daniel Mack523f1dc2007-03-26 19:11:24 +0200658
659 /* read the recently received packet and send back one which has
660 * the same layout */
661 for (frame = 0; frame < FRAMES_PER_URB; frame++) {
662 if (urb->iso_frame_desc[frame].status)
663 continue;
664
665 len = urb->iso_frame_desc[outframe].actual_length;
666 out->iso_frame_desc[outframe].length = len;
667 out->iso_frame_desc[outframe].actual_length = 0;
Daniel Mack15439bd2011-08-05 13:49:52 +0200668 out->iso_frame_desc[outframe].offset = offset;
669 offset += len;
Daniel Mack9318dce2009-06-01 21:36:23 +0200670
Daniel Mack523f1dc2007-03-26 19:11:24 +0200671 if (len > 0) {
Daniel Mack1c8470c2013-03-03 20:46:21 +0100672 spin_lock(&cdev->spinlock);
673 fill_out_urb(cdev, out, &out->iso_frame_desc[outframe]);
674 read_in_urb(cdev, urb, &urb->iso_frame_desc[frame]);
675 spin_unlock(&cdev->spinlock);
676 check_for_elapsed_periods(cdev, cdev->sub_playback);
677 check_for_elapsed_periods(cdev, cdev->sub_capture);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200678 send_it = 1;
679 }
680
681 outframe++;
682 }
683
684 if (send_it) {
Daniel Mack15439bd2011-08-05 13:49:52 +0200685 out->number_of_packets = outframe;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200686 out->transfer_flags = URB_ISO_ASAP;
687 usb_submit_urb(out, GFP_ATOMIC);
Daniel Mackda6094e2011-08-14 11:31:16 +0200688 } else {
689 struct snd_usb_caiaq_cb_info *oinfo = out->context;
Daniel Mack1c8470c2013-03-03 20:46:21 +0100690 clear_bit(oinfo->index, &cdev->outurb_active_mask);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200691 }
Daniel Mack9318dce2009-06-01 21:36:23 +0200692
Daniel Mackda6094e2011-08-14 11:31:16 +0200693requeue:
Daniel Mack523f1dc2007-03-26 19:11:24 +0200694 /* re-submit inbound urb */
695 for (frame = 0; frame < FRAMES_PER_URB; frame++) {
696 urb->iso_frame_desc[frame].offset = BYTES_PER_FRAME * frame;
697 urb->iso_frame_desc[frame].length = BYTES_PER_FRAME;
698 urb->iso_frame_desc[frame].actual_length = 0;
699 }
Daniel Mack9318dce2009-06-01 21:36:23 +0200700
Daniel Mack523f1dc2007-03-26 19:11:24 +0200701 urb->number_of_packets = FRAMES_PER_URB;
702 urb->transfer_flags = URB_ISO_ASAP;
703 usb_submit_urb(urb, GFP_ATOMIC);
704}
705
706static void write_completed(struct urb *urb)
707{
708 struct snd_usb_caiaq_cb_info *info = urb->context;
Daniel Mack1c8470c2013-03-03 20:46:21 +0100709 struct snd_usb_caiaqdev *cdev = info->cdev;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200710
Daniel Mack1c8470c2013-03-03 20:46:21 +0100711 if (!cdev->output_running) {
712 cdev->output_running = 1;
713 wake_up(&cdev->prepare_wait_queue);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200714 }
Daniel Mackda6094e2011-08-14 11:31:16 +0200715
Daniel Mack1c8470c2013-03-03 20:46:21 +0100716 clear_bit(info->index, &cdev->outurb_active_mask);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200717}
718
Daniel Mack1c8470c2013-03-03 20:46:21 +0100719static struct urb **alloc_urbs(struct snd_usb_caiaqdev *cdev, int dir, int *ret)
Daniel Mack523f1dc2007-03-26 19:11:24 +0200720{
721 int i, frame;
722 struct urb **urbs;
Daniel Mack1c8470c2013-03-03 20:46:21 +0100723 struct usb_device *usb_dev = cdev->chip.dev;
Daniel Mackf1f6b8f2013-03-03 20:46:22 +0100724 struct device *dev = caiaqdev_to_dev(cdev);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200725 unsigned int pipe;
726
Daniel Mack9318dce2009-06-01 21:36:23 +0200727 pipe = (dir == SNDRV_PCM_STREAM_PLAYBACK) ?
Daniel Mack523f1dc2007-03-26 19:11:24 +0200728 usb_sndisocpipe(usb_dev, ENDPOINT_PLAYBACK) :
729 usb_rcvisocpipe(usb_dev, ENDPOINT_CAPTURE);
730
731 urbs = kmalloc(N_URBS * sizeof(*urbs), GFP_KERNEL);
732 if (!urbs) {
Daniel Mackf1f6b8f2013-03-03 20:46:22 +0100733 dev_err(dev, "unable to kmalloc() urbs, OOM!?\n");
Daniel Mack523f1dc2007-03-26 19:11:24 +0200734 *ret = -ENOMEM;
735 return NULL;
736 }
737
738 for (i = 0; i < N_URBS; i++) {
739 urbs[i] = usb_alloc_urb(FRAMES_PER_URB, GFP_KERNEL);
740 if (!urbs[i]) {
Daniel Mackf1f6b8f2013-03-03 20:46:22 +0100741 dev_err(dev, "unable to usb_alloc_urb(), OOM!?\n");
Daniel Mack523f1dc2007-03-26 19:11:24 +0200742 *ret = -ENOMEM;
743 return urbs;
744 }
745
Daniel Mack9318dce2009-06-01 21:36:23 +0200746 urbs[i]->transfer_buffer =
Daniel Mack523f1dc2007-03-26 19:11:24 +0200747 kmalloc(FRAMES_PER_URB * BYTES_PER_FRAME, GFP_KERNEL);
748 if (!urbs[i]->transfer_buffer) {
Daniel Mackf1f6b8f2013-03-03 20:46:22 +0100749 dev_err(dev, "unable to kmalloc() transfer buffer, OOM!?\n");
Daniel Mack523f1dc2007-03-26 19:11:24 +0200750 *ret = -ENOMEM;
751 return urbs;
752 }
Daniel Mack9318dce2009-06-01 21:36:23 +0200753
Daniel Mack523f1dc2007-03-26 19:11:24 +0200754 for (frame = 0; frame < FRAMES_PER_URB; frame++) {
Daniel Mack9318dce2009-06-01 21:36:23 +0200755 struct usb_iso_packet_descriptor *iso =
Daniel Mack523f1dc2007-03-26 19:11:24 +0200756 &urbs[i]->iso_frame_desc[frame];
Daniel Mack9318dce2009-06-01 21:36:23 +0200757
Daniel Mack523f1dc2007-03-26 19:11:24 +0200758 iso->offset = BYTES_PER_FRAME * frame;
759 iso->length = BYTES_PER_FRAME;
760 }
Daniel Mack9318dce2009-06-01 21:36:23 +0200761
Daniel Mack523f1dc2007-03-26 19:11:24 +0200762 urbs[i]->dev = usb_dev;
763 urbs[i]->pipe = pipe;
Daniel Mack9318dce2009-06-01 21:36:23 +0200764 urbs[i]->transfer_buffer_length = FRAMES_PER_URB
Daniel Mack523f1dc2007-03-26 19:11:24 +0200765 * BYTES_PER_FRAME;
Daniel Mack1c8470c2013-03-03 20:46:21 +0100766 urbs[i]->context = &cdev->data_cb_info[i];
Daniel Mack523f1dc2007-03-26 19:11:24 +0200767 urbs[i]->interval = 1;
768 urbs[i]->transfer_flags = URB_ISO_ASAP;
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
778static 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 Mack9318dce2009-06-01 21:36:23 +0200788
Daniel Mack523f1dc2007-03-26 19:11:24 +0200789 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 Mack1c8470c2013-03-03 20:46:21 +0100797int snd_usb_caiaq_audio_init(struct snd_usb_caiaqdev *cdev)
Daniel Mack523f1dc2007-03-26 19:11:24 +0200798{
799 int i, ret;
Daniel Mackf1f6b8f2013-03-03 20:46:22 +0100800 struct device *dev = caiaqdev_to_dev(cdev);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200801
Daniel Mack1c8470c2013-03-03 20:46:21 +0100802 cdev->n_audio_in = max(cdev->spec.num_analog_audio_in,
803 cdev->spec.num_digital_audio_in) /
Daniel Mack523f1dc2007-03-26 19:11:24 +0200804 CHANNELS_PER_STREAM;
Daniel Mack1c8470c2013-03-03 20:46:21 +0100805 cdev->n_audio_out = max(cdev->spec.num_analog_audio_out,
806 cdev->spec.num_digital_audio_out) /
Daniel Mack523f1dc2007-03-26 19:11:24 +0200807 CHANNELS_PER_STREAM;
Daniel Mack1c8470c2013-03-03 20:46:21 +0100808 cdev->n_streams = max(cdev->n_audio_in, cdev->n_audio_out);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200809
Daniel Mackf1f6b8f2013-03-03 20:46:22 +0100810 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 Mack523f1dc2007-03-26 19:11:24 +0200813
Daniel Mack1c8470c2013-03-03 20:46:21 +0100814 if (cdev->n_streams > MAX_STREAMS) {
Daniel Mackf1f6b8f2013-03-03 20:46:22 +0100815 dev_err(dev, "unable to initialize device, too many streams.\n");
Daniel Mack523f1dc2007-03-26 19:11:24 +0200816 return -EINVAL;
817 }
818
Daniel Mack1c8470c2013-03-03 20:46:21 +0100819 ret = snd_pcm_new(cdev->chip.card, cdev->product_name, 0,
820 cdev->n_audio_out, cdev->n_audio_in, &cdev->pcm);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200821
822 if (ret < 0) {
Daniel Mackf1f6b8f2013-03-03 20:46:22 +0100823 dev_err(dev, "snd_pcm_new() returned %d\n", ret);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200824 return ret;
825 }
826
Daniel Mack1c8470c2013-03-03 20:46:21 +0100827 cdev->pcm->private_data = cdev;
828 strlcpy(cdev->pcm->name, cdev->product_name, sizeof(cdev->pcm->name));
Daniel Mack523f1dc2007-03-26 19:11:24 +0200829
Daniel Mack1c8470c2013-03-03 20:46:21 +0100830 memset(cdev->sub_playback, 0, sizeof(cdev->sub_playback));
831 memset(cdev->sub_capture, 0, sizeof(cdev->sub_capture));
Daniel Mack9318dce2009-06-01 21:36:23 +0200832
Daniel Mack1c8470c2013-03-03 20:46:21 +0100833 memcpy(&cdev->pcm_info, &snd_usb_caiaq_pcm_hardware,
Daniel Mack523f1dc2007-03-26 19:11:24 +0200834 sizeof(snd_usb_caiaq_pcm_hardware));
835
836 /* setup samplerates */
Daniel Mack1c8470c2013-03-03 20:46:21 +0100837 cdev->samplerates = cdev->pcm_info.rates;
838 switch (cdev->chip.usb_id) {
Daniel Mack523f1dc2007-03-26 19:11:24 +0200839 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AK1):
Daniel Mackad1e34b2007-09-17 14:45:14 +0200840 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_RIGKONTROL3):
Daniel Mackf3e9d5d2008-05-08 15:42:15 +0200841 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_SESSIONIO):
Daniel Mack21655922009-01-16 11:03:19 +0100842 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_GUITARRIGMOBILE):
Daniel Mack1c8470c2013-03-03 20:46:21 +0100843 cdev->samplerates |= SNDRV_PCM_RATE_192000;
Daniel Mack21655922009-01-16 11:03:19 +0100844 /* fall thru */
Daniel Mackb30c4942009-07-22 14:13:35 +0200845 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AUDIO2DJ):
Daniel Mack21655922009-01-16 11:03:19 +0100846 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AUDIO4DJ):
Daniel Mack523f1dc2007-03-26 19:11:24 +0200847 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AUDIO8DJ):
Daniel Mackdf8d81a2010-09-01 16:23:46 +0800848 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_TRAKTORAUDIO2):
Daniel Mack1c8470c2013-03-03 20:46:21 +0100849 cdev->samplerates |= SNDRV_PCM_RATE_88200;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200850 break;
851 }
852
Daniel Mack1c8470c2013-03-03 20:46:21 +0100853 snd_pcm_set_ops(cdev->pcm, SNDRV_PCM_STREAM_PLAYBACK,
Daniel Mack523f1dc2007-03-26 19:11:24 +0200854 &snd_usb_caiaq_ops);
Daniel Mack1c8470c2013-03-03 20:46:21 +0100855 snd_pcm_set_ops(cdev->pcm, SNDRV_PCM_STREAM_CAPTURE,
Daniel Mack523f1dc2007-03-26 19:11:24 +0200856 &snd_usb_caiaq_ops);
857
Daniel Mack1c8470c2013-03-03 20:46:21 +0100858 snd_pcm_lib_preallocate_pages_for_all(cdev->pcm,
Daniel Mack523f1dc2007-03-26 19:11:24 +0200859 SNDRV_DMA_TYPE_CONTINUOUS,
860 snd_dma_continuous_data(GFP_KERNEL),
861 MAX_BUFFER_SIZE, MAX_BUFFER_SIZE);
862
Daniel Mack1c8470c2013-03-03 20:46:21 +0100863 cdev->data_cb_info =
Daniel Mack9318dce2009-06-01 21:36:23 +0200864 kmalloc(sizeof(struct snd_usb_caiaq_cb_info) * N_URBS,
Daniel Mack523f1dc2007-03-26 19:11:24 +0200865 GFP_KERNEL);
866
Daniel Mack1c8470c2013-03-03 20:46:21 +0100867 if (!cdev->data_cb_info)
Daniel Mack523f1dc2007-03-26 19:11:24 +0200868 return -ENOMEM;
869
Daniel Mack1c8470c2013-03-03 20:46:21 +0100870 cdev->outurb_active_mask = 0;
871 BUILD_BUG_ON(N_URBS > (sizeof(cdev->outurb_active_mask) * 8));
Daniel Mackda6094e2011-08-14 11:31:16 +0200872
Daniel Mack523f1dc2007-03-26 19:11:24 +0200873 for (i = 0; i < N_URBS; i++) {
Daniel Mack1c8470c2013-03-03 20:46:21 +0100874 cdev->data_cb_info[i].cdev = cdev;
875 cdev->data_cb_info[i].index = i;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200876 }
Daniel Mack9318dce2009-06-01 21:36:23 +0200877
Daniel Mack1c8470c2013-03-03 20:46:21 +0100878 cdev->data_urbs_in = alloc_urbs(cdev, SNDRV_PCM_STREAM_CAPTURE, &ret);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200879 if (ret < 0) {
Daniel Mack1c8470c2013-03-03 20:46:21 +0100880 kfree(cdev->data_cb_info);
881 free_urbs(cdev->data_urbs_in);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200882 return ret;
883 }
Daniel Mack9318dce2009-06-01 21:36:23 +0200884
Daniel Mack1c8470c2013-03-03 20:46:21 +0100885 cdev->data_urbs_out = alloc_urbs(cdev, SNDRV_PCM_STREAM_PLAYBACK, &ret);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200886 if (ret < 0) {
Daniel Mack1c8470c2013-03-03 20:46:21 +0100887 kfree(cdev->data_cb_info);
888 free_urbs(cdev->data_urbs_in);
889 free_urbs(cdev->data_urbs_out);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200890 return ret;
891 }
892
893 return 0;
894}
895
Daniel Mack1c8470c2013-03-03 20:46:21 +0100896void snd_usb_caiaq_audio_free(struct snd_usb_caiaqdev *cdev)
Daniel Mack523f1dc2007-03-26 19:11:24 +0200897{
Daniel Mackf1f6b8f2013-03-03 20:46:22 +0100898 struct device *dev = caiaqdev_to_dev(cdev);
899
900 dev_dbg(dev, "%s(%p)\n", __func__, cdev);
Daniel Mack1c8470c2013-03-03 20:46:21 +0100901 stream_stop(cdev);
902 free_urbs(cdev->data_urbs_in);
903 free_urbs(cdev->data_urbs_out);
904 kfree(cdev->data_cb_info);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200905}
906