xref: /openbmc/linux/sound/usb/hiface/pcm.c (revision fc772314)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Linux driver for M2Tech hiFace compatible devices
4  *
5  * Copyright 2012-2013 (C) M2TECH S.r.l and Amarula Solutions B.V.
6  *
7  * Authors:  Michael Trimarchi <michael@amarulasolutions.com>
8  *           Antonio Ospite <ao2@amarulasolutions.com>
9  *
10  * The driver is based on the work done in TerraTec DMX 6Fire USB
11  */
12 
13 #include <linux/slab.h>
14 #include <sound/pcm.h>
15 
16 #include "pcm.h"
17 #include "chip.h"
18 
19 #define OUT_EP          0x2
20 #define PCM_N_URBS      8
21 #define PCM_PACKET_SIZE 4096
22 #define PCM_BUFFER_SIZE (2 * PCM_N_URBS * PCM_PACKET_SIZE)
23 
24 struct pcm_urb {
25 	struct hiface_chip *chip;
26 
27 	struct urb instance;
28 	struct usb_anchor submitted;
29 	u8 *buffer;
30 };
31 
32 struct pcm_substream {
33 	spinlock_t lock;
34 	struct snd_pcm_substream *instance;
35 
36 	bool active;
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 */
39 };
40 
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 */
45 	STREAM_STOPPING
46 };
47 
48 struct pcm_runtime {
49 	struct hiface_chip *chip;
50 	struct snd_pcm *instance;
51 
52 	struct pcm_substream playback;
53 	bool panic; /* if set driver won't do anymore pcm on device */
54 
55 	struct pcm_urb out_urbs[PCM_N_URBS];
56 
57 	struct mutex stream_mutex;
58 	u8 stream_state; /* one of STREAM_XXX */
59 	u8 extra_freq;
60 	wait_queue_head_t stream_wait_queue;
61 	bool stream_wait_cond;
62 };
63 
64 static const unsigned int rates[] = { 44100, 48000, 88200, 96000, 176400, 192000,
65 				      352800, 384000 };
66 static const struct snd_pcm_hw_constraint_list constraints_extra_rates = {
67 	.count = ARRAY_SIZE(rates),
68 	.list = rates,
69 	.mask = 0,
70 };
71 
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 |
78 		SNDRV_PCM_INFO_BATCH,
79 
80 	.formats = SNDRV_PCM_FMTBIT_S32_LE,
81 
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,
88 
89 	.rate_min = 44100,
90 	.rate_max = 192000, /* changes in hiface_pcm_open to support extra rates */
91 	.channels_min = 2,
92 	.channels_max = 2,
93 	.buffer_bytes_max = PCM_BUFFER_SIZE,
94 	.period_bytes_min = PCM_PACKET_SIZE,
95 	.period_bytes_max = PCM_BUFFER_SIZE,
96 	.periods_min = 2,
97 	.periods_max = 1024
98 };
99 
100 /* message values used to change the sample rate */
101 #define HIFACE_SET_RATE_REQUEST 0xb0
102 
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
111 
112 static int hiface_pcm_set_rate(struct pcm_runtime *rt, unsigned int rate)
113 {
114 	struct usb_device *device = rt->chip->dev;
115 	u16 rate_value;
116 	int ret;
117 
118 	/* We are already sure that the rate is supported here thanks to
119 	 * ALSA constraints
120 	 */
121 	switch (rate) {
122 	case 44100:
123 		rate_value = HIFACE_RATE_44100;
124 		break;
125 	case 48000:
126 		rate_value = HIFACE_RATE_48000;
127 		break;
128 	case 88200:
129 		rate_value = HIFACE_RATE_88200;
130 		break;
131 	case 96000:
132 		rate_value = HIFACE_RATE_96000;
133 		break;
134 	case 176400:
135 		rate_value = HIFACE_RATE_176400;
136 		break;
137 	case 192000:
138 		rate_value = HIFACE_RATE_192000;
139 		break;
140 	case 352800:
141 		rate_value = HIFACE_RATE_352800;
142 		break;
143 	case 384000:
144 		rate_value = HIFACE_RATE_384000;
145 		break;
146 	default:
147 		dev_err(&device->dev, "Unsupported rate %d\n", rate);
148 		return -EINVAL;
149 	}
150 
151 	/*
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
157 	 * other side
158 	 */
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);
163 	if (ret < 0) {
164 		dev_err(&device->dev, "Error setting samplerate %d.\n", rate);
165 		return ret;
166 	}
167 
168 	return 0;
169 }
170 
171 static struct pcm_substream *hiface_pcm_get_substream(struct snd_pcm_substream
172 						      *alsa_sub)
173 {
174 	struct pcm_runtime *rt = snd_pcm_substream_chip(alsa_sub);
175 	struct device *device = &rt->chip->dev->dev;
176 
177 	if (alsa_sub->stream == SNDRV_PCM_STREAM_PLAYBACK)
178 		return &rt->playback;
179 
180 	dev_err(device, "Error getting pcm substream slot.\n");
181 	return NULL;
182 }
183 
184 /* call with stream_mutex locked */
185 static void hiface_pcm_stream_stop(struct pcm_runtime *rt)
186 {
187 	int i, time;
188 
189 	if (rt->stream_state != STREAM_DISABLED) {
190 		rt->stream_state = STREAM_STOPPING;
191 
192 		for (i = 0; i < PCM_N_URBS; i++) {
193 			time = usb_wait_anchor_empty_timeout(
194 					&rt->out_urbs[i].submitted, 100);
195 			if (!time)
196 				usb_kill_anchored_urbs(
197 					&rt->out_urbs[i].submitted);
198 			usb_kill_urb(&rt->out_urbs[i].instance);
199 		}
200 
201 		rt->stream_state = STREAM_DISABLED;
202 	}
203 }
204 
205 /* call with stream_mutex locked */
206 static int hiface_pcm_stream_start(struct pcm_runtime *rt)
207 {
208 	int ret = 0;
209 	int i;
210 
211 	if (rt->stream_state == STREAM_DISABLED) {
212 
213 		/* reset panic state when starting a new stream */
214 		rt->panic = false;
215 
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,
223 					     GFP_ATOMIC);
224 			if (ret) {
225 				hiface_pcm_stream_stop(rt);
226 				return ret;
227 			}
228 		}
229 
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,
232 				   HZ);
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",
236 				 __func__);
237 			rt->stream_state = STREAM_RUNNING;
238 		} else {
239 			hiface_pcm_stream_stop(rt);
240 			return -EIO;
241 		}
242 	}
243 	return ret;
244 }
245 
246 /* The hardware wants word-swapped 32-bit values */
247 static void memcpy_swahw32(u8 *dest, u8 *src, unsigned int n)
248 {
249 	unsigned int i;
250 
251 	for (i = 0; i < n / 4; i++)
252 		((u32 *)dest)[i] = swahw32(((u32 *)src)[i]);
253 }
254 
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)
258 {
259 	struct snd_pcm_runtime *alsa_rt = sub->instance->runtime;
260 	struct device *device = &urb->chip->dev->dev;
261 	u8 *source;
262 	unsigned int pcm_buffer_size;
263 
264 	WARN_ON(alsa_rt->format != SNDRV_PCM_FORMAT_S32_LE);
265 
266 	pcm_buffer_size = snd_pcm_lib_buffer_bytes(sub->instance);
267 
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);
272 
273 		source = alsa_rt->dma_area + sub->dma_off;
274 		memcpy_swahw32(urb->buffer, source, PCM_PACKET_SIZE);
275 	} else {
276 		/* wrap around at end of ring buffer */
277 		unsigned int len;
278 
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);
282 
283 		len = pcm_buffer_size - sub->dma_off;
284 
285 		source = alsa_rt->dma_area + sub->dma_off;
286 		memcpy_swahw32(urb->buffer, source, len);
287 
288 		source = alsa_rt->dma_area;
289 		memcpy_swahw32(urb->buffer + len, source,
290 			       PCM_PACKET_SIZE - len);
291 	}
292 	sub->dma_off += PCM_PACKET_SIZE;
293 	if (sub->dma_off >= pcm_buffer_size)
294 		sub->dma_off -= pcm_buffer_size;
295 
296 	sub->period_off += PCM_PACKET_SIZE;
297 	if (sub->period_off >= alsa_rt->period_size) {
298 		sub->period_off %= alsa_rt->period_size;
299 		return true;
300 	}
301 	return false;
302 }
303 
304 static void hiface_pcm_out_urb_handler(struct urb *usb_urb)
305 {
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;
310 	unsigned long flags;
311 	int ret;
312 
313 	if (rt->panic || rt->stream_state == STREAM_STOPPING)
314 		return;
315 
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 */
320 		goto out_fail;
321 	}
322 
323 	if (rt->stream_state == STREAM_STARTING) {
324 		rt->stream_wait_cond = true;
325 		wake_up(&rt->stream_wait_queue);
326 	}
327 
328 	/* now send our playback data (if a free out urb was found) */
329 	sub = &rt->playback;
330 	spin_lock_irqsave(&sub->lock, flags);
331 	if (sub->active)
332 		do_period_elapsed = hiface_pcm_playback(sub, out_urb);
333 	else
334 		memset(out_urb->buffer, 0, PCM_PACKET_SIZE);
335 
336 	spin_unlock_irqrestore(&sub->lock, flags);
337 
338 	if (do_period_elapsed)
339 		snd_pcm_period_elapsed(sub->instance);
340 
341 	ret = usb_submit_urb(&out_urb->instance, GFP_ATOMIC);
342 	if (ret < 0)
343 		goto out_fail;
344 
345 	return;
346 
347 out_fail:
348 	rt->panic = true;
349 }
350 
351 static int hiface_pcm_open(struct snd_pcm_substream *alsa_sub)
352 {
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;
356 	int ret;
357 
358 	if (rt->panic)
359 		return -EPIPE;
360 
361 	mutex_lock(&rt->stream_mutex);
362 	alsa_rt->hw = pcm_hw;
363 
364 	if (alsa_sub->stream == SNDRV_PCM_STREAM_PLAYBACK)
365 		sub = &rt->playback;
366 
367 	if (!sub) {
368 		struct device *device = &rt->chip->dev->dev;
369 		mutex_unlock(&rt->stream_mutex);
370 		dev_err(device, "Invalid stream type\n");
371 		return -EINVAL;
372 	}
373 
374 	if (rt->extra_freq) {
375 		alsa_rt->hw.rates |= SNDRV_PCM_RATE_KNOT;
376 		alsa_rt->hw.rate_max = 384000;
377 
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);
382 		if (ret < 0) {
383 			mutex_unlock(&rt->stream_mutex);
384 			return ret;
385 		}
386 	}
387 
388 	sub->instance = alsa_sub;
389 	sub->active = false;
390 	mutex_unlock(&rt->stream_mutex);
391 	return 0;
392 }
393 
394 static int hiface_pcm_close(struct snd_pcm_substream *alsa_sub)
395 {
396 	struct pcm_runtime *rt = snd_pcm_substream_chip(alsa_sub);
397 	struct pcm_substream *sub = hiface_pcm_get_substream(alsa_sub);
398 	unsigned long flags;
399 
400 	if (rt->panic)
401 		return 0;
402 
403 	mutex_lock(&rt->stream_mutex);
404 	if (sub) {
405 		hiface_pcm_stream_stop(rt);
406 
407 		/* deactivate substream */
408 		spin_lock_irqsave(&sub->lock, flags);
409 		sub->instance = NULL;
410 		sub->active = false;
411 		spin_unlock_irqrestore(&sub->lock, flags);
412 
413 	}
414 	mutex_unlock(&rt->stream_mutex);
415 	return 0;
416 }
417 
418 static int hiface_pcm_prepare(struct snd_pcm_substream *alsa_sub)
419 {
420 	struct pcm_runtime *rt = snd_pcm_substream_chip(alsa_sub);
421 	struct pcm_substream *sub = hiface_pcm_get_substream(alsa_sub);
422 	struct snd_pcm_runtime *alsa_rt = alsa_sub->runtime;
423 	int ret;
424 
425 	if (rt->panic)
426 		return -EPIPE;
427 	if (!sub)
428 		return -ENODEV;
429 
430 	mutex_lock(&rt->stream_mutex);
431 
432 	hiface_pcm_stream_stop(rt);
433 
434 	sub->dma_off = 0;
435 	sub->period_off = 0;
436 
437 	if (rt->stream_state == STREAM_DISABLED) {
438 
439 		ret = hiface_pcm_set_rate(rt, alsa_rt->rate);
440 		if (ret) {
441 			mutex_unlock(&rt->stream_mutex);
442 			return ret;
443 		}
444 		ret = hiface_pcm_stream_start(rt);
445 		if (ret) {
446 			mutex_unlock(&rt->stream_mutex);
447 			return ret;
448 		}
449 	}
450 	mutex_unlock(&rt->stream_mutex);
451 	return 0;
452 }
453 
454 static int hiface_pcm_trigger(struct snd_pcm_substream *alsa_sub, int cmd)
455 {
456 	struct pcm_substream *sub = hiface_pcm_get_substream(alsa_sub);
457 	struct pcm_runtime *rt = snd_pcm_substream_chip(alsa_sub);
458 
459 	if (rt->panic)
460 		return -EPIPE;
461 	if (!sub)
462 		return -ENODEV;
463 
464 	switch (cmd) {
465 	case SNDRV_PCM_TRIGGER_START:
466 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
467 		spin_lock_irq(&sub->lock);
468 		sub->active = true;
469 		spin_unlock_irq(&sub->lock);
470 		return 0;
471 
472 	case SNDRV_PCM_TRIGGER_STOP:
473 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
474 		spin_lock_irq(&sub->lock);
475 		sub->active = false;
476 		spin_unlock_irq(&sub->lock);
477 		return 0;
478 
479 	default:
480 		return -EINVAL;
481 	}
482 }
483 
484 static snd_pcm_uframes_t hiface_pcm_pointer(struct snd_pcm_substream *alsa_sub)
485 {
486 	struct pcm_substream *sub = hiface_pcm_get_substream(alsa_sub);
487 	struct pcm_runtime *rt = snd_pcm_substream_chip(alsa_sub);
488 	unsigned long flags;
489 	snd_pcm_uframes_t dma_offset;
490 
491 	if (rt->panic || !sub)
492 		return SNDRV_PCM_POS_XRUN;
493 
494 	spin_lock_irqsave(&sub->lock, flags);
495 	dma_offset = sub->dma_off;
496 	spin_unlock_irqrestore(&sub->lock, flags);
497 	return bytes_to_frames(alsa_sub->runtime, dma_offset);
498 }
499 
500 static const struct snd_pcm_ops pcm_ops = {
501 	.open = hiface_pcm_open,
502 	.close = hiface_pcm_close,
503 	.prepare = hiface_pcm_prepare,
504 	.trigger = hiface_pcm_trigger,
505 	.pointer = hiface_pcm_pointer,
506 };
507 
508 static int hiface_pcm_init_urb(struct pcm_urb *urb,
509 			       struct hiface_chip *chip,
510 			       unsigned int ep,
511 			       void (*handler)(struct urb *))
512 {
513 	urb->chip = chip;
514 	usb_init_urb(&urb->instance);
515 
516 	urb->buffer = kzalloc(PCM_PACKET_SIZE, GFP_KERNEL);
517 	if (!urb->buffer)
518 		return -ENOMEM;
519 
520 	usb_fill_bulk_urb(&urb->instance, chip->dev,
521 			  usb_sndbulkpipe(chip->dev, ep), (void *)urb->buffer,
522 			  PCM_PACKET_SIZE, handler, urb);
523 	if (usb_urb_ep_type_check(&urb->instance))
524 		return -EINVAL;
525 	init_usb_anchor(&urb->submitted);
526 
527 	return 0;
528 }
529 
530 void hiface_pcm_abort(struct hiface_chip *chip)
531 {
532 	struct pcm_runtime *rt = chip->pcm;
533 
534 	if (rt) {
535 		rt->panic = true;
536 
537 		mutex_lock(&rt->stream_mutex);
538 		hiface_pcm_stream_stop(rt);
539 		mutex_unlock(&rt->stream_mutex);
540 	}
541 }
542 
543 static void hiface_pcm_destroy(struct hiface_chip *chip)
544 {
545 	struct pcm_runtime *rt = chip->pcm;
546 	int i;
547 
548 	for (i = 0; i < PCM_N_URBS; i++)
549 		kfree(rt->out_urbs[i].buffer);
550 
551 	kfree(chip->pcm);
552 	chip->pcm = NULL;
553 }
554 
555 static void hiface_pcm_free(struct snd_pcm *pcm)
556 {
557 	struct pcm_runtime *rt = pcm->private_data;
558 
559 	if (rt)
560 		hiface_pcm_destroy(rt->chip);
561 }
562 
563 int hiface_pcm_init(struct hiface_chip *chip, u8 extra_freq)
564 {
565 	int i;
566 	int ret;
567 	struct snd_pcm *pcm;
568 	struct pcm_runtime *rt;
569 
570 	rt = kzalloc(sizeof(*rt), GFP_KERNEL);
571 	if (!rt)
572 		return -ENOMEM;
573 
574 	rt->chip = chip;
575 	rt->stream_state = STREAM_DISABLED;
576 	if (extra_freq)
577 		rt->extra_freq = 1;
578 
579 	init_waitqueue_head(&rt->stream_wait_queue);
580 	mutex_init(&rt->stream_mutex);
581 	spin_lock_init(&rt->playback.lock);
582 
583 	for (i = 0; i < PCM_N_URBS; i++) {
584 		ret = hiface_pcm_init_urb(&rt->out_urbs[i], chip, OUT_EP,
585 				    hiface_pcm_out_urb_handler);
586 		if (ret < 0)
587 			goto error;
588 	}
589 
590 	ret = snd_pcm_new(chip->card, "USB-SPDIF Audio", 0, 1, 0, &pcm);
591 	if (ret < 0) {
592 		dev_err(&chip->dev->dev, "Cannot create pcm instance\n");
593 		goto error;
594 	}
595 
596 	pcm->private_data = rt;
597 	pcm->private_free = hiface_pcm_free;
598 
599 	strlcpy(pcm->name, "USB-SPDIF Audio", sizeof(pcm->name));
600 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &pcm_ops);
601 	snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_VMALLOC,
602 				       NULL, 0, 0);
603 
604 	rt->instance = pcm;
605 
606 	chip->pcm = rt;
607 	return 0;
608 
609 error:
610 	for (i = 0; i < PCM_N_URBS; i++)
611 		kfree(rt->out_urbs[i].buffer);
612 	kfree(rt);
613 	return ret;
614 }
615