2 * Linux driver for TerraTec DMX 6Fire USB
6 * Author: Torsten Schenk <torsten.schenk@zoho.com>
7 * Created: Jan 01, 2011
8 * Copyright: (C) Torsten Schenk
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
22 OUT_N_CHANNELS = 6, IN_N_CHANNELS = 4
25 /* keep next two synced with
26 * FW_EP_W_MAX_PACKET_SIZE[] and RATES_MAX_PACKET_SIZE
27 * and CONTROL_RATE_XXX in control.h */
28 static const int rates_in_packet_size[] = { 228, 228, 420, 420, 404, 404 };
29 static const int rates_out_packet_size[] = { 228, 228, 420, 420, 604, 604 };
30 static const int rates[] = { 44100, 48000, 88200, 96000, 176400, 192000 };
31 static const int rates_alsaid[] = {
32 SNDRV_PCM_RATE_44100, SNDRV_PCM_RATE_48000,
33 SNDRV_PCM_RATE_88200, SNDRV_PCM_RATE_96000,
34 SNDRV_PCM_RATE_176400, SNDRV_PCM_RATE_192000 };
36 enum { /* settings for pcm */
37 OUT_EP = 6, IN_EP = 2, MAX_BUFSIZE = 128 * 1024
40 enum { /* pcm streaming states */
41 STREAM_DISABLED, /* no pcm streaming */
42 STREAM_STARTING, /* pcm streaming requested, waiting to become ready */
43 STREAM_RUNNING, /* pcm streaming running */
47 static const struct snd_pcm_hardware pcm_hw = {
48 .info = SNDRV_PCM_INFO_MMAP |
49 SNDRV_PCM_INFO_INTERLEAVED |
50 SNDRV_PCM_INFO_BLOCK_TRANSFER |
51 SNDRV_PCM_INFO_MMAP_VALID |
54 .formats = SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE,
56 .rates = SNDRV_PCM_RATE_44100 |
57 SNDRV_PCM_RATE_48000 |
58 SNDRV_PCM_RATE_88200 |
59 SNDRV_PCM_RATE_96000 |
60 SNDRV_PCM_RATE_176400 |
61 SNDRV_PCM_RATE_192000,
66 .channels_max = 0, /* set in pcm_open, depending on capture/playback */
67 .buffer_bytes_max = MAX_BUFSIZE,
68 .period_bytes_min = PCM_N_PACKETS_PER_URB * (PCM_MAX_PACKET_SIZE - 4),
69 .period_bytes_max = MAX_BUFSIZE,
74 static int usb6fire_pcm_set_rate(struct pcm_runtime *rt)
77 struct control_runtime *ctrl_rt = rt->chip->control;
79 ctrl_rt->usb_streaming = false;
80 ret = ctrl_rt->update_streaming(ctrl_rt);
82 snd_printk(KERN_ERR PREFIX "error stopping streaming while "
83 "setting samplerate %d.\n", rates[rt->rate]);
87 ret = ctrl_rt->set_rate(ctrl_rt, rt->rate);
89 snd_printk(KERN_ERR PREFIX "error setting samplerate %d.\n",
94 ret = ctrl_rt->set_channels(ctrl_rt, OUT_N_CHANNELS, IN_N_CHANNELS,
97 snd_printk(KERN_ERR PREFIX "error initializing channels "
98 "while setting samplerate %d.\n",
103 ctrl_rt->usb_streaming = true;
104 ret = ctrl_rt->update_streaming(ctrl_rt);
106 snd_printk(KERN_ERR PREFIX "error starting streaming while "
107 "setting samplerate %d.\n", rates[rt->rate]);
111 rt->in_n_analog = IN_N_CHANNELS;
112 rt->out_n_analog = OUT_N_CHANNELS;
113 rt->in_packet_size = rates_in_packet_size[rt->rate];
114 rt->out_packet_size = rates_out_packet_size[rt->rate];
118 static struct pcm_substream *usb6fire_pcm_get_substream(
119 struct snd_pcm_substream *alsa_sub)
121 struct pcm_runtime *rt = snd_pcm_substream_chip(alsa_sub);
123 if (alsa_sub->stream == SNDRV_PCM_STREAM_PLAYBACK)
124 return &rt->playback;
125 else if (alsa_sub->stream == SNDRV_PCM_STREAM_CAPTURE)
127 snd_printk(KERN_ERR PREFIX "error getting pcm substream slot.\n");
131 /* call with stream_mutex locked */
132 static void usb6fire_pcm_stream_stop(struct pcm_runtime *rt)
135 struct control_runtime *ctrl_rt = rt->chip->control;
137 if (rt->stream_state != STREAM_DISABLED) {
139 rt->stream_state = STREAM_STOPPING;
141 for (i = 0; i < PCM_N_URBS; i++) {
142 usb_kill_urb(&rt->in_urbs[i].instance);
143 usb_kill_urb(&rt->out_urbs[i].instance);
145 ctrl_rt->usb_streaming = false;
146 ctrl_rt->update_streaming(ctrl_rt);
147 rt->stream_state = STREAM_DISABLED;
151 /* call with stream_mutex locked */
152 static int usb6fire_pcm_stream_start(struct pcm_runtime *rt)
157 struct usb_iso_packet_descriptor *packet;
159 if (rt->stream_state == STREAM_DISABLED) {
160 /* submit our in urbs */
161 rt->stream_wait_cond = false;
162 rt->stream_state = STREAM_STARTING;
163 for (i = 0; i < PCM_N_URBS; i++) {
164 for (k = 0; k < PCM_N_PACKETS_PER_URB; k++) {
165 packet = &rt->in_urbs[i].packets[k];
166 packet->offset = k * rt->in_packet_size;
167 packet->length = rt->in_packet_size;
168 packet->actual_length = 0;
171 ret = usb_submit_urb(&rt->in_urbs[i].instance,
174 usb6fire_pcm_stream_stop(rt);
179 /* wait for first out urb to return (sent in in urb handler) */
180 wait_event_timeout(rt->stream_wait_queue, rt->stream_wait_cond,
182 if (rt->stream_wait_cond)
183 rt->stream_state = STREAM_RUNNING;
185 usb6fire_pcm_stream_stop(rt);
192 /* call with substream locked */
193 static void usb6fire_pcm_capture(struct pcm_substream *sub, struct pcm_urb *urb)
198 unsigned int total_length = 0;
199 struct pcm_runtime *rt = snd_pcm_substream_chip(sub->instance);
200 struct snd_pcm_runtime *alsa_rt = sub->instance->runtime;
202 u32 *dest = (u32 *) (alsa_rt->dma_area + sub->dma_off
203 * (alsa_rt->frame_bits >> 3));
204 u32 *dest_end = (u32 *) (alsa_rt->dma_area + alsa_rt->buffer_size
205 * (alsa_rt->frame_bits >> 3));
206 int bytes_per_frame = alsa_rt->channels << 2;
208 for (i = 0; i < PCM_N_PACKETS_PER_URB; i++) {
209 /* at least 4 header bytes for valid packet.
210 * after that: 32 bits per sample for analog channels */
211 if (urb->packets[i].actual_length > 4)
212 frame_count = (urb->packets[i].actual_length - 4)
213 / (rt->in_n_analog << 2);
217 if (alsa_rt->format == SNDRV_PCM_FORMAT_S24_LE)
218 src = (u32 *) (urb->buffer + total_length);
219 else if (alsa_rt->format == SNDRV_PCM_FORMAT_S32_LE)
220 src = (u32 *) (urb->buffer - 1 + total_length);
223 src++; /* skip leading 4 bytes of every packet */
224 total_length += urb->packets[i].length;
225 for (frame = 0; frame < frame_count; frame++) {
226 memcpy(dest, src, bytes_per_frame);
227 dest += alsa_rt->channels;
228 src += rt->in_n_analog;
231 if (dest == dest_end) {
233 dest = (u32 *) alsa_rt->dma_area;
239 /* call with substream locked */
240 static void usb6fire_pcm_playback(struct pcm_substream *sub,
246 struct pcm_runtime *rt = snd_pcm_substream_chip(sub->instance);
247 struct snd_pcm_runtime *alsa_rt = sub->instance->runtime;
248 u32 *src = (u32 *) (alsa_rt->dma_area + sub->dma_off
249 * (alsa_rt->frame_bits >> 3));
250 u32 *src_end = (u32 *) (alsa_rt->dma_area + alsa_rt->buffer_size
251 * (alsa_rt->frame_bits >> 3));
253 int bytes_per_frame = alsa_rt->channels << 2;
255 if (alsa_rt->format == SNDRV_PCM_FORMAT_S32_LE)
256 dest = (u32 *) (urb->buffer - 1);
257 else if (alsa_rt->format == SNDRV_PCM_FORMAT_S24_LE)
258 dest = (u32 *) (urb->buffer);
260 snd_printk(KERN_ERR PREFIX "Unknown sample format.");
264 for (i = 0; i < PCM_N_PACKETS_PER_URB; i++) {
265 /* at least 4 header bytes for valid packet.
266 * after that: 32 bits per sample for analog channels */
267 if (urb->packets[i].length > 4)
268 frame_count = (urb->packets[i].length - 4)
269 / (rt->out_n_analog << 2);
272 dest++; /* skip leading 4 bytes of every frame */
273 for (frame = 0; frame < frame_count; frame++) {
274 memcpy(dest, src, bytes_per_frame);
275 src += alsa_rt->channels;
276 dest += rt->out_n_analog;
279 if (src == src_end) {
280 src = (u32 *) alsa_rt->dma_area;
287 static void usb6fire_pcm_in_urb_handler(struct urb *usb_urb)
289 struct pcm_urb *in_urb = usb_urb->context;
290 struct pcm_urb *out_urb = in_urb->peer;
291 struct pcm_runtime *rt = in_urb->chip->pcm;
292 struct pcm_substream *sub;
294 int total_length = 0;
301 if (usb_urb->status || rt->panic || rt->stream_state == STREAM_STOPPING)
303 for (i = 0; i < PCM_N_PACKETS_PER_URB; i++)
304 if (in_urb->packets[i].status) {
309 if (rt->stream_state == STREAM_DISABLED) {
310 snd_printk(KERN_ERR PREFIX "internal error: "
311 "stream disabled in in-urb handler.\n");
315 /* receive our capture data */
317 spin_lock_irqsave(&sub->lock, flags);
319 usb6fire_pcm_capture(sub, in_urb);
320 if (sub->period_off >= sub->instance->runtime->period_size) {
321 sub->period_off %= sub->instance->runtime->period_size;
322 spin_unlock_irqrestore(&sub->lock, flags);
323 snd_pcm_period_elapsed(sub->instance);
325 spin_unlock_irqrestore(&sub->lock, flags);
327 spin_unlock_irqrestore(&sub->lock, flags);
329 /* setup out urb structure */
330 for (i = 0; i < PCM_N_PACKETS_PER_URB; i++) {
331 out_urb->packets[i].offset = total_length;
332 out_urb->packets[i].length = (in_urb->packets[i].actual_length
333 - 4) / (rt->in_n_analog << 2)
334 * (rt->out_n_analog << 2) + 4;
335 out_urb->packets[i].status = 0;
336 total_length += out_urb->packets[i].length;
338 memset(out_urb->buffer, 0, total_length);
340 /* now send our playback data (if a free out urb was found) */
342 spin_lock_irqsave(&sub->lock, flags);
344 usb6fire_pcm_playback(sub, out_urb);
345 if (sub->period_off >= sub->instance->runtime->period_size) {
346 sub->period_off %= sub->instance->runtime->period_size;
347 spin_unlock_irqrestore(&sub->lock, flags);
348 snd_pcm_period_elapsed(sub->instance);
350 spin_unlock_irqrestore(&sub->lock, flags);
352 spin_unlock_irqrestore(&sub->lock, flags);
354 /* setup the 4th byte of each sample (0x40 for analog channels) */
355 dest = out_urb->buffer;
356 for (i = 0; i < PCM_N_PACKETS_PER_URB; i++)
357 if (out_urb->packets[i].length >= 4) {
358 frame_count = (out_urb->packets[i].length - 4)
359 / (rt->out_n_analog << 2);
362 *(dest++) = frame_count;
364 for (frame = 0; frame < frame_count; frame++)
366 channel < rt->out_n_analog;
368 dest += 3; /* skip sample data */
372 usb_submit_urb(&out_urb->instance, GFP_ATOMIC);
373 usb_submit_urb(&in_urb->instance, GFP_ATOMIC);
376 static void usb6fire_pcm_out_urb_handler(struct urb *usb_urb)
378 struct pcm_urb *urb = usb_urb->context;
379 struct pcm_runtime *rt = urb->chip->pcm;
381 if (rt->stream_state == STREAM_STARTING) {
382 rt->stream_wait_cond = true;
383 wake_up(&rt->stream_wait_queue);
387 static int usb6fire_pcm_open(struct snd_pcm_substream *alsa_sub)
389 struct pcm_runtime *rt = snd_pcm_substream_chip(alsa_sub);
390 struct pcm_substream *sub = NULL;
391 struct snd_pcm_runtime *alsa_rt = alsa_sub->runtime;
396 mutex_lock(&rt->stream_mutex);
397 alsa_rt->hw = pcm_hw;
399 if (alsa_sub->stream == SNDRV_PCM_STREAM_PLAYBACK) {
400 if (rt->rate < ARRAY_SIZE(rates))
401 alsa_rt->hw.rates = rates_alsaid[rt->rate];
402 alsa_rt->hw.channels_max = OUT_N_CHANNELS;
404 } else if (alsa_sub->stream == SNDRV_PCM_STREAM_CAPTURE) {
405 if (rt->rate < ARRAY_SIZE(rates))
406 alsa_rt->hw.rates = rates_alsaid[rt->rate];
407 alsa_rt->hw.channels_max = IN_N_CHANNELS;
412 mutex_unlock(&rt->stream_mutex);
413 snd_printk(KERN_ERR PREFIX "invalid stream type.\n");
417 sub->instance = alsa_sub;
419 mutex_unlock(&rt->stream_mutex);
423 static int usb6fire_pcm_close(struct snd_pcm_substream *alsa_sub)
425 struct pcm_runtime *rt = snd_pcm_substream_chip(alsa_sub);
426 struct pcm_substream *sub = usb6fire_pcm_get_substream(alsa_sub);
432 mutex_lock(&rt->stream_mutex);
434 /* deactivate substream */
435 spin_lock_irqsave(&sub->lock, flags);
436 sub->instance = NULL;
438 spin_unlock_irqrestore(&sub->lock, flags);
440 /* all substreams closed? if so, stop streaming */
441 if (!rt->playback.instance && !rt->capture.instance) {
442 usb6fire_pcm_stream_stop(rt);
443 rt->rate = ARRAY_SIZE(rates);
446 mutex_unlock(&rt->stream_mutex);
450 static int usb6fire_pcm_hw_params(struct snd_pcm_substream *alsa_sub,
451 struct snd_pcm_hw_params *hw_params)
453 return snd_pcm_lib_malloc_pages(alsa_sub,
454 params_buffer_bytes(hw_params));
457 static int usb6fire_pcm_hw_free(struct snd_pcm_substream *alsa_sub)
459 return snd_pcm_lib_free_pages(alsa_sub);
462 static int usb6fire_pcm_prepare(struct snd_pcm_substream *alsa_sub)
464 struct pcm_runtime *rt = snd_pcm_substream_chip(alsa_sub);
465 struct pcm_substream *sub = usb6fire_pcm_get_substream(alsa_sub);
466 struct snd_pcm_runtime *alsa_rt = alsa_sub->runtime;
474 mutex_lock(&rt->stream_mutex);
478 if (rt->stream_state == STREAM_DISABLED) {
479 for (rt->rate = 0; rt->rate < ARRAY_SIZE(rates); rt->rate++)
480 if (alsa_rt->rate == rates[rt->rate])
482 if (rt->rate == ARRAY_SIZE(rates)) {
483 mutex_unlock(&rt->stream_mutex);
484 snd_printk("invalid rate %d in prepare.\n",
489 ret = usb6fire_pcm_set_rate(rt);
491 mutex_unlock(&rt->stream_mutex);
494 ret = usb6fire_pcm_stream_start(rt);
496 mutex_unlock(&rt->stream_mutex);
497 snd_printk(KERN_ERR PREFIX
498 "could not start pcm stream.\n");
502 mutex_unlock(&rt->stream_mutex);
506 static int usb6fire_pcm_trigger(struct snd_pcm_substream *alsa_sub, int cmd)
508 struct pcm_substream *sub = usb6fire_pcm_get_substream(alsa_sub);
509 struct pcm_runtime *rt = snd_pcm_substream_chip(alsa_sub);
518 case SNDRV_PCM_TRIGGER_START:
519 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
520 spin_lock_irqsave(&sub->lock, flags);
522 spin_unlock_irqrestore(&sub->lock, flags);
525 case SNDRV_PCM_TRIGGER_STOP:
526 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
527 spin_lock_irqsave(&sub->lock, flags);
529 spin_unlock_irqrestore(&sub->lock, flags);
537 static snd_pcm_uframes_t usb6fire_pcm_pointer(
538 struct snd_pcm_substream *alsa_sub)
540 struct pcm_substream *sub = usb6fire_pcm_get_substream(alsa_sub);
541 struct pcm_runtime *rt = snd_pcm_substream_chip(alsa_sub);
543 snd_pcm_uframes_t ret;
545 if (rt->panic || !sub)
546 return SNDRV_PCM_POS_XRUN;
548 spin_lock_irqsave(&sub->lock, flags);
550 spin_unlock_irqrestore(&sub->lock, flags);
554 static struct snd_pcm_ops pcm_ops = {
555 .open = usb6fire_pcm_open,
556 .close = usb6fire_pcm_close,
557 .ioctl = snd_pcm_lib_ioctl,
558 .hw_params = usb6fire_pcm_hw_params,
559 .hw_free = usb6fire_pcm_hw_free,
560 .prepare = usb6fire_pcm_prepare,
561 .trigger = usb6fire_pcm_trigger,
562 .pointer = usb6fire_pcm_pointer,
565 static void usb6fire_pcm_init_urb(struct pcm_urb *urb,
566 struct sfire_chip *chip, bool in, int ep,
567 void (*handler)(struct urb *))
570 usb_init_urb(&urb->instance);
571 urb->instance.transfer_buffer = urb->buffer;
572 urb->instance.transfer_buffer_length =
573 PCM_N_PACKETS_PER_URB * PCM_MAX_PACKET_SIZE;
574 urb->instance.dev = chip->dev;
575 urb->instance.pipe = in ? usb_rcvisocpipe(chip->dev, ep)
576 : usb_sndisocpipe(chip->dev, ep);
577 urb->instance.interval = 1;
578 urb->instance.complete = handler;
579 urb->instance.context = urb;
580 urb->instance.number_of_packets = PCM_N_PACKETS_PER_URB;
583 static int usb6fire_pcm_buffers_init(struct pcm_runtime *rt)
587 for (i = 0; i < PCM_N_URBS; i++) {
588 rt->out_urbs[i].buffer = kzalloc(PCM_N_PACKETS_PER_URB
589 * PCM_MAX_PACKET_SIZE, GFP_KERNEL);
590 if (!rt->out_urbs[i].buffer)
592 rt->in_urbs[i].buffer = kzalloc(PCM_N_PACKETS_PER_URB
593 * PCM_MAX_PACKET_SIZE, GFP_KERNEL);
594 if (!rt->in_urbs[i].buffer)
600 static void usb6fire_pcm_buffers_destroy(struct pcm_runtime *rt)
604 for (i = 0; i < PCM_N_URBS; i++) {
605 kfree(rt->out_urbs[i].buffer);
606 kfree(rt->in_urbs[i].buffer);
610 int usb6fire_pcm_init(struct sfire_chip *chip)
615 struct pcm_runtime *rt =
616 kzalloc(sizeof(struct pcm_runtime), GFP_KERNEL);
621 ret = usb6fire_pcm_buffers_init(rt);
623 usb6fire_pcm_buffers_destroy(rt);
629 rt->stream_state = STREAM_DISABLED;
630 rt->rate = ARRAY_SIZE(rates);
631 init_waitqueue_head(&rt->stream_wait_queue);
632 mutex_init(&rt->stream_mutex);
634 spin_lock_init(&rt->playback.lock);
635 spin_lock_init(&rt->capture.lock);
637 for (i = 0; i < PCM_N_URBS; i++) {
638 usb6fire_pcm_init_urb(&rt->in_urbs[i], chip, true, IN_EP,
639 usb6fire_pcm_in_urb_handler);
640 usb6fire_pcm_init_urb(&rt->out_urbs[i], chip, false, OUT_EP,
641 usb6fire_pcm_out_urb_handler);
643 rt->in_urbs[i].peer = &rt->out_urbs[i];
644 rt->out_urbs[i].peer = &rt->in_urbs[i];
647 ret = snd_pcm_new(chip->card, "DMX6FireUSB", 0, 1, 1, &pcm);
649 usb6fire_pcm_buffers_destroy(rt);
651 snd_printk(KERN_ERR PREFIX "cannot create pcm instance.\n");
655 pcm->private_data = rt;
656 strcpy(pcm->name, "DMX 6Fire USB");
657 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &pcm_ops);
658 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &pcm_ops);
660 ret = snd_pcm_lib_preallocate_pages_for_all(pcm,
661 SNDRV_DMA_TYPE_CONTINUOUS,
662 snd_dma_continuous_data(GFP_KERNEL),
663 MAX_BUFSIZE, MAX_BUFSIZE);
665 usb6fire_pcm_buffers_destroy(rt);
667 snd_printk(KERN_ERR PREFIX
668 "error preallocating pcm buffers.\n");
677 void usb6fire_pcm_abort(struct sfire_chip *chip)
679 struct pcm_runtime *rt = chip->pcm;
686 if (rt->playback.instance) {
687 snd_pcm_stream_lock_irqsave(rt->playback.instance, flags);
688 snd_pcm_stop(rt->playback.instance,
689 SNDRV_PCM_STATE_XRUN);
690 snd_pcm_stream_unlock_irqrestore(rt->playback.instance, flags);
693 if (rt->capture.instance) {
694 snd_pcm_stream_lock_irqsave(rt->capture.instance, flags);
695 snd_pcm_stop(rt->capture.instance,
696 SNDRV_PCM_STATE_XRUN);
697 snd_pcm_stream_unlock_irqrestore(rt->capture.instance, flags);
700 for (i = 0; i < PCM_N_URBS; i++) {
701 usb_poison_urb(&rt->in_urbs[i].instance);
702 usb_poison_urb(&rt->out_urbs[i].instance);
708 void usb6fire_pcm_destroy(struct sfire_chip *chip)
710 struct pcm_runtime *rt = chip->pcm;
712 usb6fire_pcm_buffers_destroy(rt);