2 * Linux driver for M2Tech hiFace compatible devices
4 * Copyright 2012-2013 (C) M2TECH S.r.l and Amarula Solutions B.V.
6 * Authors: Michael Trimarchi <michael@amarulasolutions.com>
7 * Antonio Ospite <ao2@amarulasolutions.com>
9 * The driver is based on the work done in TerraTec DMX 6Fire USB
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.
17 #include <linux/slab.h>
18 #include <sound/pcm.h>
25 #define PCM_PACKET_SIZE 4096
26 #define PCM_BUFFER_SIZE (2 * PCM_N_URBS * PCM_PACKET_SIZE)
29 struct hiface_chip
*chip
;
32 struct usb_anchor submitted
;
36 struct pcm_substream
{
38 struct snd_pcm_substream
*instance
;
41 snd_pcm_uframes_t dma_off
; /* current position in alsa dma_area */
42 snd_pcm_uframes_t period_off
; /* current position in current period */
45 enum { /* pcm streaming states */
46 STREAM_DISABLED
, /* no pcm streaming */
47 STREAM_STARTING
, /* pcm streaming requested, waiting to become ready */
48 STREAM_RUNNING
, /* pcm streaming running */
53 struct hiface_chip
*chip
;
54 struct snd_pcm
*instance
;
56 struct pcm_substream playback
;
57 bool panic
; /* if set driver won't do anymore pcm on device */
59 struct pcm_urb out_urbs
[PCM_N_URBS
];
61 struct mutex stream_mutex
;
62 u8 stream_state
; /* one of STREAM_XXX */
64 wait_queue_head_t stream_wait_queue
;
65 bool stream_wait_cond
;
68 static const unsigned int rates
[] = { 44100, 48000, 88200, 96000, 176400, 192000,
70 static const struct snd_pcm_hw_constraint_list constraints_extra_rates
= {
71 .count
= ARRAY_SIZE(rates
),
76 static const struct snd_pcm_hardware pcm_hw
= {
77 .info
= SNDRV_PCM_INFO_MMAP
|
78 SNDRV_PCM_INFO_INTERLEAVED
|
79 SNDRV_PCM_INFO_BLOCK_TRANSFER
|
80 SNDRV_PCM_INFO_PAUSE
|
81 SNDRV_PCM_INFO_MMAP_VALID
|
84 .formats
= SNDRV_PCM_FMTBIT_S32_LE
,
86 .rates
= SNDRV_PCM_RATE_44100
|
87 SNDRV_PCM_RATE_48000
|
88 SNDRV_PCM_RATE_88200
|
89 SNDRV_PCM_RATE_96000
|
90 SNDRV_PCM_RATE_176400
|
91 SNDRV_PCM_RATE_192000
,
94 .rate_max
= 192000, /* changes in hiface_pcm_open to support extra rates */
97 .buffer_bytes_max
= PCM_BUFFER_SIZE
,
98 .period_bytes_min
= PCM_PACKET_SIZE
,
99 .period_bytes_max
= PCM_BUFFER_SIZE
,
104 /* message values used to change the sample rate */
105 #define HIFACE_SET_RATE_REQUEST 0xb0
107 #define HIFACE_RATE_44100 0x43
108 #define HIFACE_RATE_48000 0x4b
109 #define HIFACE_RATE_88200 0x42
110 #define HIFACE_RATE_96000 0x4a
111 #define HIFACE_RATE_176400 0x40
112 #define HIFACE_RATE_192000 0x48
113 #define HIFACE_RATE_352800 0x58
114 #define HIFACE_RATE_384000 0x68
116 static int hiface_pcm_set_rate(struct pcm_runtime
*rt
, unsigned int rate
)
118 struct usb_device
*device
= rt
->chip
->dev
;
122 /* We are already sure that the rate is supported here thanks to
127 rate_value
= HIFACE_RATE_44100
;
130 rate_value
= HIFACE_RATE_48000
;
133 rate_value
= HIFACE_RATE_88200
;
136 rate_value
= HIFACE_RATE_96000
;
139 rate_value
= HIFACE_RATE_176400
;
142 rate_value
= HIFACE_RATE_192000
;
145 rate_value
= HIFACE_RATE_352800
;
148 rate_value
= HIFACE_RATE_384000
;
151 dev_err(&device
->dev
, "Unsupported rate %d\n", rate
);
156 * USBIO: Vendor 0xb0(wValue=0x0043, wIndex=0x0000)
157 * 43 b0 43 00 00 00 00 00
158 * USBIO: Vendor 0xb0(wValue=0x004b, wIndex=0x0000)
159 * 43 b0 4b 00 00 00 00 00
160 * This control message doesn't have any ack from the
163 ret
= usb_control_msg(device
, usb_sndctrlpipe(device
, 0),
164 HIFACE_SET_RATE_REQUEST
,
165 USB_DIR_OUT
| USB_TYPE_VENDOR
| USB_RECIP_OTHER
,
166 rate_value
, 0, NULL
, 0, 100);
168 dev_err(&device
->dev
, "Error setting samplerate %d.\n", rate
);
175 static struct pcm_substream
*hiface_pcm_get_substream(struct snd_pcm_substream
178 struct pcm_runtime
*rt
= snd_pcm_substream_chip(alsa_sub
);
179 struct device
*device
= &rt
->chip
->dev
->dev
;
181 if (alsa_sub
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
182 return &rt
->playback
;
184 dev_err(device
, "Error getting pcm substream slot.\n");
188 /* call with stream_mutex locked */
189 static void hiface_pcm_stream_stop(struct pcm_runtime
*rt
)
193 if (rt
->stream_state
!= STREAM_DISABLED
) {
194 rt
->stream_state
= STREAM_STOPPING
;
196 for (i
= 0; i
< PCM_N_URBS
; i
++) {
197 time
= usb_wait_anchor_empty_timeout(
198 &rt
->out_urbs
[i
].submitted
, 100);
200 usb_kill_anchored_urbs(
201 &rt
->out_urbs
[i
].submitted
);
202 usb_kill_urb(&rt
->out_urbs
[i
].instance
);
205 rt
->stream_state
= STREAM_DISABLED
;
209 /* call with stream_mutex locked */
210 static int hiface_pcm_stream_start(struct pcm_runtime
*rt
)
215 if (rt
->stream_state
== STREAM_DISABLED
) {
217 /* reset panic state when starting a new stream */
220 /* submit our out urbs zero init */
221 rt
->stream_state
= STREAM_STARTING
;
222 for (i
= 0; i
< PCM_N_URBS
; i
++) {
223 memset(rt
->out_urbs
[i
].buffer
, 0, PCM_PACKET_SIZE
);
224 usb_anchor_urb(&rt
->out_urbs
[i
].instance
,
225 &rt
->out_urbs
[i
].submitted
);
226 ret
= usb_submit_urb(&rt
->out_urbs
[i
].instance
,
229 hiface_pcm_stream_stop(rt
);
234 /* wait for first out urb to return (sent in in urb handler) */
235 wait_event_timeout(rt
->stream_wait_queue
, rt
->stream_wait_cond
,
237 if (rt
->stream_wait_cond
) {
238 struct device
*device
= &rt
->chip
->dev
->dev
;
239 dev_dbg(device
, "%s: Stream is running wakeup event\n",
241 rt
->stream_state
= STREAM_RUNNING
;
243 hiface_pcm_stream_stop(rt
);
250 /* The hardware wants word-swapped 32-bit values */
251 static void memcpy_swahw32(u8
*dest
, u8
*src
, unsigned int n
)
255 for (i
= 0; i
< n
/ 4; i
++)
256 ((u32
*)dest
)[i
] = swahw32(((u32
*)src
)[i
]);
259 /* call with substream locked */
260 /* returns true if a period elapsed */
261 static bool hiface_pcm_playback(struct pcm_substream
*sub
, struct pcm_urb
*urb
)
263 struct snd_pcm_runtime
*alsa_rt
= sub
->instance
->runtime
;
264 struct device
*device
= &urb
->chip
->dev
->dev
;
266 unsigned int pcm_buffer_size
;
268 WARN_ON(alsa_rt
->format
!= SNDRV_PCM_FORMAT_S32_LE
);
270 pcm_buffer_size
= snd_pcm_lib_buffer_bytes(sub
->instance
);
272 if (sub
->dma_off
+ PCM_PACKET_SIZE
<= pcm_buffer_size
) {
273 dev_dbg(device
, "%s: (1) buffer_size %#x dma_offset %#x\n", __func__
,
274 (unsigned int) pcm_buffer_size
,
275 (unsigned int) sub
->dma_off
);
277 source
= alsa_rt
->dma_area
+ sub
->dma_off
;
278 memcpy_swahw32(urb
->buffer
, source
, PCM_PACKET_SIZE
);
280 /* wrap around at end of ring buffer */
283 dev_dbg(device
, "%s: (2) buffer_size %#x dma_offset %#x\n", __func__
,
284 (unsigned int) pcm_buffer_size
,
285 (unsigned int) sub
->dma_off
);
287 len
= pcm_buffer_size
- sub
->dma_off
;
289 source
= alsa_rt
->dma_area
+ sub
->dma_off
;
290 memcpy_swahw32(urb
->buffer
, source
, len
);
292 source
= alsa_rt
->dma_area
;
293 memcpy_swahw32(urb
->buffer
+ len
, source
,
294 PCM_PACKET_SIZE
- len
);
296 sub
->dma_off
+= PCM_PACKET_SIZE
;
297 if (sub
->dma_off
>= pcm_buffer_size
)
298 sub
->dma_off
-= pcm_buffer_size
;
300 sub
->period_off
+= PCM_PACKET_SIZE
;
301 if (sub
->period_off
>= alsa_rt
->period_size
) {
302 sub
->period_off
%= alsa_rt
->period_size
;
308 static void hiface_pcm_out_urb_handler(struct urb
*usb_urb
)
310 struct pcm_urb
*out_urb
= usb_urb
->context
;
311 struct pcm_runtime
*rt
= out_urb
->chip
->pcm
;
312 struct pcm_substream
*sub
;
313 bool do_period_elapsed
= false;
317 if (rt
->panic
|| rt
->stream_state
== STREAM_STOPPING
)
320 if (unlikely(usb_urb
->status
== -ENOENT
|| /* unlinked */
321 usb_urb
->status
== -ENODEV
|| /* device removed */
322 usb_urb
->status
== -ECONNRESET
|| /* unlinked */
323 usb_urb
->status
== -ESHUTDOWN
)) { /* device disabled */
327 if (rt
->stream_state
== STREAM_STARTING
) {
328 rt
->stream_wait_cond
= true;
329 wake_up(&rt
->stream_wait_queue
);
332 /* now send our playback data (if a free out urb was found) */
334 spin_lock_irqsave(&sub
->lock
, flags
);
336 do_period_elapsed
= hiface_pcm_playback(sub
, out_urb
);
338 memset(out_urb
->buffer
, 0, PCM_PACKET_SIZE
);
340 spin_unlock_irqrestore(&sub
->lock
, flags
);
342 if (do_period_elapsed
)
343 snd_pcm_period_elapsed(sub
->instance
);
345 ret
= usb_submit_urb(&out_urb
->instance
, GFP_ATOMIC
);
355 static int hiface_pcm_open(struct snd_pcm_substream
*alsa_sub
)
357 struct pcm_runtime
*rt
= snd_pcm_substream_chip(alsa_sub
);
358 struct pcm_substream
*sub
= NULL
;
359 struct snd_pcm_runtime
*alsa_rt
= alsa_sub
->runtime
;
365 mutex_lock(&rt
->stream_mutex
);
366 alsa_rt
->hw
= pcm_hw
;
368 if (alsa_sub
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
372 struct device
*device
= &rt
->chip
->dev
->dev
;
373 mutex_unlock(&rt
->stream_mutex
);
374 dev_err(device
, "Invalid stream type\n");
378 if (rt
->extra_freq
) {
379 alsa_rt
->hw
.rates
|= SNDRV_PCM_RATE_KNOT
;
380 alsa_rt
->hw
.rate_max
= 384000;
382 /* explicit constraints needed as we added SNDRV_PCM_RATE_KNOT */
383 ret
= snd_pcm_hw_constraint_list(alsa_sub
->runtime
, 0,
384 SNDRV_PCM_HW_PARAM_RATE
,
385 &constraints_extra_rates
);
387 mutex_unlock(&rt
->stream_mutex
);
392 sub
->instance
= alsa_sub
;
394 mutex_unlock(&rt
->stream_mutex
);
398 static int hiface_pcm_close(struct snd_pcm_substream
*alsa_sub
)
400 struct pcm_runtime
*rt
= snd_pcm_substream_chip(alsa_sub
);
401 struct pcm_substream
*sub
= hiface_pcm_get_substream(alsa_sub
);
407 mutex_lock(&rt
->stream_mutex
);
409 hiface_pcm_stream_stop(rt
);
411 /* deactivate substream */
412 spin_lock_irqsave(&sub
->lock
, flags
);
413 sub
->instance
= NULL
;
415 spin_unlock_irqrestore(&sub
->lock
, flags
);
418 mutex_unlock(&rt
->stream_mutex
);
422 static int hiface_pcm_hw_params(struct snd_pcm_substream
*alsa_sub
,
423 struct snd_pcm_hw_params
*hw_params
)
425 return snd_pcm_lib_alloc_vmalloc_buffer(alsa_sub
,
426 params_buffer_bytes(hw_params
));
429 static int hiface_pcm_hw_free(struct snd_pcm_substream
*alsa_sub
)
431 return snd_pcm_lib_free_vmalloc_buffer(alsa_sub
);
434 static int hiface_pcm_prepare(struct snd_pcm_substream
*alsa_sub
)
436 struct pcm_runtime
*rt
= snd_pcm_substream_chip(alsa_sub
);
437 struct pcm_substream
*sub
= hiface_pcm_get_substream(alsa_sub
);
438 struct snd_pcm_runtime
*alsa_rt
= alsa_sub
->runtime
;
446 mutex_lock(&rt
->stream_mutex
);
451 if (rt
->stream_state
== STREAM_DISABLED
) {
453 ret
= hiface_pcm_set_rate(rt
, alsa_rt
->rate
);
455 mutex_unlock(&rt
->stream_mutex
);
458 ret
= hiface_pcm_stream_start(rt
);
460 mutex_unlock(&rt
->stream_mutex
);
464 mutex_unlock(&rt
->stream_mutex
);
468 static int hiface_pcm_trigger(struct snd_pcm_substream
*alsa_sub
, int cmd
)
470 struct pcm_substream
*sub
= hiface_pcm_get_substream(alsa_sub
);
471 struct pcm_runtime
*rt
= snd_pcm_substream_chip(alsa_sub
);
479 case SNDRV_PCM_TRIGGER_START
:
480 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
481 spin_lock_irq(&sub
->lock
);
483 spin_unlock_irq(&sub
->lock
);
486 case SNDRV_PCM_TRIGGER_STOP
:
487 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
488 spin_lock_irq(&sub
->lock
);
490 spin_unlock_irq(&sub
->lock
);
498 static snd_pcm_uframes_t
hiface_pcm_pointer(struct snd_pcm_substream
*alsa_sub
)
500 struct pcm_substream
*sub
= hiface_pcm_get_substream(alsa_sub
);
501 struct pcm_runtime
*rt
= snd_pcm_substream_chip(alsa_sub
);
503 snd_pcm_uframes_t dma_offset
;
505 if (rt
->panic
|| !sub
)
506 return SNDRV_PCM_POS_XRUN
;
508 spin_lock_irqsave(&sub
->lock
, flags
);
509 dma_offset
= sub
->dma_off
;
510 spin_unlock_irqrestore(&sub
->lock
, flags
);
511 return bytes_to_frames(alsa_sub
->runtime
, dma_offset
);
514 static struct snd_pcm_ops pcm_ops
= {
515 .open
= hiface_pcm_open
,
516 .close
= hiface_pcm_close
,
517 .ioctl
= snd_pcm_lib_ioctl
,
518 .hw_params
= hiface_pcm_hw_params
,
519 .hw_free
= hiface_pcm_hw_free
,
520 .prepare
= hiface_pcm_prepare
,
521 .trigger
= hiface_pcm_trigger
,
522 .pointer
= hiface_pcm_pointer
,
523 .page
= snd_pcm_lib_get_vmalloc_page
,
524 .mmap
= snd_pcm_lib_mmap_vmalloc
,
527 static int hiface_pcm_init_urb(struct pcm_urb
*urb
,
528 struct hiface_chip
*chip
,
530 void (*handler
)(struct urb
*))
533 usb_init_urb(&urb
->instance
);
535 urb
->buffer
= kzalloc(PCM_PACKET_SIZE
, GFP_KERNEL
);
539 usb_fill_bulk_urb(&urb
->instance
, chip
->dev
,
540 usb_sndbulkpipe(chip
->dev
, ep
), (void *)urb
->buffer
,
541 PCM_PACKET_SIZE
, handler
, urb
);
542 init_usb_anchor(&urb
->submitted
);
547 void hiface_pcm_abort(struct hiface_chip
*chip
)
549 struct pcm_runtime
*rt
= chip
->pcm
;
554 mutex_lock(&rt
->stream_mutex
);
555 hiface_pcm_stream_stop(rt
);
556 mutex_unlock(&rt
->stream_mutex
);
560 static void hiface_pcm_destroy(struct hiface_chip
*chip
)
562 struct pcm_runtime
*rt
= chip
->pcm
;
565 for (i
= 0; i
< PCM_N_URBS
; i
++)
566 kfree(rt
->out_urbs
[i
].buffer
);
572 static void hiface_pcm_free(struct snd_pcm
*pcm
)
574 struct pcm_runtime
*rt
= pcm
->private_data
;
577 hiface_pcm_destroy(rt
->chip
);
580 int hiface_pcm_init(struct hiface_chip
*chip
, u8 extra_freq
)
585 struct pcm_runtime
*rt
;
587 rt
= kzalloc(sizeof(*rt
), GFP_KERNEL
);
592 rt
->stream_state
= STREAM_DISABLED
;
596 init_waitqueue_head(&rt
->stream_wait_queue
);
597 mutex_init(&rt
->stream_mutex
);
598 spin_lock_init(&rt
->playback
.lock
);
600 for (i
= 0; i
< PCM_N_URBS
; i
++)
601 hiface_pcm_init_urb(&rt
->out_urbs
[i
], chip
, OUT_EP
,
602 hiface_pcm_out_urb_handler
);
604 ret
= snd_pcm_new(chip
->card
, "USB-SPDIF Audio", 0, 1, 0, &pcm
);
607 dev_err(&chip
->dev
->dev
, "Cannot create pcm instance\n");
611 pcm
->private_data
= rt
;
612 pcm
->private_free
= hiface_pcm_free
;
614 strlcpy(pcm
->name
, "USB-SPDIF Audio", sizeof(pcm
->name
));
615 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_PLAYBACK
, &pcm_ops
);