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