blob: 08d51e0c9feae466175f87cb45fe87cebbcd13ec [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/init.h>
20#include <linux/module.h>
21#include <linux/moduleparam.h>
22#include <linux/interrupt.h>
23#include <linux/usb.h>
24#include <linux/spinlock.h>
25#include <sound/core.h>
26#include <sound/initval.h>
27#include <sound/pcm.h>
28#include <sound/rawmidi.h>
Daniel Mack523f1dc2007-03-26 19:11:24 +020029#include <linux/input.h>
Daniel Mack523f1dc2007-03-26 19:11:24 +020030
31#include "caiaq-device.h"
32#include "caiaq-audio.h"
33
34#define N_URBS 32
35#define CLOCK_DRIFT_TOLERANCE 5
36#define FRAMES_PER_URB 8
37#define BYTES_PER_FRAME 512
38#define CHANNELS_PER_STREAM 2
39#define BYTES_PER_SAMPLE 3
40#define BYTES_PER_SAMPLE_USB 4
41#define MAX_BUFFER_SIZE (128*1024)
Daniel Mack6e9fc6b2008-04-14 15:40:31 +020042#define MAX_ENDPOINT_SIZE 512
43
Daniel Mack523f1dc2007-03-26 19:11:24 +020044#define ENDPOINT_CAPTURE 2
45#define ENDPOINT_PLAYBACK 6
46
47#define MAKE_CHECKBYTE(dev,stream,i) \
48 (stream << 1) | (~(i / (dev->n_streams * BYTES_PER_SAMPLE_USB)) & 1)
49
50static struct snd_pcm_hardware snd_usb_caiaq_pcm_hardware = {
51 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
52 SNDRV_PCM_INFO_BLOCK_TRANSFER),
53 .formats = SNDRV_PCM_FMTBIT_S24_3BE,
54 .rates = (SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |
55 SNDRV_PCM_RATE_96000),
56 .rate_min = 44100,
57 .rate_max = 0, /* will overwrite later */
58 .channels_min = CHANNELS_PER_STREAM,
59 .channels_max = CHANNELS_PER_STREAM,
60 .buffer_bytes_max = MAX_BUFFER_SIZE,
Daniel Mack09189ac2008-01-24 18:46:42 +010061 .period_bytes_min = 128,
Daniel Mack523f1dc2007-03-26 19:11:24 +020062 .period_bytes_max = MAX_BUFFER_SIZE,
63 .periods_min = 1,
64 .periods_max = 1024,
65};
66
67static void
68activate_substream(struct snd_usb_caiaqdev *dev,
69 struct snd_pcm_substream *sub)
70{
71 if (sub->stream == SNDRV_PCM_STREAM_PLAYBACK)
72 dev->sub_playback[sub->number] = sub;
73 else
74 dev->sub_capture[sub->number] = sub;
75}
76
77static void
78deactivate_substream(struct snd_usb_caiaqdev *dev,
79 struct snd_pcm_substream *sub)
80{
Daniel Mack8d048842008-04-14 15:39:14 +020081 unsigned long flags;
82 spin_lock_irqsave(&dev->spinlock, flags);
83
Daniel Mack523f1dc2007-03-26 19:11:24 +020084 if (sub->stream == SNDRV_PCM_STREAM_PLAYBACK)
85 dev->sub_playback[sub->number] = NULL;
86 else
87 dev->sub_capture[sub->number] = NULL;
Daniel Mack8d048842008-04-14 15:39:14 +020088
89 spin_unlock_irqrestore(&dev->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
102static int stream_start(struct snd_usb_caiaqdev *dev)
103{
104 int i, ret;
105
Daniel Mack8d048842008-04-14 15:39:14 +0200106 debug("%s(%p)\n", __func__, dev);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200107
Daniel Mack8d048842008-04-14 15:39:14 +0200108 if (dev->streaming)
109 return -EINVAL;
110
111 memset(dev->sub_playback, 0, sizeof(dev->sub_playback));
112 memset(dev->sub_capture, 0, sizeof(dev->sub_capture));
Daniel Mack523f1dc2007-03-26 19:11:24 +0200113 dev->input_panic = 0;
114 dev->output_panic = 0;
115 dev->first_packet = 1;
116 dev->streaming = 1;
Daniel Mack1313e702009-03-18 11:03:53 +0100117 dev->warned = 0;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200118
119 for (i = 0; i < N_URBS; i++) {
120 ret = usb_submit_urb(dev->data_urbs_in[i], GFP_ATOMIC);
121 if (ret) {
Daniel Mack8d048842008-04-14 15:39:14 +0200122 log("unable to trigger read #%d! (ret %d)\n", i, ret);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200123 dev->streaming = 0;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200124 return -EPIPE;
125 }
126 }
127
Daniel Mack523f1dc2007-03-26 19:11:24 +0200128 return 0;
129}
130
131static void stream_stop(struct snd_usb_caiaqdev *dev)
132{
133 int i;
Daniel Mack8d048842008-04-14 15:39:14 +0200134
135 debug("%s(%p)\n", __func__, dev);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200136 if (!dev->streaming)
137 return;
138
139 dev->streaming = 0;
Daniel Mack8d048842008-04-14 15:39:14 +0200140
Daniel Mack523f1dc2007-03-26 19:11:24 +0200141 for (i = 0; i < N_URBS; i++) {
Daniel Mack8d048842008-04-14 15:39:14 +0200142 usb_kill_urb(dev->data_urbs_in[i]);
143 usb_kill_urb(dev->data_urbs_out[i]);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200144 }
145}
146
147static int snd_usb_caiaq_substream_open(struct snd_pcm_substream *substream)
148{
149 struct snd_usb_caiaqdev *dev = snd_pcm_substream_chip(substream);
Daniel Mack8d048842008-04-14 15:39:14 +0200150 debug("%s(%p)\n", __func__, substream);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200151 substream->runtime->hw = dev->pcm_info;
152 snd_pcm_limit_hw_rates(substream->runtime);
153 return 0;
154}
155
156static int snd_usb_caiaq_substream_close(struct snd_pcm_substream *substream)
157{
158 struct snd_usb_caiaqdev *dev = snd_pcm_substream_chip(substream);
159
Daniel Mack8d048842008-04-14 15:39:14 +0200160 debug("%s(%p)\n", __func__, substream);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200161 if (all_substreams_zero(dev->sub_playback) &&
162 all_substreams_zero(dev->sub_capture)) {
163 /* when the last client has stopped streaming,
164 * all sample rates are allowed again */
165 stream_stop(dev);
166 dev->pcm_info.rates = dev->samplerates;
167 }
Daniel Mack8d048842008-04-14 15:39:14 +0200168
Daniel Mack523f1dc2007-03-26 19:11:24 +0200169 return 0;
170}
171
172static int snd_usb_caiaq_pcm_hw_params(struct snd_pcm_substream *sub,
173 struct snd_pcm_hw_params *hw_params)
174{
Daniel Mack8d048842008-04-14 15:39:14 +0200175 debug("%s(%p)\n", __func__, sub);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200176 return snd_pcm_lib_malloc_pages(sub, params_buffer_bytes(hw_params));
177}
178
179static int snd_usb_caiaq_pcm_hw_free(struct snd_pcm_substream *sub)
180{
181 struct snd_usb_caiaqdev *dev = snd_pcm_substream_chip(sub);
Daniel Mack8d048842008-04-14 15:39:14 +0200182 debug("%s(%p)\n", __func__, sub);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200183 deactivate_substream(dev, sub);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200184 return snd_pcm_lib_free_pages(sub);
185}
186
187/* this should probably go upstream */
188#if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
189#error "Change this table"
190#endif
191
192static unsigned int rates[] = { 5512, 8000, 11025, 16000, 22050, 32000, 44100,
193 48000, 64000, 88200, 96000, 176400, 192000 };
194
195static int snd_usb_caiaq_pcm_prepare(struct snd_pcm_substream *substream)
196{
197 int bytes_per_sample, bpp, ret, i;
198 int index = substream->number;
199 struct snd_usb_caiaqdev *dev = snd_pcm_substream_chip(substream);
200 struct snd_pcm_runtime *runtime = substream->runtime;
201
Daniel Mack8d048842008-04-14 15:39:14 +0200202 debug("%s(%p)\n", __func__, substream);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200203
204 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
205 dev->audio_out_buf_pos[index] = BYTES_PER_SAMPLE + 1;
206 else
Daniel Mack6849d492008-04-14 15:39:47 +0200207 dev->audio_in_buf_pos[index] = BYTES_PER_SAMPLE;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200208
209 if (dev->streaming)
210 return 0;
211
212 /* the first client that opens a stream defines the sample rate
213 * setting for all subsequent calls, until the last client closed. */
214 for (i=0; i < ARRAY_SIZE(rates); i++)
215 if (runtime->rate == rates[i])
216 dev->pcm_info.rates = 1 << i;
217
218 snd_pcm_limit_hw_rates(runtime);
219
220 bytes_per_sample = BYTES_PER_SAMPLE;
221 if (dev->spec.data_alignment == 2)
222 bytes_per_sample++;
223
224 bpp = ((runtime->rate / 8000) + CLOCK_DRIFT_TOLERANCE)
225 * bytes_per_sample * CHANNELS_PER_STREAM * dev->n_streams;
Daniel Mack6e9fc6b2008-04-14 15:40:31 +0200226
227 if (bpp > MAX_ENDPOINT_SIZE)
228 bpp = MAX_ENDPOINT_SIZE;
229
Daniel Mack523f1dc2007-03-26 19:11:24 +0200230 ret = snd_usb_caiaq_set_audio_params(dev, runtime->rate,
231 runtime->sample_bits, bpp);
232 if (ret)
233 return ret;
234
235 ret = stream_start(dev);
236 if (ret)
237 return ret;
238
239 dev->output_running = 0;
240 wait_event_timeout(dev->prepare_wait_queue, dev->output_running, HZ);
241 if (!dev->output_running) {
242 stream_stop(dev);
243 return -EPIPE;
244 }
245
246 return 0;
247}
248
249static int snd_usb_caiaq_pcm_trigger(struct snd_pcm_substream *sub, int cmd)
250{
251 struct snd_usb_caiaqdev *dev = snd_pcm_substream_chip(sub);
252
253 switch (cmd) {
254 case SNDRV_PCM_TRIGGER_START:
255 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Daniel Mack523f1dc2007-03-26 19:11:24 +0200256 activate_substream(dev, sub);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200257 break;
258 case SNDRV_PCM_TRIGGER_STOP:
259 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
Daniel Mack523f1dc2007-03-26 19:11:24 +0200260 deactivate_substream(dev, sub);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200261 break;
262 default:
263 return -EINVAL;
264 }
265
266 return 0;
267}
268
269static snd_pcm_uframes_t
270snd_usb_caiaq_pcm_pointer(struct snd_pcm_substream *sub)
271{
272 int index = sub->number;
273 struct snd_usb_caiaqdev *dev = snd_pcm_substream_chip(sub);
274
275 if (dev->input_panic || dev->output_panic)
276 return SNDRV_PCM_POS_XRUN;
277
278 if (sub->stream == SNDRV_PCM_STREAM_PLAYBACK)
279 return bytes_to_frames(sub->runtime,
280 dev->audio_out_buf_pos[index]);
281 else
282 return bytes_to_frames(sub->runtime,
283 dev->audio_in_buf_pos[index]);
284}
285
286/* operators for both playback and capture */
287static struct snd_pcm_ops snd_usb_caiaq_ops = {
288 .open = snd_usb_caiaq_substream_open,
289 .close = snd_usb_caiaq_substream_close,
290 .ioctl = snd_pcm_lib_ioctl,
291 .hw_params = snd_usb_caiaq_pcm_hw_params,
292 .hw_free = snd_usb_caiaq_pcm_hw_free,
293 .prepare = snd_usb_caiaq_pcm_prepare,
294 .trigger = snd_usb_caiaq_pcm_trigger,
295 .pointer = snd_usb_caiaq_pcm_pointer
296};
297
298static void check_for_elapsed_periods(struct snd_usb_caiaqdev *dev,
299 struct snd_pcm_substream **subs)
300{
301 int stream, pb, *cnt;
302 struct snd_pcm_substream *sub;
303
304 for (stream = 0; stream < dev->n_streams; stream++) {
305 sub = subs[stream];
306 if (!sub)
307 continue;
308
309 pb = frames_to_bytes(sub->runtime,
310 sub->runtime->period_size);
311 cnt = (sub->stream == SNDRV_PCM_STREAM_PLAYBACK) ?
312 &dev->period_out_count[stream] :
313 &dev->period_in_count[stream];
314
315 if (*cnt >= pb) {
316 snd_pcm_period_elapsed(sub);
317 *cnt %= pb;
318 }
319 }
320}
321
322static void read_in_urb_mode0(struct snd_usb_caiaqdev *dev,
323 const struct urb *urb,
324 const struct usb_iso_packet_descriptor *iso)
325{
326 unsigned char *usb_buf = urb->transfer_buffer + iso->offset;
327 struct snd_pcm_substream *sub;
328 int stream, i;
329
330 if (all_substreams_zero(dev->sub_capture))
331 return;
332
Daniel Mack523f1dc2007-03-26 19:11:24 +0200333 for (i = 0; i < iso->actual_length;) {
334 for (stream = 0; stream < dev->n_streams; stream++, i++) {
335 sub = dev->sub_capture[stream];
336 if (sub) {
337 struct snd_pcm_runtime *rt = sub->runtime;
338 char *audio_buf = rt->dma_area;
339 int sz = frames_to_bytes(rt, rt->buffer_size);
340 audio_buf[dev->audio_in_buf_pos[stream]++]
341 = usb_buf[i];
342 dev->period_in_count[stream]++;
343 if (dev->audio_in_buf_pos[stream] == sz)
344 dev->audio_in_buf_pos[stream] = 0;
345 }
346 }
347 }
Daniel Mack523f1dc2007-03-26 19:11:24 +0200348}
349
350static void read_in_urb_mode2(struct snd_usb_caiaqdev *dev,
351 const struct urb *urb,
352 const struct usb_iso_packet_descriptor *iso)
353{
354 unsigned char *usb_buf = urb->transfer_buffer + iso->offset;
355 unsigned char check_byte;
356 struct snd_pcm_substream *sub;
357 int stream, i;
358
Daniel Mack523f1dc2007-03-26 19:11:24 +0200359 for (i = 0; i < iso->actual_length;) {
360 if (i % (dev->n_streams * BYTES_PER_SAMPLE_USB) == 0) {
361 for (stream = 0;
362 stream < dev->n_streams;
363 stream++, i++) {
364 if (dev->first_packet)
365 continue;
366
367 check_byte = MAKE_CHECKBYTE(dev, stream, i);
368
369 if ((usb_buf[i] & 0x3f) != check_byte)
370 dev->input_panic = 1;
371
372 if (usb_buf[i] & 0x80)
373 dev->output_panic = 1;
374 }
375 }
376 dev->first_packet = 0;
377
378 for (stream = 0; stream < dev->n_streams; stream++, i++) {
379 sub = dev->sub_capture[stream];
Daniel Mack9311c9b2009-03-18 11:03:54 +0100380 if (dev->input_panic)
381 usb_buf[i] = 0;
382
Daniel Mack523f1dc2007-03-26 19:11:24 +0200383 if (sub) {
384 struct snd_pcm_runtime *rt = sub->runtime;
385 char *audio_buf = rt->dma_area;
386 int sz = frames_to_bytes(rt, rt->buffer_size);
Karsten Wiesea971c3d42007-03-29 17:02:45 +0200387 audio_buf[dev->audio_in_buf_pos[stream]++] =
388 usb_buf[i];
Daniel Mack523f1dc2007-03-26 19:11:24 +0200389 dev->period_in_count[stream]++;
390 if (dev->audio_in_buf_pos[stream] == sz)
391 dev->audio_in_buf_pos[stream] = 0;
392 }
393 }
394 }
Daniel Mack523f1dc2007-03-26 19:11:24 +0200395}
396
397static void read_in_urb(struct snd_usb_caiaqdev *dev,
398 const struct urb *urb,
399 const struct usb_iso_packet_descriptor *iso)
400{
401 if (!dev->streaming)
402 return;
403
Daniel Mack9311c9b2009-03-18 11:03:54 +0100404 if (iso->actual_length < dev->bpp)
405 return;
406
Daniel Mack523f1dc2007-03-26 19:11:24 +0200407 switch (dev->spec.data_alignment) {
408 case 0:
409 read_in_urb_mode0(dev, urb, iso);
410 break;
411 case 2:
412 read_in_urb_mode2(dev, urb, iso);
413 break;
414 }
415
Daniel Mack1313e702009-03-18 11:03:53 +0100416 if ((dev->input_panic || dev->output_panic) && !dev->warned) {
Daniel Mack523f1dc2007-03-26 19:11:24 +0200417 debug("streaming error detected %s %s\n",
418 dev->input_panic ? "(input)" : "",
419 dev->output_panic ? "(output)" : "");
Daniel Mack1313e702009-03-18 11:03:53 +0100420 dev->warned = 1;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200421 }
Daniel Mack523f1dc2007-03-26 19:11:24 +0200422}
423
424static void fill_out_urb(struct snd_usb_caiaqdev *dev,
425 struct urb *urb,
426 const struct usb_iso_packet_descriptor *iso)
427{
428 unsigned char *usb_buf = urb->transfer_buffer + iso->offset;
429 struct snd_pcm_substream *sub;
430 int stream, i;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200431
432 for (i = 0; i < iso->length;) {
Karsten Wiesea971c3d42007-03-29 17:02:45 +0200433 for (stream = 0; stream < dev->n_streams; stream++, i++) {
Daniel Mack523f1dc2007-03-26 19:11:24 +0200434 sub = dev->sub_playback[stream];
435 if (sub) {
436 struct snd_pcm_runtime *rt = sub->runtime;
437 char *audio_buf = rt->dma_area;
438 int sz = frames_to_bytes(rt, rt->buffer_size);
Karsten Wiesea971c3d42007-03-29 17:02:45 +0200439 usb_buf[i] =
440 audio_buf[dev->audio_out_buf_pos[stream]];
441 dev->period_out_count[stream]++;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200442 dev->audio_out_buf_pos[stream]++;
443 if (dev->audio_out_buf_pos[stream] == sz)
444 dev->audio_out_buf_pos[stream] = 0;
445 } else
Karsten Wiesea971c3d42007-03-29 17:02:45 +0200446 usb_buf[i] = 0;
447 }
Daniel Mack523f1dc2007-03-26 19:11:24 +0200448
449 /* fill in the check bytes */
450 if (dev->spec.data_alignment == 2 &&
451 i % (dev->n_streams * BYTES_PER_SAMPLE_USB) ==
452 (dev->n_streams * CHANNELS_PER_STREAM))
453 for (stream = 0; stream < dev->n_streams; stream++, i++)
454 usb_buf[i] = MAKE_CHECKBYTE(dev, stream, i);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200455 }
Daniel Mack523f1dc2007-03-26 19:11:24 +0200456}
457
458static void read_completed(struct urb *urb)
459{
460 struct snd_usb_caiaq_cb_info *info = urb->context;
461 struct snd_usb_caiaqdev *dev;
462 struct urb *out;
463 int frame, len, send_it = 0, outframe = 0;
464
465 if (urb->status || !info)
466 return;
467
468 dev = info->dev;
Daniel Mack8d048842008-04-14 15:39:14 +0200469
Daniel Mack523f1dc2007-03-26 19:11:24 +0200470 if (!dev->streaming)
471 return;
472
473 out = dev->data_urbs_out[info->index];
474
475 /* read the recently received packet and send back one which has
476 * the same layout */
477 for (frame = 0; frame < FRAMES_PER_URB; frame++) {
478 if (urb->iso_frame_desc[frame].status)
479 continue;
480
481 len = urb->iso_frame_desc[outframe].actual_length;
482 out->iso_frame_desc[outframe].length = len;
483 out->iso_frame_desc[outframe].actual_length = 0;
484 out->iso_frame_desc[outframe].offset = BYTES_PER_FRAME * frame;
485
486 if (len > 0) {
Daniel Mack8d048842008-04-14 15:39:14 +0200487 spin_lock(&dev->spinlock);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200488 fill_out_urb(dev, out, &out->iso_frame_desc[outframe]);
489 read_in_urb(dev, urb, &urb->iso_frame_desc[frame]);
Daniel Mack8d048842008-04-14 15:39:14 +0200490 spin_unlock(&dev->spinlock);
491 check_for_elapsed_periods(dev, dev->sub_playback);
492 check_for_elapsed_periods(dev, dev->sub_capture);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200493 send_it = 1;
494 }
495
496 outframe++;
497 }
498
499 if (send_it) {
500 out->number_of_packets = FRAMES_PER_URB;
501 out->transfer_flags = URB_ISO_ASAP;
502 usb_submit_urb(out, GFP_ATOMIC);
503 }
504
505 /* re-submit inbound urb */
506 for (frame = 0; frame < FRAMES_PER_URB; frame++) {
507 urb->iso_frame_desc[frame].offset = BYTES_PER_FRAME * frame;
508 urb->iso_frame_desc[frame].length = BYTES_PER_FRAME;
509 urb->iso_frame_desc[frame].actual_length = 0;
510 }
511
512 urb->number_of_packets = FRAMES_PER_URB;
513 urb->transfer_flags = URB_ISO_ASAP;
514 usb_submit_urb(urb, GFP_ATOMIC);
515}
516
517static void write_completed(struct urb *urb)
518{
519 struct snd_usb_caiaq_cb_info *info = urb->context;
520 struct snd_usb_caiaqdev *dev = info->dev;
521
522 if (!dev->output_running) {
523 dev->output_running = 1;
524 wake_up(&dev->prepare_wait_queue);
525 }
526}
527
528static struct urb **alloc_urbs(struct snd_usb_caiaqdev *dev, int dir, int *ret)
529{
530 int i, frame;
531 struct urb **urbs;
532 struct usb_device *usb_dev = dev->chip.dev;
533 unsigned int pipe;
534
535 pipe = (dir == SNDRV_PCM_STREAM_PLAYBACK) ?
536 usb_sndisocpipe(usb_dev, ENDPOINT_PLAYBACK) :
537 usb_rcvisocpipe(usb_dev, ENDPOINT_CAPTURE);
538
539 urbs = kmalloc(N_URBS * sizeof(*urbs), GFP_KERNEL);
540 if (!urbs) {
541 log("unable to kmalloc() urbs, OOM!?\n");
542 *ret = -ENOMEM;
543 return NULL;
544 }
545
546 for (i = 0; i < N_URBS; i++) {
547 urbs[i] = usb_alloc_urb(FRAMES_PER_URB, GFP_KERNEL);
548 if (!urbs[i]) {
549 log("unable to usb_alloc_urb(), OOM!?\n");
550 *ret = -ENOMEM;
551 return urbs;
552 }
553
554 urbs[i]->transfer_buffer =
555 kmalloc(FRAMES_PER_URB * BYTES_PER_FRAME, GFP_KERNEL);
556 if (!urbs[i]->transfer_buffer) {
557 log("unable to kmalloc() transfer buffer, OOM!?\n");
558 *ret = -ENOMEM;
559 return urbs;
560 }
561
562 for (frame = 0; frame < FRAMES_PER_URB; frame++) {
563 struct usb_iso_packet_descriptor *iso =
564 &urbs[i]->iso_frame_desc[frame];
565
566 iso->offset = BYTES_PER_FRAME * frame;
567 iso->length = BYTES_PER_FRAME;
568 }
569
570 urbs[i]->dev = usb_dev;
571 urbs[i]->pipe = pipe;
572 urbs[i]->transfer_buffer_length = FRAMES_PER_URB
573 * BYTES_PER_FRAME;
574 urbs[i]->context = &dev->data_cb_info[i];
575 urbs[i]->interval = 1;
576 urbs[i]->transfer_flags = URB_ISO_ASAP;
577 urbs[i]->number_of_packets = FRAMES_PER_URB;
578 urbs[i]->complete = (dir == SNDRV_PCM_STREAM_CAPTURE) ?
579 read_completed : write_completed;
580 }
581
582 *ret = 0;
583 return urbs;
584}
585
586static void free_urbs(struct urb **urbs)
587{
588 int i;
589
590 if (!urbs)
591 return;
592
593 for (i = 0; i < N_URBS; i++) {
594 if (!urbs[i])
595 continue;
596
597 usb_kill_urb(urbs[i]);
598 kfree(urbs[i]->transfer_buffer);
599 usb_free_urb(urbs[i]);
600 }
601
602 kfree(urbs);
603}
604
Randy Dunlap599c3e72008-01-16 14:56:04 +0100605int snd_usb_caiaq_audio_init(struct snd_usb_caiaqdev *dev)
Daniel Mack523f1dc2007-03-26 19:11:24 +0200606{
607 int i, ret;
608
609 dev->n_audio_in = max(dev->spec.num_analog_audio_in,
610 dev->spec.num_digital_audio_in) /
611 CHANNELS_PER_STREAM;
612 dev->n_audio_out = max(dev->spec.num_analog_audio_out,
613 dev->spec.num_digital_audio_out) /
614 CHANNELS_PER_STREAM;
615 dev->n_streams = max(dev->n_audio_in, dev->n_audio_out);
616
617 debug("dev->n_audio_in = %d\n", dev->n_audio_in);
618 debug("dev->n_audio_out = %d\n", dev->n_audio_out);
619 debug("dev->n_streams = %d\n", dev->n_streams);
620
621 if (dev->n_streams > MAX_STREAMS) {
622 log("unable to initialize device, too many streams.\n");
623 return -EINVAL;
624 }
625
626 ret = snd_pcm_new(dev->chip.card, dev->product_name, 0,
627 dev->n_audio_out, dev->n_audio_in, &dev->pcm);
628
629 if (ret < 0) {
630 log("snd_pcm_new() returned %d\n", ret);
631 return ret;
632 }
633
634 dev->pcm->private_data = dev;
635 strcpy(dev->pcm->name, dev->product_name);
636
637 memset(dev->sub_playback, 0, sizeof(dev->sub_playback));
638 memset(dev->sub_capture, 0, sizeof(dev->sub_capture));
639
640 memcpy(&dev->pcm_info, &snd_usb_caiaq_pcm_hardware,
641 sizeof(snd_usb_caiaq_pcm_hardware));
642
643 /* setup samplerates */
644 dev->samplerates = dev->pcm_info.rates;
645 switch (dev->chip.usb_id) {
646 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AK1):
Daniel Mackad1e34b2007-09-17 14:45:14 +0200647 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_RIGKONTROL3):
Daniel Mackf3e9d5d2008-05-08 15:42:15 +0200648 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_SESSIONIO):
Daniel Mack21655922009-01-16 11:03:19 +0100649 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_GUITARRIGMOBILE):
Daniel Mack523f1dc2007-03-26 19:11:24 +0200650 dev->samplerates |= SNDRV_PCM_RATE_192000;
Daniel Mack21655922009-01-16 11:03:19 +0100651 /* fall thru */
652 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AUDIO4DJ):
Daniel Mack523f1dc2007-03-26 19:11:24 +0200653 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AUDIO8DJ):
654 dev->samplerates |= SNDRV_PCM_RATE_88200;
655 break;
656 }
657
658 snd_pcm_set_ops(dev->pcm, SNDRV_PCM_STREAM_PLAYBACK,
659 &snd_usb_caiaq_ops);
660 snd_pcm_set_ops(dev->pcm, SNDRV_PCM_STREAM_CAPTURE,
661 &snd_usb_caiaq_ops);
662
663 snd_pcm_lib_preallocate_pages_for_all(dev->pcm,
664 SNDRV_DMA_TYPE_CONTINUOUS,
665 snd_dma_continuous_data(GFP_KERNEL),
666 MAX_BUFFER_SIZE, MAX_BUFFER_SIZE);
667
668 dev->data_cb_info =
669 kmalloc(sizeof(struct snd_usb_caiaq_cb_info) * N_URBS,
670 GFP_KERNEL);
671
672 if (!dev->data_cb_info)
673 return -ENOMEM;
674
675 for (i = 0; i < N_URBS; i++) {
676 dev->data_cb_info[i].dev = dev;
677 dev->data_cb_info[i].index = i;
678 }
679
680 dev->data_urbs_in = alloc_urbs(dev, SNDRV_PCM_STREAM_CAPTURE, &ret);
681 if (ret < 0) {
682 kfree(dev->data_cb_info);
683 free_urbs(dev->data_urbs_in);
684 return ret;
685 }
686
687 dev->data_urbs_out = alloc_urbs(dev, SNDRV_PCM_STREAM_PLAYBACK, &ret);
688 if (ret < 0) {
689 kfree(dev->data_cb_info);
690 free_urbs(dev->data_urbs_in);
691 free_urbs(dev->data_urbs_out);
692 return ret;
693 }
694
695 return 0;
696}
697
698void snd_usb_caiaq_audio_free(struct snd_usb_caiaqdev *dev)
699{
Daniel Mack8d048842008-04-14 15:39:14 +0200700 debug("%s(%p)\n", __func__, dev);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200701 stream_stop(dev);
702 free_urbs(dev->data_urbs_in);
703 free_urbs(dev->data_urbs_out);
704 kfree(dev->data_cb_info);
705}
706