blob: 700d5fbfb2ffa4f77faf24fea40bae5b5e32b486 [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 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02008 * SPDX-License-Identifier: GPL-2.0+
Remy Bohmer23cd1382009-07-29 18:18:43 +02009 */
10
11#include <common.h>
12#include <asm/errno.h>
Vitaly Kuzmichevc85d70e2011-02-11 18:18:32 +030013#include <linux/netdevice.h>
Remy Bohmer23cd1382009-07-29 18:18:43 +020014#include <linux/usb/ch9.h>
15#include <linux/usb/cdc.h>
16#include <linux/usb/gadget.h>
17#include <net.h>
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +030018#include <malloc.h>
Remy Bohmer23cd1382009-07-29 18:18:43 +020019#include <linux/ctype.h>
20
21#include "gadget_chips.h"
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +030022#include "rndis.h"
Remy Bohmer23cd1382009-07-29 18:18:43 +020023
Vitaly Kuzmichev8f7aa832010-12-28 16:59:31 +030024#define USB_NET_NAME "usb_ether"
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +040025
Remy Bohmer23cd1382009-07-29 18:18:43 +020026#define atomic_read
27extern struct platform_data brd;
28#define spin_lock(x)
29#define spin_unlock(x)
30
31
32unsigned packet_received, packet_sent;
33
Remy Bohmer23cd1382009-07-29 18:18:43 +020034#define GFP_ATOMIC ((gfp_t) 0)
35#define GFP_KERNEL ((gfp_t) 0)
36
37/*
38 * Ethernet gadget driver -- with CDC and non-CDC options
39 * Builds on hardware support for a full duplex link.
40 *
41 * CDC Ethernet is the standard USB solution for sending Ethernet frames
42 * using USB. Real hardware tends to use the same framing protocol but look
43 * different for control features. This driver strongly prefers to use
44 * this USB-IF standard as its open-systems interoperability solution;
45 * most host side USB stacks (except from Microsoft) support it.
46 *
47 * This is sometimes called "CDC ECM" (Ethernet Control Model) to support
48 * TLA-soup. "CDC ACM" (Abstract Control Model) is for modems, and a new
49 * "CDC EEM" (Ethernet Emulation Model) is starting to spread.
50 *
51 * There's some hardware that can't talk CDC ECM. We make that hardware
52 * implement a "minimalist" vendor-agnostic CDC core: same framing, but
53 * link-level setup only requires activating the configuration. Only the
54 * endpoint descriptors, and product/vendor IDs, are relevant; no control
55 * operations are available. Linux supports it, but other host operating
56 * systems may not. (This is a subset of CDC Ethernet.)
57 *
58 * It turns out that if you add a few descriptors to that "CDC Subset",
59 * (Windows) host side drivers from MCCI can treat it as one submode of
60 * a proprietary scheme called "SAFE" ... without needing to know about
61 * specific product/vendor IDs. So we do that, making it easier to use
62 * those MS-Windows drivers. Those added descriptors make it resemble a
63 * CDC MDLM device, but they don't change device behavior at all. (See
64 * MCCI Engineering report 950198 "SAFE Networking Functions".)
65 *
66 * A third option is also in use. Rather than CDC Ethernet, or something
67 * simpler, Microsoft pushes their own approach: RNDIS. The published
68 * RNDIS specs are ambiguous and appear to be incomplete, and are also
69 * needlessly complex. They borrow more from CDC ACM than CDC ECM.
70 */
71#define ETH_ALEN 6 /* Octets in one ethernet addr */
72#define ETH_HLEN 14 /* Total octets in header. */
73#define ETH_ZLEN 60 /* Min. octets in frame sans FCS */
74#define ETH_DATA_LEN 1500 /* Max. octets in payload */
75#define ETH_FRAME_LEN PKTSIZE_ALIGN /* Max. octets in frame sans FCS */
76#define ETH_FCS_LEN 4 /* Octets in the FCS */
77
78#define DRIVER_DESC "Ethernet Gadget"
79/* Based on linux 2.6.27 version */
80#define DRIVER_VERSION "May Day 2005"
81
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +040082static const char shortname[] = "ether";
83static const char driver_desc[] = DRIVER_DESC;
Remy Bohmer23cd1382009-07-29 18:18:43 +020084
85#define RX_EXTRA 20 /* guard against rx overflows */
86
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +030087#ifndef CONFIG_USB_ETH_RNDIS
88#define rndis_uninit(x) do {} while (0)
89#define rndis_deregister(c) do {} while (0)
90#define rndis_exit() do {} while (0)
91#endif
92
93/* CDC and RNDIS support the same host-chosen outgoing packet filters. */
Remy Bohmer23cd1382009-07-29 18:18:43 +020094#define DEFAULT_FILTER (USB_CDC_PACKET_TYPE_BROADCAST \
95 |USB_CDC_PACKET_TYPE_ALL_MULTICAST \
96 |USB_CDC_PACKET_TYPE_PROMISCUOUS \
97 |USB_CDC_PACKET_TYPE_DIRECTED)
98
99#define USB_CONNECT_TIMEOUT (3 * CONFIG_SYS_HZ)
100
101/*-------------------------------------------------------------------------*/
Vitaly Kuzmichev8b6b66b2011-02-11 18:18:33 +0300102
103struct eth_dev {
104 struct usb_gadget *gadget;
105 struct usb_request *req; /* for control responses */
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300106 struct usb_request *stat_req; /* for cdc & rndis status */
Vitaly Kuzmichev8b6b66b2011-02-11 18:18:33 +0300107
108 u8 config;
109 struct usb_ep *in_ep, *out_ep, *status_ep;
110 const struct usb_endpoint_descriptor
111 *in, *out, *status;
112
113 struct usb_request *tx_req, *rx_req;
114
115 struct eth_device *net;
116 struct net_device_stats stats;
117 unsigned int tx_qlen;
118
119 unsigned zlp:1;
120 unsigned cdc:1;
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300121 unsigned rndis:1;
Vitaly Kuzmichev8b6b66b2011-02-11 18:18:33 +0300122 unsigned suspended:1;
123 unsigned network_started:1;
124 u16 cdc_filter;
125 unsigned long todo;
126 int mtu;
127#define WORK_RX_MEMORY 0
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300128 int rndis_config;
Vitaly Kuzmichev8b6b66b2011-02-11 18:18:33 +0300129 u8 host_mac[ETH_ALEN];
130};
131
132/*
133 * This version autoconfigures as much as possible at run-time.
134 *
135 * It also ASSUMES a self-powered device, without remote wakeup,
136 * although remote wakeup support would make sense.
137 */
138
139/*-------------------------------------------------------------------------*/
Remy Bohmer23cd1382009-07-29 18:18:43 +0200140static struct eth_dev l_ethdev;
141static struct eth_device l_netdev;
142static struct usb_gadget_driver eth_driver;
143
144/*-------------------------------------------------------------------------*/
145
146/* "main" config is either CDC, or its simple subset */
147static inline int is_cdc(struct eth_dev *dev)
148{
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200149#if !defined(CONFIG_USB_ETH_SUBSET)
Remy Bohmer23cd1382009-07-29 18:18:43 +0200150 return 1; /* only cdc possible */
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200151#elif !defined(CONFIG_USB_ETH_CDC)
Remy Bohmer23cd1382009-07-29 18:18:43 +0200152 return 0; /* only subset possible */
153#else
154 return dev->cdc; /* depends on what hardware we found */
155#endif
156}
157
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300158/* "secondary" RNDIS config may sometimes be activated */
159static inline int rndis_active(struct eth_dev *dev)
160{
161#ifdef CONFIG_USB_ETH_RNDIS
162 return dev->rndis;
163#else
164 return 0;
165#endif
166}
167
168#define subset_active(dev) (!is_cdc(dev) && !rndis_active(dev))
169#define cdc_active(dev) (is_cdc(dev) && !rndis_active(dev))
Remy Bohmer23cd1382009-07-29 18:18:43 +0200170
171#define DEFAULT_QLEN 2 /* double buffering by default */
172
173/* peak bulk transfer bits-per-second */
174#define HS_BPS (13 * 512 * 8 * 1000 * 8)
175#define FS_BPS (19 * 64 * 1 * 1000 * 8)
176
177#ifdef CONFIG_USB_GADGET_DUALSPEED
178#define DEVSPEED USB_SPEED_HIGH
179
Vitaly Kuzmichev2721dbf2010-08-12 16:44:40 +0400180#ifdef CONFIG_USB_ETH_QMULT
181#define qmult CONFIG_USB_ETH_QMULT
182#else
183#define qmult 5
184#endif
185
Remy Bohmer23cd1382009-07-29 18:18:43 +0200186/* for dual-speed hardware, use deeper queues at highspeed */
187#define qlen(gadget) \
188 (DEFAULT_QLEN*((gadget->speed == USB_SPEED_HIGH) ? qmult : 1))
189
190static inline int BITRATE(struct usb_gadget *g)
191{
192 return (g->speed == USB_SPEED_HIGH) ? HS_BPS : FS_BPS;
193}
194
195#else /* full speed (low speed doesn't do bulk) */
196
197#define qmult 1
198
199#define DEVSPEED USB_SPEED_FULL
200
201#define qlen(gadget) DEFAULT_QLEN
202
203static inline int BITRATE(struct usb_gadget *g)
204{
205 return FS_BPS;
206}
207#endif
208
Remy Bohmer23cd1382009-07-29 18:18:43 +0200209/*-------------------------------------------------------------------------*/
210
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400211/*
212 * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
Remy Bohmer23cd1382009-07-29 18:18:43 +0200213 * Instead: allocate your own, using normal USB-IF procedures.
214 */
215
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400216/*
217 * Thanks to NetChip Technologies for donating this product ID.
Remy Bohmer23cd1382009-07-29 18:18:43 +0200218 * It's for devices with only CDC Ethernet configurations.
219 */
220#define CDC_VENDOR_NUM 0x0525 /* NetChip */
221#define CDC_PRODUCT_NUM 0xa4a1 /* Linux-USB Ethernet Gadget */
222
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400223/*
224 * For hardware that can't talk CDC, we use the same vendor ID that
Remy Bohmer23cd1382009-07-29 18:18:43 +0200225 * ARM Linux has used for ethernet-over-usb, both with sa1100 and
226 * with pxa250. We're protocol-compatible, if the host-side drivers
227 * use the endpoint descriptors. bcdDevice (version) is nonzero, so
228 * drivers that need to hard-wire endpoint numbers have a hook.
229 *
230 * The protocol is a minimal subset of CDC Ether, which works on any bulk
231 * hardware that's not deeply broken ... even on hardware that can't talk
232 * RNDIS (like SA-1100, with no interrupt endpoint, or anything that
233 * doesn't handle control-OUT).
234 */
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300235#define SIMPLE_VENDOR_NUM 0x049f /* Compaq Computer Corp. */
236#define SIMPLE_PRODUCT_NUM 0x505a /* Linux-USB "CDC Subset" Device */
237
238/*
239 * For hardware that can talk RNDIS and either of the above protocols,
240 * use this ID ... the windows INF files will know it. Unless it's
241 * used with CDC Ethernet, Linux 2.4 hosts will need updates to choose
242 * the non-RNDIS configuration.
243 */
244#define RNDIS_VENDOR_NUM 0x0525 /* NetChip */
245#define RNDIS_PRODUCT_NUM 0xa4a2 /* Ethernet/RNDIS Gadget */
Remy Bohmer23cd1382009-07-29 18:18:43 +0200246
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400247/*
248 * Some systems will want different product identifers published in the
Remy Bohmer23cd1382009-07-29 18:18:43 +0200249 * device descriptor, either numbers or strings or both. These string
250 * parameters are in UTF-8 (superset of ASCII's 7 bit characters).
251 */
252
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300253/*
254 * Emulating them in eth_bind:
255 * static ushort idVendor;
256 * static ushort idProduct;
257 */
258
Remy Bohmer23cd1382009-07-29 18:18:43 +0200259#if defined(CONFIG_USBNET_MANUFACTURER)
260static char *iManufacturer = CONFIG_USBNET_MANUFACTURER;
261#else
262static char *iManufacturer = "U-boot";
263#endif
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300264
265/* These probably need to be configurable. */
266static ushort bcdDevice;
Remy Bohmer23cd1382009-07-29 18:18:43 +0200267static char *iProduct;
268static char *iSerialNumber;
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300269
Remy Bohmer23cd1382009-07-29 18:18:43 +0200270static char dev_addr[18];
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300271
Remy Bohmer23cd1382009-07-29 18:18:43 +0200272static char host_addr[18];
273
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300274
Remy Bohmer23cd1382009-07-29 18:18:43 +0200275/*-------------------------------------------------------------------------*/
276
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400277/*
278 * USB DRIVER HOOKUP (to the hardware driver, below us), mostly
Remy Bohmer23cd1382009-07-29 18:18:43 +0200279 * ep0 implementation: descriptors, config management, setup().
280 * also optional class-specific notification interrupt transfer.
281 */
282
283/*
284 * DESCRIPTORS ... most are static, but strings and (full) configuration
285 * descriptors are built on demand. For now we do either full CDC, or
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300286 * our simple subset, with RNDIS as an optional second configuration.
287 *
288 * RNDIS includes some CDC ACM descriptors ... like CDC Ethernet. But
289 * the class descriptors match a modem (they're ignored; it's really just
290 * Ethernet functionality), they don't need the NOP altsetting, and the
291 * status transfer endpoint isn't optional.
Remy Bohmer23cd1382009-07-29 18:18:43 +0200292 */
293
294#define STRING_MANUFACTURER 1
295#define STRING_PRODUCT 2
296#define STRING_ETHADDR 3
297#define STRING_DATA 4
298#define STRING_CONTROL 5
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300299#define STRING_RNDIS_CONTROL 6
Remy Bohmer23cd1382009-07-29 18:18:43 +0200300#define STRING_CDC 7
301#define STRING_SUBSET 8
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300302#define STRING_RNDIS 9
Remy Bohmer23cd1382009-07-29 18:18:43 +0200303#define STRING_SERIALNUMBER 10
304
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300305/* holds our biggest descriptor (or RNDIS response) */
Remy Bohmer23cd1382009-07-29 18:18:43 +0200306#define USB_BUFSIZ 256
307
308/*
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300309 * This device advertises one configuration, eth_config, unless RNDIS
310 * is enabled (rndis_config) on hardware supporting at least two configs.
311 *
312 * NOTE: Controllers like superh_udc should probably be able to use
313 * an RNDIS-only configuration.
Remy Bohmer23cd1382009-07-29 18:18:43 +0200314 *
315 * FIXME define some higher-powered configurations to make it easier
316 * to recharge batteries ...
317 */
318
319#define DEV_CONFIG_VALUE 1 /* cdc or subset */
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300320#define DEV_RNDIS_CONFIG_VALUE 2 /* rndis; optional */
Remy Bohmer23cd1382009-07-29 18:18:43 +0200321
322static struct usb_device_descriptor
323device_desc = {
324 .bLength = sizeof device_desc,
325 .bDescriptorType = USB_DT_DEVICE,
326
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400327 .bcdUSB = __constant_cpu_to_le16(0x0200),
Remy Bohmer23cd1382009-07-29 18:18:43 +0200328
329 .bDeviceClass = USB_CLASS_COMM,
330 .bDeviceSubClass = 0,
331 .bDeviceProtocol = 0,
332
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400333 .idVendor = __constant_cpu_to_le16(CDC_VENDOR_NUM),
334 .idProduct = __constant_cpu_to_le16(CDC_PRODUCT_NUM),
Remy Bohmer23cd1382009-07-29 18:18:43 +0200335 .iManufacturer = STRING_MANUFACTURER,
336 .iProduct = STRING_PRODUCT,
337 .bNumConfigurations = 1,
338};
339
340static struct usb_otg_descriptor
341otg_descriptor = {
342 .bLength = sizeof otg_descriptor,
343 .bDescriptorType = USB_DT_OTG,
344
345 .bmAttributes = USB_OTG_SRP,
346};
347
348static struct usb_config_descriptor
349eth_config = {
350 .bLength = sizeof eth_config,
351 .bDescriptorType = USB_DT_CONFIG,
352
353 /* compute wTotalLength on the fly */
354 .bNumInterfaces = 2,
355 .bConfigurationValue = DEV_CONFIG_VALUE,
356 .iConfiguration = STRING_CDC,
357 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
358 .bMaxPower = 1,
359};
360
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300361#ifdef CONFIG_USB_ETH_RNDIS
362static struct usb_config_descriptor
363rndis_config = {
364 .bLength = sizeof rndis_config,
365 .bDescriptorType = USB_DT_CONFIG,
366
367 /* compute wTotalLength on the fly */
368 .bNumInterfaces = 2,
369 .bConfigurationValue = DEV_RNDIS_CONFIG_VALUE,
370 .iConfiguration = STRING_RNDIS,
371 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
372 .bMaxPower = 1,
373};
374#endif
375
Remy Bohmer23cd1382009-07-29 18:18:43 +0200376/*
377 * Compared to the simple CDC subset, the full CDC Ethernet model adds
378 * three class descriptors, two interface descriptors, optional status
379 * endpoint. Both have a "data" interface and two bulk endpoints.
380 * There are also differences in how control requests are handled.
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300381 *
382 * RNDIS shares a lot with CDC-Ethernet, since it's a variant of the
383 * CDC-ACM (modem) spec. Unfortunately MSFT's RNDIS driver is buggy; it
384 * may hang or oops. Since bugfixes (or accurate specs, letting Linux
385 * work around those bugs) are unlikely to ever come from MSFT, you may
386 * wish to avoid using RNDIS.
387 *
388 * MCCI offers an alternative to RNDIS if you need to connect to Windows
389 * but have hardware that can't support CDC Ethernet. We add descriptors
390 * to present the CDC Subset as a (nonconformant) CDC MDLM variant called
391 * "SAFE". That borrows from both CDC Ethernet and CDC MDLM. You can
392 * get those drivers from MCCI, or bundled with various products.
Remy Bohmer23cd1382009-07-29 18:18:43 +0200393 */
394
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200395#ifdef CONFIG_USB_ETH_CDC
Remy Bohmer23cd1382009-07-29 18:18:43 +0200396static struct usb_interface_descriptor
397control_intf = {
398 .bLength = sizeof control_intf,
399 .bDescriptorType = USB_DT_INTERFACE,
400
401 .bInterfaceNumber = 0,
402 /* status endpoint is optional; this may be patched later */
403 .bNumEndpoints = 1,
404 .bInterfaceClass = USB_CLASS_COMM,
405 .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET,
406 .bInterfaceProtocol = USB_CDC_PROTO_NONE,
407 .iInterface = STRING_CONTROL,
408};
409#endif
410
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300411#ifdef CONFIG_USB_ETH_RNDIS
412static const struct usb_interface_descriptor
413rndis_control_intf = {
414 .bLength = sizeof rndis_control_intf,
415 .bDescriptorType = USB_DT_INTERFACE,
416
417 .bInterfaceNumber = 0,
418 .bNumEndpoints = 1,
419 .bInterfaceClass = USB_CLASS_COMM,
420 .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
421 .bInterfaceProtocol = USB_CDC_ACM_PROTO_VENDOR,
422 .iInterface = STRING_RNDIS_CONTROL,
423};
424#endif
425
Remy Bohmer23cd1382009-07-29 18:18:43 +0200426static const struct usb_cdc_header_desc header_desc = {
427 .bLength = sizeof header_desc,
428 .bDescriptorType = USB_DT_CS_INTERFACE,
429 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
430
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400431 .bcdCDC = __constant_cpu_to_le16(0x0110),
Remy Bohmer23cd1382009-07-29 18:18:43 +0200432};
433
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200434#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Remy Bohmer23cd1382009-07-29 18:18:43 +0200435
436static const struct usb_cdc_union_desc union_desc = {
437 .bLength = sizeof union_desc,
438 .bDescriptorType = USB_DT_CS_INTERFACE,
439 .bDescriptorSubType = USB_CDC_UNION_TYPE,
440
441 .bMasterInterface0 = 0, /* index of control interface */
442 .bSlaveInterface0 = 1, /* index of DATA interface */
443};
444
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300445#endif /* CDC || RNDIS */
446
447#ifdef CONFIG_USB_ETH_RNDIS
448
449static const struct usb_cdc_call_mgmt_descriptor call_mgmt_descriptor = {
450 .bLength = sizeof call_mgmt_descriptor,
451 .bDescriptorType = USB_DT_CS_INTERFACE,
452 .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE,
453
454 .bmCapabilities = 0x00,
455 .bDataInterface = 0x01,
456};
457
458static const struct usb_cdc_acm_descriptor acm_descriptor = {
459 .bLength = sizeof acm_descriptor,
460 .bDescriptorType = USB_DT_CS_INTERFACE,
461 .bDescriptorSubType = USB_CDC_ACM_TYPE,
462
463 .bmCapabilities = 0x00,
464};
465
466#endif
Remy Bohmer23cd1382009-07-29 18:18:43 +0200467
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200468#ifndef CONFIG_USB_ETH_CDC
Remy Bohmer23cd1382009-07-29 18:18:43 +0200469
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400470/*
471 * "SAFE" loosely follows CDC WMC MDLM, violating the spec in various
Remy Bohmer23cd1382009-07-29 18:18:43 +0200472 * ways: data endpoints live in the control interface, there's no data
473 * interface, and it's not used to talk to a cell phone radio.
474 */
475
476static const struct usb_cdc_mdlm_desc mdlm_desc = {
477 .bLength = sizeof mdlm_desc,
478 .bDescriptorType = USB_DT_CS_INTERFACE,
479 .bDescriptorSubType = USB_CDC_MDLM_TYPE,
480
481 .bcdVersion = __constant_cpu_to_le16(0x0100),
482 .bGUID = {
483 0x5d, 0x34, 0xcf, 0x66, 0x11, 0x18, 0x11, 0xd6,
484 0xa2, 0x1a, 0x00, 0x01, 0x02, 0xca, 0x9a, 0x7f,
485 },
486};
487
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400488/*
489 * since "usb_cdc_mdlm_detail_desc" is a variable length structure, we
Remy Bohmer23cd1382009-07-29 18:18:43 +0200490 * can't really use its struct. All we do here is say that we're using
491 * the submode of "SAFE" which directly matches the CDC Subset.
492 */
493static const u8 mdlm_detail_desc[] = {
494 6,
495 USB_DT_CS_INTERFACE,
496 USB_CDC_MDLM_DETAIL_TYPE,
497
498 0, /* "SAFE" */
499 0, /* network control capabilities (none) */
500 0, /* network data capabilities ("raw" encapsulation) */
501};
502
503#endif
504
Remy Bohmer23cd1382009-07-29 18:18:43 +0200505static const struct usb_cdc_ether_desc ether_desc = {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400506 .bLength = sizeof(ether_desc),
Remy Bohmer23cd1382009-07-29 18:18:43 +0200507 .bDescriptorType = USB_DT_CS_INTERFACE,
508 .bDescriptorSubType = USB_CDC_ETHERNET_TYPE,
509
510 /* this descriptor actually adds value, surprise! */
511 .iMACAddress = STRING_ETHADDR,
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400512 .bmEthernetStatistics = __constant_cpu_to_le32(0), /* no statistics */
513 .wMaxSegmentSize = __constant_cpu_to_le16(ETH_FRAME_LEN),
514 .wNumberMCFilters = __constant_cpu_to_le16(0),
Remy Bohmer23cd1382009-07-29 18:18:43 +0200515 .bNumberPowerFilters = 0,
516};
517
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200518#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Remy Bohmer23cd1382009-07-29 18:18:43 +0200519
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400520/*
521 * include the status endpoint if we can, even where it's optional.
Remy Bohmer23cd1382009-07-29 18:18:43 +0200522 * use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one
523 * packet, to simplify cancellation; and a big transfer interval, to
524 * waste less bandwidth.
525 *
526 * some drivers (like Linux 2.4 cdc-ether!) "need" it to exist even
527 * if they ignore the connect/disconnect notifications that real aether
528 * can provide. more advanced cdc configurations might want to support
529 * encapsulated commands (vendor-specific, using control-OUT).
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300530 *
531 * RNDIS requires the status endpoint, since it uses that encapsulation
532 * mechanism for its funky RPC scheme.
Remy Bohmer23cd1382009-07-29 18:18:43 +0200533 */
534
535#define LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */
536#define STATUS_BYTECOUNT 16 /* 8 byte header + data */
537
538static struct usb_endpoint_descriptor
539fs_status_desc = {
540 .bLength = USB_DT_ENDPOINT_SIZE,
541 .bDescriptorType = USB_DT_ENDPOINT,
542
543 .bEndpointAddress = USB_DIR_IN,
544 .bmAttributes = USB_ENDPOINT_XFER_INT,
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400545 .wMaxPacketSize = __constant_cpu_to_le16(STATUS_BYTECOUNT),
Remy Bohmer23cd1382009-07-29 18:18:43 +0200546 .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC,
547};
548#endif
549
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200550#ifdef CONFIG_USB_ETH_CDC
Remy Bohmer23cd1382009-07-29 18:18:43 +0200551
552/* the default data interface has no endpoints ... */
553
554static const struct usb_interface_descriptor
555data_nop_intf = {
556 .bLength = sizeof data_nop_intf,
557 .bDescriptorType = USB_DT_INTERFACE,
558
559 .bInterfaceNumber = 1,
560 .bAlternateSetting = 0,
561 .bNumEndpoints = 0,
562 .bInterfaceClass = USB_CLASS_CDC_DATA,
563 .bInterfaceSubClass = 0,
564 .bInterfaceProtocol = 0,
565};
566
567/* ... but the "real" data interface has two bulk endpoints */
568
569static const struct usb_interface_descriptor
570data_intf = {
571 .bLength = sizeof data_intf,
572 .bDescriptorType = USB_DT_INTERFACE,
573
574 .bInterfaceNumber = 1,
575 .bAlternateSetting = 1,
576 .bNumEndpoints = 2,
577 .bInterfaceClass = USB_CLASS_CDC_DATA,
578 .bInterfaceSubClass = 0,
579 .bInterfaceProtocol = 0,
580 .iInterface = STRING_DATA,
581};
582
583#endif
584
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300585#ifdef CONFIG_USB_ETH_RNDIS
586
587/* RNDIS doesn't activate by changing to the "real" altsetting */
588
589static const struct usb_interface_descriptor
590rndis_data_intf = {
591 .bLength = sizeof rndis_data_intf,
592 .bDescriptorType = USB_DT_INTERFACE,
593
594 .bInterfaceNumber = 1,
595 .bAlternateSetting = 0,
596 .bNumEndpoints = 2,
597 .bInterfaceClass = USB_CLASS_CDC_DATA,
598 .bInterfaceSubClass = 0,
599 .bInterfaceProtocol = 0,
600 .iInterface = STRING_DATA,
601};
602
603#endif
604
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200605#ifdef CONFIG_USB_ETH_SUBSET
Remy Bohmer23cd1382009-07-29 18:18:43 +0200606
607/*
608 * "Simple" CDC-subset option is a simple vendor-neutral model that most
609 * full speed controllers can handle: one interface, two bulk endpoints.
610 *
611 * To assist host side drivers, we fancy it up a bit, and add descriptors
612 * so some host side drivers will understand it as a "SAFE" variant.
613 */
614
615static const struct usb_interface_descriptor
616subset_data_intf = {
617 .bLength = sizeof subset_data_intf,
618 .bDescriptorType = USB_DT_INTERFACE,
619
620 .bInterfaceNumber = 0,
621 .bAlternateSetting = 0,
622 .bNumEndpoints = 2,
623 .bInterfaceClass = USB_CLASS_COMM,
624 .bInterfaceSubClass = USB_CDC_SUBCLASS_MDLM,
625 .bInterfaceProtocol = 0,
626 .iInterface = STRING_DATA,
627};
628
629#endif /* SUBSET */
630
Remy Bohmer23cd1382009-07-29 18:18:43 +0200631static struct usb_endpoint_descriptor
632fs_source_desc = {
633 .bLength = USB_DT_ENDPOINT_SIZE,
634 .bDescriptorType = USB_DT_ENDPOINT,
635
636 .bEndpointAddress = USB_DIR_IN,
637 .bmAttributes = USB_ENDPOINT_XFER_BULK,
638};
639
640static struct usb_endpoint_descriptor
641fs_sink_desc = {
642 .bLength = USB_DT_ENDPOINT_SIZE,
643 .bDescriptorType = USB_DT_ENDPOINT,
644
645 .bEndpointAddress = USB_DIR_OUT,
646 .bmAttributes = USB_ENDPOINT_XFER_BULK,
647};
648
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400649static const struct usb_descriptor_header *fs_eth_function[11] = {
Remy Bohmer23cd1382009-07-29 18:18:43 +0200650 (struct usb_descriptor_header *) &otg_descriptor,
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200651#ifdef CONFIG_USB_ETH_CDC
Remy Bohmer23cd1382009-07-29 18:18:43 +0200652 /* "cdc" mode descriptors */
653 (struct usb_descriptor_header *) &control_intf,
654 (struct usb_descriptor_header *) &header_desc,
655 (struct usb_descriptor_header *) &union_desc,
656 (struct usb_descriptor_header *) &ether_desc,
657 /* NOTE: status endpoint may need to be removed */
658 (struct usb_descriptor_header *) &fs_status_desc,
659 /* data interface, with altsetting */
660 (struct usb_descriptor_header *) &data_nop_intf,
661 (struct usb_descriptor_header *) &data_intf,
662 (struct usb_descriptor_header *) &fs_source_desc,
663 (struct usb_descriptor_header *) &fs_sink_desc,
664 NULL,
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200665#endif /* CONFIG_USB_ETH_CDC */
Remy Bohmer23cd1382009-07-29 18:18:43 +0200666};
667
668static inline void fs_subset_descriptors(void)
669{
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200670#ifdef CONFIG_USB_ETH_SUBSET
Remy Bohmer23cd1382009-07-29 18:18:43 +0200671 /* behavior is "CDC Subset"; extra descriptors say "SAFE" */
672 fs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
673 fs_eth_function[2] = (struct usb_descriptor_header *) &header_desc;
674 fs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc;
675 fs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc;
676 fs_eth_function[5] = (struct usb_descriptor_header *) &ether_desc;
677 fs_eth_function[6] = (struct usb_descriptor_header *) &fs_source_desc;
678 fs_eth_function[7] = (struct usb_descriptor_header *) &fs_sink_desc;
679 fs_eth_function[8] = NULL;
680#else
681 fs_eth_function[1] = NULL;
682#endif
683}
684
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300685#ifdef CONFIG_USB_ETH_RNDIS
686static const struct usb_descriptor_header *fs_rndis_function[] = {
687 (struct usb_descriptor_header *) &otg_descriptor,
688 /* control interface matches ACM, not Ethernet */
689 (struct usb_descriptor_header *) &rndis_control_intf,
690 (struct usb_descriptor_header *) &header_desc,
691 (struct usb_descriptor_header *) &call_mgmt_descriptor,
692 (struct usb_descriptor_header *) &acm_descriptor,
693 (struct usb_descriptor_header *) &union_desc,
694 (struct usb_descriptor_header *) &fs_status_desc,
695 /* data interface has no altsetting */
696 (struct usb_descriptor_header *) &rndis_data_intf,
697 (struct usb_descriptor_header *) &fs_source_desc,
698 (struct usb_descriptor_header *) &fs_sink_desc,
699 NULL,
700};
701#endif
702
Remy Bohmer23cd1382009-07-29 18:18:43 +0200703/*
704 * usb 2.0 devices need to expose both high speed and full speed
705 * descriptors, unless they only run at full speed.
706 */
707
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200708#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Remy Bohmer23cd1382009-07-29 18:18:43 +0200709static struct usb_endpoint_descriptor
710hs_status_desc = {
711 .bLength = USB_DT_ENDPOINT_SIZE,
712 .bDescriptorType = USB_DT_ENDPOINT,
713
714 .bmAttributes = USB_ENDPOINT_XFER_INT,
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400715 .wMaxPacketSize = __constant_cpu_to_le16(STATUS_BYTECOUNT),
Remy Bohmer23cd1382009-07-29 18:18:43 +0200716 .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4,
717};
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200718#endif /* CONFIG_USB_ETH_CDC */
Remy Bohmer23cd1382009-07-29 18:18:43 +0200719
720static struct usb_endpoint_descriptor
721hs_source_desc = {
722 .bLength = USB_DT_ENDPOINT_SIZE,
723 .bDescriptorType = USB_DT_ENDPOINT,
724
725 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400726 .wMaxPacketSize = __constant_cpu_to_le16(512),
Remy Bohmer23cd1382009-07-29 18:18:43 +0200727};
728
729static struct usb_endpoint_descriptor
730hs_sink_desc = {
731 .bLength = USB_DT_ENDPOINT_SIZE,
732 .bDescriptorType = USB_DT_ENDPOINT,
733
734 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400735 .wMaxPacketSize = __constant_cpu_to_le16(512),
Remy Bohmer23cd1382009-07-29 18:18:43 +0200736};
737
738static struct usb_qualifier_descriptor
739dev_qualifier = {
740 .bLength = sizeof dev_qualifier,
741 .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
742
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400743 .bcdUSB = __constant_cpu_to_le16(0x0200),
Remy Bohmer23cd1382009-07-29 18:18:43 +0200744 .bDeviceClass = USB_CLASS_COMM,
745
746 .bNumConfigurations = 1,
747};
748
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400749static const struct usb_descriptor_header *hs_eth_function[11] = {
Remy Bohmer23cd1382009-07-29 18:18:43 +0200750 (struct usb_descriptor_header *) &otg_descriptor,
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200751#ifdef CONFIG_USB_ETH_CDC
Remy Bohmer23cd1382009-07-29 18:18:43 +0200752 /* "cdc" mode descriptors */
753 (struct usb_descriptor_header *) &control_intf,
754 (struct usb_descriptor_header *) &header_desc,
755 (struct usb_descriptor_header *) &union_desc,
756 (struct usb_descriptor_header *) &ether_desc,
757 /* NOTE: status endpoint may need to be removed */
758 (struct usb_descriptor_header *) &hs_status_desc,
759 /* data interface, with altsetting */
760 (struct usb_descriptor_header *) &data_nop_intf,
761 (struct usb_descriptor_header *) &data_intf,
762 (struct usb_descriptor_header *) &hs_source_desc,
763 (struct usb_descriptor_header *) &hs_sink_desc,
764 NULL,
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200765#endif /* CONFIG_USB_ETH_CDC */
Remy Bohmer23cd1382009-07-29 18:18:43 +0200766};
767
768static inline void hs_subset_descriptors(void)
769{
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200770#ifdef CONFIG_USB_ETH_SUBSET
Remy Bohmer23cd1382009-07-29 18:18:43 +0200771 /* behavior is "CDC Subset"; extra descriptors say "SAFE" */
772 hs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
773 hs_eth_function[2] = (struct usb_descriptor_header *) &header_desc;
774 hs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc;
775 hs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc;
776 hs_eth_function[5] = (struct usb_descriptor_header *) &ether_desc;
777 hs_eth_function[6] = (struct usb_descriptor_header *) &hs_source_desc;
778 hs_eth_function[7] = (struct usb_descriptor_header *) &hs_sink_desc;
779 hs_eth_function[8] = NULL;
780#else
781 hs_eth_function[1] = NULL;
782#endif
783}
784
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300785#ifdef CONFIG_USB_ETH_RNDIS
786static const struct usb_descriptor_header *hs_rndis_function[] = {
787 (struct usb_descriptor_header *) &otg_descriptor,
788 /* control interface matches ACM, not Ethernet */
789 (struct usb_descriptor_header *) &rndis_control_intf,
790 (struct usb_descriptor_header *) &header_desc,
791 (struct usb_descriptor_header *) &call_mgmt_descriptor,
792 (struct usb_descriptor_header *) &acm_descriptor,
793 (struct usb_descriptor_header *) &union_desc,
794 (struct usb_descriptor_header *) &hs_status_desc,
795 /* data interface has no altsetting */
796 (struct usb_descriptor_header *) &rndis_data_intf,
797 (struct usb_descriptor_header *) &hs_source_desc,
798 (struct usb_descriptor_header *) &hs_sink_desc,
799 NULL,
800};
801#endif
802
803
Remy Bohmer23cd1382009-07-29 18:18:43 +0200804/* maxpacket and other transfer characteristics vary by speed. */
805static inline struct usb_endpoint_descriptor *
806ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *hs,
807 struct usb_endpoint_descriptor *fs)
808{
809 if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
810 return hs;
811 return fs;
812}
813
Remy Bohmer23cd1382009-07-29 18:18:43 +0200814/*-------------------------------------------------------------------------*/
815
816/* descriptors that are built on-demand */
817
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400818static char manufacturer[50];
819static char product_desc[40] = DRIVER_DESC;
820static char serial_number[20];
Remy Bohmer23cd1382009-07-29 18:18:43 +0200821
822/* address that the host will use ... usually assigned at random */
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400823static char ethaddr[2 * ETH_ALEN + 1];
Remy Bohmer23cd1382009-07-29 18:18:43 +0200824
825/* static strings, in UTF-8 */
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400826static struct usb_string strings[] = {
Remy Bohmer23cd1382009-07-29 18:18:43 +0200827 { STRING_MANUFACTURER, manufacturer, },
828 { STRING_PRODUCT, product_desc, },
829 { STRING_SERIALNUMBER, serial_number, },
830 { STRING_DATA, "Ethernet Data", },
831 { STRING_ETHADDR, ethaddr, },
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200832#ifdef CONFIG_USB_ETH_CDC
Remy Bohmer23cd1382009-07-29 18:18:43 +0200833 { STRING_CDC, "CDC Ethernet", },
834 { STRING_CONTROL, "CDC Communications Control", },
835#endif
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200836#ifdef CONFIG_USB_ETH_SUBSET
Remy Bohmer23cd1382009-07-29 18:18:43 +0200837 { STRING_SUBSET, "CDC Ethernet Subset", },
838#endif
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300839#ifdef CONFIG_USB_ETH_RNDIS
840 { STRING_RNDIS, "RNDIS", },
841 { STRING_RNDIS_CONTROL, "RNDIS Communications Control", },
842#endif
Remy Bohmer23cd1382009-07-29 18:18:43 +0200843 { } /* end of list */
844};
845
846static struct usb_gadget_strings stringtab = {
847 .language = 0x0409, /* en-us */
848 .strings = strings,
849};
850
Remy Bohmer23cd1382009-07-29 18:18:43 +0200851/*============================================================================*/
Joel Fernandes52907592013-09-04 18:55:14 -0500852DEFINE_CACHE_ALIGN_BUFFER(u8, control_req, USB_BUFSIZ);
853
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200854#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Joel Fernandes52907592013-09-04 18:55:14 -0500855DEFINE_CACHE_ALIGN_BUFFER(u8, status_req, STATUS_BYTECOUNT);
Lukasz Dalek563aed22012-10-02 17:04:29 +0200856#endif
Remy Bohmer23cd1382009-07-29 18:18:43 +0200857
858
Remy Bohmer23cd1382009-07-29 18:18:43 +0200859/**
860 * strlcpy - Copy a %NUL terminated string into a sized buffer
861 * @dest: Where to copy the string to
862 * @src: Where to copy the string from
863 * @size: size of destination buffer
864 *
865 * Compatible with *BSD: the result is always a valid
866 * NUL-terminated string that fits in the buffer (unless,
867 * of course, the buffer size is zero). It does not pad
868 * out the result like strncpy() does.
869 */
870size_t strlcpy(char *dest, const char *src, size_t size)
871{
872 size_t ret = strlen(src);
873
874 if (size) {
875 size_t len = (ret >= size) ? size - 1 : ret;
876 memcpy(dest, src, len);
877 dest[len] = '\0';
878 }
879 return ret;
880}
881
Remy Bohmer23cd1382009-07-29 18:18:43 +0200882/*============================================================================*/
883
884/*
885 * one config, two interfaces: control, data.
886 * complications: class descriptors, and an altsetting.
887 */
888static int
889config_buf(struct usb_gadget *g, u8 *buf, u8 type, unsigned index, int is_otg)
890{
891 int len;
892 const struct usb_config_descriptor *config;
893 const struct usb_descriptor_header **function;
894 int hs = 0;
895
896 if (gadget_is_dualspeed(g)) {
897 hs = (g->speed == USB_SPEED_HIGH);
898 if (type == USB_DT_OTHER_SPEED_CONFIG)
899 hs = !hs;
900 }
901#define which_fn(t) (hs ? hs_ ## t ## _function : fs_ ## t ## _function)
902
903 if (index >= device_desc.bNumConfigurations)
904 return -EINVAL;
905
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300906#ifdef CONFIG_USB_ETH_RNDIS
907 /*
908 * list the RNDIS config first, to make Microsoft's drivers
909 * happy. DOCSIS 1.0 needs this too.
910 */
911 if (device_desc.bNumConfigurations == 2 && index == 0) {
912 config = &rndis_config;
913 function = which_fn(rndis);
914 } else
915#endif
916 {
917 config = &eth_config;
918 function = which_fn(eth);
919 }
Remy Bohmer23cd1382009-07-29 18:18:43 +0200920
921 /* for now, don't advertise srp-only devices */
922 if (!is_otg)
923 function++;
924
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400925 len = usb_gadget_config_buf(config, buf, USB_BUFSIZ, function);
Remy Bohmer23cd1382009-07-29 18:18:43 +0200926 if (len < 0)
927 return len;
928 ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
929 return len;
930}
931
932/*-------------------------------------------------------------------------*/
933
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300934static void eth_start(struct eth_dev *dev, gfp_t gfp_flags);
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400935static int alloc_requests(struct eth_dev *dev, unsigned n, gfp_t gfp_flags);
Remy Bohmer23cd1382009-07-29 18:18:43 +0200936
937static int
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400938set_ether_config(struct eth_dev *dev, gfp_t gfp_flags)
Remy Bohmer23cd1382009-07-29 18:18:43 +0200939{
940 int result = 0;
941 struct usb_gadget *gadget = dev->gadget;
942
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200943#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300944 /* status endpoint used for RNDIS and (optionally) CDC */
Remy Bohmer23cd1382009-07-29 18:18:43 +0200945 if (!subset_active(dev) && dev->status_ep) {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400946 dev->status = ep_desc(gadget, &hs_status_desc,
Remy Bohmer23cd1382009-07-29 18:18:43 +0200947 &fs_status_desc);
948 dev->status_ep->driver_data = dev;
949
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400950 result = usb_ep_enable(dev->status_ep, dev->status);
Remy Bohmer23cd1382009-07-29 18:18:43 +0200951 if (result != 0) {
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +0400952 debug("enable %s --> %d\n",
Remy Bohmer23cd1382009-07-29 18:18:43 +0200953 dev->status_ep->name, result);
954 goto done;
955 }
956 }
957#endif
958
959 dev->in = ep_desc(gadget, &hs_source_desc, &fs_source_desc);
960 dev->in_ep->driver_data = dev;
961
962 dev->out = ep_desc(gadget, &hs_sink_desc, &fs_sink_desc);
963 dev->out_ep->driver_data = dev;
964
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400965 /*
966 * With CDC, the host isn't allowed to use these two data
Remy Bohmer23cd1382009-07-29 18:18:43 +0200967 * endpoints in the default altsetting for the interface.
968 * so we don't activate them yet. Reset from SET_INTERFACE.
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300969 *
970 * Strictly speaking RNDIS should work the same: activation is
971 * a side effect of setting a packet filter. Deactivation is
972 * from REMOTE_NDIS_HALT_MSG, reset from REMOTE_NDIS_RESET_MSG.
Remy Bohmer23cd1382009-07-29 18:18:43 +0200973 */
974 if (!cdc_active(dev)) {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400975 result = usb_ep_enable(dev->in_ep, dev->in);
Remy Bohmer23cd1382009-07-29 18:18:43 +0200976 if (result != 0) {
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +0400977 debug("enable %s --> %d\n",
Remy Bohmer23cd1382009-07-29 18:18:43 +0200978 dev->in_ep->name, result);
979 goto done;
980 }
981
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400982 result = usb_ep_enable(dev->out_ep, dev->out);
Remy Bohmer23cd1382009-07-29 18:18:43 +0200983 if (result != 0) {
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +0400984 debug("enable %s --> %d\n",
Remy Bohmer23cd1382009-07-29 18:18:43 +0200985 dev->out_ep->name, result);
986 goto done;
987 }
988 }
989
990done:
991 if (result == 0)
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400992 result = alloc_requests(dev, qlen(gadget), gfp_flags);
Remy Bohmer23cd1382009-07-29 18:18:43 +0200993
994 /* on error, disable any endpoints */
995 if (result < 0) {
Vitaly Kuzmichevd5292c12010-08-13 17:02:29 +0400996 if (!subset_active(dev) && dev->status_ep)
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400997 (void) usb_ep_disable(dev->status_ep);
Remy Bohmer23cd1382009-07-29 18:18:43 +0200998 dev->status = NULL;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400999 (void) usb_ep_disable(dev->in_ep);
1000 (void) usb_ep_disable(dev->out_ep);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001001 dev->in = NULL;
1002 dev->out = NULL;
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001003 } else if (!cdc_active(dev)) {
1004 /*
1005 * activate non-CDC configs right away
1006 * this isn't strictly according to the RNDIS spec
1007 */
1008 eth_start(dev, GFP_ATOMIC);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001009 }
1010
1011 /* caller is responsible for cleanup on error */
1012 return result;
1013}
1014
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001015static void eth_reset_config(struct eth_dev *dev)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001016{
1017 if (dev->config == 0)
1018 return;
1019
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001020 debug("%s\n", __func__);
1021
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001022 rndis_uninit(dev->rndis_config);
1023
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001024 /*
1025 * disable endpoints, forcing (synchronous) completion of
Remy Bohmer23cd1382009-07-29 18:18:43 +02001026 * pending i/o. then free the requests.
1027 */
1028
1029 if (dev->in) {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001030 usb_ep_disable(dev->in_ep);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001031 if (dev->tx_req) {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001032 usb_ep_free_request(dev->in_ep, dev->tx_req);
1033 dev->tx_req = NULL;
Remy Bohmer23cd1382009-07-29 18:18:43 +02001034 }
1035 }
1036 if (dev->out) {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001037 usb_ep_disable(dev->out_ep);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001038 if (dev->rx_req) {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001039 usb_ep_free_request(dev->out_ep, dev->rx_req);
1040 dev->rx_req = NULL;
Remy Bohmer23cd1382009-07-29 18:18:43 +02001041 }
1042 }
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001043 if (dev->status)
1044 usb_ep_disable(dev->status_ep);
1045
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001046 dev->rndis = 0;
Remy Bohmer23cd1382009-07-29 18:18:43 +02001047 dev->cdc_filter = 0;
1048 dev->config = 0;
1049}
1050
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001051/*
1052 * change our operational config. must agree with the code
Remy Bohmer23cd1382009-07-29 18:18:43 +02001053 * that returns config descriptors, and altsetting code.
1054 */
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001055static int eth_set_config(struct eth_dev *dev, unsigned number,
1056 gfp_t gfp_flags)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001057{
1058 int result = 0;
1059 struct usb_gadget *gadget = dev->gadget;
1060
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001061 if (gadget_is_sa1100(gadget)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001062 && dev->config
1063 && dev->tx_qlen != 0) {
1064 /* tx fifo is full, but we can't clear it...*/
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001065 error("can't change configurations");
Remy Bohmer23cd1382009-07-29 18:18:43 +02001066 return -ESPIPE;
1067 }
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001068 eth_reset_config(dev);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001069
1070 switch (number) {
1071 case DEV_CONFIG_VALUE:
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001072 result = set_ether_config(dev, gfp_flags);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001073 break;
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001074#ifdef CONFIG_USB_ETH_RNDIS
1075 case DEV_RNDIS_CONFIG_VALUE:
1076 dev->rndis = 1;
1077 result = set_ether_config(dev, gfp_flags);
1078 break;
1079#endif
Remy Bohmer23cd1382009-07-29 18:18:43 +02001080 default:
1081 result = -EINVAL;
1082 /* FALL THROUGH */
1083 case 0:
1084 break;
1085 }
1086
1087 if (result) {
1088 if (number)
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001089 eth_reset_config(dev);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001090 usb_gadget_vbus_draw(dev->gadget,
1091 gadget_is_otg(dev->gadget) ? 8 : 100);
1092 } else {
1093 char *speed;
1094 unsigned power;
1095
1096 power = 2 * eth_config.bMaxPower;
1097 usb_gadget_vbus_draw(dev->gadget, power);
1098
1099 switch (gadget->speed) {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001100 case USB_SPEED_FULL:
1101 speed = "full"; break;
Remy Bohmer23cd1382009-07-29 18:18:43 +02001102#ifdef CONFIG_USB_GADGET_DUALSPEED
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001103 case USB_SPEED_HIGH:
1104 speed = "high"; break;
Remy Bohmer23cd1382009-07-29 18:18:43 +02001105#endif
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001106 default:
1107 speed = "?"; break;
Remy Bohmer23cd1382009-07-29 18:18:43 +02001108 }
1109
1110 dev->config = number;
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001111 printf("%s speed config #%d: %d mA, %s, using %s\n",
Remy Bohmer23cd1382009-07-29 18:18:43 +02001112 speed, number, power, driver_desc,
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001113 rndis_active(dev)
1114 ? "RNDIS"
1115 : (cdc_active(dev)
1116 ? "CDC Ethernet"
Remy Bohmer23cd1382009-07-29 18:18:43 +02001117 : "CDC Ethernet Subset"));
1118 }
1119 return result;
1120}
1121
1122/*-------------------------------------------------------------------------*/
1123
Lukasz Dalek2bb37882012-10-02 17:04:31 +02001124#ifdef CONFIG_USB_ETH_CDC
Remy Bohmer23cd1382009-07-29 18:18:43 +02001125
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001126/*
1127 * The interrupt endpoint is used in CDC networking models (Ethernet, ATM)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001128 * only to notify the host about link status changes (which we support) or
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001129 * report completion of some encapsulated command (as used in RNDIS). Since
Remy Bohmer23cd1382009-07-29 18:18:43 +02001130 * we want this CDC Ethernet code to be vendor-neutral, we don't use that
1131 * command mechanism; and only one status request is ever queued.
1132 */
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001133static void eth_status_complete(struct usb_ep *ep, struct usb_request *req)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001134{
1135 struct usb_cdc_notification *event = req->buf;
1136 int value = req->status;
1137 struct eth_dev *dev = ep->driver_data;
1138
1139 /* issue the second notification if host reads the first */
1140 if (event->bNotificationType == USB_CDC_NOTIFY_NETWORK_CONNECTION
1141 && value == 0) {
1142 __le32 *data = req->buf + sizeof *event;
1143
1144 event->bmRequestType = 0xA1;
1145 event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001146 event->wValue = __constant_cpu_to_le16(0);
1147 event->wIndex = __constant_cpu_to_le16(1);
1148 event->wLength = __constant_cpu_to_le16(8);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001149
1150 /* SPEED_CHANGE data is up/down speeds in bits/sec */
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001151 data[0] = data[1] = cpu_to_le32(BITRATE(dev->gadget));
Remy Bohmer23cd1382009-07-29 18:18:43 +02001152
1153 req->length = STATUS_BYTECOUNT;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001154 value = usb_ep_queue(ep, req, GFP_ATOMIC);
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001155 debug("send SPEED_CHANGE --> %d\n", value);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001156 if (value == 0)
1157 return;
1158 } else if (value != -ECONNRESET) {
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001159 debug("event %02x --> %d\n",
Remy Bohmer23cd1382009-07-29 18:18:43 +02001160 event->bNotificationType, value);
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001161 if (event->bNotificationType ==
1162 USB_CDC_NOTIFY_SPEED_CHANGE) {
1163 l_ethdev.network_started = 1;
Remy Bohmer23cd1382009-07-29 18:18:43 +02001164 printf("USB network up!\n");
1165 }
1166 }
1167 req->context = NULL;
1168}
1169
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001170static void issue_start_status(struct eth_dev *dev)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001171{
1172 struct usb_request *req = dev->stat_req;
1173 struct usb_cdc_notification *event;
1174 int value;
1175
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001176 /*
1177 * flush old status
Remy Bohmer23cd1382009-07-29 18:18:43 +02001178 *
1179 * FIXME ugly idiom, maybe we'd be better with just
1180 * a "cancel the whole queue" primitive since any
1181 * unlink-one primitive has way too many error modes.
1182 * here, we "know" toggle is already clear...
1183 *
1184 * FIXME iff req->context != null just dequeue it
1185 */
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001186 usb_ep_disable(dev->status_ep);
1187 usb_ep_enable(dev->status_ep, dev->status);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001188
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001189 /*
1190 * 3.8.1 says to issue first NETWORK_CONNECTION, then
Remy Bohmer23cd1382009-07-29 18:18:43 +02001191 * a SPEED_CHANGE. could be useful in some configs.
1192 */
1193 event = req->buf;
1194 event->bmRequestType = 0xA1;
1195 event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001196 event->wValue = __constant_cpu_to_le16(1); /* connected */
1197 event->wIndex = __constant_cpu_to_le16(1);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001198 event->wLength = 0;
1199
1200 req->length = sizeof *event;
1201 req->complete = eth_status_complete;
1202 req->context = dev;
1203
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001204 value = usb_ep_queue(dev->status_ep, req, GFP_ATOMIC);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001205 if (value < 0)
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001206 debug("status buf queue --> %d\n", value);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001207}
1208
1209#endif
1210
1211/*-------------------------------------------------------------------------*/
1212
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001213static void eth_setup_complete(struct usb_ep *ep, struct usb_request *req)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001214{
1215 if (req->status || req->actual != req->length)
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001216 debug("setup complete --> %d, %d/%d\n",
Remy Bohmer23cd1382009-07-29 18:18:43 +02001217 req->status, req->actual, req->length);
1218}
1219
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001220#ifdef CONFIG_USB_ETH_RNDIS
1221
1222static void rndis_response_complete(struct usb_ep *ep, struct usb_request *req)
1223{
1224 if (req->status || req->actual != req->length)
1225 debug("rndis response complete --> %d, %d/%d\n",
1226 req->status, req->actual, req->length);
1227
1228 /* done sending after USB_CDC_GET_ENCAPSULATED_RESPONSE */
1229}
1230
1231static void rndis_command_complete(struct usb_ep *ep, struct usb_request *req)
1232{
1233 struct eth_dev *dev = ep->driver_data;
1234 int status;
1235
1236 /* received RNDIS command from USB_CDC_SEND_ENCAPSULATED_COMMAND */
1237 status = rndis_msg_parser(dev->rndis_config, (u8 *) req->buf);
1238 if (status < 0)
1239 error("%s: rndis parse error %d", __func__, status);
1240}
1241
1242#endif /* RNDIS */
1243
Remy Bohmer23cd1382009-07-29 18:18:43 +02001244/*
1245 * The setup() callback implements all the ep0 functionality that's not
1246 * handled lower down. CDC has a number of less-common features:
1247 *
1248 * - two interfaces: control, and ethernet data
1249 * - Ethernet data interface has two altsettings: default, and active
1250 * - class-specific descriptors for the control interface
1251 * - class-specific control requests
1252 */
1253static int
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001254eth_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001255{
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001256 struct eth_dev *dev = get_gadget_data(gadget);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001257 struct usb_request *req = dev->req;
1258 int value = -EOPNOTSUPP;
1259 u16 wIndex = le16_to_cpu(ctrl->wIndex);
1260 u16 wValue = le16_to_cpu(ctrl->wValue);
1261 u16 wLength = le16_to_cpu(ctrl->wLength);
1262
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001263 /*
1264 * descriptors just go into the pre-allocated ep0 buffer,
Remy Bohmer23cd1382009-07-29 18:18:43 +02001265 * while config change events may enable network traffic.
1266 */
1267
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001268 debug("%s\n", __func__);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001269
1270 req->complete = eth_setup_complete;
1271 switch (ctrl->bRequest) {
1272
1273 case USB_REQ_GET_DESCRIPTOR:
1274 if (ctrl->bRequestType != USB_DIR_IN)
1275 break;
1276 switch (wValue >> 8) {
1277
1278 case USB_DT_DEVICE:
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001279 value = min(wLength, (u16) sizeof device_desc);
1280 memcpy(req->buf, &device_desc, value);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001281 break;
1282 case USB_DT_DEVICE_QUALIFIER:
1283 if (!gadget_is_dualspeed(gadget))
1284 break;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001285 value = min(wLength, (u16) sizeof dev_qualifier);
1286 memcpy(req->buf, &dev_qualifier, value);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001287 break;
1288
1289 case USB_DT_OTHER_SPEED_CONFIG:
1290 if (!gadget_is_dualspeed(gadget))
1291 break;
1292 /* FALLTHROUGH */
1293 case USB_DT_CONFIG:
1294 value = config_buf(gadget, req->buf,
1295 wValue >> 8,
1296 wValue & 0xff,
1297 gadget_is_otg(gadget));
1298 if (value >= 0)
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001299 value = min(wLength, (u16) value);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001300 break;
1301
1302 case USB_DT_STRING:
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001303 value = usb_gadget_get_string(&stringtab,
Remy Bohmer23cd1382009-07-29 18:18:43 +02001304 wValue & 0xff, req->buf);
1305
1306 if (value >= 0)
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001307 value = min(wLength, (u16) value);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001308
1309 break;
1310 }
1311 break;
1312
1313 case USB_REQ_SET_CONFIGURATION:
1314 if (ctrl->bRequestType != 0)
1315 break;
1316 if (gadget->a_hnp_support)
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001317 debug("HNP available\n");
Remy Bohmer23cd1382009-07-29 18:18:43 +02001318 else if (gadget->a_alt_hnp_support)
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001319 debug("HNP needs a different root port\n");
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001320 value = eth_set_config(dev, wValue, GFP_ATOMIC);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001321 break;
1322 case USB_REQ_GET_CONFIGURATION:
1323 if (ctrl->bRequestType != USB_DIR_IN)
1324 break;
1325 *(u8 *)req->buf = dev->config;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001326 value = min(wLength, (u16) 1);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001327 break;
1328
1329 case USB_REQ_SET_INTERFACE:
1330 if (ctrl->bRequestType != USB_RECIP_INTERFACE
1331 || !dev->config
1332 || wIndex > 1)
1333 break;
1334 if (!cdc_active(dev) && wIndex != 0)
1335 break;
1336
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001337 /*
1338 * PXA hardware partially handles SET_INTERFACE;
Remy Bohmer23cd1382009-07-29 18:18:43 +02001339 * we need to kluge around that interference.
1340 */
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001341 if (gadget_is_pxa(gadget)) {
1342 value = eth_set_config(dev, DEV_CONFIG_VALUE,
Remy Bohmer23cd1382009-07-29 18:18:43 +02001343 GFP_ATOMIC);
Lukasz Dalek563aed22012-10-02 17:04:29 +02001344 /*
1345 * PXA25x driver use non-CDC ethernet gadget.
1346 * But only _CDC and _RNDIS code can signalize
1347 * that network is working. So we signalize it
1348 * here.
1349 */
1350 l_ethdev.network_started = 1;
1351 debug("USB network up!\n");
Remy Bohmer23cd1382009-07-29 18:18:43 +02001352 goto done_set_intf;
1353 }
1354
Lukasz Dalek2bb37882012-10-02 17:04:31 +02001355#ifdef CONFIG_USB_ETH_CDC
Remy Bohmer23cd1382009-07-29 18:18:43 +02001356 switch (wIndex) {
1357 case 0: /* control/master intf */
1358 if (wValue != 0)
1359 break;
1360 if (dev->status) {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001361 usb_ep_disable(dev->status_ep);
1362 usb_ep_enable(dev->status_ep, dev->status);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001363 }
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001364
Remy Bohmer23cd1382009-07-29 18:18:43 +02001365 value = 0;
1366 break;
1367 case 1: /* data intf */
1368 if (wValue > 1)
1369 break;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001370 usb_ep_disable(dev->in_ep);
1371 usb_ep_disable(dev->out_ep);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001372
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001373 /*
1374 * CDC requires the data transfers not be done from
Remy Bohmer23cd1382009-07-29 18:18:43 +02001375 * the default interface setting ... also, setting
1376 * the non-default interface resets filters etc.
1377 */
1378 if (wValue == 1) {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001379 if (!cdc_active(dev))
Remy Bohmer23cd1382009-07-29 18:18:43 +02001380 break;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001381 usb_ep_enable(dev->in_ep, dev->in);
1382 usb_ep_enable(dev->out_ep, dev->out);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001383 dev->cdc_filter = DEFAULT_FILTER;
1384 if (dev->status)
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001385 issue_start_status(dev);
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001386 eth_start(dev, GFP_ATOMIC);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001387 }
Remy Bohmer23cd1382009-07-29 18:18:43 +02001388 value = 0;
1389 break;
1390 }
1391#else
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001392 /*
1393 * FIXME this is wrong, as is the assumption that
Remy Bohmer23cd1382009-07-29 18:18:43 +02001394 * all non-PXA hardware talks real CDC ...
1395 */
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001396 debug("set_interface ignored!\n");
Lukasz Dalek2bb37882012-10-02 17:04:31 +02001397#endif /* CONFIG_USB_ETH_CDC */
Remy Bohmer23cd1382009-07-29 18:18:43 +02001398
1399done_set_intf:
1400 break;
1401 case USB_REQ_GET_INTERFACE:
1402 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE)
1403 || !dev->config
1404 || wIndex > 1)
1405 break;
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001406 if (!(cdc_active(dev) || rndis_active(dev)) && wIndex != 0)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001407 break;
1408
1409 /* for CDC, iff carrier is on, data interface is active. */
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001410 if (rndis_active(dev) || wIndex != 1)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001411 *(u8 *)req->buf = 0;
1412 else {
1413 /* *(u8 *)req->buf = netif_carrier_ok (dev->net) ? 1 : 0; */
1414 /* carrier always ok ...*/
1415 *(u8 *)req->buf = 1 ;
1416 }
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001417 value = min(wLength, (u16) 1);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001418 break;
1419
Lukasz Dalek2bb37882012-10-02 17:04:31 +02001420#ifdef CONFIG_USB_ETH_CDC
Remy Bohmer23cd1382009-07-29 18:18:43 +02001421 case USB_CDC_SET_ETHERNET_PACKET_FILTER:
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001422 /*
1423 * see 6.2.30: no data, wIndex = interface,
Remy Bohmer23cd1382009-07-29 18:18:43 +02001424 * wValue = packet filter bitmap
1425 */
1426 if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1427 || !cdc_active(dev)
1428 || wLength != 0
1429 || wIndex > 1)
1430 break;
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001431 debug("packet filter %02x\n", wValue);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001432 dev->cdc_filter = wValue;
1433 value = 0;
1434 break;
1435
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001436 /*
1437 * and potentially:
Remy Bohmer23cd1382009-07-29 18:18:43 +02001438 * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS:
1439 * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER:
1440 * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER:
1441 * case USB_CDC_GET_ETHERNET_STATISTIC:
1442 */
1443
Lukasz Dalek2bb37882012-10-02 17:04:31 +02001444#endif /* CONFIG_USB_ETH_CDC */
Remy Bohmer23cd1382009-07-29 18:18:43 +02001445
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001446#ifdef CONFIG_USB_ETH_RNDIS
1447 /*
1448 * RNDIS uses the CDC command encapsulation mechanism to implement
1449 * an RPC scheme, with much getting/setting of attributes by OID.
1450 */
1451 case USB_CDC_SEND_ENCAPSULATED_COMMAND:
1452 if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1453 || !rndis_active(dev)
1454 || wLength > USB_BUFSIZ
1455 || wValue
1456 || rndis_control_intf.bInterfaceNumber
1457 != wIndex)
1458 break;
1459 /* read the request, then process it */
1460 value = wLength;
1461 req->complete = rndis_command_complete;
1462 /* later, rndis_control_ack () sends a notification */
1463 break;
1464
1465 case USB_CDC_GET_ENCAPSULATED_RESPONSE:
1466 if ((USB_DIR_IN|USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1467 == ctrl->bRequestType
1468 && rndis_active(dev)
1469 /* && wLength >= 0x0400 */
1470 && !wValue
1471 && rndis_control_intf.bInterfaceNumber
1472 == wIndex) {
1473 u8 *buf;
1474 u32 n;
1475
1476 /* return the result */
1477 buf = rndis_get_next_response(dev->rndis_config, &n);
1478 if (buf) {
1479 memcpy(req->buf, buf, n);
1480 req->complete = rndis_response_complete;
1481 rndis_free_response(dev->rndis_config, buf);
1482 value = n;
1483 }
1484 /* else stalls ... spec says to avoid that */
1485 }
1486 break;
1487#endif /* RNDIS */
1488
Remy Bohmer23cd1382009-07-29 18:18:43 +02001489 default:
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001490 debug("unknown control req%02x.%02x v%04x i%04x l%d\n",
Remy Bohmer23cd1382009-07-29 18:18:43 +02001491 ctrl->bRequestType, ctrl->bRequest,
1492 wValue, wIndex, wLength);
1493 }
1494
1495 /* respond with data transfer before status phase? */
1496 if (value >= 0) {
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001497 debug("respond with data transfer before status phase\n");
Remy Bohmer23cd1382009-07-29 18:18:43 +02001498 req->length = value;
1499 req->zero = value < wLength
1500 && (value % gadget->ep0->maxpacket) == 0;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001501 value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001502 if (value < 0) {
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001503 debug("ep_queue --> %d\n", value);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001504 req->status = 0;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001505 eth_setup_complete(gadget->ep0, req);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001506 }
1507 }
1508
1509 /* host either stalls (value < 0) or reports success */
1510 return value;
1511}
1512
Remy Bohmer23cd1382009-07-29 18:18:43 +02001513/*-------------------------------------------------------------------------*/
1514
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001515static void rx_complete(struct usb_ep *ep, struct usb_request *req);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001516
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001517static int rx_submit(struct eth_dev *dev, struct usb_request *req,
Remy Bohmer23cd1382009-07-29 18:18:43 +02001518 gfp_t gfp_flags)
1519{
1520 int retval = -ENOMEM;
1521 size_t size;
1522
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001523 /*
1524 * Padding up to RX_EXTRA handles minor disagreements with host.
Remy Bohmer23cd1382009-07-29 18:18:43 +02001525 * Normally we use the USB "terminate on short read" convention;
1526 * so allow up to (N*maxpacket), since that memory is normally
1527 * already allocated. Some hardware doesn't deal well with short
1528 * reads (e.g. DMA must be N*maxpacket), so for now don't trim a
1529 * byte off the end (to force hardware errors on overflow).
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001530 *
1531 * RNDIS uses internal framing, and explicitly allows senders to
1532 * pad to end-of-packet. That's potentially nice for speed,
1533 * but means receivers can't recover synch on their own.
Remy Bohmer23cd1382009-07-29 18:18:43 +02001534 */
1535
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001536 debug("%s\n", __func__);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001537
1538 size = (ETHER_HDR_SIZE + dev->mtu + RX_EXTRA);
1539 size += dev->out_ep->maxpacket - 1;
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001540 if (rndis_active(dev))
1541 size += sizeof(struct rndis_packet_msg_type);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001542 size -= size % dev->out_ep->maxpacket;
1543
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001544 /*
1545 * Some platforms perform better when IP packets are aligned,
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001546 * but on at least one, checksumming fails otherwise. Note:
1547 * RNDIS headers involve variable numbers of LE32 values.
Remy Bohmer23cd1382009-07-29 18:18:43 +02001548 */
1549
1550 req->buf = (u8 *) NetRxPackets[0];
1551 req->length = size;
1552 req->complete = rx_complete;
1553
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001554 retval = usb_ep_queue(dev->out_ep, req, gfp_flags);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001555
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001556 if (retval)
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001557 error("rx submit --> %d", retval);
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001558
Remy Bohmer23cd1382009-07-29 18:18:43 +02001559 return retval;
1560}
1561
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001562static void rx_complete(struct usb_ep *ep, struct usb_request *req)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001563{
1564 struct eth_dev *dev = ep->driver_data;
1565
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001566 debug("%s: status %d\n", __func__, req->status);
Vitaly Kuzmichevc85d70e2011-02-11 18:18:32 +03001567 switch (req->status) {
1568 /* normal completion */
1569 case 0:
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001570 if (rndis_active(dev)) {
1571 /* we know MaxPacketsPerTransfer == 1 here */
1572 int length = rndis_rm_hdr(req->buf, req->actual);
1573 if (length < 0)
1574 goto length_err;
1575 req->length -= length;
1576 req->actual -= length;
1577 }
1578 if (req->actual < ETH_HLEN || ETH_FRAME_LEN < req->actual) {
1579length_err:
1580 dev->stats.rx_errors++;
1581 dev->stats.rx_length_errors++;
1582 debug("rx length %d\n", req->length);
1583 break;
1584 }
1585
Vitaly Kuzmichevc85d70e2011-02-11 18:18:32 +03001586 dev->stats.rx_packets++;
1587 dev->stats.rx_bytes += req->length;
1588 break;
1589
1590 /* software-driven interface shutdown */
1591 case -ECONNRESET: /* unlink */
1592 case -ESHUTDOWN: /* disconnect etc */
1593 /* for hardware automagic (such as pxa) */
1594 case -ECONNABORTED: /* endpoint reset */
1595 break;
1596
1597 /* data overrun */
1598 case -EOVERFLOW:
1599 dev->stats.rx_over_errors++;
1600 /* FALLTHROUGH */
1601 default:
1602 dev->stats.rx_errors++;
1603 break;
1604 }
Remy Bohmer23cd1382009-07-29 18:18:43 +02001605
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001606 packet_received = 1;
Remy Bohmer23cd1382009-07-29 18:18:43 +02001607}
1608
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001609static int alloc_requests(struct eth_dev *dev, unsigned n, gfp_t gfp_flags)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001610{
1611
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001612 dev->tx_req = usb_ep_alloc_request(dev->in_ep, 0);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001613
1614 if (!dev->tx_req)
Vitaly Kuzmichevac5d32d2010-09-22 13:13:55 +04001615 goto fail1;
Remy Bohmer23cd1382009-07-29 18:18:43 +02001616
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001617 dev->rx_req = usb_ep_alloc_request(dev->out_ep, 0);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001618
1619 if (!dev->rx_req)
Vitaly Kuzmichevac5d32d2010-09-22 13:13:55 +04001620 goto fail2;
Remy Bohmer23cd1382009-07-29 18:18:43 +02001621
1622 return 0;
1623
Vitaly Kuzmichevac5d32d2010-09-22 13:13:55 +04001624fail2:
1625 usb_ep_free_request(dev->in_ep, dev->tx_req);
1626fail1:
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001627 error("can't alloc requests");
Remy Bohmer23cd1382009-07-29 18:18:43 +02001628 return -1;
1629}
1630
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001631static void tx_complete(struct usb_ep *ep, struct usb_request *req)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001632{
Vitaly Kuzmichevc85d70e2011-02-11 18:18:32 +03001633 struct eth_dev *dev = ep->driver_data;
1634
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001635 debug("%s: status %s\n", __func__, (req->status) ? "failed" : "ok");
Vitaly Kuzmichevc85d70e2011-02-11 18:18:32 +03001636 switch (req->status) {
1637 default:
1638 dev->stats.tx_errors++;
1639 debug("tx err %d\n", req->status);
1640 /* FALLTHROUGH */
1641 case -ECONNRESET: /* unlink */
1642 case -ESHUTDOWN: /* disconnect etc */
1643 break;
1644 case 0:
1645 dev->stats.tx_bytes += req->length;
1646 }
1647 dev->stats.tx_packets++;
1648
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001649 packet_sent = 1;
Remy Bohmer23cd1382009-07-29 18:18:43 +02001650}
1651
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001652static inline int eth_is_promisc(struct eth_dev *dev)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001653{
1654 /* no filters for the CDC subset; always promisc */
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001655 if (subset_active(dev))
Remy Bohmer23cd1382009-07-29 18:18:43 +02001656 return 1;
1657 return dev->cdc_filter & USB_CDC_PACKET_TYPE_PROMISCUOUS;
1658}
1659
1660#if 0
1661static int eth_start_xmit (struct sk_buff *skb, struct net_device *net)
1662{
1663 struct eth_dev *dev = netdev_priv(net);
1664 int length = skb->len;
1665 int retval;
1666 struct usb_request *req = NULL;
1667 unsigned long flags;
1668
1669 /* apply outgoing CDC or RNDIS filters */
1670 if (!eth_is_promisc (dev)) {
1671 u8 *dest = skb->data;
1672
1673 if (is_multicast_ether_addr(dest)) {
1674 u16 type;
1675
1676 /* ignores USB_CDC_PACKET_TYPE_MULTICAST and host
1677 * SET_ETHERNET_MULTICAST_FILTERS requests
1678 */
1679 if (is_broadcast_ether_addr(dest))
1680 type = USB_CDC_PACKET_TYPE_BROADCAST;
1681 else
1682 type = USB_CDC_PACKET_TYPE_ALL_MULTICAST;
1683 if (!(dev->cdc_filter & type)) {
1684 dev_kfree_skb_any (skb);
1685 return 0;
1686 }
1687 }
1688 /* ignores USB_CDC_PACKET_TYPE_DIRECTED */
1689 }
1690
1691 spin_lock_irqsave(&dev->req_lock, flags);
1692 /*
1693 * this freelist can be empty if an interrupt triggered disconnect()
1694 * and reconfigured the gadget (shutting down this queue) after the
1695 * network stack decided to xmit but before we got the spinlock.
1696 */
1697 if (list_empty(&dev->tx_reqs)) {
1698 spin_unlock_irqrestore(&dev->req_lock, flags);
1699 return 1;
1700 }
1701
1702 req = container_of (dev->tx_reqs.next, struct usb_request, list);
1703 list_del (&req->list);
1704
1705 /* temporarily stop TX queue when the freelist empties */
1706 if (list_empty (&dev->tx_reqs))
1707 netif_stop_queue (net);
1708 spin_unlock_irqrestore(&dev->req_lock, flags);
1709
1710 /* no buffer copies needed, unless the network stack did it
1711 * or the hardware can't use skb buffers.
1712 * or there's not enough space for any RNDIS headers we need
1713 */
1714 if (rndis_active(dev)) {
1715 struct sk_buff *skb_rndis;
1716
1717 skb_rndis = skb_realloc_headroom (skb,
1718 sizeof (struct rndis_packet_msg_type));
1719 if (!skb_rndis)
1720 goto drop;
1721
1722 dev_kfree_skb_any (skb);
1723 skb = skb_rndis;
1724 rndis_add_hdr (skb);
1725 length = skb->len;
1726 }
1727 req->buf = skb->data;
1728 req->context = skb;
1729 req->complete = tx_complete;
1730
1731 /* use zlp framing on tx for strict CDC-Ether conformance,
1732 * though any robust network rx path ignores extra padding.
1733 * and some hardware doesn't like to write zlps.
1734 */
1735 req->zero = 1;
1736 if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0)
1737 length++;
1738
1739 req->length = length;
1740
1741 /* throttle highspeed IRQ rate back slightly */
1742 if (gadget_is_dualspeed(dev->gadget))
1743 req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH)
1744 ? ((atomic_read(&dev->tx_qlen) % qmult) != 0)
1745 : 0;
1746
1747 retval = usb_ep_queue (dev->in_ep, req, GFP_ATOMIC);
1748 switch (retval) {
1749 default:
1750 DEBUG (dev, "tx queue err %d\n", retval);
1751 break;
1752 case 0:
1753 net->trans_start = jiffies;
1754 atomic_inc (&dev->tx_qlen);
1755 }
1756
1757 if (retval) {
1758drop:
1759 dev->stats.tx_dropped++;
1760 dev_kfree_skb_any (skb);
1761 spin_lock_irqsave(&dev->req_lock, flags);
1762 if (list_empty (&dev->tx_reqs))
1763 netif_start_queue (net);
1764 list_add (&req->list, &dev->tx_reqs);
1765 spin_unlock_irqrestore(&dev->req_lock, flags);
1766 }
1767 return 0;
1768}
1769
1770/*-------------------------------------------------------------------------*/
1771#endif
1772
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001773static void eth_unbind(struct usb_gadget *gadget)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001774{
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001775 struct eth_dev *dev = get_gadget_data(gadget);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001776
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001777 debug("%s...\n", __func__);
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001778 rndis_deregister(dev->rndis_config);
1779 rndis_exit();
Remy Bohmer23cd1382009-07-29 18:18:43 +02001780
Vitaly Kuzmichev0129e322010-08-13 17:00:16 +04001781 /* we've already been disconnected ... no i/o is active */
1782 if (dev->req) {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001783 usb_ep_free_request(gadget->ep0, dev->req);
Vitaly Kuzmichev0129e322010-08-13 17:00:16 +04001784 dev->req = NULL;
1785 }
Remy Bohmer23cd1382009-07-29 18:18:43 +02001786 if (dev->stat_req) {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001787 usb_ep_free_request(dev->status_ep, dev->stat_req);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001788 dev->stat_req = NULL;
1789 }
1790
1791 if (dev->tx_req) {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001792 usb_ep_free_request(dev->in_ep, dev->tx_req);
1793 dev->tx_req = NULL;
Remy Bohmer23cd1382009-07-29 18:18:43 +02001794 }
1795
1796 if (dev->rx_req) {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001797 usb_ep_free_request(dev->out_ep, dev->rx_req);
1798 dev->rx_req = NULL;
Remy Bohmer23cd1382009-07-29 18:18:43 +02001799 }
1800
1801/* unregister_netdev (dev->net);*/
1802/* free_netdev(dev->net);*/
1803
Lei Wen988ee3e2010-12-01 23:43:43 +08001804 dev->gadget = NULL;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001805 set_gadget_data(gadget, NULL);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001806}
1807
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001808static void eth_disconnect(struct usb_gadget *gadget)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001809{
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001810 eth_reset_config(get_gadget_data(gadget));
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001811 /* FIXME RNDIS should enter RNDIS_UNINITIALIZED */
Remy Bohmer23cd1382009-07-29 18:18:43 +02001812}
1813
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001814static void eth_suspend(struct usb_gadget *gadget)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001815{
1816 /* Not used */
1817}
1818
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001819static void eth_resume(struct usb_gadget *gadget)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001820{
1821 /* Not used */
1822}
1823
1824/*-------------------------------------------------------------------------*/
1825
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001826#ifdef CONFIG_USB_ETH_RNDIS
1827
1828/*
1829 * The interrupt endpoint is used in RNDIS to notify the host when messages
1830 * other than data packets are available ... notably the REMOTE_NDIS_*_CMPLT
1831 * messages, but also REMOTE_NDIS_INDICATE_STATUS_MSG and potentially even
1832 * REMOTE_NDIS_KEEPALIVE_MSG.
1833 *
1834 * The RNDIS control queue is processed by GET_ENCAPSULATED_RESPONSE, and
1835 * normally just one notification will be queued.
1836 */
1837
1838static void rndis_control_ack_complete(struct usb_ep *ep,
1839 struct usb_request *req)
1840{
1841 struct eth_dev *dev = ep->driver_data;
1842
1843 debug("%s...\n", __func__);
1844 if (req->status || req->actual != req->length)
1845 debug("rndis control ack complete --> %d, %d/%d\n",
1846 req->status, req->actual, req->length);
1847
1848 if (!l_ethdev.network_started) {
1849 if (rndis_get_state(dev->rndis_config)
1850 == RNDIS_DATA_INITIALIZED) {
1851 l_ethdev.network_started = 1;
1852 printf("USB RNDIS network up!\n");
1853 }
1854 }
1855
1856 req->context = NULL;
1857
1858 if (req != dev->stat_req)
1859 usb_ep_free_request(ep, req);
1860}
1861
1862static char rndis_resp_buf[8] __attribute__((aligned(sizeof(__le32))));
1863
1864static int rndis_control_ack(struct eth_device *net)
1865{
1866 struct eth_dev *dev = &l_ethdev;
1867 int length;
1868 struct usb_request *resp = dev->stat_req;
1869
1870 /* in case RNDIS calls this after disconnect */
1871 if (!dev->status) {
1872 debug("status ENODEV\n");
1873 return -ENODEV;
1874 }
1875
1876 /* in case queue length > 1 */
1877 if (resp->context) {
1878 resp = usb_ep_alloc_request(dev->status_ep, GFP_ATOMIC);
1879 if (!resp)
1880 return -ENOMEM;
1881 resp->buf = rndis_resp_buf;
1882 }
1883
1884 /*
1885 * Send RNDIS RESPONSE_AVAILABLE notification;
1886 * USB_CDC_NOTIFY_RESPONSE_AVAILABLE should work too
1887 */
1888 resp->length = 8;
1889 resp->complete = rndis_control_ack_complete;
1890 resp->context = dev;
1891
1892 *((__le32 *) resp->buf) = __constant_cpu_to_le32(1);
1893 *((__le32 *) (resp->buf + 4)) = __constant_cpu_to_le32(0);
1894
1895 length = usb_ep_queue(dev->status_ep, resp, GFP_ATOMIC);
1896 if (length < 0) {
1897 resp->status = 0;
1898 rndis_control_ack_complete(dev->status_ep, resp);
1899 }
1900
1901 return 0;
1902}
1903
1904#else
1905
1906#define rndis_control_ack NULL
1907
1908#endif /* RNDIS */
1909
1910static void eth_start(struct eth_dev *dev, gfp_t gfp_flags)
1911{
1912 if (rndis_active(dev)) {
1913 rndis_set_param_medium(dev->rndis_config,
1914 NDIS_MEDIUM_802_3,
1915 BITRATE(dev->gadget)/100);
1916 rndis_signal_connect(dev->rndis_config);
1917 }
1918}
1919
1920static int eth_stop(struct eth_dev *dev)
1921{
Vitaly Kuzmicheve4ae6662011-02-11 18:18:35 +03001922#ifdef RNDIS_COMPLETE_SIGNAL_DISCONNECT
1923 unsigned long ts;
1924 unsigned long timeout = CONFIG_SYS_HZ; /* 1 sec to stop RNDIS */
1925#endif
1926
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001927 if (rndis_active(dev)) {
1928 rndis_set_param_medium(dev->rndis_config, NDIS_MEDIUM_802_3, 0);
1929 rndis_signal_disconnect(dev->rndis_config);
1930
Vitaly Kuzmicheve4ae6662011-02-11 18:18:35 +03001931#ifdef RNDIS_COMPLETE_SIGNAL_DISCONNECT
1932 /* Wait until host receives OID_GEN_MEDIA_CONNECT_STATUS */
1933 ts = get_timer(0);
1934 while (get_timer(ts) < timeout)
1935 usb_gadget_handle_interrupts();
1936#endif
1937
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001938 rndis_uninit(dev->rndis_config);
1939 dev->rndis = 0;
1940 }
1941
1942 return 0;
1943}
1944
1945/*-------------------------------------------------------------------------*/
1946
Remy Bohmer23cd1382009-07-29 18:18:43 +02001947static int is_eth_addr_valid(char *str)
1948{
1949 if (strlen(str) == 17) {
1950 int i;
1951 char *p, *q;
1952 uchar ea[6];
1953
1954 /* see if it looks like an ethernet address */
1955
1956 p = str;
1957
1958 for (i = 0; i < 6; i++) {
1959 char term = (i == 5 ? '\0' : ':');
1960
1961 ea[i] = simple_strtol(p, &q, 16);
1962
1963 if ((q - p) != 2 || *q++ != term)
1964 break;
1965
1966 p = q;
1967 }
1968
Tom Rini57a87a22012-10-31 13:30:41 +00001969 /* Now check the contents. */
1970 return is_valid_ether_addr(ea);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001971 }
1972 return 0;
1973}
1974
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001975static u8 nibble(unsigned char c)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001976{
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001977 if (likely(isdigit(c)))
Remy Bohmer23cd1382009-07-29 18:18:43 +02001978 return c - '0';
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001979 c = toupper(c);
1980 if (likely(isxdigit(c)))
Remy Bohmer23cd1382009-07-29 18:18:43 +02001981 return 10 + c - 'A';
1982 return 0;
1983}
1984
1985static int get_ether_addr(const char *str, u8 *dev_addr)
1986{
1987 if (str) {
1988 unsigned i;
1989
1990 for (i = 0; i < 6; i++) {
1991 unsigned char num;
1992
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001993 if ((*str == '.') || (*str == ':'))
Remy Bohmer23cd1382009-07-29 18:18:43 +02001994 str++;
1995 num = nibble(*str++) << 4;
1996 num |= (nibble(*str++));
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001997 dev_addr[i] = num;
Remy Bohmer23cd1382009-07-29 18:18:43 +02001998 }
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001999 if (is_valid_ether_addr(dev_addr))
Remy Bohmer23cd1382009-07-29 18:18:43 +02002000 return 0;
2001 }
2002 return 1;
2003}
2004
2005static int eth_bind(struct usb_gadget *gadget)
2006{
2007 struct eth_dev *dev = &l_ethdev;
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002008 u8 cdc = 1, zlp = 1, rndis = 1;
Remy Bohmer23cd1382009-07-29 18:18:43 +02002009 struct usb_ep *in_ep, *out_ep, *status_ep = NULL;
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002010 int status = -ENOMEM;
Remy Bohmer23cd1382009-07-29 18:18:43 +02002011 int gcnum;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002012 u8 tmp[7];
Remy Bohmer23cd1382009-07-29 18:18:43 +02002013
2014 /* these flags are only ever cleared; compiler take note */
Lukasz Dalek2bb37882012-10-02 17:04:31 +02002015#ifndef CONFIG_USB_ETH_CDC
Remy Bohmer23cd1382009-07-29 18:18:43 +02002016 cdc = 0;
2017#endif
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002018#ifndef CONFIG_USB_ETH_RNDIS
2019 rndis = 0;
2020#endif
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002021 /*
2022 * Because most host side USB stacks handle CDC Ethernet, that
Remy Bohmer23cd1382009-07-29 18:18:43 +02002023 * standard protocol is _strongly_ preferred for interop purposes.
2024 * (By everyone except Microsoft.)
2025 */
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002026 if (gadget_is_pxa(gadget)) {
Remy Bohmer23cd1382009-07-29 18:18:43 +02002027 /* pxa doesn't support altsettings */
2028 cdc = 0;
2029 } else if (gadget_is_musbhdrc(gadget)) {
2030 /* reduce tx dma overhead by avoiding special cases */
2031 zlp = 0;
2032 } else if (gadget_is_sh(gadget)) {
2033 /* sh doesn't support multiple interfaces or configs */
2034 cdc = 0;
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002035 rndis = 0;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002036 } else if (gadget_is_sa1100(gadget)) {
Remy Bohmer23cd1382009-07-29 18:18:43 +02002037 /* hardware can't write zlps */
2038 zlp = 0;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002039 /*
2040 * sa1100 CAN do CDC, without status endpoint ... we use
Remy Bohmer23cd1382009-07-29 18:18:43 +02002041 * non-CDC to be compatible with ARM Linux-2.4 "usb-eth".
2042 */
2043 cdc = 0;
2044 }
2045
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002046 gcnum = usb_gadget_controller_number(gadget);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002047 if (gcnum >= 0)
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002048 device_desc.bcdDevice = cpu_to_le16(0x0300 + gcnum);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002049 else {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002050 /*
2051 * can't assume CDC works. don't want to default to
Remy Bohmer23cd1382009-07-29 18:18:43 +02002052 * anything less functional on CDC-capable hardware,
2053 * so we fail in this case.
2054 */
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04002055 error("controller '%s' not recognized",
Remy Bohmer23cd1382009-07-29 18:18:43 +02002056 gadget->name);
2057 return -ENODEV;
2058 }
2059
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002060 /*
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002061 * If there's an RNDIS configuration, that's what Windows wants to
2062 * be using ... so use these product IDs here and in the "linux.inf"
2063 * needed to install MSFT drivers. Current Linux kernels will use
2064 * the second configuration if it's CDC Ethernet, and need some help
2065 * to choose the right configuration otherwise.
2066 */
2067 if (rndis) {
2068#if defined(CONFIG_USB_RNDIS_VENDOR_ID) && defined(CONFIG_USB_RNDIS_PRODUCT_ID)
2069 device_desc.idVendor =
2070 __constant_cpu_to_le16(CONFIG_USB_RNDIS_VENDOR_ID);
2071 device_desc.idProduct =
2072 __constant_cpu_to_le16(CONFIG_USB_RNDIS_PRODUCT_ID);
2073#else
2074 device_desc.idVendor =
2075 __constant_cpu_to_le16(RNDIS_VENDOR_NUM);
2076 device_desc.idProduct =
2077 __constant_cpu_to_le16(RNDIS_PRODUCT_NUM);
2078#endif
2079 sprintf(product_desc, "RNDIS/%s", driver_desc);
2080
2081 /*
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002082 * CDC subset ... recognized by Linux since 2.4.10, but Windows
Remy Bohmer23cd1382009-07-29 18:18:43 +02002083 * drivers aren't widely available. (That may be improved by
2084 * supporting one submode of the "SAFE" variant of MDLM.)
2085 */
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002086 } else {
Remy Bohmer23cd1382009-07-29 18:18:43 +02002087#if defined(CONFIG_USB_CDC_VENDOR_ID) && defined(CONFIG_USB_CDC_PRODUCT_ID)
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002088 device_desc.idVendor = cpu_to_le16(CONFIG_USB_CDC_VENDOR_ID);
2089 device_desc.idProduct = cpu_to_le16(CONFIG_USB_CDC_PRODUCT_ID);
2090#else
2091 if (!cdc) {
2092 device_desc.idVendor =
2093 __constant_cpu_to_le16(SIMPLE_VENDOR_NUM);
2094 device_desc.idProduct =
2095 __constant_cpu_to_le16(SIMPLE_PRODUCT_NUM);
2096 }
Remy Bohmer23cd1382009-07-29 18:18:43 +02002097#endif
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002098 }
2099 /* support optional vendor/distro customization */
Remy Bohmer23cd1382009-07-29 18:18:43 +02002100 if (bcdDevice)
2101 device_desc.bcdDevice = cpu_to_le16(bcdDevice);
2102 if (iManufacturer)
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002103 strlcpy(manufacturer, iManufacturer, sizeof manufacturer);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002104 if (iProduct)
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002105 strlcpy(product_desc, iProduct, sizeof product_desc);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002106 if (iSerialNumber) {
2107 device_desc.iSerialNumber = STRING_SERIALNUMBER,
Vitaly Kuzmichev2e12abe2010-08-13 17:00:45 +04002108 strlcpy(serial_number, iSerialNumber, sizeof serial_number);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002109 }
2110
2111 /* all we really need is bulk IN/OUT */
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002112 usb_ep_autoconfig_reset(gadget);
2113 in_ep = usb_ep_autoconfig(gadget, &fs_source_desc);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002114 if (!in_ep) {
2115autoconf_fail:
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04002116 error("can't autoconfigure on %s\n",
Remy Bohmer23cd1382009-07-29 18:18:43 +02002117 gadget->name);
2118 return -ENODEV;
2119 }
2120 in_ep->driver_data = in_ep; /* claim */
2121
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002122 out_ep = usb_ep_autoconfig(gadget, &fs_sink_desc);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002123 if (!out_ep)
2124 goto autoconf_fail;
2125 out_ep->driver_data = out_ep; /* claim */
2126
Lukasz Dalek2bb37882012-10-02 17:04:31 +02002127#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002128 /*
2129 * CDC Ethernet control interface doesn't require a status endpoint.
Remy Bohmer23cd1382009-07-29 18:18:43 +02002130 * Since some hosts expect one, try to allocate one anyway.
2131 */
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002132 if (cdc || rndis) {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002133 status_ep = usb_ep_autoconfig(gadget, &fs_status_desc);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002134 if (status_ep) {
2135 status_ep->driver_data = status_ep; /* claim */
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002136 } else if (rndis) {
2137 error("can't run RNDIS on %s", gadget->name);
2138 return -ENODEV;
Lukasz Dalek2bb37882012-10-02 17:04:31 +02002139#ifdef CONFIG_USB_ETH_CDC
Remy Bohmer23cd1382009-07-29 18:18:43 +02002140 } else if (cdc) {
2141 control_intf.bNumEndpoints = 0;
2142 /* FIXME remove endpoint from descriptor list */
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002143#endif
Remy Bohmer23cd1382009-07-29 18:18:43 +02002144 }
2145 }
2146#endif
2147
2148 /* one config: cdc, else minimal subset */
2149 if (!cdc) {
2150 eth_config.bNumInterfaces = 1;
2151 eth_config.iConfiguration = STRING_SUBSET;
2152
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002153 /*
2154 * use functions to set these up, in case we're built to work
Remy Bohmer23cd1382009-07-29 18:18:43 +02002155 * with multiple controllers and must override CDC Ethernet.
2156 */
2157 fs_subset_descriptors();
2158 hs_subset_descriptors();
2159 }
2160
2161 device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002162 usb_gadget_set_selfpowered(gadget);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002163
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002164 /* For now RNDIS is always a second config */
2165 if (rndis)
2166 device_desc.bNumConfigurations = 2;
2167
Remy Bohmer23cd1382009-07-29 18:18:43 +02002168 if (gadget_is_dualspeed(gadget)) {
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002169 if (rndis)
2170 dev_qualifier.bNumConfigurations = 2;
2171 else if (!cdc)
Remy Bohmer23cd1382009-07-29 18:18:43 +02002172 dev_qualifier.bDeviceClass = USB_CLASS_VENDOR_SPEC;
2173
2174 /* assumes ep0 uses the same value for both speeds ... */
2175 dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0;
2176
2177 /* and that all endpoints are dual-speed */
2178 hs_source_desc.bEndpointAddress =
2179 fs_source_desc.bEndpointAddress;
2180 hs_sink_desc.bEndpointAddress =
2181 fs_sink_desc.bEndpointAddress;
Lukasz Dalek2bb37882012-10-02 17:04:31 +02002182#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Remy Bohmer23cd1382009-07-29 18:18:43 +02002183 if (status_ep)
2184 hs_status_desc.bEndpointAddress =
2185 fs_status_desc.bEndpointAddress;
2186#endif
2187 }
2188
2189 if (gadget_is_otg(gadget)) {
2190 otg_descriptor.bmAttributes |= USB_OTG_HNP,
2191 eth_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
2192 eth_config.bMaxPower = 4;
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002193#ifdef CONFIG_USB_ETH_RNDIS
2194 rndis_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
2195 rndis_config.bMaxPower = 4;
2196#endif
Remy Bohmer23cd1382009-07-29 18:18:43 +02002197 }
2198
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002199
2200 /* network device setup */
Remy Bohmer23cd1382009-07-29 18:18:43 +02002201 dev->net = &l_netdev;
Remy Bohmer23cd1382009-07-29 18:18:43 +02002202
2203 dev->cdc = cdc;
2204 dev->zlp = zlp;
2205
2206 dev->in_ep = in_ep;
2207 dev->out_ep = out_ep;
2208 dev->status_ep = status_ep;
2209
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002210 /*
2211 * Module params for these addresses should come from ID proms.
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002212 * The host side address is used with CDC and RNDIS, and commonly
Remy Bohmer23cd1382009-07-29 18:18:43 +02002213 * ends up in a persistent config database. It's not clear if
2214 * host side code for the SAFE thing cares -- its original BLAN
2215 * thing didn't, Sharp never assigned those addresses on Zaurii.
2216 */
2217 get_ether_addr(dev_addr, dev->net->enetaddr);
2218
2219 memset(tmp, 0, sizeof(tmp));
2220 memcpy(tmp, dev->net->enetaddr, sizeof(dev->net->enetaddr));
2221
2222 get_ether_addr(host_addr, dev->host_mac);
2223
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002224 sprintf(ethaddr, "%02X%02X%02X%02X%02X%02X",
2225 dev->host_mac[0], dev->host_mac[1],
2226 dev->host_mac[2], dev->host_mac[3],
2227 dev->host_mac[4], dev->host_mac[5]);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002228
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002229 if (rndis) {
2230 status = rndis_init();
2231 if (status < 0) {
2232 error("can't init RNDIS, %d", status);
2233 goto fail;
2234 }
Remy Bohmer23cd1382009-07-29 18:18:43 +02002235 }
2236
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002237 /*
2238 * use PKTSIZE (or aligned... from u-boot) and set
2239 * wMaxSegmentSize accordingly
2240 */
Remy Bohmer23cd1382009-07-29 18:18:43 +02002241 dev->mtu = PKTSIZE_ALIGN; /* RNDIS does not like this, only 1514, TODO*/
2242
2243 /* preallocate control message data and buffer */
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002244 dev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002245 if (!dev->req)
2246 goto fail;
2247 dev->req->buf = control_req;
2248 dev->req->complete = eth_setup_complete;
2249
2250 /* ... and maybe likewise for status transfer */
Lukasz Dalek2bb37882012-10-02 17:04:31 +02002251#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Remy Bohmer23cd1382009-07-29 18:18:43 +02002252 if (dev->status_ep) {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002253 dev->stat_req = usb_ep_alloc_request(dev->status_ep,
2254 GFP_KERNEL);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002255 if (!dev->stat_req) {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002256 usb_ep_free_request(dev->status_ep, dev->req);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002257
2258 goto fail;
2259 }
Vitaly Kuzmichevdf559c12010-08-13 17:01:06 +04002260 dev->stat_req->buf = status_req;
Remy Bohmer23cd1382009-07-29 18:18:43 +02002261 dev->stat_req->context = NULL;
2262 }
2263#endif
2264
2265 /* finish hookup to lower layer ... */
2266 dev->gadget = gadget;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002267 set_gadget_data(gadget, dev);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002268 gadget->ep0->driver_data = dev;
2269
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002270 /*
2271 * two kinds of host-initiated state changes:
Remy Bohmer23cd1382009-07-29 18:18:43 +02002272 * - iff DATA transfer is active, carrier is "on"
2273 * - tx queueing enabled if open *and* carrier is "on"
2274 */
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002275
2276 printf("using %s, OUT %s IN %s%s%s\n", gadget->name,
2277 out_ep->name, in_ep->name,
2278 status_ep ? " STATUS " : "",
2279 status_ep ? status_ep->name : ""
2280 );
2281 printf("MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
2282 dev->net->enetaddr[0], dev->net->enetaddr[1],
2283 dev->net->enetaddr[2], dev->net->enetaddr[3],
2284 dev->net->enetaddr[4], dev->net->enetaddr[5]);
2285
2286 if (cdc || rndis)
2287 printf("HOST MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
2288 dev->host_mac[0], dev->host_mac[1],
2289 dev->host_mac[2], dev->host_mac[3],
2290 dev->host_mac[4], dev->host_mac[5]);
2291
2292 if (rndis) {
2293 u32 vendorID = 0;
2294
2295 /* FIXME RNDIS vendor id == "vendor NIC code" == ? */
2296
2297 dev->rndis_config = rndis_register(rndis_control_ack);
2298 if (dev->rndis_config < 0) {
2299fail0:
2300 eth_unbind(gadget);
2301 debug("RNDIS setup failed\n");
2302 status = -ENODEV;
2303 goto fail;
2304 }
2305
2306 /* these set up a lot of the OIDs that RNDIS needs */
2307 rndis_set_host_mac(dev->rndis_config, dev->host_mac);
2308 if (rndis_set_param_dev(dev->rndis_config, dev->net, dev->mtu,
2309 &dev->stats, &dev->cdc_filter))
2310 goto fail0;
2311 if (rndis_set_param_vendor(dev->rndis_config, vendorID,
2312 manufacturer))
2313 goto fail0;
2314 if (rndis_set_param_medium(dev->rndis_config,
2315 NDIS_MEDIUM_802_3, 0))
2316 goto fail0;
2317 printf("RNDIS ready\n");
2318 }
Remy Bohmer23cd1382009-07-29 18:18:43 +02002319 return 0;
2320
2321fail:
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002322 error("%s failed, status = %d", __func__, status);
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002323 eth_unbind(gadget);
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002324 return status;
Remy Bohmer23cd1382009-07-29 18:18:43 +02002325}
2326
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002327/*-------------------------------------------------------------------------*/
2328
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002329static int usb_eth_init(struct eth_device *netdev, bd_t *bd)
Remy Bohmer23cd1382009-07-29 18:18:43 +02002330{
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002331 struct eth_dev *dev = &l_ethdev;
Remy Bohmer23cd1382009-07-29 18:18:43 +02002332 struct usb_gadget *gadget;
2333 unsigned long ts;
2334 unsigned long timeout = USB_CONNECT_TIMEOUT;
2335
2336 if (!netdev) {
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04002337 error("received NULL ptr");
Remy Bohmer23cd1382009-07-29 18:18:43 +02002338 goto fail;
2339 }
Vitaly Kuzmichev58939fc2010-12-28 16:59:32 +03002340
2341 /* Configure default mac-addresses for the USB ethernet device */
2342#ifdef CONFIG_USBNET_DEV_ADDR
2343 strlcpy(dev_addr, CONFIG_USBNET_DEV_ADDR, sizeof(dev_addr));
2344#endif
2345#ifdef CONFIG_USBNET_HOST_ADDR
2346 strlcpy(host_addr, CONFIG_USBNET_HOST_ADDR, sizeof(host_addr));
2347#endif
2348 /* Check if the user overruled the MAC addresses */
2349 if (getenv("usbnet_devaddr"))
2350 strlcpy(dev_addr, getenv("usbnet_devaddr"),
2351 sizeof(dev_addr));
2352
2353 if (getenv("usbnet_hostaddr"))
2354 strlcpy(host_addr, getenv("usbnet_hostaddr"),
2355 sizeof(host_addr));
2356
2357 if (!is_eth_addr_valid(dev_addr)) {
2358 error("Need valid 'usbnet_devaddr' to be set");
2359 goto fail;
2360 }
2361 if (!is_eth_addr_valid(host_addr)) {
2362 error("Need valid 'usbnet_hostaddr' to be set");
2363 goto fail;
2364 }
2365
Lei Wen988ee3e2010-12-01 23:43:43 +08002366 if (usb_gadget_register_driver(&eth_driver) < 0)
2367 goto fail;
Remy Bohmer23cd1382009-07-29 18:18:43 +02002368
2369 dev->network_started = 0;
Remy Bohmer23cd1382009-07-29 18:18:43 +02002370
2371 packet_received = 0;
2372 packet_sent = 0;
2373
2374 gadget = dev->gadget;
2375 usb_gadget_connect(gadget);
2376
2377 if (getenv("cdc_connect_timeout"))
2378 timeout = simple_strtoul(getenv("cdc_connect_timeout"),
2379 NULL, 10) * CONFIG_SYS_HZ;
2380 ts = get_timer(0);
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002381 while (!l_ethdev.network_started) {
Remy Bohmer23cd1382009-07-29 18:18:43 +02002382 /* Handle control-c and timeouts */
2383 if (ctrlc() || (get_timer(ts) > timeout)) {
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04002384 error("The remote end did not respond in time.");
Remy Bohmer23cd1382009-07-29 18:18:43 +02002385 goto fail;
2386 }
2387 usb_gadget_handle_interrupts();
2388 }
2389
Vitaly Kuzmichev98fae972010-09-22 13:13:56 +04002390 packet_received = 0;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002391 rx_submit(dev, dev->rx_req, 0);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002392 return 0;
2393fail:
2394 return -1;
2395}
2396
Joe Hershberger10cbe3b2012-05-22 18:36:19 +00002397static int usb_eth_send(struct eth_device *netdev, void *packet, int length)
Remy Bohmer23cd1382009-07-29 18:18:43 +02002398{
2399 int retval;
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002400 void *rndis_pkt = NULL;
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04002401 struct eth_dev *dev = &l_ethdev;
Vitaly Kuzmichevac5d32d2010-09-22 13:13:55 +04002402 struct usb_request *req = dev->tx_req;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002403 unsigned long ts;
2404 unsigned long timeout = USB_CONNECT_TIMEOUT;
Remy Bohmer23cd1382009-07-29 18:18:43 +02002405
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04002406 debug("%s:...\n", __func__);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002407
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002408 /* new buffer is needed to include RNDIS header */
2409 if (rndis_active(dev)) {
2410 rndis_pkt = malloc(length +
2411 sizeof(struct rndis_packet_msg_type));
2412 if (!rndis_pkt) {
2413 error("No memory to alloc RNDIS packet");
2414 goto drop;
2415 }
2416 rndis_add_hdr(rndis_pkt, length);
2417 memcpy(rndis_pkt + sizeof(struct rndis_packet_msg_type),
Joe Hershberger10cbe3b2012-05-22 18:36:19 +00002418 packet, length);
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002419 packet = rndis_pkt;
2420 length += sizeof(struct rndis_packet_msg_type);
2421 }
Joe Hershberger10cbe3b2012-05-22 18:36:19 +00002422 req->buf = packet;
Remy Bohmer23cd1382009-07-29 18:18:43 +02002423 req->context = NULL;
2424 req->complete = tx_complete;
2425
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002426 /*
2427 * use zlp framing on tx for strict CDC-Ether conformance,
Remy Bohmer23cd1382009-07-29 18:18:43 +02002428 * though any robust network rx path ignores extra padding.
2429 * and some hardware doesn't like to write zlps.
2430 */
2431 req->zero = 1;
2432 if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0)
2433 length++;
2434
2435 req->length = length;
2436#if 0
2437 /* throttle highspeed IRQ rate back slightly */
2438 if (gadget_is_dualspeed(dev->gadget))
2439 req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH)
2440 ? ((dev->tx_qlen % qmult) != 0) : 0;
2441#endif
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002442 dev->tx_qlen = 1;
2443 ts = get_timer(0);
2444 packet_sent = 0;
Remy Bohmer23cd1382009-07-29 18:18:43 +02002445
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002446 retval = usb_ep_queue(dev->in_ep, req, GFP_ATOMIC);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002447
2448 if (!retval)
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04002449 debug("%s: packet queued\n", __func__);
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002450 while (!packet_sent) {
2451 if (get_timer(ts) > timeout) {
2452 printf("timeout sending packets to usb ethernet\n");
2453 return -1;
2454 }
2455 usb_gadget_handle_interrupts();
Remy Bohmer23cd1382009-07-29 18:18:43 +02002456 }
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002457 if (rndis_pkt)
2458 free(rndis_pkt);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002459
2460 return 0;
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002461drop:
2462 dev->stats.tx_dropped++;
2463 return -ENOMEM;
Remy Bohmer23cd1382009-07-29 18:18:43 +02002464}
2465
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002466static int usb_eth_recv(struct eth_device *netdev)
Remy Bohmer23cd1382009-07-29 18:18:43 +02002467{
2468 struct eth_dev *dev = &l_ethdev;
2469
2470 usb_gadget_handle_interrupts();
2471
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002472 if (packet_received) {
2473 debug("%s: packet received\n", __func__);
2474 if (dev->rx_req) {
2475 NetReceive(NetRxPackets[0], dev->rx_req->length);
2476 packet_received = 0;
Remy Bohmer23cd1382009-07-29 18:18:43 +02002477
Vitaly Kuzmichevac5d32d2010-09-22 13:13:55 +04002478 rx_submit(dev, dev->rx_req, 0);
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002479 } else
2480 error("dev->rx_req invalid");
Remy Bohmer23cd1382009-07-29 18:18:43 +02002481 }
2482 return 0;
2483}
2484
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002485void usb_eth_halt(struct eth_device *netdev)
Remy Bohmer23cd1382009-07-29 18:18:43 +02002486{
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002487 struct eth_dev *dev = &l_ethdev;
Remy Bohmer23cd1382009-07-29 18:18:43 +02002488
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002489 if (!netdev) {
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04002490 error("received NULL ptr");
Remy Bohmer23cd1382009-07-29 18:18:43 +02002491 return;
2492 }
2493
Lei Wen988ee3e2010-12-01 23:43:43 +08002494 /* If the gadget not registered, simple return */
2495 if (!dev->gadget)
2496 return;
2497
Vitaly Kuzmicheve4ae6662011-02-11 18:18:35 +03002498 /*
2499 * Some USB controllers may need additional deinitialization here
2500 * before dropping pull-up (also due to hardware issues).
2501 * For example: unhandled interrupt with status stage started may
2502 * bring the controller to fully broken state (until board reset).
2503 * There are some variants to debug and fix such cases:
2504 * 1) In the case of RNDIS connection eth_stop can perform additional
2505 * interrupt handling. See RNDIS_COMPLETE_SIGNAL_DISCONNECT definition.
2506 * 2) 'pullup' callback in your UDC driver can be improved to perform
2507 * this deinitialization.
2508 */
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002509 eth_stop(dev);
2510
Remy Bohmer23cd1382009-07-29 18:18:43 +02002511 usb_gadget_disconnect(dev->gadget);
Vitaly Kuzmichevb3649f32011-02-11 18:18:31 +03002512
2513 /* Clear pending interrupt */
2514 if (dev->network_started) {
2515 usb_gadget_handle_interrupts();
2516 dev->network_started = 0;
2517 }
2518
Lei Wen988ee3e2010-12-01 23:43:43 +08002519 usb_gadget_unregister_driver(&eth_driver);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002520}
2521
2522static struct usb_gadget_driver eth_driver = {
2523 .speed = DEVSPEED,
2524
2525 .bind = eth_bind,
2526 .unbind = eth_unbind,
2527
2528 .setup = eth_setup,
2529 .disconnect = eth_disconnect,
2530
2531 .suspend = eth_suspend,
2532 .resume = eth_resume,
2533};
2534
2535int usb_eth_initialize(bd_t *bi)
2536{
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002537 struct eth_device *netdev = &l_netdev;
Remy Bohmer23cd1382009-07-29 18:18:43 +02002538
Vitaly Kuzmichev8f7aa832010-12-28 16:59:31 +03002539 strlcpy(netdev->name, USB_NET_NAME, sizeof(netdev->name));
Remy Bohmer23cd1382009-07-29 18:18:43 +02002540
2541 netdev->init = usb_eth_init;
2542 netdev->send = usb_eth_send;
2543 netdev->recv = usb_eth_recv;
2544 netdev->halt = usb_eth_halt;
2545
2546#ifdef CONFIG_MCAST_TFTP
2547 #error not supported
2548#endif
Remy Bohmer23cd1382009-07-29 18:18:43 +02002549 eth_register(netdev);
2550 return 0;
Remy Bohmer23cd1382009-07-29 18:18:43 +02002551}