blob: 75d8ba9143f6fe3bdbd5fb644873f79e5ecf0dde [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 Mack523f1dc2007-03-26 19:11:24 +020019#include <linux/spinlock.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Daniel Macke431cf42009-03-28 21:19:49 +010021#include <linux/init.h>
22#include <linux/usb.h>
Daniel Mack523f1dc2007-03-26 19:11:24 +020023#include <sound/core.h>
Daniel Mack523f1dc2007-03-26 19:11:24 +020024#include <sound/pcm.h>
Daniel Mack523f1dc2007-03-26 19:11:24 +020025
Daniel Mack936e7d02009-04-01 19:05:39 +020026#include "device.h"
27#include "audio.h"
Daniel Mack523f1dc2007-03-26 19:11:24 +020028
29#define N_URBS 32
30#define CLOCK_DRIFT_TOLERANCE 5
31#define FRAMES_PER_URB 8
32#define BYTES_PER_FRAME 512
33#define CHANNELS_PER_STREAM 2
34#define BYTES_PER_SAMPLE 3
35#define BYTES_PER_SAMPLE_USB 4
36#define MAX_BUFFER_SIZE (128*1024)
Daniel Mack6e9fc6b2008-04-14 15:40:31 +020037#define MAX_ENDPOINT_SIZE 512
38
Daniel Mack523f1dc2007-03-26 19:11:24 +020039#define ENDPOINT_CAPTURE 2
40#define ENDPOINT_PLAYBACK 6
41
Daniel Mack1c8470c2013-03-03 20:46:21 +010042#define MAKE_CHECKBYTE(cdev,stream,i) \
43 (stream << 1) | (~(i / (cdev->n_streams * BYTES_PER_SAMPLE_USB)) & 1)
Daniel Mack523f1dc2007-03-26 19:11:24 +020044
45static struct snd_pcm_hardware snd_usb_caiaq_pcm_hardware = {
Daniel Mack9318dce2009-06-01 21:36:23 +020046 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
Daniel Mack523f1dc2007-03-26 19:11:24 +020047 SNDRV_PCM_INFO_BLOCK_TRANSFER),
48 .formats = SNDRV_PCM_FMTBIT_S24_3BE,
Daniel Mack9318dce2009-06-01 21:36:23 +020049 .rates = (SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |
Daniel Mack523f1dc2007-03-26 19:11:24 +020050 SNDRV_PCM_RATE_96000),
51 .rate_min = 44100,
52 .rate_max = 0, /* will overwrite later */
53 .channels_min = CHANNELS_PER_STREAM,
54 .channels_max = CHANNELS_PER_STREAM,
55 .buffer_bytes_max = MAX_BUFFER_SIZE,
Daniel Mack09189ac2008-01-24 18:46:42 +010056 .period_bytes_min = 128,
Daniel Mack523f1dc2007-03-26 19:11:24 +020057 .period_bytes_max = MAX_BUFFER_SIZE,
58 .periods_min = 1,
59 .periods_max = 1024,
60};
61
62static void
Daniel Mack1c8470c2013-03-03 20:46:21 +010063activate_substream(struct snd_usb_caiaqdev *cdev,
Daniel Mack523f1dc2007-03-26 19:11:24 +020064 struct snd_pcm_substream *sub)
65{
Daniel Mack1c8470c2013-03-03 20:46:21 +010066 spin_lock(&cdev->spinlock);
Mark Hillsac9dd9d2009-10-24 12:59:36 +010067
Daniel Mack523f1dc2007-03-26 19:11:24 +020068 if (sub->stream == SNDRV_PCM_STREAM_PLAYBACK)
Daniel Mack1c8470c2013-03-03 20:46:21 +010069 cdev->sub_playback[sub->number] = sub;
Daniel Mack523f1dc2007-03-26 19:11:24 +020070 else
Daniel Mack1c8470c2013-03-03 20:46:21 +010071 cdev->sub_capture[sub->number] = sub;
Mark Hillsac9dd9d2009-10-24 12:59:36 +010072
Daniel Mack1c8470c2013-03-03 20:46:21 +010073 spin_unlock(&cdev->spinlock);
Daniel Mack523f1dc2007-03-26 19:11:24 +020074}
75
Daniel Mack9318dce2009-06-01 21:36:23 +020076static void
Daniel Mack1c8470c2013-03-03 20:46:21 +010077deactivate_substream(struct snd_usb_caiaqdev *cdev,
Daniel Mack523f1dc2007-03-26 19:11:24 +020078 struct snd_pcm_substream *sub)
79{
Daniel Mack8d048842008-04-14 15:39:14 +020080 unsigned long flags;
Daniel Mack1c8470c2013-03-03 20:46:21 +010081 spin_lock_irqsave(&cdev->spinlock, flags);
Daniel Mack8d048842008-04-14 15:39:14 +020082
Daniel Mack523f1dc2007-03-26 19:11:24 +020083 if (sub->stream == SNDRV_PCM_STREAM_PLAYBACK)
Daniel Mack1c8470c2013-03-03 20:46:21 +010084 cdev->sub_playback[sub->number] = NULL;
Daniel Mack523f1dc2007-03-26 19:11:24 +020085 else
Daniel Mack1c8470c2013-03-03 20:46:21 +010086 cdev->sub_capture[sub->number] = NULL;
Daniel Mack8d048842008-04-14 15:39:14 +020087
Daniel Mack1c8470c2013-03-03 20:46:21 +010088 spin_unlock_irqrestore(&cdev->spinlock, flags);
Daniel Mack523f1dc2007-03-26 19:11:24 +020089}
90
91static int
92all_substreams_zero(struct snd_pcm_substream **subs)
93{
94 int i;
95 for (i = 0; i < MAX_STREAMS; i++)
96 if (subs[i] != NULL)
97 return 0;
98 return 1;
99}
100
Daniel Mack1c8470c2013-03-03 20:46:21 +0100101static int stream_start(struct snd_usb_caiaqdev *cdev)
Daniel Mack523f1dc2007-03-26 19:11:24 +0200102{
103 int i, ret;
104
Daniel Mack1c8470c2013-03-03 20:46:21 +0100105 debug("%s(%p)\n", __func__, cdev);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200106
Daniel Mack1c8470c2013-03-03 20:46:21 +0100107 if (cdev->streaming)
Daniel Mack8d048842008-04-14 15:39:14 +0200108 return -EINVAL;
109
Daniel Mack1c8470c2013-03-03 20:46:21 +0100110 memset(cdev->sub_playback, 0, sizeof(cdev->sub_playback));
111 memset(cdev->sub_capture, 0, sizeof(cdev->sub_capture));
112 cdev->input_panic = 0;
113 cdev->output_panic = 0;
114 cdev->first_packet = 4;
115 cdev->streaming = 1;
116 cdev->warned = 0;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200117
118 for (i = 0; i < N_URBS; i++) {
Daniel Mack1c8470c2013-03-03 20:46:21 +0100119 ret = usb_submit_urb(cdev->data_urbs_in[i], GFP_ATOMIC);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200120 if (ret) {
Daniel Mack8d048842008-04-14 15:39:14 +0200121 log("unable to trigger read #%d! (ret %d)\n", i, ret);
Daniel Mack1c8470c2013-03-03 20:46:21 +0100122 cdev->streaming = 0;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200123 return -EPIPE;
124 }
125 }
Daniel Mack9318dce2009-06-01 21:36:23 +0200126
Daniel Mack523f1dc2007-03-26 19:11:24 +0200127 return 0;
128}
129
Daniel Mack1c8470c2013-03-03 20:46:21 +0100130static void stream_stop(struct snd_usb_caiaqdev *cdev)
Daniel Mack523f1dc2007-03-26 19:11:24 +0200131{
132 int i;
Daniel Mack8d048842008-04-14 15:39:14 +0200133
Daniel Mack1c8470c2013-03-03 20:46:21 +0100134 debug("%s(%p)\n", __func__, cdev);
135 if (!cdev->streaming)
Daniel Mack523f1dc2007-03-26 19:11:24 +0200136 return;
Daniel Mack9318dce2009-06-01 21:36:23 +0200137
Daniel Mack1c8470c2013-03-03 20:46:21 +0100138 cdev->streaming = 0;
Daniel Mack8d048842008-04-14 15:39:14 +0200139
Daniel Mack523f1dc2007-03-26 19:11:24 +0200140 for (i = 0; i < N_URBS; i++) {
Daniel Mack1c8470c2013-03-03 20:46:21 +0100141 usb_kill_urb(cdev->data_urbs_in[i]);
Daniel Mackda6094e2011-08-14 11:31:16 +0200142
Daniel Mack1c8470c2013-03-03 20:46:21 +0100143 if (test_bit(i, &cdev->outurb_active_mask))
144 usb_kill_urb(cdev->data_urbs_out[i]);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200145 }
Daniel Mackda6094e2011-08-14 11:31:16 +0200146
Daniel Mack1c8470c2013-03-03 20:46:21 +0100147 cdev->outurb_active_mask = 0;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200148}
149
150static int snd_usb_caiaq_substream_open(struct snd_pcm_substream *substream)
151{
Daniel Mack1c8470c2013-03-03 20:46:21 +0100152 struct snd_usb_caiaqdev *cdev = snd_pcm_substream_chip(substream);
Daniel Mack8d048842008-04-14 15:39:14 +0200153 debug("%s(%p)\n", __func__, substream);
Daniel Mack1c8470c2013-03-03 20:46:21 +0100154 substream->runtime->hw = cdev->pcm_info;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200155 snd_pcm_limit_hw_rates(substream->runtime);
156 return 0;
157}
158
159static int snd_usb_caiaq_substream_close(struct snd_pcm_substream *substream)
160{
Daniel Mack1c8470c2013-03-03 20:46:21 +0100161 struct snd_usb_caiaqdev *cdev = snd_pcm_substream_chip(substream);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200162
Daniel Mack8d048842008-04-14 15:39:14 +0200163 debug("%s(%p)\n", __func__, substream);
Daniel Mack1c8470c2013-03-03 20:46:21 +0100164 if (all_substreams_zero(cdev->sub_playback) &&
165 all_substreams_zero(cdev->sub_capture)) {
Daniel Mack9318dce2009-06-01 21:36:23 +0200166 /* when the last client has stopped streaming,
Daniel Mack523f1dc2007-03-26 19:11:24 +0200167 * all sample rates are allowed again */
Daniel Mack1c8470c2013-03-03 20:46:21 +0100168 stream_stop(cdev);
169 cdev->pcm_info.rates = cdev->samplerates;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200170 }
Daniel Mack8d048842008-04-14 15:39:14 +0200171
Daniel Mack523f1dc2007-03-26 19:11:24 +0200172 return 0;
173}
174
175static int snd_usb_caiaq_pcm_hw_params(struct snd_pcm_substream *sub,
Daniel Mack15c5ab62010-09-10 17:04:57 +0800176 struct snd_pcm_hw_params *hw_params)
Daniel Mack523f1dc2007-03-26 19:11:24 +0200177{
Daniel Mack8d048842008-04-14 15:39:14 +0200178 debug("%s(%p)\n", __func__, sub);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200179 return snd_pcm_lib_malloc_pages(sub, params_buffer_bytes(hw_params));
180}
181
182static int snd_usb_caiaq_pcm_hw_free(struct snd_pcm_substream *sub)
183{
Daniel Mack1c8470c2013-03-03 20:46:21 +0100184 struct snd_usb_caiaqdev *cdev = snd_pcm_substream_chip(sub);
Daniel Mack8d048842008-04-14 15:39:14 +0200185 debug("%s(%p)\n", __func__, sub);
Daniel Mack1c8470c2013-03-03 20:46:21 +0100186 deactivate_substream(cdev, sub);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200187 return snd_pcm_lib_free_pages(sub);
188}
189
190/* this should probably go upstream */
191#if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
192#error "Change this table"
193#endif
194
195static unsigned int rates[] = { 5512, 8000, 11025, 16000, 22050, 32000, 44100,
Daniel Mack15c5ab62010-09-10 17:04:57 +0800196 48000, 64000, 88200, 96000, 176400, 192000 };
Daniel Mack523f1dc2007-03-26 19:11:24 +0200197
198static int snd_usb_caiaq_pcm_prepare(struct snd_pcm_substream *substream)
199{
200 int bytes_per_sample, bpp, ret, i;
201 int index = substream->number;
Daniel Mack1c8470c2013-03-03 20:46:21 +0100202 struct snd_usb_caiaqdev *cdev = snd_pcm_substream_chip(substream);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200203 struct snd_pcm_runtime *runtime = substream->runtime;
204
Daniel Mack8d048842008-04-14 15:39:14 +0200205 debug("%s(%p)\n", __func__, substream);
Daniel Mack9318dce2009-06-01 21:36:23 +0200206
Daniel Macka9b487f2009-04-27 12:18:05 +0200207 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Daniel Mack15c5ab62010-09-10 17:04:57 +0800208 int out_pos;
209
Daniel Mack1c8470c2013-03-03 20:46:21 +0100210 switch (cdev->spec.data_alignment) {
Daniel Mack15c5ab62010-09-10 17:04:57 +0800211 case 0:
212 case 2:
213 out_pos = BYTES_PER_SAMPLE + 1;
214 break;
215 case 3:
216 default:
217 out_pos = 0;
218 break;
219 }
220
Daniel Mack1c8470c2013-03-03 20:46:21 +0100221 cdev->period_out_count[index] = out_pos;
222 cdev->audio_out_buf_pos[index] = out_pos;
Daniel Macka9b487f2009-04-27 12:18:05 +0200223 } else {
Daniel Mack15c5ab62010-09-10 17:04:57 +0800224 int in_pos;
225
Daniel Mack1c8470c2013-03-03 20:46:21 +0100226 switch (cdev->spec.data_alignment) {
Daniel Mack15c5ab62010-09-10 17:04:57 +0800227 case 0:
228 in_pos = BYTES_PER_SAMPLE + 2;
229 break;
230 case 2:
231 in_pos = BYTES_PER_SAMPLE;
232 break;
233 case 3:
234 default:
235 in_pos = 0;
236 break;
237 }
238
Daniel Mack1c8470c2013-03-03 20:46:21 +0100239 cdev->period_in_count[index] = in_pos;
240 cdev->audio_in_buf_pos[index] = in_pos;
Daniel Macka9b487f2009-04-27 12:18:05 +0200241 }
242
Daniel Mack1c8470c2013-03-03 20:46:21 +0100243 if (cdev->streaming)
Daniel Mack523f1dc2007-03-26 19:11:24 +0200244 return 0;
Daniel Mack9318dce2009-06-01 21:36:23 +0200245
Daniel Mack523f1dc2007-03-26 19:11:24 +0200246 /* the first client that opens a stream defines the sample rate
247 * setting for all subsequent calls, until the last client closed. */
248 for (i=0; i < ARRAY_SIZE(rates); i++)
249 if (runtime->rate == rates[i])
Daniel Mack1c8470c2013-03-03 20:46:21 +0100250 cdev->pcm_info.rates = 1 << i;
Daniel Mack9318dce2009-06-01 21:36:23 +0200251
Daniel Mack523f1dc2007-03-26 19:11:24 +0200252 snd_pcm_limit_hw_rates(runtime);
253
254 bytes_per_sample = BYTES_PER_SAMPLE;
Daniel Mack1c8470c2013-03-03 20:46:21 +0100255 if (cdev->spec.data_alignment >= 2)
Daniel Mack523f1dc2007-03-26 19:11:24 +0200256 bytes_per_sample++;
Daniel Mack9318dce2009-06-01 21:36:23 +0200257
Daniel Mack523f1dc2007-03-26 19:11:24 +0200258 bpp = ((runtime->rate / 8000) + CLOCK_DRIFT_TOLERANCE)
Daniel Mack1c8470c2013-03-03 20:46:21 +0100259 * bytes_per_sample * CHANNELS_PER_STREAM * cdev->n_streams;
Daniel Mack6e9fc6b2008-04-14 15:40:31 +0200260
261 if (bpp > MAX_ENDPOINT_SIZE)
262 bpp = MAX_ENDPOINT_SIZE;
263
Daniel Mack1c8470c2013-03-03 20:46:21 +0100264 ret = snd_usb_caiaq_set_audio_params(cdev, runtime->rate,
Daniel Mack523f1dc2007-03-26 19:11:24 +0200265 runtime->sample_bits, bpp);
266 if (ret)
267 return ret;
268
Daniel Mack1c8470c2013-03-03 20:46:21 +0100269 ret = stream_start(cdev);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200270 if (ret)
271 return ret;
Daniel Mack9318dce2009-06-01 21:36:23 +0200272
Daniel Mack1c8470c2013-03-03 20:46:21 +0100273 cdev->output_running = 0;
274 wait_event_timeout(cdev->prepare_wait_queue, cdev->output_running, HZ);
275 if (!cdev->output_running) {
276 stream_stop(cdev);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200277 return -EPIPE;
278 }
279
280 return 0;
281}
282
283static int snd_usb_caiaq_pcm_trigger(struct snd_pcm_substream *sub, int cmd)
284{
Daniel Mack1c8470c2013-03-03 20:46:21 +0100285 struct snd_usb_caiaqdev *cdev = snd_pcm_substream_chip(sub);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200286
Daniel Mack15c5ab62010-09-10 17:04:57 +0800287 debug("%s(%p) cmd %d\n", __func__, sub, cmd);
288
Daniel Mack523f1dc2007-03-26 19:11:24 +0200289 switch (cmd) {
290 case SNDRV_PCM_TRIGGER_START:
291 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Daniel Mack1c8470c2013-03-03 20:46:21 +0100292 activate_substream(cdev, sub);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200293 break;
294 case SNDRV_PCM_TRIGGER_STOP:
295 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
Daniel Mack1c8470c2013-03-03 20:46:21 +0100296 deactivate_substream(cdev, sub);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200297 break;
298 default:
299 return -EINVAL;
300 }
301
302 return 0;
303}
304
305static snd_pcm_uframes_t
306snd_usb_caiaq_pcm_pointer(struct snd_pcm_substream *sub)
307{
308 int index = sub->number;
Daniel Mack1c8470c2013-03-03 20:46:21 +0100309 struct snd_usb_caiaqdev *cdev = snd_pcm_substream_chip(sub);
Mark Hills3702b082009-10-24 12:59:35 +0100310 snd_pcm_uframes_t ptr;
311
Daniel Mack1c8470c2013-03-03 20:46:21 +0100312 spin_lock(&cdev->spinlock);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200313
Daniel Mack1c8470c2013-03-03 20:46:21 +0100314 if (cdev->input_panic || cdev->output_panic) {
Mark Hills3702b082009-10-24 12:59:35 +0100315 ptr = SNDRV_PCM_POS_XRUN;
Mark Hillscb74eb12012-02-21 21:26:31 +0000316 goto unlock;
317 }
Daniel Mack523f1dc2007-03-26 19:11:24 +0200318
319 if (sub->stream == SNDRV_PCM_STREAM_PLAYBACK)
Mark Hills3702b082009-10-24 12:59:35 +0100320 ptr = bytes_to_frames(sub->runtime,
Daniel Mack1c8470c2013-03-03 20:46:21 +0100321 cdev->audio_out_buf_pos[index]);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200322 else
Mark Hills3702b082009-10-24 12:59:35 +0100323 ptr = bytes_to_frames(sub->runtime,
Daniel Mack1c8470c2013-03-03 20:46:21 +0100324 cdev->audio_in_buf_pos[index]);
Mark Hills3702b082009-10-24 12:59:35 +0100325
Mark Hillscb74eb12012-02-21 21:26:31 +0000326unlock:
Daniel Mack1c8470c2013-03-03 20:46:21 +0100327 spin_unlock(&cdev->spinlock);
Mark Hills3702b082009-10-24 12:59:35 +0100328 return ptr;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200329}
330
331/* operators for both playback and capture */
332static struct snd_pcm_ops snd_usb_caiaq_ops = {
333 .open = snd_usb_caiaq_substream_open,
334 .close = snd_usb_caiaq_substream_close,
335 .ioctl = snd_pcm_lib_ioctl,
336 .hw_params = snd_usb_caiaq_pcm_hw_params,
337 .hw_free = snd_usb_caiaq_pcm_hw_free,
338 .prepare = snd_usb_caiaq_pcm_prepare,
339 .trigger = snd_usb_caiaq_pcm_trigger,
340 .pointer = snd_usb_caiaq_pcm_pointer
341};
Daniel Mack9318dce2009-06-01 21:36:23 +0200342
Daniel Mack1c8470c2013-03-03 20:46:21 +0100343static void check_for_elapsed_periods(struct snd_usb_caiaqdev *cdev,
Daniel Mack523f1dc2007-03-26 19:11:24 +0200344 struct snd_pcm_substream **subs)
345{
346 int stream, pb, *cnt;
347 struct snd_pcm_substream *sub;
348
Daniel Mack1c8470c2013-03-03 20:46:21 +0100349 for (stream = 0; stream < cdev->n_streams; stream++) {
Daniel Mack523f1dc2007-03-26 19:11:24 +0200350 sub = subs[stream];
351 if (!sub)
352 continue;
353
Daniel Macka9b487f2009-04-27 12:18:05 +0200354 pb = snd_pcm_lib_period_bytes(sub);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200355 cnt = (sub->stream == SNDRV_PCM_STREAM_PLAYBACK) ?
Daniel Mack1c8470c2013-03-03 20:46:21 +0100356 &cdev->period_out_count[stream] :
357 &cdev->period_in_count[stream];
Daniel Mack523f1dc2007-03-26 19:11:24 +0200358
359 if (*cnt >= pb) {
360 snd_pcm_period_elapsed(sub);
361 *cnt %= pb;
362 }
363 }
364}
365
Daniel Mack1c8470c2013-03-03 20:46:21 +0100366static void read_in_urb_mode0(struct snd_usb_caiaqdev *cdev,
Daniel Mack523f1dc2007-03-26 19:11:24 +0200367 const struct urb *urb,
368 const struct usb_iso_packet_descriptor *iso)
369{
370 unsigned char *usb_buf = urb->transfer_buffer + iso->offset;
371 struct snd_pcm_substream *sub;
372 int stream, i;
373
Daniel Mack1c8470c2013-03-03 20:46:21 +0100374 if (all_substreams_zero(cdev->sub_capture))
Daniel Mack523f1dc2007-03-26 19:11:24 +0200375 return;
376
Daniel Mack523f1dc2007-03-26 19:11:24 +0200377 for (i = 0; i < iso->actual_length;) {
Daniel Mack1c8470c2013-03-03 20:46:21 +0100378 for (stream = 0; stream < cdev->n_streams; stream++, i++) {
379 sub = cdev->sub_capture[stream];
Daniel Mack523f1dc2007-03-26 19:11:24 +0200380 if (sub) {
381 struct snd_pcm_runtime *rt = sub->runtime;
382 char *audio_buf = rt->dma_area;
383 int sz = frames_to_bytes(rt, rt->buffer_size);
Daniel Mack1c8470c2013-03-03 20:46:21 +0100384 audio_buf[cdev->audio_in_buf_pos[stream]++]
Daniel Mack523f1dc2007-03-26 19:11:24 +0200385 = usb_buf[i];
Daniel Mack1c8470c2013-03-03 20:46:21 +0100386 cdev->period_in_count[stream]++;
387 if (cdev->audio_in_buf_pos[stream] == sz)
388 cdev->audio_in_buf_pos[stream] = 0;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200389 }
390 }
391 }
Daniel Mack523f1dc2007-03-26 19:11:24 +0200392}
393
Daniel Mack1c8470c2013-03-03 20:46:21 +0100394static void read_in_urb_mode2(struct snd_usb_caiaqdev *cdev,
Daniel Mack523f1dc2007-03-26 19:11:24 +0200395 const struct urb *urb,
396 const struct usb_iso_packet_descriptor *iso)
397{
398 unsigned char *usb_buf = urb->transfer_buffer + iso->offset;
399 unsigned char check_byte;
400 struct snd_pcm_substream *sub;
401 int stream, i;
402
Daniel Mack523f1dc2007-03-26 19:11:24 +0200403 for (i = 0; i < iso->actual_length;) {
Daniel Mack1c8470c2013-03-03 20:46:21 +0100404 if (i % (cdev->n_streams * BYTES_PER_SAMPLE_USB) == 0) {
Daniel Mack9318dce2009-06-01 21:36:23 +0200405 for (stream = 0;
Daniel Mack1c8470c2013-03-03 20:46:21 +0100406 stream < cdev->n_streams;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200407 stream++, i++) {
Daniel Mack1c8470c2013-03-03 20:46:21 +0100408 if (cdev->first_packet)
Daniel Mack523f1dc2007-03-26 19:11:24 +0200409 continue;
410
Daniel Mack1c8470c2013-03-03 20:46:21 +0100411 check_byte = MAKE_CHECKBYTE(cdev, stream, i);
Daniel Mack9318dce2009-06-01 21:36:23 +0200412
Daniel Mack523f1dc2007-03-26 19:11:24 +0200413 if ((usb_buf[i] & 0x3f) != check_byte)
Daniel Mack1c8470c2013-03-03 20:46:21 +0100414 cdev->input_panic = 1;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200415
416 if (usb_buf[i] & 0x80)
Daniel Mack1c8470c2013-03-03 20:46:21 +0100417 cdev->output_panic = 1;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200418 }
419 }
Daniel Mack1c8470c2013-03-03 20:46:21 +0100420 cdev->first_packet = 0;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200421
Daniel Mack1c8470c2013-03-03 20:46:21 +0100422 for (stream = 0; stream < cdev->n_streams; stream++, i++) {
423 sub = cdev->sub_capture[stream];
424 if (cdev->input_panic)
Daniel Mack9311c9b2009-03-18 11:03:54 +0100425 usb_buf[i] = 0;
426
Daniel Mack523f1dc2007-03-26 19:11:24 +0200427 if (sub) {
428 struct snd_pcm_runtime *rt = sub->runtime;
429 char *audio_buf = rt->dma_area;
430 int sz = frames_to_bytes(rt, rt->buffer_size);
Daniel Mack1c8470c2013-03-03 20:46:21 +0100431 audio_buf[cdev->audio_in_buf_pos[stream]++] =
Karsten Wiesea971c3d42007-03-29 17:02:45 +0200432 usb_buf[i];
Daniel Mack1c8470c2013-03-03 20:46:21 +0100433 cdev->period_in_count[stream]++;
434 if (cdev->audio_in_buf_pos[stream] == sz)
435 cdev->audio_in_buf_pos[stream] = 0;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200436 }
437 }
438 }
Daniel Mack523f1dc2007-03-26 19:11:24 +0200439}
440
Daniel Mack1c8470c2013-03-03 20:46:21 +0100441static void read_in_urb_mode3(struct snd_usb_caiaqdev *cdev,
Daniel Mack15c5ab62010-09-10 17:04:57 +0800442 const struct urb *urb,
443 const struct usb_iso_packet_descriptor *iso)
444{
445 unsigned char *usb_buf = urb->transfer_buffer + iso->offset;
446 int stream, i;
447
448 /* paranoia check */
449 if (iso->actual_length % (BYTES_PER_SAMPLE_USB * CHANNELS_PER_STREAM))
450 return;
451
452 for (i = 0; i < iso->actual_length;) {
Daniel Mack1c8470c2013-03-03 20:46:21 +0100453 for (stream = 0; stream < cdev->n_streams; stream++) {
454 struct snd_pcm_substream *sub = cdev->sub_capture[stream];
Daniel Mack15c5ab62010-09-10 17:04:57 +0800455 char *audio_buf = NULL;
456 int c, n, sz = 0;
457
Daniel Mack1c8470c2013-03-03 20:46:21 +0100458 if (sub && !cdev->input_panic) {
Daniel Mack15c5ab62010-09-10 17:04:57 +0800459 struct snd_pcm_runtime *rt = sub->runtime;
460 audio_buf = rt->dma_area;
461 sz = frames_to_bytes(rt, rt->buffer_size);
462 }
463
464 for (c = 0; c < CHANNELS_PER_STREAM; c++) {
465 /* 3 audio data bytes, followed by 1 check byte */
466 if (audio_buf) {
467 for (n = 0; n < BYTES_PER_SAMPLE; n++) {
Daniel Mack1c8470c2013-03-03 20:46:21 +0100468 audio_buf[cdev->audio_in_buf_pos[stream]++] = usb_buf[i+n];
Daniel Mack15c5ab62010-09-10 17:04:57 +0800469
Daniel Mack1c8470c2013-03-03 20:46:21 +0100470 if (cdev->audio_in_buf_pos[stream] == sz)
471 cdev->audio_in_buf_pos[stream] = 0;
Daniel Mack15c5ab62010-09-10 17:04:57 +0800472 }
473
Daniel Mack1c8470c2013-03-03 20:46:21 +0100474 cdev->period_in_count[stream] += BYTES_PER_SAMPLE;
Daniel Mack15c5ab62010-09-10 17:04:57 +0800475 }
476
477 i += BYTES_PER_SAMPLE;
478
479 if (usb_buf[i] != ((stream << 1) | c) &&
Daniel Mack1c8470c2013-03-03 20:46:21 +0100480 !cdev->first_packet) {
481 if (!cdev->input_panic)
Daniel Mack15c5ab62010-09-10 17:04:57 +0800482 printk(" EXPECTED: %02x got %02x, c %d, stream %d, i %d\n",
483 ((stream << 1) | c), usb_buf[i], c, stream, i);
Daniel Mack1c8470c2013-03-03 20:46:21 +0100484 cdev->input_panic = 1;
Daniel Mack15c5ab62010-09-10 17:04:57 +0800485 }
486
487 i++;
488 }
489 }
490 }
491
Daniel Mack1c8470c2013-03-03 20:46:21 +0100492 if (cdev->first_packet > 0)
493 cdev->first_packet--;
Daniel Mack15c5ab62010-09-10 17:04:57 +0800494}
495
Daniel Mack1c8470c2013-03-03 20:46:21 +0100496static void read_in_urb(struct snd_usb_caiaqdev *cdev,
Daniel Mack523f1dc2007-03-26 19:11:24 +0200497 const struct urb *urb,
498 const struct usb_iso_packet_descriptor *iso)
499{
Daniel Mack1c8470c2013-03-03 20:46:21 +0100500 if (!cdev->streaming)
Daniel Mack523f1dc2007-03-26 19:11:24 +0200501 return;
502
Daniel Mack1c8470c2013-03-03 20:46:21 +0100503 if (iso->actual_length < cdev->bpp)
Daniel Mack9311c9b2009-03-18 11:03:54 +0100504 return;
505
Daniel Mack1c8470c2013-03-03 20:46:21 +0100506 switch (cdev->spec.data_alignment) {
Daniel Mack523f1dc2007-03-26 19:11:24 +0200507 case 0:
Daniel Mack1c8470c2013-03-03 20:46:21 +0100508 read_in_urb_mode0(cdev, urb, iso);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200509 break;
510 case 2:
Daniel Mack1c8470c2013-03-03 20:46:21 +0100511 read_in_urb_mode2(cdev, urb, iso);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200512 break;
Daniel Mack15c5ab62010-09-10 17:04:57 +0800513 case 3:
Daniel Mack1c8470c2013-03-03 20:46:21 +0100514 read_in_urb_mode3(cdev, urb, iso);
Daniel Mack15c5ab62010-09-10 17:04:57 +0800515 break;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200516 }
517
Daniel Mack1c8470c2013-03-03 20:46:21 +0100518 if ((cdev->input_panic || cdev->output_panic) && !cdev->warned) {
Daniel Mack9318dce2009-06-01 21:36:23 +0200519 debug("streaming error detected %s %s\n",
Daniel Mack1c8470c2013-03-03 20:46:21 +0100520 cdev->input_panic ? "(input)" : "",
521 cdev->output_panic ? "(output)" : "");
522 cdev->warned = 1;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200523 }
Daniel Mack523f1dc2007-03-26 19:11:24 +0200524}
525
Daniel Mack1c8470c2013-03-03 20:46:21 +0100526static void fill_out_urb_mode_0(struct snd_usb_caiaqdev *cdev,
Daniel Mack15c5ab62010-09-10 17:04:57 +0800527 struct urb *urb,
528 const struct usb_iso_packet_descriptor *iso)
Daniel Mack523f1dc2007-03-26 19:11:24 +0200529{
530 unsigned char *usb_buf = urb->transfer_buffer + iso->offset;
531 struct snd_pcm_substream *sub;
532 int stream, i;
Daniel Mack9318dce2009-06-01 21:36:23 +0200533
Daniel Mack523f1dc2007-03-26 19:11:24 +0200534 for (i = 0; i < iso->length;) {
Daniel Mack1c8470c2013-03-03 20:46:21 +0100535 for (stream = 0; stream < cdev->n_streams; stream++, i++) {
536 sub = cdev->sub_playback[stream];
Daniel Mack523f1dc2007-03-26 19:11:24 +0200537 if (sub) {
538 struct snd_pcm_runtime *rt = sub->runtime;
539 char *audio_buf = rt->dma_area;
540 int sz = frames_to_bytes(rt, rt->buffer_size);
Karsten Wiesea971c3d42007-03-29 17:02:45 +0200541 usb_buf[i] =
Daniel Mack1c8470c2013-03-03 20:46:21 +0100542 audio_buf[cdev->audio_out_buf_pos[stream]];
543 cdev->period_out_count[stream]++;
544 cdev->audio_out_buf_pos[stream]++;
545 if (cdev->audio_out_buf_pos[stream] == sz)
546 cdev->audio_out_buf_pos[stream] = 0;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200547 } else
Karsten Wiesea971c3d42007-03-29 17:02:45 +0200548 usb_buf[i] = 0;
549 }
Daniel Mack523f1dc2007-03-26 19:11:24 +0200550
551 /* fill in the check bytes */
Daniel Mack1c8470c2013-03-03 20:46:21 +0100552 if (cdev->spec.data_alignment == 2 &&
553 i % (cdev->n_streams * BYTES_PER_SAMPLE_USB) ==
554 (cdev->n_streams * CHANNELS_PER_STREAM))
555 for (stream = 0; stream < cdev->n_streams; stream++, i++)
556 usb_buf[i] = MAKE_CHECKBYTE(cdev, stream, i);
Daniel Mack15c5ab62010-09-10 17:04:57 +0800557 }
558}
559
Daniel Mack1c8470c2013-03-03 20:46:21 +0100560static void fill_out_urb_mode_3(struct snd_usb_caiaqdev *cdev,
Daniel Mack15c5ab62010-09-10 17:04:57 +0800561 struct urb *urb,
562 const struct usb_iso_packet_descriptor *iso)
563{
564 unsigned char *usb_buf = urb->transfer_buffer + iso->offset;
565 int stream, i;
566
567 for (i = 0; i < iso->length;) {
Daniel Mack1c8470c2013-03-03 20:46:21 +0100568 for (stream = 0; stream < cdev->n_streams; stream++) {
569 struct snd_pcm_substream *sub = cdev->sub_playback[stream];
Daniel Mack15c5ab62010-09-10 17:04:57 +0800570 char *audio_buf = NULL;
571 int c, n, sz = 0;
572
573 if (sub) {
574 struct snd_pcm_runtime *rt = sub->runtime;
575 audio_buf = rt->dma_area;
576 sz = frames_to_bytes(rt, rt->buffer_size);
577 }
578
579 for (c = 0; c < CHANNELS_PER_STREAM; c++) {
580 for (n = 0; n < BYTES_PER_SAMPLE; n++) {
581 if (audio_buf) {
Daniel Mack1c8470c2013-03-03 20:46:21 +0100582 usb_buf[i+n] = audio_buf[cdev->audio_out_buf_pos[stream]++];
Daniel Mack15c5ab62010-09-10 17:04:57 +0800583
Daniel Mack1c8470c2013-03-03 20:46:21 +0100584 if (cdev->audio_out_buf_pos[stream] == sz)
585 cdev->audio_out_buf_pos[stream] = 0;
Daniel Mack15c5ab62010-09-10 17:04:57 +0800586 } else {
587 usb_buf[i+n] = 0;
588 }
589 }
590
591 if (audio_buf)
Daniel Mack1c8470c2013-03-03 20:46:21 +0100592 cdev->period_out_count[stream] += BYTES_PER_SAMPLE;
Daniel Mack15c5ab62010-09-10 17:04:57 +0800593
594 i += BYTES_PER_SAMPLE;
595
596 /* fill in the check byte pattern */
597 usb_buf[i++] = (stream << 1) | c;
598 }
599 }
600 }
601}
602
Daniel Mack1c8470c2013-03-03 20:46:21 +0100603static inline void fill_out_urb(struct snd_usb_caiaqdev *cdev,
Daniel Mack15c5ab62010-09-10 17:04:57 +0800604 struct urb *urb,
605 const struct usb_iso_packet_descriptor *iso)
606{
Daniel Mack1c8470c2013-03-03 20:46:21 +0100607 switch (cdev->spec.data_alignment) {
Daniel Mack15c5ab62010-09-10 17:04:57 +0800608 case 0:
609 case 2:
Daniel Mack1c8470c2013-03-03 20:46:21 +0100610 fill_out_urb_mode_0(cdev, urb, iso);
Daniel Mack15c5ab62010-09-10 17:04:57 +0800611 break;
612 case 3:
Daniel Mack1c8470c2013-03-03 20:46:21 +0100613 fill_out_urb_mode_3(cdev, urb, iso);
Daniel Mack15c5ab62010-09-10 17:04:57 +0800614 break;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200615 }
Daniel Mack523f1dc2007-03-26 19:11:24 +0200616}
617
618static void read_completed(struct urb *urb)
619{
Daniel Mack9318dce2009-06-01 21:36:23 +0200620 struct snd_usb_caiaq_cb_info *info = urb->context;
Daniel Mack1c8470c2013-03-03 20:46:21 +0100621 struct snd_usb_caiaqdev *cdev;
Daniel Mackda6094e2011-08-14 11:31:16 +0200622 struct urb *out = NULL;
623 int i, frame, len, send_it = 0, outframe = 0;
Daniel Mack15439bd2011-08-05 13:49:52 +0200624 size_t offset = 0;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200625
626 if (urb->status || !info)
627 return;
628
Daniel Mack1c8470c2013-03-03 20:46:21 +0100629 cdev = info->cdev;
Daniel Mack8d048842008-04-14 15:39:14 +0200630
Daniel Mack1c8470c2013-03-03 20:46:21 +0100631 if (!cdev->streaming)
Daniel Mack523f1dc2007-03-26 19:11:24 +0200632 return;
633
Daniel Mackda6094e2011-08-14 11:31:16 +0200634 /* find an unused output urb that is unused */
635 for (i = 0; i < N_URBS; i++)
Daniel Mack1c8470c2013-03-03 20:46:21 +0100636 if (test_and_set_bit(i, &cdev->outurb_active_mask) == 0) {
637 out = cdev->data_urbs_out[i];
Daniel Mackda6094e2011-08-14 11:31:16 +0200638 break;
639 }
640
641 if (!out) {
642 log("Unable to find an output urb to use\n");
643 goto requeue;
644 }
Daniel Mack523f1dc2007-03-26 19:11:24 +0200645
646 /* read the recently received packet and send back one which has
647 * the same layout */
648 for (frame = 0; frame < FRAMES_PER_URB; frame++) {
649 if (urb->iso_frame_desc[frame].status)
650 continue;
651
652 len = urb->iso_frame_desc[outframe].actual_length;
653 out->iso_frame_desc[outframe].length = len;
654 out->iso_frame_desc[outframe].actual_length = 0;
Daniel Mack15439bd2011-08-05 13:49:52 +0200655 out->iso_frame_desc[outframe].offset = offset;
656 offset += len;
Daniel Mack9318dce2009-06-01 21:36:23 +0200657
Daniel Mack523f1dc2007-03-26 19:11:24 +0200658 if (len > 0) {
Daniel Mack1c8470c2013-03-03 20:46:21 +0100659 spin_lock(&cdev->spinlock);
660 fill_out_urb(cdev, out, &out->iso_frame_desc[outframe]);
661 read_in_urb(cdev, urb, &urb->iso_frame_desc[frame]);
662 spin_unlock(&cdev->spinlock);
663 check_for_elapsed_periods(cdev, cdev->sub_playback);
664 check_for_elapsed_periods(cdev, cdev->sub_capture);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200665 send_it = 1;
666 }
667
668 outframe++;
669 }
670
671 if (send_it) {
Daniel Mack15439bd2011-08-05 13:49:52 +0200672 out->number_of_packets = outframe;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200673 out->transfer_flags = URB_ISO_ASAP;
674 usb_submit_urb(out, GFP_ATOMIC);
Daniel Mackda6094e2011-08-14 11:31:16 +0200675 } else {
676 struct snd_usb_caiaq_cb_info *oinfo = out->context;
Daniel Mack1c8470c2013-03-03 20:46:21 +0100677 clear_bit(oinfo->index, &cdev->outurb_active_mask);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200678 }
Daniel Mack9318dce2009-06-01 21:36:23 +0200679
Daniel Mackda6094e2011-08-14 11:31:16 +0200680requeue:
Daniel Mack523f1dc2007-03-26 19:11:24 +0200681 /* re-submit inbound urb */
682 for (frame = 0; frame < FRAMES_PER_URB; frame++) {
683 urb->iso_frame_desc[frame].offset = BYTES_PER_FRAME * frame;
684 urb->iso_frame_desc[frame].length = BYTES_PER_FRAME;
685 urb->iso_frame_desc[frame].actual_length = 0;
686 }
Daniel Mack9318dce2009-06-01 21:36:23 +0200687
Daniel Mack523f1dc2007-03-26 19:11:24 +0200688 urb->number_of_packets = FRAMES_PER_URB;
689 urb->transfer_flags = URB_ISO_ASAP;
690 usb_submit_urb(urb, GFP_ATOMIC);
691}
692
693static void write_completed(struct urb *urb)
694{
695 struct snd_usb_caiaq_cb_info *info = urb->context;
Daniel Mack1c8470c2013-03-03 20:46:21 +0100696 struct snd_usb_caiaqdev *cdev = info->cdev;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200697
Daniel Mack1c8470c2013-03-03 20:46:21 +0100698 if (!cdev->output_running) {
699 cdev->output_running = 1;
700 wake_up(&cdev->prepare_wait_queue);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200701 }
Daniel Mackda6094e2011-08-14 11:31:16 +0200702
Daniel Mack1c8470c2013-03-03 20:46:21 +0100703 clear_bit(info->index, &cdev->outurb_active_mask);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200704}
705
Daniel Mack1c8470c2013-03-03 20:46:21 +0100706static struct urb **alloc_urbs(struct snd_usb_caiaqdev *cdev, int dir, int *ret)
Daniel Mack523f1dc2007-03-26 19:11:24 +0200707{
708 int i, frame;
709 struct urb **urbs;
Daniel Mack1c8470c2013-03-03 20:46:21 +0100710 struct usb_device *usb_dev = cdev->chip.dev;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200711 unsigned int pipe;
712
Daniel Mack9318dce2009-06-01 21:36:23 +0200713 pipe = (dir == SNDRV_PCM_STREAM_PLAYBACK) ?
Daniel Mack523f1dc2007-03-26 19:11:24 +0200714 usb_sndisocpipe(usb_dev, ENDPOINT_PLAYBACK) :
715 usb_rcvisocpipe(usb_dev, ENDPOINT_CAPTURE);
716
717 urbs = kmalloc(N_URBS * sizeof(*urbs), GFP_KERNEL);
718 if (!urbs) {
719 log("unable to kmalloc() urbs, OOM!?\n");
720 *ret = -ENOMEM;
721 return NULL;
722 }
723
724 for (i = 0; i < N_URBS; i++) {
725 urbs[i] = usb_alloc_urb(FRAMES_PER_URB, GFP_KERNEL);
726 if (!urbs[i]) {
727 log("unable to usb_alloc_urb(), OOM!?\n");
728 *ret = -ENOMEM;
729 return urbs;
730 }
731
Daniel Mack9318dce2009-06-01 21:36:23 +0200732 urbs[i]->transfer_buffer =
Daniel Mack523f1dc2007-03-26 19:11:24 +0200733 kmalloc(FRAMES_PER_URB * BYTES_PER_FRAME, GFP_KERNEL);
734 if (!urbs[i]->transfer_buffer) {
735 log("unable to kmalloc() transfer buffer, OOM!?\n");
736 *ret = -ENOMEM;
737 return urbs;
738 }
Daniel Mack9318dce2009-06-01 21:36:23 +0200739
Daniel Mack523f1dc2007-03-26 19:11:24 +0200740 for (frame = 0; frame < FRAMES_PER_URB; frame++) {
Daniel Mack9318dce2009-06-01 21:36:23 +0200741 struct usb_iso_packet_descriptor *iso =
Daniel Mack523f1dc2007-03-26 19:11:24 +0200742 &urbs[i]->iso_frame_desc[frame];
Daniel Mack9318dce2009-06-01 21:36:23 +0200743
Daniel Mack523f1dc2007-03-26 19:11:24 +0200744 iso->offset = BYTES_PER_FRAME * frame;
745 iso->length = BYTES_PER_FRAME;
746 }
Daniel Mack9318dce2009-06-01 21:36:23 +0200747
Daniel Mack523f1dc2007-03-26 19:11:24 +0200748 urbs[i]->dev = usb_dev;
749 urbs[i]->pipe = pipe;
Daniel Mack9318dce2009-06-01 21:36:23 +0200750 urbs[i]->transfer_buffer_length = FRAMES_PER_URB
Daniel Mack523f1dc2007-03-26 19:11:24 +0200751 * BYTES_PER_FRAME;
Daniel Mack1c8470c2013-03-03 20:46:21 +0100752 urbs[i]->context = &cdev->data_cb_info[i];
Daniel Mack523f1dc2007-03-26 19:11:24 +0200753 urbs[i]->interval = 1;
754 urbs[i]->transfer_flags = URB_ISO_ASAP;
755 urbs[i]->number_of_packets = FRAMES_PER_URB;
756 urbs[i]->complete = (dir == SNDRV_PCM_STREAM_CAPTURE) ?
757 read_completed : write_completed;
758 }
759
760 *ret = 0;
761 return urbs;
762}
763
764static void free_urbs(struct urb **urbs)
765{
766 int i;
767
768 if (!urbs)
769 return;
770
771 for (i = 0; i < N_URBS; i++) {
772 if (!urbs[i])
773 continue;
Daniel Mack9318dce2009-06-01 21:36:23 +0200774
Daniel Mack523f1dc2007-03-26 19:11:24 +0200775 usb_kill_urb(urbs[i]);
776 kfree(urbs[i]->transfer_buffer);
777 usb_free_urb(urbs[i]);
778 }
779
780 kfree(urbs);
781}
782
Daniel Mack1c8470c2013-03-03 20:46:21 +0100783int snd_usb_caiaq_audio_init(struct snd_usb_caiaqdev *cdev)
Daniel Mack523f1dc2007-03-26 19:11:24 +0200784{
785 int i, ret;
786
Daniel Mack1c8470c2013-03-03 20:46:21 +0100787 cdev->n_audio_in = max(cdev->spec.num_analog_audio_in,
788 cdev->spec.num_digital_audio_in) /
Daniel Mack523f1dc2007-03-26 19:11:24 +0200789 CHANNELS_PER_STREAM;
Daniel Mack1c8470c2013-03-03 20:46:21 +0100790 cdev->n_audio_out = max(cdev->spec.num_analog_audio_out,
791 cdev->spec.num_digital_audio_out) /
Daniel Mack523f1dc2007-03-26 19:11:24 +0200792 CHANNELS_PER_STREAM;
Daniel Mack1c8470c2013-03-03 20:46:21 +0100793 cdev->n_streams = max(cdev->n_audio_in, cdev->n_audio_out);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200794
Daniel Mack1c8470c2013-03-03 20:46:21 +0100795 debug("cdev->n_audio_in = %d\n", cdev->n_audio_in);
796 debug("cdev->n_audio_out = %d\n", cdev->n_audio_out);
797 debug("cdev->n_streams = %d\n", cdev->n_streams);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200798
Daniel Mack1c8470c2013-03-03 20:46:21 +0100799 if (cdev->n_streams > MAX_STREAMS) {
Daniel Mack523f1dc2007-03-26 19:11:24 +0200800 log("unable to initialize device, too many streams.\n");
801 return -EINVAL;
802 }
803
Daniel Mack1c8470c2013-03-03 20:46:21 +0100804 ret = snd_pcm_new(cdev->chip.card, cdev->product_name, 0,
805 cdev->n_audio_out, cdev->n_audio_in, &cdev->pcm);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200806
807 if (ret < 0) {
808 log("snd_pcm_new() returned %d\n", ret);
809 return ret;
810 }
811
Daniel Mack1c8470c2013-03-03 20:46:21 +0100812 cdev->pcm->private_data = cdev;
813 strlcpy(cdev->pcm->name, cdev->product_name, sizeof(cdev->pcm->name));
Daniel Mack523f1dc2007-03-26 19:11:24 +0200814
Daniel Mack1c8470c2013-03-03 20:46:21 +0100815 memset(cdev->sub_playback, 0, sizeof(cdev->sub_playback));
816 memset(cdev->sub_capture, 0, sizeof(cdev->sub_capture));
Daniel Mack9318dce2009-06-01 21:36:23 +0200817
Daniel Mack1c8470c2013-03-03 20:46:21 +0100818 memcpy(&cdev->pcm_info, &snd_usb_caiaq_pcm_hardware,
Daniel Mack523f1dc2007-03-26 19:11:24 +0200819 sizeof(snd_usb_caiaq_pcm_hardware));
820
821 /* setup samplerates */
Daniel Mack1c8470c2013-03-03 20:46:21 +0100822 cdev->samplerates = cdev->pcm_info.rates;
823 switch (cdev->chip.usb_id) {
Daniel Mack523f1dc2007-03-26 19:11:24 +0200824 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AK1):
Daniel Mackad1e34b2007-09-17 14:45:14 +0200825 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_RIGKONTROL3):
Daniel Mackf3e9d5d2008-05-08 15:42:15 +0200826 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_SESSIONIO):
Daniel Mack21655922009-01-16 11:03:19 +0100827 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_GUITARRIGMOBILE):
Daniel Mack1c8470c2013-03-03 20:46:21 +0100828 cdev->samplerates |= SNDRV_PCM_RATE_192000;
Daniel Mack21655922009-01-16 11:03:19 +0100829 /* fall thru */
Daniel Mackb30c4942009-07-22 14:13:35 +0200830 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AUDIO2DJ):
Daniel Mack21655922009-01-16 11:03:19 +0100831 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AUDIO4DJ):
Daniel Mack523f1dc2007-03-26 19:11:24 +0200832 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AUDIO8DJ):
Daniel Mackdf8d81a2010-09-01 16:23:46 +0800833 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_TRAKTORAUDIO2):
Daniel Mack1c8470c2013-03-03 20:46:21 +0100834 cdev->samplerates |= SNDRV_PCM_RATE_88200;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200835 break;
836 }
837
Daniel Mack1c8470c2013-03-03 20:46:21 +0100838 snd_pcm_set_ops(cdev->pcm, SNDRV_PCM_STREAM_PLAYBACK,
Daniel Mack523f1dc2007-03-26 19:11:24 +0200839 &snd_usb_caiaq_ops);
Daniel Mack1c8470c2013-03-03 20:46:21 +0100840 snd_pcm_set_ops(cdev->pcm, SNDRV_PCM_STREAM_CAPTURE,
Daniel Mack523f1dc2007-03-26 19:11:24 +0200841 &snd_usb_caiaq_ops);
842
Daniel Mack1c8470c2013-03-03 20:46:21 +0100843 snd_pcm_lib_preallocate_pages_for_all(cdev->pcm,
Daniel Mack523f1dc2007-03-26 19:11:24 +0200844 SNDRV_DMA_TYPE_CONTINUOUS,
845 snd_dma_continuous_data(GFP_KERNEL),
846 MAX_BUFFER_SIZE, MAX_BUFFER_SIZE);
847
Daniel Mack1c8470c2013-03-03 20:46:21 +0100848 cdev->data_cb_info =
Daniel Mack9318dce2009-06-01 21:36:23 +0200849 kmalloc(sizeof(struct snd_usb_caiaq_cb_info) * N_URBS,
Daniel Mack523f1dc2007-03-26 19:11:24 +0200850 GFP_KERNEL);
851
Daniel Mack1c8470c2013-03-03 20:46:21 +0100852 if (!cdev->data_cb_info)
Daniel Mack523f1dc2007-03-26 19:11:24 +0200853 return -ENOMEM;
854
Daniel Mack1c8470c2013-03-03 20:46:21 +0100855 cdev->outurb_active_mask = 0;
856 BUILD_BUG_ON(N_URBS > (sizeof(cdev->outurb_active_mask) * 8));
Daniel Mackda6094e2011-08-14 11:31:16 +0200857
Daniel Mack523f1dc2007-03-26 19:11:24 +0200858 for (i = 0; i < N_URBS; i++) {
Daniel Mack1c8470c2013-03-03 20:46:21 +0100859 cdev->data_cb_info[i].cdev = cdev;
860 cdev->data_cb_info[i].index = i;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200861 }
Daniel Mack9318dce2009-06-01 21:36:23 +0200862
Daniel Mack1c8470c2013-03-03 20:46:21 +0100863 cdev->data_urbs_in = alloc_urbs(cdev, SNDRV_PCM_STREAM_CAPTURE, &ret);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200864 if (ret < 0) {
Daniel Mack1c8470c2013-03-03 20:46:21 +0100865 kfree(cdev->data_cb_info);
866 free_urbs(cdev->data_urbs_in);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200867 return ret;
868 }
Daniel Mack9318dce2009-06-01 21:36:23 +0200869
Daniel Mack1c8470c2013-03-03 20:46:21 +0100870 cdev->data_urbs_out = alloc_urbs(cdev, SNDRV_PCM_STREAM_PLAYBACK, &ret);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200871 if (ret < 0) {
Daniel Mack1c8470c2013-03-03 20:46:21 +0100872 kfree(cdev->data_cb_info);
873 free_urbs(cdev->data_urbs_in);
874 free_urbs(cdev->data_urbs_out);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200875 return ret;
876 }
877
878 return 0;
879}
880
Daniel Mack1c8470c2013-03-03 20:46:21 +0100881void snd_usb_caiaq_audio_free(struct snd_usb_caiaqdev *cdev)
Daniel Mack523f1dc2007-03-26 19:11:24 +0200882{
Daniel Mack1c8470c2013-03-03 20:46:21 +0100883 debug("%s(%p)\n", __func__, cdev);
884 stream_stop(cdev);
885 free_urbs(cdev->data_urbs_in);
886 free_urbs(cdev->data_urbs_out);
887 kfree(cdev->data_cb_info);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200888}
889