From cf14a2f01ae1749143060aa04df6f58dd5859d38 Mon Sep 17 00:00:00 2001 From: Vincent Palatin Date: Fri, 29 Jul 2011 14:12:02 -0700 Subject: eth: remove usb-ethernet devices before re-enumerating them Fix the crash when running several times usb_init() with a USB ethernet device plugged. BUG=chromium-os:15206 TEST=run several "usb start" commands and having inserted/removed all possible combinations of 0 to 3 USB Ethernet devices. Change-Id: I23109ad8774daeb07eeba5de615700fec643dfdb Reviewed-on: http://gerrit.chromium.org/gerrit/5026 Tested-by: Vincent Palatin Reviewed-by: Doug Anderson Reviewed-by: Simon Glass --- drivers/usb/eth/usb_ether.c | 7 +++++-- include/net.h | 1 + net/eth.c | 29 +++++++++++++++++++++++++++++ 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/drivers/usb/eth/usb_ether.c b/drivers/usb/eth/usb_ether.c index 68a08836ec9..b7106731b27 100644 --- a/drivers/usb/eth/usb_ether.c +++ b/drivers/usb/eth/usb_ether.c @@ -115,8 +115,11 @@ int usb_host_eth_scan(int mode) old_async = usb_disable_asynch(1); /* asynch transfer not allowed */ - for (i = 0; i < USB_MAX_ETH_DEV; i++) - memset(&usb_eth[i], 0, sizeof(usb_eth[i])); + /* unregister a previously detected device */ + for (i = 0; i < usb_max_eth_dev; i++) + eth_unregister(&usb_eth[i].eth_dev); + + memset(usb_eth, 0, sizeof(usb_eth)); for (i = 0; prob_dev[i].probe; i++) { if (prob_dev[i].before_probe) diff --git a/include/net.h b/include/net.h index 018a74402c8..01932054714 100644 --- a/include/net.h +++ b/include/net.h @@ -117,6 +117,7 @@ struct eth_device { extern int eth_initialize(bd_t *bis); /* Initialize network subsystem */ extern int eth_register(struct eth_device* dev);/* Register network device */ +extern int eth_unregister(struct eth_device* dev);/* Remove network device */ extern void eth_try_another(int first_restart); /* Change the device */ #ifdef CONFIG_NET_MULTI extern void eth_set_current(void); /* set nterface to ethcur var */ diff --git a/net/eth.c b/net/eth.c index 65238340601..0b198250a93 100644 --- a/net/eth.c +++ b/net/eth.c @@ -206,6 +206,35 @@ int eth_register(struct eth_device *dev) return 0; } +int eth_unregister(struct eth_device *dev) +{ + struct eth_device *cur; + + /* No device */ + if (!eth_devices) + return -1; + + for (cur = eth_devices; cur->next != eth_devices && cur->next != dev; + cur = cur->next) + ; + + /* Device not found */ + if (cur->next != dev) + return -1; + + cur->next = dev->next; + + if (eth_devices == dev) + eth_devices = dev->next == eth_devices ? NULL : dev->next; + + if (eth_current == dev) { + eth_current = eth_devices; + eth_current_changed(); + } + + return 0; +} + int eth_initialize(bd_t *bis) { unsigned char env_enetaddr[6]; -- cgit v1.2.3