summaryrefslogtreecommitdiff
path: root/sound/usb/usx2y/us144mkii_controls.c
blob: 81ded11e370981bde6620c5e19f0518112541421 (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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
// SPDX-License-Identifier: GPL-2.0-only
// Copyright (c) 2025 Šerif Rami <ramiserifpersia@gmail.com>

#include "us144mkii.h"

/*
 * Text descriptions for playback output source options.
 *
 * Used by ALSA kcontrol elements to provide user-friendly names for
 * the playback routing options (e.g., "Playback 1-2", "Playback 3-4").
 */
static const char *const playback_source_texts[] = { "Playback 1-2",
						     "Playback 3-4" };

/*
 * Text descriptions for capture input source options.
 *
 * Used by ALSA kcontrol elements to provide user-friendly names for
 * the capture routing options (e.g., "Analog In", "Digital In").
 */
static const char *const capture_source_texts[] = { "Analog In", "Digital In" };

/*
 * tascam_playback_source_info() - ALSA control info callback for playback
 * source.
 * @kcontrol: The ALSA kcontrol instance.
 * @uinfo: The ALSA control element info structure to fill.
 *
 * This function provides information about the enumerated playback source
 * control, including its type, count, and available items (Playback 1-2,
 * Playback 3-4).
 *
 * Return: 0 on success.
 */
static int tascam_playback_source_info(struct snd_kcontrol *kcontrol,
				       struct snd_ctl_elem_info *uinfo)
{
	return snd_ctl_enum_info(uinfo, 1, 2, playback_source_texts);
}

/*
 * tascam_line_out_get() - ALSA control get callback for Line Outputs Source.
 * @kcontrol: The ALSA kcontrol instance.
 * @ucontrol: The ALSA control element value structure to fill.
 *
 * This function retrieves the current selection for the Line Outputs source
 * (Playback 1-2 or Playback 3-4) from the driver's private data and populates
 * the ALSA control element value.
 *
 * Return: 0 on success.
 */
static int tascam_line_out_get(struct snd_kcontrol *kcontrol,
			       struct snd_ctl_elem_value *ucontrol)
{
	struct tascam_card *tascam = snd_kcontrol_chip(kcontrol);

	scoped_guard(spinlock_irqsave, &tascam->lock) {
		ucontrol->value.enumerated.item[0] = tascam->line_out_source;
	}
	return 0;
}

/*
 * tascam_line_out_put() - ALSA control put callback for Line Outputs Source.
 * @kcontrol: The ALSA kcontrol instance.
 * @ucontrol: The ALSA control element value structure containing the new value.
 *
 * This function sets the Line Outputs source (Playback 1-2 or Playback 3-4)
 * based on the user's selection from the ALSA control element. It validates
 * the input and updates the driver's private data.
 *
 * Return: 1 if the value was changed, 0 if unchanged, or a negative error code.
 */
static int tascam_line_out_put(struct snd_kcontrol *kcontrol,
			       struct snd_ctl_elem_value *ucontrol)
{
	struct tascam_card *tascam = snd_kcontrol_chip(kcontrol);
	int changed = 0;

	if (ucontrol->value.enumerated.item[0] > 1)
		return -EINVAL;

	scoped_guard(spinlock_irqsave, &tascam->lock) {
		if (tascam->line_out_source != ucontrol->value.enumerated.item[0]) {
			tascam->line_out_source = ucontrol->value.enumerated.item[0];
			changed = 1;
		}
	}
	return changed;
}

/*
 * tascam_line_out_control - ALSA kcontrol definition for Line Outputs Source.
 *
 * This defines a new ALSA mixer control named "Line OUTPUTS Source" that allows
 * the user to select between "Playback 1-2" and "Playback 3-4" for the analog
 * line outputs of the device. It uses the `tascam_playback_source_info` for
 * information and `tascam_line_out_get`/`tascam_line_out_put` for value
 * handling.
 */
static const struct snd_kcontrol_new tascam_line_out_control = {
	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
	.name = "Line Playback Source",
	.info = tascam_playback_source_info,
	.get = tascam_line_out_get,
	.put = tascam_line_out_put,
};

/*
 * tascam_digital_out_get() - ALSA control get callback for Digital Outputs
 * Source.
 * @kcontrol: The ALSA kcontrol instance.
 * @ucontrol: The ALSA control element value structure to fill.
 *
 * This function retrieves the current selection for the Digital Outputs source
 * (Playback 1-2 or Playback 3-4) from the driver's private data and populates
 * the ALSA control element value.
 *
 * Return: 0 on success.
 */
static int tascam_digital_out_get(struct snd_kcontrol *kcontrol,
				  struct snd_ctl_elem_value *ucontrol)
{
	struct tascam_card *tascam = snd_kcontrol_chip(kcontrol);

	scoped_guard(spinlock_irqsave, &tascam->lock) {
		ucontrol->value.enumerated.item[0] = tascam->digital_out_source;
	}
	return 0;
}

/*
 * tascam_digital_out_put() - ALSA control put callback for Digital Outputs
 * Source.
 * @kcontrol: The ALSA kcontrol instance.
 * @ucontrol: The ALSA control element value structure containing the new value.
 *
 * This function sets the Digital Outputs source (Playback 1-2 or Playback 3-4)
 * based on the user's selection from the ALSA control element. It validates
 * the input and updates the driver's private data.
 *
 * Return: 1 if the value was changed, 0 if unchanged, or a negative error code.
 */
static int tascam_digital_out_put(struct snd_kcontrol *kcontrol,
				  struct snd_ctl_elem_value *ucontrol)
{
	struct tascam_card *tascam = snd_kcontrol_chip(kcontrol);
	int changed = 0;

	if (ucontrol->value.enumerated.item[0] > 1)
		return -EINVAL;

	scoped_guard(spinlock_irqsave, &tascam->lock) {
		if (tascam->digital_out_source != ucontrol->value.enumerated.item[0]) {
			tascam->digital_out_source = ucontrol->value.enumerated.item[0];
			changed = 1;
		}
	}
	return changed;
}

/*
 * tascam_digital_out_control - ALSA kcontrol definition for Digital Outputs
 * Source.
 *
 * This defines a new ALSA mixer control named "Digital OUTPUTS Source" that
 * allows the user to select between "Playback 1-2" and "Playback 3-4" for the
 * digital outputs of the device. It uses the `tascam_playback_source_info` for
 * information and `tascam_digital_out_get`/`tascam_digital_out_put` for value
 * handling.
 */
static const struct snd_kcontrol_new tascam_digital_out_control = {
	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
	.name = "Digital Playback Source",
	.info = tascam_playback_source_info,
	.get = tascam_digital_out_get,
	.put = tascam_digital_out_put,
};

/*
 * tascam_capture_source_info() - ALSA control info callback for capture source.
 * @kcontrol: The ALSA kcontrol instance.
 * @uinfo: The ALSA control element info structure to fill.
 *
 * This function provides information about the enumerated capture source
 * control, including its type, count, and available items (Analog In, Digital
 * In).
 *
 * Return: 0 on success.
 */
static int tascam_capture_source_info(struct snd_kcontrol *kcontrol,
				      struct snd_ctl_elem_info *uinfo)
{
	return snd_ctl_enum_info(uinfo, 1, 2, capture_source_texts);
}

/*
 * tascam_capture_12_get() - ALSA control get callback for Capture channels 1
 * and 2 Source.
 * @kcontrol: The ALSA kcontrol instance.
 * @ucontrol: The ALSA control element value structure to fill.
 *
 * This function retrieves the current selection for the Capture channels 1 and
 * 2 source (Analog In or Digital In) from the driver's private data and
 * populates the ALSA control element value.
 *
 * Return: 0 on success.
 */
static int tascam_capture_12_get(struct snd_kcontrol *kcontrol,
				 struct snd_ctl_elem_value *ucontrol)
{
	struct tascam_card *tascam = snd_kcontrol_chip(kcontrol);

	scoped_guard(spinlock_irqsave, &tascam->lock) {
		ucontrol->value.enumerated.item[0] = tascam->capture_12_source;
	}
	return 0;
}

/*
 * tascam_capture_12_put() - ALSA control put callback for Capture channels 1
 * and 2 Source.
 * @kcontrol: The ALSA kcontrol instance.
 * @ucontrol: The ALSA control element value structure containing the new value.
 *
 * This function sets the Capture channels 1 and 2 source (Analog In or Digital
 * In) based on the user's selection from the ALSA control element. It validates
 * the input and updates the driver's private data.
 *
 * Return: 1 if the value was changed, 0 if unchanged, or a negative error code.
 */
static int tascam_capture_12_put(struct snd_kcontrol *kcontrol,
				 struct snd_ctl_elem_value *ucontrol)
{
	struct tascam_card *tascam = snd_kcontrol_chip(kcontrol);
	int changed = 0;

	if (ucontrol->value.enumerated.item[0] > 1)
		return -EINVAL;

	scoped_guard(spinlock_irqsave, &tascam->lock) {
		if (tascam->capture_12_source != ucontrol->value.enumerated.item[0]) {
			tascam->capture_12_source = ucontrol->value.enumerated.item[0];
			changed = 1;
		}
	}
	return changed;
}

/*
 * tascam_capture_12_control - ALSA kcontrol definition for Capture channels 1
 * and 2 Source.
 *
 * This defines a new ALSA mixer control named "ch1 and ch2 Source" that allows
 * the user to select between "Analog In" and "Digital In" for the first two
 * capture channels of the device. It uses the `tascam_capture_source_info` for
 * information and `tascam_capture_12_get`/`tascam_capture_12_put` for value
 * handling.
 */
static const struct snd_kcontrol_new tascam_capture_12_control = {
	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
	.name = "Ch1/2 Capture Source",
	.info = tascam_capture_source_info,
	.get = tascam_capture_12_get,
	.put = tascam_capture_12_put,
};

/*
 * tascam_capture_34_get() - ALSA control get callback for Capture channels 3
 * and 4 Source.
 * @kcontrol: The ALSA kcontrol instance.
 * @ucontrol: The ALSA control element value structure to fill.
 *
 * This function retrieves the current selection for the Capture channels 3 and
 * 4 source (Analog In or Digital In) from the driver's private data and
 * populates the ALSA control element value.
 *
 * Return: 0 on success.
 */
static int tascam_capture_34_get(struct snd_kcontrol *kcontrol,
				 struct snd_ctl_elem_value *ucontrol)
{
	struct tascam_card *tascam = snd_kcontrol_chip(kcontrol);

	scoped_guard(spinlock_irqsave, &tascam->lock) {
		ucontrol->value.enumerated.item[0] = tascam->capture_34_source;
	}
	return 0;
}

/*
 * tascam_capture_34_put() - ALSA control put callback for Capture channels 3
 * and 4 Source.
 * @kcontrol: The ALSA kcontrol instance.
 * @ucontrol: The ALSA control element value structure containing the new value.
 *
 * This function sets the Capture channels 3 and 4 source (Analog In or Digital
 * In) based on the user's selection from the ALSA control element. It validates
 * the input and updates the driver's private data.
 *
 * Return: 1 if the value was changed, 0 if unchanged, or a negative error code.
 */
static int tascam_capture_34_put(struct snd_kcontrol *kcontrol,
				 struct snd_ctl_elem_value *ucontrol)
{
	struct tascam_card *tascam = snd_kcontrol_chip(kcontrol);
	int changed = 0;

	if (ucontrol->value.enumerated.item[0] > 1)
		return -EINVAL;

	scoped_guard(spinlock_irqsave, &tascam->lock) {
		if (tascam->capture_34_source != ucontrol->value.enumerated.item[0]) {
			tascam->capture_34_source = ucontrol->value.enumerated.item[0];
			changed = 1;
		}
	}
	return changed;
}

/*
 * tascam_capture_34_control - ALSA kcontrol definition for Capture channels 3
 * and 4 Source.
 *
 * This defines a new ALSA mixer control named "ch3 and ch4 Source" that allows
 * the user to select between "Analog In" and "Digital In" for the third and
 * fourth capture channels of the device. It uses the
 * `tascam_capture_source_info` for information and
 * `tascam_capture_34_get`/`tascam_capture_34_put` for value handling.
 */
static const struct snd_kcontrol_new tascam_capture_34_control = {
	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
	.name = "Ch3/4 Capture Source",
	.info = tascam_capture_source_info,
	.get = tascam_capture_34_get,
	.put = tascam_capture_34_put,
};

/*
 * tascam_samplerate_info() - ALSA control info callback for Sample Rate.
 * @kcontrol: The ALSA kcontrol instance.
 * @uinfo: The ALSA control element info structure to fill.
 *
 * This function provides information about the Sample Rate control, defining
 * it as an integer type with a minimum value of 0 and a maximum of 96000.
 *
 * Return: 0 on success.
 */
static int tascam_samplerate_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 = 0;
	uinfo->value.integer.max = 96000;
	return 0;
}

