blob: b13ce767ac7235314bd8dad23373d640302167ca [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>
Daniel Macke431cf42009-03-28 21:19:49 +010020#include <linux/init.h>
21#include <linux/usb.h>
Daniel Mack523f1dc2007-03-26 19:11:24 +020022#include <sound/core.h>
Daniel Mack523f1dc2007-03-26 19:11:24 +020023#include <sound/pcm.h>
Daniel Mack523f1dc2007-03-26 19:11:24 +020024
Daniel Mack936e7d02009-04-01 19:05:39 +020025#include "device.h"
26#include "audio.h"
Daniel Mack523f1dc2007-03-26 19:11:24 +020027
28#define N_URBS 32
29#define CLOCK_DRIFT_TOLERANCE 5
30#define FRAMES_PER_URB 8
31#define BYTES_PER_FRAME 512
32#define CHANNELS_PER_STREAM 2
33#define BYTES_PER_SAMPLE 3
34#define BYTES_PER_SAMPLE_USB 4
35#define MAX_BUFFER_SIZE (128*1024)
Daniel Mack6e9fc6b2008-04-14 15:40:31 +020036#define MAX_ENDPOINT_SIZE 512
37
Daniel Mack523f1dc2007-03-26 19:11:24 +020038#define ENDPOINT_CAPTURE 2
39#define ENDPOINT_PLAYBACK 6
40
41#define MAKE_CHECKBYTE(dev,stream,i) \
42 (stream << 1) | (~(i / (dev->n_streams * BYTES_PER_SAMPLE_USB)) & 1)
43
44static struct snd_pcm_hardware snd_usb_caiaq_pcm_hardware = {
45 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
46 SNDRV_PCM_INFO_BLOCK_TRANSFER),
47 .formats = SNDRV_PCM_FMTBIT_S24_3BE,
48 .rates = (SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |
49 SNDRV_PCM_RATE_96000),
50 .rate_min = 44100,
51 .rate_max = 0, /* will overwrite later */
52 .channels_min = CHANNELS_PER_STREAM,
53 .channels_max = CHANNELS_PER_STREAM,
54 .buffer_bytes_max = MAX_BUFFER_SIZE,
Daniel Mack09189ac2008-01-24 18:46:42 +010055 .period_bytes_min = 128,
Daniel Mack523f1dc2007-03-26 19:11:24 +020056 .period_bytes_max = MAX_BUFFER_SIZE,
57 .periods_min = 1,
58 .periods_max = 1024,
59};
60
61static void
62activate_substream(struct snd_usb_caiaqdev *dev,
63 struct snd_pcm_substream *sub)
64{
65 if (sub->stream == SNDRV_PCM_STREAM_PLAYBACK)
66 dev->sub_playback[sub->number] = sub;
67 else
68 dev->sub_capture[sub->number] = sub;
69}
70
71static void
72deactivate_substream(struct snd_usb_caiaqdev *dev,
73 struct snd_pcm_substream *sub)
74{
Daniel Mack8d048842008-04-14 15:39:14 +020075 unsigned long flags;
76 spin_lock_irqsave(&dev->spinlock, flags);
77
Daniel Mack523f1dc2007-03-26 19:11:24 +020078 if (sub->stream == SNDRV_PCM_STREAM_PLAYBACK)
79 dev->sub_playback[sub->number] = NULL;
80 else
81 dev->sub_capture[sub->number] = NULL;
Daniel Mack8d048842008-04-14 15:39:14 +020082
83 spin_unlock_irqrestore(&dev->spinlock, flags);
Daniel Mack523f1dc2007-03-26 19:11:24 +020084}
85
86static int
87all_substreams_zero(struct snd_pcm_substream **subs)
88{
89 int i;
90 for (i = 0; i < MAX_STREAMS; i++)
91 if (subs[i] != NULL)
92 return 0;
93 return 1;
94}
95
96static int stream_start(struct snd_usb_caiaqdev *dev)
97{
98 int i, ret;
99
Daniel Mack8d048842008-04-14 15:39:14 +0200100 debug("%s(%p)\n", __func__, dev);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200101
Daniel Mack8d048842008-04-14 15:39:14 +0200102 if (dev->streaming)
103 return -EINVAL;
104
105 memset(dev->sub_playback, 0, sizeof(dev->sub_playback));
106 memset(dev->sub_capture, 0, sizeof(dev->sub_capture));
Daniel Mack523f1dc2007-03-26 19:11:24 +0200107 dev->input_panic = 0;
108 dev->output_panic = 0;
109 dev->first_packet = 1;
110 dev->streaming = 1;
Daniel Mack1313e702009-03-18 11:03:53 +0100111 dev->warned = 0;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200112
113 for (i = 0; i < N_URBS; i++) {
114 ret = usb_submit_urb(dev->data_urbs_in[i], GFP_ATOMIC);
115 if (ret) {
Daniel Mack8d048842008-04-14 15:39:14 +0200116 log("unable to trigger read #%d! (ret %d)\n", i, ret);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200117 dev->streaming = 0;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200118 return -EPIPE;
119 }
120 }
121
Daniel Mack523f1dc2007-03-26 19:11:24 +0200122 return 0;
123}
124
125static void stream_stop(struct snd_usb_caiaqdev *dev)
126{
127 int i;
Daniel Mack8d048842008-04-14 15:39:14 +0200128
129 debug("%s(%p)\n", __func__, dev);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200130 if (!dev->streaming)
131 return;
132
133 dev->streaming = 0;
Daniel Mack8d048842008-04-14 15:39:14 +0200134
Daniel Mack523f1dc2007-03-26 19:11:24 +0200135 for (i = 0; i < N_URBS; i++) {
Daniel Mack8d048842008-04-14 15:39:14 +0200136 usb_kill_urb(dev->data_urbs_in[i]);
137 usb_kill_urb(dev->data_urbs_out[i]);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200138 }
139}
140
141static int snd_usb_caiaq_substream_open(struct snd_pcm_substream *substream)
142{
143 struct snd_usb_caiaqdev *dev = snd_pcm_substream_chip(substream);
Daniel Mack8d048842008-04-14 15:39:14 +0200144 debug("%s(%p)\n", __func__, substream);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200145 substream->runtime->hw = dev->pcm_info;
146 snd_pcm_limit_hw_rates(substream->runtime);
147 return 0;
148}
149
150static int snd_usb_caiaq_substream_close(struct snd_pcm_substream *substream)
151{
152 struct snd_usb_caiaqdev *dev = snd_pcm_substream_chip(substream);
153
Daniel Mack8d048842008-04-14 15:39:14 +0200154 debug("%s(%p)\n", __func__, substream);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200155 if (all_substreams_zero(dev->sub_playback) &&
156 all_substreams_zero(dev->sub_capture)) {
157 /* when the last client has stopped streaming,
158 * all sample rates are allowed again */
159 stream_stop(dev);
160 dev->pcm_info.rates = dev->samplerates;
161 }
Daniel Mack8d048842008-04-14 15:39:14 +0200162
Daniel Mack523f1dc2007-03-26 19:11:24 +0200163 return 0;
164}
165
166static int snd_usb_caiaq_pcm_hw_params(struct snd_pcm_substream *sub,
167 struct snd_pcm_hw_params *hw_params)
168{
Daniel Mack8d048842008-04-14 15:39:14 +0200169 debug("%s(%p)\n", __func__, sub);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200170 return snd_pcm_lib_malloc_pages(sub, params_buffer_bytes(hw_params));
171}
172
173static int snd_usb_caiaq_pcm_hw_free(struct snd_pcm_substream *sub)
174{
175 struct snd_usb_caiaqdev *dev = snd_pcm_substream_chip(sub);
Daniel Mack8d048842008-04-14 15:39:14 +0200176 debug("%s(%p)\n", __func__, sub);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200177 deactivate_substream(dev, sub);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200178 return snd_pcm_lib_free_pages(sub);
179}
180
181/* this should probably go upstream */
182#if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
183#error "Change this table"
184#endif
185
186static unsigned int rates[] = { 5512, 8000, 11025, 16000, 22050, 32000, 44100,
187 48000, 64000, 88200, 96000, 176400, 192000 };
188
189static int snd_usb_caiaq_pcm_prepare(struct snd_pcm_substream *substream)
190{
191 int bytes_per_sample, bpp, ret, i;
192 int index = substream->number;
193 struct snd_usb_caiaqdev *dev = snd_pcm_substream_chip(substream);
194 struct snd_pcm_runtime *runtime = substream->runtime;
195
Daniel Mack8d048842008-04-14 15:39:14 +0200196 debug("%s(%p)\n", __func__, substream);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200197
Daniel Macka9b487f2009-04-27 12:18:05 +0200198 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
199 dev->period_out_count[index] = BYTES_PER_SAMPLE + 1;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200200 dev->audio_out_buf_pos[index] = BYTES_PER_SAMPLE + 1;
Daniel Macka9b487f2009-04-27 12:18:05 +0200201 } else {
202 dev->period_in_count[index] = BYTES_PER_SAMPLE;
Daniel Mack6849d492008-04-14 15:39:47 +0200203 dev->audio_in_buf_pos[index] = BYTES_PER_SAMPLE;
Daniel Macka9b487f2009-04-27 12:18:05 +0200204 }
205
Daniel Mack523f1dc2007-03-26 19:11:24 +0200206 if (dev->streaming)
207 return 0;
208
209 /* the first client that opens a stream defines the sample rate
210 * setting for all subsequent calls, until the last client closed. */
211 for (i=0; i < ARRAY_SIZE(rates); i++)
212 if (runtime->rate == rates[i])
213 dev->pcm_info.rates = 1 << i;
214
215 snd_pcm_limit_hw_rates(runtime);
216
217 bytes_per_sample = BYTES_PER_SAMPLE;
218 if (dev->spec.data_alignment == 2)
219 bytes_per_sample++;
220
221 bpp = ((runtime->rate / 8000) + CLOCK_DRIFT_TOLERANCE)
222 * bytes_per_sample * CHANNELS_PER_STREAM * dev->n_streams;
Daniel Mack6e9fc6b2008-04-14 15:40:31 +0200223
224 if (bpp > MAX_ENDPOINT_SIZE)
225 bpp = MAX_ENDPOINT_SIZE;
226
Daniel Mack523f1dc2007-03-26 19:11:24 +0200227 ret = snd_usb_caiaq_set_audio_params(dev, runtime->rate,
228 runtime->sample_bits, bpp);
229 if (ret)
230 return ret;
231
232 ret = stream_start(dev);
233 if (ret)
234 return ret;
235
236 dev->output_running = 0;
237 wait_event_timeout(dev->prepare_wait_queue, dev->output_running, HZ);
238 if (!dev->output_running) {
239 stream_stop(dev);
240 return -EPIPE;
241 }
242
243 return 0;
244}
245
246static int snd_usb_caiaq_pcm_trigger(struct snd_pcm_substream *sub, int cmd)
247{
248 struct snd_usb_caiaqdev *dev = snd_pcm_substream_chip(sub);
249
250 switch (cmd) {
251 case SNDRV_PCM_TRIGGER_START:
252 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Daniel Mack523f1dc2007-03-26 19:11:24 +0200253 activate_substream(dev, sub);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200254 break;
255 case SNDRV_PCM_TRIGGER_STOP:
256 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
Daniel Mack523f1dc2007-03-26 19:11:24 +0200257 deactivate_substream(dev, sub);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200258 break;
259 default:
260 return -EINVAL;
261 }
262
263 return 0;
264}
265
266static snd_pcm_uframes_t
267snd_usb_caiaq_pcm_pointer(struct snd_pcm_substream *sub)
268{
269 int index = sub->number;
270 struct snd_usb_caiaqdev *dev = snd_pcm_substream_chip(sub);
271
272 if (dev->input_panic || dev->output_panic)
273 return SNDRV_PCM_POS_XRUN;
274
275 if (sub->stream == SNDRV_PCM_STREAM_PLAYBACK)
276 return bytes_to_frames(sub->runtime,
277 dev->audio_out_buf_pos[index]);
278 else
279 return bytes_to_frames(sub->runtime,
280 dev->audio_in_buf_pos[index]);
281}
282
283/* operators for both playback and capture */
284static struct snd_pcm_ops snd_usb_caiaq_ops = {
285 .open = snd_usb_caiaq_substream_open,
286 .close = snd_usb_caiaq_substream_close,
287 .ioctl = snd_pcm_lib_ioctl,
288 .hw_params = snd_usb_caiaq_pcm_hw_params,
289 .hw_free = snd_usb_caiaq_pcm_hw_free,
290 .prepare = snd_usb_caiaq_pcm_prepare,
291 .trigger = snd_usb_caiaq_pcm_trigger,
292 .pointer = snd_usb_caiaq_pcm_pointer
293};
294
295static void check_for_elapsed_periods(struct snd_usb_caiaqdev *dev,
296 struct snd_pcm_substream **subs)
297{
298 int stream, pb, *cnt;
299 struct snd_pcm_substream *sub;
300
301 for (stream = 0; stream < dev->n_streams; stream++) {
302 sub = subs[stream];
303 if (!sub)
304 continue;
305
Daniel Macka9b487f2009-04-27 12:18:05 +0200306 pb = snd_pcm_lib_period_bytes(sub);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200307 cnt = (sub->stream == SNDRV_PCM_STREAM_PLAYBACK) ?
308 &dev->period_out_count[stream] :
309 &dev->period_in_count[stream];
310
311 if (*cnt >= pb) {
312 snd_pcm_period_elapsed(sub);
313 *cnt %= pb;
314 }
315 }
316}
317
318static void read_in_urb_mode0(struct snd_usb_caiaqdev *dev,
319 const struct urb *urb,
320 const struct usb_iso_packet_descriptor *iso)
321{
322 unsigned char *usb_buf = urb->transfer_buffer + iso->offset;
323 struct snd_pcm_substream *sub;
324 int stream, i;
325
326 if (all_substreams_zero(dev->sub_capture))
327 return;
328
Daniel Mack523f1dc2007-03-26 19:11:24 +0200329 for (i = 0; i < iso->actual_length;) {
330 for (stream = 0; stream < dev->n_streams; stream++, i++) {
331 sub = dev->sub_capture[stream];
332 if (sub) {
333 struct snd_pcm_runtime *rt = sub->runtime;
334 char *audio_buf = rt->dma_area;
335 int sz = frames_to_bytes(rt, rt->buffer_size);
336 audio_buf[dev->audio_in_buf_pos[stream]++]
337 = usb_buf[i];
338 dev->period_in_count[stream]++;
339 if (dev->audio_in_buf_pos[stream] == sz)
340 dev->audio_in_buf_pos[stream] = 0;
341 }
342 }
343 }
Daniel Mack523f1dc2007-03-26 19:11:24 +0200344}
345
346static void read_in_urb_mode2(struct snd_usb_caiaqdev *dev,
347 const struct urb *urb,
348 const struct usb_iso_packet_descriptor *iso)
349{
350 unsigned char *usb_buf = urb->transfer_buffer + iso->offset;
351 unsigned char check_byte;
352 struct snd_pcm_substream *sub;
353 int stream, i;
354
Daniel Mack523f1dc2007-03-26 19:11:24 +0200355 for (i = 0; i < iso->actual_length;) {
356 if (i % (dev->n_streams * BYTES_PER_SAMPLE_USB) == 0) {
357 for (stream = 0;
358 stream < dev->n_streams;
359 stream++, i++) {
360 if (dev->first_packet)
361 continue;
362
363 check_byte = MAKE_CHECKBYTE(dev, stream, i);
364
365 if ((usb_buf[i] & 0x3f) != check_byte)
366 dev->input_panic = 1;
367
368 if (usb_buf[i] & 0x80)
369 dev->output_panic = 1;
370 }
371 }
372 dev->first_packet = 0;
373
374 for (stream = 0; stream < dev->n_streams; stream++, i++) {
375 sub = dev->sub_capture[stream];
Daniel Mack9311c9b2009-03-18 11:03:54 +0100376 if (dev->input_panic)
377 usb_buf[i] = 0;
378
Daniel Mack523f1dc2007-03-26 19:11:24 +0200379 if (sub) {
380 struct snd_pcm_runtime *rt = sub->runtime;
381 char *audio_buf = rt->dma_area;
382 int sz = frames_to_bytes(rt, rt->buffer_size);
Karsten Wiesea971c3d42007-03-29 17:02:45 +0200383 audio_buf[dev->audio_in_buf_pos[stream]++] =
384 usb_buf[i];
Daniel Mack523f1dc2007-03-26 19:11:24 +0200385 dev->period_in_count[stream]++;
386 if (dev->audio_in_buf_pos[stream] == sz)
387 dev->audio_in_buf_pos[stream] = 0;
388 }
389 }
390 }
Daniel Mack523f1dc2007-03-26 19:11:24 +0200391}
392
393static void read_in_urb(struct snd_usb_caiaqdev *dev,
394 const struct urb *urb,
395 const struct usb_iso_packet_descriptor *iso)
396{
397 if (!dev->streaming)
398 return;
399
Daniel Mack9311c9b2009-03-18 11:03:54 +0100400 if (iso->actual_length < dev->bpp)
401 return;
402
Daniel Mack523f1dc2007-03-26 19:11:24 +0200403 switch (dev->spec.data_alignment) {
404 case 0:
405 read_in_urb_mode0(dev, urb, iso);
406 break;
407 case 2:
408 read_in_urb_mode2(dev, urb, iso);
409 break;
410 }
411
Daniel Mack1313e702009-03-18 11:03:53 +0100412 if ((dev->input_panic || dev->output_panic) && !dev->warned) {
Daniel Mack523f1dc2007-03-26 19:11:24 +0200413 debug("streaming error detected %s %s\n",
414 dev->input_panic ? "(input)" : "",
415 dev->output_panic ? "(output)" : "");
Daniel Mack1313e702009-03-18 11:03:53 +0100416 dev->warned = 1;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200417 }
Daniel Mack523f1dc2007-03-26 19:11:24 +0200418}
419
420static void fill_out_urb(struct snd_usb_caiaqdev *dev,
421 struct urb *urb,
422 const struct usb_iso_packet_descriptor *iso)
423{
424 unsigned char *usb_buf = urb->transfer_buffer + iso->offset;
425 struct snd_pcm_substream *sub;
426 int stream, i;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200427
428 for (i = 0; i < iso->length;) {
Karsten Wiesea971c3d42007-03-29 17:02:45 +0200429 for (stream = 0; stream < dev->n_streams; stream++, i++) {
Daniel Mack523f1dc2007-03-26 19:11:24 +0200430 sub = dev->sub_playback[stream];
431 if (sub) {
432 struct snd_pcm_runtime *rt = sub->runtime;
433 char *audio_buf = rt->dma_area;
434 int sz = frames_to_bytes(rt, rt->buffer_size);
Karsten Wiesea971c3d42007-03-29 17:02:45 +0200435 usb_buf[i] =
436 audio_buf[dev->audio_out_buf_pos[stream]];
437 dev->period_out_count[stream]++;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200438 dev->audio_out_buf_pos[stream]++;
439 if (dev->audio_out_buf_pos[stream] == sz)
440 dev->audio_out_buf_pos[stream] = 0;
441 } else
Karsten Wiesea971c3d42007-03-29 17:02:45 +0200442 usb_buf[i] = 0;
443 }
Daniel Mack523f1dc2007-03-26 19:11:24 +0200444
445 /* fill in the check bytes */
446 if (dev->spec.data_alignment == 2 &&
447 i % (dev->n_streams * BYTES_PER_SAMPLE_USB) ==
448 (dev->n_streams * CHANNELS_PER_STREAM))
449 for (stream = 0; stream < dev->n_streams; stream++, i++)
450 usb_buf[i] = MAKE_CHECKBYTE(dev, stream, i);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200451 }
Daniel Mack523f1dc2007-03-26 19:11:24 +0200452}
453
454static void read_completed(struct urb *urb)
455{
456 struct snd_usb_caiaq_cb_info *info = urb->context;
457 struct snd_usb_caiaqdev *dev;
458 struct urb *out;
459 int frame, len, send_it = 0, outframe = 0;
460
461 if (urb->status || !info)
462 return;
463
464 dev = info->dev;
Daniel Mack8d048842008-04-14 15:39:14 +0200465
Daniel Mack523f1dc2007-03-26 19:11:24 +0200466 if (!dev->streaming)
467 return;
468
469 out = dev->data_urbs_out[info->index];
470
471 /* read the recently received packet and send back one which has
472 * the same layout */
473 for (frame = 0; frame < FRAMES_PER_URB; frame++) {
474 if (urb->iso_frame_desc[frame].status)
475 continue;
476
477 len = urb->iso_frame_desc[outframe].actual_length;
478 out->iso_frame_desc[outframe].length = len;
479 out->iso_frame_desc[outframe].actual_length = 0;
480 out->iso_frame_desc[outframe].offset = BYTES_PER_FRAME * frame;
481
482 if (len > 0) {
Daniel Mack8d048842008-04-14 15:39:14 +0200483 spin_lock(&dev->spinlock);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200484 fill_out_urb(dev, out, &out->iso_frame_desc[outframe]);
485 read_in_urb(dev, urb, &urb->iso_frame_desc[frame]);
Daniel Mack8d048842008-04-14 15:39:14 +0200486 spin_unlock(&dev->spinlock);
487 check_for_elapsed_periods(dev, dev->sub_playback);
488 check_for_elapsed_periods(dev, dev->sub_capture);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200489 send_it = 1;
490 }
491
492 outframe++;
493 }
494
495 if (send_it) {
496 out->number_of_packets = FRAMES_PER_URB;
497 out->transfer_flags = URB_ISO_ASAP;
498 usb_submit_urb(out, GFP_ATOMIC);
499 }
500
501 /* re-submit inbound urb */
502 for (frame = 0; frame < FRAMES_PER_URB; frame++) {
503 urb->iso_frame_desc[frame].offset = BYTES_PER_FRAME * frame;
504 urb->iso_frame_desc[frame].length = BYTES_PER_FRAME;
505 urb->iso_frame_desc[frame].actual_length = 0;
506 }
507
508 urb->number_of_packets = FRAMES_PER_URB;
509 urb->transfer_flags = URB_ISO_ASAP;
510 usb_submit_urb(urb, GFP_ATOMIC);
511}
512
513static void write_completed(struct urb *urb)
514{
515 struct snd_usb_caiaq_cb_info *info = urb->context;
516 struct snd_usb_caiaqdev *dev = info->dev;
517
518 if (!dev->output_running) {
519 dev->output_running = 1;
520 wake_up(&dev->prepare_wait_queue);
521 }
522}
523
524static struct urb **alloc_urbs(struct snd_usb_caiaqdev *dev, int dir, int *ret)
525{
526 int i, frame;
527 struct urb **urbs;
528 struct usb_device *usb_dev = dev->chip.dev;
529 unsigned int pipe;
530
531 pipe = (dir == SNDRV_PCM_STREAM_PLAYBACK) ?
532 usb_sndisocpipe(usb_dev, ENDPOINT_PLAYBACK) :
533 usb_rcvisocpipe(usb_dev, ENDPOINT_CAPTURE);
534
535 urbs = kmalloc(N_URBS * sizeof(*urbs), GFP_KERNEL);
536 if (!urbs) {
537 log("unable to kmalloc() urbs, OOM!?\n");
538 *ret = -ENOMEM;
539 return NULL;
540 }
541
542 for (i = 0; i < N_URBS; i++) {
543 urbs[i] = usb_alloc_urb(FRAMES_PER_URB, GFP_KERNEL);
544 if (!urbs[i]) {
545 log("unable to usb_alloc_urb(), OOM!?\n");
546 *ret = -ENOMEM;
547 return urbs;
548 }
549
550 urbs[i]->transfer_buffer =
551 kmalloc(FRAMES_PER_URB * BYTES_PER_FRAME, GFP_KERNEL);
552 if (!urbs[i]->transfer_buffer) {
553 log("unable to kmalloc() transfer buffer, OOM!?\n");
554 *ret = -ENOMEM;
555 return urbs;
556 }
557
558 for (frame = 0; frame < FRAMES_PER_URB; frame++) {
559 struct usb_iso_packet_descriptor *iso =
560 &urbs[i]->iso_frame_desc[frame];
561
562 iso->offset = BYTES_PER_FRAME * frame;
563 iso->length = BYTES_PER_FRAME;
564 }
565
566 urbs[i]->dev = usb_dev;
567 urbs[i]->pipe = pipe;
568 urbs[i]->transfer_buffer_length = FRAMES_PER_URB
569 * BYTES_PER_FRAME;
570 urbs[i]->context = &dev->data_cb_info[i];
571 urbs[i]->interval = 1;
572 urbs[i]->transfer_flags = URB_ISO_ASAP;
573 urbs[i]->number_of_packets = FRAMES_PER_URB;
574 urbs[i]->complete = (dir == SNDRV_PCM_STREAM_CAPTURE) ?
575 read_completed : write_completed;
576 }
577
578 *ret = 0;
579 return urbs;
580}
581
582static void free_urbs(struct urb **urbs)
583{
584 int i;
585
586 if (!urbs)
587 return;
588
589 for (i = 0; i < N_URBS; i++) {
590 if (!urbs[i])
591 continue;
592
593 usb_kill_urb(urbs[i]);
594 kfree(urbs[i]->transfer_buffer);
595 usb_free_urb(urbs[i]);
596 }
597
598 kfree(urbs);
599}
600
Randy Dunlap599c3e72008-01-16 14:56:04 +0100601int snd_usb_caiaq_audio_init(struct snd_usb_caiaqdev *dev)
Daniel Mack523f1dc2007-03-26 19:11:24 +0200602{
603 int i, ret;
604
605 dev->n_audio_in = max(dev->spec.num_analog_audio_in,
606 dev->spec.num_digital_audio_in) /
607 CHANNELS_PER_STREAM;
608 dev->n_audio_out = max(dev->spec.num_analog_audio_out,
609 dev->spec.num_digital_audio_out) /
610 CHANNELS_PER_STREAM;
611 dev->n_streams = max(dev->n_audio_in, dev->n_audio_out);
612
613 debug("dev->n_audio_in = %d\n", dev->n_audio_in);
614 debug("dev->n_audio_out = %d\n", dev->n_audio_out);
615 debug("dev->n_streams = %d\n", dev->n_streams);
616
617 if (dev->n_streams > MAX_STREAMS) {
618 log("unable to initialize device, too many streams.\n");
619 return -EINVAL;
620 }
621
622 ret = snd_pcm_new(dev->chip.card, dev->product_name, 0,
623 dev->n_audio_out, dev->n_audio_in, &dev->pcm);
624
625 if (ret < 0) {
626 log("snd_pcm_new() returned %d\n", ret);
627 return ret;
628 }
629
630 dev->pcm->private_data = dev;
631 strcpy(dev->pcm->name, dev->product_name);
632
633 memset(dev->sub_playback, 0, sizeof(dev->sub_playback));
634 memset(dev->sub_capture, 0, sizeof(dev->sub_capture));
635
636 memcpy(&dev->pcm_info, &snd_usb_caiaq_pcm_hardware,
637 sizeof(snd_usb_caiaq_pcm_hardware));
638
639 /* setup samplerates */
640 dev->samplerates = dev->pcm_info.rates;
641 switch (dev->chip.usb_id) {
642 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AK1):
Daniel Mackad1e34b2007-09-17 14:45:14 +0200643 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_RIGKONTROL3):
Daniel Mackf3e9d5d2008-05-08 15:42:15 +0200644 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_SESSIONIO):
Daniel Mack21655922009-01-16 11:03:19 +0100645 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_GUITARRIGMOBILE):
Daniel Mack523f1dc2007-03-26 19:11:24 +0200646 dev->samplerates |= SNDRV_PCM_RATE_192000;
Daniel Mack21655922009-01-16 11:03:19 +0100647 /* fall thru */
648 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AUDIO4DJ):
Daniel Mack523f1dc2007-03-26 19:11:24 +0200649 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AUDIO8DJ):
650 dev->samplerates |= SNDRV_PCM_RATE_88200;
651 break;
652 }
653
654 snd_pcm_set_ops(dev->pcm, SNDRV_PCM_STREAM_PLAYBACK,
655 &snd_usb_caiaq_ops);
656 snd_pcm_set_ops(dev->pcm, SNDRV_PCM_STREAM_CAPTURE,
657 &snd_usb_caiaq_ops);
658
659 snd_pcm_lib_preallocate_pages_for_all(dev->pcm,
660 SNDRV_DMA_TYPE_CONTINUOUS,
661 snd_dma_continuous_data(GFP_KERNEL),
662 MAX_BUFFER_SIZE, MAX_BUFFER_SIZE);
663
664 dev->data_cb_info =
665 kmalloc(sizeof(struct snd_usb_caiaq_cb_info) * N_URBS,
666 GFP_KERNEL);
667
668 if (!dev->data_cb_info)
669 return -ENOMEM;
670
671 for (i = 0; i < N_URBS; i++) {
672 dev->data_cb_info[i].dev = dev;
673 dev->data_cb_info[i].index = i;
674 }
675
676 dev->data_urbs_in = alloc_urbs(dev, SNDRV_PCM_STREAM_CAPTURE, &ret);
677 if (ret < 0) {
678 kfree(dev->data_cb_info);
679 free_urbs(dev->data_urbs_in);
680 return ret;
681 }
682
683 dev->data_urbs_out = alloc_urbs(dev, SNDRV_PCM_STREAM_PLAYBACK, &ret);
684 if (ret < 0) {
685 kfree(dev->data_cb_info);
686 free_urbs(dev->data_urbs_in);
687 free_urbs(dev->data_urbs_out);
688 return ret;
689 }
690
691 return 0;
692}
693
694void snd_usb_caiaq_audio_free(struct snd_usb_caiaqdev *dev)
695{
Daniel Mack8d048842008-04-14 15:39:14 +0200696 debug("%s(%p)\n", __func__, dev);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200697 stream_stop(dev);
698 free_urbs(dev->data_urbs_in);
699 free_urbs(dev->data_urbs_out);
700 kfree(dev->data_cb_info);
701}
702