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 v7 4/6] usb: gadget: udc: crg: Add endpoint 0 handling
Date: Mon, 30 Nov 2026 10:18:19 +0000	[thread overview]
Message-ID: <sashiko-outbox-240842@kernel.org> (raw)
In-Reply-To: <20261130-axiado-ax3000-usb-device-controller-v7-4-f240b1418955@axiado.com>

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] usb: udc: crg: missing pending_setup_valid check in
  crg_udc_handle_setup_pkt()
--

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

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

This commit adds endpoint 0 setup packet, status, 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..626f39f22b6d0
> --- /dev/null
> +++ b/drivers/usb/gadget/udc/crg/crg-udc-ep0.c
> @@ -0,0 +1,1273 @@

[ ... ]

> +static int crg_udc_ep0_check_state(struct crg_gadget_dev *crg_udc)
> +{
> +	if (crg_udc->ep0_state != EP0_STATE_IDLE || crg_udc->udc_ep[0].nuking ||
> +	    crg_udc->pending_setup_valid)
> +		return -ECONNRESET;
> +	if (!crg_udc_gadget_ready(crg_udc) || crg_udc->bus_reset_pending ||
> +	    crg_udc->device_state == USB_STATE_RECONNECTING ||
> +	    crg_udc_ep0_port_change_pending(crg_udc))
> +		return -ESHUTDOWN;
> +	return 0;
> +}

[ ... ]

> +void crg_udc_handle_setup_pkt(struct crg_gadget_dev *crg_udc,
> +			      struct usb_ctrlrequest *setup_pkt, u8 setup_tag,
> +			      unsigned long *flags)
> +{

[ ... ]

> +	/* The callback drops udc_lock; a newer SETUP or reset can supersede us. */
> +	ep0_generation = crg_udc->udc_ep[0].generation;
> +	event_generation = crg_udc->udc_event[0].generation;
> +	active_setup_tag = crg_udc->setup_tag;
> +	driver = crg_udc->async_callbacks ? crg_udc->gadget_driver : NULL;
> +	crg_udc_callback_enter(crg_udc);
> +	spin_unlock_irqrestore(&crg_udc->udc_lock, *flags);
> +
> +	if (!driver || !READ_ONCE(crg_udc->async_callbacks) ||
> +	    READ_ONCE(crg_udc->gadget_driver) != driver)
> +		setup_ret = -ESHUTDOWN;
> +	else
> +		setup_ret = driver->setup(&crg_udc->gadget, setup_pkt);
> +
> +	spin_lock_irqsave(&crg_udc->udc_lock, *flags);
> +	crg_udc_callback_exit(crg_udc);
> +	/* Never STALL or update device state for an obsolete SETUP. */
> +	if (!crg_udc_gadget_ready(crg_udc) ||
> +	    crg_udc->bus_reset_pending || crg_udc_ep0_port_change_pending(crg_udc) ||
> +	    crg_udc->device_state == USB_STATE_RECONNECTING ||
> +	    !driver || crg_udc->gadget_driver != driver ||
> +	    crg_udc->udc_ep[0].generation != ep0_generation ||
> +	    crg_udc->udc_event[0].generation != event_generation ||
> +	    crg_udc->setup_tag != active_setup_tag ||
> +	    crg_udc->ep0_state != EP0_STATE_IDLE ||
> +	    crg_udc->udc_ep[0].ep_state != EP_STATE_RUNNING)
> +		return;
> +
> +	if (setup_ret < 0) {
> +		crg_udc_set_ep0_halt(crg_udc);
> +		return;
> +	}
> +
> +	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 {
> +			crg_udc_clear_portpm(crg_udc);
> +			crg_udc->device_state = USB_STATE_ADDRESS;
> +			usb_gadget_set_state(&crg_udc->gadget,
> +					     USB_STATE_ADDRESS);
> +		}
> +	}
> +}

[Severity: Medium]
Can crg_udc_handle_setup_pkt() update crg_udc->device_state and port power
management for an obsolete USB_REQ_SET_CONFIGURATION request when a new
SETUP packet arrives while udc_lock is dropped?

While gadget_callbacks_active is elevated during driver->setup(), a newly
arrived SETUP event is deferred by setting crg_udc->pending_setup_valid
without changing udc_ep[0].generation, udc_event[0].generation,
crg_udc->setup_tag, or crg_udc->ep0_state.  When driver->setup() returns
and udc_lock is reacquired, crg_udc_callback_exit() only schedules
udc_reset_work to process the deferred SETUP after udc_lock is released:

CPU0                                  CPU1
crg_udc_handle_setup_pkt()
  crg_udc_callback_enter()
  spin_unlock(&crg_udc->udc_lock)
  driver->setup()
                                      /* new SETUP event arrives */
                                      spin_lock(&crg_udc->udc_lock)
                                      /* gadget_callbacks_active > 0 */
                                      crg_udc->pending_setup_valid = true
                                      spin_unlock(&crg_udc->udc_lock)
  spin_lock(&crg_udc->udc_lock)
  crg_udc_callback_exit()
    /* kicks udc_reset_work */
  /* pending_setup_valid not checked */
  crg_udc->device_state = ...         <-- mutates state for obsolete SETUP

Unlike setfeaturesrequest(), which calls crg_udc_ep0_check_state() after
reacquiring udc_lock, the post-callback validation here omits
crg_udc_ep0_check_state() (specifically crg_udc->pending_setup_valid and
crg_udc->udc_ep[0].nuking).  Calling crg_udc_ep0_check_state(crg_udc) in
this post-lock check would prevent committing state changes for a
superseded control transfer.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261130-axiado-ax3000-usb-device-controller-v7-0-f240b1418955@axiado.com?part=4

  reply	other threads:[~2026-11-30 10:18 UTC|newest]

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

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-240842@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