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.
7 * Authors: Michael Trimarchi <michael@amarulasolutions.com>
8 * Antonio Ospite <ao2@amarulasolutions.com>
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(device, usb_sndctrlpipe(device, 0),
160 HIFACE_SET_RATE_REQUEST,
161 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
162 rate_value, 0, NULL, 0, 100);
164 dev_err(&device->dev, "Error setting samplerate %d.\n", rate);
171 static struct pcm_substream *hiface_pcm_get_substream(struct snd_pcm_substream
174 struct pcm_runtime *rt = snd_pcm_substream_chip(alsa_sub);
175 struct device *device = &rt->chip->dev->dev;
177 if (alsa_sub->stream == SNDRV_PCM_STREAM_PLAYBACK)
178 return &rt->playback;
180 dev_err(device, "Error getting pcm substream slot.\n");
184 /* call with stream_mutex locked */
185 static void hiface_pcm_stream_stop(struct pcm_runtime *rt)
189 if (rt->stream_state != STREAM_DISABLED) {
190 rt->stream_state = STREAM_STOPPING;
192 for (i = 0; i < PCM_N_URBS; i++) {
193 time = usb_wait_anchor_empty_timeout(
194 &rt->out_urbs[i].submitted, 100);
196 usb_kill_anchored_urbs(
197 &rt->out_urbs[i].submitted);
198 usb_kill_urb(&rt->out_urbs[i].instance);
201 rt->stream_state = STREAM_DISABLED;
205 /* call with stream_mutex locked */
206 static int hiface_pcm_stream_start(struct pcm_runtime *rt)
211 if (rt->stream_state == STREAM_DISABLED) {
213 /* reset panic state when starting a new stream */
216 /* submit our out urbs zero init */
217 rt->stream_state = STREAM_STARTING;
218 for (i = 0; i < PCM_N_URBS; i++) {
219 memset(rt->out_urbs[i].buffer, 0, PCM_PACKET_SIZE);
220 usb_anchor_urb(&rt->out_urbs[i].instance,
221 &rt->out_urbs[i].submitted);
222 ret = usb_submit_urb(&rt->out_urbs[i].instance,
225 hiface_pcm_stream_stop(rt);
230 /* wait for first out urb to return (sent in in urb handler) */
231 wait_event_timeout(rt->stream_wait_queue, rt->stream_wait_cond,
233 if (rt->stream_wait_cond) {
234 struct device *device = &rt->chip->dev->dev;
235 dev_dbg(device, "%s: Stream is running wakeup event\n",
237 rt->stream_state = STREAM_RUNNING;
239 hiface_pcm_stream_stop(rt);
246 /* The hardware wants word-swapped 32-bit values */
247 static void memcpy_swahw32(u8 *dest, u8 *src, unsigned int n)
251 for (i = 0; i < n / 4; i++)
252 ((u32 *)dest)[i] = swahw32(((u32 *)src)[i]);
255 /* call with substream locked */
256 /* returns true if a period elapsed */
257 static bool hiface_pcm_playback(struct pcm_substream *sub, struct pcm_urb *urb)
259 struct snd_pcm_runtime *alsa_rt = sub->instance->runtime;
260 struct device *device = &urb->chip->dev->dev;
262 unsigned int pcm_buffer_size;
264 WARN_ON(alsa_rt->format != SNDRV_PCM_FORMAT_S32_LE);
266 pcm_buffer_size = snd_pcm_lib_buffer_bytes(sub->instance);
268 if (sub->dma_off + PCM_PACKET_SIZE <= pcm_buffer_size) {
269 dev_dbg(device, "%s: (1) buffer_size %#x dma_offset %#x\n", __func__,
270 (unsigned int) pcm_buffer_size,
271 (unsigned int) sub->dma_off);
273 source = alsa_rt->dma_area + sub->dma_off;
274 memcpy_swahw32(urb->buffer, source, PCM_PACKET_SIZE);
276 /* wrap around at end of ring buffer */
279 dev_dbg(device, "%s: (2) buffer_size %#x dma_offset %#x\n", __func__,
280 (unsigned int) pcm_buffer_size,
281 (unsigned int) sub->dma_off);
283 len = pcm_buffer_size - sub->dma_off;
285 source = alsa_rt->dma_area + sub->dma_off;
286 memcpy_swahw32(urb->buffer, source, len);
288 source = alsa_rt->dma_area;
289 memcpy_swahw32(urb->buffer + len, source,
290 PCM_PACKET_SIZE - len);
292 sub->dma_off += PCM_PACKET_SIZE;
293 if (sub->dma_off >= pcm_buffer_size)
294 sub->dma_off -= pcm_buffer_size;
296 sub->period_off += PCM_PACKET_SIZE;
297 if (sub->period_off >= alsa_rt->period_size) {
298 sub->period_off %= alsa_rt->period_size;
304 static void hiface_pcm_out_urb_handler(struct urb *usb_urb)
306 struct pcm_urb *out_urb = usb_urb->context;
307 struct pcm_runtime *rt = out_urb->chip->pcm;
308 struct pcm_substream *sub;
309 bool do_period_elapsed = false;
313 if (rt->panic || rt->stream_state == STREAM_STOPPING)
316 if (unlikely(usb_urb->status == -ENOENT || /* unlinked */
317 usb_urb->status == -ENODEV || /* device removed */
318 usb_urb->status == -ECONNRESET || /* unlinked */
319 usb_urb->status == -ESHUTDOWN)) { /* device disabled */
323 if (rt->stream_state == STREAM_STARTING) {
324 rt->stream_wait_cond = true;
325 wake_up(&rt->stream_wait_queue);
328 /* now send our playback data (if a free out urb was found) */
330 spin_lock_irqsave(&sub->lock, flags);
332 do_period_elapsed = hiface_pcm_playback(sub, out_urb);
334 memset(out_urb->buffer, 0, PCM_PACKET_SIZE);
336 spin_unlock_irqrestore(&sub->lock, flags);
338 if (do_period_elapsed)
339 snd_pcm_period_elapsed(sub->instance);
341 ret = usb_submit_urb(&out_urb->instance, GFP_ATOMIC);
351 static int hiface_pcm_open(struct snd_pcm_substream *alsa_sub)
353 struct pcm_runtime *rt = snd_pcm_substream_chip(alsa_sub);
354 struct pcm_substream *sub = NULL;
355 struct snd_pcm_runtime *alsa_rt = alsa_sub->runtime;
361 mutex_lock(&rt->stream_mutex);
362 alsa_rt->hw = pcm_hw;
364 if (alsa_sub->stream == SNDRV_PCM_STREAM_PLAYBACK)
368 struct device *device = &rt->chip->dev->dev;
369 mutex_unlock(&rt->stream_mutex);
370 dev_err(device, "Invalid stream type\n");
374 if (rt->extra_freq) {
375 alsa_rt->hw.rates |= SNDRV_PCM_RATE_KNOT;
376 alsa_rt->hw.rate_max = 384000;
378 /* explicit constraints needed as we added SNDRV_PCM_RATE_KNOT */
379 ret = snd_pcm_hw_constraint_list(alsa_sub->runtime, 0,
380 SNDRV_PCM_HW_PARAM_RATE,
381 &constraints_extra_rates);
383 mutex_unlock(&rt->stream_mutex);
388 sub->instance = alsa_sub;
390 mutex_unlock(&rt->stream_mutex);
394 static int hiface_pcm_close(struct snd_pcm_substream *alsa_sub)
396 struct pcm_runtime *rt = snd_pcm_substream_chip(alsa_sub);
397 struct pcm_substream *sub = hiface_pcm_get_substream(alsa_sub);
403 mutex_lock(&rt->stream_mutex);
405 hiface_pcm_stream_stop(rt);
407 /* deactivate substream */
408 spin_lock_irqsave(&sub->lock, flags);
409 sub->instance = NULL;
411 spin_unlock_irqrestore(&sub->lock, flags);
414 mutex_unlock(&rt->stream_mutex);
418 static int hiface_pcm_hw_params(struct snd_pcm_substream *alsa_sub,
419 struct snd_pcm_hw_params *hw_params)
421 return snd_pcm_lib_alloc_vmalloc_buffer(alsa_sub,
422 params_buffer_bytes(hw_params));
425 static int hiface_pcm_hw_free(struct snd_pcm_substream *alsa_sub)
427 return snd_pcm_lib_free_vmalloc_buffer(alsa_sub);
430 static int hiface_pcm_prepare(struct snd_pcm_substream *alsa_sub)
432 struct pcm_runtime *rt = snd_pcm_substream_chip(alsa_sub);
433 struct pcm_substream *sub = hiface_pcm_get_substream(alsa_sub);
434 struct snd_pcm_runtime *alsa_rt = alsa_sub->runtime;
442 mutex_lock(&rt->stream_mutex);
444 hiface_pcm_stream_stop(rt);
449 if (rt->stream_state == STREAM_DISABLED) {
451 ret = hiface_pcm_set_rate(rt, alsa_rt->rate);
453 mutex_unlock(&rt->stream_mutex);
456 ret = hiface_pcm_stream_start(rt);
458 mutex_unlock(&rt->stream_mutex);
462 mutex_unlock(&rt->stream_mutex);
466 static int hiface_pcm_trigger(struct snd_pcm_substream *alsa_sub, int cmd)
468 struct pcm_substream *sub = hiface_pcm_get_substream(alsa_sub);
469 struct pcm_runtime *rt = snd_pcm_substream_chip(alsa_sub);
477 case SNDRV_PCM_TRIGGER_START:
478 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
479 spin_lock_irq(&sub->lock);
481 spin_unlock_irq(&sub->lock);
484 case SNDRV_PCM_TRIGGER_STOP:
485 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
486 spin_lock_irq(&sub->lock);
488 spin_unlock_irq(&sub->lock);
496 static snd_pcm_uframes_t hiface_pcm_pointer(struct snd_pcm_substream *alsa_sub)
498 struct pcm_substream *sub = hiface_pcm_get_substream(alsa_sub);
499 struct pcm_runtime *rt = snd_pcm_substream_chip(alsa_sub);
501 snd_pcm_uframes_t dma_offset;
503 if (rt->panic || !sub)
504 return SNDRV_PCM_POS_XRUN;
506 spin_lock_irqsave(&sub->lock, flags);
507 dma_offset = sub->dma_off;
508 spin_unlock_irqrestore(&sub->lock, flags);
509 return bytes_to_frames(alsa_sub->runtime, dma_offset);
512 static const struct snd_pcm_ops pcm_ops = {
513 .open = hiface_pcm_open,
514 .close = hiface_pcm_close,
515 .ioctl = snd_pcm_lib_ioctl,
516 .hw_params = hiface_pcm_hw_params,
517 .hw_free = hiface_pcm_hw_free,
518 .prepare = hiface_pcm_prepare,
519 .trigger = hiface_pcm_trigger,
520 .pointer = hiface_pcm_pointer,
521 .page = snd_pcm_lib_get_vmalloc_page,
524 static int hiface_pcm_init_urb(struct pcm_urb *urb,
525 struct hiface_chip *chip,
527 void (*handler)(struct urb *))
530 usb_init_urb(&urb->instance);
532 urb->buffer = kzalloc(PCM_PACKET_SIZE, GFP_KERNEL);
536 usb_fill_bulk_urb(&urb->instance, chip->dev,
537 usb_sndbulkpipe(chip->dev, ep), (void *)urb->buffer,
538 PCM_PACKET_SIZE, handler, urb);
539 if (usb_urb_ep_type_check(&urb->instance))
541 init_usb_anchor(&urb->submitted);
546 void hiface_pcm_abort(struct hiface_chip *chip)
548 struct pcm_runtime *rt = chip->pcm;
553 mutex_lock(&rt->stream_mutex);
554 hiface_pcm_stream_stop(rt);
555 mutex_unlock(&rt->stream_mutex);
559 static void hiface_pcm_destroy(struct hiface_chip *chip)
561 struct pcm_runtime *rt = chip->pcm;
564 for (i = 0; i < PCM_N_URBS; i++)
565 kfree(rt->out_urbs[i].buffer);
571 static void hiface_pcm_free(struct snd_pcm *pcm)
573 struct pcm_runtime *rt = pcm->private_data;
576 hiface_pcm_destroy(rt->chip);
579 int hiface_pcm_init(struct hiface_chip *chip, u8 extra_freq)
584 struct pcm_runtime *rt;
586 rt = kzalloc(sizeof(*rt), GFP_KERNEL);
591 rt->stream_state = STREAM_DISABLED;
595 init_waitqueue_head(&rt->stream_wait_queue);
596 mutex_init(&rt->stream_mutex);
597 spin_lock_init(&rt->playback.lock);
599 for (i = 0; i < PCM_N_URBS; i++) {
600 ret = hiface_pcm_init_urb(&rt->out_urbs[i], chip, OUT_EP,
601 hiface_pcm_out_urb_handler);
606 ret = snd_pcm_new(chip->card, "USB-SPDIF Audio", 0, 1, 0, &pcm);
609 dev_err(&chip->dev->dev, "Cannot create pcm instance\n");
613 pcm->private_data = rt;
614 pcm->private_free = hiface_pcm_free;
616 strlcpy(pcm->name, "USB-SPDIF Audio", sizeof(pcm->name));
617 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &pcm_ops);