2 * Linux driver for TerraTec DMX 6Fire USB
6 * Author: Torsten Schenk <torsten.schenk@zoho.com>
7 * Created: Jan 01, 2011
9 * Copyright: (C) Torsten Schenk
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
23 OUT_N_CHANNELS
= 6, IN_N_CHANNELS
= 4
26 /* keep next two synced with
27 * FW_EP_W_MAX_PACKET_SIZE[] and RATES_MAX_PACKET_SIZE
28 * and CONTROL_RATE_XXX in control.h */
29 static const int rates_in_packet_size
[] = { 228, 228, 420, 420, 404, 404 };
30 static const int rates_out_packet_size
[] = { 228, 228, 420, 420, 604, 604 };
31 static const int rates
[] = { 44100, 48000, 88200, 96000, 176400, 192000 };
32 static const int rates_alsaid
[] = {
33 SNDRV_PCM_RATE_44100
, SNDRV_PCM_RATE_48000
,
34 SNDRV_PCM_RATE_88200
, SNDRV_PCM_RATE_96000
,
35 SNDRV_PCM_RATE_176400
, SNDRV_PCM_RATE_192000
};
37 enum { /* settings for pcm */
38 OUT_EP
= 6, IN_EP
= 2, MAX_BUFSIZE
= 128 * 1024
41 enum { /* pcm streaming states */
42 STREAM_DISABLED
, /* no pcm streaming */
43 STREAM_STARTING
, /* pcm streaming requested, waiting to become ready */
44 STREAM_RUNNING
, /* pcm streaming running */
48 static const struct snd_pcm_hardware pcm_hw
= {
49 .info
= SNDRV_PCM_INFO_MMAP
|
50 SNDRV_PCM_INFO_INTERLEAVED
|
51 SNDRV_PCM_INFO_BLOCK_TRANSFER
|
52 SNDRV_PCM_INFO_MMAP_VALID
|
55 .formats
= SNDRV_PCM_FMTBIT_S24_LE
| SNDRV_PCM_FMTBIT_S32_LE
,
57 .rates
= SNDRV_PCM_RATE_44100
|
58 SNDRV_PCM_RATE_48000
|
59 SNDRV_PCM_RATE_88200
|
60 SNDRV_PCM_RATE_96000
|
61 SNDRV_PCM_RATE_176400
|
62 SNDRV_PCM_RATE_192000
,
67 .channels_max
= 0, /* set in pcm_open, depending on capture/playback */
68 .buffer_bytes_max
= MAX_BUFSIZE
,
69 .period_bytes_min
= PCM_N_PACKETS_PER_URB
* (PCM_MAX_PACKET_SIZE
- 4),
70 .period_bytes_max
= MAX_BUFSIZE
,
75 static int usb6fire_pcm_set_rate(struct pcm_runtime
*rt
)
78 struct control_runtime
*ctrl_rt
= rt
->chip
->control
;
80 ctrl_rt
->usb_streaming
= false;
81 ret
= ctrl_rt
->update_streaming(ctrl_rt
);
83 snd_printk(KERN_ERR PREFIX
"error stopping streaming while "
84 "setting samplerate %d.\n", rates
[rt
->rate
]);
88 ret
= ctrl_rt
->set_rate(ctrl_rt
, rt
->rate
);
90 snd_printk(KERN_ERR PREFIX
"error setting samplerate %d.\n",
95 ret
= ctrl_rt
->set_channels(ctrl_rt
, OUT_N_CHANNELS
, IN_N_CHANNELS
,
98 snd_printk(KERN_ERR PREFIX
"error initializing channels "
99 "while setting samplerate %d.\n",
104 ctrl_rt
->usb_streaming
= true;
105 ret
= ctrl_rt
->update_streaming(ctrl_rt
);
107 snd_printk(KERN_ERR PREFIX
"error starting streaming while "
108 "setting samplerate %d.\n", rates
[rt
->rate
]);
112 rt
->in_n_analog
= IN_N_CHANNELS
;
113 rt
->out_n_analog
= OUT_N_CHANNELS
;
114 rt
->in_packet_size
= rates_in_packet_size
[rt
->rate
];
115 rt
->out_packet_size
= rates_out_packet_size
[rt
->rate
];
119 static struct pcm_substream
*usb6fire_pcm_get_substream(
120 struct snd_pcm_substream
*alsa_sub
)
122 struct pcm_runtime
*rt
= snd_pcm_substream_chip(alsa_sub
);
124 if (alsa_sub
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
125 return &rt
->playback
;
126 else if (alsa_sub
->stream
== SNDRV_PCM_STREAM_CAPTURE
)
128 snd_printk(KERN_ERR PREFIX
"error getting pcm substream slot.\n");
132 /* call with stream_mutex locked */
133 static void usb6fire_pcm_stream_stop(struct pcm_runtime
*rt
)
136 struct control_runtime
*ctrl_rt
= rt
->chip
->control
;
138 if (rt
->stream_state
!= STREAM_DISABLED
) {
139 for (i
= 0; i
< PCM_N_URBS
; i
++) {
140 usb_kill_urb(&rt
->in_urbs
[i
].instance
);
141 usb_kill_urb(&rt
->out_urbs
[i
].instance
);
143 ctrl_rt
->usb_streaming
= false;
144 ctrl_rt
->update_streaming(ctrl_rt
);
145 rt
->stream_state
= STREAM_DISABLED
;
149 /* call with stream_mutex locked */
150 static int usb6fire_pcm_stream_start(struct pcm_runtime
*rt
)
155 struct usb_iso_packet_descriptor
*packet
;
157 if (rt
->stream_state
== STREAM_DISABLED
) {
158 /* submit our in urbs */
159 rt
->stream_wait_cond
= false;
160 rt
->stream_state
= STREAM_STARTING
;
161 for (i
= 0; i
< PCM_N_URBS
; i
++) {
162 for (k
= 0; k
< PCM_N_PACKETS_PER_URB
; k
++) {
163 packet
= &rt
->in_urbs
[i
].packets
[k
];
164 packet
->offset
= k
* rt
->in_packet_size
;
165 packet
->length
= rt
->in_packet_size
;
166 packet
->actual_length
= 0;
169 ret
= usb_submit_urb(&rt
->in_urbs
[i
].instance
,
172 usb6fire_pcm_stream_stop(rt
);
177 /* wait for first out urb to return (sent in in urb handler) */
178 wait_event_timeout(rt
->stream_wait_queue
, rt
->stream_wait_cond
,
180 if (rt
->stream_wait_cond
)
181 rt
->stream_state
= STREAM_RUNNING
;
183 usb6fire_pcm_stream_stop(rt
);
190 /* call with substream locked */
191 static void usb6fire_pcm_capture(struct pcm_substream
*sub
, struct pcm_urb
*urb
)
196 unsigned int total_length
= 0;
197 struct pcm_runtime
*rt
= snd_pcm_substream_chip(sub
->instance
);
198 struct snd_pcm_runtime
*alsa_rt
= sub
->instance
->runtime
;
200 u32
*dest
= (u32
*) (alsa_rt
->dma_area
+ sub
->dma_off
201 * (alsa_rt
->frame_bits
>> 3));
202 u32
*dest_end
= (u32
*) (alsa_rt
->dma_area
+ alsa_rt
->buffer_size
203 * (alsa_rt
->frame_bits
>> 3));
204 int bytes_per_frame
= alsa_rt
->channels
<< 2;
206 for (i
= 0; i
< PCM_N_PACKETS_PER_URB
; i
++) {
207 /* at least 4 header bytes for valid packet.
208 * after that: 32 bits per sample for analog channels */
209 if (urb
->packets
[i
].actual_length
> 4)
210 frame_count
= (urb
->packets
[i
].actual_length
- 4)
211 / (rt
->in_n_analog
<< 2);
215 if (alsa_rt
->format
== SNDRV_PCM_FORMAT_S24_LE
)
216 src
= (u32
*) (urb
->buffer
+ total_length
);
217 else if (alsa_rt
->format
== SNDRV_PCM_FORMAT_S32_LE
)
218 src
= (u32
*) (urb
->buffer
- 1 + total_length
);
221 src
++; /* skip leading 4 bytes of every packet */
222 total_length
+= urb
->packets
[i
].length
;
223 for (frame
= 0; frame
< frame_count
; frame
++) {
224 memcpy(dest
, src
, bytes_per_frame
);
225 dest
+= alsa_rt
->channels
;
226 src
+= rt
->in_n_analog
;
229 if (dest
== dest_end
) {
231 dest
= (u32
*) alsa_rt
->dma_area
;
237 /* call with substream locked */
238 static void usb6fire_pcm_playback(struct pcm_substream
*sub
,
244 struct pcm_runtime
*rt
= snd_pcm_substream_chip(sub
->instance
);
245 struct snd_pcm_runtime
*alsa_rt
= sub
->instance
->runtime
;
246 u32
*src
= (u32
*) (alsa_rt
->dma_area
+ sub
->dma_off
247 * (alsa_rt
->frame_bits
>> 3));
248 u32
*src_end
= (u32
*) (alsa_rt
->dma_area
+ alsa_rt
->buffer_size
249 * (alsa_rt
->frame_bits
>> 3));
251 int bytes_per_frame
= alsa_rt
->channels
<< 2;
253 if (alsa_rt
->format
== SNDRV_PCM_FORMAT_S32_LE
)
254 dest
= (u32
*) (urb
->buffer
- 1);
255 else if (alsa_rt
->format
== SNDRV_PCM_FORMAT_S24_LE
)
256 dest
= (u32
*) (urb
->buffer
);
258 snd_printk(KERN_ERR PREFIX
"Unknown sample format.");
262 for (i
= 0; i
< PCM_N_PACKETS_PER_URB
; i
++) {
263 /* at least 4 header bytes for valid packet.
264 * after that: 32 bits per sample for analog channels */
265 if (urb
->packets
[i
].length
> 4)
266 frame_count
= (urb
->packets
[i
].length
- 4)
267 / (rt
->out_n_analog
<< 2);
270 dest
++; /* skip leading 4 bytes of every frame */
271 for (frame
= 0; frame
< frame_count
; frame
++) {
272 memcpy(dest
, src
, bytes_per_frame
);
273 src
+= alsa_rt
->channels
;
274 dest
+= rt
->out_n_analog
;
277 if (src
== src_end
) {
278 src
= (u32
*) alsa_rt
->dma_area
;
285 static void usb6fire_pcm_in_urb_handler(struct urb
*usb_urb
)
287 struct pcm_urb
*in_urb
= usb_urb
->context
;
288 struct pcm_urb
*out_urb
= in_urb
->peer
;
289 struct pcm_runtime
*rt
= in_urb
->chip
->pcm
;
290 struct pcm_substream
*sub
;
292 int total_length
= 0;
299 if (usb_urb
->status
|| rt
->panic
|| rt
->stream_state
== STREAM_STOPPING
)
301 for (i
= 0; i
< PCM_N_PACKETS_PER_URB
; i
++)
302 if (in_urb
->packets
[i
].status
) {
307 if (rt
->stream_state
== STREAM_DISABLED
) {
308 snd_printk(KERN_ERR PREFIX
"internal error: "
309 "stream disabled in in-urb handler.\n");
313 /* receive our capture data */
315 spin_lock_irqsave(&sub
->lock
, flags
);
317 usb6fire_pcm_capture(sub
, in_urb
);
318 if (sub
->period_off
>= sub
->instance
->runtime
->period_size
) {
319 sub
->period_off
%= sub
->instance
->runtime
->period_size
;
320 spin_unlock_irqrestore(&sub
->lock
, flags
);
321 snd_pcm_period_elapsed(sub
->instance
);
323 spin_unlock_irqrestore(&sub
->lock
, flags
);
325 spin_unlock_irqrestore(&sub
->lock
, flags
);
327 /* setup out urb structure */
328 for (i
= 0; i
< PCM_N_PACKETS_PER_URB
; i
++) {
329 out_urb
->packets
[i
].offset
= total_length
;
330 out_urb
->packets
[i
].length
= (in_urb
->packets
[i
].actual_length
331 - 4) / (rt
->in_n_analog
<< 2)
332 * (rt
->out_n_analog
<< 2) + 4;
333 out_urb
->packets
[i
].status
= 0;
334 total_length
+= out_urb
->packets
[i
].length
;
336 memset(out_urb
->buffer
, 0, total_length
);
338 /* now send our playback data (if a free out urb was found) */
340 spin_lock_irqsave(&sub
->lock
, flags
);
342 usb6fire_pcm_playback(sub
, out_urb
);
343 if (sub
->period_off
>= sub
->instance
->runtime
->period_size
) {
344 sub
->period_off
%= sub
->instance
->runtime
->period_size
;
345 spin_unlock_irqrestore(&sub
->lock
, flags
);
346 snd_pcm_period_elapsed(sub
->instance
);
348 spin_unlock_irqrestore(&sub
->lock
, flags
);
350 spin_unlock_irqrestore(&sub
->lock
, flags
);
352 /* setup the 4th byte of each sample (0x40 for analog channels) */
353 dest
= out_urb
->buffer
;
354 for (i
= 0; i
< PCM_N_PACKETS_PER_URB
; i
++)
355 if (out_urb
->packets
[i
].length
>= 4) {
356 frame_count
= (out_urb
->packets
[i
].length
- 4)
357 / (rt
->out_n_analog
<< 2);
360 *(dest
++) = frame_count
;
362 for (frame
= 0; frame
< frame_count
; frame
++)
364 channel
< rt
->out_n_analog
;
366 dest
+= 3; /* skip sample data */
370 usb_submit_urb(&out_urb
->instance
, GFP_ATOMIC
);
371 usb_submit_urb(&in_urb
->instance
, GFP_ATOMIC
);
374 static void usb6fire_pcm_out_urb_handler(struct urb
*usb_urb
)
376 struct pcm_urb
*urb
= usb_urb
->context
;
377 struct pcm_runtime
*rt
= urb
->chip
->pcm
;
379 if (rt
->stream_state
== STREAM_STARTING
) {
380 rt
->stream_wait_cond
= true;
381 wake_up(&rt
->stream_wait_queue
);
385 static int usb6fire_pcm_open(struct snd_pcm_substream
*alsa_sub
)
387 struct pcm_runtime
*rt
= snd_pcm_substream_chip(alsa_sub
);
388 struct pcm_substream
*sub
= NULL
;
389 struct snd_pcm_runtime
*alsa_rt
= alsa_sub
->runtime
;
394 mutex_lock(&rt
->stream_mutex
);
395 alsa_rt
->hw
= pcm_hw
;
397 if (alsa_sub
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
398 if (rt
->rate
< ARRAY_SIZE(rates
))
399 alsa_rt
->hw
.rates
= rates_alsaid
[rt
->rate
];
400 alsa_rt
->hw
.channels_max
= OUT_N_CHANNELS
;
402 } else if (alsa_sub
->stream
== SNDRV_PCM_STREAM_CAPTURE
) {
403 if (rt
->rate
< ARRAY_SIZE(rates
))
404 alsa_rt
->hw
.rates
= rates_alsaid
[rt
->rate
];
405 alsa_rt
->hw
.channels_max
= IN_N_CHANNELS
;
410 mutex_unlock(&rt
->stream_mutex
);
411 snd_printk(KERN_ERR PREFIX
"invalid stream type.\n");
415 sub
->instance
= alsa_sub
;
417 mutex_unlock(&rt
->stream_mutex
);
421 static int usb6fire_pcm_close(struct snd_pcm_substream
*alsa_sub
)
423 struct pcm_runtime
*rt
= snd_pcm_substream_chip(alsa_sub
);
424 struct pcm_substream
*sub
= usb6fire_pcm_get_substream(alsa_sub
);
430 mutex_lock(&rt
->stream_mutex
);
432 /* deactivate substream */
433 spin_lock_irqsave(&sub
->lock
, flags
);
434 sub
->instance
= NULL
;
436 spin_unlock_irqrestore(&sub
->lock
, flags
);
438 /* all substreams closed? if so, stop streaming */
439 if (!rt
->playback
.instance
&& !rt
->capture
.instance
) {
440 usb6fire_pcm_stream_stop(rt
);
441 rt
->rate
= ARRAY_SIZE(rates
);
444 mutex_unlock(&rt
->stream_mutex
);
448 static int usb6fire_pcm_hw_params(struct snd_pcm_substream
*alsa_sub
,
449 struct snd_pcm_hw_params
*hw_params
)
451 return snd_pcm_lib_malloc_pages(alsa_sub
,
452 params_buffer_bytes(hw_params
));
455 static int usb6fire_pcm_hw_free(struct snd_pcm_substream
*alsa_sub
)
457 return snd_pcm_lib_free_pages(alsa_sub
);
460 static int usb6fire_pcm_prepare(struct snd_pcm_substream
*alsa_sub
)
462 struct pcm_runtime
*rt
= snd_pcm_substream_chip(alsa_sub
);
463 struct pcm_substream
*sub
= usb6fire_pcm_get_substream(alsa_sub
);
464 struct snd_pcm_runtime
*alsa_rt
= alsa_sub
->runtime
;
472 mutex_lock(&rt
->stream_mutex
);
476 if (rt
->stream_state
== STREAM_DISABLED
) {
477 for (rt
->rate
= 0; rt
->rate
< ARRAY_SIZE(rates
); rt
->rate
++)
478 if (alsa_rt
->rate
== rates
[rt
->rate
])
480 if (rt
->rate
== ARRAY_SIZE(rates
)) {
481 mutex_unlock(&rt
->stream_mutex
);
482 snd_printk("invalid rate %d in prepare.\n",
487 ret
= usb6fire_pcm_set_rate(rt
);
489 mutex_unlock(&rt
->stream_mutex
);
492 ret
= usb6fire_pcm_stream_start(rt
);
494 mutex_unlock(&rt
->stream_mutex
);
495 snd_printk(KERN_ERR PREFIX
496 "could not start pcm stream.\n");
500 mutex_unlock(&rt
->stream_mutex
);
504 static int usb6fire_pcm_trigger(struct snd_pcm_substream
*alsa_sub
, int cmd
)
506 struct pcm_substream
*sub
= usb6fire_pcm_get_substream(alsa_sub
);
507 struct pcm_runtime
*rt
= snd_pcm_substream_chip(alsa_sub
);
516 case SNDRV_PCM_TRIGGER_START
:
517 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
518 spin_lock_irqsave(&sub
->lock
, flags
);
520 spin_unlock_irqrestore(&sub
->lock
, flags
);
523 case SNDRV_PCM_TRIGGER_STOP
:
524 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
525 spin_lock_irqsave(&sub
->lock
, flags
);
527 spin_unlock_irqrestore(&sub
->lock
, flags
);
535 static snd_pcm_uframes_t
usb6fire_pcm_pointer(
536 struct snd_pcm_substream
*alsa_sub
)
538 struct pcm_substream
*sub
= usb6fire_pcm_get_substream(alsa_sub
);
539 struct pcm_runtime
*rt
= snd_pcm_substream_chip(alsa_sub
);
541 snd_pcm_uframes_t ret
;
543 if (rt
->panic
|| !sub
)
544 return SNDRV_PCM_STATE_XRUN
;
546 spin_lock_irqsave(&sub
->lock
, flags
);
548 spin_unlock_irqrestore(&sub
->lock
, flags
);
552 static struct snd_pcm_ops pcm_ops
= {
553 .open
= usb6fire_pcm_open
,
554 .close
= usb6fire_pcm_close
,
555 .ioctl
= snd_pcm_lib_ioctl
,
556 .hw_params
= usb6fire_pcm_hw_params
,
557 .hw_free
= usb6fire_pcm_hw_free
,
558 .prepare
= usb6fire_pcm_prepare
,
559 .trigger
= usb6fire_pcm_trigger
,
560 .pointer
= usb6fire_pcm_pointer
,
563 static void __devinit
usb6fire_pcm_init_urb(struct pcm_urb
*urb
,
564 struct sfire_chip
*chip
, bool in
, int ep
,
565 void (*handler
)(struct urb
*))
568 usb_init_urb(&urb
->instance
);
569 urb
->instance
.transfer_buffer
= urb
->buffer
;
570 urb
->instance
.transfer_buffer_length
=
571 PCM_N_PACKETS_PER_URB
* PCM_MAX_PACKET_SIZE
;
572 urb
->instance
.dev
= chip
->dev
;
573 urb
->instance
.pipe
= in
? usb_rcvisocpipe(chip
->dev
, ep
)
574 : usb_sndisocpipe(chip
->dev
, ep
);
575 urb
->instance
.interval
= 1;
576 urb
->instance
.transfer_flags
= URB_ISO_ASAP
;
577 urb
->instance
.complete
= handler
;
578 urb
->instance
.context
= urb
;
579 urb
->instance
.number_of_packets
= PCM_N_PACKETS_PER_URB
;
582 int __devinit
usb6fire_pcm_init(struct sfire_chip
*chip
)
587 struct pcm_runtime
*rt
=
588 kzalloc(sizeof(struct pcm_runtime
), GFP_KERNEL
);
594 rt
->stream_state
= STREAM_DISABLED
;
595 rt
->rate
= ARRAY_SIZE(rates
);
596 init_waitqueue_head(&rt
->stream_wait_queue
);
597 mutex_init(&rt
->stream_mutex
);
599 spin_lock_init(&rt
->playback
.lock
);
600 spin_lock_init(&rt
->capture
.lock
);
602 for (i
= 0; i
< PCM_N_URBS
; i
++) {
603 usb6fire_pcm_init_urb(&rt
->in_urbs
[i
], chip
, true, IN_EP
,
604 usb6fire_pcm_in_urb_handler
);
605 usb6fire_pcm_init_urb(&rt
->out_urbs
[i
], chip
, false, OUT_EP
,
606 usb6fire_pcm_out_urb_handler
);
608 rt
->in_urbs
[i
].peer
= &rt
->out_urbs
[i
];
609 rt
->out_urbs
[i
].peer
= &rt
->in_urbs
[i
];
612 ret
= snd_pcm_new(chip
->card
, "DMX6FireUSB", 0, 1, 1, &pcm
);
615 snd_printk(KERN_ERR PREFIX
"cannot create pcm instance.\n");
619 pcm
->private_data
= rt
;
620 strcpy(pcm
->name
, "DMX 6Fire USB");
621 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_PLAYBACK
, &pcm_ops
);
622 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_CAPTURE
, &pcm_ops
);
624 ret
= snd_pcm_lib_preallocate_pages_for_all(pcm
,
625 SNDRV_DMA_TYPE_CONTINUOUS
,
626 snd_dma_continuous_data(GFP_KERNEL
),
627 MAX_BUFSIZE
, MAX_BUFSIZE
);
630 snd_printk(KERN_ERR PREFIX
631 "error preallocating pcm buffers.\n");
640 void usb6fire_pcm_abort(struct sfire_chip
*chip
)
642 struct pcm_runtime
*rt
= chip
->pcm
;
648 if (rt
->playback
.instance
)
649 snd_pcm_stop(rt
->playback
.instance
,
650 SNDRV_PCM_STATE_XRUN
);
651 if (rt
->capture
.instance
)
652 snd_pcm_stop(rt
->capture
.instance
,
653 SNDRV_PCM_STATE_XRUN
);
655 for (i
= 0; i
< PCM_N_URBS
; i
++) {
656 usb_poison_urb(&rt
->in_urbs
[i
].instance
);
657 usb_poison_urb(&rt
->out_urbs
[i
].instance
);
663 void usb6fire_pcm_destroy(struct sfire_chip
*chip
)