[PATCH] [RFC] usb: gadget: Convert to usb_gadget_driver callback wrappers

Marek Vasut marex at denx.de
Fri Dec 16 04:20:47 CET 2022


Instead of having the entire USB gadget code base littered with direct
calls to usb_gadget_driver members, add wrappers which call those members.
These wrappers can perform additional request sanitization as well, esp.
the setup wrapper.

Convert all the drivers using regex and slight manual adjustment:
"
s@\([^ \t]\+\)->\(\(bind\|unbind\|setup\|disconnect\|suspend\|reset\)(\)@usb_gadget_driver_\2\1,
"

Signed-off-by: Marek Vasut <marex at denx.de>
---
Cc: Chunfeng Yun <chunfeng.yun at mediatek.com>
Cc: GSS_MTK_Uboot_upstream <GSS_MTK_Uboot_upstream at mediatek.com>
Cc: Heinrich Schuchardt <xypron.glpk at gmx.de>
Cc: Lukasz Majewski <lukma at denx.de>
Cc: Rui Miguel Silva <rui.silva at linaro.org>
Cc: Ryder Lee <ryder.lee at mediatek.com>
Cc: Simon Glass <sjg at chromium.org>
Cc: Stefan Roese <sr at denx.de>
Cc: Szymon Heidrich <szymon.heidrich at gmail.com>
Cc: Weijie Gao <weijie.gao at mediatek.com>
---
 drivers/usb/cdns3/ep0.c                    |  2 +-
 drivers/usb/dwc3/ep0.c                     |  2 +-
 drivers/usb/gadget/at91_udc.c              | 14 ++++-----
 drivers/usb/gadget/atmel_usba_udc.c        | 14 ++++-----
 drivers/usb/gadget/ci_udc.c                |  8 ++---
 drivers/usb/gadget/dwc2_udc_otg.c          |  6 ++--
 drivers/usb/gadget/dwc2_udc_otg_xfer_dma.c |  6 ++--
 drivers/usb/gadget/fotg210.c               |  6 ++--
 drivers/usb/gadget/max3420_udc.c           |  4 +--
 drivers/usb/gadget/udc/udc-core.c          | 10 +++---
 drivers/usb/mtu3/mtu3_gadget_ep0.c         |  2 +-
 drivers/usb/musb-new/musb_gadget_ep0.c     |  2 +-
 drivers/usb/musb-new/musb_uboot.c          |  6 ++--
 include/linux/usb/gadget.h                 | 36 ++++++++++++++++++++++
 14 files changed, 77 insertions(+), 41 deletions(-)

diff --git a/drivers/usb/cdns3/ep0.c b/drivers/usb/cdns3/ep0.c
index acff79ae1ca..70b106140c1 100644
--- a/drivers/usb/cdns3/ep0.c
+++ b/drivers/usb/cdns3/ep0.c
@@ -101,7 +101,7 @@ static int cdns3_ep0_delegate_req(struct cdns3_device *priv_dev,
 
 	spin_unlock(&priv_dev->lock);
 	priv_dev->setup_pending = 1;
-	ret = priv_dev->gadget_driver->setup(&priv_dev->gadget, ctrl_req);
+	ret = usb_gadget_driver_setup(priv_dev->gadget_driver, &priv_dev->gadget, ctrl_req);
 	priv_dev->setup_pending = 0;
 	spin_lock(&priv_dev->lock);
 	return ret;
diff --git a/drivers/usb/dwc3/ep0.c b/drivers/usb/dwc3/ep0.c
index 75ac993bc64..8d466036838 100644
--- a/drivers/usb/dwc3/ep0.c
+++ b/drivers/usb/dwc3/ep0.c
@@ -532,7 +532,7 @@ static int dwc3_ep0_delegate_req(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
 	int ret;
 
 	spin_unlock(&dwc->lock);
-	ret = dwc->gadget_driver->setup(&dwc->gadget, ctrl);
+	ret = usb_gadget_driver_setup(dwc->gadget_driver, &dwc->gadget, ctrl);
 	spin_lock(&dwc->lock);
 	return ret;
 }
diff --git a/drivers/usb/gadget/at91_udc.c b/drivers/usb/gadget/at91_udc.c
index 1feed417d68..baa4c431fed 100644
--- a/drivers/usb/gadget/at91_udc.c
+++ b/drivers/usb/gadget/at91_udc.c
@@ -673,7 +673,7 @@ static void reset_gadget(struct at91_udc *udc)
 	}
 	if (driver) {
 		spin_unlock(&udc->lock);
-		udc->driver->disconnect(&udc->gadget);
+		usb_gadget_driver_disconnect(udc->driver, &udc->gadget);
 		spin_lock(&udc->lock);
 	}
 
@@ -697,7 +697,7 @@ static void stop_activity(struct at91_udc *udc)
 	}
 	if (driver) {
 		spin_unlock(&udc->lock);
-		driver->disconnect(&udc->gadget);
+		usb_gadget_driver_disconnect(driver, &udc->gadget);
 		spin_lock(&udc->lock);
 	}
 
