From bbc20c35ac6a17bf0c3b098e7e632698ce43a48e Mon Sep 17 00:00:00 2001 From: Mathias Nyman Date: Fri, 16 Oct 2026 01:46:09 +0300 Subject: [PATCH 01/53] xhci: sideband: avoid use-after-free of stale virt device pointer The sideband structure caches vdev, an xhci_virt_device pointer that the xhci driver may free and reallocate while the sideband is still registered, leaving the sideband with a dangling sb->vdev pointer. This can happen during resume, either when the xhci driver detects xHC power loss and resets the xHC, freeing all virt devices without informing class drivers, or during reset-resume when usb core re-enumerates the device and xhci frees and allocates a new virt device. Store the usb_device pointer (udev) instead. It stays valid across reset-resume and xHC reset. Look up the current virt device and ensure it still points to this sideband before using and dereferencing it in sideband API calls. Make sure xhci_sideband_unregister() works properly when called with an invalid vdev. Don't dereference it, or ask the xHC to stop non-existing endpoints, but make sure the sideband is properly freed. Fail other relevant sideband API calls that would access an invalid vdev. This solution isn't airtight. It only validates vdev when entering sideband API functions. It does not serialize the sideband API functions against the code in the xhci driver that reallocates vdev, so vdev may still be freed in the middle of a sideband call. It's still a big improvement and a first step in resolving this. The following patch starts working on the serialization. Fixes: de66754e9f80 ("xhci: sideband: add initial api to register a secondary interrupter entity") Signed-off-by: Mathias Nyman --- drivers/usb/host/xhci-sideband.c | 46 +++++++++++++++++++++---------- include/linux/usb/xhci-sideband.h | 4 +-- 2 files changed, 34 insertions(+), 16 deletions(-) diff --git a/drivers/usb/host/xhci-sideband.c b/drivers/usb/host/xhci-sideband.c index a5deeee4d5dce4..e4dd298b80f1ac 100644 --- a/drivers/usb/host/xhci-sideband.c +++ b/drivers/usb/host/xhci-sideband.c @@ -75,6 +75,22 @@ xhci_ring_to_sgtable(struct xhci_sideband *sb, struct xhci_ring *ring) return NULL; } +/* Return the current vdev of sb->udev, or NULL if it was freed or reallocated */ +static struct xhci_virt_device * +sideband_to_vdev(struct xhci_sideband *sb) +{ + struct xhci_virt_device *vdev; + + if (!sb->udev || !sb->udev->slot_id) + return NULL; + + vdev = sb->xhci->devs[sb->udev->slot_id]; + if (vdev && vdev->sideband == sb) + return vdev; + + return NULL; +} + /* Caller must hold sb->mutex */ static void __xhci_sideband_remove_endpoint(struct xhci_sideband *sb, struct xhci_virt_ep *ep) @@ -146,16 +162,18 @@ int xhci_sideband_add_endpoint(struct xhci_sideband *sb, struct usb_host_endpoint *host_ep) { + struct xhci_virt_device *vdev; struct xhci_virt_ep *ep; unsigned int ep_index; guard(mutex)(&sb->mutex); - if (!sb->vdev) + vdev = sideband_to_vdev(sb); + if (!vdev) return -ENODEV; ep_index = xhci_get_endpoint_index(&host_ep->desc); - ep = &sb->vdev->eps[ep_index]; + ep = &vdev->eps[ep_index]; if (ep->ep_state & EP_HAS_STREAMS) return -EINVAL; @@ -202,9 +220,10 @@ xhci_sideband_remove_endpoint(struct xhci_sideband *sb, ep_index = xhci_get_endpoint_index(&host_ep->desc); ep = sb->eps[ep_index]; - if (!ep || !ep->sideband || ep->sideband != sb) + if (!sideband_to_vdev(sb) || !ep || ep->sideband != sb) { + sb->eps[ep_index] = NULL; return -ENODEV; - + } __xhci_sideband_remove_endpoint(sb, ep); return 0; @@ -221,7 +240,7 @@ xhci_sideband_stop_endpoint(struct xhci_sideband *sb, ep_index = xhci_get_endpoint_index(&host_ep->desc); ep = sb->eps[ep_index]; - if (!ep || !ep->sideband || ep->sideband != sb) + if (!sideband_to_vdev(sb) || !ep || ep->sideband != sb) return -EINVAL; return xhci_stop_endpoint_sync(sb->xhci, ep, 0, GFP_KERNEL); @@ -251,7 +270,7 @@ xhci_sideband_get_endpoint_buffer(struct xhci_sideband *sb, ep_index = xhci_get_endpoint_index(&host_ep->desc); ep = sb->eps[ep_index]; - if (!ep || !ep->ring || !ep->sideband || ep->sideband != sb) + if (!sideband_to_vdev(sb) || !ep || !ep->ring || ep->sideband != sb) return NULL; return xhci_ring_to_sgtable(sb, ep->ring); @@ -329,7 +348,7 @@ xhci_sideband_create_interrupter(struct xhci_sideband *sb, int num_seg, guard(mutex)(&sb->mutex); - if (!sb->vdev) + if (!sideband_to_vdev(sb)) return -ENODEV; if (sb->ir) @@ -436,7 +455,7 @@ xhci_sideband_register(struct usb_interface *intf, enum xhci_sideband_type type, } sb->xhci = xhci; - sb->vdev = vdev; + sb->udev = udev; sb->intf = intf; sb->type = type; sb->notify_client = notify_client; @@ -471,22 +490,21 @@ xhci_sideband_unregister(struct xhci_sideband *sb) xhci = sb->xhci; scoped_guard(mutex, &sb->mutex) { - vdev = sb->vdev; - if (!vdev) - return; + vdev = sideband_to_vdev(sb); for (i = 0; i < EP_CTX_PER_DEV; i++) - if (sb->eps[i]) + if (vdev && sb->eps[i]) __xhci_sideband_remove_endpoint(sb, sb->eps[i]); __xhci_sideband_remove_interrupter(sb); - sb->vdev = NULL; + sb->udev = NULL; } spin_lock_irq(&xhci->lock); sb->xhci = NULL; - vdev->sideband = NULL; + if (vdev) + vdev->sideband = NULL; spin_unlock_irq(&xhci->lock); kfree(sb); diff --git a/include/linux/usb/xhci-sideband.h b/include/linux/usb/xhci-sideband.h index 005257085dcbd7..2d153cb5783120 100644 --- a/include/linux/usb/xhci-sideband.h +++ b/include/linux/usb/xhci-sideband.h @@ -39,7 +39,7 @@ struct xhci_sideband_event { /** * struct xhci_sideband - representation of a sideband accessed usb device. * @xhci: The xhci host controller the usb device is connected to - * @vdev: the usb device accessed via sideband + * @udev: the usb device accessed via sideband * @eps: array of endpoints controlled via sideband * @ir: event handling and buffer for sideband accessed device * @type: xHCI sideband type @@ -51,7 +51,7 @@ struct xhci_sideband_event { */ struct xhci_sideband { struct xhci_hcd *xhci; - struct xhci_virt_device *vdev; + struct usb_device *udev; struct xhci_virt_ep *eps[EP_CTX_PER_DEV]; struct xhci_interrupter *ir; enum xhci_sideband_type type; From cd721a54e937fb0134d9912b16aace19fb6482b4 Mon Sep 17 00:00:00 2001 From: Mathias Nyman Date: Wed, 18 Nov 2026 00:56:46 +0200 Subject: [PATCH 02/53] xhci: sideband: check virt device under xhci lock when stopping endpoints Start work to protect sideband API functions against the xhci driver freeing or reallocating the virt device (vdev) in the middle of an API call. Make the following sideband API calls hold xhci->lock while validating the vdev and its endpoint, and while queueing the stop endpoint command: xhci_sideband_unregister() xhci_sideband_stop_endpoint() xhci_sideband_remove_endpoint() Locking changes are still needed in several xhci 'core' driver vdev free paths before the above calls are fully serialized against vdev free. Cases involve xHC reset in resume, enumeration retry, and reset-resume. Move the stop endpoint code from xhci_stop_endpoint_sync() in xhci.c to a new sideband_stop_endpoint_sync() helper in xhci-sideband.c. xhci_stop_endpoint_sync() has no other users, so remove it. Also take sb->mutex in xhci_sideband_stop_endpoint(), and keep holding it in xhci_sideband_unregister() until the sideband is detached from the vdev. Fixes: de66754e9f80 ("xhci: sideband: add initial api to register a secondary interrupter entity") Signed-off-by: Mathias Nyman --- drivers/usb/host/xhci-sideband.c | 121 ++++++++++++++++++++++--------- drivers/usb/host/xhci.c | 55 -------------- drivers/usb/host/xhci.h | 2 - 3 files changed, 87 insertions(+), 91 deletions(-) diff --git a/drivers/usb/host/xhci-sideband.c b/drivers/usb/host/xhci-sideband.c index e4dd298b80f1ac..6f7712f950331a 100644 --- a/drivers/usb/host/xhci-sideband.c +++ b/drivers/usb/host/xhci-sideband.c @@ -91,20 +91,78 @@ sideband_to_vdev(struct xhci_sideband *sb) return NULL; } -/* Caller must hold sb->mutex */ -static void -__xhci_sideband_remove_endpoint(struct xhci_sideband *sb, struct xhci_virt_ep *ep) +/* + * Synchronous xHCI stop endpoint helper. Issues a stop endpoint command and + * waits for the command completion before returning. This does not call + * xhci_handle_cmd_stop_ep(), which has additional handling for 'context error' + * cases, along with transfer ring cleanup. + * + * Caller must hold sb->mutex + */ +static int +sideband_stop_endpoint_sync(struct xhci_sideband *sb, unsigned int ep_index, + bool remove) { + struct xhci_virt_device *vdev; + struct xhci_command *cmd; + struct xhci_virt_ep *ep; + struct xhci_ep_ctx *ctx; + unsigned long flags; + int ret = -ENODEV; + lockdep_assert_held(&sb->mutex); - /* - * Issue a stop endpoint command when an endpoint is removed. - * The stop ep cmd handler will handle the ring cleanup. - */ - xhci_stop_endpoint_sync(sb->xhci, ep, 0, GFP_KERNEL); + cmd = xhci_alloc_command(sb->xhci, true, GFP_KERNEL); + + spin_lock_irqsave(&sb->xhci->lock, flags); + + vdev = sideband_to_vdev(sb); + ep = sb->eps[ep_index]; + + /* validate that xhci didn't reallocate vdev during reset or resume */ + if (!vdev || !ep || ep->sideband != sb) + goto out_unlock; + + if (remove) { + ep->sideband = NULL; + /* endpoint is removed, not running is not an error */ + ret = 0; + } + + if (!ep->ring) + goto out_unlock; + + /* Stop the endpoint if it's running */ + ctx = xhci_get_ep_ctx(sb->xhci, vdev->out_ctx, ep_index); + if (GET_EP_CTX_STATE(ctx) == EP_STATE_RUNNING) { + if (!cmd) { + ret = -ENOMEM; + goto out_unlock; + } + ret = xhci_queue_stop_endpoint(sb->xhci, cmd, vdev->slot_id, + ep_index, 0); + if (ret) + goto out_unlock; + + xhci_ring_cmd_db(sb->xhci); + spin_unlock_irqrestore(&sb->xhci->lock, flags); + wait_for_completion(cmd->completion); + + if (cmd->status == COMP_COMMAND_ABORTED || + cmd->status == COMP_COMMAND_RING_STOPPED) { + xhci_warn(sb->xhci, "Timeout while waiting for stop endpoint command\n"); + ret = -ETIME; + } + xhci_free_command(sb->xhci, cmd); + return ret; + } - ep->sideband = NULL; - sb->eps[ep->ep_index] = NULL; +out_unlock: + spin_unlock_irqrestore(&sb->xhci->lock, flags); + if (cmd) + xhci_free_command(sb->xhci, cmd); + + return ret; } /* Caller must hold sb->mutex */ @@ -212,21 +270,17 @@ int xhci_sideband_remove_endpoint(struct xhci_sideband *sb, struct usb_host_endpoint *host_ep) { - struct xhci_virt_ep *ep; unsigned int ep_index; + int ret; guard(mutex)(&sb->mutex); ep_index = xhci_get_endpoint_index(&host_ep->desc); - ep = sb->eps[ep_index]; - if (!sideband_to_vdev(sb) || !ep || ep->sideband != sb) { - sb->eps[ep_index] = NULL; - return -ENODEV; - } - __xhci_sideband_remove_endpoint(sb, ep); + ret = sideband_stop_endpoint_sync(sb, ep_index, true); + sb->eps[ep_index] = NULL; - return 0; + return ret; } EXPORT_SYMBOL_GPL(xhci_sideband_remove_endpoint); @@ -234,16 +288,13 @@ int xhci_sideband_stop_endpoint(struct xhci_sideband *sb, struct usb_host_endpoint *host_ep) { - struct xhci_virt_ep *ep; unsigned int ep_index; - ep_index = xhci_get_endpoint_index(&host_ep->desc); - ep = sb->eps[ep_index]; + guard(mutex)(&sb->mutex); - if (!sideband_to_vdev(sb) || !ep || ep->sideband != sb) - return -EINVAL; + ep_index = xhci_get_endpoint_index(&host_ep->desc); - return xhci_stop_endpoint_sync(sb->xhci, ep, 0, GFP_KERNEL); + return sideband_stop_endpoint_sync(sb, ep_index, false); } EXPORT_SYMBOL_GPL(xhci_sideband_stop_endpoint); @@ -489,24 +540,26 @@ xhci_sideband_unregister(struct xhci_sideband *sb) xhci = sb->xhci; - scoped_guard(mutex, &sb->mutex) { - vdev = sideband_to_vdev(sb); - - for (i = 0; i < EP_CTX_PER_DEV; i++) - if (vdev && sb->eps[i]) - __xhci_sideband_remove_endpoint(sb, sb->eps[i]); + mutex_lock(&sb->mutex); - __xhci_sideband_remove_interrupter(sb); - - sb->udev = NULL; + for (i = 0; i < EP_CTX_PER_DEV; i++) { + if (sb->eps[i]) { + sideband_stop_endpoint_sync(sb, i, true); + sb->eps[i] = NULL; + } } + __xhci_sideband_remove_interrupter(sb); spin_lock_irq(&xhci->lock); - sb->xhci = NULL; + vdev = sideband_to_vdev(sb); if (vdev) vdev->sideband = NULL; + sb->udev = NULL; + sb->xhci = NULL; spin_unlock_irq(&xhci->lock); + mutex_unlock(&sb->mutex); + kfree(sb); } EXPORT_SYMBOL_GPL(xhci_sideband_unregister); diff --git a/drivers/usb/host/xhci.c b/drivers/usb/host/xhci.c index 30ac1bc4559b74..bcb482795bbc3a 100644 --- a/drivers/usb/host/xhci.c +++ b/drivers/usb/host/xhci.c @@ -2883,61 +2883,6 @@ static int xhci_reserve_bandwidth(struct xhci_hcd *xhci, return -ENOMEM; } -/* - * Synchronous XHCI stop endpoint helper. Issues the stop endpoint command and - * waits for the command completion before returning. This does not call - * xhci_handle_cmd_stop_ep(), which has additional handling for 'context error' - * cases, along with transfer ring cleanup. - * - * xhci_stop_endpoint_sync() is intended to be utilized by clients that manage - * their own transfer ring, such as offload situations. - */ -int xhci_stop_endpoint_sync(struct xhci_hcd *xhci, struct xhci_virt_ep *ep, int suspend, - gfp_t gfp_flags) -{ - struct xhci_command *command; - struct xhci_ep_ctx *ep_ctx; - unsigned long flags; - int ret = -ENODEV; - - command = xhci_alloc_command(xhci, true, gfp_flags); - if (!command) - return -ENOMEM; - - spin_lock_irqsave(&xhci->lock, flags); - - /* make sure endpoint exists and is running before stopping it */ - if (ep->ring) { - ep_ctx = xhci_get_ep_ctx(xhci, ep->vdev->out_ctx, ep->ep_index); - if (GET_EP_CTX_STATE(ep_ctx) == EP_STATE_RUNNING) - ret = xhci_queue_stop_endpoint(xhci, command, - ep->vdev->slot_id, - ep->ep_index, suspend); - } - - if (ret < 0) { - spin_unlock_irqrestore(&xhci->lock, flags); - goto out; - } - - xhci_ring_cmd_db(xhci); - spin_unlock_irqrestore(&xhci->lock, flags); - - wait_for_completion(command->completion); - - /* No handling for COMP_CONTEXT_STATE_ERROR done at command completion*/ - if (command->status == COMP_COMMAND_ABORTED || - command->status == COMP_COMMAND_RING_STOPPED) { - xhci_warn(xhci, "Timeout while waiting for stop endpoint command\n"); - ret = -ETIME; - } -out: - xhci_free_command(xhci, command); - - return ret; -} -EXPORT_SYMBOL_GPL(xhci_stop_endpoint_sync); - /* * xhci_usb_endpoint_maxp - get endpoint max packet size * @host_ep: USB host endpoint to be checked diff --git a/drivers/usb/host/xhci.h b/drivers/usb/host/xhci.h index ec4bfeb4887cbf..6201b6b242528c 100644 --- a/drivers/usb/host/xhci.h +++ b/drivers/usb/host/xhci.h @@ -1963,8 +1963,6 @@ void xhci_cleanup_command_queue(struct xhci_hcd *xhci); void inc_deq(struct xhci_hcd *xhci, struct xhci_ring *ring); unsigned int count_trbs(u64 addr, u64 len); unsigned int xhci_num_trbs_free(struct xhci_ring *ring); -int xhci_stop_endpoint_sync(struct xhci_hcd *xhci, struct xhci_virt_ep *ep, - int suspend, gfp_t gfp_flags); void xhci_process_cancelled_tds(struct xhci_hcd *xhci, struct xhci_virt_ep *ep); void xhci_update_erst_dequeue(struct xhci_hcd *xhci, struct xhci_interrupter *ir, From aea12576d00e76cbc738ae0f92608cd9e868c043 Mon Sep 17 00:00:00 2001 From: Mathias Nyman Date: Tue, 24 Nov 2026 23:35:46 +0200 Subject: [PATCH 03/53] xhci: sideband: check virt device under xhci lock when adding endpoints xhci_sideband_add_endpoint() looks up the virt device and endpoint, and assigns the endpoint to the sideband, without holding the xhci lock. The xhci driver may free or reallocate the virt device in the middle of this, for example during xHC reset in resume, reset-resume, or retried enumeration. Add a sideband_to_ep() helper, and look up, check and assign the endpoint while holding xhci->lock. Fixes: de66754e9f80 ("xhci: sideband: add initial api to register a secondary interrupter entity") Signed-off-by: Mathias Nyman --- drivers/usb/host/xhci-sideband.c | 52 ++++++++++++++++++++++++-------- 1 file changed, 39 insertions(+), 13 deletions(-) diff --git a/drivers/usb/host/xhci-sideband.c b/drivers/usb/host/xhci-sideband.c index 6f7712f950331a..d465ef746b297a 100644 --- a/drivers/usb/host/xhci-sideband.c +++ b/drivers/usb/host/xhci-sideband.c @@ -91,6 +91,25 @@ sideband_to_vdev(struct xhci_sideband *sb) return NULL; } +/* + * Return the current virt ep of sb->udev at ep_index, + * or NULL if vdev was freed or reallocated + */ +static struct xhci_virt_ep * +sideband_to_ep(struct xhci_sideband *sb, unsigned int ep_index) +{ + struct xhci_virt_device *vdev; + + lockdep_assert_held(&sb->mutex); + lockdep_assert_held(&sb->xhci->lock); + + vdev = sideband_to_vdev(sb); + if (!vdev) + return NULL; + + return &vdev->eps[ep_index]; +} + /* * Synchronous xHCI stop endpoint helper. Issues a stop endpoint command and * waits for the command completion before returning. This does not call @@ -220,21 +239,17 @@ int xhci_sideband_add_endpoint(struct xhci_sideband *sb, struct usb_host_endpoint *host_ep) { - struct xhci_virt_device *vdev; struct xhci_virt_ep *ep; unsigned int ep_index; + unsigned long flags; + int ret = 0; guard(mutex)(&sb->mutex); - vdev = sideband_to_vdev(sb); - if (!vdev) - return -ENODEV; - ep_index = xhci_get_endpoint_index(&host_ep->desc); - ep = &vdev->eps[ep_index]; - if (ep->ep_state & EP_HAS_STREAMS) - return -EINVAL; + if (sb->eps[ep_index]) + return -EBUSY; /* * Note, we don't know the DMA mask of the audio DSP device, if its @@ -244,13 +259,24 @@ xhci_sideband_add_endpoint(struct xhci_sideband *sb, * and let this function add the endpoint and allocate the ring buffer * with the smallest common DMA mask */ - if (sb->eps[ep_index] || ep->sideband) - return -EBUSY; + spin_lock_irqsave(&sb->xhci->lock, flags); - ep->sideband = sb; - sb->eps[ep_index] = ep; + ep = sideband_to_ep(sb, ep_index); + if (!ep) { + ret = -ENODEV; + } else if (ep->sideband) { + ret = -EBUSY; + } else if (ep->ep_state & EP_HAS_STREAMS) { + ret = -EINVAL; + } else { + /* success */ + ep->sideband = sb; + sb->eps[ep_index] = ep; + } - return 0; + spin_unlock_irqrestore(&sb->xhci->lock, flags); + + return ret; } EXPORT_SYMBOL_GPL(xhci_sideband_add_endpoint); From 9ace4f837d9cd334bd47a16e7309e4cbb1f805a0 Mon Sep 17 00:00:00 2001 From: Mathias Nyman Date: Wed, 12 Jun 2024 19:46:03 +0300 Subject: [PATCH 04/53] xhci: Add initial support for Extended Property commands Extended Property Get and Set commands are used to control and list extended properties. The Get/Set Extended Property Command references a specific Extended Capability using the Extended Capability Identifier (ECI). Some of these commands take a pointer to an Extended Property Context that the xHC will fill. Get Extended Property with ECI set to 0 will list all supported extended capabilities. Audio sideband control is currently the only supported extended capability. More details in the xHCI specification sections 6.4.3.16-17 and 7.9.1-2. Signed-off-by: Mathias Nyman --- drivers/usb/host/xhci-ring.c | 35 ++++++++++++++++++++++ drivers/usb/host/xhci.h | 58 +++++++++++++++++++++++++++++++++++- 2 files changed, 92 insertions(+), 1 deletion(-) diff --git a/drivers/usb/host/xhci-ring.c b/drivers/usb/host/xhci-ring.c index c23434001e9cbf..a26ad127ea6c66 100644 --- a/drivers/usb/host/xhci-ring.c +++ b/drivers/usb/host/xhci-ring.c @@ -1926,6 +1926,12 @@ static void handle_cmd_completion(struct xhci_hcd *xhci, break; case TRB_GET_BW: break; + case TRB_GET_EXT_PROP: + xhci_dbg(xhci, "Get Extended Property status 0x%08x\n", status); + break; + case TRB_SET_EXT_PROP: + xhci_dbg(xhci, "Set Extended Property status 0x%08x\n", status); + break; default: /* Skip over unknown commands on the event ring */ xhci_info(xhci, "INFO unknown command type %d\n", cmd_type); @@ -4494,3 +4500,32 @@ int xhci_queue_reset_ep(struct xhci_hcd *xhci, struct xhci_command *cmd, return queue_command(xhci, cmd, 0, 0, 0, trb_slot_id | trb_ep_index | type, false); } + +int xhci_queue_get_ext_property(struct xhci_hcd *xhci, struct xhci_command *cmd, + u32 slot_id, u32 ep_id, dma_addr_t ctx_dma, + u32 property, u32 subtype) +{ + u32 type = TRB_TYPE(TRB_GET_EXT_PROP) | SLOT_ID_FOR_TRB(slot_id) | + FIELD_PREP(TRB_PROP_EP_ID, ep_id) | + FIELD_PREP(TRB_PROP_SUBTYPE, subtype); + + return queue_command(xhci, cmd, lower_32_bits(ctx_dma), + upper_32_bits(ctx_dma), + FIELD_PREP(TRB_PROP_ECI, property), type, false); +} + +int xhci_queue_set_ext_property(struct xhci_hcd *xhci, struct xhci_command *cmd, + u32 slot_id, u32 ep_id, dma_addr_t ctx_dma, + u32 property, u32 subtype, u32 param) +{ + u32 type = TRB_TYPE(TRB_SET_EXT_PROP) | SLOT_ID_FOR_TRB(slot_id) | + FIELD_PREP(TRB_PROP_EP_ID, ep_id) | + FIELD_PREP(TRB_PROP_SUBTYPE, subtype); + u32 cap_param; + + cap_param = FIELD_PREP(TRB_PROP_ECPARAM, param) | + FIELD_PREP(TRB_PROP_ECI, property); + + return queue_command(xhci, cmd, lower_32_bits(ctx_dma), + upper_32_bits(ctx_dma), cap_param, type, false); +} diff --git a/drivers/usb/host/xhci.h b/drivers/usb/host/xhci.h index 6201b6b242528c..037cc5bfdda0af 100644 --- a/drivers/usb/host/xhci.h +++ b/drivers/usb/host/xhci.h @@ -532,6 +532,36 @@ struct xhci_input_control_ctx { #define EP_IS_DROPPED(ctrl_ctx, i) \ (le32_to_cpu(ctrl_ctx->drop_flags) & (1 << (i + 1))) +/* + * xhci_extprop_ctx - union of extended property context structures + * + * Used with several different types of Get Extended Property commands. See: + * 7.9.1.1 Get Extended Property: Get Supported Resources Command + * 7.9.1.2 Get Extended Property: Get Endpoint Properties Command + * 7.10.3 Get Extended Property: Get Time Stamp Command + * + * Layout varies, but size is so far always 16 bytes. + * Define one extended property context for generic use. + * Define one extended property context for audio sideband extended capability + * endpoint properties + */ +struct xhci_generic_extprop_ctx { + u8 byte[16]; +} __packed; + +struct xhci_audio_ep_extprop_ctx { + u8 rh_port; + u8 res_id; + u8 port_speed_tt; + u8 rsvdz; + __le32 rsvdz2[3]; +} __packed; + +union xhci_extprop_ctx { + struct xhci_generic_extprop_ctx generic; + struct xhci_audio_ep_extprop_ctx audio_ep; +}; + /* Represents everything that is needed to issue a command on the command ring. * It's useful to pre-allocate these for commands that cannot fail due to * out-of-memory errors, like freeing streams. @@ -1022,6 +1052,22 @@ enum xhci_setup_dev { #define STREAM_ID_FOR_TRB(p) ((((p)) & 0xffff) << 16) #define SCT_FOR_TRB(p) (((p) & 0x7) << 1) +/* Get/Set Extended Property command TRB fields */ +#define TRB_PROP_ECI GENMASK(15, 0) +#define TRB_PROP_ECPARAM GENMASK(23, 16) + +#define TRB_PROP_SUBTYPE GENMASK(18, 16) +#define TRB_PROP_EP_ID GENMASK(23, 19) + +#define PROP_ECI_ALL 0 /* use extended capability 0 to list capabilities */ +#define PROP_ECI_AUDIO BIT(0) /* Audio sideband extended capability identifier */ + +/* Get/Set Extended Property Audio sideband subtypes */ +#define AUDIO_SUBTYPE_GET_RES 1 /* Get supported resources */ +#define AUDIO_SUBTYPE_GET_EP_PROP 2 /* Get endpoint properties */ + +#define AUDIO_SUBTYPE_SET_RES 1 /* Set resource assignment */ + /* Link TRB specific fields */ #define TRB_TC BIT(1) @@ -1148,7 +1194,11 @@ union xhci_trb { #define TRB_FORCE_HEADER 22 /* No-op Command - not for transfer rings */ #define TRB_CMD_NOOP 23 -/* TRB IDs 24-31 reserved */ +/* Get Extended Property Command (opt) */ +#define TRB_GET_EXT_PROP 24 +/* Set Extended Property Command (opt) */ +#define TRB_SET_EXT_PROP 25 +/* TRB IDs 26-31 reserved */ /* Event TRBS */ /* Transfer Event */ #define TRB_TRANSFER 32 @@ -1952,6 +2002,12 @@ int xhci_queue_reset_ep(struct xhci_hcd *xhci, struct xhci_command *cmd, enum xhci_ep_reset_type reset_type); int xhci_queue_reset_device(struct xhci_hcd *xhci, struct xhci_command *cmd, u32 slot_id); +int xhci_queue_get_ext_property(struct xhci_hcd *xhci, struct xhci_command *cmd, + u32 slot_id, u32 ep_id, dma_addr_t ctx_dma, u32 property, + u32 subtype); +int xhci_queue_set_ext_property(struct xhci_hcd *xhci, struct xhci_command *cmd, + u32 slot_id, u32 ep_id, dma_addr_t ctx_dma, u32 property, + u32 subtype, u32 param); void xhci_handle_command_timeout(struct work_struct *work); void xhci_ring_ep_doorbell(struct xhci_hcd *xhci, unsigned int slot_id, From 91a8f84fff520246ac9187abc2d32b05b65d17a1 Mon Sep 17 00:00:00 2001 From: Mathias Nyman Date: Mon, 16 Dec 2024 17:18:30 +0200 Subject: [PATCH 05/53] xhci: add Get Extended Property synchronous helper Add an easy to use synchronous helper for Get Extended Property. It takes care of the tedious tasks of allocating a command, DMA mapping, queueing a command and waiting for completion. The helper is synchronous and will sleep. Note that the buffer *buf passed to xhci_get_ext_property() needs to be DMA mappable for all buffers using capability IDs, except for PROP_ECI_ALL (ECI 0) which returns a bitmap of supported capabilities in the command completion event TRB. Signed-off-by: Mathias Nyman --- drivers/usb/host/xhci.c | 89 +++++++++++++++++++++++++++++++++++++++++ drivers/usb/host/xhci.h | 2 + 2 files changed, 91 insertions(+) diff --git a/drivers/usb/host/xhci.c b/drivers/usb/host/xhci.c index bcb482795bbc3a..d48ab6bd5681e5 100644 --- a/drivers/usb/host/xhci.c +++ b/drivers/usb/host/xhci.c @@ -5208,6 +5208,95 @@ static int xhci_disable_usb3_lpm_timeout(struct usb_hcd *hcd, } #endif /* CONFIG_PM */ +/** + * xhci_get_ext_property - issue a Get Extended Property command and wait + * @xhci: xHCI host controller + * @slot_id: slot ID of the device, or 0 for device independent properties + * @ep_id: endpoint id (DCI), only used if @slot_id is set. + * @property: Extended Capability Identifier (ECI), PROP_ECI_ALL to list + * supported extended capabilities + * @subtype: command subtype, specific to the extended capability + * @buf: buffer for the returned Extended Property Context + * @buf_len: size of @buf + * + * For PROP_ECI_ALL the xHC returns a 16 bit bitmap of supported extended + * capabilities in the command completion event, which is copied to @buf. + * For all other properties the xHC writes the Extended Property Context + * directly to @buf using DMA, so @buf must be DMA-able memory, i.e. not on + * the stack or vmalloc'ed. + * + * Context: Process context. Sleeps until the command completes. + * + * Return: 0 on success, negative error code otherwise. + */ +int xhci_get_ext_property(struct xhci_hcd *xhci, u32 slot_id, u32 ep_id, + u32 property, u32 subtype, void *buf, size_t buf_len) +{ + struct device *dev = xhci_to_hcd(xhci)->self.sysdev; + struct xhci_command *cmd; + dma_addr_t dma = 0; + unsigned long flags; + int ret; + + if (!buf) + return -EINVAL; + + /* PROP_ECI_ALL xHC does not write to buf, data is in event TRB */ + if (property == PROP_ECI_ALL && (buf_len < sizeof(u16))) + return -EINVAL; + + cmd = xhci_alloc_command(xhci, true, GFP_KERNEL); + if (!cmd) + return -ENOMEM; + + /* xHC writes the Extended Property Context to buf for other properties */ + if (property != PROP_ECI_ALL) { + dma = dma_map_single(dev, buf, buf_len, DMA_FROM_DEVICE); + if (dma_mapping_error(dev, dma)) { + ret = -ENOMEM; + goto free_cmd; + } + } + + spin_lock_irqsave(&xhci->lock, flags); + + if (slot_id && !xhci->devs[slot_id]) { + spin_unlock_irqrestore(&xhci->lock, flags); + ret = -ENODEV; + goto unmap; + } + + ret = xhci_queue_get_ext_property(xhci, cmd, slot_id, ep_id, dma, + property, subtype); + if (ret) { + spin_unlock_irqrestore(&xhci->lock, flags); + goto unmap; + } + + xhci_ring_cmd_db(xhci); + spin_unlock_irqrestore(&xhci->lock, flags); + + wait_for_completion(cmd->completion); + + switch (cmd->status) { + case COMP_SUCCESS: + /* cmd->comp_param is u32, PROP_ECI_ALL only uses bits 15:0 */ + if (property == PROP_ECI_ALL) + *(u16 *)buf = cmd->comp_param; + break; + default: + xhci_dbg(xhci, "Get ext property command failed %u\n", cmd->status); + ret = -EIO; + } +unmap: + if (property != PROP_ECI_ALL) + dma_unmap_single(dev, dma, buf_len, DMA_FROM_DEVICE); +free_cmd: + xhci_free_command(xhci, cmd); + + return ret; +} + /*-------------------------------------------------------------------------*/ /* Once a hub descriptor is fetched for a device, we need to update the xHC's diff --git a/drivers/usb/host/xhci.h b/drivers/usb/host/xhci.h index 037cc5bfdda0af..0d8bb1fb36f728 100644 --- a/drivers/usb/host/xhci.h +++ b/drivers/usb/host/xhci.h @@ -2008,6 +2008,8 @@ int xhci_queue_get_ext_property(struct xhci_hcd *xhci, struct xhci_command *cmd, int xhci_queue_set_ext_property(struct xhci_hcd *xhci, struct xhci_command *cmd, u32 slot_id, u32 ep_id, dma_addr_t ctx_dma, u32 property, u32 subtype, u32 param); +int xhci_get_ext_property(struct xhci_hcd *xhci, u32 slot_id, u32 ep_id, + u32 property, u32 subtype, void *buf, size_t buf_len); void xhci_handle_command_timeout(struct work_struct *work); void xhci_ring_ep_doorbell(struct xhci_hcd *xhci, unsigned int slot_id, From 7ae11a744598bf1a9137c119fdf3da47735e7ece Mon Sep 17 00:00:00 2001 From: Mathias Nyman Date: Fri, 13 Dec 2024 18:06:57 +0200 Subject: [PATCH 06/53] xhci: sideband: support registering xHC controlled audio sideband Add support for registering standard xHCI Audio Sideband Capability, as described in xHCI specification section 7.9. This is the xHC hardware managed audio sideband that can be controlled by Set and Get Extended Property commands. Define the type of this xHC hardware controlled audio sideband as XHCI_SIDEBAND_AUDIO. Check that xHCI is at least version 1.2 with the GSC support bit (Get/Set Extended Property Capability) set, and that Get Extended Property reports that the Audio Sideband Extended Capability is supported. Check if the sideband type is XHCI_SIDEBAND_AUDIO or XHCI_SIDEBAND_VENDOR in code paths where it matters. Signed-off-by: Mathias Nyman --- drivers/usb/host/xhci-sideband.c | 81 +++++++++++++++++++++++++------ include/linux/usb/xhci-sideband.h | 2 - 2 files changed, 66 insertions(+), 17 deletions(-) diff --git a/drivers/usb/host/xhci-sideband.c b/drivers/usb/host/xhci-sideband.c index d465ef746b297a..6fcacad1efd373 100644 --- a/drivers/usb/host/xhci-sideband.c +++ b/drivers/usb/host/xhci-sideband.c @@ -14,6 +14,28 @@ #include "xhci.h" /* sideband internal helpers */ + +static bool +xhci_sideband_hw_capable(struct xhci_hcd *xhci) +{ + u16 buf = 0; + int ret; + + /* xHCI 1.2 and later can support GSC bit in hcc_params2 register */ + if ((xhci->hci_version < 0x120) || !(xhci->hcc_params2 & HCC2_GSC)) + return false; + /* + * Note, normally xHC hardware writes directly to buf, so buf needs to + * be DMA mappable. PROP_ECI_ALL is special and returns 16 bits data in + * the command completion event, which is then copied to buf. + */ + ret = xhci_get_ext_property(xhci, 0, 0, PROP_ECI_ALL, 0, &buf, sizeof(buf)); + if (ret >= 0 && buf & PROP_ECI_AUDIO) + return true; + + return false; +} + static struct sg_table * xhci_ring_to_sgtable(struct xhci_sideband *sb, struct xhci_ring *ring) { @@ -184,6 +206,22 @@ sideband_stop_endpoint_sync(struct xhci_sideband *sb, unsigned int ep_index, return ret; } +/* Caller must hold sb->mutex */ +static int +__xhci_sideband_remove_endpoint(struct xhci_sideband *sb, unsigned int ep_index) +{ + int ret = 0; + + lockdep_assert_held(&sb->mutex); + + if (sb->type == XHCI_SIDEBAND_VENDOR) + ret = sideband_stop_endpoint_sync(sb, ep_index, true); + + sb->eps[ep_index] = NULL; + + return ret; +} + /* Caller must hold sb->mutex */ static void __xhci_sideband_remove_interrupter(struct xhci_sideband *sb) @@ -251,6 +289,9 @@ xhci_sideband_add_endpoint(struct xhci_sideband *sb, if (sb->eps[ep_index]) return -EBUSY; + if (sb->type == XHCI_SIDEBAND_AUDIO) + return -EINVAL; + /* * Note, we don't know the DMA mask of the audio DSP device, if its * smaller than for xhci it won't be able to access the endpoint ring @@ -297,16 +338,12 @@ xhci_sideband_remove_endpoint(struct xhci_sideband *sb, struct usb_host_endpoint *host_ep) { unsigned int ep_index; - int ret; guard(mutex)(&sb->mutex); ep_index = xhci_get_endpoint_index(&host_ep->desc); - ret = sideband_stop_endpoint_sync(sb, ep_index, true); - sb->eps[ep_index] = NULL; - - return ret; + return __xhci_sideband_remove_endpoint(sb, ep_index); } EXPORT_SYMBOL_GPL(xhci_sideband_remove_endpoint); @@ -504,14 +541,23 @@ xhci_sideband_register(struct usb_interface *intf, enum xhci_sideband_type type, struct xhci_virt_device *vdev; struct xhci_sideband *sb; - /* - * Make sure the usb device is connected to a xhci controller. Fail - * registration if the type is anything other than XHCI_SIDEBAND_VENDOR, - * as this is the only type that is currently supported by xhci-sideband. - */ - if (!udev->slot_id || type != XHCI_SIDEBAND_VENDOR) + /* Make sure the usb device is connected to a xhci controller. */ + if (!udev->slot_id) return NULL; + switch (type) { + case XHCI_SIDEBAND_VENDOR: + break; + case XHCI_SIDEBAND_AUDIO: + if (!xhci_sideband_hw_capable(xhci)) { + dev_info(&udev->dev, "Host not sideband capable\n"); + return NULL; + } + break; + default: + return NULL; + } + sb = kzalloc_node(sizeof(*sb), GFP_KERNEL, dev_to_node(hcd->self.sysdev)); if (!sb) return NULL; @@ -540,6 +586,8 @@ xhci_sideband_register(struct usb_interface *intf, enum xhci_sideband_type type, spin_unlock_irq(&xhci->lock); + dev_dbg(&udev->dev, "Registered for sideband use\n"); + return sb; } EXPORT_SYMBOL_GPL(xhci_sideband_register); @@ -558,6 +606,7 @@ void xhci_sideband_unregister(struct xhci_sideband *sb) { struct xhci_virt_device *vdev; + struct usb_device *udev; struct xhci_hcd *xhci; int i; @@ -565,15 +614,15 @@ xhci_sideband_unregister(struct xhci_sideband *sb) return; xhci = sb->xhci; + udev = sb->udev; mutex_lock(&sb->mutex); for (i = 0; i < EP_CTX_PER_DEV; i++) { - if (sb->eps[i]) { - sideband_stop_endpoint_sync(sb, i, true); - sb->eps[i] = NULL; - } + if (sb->eps[i]) + __xhci_sideband_remove_endpoint(sb, i); } + __xhci_sideband_remove_interrupter(sb); spin_lock_irq(&xhci->lock); @@ -586,6 +635,8 @@ xhci_sideband_unregister(struct xhci_sideband *sb) mutex_unlock(&sb->mutex); + dev_dbg(&udev->dev, "sideband unregistered\n"); + kfree(sb); } EXPORT_SYMBOL_GPL(xhci_sideband_unregister); diff --git a/include/linux/usb/xhci-sideband.h b/include/linux/usb/xhci-sideband.h index 2d153cb5783120..882eee9018862c 100644 --- a/include/linux/usb/xhci-sideband.h +++ b/include/linux/usb/xhci-sideband.h @@ -46,8 +46,6 @@ struct xhci_sideband_event { * @mutex: mutex for sideband operations * @intf: USB sideband client interface * @notify_client: callback for xHCI sideband sequences - * - * FIXME usb device accessed via sideband Keeping track of sideband accessed usb devices. */ struct xhci_sideband { struct xhci_hcd *xhci; From 54a8399d2a2bab15fa621c5da82a92292393b84f Mon Sep 17 00:00:00 2001 From: Mathias Nyman Date: Thu, 19 Dec 2024 12:04:05 +0200 Subject: [PATCH 07/53] xhci: sideband: reserve and track sideband endpoint use with resource IDs Endpoints controlled by the xHC hardware audio sideband need a unique resource ID, used when handing the endpoint over to the sideband with a Set Extended Property: Set Resource Assignment command (xHCI 7.9.2.1). Track the resource IDs of an xHC host in an xarray, and add xhci_sideband_alloc_resource_id() and xhci_sideband_free_resource_id() to the sideband API. An XHCI_SIDEBAND_AUDIO endpoint needs a reserved resource ID before it can be added with xhci_sideband_add_endpoint(). The number of resource IDs an xHC supports is fetched with a Get Extended Property: Get Supported Resources Command, and returned in the first 8 bit 'Resource count' field of the Extended Property Context. Store and use this limit as the max xarray index. Intel xHCs have a hard resource limit of 6, even if the host reports otherwise. The caller can narrow the ID range within the xHC limits, as an offload engine may tie a function to specific resources. For example, the Intel DSP firmware selects a link FIFO stream by resource ID. Passing 0 for both bounds reserves any free ID. An endpoint holds only one reservation at a time. Co-developed-by: Peter Ujfalusi Signed-off-by: Peter Ujfalusi Signed-off-by: Mathias Nyman --- drivers/usb/host/xhci-mem.c | 4 + drivers/usb/host/xhci-sideband.c | 223 +++++++++++++++++++++++++++++- drivers/usb/host/xhci.h | 8 +- include/linux/usb/xhci-sideband.h | 9 ++ 4 files changed, 242 insertions(+), 2 deletions(-) diff --git a/drivers/usb/host/xhci-mem.c b/drivers/usb/host/xhci-mem.c index af8d4b74c4ba0c..fce5fefdfed0e8 100644 --- a/drivers/usb/host/xhci-mem.c +++ b/drivers/usb/host/xhci-mem.c @@ -1933,6 +1933,8 @@ void xhci_mem_cleanup(struct xhci_hcd *xhci) cancel_delayed_work_sync(&xhci->cmd_timer); + xa_destroy(&xhci->sideband_idr.id_xa); + for (i = 0; xhci->interrupters && i < xhci->max_interrupters; i++) { if (xhci->interrupters[i]) { xhci_remove_interrupter(xhci, xhci->interrupters[i]); @@ -2416,6 +2418,8 @@ int xhci_mem_init(struct xhci_hcd *xhci, gfp_t flags) xhci_dbg_trace(xhci, trace_xhci_dbg_init, "Starting %s", __func__); + xa_init_flags(&xhci->sideband_idr.id_xa, XA_FLAGS_ALLOC); + xhci_dbg_trace(xhci, trace_xhci_dbg_init, "Allocating internal virtual device array"); xhci->devs = kcalloc_node(xhci->max_slots + 1, sizeof(*xhci->devs), flags, dev_to_node(dev)); diff --git a/drivers/usb/host/xhci-sideband.c b/drivers/usb/host/xhci-sideband.c index 6fcacad1efd373..5bccbf34e3c285 100644 --- a/drivers/usb/host/xhci-sideband.c +++ b/drivers/usb/host/xhci-sideband.c @@ -14,13 +14,61 @@ #include "xhci.h" /* sideband internal helpers */ +static int +sideband_res_id_to_ep_index(struct xhci_sideband *sb, unsigned int res_id) +{ + int i; + + if (res_id == 0) + return -EINVAL; + + for (i = 0; i < EP_CTX_PER_DEV; i++) { + if (sb->eps_res_id[i] == res_id) + return i; + } + + return -ENODEV; +} + +static int +xhci_sideband_hw_max_resources(struct xhci_hcd *xhci) +{ + union xhci_extprop_ctx *ctx; + unsigned int res_max; + int ret; + + ctx = kzalloc_obj(*ctx, GFP_KERNEL); + if (!ctx) + return -ENOMEM; + + ret = xhci_get_ext_property(xhci, 0, 0, PROP_ECI_AUDIO, + AUDIO_SUBTYPE_GET_RES, ctx, sizeof(*ctx)); + if (ret < 0) { + kfree(ctx); + return ret; + } + + res_max = ctx->generic.byte[0]; + kfree(ctx); + + /* Intel hosts sideband resources are capped at 6 */ + if (xhci->quirks & XHCI_INTEL_HOST) + res_max = min(res_max, 6); + + return res_max; +} static bool xhci_sideband_hw_capable(struct xhci_hcd *xhci) { u16 buf = 0; + int res_max; int ret; + /* fastpath, all checks are done if resource limits are set */ + if (xhci->sideband_idr.id_lim.max > 0) + return true; + /* xHCI 1.2 and later can support GSC bit in hcc_params2 register */ if ((xhci->hci_version < 0x120) || !(xhci->hcc_params2 & HCC2_GSC)) return false; @@ -30,8 +78,16 @@ xhci_sideband_hw_capable(struct xhci_hcd *xhci) * the command completion event, which is then copied to buf. */ ret = xhci_get_ext_property(xhci, 0, 0, PROP_ECI_ALL, 0, &buf, sizeof(buf)); - if (ret >= 0 && buf & PROP_ECI_AUDIO) + if (ret < 0 || !(buf & PROP_ECI_AUDIO)) + return false; + + /* check xHC has at least one resource for endpoint sideband use */ + res_max = xhci_sideband_hw_max_resources(xhci); + if (res_max > 0) { + xhci->sideband_idr.id_lim.min = 1; + xhci->sideband_idr.id_lim.max = res_max; return true; + } return false; } @@ -113,6 +169,23 @@ sideband_to_vdev(struct xhci_sideband *sb) return NULL; } +static bool +xhci_sideband_endpoint_belongs(struct xhci_sideband *sb, + struct usb_host_endpoint *host_ep) +{ + unsigned int epnum; + + if (!sb || !host_ep) + return false; + + epnum = usb_endpoint_num(&host_ep->desc); + + if (usb_endpoint_dir_in(&host_ep->desc)) + return sb->udev->ep_in[epnum] == host_ep; + + return sb->udev->ep_out[epnum] == host_ep; +} + /* * Return the current virt ep of sb->udev at ep_index, * or NULL if vdev was freed or reallocated @@ -237,6 +310,140 @@ __xhci_sideband_remove_interrupter(struct xhci_sideband *sb) /* sideband api functions */ +/** + * xhci_sideband_alloc_resource_id - reserve a resource id to offload an endpoint + * @sb: sideband instance for this usb device + * @host_ep: usb host endpoint + * @min: lowest resource id acceptable to the caller, 0 for no preference + * @max: highest resource id acceptable to the caller, 0 for no preference + * + * Reserves a sideband resource id for this endpoint from the limited set of + * xHC resource ids. Only valid for XHCI_SIDEBAND_AUDIO sideband types. + * Once an endpoint has a valid resource id then it can be assigned to audio + * sideband by calling xhci_sideband_add_endpoint() + * + * The xHC limits which resources an endpoint of a direction may use. An + * offload engine which ties a function to particular resources narrows that + * further with @min and @max, which have to lie within the xHC's limits for + * the endpoint; the first free resource of the intersection is reserved. + * With both 0 the first free resource the xHC allows is reserved. + * + * An endpoint holds one reservation at a time; it has to be released with + * xhci_sideband_free_resource_id() before another one can be made. + * + * Returns: positive resource id on success, -EBUSY if all resources of the + * range are taken or the endpoint already holds a reservation, negative + * error otherwise. + */ +int +xhci_sideband_alloc_resource_id(struct xhci_sideband *sb, + struct usb_host_endpoint *host_ep, + unsigned int min, unsigned int max) +{ + struct xhci_sideband_idr *res; + struct xa_limit lim; + unsigned int ep_index; + unsigned int res_id; + int ret; + + if (!xhci_sideband_endpoint_belongs(sb, host_ep)) + return -EINVAL; + + guard(mutex)(&sb->mutex); + + if (sb->type != XHCI_SIDEBAND_AUDIO) + return -EINVAL; + + ep_index = xhci_get_endpoint_index(&host_ep->desc); + res = &sb->xhci->sideband_idr; + lim = res->id_lim; + + /* + * Intel xHC hardware limits resource IDs 1 and 2 to output endpoints. + * IDs 3-6 are for input endpoints + */ + if (sb->xhci->quirks & XHCI_INTEL_HOST) { + if (usb_endpoint_dir_out(&host_ep->desc)) + lim.max = min(lim.max, 2); + else + lim.min = 3; + } + + if (min) { + if (min < lim.min || min > lim.max) + return -EINVAL; + lim.min = min; + } + + if (max) { + if (max < lim.min || max > lim.max) + return -EINVAL; + lim.max = max; + } + + if (sb->eps_res_id[ep_index]) { + dev_dbg(&sb->udev->dev, "ep %u already holds resource id %u\n", + ep_index, sb->eps_res_id[ep_index]); + return -EBUSY; + } + + ret = xa_alloc(&res->id_xa, &res_id, sb, lim, GFP_KERNEL); + if (ret) + return ret; + + sb->eps_res_id[ep_index] = res_id; + + dev_dbg(&sb->udev->dev, "allocated sideband resource %u for ep %u\n", + res_id, ep_index); + + return res_id; +} +EXPORT_SYMBOL_GPL(xhci_sideband_alloc_resource_id); + +/** + * xhci_sideband_free_resource_id - release a resource id + * @sb: sideband instance for this usb device + * @res_id: resource id to free + * + * Free a previously reserved sideband resource id, making it available for + * endpoints. Don't release the resource id if the endpoint is in active + * sideband use. + * + * Returns: 0 on success, negative error otherwise. + */ +int +xhci_sideband_free_resource_id(struct xhci_sideband *sb, + unsigned int res_id) +{ + struct xhci_sideband_idr *res; + int ep_index; + + if (!sb) + return -EINVAL; + + res = &sb->xhci->sideband_idr; + + guard(mutex)(&sb->mutex); + + ep_index = sideband_res_id_to_ep_index(sb, res_id); + if (ep_index < 0) + return -ENODEV; + + if (sb != xa_load(&res->id_xa, res_id)) + return -ENODEV; + if (sb->eps[ep_index]) + return -EBUSY; + + xa_erase(&res->id_xa, res_id); + sb->eps_res_id[ep_index] = 0; + + dev_dbg(&sb->udev->dev, "erase ep %u resource id %u\n", + ep_index, res_id); + + return 0; +} +EXPORT_SYMBOL_GPL(xhci_sideband_free_resource_id); + /** * xhci_sideband_notify_ep_ring_free - notify client of xfer ring free * @sb: sideband instance for this usb device @@ -284,6 +491,9 @@ xhci_sideband_add_endpoint(struct xhci_sideband *sb, guard(mutex)(&sb->mutex); + if (!xhci_sideband_endpoint_belongs(sb, host_ep)) + return -EINVAL; + ep_index = xhci_get_endpoint_index(&host_ep->desc); if (sb->eps[ep_index]) @@ -608,6 +818,7 @@ xhci_sideband_unregister(struct xhci_sideband *sb) struct xhci_virt_device *vdev; struct usb_device *udev; struct xhci_hcd *xhci; + int res_id; int i; if (!sb) @@ -621,6 +832,16 @@ xhci_sideband_unregister(struct xhci_sideband *sb) for (i = 0; i < EP_CTX_PER_DEV; i++) { if (sb->eps[i]) __xhci_sideband_remove_endpoint(sb, i); + + if (sb->type != XHCI_SIDEBAND_AUDIO) + continue; + + res_id = sb->eps_res_id[i]; + if (res_id && sb == xa_load(&xhci->sideband_idr.id_xa, res_id)) { + dev_dbg(&udev->dev, "%s erase ep %u res_id %u\n", + __func__, i, res_id); + xa_erase(&xhci->sideband_idr.id_xa, res_id); + } } __xhci_sideband_remove_interrupter(sb); diff --git a/drivers/usb/host/xhci.h b/drivers/usb/host/xhci.h index 0d8bb1fb36f728..b1c421c1bbd4aa 100644 --- a/drivers/usb/host/xhci.h +++ b/drivers/usb/host/xhci.h @@ -1554,6 +1554,11 @@ struct xhci_hub { u8 min_rev; }; +struct xhci_sideband_idr { + struct xarray id_xa; + struct xa_limit id_lim; +}; + /* There is one xhci_hcd structure per controller */ struct xhci_hcd { struct usb_hcd *main_hcd; @@ -1727,7 +1732,8 @@ struct xhci_hcd { struct dentry *debugfs_root; struct dentry *debugfs_slots; struct list_head regset_list; - + /* Sideband controlled endpoint resource ids, xhci 7.9.2.1 */ + struct xhci_sideband_idr sideband_idr; void *dbc; /* platform-specific data -- must come last */ unsigned long priv[] __aligned(sizeof(s64)); diff --git a/include/linux/usb/xhci-sideband.h b/include/linux/usb/xhci-sideband.h index 882eee9018862c..82b3ee10197cb6 100644 --- a/include/linux/usb/xhci-sideband.h +++ b/include/linux/usb/xhci-sideband.h @@ -41,6 +41,7 @@ struct xhci_sideband_event { * @xhci: The xhci host controller the usb device is connected to * @udev: the usb device accessed via sideband * @eps: array of endpoints controlled via sideband + * @eps_res_id: array of resource ids for endpoints * @ir: event handling and buffer for sideband accessed device * @type: xHCI sideband type * @mutex: mutex for sideband operations @@ -51,6 +52,7 @@ struct xhci_sideband { struct xhci_hcd *xhci; struct usb_device *udev; struct xhci_virt_ep *eps[EP_CTX_PER_DEV]; + unsigned int eps_res_id[EP_CTX_PER_DEV]; struct xhci_interrupter *ir; enum xhci_sideband_type type; @@ -97,6 +99,13 @@ void xhci_sideband_remove_interrupter(struct xhci_sideband *sb); int xhci_sideband_interrupter_id(struct xhci_sideband *sb); +int +xhci_sideband_alloc_resource_id(struct xhci_sideband *sb, + struct usb_host_endpoint *host_ep, + unsigned int min, unsigned int max); +int +xhci_sideband_free_resource_id(struct xhci_sideband *sb, + unsigned int res_id); #if IS_ENABLED(CONFIG_USB_XHCI_SIDEBAND) void xhci_sideband_notify_ep_ring_free(struct xhci_sideband *sb, From 246edf99c36533f3324dd110a69675333a2710b2 Mon Sep 17 00:00:00 2001 From: Mathias Nyman Date: Fri, 13 Dec 2024 18:46:04 +0200 Subject: [PATCH 08/53] xhci: sideband: support handing over endpoints to hardware controlled audio sideband Support handing endpoints over to xHC sideband control for the hardware controlled XHCI_SIDEBAND_AUDIO sideband type. These endpoints need to have a valid unique resource ID allocated by calling xhci_sideband_alloc_resource_id() before handing the endpoint over to the sideband by calling xhci_sideband_add_endpoint(). Endpoint control is handed over to the sideband with a Set Extended Property command that includes the allocated unique resource ID. See xHCI specification 7.9.2.1 "Set Extended Property: Set Resource Assignment Command". Add a synchronous Set Resource Assignment helper to xhci-sideband.c. It validates the virt device and endpoint under the same xhci->lock that queues the command, and only sets or clears ep->sideband once the command completed successfully and the virt device has been validated again. If giving an endpoint back fails it stays assigned to the sideband, and xhci_sideband_unregister() clears what is left. Signed-off-by: Mathias Nyman --- drivers/usb/host/xhci-sideband.c | 115 +++++++++++++++++++++++++++++-- 1 file changed, 111 insertions(+), 4 deletions(-) diff --git a/drivers/usb/host/xhci-sideband.c b/drivers/usb/host/xhci-sideband.c index 5bccbf34e3c285..0f0bb0c2110c22 100644 --- a/drivers/usb/host/xhci-sideband.c +++ b/drivers/usb/host/xhci-sideband.c @@ -279,6 +279,87 @@ sideband_stop_endpoint_sync(struct xhci_sideband *sb, unsigned int ep_index, return ret; } +/* + * Hand an endpoint over to the xHC audio sideband using resource @res_id, or + * give it back to xhci if @res_id is 0 (xHCI 7.9.2.1). The vdev is validated + * under the same xhci->lock that queues the command. ep->sideband and + * sb->eps[] are only updated once the command completed successfully and the + * vdev has been validated again. + * + * Caller must hold sb->mutex + */ +static int +sideband_set_ep_resource_sync(struct xhci_sideband *sb, unsigned int ep_index, + u32 res_id) +{ + struct xhci_command *cmd; + struct xhci_virt_ep *ep; + unsigned long flags; + int ret = 0; + + lockdep_assert_held(&sb->mutex); + + cmd = xhci_alloc_command(sb->xhci, true, GFP_KERNEL); + if (!cmd) + ret = -ENOMEM; + + spin_lock_irqsave(&sb->xhci->lock, flags); + + /* validate that xhci didn't free or reallocate vdev */ + ep = sideband_to_ep(sb, ep_index); + if (!ep) { + ret = -ENODEV; + goto out_unlock; + } + + /* fail add if ep is already in sideband use, or uses streams */ + if (res_id) { + if (ep->sideband) + ret = -EBUSY; + else if (ep->ep_state & EP_HAS_STREAMS) + ret = -EINVAL; + } else if (ep->sideband != sb) { + /* fail remove if endpoint isn't part of this sideband */ + ret = -ENODEV; + } + + if (ret) + goto out_unlock; + + ret = xhci_queue_set_ext_property(sb->xhci, cmd, sb->udev->slot_id, + ep_index + 1, 0, PROP_ECI_AUDIO, + AUDIO_SUBTYPE_SET_RES, res_id); + if (ret) + goto out_unlock; + + xhci_ring_cmd_db(sb->xhci); + spin_unlock_irqrestore(&sb->xhci->lock, flags); + + wait_for_completion(cmd->completion); + + spin_lock_irqsave(&sb->xhci->lock, flags); + ep = sideband_to_ep(sb, ep_index); + if (!ep) { + ret = -ENODEV; + goto out_unlock; + } + + if (cmd->status != COMP_SUCCESS) { + ret = -EIO; + goto out_unlock; + } + + ep->sideband = res_id ? sb : NULL; + sb->eps[ep_index] = res_id ? ep : NULL; + +out_unlock: + spin_unlock_irqrestore(&sb->xhci->lock, flags); + if (cmd) + xhci_free_command(sb->xhci, cmd); + + return ret; +} + /* Caller must hold sb->mutex */ static int __xhci_sideband_remove_endpoint(struct xhci_sideband *sb, unsigned int ep_index) @@ -287,10 +368,17 @@ __xhci_sideband_remove_endpoint(struct xhci_sideband *sb, unsigned int ep_index) lockdep_assert_held(&sb->mutex); - if (sb->type == XHCI_SIDEBAND_VENDOR) + if (sb->type == XHCI_SIDEBAND_VENDOR) { ret = sideband_stop_endpoint_sync(sb, ep_index, true); + sb->eps[ep_index] = NULL; + } - sb->eps[ep_index] = NULL; + if (sb->type == XHCI_SIDEBAND_AUDIO) { + ret = sideband_set_ep_resource_sync(sb, ep_index, 0); + /* removed on success or when the endpoint is no longer ours */ + if (ret == 0 || ret == -ENODEV) + sb->eps[ep_index] = NULL; + } return ret; } @@ -486,6 +574,7 @@ xhci_sideband_add_endpoint(struct xhci_sideband *sb, { struct xhci_virt_ep *ep; unsigned int ep_index; + unsigned int res_id; unsigned long flags; int ret = 0; @@ -499,8 +588,17 @@ xhci_sideband_add_endpoint(struct xhci_sideband *sb, if (sb->eps[ep_index]) return -EBUSY; - if (sb->type == XHCI_SIDEBAND_AUDIO) - return -EINVAL; + if (sb->type == XHCI_SIDEBAND_AUDIO) { + res_id = sb->eps_res_id[ep_index]; + if (res_id == 0) { + dev_dbg(&sb->udev->dev, + "Can't offload endpoint %u, missing resource id\n", + ep_index); + return -EINVAL; + } + + return sideband_set_ep_resource_sync(sb, ep_index, res_id); + } /* * Note, we don't know the DMA mask of the audio DSP device, if its @@ -816,6 +914,7 @@ void xhci_sideband_unregister(struct xhci_sideband *sb) { struct xhci_virt_device *vdev; + struct xhci_virt_ep *ep; struct usb_device *udev; struct xhci_hcd *xhci; int res_id; @@ -847,6 +946,14 @@ xhci_sideband_unregister(struct xhci_sideband *sb) __xhci_sideband_remove_interrupter(sb); spin_lock_irq(&xhci->lock); + + /* clear endpoint sideband pointers for endpoints that failed removal */ + for (i = 0; i < EP_CTX_PER_DEV; i++) { + ep = sideband_to_ep(sb, i); + if (ep && ep->sideband == sb) + ep->sideband = NULL; + sb->eps[i] = NULL; + } vdev = sideband_to_vdev(sb); if (vdev) vdev->sideband = NULL; From 302fcff6ab887b51be8f0618698958f7ee6f3360 Mon Sep 17 00:00:00 2001 From: Mathias Nyman Date: Mon, 2 Feb 2026 11:18:15 +0200 Subject: [PATCH 09/53] xhci: sideband: Add endpoint property query to sideband API Prepare the xHCI Audio Sideband API for Intel UAOL consumers. Add xhci_sideband_get_ep_properties(), an endpoint property query returning all the xHC and endpoint properties needed to set up Intel UAOL. Partially based on work by Peter Ujfalusi. Cc: Peter Ujfalusi Signed-off-by: Mathias Nyman --- drivers/usb/host/xhci-sideband.c | 74 +++++++++++++++++++++++++++++++ include/linux/usb/xhci-sideband.h | 37 ++++++++++++++++ 2 files changed, 111 insertions(+) diff --git a/drivers/usb/host/xhci-sideband.c b/drivers/usb/host/xhci-sideband.c index 0f0bb0c2110c22..09d3af36060dc5 100644 --- a/drivers/usb/host/xhci-sideband.c +++ b/drivers/usb/host/xhci-sideband.c @@ -10,6 +10,7 @@ #include #include +#include #include "xhci.h" @@ -398,6 +399,79 @@ __xhci_sideband_remove_interrupter(struct xhci_sideband *sb) /* sideband api functions */ +/** + * xhci_sideband_get_ep_properties - query xHC properties for an endpoint + * @sb: sideband instance for this usb device + * @host_ep: usb host endpoint + * @properties: returned properties. + * + * Return: 0 on success, negative error otherwise + */ +int +xhci_sideband_get_ep_properties(struct xhci_sideband *sb, + struct usb_host_endpoint *host_ep, + struct xhci_sideband_ep_properties *properties) +{ + union xhci_extprop_ctx *ctx; + unsigned int ep_index; + struct device *dev; + u32 ep_id; + int ret = 0; + + guard(mutex)(&sb->mutex); + + if (!sideband_to_vdev(sb)) + return -ENODEV; + + if (!xhci_sideband_endpoint_belongs(sb, host_ep)) + return -EINVAL; + + if (sb->type != XHCI_SIDEBAND_AUDIO || !properties) + return -EINVAL; + + memset(properties, 0, sizeof(*properties)); + ep_index = xhci_get_endpoint_index(&host_ep->desc); + dev = xhci_to_hcd(sb->xhci)->self.controller; + ep_id = ep_index + 1; + + /* get the PCI B:D:F data of parent xHC */ + if (dev_is_pci(dev)) { + struct pci_dev *pdev = to_pci_dev(dev); + + properties->bus = pdev->bus->number; + properties->device = PCI_SLOT(pdev->devfn); + properties->function = PCI_FUNC(pdev->devfn); + } + + properties->slot_id = sb->udev->slot_id; + + properties->reserved_res_id = sb->eps_res_id[ep_index]; + + /* no resource id set for endpoint, don't query ep property */ + if (properties->reserved_res_id == 0) + return 0; + + ctx = kzalloc_obj(*ctx, GFP_KERNEL); + if (!ctx) + return -ENOMEM; + + /* query extended endpoint property from xHC */ + ret = xhci_get_ext_property(sb->xhci, sb->udev->slot_id, ep_id, + PROP_ECI_AUDIO, AUDIO_SUBTYPE_GET_EP_PROP, + ctx, sizeof(*ctx)); + if (!ret) { + properties->rh_port = ctx->audio_ep.rh_port; + properties->res_id = ctx->audio_ep.res_id; + properties->port_speed = ctx->audio_ep.port_speed_tt & 0xf; + properties->tt = !!(ctx->audio_ep.port_speed_tt & 0x10); + } + + kfree(ctx); + + return ret; +} +EXPORT_SYMBOL_GPL(xhci_sideband_get_ep_properties); + /** * xhci_sideband_alloc_resource_id - reserve a resource id to offload an endpoint * @sb: sideband instance for this usb device diff --git a/include/linux/usb/xhci-sideband.h b/include/linux/usb/xhci-sideband.h index 82b3ee10197cb6..6ad43324a899d5 100644 --- a/include/linux/usb/xhci-sideband.h +++ b/include/linux/usb/xhci-sideband.h @@ -36,6 +36,39 @@ struct xhci_sideband_event { void *evt_data; }; +/** + * struct xhci_sideband_ep_properties - controller properties of an endpoint. + * @bus: PCI bus number if xHCI is on a PCI bus + * @device: PCI device number if xHCI is on a PCI bus + * @function: PCI function number if xHCI is on a PCI bus + * @slot_id: xHCI slot_id of the usb device this endpoint is part of + * @reserved_res_id: driver reserved resource number for this endpoint + * @rh_port: root hub port number + * @res_id: xHCI sideband resource number in use by offloaded endpoint + * @port_speed: PORTSC protocol speed ID + * @tt: endpoint is behind a USB 2.0 transaction translator + * + * reserved_res_id is set after calling xhci_sideband_alloc_resource_id(), + * res_id is set after offloading the endpoint when xhci_sideband_add_endpoint() + * is called, and should match reserved_res_id at that point. + */ +struct xhci_sideband_ep_properties { + /* PCI bus, device and function */ + u8 bus; + u8 device; + u8 function; + + /* xHCI usb device slot_id */ + int slot_id; + u8 reserved_res_id; + + /* Properties queried with Get Endpoint Properties Command */ + u8 rh_port; + u8 res_id; + u8 port_speed; + bool tt; +}; + /** * struct xhci_sideband - representation of a sideband accessed usb device. * @xhci: The xhci host controller the usb device is connected to @@ -106,6 +139,10 @@ xhci_sideband_alloc_resource_id(struct xhci_sideband *sb, int xhci_sideband_free_resource_id(struct xhci_sideband *sb, unsigned int res_id); +int +xhci_sideband_get_ep_properties(struct xhci_sideband *sb, + struct usb_host_endpoint *host_ep, + struct xhci_sideband_ep_properties *properties); #if IS_ENABLED(CONFIG_USB_XHCI_SIDEBAND) void xhci_sideband_notify_ep_ring_free(struct xhci_sideband *sb, From e6db236ae3b179fa27adccf8b85fdd3e9714f84e Mon Sep 17 00:00:00 2001 From: Mathias Nyman Date: Mon, 19 Oct 2026 10:08:57 +0300 Subject: [PATCH 10/53] xhci: sideband: Don't support Intel audio sideband for devices behind a hub Audio quality cannot be guaranteed with Intel xHC audio sideband offloading when the device is connected behind a hub. Only allow Intel hosts to register an audio sideband for devices connected directly to a root hub port. Other hosts are not affected. Signed-off-by: Mathias Nyman --- drivers/usb/host/xhci-sideband.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/drivers/usb/host/xhci-sideband.c b/drivers/usb/host/xhci-sideband.c index 09d3af36060dc5..97dfe34de508b6 100644 --- a/drivers/usb/host/xhci-sideband.c +++ b/drivers/usb/host/xhci-sideband.c @@ -931,10 +931,16 @@ xhci_sideband_register(struct usb_interface *intf, enum xhci_sideband_type type, case XHCI_SIDEBAND_VENDOR: break; case XHCI_SIDEBAND_AUDIO: + if (xhci->quirks & XHCI_INTEL_HOST && udev->parent->parent) { + dev_dbg(&udev->dev, "Intel Audio sideband not supported behind hub\n"); + return NULL; + } + if (!xhci_sideband_hw_capable(xhci)) { dev_info(&udev->dev, "Host not sideband capable\n"); return NULL; } + break; default: return NULL; From 764836f128412ff7bb036664d70086ff78c848ad Mon Sep 17 00:00:00 2001 From: Peter Ujfalusi Date: Thu, 19 Nov 2026 10:37:05 +0200 Subject: [PATCH 11/53] xhci: sideband: Add xhci_sideband_start_endpoint() A client driving endpoints over the audio sideband can stop them with xhci_sideband_stop_endpoint(), but has no way to start them again: a stopped endpoint only returns to the running state when its doorbell is rung, and nothing rings it for an endpoint the sideband owns. A stream which is stopped and then started again without its endpoints being reconfigured in between, like one recovering from an xrun, stays stalled. Add xhci_sideband_start_endpoint(), which rings the doorbell of an endpoint the sideband owns. [mn: use sideband_to_ep(), return -ENODEV instead of -EINVAL if it fails] Signed-off-by: Peter Ujfalusi Signed-off-by: Mathias Nyman --- drivers/usb/host/xhci-sideband.c | 36 +++++++++++++++++++++++++++++++ include/linux/usb/xhci-sideband.h | 3 +++ 2 files changed, 39 insertions(+) diff --git a/drivers/usb/host/xhci-sideband.c b/drivers/usb/host/xhci-sideband.c index 97dfe34de508b6..ca27460d5009c2 100644 --- a/drivers/usb/host/xhci-sideband.c +++ b/drivers/usb/host/xhci-sideband.c @@ -743,6 +743,42 @@ xhci_sideband_stop_endpoint(struct xhci_sideband *sb, } EXPORT_SYMBOL_GPL(xhci_sideband_stop_endpoint); +/** + * xhci_sideband_start_endpoint - restart a sideband endpoint + * @sb: sideband instance for this usb device + * @host_ep: usb host endpoint + * + * Rings the doorbell of an endpoint stopped with xhci_sideband_stop_endpoint(), + * which is what moves a stopped endpoint back to the running state. Ringing + * the doorbell of a running endpoint does nothing. + * + * Return: 0 on success, negative error otherwise. + */ +int +xhci_sideband_start_endpoint(struct xhci_sideband *sb, + struct usb_host_endpoint *host_ep) +{ + struct xhci_virt_ep *ep; + unsigned int ep_index; + + /* the same locking as stopping the endpoint: sb->eps[] is sb->mutex's */ + guard(mutex)(&sb->mutex); + + ep_index = xhci_get_endpoint_index(&host_ep->desc); + + guard(spinlock_irqsave)(&sb->xhci->lock); + + /* validate that xhci didn't free or reallocate vdev and its endpoint */ + ep = sideband_to_ep(sb, ep_index); + if (!ep || ep->sideband != sb) + return -ENODEV; + + xhci_ring_ep_doorbell(sb->xhci, sb->udev->slot_id, ep_index, 0); + + return 0; +} +EXPORT_SYMBOL_GPL(xhci_sideband_start_endpoint); + /** * xhci_sideband_get_endpoint_buffer - gets the endpoint transfer buffer address * @sb: sideband instance for this usb device diff --git a/include/linux/usb/xhci-sideband.h b/include/linux/usb/xhci-sideband.h index 6ad43324a899d5..4ec441b5628eb6 100644 --- a/include/linux/usb/xhci-sideband.h +++ b/include/linux/usb/xhci-sideband.h @@ -112,6 +112,9 @@ xhci_sideband_remove_endpoint(struct xhci_sideband *sb, int xhci_sideband_stop_endpoint(struct xhci_sideband *sb, struct usb_host_endpoint *host_ep); +int +xhci_sideband_start_endpoint(struct xhci_sideband *sb, + struct usb_host_endpoint *host_ep); struct sg_table * xhci_sideband_get_endpoint_buffer(struct xhci_sideband *sb, struct usb_host_endpoint *host_ep); From c86e2ccc533b62ffb93a082ad0a2ba8d7cc667e1 Mon Sep 17 00:00:00 2001 From: Peter Ujfalusi Date: Wed, 2 Sep 2026 17:29:18 +0300 Subject: [PATCH 12/53] ALSA: hda: Add the 384kHz stream format encoding snd_hdac_stream_format() silently falls back to the 48kHz encoding for any rate missing from rate_bits[]. USB audio devices offloaded over the HDaudio link can run at 384kHz, so add the encoding: base 48kHz, mult 8, div 1. Signed-off-by: Peter Ujfalusi --- sound/hda/core/device.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sound/hda/core/device.c b/sound/hda/core/device.c index 6187ffaedbfa3f..a27a48d15e2398 100644 --- a/sound/hda/core/device.c +++ b/sound/hda/core/device.c @@ -712,10 +712,10 @@ static const struct hda_rate_tbl rate_bits[] = { { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) }, { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) }, #define AC_PAR_PCM_RATE_BITS 11 - /* up to bits 10, 384kHZ isn't supported properly */ + /* up to bits 10, the rest below are not autodetected values */ - /* not autodetected value */ { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) }, + { 384000, SNDRV_PCM_RATE_384000, HDA_RATE(48, 8, 1) }, { 0 } /* terminator */ }; From 6ae4be9cd98d43763ec42b3e20b8b65002062313 Mon Sep 17 00:00:00 2001 From: Peter Ujfalusi Date: Wed, 2 Sep 2026 17:29:33 +0300 Subject: [PATCH 13/53] ASoC: SOF: Intel: hda-pcm: Add the 384kHz mult/div encoding hda_dsp_get_mult_div() falls back to the 48kHz encoding for a rate it does not recognise and only warns about it, so a 384kHz host stream had its SDnFMT MULT/DIV programmed for 48kHz. Add 384000 with the same 8x multiplier snd_hdac_stream_format() uses. Signed-off-by: Peter Ujfalusi --- sound/soc/sof/intel/hda-pcm.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/sound/soc/sof/intel/hda-pcm.c b/sound/soc/sof/intel/hda-pcm.c index 70abff12fef0ee..877465b4ae635d 100644 --- a/sound/soc/sof/intel/hda-pcm.c +++ b/sound/soc/sof/intel/hda-pcm.c @@ -71,6 +71,8 @@ u32 hda_dsp_get_mult_div(struct snd_sof_dev *sdev, int rate) return SDnFMT_BASE(1) | SDnFMT_MULT(4); case 192000: return SDnFMT_MULT(4); + case 384000: + return SDnFMT_MULT(8); default: dev_warn(sdev->dev, "can't find div rate %d using 48kHz\n", rate); From a50802fc18151be914e940f2f20f44f687fbe6c8 Mon Sep 17 00:00:00 2001 From: Peter Ujfalusi Date: Mon, 10 Aug 2026 11:20:04 +0300 Subject: [PATCH 14/53] ASoC: usb: Match the backend context by device on non-DT platforms snd_soc_find_usb_ctx() only resolves the backend context through the device tree, so on ACPI/PCI platforms snd_soc_usb_connect() and snd_soc_usb_disconnect() silently do nothing. Without an of_node, match the registered component's device against the caller's device instead. DT platforms are unaffected. Signed-off-by: Peter Ujfalusi --- Documentation/sound/soc/usb.rst | 5 ++++- sound/soc/soc-usb.c | 29 ++++++++++++++++++++++++----- 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/Documentation/sound/soc/usb.rst b/Documentation/sound/soc/usb.rst index 9cb860547f22d6..eaf16bb3acfdc8 100644 --- a/Documentation/sound/soc/usb.rst +++ b/Documentation/sound/soc/usb.rst @@ -62,7 +62,10 @@ Structures The snd_soc_usb structure can be referenced using the ASoC platform card device, or a USB device (udev->dev). This is created by the ASoC BE DAI link, and the USB sound entity will be able to pass information to the -ASoC BE DAI link using this structure. +ASoC BE DAI link using this structure. On device tree platforms the USB +device is tied to the port through the ``usb-soc-be`` phandle; without a +device tree node the port is found by the device its component was +registered with, so the caller passes that device. ``struct snd_soc_usb_device`` diff --git a/sound/soc/soc-usb.c b/sound/soc/soc-usb.c index 6386404a8db8ca..58c5ce0d160ec7 100644 --- a/sound/soc/soc-usb.c +++ b/sound/soc/soc-usb.c @@ -24,7 +24,7 @@ static struct device_node *snd_soc_find_phandle(struct device *dev) return node; } -static struct snd_soc_usb *snd_soc_usb_ctx_lookup(struct device_node *node) +static struct snd_soc_usb *snd_soc_usb_ctx_lookup_node(struct device_node *node) { struct snd_soc_usb *ctx; @@ -39,6 +39,25 @@ static struct snd_soc_usb *snd_soc_usb_ctx_lookup(struct device_node *node) return NULL; } +static struct snd_soc_usb *snd_soc_usb_ctx_lookup(struct device *dev) +{ + struct snd_soc_usb *ctx; + + if (!dev) + return NULL; + + if (dev->of_node) + return snd_soc_usb_ctx_lookup_node(dev->of_node); + + /* ACPI/PCI platforms have no of_node, match on the device itself */ + list_for_each_entry(ctx, &usb_ctx_list, list) { + if (ctx->component->dev == dev) + return ctx; + } + + return NULL; +} + static struct snd_soc_usb *snd_soc_find_usb_ctx(struct device *dev) { struct snd_soc_usb *ctx; @@ -46,13 +65,13 @@ static struct snd_soc_usb *snd_soc_find_usb_ctx(struct device *dev) node = snd_soc_find_phandle(dev); if (!IS_ERR(node)) { - ctx = snd_soc_usb_ctx_lookup(node); + ctx = snd_soc_usb_ctx_lookup_node(node); of_node_put(node); - } else { - ctx = snd_soc_usb_ctx_lookup(dev->of_node); + + return ctx; } - return ctx ? ctx : NULL; + return snd_soc_usb_ctx_lookup(dev); } /* SOC USB sound kcontrols */ From d25b930137ee4e56c46512abed9812b905181f65 Mon Sep 17 00:00:00 2001 From: Peter Ujfalusi Date: Wed, 12 Aug 2026 16:13:05 +0300 Subject: [PATCH 15/53] ALSA: usb-audio: Let the offload path claim a PCM substream A USB audio stream can be driven by the host or, on platforms with an audio DSP, by the DSP. Both configure the same endpoints of the same struct snd_usb_substream and nothing arbitrates between them: if user space opens the PCM while the DSP owns the endpoints, snd_usb_hw_params() either shares the endpoint or closes and reopens it, freeing the transfer ring behind the DSP's back. snd_usb_pcm_open() already refuses a second opener through subs->opened, so give the offload path a way to take the same reference. A claimed substream cannot be opened from user space and an opened substream cannot be claimed; whoever asks first gets the device, and the device stays fully usable by the host whenever nothing offloads it. Signed-off-by: Peter Ujfalusi --- sound/usb/card.h | 2 +- sound/usb/pcm.c | 37 +++++++++++++++++++++++++++++++++++++ sound/usb/pcm.h | 3 +++ 3 files changed, 41 insertions(+), 1 deletion(-) diff --git a/sound/usb/card.h b/sound/usb/card.h index f956bcf00f77b4..42a216a2705739 100644 --- a/sound/usb/card.h +++ b/sound/usb/card.h @@ -167,7 +167,7 @@ struct snd_usb_substream { unsigned int pkt_offset_adj; /* Bytes to drop from beginning of packets (for non-compliant devices) */ unsigned int stream_offset_adj; /* Bytes to drop from beginning of stream (for non-compliant devices) */ - unsigned int opened:1; /* pcm device opened */ + unsigned int opened:1; /* pcm device opened or claimed for offload */ unsigned int running: 1; /* running status */ unsigned int period_elapsed_pending; /* delay period handling */ diff --git a/sound/usb/pcm.c b/sound/usb/pcm.c index c2f7b08607ac86..640b9f56b4bba7 100644 --- a/sound/usb/pcm.c +++ b/sound/usb/pcm.c @@ -1311,6 +1311,43 @@ static int snd_usb_pcm_close(struct snd_pcm_substream *substream) return 0; } +/** + * snd_usb_offload_claim_substream - take a substream over for offloaded use + * @subs: the substream to claim + * + * Claims @subs on behalf of a DSP that drives the endpoints itself. It takes + * the same reference snd_usb_pcm_open() takes, so the two exclude each other: + * a claimed substream cannot be opened from user space, and a substream that + * user space already opened cannot be claimed. + * + * Return: 0 on success, -EBUSY if the substream is already in use. + */ +int snd_usb_offload_claim_substream(struct snd_usb_substream *subs) +{ + struct snd_usb_audio *chip = subs->stream->chip; + + guard(mutex)(&chip->mutex); + if (subs->opened) + return -EBUSY; + subs->opened = 1; + + return 0; +} +EXPORT_SYMBOL_GPL(snd_usb_offload_claim_substream); + +/** + * snd_usb_offload_release_substream - give a claimed substream back + * @subs: the substream to release + */ +void snd_usb_offload_release_substream(struct snd_usb_substream *subs) +{ + struct snd_usb_audio *chip = subs->stream->chip; + + guard(mutex)(&chip->mutex); + subs->opened = 0; +} +EXPORT_SYMBOL_GPL(snd_usb_offload_release_substream); + /* Since a URB can handle only a single linear buffer, we must use double * buffering when the data to be transferred overflows the buffer boundary. * To avoid inconsistencies when updating hwptr_done, we use double buffering diff --git a/sound/usb/pcm.h b/sound/usb/pcm.h index c096021adb2b11..900202b6ab2dde 100644 --- a/sound/usb/pcm.h +++ b/sound/usb/pcm.h @@ -26,4 +26,7 @@ snd_usb_find_substream_format(struct snd_usb_substream *subs, int snd_usb_hw_params(struct snd_usb_substream *subs, struct snd_pcm_hw_params *hw_params); int snd_usb_hw_free(struct snd_usb_substream *subs); + +int snd_usb_offload_claim_substream(struct snd_usb_substream *subs); +void snd_usb_offload_release_substream(struct snd_usb_substream *subs); #endif /* __USBAUDIO_PCM_H */ From 64aa0004f3150af003cc8a0cdeccfdd3fda94572 Mon Sep 17 00:00:00 2001 From: Peter Ujfalusi Date: Thu, 19 Nov 2026 13:14:09 +0200 Subject: [PATCH 16/53] ALSA: usb-audio: Keep an offloaded endpoint away from the host Claiming a substream for offloaded use keeps user space off that PCM, but not off its endpoints. With implicit feedback a playback stream syncs to the data endpoint of the capture interface: when the capture stream is offloaded, the host's playback still opens that endpoint as its sync source and submits URBs on it while the endpoint's ring belongs to the DSP, and both streams stall, whichever of them was started first. Let the offload path claim the endpoints the DSP drives with snd_usb_endpoint_claim_offload() and have snd_usb_endpoint_open() refuse to share a claimed endpoint. An endpoint which another stream has open already cannot be claimed, the offload path backs out then. snd_usb_endpoint_open() returns an ERR_PTR() for this, so that snd_usb_hw_params() reports the refusal as -EBUSY instead of the -EINVAL of a failed open. Signed-off-by: Peter Ujfalusi --- sound/usb/card.h | 1 + sound/usb/endpoint.c | 58 ++++++++++++++++++++++++++++++++++++++++---- sound/usb/endpoint.h | 4 +++ sound/usb/pcm.c | 19 ++++++++------- 4 files changed, 68 insertions(+), 14 deletions(-) diff --git a/sound/usb/card.h b/sound/usb/card.h index 42a216a2705739..422457823b3dd9 100644 --- a/sound/usb/card.h +++ b/sound/usb/card.h @@ -67,6 +67,7 @@ struct snd_usb_endpoint { struct snd_usb_clock_ref *clock_ref; int opened; /* open refcount; protect with chip->mutex */ + bool offloaded; /* driven by an offload engine; protect with chip->mutex */ atomic_t running; /* running status */ int ep_num; /* the referenced endpoint number */ int type; /* SND_USB_ENDPOINT_TYPE_* */ diff --git a/sound/usb/endpoint.c b/sound/usb/endpoint.c index 8dd0b3525e4b07..9973a4706ed7d3 100644 --- a/sound/usb/endpoint.c +++ b/sound/usb/endpoint.c @@ -792,6 +792,9 @@ bool snd_usb_endpoint_compatible(struct snd_usb_audio *chip, * Note that this function doesn't configure the endpoint. The substream * needs to set it up later via snd_usb_endpoint_set_params() and * snd_usb_endpoint_prepare(). + * + * Returns the endpoint, or an ERR_PTR(): -EBUSY if an offload engine drives + * the endpoint, see snd_usb_endpoint_claim_offload(), -EINVAL otherwise. */ struct snd_usb_endpoint * snd_usb_endpoint_open(struct snd_usb_audio *chip, @@ -807,7 +810,7 @@ snd_usb_endpoint_open(struct snd_usb_audio *chip, ep = snd_usb_get_endpoint(chip, ep_num); if (!ep) { usb_audio_err(chip, "Cannot find EP 0x%x to open\n", ep_num); - return NULL; + return ERR_PTR(-EINVAL); } if (!ep->opened) { @@ -825,12 +828,12 @@ snd_usb_endpoint_open(struct snd_usb_audio *chip, ep->iface_ref = iface_ref_find(chip, ep->iface); if (!ep->iface_ref) - return NULL; + return ERR_PTR(-EINVAL); if (fp->protocol != UAC_VERSION_1) { ep->clock_ref = clock_ref_find(chip, fp->clock); if (!ep->clock_ref) - return NULL; + return ERR_PTR(-EINVAL); ep->clock_ref->opened++; } @@ -860,12 +863,18 @@ snd_usb_endpoint_open(struct snd_usb_audio *chip, } else { if (WARN_ON(!ep->iface_ref)) - return NULL; + return ERR_PTR(-EINVAL); + + /* the host must not submit URBs on an endpoint a DSP drives */ + if (ep->offloaded) { + usb_audio_dbg(chip, "EP 0x%x is offloaded\n", ep_num); + return ERR_PTR(-EBUSY); + } if (!endpoint_compatible(ep, fp, params)) { usb_audio_err(chip, "Incompatible EP setup for 0x%x\n", ep_num); - return NULL; + return ERR_PTR(-EINVAL); } usb_audio_dbg(chip, "Reopened EP 0x%x (count %d)\n", @@ -977,10 +986,49 @@ void snd_usb_endpoint_close(struct snd_usb_audio *chip, ep->cur_rate = 0; ep->iface_ref = NULL; ep->clock_ref = NULL; + ep->offloaded = false; usb_audio_dbg(chip, "EP 0x%x closed\n", ep->ep_num); } } +/** + * snd_usb_endpoint_claim_offload - hand an open endpoint over to an offload engine + * @chip: the USB audio chip + * @ep: the endpoint, opened for the substream which is offloaded + * + * A DSP drives the endpoint from now on, snd_usb_endpoint_open() refuses to + * share it with another stream, as no host stream must submit URBs on it. An + * endpoint which another stream has open as well, like the capture endpoint a + * playback stream with implicit feedback syncs to, cannot be claimed. + * + * Return: 0 on success, -EBUSY if @ep is in use elsewhere. + */ +int snd_usb_endpoint_claim_offload(struct snd_usb_audio *chip, + struct snd_usb_endpoint *ep) +{ + guard(mutex)(&chip->mutex); + if (ep->opened > 1 || ep->offloaded) + return -EBUSY; + + ep->offloaded = true; + + return 0; +} +EXPORT_SYMBOL_GPL(snd_usb_endpoint_claim_offload); + +/** + * snd_usb_endpoint_release_offload - give a claimed endpoint back to the host + * @chip: the USB audio chip + * @ep: the endpoint snd_usb_endpoint_claim_offload() claimed + */ +void snd_usb_endpoint_release_offload(struct snd_usb_audio *chip, + struct snd_usb_endpoint *ep) +{ + guard(mutex)(&chip->mutex); + ep->offloaded = false; +} +EXPORT_SYMBOL_GPL(snd_usb_endpoint_release_offload); + /* Prepare for suspening EP, called from the main suspend handler */ void snd_usb_endpoint_suspend(struct snd_usb_endpoint *ep) { diff --git a/sound/usb/endpoint.h b/sound/usb/endpoint.h index ba70f52f68602e..2ea22835fcd968 100644 --- a/sound/usb/endpoint.h +++ b/sound/usb/endpoint.h @@ -18,6 +18,10 @@ snd_usb_endpoint_open(struct snd_usb_audio *chip, bool fixed_rate); void snd_usb_endpoint_close(struct snd_usb_audio *chip, struct snd_usb_endpoint *ep); +int snd_usb_endpoint_claim_offload(struct snd_usb_audio *chip, + struct snd_usb_endpoint *ep); +void snd_usb_endpoint_release_offload(struct snd_usb_audio *chip, + struct snd_usb_endpoint *ep); int snd_usb_endpoint_set_params(struct snd_usb_audio *chip, struct snd_usb_endpoint *ep); int snd_usb_endpoint_prepare(struct snd_usb_audio *chip, diff --git a/sound/usb/pcm.c b/sound/usb/pcm.c index 640b9f56b4bba7..f7f6564e81d5d8 100644 --- a/sound/usb/pcm.c +++ b/sound/usb/pcm.c @@ -485,6 +485,7 @@ int snd_usb_hw_params(struct snd_usb_substream *subs, struct snd_usb_audio *chip = subs->stream->chip; const struct audioformat *fmt; const struct audioformat *sync_fmt; + struct snd_usb_endpoint *ep; bool fixed_rate, sync_fixed_rate; int ret; @@ -540,21 +541,21 @@ int snd_usb_hw_params(struct snd_usb_substream *subs, close_endpoints(chip, subs); } - subs->data_endpoint = snd_usb_endpoint_open(chip, fmt, hw_params, false, fixed_rate); - if (!subs->data_endpoint) { - ret = -EINVAL; + ep = snd_usb_endpoint_open(chip, fmt, hw_params, false, fixed_rate); + if (IS_ERR(ep)) { + ret = PTR_ERR(ep); goto unlock; } + subs->data_endpoint = ep; if (fmt->sync_ep) { - subs->sync_endpoint = snd_usb_endpoint_open(chip, sync_fmt, - hw_params, - fmt == sync_fmt, - sync_fixed_rate); - if (!subs->sync_endpoint) { - ret = -EINVAL; + ep = snd_usb_endpoint_open(chip, sync_fmt, hw_params, + fmt == sync_fmt, sync_fixed_rate); + if (IS_ERR(ep)) { + ret = PTR_ERR(ep); goto unlock; } + subs->sync_endpoint = ep; snd_usb_endpoint_set_sync(chip, subs->data_endpoint, subs->sync_endpoint); From 0fc9c77d3d2aa119cb7623f528f33edb16128fa1 Mon Sep 17 00:00:00 2001 From: Peter Ujfalusi Date: Thu, 19 Nov 2026 15:12:45 +0200 Subject: [PATCH 17/53] ALSA: usb-audio: Add snd_usb_release_platform_ops() A platform that keeps state for the USB SND devices it is told of, an offload driver which holds their streams and xHCI sideband, has no safe way to let go of them when it unregisters. Once its operations are unregistered, a device disconnected in the meantime is not reported to it anymore and its chip, card and usb_device are freed while the platform still points to them. Unregistering after cleaning up instead leaves a window for a device connected in between. Add snd_usb_release_platform_ops(), the counterpart of snd_usb_rediscover_devices(): under register_mutex, which serialises it against probe and disconnect, it runs the disconnect callback for every connected device and then unregisters the operations. Signed-off-by: Peter Ujfalusi --- sound/usb/card.c | 25 +++++++++++++++++++++++++ sound/usb/card.h | 1 + 2 files changed, 26 insertions(+) diff --git a/sound/usb/card.c b/sound/usb/card.c index 59979b793fe91f..9451cf40e5d84d 100644 --- a/sound/usb/card.c +++ b/sound/usb/card.c @@ -195,6 +195,31 @@ void snd_usb_rediscover_devices(void) } EXPORT_SYMBOL_GPL(snd_usb_rediscover_devices); +/* + * The counterpart of snd_usb_rediscover_devices(): run the disconnect + * callback for every connected USB SND device, then unregister the platform + * operations. A platform which keeps state for the devices it was told of + * lets go of all of them this way. Unregistering alone leaves it to clean up + * after a device which could be freed under it, as one disconnected in the + * meantime would not be reported anymore. + */ +void snd_usb_release_platform_ops(void) +{ + int i; + + guard(mutex)(®ister_mutex); + + if (platform_ops && platform_ops->disconnect_cb) { + for (i = 0; i < SNDRV_CARDS; i++) { + if (usb_chip[i]) + platform_ops->disconnect_cb(usb_chip[i]); + } + } + + platform_ops = NULL; +} +EXPORT_SYMBOL_GPL(snd_usb_release_platform_ops); + /* * Checks to see if requested audio profile, i.e sample rate, # of * channels, etc... is supported by the substream associated to the diff --git a/sound/usb/card.h b/sound/usb/card.h index 422457823b3dd9..e2f51608b87cdb 100644 --- a/sound/usb/card.h +++ b/sound/usb/card.h @@ -225,6 +225,7 @@ snd_usb_find_suppported_substream(int card_idx, struct snd_pcm_hw_params *params int snd_usb_register_platform_ops(struct snd_usb_platform_ops *ops); int snd_usb_unregister_platform_ops(void); +void snd_usb_release_platform_ops(void); void snd_usb_rediscover_devices(void); #endif /* __USBAUDIO_CARD_H */ From 5ae93764f6a77efff41a0d2249a309b724a46e32 Mon Sep 17 00:00:00 2001 From: Rene Sapiens Date: Wed, 4 Nov 2026 14:21:40 +0200 Subject: [PATCH 18/53] ALSA: usb: Add USB audio offload library Add a library that tracks offload capable USB audio devices and forwards their lifecycle to a host driver owning the DSP. Device ownership stays with snd-usb-audio. The library hooks the USB-SND platform-op callbacks and reports the hotplug events of the devices that can be offloaded to the host through a callback table, struct usb_offload_ops. The host turns each callback into whatever its DSP expects; the library builds no IPC message, touches no register and owns no interrupt. The audio-control interface is looked up in the active configuration rather than assumed to be interface 0, so composite devices where the audio function sits behind other interfaces are candidates too. A device exposing more than one audio function is offloaded through the first one. The data path is built on the xHCI Audio Sideband, which routes an endpoint's traffic to the DSP. An endpoint only exists while the streaming altsetting is active, so on stream acquire the library first claims the USB substream and starts it, the same way a host driven stream is started, then reports the data endpoint. Claiming the substream also keeps user space off the device for as long as it is offloaded. A device can expose several PCMs of the same direction (e.g. separate speaker and line-out streams), so the acquisition takes the USB PCM device number the caller wants, or a negative index to take the first PCM matching the format. The acquired streams are kept per USB PCM: a PCM is offloaded once, the other PCMs of the device can be acquired alongside. The PCM number the stream landed on is reported back and names the stream to usb_offload_release_stream(). No card is created here and no stream is driven from here: the host's backend DAI acquires the endpoint with usb_offload_acquire_stream() when its own framework triggers it. A context stands for one offload engine, the link over which a DSP reaches a host controller; a host with several registers one context for each. There is no descriptor filter of its own: by the time the connect callback runs, snd-usb-audio has parsed the device and left the streams it can drive in chip->pcm_list, with the malformed ones already left out. A device is a candidate if it is a full or high speed audio function, and each of its PCMs is offered for offload on its own. The endpoints are claimed and their sideband resources allocated per stream at acquire, so there is nothing to count or reserve beforehand. usb_offload_connect() notifies the host, which can call back into the device record through its own framework before returning (e.g. a route control's busy check), so the device list lock is dropped around the connect and disconnect notifications. The record cannot go away meanwhile: the callbacks that create and free it are serialised by snd-usb-audio's register_mutex. A device being torn down is marked disconnected before the lock is dropped, so that a stream acquire racing with the unplug is refused instead of handing out a device which is already gone. On an unplug the host is notified first, so that it ends the streams it runs on the device and stops the DSP driving their endpoints before the library releases the streams and their resources. Nothing in the interface is tied to a sound framework, but no consumer outside ALSA exists, so the header lives under include/sound/. Signed-off-by: Rene Sapiens Co-developed-by: Pooja Katiyar Signed-off-by: Pooja Katiyar Co-developed-by: Peter Ujfalusi Signed-off-by: Peter Ujfalusi --- include/sound/usb-offload.h | 144 ++++++++ sound/usb/Kconfig | 1 + sound/usb/Makefile | 1 + sound/usb/offload/Kconfig | 21 ++ sound/usb/offload/Makefile | 6 + sound/usb/offload/init.c | 107 ++++++ sound/usb/offload/offload.h | 59 ++++ sound/usb/offload/usb-audio.c | 608 ++++++++++++++++++++++++++++++++++ 8 files changed, 947 insertions(+) create mode 100644 include/sound/usb-offload.h create mode 100644 sound/usb/offload/Kconfig create mode 100644 sound/usb/offload/Makefile create mode 100644 sound/usb/offload/init.c create mode 100644 sound/usb/offload/offload.h create mode 100644 sound/usb/offload/usb-audio.c diff --git a/include/sound/usb-offload.h b/include/sound/usb-offload.h new file mode 100644 index 00000000000000..29d1e909f20cae --- /dev/null +++ b/include/sound/usb-offload.h @@ -0,0 +1,144 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * USB audio offload. + * + * An offloaded USB audio stream is driven by a DSP instead of by the + * host: the host controller routes the endpoint's traffic to the DSP over + * a dedicated link, an xHCI Audio Sideband resource. + * + * The USB audio offload library tracks offload capable USB audio devices + * and forwards their lifecycle to the driver owning the DSP. That driver + * installs a struct usb_offload_ops and turns each callback into whatever + * its DSP expects, normally an IPC message. The library builds no IPC + * message, touches no register and owns no interrupt. + * + * Lifecycle: + * + * ctx = usb_offload_register(&res); + * ...devices are reported via ops->notify_connect/notify_disconnect... + * usb_offload_unregister(ctx); + * + * One context stands for one offload engine, the link over which a DSP + * reaches a host controller. A host with several of them registers one + * context for each. + * + * The data path is not driven from here. The host's backend DAI reserves + * the endpoint resources itself with usb_offload_acquire_stream(). + */ + +#ifndef __SOUND_USB_OFFLOAD_H +#define __SOUND_USB_OFFLOAD_H + +#include + +struct device; +struct snd_pcm_hw_params; +struct usb_device; +struct usb_offload_ctx; + +#define USB_OFFLOAD_MAX_PCM 8 +#define USB_OFFLOAD_PCM_NAME_LEN 64 + +/** + * struct usb_offload_usb_dev_info - identity of an offloadable USB device + * @card_idx: snd_card number + * @chip_idx: snd_usb_audio index + * @num_playback: populated @ppcm_idx entries + * @num_capture: populated @cpcm_idx entries + * @ppcm_idx: playback PCM device numbers + * @cpcm_idx: capture PCM device numbers + * @ppcm_names: playback PCM names, parallel to @ppcm_idx - ", | + * - " so that a device offering several + * destinations, and several devices offering the same generic PCM + * name, both stay distinguishable + * @cpcm_names: capture PCM names, parallel to @cpcm_idx, same convention + * + * Filled in by the snd-usb-audio ownership bridge, which holds the + * struct snd_usb_audio chip, and handed to the host on connect. + */ +struct usb_offload_usb_dev_info { + int card_idx; + int chip_idx; + int num_playback; + int num_capture; + unsigned int ppcm_idx[USB_OFFLOAD_MAX_PCM]; + unsigned int cpcm_idx[USB_OFFLOAD_MAX_PCM]; + char ppcm_names[USB_OFFLOAD_MAX_PCM][USB_OFFLOAD_PCM_NAME_LEN]; + char cpcm_names[USB_OFFLOAD_MAX_PCM][USB_OFFLOAD_PCM_NAME_LEN]; +}; + +/** + * struct usb_offload_ops - host callback table, library to host + * @notify_connect: an offloadable device appeared + * @notify_disconnect: an offloaded device went away + * + * @notify_connect and @notify_disconnect are mandatory. All callbacks run in + * process context. + */ +struct usb_offload_ops { + int (*notify_connect)(struct device *dev, struct usb_device *udev, + const struct usb_offload_usb_dev_info *id); + int (*notify_disconnect)(struct device *dev, struct usb_device *udev); +}; + +/** + * struct usb_offload_res - what the host hands the library at registration + * @dev: the host's device, passed back to the @ops callbacks + * @ops: host callback table + */ +struct usb_offload_res { + struct device *dev; + const struct usb_offload_ops *ops; +}; + +struct usb_offload_ctx *usb_offload_register(const struct usb_offload_res *res); +void usb_offload_unregister(struct usb_offload_ctx *ctx); + +/** + * struct usb_offload_ep_info - an endpoint handed to the DSP + * @ep_addr: bEndpointAddress of the endpoint + */ +struct usb_offload_ep_info { + u8 ep_addr; +}; + +/** + * struct usb_offload_stream_hw - resources reserved for one offloaded stream + * @pcm_index: USB SND PCM device number the stream was acquired on, to be + * passed back to usb_offload_release_stream() + * @data: the isochronous data endpoint + * + * Filled in by usb_offload_acquire_stream() and released again by + * usb_offload_release_stream(). + */ +struct usb_offload_stream_hw { + int pcm_index; + struct usb_offload_ep_info data; +}; + +/* + * Data-path resource acquisition + * + * acquire_stream() starts the stream on the USB side and reports the + * connected device's data endpoint so that the caller can program its DSP. + * The USB PCM is taken away from the host for as long as it is held; the + * other PCMs of the device are not affected and can be acquired alongside. + * release_stream() reverses it, naming the stream by its direction and the + * USB PCM device number acquire_stream() reported in + * usb_offload_stream_hw.pcm_index. + * + * @params is the format to run the device at and @direction an + * SNDRV_PCM_STREAM_* value. @pcm_index pins the search to one USB SND PCM + * device of a multi-stream device (e.g. via snd_soc_usb_get_selected_device()), + * or is negative to consider every PCM stream of the device. + */ +int usb_offload_acquire_stream(struct usb_offload_ctx *ctx, + struct usb_device *udev, int direction, + int pcm_index, + struct snd_pcm_hw_params *params, + struct usb_offload_stream_hw *hw); +int usb_offload_release_stream(struct usb_offload_ctx *ctx, + struct usb_device *udev, int direction, + int pcm_index); + +#endif /* __SOUND_USB_OFFLOAD_H */ diff --git a/sound/usb/Kconfig b/sound/usb/Kconfig index b4588915efa114..3ba255dbae3cc3 100644 --- a/sound/usb/Kconfig +++ b/sound/usb/Kconfig @@ -205,6 +205,7 @@ config SND_USB_AUDIO_QMI will be called snd-usb-audio-qmi. source "sound/usb/line6/Kconfig" +source "sound/usb/offload/Kconfig" endif # SND_USB diff --git a/sound/usb/Makefile b/sound/usb/Makefile index 2e842dcc73de1a..9759e2015ee96a 100644 --- a/sound/usb/Makefile +++ b/sound/usb/Makefile @@ -38,3 +38,4 @@ obj-$(CONFIG_SND_USB_US122L) += snd-usbmidi-lib.o obj-$(CONFIG_SND) += misc/ usx2y/ caiaq/ 6fire/ hiface/ bcd2000/ qcom/ obj-$(CONFIG_SND_USB_LINE6) += line6/ +obj-$(CONFIG_SND_USB_OFFLOAD) += offload/ diff --git a/sound/usb/offload/Kconfig b/sound/usb/offload/Kconfig new file mode 100644 index 00000000000000..cfd48e0020f86c --- /dev/null +++ b/sound/usb/offload/Kconfig @@ -0,0 +1,21 @@ +# SPDX-License-Identifier: GPL-2.0-only +config SND_USB_OFFLOAD + tristate "USB Audio Offload library" + depends on SND_USB_AUDIO + depends on USB_XHCI_HCD && USB_XHCI_SIDEBAND + help + Library implementing the lifecycle of an offloaded USB audio + device, for a driver that owns a DSP capable of driving USB + endpoints. A host registers once, is told of the offload capable + devices as they come and go, and acquires their streams for the + DSP on demand. + + Device ownership stays with snd-usb-audio: the library hooks the + USB-SND platform-op callbacks to pick the PCMs it can offload and + forward offloadable devices to the registered host. + + The data path is built on the xHCI Audio Sideband: the library + claims a sideband resource for each offloaded endpoint, which makes + the controller route the endpoint's traffic to the DSP. + + If unsure, say N. diff --git a/sound/usb/offload/Makefile b/sound/usb/offload/Makefile new file mode 100644 index 00000000000000..9ec94222e30653 --- /dev/null +++ b/sound/usb/offload/Makefile @@ -0,0 +1,6 @@ +# SPDX-License-Identifier: GPL-2.0-only +# Makefile for the USB Audio Offload library +# + +snd-usb-offload-y := init.o usb-audio.o +obj-$(CONFIG_SND_USB_OFFLOAD) += snd-usb-offload.o diff --git a/sound/usb/offload/init.c b/sound/usb/offload/init.c new file mode 100644 index 00000000000000..c2a9434fefc039 --- /dev/null +++ b/sound/usb/offload/init.c @@ -0,0 +1,107 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * USB audio offload library - top level lifecycle. + * + * Exported in the USB_AUDIO_OFFLOAD module namespace. + */ +#include +#include +#include +#include +#include +#include + +#include "offload.h" + +/** + * usb_offload_register - register an offload engine with the library + * @res: the host's device and callbacks + * + * Starts tracking the offload capable USB audio devices for the host and + * reports the ones that are already connected through res->ops. + * + * Return: the context to pass to the other API calls, or an ERR_PTR(). + */ +struct usb_offload_ctx *usb_offload_register(const struct usb_offload_res *res) +{ + struct usb_offload_ctx *ctx; + int ret; + + if (!res || !res->dev || !res->ops || !res->ops->notify_connect || + !res->ops->notify_disconnect) + return ERR_PTR(-EINVAL); + + ctx = kzalloc_obj(*ctx); + if (!ctx) + return ERR_PTR(-ENOMEM); + + ctx->dev = res->dev; + ctx->ops = res->ops; + + ret = usb_offload_usb_audio_init(ctx); + if (ret) { + kfree(ctx); + return ERR_PTR(ret); + } + + /* + * Backfill the USB audio devices that enumerated before the bridge + * registered, e.g. when the DSP came up after USB enumeration finished. + */ + usb_offload_usb_audio_rediscover(); + + return ctx; +} +EXPORT_SYMBOL_NS_GPL(usb_offload_register, "USB_AUDIO_OFFLOAD"); + +/** + * usb_offload_unregister - unregister an offload engine + * @ctx: the context usb_offload_register() returned + * + * Gives every stream still held back to the host, reports the devices as + * disconnected and frees the context. + */ +void usb_offload_unregister(struct usb_offload_ctx *ctx) +{ + if (IS_ERR_OR_NULL(ctx)) + return; + + usb_offload_usb_audio_exit(ctx); + kfree(ctx); +} +EXPORT_SYMBOL_NS_GPL(usb_offload_unregister, "USB_AUDIO_OFFLOAD"); + +/* + * Hotplug bridge - called by the USB SND side + * + * USB audio devices appear and disappear at runtime. Each event is + * delivered to the host via ops->notify_connect / notify_disconnect. On + * connect the USB-SND device identity (@id, card / chip / PCM indices) + * travels with the event so the host can build a struct snd_soc_usb_device + * without reaching into the USB-audio core. + */ +int usb_offload_connect(struct usb_offload_ctx *ctx, struct usb_device *udev, + const struct usb_offload_usb_dev_info *id) +{ + int ret; + + ret = ctx->ops->notify_connect(ctx->dev, udev, id); + if (ret) { + dev_warn(ctx->dev, "USB offload: %s: connect rejected: %d\n", + dev_name(&udev->dev), ret); + return ret; + } + + dev_dbg(ctx->dev, "USB offload: %s connected\n", dev_name(&udev->dev)); + return 0; +} + +void usb_offload_disconnect(struct usb_offload_ctx *ctx, struct usb_device *udev) +{ + ctx->ops->notify_disconnect(ctx->dev, udev); + + dev_dbg(ctx->dev, "USB offload: %s disconnected\n", dev_name(&udev->dev)); +} + +MODULE_LICENSE("GPL"); +MODULE_DESCRIPTION("USB audio offload library"); diff --git a/sound/usb/offload/offload.h b/sound/usb/offload/offload.h new file mode 100644 index 00000000000000..84a79319be6e1c --- /dev/null +++ b/sound/usb/offload/offload.h @@ -0,0 +1,59 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * Internal definitions for the USB audio offload library. + * + * The host callback table lives in ; the host turns + * those callbacks into IPC messages. The ASoC USB framework port is not + * owned here: the host owns the snd_soc_usb port and drives the + * connection callbacks, the library only forwards hotplug events through + * ops->notify_connect / notify_disconnect. + */ + +#ifndef __SOUND_USB_OFFLOAD_LIB_H +#define __SOUND_USB_OFFLOAD_LIB_H + +#include + +/* + * One offload engine. Returned to the host by usb_offload_register() and + * passed back in to every other API call. + */ +struct usb_offload_ctx { + struct device *dev; + const struct usb_offload_ops *ops; +}; + +/* + * Candidate filter + USB-audio device ownership (usb-audio.c) + * + * No dedicated USB driver claims the device: snd-usb-audio keeps + * ownership and notifies us via snd_usb_platform_ops. We check the + * PCMs it has set up and bridge the hotplug event into the library for + * offloadable devices. + */ + +/* + * USB hotplug bridge: how the usb-audio side hands a device to the library, + * which propagates it to the host via ops->notify_connect / notify_disconnect. + */ +int usb_offload_connect(struct usb_offload_ctx *ctx, struct usb_device *udev, + const struct usb_offload_usb_dev_info *id); +void usb_offload_disconnect(struct usb_offload_ctx *ctx, struct usb_device *udev); + +/* + * Register / unregister the snd_usb_platform_ops bridge. init() is called + * from usb_offload_register(), exit() from usb_offload_unregister(). Both + * manage the per-device offload records. + */ +int usb_offload_usb_audio_init(struct usb_offload_ctx *ctx); +void usb_offload_usb_audio_exit(struct usb_offload_ctx *ctx); + +/* + * Re-emit connect events for USB audio devices that enumerated before the + * platform ops were registered. Wraps the usb-audio core's + * snd_usb_rediscover_devices(), which owns the private sound/usb headers; the + * connect callback is idempotent so this is safe to call repeatedly. + */ +void usb_offload_usb_audio_rediscover(void); + +#endif /* __SOUND_USB_OFFLOAD_LIB_H */ diff --git a/sound/usb/offload/usb-audio.c b/sound/usb/offload/usb-audio.c new file mode 100644 index 00000000000000..fc8eaa49648955 --- /dev/null +++ b/sound/usb/offload/usb-audio.c @@ -0,0 +1,608 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * USB audio offload library - USB-audio device ownership + candidate filter. + * + * snd-usb-audio stays in charge of the device. This file hooks the + * snd_usb_platform_ops connect/disconnect notifications of the usb-audio + * core (sound/usb/card.c), checks the device and the PCMs usb-audio set up to decide + * whether a device can be offloaded, and for the candidates it bridges the + * hotplug event into the library via usb_offload_connect() / + * usb_offload_disconnect(). + * + * The xHCI audio sideband resources and the DSP gateway configuration + * happen later, on the PCM data path: usb_offload_acquire_stream() starts + * the stream on the USB side and claims the endpoint for the DSP. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +/* + * snd_usb_audio and the snd_usb_platform_ops bridge live in the private + * usb-audio headers one directory up. The Qualcomm in-tree consumer + * (sound/usb/qcom/) includes them the same way. + */ +#include "../usbaudio.h" +#include "../card.h" +#include "../endpoint.h" +#include "../pcm.h" +#include "../power.h" + +#include "offload.h" + +static bool offload_disable; +module_param(offload_disable, bool, 0444); +MODULE_PARM_DESC(offload_disable, + "Disable USB audio offload candidate selection (default: 0)"); + +/* + * Candidate filter + * + * snd-usb-audio has parsed the descriptors by the time the connect callback + * runs and left the result in chip->pcm_list, with the malformed and the + * unsupported streams already left out. All that is left to decide here is + * whether the device itself can be offloaded; which of its PCMs can is up to + * usb_offload_ua_build_id(). + */ +static bool usb_offload_chip_is_candidate(struct snd_usb_audio *chip) +{ + if (offload_disable) + return false; + + switch (chip->dev->speed) { + case USB_SPEED_FULL: + case USB_SPEED_HIGH: + break; + default: + return false; + } + + /* the audio function the sideband is registered on */ + return chip->ctrl_intf && + chip->ctrl_intf->desc.bInterfaceClass == USB_CLASS_AUDIO; +} + +/* + * Device-ownership bridge (snd_usb_platform_ops) + * + * One module-global registration; the usb-audio core allows only a + * single set of platform ops. We map each offloaded snd_usb_audio chip + * to a record of its offload state. + */ + +/** + * struct usb_offload_ua_stream - one acquired stream of a device. + * @node: link on usb_offload_dev.streams. + * @subs: the claimed usb-audio substream. + * @direction: SNDRV_PCM_STREAM_* of the stream. + * @pcm_index: USB SND PCM device number of @subs. + * + * A device can have several PCMs per direction, and each can be offloaded + * on a stream of its own at the same time. + */ +struct usb_offload_ua_stream { + struct list_head node; + struct snd_usb_substream *subs; + int direction; + int pcm_index; +}; + +/** + * struct usb_offload_dev - per-offloaded-device record. + * @node: link on usb_offload_ua_devs. + * @chip: the owning usb-audio chip. + * @udev: shortcut to chip->dev. + * @connected: true once usb_offload_connect() succeeded. + * @streams: the streams currently acquired on the device. + */ +struct usb_offload_dev { + struct list_head node; + struct snd_usb_audio *chip; + struct usb_device *udev; + bool connected; + struct list_head streams; +}; + +static DEFINE_MUTEX(usb_offload_ua_lock); +static struct usb_offload_ctx *usb_offload_ua_ctx; /* active offload ctx */ +static LIST_HEAD(usb_offload_ua_devs); + +/* Caller holds usb_offload_ua_lock. */ +static struct usb_offload_dev *usb_offload_ua_find(struct snd_usb_audio *chip) +{ + struct usb_offload_dev *dev; + + list_for_each_entry(dev, &usb_offload_ua_devs, node) + if (dev->chip == chip) + return dev; + return NULL; +} + +/* Caller holds usb_offload_ua_lock. */ +static struct usb_offload_dev *usb_offload_ua_find_udev(struct usb_device *udev) +{ + struct usb_offload_dev *dev; + + list_for_each_entry(dev, &usb_offload_ua_devs, node) + if (dev->udev == udev) + return dev; + return NULL; +} + +/* Caller holds usb_offload_ua_lock. */ +static struct usb_offload_ua_stream * +usb_offload_ua_find_stream(struct usb_offload_dev *dev, int direction, int pcm_index) +{ + struct usb_offload_ua_stream *stream; + + list_for_each_entry(stream, &dev->streams, node) + if (stream->direction == direction && stream->pcm_index == pcm_index) + return stream; + return NULL; +} + +/* + * Start the device streaming so that its endpoints, and with them their + * xHCI transfer rings, exist. This is what snd_usb_pcm_prepare() does for + * a host driven stream. Caller holds usb_offload_ua_lock. + */ +static int usb_offload_ua_start_stream(struct usb_offload_dev *dev, + struct snd_usb_substream *subs, + struct snd_pcm_hw_params *params) +{ + struct snd_usb_audio *chip = dev->chip; + int ret; + + ret = snd_usb_offload_claim_substream(subs); + if (ret) + return ret; + + ret = snd_usb_autoresume(chip); + if (ret < 0) + goto err_release; + + ret = snd_usb_hw_params(subs, params); + if (ret < 0) + goto err_suspend; + + if (atomic_read(&chip->shutdown)) { + ret = -ENODEV; + goto err_hw_free; + } + + { + CLASS(snd_usb_lock, pm)(chip); + + if (pm.err < 0) { + ret = pm.err; + goto err_hw_free; + } + + if (subs->sync_endpoint) { + ret = snd_usb_endpoint_prepare(chip, subs->sync_endpoint); + if (ret < 0) + goto err_hw_free; + } + + ret = snd_usb_endpoint_prepare(chip, subs->data_endpoint); + if (ret < 0) + goto err_hw_free; + } + + return 0; + +err_hw_free: + snd_usb_hw_free(subs); +err_suspend: + snd_usb_autosuspend(chip); +err_release: + snd_usb_offload_release_substream(subs); + + return ret; +} + +/* Caller holds usb_offload_ua_lock. */ +static void usb_offload_ua_stop_stream(struct usb_offload_dev *dev, + struct snd_usb_substream *subs) +{ + snd_usb_hw_free(subs); + snd_usb_autosuspend(dev->chip); + snd_usb_offload_release_substream(subs); +} + +/* + * Give the substream back to the host and drop the record. Caller holds + * usb_offload_ua_lock. + */ +static void usb_offload_ua_release(struct usb_offload_dev *dev, + struct usb_offload_ua_stream *stream) +{ + usb_offload_ua_stop_stream(dev, stream->subs); + + list_del(&stream->node); + kfree(stream); +} + +int usb_offload_acquire_stream(struct usb_offload_ctx *ctx, + struct usb_device *udev, int direction, int pcm_index, + struct snd_pcm_hw_params *params, + struct usb_offload_stream_hw *hw) +{ + const struct audioformat *format; + struct usb_host_endpoint *host_ep; + struct usb_offload_ua_stream *stream; + struct snd_usb_substream *subs; + struct usb_offload_dev *dev; + struct snd_usb_stream *as; + int ret; + + if (!ctx || !udev || !params || !hw) + return -EINVAL; + + if (direction < 0 || direction > SNDRV_PCM_STREAM_LAST) + return -EINVAL; + + guard(mutex)(&usb_offload_ua_lock); + + if (usb_offload_ua_ctx != ctx) + return -ENODEV; + + dev = usb_offload_ua_find_udev(udev); + if (!dev || !dev->connected) + return -ENODEV; + + /* + * snd_usb_find_suppported_substream() would do this, but it takes the + * usb-audio register_mutex to turn a card index into the chip. The + * chip is already known here, and the caller is a backend DAI holding + * the ASoC card's pcm_mutex, which that mutex nests inside. + */ + subs = NULL; + list_for_each_entry(as, &dev->chip->pcm_list, list) { + if (!as->pcm || (pcm_index >= 0 && as->pcm->device != pcm_index)) + continue; + if (!as->substream[direction].num_formats) + continue; + + format = snd_usb_find_substream_format(&as->substream[direction], + params); + if (!format) + continue; + subs = &as->substream[direction]; + break; + } + if (!subs) { + dev_dbg(&udev->dev, "USB offload: no %s format for %s %uch %uHz\n", + direction == SNDRV_PCM_STREAM_PLAYBACK ? "playback" : "capture", + snd_pcm_format_name(params_format(params)), + params_channels(params), params_rate(params)); + return -EOPNOTSUPP; + } + + /* a PCM is offloaded once; another PCM of the device may well be running */ + if (usb_offload_ua_find_stream(dev, direction, as->pcm->device)) + return -EBUSY; + + stream = kzalloc_obj(*stream); + if (!stream) + return -ENOMEM; + stream->subs = subs; + stream->direction = direction; + stream->pcm_index = as->pcm->device; + + ret = usb_offload_ua_start_stream(dev, subs, params); + if (ret) + goto err_free; + + host_ep = usb_pipe_endpoint(udev, subs->data_endpoint->pipe); + if (!host_ep) { + ret = -ENXIO; + goto err_stop; + } + + memset(hw, 0, sizeof(*hw)); + hw->pcm_index = stream->pcm_index; + hw->data.ep_addr = host_ep->desc.bEndpointAddress; + + list_add_tail(&stream->node, &dev->streams); + + dev_dbg(&udev->dev, "USB offload: acquired pcm=%d ep=%#x\n", + hw->pcm_index, hw->data.ep_addr); + + return 0; + +err_stop: + usb_offload_ua_stop_stream(dev, subs); +err_free: + kfree(stream); + return ret; +} +EXPORT_SYMBOL_NS_GPL(usb_offload_acquire_stream, "USB_AUDIO_OFFLOAD"); + +int usb_offload_release_stream(struct usb_offload_ctx *ctx, + struct usb_device *udev, int direction, int pcm_index) +{ + struct usb_offload_ua_stream *stream; + struct usb_offload_dev *dev; + + if (!ctx || !udev) + return -EINVAL; + + if (direction < 0 || direction > SNDRV_PCM_STREAM_LAST) + return -EINVAL; + + guard(mutex)(&usb_offload_ua_lock); + + if (usb_offload_ua_ctx != ctx) + return -ENODEV; + + dev = usb_offload_ua_find_udev(udev); + if (!dev) + return -ENODEV; + + stream = usb_offload_ua_find_stream(dev, direction, pcm_index); + if (!stream) + return -ENODEV; + + usb_offload_ua_release(dev, stream); + + return 0; +} +EXPORT_SYMBOL_NS_GPL(usb_offload_release_stream, "USB_AUDIO_OFFLOAD"); + +/* Tear down one record. Caller holds usb_offload_ua_lock. */ +static void usb_offload_ua_teardown(struct usb_offload_dev *dev) +{ + struct usb_offload_ctx *ctx = usb_offload_ua_ctx; + struct usb_offload_ua_stream *stream, *tmp; + + /* + * The host is told first: it ends the streams it runs on the device, + * which stops the DSP driving the endpoints, and quiesces them through + * the stream records, so these have to be still there. Nothing can + * acquire the device anymore once it is not connected. + * + * Runs before the chip is marked shut down, so what the host has not + * given back by the time it returns can still be released properly. + */ + if (dev->connected && ctx) { + dev->connected = false; + mutex_unlock(&usb_offload_ua_lock); + usb_offload_disconnect(ctx, dev->udev); + mutex_lock(&usb_offload_ua_lock); + } + + list_for_each_entry_safe(stream, tmp, &dev->streams, node) + usb_offload_ua_release(dev, stream); + list_del(&dev->node); + kfree(dev); +} + +/* + * Build the USB-SND device identity from the usb-audio chip. This is the + * only place the chip's card / PCM topology is read; the result travels + * to the host (via usb_offload_connect) so that it can construct a + * struct snd_soc_usb_device for the ASoC USB framework without the + * library ever including . + */ +static void usb_offload_ua_build_id(struct snd_usb_audio *chip, + struct usb_offload_usb_dev_info *id) +{ + struct snd_usb_substream *subs; + struct snd_usb_stream *as; + const char *product; + const char *pcm_name; + int index; + + memset(id, 0, sizeof(*id)); + id->card_idx = chip->card ? chip->card->number : -1; + id->chip_idx = chip->index; + product = chip->dev->product ?: "USB Audio"; + + list_for_each_entry(as, &chip->pcm_list, list) { + if (!as->pcm) + continue; + pcm_name = as->pcm->name[0] ? as->pcm->name : "USB PCM"; + + subs = &as->substream[SNDRV_PCM_STREAM_PLAYBACK]; + if (subs->num_formats && id->num_playback < USB_OFFLOAD_MAX_PCM) { + index = id->num_playback++; + id->ppcm_idx[index] = as->pcm->device; + snprintf(id->ppcm_names[index], USB_OFFLOAD_PCM_NAME_LEN, + "%d,%u | %.24s - %.16s", id->card_idx, + as->pcm->device, product, pcm_name); + } + + subs = &as->substream[SNDRV_PCM_STREAM_CAPTURE]; + if (subs->num_formats && id->num_capture < USB_OFFLOAD_MAX_PCM) { + index = id->num_capture++; + id->cpcm_idx[index] = as->pcm->device; + snprintf(id->cpcm_names[index], USB_OFFLOAD_PCM_NAME_LEN, + "%d,%u | %.24s - %.16s", id->card_idx, + as->pcm->device, product, pcm_name); + } + } +} + +static void usb_offload_ua_connect_cb(struct snd_usb_audio *chip) +{ + struct usb_offload_usb_dev_info *id __free(kfree) = NULL; + struct usb_offload_ctx *ctx; + struct usb_offload_dev *dev; + struct usb_device *udev; + int ret; + + if (!chip || !chip->dev) + return; + udev = chip->dev; + + if (!usb_offload_chip_is_candidate(chip)) { + dev_dbg(&udev->dev, "USB offload: device not a candidate\n"); + return; + } + + id = kzalloc_obj(*id); + if (!id) + return; + + mutex_lock(&usb_offload_ua_lock); + + ctx = usb_offload_ua_ctx; + if (!ctx) + goto unlock; + + /* connect_cb fires per interface; offload the chip only once. */ + dev = usb_offload_ua_find(chip); + if (dev) { + bool connected = dev->connected; + + mutex_unlock(&usb_offload_ua_lock); + + if (connected) { + usb_offload_ua_build_id(chip, id); + ret = usb_offload_connect(ctx, udev, id); + if (ret) + dev_dbg(&udev->dev, + "USB offload: re-notify failed: %d\n", + ret); + } + return; + } + + dev = kzalloc_obj(*dev); + if (!dev) + goto unlock; + + dev->chip = chip; + dev->udev = udev; + INIT_LIST_HEAD(&dev->streams); + + list_add_tail(&dev->node, &usb_offload_ua_devs); + mutex_unlock(&usb_offload_ua_lock); + + /* Inform the DSP and the ASoC USB framework. */ + usb_offload_ua_build_id(chip, id); + ret = usb_offload_connect(ctx, udev, id); + + mutex_lock(&usb_offload_ua_lock); + if (ret) { + list_del(&dev->node); + kfree(dev); + goto unlock; + } + dev->connected = true; + mutex_unlock(&usb_offload_ua_lock); + + dev_dbg(&udev->dev, + "USB offload: device offloaded (UAC protocol %#x)\n", + chip->ctrl_intf->desc.bInterfaceProtocol); + return; + +unlock: + mutex_unlock(&usb_offload_ua_lock); +} + +static void usb_offload_ua_disconnect_cb(struct snd_usb_audio *chip) +{ + struct usb_offload_dev *dev; + + if (!chip) + return; + + guard(mutex)(&usb_offload_ua_lock); + + dev = usb_offload_ua_find(chip); + if (dev) { + dev_dbg(&dev->udev->dev, "USB offload: device removed\n"); + usb_offload_ua_teardown(dev); + } +} + +static struct snd_usb_platform_ops usb_offload_ua_ops = { + .connect_cb = usb_offload_ua_connect_cb, + .disconnect_cb = usb_offload_ua_disconnect_cb, +}; + +int usb_offload_usb_audio_init(struct usb_offload_ctx *ctx) +{ + int ret; + + if (!ctx) + return -EINVAL; + + scoped_guard(mutex, &usb_offload_ua_lock) { + if (usb_offload_ua_ctx) + return -EEXIST; + usb_offload_ua_ctx = ctx; + } + + /* + * register/unregister take the usb-audio register_mutex; do not hold + * usb_offload_ua_lock across them to avoid an A-B / B-A inversion with + * the connect/disconnect callbacks (which run under register_mutex and + * then take usb_offload_ua_lock). + */ + ret = snd_usb_register_platform_ops(&usb_offload_ua_ops); + if (ret) { + scoped_guard(mutex, &usb_offload_ua_lock) + usb_offload_ua_ctx = NULL; + return ret; + } + + dev_dbg(ctx->dev, "USB offload: usb-audio bridge registered\n"); + return 0; +} + +void usb_offload_usb_audio_exit(struct usb_offload_ctx *ctx) +{ + struct usb_offload_dev *dev, *tmp; + + scoped_guard(mutex, &usb_offload_ua_lock) { + if (usb_offload_ua_ctx != ctx) + return; + } + + /* + * Let go of every device while snd-usb-audio still reports their + * disconnect, and stop the callbacks. snd_usb_release_platform_ops() + * runs our disconnect callback for each device and unregisters under + * the usb-audio register_mutex, so a device cannot be freed in between + * unnoticed, and once it returns no callback is running and none can + * start. + */ + snd_usb_release_platform_ops(); + + scoped_guard(mutex, &usb_offload_ua_lock) { + /* every record is of a connected chip, there should be none left */ + list_for_each_entry_safe(dev, tmp, &usb_offload_ua_devs, node) + usb_offload_ua_teardown(dev); + usb_offload_ua_ctx = NULL; + } + + dev_dbg(ctx->dev, "USB offload: usb-audio bridge unregistered\n"); +} + +void usb_offload_usb_audio_rediscover(void) +{ + /* + * Walk the usb-audio core's connected-chip array and re-run our + * connect_cb for each. snd_usb_rediscover_devices() does this under + * the usb-audio register_mutex; our callback only re-notifies for + * chips already offloaded rather than setting them up again, and is + * a no-op if the platform ops were never registered, so this is + * always safe. + */ + snd_usb_rediscover_devices(); +} From 2f26a647a12d56daab6b10fd106312a7d0d26959 Mon Sep 17 00:00:00 2001 From: Peter Ujfalusi Date: Wed, 4 Nov 2026 14:24:11 +0200 Subject: [PATCH 19/53] ALSA: usb: offload: Claim the xHCI audio sideband resources The library starts the USB stream for the DSP, but the endpoint is still serviced by the host controller the normal way: nothing routes its traffic to the DSP, and the DSP is not told which endpoint to drive. Register an XHCI_SIDEBAND_AUDIO sideband for the device when it is offloaded, reserve a sideband resource for the data endpoint when a stream is acquired and attach the endpoint to it, which makes the xHCI route the endpoint over the offload link. An endpoint only exists while its streaming altsetting is active, so the resource cannot be reserved ahead of time at connect: it is taken right before binding the endpoint in usb_offload_acquire_stream(), and freed again when the stream is released or the device unplugged, once the endpoint is unbound. Which resource an endpoint ends up on is not always indifferent to the host: the resource selects the stream of the offload link, and a firmware may tie the link's streams to functions, with one set carrying playback and another capture. An endpoint on the wrong stream is then served from the wrong FIFO area. Add an optional get_resource_range() host op telling the library the inclusive resource range an endpoint of a given direction may use. The library passes the range on to xhci_sideband_alloc_resource_id() and fails the acquisition with -EBUSY when all of its resources are taken; it does not fall back to a resource outside the range. A host without the op gets the lowest free resource; an error from the op fails the acquisition. The op is called from the stream acquisition with the library's device lock held, so the host answers from what it knows already and must not call back into the library. Report what the DSP needs to drive the endpoint in usb_offload_stream_hw: the xHCI controller's PCI address and the device's slot, the endpoint's address, packet size and interval, the resource it was given, whether it is reached through a transaction translator, the speed of the root port and the service interval in microseconds. Signed-off-by: Peter Ujfalusi --- include/sound/usb-offload.h | 30 ++++++++ sound/usb/offload/init.c | 18 +++++ sound/usb/offload/offload.h | 4 ++ sound/usb/offload/usb-audio.c | 132 ++++++++++++++++++++++++++++++++-- 4 files changed, 180 insertions(+), 4 deletions(-) diff --git a/include/sound/usb-offload.h b/include/sound/usb-offload.h index 29d1e909f20cae..4f3d0bf04ff4ef 100644 --- a/include/sound/usb-offload.h +++ b/include/sound/usb-offload.h @@ -71,6 +71,14 @@ struct usb_offload_usb_dev_info { * struct usb_offload_ops - host callback table, library to host * @notify_connect: an offloadable device appeared * @notify_disconnect: an offloaded device went away + * @get_resource_range: the sideband resources an endpoint may be placed on, + * as an inclusive range of xHCI Audio Sideband resource + * IDs, @first to @last. @direction is the SNDRV_PCM_STREAM_* + * value of the stream. Optional: + * an offload engine whose firmware ties a function to + * particular resources answers, one that does not care + * leaves it unset and the lowest free resource is taken. + * An error fails the acquisition of the stream. * * @notify_connect and @notify_disconnect are mandatory. All callbacks run in * process context. @@ -79,6 +87,9 @@ struct usb_offload_ops { int (*notify_connect)(struct device *dev, struct usb_device *udev, const struct usb_offload_usb_dev_info *id); int (*notify_disconnect)(struct device *dev, struct usb_device *udev); + + int (*get_resource_range)(struct device *dev, int direction, + unsigned int *first, unsigned int *last); }; /** @@ -97,23 +108,42 @@ void usb_offload_unregister(struct usb_offload_ctx *ctx); /** * struct usb_offload_ep_info - an endpoint handed to the DSP * @ep_addr: bEndpointAddress of the endpoint + * @resource: xHCI Audio Sideband resource ID the endpoint is bound to + * @maxpacket: wMaxPacketSize of the endpoint + * @interval: bInterval of the endpoint */ struct usb_offload_ep_info { u8 ep_addr; + u8 resource; + u16 maxpacket; + u8 interval; }; /** * struct usb_offload_stream_hw - resources reserved for one offloaded stream * @pcm_index: USB SND PCM device number the stream was acquired on, to be * passed back to usb_offload_release_stream() + * @bus_devid: PCI bus, device and function of the xHCI controller, as + * PCI_DEVID() lays them out + * @slot_id: xHCI slot ID of the USB device + * @high_speed: the root port the device hangs off runs at high speed + * @split: the endpoint is reached through a USB 2.0 transaction translator * @data: the isochronous data endpoint + * @service_interval_us: service interval of @data in microseconds: a multiple + * of a 1 ms frame for a full speed device, of a 125 us + * microframe for a high speed one * * Filled in by usb_offload_acquire_stream() and released again by * usb_offload_release_stream(). */ struct usb_offload_stream_hw { int pcm_index; + u16 bus_devid; + u8 slot_id; + bool high_speed; + bool split; struct usb_offload_ep_info data; + unsigned int service_interval_us; }; /* diff --git a/sound/usb/offload/init.c b/sound/usb/offload/init.c index c2a9434fefc039..b0855d0d5f311f 100644 --- a/sound/usb/offload/init.c +++ b/sound/usb/offload/init.c @@ -103,5 +103,23 @@ void usb_offload_disconnect(struct usb_offload_ctx *ctx, struct usb_device *udev dev_dbg(ctx->dev, "USB offload: %s disconnected\n", dev_name(&udev->dev)); } +/* + * Resource placement + * + * Runs from the stream acquisition path, under the usb-audio side's lock. + */ +int usb_offload_resource_range(struct usb_offload_ctx *ctx, int direction, + unsigned int *first, unsigned int *last) +{ + if (ctx->ops->get_resource_range) + return ctx->ops->get_resource_range(ctx->dev, direction, + first, last); + + *first = 0; + *last = 0; + + return 0; +} + MODULE_LICENSE("GPL"); MODULE_DESCRIPTION("USB audio offload library"); diff --git a/sound/usb/offload/offload.h b/sound/usb/offload/offload.h index 84a79319be6e1c..346aee71e39555 100644 --- a/sound/usb/offload/offload.h +++ b/sound/usb/offload/offload.h @@ -23,6 +23,10 @@ struct usb_offload_ctx { const struct usb_offload_ops *ops; }; +/* Where the host wants a stream's endpoint placed (called from usb-audio.c) */ +int usb_offload_resource_range(struct usb_offload_ctx *ctx, int direction, + unsigned int *first, unsigned int *last); + /* * Candidate filter + USB-audio device ownership (usb-audio.c) * diff --git a/sound/usb/offload/usb-audio.c b/sound/usb/offload/usb-audio.c index fc8eaa49648955..4fb69b247f6e53 100644 --- a/sound/usb/offload/usb-audio.c +++ b/sound/usb/offload/usb-audio.c @@ -19,9 +19,11 @@ #include #include #include +#include #include #include #include +#include #include #include @@ -29,6 +31,9 @@ #include #include +/* PORTSC Port Speed ID of a high-speed root port, xHCI 7.2.2.1.1 */ +#define USB_OFFLOAD_PORT_SPEED_HIGH 3 + /* * snd_usb_audio and the snd_usb_platform_ops bridge live in the private * usb-audio headers one directory up. The Qualcomm in-tree consumer @@ -88,6 +93,8 @@ static bool usb_offload_chip_is_candidate(struct snd_usb_audio *chip) * @subs: the claimed usb-audio substream. * @direction: SNDRV_PCM_STREAM_* of the stream. * @pcm_index: USB SND PCM device number of @subs. + * @resource: sideband resource ID of the data endpoint. + * @data_host_ep: the data endpoint. * * A device can have several PCMs per direction, and each can be offloaded * on a stream of its own at the same time. @@ -97,6 +104,8 @@ struct usb_offload_ua_stream { struct snd_usb_substream *subs; int direction; int pcm_index; + u8 resource; + struct usb_host_endpoint *data_host_ep; }; /** @@ -104,6 +113,7 @@ struct usb_offload_ua_stream { * @node: link on usb_offload_ua_devs. * @chip: the owning usb-audio chip. * @udev: shortcut to chip->dev. + * @sb: xHCI sideband instance for this device. * @connected: true once usb_offload_connect() succeeded. * @streams: the streams currently acquired on the device. */ @@ -111,6 +121,7 @@ struct usb_offload_dev { struct list_head node; struct snd_usb_audio *chip; struct usb_device *udev; + struct xhci_sideband *sb; bool connected; struct list_head streams; }; @@ -153,6 +164,14 @@ usb_offload_ua_find_stream(struct usb_offload_dev *dev, int direction, int pcm_i return NULL; } +static void usb_offload_fill_ep(struct usb_offload_ep_info *desc, + const struct usb_host_endpoint *host_ep) +{ + desc->ep_addr = host_ep->desc.bEndpointAddress; + desc->maxpacket = usb_endpoint_maxp(&host_ep->desc); + desc->interval = host_ep->desc.bInterval; +} + /* * Start the device streaming so that its endpoints, and with them their * xHCI transfer rings, exist. This is what snd_usb_pcm_prepare() does for @@ -223,18 +242,51 @@ static void usb_offload_ua_stop_stream(struct usb_offload_dev *dev, } /* - * Give the substream back to the host and drop the record. Caller holds + * Detach the stream's endpoint from its sideband resource, give the + * substream back to the host and drop the record. Caller holds * usb_offload_ua_lock. */ static void usb_offload_ua_release(struct usb_offload_dev *dev, struct usb_offload_ua_stream *stream) { + if (stream->data_host_ep) { + xhci_sideband_remove_endpoint(dev->sb, stream->data_host_ep); + xhci_sideband_free_resource_id(dev->sb, stream->resource); + } + usb_offload_ua_stop_stream(dev, stream->subs); list_del(&stream->node); kfree(stream); } +/* + * Reserve the sideband resource @host_ep is offloaded on. The host says + * where an endpoint of this function belongs when its engine ties the + * resources to functions, and the first free one of those is taken; a host + * without an opinion gets the first free resource of the controller. + * Caller holds usb_offload_ua_lock. + */ +static int usb_offload_ua_alloc_resource(struct usb_offload_dev *dev, + struct usb_host_endpoint *host_ep, + int direction) +{ + unsigned int first, last; + int ret; + + ret = usb_offload_resource_range(usb_offload_ua_ctx, direction, + &first, &last); + if (ret) + return ret; + + ret = xhci_sideband_alloc_resource_id(dev->sb, host_ep, first, last); + if (ret == -EBUSY) + dev_dbg(&dev->udev->dev, "USB offload: no free resource in %u-%u\n", + first, last); + + return ret; +} + int usb_offload_acquire_stream(struct usb_offload_ctx *ctx, struct usb_device *udev, int direction, int pcm_index, struct snd_pcm_hw_params *params, @@ -245,7 +297,10 @@ int usb_offload_acquire_stream(struct usb_offload_ctx *ctx, struct usb_offload_ua_stream *stream; struct snd_usb_substream *subs; struct usb_offload_dev *dev; + struct xhci_sideband_ep_properties ep_prop; struct snd_usb_stream *as; + u8 interval; + int res_id; int ret; if (!ctx || !udev || !params || !hw) @@ -312,9 +367,45 @@ int usb_offload_acquire_stream(struct usb_offload_ctx *ctx, goto err_stop; } + /* the endpoint exists only now, so the resource is reserved here */ + ret = usb_offload_ua_alloc_resource(dev, host_ep, direction); + if (ret < 0) + goto err_stop; + res_id = ret; + + ret = xhci_sideband_add_endpoint(dev->sb, host_ep); + if (ret) + goto err_free_data; + + ret = xhci_sideband_get_ep_properties(dev->sb, host_ep, &ep_prop); + if (ret) + goto err_remove_data; + memset(hw, 0, sizeof(*hw)); hw->pcm_index = stream->pcm_index; - hw->data.ep_addr = host_ep->desc.bEndpointAddress; + hw->slot_id = ep_prop.slot_id; + hw->bus_devid = PCI_DEVID(ep_prop.bus, + PCI_DEVFN(ep_prop.device, ep_prop.function)); + usb_offload_fill_ep(&hw->data, host_ep); + hw->data.resource = ep_prop.res_id; + stream->resource = res_id; + stream->data_host_ep = host_ep; + hw->split = ep_prop.tt; + /* + * The root port's speed is what the link runs at, also for a full + * speed device behind a high speed hub; the service interval is in + * the device's own frame units, so that one goes by udev->speed. + */ + hw->high_speed = ep_prop.port_speed == USB_OFFLOAD_PORT_SPEED_HIGH; + + /* + * An isochronous bInterval is an exponent at both speeds, in frames + * (1 ms) at full speed and in microframes (125 us) at high speed, which + * is how the xHC schedules the endpoint (xhci_parse_exponent_interval). + */ + interval = clamp_t(u8, hw->data.interval, 1, 16); + hw->service_interval_us = (udev->speed == USB_SPEED_HIGH ? 125 : 1000) << + (interval - 1); list_add_tail(&stream->node, &dev->streams); @@ -323,6 +414,10 @@ int usb_offload_acquire_stream(struct usb_offload_ctx *ctx, return 0; +err_remove_data: + xhci_sideband_remove_endpoint(dev->sb, host_ep); +err_free_data: + xhci_sideband_free_resource_id(dev->sb, res_id); err_stop: usb_offload_ua_stop_stream(dev, subs); err_free: @@ -386,6 +481,8 @@ static void usb_offload_ua_teardown(struct usb_offload_dev *dev) list_for_each_entry_safe(stream, tmp, &dev->streams, node) usb_offload_ua_release(dev, stream); + + xhci_sideband_unregister(dev->sb); list_del(&dev->node); kfree(dev); } @@ -441,6 +538,7 @@ static void usb_offload_ua_connect_cb(struct snd_usb_audio *chip) struct usb_offload_usb_dev_info *id __free(kfree) = NULL; struct usb_offload_ctx *ctx; struct usb_offload_dev *dev; + struct usb_interface *audctrl; struct usb_device *udev; int ret; @@ -488,6 +586,28 @@ static void usb_offload_ua_connect_cb(struct snd_usb_audio *chip) dev->chip = chip; dev->udev = udev; INIT_LIST_HEAD(&dev->streams); + audctrl = usb_ifnum_to_if(udev, chip->ctrl_intf->desc.bInterfaceNumber); + if (!audctrl) { + ret = -ENODEV; + goto free_dev_unlock; + } + + /* + * The first registration on a controller asks it for its sideband + * support with a command, which it cannot run while suspended. A + * device found again by the rediscovery may be autosuspended by then, + * and the controller with it. + */ + ret = usb_autopm_get_interface(audctrl); + if (ret) + goto free_dev_unlock; + + dev->sb = xhci_sideband_register(audctrl, XHCI_SIDEBAND_AUDIO, NULL); + usb_autopm_put_interface(audctrl); + if (!dev->sb) { + ret = -EOPNOTSUPP; + goto free_dev_unlock; + } list_add_tail(&dev->node, &usb_offload_ua_devs); mutex_unlock(&usb_offload_ua_lock); @@ -499,8 +619,7 @@ static void usb_offload_ua_connect_cb(struct snd_usb_audio *chip) mutex_lock(&usb_offload_ua_lock); if (ret) { list_del(&dev->node); - kfree(dev); - goto unlock; + goto unregister_sb; } dev->connected = true; mutex_unlock(&usb_offload_ua_lock); @@ -510,6 +629,11 @@ static void usb_offload_ua_connect_cb(struct snd_usb_audio *chip) chip->ctrl_intf->desc.bInterfaceProtocol); return; +unregister_sb: + xhci_sideband_unregister(dev->sb); +free_dev_unlock: + dev_dbg(&udev->dev, "USB offload: sideband setup failed: %d\n", ret); + kfree(dev); unlock: mutex_unlock(&usb_offload_ua_lock); } From 6c65da3267abe74a5a26e74de435f8d6faf5ee6a Mon Sep 17 00:00:00 2001 From: Peter Ujfalusi Date: Wed, 4 Nov 2026 14:25:33 +0200 Subject: [PATCH 20/53] ALSA: usb: offload: Reserve the isochronous feedback endpoint An asynchronous playback stream has a clock feedback endpoint the sender has to follow. The library only binds the data endpoint to the sideband, so the DSP driving the data endpoint would run without the feedback, while the host, which does not service the stream, still gets the feedback packets. Reserve a sideband resource for the feedback endpoint of the selected alternate setting as well, bind it next to the data endpoint and report it in usb_offload_stream_hw, so the host can hand it to the DSP. Only an explicit feedback endpoint is claimed: implicit feedback redirects the sync source to another interface's capture data endpoint (e.g. the Behringer UMC204HD/UMC404HD family, QUIRK_FLAG_GENERIC_IMPLICIT_FB), which carries audio samples rather than a feedback value the DSP can interpret. Such a stream is reported without feedback. The firmware reads the clock feedback of the playback streams from streams of their own, so the get_resource_range() host op is told whether the endpoint it is asked about is the feedback one: the op takes a feedback argument, which the library passes on. The selected host endpoints are kept with the stream so that the release and the unplug paths remove the same endpoints before USB audio frees their rings. Suggested-by: Serhiy Katsyuba Signed-off-by: Peter Ujfalusi --- include/sound/usb-offload.h | 15 +++- sound/usb/offload/init.c | 5 +- sound/usb/offload/offload.h | 3 +- sound/usb/offload/usb-audio.c | 135 ++++++++++++++++++++++++++++------ 4 files changed, 130 insertions(+), 28 deletions(-) diff --git a/include/sound/usb-offload.h b/include/sound/usb-offload.h index 4f3d0bf04ff4ef..2ceed9056221b5 100644 --- a/include/sound/usb-offload.h +++ b/include/sound/usb-offload.h @@ -74,7 +74,8 @@ struct usb_offload_usb_dev_info { * @get_resource_range: the sideband resources an endpoint may be placed on, * as an inclusive range of xHCI Audio Sideband resource * IDs, @first to @last. @direction is the SNDRV_PCM_STREAM_* - * value of the stream. Optional: + * value of the stream and @feedback tells a playback stream's + * clock feedback endpoint from its data endpoint. Optional: * an offload engine whose firmware ties a function to * particular resources answers, one that does not care * leaves it unset and the lowest free resource is taken. @@ -88,7 +89,7 @@ struct usb_offload_ops { const struct usb_offload_usb_dev_info *id); int (*notify_disconnect)(struct device *dev, struct usb_device *udev); - int (*get_resource_range)(struct device *dev, int direction, + int (*get_resource_range)(struct device *dev, int direction, bool feedback, unsigned int *first, unsigned int *last); }; @@ -110,7 +111,10 @@ void usb_offload_unregister(struct usb_offload_ctx *ctx); * @ep_addr: bEndpointAddress of the endpoint * @resource: xHCI Audio Sideband resource ID the endpoint is bound to * @maxpacket: wMaxPacketSize of the endpoint - * @interval: bInterval of the endpoint + * @interval: period of the endpoint as an isochronous bInterval, 2^(n-1) + * frames (full speed) or microframes (high speed). For a feedback + * endpoint this is the refresh period the host would poll it at, + * which a UAC1 device states in bRefresh rather than bInterval. */ struct usb_offload_ep_info { u8 ep_addr; @@ -129,6 +133,9 @@ struct usb_offload_ep_info { * @high_speed: the root port the device hangs off runs at high speed * @split: the endpoint is reached through a USB 2.0 transaction translator * @data: the isochronous data endpoint + * @feedback: the explicit clock feedback endpoint of a playback stream, + * valid when @has_feedback is set + * @has_feedback: @feedback has been reserved for the DSP * @service_interval_us: service interval of @data in microseconds: a multiple * of a 1 ms frame for a full speed device, of a 125 us * microframe for a high speed one @@ -143,6 +150,8 @@ struct usb_offload_stream_hw { bool high_speed; bool split; struct usb_offload_ep_info data; + struct usb_offload_ep_info feedback; + bool has_feedback; unsigned int service_interval_us; }; diff --git a/sound/usb/offload/init.c b/sound/usb/offload/init.c index b0855d0d5f311f..fba3ebc5bceb45 100644 --- a/sound/usb/offload/init.c +++ b/sound/usb/offload/init.c @@ -109,11 +109,12 @@ void usb_offload_disconnect(struct usb_offload_ctx *ctx, struct usb_device *udev * Runs from the stream acquisition path, under the usb-audio side's lock. */ int usb_offload_resource_range(struct usb_offload_ctx *ctx, int direction, - unsigned int *first, unsigned int *last) + bool feedback, unsigned int *first, + unsigned int *last) { if (ctx->ops->get_resource_range) return ctx->ops->get_resource_range(ctx->dev, direction, - first, last); + feedback, first, last); *first = 0; *last = 0; diff --git a/sound/usb/offload/offload.h b/sound/usb/offload/offload.h index 346aee71e39555..5937df5e0042fa 100644 --- a/sound/usb/offload/offload.h +++ b/sound/usb/offload/offload.h @@ -25,7 +25,8 @@ struct usb_offload_ctx { /* Where the host wants a stream's endpoint placed (called from usb-audio.c) */ int usb_offload_resource_range(struct usb_offload_ctx *ctx, int direction, - unsigned int *first, unsigned int *last); + bool feedback, unsigned int *first, + unsigned int *last); /* * Candidate filter + USB-audio device ownership (usb-audio.c) diff --git a/sound/usb/offload/usb-audio.c b/sound/usb/offload/usb-audio.c index 4fb69b247f6e53..ab12515e3f0d35 100644 --- a/sound/usb/offload/usb-audio.c +++ b/sound/usb/offload/usb-audio.c @@ -94,7 +94,9 @@ static bool usb_offload_chip_is_candidate(struct snd_usb_audio *chip) * @direction: SNDRV_PCM_STREAM_* of the stream. * @pcm_index: USB SND PCM device number of @subs. * @resource: sideband resource ID of the data endpoint. + * @fb_resource: sideband resource ID of the feedback endpoint, 0 if none. * @data_host_ep: the data endpoint. + * @feedback_host_ep: the feedback endpoint, NULL if none. * * A device can have several PCMs per direction, and each can be offloaded * on a stream of its own at the same time. @@ -105,7 +107,9 @@ struct usb_offload_ua_stream { int direction; int pcm_index; u8 resource; + u8 fb_resource; struct usb_host_endpoint *data_host_ep; + struct usb_host_endpoint *feedback_host_ep; }; /** @@ -242,17 +246,46 @@ static void usb_offload_ua_stop_stream(struct usb_offload_dev *dev, } /* - * Detach the stream's endpoint from its sideband resource, give the + * Detach @host_ep from its sideband resource and free the resource. -ENODEV + * means the endpoint is not attached to the sideband (any more), nothing + * holds on to the resource then. On any other failure the xHC may still have + * the endpoint on the resource, so the reservation is kept rather than given + * to the next stream: the endpoint cannot be offloaded again until the + * sideband goes away with the device, but no two endpoints share a resource. + */ +static void usb_offload_ua_unclaim_ep(struct usb_offload_dev *dev, + struct usb_host_endpoint *host_ep, int res_id) +{ + int ret; + + ret = xhci_sideband_remove_endpoint(dev->sb, host_ep); + if (ret && ret != -ENODEV) { + dev_warn(&dev->udev->dev, + "USB offload: ep %#x not detached (%d), keeping resource %d\n", + host_ep->desc.bEndpointAddress, ret, res_id); + return; + } + + ret = xhci_sideband_free_resource_id(dev->sb, res_id); + if (ret) + dev_warn(&dev->udev->dev, + "USB offload: ep %#x resource %d not freed: %d\n", + host_ep->desc.bEndpointAddress, res_id, ret); +} + +/* + * Detach the stream's endpoints from their sideband resources, give the * substream back to the host and drop the record. Caller holds * usb_offload_ua_lock. */ static void usb_offload_ua_release(struct usb_offload_dev *dev, struct usb_offload_ua_stream *stream) { - if (stream->data_host_ep) { - xhci_sideband_remove_endpoint(dev->sb, stream->data_host_ep); - xhci_sideband_free_resource_id(dev->sb, stream->resource); - } + if (stream->feedback_host_ep) + usb_offload_ua_unclaim_ep(dev, stream->feedback_host_ep, + stream->fb_resource); + if (stream->data_host_ep) + usb_offload_ua_unclaim_ep(dev, stream->data_host_ep, stream->resource); usb_offload_ua_stop_stream(dev, stream->subs); @@ -269,20 +302,51 @@ static void usb_offload_ua_release(struct usb_offload_dev *dev, */ static int usb_offload_ua_alloc_resource(struct usb_offload_dev *dev, struct usb_host_endpoint *host_ep, - int direction) + int direction, bool feedback) { unsigned int first, last; int ret; - ret = usb_offload_resource_range(usb_offload_ua_ctx, direction, + ret = usb_offload_resource_range(usb_offload_ua_ctx, direction, feedback, &first, &last); if (ret) return ret; ret = xhci_sideband_alloc_resource_id(dev->sb, host_ep, first, last); if (ret == -EBUSY) - dev_dbg(&dev->udev->dev, "USB offload: no free resource in %u-%u\n", - first, last); + dev_dbg(&dev->udev->dev, "USB offload: no free %s resource in %u-%u\n", + feedback ? "feedback" : "data", first, last); + + return ret; +} + +/* + * Reserve a sideband resource for @host_ep, attach the endpoint to it and read + * back what the xHC knows about it. Undone by usb_offload_ua_unclaim_ep(). + * Caller holds usb_offload_ua_lock. + */ +static int usb_offload_ua_claim_ep(struct usb_offload_dev *dev, + struct usb_host_endpoint *host_ep, int direction, + bool feedback, int *res_id, + struct xhci_sideband_ep_properties *prop) +{ + int ret; + + ret = usb_offload_ua_alloc_resource(dev, host_ep, direction, feedback); + if (ret < 0) + return ret; + *res_id = ret; + + /* nothing is attached when the add fails, only the reservation is undone */ + ret = xhci_sideband_add_endpoint(dev->sb, host_ep); + if (ret) { + xhci_sideband_free_resource_id(dev->sb, *res_id); + return ret; + } + + ret = xhci_sideband_get_ep_properties(dev->sb, host_ep, prop); + if (ret) + usb_offload_ua_unclaim_ep(dev, host_ep, *res_id); return ret; } @@ -294,11 +358,14 @@ int usb_offload_acquire_stream(struct usb_offload_ctx *ctx, { const struct audioformat *format; struct usb_host_endpoint *host_ep; + struct usb_host_endpoint *fb_host_ep = NULL; struct usb_offload_ua_stream *stream; struct snd_usb_substream *subs; struct usb_offload_dev *dev; struct xhci_sideband_ep_properties ep_prop; + struct xhci_sideband_ep_properties fb_ep_prop; struct snd_usb_stream *as; + int fb_res_id = 0; u8 interval; int res_id; int ret; @@ -368,18 +435,29 @@ int usb_offload_acquire_stream(struct usb_offload_ctx *ctx, } /* the endpoint exists only now, so the resource is reserved here */ - ret = usb_offload_ua_alloc_resource(dev, host_ep, direction); - if (ret < 0) + ret = usb_offload_ua_claim_ep(dev, host_ep, direction, false, &res_id, &ep_prop); + if (ret) goto err_stop; - res_id = ret; - ret = xhci_sideband_add_endpoint(dev->sb, host_ep); - if (ret) - goto err_free_data; + /* + * Implicit feedback reuses another interface's capture data endpoint + * as a sync source; it carries audio samples, not a feedback value + * the DSP can interpret, so only an explicit sync endpoint is claimed. + * usb-audio flags the data endpoint, which syncs with the implicit + * feedback, not the capture endpoint it syncs to. + */ + if (subs->sync_endpoint && !subs->data_endpoint->implicit_fb_sync) { + fb_host_ep = usb_pipe_endpoint(udev, subs->sync_endpoint->pipe); + if (!fb_host_ep) { + ret = -ENXIO; + goto err_unclaim_data; + } - ret = xhci_sideband_get_ep_properties(dev->sb, host_ep, &ep_prop); - if (ret) - goto err_remove_data; + ret = usb_offload_ua_claim_ep(dev, fb_host_ep, direction, true, + &fb_res_id, &fb_ep_prop); + if (ret) + goto err_unclaim_data; + } memset(hw, 0, sizeof(*hw)); hw->pcm_index = stream->pcm_index; @@ -389,7 +467,22 @@ int usb_offload_acquire_stream(struct usb_offload_ctx *ctx, usb_offload_fill_ep(&hw->data, host_ep); hw->data.resource = ep_prop.res_id; stream->resource = res_id; + stream->fb_resource = fb_res_id; stream->data_host_ep = host_ep; + stream->feedback_host_ep = fb_host_ep; + if (fb_host_ep) { + usb_offload_fill_ep(&hw->feedback, fb_host_ep); + hw->feedback.resource = fb_ep_prop.res_id; + /* + * The period the feedback value is refreshed at, the one + * snd-usb-audio polls its sync endpoint at: bRefresh of a UAC1 + * endpoint, otherwise 2 frames at full speed and bInterval at + * high speed, 1 ms when that is not usable either. It is kept + * as log2 in syncinterval, here in bInterval form. + */ + hw->feedback.interval = subs->sync_endpoint->syncinterval + 1; + hw->has_feedback = true; + } hw->split = ep_prop.tt; /* * The root port's speed is what the link runs at, also for a full @@ -414,10 +507,8 @@ int usb_offload_acquire_stream(struct usb_offload_ctx *ctx, return 0; -err_remove_data: - xhci_sideband_remove_endpoint(dev->sb, host_ep); -err_free_data: - xhci_sideband_free_resource_id(dev->sb, res_id); +err_unclaim_data: + usb_offload_ua_unclaim_ep(dev, host_ep, res_id); err_stop: usb_offload_ua_stop_stream(dev, subs); err_free: From e6826e868ddb15456316e59118aa2d0bb86b7271 Mon Sep 17 00:00:00 2001 From: Peter Ujfalusi Date: Wed, 4 Nov 2026 14:26:26 +0200 Subject: [PATCH 21/53] ALSA: usb: offload: Refine hw params to the device's capabilities The host's PCM advertises whatever its own pipeline was configured for, which has nothing to do with the USB device that happens to be connected. The mismatch only shows at hw_params time, when the acquisition finds no alternate setting for the negotiated format. Add usb_offload_refine_hw_params() for the host to install as hardware rules on its PCM. It narrows the rate, the channel count or the format to the combinations one alternate setting of the selected USB PCM can serve, considering what the other two parameters still allow, so that userspace negotiates against the device. The selection follows the same pcm_index as the acquisition: one USB PCM of the device, or any of them. Signed-off-by: Peter Ujfalusi --- include/sound/usb-offload.h | 11 +++ sound/usb/offload/usb-audio.c | 134 ++++++++++++++++++++++++++++++++++ 2 files changed, 145 insertions(+) diff --git a/include/sound/usb-offload.h b/include/sound/usb-offload.h index 2ceed9056221b5..11c64b3a5659f1 100644 --- a/include/sound/usb-offload.h +++ b/include/sound/usb-offload.h @@ -180,4 +180,15 @@ int usb_offload_release_stream(struct usb_offload_ctx *ctx, struct usb_device *udev, int direction, int pcm_index); +/* + * Narrow one of SNDRV_PCM_HW_PARAM_RATE, _CHANNELS or _FORMAT (@var) in + * @params to what the selected USB PCM of @udev can run at, considering the + * other two parameters, as a hardware rule of the host's PCM would. The + * selection is the same as for usb_offload_acquire_stream(). + */ +int usb_offload_refine_hw_params(struct usb_offload_ctx *ctx, + struct usb_device *udev, int direction, + int pcm_index, int var, + struct snd_pcm_hw_params *params); + #endif /* __SOUND_USB_OFFLOAD_H */ diff --git a/sound/usb/offload/usb-audio.c b/sound/usb/offload/usb-audio.c index ab12515e3f0d35..06b22bafda484c 100644 --- a/sound/usb/offload/usb-audio.c +++ b/sound/usb/offload/usb-audio.c @@ -548,6 +548,140 @@ int usb_offload_release_stream(struct usb_offload_ctx *ctx, } EXPORT_SYMBOL_NS_GPL(usb_offload_release_stream, "USB_AUDIO_OFFLOAD"); +static bool usb_offload_fmt_usable(const struct audioformat *fp, + struct snd_pcm_hw_params *params) +{ + struct snd_interval *c = hw_param_interval(params, + SNDRV_PCM_HW_PARAM_CHANNELS); + struct snd_interval *r = hw_param_interval(params, + SNDRV_PCM_HW_PARAM_RATE); + struct snd_mask fmts; + + snd_mask_none(&fmts); + fmts.bits[0] = lower_32_bits(fp->formats); + fmts.bits[1] = upper_32_bits(fp->formats); + snd_mask_intersect(&fmts, + hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT)); + if (snd_mask_empty(&fmts)) + return false; + + if (fp->channels < c->min || fp->channels > c->max) + return false; + + if (fp->rate_min > r->max || + (fp->rate_min == r->max && r->openmax)) + return false; + if (fp->rate_max < r->min || + (fp->rate_max == r->min && r->openmin)) + return false; + + return true; +} + +static void usb_offload_fmt_rates(const struct audioformat *fp, + struct snd_pcm_hw_params *params, + unsigned int *rmin, unsigned int *rmax) +{ + struct snd_interval *r = hw_param_interval(params, + SNDRV_PCM_HW_PARAM_RATE); + unsigned int i; + + if (!fp->rate_table || !fp->nr_rates) { + *rmin = min(*rmin, fp->rate_min); + *rmax = max(*rmax, fp->rate_max); + return; + } + + for (i = 0; i < fp->nr_rates; i++) { + if (!snd_interval_test(r, fp->rate_table[i])) + continue; + + *rmin = min(*rmin, fp->rate_table[i]); + *rmax = max(*rmax, fp->rate_table[i]); + } +} + +int usb_offload_refine_hw_params(struct usb_offload_ctx *ctx, + struct usb_device *udev, int direction, + int pcm_index, int var, + struct snd_pcm_hw_params *params) +{ + unsigned int rmin = UINT_MAX, rmax = 0; + struct snd_usb_substream *subs; + struct usb_offload_dev *dev; + const struct audioformat *fp; + struct snd_usb_stream *as; + struct snd_interval t; + struct snd_mask m; + u64 formats = 0; + + if (!ctx || !udev || !params) + return -EINVAL; + + if (direction < 0 || direction > SNDRV_PCM_STREAM_LAST) + return -EINVAL; + + if (var != SNDRV_PCM_HW_PARAM_RATE && + var != SNDRV_PCM_HW_PARAM_CHANNELS && + var != SNDRV_PCM_HW_PARAM_FORMAT) + return -EINVAL; + + guard(mutex)(&usb_offload_ua_lock); + + if (usb_offload_ua_ctx != ctx) + return 0; + + dev = usb_offload_ua_find_udev(udev); + if (!dev || !dev->connected) + return 0; + + list_for_each_entry(as, &dev->chip->pcm_list, list) { + if (pcm_index >= 0 && + (!as->pcm || as->pcm->device != pcm_index)) + continue; + + subs = &as->substream[direction]; + if (!subs->ep_num) + continue; + + list_for_each_entry(fp, &subs->fmt_list, list) { + if (!usb_offload_fmt_usable(fp, params)) + continue; + + switch (var) { + case SNDRV_PCM_HW_PARAM_RATE: + usb_offload_fmt_rates(fp, params, &rmin, &rmax); + break; + case SNDRV_PCM_HW_PARAM_CHANNELS: + rmin = min(rmin, fp->channels); + rmax = max(rmax, fp->channels); + break; + case SNDRV_PCM_HW_PARAM_FORMAT: + formats |= fp->formats; + break; + } + } + } + + if (var == SNDRV_PCM_HW_PARAM_FORMAT) { + snd_mask_none(&m); + m.bits[0] = lower_32_bits(formats); + m.bits[1] = upper_32_bits(formats); + + return snd_mask_refine(hw_param_mask(params, var), &m); + } + + if (rmin > rmax) { + hw_param_interval(params, var)->empty = 1; + return -EINVAL; + } + + t = (struct snd_interval){ .min = rmin, .max = rmax, .integer = 1 }; + + return snd_interval_refine(hw_param_interval(params, var), &t); +} +EXPORT_SYMBOL_NS_GPL(usb_offload_refine_hw_params, "USB_AUDIO_OFFLOAD"); + /* Tear down one record. Caller holds usb_offload_ua_lock. */ static void usb_offload_ua_teardown(struct usb_offload_dev *dev) { From 6ae3239bbb7da77f2ff4c8400b9448c32d436557 Mon Sep 17 00:00:00 2001 From: Peter Ujfalusi Date: Wed, 2 Sep 2026 15:50:42 +0300 Subject: [PATCH 22/53] ALSA: usb: offload: Let a host treat S24_3LE as S24_LE S24_3LE and S24_LE carry the same 24 valid bits and differ only in the container size. A host whose pipeline only knows S24_LE cannot negotiate 24-bit audio with a device that only offers S24_3LE, while a host that can consume S24_3LE directly should see the device as it is. Add an opt-in s24_3le_as_s24_le flag to usb_offload_res. When set, usb_offload_refine_hw_params() reports a device's S24_3LE as S24_LE, and usb_offload_acquire_stream() asks the device for the container it actually has: when no PCM can run the S24_LE the host negotiated, it looks again with S24_3LE and reports the format the stream runs in as usb_offload_stream_hw.format, for the host to set its link up with. The lookup considers every PCM for the exact format before it falls back, so a PCM with S24_LE is preferred to one with S24_3LE. Split the format independent part of the usable-alternate-setting test into usb_offload_fmt_in_range(): refining the format has to consider an alternate setting by rate and channels alone, a format-first check would rule it out before the folding gets a chance to run. Signed-off-by: Peter Ujfalusi --- include/sound/usb-offload.h | 14 +++ sound/usb/offload/init.c | 1 + sound/usb/offload/offload.h | 1 + sound/usb/offload/usb-audio.c | 166 +++++++++++++++++++++++++--------- 4 files changed, 141 insertions(+), 41 deletions(-) diff --git a/include/sound/usb-offload.h b/include/sound/usb-offload.h index 11c64b3a5659f1..8de313596280ea 100644 --- a/include/sound/usb-offload.h +++ b/include/sound/usb-offload.h @@ -31,6 +31,8 @@ #include +#include + struct device; struct snd_pcm_hw_params; struct usb_device; @@ -97,10 +99,18 @@ struct usb_offload_ops { * struct usb_offload_res - what the host hands the library at registration * @dev: the host's device, passed back to the @ops callbacks * @ops: host callback table + * @s24_3le_as_s24_le: treat a device that only offers S24_3LE as if it also + * offered S24_LE when refining a stream's format, and have + * usb_offload_acquire_stream() run the stream in S24_3LE for + * the device, see usb_offload_stream_hw.format. Set this when + * the host's own pipeline only has a 24-bit representation + * as S24_LE (24 valid bits in a 4-byte container) and cannot + * take S24_3LE directly; leave false otherwise. */ struct usb_offload_res { struct device *dev; const struct usb_offload_ops *ops; + bool s24_3le_as_s24_le; }; struct usb_offload_ctx *usb_offload_register(const struct usb_offload_res *res); @@ -129,6 +139,9 @@ struct usb_offload_ep_info { * passed back to usb_offload_release_stream() * @bus_devid: PCI bus, device and function of the xHCI controller, as * PCI_DEVID() lays them out + * @format: the sample format the device runs the stream in. It is the format of + * the params handed to usb_offload_acquire_stream(), except for the + * container of a 24-bit stream, see @s24_3le_as_s24_le * @slot_id: xHCI slot ID of the USB device * @high_speed: the root port the device hangs off runs at high speed * @split: the endpoint is reached through a USB 2.0 transaction translator @@ -145,6 +158,7 @@ struct usb_offload_ep_info { */ struct usb_offload_stream_hw { int pcm_index; + snd_pcm_format_t format; u16 bus_devid; u8 slot_id; bool high_speed; diff --git a/sound/usb/offload/init.c b/sound/usb/offload/init.c index fba3ebc5bceb45..0fbf0bfdb97200 100644 --- a/sound/usb/offload/init.c +++ b/sound/usb/offload/init.c @@ -37,6 +37,7 @@ struct usb_offload_ctx *usb_offload_register(const struct usb_offload_res *res) ctx->dev = res->dev; ctx->ops = res->ops; + ctx->s24_3le_as_s24_le = res->s24_3le_as_s24_le; ret = usb_offload_usb_audio_init(ctx); if (ret) { diff --git a/sound/usb/offload/offload.h b/sound/usb/offload/offload.h index 5937df5e0042fa..a09fc4ec299e3b 100644 --- a/sound/usb/offload/offload.h +++ b/sound/usb/offload/offload.h @@ -21,6 +21,7 @@ struct usb_offload_ctx { struct device *dev; const struct usb_offload_ops *ops; + bool s24_3le_as_s24_le; }; /* Where the host wants a stream's endpoint placed (called from usb-audio.c) */ diff --git a/sound/usb/offload/usb-audio.c b/sound/usb/offload/usb-audio.c index 06b22bafda484c..b4f28407e6cdef 100644 --- a/sound/usb/offload/usb-audio.c +++ b/sound/usb/offload/usb-audio.c @@ -351,12 +351,65 @@ static int usb_offload_ua_claim_ep(struct usb_offload_dev *dev, return ret; } +/* + * A host which has only S24_LE for 24-bit audio (usb_offload_res.s24_3le_as_s24_le) + * asks for it also of a device which packs 24-bit samples into 3 bytes and + * will not recognise S24_LE. Rewrite @params to that container, the DSP + * packs the samples on their way to the link. Returns false when the host + * did not ask for S24_LE or has no use for the rewrite. + */ +static bool usb_offload_s24_container(const struct usb_offload_ctx *ctx, + struct snd_pcm_hw_params *params) +{ + struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT); + + if (!ctx->s24_3le_as_s24_le || !snd_mask_test_format(fmt, SNDRV_PCM_FORMAT_S24_LE)) + return false; + + snd_mask_none(fmt); + snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S24_3LE); + + return true; +} + +/* + * The substream of @dev which can run @params. Caller holds + * usb_offload_ua_lock. + * + * snd_usb_find_suppported_substream() would do this, but it takes the + * usb-audio register_mutex to turn a card index into the chip. The chip is + * already known here, and the caller is a backend DAI holding the ASoC + * card's pcm_mutex, which that mutex nests inside. + */ +static struct snd_usb_substream * +usb_offload_find_subs(struct usb_offload_dev *dev, int direction, int pcm_index, + struct snd_pcm_hw_params *params, int *pcm_device) +{ + struct snd_usb_stream *as; + + list_for_each_entry(as, &dev->chip->pcm_list, list) { + if (!as->pcm || (pcm_index >= 0 && as->pcm->device != pcm_index)) + continue; + if (!as->substream[direction].num_formats) + continue; + + if (!snd_usb_find_substream_format(&as->substream[direction], params)) + continue; + + *pcm_device = as->pcm->device; + + return &as->substream[direction]; + } + + return NULL; +} + int usb_offload_acquire_stream(struct usb_offload_ctx *ctx, struct usb_device *udev, int direction, int pcm_index, struct snd_pcm_hw_params *params, struct usb_offload_stream_hw *hw) { - const struct audioformat *format; + struct snd_pcm_hw_params dev_params; struct usb_host_endpoint *host_ep; struct usb_host_endpoint *fb_host_ep = NULL; struct usb_offload_ua_stream *stream; @@ -364,7 +417,7 @@ int usb_offload_acquire_stream(struct usb_offload_ctx *ctx, struct usb_offload_dev *dev; struct xhci_sideband_ep_properties ep_prop; struct xhci_sideband_ep_properties fb_ep_prop; - struct snd_usb_stream *as; + int pcm_device; int fb_res_id = 0; u8 interval; int res_id; @@ -385,26 +438,12 @@ int usb_offload_acquire_stream(struct usb_offload_ctx *ctx, if (!dev || !dev->connected) return -ENODEV; - /* - * snd_usb_find_suppported_substream() would do this, but it takes the - * usb-audio register_mutex to turn a card index into the chip. The - * chip is already known here, and the caller is a backend DAI holding - * the ASoC card's pcm_mutex, which that mutex nests inside. - */ - subs = NULL; - list_for_each_entry(as, &dev->chip->pcm_list, list) { - if (!as->pcm || (pcm_index >= 0 && as->pcm->device != pcm_index)) - continue; - if (!as->substream[direction].num_formats) - continue; - - format = snd_usb_find_substream_format(&as->substream[direction], - params); - if (!format) - continue; - subs = &as->substream[direction]; - break; - } + /* the container the device has, not necessarily the one the host asked for */ + dev_params = *params; + subs = usb_offload_find_subs(dev, direction, pcm_index, &dev_params, &pcm_device); + if (!subs && usb_offload_s24_container(ctx, &dev_params)) + subs = usb_offload_find_subs(dev, direction, pcm_index, &dev_params, + &pcm_device); if (!subs) { dev_dbg(&udev->dev, "USB offload: no %s format for %s %uch %uHz\n", direction == SNDRV_PCM_STREAM_PLAYBACK ? "playback" : "capture", @@ -414,7 +453,7 @@ int usb_offload_acquire_stream(struct usb_offload_ctx *ctx, } /* a PCM is offloaded once; another PCM of the device may well be running */ - if (usb_offload_ua_find_stream(dev, direction, as->pcm->device)) + if (usb_offload_ua_find_stream(dev, direction, pcm_device)) return -EBUSY; stream = kzalloc_obj(*stream); @@ -422,9 +461,9 @@ int usb_offload_acquire_stream(struct usb_offload_ctx *ctx, return -ENOMEM; stream->subs = subs; stream->direction = direction; - stream->pcm_index = as->pcm->device; + stream->pcm_index = pcm_device; - ret = usb_offload_ua_start_stream(dev, subs, params); + ret = usb_offload_ua_start_stream(dev, subs, &dev_params); if (ret) goto err_free; @@ -461,6 +500,7 @@ int usb_offload_acquire_stream(struct usb_offload_ctx *ctx, memset(hw, 0, sizeof(*hw)); hw->pcm_index = stream->pcm_index; + hw->format = params_format(&dev_params); hw->slot_id = ep_prop.slot_id; hw->bus_devid = PCI_DEVID(ep_prop.bus, PCI_DEVFN(ep_prop.device, ep_prop.function)); @@ -548,22 +588,22 @@ int usb_offload_release_stream(struct usb_offload_ctx *ctx, } EXPORT_SYMBOL_NS_GPL(usb_offload_release_stream, "USB_AUDIO_OFFLOAD"); -static bool usb_offload_fmt_usable(const struct audioformat *fp, - struct snd_pcm_hw_params *params) +static u64 usb_offload_add_s24_le(struct usb_offload_ctx *ctx, u64 formats) +{ + if (ctx->s24_3le_as_s24_le && (formats & SNDRV_PCM_FMTBIT_S24_3LE)) + formats |= SNDRV_PCM_FMTBIT_S24_LE; + + return formats; +} + +static bool usb_offload_fmt_in_range(const struct audioformat *fp, + struct snd_pcm_hw_params *params) { struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS); struct snd_interval *r = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE); - struct snd_mask fmts; - - snd_mask_none(&fmts); - fmts.bits[0] = lower_32_bits(fp->formats); - fmts.bits[1] = upper_32_bits(fp->formats); - snd_mask_intersect(&fmts, - hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT)); - if (snd_mask_empty(&fmts)) - return false; + unsigned int i; if (fp->channels < c->min || fp->channels > c->max) return false; @@ -575,7 +615,38 @@ static bool usb_offload_fmt_usable(const struct audioformat *fp, (fp->rate_max == r->min && r->openmin)) return false; - return true; + /* + * A format with a rate table can have none of its rates in the + * interval even though the interval is within its bounds. FORMAT is + * chosen before RATE, so offering it would leave no rate to choose. + */ + if ((fp->rates & SNDRV_PCM_RATE_CONTINUOUS) || !fp->rate_table || !fp->nr_rates) + return true; + + for (i = 0; i < fp->nr_rates; i++) { + if (snd_interval_test(r, fp->rate_table[i])) + return true; + } + + return false; +} + +static bool usb_offload_fmt_usable(struct usb_offload_ctx *ctx, + const struct audioformat *fp, + struct snd_pcm_hw_params *params) +{ + struct snd_mask fmts; + u64 formats = usb_offload_add_s24_le(ctx, fp->formats); + + snd_mask_none(&fmts); + fmts.bits[0] = lower_32_bits(formats); + fmts.bits[1] = upper_32_bits(formats); + snd_mask_intersect(&fmts, + hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT)); + if (snd_mask_empty(&fmts)) + return false; + + return usb_offload_fmt_in_range(fp, params); } static void usb_offload_fmt_rates(const struct audioformat *fp, @@ -645,7 +716,21 @@ int usb_offload_refine_hw_params(struct usb_offload_ctx *ctx, continue; list_for_each_entry(fp, &subs->fmt_list, list) { - if (!usb_offload_fmt_usable(fp, params)) + /* + * Resolving FORMAT is what decides whether a format + * is usable in the first place - e.g. an alternate + * setting whose only format is S24_3LE must still be + * found here so that it can stand in for S24_LE + * below, so this is checked on rate and channels + * alone, unlike the other two parameters. + */ + if (var == SNDRV_PCM_HW_PARAM_FORMAT) { + if (usb_offload_fmt_in_range(fp, params)) + formats |= fp->formats; + continue; + } + + if (!usb_offload_fmt_usable(ctx, fp, params)) continue; switch (var) { @@ -656,14 +741,13 @@ int usb_offload_refine_hw_params(struct usb_offload_ctx *ctx, rmin = min(rmin, fp->channels); rmax = max(rmax, fp->channels); break; - case SNDRV_PCM_HW_PARAM_FORMAT: - formats |= fp->formats; - break; } } } if (var == SNDRV_PCM_HW_PARAM_FORMAT) { + formats = usb_offload_add_s24_le(ctx, formats); + snd_mask_none(&m); m.bits[0] = lower_32_bits(formats); m.bits[1] = upper_32_bits(formats); From 4e94a14f439ac7791f07e17f19d2730e8a39c4a7 Mon Sep 17 00:00:00 2001 From: Peter Ujfalusi Date: Wed, 4 Nov 2026 16:50:00 +0200 Subject: [PATCH 23/53] ALSA: usb: offload: Let the host keep asynchronous playback to itself An asynchronous OUT endpoint runs off the device's own clock and the sender has to follow it via the clock feedback. Whether the DSP applies that feedback well enough is a property of the host: the SOF firmware spreads samples linearly over the service interval, which is an audible degradation on the kind of gear that uses asynchronous playback in the first place (audio interfaces, mixers). Implicit feedback is in the same boat: its sync source is a capture data endpoint the DSP cannot use, so such a stream would be offloaded without any rate tracking at all. Add the async_playback_on_host flag to usb_offload_res for the host to leave these playback streams on the host: a playback PCM whose formats all need clock feedback is not reported in the offload device identity, formats that need it are skipped when refining hw_params, and usb_offload_acquire_stream() refuses a format that still ends up needing it. A format needs it when its data endpoint is asynchronous or when it has implicit feedback, which a quirk can also give an endpoint that is not declared asynchronous. Both are taken from the audioformat, so the rule covers explicit and implicit feedback alike and does not depend on how the usb-audio quirks pick the sync source. Capture is not affected, an asynchronous IN endpoint needs no feedback. The new allow_feedback module parameter of the library (default off) overrides the flag for testing the feedback path. Signed-off-by: Peter Ujfalusi --- include/sound/usb-offload.h | 10 ++++ sound/usb/offload/init.c | 1 + sound/usb/offload/offload.h | 1 + sound/usb/offload/usb-audio.c | 94 ++++++++++++++++++++++++++++++----- 4 files changed, 93 insertions(+), 13 deletions(-) diff --git a/include/sound/usb-offload.h b/include/sound/usb-offload.h index 8de313596280ea..ecee88d6403fb8 100644 --- a/include/sound/usb-offload.h +++ b/include/sound/usb-offload.h @@ -106,11 +106,21 @@ struct usb_offload_ops { * the host's own pipeline only has a 24-bit representation * as S24_LE (24 valid bits in a 4-byte container) and cannot * take S24_3LE directly; leave false otherwise. + * @async_playback_on_host: keep playback streams which need clock feedback + * (an asynchronous OUT endpoint, explicit or implicit + * feedback) on the host: their PCMs are not reported in the + * device identity when no other format can be offloaded, the + * formats are skipped when refining hw_params, + * and usb_offload_acquire_stream() refuses them. Set this + * when the DSP applies the feedback in a way that degrades + * the audio. The allow_feedback module parameter of the + * library overrides it for testing. */ struct usb_offload_res { struct device *dev; const struct usb_offload_ops *ops; bool s24_3le_as_s24_le; + bool async_playback_on_host; }; struct usb_offload_ctx *usb_offload_register(const struct usb_offload_res *res); diff --git a/sound/usb/offload/init.c b/sound/usb/offload/init.c index 0fbf0bfdb97200..b79afd5088294a 100644 --- a/sound/usb/offload/init.c +++ b/sound/usb/offload/init.c @@ -38,6 +38,7 @@ struct usb_offload_ctx *usb_offload_register(const struct usb_offload_res *res) ctx->dev = res->dev; ctx->ops = res->ops; ctx->s24_3le_as_s24_le = res->s24_3le_as_s24_le; + ctx->async_playback_on_host = res->async_playback_on_host; ret = usb_offload_usb_audio_init(ctx); if (ret) { diff --git a/sound/usb/offload/offload.h b/sound/usb/offload/offload.h index a09fc4ec299e3b..e06359e93a9ec3 100644 --- a/sound/usb/offload/offload.h +++ b/sound/usb/offload/offload.h @@ -22,6 +22,7 @@ struct usb_offload_ctx { struct device *dev; const struct usb_offload_ops *ops; bool s24_3le_as_s24_le; + bool async_playback_on_host; }; /* Where the host wants a stream's endpoint placed (called from usb-audio.c) */ diff --git a/sound/usb/offload/usb-audio.c b/sound/usb/offload/usb-audio.c index b4f28407e6cdef..07285d214e940e 100644 --- a/sound/usb/offload/usb-audio.c +++ b/sound/usb/offload/usb-audio.c @@ -52,6 +52,44 @@ module_param(offload_disable, bool, 0444); MODULE_PARM_DESC(offload_disable, "Disable USB audio offload candidate selection (default: 0)"); +static bool allow_feedback; +module_param(allow_feedback, bool, 0444); +MODULE_PARM_DESC(allow_feedback, + "Offload asynchronous playback streams also when the host keeps them (default: 0)"); + +/* + * An asynchronous OUT endpoint runs off the device's own clock and needs a + * feedback source for the sender to follow it. A host whose DSP applies that + * feedback in a way which degrades the audio keeps such streams to itself + * (usb_offload_res.async_playback_on_host), unless allowed for testing. This + * also covers implicit feedback, whose sync source is a capture data endpoint + * the DSP cannot use: that stream would run blind. A quirk can give an + * endpoint which is not declared asynchronous implicit feedback, so that is + * checked for on its own. + */ +static bool usb_offload_fmt_offloadable(const struct usb_offload_ctx *ctx, + const struct audioformat *fp, int direction) +{ + if (!ctx->async_playback_on_host || allow_feedback || + direction != SNDRV_PCM_STREAM_PLAYBACK) + return true; + + return !fp->implicit_fb && + (fp->ep_attr & USB_ENDPOINT_SYNCTYPE) != USB_ENDPOINT_SYNC_ASYNC; +} + +static bool usb_offload_subs_offloadable(const struct usb_offload_ctx *ctx, + const struct snd_usb_substream *subs) +{ + const struct audioformat *fp; + + list_for_each_entry(fp, &subs->fmt_list, list) + if (usb_offload_fmt_offloadable(ctx, fp, subs->direction)) + return true; + + return false; +} + /* * Candidate filter * @@ -373,8 +411,9 @@ static bool usb_offload_s24_container(const struct usb_offload_ctx *ctx, } /* - * The substream of @dev which can run @params. Caller holds - * usb_offload_ua_lock. + * The substream of @dev which can run @params and the host lets the DSP + * have. @needs_feedback is set when one could, but needs clock feedback the + * host keeps. Caller holds usb_offload_ua_lock. * * snd_usb_find_suppported_substream() would do this, but it takes the * usb-audio register_mutex to turn a card index into the chip. The chip is @@ -382,9 +421,11 @@ static bool usb_offload_s24_container(const struct usb_offload_ctx *ctx, * card's pcm_mutex, which that mutex nests inside. */ static struct snd_usb_substream * -usb_offload_find_subs(struct usb_offload_dev *dev, int direction, int pcm_index, - struct snd_pcm_hw_params *params, int *pcm_device) +usb_offload_find_subs(const struct usb_offload_ctx *ctx, struct usb_offload_dev *dev, + int direction, int pcm_index, struct snd_pcm_hw_params *params, + int *pcm_device, bool *needs_feedback) { + const struct audioformat *format; struct snd_usb_stream *as; list_for_each_entry(as, &dev->chip->pcm_list, list) { @@ -393,8 +434,14 @@ usb_offload_find_subs(struct usb_offload_dev *dev, int direction, int pcm_index, if (!as->substream[direction].num_formats) continue; - if (!snd_usb_find_substream_format(&as->substream[direction], params)) + format = snd_usb_find_substream_format(&as->substream[direction], params); + if (!format) + continue; + /* find_format() already prefers a non-async altsetting if one matches */ + if (!usb_offload_fmt_offloadable(ctx, format, direction)) { + *needs_feedback = true; continue; + } *pcm_device = as->pcm->device; @@ -417,6 +464,7 @@ int usb_offload_acquire_stream(struct usb_offload_ctx *ctx, struct usb_offload_dev *dev; struct xhci_sideband_ep_properties ep_prop; struct xhci_sideband_ep_properties fb_ep_prop; + bool needs_feedback = false; int pcm_device; int fb_res_id = 0; u8 interval; @@ -440,10 +488,17 @@ int usb_offload_acquire_stream(struct usb_offload_ctx *ctx, /* the container the device has, not necessarily the one the host asked for */ dev_params = *params; - subs = usb_offload_find_subs(dev, direction, pcm_index, &dev_params, &pcm_device); + subs = usb_offload_find_subs(ctx, dev, direction, pcm_index, &dev_params, + &pcm_device, &needs_feedback); if (!subs && usb_offload_s24_container(ctx, &dev_params)) - subs = usb_offload_find_subs(dev, direction, pcm_index, &dev_params, - &pcm_device); + subs = usb_offload_find_subs(ctx, dev, direction, pcm_index, &dev_params, + &pcm_device, &needs_feedback); + if (!subs && needs_feedback) { + dev_dbg(&udev->dev, + "USB offload: %uch %uHz playback needs clock feedback, not offloaded\n", + params_channels(params), params_rate(params)); + return -EOPNOTSUPP; + } if (!subs) { dev_dbg(&udev->dev, "USB offload: no %s format for %s %uch %uHz\n", direction == SNDRV_PCM_STREAM_PLAYBACK ? "playback" : "capture", @@ -716,6 +771,9 @@ int usb_offload_refine_hw_params(struct usb_offload_ctx *ctx, continue; list_for_each_entry(fp, &subs->fmt_list, list) { + if (!usb_offload_fmt_offloadable(ctx, fp, direction)) + continue; + /* * Resolving FORMAT is what decides whether a format * is usable in the first place - e.g. an alternate @@ -802,9 +860,13 @@ static void usb_offload_ua_teardown(struct usb_offload_dev *dev) * to the host (via usb_offload_connect) so that it can construct a * struct snd_soc_usb_device for the ASoC USB framework without the * library ever including . + * + * @log: report PCMs left out of the offload list; the caller passes true + * only on the first connect of a device, as this runs on every re-notify. */ -static void usb_offload_ua_build_id(struct snd_usb_audio *chip, - struct usb_offload_usb_dev_info *id) +static void usb_offload_ua_build_id(const struct usb_offload_ctx *ctx, + struct snd_usb_audio *chip, + struct usb_offload_usb_dev_info *id, bool log) { struct snd_usb_substream *subs; struct snd_usb_stream *as; @@ -823,7 +885,13 @@ static void usb_offload_ua_build_id(struct snd_usb_audio *chip, pcm_name = as->pcm->name[0] ? as->pcm->name : "USB PCM"; subs = &as->substream[SNDRV_PCM_STREAM_PLAYBACK]; - if (subs->num_formats && id->num_playback < USB_OFFLOAD_MAX_PCM) { + if (subs->num_formats && !usb_offload_subs_offloadable(ctx, subs)) { + if (log) + dev_dbg(&chip->dev->dev, + "USB offload: PCM %u playback needs clock feedback, not offloaded\n", + as->pcm->device); + } else if (subs->num_formats && + id->num_playback < USB_OFFLOAD_MAX_PCM) { index = id->num_playback++; id->ppcm_idx[index] = as->pcm->device; snprintf(id->ppcm_names[index], USB_OFFLOAD_PCM_NAME_LEN, @@ -878,7 +946,7 @@ static void usb_offload_ua_connect_cb(struct snd_usb_audio *chip) mutex_unlock(&usb_offload_ua_lock); if (connected) { - usb_offload_ua_build_id(chip, id); + usb_offload_ua_build_id(ctx, chip, id, false); ret = usb_offload_connect(ctx, udev, id); if (ret) dev_dbg(&udev->dev, @@ -922,7 +990,7 @@ static void usb_offload_ua_connect_cb(struct snd_usb_audio *chip) mutex_unlock(&usb_offload_ua_lock); /* Inform the DSP and the ASoC USB framework. */ - usb_offload_ua_build_id(chip, id); + usb_offload_ua_build_id(ctx, chip, id, true); ret = usb_offload_connect(ctx, udev, id); mutex_lock(&usb_offload_ua_lock); From 353ea979267a3dca6b3c81d09f2eecf1fc349df3 Mon Sep 17 00:00:00 2001 From: Peter Ujfalusi Date: Thu, 19 Nov 2026 13:48:39 +0200 Subject: [PATCH 24/53] ALSA: usb: offload: Keep implicit feedback streams on the host A playback stream whose OUT endpoint has implicit feedback is clocked by a capture data endpoint the DSP cannot use, and the hardware has no way to follow it: offloaded, it would run blind. Unlike asynchronous playback with explicit feedback, which a host whose DSP applies it well can have offloaded, this is not up to the host. Such a playback format always stays on the host, whatever async_playback_on_host and allow_feedback say. That clock is the data endpoint of the device's capture interface, which is still offered for offload: once the capture is offloaded the endpoint belongs to the DSP and the host cannot run its playback in any format which syncs to it. With a Behringer UMC204HD that is the 24-bit format a sound server picks. Keep such a capture format on the host as well: a capture format whose endpoint is the implicit feedback source of a playback format the host keeps is not offloadable. Its PCM is left out of the device identity when no other format can be offloaded, and the format is skipped when refining and acquiring a stream. A device can now end up with no PCM to offload in either direction, as can one whose only PCM is asynchronous playback. Do not report such a device to the host at all, its route items would be empty while the host counts it as connected. A capture format can also be the implicit feedback sink: snd-usb-audio gives it that for a device which only captures while its playback endpoint streams, like the Behringer FCA1616 via capture_implicit_fb_quirks[]. The library then hands neither the playback endpoint to the DSP nor claims it, while snd_usb_hw_params() still opens it as the sync endpoint. Nothing streams that endpoint once the capture is offloaded, and the device has no clock to capture with. Such a format is not offloadable in any configuration. The vendor class devices with the same quirk, Roland and BOSS, never pass the candidate filter. The log message for a capture PCM left out of the device identity names the reasons. Signed-off-by: Peter Ujfalusi --- include/sound/usb-offload.h | 20 +++--- sound/usb/offload/usb-audio.c | 116 +++++++++++++++++++++++++--------- 2 files changed, 97 insertions(+), 39 deletions(-) diff --git a/include/sound/usb-offload.h b/include/sound/usb-offload.h index ecee88d6403fb8..7713828bd2a628 100644 --- a/include/sound/usb-offload.h +++ b/include/sound/usb-offload.h @@ -106,15 +106,17 @@ struct usb_offload_ops { * the host's own pipeline only has a 24-bit representation * as S24_LE (24 valid bits in a 4-byte container) and cannot * take S24_3LE directly; leave false otherwise. - * @async_playback_on_host: keep playback streams which need clock feedback - * (an asynchronous OUT endpoint, explicit or implicit - * feedback) on the host: their PCMs are not reported in the - * device identity when no other format can be offloaded, the - * formats are skipped when refining hw_params, - * and usb_offload_acquire_stream() refuses them. Set this - * when the DSP applies the feedback in a way that degrades - * the audio. The allow_feedback module parameter of the - * library overrides it for testing. + * @async_playback_on_host: keep playback streams whose asynchronous OUT + * endpoint needs explicit clock feedback on the host: their + * PCMs are not reported in the device identity when no + * other format can be offloaded, the formats are skipped + * when refining hw_params, and usb_offload_acquire_stream() + * refuses them. Set this when the DSP applies the feedback + * in a way that degrades the audio. The allow_feedback + * module parameter of the library overrides it for testing. + * Playback with implicit feedback, and the capture it syncs + * to, stays on the host whatever this says: the DSP cannot + * follow that clock. */ struct usb_offload_res { struct device *dev; diff --git a/sound/usb/offload/usb-audio.c b/sound/usb/offload/usb-audio.c index 07285d214e940e..a8dd150d5c672f 100644 --- a/sound/usb/offload/usb-audio.c +++ b/sound/usb/offload/usb-audio.c @@ -58,24 +58,66 @@ MODULE_PARM_DESC(allow_feedback, "Offload asynchronous playback streams also when the host keeps them (default: 0)"); /* + * An OUT endpoint with implicit feedback is clocked by a capture data endpoint + * the DSP cannot use, and the hardware has no way to follow it: that stream + * would run blind. It always stays on the host. A quirk can give an endpoint + * which is not declared asynchronous implicit feedback, so that is checked + * for on its own. + * * An asynchronous OUT endpoint runs off the device's own clock and needs a * feedback source for the sender to follow it. A host whose DSP applies that * feedback in a way which degrades the audio keeps such streams to itself - * (usb_offload_res.async_playback_on_host), unless allowed for testing. This - * also covers implicit feedback, whose sync source is a capture data endpoint - * the DSP cannot use: that stream would run blind. A quirk can give an - * endpoint which is not declared asynchronous implicit feedback, so that is - * checked for on its own. + * (usb_offload_res.async_playback_on_host), unless allowed for testing. + */ +static bool usb_offload_playback_kept(const struct usb_offload_ctx *ctx, + const struct audioformat *fp) +{ + if (fp->implicit_fb) + return true; + + if (!ctx->async_playback_on_host || allow_feedback) + return false; + + return (fp->ep_attr & USB_ENDPOINT_SYNCTYPE) == USB_ENDPOINT_SYNC_ASYNC; +} + +/* + * The capture endpoint a playback format with implicit feedback syncs to is + * that playback's clock. Giving it to the DSP would leave a playback stream + * which the host keeps without one, so the host keeps such a capture format + * as well. + * + * A capture format with implicit feedback of its own, which a quirk gives to + * a device that only captures while its playback endpoint streams, stays on + * the host too: the DSP only gets the capture endpoint and nothing would + * stream the playback one. */ static bool usb_offload_fmt_offloadable(const struct usb_offload_ctx *ctx, + struct snd_usb_audio *chip, const struct audioformat *fp, int direction) { - if (!ctx->async_playback_on_host || allow_feedback || - direction != SNDRV_PCM_STREAM_PLAYBACK) - return true; + const struct audioformat *pfp; + struct snd_usb_stream *as; + + if (direction == SNDRV_PCM_STREAM_PLAYBACK) + return !usb_offload_playback_kept(ctx, fp); - return !fp->implicit_fb && - (fp->ep_attr & USB_ENDPOINT_SYNCTYPE) != USB_ENDPOINT_SYNC_ASYNC; + if (fp->implicit_fb) + return false; + + list_for_each_entry(as, &chip->pcm_list, list) { + /* the list of a direction a stream does not have is not set up */ + if (!as->substream[SNDRV_PCM_STREAM_PLAYBACK].num_formats) + continue; + + list_for_each_entry(pfp, &as->substream[SNDRV_PCM_STREAM_PLAYBACK].fmt_list, list) { + if (pfp->implicit_fb && pfp->sync_ep == fp->endpoint && + usb_offload_playback_kept(ctx, pfp)) + return false; + } + } + + return true; } static bool usb_offload_subs_offloadable(const struct usb_offload_ctx *ctx, @@ -84,7 +126,8 @@ static bool usb_offload_subs_offloadable(const struct usb_offload_ctx *ctx, const struct audioformat *fp; list_for_each_entry(fp, &subs->fmt_list, list) - if (usb_offload_fmt_offloadable(ctx, fp, subs->direction)) + if (usb_offload_fmt_offloadable(ctx, subs->stream->chip, fp, + subs->direction)) return true; return false; @@ -412,8 +455,8 @@ static bool usb_offload_s24_container(const struct usb_offload_ctx *ctx, /* * The substream of @dev which can run @params and the host lets the DSP - * have. @needs_feedback is set when one could, but needs clock feedback the - * host keeps. Caller holds usb_offload_ua_lock. + * have. @kept is set when one could, but the host keeps it. Caller holds + * usb_offload_ua_lock. * * snd_usb_find_suppported_substream() would do this, but it takes the * usb-audio register_mutex to turn a card index into the chip. The chip is @@ -423,7 +466,7 @@ static bool usb_offload_s24_container(const struct usb_offload_ctx *ctx, static struct snd_usb_substream * usb_offload_find_subs(const struct usb_offload_ctx *ctx, struct usb_offload_dev *dev, int direction, int pcm_index, struct snd_pcm_hw_params *params, - int *pcm_device, bool *needs_feedback) + int *pcm_device, bool *kept) { const struct audioformat *format; struct snd_usb_stream *as; @@ -438,8 +481,8 @@ usb_offload_find_subs(const struct usb_offload_ctx *ctx, struct usb_offload_dev if (!format) continue; /* find_format() already prefers a non-async altsetting if one matches */ - if (!usb_offload_fmt_offloadable(ctx, format, direction)) { - *needs_feedback = true; + if (!usb_offload_fmt_offloadable(ctx, dev->chip, format, direction)) { + *kept = true; continue; } @@ -464,7 +507,7 @@ int usb_offload_acquire_stream(struct usb_offload_ctx *ctx, struct usb_offload_dev *dev; struct xhci_sideband_ep_properties ep_prop; struct xhci_sideband_ep_properties fb_ep_prop; - bool needs_feedback = false; + bool kept = false; int pcm_device; int fb_res_id = 0; u8 interval; @@ -489,14 +532,15 @@ int usb_offload_acquire_stream(struct usb_offload_ctx *ctx, /* the container the device has, not necessarily the one the host asked for */ dev_params = *params; subs = usb_offload_find_subs(ctx, dev, direction, pcm_index, &dev_params, - &pcm_device, &needs_feedback); + &pcm_device, &kept); if (!subs && usb_offload_s24_container(ctx, &dev_params)) subs = usb_offload_find_subs(ctx, dev, direction, pcm_index, &dev_params, - &pcm_device, &needs_feedback); - if (!subs && needs_feedback) { + &pcm_device, &kept); + if (!subs && kept) { dev_dbg(&udev->dev, - "USB offload: %uch %uHz playback needs clock feedback, not offloaded\n", - params_channels(params), params_rate(params)); + "USB offload: %uch %uHz %s stays with the host, not offloaded\n", + params_channels(params), params_rate(params), + direction == SNDRV_PCM_STREAM_PLAYBACK ? "playback" : "capture"); return -EOPNOTSUPP; } if (!subs) { @@ -534,11 +578,11 @@ int usb_offload_acquire_stream(struct usb_offload_ctx *ctx, goto err_stop; /* - * Implicit feedback reuses another interface's capture data endpoint - * as a sync source; it carries audio samples, not a feedback value - * the DSP can interpret, so only an explicit sync endpoint is claimed. - * usb-audio flags the data endpoint, which syncs with the implicit - * feedback, not the capture endpoint it syncs to. + * Playback with implicit feedback is never offloaded, see + * usb_offload_playback_kept(), so the sync endpoint a stream has here + * is an explicit feedback one. The flag is checked nonetheless: the + * endpoint such a stream would sync to carries audio samples, not a + * feedback value the DSP can interpret. */ if (subs->sync_endpoint && !subs->data_endpoint->implicit_fb_sync) { fb_host_ep = usb_pipe_endpoint(udev, subs->sync_endpoint->pipe); @@ -771,7 +815,7 @@ int usb_offload_refine_hw_params(struct usb_offload_ctx *ctx, continue; list_for_each_entry(fp, &subs->fmt_list, list) { - if (!usb_offload_fmt_offloadable(ctx, fp, direction)) + if (!usb_offload_fmt_offloadable(ctx, dev->chip, fp, direction)) continue; /* @@ -900,7 +944,13 @@ static void usb_offload_ua_build_id(const struct usb_offload_ctx *ctx, } subs = &as->substream[SNDRV_PCM_STREAM_CAPTURE]; - if (subs->num_formats && id->num_capture < USB_OFFLOAD_MAX_PCM) { + if (subs->num_formats && !usb_offload_subs_offloadable(ctx, subs)) { + if (log) + dev_dbg(&chip->dev->dev, + "USB offload: PCM %u capture is tied to the playback, not offloaded\n", + as->pcm->device); + } else if (subs->num_formats && + id->num_capture < USB_OFFLOAD_MAX_PCM) { index = id->num_capture++; id->cpcm_idx[index] = as->pcm->device; snprintf(id->cpcm_names[index], USB_OFFLOAD_PCM_NAME_LEN, @@ -956,6 +1006,13 @@ static void usb_offload_ua_connect_cb(struct snd_usb_audio *chip) return; } + /* a device whose PCMs all stay with the host has nothing to offload */ + usb_offload_ua_build_id(ctx, chip, id, true); + if (!id->num_playback && !id->num_capture) { + dev_dbg(&udev->dev, "USB offload: no PCM can be offloaded\n"); + goto unlock; + } + dev = kzalloc_obj(*dev); if (!dev) goto unlock; @@ -990,7 +1047,6 @@ static void usb_offload_ua_connect_cb(struct snd_usb_audio *chip) mutex_unlock(&usb_offload_ua_lock); /* Inform the DSP and the ASoC USB framework. */ - usb_offload_ua_build_id(ctx, chip, id, true); ret = usb_offload_connect(ctx, udev, id); mutex_lock(&usb_offload_ua_lock); From fd9cb3bcfd4649a6a69c7871296568ea568c4803 Mon Sep 17 00:00:00 2001 From: Peter Ujfalusi Date: Thu, 19 Nov 2026 12:26:17 +0200 Subject: [PATCH 25/53] ASoC: soc-usb: Make the snd_soc_usb_update_offload_route() stub inline The !CONFIG_SND_SOC_USB stub of snd_soc_usb_update_offload_route() is static but not inline, unlike the other stubs in the header, so a file including without calling it gets a 'defined but not used' warning. Make it static inline. Fixes: 234ed325920c ("ASoC: usb: Fetch ASoC card and pcm device information") Signed-off-by: Peter Ujfalusi --- include/sound/soc-usb.h | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/include/sound/soc-usb.h b/include/sound/soc-usb.h index 124acb1939e57f..e6c01ee743ba68 100644 --- a/include/sound/soc-usb.h +++ b/include/sound/soc-usb.h @@ -113,9 +113,10 @@ static inline int snd_soc_usb_setup_offload_jack(struct snd_soc_component *compo return 0; } -static int snd_soc_usb_update_offload_route(struct device *dev, int card, int pcm, - int direction, enum snd_soc_usb_kctl path, - long *route) +static inline int +snd_soc_usb_update_offload_route(struct device *dev, int card, int pcm, + int direction, enum snd_soc_usb_kctl path, + long *route) { return -ENODEV; } From e5f72ed836b8cf46f26837b4fd4c53669d197f26 Mon Sep 17 00:00:00 2001 From: Peter Ujfalusi Date: Wed, 4 Nov 2026 14:27:30 +0200 Subject: [PATCH 26/53] ASoC: soc-usb: Track the connected devices and the offload routes of a port A USB audio device can expose several PCM streams, and an offload backend can run several streams per direction at a time, each of which has to be pointed at a device and one of its PCMs. Give the port what it needs to tell which that is. The connected devices are kept on the port's available_devices list through a node soc-usb owns; snd_soc_usb_device's list stays the backend's, q6usb links it into its own device list. Every (device, pcm) pair of a direction, across all connected devices, is a candidate, in device connection order and then PCM order; the devices report a name per PCM. The port has a number of routes per direction, one by default, which the backend raises with snd_soc_usb_set_num_routes() before the port is added. A route nobody picked a pair for follows a default: the Nth pair of the direction for route N, wrapping around. It is not stored but worked out on every query, so it follows the devices as they come and go. snd_soc_usb_get_selected_device() returns the pair a route points at for the backend to acquire. Any route can point at any pair, also one another route of the direction has; a pair can only be offloaded once, so of two routes on the same pair the second backend to acquire fails with -EBUSY. snd_soc_usb_connect() reports an error now, where it returned 0 before: -ENODEV when there is no port for the device, -EEXIST, with a warning, when the device is listed already. Such a device is not listed and must not be disconnected later. Signed-off-by: Peter Ujfalusi --- Documentation/sound/soc/usb.rst | 59 +++++++- include/sound/soc-usb.h | 54 +++++++- sound/soc/soc-usb.c | 231 +++++++++++++++++++++++++++++++- 3 files changed, 336 insertions(+), 8 deletions(-) diff --git a/Documentation/sound/soc/usb.rst b/Documentation/sound/soc/usb.rst index eaf16bb3acfdc8..146d6793d6a6a4 100644 --- a/Documentation/sound/soc/usb.rst +++ b/Documentation/sound/soc/usb.rst @@ -57,6 +57,8 @@ Structures - ``connection_status_cb``: callback to notify connection events - ``update_offload_route_info``: callback to fetch selected USB sound card/PCM device + - ``available_devices``: the connected ``snd_soc_usb_device`` entries + - ``routes``, ``num_routes``: the offload routes of each direction - ``priv_data``: driver data The snd_soc_usb structure can be referenced using the ASoC platform card @@ -73,9 +75,12 @@ registered with, so the caller passes that device. - ``chip_idx``: USB sound chip array index - ``cpcm_idx``: capture pcm device indexes associated with the USB sound device - ``ppcm_idx``: playback pcm device indexes associated with the USB sound device + - ``cpcm_names``, ``ppcm_names``: optional human readable names of the PCMs, + parallel to the index arrays - ``num_playback``: number of playback streams - ``num_capture``: number of capture streams - - ``list``: list head for the USB sound device list + - ``list``: list head for the backend's own use + - ``available_node``: list head on ``available_devices``, owned by SoC USB The struct snd_soc_usb_device is created by the USB sound offload driver. This will carry basic parameters/limitations that will be used to @@ -109,9 +114,13 @@ Returns 0 on success, and -EOPNOTSUPP on failure. **snd_soc_usb_connect()** notifies the ASoC USB DPCM BE DAI link of a USB audio device detection. This can be utilized in the BE DAI driver to keep track of available USB audio devices. This is intended -to be called by the USB offload driver residing in USB SND. +to be called by the USB offload driver residing in USB SND. The device is +added to ``available_devices``. -Returns 0 on success, negative error code on failure. +Returns 0 on success, -ENODEV when no port is registered for ``usbdev`` (the +device is then not listed and must not be reported as disconnected later), +-EEXIST when ``sdev`` is connected already, negative error code on other +failures. .. code-block:: rst @@ -187,10 +196,52 @@ Returns 0 on success, negative otherwise. - ``data``: private data **snd_soc_usb_allocate_port()** allocates a SoC USB device and populates standard -parameters that is used for further operations. +parameters that is used for further operations. The port starts out with one +offload route per direction. Returns a pointer to struct soc_usb on success, negative on error. +.. code-block:: rst + + int snd_soc_usb_set_num_routes(struct snd_soc_usb *usb, int direction, + unsigned int num_routes); +.. + + - ``usb``: SoC USB device, not yet added with snd_soc_usb_add_port() + - ``direction``: SNDRV_PCM_STREAM_PLAYBACK or SNDRV_PCM_STREAM_CAPTURE + - ``num_routes``: streams the backend can offload at a time in ``direction`` + +**snd_soc_usb_set_num_routes()** gives a backend which runs several offloaded +streams of a direction at a time a route per stream, so that each can be +pointed at a (device, PCM) pair of its own. To be called before the port is +added. + +Returns 0 on success, negative on error. + +.. code-block:: rst + + int snd_soc_usb_get_selected_device(struct snd_soc_usb *usb, int direction, + unsigned int route, + struct snd_soc_usb_device **sdev, + int *pcm_index); +.. + + - ``usb``: SoC USB device + - ``direction``: SNDRV_PCM_STREAM_PLAYBACK or SNDRV_PCM_STREAM_CAPTURE + - ``route``: the route of ``direction`` + - ``sdev``: set to the routed device, NULL if nothing is selected + - ``pcm_index``: set to the routed USB SND PCM device index + +**snd_soc_usb_get_selected_device()** returns the (device, PCM) pair a route +currently points at, for the backend to consult when it starts a stream +instead of picking a device itself. A route nothing was picked for follows its +default: route N gets the Nth available pair of its direction, wrapping around +when there are fewer pairs than routes, so that the routes spread over what is +connected. The default is worked out on every query, it is not stored, so it +moves as devices connect and disconnect. + +Returns 0 on success (also when nothing is selected), negative on error. + .. code-block:: rst void snd_soc_usb_free_port(struct snd_soc_usb *usb); diff --git a/include/sound/soc-usb.h b/include/sound/soc-usb.h index e6c01ee743ba68..a6f6cf777ce35c 100644 --- a/include/sound/soc-usb.h +++ b/include/sound/soc-usb.h @@ -19,9 +19,12 @@ enum snd_soc_usb_kctl { * @chip_idx: USB sound chip array index * @cpcm_idx: capture PCM index array associated with USB device * @ppcm_idx: playback PCM index array associated with USB device + * @cpcm_names: capture PCM name array, parallel to @cpcm_idx + * @ppcm_names: playback PCM name array, parallel to @ppcm_idx * @num_capture: number of capture streams * @num_playback: number of playback streams - * @list: list head for SoC USB devices + * @list: list head for the backend's own use + * @available_node: link on snd_soc_usb.available_devices, owned by soc-usb **/ struct snd_soc_usb_device { int card_idx; @@ -30,10 +33,29 @@ struct snd_soc_usb_device { /* PCM index arrays */ unsigned int *cpcm_idx; /* TODO: capture path is not tested yet */ unsigned int *ppcm_idx; + /* human readable PCM name arrays, parallel to the index arrays above */ + const char * const *cpcm_names; + const char * const *ppcm_names; int num_capture; /* TODO: capture path is not tested yet */ int num_playback; struct list_head list; + struct list_head available_node; +}; + +/** + * struct snd_soc_usb_route - one offload route of a direction + * @sdev: the device picked for the route, NULL while nothing is picked and the + * route follows its default + * @pcm_idx: the PCM index of @sdev picked, -1 while nothing is picked + * + * A backend able to run several offloaded streams in a direction at a time + * has a route per stream. Two routes may point at the same pair; only one of + * them can offload it at a time, the other fails when it tries. + **/ +struct snd_soc_usb_route { + struct snd_soc_usb_device *sdev; + int pcm_idx; }; /** @@ -47,6 +69,10 @@ struct snd_soc_usb_device { * an array used for a kcontrol output for either * the card or pcm index. "path" determines the * which entry to look for. (ie mapped card or pcm) + * @available_devices: list of currently connected snd_soc_usb_device entries + * @routes: the routes of each direction, @num_routes of them + * @num_routes: number of routes per direction, one unless the backend set + * more with snd_soc_usb_set_num_routes() * @priv_data: driver data **/ struct snd_soc_usb { @@ -59,6 +85,11 @@ struct snd_soc_usb { int card, int pcm, int direction, enum snd_soc_usb_kctl path, long *route); + + struct list_head available_devices; + struct snd_soc_usb_route *routes[SNDRV_PCM_STREAM_LAST + 1]; + unsigned int num_routes[SNDRV_PCM_STREAM_LAST + 1]; + void *priv_data; }; @@ -80,8 +111,15 @@ int snd_soc_usb_update_offload_route(struct device *dev, int card, int pcm, struct snd_soc_usb *snd_soc_usb_allocate_port(struct snd_soc_component *component, void *data); void snd_soc_usb_free_port(struct snd_soc_usb *usb); +int snd_soc_usb_set_num_routes(struct snd_soc_usb *usb, int direction, + unsigned int num_routes); void snd_soc_usb_add_port(struct snd_soc_usb *usb); void snd_soc_usb_remove_port(struct snd_soc_usb *usb); + +int snd_soc_usb_get_selected_device(struct snd_soc_usb *usb, int direction, + unsigned int route, + struct snd_soc_usb_device **sdev, + int *pcm_index); #else static inline int snd_soc_usb_find_supported_format(int card_idx, struct snd_pcm_hw_params *params, @@ -130,10 +168,24 @@ snd_soc_usb_allocate_port(struct snd_soc_component *component, void *data) static inline void snd_soc_usb_free_port(struct snd_soc_usb *usb) { } +static inline int snd_soc_usb_set_num_routes(struct snd_soc_usb *usb, int direction, + unsigned int num_routes) +{ + return -ENODEV; +} + static inline void snd_soc_usb_add_port(struct snd_soc_usb *usb) { } static inline void snd_soc_usb_remove_port(struct snd_soc_usb *usb) { } + +static inline int snd_soc_usb_get_selected_device(struct snd_soc_usb *usb, int direction, + unsigned int route, + struct snd_soc_usb_device **sdev, + int *pcm_index) +{ + return -ENODEV; +} #endif /* IS_ENABLED(CONFIG_SND_SOC_USB) */ #endif /*__LINUX_SND_SOC_USB_H */ diff --git a/sound/soc/soc-usb.c b/sound/soc/soc-usb.c index 58c5ce0d160ec7..2a94ae44ab2e05 100644 --- a/sound/soc/soc-usb.c +++ b/sound/soc/soc-usb.c @@ -74,6 +74,96 @@ static struct snd_soc_usb *snd_soc_find_usb_ctx(struct device *dev) return snd_soc_usb_ctx_lookup(dev); } +/** + * struct snd_soc_usb_route_entry - one (device, pcm) pair offloadable for a + * direction + * @sdev: the device this pcm belongs to + * @pcm_idx: the USB SND PCM device index within @sdev + * @name: human readable name for this entry, borrowed from @sdev, valid only + * as long as @sdev stays in usb->available_devices + */ +struct snd_soc_usb_route_entry { + struct snd_soc_usb_device *sdev; + int pcm_idx; + const char *name; +}; + +/* + * Caller holds ctx_mutex. Walks usb->available_devices in order, treating + * every (device, pcm) pair for @direction across every device as one flat, + * positionally indexed list - this positional index is what the route + * kcontrol's enumerated item number means. Returns false once @index runs + * past the end. + */ +static bool snd_soc_usb_route_entry_at(struct snd_soc_usb *usb, int direction, + unsigned int index, + struct snd_soc_usb_route_entry *entry) +{ + struct snd_soc_usb_device *sdev; + unsigned int *pcm_idx; + const char * const *pcm_names; + int num, i; + + list_for_each_entry(sdev, &usb->available_devices, available_node) { + if (direction == SNDRV_PCM_STREAM_PLAYBACK) { + num = sdev->num_playback; + pcm_idx = sdev->ppcm_idx; + pcm_names = sdev->ppcm_names; + } else { + num = sdev->num_capture; + pcm_idx = sdev->cpcm_idx; + pcm_names = sdev->cpcm_names; + } + + for (i = 0; i < num; i++) { + if (index == 0) { + entry->sdev = sdev; + entry->pcm_idx = pcm_idx[i]; + entry->name = (pcm_names && pcm_names[i]) ? + pcm_names[i] : "USB PCM"; + return true; + } + index--; + } + } + + return false; +} + +/* + * Caller holds ctx_mutex. The pair @route of @direction currently points at, + * and its position in the flat list: the one picked for it, or while nothing + * is picked (or the picked pair is gone) the one it gets by default. By + * default route N gets the Nth pair, wrapping around when there are fewer + * pairs than routes, so that the routes spread over what is connected. The + * default is worked out here on every call instead of being stored, it + * follows the devices as they come and go. Returns false when no pair is + * available. + */ +static bool snd_soc_usb_route_resolve(struct snd_soc_usb *usb, int direction, + unsigned int route, + struct snd_soc_usb_route_entry *entry, + unsigned int *index) +{ + struct snd_soc_usb_route *r = &usb->routes[direction][route]; + unsigned int i, count = 0; + + for (i = 0; snd_soc_usb_route_entry_at(usb, direction, i, entry); i++) { + if (r->sdev && entry->sdev == r->sdev && entry->pcm_idx == r->pcm_idx) { + *index = i; + return true; + } + count++; + } + + if (!count) + return false; + + *index = route % count; + + return snd_soc_usb_route_entry_at(usb, direction, *index, entry); +} + /* SOC USB sound kcontrols */ /** * snd_soc_usb_setup_offload_jack() - Create USB offloading jack @@ -146,6 +236,47 @@ int snd_soc_usb_update_offload_route(struct device *dev, int card, int pcm, } EXPORT_SYMBOL_GPL(snd_soc_usb_update_offload_route); +/** + * snd_soc_usb_get_selected_device() - Get the device/pcm a route points at + * @usb: soc usb port + * @direction: SNDRV_PCM_STREAM_PLAYBACK or SNDRV_PCM_STREAM_CAPTURE + * @route: the route of @direction, below usb->num_routes[direction] + * @sdev: set to the routed device, or NULL if nothing is selected + * @pcm_index: set to the routed device's USB SND PCM device index + * + * A backend consults this at stream acquire time instead of picking a device + * itself, so the stream goes to whatever the route currently points at. + * + * Return: 0 on success (even if nothing is selected - check @sdev), negative + * on error. + */ +int snd_soc_usb_get_selected_device(struct snd_soc_usb *usb, int direction, + unsigned int route, + struct snd_soc_usb_device **sdev, + int *pcm_index) +{ + struct snd_soc_usb_route_entry entry; + unsigned int index; + + if (!usb || direction < 0 || direction > SNDRV_PCM_STREAM_LAST) + return -EINVAL; + + if (route >= usb->num_routes[direction]) + return -EINVAL; + + guard(mutex)(&ctx_mutex); + if (snd_soc_usb_route_resolve(usb, direction, route, &entry, &index)) { + *sdev = entry.sdev; + *pcm_index = entry.pcm_idx; + } else { + *sdev = NULL; + *pcm_index = -1; + } + + return 0; +} +EXPORT_SYMBOL_GPL(snd_soc_usb_get_selected_device); + /** * snd_soc_usb_find_priv_data() - Retrieve private data stored * @usbdev: device reference @@ -191,6 +322,30 @@ int snd_soc_usb_find_supported_format(int card_idx, } EXPORT_SYMBOL_GPL(snd_soc_usb_find_supported_format); +/* Not under ctx_mutex: the port is not yet, or no longer, on usb_ctx_list */ +static int snd_soc_usb_alloc_routes(struct snd_soc_usb *usb, int direction, + unsigned int num_routes) +{ + struct snd_soc_usb_route *routes; + unsigned int i; + + if (!num_routes) + return -EINVAL; + + routes = kcalloc(num_routes, sizeof(*routes), GFP_KERNEL); + if (!routes) + return -ENOMEM; + + for (i = 0; i < num_routes; i++) + routes[i].pcm_idx = -1; + + kfree(usb->routes[direction]); + usb->routes[direction] = routes; + usb->num_routes[direction] = num_routes; + + return 0; +} + /** * snd_soc_usb_allocate_port() - allocate a SoC USB port for offloading support * @component: USB DPCM backend DAI component @@ -200,13 +355,15 @@ EXPORT_SYMBOL_GPL(snd_soc_usb_find_supported_format); * different USB audio devices attached, in order to start audio offloading handled * by an ASoC entity. USB device plug in/out events are signaled with a * notification, but don't directly impact the memory allocated for the SoC USB - * port. + * port. The port starts out with one route per direction, see + * snd_soc_usb_set_num_routes(). * */ struct snd_soc_usb *snd_soc_usb_allocate_port(struct snd_soc_component *component, void *data) { struct snd_soc_usb *usb; + int i; usb = kzalloc_obj(*usb); if (!usb) @@ -215,10 +372,40 @@ struct snd_soc_usb *snd_soc_usb_allocate_port(struct snd_soc_component *componen usb->component = component; usb->priv_data = data; + INIT_LIST_HEAD(&usb->available_devices); + for (i = 0; i <= SNDRV_PCM_STREAM_LAST; i++) { + if (snd_soc_usb_alloc_routes(usb, i, 1)) { + snd_soc_usb_free_port(usb); + return ERR_PTR(-ENOMEM); + } + } + return usb; } EXPORT_SYMBOL_GPL(snd_soc_usb_allocate_port); +/** + * snd_soc_usb_set_num_routes() - Set the number of offload routes of a direction + * @usb: allocated SoC USB port, not yet added with snd_soc_usb_add_port() + * @direction: SNDRV_PCM_STREAM_PLAYBACK or SNDRV_PCM_STREAM_CAPTURE + * @num_routes: streams the backend can offload at a time in @direction + * + * A backend running several offloaded streams in a direction at a time gets + * a route per stream, so that each can be pointed at a (device, pcm) pair of + * its own. To be called before the port is added. + * + * Return: 0 on success, negative on error. + */ +int snd_soc_usb_set_num_routes(struct snd_soc_usb *usb, int direction, + unsigned int num_routes) +{ + if (!usb || direction < 0 || direction > SNDRV_PCM_STREAM_LAST) + return -EINVAL; + + return snd_soc_usb_alloc_routes(usb, direction, num_routes); +} +EXPORT_SYMBOL_GPL(snd_soc_usb_set_num_routes); + /** * snd_soc_usb_free_port() - free a SoC USB port used for offloading support * @usb: allocated SoC USB port @@ -229,7 +416,11 @@ EXPORT_SYMBOL_GPL(snd_soc_usb_allocate_port); */ void snd_soc_usb_free_port(struct snd_soc_usb *usb) { + int i; + snd_soc_usb_remove_port(usb); + for (i = 0; i <= SNDRV_PCM_STREAM_LAST; i++) + kfree(usb->routes[i]); kfree(usb); } EXPORT_SYMBOL_GPL(snd_soc_usb_free_port); @@ -275,6 +466,20 @@ void snd_soc_usb_remove_port(struct snd_soc_usb *usb) } EXPORT_SYMBOL_GPL(snd_soc_usb_remove_port); +/* Caller holds ctx_mutex. Whether @sdev is on the port's available_devices. */ +static bool snd_soc_usb_device_listed(struct snd_soc_usb *ctx, + struct snd_soc_usb_device *sdev) +{ + struct snd_soc_usb_device *entry; + + list_for_each_entry(entry, &ctx->available_devices, available_node) { + if (entry == sdev) + return true; + } + + return false; +} + /** * snd_soc_usb_connect() - Notification of USB device connection * @usbdev: USB bus device @@ -284,26 +489,40 @@ EXPORT_SYMBOL_GPL(snd_soc_usb_remove_port); * handle how the DPCM backend selects, which device to enable USB offloading * on. * + * Return: 0 on success, -ENODEV if no port is registered for @usbdev (the + * device is then not listed and must not be reported as disconnected later), + * -EEXIST if @sdev is connected already. */ int snd_soc_usb_connect(struct device *usbdev, struct snd_soc_usb_device *sdev) { struct snd_soc_usb *ctx; + int ret = 0; if (!usbdev) return -ENODEV; mutex_lock(&ctx_mutex); ctx = snd_soc_find_usb_ctx(usbdev); - if (!ctx) + if (!ctx) { + ret = -ENODEV; + goto exit; + } + + /* the device is linked into the list through itself, it can only be listed once */ + if (WARN_ON(snd_soc_usb_device_listed(ctx, sdev))) { + ret = -EEXIST; goto exit; + } if (ctx->connection_status_cb) ctx->connection_status_cb(ctx, sdev, true); + list_add_tail(&sdev->available_node, &ctx->available_devices); + exit: mutex_unlock(&ctx_mutex); - return 0; + return ret; } EXPORT_SYMBOL_GPL(snd_soc_usb_connect); @@ -330,6 +549,12 @@ int snd_soc_usb_disconnect(struct device *usbdev, struct snd_soc_usb_device *sde if (ctx->connection_status_cb) ctx->connection_status_cb(ctx, sdev, false); + /* a device the platform never connected has nothing to take back */ + if (!snd_soc_usb_device_listed(ctx, sdev)) + goto exit; + + list_del(&sdev->available_node); + exit: mutex_unlock(&ctx_mutex); From 809d0c28e34465351c5bccf598f10adc576447af Mon Sep 17 00:00:00 2001 From: Peter Ujfalusi Date: Wed, 4 Nov 2026 14:27:30 +0200 Subject: [PATCH 27/53] ASoC: soc-usb: Add USB offload route controls Add writable enumerated controls that let userspace select the USB device and PCM each offloaded stream of a direction goes to. Every (device, pcm) pair of a direction is one item, and with no device connected the only item is "None", so the control stays valid. Each route gets a control of its own, "USB Offload 0 Playback Route", "USB Offload 1 Playback Route" and so on; the number is the route and is always there, so the names do not depend on the stream count. Swapping two devices between two routes is two plain writes. A route is pointed at a pair by writing its control, which pins it to that pair until the device is disconnected, when it falls back to its default. The backend can veto a write which would change what a route points at: the optional is_route_busy() callback is asked about the route, and the write fails with -EBUSY while a stream is set up on it. Signed-off-by: Peter Ujfalusi --- Documentation/sound/soc/usb.rst | 97 ++++++++++++++--- include/sound/soc-usb.h | 17 +++ sound/soc/soc-usb.c | 179 +++++++++++++++++++++++++++++++- 3 files changed, 275 insertions(+), 18 deletions(-) diff --git a/Documentation/sound/soc/usb.rst b/Documentation/sound/soc/usb.rst index 146d6793d6a6a4..f359636f2a637f 100644 --- a/Documentation/sound/soc/usb.rst +++ b/Documentation/sound/soc/usb.rst @@ -57,8 +57,11 @@ Structures - ``connection_status_cb``: callback to notify connection events - ``update_offload_route_info``: callback to fetch selected USB sound card/PCM device + - ``is_route_busy``: callback reporting whether the backend has a stream open + on a route, consulted before a route selection is changed - ``available_devices``: the connected ``snd_soc_usb_device`` entries - - ``routes``, ``num_routes``: the offload routes of each direction + - ``routes``, ``num_routes``: the offload routes of each direction, see + `Platform Card Route Kcontrols`_ - ``priv_data``: driver data The snd_soc_usb structure can be referenced using the ASoC platform card @@ -76,7 +79,7 @@ registered with, so the caller passes that device. - ``cpcm_idx``: capture pcm device indexes associated with the USB sound device - ``ppcm_idx``: playback pcm device indexes associated with the USB sound device - ``cpcm_names``, ``ppcm_names``: optional human readable names of the PCMs, - parallel to the index arrays + parallel to the index arrays, shown as the items of the route kcontrols - ``num_playback``: number of playback streams - ``num_capture``: number of capture streams - ``list``: list head for the backend's own use @@ -115,7 +118,8 @@ Returns 0 on success, and -EOPNOTSUPP on failure. audio device detection. This can be utilized in the BE DAI driver to keep track of available USB audio devices. This is intended to be called by the USB offload driver residing in USB SND. The device is -added to ``available_devices``. +added to ``available_devices``. A route nothing was picked for follows its +default pair, see the selection rules below. Returns 0 on success, -ENODEV when no port is registered for ``usbdev`` (the device is then not listed and must not be reported as disconnected later), @@ -132,7 +136,8 @@ failures. **snd_soc_usb_disconnect()** notifies the ASoC USB DPCM BE DAI link of a USB audio device removal. This is intended to be called by the USB offload -driver that resides in USB SND. +driver that resides in USB SND. Every route that was picked for the device +goes back to its default pair. .. code-block:: rst @@ -212,9 +217,25 @@ Returns a pointer to struct soc_usb on success, negative on error. - ``num_routes``: streams the backend can offload at a time in ``direction`` **snd_soc_usb_set_num_routes()** gives a backend which runs several offloaded -streams of a direction at a time a route per stream, so that each can be -pointed at a (device, PCM) pair of its own. To be called before the port is -added. +streams of a direction at a time a route, and with +snd_soc_usb_setup_offload_route_ctl() a kcontrol, per stream, so that each can +be pointed at a (device, PCM) pair of its own. To be called before the route +kcontrols are created and the port is added. + +Returns 0 on success, negative on error. + +.. code-block:: rst + + int snd_soc_usb_setup_offload_route_ctl(struct snd_soc_component *component, + struct snd_soc_usb *usb); +.. + + - ``component``: DPCM BE DAI link component + - ``usb``: SoC USB device to attach the controls to + +**snd_soc_usb_setup_offload_route_ctl()** creates one writable enumerated +kcontrol per route of each direction on the platform sound card, see +`Platform Card Route Kcontrols`_. Returns 0 on success, negative on error. @@ -234,11 +255,7 @@ Returns 0 on success, negative on error. **snd_soc_usb_get_selected_device()** returns the (device, PCM) pair a route currently points at, for the backend to consult when it starts a stream -instead of picking a device itself. A route nothing was picked for follows its -default: route N gets the Nth available pair of its direction, wrapping around -when there are fewer pairs than routes, so that the routes spread over what is -connected. The default is worked out on every query, it is not stored, so it -moves as devices connect and disconnect. +instead of picking a device itself. Returns 0 on success (also when nothing is selected), negative on error. @@ -393,9 +410,17 @@ USB Offload Related Kcontrols Details ------- A set of kcontrols can be utilized by applications to help select the proper sound -devices to enable USB audio offloading. SoC USB exposes the get_offload_dev() -callback that designs can use to ensure that the proper indices are returned to the -application. +devices to enable USB audio offloading. Two models are available: read-only +controls on the USB sound card reporting where a USB PCM is offloaded to +(`USB Sound Card Route Kcontrols`_), and writable controls on the platform +sound card selecting which USB device an offloaded stream goes to +(`Platform Card Route Kcontrols`_). A backend uses the one that fits how +its DSP is driven; the two can coexist. + +USB Sound Card Route Kcontrols +------------------------------ +SoC USB exposes the get_offload_dev() callback that designs can use to ensure +that the proper indices are returned to the application. Implementation -------------- @@ -510,6 +535,48 @@ execute the registered function calls to the DPCM BE DAI link. ret = snd_soc_usb_add_port(usb); .. +Platform Card Route Kcontrols +----------------------------- +A backend whose offloaded PCMs exist on the platform card whether or not a +USB device is connected, and which can offload any connected device, lets +the user choose the device instead: it has a *route* per stream it can +offload at a time in a direction (see snd_soc_usb_set_num_routes()), and +snd_soc_usb_setup_offload_route_ctl() creates a writable enumerated kcontrol +per route on the platform card: + + ``USB Offload Playback Route`` + + ``USB Offload Capture Route`` + +```` is the route and is always present, also with a single route per +direction, so the names do not depend on how many streams a backend runs. +A backend pairs its PCMs with the controls by naming them the same way, +``USB Offload ``. + +The items of a control are every (device, PCM) pair of the direction over +all connected devices, in connection order and then PCM order, named by the +backend through ``ppcm_names``/``cpcm_names``; a name should start with the +USB card and PCM numbers, ``,``, so that a sound server can match +the device it targets. The item list is rebuilt on every query, so it +always reflects what is connected; with nothing connected the single item is +``None``. + +Selection rules: + + - A route nothing was picked for follows its default: route N gets the Nth + available pair of its direction, wrapping around when there are fewer + pairs than routes, so that the routes spread over what is connected. + The default is worked out on every query, it is not stored, so it moves + as devices connect and disconnect. + - A write picks a pair for the route. Picking the pair the route already + points at pins it there. + - On disconnect, routes picked for the device go back to their default. + - A write is refused with ``-EBUSY`` while the backend reports the route + busy through ``is_route_busy()``, i.e. while a stream is open on it. + Writing the current selection is a no-op. A selection by itself claims + no USB or DSP resource; the backend reads it with + snd_soc_usb_get_selected_device() when a stream starts. + Existing USB Sound Kcontrol --------------------------- With the introduction of USB offload support, the above USB offload kcontrol diff --git a/include/sound/soc-usb.h b/include/sound/soc-usb.h index a6f6cf777ce35c..d0807f0aded3e2 100644 --- a/include/sound/soc-usb.h +++ b/include/sound/soc-usb.h @@ -48,6 +48,8 @@ struct snd_soc_usb_device { * @sdev: the device picked for the route, NULL while nothing is picked and the * route follows its default * @pcm_idx: the PCM index of @sdev picked, -1 while nothing is picked + * @kctl: the enumerated route kcontrol, once created by + * snd_soc_usb_setup_offload_route_ctl() * * A backend able to run several offloaded streams in a direction at a time * has a route per stream. Two routes may point at the same pair; only one of @@ -56,6 +58,7 @@ struct snd_soc_usb_device { struct snd_soc_usb_route { struct snd_soc_usb_device *sdev; int pcm_idx; + struct snd_kcontrol *kctl; }; /** @@ -69,6 +72,10 @@ struct snd_soc_usb_route { * an array used for a kcontrol output for either * the card or pcm index. "path" determines the * which entry to look for. (ie mapped card or pcm) + * @is_route_busy: callback reporting whether the backend already has a + * stream open on route @route of the given direction. Consulted + * before a route change is allowed, so a link a stream is + * actively using is never switched out from under it. * @available_devices: list of currently connected snd_soc_usb_device entries * @routes: the routes of each direction, @num_routes of them * @num_routes: number of routes per direction, one unless the backend set @@ -85,6 +92,8 @@ struct snd_soc_usb { int card, int pcm, int direction, enum snd_soc_usb_kctl path, long *route); + bool (*is_route_busy)(struct snd_soc_usb *usb, int direction, + unsigned int route); struct list_head available_devices; struct snd_soc_usb_route *routes[SNDRV_PCM_STREAM_LAST + 1]; @@ -116,6 +125,8 @@ int snd_soc_usb_set_num_routes(struct snd_soc_usb *usb, int direction, void snd_soc_usb_add_port(struct snd_soc_usb *usb); void snd_soc_usb_remove_port(struct snd_soc_usb *usb); +int snd_soc_usb_setup_offload_route_ctl(struct snd_soc_component *component, + struct snd_soc_usb *usb); int snd_soc_usb_get_selected_device(struct snd_soc_usb *usb, int direction, unsigned int route, struct snd_soc_usb_device **sdev, @@ -180,6 +191,12 @@ static inline void snd_soc_usb_add_port(struct snd_soc_usb *usb) static inline void snd_soc_usb_remove_port(struct snd_soc_usb *usb) { } +static inline int snd_soc_usb_setup_offload_route_ctl(struct snd_soc_component *component, + struct snd_soc_usb *usb) +{ + return 0; +} + static inline int snd_soc_usb_get_selected_device(struct snd_soc_usb *usb, int direction, unsigned int route, struct snd_soc_usb_device **sdev, diff --git a/sound/soc/soc-usb.c b/sound/soc/soc-usb.c index 2a94ae44ab2e05..3040e4c7bf5e7e 100644 --- a/sound/soc/soc-usb.c +++ b/sound/soc/soc-usb.c @@ -245,7 +245,8 @@ EXPORT_SYMBOL_GPL(snd_soc_usb_update_offload_route); * @pcm_index: set to the routed device's USB SND PCM device index * * A backend consults this at stream acquire time instead of picking a device - * itself, so the stream goes to whatever the route currently points at. + * itself, so the stream goes to whatever the route kcontrol (or the + * connect-time default) currently points at. * * Return: 0 on success (even if nothing is selected - check @sdev), negative * on error. @@ -391,8 +392,9 @@ EXPORT_SYMBOL_GPL(snd_soc_usb_allocate_port); * @num_routes: streams the backend can offload at a time in @direction * * A backend running several offloaded streams in a direction at a time gets - * a route per stream, so that each can be pointed at a (device, pcm) pair of - * its own. To be called before the port is added. + * a route, and with snd_soc_usb_setup_offload_route_ctl() a kcontrol, per + * stream, so that each can be pointed at a (device, pcm) pair of its own. + * To be called before the route kcontrols are created and the port is added. * * Return: 0 on success, negative on error. */ @@ -537,6 +539,7 @@ EXPORT_SYMBOL_GPL(snd_soc_usb_connect); int snd_soc_usb_disconnect(struct device *usbdev, struct snd_soc_usb_device *sdev) { struct snd_soc_usb *ctx; + int direction; if (!usbdev) return -ENODEV; @@ -555,6 +558,20 @@ int snd_soc_usb_disconnect(struct device *usbdev, struct snd_soc_usb_device *sde list_del(&sdev->available_node); + /* a route picked for the device falls back to its default */ + for (direction = 0; direction <= SNDRV_PCM_STREAM_LAST; direction++) { + unsigned int route; + + for (route = 0; route < ctx->num_routes[direction]; route++) { + struct snd_soc_usb_route *r = &ctx->routes[direction][route]; + + if (r->sdev == sdev) { + r->sdev = NULL; + r->pcm_idx = -1; + } + } + } + exit: mutex_unlock(&ctx_mutex); @@ -562,5 +579,161 @@ int snd_soc_usb_disconnect(struct device *usbdev, struct snd_soc_usb_device *sde } EXPORT_SYMBOL_GPL(snd_soc_usb_disconnect); +/* The kcontrol's private_value carries the direction and the route */ +#define SND_SOC_USB_KCTL_VALUE(direction, route) ((direction) | ((route) << 8)) +#define SND_SOC_USB_KCTL_DIRECTION(value) ((value) & 0xff) +#define SND_SOC_USB_KCTL_ROUTE(value) ((value) >> 8) + +static int snd_soc_usb_route_info(struct snd_kcontrol *kcontrol, + struct snd_ctl_elem_info *uinfo) +{ + struct snd_soc_usb *usb = snd_kcontrol_chip(kcontrol); + int direction = SND_SOC_USB_KCTL_DIRECTION(kcontrol->private_value); + struct snd_soc_usb_route_entry entry; + unsigned int count; + + uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; + uinfo->count = 1; + + guard(mutex)(&ctx_mutex); + + for (count = 0; snd_soc_usb_route_entry_at(usb, direction, count, &entry); count++) + ; + + /* with no route available the only item is "None" */ + if (!count) { + uinfo->value.enumerated.items = 1; + uinfo->value.enumerated.item = 0; + strscpy(uinfo->value.enumerated.name, "None", + sizeof(uinfo->value.enumerated.name)); + return 0; + } + + uinfo->value.enumerated.items = count; + + if (uinfo->value.enumerated.item >= count) + uinfo->value.enumerated.item = count - 1; + + snd_soc_usb_route_entry_at(usb, direction, uinfo->value.enumerated.item, &entry); + strscpy(uinfo->value.enumerated.name, entry.name, + sizeof(uinfo->value.enumerated.name)); + + return 0; +} + +static int snd_soc_usb_route_get(struct snd_kcontrol *kcontrol, + struct snd_ctl_elem_value *ucontrol) +{ + struct snd_soc_usb *usb = snd_kcontrol_chip(kcontrol); + int direction = SND_SOC_USB_KCTL_DIRECTION(kcontrol->private_value); + unsigned int route = SND_SOC_USB_KCTL_ROUTE(kcontrol->private_value); + struct snd_soc_usb_route_entry entry; + unsigned int index = 0; + + scoped_guard(mutex, &ctx_mutex) { + /* item 0 is "None" when nothing is available */ + if (!snd_soc_usb_route_resolve(usb, direction, route, &entry, &index)) + index = 0; + } + + ucontrol->value.enumerated.item[0] = index; + + return 0; +} + +static int snd_soc_usb_route_put(struct snd_kcontrol *kcontrol, + struct snd_ctl_elem_value *ucontrol) +{ + struct snd_soc_usb *usb = snd_kcontrol_chip(kcontrol); + int direction = SND_SOC_USB_KCTL_DIRECTION(kcontrol->private_value); + unsigned int route = SND_SOC_USB_KCTL_ROUTE(kcontrol->private_value); + unsigned int requested = ucontrol->value.enumerated.item[0]; + struct snd_soc_usb_route *r = &usb->routes[direction][route]; + struct snd_soc_usb_route_entry entry, current_entry; + unsigned int current_index; + bool changed; + + guard(mutex)(&ctx_mutex); + + /* item 0 is "None" when nothing is available, accept it as a no-op */ + if (!snd_soc_usb_route_entry_at(usb, direction, requested, &entry)) + return requested ? -EINVAL : 0; + + changed = !snd_soc_usb_route_resolve(usb, direction, route, ¤t_entry, + ¤t_index) || + current_index != requested; + + if (changed && usb->is_route_busy && usb->is_route_busy(usb, direction, route)) + return -EBUSY; + + /* picking what the route points at already pins it there */ + r->sdev = entry.sdev; + r->pcm_idx = entry.pcm_idx; + + return changed; +} + +static const struct snd_kcontrol_new snd_soc_usb_route_ctl_template = { + .iface = SNDRV_CTL_ELEM_IFACE_CARD, + .info = snd_soc_usb_route_info, + .get = snd_soc_usb_route_get, + .put = snd_soc_usb_route_put, +}; + +/** + * snd_soc_usb_setup_offload_route_ctl() - Create the offload route kcontrols + * @component: USB DPCM backend DAI component + * @usb: soc usb port to attach the controls to + * + * Creates one writable enumerated kcontrol per route of each direction, + * "USB Offload 0 Playback Route", "USB Offload 1 Playback Route", "USB Offload + * 0 Capture Route" and so on, letting userspace pick which of the currently + * connected devices' PCMs the offloaded stream of that route is routed to. + * The number is the route and matches the backend's ordinal, whatever the + * number of routes a direction has. The item list is rebuilt live from + * usb->available_devices on every query, so it always reflects what is + * currently connected; with nothing connected the single item is "None". + * + * Return: 0 on success, negative on error. + */ +int snd_soc_usb_setup_offload_route_ctl(struct snd_soc_component *component, + struct snd_soc_usb *usb) +{ + static const char * const names[] = { + [SNDRV_PCM_STREAM_PLAYBACK] = "Playback", + [SNDRV_PCM_STREAM_CAPTURE] = "Capture", + }; + int direction, ret; + + for (direction = 0; direction <= SNDRV_PCM_STREAM_LAST; direction++) { + unsigned int route; + + for (route = 0; route < usb->num_routes[direction]; route++) { + struct snd_kcontrol_new kctl = snd_soc_usb_route_ctl_template; + char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN]; + struct snd_kcontrol *kcontrol; + + snprintf(name, sizeof(name), "USB Offload %u %s Route", + route, names[direction]); + + kctl.name = name; + kctl.private_value = SND_SOC_USB_KCTL_VALUE(direction, route); + + kcontrol = snd_ctl_new1(&kctl, usb); + if (!kcontrol) + return -ENOMEM; + + ret = snd_ctl_add(component->card->snd_card, kcontrol); + if (ret < 0) + return ret; + + usb->routes[direction][route].kctl = kcontrol; + } + } + + return 0; +} +EXPORT_SYMBOL_GPL(snd_soc_usb_setup_offload_route_ctl); + MODULE_LICENSE("GPL"); MODULE_DESCRIPTION("SoC USB driver for offloading"); From f3ec4be1cca300320c2b363fa3fb4bdf1c781cb7 Mon Sep 17 00:00:00 2001 From: Peter Ujfalusi Date: Wed, 4 Nov 2026 14:27:30 +0200 Subject: [PATCH 28/53] ASoC: soc-usb: Notify the route controls when a device connects or disconnects Connecting or disconnecting a device changes the items of every route control, and the pair of the routes which follow the default. Notify SNDRV_CTL_EVENT_MASK_INFO and SNDRV_CTL_EVENT_MASK_VALUE on all of them, so that userspace re-reads both. Signed-off-by: Peter Ujfalusi --- Documentation/sound/soc/usb.rst | 8 ++++++-- sound/soc/soc-usb.c | 29 +++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/Documentation/sound/soc/usb.rst b/Documentation/sound/soc/usb.rst index f359636f2a637f..ea0b1eef20d2b8 100644 --- a/Documentation/sound/soc/usb.rst +++ b/Documentation/sound/soc/usb.rst @@ -118,8 +118,9 @@ Returns 0 on success, and -EOPNOTSUPP on failure. audio device detection. This can be utilized in the BE DAI driver to keep track of available USB audio devices. This is intended to be called by the USB offload driver residing in USB SND. The device is -added to ``available_devices``. A route nothing was picked for follows its -default pair, see the selection rules below. +added to ``available_devices`` and the route controls are notified. A route +nothing was picked for follows its default pair, see the selection rules +below. Returns 0 on success, -ENODEV when no port is registered for ``usbdev`` (the device is then not listed and must not be reported as disconnected later), @@ -577,6 +578,9 @@ Selection rules: no USB or DSP resource; the backend reads it with snd_soc_usb_get_selected_device() when a stream starts. +Control ``INFO`` and ``VALUE`` change notifications are sent whenever a +connect, a disconnect or a write changes the item list or the selection. + Existing USB Sound Kcontrol --------------------------- With the introduction of USB offload support, the above USB offload kcontrol diff --git a/sound/soc/soc-usb.c b/sound/soc/soc-usb.c index 3040e4c7bf5e7e..eac69451a63b0a 100644 --- a/sound/soc/soc-usb.c +++ b/sound/soc/soc-usb.c @@ -468,6 +468,31 @@ void snd_soc_usb_remove_port(struct snd_soc_usb *usb) } EXPORT_SYMBOL_GPL(snd_soc_usb_remove_port); +/* + * Caller holds ctx_mutex. The device list changed: the items of every route + * control did, and so can the pair a route which was not picked for follows. + */ +static void snd_soc_usb_notify_routes_changed(struct snd_soc_usb *ctx) +{ + int direction; + + if (!ctx->component || !ctx->component->card) + return; + + for (direction = 0; direction <= SNDRV_PCM_STREAM_LAST; direction++) { + unsigned int route; + + for (route = 0; route < ctx->num_routes[direction]; route++) { + struct snd_kcontrol *kctl = ctx->routes[direction][route].kctl; + + if (kctl) + snd_ctl_notify(ctx->component->card->snd_card, + SNDRV_CTL_EVENT_MASK_INFO | + SNDRV_CTL_EVENT_MASK_VALUE, &kctl->id); + } + } +} + /* Caller holds ctx_mutex. Whether @sdev is on the port's available_devices. */ static bool snd_soc_usb_device_listed(struct snd_soc_usb *ctx, struct snd_soc_usb_device *sdev) @@ -521,6 +546,8 @@ int snd_soc_usb_connect(struct device *usbdev, struct snd_soc_usb_device *sdev) list_add_tail(&sdev->available_node, &ctx->available_devices); + snd_soc_usb_notify_routes_changed(ctx); + exit: mutex_unlock(&ctx_mutex); @@ -572,6 +599,8 @@ int snd_soc_usb_disconnect(struct device *usbdev, struct snd_soc_usb_device *sde } } + snd_soc_usb_notify_routes_changed(ctx); + exit: mutex_unlock(&ctx_mutex); From 817e8ffadac2122b463622a83ba925d5e41c5397 Mon Sep 17 00:00:00 2001 From: Peter Ujfalusi Date: Fri, 7 Aug 2026 16:46:34 +0300 Subject: [PATCH 29/53] ASoC: SOF: Intel: hda: Report the UAOL links to the machine driver The USB Audio Offload Link (UAOL) is enumerated as an alternate HDaudio multi-link by hda_bus_ml_init(), but nothing keeps track of it afterwards, so a machine driver has no way to learn how many UAOL backends it should create. Read the link count once the controller is initialized and pass it to the machine driver in mach_params as a link mask, next to the BT offload link mask, with a module parameter to override it for testing. The count comes from the SLCOUNT field of the link's AZX_REG_ML_LCAP, which can encode up to eight sublinks. Only sublink 0 can be reached: hda-mlink has no instance offset for UAOL, so every sublink resolves to the registers of sublink 0. Clamp the count to one. A single link is not a practical restriction, the firmware reports the number of streams it can carry and one link serves more than one endpoint. The mask describes what the controller exposes. Whether the firmware can serve those links is not known this early in the probe and is handled separately. Signed-off-by: Peter Ujfalusi --- include/sound/soc-acpi.h | 2 ++ sound/soc/sof/intel/hda.c | 29 +++++++++++++++++++++++++++++ sound/soc/sof/intel/hda.h | 3 +++ 3 files changed, 34 insertions(+) diff --git a/include/sound/soc-acpi.h b/include/sound/soc-acpi.h index 2ee303b791dc84..7bd928710eb3ae 100644 --- a/include/sound/soc-acpi.h +++ b/include/sound/soc-acpi.h @@ -74,6 +74,7 @@ static inline struct snd_soc_acpi_mach *snd_soc_acpi_codec_list(void *arg) * @subsystem_id_set: true if a value has been written to * subsystem_vendor and subsystem_device. * @bt_link_mask: BT offload link enabled on the board + * @uaol_link_mask: USB audio offload links available on the board */ struct snd_soc_acpi_mach_params { u32 acpi_ipc_irq_index; @@ -90,6 +91,7 @@ struct snd_soc_acpi_mach_params { unsigned short subsystem_rev; bool subsystem_id_set; u32 bt_link_mask; + u32 uaol_link_mask; }; /** diff --git a/sound/soc/sof/intel/hda.c b/sound/soc/sof/intel/hda.c index 45e968fb19104b..22110f03210fa5 100644 --- a/sound/soc/sof/intel/hda.c +++ b/sound/soc/sof/intel/hda.c @@ -500,6 +500,10 @@ static int bt_link_mask_override = -1; module_param_named(bt_link_mask, bt_link_mask_override, int, 0444); MODULE_PARM_DESC(bt_link_mask, "SOF BT offload link mask"); +static int uaol_link_mask_override = -1; +module_param_named(uaol_link_mask, uaol_link_mask_override, int, 0444); +MODULE_PARM_DESC(uaol_link_mask, "SOF USB audio offload link mask"); + static int hda_init(struct snd_sof_dev *sdev) { struct hda_bus *hbus; @@ -639,6 +643,16 @@ static int hda_init_caps(struct snd_sof_dev *sdev) return ret; } + /* the UAOL links have been enumerated by hda_bus_ml_init() */ + hdev->uaol_link_count = hdac_bus_eml_get_count(bus, true, + AZX_REG_ML_LEPTR_ID_INTEL_UAOL); + if (hdev->uaol_link_count) { + /* hda-mlink has no UAOL instance offset, the sublinks all alias to sublink 0 */ + dev_dbg(sdev->dev, "UAOL links on the controller: %d (usable: 1)\n", + hdev->uaol_link_count); + hdev->uaol_link_count = 1; + } + /* Skip SoundWire if it is not supported */ if (!(interface_mask & BIT(SOF_DAI_INTEL_ALH))) goto skip_soundwire; @@ -1537,6 +1551,7 @@ struct snd_soc_acpi_mach *hda_machine_select(struct snd_sof_dev *sdev) u32 interface_mask = hda_get_interface_mask(sdev); struct snd_sof_pdata *sof_pdata = sdev->pdata; const struct sof_dev_desc *desc = sof_pdata->desc; + struct sof_intel_hda_dev *hdev = sdev->pdata->hw_pdata; struct hdac_bus *bus = sof_to_bus(sdev); struct snd_soc_acpi_mach *mach = NULL; enum snd_soc_acpi_intel_codec codec_type, amp_type; @@ -1593,6 +1608,20 @@ struct snd_soc_acpi_mach *hda_machine_select(struct snd_sof_dev *sdev) mach->mach_params.bt_link_mask = 0; } + /* report the USB audio offload links enumerated on the controller */ + if (hdev->uaol_link_count) + mach->mach_params.uaol_link_mask = GENMASK(hdev->uaol_link_count - 1, 0); + + /* allow for module parameter override */ + if (uaol_link_mask_override != -1) { + dev_dbg(sdev->dev, "overriding UAOL link mask %#x by kernel param %#x\n", + mach->mach_params.uaol_link_mask, uaol_link_mask_override); + if (uaol_link_mask_override & ~mach->mach_params.uaol_link_mask) + dev_warn(sdev->dev, "UAOL link mask %#x exceeds the links on the controller %#x\n", + uaol_link_mask_override, mach->mach_params.uaol_link_mask); + mach->mach_params.uaol_link_mask = uaol_link_mask_override; + } + /* * Fixup tplg file name by appending dmic num, ssp num, codec/amplifier * name string if quirk flag is set. diff --git a/sound/soc/sof/intel/hda.h b/sound/soc/sof/intel/hda.h index bc11519427649f..e06608022ef4dd 100644 --- a/sound/soc/sof/intel/hda.h +++ b/sound/soc/sof/intel/hda.h @@ -523,6 +523,9 @@ struct sof_intel_hda_dev { /* the maximum number of streams (playback + capture) supported */ u32 stream_max; + /* USB Audio Offload links on the controller, 0 if UAOL is not present */ + int uaol_link_count; + /* * ACE2+ link DMA stream allocation constraints (stream index = * stream_tag - 1, shared between input and output directions). All From 66e1545c08d56ae95c8b089e5150ba6af9c2f995 Mon Sep 17 00:00:00 2001 From: Peter Ujfalusi Date: Thu, 17 Sep 2026 11:06:06 +0300 Subject: [PATCH 30/53] ASoC: SOF: ipc4-loader: Query the UAOL support from the firmware Whether a booted firmware can do USB Audio Offload, and to what extent, is described by two parts of the firmware configuration, both ignored so far. Firmware configuration tuple 21, which the kernel names RESERVED, tells whether the firmware has been built with UAOL support. The hardware configuration carries the IPC4_UAOL_CAPS_HW_CFG tuple: the number of UAOL links and, for each, the supported input, output and bidirectional stream counts and the maximum TX and RX FIFO sizes, i.e. how many streams can be offloaded before the resources run out. Parse both, store them and print the capabilities for debugging. The link count comes from the firmware and is used as the length of a flexible array member, so validate it against the tuple size before copying. Signed-off-by: Peter Ujfalusi --- include/sound/sof/ipc4/header.h | 18 +++++++++++- sound/soc/sof/ipc4-loader.c | 52 +++++++++++++++++++++++++++++++++ sound/soc/sof/ipc4-priv.h | 5 ++++ 3 files changed, 74 insertions(+), 1 deletion(-) diff --git a/include/sound/sof/ipc4/header.h b/include/sound/sof/ipc4/header.h index 0f6f1de3eee90f..70ba718519ba9f 100644 --- a/include/sound/sof/ipc4/header.h +++ b/include/sound/sof/ipc4/header.h @@ -434,7 +434,7 @@ enum sof_ipc4_fw_config_params { SOF_IPC4_FW_CFG_SCHEDULER_CONFIG, SOF_IPC4_FW_CFG_XTAL_FREQ_HZ, SOF_IPC4_FW_CFG_CLOCKS_CONFIG, - SOF_IPC4_FW_CFG_RESERVED, + SOF_IPC4_FW_CFG_UAOL_SUPPORT, SOF_IPC4_FW_CFG_POWER_GATING_POLICY, SOF_IPC4_FW_CFG_ASSERT_MODE, /* Reserved: 24 - 28 */ @@ -467,9 +467,25 @@ struct sof_ipc4_dx_state_info { } __packed __aligned(4); enum sof_ipc4_hw_config_params { + SOF_IPC4_HW_CFG_INTEL_UAOL_CAPS = 10, SOF_IPC4_HW_CFG_INTEL_MIC_PRIVACY_CAPS = 11, }; +#define SOF_IPC4_UAOL_INPUT_STREAMS_MASK GENMASK(3, 0) +#define SOF_IPC4_UAOL_OUTPUT_STREAMS_MASK GENMASK(7, 4) +#define SOF_IPC4_UAOL_BIDIRECTIONAL_STREAMS_MASK GENMASK(12, 8) + +struct sof_ipc4_intel_uaol_link_caps { + u32 stream_caps; + u32 max_tx_fifo_size; + u32 max_rx_fifo_size; +} __packed __aligned(4); + +struct sof_ipc4_intel_uaol_caps { + u32 link_count; + struct sof_ipc4_intel_uaol_link_caps link_caps[]; +} __packed __aligned(4); + #define SOF_IPC_INTEL_MIC_PRIVACY_VERSION_PTL 1 struct sof_ipc4_intel_mic_privacy_cap { diff --git a/sound/soc/sof/ipc4-loader.c b/sound/soc/sof/ipc4-loader.c index 1da51f7490e579..fb35249044f78f 100644 --- a/sound/soc/sof/ipc4-loader.c +++ b/sound/soc/sof/ipc4-loader.c @@ -5,6 +5,7 @@ // // Copyright(c) 2022 Intel Corporation +#include #include #include #include @@ -449,6 +450,47 @@ static int sof_ipc4_query_sof_info(struct snd_sof_dev *sdev, return 0; } +static int sof_ipc4_query_uaol_caps(struct snd_sof_dev *sdev, void *caps_data, + size_t caps_size) +{ + struct sof_ipc4_fw_data *ipc4_data = sdev->private; + struct sof_ipc4_intel_uaol_caps *caps = caps_data; + size_t expected_size; + u32 i; + + if (caps_size < sizeof(*caps)) { + dev_err(sdev->dev, "Invalid UAOL capabilities size %zu\n", caps_size); + return -EINVAL; + } + + expected_size = struct_size(caps, link_caps, caps->link_count); + if (expected_size > caps_size) { + dev_err(sdev->dev, "Invalid UAOL link count %u for size %zu\n", + caps->link_count, caps_size); + return -EINVAL; + } + + ipc4_data->uaol_caps = devm_kmemdup(sdev->dev, caps, expected_size, GFP_KERNEL); + if (!ipc4_data->uaol_caps) + return -ENOMEM; + + dev_dbg(sdev->dev, "UAOL links reported by firmware: %u\n", caps->link_count); + + for (i = 0; i < caps->link_count; i++) { + struct sof_ipc4_intel_uaol_link_caps *link = &caps->link_caps[i]; + + dev_dbg(sdev->dev, + "UAOL link %u: streams in/out/bidir %u/%u/%u, FIFO tx/rx %u/%u\n", i, + (u32)FIELD_GET(SOF_IPC4_UAOL_INPUT_STREAMS_MASK, link->stream_caps), + (u32)FIELD_GET(SOF_IPC4_UAOL_OUTPUT_STREAMS_MASK, link->stream_caps), + (u32)FIELD_GET(SOF_IPC4_UAOL_BIDIRECTIONAL_STREAMS_MASK, + link->stream_caps), + link->max_tx_fifo_size, link->max_rx_fifo_size); + } + + return 0; +} + int sof_ipc4_query_fw_configuration(struct snd_sof_dev *sdev) { struct sof_ipc4_fw_data *ipc4_data = sdev->private; @@ -524,6 +566,11 @@ int sof_ipc4_query_fw_configuration(struct snd_sof_dev *sdev) */ ipc4_data->libraries_restored = ipc4_data->fw_context_save; break; + case SOF_IPC4_FW_CFG_UAOL_SUPPORT: + ipc4_data->uaol_supported = *tuple->value; + trace_sof_ipc4_fw_config(sdev, "UAOL support", + *tuple->value); + break; case SOF_IPC4_FW_CFG_SOF_INFO: ret = sof_ipc4_query_sof_info(sdev, tuple->value, tuple->size); if (ret) @@ -554,6 +601,11 @@ int sof_ipc4_query_fw_configuration(struct snd_sof_dev *sdev) tuple = (struct sof_ipc4_tuple *)((u8 *)msg.data_ptr + offset); switch (tuple->type) { + case SOF_IPC4_HW_CFG_INTEL_UAOL_CAPS: + /* a layout we cannot parse costs UAOL, not the DSP */ + if (sof_ipc4_query_uaol_caps(sdev, tuple->value, tuple->size)) + dev_warn(sdev->dev, "UAOL capabilities ignored\n"); + break; case SOF_IPC4_HW_CFG_INTEL_MIC_PRIVACY_CAPS: if (ipc4_data->intel_configure_mic_privacy) { struct sof_ipc4_intel_mic_privacy_cap *caps; diff --git a/sound/soc/sof/ipc4-priv.h b/sound/soc/sof/ipc4-priv.h index 31f7d68029ce71..175690f12c6062 100644 --- a/sound/soc/sof/ipc4-priv.h +++ b/sound/soc/sof/ipc4-priv.h @@ -80,6 +80,9 @@ struct snd_ipc4_nhlt { * @fw_context_save: Firmware supports full context save and restore * @libraries_restored: The libraries have been retained during firmware boot * @nhlt_list: The NHLT tables from the BIOS and the topology manifest + * @uaol_supported: The firmware has been built with UAOL support + * @uaol_caps: UAOL capabilities reported by the firmware, NULL if they were not + * reported * * @codec_info: Information about the available codecs in booted firmware. The * data is to be used by the code for compressed support. @@ -98,6 +101,8 @@ struct sof_ipc4_fw_data { bool fw_context_save; bool libraries_restored; struct list_head nhlt_list; + bool uaol_supported; + struct sof_ipc4_intel_uaol_caps *uaol_caps; void *codec_info; From 8fbb12e9d5a16d40af124405245e68368ebf5cfe Mon Sep 17 00:00:00 2001 From: Peter Ujfalusi Date: Thu, 5 Nov 2026 09:11:04 +0200 Subject: [PATCH 31/53] ASoC: SOF: Let the platform finalize the machine params at registration The machine params are filled in when the machine is selected, before the firmware has booted, and the selected machine is not touched afterwards. The UAOL link mask is the first parameter that also depends on the booted firmware: the controller enumerates the links regardless of what the firmware can do with them, and a machine driver creating backends for them with a firmware that cannot serve them fails to load the topology. sof_machine_register() already hands the machine driver a copy of the selected machine. Add an optional finalize_mach_params() platform op and run it on that copy right before it is registered, so the platform can fold in what it only learned from the firmware while the selected machine stays as it was. Signed-off-by: Peter Ujfalusi --- sound/soc/sof/core.c | 19 +++++++++++-------- sound/soc/sof/sof-priv.h | 3 +++ 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/sound/soc/sof/core.c b/sound/soc/sof/core.c index 1f38fe03e49395..0a2076e1d78021 100644 --- a/sound/soc/sof/core.c +++ b/sound/soc/sof/core.c @@ -825,18 +825,21 @@ EXPORT_SYMBOL(snd_sof_device_shutdown); int sof_machine_register(struct snd_sof_dev *sdev, void *pdata) { struct snd_sof_pdata *plat_data = pdata; - const char *drv_name; - const void *mach; - int size; + struct snd_soc_acpi_mach mach; - drv_name = plat_data->machine->drv_name; - mach = plat_data->machine; - size = sizeof(*plat_data->machine); + /* + * The machine driver gets a copy of the selected machine. The platform + * can adjust the params of that copy with what it only learned from the + * booted firmware, the selected machine itself stays as it was. + */ + memcpy(&mach, plat_data->machine, sizeof(mach)); + if (sof_ops(sdev)->finalize_mach_params) + sof_ops(sdev)->finalize_mach_params(sdev, &mach.mach_params); /* register machine driver, pass machine info as pdata */ plat_data->pdev_mach = - platform_device_register_data(sdev->dev, drv_name, - PLATFORM_DEVID_NONE, mach, size); + platform_device_register_data(sdev->dev, mach.drv_name, + PLATFORM_DEVID_NONE, &mach, sizeof(mach)); if (IS_ERR(plat_data->pdev_mach)) return PTR_ERR(plat_data->pdev_mach); diff --git a/sound/soc/sof/sof-priv.h b/sound/soc/sof/sof-priv.h index 83e4d83cc27cbb..9bbd57e21fd32f 100644 --- a/sound/soc/sof/sof-priv.h +++ b/sound/soc/sof/sof-priv.h @@ -359,6 +359,9 @@ struct snd_sof_dsp_ops { struct snd_soc_acpi_mach * (*machine_select)(struct snd_sof_dev *sdev); /* optional */ void (*set_mach_params)(struct snd_soc_acpi_mach *mach, struct snd_sof_dev *sdev); /* optional */ + /* last word on the params handed to the machine driver, runs with the firmware booted */ + void (*finalize_mach_params)(struct snd_sof_dev *sdev, + struct snd_soc_acpi_mach_params *params); /* optional */ /* IPC client ops */ int (*register_ipc_clients)(struct snd_sof_dev *sdev); /* optional */ From 5a616297ce4bde662eb4e7704f5f47952f88c781 Mon Sep 17 00:00:00 2001 From: Peter Ujfalusi Date: Thu, 5 Nov 2026 09:11:32 +0200 Subject: [PATCH 32/53] ASoC: SOF: Intel: hda: Register with the USB audio offload library The USB audio offload library tracks offload capable USB audio devices and needs a host that owns the DSP. On Intel platforms that host is SOF, which reaches the xHCI controller over a USB Audio Offload Link (UAOL). Add the registration with the library, to run once the firmware has booted, and the unregistration on remove. One offload context in the library is one UAOL link, over which the DSP reaches an xHCI controller. The offload capabilities are part of the firmware configuration, which is only parsed after post_fw_run, so the registration is done from machine_register, the first hook that runs late enough. The hook is not installed yet: the library reports the devices it tracks to the driver, which needs the rest of the offload support in place to act on them. Implement finalize_mach_params() to tell the machine driver what the booted firmware can serve: a link with T output and I input streams carries T playback and I - T capture streams, the remaining T input streams being the clock feedback of the playback streams. Report the two counts in two new mach_params fields, with the static DAI count as the ceiling, and withdraw the link mask when the firmware does not support UAOL or the link has no usable stream. The firmware lays the UAOL link FIFOs out by stream, and the xHCI resource an endpoint gets is what selects the stream (stream = resource - 1), so a capture endpoint has to stay on the streams the firmware reads audio from and a clock feedback endpoint on the ones it reads feedback from. Implement the library's get_resource_range() op from the link's stream capabilities: playback on the output streams, capture on the input streams before the feedback ones, feedback on the last T input streams. With two output and four input streams this is resources 1-2, 3-4 and 5-6. SOF's pipeline only has S24_LE as 24-bit representation, while a USB device commonly packs 24-bit audio into 3 bytes. Set s24_3le_as_s24_le so the library treats the two as interchangeable during negotiation. The firmware applies the clock feedback of an asynchronous playback stream by spreading the samples linearly over the service interval, which is audible on the gear that uses asynchronous playback in the first place. Set async_playback_on_host so the library leaves such streams to the host. A failing registration only means that offload is unavailable, it does not fail the probe of the audio card. Signed-off-by: Peter Ujfalusi --- include/sound/soc-acpi.h | 4 + sound/soc/sof/intel/Kconfig | 16 ++ sound/soc/sof/intel/hda-common-ops.c | 1 + sound/soc/sof/intel/hda.c | 226 +++++++++++++++++++++++++++ sound/soc/sof/intel/hda.h | 19 +++ 5 files changed, 266 insertions(+) diff --git a/include/sound/soc-acpi.h b/include/sound/soc-acpi.h index 7bd928710eb3ae..09b011bd660a6f 100644 --- a/include/sound/soc-acpi.h +++ b/include/sound/soc-acpi.h @@ -75,6 +75,8 @@ static inline struct snd_soc_acpi_mach *snd_soc_acpi_codec_list(void *arg) * subsystem_vendor and subsystem_device. * @bt_link_mask: BT offload link enabled on the board * @uaol_link_mask: USB audio offload links available on the board + * @uaol_playback_num: playback streams of each USB audio offload link + * @uaol_capture_num: capture streams of each USB audio offload link */ struct snd_soc_acpi_mach_params { u32 acpi_ipc_irq_index; @@ -92,6 +94,8 @@ struct snd_soc_acpi_mach_params { bool subsystem_id_set; u32 bt_link_mask; u32 uaol_link_mask; + u32 uaol_playback_num; + u32 uaol_capture_num; }; /** diff --git a/sound/soc/sof/intel/Kconfig b/sound/soc/sof/intel/Kconfig index 02b41e4ccab264..5b709709a5bf83 100644 --- a/sound/soc/sof/intel/Kconfig +++ b/sound/soc/sof/intel/Kconfig @@ -406,6 +406,22 @@ config SND_SOC_SOF_INTEL_SOUNDWIRE Say Y if you want to enable SoundWire links with SOF. If unsure select "N". +config SND_SOC_SOF_INTEL_USB_OFFLOAD + bool "SOF support for USB audio offload" + depends on SND_SOC_SOF_HDA_GENERIC + depends on SND_SOC_SOF_HDA_LINK + depends on SND_USB_OFFLOAD + depends on SND_SOC_USB + depends on !(SND_USB_OFFLOAD=m && SND_SOC_SOF_HDA_COMMON=y) + depends on !(SND_SOC_USB=m && SND_SOC_SOF_HDA_COMMON=y) + help + This adds support for USB audio offload with Sound Open Firmware + for Intel(R) platforms. The DSP reaches the xHCI controller over a + USB Audio Offload Link (UAOL) and drives the endpoint itself, so + audio keeps playing while the host stays idle. + Say Y if you want to enable USB audio offload with SOF. + If unsure select "N". + endif ## SND_SOC_SOF_PCI endif ## SND_SOC_SOF_INTEL_TOPLEVEL diff --git a/sound/soc/sof/intel/hda-common-ops.c b/sound/soc/sof/intel/hda-common-ops.c index aa91f2b6fd657b..5b4f3a64be6161 100644 --- a/sound/soc/sof/intel/hda-common-ops.c +++ b/sound/soc/sof/intel/hda-common-ops.c @@ -43,6 +43,7 @@ const struct snd_sof_dsp_ops sof_hda_common_ops = { .machine_register = sof_machine_register, .machine_unregister = sof_machine_unregister, .set_mach_params = hda_set_mach_params, + .finalize_mach_params = hda_finalize_mach_params, /* debug */ .dbg_dump = hda_dsp_dump, diff --git a/sound/soc/sof/intel/hda.c b/sound/soc/sof/intel/hda.c index 22110f03210fa5..3c642ef4dc7437 100644 --- a/sound/soc/sof/intel/hda.c +++ b/sound/soc/sof/intel/hda.c @@ -19,6 +19,7 @@ #include #include +#include #include #include #include @@ -30,9 +31,11 @@ #include #include #include +#include #include "../sof-audio.h" #include "../sof-pci-dev.h" #include "../ops.h" +#include "../ipc4-priv.h" #include "../ipc4-topology.h" #include "../../intel/common/sof-function-topology-lib.h" #include "hda.h" @@ -418,6 +421,173 @@ static inline void hda_dsp_sdw_process_mic_privacy(struct snd_sof_dev *sdev) { } #endif /* IS_ENABLED(CONFIG_SND_SOC_SOF_INTEL_SOUNDWIRE) */ +#if IS_ENABLED(CONFIG_SND_SOC_SOF_INTEL_USB_OFFLOAD) + +/* Whether the UAOL links of the controller can be put to use at all */ +static bool hda_uaol_available(struct snd_sof_dev *sdev) +{ + struct sof_ipc4_fw_data *ipc4_data = sdev->private; + + if (sdev->pdata->ipc_type != SOF_IPC_TYPE_4) + return false; + + if (!ipc4_data->uaol_supported || !ipc4_data->uaol_caps) { + dev_dbg(sdev->dev, "UAOL: no firmware support\n"); + return false; + } + + return true; +} + +/* The stream capabilities the firmware reported for UAOL link @link */ +static int hda_uaol_link_caps(struct snd_sof_dev *sdev, unsigned int link, u32 *caps) +{ + struct sof_ipc4_fw_data *ipc4_data = sdev->private; + struct sof_ipc4_intel_uaol_caps *uaol_caps; + + if (!hda_uaol_available(sdev)) + return -ENODEV; + + uaol_caps = ipc4_data->uaol_caps; + if (link >= uaol_caps->link_count) + return -ENODEV; + + *caps = uaol_caps->link_caps[link].stream_caps; + + return 0; +} + +/* + * The link's streams are tied to functions by the firmware's FIFO layout, and + * the xHCI resource an endpoint gets decides the stream it runs on (stream = + * resource - 1): the output streams come first, then the input streams which + * carry audio, and the last ones, one per output stream, carry the clock + * feedback. An endpoint has to be placed within the streams of its function. + */ +static int uaol_get_resource_range(struct device *dev, int direction, bool feedback, + unsigned int *first, unsigned int *last) +{ + unsigned int tx_streams, rx_streams, audio_rx_streams, start, count; + struct snd_sof_dev *sdev = dev_get_drvdata(dev); + u32 caps; + int ret; + + ret = hda_uaol_link_caps(sdev, 0, &caps); + if (ret) + return ret; + + tx_streams = FIELD_GET(SOF_IPC4_UAOL_OUTPUT_STREAMS_MASK, caps); + rx_streams = FIELD_GET(SOF_IPC4_UAOL_INPUT_STREAMS_MASK, caps); + audio_rx_streams = rx_streams > tx_streams ? rx_streams - tx_streams : 0; + + if (feedback) { + start = tx_streams + audio_rx_streams; + count = tx_streams; + } else if (direction == SNDRV_PCM_STREAM_PLAYBACK) { + start = 0; + count = tx_streams; + } else { + start = tx_streams; + count = audio_rx_streams; + } + + if (!count) + return -ENODEV; + + *first = start + 1; + *last = start + count; + + return 0; +} + +static const struct usb_offload_ops uaol_callback = { + .get_resource_range = uaol_get_resource_range, +}; + +/* One offload context in the library is one UAOL link, over which the DSP reaches an xHCI */ +static int hda_uaol_register(struct snd_sof_dev *sdev) +{ + struct sof_intel_hda_dev *hdev = sdev->pdata->hw_pdata; + struct usb_offload_res res = { + .dev = sdev->dev, + .ops = &uaol_callback, + /* + * SOF's pipeline only has a 24-bit representation as S24_LE + * (24 valid bits in a 4-byte container); a device that only + * packs its 24-bit audio into 3 bytes (S24_3LE) still needs to + * be usable. + */ + .s24_3le_as_s24_le = true, + /* + * The firmware applies the clock feedback of an asynchronous + * playback stream by spreading the samples linearly over the + * service interval, which is audible on the gear that uses + * asynchronous playback in the first place. + */ + .async_playback_on_host = true, + }; + struct usb_offload_ctx *uaol; + + if (!hdev->uaol_link_count || !hda_uaol_available(sdev)) + return 0; + + uaol = usb_offload_register(&res); + if (IS_ERR(uaol)) { + dev_err(sdev->dev, "USB audio offload registration failed: %ld\n", + PTR_ERR(uaol)); + return PTR_ERR(uaol); + } + + hdev->uaol = uaol; + + return 0; +} + +static void hda_uaol_unregister(struct snd_sof_dev *sdev) +{ + struct sof_intel_hda_dev *hdev = sdev->pdata->hw_pdata; + + usb_offload_unregister(hdev->uaol); + hdev->uaol = NULL; +} + +#else /* IS_ENABLED(CONFIG_SND_SOC_SOF_INTEL_USB_OFFLOAD) */ + +static inline bool hda_uaol_available(struct snd_sof_dev *sdev) +{ + return false; +} + +static inline int hda_uaol_link_caps(struct snd_sof_dev *sdev, unsigned int link, u32 *caps) +{ + return -ENODEV; +} + +static inline int hda_uaol_register(struct snd_sof_dev *sdev) +{ + return 0; +} + +static inline void hda_uaol_unregister(struct snd_sof_dev *sdev) { } + +#endif /* IS_ENABLED(CONFIG_SND_SOC_SOF_INTEL_USB_OFFLOAD) */ + +/* + * The firmware configuration, and with it the offload capabilities, is only + * parsed once the DSP boot has completed, which is after the post_fw_run + * stage. This is the first hook that runs late enough to query it. + */ +int hda_machine_register(struct snd_sof_dev *sdev, void *pdata) +{ + int ret; + + ret = hda_uaol_register(sdev); + if (ret < 0) + dev_dbg(sdev->dev, "USB audio offload is not available: %d\n", ret); + + return sof_machine_register(sdev, pdata); +} + /* pre fw run operations */ int hda_dsp_pre_fw_run(struct snd_sof_dev *sdev) { @@ -986,6 +1156,8 @@ void hda_dsp_remove(struct snd_sof_dev *sdev) hda_codec_device_remove(sdev); + hda_uaol_unregister(sdev); + hda_sdw_exit(sdev); if (!IS_ERR_OR_NULL(hda->dmic_dev)) @@ -1512,6 +1684,59 @@ void hda_set_mach_params(struct snd_soc_acpi_mach *mach, mach_params->dai_drivers = desc->ops->drv; } +/* + * The link runs an audio stream per output stream and per input stream not + * taken by the clock feedback of an output stream; the DAIs put a ceiling on + * how many of them are used. 0 when the link cannot be used. + */ +static unsigned int hda_uaol_stream_num(struct snd_sof_dev *sdev, int direction) +{ + unsigned int tx_streams, rx_streams; + u32 caps; + + if (hda_uaol_link_caps(sdev, 0, &caps)) + return 0; + + tx_streams = FIELD_GET(SOF_IPC4_UAOL_OUTPUT_STREAMS_MASK, caps); + rx_streams = FIELD_GET(SOF_IPC4_UAOL_INPUT_STREAMS_MASK, caps); + + if (direction == SNDRV_PCM_STREAM_PLAYBACK) + return min(tx_streams, SOF_UAOL_MAX_PLAYBACK_STREAMS); + + return min(rx_streams > tx_streams ? rx_streams - tx_streams : 0, + SOF_UAOL_MAX_CAPTURE_STREAMS); +} + +void hda_finalize_mach_params(struct snd_sof_dev *sdev, + struct snd_soc_acpi_mach_params *params) +{ + struct sof_intel_hda_dev *hdev = sdev->pdata->hw_pdata; + int i; + + if (!params->uaol_link_mask) + return; + + /* the UAOL DAIs size their offload routes by the same counts */ + for (i = 0; i <= SNDRV_PCM_STREAM_LAST; i++) + hdev->uaol_stream_num[i] = hda_uaol_stream_num(sdev, i); + params->uaol_playback_num = hdev->uaol_stream_num[SNDRV_PCM_STREAM_PLAYBACK]; + params->uaol_capture_num = hdev->uaol_stream_num[SNDRV_PCM_STREAM_CAPTURE]; + + /* + * The controller's UAOL links are only usable if the booted firmware + * supports them and the DAIs are built. + */ + if (!params->uaol_playback_num && !params->uaol_capture_num) { + dev_dbg(sdev->dev, "UAOL: withdrawing link mask %#x\n", + params->uaol_link_mask); + params->uaol_link_mask = 0; + return; + } + + dev_dbg(sdev->dev, "UAOL: link mask %#x, %u playback and %u capture streams per link\n", + params->uaol_link_mask, params->uaol_playback_num, params->uaol_capture_num); +} + static int check_tplg_quirk_mask(struct snd_soc_acpi_mach *mach) { u32 dmic_ssp_quirk; @@ -1853,3 +2078,4 @@ MODULE_IMPORT_NS("SND_SOC_SDW_UTILS"); MODULE_IMPORT_NS("SND_SOC_SOF_HDA_MLINK"); MODULE_IMPORT_NS("SND_SOC_SOF_INTEL_HDA_COMMON"); MODULE_IMPORT_NS("SND_SOC_ACPI_INTEL_MATCH"); +MODULE_IMPORT_NS("USB_AUDIO_OFFLOAD"); diff --git a/sound/soc/sof/intel/hda.h b/sound/soc/sof/intel/hda.h index e06608022ef4dd..be2c99c72ce02b 100644 --- a/sound/soc/sof/intel/hda.h +++ b/sound/soc/sof/intel/hda.h @@ -21,6 +21,8 @@ #include "../sof-audio.h" #include "shim.h" +struct usb_offload_ctx; + /* PCI registers */ #define PCI_TCSEL 0x44 #define PCI_PGCTL PCI_TCSEL @@ -426,6 +428,14 @@ #define SOF_SKL_NUM_DAIS SOF_SKL_NUM_DAIS_NOCODEC #endif +/* + * A UAOL link has a stream per direction for each one it can run + * concurrently; how many of these the machine driver uses is reported in the + * mach_params, with these as the ceiling. + */ +#define SOF_UAOL_MAX_PLAYBACK_STREAMS 2 +#define SOF_UAOL_MAX_CAPTURE_STREAMS 2 + /* Intel HD Audio SRAM Window 0*/ #define HDA_DSP_SRAM_REG_ROM_STATUS_SKL 0x8000 #define HDA_ADSP_SRAM0_BASE_SKL 0x8000 @@ -525,6 +535,8 @@ struct sof_intel_hda_dev { /* USB Audio Offload links on the controller, 0 if UAOL is not present */ int uaol_link_count; + /* streams a link runs per direction, as reported to the machine driver */ + unsigned int uaol_stream_num[SNDRV_PCM_STREAM_LAST + 1]; /* * ACE2+ link DMA stream allocation constraints (stream index = @@ -564,6 +576,9 @@ struct sof_intel_hda_dev { /* sdw context allocated by SoundWire driver */ struct sdw_intel_ctx *sdw; + /* USB audio offload context allocated by the offload library */ + struct usb_offload_ctx *uaol; + /* FW clock config, 0:HPRO, 1:LPRO */ bool clk_config_lpro; @@ -934,6 +949,8 @@ static inline bool hda_common_check_sdw_irq(struct snd_sof_dev *sdev) #endif +int hda_machine_register(struct snd_sof_dev *sdev, void *pdata); + int sdw_hda_dai_hw_params(struct snd_pcm_substream *substream, struct snd_pcm_hw_params *params, struct snd_soc_dai *cpu_dai, @@ -1016,6 +1033,8 @@ void hda_unregister_clients(struct snd_sof_dev *sdev); struct snd_soc_acpi_mach *hda_machine_select(struct snd_sof_dev *sdev); void hda_set_mach_params(struct snd_soc_acpi_mach *mach, struct snd_sof_dev *sdev); +void hda_finalize_mach_params(struct snd_sof_dev *sdev, + struct snd_soc_acpi_mach_params *params); /* PCI driver selection and probe */ int hda_pci_intel_probe(struct pci_dev *pci, const struct pci_device_id *pci_id); From 830b37823878d2e070f6d7d0de2e66e332c8d3e2 Mon Sep 17 00:00:00 2001 From: Peter Ujfalusi Date: Tue, 11 Aug 2026 13:29:02 +0300 Subject: [PATCH 33/53] ASoC: SOF: Intel: hda-mlink: Add helper to get the UAOL hlink The UAOL alternate link is already enumerated and its link type is reported as HDA_BUS_ML_LINK_UAOL, but there is no way for a client to retrieve the hdac_ext_link for it. Add hdac_bus_eml_uaol_get_hlink() following the existing SSP, DMIC and SoundWire helpers. It is needed by the UAOL DAI to assign a link DMA channel. Signed-off-by: Peter Ujfalusi --- include/sound/hda-mlink.h | 4 ++++ sound/soc/sof/intel/hda-mlink.c | 12 ++++++++++++ 2 files changed, 16 insertions(+) diff --git a/include/sound/hda-mlink.h b/include/sound/hda-mlink.h index ba35f03576b9da..9cd4889791bb9e 100644 --- a/include/sound/hda-mlink.h +++ b/include/sound/hda-mlink.h @@ -74,6 +74,7 @@ enum hda_bus_ml_link_type hda_bus_ml_link_get_type(struct hdac_ext_link *hlink); struct hdac_ext_link *hdac_bus_eml_ssp_get_hlink(struct hdac_bus *bus); struct hdac_ext_link *hdac_bus_eml_dmic_get_hlink(struct hdac_bus *bus); struct hdac_ext_link *hdac_bus_eml_sdw_get_hlink(struct hdac_bus *bus); +struct hdac_ext_link *hdac_bus_eml_uaol_get_hlink(struct hdac_bus *bus); struct mutex *hdac_bus_eml_get_mutex(struct hdac_bus *bus, bool alt, int elid); @@ -202,6 +203,9 @@ hdac_bus_eml_dmic_get_hlink(struct hdac_bus *bus) { return NULL; } static inline struct hdac_ext_link * hdac_bus_eml_sdw_get_hlink(struct hdac_bus *bus) { return NULL; } +static inline struct hdac_ext_link * +hdac_bus_eml_uaol_get_hlink(struct hdac_bus *bus) { return NULL; } + static inline struct mutex * hdac_bus_eml_get_mutex(struct hdac_bus *bus, bool alt, int elid) { return NULL; } diff --git a/sound/soc/sof/intel/hda-mlink.c b/sound/soc/sof/intel/hda-mlink.c index 6f02fb5b70cedc..1ddd214f56852e 100644 --- a/sound/soc/sof/intel/hda-mlink.c +++ b/sound/soc/sof/intel/hda-mlink.c @@ -997,6 +997,18 @@ struct hdac_ext_link *hdac_bus_eml_sdw_get_hlink(struct hdac_bus *bus) } EXPORT_SYMBOL_NS(hdac_bus_eml_sdw_get_hlink, "SND_SOC_SOF_HDA_MLINK"); +struct hdac_ext_link *hdac_bus_eml_uaol_get_hlink(struct hdac_bus *bus) +{ + struct hdac_ext2_link *h2link; + + h2link = find_ext2_link(bus, true, AZX_REG_ML_LEPTR_ID_INTEL_UAOL); + if (!h2link) + return NULL; + + return &h2link->hext_link; +} +EXPORT_SYMBOL_NS(hdac_bus_eml_uaol_get_hlink, "SND_SOC_SOF_HDA_MLINK"); + void hdac_bus_eml_enable_offload(struct hdac_bus *bus, bool alt, int elid, bool enable) { struct hdac_ext2_link *h2link; From 2f545194c185b1fb4ab220ba386047258f42ad9a Mon Sep 17 00:00:00 2001 From: Peter Ujfalusi Date: Tue, 11 Aug 2026 13:29:13 +0300 Subject: [PATCH 34/53] ASoC: SOF: Add support for the UAOL DAI type SOF_DAI_INTEL_UAOL is defined in the IPC ABI but it is not reachable from topology: find_dai() has no matching name, so a topology using it resolves to SOF_DAI_INTEL_NONE, and the IPC4 dai_config op rejects the type as unsupported. Add the "UAOL" name to the DAI type table and accept the type in sof_ipc4_dai_config(). Unlike HDA, the node index of the copier gateway is not the link DMA stream tag but the UAOL link stream the endpoint is routed to, which is only known once the USB endpoint has been claimed; the DAI sets it from its own hw_params path. Signed-off-by: Peter Ujfalusi --- sound/soc/sof/ipc4-topology.c | 3 +++ sound/soc/sof/topology.c | 1 + 2 files changed, 4 insertions(+) diff --git a/sound/soc/sof/ipc4-topology.c b/sound/soc/sof/ipc4-topology.c index 4e72a1a93d34aa..b75cb749fc0cb7 100644 --- a/sound/soc/sof/ipc4-topology.c +++ b/sound/soc/sof/ipc4-topology.c @@ -4134,6 +4134,9 @@ static int sof_ipc4_dai_config(struct snd_sof_dev *sdev, struct snd_sof_widget * case SOF_DAI_INTEL_SSP: /* nothing to do for SSP/DMIC */ break; + case SOF_DAI_INTEL_UAOL: + /* the node index is the link stream, set when the endpoint is known */ + break; default: dev_err(sdev->dev, "%s: unsupported dai type %d\n", __func__, ipc4_copier->dai_type); diff --git a/sound/soc/sof/topology.c b/sound/soc/sof/topology.c index 750916b57def74..1aae49687bfde9 100644 --- a/sound/soc/sof/topology.c +++ b/sound/soc/sof/topology.c @@ -298,6 +298,7 @@ static const struct sof_dai_types sof_dais[] = { {"HDA", SOF_DAI_INTEL_HDA}, {"DMIC", SOF_DAI_INTEL_DMIC}, {"ALH", SOF_DAI_INTEL_ALH}, + {"UAOL", SOF_DAI_INTEL_UAOL}, {"SAI", SOF_DAI_IMX_SAI}, {"ESAI", SOF_DAI_IMX_ESAI}, {"ACPBT", SOF_DAI_AMD_BT}, From 67ecc90282cb2078f164fbadd4bc274bd880107e Mon Sep 17 00:00:00 2001 From: Peter Ujfalusi Date: Wed, 4 Nov 2026 14:49:07 +0200 Subject: [PATCH 35/53] ASoC: SOF: Intel: hda-dai: Add UAOL link DMA support Provide the CPU DAIs and the widget DMA ops needed to run a stream over a UAOL link. The UAOL link is an ACE2+ alternate link using a link DMA, so the generic HDA link DMA handling applies unchanged and the ops are identical to the SSP and DMIC ones, only the hlink lookup and the stream format calculation differ. The UAOL DAI code gets a file of its own, hda-uaol.c, built into the HDA common module when SND_SOC_SOF_INTEL_USB_OFFLOAD is enabled; the DAI callbacks and helpers of hda-dai.c it builds on are made visible within the module for that. A UAOL link runs several streams at a time, two in each direction on the current hardware, and each stream the link can carry concurrently needs a backend of its own, in the way a SoundWire link gets a backend per PDI. Give the component a playback and a capture DAI for each stream, "UAOL0 Playback Pin0", "UAOL0 Playback Pin1", "UAOL0 Capture Pin0" and "UAOL0 Capture Pin1". The DAIs are unidirectional: the link's streams are, and the backend hw_params fixup is not told the direction, so a duplex DAI would gain nothing. The pin number only tells the DAIs of a direction apart, it is not the UAOL stream the DAI runs on: that is decided by the xHCI sideband resource the endpoint gets when the USB device is acquired. The DAIs are kept last in skl_dai[], as the number of DAIs the component registers is a prefix of the array. The link DMA stream allocator already knows about HDA_BUS_ML_LINK_UAOL and applies the ACE2+ allocation constraints to it. Signed-off-by: Peter Ujfalusi --- sound/soc/sof/intel/Makefile | 1 + sound/soc/sof/intel/hda-dai-ops.c | 25 ++++++++++ sound/soc/sof/intel/hda-dai.c | 76 ++++++++++++++++++++----------- sound/soc/sof/intel/hda-uaol.c | 44 ++++++++++++++++++ sound/soc/sof/intel/hda.h | 53 +++++++++++++++++++-- 5 files changed, 168 insertions(+), 31 deletions(-) create mode 100644 sound/soc/sof/intel/hda-uaol.c diff --git a/sound/soc/sof/intel/Makefile b/sound/soc/sof/intel/Makefile index cc9783e933f8e5..5cb8326eecb686 100644 --- a/sound/soc/sof/intel/Makefile +++ b/sound/soc/sof/intel/Makefile @@ -15,6 +15,7 @@ snd-sof-intel-hda-mlink-y := hda-mlink.o snd-sof-intel-hda-sdw-bpt-objs := hda-sdw-bpt.o snd-sof-intel-hda-common-$(CONFIG_SND_SOC_SOF_HDA_PROBES) += hda-probes.o +snd-sof-intel-hda-common-$(CONFIG_SND_SOC_SOF_INTEL_USB_OFFLOAD) += hda-uaol.o snd-sof-intel-hda-y := hda-codec.o diff --git a/sound/soc/sof/intel/hda-dai-ops.c b/sound/soc/sof/intel/hda-dai-ops.c index c0f843d0062b67..0205fb40db6324 100644 --- a/sound/soc/sof/intel/hda-dai-ops.c +++ b/sound/soc/sof/intel/hda-dai-ops.c @@ -414,6 +414,14 @@ hda_ipc4_find_owning_pipe_widget(struct snd_pcm_substream *substream) return NULL; } +static struct hdac_ext_link *uaol_get_hlink(struct snd_sof_dev *sdev, + struct snd_pcm_substream *substream) +{ + struct hdac_bus *bus = sof_to_bus(sdev); + + return hdac_bus_eml_uaol_get_hlink(bus); +} + static int hda_ipc4_pre_trigger(struct snd_sof_dev *sdev, struct snd_soc_dai *cpu_dai, struct snd_pcm_substream *substream, int cmd) { @@ -632,6 +640,19 @@ static const struct hda_dai_widget_dma_ops dmic_ipc4_dma_ops = { .get_hlink = dmic_get_hlink, }; +static const struct hda_dai_widget_dma_ops uaol_ipc4_dma_ops = { + .get_hext_stream = hda_ipc4_get_hext_stream, + .assign_hext_stream = hda_assign_hext_stream, + .release_hext_stream = hda_release_hext_stream, + .setup_hext_stream = hda_setup_hext_stream, + .reset_hext_stream = hda_reset_hext_stream, + .pre_trigger = hda_ipc4_pre_trigger, + .trigger = hda_trigger, + .post_trigger = hda_ipc4_post_trigger, + .calc_stream_format = generic_calc_stream_format, + .get_hlink = uaol_get_hlink, +}; + static const struct hda_dai_widget_dma_ops sdw_ipc4_dma_ops = { .get_hext_stream = hda_ipc4_get_hext_stream, .assign_hext_stream = hda_assign_hext_stream, @@ -798,6 +819,10 @@ hda_select_dai_widget_ops(struct snd_sof_dev *sdev, struct snd_sof_widget *swidg if (chip->hw_ip_version < SOF_INTEL_ACE_2_0) return NULL; return &dmic_ipc4_dma_ops; + case SOF_DAI_INTEL_UAOL: + if (chip->hw_ip_version < SOF_INTEL_ACE_2_0) + return NULL; + return &uaol_ipc4_dma_ops; case SOF_DAI_INTEL_ALH: if (chip->hw_ip_version < SOF_INTEL_ACE_2_0) return NULL; diff --git a/sound/soc/sof/intel/hda-dai.c b/sound/soc/sof/intel/hda-dai.c index 7a8875a885ab34..86048893f62807 100644 --- a/sound/soc/sof/intel/hda-dai.c +++ b/sound/soc/sof/intel/hda-dai.c @@ -58,14 +58,6 @@ EXPORT_SYMBOL_NS(hda_dai_config, "SND_SOC_SOF_INTEL_HDA_COMMON"); #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA_LINK) -static struct snd_sof_dev *dai_to_sdev(struct snd_pcm_substream *substream, - struct snd_soc_dai *cpu_dai) -{ - struct snd_soc_dapm_widget *w = snd_soc_dai_get_widget(cpu_dai, substream->stream); - - return widget_to_sdev(w); -} - static const struct hda_dai_widget_dma_ops * hda_dai_get_ops(struct snd_pcm_substream *substream, struct snd_soc_dai *cpu_dai) { @@ -220,8 +212,8 @@ static int hda_link_dma_hw_params(struct snd_pcm_substream *substream, return 0; } -static int __maybe_unused hda_dai_hw_free(struct snd_pcm_substream *substream, - struct snd_soc_dai *cpu_dai) +int hda_dai_hw_free(struct snd_pcm_substream *substream, + struct snd_soc_dai *cpu_dai) { const struct hda_dai_widget_dma_ops *ops = hda_dai_get_ops(substream, cpu_dai); struct hdac_ext_stream *hext_stream; @@ -312,8 +304,8 @@ static bool hda_dai_has_wov_fe(struct snd_sof_dev *sdev, struct snd_pcm_substrea * In contrast to IPC3, the dai trigger in IPC4 mixes pipeline state changes * (over IPC channel) and DMA state change (direct host register changes). */ -static int __maybe_unused hda_dai_trigger(struct snd_pcm_substream *substream, int cmd, - struct snd_soc_dai *dai) +int hda_dai_trigger(struct snd_pcm_substream *substream, int cmd, + struct snd_soc_dai *dai) { const struct hda_dai_widget_dma_ops *ops = hda_dai_get_ops(substream, dai); struct hdac_ext_stream *hext_stream; @@ -407,15 +399,6 @@ static const struct snd_soc_dai_ops hda_dai_ops = { #endif -static struct sof_ipc4_copier *widget_to_copier(struct snd_soc_dapm_widget *w) -{ - struct snd_sof_widget *swidget = w->dobj.private; - struct snd_sof_dai *sdai = swidget->private; - struct sof_ipc4_copier *ipc4_copier = (struct sof_ipc4_copier *)sdai->private; - - return ipc4_copier; -} - static int non_hda_dai_hw_params_data(struct snd_pcm_substream *substream, struct snd_pcm_hw_params *params, struct snd_soc_dai *cpu_dai, @@ -504,9 +487,9 @@ static int non_hda_dai_hw_params_data(struct snd_pcm_substream *substream, return 0; } -static int non_hda_dai_hw_params(struct snd_pcm_substream *substream, - struct snd_pcm_hw_params *params, - struct snd_soc_dai *cpu_dai) +int non_hda_dai_hw_params(struct snd_pcm_substream *substream, + struct snd_pcm_hw_params *params, + struct snd_soc_dai *cpu_dai) { struct snd_sof_dai_config_data data = { 0 }; unsigned int flags = SOF_DAI_CONFIG_FLAGS_HW_PARAMS; @@ -514,8 +497,8 @@ static int non_hda_dai_hw_params(struct snd_pcm_substream *substream, return non_hda_dai_hw_params_data(substream, params, cpu_dai, &data, flags); } -static int non_hda_dai_prepare(struct snd_pcm_substream *substream, - struct snd_soc_dai *cpu_dai) +int non_hda_dai_prepare(struct snd_pcm_substream *substream, + struct snd_soc_dai *cpu_dai) { struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream); int stream = substream->stream; @@ -804,6 +787,7 @@ void hda_set_dai_drv_ops(struct snd_sof_dev *sdev, struct snd_sof_dsp_ops *ops) ssp_set_dai_drv_ops(sdev, ops); dmic_set_dai_drv_ops(sdev, ops); + hda_uaol_set_dai_drv_ops(sdev, ops); if (sdev->pdata->ipc_type == SOF_IPC_TYPE_4 && !hda_use_tplg_nhlt) { struct sof_ipc4_fw_data *ipc4_data = sdev->private; @@ -1004,7 +988,47 @@ struct snd_soc_dai_driver skl_dai[] = { }, }, #endif +/* + * The number of DAIs the component registers is a prefix of this array, so the + * UAOL DAIs are kept last: an unconditional entry in the middle would push the + * DAIs after it out of the range a configuration not counting the UAOL ones + * registers. The pin number tells the DAIs of a direction apart, it is not the + * UAOL stream the DAI runs on: that is picked when a USB device is acquired. + */ +#if IS_ENABLED(CONFIG_SND_SOC_SOF_INTEL_USB_OFFLOAD) +{ + .name = "UAOL0 Playback Pin0", + .playback = { + .channels_min = 1, + .channels_max = 8, + }, +}, +{ + .name = "UAOL0 Playback Pin1", + .playback = { + .channels_min = 1, + .channels_max = 8, + }, +}, +{ + .name = "UAOL0 Capture Pin0", + .capture = { + .channels_min = 1, + .channels_max = 8, + }, +}, +{ + .name = "UAOL0 Capture Pin1", + .capture = { + .channels_min = 1, + .channels_max = 8, + }, +}, +#endif }; + +static_assert(ARRAY_SIZE(skl_dai) == SOF_SKL_NUM_DAIS, + "SOF_SKL_NUM_DAIS does not cover skl_dai[]"); EXPORT_SYMBOL_NS(skl_dai, "SND_SOC_SOF_INTEL_HDA_COMMON"); int hda_dsp_dais_suspend(struct snd_sof_dev *sdev) diff --git a/sound/soc/sof/intel/hda-uaol.c b/sound/soc/sof/intel/hda-uaol.c new file mode 100644 index 00000000000000..f9b77043cf5119 --- /dev/null +++ b/sound/soc/sof/intel/hda-uaol.c @@ -0,0 +1,44 @@ +// SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause) +// +// This file is provided under a dual BSD/GPLv2 license. When using or +// redistributing this file, you may do so under either license. +// +// Copyright(c) 2026 Intel Corporation +// +// USB Audio Offload Link (UAOL) DAIs: the DSP drives a USB audio endpoint +// over the UAOL instead of the host. +// + +#include +#include +#include +#include +#include +#include "../ipc4-priv.h" +#include "../ipc4-topology.h" +#include "../sof-priv.h" +#include "../sof-audio.h" +#include "hda.h" + +/* The link DMA is run like the one of the SSP and DMIC DAIs */ +static const struct snd_soc_dai_ops uaol_dai_ops = { + .hw_params = non_hda_dai_hw_params, + .hw_free = hda_dai_hw_free, + .trigger = hda_dai_trigger, + .prepare = non_hda_dai_prepare, +}; + +void hda_uaol_set_dai_drv_ops(struct snd_sof_dev *sdev, struct snd_sof_dsp_ops *ops) +{ + const struct sof_intel_dsp_desc *chip; + int i; + + chip = get_chip_info(sdev->pdata); + + if (chip->hw_ip_version >= SOF_INTEL_ACE_2_0) { + for (i = 0; i < ops->num_drv; i++) { + if (strstr(ops->drv[i].name, "UAOL")) + ops->drv[i].ops = &uaol_dai_ops; + } + } +} diff --git a/sound/soc/sof/intel/hda.h b/sound/soc/sof/intel/hda.h index be2c99c72ce02b..3e148d65f6eb4d 100644 --- a/sound/soc/sof/intel/hda.h +++ b/sound/soc/sof/intel/hda.h @@ -423,18 +423,25 @@ struct usb_offload_ctx; #define SOF_SKL_NUM_DAIS_NOCODEC 9 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA_AUDIO_CODEC) -#define SOF_SKL_NUM_DAIS 16 +#define SOF_SKL_NUM_DAIS_BASE 16 #else -#define SOF_SKL_NUM_DAIS SOF_SKL_NUM_DAIS_NOCODEC +#define SOF_SKL_NUM_DAIS_BASE SOF_SKL_NUM_DAIS_NOCODEC #endif /* - * A UAOL link has a stream per direction for each one it can run - * concurrently; how many of these the machine driver uses is reported in the - * mach_params, with these as the ceiling. + * The UAOL DAIs are the last entries of skl_dai[]. A link has a stream per + * direction for each one it can run concurrently; how many of these the + * machine driver uses is reported in the mach_params. */ #define SOF_UAOL_MAX_PLAYBACK_STREAMS 2 #define SOF_UAOL_MAX_CAPTURE_STREAMS 2 +#define SOF_UAOL_NUM_DAIS (SOF_UAOL_MAX_PLAYBACK_STREAMS + \ + SOF_UAOL_MAX_CAPTURE_STREAMS) +#if IS_ENABLED(CONFIG_SND_SOC_SOF_INTEL_USB_OFFLOAD) +#define SOF_SKL_NUM_DAIS (SOF_SKL_NUM_DAIS_BASE + SOF_UAOL_NUM_DAIS) +#else +#define SOF_SKL_NUM_DAIS SOF_SKL_NUM_DAIS_BASE +#endif /* Intel HD Audio SRAM Window 0*/ #define HDA_DSP_SRAM_REG_ROM_STATUS_SKL 0x8000 @@ -1128,4 +1135,40 @@ static inline struct snd_sof_dev *widget_to_sdev(struct snd_soc_dapm_widget *w) return snd_soc_component_get_drvdata(component); } +static inline struct snd_sof_dev *dai_to_sdev(struct snd_pcm_substream *substream, + struct snd_soc_dai *cpu_dai) +{ + struct snd_soc_dapm_widget *w = snd_soc_dai_get_widget(cpu_dai, substream->stream); + + return widget_to_sdev(w); +} + +static inline struct sof_ipc4_copier *widget_to_copier(struct snd_soc_dapm_widget *w) +{ + struct snd_sof_widget *swidget = w->dobj.private; + struct snd_sof_dai *sdai = swidget->private; + + return sdai->private; +} + +#if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA_LINK) +/* the DAI callbacks the UAOL DAI builds on, shared with the SSP and DMIC DAIs */ +int non_hda_dai_hw_params(struct snd_pcm_substream *substream, + struct snd_pcm_hw_params *params, + struct snd_soc_dai *cpu_dai); +int non_hda_dai_prepare(struct snd_pcm_substream *substream, + struct snd_soc_dai *cpu_dai); +int hda_dai_hw_free(struct snd_pcm_substream *substream, + struct snd_soc_dai *cpu_dai); +int hda_dai_trigger(struct snd_pcm_substream *substream, int cmd, + struct snd_soc_dai *dai); +#endif + +#if IS_ENABLED(CONFIG_SND_SOC_SOF_INTEL_USB_OFFLOAD) +void hda_uaol_set_dai_drv_ops(struct snd_sof_dev *sdev, struct snd_sof_dsp_ops *ops); +#else +static inline void hda_uaol_set_dai_drv_ops(struct snd_sof_dev *sdev, + struct snd_sof_dsp_ops *ops) { } +#endif + #endif From 0d53b15793932aae179ba0ec39ca215badad900b Mon Sep 17 00:00:00 2001 From: Peter Ujfalusi Date: Wed, 4 Nov 2026 14:57:15 +0200 Subject: [PATCH 36/53] ASoC: SOF: Intel: Add the ASoC USB offload port The offload library reports offload capable USB audio devices to the driver, but they still have to be presented to the ASoC USB framework so that a card can route to them. Allocate the ASoC USB port when the first UAOL DAI is bound into a card and release it when the DAI goes away. It cannot be done when the offload library is started: the firmware boots before the audio component is registered, so there is no component to hand over at that point. Devices which enumerated before the card existed are rediscovered by the ASoC USB core when the port is added. The connected devices are kept on a list owned by the controller, which is where the DAI looks up the device to offload. The record has its own list node, struct snd_soc_usb_device's list_head belongs to the ASoC USB core. A device can expose several PCMs (e.g. separate speaker and line-out streams) and the link runs several streams per direction, each on a backend of its own. Which device and PCM a backend plays to is decided by the ASoC USB route controls: give the port as many routes per direction as the link has streams, create the controls with the port and report each PCM's name for the controls to show. The selected PCM's sideband resources are acquired only once a stream on it is actually started. The UAOL PCMs exist whether or not a device is connected, so report the availability on a SND_JACK_USB jack named "USB Offload Jack", the name the Qualcomm offload path uses. The jack follows the device list, so removing one of several connected devices leaves it reported. It is created before the port is added, as that is what reports the already connected devices. The UAOL PCMs also exist if the driver could not register with the offload library, so the jack is created in that case too and reported off. The port can go away with the card while a device is being reported, so the port check and the record are handled under the device list lock and a device the port refused is dropped again rather than kept as known. Signed-off-by: Peter Ujfalusi --- sound/soc/sof/intel/hda-uaol.c | 293 +++++++++++++++++++++++++++++++++ sound/soc/sof/intel/hda.c | 5 + sound/soc/sof/intel/hda.h | 17 ++ 3 files changed, 315 insertions(+) diff --git a/sound/soc/sof/intel/hda-uaol.c b/sound/soc/sof/intel/hda-uaol.c index f9b77043cf5119..8099d2127ca22d 100644 --- a/sound/soc/sof/intel/hda-uaol.c +++ b/sound/soc/sof/intel/hda-uaol.c @@ -12,7 +12,10 @@ #include #include #include +#include +#include #include +#include #include #include "../ipc4-priv.h" #include "../ipc4-topology.h" @@ -20,8 +23,298 @@ #include "../sof-audio.h" #include "hda.h" +/** + * struct hda_uaol_dev - an offload capable USB audio device + * @sdev: the device identity handed to the ASoC USB framework + * @udev: USB device this record stands for, the key used on disconnect + * @node: link on hdev->uaol_devices, this backend's own record of what it + * has allocated - not the routing list, see hdev->uaol_usb's + * available_devices for that + * @ppcm: backing store for @sdev.ppcm_idx + * @cpcm: backing store for @sdev.cpcm_idx + * @ppcm_names: backing store for @sdev.ppcm_names + * @cpcm_names: backing store for @sdev.cpcm_names + * @ppcm_name_storage: string storage @ppcm_names points into + * @cpcm_name_storage: string storage @cpcm_names points into + * + * The offload library reports the identity as a value, while the ASoC USB + * framework wants a list node it can keep, so the record is owned here. + */ +struct hda_uaol_dev { + struct snd_soc_usb_device sdev; + struct usb_device *udev; + struct list_head node; + unsigned int ppcm[USB_OFFLOAD_MAX_PCM]; + unsigned int cpcm[USB_OFFLOAD_MAX_PCM]; + const char *ppcm_names[USB_OFFLOAD_MAX_PCM]; + const char *cpcm_names[USB_OFFLOAD_MAX_PCM]; + char ppcm_name_storage[USB_OFFLOAD_MAX_PCM][USB_OFFLOAD_PCM_NAME_LEN]; + char cpcm_name_storage[USB_OFFLOAD_MAX_PCM][USB_OFFLOAD_PCM_NAME_LEN]; +}; + +/* Caller holds hdev->uaol_mutex. */ +static struct hda_uaol_dev *hda_uaol_find(struct sof_intel_hda_dev *hdev, + struct usb_device *udev) +{ + struct hda_uaol_dev *dev; + + list_for_each_entry(dev, &hdev->uaol_devices, node) { + if (dev->udev == udev) + return dev; + } + + return NULL; +} + +/* + * The identity is everything the port is told about the device; a re-notify + * with all of it unchanged carries nothing new. + */ +static bool hda_uaol_dev_identity_equal(const struct hda_uaol_dev *a, + const struct hda_uaol_dev *b) +{ + return a->sdev.card_idx == b->sdev.card_idx && + a->sdev.chip_idx == b->sdev.chip_idx && + a->sdev.num_playback == b->sdev.num_playback && + a->sdev.num_capture == b->sdev.num_capture && + !memcmp(a->ppcm, b->ppcm, sizeof(a->ppcm)) && + !memcmp(a->cpcm, b->cpcm, sizeof(a->cpcm)) && + !memcmp(a->ppcm_name_storage, b->ppcm_name_storage, + sizeof(a->ppcm_name_storage)) && + !memcmp(a->cpcm_name_storage, b->cpcm_name_storage, + sizeof(a->cpcm_name_storage)); +} + +int hda_uaol_notify_connect(struct device *dev, struct usb_device *udev, + const struct usb_offload_usb_dev_info *id) +{ + struct snd_sof_dev *sdev = dev_get_drvdata(dev); + struct sof_intel_hda_dev *hdev = sdev->pdata->hw_pdata; + struct hda_uaol_dev *offload_dev, *old; + int ret; + + offload_dev = kzalloc_obj(*offload_dev, GFP_KERNEL); + if (!offload_dev) + return -ENOMEM; + + offload_dev->udev = udev; + INIT_LIST_HEAD(&offload_dev->sdev.available_node); + offload_dev->sdev.card_idx = id->card_idx; + offload_dev->sdev.chip_idx = id->chip_idx; + offload_dev->sdev.num_playback = id->num_playback; + offload_dev->sdev.num_capture = id->num_capture; + memcpy(offload_dev->ppcm, id->ppcm_idx, sizeof(offload_dev->ppcm)); + memcpy(offload_dev->cpcm, id->cpcm_idx, sizeof(offload_dev->cpcm)); + memcpy(offload_dev->ppcm_name_storage, id->ppcm_names, + sizeof(offload_dev->ppcm_name_storage)); + memcpy(offload_dev->cpcm_name_storage, id->cpcm_names, + sizeof(offload_dev->cpcm_name_storage)); + for (int i = 0; i < USB_OFFLOAD_MAX_PCM; i++) { + offload_dev->ppcm_names[i] = offload_dev->ppcm_name_storage[i]; + offload_dev->cpcm_names[i] = offload_dev->cpcm_name_storage[i]; + } + offload_dev->sdev.ppcm_idx = offload_dev->ppcm; + offload_dev->sdev.cpcm_idx = offload_dev->cpcm; + offload_dev->sdev.ppcm_names = offload_dev->ppcm_names; + offload_dev->sdev.cpcm_names = offload_dev->cpcm_names; + + /* + * Held across the connect, so that the card removal, which takes the + * port away under the same lock, cannot slip in between the check for + * the port and the device being listed. + */ + guard(mutex)(&hdev->uaol_mutex); + + /* + * Without a card there is nothing to attach the device to. The port + * rediscovers the already enumerated devices when it comes up, so the + * event is not lost. + */ + if (!hdev->uaol_usb) { + kfree(offload_dev); + return 0; + } + + /* + * A notification for a listed device carries its updated identity: the + * PCM list grows as further interfaces of a multi-function device are + * probed. Replace the stale entry; an unchanged identity is left alone, + * a disconnect would drop the routes picked for the device. + */ + old = hda_uaol_find(hdev, udev); + if (old) { + if (hda_uaol_dev_identity_equal(old, offload_dev)) { + kfree(offload_dev); + return 0; + } + + list_del(&old->node); + snd_soc_usb_disconnect(sdev->dev, &old->sdev); + kfree(old); + } + + ret = snd_soc_usb_connect(sdev->dev, &offload_dev->sdev); + if (ret) { + kfree(offload_dev); + return ret; + } + + list_add_tail(&offload_dev->node, &hdev->uaol_devices); + if (hdev->uaol_jack.jack) + snd_jack_report(hdev->uaol_jack.jack, SND_JACK_USB); + + return 0; +} +EXPORT_SYMBOL_NS(hda_uaol_notify_connect, "SND_SOC_SOF_INTEL_HDA_COMMON"); + +int hda_uaol_notify_disconnect(struct device *dev, struct usb_device *udev) +{ + struct snd_sof_dev *sdev = dev_get_drvdata(dev); + struct sof_intel_hda_dev *hdev = sdev->pdata->hw_pdata; + struct hda_uaol_dev *offload_dev; + + scoped_guard(mutex, &hdev->uaol_mutex) { + offload_dev = hda_uaol_find(hdev, udev); + if (offload_dev) + list_del(&offload_dev->node); + } + if (!offload_dev) + return 0; + + snd_soc_usb_disconnect(sdev->dev, &offload_dev->sdev); + kfree(offload_dev); + + /* + * The jack follows the list and not this one event, so that removing + * one of several connected devices still leaves it reported. The list + * is read where the jack is reported, a device connecting in between + * would otherwise be reported as gone. + */ + scoped_guard(mutex, &hdev->uaol_mutex) { + if (hdev->uaol_jack.jack) + snd_jack_report(hdev->uaol_jack.jack, + list_empty(&hdev->uaol_devices) ? 0 : SND_JACK_USB); + } + + return 0; +} +EXPORT_SYMBOL_NS(hda_uaol_notify_disconnect, "SND_SOC_SOF_INTEL_HDA_COMMON"); + +/* + * The ASoC USB port is published once the UAOL DAI is bound into a card, + * which is the point where an offloaded device has somewhere to be routed. + * The firmware has booted long before that, so the offload library cannot + * do this itself. + */ +static int uaol_dai_probe(struct snd_soc_dai *dai) +{ + struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(dai->component); + struct sof_intel_hda_dev *hdev = sdev->pdata->hw_pdata; + struct snd_soc_usb *usb; + int ret, i; + + if (!hdev->uaol) { + /* + * The UAOL PCMs exist when the controller and firmware + * support offload, even if the offload library itself failed to + * probe. Give userspace a jack it can read instead of a control + * that silently doesn't exist. + */ + if (hdev->uaol_jack.jack) + return 0; + + ret = snd_soc_card_jack_new(dai->component->card, "USB Offload Jack", + SND_JACK_USB, &hdev->uaol_jack); + if (ret) + return ret; + + snd_jack_report(hdev->uaol_jack.jack, 0); + return 0; + } + + /* the port belongs to the controller, every UAOL CPU DAI probes here */ + if (hdev->uaol_usb) + return 0; + + usb = snd_soc_usb_allocate_port(dai->component, hdev); + if (IS_ERR(usb)) + return PTR_ERR(usb); + + /* a route per backend of a direction, each with a device of its own */ + for (i = 0; i <= SNDRV_PCM_STREAM_LAST; i++) { + ret = snd_soc_usb_set_num_routes(usb, i, max(hdev->uaol_stream_num[i], 1U)); + if (ret) { + snd_soc_usb_free_port(usb); + return ret; + } + } + + /* + * The UAOL PCMs exist whether or not a device is there to play to, and + * nothing else tells userspace which of the two it is. The jack has to + * be in place before the port goes up, as that is what reports the + * devices which are already connected. + */ + ret = snd_soc_card_jack_new(dai->component->card, "USB Offload Jack", + SND_JACK_USB, &hdev->uaol_jack); + if (ret) { + snd_soc_usb_free_port(usb); + return ret; + } + + ret = snd_soc_usb_setup_offload_route_ctl(dai->component, usb); + if (ret) { + snd_soc_usb_free_port(usb); + return ret; + } + + scoped_guard(mutex, &hdev->uaol_mutex) + hdev->uaol_usb = usb; + + /* rediscovers the devices which enumerated before the card existed */ + snd_soc_usb_add_port(usb); + + return 0; +} + +static int uaol_dai_remove(struct snd_soc_dai *dai) +{ + struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(dai->component); + struct sof_intel_hda_dev *hdev = sdev->pdata->hw_pdata; + struct hda_uaol_dev *dev, *tmp; + struct snd_soc_usb *usb; + + /* + * Taken away under the lock so that a connect racing with the card + * going away sees no port to attach to. The jack itself belongs to the + * card and goes away with it. + */ + scoped_guard(mutex, &hdev->uaol_mutex) { + usb = hdev->uaol_usb; + hdev->uaol_usb = NULL; + hdev->uaol_jack.jack = NULL; + } + if (!usb) + return 0; + + snd_soc_usb_remove_port(usb); + snd_soc_usb_free_port(usb); + + /* the port is gone, no disconnect event will report these anymore */ + scoped_guard(mutex, &hdev->uaol_mutex) { + list_for_each_entry_safe(dev, tmp, &hdev->uaol_devices, node) { + list_del(&dev->node); + kfree(dev); + } + } + + return 0; +} + /* The link DMA is run like the one of the SSP and DMIC DAIs */ static const struct snd_soc_dai_ops uaol_dai_ops = { + .probe = uaol_dai_probe, + .remove = uaol_dai_remove, .hw_params = non_hda_dai_hw_params, .hw_free = hda_dai_hw_free, .trigger = hda_dai_trigger, diff --git a/sound/soc/sof/intel/hda.c b/sound/soc/sof/intel/hda.c index 3c642ef4dc7437..1a14e535eb19bd 100644 --- a/sound/soc/sof/intel/hda.c +++ b/sound/soc/sof/intel/hda.c @@ -501,6 +501,8 @@ static int uaol_get_resource_range(struct device *dev, int direction, bool feedb } static const struct usb_offload_ops uaol_callback = { + .notify_connect = hda_uaol_notify_connect, + .notify_disconnect = hda_uaol_notify_disconnect, .get_resource_range = uaol_get_resource_range, }; @@ -528,6 +530,9 @@ static int hda_uaol_register(struct snd_sof_dev *sdev) }; struct usb_offload_ctx *uaol; + INIT_LIST_HEAD(&hdev->uaol_devices); + mutex_init(&hdev->uaol_mutex); + if (!hdev->uaol_link_count || !hda_uaol_available(sdev)) return 0; diff --git a/sound/soc/sof/intel/hda.h b/sound/soc/sof/intel/hda.h index 3e148d65f6eb4d..b8b7fda35139da 100644 --- a/sound/soc/sof/intel/hda.h +++ b/sound/soc/sof/intel/hda.h @@ -22,6 +22,9 @@ #include "shim.h" struct usb_offload_ctx; +struct usb_device; +struct usb_offload_usb_dev_info; +struct snd_soc_usb; /* PCI registers */ #define PCI_TCSEL 0x44 @@ -586,6 +589,17 @@ struct sof_intel_hda_dev { /* USB audio offload context allocated by the offload library */ struct usb_offload_ctx *uaol; + /* ASoC USB port, non NULL while the UAOL DAI is bound into a card */ + struct snd_soc_usb *uaol_usb; + + /* offload capable USB audio devices */ + struct list_head uaol_devices; + /* protects uaol_devices */ + struct mutex uaol_mutex; + + /* reports whether any offload capable device is connected */ + struct snd_soc_jack uaol_jack; + /* FW clock config, 0:HPRO, 1:LPRO */ bool clk_config_lpro; @@ -1166,6 +1180,9 @@ int hda_dai_trigger(struct snd_pcm_substream *substream, int cmd, #if IS_ENABLED(CONFIG_SND_SOC_SOF_INTEL_USB_OFFLOAD) void hda_uaol_set_dai_drv_ops(struct snd_sof_dev *sdev, struct snd_sof_dsp_ops *ops); +int hda_uaol_notify_connect(struct device *dev, struct usb_device *udev, + const struct usb_offload_usb_dev_info *id); +int hda_uaol_notify_disconnect(struct device *dev, struct usb_device *udev); #else static inline void hda_uaol_set_dai_drv_ops(struct snd_sof_dev *sdev, struct snd_sof_dsp_ops *ops) { } From 5d329651a469f5ef3c2d323805b673b5ba6e2bb0 Mon Sep 17 00:00:00 2001 From: Peter Ujfalusi Date: Wed, 4 Nov 2026 14:59:55 +0200 Subject: [PATCH 37/53] ASoC: SOF: Intel: hda-uaol: Offload the USB stream on the UAOL DAI The UAOL backend DAI only sets up the link DMA, the DSP side of the link. Nothing claims the USB side, so the endpoint the DSP is supposed to read from is still owned by the host and its transfer ring does not even exist: a ring is only allocated once the streaming altsetting is selected. Acquire the stream from the offload library in hw_params and release it in hw_free. This starts the device on the USB side, hands its endpoint ring to the DSP and takes the device away from user space for as long as the DAI holds it. Every UAOL CPU DAI claims a struct hda_uaol_stream of its own when it probes, found from the DAI on every callback: that is where the acquired device and its USB PCM, the backend substream and the suspended flag of the backend live, so the backends of a direction do not step on each other. The ASoC USB route a backend plays to is the pin number of its DAI name, "UAOL0 Playback Pin1" playing to playback route 1. DPCM runs a backend's hw_params once for every frontend that reaches its own hw_params while the backend is still in the OPEN, HW_PARAMS or HW_FREE state, so with a playback PCM and its deep buffer opened at once the backend sees two calls. The stream is acquired by the first one only, and released from hw_free, which DPCM runs once the last frontend is gone. A system suspend resets the pipeline and frees the DAI copier while the stream stays open, taking the device's streaming altsetting with it. DPCM brings such a stream back through the backend's prepare, not hw_params, so acquire the stream again from prepare when the backend saw a suspend trigger. The trigger runs under the PCM stream lock and only records the fact. Which device and which of its PCMs a backend offloads is the ASoC USB route control's decision: the DAI resolves the selection of its route when acquiring the stream and reports the route as busy while it holds one, so the selection cannot change under a running stream. Unplugging the device a stream plays to would otherwise leave that stream running: the library gives the USB side back and the host drops the device from the port, but nothing reaches the frontend, so the link keeps running into a dead endpoint and the application keeps feeding it. The USB audio card ends its users' streams when it goes away, by stopping them into the DISCONNECTED state; do the same for the frontends routed to the backends whose stream belongs to the unplugged device, so that the application sees the device disappear the way it does when playing to the card directly. The backend substream is remembered along with the device for that, and the frontends are walked under the card's DPCM mutex, which the backend's hw_params and hw_free also hold. The backend then forgets the device: the library has given the USB side back already and the usb_device goes away with the disconnect, so the hw_free of the frontends' teardown must not hand it to the library again. SOF only hands this DAI S24_LE, while a USB device commonly packs 24-bit audio into 3 bytes. The library reports the container the acquired stream runs in, so set the link DMA up from that, so that it runs the same 24-bit container and the DSP packs the samples on their way to the link. DPCM hands hw_params a copy of the params, while prepare programs the link DMA again after a stop from the backend's own copy, so prepare applies the container to that one as well. A second frontend, the deep buffer, can share the backend with the one which has it open. They share the one endpoint and the one mixer, so the second one can only run what the first one opened the backend with: the rate, the channel count and the format. Remember those when the stream is acquired and refuse a joining frontend which asks for anything else, before the core prepares anything for it. The route control refuses to change a route while its backend holds a stream, but the selection is read before the device is acquired and the route is marked busy once it is held. Read the selection back once the stream is set: the control decides whether the route is busy under the lock the selection is read with, so a change made during the acquisition is seen there and a later one is refused. Release the stream and fail with -EBUSY when the selection moved, the stream has not started yet. Keep a stream suspended by the SUSPEND trigger marked until it is acquired again: a prepare retried after a failed one, after a route change in between or after the device went away, must not run the common prepare and start the DSP on a gateway whose xHCI sideband resources were released and may belong to another stream by then. Clear the flag only once the stream is acquired again, or the stream is closed, and fail prepare when the backend has no device. Signed-off-by: Peter Ujfalusi --- sound/soc/sof/intel/hda-uaol.c | 450 +++++++++++++++++++++++++++++++-- sound/soc/sof/intel/hda.h | 38 +++ 2 files changed, 467 insertions(+), 21 deletions(-) diff --git a/sound/soc/sof/intel/hda-uaol.c b/sound/soc/sof/intel/hda-uaol.c index 8099d2127ca22d..e4325b317daa4a 100644 --- a/sound/soc/sof/intel/hda-uaol.c +++ b/sound/soc/sof/intel/hda-uaol.c @@ -85,6 +85,42 @@ static bool hda_uaol_dev_identity_equal(const struct hda_uaol_dev *a, sizeof(a->cpcm_name_storage)); } +/* + * Whether a stream is already open on @route of @direction, consulted by the + * route kcontrol before it lets a selection change - a link a running stream + * owns is never switched out from under it. + */ +static bool hda_uaol_is_route_busy(struct snd_soc_usb *usb, int direction, + unsigned int route) +{ + struct sof_intel_hda_dev *hdev = usb->priv_data; + int i; + + for (i = 0; i < ARRAY_SIZE(hdev->uaol_streams); i++) { + struct hda_uaol_stream *stream = &hdev->uaol_streams[i]; + + if (stream->dai && stream->direction == direction && + stream->route == route && stream->udev) + return true; + } + + return false; +} + +/* The backend state of a UAOL CPU DAI, claimed when the DAI probed */ +static struct hda_uaol_stream *hda_uaol_stream_get(struct sof_intel_hda_dev *hdev, + struct snd_soc_dai *dai) +{ + int i; + + for (i = 0; i < ARRAY_SIZE(hdev->uaol_streams); i++) { + if (hdev->uaol_streams[i].dai == dai) + return &hdev->uaol_streams[i]; + } + + return NULL; +} + int hda_uaol_notify_connect(struct device *dev, struct usb_device *udev, const struct usb_offload_usb_dev_info *id) { @@ -167,12 +203,73 @@ int hda_uaol_notify_connect(struct device *dev, struct usb_device *udev, } EXPORT_SYMBOL_NS(hda_uaol_notify_connect, "SND_SOC_SOF_INTEL_HDA_COMMON"); +/* + * Nothing else tells a frontend that the device it plays to has been unplugged: + * the link keeps running into a dead endpoint and the application keeps + * feeding it. End its stream the way the USB card ends its own users' streams + * when it goes away, so the application sees the device disappear instead of + * silence. + */ +static void hda_uaol_stop_frontends(struct sof_intel_hda_dev *hdev, + struct usb_device *udev) +{ + int i; + + for (i = 0; i < ARRAY_SIZE(hdev->uaol_streams); i++) { + struct hda_uaol_stream *stream = &hdev->uaol_streams[i]; + struct snd_pcm_substream *be_substream = READ_ONCE(stream->be); + int dir = stream->direction; + struct snd_soc_pcm_runtime *be; + struct snd_soc_dpcm *dpcm; + + if (!be_substream) + continue; + + be = snd_soc_substream_to_rtd(be_substream); + + /* the backend's hw_params and hw_free run under this, so the pair is stable */ + snd_soc_dpcm_mutex_lock(be); + if (stream->udev != udev || stream->be != be_substream) { + snd_soc_dpcm_mutex_unlock(be); + continue; + } + + for_each_dpcm_fe(be, dir, dpcm) { + struct snd_pcm_substream *fe_substream = + snd_soc_dpcm_get_substream(dpcm->fe, dir); + + snd_pcm_stream_lock_irq(fe_substream); + if (fe_substream->runtime) { + if (snd_pcm_running(fe_substream)) + snd_pcm_stop(fe_substream, SNDRV_PCM_STATE_DISCONNECTED); + else + __snd_pcm_set_state(fe_substream->runtime, + SNDRV_PCM_STATE_DISCONNECTED); + } + snd_pcm_stream_unlock_irq(fe_substream); + } + + /* + * The library has already given the USB side back and the device + * goes away with the disconnect. Forget it, so that the trigger and + * hw_free of the frontends' teardown do not hand the stale pointer + * back to the library, where a device reusing the address would + * match. + */ + stream->udev = NULL; + stream->be = NULL; + snd_soc_dpcm_mutex_unlock(be); + } +} + int hda_uaol_notify_disconnect(struct device *dev, struct usb_device *udev) { struct snd_sof_dev *sdev = dev_get_drvdata(dev); struct sof_intel_hda_dev *hdev = sdev->pdata->hw_pdata; struct hda_uaol_dev *offload_dev; + hda_uaol_stop_frontends(hdev, udev); + scoped_guard(mutex, &hdev->uaol_mutex) { offload_dev = hda_uaol_find(hdev, udev); if (offload_dev) @@ -210,8 +307,29 @@ static int uaol_dai_probe(struct snd_soc_dai *dai) { struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(dai->component); struct sof_intel_hda_dev *hdev = sdev->pdata->hw_pdata; + struct hda_uaol_stream *stream; struct snd_soc_usb *usb; - int ret, i; + unsigned int route; + int direction, ret, i; + + /* + * The pin number of the DAI, "UAOL0 Playback Pin1", is the ordinal of + * the backend among those of its direction, which is the ASoC USB + * route it plays to. + */ + if (sscanf(dai->driver->name, "UAOL%*u %*s Pin%u", &route) != 1) + return -EINVAL; + direction = dai->driver->playback.channels_min ? SNDRV_PCM_STREAM_PLAYBACK : + SNDRV_PCM_STREAM_CAPTURE; + + /* a free slot has no DAI */ + stream = hda_uaol_stream_get(hdev, NULL); + if (!stream) + return -ENOSPC; + stream->hdev = hdev; + stream->dai = dai; + stream->direction = direction; + stream->route = route; if (!hdev->uaol) { /* @@ -226,7 +344,7 @@ static int uaol_dai_probe(struct snd_soc_dai *dai) ret = snd_soc_card_jack_new(dai->component->card, "USB Offload Jack", SND_JACK_USB, &hdev->uaol_jack); if (ret) - return ret; + goto err_free_slot; snd_jack_report(hdev->uaol_jack.jack, 0); return 0; @@ -237,16 +355,18 @@ static int uaol_dai_probe(struct snd_soc_dai *dai) return 0; usb = snd_soc_usb_allocate_port(dai->component, hdev); - if (IS_ERR(usb)) - return PTR_ERR(usb); + if (IS_ERR(usb)) { + ret = PTR_ERR(usb); + goto err_free_slot; + } + + usb->is_route_busy = hda_uaol_is_route_busy; /* a route per backend of a direction, each with a device of its own */ for (i = 0; i <= SNDRV_PCM_STREAM_LAST; i++) { ret = snd_soc_usb_set_num_routes(usb, i, max(hdev->uaol_stream_num[i], 1U)); - if (ret) { - snd_soc_usb_free_port(usb); - return ret; - } + if (ret) + goto err_free_port; } /* @@ -257,16 +377,12 @@ static int uaol_dai_probe(struct snd_soc_dai *dai) */ ret = snd_soc_card_jack_new(dai->component->card, "USB Offload Jack", SND_JACK_USB, &hdev->uaol_jack); - if (ret) { - snd_soc_usb_free_port(usb); - return ret; - } + if (ret) + goto err_free_port; ret = snd_soc_usb_setup_offload_route_ctl(dai->component, usb); - if (ret) { - snd_soc_usb_free_port(usb); - return ret; - } + if (ret) + goto err_free_port; scoped_guard(mutex, &hdev->uaol_mutex) hdev->uaol_usb = usb; @@ -275,15 +391,33 @@ static int uaol_dai_probe(struct snd_soc_dai *dai) snd_soc_usb_add_port(usb); return 0; + +err_free_port: + snd_soc_usb_free_port(usb); + /* + * ASoC does not call remove for a DAI whose probe failed, and the card + * frees the jack of the failed bring-up: do not leave it for the + * connect notifications to report on. + */ + scoped_guard(mutex, &hdev->uaol_mutex) + hdev->uaol_jack.jack = NULL; +err_free_slot: + /* ASoC does not call remove for a DAI whose probe failed */ + stream->dai = NULL; + return ret; } static int uaol_dai_remove(struct snd_soc_dai *dai) { struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(dai->component); struct sof_intel_hda_dev *hdev = sdev->pdata->hw_pdata; + struct hda_uaol_stream *stream = hda_uaol_stream_get(hdev, dai); struct hda_uaol_dev *dev, *tmp; struct snd_soc_usb *usb; + if (stream) + stream->dai = NULL; + /* * Taken away under the lock so that a connect racing with the card * going away sees no port to attach to. The jack itself belongs to the @@ -311,14 +445,286 @@ static int uaol_dai_remove(struct snd_soc_dai *dai) return 0; } -/* The link DMA is run like the one of the SSP and DMIC DAIs */ +/* + * Which device a backend plays to is the ASoC USB route kcontrol's job to + * decide, per route (defaulting to the Nth connected pair for route N, see + * snd_soc_usb_get_selected_device()) - this + * just resolves that selection back to the Intel-side record that carries + * the actual usb_device pointer. + */ +static struct usb_device *hda_uaol_selected(struct sof_intel_hda_dev *hdev, + int direction, unsigned int route, + int *pcm_index) +{ + struct snd_soc_usb_device *sdev; + struct hda_uaol_dev *dev; + int ret; + + ret = snd_soc_usb_get_selected_device(hdev->uaol_usb, direction, route, + &sdev, pcm_index); + if (ret || !sdev) + return NULL; + + scoped_guard(mutex, &hdev->uaol_mutex) { + list_for_each_entry(dev, &hdev->uaol_devices, node) { + if (&dev->sdev == sdev) + return dev->udev; + } + } + + return NULL; +} + +static void uaol_set_be_format(struct snd_pcm_hw_params *params, snd_pcm_format_t format) +{ + struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT); + + snd_mask_none(fmt); + snd_mask_set_format(fmt, format); +} + +static void uaol_dai_stop_stream(struct snd_pcm_substream *substream, + struct hda_uaol_stream *stream); + +/* + * Take the device away from the host for as long as the DSP drives its + * endpoint. The stream is started from hw_params and, after a system + * suspend, from prepare, which is the only callback the resume path runs. + */ +static int uaol_dai_start_stream(struct snd_pcm_substream *substream, + struct snd_pcm_hw_params *params, + struct hda_uaol_stream *stream) +{ + struct snd_soc_dai *cpu_dai = stream->dai; + struct snd_sof_dev *sdev = dai_to_sdev(substream, cpu_dai); + struct sof_intel_hda_dev *hdev = sdev->pdata->hw_pdata; + int direction = substream->stream; + struct usb_offload_stream_hw hw; + struct usb_device *udev; + int pcm_index; + int ret; + + if (!hdev->uaol) + return -ENODEV; + + udev = hda_uaol_selected(hdev, direction, stream->route, &pcm_index); + if (!udev) { + dev_err(cpu_dai->dev, "no USB device selected for offload\n"); + return -ENODEV; + } + + ret = usb_offload_acquire_stream(hdev->uaol, udev, direction, pcm_index, + params, &hw); + if (ret) { + dev_err(cpu_dai->dev, "failed to acquire the USB stream: %d\n", ret); + return ret; + } + + /* + * The device may run the stream in another container than the one the + * frontend negotiated, SOF only ever hands S24_LE for 24 bits and a + * device packing them into 3 bytes will not recognise it. The link DMA + * is set up from the params, so it runs the container of the device, + * the DSP packs the samples on their way to the link. On hw_params + * those are a copy DPCM made for this call, prepare sets them again + * for itself. + */ + stream->be_format = hw.format; + uaol_set_be_format(params, hw.format); + + stream->udev = udev; + stream->pcm_index = hw.pcm_index; + stream->be = substream; + + /* + * The route control refuses a change once the stream is set here, and + * it decides that under the lock the selection is read back with: a + * change which went through while the device was being acquired is + * seen now, a later one finds the route busy. Back out of the former, + * the stream would play to a device the route no longer selects. + */ + if (hda_uaol_selected(hdev, direction, stream->route, &pcm_index) != udev || + pcm_index != hw.pcm_index) { + uaol_dai_stop_stream(substream, stream); + return -EBUSY; + } + + return 0; +} + +static void uaol_dai_stop_stream(struct snd_pcm_substream *substream, + struct hda_uaol_stream *stream) +{ + struct snd_sof_dev *sdev = dai_to_sdev(substream, stream->dai); + struct sof_intel_hda_dev *hdev = sdev->pdata->hw_pdata; + struct usb_device *udev = stream->udev; + + if (!udev) + return; + + stream->udev = NULL; + stream->be = NULL; + usb_offload_release_stream(hdev->uaol, udev, stream->direction, stream->pcm_index); +} + +static int uaol_dai_hw_params(struct snd_pcm_substream *substream, + struct snd_pcm_hw_params *params, + struct snd_soc_dai *cpu_dai) +{ + struct snd_sof_dev *sdev = dai_to_sdev(substream, cpu_dai); + struct hda_uaol_stream *stream = hda_uaol_stream_get(sdev->pdata->hw_pdata, cpu_dai); + unsigned int rate, channels; + snd_pcm_format_t format; + int ret; + + if (!stream) + return -ENODEV; + + /* + * A second frontend joining the stream's mixer, the deep buffer, runs + * the backend's hw_params again while the first one still has it. The + * USB stream is acquired once, by whichever frontend came first, and + * stays until the last one is gone. + * + * The two share the one endpoint and the one mixer, so the second one + * can only run what the first one opened the backend with. Refuse it + * here, before the core prepares anything for it: a frontend failing + * further down would take what the first one prepared with it. + */ + if (stream->udev) { + /* pairs with the release once the first frontend's parameters are stored */ + if (!smp_load_acquire(&stream->params_valid) || + params_rate(params) != stream->rate || + params_channels(params) != stream->channels || + params_format(params) != stream->format) { + dev_err(cpu_dai->dev, + "stream is open at %u Hz, %u ch, format %d, not %u Hz, %u ch, format %d\n", + stream->rate, stream->channels, stream->format, + params_rate(params), params_channels(params), + params_format(params)); + return -EBUSY; + } + + /* the link DMA runs the format of the device, not the frontend's */ + uaol_set_be_format(params, stream->be_format); + + return non_hda_dai_hw_params(substream, params, cpu_dai); + } + + /* the params are set to the format of the device, keep what the frontend asked for */ + rate = params_rate(params); + channels = params_channels(params); + format = params_format(params); + + ret = uaol_dai_start_stream(substream, params, stream); + if (ret) + return ret; + + ret = non_hda_dai_hw_params(substream, params, cpu_dai); + if (ret) { + uaol_dai_stop_stream(substream, stream); + return ret; + } + + stream->rate = rate; + stream->channels = channels; + stream->format = format; + /* publish the parameters to the frontend rules and to a joining frontend */ + smp_store_release(&stream->params_valid, true); + + return 0; +} + +static int uaol_dai_hw_free(struct snd_pcm_substream *substream, + struct snd_soc_dai *cpu_dai) +{ + struct snd_sof_dev *sdev = dai_to_sdev(substream, cpu_dai); + struct hda_uaol_stream *stream = hda_uaol_stream_get(sdev->pdata->hw_pdata, cpu_dai); + int ret = hda_dai_hw_free(substream, cpu_dai); + + if (stream) { + /* the next frontend to open the backend decides the parameters */ + smp_store_release(&stream->params_valid, false); + uaol_dai_stop_stream(substream, stream); + /* a stream closed while suspended has nothing left to reacquire */ + stream->suspended = false; + } + + return ret; +} + +/* + * A system suspend resets the pipeline and frees the DAI copier, and takes + * the device's streaming altsetting with it, while the stream itself stays + * open. The resume path only runs prepare on the backend, so nothing + * reestablishes the USB side on its own and the stream is acquired again + * from here, before the pipeline is rebuilt. + */ +static int uaol_dai_prepare(struct snd_pcm_substream *substream, + struct snd_soc_dai *cpu_dai) +{ + struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream); + struct snd_sof_dev *sdev = dai_to_sdev(substream, cpu_dai); + struct sof_intel_hda_dev *hdev = sdev->pdata->hw_pdata; + struct hda_uaol_stream *stream = hda_uaol_stream_get(hdev, cpu_dai); + struct snd_pcm_hw_params *params; + int direction = substream->stream; + int ret; + + if (!stream) + return non_hda_dai_prepare(substream, cpu_dai); + + params = &rtd->dpcm[direction].hw_params; + + /* left set until the stream is back, for a failed prepare to retry */ + if (stream->suspended) { + uaol_dai_stop_stream(substream, stream); + + ret = uaol_dai_start_stream(substream, params, stream); + if (ret) + return ret; + + stream->suspended = false; + } + + /* the device is gone, the gateway would run on a released resource */ + if (!stream->udev) + return -ENODEV; + + /* + * The common prepare programs the link DMA again after a stop, from + * the backend's own copy of the params, which DPCM keeps at what the + * frontend negotiated. Set the format the stream was acquired with, + * as it was for hw_params. + */ + uaol_set_be_format(params, stream->be_format); + + return non_hda_dai_prepare(substream, cpu_dai); +} + +/* + * The backend is triggered on the way into a system suspend, which is the + * only notice this driver gets that the stream will have to be reacquired. + */ +static int uaol_dai_trigger(struct snd_pcm_substream *substream, int cmd, + struct snd_soc_dai *cpu_dai) +{ + struct snd_sof_dev *sdev = dai_to_sdev(substream, cpu_dai); + struct hda_uaol_stream *stream = hda_uaol_stream_get(sdev->pdata->hw_pdata, cpu_dai); + + if (stream && cmd == SNDRV_PCM_TRIGGER_SUSPEND) + stream->suspended = true; + + return hda_dai_trigger(substream, cmd, cpu_dai); +} + static const struct snd_soc_dai_ops uaol_dai_ops = { .probe = uaol_dai_probe, .remove = uaol_dai_remove, - .hw_params = non_hda_dai_hw_params, - .hw_free = hda_dai_hw_free, - .trigger = hda_dai_trigger, - .prepare = non_hda_dai_prepare, + .hw_params = uaol_dai_hw_params, + .hw_free = uaol_dai_hw_free, + .trigger = uaol_dai_trigger, + .prepare = uaol_dai_prepare, }; void hda_uaol_set_dai_drv_ops(struct snd_sof_dev *sdev, struct snd_sof_dsp_ops *ops) @@ -335,3 +741,5 @@ void hda_uaol_set_dai_drv_ops(struct snd_sof_dev *sdev, struct snd_sof_dsp_ops * } } } + +MODULE_IMPORT_NS("USB_AUDIO_OFFLOAD"); diff --git a/sound/soc/sof/intel/hda.h b/sound/soc/sof/intel/hda.h index b8b7fda35139da..b6ab362efd2588 100644 --- a/sound/soc/sof/intel/hda.h +++ b/sound/soc/sof/intel/hda.h @@ -512,6 +512,41 @@ struct sof_ace3_mic_privacy { struct work_struct work; }; +/* + * One UAOL backend: the DAI it is bound to and the USB stream it holds. This + * lives for the lifetime of the card and names the device only indirectly, by + * the route of its direction the backend plays to. + */ +struct hda_uaol_stream { + struct sof_intel_hda_dev *hdev; + /* the UAOL CPU DAI, NULL while no DAI is bound to this slot */ + struct snd_soc_dai *dai; + int direction; + /* the ASoC USB route of @direction this backend plays to */ + unsigned int route; + + /* device whose stream the DSP owns, NULL when idle */ + struct usb_device *udev; + /* USB PCM of @udev the stream was acquired on */ + int pcm_index; + /* backend substream the stream runs on, its frontends are stopped on unplug */ + struct snd_pcm_substream *be; + /* + * Parameters the first frontend opened the backend with, as it passed + * them in. A frontend joining it, the deep buffer, shares the backend + * and its mixer and has to use the same. + */ + unsigned int rate; + unsigned int channels; + snd_pcm_format_t format; + /* the format the device runs the stream in, what the backend is set up with */ + snd_pcm_format_t be_format; + /* @rate, @channels and @format are valid, published after they are set */ + bool params_valid; + /* the stream was suspended and has to be reacquired on prepare */ + bool suspended; +}; + /* represents DSP HDA controller frontend - i.e. host facing control */ struct sof_intel_hda_dev { bool imrboot_supported; @@ -600,6 +635,9 @@ struct sof_intel_hda_dev { /* reports whether any offload capable device is connected */ struct snd_soc_jack uaol_jack; + /* one per UAOL CPU DAI, claimed when the DAI probes */ + struct hda_uaol_stream uaol_streams[SOF_UAOL_NUM_DAIS]; + /* FW clock config, 0:HPRO, 1:LPRO */ bool clk_config_lpro; From a1873c89208fe903192853c157f505fa51adc7ce Mon Sep 17 00:00:00 2001 From: Peter Ujfalusi Date: Thu, 5 Nov 2026 09:11:58 +0200 Subject: [PATCH 38/53] ASoC: SOF: ipc4-topology: Build the UAOL DAI copier gateway blob The UAOL gateway needs a configuration blob the topology cannot provide: it describes the link and the USB endpoint behind it, neither of which is known before the stream is opened. The firmware reads it as a sequence of TLVs: the xHCI bus address, the link configuration, the FIFO start address offsets, the data and feedback endpoint information, the ART divider and the endpoint's service interval. Define the blob and build it when the DAI widget is set up, since its size is part of the copier IPC. The link index, the ART divider and the FIFO layout are known at that point: the link FIFO is divided evenly between the streams of a direction, with the T output streams carrying playback and the I input streams carrying the capture and the clock feedback. The fields which describe the USB endpoint are left for the platform code to fill in once the stream has been acquired. The number of streams per direction the link is advertised with comes from sof_ipc4_uaol_get_stream_counts(), which the FIFO layout uses as well: a link the gateway configuration cannot be built for, because the firmware reports more streams than it has room for or none to play to, is not offered with UAOL PCMs which would fail to set up. Signed-off-by: Peter Ujfalusi --- sound/soc/sof/intel/hda.c | 20 ++-- sound/soc/sof/ipc4-topology.c | 168 ++++++++++++++++++++++++++++++++++ sound/soc/sof/ipc4-topology.h | 114 +++++++++++++++++++++++ 3 files changed, 292 insertions(+), 10 deletions(-) diff --git a/sound/soc/sof/intel/hda.c b/sound/soc/sof/intel/hda.c index 1a14e535eb19bd..77805361d477db 100644 --- a/sound/soc/sof/intel/hda.c +++ b/sound/soc/sof/intel/hda.c @@ -1696,20 +1696,20 @@ void hda_set_mach_params(struct snd_soc_acpi_mach *mach, */ static unsigned int hda_uaol_stream_num(struct snd_sof_dev *sdev, int direction) { - unsigned int tx_streams, rx_streams; - u32 caps; + unsigned int playback, capture; - if (hda_uaol_link_caps(sdev, 0, &caps)) - return 0; + /* what the gateway configuration can hold is what the DAIs have to offer */ + static_assert(SOF_UAOL_MAX_PLAYBACK_STREAMS >= SOF_IPC4_UAOL_FIFO_SAO_STREAMS && + SOF_UAOL_MAX_CAPTURE_STREAMS >= SOF_IPC4_UAOL_FIFO_SAO_STREAMS); - tx_streams = FIELD_GET(SOF_IPC4_UAOL_OUTPUT_STREAMS_MASK, caps); - rx_streams = FIELD_GET(SOF_IPC4_UAOL_INPUT_STREAMS_MASK, caps); + if (!hda_uaol_available(sdev)) + return 0; - if (direction == SNDRV_PCM_STREAM_PLAYBACK) - return min(tx_streams, SOF_UAOL_MAX_PLAYBACK_STREAMS); + /* the counts the gateway configuration can be built for, see the topology side */ + if (sof_ipc4_uaol_get_stream_counts(sdev, 0, &playback, &capture)) + return 0; - return min(rx_streams > tx_streams ? rx_streams - tx_streams : 0, - SOF_UAOL_MAX_CAPTURE_STREAMS); + return direction == SNDRV_PCM_STREAM_PLAYBACK ? playback : capture; } void hda_finalize_mach_params(struct snd_sof_dev *sdev, diff --git a/sound/soc/sof/ipc4-topology.c b/sound/soc/sof/ipc4-topology.c index b75cb749fc0cb7..e54f23f3cca0ec 100644 --- a/sound/soc/sof/ipc4-topology.c +++ b/sound/soc/sof/ipc4-topology.c @@ -805,6 +805,169 @@ static void sof_ipc4_widget_free_comp_pcm(struct snd_sof_widget *swidget) swidget->private = NULL; } +/** + * struct sof_ipc4_uaol_fifo_layout - division of a UAOL link FIFO + * @tx_size: space available to each transmit stream + * @rx_base: start offset of the first receive stream + * @rx_size: space available to each receive stream + */ +struct sof_ipc4_uaol_fifo_layout { + u32 tx_size; + u32 rx_base; + u32 rx_size; +}; + +/** + * sof_ipc4_uaol_get_stream_counts - audio streams a UAOL link can run + * @sdev: SOF device + * @link_idx: index of the UAOL link + * @playback: set to the number of playback streams + * @capture: set to the number of capture streams + * + * The firmware reports the output streams of the link and the input streams, + * of which one per output stream carries the clock feedback and not audio. + * The gateway configuration has the start offsets of two streams per direction + * in fixed width fields. A link with more than that, or with no stream to play + * to or record from, cannot be configured, and is reported as not usable + * instead of having its streams offered and failing later. + * + * Return: 0 on success, negative error code if the link cannot be used. + */ +int sof_ipc4_uaol_get_stream_counts(struct snd_sof_dev *sdev, int link_idx, + unsigned int *playback, unsigned int *capture) +{ + struct sof_ipc4_fw_data *ipc4_data = sdev->private; + struct sof_ipc4_intel_uaol_link_caps *caps; + u32 tx_streams, rx_streams; + + if (!ipc4_data->uaol_caps || link_idx >= ipc4_data->uaol_caps->link_count) + return -ENODEV; + + caps = &ipc4_data->uaol_caps->link_caps[link_idx]; + + tx_streams = FIELD_GET(SOF_IPC4_UAOL_OUTPUT_STREAMS_MASK, caps->stream_caps); + rx_streams = FIELD_GET(SOF_IPC4_UAOL_INPUT_STREAMS_MASK, caps->stream_caps); + + if (!tx_streams || rx_streams <= tx_streams) + return -EINVAL; + + rx_streams -= tx_streams; + + if (tx_streams > SOF_IPC4_UAOL_FIFO_SAO_STREAMS || + rx_streams > SOF_IPC4_UAOL_FIFO_SAO_STREAMS) + return -EINVAL; + + *playback = tx_streams; + *capture = rx_streams; + + return 0; +} +EXPORT_SYMBOL(sof_ipc4_uaol_get_stream_counts); + +/* + * Only the start offset of a stream is programmable, the space it can use + * reaches up to the start offset of the next one, so the FIFOs are divided + * evenly between the hardware's full stream count for the link - not how + * many DAIs SOF happens to configure. Which of the hardware's stream slots + * a given USB device's acquire lands on is decided by the xHCI sideband + * resource the endpoint gets, within the range of its function, which is + * independent of SOF's own widget count: even with only ever one stream + * actually running, that one stream can land on any slot of its function, + * so every slot needs a real, non-zero share regardless of how many are ever + * simultaneously active. Each transmit stream has a clock feedback channel + * taking one bus word at the beginning of the receive FIFO, so the receive + * streams carrying audio are the reported ones less the transmit stream + * count. + */ +static int sof_ipc4_uaol_get_fifo_layout(struct snd_sof_dev *sdev, int link_idx, + struct sof_ipc4_uaol_fifo_layout *layout) +{ + struct sof_ipc4_fw_data *ipc4_data = sdev->private; + struct sof_ipc4_intel_uaol_link_caps *caps; + unsigned int tx_streams, rx_streams; + int ret; + + ret = sof_ipc4_uaol_get_stream_counts(sdev, link_idx, &tx_streams, &rx_streams); + if (ret) { + dev_err(sdev->dev, "UAOL link %d has no usable streams: %d\n", link_idx, ret); + return ret; + } + + caps = &ipc4_data->uaol_caps->link_caps[link_idx]; + + layout->tx_size = round_down(caps->max_tx_fifo_size / tx_streams, + SOF_IPC4_UAOL_FIFO_BUS_WIDTH); + layout->rx_base = tx_streams * SOF_IPC4_UAOL_FIFO_BUS_WIDTH; + if (caps->max_rx_fifo_size > layout->rx_base) + layout->rx_size = round_down((caps->max_rx_fifo_size - layout->rx_base) / + rx_streams, SOF_IPC4_UAOL_FIFO_BUS_WIDTH); + else + layout->rx_size = 0; + + if (!layout->tx_size || !layout->rx_size || + !FIELD_FIT(SOF_IPC4_UAOL_FIFO_SAO_TX1, layout->tx_size) || + !FIELD_FIT(SOF_IPC4_UAOL_FIFO_SAO_RX1, layout->rx_base + layout->rx_size)) { + dev_err(sdev->dev, "UAOL link %d: FIFO sizes tx %u rx %u do not fit the gateway configuration\n", + link_idx, caps->max_tx_fifo_size, caps->max_rx_fifo_size); + return -EINVAL; + } + + return 0; +} + +static int sof_ipc4_widget_setup_uaol(struct snd_sof_dev *sdev, + struct snd_sof_widget *swidget, + struct sof_ipc4_copier *ipc4_copier) +{ + struct sof_ipc4_uaol_configuration_blob *blob; + struct sof_ipc4_uaol_fifo_layout layout; + int ret; + + ret = sof_ipc4_uaol_get_fifo_layout(sdev, ipc4_copier->dai_index, &layout); + if (ret) + return ret; + + blob = kzalloc_obj(*blob); + if (!blob) + return -ENOMEM; + + blob->xhci_bdf_tlv.type = SOF_IPC4_UAOL_XHCI_BDF_ID; + blob->xhci_bdf_tlv.length = sizeof(blob->xhci_bdf); + blob->config_tlv.type = SOF_IPC4_UAOL_CONFIG_ID; + blob->config_tlv.length = sizeof(blob->config); + blob->fifo_sao_tlv.type = SOF_IPC4_UAOL_FIFO_SAO_ID; + blob->fifo_sao_tlv.length = sizeof(blob->fifo_sao_tx) + sizeof(blob->fifo_sao_rx); + blob->ep_info_tlv.type = SOF_IPC4_UAOL_EP_INFO_ID; + blob->ep_info_tlv.length = sizeof(blob->ep_info); + blob->feedback_ep_info_tlv.type = SOF_IPC4_UAOL_FEEDBACK_EP_INFO_ID; + blob->feedback_ep_info_tlv.length = sizeof(blob->feedback_ep_info); + blob->art_divider_tlv.type = SOF_IPC4_UAOL_ART_DIVIDER_ID; + blob->art_divider_tlv.length = sizeof(blob->art_divider); + blob->service_interval_tlv.type = SOF_IPC4_UAOL_SERVICE_INTERVAL_ID; + blob->service_interval_tlv.length = sizeof(blob->service_interval_us); + + /* the stream is only known once the sideband resource is allocated */ + blob->config = FIELD_PREP(SOF_IPC4_UAOL_CONFIG_LINK_IDX, ipc4_copier->dai_index); + + /* the xHCI global time is already expressed in DSP timer ticks */ + blob->art_divider = FIELD_PREP(SOF_IPC4_UAOL_ART_MULTIPLIER, 1) | + FIELD_PREP(SOF_IPC4_UAOL_ART_DIVIDER, 1); + + /* + * The transmit stream 0 and the clock feedback channels both start at + * offset 0, every stream after them starts where its predecessor ends. + */ + blob->fifo_sao_tx = FIELD_PREP(SOF_IPC4_UAOL_FIFO_SAO_TX1, layout.tx_size); + blob->fifo_sao_rx = FIELD_PREP(SOF_IPC4_UAOL_FIFO_SAO_RX0, layout.rx_base) | + FIELD_PREP(SOF_IPC4_UAOL_FIFO_SAO_RX1, + layout.rx_base + layout.rx_size); + + ipc4_copier->copier_config = (uint32_t *)blob; + ipc4_copier->data.gtw_cfg.config_length = sizeof(*blob) >> 2; + + return 0; +} + static int sof_ipc4_widget_setup_comp_dai(struct snd_sof_widget *swidget) { struct sof_ipc4_available_audio_format *available_fmt; @@ -925,6 +1088,11 @@ static int sof_ipc4_widget_setup_comp_dai(struct snd_sof_widget *swidget) ipc4_copier->data.gtw_cfg.node_id |= SOF_IPC4_NODE_INDEX_INTEL_DMIC(ipc4_copier->dai_index); break; + case SOF_DAI_INTEL_UAOL: + ret = sof_ipc4_widget_setup_uaol(sdev, swidget, ipc4_copier); + if (ret) + goto free_available_fmt; + break; default: ipc4_copier->gtw_attr = kzalloc_obj(*ipc4_copier->gtw_attr); if (!ipc4_copier->gtw_attr) { diff --git a/sound/soc/sof/ipc4-topology.h b/sound/soc/sof/ipc4-topology.h index b538b4183897a9..3de9fbbad307bd 100644 --- a/sound/soc/sof/ipc4-topology.h +++ b/sound/soc/sof/ipc4-topology.h @@ -326,6 +326,120 @@ struct sof_ipc4_alh_configuration_blob { struct sof_ipc4_dma_stream_ch_map alh_cfg; }; +/* + * The UAOL gateway configuration is a plain list of TLVs, which the firmware + * hands over to the UAOL hardware driver as it is. Unknown types are skipped, + * so the DMA config TLV can be appended to it like for any other gateway. + */ +#define SOF_IPC4_UAOL_XHCI_BDF_ID 0 +#define SOF_IPC4_UAOL_CONFIG_ID 1 +#define SOF_IPC4_UAOL_FIFO_SAO_ID 2 +#define SOF_IPC4_UAOL_EP_INFO_ID 3 +#define SOF_IPC4_UAOL_FEEDBACK_EP_INFO_ID 4 +#define SOF_IPC4_UAOL_ART_DIVIDER_ID 5 +#define SOF_IPC4_UAOL_SERVICE_INTERVAL_ID 6 + +/* PCI address of the xHCI controller the offloaded device is attached to */ +#define SOF_IPC4_UAOL_XHCI_BDF_BUS GENMASK(7, 0) +#define SOF_IPC4_UAOL_XHCI_BDF_DEVICE GENMASK(12, 8) +#define SOF_IPC4_UAOL_XHCI_BDF_FUNCTION GENMASK(15, 13) + +/* UAOL link and stream the gateway is bound to */ +#define SOF_IPC4_UAOL_CONFIG_LINK_IDX GENMASK(7, 0) +#define SOF_IPC4_UAOL_CONFIG_STREAM_IDX GENMASK(15, 8) +#define SOF_IPC4_UAOL_CONFIG_FEEDBACK_IDX GENMASK(23, 16) +#define SOF_IPC4_UAOL_CONFIG_FEEDBACK_PERIOD GENMASK(31, 24) + +/* Start offset of each stream within the UAOL FIFO */ +#define SOF_IPC4_UAOL_FIFO_SAO_TX0 GENMASK(11, 0) +#define SOF_IPC4_UAOL_FIFO_SAO_TX1 GENMASK(27, 16) +#define SOF_IPC4_UAOL_FIFO_SAO_RX0 GENMASK(11, 0) +#define SOF_IPC4_UAOL_FIFO_SAO_RX1 GENMASK(27, 16) + +/* Streams per direction the FIFO start offsets of the configuration describe */ +#define SOF_IPC4_UAOL_FIFO_SAO_STREAMS 2 + +/* The firmware rejects a start offset which is not aligned to the IOSF bus */ +#define SOF_IPC4_UAOL_FIFO_BUS_WIDTH 8 + +/* Identity of the offloaded USB endpoint */ +#define SOF_IPC4_UAOL_EP_INFO_DIRECTION BIT(0) +#define SOF_IPC4_UAOL_EP_INFO_NUMBER GENMASK(4, 1) +#define SOF_IPC4_UAOL_EP_INFO_SLOT GENMASK(12, 5) +#define SOF_IPC4_UAOL_EP_INFO_SPLIT BIT(13) +#define SOF_IPC4_UAOL_EP_INFO_HIGH_SPEED BIT(14) +#define SOF_IPC4_UAOL_EP_INFO_MAX_PACKET GENMASK(31, 16) + +/* Conversion of the xHCI global time to the DSP always running timer */ +#define SOF_IPC4_UAOL_ART_MULTIPLIER GENMASK(15, 0) +#define SOF_IPC4_UAOL_ART_DIVIDER GENMASK(31, 16) + +/** + * struct sof_ipc4_uaol_tlv - header of a UAOL gateway configuration TLV + * @type: one of the SOF_IPC4_UAOL_*_ID types + * @length: size of the value following this header, in bytes + */ +struct sof_ipc4_uaol_tlv { + u32 type; + u32 length; +} __packed; + +/** + * struct sof_ipc4_uaol_configuration_blob - UAOL gateway blob + * @xhci_bdf_tlv: header of the xHCI controller address + * @xhci_bdf: PCI address of the xHCI controller + * @config_tlv: header of the link and stream binding + * @config: UAOL link, stream and clock feedback binding + * @fifo_sao_tlv: header of the FIFO offsets + * @fifo_sao_tx: start offset of the two transmit streams + * @fifo_sao_rx: start offset of the two receive streams + * @ep_info_tlv: header of the data endpoint description + * @ep_info: identity of the offloaded data endpoint + * @feedback_ep_info_tlv: header of the feedback endpoint description + * @feedback_ep_info: identity of the clock feedback endpoint, 0 if there is none + * @art_divider_tlv: header of the timestamp conversion factors + * @art_divider: multiplier and divider of the xHCI to DSP timestamp conversion + * @service_interval_tlv: header of the service interval in microseconds + * @service_interval_us: service interval in microseconds, from the USB endpoint descriptor + * + * The firmware expects all of the TLVs to be present even when the values are + * left at zero. + */ +struct sof_ipc4_uaol_configuration_blob { + struct sof_ipc4_uaol_tlv xhci_bdf_tlv; + u32 xhci_bdf; + struct sof_ipc4_uaol_tlv config_tlv; + u32 config; + struct sof_ipc4_uaol_tlv fifo_sao_tlv; + u32 fifo_sao_tx; + u32 fifo_sao_rx; + struct sof_ipc4_uaol_tlv ep_info_tlv; + u32 ep_info; + struct sof_ipc4_uaol_tlv feedback_ep_info_tlv; + u32 feedback_ep_info; + struct sof_ipc4_uaol_tlv art_divider_tlv; + u32 art_divider; + struct sof_ipc4_uaol_tlv service_interval_tlv; + u32 service_interval_us; +} __packed; + +/* + * The gateway node id addresses a UAOL stream by the HDA link stream it uses. + * The mapping between the two is fixed in hardware. + */ +static inline int sof_ipc4_uaol_link_stream(unsigned int stream) +{ + static const u8 link_stream[] = { 13, 14, 15, 16, 29, 30 }; + + if (stream >= ARRAY_SIZE(link_stream)) + return -EINVAL; + + return link_stream[stream]; +} + +int sof_ipc4_uaol_get_stream_counts(struct snd_sof_dev *sdev, int link_idx, + unsigned int *playback, unsigned int *capture); + /** * struct sof_ipc4_copier - copier config data * @data: IPC copier data From 53231865d93847ea1972664eb64bb76aee292d73 Mon Sep 17 00:00:00 2001 From: Peter Ujfalusi Date: Thu, 5 Nov 2026 09:11:58 +0200 Subject: [PATCH 39/53] ASoC: SOF: Intel: hda-uaol: Configure the UAOL gateway The UAOL gateway configuration blob describes the USB endpoint behind the link, which is only known once the stream has been acquired. Complete the blob in hw_params with the xHCI address, the endpoint information, the sideband resource the endpoint was given and the endpoint's service interval. The resource number is also the UAOL stream, which selects the gateway node index. The service interval lets the firmware size the FIFO time slot to the endpoint's real cadence (1000 us for a standard isochronous endpoint, 125-500 us for high bandwidth ones) instead of assuming one. The feedback fields of the configuration are cleared when the acquired stream has no feedback endpoint, as they can be left over from a previously offloaded device. A widget freed on system suspend stays prepared, so on resume its IPC payload is not rebuilt and sof_ipc4_widget_setup() refreshes only the copier data and the DMA config TLVs; the gateway configuration in between would be sent as it was copied at prepare time. The stream is acquired again on resume, and the sideband resource, with it the UAOL stream index the blob carries, and the slot of the device need not come back the same. Refresh the gateway configuration from copier_config at setup as well. For the other DAI types it has not changed since prepare and the copy is the same. Signed-off-by: Peter Ujfalusi --- sound/soc/sof/intel/hda-uaol.c | 89 ++++++++++++++++++++++++++++++++-- sound/soc/sof/ipc4-topology.c | 12 +++-- 2 files changed, 95 insertions(+), 6 deletions(-) diff --git a/sound/soc/sof/intel/hda-uaol.c b/sound/soc/sof/intel/hda-uaol.c index e4325b317daa4a..6428717c6d22bd 100644 --- a/sound/soc/sof/intel/hda-uaol.c +++ b/sound/soc/sof/intel/hda-uaol.c @@ -9,6 +9,8 @@ // over the UAOL instead of the host. // +#include +#include #include #include #include @@ -475,6 +477,78 @@ static struct usb_device *hda_uaol_selected(struct sof_intel_hda_dev *hdev, return NULL; } +static u32 uaol_ep_info(const struct usb_offload_stream_hw *hw, + const struct usb_offload_ep_info *ep) +{ + return FIELD_PREP(SOF_IPC4_UAOL_EP_INFO_DIRECTION, + !!(ep->ep_addr & USB_DIR_IN)) | + FIELD_PREP(SOF_IPC4_UAOL_EP_INFO_NUMBER, + ep->ep_addr & USB_ENDPOINT_NUMBER_MASK) | + FIELD_PREP(SOF_IPC4_UAOL_EP_INFO_SLOT, hw->slot_id) | + FIELD_PREP(SOF_IPC4_UAOL_EP_INFO_SPLIT, hw->split) | + FIELD_PREP(SOF_IPC4_UAOL_EP_INFO_HIGH_SPEED, hw->high_speed) | + FIELD_PREP(SOF_IPC4_UAOL_EP_INFO_MAX_PACKET, ep->maxpacket); +} + +/* + * The gateway only knows which USB endpoint to drive once the device is + * acquired, so complete the configuration the topology set up. + */ +static int uaol_dai_set_endpoint(struct snd_soc_dai *cpu_dai, int direction, + const struct usb_offload_stream_hw *hw) +{ + struct snd_soc_dapm_widget *w = snd_soc_dai_get_widget(cpu_dai, direction); + struct sof_ipc4_uaol_configuration_blob *blob; + struct sof_ipc4_copier *ipc4_copier; + u32 stream = hw->data.resource - 1; + int link_stream; + + link_stream = sof_ipc4_uaol_link_stream(stream); + if (link_stream < 0) { + dev_err(cpu_dai->dev, "invalid UAOL stream %u\n", stream); + return link_stream; + } + + ipc4_copier = widget_to_copier(w); + blob = (struct sof_ipc4_uaol_configuration_blob *)ipc4_copier->copier_config; + + blob->config &= ~SOF_IPC4_UAOL_CONFIG_STREAM_IDX; + blob->config |= FIELD_PREP(SOF_IPC4_UAOL_CONFIG_STREAM_IDX, stream); + + ipc4_copier->data.gtw_cfg.node_id &= ~SOF_IPC4_NODE_INDEX_MASK; + ipc4_copier->data.gtw_cfg.node_id |= SOF_IPC4_NODE_INDEX(link_stream); + + blob->xhci_bdf = FIELD_PREP(SOF_IPC4_UAOL_XHCI_BDF_BUS, + PCI_BUS_NUM(hw->bus_devid)) | + FIELD_PREP(SOF_IPC4_UAOL_XHCI_BDF_DEVICE, + PCI_SLOT(hw->bus_devid)) | + FIELD_PREP(SOF_IPC4_UAOL_XHCI_BDF_FUNCTION, + PCI_FUNC(hw->bus_devid)); + + blob->ep_info = uaol_ep_info(hw, &hw->data); + + /* Service interval from the USB endpoint descriptor */ + blob->service_interval_us = hw->service_interval_us; + + /* the feedback fields are left over from the previously offloaded device */ + if (hw->has_feedback) { + blob->feedback_ep_info = uaol_ep_info(hw, &hw->feedback); + blob->config &= ~(SOF_IPC4_UAOL_CONFIG_FEEDBACK_IDX | + SOF_IPC4_UAOL_CONFIG_FEEDBACK_PERIOD); + blob->config |= FIELD_PREP(SOF_IPC4_UAOL_CONFIG_FEEDBACK_IDX, + hw->feedback.resource - 1) | + /* bInterval form, the firmware polls every 2^(n-1) (micro)frames */ + FIELD_PREP(SOF_IPC4_UAOL_CONFIG_FEEDBACK_PERIOD, + hw->feedback.interval); + } else { + blob->feedback_ep_info = 0; + blob->config &= ~(SOF_IPC4_UAOL_CONFIG_FEEDBACK_IDX | + SOF_IPC4_UAOL_CONFIG_FEEDBACK_PERIOD); + } + + return 0; +} + static void uaol_set_be_format(struct snd_pcm_hw_params *params, snd_pcm_format_t format) { struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT); @@ -487,9 +561,12 @@ static void uaol_dai_stop_stream(struct snd_pcm_substream *substream, struct hda_uaol_stream *stream); /* - * Take the device away from the host for as long as the DSP drives its - * endpoint. The stream is started from hw_params and, after a system - * suspend, from prepare, which is the only callback the resume path runs. + * Take the device away from the host and finish the gateway configuration + * with what the acquire reports. The two belong together: the sideband + * resource the endpoint lands on is what the stream index and the node + * index are derived from, and it need not come back the same. The stream + * is started from hw_params and, after a system suspend, from prepare, + * which is the only callback the resume path runs. */ static int uaol_dai_start_stream(struct snd_pcm_substream *substream, struct snd_pcm_hw_params *params, @@ -532,6 +609,12 @@ static int uaol_dai_start_stream(struct snd_pcm_substream *substream, stream->be_format = hw.format; uaol_set_be_format(params, hw.format); + ret = uaol_dai_set_endpoint(cpu_dai, direction, &hw); + if (ret) { + usb_offload_release_stream(hdev->uaol, udev, direction, hw.pcm_index); + return ret; + } + stream->udev = udev; stream->pcm_index = hw.pcm_index; stream->be = substream; diff --git a/sound/soc/sof/ipc4-topology.c b/sound/soc/sof/ipc4-topology.c index e54f23f3cca0ec..7bd7bed11372a7 100644 --- a/sound/soc/sof/ipc4-topology.c +++ b/sound/soc/sof/ipc4-topology.c @@ -3736,9 +3736,11 @@ static int sof_ipc4_widget_setup(struct snd_sof_dev *sdev, struct snd_sof_widget ipc_data = ipc4_copier->ipc_config_data; /* - * Refresh copier_data and dma_config_tlv in ipc_config_data. - * These may have been updated after ipc_prepare, e.g. when - * link DMA stream tags change after a suspend/resume cycle. + * Refresh copier_data, the gateway configuration and + * dma_config_tlv in ipc_config_data. These may have been + * updated after ipc_prepare, e.g. when link DMA stream tags + * change after a suspend/resume cycle, or when the DAI + * reconfigures its gateway for the resources it reacquired. * * copier_data->gtw_cfg.config_length does not include the * TLV size (it was restored after sof_ipc4_prepare_copier_module), @@ -3752,6 +3754,10 @@ static int sof_ipc4_widget_setup(struct snd_sof_dev *sdev, struct snd_sof_widget memcpy(ipc_data, copier_data, sizeof(*copier_data)); copier_data->gtw_cfg.config_length = gtw_cfg_config_length / 4; + if (gtw_cfg_config_length && ipc4_copier->copier_config) + memcpy(ipc_data + sizeof(*copier_data), + ipc4_copier->copier_config, gtw_cfg_config_length); + if (tlv_size) memcpy(ipc_data + sizeof(*copier_data) + gtw_cfg_config_length, &ipc4_copier->dma_config_tlv, tlv_size); From 217a99a085ac660907f17c1c52a35af438d40807 Mon Sep 17 00:00:00 2001 From: Peter Ujfalusi Date: Thu, 5 Nov 2026 09:12:18 +0200 Subject: [PATCH 40/53] ASoC: SOF: Intel: hda-pcm/uaol: Constrain UAOL frontends to the link FIFO The share of the link FIFO a UAOL stream is given is decided when the gateway is configured, and the rate, the channel count and the format together decide whether the stream fits in it. An overrun is not reported anywhere, the audio just comes out corrupted. The limit is a single byte rate, so it cuts across all three parameters at once: 192kHz stereo S32_LE needs exactly as much room as 48kHz 8 channel S32_LE. Add hardware rules for the three parameters to the frontend so that only the combinations which fit are offered. The real service interval is not known before a device's altsetting is resolved, so the rule assumes the shortest possible one (125 us) as a coarse pre-filter rather than capping high-bandwidth devices needlessly. Once the USB stream is acquired, the actual service interval is known: check the payload of two such intervals, rounded to whole sample frames, against the FIFO share and refuse the stream if it is larger. A payload which is exactly as large as the share fits. The frontend's backends are resolved before it is opened, and everything the core does to runtime->hw afterwards only narrows it, so the rules are added from the platform PCM open. The hook looks at the backends the frontend routes to and acts on the UAOL ones only; others whose capabilities are only known at runtime could be added to it. The limit is the FIFO share the layout computed for the gateway configuration gives a stream. Add sof_ipc4_uaol_get_fifo_size(), returning the bytes a stream of a direction can use on a link, so that the platform code and the gateway agree on the number. Signed-off-by: Peter Ujfalusi --- sound/soc/sof/intel/hda-pcm.c | 8 ++ sound/soc/sof/intel/hda-uaol.c | 214 +++++++++++++++++++++++++++++++++ sound/soc/sof/intel/hda.h | 4 + sound/soc/sof/ipc4-topology.c | 22 ++++ sound/soc/sof/ipc4-topology.h | 1 + 5 files changed, 249 insertions(+) diff --git a/sound/soc/sof/intel/hda-pcm.c b/sound/soc/sof/intel/hda-pcm.c index 877465b4ae635d..fd521546e190d4 100644 --- a/sound/soc/sof/intel/hda-pcm.c +++ b/sound/soc/sof/intel/hda-pcm.c @@ -349,6 +349,14 @@ int hda_dsp_pcm_open(struct snd_sof_dev *sdev, !spcm->stream[substream->stream].pause_supported)) runtime->hw.info &= ~SNDRV_PCM_INFO_PAUSE; + /* + * The backends this frontend can reach are resolved before it is opened, + * and everything the core does to runtime->hw afterwards only narrows + * it, so this is the place to bring in a capability which is only known + * at runtime, like what a UAOL backend can carry. + */ + hda_apply_constraint_from_be(sdev, substream); + if (hda_always_enable_dmi_l1 || direction == SNDRV_PCM_STREAM_PLAYBACK || spcm->stream[substream->stream].d0i3_compatible) diff --git a/sound/soc/sof/intel/hda-uaol.c b/sound/soc/sof/intel/hda-uaol.c index 6428717c6d22bd..de68618c333803 100644 --- a/sound/soc/sof/intel/hda-uaol.c +++ b/sound/soc/sof/intel/hda-uaol.c @@ -477,6 +477,153 @@ static struct usb_device *hda_uaol_selected(struct sof_intel_hda_dev *hdev, return NULL; } +/* + * The gateway stages a service interval worth of payload in the link FIFO, so + * the space a stream takes is its byte rate per interval. Two intervals have to + * fit for the DSP to be able to refill the FIFO while the link drains it. + */ +#define SOF_UAOL_FIFO_INTERVALS 2 + +/* + * The gateway is programmed with the valid bits of the format and the link + * packs the samples to that width, so the container is not what occupies the + * FIFO. S24_LE is 3 bytes: the HDA DMA only knows a 32 bit container, of which + * it takes the 24 MSBs and drops the LSB byte. Only a full 32 bit format + * takes 4 bytes. + */ +static unsigned int uaol_sample_bytes(snd_pcm_format_t format) +{ + int width = snd_pcm_format_width(format); + + return width > 0 ? DIV_ROUND_UP(width, BITS_PER_BYTE) : 0; +} + +static unsigned int uaol_min_sample_bytes(const struct snd_mask *formats) +{ + unsigned int min_bytes = UINT_MAX; + snd_pcm_format_t format; + + pcm_for_each_format(format) { + unsigned int bytes; + + if (!snd_mask_test_format(formats, format)) + continue; + + bytes = uaol_sample_bytes(format); + if (bytes) + min_bytes = min(min_bytes, bytes); + } + + return min_bytes == UINT_MAX ? 0 : min_bytes; +} + +/* + * The budget is one dimensional in the byte rate, so the bound it puts on any + * one of the three parameters depends on the other two. Taking the smallest + * value the other two can still take keeps each bound the loosest one that is + * valid, and the refining converges on the combinations that fit. + */ +static int uaol_rule_rate_channels(struct snd_pcm_hw_params *params, + struct snd_pcm_hw_rule *rule) +{ + /* the product of the two is what the budget bounds, so one limits the other */ + int other = rule->var == SNDRV_PCM_HW_PARAM_RATE ? SNDRV_PCM_HW_PARAM_CHANNELS : + SNDRV_PCM_HW_PARAM_RATE; + struct snd_interval *other_i = hw_param_interval(params, other); + unsigned int budget = (uintptr_t)rule->private; + unsigned int bytes; + struct snd_interval t = { .integer = 1 }; + + bytes = uaol_min_sample_bytes(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT)); + if (!bytes || !other_i->min) + return 0; + + t.max = budget / (other_i->min * bytes); + + return snd_interval_refine(hw_param_interval(params, rule->var), &t); +} + +static int uaol_rule_format(struct snd_pcm_hw_params *params, struct snd_pcm_hw_rule *rule) +{ + struct snd_interval *channels = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS); + struct snd_interval *rate = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE); + unsigned int budget = (uintptr_t)rule->private; + snd_pcm_format_t format; + struct snd_mask m; + + if (!rate->min || !channels->min) + return 0; + + snd_mask_none(&m); + pcm_for_each_format(format) { + unsigned int bytes = uaol_sample_bytes(format); + + if (bytes && (u64)channels->min * bytes * rate->min <= budget) + snd_mask_set_format(&m, format); + } + + return snd_mask_refine(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), &m); +} + +/* + * The share of the link FIFO a stream gets is fixed when the gateway is + * configured, and the rate, the channel count and the format together decide + * whether what the stream produces fits in it. Overrunning it is not reported + * anywhere, the audio simply comes out corrupted, so keep the combinations that + * do not fit off the table. + * + * This is a property of the link and not of the connected device, hence it is + * applied whether or not there is a device to narrow to. + */ +static void hda_uaol_set_fifo_constraint(struct snd_sof_dev *sdev, + struct snd_soc_dai *be_dai, + struct snd_pcm_substream *substream) +{ + struct snd_soc_dapm_widget *w = snd_soc_dai_get_widget(be_dai, substream->stream); + struct snd_pcm_runtime *runtime = substream->runtime; + struct sof_ipc4_copier *ipc4_copier; + unsigned int budget; + int size; + + if (!w) + return; + + ipc4_copier = widget_to_copier(w); + + size = sof_ipc4_uaol_get_fifo_size(sdev, ipc4_copier->dai_index, substream->stream); + if (size <= 0) + return; + + /* + * The real service interval is only known once a device's altsetting is + * actually resolved, so this early negotiation-time rule can only offer a + * coarse pre-filter: assume the shortest possible interval (125 us), which + * keeps high-bandwidth devices from being needlessly capped here. + */ + budget = size / SOF_UAOL_FIFO_INTERVALS * (USEC_PER_SEC / 125); + + snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, + uaol_rule_rate_channels, (void *)(uintptr_t)budget, + SNDRV_PCM_HW_PARAM_CHANNELS, SNDRV_PCM_HW_PARAM_FORMAT, -1); + snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS, + uaol_rule_rate_channels, (void *)(uintptr_t)budget, + SNDRV_PCM_HW_PARAM_RATE, SNDRV_PCM_HW_PARAM_FORMAT, -1); + snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT, + uaol_rule_format, (void *)(uintptr_t)budget, + SNDRV_PCM_HW_PARAM_RATE, SNDRV_PCM_HW_PARAM_CHANNELS, -1); +} + +/* + * A UAOL frontend advertises what the topology configured the link for. Narrow + * it to what the link can carry. + */ +static void hda_uaol_set_constraint(struct snd_sof_dev *sdev, + struct snd_soc_dai *be_dai, + struct snd_pcm_substream *substream) +{ + hda_uaol_set_fifo_constraint(sdev, be_dai, substream); +} + static u32 uaol_ep_info(const struct usb_offload_stream_hw *hw, const struct usb_offload_ep_info *ep) { @@ -560,6 +707,47 @@ static void uaol_set_be_format(struct snd_pcm_hw_params *params, snd_pcm_format_ static void uaol_dai_stop_stream(struct snd_pcm_substream *substream, struct hda_uaol_stream *stream); +/* + * The negotiation-time rule assumes the shortest service interval. Once the + * device's endpoint is known, check the payload of SOF_UAOL_FIFO_INTERVALS + * actual service intervals, in whole sample frames, against the FIFO share. + * A stream taking exactly the whole share is valid, e.g. 384 kHz stereo on a + * 500 us endpoint, only a payload that is larger than the share overruns it. + */ +static int uaol_check_fifo_fit(struct snd_sof_dev *sdev, struct snd_soc_dai *cpu_dai, + struct snd_pcm_substream *substream, + struct snd_pcm_hw_params *params, + const struct usb_offload_stream_hw *hw) +{ + struct snd_soc_dapm_widget *w = snd_soc_dai_get_widget(cpu_dai, substream->stream); + struct sof_ipc4_copier *ipc4_copier; + u64 frames, bytes; + int size; + + if (!w || !hw->service_interval_us) + return 0; + + ipc4_copier = widget_to_copier(w); + + size = sof_ipc4_uaol_get_fifo_size(sdev, ipc4_copier->dai_index, substream->stream); + if (size <= 0) + return 0; + + frames = DIV_ROUND_UP_ULL((u64)params_rate(params) * hw->service_interval_us, + USEC_PER_SEC); + bytes = frames * params_channels(params) * + uaol_sample_bytes(params_format(params)) * SOF_UAOL_FIFO_INTERVALS; + + if (bytes > size) { + dev_err(cpu_dai->dev, + "stream needs %llu bytes of link FIFO, only %d available\n", + bytes, size); + return -EINVAL; + } + + return 0; +} + /* * Take the device away from the host and finish the gateway configuration * with what the acquire reports. The two belong together: the sideband @@ -609,6 +797,12 @@ static int uaol_dai_start_stream(struct snd_pcm_substream *substream, stream->be_format = hw.format; uaol_set_be_format(params, hw.format); + ret = uaol_check_fifo_fit(sdev, cpu_dai, substream, params, &hw); + if (ret) { + usb_offload_release_stream(hdev->uaol, udev, direction, hw.pcm_index); + return ret; + } + ret = uaol_dai_set_endpoint(cpu_dai, direction, &hw); if (ret) { usb_offload_release_stream(hdev->uaol, udev, direction, hw.pcm_index); @@ -810,6 +1004,26 @@ static const struct snd_soc_dai_ops uaol_dai_ops = { .prepare = uaol_dai_prepare, }; +/* + * Called for every frontend the platform component opens, so the backends it + * routes to are what says whether there is anything to do here. + */ +void hda_apply_constraint_from_be(struct snd_sof_dev *sdev, + struct snd_pcm_substream *substream) +{ + struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream); + struct snd_soc_dpcm *dpcm; + + for_each_dpcm_be(rtd, substream->stream, dpcm) { + struct snd_soc_dai *be_dai = snd_soc_rtd_to_cpu(dpcm->be, 0); + + if (be_dai->driver->ops == &uaol_dai_ops) { + hda_uaol_set_constraint(sdev, be_dai, substream); + return; + } + } +} + void hda_uaol_set_dai_drv_ops(struct snd_sof_dev *sdev, struct snd_sof_dsp_ops *ops) { const struct sof_intel_dsp_desc *chip; diff --git a/sound/soc/sof/intel/hda.h b/sound/soc/sof/intel/hda.h index b6ab362efd2588..30c7f421349b89 100644 --- a/sound/soc/sof/intel/hda.h +++ b/sound/soc/sof/intel/hda.h @@ -1218,12 +1218,16 @@ int hda_dai_trigger(struct snd_pcm_substream *substream, int cmd, #if IS_ENABLED(CONFIG_SND_SOC_SOF_INTEL_USB_OFFLOAD) void hda_uaol_set_dai_drv_ops(struct snd_sof_dev *sdev, struct snd_sof_dsp_ops *ops); +void hda_apply_constraint_from_be(struct snd_sof_dev *sdev, + struct snd_pcm_substream *substream); int hda_uaol_notify_connect(struct device *dev, struct usb_device *udev, const struct usb_offload_usb_dev_info *id); int hda_uaol_notify_disconnect(struct device *dev, struct usb_device *udev); #else static inline void hda_uaol_set_dai_drv_ops(struct snd_sof_dev *sdev, struct snd_sof_dsp_ops *ops) { } +static inline void hda_apply_constraint_from_be(struct snd_sof_dev *sdev, + struct snd_pcm_substream *substream) { } #endif #endif diff --git a/sound/soc/sof/ipc4-topology.c b/sound/soc/sof/ipc4-topology.c index 7bd7bed11372a7..cc86f6861ec430 100644 --- a/sound/soc/sof/ipc4-topology.c +++ b/sound/soc/sof/ipc4-topology.c @@ -915,6 +915,28 @@ static int sof_ipc4_uaol_get_fifo_layout(struct snd_sof_dev *sdev, int link_idx, return 0; } +/** + * sof_ipc4_uaol_get_fifo_size - space in the link FIFO for one UAOL stream + * @sdev: SOF device + * @link_idx: index of the UAOL link + * @direction: SNDRV_PCM_STREAM_PLAYBACK or SNDRV_PCM_STREAM_CAPTURE + * + * Return: the number of bytes a stream of @direction can use on the link, or a + * negative error code. + */ +int sof_ipc4_uaol_get_fifo_size(struct snd_sof_dev *sdev, int link_idx, int direction) +{ + struct sof_ipc4_uaol_fifo_layout layout; + int ret; + + ret = sof_ipc4_uaol_get_fifo_layout(sdev, link_idx, &layout); + if (ret) + return ret; + + return direction == SNDRV_PCM_STREAM_PLAYBACK ? layout.tx_size : layout.rx_size; +} +EXPORT_SYMBOL(sof_ipc4_uaol_get_fifo_size); + static int sof_ipc4_widget_setup_uaol(struct snd_sof_dev *sdev, struct snd_sof_widget *swidget, struct sof_ipc4_copier *ipc4_copier) diff --git a/sound/soc/sof/ipc4-topology.h b/sound/soc/sof/ipc4-topology.h index 3de9fbbad307bd..330023c1dacf4b 100644 --- a/sound/soc/sof/ipc4-topology.h +++ b/sound/soc/sof/ipc4-topology.h @@ -437,6 +437,7 @@ static inline int sof_ipc4_uaol_link_stream(unsigned int stream) return link_stream[stream]; } +int sof_ipc4_uaol_get_fifo_size(struct snd_sof_dev *sdev, int link_idx, int direction); int sof_ipc4_uaol_get_stream_counts(struct snd_sof_dev *sdev, int link_idx, unsigned int *playback, unsigned int *capture); From 550e47bcbeb19d29580783e203be7519f0f1eec1 Mon Sep 17 00:00:00 2001 From: Peter Ujfalusi Date: Wed, 4 Nov 2026 15:04:39 +0200 Subject: [PATCH 41/53] ASoC: SOF: Intel: hda-uaol: Constrain UAOL frontends to the USB device The capabilities of the connected device cannot be coded into the topology file, the user can connect a wide range of devices. The topology provides a broad set of formats, rates and channels for the UAOL PCM, which is then narrowed down to what the actual USB device supports. For example: tplg: S16_LE/S24_LE/S32_LE, 48K/96K, stereo, headset: S16_LE only, 8-192K, stereo/2ch result on PCM: S16_LE (S24_LE/S32_LE dropped), 48K/96K, stereo The device is brought in as hardware rules and not as plain ranges, because what one parameter can be depends on the other two: a device can have a multichannel stream at one rate and a stereo one at another, and the ranges alone would offer the combination of the two that it does not have. The rules resolve the selected device on every call, so a device connected, removed or selected while a PCM sits open is picked up on the next negotiation. Each rule also depends on the parameter it refines, so that a request which changes only that parameter is checked against the device as well. A frontend which shares the backend with one that has it open, the deep buffer, can only run what the first one opened the backend with. Add rules which clone those parameters as the only ones allowed to the second frontend, so that userspace negotiates against the running stream. They read what the first frontend opened the backend with when they are evaluated, so they allow everything while the backend is idle. Signed-off-by: Peter Ujfalusi --- sound/soc/sof/intel/hda-uaol.c | 104 ++++++++++++++++++++++++++++++++- 1 file changed, 102 insertions(+), 2 deletions(-) diff --git a/sound/soc/sof/intel/hda-uaol.c b/sound/soc/sof/intel/hda-uaol.c index de68618c333803..df32c07aec3541 100644 --- a/sound/soc/sof/intel/hda-uaol.c +++ b/sound/soc/sof/intel/hda-uaol.c @@ -484,6 +484,12 @@ static struct usb_device *hda_uaol_selected(struct sof_intel_hda_dev *hdev, */ #define SOF_UAOL_FIFO_INTERVALS 2 +static const int uaol_params[] = { + SNDRV_PCM_HW_PARAM_RATE, + SNDRV_PCM_HW_PARAM_CHANNELS, + SNDRV_PCM_HW_PARAM_FORMAT, +}; + /* * The gateway is programmed with the valid bits of the format and the link * packs the samples to that width, so the container is not what occupies the @@ -614,14 +620,108 @@ static void hda_uaol_set_fifo_constraint(struct snd_sof_dev *sdev, } /* - * A UAOL frontend advertises what the topology configured the link for. Narrow - * it to what the link can carry. + * The device is resolved on every call rather than once at open, so that a + * device connected or removed, or the route kcontrol changing which device + * is selected, while a frontend sits open is picked up on the next + * negotiation. + */ +static int uaol_rule_device(struct snd_pcm_hw_params *params, + struct snd_pcm_hw_rule *rule) +{ + struct hda_uaol_stream *stream = rule->private; + struct sof_intel_hda_dev *hdev = stream->hdev; + struct usb_device *udev; + int pcm_index; + + udev = hda_uaol_selected(hdev, stream->direction, stream->route, &pcm_index); + if (!udev) + return 0; + + return usb_offload_refine_hw_params(hdev->uaol, udev, stream->direction, + pcm_index, rule->var, params); +} + +/* + * A frontend sharing the backend with one which has it open, the deep buffer, + * can only run what the first one opened the backend with: the two share the + * endpoint and the mixer. Clone those parameters as the only ones allowed, so + * userspace negotiates against the running stream and not against what the + * topology offers. + * + * What the first frontend opened the backend with is only known once its + * hw_params ran, hence rules and not plain constraints. + */ +static int uaol_rule_shared(struct snd_pcm_hw_params *params, + struct snd_pcm_hw_rule *rule) +{ + struct hda_uaol_stream *stream = rule->private; + struct snd_interval t = { .integer = 1 }; + struct snd_mask m; + + /* pairs with the release which publishes the first frontend's parameters */ + if (!smp_load_acquire(&stream->params_valid)) + return 0; + + switch (rule->var) { + case SNDRV_PCM_HW_PARAM_RATE: + t.min = stream->rate; + t.max = stream->rate; + break; + case SNDRV_PCM_HW_PARAM_CHANNELS: + t.min = stream->channels; + t.max = stream->channels; + break; + default: + snd_mask_none(&m); + snd_mask_set_format(&m, stream->format); + + return snd_mask_refine(hw_param_mask(params, rule->var), &m); + } + + return snd_interval_refine(hw_param_interval(params, rule->var), &t); +} + +/* + * A UAOL frontend advertises what the topology configured the link for, which + * has nothing to do with the device that happens to be connected. Narrow it to + * what that device can be run at, so that userspace negotiates against the + * device instead of finding out at hw_params time that its choice is + * impossible. + * + * Rules and not plain constraints, because what one parameter can be depends on + * the other two: a device can well have a multichannel stream at one rate and a + * stereo one at another, and the ranges alone would offer the combination of the + * two that it does not have. Each rule reports what the parameters still allow, + * so the refining settles on a combination a single alternate setting can serve, + * which is what the acquire in uaol_dai_start_stream() goes looking for. The + * rules are given the backend they run for, which lives as long as the card. */ static void hda_uaol_set_constraint(struct snd_sof_dev *sdev, struct snd_soc_dai *be_dai, struct snd_pcm_substream *substream) { + struct sof_intel_hda_dev *hdev = sdev->pdata->hw_pdata; + struct snd_pcm_runtime *runtime = substream->runtime; + struct hda_uaol_stream *ctx; + int i; + hda_uaol_set_fifo_constraint(sdev, be_dai, substream); + + if (!hdev->uaol) + return; + + ctx = hda_uaol_stream_get(hdev, be_dai); + if (!ctx) + return; + + for (i = 0; i < ARRAY_SIZE(uaol_params); i++) { + /* what each of the three can be depends on all of them */ + snd_pcm_hw_rule_add(runtime, 0, uaol_params[i], uaol_rule_device, ctx, + SNDRV_PCM_HW_PARAM_RATE, SNDRV_PCM_HW_PARAM_CHANNELS, + SNDRV_PCM_HW_PARAM_FORMAT, -1); + snd_pcm_hw_rule_add(runtime, 0, uaol_params[i], uaol_rule_shared, ctx, + uaol_params[i], -1); + } } static u32 uaol_ep_info(const struct usb_offload_stream_hw *hw, From cad3eb1278fb1668fd569f67e30fa90471bd8fc1 Mon Sep 17 00:00:00 2001 From: Peter Ujfalusi Date: Wed, 4 Nov 2026 15:07:45 +0200 Subject: [PATCH 42/53] ASoC: SOF: Intel: hda-uaol: Supply a link DMA for the UAOL feedback The firmware reads a USB device's clock feedback endpoint through a link DMA of its own, which it expects as a second DMA config TLV in the UAOL gateway configuration (host_dma_config[1]). The driver only ever supplied the audio link DMA, so the firmware ran every playback stream without rate feedback. When the acquired USB stream has a feedback endpoint, allocate a second link DMA stream on the UAOL link, in the capture direction, and describe it in dma_config_tlv[1] the same way the audio one is described in dma_config_tlv[0]. It belongs to the playback substream but is neither the DAI's DMA data nor a stream of its own, so it is kept with the backend's stream state, is not registered as the link_substream, and is run and stopped from the UAOL trigger alongside the audio stream. It is released, and its TLV cleared, together with the USB stream, so that a following device without feedback does not inherit it. hda_link_stream_assign() gets the stream direction as a parameter for that, and the release of a link stream is factored out so that it can be undone without a DAI DMA data pointer. Co-developed-by: Serhiy Katsyuba Signed-off-by: Serhiy Katsyuba Signed-off-by: Peter Ujfalusi --- sound/soc/sof/intel/hda-dai-ops.c | 91 +++++++++++++----- sound/soc/sof/intel/hda-uaol.c | 149 ++++++++++++++++++++++++++---- sound/soc/sof/intel/hda.h | 8 ++ 3 files changed, 210 insertions(+), 38 deletions(-) diff --git a/sound/soc/sof/intel/hda-dai-ops.c b/sound/soc/sof/intel/hda-dai-ops.c index 0205fb40db6324..47fd81fa1ee7a4 100644 --- a/sound/soc/sof/intel/hda-dai-ops.c +++ b/sound/soc/sof/intel/hda-dai-ops.c @@ -43,7 +43,7 @@ static bool hda_check_fes(struct snd_soc_pcm_runtime *rtd, static struct hdac_ext_stream * hda_link_stream_assign(struct hdac_bus *bus, struct snd_pcm_substream *substream, - enum hda_bus_ml_link_type link_type) + enum hda_bus_ml_link_type link_type, int stream_dir) { struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream); struct sof_intel_hda_dev *sof_hda = bus_to_sof_hda(bus); @@ -52,7 +52,6 @@ hda_link_stream_assign(struct hdac_bus *bus, struct snd_pcm_substream *substream struct snd_sof_dev *sdev; struct hdac_ext_stream *res = NULL; struct hdac_stream *hstream = NULL; - int stream_dir = substream->stream; bool is_multi = link_type == HDA_BUS_ML_LINK_HDA || link_type == HDA_BUS_ML_LINK_UAOL; bool is_play = stream_dir == SNDRV_PCM_STREAM_PLAYBACK; bool is_sdw = link_type == HDA_BUS_ML_LINK_SDW; @@ -92,7 +91,7 @@ hda_link_stream_assign(struct hdac_bus *bus, struct snd_pcm_substream *substream list_for_each_entry(hstream, &bus->stream_list, list) { struct hdac_ext_stream *hext_stream = stream_to_hdac_ext_stream(hstream); - if (hstream->direction != substream->stream) + if (hstream->direction != stream_dir) continue; hda_stream = hstream_to_sof_hda_stream(hext_stream); @@ -160,6 +159,71 @@ hda_link_stream_assign(struct hdac_bus *bus, struct snd_pcm_substream *substream return res; } +/* Undo hda_link_stream_assign(); the stream index is already out of use */ +static void hda_link_stream_release(struct snd_sof_dev *sdev, + struct hdac_ext_stream *hext_stream, int dir) +{ + struct sof_intel_hda_dev *sof_hda = sdev->pdata->hw_pdata; + struct hdac_bus *bus = sof_to_bus(sdev); + unsigned int stream_idx = hext_stream->hstream.stream_tag - 1; + + /* + * Drop the stream index from the per-direction active concurrency masks. + * The two masks are mutually exclusive for a given stream/direction + * (and a stream of the SSP/DMIC kind appears in neither), so a blind + * clear of both is safe and lets us avoid having to remember the + * link type at allocation time. + */ + scoped_guard(spinlock_irq, &bus->reg_lock) { + sof_hda->link_dma_active_sdw_mask[dir] &= ~BIT(stream_idx); + sof_hda->link_dma_active_multi_mask[dir] &= ~BIT(stream_idx); + } + + snd_hdac_ext_stream_release(hext_stream, HDAC_EXT_STREAM_TYPE_LINK); +} + +#if IS_ENABLED(CONFIG_SND_SOC_SOF_INTEL_USB_OFFLOAD) + +/* + * A UAOL playback stream with an explicit feedback endpoint needs a second + * link DMA, in the capture direction, for the DSP to read the feedback + * through. It belongs to the playback substream but is not the DAI's DMA + * data and is not a stream of its own, so it is not registered as the + * link_substream either - hda_dai_suspend() has nothing to do with it. + */ +struct hdac_ext_stream *hda_uaol_assign_fb_link_stream(struct snd_sof_dev *sdev, + struct snd_pcm_substream *substream, + unsigned int format_val) +{ + struct hdac_ext_stream *hext_stream; + + hext_stream = hda_link_stream_assign(sof_to_bus(sdev), substream, + HDA_BUS_ML_LINK_UAOL, SNDRV_PCM_STREAM_CAPTURE); + if (!hext_stream) + return NULL; + + hext_stream->link_substream = NULL; + + snd_hdac_ext_stream_reset(hext_stream); + snd_hdac_ext_stream_setup(hext_stream, format_val); + + return hext_stream; +} + +void hda_uaol_release_fb_link_stream(struct snd_sof_dev *sdev, + struct hdac_ext_stream *hext_stream) +{ + struct sof_intel_hda_stream *hda_stream = hstream_to_sof_hda_stream(hext_stream); + + snd_hdac_ext_stream_clear(hext_stream); + hda_link_stream_release(sdev, hext_stream, SNDRV_PCM_STREAM_CAPTURE); + + /* free the host DMA channel reserved by the hostless stream */ + hda_stream->host_reserved = 0; +} + +#endif /* IS_ENABLED(CONFIG_SND_SOC_SOF_INTEL_USB_OFFLOAD) */ + static struct hdac_ext_stream *hda_get_hext_stream(struct snd_sof_dev *sdev, struct snd_soc_dai *cpu_dai, struct snd_pcm_substream *substream) @@ -195,7 +259,8 @@ static struct hdac_ext_stream *hda_assign_hext_stream(struct snd_sof_dev *sdev, struct hdac_ext_stream *hext_stream; enum hda_bus_ml_link_type link_type = hda_bus_ml_link_get_type(hlink); - hext_stream = hda_link_stream_assign(sof_to_bus(sdev), substream, link_type); + hext_stream = hda_link_stream_assign(sof_to_bus(sdev), substream, link_type, + substream->stream); if (!hext_stream) return NULL; @@ -208,25 +273,9 @@ static void hda_release_hext_stream(struct snd_sof_dev *sdev, struct snd_soc_dai struct snd_pcm_substream *substream) { struct hdac_ext_stream *hext_stream = hda_get_hext_stream(sdev, cpu_dai, substream); - struct sof_intel_hda_dev *sof_hda = sdev->pdata->hw_pdata; - struct hdac_bus *bus = sof_to_bus(sdev); - int dir = substream->stream; - unsigned int stream_idx = hext_stream->hstream.stream_tag - 1; - - /* - * Drop the stream index from the per-direction active concurrency masks. - * The two masks are mutually exclusive for a given stream/direction - * (and a stream of the SSP/DMIC kind appears in neither), so a blind - * clear of both is safe and lets us avoid having to remember the - * link type at allocation time. - */ - scoped_guard(spinlock_irq, &bus->reg_lock) { - sof_hda->link_dma_active_sdw_mask[dir] &= ~BIT(stream_idx); - sof_hda->link_dma_active_multi_mask[dir] &= ~BIT(stream_idx); - } snd_soc_dai_set_dma_data(cpu_dai, substream, NULL); - snd_hdac_ext_stream_release(hext_stream, HDAC_EXT_STREAM_TYPE_LINK); + hda_link_stream_release(sdev, hext_stream, substream->stream); } static void hda_setup_hext_stream(struct snd_sof_dev *sdev, struct hdac_ext_stream *hext_stream, diff --git a/sound/soc/sof/intel/hda-uaol.c b/sound/soc/sof/intel/hda-uaol.c index df32c07aec3541..9555dc0de6ca66 100644 --- a/sound/soc/sof/intel/hda-uaol.c +++ b/sound/soc/sof/intel/hda-uaol.c @@ -796,14 +796,6 @@ static int uaol_dai_set_endpoint(struct snd_soc_dai *cpu_dai, int direction, return 0; } -static void uaol_set_be_format(struct snd_pcm_hw_params *params, snd_pcm_format_t format) -{ - struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT); - - snd_mask_none(fmt); - snd_mask_set_format(fmt, format); -} - static void uaol_dai_stop_stream(struct snd_pcm_substream *substream, struct hda_uaol_stream *stream); @@ -848,6 +840,93 @@ static int uaol_check_fifo_fit(struct snd_sof_dev *sdev, struct snd_soc_dai *cpu return 0; } +/* + * Index of the DMA config TLV describing the feedback link DMA. A UAOL + * backend has a single CPU DAI, so the audio link DMA is always TLV 0 and + * the firmware takes the second one as the feedback (host_dma_config[1]). + */ +#define SOF_UAOL_FB_DMA_TLV_IDX 1 + +static void uaol_dai_release_feedback(struct snd_sof_dev *sdev, + struct hda_uaol_stream *stream) +{ + struct snd_soc_dapm_widget *w = snd_soc_dai_get_widget(stream->dai, stream->direction); + struct sof_ipc4_copier *ipc4_copier = widget_to_copier(w); + + /* a stale TLV would be sent along with the next, feedback-less, device */ + memset(&ipc4_copier->dma_config_tlv[SOF_UAOL_FB_DMA_TLV_IDX], 0, + sizeof(ipc4_copier->dma_config_tlv[SOF_UAOL_FB_DMA_TLV_IDX])); + + if (!stream->fb_hext_stream) + return; + + hda_uaol_release_fb_link_stream(sdev, stream->fb_hext_stream); + stream->fb_hext_stream = NULL; +} + +/* + * The feedback endpoint is read by the DSP through a link DMA of its own, in + * the capture direction. Its stream tag is what the firmware needs to bind + * the UAOL feedback stream to, handed over as a second DMA config TLV after + * the one of the audio link DMA. + */ +static int uaol_dai_setup_feedback(struct snd_pcm_substream *substream, + struct snd_pcm_hw_params *params, + struct hda_uaol_stream *stream) +{ + struct snd_soc_dai *cpu_dai = stream->dai; + struct snd_soc_dapm_widget *w = snd_soc_dai_get_widget(cpu_dai, substream->stream); + struct snd_sof_dev *sdev = dai_to_sdev(substream, cpu_dai); + struct sof_ipc4_copier *ipc4_copier = widget_to_copier(w); + struct sof_ipc4_dma_config_tlv *dma_config_tlv; + struct sof_ipc4_dma_config *dma_config; + struct hdac_ext_stream *hext_stream; + unsigned int format_val; + unsigned int format_bits; + int stream_tag; + + if (sdev->dspless_mode_selected) + return 0; + + /* one 32-bit feedback value per service interval */ + format_bits = snd_hdac_stream_format_bits(SNDRV_PCM_FORMAT_S32_LE, + SNDRV_PCM_SUBFORMAT_STD, 32); + format_val = snd_hdac_stream_format(1, format_bits, params_rate(params)); + + hext_stream = hda_uaol_assign_fb_link_stream(sdev, substream, format_val); + if (!hext_stream) { + dev_err(cpu_dai->dev, "no link DMA available for the USB feedback\n"); + return -EBUSY; + } + + stream->fb_hext_stream = hext_stream; + stream_tag = hdac_stream(hext_stream)->stream_tag; + + dma_config_tlv = &ipc4_copier->dma_config_tlv[SOF_UAOL_FB_DMA_TLV_IDX]; + dma_config_tlv->type = SOF_IPC4_GTW_DMA_CONFIG_ID; + dma_config_tlv->length = sizeof(dma_config_tlv->dma_config); + + dma_config = &dma_config_tlv->dma_config; + dma_config->dma_method = SOF_IPC4_DMA_METHOD_HDA; + dma_config->pre_allocated_by_host = 1; + dma_config->dma_channel_id = stream_tag - 1; + dma_config->stream_id = stream_tag; + dma_config->dma_stream_channel_map.device_count = 1; + dma_config->dma_priv_config_size = 0; + + dev_dbg(cpu_dai->dev, "USB feedback on link DMA stream %d\n", stream_tag); + + return 0; +} + +static void uaol_set_be_format(struct snd_pcm_hw_params *params, snd_pcm_format_t format) +{ + struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT); + + snd_mask_none(fmt); + snd_mask_set_format(fmt, format); +} + /* * Take the device away from the host and finish the gateway configuration * with what the acquire reports. The two belong together: the sideband @@ -898,15 +977,17 @@ static int uaol_dai_start_stream(struct snd_pcm_substream *substream, uaol_set_be_format(params, hw.format); ret = uaol_check_fifo_fit(sdev, cpu_dai, substream, params, &hw); - if (ret) { - usb_offload_release_stream(hdev->uaol, udev, direction, hw.pcm_index); - return ret; - } + if (ret) + goto err_release; ret = uaol_dai_set_endpoint(cpu_dai, direction, &hw); - if (ret) { - usb_offload_release_stream(hdev->uaol, udev, direction, hw.pcm_index); - return ret; + if (ret) + goto err_release; + + if (hw.has_feedback) { + ret = uaol_dai_setup_feedback(substream, params, stream); + if (ret) + goto err_release; } stream->udev = udev; @@ -927,6 +1008,10 @@ static int uaol_dai_start_stream(struct snd_pcm_substream *substream, } return 0; + +err_release: + usb_offload_release_stream(hdev->uaol, udev, direction, hw.pcm_index); + return ret; } static void uaol_dai_stop_stream(struct snd_pcm_substream *substream, @@ -936,6 +1021,10 @@ static void uaol_dai_stop_stream(struct snd_pcm_substream *substream, struct sof_intel_hda_dev *hdev = sdev->pdata->hw_pdata; struct usb_device *udev = stream->udev; + /* also after an unplug, which takes only the USB side away */ + if (stream->fb_hext_stream) + uaol_dai_release_feedback(sdev, stream); + if (!udev) return; @@ -1082,17 +1171,43 @@ static int uaol_dai_prepare(struct snd_pcm_substream *substream, /* * The backend is triggered on the way into a system suspend, which is the * only notice this driver gets that the stream will have to be reacquired. + * The feedback link DMA is not the DAI's DMA data, so the common trigger + * does not know about it and it is run from here, alongside the audio one. */ static int uaol_dai_trigger(struct snd_pcm_substream *substream, int cmd, struct snd_soc_dai *cpu_dai) { struct snd_sof_dev *sdev = dai_to_sdev(substream, cpu_dai); struct hda_uaol_stream *stream = hda_uaol_stream_get(sdev->pdata->hw_pdata, cpu_dai); + struct hdac_ext_stream *fb_hext_stream = NULL; + int ret; - if (stream && cmd == SNDRV_PCM_TRIGGER_SUSPEND) + if (!stream) + return hda_dai_trigger(substream, cmd, cpu_dai); + + if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) + fb_hext_stream = stream->fb_hext_stream; + + if (cmd == SNDRV_PCM_TRIGGER_SUSPEND) stream->suspended = true; - return hda_dai_trigger(substream, cmd, cpu_dai); + if (fb_hext_stream && + (cmd == SNDRV_PCM_TRIGGER_START || cmd == SNDRV_PCM_TRIGGER_PAUSE_RELEASE)) + snd_hdac_ext_stream_start(fb_hext_stream); + + ret = hda_dai_trigger(substream, cmd, cpu_dai); + + /* nothing was started: do not leave the feedback stream running */ + if (ret && fb_hext_stream && + (cmd == SNDRV_PCM_TRIGGER_START || cmd == SNDRV_PCM_TRIGGER_PAUSE_RELEASE)) + snd_hdac_ext_stream_clear(fb_hext_stream); + + if (fb_hext_stream && + (cmd == SNDRV_PCM_TRIGGER_STOP || cmd == SNDRV_PCM_TRIGGER_SUSPEND || + cmd == SNDRV_PCM_TRIGGER_PAUSE_PUSH)) + snd_hdac_ext_stream_clear(fb_hext_stream); + + return ret; } static const struct snd_soc_dai_ops uaol_dai_ops = { diff --git a/sound/soc/sof/intel/hda.h b/sound/soc/sof/intel/hda.h index 30c7f421349b89..3b10c2c4b74711 100644 --- a/sound/soc/sof/intel/hda.h +++ b/sound/soc/sof/intel/hda.h @@ -545,6 +545,8 @@ struct hda_uaol_stream { bool params_valid; /* the stream was suspended and has to be reacquired on prepare */ bool suspended; + /* link DMA the DSP reads the playback feedback endpoint through, if any */ + struct hdac_ext_stream *fb_hext_stream; }; /* represents DSP HDA controller frontend - i.e. host facing control */ @@ -1179,6 +1181,12 @@ hda_select_dai_widget_ops(struct snd_sof_dev *sdev, struct snd_sof_widget *swidg int hda_dai_config(struct snd_soc_dapm_widget *w, unsigned int flags, struct snd_sof_dai_config_data *data); +struct hdac_ext_stream *hda_uaol_assign_fb_link_stream(struct snd_sof_dev *sdev, + struct snd_pcm_substream *substream, + unsigned int format_val); +void hda_uaol_release_fb_link_stream(struct snd_sof_dev *sdev, + struct hdac_ext_stream *hext_stream); + static inline struct snd_sof_dev *widget_to_sdev(struct snd_soc_dapm_widget *w) { struct snd_sof_widget *swidget = w->dobj.private; From c7bf691125e705fc913691b39ef738c1911a292c Mon Sep 17 00:00:00 2001 From: Peter Ujfalusi Date: Wed, 4 Nov 2026 15:08:37 +0200 Subject: [PATCH 43/53] ALSA: usb: offload: Add usb_offload_quiesce_stream() The offloaded stream is released from the host's hw_free, which runs only after the DSP has stopped and freed its side of the link. Until then the xHCI keeps servicing the offloaded endpoints. An isochronous IN endpoint, the feedback endpoint of a playback stream in particular, then has nowhere to deliver its packets, which can stall the controller's periodic schedule and lock it up. Add usb_offload_quiesce_stream() so that the host can stop the endpoints on the controller once its own side of the link has stopped, before it is torn down. The feedback endpoint is stopped first, then the data endpoint. The sideband resources stay claimed and usb_offload_release_stream() still has to follow. The stream is named the way release_stream() names it, by its direction and USB PCM device number. Co-developed-by: Serhiy Katsyuba Signed-off-by: Serhiy Katsyuba Signed-off-by: Peter Ujfalusi --- include/sound/usb-offload.h | 14 ++++++--- sound/usb/offload/usb-audio.c | 56 +++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 4 deletions(-) diff --git a/include/sound/usb-offload.h b/include/sound/usb-offload.h index 7713828bd2a628..42b19e0de0a555 100644 --- a/include/sound/usb-offload.h +++ b/include/sound/usb-offload.h @@ -148,7 +148,8 @@ struct usb_offload_ep_info { /** * struct usb_offload_stream_hw - resources reserved for one offloaded stream * @pcm_index: USB SND PCM device number the stream was acquired on, to be - * passed back to usb_offload_release_stream() + * passed back to usb_offload_quiesce_stream() and + * usb_offload_release_stream() * @bus_devid: PCI bus, device and function of the xHCI controller, as * PCI_DEVID() lays them out * @format: the sample format the device runs the stream in. It is the format of @@ -188,9 +189,11 @@ struct usb_offload_stream_hw { * connected device's data endpoint so that the caller can program its DSP. * The USB PCM is taken away from the host for as long as it is held; the * other PCMs of the device are not affected and can be acquired alongside. - * release_stream() reverses it, naming the stream by its direction and the - * USB PCM device number acquire_stream() reported in - * usb_offload_stream_hw.pcm_index. + * release_stream() reverses it. quiesce_stream() only stops the endpoints on + * the controller, for the caller to run once its own side of the link has + * stopped, before it is torn down; release_stream() still has to follow. + * Both name the stream by its direction and the USB PCM device number + * acquire_stream() reported in usb_offload_stream_hw.pcm_index. * * @params is the format to run the device at and @direction an * SNDRV_PCM_STREAM_* value. @pcm_index pins the search to one USB SND PCM @@ -205,6 +208,9 @@ int usb_offload_acquire_stream(struct usb_offload_ctx *ctx, int usb_offload_release_stream(struct usb_offload_ctx *ctx, struct usb_device *udev, int direction, int pcm_index); +int usb_offload_quiesce_stream(struct usb_offload_ctx *ctx, + struct usb_device *udev, int direction, + int pcm_index); /* * Narrow one of SNDRV_PCM_HW_PARAM_RATE, _CHANNELS or _FORMAT (@var) in diff --git a/sound/usb/offload/usb-audio.c b/sound/usb/offload/usb-audio.c index a8dd150d5c672f..3f19a19e56d172 100644 --- a/sound/usb/offload/usb-audio.c +++ b/sound/usb/offload/usb-audio.c @@ -687,6 +687,62 @@ int usb_offload_release_stream(struct usb_offload_ctx *ctx, } EXPORT_SYMBOL_NS_GPL(usb_offload_release_stream, "USB_AUDIO_OFFLOAD"); +/* + * Stop the offloaded endpoints on the xHCI once the DSP has stopped its side + * of the link, before the link goes down: an isochronous IN endpoint that the + * controller still services then has nowhere to deliver its packets, which + * can stall the controller's periodic schedule. The feedback endpoint is the + * IN one on a playback stream, so it goes first. The resources stay claimed + * until usb_offload_release_stream(). + * + * Return: 0 if the endpoints stopped, the first error otherwise. + */ +int usb_offload_quiesce_stream(struct usb_offload_ctx *ctx, + struct usb_device *udev, int direction, int pcm_index) +{ + struct usb_offload_ua_stream *stream; + struct usb_offload_dev *dev; + int ret = 0; + + if (!ctx || !udev) + return -EINVAL; + + if (direction < 0 || direction > SNDRV_PCM_STREAM_LAST) + return -EINVAL; + + guard(mutex)(&usb_offload_ua_lock); + + if (usb_offload_ua_ctx != ctx) + return -ENODEV; + + dev = usb_offload_ua_find_udev(udev); + if (!dev) + return -ENODEV; + + stream = usb_offload_ua_find_stream(dev, direction, pcm_index); + if (!stream) + return -ENODEV; + + /* both are attempted, the first failure is the one reported */ + if (stream->feedback_host_ep) { + ret = xhci_sideband_stop_endpoint(dev->sb, stream->feedback_host_ep); + if (ret) + dev_dbg(&udev->dev, "USB offload: stop feedback ep: %d\n", ret); + } + + if (stream->data_host_ep) { + int data_ret = xhci_sideband_stop_endpoint(dev->sb, stream->data_host_ep); + + if (data_ret) + dev_dbg(&udev->dev, "USB offload: stop ep: %d\n", data_ret); + if (!ret) + ret = data_ret; + } + + return ret; +} +EXPORT_SYMBOL_NS_GPL(usb_offload_quiesce_stream, "USB_AUDIO_OFFLOAD"); + static u64 usb_offload_add_s24_le(struct usb_offload_ctx *ctx, u64 formats) { if (ctx->s24_3le_as_s24_le && (formats & SNDRV_PCM_FMTBIT_S24_3LE)) From 5bec0be8e68c6190941f56d3c99c043f95f8e2e4 Mon Sep 17 00:00:00 2001 From: Peter Ujfalusi Date: Thu, 19 Nov 2026 10:37:05 +0200 Subject: [PATCH 44/53] ALSA: usb: offload: Add usb_offload_restart_stream() usb_offload_quiesce_stream() stops the endpoints of an offloaded stream when the host stops it. A stream can be started again without being released in between, which is how an xrun is recovered from, and its endpoints then stay stopped: the stream stalls. Add usb_offload_restart_stream() for the host to run before it starts its own side of the link again. The data endpoint is restarted first, the feedback endpoint, which the quiesce stops first, last. The stream lookup is shared with the quiesce. Signed-off-by: Peter Ujfalusi --- include/sound/usb-offload.h | 14 ++++--- sound/usb/offload/usb-audio.c | 73 +++++++++++++++++++++++++++++++---- 2 files changed, 74 insertions(+), 13 deletions(-) diff --git a/include/sound/usb-offload.h b/include/sound/usb-offload.h index 42b19e0de0a555..3c26c52fc8b510 100644 --- a/include/sound/usb-offload.h +++ b/include/sound/usb-offload.h @@ -148,8 +148,8 @@ struct usb_offload_ep_info { /** * struct usb_offload_stream_hw - resources reserved for one offloaded stream * @pcm_index: USB SND PCM device number the stream was acquired on, to be - * passed back to usb_offload_quiesce_stream() and - * usb_offload_release_stream() + * passed back to usb_offload_quiesce_stream(), + * usb_offload_restart_stream() and usb_offload_release_stream() * @bus_devid: PCI bus, device and function of the xHCI controller, as * PCI_DEVID() lays them out * @format: the sample format the device runs the stream in. It is the format of @@ -191,9 +191,10 @@ struct usb_offload_stream_hw { * other PCMs of the device are not affected and can be acquired alongside. * release_stream() reverses it. quiesce_stream() only stops the endpoints on * the controller, for the caller to run once its own side of the link has - * stopped, before it is torn down; release_stream() still has to follow. - * Both name the stream by its direction and the USB PCM device number - * acquire_stream() reported in usb_offload_stream_hw.pcm_index. + * stopped, before it is torn down; release_stream() still has to follow, or + * restart_stream() when the stream is started again without being released + * in between. All of them name the stream by its direction and the USB PCM + * device number acquire_stream() reported in usb_offload_stream_hw.pcm_index. * * @params is the format to run the device at and @direction an * SNDRV_PCM_STREAM_* value. @pcm_index pins the search to one USB SND PCM @@ -211,6 +212,9 @@ int usb_offload_release_stream(struct usb_offload_ctx *ctx, int usb_offload_quiesce_stream(struct usb_offload_ctx *ctx, struct usb_device *udev, int direction, int pcm_index); +int usb_offload_restart_stream(struct usb_offload_ctx *ctx, + struct usb_device *udev, int direction, + int pcm_index); /* * Narrow one of SNDRV_PCM_HW_PARAM_RATE, _CHANNELS or _FORMAT (@var) in diff --git a/sound/usb/offload/usb-audio.c b/sound/usb/offload/usb-audio.c index 3f19a19e56d172..81d37a72d0c658 100644 --- a/sound/usb/offload/usb-audio.c +++ b/sound/usb/offload/usb-audio.c @@ -687,6 +687,24 @@ int usb_offload_release_stream(struct usb_offload_ctx *ctx, } EXPORT_SYMBOL_NS_GPL(usb_offload_release_stream, "USB_AUDIO_OFFLOAD"); +/* + * The acquired stream of @udev, named by its direction and USB PCM, NULL if + * there is none. Caller holds usb_offload_ua_lock. + */ +static struct usb_offload_ua_stream * +usb_offload_ua_get_stream(struct usb_offload_ctx *ctx, struct usb_device *udev, + int direction, int pcm_index, struct usb_offload_dev **dev) +{ + if (usb_offload_ua_ctx != ctx) + return NULL; + + *dev = usb_offload_ua_find_udev(udev); + if (!*dev) + return NULL; + + return usb_offload_ua_find_stream(*dev, direction, pcm_index); +} + /* * Stop the offloaded endpoints on the xHCI once the DSP has stopped its side * of the link, before the link goes down: an isochronous IN endpoint that the @@ -712,14 +730,7 @@ int usb_offload_quiesce_stream(struct usb_offload_ctx *ctx, guard(mutex)(&usb_offload_ua_lock); - if (usb_offload_ua_ctx != ctx) - return -ENODEV; - - dev = usb_offload_ua_find_udev(udev); - if (!dev) - return -ENODEV; - - stream = usb_offload_ua_find_stream(dev, direction, pcm_index); + stream = usb_offload_ua_get_stream(ctx, udev, direction, pcm_index, &dev); if (!stream) return -ENODEV; @@ -743,6 +754,52 @@ int usb_offload_quiesce_stream(struct usb_offload_ctx *ctx, } EXPORT_SYMBOL_NS_GPL(usb_offload_quiesce_stream, "USB_AUDIO_OFFLOAD"); +/* + * Restart the endpoints usb_offload_quiesce_stream() stopped, for a stream + * which is started again without having been released in between, as when + * it recovers from an xrun. A stopped endpoint stays stopped until it is + * restarted, the DSP starting its side of the link does not do it. The + * feedback endpoint, which the quiesce stops first, comes back last. + */ +int usb_offload_restart_stream(struct usb_offload_ctx *ctx, + struct usb_device *udev, int direction, int pcm_index) +{ + struct usb_offload_ua_stream *stream; + struct usb_offload_dev *dev; + int ret; + + if (!ctx || !udev) + return -EINVAL; + + if (direction < 0 || direction > SNDRV_PCM_STREAM_LAST) + return -EINVAL; + + guard(mutex)(&usb_offload_ua_lock); + + stream = usb_offload_ua_get_stream(ctx, udev, direction, pcm_index, &dev); + if (!stream) + return -ENODEV; + + if (stream->data_host_ep) { + ret = xhci_sideband_start_endpoint(dev->sb, stream->data_host_ep); + if (ret) { + dev_dbg(&udev->dev, "USB offload: start ep: %d\n", ret); + return ret; + } + } + + if (stream->feedback_host_ep) { + ret = xhci_sideband_start_endpoint(dev->sb, stream->feedback_host_ep); + if (ret) { + dev_dbg(&udev->dev, "USB offload: start feedback ep: %d\n", ret); + return ret; + } + } + + return 0; +} +EXPORT_SYMBOL_NS_GPL(usb_offload_restart_stream, "USB_AUDIO_OFFLOAD"); + static u64 usb_offload_add_s24_le(struct usb_offload_ctx *ctx, u64 formats) { if (ctx->s24_3le_as_s24_le && (formats & SNDRV_PCM_FMTBIT_S24_3LE)) From 8e55a3bce5f5783a2c1f69369cde58a8910dec93 Mon Sep 17 00:00:00 2001 From: Peter Ujfalusi Date: Thu, 19 Nov 2026 13:17:29 +0200 Subject: [PATCH 45/53] ALSA: usb: offload: Claim the endpoints handed to the DSP The library claims the USB substream it offloads, but the endpoints the DSP drives can still be shared with a host stream: with implicit feedback the capture data endpoint is also the sync source of the device's playback. An offloaded capture stream and the host's playback then both run that endpoint, the host submitting URBs on a ring the sideband owns, and both stall. Claim the data endpoint, and an explicit feedback endpoint, with snd_usb_endpoint_claim_offload() right after their hw_params, before anything is set up on them, and release them before the stream's hw_free. An endpoint which another stream has open already fails the acquisition with -EBUSY, and the host cannot open one while the stream is held. The test whether a stream has an explicit feedback endpoint is shared with the feedback reservation. Signed-off-by: Peter Ujfalusi --- include/sound/usb-offload.h | 4 +++ sound/usb/offload/usb-audio.c | 63 ++++++++++++++++++++++++++++++++--- 2 files changed, 62 insertions(+), 5 deletions(-) diff --git a/include/sound/usb-offload.h b/include/sound/usb-offload.h index 3c26c52fc8b510..089ff39e9309fc 100644 --- a/include/sound/usb-offload.h +++ b/include/sound/usb-offload.h @@ -189,6 +189,10 @@ struct usb_offload_stream_hw { * connected device's data endpoint so that the caller can program its DSP. * The USB PCM is taken away from the host for as long as it is held; the * other PCMs of the device are not affected and can be acquired alongside. + * So are the endpoints the DSP drives: one which another stream holds, like + * the capture endpoint a playback stream with implicit feedback syncs to, + * fails the acquisition with -EBUSY, and the host cannot open one while the + * stream is held. * release_stream() reverses it. quiesce_stream() only stops the endpoints on * the controller, for the caller to run once its own side of the link has * stopped, before it is torn down; release_stream() still has to follow, or diff --git a/sound/usb/offload/usb-audio.c b/sound/usb/offload/usb-audio.c index 81d37a72d0c658..9aa57ce39b13e8 100644 --- a/sound/usb/offload/usb-audio.c +++ b/sound/usb/offload/usb-audio.c @@ -257,6 +257,48 @@ static void usb_offload_fill_ep(struct usb_offload_ep_info *desc, desc->interval = host_ep->desc.bInterval; } +/* + * Whether the stream has an explicit clock feedback endpoint for the DSP to + * drive. usb-audio flags the data endpoint of a stream which syncs to an + * implicit feedback source, not the capture endpoint it syncs to. + */ +static bool usb_offload_ua_explicit_fb(struct snd_usb_substream *subs) +{ + return subs->sync_endpoint && !subs->data_endpoint->implicit_fb_sync; +} + +/* + * The endpoints the DSP drives are kept away from the host for as long as the + * stream is offloaded. One which another stream has open already, like the + * capture endpoint a host playback stream with implicit feedback syncs to, + * cannot be taken. + */ +static int usb_offload_ua_claim_eps(struct snd_usb_audio *chip, + struct snd_usb_substream *subs) +{ + int ret; + + ret = snd_usb_endpoint_claim_offload(chip, subs->data_endpoint); + if (ret) + return ret; + + if (usb_offload_ua_explicit_fb(subs)) { + ret = snd_usb_endpoint_claim_offload(chip, subs->sync_endpoint); + if (ret) + snd_usb_endpoint_release_offload(chip, subs->data_endpoint); + } + + return ret; +} + +static void usb_offload_ua_release_eps(struct snd_usb_audio *chip, + struct snd_usb_substream *subs) +{ + if (usb_offload_ua_explicit_fb(subs)) + snd_usb_endpoint_release_offload(chip, subs->sync_endpoint); + snd_usb_endpoint_release_offload(chip, subs->data_endpoint); +} + /* * Start the device streaming so that its endpoints, and with them their * xHCI transfer rings, exist. This is what snd_usb_pcm_prepare() does for @@ -281,9 +323,17 @@ static int usb_offload_ua_start_stream(struct usb_offload_dev *dev, if (ret < 0) goto err_suspend; + /* before anything is set up on an endpoint another stream may run */ + ret = usb_offload_ua_claim_eps(chip, subs); + if (ret) { + dev_dbg(&dev->udev->dev, + "USB offload: endpoint in use by another stream\n"); + goto err_hw_free; + } + if (atomic_read(&chip->shutdown)) { ret = -ENODEV; - goto err_hw_free; + goto err_release_eps; } { @@ -291,22 +341,24 @@ static int usb_offload_ua_start_stream(struct usb_offload_dev *dev, if (pm.err < 0) { ret = pm.err; - goto err_hw_free; + goto err_release_eps; } if (subs->sync_endpoint) { ret = snd_usb_endpoint_prepare(chip, subs->sync_endpoint); if (ret < 0) - goto err_hw_free; + goto err_release_eps; } ret = snd_usb_endpoint_prepare(chip, subs->data_endpoint); if (ret < 0) - goto err_hw_free; + goto err_release_eps; } return 0; +err_release_eps: + usb_offload_ua_release_eps(chip, subs); err_hw_free: snd_usb_hw_free(subs); err_suspend: @@ -321,6 +373,7 @@ static int usb_offload_ua_start_stream(struct usb_offload_dev *dev, static void usb_offload_ua_stop_stream(struct usb_offload_dev *dev, struct snd_usb_substream *subs) { + usb_offload_ua_release_eps(dev->chip, subs); snd_usb_hw_free(subs); snd_usb_autosuspend(dev->chip); snd_usb_offload_release_substream(subs); @@ -584,7 +637,7 @@ int usb_offload_acquire_stream(struct usb_offload_ctx *ctx, * endpoint such a stream would sync to carries audio samples, not a * feedback value the DSP can interpret. */ - if (subs->sync_endpoint && !subs->data_endpoint->implicit_fb_sync) { + if (usb_offload_ua_explicit_fb(subs)) { fb_host_ep = usb_pipe_endpoint(udev, subs->sync_endpoint->pipe); if (!fb_host_ep) { ret = -ENXIO; From f520be295d4f6181c1b06f399c38e76fafdef6c1 Mon Sep 17 00:00:00 2001 From: Peter Ujfalusi Date: Thu, 19 Nov 2026 14:29:35 +0200 Subject: [PATCH 46/53] ALSA: usb: offload: Run the format quirk of the device snd_usb_pcm_prepare() runs snd_usb_set_format_quirk() whenever it sets up the data endpoint afresh. The library prepares the endpoints itself and skipped it, leaving a device which takes its sample rate through vendor requests at whatever rate it ran last. Export the quirk and run it the same way. Some quirks only set up the host's URB path: the MacroSilicon MS2100 and MS2109 capture cards need the first two bytes of the stream dropped, or the channels come out swapped and out of phase, and the E-Mu devices need the start of each packet skipped at 176.4 and 192kHz. The DSP gets the data as the device sends it, so refuse a stream that is left with such an offset with -EOPNOTSUPP. The E-Mu devices are vendor class and never pass the candidate filter, the MacroSilicon cards do. Signed-off-by: Peter Ujfalusi --- sound/usb/offload/usb-audio.c | 16 ++++++++++++++++ sound/usb/quirks.c | 1 + 2 files changed, 17 insertions(+) diff --git a/sound/usb/offload/usb-audio.c b/sound/usb/offload/usb-audio.c index 9aa57ce39b13e8..8c08ccdaad634d 100644 --- a/sound/usb/offload/usb-audio.c +++ b/sound/usb/offload/usb-audio.c @@ -44,6 +44,7 @@ #include "../endpoint.h" #include "../pcm.h" #include "../power.h" +#include "../quirks.h" #include "offload.h" @@ -353,6 +354,21 @@ static int usb_offload_ua_start_stream(struct usb_offload_dev *dev, ret = snd_usb_endpoint_prepare(chip, subs->data_endpoint); if (ret < 0) goto err_release_eps; + /* the device's own format setup, as snd_usb_pcm_prepare() runs it */ + if (ret > 0) + snd_usb_set_format_quirk(subs, subs->cur_audiofmt); + } + + /* + * A quirk can leave the host skipping bytes at the start of the stream + * or of each packet to get the samples aligned, which only the URB path + * does. The DSP would get them misaligned. + */ + if (subs->stream_offset_adj || subs->pkt_offset_adj) { + dev_dbg(&dev->udev->dev, + "USB offload: format needs the host to align the data, not offloaded\n"); + ret = -EOPNOTSUPP; + goto err_release_eps; } return 0; diff --git a/sound/usb/quirks.c b/sound/usb/quirks.c index 7c581e283903ff..2c2ca3c3d72395 100644 --- a/sound/usb/quirks.c +++ b/sound/usb/quirks.c @@ -2137,6 +2137,7 @@ void snd_usb_set_format_quirk(struct snd_usb_substream *subs, break; } } +EXPORT_SYMBOL_GPL(snd_usb_set_format_quirk); int snd_usb_select_mode_quirk(struct snd_usb_audio *chip, const struct audioformat *fmt) From fbee853c4790c8050168a33cf0ef38a2309f5812 Mon Sep 17 00:00:00 2001 From: Peter Ujfalusi Date: Wed, 4 Nov 2026 15:09:13 +0200 Subject: [PATCH 47/53] ASoC: SOF: Intel: hda-uaol: Quiesce the USB stream after the DSP stops UAOL The USB stream is released from hw_free, after the DAI copier has been freed and the UAOL link powered off. Until then the xHCI keeps servicing the offloaded endpoints, and the feedback IN endpoint has nowhere to deliver its packets, which can lock up the controller. On TRIGGER_STOP and TRIGGER_SUSPEND, stop the xHCI endpoints with usb_offload_quiesce_stream() right after hda_dai_trigger() has stopped the DAI pipeline. They cannot be stopped any earlier: a UAOL stream cannot go idle once the xHCI no longer services an endpoint still bound to it, so the DSP would time out stopping both the audio and the feedback stream and fall back to resetting them. A stopped endpoint stays stopped until it is restarted, and only a hw_free followed by hw_params, or the reacquire after a system suspend, brings up fresh ones. A stream started again right after a stop, which is how an xrun is recovered from (STOP, prepare, START), would run the DSP against stopped endpoints: the link makes no progress and the application's next write fails with -EIO. Remember that the endpoints were stopped and restart them with usb_offload_restart_stream() on TRIGGER_START, before the DSP starts its side of the link, so that they run then as they do on the first start. Releasing the stream, at hw_free or before a reacquire, forgets it. Co-developed-by: Serhiy Katsyuba Signed-off-by: Serhiy Katsyuba Signed-off-by: Peter Ujfalusi --- sound/soc/sof/intel/hda-uaol.c | 67 ++++++++++++++++++++++++++++++---- sound/soc/sof/intel/hda.h | 2 + 2 files changed, 61 insertions(+), 8 deletions(-) diff --git a/sound/soc/sof/intel/hda-uaol.c b/sound/soc/sof/intel/hda-uaol.c index 9555dc0de6ca66..29c0328a6df742 100644 --- a/sound/soc/sof/intel/hda-uaol.c +++ b/sound/soc/sof/intel/hda-uaol.c @@ -258,8 +258,8 @@ static void hda_uaol_stop_frontends(struct sof_intel_hda_dev *hdev, * back to the library, where a device reusing the address would * match. */ - stream->udev = NULL; - stream->be = NULL; + WRITE_ONCE(stream->udev, NULL); + WRITE_ONCE(stream->be, NULL); snd_soc_dpcm_mutex_unlock(be); } } @@ -990,9 +990,10 @@ static int uaol_dai_start_stream(struct snd_pcm_substream *substream, goto err_release; } - stream->udev = udev; stream->pcm_index = hw.pcm_index; - stream->be = substream; + /* these are read without the DPCM mutex by the trigger and by unplug handling */ + WRITE_ONCE(stream->be, substream); + WRITE_ONCE(stream->udev, udev); /* * The route control refuses a change once the stream is set here, and @@ -1021,6 +1022,9 @@ static void uaol_dai_stop_stream(struct snd_pcm_substream *substream, struct sof_intel_hda_dev *hdev = sdev->pdata->hw_pdata; struct usb_device *udev = stream->udev; + /* a stream given up after a stop has nothing to restart */ + stream->quiesced = false; + /* also after an unplug, which takes only the USB side away */ if (stream->fb_hext_stream) uaol_dai_release_feedback(sdev, stream); @@ -1028,8 +1032,8 @@ static void uaol_dai_stop_stream(struct snd_pcm_substream *substream, if (!udev) return; - stream->udev = NULL; - stream->be = NULL; + WRITE_ONCE(stream->udev, NULL); + WRITE_ONCE(stream->be, NULL); usb_offload_release_stream(hdev->uaol, udev, stream->direction, stream->pcm_index); } @@ -1173,24 +1177,55 @@ static int uaol_dai_prepare(struct snd_pcm_substream *substream, * only notice this driver gets that the stream will have to be reacquired. * The feedback link DMA is not the DAI's DMA data, so the common trigger * does not know about it and it is run from here, alongside the audio one. + * + * On the way down the xHCI endpoints are stopped right after the DSP has + * stopped the UAOL streams, not left to hw_free, which runs only after the + * DAI copier has been freed and the UAOL link powered off. An endpoint the + * controller keeps servicing past that point, the feedback IN endpoint in + * particular, has nowhere to deliver to and can lock up the controller. + * They are not stopped any earlier: a UAOL stream cannot go idle once the + * xHCI no longer services its endpoint, and the DSP times out stopping it. + * + * A stopped endpoint stays stopped, so a stream started again without a + * hw_free in between, as on the recovery from an xrun, restarts them before + * the DSP starts its side of the link, the way they run on the first start. */ static int uaol_dai_trigger(struct snd_pcm_substream *substream, int cmd, struct snd_soc_dai *cpu_dai) { struct snd_sof_dev *sdev = dai_to_sdev(substream, cpu_dai); - struct hda_uaol_stream *stream = hda_uaol_stream_get(sdev->pdata->hw_pdata, cpu_dai); + struct sof_intel_hda_dev *hdev = sdev->pdata->hw_pdata; + struct hda_uaol_stream *stream = hda_uaol_stream_get(hdev, cpu_dai); struct hdac_ext_stream *fb_hext_stream = NULL; + int direction = substream->stream; + struct usb_device *udev; int ret; if (!stream) return hda_dai_trigger(substream, cmd, cpu_dai); - if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) + /* + * An unplug clears it under the DPCM mutex, which the trigger does not + * hold: look at it once, a device which went away in the meantime is + * reported by the library. + */ + udev = READ_ONCE(stream->udev); + + if (direction == SNDRV_PCM_STREAM_PLAYBACK) fb_hext_stream = stream->fb_hext_stream; if (cmd == SNDRV_PCM_TRIGGER_SUSPEND) stream->suspended = true; + if (cmd == SNDRV_PCM_TRIGGER_START && stream->quiesced && udev) { + ret = usb_offload_restart_stream(hdev->uaol, udev, direction, + stream->pcm_index); + if (ret) + return ret; + + stream->quiesced = false; + } + if (fb_hext_stream && (cmd == SNDRV_PCM_TRIGGER_START || cmd == SNDRV_PCM_TRIGGER_PAUSE_RELEASE)) snd_hdac_ext_stream_start(fb_hext_stream); @@ -1202,6 +1237,22 @@ static int uaol_dai_trigger(struct snd_pcm_substream *substream, int cmd, (cmd == SNDRV_PCM_TRIGGER_START || cmd == SNDRV_PCM_TRIGGER_PAUSE_RELEASE)) snd_hdac_ext_stream_clear(fb_hext_stream); + /* + * Only once the DSP has stopped its side: a UAOL stream whose endpoint + * is no longer serviced cannot go idle. Restarting an endpoint which + * did stop is harmless, so the stream counts as quiesced also when only + * one of them could be stopped, and the failure is reported. A device + * which has been unplugged in the meantime has nothing left to stop. + */ + if (!ret && (cmd == SNDRV_PCM_TRIGGER_STOP || cmd == SNDRV_PCM_TRIGGER_SUSPEND) && + udev) { + ret = usb_offload_quiesce_stream(hdev->uaol, udev, direction, + stream->pcm_index); + if (ret == -ENODEV) + ret = 0; + stream->quiesced = true; + } + if (fb_hext_stream && (cmd == SNDRV_PCM_TRIGGER_STOP || cmd == SNDRV_PCM_TRIGGER_SUSPEND || cmd == SNDRV_PCM_TRIGGER_PAUSE_PUSH)) diff --git a/sound/soc/sof/intel/hda.h b/sound/soc/sof/intel/hda.h index 3b10c2c4b74711..ba25e7b767bc84 100644 --- a/sound/soc/sof/intel/hda.h +++ b/sound/soc/sof/intel/hda.h @@ -545,6 +545,8 @@ struct hda_uaol_stream { bool params_valid; /* the stream was suspended and has to be reacquired on prepare */ bool suspended; + /* the USB endpoints were stopped and have to be restarted on start */ + bool quiesced; /* link DMA the DSP reads the playback feedback endpoint through, if any */ struct hdac_ext_stream *fb_hext_stream; }; From 539fad3830dcaa6ea3d60e8a7f5d9bd3edde7a16 Mon Sep 17 00:00:00 2001 From: Peter Ujfalusi Date: Thu, 19 Nov 2026 11:08:50 +0200 Subject: [PATCH 48/53] ASoC: SOF: Intel: hda: Start the USB audio offload The pieces of the USB audio offload are in place: the device notifications reach the ASoC USB port, the UAOL DAIs acquire, configure, quiesce and restart the USB stream, and the frontends are constrained to what the link and the device can carry. Install hda_machine_register() as the machine_register op, so that the driver registers with the USB audio offload library once the firmware has booted and the library starts reporting the offloadable devices. Signed-off-by: Peter Ujfalusi --- sound/soc/sof/intel/hda-common-ops.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sound/soc/sof/intel/hda-common-ops.c b/sound/soc/sof/intel/hda-common-ops.c index 5b4f3a64be6161..0912b8a1491e71 100644 --- a/sound/soc/sof/intel/hda-common-ops.c +++ b/sound/soc/sof/intel/hda-common-ops.c @@ -40,7 +40,7 @@ const struct snd_sof_dsp_ops sof_hda_common_ops = { /* machine driver */ .machine_select = hda_machine_select, - .machine_register = sof_machine_register, + .machine_register = hda_machine_register, .machine_unregister = sof_machine_unregister, .set_mach_params = hda_set_mach_params, .finalize_mach_params = hda_finalize_mach_params, From 63c7689fa34a38ecdbe4e0ecd8e460aa68ac2f8f Mon Sep 17 00:00:00 2001 From: Peter Ujfalusi Date: Wed, 4 Nov 2026 15:05:16 +0200 Subject: [PATCH 49/53] ASoC: Intel: sof_sdw: Add the backend DAI links for the UAOL Create the UAOL backend DAI links from the link mask and the stream counts reported in mach_params, following the BT offload link, with their own function topology so that the PCMs they add do not collide with the other functions of the card. Each stream of each direction gets a link of its own: the backend hw_params fixup is called without the stream direction, so a duplex link could not tell which of the two DAI copiers describes the stream being configured and would have to constrain both directions to the same format. With two streams per direction the backends are "UAOL0-Play0", "UAOL0-Play1", "UAOL0-Cap0" and "UAOL0-Cap1", on the CPU DAIs "UAOL0 Playback Pin0" and so on. The link and CPU DAI names are kept in sof_uaol_common.h for the other boards to share. The backends are created ordinal first, playback before capture within an ordinal, so the first playback and capture backend keep two consecutive IDs whatever the stream count, and a topology describing one stream per direction stays valid. The ordinal only tells the backends of a direction apart: it is not the UAOL stream the backend runs on, which is decided by the xHCI sideband resource the endpoint gets when the USB device is acquired. The backend names are abbreviated on purpose: the topology names its DAI copier widget "dai-copier.UAOL..", and alsa-lib keeps element ids in SNDRV_CTL_ELEM_ID_NAME_MAXLEN bytes, so from 38 characters on the ids of the widget's "dai.word" and "dai.string" tuple sections collide and one set is silently dropped from the topology. The topology is named sof-uaol-id.tplg after the id of the first playback link. The function name carries no link index, like dmic and hdmi, as the host driver reports a single link. A missing UAOL function topology file is not fatal for the card: like with best_effort the link is skipped when the topology files are collected, and the card comes up without USB offload. Signed-off-by: Peter Ujfalusi --- sound/soc/intel/boards/sof_sdw.c | 68 +++++++++++++++++-- sound/soc/intel/boards/sof_uaol_common.h | 46 +++++++++++++ .../intel/common/sof-function-topology-lib.c | 7 +- 3 files changed, 116 insertions(+), 5 deletions(-) create mode 100644 sound/soc/intel/boards/sof_uaol_common.h diff --git a/sound/soc/intel/boards/sof_sdw.c b/sound/soc/intel/boards/sof_sdw.c index 5a4620faa9514f..bef2b662ce7593 100644 --- a/sound/soc/intel/boards/sof_sdw.c +++ b/sound/soc/intel/boards/sof_sdw.c @@ -16,6 +16,7 @@ #include #include #include "sof_sdw_common.h" +#include "sof_uaol_common.h" #include "../../codecs/rt711.h" static unsigned long sof_sdw_quirk = RT711_JD1; @@ -1225,6 +1226,56 @@ static int create_bt_dailinks(struct snd_soc_card *card, return 0; } +static int create_uaol_dailinks(struct snd_soc_card *card, + struct snd_soc_dai_link **dai_links, int *be_id, + const struct snd_soc_acpi_mach_params *mach_params) +{ + unsigned long uaol_mask = mach_params->uaol_link_mask; + unsigned int num_streams = max(mach_params->uaol_playback_num, + mach_params->uaol_capture_num); + struct device *dev = card->dev; + int i, j, stream, ret; + + /* + * Ordinal first, playback before capture within an ordinal: the IDs of + * the first playback and capture backend then do not depend on how many + * streams the link has. + */ + for_each_set_bit(i, &uaol_mask, BITS_PER_TYPE(uaol_mask)) { + for (j = 0; j < num_streams; j++) { + for_each_pcm_streams(stream) { + bool playback = stream == SNDRV_PCM_STREAM_PLAYBACK; + char *cpu_dai_name; + char *name; + + if (j >= sof_uaol_stream_num(mach_params, stream)) + continue; + + name = devm_kasprintf(dev, GFP_KERNEL, + SOF_UAOL_LINK_NAME(stream), i, j); + cpu_dai_name = devm_kasprintf(dev, GFP_KERNEL, + SOF_UAOL_CPU_DAI_NAME(stream), + i, j); + if (!name || !cpu_dai_name) + return -ENOMEM; + + ret = asoc_sdw_init_simple_dai_link(dev, *dai_links, be_id, + name, playback, !playback, + cpu_dai_name, "dummy", + snd_soc_dummy_dlc.name, + snd_soc_dummy_dlc.dai_name, + 1, NULL, NULL); + if (ret) + return ret; + + (*dai_links)++; + } + } + } + + return 0; +} + static int create_echoref_dailink(struct snd_soc_card *card, struct snd_soc_dai_link **dai_links, int *be_id) { @@ -1257,7 +1308,7 @@ static int sof_card_dai_links_create(struct snd_soc_card *card) { struct device *dev = card->dev; struct snd_soc_acpi_mach *mach = dev_get_platdata(card->dev); - int sdw_be_num = 0, ssp_num = 0, dmic_num = 0, bt_num = 0; + int sdw_be_num = 0, ssp_num = 0, dmic_num = 0, bt_num = 0, uaol_num = 0; struct asoc_sdw_mc_private *ctx = snd_soc_card_get_drvdata(card); struct intel_mc_ctx *intel_ctx = (struct intel_mc_ctx *)ctx->private; struct snd_soc_acpi_mach_params *mach_params = &mach->mach_params; @@ -1351,9 +1402,11 @@ static int sof_card_dai_links_create(struct snd_soc_card *card) if (sof_sdw_quirk & SOF_SSP_BT_OFFLOAD_PRESENT || mach_params->bt_link_mask) bt_num = 1; - dev_dbg(dev, "DAI link numbers: sdw %d, ssp %d, dmic %d, hdmi %d, bt: %d\n", + uaol_num = sof_uaol_link_count(mach_params); + + dev_dbg(dev, "DAI link numbers: sdw %d, ssp %d, dmic %d, hdmi %d, bt: %d, uaol %d\n", sdw_be_num, ssp_num, dmic_num, - intel_ctx->hdmi.idisp_codec ? hdmi_num : 0, bt_num); + intel_ctx->hdmi.idisp_codec ? hdmi_num : 0, bt_num, uaol_num); codec_conf = devm_kcalloc(dev, num_confs, sizeof(*codec_conf), GFP_KERNEL); if (!codec_conf) { @@ -1366,7 +1419,7 @@ static int sof_card_dai_links_create(struct snd_soc_card *card) * This should be the last DAI link and it is expected both for monolithic * and functional SOF topologies to support echo reference. */ - num_links = sdw_be_num + ssp_num + dmic_num + hdmi_num + bt_num + 1; + num_links = sdw_be_num + ssp_num + dmic_num + hdmi_num + bt_num + uaol_num + 1; dai_links = devm_kcalloc(dev, num_links, sizeof(*dai_links), GFP_KERNEL); if (!dai_links) { ret = -ENOMEM; @@ -1415,6 +1468,13 @@ static int sof_card_dai_links_create(struct snd_soc_card *card) goto err_end; } + /* UAOL */ + if (uaol_num) { + ret = create_uaol_dailinks(card, &dai_links, &be_id, mach_params); + if (ret) + goto err_end; + } + /* dummy echo ref link. keep this as the last DAI link. The DAI link ID does not matter */ ret = create_echoref_dailink(card, &dai_links, &be_id); if (ret) { diff --git a/sound/soc/intel/boards/sof_uaol_common.h b/sound/soc/intel/boards/sof_uaol_common.h new file mode 100644 index 00000000000000..e4d10275804823 --- /dev/null +++ b/sound/soc/intel/boards/sof_uaol_common.h @@ -0,0 +1,46 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * Copyright(c) 2026 Intel Corporation. + */ + +#ifndef __SOF_UAOL_COMMON_H +#define __SOF_UAOL_COMMON_H + +#include +#include +#include + +/* + * Each UAOL link has a number of streams per direction, and every one of + * them gets a backend DAI link of its own, unidirectional: the backend + * hw_params fixup is called without the stream direction, so a duplex link + * could not tell which of the two DAI copiers of the topology describes the + * stream being configured. Both names take the UAOL link index and then the + * ordinal of the stream among those of its direction. The ordinal only tells + * the backends apart, it is not the UAOL stream the backend ends up on. + * + * The backend names are abbreviated: the topology's DAI copier widget is + * named "dai-copier.UAOL..", and alsa-lib silently drops + * the widget's tuples once that exceeds 37 characters. + */ +#define SOF_UAOL_LINK_NAME(dir) \ + ((dir) == SNDRV_PCM_STREAM_PLAYBACK ? "UAOL%d-Play%d" : "UAOL%d-Cap%d") +#define SOF_UAOL_CPU_DAI_NAME(dir) \ + ((dir) == SNDRV_PCM_STREAM_PLAYBACK ? "UAOL%d Playback Pin%d" : "UAOL%d Capture Pin%d") + +static inline unsigned int +sof_uaol_stream_num(const struct snd_soc_acpi_mach_params *mach_params, int dir) +{ + return dir == SNDRV_PCM_STREAM_PLAYBACK ? mach_params->uaol_playback_num : + mach_params->uaol_capture_num; +} + +/* Backend DAI links of all the streams of all the UAOL links in the mask */ +static inline unsigned int +sof_uaol_link_count(const struct snd_soc_acpi_mach_params *mach_params) +{ + return hweight32(mach_params->uaol_link_mask) * + (mach_params->uaol_playback_num + mach_params->uaol_capture_num); +} + +#endif /* __SOF_UAOL_COMMON_H */ diff --git a/sound/soc/intel/common/sof-function-topology-lib.c b/sound/soc/intel/common/sof-function-topology-lib.c index 57e089f8e721dc..bef95bd629ebff 100644 --- a/sound/soc/intel/common/sof-function-topology-lib.c +++ b/sound/soc/intel/common/sof-function-topology-lib.c @@ -24,6 +24,7 @@ enum tplg_device_id { TPLG_DEVICE_SSP_BT, TPLG_DEVICE_SSP_HDMI_IN, TPLG_DEVICE_LOOPBACK_VIRTUAL, + TPLG_DEVICE_UAOL, TPLG_DEVICE_MAX }; @@ -213,6 +214,9 @@ int sof_sdw_get_tplg_files(struct snd_soc_card *card, const struct snd_soc_acpi_ if (get_ssp_tplg_dev(card->dev, dai_link, &hdmi_in_mask, &tplg_dev, &tplg_dev_name) < 0) continue; + } else if (strstr(dai_link->name, "UAOL")) { + tplg_dev = TPLG_DEVICE_UAOL; + tplg_dev_name = "uaol"; } else if (strstr(dai_link->name, "Loopback_Virtual")) { tplg_dev = TPLG_DEVICE_LOOPBACK_VIRTUAL; /* @@ -243,7 +247,8 @@ int sof_sdw_get_tplg_files(struct snd_soc_card *card, const struct snd_soc_acpi_ /* Check presence of sub-topologies */ if (!tplg_files_exist(card->dev, tplg_file)) { devm_kfree(card->dev, tplg_file); - if (best_effort) + /* without its fragment a UAOL link only offers no USB offload */ + if (best_effort || tplg_dev == TPLG_DEVICE_UAOL) continue; return 0; From a112b794133e0c3b869d773ed9b413fc9edfee5c Mon Sep 17 00:00:00 2001 From: Peter Ujfalusi Date: Wed, 4 Nov 2026 15:05:35 +0200 Subject: [PATCH 50/53] ASoC: Intel: sof_board_helpers: Add support for UAOL backend DAI links Add a link type creating the UAOL backend DAI links from a link mask and the stream counts per direction, so that boards using the helpers can place them in their link order like any other backend. Each stream of each direction gets a link of its own, named like sof_sdw does it, see sof_uaol_common.h. The backends are created ordinal first, playback before capture within an ordinal, so the IDs of the first playback and capture backend do not depend on how many streams the link has. Signed-off-by: Peter Ujfalusi --- sound/soc/intel/boards/sof_board_helpers.c | 97 ++++++++++++++++++++++ sound/soc/intel/boards/sof_board_helpers.h | 7 ++ 2 files changed, 104 insertions(+) diff --git a/sound/soc/intel/boards/sof_board_helpers.c b/sound/soc/intel/boards/sof_board_helpers.c index f741a1e142bee1..c025b6073d9482 100644 --- a/sound/soc/intel/boards/sof_board_helpers.c +++ b/sound/soc/intel/boards/sof_board_helpers.c @@ -6,6 +6,7 @@ #include "../common/soc-intel-quirks.h" #include "hda_dsp_common.h" #include "sof_board_helpers.h" +#include "sof_uaol_common.h" /* * Intel HDMI DAI Link @@ -497,6 +498,86 @@ static int set_hda_codec_link(struct device *dev, struct snd_soc_dai_link *link, return 0; } +static int set_uaol_link(struct device *dev, struct snd_soc_dai_link *link, + int be_id, int uaol_id, int stream, int index) +{ + bool playback = stream == SNDRV_PCM_STREAM_PLAYBACK; + struct snd_soc_dai_link_component *cpus; + + dev_dbg(dev, "link %d: uaol %d, %s %d\n", be_id, uaol_id, + playback ? "playback" : "capture", index); + + link->name = devm_kasprintf(dev, GFP_KERNEL, SOF_UAOL_LINK_NAME(stream), + uaol_id, index); + if (!link->name) + return -ENOMEM; + + /* cpus */ + cpus = devm_kzalloc(dev, sizeof(struct snd_soc_dai_link_component), + GFP_KERNEL); + if (!cpus) + return -ENOMEM; + + cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL, SOF_UAOL_CPU_DAI_NAME(stream), + uaol_id, index); + if (!cpus->dai_name) + return -ENOMEM; + + link->cpus = cpus; + link->num_cpus = 1; + + /* codecs */ + link->codecs = &snd_soc_dummy_dlc; + link->num_codecs = 1; + + /* platforms */ + link->platforms = platform_component; + link->num_platforms = ARRAY_SIZE(platform_component); + + link->id = be_id; + link->no_pcm = 1; + if (playback) + link->playback_only = 1; + else + link->capture_only = 1; + + return 0; +} + +/* + * One backend per stream of each direction of UAOL link @uaol_id. Ordinal + * first, playback before capture within an ordinal: the IDs of the first + * playback and capture backend then do not depend on how many streams the + * link has. + */ +static int set_uaol_links(struct device *dev, struct sof_card_private *ctx, + struct snd_soc_dai_link *links, int *idx, + unsigned long *be_id, int uaol_id) +{ + unsigned int num_streams = max(ctx->uaol_playback_num, ctx->uaol_capture_num); + int stream, i, ret; + + for (i = 0; i < num_streams; i++) { + for_each_pcm_streams(stream) { + unsigned int num = stream == SNDRV_PCM_STREAM_PLAYBACK ? + ctx->uaol_playback_num : ctx->uaol_capture_num; + + if (i >= num) + continue; + + ret = set_uaol_link(dev, &links[*idx], *be_id, uaol_id, + stream, i); + if (ret) + return ret; + + (*idx)++; + (*be_id)++; + } + } + + return 0; +} + static int calculate_num_links(struct sof_card_private *ctx) { int num_links = 0; @@ -530,6 +611,10 @@ static int calculate_num_links(struct sof_card_private *ctx) if (ctx->hda_codec_present) num_links += 2; + /* USB offload, one link per stream of each direction */ + num_links += hweight_long(ctx->uaol_link_mask) * + (ctx->uaol_playback_num + ctx->uaol_capture_num); + return num_links; } @@ -542,6 +627,7 @@ int sof_intel_board_set_dai_link(struct device *dev, struct snd_soc_card *card, int idx = 0; int ret; int ssp_hdmi_in = 0; + int uaol_id = 0; unsigned long link_order, link; unsigned long link_ids, be_id; @@ -713,6 +799,17 @@ int sof_intel_board_set_dai_link(struct device *dev, struct snd_soc_card *card, idx++; break; + case SOF_LINK_UAOL: + /* USB offload */ + for_each_set_bit(uaol_id, &ctx->uaol_link_mask, + BITS_PER_TYPE(ctx->uaol_link_mask)) { + ret = set_uaol_links(dev, ctx, links, &idx, &be_id, uaol_id); + if (ret) { + dev_err(dev, "fail to set uaol link, ret %d\n", ret); + return ret; + } + } + break; case SOF_LINK_NONE: /* caught here if it's not used as terminator in macro */ fallthrough; diff --git a/sound/soc/intel/boards/sof_board_helpers.h b/sound/soc/intel/boards/sof_board_helpers.h index 33a9601b770c54..dc2c974d68d57b 100644 --- a/sound/soc/intel/boards/sof_board_helpers.h +++ b/sound/soc/intel/boards/sof_board_helpers.h @@ -58,6 +58,7 @@ enum { SOF_LINK_BT_OFFLOAD, SOF_LINK_HDMI_IN, SOF_LINK_HDA, + SOF_LINK_UAOL, }; #define SOF_LINK_ORDER_MASK (0xF) @@ -121,6 +122,9 @@ struct sof_rt5682_private { * @ssp_amp: ssp port number of speaker BE link * @ssp_bt: ssp port number of BT offload BE link * @ssp_mask_hdmi_in: ssp port mask of HDMI-IN BE link + * @uaol_link_mask: mask of the USB offload links to create BE links for + * @uaol_playback_num: playback BE links to create per USB offload link + * @uaol_capture_num: capture BE links to create per USB offload link * @bt_offload_present: true to create BT offload BE link * @hda_codec_present: true to create HDA codec BE links * @codec_link: pointer to headset codec dai link @@ -144,6 +148,9 @@ struct sof_card_private { int ssp_amp; int ssp_bt; unsigned long ssp_mask_hdmi_in; + unsigned long uaol_link_mask; + unsigned int uaol_playback_num; + unsigned int uaol_capture_num; bool bt_offload_present; bool hda_codec_present; From 0b7ed5c14eca3fdd0d8085efbefc21fde868c6a9 Mon Sep 17 00:00:00 2001 From: Peter Ujfalusi Date: Wed, 4 Nov 2026 15:05:54 +0200 Subject: [PATCH 51/53] ASoC: Intel: skl_hda_dsp_generic: Add the backend DAI links for the UAOL Create the UAOL backend DAI links from the link mask and the stream counts reported in mach_params, following the BT offload link. The board assigns fixed backend ids, so the UAOL links can take the ids after the BT offload one regardless of which other functions are present. A topology describing the UAOL therefore does not need a variant per board configuration. Signed-off-by: Peter Ujfalusi --- sound/soc/intel/boards/skl_hda_dsp_generic.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/sound/soc/intel/boards/skl_hda_dsp_generic.c b/sound/soc/intel/boards/skl_hda_dsp_generic.c index 08e5e22de9da43..4c6dddf852b97e 100644 --- a/sound/soc/intel/boards/skl_hda_dsp_generic.c +++ b/sound/soc/intel/boards/skl_hda_dsp_generic.c @@ -53,6 +53,7 @@ static void skl_set_hda_codec_autosuspend_delay(struct snd_soc_card *card) #define DMIC01_BE_ID 6 #define DMIC16K_BE_ID 7 #define BT_OFFLOAD_BE_ID 8 +#define UAOL_BE_ID 9 #define HDMI_IN_BE_ID 13 #define HDA_LINK_ORDER SOF_LINK_ORDER(SOF_LINK_IDISP_HDMI, \ @@ -60,16 +61,16 @@ static void skl_set_hda_codec_autosuspend_delay(struct snd_soc_card *card) SOF_LINK_DMIC01, \ SOF_LINK_DMIC16K, \ SOF_LINK_BT_OFFLOAD, \ - SOF_LINK_HDMI_IN, \ - SOF_LINK_NONE) + SOF_LINK_UAOL, \ + SOF_LINK_HDMI_IN) #define HDA_LINK_IDS SOF_LINK_ORDER(IDISP_HDMI_BE_ID, \ HDA_BE_ID, \ DMIC01_BE_ID, \ DMIC16K_BE_ID, \ BT_OFFLOAD_BE_ID, \ - HDMI_IN_BE_ID, \ - 0) + UAOL_BE_ID, \ + HDMI_IN_BE_ID) static unsigned long skl_hda_get_board_quirk(struct snd_soc_acpi_mach_params *mach_params) @@ -130,6 +131,10 @@ static int skl_hda_audio_probe(struct platform_device *pdev) if (mach->mach_params.codec_mask & IDISP_CODEC_MASK) ctx->hdmi.idisp_codec = true; + ctx->uaol_link_mask = mach->mach_params.uaol_link_mask; + ctx->uaol_playback_num = mach->mach_params.uaol_playback_num; + ctx->uaol_capture_num = mach->mach_params.uaol_capture_num; + ctx->link_order_overwrite = HDA_LINK_ORDER; ctx->link_id_overwrite = HDA_LINK_IDS; From fb40f04b49c2beb06e8e4ebf8df13fdb7627f319 Mon Sep 17 00:00:00 2001 From: Peter Ujfalusi Date: Wed, 4 Nov 2026 15:11:26 +0200 Subject: [PATCH 52/53] ASoC: SOF: docs: Describe the UAOL architecture Add Documentation/sound/soc/sof-uaol.rst, describing the USB audio offload on Intel platforms with SOF: how the xHCI Audio Sideband, the USB audio offload library, the ASoC USB framework, the SOF HDA driver and the machine drivers fit together, what the user visible interface is (the route controls and the jack), how a stream is acquired and released, how the UAOL gateway is configured and how the firmware's FIFO layout decides where an endpoint may be placed, the asynchronous playback policy, and the current limits. Signed-off-by: Peter Ujfalusi --- Documentation/sound/soc/index.rst | 1 + Documentation/sound/soc/sof-uaol.rst | 1232 ++++++++++++++++++++++++++ Documentation/sound/soc/usb.rst | 1 + 3 files changed, 1234 insertions(+) create mode 100644 Documentation/sound/soc/sof-uaol.rst diff --git a/Documentation/sound/soc/index.rst b/Documentation/sound/soc/index.rst index 22878d73f7a54a..2d2c0f35e6d131 100644 --- a/Documentation/sound/soc/index.rst +++ b/Documentation/sound/soc/index.rst @@ -19,3 +19,4 @@ The documentation is split into the following sections:- dpcm codec-to-codec usb + sof-uaol diff --git a/Documentation/sound/soc/sof-uaol.rst b/Documentation/sound/soc/sof-uaol.rst new file mode 100644 index 00000000000000..e788f6a236af97 --- /dev/null +++ b/Documentation/sound/soc/sof-uaol.rst @@ -0,0 +1,1232 @@ +.. SPDX-License-Identifier: GPL-2.0 + +=================================== +SOF USB Audio Offload Link (UAOL) +=================================== + +Scope +===== + +This document describes the Linux implementation of USB audio offload on +Intel platforms with SOF. It covers the Linux host side, including the +configuration the host hands to the DSP firmware. It does not describe +firmware internals and is not a hardware specification. + +For the generic ASoC USB framework, see :doc:`usb`. Of the two control +models it describes, SOF uses the platform card route controls: writable +``USB Offload Playback Route`` and ``USB Offload Capture Route`` +controls, one per backend, on the *SOF platform* card, which select the USB +device the offloaded stream of that backend goes to. The read-only +``USB Offload ... Card/PCM Route`` controls on the *USB* sound card are not +created. + +.. contents:: In this document + :local: + :depth: 2 + +Architecture and design principles +================================== + +The core idea: a second path into the same USB PCM +-------------------------------------------------- + +Offload does not replace the normal USB audio path, and does not change the +USB sound card. snd-usb-audio still creates the USB card and its PCMs, and +those PCMs still move audio the usual way: the CPU submits URBs to the xHCI +controller. This document calls that the *URB path*. + +The SOF card adds a second way into the same USB PCM, the *offload path*. +A SOF frontend PCM carries the audio through the DSP and the UAOL link +directly into the USB PCM that a route control has selected. + +The diagram below shows, for playback, how a sound server such as PipeWire +or PulseAudio could choose between the two paths for a USB headset, and the +route the audio then takes. Capture uses the same paths in the opposite +direction:: + + +--------------------------------------------+ + | Desktop / Sound Server (PW / PA) | + | Target: USB Headset (Card #1, PCM #0) | + +---------------------+----------------------+ + | + +-----------------+-----------------+ + | 'Use USB Offload' Checkbox? | + [ UNCHECKED ] [ CHECKED ] + | | + | +----------v----------+ + | | 1. Has 'USB Offload | + | | Jack' (Plugged)? | + | +----+-----------+----+ + | No | Yes | + | | v + | | +-----------------+ + | | | 2. Is Headset | + | | | in Route? | + | | +----+-------+----+ + | | No | Yes | + | |<------+ | + | | v + v v [ UAOL Path ] + [ Standard URB Path ] <------------------+ 1. Set Route + Open: hw:, 2. Open: hw:, + | | + v v + +--------------------+ +--------------------+ + | snd-usb-audio | | SOF Frontend | + | Host Driver | +---------+----------+ + +---------+----------+ | Host DMA + | v + Submits | URBs +--------------------+ + v | Intel Audio DSP | + +--------------------+ | pipeline -> UAOL | + | System Memory | | DAI copier (IPC4 | + | Transfer Rings | | gateway, set up | + +---------+----------+ | by the UAOL BE) | + | +---------+----------+ + Periodic DMA | Link DMA over + | | the dedicated + | | UAOL link + | | + v v + +---------------------------------------------------------------------+ + | xHCI Controller Engine | + | [ System Memory Rings (URB) ] vs. [ Audio Sideband (UAOL) ] | + +----------------------------------+----------------------------------+ + | + Isochronous Packets + v + +-------------------------------+ + | Physical USB Headset Endpoint | + +-------------------------------+ + +The decision part of the diagram is userspace policy, shown as an example. +The "Use USB Offload" switch belongs to the sound server; the kernel provides +no such setting. What the kernel provides are the inputs to the decision and +the means to act on it: + +1. The ``USB Offload Jack`` tells whether any USB device can be offloaded at + all. +2. The items of the ``USB Offload Playback Route`` and ``USB Offload + Capture Route`` controls tell which ones. Each item name starts with + ``,``, the USB card and PCM numbers, so the sound server can + match the device it targets. +3. Selecting the item in the route control of a backend and opening the SOF + frontend PCM routed to that backend starts offload. The selection fails + with ``-EBUSY`` while an offloaded stream is set up on that backend. + +Opening the SOF frontend can still fail later, in ``hw_params``. The usual +reasons are that the USB card's PCM is already open, or that the device +cannot run the requested format over UAOL. The sound server should then fall +back to the URB path, just as it does when the jack or the route item is +missing. + +For userspace the difference between the two paths is which PCM device it +opens: + +* **Same device, same card, same controls.** The USB device, its USB card, + and the card's mixer controls stay as they are. Device controls such as the + USB device's own volume and mute remain on the USB card and apply to + whichever path is in use, because snd-usb-audio still owns the device. +* **Same userspace rules.** Whatever userspace applies to the USB card, for + example its UCM configuration, applies to the offload path as well. Only + the PCM device differs: the SOF card's frontend instead of the USB card's + PCM. +* **One path at a time per USB PCM and direction.** Both paths share the USB + substream's ownership flag. While the offload path holds it, opening the + USB card's PCM fails with ``-EBUSY``. While the USB card's PCM is open, the + offload stream's ``hw_params`` fails with ``-EBUSY``. A running stream is + never moved from one path to the other. +* **Offload is optional.** The URB path is always available, and is the + fallback whenever the offload path is not. + +What is offloaded? +------------------ + +UAOL (USB Audio Offload Link) is a dedicated hardware link between the Intel +audio controller and the xHCI controller's Audio Sideband interface. Once an +isochronous endpoint is assigned an Audio Sideband resource, the xHCI +controller exchanges that endpoint's audio data with the DSP over UAOL instead +of with transfer rings in system memory. The xHCI controller still schedules +the USB transactions; the DSP produces or consumes the audio. + +The CPU remains in charge of everything else: USB enumeration, control +requests and interface configuration, PCM negotiation, resource allocation, +and the stream lifecycle. The goal is to take the CPU off the steady-state +audio data path so that the host can stay idle for longer. + +This is not compressed-audio offload: the DSP carries PCM audio. Audio is +offloaded only when an application opens a SOF PCM that the topology routes +to a UAOL backend; opening the USB card's own PCM always uses the URB path. + +Two views of the same playback stream are useful. Capture is the mirror image, +with audio flowing upwards:: + + ASoC / ALSA view Audio data path + ---------------- --------------- + Application + | + v + FE: SOF platform-card PCM ...... Host DMA (system memory -> DSP) + | | + | DPCM, connected by v + | topology routes DSP pipeline (host copier, + | processing modules) + v | + BE: UAOL0-Play0 (no PCM) ........ UAOL DAI copier (IPC4 gateway) + CPU DAI "UAOL0 Playback Pin0" | + v + Audio link DMA + | + v + UAOL link + | + v + xHCI Audio Sideband + | + v + USB isochronous OUT endpoint + + Explicit playback feedback (separate return path): + USB feedback IN endpoint -> xHCI Audio Sideband -> UAOL link + -> feedback link DMA (capture direction) -> UAOL DAI copier + +The frontend (FE) is the PCM that the application opens. The backend (BE) is +the ASoC DAI link on the UAOL side. Topology routes connect the FE to the BE +through DPCM. The dotted lines link each ASoC object to the part of the data +path it controls. The FE controls the host DMA. The BE's CPU DAI callbacks +acquire the USB endpoints, set up the link DMAs, and configure the UAOL DAI +copier, which is the DSP-side gateway to the link. The BE is not a separate +processing stage. + +Control and setup run on the CPU, apart from the audio path:: + + Hotplug (device discovery): + snd-usb-audio enumeration / platform callbacks + -> USB offload library (candidate filter, sideband registration) + -> SOF hotplug callbacks + -> ASoC USB device list, route controls, and SOF jack + + Stream setup (BE hw_params): + SOF UAOL BE DAI + -> USB offload library + -> snd-usb-audio endpoint preparation + xHCI sideband resource binding + -> endpoint/resource identity returned to SOF + -> IPC4 gateway configuration and link DMA setup + +Unlike the Qualcomm USB offload design, the DSP does not get the xHCI transfer +and event rings and does not queue TRBs itself. The SOF path calls none of the +sideband ring-export or secondary-interrupter APIs; the endpoint-to-resource +assignment alone routes the data. + +Ownership boundaries +-------------------- + +The central rule is to retain each subsystem's existing responsibility: + +* **snd-usb-audio owns the USB device.** No competing USB class driver claims + it. Its descriptor parsing, format lists, interface configuration, endpoint + setup, and runtime-PM helpers are reused. +* **The USB offload library owns the USB-side offload records.** It filters + candidates, registers Audio Sideband access, claims USB substreams, and + acquires/releases endpoint resources. It does not build SOF IPC messages, + program audio registers, or own the ASoC USB port. +* **SOF owns the DSP and audio-controller side.** It publishes the ASoC USB + port, translates hotplug identities into ASoC USB devices, manages the jack, + allocates link DMAs, and completes the firmware gateway configuration. +* **The ASoC USB framework owns route policy/state.** It keeps the available + device list and the selected USB device/PCM pair for each direction. +* **The xHCI driver owns controller-side endpoint binding.** It reserves Audio + Sideband resources and attaches them to endpoints with xHCI + extended-property commands. +* **The topology and machine driver define the audio graph.** A route control + only chooses which USB device a UAOL backend talks to. It does not create a + frontend, load a topology, or change the DSP processing graph. + +A USB device moves through three states, which are easily confused: + +Available + The device passed the candidate filter, registered for Audio Sideband use, + and was published to the SOF port. It appears in the route controls and + counts toward the jack state. + +Selected + A direction's route control points at one of the device's USB PCMs. + Selecting a route does not claim the USB substream or allocate any endpoint + resource. + +Acquired + The BE's ``hw_params`` has claimed the USB substream and bound its active + endpoints to Audio Sideband resources. The actual endpoint and resource + identities are only known from this point. + +A device can be available and selected and still fail acquisition, for example +because its USB PCM is already open on the USB card. A stream that is already +running on the USB card is never moved to the DSP. + +Platform discovery and card construction +======================================== + +Build and runtime gates +----------------------- + +``CONFIG_SND_SOC_SOF_INTEL_USB_OFFLOAD`` enables SOF's integration. Its +dependencies include ``SND_SOC_SOF_HDA_GENERIC``, ``SND_SOC_SOF_HDA_LINK``, +``SND_USB_OFFLOAD``, and ``SND_SOC_USB``; Kconfig also prevents incompatible +built-in/module combinations. The USB library requires ``SND_USB_AUDIO``, +``USB_XHCI_HCD``, and ``USB_XHCI_SIDEBAND``. + +Runtime discovery has two independent inputs: + +1. HDA extended-multilink enumeration finds Intel UAOL alternate links. +2. The booted IPC4 firmware reports ``SOF_IPC4_FW_CFG_UAOL_SUPPORT`` and + ``SOF_IPC4_HW_CFG_INTEL_UAOL_CAPS``. The latter supplies the link count, + stream capabilities, and transmit/receive FIFO sizes. + +The loader checks the size of the capability payload before keeping it. +``hda_uaol_link_caps()`` returns the cached capabilities of a link, and +``hda_uaol_available()`` tells whether the firmware supports UAOL at all. + +``hda_machine_register()`` registers with the offload library +(``usb_offload_register()``) after the firmware configuration has been +parsed, then registers the machine driver. The registration passes a +``struct usb_offload_res``: the SOF device, the hotplug and resource-range +callbacks, and two policy flags, ``s24_3le_as_s24_le`` and +``async_playback_on_host`` (see `Negotiation rules`_ and `Asynchronous +playback stays on the host`_). The library knows nothing about UAOL links or +xHCI controllers: it has one context per registered host, and the host +answers for the resources. + +A failure to set up offload does not stop the rest of the sound card from +registering; it is logged at debug level only. A working sound card +therefore does not prove that USB offload is working. + +Backend links and topology +-------------------------- + +``mach_params.uaol_link_mask`` tells the machine driver which UAOL links +exist, and ``mach_params.uaol_playback_num`` and ``uaol_capture_num`` how +many streams per direction each link runs at a time. ``hda_finalize_mach_params()`` +fills the stream counts in from the firmware's link capabilities once the +firmware has booted: a link with ``T`` output and ``I`` input streams carries +``T`` playback and ``I - T`` capture streams, the remaining ``T`` input +streams being the clock feedback of the playback streams (see `FIFO +partitioning and constraints`_). The static DAI count is the ceiling. It +clears the mask if UAOL support is not built in or the firmware does not +support it. The ``snd_sof_intel_hda_generic.uaol_link_mask`` module +parameter, set at load time, can override the discovered mask. Setting bits +for links that were not discovered produces a warning; it cannot add hardware +that is not there. + +The generic HDA board helper and the SoundWire machine driver add one +unidirectional backend per stream of each enabled link. With two streams per +direction: + +.. list-table:: + :header-rows: 1 + :widths: 20 30 50 + + * - Direction + - Backend link name + - CPU DAI name + * - Playback + - ``UAOL0-Play0``, ``UAOL0-Play1`` + - ``UAOL0 Playback Pin0``, ``UAOL0 Playback Pin1`` + * - Capture + - ``UAOL0-Cap0``, ``UAOL0-Cap1`` + - ``UAOL0 Capture Pin0``, ``UAOL0 Capture Pin1`` + +These are no-PCM backend links with a dummy codec. They are not user-visible +PCMs and not USB sound cards. In the names, the first ``0`` is the UAOL link +index and the trailing number the ordinal of the stream among those of its +direction. The ordinal only tells the backends apart: it is **not** the UAOL +stream the backend runs on, which is decided by the sideband resource its +endpoint gets at acquisition (see `Resource IDs, UAOL stream indices, and DMA +tags`_). Two backends of a direction are interchangeable. + +The backends are created ordinal first, playback before capture within an +ordinal: ``Play0``, ``Cap0``, ``Play1``, ``Cap1``. The first pair therefore +keeps two consecutive backend IDs whatever the stream count, and a topology +describing one stream per direction stays valid; a backend the topology +leaves out is simply unused. + +Each direction gets its own backend and DAI because the backend ``hw_params`` +fixup is not told the stream direction. A duplex backend could not tell +whether to use the topology's playback or capture DAI copier. + +The backend names are abbreviated on purpose. The topology names the DAI +copier widget ``dai-copier.UAOL..``, and alsa-lib keeps +element ids in ``SNDRV_CTL_ELEM_ID_NAME_MAXLEN`` bytes: from 38 characters on, +the ids of the widget's ``dai.word`` and ``dai.string`` tuple sections collide +and one set is silently dropped from the topology. + +A matching topology must provide the frontends, the routes, and the +``SOF_DAI_INTEL_UAOL`` DAI copiers. The topology decides the frontend PCM +numbers and the processing graph; the backend names above do not. + +In this implementation the HDA code limits the usable UAOL link count to one. +The multilink code has no per-instance offset for UAOL, so further sublinks +would all alias to sublink zero. Within the link, every backend has an ASoC +USB route of its own, so with two devices connected, or one device with two +PCM pairs, both backends of a direction can run at the same time, each on its +own device or PCM. + +Publishing the ASoC USB port +---------------------------- + +The ASoC USB port (``struct snd_soc_usb``) is where USB devices are published +and where the route state lives. There is one per audio controller. The first +UAOL CPU DAI to probe in the card creates it, and the other UAOL DAIs reuse +it. Every UAOL CPU DAI also claims a ``struct hda_uaol_stream`` of its own at +probe time; that is where the acquired device, the backend substream, the +feedback link DMA and the frontend narrowing rules of its backend live, and +which ASoC USB route of its direction the backend plays to. The link and the +route are parsed from the DAI name, ``UAOL Pin``: +``UAOL0 Playback Pin1`` is playback route 1 of link 0. + +The probe sets the ``is_route_busy`` callback, which reports whether the +backend of a route holds an acquired stream, gives the port as many routes +per direction as the link has streams (``snd_soc_usb_set_num_routes()``), +creates the ``USB Offload Jack``, creates the route controls, stores the +port in ``hdev->uaol_usb``, and finally calls ``snd_soc_usb_add_port()``. The +jack has to exist before the port is added, because adding the port +rediscovers devices that were connected earlier, and those report the jack. + +ASoC USB normally finds the port through a device-tree ``usb-soc-be`` +phandle. On these ACPI/PCI platforms there is none, so the port is found by +its component's device: SOF passes ``sdev->dev`` to +``snd_soc_usb_connect()`` and ``snd_soc_usb_disconnect()``. + +If the offload library is not running, the probe still creates the jack and +leaves it unplugged, but adds no port and no route controls. + +USB discovery and route population +================================== + +Candidate detection +------------------- + +The library registers a ``snd_usb_platform_ops`` table with connect and +disconnect callbacks. snd-usb-audio keeps driving the device and calls these +callbacks as its interfaces are probed and removed. + +The bridge accepts a device only if it is an audio-class function usb-audio +has set up PCMs for *and* registers for xHCI Audio Sideband use: + +* The device must be full-speed or high-speed. SuperSpeed devices are rejected. +* The descriptors are not parsed again: the streams, formats and endpoints are + the ones snd-usb-audio found, with whatever it rejected as malformed already + left out. A device without any PCM that can be offloaded is rejected. +* ``snd_usb_offload.offload_disable=1`` disables candidate selection. + +Sideband registration needs a valid xHCI slot and a controller with Audio +Sideband capability: xHCI 1.2 or later, extended-property support, and at +least one audio resource. + +Registration uses the ``XHCI_SIDEBAND_AUDIO`` type, not the vendor sideband. +It does not allocate any endpoint resources; that happens only at +acquisition. + +Asynchronous playback stays on the host +--------------------------------------- + +An asynchronous OUT endpoint runs off the device's own clock, and the sender +has to follow that clock through the feedback the device provides. Whether +the DSP applies that feedback well enough is the host's call: the SOF +firmware spreads samples linearly over the service interval, which is +audible on the kind of equipment that uses asynchronous playback in the +first place: audio interfaces and mixers. Implicit feedback is no better: its +sync source is a capture data endpoint, which the DSP cannot use, so such a +stream would run with no rate tracking at all. + +SOF therefore registers with ``async_playback_on_host`` set, and the library +keeps asynchronous playback on the URB path. The decision is made per +``audioformat``, on the sync type of the data endpoint and its implicit +feedback flag, so it covers explicit and implicit feedback alike and does not +depend on the snd-usb-audio quirks that choose between the two: + +* A playback PCM whose formats all need feedback is left out of the device + identity, so it never appears in the playback route controls. The + library logs ``PCM playback needs clock feedback, not offloaded`` once + per connect. +* Formats that need feedback are skipped during negotiation, so a PCM that + mixes adaptive and asynchronous alternate settings only negotiates the + adaptive ones. +* ``usb_offload_acquire_stream()`` refuses a format that still needs + feedback with ``-EOPNOTSUPP``. + +Capture is not affected by its own sync type: an asynchronous IN endpoint +needs no feedback. It is affected when its data endpoint is the implicit +feedback source of a playback format the host keeps, as with the Behringer +UMC204HD's 24-bit playback: that endpoint is the playback's clock, and once +the DSP owned it the host could not run the playback at all. Such a capture +format stays on the host as well, and the library logs ``PCM capture is +tied to the playback, not offloaded``. A device left with no PCM +to offload in either direction is not reported to SOF at all, the library +logs ``no PCM can be offloaded``. + +``snd_usb_offload.allow_feedback=1`` overrides the host's flag for testing +the feedback path. Explicit feedback is then claimed as described under +`Acquiring the USB endpoints`_; implicit feedback is still left unused. + +From a USB chip to enumerated control items +------------------------------------------- + +The connection sequence is:: + + snd-usb-audio connect_cb(chip) + usb_offload_chip_is_candidate() speed/class check + xhci_sideband_register() controller eligibility + usb_offload_ua_build_id() card/chip/PCM identity + usb_offload_connect() + hda_uaol_notify_connect() + allocate hda_uaol_dev + copy identity and name storage + snd_soc_usb_connect(SOF device, &record->sdev) + append to available_devices + notify control INFO / VALUE changes + report SND_JACK_USB + +``usb_offload_ua_build_id()`` walks ``chip->pcm_list``. A USB PCM adds a +playback or capture entry if it has formats for that direction that can be +offloaded (see `Asynchronous playback stays on the host`_). The identity +passed to SOF holds the ALSA card number, the USB chip index, and the PCM +device numbers and names. At most eight PCMs per direction are exported. + +Each entry's display name has this form, with the product name cut to 24 +characters and the PCM name to 16:: + + , | - + +so that several PCMs on one device, and identical devices on different cards, +can be told apart. The library passes the identity by value, and only for the +duration of the callback. ASoC USB, however, keeps pointers to it and links it +into its device list, so SOF copies it, names included, into its own +``hda_uaol_dev`` record. + +There is no control per device. A published port has one *route* per +backend of each direction, and a control per route: + +* ``USB Offload 0 Playback Route``, ``USB Offload 1 Playback Route``, ... +* ``USB Offload 0 Capture Route``, ``USB Offload 1 Capture Route``, ... + +The number in the name is the backend ordinal, the same as in +``UAOL0-Play``: route ``n`` of a direction is what backend ``n`` of that +direction plays to. The number is always present, also for a direction with +a single backend, so the names do not depend on the stream count. The +topologies name the frontend PCMs with the same ordinal, ``USB Offload 0`` +and ``USB Offload 1`` (``Deepbuffer USB Offload `` for the deep buffers), +so that a PCM, its route controls and the ``USB Offload Jack`` share a name +and pair up by number. + +Each is a writable enumerated control on the SOF platform card. Its item list +is rebuilt on every query. ASoC USB lists every available ``(device, USB +PCM)`` pair for that direction, in device-connection order and then PCM +order. A playback-only device adds no capture items, and the reverse. All +routes of a direction list the same items. + +An item number is a position in this live list. It is **not** a card number, +USB PCM number, endpoint number, or sideband resource ID. Hotplug can shift +positions even while the selected device stays the same, so userspace should +re-read the item list when it gets a control notification. + +Selection policy and changes +---------------------------- + +Any route can be pointed at any available pair, including a pair another +route of the direction already points at. A ``(device, USB PCM)`` pair can +only be offloaded once, though: of two routes pointing at the same pair, the +first backend to acquire it gets it and the second fails ``hw_params`` with +``-EBUSY`` until the first releases it. Swapping two devices between two +routes is therefore a plain sequence of two writes, through an intermediate +state where both routes point at the same device. + +A route nothing was picked for follows its default, which ASoC USB works out +whenever the route is queried: route N gets the Nth available pair of its +direction, wrapping around when there are fewer pairs than routes. A pick, +by a write to the control, overrides it until the device goes away. With two +backends per direction, one connected device offering one PCM pair puts both +routes on it; when a second device, or a second PCM of the first, appears, +route 1 follows to it, unless it was picked. Playback and capture routes are +independent of each other. + +When no pair is available, the control has one item, ``None``, and reads as +item zero. ``None`` only marks an empty list. Once devices are available it +disappears, so it cannot be used to turn offload off. + +A write resolves the item number to a device/PCM pair: + +* Writing the pair that is already selected does nothing. +* Writing an item number past the end of the list fails with ``-EINVAL``, + except item zero on an empty list, which does nothing. +* Selecting a different pair fails with ``-EBUSY`` while SOF holds an + acquired stream on that route's backend, from BE ``hw_params`` until + ``hw_free`` or the unplug of the device, whether or not the stream is + running. +* A write which changes the pair while the route's backend is acquiring its + stream is not refused, the backend is not busy yet. The backend reads the + selection back once it holds the stream and, finding it changed, releases + the stream again and fails its ``hw_params`` (or ``prepare`` after a + system suspend) with ``-EBUSY``. +* Otherwise the new pair becomes the selection. The next acquisition uses it. + The write itself allocates no USB or xHCI resources. + +A successful selection does not reserve any xHCI resource. + +On disconnect, the device's entries are removed. Every route that pointed at +the device is cleared, and then every empty route gets its default again, if +any pair is left. Connect and disconnect send +``SNDRV_CTL_EVENT_MASK_INFO`` to every route control of the direction and +add ``SNDRV_CTL_EVENT_MASK_VALUE`` where the selection changed. A stream +already acquired on the removed device is not moved to the new selection; see +`Physical disconnect`_. + +Probe ordering and rediscovery +------------------------------ + +USB enumeration, DSP startup, and machine-card binding need not occur in a +fixed order. Two rediscovery points close the gaps: + +1. Registering with the library installs the USB platform callbacks and + re-emits connect callbacks for USB chips that already exist. +2. Port publication calls ``snd_usb_rediscover_devices()`` again, after the + SOF card has somewhere to route them. + +If the SOF port does not exist yet, ``hda_uaol_notify_connect()`` publishes +nothing but still returns success. The library keeps its record of the +device and notifies SOF again on the next rediscovery. Both the library and +SOF ignore devices they already know, so rediscovery does not create +duplicate entries. + +PCM negotiation and offload acquisition +======================================= + +Negotiation rules +----------------- + +What the topology allows on a frontend is not necessarily what the selected +USB device can play. When a frontend that routes to a UAOL backend is opened, +``hda_apply_constraint_from_be()`` adds hardware rules derived from that +backend. + +There are three sets of rules, each covering rate, channels, and format: + +* **Link FIFO rules** limit the byte rate to what fits in the stream's share + of the UAOL FIFO. They apply even when no USB device is selected. The + service interval is not known yet, so they assume the shortest one, + 125 us, and the real fit is checked after the acquisition (see + `Acquiring the USB endpoints`_). +* **USB device rules** limit the parameters to the selected USB PCM's + ``audioformat`` list, leaving out formats that need clock feedback (see + `Asynchronous playback stays on the host`_). They look up the current + selection each time + they run, so a route change or hotplug after open is taken into account. + Each rule also depends on the parameter it refines, so a request which + changes only that one is checked as well. +* **Shared backend rules** apply to the second frontend of a backend, the + deep buffer. They allow only the rate, channels, and format the backend + was opened with, and allow everything while the backend is idle. + +The rules depend on each other: choosing a channel count, for example, can +narrow the allowed rates and formats. The USB rules produce ranges for rate +and channels and a mask for format; they do not reproduce gaps in a device's +discrete rate list. Acquisition then does an exact format lookup and can +still reject a combination that passed negotiation. + +SOF's pipeline represents 24-bit audio only as ``S24_LE`` (24 valid bits in +a 4-byte container), while many USB devices pack 24-bit audio in 3 bytes +(``S24_3LE``). SOF therefore sets ``s24_3le_as_s24_le`` when registering with +the library, and devices offering ``S24_3LE`` are also offered as ``S24_LE`` during +negotiation. Acquisition then looks for the format in every PCM of the device +first as the frontend asked for it, and only then as ``S24_3LE``, so a device +with ``S24_LE`` at the chosen rate and channel count keeps it. It reports the +format the stream runs in as ``usb_offload_stream_hw.format``. SOF sets the BE +parameters to it right after the acquire, and the DSP packs the samples on +their way to the link. DPCM hands the BE ``hw_params`` a copy of the +parameters, while the common prepare programs the link DMA again after a stop +from the BE's own copy, so ``uaol_dai_prepare()`` sets that copy to the format +the stream was acquired with; otherwise a stream recovering from an xrun would +run its link DMA with the 4-byte container against the 3-byte one of the +device. + +Acquiring the USB endpoints +--------------------------- + +``uaol_dai_hw_params()`` acquires the USB side first, through +``uaol_dai_start_stream()``, and then runs the common link DMA ``hw_params`` +setup also used for SSP and DMIC. A backend fed by two frontends, the +playback PCM and its deep buffer, runs ``hw_params`` once per frontend while +the first one has not started yet; the USB side is acquired by the first call +only and kept until the backend's ``hw_free``, which DPCM runs when the last +frontend lets go. + +The two frontends share one endpoint and one mixer, so the second one can +only run what the first one opened the backend with. The backend stores the +rate, channels, and format of the first call and refuses a later one that +differs with ``-EBUSY``, before the core prepares anything for it. A +frontend failing further down would otherwise take the widgets the first one +prepared with it. The shared backend rules above keep this from being +reached in the normal case. + +1. Map the selected ASoC USB device back to its ``usb_device`` and the + selected USB PCM number. If nothing is selected, or the library is not + running, this fails with ``-ENODEV``. +2. Call ``usb_offload_acquire_stream()`` with the direction, USB PCM number, + and BE parameters; it settles the 24-bit container itself (see above). +3. The library looks only at the selected USB PCM for a format that exactly + matches. If there is none, or the match needs clock feedback the host + keeps to itself, this fails with ``-EOPNOTSUPP``. A USB PCM is + offloaded once, so a PCM already acquired fails with ``-EBUSY``; the other + PCMs of the same device are not affected and can be acquired alongside. +4. Claim the USB substream with ``snd_usb_offload_claim_substream()``. This + uses the same ``subs->opened`` flag, under ``chip->mutex``, as opening the + USB card's PCM, so only one of the two can own the substream. +5. Resume the USB device, call ``snd_usb_hw_params()``, claim the data + endpoint and an explicit feedback endpoint with + ``snd_usb_endpoint_claim_offload()``, and prepare the sync endpoint, if + any, and the data endpoint. This selects the streaming alternate setting, + which makes the xHCI driver set up the endpoint. No URBs are submitted: + the host does not stream the data itself. A claimed endpoint cannot be + opened by another stream, the host's ``hw_params`` fails with ``-EBUSY``. + An endpoint which another stream has open already cannot be claimed and + the acquisition fails with ``-EBUSY``: with implicit feedback the capture + data endpoint is also the sync source of the device's playback. A freshly + prepared data endpoint gets the device's format quirk run, as + ``snd_usb_pcm_prepare()`` does, which sets the sample rate of devices that + take it through vendor requests. A quirk which leaves the host to drop + bytes from the stream or its packets to align the samples, as for the + MacroSilicon MS2100/MS2109 capture cards, fails the acquisition with + ``-EOPNOTSUPP``: only the URB path does that. +6. Reserve an Audio Sideband resource for the data endpoint and attach it + with ``xhci_sideband_add_endpoint()``. From now on the controller routes + this endpoint's data over UAOL. Which resources the endpoint may use is + SOF's choice, see `Resource IDs, UAOL stream indices, and DMA tags`_. +7. If the stream has an explicit feedback endpoint, reserve and attach a + resource for it too. An implicit-feedback source is **not** claimed: it is + another interface's audio data endpoint, not a feedback value the DSP can + use. +8. Query the xHCI endpoint properties and return them in + ``usb_offload_stream_hw``: the resource IDs actually assigned, the xHCI + controller's PCI bus/device/function, the slot ID, endpoint addresses, + maximum packet sizes, intervals, speed, whether a transaction translator + is used, and the USB PCM number the stream was acquired on, which names + the stream to ``usb_offload_quiesce_stream()``, + ``usb_offload_restart_stream()`` and ``usb_offload_release_stream()`` + together with the direction. +9. Back in SOF, check that the payload of two service intervals, rounded to + whole sample frames, fits in the stream's share of the link FIFO. The + interval is the one the library reports. A payload exactly as large as the + share fits; a larger one is refused with ``-EINVAL``, as an overrun is not + reported anywhere and only corrupts the audio. +10. Fill in the runtime fields of the UAOL gateway configuration, allocate + the feedback link DMA if needed, and record the acquired device and BE + substream. +11. Allocate the audio link DMA and add its IPC4 DMA configuration TLV. + +If any step fails, the steps already done are undone, including the USB +claim, and a failure in step 11 releases the USB side as well. On success, +the claim and the USB runtime-PM reference are held until the stream is +released. + +Resource IDs, UAOL stream indices, and DMA tags +----------------------------------------------- + +Several kinds of index are involved. They are independent of each other and +must not be mixed up: + +.. list-table:: + :header-rows: 1 + :widths: 35 65 + + * - Identifier + - Meaning + * - USB card / USB PCM number + - ALSA identity of the device, used by route selection. + * - USB endpoint address / xHCI slot ID + - The endpoint and its device, as the xHCI controller knows them. + * - xHCI Audio Sideband resource ID + - Allocated at acquisition. On Intel hosts, IDs 1--2 are for OUT + endpoints and IDs 3--6 for IN endpoints; SOF narrows these further + by function, see below. + * - UAOL stream index + - Sideband resource ID minus one. + * - Gateway node index + - The HDA link stream that hardware ties to a UAOL stream: + UAOL streams 0--5 map to link streams 13, 14, 15, 16, 29, 30. + * - HDA link DMA stream tag + - Allocated separately for the audio and feedback link DMAs and passed + to the firmware in DMA configuration TLVs. + +Resource IDs come from one allocator per xHCI controller, shared by all +devices on it, and are assigned when a stream is acquired. The firmware ties +the link's streams to functions through its FIFO layout: the ``T`` output +streams carry playback, the first ``I - T`` input streams carry capture, and +the last ``T`` input streams carry the clock feedback of the playback +streams. An endpoint on a stream outside its function's range would be +served from the wrong FIFO area. SOF therefore answers the library's +``get_resource_range`` callback with the resources of the endpoint's +function, and the library passes that range to +``xhci_sideband_alloc_resource_id()``, which reserves the first free one of +it; when all are taken the acquisition fails with ``-EBUSY``. With two output and four input streams this places +playback on resources 1--2, capture on 3--4, and feedback on 5--6. + +Within a range, neither the order in which devices were connected nor their +position in the route control predicts which ID a stream gets. SOF therefore +rebuilds the gateway binding from the values returned by each acquisition. + +``xhci_sideband_add_endpoint()`` issues the ``PROP_ECI_AUDIO`` / +``AUDIO_SUBTYPE_SET_RES`` extended-property command for the endpoint. This +command is what routes the endpoint to its sideband resource. Registering +the device or selecting a route does not. + +Firmware configuration and link DMA +=================================== + +Two-stage gateway configuration +------------------------------- + +The UAOL DAI copier's gateway configuration is filled in two stages, because +part of it depends on which USB device the stream ends up on. + +1. At topology load, ``sof_ipc4_widget_setup_uaol()`` allocates a + ``sof_ipc4_uaol_configuration_blob`` and fills in what does not depend on + the device: the link index, the FIFO layout, the timestamp conversion, and + all TLV headers. +2. At every acquisition, ``uaol_dai_set_endpoint()`` fills in the rest from + the ``usb_offload_stream_hw`` the library returned. + +The blob is a plain list of type/length/value records; lengths are in bytes. +All seven TLVs are always present, with the feedback fields zeroed when there +is no feedback endpoint: + +.. list-table:: + :header-rows: 1 + :widths: 10 30 60 + + * - Type + - Value + - Source + * - 0 + - xHCI bus/device/function + - PCI address of the xHCI controller, from the endpoint properties. + * - 1 + - Link/stream binding + - Topology link index; data resource ID minus one; if present, the + feedback resource ID minus one and the feedback endpoint's interval. + * - 2 + - FIFO start offsets + - Computed from the FIFO sizes and stream counts reported by the + firmware (see below). + * - 3 + - Data endpoint information + - Direction, endpoint number, slot ID, split/high-speed flags, maximum + packet size. + * - 4 + - Feedback endpoint information + - Same layout for the feedback endpoint, or zero if there is none. + * - 5 + - ART conversion + - Multiplier and divider both 1: the xHCI global time is already + counted in DSP timer ticks. + * - 6 + - Service interval, microseconds + - Derived from the data endpoint's ``bInterval``. + +For a high-speed data endpoint the service interval is +``125 * 2^(bInterval - 1)`` microseconds, for a full-speed one +``1000 * 2^(bInterval - 1)``: an isochronous ``bInterval`` is an exponent at +both speeds. ``bInterval`` is first clamped to 1--16. + +The copier's gateway node index is the link stream mapped from the data +resource ID (see the table above), not the audio link DMA's stream tag. When +the copier is created, the common IPC4 code appends the DMA configuration +TLVs to this blob and sends the result to the firmware. + +FIFO partitioning and constraints +--------------------------------- + +The UAOL link has one transmit and one receive FIFO, shared by all of its +streams. Which stream slot a USB endpoint uses follows from the sideband +resource ID it gets at acquisition. Even a single active stream can land on +any slot, so the FIFOs are divided among all the streams the hardware has, +not just the ones the topology uses. + +The firmware reports ``T`` output (transmit) streams and ``I`` input (receive) +streams. The receive FIFO also carries one eight-byte clock-feedback word per +transmit stream, at its start, and those feedback channels are counted in +``I``. The layout is:: + + audio_receive_streams = I - T + tx_size = round_down(max_tx_fifo_size / T, 8) + rx_base = T * 8 + rx_size = round_down((max_rx_fifo_size - rx_base) / + audio_receive_streams, 8) + +All start offsets are multiples of the eight-byte bus width. A link with no +transmit streams, or no receive streams left for audio, is rejected. + +A stream's share must hold two service intervals of audio, so that the DSP +can refill it while the link drains it. Samples occupy their valid width in +the FIFO, rounded up to whole bytes, not their container size. The +requirement is:: + + 2 * rate * channels * sample_bytes * service_interval_seconds + <= per_stream_fifo_size + +The service interval is only known once a device's alternate setting has +been chosen, which happens after negotiation. The frontend rules therefore +assume the shortest possible interval, 125 microseconds:: + + rate * channels * sample_bytes <= (fifo_size / 2) * 8000 + +This is only a coarse filter: with a longer service interval, a combination +that passes it can still overflow the FIFO. SOF does not repeat the check +with the real interval at acquisition. An overflow is not reported anywhere; +the audio is simply corrupted. Keep this in mind when a combination that +negotiated successfully produces corrupted audio. + +Audio and explicit-feedback DMAs +-------------------------------- + +UAOL uses the same IPC4 link DMA setup as SSP and DMIC, with a UAOL-specific +lookup of the multilink. The audio link DMA is described in DMA TLV slot +zero: HDA DMA method, preallocated by the host, ``dma_channel_id = tag - 1``, +and ``stream_id = tag``. + +A playback stream with an explicit feedback endpoint needs a second link DMA, +in the capture direction, to bring the feedback values to the DSP. It is part +of the playback stream, not a capture stream of its own: + +* It carries one 32-bit (``S32_LE``) mono value at the stream's rate. +* It is described in DMA TLV slot one; the firmware treats the second DMA as + the feedback channel. +* It is not the DAI's DMA data and is not registered as a ``link_substream``, + so the common code does not handle it. The UAOL DAI starts, stops, and + frees it itself. +* When it is freed, its TLV is cleared, so that a later device without a + feedback endpoint does not inherit a stale feedback DMA. + +Both link DMAs come from the shared HDA link-stream allocator. On ACE2+ it +avoids two hardware errata: a SoundWire stream cannot share a stream index +with an HDA, iDisp, or UAOL stream of the opposite direction, and a playback +stream index once used by HDA or iDisp cannot be used for another link type +until the controller is reset. Free sideband resources therefore do not +guarantee that the link DMAs can be allocated. + +Trigger, teardown, and power transitions +======================================== + +Start and pause +--------------- + +On ``START`` and ``PAUSE_RELEASE``, the UAOL DAI starts the feedback link DMA, +if there is one, and then runs the common HDA DAI trigger. That starts the +audio link DMA and sets the IPC4 pipeline to running. + +On ``PAUSE_PUSH``, the common trigger pauses the DSP pipeline and stops the +audio link DMA, and the UAOL DAI also stops the feedback link DMA. Unlike +``STOP`` and ``SUSPEND``, pausing leaves the USB endpoints running and keeps +all resources. + +Stop ordering is a hardware requirement +--------------------------------------- + +On ``STOP`` and ``SUSPEND`` the steps run in this order:: + + common HDA DAI trigger: + pause the DSP pipeline (the DSP stops its UAOL stream) + stop the audio link DMA, common link DMA cleanup + usb_offload_quiesce_stream(): + stop the xHCI feedback endpoint, if any + stop the xHCI data endpoint + stop the feedback link DMA + ... later: DAI copier freed, UAOL link powered down ... + hw_free: + free the audio link DMA + free the feedback link DMA, clear its TLV + detach endpoints and free sideband resources, USB hw_free, + drop the USB runtime-PM reference, release the substream claim + +The xHCI endpoints must be stopped in a narrow window: + +* **Not before the DSP has stopped.** The DSP cannot bring its UAOL stream to + idle while the xHCI controller no longer services an endpoint still bound + to the stream, and the stop times out. An endpoint already detached from + its resource, as on a disconnect, does not hold the stop up. +* **Not as late as hw_free.** By then the DAI copier has been freed and the + UAOL link powered down. An IN endpoint the controller still services, in + particular the feedback endpoint of a playback stream, has nowhere to + deliver its data and can lock up the controller. + +``usb_offload_quiesce_stream()`` only stops the endpoints. It keeps the +sideband resources and the substream claim, and +``usb_offload_release_stream()`` must still follow. Release detaches the +feedback endpoint before the data endpoint, frees their resource IDs, then +runs USB hw_free, drops the runtime-PM reference, and releases the claim. + +A stream can also be started again without ``hw_free`` in between, which is +how an xrun is recovered from: ``STOP``, ``prepare``, ``START``. A stopped +xHCI endpoint only runs again once its doorbell is rung, and starting the DSP +side of the link does not do that, so the ``START`` trigger of a quiesced +stream first restarts the endpoints with ``usb_offload_restart_stream()``: +the data endpoint, then the feedback endpoint. They then run before the DSP +starts its UAOL stream, as they do on the first start. A release forgets +that the endpoints were quiesced; after a system suspend the stream is +acquired again with fresh endpoints instead. + +Quiesce logs a failure to stop an endpoint only at debug level and still +returns success, and the trigger does not check its return value. A +successful stop therefore does not prove that the endpoints are idle. + +System suspend and resume +------------------------- + +System suspend frees the DSP pipeline and DAI copier, and resets the USB +device's streaming alternate setting, while the application keeps the PCM +open. The ``SUSPEND`` trigger records that the direction has to be acquired +again. ALSA only triggers a running stream, so a stream which is paused, or +not running, when the system suspends is not acquired again. + +On resume the backend gets only ``prepare``, not ``hw_params``. So +``uaol_dai_prepare()`` releases the old acquisition, acquires again with the +saved BE parameters, which rebuilds the endpoint binding, the gateway +configuration, and the feedback link DMA, and then runs the common prepare. +The resource IDs may differ from before the suspend. The widget stays +prepared over the suspend, so its IPC payload is not built again; the copier +setup refreshes the gateway configuration in it from the one the DAI +rewrote, along with the node ID and the DMA configuration TLVs. + +The flag is only cleared once the stream is acquired again, a failed +``prepare`` tries again the next time. A backend without a device, because +it was unplugged, fails ``prepare`` with ``-ENODEV`` rather than starting the +DSP on a gateway whose resources are gone. If the stream is closed while +suspended, the flag is cleared so that nothing is acquired again later. +Audio does not keep flowing through system suspend: the stream stops, and is +set up again when the PCM is prepared after resume. + +Jack semantics, unplug, and lifetime +==================================== + +What the jack means +------------------- + +SOF creates one card-wide jack, ``USB Offload Jack``, of type +``SND_JACK_USB``, and reports it directly with ``snd_jack_report()``. It does +not use the generic ``snd_soc_usb_setup_offload_jack()`` helper, which would +also hand the jack to the component through ``set_jack``. + +The jack answers one question: **is there at least one USB device that +offload can be routed to?** + +* It is reported plugged when a device is published to the SOF port. +* On disconnect it stays plugged as long as SOF still knows of another + device, and is reported unplugged when the last one goes away. +* A playback-only or a capture-only device is enough to plug it. +* It does not say which device is selected, whether a stream is running, + whether both directions are available, or whether acquisition will succeed. + +It is not the headset's own headphone or microphone jack, and there is no +jack per device. It switches no DAPM pins and moves no streams. Userspace +needs the route controls, and PCM negotiation, to know what it can play. + +With a working port and no offloadable device, both route controls show +``None`` and the jack is unplugged. A device rejected by the candidate filter +or by sideband registration, or one whose PCMs all stay with the host, never +plugs the jack. If the library failed to +probe altogether, the UAOL DAI still creates the jack, permanently unplugged, +but no route controls. + +Physical disconnect +------------------- + +Unplugging a device that has an acquired stream does not follow the orderly +stop sequence above. The USB side goes first, while the DSP may still be +running: + +1. snd-usb-audio calls the bridge's disconnect callback before marking the + chip as shut down. The bridge detaches the endpoints from their sideband + resources, frees the resources, and releases the USB substream. +2. The bridge then notifies SOF. ``hda_uaol_stop_frontends()`` finds the BE + substreams acquired on this device and, under the BE's DPCM lock, sets + every connected FE to ``SNDRV_PCM_STATE_DISCONNECTED``, stopping it first + if it is running. Without this the application would keep writing to a + PCM whose USB device no longer exists. The BE then forgets the device: + the USB side is already released and the ``usb_device`` goes away with + the disconnect, so the FE's later stop, ``hw_free``, and close do not + hand its stale pointer back to the bridge, where a newly plugged device + reusing the address could match. The feedback link DMA stays with the BE + until its ``hw_free``. +3. SOF removes its device record from ASoC USB, which drops the device's + route items and selects another device if one is available. The jack is + then updated from the remaining devices. +4. Finally the bridge unregisters the device's sideband handle. + +Card and library removal +------------------------ + +When the UAOL DAI is removed, SOF clears its port and jack pointers under its +UAOL mutex, removes and frees the port, and then frees the device records it +still holds. The jack belongs to the sound card and is freed with it. + +``usb_offload_unregister()`` lets snd-usb-audio run the disconnect callback +for every connected device and unregister the USB platform callbacks, both +under its ``register_mutex`` with ``snd_usb_release_platform_ops()``. The +bridge's device records are released and the host is told of the +disconnects as on an unplug, and no device can be freed under a record in +the meantime. Once it returns no callback can be running or start, and the +context is freed. + +Synchronization model +===================== + +The implementation has several distinct locks rather than one global stack +lock: + +* USB-audio ``register_mutex`` serializes platform-callback registration, + rediscovery, and USB-chip callbacks. +* ``usb_offload_ua_lock`` protects the active library context, per-device + bridge records, and USB-side stream acquisition/release. The library has + no lock of its own: the host callbacks are dispatched from the bridge. +* SOF ``uaol_mutex`` protects its published-port/device-record bookkeeping. +* ASoC USB ``ctx_mutex`` protects ports, available-device lists, selections, + and route-control operations. +* ``chip->mutex`` decides who owns a USB substream: the USB card's own PCM or + the offload path. +* The backend DPCM mutex stabilizes frontend/backend associations during + disconnect, with PCM stream locks used to change frontend state. +* The HDA bus register lock protects link-DMA allocator masks; each sideband + handle has its own mutex for sideband operations. + +Registration and rediscovery are never done while holding the library's +controller lock, and the bridge does not hold ``usb_offload_ua_lock`` while +it registers or unregisters with snd-usb-audio. The disconnect path drops +``usb_offload_ua_lock`` while it notifies SOF, because stopping the frontends +triggers the BE, which calls back into the library. Keep these rules in mind +when adding callbacks: a hotplug notification runs with locks held and can +re-enter other parts of the stack. + +Current limits and troubleshooting +================================== + +Current limits: + +* One usable UAOL link, with two streams per direction. A ``(device, USB + PCM)`` pair is offloaded once: the two backends of a direction need two + devices, or a device with two PCM pairs, to both run. +* One set of USB platform callbacks system-wide, and one active library + context, so one registered host. There is no mapping from xHCI + controllers to UAOL links yet: every eligible device on any controller is + published to the one host. +* On Intel hosts the xHCI controller has six Audio Sideband resources: two + for OUT and four for IN endpoints, of which SOF uses two for capture and + two for clock feedback. An explicit feedback endpoint uses one of the + feedback resources plus a capture-direction link DMA. +* A device that appears in the route controls is not guaranteed to work in + every format. +* SOF keeps asynchronous playback on the host unless ``allow_feedback`` is + set; see `Asynchronous playback stays on the host`_. +* Implicit feedback is not supported; such a sync source is left unused even + with ``allow_feedback``. Without it, the playback and the capture it syncs + to both stay on the host. With it, the two directions cannot be split, as + the capture data endpoint is also the sync source of the playback: while + the capture is offloaded, the device's playback cannot be opened on the + host, nor offloaded, and the other way around. A capture format with + implicit feedback of its own, which snd-usb-audio gives to a device that + only captures while its playback endpoint streams (the Behringer FCA1616), + always stays on the host: the DSP only gets the capture endpoint. + +When bring-up fails, work through the stack from the bottom up: + +1. **No UAOL backend:** check the Kconfig options, HDA multilink discovery, + the firmware's UAOL capability messages, the final ``uaol_link_mask``, and + that the topology's DAI names match the backends. +2. **No route controls:** check that SOF registered with the library and + that the UAOL DAI is bound in the card. A jack without route controls + means the library is not running. +3. **Route controls show only None:** check that the platform callbacks are + registered, the candidate-filter debug messages, the speed rule, + the controller's sideband capability, and the rediscovery after the card registered. A playback route that stays + ``None`` while the capture route lists the device points at an + asynchronous playback endpoint; the library's debug message names the + PCM. +4. **Route selected but hw_params fails:** check the selected direction and + USB PCM, whether the USB card's own PCM is open, exact format support, + free sideband resources, and free link DMAs, including for feedback. +5. **Corrupted audio with accepted parameters:** check sample packing, the + device's real service interval against the FIFO share (negotiation + assumes 125 microseconds), and the endpoint fields in the gateway + configuration. +6. **Stop timeout or xHCI controller hang:** check the stop order: DSP + pipeline, then xHCI endpoints including feedback, then copier free and + link power-down. +7. **Failure after resume:** check that ``prepare`` acquired the stream again + and rebuilt the bindings, rather than reusing the old resource IDs. + +The route controls and the jack are card-interface controls, so ``amixer`` +needs ``iface=CARD``. With ``$SOF_CARD`` set to the SOF card number:: + + amixer -c "$SOF_CARD" controls | grep 'USB Offload' + amixer -c "$SOF_CARD" cget iface=CARD,name='USB Offload 0 Playback Route' + amixer -c "$SOF_CARD" cget iface=CARD,name='USB Offload 0 Capture Route' + amixer -c "$SOF_CARD" cget iface=CARD,name='USB Offload Jack' + +To point the first playback backend at item zero of the current list, and +the second at item one:: + + amixer -c "$SOF_CARD" cset iface=CARD,name='USB Offload 0 Playback Route' 0 + amixer -c "$SOF_CARD" cset iface=CARD,name='USB Offload 1 Playback Route' 1 + +To exercise offload, open the SOF frontend PCM that the topology routes to +that UAOL backend, not the USB card's PCM. Item numbers change with hotplug, +so do not store them as device identities; re-read the list instead. + +Source map +========== + +The following paths are relative to the kernel source root: + +.. list-table:: + :header-rows: 1 + :widths: 55 45 + + * - Source + - Primary responsibility + * - :file:`include/sound/usb-offload.h` + - Library API, host callbacks, device and stream-resource identities. + * - :file:`sound/usb/offload/init.c` + - Host registration, context lifecycle and hotplug dispatch. + * - :file:`sound/usb/offload/usb-audio.c` + - Candidate filter, platform-ops bridge, format refinement, resource + acquisition, quiesce, restart, release. + * - :file:`sound/usb/card.c`, :file:`sound/usb/pcm.c` + - USB platform callbacks and rediscovery; arbitration of substream + ownership between the USB card's PCM and offload. + * - :file:`sound/soc/soc-usb.c`, :file:`include/sound/soc-usb.h` + - Port lookup, available devices, route lists, controls and policy. + * - :file:`sound/soc/sof/intel/hda.c` + - Discovery, firmware gates, library callbacks, machine registration. + * - :file:`sound/soc/sof/intel/hda-uaol.c` + - SOF records, port, jack, constraints, endpoint-to-gateway binding, + backend lifecycle and disconnect handling. + * - :file:`sound/soc/sof/intel/hda-dai.c`, + :file:`sound/soc/sof/intel/hda-dai-ops.c` + - The DAI callbacks the UAOL DAI builds on, link DMA allocation, + feedback DMA, IPC4 trigger machinery. + * - :file:`sound/soc/sof/intel/hda-pcm.c` + - Applying backend-derived constraints during frontend open. + * - :file:`sound/soc/sof/intel/hda-mlink.c`, + :file:`sound/soc/sof/intel/lnl.c` + - UAOL multilink lookup and alternate-link offload enablement. + * - :file:`sound/soc/sof/ipc4-loader.c` + - Firmware UAOL support/capability parsing. + * - :file:`sound/soc/sof/ipc4-topology.c`, + :file:`sound/soc/sof/ipc4-topology.h` + - Gateway TLV layout, FIFO partitioning and copier IPC assembly. + * - :file:`sound/soc/intel/boards/sof_board_helpers.c`, + :file:`sound/soc/intel/boards/skl_hda_dsp_generic.c`, + :file:`sound/soc/intel/boards/sof_sdw.c`, + :file:`sound/soc/intel/boards/sof_uaol_common.h` + - Machine backend links and shared naming. + * - :file:`drivers/usb/host/xhci-sideband.c`, + :file:`include/linux/usb/xhci-sideband.h` + - Controller eligibility, sideband resource reservation, endpoint + binding/properties, and endpoint stop. diff --git a/Documentation/sound/soc/usb.rst b/Documentation/sound/soc/usb.rst index ea0b1eef20d2b8..61d789a0f940c3 100644 --- a/Documentation/sound/soc/usb.rst +++ b/Documentation/sound/soc/usb.rst @@ -580,6 +580,7 @@ Selection rules: Control ``INFO`` and ``VALUE`` change notifications are sent whenever a connect, a disconnect or a write changes the item list or the selection. +For a backend using this model see :doc:`sof-uaol`. Existing USB Sound Kcontrol --------------------------- From d9406d5bf3fd50b7c7dcdb15222159a54fef859f Mon Sep 17 00:00:00 2001 From: Peter Ujfalusi Date: Wed, 4 Nov 2026 15:28:55 +0200 Subject: [PATCH 53/53] Revert "xhci: sideband: Don't support Intel audio sideband for devices behind a hub" This reverts commit 0e544eef569fbdb90db410b3457e37224788a6e2. Signed-off-by: Peter Ujfalusi --- drivers/usb/host/xhci-sideband.c | 6 ------ 1 file changed, 6 deletions(-) diff --git a/drivers/usb/host/xhci-sideband.c b/drivers/usb/host/xhci-sideband.c index ca27460d5009c2..651896ee9cec8a 100644 --- a/drivers/usb/host/xhci-sideband.c +++ b/drivers/usb/host/xhci-sideband.c @@ -967,16 +967,10 @@ xhci_sideband_register(struct usb_interface *intf, enum xhci_sideband_type type, case XHCI_SIDEBAND_VENDOR: break; case XHCI_SIDEBAND_AUDIO: - if (xhci->quirks & XHCI_INTEL_HOST && udev->parent->parent) { - dev_dbg(&udev->dev, "Intel Audio sideband not supported behind hub\n"); - return NULL; - } - if (!xhci_sideband_hw_capable(xhci)) { dev_info(&udev->dev, "Host not sideband capable\n"); return NULL; } - break; default: return NULL;