1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Linux driver for M2Tech hiFace compatible devices
5 * Copyright 2012-2013 (C) M2TECH S.r.l and Amarula Solutions B.V.
10 * The driver is based on the work done in TerraTec DMX 6Fire USB
13 #include <linux/slab.h>
14 #include <sound/pcm.h>
21 #define PCM_PACKET_SIZE 4096
22 #define PCM_BUFFER_SIZE (2 * PCM_N_URBS * PCM_PACKET_SIZE)
25 struct hiface_chip *chip;
28 struct usb_anchor submitted;
32 struct pcm_substream {
34 struct snd_pcm_substream *instance;
37 snd_pcm_uframes_t dma_off; /* current position in alsa dma_area */
38 snd_pcm_uframes_t period_off; /* current position in current period */
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 */
49 struct hiface_chip *chip;
50 struct snd_pcm *instance;
52 struct pcm_substream playback;
53 bool panic; /* if set driver won't do anymore pcm on device */
55 struct pcm_urb out_urbs[PCM_N_URBS];
57 struct mutex stream_mutex;
58 u8 stream_state; /* one of STREAM_XXX */
60 wait_queue_head_t stream_wait_queue;
61 bool stream_wait_cond;
64 static const unsigned int rates[] = { 44100, 48000, 88200, 96000, 176400, 192000,
66 static const struct snd_pcm_hw_constraint_list constraints_extra_rates = {
67 .count = ARRAY_SIZE(rates),
72 static const struct snd_pcm_hardware pcm_hw = {
73 .info = SNDRV_PCM_INFO_MMAP |
74 SNDRV_PCM_INFO_INTERLEAVED |
75 SNDRV_PCM_INFO_BLOCK_TRANSFER |
76 SNDRV_PCM_INFO_PAUSE |
77 SNDRV_PCM_INFO_MMAP_VALID |
80 .formats = SNDRV_PCM_FMTBIT_S32_LE,
82 .rates = SNDRV_PCM_RATE_44100 |
83 SNDRV_PCM_RATE_48000 |
84 SNDRV_PCM_RATE_88200 |
85 SNDRV_PCM_RATE_96000 |
86 SNDRV_PCM_RATE_176400 |
87 SNDRV_PCM_RATE_192000,
90 .rate_max = 192000, /* changes in hiface_pcm_open to support extra rates */
93 .buffer_bytes_max = PCM_BUFFER_SIZE,
94 .period_bytes_min = PCM_PACKET_SIZE,
95 .period_bytes_max = PCM_BUFFER_SIZE,
100 /* message values used to change the sample rate */
101 #define HIFACE_SET_RATE_REQUEST 0xb0
103 #define HIFACE_RATE_44100 0x43
104 #define HIFACE_RATE_48000 0x4b
105 #define HIFACE_RATE_88200 0x42
106 #define HIFACE_RATE_96000 0x4a
107 #define HIFACE_RATE_176400 0x40
108 #define HIFACE_RATE_192000 0x48
109 #define HIFACE_RATE_352800 0x58
110 #define HIFACE_RATE_384000 0x68
112 static int hiface_pcm_set_rate(struct pcm_runtime *rt, unsigned int rate)
114 struct usb_device *device = rt->chip->dev;
118 /* We are already sure that the rate is supported here thanks to
123 rate_value = HIFACE_RATE_44100;
126 rate_value = HIFACE_RATE_48000;
129 rate_value = HIFACE_RATE_88200;
132 rate_value = HIFACE_RATE_96000;
135 rate_value = HIFACE_RATE_176400;
138 rate_value = HIFACE_RATE_192000;
141 rate_value = HIFACE_RATE_352800;
144 rate_value = HIFACE_RATE_384000;
147 dev_err(&device->dev, "Unsupported rate %d\n", rate);
152 * USBIO: Vendor 0xb0(wValue=0x0043, wIndex=0x0000)
153 * 43 b0 43 00 00 00 00 00
154 * USBIO: Vendor 0xb0(wValue=0x004b, wIndex=0x0000)
155 * 43 b0 4b 00 00 00 00 00
156 * This control message doesn't have any ack from the
159 ret = usb_control_msg_send(device, 0,
160 HIFACE_SET_RATE_REQUEST,
161 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
162 rate_value, 0, NULL, 0, 100, GFP_KERNEL);
164 dev_err(&device->dev, "Error setting samplerate %d.\n", rate);
169 static struct pcm_substream *hiface_pcm_get_substream(struct snd_pcm_substream
172 struct pcm_runtime *rt = snd_pcm_substream_chip(alsa_sub);
173 struct device *device = &rt->chip->dev->dev;
175 if (alsa_sub->stream == SNDRV_PCM_STREAM_PLAYBACK)
176 return &rt->playback;
178 dev_err(device, "Error getting pcm substream slot.\n");
182 /* call with stream_mutex locked */
183 static void hiface_pcm_stream_stop(struct pcm_runtime *rt)
187 if (rt->stream_state != STREAM_DISABLED) {
188 rt->stream_state = STREAM_STOPPING;
190 for (i = 0; i < PCM_N_URBS; i++) {
191 time = usb_wait_anchor_empty_timeout(
192 &rt->out_urbs[i].submitted, 100);
194 usb_kill_anchored_urbs(
195 &rt->out_urbs[i].submitted);
196 usb_kill_urb(&rt->out_urbs[i].instance);
199 rt->stream_state = STREAM_DISABLED;
203 /* call with stream_mutex locked */
204 static int hiface_pcm_stream_start(struct pcm_runtime *rt)
209 if (rt->stream_state == STREAM_DISABLED) {
211 /* reset panic state when starting a new stream */
214 /* submit our out urbs zero init */
215 rt->stream_state = STREAM_STARTING;
216 for (i = 0; i < PCM_N_URBS; i++) {
217 memset(rt->out_urbs[i].buffer, 0, PCM_PACKET_SIZE);
218 usb_anchor_urb(&rt->out_urbs[i].instance,
219 &rt->out_urbs[i].submitted);
220 ret = usb_submit_urb(&rt->out_urbs[i].instance,
223 hiface_pcm_stream_stop(rt);
228 /* wait for first out urb to return (sent in urb handler) */
229 wait_event_timeout(rt->stream_wait_queue, rt->stream_wait_cond,
231 if (rt->stream_wait_cond) {
232 struct device *device = &rt->chip->dev->dev;
233 dev_dbg(device, "%s: Stream is running wakeup event\n",
235 rt->stream_state = STREAM_RUNNING;
237 hiface_pcm_stream_stop(rt);
244 /* The hardware wants word-swapped 32-bit values */
245 static void memcpy_swahw32(u8 *dest, u8 *src, unsigned int n)
249 for (i = 0; i < n / 4; i++)
250 ((u32 *)dest)[i] = swahw32(((u32 *)src)[i]);
253 /* call with substream locked */
254 /* returns true if a period elapsed */
255 static bool hiface_pcm_playback(struct pcm_substream *sub, struct pcm_urb *urb)
257 struct snd_pcm_runtime *alsa_rt = sub->instance->runtime;
258 struct device *device = &urb->chip->dev->dev;
260 unsigned int pcm_buffer_size;
262 WARN_ON(alsa_rt->format != SNDRV_PCM_FORMAT_S32_LE);
264 pcm_buffer_size = snd_pcm_lib_buffer_bytes(sub->instance);
266 if (sub->dma_off + PCM_PACKET_SIZE <= pcm_buffer_size) {
267 dev_dbg(device, "%s: (1) buffer_size %#x dma_offset %#x\n", __func__,
268 (unsigned int) pcm_buffer_size,
269 (unsigned int) sub->dma_off);
271 source = alsa_rt->dma_area + sub->dma_off;
272 memcpy_swahw32(urb->buffer, source, PCM_PACKET_SIZE);
274 /* wrap around at end of ring buffer */
277 dev_dbg(device, "%s: (2) buffer_size %#x dma_offset %#x\n", __func__,
278 (unsigned int) pcm_buffer_size,
279 (unsigned int) sub->dma_off);
281 len = pcm_buffer_size - sub->dma_off;
283 source = alsa_rt->dma_area + sub->dma_off;
284 memcpy_swahw32(urb->buffer, source, len);
286 source = alsa_rt->dma_area;
287 memcpy_swahw32(urb->buffer + len, source,
288 PCM_PACKET_SIZE - len);
290 sub->dma_off += PCM_PACKET_SIZE;
291 if (sub->dma_off >= pcm_buffer_size)
292 sub->dma_off -= pcm_buffer_size;
294 sub->period_off += PCM_PACKET_SIZE;
295 if (sub->period_off >= alsa_rt->period_size) {
296 sub->period_off %= alsa_rt->period_size;
302 static void hiface_pcm_out_urb_handler(struct urb *usb_urb)
304 struct pcm_urb *out_urb = usb_urb->context;
305 struct pcm_runtime *rt = out_urb->chip->pcm;
306 struct pcm_substream *sub;
307 bool do_period_elapsed = false;
311 if (rt->panic || rt->stream_state == STREAM_STOPPING)
314 if (unlikely(usb_urb->status == -ENOENT || /* unlinked */
315 usb_urb->status == -ENODEV || /* device removed */
316 usb_urb->status == -ECONNRESET || /* unlinked */
317 usb_urb->status == -ESHUTDOWN)) { /* device disabled */
321 if (rt->stream_state == STREAM_STARTING) {
322 rt->stream_wait_cond = true;
323 wake_up(&rt->stream_wait_queue);
326 /* now send our playback data (if a free out urb was found) */
328 spin_lock_irqsave(&sub->lock, flags);
330 do_period_elapsed = hiface_pcm_playback(sub, out_urb);
332 memset(out_urb->buffer, 0, PCM_PACKET_SIZE);
334 spin_unlock_irqrestore(&sub->lock, flags);
336 if (do_period_elapsed)
337 snd_pcm_period_elapsed(sub->instance);
339 ret = usb_submit_urb(&out_urb->instance, GFP_ATOMIC);
349 static int hiface_pcm_open(struct snd_pcm_substream *alsa_sub)
351 struct pcm_runtime *rt = snd_pcm_substream_chip(alsa_sub);
352 struct pcm_substream *sub = NULL;
353 struct snd_pcm_runtime *alsa_rt = alsa_sub->runtime;
359 mutex_lock(&rt->stream_mutex);
360 alsa_rt->hw = pcm_hw;
362 if (alsa_sub->stream == SNDRV_PCM_STREAM_PLAYBACK)
366 struct device *device = &rt->chip->dev->dev;
367 mutex_unlock(&rt->stream_mutex);
368 dev_err(device, "Invalid stream type\n");
372 if (rt->extra_freq) {
373 alsa_rt->hw.rates |= SNDRV_PCM_RATE_KNOT;
374 alsa_rt->hw.rate_max = 384000;
376 /* explicit constraints needed as we added SNDRV_PCM_RATE_KNOT */
377 ret = snd_pcm_hw_constraint_list(alsa_sub->runtime, 0,
378 SNDRV_PCM_HW_PARAM_RATE,
379 &constraints_extra_rates);
381 mutex_unlock(&rt->stream_mutex);
386 sub->instance = alsa_sub;
388 mutex_unlock(&rt->stream_mutex);
392 static int hiface_pcm_close(struct snd_pcm_substream *alsa_sub)
394 struct pcm_runtime *rt = snd_pcm_substream_chip(alsa_sub);
395 struct pcm_substream *sub = hiface_pcm_get_substream(alsa_sub);
401 mutex_lock(&rt->stream_mutex);
403 hiface_pcm_stream_stop(rt);
405 /* deactivate substream */
406 spin_lock_irqsave(&sub->lock, flags);
407 sub->instance = NULL;
409 spin_unlock_irqrestore(&sub->lock, flags);
412 mutex_unlock(&rt->stream_mutex);
416 static int hiface_pcm_prepare(struct snd_pcm_substream *alsa_sub)
418 struct pcm_runtime *rt = snd_pcm_substream_chip(alsa_sub);
419 struct pcm_substream *sub = hiface_pcm_get_substream(alsa_sub);
420 struct snd_pcm_runtime *alsa_rt = alsa_sub->runtime;
428 mutex_lock(&rt->stream_mutex);
430 hiface_pcm_stream_stop(rt);
435 if (rt->stream_state == STREAM_DISABLED) {
437 ret = hiface_pcm_set_rate(rt, alsa_rt->rate);
439 mutex_unlock(&rt->stream_mutex);
442 ret = hiface_pcm_stream_start(rt);
444 mutex_unlock(&rt->stream_mutex);
448 mutex_unlock(&rt->stream_mutex);
452 static int hiface_pcm_trigger(struct snd_pcm_substream *alsa_sub, int cmd)
454 struct pcm_substream *sub = hiface_pcm_get_substream(alsa_sub);
455 struct pcm_runtime *rt = snd_pcm_substream_chip(alsa_sub);
463 case SNDRV_PCM_TRIGGER_START:
464 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
465 spin_lock_irq(&sub->lock);
467 spin_unlock_irq(&sub->lock);
470 case SNDRV_PCM_TRIGGER_STOP:
471 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
472 spin_lock_irq(&sub->lock);
474 spin_unlock_irq(&sub->lock);
482 static snd_pcm_uframes_t hiface_pcm_pointer(struct snd_pcm_substream *alsa_sub)
484 struct pcm_substream *sub = hiface_pcm_get_substream(alsa_sub);
485 struct pcm_runtime *rt = snd_pcm_substream_chip(alsa_sub);
487 snd_pcm_uframes_t dma_offset;
489 if (rt->panic || !sub)
490 return SNDRV_PCM_POS_XRUN;
492 spin_lock_irqsave(&sub->lock, flags);
493 dma_offset = sub->dma_off;
494 spin_unlock_irqrestore(&sub->lock, flags);
495 return bytes_to_frames(alsa_sub->runtime, dma_offset);
498 static const struct snd_pcm_ops pcm_ops = {
499 .open = hiface_pcm_open,
500 .close = hiface_pcm_close,
501 .prepare = hiface_pcm_prepare,
502 .trigger = hiface_pcm_trigger,
503 .pointer = hiface_pcm_pointer,
506 static int hiface_pcm_init_urb(struct pcm_urb *urb,
507 struct hiface_chip *chip,
509 void (*handler)(struct urb *))
512 usb_init_urb(&urb->instance);
514 urb->buffer = kzalloc(PCM_PACKET_SIZE, GFP_KERNEL);
518 usb_fill_bulk_urb(&urb->instance, chip->dev,
519 usb_sndbulkpipe(chip->dev, ep), (void *)urb->buffer,
520 PCM_PACKET_SIZE, handler, urb);
521 if (usb_urb_ep_type_check(&urb->instance))
523 init_usb_anchor(&urb->submitted);
528 void hiface_pcm_abort(struct hiface_chip *chip)
530 struct pcm_runtime *rt = chip->pcm;
535 mutex_lock(&rt->stream_mutex);
536 hiface_pcm_stream_stop(rt);
537 mutex_unlock(&rt->stream_mutex);
541 static void hiface_pcm_destroy(struct hiface_chip *chip)
543 struct pcm_runtime *rt = chip->pcm;
546 for (i = 0; i < PCM_N_URBS; i++)
547 kfree(rt->out_urbs[i].buffer);
553 static void hiface_pcm_free(struct snd_pcm *pcm)
555 struct pcm_runtime *rt = pcm->private_data;
558 hiface_pcm_destroy(rt->chip);
561 int hiface_pcm_init(struct hiface_chip *chip, u8 extra_freq)
566 struct pcm_runtime *rt;
568 rt = kzalloc(sizeof(*rt), GFP_KERNEL);
573 rt->stream_state = STREAM_DISABLED;
577 init_waitqueue_head(&rt->stream_wait_queue);
578 mutex_init(&rt->stream_mutex);
579 spin_lock_init(&rt->playback.lock);
581 for (i = 0; i < PCM_N_URBS; i++) {
582 ret = hiface_pcm_init_urb(&rt->out_urbs[i], chip, OUT_EP,
583 hiface_pcm_out_urb_handler);
588 ret = snd_pcm_new(chip->card, "USB-SPDIF Audio", 0, 1, 0, &pcm);
590 dev_err(&chip->dev->dev, "Cannot create pcm instance\n");
594 pcm->private_data = rt;
595 pcm->private_free = hiface_pcm_free;
597 strscpy(pcm->name, "USB-SPDIF Audio", sizeof(pcm->name));
598 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &pcm_ops);
599 snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_VMALLOC,
608 for (i = 0; i < PCM_N_URBS; i++)
609 kfree(rt->out_urbs[i].buffer);