@@ -1050,7 +1050,7 @@ static void handle_setup(struct at91_udc *udc, struct at91_ep *ep, u32 csr)
 	/* pass request up to the gadget driver */
 	if (udc->driver) {
 		spin_unlock(&udc->lock);
-		status = udc->driver->setup(&udc->gadget, &pkt.r);
+		status = usb_gadget_driver_setup(udc->driver, &udc->gadget, &pkt.r);
 		spin_lock(&udc->lock);
 	}
 	else
@@ -1261,7 +1261,7 @@ static irqreturn_t at91_udc_irq(struct at91_udc *udc)
 			 */
 			if (udc->driver && udc->driver->suspend) {
 				spin_unlock(&udc->lock);
-				udc->driver->suspend(&udc->gadget);
+				usb_gadget_driver_suspend(udc->driver, &udc->gadget);
 				spin_lock(&udc->lock);
 			}
 
@@ -1455,7 +1455,7 @@ int usb_gadget_register_driver(struct usb_gadget_driver *driver)
 
 	udc->driver = driver;
 
-	ret = driver->bind(&udc->gadget);
+	ret = usb_gadget_driver_bind(driver, &udc->gadget);
 	if (ret) {
 		pr_err("driver->bind() returned %d\n", ret);
 		udc->driver = NULL;
@@ -1473,8 +1473,8 @@ int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
 		return -EINVAL;
 	}
 
-	driver->disconnect(&udc->gadget);
-	driver->unbind(&udc->gadget);
+	usb_gadget_driver_disconnect(driver, &udc->gadget);
+	usb_gadget_driver_unbind(driver, &udc->gadget);
 	udc->driver = NULL;
 
 	at91_stop(&udc->gadget);
diff --git a/drivers/usb/gadget/atmel_usba_udc.c b/drivers/usb/gadget/atmel_usba_udc.c
index 7d51821497b..f4dcd205a4f 100644
--- a/drivers/usb/gadget/atmel_usba_udc.c
+++ b/drivers/usb/gadget/atmel_usba_udc.c
@@ -823,7 +823,7 @@ static int handle_ep0_setup(struct usba_udc *udc, struct usba_ep *ep,
 	default:
 delegate:
 		spin_unlock(&udc->lock);
-		retval = udc->driver->setup(&udc->gadget, crq);
+		retval = usb_gadget_driver_setup(udc->driver, &udc->gadget, crq);
 		spin_lock(&udc->lock);
 	}
 
@@ -1006,7 +1006,7 @@ restart:
 			ret = handle_ep0_setup(udc, ep, &crq.crq);
 		} else {
 			spin_unlock(&udc->lock);
-			ret = udc->driver->setup(&udc->gadget, &crq.crq);
+			ret = usb_gadget_driver_setup(udc->driver, &udc->gadget, &crq.crq);
 			spin_lock(&udc->lock);
 		}
 
