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
445
446
447
448
449
450
451
452
453
454
455
456
|
// SPDX-License-Identifier: GPL-2.0-only
// Copyright (c) 2025 Šerif Rami <ramiserifpersia@gmail.com>
#include "us144mkii.h"
/*
* tascam_playback_open() - Opens the PCM playback substream.
* @substream: The ALSA PCM substream to open.
*
* This function sets the hardware parameters for the playback substream
* and stores a reference to the substream in the driver's private data.
*
* Return: 0 on success.
*/
static int tascam_playback_open(struct snd_pcm_substream *substream)
{
struct tascam_card *tascam = snd_pcm_substream_chip(substream);
substream->runtime->hw = tascam_pcm_hw;
tascam->playback_substream = substream;
atomic_set(&tascam->playback_active, 0);
return 0;
}
/*
* tascam_playback_close() - Closes the PCM playback substream.
* @substream: The ALSA PCM substream to close.
*
* This function clears the reference to the playback substream in the
* driver's private data.
*
* Return: 0 on success.
*/
static int tascam_playback_close(struct snd_pcm_substream *substream)
{
struct tascam_card *tascam = snd_pcm_substream_chip(substream);
tascam->playback_substream = NULL;
return 0;
}
/*
* tascam_playback_prepare() - Prepares the PCM playback substream for use.
* @substream: The ALSA PCM substream to prepare.
*
* This function initializes playback-related counters and flags, and configures
* the playback URBs with appropriate packet sizes based on the nominal frame
* rate.
*
* Return: 0 on success.
*/
static int tascam_playback_prepare(struct snd_pcm_substream *substream)
{
struct tascam_card *tascam = snd_pcm_substream_chip(substream);
struct snd_pcm_runtime *runtime = substream->runtime;
int i, u;
size_t nominal_frames_per_packet, nominal_bytes_per_packet;
size_t total_bytes_in_urb;
tascam->driver_playback_pos = 0;
tascam->playback_frames_consumed = 0;
tascam->last_period_pos = 0;
tascam->feedback_pattern_in_idx = 0;
tascam->feedback_pattern_out_idx = 0;
tascam->feedback_synced = false;
tascam->feedback_consecutive_errors = 0;
tascam->feedback_urb_skip_count = NUM_FEEDBACK_URBS;
nominal_frames_per_packet = runtime->rate / 8000;
for (i = 0; i < FEEDBACK_ACCUMULATOR_SIZE; i++)
tascam->feedback_accumulator_pattern[i] =
nominal_frames_per_packet;
for (i = 0; i < NUM_FEEDBACK_URBS; i++) {
struct urb *f_urb = tascam->feedback_urbs[i];
int j;
f_urb->number_of_packets = FEEDBACK_URB_PACKETS;
f_urb->transfer_buffer_length =
FEEDBACK_URB_PACKETS * FEEDBACK_PACKET_SIZE;
for (j = 0; j < FEEDBACK_URB_PACKETS; j++) {
f_urb->iso_frame_desc[j].offset =
j * FEEDBACK_PACKET_SIZE;
f_urb->iso_frame_desc[j].length = FEEDBACK_PACKET_SIZE;
}
}
nominal_bytes_per_packet = nominal_frames_per_packet * BYTES_PER_FRAME;
total_bytes_in_urb = nominal_bytes_per_packet * PLAYBACK_URB_PACKETS;
for (u = 0; u < NUM_PLAYBACK_URBS; u++) {
struct urb *urb = tascam->playback_urbs[u];
memset(urb->transfer_buffer, 0,
tascam->playback_urb_alloc_size);
urb->transfer_buffer_length = total_bytes_in_urb;
urb->number_of_packets = PLAYBACK_URB_PACKETS;
for (i = 0; i < PLAYBACK_URB_PACKETS; i++) {
urb->iso_frame_desc[i].offset =
i * nominal_bytes_per_packet;
urb->iso_frame_desc[i].length =
nominal_bytes_per_packet;
}
}
return 0;
}
/*
* tascam_playback_pointer() - Returns the current playback pointer position.
* @substream: The ALSA PCM substream.
*
* This function returns the current position of the playback pointer within
* the ALSA ring buffer, in frames.
*
* Return: The current playback pointer position in frames.
*/
static snd_pcm_uframes_t
tascam_playback_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->playback_active))
return 0;
scoped_guard(spinlock_irqsave, &tascam->lock) {
pos = tascam->playback_frames_consumed;
}
if (runtime->buffer_size == 0)
return 0;
return do_div(pos, runtime->buffer_size);
}
/*
* tascam_playback_ops - ALSA PCM operations for playback.
*
* This structure defines the callback functions for playback stream operations,
* including open, close, ioctl, hardware parameters, hardware free, prepare,
* trigger, and pointer.
*/
const struct snd_pcm_ops tascam_playback_ops = {
.open = tascam_playback_open,
.close = tascam_playback_close,
.ioctl = snd_pcm_lib_ioctl,
.hw_params = tascam_pcm_hw_params,
.hw_free = tascam_pcm_hw_free,
.prepare = tascam_playback_prepare,
.trigger = tascam_pcm_trigger,
.pointer = tascam_playback_pointer,
};
void playback_urb_complete(struct urb *urb)
{
struct tascam_card *tascam = urb->context;
struct snd_pcm_substream *substream;
struct snd_pcm_runtime *runtime;
size_t total_bytes_for_urb = 0;
snd_pcm_uframes_t offset_frames;
snd_pcm_uframes_t frames_to_copy;
int ret, i;
if (urb->status) {
if (urb->status != -ENOENT && urb->status != -ECONNRESET &&
urb->status != -ESHUTDOWN && urb->status != -ENODEV)
dev_err_ratelimited(tascam->card->dev,
"Playback URB failed: %d\n",
urb->status);
goto out;
}
if (!tascam || !atomic_read(&tascam->playback_active))
goto out;
substream = tascam->playback_substream;
if (!substream || !substream->runtime)
goto out;
runtime = substream->runtime;
scoped_guard(spinlock_irqsave, &tascam->lock) {
for (i = 0; i < urb->number_of_packets; i++) {
unsigned int frames_for_packet;
size_t bytes_for_packet;
if (tascam->feedback_synced) {
frames_for_packet =
tascam->feedback_accumulator_pattern
[tascam->feedback_pattern_out_idx];
tascam->feedback_pattern_out_idx =
(tascam->feedback_pattern_out_idx + 1) %
FEEDBACK_ACCUMULATOR_SIZE;
} else {
frames_for_packet = runtime->rate / 8000;
}
bytes_for_packet = frames_for_packet * BYTES_PER_FRAME;
urb->iso_frame_desc[i].offset = total_bytes_for_urb;
urb->iso_frame_desc[i].length = bytes_for_packet;
total_bytes_for_urb += bytes_for_packet;
}
urb->transfer_buffer_length = total_bytes_for_urb;
offset_frames = tascam->driver_playback_pos;
frames_to_copy = bytes_to_frames(runtime, total_bytes_for_urb);
tascam->driver_playback_pos =
(offset_frames + frames_to_copy) % runtime->buffer_size;
}
if (total_bytes_for_urb > 0) {
u8 *dst_buf = urb->transfer_buffer;
/* Handle ring buffer wrap-around */
if (offset_frames + frames_to_copy > runtime->buffer_size) {
size_t first_chunk_bytes = frames_to_bytes(
runtime, runtime->buffer_size - offset_frames);
size_t second_chunk_bytes =
total_bytes_for_urb - first_chunk_bytes;
memcpy(dst_buf,
runtime->dma_area +
frames_to_bytes(runtime, offset_frames),
first_chunk_bytes);
memcpy(dst_buf + first_chunk_bytes, runtime->dma_area,
second_chunk_bytes);
} else {
memcpy(dst_buf,
runtime->dma_area +
frames_to_bytes(runtime, offset_frames),
total_bytes_for_urb);
}
process_playback_routing_us144mkii(tascam, dst_buf, dst_buf,
frames_to_copy);
}
urb->dev = tascam->dev;
usb_get_urb(urb);
usb_anchor_urb(urb, &tascam->playback_anchor);
ret = usb_submit_urb(urb, GFP_ATOMIC);
if (ret < 0) {
dev_err_ratelimited(tascam->card->dev,
"Failed to resubmit playback 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);
}
void feedback_urb_complete(struct urb *urb)
{
struct tascam_card *tascam = urb->context;
struct snd_pcm_substream *playback_ss, *capture_ss;
struct snd_pcm_runtime *playback_rt, *capture_rt;
u64 total_frames_in_urb = 0;
int ret, p;
unsigned int old_in_idx, new_in_idx;
bool playback_period_elapsed = false;
bool capture_period_elapsed = false;
if (urb->status) {
if (urb->status != -ENOENT && urb->status != -ECONNRESET &&
urb->status != -ESHUTDOWN && urb->status != -ENODEV) {
dev_err_ratelimited(tascam->card->dev,
"Feedback URB failed: %d\n",
urb->status);
atomic_dec(
&tascam->active_urbs); /* Decrement on failed resubmission */
}
goto out;
}
if (!tascam || !atomic_read(&tascam->playback_active))
goto out;
playback_ss = tascam->playback_substream;
if (!playback_ss || !playback_ss->runtime)
goto out;
playback_rt = playback_ss->runtime;
capture_ss = tascam->capture_substream;
capture_rt = capture_ss ? capture_ss->runtime : NULL;
scoped_guard(spinlock_irqsave, &tascam->lock) {
if (tascam->feedback_urb_skip_count > 0) {
tascam->feedback_urb_skip_count--;
break;
}
old_in_idx = tascam->feedback_pattern_in_idx;
for (p = 0; p < urb->number_of_packets; p++) {
u8 feedback_value = 0;
const unsigned int *pattern;
bool packet_ok =
(urb->iso_frame_desc[p].status == 0 &&
urb->iso_frame_desc[p].actual_length >= 1);
if (packet_ok)
feedback_value =
*((u8 *)urb->transfer_buffer +
urb->iso_frame_desc[p].offset);
if (packet_ok) {
int delta = feedback_value -
tascam->fpo.base_feedback_value +
tascam->fpo.feedback_offset;
int pattern_idx;
if (delta < 0) {
pattern_idx =
0; // Clamp to the lowest pattern
} else if (delta >= 5) {
pattern_idx =
4; // Clamp to the highest pattern
} else {
pattern_idx = delta;
}
pattern =
tascam->fpo
.full_frame_patterns[pattern_idx];
tascam->feedback_consecutive_errors = 0;
int i;
for (i = 0; i < 8; i++) {
unsigned int in_idx =
(tascam->feedback_pattern_in_idx +
i) %
FEEDBACK_ACCUMULATOR_SIZE;
tascam->feedback_accumulator_pattern
[in_idx] = pattern[i];
total_frames_in_urb += pattern[i];
}
} else {
unsigned int nominal_frames =
playback_rt->rate / 8000;
int i;
if (tascam->feedback_synced) {
tascam->feedback_consecutive_errors++;
if (tascam->feedback_consecutive_errors >
FEEDBACK_SYNC_LOSS_THRESHOLD) {
dev_err(tascam->card->dev,
"Fatal: Feedback sync lost. Stopping stream.\n");
schedule_work(
&tascam->stop_pcm_work);
tascam->feedback_synced = false;
break;
}
}
for (i = 0; i < 8; i++) {
unsigned int in_idx =
(tascam->feedback_pattern_in_idx +
i) %
FEEDBACK_ACCUMULATOR_SIZE;
tascam->feedback_accumulator_pattern
[in_idx] = nominal_frames;
total_frames_in_urb += nominal_frames;
}
}
tascam->feedback_pattern_in_idx =
(tascam->feedback_pattern_in_idx + 8) %
FEEDBACK_ACCUMULATOR_SIZE;
}
new_in_idx = tascam->feedback_pattern_in_idx;
if (!tascam->feedback_synced) {
unsigned int out_idx = tascam->feedback_pattern_out_idx;
bool is_ahead = (new_in_idx - out_idx) %
FEEDBACK_ACCUMULATOR_SIZE <
(FEEDBACK_ACCUMULATOR_SIZE / 2);
bool was_behind = (old_in_idx - out_idx) %
FEEDBACK_ACCUMULATOR_SIZE >=
(FEEDBACK_ACCUMULATOR_SIZE / 2);
if (is_ahead && was_behind) {
dev_dbg(tascam->card->dev,
"Sync Acquired! (in: %u, out: %u)\n",
new_in_idx, out_idx);
tascam->feedback_synced = true;
tascam->feedback_consecutive_errors = 0;
}
}
if (total_frames_in_urb > 0) {
tascam->playback_frames_consumed += total_frames_in_urb;
if (atomic_read(&tascam->capture_active))
tascam->capture_frames_processed +=
total_frames_in_urb;
}
if (playback_rt->period_size > 0) {
u64 current_period =
div_u64(tascam->playback_frames_consumed,
playback_rt->period_size);
if (current_period > tascam->last_period_pos) {
tascam->last_period_pos = current_period;
playback_period_elapsed = true;
}
}
if (atomic_read(&tascam->capture_active) && capture_rt &&
capture_rt->period_size > 0) {
u64 current_capture_period =
div_u64(tascam->capture_frames_processed,
capture_rt->period_size);
if (current_capture_period >
tascam->last_capture_period_pos) {
tascam->last_capture_period_pos =
current_capture_period;
capture_period_elapsed = true;
}
}
}
if (playback_period_elapsed)
snd_pcm_period_elapsed(playback_ss);
if (capture_period_elapsed)
snd_pcm_period_elapsed(capture_ss);
urb->dev = tascam->dev;
usb_get_urb(urb);
usb_anchor_urb(urb, &tascam->feedback_anchor);
ret = usb_submit_urb(urb, GFP_ATOMIC);
if (ret < 0) {
dev_err_ratelimited(tascam->card->dev,
"Failed to resubmit feedback URB: %d\n",
ret);
usb_unanchor_urb(urb);
usb_put_urb(urb);
}
out:
usb_put_urb(urb);
}
void tascam_stop_pcm_work_handler(struct work_struct *work)
{
struct tascam_card *tascam =
container_of(work, struct tascam_card, stop_pcm_work);
if (tascam->playback_substream)
snd_pcm_stop(tascam->playback_substream, SNDRV_PCM_STATE_XRUN);
if (tascam->capture_substream)
snd_pcm_stop(tascam->capture_substream, SNDRV_PCM_STATE_XRUN);
}
|