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.
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 static const int rates_in_packet_size
[] = { 228, 228, 420, 420, 404, 404 };
28 static const int rates_out_packet_size
[] = { 228, 228, 420, 420, 604, 604 };
29 static const int rates
[] = { 44100, 48000, 88200, 96000, 176400, 192000 };
30 static const int rates_altsetting
[] = { 1, 1, 2, 2, 3, 3 };
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 /* values to write to soundcard register for all samplerates */
37 static const u16 rates_6fire_vl
[] = {0x00, 0x01, 0x00, 0x01, 0x00, 0x01};
38 static const u16 rates_6fire_vh
[] = {0x11, 0x11, 0x10, 0x10, 0x00, 0x00};
40 enum { /* settings for pcm */
41 OUT_EP
= 6, IN_EP
= 2, MAX_BUFSIZE
= 128 * 1024
44 enum { /* pcm streaming states */
45 STREAM_DISABLED
, /* no pcm streaming */
46 STREAM_STARTING
, /* pcm streaming requested, waiting to become ready */
47 STREAM_RUNNING
, /* pcm streaming running */
51 enum { /* pcm sample rates (also index into RATES_XXX[]) */
60 static const struct snd_pcm_hardware pcm_hw
= {
61 .info
= SNDRV_PCM_INFO_MMAP
|
62 SNDRV_PCM_INFO_INTERLEAVED
|
63 SNDRV_PCM_INFO_BLOCK_TRANSFER
|
64 SNDRV_PCM_INFO_MMAP_VALID
|
67 .formats
= SNDRV_PCM_FMTBIT_S24_LE
,
69 .rates
= SNDRV_PCM_RATE_44100
|
70 SNDRV_PCM_RATE_48000
|
71 SNDRV_PCM_RATE_88200
|
72 SNDRV_PCM_RATE_96000
|
73 SNDRV_PCM_RATE_176400
|
74 SNDRV_PCM_RATE_192000
,
79 .channels_max
= 0, /* set in pcm_open, depending on capture/playback */
80 .buffer_bytes_max
= MAX_BUFSIZE
,
81 .period_bytes_min
= PCM_N_PACKETS_PER_URB
* (PCM_MAX_PACKET_SIZE
- 4),
82 .period_bytes_max
= MAX_BUFSIZE
,
87 static int usb6fire_pcm_set_rate(struct pcm_runtime
*rt
)
90 struct usb_device
*device
= rt
->chip
->dev
;
91 struct comm_runtime
*comm_rt
= rt
->chip
->comm
;
93 if (rt
->rate
>= ARRAY_SIZE(rates
))
95 /* disable streaming */
96 ret
= comm_rt
->write16(comm_rt
, 0x02, 0x00, 0x00, 0x00);
98 snd_printk(KERN_ERR PREFIX
"error stopping streaming while "
99 "setting samplerate %d.\n", rates
[rt
->rate
]);
103 ret
= usb_set_interface(device
, 1, rates_altsetting
[rt
->rate
]);
105 snd_printk(KERN_ERR PREFIX
"error setting interface "
106 "altsetting %d for samplerate %d.\n",
107 rates_altsetting
[rt
->rate
], rates
[rt
->rate
]);
111 /* set soundcard clock */
112 ret
= comm_rt
->write16(comm_rt
, 0x02, 0x01, rates_6fire_vl
[rt
->rate
],
113 rates_6fire_vh
[rt
->rate
]);
115 snd_printk(KERN_ERR PREFIX
"error setting samplerate %d.\n",
120 /* enable analog inputs and outputs
121 * (one bit per stereo-channel) */
122 ret
= comm_rt
->write16(comm_rt
, 0x02, 0x02,
123 (1 << (OUT_N_CHANNELS
/ 2)) - 1,
124 (1 << (IN_N_CHANNELS
/ 2)) - 1);
126 snd_printk(KERN_ERR PREFIX
"error initializing analog channels "
127 "while setting samplerate %d.\n",
131 /* disable digital inputs and outputs */
132 ret
= comm_rt
->write16(comm_rt
, 0x02, 0x03, 0x00, 0x00);
134 snd_printk(KERN_ERR PREFIX
"error initializing digital "
135 "channels while setting samplerate %d.\n",
140 ret
= comm_rt
->write16(comm_rt
, 0x02, 0x00, 0x00, 0x01);
142 snd_printk(KERN_ERR PREFIX
"error starting streaming while "
143 "setting samplerate %d.\n", rates
[rt
->rate
]);
147 rt
->in_n_analog
= IN_N_CHANNELS
;
148 rt
->out_n_analog
= OUT_N_CHANNELS
;
149 rt
->in_packet_size
= rates_in_packet_size
[rt
->rate
];
150 rt
->out_packet_size
= rates_out_packet_size
[rt
->rate
];
154 static struct pcm_substream
*usb6fire_pcm_get_substream(
155 struct snd_pcm_substream
*alsa_sub
)
157 struct pcm_runtime
*rt
= snd_pcm_substream_chip(alsa_sub
);
159 if (alsa_sub
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
160 return &rt
->playback
;
161 else if (alsa_sub
->stream
== SNDRV_PCM_STREAM_CAPTURE
)
163 snd_printk(KERN_ERR PREFIX
"error getting pcm substream slot.\n");
167 /* call with stream_mutex locked */
168 static void usb6fire_pcm_stream_stop(struct pcm_runtime
*rt
)
172 if (rt
->stream_state
!= STREAM_DISABLED
) {
173 for (i
= 0; i
< PCM_N_URBS
; i
++) {
174 usb_kill_urb(&rt
->in_urbs
[i
].instance
);
175 usb_kill_urb(&rt
->out_urbs
[i
].instance
);
177 rt
->stream_state
= STREAM_DISABLED
;
181 /* call with stream_mutex locked */
182 static int usb6fire_pcm_stream_start(struct pcm_runtime
*rt
)
187 struct usb_iso_packet_descriptor
*packet
;
189 if (rt
->stream_state
== STREAM_DISABLED
) {
190 /* submit our in urbs */
191 rt
->stream_wait_cond
= false;
192 rt
->stream_state
= STREAM_STARTING
;
193 for (i
= 0; i
< PCM_N_URBS
; i
++) {
194 for (k
= 0; k
< PCM_N_PACKETS_PER_URB
; k
++) {
195 packet
= &rt
->in_urbs
[i
].packets
[k
];
196 packet
->offset
= k
* rt
->in_packet_size
;
197 packet
->length
= rt
->in_packet_size
;
198 packet
->actual_length
= 0;
201 ret
= usb_submit_urb(&rt
->in_urbs
[i
].instance
,
204 usb6fire_pcm_stream_stop(rt
);
209 /* wait for first out urb to return (sent in in urb handler) */
210 wait_event_timeout(rt
->stream_wait_queue
, rt
->stream_wait_cond
,
212 if (rt
->stream_wait_cond
)
213 rt
->stream_state
= STREAM_RUNNING
;
215 usb6fire_pcm_stream_stop(rt
);
222 /* call with substream locked */
223 static void usb6fire_pcm_capture(struct pcm_substream
*sub
, struct pcm_urb
*urb
)
228 unsigned int total_length
= 0;
229 struct pcm_runtime
*rt
= snd_pcm_substream_chip(sub
->instance
);
230 struct snd_pcm_runtime
*alsa_rt
= sub
->instance
->runtime
;
231 u32
*src
= (u32
*) urb
->buffer
;
232 u32
*dest
= (u32
*) (alsa_rt
->dma_area
+ sub
->dma_off
233 * (alsa_rt
->frame_bits
>> 3));
234 u32
*dest_end
= (u32
*) (alsa_rt
->dma_area
+ alsa_rt
->buffer_size
235 * (alsa_rt
->frame_bits
>> 3));
236 int bytes_per_frame
= alsa_rt
->channels
<< 2;
238 for (i
= 0; i
< PCM_N_PACKETS_PER_URB
; i
++) {
239 /* at least 4 header bytes for valid packet.
240 * after that: 32 bits per sample for analog channels */
241 if (urb
->packets
[i
].actual_length
> 4)
242 frame_count
= (urb
->packets
[i
].actual_length
- 4)
243 / (rt
->in_n_analog
<< 2);
247 src
= (u32
*) (urb
->buffer
+ total_length
);
248 src
++; /* skip leading 4 bytes of every packet */
249 total_length
+= urb
->packets
[i
].length
;
250 for (frame
= 0; frame
< frame_count
; frame
++) {
251 memcpy(dest
, src
, bytes_per_frame
);
252 dest
+= alsa_rt
->channels
;
253 src
+= rt
->in_n_analog
;
256 if (dest
== dest_end
) {
258 dest
= (u32
*) alsa_rt
->dma_area
;
264 /* call with substream locked */
265 static void usb6fire_pcm_playback(struct pcm_substream
*sub
,
271 struct pcm_runtime
*rt
= snd_pcm_substream_chip(sub
->instance
);
272 struct snd_pcm_runtime
*alsa_rt
= sub
->instance
->runtime
;
273 u32
*src
= (u32
*) (alsa_rt
->dma_area
+ sub
->dma_off
274 * (alsa_rt
->frame_bits
>> 3));
275 u32
*src_end
= (u32
*) (alsa_rt
->dma_area
+ alsa_rt
->buffer_size
276 * (alsa_rt
->frame_bits
>> 3));
277 u32
*dest
= (u32
*) urb
->buffer
;
278 int bytes_per_frame
= alsa_rt
->channels
<< 2;
280 for (i
= 0; i
< PCM_N_PACKETS_PER_URB
; i
++) {
281 /* at least 4 header bytes for valid packet.
282 * after that: 32 bits per sample for analog channels */
283 if (urb
->packets
[i
].length
> 4)
284 frame_count
= (urb
->packets
[i
].length
- 4)
285 / (rt
->out_n_analog
<< 2);
288 dest
++; /* skip leading 4 bytes of every frame */
289 for (frame
= 0; frame
< frame_count
; frame
++) {
290 memcpy(dest
, src
, bytes_per_frame
);
291 src
+= alsa_rt
->channels
;
292 dest
+= rt
->out_n_analog
;
295 if (src
== src_end
) {
296 src
= (u32
*) alsa_rt
->dma_area
;
303 static void usb6fire_pcm_in_urb_handler(struct urb
*usb_urb
)
305 struct pcm_urb
*in_urb
= usb_urb
->context
;
306 struct pcm_urb
*out_urb
= in_urb
->peer
;
307 struct pcm_runtime
*rt
= in_urb
->chip
->pcm
;
308 struct pcm_substream
*sub
;
310 int total_length
= 0;
317 if (usb_urb
->status
|| rt
->panic
|| rt
->stream_state
== STREAM_STOPPING
)
319 for (i
= 0; i
< PCM_N_PACKETS_PER_URB
; i
++)
320 if (in_urb
->packets
[i
].status
) {
325 if (rt
->stream_state
== STREAM_DISABLED
) {
326 snd_printk(KERN_ERR PREFIX
"internal error: "
327 "stream disabled in in-urb handler.\n");
331 /* receive our capture data */
333 spin_lock_irqsave(&sub
->lock
, flags
);
335 usb6fire_pcm_capture(sub
, in_urb
);
336 if (sub
->period_off
>= sub
->instance
->runtime
->period_size
) {
337 sub
->period_off
%= sub
->instance
->runtime
->period_size
;
338 spin_unlock_irqrestore(&sub
->lock
, flags
);
339 snd_pcm_period_elapsed(sub
->instance
);
341 spin_unlock_irqrestore(&sub
->lock
, flags
);
343 spin_unlock_irqrestore(&sub
->lock
, flags
);
345 /* setup out urb structure */
346 for (i
= 0; i
< PCM_N_PACKETS_PER_URB
; i
++) {
347 out_urb
->packets
[i
].offset
= total_length
;
348 out_urb
->packets
[i
].length
= (in_urb
->packets
[i
].actual_length
349 - 4) / (rt
->in_n_analog
<< 2)
350 * (rt
->out_n_analog
<< 2) + 4;
351 out_urb
->packets
[i
].status
= 0;
352 total_length
+= out_urb
->packets
[i
].length
;
354 memset(out_urb
->buffer
, 0, total_length
);
356 /* now send our playback data (if a free out urb was found) */
358 spin_lock_irqsave(&sub
->lock
, flags
);
360 usb6fire_pcm_playback(sub
, out_urb
);
361 if (sub
->period_off
>= sub
->instance
->runtime
->period_size
) {
362 sub
->period_off
%= sub
->instance
->runtime
->period_size
;
363 spin_unlock_irqrestore(&sub
->lock
, flags
);
364 snd_pcm_period_elapsed(sub
->instance
);
366 spin_unlock_irqrestore(&sub
->lock
, flags
);
368 spin_unlock_irqrestore(&sub
->lock
, flags
);
370 /* setup the 4th byte of each sample (0x40 for analog channels) */
371 dest
= out_urb
->buffer
;
372 for (i
= 0; i
< PCM_N_PACKETS_PER_URB
; i
++)
373 if (out_urb
->packets
[i
].length
>= 4) {
374 frame_count
= (out_urb
->packets
[i
].length
- 4)
375 / (rt
->out_n_analog
<< 2);
378 *(dest
++) = frame_count
;
380 for (frame
= 0; frame
< frame_count
; frame
++)
382 channel
< rt
->out_n_analog
;
384 dest
+= 3; /* skip sample data */
388 usb_submit_urb(&out_urb
->instance
, GFP_ATOMIC
);
389 usb_submit_urb(&in_urb
->instance
, GFP_ATOMIC
);
392 static void usb6fire_pcm_out_urb_handler(struct urb
*usb_urb
)
394 struct pcm_urb
*urb
= usb_urb
->context
;
395 struct pcm_runtime
*rt
= urb
->chip
->pcm
;
397 if (rt
->stream_state
== STREAM_STARTING
) {
398 rt
->stream_wait_cond
= true;
399 wake_up(&rt
->stream_wait_queue
);
403 static int usb6fire_pcm_open(struct snd_pcm_substream
*alsa_sub
)
405 struct pcm_runtime
*rt
= snd_pcm_substream_chip(alsa_sub
);
406 struct pcm_substream
*sub
= NULL
;
407 struct snd_pcm_runtime
*alsa_rt
= alsa_sub
->runtime
;
412 mutex_lock(&rt
->stream_mutex
);
413 alsa_rt
->hw
= pcm_hw
;
415 if (alsa_sub
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
417 alsa_rt
->hw
.rates
= rates_alsaid
[rt
->rate
];
418 alsa_rt
->hw
.channels_max
= OUT_N_CHANNELS
;
420 } else if (alsa_sub
->stream
== SNDRV_PCM_STREAM_CAPTURE
) {
422 alsa_rt
->hw
.rates
= rates_alsaid
[rt
->rate
];
423 alsa_rt
->hw
.channels_max
= IN_N_CHANNELS
;
428 mutex_unlock(&rt
->stream_mutex
);
429 snd_printk(KERN_ERR PREFIX
"invalid stream type.\n");
433 sub
->instance
= alsa_sub
;
435 mutex_unlock(&rt
->stream_mutex
);
439 static int usb6fire_pcm_close(struct snd_pcm_substream
*alsa_sub
)
441 struct pcm_runtime
*rt
= snd_pcm_substream_chip(alsa_sub
);
442 struct pcm_substream
*sub
= usb6fire_pcm_get_substream(alsa_sub
);
448 mutex_lock(&rt
->stream_mutex
);
450 /* deactivate substream */
451 spin_lock_irqsave(&sub
->lock
, flags
);
452 sub
->instance
= NULL
;
454 spin_unlock_irqrestore(&sub
->lock
, flags
);
456 /* all substreams closed? if so, stop streaming */
457 if (!rt
->playback
.instance
&& !rt
->capture
.instance
) {
458 usb6fire_pcm_stream_stop(rt
);
462 mutex_unlock(&rt
->stream_mutex
);
466 static int usb6fire_pcm_hw_params(struct snd_pcm_substream
*alsa_sub
,
467 struct snd_pcm_hw_params
*hw_params
)
469 return snd_pcm_lib_malloc_pages(alsa_sub
,
470 params_buffer_bytes(hw_params
));
473 static int usb6fire_pcm_hw_free(struct snd_pcm_substream
*alsa_sub
)
475 return snd_pcm_lib_free_pages(alsa_sub
);
478 static int usb6fire_pcm_prepare(struct snd_pcm_substream
*alsa_sub
)
480 struct pcm_runtime
*rt
= snd_pcm_substream_chip(alsa_sub
);
481 struct pcm_substream
*sub
= usb6fire_pcm_get_substream(alsa_sub
);
482 struct snd_pcm_runtime
*alsa_rt
= alsa_sub
->runtime
;
491 mutex_lock(&rt
->stream_mutex
);
495 if (rt
->stream_state
== STREAM_DISABLED
) {
496 for (i
= 0; i
< ARRAY_SIZE(rates
); i
++)
497 if (alsa_rt
->rate
== rates
[i
]) {
501 if (i
== ARRAY_SIZE(rates
)) {
502 mutex_unlock(&rt
->stream_mutex
);
503 snd_printk("invalid rate %d in prepare.\n",
508 ret
= usb6fire_pcm_set_rate(rt
);
510 mutex_unlock(&rt
->stream_mutex
);
513 ret
= usb6fire_pcm_stream_start(rt
);
515 mutex_unlock(&rt
->stream_mutex
);
516 snd_printk(KERN_ERR PREFIX
517 "could not start pcm stream.\n");
521 mutex_unlock(&rt
->stream_mutex
);
525 static int usb6fire_pcm_trigger(struct snd_pcm_substream
*alsa_sub
, int cmd
)
527 struct pcm_substream
*sub
= usb6fire_pcm_get_substream(alsa_sub
);
528 struct pcm_runtime
*rt
= snd_pcm_substream_chip(alsa_sub
);
537 case SNDRV_PCM_TRIGGER_START
:
538 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
539 spin_lock_irqsave(&sub
->lock
, flags
);
541 spin_unlock_irqrestore(&sub
->lock
, flags
);
544 case SNDRV_PCM_TRIGGER_STOP
:
545 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
546 spin_lock_irqsave(&sub
->lock
, flags
);
548 spin_unlock_irqrestore(&sub
->lock
, flags
);
556 static snd_pcm_uframes_t
usb6fire_pcm_pointer(
557 struct snd_pcm_substream
*alsa_sub
)
559 struct pcm_substream
*sub
= usb6fire_pcm_get_substream(alsa_sub
);
560 struct pcm_runtime
*rt
= snd_pcm_substream_chip(alsa_sub
);
562 snd_pcm_uframes_t ret
;
564 if (rt
->panic
|| !sub
)
565 return SNDRV_PCM_STATE_XRUN
;
567 spin_lock_irqsave(&sub
->lock
, flags
);
569 spin_unlock_irqrestore(&sub
->lock
, flags
);
573 static struct snd_pcm_ops pcm_ops
= {
574 .open
= usb6fire_pcm_open
,
575 .close
= usb6fire_pcm_close
,
576 .ioctl
= snd_pcm_lib_ioctl
,
577 .hw_params
= usb6fire_pcm_hw_params
,
578 .hw_free
= usb6fire_pcm_hw_free
,
579 .prepare
= usb6fire_pcm_prepare
,
580 .trigger
= usb6fire_pcm_trigger
,
581 .pointer
= usb6fire_pcm_pointer
,
584 static void __devinit
usb6fire_pcm_init_urb(struct pcm_urb
*urb
,
585 struct sfire_chip
*chip
, bool in
, int ep
,
586 void (*handler
)(struct urb
*))
589 usb_init_urb(&urb
->instance
);
590 urb
->instance
.transfer_buffer
= urb
->buffer
;
591 urb
->instance
.transfer_buffer_length
=
592 PCM_N_PACKETS_PER_URB
* PCM_MAX_PACKET_SIZE
;
593 urb
->instance
.dev
= chip
->dev
;
594 urb
->instance
.pipe
= in
? usb_rcvisocpipe(chip
->dev
, ep
)
595 : usb_sndisocpipe(chip
->dev
, ep
);
596 urb
->instance
.interval
= 1;
597 urb
->instance
.transfer_flags
= URB_ISO_ASAP
;
598 urb
->instance
.complete
= handler
;
599 urb
->instance
.context
= urb
;
600 urb
->instance
.number_of_packets
= PCM_N_PACKETS_PER_URB
;
603 int __devinit
usb6fire_pcm_init(struct sfire_chip
*chip
)
608 struct pcm_runtime
*rt
=
609 kzalloc(sizeof(struct pcm_runtime
), GFP_KERNEL
);
615 rt
->stream_state
= STREAM_DISABLED
;
617 init_waitqueue_head(&rt
->stream_wait_queue
);
618 mutex_init(&rt
->stream_mutex
);
620 spin_lock_init(&rt
->playback
.lock
);
621 spin_lock_init(&rt
->capture
.lock
);
623 for (i
= 0; i
< PCM_N_URBS
; i
++) {
624 usb6fire_pcm_init_urb(&rt
->in_urbs
[i
], chip
, true, IN_EP
,
625 usb6fire_pcm_in_urb_handler
);
626 usb6fire_pcm_init_urb(&rt
->out_urbs
[i
], chip
, false, OUT_EP
,
627 usb6fire_pcm_out_urb_handler
);
629 rt
->in_urbs
[i
].peer
= &rt
->out_urbs
[i
];
630 rt
->out_urbs
[i
].peer
= &rt
->in_urbs
[i
];
633 ret
= snd_pcm_new(chip
->card
, "DMX6FireUSB", 0, 1, 1, &pcm
);
636 snd_printk(KERN_ERR PREFIX
"cannot create pcm instance.\n");
640 pcm
->private_data
= rt
;
641 strcpy(pcm
->name
, "DMX 6Fire USB");
642 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_PLAYBACK
, &pcm_ops
);
643 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_CAPTURE
, &pcm_ops
);
645 ret
= snd_pcm_lib_preallocate_pages_for_all(pcm
,
646 SNDRV_DMA_TYPE_CONTINUOUS
,
647 snd_dma_continuous_data(GFP_KERNEL
),
648 MAX_BUFSIZE
, MAX_BUFSIZE
);
651 snd_printk(KERN_ERR PREFIX
652 "error preallocating pcm buffers.\n");
661 void usb6fire_pcm_abort(struct sfire_chip
*chip
)
663 struct pcm_runtime
*rt
= chip
->pcm
;
669 if (rt
->playback
.instance
)
670 snd_pcm_stop(rt
->playback
.instance
,
671 SNDRV_PCM_STATE_XRUN
);
672 if (rt
->capture
.instance
)
673 snd_pcm_stop(rt
->capture
.instance
,
674 SNDRV_PCM_STATE_XRUN
);
676 for (i
= 0; i
< PCM_N_URBS
; i
++) {
677 usb_poison_urb(&rt
->in_urbs
[i
].instance
);
678 usb_poison_urb(&rt
->out_urbs
[i
].instance
);
684 void usb6fire_pcm_destroy(struct sfire_chip
*chip
)