blob: 4931c1e69e89ed4365aac771f9cc01bb2c7efee5 [file] [log] [blame]
Ofir Cohen7e942b52012-07-26 13:54:14 +03001/*
2 * u_qc_ether.c -- Ethernet-over-USB link layer utilities for Gadget stack
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 * Copyright (c) 2012, Code Aurora Forum. All rights reserved.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
22/* #define VERBOSE_DEBUG */
23
24#include <linux/kernel.h>
25#include <linux/gfp.h>
26#include <linux/device.h>
27#include <linux/ctype.h>
28#include <linux/etherdevice.h>
29#include <linux/ethtool.h>
30
31#include "u_ether.h"
32
33
34/*
35 * This component encapsulates the Ethernet link glue needed to provide
36 * one (!) network link through the USB gadget stack, normally "usb0".
37 *
38 * The control and data models are handled by the function driver which
39 * connects to this code; such as CDC Ethernet (ECM or EEM),
40 * "CDC Subset", or RNDIS. That includes all descriptor and endpoint
41 * management.
42 *
43 * Link level addressing is handled by this component using module
44 * parameters; if no such parameters are provided, random link level
45 * addresses are used. Each end of the link uses one address. The
46 * host end address is exported in various ways, and is often recorded
47 * in configuration databases.
48 *
49 * The driver which assembles each configuration using such a link is
50 * responsible for ensuring that each configuration includes at most one
51 * instance of is network link. (The network layer provides ways for
52 * this single "physical" link to be used by multiple virtual links.)
53 *
54 * This utilities is based on Ethernet-over-USB link layer utilities and
55 * contains MSM specific implementation.
56 */
57
58#define UETH__VERSION "29-May-2008"
59
60struct eth_qc_dev {
61 /* lock is held while accessing port_usb
62 * or updating its backlink port_usb->ioport
63 */
64 spinlock_t lock;
65 struct qc_gether *port_usb;
66
67 struct net_device *net;
68 struct usb_gadget *gadget;
69
70 unsigned header_len;
71
72 bool zlp;
73 u8 host_mac[ETH_ALEN];
74};
75
76/*-------------------------------------------------------------------------*/
77
78#undef DBG
79#undef VDBG
80#undef ERROR
81#undef INFO
82
83#define xprintk(d, level, fmt, args...) \
84 printk(level "%s: " fmt , (d)->net->name , ## args)
85
86#ifdef DEBUG
87#undef DEBUG
88#define DBG(dev, fmt, args...) \
89 xprintk(dev , KERN_DEBUG , fmt , ## args)
90#else
91#define DBG(dev, fmt, args...) \
92 do { } while (0)
93#endif /* DEBUG */
94
95#ifdef VERBOSE_DEBUG
96#define VDBG DBG
97#else
98#define VDBG(dev, fmt, args...) \
99 do { } while (0)
100#endif /* DEBUG */
101
102#define ERROR(dev, fmt, args...) \
103 xprintk(dev , KERN_ERR , fmt , ## args)
104#define INFO(dev, fmt, args...) \
105 xprintk(dev , KERN_INFO , fmt , ## args)
106
107/*-------------------------------------------------------------------------*/
108
109/* NETWORK DRIVER HOOKUP (to the layer above this driver) */
110static int ueth_qc_change_mtu(struct net_device *net, int new_mtu)
111{
112 struct eth_qc_dev *dev = netdev_priv(net);
113 unsigned long flags;
114 int status = 0;
115
116 /* don't change MTU on "live" link (peer won't know) */
117 spin_lock_irqsave(&dev->lock, flags);
118 if (dev->port_usb)
119 status = -EBUSY;
120 else if (new_mtu <= ETH_HLEN || new_mtu > ETH_FRAME_LEN)
121 status = -ERANGE;
122 else
123 net->mtu = new_mtu;
124 spin_unlock_irqrestore(&dev->lock, flags);
125
126 return status;
127}
128
129static void eth_qc_get_drvinfo(struct net_device *net,
130 struct ethtool_drvinfo *p)
131{
132 struct eth_qc_dev *dev = netdev_priv(net);
133
134 strlcpy(p->driver, "g_qc_ether", sizeof p->driver);
135 strlcpy(p->version, UETH__VERSION, sizeof p->version);
136 strlcpy(p->fw_version, dev->gadget->name, sizeof p->fw_version);
137 strlcpy(p->bus_info, dev_name(&dev->gadget->dev), sizeof p->bus_info);
138}
139
140static const struct ethtool_ops qc_ethtool_ops = {
141 .get_drvinfo = eth_qc_get_drvinfo,
142 .get_link = ethtool_op_get_link,
143};
144
145static netdev_tx_t eth_qc_start_xmit(struct sk_buff *skb,
146 struct net_device *net)
147{
148 return NETDEV_TX_OK;
149}
150
151static int eth_qc_open(struct net_device *net)
152{
153 struct eth_qc_dev *dev = netdev_priv(net);
154 struct qc_gether *link;
155
156 DBG(dev, "%s\n", __func__);
157 if (netif_carrier_ok(dev->net)) {
158 /* Force the netif to send the RTM_NEWLINK event
159 * that in use to notify on the USB cable status.
160 */
161 netif_carrier_off(dev->net);
162 netif_carrier_on(dev->net);
163 netif_wake_queue(dev->net);
164 }
165
166 spin_lock_irq(&dev->lock);
167 link = dev->port_usb;
168 if (link && link->open)
169 link->open(link);
170 spin_unlock_irq(&dev->lock);
171
172 return 0;
173}
174
175static int eth_qc_stop(struct net_device *net)
176{
177 struct eth_qc_dev *dev = netdev_priv(net);
178 unsigned long flags;
179 struct qc_gether *link = dev->port_usb;
180
181 VDBG(dev, "%s\n", __func__);
182 netif_stop_queue(net);
183
184 spin_lock_irqsave(&dev->lock, flags);
185 if (dev->port_usb && link->close)
186 link->close(link);
187 spin_unlock_irqrestore(&dev->lock, flags);
188
189 return 0;
190}
191
192/*-------------------------------------------------------------------------*/
193
194/* initial value, changed by "ifconfig usb0 hw ether xx:xx:xx:xx:xx:xx" */
195static char *qc_dev_addr;
196module_param(qc_dev_addr, charp, S_IRUGO);
197MODULE_PARM_DESC(qc_dev_addr, "QC Device Ethernet Address");
198
199/* this address is invisible to ifconfig */
200static char *qc_host_addr;
201module_param(qc_host_addr, charp, S_IRUGO);
202MODULE_PARM_DESC(qc_host_addr, "QC Host Ethernet Address");
203
204static int get_qc_ether_addr(const char *str, u8 *dev_addr)
205{
206 if (str) {
207 unsigned i;
208
209 for (i = 0; i < 6; i++) {
210 unsigned char num;
211
212 if ((*str == '.') || (*str == ':'))
213 str++;
214 num = hex_to_bin(*str++) << 4;
215 num |= hex_to_bin(*str++);
216 dev_addr[i] = num;
217 }
218 if (is_valid_ether_addr(dev_addr))
219 return 0;
220 }
221 random_ether_addr(dev_addr);
222 return 1;
223}
224
Ofir Cohen7e942b52012-07-26 13:54:14 +0300225static const struct net_device_ops eth_qc_netdev_ops = {
226 .ndo_open = eth_qc_open,
227 .ndo_stop = eth_qc_stop,
228 .ndo_start_xmit = eth_qc_start_xmit,
229 .ndo_change_mtu = ueth_qc_change_mtu,
230 .ndo_set_mac_address = eth_mac_addr,
231 .ndo_validate_addr = eth_validate_addr,
232};
233
234static struct device_type qc_gadget_type = {
235 .name = "gadget",
236};
237
238/**
239 * gether_qc_setup - initialize one ethernet-over-usb link
240 * @g: gadget to associated with these links
241 * @ethaddr: NULL, or a buffer in which the ethernet address of the
242 * host side of the link is recorded
243 * Context: may sleep
244 *
245 * This sets up the single network link that may be exported by a
246 * gadget driver using this framework. The link layer addresses are
247 * set up using module parameters.
248 *
249 * Returns negative errno, or zero on success
250 */
251int gether_qc_setup(struct usb_gadget *g, u8 ethaddr[ETH_ALEN])
252{
253 return gether_qc_setup_name(g, ethaddr, "usb");
254}
255
256/**
257 * gether_qc_setup_name - initialize one ethernet-over-usb link
258 * @g: gadget to associated with these links
259 * @ethaddr: NULL, or a buffer in which the ethernet address of the
260 * host side of the link is recorded
261 * @netname: name for network device (for example, "usb")
262 * Context: may sleep
263 *
264 * This sets up the single network link that may be exported by a
265 * gadget driver using this framework. The link layer addresses are
266 * set up using module parameters.
267 *
268 * Returns negative errno, or zero on success
269 */
270int gether_qc_setup_name(struct usb_gadget *g, u8 ethaddr[ETH_ALEN],
271 const char *netname)
272{
273 struct eth_qc_dev *dev;
274 struct net_device *net;
275 int status;
276
Ofir Cohen7e942b52012-07-26 13:54:14 +0300277 net = alloc_etherdev(sizeof *dev);
278 if (!net)
279 return -ENOMEM;
280
281 dev = netdev_priv(net);
282 spin_lock_init(&dev->lock);
283
284 /* network device setup */
285 dev->net = net;
286 snprintf(net->name, sizeof(net->name), "%s%%d", netname);
287
288 if (get_qc_ether_addr(qc_dev_addr, net->dev_addr))
289 dev_warn(&g->dev,
290 "using random %s ethernet address\n", "self");
291 if (get_qc_ether_addr(qc_host_addr, dev->host_mac))
292 dev_warn(&g->dev,
293 "using random %s ethernet address\n", "host");
294
295 if (ethaddr)
296 memcpy(ethaddr, dev->host_mac, ETH_ALEN);
297
298 net->netdev_ops = &eth_qc_netdev_ops;
299
300 SET_ETHTOOL_OPS(net, &qc_ethtool_ops);
301
302 netif_carrier_off(net);
303
304 dev->gadget = g;
305 SET_NETDEV_DEV(net, &g->dev);
306 SET_NETDEV_DEVTYPE(net, &qc_gadget_type);
307
308 status = register_netdev(net);
309 if (status < 0) {
310 dev_dbg(&g->dev, "register_netdev failed, %d\n", status);
311 free_netdev(net);
312 } else {
313 INFO(dev, "MAC %pM\n", net->dev_addr);
314 INFO(dev, "HOST MAC %pM\n", dev->host_mac);
315
Ofir Cohen7e942b52012-07-26 13:54:14 +0300316 }
317
318 return status;
319}
320
321/**
Amit Blayd6d690a2012-10-16 13:37:42 +0200322 * gether_qc_cleanup_name - remove Ethernet-over-USB device
Ofir Cohen7e942b52012-07-26 13:54:14 +0300323 * Context: may sleep
324 *
325 * This is called to free all resources allocated by @gether_qc_setup().
326 */
Amit Blayd6d690a2012-10-16 13:37:42 +0200327void gether_qc_cleanup_name(const char *netname)
Ofir Cohen7e942b52012-07-26 13:54:14 +0300328{
Amit Blayd6d690a2012-10-16 13:37:42 +0200329 struct net_device *net_dev;
Ofir Cohen7e942b52012-07-26 13:54:14 +0300330
Amit Blayd6d690a2012-10-16 13:37:42 +0200331 /* Extract the eth_qc_dev from the net device */
332 net_dev = dev_get_by_name(&init_net, netname);
Ofir Cohen7e942b52012-07-26 13:54:14 +0300333
Amit Blayd6d690a2012-10-16 13:37:42 +0200334 if (net_dev) {
335 unregister_netdev(net_dev);
336 free_netdev(net_dev);
337 }
Ofir Cohen7e942b52012-07-26 13:54:14 +0300338}
339
Ofir Cohen7e942b52012-07-26 13:54:14 +0300340/**
Amit Blayd6d690a2012-10-16 13:37:42 +0200341 * gether_qc_connect_name - notify network layer that USB link
342 * is active
Ofir Cohen7e942b52012-07-26 13:54:14 +0300343 * @link: the USB link, set up with endpoints, descriptors matching
344 * current device speed, and any framing wrapper(s) set up.
345 * Context: irqs blocked
346 *
347 * This is called to let the network layer know the connection
348 * is active ("carrier detect").
349 */
Amit Blayd6d690a2012-10-16 13:37:42 +0200350struct net_device *gether_qc_connect_name(struct qc_gether *link,
351 const char *netname)
Ofir Cohen7e942b52012-07-26 13:54:14 +0300352{
Amit Blayd6d690a2012-10-16 13:37:42 +0200353 struct net_device *net_dev;
354 struct eth_qc_dev *dev;
355
356 /* Extract the eth_qc_dev from the net device */
357 net_dev = dev_get_by_name(&init_net, netname);
358 dev = netdev_priv(net_dev);
Ofir Cohen7e942b52012-07-26 13:54:14 +0300359
360 if (!dev)
361 return ERR_PTR(-EINVAL);
362
363 dev->zlp = link->is_zlp_ok;
364 dev->header_len = link->header_len;
365
366 spin_lock(&dev->lock);
367 dev->port_usb = link;
368 link->ioport = dev;
369 if (netif_running(dev->net)) {
370 if (link->open)
371 link->open(link);
372 } else {
373 if (link->close)
374 link->close(link);
375 }
376 spin_unlock(&dev->lock);
377
378 netif_carrier_on(dev->net);
379 if (netif_running(dev->net))
380 netif_wake_queue(dev->net);
381
382 return dev->net;
383}
384
385/**
Amit Blayd6d690a2012-10-16 13:37:42 +0200386 * gether_qc_disconnect_name - notify network layer that USB
387 * link is inactive
Ofir Cohen7e942b52012-07-26 13:54:14 +0300388 * @link: the USB link, on which gether_connect() was called
389 * Context: irqs blocked
390 *
391 * This is called to let the network layer know the connection
392 * went inactive ("no carrier").
393 *
394 * On return, the state is as if gether_connect() had never been called.
395 */
Amit Blayd6d690a2012-10-16 13:37:42 +0200396void gether_qc_disconnect_name(struct qc_gether *link, const char *netname)
Ofir Cohen7e942b52012-07-26 13:54:14 +0300397{
Amit Blayd6d690a2012-10-16 13:37:42 +0200398 struct net_device *net_dev;
399 struct eth_qc_dev *dev;
400
401 /* Extract the eth_qc_dev from the net device */
402 net_dev = dev_get_by_name(&init_net, netname);
403 dev = netdev_priv(net_dev);
Ofir Cohen7e942b52012-07-26 13:54:14 +0300404
405 if (!dev)
406 return;
407
408 DBG(dev, "%s\n", __func__);
409
410 netif_stop_queue(dev->net);
411 netif_carrier_off(dev->net);
412
413 spin_lock(&dev->lock);
414 dev->port_usb = NULL;
415 link->ioport = NULL;
416 spin_unlock(&dev->lock);
417}