Devicetree
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Petar Stepanovic" <pstepanovic@axiado.com>
Cc: linux-usb@vger.kernel.org, devicetree@vger.kernel.org,
	robh@kernel.org, conor+dt@kernel.org
Subject: Re: [PATCH v5 4/6] usb: gadget: udc: crg: Add endpoint 0 handling
Date: Mon, 16 Nov 2026 20:16:31 +0000	[thread overview]
Message-ID: <sashiko-outbox-217645@kernel.org> (raw)
In-Reply-To: <20261116-axiado-ax3000-usb-device-controller-v5-4-49271ed5c793@axiado.com>

Thank you for your contribution! Sashiko AI review found 5 potential
issues to consider:
- [High] usb: gadget: udc: crg: broken EP0 halt and lock-drop handling
  in setfeaturesrequest()
- [Medium] usb: gadget: udc: crg: clear U1 and U2 LPM state on
  SET_CONFIGURATION(0)
- [Medium] usb: gadget: udc: crg: mishandled interface GET_STATUS and
  FUNC_SUSPEND on EP0
- [Medium] usb: gadget: crg: missing setup_status error return and
  ep0_state checks on EP0
- [Medium] usb: gadget: crg: missing ep0_state checks in EP0 status and
  halt paths
--

commit 38b91eca05d2bb158aa1aa4be9dbbe69096a9ef9
Author: Petar Stepanovic <pstepanovic@axiado.com>

usb: gadget: udc: crg: Add endpoint 0 handling

This commit adds endpoint 0 setup packet, status stage, and control transfer
handling for the Axiado AX3000/AX3005 UDC driver.