/*
 * tascam_samplerate_get() - ALSA control get callback for Sample Rate.
 * @kcontrol: The ALSA kcontrol instance.
 * @ucontrol: The ALSA control element value structure to fill.
 *
 * This function retrieves the current sample rate from the device via a USB
 * control message and populates the ALSA control element value. If the rate
 * is already known (i.e., `current_rate` is set), it returns that value
 * directly.
 *
 * Return: 0 on success, or a negative error code on failure.
 */
static int tascam_samplerate_get(struct snd_kcontrol *kcontrol,
				 struct snd_ctl_elem_value *ucontrol)
{
	struct tascam_card *tascam =
		(struct tascam_card *)snd_kcontrol_chip(kcontrol);
	int err;
	u32 rate = 0;

	scoped_guard(spinlock_irqsave, &tascam->lock) {
		if (tascam->current_rate > 0) {
			ucontrol->value.integer.value[0] = tascam->current_rate;
			return 0;
		}
	}

	u8 *buf __free(kfree) =
		kmalloc(3, GFP_KERNEL);
	if (!buf)
		return -ENOMEM;

	err = usb_control_msg(tascam->dev, usb_rcvctrlpipe(tascam->dev, 0),
			      UAC_GET_CUR, RT_D2H_CLASS_EP,
			      UAC_SAMPLING_FREQ_CONTROL, EP_AUDIO_IN, buf, 3,
			      USB_CTRL_TIMEOUT_MS);

