From ad59ef075f4df4f01ce05c030cebbf31be9adb9c Mon Sep 17 00:00:00 2001 From: dongjiuzhu1 Date: Wed, 13 Dec 2023 12:28:36 +0800 Subject: [PATCH] drivers/usbdev: remove first uninitialization and release resouce once. Signed-off-by: dongjiuzhu1 --- drivers/usbdev/cdcacm.c | 57 ++++---------------------------------- drivers/usbdev/cdcecm.c | 54 ++---------------------------------- drivers/usbdev/composite.c | 15 ++-------- drivers/usbdev/rndis.c | 32 +++------------------ drivers/usbdev/usbdev_fs.c | 34 ++++++++--------------- drivers/usbdev/usbmsc.c | 27 +----------------- include/nuttx/usb/cdcacm.h | 6 ---- 7 files changed, 27 insertions(+), 198 deletions(-) diff --git a/drivers/usbdev/cdcacm.c b/drivers/usbdev/cdcacm.c index dcf9cbf3ca..3789f8e07c 100644 --- a/drivers/usbdev/cdcacm.c +++ b/drivers/usbdev/cdcacm.c @@ -3058,46 +3058,19 @@ int cdcacm_initialize(int minor, FAR void **handle) * standalone USB driver: * * classdev - The class object returned by cdcacm_classobject() - * handle - The opaque handle representing the class object returned by - * a previous call to cdcacm_initialize(). * * Returned Value: * None * ****************************************************************************/ -#ifdef CONFIG_CDCACM_COMPOSITE void cdcacm_uninitialize(FAR struct usbdevclass_driver_s *classdev) -#else -void cdcacm_uninitialize(FAR void *handle) -#endif { -#ifdef CONFIG_CDCACM_COMPOSITE FAR struct cdcacm_driver_s *drvr = (FAR struct cdcacm_driver_s *)classdev; -#else - FAR struct cdcacm_driver_s *drvr = (FAR struct cdcacm_driver_s *)handle; -#endif FAR struct cdcacm_dev_s *priv = drvr->dev; char devname[CDCACM_DEVNAME_SIZE]; int ret; -#ifdef CONFIG_CDCACM_COMPOSITE - /* Check for pass 2 uninitialization. We did most of the work on the - * first pass uninitialization. - */ - - if (priv->minor == (uint8_t)-1) - { - /* In this second and final pass, all that remains to be done is to - * free the memory resources. - */ - - wd_cancel(&priv->rxfailsafe); - kmm_free(priv); - return; - } -#endif - /* Un-register the CDC/ACM TTY device */ snprintf(devname, CDCACM_DEVNAME_SIZE, CDCACM_DEVNAME_FORMAT, priv->minor); @@ -3108,35 +3081,17 @@ void cdcacm_uninitialize(FAR void *handle) (uint16_t)-ret); } - /* Unregister the driver (unless we are a part of a composite device). The - * device unregister logic will (1) return all of the requests to us then - * (2) all the unbind method. - * - * The same thing will happen in the composite case except that: (1) the - * composite driver will call usbdev_unregister() which will (2) return the - * requests for all members of the composite, and (3) call the unbind - * method in the composite device which will (4) call the unbind method - * for this device. - */ - #ifndef CONFIG_CDCACM_COMPOSITE usbdev_unregister(&drvr->drvr); +#endif /* And free the memory resources. */ - wd_cancel(&priv->rxfailsafe); - kmm_free(priv); - -#else - /* For the case of the composite driver, there is a two pass - * uninitialization sequence. We cannot yet free the driver structure. - * We will do that on the second pass. We mark the fact that we have - * already uninitialized by setting the minor number to -1. If/when we - * are called again, then we will free the memory resources. - */ - - priv->minor = (uint8_t)-1; -#endif + if (priv->serdev.open_count <= 0) + { + wd_cancel(&priv->rxfailsafe); + kmm_free(priv); + } } /**************************************************************************** diff --git a/drivers/usbdev/cdcecm.c b/drivers/usbdev/cdcecm.c index 50ba1b0443..1441a9af1d 100644 --- a/drivers/usbdev/cdcecm.c +++ b/drivers/usbdev/cdcecm.c @@ -138,7 +138,6 @@ struct cdcecm_driver_s struct net_driver_s dev; /* Interface understood by the * network */ - bool registered; /* netdev is currently registered */ }; /**************************************************************************** @@ -1865,11 +1864,8 @@ static int cdcecm_classobject(int minor, if (ret < 0) { nerr("netdev_register failed. ret: %d\n", ret); - return ret; } - self->registered = true; - *classdev = (FAR struct usbdevclass_driver_s *)self; return ret; } @@ -1888,44 +1884,18 @@ static int cdcecm_classobject(int minor, * CDC/ECM driver is an internal part of a composite device, or a * standalone USB driver: * - * classdev - The class object returned by cdcacm_classobject() - * handle - The opaque handle representing the class object returned by - * a previous call to cdcacm_initialize(). + * classdev - The class object returned by cdcecm_classobject() * * Returned Value: * None * ****************************************************************************/ -#ifdef CONFIG_CDCECM_COMPOSITE void cdcecm_uninitialize(FAR struct usbdevclass_driver_s *classdev) -#else -void cdcecm_uninitialize(FAR void *handle) -#endif { -#ifdef CONFIG_CDCECM_COMPOSITE FAR struct cdcecm_driver_s *self = (FAR struct cdcecm_driver_s *)classdev; -#else - FAR struct cdcecm_driver_s *self = (FAR struct cdcecm_driver_s *)handle; -#endif int ret; -#ifdef CONFIG_CDCECM_COMPOSITE - /* Check for pass 2 uninitialization. We did most of the work on the - * first pass uninitialization. - */ - - if (!self->registered) - { - /* In this second and final pass, all that remains to be done is to - * free the memory resources. - */ - - kmm_free(self); - return; - } -#endif - /* Un-register the CDC/ECM netdev device */ ret = netdev_unregister(&self->dev); @@ -1934,33 +1904,13 @@ void cdcecm_uninitialize(FAR void *handle) nerr("ERROR: netdev_unregister failed. ret: %d\n", ret); } - /* For the case of the composite driver, there is a two pass - * uninitialization sequence. We cannot yet free the driver structure. - * We will do that on the second pass. We mark the fact that we have - * already uninitialized by setting the registered flag to false. - * If/when we are called again, then we will free the memory resources. - */ - - self->registered = false; /* Successfully unregistered netdev */ - - /* Unregister the driver (unless we are a part of a composite device). The - * device unregister logic will (1) return all of the requests to us then - * (2) call the unbind method. - * - * The same thing will happen in the composite case except that: (1) the - * composite driver will call usbdev_unregister() which will (2) return the - * requests for all members of the composite, and (3) call the unbind - * method in the composite device which will (4) call the unbind method - * for this device. - */ - #ifndef CONFIG_CDCECM_COMPOSITE usbdev_unregister(&self->usbdev); +#endif /* And free the driver structure */ kmm_free(self); -#endif } /**************************************************************************** diff --git a/drivers/usbdev/composite.c b/drivers/usbdev/composite.c index 306cc40a64..5d3ca13a33 100644 --- a/drivers/usbdev/composite.c +++ b/drivers/usbdev/composite.c @@ -1115,24 +1115,15 @@ void composite_uninitialize(FAR void *handle) DEBUGASSERT(alloc != NULL); - /* First phase uninitialization each of the member classes */ - priv = &alloc->dev; - for (i = 0; i < priv->ndevices; i++) - { - priv->device[i].compdesc.uninitialize(priv->device[i].dev); - } - /* Then unregister and destroy the composite class */ usbdev_unregister(&alloc->drvr.drvr); - /* Free any resources used by the composite driver */ - - /* None */ - - /* Second phase uninitialization: Clean up all memory resources */ + /* Uninitialization each of the member classes and clean up + * all memory resources + */ for (i = 0; i < priv->ndevices; i++) { diff --git a/drivers/usbdev/rndis.c b/drivers/usbdev/rndis.c index e6f798eb60..2d189d144c 100644 --- a/drivers/usbdev/rndis.c +++ b/drivers/usbdev/rndis.c @@ -160,8 +160,6 @@ struct rndis_dev_s struct work_s rxwork; /* Worker for dispatching RX packets */ struct work_s pollwork; /* TX poll worker */ - bool registered; /* Has netdev_register() been called */ - uint8_t config; /* USB Configuration number */ FAR struct rndis_req_s *net_req; /* Pointer to request whose buffer is assigned to network */ FAR struct rndis_req_s *rx_req; /* Pointer request container that holds RX buffer */ @@ -2312,12 +2310,6 @@ static void usbclass_unbind(FAR struct usbdevclass_driver_s *driver, usbdev_freereq(priv->epbulkout, priv->rdreq); } - if (priv->registered) - { - netdev_unregister(&priv->netdev); - priv->registered = false; - } - /* Free write requests that are not in use (which should be all * of them */ @@ -2872,34 +2864,18 @@ static int usbclass_classobject(int minor, if (ret) { uerr("Failed to register net device"); - return ret; } - drvr->dev->registered = true; - - return OK; + return ret; } static void usbclass_uninitialize(FAR struct usbdevclass_driver_s *classdev) { FAR struct rndis_driver_s *drvr = (FAR struct rndis_driver_s *)classdev; FAR struct rndis_alloc_s *alloc = (FAR struct rndis_alloc_s *)drvr->dev; - if (!alloc->dev.registered) - { -#ifdef CONFIG_RNDIS_COMPOSITE - kmm_free(alloc); -#endif - return; - } - if (drvr->dev->registered) - { - netdev_unregister(&drvr->dev->netdev); - drvr->dev->registered = false; -#ifndef CONFIG_RNDIS_COMPOSITE - kmm_free(alloc); -#endif - return; - } + + netdev_unregister(&drvr->dev->netdev); + kmm_free(alloc); } /**************************************************************************** diff --git a/drivers/usbdev/usbdev_fs.c b/drivers/usbdev/usbdev_fs.c index 34095125c7..f94f880dfd 100644 --- a/drivers/usbdev/usbdev_fs.c +++ b/drivers/usbdev/usbdev_fs.c @@ -80,7 +80,6 @@ struct usbdev_fs_ep_s struct usbdev_fs_dev_s { FAR struct composite_dev_s *cdev; - bool registered; uint8_t config; struct usbdev_devinfo_s devinfo; FAR struct usbdev_fs_ep_s *eps; @@ -531,14 +530,12 @@ static int usbdev_fs_close(FAR struct file *filep) if (do_free) { + FAR struct usbdev_fs_driver_s *alloc = container_of( + fs, FAR struct usbdev_fs_driver_s, dev); + kmm_free(fs->eps); fs->eps = NULL; - if (!fs->registered) - { - FAR struct usbdev_fs_driver_s *alloc = container_of( - fs, FAR struct usbdev_fs_driver_s, dev); - kmm_free(alloc); - } + kmm_free(alloc); } } else @@ -1394,7 +1391,6 @@ int usbdev_fs_classobject(int minor, alloc->drvr.ops = &g_usbdev_fs_classops; *classdev = &alloc->drvr; - alloc->dev.registered = true; return OK; } @@ -1410,26 +1406,18 @@ void usbdev_fs_classuninitialize(FAR struct usbdevclass_driver_s *classdev) { FAR struct usbdev_fs_driver_s *alloc = container_of( classdev, FAR struct usbdev_fs_driver_s, drvr); + FAR struct usbdev_fs_dev_s *fs = &alloc->dev; + int i; - if (alloc->dev.registered) + for (i = 0; i < fs->devinfo.nendpoints; i++) { - alloc->dev.registered = false; - } - else - { - FAR struct usbdev_fs_dev_s *fs = &alloc->dev; - int i; - - for (i = 0; i < fs->devinfo.nendpoints; i++) + if (fs->eps != NULL && fs->eps[i].crefs > 0) { - if (fs->eps != NULL && fs->eps[i].crefs > 0) - { - return; - } + return; } - - kmm_free(alloc); } + + kmm_free(alloc); } /**************************************************************************** diff --git a/drivers/usbdev/usbmsc.c b/drivers/usbdev/usbmsc.c index 98247d41ae..a2c91853b0 100644 --- a/drivers/usbdev/usbmsc.c +++ b/drivers/usbdev/usbmsc.c @@ -1745,7 +1745,7 @@ int usbmsc_classobject(FAR void *handle, void usbmsc_uninitialize(FAR void *handle) { - FAR struct usbmsc_alloc_s *alloc = (FAR struct usbmsc_alloc_s *)handle; + FAR struct usbmsc_alloc_s *alloc = handle; FAR struct usbmsc_dev_s *priv; irqstate_t flags; int ret; @@ -1761,22 +1761,6 @@ void usbmsc_uninitialize(FAR void *handle) priv = &alloc->dev; -#ifdef CONFIG_USBMSC_COMPOSITE - /* Check for pass 2 uninitialization. We did most of the work on the - * first pass uninitialization. - */ - - if (priv->thpid == 0) - { - /* In this second and final pass, all that remains to be done is to - * free the memory resources. - */ - - kmm_free(priv); - return; - } -#endif - /* If the thread hasn't already exitted, tell it to exit now */ if (priv->thstate != USBMSC_STATE_NOTSTARTED) @@ -1855,16 +1839,7 @@ void usbmsc_uninitialize(FAR void *handle) nxsem_destroy(&priv->thsynch); nxmutex_destroy(&priv->thlock); nxsem_destroy(&priv->thwaitsem); - -#ifndef CONFIG_USBMSC_COMPOSITE - /* For the case of the composite driver, there is a two pass - * uninitialization sequence. We cannot yet free the driver structure. - * We will do that on the second pass (and we will know that it is the - * second pass because of priv->thpid == 0) - */ - kmm_free(priv); -#endif } /**************************************************************************** diff --git a/include/nuttx/usb/cdcacm.h b/include/nuttx/usb/cdcacm.h index cda993b697..d9ad17d108 100644 --- a/include/nuttx/usb/cdcacm.h +++ b/include/nuttx/usb/cdcacm.h @@ -378,19 +378,13 @@ int cdcacm_initialize(int minor, FAR void **handle); * standalone USB driver: * * classdev - The class object returned by cdcacm_classobject() - * handle - The opaque handle representing the class object returned by - * a previous call to cdcacm_initialize(). * * Returned Value: * None * ****************************************************************************/ -#if defined(CONFIG_USBDEV_COMPOSITE) && defined(CONFIG_CDCACM_COMPOSITE) void cdcacm_uninitialize(FAR struct usbdevclass_driver_s *classdev); -#else -void cdcacm_uninitialize(FAR void *handle); -#endif /**************************************************************************** * Name: cdcacm_get_composite_devdesc