summaryrefslogtreecommitdiffstats
path: root/sound/soc/soc-card-test.c
blob: e4a4b101d7438dda2913405acbe6d86457f66566 (plain)
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// SPDX-License-Identifier: GPL-2.0-only
// Copyright (C) 2024 Cirrus Logic, Inc. and
//                    Cirrus Logic International Semiconductor Ltd.

#include <kunit/device.h>
#include <kunit/test.h>
#include <linux/module.h>
#include <sound/control.h>
#include <sound/soc.h>
#include <sound/soc-card.h>

struct soc_card_test_priv {
	struct device *card_dev;
	struct snd_soc_card *card;
};

static const struct snd_kcontrol_new test_card_controls[] = {
	SOC_SINGLE("Fee", SND_SOC_NOPM, 0, 1, 0),
	SOC_SINGLE("Fi", SND_SOC_NOPM, 1, 1, 0),
	SOC_SINGLE("Fo", SND_SOC_NOPM, 2, 1, 0),
	SOC_SINGLE("Fum", SND_SOC_NOPM, 3, 1, 0),
	SOC_SINGLE("Left Fee", SND_SOC_NOPM, 4, 1, 0),
	SOC_SINGLE("Right Fee", SND_SOC_NOPM, 5, 1, 0),
	SOC_SINGLE("Left Fi", SND_SOC_NOPM, 6, 1, 0),
	SOC_SINGLE("Right Fi", SND_SOC_NOPM, 7, 1, 0),
	SOC_SINGLE("Left Fo", SND_SOC_NOPM, 8, 1, 0),
	SOC_SINGLE("Right Fo", SND_SOC_NOPM, 9, 1, 0),
	SOC_SINGLE("Left Fum", SND_SOC_NOPM, 10, 1, 0),
	SOC_SINGLE("Right Fum", SND_SOC_NOPM, 11, 1, 0),
};

static void test_snd_soc_card_get_kcontrol(struct kunit *test)
{
	struct soc_card_test_priv *priv = test->priv;
	struct snd_soc_card *card = priv->card;
	struct snd_kcontrol *kc;
	struct soc_mixer_control *mc;
	int i, ret;

	ret = snd_soc_add_card_controls(card, test_card_controls, ARRAY_SIZE(test_card_controls));
	KUNIT_ASSERT_EQ(test, ret, 0);

	/* Look up every control */
	for (i = 0; i < ARRAY_SIZE(test_card_controls); ++i) {
		kc = snd_soc_card_get_kcontrol(card, test_card_controls[i].name);
		KUNIT_EXPECT_NOT_ERR_OR_NULL_MSG(test, kc, "Failed to find '%s'\n",
						 test_card_controls[i].name);
		if (!kc)
			continue;

		/* Test that it is the correct control */
		mc = (struct soc_mixer_control *)kc->private_value;
		KUNIT_EXPECT_EQ_MSG(test, mc->shift, i, "For '%s'\n", test_card_controls[i].name);
	}

	/* Test some names that should not be found */
	kc = snd_soc_card_get_kcontrol(card, "None");
	KUNIT_EXPECT_NULL(test, kc);

	kc = snd_soc_card_get_kcontrol(card, "Left None");
	KUNIT_EXPECT_NULL(test, kc);

	kc = snd_soc_card_get_kcontrol(card, "Left");
	KUNIT_EXPECT_NULL(test, kc);

	kc = snd_soc_card_get_kcontrol(card, NULL);
	KUNIT_EXPECT_NULL(test, kc);
}

