Commit 0c098112 authored by Kuen-Han Tsai's avatar Kuen-Han Tsai Committed by Greg Kroah-Hartman
Browse files

usb: gadget: u_ether: Add auto-cleanup helper for freeing net_device



The net_device in the u_ether framework currently requires explicit
calls to unregister and free the device.

Introduce gether_unregister_free_netdev() and the corresponding
auto-cleanup macro. This ensures that if a net_device is registered, it
is properly unregistered and the associated work queue is flushed before
the memory is freed.

This is a preparatory patch to simplify error handling paths in gadget
drivers by removing the need for explicit goto labels for net_device
cleanup.

Signed-off-by: default avatarKuen-Han Tsai <khtsai@google.com>
Link: https://patch.msgid.link/20251230-ncm-refactor-v1-2-793e347bc7a7@google.com


Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
parent e065c6a7
Loading
Loading
Loading
Loading
+15 −0
Original line number Diff line number Diff line
@@ -1125,6 +1125,21 @@ void gether_cleanup(struct eth_dev *dev)
}
EXPORT_SYMBOL_GPL(gether_cleanup);

void gether_unregister_free_netdev(struct net_device *net)
{
	if (!net)
		return;

	struct eth_dev *dev = netdev_priv(net);

	if (net->reg_state == NETREG_REGISTERED) {
		unregister_netdev(net);
		flush_work(&dev->work);
	}
	free_netdev(net);
}
EXPORT_SYMBOL_GPL(gether_unregister_free_netdev);

/**
 * gether_connect - notify network layer that USB link is active
 * @link: the USB link, set up with endpoints, descriptors matching
+2 −0
Original line number Diff line number Diff line
@@ -283,6 +283,8 @@ int gether_get_ifname(struct net_device *net, char *name, int len);
int gether_set_ifname(struct net_device *net, const char *name, int len);

void gether_cleanup(struct eth_dev *dev);
void gether_unregister_free_netdev(struct net_device *net);
DEFINE_FREE(free_gether_netdev, struct net_device *, gether_unregister_free_netdev(_T));

void gether_setup_opts_default(struct gether_opts *opts, const char *name);
void gether_apply_opts(struct net_device *net, struct gether_opts *opts);