fix incomplete refactor
This commit is contained in:
parent
5675faf5d3
commit
9f0c814b2d
4
Makefile
4
Makefile
|
|
@ -1,8 +1,8 @@
|
|||
obj-m += snd-usb-us144mkii.o
|
||||
snd-usb-us144mkii-y := us144mkii.o pcm.o playback.o capture.o controls.o midi.o
|
||||
snd-usb-us144mkii-y := us144mkii.o pcm.o midi.o controls.o
|
||||
|
||||
all:
|
||||
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
|
||||
|
||||
clean:
|
||||
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
|
||||
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
|
||||
212
capture.c
212
capture.c
|
|
@ -1,212 +0,0 @@
|
|||
// SPDX-License-Identifier: GPL-2.0-only
|
||||
|
||||
#include "us144mkii.h"
|
||||
#include "capture.h"
|
||||
|
||||
/**
|
||||
* process_capture_routing_us144mkii() - Apply capture routing matrix
|
||||
* @tascam: The driver instance.
|
||||
* @decoded_block: Buffer containing 4 channels of S32LE decoded audio.
|
||||
* @routed_block: Buffer to be filled for ALSA.
|
||||
*/
|
||||
void process_capture_routing_us144mkii(struct tascam_card *tascam,
|
||||
const s32 *decoded_block,
|
||||
s32 *routed_block)
|
||||
{
|
||||
int f;
|
||||
const s32 *src_frame;
|
||||
s32 *dst_frame;
|
||||
|
||||
for (f = 0; f < FRAMES_PER_DECODE_BLOCK; f++) {
|
||||
src_frame = decoded_block + (f * DECODED_CHANNELS_PER_FRAME);
|
||||
dst_frame = routed_block + (f * DECODED_CHANNELS_PER_FRAME);
|
||||
|
||||
/* ch1 and ch2 Source */
|
||||
if (tascam->capture_12_source == 0) { /* analog inputs */
|
||||
dst_frame[0] = src_frame[0]; /* Analog L */
|
||||
dst_frame[1] = src_frame[1]; /* Analog R */
|
||||
} else { /* digital inputs */
|
||||
dst_frame[0] = src_frame[2]; /* Digital L */
|
||||
dst_frame[1] = src_frame[3]; /* Digital R */
|
||||
}
|
||||
|
||||
/* ch3 and ch4 Source */
|
||||
if (tascam->capture_34_source == 0) { /* analog inputs */
|
||||
dst_frame[2] = src_frame[0]; /* Analog L (Duplicate) */
|
||||
dst_frame[3] = src_frame[1]; /* Analog R (Duplicate) */
|
||||
} else { /* digital inputs */
|
||||
dst_frame[2] = src_frame[2]; /* Digital L */
|
||||
dst_frame[3] = src_frame[3]; /* Digital R */
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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).
|
||||
*/
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* tascam_capture_work_handler() - Deferred work for processing capture data.
|
||||
* @work: the work_struct instance
|
||||
*
|
||||
* This function runs in a kernel thread context, not an IRQ context. It reads
|
||||
* raw data from the capture ring buffer, decodes it, applies routing, and
|
||||
* copies the final audio data into the ALSA capture ring buffer. This offloads
|
||||
* the CPU-intensive decoding from the time-sensitive URB completion handlers.
|
||||
*/
|
||||
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;
|
||||
unsigned long flags;
|
||||
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;
|
||||
|
||||
spin_lock_irqsave(&tascam->lock, flags);
|
||||
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 i;
|
||||
|
||||
for (i = 0; i < RAW_BYTES_PER_DECODE_BLOCK; i++)
|
||||
raw_block[i] = tascam->capture_ring_buffer[(read_ptr + i) % CAPTURE_RING_BUFFER_SIZE];
|
||||
tascam->capture_ring_buffer_read_ptr = (read_ptr + RAW_BYTES_PER_DECODE_BLOCK) % CAPTURE_RING_BUFFER_SIZE;
|
||||
}
|
||||
spin_unlock_irqrestore(&tascam->lock, flags);
|
||||
|
||||
if (!can_process)
|
||||
break;
|
||||
|
||||
decode_tascam_capture_block(raw_block, decoded_block);
|
||||
process_capture_routing_us144mkii(tascam, decoded_block, routed_block);
|
||||
|
||||
spin_lock_irqsave(&tascam->lock, flags);
|
||||
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;
|
||||
}
|
||||
}
|
||||
spin_unlock_irqrestore(&tascam->lock, flags);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* capture_urb_complete() - Completion handler for capture bulk URBs.
|
||||
* @urb: the completed URB
|
||||
*
|
||||
* This function runs in interrupt context. It copies the received raw data
|
||||
* into an intermediate ring buffer and then schedules the workqueue to process
|
||||
* it. It then resubmits the URB to receive more data.
|
||||
*/
|
||||
void capture_urb_complete(struct urb *urb)
|
||||
{
|
||||
struct tascam_card *tascam = urb->context;
|
||||
int ret;
|
||||
unsigned long flags;
|
||||
|
||||
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) {
|
||||
size_t i;
|
||||
size_t write_ptr;
|
||||
|
||||
spin_lock_irqsave(&tascam->lock, flags);
|
||||
write_ptr = tascam->capture_ring_buffer_write_ptr;
|
||||
for (i = 0; i < urb->actual_length; i++) {
|
||||
tascam->capture_ring_buffer[write_ptr] = ((u8 *)urb->transfer_buffer)[i];
|
||||
write_ptr = (write_ptr + 1) % CAPTURE_RING_BUFFER_SIZE;
|
||||
}
|
||||
tascam->capture_ring_buffer_write_ptr = write_ptr;
|
||||
spin_unlock_irqrestore(&tascam->lock, flags);
|
||||
|
||||
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);
|
||||
}
|
||||
out:
|
||||
usb_put_urb(urb);
|
||||
}
|
||||
12
capture.h
12
capture.h
|
|
@ -1,12 +0,0 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
#ifndef __US144MKII_CAPTURE_H
|
||||
#define __US144MKII_CAPTURE_H
|
||||
|
||||
#include "us144mkii.h"
|
||||
|
||||
void process_capture_routing_us144mkii(struct tascam_card *tascam, const s32 *decoded_block, s32 *routed_block);
|
||||
void decode_tascam_capture_block(const u8 *src_block, s32 *dst_block);
|
||||
void tascam_capture_work_handler(struct work_struct *work);
|
||||
void capture_urb_complete(struct urb *urb);
|
||||
|
||||
#endif /* __US144MKII_CAPTURE_H */
|
||||
192
controls.c
192
controls.c
|
|
@ -1,11 +1,34 @@
|
|||
// SPDX-License-Identifier: GPL-2.0-only
|
||||
// Copyright (c) 2025 serifpersia <ramiserifpersia@gmail.com>
|
||||
|
||||
#include "us144mkii.h"
|
||||
#include "controls.h"
|
||||
|
||||
/**
|
||||
* @brief 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"};
|
||||
|
||||
/**
|
||||
* @brief 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)
|
||||
{
|
||||
uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
|
||||
|
|
@ -19,6 +42,17 @@ static int tascam_playback_source_info(struct snd_kcontrol *kcontrol, struct snd
|
|||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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);
|
||||
|
|
@ -27,6 +61,17 @@ static int tascam_line_out_get(struct snd_kcontrol *kcontrol, struct snd_ctl_ele
|
|||
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);
|
||||
|
|
@ -39,11 +84,30 @@ static int tascam_line_out_put(struct snd_kcontrol *kcontrol, struct snd_ctl_ele
|
|||
return 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 OUTPUTS 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);
|
||||
|
|
@ -52,6 +116,17 @@ static int tascam_digital_out_get(struct snd_kcontrol *kcontrol, struct snd_ctl_
|
|||
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);
|
||||
|
|
@ -64,11 +139,29 @@ static int tascam_digital_out_put(struct snd_kcontrol *kcontrol, struct snd_ctl_
|
|||
return 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 OUTPUTS 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)
|
||||
{
|
||||
uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
|
||||
|
|
@ -82,6 +175,17 @@ static int tascam_capture_source_info(struct snd_kcontrol *kcontrol, struct snd_
|
|||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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);
|
||||
|
|
@ -90,6 +194,17 @@ static int tascam_capture_12_get(struct snd_kcontrol *kcontrol, struct snd_ctl_e
|
|||
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);
|
||||
|
|
@ -102,11 +217,30 @@ static int tascam_capture_12_put(struct snd_kcontrol *kcontrol, struct snd_ctl_e
|
|||
return 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 and ch2 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);
|
||||
|
|
@ -115,6 +249,17 @@ static int tascam_capture_34_get(struct snd_kcontrol *kcontrol, struct snd_ctl_e
|
|||
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);
|
||||
|
|
@ -127,11 +272,29 @@ static int tascam_capture_34_put(struct snd_kcontrol *kcontrol, struct snd_ctl_e
|
|||
return 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 and ch4 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;
|
||||
|
|
@ -141,6 +304,17 @@ static int tascam_samplerate_info(struct snd_kcontrol *kcontrol, struct snd_ctl_
|
|||
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);
|
||||
|
|
@ -170,6 +344,12 @@ static int tascam_samplerate_get(struct snd_kcontrol *kcontrol, struct snd_ctl_e
|
|||
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",
|
||||
|
|
@ -178,6 +358,16 @@ static const struct snd_kcontrol_new tascam_samplerate_control = {
|
|||
.access = SNDRV_CTL_ELEM_ACCESS_READ,
|
||||
};
|
||||
|
||||
/**
|
||||
* tascam_create_controls() - Creates and adds ALSA mixer controls for the device.
|
||||
* @tascam: The driver instance.
|
||||
*
|
||||
* This function registers custom ALSA controls for managing audio routing
|
||||
* (line out source, digital out source, capture 1-2 source, capture 3-4 source)
|
||||
* and displaying the current sample rate.
|
||||
*
|
||||
* Return: 0 on success, or a negative error code on failure.
|
||||
*/
|
||||
int tascam_create_controls(struct tascam_card *tascam)
|
||||
{
|
||||
int err;
|
||||
|
|
|
|||
|
|
@ -1,9 +0,0 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
#ifndef __US144MKII_CONTROLS_H
|
||||
#define __US144MKII_CONTROLS_H
|
||||
|
||||
#include "us144mkii.h"
|
||||
|
||||
int tascam_create_controls(struct tascam_card *tascam);
|
||||
|
||||
#endif /* __US144MKII_CONTROLS_H */
|
||||
78
midi.c
78
midi.c
|
|
@ -1,7 +1,9 @@
|
|||
// SPDX-License-Identifier: GPL-2.0-only
|
||||
// Copyright (c) 2025 serifpersia <ramiserifpersia@gmail.com>
|
||||
|
||||
#include "us144mkii.h"
|
||||
#include "midi.h"
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* tascam_midi_in_work_handler() - Deferred work for processing MIDI input.
|
||||
|
|
@ -11,7 +13,7 @@
|
|||
* the kfifo, processes it by stripping protocol-specific padding bytes, and
|
||||
* passes the clean MIDI data to the ALSA rawmidi subsystem.
|
||||
*/
|
||||
void tascam_midi_in_work_handler(struct work_struct *work)
|
||||
static void tascam_midi_in_work_handler(struct work_struct *work)
|
||||
{
|
||||
struct tascam_card *tascam = container_of(work, struct tascam_card, midi_in_work);
|
||||
u8 buf[MIDI_IN_BUF_SIZE];
|
||||
|
|
@ -89,6 +91,15 @@ out:
|
|||
usb_put_urb(urb);
|
||||
}
|
||||
|
||||
/**
|
||||
* tascam_midi_in_open() - Opens the MIDI input substream.
|
||||
* @substream: The ALSA rawmidi substream to open.
|
||||
*
|
||||
* This function stores a reference to the MIDI input substream in the
|
||||
* driver's private data.
|
||||
*
|
||||
* Return: 0 on success.
|
||||
*/
|
||||
static int tascam_midi_in_open(struct snd_rawmidi_substream *substream)
|
||||
{
|
||||
struct tascam_card *tascam = substream->rmidi->private_data;
|
||||
|
|
@ -97,11 +108,27 @@ static int tascam_midi_in_open(struct snd_rawmidi_substream *substream)
|
|||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* tascam_midi_in_close() - Closes the MIDI input substream.
|
||||
* @substream: The ALSA rawmidi substream to close.
|
||||
*
|
||||
* Return: 0 on success.
|
||||
*/
|
||||
static int tascam_midi_in_close(struct snd_rawmidi_substream *substream)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* tascam_midi_in_trigger() - Triggers MIDI input stream activity.
|
||||
* @substream: The ALSA rawmidi substream.
|
||||
* @up: Boolean indicating whether to start (1) or stop (0) the stream.
|
||||
*
|
||||
* This function starts or stops the MIDI input URBs based on the 'up' parameter.
|
||||
* When starting, it resets the kfifo and submits all MIDI input URBs.
|
||||
* When stopping, it kills all anchored MIDI input URBs and cancels the
|
||||
* associated workqueue.
|
||||
*/
|
||||
static void tascam_midi_in_trigger(struct snd_rawmidi_substream *substream, int up)
|
||||
{
|
||||
struct tascam_card *tascam = substream->rmidi->private_data;
|
||||
|
|
@ -133,6 +160,12 @@ static void tascam_midi_in_trigger(struct snd_rawmidi_substream *substream, int
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* tascam_midi_in_ops - ALSA rawmidi operations for MIDI input.
|
||||
*
|
||||
* This structure defines the callback functions for MIDI input stream operations,
|
||||
* including open, close, and trigger.
|
||||
*/
|
||||
static struct snd_rawmidi_ops tascam_midi_in_ops = {
|
||||
.open = tascam_midi_in_open,
|
||||
.close = tascam_midi_in_close,
|
||||
|
|
@ -194,7 +227,7 @@ out:
|
|||
* This function pulls as many bytes as will fit into one packet from the
|
||||
* ALSA buffer and sends them.
|
||||
*/
|
||||
void tascam_midi_out_work_handler(struct work_struct *work)
|
||||
static void tascam_midi_out_work_handler(struct work_struct *work)
|
||||
{
|
||||
struct tascam_card *tascam =
|
||||
container_of(work, struct tascam_card, midi_out_work);
|
||||
|
|
@ -261,6 +294,15 @@ void tascam_midi_out_work_handler(struct work_struct *work)
|
|||
}
|
||||
|
||||
|
||||
/**
|
||||
* tascam_midi_out_open() - Opens the MIDI output substream.
|
||||
* @substream: The ALSA rawmidi substream to open.
|
||||
*
|
||||
* This function stores a reference to the MIDI output substream in the
|
||||
* driver's private data and initializes the MIDI running status.
|
||||
*
|
||||
* Return: 0 on success.
|
||||
*/
|
||||
static int tascam_midi_out_open(struct snd_rawmidi_substream *substream)
|
||||
{
|
||||
struct tascam_card *tascam = substream->rmidi->private_data;
|
||||
|
|
@ -271,11 +313,24 @@ static int tascam_midi_out_open(struct snd_rawmidi_substream *substream)
|
|||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* tascam_midi_out_close() - Closes the MIDI output substream.
|
||||
* @substream: The ALSA rawmidi substream to close.
|
||||
*
|
||||
* Return: 0 on success.
|
||||
*/
|
||||
static int tascam_midi_out_close(struct snd_rawmidi_substream *substream)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* tascam_midi_out_drain() - Drains the MIDI output stream.
|
||||
* @substream: The ALSA rawmidi substream.
|
||||
*
|
||||
* This function cancels any pending MIDI output work and kills all
|
||||
* anchored MIDI output URBs, ensuring all data is sent or discarded.
|
||||
*/
|
||||
static void tascam_midi_out_drain(struct snd_rawmidi_substream *substream)
|
||||
{
|
||||
struct tascam_card *tascam = substream->rmidi->private_data;
|
||||
|
|
@ -284,6 +339,14 @@ static void tascam_midi_out_drain(struct snd_rawmidi_substream *substream)
|
|||
usb_kill_anchored_urbs(&tascam->midi_out_anchor);
|
||||
}
|
||||
|
||||
/**
|
||||
* tascam_midi_out_trigger() - Triggers MIDI output stream activity.
|
||||
* @substream: The ALSA rawmidi substream.
|
||||
* @up: Boolean indicating whether to start (1) or stop (0) the stream.
|
||||
*
|
||||
* This function starts or stops the MIDI output workqueue based on the
|
||||
* 'up' parameter.
|
||||
*/
|
||||
static void tascam_midi_out_trigger(struct snd_rawmidi_substream *substream, int up)
|
||||
{
|
||||
struct tascam_card *tascam = substream->rmidi->private_data;
|
||||
|
|
@ -296,6 +359,12 @@ static void tascam_midi_out_trigger(struct snd_rawmidi_substream *substream, int
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* tascam_midi_out_ops - ALSA rawmidi operations for MIDI output.
|
||||
*
|
||||
* This structure defines the callback functions for MIDI output stream operations,
|
||||
* including open, close, trigger, and drain.
|
||||
*/
|
||||
static struct snd_rawmidi_ops tascam_midi_out_ops = {
|
||||
.open = tascam_midi_out_open,
|
||||
.close = tascam_midi_out_close,
|
||||
|
|
@ -327,5 +396,8 @@ int tascam_create_midi(struct tascam_card *tascam)
|
|||
SNDRV_RAWMIDI_INFO_OUTPUT |
|
||||
SNDRV_RAWMIDI_INFO_DUPLEX;
|
||||
|
||||
INIT_WORK(&tascam->midi_in_work, tascam_midi_in_work_handler);
|
||||
INIT_WORK(&tascam->midi_out_work, tascam_midi_out_work_handler);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
9
midi.h
9
midi.h
|
|
@ -1,9 +0,0 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
#ifndef __US144MKII_MIDI_H
|
||||
#define __US144MKII_MIDI_H
|
||||
|
||||
#include "us144mkii.h"
|
||||
|
||||
int tascam_create_midi(struct tascam_card *tascam);
|
||||
|
||||
#endif /* __US144MKII_MIDI_H */
|
||||
16
pcm.h
16
pcm.h
|
|
@ -1,16 +0,0 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
#ifndef __US144MKII_PCM_H
|
||||
#define __US144MKII_PCM_H
|
||||
|
||||
#include "us144mkii.h"
|
||||
|
||||
extern const struct snd_pcm_ops tascam_playback_ops;
|
||||
extern const struct snd_pcm_ops tascam_capture_ops;
|
||||
|
||||
int tascam_create_pcm(struct tascam_card *tascam);
|
||||
void tascam_free_urbs(struct tascam_card *tascam);
|
||||
int tascam_alloc_urbs(struct tascam_card *tascam);
|
||||
int us144mkii_configure_device_for_rate(struct tascam_card *tascam, int rate);
|
||||
void tascam_stop_work_handler(struct work_struct *work);
|
||||
|
||||
#endif /* __US144MKII_PCM_H */
|
||||
288
playback.c
288
playback.c
|
|
@ -1,288 +0,0 @@
|
|||
// SPDX-License-Identifier: GPL-2.0-only
|
||||
|
||||
#include "us144mkii.h"
|
||||
#include "playback.h"
|
||||
|
||||
/**
|
||||
* process_playback_routing_us144mkii() - Apply playback routing matrix
|
||||
* @tascam: The driver instance.
|
||||
* @src_buffer: Buffer containing 4 channels of S24_3LE audio from ALSA.
|
||||
* @dst_buffer: Buffer to be filled for the USB device.
|
||||
* @frames: Number of frames to process.
|
||||
*/
|
||||
void process_playback_routing_us144mkii(struct tascam_card *tascam,
|
||||
const u8 *src_buffer,
|
||||
u8 *dst_buffer, size_t frames)
|
||||
{
|
||||
size_t f;
|
||||
const u8 *src_12, *src_34;
|
||||
u8 *dst_line, *dst_digital;
|
||||
|
||||
for (f = 0; f < frames; ++f) {
|
||||
src_12 = src_buffer + f * BYTES_PER_FRAME;
|
||||
src_34 = src_12 + (2 * BYTES_PER_SAMPLE);
|
||||
dst_line = dst_buffer + f * BYTES_PER_FRAME;
|
||||
dst_digital = dst_line + (2 * BYTES_PER_SAMPLE);
|
||||
|
||||
/* LINE OUTPUTS (ch1/2 on device) */
|
||||
if (tascam->line_out_source == 0) /* "ch1 and ch2" */
|
||||
memcpy(dst_line, src_12, 2 * BYTES_PER_SAMPLE);
|
||||
else /* "ch3 and ch4" */
|
||||
memcpy(dst_line, src_34, 2 * BYTES_PER_SAMPLE);
|
||||
|
||||
/* DIGITAL OUTPUTS (ch3/4 on device) */
|
||||
if (tascam->digital_out_source == 0) /* "ch1 and ch2" */
|
||||
memcpy(dst_digital, src_12, 2 * BYTES_PER_SAMPLE);
|
||||
else /* "ch3 and ch4" */
|
||||
memcpy(dst_digital, src_34, 2 * BYTES_PER_SAMPLE);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* playback_urb_complete() - Completion handler for playback isochronous URBs.
|
||||
* @urb: the completed URB
|
||||
*
|
||||
* This function runs in interrupt context. It calculates the number of bytes
|
||||
* to send in the next set of packets based on the feedback-driven clock,
|
||||
* copies the audio data from the ALSA ring buffer (applying routing), and
|
||||
* resubmits the URB.
|
||||
*/
|
||||
void playback_urb_complete(struct urb *urb)
|
||||
{
|
||||
struct tascam_card *tascam = urb->context;
|
||||
struct snd_pcm_substream *substream;
|
||||
struct snd_pcm_runtime *runtime;
|
||||
unsigned long flags;
|
||||
u8 *src_buf, *dst_buf;
|
||||
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;
|
||||
|
||||
spin_lock_irqsave(&tascam->lock, flags);
|
||||
|
||||
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;
|
||||
|
||||
spin_unlock_irqrestore(&tascam->lock, flags);
|
||||
|
||||
if (total_bytes_for_urb > 0) {
|
||||
src_buf = runtime->dma_area + frames_to_bytes(runtime, offset_frames);
|
||||
dst_buf = tascam->playback_routing_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, src_buf, first_chunk_bytes);
|
||||
memcpy(dst_buf + first_chunk_bytes, runtime->dma_area, second_chunk_bytes);
|
||||
} else {
|
||||
memcpy(dst_buf, src_buf, total_bytes_for_urb);
|
||||
}
|
||||
|
||||
/* Apply routing to the contiguous data in our routing buffer */
|
||||
process_playback_routing_us144mkii(tascam, dst_buf, dst_buf, frames_to_copy);
|
||||
memcpy(urb->transfer_buffer, dst_buf, total_bytes_for_urb);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
out:
|
||||
usb_put_urb(urb);
|
||||
}
|
||||
|
||||
/**
|
||||
* feedback_urb_complete() - Completion handler for feedback isochronous URBs.
|
||||
* @urb: the completed URB
|
||||
*
|
||||
* This is the master clock for the driver. It runs in interrupt context.
|
||||
* It reads the feedback value from the device, which indicates how many
|
||||
* samples the device has consumed. This information is used to adjust the
|
||||
* playback rate and to advance the capture stream pointer, keeping both
|
||||
* streams in sync. It then calls snd_pcm_period_elapsed if necessary and
|
||||
* resubmits itself.
|
||||
*/
|
||||
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;
|
||||
unsigned long flags;
|
||||
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);
|
||||
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;
|
||||
|
||||
spin_lock_irqsave(&tascam->lock, flags);
|
||||
|
||||
if (tascam->feedback_urb_skip_count > 0) {
|
||||
tascam->feedback_urb_skip_count--;
|
||||
goto unlock_and_continue;
|
||||
}
|
||||
|
||||
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 && feedback_value >= tascam->feedback_base_value &&
|
||||
feedback_value <= tascam->feedback_max_value) {
|
||||
pattern = tascam->feedback_patterns[feedback_value - tascam->feedback_base_value];
|
||||
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");
|
||||
if (playback_ss)
|
||||
snd_pcm_stop(playback_ss, SNDRV_PCM_STATE_XRUN);
|
||||
if (capture_ss)
|
||||
snd_pcm_stop(capture_ss, SNDRV_PCM_STATE_XRUN);
|
||||
tascam->feedback_synced = false;
|
||||
goto unlock_and_continue;
|
||||
}
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
unlock_and_continue:
|
||||
spin_unlock_irqrestore(&tascam->lock, flags);
|
||||
|
||||
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);
|
||||
}
|
||||
11
playback.h
11
playback.h
|
|
@ -1,11 +0,0 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
#ifndef __US144MKII_PLAYBACK_H
|
||||
#define __US144MKII_PLAYBACK_H
|
||||
|
||||
#include "us144mkii.h"
|
||||
|
||||
void process_playback_routing_us144mkii(struct tascam_card *tascam, const u8 *src_buffer, u8 *dst_buffer, size_t frames);
|
||||
void playback_urb_complete(struct urb *urb);
|
||||
void feedback_urb_complete(struct urb *urb);
|
||||
|
||||
#endif /* __US144MKII_PLAYBACK_H */
|
||||
2013
us144mkii.c
2013
us144mkii.c
File diff suppressed because it is too large
Load Diff
225
us144mkii.h
225
us144mkii.h
|
|
@ -1,4 +1,6 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
// Copyright (c) 2025 serifpersia <ramiserifpersia@gmail.com>
|
||||
|
||||
#ifndef __US144MKII_H
|
||||
#define __US144MKII_H
|
||||
|
||||
|
|
@ -7,12 +9,11 @@
|
|||
#include <linux/kfifo.h>
|
||||
#include <sound/core.h>
|
||||
#include <sound/pcm.h>
|
||||
#include <sound/pcm_params.h>
|
||||
#include <sound/rawmidi.h>
|
||||
#include <sound/initval.h>
|
||||
#include <sound/control.h>
|
||||
|
||||
#define DRIVER_NAME "snd-usb-us144mkii"
|
||||
#define DRIVER_NAME "us144mkii"
|
||||
#define DRIVER_VERSION "1.7.4"
|
||||
|
||||
/* --- USB Device Identification --- */
|
||||
|
|
@ -98,7 +99,80 @@ enum tascam_register {
|
|||
#define FRAMES_PER_DECODE_BLOCK 8
|
||||
#define RAW_BYTES_PER_DECODE_BLOCK 512
|
||||
|
||||
/* --- Main Driver Data Structure --- */
|
||||
/**
|
||||
* struct tascam_card - Main driver data structure for the TASCAM US-144MKII.
|
||||
* @dev: Pointer to the USB device.
|
||||
* @iface0: Pointer to USB interface 0 (audio).
|
||||
* @iface1: Pointer to USB interface 1 (MIDI).
|
||||
* @card: Pointer to the ALSA sound card instance.
|
||||
* @pcm: Pointer to the ALSA PCM device.
|
||||
* @rmidi: Pointer to the ALSA rawmidi device.
|
||||
*
|
||||
* @playback_substream: Pointer to the active playback PCM substream.
|
||||
* @playback_urbs: Array of URBs for playback.
|
||||
* @playback_urb_alloc_size: Size of allocated buffer for each playback URB.
|
||||
* @feedback_urbs: Array of URBs for feedback.
|
||||
* @feedback_urb_alloc_size: Size of allocated buffer for each feedback URB.
|
||||
* @playback_active: Atomic flag indicating if playback is active.
|
||||
* @playback_frames_consumed: Total frames consumed by playback.
|
||||
* @driver_playback_pos: Current position in the ALSA playback buffer (frames).
|
||||
* @last_period_pos: Last reported period position for playback.
|
||||
* @playback_routing_buffer: Intermediate buffer for playback routing.
|
||||
*
|
||||
* @capture_substream: Pointer to the active capture PCM substream.
|
||||
* @capture_urbs: Array of URBs for capture.
|
||||
* @capture_urb_alloc_size: Size of allocated buffer for each capture URB.
|
||||
* @capture_active: Atomic flag indicating if capture is active.
|
||||
* @driver_capture_pos: Current position in the ALSA capture buffer (frames).
|
||||
* @capture_frames_processed: Total frames processed for capture.
|
||||
* @last_capture_period_pos: Last reported period position for capture.
|
||||
* @capture_ring_buffer: Ring buffer for raw capture data from USB.
|
||||
* @capture_ring_buffer_read_ptr: Read pointer for the capture ring buffer.
|
||||
* @capture_ring_buffer_write_ptr: Write pointer for the capture ring buffer.
|
||||
* @capture_decode_raw_block: Buffer for a raw 512-byte capture block.
|
||||
* @capture_decode_dst_block: Buffer for decoded 32-bit capture samples.
|
||||
* @capture_routing_buffer: Intermediate buffer for capture routing.
|
||||
* @capture_work: Work struct for deferred capture processing.
|
||||
* @stop_work: Work struct for deferred stream stopping.
|
||||
*
|
||||
* @midi_in_substream: Pointer to the active MIDI input substream.
|
||||
* @midi_out_substream: Pointer to the active MIDI output substream.
|
||||
* @midi_in_urbs: Array of URBs for MIDI input.
|
||||
* @midi_in_active: Atomic flag indicating if MIDI input is active.
|
||||
* @midi_in_fifo: FIFO for raw MIDI input data.
|
||||
* @midi_in_work: Work struct for deferred MIDI input processing.
|
||||
* @midi_in_lock: Spinlock for MIDI input FIFO.
|
||||
* @midi_out_urbs: Array of URBs for MIDI output.
|
||||
* @midi_out_active: Atomic flag indicating if MIDI output is active.
|
||||
* @midi_out_work: Work struct for deferred MIDI output processing.
|
||||
* @midi_out_urbs_in_flight: Bitmap of MIDI output URBs currently in flight.
|
||||
* @midi_out_lock: Spinlock for MIDI output.
|
||||
* @midi_running_status: Stores the last MIDI status byte for running status.
|
||||
*
|
||||
* @lock: Main spinlock for protecting shared driver state.
|
||||
* @active_urbs: Atomic counter for active URBs.
|
||||
* @current_rate: Currently configured sample rate of the device.
|
||||
* @line_out_source: Source for Line Outputs (0: Playback 1-2, 1: Playback 3-4).
|
||||
* @digital_out_source: Source for Digital Outputs (0: Playback 1-2, 1: Playback 3-4).
|
||||
* @capture_12_source: Source for Capture channels 1-2 (0: Analog In, 1: Digital In).
|
||||
* @capture_34_source: Source for Capture channels 3-4 (0: Analog In, 1: Digital In).
|
||||
*
|
||||
* @feedback_accumulator_pattern: Stores the calculated frames per packet for feedback.
|
||||
* @feedback_pattern_out_idx: Read index for feedback_accumulator_pattern.
|
||||
* @feedback_pattern_in_idx: Write index for feedback_accumulator_pattern.
|
||||
* @feedback_synced: Flag indicating if feedback is synced.
|
||||
* @feedback_consecutive_errors: Counter for consecutive feedback errors.
|
||||
* @feedback_urb_skip_count: Number of feedback URBs to skip initially for stabilization.
|
||||
* @feedback_patterns: Pointer to the current feedback patterns based on sample rate.
|
||||
* @feedback_base_value: Base value for feedback pattern lookup.
|
||||
* @feedback_max_value: Max value for feedback pattern lookup.
|
||||
*
|
||||
* @playback_anchor: USB anchor for playback URBs.
|
||||
* @capture_anchor: USB anchor for capture URBs.
|
||||
* @feedback_anchor: USB anchor for feedback URBs.
|
||||
* @midi_in_anchor: USB anchor for MIDI input URBs.
|
||||
* @midi_out_anchor: USB anchor for MIDI output URBs.
|
||||
*/
|
||||
struct tascam_card {
|
||||
struct usb_device *dev;
|
||||
struct usb_interface *iface0;
|
||||
|
|
@ -178,34 +252,145 @@ struct tascam_card {
|
|||
struct usb_anchor midi_out_anchor;
|
||||
};
|
||||
|
||||
/* from pcm.c */
|
||||
extern const struct snd_pcm_ops tascam_playback_ops;
|
||||
extern const struct snd_pcm_ops tascam_capture_ops;
|
||||
int tascam_create_pcm(struct tascam_card *tascam);
|
||||
/* main */
|
||||
/**
|
||||
* tascam_free_urbs() - Free all allocated URBs and associated buffers.
|
||||
* @tascam: the tascam_card instance
|
||||
*
|
||||
* This function kills, unlinks, and frees all playback, feedback, capture,
|
||||
* and MIDI URBs, along with their transfer buffers and the capture
|
||||
* ring/decode buffers.
|
||||
*/
|
||||
void tascam_free_urbs(struct tascam_card *tascam);
|
||||
|
||||
/**
|
||||
* tascam_alloc_urbs() - Allocate all URBs and associated buffers.
|
||||
* @tascam: the tascam_card instance
|
||||
*
|
||||
* This function allocates and initializes all URBs for playback, feedback,
|
||||
* capture, and MIDI, as well as the necessary buffers for data processing.
|
||||
*
|
||||
* Return: 0 on success, or a negative error code on failure.
|
||||
*/
|
||||
int tascam_alloc_urbs(struct tascam_card *tascam);
|
||||
int us144mkii_configure_device_for_rate(struct tascam_card *tascam, int rate);
|
||||
|
||||
/**
|
||||
* tascam_stop_work_handler() - Work handler to stop all active streams.
|
||||
* @work: Pointer to the work_struct.
|
||||
*
|
||||
* This function is scheduled to stop all active URBs (playback, feedback, capture)
|
||||
* and reset the active_urbs counter. It is used to gracefully stop streams
|
||||
* from a workqueue context.
|
||||
*/
|
||||
void tascam_stop_work_handler(struct work_struct *work);
|
||||
|
||||
/* from playback.c */
|
||||
void process_playback_routing_us144mkii(struct tascam_card *tascam, const u8 *src_buffer, u8 *dst_buffer, size_t frames);
|
||||
/* pcm.c */
|
||||
/**
|
||||
* playback_urb_complete() - Completion handler for playback isochronous URBs.
|
||||
* @urb: the completed URB
|
||||
*
|
||||
* This function runs in interrupt context. It calculates the number of bytes
|
||||
* to send in the next set of packets based on the feedback-driven clock,
|
||||
* copies the audio data from the ALSA ring buffer (applying routing), and
|
||||
* resubmits the URB.
|
||||
*/
|
||||
void playback_urb_complete(struct urb *urb);
|
||||
|
||||
/**
|
||||
* feedback_urb_complete() - Completion handler for feedback isochronous URBs.
|
||||
* @urb: the completed URB
|
||||
*
|
||||
* This is the master clock for the driver. It runs in interrupt context.
|
||||
* It reads the feedback value from the device, which indicates how many
|
||||
* samples the device has consumed. This information is used to adjust the
|
||||
* playback rate and to advance the capture stream pointer, keeping both
|
||||
* streams in sync. It then calls snd_pcm_period_elapsed if necessary and
|
||||
* resubmits itself.
|
||||
*/
|
||||
void feedback_urb_complete(struct urb *urb);
|
||||
|
||||
/* from capture.c */
|
||||
void process_capture_routing_us144mkii(struct tascam_card *tascam, const s32 *decoded_block, s32 *routed_block);
|
||||
void decode_tascam_capture_block(const u8 *src_block, s32 *dst_block);
|
||||
void tascam_capture_work_handler(struct work_struct *work);
|
||||
/**
|
||||
* capture_urb_complete() - Completion handler for capture bulk URBs.
|
||||
* @urb: the completed URB
|
||||
*
|
||||
* This function runs in interrupt context. It copies the received raw data
|
||||
* into an intermediate ring buffer and then schedules the workqueue to process
|
||||
* it. It then resubmits the URB to receive more data.
|
||||
*/
|
||||
void capture_urb_complete(struct urb *urb);
|
||||
|
||||
/* from controls.c */
|
||||
int tascam_create_controls(struct tascam_card *tascam);
|
||||
/**
|
||||
* tascam_init_pcm() - Initializes the ALSA PCM device.
|
||||
* @pcm: Pointer to the ALSA PCM device to initialize.
|
||||
*
|
||||
* This function sets up the PCM operations, adds ALSA controls for routing
|
||||
* and sample rate, and preallocates pages for the PCM buffer.
|
||||
*
|
||||
* Return: 0 on success, or a negative error code on failure.
|
||||
*/
|
||||
int tascam_init_pcm(struct snd_pcm *pcm);
|
||||
|
||||
/* from midi.c */
|
||||
int tascam_create_midi(struct tascam_card *tascam);
|
||||
void tascam_midi_in_work_handler(struct work_struct *work);
|
||||
void tascam_midi_out_work_handler(struct work_struct *work);
|
||||
/**
|
||||
* us144mkii_configure_device_for_rate() - Set sample rate via USB control msgs
|
||||
* @tascam: the tascam_card instance
|
||||
* @rate: the target sample rate (e.g., 44100, 96000)
|
||||
*
|
||||
* This function sends a sequence of vendor-specific and UAC control messages
|
||||
* to configure the device hardware for the specified sample rate.
|
||||
*
|
||||
* Return: 0 on success, or a negative error code on failure.
|
||||
*/
|
||||
int us144mkii_configure_device_for_rate(struct tascam_card *tascam, int rate);
|
||||
|
||||
/**
|
||||
* tascam_pcm_hw - Hardware capabilities for TASCAM US-144MKII PCM.
|
||||
*
|
||||
* Defines the supported PCM formats, rates, channels, and buffer/period sizes
|
||||
* for the TASCAM US-144MKII audio interface.
|
||||
*/
|
||||
extern const struct snd_pcm_hardware tascam_pcm_hw;
|
||||
|
||||
/* midi.c */
|
||||
/**
|
||||
* tascam_midi_in_urb_complete() - Completion handler for MIDI IN URBs
|
||||
* @urb: The completed URB.
|
||||
*
|
||||
* This function runs in interrupt context. It places the raw data from the
|
||||
* USB endpoint into a kfifo and schedules a work item to process it later,
|
||||
* ensuring the interrupt handler remains fast.
|
||||
*/
|
||||
void tascam_midi_in_urb_complete(struct urb *urb);
|
||||
|
||||
/**
|
||||
* tascam_midi_out_urb_complete() - Completion handler for MIDI OUT bulk URB.
|
||||
* @urb: The completed URB.
|
||||
*
|
||||
* This function runs in interrupt context. It marks the output URB as no
|
||||
* longer in-flight. It then re-schedules the work handler to check for and
|
||||
* send any more data waiting in the ALSA buffer. This is a safe, non-blocking
|
||||
* way to continue the data transmission chain.
|
||||
*/
|
||||
void tascam_midi_out_urb_complete(struct urb *urb);
|
||||
|
||||
/**
|
||||
* tascam_create_midi() - Create and initialize the ALSA rawmidi device.
|
||||
* @tascam: The driver instance.
|
||||
*
|
||||
* Return: 0 on success, or a negative error code on failure.
|
||||
*/
|
||||
int tascam_create_midi(struct tascam_card *tascam);
|
||||
|
||||
/* controls.c */
|
||||
/**
|
||||
* tascam_create_controls() - Creates and adds ALSA mixer controls for the device.
|
||||
* @tascam: The driver instance.
|
||||
*
|
||||
* This function registers custom ALSA controls for managing audio routing
|
||||
* (line out source, digital out source, capture 1-2 source, capture 3-4 source)
|
||||
* and displaying the current sample rate.
|
||||
*
|
||||
* Return: 0 on success, or a negative error code on failure.
|
||||
*/
|
||||
int tascam_create_controls(struct tascam_card *tascam);
|
||||
|
||||
#endif /* __US144MKII_H */
|
||||
|
|
|
|||
Loading…
Reference in New Issue