static void test_snd_soc_card_get_kcontrol_locked(struct kunit *test)
{
	struct soc_card_test_priv *priv = test->priv;
	struct snd_soc_card *card = priv->card;
	struct snd_kcontrol *kc, *kcw;
	struct soc_mixer_control *mc;
	int i, ret;

	ret = snd_soc_add_card_controls(card, test_card_controls, ARRAY_SIZE(test_card_controls));
	KUNIT_ASSERT_EQ(test, ret, 0);

	/* Look up every control */
	for (i = 0; i < ARRAY_SIZE(test_card_controls); ++i) {
		down_read(&card->snd_card->controls_rwsem);
		kc = snd_soc_card_get_kcontrol_locked(card, test_card_controls[i].name);
		up_read(&card->snd_card->controls_rwsem);
		KUNIT_EXPECT_NOT_ERR_OR_NULL_MSG(test, kc, "Failed to find '%s'\n",
						 test_card_controls[i].name);
		if (!kc)
			continue;

		/* Test that it is the correct control */
		mc = (struct soc_mixer_control *)kc->private_value;
		KUNIT_EXPECT_EQ_MSG(test, mc->shift, i, "For '%s'\n", test_card_controls[i].name);

		down_write(&card->snd_card->controls_rwsem);
		kcw = snd_soc_card_get_kcontrol_locked(card, test_card_controls[i].name);
		up_write(&card->snd_card->controls_rwsem);
		KUNIT_EXPECT_NOT_ERR_OR_NULL_MSG(test, kcw, "Failed to find '%s'\n",
						 test_card_controls[i].name);

		KUNIT_EXPECT_PTR_EQ(test, kc, kcw);
	}

	/* Test some names that should not be found */
	down_read(&card->snd_card->controls_rwsem);
	kc = snd_soc_card_get_kcontrol_locked(card, "None");
	up_read(&card->snd_card->controls_rwsem);
	KUNIT_EXPECT_NULL(test, kc);

	down_read(&card->snd_card->controls_rwsem);
	kc = snd_soc_card_get_kcontrol_locked(card, "Left None");
	up_read(&card->snd_card->controls_rwsem);
	KUNIT_EXPECT_NULL(test, kc);

	down_read(&card->snd_card->controls_rwsem);
	kc = snd_soc_card_get_kcontrol_locked(card, "Left");
	up_read(&card->snd_card->controls_rwsem);
	KUNIT_EXPECT_NULL(test, kc);

	down_read(&card->snd_card->controls_rwsem);
	kc = snd_soc_card_get_kcontrol_locked(card, NULL);
	up_read(&card->snd_card->controls_rwsem);
	KUNIT_EXPECT_NULL(test, kc);
}

static int soc_card_test_case_init(struct kunit *test)
{
	struct soc_card_test_priv *priv;
	int ret;

	priv = kunit_kzalloc(test, sizeof(*priv), GFP_KERNEL);
	if (!priv)
		return -ENOMEM;

	test->priv = priv;

	priv->card = kunit_kzalloc(test, sizeof(*priv->card), GFP_KERNEL);
	if (!priv->card)
		return -ENOMEM;

	priv->card_dev = kunit_device_register(test, "sound-soc-card-test");
	priv->card_dev = get_device(priv->card_dev);
	if (!priv->card_dev)
		return -ENODEV;

	priv->card->name = "soc-card-test";
	priv->card->dev = priv->card_dev;
	priv->card->owner = THIS_MODULE;

	ret = snd_soc_register_card(priv->card);
	if (ret) {
		put_device(priv->card_dev);
		return ret;
	}

	return 0;
}

static void soc_card_test_case_exit(struct kunit *test)
{
	struct soc_card_test_priv *priv = test->priv;

	if (priv->card)
		snd_soc_unregister_card(priv->card);

	if (priv->card_dev)
		put_device(priv->card_dev);
}

static struct kunit_case soc_card_test_cases[] = {
	KUNIT_CASE(test_snd_soc_card_get_kcontrol),
	KUNIT_CASE(test_snd_soc_card_get_kcontrol_locked),
	{}
};

static struct kunit_suite soc_card_test_suite = {
	.name = "soc-card",
	.test_cases = soc_card_test_cases,
	.init = soc_card_test_case_init,
	.exit = soc_card_test_case_exit,
};

kunit_test_suites(&soc_card_test_suite);

MODULE_DESCRIPTION("ASoC soc-card KUnit test");
MODULE_LICENSE("GPL");