blob: e10ec25bf932ad7746dcd813fa190713b83fd5fb [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
Amit Blayf9b352b2013-03-04 15:01:40 +02007 * Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
Ofir Cohen7e942b52012-07-26 13:54:14 +03008 *
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;
Amit Blayf9b352b2013-03-04 15:01:40 +020065 struct qc_gether *port_usb;
Ofir Cohen7e942b52012-07-26 13:54:14 +030066
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
Amit Blayf9b352b2013-03-04 15:01:40 +0200238void gether_qc_get_macs(u8 dev_mac[ETH_ALEN], u8 host_mac[ETH_ALEN])
239{
240 if (get_qc_ether_addr(qc_dev_addr, dev_mac))
241 pr_debug("using random dev_mac ethernet address\n");
242 if (get_qc_ether_addr(qc_host_addr, host_mac))
243 pr_debug("using random host_mac ethernet address\n");
244}
245
Ofir Cohen7e942b52012-07-26 13:54:14 +0300246/**
247 * gether_qc_setup - initialize one ethernet-over-usb link
248 * @g: gadget to associated with these links
249 * @ethaddr: NULL, or a buffer in which the ethernet address of the
250 * host side of the link is recorded
251 * Context: may sleep
252 *
253 * This sets up the single network link that may be exported by a
254 * gadget driver using this framework. The link layer addresses are
255 * set up using module parameters.
256 *
257 * Returns negative errno, or zero on success
258 */
259int gether_qc_setup(struct usb_gadget *g, u8 ethaddr[ETH_ALEN])
260{
261 return gether_qc_setup_name(g, ethaddr, "usb");
262}
263
264/**
265 * gether_qc_setup_name - initialize one ethernet-over-usb link
266 * @g: gadget to associated with these links
267 * @ethaddr: NULL, or a buffer in which the ethernet address of the
268 * host side of the link is recorded
269 * @netname: name for network device (for example, "usb")
270 * Context: may sleep
271 *
272 * This sets up the single network link that may be exported by a
273 * gadget driver using this framework. The link layer addresses are
274 * set up using module parameters.
275 *
276 * Returns negative errno, or zero on success
277 */
278int gether_qc_setup_name(struct usb_gadget *g, u8 ethaddr[ETH_ALEN],
279 const char *netname)
280{
281 struct eth_qc_dev *dev;
282 struct net_device *net;
283 int status;
284
Ofir Cohen7e942b52012-07-26 13:54:14 +0300285 net = alloc_etherdev(sizeof *dev);
286 if (!net)
287 return -ENOMEM;
288
289 dev = netdev_priv(net);
290 spin_lock_init(&dev->lock);
291
292 /* network device setup */
293 dev->net = net;
294 snprintf(net->name, sizeof(net->name), "%s%%d", netname);
295
296 if (get_qc_ether_addr(qc_dev_addr, net->dev_addr))
297 dev_warn(&g->dev,
298 "using random %s ethernet address\n", "self");
299 if (get_qc_ether_addr(qc_host_addr, dev->host_mac))
300 dev_warn(&g->dev,
301 "using random %s ethernet address\n", "host");
302
303 if (ethaddr)
304 memcpy(ethaddr, dev->host_mac, ETH_ALEN);
305
306 net->netdev_ops = &eth_qc_netdev_ops;
307
308 SET_ETHTOOL_OPS(net, &qc_ethtool_ops);
309
310 netif_carrier_off(net);
311
312 dev->gadget = g;
313 SET_NETDEV_DEV(net, &g->dev);
314 SET_NETDEV_DEVTYPE(net, &qc_gadget_type);
315
316 status = register_netdev(net);
317 if (status < 0) {
318 dev_dbg(&g->dev, "register_netdev failed, %d\n", status);
319 free_netdev(net);
320 } else {
321 INFO(dev, "MAC %pM\n", net->dev_addr);
322 INFO(dev, "HOST MAC %pM\n", dev->host_mac);
323
Ofir Cohen7e942b52012-07-26 13:54:14 +0300324 }
325
326 return status;
327}
328
329/**
Amit Blayd6d690a2012-10-16 13:37:42 +0200330 * gether_qc_cleanup_name - remove Ethernet-over-USB device
Amit Blayf9b352b2013-03-04 15:01:40 +0200331 * @netname: name for network device (for example, "usb")
Ofir Cohen7e942b52012-07-26 13:54:14 +0300332 * Context: may sleep
333 *
334 * This is called to free all resources allocated by @gether_qc_setup().
335 */
Amit Blayd6d690a2012-10-16 13:37:42 +0200336void gether_qc_cleanup_name(const char *netname)
Ofir Cohen7e942b52012-07-26 13:54:14 +0300337{
Amit Blayd6d690a2012-10-16 13:37:42 +0200338 struct net_device *net_dev;
Ofir Cohen7e942b52012-07-26 13:54:14 +0300339
Amit Blayd6d690a2012-10-16 13:37:42 +0200340 /* Extract the eth_qc_dev from the net device */
341 net_dev = dev_get_by_name(&init_net, netname);
Ofir Cohen7e942b52012-07-26 13:54:14 +0300342
Amit Blayd6d690a2012-10-16 13:37:42 +0200343 if (net_dev) {
Anna Perel2b9e3912012-11-18 10:35:04 +0200344 dev_put(net_dev);
Amit Blayd6d690a2012-10-16 13:37:42 +0200345 unregister_netdev(net_dev);
346 free_netdev(net_dev);
347 }
Ofir Cohen7e942b52012-07-26 13:54:14 +0300348}
349
Ofir Cohen7e942b52012-07-26 13:54:14 +0300350/**
Amit Blayd6d690a2012-10-16 13:37:42 +0200351 * gether_qc_connect_name - notify network layer that USB link
352 * is active
Ofir Cohen7e942b52012-07-26 13:54:14 +0300353 * @link: the USB link, set up with endpoints, descriptors matching
354 * current device speed, and any framing wrapper(s) set up.
Amit Blayf9b352b2013-03-04 15:01:40 +0200355 * @netname: name for network device (for example, "usb")
Ofir Cohen7e942b52012-07-26 13:54:14 +0300356 * Context: irqs blocked
357 *
358 * This is called to let the network layer know the connection
359 * is active ("carrier detect").
360 */
Amit Blayd6d690a2012-10-16 13:37:42 +0200361struct net_device *gether_qc_connect_name(struct qc_gether *link,
362 const char *netname)
Ofir Cohen7e942b52012-07-26 13:54:14 +0300363{
Amit Blayd6d690a2012-10-16 13:37:42 +0200364 struct net_device *net_dev;
365 struct eth_qc_dev *dev;
366
367 /* Extract the eth_qc_dev from the net device */
368 net_dev = dev_get_by_name(&init_net, netname);
Anna Perel2b9e3912012-11-18 10:35:04 +0200369 if (!net_dev)
370 return ERR_PTR(-EINVAL);
371
372 dev_put(net_dev);
Amit Blayd6d690a2012-10-16 13:37:42 +0200373 dev = netdev_priv(net_dev);
Ofir Cohen7e942b52012-07-26 13:54:14 +0300374
375 if (!dev)
376 return ERR_PTR(-EINVAL);
377
378 dev->zlp = link->is_zlp_ok;
379 dev->header_len = link->header_len;
380
381 spin_lock(&dev->lock);
382 dev->port_usb = link;
383 link->ioport = dev;
384 if (netif_running(dev->net)) {
385 if (link->open)
386 link->open(link);
387 } else {
388 if (link->close)
389 link->close(link);
390 }
391 spin_unlock(&dev->lock);
392
393 netif_carrier_on(dev->net);
394 if (netif_running(dev->net))
395 netif_wake_queue(dev->net);
396
397 return dev->net;
398}
399
400/**
Amit Blayd6d690a2012-10-16 13:37:42 +0200401 * gether_qc_disconnect_name - notify network layer that USB
402 * link is inactive
Ofir Cohen7e942b52012-07-26 13:54:14 +0300403 * @link: the USB link, on which gether_connect() was called
Amit Blayf9b352b2013-03-04 15:01:40 +0200404 * @netname: name for network device (for example, "usb")
Ofir Cohen7e942b52012-07-26 13:54:14 +0300405 * Context: irqs blocked
406 *
407 * This is called to let the network layer know the connection
408 * went inactive ("no carrier").
409 *
410 * On return, the state is as if gether_connect() had never been called.
411 */
Amit Blayd6d690a2012-10-16 13:37:42 +0200412void gether_qc_disconnect_name(struct qc_gether *link, const char *netname)
Ofir Cohen7e942b52012-07-26 13:54:14 +0300413{
Amit Blayd6d690a2012-10-16 13:37:42 +0200414 struct net_device *net_dev;
415 struct eth_qc_dev *dev;
416
417 /* Extract the eth_qc_dev from the net device */
418 net_dev = dev_get_by_name(&init_net, netname);
Anna Perel2b9e3912012-11-18 10:35:04 +0200419 if (!net_dev)
420 return;
421
422 dev_put(net_dev);
Amit Blayd6d690a2012-10-16 13:37:42 +0200423 dev = netdev_priv(net_dev);
Ofir Cohen7e942b52012-07-26 13:54:14 +0300424
425 if (!dev)
426 return;
427
428 DBG(dev, "%s\n", __func__);
429
430 netif_stop_queue(dev->net);
431 netif_carrier_off(dev->net);
432
433 spin_lock(&dev->lock);
434 dev->port_usb = NULL;
435 link->ioport = NULL;
436 spin_unlock(&dev->lock);
437}