@@ -1079,7 +1079,7 @@ static int usba_udc_irq(struct usba_udc *udc)
 		if (udc->gadget.speed != USB_SPEED_UNKNOWN &&
 		    udc->driver && udc->driver->suspend) {
 			spin_unlock(&udc->lock);
-			udc->driver->suspend(&udc->gadget);
+			usb_gadget_driver_suspend(udc->driver, &udc->gadget);
 			spin_lock(&udc->lock);
 		}
 	}
@@ -1123,7 +1123,7 @@ static int usba_udc_irq(struct usba_udc *udc)
 		    udc->driver->disconnect) {
 			udc->gadget.speed = USB_SPEED_UNKNOWN;
 			spin_unlock(&udc->lock);
-			udc->driver->disconnect(&udc->gadget);
+			usb_gadget_driver_disconnect(udc->driver, &udc->gadget);
 			spin_lock(&udc->lock);
 		}
 
@@ -1225,7 +1225,7 @@ int usb_gadget_register_driver(struct usb_gadget_driver *driver)
 
 	udc->driver = driver;
 
-	ret = driver->bind(&udc->gadget);
+	ret = usb_gadget_driver_bind(driver, &udc->gadget);
 	if (ret) {
 		pr_err("driver->bind() returned %d\n", ret);
 		udc->driver = NULL;
@@ -1243,8 +1243,8 @@ int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
 		return -EINVAL;
 	}
 
-	driver->disconnect(&udc->gadget);
-	driver->unbind(&udc->gadget);
+	usb_gadget_driver_disconnect(driver, &udc->gadget);
+	usb_gadget_driver_unbind(driver, &udc->gadget);
 	udc->driver = NULL;
 
 	atmel_usba_stop(udc);
diff --git a/drivers/usb/gadget/ci_udc.c b/drivers/usb/gadget/ci_udc.c
index d9a89a17b27..b60bb99ca53 100644
--- a/drivers/usb/gadget/ci_udc.c
+++ b/drivers/usb/gadget/ci_udc.c
@@ -761,7 +761,7 @@ static void handle_setup(void)
 	}
 	/* pass request up to the gadget driver */
 	if (controller.driver)
-		status = controller.driver->setup(&controller.gadget, &r);
+		status = usb_gadget_driver_setup(controller.driver, &controller.gadget, &r);
 	else
 		status = -ENODEV;
 
@@ -889,7 +889,7 @@ void udc_disconnect(void)
 	writel(USBCMD_FS2, &udc->usbcmd);
 	udelay(800);
 	if (controller.driver)
-		controller.driver->disconnect(&controller.gadget);
+		usb_gadget_driver_disconnect(controller.driver, &controller.gadget);
 }
 
 static int ci_pullup(struct usb_gadget *gadget, int is_on)
@@ -1036,7 +1036,7 @@ int usb_gadget_register_driver(struct usb_gadget_driver *driver)
 		return ret;
 	}
 
