drivers/usbhost: use small lock to protect usbhost bthci

replace critical_section with spinlock

Signed-off-by: yangsong8 <yangsong8@xiaomi.com>
This commit is contained in:
yangsong8
2025-06-20 22:05:23 +08:00
committed by Xiang Xiao
parent 5571ec8fa2
commit 62f7a8fa22
+8 -9
View File
@@ -25,6 +25,7 @@
****************************************************************************/ ****************************************************************************/
#include <nuttx/config.h> #include <nuttx/config.h>
#include <nuttx/spinlock.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@@ -219,6 +220,8 @@ static struct usbhost_registry_s g_bthci =
static uint32_t g_devinuse; static uint32_t g_devinuse;
static spinlock_t g_lock = SP_UNLOCKED;
/**************************************************************************** /****************************************************************************
* Private Functions * Private Functions
****************************************************************************/ ****************************************************************************/
@@ -294,7 +297,7 @@ static int usbhost_allocdevno(FAR struct usbhost_state_s *priv)
irqstate_t flags; irqstate_t flags;
int devno; int devno;
flags = enter_critical_section(); flags = spin_lock_irqsave(&g_lock);
for (devno = 0; devno < 26; devno++) for (devno = 0; devno < 26; devno++)
{ {
uint32_t bitno = 1 << devno; uint32_t bitno = 1 << devno;
@@ -302,12 +305,12 @@ static int usbhost_allocdevno(FAR struct usbhost_state_s *priv)
{ {
g_devinuse |= bitno; g_devinuse |= bitno;
priv->devchar = 'a' + devno; priv->devchar = 'a' + devno;
leave_critical_section(flags); spin_unlock_irqrestore(&g_lock, flags);
return OK; return OK;
} }
} }
leave_critical_section(flags); spin_unlock_irqrestore(&g_lock, flags);
return -EMFILE; return -EMFILE;
} }
@@ -319,9 +322,9 @@ static void usbhost_freedevno(FAR struct usbhost_state_s *priv)
if (devno >= 0 && devno < 26) if (devno >= 0 && devno < 26)
{ {
irqstate_t flags = enter_critical_section(); irqstate_t flags = spin_lock_irqsave(&g_lock);
g_devinuse &= ~(1 << devno); g_devinuse &= ~(1 << devno);
leave_critical_section(flags); spin_unlock_irqrestore(&g_lock, flags);
} }
} }
} }
@@ -1440,7 +1443,6 @@ static int usbhci_connect(FAR struct usbhost_class_s *usbclass,
static int usbhost_disconnected(FAR struct usbhost_class_s *usbclass) static int usbhost_disconnected(FAR struct usbhost_class_s *usbclass)
{ {
FAR struct usbhost_state_s *priv = (FAR struct usbhost_state_s *)usbclass; FAR struct usbhost_state_s *priv = (FAR struct usbhost_state_s *)usbclass;
irqstate_t flags;
DEBUGASSERT(priv != NULL); DEBUGASSERT(priv != NULL);
@@ -1448,8 +1450,6 @@ static int usbhost_disconnected(FAR struct usbhost_class_s *usbclass)
* longer available. * longer available.
*/ */
flags = enter_critical_section();
priv->disconnected = true; priv->disconnected = true;
if (priv->devchar == 'a') if (priv->devchar == 'a')
@@ -1481,7 +1481,6 @@ static int usbhost_disconnected(FAR struct usbhost_class_s *usbclass)
usbhost_destroy(priv); usbhost_destroy(priv);
} }
leave_critical_section(flags);
return OK; return OK;
} }