summaryrefslogtreecommitdiff
path: root/sound/usb/usx2y/us144mkii_capture.c
blob: af120bf621733499e632384e9691c7810a72f202 (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
// SPDX-License-Identifier: GPL-2.0-only
// Copyright (c) 2025 Šerif Rami <ramiserifpersia@gmail.com>

#include "us144mkii.h"

/*
 * tascam_capture_open() - Opens the PCM capture substream.
 * @substream: The ALSA PCM substream to open.
 *
 * This function sets the hardware parameters for the capture substream
 * and stores a reference to the substream in the driver's private data.
 *
 * Return: 0 on success.
 */
static int tascam_capture_open(struct snd_pcm_substream *substream)
{
	struct tascam_card *tascam = snd_pcm_substream_chip(substream);

	substream->runtime->hw = tascam_pcm_hw;
	tascam->capture_substream = substream;
	atomic_set(&tascam->capture_active, 0);

	return 0;
}

/*
 * tascam_capture_close() - Closes the PCM capture substream.
 * @substream: The ALSA PCM substream to close.
 *
 * This function clears the reference to the capture substream in the
 * driver's private data.
 *
 * Return: 0 on success.
 */
static int tascam_capture_close(struct snd_pcm_substream *substream)
{
	struct tascam_card *tascam = snd_pcm_substream_chip(substream);

	tascam->capture_substream = NULL;

	return 0;
}

/*
 * tascam_capture_prepare() - Prepares the PCM capture substream for use.
 * @substream: The ALSA PCM substream to prepare.
 *
 * This function initializes capture-related counters and ring buffer pointers.
 *
 * Return: 0 on success.
 */
static int tascam_capture_prepare(struct snd_pcm_substream *substream)
{
	struct tascam_card *tascam = snd_pcm_substream_chip(substream);

	tascam->driver_capture_pos = 0;
	tascam->capture_frames_processed = 0;
	tascam->last_capture_period_pos = 0;
	tascam->capture_ring_buffer_read_ptr = 0;
	tascam->capture_ring_buffer_write_ptr = 0;

	return 0;
}

/*
 * tascam_capture_pointer() - Returns the current capture pointer position.
 * @substream: The ALSA PCM substream.
 *
 * This function returns the current position of the capture pointer within
 * the ALSA ring buffer, in frames.
 *
 * Return: The current capture pointer position in frames.
 */
static snd_pcm_uframes_t
tascam_capture_pointer(struct snd_pcm_substream *substream)
{
	struct tascam_card *tascam = snd_pcm_substream_chip(substream);
	struct snd_pcm_runtime *runtime = substream->runtime;
	u64 pos;

	if (!atomic_read(&tascam->capture_active))
		return 0;

	scoped_guard(spinlock_irqsave, &tascam->lock) {
		pos = tascam->capture_frames_processed;
	}

	if (runtime->buffer_size == 0)
		return 0;

	return do_div(pos, runtime->buffer_size);
}

/*
 * tascam_capture_ops - ALSA PCM operations for capture.
 *
 * This structure defines the callback functions for capture stream operations,
 * including open, close, ioctl, hardware parameters, hardware free, prepare,
 * trigger, and pointer.
 */
const struct snd_pcm_ops tascam_capture_ops = {
	.open = tascam_capture_open,
	.close = tascam_capture_close,
	.ioctl = snd_pcm_lib_ioctl,
	.hw_params = tascam_pcm_hw_params,
	.hw_free = tascam_pcm_hw_free,
	.prepare = tascam_capture_prepare,
	.trigger = tascam_pcm_trigger,
	.pointer = tascam_capture_pointer,
};

/*
 * decode_tascam_capture_block() - Decodes a raw 512-byte block from the device.
 * @src_block: Pointer to the 512-byte raw source block.
 * @dst_block: Pointer to the destination buffer for decoded audio frames.
 *
 * The device sends audio data in a complex, multiplexed format. This function
 * demultiplexes the bits from the raw block into 8 frames of 4-channel,
 * 24-bit audio (stored in 32-bit containers).
 */
static void decode_tascam_capture_block(const u8 *src_block, s32 *dst_block)
{
	int frame, bit;

	memset(dst_block, 0,
	       FRAMES_PER_DECODE_BLOCK * DECODED_CHANNELS_PER_FRAME *
		       DECODED_SAMPLE_SIZE);

	for (frame = 0; frame < FRAMES_PER_DECODE_BLOCK; ++frame) {
		const u8 *p_src_frame_base = src_block + frame * 64;
		s32 *p_dst_frame = dst_block + frame * 4;

		s32 ch[4] = { 0 };

		for (bit = 0; bit < 24; ++bit) {
			u8 byte1 = p_src_frame_base[bit];
			u8 byte2 = p_src_frame_base[bit + 32];

			ch[0] = (ch[0] << 1) | (byte1 & 1);
			ch[2] = (ch[2] << 1) | ((byte1 >> 1) & 1);

			ch[1] = (ch[1] << 1) | (byte2 & 1);
			ch[3] = (ch[3] << 1) | ((byte2 >> 1) & 1);
		}

		/*
		 * The result is a 24-bit sample. Shift left by 8 to align it to
		 * the most significant bits of a 32-bit integer (S32_LE format).
		 */
		p_dst_frame[0] = ch[0] << 8;
		p_dst_frame[1] = ch[1] << 8;
		p_dst_frame[2] = ch[2] << 8;
		p_dst_frame[3] = ch[3] << 8;
	}
}

void tascam_capture_work_handler(struct work_struct *work)
{
	struct tascam_card *tascam =
		container_of(work, struct tascam_card, capture_work);
	struct snd_pcm_substream *substream = tascam->capture_substream;
	struct snd_pcm_runtime *runtime;
	u8 *raw_block = tascam->capture_decode_raw_block;
	s32 *decoded_block = tascam->capture_decode_dst_block;
	s32 *routed_block = tascam->capture_routing_buffer;

	if (!substream || !substream->runtime)
		return;
	runtime = substream->runtime;

	if (!raw_block || !decoded_block || !routed_block) {
		dev_err(tascam->card->dev,
			"Capture decode/routing buffers not allocated!\n");
		return;
	}

	while (atomic_read(&tascam->capture_active)) {
		size_t write_ptr, read_ptr, available_data;
		bool can_process;

		scoped_guard(spinlock_irqsave, &tascam->lock) {
			write_ptr = tascam->capture_ring_buffer_write_ptr;
			read_ptr = tascam->capture_ring_buffer_read_ptr;
			available_data = (write_ptr >= read_ptr) ?
						 (write_ptr - read_ptr) :
						 (CAPTURE_RING_BUFFER_SIZE -
						  read_ptr + write_ptr);
			can_process =
				(available_data >= RAW_BYTES_PER_DECODE_BLOCK);

			if (can_process) {
				size_t bytes_to_end =
					CAPTURE_RING_BUFFER_SIZE - read_ptr;
				if (bytes_to_end >=
				    RAW_BYTES_PER_DECODE_BLOCK) {
					memcpy(raw_block,
					       tascam->capture_ring_buffer +
						       read_ptr,
					       RAW_BYTES_PER_DECODE_BLOCK);
				} else {
					memcpy(raw_block,
					       tascam->capture_ring_buffer +
						       read_ptr,
					       bytes_to_end);
					memcpy(raw_block + bytes_to_end,
					       tascam->capture_ring_buffer,
					       RAW_BYTES_PER_DECODE_BLOCK -
						       bytes_to_end);
				}
				tascam->capture_ring_buffer_read_ptr =
					(read_ptr +
					 RAW_BYTES_PER_DECODE_BLOCK) %
					CAPTURE_RING_BUFFER_SIZE;
			}
		}

		if (!can_process)
			break;

		decode_tascam_capture_block(raw_block, decoded_block);
		process_capture_routing_us144mkii(tascam, decoded_block,
						  routed_block);

		scoped_guard(spinlock_irqsave, &tascam->lock) {
			if (atomic_read(&tascam->capture_active)) {
				int f;

				for (f = 0; f < FRAMES_PER_DECODE_BLOCK; ++f) {
					u8 *dst_frame_start =
						runtime->dma_area +
						frames_to_bytes(
							runtime,
							tascam->driver_capture_pos);
					s32 *routed_frame_start =
						routed_block +
						(f * NUM_CHANNELS);
					int c;

					for (c = 0; c < NUM_CHANNELS; c++) {
						u8 *dst_channel =
							dst_frame_start +
							(c * BYTES_PER_SAMPLE);
						s32 *src_channel_s32 =
							routed_frame_start + c;

						memcpy(dst_channel,
						       ((char *)src_channel_s32) +
							       1,
						       3);
					}

					tascam->driver_capture_pos =
						(tascam->driver_capture_pos +
						 1) %
						runtime->buffer_size;
				}
			}
		}
	}
}

void capture_urb_complete(struct urb *urb)
{
	struct tascam_card *tascam = urb->context;
	int ret;

	if (urb->status) {
		if (urb->status != -ENOENT && urb->status != -ECONNRESET &&
		    urb->status != -ESHUTDOWN && urb->status != -ENODEV &&
		    urb->status != -EPROTO)
			dev_err_ratelimited(tascam->card->dev,
					    "Capture URB failed: %d\n",
					    urb->status);
		goto out;
	}
	if (!tascam || !atomic_read(&tascam->capture_active))
		goto out;

	if (urb->actual_length > 0) {
		scoped_guard(spinlock_irqsave, &tascam->lock) {
			size_t write_ptr = tascam->capture_ring_buffer_write_ptr;
			size_t bytes_to_end = CAPTURE_RING_BUFFER_SIZE - write_ptr;

			if (urb->actual_length > bytes_to_end) {
				memcpy(tascam->capture_ring_buffer + write_ptr,
				       urb->transfer_buffer, bytes_to_end);
				memcpy(tascam->capture_ring_buffer,
				       urb->transfer_buffer + bytes_to_end,
				       urb->actual_length - bytes_to_end);
			} else {
				memcpy(tascam->capture_ring_buffer + write_ptr,
				       urb->transfer_buffer,
				       urb->actual_length);
			}

			tascam->capture_ring_buffer_write_ptr =
				(write_ptr + urb->actual_length) %
				CAPTURE_RING_BUFFER_SIZE;
		}

		schedule_work(&tascam->capture_work);
	}

	usb_get_urb(urb);
	usb_anchor_urb(urb, &tascam->capture_anchor);
	ret = usb_submit_urb(urb, GFP_ATOMIC);
	if (ret < 0) {
		dev_err_ratelimited(tascam->card->dev,
				    "Failed to resubmit capture URB: %d\n",
				    ret);
		usb_unanchor_urb(urb);
		usb_put_urb(urb);
		atomic_dec(
			&tascam->active_urbs); /* Decrement on failed resubmission */
	}
out:
	usb_put_urb(urb);
}