	if (err >= 3)
		rate = buf[0] | (buf[1] << 8) | (buf[2] << 16);

	ucontrol->value.integer.value[0] = rate;
	return 0;
}

/*
 * tascam_samplerate_control - ALSA kcontrol definition for Sample Rate.
 *
 * This defines a new ALSA mixer control named "Sample Rate" that displays
 * the current sample rate of the device. It is a read-only control.
 */
static const struct snd_kcontrol_new tascam_samplerate_control = {
	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
	.name = "Sample Rate",
	.info = tascam_samplerate_info,
	.get = tascam_samplerate_get,
	.access = SNDRV_CTL_ELEM_ACCESS_READ,
};

int tascam_create_controls(struct tascam_card *tascam)
{
	int err;

	err = snd_ctl_add(tascam->card,
			  snd_ctl_new1(&tascam_line_out_control, tascam));
	if (err < 0)
		return err;
	err = snd_ctl_add(tascam->card,
			  snd_ctl_new1(&tascam_digital_out_control, tascam));
	if (err < 0)
		return err;
	err = snd_ctl_add(tascam->card,
			  snd_ctl_new1(&tascam_capture_12_control, tascam));
	if (err < 0)
		return err;
	err = snd_ctl_add(tascam->card,
			  snd_ctl_new1(&tascam_capture_34_control, tascam));
	if (err < 0)
		return err;

	err = snd_ctl_add(tascam->card,
			  snd_ctl_new1(&tascam_samplerate_control, tascam));
	if (err < 0)
		return err;

	return 0;
}