feat:stability and spec, new libusb tool for testing audio performance
This commit is contained in:
parent
ded6ed5652
commit
ab377f904c
|
|
@ -0,0 +1,118 @@
|
|||
#!/bin/bash
|
||||
|
||||
# MIT License
|
||||
# Copyright (c) 2025 serifpersia
|
||||
#
|
||||
# Interactive launcher for the TASCAM US-144MKII FIFO streamer.
|
||||
# Prompts for sample rate, latency profile, and logging options,
|
||||
# then configures PulseAudio and the C streamer binary accordingly.
|
||||
|
||||
# --- Configuration ---
|
||||
SINK_NAME="TASCAM-US144MKII-OUT"
|
||||
FIFO_PLAYBACK_PATH="/tmp/tascam-audio-playback"
|
||||
STREAMER_BINARY="./tascam_streamer" # Assumes the C program is in the same directory
|
||||
CHANNELS="2"
|
||||
FORMAT="s24le"
|
||||
|
||||
# --- Cleanup Function ---
|
||||
cleanup() {
|
||||
echo ""
|
||||
echo "--- Running cleanup... ---"
|
||||
|
||||
pkill -f "$STREAMER_BINARY" 2>/dev/null
|
||||
sleep 0.5
|
||||
|
||||
echo "Unloading PulseAudio module..."
|
||||
pactl unload-module module-pipe-sink 2>/dev/null
|
||||
|
||||
echo "Removing FIFO file..."
|
||||
rm -f "$FIFO_PLAYBACK_PATH"
|
||||
|
||||
echo "--- Cleanup complete. ---"
|
||||
exit 0
|
||||
}
|
||||
|
||||
# Trap signals to ensure cleanup runs
|
||||
trap cleanup SIGINT TERM EXIT
|
||||
|
||||
# --- Interactive Setup ---
|
||||
echo "--- TASCAM Streamer Interactive Setup ---"
|
||||
|
||||
# 1. Select Sample Rate
|
||||
rates=("44100" "48000" "88200" "96000")
|
||||
PS3="Please select a sample rate: "
|
||||
select rate_choice in "${rates[@]}"; do
|
||||
if [[ -n "$rate_choice" ]]; then
|
||||
SELECTED_RATE="$rate_choice"
|
||||
echo "Selected rate: $SELECTED_RATE Hz"
|
||||
break
|
||||
else
|
||||
echo "Invalid selection. Please try again."
|
||||
fi
|
||||
done
|
||||
echo ""
|
||||
|
||||
# 2. Select Latency Profile
|
||||
profiles=("0: Lowest" "1: Low" "2: Normal" "3: High" "4: Highest")
|
||||
PS3="Please select a latency profile: "
|
||||
select profile_choice in "${profiles[@]}"; do
|
||||
if [[ -n "$profile_choice" ]]; then
|
||||
SELECTED_PROFILE_INDEX=$((REPLY - 1))
|
||||
echo "Selected profile: $profile_choice"
|
||||
break
|
||||
else
|
||||
echo "Invalid selection. Please try again."
|
||||
fi
|
||||
done
|
||||
echo ""
|
||||
|
||||
# 3. Select Logging Mode
|
||||
LOG_MODE_FLAG=""
|
||||
LOG_INTERVAL_FLAG=""
|
||||
read -p "Use minimal logging instead of the live dashboard? (y/n) [default: n]: " minimal_choice
|
||||
if [[ "$minimal_choice" == "y" || "$minimal_choice" == "Y" ]]; then
|
||||
LOG_MODE_FLAG="--minimal-log"
|
||||
read -p "Enter log interval in milliseconds [default: 1000]: " interval_ms
|
||||
if [[ -z "$interval_ms" ]]; then
|
||||
interval_ms=1000 # Set default if user enters nothing
|
||||
fi
|
||||
LOG_INTERVAL_FLAG="--log-interval $interval_ms"
|
||||
LOG_MODE_SUMMARY="Minimal (updates every ${interval_ms}ms)"
|
||||
else
|
||||
LOG_MODE_SUMMARY="Live Dashboard (updates every 100ms)"
|
||||
fi
|
||||
|
||||
echo "---------------------------------------------"
|
||||
echo "Configuration:"
|
||||
echo " Rate: $SELECTED_RATE Hz"
|
||||
echo " Profile: $SELECTED_PROFILE_INDEX ($profile_choice)"
|
||||
echo " Logging: $LOG_MODE_SUMMARY"
|
||||
echo "---------------------------------------------"
|
||||
|
||||
# --- Main Execution ---
|
||||
rm -f "$FIFO_PLAYBACK_PATH"
|
||||
echo "Creating playback FIFO at $FIFO_PLAYBACK_PATH..."
|
||||
mkfifo "$FIFO_PLAYBACK_PATH"
|
||||
|
||||
echo "Loading PulseAudio pipe-sink module..."
|
||||
SINK_MODULE_ID=$(pactl load-module module-pipe-sink file="$FIFO_PLAYBACK_PATH" sink_name="$SINK_NAME" format=$FORMAT rate=$SELECTED_RATE channels=$CHANNELS)
|
||||
if [ -z "$SINK_MODULE_ID" ]; then
|
||||
echo "Error: Failed to load PulseAudio pipe-sink module. Aborting."
|
||||
exit 1
|
||||
fi
|
||||
echo "Playback Sink ('$SINK_NAME') loaded with ID: $SINK_MODULE_ID"
|
||||
echo "You can now select '$SINK_NAME' as an output device in your sound settings."
|
||||
echo "---------------------------------------------"
|
||||
|
||||
echo "Starting C streamer binary..."
|
||||
# Launch the C program with all selected arguments.
|
||||
# The log flags will be empty strings if not selected, which bash ignores.
|
||||
sudo "$STREAMER_BINARY" \
|
||||
-r "$SELECTED_RATE" \
|
||||
-p "$SELECTED_PROFILE_INDEX" \
|
||||
--pipe "$FIFO_PLAYBACK_PATH" \
|
||||
$LOG_MODE_FLAG \
|
||||
$LOG_INTERVAL_FLAG
|
||||
|
||||
echo "Streamer exited. Waiting for cleanup..."
|
||||
wait
|
||||
|
|
@ -0,0 +1,530 @@
|
|||
// MIT License
|
||||
// Copyright (c) 2025 serifpersia
|
||||
//
|
||||
// Final verification tool by an AI assistant. This version is a fully functional,
|
||||
// multi-rate, multi-profile FIFO audio player with selectable logging modes for
|
||||
// either deep diagnostics or minimal-overhead monitoring.
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <libusb-1.0/libusb.h>
|
||||
#include <stdbool.h>
|
||||
#include <signal.h>
|
||||
#include <stdint.h>
|
||||
#include <math.h>
|
||||
#include <pthread.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/time.h>
|
||||
#include <float.h>
|
||||
#include <fcntl.h>
|
||||
#include <errno.h>
|
||||
|
||||
// --- Device and Endpoint Configuration ---
|
||||
#define TASCAM_VID 0x0644
|
||||
#define TASCAM_PID 0x8020
|
||||
#define EP_AUDIO_OUT 0x02
|
||||
#define EP_PLAYBACK_FEEDBACK 0x81
|
||||
#define EP_CAPTURE_DATA 0x86
|
||||
|
||||
// --- USB Request Types ---
|
||||
#define RT_H2D_CLASS_EP 0x22
|
||||
#define RT_D2H_VENDOR_DEV 0xc0
|
||||
#define RT_H2D_VENDOR_DEV 0x40
|
||||
|
||||
// --- UAC / Vendor Requests ---
|
||||
#define UAC_SET_CUR 0x01
|
||||
#define UAC_SAMPLING_FREQ_CONTROL 0x0100
|
||||
#define VENDOR_REQ_REGISTER_WRITE 65
|
||||
#define VENDOR_REQ_MODE_CONTROL 73
|
||||
|
||||
// --- Streaming Configuration ---
|
||||
#define BYTES_PER_SAMPLE 3
|
||||
#define DEVICE_CHANNELS 4
|
||||
#define PIPE_CHANNELS 2
|
||||
#define DEVICE_FRAME_SIZE (DEVICE_CHANNELS * BYTES_PER_SAMPLE)
|
||||
#define PIPE_FRAME_SIZE (PIPE_CHANNELS * BYTES_PER_SAMPLE)
|
||||
#define ISO_PLAYBACK_PACKETS_PER_TRANSFER 40
|
||||
#define NUM_PLAYBACK_TRANSFERS 4
|
||||
#define NUM_FEEDBACK_TRANSFERS 4
|
||||
#define FEEDBACK_PACKET_SIZE 3
|
||||
#define MAX_FEEDBACK_PACKETS_PER_URB 5
|
||||
#define USB_TIMEOUT 1000
|
||||
|
||||
// --- Feedback Synchronization Engine ---
|
||||
#define FEEDBACK_ACCUMULATOR_SIZE 128
|
||||
#define WARMUP_THRESHOLD (ISO_PLAYBACK_PACKETS_PER_TRANSFER * 2)
|
||||
|
||||
// --- Data Structures for Rate/Profile Configuration ---
|
||||
struct latency_profile_config {
|
||||
const char *name;
|
||||
int feedback_packets_per_urb;
|
||||
int asio_buffer_size_frames;
|
||||
double expected_feedback_ms;
|
||||
};
|
||||
|
||||
struct sample_rate_config {
|
||||
int rate;
|
||||
const unsigned char rate_data[3];
|
||||
uint16_t rate_vendor_wValue;
|
||||
const unsigned int (*feedback_patterns)[8];
|
||||
unsigned int feedback_base_value;
|
||||
unsigned int feedback_max_value;
|
||||
const struct latency_profile_config profiles[5];
|
||||
};
|
||||
|
||||
// --- Pre-calculated Pattern Tables ---
|
||||
static const unsigned int patterns_44khz[5][8] = {
|
||||
{5, 5, 5, 6, 5, 5, 5, 6}, {5, 5, 6, 5, 5, 6, 5, 6},
|
||||
{5, 6, 5, 6, 5, 6, 5, 6}, {6, 5, 6, 6, 5, 6, 5, 6},
|
||||
{6, 6, 6, 5, 6, 6, 6, 5}
|
||||
};
|
||||
static const unsigned int patterns_48khz[5][8] = {
|
||||
{5, 6, 6, 6, 5, 6, 6, 6}, {5, 6, 6, 6, 6, 6, 6, 6},
|
||||
{6, 6, 6, 6, 6, 6, 6, 6}, {7, 6, 6, 6, 6, 6, 6, 6},
|
||||
{7, 6, 6, 6, 7, 6, 6, 6}
|
||||
};
|
||||
static const unsigned int patterns_88khz[5][8] = {
|
||||
{10, 11, 11, 11, 10, 11, 11, 11}, {10, 11, 11, 11, 11, 11, 11, 11},
|
||||
{11, 11, 11, 11, 11, 11, 11, 11}, {12, 11, 11, 11, 11, 11, 11, 11},
|
||||
{12, 11, 11, 11, 12, 11, 11, 11}
|
||||
};
|
||||
static const unsigned int patterns_96khz[5][8] = {
|
||||
{11, 12, 12, 12, 11, 12, 12, 12}, {11, 12, 12, 12, 12, 12, 12, 12},
|
||||
{12, 12, 12, 12, 12, 12, 12, 12}, {13, 12, 12, 12, 12, 12, 12, 12},
|
||||
{13, 12, 12, 12, 13, 12, 12, 12}
|
||||
};
|
||||
|
||||
// --- Global Configuration Table ---
|
||||
static const struct sample_rate_config g_rate_configs[] = {
|
||||
{ 44100, {0x44, 0xac, 0x00}, 0x1000, patterns_44khz, 42, 46, { {"Lowest",1,49,2.0}, {"Low",1,64,2.0}, {"Normal",2,128,2.0}, {"High",5,256,5.0}, {"Highest",5,512,5.0} } },
|
||||
{ 48000, {0x80, 0xbb, 0x00}, 0x1002, patterns_48khz, 46, 50, { {"Lowest",1,48,1.0}, {"Low",1,64,2.0}, {"Normal",2,128,2.0}, {"High",5,256,5.0}, {"Highest",5,512,5.0} } },
|
||||
{ 88200, {0x88, 0x58, 0x01}, 0x1008, patterns_88khz, 86, 90, { {"Lowest",1,98,1.0}, {"Low",1,128,2.0}, {"Normal",2,256,2.0}, {"High",5,512,5.0}, {"Highest",5,1024,5.0} } },
|
||||
{ 96000, {0x00, 0x77, 0x01}, 0x100a, patterns_96khz, 94, 98, { {"Lowest",1,96,1.0}, {"Low",1,128,2.0}, {"Normal",2,256,2.0}, {"High",5,512,5.0}, {"Highest",5,1024,5.0} } }
|
||||
};
|
||||
#define NUM_SUPPORTED_RATES (sizeof(g_rate_configs) / sizeof(g_rate_configs[0]))
|
||||
#define NUM_PROFILES 5
|
||||
|
||||
// --- Global State ---
|
||||
static volatile bool is_running = true;
|
||||
|
||||
struct stream_state {
|
||||
int fifo_fd;
|
||||
pthread_mutex_t lock;
|
||||
const struct sample_rate_config *rate_cfg;
|
||||
const struct latency_profile_config *profile_cfg;
|
||||
unsigned int feedback_accumulator_pattern[FEEDBACK_ACCUMULATOR_SIZE];
|
||||
unsigned int feedback_pattern_out_idx;
|
||||
unsigned int feedback_pattern_in_idx;
|
||||
bool feedback_synced;
|
||||
bool feedback_warmed_up;
|
||||
int last_feedback_value;
|
||||
struct timeval last_feedback_completion_time;
|
||||
double last_feedback_interval_ms;
|
||||
double min_feedback_interval_ms;
|
||||
double max_feedback_interval_ms;
|
||||
double avg_feedback_interval_sum;
|
||||
unsigned long feedback_interval_count;
|
||||
unsigned long underrun_count;
|
||||
unsigned long overrun_count;
|
||||
};
|
||||
|
||||
struct logging_thread_args {
|
||||
struct stream_state *state;
|
||||
bool minimal_log;
|
||||
int log_interval_ms;
|
||||
};
|
||||
|
||||
// --- Function Prototypes ---
|
||||
void print_usage(const char *prog_name);
|
||||
int perform_initialization_sequence(libusb_device_handle *handle, const struct sample_rate_config *rate_config);
|
||||
static void LIBUSB_CALL iso_playback_callback(struct libusb_transfer *transfer);
|
||||
static void LIBUSB_CALL feedback_callback(struct libusb_transfer *transfer);
|
||||
void *logging_thread_func(void *arg);
|
||||
double timeval_diff_ms(struct timeval *start, struct timeval *end);
|
||||
|
||||
void sigint_handler(int signum) {
|
||||
if (is_running) {
|
||||
printf("\n\n\n\n\nCtrl+C detected, stopping...\n");
|
||||
is_running = false;
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
int sample_rate = 0;
|
||||
int profile_index = -1;
|
||||
const char *pipe_path = NULL;
|
||||
bool minimal_log = false;
|
||||
int log_interval_ms = 100; // Default to 100ms for dashboard
|
||||
|
||||
for (int i = 1; i < argc; i++) {
|
||||
if (strcmp(argv[i], "-r") == 0 && i + 1 < argc) sample_rate = atoi(argv[++i]);
|
||||
else if (strcmp(argv[i], "-p") == 0 && i + 1 < argc) profile_index = atoi(argv[++i]);
|
||||
else if (strcmp(argv[i], "--pipe") == 0 && i + 1 < argc) pipe_path = argv[++i];
|
||||
else if (strcmp(argv[i], "--minimal-log") == 0) minimal_log = true;
|
||||
else if (strcmp(argv[i], "--log-interval") == 0 && i + 1 < argc) log_interval_ms = atoi(argv[++i]);
|
||||
}
|
||||
|
||||
if (sample_rate == 0 || profile_index < 0 || !pipe_path) {
|
||||
print_usage(argv[0]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
const struct sample_rate_config *rate_config = NULL;
|
||||
for (unsigned int i = 0; i < NUM_SUPPORTED_RATES; i++) {
|
||||
if (g_rate_configs[i].rate == sample_rate) {
|
||||
rate_config = &g_rate_configs[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!rate_config) {
|
||||
fprintf(stderr, "Error: Sample rate %d is not supported.\n", sample_rate);
|
||||
print_usage(argv[0]);
|
||||
return 1;
|
||||
}
|
||||
if (profile_index >= NUM_PROFILES) {
|
||||
fprintf(stderr, "Error: Invalid profile index %d.\n", profile_index);
|
||||
print_usage(argv[0]);
|
||||
return 1;
|
||||
}
|
||||
const struct latency_profile_config *profile_config = &rate_config->profiles[profile_index];
|
||||
|
||||
libusb_device_handle *handle = NULL;
|
||||
struct libusb_transfer *playback_transfers[NUM_PLAYBACK_TRANSFERS] = {0};
|
||||
struct libusb_transfer *feedback_transfers[NUM_FEEDBACK_TRANSFERS] = {0};
|
||||
struct stream_state state = { .fifo_fd = -1 };
|
||||
struct logging_thread_args log_args = { &state, minimal_log, log_interval_ms };
|
||||
pthread_t logging_thread = 0;
|
||||
bool kernel_driver_was_active[2] = {false, false};
|
||||
int r = 0;
|
||||
|
||||
const int max_frames_per_packet = (rate_config->rate / 8000) + 2;
|
||||
const int playback_packet_max_size = max_frames_per_packet * DEVICE_FRAME_SIZE;
|
||||
const int playback_transfer_size = playback_packet_max_size * ISO_PLAYBACK_PACKETS_PER_TRANSFER;
|
||||
const int feedback_transfer_size = FEEDBACK_PACKET_SIZE * MAX_FEEDBACK_PACKETS_PER_URB;
|
||||
|
||||
printf("--- TASCAM US-144MKII FIFO Streamer ---\n");
|
||||
printf("Profile: %d, Rate: %d Hz, Latency: %s (%d-sample buffer)\n",
|
||||
profile_index, rate_config->rate, profile_config->name, profile_config->asio_buffer_size_frames);
|
||||
printf("Config: Feedback URB contains %d packet(s), expected interval %.1f ms.\n",
|
||||
profile_config->feedback_packets_per_urb, profile_config->expected_feedback_ms);
|
||||
printf("Pipe: Reading 24-bit stereo audio from %s\n", pipe_path);
|
||||
|
||||
pthread_mutex_init(&state.lock, NULL);
|
||||
state.rate_cfg = rate_config;
|
||||
state.profile_cfg = profile_config;
|
||||
state.min_feedback_interval_ms = DBL_MAX;
|
||||
|
||||
state.fifo_fd = open(pipe_path, O_RDONLY | O_NONBLOCK);
|
||||
if (state.fifo_fd < 0) {
|
||||
perror("Error opening FIFO pipe");
|
||||
return 1;
|
||||
}
|
||||
|
||||
signal(SIGINT, sigint_handler);
|
||||
if (libusb_init(NULL) < 0) { r = 1; goto cleanup; }
|
||||
|
||||
handle = libusb_open_device_with_vid_pid(NULL, TASCAM_VID, TASCAM_PID);
|
||||
if (!handle) { fprintf(stderr, "Device not found\n"); r = 1; goto cleanup; }
|
||||
|
||||
for (int i = 0; i < 2; i++) {
|
||||
if (libusb_kernel_driver_active(handle, i)) {
|
||||
kernel_driver_was_active[i] = true;
|
||||
if ((r = libusb_detach_kernel_driver(handle, i)) != 0) {
|
||||
fprintf(stderr, "Could not detach kernel driver for interface %d: %s\n", i, libusb_error_name(r));
|
||||
r = 1; goto cleanup;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (perform_initialization_sequence(handle, rate_config) != 0) {
|
||||
fprintf(stderr, "Device configuration failed.\n"); r = 1; goto cleanup;
|
||||
}
|
||||
|
||||
printf("Starting streams... (waiting for buffer warm-up)\n");
|
||||
for (int i = 0; i < NUM_PLAYBACK_TRANSFERS; i++) {
|
||||
playback_transfers[i] = libusb_alloc_transfer(ISO_PLAYBACK_PACKETS_PER_TRANSFER);
|
||||
unsigned char *buf = malloc(playback_transfer_size);
|
||||
memset(buf, 0, playback_transfer_size);
|
||||
libusb_fill_iso_transfer(playback_transfers[i], handle, EP_AUDIO_OUT, buf, playback_transfer_size, ISO_PLAYBACK_PACKETS_PER_TRANSFER, iso_playback_callback, &state, USB_TIMEOUT);
|
||||
int nominal_packet_size = (rate_config->rate / 8000) * DEVICE_FRAME_SIZE;
|
||||
libusb_set_iso_packet_lengths(playback_transfers[i], nominal_packet_size);
|
||||
libusb_submit_transfer(playback_transfers[i]);
|
||||
}
|
||||
|
||||
for (int i = 0; i < NUM_FEEDBACK_TRANSFERS; i++) {
|
||||
feedback_transfers[i] = libusb_alloc_transfer(profile_config->feedback_packets_per_urb);
|
||||
unsigned char *buf = malloc(feedback_transfer_size);
|
||||
libusb_fill_iso_transfer(feedback_transfers[i], handle, EP_PLAYBACK_FEEDBACK, buf, feedback_transfer_size, profile_config->feedback_packets_per_urb, feedback_callback, &state, USB_TIMEOUT);
|
||||
libusb_set_iso_packet_lengths(feedback_transfers[i], FEEDBACK_PACKET_SIZE);
|
||||
libusb_submit_transfer(feedback_transfers[i]);
|
||||
}
|
||||
|
||||
if (pthread_create(&logging_thread, NULL, logging_thread_func, &log_args) != 0) {
|
||||
fprintf(stderr, "Failed to create logging thread.\n");
|
||||
is_running = false;
|
||||
}
|
||||
|
||||
printf("Draining stale data from FIFO pipe to ensure stream alignment...\n");
|
||||
char drain_buf[4096];
|
||||
while (read(state.fifo_fd, drain_buf, sizeof(drain_buf)) > 0);
|
||||
|
||||
printf("\n--- Playback active. Press Ctrl+C to stop. ---\n");
|
||||
if (!minimal_log) printf("\n\n\n\n\n"); // Space for dashboard
|
||||
|
||||
while (is_running) {
|
||||
libusb_handle_events_timeout_completed(NULL, &(struct timeval){0, 100000}, NULL);
|
||||
}
|
||||
|
||||
cleanup:
|
||||
is_running = false;
|
||||
if (logging_thread) pthread_join(logging_thread, NULL);
|
||||
for (int i = 0; i < NUM_PLAYBACK_TRANSFERS; i++) if (playback_transfers[i]) libusb_cancel_transfer(playback_transfers[i]);
|
||||
for (int i = 0; i < NUM_FEEDBACK_TRANSFERS; i++) if (feedback_transfers[i]) libusb_cancel_transfer(feedback_transfers[i]);
|
||||
if (handle) {
|
||||
struct timeval tv = {0, 100000};
|
||||
libusb_handle_events_timeout_completed(NULL, &tv, NULL);
|
||||
libusb_release_interface(handle, 1);
|
||||
libusb_release_interface(handle, 0);
|
||||
for(int i = 0; i < 2; i++) if (kernel_driver_was_active[i]) libusb_attach_kernel_driver(handle, i);
|
||||
libusb_close(handle);
|
||||
}
|
||||
for (int i = 0; i < NUM_PLAYBACK_TRANSFERS; i++) if (playback_transfers[i]) { if (playback_transfers[i]->buffer) free(playback_transfers[i]->buffer); libusb_free_transfer(playback_transfers[i]); }
|
||||
for (int i = 0; i < NUM_FEEDBACK_TRANSFERS; i++) if (feedback_transfers[i]) { if (feedback_transfers[i]->buffer) free(feedback_transfers[i]->buffer); libusb_free_transfer(feedback_transfers[i]); }
|
||||
if (state.fifo_fd >= 0) close(state.fifo_fd);
|
||||
pthread_mutex_destroy(&state.lock);
|
||||
if (r != 1) libusb_exit(NULL);
|
||||
printf("Cleanup complete.\n");
|
||||
return r;
|
||||
}
|
||||
|
||||
void print_usage(const char *prog_name) {
|
||||
fprintf(stderr, "Usage: %s -r <rate> -p <profile> --pipe <path> [options]\n", prog_name);
|
||||
fprintf(stderr, "Required:\n");
|
||||
fprintf(stderr, " -r <rate> : 44100, 48000, 88200, 96000\n");
|
||||
fprintf(stderr, " -p <profile> : 0-4 (Lowest, Low, Normal, High, Highest)\n");
|
||||
fprintf(stderr, " --pipe <path> : Path to the named pipe for audio input\n");
|
||||
fprintf(stderr, "Optional:\n");
|
||||
fprintf(stderr, " --minimal-log : Switch to a simple, single-line status summary.\n");
|
||||
fprintf(stderr, " --log-interval <ms>: Set summary update frequency (default: 100ms).\n");
|
||||
}
|
||||
|
||||
double timeval_diff_ms(struct timeval *start, struct timeval *end) {
|
||||
return (end->tv_sec - start->tv_sec) * 1000.0 + (end->tv_usec - start->tv_usec) / 1000.0;
|
||||
}
|
||||
|
||||
void *logging_thread_func(void *arg) {
|
||||
struct logging_thread_args *args = (struct logging_thread_args *)arg;
|
||||
struct stream_state *state = args->state;
|
||||
const int bar_width = 20;
|
||||
|
||||
while (is_running) {
|
||||
usleep(args->log_interval_ms * 1000);
|
||||
pthread_mutex_lock(&state->lock);
|
||||
|
||||
const char *health = (state->underrun_count > 0 || state->overrun_count > 0) ? "\033[1;31mUNSTABLE\033[0m" : "\033[1;32mSTABLE\033[0m";
|
||||
const char *sync_status_str;
|
||||
if (state->feedback_synced) {
|
||||
sync_status_str = state->feedback_warmed_up ? "\033[1;32mACQUIRED\033[0m" : "\033[1;33mWARM-UP\033[0m";
|
||||
} else {
|
||||
sync_status_str = "\033[1;31mLOST/OFF\033[0m";
|
||||
}
|
||||
|
||||
double avg_interval = (state->feedback_interval_count > 0) ? state->avg_feedback_interval_sum / state->feedback_interval_count : 0.0;
|
||||
|
||||
if (args->minimal_log) {
|
||||
printf("Health: %s, Sync: %s, Avg Interval: %.2fms, Underruns: %lu, Overruns: %lu \r",
|
||||
(state->underrun_count > 0 || state->overrun_count > 0) ? "UNSTABLE" : "STABLE",
|
||||
state->feedback_warmed_up ? "ACQUIRED" : "WARMING",
|
||||
avg_interval, state->underrun_count, state->overrun_count);
|
||||
} else {
|
||||
size_t fill = (state->feedback_pattern_in_idx - state->feedback_pattern_out_idx + FEEDBACK_ACCUMULATOR_SIZE) % FEEDBACK_ACCUMULATOR_SIZE;
|
||||
int filled_chars = (int)((double)fill / FEEDBACK_ACCUMULATOR_SIZE * bar_width);
|
||||
|
||||
printf("\033[5A\033[K\n\033[K\n\033[K\n\033[K\n\033[K\n\033[5A");
|
||||
printf("--- TASCAM US-144MKII Stream Health ---\n");
|
||||
printf(" Health: %-18s Sync: %-18s Feedback: %-3d\n", health, sync_status_str, state->last_feedback_value);
|
||||
printf(" Buffer: [");
|
||||
for(int i=0; i<bar_width; ++i) putchar(i < filled_chars ? '#' : '-');
|
||||
printf("] %3zu/%d\n", fill, FEEDBACK_ACCUMULATOR_SIZE);
|
||||
printf(" Interval (ms) -> Now: %4.2f Min: %4.2f Avg: %4.2f Max: %4.2f\n",
|
||||
state->last_feedback_interval_ms,
|
||||
state->min_feedback_interval_ms == DBL_MAX ? 0.0 : state->min_feedback_interval_ms,
|
||||
avg_interval, state->max_feedback_interval_ms);
|
||||
printf(" Errors -> Underruns: %-5lu Overruns: %lu\n", state->underrun_count, state->overrun_count);
|
||||
}
|
||||
fflush(stdout);
|
||||
pthread_mutex_unlock(&state->lock);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static void LIBUSB_CALL feedback_callback(struct libusb_transfer *transfer) {
|
||||
if (!is_running) return;
|
||||
struct stream_state *state = transfer->user_data;
|
||||
struct timeval now;
|
||||
gettimeofday(&now, NULL);
|
||||
|
||||
if (transfer->status != LIBUSB_TRANSFER_COMPLETED) {
|
||||
if (transfer->status != LIBUSB_TRANSFER_CANCELLED) {
|
||||
pthread_mutex_lock(&state->lock);
|
||||
if (state->feedback_synced) printf("\nSync Lost (URB Error: %s)!\n", libusb_error_name(transfer->status));
|
||||
state->feedback_synced = false;
|
||||
state->feedback_warmed_up = false;
|
||||
pthread_mutex_unlock(&state->lock);
|
||||
}
|
||||
goto resubmit;
|
||||
}
|
||||
|
||||
pthread_mutex_lock(&state->lock);
|
||||
if (state->last_feedback_completion_time.tv_sec > 0) {
|
||||
state->last_feedback_interval_ms = timeval_diff_ms(&state->last_feedback_completion_time, &now);
|
||||
if (state->feedback_warmed_up) {
|
||||
if (state->last_feedback_interval_ms < state->min_feedback_interval_ms) state->min_feedback_interval_ms = state->last_feedback_interval_ms;
|
||||
if (state->last_feedback_interval_ms > state->max_feedback_interval_ms) state->max_feedback_interval_ms = state->last_feedback_interval_ms;
|
||||
state->avg_feedback_interval_sum += state->last_feedback_interval_ms;
|
||||
state->feedback_interval_count++;
|
||||
}
|
||||
}
|
||||
state->last_feedback_completion_time = now;
|
||||
|
||||
bool was_synced = state->feedback_synced;
|
||||
bool sync_lost_this_urb = false;
|
||||
|
||||
for (int p = 0; p < transfer->num_iso_packets; p++) {
|
||||
struct libusb_iso_packet_descriptor *pack = &transfer->iso_packet_desc[p];
|
||||
if (pack->status != 0 || pack->actual_length < 1) {
|
||||
sync_lost_this_urb = true;
|
||||
continue;
|
||||
}
|
||||
size_t packet_offset = p * FEEDBACK_PACKET_SIZE;
|
||||
uint8_t feedback_value = transfer->buffer[packet_offset];
|
||||
state->last_feedback_value = feedback_value;
|
||||
|
||||
if (feedback_value >= state->rate_cfg->feedback_base_value && feedback_value <= state->rate_cfg->feedback_max_value) {
|
||||
int pattern_index = feedback_value - state->rate_cfg->feedback_base_value;
|
||||
const unsigned int *pattern = state->rate_cfg->feedback_patterns[pattern_index];
|
||||
size_t fill_level = (state->feedback_pattern_in_idx - state->feedback_pattern_out_idx + FEEDBACK_ACCUMULATOR_SIZE) % FEEDBACK_ACCUMULATOR_SIZE;
|
||||
if (fill_level > (FEEDBACK_ACCUMULATOR_SIZE - 16)) state->overrun_count++;
|
||||
for (int i = 0; i < 8; i++) {
|
||||
unsigned int in_idx = (state->feedback_pattern_in_idx + i) % FEEDBACK_ACCUMULATOR_SIZE;
|
||||
state->feedback_accumulator_pattern[in_idx] = pattern[i];
|
||||
}
|
||||
state->feedback_pattern_in_idx = (state->feedback_pattern_in_idx + 8) % FEEDBACK_ACCUMULATOR_SIZE;
|
||||
} else {
|
||||
sync_lost_this_urb = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (sync_lost_this_urb) {
|
||||
if (was_synced) printf("\nSync Lost (Bad Packet)!\n");
|
||||
state->feedback_synced = false;
|
||||
state->feedback_warmed_up = false;
|
||||
} else {
|
||||
if (!was_synced) printf("\nSync Acquired!\n");
|
||||
state->feedback_synced = true;
|
||||
size_t fill_level = (state->feedback_pattern_in_idx - state->feedback_pattern_out_idx + FEEDBACK_ACCUMULATOR_SIZE) % FEEDBACK_ACCUMULATOR_SIZE;
|
||||
if (!state->feedback_warmed_up && fill_level >= WARMUP_THRESHOLD) {
|
||||
state->feedback_warmed_up = true;
|
||||
state->min_feedback_interval_ms = DBL_MAX;
|
||||
state->max_feedback_interval_ms = 0.0;
|
||||
state->avg_feedback_interval_sum = 0.0;
|
||||
state->feedback_interval_count = 0;
|
||||
printf("\nBuffer warmed up. Measuring steady-state performance.\n");
|
||||
}
|
||||
}
|
||||
pthread_mutex_unlock(&state->lock);
|
||||
|
||||
resubmit:
|
||||
if (is_running) libusb_submit_transfer(transfer);
|
||||
}
|
||||
|
||||
static void LIBUSB_CALL iso_playback_callback(struct libusb_transfer *transfer) {
|
||||
if (!is_running) return;
|
||||
struct stream_state *state = transfer->user_data;
|
||||
|
||||
if (transfer->status != LIBUSB_TRANSFER_COMPLETED) {
|
||||
if (transfer->status != LIBUSB_TRANSFER_CANCELLED) {
|
||||
fprintf(stderr, "\nPlayback callback error: %s\n", libusb_error_name(transfer->status));
|
||||
is_running = false;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
pthread_mutex_lock(&state->lock);
|
||||
int nominal_frames = state->rate_cfg->rate / 8000;
|
||||
|
||||
if (!state->feedback_warmed_up) {
|
||||
libusb_set_iso_packet_lengths(transfer, nominal_frames * DEVICE_FRAME_SIZE);
|
||||
memset(transfer->buffer, 0, transfer->length);
|
||||
pthread_mutex_unlock(&state->lock);
|
||||
goto resubmit_playback;
|
||||
}
|
||||
|
||||
unsigned char *buf_ptr = transfer->buffer;
|
||||
size_t total_bytes_in_urb = 0;
|
||||
|
||||
for (int i = 0; i < transfer->num_iso_packets; i++) {
|
||||
unsigned int frames_for_packet;
|
||||
if (state->feedback_pattern_out_idx == state->feedback_pattern_in_idx) {
|
||||
state->underrun_count++;
|
||||
frames_for_packet = nominal_frames;
|
||||
} else {
|
||||
frames_for_packet = state->feedback_accumulator_pattern[state->feedback_pattern_out_idx];
|
||||
state->feedback_pattern_out_idx = (state->feedback_pattern_out_idx + 1) % FEEDBACK_ACCUMULATOR_SIZE;
|
||||
}
|
||||
size_t bytes_for_packet = frames_for_packet * DEVICE_FRAME_SIZE;
|
||||
size_t bytes_to_read_from_pipe = frames_for_packet * PIPE_FRAME_SIZE;
|
||||
|
||||
ssize_t bytes_read = read(state->fifo_fd, buf_ptr, bytes_to_read_from_pipe);
|
||||
|
||||
if (bytes_read > 0) {
|
||||
int frames_read = bytes_read / PIPE_FRAME_SIZE;
|
||||
for (int f = frames_read - 1; f >= 0; f--) {
|
||||
unsigned char* src = buf_ptr + f * PIPE_FRAME_SIZE;
|
||||
unsigned char* dst = buf_ptr + f * DEVICE_FRAME_SIZE;
|
||||
memmove(dst, src, PIPE_FRAME_SIZE);
|
||||
memset(dst + PIPE_FRAME_SIZE, 0, DEVICE_FRAME_SIZE - PIPE_FRAME_SIZE);
|
||||
}
|
||||
if ((size_t)bytes_read < bytes_to_read_from_pipe) {
|
||||
memset(buf_ptr + (frames_read * DEVICE_FRAME_SIZE), 0, bytes_for_packet - (frames_read * DEVICE_FRAME_SIZE));
|
||||
}
|
||||
} else {
|
||||
memset(buf_ptr, 0, bytes_for_packet);
|
||||
}
|
||||
|
||||
buf_ptr += bytes_for_packet;
|
||||
transfer->iso_packet_desc[i].length = bytes_for_packet;
|
||||
total_bytes_in_urb += bytes_for_packet;
|
||||
}
|
||||
pthread_mutex_unlock(&state->lock);
|
||||
|
||||
transfer->length = total_bytes_in_urb;
|
||||
|
||||
resubmit_playback:
|
||||
if (is_running && libusb_submit_transfer(transfer) < 0) {
|
||||
fprintf(stderr, "\nError resubmitting playback transfer\n");
|
||||
is_running = false;
|
||||
}
|
||||
}
|
||||
|
||||
int perform_initialization_sequence(libusb_device_handle *handle, const struct sample_rate_config *rate_config) {
|
||||
unsigned char buf[64]; int r;
|
||||
printf("\n--- STARTING DEVICE CONFIGURATION (per Spec v5.0) ---\n");
|
||||
#define CHECK(desc, call) r = (call); if (r < 0) { fprintf(stderr, " [FAIL] %s: %s\n", desc, libusb_error_name(r)); return -1; } else { printf(" [OK] %s (returned %d)\n", desc, r); }
|
||||
printf(" [INFO] Step 1: Set Interfaces\n");
|
||||
r = libusb_set_configuration(handle, 1); if (r < 0 && r != LIBUSB_ERROR_BUSY) { fprintf(stderr, " [FAIL] Set Configuration 1: %s\n", libusb_error_name(r)); return -1; }
|
||||
for (int i=0; i<=1; i++) { r = libusb_claim_interface(handle, i); if (r < 0) { fprintf(stderr, " [FAIL] Claim Interface %d: %s\n", i, libusb_error_name(r)); return -1; } r = libusb_set_interface_alt_setting(handle, i, 1); if (r < 0) { fprintf(stderr, " [FAIL] Set Alt Setting on Intf %d: %s\n", i, libusb_error_name(r)); return -1; } }
|
||||
printf(" [OK] Step 1: Interfaces set and claimed.\n");
|
||||
printf("\n-- Step 2: Initial Handshake --\n"); CHECK("Status Check", libusb_control_transfer(handle, RT_D2H_VENDOR_DEV, VENDOR_REQ_MODE_CONTROL, 0x0000, 0x0000, buf, 1, USB_TIMEOUT));
|
||||
printf("\n-- Step 3: Set Initial Mode --\n"); CHECK("Set Initial Mode", libusb_control_transfer(handle, RT_H2D_VENDOR_DEV, VENDOR_REQ_MODE_CONTROL, 0x0010, 0x0000, NULL, 0, USB_TIMEOUT));
|
||||
printf("\n-- Step 4: Set Sample Rate to %d Hz --\n", rate_config->rate);
|
||||
CHECK("Set Rate on Capture EP (0x86)", libusb_control_transfer(handle, RT_H2D_CLASS_EP, UAC_SET_CUR, UAC_SAMPLING_FREQ_CONTROL, EP_CAPTURE_DATA, (unsigned char*)rate_config->rate_data, 3, USB_TIMEOUT));
|
||||
CHECK("Set Rate on Playback EP (0x02)", libusb_control_transfer(handle, RT_H2D_CLASS_EP, UAC_SET_CUR, UAC_SAMPLING_FREQ_CONTROL, EP_AUDIO_OUT, (unsigned char*)rate_config->rate_data, 3, USB_TIMEOUT));
|
||||
printf("\n-- Step 5: Configure Internal Registers --\n"); CHECK("Reg Write 1 (0x0d04)", libusb_control_transfer(handle, RT_H2D_VENDOR_DEV, VENDOR_REQ_REGISTER_WRITE, 0x0d04, 0x0101, NULL, 0, USB_TIMEOUT)); CHECK("Reg Write 2 (0x0e00)", libusb_control_transfer(handle, RT_H2D_VENDOR_DEV, VENDOR_REQ_REGISTER_WRITE, 0x0e00, 0x0101, NULL, 0, USB_TIMEOUT)); CHECK("Reg Write 3 (0x0f00)", libusb_control_transfer(handle, RT_H2D_VENDOR_DEV, VENDOR_REQ_REGISTER_WRITE, 0x0f00, 0x0101, NULL, 0, USB_TIMEOUT));
|
||||
CHECK("Reg Write 4 (Rate-Dep)", libusb_control_transfer(handle, RT_H2D_VENDOR_DEV, VENDOR_REQ_REGISTER_WRITE, rate_config->rate_vendor_wValue, 0x0101, NULL, 0, USB_TIMEOUT));
|
||||
CHECK("Reg Write 5 (0x110b)", libusb_control_transfer(handle, RT_H2D_VENDOR_DEV, VENDOR_REQ_REGISTER_WRITE, 0x110b, 0x0101, NULL, 0, USB_TIMEOUT));
|
||||
printf("\n-- Step 6: Enable Streaming --\n"); CHECK("Enable Streaming", libusb_control_transfer(handle, RT_H2D_VENDOR_DEV, VENDOR_REQ_MODE_CONTROL, 0x0030, 0x0000, NULL, 0, USB_TIMEOUT));
|
||||
printf("\n--- CONFIGURATION COMPLETE ---\n\n"); return 0;
|
||||
}
|
||||
Binary file not shown.
278
us144mkii.c
278
us144mkii.c
|
|
@ -1,5 +1,8 @@
|
|||
// SPDX-License-Identifier: GPL-2.0
|
||||
// (c) 2025 serifpersia <ramiserifpersia@gmail.com>
|
||||
/*
|
||||
* ALSA Driver for TASCAM US-144MKII Audio Interface
|
||||
*/
|
||||
|
||||
#include <linux/kernel.h>
|
||||
#include <linux/module.h>
|
||||
|
|
@ -14,7 +17,7 @@
|
|||
#include <sound/initval.h>
|
||||
|
||||
MODULE_AUTHOR("serifpersia");
|
||||
MODULE_DESCRIPTION("ALSA Driver for TASCAM US-144MKII with Isochronous Feedback");
|
||||
MODULE_DESCRIPTION("ALSA Driver for TASCAM US-144MKII");
|
||||
MODULE_LICENSE("GPL");
|
||||
|
||||
#define DRIVER_NAME "us144mkii"
|
||||
|
|
@ -23,21 +26,6 @@ MODULE_LICENSE("GPL");
|
|||
/* --- Module Parameters --- */
|
||||
/*============================================================================*/
|
||||
|
||||
/*
|
||||
* Latency Profile Cheatsheet (Updated based on Windows ASIO driver behavior)
|
||||
*
|
||||
* The driver selects a hardware profile dynamically based on the period size
|
||||
* requested by the application. The device has 3 true hardware modes.
|
||||
* The thresholds are ~2ms and ~3ms.
|
||||
*
|
||||
* Profile | Feedback URB | Approx. Latency | Typical Period Size (Frames)
|
||||
* Name | Packet Count | (Hardware) | 48kHz | 96kHz
|
||||
* --------|--------------|-----------------|------------|------------
|
||||
* Low | 1 packet | <= 2ms | <= 96 | <= 192
|
||||
* Normal | 2 packets | > 2ms to <= 3ms | <= 144 | <= 288
|
||||
* High | 5 packets | > 3ms | > 144 | > 288
|
||||
*/
|
||||
|
||||
static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
|
||||
static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
|
||||
static bool enable[SNDRV_CARDS] = {1, [1 ... (SNDRV_CARDS - 1)] = 0};
|
||||
|
|
@ -57,35 +45,45 @@ MODULE_PARM_DESC(enable, "Enable this US-144MKII soundcard.");
|
|||
#define TASCAM_VID 0x0644
|
||||
#define TASCAM_PID 0x8020
|
||||
|
||||
#define EP_AUDIO_OUT 0x02
|
||||
#define EP_PLAYBACK_FEEDBACK 0x81
|
||||
#define EP_CAPTURE_DATA 0x86
|
||||
/* USB Endpoints from descriptor report */
|
||||
#define EP_AUDIO_OUT 0x02 // Isochronous OUT for playback audio
|
||||
#define EP_PLAYBACK_FEEDBACK 0x81 // Isochronous IN for clock feedback
|
||||
#define EP_CAPTURE_DATA 0x86 // Bulk IN for capture audio/MIDI
|
||||
#define EP_MIDI_OUT 0x04 // Bulk OUT for MIDI
|
||||
#define EP_MIDI_IN 0x83 // Bulk IN for MIDI
|
||||
|
||||
#define RT_H2D_CLASS_EP 0x22
|
||||
#define RT_H2D_VENDOR_DEV 0x40
|
||||
#define RT_D2H_VENDOR_DEV 0xc0
|
||||
/* USB Control Message Request Types */
|
||||
#define RT_H2D_CLASS_EP 0x22 // Host-to-Device, Class, Endpoint
|
||||
#define RT_H2D_VENDOR_DEV 0x40 // Host-to-Device, Vendor, Device
|
||||
#define RT_D2H_VENDOR_DEV 0xc0 // Device-to-Host, Vendor, Device
|
||||
|
||||
/* USB Control Message Requests */
|
||||
#define UAC_SET_CUR 0x01
|
||||
#define UAC_SAMPLING_FREQ_CONTROL 0x0100
|
||||
#define VENDOR_REQ_REGISTER_WRITE 65
|
||||
#define VENDOR_REQ_MODE_CONTROL 73
|
||||
#define VENDOR_REQ_REGISTER_WRITE 65 // bRequest 0x41
|
||||
#define VENDOR_REQ_MODE_CONTROL 73 // bRequest 0x49
|
||||
|
||||
/* URB Configuration */
|
||||
#define NUM_PLAYBACK_URBS 8
|
||||
#define NUM_FEEDBACK_URBS 4
|
||||
#define MAX_FEEDBACK_PACKETS 5
|
||||
#define MAX_FEEDBACK_PACKETS 5 // For the highest latency setting
|
||||
#define PLAYBACK_URB_ISO_PACKETS 40
|
||||
#define FEEDBACK_PACKET_SIZE 3
|
||||
#define USB_CTRL_TIMEOUT_MS 1000
|
||||
|
||||
#define BYTES_PER_SAMPLE 3
|
||||
#define ALSA_CHANNELS 2
|
||||
#define DEVICE_CHANNELS 4
|
||||
/* Audio Format Configuration */
|
||||
#define BYTES_PER_SAMPLE 3 // 24-bit
|
||||
#define ALSA_CHANNELS 2 // Stereo from user-space
|
||||
#define DEVICE_CHANNELS 4 // Device expects 4 channels of data
|
||||
#define ALSA_BYTES_PER_FRAME (ALSA_CHANNELS * BYTES_PER_SAMPLE)
|
||||
#define DEVICE_BYTES_PER_FRAME (DEVICE_CHANNELS * BYTES_PER_SAMPLE)
|
||||
|
||||
/* Feedback Synchronization Engine Configuration */
|
||||
#define FEEDBACK_ACCUMULATOR_SIZE 128
|
||||
|
||||
static struct usb_driver tascam_alsa_driver;
|
||||
|
||||
/* Main driver data structure */
|
||||
struct tascam_card {
|
||||
struct usb_device *dev;
|
||||
struct usb_interface *iface0;
|
||||
|
|
@ -106,43 +104,78 @@ struct tascam_card {
|
|||
|
||||
/* --- Feedback Synchronization State --- */
|
||||
unsigned int feedback_accumulator_pattern[FEEDBACK_ACCUMULATOR_SIZE];
|
||||
unsigned int feedback_pattern_out_idx;
|
||||
unsigned int feedback_pattern_in_idx;
|
||||
unsigned int feedback_pattern_out_idx; // Read index for playback
|
||||
unsigned int feedback_pattern_in_idx; // Write index from feedback
|
||||
bool feedback_synced;
|
||||
unsigned int feedback_urb_skip_count; // Initial URBs to discard
|
||||
|
||||
/* --- Playback Position Tracking --- */
|
||||
snd_pcm_uframes_t driver_playback_pos;
|
||||
u64 playback_frames_consumed;
|
||||
u64 last_period_pos;
|
||||
snd_pcm_uframes_t driver_playback_pos; // Pointer within ALSA buffer
|
||||
u64 playback_frames_consumed; // Total frames consumed by hw
|
||||
u64 last_period_pos; // Last reported period
|
||||
|
||||
/* --- Rate-Specific Data --- */
|
||||
const unsigned int (*feedback_patterns)[8];
|
||||
unsigned int feedback_base_value;
|
||||
unsigned int feedback_max_value;
|
||||
unsigned int feedback_urb_skip_count;
|
||||
};
|
||||
|
||||
/* Pre-calculated patterns for frames-per-microframe based on feedback value. */
|
||||
static const unsigned int latency_profile_packets[] = { 5, 1, 2, 5, 5 };
|
||||
/*
|
||||
* Latency Profile Cheatsheet (from reverse-engineering report)
|
||||
*
|
||||
* The driver selects a hardware profile dynamically based on the period size
|
||||
* requested by the application. The device has 3 true hardware modes,
|
||||
* determined by the number of packets in the feedback URB.
|
||||
*
|
||||
* Profile | Feedback URB | Approx. Latency | Typical Period Size (Frames)
|
||||
* Name | Packet Count | (Hardware) | 48kHz | 96kHz
|
||||
* --------|--------------|-----------------|------------|------------
|
||||
* Low | 1 packet | <= 2ms | <= 96 | <= 192
|
||||
* Normal | 2 packets | > 2ms to <= 3ms | <= 144 | <= 288
|
||||
* High | 5 packets | > 3ms | > 144 | > 288
|
||||
*/
|
||||
static const unsigned int latency_profile_packets[] = {
|
||||
0, // Profile 0 unused
|
||||
1, // Low latency
|
||||
2, // Normal latency
|
||||
5, // High latency
|
||||
};
|
||||
|
||||
/*
|
||||
* Pre-calculated patterns for frames-per-microframe based on feedback value.
|
||||
* These are the core of the "Packet Fixing" engine. Each array represents
|
||||
* the number of audio frames to send in each of the 8 microframes of a USB frame.
|
||||
* The sum of each pattern equals the feedback value.
|
||||
* E.g., for 48kHz, nominal is 48000/1000 = 48 frames/ms.
|
||||
* The pattern for feedback value 48 is {6,6,6,6,6,6,6,6}, since 6*8=48.
|
||||
*/
|
||||
static const unsigned int patterns_48khz[5][8] = {
|
||||
{5, 6, 6, 6, 5, 6, 6, 6}, {5, 6, 6, 6, 6, 6, 6, 6},
|
||||
{6, 6, 6, 6, 6, 6, 6, 6}, {7, 6, 6, 6, 6, 6, 6, 6},
|
||||
{7, 6, 6, 6, 7, 6, 6, 6}
|
||||
{5, 6, 6, 6, 5, 6, 6, 6}, // 46
|
||||
{5, 6, 6, 6, 6, 6, 6, 6}, // 47
|
||||
{6, 6, 6, 6, 6, 6, 6, 6}, // 48 (Nominal)
|
||||
{7, 6, 6, 6, 6, 6, 6, 6}, // 49
|
||||
{7, 6, 6, 6, 7, 6, 6, 6} // 50
|
||||
};
|
||||
static const unsigned int patterns_96khz[5][8] = {
|
||||
{11, 12, 12, 12, 11, 12, 12, 12}, {11, 12, 12, 12, 12, 12, 12, 12},
|
||||
{12, 12, 12, 12, 12, 12, 12, 12}, {13, 12, 12, 12, 12, 12, 12, 12},
|
||||
{13, 12, 12, 12, 13, 12, 12, 12}
|
||||
{11, 12, 12, 12, 11, 12, 12, 12}, // 94
|
||||
{11, 12, 12, 12, 12, 12, 12, 12}, // 95
|
||||
{12, 12, 12, 12, 12, 12, 12, 12}, // 96 (Nominal)
|
||||
{13, 12, 12, 12, 12, 12, 12, 12}, // 97
|
||||
{13, 12, 12, 12, 13, 12, 12, 12} // 98
|
||||
};
|
||||
static const unsigned int patterns_88khz[5][8] = {
|
||||
{10, 11, 11, 11, 10, 11, 11, 11}, {10, 11, 11, 11, 11, 11, 11, 11},
|
||||
{11, 11, 11, 11, 11, 11, 11, 11}, {12, 11, 11, 11, 11, 11, 11, 11},
|
||||
{12, 11, 11, 11, 12, 11, 11, 11}
|
||||
{10, 11, 11, 11, 10, 11, 11, 11}, // 86
|
||||
{10, 11, 11, 11, 11, 11, 11, 11}, // 87
|
||||
{11, 11, 11, 11, 11, 11, 11, 11}, // 88 (Nominal)
|
||||
{12, 11, 11, 11, 11, 11, 11, 11}, // 89
|
||||
{12, 11, 11, 11, 12, 11, 11, 11} // 90
|
||||
};
|
||||
static const unsigned int patterns_44khz[5][8] = {
|
||||
{5, 5, 5, 6, 5, 5, 5, 6}, {5, 5, 6, 5, 5, 6, 5, 6},
|
||||
{5, 6, 5, 6, 5, 6, 5, 6}, {6, 5, 6, 6, 5, 6, 5, 6},
|
||||
{6, 6, 6, 5, 6, 6, 6, 5}
|
||||
{5, 5, 5, 6, 5, 5, 5, 6}, // 42
|
||||
{5, 5, 6, 5, 5, 6, 5, 6}, // 43
|
||||
{5, 6, 5, 6, 5, 6, 5, 6}, // 44 (Nominal is 44.1)
|
||||
{6, 5, 6, 6, 5, 6, 5, 6}, // 45
|
||||
{6, 6, 6, 5, 6, 6, 6, 5} // 46
|
||||
};
|
||||
|
||||
|
||||
|
|
@ -208,6 +241,7 @@ static struct snd_pcm_ops tascam_playback_ops = {
|
|||
.pointer = tascam_pcm_pointer,
|
||||
};
|
||||
|
||||
// Stub for capture, as this driver only implements playback.
|
||||
static int tascam_capture_open_stub(struct snd_pcm_substream *substream) { return -ENODEV; }
|
||||
static int tascam_capture_close_stub(struct snd_pcm_substream *substream) { return 0; }
|
||||
static struct snd_pcm_ops tascam_capture_ops = {
|
||||
|
|
@ -254,6 +288,8 @@ static int tascam_alloc_urbs(struct tascam_card *tascam)
|
|||
int i;
|
||||
size_t max_frames_per_packet, max_packet_size;
|
||||
|
||||
// Calculate max possible packet size to allocate enough buffer space.
|
||||
// Add a margin of 2 for safety.
|
||||
max_frames_per_packet = (96000 / 8000) + 2;
|
||||
max_packet_size = max_frames_per_packet * DEVICE_BYTES_PER_FRAME;
|
||||
tascam->playback_urb_alloc_size = max_packet_size * PLAYBACK_URB_ISO_PACKETS;
|
||||
|
|
@ -265,17 +301,19 @@ static int tascam_alloc_urbs(struct tascam_card *tascam)
|
|||
|
||||
for (i = 0; i < NUM_PLAYBACK_URBS; i++) {
|
||||
struct urb *urb = usb_alloc_urb(PLAYBACK_URB_ISO_PACKETS, GFP_KERNEL);
|
||||
if (!urb) goto error;
|
||||
if (!urb)
|
||||
goto error;
|
||||
tascam->playback_urbs[i] = urb;
|
||||
|
||||
urb->transfer_buffer = usb_alloc_coherent(tascam->dev, tascam->playback_urb_alloc_size,
|
||||
GFP_KERNEL, &urb->transfer_dma);
|
||||
if (!urb->transfer_buffer) goto error;
|
||||
if (!urb->transfer_buffer)
|
||||
goto error;
|
||||
|
||||
urb->dev = tascam->dev;
|
||||
urb->pipe = usb_sndisocpipe(tascam->dev, EP_AUDIO_OUT);
|
||||
urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
|
||||
urb->interval = 1;
|
||||
urb->interval = 1; // bInterval from descriptor
|
||||
urb->context = tascam;
|
||||
urb->complete = playback_urb_complete;
|
||||
urb->number_of_packets = PLAYBACK_URB_ISO_PACKETS;
|
||||
|
|
@ -285,17 +323,19 @@ static int tascam_alloc_urbs(struct tascam_card *tascam)
|
|||
|
||||
for (i = 0; i < NUM_FEEDBACK_URBS; i++) {
|
||||
struct urb *f_urb = usb_alloc_urb(MAX_FEEDBACK_PACKETS, GFP_KERNEL);
|
||||
if (!f_urb) goto error;
|
||||
if (!f_urb)
|
||||
goto error;
|
||||
tascam->feedback_urbs[i] = f_urb;
|
||||
|
||||
f_urb->transfer_buffer = usb_alloc_coherent(tascam->dev, tascam->feedback_urb_alloc_size,
|
||||
GFP_KERNEL, &f_urb->transfer_dma);
|
||||
if (!f_urb->transfer_buffer) goto error;
|
||||
if (!f_urb->transfer_buffer)
|
||||
goto error;
|
||||
|
||||
f_urb->dev = tascam->dev;
|
||||
f_urb->pipe = usb_rcvisocpipe(tascam->dev, EP_PLAYBACK_FEEDBACK);
|
||||
f_urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
|
||||
f_urb->interval = 4;
|
||||
f_urb->interval = 4; // bInterval from descriptor
|
||||
f_urb->context = tascam;
|
||||
f_urb->complete = feedback_urb_complete;
|
||||
}
|
||||
|
|
@ -313,6 +353,9 @@ error:
|
|||
/* --- PCM Implementation --- */
|
||||
/*============================================================================*/
|
||||
|
||||
// This rule constrains the period size to the values reported by the
|
||||
// Windows ASIO driver, ensuring we don't request a latency the
|
||||
// hardware can't handle.
|
||||
static int tascam_pcm_period_size_rule(struct snd_pcm_hw_params *params,
|
||||
struct snd_pcm_hw_rule *rule)
|
||||
{
|
||||
|
|
@ -372,6 +415,7 @@ static int tascam_pcm_open(struct snd_pcm_substream *substream)
|
|||
static int tascam_pcm_close(struct snd_pcm_substream *substream)
|
||||
{
|
||||
struct tascam_card *tascam = snd_pcm_substream_chip(substream);
|
||||
|
||||
tascam_free_urbs(tascam);
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -388,12 +432,13 @@ static int tascam_pcm_hw_params(struct snd_pcm_substream *substream,
|
|||
int active_latency_profile;
|
||||
unsigned int feedback_urb_packets;
|
||||
|
||||
// Select a hardware latency profile based on the user's requested period size.
|
||||
if (period_frames <= (normal_thresh_ms * rate / 1000)) {
|
||||
active_latency_profile = 1; // 1-packet mode
|
||||
active_latency_profile = 1; // Low latency (1 feedback packet)
|
||||
} else if (period_frames <= (high_thresh_ms * rate / 1000)) {
|
||||
active_latency_profile = 2; // 2-packet mode
|
||||
active_latency_profile = 2; // Normal latency (2 feedback packets)
|
||||
} else {
|
||||
active_latency_profile = 3; // 5-packet mode
|
||||
active_latency_profile = 3; // High latency (5 feedback packets)
|
||||
}
|
||||
|
||||
dev_info(tascam->card->dev,
|
||||
|
|
@ -402,9 +447,11 @@ static int tascam_pcm_hw_params(struct snd_pcm_substream *substream,
|
|||
|
||||
feedback_urb_packets = latency_profile_packets[active_latency_profile];
|
||||
|
||||
// Configure the feedback URBs for the selected latency profile.
|
||||
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++) {
|
||||
|
|
@ -417,6 +464,7 @@ static int tascam_pcm_hw_params(struct snd_pcm_substream *substream,
|
|||
if (err < 0)
|
||||
return err;
|
||||
|
||||
// Load the correct feedback patterns and range for the selected sample rate.
|
||||
switch (rate) {
|
||||
case 44100:
|
||||
tascam->feedback_patterns = patterns_44khz;
|
||||
|
|
@ -443,6 +491,7 @@ static int tascam_pcm_hw_params(struct snd_pcm_substream *substream,
|
|||
return -EINVAL;
|
||||
}
|
||||
|
||||
// If the sample rate has changed, reconfigure the device.
|
||||
if (tascam->current_rate != rate) {
|
||||
err = us144mkii_configure_device_for_rate(tascam, rate);
|
||||
if (err < 0) {
|
||||
|
|
@ -476,15 +525,17 @@ static int tascam_pcm_prepare(struct snd_pcm_substream *substream)
|
|||
tascam->feedback_pattern_in_idx = 0;
|
||||
tascam->feedback_pattern_out_idx = 0;
|
||||
tascam->feedback_synced = false;
|
||||
// Discard the first few feedback URBs to allow the hardware clock to stabilize.
|
||||
tascam->feedback_urb_skip_count = NUM_FEEDBACK_URBS * 2;
|
||||
|
||||
/* DEBUG: Log the initial state on prepare. */
|
||||
dev_info(tascam->card->dev, "Prepare: Sync state reset, starting in unsynced mode.\n");
|
||||
|
||||
// Initialize the feedback accumulator with the nominal number of frames.
|
||||
nominal_frames_per_packet = runtime->rate / 8000;
|
||||
for (i = 0; i < FEEDBACK_ACCUMULATOR_SIZE; i++)
|
||||
tascam->feedback_accumulator_pattern[i] = nominal_frames_per_packet;
|
||||
|
||||
// Initialize playback URBs with nominal packet sizes.
|
||||
nominal_bytes_per_packet = nominal_frames_per_packet * DEVICE_BYTES_PER_FRAME;
|
||||
total_bytes_in_urb = nominal_bytes_per_packet * PLAYBACK_URB_ISO_PACKETS;
|
||||
|
||||
|
|
@ -496,6 +547,7 @@ static int tascam_pcm_prepare(struct snd_pcm_substream *substream)
|
|||
|
||||
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;
|
||||
for (i = 0; i < PLAYBACK_URB_ISO_PACKETS; i++) {
|
||||
|
|
@ -530,6 +582,7 @@ static int tascam_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
|
|||
}
|
||||
|
||||
if (start) {
|
||||
// Submit all feedback and playback URBs to start the stream.
|
||||
for (i = 0; i < NUM_FEEDBACK_URBS; i++) {
|
||||
err = usb_submit_urb(tascam->feedback_urbs[i], GFP_ATOMIC);
|
||||
if (err < 0) {
|
||||
|
|
@ -542,8 +595,10 @@ static int tascam_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
|
|||
err = usb_submit_urb(tascam->playback_urbs[i], GFP_ATOMIC);
|
||||
if (err < 0) {
|
||||
int j;
|
||||
|
||||
dev_err(tascam->card->dev, "Failed to submit playback URB %d: %d\n", i, err);
|
||||
atomic_set(&tascam->playback_active, 0);
|
||||
// Unlink any URBs that were successfully submitted.
|
||||
for (j = 0; j < NUM_FEEDBACK_URBS; j++)
|
||||
usb_unlink_urb(tascam->feedback_urbs[j]);
|
||||
for (j = 0; j < i; j++)
|
||||
|
|
@ -552,6 +607,7 @@ static int tascam_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
|
|||
}
|
||||
}
|
||||
} else {
|
||||
// Unlink all URBs to stop the stream.
|
||||
for (i = 0; i < NUM_PLAYBACK_URBS; i++)
|
||||
usb_unlink_urb(tascam->playback_urbs[i]);
|
||||
for (i = 0; i < NUM_FEEDBACK_URBS; i++)
|
||||
|
|
@ -570,7 +626,10 @@ static snd_pcm_uframes_t tascam_pcm_pointer(struct snd_pcm_substream *substream)
|
|||
return 0;
|
||||
|
||||
pos = tascam->playback_frames_consumed;
|
||||
return runtime ? div_u64(pos, 1) % runtime->buffer_size : 0;
|
||||
|
||||
// Return the hardware position within the circular buffer.
|
||||
// The 64-bit modulo will be handled correctly by the compiler.
|
||||
return runtime ? pos % runtime->buffer_size : 0;
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -578,6 +637,7 @@ static snd_pcm_uframes_t tascam_pcm_pointer(struct snd_pcm_substream *substream)
|
|||
/* --- URB Completion Handlers --- */
|
||||
/*============================================================================*/
|
||||
|
||||
// This is the playback half of the "Packet Fixing" engine.
|
||||
static void playback_urb_complete(struct urb *urb)
|
||||
{
|
||||
struct tascam_card *tascam = urb->context;
|
||||
|
|
@ -602,16 +662,13 @@ static void playback_urb_complete(struct urb *urb)
|
|||
|
||||
spin_lock_irqsave(&tascam->lock, flags);
|
||||
|
||||
/* DEBUG: Log which sizing logic is being used. */
|
||||
if (tascam->feedback_synced)
|
||||
dev_info_ratelimited(tascam->card->dev, "Playback: Using DYNAMIC packet sizes (synced).\n");
|
||||
else
|
||||
dev_info_ratelimited(tascam->card->dev, "Playback: Using NOMINAL packet sizes (not synced).\n");
|
||||
|
||||
// Prepare the next playback URB.
|
||||
for (i = 0; i < PLAYBACK_URB_ISO_PACKETS; i++) {
|
||||
unsigned int frames_for_packet;
|
||||
size_t bytes_for_packet;
|
||||
|
||||
// If synced, use the dynamic frame count from the accumulator.
|
||||
// If not, use the nominal frame count.
|
||||
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;
|
||||
|
|
@ -622,15 +679,18 @@ static void playback_urb_complete(struct urb *urb)
|
|||
bytes_for_packet = frames_for_packet * DEVICE_BYTES_PER_FRAME;
|
||||
|
||||
if ((urb_total_bytes + bytes_for_packet) > tascam->playback_urb_alloc_size) {
|
||||
dev_warn_ratelimited(tascam->card->dev, "Playback URB overflow, truncating packet.\n");
|
||||
urb->iso_frame_desc[i].length = 0;
|
||||
urb->iso_frame_desc[i].offset = urb_total_bytes;
|
||||
continue;
|
||||
}
|
||||
|
||||
// Copy audio data from ALSA buffer to the URB.
|
||||
for (f = 0; f < frames_for_packet; f++) {
|
||||
size_t alsa_pos_bytes = frames_to_bytes(runtime, tascam->driver_playback_pos);
|
||||
char *alsa_frame_ptr = runtime->dma_area + alsa_pos_bytes;
|
||||
|
||||
// Copy 2 channels from ALSA, then zero-pad to 4 channels for the device.
|
||||
memcpy(urb_buf_ptr, alsa_frame_ptr, ALSA_BYTES_PER_FRAME);
|
||||
memset(urb_buf_ptr + ALSA_BYTES_PER_FRAME, 0, DEVICE_BYTES_PER_FRAME - ALSA_BYTES_PER_FRAME);
|
||||
|
||||
|
|
@ -649,9 +709,6 @@ static void playback_urb_complete(struct urb *urb)
|
|||
|
||||
urb->transfer_buffer_length = urb_total_bytes;
|
||||
|
||||
/* DEBUG: Log the size of the URB we just prepared. */
|
||||
dev_info_ratelimited(tascam->card->dev, "Prepared playback URB, total bytes: %zu\n", urb_total_bytes);
|
||||
|
||||
if (atomic_read(&tascam->playback_active)) {
|
||||
urb->dev = tascam->dev;
|
||||
ret = usb_submit_urb(urb, GFP_ATOMIC);
|
||||
|
|
@ -660,6 +717,7 @@ static void playback_urb_complete(struct urb *urb)
|
|||
}
|
||||
}
|
||||
|
||||
// This is the feedback half of the "Packet Fixing" engine.
|
||||
static void feedback_urb_complete(struct urb *urb)
|
||||
{
|
||||
struct tascam_card *tascam = urb->context;
|
||||
|
|
@ -697,11 +755,13 @@ static void feedback_urb_complete(struct urb *urb)
|
|||
|
||||
was_synced = tascam->feedback_synced;
|
||||
|
||||
// Skip initial URBs to let the clock stabilize.
|
||||
if (tascam->feedback_urb_skip_count > 0) {
|
||||
tascam->feedback_urb_skip_count--;
|
||||
goto unlock_and_resubmit;
|
||||
}
|
||||
|
||||
// Process each feedback packet in the URB.
|
||||
for (p = 0; p < urb->number_of_packets; p++) {
|
||||
u8 feedback_value;
|
||||
const unsigned int *pattern;
|
||||
|
|
@ -712,9 +772,10 @@ static void feedback_urb_complete(struct urb *urb)
|
|||
continue;
|
||||
}
|
||||
|
||||
// The feedback value is the first byte of the 3-byte packet.
|
||||
feedback_value = *((u8 *)urb->transfer_buffer + urb->iso_frame_desc[p].offset);
|
||||
dev_info_ratelimited(tascam->card->dev, "Feedback received, value: %u\n", feedback_value);
|
||||
|
||||
// Validate the feedback value and look up the corresponding pattern.
|
||||
if (feedback_value >= tascam->feedback_base_value &&
|
||||
feedback_value <= tascam->feedback_max_value) {
|
||||
pattern_index = feedback_value - tascam->feedback_base_value;
|
||||
|
|
@ -724,20 +785,24 @@ static void feedback_urb_complete(struct urb *urb)
|
|||
pattern = NULL;
|
||||
}
|
||||
|
||||
// If a valid pattern was found, write it to the accumulator.
|
||||
if (pattern) {
|
||||
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];
|
||||
}
|
||||
tascam->feedback_pattern_in_idx = (tascam->feedback_pattern_in_idx + 8) % FEEDBACK_ACCUMULATOR_SIZE;
|
||||
} else {
|
||||
// If pattern is invalid, assume nominal rate for this interval.
|
||||
u64 nominal_frames_per_ms = runtime->rate / 1000;
|
||||
|
||||
total_frames_in_urb += nominal_frames_per_ms;
|
||||
}
|
||||
}
|
||||
|
||||
/* Update and log the sync state transition. */
|
||||
// Update and log the sync state transition.
|
||||
if (sync_lost_this_urb) {
|
||||
if (was_synced)
|
||||
dev_info(tascam->card->dev, "Sync Lost (bad packet)!\n");
|
||||
|
|
@ -748,9 +813,11 @@ static void feedback_urb_complete(struct urb *urb)
|
|||
tascam->feedback_synced = true;
|
||||
}
|
||||
|
||||
// Update the total number of frames consumed by the hardware.
|
||||
if (total_frames_in_urb > 0)
|
||||
tascam->playback_frames_consumed += total_frames_in_urb;
|
||||
|
||||
// Check if a period has elapsed and notify ALSA.
|
||||
current_period = div_u64(tascam->playback_frames_consumed, runtime->period_size);
|
||||
if (current_period > tascam->last_period_pos) {
|
||||
tascam->last_period_pos = current_period;
|
||||
|
|
@ -778,6 +845,9 @@ resubmit:
|
|||
/* --- Device Configuration and Probing --- */
|
||||
/*============================================================================*/
|
||||
|
||||
// This function sends the precise sequence of control messages required to
|
||||
// initialize the device and set a new sample rate. This sequence was
|
||||
// determined by reverse-engineering the official drivers.
|
||||
static int us144mkii_configure_device_for_rate(struct tascam_card *tascam, int rate)
|
||||
{
|
||||
struct usb_device *dev = tascam->dev;
|
||||
|
|
@ -785,6 +855,7 @@ static int us144mkii_configure_device_for_rate(struct tascam_card *tascam, int r
|
|||
u16 rate_vendor_wValue;
|
||||
int err = 0;
|
||||
|
||||
// Payloads for UAC_SET_CUR request, specific to each sample rate.
|
||||
static const u8 payload_44100[] = {0x44, 0xac, 0x00};
|
||||
static const u8 payload_48000[] = {0x80, 0xbb, 0x00};
|
||||
static const u8 payload_88200[] = {0x88, 0x58, 0x01};
|
||||
|
|
@ -792,7 +863,8 @@ static int us144mkii_configure_device_for_rate(struct tascam_card *tascam, int r
|
|||
const u8 *current_payload_src;
|
||||
|
||||
rate_payload_buf = kmalloc(3, GFP_KERNEL);
|
||||
if (!rate_payload_buf) return -ENOMEM;
|
||||
if (!rate_payload_buf)
|
||||
return -ENOMEM;
|
||||
|
||||
switch (rate) {
|
||||
case 44100: current_payload_src = payload_44100; rate_vendor_wValue = 0x1000; break;
|
||||
|
|
@ -807,31 +879,53 @@ static int us144mkii_configure_device_for_rate(struct tascam_card *tascam, int r
|
|||
|
||||
memcpy(rate_payload_buf, current_payload_src, 3);
|
||||
|
||||
// --- Begin Control Message Sequence ---
|
||||
dev_info(&dev->dev, "Configuring device for %d Hz\n", rate);
|
||||
|
||||
// 1. Set Initial Mode
|
||||
err = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), VENDOR_REQ_MODE_CONTROL, RT_H2D_VENDOR_DEV, 0x0010, 0x0000, NULL, 0, USB_CTRL_TIMEOUT_MS);
|
||||
if (err < 0) { goto cleanup_buf; }
|
||||
if (err < 0)
|
||||
goto cleanup_buf;
|
||||
|
||||
// 2. Set Sample Rate on Capture and Playback Endpoints
|
||||
err = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), UAC_SET_CUR, RT_H2D_CLASS_EP, UAC_SAMPLING_FREQ_CONTROL, EP_CAPTURE_DATA, rate_payload_buf, 3, USB_CTRL_TIMEOUT_MS);
|
||||
if (err < 0) { goto cleanup_buf; }
|
||||
if (err < 0)
|
||||
goto cleanup_buf;
|
||||
err = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), UAC_SET_CUR, RT_H2D_CLASS_EP, UAC_SAMPLING_FREQ_CONTROL, EP_AUDIO_OUT, rate_payload_buf, 3, USB_CTRL_TIMEOUT_MS);
|
||||
if (err < 0) { goto cleanup_buf; }
|
||||
if (err < 0)
|
||||
goto cleanup_buf;
|
||||
|
||||
// 3. Vendor-specific register writes
|
||||
err = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), VENDOR_REQ_REGISTER_WRITE, RT_H2D_VENDOR_DEV, 0x0d04, 0x0101, NULL, 0, USB_CTRL_TIMEOUT_MS);
|
||||
if (err < 0) { goto cleanup_buf; }
|
||||
if (err < 0)
|
||||
goto cleanup_buf;
|
||||
err = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), VENDOR_REQ_REGISTER_WRITE, RT_H2D_VENDOR_DEV, 0x0e00, 0x0101, NULL, 0, USB_CTRL_TIMEOUT_MS);
|
||||
if (err < 0) { goto cleanup_buf; }
|
||||
if (err < 0)
|
||||
goto cleanup_buf;
|
||||
err = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), VENDOR_REQ_REGISTER_WRITE, RT_H2D_VENDOR_DEV, 0x0f00, 0x0101, NULL, 0, USB_CTRL_TIMEOUT_MS);
|
||||
if (err < 0) { goto cleanup_buf; }
|
||||
if (err < 0)
|
||||
goto cleanup_buf;
|
||||
|
||||
// 4. Rate-dependent register write
|
||||
err = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), VENDOR_REQ_REGISTER_WRITE, RT_H2D_VENDOR_DEV, rate_vendor_wValue, 0x0101, NULL, 0, USB_CTRL_TIMEOUT_MS);
|
||||
if (err < 0) { goto cleanup_buf; }
|
||||
if (err < 0)
|
||||
goto cleanup_buf;
|
||||
|
||||
// 5. Final register write
|
||||
err = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), VENDOR_REQ_REGISTER_WRITE, RT_H2D_VENDOR_DEV, 0x110b, 0x0101, NULL, 0, USB_CTRL_TIMEOUT_MS);
|
||||
if (err < 0) { goto cleanup_buf; }
|
||||
if (err < 0)
|
||||
goto cleanup_buf;
|
||||
|
||||
// 6. Enable Streaming
|
||||
err = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), VENDOR_REQ_MODE_CONTROL, RT_H2D_VENDOR_DEV, 0x0030, 0x0000, NULL, 0, USB_CTRL_TIMEOUT_MS);
|
||||
if (err < 0) { goto cleanup_buf; }
|
||||
if (err < 0)
|
||||
goto cleanup_buf;
|
||||
|
||||
// --- End Control Message Sequence ---
|
||||
|
||||
cleanup_buf:
|
||||
if (err < 0)
|
||||
dev_err(&dev->dev, "Device configuration failed at rate %d with error %d\n", rate, err);
|
||||
kfree(rate_payload_buf);
|
||||
return err;
|
||||
}
|
||||
|
|
@ -841,6 +935,7 @@ static int tascam_create_pcm(struct tascam_card *tascam)
|
|||
struct snd_pcm *pcm;
|
||||
int err;
|
||||
|
||||
// Create one PCM device with 1 playback and 1 (stubbed) capture stream.
|
||||
err = snd_pcm_new(tascam->card, "US144MKII PCM", 0, 1, 1, &pcm);
|
||||
if (err < 0) {
|
||||
dev_err(tascam->card->dev, "Failed to create snd_pcm: %d\n", err);
|
||||
|
|
@ -864,6 +959,7 @@ static int tascam_create_pcm(struct tascam_card *tascam)
|
|||
static void tascam_card_private_free(struct snd_card *card)
|
||||
{
|
||||
struct tascam_card *tascam = card->private_data;
|
||||
|
||||
if (tascam && tascam->dev) {
|
||||
usb_put_dev(tascam->dev);
|
||||
tascam->dev = NULL;
|
||||
|
|
@ -878,6 +974,7 @@ static int tascam_probe(struct usb_interface *intf, const struct usb_device_id *
|
|||
int err, dev_idx;
|
||||
u8 *handshake_buf;
|
||||
|
||||
// This driver binds to interface 0.
|
||||
dev_idx = intf->cur_altsetting->desc.bInterfaceNumber;
|
||||
if (dev_idx != 0)
|
||||
return -ENODEV;
|
||||
|
|
@ -907,6 +1004,7 @@ static int tascam_probe(struct usb_interface *intf, const struct usb_device_id *
|
|||
le16_to_cpu(dev->descriptor.idProduct),
|
||||
dev->bus->bus_name);
|
||||
|
||||
// The device has two interfaces; we need to claim both.
|
||||
tascam->iface1 = usb_ifnum_to_if(dev, 1);
|
||||
if (!tascam->iface1) {
|
||||
dev_err(&intf->dev, "Interface 1 not found.\n");
|
||||
|
|
@ -920,13 +1018,24 @@ static int tascam_probe(struct usb_interface *intf, const struct usb_device_id *
|
|||
goto free_card_obj;
|
||||
}
|
||||
|
||||
// Set both interfaces to alternate setting 1 to enable all endpoints.
|
||||
err = usb_set_interface(dev, 0, 1);
|
||||
if (err < 0) { dev_err(&intf->dev, "Set Alt Setting on Intf 0 failed: %d\n", err); goto release_iface1_and_free_card; }
|
||||
if (err < 0) {
|
||||
dev_err(&intf->dev, "Set Alt Setting on Intf 0 failed: %d\n", err);
|
||||
goto release_iface1_and_free_card;
|
||||
}
|
||||
err = usb_set_interface(dev, 1, 1);
|
||||
if (err < 0) { dev_err(&intf->dev, "Set Alt Setting on Intf 1 failed: %d\n", err); goto release_iface1_and_free_card; }
|
||||
if (err < 0) {
|
||||
dev_err(&intf->dev, "Set Alt Setting on Intf 1 failed: %d\n", err);
|
||||
goto release_iface1_and_free_card;
|
||||
}
|
||||
|
||||
// Perform the initial handshake read, as per the reverse-eng report.
|
||||
handshake_buf = kmalloc(1, GFP_KERNEL);
|
||||
if (!handshake_buf) { err = -ENOMEM; goto release_iface1_and_free_card; }
|
||||
if (!handshake_buf) {
|
||||
err = -ENOMEM;
|
||||
goto release_iface1_and_free_card;
|
||||
}
|
||||
err = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), VENDOR_REQ_MODE_CONTROL,
|
||||
RT_D2H_VENDOR_DEV, 0x0000, 0x0000,
|
||||
handshake_buf, 1, USB_CTRL_TIMEOUT_MS);
|
||||
|
|
@ -971,6 +1080,7 @@ static void tascam_disconnect(struct usb_interface *intf)
|
|||
if (!tascam)
|
||||
return;
|
||||
|
||||
// Only disconnect if this is the primary interface (iface0).
|
||||
if (intf != tascam->iface0)
|
||||
return;
|
||||
|
||||
|
|
@ -978,12 +1088,14 @@ static void tascam_disconnect(struct usb_interface *intf)
|
|||
|
||||
snd_card_disconnect(tascam->card);
|
||||
|
||||
// Release the secondary interface.
|
||||
if (tascam->iface1) {
|
||||
usb_set_intfdata(tascam->iface1, NULL);
|
||||
usb_driver_release_interface(&tascam_alsa_driver, tascam->iface1);
|
||||
tascam->iface1 = NULL;
|
||||
}
|
||||
|
||||
// The card and its private data will be freed when all PCMs are closed.
|
||||
snd_card_free_when_closed(tascam->card);
|
||||
}
|
||||
|
||||
|
|
|
|||
BIN
us144mkii.ko
BIN
us144mkii.ko
Binary file not shown.
Loading…
Reference in New Issue