From edb7d034671594194c04739532c2cfd5d22cce3f Mon Sep 17 00:00:00 2001 From: Justin Hammond Date: Sun, 16 Aug 2026 16:03:55 +0800 Subject: [PATCH] drivers/usbhost: Allow an xHCI controller with no scratchpad buffers. HCSPARAMS2 may report zero scratchpad buffers; QEMU's does. The driver sized the array from that count unconditionally and read the NULL from a zero byte kmm_memalign() as -ENOMEM, so such a controller never started. Skip the allocation when no_scratch is zero, leaving DCBAA[0] clear. Assisted-by: Claude:claude-opus-5 Signed-off-by: Justin Hammond --- drivers/usbhost/usbhost_xhci_pci.c | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/drivers/usbhost/usbhost_xhci_pci.c b/drivers/usbhost/usbhost_xhci_pci.c index 68d5e06aa4e..0f31007d9bb 100644 --- a/drivers/usbhost/usbhost_xhci_pci.c +++ b/drivers/usbhost/usbhost_xhci_pci.c @@ -4365,17 +4365,22 @@ static int xhci_mem_alloc(FAR struct usbhost_xhci_s *priv) size_t tmp; int i; - /* Allocate Scratchpad Buffer Array */ + /* Allocate the Scratchpad Buffer Array, if one is wanted. no_scratch + * may be zero, and a zero byte allocation returns NULL. + */ - tmp = priv->no_scratch * sizeof(uint64_t); - priv->pg_sb = kmm_memalign(XHCI_BUF_ALIGN, tmp); - if (!priv->pg_sb) + if (priv->no_scratch > 0) { - pcierr("pg_sb malloc failed\n"); - return -ENOMEM; - } + tmp = priv->no_scratch * sizeof(uint64_t); + priv->pg_sb = kmm_memalign(XHCI_BUF_ALIGN, tmp); + if (!priv->pg_sb) + { + pcierr("pg_sb malloc failed\n"); + return -ENOMEM; + } - memset(priv->pg_sb, 0, tmp); + memset(priv->pg_sb, 0, tmp); + } for (i = 0; i < priv->no_scratch; i++) {