xref: /OK3568_Linux_fs/kernel/drivers/usb/gadget/function/u_ether.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0+ */
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * u_ether.h -- interface to USB gadget "ethernet link" utilities
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright (C) 2003-2005,2008 David Brownell
6*4882a593Smuzhiyun  * Copyright (C) 2003-2004 Robert Schwebel, Benedikt Spranger
7*4882a593Smuzhiyun  * Copyright (C) 2008 Nokia Corporation
8*4882a593Smuzhiyun  */
9*4882a593Smuzhiyun 
10*4882a593Smuzhiyun #ifndef __U_ETHER_H
11*4882a593Smuzhiyun #define __U_ETHER_H
12*4882a593Smuzhiyun 
13*4882a593Smuzhiyun #include <linux/err.h>
14*4882a593Smuzhiyun #include <linux/if_ether.h>
15*4882a593Smuzhiyun #include <linux/usb/composite.h>
16*4882a593Smuzhiyun #include <linux/usb/cdc.h>
17*4882a593Smuzhiyun #include <linux/netdevice.h>
18*4882a593Smuzhiyun 
19*4882a593Smuzhiyun #define QMULT_DEFAULT 5
20*4882a593Smuzhiyun 
21*4882a593Smuzhiyun /*
22*4882a593Smuzhiyun  * dev_addr: initial value
23*4882a593Smuzhiyun  * changed by "ifconfig usb0 hw ether xx:xx:xx:xx:xx:xx"
24*4882a593Smuzhiyun  * host_addr: this address is invisible to ifconfig
25*4882a593Smuzhiyun  */
26*4882a593Smuzhiyun #define USB_ETHERNET_MODULE_PARAMETERS() \
27*4882a593Smuzhiyun 	static unsigned qmult = QMULT_DEFAULT;				\
28*4882a593Smuzhiyun 	module_param(qmult, uint, S_IRUGO|S_IWUSR);			\
29*4882a593Smuzhiyun 	MODULE_PARM_DESC(qmult, "queue length multiplier at high/super speed");\
30*4882a593Smuzhiyun 									\
31*4882a593Smuzhiyun 	static char *dev_addr;						\
32*4882a593Smuzhiyun 	module_param(dev_addr, charp, S_IRUGO);				\
33*4882a593Smuzhiyun 	MODULE_PARM_DESC(dev_addr, "Device Ethernet Address");		\
34*4882a593Smuzhiyun 									\
35*4882a593Smuzhiyun 	static char *host_addr;						\
36*4882a593Smuzhiyun 	module_param(host_addr, charp, S_IRUGO);			\
37*4882a593Smuzhiyun 	MODULE_PARM_DESC(host_addr, "Host Ethernet Address")
38*4882a593Smuzhiyun 
39*4882a593Smuzhiyun struct eth_dev;
40*4882a593Smuzhiyun 
41*4882a593Smuzhiyun /*
42*4882a593Smuzhiyun  * This represents the USB side of an "ethernet" link, managed by a USB
43*4882a593Smuzhiyun  * function which provides control and (maybe) framing.  Two functions
44*4882a593Smuzhiyun  * in different configurations could share the same ethernet link/netdev,
45*4882a593Smuzhiyun  * using different host interaction models.
46*4882a593Smuzhiyun  *
47*4882a593Smuzhiyun  * There is a current limitation that only one instance of this link may
48*4882a593Smuzhiyun  * be present in any given configuration.  When that's a problem, network
49*4882a593Smuzhiyun  * layer facilities can be used to package multiple logical links on this
50*4882a593Smuzhiyun  * single "physical" one.
51*4882a593Smuzhiyun  */
52*4882a593Smuzhiyun struct gether {
53*4882a593Smuzhiyun 	struct usb_function		func;
54*4882a593Smuzhiyun 
55*4882a593Smuzhiyun 	/* updated by gether_{connect,disconnect} */
56*4882a593Smuzhiyun 	struct eth_dev			*ioport;
57*4882a593Smuzhiyun 
58*4882a593Smuzhiyun 	/* endpoints handle full and/or high speeds */
59*4882a593Smuzhiyun 	struct usb_ep			*in_ep;
60*4882a593Smuzhiyun 	struct usb_ep			*out_ep;
61*4882a593Smuzhiyun 
62*4882a593Smuzhiyun 	bool				is_zlp_ok;
63*4882a593Smuzhiyun 
64*4882a593Smuzhiyun 	u16				cdc_filter;
65*4882a593Smuzhiyun 
66*4882a593Smuzhiyun 	/* hooks for added framing, as needed for RNDIS and EEM. */
67*4882a593Smuzhiyun 	u32				header_len;
68*4882a593Smuzhiyun 	/* NCM requires fixed size bundles */
69*4882a593Smuzhiyun 	bool				is_fixed;
70*4882a593Smuzhiyun 	u32				fixed_out_len;
71*4882a593Smuzhiyun 	u32				fixed_in_len;
72*4882a593Smuzhiyun 	bool				supports_multi_frame;
73*4882a593Smuzhiyun 	struct sk_buff			*(*wrap)(struct gether *port,
74*4882a593Smuzhiyun 						struct sk_buff *skb);
75*4882a593Smuzhiyun 	int				(*unwrap)(struct gether *port,
76*4882a593Smuzhiyun 						struct sk_buff *skb,
77*4882a593Smuzhiyun 						struct sk_buff_head *list);
78*4882a593Smuzhiyun 
79*4882a593Smuzhiyun 	/* called on network open/close */
80*4882a593Smuzhiyun 	void				(*open)(struct gether *);
81*4882a593Smuzhiyun 	void				(*close)(struct gether *);
82*4882a593Smuzhiyun };
83*4882a593Smuzhiyun 
84*4882a593Smuzhiyun #define	DEFAULT_FILTER	(USB_CDC_PACKET_TYPE_BROADCAST \
85*4882a593Smuzhiyun 			|USB_CDC_PACKET_TYPE_ALL_MULTICAST \
86*4882a593Smuzhiyun 			|USB_CDC_PACKET_TYPE_PROMISCUOUS \
87*4882a593Smuzhiyun 			|USB_CDC_PACKET_TYPE_DIRECTED)
88*4882a593Smuzhiyun 
89*4882a593Smuzhiyun /* variant of gether_setup that allows customizing network device name */
90*4882a593Smuzhiyun struct eth_dev *gether_setup_name(struct usb_gadget *g,
91*4882a593Smuzhiyun 		const char *dev_addr, const char *host_addr,
92*4882a593Smuzhiyun 		u8 ethaddr[ETH_ALEN], unsigned qmult, const char *netname);
93*4882a593Smuzhiyun 
94*4882a593Smuzhiyun /* netdev setup/teardown as directed by the gadget driver */
95*4882a593Smuzhiyun /* gether_setup - initialize one ethernet-over-usb link
96*4882a593Smuzhiyun  * @g: gadget to associated with these links
97*4882a593Smuzhiyun  * @ethaddr: NULL, or a buffer in which the ethernet address of the
98*4882a593Smuzhiyun  *	host side of the link is recorded
99*4882a593Smuzhiyun  * Context: may sleep
100*4882a593Smuzhiyun  *
101*4882a593Smuzhiyun  * This sets up the single network link that may be exported by a
102*4882a593Smuzhiyun  * gadget driver using this framework.  The link layer addresses are
103*4882a593Smuzhiyun  * set up using module parameters.
104*4882a593Smuzhiyun  *
105*4882a593Smuzhiyun  * Returns a eth_dev pointer on success, or an ERR_PTR on failure
106*4882a593Smuzhiyun  */
gether_setup(struct usb_gadget * g,const char * dev_addr,const char * host_addr,u8 ethaddr[ETH_ALEN],unsigned qmult)107*4882a593Smuzhiyun static inline struct eth_dev *gether_setup(struct usb_gadget *g,
108*4882a593Smuzhiyun 		const char *dev_addr, const char *host_addr,
109*4882a593Smuzhiyun 		u8 ethaddr[ETH_ALEN], unsigned qmult)
110*4882a593Smuzhiyun {
111*4882a593Smuzhiyun 	return gether_setup_name(g, dev_addr, host_addr, ethaddr, qmult, "usb");
112*4882a593Smuzhiyun }
113*4882a593Smuzhiyun 
114*4882a593Smuzhiyun /*
115*4882a593Smuzhiyun  * variant of gether_setup_default that allows customizing
116*4882a593Smuzhiyun  * network device name
117*4882a593Smuzhiyun  */
118*4882a593Smuzhiyun struct net_device *gether_setup_name_default(const char *netname);
119*4882a593Smuzhiyun 
120*4882a593Smuzhiyun /*
121*4882a593Smuzhiyun  * gether_register_netdev - register the net device
122*4882a593Smuzhiyun  * @net: net device to register
123*4882a593Smuzhiyun  *
124*4882a593Smuzhiyun  * Registers the net device associated with this ethernet-over-usb link
125*4882a593Smuzhiyun  *
126*4882a593Smuzhiyun  */
127*4882a593Smuzhiyun int gether_register_netdev(struct net_device *net);
128*4882a593Smuzhiyun 
129*4882a593Smuzhiyun /* gether_setup_default - initialize one ethernet-over-usb link
130*4882a593Smuzhiyun  * Context: may sleep
131*4882a593Smuzhiyun  *
132*4882a593Smuzhiyun  * This sets up the single network link that may be exported by a
133*4882a593Smuzhiyun  * gadget driver using this framework.  The link layer addresses
134*4882a593Smuzhiyun  * are set to random values.
135*4882a593Smuzhiyun  *
136*4882a593Smuzhiyun  * Returns negative errno, or zero on success
137*4882a593Smuzhiyun  */
gether_setup_default(void)138*4882a593Smuzhiyun static inline struct net_device *gether_setup_default(void)
139*4882a593Smuzhiyun {
140*4882a593Smuzhiyun 	return gether_setup_name_default("usb");
141*4882a593Smuzhiyun }
142*4882a593Smuzhiyun 
143*4882a593Smuzhiyun /**
144*4882a593Smuzhiyun  * gether_set_gadget - initialize one ethernet-over-usb link with a gadget
145*4882a593Smuzhiyun  * @net: device representing this link
146*4882a593Smuzhiyun  * @g: the gadget to initialize with
147*4882a593Smuzhiyun  *
148*4882a593Smuzhiyun  * This associates one ethernet-over-usb link with a gadget.
149*4882a593Smuzhiyun  */
150*4882a593Smuzhiyun void gether_set_gadget(struct net_device *net, struct usb_gadget *g);
151*4882a593Smuzhiyun 
152*4882a593Smuzhiyun /**
153*4882a593Smuzhiyun  * gether_set_dev_addr - initialize an ethernet-over-usb link with eth address
154*4882a593Smuzhiyun  * @net: device representing this link
155*4882a593Smuzhiyun  * @dev_addr: eth address of this device
156*4882a593Smuzhiyun  *
157*4882a593Smuzhiyun  * This sets the device-side Ethernet address of this ethernet-over-usb link
158*4882a593Smuzhiyun  * if dev_addr is correct.
159*4882a593Smuzhiyun  * Returns negative errno if the new address is incorrect.
160*4882a593Smuzhiyun  */
161*4882a593Smuzhiyun int gether_set_dev_addr(struct net_device *net, const char *dev_addr);
162*4882a593Smuzhiyun 
163*4882a593Smuzhiyun /**
164*4882a593Smuzhiyun  * gether_get_dev_addr - get an ethernet-over-usb link eth address
165*4882a593Smuzhiyun  * @net: device representing this link
166*4882a593Smuzhiyun  * @dev_addr: place to store device's eth address
167*4882a593Smuzhiyun  * @len: length of the @dev_addr buffer
168*4882a593Smuzhiyun  *
169*4882a593Smuzhiyun  * This gets the device-side Ethernet address of this ethernet-over-usb link.
170*4882a593Smuzhiyun  * Returns zero on success, else negative errno.
171*4882a593Smuzhiyun  */
172*4882a593Smuzhiyun int gether_get_dev_addr(struct net_device *net, char *dev_addr, int len);
173*4882a593Smuzhiyun 
174*4882a593Smuzhiyun /**
175*4882a593Smuzhiyun  * gether_set_host_addr - initialize an ethernet-over-usb link with host address
176*4882a593Smuzhiyun  * @net: device representing this link
177*4882a593Smuzhiyun  * @host_addr: eth address of the host
178*4882a593Smuzhiyun  *
179*4882a593Smuzhiyun  * This sets the host-side Ethernet address of this ethernet-over-usb link
180*4882a593Smuzhiyun  * if host_addr is correct.
181*4882a593Smuzhiyun  * Returns negative errno if the new address is incorrect.
182*4882a593Smuzhiyun  */
183*4882a593Smuzhiyun int gether_set_host_addr(struct net_device *net, const char *host_addr);
184*4882a593Smuzhiyun 
185*4882a593Smuzhiyun /**
186*4882a593Smuzhiyun  * gether_get_host_addr - get an ethernet-over-usb link host address
187*4882a593Smuzhiyun  * @net: device representing this link
188*4882a593Smuzhiyun  * @host_addr: place to store eth address of the host
189*4882a593Smuzhiyun  * @len: length of the @host_addr buffer
190*4882a593Smuzhiyun  *
191*4882a593Smuzhiyun  * This gets the host-side Ethernet address of this ethernet-over-usb link.
192*4882a593Smuzhiyun  * Returns zero on success, else negative errno.
193*4882a593Smuzhiyun  */
194*4882a593Smuzhiyun int gether_get_host_addr(struct net_device *net, char *host_addr, int len);
195*4882a593Smuzhiyun 
196*4882a593Smuzhiyun /**
197*4882a593Smuzhiyun  * gether_get_host_addr_cdc - get an ethernet-over-usb link host address
198*4882a593Smuzhiyun  * @net: device representing this link
199*4882a593Smuzhiyun  * @host_addr: place to store eth address of the host
200*4882a593Smuzhiyun  * @len: length of the @host_addr buffer
201*4882a593Smuzhiyun  *
202*4882a593Smuzhiyun  * This gets the CDC formatted host-side Ethernet address of this
203*4882a593Smuzhiyun  * ethernet-over-usb link.
204*4882a593Smuzhiyun  * Returns zero on success, else negative errno.
205*4882a593Smuzhiyun  */
206*4882a593Smuzhiyun int gether_get_host_addr_cdc(struct net_device *net, char *host_addr, int len);
207*4882a593Smuzhiyun 
208*4882a593Smuzhiyun /**
209*4882a593Smuzhiyun  * gether_get_host_addr_u8 - get an ethernet-over-usb link host address
210*4882a593Smuzhiyun  * @net: device representing this link
211*4882a593Smuzhiyun  * @host_mac: place to store the eth address of the host
212*4882a593Smuzhiyun  *
213*4882a593Smuzhiyun  * This gets the binary formatted host-side Ethernet address of this
214*4882a593Smuzhiyun  * ethernet-over-usb link.
215*4882a593Smuzhiyun  */
216*4882a593Smuzhiyun void gether_get_host_addr_u8(struct net_device *net, u8 host_mac[ETH_ALEN]);
217*4882a593Smuzhiyun 
218*4882a593Smuzhiyun /**
219*4882a593Smuzhiyun  * gether_set_qmult - initialize an ethernet-over-usb link with a multiplier
220*4882a593Smuzhiyun  * @net: device representing this link
221*4882a593Smuzhiyun  * @qmult: queue multiplier
222*4882a593Smuzhiyun  *
223*4882a593Smuzhiyun  * This sets the queue length multiplier of this ethernet-over-usb link.
224*4882a593Smuzhiyun  * For higher speeds use longer queues.
225*4882a593Smuzhiyun  */
226*4882a593Smuzhiyun void gether_set_qmult(struct net_device *net, unsigned qmult);
227*4882a593Smuzhiyun 
228*4882a593Smuzhiyun /**
229*4882a593Smuzhiyun  * gether_get_qmult - get an ethernet-over-usb link multiplier
230*4882a593Smuzhiyun  * @net: device representing this link
231*4882a593Smuzhiyun  *
232*4882a593Smuzhiyun  * This gets the queue length multiplier of this ethernet-over-usb link.
233*4882a593Smuzhiyun  */
234*4882a593Smuzhiyun unsigned gether_get_qmult(struct net_device *net);
235*4882a593Smuzhiyun 
236*4882a593Smuzhiyun /**
237*4882a593Smuzhiyun  * gether_get_ifname - get an ethernet-over-usb link interface name
238*4882a593Smuzhiyun  * @net: device representing this link
239*4882a593Smuzhiyun  * @name: place to store the interface name
240*4882a593Smuzhiyun  * @len: length of the @name buffer
241*4882a593Smuzhiyun  *
242*4882a593Smuzhiyun  * This gets the interface name of this ethernet-over-usb link.
243*4882a593Smuzhiyun  * Returns zero on success, else negative errno.
244*4882a593Smuzhiyun  */
245*4882a593Smuzhiyun int gether_get_ifname(struct net_device *net, char *name, int len);
246*4882a593Smuzhiyun 
247*4882a593Smuzhiyun /**
248*4882a593Smuzhiyun  * gether_set_ifname - set an ethernet-over-usb link interface name
249*4882a593Smuzhiyun  * @net: device representing this link
250*4882a593Smuzhiyun  * @name: new interface name
251*4882a593Smuzhiyun  * @len: length of @name
252*4882a593Smuzhiyun  *
253*4882a593Smuzhiyun  * This sets the interface name of this ethernet-over-usb link.
254*4882a593Smuzhiyun  * A single terminating newline, if any, is ignored.
255*4882a593Smuzhiyun  * Returns zero on success, else negative errno.
256*4882a593Smuzhiyun  */
257*4882a593Smuzhiyun int gether_set_ifname(struct net_device *net, const char *name, int len);
258*4882a593Smuzhiyun 
259*4882a593Smuzhiyun void gether_cleanup(struct eth_dev *dev);
260*4882a593Smuzhiyun 
261*4882a593Smuzhiyun /* connect/disconnect is handled by individual functions */
262*4882a593Smuzhiyun struct net_device *gether_connect(struct gether *);
263*4882a593Smuzhiyun void gether_disconnect(struct gether *);
264*4882a593Smuzhiyun 
265*4882a593Smuzhiyun /* Some controllers can't support CDC Ethernet (ECM) ... */
can_support_ecm(struct usb_gadget * gadget)266*4882a593Smuzhiyun static inline bool can_support_ecm(struct usb_gadget *gadget)
267*4882a593Smuzhiyun {
268*4882a593Smuzhiyun 	if (!gadget_is_altset_supported(gadget))
269*4882a593Smuzhiyun 		return false;
270*4882a593Smuzhiyun 
271*4882a593Smuzhiyun 	/* Everything else is *presumably* fine ... but this is a bit
272*4882a593Smuzhiyun 	 * chancy, so be **CERTAIN** there are no hardware issues with
273*4882a593Smuzhiyun 	 * your controller.  Add it above if it can't handle CDC.
274*4882a593Smuzhiyun 	 */
275*4882a593Smuzhiyun 	return true;
276*4882a593Smuzhiyun }
277*4882a593Smuzhiyun 
278*4882a593Smuzhiyun #endif /* __U_ETHER_H */
279