blob: d74b8f7214a43c2daf54b37c9cc0cc8ece6b8f29 [file] [log] [blame]
David Brownell2b3d9422008-06-19 18:19:28 -07001/*
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.
David Brownell2b3d9422008-06-19 18:19:28 -070012 */
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
David Brownellda741b82008-06-19 18:19:46 -070022#include "gadget_chips.h"
23
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +020024#define QMULT_DEFAULT 5
25
26/*
27 * dev_addr: initial value
28 * changed by "ifconfig usb0 hw ether xx:xx:xx:xx:xx:xx"
29 * host_addr: this address is invisible to ifconfig
30 */
31#define USB_ETHERNET_MODULE_PARAMETERS() \
32 static unsigned qmult = QMULT_DEFAULT; \
33 module_param(qmult, uint, S_IRUGO|S_IWUSR); \
34 MODULE_PARM_DESC(qmult, "queue length multiplier at high/super speed");\
35 \
36 static char *dev_addr; \
37 module_param(dev_addr, charp, S_IRUGO); \
38 MODULE_PARM_DESC(dev_addr, "Device Ethernet Address"); \
39 \
40 static char *host_addr; \
41 module_param(host_addr, charp, S_IRUGO); \
42 MODULE_PARM_DESC(host_addr, "Host Ethernet Address")
43
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +010044struct eth_dev;
David Brownellda741b82008-06-19 18:19:46 -070045
David Brownell2b3d9422008-06-19 18:19:28 -070046/*
47 * This represents the USB side of an "ethernet" link, managed by a USB
48 * function which provides control and (maybe) framing. Two functions
David Brownell45fe3b82008-06-19 18:20:04 -070049 * in different configurations could share the same ethernet link/netdev,
50 * using different host interaction models.
David Brownell2b3d9422008-06-19 18:19:28 -070051 *
David Brownell45fe3b82008-06-19 18:20:04 -070052 * There is a current limitation that only one instance of this link may
53 * be present in any given configuration. When that's a problem, network
54 * layer facilities can be used to package multiple logical links on this
55 * single "physical" one.
David Brownell2b3d9422008-06-19 18:19:28 -070056 */
57struct gether {
58 struct usb_function func;
59
60 /* updated by gether_{connect,disconnect} */
61 struct eth_dev *ioport;
62
63 /* endpoints handle full and/or high speeds */
64 struct usb_ep *in_ep;
65 struct usb_ep *out_ep;
66
David Brownell2b3d9422008-06-19 18:19:28 -070067 bool is_zlp_ok;
68
69 u16 cdc_filter;
70
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -050071 /* hooks for added framing, as needed for RNDIS and EEM. */
David Brownell2b3d9422008-06-19 18:19:28 -070072 u32 header_len;
Yauheni Kaliuta5c1168d2010-12-08 13:12:04 +020073 /* NCM requires fixed size bundles */
74 bool is_fixed;
75 u32 fixed_out_len;
76 u32 fixed_in_len;
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -050077 struct sk_buff *(*wrap)(struct gether *port,
78 struct sk_buff *skb);
79 int (*unwrap)(struct gether *port,
80 struct sk_buff *skb,
81 struct sk_buff_head *list);
David Brownell2b3d9422008-06-19 18:19:28 -070082
83 /* called on network open/close */
84 void (*open)(struct gether *);
85 void (*close)(struct gether *);
86};
87
88#define DEFAULT_FILTER (USB_CDC_PACKET_TYPE_BROADCAST \
89 |USB_CDC_PACKET_TYPE_ALL_MULTICAST \
90 |USB_CDC_PACKET_TYPE_PROMISCUOUS \
91 |USB_CDC_PACKET_TYPE_DIRECTED)
92
Mike Lockwood036e98b2012-05-10 10:08:02 +020093/* variant of gether_setup that allows customizing network device name */
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +020094struct eth_dev *gether_setup_name(struct usb_gadget *g,
95 const char *dev_addr, const char *host_addr,
96 u8 ethaddr[ETH_ALEN], unsigned qmult, const char *netname);
David Brownell2b3d9422008-06-19 18:19:28 -070097
98/* netdev setup/teardown as directed by the gadget driver */
Mike Lockwood036e98b2012-05-10 10:08:02 +020099/* gether_setup - initialize one ethernet-over-usb link
100 * @g: gadget to associated with these links
101 * @ethaddr: NULL, or a buffer in which the ethernet address of the
102 * host side of the link is recorded
103 * Context: may sleep
104 *
105 * This sets up the single network link that may be exported by a
106 * gadget driver using this framework. The link layer addresses are
107 * set up using module parameters.
108 *
109 * Returns negative errno, or zero on success
110 */
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +0100111static inline struct eth_dev *gether_setup(struct usb_gadget *g,
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +0200112 const char *dev_addr, const char *host_addr,
113 u8 ethaddr[ETH_ALEN], unsigned qmult)
Mike Lockwood036e98b2012-05-10 10:08:02 +0200114{
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +0200115 return gether_setup_name(g, dev_addr, host_addr, ethaddr, qmult, "usb");
Mike Lockwood036e98b2012-05-10 10:08:02 +0200116}
117
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +0200118/*
119 * variant of gether_setup_default that allows customizing
120 * network device name
121 */
122struct net_device *gether_setup_name_default(const char *netname);
123
124/*
125 * gether_register_netdev - register the net device
126 * @net: net device to register
127 *
128 * Registers the net device associated with this ethernet-over-usb link
129 *
130 */
131int gether_register_netdev(struct net_device *net);
132
133/* gether_setup_default - initialize one ethernet-over-usb link
134 * Context: may sleep
135 *
136 * This sets up the single network link that may be exported by a
137 * gadget driver using this framework. The link layer addresses
138 * are set to random values.
139 *
140 * Returns negative errno, or zero on success
141 */
142static inline struct net_device *gether_setup_default(void)
143{
144 return gether_setup_name_default("usb");
145}
146
147/**
148 * gether_set_gadget - initialize one ethernet-over-usb link with a gadget
149 * @net: device representing this link
150 * @g: the gadget to initialize with
151 *
152 * This associates one ethernet-over-usb link with a gadget.
153 */
154void gether_set_gadget(struct net_device *net, struct usb_gadget *g);
155
156/**
157 * gether_set_dev_addr - initialize an ethernet-over-usb link with eth address
158 * @net: device representing this link
159 * @dev_addr: eth address of this device
160 *
161 * This sets the device-side Ethernet address of this ethernet-over-usb link
162 * if dev_addr is correct.
163 * Returns negative errno if the new address is incorrect.
164 */
165int gether_set_dev_addr(struct net_device *net, const char *dev_addr);
166
167/**
168 * gether_get_dev_addr - get an ethernet-over-usb link eth address
169 * @net: device representing this link
170 * @dev_addr: place to store device's eth address
171 * @len: length of the @dev_addr buffer
172 *
173 * This gets the device-side Ethernet address of this ethernet-over-usb link.
174 * Returns zero on success, else negative errno.
175 */
176int gether_get_dev_addr(struct net_device *net, char *dev_addr, int len);
177
178/**
179 * gether_set_host_addr - initialize an ethernet-over-usb link with host address
180 * @net: device representing this link
181 * @host_addr: eth address of the host
182 *
183 * This sets the host-side Ethernet address of this ethernet-over-usb link
184 * if host_addr is correct.
185 * Returns negative errno if the new address is incorrect.
186 */
187int gether_set_host_addr(struct net_device *net, const char *host_addr);
188
189/**
190 * gether_get_host_addr - get an ethernet-over-usb link host address
191 * @net: device representing this link
192 * @host_addr: place to store eth address of the host
193 * @len: length of the @host_addr buffer
194 *
195 * This gets the host-side Ethernet address of this ethernet-over-usb link.
196 * Returns zero on success, else negative errno.
197 */
198int gether_get_host_addr(struct net_device *net, char *host_addr, int len);
199
200/**
201 * gether_get_host_addr_cdc - get an ethernet-over-usb link host address
202 * @net: device representing this link
203 * @host_addr: place to store eth address of the host
204 * @len: length of the @host_addr buffer
205 *
206 * This gets the CDC formatted host-side Ethernet address of this
207 * ethernet-over-usb link.
208 * Returns zero on success, else negative errno.
209 */
210int gether_get_host_addr_cdc(struct net_device *net, char *host_addr, int len);
211
212/**
213 * gether_set_qmult - initialize an ethernet-over-usb link with a multiplier
214 * @net: device representing this link
215 * @qmult: queue multiplier
216 *
217 * This sets the queue length multiplier of this ethernet-over-usb link.
218 * For higher speeds use longer queues.
219 */
220void gether_set_qmult(struct net_device *net, unsigned qmult);
221
222/**
223 * gether_get_qmult - get an ethernet-over-usb link multiplier
224 * @net: device representing this link
225 *
226 * This gets the queue length multiplier of this ethernet-over-usb link.
227 */
228unsigned gether_get_qmult(struct net_device *net);
229
230/**
231 * gether_get_ifname - get an ethernet-over-usb link interface name
232 * @net: device representing this link
233 * @name: place to store the interface name
234 * @len: length of the @name buffer
235 *
236 * This gets the interface name of this ethernet-over-usb link.
237 * Returns zero on success, else negative errno.
238 */
239int gether_get_ifname(struct net_device *net, char *name, int len);
240
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +0100241void gether_cleanup(struct eth_dev *dev);
David Brownell2b3d9422008-06-19 18:19:28 -0700242
243/* connect/disconnect is handled by individual functions */
244struct net_device *gether_connect(struct gether *);
245void gether_disconnect(struct gether *);
246
David Brownellda741b82008-06-19 18:19:46 -0700247/* Some controllers can't support CDC Ethernet (ECM) ... */
248static inline bool can_support_ecm(struct usb_gadget *gadget)
249{
250 if (!gadget_supports_altsettings(gadget))
251 return false;
252
David Brownellda741b82008-06-19 18:19:46 -0700253 /* Everything else is *presumably* fine ... but this is a bit
254 * chancy, so be **CERTAIN** there are no hardware issues with
255 * your controller. Add it above if it can't handle CDC.
256 */
257 return true;
258}
259
David Brownell8a408192008-06-19 18:19:32 -0700260/* each configuration may bind one instance of an ethernet link */
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +0100261int geth_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN],
262 struct eth_dev *dev);
263int ecm_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN],
264 struct eth_dev *dev);
265int ncm_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN],
266 struct eth_dev *dev);
267int eem_bind_config(struct usb_configuration *c, struct eth_dev *dev);
David Brownell8a408192008-06-19 18:19:32 -0700268
Michal Nazarewicz396cda92009-11-30 10:55:40 +0100269#ifdef USB_ETH_RNDIS
David Brownell45fe3b82008-06-19 18:20:04 -0700270
Benoit Goby1fbfeff2012-05-10 10:08:04 +0200271int rndis_bind_config_vendor(struct usb_configuration *c, u8 ethaddr[ETH_ALEN],
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +0100272 u32 vendorID, const char *manufacturer, struct eth_dev *dev);
David Brownell45fe3b82008-06-19 18:20:04 -0700273
274#else
275
276static inline int
Benoit Goby1fbfeff2012-05-10 10:08:04 +0200277rndis_bind_config_vendor(struct usb_configuration *c, u8 ethaddr[ETH_ALEN],
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +0100278 u32 vendorID, const char *manufacturer, struct eth_dev *dev)
David Brownell45fe3b82008-06-19 18:20:04 -0700279{
280 return 0;
281}
282
283#endif
284
Benoit Goby1fbfeff2012-05-10 10:08:04 +0200285/**
286 * rndis_bind_config - add RNDIS network link to a configuration
287 * @c: the configuration to support the network link
288 * @ethaddr: a buffer in which the ethernet address of the host side
289 * side of the link was recorded
290 * Context: single threaded during gadget setup
291 *
292 * Returns zero on success, else negative errno.
293 *
294 * Caller must have called @gether_setup(). Caller is also responsible
295 * for calling @gether_cleanup() before module unload.
296 */
297static inline int rndis_bind_config(struct usb_configuration *c,
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +0100298 u8 ethaddr[ETH_ALEN], struct eth_dev *dev)
Benoit Goby1fbfeff2012-05-10 10:08:04 +0200299{
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +0100300 return rndis_bind_config_vendor(c, ethaddr, 0, NULL, dev);
Benoit Goby1fbfeff2012-05-10 10:08:04 +0200301}
302
303
David Brownell2b3d9422008-06-19 18:19:28 -0700304#endif /* __U_ETHER_H */