blob: 13a434515cd3a8c33518e0013942d3803c836399 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * u_ether.h -- interface to USB gadget "ethernet link" utilities
4 *
5 * Copyright (C) 2003-2005,2008 David Brownell
6 * Copyright (C) 2003-2004 Robert Schwebel, Benedikt Spranger
7 * Copyright (C) 2008 Nokia Corporation
8 */
9
10#ifndef __U_ETHER_H
11#define __U_ETHER_H
12
13#include <linux/err.h>
14#include <linux/if_ether.h>
15#include <linux/usb/composite.h>
16#include <linux/usb/cdc.h>
17
18#include "gadget_chips.h"
19#ifdef CONFIG_ASR_TOE
20#include <linux/toe.h>
21#endif
22
23struct eth_dev;
24struct aggr_ctx;
25
26#ifdef CONFIG_CPU_ASR1903
27#define CONFIG_USBNET_USE_SG 1
28#endif
29
30#define USBNET_SG_NENTS (32)
31
32/*
33 * This represents the USB side of an "ethernet" link, managed by a USB
34 * function which provides control and (maybe) framing. Two functions
35 * in different configurations could share the same ethernet link/netdev,
36 * using different host interaction models.
37 *
38 * There is a current limitation that only one instance of this link may
39 * be present in any given configuration. When that's a problem, network
40 * layer facilities can be used to package multiple logical links on this
41 * single "physical" one.
42 */
43struct gether {
44 struct usb_function func;
45
46 /* updated by gether_{connect,disconnect} */
47 struct eth_dev *ioport;
48
49 /* endpoints handle full and/or high speeds */
50 struct usb_ep *in_ep;
51 struct usb_ep *out_ep;
52
53 bool is_zlp_ok;
54 bool is_sg_mode;
55 u16 cdc_filter;
56
57 /* hooks for added framing, as needed for RNDIS and EEM. */
58 u32 header_len;
59 /* NCM requires fixed size bundles */
60 bool is_fixed;
61 u32 fixed_out_len;
62 u32 fixed_in_len;
63
64 unsigned ul_max_pkts_per_xfer;
65 struct sk_buff *(*wrap)(struct gether *port,
66 struct sk_buff *skb,
67 struct aggr_ctx *aggr_ctx);
68 int (*unwrap)(struct gether *port,
69 struct sk_buff *skb,
70 struct sk_buff_head *list);
71 struct sk_buff *(*unwrap_fixup)(struct gether *,
72 struct sk_buff *skb);
73
74 /* called on network open/close */
75 void (*open)(struct gether *);
76 void (*close)(struct gether *);
77
78#ifdef CONFIG_ASR_TOE
79 enum uether_type ueth_type;
80#endif
81};
82
83struct aggr_ctx {
84#ifdef CONFIG_ASR_TOE
85 struct list_head toe_list;
86#endif
87 struct sk_buff_head skb_list;
88 struct sk_buff *pending_skb;
89 unsigned total_size;
90
91#ifdef CONFIG_USBNET_USE_SG
92 struct scatterlist *sg;
93 u32 num_sgs;
94#endif
95
96#ifdef CONFIG_ASR_TOE
97 bool is_toe;
98#endif
99};
100
101#ifdef CONFIG_ASR_TOE
102#define MAX_RX_REQ_NUMBER (32)
103struct uether_rx_aggr{
104 int nr_rx_req;
105 struct usb_request *rx_req_array[MAX_RX_REQ_NUMBER];
106};
107
108#define CONFIG_ASR_TOE_USE_SMALL_MEMORY 1
109#endif
110
111
112
113#define AGGR_MAX_PADDING (256)
114#define AGGRCTX(x) ((struct aggr_ctx*)((x)->context))
115
116#define DEFAULT_FILTER (USB_CDC_PACKET_TYPE_BROADCAST \
117 |USB_CDC_PACKET_TYPE_ALL_MULTICAST \
118 |USB_CDC_PACKET_TYPE_PROMISCUOUS \
119 |USB_CDC_PACKET_TYPE_DIRECTED)
120
121/* variant of gether_setup that allows customizing network device name */
122struct eth_dev *gether_setup_name(struct usb_gadget *g, u8 ethaddr[ETH_ALEN],
123 const char *netname);
124#ifdef CONFIG_USB_MV_HSIC_UDC
125struct eth_dev *gether_setup_name_hsic(struct usb_gadget *g, u8 ethaddr[ETH_ALEN],
126 const char *netname);
127#endif
128/* netdev setup/teardown as directed by the gadget driver */
129/* gether_setup - initialize one ethernet-over-usb link
130 * @g: gadget to associated with these links
131 * @ethaddr: NULL, or a buffer in which the ethernet address of the
132 * host side of the link is recorded
133 * Context: may sleep
134 *
135 * This sets up the single network link that may be exported by a
136 * gadget driver using this framework. The link layer addresses are
137 * set up using module parameters.
138 *
139 * Returns negative errno, or zero on success
140 */
141static inline struct eth_dev *gether_setup(struct usb_gadget *g,
142 u8 ethaddr[ETH_ALEN])
143{
144 return gether_setup_name(g, ethaddr, "usb");
145}
146
147void gether_cleanup(struct eth_dev *dev);
148
149/* connect/disconnect is handled by individual functions */
150struct net_device *gether_connect(struct gether *);
151void gether_disconnect(struct gether *);
152
153/* Some controllers can't support CDC Ethernet (ECM) ... */
154static inline bool can_support_ecm(struct usb_gadget *gadget)
155{
156 if (!gadget_supports_altsettings(gadget))
157 return false;
158
159 /* Everything else is *presumably* fine ... but this is a bit
160 * chancy, so be **CERTAIN** there are no hardware issues with
161 * your controller. Add it above if it can't handle CDC.
162 */
163 return true;
164}
165
166/* each configuration may bind one instance of an ethernet link */
167int geth_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN],
168 struct eth_dev *dev);
169int ecm_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN],
170 struct eth_dev *dev);
171int ncm_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN],
172 struct eth_dev *dev);
173int eem_bind_config(struct usb_configuration *c, struct eth_dev *dev);
174
175#ifdef USB_ETH_RNDIS
176
177int rndis_bind_config_vendor(struct usb_configuration *c, u8 ethaddr[ETH_ALEN],
178 u32 vendorID, const char *manufacturer, struct eth_dev *dev);
179
180#ifdef CONFIG_USB_MV_HSIC_UDC
181int rndis_bind_config_vendor_hsic(struct usb_configuration *c, u8 ethaddr[ETH_ALEN],
182 u32 vendorID, const char *manufacturer, struct eth_dev *dev);
183#endif
184#else
185
186static inline int
187rndis_bind_config_vendor(struct usb_configuration *c, u8 ethaddr[ETH_ALEN],
188 u32 vendorID, const char *manufacturer, struct eth_dev *dev)
189{
190 return 0;
191}
192
193#endif
194
195/**
196 * rndis_bind_config - add RNDIS network link to a configuration
197 * @c: the configuration to support the network link
198 * @ethaddr: a buffer in which the ethernet address of the host side
199 * side of the link was recorded
200 * Context: single threaded during gadget setup
201 *
202 * Returns zero on success, else negative errno.
203 *
204 * Caller must have called @gether_setup(). Caller is also responsible
205 * for calling @gether_cleanup() before module unload.
206 */
207static inline int rndis_bind_config(struct usb_configuration *c,
208 u8 ethaddr[ETH_ALEN], struct eth_dev *dev)
209{
210 return rndis_bind_config_vendor(c, ethaddr, 0, NULL, dev);
211}
212
213#endif /* __U_ETHER_H */