drivers/usb/gadget/function/u_ether.c
Source file repositories/reference/linux-study-clean/drivers/usb/gadget/function/u_ether.c
File Facts
- System
- Linux kernel
- Corpus path
drivers/usb/gadget/function/u_ether.c- Extension
.c- Size
- 31291 bytes
- Lines
- 1276
- Domain
- Driver Families
- Bucket
- drivers/usb
- Inferred role
- Driver Families: operation-table or driver-model contract
- Status
- pattern implementation candidate
Why This File Exists
Repeatable hardware-adapter layer. Deep compatibility for every driver is out of scope; this atlas records patterns, probe lifecycles, bus glue, IRQ/DMA usage, and links back to core abstractions.
- Repeatable hardware-adapter layer. Deep compatibility for every driver is out of scope; this atlas records patterns, probe lifecycles, bus glue, IRQ/DMA usage, and links back to core abstractions.
- Defines an operation table; this is where Linux turns generic core objects into subsystem-specific behavior.
- Uses kernel synchronization; read lock ordering, sleepability, and interrupt context assumptions before translating.
- Touches IRQ or DMA behavior; this matters for the representative real-device path.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
linux/kernel.hlinux/module.hlinux/gfp.hlinux/device.hlinux/ctype.hlinux/etherdevice.hlinux/ethtool.hlinux/hex.hlinux/if_vlan.hlinux/string_helpers.hlinux/usb/composite.hu_ether.h
Detected Declarations
struct eth_devfunction qlenfunction eth_get_drvinfofunction defer_keventfunction rx_submitfunction rx_completefunction preallocfunction alloc_requestsfunction rx_fillfunction eth_workfunction tx_completefunction is_promiscfunction ether_wakeup_hostfunction eth_start_xmitfunction disconnectfunction eth_startfunction eth_openfunction eth_stopfunction get_ether_addrfunction get_ether_addr_strfunction gether_register_netdevfunction gether_set_gadgetfunction gether_attach_gadgetfunction gether_detach_gadgetfunction gether_set_dev_addrfunction gether_get_dev_addrfunction gether_set_host_addrfunction gether_get_host_addrfunction gether_get_host_addr_cdcfunction gether_get_host_addr_u8function gether_set_qmultfunction gether_get_qmultfunction gether_get_ifnamefunction gether_set_ifnamefunction gether_suspendfunction gether_resumefunction gether_cleanupfunction activefunction inactiveexport gether_setup_nameexport gether_setup_name_defaultexport gether_register_netdevexport gether_set_gadgetexport gether_attach_gadgetexport gether_detach_gadgetexport gether_set_dev_addrexport gether_get_dev_addrexport gether_set_host_addr
Annotated Snippet
static const struct net_device_ops eth_netdev_ops = {
.ndo_open = eth_open,
.ndo_stop = eth_stop,
.ndo_start_xmit = eth_start_xmit,
.ndo_set_mac_address = eth_mac_addr,
.ndo_validate_addr = eth_validate_addr,
};
static const struct device_type gadget_type = {
.name = "gadget",
};
/*
* gether_setup_name - initialize one ethernet-over-usb link
* @g: gadget to associated with these links
* @ethaddr: NULL, or a buffer in which the ethernet address of the
* host side of the link is recorded
* @netname: name for network device (for example, "usb")
* Context: may sleep
*
* This sets up the single network link that may be exported by a
* gadget driver using this framework. The link layer addresses are
* set up using module parameters.
*
* Returns an eth_dev pointer on success, or an ERR_PTR on failure.
*/
struct eth_dev *gether_setup_name(struct usb_gadget *g,
const char *dev_addr, const char *host_addr,
u8 ethaddr[ETH_ALEN], unsigned qmult, const char *netname)
{
struct eth_dev *dev;
struct net_device *net;
int status;
u8 addr[ETH_ALEN];
net = alloc_etherdev(sizeof *dev);
if (!net)
return ERR_PTR(-ENOMEM);
dev = netdev_priv(net);
spin_lock_init(&dev->lock);
spin_lock_init(&dev->req_lock);
INIT_WORK(&dev->work, eth_work);
INIT_LIST_HEAD(&dev->tx_reqs);
INIT_LIST_HEAD(&dev->rx_reqs);
skb_queue_head_init(&dev->rx_frames);
/* network device setup */
dev->net = net;
dev->qmult = qmult;
snprintf(net->name, sizeof(net->name), "%s%%d", netname);
if (get_ether_addr(dev_addr, addr)) {
net->addr_assign_type = NET_ADDR_RANDOM;
dev_warn(&g->dev,
"using random %s ethernet address\n", "self");
} else {
net->addr_assign_type = NET_ADDR_SET;
}
eth_hw_addr_set(net, addr);
if (get_ether_addr(host_addr, dev->host_mac))
dev_warn(&g->dev,
"using random %s ethernet address\n", "host");
if (ethaddr)
memcpy(ethaddr, dev->host_mac, ETH_ALEN);
net->netdev_ops = ð_netdev_ops;
net->ethtool_ops = &ops;
/* MTU range: 14 - 15412 */
net->min_mtu = ETH_HLEN;
net->max_mtu = GETHER_MAX_MTU_SIZE;
dev->gadget = g;
SET_NETDEV_DEV(net, &g->dev);
SET_NETDEV_DEVTYPE(net, &gadget_type);
status = register_netdev(net);
if (status < 0) {
dev_dbg(&g->dev, "register_netdev failed, %d\n", status);
free_netdev(net);
dev = ERR_PTR(status);
} else {
INFO(dev, "MAC %pM\n", net->dev_addr);
INFO(dev, "HOST MAC %pM\n", dev->host_mac);
/*
Annotation
- Immediate include surface: `linux/kernel.h`, `linux/module.h`, `linux/gfp.h`, `linux/device.h`, `linux/ctype.h`, `linux/etherdevice.h`, `linux/ethtool.h`, `linux/hex.h`.
- Detected declarations: `struct eth_dev`, `function qlen`, `function eth_get_drvinfo`, `function defer_kevent`, `function rx_submit`, `function rx_complete`, `function prealloc`, `function alloc_requests`, `function rx_fill`, `function eth_work`.
- Atlas domain: Driver Families / drivers/usb.
- Implementation status: pattern implementation candidate.
- Synchronization appears in or near this file; preserve lock ordering, sleepability, and interrupt-context constraints.
- IRQ or DMA behavior appears here, which is relevant to the selected PCIe/NVMe device path.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.