blob: 8f66ba730d69d943196ddd7629e777083846b9ec [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{
Antonio Ospitefc76f862013-06-21 13:11:49 +0200186 return snd_pcm_lib_alloc_vmalloc_buffer(sub,
187 params_buffer_bytes(hw_params));
Daniel Mack523f1dc2007-03-26 19:11:24 +0200188}
189
190static int snd_usb_caiaq_pcm_hw_free(struct snd_pcm_substream *sub)
191{
Daniel Mack1c8470c2013-03-03 20:46:21 +0100192 struct snd_usb_caiaqdev *cdev = snd_pcm_substream_chip(sub);
Daniel Mack1c8470c2013-03-03 20:46:21 +0100193 deactivate_substream(cdev, sub);
Antonio Ospitefc76f862013-06-21 13:11:49 +0200194 return snd_pcm_lib_free_vmalloc_buffer(sub);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200195}
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
202static unsigned int rates[] = { 5512, 8000, 11025, 16000, 22050, 32000, 44100,
Daniel Mack15c5ab62010-09-10 17:04:57 +0800203 48000, 64000, 88200, 96000, 176400, 192000 };
Daniel Mack523f1dc2007-03-26 19:11:24 +0200204
205static 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 Mack1c8470c2013-03-03 20:46:21 +0100209 struct snd_usb_caiaqdev *cdev = snd_pcm_substream_chip(substream);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200210 struct snd_pcm_runtime *runtime = substream->runtime;
Daniel Mackf1f6b8f2013-03-03 20:46:22 +0100211 struct device *dev = caiaqdev_to_dev(cdev);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200212
Daniel Mackf1f6b8f2013-03-03 20:46:22 +0100213 dev_dbg(dev, "%s(%p)\n", __func__, substream);
Daniel Mack9318dce2009-06-01 21:36:23 +0200214
Daniel Macka9b487f2009-04-27 12:18:05 +0200215 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Daniel Mack15c5ab62010-09-10 17:04:57 +0800216 int out_pos;
217
Daniel Mack1c8470c2013-03-03 20:46:21 +0100218 switch (cdev->spec.data_alignment) {
Daniel Mack15c5ab62010-09-10 17:04:57 +0800219 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 Mack1c8470c2013-03-03 20:46:21 +0100229 cdev->period_out_count[index] = out_pos;
230 cdev->audio_out_buf_pos[index] = out_pos;
Daniel Macka9b487f2009-04-27 12:18:05 +0200231 } else {
Daniel Mack15c5ab62010-09-10 17:04:57 +0800232 int in_pos;
233
Daniel Mack1c8470c2013-03-03 20:46:21 +0100234 switch (cdev->spec.data_alignment) {
Daniel Mack15c5ab62010-09-10 17:04:57 +0800235 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 Mack1c8470c2013-03-03 20:46:21 +0100247 cdev->period_in_count[index] = in_pos;
248 cdev->audio_in_buf_pos[index] = in_pos;
Daniel Macka9b487f2009-04-27 12:18:05 +0200249 }
250
Daniel Mack1c8470c2013-03-03 20:46:21 +0100251 if (cdev->streaming)
Daniel Mack523f1dc2007-03-26 19:11:24 +0200252 return 0;
Daniel Mack9318dce2009-06-01 21:36:23 +0200253
Daniel Mack523f1dc2007-03-26 19:11:24 +0200254 /* 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 Mack1c8470c2013-03-03 20:46:21 +0100258 cdev->pcm_info.rates = 1 << i;
Daniel Mack9318dce2009-06-01 21:36:23 +0200259
Daniel Mack523f1dc2007-03-26 19:11:24 +0200260 snd_pcm_limit_hw_rates(runtime);
261
262 bytes_per_sample = BYTES_PER_SAMPLE;
Daniel Mack1c8470c2013-03-03 20:46:21 +0100263 if (cdev->spec.data_alignment >= 2)
Daniel Mack523f1dc2007-03-26 19:11:24 +0200264 bytes_per_sample++;
Daniel Mack9318dce2009-06-01 21:36:23 +0200265
Daniel Mack523f1dc2007-03-26 19:11:24 +0200266 bpp = ((runtime->rate / 8000) + CLOCK_DRIFT_TOLERANCE)
Daniel Mack1c8470c2013-03-03 20:46:21 +0100267 * bytes_per_sample * CHANNELS_PER_STREAM * cdev->n_streams;
Daniel Mack6e9fc6b2008-04-14 15:40:31 +0200268
269 if (bpp > MAX_ENDPOINT_SIZE)
270 bpp = MAX_ENDPOINT_SIZE;
271
Daniel Mack1c8470c2013-03-03 20:46:21 +0100272 ret = snd_usb_caiaq_set_audio_params(cdev, runtime->rate,
Daniel Mack523f1dc2007-03-26 19:11:24 +0200273 runtime->sample_bits, bpp);
274 if (ret)
275 return ret;
276
Daniel Mack1c8470c2013-03-03 20:46:21 +0100277 ret = stream_start(cdev);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200278 if (ret)
279 return ret;
Daniel Mack9318dce2009-06-01 21:36:23 +0200280
Daniel Mack1c8470c2013-03-03 20:46:21 +0100281 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 Mack523f1dc2007-03-26 19:11:24 +0200285 return -EPIPE;
286 }
287
288 return 0;
289}
290
291static int snd_usb_caiaq_pcm_trigger(struct snd_pcm_substream *sub, int cmd)
292{
Daniel Mack1c8470c2013-03-03 20:46:21 +0100293 struct snd_usb_caiaqdev *cdev = snd_pcm_substream_chip(sub);
Daniel Mackf1f6b8f2013-03-03 20:46:22 +0100294 struct device *dev = caiaqdev_to_dev(cdev);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200295
Daniel Mackf1f6b8f2013-03-03 20:46:22 +0100296 dev_dbg(dev, "%s(%p) cmd %d\n", __func__, sub, cmd);
Daniel Mack15c5ab62010-09-10 17:04:57 +0800297
Daniel Mack523f1dc2007-03-26 19:11:24 +0200298 switch (cmd) {
299 case SNDRV_PCM_TRIGGER_START:
300 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Daniel Mack1c8470c2013-03-03 20:46:21 +0100301 activate_substream(cdev, sub);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200302 break;
303 case SNDRV_PCM_TRIGGER_STOP:
304 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
Daniel Mack1c8470c2013-03-03 20:46:21 +0100305 deactivate_substream(cdev, sub);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200306 break;
307 default:
308 return -EINVAL;
309 }
310
311 return 0;
312}
313
314static snd_pcm_uframes_t
315snd_usb_caiaq_pcm_pointer(struct snd_pcm_substream *sub)
316{
317 int index = sub->number;
Daniel Mack1c8470c2013-03-03 20:46:21 +0100318 struct snd_usb_caiaqdev *cdev = snd_pcm_substream_chip(sub);
Mark Hills3702b082009-10-24 12:59:35 +0100319 snd_pcm_uframes_t ptr;
320
Daniel Mack1c8470c2013-03-03 20:46:21 +0100321 spin_lock(&cdev->spinlock);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200322
Daniel Mack1c8470c2013-03-03 20:46:21 +0100323 if (cdev->input_panic || cdev->output_panic) {
Mark Hills3702b082009-10-24 12:59:35 +0100324 ptr = SNDRV_PCM_POS_XRUN;
Mark Hillscb74eb12012-02-21 21:26:31 +0000325 goto unlock;
326 }
Daniel Mack523f1dc2007-03-26 19:11:24 +0200327
328 if (sub->stream == SNDRV_PCM_STREAM_PLAYBACK)
Mark Hills3702b082009-10-24 12:59:35 +0100329 ptr = bytes_to_frames(sub->runtime,
Daniel Mack1c8470c2013-03-03 20:46:21 +0100330 cdev->audio_out_buf_pos[index]);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200331 else
Mark Hills3702b082009-10-24 12:59:35 +0100332 ptr = bytes_to_frames(sub->runtime,
Daniel Mack1c8470c2013-03-03 20:46:21 +0100333 cdev->audio_in_buf_pos[index]);
Mark Hills3702b082009-10-24 12:59:35 +0100334
Mark Hillscb74eb12012-02-21 21:26:31 +0000335unlock:
Daniel Mack1c8470c2013-03-03 20:46:21 +0100336 spin_unlock(&cdev->spinlock);
Mark Hills3702b082009-10-24 12:59:35 +0100337 return ptr;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200338}
339
340/* operators for both playback and capture */
341static 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 Ospitefc76f862013-06-21 13:11:49 +0200349 .pointer = snd_usb_caiaq_pcm_pointer,
350 .page = snd_pcm_lib_get_vmalloc_page,
351 .mmap = snd_pcm_lib_mmap_vmalloc,
Daniel Mack523f1dc2007-03-26 19:11:24 +0200352};
Daniel Mack9318dce2009-06-01 21:36:23 +0200353
Daniel Mack1c8470c2013-03-03 20:46:21 +0100354static void check_for_elapsed_periods(struct snd_usb_caiaqdev *cdev,
Daniel Mack523f1dc2007-03-26 19:11:24 +0200355 struct snd_pcm_substream **subs)
356{
357 int stream, pb, *cnt;
358 struct snd_pcm_substream *sub;
359
Daniel Mack1c8470c2013-03-03 20:46:21 +0100360 for (stream = 0; stream < cdev->n_streams; stream++) {
Daniel Mack523f1dc2007-03-26 19:11:24 +0200361 sub = subs[stream];
362 if (!sub)
363 continue;
364
Daniel Macka9b487f2009-04-27 12:18:05 +0200365 pb = snd_pcm_lib_period_bytes(sub);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200366 cnt = (sub->stream == SNDRV_PCM_STREAM_PLAYBACK) ?
Daniel Mack1c8470c2013-03-03 20:46:21 +0100367 &cdev->period_out_count[stream] :
368 &cdev->period_in_count[stream];
Daniel Mack523f1dc2007-03-26 19:11:24 +0200369
370 if (*cnt >= pb) {
371 snd_pcm_period_elapsed(sub);
372 *cnt %= pb;
373 }
374 }
375}
376
Daniel Mack1c8470c2013-03-03 20:46:21 +0100377static void read_in_urb_mode0(struct snd_usb_caiaqdev *cdev,
Daniel Mack523f1dc2007-03-26 19:11:24 +0200378 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 Mack1c8470c2013-03-03 20:46:21 +0100385 if (all_substreams_zero(cdev->sub_capture))
Daniel Mack523f1dc2007-03-26 19:11:24 +0200386 return;
387
Daniel Mack523f1dc2007-03-26 19:11:24 +0200388 for (i = 0; i < iso->actual_length;) {
Daniel Mack1c8470c2013-03-03 20:46:21 +0100389 for (stream = 0; stream < cdev->n_streams; stream++, i++) {
390 sub = cdev->sub_capture[stream];
Daniel Mack523f1dc2007-03-26 19:11:24 +0200391 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 Mack1c8470c2013-03-03 20:46:21 +0100395 audio_buf[cdev->audio_in_buf_pos[stream]++]
Daniel Mack523f1dc2007-03-26 19:11:24 +0200396 = usb_buf[i];
Daniel Mack1c8470c2013-03-03 20:46:21 +0100397 cdev->period_in_count[stream]++;
398 if (cdev->audio_in_buf_pos[stream] == sz)
399 cdev->audio_in_buf_pos[stream] = 0;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200400 }
401 }
402 }
Daniel Mack523f1dc2007-03-26 19:11:24 +0200403}
404
Daniel Mack1c8470c2013-03-03 20:46:21 +0100405static void read_in_urb_mode2(struct snd_usb_caiaqdev *cdev,
Daniel Mack523f1dc2007-03-26 19:11:24 +0200406 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 Mack523f1dc2007-03-26 19:11:24 +0200414 for (i = 0; i < iso->actual_length;) {
Daniel Mack1c8470c2013-03-03 20:46:21 +0100415 if (i % (cdev->n_streams * BYTES_PER_SAMPLE_USB) == 0) {
Daniel Mack9318dce2009-06-01 21:36:23 +0200416 for (stream = 0;
Daniel Mack1c8470c2013-03-03 20:46:21 +0100417 stream < cdev->n_streams;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200418 stream++, i++) {
Daniel Mack1c8470c2013-03-03 20:46:21 +0100419 if (cdev->first_packet)
Daniel Mack523f1dc2007-03-26 19:11:24 +0200420 continue;
421
Daniel Mack1c8470c2013-03-03 20:46:21 +0100422 check_byte = MAKE_CHECKBYTE(cdev, stream, i);
Daniel Mack9318dce2009-06-01 21:36:23 +0200423
Daniel Mack523f1dc2007-03-26 19:11:24 +0200424 if ((usb_buf[i] & 0x3f) != check_byte)
Daniel Mack1c8470c2013-03-03 20:46:21 +0100425 cdev->input_panic = 1;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200426
427 if (usb_buf[i] & 0x80)
Daniel Mack1c8470c2013-03-03 20:46:21 +0100428 cdev->output_panic = 1;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200429 }
430 }
Daniel Mack1c8470c2013-03-03 20:46:21 +0100431 cdev->first_packet = 0;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200432
Daniel Mack1c8470c2013-03-03 20:46:21 +0100433 for (stream = 0; stream < cdev->n_streams; stream++, i++) {
434 sub = cdev->sub_capture[stream];
435 if (cdev->input_panic)
Daniel Mack9311c9b2009-03-18 11:03:54 +0100436 usb_buf[i] = 0;
437
Daniel Mack523f1dc2007-03-26 19:11:24 +0200438 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 Mack1c8470c2013-03-03 20:46:21 +0100442 audio_buf[cdev->audio_in_buf_pos[stream]++] =
Karsten Wiesea971c3d42007-03-29 17:02:45 +0200443 usb_buf[i];
Daniel Mack1c8470c2013-03-03 20:46:21 +0100444 cdev->period_in_count[stream]++;
445 if (cdev->audio_in_buf_pos[stream] == sz)
446 cdev->audio_in_buf_pos[stream] = 0;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200447 }
448 }
449 }
Daniel Mack523f1dc2007-03-26 19:11:24 +0200450}
451
Daniel Mack1c8470c2013-03-03 20:46:21 +0100452static void read_in_urb_mode3(struct snd_usb_caiaqdev *cdev,
Daniel Mack15c5ab62010-09-10 17:04:57 +0800453 const struct urb *urb,
454 const struct usb_iso_packet_descriptor *iso)
455{
456 unsigned char *usb_buf = urb->transfer_buffer + iso->offset;
Daniel Mackf1f6b8f2013-03-03 20:46:22 +0100457 struct device *dev = caiaqdev_to_dev(cdev);
Daniel Mack15c5ab62010-09-10 17:04:57 +0800458 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 Mack1c8470c2013-03-03 20:46:21 +0100465 for (stream = 0; stream < cdev->n_streams; stream++) {
466 struct snd_pcm_substream *sub = cdev->sub_capture[stream];
Daniel Mack15c5ab62010-09-10 17:04:57 +0800467 char *audio_buf = NULL;
468 int c, n, sz = 0;
469
Daniel Mack1c8470c2013-03-03 20:46:21 +0100470 if (sub && !cdev->input_panic) {
Daniel Mack15c5ab62010-09-10 17:04:57 +0800471 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 Mack1c8470c2013-03-03 20:46:21 +0100480 audio_buf[cdev->audio_in_buf_pos[stream]++] = usb_buf[i+n];
Daniel Mack15c5ab62010-09-10 17:04:57 +0800481
Daniel Mack1c8470c2013-03-03 20:46:21 +0100482 if (cdev->audio_in_buf_pos[stream] == sz)
483 cdev->audio_in_buf_pos[stream] = 0;
Daniel Mack15c5ab62010-09-10 17:04:57 +0800484 }
485
Daniel Mack1c8470c2013-03-03 20:46:21 +0100486 cdev->period_in_count[stream] += BYTES_PER_SAMPLE;
Daniel Mack15c5ab62010-09-10 17:04:57 +0800487 }
488
489 i += BYTES_PER_SAMPLE;
490
491 if (usb_buf[i] != ((stream << 1) | c) &&
Daniel Mack1c8470c2013-03-03 20:46:21 +0100492 !cdev->first_packet) {
493 if (!cdev->input_panic)
Daniel Mackf1f6b8f2013-03-03 20:46:22 +0100494 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 Mack1c8470c2013-03-03 20:46:21 +0100496 cdev->input_panic = 1;
Daniel Mack15c5ab62010-09-10 17:04:57 +0800497 }
498
499 i++;
500 }
501 }
502 }
503
Daniel Mack1c8470c2013-03-03 20:46:21 +0100504 if (cdev->first_packet > 0)
505 cdev->first_packet--;
Daniel Mack15c5ab62010-09-10 17:04:57 +0800506}
507
Daniel Mack1c8470c2013-03-03 20:46:21 +0100508static void read_in_urb(struct snd_usb_caiaqdev *cdev,
Daniel Mack523f1dc2007-03-26 19:11:24 +0200509 const struct urb *urb,
510 const struct usb_iso_packet_descriptor *iso)
511{
Daniel Mackf1f6b8f2013-03-03 20:46:22 +0100512 struct device *dev = caiaqdev_to_dev(cdev);
513
Daniel Mack1c8470c2013-03-03 20:46:21 +0100514 if (!cdev->streaming)
Daniel Mack523f1dc2007-03-26 19:11:24 +0200515 return;
516
Daniel Mack1c8470c2013-03-03 20:46:21 +0100517 if (iso->actual_length < cdev->bpp)
Daniel Mack9311c9b2009-03-18 11:03:54 +0100518 return;
519
Daniel Mack1c8470c2013-03-03 20:46:21 +0100520 switch (cdev->spec.data_alignment) {
Daniel Mack523f1dc2007-03-26 19:11:24 +0200521 case 0:
Daniel Mack1c8470c2013-03-03 20:46:21 +0100522 read_in_urb_mode0(cdev, urb, iso);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200523 break;
524 case 2:
Daniel Mack1c8470c2013-03-03 20:46:21 +0100525 read_in_urb_mode2(cdev, urb, iso);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200526 break;
Daniel Mack15c5ab62010-09-10 17:04:57 +0800527 case 3:
Daniel Mack1c8470c2013-03-03 20:46:21 +0100528 read_in_urb_mode3(cdev, urb, iso);
Daniel Mack15c5ab62010-09-10 17:04:57 +0800529 break;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200530 }
531
Daniel Mack1c8470c2013-03-03 20:46:21 +0100532 if ((cdev->input_panic || cdev->output_panic) && !cdev->warned) {
Daniel Mackf1f6b8f2013-03-03 20:46:22 +0100533 dev_warn(dev, "streaming error detected %s %s\n",
Daniel Mack1c8470c2013-03-03 20:46:21 +0100534 cdev->input_panic ? "(input)" : "",
535 cdev->output_panic ? "(output)" : "");
536 cdev->warned = 1;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200537 }
Daniel Mack523f1dc2007-03-26 19:11:24 +0200538}
539
Daniel Mack1c8470c2013-03-03 20:46:21 +0100540static void fill_out_urb_mode_0(struct snd_usb_caiaqdev *cdev,
Daniel Mack15c5ab62010-09-10 17:04:57 +0800541 struct urb *urb,
542 const struct usb_iso_packet_descriptor *iso)
Daniel Mack523f1dc2007-03-26 19:11:24 +0200543{
544 unsigned char *usb_buf = urb->transfer_buffer + iso->offset;
545 struct snd_pcm_substream *sub;
546 int stream, i;
Daniel Mack9318dce2009-06-01 21:36:23 +0200547
Daniel Mack523f1dc2007-03-26 19:11:24 +0200548 for (i = 0; i < iso->length;) {
Daniel Mack1c8470c2013-03-03 20:46:21 +0100549 for (stream = 0; stream < cdev->n_streams; stream++, i++) {
550 sub = cdev->sub_playback[stream];
Daniel Mack523f1dc2007-03-26 19:11:24 +0200551 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 Wiesea971c3d42007-03-29 17:02:45 +0200555 usb_buf[i] =
Daniel Mack1c8470c2013-03-03 20:46:21 +0100556 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 Mack523f1dc2007-03-26 19:11:24 +0200561 } else
Karsten Wiesea971c3d42007-03-29 17:02:45 +0200562 usb_buf[i] = 0;
563 }
Daniel Mack523f1dc2007-03-26 19:11:24 +0200564
565 /* fill in the check bytes */
Daniel Mack1c8470c2013-03-03 20:46:21 +0100566 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 Mack15c5ab62010-09-10 17:04:57 +0800571 }
572}
573
Daniel Mack1c8470c2013-03-03 20:46:21 +0100574static void fill_out_urb_mode_3(struct snd_usb_caiaqdev *cdev,
Daniel Mack15c5ab62010-09-10 17:04:57 +0800575 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 Mack1c8470c2013-03-03 20:46:21 +0100582 for (stream = 0; stream < cdev->n_streams; stream++) {
583 struct snd_pcm_substream *sub = cdev->sub_playback[stream];
Daniel Mack15c5ab62010-09-10 17:04:57 +0800584 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 Mack1c8470c2013-03-03 20:46:21 +0100596 usb_buf[i+n] = audio_buf[cdev->audio_out_buf_pos[stream]++];
Daniel Mack15c5ab62010-09-10 17:04:57 +0800597
Daniel Mack1c8470c2013-03-03 20:46:21 +0100598 if (cdev->audio_out_buf_pos[stream] == sz)
599 cdev->audio_out_buf_pos[stream] = 0;
Daniel Mack15c5ab62010-09-10 17:04:57 +0800600 } else {
601 usb_buf[i+n] = 0;
602 }
603 }
604
605 if (audio_buf)
Daniel Mack1c8470c2013-03-03 20:46:21 +0100606 cdev->period_out_count[stream] += BYTES_PER_SAMPLE;
Daniel Mack15c5ab62010-09-10 17:04:57 +0800607
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 Mack1c8470c2013-03-03 20:46:21 +0100617static inline void fill_out_urb(struct snd_usb_caiaqdev *cdev,
Daniel Mack15c5ab62010-09-10 17:04:57 +0800618 struct urb *urb,
619 const struct usb_iso_packet_descriptor *iso)
620{
Daniel Mack1c8470c2013-03-03 20:46:21 +0100621 switch (cdev->spec.data_alignment) {
Daniel Mack15c5ab62010-09-10 17:04:57 +0800622 case 0:
623 case 2:
Daniel Mack1c8470c2013-03-03 20:46:21 +0100624 fill_out_urb_mode_0(cdev, urb, iso);
Daniel Mack15c5ab62010-09-10 17:04:57 +0800625 break;
626 case 3:
Daniel Mack1c8470c2013-03-03 20:46:21 +0100627 fill_out_urb_mode_3(cdev, urb, iso);
Daniel Mack15c5ab62010-09-10 17:04:57 +0800628 break;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200629 }
Daniel Mack523f1dc2007-03-26 19:11:24 +0200630}
631
632static void read_completed(struct urb *urb)
633{
Daniel Mack9318dce2009-06-01 21:36:23 +0200634 struct snd_usb_caiaq_cb_info *info = urb->context;
Daniel Mack1c8470c2013-03-03 20:46:21 +0100635 struct snd_usb_caiaqdev *cdev;
Daniel Mackf1f6b8f2013-03-03 20:46:22 +0100636 struct device *dev;
Daniel Mackda6094e2011-08-14 11:31:16 +0200637 struct urb *out = NULL;
638 int i, frame, len, send_it = 0, outframe = 0;
Daniel Mack15439bd2011-08-05 13:49:52 +0200639 size_t offset = 0;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200640
641 if (urb->status || !info)
642 return;
643
Daniel Mack1c8470c2013-03-03 20:46:21 +0100644 cdev = info->cdev;
Daniel Mackf1f6b8f2013-03-03 20:46:22 +0100645 dev = caiaqdev_to_dev(cdev);
Daniel Mack8d048842008-04-14 15:39:14 +0200646
Daniel Mack1c8470c2013-03-03 20:46:21 +0100647 if (!cdev->streaming)
Daniel Mack523f1dc2007-03-26 19:11:24 +0200648 return;
649
Daniel Mackda6094e2011-08-14 11:31:16 +0200650 /* find an unused output urb that is unused */
651 for (i = 0; i < N_URBS; i++)
Daniel Mack1c8470c2013-03-03 20:46:21 +0100652 if (test_and_set_bit(i, &cdev->outurb_active_mask) == 0) {
653 out = cdev->data_urbs_out[i];
Daniel Mackda6094e2011-08-14 11:31:16 +0200654 break;
655 }
656
657 if (!out) {
Daniel Mackf1f6b8f2013-03-03 20:46:22 +0100658 dev_err(dev, "Unable to find an output urb to use\n");
Daniel Mackda6094e2011-08-14 11:31:16 +0200659 goto requeue;
660 }
Daniel Mack523f1dc2007-03-26 19:11:24 +0200661
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 Mack15439bd2011-08-05 13:49:52 +0200671 out->iso_frame_desc[outframe].offset = offset;
672 offset += len;
Daniel Mack9318dce2009-06-01 21:36:23 +0200673
Daniel Mack523f1dc2007-03-26 19:11:24 +0200674 if (len > 0) {
Daniel Mack1c8470c2013-03-03 20:46:21 +0100675 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 Mack523f1dc2007-03-26 19:11:24 +0200681 send_it = 1;
682 }
683
684 outframe++;
685 }
686
687 if (send_it) {
Daniel Mack15439bd2011-08-05 13:49:52 +0200688 out->number_of_packets = outframe;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200689 usb_submit_urb(out, GFP_ATOMIC);
Daniel Mackda6094e2011-08-14 11:31:16 +0200690 } else {
691 struct snd_usb_caiaq_cb_info *oinfo = out->context;
Daniel Mack1c8470c2013-03-03 20:46:21 +0100692 clear_bit(oinfo->index, &cdev->outurb_active_mask);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200693 }
Daniel Mack9318dce2009-06-01 21:36:23 +0200694
Daniel Mackda6094e2011-08-14 11:31:16 +0200695requeue:
Daniel Mack523f1dc2007-03-26 19:11:24 +0200696 /* 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 Mack9318dce2009-06-01 21:36:23 +0200702
Daniel Mack523f1dc2007-03-26 19:11:24 +0200703 urb->number_of_packets = FRAMES_PER_URB;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200704 usb_submit_urb(urb, GFP_ATOMIC);
705}
706
707static void write_completed(struct urb *urb)
708{
709 struct snd_usb_caiaq_cb_info *info = urb->context;
Daniel Mack1c8470c2013-03-03 20:46:21 +0100710 struct snd_usb_caiaqdev *cdev = info->cdev;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200711
Daniel Mack1c8470c2013-03-03 20:46:21 +0100712 if (!cdev->output_running) {
713 cdev->output_running = 1;
714 wake_up(&cdev->prepare_wait_queue);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200715 }
Daniel Mackda6094e2011-08-14 11:31:16 +0200716
Daniel Mack1c8470c2013-03-03 20:46:21 +0100717 clear_bit(info->index, &cdev->outurb_active_mask);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200718}
719
Daniel Mack1c8470c2013-03-03 20:46:21 +0100720static struct urb **alloc_urbs(struct snd_usb_caiaqdev *cdev, int dir, int *ret)
Daniel Mack523f1dc2007-03-26 19:11:24 +0200721{
722 int i, frame;
723 struct urb **urbs;
Daniel Mack1c8470c2013-03-03 20:46:21 +0100724 struct usb_device *usb_dev = cdev->chip.dev;
Daniel Mackf1f6b8f2013-03-03 20:46:22 +0100725 struct device *dev = caiaqdev_to_dev(cdev);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200726 unsigned int pipe;
727
Daniel Mack9318dce2009-06-01 21:36:23 +0200728 pipe = (dir == SNDRV_PCM_STREAM_PLAYBACK) ?
Daniel Mack523f1dc2007-03-26 19:11:24 +0200729 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 Mackf1f6b8f2013-03-03 20:46:22 +0100734 dev_err(dev, "unable to kmalloc() urbs, OOM!?\n");
Daniel Mack523f1dc2007-03-26 19:11:24 +0200735 *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 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;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200768 urbs[i]->number_of_packets = FRAMES_PER_URB;
769 urbs[i]->complete = (dir == SNDRV_PCM_STREAM_CAPTURE) ?
770 read_completed : write_completed;
771 }
772
773 *ret = 0;
774 return urbs;
775}
776
777static void free_urbs(struct urb **urbs)
778{
779 int i;
780
781 if (!urbs)
782 return;
783
784 for (i = 0; i < N_URBS; i++) {
785 if (!urbs[i])
786 continue;
Daniel Mack9318dce2009-06-01 21:36:23 +0200787
Daniel Mack523f1dc2007-03-26 19:11:24 +0200788 usb_kill_urb(urbs[i]);
789 kfree(urbs[i]->transfer_buffer);
790 usb_free_urb(urbs[i]);
791 }
792
793 kfree(urbs);
794}
795
Daniel Mack1c8470c2013-03-03 20:46:21 +0100796int snd_usb_caiaq_audio_init(struct snd_usb_caiaqdev *cdev)
Daniel Mack523f1dc2007-03-26 19:11:24 +0200797{
798 int i, ret;
Daniel Mackf1f6b8f2013-03-03 20:46:22 +0100799 struct device *dev = caiaqdev_to_dev(cdev);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200800
Daniel Mack1c8470c2013-03-03 20:46:21 +0100801 cdev->n_audio_in = max(cdev->spec.num_analog_audio_in,
802 cdev->spec.num_digital_audio_in) /
Daniel Mack523f1dc2007-03-26 19:11:24 +0200803 CHANNELS_PER_STREAM;
Daniel Mack1c8470c2013-03-03 20:46:21 +0100804 cdev->n_audio_out = max(cdev->spec.num_analog_audio_out,
805 cdev->spec.num_digital_audio_out) /
Daniel Mack523f1dc2007-03-26 19:11:24 +0200806 CHANNELS_PER_STREAM;
Daniel Mack1c8470c2013-03-03 20:46:21 +0100807 cdev->n_streams = max(cdev->n_audio_in, cdev->n_audio_out);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200808
Daniel Mackf1f6b8f2013-03-03 20:46:22 +0100809 dev_dbg(dev, "cdev->n_audio_in = %d\n", cdev->n_audio_in);
810 dev_dbg(dev, "cdev->n_audio_out = %d\n", cdev->n_audio_out);
811 dev_dbg(dev, "cdev->n_streams = %d\n", cdev->n_streams);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200812
Daniel Mack1c8470c2013-03-03 20:46:21 +0100813 if (cdev->n_streams > MAX_STREAMS) {
Daniel Mackf1f6b8f2013-03-03 20:46:22 +0100814 dev_err(dev, "unable to initialize device, too many streams.\n");
Daniel Mack523f1dc2007-03-26 19:11:24 +0200815 return -EINVAL;
816 }
817
Daniel Mack49cdd5b2015-01-04 19:59:29 +0100818 if (cdev->n_streams < 1) {
Daniel Mack897c3292014-10-07 14:25:13 +0200819 dev_err(dev, "bogus number of streams: %d\n", cdev->n_streams);
820 return -EINVAL;
821 }
822
Daniel Mack1c8470c2013-03-03 20:46:21 +0100823 ret = snd_pcm_new(cdev->chip.card, cdev->product_name, 0,
824 cdev->n_audio_out, cdev->n_audio_in, &cdev->pcm);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200825
826 if (ret < 0) {
Daniel Mackf1f6b8f2013-03-03 20:46:22 +0100827 dev_err(dev, "snd_pcm_new() returned %d\n", ret);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200828 return ret;
829 }
830
Daniel Mack1c8470c2013-03-03 20:46:21 +0100831 cdev->pcm->private_data = cdev;
832 strlcpy(cdev->pcm->name, cdev->product_name, sizeof(cdev->pcm->name));
Daniel Mack523f1dc2007-03-26 19:11:24 +0200833
Daniel Mack1c8470c2013-03-03 20:46:21 +0100834 memset(cdev->sub_playback, 0, sizeof(cdev->sub_playback));
835 memset(cdev->sub_capture, 0, sizeof(cdev->sub_capture));
Daniel Mack9318dce2009-06-01 21:36:23 +0200836
Daniel Mack1c8470c2013-03-03 20:46:21 +0100837 memcpy(&cdev->pcm_info, &snd_usb_caiaq_pcm_hardware,
Daniel Mack523f1dc2007-03-26 19:11:24 +0200838 sizeof(snd_usb_caiaq_pcm_hardware));
839
840 /* setup samplerates */
Daniel Mack1c8470c2013-03-03 20:46:21 +0100841 cdev->samplerates = cdev->pcm_info.rates;
842 switch (cdev->chip.usb_id) {
Daniel Mack523f1dc2007-03-26 19:11:24 +0200843 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AK1):
Daniel Mackad1e34b2007-09-17 14:45:14 +0200844 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_RIGKONTROL3):
Daniel Mackf3e9d5d2008-05-08 15:42:15 +0200845 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_SESSIONIO):
Daniel Mack21655922009-01-16 11:03:19 +0100846 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_GUITARRIGMOBILE):
Daniel Mack1c8470c2013-03-03 20:46:21 +0100847 cdev->samplerates |= SNDRV_PCM_RATE_192000;
Daniel Mack21655922009-01-16 11:03:19 +0100848 /* fall thru */
Daniel Mackb30c4942009-07-22 14:13:35 +0200849 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AUDIO2DJ):
Daniel Mack21655922009-01-16 11:03:19 +0100850 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AUDIO4DJ):
Daniel Mack523f1dc2007-03-26 19:11:24 +0200851 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AUDIO8DJ):
Daniel Mackdf8d81a2010-09-01 16:23:46 +0800852 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_TRAKTORAUDIO2):
Daniel Mack1c8470c2013-03-03 20:46:21 +0100853 cdev->samplerates |= SNDRV_PCM_RATE_88200;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200854 break;
855 }
856
Daniel Mack1c8470c2013-03-03 20:46:21 +0100857 snd_pcm_set_ops(cdev->pcm, SNDRV_PCM_STREAM_PLAYBACK,
Daniel Mack523f1dc2007-03-26 19:11:24 +0200858 &snd_usb_caiaq_ops);
Daniel Mack1c8470c2013-03-03 20:46:21 +0100859 snd_pcm_set_ops(cdev->pcm, SNDRV_PCM_STREAM_CAPTURE,
Daniel Mack523f1dc2007-03-26 19:11:24 +0200860 &snd_usb_caiaq_ops);
861
Daniel Mack1c8470c2013-03-03 20:46:21 +0100862 cdev->data_cb_info =
Daniel Mack9318dce2009-06-01 21:36:23 +0200863 kmalloc(sizeof(struct snd_usb_caiaq_cb_info) * N_URBS,
Daniel Mack523f1dc2007-03-26 19:11:24 +0200864 GFP_KERNEL);
865
Daniel Mack1c8470c2013-03-03 20:46:21 +0100866 if (!cdev->data_cb_info)
Daniel Mack523f1dc2007-03-26 19:11:24 +0200867 return -ENOMEM;
868
Daniel Mack1c8470c2013-03-03 20:46:21 +0100869 cdev->outurb_active_mask = 0;
870 BUILD_BUG_ON(N_URBS > (sizeof(cdev->outurb_active_mask) * 8));
Daniel Mackda6094e2011-08-14 11:31:16 +0200871
Daniel Mack523f1dc2007-03-26 19:11:24 +0200872 for (i = 0; i < N_URBS; i++) {
Daniel Mack1c8470c2013-03-03 20:46:21 +0100873 cdev->data_cb_info[i].cdev = cdev;
874 cdev->data_cb_info[i].index = i;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200875 }
Daniel Mack9318dce2009-06-01 21:36:23 +0200876
Daniel Mack1c8470c2013-03-03 20:46:21 +0100877 cdev->data_urbs_in = alloc_urbs(cdev, SNDRV_PCM_STREAM_CAPTURE, &ret);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200878 if (ret < 0) {
Daniel Mack1c8470c2013-03-03 20:46:21 +0100879 kfree(cdev->data_cb_info);
880 free_urbs(cdev->data_urbs_in);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200881 return ret;
882 }
Daniel Mack9318dce2009-06-01 21:36:23 +0200883
Daniel Mack1c8470c2013-03-03 20:46:21 +0100884 cdev->data_urbs_out = alloc_urbs(cdev, SNDRV_PCM_STREAM_PLAYBACK, &ret);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200885 if (ret < 0) {
Daniel Mack1c8470c2013-03-03 20:46:21 +0100886 kfree(cdev->data_cb_info);
887 free_urbs(cdev->data_urbs_in);
888 free_urbs(cdev->data_urbs_out);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200889 return ret;
890 }
891
892 return 0;
893}
894
Daniel Mack1c8470c2013-03-03 20:46:21 +0100895void snd_usb_caiaq_audio_free(struct snd_usb_caiaqdev *cdev)
Daniel Mack523f1dc2007-03-26 19:11:24 +0200896{
Daniel Mackf1f6b8f2013-03-03 20:46:22 +0100897 struct device *dev = caiaqdev_to_dev(cdev);
898
899 dev_dbg(dev, "%s(%p)\n", __func__, cdev);
Daniel Mack1c8470c2013-03-03 20:46:21 +0100900 stream_stop(cdev);
901 free_urbs(cdev->data_urbs_in);
902 free_urbs(cdev->data_urbs_out);
903 kfree(cdev->data_cb_info);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200904}
905