Skip to content

Commit

Permalink
Use small lock to protect usbdev and endpoint in arch renesas.
Browse files Browse the repository at this point in the history
Signed-off-by: wangzhi16 <[email protected]>
  • Loading branch information
wangzhi-art authored and xiaoxiang781216 committed Jan 15, 2025
1 parent ac2b495 commit e211904
Showing 1 changed file with 34 additions and 15 deletions.
49 changes: 34 additions & 15 deletions arch/renesas/src/rx65n/rx65n_usbdev.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include <debug.h>
#include <stdint.h>
#include <stdio.h>
#include <sched.h>

#include <nuttx/arch.h>
#include <nuttx/kmalloc.h>
Expand All @@ -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>

Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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 */

Expand All @@ -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;
}

Expand All @@ -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;
}
}
Expand All @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand All @@ -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)
{
Expand All @@ -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;
}

Expand Down Expand Up @@ -3366,9 +3381,9 @@ static struct usbdev_ep_s *rx65n_allocep(struct usbdev_s *dev, uint8_t epno,
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);
}

/****************************************************************************
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit e211904

Please sign in to comment.