This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git


The following commit(s) were added to refs/heads/master by this push:
     new e211904640 Use small lock to protect usbdev and endpoint in arch 
renesas.
e211904640 is described below

commit e211904640262f602f82afd13d414db2a7d39986
Author: wangzhi16 <wangzh...@xiaomi.com>
AuthorDate: Wed Jan 15 14:25:06 2025 +0800

    Use small lock to protect usbdev and endpoint in arch renesas.
    
    Signed-off-by: wangzhi16 <wangzh...@xiaomi.com>
---
 arch/renesas/src/rx65n/rx65n_usbdev.c | 49 ++++++++++++++++++++++++-----------
 1 file changed, 34 insertions(+), 15 deletions(-)

diff --git a/arch/renesas/src/rx65n/rx65n_usbdev.c 
b/arch/renesas/src/rx65n/rx65n_usbdev.c
index e27e9336cb..ec2d54a3d7 100644
--- a/arch/renesas/src/rx65n/rx65n_usbdev.c
+++ b/arch/renesas/src/rx65n/rx65n_usbdev.c
@@ -35,6 +35,7 @@
 #include <debug.h>
 #include <stdint.h>
 #include <stdio.h>
+#include <sched.h>
 
 #include <nuttx/arch.h>
 #include <nuttx/kmalloc.h>
@@ -45,6 +46,7 @@
 #include <nuttx/serial/serial.h>
 
 #include <nuttx/irq.h>
+#include <nuttx/spinlock.h>
 #include <arch/board/board.h>
 #include <arch/chip/types.h>
 
@@ -240,6 +242,10 @@ struct rx65n_usbdev_s
   /* The endpoint list */
 
   struct rx65n_ep_s      eplist[RX65N_NENDPOINTS];
+
+  /* Spinlock */
+
+  spinlock_t             lock;
 };
 
 /* For maintaining tables of endpoint info */
@@ -1838,9 +1844,9 @@ static void rx65n_reqcomplete(struct rx65n_ep_s *privep, 
int16_t result)
 
   /* Remove the completed request at the head of the endpoint request list */
 
-  flags = enter_critical_section();
+  flags = spin_lock_irqsave(&privep->dev->lock);
   privreq = rx65n_rqdequeue(privep);
-  leave_critical_section(flags);
+  spin_unlock_irqrestore(&privep->dev->lock, flags);
 
   if (privreq)
     {
@@ -2998,10 +3004,12 @@ static int rx65n_epdisable(struct usbdev_ep_s *ep)
 
   /* Cancel any ongoing activity */
 
-  flags = enter_critical_section();
+  flags = spin_lock_irqsave(&privep->dev->lock);
+  sched_lock();
   rx65n_cancelrequests(privep);
 
-  leave_critical_section(flags);
+  spin_unlock_irqrestore(&privep->dev->lock, flags);
+  sched_unlock();
   return OK;
 }
 
@@ -3106,7 +3114,8 @@ static int rx65n_epsubmit(struct usbdev_ep_s *ep, struct 
usbdev_req_s *req)
   epno        = USB_EPNO(ep->eplog);
   req->result = -EINPROGRESS;
   req->xfrd   = 0;
-  flags       = enter_critical_section();
+  flags       = spin_lock_irqsave(&priv->lock);
+  sched_lock();
 
   /* If we are stalled, then drop all requests on the floor */
 
@@ -3133,7 +3142,8 @@ static int rx65n_epsubmit(struct usbdev_ep_s *ep, struct 
usbdev_req_s *req)
         {
           privreq->req.len = CDC_CLASS_DATA_LENGTH;
           rx65n_rdrequest(epno, priv, privep);
-          leave_critical_section(flags);
+          spin_unlock_irqrestore(&priv->lock, flags);
+          sched_unlock();
           return OK;
         }
 
@@ -3142,7 +3152,8 @@ static int rx65n_epsubmit(struct usbdev_ep_s *ep, struct 
usbdev_req_s *req)
       if (!privep->txbusy)
         {
           ret = rx65n_wrrequest(epno, priv, privep);
-          leave_critical_section(flags);
+          spin_unlock_irqrestore(&priv->lock, flags);
+          sched_unlock();
           return OK;
         }
     }
@@ -3156,7 +3167,8 @@ static int rx65n_epsubmit(struct usbdev_ep_s *ep, struct 
usbdev_req_s *req)
       if (priv->ep0state == EP0STATE_RDREQUEST)
         {
           rx65n_rdrequest(epno, priv, privep);
-          leave_critical_section(flags);
+          spin_unlock_irqrestore(&priv->lock, flags);
+          sched_unlock();
           return OK;
         }
 
@@ -3189,7 +3201,8 @@ static int rx65n_epsubmit(struct usbdev_ep_s *ep, struct 
usbdev_req_s *req)
         }
     }
 
-  leave_critical_section(flags);
+  spin_unlock_irqrestore(&priv->lock, flags);
+  sched_unlock();
   return ret;
 }
 
@@ -3230,9 +3243,11 @@ static int rx65n_epcancel(struct usbdev_ep_s *ep, struct 
usbdev_req_s *req)
 #endif
   usbtrace(TRACE_EPCANCEL, USB_EPNO(ep->eplog));
 
-  flags = enter_critical_section();
+  flags = spin_lock_irqsave(&privep->dev->lock);
+  sched_lock();
   rx65n_cancelrequests(privep);
-  leave_critical_section(flags);
+  spin_unlock_irqrestore(&privep->dev->lock, flags);
+  sched_unlock();
   return OK;
 }
 
@@ -3247,7 +3262,7 @@ static inline struct rx65n_ep_s *rx65n_epreserve(struct 
rx65n_usbdev_s *priv,
   irqstate_t flags;
   int epndx = 0;
 
-  flags = enter_critical_section();
+  flags = spin_lock_irqsave(&priv->lock);
   epset &= priv->epavail;
   if (epset)
     {
@@ -3272,7 +3287,7 @@ static inline struct rx65n_ep_s *rx65n_epreserve(struct 
rx65n_usbdev_s *priv,
         }
     }
 
-  leave_critical_section(flags);
+  spin_unlock_irqrestore(&priv->lock, flags);
   return privep;
 }
 
@@ -3366,9 +3381,9 @@ errout:
 static inline void
 rx65n_epunreserve(struct rx65n_usbdev_s *priv, struct rx65n_ep_s *privep)
 {
-  irqstate_t flags = enter_critical_section();
+  irqstate_t flags = spin_lock_irqsave(&priv->lock);
   priv->epavail |= RX65N_ENDP_BIT(USB_EPNO(privep->ep.eplog));
-  leave_critical_section(flags);
+  spin_unlock_irqrestore(&priv->lock, flags);
 }
 
 /****************************************************************************
@@ -6031,6 +6046,10 @@ void renesas_usbinitialize(void)
   int epno;
   usbtrace(TRACE_DEVINIT, 0);
 
+  /* Initialize driver lock */
+
+  spin_lock_init(&priv->lock);
+
   /* Enable write to System registers */
 
   regval = rx65n_getreg16(RX65N_PRCR_ADDR);

Reply via email to