-	ret = driver->bind(&controller.gadget);
+	ret = usb_gadget_driver_bind(driver, &controller.gadget);
 	if (ret) {
 		DBG("driver->bind() returned %d\n", ret);
 		return ret;
@@ -1050,7 +1050,7 @@ int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
 {
 	udc_disconnect();
 
-	driver->unbind(&controller.gadget);
+	usb_gadget_driver_unbind(driver, &controller.gadget);
 	controller.driver = NULL;
 
 	ci_ep_free_request(&controller.ep[0].ep, &controller.ep0_req->req);
diff --git a/drivers/usb/gadget/dwc2_udc_otg.c b/drivers/usb/gadget/dwc2_udc_otg.c
index 77988f78ab3..a6730699b18 100644
--- a/drivers/usb/gadget/dwc2_udc_otg.c
+++ b/drivers/usb/gadget/dwc2_udc_otg.c
@@ -266,7 +266,7 @@ int usb_gadget_register_driver(struct usb_gadget_driver *driver)
 		return retval;
 	}
 
-	retval = driver->bind(&dev->gadget);
+	retval = usb_gadget_driver_bind(driver, &dev->gadget);
 	if (retval) {
 		debug_cond(DEBUG_SETUP != 0,
 			   "%s: bind to driver --> error %d\n",
@@ -302,7 +302,7 @@ int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
 	stop_activity(dev, driver);
 	spin_unlock_irqrestore(&dev->lock, flags);
 
-	driver->unbind(&dev->gadget);
+	usb_gadget_driver_unbind(driver, &dev->gadget);
 
 	disable_irq(IRQ_OTG);
 
@@ -443,7 +443,7 @@ static void stop_activity(struct dwc2_udc *dev,
 	/* report disconnect; the driver is already quiesced */
 	if (driver) {
 		spin_unlock(&dev->lock);
-		driver->disconnect(&dev->gadget);
+		usb_gadget_driver_disconnect(driver, &dev->gadget);
 		spin_lock(&dev->lock);
 	}
 
diff --git a/drivers/usb/gadget/dwc2_udc_otg_xfer_dma.c b/drivers/usb/gadget/dwc2_udc_otg_xfer_dma.c
index 1c34b753511..6949784b970 100644
--- a/drivers/usb/gadget/dwc2_udc_otg_xfer_dma.c
+++ b/drivers/usb/gadget/dwc2_udc_otg_xfer_dma.c
@@ -535,7 +535,7 @@ static int dwc2_udc_irq(int irq, void *_dev)
 		if (dev->gadget.speed != USB_SPEED_UNKNOWN
 		    && dev->driver) {
 			if (dev->driver->suspend)
-				dev->driver->suspend(&dev->gadget);
+				usb_gadget_driver_suspend(dev->driver, &dev->gadget);
 		}
 	}
 
@@ -549,7 +549,7 @@ static int dwc2_udc_irq(int irq, void *_dev)
 			/* Let gadget detect disconnected state */
 			if (dev->driver->disconnect) {
 				spin_unlock_irqrestore(&dev->lock, flags);
-				dev->driver->disconnect(&dev->gadget);
+				usb_gadget_driver_disconnect(dev->driver, &dev->gadget);
 				spin_lock_irqsave(&dev->lock, flags);
 			}
 		}
@@ -1450,7 +1450,7 @@ static void dwc2_ep0_setup(struct dwc2_udc *dev)
 			    __func__);
 
 		spin_unlock(&dev->lock);
-		i = dev->driver->setup(&dev->gadget, usb_ctrl);
+		i = usb_gadget_driver_setup(dev->driver, &dev->gadget, usb_ctrl);
 		spin_lock(&dev->lock);
 
 		if (i < 0) {
diff --git a/drivers/usb/gadget/fotg210.c b/drivers/usb/gadget/fotg210.c
index af43433d88d..8afaf5c75ea 100644
--- a/drivers/usb/gadget/fotg210.c
+++ b/drivers/usb/gadget/fotg210.c
@@ -439,7 +439,7 @@ static void fotg210_setup(struct fotg210_chip *chip)
 	} /* if ((req->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) */
 
 	if (ret == CX_IDLE && chip->driver->setup) {
-		if (chip->driver->setup(&chip->gadget, req) < 0)
+		if (usb_gadget_driver_setup(chip->driver, &chip->gadget, req) < 0)
 			ret = CX_STALL;
 		else
 			ret = CX_FINISH;
@@ -941,7 +941,7 @@ int usb_gadget_register_driver(struct usb_gadget_driver *driver)
 		return -EINVAL;
 	}
 
-	ret = driver->bind(&chip->gadget);
+	ret = usb_gadget_driver_bind(driver, &chip->gadget);
 	if (ret) {
 		debug("fotg210: driver->bind() returned %d\n", ret);
 		return ret;
@@ -955,7 +955,7 @@ int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
 {
 	struct fotg210_chip *chip = &controller;
 
-	driver->unbind(&chip->gadget);
+	usb_gadget_driver_unbind(driver, &chip->gadget);
 	chip->driver = NULL;
 
 	pullup(chip, 0);
diff --git a/drivers/usb/gadget/max3420_udc.c b/drivers/usb/gadget/max3420_udc.c
index fa655c98dcc..3f639c6cda8 100644
--- a/drivers/usb/gadget/max3420_udc.c
+++ b/drivers/usb/gadget/max3420_udc.c
@@ -506,7 +506,7 @@ static void max3420_getstatus(struct max3420_udc *udc)
 		status |= (udc->remote_wkp << USB_DEVICE_REMOTE_WAKEUP);
 		break;
 	case USB_RECIP_INTERFACE:
-		if (udc->driver->setup(&udc->gadget, &udc->setup) < 0)
+		if (usb_gadget_driver_setup(udc->driver, &udc->gadget, &udc->setup) < 0)
 			goto stall;
 		break;
 	case USB_RECIP_ENDPOINT:
@@ -603,7 +603,7 @@ static void max3420_handle_setup(struct max3420_udc *udc)
 		break;
 	}
 
-	if (udc->driver->setup(&udc->gadget, &setup) < 0) {
+	if (usb_gadget_driver_setup(udc->driver, &udc->gadget, &setup) < 0) {
 		/* Stall EP0 */
 		spi_wr8(udc, MAX3420_REG_EPSTALLS,
 			bSTLEP0IN | bSTLEP0OUT | bSTLSTAT);
diff --git a/drivers/usb/gadget/udc/udc-core.c b/drivers/usb/gadget/udc/udc-core.c
index 7f73926cb3e..f07b0baf57c 100644
--- a/drivers/usb/gadget/udc/udc-core.c
+++ b/drivers/usb/gadget/udc/udc-core.c
@@ -111,7 +111,7 @@ EXPORT_SYMBOL_GPL(usb_gadget_set_state);
 void usb_gadget_udc_reset(struct usb_gadget *gadget,
 		struct usb_gadget_driver *driver)
 {
-	driver->reset(gadget);
+	usb_gadget_driver_reset(driver, gadget);
 	usb_gadget_set_state(gadget, USB_STATE_DEFAULT);
 }
 EXPORT_SYMBOL_GPL(usb_gadget_udc_reset);
@@ -228,8 +228,8 @@ static void usb_gadget_remove_driver(struct usb_udc *udc)
 			udc->driver->function);
 
 	usb_gadget_disconnect(udc->gadget);
-	udc->driver->disconnect(udc->gadget);
-	udc->driver->unbind(udc->gadget);
+	usb_gadget_driver_disconnect(udc->driver, udc->gadget);
+	usb_gadget_driver_unbind(udc->driver, udc->gadget);
 	usb_gadget_udc_stop(udc);
 
 	udc->driver = NULL;
@@ -301,12 +301,12 @@ static int udc_bind_to_driver(struct usb_udc *udc, struct usb_gadget_driver *dri
 
 	usb_gadget_udc_set_speed(udc, driver->speed);
 
-	ret = driver->bind(udc->gadget);
+	ret = usb_gadget_driver_bind(driver, udc->gadget);
 	if (ret)
 		goto err1;
 	ret = usb_gadget_udc_start(udc);
 	if (ret) {
-		driver->unbind(udc->gadget);
+		usb_gadget_driver_unbind(driver, udc->gadget);
 		goto err1;
 	}
 	usb_gadget_connect(udc->gadget);
diff --git a/drivers/usb/mtu3/mtu3_gadget_ep0.c b/drivers/usb/mtu3/mtu3_gadget_ep0.c
index 4b0bc5f02d1..5e17378cada 100644
--- a/drivers/usb/mtu3/mtu3_gadget_ep0.c
+++ b/drivers/usb/mtu3/mtu3_gadget_ep0.c
@@ -68,7 +68,7 @@ __acquires(mtu->lock)
 		return -EOPNOTSUPP;
 
 	spin_unlock(&mtu->lock);
-	ret = mtu->gadget_driver->setup(&mtu->g, setup);
+	ret = usb_gadget_driver_setup(mtu->gadget_driver, &mtu->g, setup);
 	spin_lock(&mtu->lock);
 
 	dev_dbg(mtu->dev, "%s ret %d\n", __func__, ret);
diff --git a/drivers/usb/musb-new/musb_gadget_ep0.c b/drivers/usb/musb-new/musb_gadget_ep0.c
index 7fdd24005e7..b2c9027684d 100644
--- a/drivers/usb/musb-new/musb_gadget_ep0.c
+++ b/drivers/usb/musb-new/musb_gadget_ep0.c
@@ -636,7 +636,7 @@ __acquires(musb->lock)
 	if (!musb->gadget_driver)
 		return -EOPNOTSUPP;
 	spin_unlock(&musb->lock);
-	retval = musb->gadget_driver->setup(&musb->g, ctrlrequest);
+	retval = usb_gadget_driver_setup(musb->gadget_driver, &musb->g, ctrlrequest);
 	spin_lock(&musb->lock);
 	return retval;
 }
diff --git a/drivers/usb/musb-new/musb_uboot.c b/drivers/usb/musb-new/musb_uboot.c
index 62c5e8e5fa4..67dc11669d2 100644
--- a/drivers/usb/musb-new/musb_uboot.c
+++ b/drivers/usb/musb-new/musb_uboot.c
@@ -406,7 +406,7 @@ int usb_gadget_register_driver(struct usb_gadget_driver *driver)
 		return ret;
 	}
 
-	ret = driver->bind(&gadget->g);
+	ret = usb_gadget_driver_bind(driver, &gadget->g);
 	if (ret < 0) {
 		printf("bind failed with %d\n", ret);
 		return ret;
@@ -418,9 +418,9 @@ int usb_gadget_register_driver(struct usb_gadget_driver *driver)
 int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
 {
 	if (driver->disconnect)
-		driver->disconnect(&gadget->g);
+		usb_gadget_driver_disconnect(driver, &gadget->g);
 	if (driver->unbind)
-		driver->unbind(&gadget->g);
+		usb_gadget_driver_unbind(driver, &gadget->g);
 	return 0;
 }
 #endif /* CONFIG_USB_MUSB_GADGET */
diff --git a/include/linux/usb/gadget.h b/include/linux/usb/gadget.h
index b3f4b8d134c..2f694fc25c1 100644
--- a/include/linux/usb/gadget.h
+++ b/include/linux/usb/gadget.h
@@ -856,6 +856,42 @@ struct usb_gadget_driver {
 	void			(*reset)(struct usb_gadget *);
 };
 
+static inline int usb_gadget_driver_bind(struct usb_gadget_driver *d,
+					 struct usb_gadget *g)
+{
+	return d->bind(g);
+}
+
+static inline void usb_gadget_driver_unbind(struct usb_gadget_driver *d,
+					    struct usb_gadget *g)
+{
+	d->unbind(g);
+}
+
+static inline int usb_gadget_driver_setup(struct usb_gadget_driver *d,
+					  struct usb_gadget *g,
+					  struct usb_ctrlrequest *r)
+{
+	return d->setup(g, r);
+}
+
+static inline void usb_gadget_driver_disconnect(struct usb_gadget_driver *d,
+						struct usb_gadget *g)
+{
+	d->disconnect(g);
+}
+
+static inline void usb_gadget_driver_suspend(struct usb_gadget_driver *d,
+					     struct usb_gadget *g)
+{
+	d->suspend(g);
+}
+
+static inline void usb_gadget_driver_reset(struct usb_gadget_driver *d,
+					   struct usb_gadget *g)
+{
+	d->reset(g);
+}
 
 /*-------------------------------------------------------------------------*/
 
-- 
2.35.1



More information about the U-Boot mailing list