blob: 334b389479165b4bf3391ee39e086f18a777ca9e [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>
Jim Baxter6d3865f2014-07-07 18:33:18 +010021#include <linux/netdevice.h>
David Brownell2b3d9422008-06-19 18:19:28 -070022
David Brownellda741b82008-06-19 18:19:46 -070023#include "gadget_chips.h"
24
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +020025#define QMULT_DEFAULT 5
26
27/*
28 * dev_addr: initial value
29 * changed by "ifconfig usb0 hw ether xx:xx:xx:xx:xx:xx"
30 * host_addr: this address is invisible to ifconfig
31 */
32#define USB_ETHERNET_MODULE_PARAMETERS() \
33 static unsigned qmult = QMULT_DEFAULT; \
34 module_param(qmult, uint, S_IRUGO|S_IWUSR); \
35 MODULE_PARM_DESC(qmult, "queue length multiplier at high/super speed");\
36 \
37 static char *dev_addr; \
38 module_param(dev_addr, charp, S_IRUGO); \
39 MODULE_PARM_DESC(dev_addr, "Device Ethernet Address"); \
40 \
41 static char *host_addr; \
42 module_param(host_addr, charp, S_IRUGO); \
43 MODULE_PARM_DESC(host_addr, "Host Ethernet Address")
44
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +010045struct eth_dev;
David Brownellda741b82008-06-19 18:19:46 -070046
David Brownell2b3d9422008-06-19 18:19:28 -070047/*
48 * This represents the USB side of an "ethernet" link, managed by a USB
49 * function which provides control and (maybe) framing. Two functions
David Brownell45fe3b82008-06-19 18:20:04 -070050 * in different configurations could share the same ethernet link/netdev,
51 * using different host interaction models.
David Brownell2b3d9422008-06-19 18:19:28 -070052 *
David Brownell45fe3b82008-06-19 18:20:04 -070053 * There is a current limitation that only one instance of this link may
54 * be present in any given configuration. When that's a problem, network
55 * layer facilities can be used to package multiple logical links on this
56 * single "physical" one.
David Brownell2b3d9422008-06-19 18:19:28 -070057 */
58struct gether {
59 struct usb_function func;
60
61 /* updated by gether_{connect,disconnect} */
62 struct eth_dev *ioport;
63
64 /* endpoints handle full and/or high speeds */
65 struct usb_ep *in_ep;
66 struct usb_ep *out_ep;
67
David Brownell2b3d9422008-06-19 18:19:28 -070068 bool is_zlp_ok;
69
70 u16 cdc_filter;
71
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -050072 /* hooks for added framing, as needed for RNDIS and EEM. */
David Brownell2b3d9422008-06-19 18:19:28 -070073 u32 header_len;
Yauheni Kaliuta5c1168d2010-12-08 13:12:04 +020074 /* NCM requires fixed size bundles */
75 bool is_fixed;
76 u32 fixed_out_len;
77 u32 fixed_in_len;
Jim Baxter6d3865f2014-07-07 18:33:18 +010078 bool supports_multi_frame;
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -050079 struct sk_buff *(*wrap)(struct gether *port,
80 struct sk_buff *skb);
81 int (*unwrap)(struct gether *port,
82 struct sk_buff *skb,
83 struct sk_buff_head *list);
David Brownell2b3d9422008-06-19 18:19:28 -070084
85 /* called on network open/close */
86 void (*open)(struct gether *);
87 void (*close)(struct gether *);
88};
89
90#define DEFAULT_FILTER (USB_CDC_PACKET_TYPE_BROADCAST \
91 |USB_CDC_PACKET_TYPE_ALL_MULTICAST \
92 |USB_CDC_PACKET_TYPE_PROMISCUOUS \
93 |USB_CDC_PACKET_TYPE_DIRECTED)
94
Mike Lockwood036e98b2012-05-10 10:08:02 +020095/* variant of gether_setup that allows customizing network device name */
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +020096struct eth_dev *gether_setup_name(struct usb_gadget *g,
97 const char *dev_addr, const char *host_addr,
98 u8 ethaddr[ETH_ALEN], unsigned qmult, const char *netname);
David Brownell2b3d9422008-06-19 18:19:28 -070099
100/* netdev setup/teardown as directed by the gadget driver */
Mike Lockwood036e98b2012-05-10 10:08:02 +0200101/* gether_setup - initialize one ethernet-over-usb link
102 * @g: gadget to associated with these links
103 * @ethaddr: NULL, or a buffer in which the ethernet address of the
104 * host side of the link is recorded
105 * Context: may sleep
106 *
107 * This sets up the single network link that may be exported by a
108 * gadget driver using this framework. The link layer addresses are
109 * set up using module parameters.
110 *
Dan Carpenter574f24f2013-11-14 11:42:11 +0300111 * Returns a eth_dev pointer on success, or an ERR_PTR on failure
Mike Lockwood036e98b2012-05-10 10:08:02 +0200112 */
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +0100113static inline struct eth_dev *gether_setup(struct usb_gadget *g,
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +0200114 const char *dev_addr, const char *host_addr,
115 u8 ethaddr[ETH_ALEN], unsigned qmult)
Mike Lockwood036e98b2012-05-10 10:08:02 +0200116{
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +0200117 return gether_setup_name(g, dev_addr, host_addr, ethaddr, qmult, "usb");
Mike Lockwood036e98b2012-05-10 10:08:02 +0200118}
119
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +0200120/*
121 * variant of gether_setup_default that allows customizing
122 * network device name
123 */
124struct net_device *gether_setup_name_default(const char *netname);
125
126/*
127 * gether_register_netdev - register the net device
128 * @net: net device to register
129 *
130 * Registers the net device associated with this ethernet-over-usb link
131 *
132 */
133int gether_register_netdev(struct net_device *net);
134
135/* gether_setup_default - initialize one ethernet-over-usb link
136 * Context: may sleep
137 *
138 * This sets up the single network link that may be exported by a
139 * gadget driver using this framework. The link layer addresses
140 * are set to random values.
141 *
142 * Returns negative errno, or zero on success
143 */
144static inline struct net_device *gether_setup_default(void)
145{
146 return gether_setup_name_default("usb");
147}
148
149/**
150 * gether_set_gadget - initialize one ethernet-over-usb link with a gadget
151 * @net: device representing this link
152 * @g: the gadget to initialize with
153 *
154 * This associates one ethernet-over-usb link with a gadget.
155 */
156void gether_set_gadget(struct net_device *net, struct usb_gadget *g);
157
158/**
159 * gether_set_dev_addr - initialize an ethernet-over-usb link with eth address
160 * @net: device representing this link
161 * @dev_addr: eth address of this device
162 *
163 * This sets the device-side Ethernet address of this ethernet-over-usb link
164 * if dev_addr is correct.
165 * Returns negative errno if the new address is incorrect.
166 */
167int gether_set_dev_addr(struct net_device *net, const char *dev_addr);
168
169/**
170 * gether_get_dev_addr - get an ethernet-over-usb link eth address
171 * @net: device representing this link
172 * @dev_addr: place to store device's eth address
173 * @len: length of the @dev_addr buffer
174 *
175 * This gets the device-side Ethernet address of this ethernet-over-usb link.
176 * Returns zero on success, else negative errno.
177 */
178int gether_get_dev_addr(struct net_device *net, char *dev_addr, int len);
179
180/**
181 * gether_set_host_addr - initialize an ethernet-over-usb link with host address
182 * @net: device representing this link
183 * @host_addr: eth address of the host
184 *
185 * This sets the host-side Ethernet address of this ethernet-over-usb link
186 * if host_addr is correct.
187 * Returns negative errno if the new address is incorrect.
188 */
189int gether_set_host_addr(struct net_device *net, const char *host_addr);
190
191/**
192 * gether_get_host_addr - get an ethernet-over-usb link host address
193 * @net: device representing this link
194 * @host_addr: place to store eth address of the host
195 * @len: length of the @host_addr buffer
196 *
197 * This gets the host-side Ethernet address of this ethernet-over-usb link.
198 * Returns zero on success, else negative errno.
199 */
200int gether_get_host_addr(struct net_device *net, char *host_addr, int len);
201
202/**
203 * gether_get_host_addr_cdc - get an ethernet-over-usb link host address
204 * @net: device representing this link
205 * @host_addr: place to store eth address of the host
206 * @len: length of the @host_addr buffer
207 *
208 * This gets the CDC formatted host-side Ethernet address of this
209 * ethernet-over-usb link.
210 * Returns zero on success, else negative errno.
211 */
212int gether_get_host_addr_cdc(struct net_device *net, char *host_addr, int len);
213
214/**
Andrzej Pietrasiewiczbf4277c2013-05-28 09:15:45 +0200215 * gether_get_host_addr_u8 - get an ethernet-over-usb link host address
216 * @net: device representing this link
217 * @host_mac: place to store the eth address of the host
218 *
219 * This gets the binary formatted host-side Ethernet address of this
220 * ethernet-over-usb link.
221 */
222void gether_get_host_addr_u8(struct net_device *net, u8 host_mac[ETH_ALEN]);
223
224/**
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +0200225 * gether_set_qmult - initialize an ethernet-over-usb link with a multiplier
226 * @net: device representing this link
227 * @qmult: queue multiplier
228 *
229 * This sets the queue length multiplier of this ethernet-over-usb link.
230 * For higher speeds use longer queues.
231 */
232void gether_set_qmult(struct net_device *net, unsigned qmult);
233
234/**
235 * gether_get_qmult - get an ethernet-over-usb link multiplier
236 * @net: device representing this link
237 *
238 * This gets the queue length multiplier of this ethernet-over-usb link.
239 */
240unsigned gether_get_qmult(struct net_device *net);
241
242/**
243 * gether_get_ifname - get an ethernet-over-usb link interface name
244 * @net: device representing this link
245 * @name: place to store the interface name
246 * @len: length of the @name buffer
247 *
248 * This gets the interface name of this ethernet-over-usb link.
249 * Returns zero on success, else negative errno.
250 */
251int gether_get_ifname(struct net_device *net, char *name, int len);
252
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +0100253void gether_cleanup(struct eth_dev *dev);
David Brownell2b3d9422008-06-19 18:19:28 -0700254
255/* connect/disconnect is handled by individual functions */
256struct net_device *gether_connect(struct gether *);
257void gether_disconnect(struct gether *);
258
David Brownellda741b82008-06-19 18:19:46 -0700259/* Some controllers can't support CDC Ethernet (ECM) ... */
260static inline bool can_support_ecm(struct usb_gadget *gadget)
261{
262 if (!gadget_supports_altsettings(gadget))
263 return false;
264
David Brownellda741b82008-06-19 18:19:46 -0700265 /* Everything else is *presumably* fine ... but this is a bit
266 * chancy, so be **CERTAIN** there are no hardware issues with
267 * your controller. Add it above if it can't handle CDC.
268 */
269 return true;
270}
271
David Brownell2b3d9422008-06-19 18:19:28 -0700272#endif /* __U_ETHER_H */