2 * Copyright (c) 2006,2007 Daniel Mack, Karsten Wiese
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.
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.
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
19 #include <sound/driver.h>
20 #include <linux/init.h>
21 #include <linux/module.h>
22 #include <linux/moduleparam.h>
23 #include <linux/interrupt.h>
24 #include <linux/usb.h>
25 #include <linux/spinlock.h>
26 #include <sound/core.h>
27 #include <sound/initval.h>
28 #include <sound/pcm.h>
29 #include <sound/rawmidi.h>
30 #include <linux/input.h>
32 #include "caiaq-device.h"
33 #include "caiaq-audio.h"
36 #define CLOCK_DRIFT_TOLERANCE 5
37 #define FRAMES_PER_URB 8
38 #define BYTES_PER_FRAME 512
39 #define CHANNELS_PER_STREAM 2
40 #define BYTES_PER_SAMPLE 3
41 #define BYTES_PER_SAMPLE_USB 4
42 #define MAX_BUFFER_SIZE (128*1024)
44 #define ENDPOINT_CAPTURE 2
45 #define ENDPOINT_PLAYBACK 6
47 #define MAKE_CHECKBYTE(dev,stream,i) \
48 (stream << 1) | (~(i / (dev->n_streams * BYTES_PER_SAMPLE_USB)) & 1)
50 static 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),
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,
61 .period_bytes_min = 4096,
62 .period_bytes_max = MAX_BUFFER_SIZE,
68 activate_substream(struct snd_usb_caiaqdev *dev,
69 struct snd_pcm_substream *sub)
71 if (sub->stream == SNDRV_PCM_STREAM_PLAYBACK)
72 dev->sub_playback[sub->number] = sub;
74 dev->sub_capture[sub->number] = sub;
78 deactivate_substream(struct snd_usb_caiaqdev *dev,
79 struct snd_pcm_substream *sub)
81 if (sub->stream == SNDRV_PCM_STREAM_PLAYBACK)
82 dev->sub_playback[sub->number] = NULL;
84 dev->sub_capture[sub->number] = NULL;
88 all_substreams_zero(struct snd_pcm_substream **subs)
91 for (i = 0; i < MAX_STREAMS; i++)
97 static int stream_start(struct snd_usb_caiaqdev *dev)
101 debug("stream_start(%p)\n", dev);
102 spin_lock_irq(&dev->spinlock);
103 if (dev->streaming) {
104 spin_unlock_irq(&dev->spinlock);
108 dev->input_panic = 0;
109 dev->output_panic = 0;
110 dev->first_packet = 1;
113 for (i = 0; i < N_URBS; i++) {
114 ret = usb_submit_urb(dev->data_urbs_in[i], GFP_ATOMIC);
116 log("unable to trigger initial read #%d! (ret = %d)\n",
119 spin_unlock_irq(&dev->spinlock);
124 spin_unlock_irq(&dev->spinlock);
128 static void stream_stop(struct snd_usb_caiaqdev *dev)
132 debug("stream_stop(%p)\n", dev);
137 for (i = 0; i < N_URBS; i++) {
138 usb_unlink_urb(dev->data_urbs_in[i]);
139 usb_unlink_urb(dev->data_urbs_out[i]);
143 static int snd_usb_caiaq_substream_open(struct snd_pcm_substream *substream)
145 struct snd_usb_caiaqdev *dev = snd_pcm_substream_chip(substream);
146 debug("snd_usb_caiaq_substream_open(%p)\n", substream);
147 substream->runtime->hw = dev->pcm_info;
148 snd_pcm_limit_hw_rates(substream->runtime);
152 static int snd_usb_caiaq_substream_close(struct snd_pcm_substream *substream)
154 struct snd_usb_caiaqdev *dev = snd_pcm_substream_chip(substream);
156 debug("snd_usb_caiaq_substream_close(%p)\n", substream);
157 if (all_substreams_zero(dev->sub_playback) &&
158 all_substreams_zero(dev->sub_capture)) {
159 /* when the last client has stopped streaming,
160 * all sample rates are allowed again */
162 dev->pcm_info.rates = dev->samplerates;
168 static int snd_usb_caiaq_pcm_hw_params(struct snd_pcm_substream *sub,
169 struct snd_pcm_hw_params *hw_params)
171 debug("snd_usb_caiaq_pcm_hw_params(%p)\n", sub);
172 return snd_pcm_lib_malloc_pages(sub, params_buffer_bytes(hw_params));
175 static int snd_usb_caiaq_pcm_hw_free(struct snd_pcm_substream *sub)
177 struct snd_usb_caiaqdev *dev = snd_pcm_substream_chip(sub);
178 debug("snd_usb_caiaq_pcm_hw_free(%p)\n", sub);
179 spin_lock_irq(&dev->spinlock);
180 deactivate_substream(dev, sub);
181 spin_unlock_irq(&dev->spinlock);
182 return snd_pcm_lib_free_pages(sub);
185 /* this should probably go upstream */
186 #if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
187 #error "Change this table"
190 static unsigned int rates[] = { 5512, 8000, 11025, 16000, 22050, 32000, 44100,
191 48000, 64000, 88200, 96000, 176400, 192000 };
193 static int snd_usb_caiaq_pcm_prepare(struct snd_pcm_substream *substream)
195 int bytes_per_sample, bpp, ret, i;
196 int index = substream->number;
197 struct snd_usb_caiaqdev *dev = snd_pcm_substream_chip(substream);
198 struct snd_pcm_runtime *runtime = substream->runtime;
200 debug("snd_usb_caiaq_pcm_prepare(%p)\n", substream);
202 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
203 dev->audio_out_buf_pos[index] = BYTES_PER_SAMPLE + 1;
205 dev->audio_in_buf_pos[index] = 0;
210 /* the first client that opens a stream defines the sample rate
211 * setting for all subsequent calls, until the last client closed. */
212 for (i=0; i < ARRAY_SIZE(rates); i++)
213 if (runtime->rate == rates[i])
214 dev->pcm_info.rates = 1 << i;
216 snd_pcm_limit_hw_rates(runtime);
218 bytes_per_sample = BYTES_PER_SAMPLE;
219 if (dev->spec.data_alignment == 2)
222 bpp = ((runtime->rate / 8000) + CLOCK_DRIFT_TOLERANCE)
223 * bytes_per_sample * CHANNELS_PER_STREAM * dev->n_streams;
225 ret = snd_usb_caiaq_set_audio_params(dev, runtime->rate,
226 runtime->sample_bits, bpp);
230 ret = stream_start(dev);
234 dev->output_running = 0;
235 wait_event_timeout(dev->prepare_wait_queue, dev->output_running, HZ);
236 if (!dev->output_running) {
244 static int snd_usb_caiaq_pcm_trigger(struct snd_pcm_substream *sub, int cmd)
246 struct snd_usb_caiaqdev *dev = snd_pcm_substream_chip(sub);
249 case SNDRV_PCM_TRIGGER_START:
250 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
251 spin_lock(&dev->spinlock);
252 activate_substream(dev, sub);
253 spin_unlock(&dev->spinlock);
255 case SNDRV_PCM_TRIGGER_STOP:
256 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
257 spin_lock(&dev->spinlock);
258 deactivate_substream(dev, sub);
259 spin_unlock(&dev->spinlock);
268 static snd_pcm_uframes_t
269 snd_usb_caiaq_pcm_pointer(struct snd_pcm_substream *sub)
271 int index = sub->number;
272 struct snd_usb_caiaqdev *dev = snd_pcm_substream_chip(sub);
274 if (dev->input_panic || dev->output_panic)
275 return SNDRV_PCM_POS_XRUN;
277 if (sub->stream == SNDRV_PCM_STREAM_PLAYBACK)
278 return bytes_to_frames(sub->runtime,
279 dev->audio_out_buf_pos[index]);
281 return bytes_to_frames(sub->runtime,
282 dev->audio_in_buf_pos[index]);
285 /* operators for both playback and capture */
286 static struct snd_pcm_ops snd_usb_caiaq_ops = {
287 .open = snd_usb_caiaq_substream_open,
288 .close = snd_usb_caiaq_substream_close,
289 .ioctl = snd_pcm_lib_ioctl,
290 .hw_params = snd_usb_caiaq_pcm_hw_params,
291 .hw_free = snd_usb_caiaq_pcm_hw_free,
292 .prepare = snd_usb_caiaq_pcm_prepare,
293 .trigger = snd_usb_caiaq_pcm_trigger,
294 .pointer = snd_usb_caiaq_pcm_pointer
297 static void check_for_elapsed_periods(struct snd_usb_caiaqdev *dev,
298 struct snd_pcm_substream **subs)
300 int stream, pb, *cnt;
301 struct snd_pcm_substream *sub;
303 for (stream = 0; stream < dev->n_streams; stream++) {
308 pb = frames_to_bytes(sub->runtime,
309 sub->runtime->period_size);
310 cnt = (sub->stream == SNDRV_PCM_STREAM_PLAYBACK) ?
311 &dev->period_out_count[stream] :
312 &dev->period_in_count[stream];
315 snd_pcm_period_elapsed(sub);
321 static void read_in_urb_mode0(struct snd_usb_caiaqdev *dev,
322 const struct urb *urb,
323 const struct usb_iso_packet_descriptor *iso)
325 unsigned char *usb_buf = urb->transfer_buffer + iso->offset;
326 struct snd_pcm_substream *sub;
329 if (all_substreams_zero(dev->sub_capture))
332 spin_lock(&dev->spinlock);
334 for (i = 0; i < iso->actual_length;) {
335 for (stream = 0; stream < dev->n_streams; stream++, i++) {
336 sub = dev->sub_capture[stream];
338 struct snd_pcm_runtime *rt = sub->runtime;
339 char *audio_buf = rt->dma_area;
340 int sz = frames_to_bytes(rt, rt->buffer_size);
341 audio_buf[dev->audio_in_buf_pos[stream]++]
343 dev->period_in_count[stream]++;
344 if (dev->audio_in_buf_pos[stream] == sz)
345 dev->audio_in_buf_pos[stream] = 0;
350 spin_unlock(&dev->spinlock);
353 static void read_in_urb_mode2(struct snd_usb_caiaqdev *dev,
354 const struct urb *urb,
355 const struct usb_iso_packet_descriptor *iso)
357 unsigned char *usb_buf = urb->transfer_buffer + iso->offset;
358 unsigned char check_byte;
359 struct snd_pcm_substream *sub;
362 spin_lock(&dev->spinlock);
364 for (i = 0; i < iso->actual_length;) {
365 if (i % (dev->n_streams * BYTES_PER_SAMPLE_USB) == 0) {
367 stream < dev->n_streams;
369 if (dev->first_packet)
372 check_byte = MAKE_CHECKBYTE(dev, stream, i);
374 if ((usb_buf[i] & 0x3f) != check_byte)
375 dev->input_panic = 1;
377 if (usb_buf[i] & 0x80)
378 dev->output_panic = 1;
381 dev->first_packet = 0;
383 for (stream = 0; stream < dev->n_streams; stream++, i++) {
384 sub = dev->sub_capture[stream];
386 struct snd_pcm_runtime *rt = sub->runtime;
387 char *audio_buf = rt->dma_area;
388 int sz = frames_to_bytes(rt, rt->buffer_size);
389 audio_buf[dev->audio_in_buf_pos[stream]++] =
391 dev->period_in_count[stream]++;
392 if (dev->audio_in_buf_pos[stream] == sz)
393 dev->audio_in_buf_pos[stream] = 0;
398 spin_unlock(&dev->spinlock);
401 static void read_in_urb(struct snd_usb_caiaqdev *dev,
402 const struct urb *urb,
403 const struct usb_iso_packet_descriptor *iso)
408 switch (dev->spec.data_alignment) {
410 read_in_urb_mode0(dev, urb, iso);
413 read_in_urb_mode2(dev, urb, iso);
417 if (dev->input_panic || dev->output_panic) {
418 debug("streaming error detected %s %s\n",
419 dev->input_panic ? "(input)" : "",
420 dev->output_panic ? "(output)" : "");
423 check_for_elapsed_periods(dev, dev->sub_capture);
426 static void fill_out_urb(struct snd_usb_caiaqdev *dev,
428 const struct usb_iso_packet_descriptor *iso)
430 unsigned char *usb_buf = urb->transfer_buffer + iso->offset;
431 struct snd_pcm_substream *sub;
434 spin_lock(&dev->spinlock);
436 for (i = 0; i < iso->length;) {
437 for (stream = 0; stream < dev->n_streams; stream++, i++) {
438 sub = dev->sub_playback[stream];
440 struct snd_pcm_runtime *rt = sub->runtime;
441 char *audio_buf = rt->dma_area;
442 int sz = frames_to_bytes(rt, rt->buffer_size);
444 audio_buf[dev->audio_out_buf_pos[stream]];
445 dev->period_out_count[stream]++;
446 dev->audio_out_buf_pos[stream]++;
447 if (dev->audio_out_buf_pos[stream] == sz)
448 dev->audio_out_buf_pos[stream] = 0;
453 /* fill in the check bytes */
454 if (dev->spec.data_alignment == 2 &&
455 i % (dev->n_streams * BYTES_PER_SAMPLE_USB) ==
456 (dev->n_streams * CHANNELS_PER_STREAM))
457 for (stream = 0; stream < dev->n_streams; stream++, i++)
458 usb_buf[i] = MAKE_CHECKBYTE(dev, stream, i);
461 spin_unlock(&dev->spinlock);
462 check_for_elapsed_periods(dev, dev->sub_playback);
465 static void read_completed(struct urb *urb)
467 struct snd_usb_caiaq_cb_info *info = urb->context;
468 struct snd_usb_caiaqdev *dev;
470 int frame, len, send_it = 0, outframe = 0;
472 if (urb->status || !info)
479 out = dev->data_urbs_out[info->index];
481 /* read the recently received packet and send back one which has
483 for (frame = 0; frame < FRAMES_PER_URB; frame++) {
484 if (urb->iso_frame_desc[frame].status)
487 len = urb->iso_frame_desc[outframe].actual_length;
488 out->iso_frame_desc[outframe].length = len;
489 out->iso_frame_desc[outframe].actual_length = 0;
490 out->iso_frame_desc[outframe].offset = BYTES_PER_FRAME * frame;
493 fill_out_urb(dev, out, &out->iso_frame_desc[outframe]);
494 read_in_urb(dev, urb, &urb->iso_frame_desc[frame]);
502 out->number_of_packets = FRAMES_PER_URB;
503 out->transfer_flags = URB_ISO_ASAP;
504 usb_submit_urb(out, GFP_ATOMIC);
507 /* re-submit inbound urb */
508 for (frame = 0; frame < FRAMES_PER_URB; frame++) {
509 urb->iso_frame_desc[frame].offset = BYTES_PER_FRAME * frame;
510 urb->iso_frame_desc[frame].length = BYTES_PER_FRAME;
511 urb->iso_frame_desc[frame].actual_length = 0;
514 urb->number_of_packets = FRAMES_PER_URB;
515 urb->transfer_flags = URB_ISO_ASAP;
516 usb_submit_urb(urb, GFP_ATOMIC);
519 static void write_completed(struct urb *urb)
521 struct snd_usb_caiaq_cb_info *info = urb->context;
522 struct snd_usb_caiaqdev *dev = info->dev;
524 if (!dev->output_running) {
525 dev->output_running = 1;
526 wake_up(&dev->prepare_wait_queue);
530 static struct urb **alloc_urbs(struct snd_usb_caiaqdev *dev, int dir, int *ret)
534 struct usb_device *usb_dev = dev->chip.dev;
537 pipe = (dir == SNDRV_PCM_STREAM_PLAYBACK) ?
538 usb_sndisocpipe(usb_dev, ENDPOINT_PLAYBACK) :
539 usb_rcvisocpipe(usb_dev, ENDPOINT_CAPTURE);
541 urbs = kmalloc(N_URBS * sizeof(*urbs), GFP_KERNEL);
543 log("unable to kmalloc() urbs, OOM!?\n");
548 for (i = 0; i < N_URBS; i++) {
549 urbs[i] = usb_alloc_urb(FRAMES_PER_URB, GFP_KERNEL);
551 log("unable to usb_alloc_urb(), OOM!?\n");
556 urbs[i]->transfer_buffer =
557 kmalloc(FRAMES_PER_URB * BYTES_PER_FRAME, GFP_KERNEL);
558 if (!urbs[i]->transfer_buffer) {
559 log("unable to kmalloc() transfer buffer, OOM!?\n");
564 for (frame = 0; frame < FRAMES_PER_URB; frame++) {
565 struct usb_iso_packet_descriptor *iso =
566 &urbs[i]->iso_frame_desc[frame];
568 iso->offset = BYTES_PER_FRAME * frame;
569 iso->length = BYTES_PER_FRAME;
572 urbs[i]->dev = usb_dev;
573 urbs[i]->pipe = pipe;
574 urbs[i]->transfer_buffer_length = FRAMES_PER_URB
576 urbs[i]->context = &dev->data_cb_info[i];
577 urbs[i]->interval = 1;
578 urbs[i]->transfer_flags = URB_ISO_ASAP;
579 urbs[i]->number_of_packets = FRAMES_PER_URB;
580 urbs[i]->complete = (dir == SNDRV_PCM_STREAM_CAPTURE) ?
581 read_completed : write_completed;
588 static void free_urbs(struct urb **urbs)
595 for (i = 0; i < N_URBS; i++) {
599 usb_kill_urb(urbs[i]);
600 kfree(urbs[i]->transfer_buffer);
601 usb_free_urb(urbs[i]);
607 int __devinit snd_usb_caiaq_audio_init(struct snd_usb_caiaqdev *dev)
611 dev->n_audio_in = max(dev->spec.num_analog_audio_in,
612 dev->spec.num_digital_audio_in) /
614 dev->n_audio_out = max(dev->spec.num_analog_audio_out,
615 dev->spec.num_digital_audio_out) /
617 dev->n_streams = max(dev->n_audio_in, dev->n_audio_out);
619 debug("dev->n_audio_in = %d\n", dev->n_audio_in);
620 debug("dev->n_audio_out = %d\n", dev->n_audio_out);
621 debug("dev->n_streams = %d\n", dev->n_streams);
623 if (dev->n_streams > MAX_STREAMS) {
624 log("unable to initialize device, too many streams.\n");
628 ret = snd_pcm_new(dev->chip.card, dev->product_name, 0,
629 dev->n_audio_out, dev->n_audio_in, &dev->pcm);
632 log("snd_pcm_new() returned %d\n", ret);
636 dev->pcm->private_data = dev;
637 strcpy(dev->pcm->name, dev->product_name);
639 memset(dev->sub_playback, 0, sizeof(dev->sub_playback));
640 memset(dev->sub_capture, 0, sizeof(dev->sub_capture));
642 memcpy(&dev->pcm_info, &snd_usb_caiaq_pcm_hardware,
643 sizeof(snd_usb_caiaq_pcm_hardware));
645 /* setup samplerates */
646 dev->samplerates = dev->pcm_info.rates;
647 switch (dev->chip.usb_id) {
648 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AK1):
649 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_RIGKONTROL3):
650 dev->samplerates |= SNDRV_PCM_RATE_88200;
651 dev->samplerates |= SNDRV_PCM_RATE_192000;
653 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AUDIO8DJ):
654 dev->samplerates |= SNDRV_PCM_RATE_88200;
658 snd_pcm_set_ops(dev->pcm, SNDRV_PCM_STREAM_PLAYBACK,
660 snd_pcm_set_ops(dev->pcm, SNDRV_PCM_STREAM_CAPTURE,
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);
669 kmalloc(sizeof(struct snd_usb_caiaq_cb_info) * N_URBS,
672 if (!dev->data_cb_info)
675 for (i = 0; i < N_URBS; i++) {
676 dev->data_cb_info[i].dev = dev;
677 dev->data_cb_info[i].index = i;
680 dev->data_urbs_in = alloc_urbs(dev, SNDRV_PCM_STREAM_CAPTURE, &ret);
682 kfree(dev->data_cb_info);
683 free_urbs(dev->data_urbs_in);
687 dev->data_urbs_out = alloc_urbs(dev, SNDRV_PCM_STREAM_PLAYBACK, &ret);
689 kfree(dev->data_cb_info);
690 free_urbs(dev->data_urbs_in);
691 free_urbs(dev->data_urbs_out);
698 void snd_usb_caiaq_audio_free(struct snd_usb_caiaqdev *dev)
700 debug("snd_usb_caiaq_audio_free (%p)\n", dev);
702 free_urbs(dev->data_urbs_in);
703 free_urbs(dev->data_urbs_out);
704 kfree(dev->data_cb_info);