mirror of
https://github.com/apache/nuttx.git
synced 2026-06-06 16:50:55 +08:00
drivers: Destroy mutex and sem in the error path
also correct the order to ensure the memory free is last step Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
This commit is contained in:
committed by
Masayuki Ishikawa
parent
dee38ce3e8
commit
dca5a3483f
@@ -1480,11 +1480,15 @@ int cc1101_register(FAR const char *path, FAR struct cc1101_dev_s *dev)
|
||||
dev->fifo_len = 0;
|
||||
nxmutex_init(&dev->devlock);
|
||||
nxmutex_init(&dev->lock_rx_buffer);
|
||||
nxsem_init(&(dev->sem_rx), 0, 0);
|
||||
nxsem_init(&(dev->sem_tx), 0, 0);
|
||||
nxsem_init(&dev->sem_rx, 0, 0);
|
||||
nxsem_init(&dev->sem_tx, 0, 0);
|
||||
|
||||
if (cc1101_init2(dev) < 0)
|
||||
{
|
||||
nxmutex_destroy(&dev->devlock);
|
||||
nxmutex_destroy(&dev->lock_rx_buffer);
|
||||
nxsem_destroy(&dev->sem_rx);
|
||||
nxsem_destroy(&dev->sem_tx);
|
||||
kmm_free(dev);
|
||||
wlerr("ERROR: Failed to initialize cc1101_init\n");
|
||||
return -ENODEV;
|
||||
|
||||
@@ -3491,7 +3491,6 @@ FAR void *gs2200m_register(FAR const char *devpath,
|
||||
|
||||
size = sizeof(struct gs2200m_dev_s);
|
||||
dev = (FAR struct gs2200m_dev_s *)kmm_malloc(size);
|
||||
|
||||
if (!dev)
|
||||
{
|
||||
wlerr("Failed to allocate instance.\n");
|
||||
@@ -3506,10 +3505,7 @@ FAR void *gs2200m_register(FAR const char *devpath,
|
||||
|
||||
nxmutex_init(&dev->dev_lock);
|
||||
|
||||
dev->pfd = NULL;
|
||||
|
||||
ret = gs2200m_initialize(dev, lower);
|
||||
|
||||
if (ret < 0)
|
||||
{
|
||||
wlerr("Failed to initialize driver: %d\n", ret);
|
||||
@@ -3517,7 +3513,6 @@ FAR void *gs2200m_register(FAR const char *devpath,
|
||||
}
|
||||
|
||||
ret = register_driver(devpath, &g_gs2200m_fops, 0666, dev);
|
||||
|
||||
if (ret < 0)
|
||||
{
|
||||
wlerr("Failed to register driver: %d\n", ret);
|
||||
@@ -3525,10 +3520,16 @@ FAR void *gs2200m_register(FAR const char *devpath,
|
||||
}
|
||||
|
||||
ret = netdev_register(&dev->net_dev, NET_LL_IEEE80211);
|
||||
if (ret < 0)
|
||||
{
|
||||
unregister_driver(devpath);
|
||||
goto errout;
|
||||
}
|
||||
|
||||
return (FAR void *)dev;
|
||||
return dev;
|
||||
|
||||
errout:
|
||||
nxmutex_destroy(&dev->dev_lock);
|
||||
kmm_free(dev);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -1439,10 +1439,14 @@ static int nrf24l01_unregister(FAR struct nrf24l01_dev_s *dev)
|
||||
/* Free memory */
|
||||
|
||||
#ifdef CONFIG_WL_NRF24L01_RXSUPPORT
|
||||
nxmutex_destroy(&dev->lock_fifo);
|
||||
nxsem_destroy(&dev->sem_rx);
|
||||
kmm_free(dev->rx_fifo);
|
||||
#endif
|
||||
kmm_free(dev);
|
||||
|
||||
nxmutex_destroy(&dev->devlock);
|
||||
nxsem_destroy(&dev->sem_tx);
|
||||
kmm_free(dev);
|
||||
return OK;
|
||||
}
|
||||
|
||||
@@ -1484,14 +1488,16 @@ int nrf24l01_register(FAR struct spi_dev_s *spi,
|
||||
#ifdef CONFIG_WL_NRF24L01_RXSUPPORT
|
||||
if ((rx_fifo = kmm_malloc(CONFIG_WL_NRF24L01_RXFIFO_LEN)) == NULL)
|
||||
{
|
||||
nxmutex_destroy(&dev->devlock);
|
||||
nxsem_destroy(&dev->sem_tx);
|
||||
kmm_free(dev);
|
||||
return -ENOMEM;
|
||||
}
|
||||
|
||||
dev->rx_fifo = rx_fifo;
|
||||
dev->rx_fifo = rx_fifo;
|
||||
|
||||
nxmutex_init(&dev->lock_fifo);
|
||||
nxsem_init(&(dev->sem_rx), 0, 0);
|
||||
nxsem_init(&dev->sem_rx, 0, 0);
|
||||
nxsem_set_protocol(&dev->sem_rx, SEM_PRIO_NONE);
|
||||
#endif
|
||||
|
||||
|
||||
@@ -2670,7 +2670,7 @@ int spirit_netdev_initialize(FAR struct spi_dev_s *spi,
|
||||
if (ret < 0)
|
||||
{
|
||||
wlerr("ERROR: spirit_hw_initialize failed: %d\n", ret);
|
||||
goto errout_with_attach;
|
||||
goto errout_with_alloc;
|
||||
}
|
||||
|
||||
#ifdef CONFIG_NET_6LOWPAN
|
||||
@@ -2682,15 +2682,6 @@ int spirit_netdev_initialize(FAR struct spi_dev_s *spi,
|
||||
priv->radio.r_dev.d_buf = g_iobuffer.rb_buf;
|
||||
#endif
|
||||
|
||||
/* Register the device with the OS so that IOCTLs can be performed. */
|
||||
|
||||
ret = netdev_register(dev, NET_LL_PKTRADIO);
|
||||
if (ret < 0)
|
||||
{
|
||||
wlerr("ERROR: netdev_register failed: %d\n", ret);
|
||||
goto errout_with_attach;
|
||||
}
|
||||
|
||||
/* Attach irq */
|
||||
|
||||
ret = lower->attach(lower, spirit_interrupt, priv);
|
||||
@@ -2700,6 +2691,15 @@ int spirit_netdev_initialize(FAR struct spi_dev_s *spi,
|
||||
goto errout_with_alloc;
|
||||
}
|
||||
|
||||
/* Register the device with the OS so that IOCTLs can be performed. */
|
||||
|
||||
ret = netdev_register(dev, NET_LL_PKTRADIO);
|
||||
if (ret < 0)
|
||||
{
|
||||
wlerr("ERROR: netdev_register failed: %d\n", ret);
|
||||
goto errout_with_attach;
|
||||
}
|
||||
|
||||
/* Enable Radio IRQ */
|
||||
|
||||
lower->enable(lower, true);
|
||||
@@ -2709,6 +2709,8 @@ errout_with_attach:
|
||||
lower->attach(lower, NULL, NULL);
|
||||
|
||||
errout_with_alloc:
|
||||
nxmutex_destroy(&priv->rxlock);
|
||||
nxmutex_destroy(&priv->txlock);
|
||||
kmm_free(priv);
|
||||
return ret;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user