1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Linux driver for TerraTec DMX 6Fire USB
7 * Author: Torsten Schenk <torsten.schenk@zoho.com>
8 * Created: Jan 01, 2011
9 * Copyright: (C) Torsten Schenk
18 OUT_N_CHANNELS
= 6, IN_N_CHANNELS
= 4
21 /* keep next two synced with
22 * FW_EP_W_MAX_PACKET_SIZE[] and RATES_MAX_PACKET_SIZE
23 * and CONTROL_RATE_XXX in control.h */
24 static const int rates_in_packet_size
[] = { 228, 228, 420, 420, 404, 404 };
25 static const int rates_out_packet_size
[] = { 228, 228, 420, 420, 604, 604 };
26 static const int rates
[] = { 44100, 48000, 88200, 96000, 176400, 192000 };
27 static const int rates_alsaid
[] = {
28 SNDRV_PCM_RATE_44100
, SNDRV_PCM_RATE_48000
,
29 SNDRV_PCM_RATE_88200
, SNDRV_PCM_RATE_96000
,
30 SNDRV_PCM_RATE_176400
, SNDRV_PCM_RATE_192000
};
32 enum { /* settings for pcm */
33 OUT_EP
= 6, IN_EP
= 2, MAX_BUFSIZE
= 128 * 1024
36 enum { /* pcm streaming states */
37 STREAM_DISABLED
, /* no pcm streaming */
38 STREAM_STARTING
, /* pcm streaming requested, waiting to become ready */
39 STREAM_RUNNING
, /* pcm streaming running */
43 static const struct snd_pcm_hardware pcm_hw
= {
44 .info
= SNDRV_PCM_INFO_MMAP
|
45 SNDRV_PCM_INFO_INTERLEAVED
|
46 SNDRV_PCM_INFO_BLOCK_TRANSFER
|
47 SNDRV_PCM_INFO_MMAP_VALID
|
50 .formats
= SNDRV_PCM_FMTBIT_S24_LE
| SNDRV_PCM_FMTBIT_S32_LE
,
52 .rates
= SNDRV_PCM_RATE_44100
|
53 SNDRV_PCM_RATE_48000
|
54 SNDRV_PCM_RATE_88200
|
55 SNDRV_PCM_RATE_96000
|
56 SNDRV_PCM_RATE_176400
|
57 SNDRV_PCM_RATE_192000
,
62 .channels_max
= 0, /* set in pcm_open, depending on capture/playback */
63 .buffer_bytes_max
= MAX_BUFSIZE
,
64 .period_bytes_min
= PCM_N_PACKETS_PER_URB
* (PCM_MAX_PACKET_SIZE
- 4),
65 .period_bytes_max
= MAX_BUFSIZE
,
70 static int usb6fire_pcm_set_rate(struct pcm_runtime
*rt
)
73 struct control_runtime
*ctrl_rt
= rt
->chip
->control
;
75 ctrl_rt
->usb_streaming
= false;
76 ret
= ctrl_rt
->update_streaming(ctrl_rt
);
78 dev_err(&rt
->chip
->dev
->dev
,
79 "error stopping streaming while setting samplerate %d.\n",
84 ret
= ctrl_rt
->set_rate(ctrl_rt
, rt
->rate
);
86 dev_err(&rt
->chip
->dev
->dev
,
87 "error setting samplerate %d.\n",
92 ret
= ctrl_rt
->set_channels(ctrl_rt
, OUT_N_CHANNELS
, IN_N_CHANNELS
,
95 dev_err(&rt
->chip
->dev
->dev
,
96 "error initializing channels while setting samplerate %d.\n",
101 ctrl_rt
->usb_streaming
= true;
102 ret
= ctrl_rt
->update_streaming(ctrl_rt
);
104 dev_err(&rt
->chip
->dev
->dev
,
105 "error starting streaming while setting samplerate %d.\n",
110 rt
->in_n_analog
= IN_N_CHANNELS
;
111 rt
->out_n_analog
= OUT_N_CHANNELS
;
112 rt
->in_packet_size
= rates_in_packet_size
[rt
->rate
];
113 rt
->out_packet_size
= rates_out_packet_size
[rt
->rate
];
117 static struct pcm_substream
*usb6fire_pcm_get_substream(
118 struct snd_pcm_substream
*alsa_sub
)
120 struct pcm_runtime
*rt
= snd_pcm_substream_chip(alsa_sub
);
122 if (alsa_sub
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
123 return &rt
->playback
;
124 else if (alsa_sub
->stream
== SNDRV_PCM_STREAM_CAPTURE
)
126 dev_err(&rt
->chip
->dev
->dev
, "error getting pcm substream slot.\n");
130 /* call with stream_mutex locked */
131 static void usb6fire_pcm_stream_stop(struct pcm_runtime
*rt
)
134 struct control_runtime
*ctrl_rt
= rt
->chip
->control
;
136 if (rt
->stream_state
!= STREAM_DISABLED
) {
138 rt
->stream_state
= STREAM_STOPPING
;
140 for (i
= 0; i
< PCM_N_URBS
; i
++) {
141 usb_kill_urb(&rt
->in_urbs
[i
].instance
);
142 usb_kill_urb(&rt
->out_urbs
[i
].instance
);
144 ctrl_rt
->usb_streaming
= false;
145 ctrl_rt
->update_streaming(ctrl_rt
);
146 rt
->stream_state
= STREAM_DISABLED
;
150 /* call with stream_mutex locked */
151 static int usb6fire_pcm_stream_start(struct pcm_runtime
*rt
)
156 struct usb_iso_packet_descriptor
*packet
;
158 if (rt
->stream_state
== STREAM_DISABLED
) {
159 /* submit our in urbs */
160 rt
->stream_wait_cond
= false;
161 rt
->stream_state
= STREAM_STARTING
;
162 for (i
= 0; i
< PCM_N_URBS
; i
++) {
163 for (k
= 0; k
< PCM_N_PACKETS_PER_URB
; k
++) {
164 packet
= &rt
->in_urbs
[i
].packets
[k
];
165 packet
->offset
= k
* rt
->in_packet_size
;
166 packet
->length
= rt
->in_packet_size
;
167 packet
->actual_length
= 0;
170 ret
= usb_submit_urb(&rt
->in_urbs
[i
].instance
,
173 usb6fire_pcm_stream_stop(rt
);
178 /* wait for first out urb to return (sent in in urb handler) */
179 wait_event_timeout(rt
->stream_wait_queue
, rt
->stream_wait_cond
,
181 if (rt
->stream_wait_cond
)
182 rt
->stream_state
= STREAM_RUNNING
;
184 usb6fire_pcm_stream_stop(rt
);
191 /* call with substream locked */
192 static void usb6fire_pcm_capture(struct pcm_substream
*sub
, struct pcm_urb
*urb
)
197 unsigned int total_length
= 0;
198 struct pcm_runtime
*rt
= snd_pcm_substream_chip(sub
->instance
);
199 struct snd_pcm_runtime
*alsa_rt
= sub
->instance
->runtime
;
201 u32
*dest
= (u32
*) (alsa_rt
->dma_area
+ sub
->dma_off
202 * (alsa_rt
->frame_bits
>> 3));
203 u32
*dest_end
= (u32
*) (alsa_rt
->dma_area
+ alsa_rt
->buffer_size
204 * (alsa_rt
->frame_bits
>> 3));
205 int bytes_per_frame
= alsa_rt
->channels
<< 2;
207 for (i
= 0; i
< PCM_N_PACKETS_PER_URB
; i
++) {
208 /* at least 4 header bytes for valid packet.
209 * after that: 32 bits per sample for analog channels */
210 if (urb
->packets
[i
].actual_length
> 4)
211 frame_count
= (urb
->packets
[i
].actual_length
- 4)
212 / (rt
->in_n_analog
<< 2);
216 if (alsa_rt
->format
== SNDRV_PCM_FORMAT_S24_LE
)
217 src
= (u32
*) (urb
->buffer
+ total_length
);
218 else if (alsa_rt
->format
== SNDRV_PCM_FORMAT_S32_LE
)
219 src
= (u32
*) (urb
->buffer
- 1 + total_length
);
222 src
++; /* skip leading 4 bytes of every packet */
223 total_length
+= urb
->packets
[i
].length
;
224 for (frame
= 0; frame
< frame_count
; frame
++) {
225 memcpy(dest
, src
, bytes_per_frame
);
226 dest
+= alsa_rt
->channels
;
227 src
+= rt
->in_n_analog
;
230 if (dest
== dest_end
) {
232 dest
= (u32
*) alsa_rt
->dma_area
;
238 /* call with substream locked */
239 static void usb6fire_pcm_playback(struct pcm_substream
*sub
,
245 struct pcm_runtime
*rt
= snd_pcm_substream_chip(sub
->instance
);
246 struct snd_pcm_runtime
*alsa_rt
= sub
->instance
->runtime
;
247 u32
*src
= (u32
*) (alsa_rt
->dma_area
+ sub
->dma_off
248 * (alsa_rt
->frame_bits
>> 3));
249 u32
*src_end
= (u32
*) (alsa_rt
->dma_area
+ alsa_rt
->buffer_size
250 * (alsa_rt
->frame_bits
>> 3));
252 int bytes_per_frame
= alsa_rt
->channels
<< 2;
254 if (alsa_rt
->format
== SNDRV_PCM_FORMAT_S32_LE
)
255 dest
= (u32
*) (urb
->buffer
- 1);
256 else if (alsa_rt
->format
== SNDRV_PCM_FORMAT_S24_LE
)
257 dest
= (u32
*) (urb
->buffer
);
259 dev_err(&rt
->chip
->dev
->dev
, "Unknown sample format.");
263 for (i
= 0; i
< PCM_N_PACKETS_PER_URB
; i
++) {
264 /* at least 4 header bytes for valid packet.
265 * after that: 32 bits per sample for analog channels */
266 if (urb
->packets
[i
].length
> 4)
267 frame_count
= (urb
->packets
[i
].length
- 4)
268 / (rt
->out_n_analog
<< 2);
271 dest
++; /* skip leading 4 bytes of every frame */
272 for (frame
= 0; frame
< frame_count
; frame
++) {
273 memcpy(dest
, src
, bytes_per_frame
);
274 src
+= alsa_rt
->channels
;
275 dest
+= rt
->out_n_analog
;
278 if (src
== src_end
) {
279 src
= (u32
*) alsa_rt
->dma_area
;
286 static void usb6fire_pcm_in_urb_handler(struct urb
*usb_urb
)
288 struct pcm_urb
*in_urb
= usb_urb
->context
;
289 struct pcm_urb
*out_urb
= in_urb
->peer
;
290 struct pcm_runtime
*rt
= in_urb
->chip
->pcm
;
291 struct pcm_substream
*sub
;
293 int total_length
= 0;
300 if (usb_urb
->status
|| rt
->panic
|| rt
->stream_state
== STREAM_STOPPING
)
302 for (i
= 0; i
< PCM_N_PACKETS_PER_URB
; i
++)
303 if (in_urb
->packets
[i
].status
) {
308 if (rt
->stream_state
== STREAM_DISABLED
) {
309 dev_err(&rt
->chip
->dev
->dev
,
310 "internal error: stream disabled in in-urb handler.\n");
314 /* receive our capture data */
316 spin_lock_irqsave(&sub
->lock
, flags
);
318 usb6fire_pcm_capture(sub
, in_urb
);
319 if (sub
->period_off
>= sub
->instance
->runtime
->period_size
) {
320 sub
->period_off
%= sub
->instance
->runtime
->period_size
;
321 spin_unlock_irqrestore(&sub
->lock
, flags
);
322 snd_pcm_period_elapsed(sub
->instance
);
324 spin_unlock_irqrestore(&sub
->lock
, flags
);
326 spin_unlock_irqrestore(&sub
->lock
, flags
);
328 /* setup out urb structure */
329 for (i
= 0; i
< PCM_N_PACKETS_PER_URB
; i
++) {
330 out_urb
->packets
[i
].offset
= total_length
;
331 out_urb
->packets
[i
].length
= (in_urb
->packets
[i
].actual_length
332 - 4) / (rt
->in_n_analog
<< 2)
333 * (rt
->out_n_analog
<< 2) + 4;
334 out_urb
->packets
[i
].status
= 0;
335 total_length
+= out_urb
->packets
[i
].length
;
337 memset(out_urb
->buffer
, 0, total_length
);
339 /* now send our playback data (if a free out urb was found) */
341 spin_lock_irqsave(&sub
->lock
, flags
);
343 usb6fire_pcm_playback(sub
, out_urb
);
344 if (sub
->period_off
>= sub
->instance
->runtime
->period_size
) {
345 sub
->period_off
%= sub
->instance
->runtime
->period_size
;
346 spin_unlock_irqrestore(&sub
->lock
, flags
);
347 snd_pcm_period_elapsed(sub
->instance
);
349 spin_unlock_irqrestore(&sub
->lock
, flags
);
351 spin_unlock_irqrestore(&sub
->lock
, flags
);
353 /* setup the 4th byte of each sample (0x40 for analog channels) */
354 dest
= out_urb
->buffer
;
355 for (i
= 0; i
< PCM_N_PACKETS_PER_URB
; i
++)
356 if (out_urb
->packets
[i
].length
>= 4) {
357 frame_count
= (out_urb
->packets
[i
].length
- 4)
358 / (rt
->out_n_analog
<< 2);
361 *(dest
++) = frame_count
;
363 for (frame
= 0; frame
< frame_count
; frame
++)
365 channel
< rt
->out_n_analog
;
367 dest
+= 3; /* skip sample data */
371 usb_submit_urb(&out_urb
->instance
, GFP_ATOMIC
);
372 usb_submit_urb(&in_urb
->instance
, GFP_ATOMIC
);
375 static void usb6fire_pcm_out_urb_handler(struct urb
*usb_urb
)
377 struct pcm_urb
*urb
= usb_urb
->context
;
378 struct pcm_runtime
*rt
= urb
->chip
->pcm
;
380 if (rt
->stream_state
== STREAM_STARTING
) {
381 rt
->stream_wait_cond
= true;
382 wake_up(&rt
->stream_wait_queue
);
386 static int usb6fire_pcm_open(struct snd_pcm_substream
*alsa_sub
)
388 struct pcm_runtime
*rt
= snd_pcm_substream_chip(alsa_sub
);
389 struct pcm_substream
*sub
= NULL
;
390 struct snd_pcm_runtime
*alsa_rt
= alsa_sub
->runtime
;
395 mutex_lock(&rt
->stream_mutex
);
396 alsa_rt
->hw
= pcm_hw
;
398 if (alsa_sub
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
399 if (rt
->rate
< ARRAY_SIZE(rates
))
400 alsa_rt
->hw
.rates
= rates_alsaid
[rt
->rate
];
401 alsa_rt
->hw
.channels_max
= OUT_N_CHANNELS
;
403 } else if (alsa_sub
->stream
== SNDRV_PCM_STREAM_CAPTURE
) {
404 if (rt
->rate
< ARRAY_SIZE(rates
))
405 alsa_rt
->hw
.rates
= rates_alsaid
[rt
->rate
];
406 alsa_rt
->hw
.channels_max
= IN_N_CHANNELS
;
411 mutex_unlock(&rt
->stream_mutex
);
412 dev_err(&rt
->chip
->dev
->dev
, "invalid stream type.\n");
416 sub
->instance
= alsa_sub
;
418 mutex_unlock(&rt
->stream_mutex
);
422 static int usb6fire_pcm_close(struct snd_pcm_substream
*alsa_sub
)
424 struct pcm_runtime
*rt
= snd_pcm_substream_chip(alsa_sub
);
425 struct pcm_substream
*sub
= usb6fire_pcm_get_substream(alsa_sub
);
431 mutex_lock(&rt
->stream_mutex
);
433 /* deactivate substream */
434 spin_lock_irqsave(&sub
->lock
, flags
);
435 sub
->instance
= NULL
;
437 spin_unlock_irqrestore(&sub
->lock
, flags
);
439 /* all substreams closed? if so, stop streaming */
440 if (!rt
->playback
.instance
&& !rt
->capture
.instance
) {
441 usb6fire_pcm_stream_stop(rt
);
442 rt
->rate
= ARRAY_SIZE(rates
);
445 mutex_unlock(&rt
->stream_mutex
);
449 static int usb6fire_pcm_prepare(struct snd_pcm_substream
*alsa_sub
)
451 struct pcm_runtime
*rt
= snd_pcm_substream_chip(alsa_sub
);
452 struct pcm_substream
*sub
= usb6fire_pcm_get_substream(alsa_sub
);
453 struct snd_pcm_runtime
*alsa_rt
= alsa_sub
->runtime
;
461 mutex_lock(&rt
->stream_mutex
);
465 if (rt
->stream_state
== STREAM_DISABLED
) {
466 for (rt
->rate
= 0; rt
->rate
< ARRAY_SIZE(rates
); rt
->rate
++)
467 if (alsa_rt
->rate
== rates
[rt
->rate
])
469 if (rt
->rate
== ARRAY_SIZE(rates
)) {
470 mutex_unlock(&rt
->stream_mutex
);
471 dev_err(&rt
->chip
->dev
->dev
,
472 "invalid rate %d in prepare.\n",
477 ret
= usb6fire_pcm_set_rate(rt
);
479 mutex_unlock(&rt
->stream_mutex
);
482 ret
= usb6fire_pcm_stream_start(rt
);
484 mutex_unlock(&rt
->stream_mutex
);
485 dev_err(&rt
->chip
->dev
->dev
,
486 "could not start pcm stream.\n");
490 mutex_unlock(&rt
->stream_mutex
);
494 static int usb6fire_pcm_trigger(struct snd_pcm_substream
*alsa_sub
, int cmd
)
496 struct pcm_substream
*sub
= usb6fire_pcm_get_substream(alsa_sub
);
497 struct pcm_runtime
*rt
= snd_pcm_substream_chip(alsa_sub
);
506 case SNDRV_PCM_TRIGGER_START
:
507 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
508 spin_lock_irqsave(&sub
->lock
, flags
);
510 spin_unlock_irqrestore(&sub
->lock
, flags
);
513 case SNDRV_PCM_TRIGGER_STOP
:
514 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
515 spin_lock_irqsave(&sub
->lock
, flags
);
517 spin_unlock_irqrestore(&sub
->lock
, flags
);
525 static snd_pcm_uframes_t
usb6fire_pcm_pointer(
526 struct snd_pcm_substream
*alsa_sub
)
528 struct pcm_substream
*sub
= usb6fire_pcm_get_substream(alsa_sub
);
529 struct pcm_runtime
*rt
= snd_pcm_substream_chip(alsa_sub
);
531 snd_pcm_uframes_t ret
;
533 if (rt
->panic
|| !sub
)
534 return SNDRV_PCM_POS_XRUN
;
536 spin_lock_irqsave(&sub
->lock
, flags
);
538 spin_unlock_irqrestore(&sub
->lock
, flags
);
542 static const struct snd_pcm_ops pcm_ops
= {
543 .open
= usb6fire_pcm_open
,
544 .close
= usb6fire_pcm_close
,
545 .prepare
= usb6fire_pcm_prepare
,
546 .trigger
= usb6fire_pcm_trigger
,
547 .pointer
= usb6fire_pcm_pointer
,
550 static void usb6fire_pcm_init_urb(struct pcm_urb
*urb
,
551 struct sfire_chip
*chip
, bool in
, int ep
,
552 void (*handler
)(struct urb
*))
555 usb_init_urb(&urb
->instance
);
556 urb
->instance
.transfer_buffer
= urb
->buffer
;
557 urb
->instance
.transfer_buffer_length
=
558 PCM_N_PACKETS_PER_URB
* PCM_MAX_PACKET_SIZE
;
559 urb
->instance
.dev
= chip
->dev
;
560 urb
->instance
.pipe
= in
? usb_rcvisocpipe(chip
->dev
, ep
)
561 : usb_sndisocpipe(chip
->dev
, ep
);
562 urb
->instance
.interval
= 1;
563 urb
->instance
.complete
= handler
;
564 urb
->instance
.context
= urb
;
565 urb
->instance
.number_of_packets
= PCM_N_PACKETS_PER_URB
;
568 static int usb6fire_pcm_buffers_init(struct pcm_runtime
*rt
)
572 for (i
= 0; i
< PCM_N_URBS
; i
++) {
573 rt
->out_urbs
[i
].buffer
= kcalloc(PCM_MAX_PACKET_SIZE
,
574 PCM_N_PACKETS_PER_URB
,
576 if (!rt
->out_urbs
[i
].buffer
)
578 rt
->in_urbs
[i
].buffer
= kcalloc(PCM_MAX_PACKET_SIZE
,
579 PCM_N_PACKETS_PER_URB
,
581 if (!rt
->in_urbs
[i
].buffer
)
587 static void usb6fire_pcm_buffers_destroy(struct pcm_runtime
*rt
)
591 for (i
= 0; i
< PCM_N_URBS
; i
++) {
592 kfree(rt
->out_urbs
[i
].buffer
);
593 kfree(rt
->in_urbs
[i
].buffer
);
597 int usb6fire_pcm_init(struct sfire_chip
*chip
)
602 struct pcm_runtime
*rt
=
603 kzalloc(sizeof(struct pcm_runtime
), GFP_KERNEL
);
608 ret
= usb6fire_pcm_buffers_init(rt
);
610 usb6fire_pcm_buffers_destroy(rt
);
616 rt
->stream_state
= STREAM_DISABLED
;
617 rt
->rate
= ARRAY_SIZE(rates
);
618 init_waitqueue_head(&rt
->stream_wait_queue
);
619 mutex_init(&rt
->stream_mutex
);
621 spin_lock_init(&rt
->playback
.lock
);
622 spin_lock_init(&rt
->capture
.lock
);
624 for (i
= 0; i
< PCM_N_URBS
; i
++) {
625 usb6fire_pcm_init_urb(&rt
->in_urbs
[i
], chip
, true, IN_EP
,
626 usb6fire_pcm_in_urb_handler
);
627 usb6fire_pcm_init_urb(&rt
->out_urbs
[i
], chip
, false, OUT_EP
,
628 usb6fire_pcm_out_urb_handler
);
630 rt
->in_urbs
[i
].peer
= &rt
->out_urbs
[i
];
631 rt
->out_urbs
[i
].peer
= &rt
->in_urbs
[i
];
634 ret
= snd_pcm_new(chip
->card
, "DMX6FireUSB", 0, 1, 1, &pcm
);
636 usb6fire_pcm_buffers_destroy(rt
);
638 dev_err(&chip
->dev
->dev
, "cannot create pcm instance.\n");
642 pcm
->private_data
= rt
;
643 strcpy(pcm
->name
, "DMX 6Fire USB");
644 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_PLAYBACK
, &pcm_ops
);
645 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_CAPTURE
, &pcm_ops
);
646 snd_pcm_set_managed_buffer_all(pcm
, SNDRV_DMA_TYPE_VMALLOC
, NULL
, 0, 0);
654 void usb6fire_pcm_abort(struct sfire_chip
*chip
)
656 struct pcm_runtime
*rt
= chip
->pcm
;
662 if (rt
->playback
.instance
)
663 snd_pcm_stop_xrun(rt
->playback
.instance
);
665 if (rt
->capture
.instance
)
666 snd_pcm_stop_xrun(rt
->capture
.instance
);
668 for (i
= 0; i
< PCM_N_URBS
; i
++) {
669 usb_poison_urb(&rt
->in_urbs
[i
].instance
);
670 usb_poison_urb(&rt
->out_urbs
[i
].instance
);
676 void usb6fire_pcm_destroy(struct sfire_chip
*chip
)
678 struct pcm_runtime
*rt
= chip
->pcm
;
680 usb6fire_pcm_buffers_destroy(rt
);