> diff --git a/drivers/usb/gadget/udc/crg/crg-udc-ep0.c b/drivers/usb/gadget/udc/crg/crg-udc-ep0.c
> new file mode 100644
> index 0000000000000..4bd0dd4da8c67
> --- /dev/null
> +++ b/drivers/usb/gadget/udc/crg/crg-udc-ep0.c
> @@ -0,0 +1,1264 @@
[ ... ]
> +int crg_udc_build_ep0_status(struct crg_udc_ep *udc_ep_ptr, bool default_value,
> +			      u32 status, struct crg_udc_request *udc_req_ptr,
> +			      u8 set_addr, u8 stall)
> +{
> +	struct crg_gadget_dev *crg_udc = udc_ep_ptr->crg_udc;
> +	struct crg_transfer_trb *enq_pt = udc_ep_ptr->enq_pt;
> +
> +	/* Internal status requests bypass usb_ep_queue(), so they must also
> +	 * respect a teardown that is giving back the old EP0 queue.
> +	 */
> +	if (udc_ep_ptr->nuking) {
> +		crg_udc->ep0_state = EP0_STATE_ERROR;
> +		return -EBUSY;
> +	}
> +
> +	/* Reserve a free transfer-ring slot before touching request state or
> +	 * publishing the STATUS TRB. crg_udc_room_on_ring() treats equal
> +	 * pointers as empty, so check tran_ring_full separately.
> +	 */
> +	if (!udc_ep_ptr->first_trb || !udc_ep_ptr->last_trb ||
> +	    !udc_ep_ptr->enq_pt || !udc_ep_ptr->deq_pt)
> +		return -ENODEV;
> +	if (udc_ep_ptr->ep_state != EP_STATE_RUNNING)
> +		return -ESHUTDOWN;

[Severity: Medium]
Should crg_udc_build_ep0_status() and crg_udc_set_ep0_halt() check that
crg_udc->ep0_state is EP0_STATE_IDLE before queuing a status or stall TRB?

crg_udc_handle_setup_pkt() sets crg_udc->ep0_state = EP0_STATE_ABORTING
while udc_ep[0].ep_state is still EP_STATE_RUNNING, then drops udc_lock
across flush_work(&crg_udc->udc_reset_work):

CPU0                              CPU1
crg_udc_handle_setup_pkt()
  ep0_state = EP0_STATE_ABORTING
  spin_unlock(&udc_lock)
  flush_work(&udc_reset_work)
                                  crg_udc_ep_set_halt(ep0)
                                    ...
                                      crg_udc_set_ep0_halt()
                                        crg_udc_build_ep0_status()
                                        ep_state = EP_STATE_HALTED
  spin_lock(&udc_lock)
  if (ep_state != EP_STATE_RUNNING)
    return;  <-- drops new SETUP with ep0_state stuck in EP0_STATE_ABORTING

Because neither crg_udc_set_ep0_halt() nor crg_udc_build_ep0_status() checks
crg_udc->ep0_state, a concurrent halt or status submission while ep0_state
is EP0_STATE_ABORTING or EP0_STATE_ERROR queues status_req with the previous
setup_tag, rings doorbell 0, and sets ep_state to EP_STATE_HALTED without
bumping generation, causing crg_udc_handle_setup_pkt() to abort the incoming
SETUP and leave EP0 blocked in EP0_STATE_ABORTING.

[ ... ]

> +static bool setfeaturesrequest(struct crg_gadget_dev *crg_udc, u8 request_type,
> +			       u8 b_request, u16 value, u16 index, u16 length,
> +			       unsigned long *flags)
> +{
> +	int status = -EINPROGRESS;
> +	u8 dci;
> +	struct crg_udc_ep *udc_ep_ptr;
> +	u32 tmp;
> +	u32 set_feat = 0;
> +	struct crg_uccr __iomem *uccr = crg_udc->uccr;
[ ... ]
> +		udc_ep_ptr = &crg_udc->udc_ep[dci];
> +		status = crg_udc_ep_halt(
> +			udc_ep_ptr, (b_request == USB_REQ_SET_FEATURE) ? 1 : 0,
> +			0, flags);
> +
> +		if (status < 0)
> +			goto set_feature_error;
[ ... ]
> +	} else if ((request_type & (USB_RECIP_MASK | USB_TYPE_MASK)) ==
> +		   (USB_RECIP_INTERFACE | USB_TYPE_STANDARD)) {
[ ... ]
> +		/* Do not need to return status stage here
> +		 * Pass to composite gadget driver to process
> +		 * the request
> +		 */
> +		return false;
> +	} else {
> +		/* Unknown recipient must not receive a successful status stage. */
> +		goto set_feature_error;
> +	}
> +
> +	crg_udc->setup_status = STATUS_STAGE_XFER;
> +	crg_udc_submit_ep0_status(crg_udc, status, 0);
> +	return true;
> +
> +set_feature_error:
> +	crg_udc_set_ep0_halt(crg_udc);
> +	return true;
> +}

[Severity: High]
Does the USB_RECIP_ENDPOINT path in setfeaturesrequest() mishandle EP0
targets, status-stage request state, and lock drops across
crg_udc_ep_halt()?

First, when SET_FEATURE(USB_ENDPOINT_HALT) targets EP0 (dci == 0),
crg_udc_ep_halt() calls crg_udc_set_ep0_halt(), which already queues
status_req with stall = 1 and sets udc_ep[0].ep_state = EP_STATE_HALTED;
falling through to crg_udc_submit_ep0_status() then fails with -ESHUTDOWN
and forces crg_udc->ep0_state into EP0_STATE_ERROR.

Second, crg_udc_ep_halt() can drop udc_lock via flags during request
givebacks in crg_udc_ep_nuke() or crg_udc_queue_pending_trbs(), but
setfeaturesrequest() does not revalidate EP0/event generations or reset
state before calling crg_udc_submit_ep0_status() or crg_udc_set_ep0_halt(),
which can publish a status or stall TRB and ring doorbell 0 during a reset
or on a reinitialized EP0 ring.

Third, assigning status = crg_udc_ep_halt(...) overwrites status from
-EINPROGRESS to 0 on success, so crg_udc_submit_ep0_status() queues the
in-flight status_req with usb_req.status = 0 before hardware completion.

