blob: 1e187e5b5261adbcdffcc30670438e9df7ed59dc [file] [log] [blame]
Remy Bohmer23cd1382009-07-29 18:18:43 +02001/*
2 * ether.c -- Ethernet gadget driver, with CDC and non-CDC options
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 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22
23#include <common.h>
24#include <asm/errno.h>
Vitaly Kuzmichevc85d70e2011-02-11 18:18:32 +030025#include <linux/netdevice.h>
Remy Bohmer23cd1382009-07-29 18:18:43 +020026#include <linux/usb/ch9.h>
Lukasz Majewskib9300532012-05-02 13:11:34 +020027#include <usbdescriptors.h>
Remy Bohmer23cd1382009-07-29 18:18:43 +020028#include <linux/usb/cdc.h>
29#include <linux/usb/gadget.h>
30#include <net.h>
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +030031#include <malloc.h>
Remy Bohmer23cd1382009-07-29 18:18:43 +020032#include <linux/ctype.h>
33
34#include "gadget_chips.h"
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +030035#include "rndis.h"
Remy Bohmer23cd1382009-07-29 18:18:43 +020036
Vitaly Kuzmichev8f7aa832010-12-28 16:59:31 +030037#define USB_NET_NAME "usb_ether"
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +040038
Remy Bohmer23cd1382009-07-29 18:18:43 +020039#define atomic_read
40extern struct platform_data brd;
41#define spin_lock(x)
42#define spin_unlock(x)
43
44
45unsigned packet_received, packet_sent;
46
Remy Bohmer23cd1382009-07-29 18:18:43 +020047#define GFP_ATOMIC ((gfp_t) 0)
48#define GFP_KERNEL ((gfp_t) 0)
49
50/*
51 * Ethernet gadget driver -- with CDC and non-CDC options
52 * Builds on hardware support for a full duplex link.
53 *
54 * CDC Ethernet is the standard USB solution for sending Ethernet frames
55 * using USB. Real hardware tends to use the same framing protocol but look
56 * different for control features. This driver strongly prefers to use
57 * this USB-IF standard as its open-systems interoperability solution;
58 * most host side USB stacks (except from Microsoft) support it.
59 *
60 * This is sometimes called "CDC ECM" (Ethernet Control Model) to support
61 * TLA-soup. "CDC ACM" (Abstract Control Model) is for modems, and a new
62 * "CDC EEM" (Ethernet Emulation Model) is starting to spread.
63 *
64 * There's some hardware that can't talk CDC ECM. We make that hardware
65 * implement a "minimalist" vendor-agnostic CDC core: same framing, but
66 * link-level setup only requires activating the configuration. Only the
67 * endpoint descriptors, and product/vendor IDs, are relevant; no control
68 * operations are available. Linux supports it, but other host operating
69 * systems may not. (This is a subset of CDC Ethernet.)
70 *
71 * It turns out that if you add a few descriptors to that "CDC Subset",
72 * (Windows) host side drivers from MCCI can treat it as one submode of
73 * a proprietary scheme called "SAFE" ... without needing to know about
74 * specific product/vendor IDs. So we do that, making it easier to use
75 * those MS-Windows drivers. Those added descriptors make it resemble a
76 * CDC MDLM device, but they don't change device behavior at all. (See
77 * MCCI Engineering report 950198 "SAFE Networking Functions".)
78 *
79 * A third option is also in use. Rather than CDC Ethernet, or something
80 * simpler, Microsoft pushes their own approach: RNDIS. The published
81 * RNDIS specs are ambiguous and appear to be incomplete, and are also
82 * needlessly complex. They borrow more from CDC ACM than CDC ECM.
83 */
84#define ETH_ALEN 6 /* Octets in one ethernet addr */
85#define ETH_HLEN 14 /* Total octets in header. */
86#define ETH_ZLEN 60 /* Min. octets in frame sans FCS */
87#define ETH_DATA_LEN 1500 /* Max. octets in payload */
88#define ETH_FRAME_LEN PKTSIZE_ALIGN /* Max. octets in frame sans FCS */
89#define ETH_FCS_LEN 4 /* Octets in the FCS */
90
91#define DRIVER_DESC "Ethernet Gadget"
92/* Based on linux 2.6.27 version */
93#define DRIVER_VERSION "May Day 2005"
94
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +040095static const char shortname[] = "ether";
96static const char driver_desc[] = DRIVER_DESC;
Remy Bohmer23cd1382009-07-29 18:18:43 +020097
98#define RX_EXTRA 20 /* guard against rx overflows */
99
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300100#ifndef CONFIG_USB_ETH_RNDIS
101#define rndis_uninit(x) do {} while (0)
102#define rndis_deregister(c) do {} while (0)
103#define rndis_exit() do {} while (0)
104#endif
105
106/* CDC and RNDIS support the same host-chosen outgoing packet filters. */
Remy Bohmer23cd1382009-07-29 18:18:43 +0200107#define DEFAULT_FILTER (USB_CDC_PACKET_TYPE_BROADCAST \
108 |USB_CDC_PACKET_TYPE_ALL_MULTICAST \
109 |USB_CDC_PACKET_TYPE_PROMISCUOUS \
110 |USB_CDC_PACKET_TYPE_DIRECTED)
111
112#define USB_CONNECT_TIMEOUT (3 * CONFIG_SYS_HZ)
113
114/*-------------------------------------------------------------------------*/
Vitaly Kuzmichev8b6b66b2011-02-11 18:18:33 +0300115
116struct eth_dev {
117 struct usb_gadget *gadget;
118 struct usb_request *req; /* for control responses */
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300119 struct usb_request *stat_req; /* for cdc & rndis status */
Vitaly Kuzmichev8b6b66b2011-02-11 18:18:33 +0300120
121 u8 config;
122 struct usb_ep *in_ep, *out_ep, *status_ep;
123 const struct usb_endpoint_descriptor
124 *in, *out, *status;
125
126 struct usb_request *tx_req, *rx_req;
127
128 struct eth_device *net;
129 struct net_device_stats stats;
130 unsigned int tx_qlen;
131
132 unsigned zlp:1;
133 unsigned cdc:1;
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300134 unsigned rndis:1;
Vitaly Kuzmichev8b6b66b2011-02-11 18:18:33 +0300135 unsigned suspended:1;
136 unsigned network_started:1;
137 u16 cdc_filter;
138 unsigned long todo;
139 int mtu;
140#define WORK_RX_MEMORY 0
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300141 int rndis_config;
Vitaly Kuzmichev8b6b66b2011-02-11 18:18:33 +0300142 u8 host_mac[ETH_ALEN];
143};
144
145/*
146 * This version autoconfigures as much as possible at run-time.
147 *
148 * It also ASSUMES a self-powered device, without remote wakeup,
149 * although remote wakeup support would make sense.
150 */
151
152/*-------------------------------------------------------------------------*/
Remy Bohmer23cd1382009-07-29 18:18:43 +0200153static struct eth_dev l_ethdev;
154static struct eth_device l_netdev;
155static struct usb_gadget_driver eth_driver;
156
157/*-------------------------------------------------------------------------*/
158
159/* "main" config is either CDC, or its simple subset */
160static inline int is_cdc(struct eth_dev *dev)
161{
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200162#if !defined(CONFIG_USB_ETH_SUBSET)
Remy Bohmer23cd1382009-07-29 18:18:43 +0200163 return 1; /* only cdc possible */
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200164#elif !defined(CONFIG_USB_ETH_CDC)
Remy Bohmer23cd1382009-07-29 18:18:43 +0200165 return 0; /* only subset possible */
166#else
167 return dev->cdc; /* depends on what hardware we found */
168#endif
169}
170
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300171/* "secondary" RNDIS config may sometimes be activated */
172static inline int rndis_active(struct eth_dev *dev)
173{
174#ifdef CONFIG_USB_ETH_RNDIS
175 return dev->rndis;
176#else
177 return 0;
178#endif
179}
180
181#define subset_active(dev) (!is_cdc(dev) && !rndis_active(dev))
182#define cdc_active(dev) (is_cdc(dev) && !rndis_active(dev))
Remy Bohmer23cd1382009-07-29 18:18:43 +0200183
184#define DEFAULT_QLEN 2 /* double buffering by default */
185
186/* peak bulk transfer bits-per-second */
187#define HS_BPS (13 * 512 * 8 * 1000 * 8)
188#define FS_BPS (19 * 64 * 1 * 1000 * 8)
189
190#ifdef CONFIG_USB_GADGET_DUALSPEED
191#define DEVSPEED USB_SPEED_HIGH
192
Vitaly Kuzmichev2721dbf2010-08-12 16:44:40 +0400193#ifdef CONFIG_USB_ETH_QMULT
194#define qmult CONFIG_USB_ETH_QMULT
195#else
196#define qmult 5
197#endif
198
Remy Bohmer23cd1382009-07-29 18:18:43 +0200199/* for dual-speed hardware, use deeper queues at highspeed */
200#define qlen(gadget) \
201 (DEFAULT_QLEN*((gadget->speed == USB_SPEED_HIGH) ? qmult : 1))
202
203static inline int BITRATE(struct usb_gadget *g)
204{
205 return (g->speed == USB_SPEED_HIGH) ? HS_BPS : FS_BPS;
206}
207
208#else /* full speed (low speed doesn't do bulk) */
209
210#define qmult 1
211
212#define DEVSPEED USB_SPEED_FULL
213
214#define qlen(gadget) DEFAULT_QLEN
215
216static inline int BITRATE(struct usb_gadget *g)
217{
218 return FS_BPS;
219}
220#endif
221
Remy Bohmer23cd1382009-07-29 18:18:43 +0200222/*-------------------------------------------------------------------------*/
223
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400224/*
225 * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
Remy Bohmer23cd1382009-07-29 18:18:43 +0200226 * Instead: allocate your own, using normal USB-IF procedures.
227 */
228
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400229/*
230 * Thanks to NetChip Technologies for donating this product ID.
Remy Bohmer23cd1382009-07-29 18:18:43 +0200231 * It's for devices with only CDC Ethernet configurations.
232 */
233#define CDC_VENDOR_NUM 0x0525 /* NetChip */
234#define CDC_PRODUCT_NUM 0xa4a1 /* Linux-USB Ethernet Gadget */
235
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400236/*
237 * For hardware that can't talk CDC, we use the same vendor ID that
Remy Bohmer23cd1382009-07-29 18:18:43 +0200238 * ARM Linux has used for ethernet-over-usb, both with sa1100 and
239 * with pxa250. We're protocol-compatible, if the host-side drivers
240 * use the endpoint descriptors. bcdDevice (version) is nonzero, so
241 * drivers that need to hard-wire endpoint numbers have a hook.
242 *
243 * The protocol is a minimal subset of CDC Ether, which works on any bulk
244 * hardware that's not deeply broken ... even on hardware that can't talk
245 * RNDIS (like SA-1100, with no interrupt endpoint, or anything that
246 * doesn't handle control-OUT).
247 */
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300248#define SIMPLE_VENDOR_NUM 0x049f /* Compaq Computer Corp. */
249#define SIMPLE_PRODUCT_NUM 0x505a /* Linux-USB "CDC Subset" Device */
250
251/*
252 * For hardware that can talk RNDIS and either of the above protocols,
253 * use this ID ... the windows INF files will know it. Unless it's
254 * used with CDC Ethernet, Linux 2.4 hosts will need updates to choose
255 * the non-RNDIS configuration.
256 */
257#define RNDIS_VENDOR_NUM 0x0525 /* NetChip */
258#define RNDIS_PRODUCT_NUM 0xa4a2 /* Ethernet/RNDIS Gadget */
Remy Bohmer23cd1382009-07-29 18:18:43 +0200259
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400260/*
261 * Some systems will want different product identifers published in the
Remy Bohmer23cd1382009-07-29 18:18:43 +0200262 * device descriptor, either numbers or strings or both. These string
263 * parameters are in UTF-8 (superset of ASCII's 7 bit characters).
264 */
265
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300266/*
267 * Emulating them in eth_bind:
268 * static ushort idVendor;
269 * static ushort idProduct;
270 */
271
Remy Bohmer23cd1382009-07-29 18:18:43 +0200272#if defined(CONFIG_USBNET_MANUFACTURER)
273static char *iManufacturer = CONFIG_USBNET_MANUFACTURER;
274#else
275static char *iManufacturer = "U-boot";
276#endif
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300277
278/* These probably need to be configurable. */
279static ushort bcdDevice;
Remy Bohmer23cd1382009-07-29 18:18:43 +0200280static char *iProduct;
281static char *iSerialNumber;
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300282
Remy Bohmer23cd1382009-07-29 18:18:43 +0200283static char dev_addr[18];
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300284
Remy Bohmer23cd1382009-07-29 18:18:43 +0200285static char host_addr[18];
286
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300287
Remy Bohmer23cd1382009-07-29 18:18:43 +0200288/*-------------------------------------------------------------------------*/
289
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400290/*
291 * USB DRIVER HOOKUP (to the hardware driver, below us), mostly
Remy Bohmer23cd1382009-07-29 18:18:43 +0200292 * ep0 implementation: descriptors, config management, setup().
293 * also optional class-specific notification interrupt transfer.
294 */
295
296/*
297 * DESCRIPTORS ... most are static, but strings and (full) configuration
298 * descriptors are built on demand. For now we do either full CDC, or
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300299 * our simple subset, with RNDIS as an optional second configuration.
300 *
301 * RNDIS includes some CDC ACM descriptors ... like CDC Ethernet. But
302 * the class descriptors match a modem (they're ignored; it's really just
303 * Ethernet functionality), they don't need the NOP altsetting, and the
304 * status transfer endpoint isn't optional.
Remy Bohmer23cd1382009-07-29 18:18:43 +0200305 */
306
307#define STRING_MANUFACTURER 1
308#define STRING_PRODUCT 2
309#define STRING_ETHADDR 3
310#define STRING_DATA 4
311#define STRING_CONTROL 5
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300312#define STRING_RNDIS_CONTROL 6
Remy Bohmer23cd1382009-07-29 18:18:43 +0200313#define STRING_CDC 7
314#define STRING_SUBSET 8
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300315#define STRING_RNDIS 9
Remy Bohmer23cd1382009-07-29 18:18:43 +0200316#define STRING_SERIALNUMBER 10
317
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300318/* holds our biggest descriptor (or RNDIS response) */
Remy Bohmer23cd1382009-07-29 18:18:43 +0200319#define USB_BUFSIZ 256
320
321/*
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300322 * This device advertises one configuration, eth_config, unless RNDIS
323 * is enabled (rndis_config) on hardware supporting at least two configs.
324 *
325 * NOTE: Controllers like superh_udc should probably be able to use
326 * an RNDIS-only configuration.
Remy Bohmer23cd1382009-07-29 18:18:43 +0200327 *
328 * FIXME define some higher-powered configurations to make it easier
329 * to recharge batteries ...
330 */
331
332#define DEV_CONFIG_VALUE 1 /* cdc or subset */
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300333#define DEV_RNDIS_CONFIG_VALUE 2 /* rndis; optional */
Remy Bohmer23cd1382009-07-29 18:18:43 +0200334
335static struct usb_device_descriptor
336device_desc = {
337 .bLength = sizeof device_desc,
338 .bDescriptorType = USB_DT_DEVICE,
339
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400340 .bcdUSB = __constant_cpu_to_le16(0x0200),
Remy Bohmer23cd1382009-07-29 18:18:43 +0200341
342 .bDeviceClass = USB_CLASS_COMM,
343 .bDeviceSubClass = 0,
344 .bDeviceProtocol = 0,
345
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400346 .idVendor = __constant_cpu_to_le16(CDC_VENDOR_NUM),
347 .idProduct = __constant_cpu_to_le16(CDC_PRODUCT_NUM),
Remy Bohmer23cd1382009-07-29 18:18:43 +0200348 .iManufacturer = STRING_MANUFACTURER,
349 .iProduct = STRING_PRODUCT,
350 .bNumConfigurations = 1,
351};
352
353static struct usb_otg_descriptor
354otg_descriptor = {
355 .bLength = sizeof otg_descriptor,
356 .bDescriptorType = USB_DT_OTG,
357
358 .bmAttributes = USB_OTG_SRP,
359};
360
361static struct usb_config_descriptor
362eth_config = {
363 .bLength = sizeof eth_config,
364 .bDescriptorType = USB_DT_CONFIG,
365
366 /* compute wTotalLength on the fly */
367 .bNumInterfaces = 2,
368 .bConfigurationValue = DEV_CONFIG_VALUE,
369 .iConfiguration = STRING_CDC,
370 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
371 .bMaxPower = 1,
372};
373
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300374#ifdef CONFIG_USB_ETH_RNDIS
375static struct usb_config_descriptor
376rndis_config = {
377 .bLength = sizeof rndis_config,
378 .bDescriptorType = USB_DT_CONFIG,
379
380 /* compute wTotalLength on the fly */
381 .bNumInterfaces = 2,
382 .bConfigurationValue = DEV_RNDIS_CONFIG_VALUE,
383 .iConfiguration = STRING_RNDIS,
384 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
385 .bMaxPower = 1,
386};
387#endif
388
Remy Bohmer23cd1382009-07-29 18:18:43 +0200389/*
390 * Compared to the simple CDC subset, the full CDC Ethernet model adds
391 * three class descriptors, two interface descriptors, optional status
392 * endpoint. Both have a "data" interface and two bulk endpoints.
393 * There are also differences in how control requests are handled.
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300394 *
395 * RNDIS shares a lot with CDC-Ethernet, since it's a variant of the
396 * CDC-ACM (modem) spec. Unfortunately MSFT's RNDIS driver is buggy; it
397 * may hang or oops. Since bugfixes (or accurate specs, letting Linux
398 * work around those bugs) are unlikely to ever come from MSFT, you may
399 * wish to avoid using RNDIS.
400 *
401 * MCCI offers an alternative to RNDIS if you need to connect to Windows
402 * but have hardware that can't support CDC Ethernet. We add descriptors
403 * to present the CDC Subset as a (nonconformant) CDC MDLM variant called
404 * "SAFE". That borrows from both CDC Ethernet and CDC MDLM. You can
405 * get those drivers from MCCI, or bundled with various products.
Remy Bohmer23cd1382009-07-29 18:18:43 +0200406 */
407
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200408#ifdef CONFIG_USB_ETH_CDC
Remy Bohmer23cd1382009-07-29 18:18:43 +0200409static struct usb_interface_descriptor
410control_intf = {
411 .bLength = sizeof control_intf,
412 .bDescriptorType = USB_DT_INTERFACE,
413
414 .bInterfaceNumber = 0,
415 /* status endpoint is optional; this may be patched later */
416 .bNumEndpoints = 1,
417 .bInterfaceClass = USB_CLASS_COMM,
418 .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET,
419 .bInterfaceProtocol = USB_CDC_PROTO_NONE,
420 .iInterface = STRING_CONTROL,
421};
422#endif
423
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300424#ifdef CONFIG_USB_ETH_RNDIS
425static const struct usb_interface_descriptor
426rndis_control_intf = {
427 .bLength = sizeof rndis_control_intf,
428 .bDescriptorType = USB_DT_INTERFACE,
429
430 .bInterfaceNumber = 0,
431 .bNumEndpoints = 1,
432 .bInterfaceClass = USB_CLASS_COMM,
433 .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
434 .bInterfaceProtocol = USB_CDC_ACM_PROTO_VENDOR,
435 .iInterface = STRING_RNDIS_CONTROL,
436};
437#endif
438
Remy Bohmer23cd1382009-07-29 18:18:43 +0200439static const struct usb_cdc_header_desc header_desc = {
440 .bLength = sizeof header_desc,
441 .bDescriptorType = USB_DT_CS_INTERFACE,
442 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
443
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400444 .bcdCDC = __constant_cpu_to_le16(0x0110),
Remy Bohmer23cd1382009-07-29 18:18:43 +0200445};
446
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200447#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Remy Bohmer23cd1382009-07-29 18:18:43 +0200448
449static const struct usb_cdc_union_desc union_desc = {
450 .bLength = sizeof union_desc,
451 .bDescriptorType = USB_DT_CS_INTERFACE,
452 .bDescriptorSubType = USB_CDC_UNION_TYPE,
453
454 .bMasterInterface0 = 0, /* index of control interface */
455 .bSlaveInterface0 = 1, /* index of DATA interface */
456};
457
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300458#endif /* CDC || RNDIS */
459
460#ifdef CONFIG_USB_ETH_RNDIS
461
462static const struct usb_cdc_call_mgmt_descriptor call_mgmt_descriptor = {
463 .bLength = sizeof call_mgmt_descriptor,
464 .bDescriptorType = USB_DT_CS_INTERFACE,
465 .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE,
466
467 .bmCapabilities = 0x00,
468 .bDataInterface = 0x01,
469};
470
471static const struct usb_cdc_acm_descriptor acm_descriptor = {
472 .bLength = sizeof acm_descriptor,
473 .bDescriptorType = USB_DT_CS_INTERFACE,
474 .bDescriptorSubType = USB_CDC_ACM_TYPE,
475
476 .bmCapabilities = 0x00,
477};
478
479#endif
Remy Bohmer23cd1382009-07-29 18:18:43 +0200480
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200481#ifndef CONFIG_USB_ETH_CDC
Remy Bohmer23cd1382009-07-29 18:18:43 +0200482
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400483/*
484 * "SAFE" loosely follows CDC WMC MDLM, violating the spec in various
Remy Bohmer23cd1382009-07-29 18:18:43 +0200485 * ways: data endpoints live in the control interface, there's no data
486 * interface, and it's not used to talk to a cell phone radio.
487 */
488
489static const struct usb_cdc_mdlm_desc mdlm_desc = {
490 .bLength = sizeof mdlm_desc,
491 .bDescriptorType = USB_DT_CS_INTERFACE,
492 .bDescriptorSubType = USB_CDC_MDLM_TYPE,
493
494 .bcdVersion = __constant_cpu_to_le16(0x0100),
495 .bGUID = {
496 0x5d, 0x34, 0xcf, 0x66, 0x11, 0x18, 0x11, 0xd6,
497 0xa2, 0x1a, 0x00, 0x01, 0x02, 0xca, 0x9a, 0x7f,
498 },
499};
500
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400501/*
502 * since "usb_cdc_mdlm_detail_desc" is a variable length structure, we
Remy Bohmer23cd1382009-07-29 18:18:43 +0200503 * can't really use its struct. All we do here is say that we're using
504 * the submode of "SAFE" which directly matches the CDC Subset.
505 */
506static const u8 mdlm_detail_desc[] = {
507 6,
508 USB_DT_CS_INTERFACE,
509 USB_CDC_MDLM_DETAIL_TYPE,
510
511 0, /* "SAFE" */
512 0, /* network control capabilities (none) */
513 0, /* network data capabilities ("raw" encapsulation) */
514};
515
516#endif
517
Remy Bohmer23cd1382009-07-29 18:18:43 +0200518static const struct usb_cdc_ether_desc ether_desc = {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400519 .bLength = sizeof(ether_desc),
Remy Bohmer23cd1382009-07-29 18:18:43 +0200520 .bDescriptorType = USB_DT_CS_INTERFACE,
521 .bDescriptorSubType = USB_CDC_ETHERNET_TYPE,
522
523 /* this descriptor actually adds value, surprise! */
524 .iMACAddress = STRING_ETHADDR,
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400525 .bmEthernetStatistics = __constant_cpu_to_le32(0), /* no statistics */
526 .wMaxSegmentSize = __constant_cpu_to_le16(ETH_FRAME_LEN),
527 .wNumberMCFilters = __constant_cpu_to_le16(0),
Remy Bohmer23cd1382009-07-29 18:18:43 +0200528 .bNumberPowerFilters = 0,
529};
530
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200531#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Remy Bohmer23cd1382009-07-29 18:18:43 +0200532
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400533/*
534 * include the status endpoint if we can, even where it's optional.
Remy Bohmer23cd1382009-07-29 18:18:43 +0200535 * use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one
536 * packet, to simplify cancellation; and a big transfer interval, to
537 * waste less bandwidth.
538 *
539 * some drivers (like Linux 2.4 cdc-ether!) "need" it to exist even
540 * if they ignore the connect/disconnect notifications that real aether
541 * can provide. more advanced cdc configurations might want to support
542 * encapsulated commands (vendor-specific, using control-OUT).
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300543 *
544 * RNDIS requires the status endpoint, since it uses that encapsulation
545 * mechanism for its funky RPC scheme.
Remy Bohmer23cd1382009-07-29 18:18:43 +0200546 */
547
548#define LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */
549#define STATUS_BYTECOUNT 16 /* 8 byte header + data */
550
551static struct usb_endpoint_descriptor
552fs_status_desc = {
553 .bLength = USB_DT_ENDPOINT_SIZE,
554 .bDescriptorType = USB_DT_ENDPOINT,
555
556 .bEndpointAddress = USB_DIR_IN,
557 .bmAttributes = USB_ENDPOINT_XFER_INT,
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400558 .wMaxPacketSize = __constant_cpu_to_le16(STATUS_BYTECOUNT),
Remy Bohmer23cd1382009-07-29 18:18:43 +0200559 .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC,
560};
561#endif
562
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200563#ifdef CONFIG_USB_ETH_CDC
Remy Bohmer23cd1382009-07-29 18:18:43 +0200564
565/* the default data interface has no endpoints ... */
566
567static const struct usb_interface_descriptor
568data_nop_intf = {
569 .bLength = sizeof data_nop_intf,
570 .bDescriptorType = USB_DT_INTERFACE,
571
572 .bInterfaceNumber = 1,
573 .bAlternateSetting = 0,
574 .bNumEndpoints = 0,
575 .bInterfaceClass = USB_CLASS_CDC_DATA,
576 .bInterfaceSubClass = 0,
577 .bInterfaceProtocol = 0,
578};
579
580/* ... but the "real" data interface has two bulk endpoints */
581
582static const struct usb_interface_descriptor
583data_intf = {
584 .bLength = sizeof data_intf,
585 .bDescriptorType = USB_DT_INTERFACE,
586
587 .bInterfaceNumber = 1,
588 .bAlternateSetting = 1,
589 .bNumEndpoints = 2,
590 .bInterfaceClass = USB_CLASS_CDC_DATA,
591 .bInterfaceSubClass = 0,
592 .bInterfaceProtocol = 0,
593 .iInterface = STRING_DATA,
594};
595
596#endif
597
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300598#ifdef CONFIG_USB_ETH_RNDIS
599
600/* RNDIS doesn't activate by changing to the "real" altsetting */
601
602static const struct usb_interface_descriptor
603rndis_data_intf = {
604 .bLength = sizeof rndis_data_intf,
605 .bDescriptorType = USB_DT_INTERFACE,
606
607 .bInterfaceNumber = 1,
608 .bAlternateSetting = 0,
609 .bNumEndpoints = 2,
610 .bInterfaceClass = USB_CLASS_CDC_DATA,
611 .bInterfaceSubClass = 0,
612 .bInterfaceProtocol = 0,
613 .iInterface = STRING_DATA,
614};
615
616#endif
617
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200618#ifdef CONFIG_USB_ETH_SUBSET
Remy Bohmer23cd1382009-07-29 18:18:43 +0200619
620/*
621 * "Simple" CDC-subset option is a simple vendor-neutral model that most
622 * full speed controllers can handle: one interface, two bulk endpoints.
623 *
624 * To assist host side drivers, we fancy it up a bit, and add descriptors
625 * so some host side drivers will understand it as a "SAFE" variant.
626 */
627
628static const struct usb_interface_descriptor
629subset_data_intf = {
630 .bLength = sizeof subset_data_intf,
631 .bDescriptorType = USB_DT_INTERFACE,
632
633 .bInterfaceNumber = 0,
634 .bAlternateSetting = 0,
635 .bNumEndpoints = 2,
636 .bInterfaceClass = USB_CLASS_COMM,
637 .bInterfaceSubClass = USB_CDC_SUBCLASS_MDLM,
638 .bInterfaceProtocol = 0,
639 .iInterface = STRING_DATA,
640};
641
642#endif /* SUBSET */
643
Remy Bohmer23cd1382009-07-29 18:18:43 +0200644static struct usb_endpoint_descriptor
645fs_source_desc = {
646 .bLength = USB_DT_ENDPOINT_SIZE,
647 .bDescriptorType = USB_DT_ENDPOINT,
648
649 .bEndpointAddress = USB_DIR_IN,
650 .bmAttributes = USB_ENDPOINT_XFER_BULK,
651};
652
653static struct usb_endpoint_descriptor
654fs_sink_desc = {
655 .bLength = USB_DT_ENDPOINT_SIZE,
656 .bDescriptorType = USB_DT_ENDPOINT,
657
658 .bEndpointAddress = USB_DIR_OUT,
659 .bmAttributes = USB_ENDPOINT_XFER_BULK,
660};
661
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400662static const struct usb_descriptor_header *fs_eth_function[11] = {
Remy Bohmer23cd1382009-07-29 18:18:43 +0200663 (struct usb_descriptor_header *) &otg_descriptor,
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200664#ifdef CONFIG_USB_ETH_CDC
Remy Bohmer23cd1382009-07-29 18:18:43 +0200665 /* "cdc" mode descriptors */
666 (struct usb_descriptor_header *) &control_intf,
667 (struct usb_descriptor_header *) &header_desc,
668 (struct usb_descriptor_header *) &union_desc,
669 (struct usb_descriptor_header *) &ether_desc,
670 /* NOTE: status endpoint may need to be removed */
671 (struct usb_descriptor_header *) &fs_status_desc,
672 /* data interface, with altsetting */
673 (struct usb_descriptor_header *) &data_nop_intf,
674 (struct usb_descriptor_header *) &data_intf,
675 (struct usb_descriptor_header *) &fs_source_desc,
676 (struct usb_descriptor_header *) &fs_sink_desc,
677 NULL,
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200678#endif /* CONFIG_USB_ETH_CDC */
Remy Bohmer23cd1382009-07-29 18:18:43 +0200679};
680
681static inline void fs_subset_descriptors(void)
682{
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200683#ifdef CONFIG_USB_ETH_SUBSET
Remy Bohmer23cd1382009-07-29 18:18:43 +0200684 /* behavior is "CDC Subset"; extra descriptors say "SAFE" */
685 fs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
686 fs_eth_function[2] = (struct usb_descriptor_header *) &header_desc;
687 fs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc;
688 fs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc;
689 fs_eth_function[5] = (struct usb_descriptor_header *) &ether_desc;
690 fs_eth_function[6] = (struct usb_descriptor_header *) &fs_source_desc;
691 fs_eth_function[7] = (struct usb_descriptor_header *) &fs_sink_desc;
692 fs_eth_function[8] = NULL;
693#else
694 fs_eth_function[1] = NULL;
695#endif
696}
697
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300698#ifdef CONFIG_USB_ETH_RNDIS
699static const struct usb_descriptor_header *fs_rndis_function[] = {
700 (struct usb_descriptor_header *) &otg_descriptor,
701 /* control interface matches ACM, not Ethernet */
702 (struct usb_descriptor_header *) &rndis_control_intf,
703 (struct usb_descriptor_header *) &header_desc,
704 (struct usb_descriptor_header *) &call_mgmt_descriptor,
705 (struct usb_descriptor_header *) &acm_descriptor,
706 (struct usb_descriptor_header *) &union_desc,
707 (struct usb_descriptor_header *) &fs_status_desc,
708 /* data interface has no altsetting */
709 (struct usb_descriptor_header *) &rndis_data_intf,
710 (struct usb_descriptor_header *) &fs_source_desc,
711 (struct usb_descriptor_header *) &fs_sink_desc,
712 NULL,
713};
714#endif
715
Remy Bohmer23cd1382009-07-29 18:18:43 +0200716/*
717 * usb 2.0 devices need to expose both high speed and full speed
718 * descriptors, unless they only run at full speed.
719 */
720
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200721#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Remy Bohmer23cd1382009-07-29 18:18:43 +0200722static struct usb_endpoint_descriptor
723hs_status_desc = {
724 .bLength = USB_DT_ENDPOINT_SIZE,
725 .bDescriptorType = USB_DT_ENDPOINT,
726
727 .bmAttributes = USB_ENDPOINT_XFER_INT,
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400728 .wMaxPacketSize = __constant_cpu_to_le16(STATUS_BYTECOUNT),
Remy Bohmer23cd1382009-07-29 18:18:43 +0200729 .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4,
730};
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200731#endif /* CONFIG_USB_ETH_CDC */
Remy Bohmer23cd1382009-07-29 18:18:43 +0200732
733static struct usb_endpoint_descriptor
734hs_source_desc = {
735 .bLength = USB_DT_ENDPOINT_SIZE,
736 .bDescriptorType = USB_DT_ENDPOINT,
737
738 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400739 .wMaxPacketSize = __constant_cpu_to_le16(512),
Remy Bohmer23cd1382009-07-29 18:18:43 +0200740};
741
742static struct usb_endpoint_descriptor
743hs_sink_desc = {
744 .bLength = USB_DT_ENDPOINT_SIZE,
745 .bDescriptorType = USB_DT_ENDPOINT,
746
747 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400748 .wMaxPacketSize = __constant_cpu_to_le16(512),
Remy Bohmer23cd1382009-07-29 18:18:43 +0200749};
750
751static struct usb_qualifier_descriptor
752dev_qualifier = {
753 .bLength = sizeof dev_qualifier,
754 .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
755
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400756 .bcdUSB = __constant_cpu_to_le16(0x0200),
Remy Bohmer23cd1382009-07-29 18:18:43 +0200757 .bDeviceClass = USB_CLASS_COMM,
758
759 .bNumConfigurations = 1,
760};
761
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400762static const struct usb_descriptor_header *hs_eth_function[11] = {
Remy Bohmer23cd1382009-07-29 18:18:43 +0200763 (struct usb_descriptor_header *) &otg_descriptor,
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200764#ifdef CONFIG_USB_ETH_CDC
Remy Bohmer23cd1382009-07-29 18:18:43 +0200765 /* "cdc" mode descriptors */
766 (struct usb_descriptor_header *) &control_intf,
767 (struct usb_descriptor_header *) &header_desc,
768 (struct usb_descriptor_header *) &union_desc,
769 (struct usb_descriptor_header *) &ether_desc,
770 /* NOTE: status endpoint may need to be removed */
771 (struct usb_descriptor_header *) &hs_status_desc,
772 /* data interface, with altsetting */
773 (struct usb_descriptor_header *) &data_nop_intf,
774 (struct usb_descriptor_header *) &data_intf,
775 (struct usb_descriptor_header *) &hs_source_desc,
776 (struct usb_descriptor_header *) &hs_sink_desc,
777 NULL,
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200778#endif /* CONFIG_USB_ETH_CDC */
Remy Bohmer23cd1382009-07-29 18:18:43 +0200779};
780
781static inline void hs_subset_descriptors(void)
782{
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200783#ifdef CONFIG_USB_ETH_SUBSET
Remy Bohmer23cd1382009-07-29 18:18:43 +0200784 /* behavior is "CDC Subset"; extra descriptors say "SAFE" */
785 hs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
786 hs_eth_function[2] = (struct usb_descriptor_header *) &header_desc;
787 hs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc;
788 hs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc;
789 hs_eth_function[5] = (struct usb_descriptor_header *) &ether_desc;
790 hs_eth_function[6] = (struct usb_descriptor_header *) &hs_source_desc;
791 hs_eth_function[7] = (struct usb_descriptor_header *) &hs_sink_desc;
792 hs_eth_function[8] = NULL;
793#else
794 hs_eth_function[1] = NULL;
795#endif
796}
797
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300798#ifdef CONFIG_USB_ETH_RNDIS
799static const struct usb_descriptor_header *hs_rndis_function[] = {
800 (struct usb_descriptor_header *) &otg_descriptor,
801 /* control interface matches ACM, not Ethernet */
802 (struct usb_descriptor_header *) &rndis_control_intf,
803 (struct usb_descriptor_header *) &header_desc,
804 (struct usb_descriptor_header *) &call_mgmt_descriptor,
805 (struct usb_descriptor_header *) &acm_descriptor,
806 (struct usb_descriptor_header *) &union_desc,
807 (struct usb_descriptor_header *) &hs_status_desc,
808 /* data interface has no altsetting */
809 (struct usb_descriptor_header *) &rndis_data_intf,
810 (struct usb_descriptor_header *) &hs_source_desc,
811 (struct usb_descriptor_header *) &hs_sink_desc,
812 NULL,
813};
814#endif
815
816
Remy Bohmer23cd1382009-07-29 18:18:43 +0200817/* maxpacket and other transfer characteristics vary by speed. */
818static inline struct usb_endpoint_descriptor *
819ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *hs,
820 struct usb_endpoint_descriptor *fs)
821{
822 if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
823 return hs;
824 return fs;
825}
826
Remy Bohmer23cd1382009-07-29 18:18:43 +0200827/*-------------------------------------------------------------------------*/
828
829/* descriptors that are built on-demand */
830
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400831static char manufacturer[50];
832static char product_desc[40] = DRIVER_DESC;
833static char serial_number[20];
Remy Bohmer23cd1382009-07-29 18:18:43 +0200834
835/* address that the host will use ... usually assigned at random */
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400836static char ethaddr[2 * ETH_ALEN + 1];
Remy Bohmer23cd1382009-07-29 18:18:43 +0200837
838/* static strings, in UTF-8 */
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400839static struct usb_string strings[] = {
Remy Bohmer23cd1382009-07-29 18:18:43 +0200840 { STRING_MANUFACTURER, manufacturer, },
841 { STRING_PRODUCT, product_desc, },
842 { STRING_SERIALNUMBER, serial_number, },
843 { STRING_DATA, "Ethernet Data", },
844 { STRING_ETHADDR, ethaddr, },
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200845#ifdef CONFIG_USB_ETH_CDC
Remy Bohmer23cd1382009-07-29 18:18:43 +0200846 { STRING_CDC, "CDC Ethernet", },
847 { STRING_CONTROL, "CDC Communications Control", },
848#endif
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200849#ifdef CONFIG_USB_ETH_SUBSET
Remy Bohmer23cd1382009-07-29 18:18:43 +0200850 { STRING_SUBSET, "CDC Ethernet Subset", },
851#endif
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300852#ifdef CONFIG_USB_ETH_RNDIS
853 { STRING_RNDIS, "RNDIS", },
854 { STRING_RNDIS_CONTROL, "RNDIS Communications Control", },
855#endif
Remy Bohmer23cd1382009-07-29 18:18:43 +0200856 { } /* end of list */
857};
858
859static struct usb_gadget_strings stringtab = {
860 .language = 0x0409, /* en-us */
861 .strings = strings,
862};
863
Remy Bohmer23cd1382009-07-29 18:18:43 +0200864/*============================================================================*/
865static u8 control_req[USB_BUFSIZ];
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200866#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Stefano Babic80460772010-08-15 14:18:59 +0200867static u8 status_req[STATUS_BYTECOUNT] __attribute__ ((aligned(4)));
Lukasz Dalek563aed22012-10-02 17:04:29 +0200868#endif
Remy Bohmer23cd1382009-07-29 18:18:43 +0200869
870
Remy Bohmer23cd1382009-07-29 18:18:43 +0200871/**
872 * strlcpy - Copy a %NUL terminated string into a sized buffer
873 * @dest: Where to copy the string to
874 * @src: Where to copy the string from
875 * @size: size of destination buffer
876 *
877 * Compatible with *BSD: the result is always a valid
878 * NUL-terminated string that fits in the buffer (unless,
879 * of course, the buffer size is zero). It does not pad
880 * out the result like strncpy() does.
881 */
882size_t strlcpy(char *dest, const char *src, size_t size)
883{
884 size_t ret = strlen(src);
885
886 if (size) {
887 size_t len = (ret >= size) ? size - 1 : ret;
888 memcpy(dest, src, len);
889 dest[len] = '\0';
890 }
891 return ret;
892}
893
Remy Bohmer23cd1382009-07-29 18:18:43 +0200894/*============================================================================*/
895
896/*
897 * one config, two interfaces: control, data.
898 * complications: class descriptors, and an altsetting.
899 */
900static int
901config_buf(struct usb_gadget *g, u8 *buf, u8 type, unsigned index, int is_otg)
902{
903 int len;
904 const struct usb_config_descriptor *config;
905 const struct usb_descriptor_header **function;
906 int hs = 0;
907
908 if (gadget_is_dualspeed(g)) {
909 hs = (g->speed == USB_SPEED_HIGH);
910 if (type == USB_DT_OTHER_SPEED_CONFIG)
911 hs = !hs;
912 }
913#define which_fn(t) (hs ? hs_ ## t ## _function : fs_ ## t ## _function)
914
915 if (index >= device_desc.bNumConfigurations)
916 return -EINVAL;
917
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300918#ifdef CONFIG_USB_ETH_RNDIS
919 /*
920 * list the RNDIS config first, to make Microsoft's drivers
921 * happy. DOCSIS 1.0 needs this too.
922 */
923 if (device_desc.bNumConfigurations == 2 && index == 0) {
924 config = &rndis_config;
925 function = which_fn(rndis);
926 } else
927#endif
928 {
929 config = &eth_config;
930 function = which_fn(eth);
931 }
Remy Bohmer23cd1382009-07-29 18:18:43 +0200932
933 /* for now, don't advertise srp-only devices */
934 if (!is_otg)
935 function++;
936
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400937 len = usb_gadget_config_buf(config, buf, USB_BUFSIZ, function);
Remy Bohmer23cd1382009-07-29 18:18:43 +0200938 if (len < 0)
939 return len;
940 ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
941 return len;
942}
943
944/*-------------------------------------------------------------------------*/
945
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300946static void eth_start(struct eth_dev *dev, gfp_t gfp_flags);
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400947static int alloc_requests(struct eth_dev *dev, unsigned n, gfp_t gfp_flags);
Remy Bohmer23cd1382009-07-29 18:18:43 +0200948
949static int
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400950set_ether_config(struct eth_dev *dev, gfp_t gfp_flags)
Remy Bohmer23cd1382009-07-29 18:18:43 +0200951{
952 int result = 0;
953 struct usb_gadget *gadget = dev->gadget;
954
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200955#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300956 /* status endpoint used for RNDIS and (optionally) CDC */
Remy Bohmer23cd1382009-07-29 18:18:43 +0200957 if (!subset_active(dev) && dev->status_ep) {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400958 dev->status = ep_desc(gadget, &hs_status_desc,
Remy Bohmer23cd1382009-07-29 18:18:43 +0200959 &fs_status_desc);
960 dev->status_ep->driver_data = dev;
961
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400962 result = usb_ep_enable(dev->status_ep, dev->status);
Remy Bohmer23cd1382009-07-29 18:18:43 +0200963 if (result != 0) {
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +0400964 debug("enable %s --> %d\n",
Remy Bohmer23cd1382009-07-29 18:18:43 +0200965 dev->status_ep->name, result);
966 goto done;
967 }
968 }
969#endif
970
971 dev->in = ep_desc(gadget, &hs_source_desc, &fs_source_desc);
972 dev->in_ep->driver_data = dev;
973
974 dev->out = ep_desc(gadget, &hs_sink_desc, &fs_sink_desc);
975 dev->out_ep->driver_data = dev;
976
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400977 /*
978 * With CDC, the host isn't allowed to use these two data
Remy Bohmer23cd1382009-07-29 18:18:43 +0200979 * endpoints in the default altsetting for the interface.
980 * so we don't activate them yet. Reset from SET_INTERFACE.
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300981 *
982 * Strictly speaking RNDIS should work the same: activation is
983 * a side effect of setting a packet filter. Deactivation is
984 * from REMOTE_NDIS_HALT_MSG, reset from REMOTE_NDIS_RESET_MSG.
Remy Bohmer23cd1382009-07-29 18:18:43 +0200985 */
986 if (!cdc_active(dev)) {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400987 result = usb_ep_enable(dev->in_ep, dev->in);
Remy Bohmer23cd1382009-07-29 18:18:43 +0200988 if (result != 0) {
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +0400989 debug("enable %s --> %d\n",
Remy Bohmer23cd1382009-07-29 18:18:43 +0200990 dev->in_ep->name, result);
991 goto done;
992 }
993
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400994 result = usb_ep_enable(dev->out_ep, dev->out);
Remy Bohmer23cd1382009-07-29 18:18:43 +0200995 if (result != 0) {
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +0400996 debug("enable %s --> %d\n",
Remy Bohmer23cd1382009-07-29 18:18:43 +0200997 dev->out_ep->name, result);
998 goto done;
999 }
1000 }
1001
1002done:
1003 if (result == 0)
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001004 result = alloc_requests(dev, qlen(gadget), gfp_flags);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001005
1006 /* on error, disable any endpoints */
1007 if (result < 0) {
Vitaly Kuzmichevd5292c12010-08-13 17:02:29 +04001008 if (!subset_active(dev) && dev->status_ep)
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001009 (void) usb_ep_disable(dev->status_ep);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001010 dev->status = NULL;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001011 (void) usb_ep_disable(dev->in_ep);
1012 (void) usb_ep_disable(dev->out_ep);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001013 dev->in = NULL;
1014 dev->out = NULL;
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001015 } else if (!cdc_active(dev)) {
1016 /*
1017 * activate non-CDC configs right away
1018 * this isn't strictly according to the RNDIS spec
1019 */
1020 eth_start(dev, GFP_ATOMIC);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001021 }
1022
1023 /* caller is responsible for cleanup on error */
1024 return result;
1025}
1026
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001027static void eth_reset_config(struct eth_dev *dev)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001028{
1029 if (dev->config == 0)
1030 return;
1031
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001032 debug("%s\n", __func__);
1033
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001034 rndis_uninit(dev->rndis_config);
1035
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001036 /*
1037 * disable endpoints, forcing (synchronous) completion of
Remy Bohmer23cd1382009-07-29 18:18:43 +02001038 * pending i/o. then free the requests.
1039 */
1040
1041 if (dev->in) {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001042 usb_ep_disable(dev->in_ep);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001043 if (dev->tx_req) {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001044 usb_ep_free_request(dev->in_ep, dev->tx_req);
1045 dev->tx_req = NULL;
Remy Bohmer23cd1382009-07-29 18:18:43 +02001046 }
1047 }
1048 if (dev->out) {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001049 usb_ep_disable(dev->out_ep);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001050 if (dev->rx_req) {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001051 usb_ep_free_request(dev->out_ep, dev->rx_req);
1052 dev->rx_req = NULL;
Remy Bohmer23cd1382009-07-29 18:18:43 +02001053 }
1054 }
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001055 if (dev->status)
1056 usb_ep_disable(dev->status_ep);
1057
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001058 dev->rndis = 0;
Remy Bohmer23cd1382009-07-29 18:18:43 +02001059 dev->cdc_filter = 0;
1060 dev->config = 0;
1061}
1062
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001063/*
1064 * change our operational config. must agree with the code
Remy Bohmer23cd1382009-07-29 18:18:43 +02001065 * that returns config descriptors, and altsetting code.
1066 */
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001067static int eth_set_config(struct eth_dev *dev, unsigned number,
1068 gfp_t gfp_flags)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001069{
1070 int result = 0;
1071 struct usb_gadget *gadget = dev->gadget;
1072
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001073 if (gadget_is_sa1100(gadget)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001074 && dev->config
1075 && dev->tx_qlen != 0) {
1076 /* tx fifo is full, but we can't clear it...*/
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001077 error("can't change configurations");
Remy Bohmer23cd1382009-07-29 18:18:43 +02001078 return -ESPIPE;
1079 }
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001080 eth_reset_config(dev);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001081
1082 switch (number) {
1083 case DEV_CONFIG_VALUE:
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001084 result = set_ether_config(dev, gfp_flags);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001085 break;
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001086#ifdef CONFIG_USB_ETH_RNDIS
1087 case DEV_RNDIS_CONFIG_VALUE:
1088 dev->rndis = 1;
1089 result = set_ether_config(dev, gfp_flags);
1090 break;
1091#endif
Remy Bohmer23cd1382009-07-29 18:18:43 +02001092 default:
1093 result = -EINVAL;
1094 /* FALL THROUGH */
1095 case 0:
1096 break;
1097 }
1098
1099 if (result) {
1100 if (number)
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001101 eth_reset_config(dev);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001102 usb_gadget_vbus_draw(dev->gadget,
1103 gadget_is_otg(dev->gadget) ? 8 : 100);
1104 } else {
1105 char *speed;
1106 unsigned power;
1107
1108 power = 2 * eth_config.bMaxPower;
1109 usb_gadget_vbus_draw(dev->gadget, power);
1110
1111 switch (gadget->speed) {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001112 case USB_SPEED_FULL:
1113 speed = "full"; break;
Remy Bohmer23cd1382009-07-29 18:18:43 +02001114#ifdef CONFIG_USB_GADGET_DUALSPEED
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001115 case USB_SPEED_HIGH:
1116 speed = "high"; break;
Remy Bohmer23cd1382009-07-29 18:18:43 +02001117#endif
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001118 default:
1119 speed = "?"; break;
Remy Bohmer23cd1382009-07-29 18:18:43 +02001120 }
1121
1122 dev->config = number;
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001123 printf("%s speed config #%d: %d mA, %s, using %s\n",
Remy Bohmer23cd1382009-07-29 18:18:43 +02001124 speed, number, power, driver_desc,
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001125 rndis_active(dev)
1126 ? "RNDIS"
1127 : (cdc_active(dev)
1128 ? "CDC Ethernet"
Remy Bohmer23cd1382009-07-29 18:18:43 +02001129 : "CDC Ethernet Subset"));
1130 }
1131 return result;
1132}
1133
1134/*-------------------------------------------------------------------------*/
1135
Lukasz Dalek2bb37882012-10-02 17:04:31 +02001136#ifdef CONFIG_USB_ETH_CDC
Remy Bohmer23cd1382009-07-29 18:18:43 +02001137
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001138/*
1139 * The interrupt endpoint is used in CDC networking models (Ethernet, ATM)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001140 * only to notify the host about link status changes (which we support) or
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001141 * report completion of some encapsulated command (as used in RNDIS). Since
Remy Bohmer23cd1382009-07-29 18:18:43 +02001142 * we want this CDC Ethernet code to be vendor-neutral, we don't use that
1143 * command mechanism; and only one status request is ever queued.
1144 */
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001145static void eth_status_complete(struct usb_ep *ep, struct usb_request *req)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001146{
1147 struct usb_cdc_notification *event = req->buf;
1148 int value = req->status;
1149 struct eth_dev *dev = ep->driver_data;
1150
1151 /* issue the second notification if host reads the first */
1152 if (event->bNotificationType == USB_CDC_NOTIFY_NETWORK_CONNECTION
1153 && value == 0) {
1154 __le32 *data = req->buf + sizeof *event;
1155
1156 event->bmRequestType = 0xA1;
1157 event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001158 event->wValue = __constant_cpu_to_le16(0);
1159 event->wIndex = __constant_cpu_to_le16(1);
1160 event->wLength = __constant_cpu_to_le16(8);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001161
1162 /* SPEED_CHANGE data is up/down speeds in bits/sec */
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001163 data[0] = data[1] = cpu_to_le32(BITRATE(dev->gadget));
Remy Bohmer23cd1382009-07-29 18:18:43 +02001164
1165 req->length = STATUS_BYTECOUNT;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001166 value = usb_ep_queue(ep, req, GFP_ATOMIC);
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001167 debug("send SPEED_CHANGE --> %d\n", value);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001168 if (value == 0)
1169 return;
1170 } else if (value != -ECONNRESET) {
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001171 debug("event %02x --> %d\n",
Remy Bohmer23cd1382009-07-29 18:18:43 +02001172 event->bNotificationType, value);
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001173 if (event->bNotificationType ==
1174 USB_CDC_NOTIFY_SPEED_CHANGE) {
1175 l_ethdev.network_started = 1;
Remy Bohmer23cd1382009-07-29 18:18:43 +02001176 printf("USB network up!\n");
1177 }
1178 }
1179 req->context = NULL;
1180}
1181
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001182static void issue_start_status(struct eth_dev *dev)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001183{
1184 struct usb_request *req = dev->stat_req;
1185 struct usb_cdc_notification *event;
1186 int value;
1187
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001188 /*
1189 * flush old status
Remy Bohmer23cd1382009-07-29 18:18:43 +02001190 *
1191 * FIXME ugly idiom, maybe we'd be better with just
1192 * a "cancel the whole queue" primitive since any
1193 * unlink-one primitive has way too many error modes.
1194 * here, we "know" toggle is already clear...
1195 *
1196 * FIXME iff req->context != null just dequeue it
1197 */
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001198 usb_ep_disable(dev->status_ep);
1199 usb_ep_enable(dev->status_ep, dev->status);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001200
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001201 /*
1202 * 3.8.1 says to issue first NETWORK_CONNECTION, then
Remy Bohmer23cd1382009-07-29 18:18:43 +02001203 * a SPEED_CHANGE. could be useful in some configs.
1204 */
1205 event = req->buf;
1206 event->bmRequestType = 0xA1;
1207 event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001208 event->wValue = __constant_cpu_to_le16(1); /* connected */
1209 event->wIndex = __constant_cpu_to_le16(1);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001210 event->wLength = 0;
1211
1212 req->length = sizeof *event;
1213 req->complete = eth_status_complete;
1214 req->context = dev;
1215
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001216 value = usb_ep_queue(dev->status_ep, req, GFP_ATOMIC);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001217 if (value < 0)
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001218 debug("status buf queue --> %d\n", value);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001219}
1220
1221#endif
1222
1223/*-------------------------------------------------------------------------*/
1224
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001225static void eth_setup_complete(struct usb_ep *ep, struct usb_request *req)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001226{
1227 if (req->status || req->actual != req->length)
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001228 debug("setup complete --> %d, %d/%d\n",
Remy Bohmer23cd1382009-07-29 18:18:43 +02001229 req->status, req->actual, req->length);
1230}
1231
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001232#ifdef CONFIG_USB_ETH_RNDIS
1233
1234static void rndis_response_complete(struct usb_ep *ep, struct usb_request *req)
1235{
1236 if (req->status || req->actual != req->length)
1237 debug("rndis response complete --> %d, %d/%d\n",
1238 req->status, req->actual, req->length);
1239
1240 /* done sending after USB_CDC_GET_ENCAPSULATED_RESPONSE */
1241}
1242
1243static void rndis_command_complete(struct usb_ep *ep, struct usb_request *req)
1244{
1245 struct eth_dev *dev = ep->driver_data;
1246 int status;
1247
1248 /* received RNDIS command from USB_CDC_SEND_ENCAPSULATED_COMMAND */
1249 status = rndis_msg_parser(dev->rndis_config, (u8 *) req->buf);
1250 if (status < 0)
1251 error("%s: rndis parse error %d", __func__, status);
1252}
1253
1254#endif /* RNDIS */
1255
Remy Bohmer23cd1382009-07-29 18:18:43 +02001256/*
1257 * The setup() callback implements all the ep0 functionality that's not
1258 * handled lower down. CDC has a number of less-common features:
1259 *
1260 * - two interfaces: control, and ethernet data
1261 * - Ethernet data interface has two altsettings: default, and active
1262 * - class-specific descriptors for the control interface
1263 * - class-specific control requests
1264 */
1265static int
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001266eth_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001267{
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001268 struct eth_dev *dev = get_gadget_data(gadget);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001269 struct usb_request *req = dev->req;
1270 int value = -EOPNOTSUPP;
1271 u16 wIndex = le16_to_cpu(ctrl->wIndex);
1272 u16 wValue = le16_to_cpu(ctrl->wValue);
1273 u16 wLength = le16_to_cpu(ctrl->wLength);
1274
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001275 /*
1276 * descriptors just go into the pre-allocated ep0 buffer,
Remy Bohmer23cd1382009-07-29 18:18:43 +02001277 * while config change events may enable network traffic.
1278 */
1279
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001280 debug("%s\n", __func__);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001281
1282 req->complete = eth_setup_complete;
1283 switch (ctrl->bRequest) {
1284
1285 case USB_REQ_GET_DESCRIPTOR:
1286 if (ctrl->bRequestType != USB_DIR_IN)
1287 break;
1288 switch (wValue >> 8) {
1289
1290 case USB_DT_DEVICE:
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001291 value = min(wLength, (u16) sizeof device_desc);
1292 memcpy(req->buf, &device_desc, value);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001293 break;
1294 case USB_DT_DEVICE_QUALIFIER:
1295 if (!gadget_is_dualspeed(gadget))
1296 break;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001297 value = min(wLength, (u16) sizeof dev_qualifier);
1298 memcpy(req->buf, &dev_qualifier, value);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001299 break;
1300
1301 case USB_DT_OTHER_SPEED_CONFIG:
1302 if (!gadget_is_dualspeed(gadget))
1303 break;
1304 /* FALLTHROUGH */
1305 case USB_DT_CONFIG:
1306 value = config_buf(gadget, req->buf,
1307 wValue >> 8,
1308 wValue & 0xff,
1309 gadget_is_otg(gadget));
1310 if (value >= 0)
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001311 value = min(wLength, (u16) value);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001312 break;
1313
1314 case USB_DT_STRING:
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001315 value = usb_gadget_get_string(&stringtab,
Remy Bohmer23cd1382009-07-29 18:18:43 +02001316 wValue & 0xff, req->buf);
1317
1318 if (value >= 0)
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001319 value = min(wLength, (u16) value);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001320
1321 break;
1322 }
1323 break;
1324
1325 case USB_REQ_SET_CONFIGURATION:
1326 if (ctrl->bRequestType != 0)
1327 break;
1328 if (gadget->a_hnp_support)
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001329 debug("HNP available\n");
Remy Bohmer23cd1382009-07-29 18:18:43 +02001330 else if (gadget->a_alt_hnp_support)
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001331 debug("HNP needs a different root port\n");
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001332 value = eth_set_config(dev, wValue, GFP_ATOMIC);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001333 break;
1334 case USB_REQ_GET_CONFIGURATION:
1335 if (ctrl->bRequestType != USB_DIR_IN)
1336 break;
1337 *(u8 *)req->buf = dev->config;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001338 value = min(wLength, (u16) 1);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001339 break;
1340
1341 case USB_REQ_SET_INTERFACE:
1342 if (ctrl->bRequestType != USB_RECIP_INTERFACE
1343 || !dev->config
1344 || wIndex > 1)
1345 break;
1346 if (!cdc_active(dev) && wIndex != 0)
1347 break;
1348
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001349 /*
1350 * PXA hardware partially handles SET_INTERFACE;
Remy Bohmer23cd1382009-07-29 18:18:43 +02001351 * we need to kluge around that interference.
1352 */
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001353 if (gadget_is_pxa(gadget)) {
1354 value = eth_set_config(dev, DEV_CONFIG_VALUE,
Remy Bohmer23cd1382009-07-29 18:18:43 +02001355 GFP_ATOMIC);
Lukasz Dalek563aed22012-10-02 17:04:29 +02001356 /*
1357 * PXA25x driver use non-CDC ethernet gadget.
1358 * But only _CDC and _RNDIS code can signalize
1359 * that network is working. So we signalize it
1360 * here.
1361 */
1362 l_ethdev.network_started = 1;
1363 debug("USB network up!\n");
Remy Bohmer23cd1382009-07-29 18:18:43 +02001364 goto done_set_intf;
1365 }
1366
Lukasz Dalek2bb37882012-10-02 17:04:31 +02001367#ifdef CONFIG_USB_ETH_CDC
Remy Bohmer23cd1382009-07-29 18:18:43 +02001368 switch (wIndex) {
1369 case 0: /* control/master intf */
1370 if (wValue != 0)
1371 break;
1372 if (dev->status) {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001373 usb_ep_disable(dev->status_ep);
1374 usb_ep_enable(dev->status_ep, dev->status);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001375 }
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001376
Remy Bohmer23cd1382009-07-29 18:18:43 +02001377 value = 0;
1378 break;
1379 case 1: /* data intf */
1380 if (wValue > 1)
1381 break;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001382 usb_ep_disable(dev->in_ep);
1383 usb_ep_disable(dev->out_ep);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001384
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001385 /*
1386 * CDC requires the data transfers not be done from
Remy Bohmer23cd1382009-07-29 18:18:43 +02001387 * the default interface setting ... also, setting
1388 * the non-default interface resets filters etc.
1389 */
1390 if (wValue == 1) {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001391 if (!cdc_active(dev))
Remy Bohmer23cd1382009-07-29 18:18:43 +02001392 break;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001393 usb_ep_enable(dev->in_ep, dev->in);
1394 usb_ep_enable(dev->out_ep, dev->out);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001395 dev->cdc_filter = DEFAULT_FILTER;
1396 if (dev->status)
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001397 issue_start_status(dev);
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001398 eth_start(dev, GFP_ATOMIC);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001399 }
Remy Bohmer23cd1382009-07-29 18:18:43 +02001400 value = 0;
1401 break;
1402 }
1403#else
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001404 /*
1405 * FIXME this is wrong, as is the assumption that
Remy Bohmer23cd1382009-07-29 18:18:43 +02001406 * all non-PXA hardware talks real CDC ...
1407 */
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001408 debug("set_interface ignored!\n");
Lukasz Dalek2bb37882012-10-02 17:04:31 +02001409#endif /* CONFIG_USB_ETH_CDC */
Remy Bohmer23cd1382009-07-29 18:18:43 +02001410
1411done_set_intf:
1412 break;
1413 case USB_REQ_GET_INTERFACE:
1414 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE)
1415 || !dev->config
1416 || wIndex > 1)
1417 break;
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001418 if (!(cdc_active(dev) || rndis_active(dev)) && wIndex != 0)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001419 break;
1420
1421 /* for CDC, iff carrier is on, data interface is active. */
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001422 if (rndis_active(dev) || wIndex != 1)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001423 *(u8 *)req->buf = 0;
1424 else {
1425 /* *(u8 *)req->buf = netif_carrier_ok (dev->net) ? 1 : 0; */
1426 /* carrier always ok ...*/
1427 *(u8 *)req->buf = 1 ;
1428 }
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001429 value = min(wLength, (u16) 1);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001430 break;
1431
Lukasz Dalek2bb37882012-10-02 17:04:31 +02001432#ifdef CONFIG_USB_ETH_CDC
Remy Bohmer23cd1382009-07-29 18:18:43 +02001433 case USB_CDC_SET_ETHERNET_PACKET_FILTER:
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001434 /*
1435 * see 6.2.30: no data, wIndex = interface,
Remy Bohmer23cd1382009-07-29 18:18:43 +02001436 * wValue = packet filter bitmap
1437 */
1438 if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1439 || !cdc_active(dev)
1440 || wLength != 0
1441 || wIndex > 1)
1442 break;
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001443 debug("packet filter %02x\n", wValue);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001444 dev->cdc_filter = wValue;
1445 value = 0;
1446 break;
1447
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001448 /*
1449 * and potentially:
Remy Bohmer23cd1382009-07-29 18:18:43 +02001450 * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS:
1451 * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER:
1452 * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER:
1453 * case USB_CDC_GET_ETHERNET_STATISTIC:
1454 */
1455
Lukasz Dalek2bb37882012-10-02 17:04:31 +02001456#endif /* CONFIG_USB_ETH_CDC */
Remy Bohmer23cd1382009-07-29 18:18:43 +02001457
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001458#ifdef CONFIG_USB_ETH_RNDIS
1459 /*
1460 * RNDIS uses the CDC command encapsulation mechanism to implement
1461 * an RPC scheme, with much getting/setting of attributes by OID.
1462 */
1463 case USB_CDC_SEND_ENCAPSULATED_COMMAND:
1464 if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1465 || !rndis_active(dev)
1466 || wLength > USB_BUFSIZ
1467 || wValue
1468 || rndis_control_intf.bInterfaceNumber
1469 != wIndex)
1470 break;
1471 /* read the request, then process it */
1472 value = wLength;
1473 req->complete = rndis_command_complete;
1474 /* later, rndis_control_ack () sends a notification */
1475 break;
1476
1477 case USB_CDC_GET_ENCAPSULATED_RESPONSE:
1478 if ((USB_DIR_IN|USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1479 == ctrl->bRequestType
1480 && rndis_active(dev)
1481 /* && wLength >= 0x0400 */
1482 && !wValue
1483 && rndis_control_intf.bInterfaceNumber
1484 == wIndex) {
1485 u8 *buf;
1486 u32 n;
1487
1488 /* return the result */
1489 buf = rndis_get_next_response(dev->rndis_config, &n);
1490 if (buf) {
1491 memcpy(req->buf, buf, n);
1492 req->complete = rndis_response_complete;
1493 rndis_free_response(dev->rndis_config, buf);
1494 value = n;
1495 }
1496 /* else stalls ... spec says to avoid that */
1497 }
1498 break;
1499#endif /* RNDIS */
1500
Remy Bohmer23cd1382009-07-29 18:18:43 +02001501 default:
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001502 debug("unknown control req%02x.%02x v%04x i%04x l%d\n",
Remy Bohmer23cd1382009-07-29 18:18:43 +02001503 ctrl->bRequestType, ctrl->bRequest,
1504 wValue, wIndex, wLength);
1505 }
1506
1507 /* respond with data transfer before status phase? */
1508 if (value >= 0) {
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001509 debug("respond with data transfer before status phase\n");
Remy Bohmer23cd1382009-07-29 18:18:43 +02001510 req->length = value;
1511 req->zero = value < wLength
1512 && (value % gadget->ep0->maxpacket) == 0;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001513 value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001514 if (value < 0) {
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001515 debug("ep_queue --> %d\n", value);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001516 req->status = 0;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001517 eth_setup_complete(gadget->ep0, req);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001518 }
1519 }
1520
1521 /* host either stalls (value < 0) or reports success */
1522 return value;
1523}
1524
Remy Bohmer23cd1382009-07-29 18:18:43 +02001525/*-------------------------------------------------------------------------*/
1526
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001527static void rx_complete(struct usb_ep *ep, struct usb_request *req);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001528
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001529static int rx_submit(struct eth_dev *dev, struct usb_request *req,
Remy Bohmer23cd1382009-07-29 18:18:43 +02001530 gfp_t gfp_flags)
1531{
1532 int retval = -ENOMEM;
1533 size_t size;
1534
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001535 /*
1536 * Padding up to RX_EXTRA handles minor disagreements with host.
Remy Bohmer23cd1382009-07-29 18:18:43 +02001537 * Normally we use the USB "terminate on short read" convention;
1538 * so allow up to (N*maxpacket), since that memory is normally
1539 * already allocated. Some hardware doesn't deal well with short
1540 * reads (e.g. DMA must be N*maxpacket), so for now don't trim a
1541 * byte off the end (to force hardware errors on overflow).
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001542 *
1543 * RNDIS uses internal framing, and explicitly allows senders to
1544 * pad to end-of-packet. That's potentially nice for speed,
1545 * but means receivers can't recover synch on their own.
Remy Bohmer23cd1382009-07-29 18:18:43 +02001546 */
1547
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001548 debug("%s\n", __func__);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001549
1550 size = (ETHER_HDR_SIZE + dev->mtu + RX_EXTRA);
1551 size += dev->out_ep->maxpacket - 1;
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001552 if (rndis_active(dev))
1553 size += sizeof(struct rndis_packet_msg_type);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001554 size -= size % dev->out_ep->maxpacket;
1555
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001556 /*
1557 * Some platforms perform better when IP packets are aligned,
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001558 * but on at least one, checksumming fails otherwise. Note:
1559 * RNDIS headers involve variable numbers of LE32 values.
Remy Bohmer23cd1382009-07-29 18:18:43 +02001560 */
1561
1562 req->buf = (u8 *) NetRxPackets[0];
1563 req->length = size;
1564 req->complete = rx_complete;
1565
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001566 retval = usb_ep_queue(dev->out_ep, req, gfp_flags);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001567
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001568 if (retval)
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001569 error("rx submit --> %d", retval);
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001570
Remy Bohmer23cd1382009-07-29 18:18:43 +02001571 return retval;
1572}
1573
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001574static void rx_complete(struct usb_ep *ep, struct usb_request *req)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001575{
1576 struct eth_dev *dev = ep->driver_data;
1577
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001578 debug("%s: status %d\n", __func__, req->status);
Vitaly Kuzmichevc85d70e2011-02-11 18:18:32 +03001579 switch (req->status) {
1580 /* normal completion */
1581 case 0:
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001582 if (rndis_active(dev)) {
1583 /* we know MaxPacketsPerTransfer == 1 here */
1584 int length = rndis_rm_hdr(req->buf, req->actual);
1585 if (length < 0)
1586 goto length_err;
1587 req->length -= length;
1588 req->actual -= length;
1589 }
1590 if (req->actual < ETH_HLEN || ETH_FRAME_LEN < req->actual) {
1591length_err:
1592 dev->stats.rx_errors++;
1593 dev->stats.rx_length_errors++;
1594 debug("rx length %d\n", req->length);
1595 break;
1596 }
1597
Vitaly Kuzmichevc85d70e2011-02-11 18:18:32 +03001598 dev->stats.rx_packets++;
1599 dev->stats.rx_bytes += req->length;
1600 break;
1601
1602 /* software-driven interface shutdown */
1603 case -ECONNRESET: /* unlink */
1604 case -ESHUTDOWN: /* disconnect etc */
1605 /* for hardware automagic (such as pxa) */
1606 case -ECONNABORTED: /* endpoint reset */
1607 break;
1608
1609 /* data overrun */
1610 case -EOVERFLOW:
1611 dev->stats.rx_over_errors++;
1612 /* FALLTHROUGH */
1613 default:
1614 dev->stats.rx_errors++;
1615 break;
1616 }
Remy Bohmer23cd1382009-07-29 18:18:43 +02001617
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001618 packet_received = 1;
Remy Bohmer23cd1382009-07-29 18:18:43 +02001619}
1620
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001621static int alloc_requests(struct eth_dev *dev, unsigned n, gfp_t gfp_flags)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001622{
1623
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001624 dev->tx_req = usb_ep_alloc_request(dev->in_ep, 0);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001625
1626 if (!dev->tx_req)
Vitaly Kuzmichevac5d32d2010-09-22 13:13:55 +04001627 goto fail1;
Remy Bohmer23cd1382009-07-29 18:18:43 +02001628
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001629 dev->rx_req = usb_ep_alloc_request(dev->out_ep, 0);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001630
1631 if (!dev->rx_req)
Vitaly Kuzmichevac5d32d2010-09-22 13:13:55 +04001632 goto fail2;
Remy Bohmer23cd1382009-07-29 18:18:43 +02001633
1634 return 0;
1635
Vitaly Kuzmichevac5d32d2010-09-22 13:13:55 +04001636fail2:
1637 usb_ep_free_request(dev->in_ep, dev->tx_req);
1638fail1:
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001639 error("can't alloc requests");
Remy Bohmer23cd1382009-07-29 18:18:43 +02001640 return -1;
1641}
1642
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001643static void tx_complete(struct usb_ep *ep, struct usb_request *req)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001644{
Vitaly Kuzmichevc85d70e2011-02-11 18:18:32 +03001645 struct eth_dev *dev = ep->driver_data;
1646
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001647 debug("%s: status %s\n", __func__, (req->status) ? "failed" : "ok");
Vitaly Kuzmichevc85d70e2011-02-11 18:18:32 +03001648 switch (req->status) {
1649 default:
1650 dev->stats.tx_errors++;
1651 debug("tx err %d\n", req->status);
1652 /* FALLTHROUGH */
1653 case -ECONNRESET: /* unlink */
1654 case -ESHUTDOWN: /* disconnect etc */
1655 break;
1656 case 0:
1657 dev->stats.tx_bytes += req->length;
1658 }
1659 dev->stats.tx_packets++;
1660
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001661 packet_sent = 1;
Remy Bohmer23cd1382009-07-29 18:18:43 +02001662}
1663
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001664static inline int eth_is_promisc(struct eth_dev *dev)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001665{
1666 /* no filters for the CDC subset; always promisc */
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001667 if (subset_active(dev))
Remy Bohmer23cd1382009-07-29 18:18:43 +02001668 return 1;
1669 return dev->cdc_filter & USB_CDC_PACKET_TYPE_PROMISCUOUS;
1670}
1671
1672#if 0
1673static int eth_start_xmit (struct sk_buff *skb, struct net_device *net)
1674{
1675 struct eth_dev *dev = netdev_priv(net);
1676 int length = skb->len;
1677 int retval;
1678 struct usb_request *req = NULL;
1679 unsigned long flags;
1680
1681 /* apply outgoing CDC or RNDIS filters */
1682 if (!eth_is_promisc (dev)) {
1683 u8 *dest = skb->data;
1684
1685 if (is_multicast_ether_addr(dest)) {
1686 u16 type;
1687
1688 /* ignores USB_CDC_PACKET_TYPE_MULTICAST and host
1689 * SET_ETHERNET_MULTICAST_FILTERS requests
1690 */
1691 if (is_broadcast_ether_addr(dest))
1692 type = USB_CDC_PACKET_TYPE_BROADCAST;
1693 else
1694 type = USB_CDC_PACKET_TYPE_ALL_MULTICAST;
1695 if (!(dev->cdc_filter & type)) {
1696 dev_kfree_skb_any (skb);
1697 return 0;
1698 }
1699 }
1700 /* ignores USB_CDC_PACKET_TYPE_DIRECTED */
1701 }
1702
1703 spin_lock_irqsave(&dev->req_lock, flags);
1704 /*
1705 * this freelist can be empty if an interrupt triggered disconnect()
1706 * and reconfigured the gadget (shutting down this queue) after the
1707 * network stack decided to xmit but before we got the spinlock.
1708 */
1709 if (list_empty(&dev->tx_reqs)) {
1710 spin_unlock_irqrestore(&dev->req_lock, flags);
1711 return 1;
1712 }
1713
1714 req = container_of (dev->tx_reqs.next, struct usb_request, list);
1715 list_del (&req->list);
1716
1717 /* temporarily stop TX queue when the freelist empties */
1718 if (list_empty (&dev->tx_reqs))
1719 netif_stop_queue (net);
1720 spin_unlock_irqrestore(&dev->req_lock, flags);
1721
1722 /* no buffer copies needed, unless the network stack did it
1723 * or the hardware can't use skb buffers.
1724 * or there's not enough space for any RNDIS headers we need
1725 */
1726 if (rndis_active(dev)) {
1727 struct sk_buff *skb_rndis;
1728
1729 skb_rndis = skb_realloc_headroom (skb,
1730 sizeof (struct rndis_packet_msg_type));
1731 if (!skb_rndis)
1732 goto drop;
1733
1734 dev_kfree_skb_any (skb);
1735 skb = skb_rndis;
1736 rndis_add_hdr (skb);
1737 length = skb->len;
1738 }
1739 req->buf = skb->data;
1740 req->context = skb;
1741 req->complete = tx_complete;
1742
1743 /* use zlp framing on tx for strict CDC-Ether conformance,
1744 * though any robust network rx path ignores extra padding.
1745 * and some hardware doesn't like to write zlps.
1746 */
1747 req->zero = 1;
1748 if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0)
1749 length++;
1750
1751 req->length = length;
1752
1753 /* throttle highspeed IRQ rate back slightly */
1754 if (gadget_is_dualspeed(dev->gadget))
1755 req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH)
1756 ? ((atomic_read(&dev->tx_qlen) % qmult) != 0)
1757 : 0;
1758
1759 retval = usb_ep_queue (dev->in_ep, req, GFP_ATOMIC);
1760 switch (retval) {
1761 default:
1762 DEBUG (dev, "tx queue err %d\n", retval);
1763 break;
1764 case 0:
1765 net->trans_start = jiffies;
1766 atomic_inc (&dev->tx_qlen);
1767 }
1768
1769 if (retval) {
1770drop:
1771 dev->stats.tx_dropped++;
1772 dev_kfree_skb_any (skb);
1773 spin_lock_irqsave(&dev->req_lock, flags);
1774 if (list_empty (&dev->tx_reqs))
1775 netif_start_queue (net);
1776 list_add (&req->list, &dev->tx_reqs);
1777 spin_unlock_irqrestore(&dev->req_lock, flags);
1778 }
1779 return 0;
1780}
1781
1782/*-------------------------------------------------------------------------*/
1783#endif
1784
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001785static void eth_unbind(struct usb_gadget *gadget)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001786{
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001787 struct eth_dev *dev = get_gadget_data(gadget);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001788
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001789 debug("%s...\n", __func__);
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001790 rndis_deregister(dev->rndis_config);
1791 rndis_exit();
Remy Bohmer23cd1382009-07-29 18:18:43 +02001792
Vitaly Kuzmichev0129e322010-08-13 17:00:16 +04001793 /* we've already been disconnected ... no i/o is active */
1794 if (dev->req) {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001795 usb_ep_free_request(gadget->ep0, dev->req);
Vitaly Kuzmichev0129e322010-08-13 17:00:16 +04001796 dev->req = NULL;
1797 }
Remy Bohmer23cd1382009-07-29 18:18:43 +02001798 if (dev->stat_req) {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001799 usb_ep_free_request(dev->status_ep, dev->stat_req);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001800 dev->stat_req = NULL;
1801 }
1802
1803 if (dev->tx_req) {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001804 usb_ep_free_request(dev->in_ep, dev->tx_req);
1805 dev->tx_req = NULL;
Remy Bohmer23cd1382009-07-29 18:18:43 +02001806 }
1807
1808 if (dev->rx_req) {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001809 usb_ep_free_request(dev->out_ep, dev->rx_req);
1810 dev->rx_req = NULL;
Remy Bohmer23cd1382009-07-29 18:18:43 +02001811 }
1812
1813/* unregister_netdev (dev->net);*/
1814/* free_netdev(dev->net);*/
1815
Lei Wen988ee3e2010-12-01 23:43:43 +08001816 dev->gadget = NULL;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001817 set_gadget_data(gadget, NULL);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001818}
1819
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001820static void eth_disconnect(struct usb_gadget *gadget)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001821{
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001822 eth_reset_config(get_gadget_data(gadget));
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001823 /* FIXME RNDIS should enter RNDIS_UNINITIALIZED */
Remy Bohmer23cd1382009-07-29 18:18:43 +02001824}
1825
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001826static void eth_suspend(struct usb_gadget *gadget)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001827{
1828 /* Not used */
1829}
1830
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001831static void eth_resume(struct usb_gadget *gadget)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001832{
1833 /* Not used */
1834}
1835
1836/*-------------------------------------------------------------------------*/
1837
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001838#ifdef CONFIG_USB_ETH_RNDIS
1839
1840/*
1841 * The interrupt endpoint is used in RNDIS to notify the host when messages
1842 * other than data packets are available ... notably the REMOTE_NDIS_*_CMPLT
1843 * messages, but also REMOTE_NDIS_INDICATE_STATUS_MSG and potentially even
1844 * REMOTE_NDIS_KEEPALIVE_MSG.
1845 *
1846 * The RNDIS control queue is processed by GET_ENCAPSULATED_RESPONSE, and
1847 * normally just one notification will be queued.
1848 */
1849
1850static void rndis_control_ack_complete(struct usb_ep *ep,
1851 struct usb_request *req)
1852{
1853 struct eth_dev *dev = ep->driver_data;
1854
1855 debug("%s...\n", __func__);
1856 if (req->status || req->actual != req->length)
1857 debug("rndis control ack complete --> %d, %d/%d\n",
1858 req->status, req->actual, req->length);
1859
1860 if (!l_ethdev.network_started) {
1861 if (rndis_get_state(dev->rndis_config)
1862 == RNDIS_DATA_INITIALIZED) {
1863 l_ethdev.network_started = 1;
1864 printf("USB RNDIS network up!\n");
1865 }
1866 }
1867
1868 req->context = NULL;
1869
1870 if (req != dev->stat_req)
1871 usb_ep_free_request(ep, req);
1872}
1873
1874static char rndis_resp_buf[8] __attribute__((aligned(sizeof(__le32))));
1875
1876static int rndis_control_ack(struct eth_device *net)
1877{
1878 struct eth_dev *dev = &l_ethdev;
1879 int length;
1880 struct usb_request *resp = dev->stat_req;
1881
1882 /* in case RNDIS calls this after disconnect */
1883 if (!dev->status) {
1884 debug("status ENODEV\n");
1885 return -ENODEV;
1886 }
1887
1888 /* in case queue length > 1 */
1889 if (resp->context) {
1890 resp = usb_ep_alloc_request(dev->status_ep, GFP_ATOMIC);
1891 if (!resp)
1892 return -ENOMEM;
1893 resp->buf = rndis_resp_buf;
1894 }
1895
1896 /*
1897 * Send RNDIS RESPONSE_AVAILABLE notification;
1898 * USB_CDC_NOTIFY_RESPONSE_AVAILABLE should work too
1899 */
1900 resp->length = 8;
1901 resp->complete = rndis_control_ack_complete;
1902 resp->context = dev;
1903
1904 *((__le32 *) resp->buf) = __constant_cpu_to_le32(1);
1905 *((__le32 *) (resp->buf + 4)) = __constant_cpu_to_le32(0);
1906
1907 length = usb_ep_queue(dev->status_ep, resp, GFP_ATOMIC);
1908 if (length < 0) {
1909 resp->status = 0;
1910 rndis_control_ack_complete(dev->status_ep, resp);
1911 }
1912
1913 return 0;
1914}
1915
1916#else
1917
1918#define rndis_control_ack NULL
1919
1920#endif /* RNDIS */
1921
1922static void eth_start(struct eth_dev *dev, gfp_t gfp_flags)
1923{
1924 if (rndis_active(dev)) {
1925 rndis_set_param_medium(dev->rndis_config,
1926 NDIS_MEDIUM_802_3,
1927 BITRATE(dev->gadget)/100);
1928 rndis_signal_connect(dev->rndis_config);
1929 }
1930}
1931
1932static int eth_stop(struct eth_dev *dev)
1933{
Vitaly Kuzmicheve4ae6662011-02-11 18:18:35 +03001934#ifdef RNDIS_COMPLETE_SIGNAL_DISCONNECT
1935 unsigned long ts;
1936 unsigned long timeout = CONFIG_SYS_HZ; /* 1 sec to stop RNDIS */
1937#endif
1938
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001939 if (rndis_active(dev)) {
1940 rndis_set_param_medium(dev->rndis_config, NDIS_MEDIUM_802_3, 0);
1941 rndis_signal_disconnect(dev->rndis_config);
1942
Vitaly Kuzmicheve4ae6662011-02-11 18:18:35 +03001943#ifdef RNDIS_COMPLETE_SIGNAL_DISCONNECT
1944 /* Wait until host receives OID_GEN_MEDIA_CONNECT_STATUS */
1945 ts = get_timer(0);
1946 while (get_timer(ts) < timeout)
1947 usb_gadget_handle_interrupts();
1948#endif
1949
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001950 rndis_uninit(dev->rndis_config);
1951 dev->rndis = 0;
1952 }
1953
1954 return 0;
1955}
1956
1957/*-------------------------------------------------------------------------*/
1958
Remy Bohmer23cd1382009-07-29 18:18:43 +02001959static int is_eth_addr_valid(char *str)
1960{
1961 if (strlen(str) == 17) {
1962 int i;
1963 char *p, *q;
1964 uchar ea[6];
1965
1966 /* see if it looks like an ethernet address */
1967
1968 p = str;
1969
1970 for (i = 0; i < 6; i++) {
1971 char term = (i == 5 ? '\0' : ':');
1972
1973 ea[i] = simple_strtol(p, &q, 16);
1974
1975 if ((q - p) != 2 || *q++ != term)
1976 break;
1977
1978 p = q;
1979 }
1980
1981 if (i == 6) /* it looks ok */
1982 return 1;
1983 }
1984 return 0;
1985}
1986
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001987static u8 nibble(unsigned char c)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001988{
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001989 if (likely(isdigit(c)))
Remy Bohmer23cd1382009-07-29 18:18:43 +02001990 return c - '0';
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001991 c = toupper(c);
1992 if (likely(isxdigit(c)))
Remy Bohmer23cd1382009-07-29 18:18:43 +02001993 return 10 + c - 'A';
1994 return 0;
1995}
1996
1997static int get_ether_addr(const char *str, u8 *dev_addr)
1998{
1999 if (str) {
2000 unsigned i;
2001
2002 for (i = 0; i < 6; i++) {
2003 unsigned char num;
2004
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002005 if ((*str == '.') || (*str == ':'))
Remy Bohmer23cd1382009-07-29 18:18:43 +02002006 str++;
2007 num = nibble(*str++) << 4;
2008 num |= (nibble(*str++));
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002009 dev_addr[i] = num;
Remy Bohmer23cd1382009-07-29 18:18:43 +02002010 }
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002011 if (is_valid_ether_addr(dev_addr))
Remy Bohmer23cd1382009-07-29 18:18:43 +02002012 return 0;
2013 }
2014 return 1;
2015}
2016
2017static int eth_bind(struct usb_gadget *gadget)
2018{
2019 struct eth_dev *dev = &l_ethdev;
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002020 u8 cdc = 1, zlp = 1, rndis = 1;
Remy Bohmer23cd1382009-07-29 18:18:43 +02002021 struct usb_ep *in_ep, *out_ep, *status_ep = NULL;
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002022 int status = -ENOMEM;
Remy Bohmer23cd1382009-07-29 18:18:43 +02002023 int gcnum;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002024 u8 tmp[7];
Remy Bohmer23cd1382009-07-29 18:18:43 +02002025
2026 /* these flags are only ever cleared; compiler take note */
Lukasz Dalek2bb37882012-10-02 17:04:31 +02002027#ifndef CONFIG_USB_ETH_CDC
Remy Bohmer23cd1382009-07-29 18:18:43 +02002028 cdc = 0;
2029#endif
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002030#ifndef CONFIG_USB_ETH_RNDIS
2031 rndis = 0;
2032#endif
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002033 /*
2034 * Because most host side USB stacks handle CDC Ethernet, that
Remy Bohmer23cd1382009-07-29 18:18:43 +02002035 * standard protocol is _strongly_ preferred for interop purposes.
2036 * (By everyone except Microsoft.)
2037 */
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002038 if (gadget_is_pxa(gadget)) {
Remy Bohmer23cd1382009-07-29 18:18:43 +02002039 /* pxa doesn't support altsettings */
2040 cdc = 0;
2041 } else if (gadget_is_musbhdrc(gadget)) {
2042 /* reduce tx dma overhead by avoiding special cases */
2043 zlp = 0;
2044 } else if (gadget_is_sh(gadget)) {
2045 /* sh doesn't support multiple interfaces or configs */
2046 cdc = 0;
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002047 rndis = 0;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002048 } else if (gadget_is_sa1100(gadget)) {
Remy Bohmer23cd1382009-07-29 18:18:43 +02002049 /* hardware can't write zlps */
2050 zlp = 0;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002051 /*
2052 * sa1100 CAN do CDC, without status endpoint ... we use
Remy Bohmer23cd1382009-07-29 18:18:43 +02002053 * non-CDC to be compatible with ARM Linux-2.4 "usb-eth".
2054 */
2055 cdc = 0;
2056 }
2057
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002058 gcnum = usb_gadget_controller_number(gadget);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002059 if (gcnum >= 0)
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002060 device_desc.bcdDevice = cpu_to_le16(0x0300 + gcnum);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002061 else {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002062 /*
2063 * can't assume CDC works. don't want to default to
Remy Bohmer23cd1382009-07-29 18:18:43 +02002064 * anything less functional on CDC-capable hardware,
2065 * so we fail in this case.
2066 */
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04002067 error("controller '%s' not recognized",
Remy Bohmer23cd1382009-07-29 18:18:43 +02002068 gadget->name);
2069 return -ENODEV;
2070 }
2071
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002072 /*
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002073 * If there's an RNDIS configuration, that's what Windows wants to
2074 * be using ... so use these product IDs here and in the "linux.inf"
2075 * needed to install MSFT drivers. Current Linux kernels will use
2076 * the second configuration if it's CDC Ethernet, and need some help
2077 * to choose the right configuration otherwise.
2078 */
2079 if (rndis) {
2080#if defined(CONFIG_USB_RNDIS_VENDOR_ID) && defined(CONFIG_USB_RNDIS_PRODUCT_ID)
2081 device_desc.idVendor =
2082 __constant_cpu_to_le16(CONFIG_USB_RNDIS_VENDOR_ID);
2083 device_desc.idProduct =
2084 __constant_cpu_to_le16(CONFIG_USB_RNDIS_PRODUCT_ID);
2085#else
2086 device_desc.idVendor =
2087 __constant_cpu_to_le16(RNDIS_VENDOR_NUM);
2088 device_desc.idProduct =
2089 __constant_cpu_to_le16(RNDIS_PRODUCT_NUM);
2090#endif
2091 sprintf(product_desc, "RNDIS/%s", driver_desc);
2092
2093 /*
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002094 * CDC subset ... recognized by Linux since 2.4.10, but Windows
Remy Bohmer23cd1382009-07-29 18:18:43 +02002095 * drivers aren't widely available. (That may be improved by
2096 * supporting one submode of the "SAFE" variant of MDLM.)
2097 */
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002098 } else {
Remy Bohmer23cd1382009-07-29 18:18:43 +02002099#if defined(CONFIG_USB_CDC_VENDOR_ID) && defined(CONFIG_USB_CDC_PRODUCT_ID)
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002100 device_desc.idVendor = cpu_to_le16(CONFIG_USB_CDC_VENDOR_ID);
2101 device_desc.idProduct = cpu_to_le16(CONFIG_USB_CDC_PRODUCT_ID);
2102#else
2103 if (!cdc) {
2104 device_desc.idVendor =
2105 __constant_cpu_to_le16(SIMPLE_VENDOR_NUM);
2106 device_desc.idProduct =
2107 __constant_cpu_to_le16(SIMPLE_PRODUCT_NUM);
2108 }
Remy Bohmer23cd1382009-07-29 18:18:43 +02002109#endif
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002110 }
2111 /* support optional vendor/distro customization */
Remy Bohmer23cd1382009-07-29 18:18:43 +02002112 if (bcdDevice)
2113 device_desc.bcdDevice = cpu_to_le16(bcdDevice);
2114 if (iManufacturer)
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002115 strlcpy(manufacturer, iManufacturer, sizeof manufacturer);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002116 if (iProduct)
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002117 strlcpy(product_desc, iProduct, sizeof product_desc);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002118 if (iSerialNumber) {
2119 device_desc.iSerialNumber = STRING_SERIALNUMBER,
Vitaly Kuzmichev2e12abe2010-08-13 17:00:45 +04002120 strlcpy(serial_number, iSerialNumber, sizeof serial_number);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002121 }
2122
2123 /* all we really need is bulk IN/OUT */
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002124 usb_ep_autoconfig_reset(gadget);
2125 in_ep = usb_ep_autoconfig(gadget, &fs_source_desc);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002126 if (!in_ep) {
2127autoconf_fail:
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04002128 error("can't autoconfigure on %s\n",
Remy Bohmer23cd1382009-07-29 18:18:43 +02002129 gadget->name);
2130 return -ENODEV;
2131 }
2132 in_ep->driver_data = in_ep; /* claim */
2133
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002134 out_ep = usb_ep_autoconfig(gadget, &fs_sink_desc);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002135 if (!out_ep)
2136 goto autoconf_fail;
2137 out_ep->driver_data = out_ep; /* claim */
2138
Lukasz Dalek2bb37882012-10-02 17:04:31 +02002139#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002140 /*
2141 * CDC Ethernet control interface doesn't require a status endpoint.
Remy Bohmer23cd1382009-07-29 18:18:43 +02002142 * Since some hosts expect one, try to allocate one anyway.
2143 */
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002144 if (cdc || rndis) {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002145 status_ep = usb_ep_autoconfig(gadget, &fs_status_desc);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002146 if (status_ep) {
2147 status_ep->driver_data = status_ep; /* claim */
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002148 } else if (rndis) {
2149 error("can't run RNDIS on %s", gadget->name);
2150 return -ENODEV;
Lukasz Dalek2bb37882012-10-02 17:04:31 +02002151#ifdef CONFIG_USB_ETH_CDC
Remy Bohmer23cd1382009-07-29 18:18:43 +02002152 } else if (cdc) {
2153 control_intf.bNumEndpoints = 0;
2154 /* FIXME remove endpoint from descriptor list */
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002155#endif
Remy Bohmer23cd1382009-07-29 18:18:43 +02002156 }
2157 }
2158#endif
2159
2160 /* one config: cdc, else minimal subset */
2161 if (!cdc) {
2162 eth_config.bNumInterfaces = 1;
2163 eth_config.iConfiguration = STRING_SUBSET;
2164
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002165 /*
2166 * use functions to set these up, in case we're built to work
Remy Bohmer23cd1382009-07-29 18:18:43 +02002167 * with multiple controllers and must override CDC Ethernet.
2168 */
2169 fs_subset_descriptors();
2170 hs_subset_descriptors();
2171 }
2172
2173 device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002174 usb_gadget_set_selfpowered(gadget);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002175
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002176 /* For now RNDIS is always a second config */
2177 if (rndis)
2178 device_desc.bNumConfigurations = 2;
2179
Remy Bohmer23cd1382009-07-29 18:18:43 +02002180 if (gadget_is_dualspeed(gadget)) {
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002181 if (rndis)
2182 dev_qualifier.bNumConfigurations = 2;
2183 else if (!cdc)
Remy Bohmer23cd1382009-07-29 18:18:43 +02002184 dev_qualifier.bDeviceClass = USB_CLASS_VENDOR_SPEC;
2185
2186 /* assumes ep0 uses the same value for both speeds ... */
2187 dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0;
2188
2189 /* and that all endpoints are dual-speed */
2190 hs_source_desc.bEndpointAddress =
2191 fs_source_desc.bEndpointAddress;
2192 hs_sink_desc.bEndpointAddress =
2193 fs_sink_desc.bEndpointAddress;
Lukasz Dalek2bb37882012-10-02 17:04:31 +02002194#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Remy Bohmer23cd1382009-07-29 18:18:43 +02002195 if (status_ep)
2196 hs_status_desc.bEndpointAddress =
2197 fs_status_desc.bEndpointAddress;
2198#endif
2199 }
2200
2201 if (gadget_is_otg(gadget)) {
2202 otg_descriptor.bmAttributes |= USB_OTG_HNP,
2203 eth_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
2204 eth_config.bMaxPower = 4;
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002205#ifdef CONFIG_USB_ETH_RNDIS
2206 rndis_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
2207 rndis_config.bMaxPower = 4;
2208#endif
Remy Bohmer23cd1382009-07-29 18:18:43 +02002209 }
2210
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002211
2212 /* network device setup */
Remy Bohmer23cd1382009-07-29 18:18:43 +02002213 dev->net = &l_netdev;
Remy Bohmer23cd1382009-07-29 18:18:43 +02002214
2215 dev->cdc = cdc;
2216 dev->zlp = zlp;
2217
2218 dev->in_ep = in_ep;
2219 dev->out_ep = out_ep;
2220 dev->status_ep = status_ep;
2221
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002222 /*
2223 * Module params for these addresses should come from ID proms.
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002224 * The host side address is used with CDC and RNDIS, and commonly
Remy Bohmer23cd1382009-07-29 18:18:43 +02002225 * ends up in a persistent config database. It's not clear if
2226 * host side code for the SAFE thing cares -- its original BLAN
2227 * thing didn't, Sharp never assigned those addresses on Zaurii.
2228 */
2229 get_ether_addr(dev_addr, dev->net->enetaddr);
2230
2231 memset(tmp, 0, sizeof(tmp));
2232 memcpy(tmp, dev->net->enetaddr, sizeof(dev->net->enetaddr));
2233
2234 get_ether_addr(host_addr, dev->host_mac);
2235
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002236 sprintf(ethaddr, "%02X%02X%02X%02X%02X%02X",
2237 dev->host_mac[0], dev->host_mac[1],
2238 dev->host_mac[2], dev->host_mac[3],
2239 dev->host_mac[4], dev->host_mac[5]);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002240
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002241 if (rndis) {
2242 status = rndis_init();
2243 if (status < 0) {
2244 error("can't init RNDIS, %d", status);
2245 goto fail;
2246 }
Remy Bohmer23cd1382009-07-29 18:18:43 +02002247 }
2248
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002249 /*
2250 * use PKTSIZE (or aligned... from u-boot) and set
2251 * wMaxSegmentSize accordingly
2252 */
Remy Bohmer23cd1382009-07-29 18:18:43 +02002253 dev->mtu = PKTSIZE_ALIGN; /* RNDIS does not like this, only 1514, TODO*/
2254
2255 /* preallocate control message data and buffer */
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002256 dev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002257 if (!dev->req)
2258 goto fail;
2259 dev->req->buf = control_req;
2260 dev->req->complete = eth_setup_complete;
2261
2262 /* ... and maybe likewise for status transfer */
Lukasz Dalek2bb37882012-10-02 17:04:31 +02002263#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Remy Bohmer23cd1382009-07-29 18:18:43 +02002264 if (dev->status_ep) {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002265 dev->stat_req = usb_ep_alloc_request(dev->status_ep,
2266 GFP_KERNEL);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002267 if (!dev->stat_req) {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002268 usb_ep_free_request(dev->status_ep, dev->req);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002269
2270 goto fail;
2271 }
Vitaly Kuzmichevdf559c12010-08-13 17:01:06 +04002272 dev->stat_req->buf = status_req;
Remy Bohmer23cd1382009-07-29 18:18:43 +02002273 dev->stat_req->context = NULL;
2274 }
2275#endif
2276
2277 /* finish hookup to lower layer ... */
2278 dev->gadget = gadget;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002279 set_gadget_data(gadget, dev);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002280 gadget->ep0->driver_data = dev;
2281
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002282 /*
2283 * two kinds of host-initiated state changes:
Remy Bohmer23cd1382009-07-29 18:18:43 +02002284 * - iff DATA transfer is active, carrier is "on"
2285 * - tx queueing enabled if open *and* carrier is "on"
2286 */
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002287
2288 printf("using %s, OUT %s IN %s%s%s\n", gadget->name,
2289 out_ep->name, in_ep->name,
2290 status_ep ? " STATUS " : "",
2291 status_ep ? status_ep->name : ""
2292 );
2293 printf("MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
2294 dev->net->enetaddr[0], dev->net->enetaddr[1],
2295 dev->net->enetaddr[2], dev->net->enetaddr[3],
2296 dev->net->enetaddr[4], dev->net->enetaddr[5]);
2297
2298 if (cdc || rndis)
2299 printf("HOST MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
2300 dev->host_mac[0], dev->host_mac[1],
2301 dev->host_mac[2], dev->host_mac[3],
2302 dev->host_mac[4], dev->host_mac[5]);
2303
2304 if (rndis) {
2305 u32 vendorID = 0;
2306
2307 /* FIXME RNDIS vendor id == "vendor NIC code" == ? */
2308
2309 dev->rndis_config = rndis_register(rndis_control_ack);
2310 if (dev->rndis_config < 0) {
2311fail0:
2312 eth_unbind(gadget);
2313 debug("RNDIS setup failed\n");
2314 status = -ENODEV;
2315 goto fail;
2316 }
2317
2318 /* these set up a lot of the OIDs that RNDIS needs */
2319 rndis_set_host_mac(dev->rndis_config, dev->host_mac);
2320 if (rndis_set_param_dev(dev->rndis_config, dev->net, dev->mtu,
2321 &dev->stats, &dev->cdc_filter))
2322 goto fail0;
2323 if (rndis_set_param_vendor(dev->rndis_config, vendorID,
2324 manufacturer))
2325 goto fail0;
2326 if (rndis_set_param_medium(dev->rndis_config,
2327 NDIS_MEDIUM_802_3, 0))
2328 goto fail0;
2329 printf("RNDIS ready\n");
2330 }
Remy Bohmer23cd1382009-07-29 18:18:43 +02002331 return 0;
2332
2333fail:
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002334 error("%s failed, status = %d", __func__, status);
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002335 eth_unbind(gadget);
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002336 return status;
Remy Bohmer23cd1382009-07-29 18:18:43 +02002337}
2338
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002339/*-------------------------------------------------------------------------*/
2340
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002341static int usb_eth_init(struct eth_device *netdev, bd_t *bd)
Remy Bohmer23cd1382009-07-29 18:18:43 +02002342{
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002343 struct eth_dev *dev = &l_ethdev;
Remy Bohmer23cd1382009-07-29 18:18:43 +02002344 struct usb_gadget *gadget;
2345 unsigned long ts;
2346 unsigned long timeout = USB_CONNECT_TIMEOUT;
2347
2348 if (!netdev) {
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04002349 error("received NULL ptr");
Remy Bohmer23cd1382009-07-29 18:18:43 +02002350 goto fail;
2351 }
Vitaly Kuzmichev58939fc2010-12-28 16:59:32 +03002352
2353 /* Configure default mac-addresses for the USB ethernet device */
2354#ifdef CONFIG_USBNET_DEV_ADDR
2355 strlcpy(dev_addr, CONFIG_USBNET_DEV_ADDR, sizeof(dev_addr));
2356#endif
2357#ifdef CONFIG_USBNET_HOST_ADDR
2358 strlcpy(host_addr, CONFIG_USBNET_HOST_ADDR, sizeof(host_addr));
2359#endif
2360 /* Check if the user overruled the MAC addresses */
2361 if (getenv("usbnet_devaddr"))
2362 strlcpy(dev_addr, getenv("usbnet_devaddr"),
2363 sizeof(dev_addr));
2364
2365 if (getenv("usbnet_hostaddr"))
2366 strlcpy(host_addr, getenv("usbnet_hostaddr"),
2367 sizeof(host_addr));
2368
2369 if (!is_eth_addr_valid(dev_addr)) {
2370 error("Need valid 'usbnet_devaddr' to be set");
2371 goto fail;
2372 }
2373 if (!is_eth_addr_valid(host_addr)) {
2374 error("Need valid 'usbnet_hostaddr' to be set");
2375 goto fail;
2376 }
2377
Lei Wen988ee3e2010-12-01 23:43:43 +08002378 if (usb_gadget_register_driver(&eth_driver) < 0)
2379 goto fail;
Remy Bohmer23cd1382009-07-29 18:18:43 +02002380
2381 dev->network_started = 0;
Remy Bohmer23cd1382009-07-29 18:18:43 +02002382
2383 packet_received = 0;
2384 packet_sent = 0;
2385
2386 gadget = dev->gadget;
2387 usb_gadget_connect(gadget);
2388
2389 if (getenv("cdc_connect_timeout"))
2390 timeout = simple_strtoul(getenv("cdc_connect_timeout"),
2391 NULL, 10) * CONFIG_SYS_HZ;
2392 ts = get_timer(0);
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002393 while (!l_ethdev.network_started) {
Remy Bohmer23cd1382009-07-29 18:18:43 +02002394 /* Handle control-c and timeouts */
2395 if (ctrlc() || (get_timer(ts) > timeout)) {
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04002396 error("The remote end did not respond in time.");
Remy Bohmer23cd1382009-07-29 18:18:43 +02002397 goto fail;
2398 }
2399 usb_gadget_handle_interrupts();
2400 }
2401
Vitaly Kuzmichev98fae972010-09-22 13:13:56 +04002402 packet_received = 0;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002403 rx_submit(dev, dev->rx_req, 0);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002404 return 0;
2405fail:
2406 return -1;
2407}
2408
Joe Hershberger10cbe3b2012-05-22 18:36:19 +00002409static int usb_eth_send(struct eth_device *netdev, void *packet, int length)
Remy Bohmer23cd1382009-07-29 18:18:43 +02002410{
2411 int retval;
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002412 void *rndis_pkt = NULL;
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04002413 struct eth_dev *dev = &l_ethdev;
Vitaly Kuzmichevac5d32d2010-09-22 13:13:55 +04002414 struct usb_request *req = dev->tx_req;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002415 unsigned long ts;
2416 unsigned long timeout = USB_CONNECT_TIMEOUT;
Remy Bohmer23cd1382009-07-29 18:18:43 +02002417
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04002418 debug("%s:...\n", __func__);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002419
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002420 /* new buffer is needed to include RNDIS header */
2421 if (rndis_active(dev)) {
2422 rndis_pkt = malloc(length +
2423 sizeof(struct rndis_packet_msg_type));
2424 if (!rndis_pkt) {
2425 error("No memory to alloc RNDIS packet");
2426 goto drop;
2427 }
2428 rndis_add_hdr(rndis_pkt, length);
2429 memcpy(rndis_pkt + sizeof(struct rndis_packet_msg_type),
Joe Hershberger10cbe3b2012-05-22 18:36:19 +00002430 packet, length);
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002431 packet = rndis_pkt;
2432 length += sizeof(struct rndis_packet_msg_type);
2433 }
Joe Hershberger10cbe3b2012-05-22 18:36:19 +00002434 req->buf = packet;
Remy Bohmer23cd1382009-07-29 18:18:43 +02002435 req->context = NULL;
2436 req->complete = tx_complete;
2437
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002438 /*
2439 * use zlp framing on tx for strict CDC-Ether conformance,
Remy Bohmer23cd1382009-07-29 18:18:43 +02002440 * though any robust network rx path ignores extra padding.
2441 * and some hardware doesn't like to write zlps.
2442 */
2443 req->zero = 1;
2444 if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0)
2445 length++;
2446
2447 req->length = length;
2448#if 0
2449 /* throttle highspeed IRQ rate back slightly */
2450 if (gadget_is_dualspeed(dev->gadget))
2451 req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH)
2452 ? ((dev->tx_qlen % qmult) != 0) : 0;
2453#endif
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002454 dev->tx_qlen = 1;
2455 ts = get_timer(0);
2456 packet_sent = 0;
Remy Bohmer23cd1382009-07-29 18:18:43 +02002457
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002458 retval = usb_ep_queue(dev->in_ep, req, GFP_ATOMIC);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002459
2460 if (!retval)
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04002461 debug("%s: packet queued\n", __func__);
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002462 while (!packet_sent) {
2463 if (get_timer(ts) > timeout) {
2464 printf("timeout sending packets to usb ethernet\n");
2465 return -1;
2466 }
2467 usb_gadget_handle_interrupts();
Remy Bohmer23cd1382009-07-29 18:18:43 +02002468 }
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002469 if (rndis_pkt)
2470 free(rndis_pkt);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002471
2472 return 0;
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002473drop:
2474 dev->stats.tx_dropped++;
2475 return -ENOMEM;
Remy Bohmer23cd1382009-07-29 18:18:43 +02002476}
2477
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002478static int usb_eth_recv(struct eth_device *netdev)
Remy Bohmer23cd1382009-07-29 18:18:43 +02002479{
2480 struct eth_dev *dev = &l_ethdev;
2481
2482 usb_gadget_handle_interrupts();
2483
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002484 if (packet_received) {
2485 debug("%s: packet received\n", __func__);
2486 if (dev->rx_req) {
2487 NetReceive(NetRxPackets[0], dev->rx_req->length);
2488 packet_received = 0;
Remy Bohmer23cd1382009-07-29 18:18:43 +02002489
Vitaly Kuzmichevac5d32d2010-09-22 13:13:55 +04002490 rx_submit(dev, dev->rx_req, 0);
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002491 } else
2492 error("dev->rx_req invalid");
Remy Bohmer23cd1382009-07-29 18:18:43 +02002493 }
2494 return 0;
2495}
2496
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002497void usb_eth_halt(struct eth_device *netdev)
Remy Bohmer23cd1382009-07-29 18:18:43 +02002498{
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002499 struct eth_dev *dev = &l_ethdev;
Remy Bohmer23cd1382009-07-29 18:18:43 +02002500
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002501 if (!netdev) {
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04002502 error("received NULL ptr");
Remy Bohmer23cd1382009-07-29 18:18:43 +02002503 return;
2504 }
2505
Lei Wen988ee3e2010-12-01 23:43:43 +08002506 /* If the gadget not registered, simple return */
2507 if (!dev->gadget)
2508 return;
2509
Vitaly Kuzmicheve4ae6662011-02-11 18:18:35 +03002510 /*
2511 * Some USB controllers may need additional deinitialization here
2512 * before dropping pull-up (also due to hardware issues).
2513 * For example: unhandled interrupt with status stage started may
2514 * bring the controller to fully broken state (until board reset).
2515 * There are some variants to debug and fix such cases:
2516 * 1) In the case of RNDIS connection eth_stop can perform additional
2517 * interrupt handling. See RNDIS_COMPLETE_SIGNAL_DISCONNECT definition.
2518 * 2) 'pullup' callback in your UDC driver can be improved to perform
2519 * this deinitialization.
2520 */
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002521 eth_stop(dev);
2522
Remy Bohmer23cd1382009-07-29 18:18:43 +02002523 usb_gadget_disconnect(dev->gadget);
Vitaly Kuzmichevb3649f32011-02-11 18:18:31 +03002524
2525 /* Clear pending interrupt */
2526 if (dev->network_started) {
2527 usb_gadget_handle_interrupts();
2528 dev->network_started = 0;
2529 }
2530
Lei Wen988ee3e2010-12-01 23:43:43 +08002531 usb_gadget_unregister_driver(&eth_driver);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002532}
2533
2534static struct usb_gadget_driver eth_driver = {
2535 .speed = DEVSPEED,
2536
2537 .bind = eth_bind,
2538 .unbind = eth_unbind,
2539
2540 .setup = eth_setup,
2541 .disconnect = eth_disconnect,
2542
2543 .suspend = eth_suspend,
2544 .resume = eth_resume,
2545};
2546
2547int usb_eth_initialize(bd_t *bi)
2548{
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002549 struct eth_device *netdev = &l_netdev;
Remy Bohmer23cd1382009-07-29 18:18:43 +02002550
Vitaly Kuzmichev8f7aa832010-12-28 16:59:31 +03002551 strlcpy(netdev->name, USB_NET_NAME, sizeof(netdev->name));
Remy Bohmer23cd1382009-07-29 18:18:43 +02002552
2553 netdev->init = usb_eth_init;
2554 netdev->send = usb_eth_send;
2555 netdev->recv = usb_eth_recv;
2556 netdev->halt = usb_eth_halt;
2557
2558#ifdef CONFIG_MCAST_TFTP
2559 #error not supported
2560#endif
Remy Bohmer23cd1382009-07-29 18:18:43 +02002561 eth_register(netdev);
2562 return 0;
Remy Bohmer23cd1382009-07-29 18:18:43 +02002563}