aboutsummaryrefslogtreecommitdiffstats
path: root/sound/usb/qcom/mixer_usb_offload.c
blob: 2adeb64f4d33f4c33a915d3a895a071fd8d0630c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// SPDX-License-Identifier: GPL-2.0
/*
 * Copyright (c) 2022-2025 Qualcomm Innovation Center, Inc. All rights reserved.
 */

#include <linux/usb.h>

#include <sound/core.h>
#include <sound/control.h>
#include <sound/soc-usb.h>

#include "../usbaudio.h"
#include "../card.h"
#include "../helper.h"
#include "../mixer.h"

#include "mixer_usb_offload.h"

#define PCM_IDX(n)  ((n) & 0xffff)
#define CARD_IDX(n) ((n) >> 16)

static int
snd_usb_offload_card_route_get(struct snd_kcontrol *kcontrol,
			       struct snd_ctl_elem_value *ucontrol)
{
	struct device *sysdev = snd_kcontrol_chip(kcontrol);
	int ret;

	ret = snd_soc_usb_update_offload_route(sysdev,
					       CARD_IDX(kcontrol->private_value),
					       PCM_IDX(kcontrol->private_value),
					       SNDRV_PCM_STREAM_PLAYBACK,
					       SND_SOC_USB_KCTL_CARD_ROUTE,
					       ucontrol->value.integer.value);
	if (ret < 0) {
		ucontrol->value.integer.value[0] = -1;
		ucontrol->value.integer.value[1] = -1;
	}

	return 0;
}

static int snd_usb_offload_card_route_info(struct snd_kcontrol *kcontrol,
					   struct snd_ctl_elem_info *uinfo)
{
	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
	uinfo->count = 1;
	uinfo->value.integer.min = -1;
	uinfo->value.integer.max = SNDRV_CARDS;

	return 0;
}

static struct snd_kcontrol_new snd_usb_offload_mapped_card_ctl = {
	.iface = SNDRV_CTL_ELEM_IFACE_CARD,
	.access = SNDRV_CTL_ELEM_ACCESS_READ,
	.info = snd_usb_offload_card_route_info,
	.get = snd_usb_offload_card_route_get,
};

static int
snd_usb_offload_pcm_route_get(struct snd_kcontrol *kcontrol,
			      struct snd_ctl_elem_value *ucontrol)
{
	struct device *sysdev = snd_kcontrol_chip(kcontrol);
	int ret;

	ret = snd_soc_usb_update_offload_route(sysdev,
					       CARD_IDX(kcontrol->private_value),
					       PCM_IDX(kcontrol->private_value),
					       SNDRV_PCM_STREAM_PLAYBACK,
					       SND_SOC_USB_KCTL_PCM_ROUTE,
					       ucontrol->value.integer.value);
	if (ret < 0) {
		ucontrol->value.integer.value[0] = -1;
		ucontrol->value.integer.value[1] = -1;
	}

	return 0;
}

static int snd_usb_offload_pcm_route_info(struct snd_kcontrol *kcontrol,
					  struct snd_ctl_elem_info *uinfo)
{
	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
	uinfo->count = 1;
	uinfo->value.integer.min = -1;
	/* Arbitrary max value, as there is no 'limit' on number of PCM devices */
	uinfo->value.integer.max = 0xff;

	return 0;
}

static struct snd_kcontrol_new snd_usb_offload_mapped_pcm_ctl = {
	.iface = SNDRV_CTL_ELEM_IFACE_CARD,
	.access = SNDRV_CTL_ELEM_ACCESS_READ,
	.info = snd_usb_offload_pcm_route_info,
	.get = snd_usb_offload_pcm_route_get,
};

/**
 * snd_usb_offload_create_ctl() - Add USB offload bounded mixer
 * @chip: USB SND chip device
 * @bedev: Reference to USB backend DAI device
 *
 * Creates a sound control for a USB audio device, so that applications can
 * query for if there is an available USB audio offload path, and which
 * card is managing it.
 */
int snd_usb_offload_create_ctl(struct snd_usb_audio *chip, struct device *bedev)
{
	struct snd_kcontrol_new *chip_kctl;
	struct snd_usb_substream *subs;
	struct snd_usb_stream *as;
	char ctl_name[48];
	int ret;

	list_for_each_entry(as, &chip->pcm_list, list) {
		subs = &as->substream[SNDRV_PCM_STREAM_PLAYBACK];
		if (!subs->ep_num || as->pcm_index > 0xff)
			continue;

		chip_kctl = &snd_usb_offload_mapped_card_ctl;
		chip_kctl->count = 1;
		/*
		 * Store the associated USB SND card number and PCM index for
		 * the kctl.
		 */
		chip_kctl->private_value = as->pcm_index |
					  chip->card->number << 16;
		sprintf(ctl_name, "USB Offload Playback Card Route PCM#%d",
			as->pcm_index);
		chip_kctl->name = ctl_name;
		ret = snd_ctl_add(chip->card, snd_ctl_new1(chip_kctl, bedev));
		if (ret < 0)
			break;

		chip_kctl = &snd_usb_offload_mapped_pcm_ctl;
		chip_kctl->count = 1;
		/*
		 * Store the associated USB SND card number and PCM index for
		 * the kctl.
		 */
		chip_kctl->private_value = as->pcm_index |
					  chip->card->number << 16;
		sprintf(ctl_name, "USB Offload Playback PCM Route PCM#%d",
			as->pcm_index);
		chip_kctl->name = ctl_name;
		ret = snd_ctl_add(chip->card, snd_ctl_new1(chip_kctl, bedev));
		if (ret < 0)
			break;
	}

	return ret;
}