[ ... ]

> +	if ((setup_pkt->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
> +		switch (setup_pkt->bRequest) {
> +		case USB_REQ_GET_STATUS:
> +			if (setup_pkt->bRequestType !=
> +				    (USB_DIR_IN | USB_RECIP_DEVICE |
> +				     USB_TYPE_STANDARD) &&
> +			    setup_pkt->bRequestType !=
> +				    (USB_DIR_IN | USB_RECIP_INTERFACE |
> +				     USB_TYPE_STANDARD) &&
> +			    setup_pkt->bRequestType !=
> +				    (USB_DIR_IN | USB_RECIP_ENDPOINT |
> +				     USB_TYPE_STANDARD)) {
> +				crg_udc_set_ep0_halt(crg_udc);
> +				return;
> +			}
> +
> +			getstatusrequest(crg_udc, setup_pkt->bRequestType,
> +					 w_value, w_index, w_length);
> +			return;

[Severity: Medium]
Should SuperSpeed USB_REQ_GET_STATUS for USB_RECIP_INTERFACE be delegated to
driver->setup(), and should setfeaturesrequest() reject
USB_INTRF_FUNC_SUSPEND below SuperSpeed?

setfeaturesrequest() delegates USB_INTRF_FUNC_SUSPEND to driver->setup() so
the composite driver can update function wakeup state, but
crg_udc_handle_setup_pkt() intercepts USB_REQ_GET_STATUS for
USB_RECIP_INTERFACE and getstatusrequest() always replies with 0 instead of
letting composite_setup() report USB_INTRF_STAT_FUNC_RW_CAP and
USB_INTRF_STAT_FUNC_RW.  In addition, the USB_RECIP_INTERFACE branch in
setfeaturesrequest() does not check crg_udc->gadget.speed >= USB_SPEED_SUPER,
and composite_setup() only checks gadget_is_superspeed() (which tests
max_speed), so USB_INTRF_FUNC_SUSPEND is accepted at High-Speed and
Full-Speed instead of stalling EP0.

[ ... ]

> +	if (setup_pkt->bRequest == USB_REQ_SET_CONFIGURATION &&
> +	    (setup_pkt->bRequestType & (USB_TYPE_MASK | USB_RECIP_MASK)) ==
> +		    (USB_TYPE_STANDARD | USB_RECIP_DEVICE)) {
> +		if (w_value) {
> +			crg_udc->device_state = USB_STATE_CONFIGURED;
> +			usb_gadget_set_state(&crg_udc->gadget,
> +					     USB_STATE_CONFIGURED);
> +		} else {
> +			/* Endpoint disable may already have updated device_state. */
> +			crg_udc->device_state = USB_STATE_ADDRESS;
> +			usb_gadget_set_state(&crg_udc->gadget,
> +					     USB_STATE_ADDRESS);
> +		}
> +	}
> +}

[Severity: Medium]
When USB_REQ_SET_CONFIGURATION with w_value == 0 transitions the device back
to USB_STATE_ADDRESS, should it also clear U1/U2 link power management state
via crg_udc_clear_portpm()?

Without clearing uccr->u3portpmsc, feature_u1_enable, and feature_u2_enable
on deconfiguration, hardware U1/U2 initiation and timeouts stay active in
USB_STATE_ADDRESS, getstatusrequest() continues reporting
USB_DEV_STAT_U1_ENABLED and USB_DEV_STAT_U2_ENABLED, and the host cannot
clear them via CLEAR_FEATURE because setfeaturesrequest() rejects U1/U2
requests outside USB_STATE_CONFIGURED.

[ ... ]

> +int crg_udc_queue_ctrl(struct crg_udc_ep *udc_ep_ptr,
> +		       struct crg_udc_request *udc_req_ptr,
> +		       u32 num_of_trbs_needed)
> +{
[ ... ]
> +	if (crg_udc->ep0_state == EP0_STATE_ABORTING)
> +		return -ECONNRESET;
> +
> +	ep_state = crg_udc_get_ep_state(crg_udc, 0);
> +	/* Need to queue the request even ep is paused or halted */
> +	if (ep_state != EP_STATE_RUNNING) {
> +		dev_dbg(crg_udc->dev, "EP State = 0x%x\n", ep_state);
> +		return -EINVAL;
> +	}
> +	if (list_empty(&udc_ep_ptr->queue)) {
> +		/* For control endpoint, we can handle one setup request at a
> +		 * time. so if there are TD pending in the transfer ring.
> +		 * wait for the sequence number error event. Then put the new
> +		 * request to transfer ring
> +		 */
> +		if (enq_pt == dq_pt && !udc_ep_ptr->tran_ring_full) {
> +			u32 i, td_done = 0;
> +			bool need_zlp = false;
> +			struct crg_udc_bounce *bounce = udc_req_ptr->bounce;
> +			dma_addr_t trb_dma = bounce ? bounce->dma : 0;
> +			u64 remaining = usb_req->length;
> +			/* Transfer ring is empty
> +			 * setup data stage TRBs
> +			 */
> +			udc_req_ptr->first_trb = udc_ep_ptr->enq_pt;
> +			if (crg_udc->setup_status == DATA_STAGE_XFER)
> +				dir = 0;
> +			else if (crg_udc->setup_status == DATA_STAGE_RECV)
> +				dir = 1;
> +			else
> +				dev_dbg(crg_udc->dev,
> +					"unexpected setup_status!%d\n",
> +					crg_udc->setup_status);

[Severity: Medium]
Should crg_udc_queue_ctrl() return -EINVAL when setup_status is neither
DATA_STAGE_XFER nor DATA_STAGE_RECV, and also reject requests when
crg_udc->ep0_state != EP0_STATE_IDLE?

In crg_udc_ep_queue(), a non-zero-length EP0 request queued while
setup_status is STATUS_STAGE_XFER or STATUS_STAGE_RECV (for example, from a
completion callback during status-stage giveback before setup_status is
reset to WAIT_FOR_SETUP) passes the !_req->length check and reaches
crg_udc_queue_ctrl().  Because the else branch only logs dev_dbg(),
crg_udc_queue_ctrl() proceeds to queue TRB_TYPE_XFER_DATA_STAGE TRBs with
dir = 0 and ring the EP0 doorbell.  Similarly, checking only
ep0_state == EP0_STATE_ABORTING at entry allows control requests to be
queued while ep0_state is EP0_STATE_ERROR.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261116-axiado-ax3000-usb-device-controller-v5-0-49271ed5c793@axiado.com?part=4

  reply	other threads:[~2026-11-16 20:16 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-16 19:06 [PATCH v5 0/6] Axiado AX3000 and AX3005 USB Device Controller (UDC) Petar Stepanovic
2026-11-16 19:06 ` [PATCH v5 1/6] dt-bindings: usb: gadget: udc: add Axiado AX3000 and AX3005 UDC Petar Stepanovic
2026-11-16 19:06 ` [PATCH v5 2/6] usb: gadget: udc: crg: Add hardware definitions and structs Petar Stepanovic
2026-11-16 19:06 ` [PATCH v5 3/6] usb: gadget: udc: crg: Add ring and transfer management Petar Stepanovic
2026-11-16 19:46   ` sashiko-bot
2026-11-16 19:06 ` [PATCH v5 4/6] usb: gadget: udc: crg: Add endpoint 0 handling Petar Stepanovic
2026-11-16 20:16   ` sashiko-bot [this message]
2026-11-16 19:06 ` [PATCH v5 5/6] usb: gadget: udc: crg: Add gadget and endpoint operations Petar Stepanovic
2026-11-16 19:06 ` [PATCH v5 6/6] usb: gadget: udc: crg: Add core driver and build integration Petar Stepanovic

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-217645@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=conor+dt@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=pstepanovic@axiado.com \
    --cc=robh@kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox