drivers/usb/gadget/function/f_ncm.c
Source file repositories/reference/linux-study-clean/drivers/usb/gadget/function/f_ncm.c
File Facts
- System
- Linux kernel
- Corpus path
drivers/usb/gadget/function/f_ncm.c- Extension
.c- Size
- 48715 bytes
- Lines
- 1804
- Domain
- Driver Families
- Bucket
- drivers/usb
- Inferred role
- Driver Families: implementation source
- Status
- source 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.
- Uses kernel synchronization; read lock ordering, sleepability, and interrupt context assumptions before translating.
- Allocates kernel memory; connect allocation flags and lifetime to context constraints.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
linux/cleanup.hlinux/kernel.hlinux/interrupt.hlinux/module.hlinux/device.hlinux/etherdevice.hlinux/crc32.hlinux/string_choices.hlinux/usb/cdc.hlinux/usb/gadget.hu_ether.hu_ether_configfs.hu_ncm.hconfigfs.h
Detected Declarations
struct f_ncmstruct ndp_parser_optsenum ncm_notify_statefunction put_ncmfunction get_ncmfunction ncm_reset_valuesfunction ncm_do_notifyfunction ncm_notifyfunction ncm_notify_completefunction ncm_ep0out_completefunction ncm_setupfunction ncm_set_altfunction get_altfunction ncm_tx_timeoutfunction ncm_unwrap_ntbfunction ncm_disablefunction SET_INTERFACEfunction ncm_closefunction ncm_bindfunction scoped_guardfunction ncm_opts_max_segment_size_showfunction ncm_opts_max_segment_size_storefunction ncm_free_instfunction ncm_freefunction ncm_unbind
Annotated Snippet
struct f_ncm {
struct gether port;
u8 ctrl_id, data_id;
char ethaddr[14];
struct usb_ep *notify;
struct usb_request *notify_req;
u8 notify_state;
atomic_t notify_count;
bool is_open;
const struct ndp_parser_opts *parser_opts;
bool is_crc;
u32 ndp_sign;
/*
* for notification, it is accessed from both
* callback and ethernet open/close
*/
spinlock_t lock;
struct net_device *netdev;
/* For multi-frame NDP TX */
struct sk_buff *skb_tx_data;
struct sk_buff *skb_tx_ndp;
u16 ndp_dgram_count;
struct hrtimer task_timer;
};
static inline struct f_ncm *func_to_ncm(struct usb_function *f)
{
return container_of(f, struct f_ncm, port.func);
}
/*-------------------------------------------------------------------------*/
/*
* We cannot group frames so use just the minimal size which ok to put
* one max-size ethernet frame.
* If the host can group frames, allow it to do that, 16K is selected,
* because it's used by default by the current linux host driver
*/
#define NTB_DEFAULT_IN_SIZE 16384
#define NTB_OUT_SIZE 16384
/* Allocation for storing the NDP, 32 should suffice for a
* 16k packet. This allows a maximum of 32 * 507 Byte packets to
* be transmitted in a single 16kB skb, though when sending full size
* packets this limit will be plenty.
* Smaller packets are not likely to be trying to maximize the
* throughput and will be mstly sending smaller infrequent frames.
*/
#define TX_MAX_NUM_DPE 32
/* Delay for the transmit to wait before sending an unfilled NTB frame. */
#define TX_TIMEOUT_NSECS 300000
/*
* Although max mtu as dictated by u_ether is 15412 bytes, setting
* max_segment_size to 15426 would not be efficient. If user chooses segment
* size to be (>= 8192), then we can't aggregate more than one buffer in each
* NTB (assuming each packet coming from network layer is >= 8192 bytes) as ep
* maxpacket limit is 16384. So let max_segment_size be limited to 8000 to allow
* at least 2 packets to be aggregated reducing wastage of NTB buffer space
*/
#define MAX_DATAGRAM_SIZE 8000
#define FORMATS_SUPPORTED (USB_CDC_NCM_NTB16_SUPPORTED | \
USB_CDC_NCM_NTB32_SUPPORTED)
static struct usb_cdc_ncm_ntb_parameters ntb_parameters = {
.wLength = cpu_to_le16(sizeof(ntb_parameters)),
.bmNtbFormatsSupported = cpu_to_le16(FORMATS_SUPPORTED),
.dwNtbInMaxSize = cpu_to_le32(NTB_DEFAULT_IN_SIZE),
.wNdpInDivisor = cpu_to_le16(4),
.wNdpInPayloadRemainder = cpu_to_le16(0),
.wNdpInAlignment = cpu_to_le16(4),
.dwNtbOutMaxSize = cpu_to_le32(NTB_OUT_SIZE),
.wNdpOutDivisor = cpu_to_le16(4),
.wNdpOutPayloadRemainder = cpu_to_le16(0),
.wNdpOutAlignment = cpu_to_le16(4),
};
/*
* Use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one
* packet, to simplify cancellation; and a big transfer interval, to
* waste less bandwidth.
Annotation
- Immediate include surface: `linux/cleanup.h`, `linux/kernel.h`, `linux/interrupt.h`, `linux/module.h`, `linux/device.h`, `linux/etherdevice.h`, `linux/crc32.h`, `linux/string_choices.h`.
- Detected declarations: `struct f_ncm`, `struct ndp_parser_opts`, `enum ncm_notify_state`, `function put_ncm`, `function get_ncm`, `function ncm_reset_values`, `function ncm_do_notify`, `function ncm_notify`, `function ncm_notify_complete`, `function ncm_ep0out_complete`.
- Atlas domain: Driver Families / drivers/usb.
- Implementation status: source implementation candidate.
- Synchronization appears in or near this file; preserve lock ordering, sleepability, and interrupt-context constraints.
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.