blob: cc6cc1f32ae299425bd610cc446281771a722fb5 [file] [log] [blame]
Remy Bohmerdf063442009-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 Denkd79de1d2013-07-08 09:37:19 +02008 * SPDX-License-Identifier: GPL-2.0+
Remy Bohmerdf063442009-07-29 18:18:43 +02009 */
10
11#include <common.h>
12#include <asm/errno.h>
Vitaly Kuzmichev24fcb472011-02-11 18:18:32 +030013#include <linux/netdevice.h>
Remy Bohmerdf063442009-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 Kuzmichev216c4472011-02-11 18:18:34 +030018#include <malloc.h>
Remy Bohmerdf063442009-07-29 18:18:43 +020019#include <linux/ctype.h>
20
21#include "gadget_chips.h"
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +030022#include "rndis.h"
Remy Bohmerdf063442009-07-29 18:18:43 +020023
Vitaly Kuzmicheva36cff12010-12-28 16:59:31 +030024#define USB_NET_NAME "usb_ether"
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +040025
Remy Bohmerdf063442009-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 Bohmerdf063442009-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 Kuzmichev49ed8052010-09-13 18:37:11 +040082static const char shortname[] = "ether";
83static const char driver_desc[] = DRIVER_DESC;
Remy Bohmerdf063442009-07-29 18:18:43 +020084
85#define RX_EXTRA 20 /* guard against rx overflows */
86
Vitaly Kuzmichev216c4472011-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 Bohmerdf063442009-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 Kuzmichev737f6b22011-02-11 18:18:33 +0300102
103struct eth_dev {
104 struct usb_gadget *gadget;
105 struct usb_request *req; /* for control responses */
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300106 struct usb_request *stat_req; /* for cdc & rndis status */
Vitaly Kuzmichev737f6b22011-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 Kuzmichev216c4472011-02-11 18:18:34 +0300121 unsigned rndis:1;
Vitaly Kuzmichev737f6b22011-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 Kuzmichev216c4472011-02-11 18:18:34 +0300128 int rndis_config;
Vitaly Kuzmichev737f6b22011-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 Bohmerdf063442009-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 Dalek284c4cf2012-10-02 17:04:31 +0200149#if !defined(CONFIG_USB_ETH_SUBSET)
Remy Bohmerdf063442009-07-29 18:18:43 +0200150 return 1; /* only cdc possible */
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200151#elif !defined(CONFIG_USB_ETH_CDC)
Remy Bohmerdf063442009-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 Kuzmichev216c4472011-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 Bohmerdf063442009-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 Kuzmichevaec83b82010-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 Bohmerdf063442009-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 Bohmerdf063442009-07-29 18:18:43 +0200209/*-------------------------------------------------------------------------*/
210
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400211/*
212 * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
Remy Bohmerdf063442009-07-29 18:18:43 +0200213 * Instead: allocate your own, using normal USB-IF procedures.
214 */
215
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400216/*
217 * Thanks to NetChip Technologies for donating this product ID.
Remy Bohmerdf063442009-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 Kuzmichev49ed8052010-09-13 18:37:11 +0400223/*
224 * For hardware that can't talk CDC, we use the same vendor ID that
Remy Bohmerdf063442009-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 Kuzmichev216c4472011-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 */
Remy Bohmerdf063442009-07-29 18:18:43 +0200237
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400238/*
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300239 * 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 */
246
247/*
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400248 * Some systems will want different product identifers published in the
Remy Bohmerdf063442009-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 Kuzmichev216c4472011-02-11 18:18:34 +0300253/*
254 * Emulating them in eth_bind:
255 * static ushort idVendor;
256 * static ushort idProduct;
257 */
258
Remy Bohmerdf063442009-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 Kuzmichev216c4472011-02-11 18:18:34 +0300264
265/* These probably need to be configurable. */
266static ushort bcdDevice;
Remy Bohmerdf063442009-07-29 18:18:43 +0200267static char *iProduct;
268static char *iSerialNumber;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300269
Remy Bohmerdf063442009-07-29 18:18:43 +0200270static char dev_addr[18];
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300271
Remy Bohmerdf063442009-07-29 18:18:43 +0200272static char host_addr[18];
273
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300274
Remy Bohmerdf063442009-07-29 18:18:43 +0200275/*-------------------------------------------------------------------------*/
276
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400277/*
278 * USB DRIVER HOOKUP (to the hardware driver, below us), mostly
Remy Bohmerdf063442009-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 Kuzmichev216c4472011-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 Bohmerdf063442009-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 Kuzmichev216c4472011-02-11 18:18:34 +0300299#define STRING_RNDIS_CONTROL 6
Remy Bohmerdf063442009-07-29 18:18:43 +0200300#define STRING_CDC 7
301#define STRING_SUBSET 8
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300302#define STRING_RNDIS 9
Remy Bohmerdf063442009-07-29 18:18:43 +0200303#define STRING_SERIALNUMBER 10
304
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300305/* holds our biggest descriptor (or RNDIS response) */
Remy Bohmerdf063442009-07-29 18:18:43 +0200306#define USB_BUFSIZ 256
307
308/*
Vitaly Kuzmichev216c4472011-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 Bohmerdf063442009-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 Kuzmichev216c4472011-02-11 18:18:34 +0300320#define DEV_RNDIS_CONFIG_VALUE 2 /* rndis; optional */
Remy Bohmerdf063442009-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 Kuzmichev49ed8052010-09-13 18:37:11 +0400327 .bcdUSB = __constant_cpu_to_le16(0x0200),
Remy Bohmerdf063442009-07-29 18:18:43 +0200328
329 .bDeviceClass = USB_CLASS_COMM,
330 .bDeviceSubClass = 0,
331 .bDeviceProtocol = 0,
332
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400333 .idVendor = __constant_cpu_to_le16(CDC_VENDOR_NUM),
334 .idProduct = __constant_cpu_to_le16(CDC_PRODUCT_NUM),
Remy Bohmerdf063442009-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};
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300360
361#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
Remy Bohmerdf063442009-07-29 18:18:43 +0200375
376/*
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 Kuzmichev216c4472011-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 Bohmerdf063442009-07-29 18:18:43 +0200393 */
394
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200395#ifdef CONFIG_USB_ETH_CDC
Remy Bohmerdf063442009-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 Kuzmichev216c4472011-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 Bohmerdf063442009-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 Kuzmichev49ed8052010-09-13 18:37:11 +0400431 .bcdCDC = __constant_cpu_to_le16(0x0110),
Remy Bohmerdf063442009-07-29 18:18:43 +0200432};
433
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200434#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Remy Bohmerdf063442009-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 Kuzmichev216c4472011-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 Bohmerdf063442009-07-29 18:18:43 +0200467
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200468#ifndef CONFIG_USB_ETH_CDC
Remy Bohmerdf063442009-07-29 18:18:43 +0200469
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400470/*
471 * "SAFE" loosely follows CDC WMC MDLM, violating the spec in various
Remy Bohmerdf063442009-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 Kuzmichev49ed8052010-09-13 18:37:11 +0400488/*
489 * since "usb_cdc_mdlm_detail_desc" is a variable length structure, we
Remy Bohmerdf063442009-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 Bohmerdf063442009-07-29 18:18:43 +0200505static const struct usb_cdc_ether_desc ether_desc = {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400506 .bLength = sizeof(ether_desc),
Remy Bohmerdf063442009-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 Kuzmichev49ed8052010-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 Bohmerdf063442009-07-29 18:18:43 +0200515 .bNumberPowerFilters = 0,
516};
517
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200518#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Remy Bohmerdf063442009-07-29 18:18:43 +0200519
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400520/*
521 * include the status endpoint if we can, even where it's optional.
Remy Bohmerdf063442009-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 Kuzmichev216c4472011-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 Bohmerdf063442009-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 Kuzmichev49ed8052010-09-13 18:37:11 +0400545 .wMaxPacketSize = __constant_cpu_to_le16(STATUS_BYTECOUNT),
Remy Bohmerdf063442009-07-29 18:18:43 +0200546 .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC,
547};
548#endif
549
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200550#ifdef CONFIG_USB_ETH_CDC
Remy Bohmerdf063442009-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 Kuzmichev216c4472011-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 Dalek284c4cf2012-10-02 17:04:31 +0200605#ifdef CONFIG_USB_ETH_SUBSET
Remy Bohmerdf063442009-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 Bohmerdf063442009-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,
Troy Kiskydbadc542013-09-25 18:41:04 -0700638 .wMaxPacketSize = __constant_cpu_to_le16(64),
Remy Bohmerdf063442009-07-29 18:18:43 +0200639};
640
641static struct usb_endpoint_descriptor
642fs_sink_desc = {
643 .bLength = USB_DT_ENDPOINT_SIZE,
644 .bDescriptorType = USB_DT_ENDPOINT,
645
646 .bEndpointAddress = USB_DIR_OUT,
647 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Troy Kiskydbadc542013-09-25 18:41:04 -0700648 .wMaxPacketSize = __constant_cpu_to_le16(64),
Remy Bohmerdf063442009-07-29 18:18:43 +0200649};
650
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400651static const struct usb_descriptor_header *fs_eth_function[11] = {
Remy Bohmerdf063442009-07-29 18:18:43 +0200652 (struct usb_descriptor_header *) &otg_descriptor,
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200653#ifdef CONFIG_USB_ETH_CDC
Remy Bohmerdf063442009-07-29 18:18:43 +0200654 /* "cdc" mode descriptors */
655 (struct usb_descriptor_header *) &control_intf,
656 (struct usb_descriptor_header *) &header_desc,
657 (struct usb_descriptor_header *) &union_desc,
658 (struct usb_descriptor_header *) &ether_desc,
659 /* NOTE: status endpoint may need to be removed */
660 (struct usb_descriptor_header *) &fs_status_desc,
661 /* data interface, with altsetting */
662 (struct usb_descriptor_header *) &data_nop_intf,
663 (struct usb_descriptor_header *) &data_intf,
664 (struct usb_descriptor_header *) &fs_source_desc,
665 (struct usb_descriptor_header *) &fs_sink_desc,
666 NULL,
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200667#endif /* CONFIG_USB_ETH_CDC */
Remy Bohmerdf063442009-07-29 18:18:43 +0200668};
669
670static inline void fs_subset_descriptors(void)
671{
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200672#ifdef CONFIG_USB_ETH_SUBSET
Remy Bohmerdf063442009-07-29 18:18:43 +0200673 /* behavior is "CDC Subset"; extra descriptors say "SAFE" */
674 fs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
675 fs_eth_function[2] = (struct usb_descriptor_header *) &header_desc;
676 fs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc;
677 fs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc;
678 fs_eth_function[5] = (struct usb_descriptor_header *) &ether_desc;
679 fs_eth_function[6] = (struct usb_descriptor_header *) &fs_source_desc;
680 fs_eth_function[7] = (struct usb_descriptor_header *) &fs_sink_desc;
681 fs_eth_function[8] = NULL;
682#else
683 fs_eth_function[1] = NULL;
684#endif
685}
686
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300687#ifdef CONFIG_USB_ETH_RNDIS
688static const struct usb_descriptor_header *fs_rndis_function[] = {
689 (struct usb_descriptor_header *) &otg_descriptor,
690 /* control interface matches ACM, not Ethernet */
691 (struct usb_descriptor_header *) &rndis_control_intf,
692 (struct usb_descriptor_header *) &header_desc,
693 (struct usb_descriptor_header *) &call_mgmt_descriptor,
694 (struct usb_descriptor_header *) &acm_descriptor,
695 (struct usb_descriptor_header *) &union_desc,
696 (struct usb_descriptor_header *) &fs_status_desc,
697 /* data interface has no altsetting */
698 (struct usb_descriptor_header *) &rndis_data_intf,
699 (struct usb_descriptor_header *) &fs_source_desc,
700 (struct usb_descriptor_header *) &fs_sink_desc,
701 NULL,
702};
703#endif
704
Remy Bohmerdf063442009-07-29 18:18:43 +0200705/*
706 * usb 2.0 devices need to expose both high speed and full speed
707 * descriptors, unless they only run at full speed.
708 */
709
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200710#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Remy Bohmerdf063442009-07-29 18:18:43 +0200711static struct usb_endpoint_descriptor
712hs_status_desc = {
713 .bLength = USB_DT_ENDPOINT_SIZE,
714 .bDescriptorType = USB_DT_ENDPOINT,
715
716 .bmAttributes = USB_ENDPOINT_XFER_INT,
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400717 .wMaxPacketSize = __constant_cpu_to_le16(STATUS_BYTECOUNT),
Remy Bohmerdf063442009-07-29 18:18:43 +0200718 .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4,
719};
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200720#endif /* CONFIG_USB_ETH_CDC */
Remy Bohmerdf063442009-07-29 18:18:43 +0200721
722static struct usb_endpoint_descriptor
723hs_source_desc = {
724 .bLength = USB_DT_ENDPOINT_SIZE,
725 .bDescriptorType = USB_DT_ENDPOINT,
726
727 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400728 .wMaxPacketSize = __constant_cpu_to_le16(512),
Remy Bohmerdf063442009-07-29 18:18:43 +0200729};
730
731static struct usb_endpoint_descriptor
732hs_sink_desc = {
733 .bLength = USB_DT_ENDPOINT_SIZE,
734 .bDescriptorType = USB_DT_ENDPOINT,
735
736 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400737 .wMaxPacketSize = __constant_cpu_to_le16(512),
Remy Bohmerdf063442009-07-29 18:18:43 +0200738};
739
740static struct usb_qualifier_descriptor
741dev_qualifier = {
742 .bLength = sizeof dev_qualifier,
743 .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
744
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400745 .bcdUSB = __constant_cpu_to_le16(0x0200),
Remy Bohmerdf063442009-07-29 18:18:43 +0200746 .bDeviceClass = USB_CLASS_COMM,
747
748 .bNumConfigurations = 1,
749};
750
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400751static const struct usb_descriptor_header *hs_eth_function[11] = {
Remy Bohmerdf063442009-07-29 18:18:43 +0200752 (struct usb_descriptor_header *) &otg_descriptor,
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200753#ifdef CONFIG_USB_ETH_CDC
Remy Bohmerdf063442009-07-29 18:18:43 +0200754 /* "cdc" mode descriptors */
755 (struct usb_descriptor_header *) &control_intf,
756 (struct usb_descriptor_header *) &header_desc,
757 (struct usb_descriptor_header *) &union_desc,
758 (struct usb_descriptor_header *) &ether_desc,
759 /* NOTE: status endpoint may need to be removed */
760 (struct usb_descriptor_header *) &hs_status_desc,
761 /* data interface, with altsetting */
762 (struct usb_descriptor_header *) &data_nop_intf,
763 (struct usb_descriptor_header *) &data_intf,
764 (struct usb_descriptor_header *) &hs_source_desc,
765 (struct usb_descriptor_header *) &hs_sink_desc,
766 NULL,
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200767#endif /* CONFIG_USB_ETH_CDC */
Remy Bohmerdf063442009-07-29 18:18:43 +0200768};
769
770static inline void hs_subset_descriptors(void)
771{
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200772#ifdef CONFIG_USB_ETH_SUBSET
Remy Bohmerdf063442009-07-29 18:18:43 +0200773 /* behavior is "CDC Subset"; extra descriptors say "SAFE" */
774 hs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
775 hs_eth_function[2] = (struct usb_descriptor_header *) &header_desc;
776 hs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc;
777 hs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc;
778 hs_eth_function[5] = (struct usb_descriptor_header *) &ether_desc;
779 hs_eth_function[6] = (struct usb_descriptor_header *) &hs_source_desc;
780 hs_eth_function[7] = (struct usb_descriptor_header *) &hs_sink_desc;
781 hs_eth_function[8] = NULL;
782#else
783 hs_eth_function[1] = NULL;
784#endif
785}
786
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300787#ifdef CONFIG_USB_ETH_RNDIS
788static const struct usb_descriptor_header *hs_rndis_function[] = {
789 (struct usb_descriptor_header *) &otg_descriptor,
790 /* control interface matches ACM, not Ethernet */
791 (struct usb_descriptor_header *) &rndis_control_intf,
792 (struct usb_descriptor_header *) &header_desc,
793 (struct usb_descriptor_header *) &call_mgmt_descriptor,
794 (struct usb_descriptor_header *) &acm_descriptor,
795 (struct usb_descriptor_header *) &union_desc,
796 (struct usb_descriptor_header *) &hs_status_desc,
797 /* data interface has no altsetting */
798 (struct usb_descriptor_header *) &rndis_data_intf,
799 (struct usb_descriptor_header *) &hs_source_desc,
800 (struct usb_descriptor_header *) &hs_sink_desc,
801 NULL,
802};
803#endif
804
805
Remy Bohmerdf063442009-07-29 18:18:43 +0200806/* maxpacket and other transfer characteristics vary by speed. */
807static inline struct usb_endpoint_descriptor *
808ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *hs,
809 struct usb_endpoint_descriptor *fs)
810{
811 if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
812 return hs;
813 return fs;
814}
815
Remy Bohmerdf063442009-07-29 18:18:43 +0200816/*-------------------------------------------------------------------------*/
817
818/* descriptors that are built on-demand */
819
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400820static char manufacturer[50];
821static char product_desc[40] = DRIVER_DESC;
822static char serial_number[20];
Remy Bohmerdf063442009-07-29 18:18:43 +0200823
824/* address that the host will use ... usually assigned at random */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400825static char ethaddr[2 * ETH_ALEN + 1];
Remy Bohmerdf063442009-07-29 18:18:43 +0200826
827/* static strings, in UTF-8 */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400828static struct usb_string strings[] = {
Remy Bohmerdf063442009-07-29 18:18:43 +0200829 { STRING_MANUFACTURER, manufacturer, },
830 { STRING_PRODUCT, product_desc, },
831 { STRING_SERIALNUMBER, serial_number, },
832 { STRING_DATA, "Ethernet Data", },
833 { STRING_ETHADDR, ethaddr, },
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200834#ifdef CONFIG_USB_ETH_CDC
Remy Bohmerdf063442009-07-29 18:18:43 +0200835 { STRING_CDC, "CDC Ethernet", },
836 { STRING_CONTROL, "CDC Communications Control", },
837#endif
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200838#ifdef CONFIG_USB_ETH_SUBSET
Remy Bohmerdf063442009-07-29 18:18:43 +0200839 { STRING_SUBSET, "CDC Ethernet Subset", },
840#endif
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300841#ifdef CONFIG_USB_ETH_RNDIS
842 { STRING_RNDIS, "RNDIS", },
843 { STRING_RNDIS_CONTROL, "RNDIS Communications Control", },
844#endif
Remy Bohmerdf063442009-07-29 18:18:43 +0200845 { } /* end of list */
846};
847
848static struct usb_gadget_strings stringtab = {
849 .language = 0x0409, /* en-us */
850 .strings = strings,
851};
852
Remy Bohmerdf063442009-07-29 18:18:43 +0200853/*============================================================================*/
Joel Fernandes00b78392013-09-04 18:55:14 -0500854DEFINE_CACHE_ALIGN_BUFFER(u8, control_req, USB_BUFSIZ);
855
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200856#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Joel Fernandes00b78392013-09-04 18:55:14 -0500857DEFINE_CACHE_ALIGN_BUFFER(u8, status_req, STATUS_BYTECOUNT);
Lukasz Dalek6ca42dd2012-10-02 17:04:29 +0200858#endif
Remy Bohmerdf063442009-07-29 18:18:43 +0200859
860
Remy Bohmerdf063442009-07-29 18:18:43 +0200861/**
862 * strlcpy - Copy a %NUL terminated string into a sized buffer
863 * @dest: Where to copy the string to
864 * @src: Where to copy the string from
865 * @size: size of destination buffer
866 *
867 * Compatible with *BSD: the result is always a valid
868 * NUL-terminated string that fits in the buffer (unless,
869 * of course, the buffer size is zero). It does not pad
870 * out the result like strncpy() does.
871 */
872size_t strlcpy(char *dest, const char *src, size_t size)
873{
874 size_t ret = strlen(src);
875
876 if (size) {
877 size_t len = (ret >= size) ? size - 1 : ret;
878 memcpy(dest, src, len);
879 dest[len] = '\0';
880 }
881 return ret;
882}
883
Remy Bohmerdf063442009-07-29 18:18:43 +0200884/*============================================================================*/
885
886/*
887 * one config, two interfaces: control, data.
888 * complications: class descriptors, and an altsetting.
889 */
890static int
891config_buf(struct usb_gadget *g, u8 *buf, u8 type, unsigned index, int is_otg)
892{
893 int len;
894 const struct usb_config_descriptor *config;
895 const struct usb_descriptor_header **function;
896 int hs = 0;
897
898 if (gadget_is_dualspeed(g)) {
899 hs = (g->speed == USB_SPEED_HIGH);
900 if (type == USB_DT_OTHER_SPEED_CONFIG)
901 hs = !hs;
902 }
903#define which_fn(t) (hs ? hs_ ## t ## _function : fs_ ## t ## _function)
904
905 if (index >= device_desc.bNumConfigurations)
906 return -EINVAL;
907
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300908#ifdef CONFIG_USB_ETH_RNDIS
909 /*
910 * list the RNDIS config first, to make Microsoft's drivers
911 * happy. DOCSIS 1.0 needs this too.
912 */
913 if (device_desc.bNumConfigurations == 2 && index == 0) {
914 config = &rndis_config;
915 function = which_fn(rndis);
916 } else
917#endif
918 {
919 config = &eth_config;
920 function = which_fn(eth);
921 }
Remy Bohmerdf063442009-07-29 18:18:43 +0200922
923 /* for now, don't advertise srp-only devices */
924 if (!is_otg)
925 function++;
926
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400927 len = usb_gadget_config_buf(config, buf, USB_BUFSIZ, function);
Remy Bohmerdf063442009-07-29 18:18:43 +0200928 if (len < 0)
929 return len;
930 ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
931 return len;
932}
933
934/*-------------------------------------------------------------------------*/
935
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300936static void eth_start(struct eth_dev *dev, gfp_t gfp_flags);
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400937static int alloc_requests(struct eth_dev *dev, unsigned n, gfp_t gfp_flags);
Remy Bohmerdf063442009-07-29 18:18:43 +0200938
939static int
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400940set_ether_config(struct eth_dev *dev, gfp_t gfp_flags)
Remy Bohmerdf063442009-07-29 18:18:43 +0200941{
942 int result = 0;
943 struct usb_gadget *gadget = dev->gadget;
944
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200945#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300946 /* status endpoint used for RNDIS and (optionally) CDC */
Remy Bohmerdf063442009-07-29 18:18:43 +0200947 if (!subset_active(dev) && dev->status_ep) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400948 dev->status = ep_desc(gadget, &hs_status_desc,
Remy Bohmerdf063442009-07-29 18:18:43 +0200949 &fs_status_desc);
950 dev->status_ep->driver_data = dev;
951
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400952 result = usb_ep_enable(dev->status_ep, dev->status);
Remy Bohmerdf063442009-07-29 18:18:43 +0200953 if (result != 0) {
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +0400954 debug("enable %s --> %d\n",
Remy Bohmerdf063442009-07-29 18:18:43 +0200955 dev->status_ep->name, result);
956 goto done;
957 }
958 }
959#endif
960
961 dev->in = ep_desc(gadget, &hs_source_desc, &fs_source_desc);
962 dev->in_ep->driver_data = dev;
963
964 dev->out = ep_desc(gadget, &hs_sink_desc, &fs_sink_desc);
965 dev->out_ep->driver_data = dev;
966
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400967 /*
968 * With CDC, the host isn't allowed to use these two data
Remy Bohmerdf063442009-07-29 18:18:43 +0200969 * endpoints in the default altsetting for the interface.
970 * so we don't activate them yet. Reset from SET_INTERFACE.
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300971 *
972 * Strictly speaking RNDIS should work the same: activation is
973 * a side effect of setting a packet filter. Deactivation is
974 * from REMOTE_NDIS_HALT_MSG, reset from REMOTE_NDIS_RESET_MSG.
Remy Bohmerdf063442009-07-29 18:18:43 +0200975 */
976 if (!cdc_active(dev)) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400977 result = usb_ep_enable(dev->in_ep, dev->in);
Remy Bohmerdf063442009-07-29 18:18:43 +0200978 if (result != 0) {
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +0400979 debug("enable %s --> %d\n",
Remy Bohmerdf063442009-07-29 18:18:43 +0200980 dev->in_ep->name, result);
981 goto done;
982 }
983
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400984 result = usb_ep_enable(dev->out_ep, dev->out);
Remy Bohmerdf063442009-07-29 18:18:43 +0200985 if (result != 0) {
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +0400986 debug("enable %s --> %d\n",
Remy Bohmerdf063442009-07-29 18:18:43 +0200987 dev->out_ep->name, result);
988 goto done;
989 }
990 }
991
992done:
993 if (result == 0)
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400994 result = alloc_requests(dev, qlen(gadget), gfp_flags);
Remy Bohmerdf063442009-07-29 18:18:43 +0200995
996 /* on error, disable any endpoints */
997 if (result < 0) {
Vitaly Kuzmichev750be4c2010-08-13 17:02:29 +0400998 if (!subset_active(dev) && dev->status_ep)
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400999 (void) usb_ep_disable(dev->status_ep);
Remy Bohmerdf063442009-07-29 18:18:43 +02001000 dev->status = NULL;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001001 (void) usb_ep_disable(dev->in_ep);
1002 (void) usb_ep_disable(dev->out_ep);
Remy Bohmerdf063442009-07-29 18:18:43 +02001003 dev->in = NULL;
1004 dev->out = NULL;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001005 } else if (!cdc_active(dev)) {
1006 /*
1007 * activate non-CDC configs right away
1008 * this isn't strictly according to the RNDIS spec
1009 */
1010 eth_start(dev, GFP_ATOMIC);
Remy Bohmerdf063442009-07-29 18:18:43 +02001011 }
1012
1013 /* caller is responsible for cleanup on error */
1014 return result;
1015}
1016
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001017static void eth_reset_config(struct eth_dev *dev)
Remy Bohmerdf063442009-07-29 18:18:43 +02001018{
1019 if (dev->config == 0)
1020 return;
1021
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001022 debug("%s\n", __func__);
1023
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001024 rndis_uninit(dev->rndis_config);
1025
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001026 /*
1027 * disable endpoints, forcing (synchronous) completion of
Remy Bohmerdf063442009-07-29 18:18:43 +02001028 * pending i/o. then free the requests.
1029 */
1030
1031 if (dev->in) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001032 usb_ep_disable(dev->in_ep);
Remy Bohmerdf063442009-07-29 18:18:43 +02001033 if (dev->tx_req) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001034 usb_ep_free_request(dev->in_ep, dev->tx_req);
1035 dev->tx_req = NULL;
Remy Bohmerdf063442009-07-29 18:18:43 +02001036 }
1037 }
1038 if (dev->out) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001039 usb_ep_disable(dev->out_ep);
Remy Bohmerdf063442009-07-29 18:18:43 +02001040 if (dev->rx_req) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001041 usb_ep_free_request(dev->out_ep, dev->rx_req);
1042 dev->rx_req = NULL;
Remy Bohmerdf063442009-07-29 18:18:43 +02001043 }
1044 }
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001045 if (dev->status)
1046 usb_ep_disable(dev->status_ep);
1047
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001048 dev->rndis = 0;
Remy Bohmerdf063442009-07-29 18:18:43 +02001049 dev->cdc_filter = 0;
1050 dev->config = 0;
1051}
1052
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001053/*
1054 * change our operational config. must agree with the code
Remy Bohmerdf063442009-07-29 18:18:43 +02001055 * that returns config descriptors, and altsetting code.
1056 */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001057static int eth_set_config(struct eth_dev *dev, unsigned number,
1058 gfp_t gfp_flags)
Remy Bohmerdf063442009-07-29 18:18:43 +02001059{
1060 int result = 0;
1061 struct usb_gadget *gadget = dev->gadget;
1062
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001063 if (gadget_is_sa1100(gadget)
Remy Bohmerdf063442009-07-29 18:18:43 +02001064 && dev->config
1065 && dev->tx_qlen != 0) {
1066 /* tx fifo is full, but we can't clear it...*/
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001067 error("can't change configurations");
Remy Bohmerdf063442009-07-29 18:18:43 +02001068 return -ESPIPE;
1069 }
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001070 eth_reset_config(dev);
Remy Bohmerdf063442009-07-29 18:18:43 +02001071
1072 switch (number) {
1073 case DEV_CONFIG_VALUE:
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001074 result = set_ether_config(dev, gfp_flags);
Remy Bohmerdf063442009-07-29 18:18:43 +02001075 break;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001076#ifdef CONFIG_USB_ETH_RNDIS
1077 case DEV_RNDIS_CONFIG_VALUE:
1078 dev->rndis = 1;
1079 result = set_ether_config(dev, gfp_flags);
1080 break;
1081#endif
Remy Bohmerdf063442009-07-29 18:18:43 +02001082 default:
1083 result = -EINVAL;
1084 /* FALL THROUGH */
1085 case 0:
1086 break;
1087 }
1088
1089 if (result) {
1090 if (number)
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001091 eth_reset_config(dev);
Remy Bohmerdf063442009-07-29 18:18:43 +02001092 usb_gadget_vbus_draw(dev->gadget,
1093 gadget_is_otg(dev->gadget) ? 8 : 100);
1094 } else {
1095 char *speed;
1096 unsigned power;
1097
1098 power = 2 * eth_config.bMaxPower;
1099 usb_gadget_vbus_draw(dev->gadget, power);
1100
1101 switch (gadget->speed) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001102 case USB_SPEED_FULL:
1103 speed = "full"; break;
Remy Bohmerdf063442009-07-29 18:18:43 +02001104#ifdef CONFIG_USB_GADGET_DUALSPEED
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001105 case USB_SPEED_HIGH:
1106 speed = "high"; break;
Remy Bohmerdf063442009-07-29 18:18:43 +02001107#endif
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001108 default:
1109 speed = "?"; break;
Remy Bohmerdf063442009-07-29 18:18:43 +02001110 }
1111
1112 dev->config = number;
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001113 printf("%s speed config #%d: %d mA, %s, using %s\n",
Remy Bohmerdf063442009-07-29 18:18:43 +02001114 speed, number, power, driver_desc,
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001115 rndis_active(dev)
1116 ? "RNDIS"
1117 : (cdc_active(dev)
1118 ? "CDC Ethernet"
Remy Bohmerdf063442009-07-29 18:18:43 +02001119 : "CDC Ethernet Subset"));
1120 }
1121 return result;
1122}
1123
1124/*-------------------------------------------------------------------------*/
1125
Lukasz Dalek284c4cf2012-10-02 17:04:31 +02001126#ifdef CONFIG_USB_ETH_CDC
Remy Bohmerdf063442009-07-29 18:18:43 +02001127
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001128/*
1129 * The interrupt endpoint is used in CDC networking models (Ethernet, ATM)
Remy Bohmerdf063442009-07-29 18:18:43 +02001130 * only to notify the host about link status changes (which we support) or
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001131 * report completion of some encapsulated command (as used in RNDIS). Since
Remy Bohmerdf063442009-07-29 18:18:43 +02001132 * we want this CDC Ethernet code to be vendor-neutral, we don't use that
1133 * command mechanism; and only one status request is ever queued.
1134 */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001135static void eth_status_complete(struct usb_ep *ep, struct usb_request *req)
Remy Bohmerdf063442009-07-29 18:18:43 +02001136{
1137 struct usb_cdc_notification *event = req->buf;
1138 int value = req->status;
1139 struct eth_dev *dev = ep->driver_data;
1140
1141 /* issue the second notification if host reads the first */
1142 if (event->bNotificationType == USB_CDC_NOTIFY_NETWORK_CONNECTION
1143 && value == 0) {
1144 __le32 *data = req->buf + sizeof *event;
1145
1146 event->bmRequestType = 0xA1;
1147 event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001148 event->wValue = __constant_cpu_to_le16(0);
1149 event->wIndex = __constant_cpu_to_le16(1);
1150 event->wLength = __constant_cpu_to_le16(8);
Remy Bohmerdf063442009-07-29 18:18:43 +02001151
1152 /* SPEED_CHANGE data is up/down speeds in bits/sec */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001153 data[0] = data[1] = cpu_to_le32(BITRATE(dev->gadget));
Remy Bohmerdf063442009-07-29 18:18:43 +02001154
1155 req->length = STATUS_BYTECOUNT;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001156 value = usb_ep_queue(ep, req, GFP_ATOMIC);
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001157 debug("send SPEED_CHANGE --> %d\n", value);
Remy Bohmerdf063442009-07-29 18:18:43 +02001158 if (value == 0)
1159 return;
1160 } else if (value != -ECONNRESET) {
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001161 debug("event %02x --> %d\n",
Remy Bohmerdf063442009-07-29 18:18:43 +02001162 event->bNotificationType, value);
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001163 if (event->bNotificationType ==
1164 USB_CDC_NOTIFY_SPEED_CHANGE) {
1165 l_ethdev.network_started = 1;
Remy Bohmerdf063442009-07-29 18:18:43 +02001166 printf("USB network up!\n");
1167 }
1168 }
1169 req->context = NULL;
1170}
1171
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001172static void issue_start_status(struct eth_dev *dev)
Remy Bohmerdf063442009-07-29 18:18:43 +02001173{
1174 struct usb_request *req = dev->stat_req;
1175 struct usb_cdc_notification *event;
1176 int value;
1177
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001178 /*
1179 * flush old status
Remy Bohmerdf063442009-07-29 18:18:43 +02001180 *
1181 * FIXME ugly idiom, maybe we'd be better with just
1182 * a "cancel the whole queue" primitive since any
1183 * unlink-one primitive has way too many error modes.
1184 * here, we "know" toggle is already clear...
1185 *
1186 * FIXME iff req->context != null just dequeue it
1187 */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001188 usb_ep_disable(dev->status_ep);
1189 usb_ep_enable(dev->status_ep, dev->status);
Remy Bohmerdf063442009-07-29 18:18:43 +02001190
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001191 /*
1192 * 3.8.1 says to issue first NETWORK_CONNECTION, then
Remy Bohmerdf063442009-07-29 18:18:43 +02001193 * a SPEED_CHANGE. could be useful in some configs.
1194 */
1195 event = req->buf;
1196 event->bmRequestType = 0xA1;
1197 event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001198 event->wValue = __constant_cpu_to_le16(1); /* connected */
1199 event->wIndex = __constant_cpu_to_le16(1);
Remy Bohmerdf063442009-07-29 18:18:43 +02001200 event->wLength = 0;
1201
1202 req->length = sizeof *event;
1203 req->complete = eth_status_complete;
1204 req->context = dev;
1205
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001206 value = usb_ep_queue(dev->status_ep, req, GFP_ATOMIC);
Remy Bohmerdf063442009-07-29 18:18:43 +02001207 if (value < 0)
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001208 debug("status buf queue --> %d\n", value);
Remy Bohmerdf063442009-07-29 18:18:43 +02001209}
1210
1211#endif
1212
1213/*-------------------------------------------------------------------------*/
1214
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001215static void eth_setup_complete(struct usb_ep *ep, struct usb_request *req)
Remy Bohmerdf063442009-07-29 18:18:43 +02001216{
1217 if (req->status || req->actual != req->length)
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001218 debug("setup complete --> %d, %d/%d\n",
Remy Bohmerdf063442009-07-29 18:18:43 +02001219 req->status, req->actual, req->length);
1220}
1221
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001222#ifdef CONFIG_USB_ETH_RNDIS
1223
1224static void rndis_response_complete(struct usb_ep *ep, struct usb_request *req)
1225{
1226 if (req->status || req->actual != req->length)
1227 debug("rndis response complete --> %d, %d/%d\n",
1228 req->status, req->actual, req->length);
1229
1230 /* done sending after USB_CDC_GET_ENCAPSULATED_RESPONSE */
1231}
1232
1233static void rndis_command_complete(struct usb_ep *ep, struct usb_request *req)
1234{
1235 struct eth_dev *dev = ep->driver_data;
1236 int status;
1237
1238 /* received RNDIS command from USB_CDC_SEND_ENCAPSULATED_COMMAND */
1239 status = rndis_msg_parser(dev->rndis_config, (u8 *) req->buf);
1240 if (status < 0)
1241 error("%s: rndis parse error %d", __func__, status);
1242}
1243
1244#endif /* RNDIS */
1245
Remy Bohmerdf063442009-07-29 18:18:43 +02001246/*
1247 * The setup() callback implements all the ep0 functionality that's not
1248 * handled lower down. CDC has a number of less-common features:
1249 *
1250 * - two interfaces: control, and ethernet data
1251 * - Ethernet data interface has two altsettings: default, and active
1252 * - class-specific descriptors for the control interface
1253 * - class-specific control requests
1254 */
1255static int
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001256eth_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
Remy Bohmerdf063442009-07-29 18:18:43 +02001257{
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001258 struct eth_dev *dev = get_gadget_data(gadget);
Remy Bohmerdf063442009-07-29 18:18:43 +02001259 struct usb_request *req = dev->req;
1260 int value = -EOPNOTSUPP;
1261 u16 wIndex = le16_to_cpu(ctrl->wIndex);
1262 u16 wValue = le16_to_cpu(ctrl->wValue);
1263 u16 wLength = le16_to_cpu(ctrl->wLength);
1264
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001265 /*
1266 * descriptors just go into the pre-allocated ep0 buffer,
Remy Bohmerdf063442009-07-29 18:18:43 +02001267 * while config change events may enable network traffic.
1268 */
1269
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001270 debug("%s\n", __func__);
Remy Bohmerdf063442009-07-29 18:18:43 +02001271
1272 req->complete = eth_setup_complete;
1273 switch (ctrl->bRequest) {
1274
1275 case USB_REQ_GET_DESCRIPTOR:
1276 if (ctrl->bRequestType != USB_DIR_IN)
1277 break;
1278 switch (wValue >> 8) {
1279
1280 case USB_DT_DEVICE:
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001281 value = min(wLength, (u16) sizeof device_desc);
1282 memcpy(req->buf, &device_desc, value);
Remy Bohmerdf063442009-07-29 18:18:43 +02001283 break;
1284 case USB_DT_DEVICE_QUALIFIER:
1285 if (!gadget_is_dualspeed(gadget))
1286 break;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001287 value = min(wLength, (u16) sizeof dev_qualifier);
1288 memcpy(req->buf, &dev_qualifier, value);
Remy Bohmerdf063442009-07-29 18:18:43 +02001289 break;
1290
1291 case USB_DT_OTHER_SPEED_CONFIG:
1292 if (!gadget_is_dualspeed(gadget))
1293 break;
1294 /* FALLTHROUGH */
1295 case USB_DT_CONFIG:
1296 value = config_buf(gadget, req->buf,
1297 wValue >> 8,
1298 wValue & 0xff,
1299 gadget_is_otg(gadget));
1300 if (value >= 0)
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001301 value = min(wLength, (u16) value);
Remy Bohmerdf063442009-07-29 18:18:43 +02001302 break;
1303
1304 case USB_DT_STRING:
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001305 value = usb_gadget_get_string(&stringtab,
Remy Bohmerdf063442009-07-29 18:18:43 +02001306 wValue & 0xff, req->buf);
1307
1308 if (value >= 0)
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001309 value = min(wLength, (u16) value);
Remy Bohmerdf063442009-07-29 18:18:43 +02001310
1311 break;
1312 }
1313 break;
1314
1315 case USB_REQ_SET_CONFIGURATION:
1316 if (ctrl->bRequestType != 0)
1317 break;
1318 if (gadget->a_hnp_support)
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001319 debug("HNP available\n");
Remy Bohmerdf063442009-07-29 18:18:43 +02001320 else if (gadget->a_alt_hnp_support)
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001321 debug("HNP needs a different root port\n");
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001322 value = eth_set_config(dev, wValue, GFP_ATOMIC);
Remy Bohmerdf063442009-07-29 18:18:43 +02001323 break;
1324 case USB_REQ_GET_CONFIGURATION:
1325 if (ctrl->bRequestType != USB_DIR_IN)
1326 break;
1327 *(u8 *)req->buf = dev->config;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001328 value = min(wLength, (u16) 1);
Remy Bohmerdf063442009-07-29 18:18:43 +02001329 break;
1330
1331 case USB_REQ_SET_INTERFACE:
1332 if (ctrl->bRequestType != USB_RECIP_INTERFACE
1333 || !dev->config
1334 || wIndex > 1)
1335 break;
1336 if (!cdc_active(dev) && wIndex != 0)
1337 break;
1338
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001339 /*
1340 * PXA hardware partially handles SET_INTERFACE;
Remy Bohmerdf063442009-07-29 18:18:43 +02001341 * we need to kluge around that interference.
1342 */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001343 if (gadget_is_pxa(gadget)) {
1344 value = eth_set_config(dev, DEV_CONFIG_VALUE,
Remy Bohmerdf063442009-07-29 18:18:43 +02001345 GFP_ATOMIC);
Lukasz Dalek6ca42dd2012-10-02 17:04:29 +02001346 /*
1347 * PXA25x driver use non-CDC ethernet gadget.
1348 * But only _CDC and _RNDIS code can signalize
1349 * that network is working. So we signalize it
1350 * here.
1351 */
1352 l_ethdev.network_started = 1;
1353 debug("USB network up!\n");
Remy Bohmerdf063442009-07-29 18:18:43 +02001354 goto done_set_intf;
1355 }
1356
Lukasz Dalek284c4cf2012-10-02 17:04:31 +02001357#ifdef CONFIG_USB_ETH_CDC
Remy Bohmerdf063442009-07-29 18:18:43 +02001358 switch (wIndex) {
1359 case 0: /* control/master intf */
1360 if (wValue != 0)
1361 break;
1362 if (dev->status) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001363 usb_ep_disable(dev->status_ep);
1364 usb_ep_enable(dev->status_ep, dev->status);
Remy Bohmerdf063442009-07-29 18:18:43 +02001365 }
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001366
Remy Bohmerdf063442009-07-29 18:18:43 +02001367 value = 0;
1368 break;
1369 case 1: /* data intf */
1370 if (wValue > 1)
1371 break;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001372 usb_ep_disable(dev->in_ep);
1373 usb_ep_disable(dev->out_ep);
Remy Bohmerdf063442009-07-29 18:18:43 +02001374
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001375 /*
1376 * CDC requires the data transfers not be done from
Remy Bohmerdf063442009-07-29 18:18:43 +02001377 * the default interface setting ... also, setting
1378 * the non-default interface resets filters etc.
1379 */
1380 if (wValue == 1) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001381 if (!cdc_active(dev))
Remy Bohmerdf063442009-07-29 18:18:43 +02001382 break;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001383 usb_ep_enable(dev->in_ep, dev->in);
1384 usb_ep_enable(dev->out_ep, dev->out);
Remy Bohmerdf063442009-07-29 18:18:43 +02001385 dev->cdc_filter = DEFAULT_FILTER;
1386 if (dev->status)
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001387 issue_start_status(dev);
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001388 eth_start(dev, GFP_ATOMIC);
Remy Bohmerdf063442009-07-29 18:18:43 +02001389 }
Remy Bohmerdf063442009-07-29 18:18:43 +02001390 value = 0;
1391 break;
1392 }
1393#else
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001394 /*
1395 * FIXME this is wrong, as is the assumption that
Remy Bohmerdf063442009-07-29 18:18:43 +02001396 * all non-PXA hardware talks real CDC ...
1397 */
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001398 debug("set_interface ignored!\n");
Lukasz Dalek284c4cf2012-10-02 17:04:31 +02001399#endif /* CONFIG_USB_ETH_CDC */
Remy Bohmerdf063442009-07-29 18:18:43 +02001400
1401done_set_intf:
1402 break;
1403 case USB_REQ_GET_INTERFACE:
1404 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE)
1405 || !dev->config
1406 || wIndex > 1)
1407 break;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001408 if (!(cdc_active(dev) || rndis_active(dev)) && wIndex != 0)
Remy Bohmerdf063442009-07-29 18:18:43 +02001409 break;
1410
1411 /* for CDC, iff carrier is on, data interface is active. */
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001412 if (rndis_active(dev) || wIndex != 1)
Remy Bohmerdf063442009-07-29 18:18:43 +02001413 *(u8 *)req->buf = 0;
1414 else {
1415 /* *(u8 *)req->buf = netif_carrier_ok (dev->net) ? 1 : 0; */
1416 /* carrier always ok ...*/
1417 *(u8 *)req->buf = 1 ;
1418 }
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001419 value = min(wLength, (u16) 1);
Remy Bohmerdf063442009-07-29 18:18:43 +02001420 break;
1421
Lukasz Dalek284c4cf2012-10-02 17:04:31 +02001422#ifdef CONFIG_USB_ETH_CDC
Remy Bohmerdf063442009-07-29 18:18:43 +02001423 case USB_CDC_SET_ETHERNET_PACKET_FILTER:
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001424 /*
1425 * see 6.2.30: no data, wIndex = interface,
Remy Bohmerdf063442009-07-29 18:18:43 +02001426 * wValue = packet filter bitmap
1427 */
1428 if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1429 || !cdc_active(dev)
1430 || wLength != 0
1431 || wIndex > 1)
1432 break;
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001433 debug("packet filter %02x\n", wValue);
Remy Bohmerdf063442009-07-29 18:18:43 +02001434 dev->cdc_filter = wValue;
1435 value = 0;
1436 break;
1437
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001438 /*
1439 * and potentially:
Remy Bohmerdf063442009-07-29 18:18:43 +02001440 * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS:
1441 * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER:
1442 * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER:
1443 * case USB_CDC_GET_ETHERNET_STATISTIC:
1444 */
1445
Lukasz Dalek284c4cf2012-10-02 17:04:31 +02001446#endif /* CONFIG_USB_ETH_CDC */
Remy Bohmerdf063442009-07-29 18:18:43 +02001447
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001448#ifdef CONFIG_USB_ETH_RNDIS
1449 /*
1450 * RNDIS uses the CDC command encapsulation mechanism to implement
1451 * an RPC scheme, with much getting/setting of attributes by OID.
1452 */
1453 case USB_CDC_SEND_ENCAPSULATED_COMMAND:
1454 if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1455 || !rndis_active(dev)
1456 || wLength > USB_BUFSIZ
1457 || wValue
1458 || rndis_control_intf.bInterfaceNumber
1459 != wIndex)
1460 break;
1461 /* read the request, then process it */
1462 value = wLength;
1463 req->complete = rndis_command_complete;
1464 /* later, rndis_control_ack () sends a notification */
1465 break;
1466
1467 case USB_CDC_GET_ENCAPSULATED_RESPONSE:
1468 if ((USB_DIR_IN|USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1469 == ctrl->bRequestType
1470 && rndis_active(dev)
1471 /* && wLength >= 0x0400 */
1472 && !wValue
1473 && rndis_control_intf.bInterfaceNumber
1474 == wIndex) {
1475 u8 *buf;
1476 u32 n;
1477
1478 /* return the result */
1479 buf = rndis_get_next_response(dev->rndis_config, &n);
1480 if (buf) {
1481 memcpy(req->buf, buf, n);
1482 req->complete = rndis_response_complete;
1483 rndis_free_response(dev->rndis_config, buf);
1484 value = n;
1485 }
1486 /* else stalls ... spec says to avoid that */
1487 }
1488 break;
1489#endif /* RNDIS */
1490
Remy Bohmerdf063442009-07-29 18:18:43 +02001491 default:
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001492 debug("unknown control req%02x.%02x v%04x i%04x l%d\n",
Remy Bohmerdf063442009-07-29 18:18:43 +02001493 ctrl->bRequestType, ctrl->bRequest,
1494 wValue, wIndex, wLength);
1495 }
1496
1497 /* respond with data transfer before status phase? */
1498 if (value >= 0) {
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001499 debug("respond with data transfer before status phase\n");
Remy Bohmerdf063442009-07-29 18:18:43 +02001500 req->length = value;
1501 req->zero = value < wLength
1502 && (value % gadget->ep0->maxpacket) == 0;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001503 value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
Remy Bohmerdf063442009-07-29 18:18:43 +02001504 if (value < 0) {
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001505 debug("ep_queue --> %d\n", value);
Remy Bohmerdf063442009-07-29 18:18:43 +02001506 req->status = 0;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001507 eth_setup_complete(gadget->ep0, req);
Remy Bohmerdf063442009-07-29 18:18:43 +02001508 }
1509 }
1510
1511 /* host either stalls (value < 0) or reports success */
1512 return value;
1513}
1514
Remy Bohmerdf063442009-07-29 18:18:43 +02001515/*-------------------------------------------------------------------------*/
1516
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001517static void rx_complete(struct usb_ep *ep, struct usb_request *req);
Remy Bohmerdf063442009-07-29 18:18:43 +02001518
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001519static int rx_submit(struct eth_dev *dev, struct usb_request *req,
Remy Bohmerdf063442009-07-29 18:18:43 +02001520 gfp_t gfp_flags)
1521{
1522 int retval = -ENOMEM;
1523 size_t size;
1524
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001525 /*
1526 * Padding up to RX_EXTRA handles minor disagreements with host.
Remy Bohmerdf063442009-07-29 18:18:43 +02001527 * Normally we use the USB "terminate on short read" convention;
1528 * so allow up to (N*maxpacket), since that memory is normally
1529 * already allocated. Some hardware doesn't deal well with short
1530 * reads (e.g. DMA must be N*maxpacket), so for now don't trim a
1531 * byte off the end (to force hardware errors on overflow).
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001532 *
1533 * RNDIS uses internal framing, and explicitly allows senders to
1534 * pad to end-of-packet. That's potentially nice for speed,
1535 * but means receivers can't recover synch on their own.
Remy Bohmerdf063442009-07-29 18:18:43 +02001536 */
1537
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001538 debug("%s\n", __func__);
Troy Kisky1d01de12013-09-25 18:41:05 -07001539 if (!req)
1540 return -EINVAL;
Remy Bohmerdf063442009-07-29 18:18:43 +02001541
1542 size = (ETHER_HDR_SIZE + dev->mtu + RX_EXTRA);
1543 size += dev->out_ep->maxpacket - 1;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001544 if (rndis_active(dev))
1545 size += sizeof(struct rndis_packet_msg_type);
Remy Bohmerdf063442009-07-29 18:18:43 +02001546 size -= size % dev->out_ep->maxpacket;
1547
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001548 /*
1549 * Some platforms perform better when IP packets are aligned,
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001550 * but on at least one, checksumming fails otherwise. Note:
1551 * RNDIS headers involve variable numbers of LE32 values.
Remy Bohmerdf063442009-07-29 18:18:43 +02001552 */
1553
1554 req->buf = (u8 *) NetRxPackets[0];
1555 req->length = size;
1556 req->complete = rx_complete;
1557
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001558 retval = usb_ep_queue(dev->out_ep, req, gfp_flags);
Remy Bohmerdf063442009-07-29 18:18:43 +02001559
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001560 if (retval)
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001561 error("rx submit --> %d", retval);
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001562
Remy Bohmerdf063442009-07-29 18:18:43 +02001563 return retval;
1564}
1565
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001566static void rx_complete(struct usb_ep *ep, struct usb_request *req)
Remy Bohmerdf063442009-07-29 18:18:43 +02001567{
1568 struct eth_dev *dev = ep->driver_data;
1569
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001570 debug("%s: status %d\n", __func__, req->status);
Vitaly Kuzmichev24fcb472011-02-11 18:18:32 +03001571 switch (req->status) {
1572 /* normal completion */
1573 case 0:
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001574 if (rndis_active(dev)) {
1575 /* we know MaxPacketsPerTransfer == 1 here */
1576 int length = rndis_rm_hdr(req->buf, req->actual);
1577 if (length < 0)
1578 goto length_err;
1579 req->length -= length;
1580 req->actual -= length;
1581 }
1582 if (req->actual < ETH_HLEN || ETH_FRAME_LEN < req->actual) {
1583length_err:
1584 dev->stats.rx_errors++;
1585 dev->stats.rx_length_errors++;
1586 debug("rx length %d\n", req->length);
1587 break;
1588 }
1589
Vitaly Kuzmichev24fcb472011-02-11 18:18:32 +03001590 dev->stats.rx_packets++;
1591 dev->stats.rx_bytes += req->length;
1592 break;
1593
1594 /* software-driven interface shutdown */
1595 case -ECONNRESET: /* unlink */
1596 case -ESHUTDOWN: /* disconnect etc */
1597 /* for hardware automagic (such as pxa) */
1598 case -ECONNABORTED: /* endpoint reset */
1599 break;
1600
1601 /* data overrun */
1602 case -EOVERFLOW:
1603 dev->stats.rx_over_errors++;
1604 /* FALLTHROUGH */
1605 default:
1606 dev->stats.rx_errors++;
1607 break;
1608 }
Remy Bohmerdf063442009-07-29 18:18:43 +02001609
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001610 packet_received = 1;
Remy Bohmerdf063442009-07-29 18:18:43 +02001611}
1612
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001613static int alloc_requests(struct eth_dev *dev, unsigned n, gfp_t gfp_flags)
Remy Bohmerdf063442009-07-29 18:18:43 +02001614{
1615
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001616 dev->tx_req = usb_ep_alloc_request(dev->in_ep, 0);
Remy Bohmerdf063442009-07-29 18:18:43 +02001617
1618 if (!dev->tx_req)
Vitaly Kuzmichev2907b2c2010-09-22 13:13:55 +04001619 goto fail1;
Remy Bohmerdf063442009-07-29 18:18:43 +02001620
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001621 dev->rx_req = usb_ep_alloc_request(dev->out_ep, 0);
Remy Bohmerdf063442009-07-29 18:18:43 +02001622
1623 if (!dev->rx_req)
Vitaly Kuzmichev2907b2c2010-09-22 13:13:55 +04001624 goto fail2;
Remy Bohmerdf063442009-07-29 18:18:43 +02001625
1626 return 0;
1627
Vitaly Kuzmichev2907b2c2010-09-22 13:13:55 +04001628fail2:
1629 usb_ep_free_request(dev->in_ep, dev->tx_req);
1630fail1:
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001631 error("can't alloc requests");
Remy Bohmerdf063442009-07-29 18:18:43 +02001632 return -1;
1633}
1634
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001635static void tx_complete(struct usb_ep *ep, struct usb_request *req)
Remy Bohmerdf063442009-07-29 18:18:43 +02001636{
Vitaly Kuzmichev24fcb472011-02-11 18:18:32 +03001637 struct eth_dev *dev = ep->driver_data;
1638
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001639 debug("%s: status %s\n", __func__, (req->status) ? "failed" : "ok");
Vitaly Kuzmichev24fcb472011-02-11 18:18:32 +03001640 switch (req->status) {
1641 default:
1642 dev->stats.tx_errors++;
1643 debug("tx err %d\n", req->status);
1644 /* FALLTHROUGH */
1645 case -ECONNRESET: /* unlink */
1646 case -ESHUTDOWN: /* disconnect etc */
1647 break;
1648 case 0:
1649 dev->stats.tx_bytes += req->length;
1650 }
1651 dev->stats.tx_packets++;
1652
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001653 packet_sent = 1;
Remy Bohmerdf063442009-07-29 18:18:43 +02001654}
1655
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001656static inline int eth_is_promisc(struct eth_dev *dev)
Remy Bohmerdf063442009-07-29 18:18:43 +02001657{
1658 /* no filters for the CDC subset; always promisc */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001659 if (subset_active(dev))
Remy Bohmerdf063442009-07-29 18:18:43 +02001660 return 1;
1661 return dev->cdc_filter & USB_CDC_PACKET_TYPE_PROMISCUOUS;
1662}
1663
1664#if 0
1665static int eth_start_xmit (struct sk_buff *skb, struct net_device *net)
1666{
1667 struct eth_dev *dev = netdev_priv(net);
1668 int length = skb->len;
1669 int retval;
1670 struct usb_request *req = NULL;
1671 unsigned long flags;
1672
1673 /* apply outgoing CDC or RNDIS filters */
1674 if (!eth_is_promisc (dev)) {
1675 u8 *dest = skb->data;
1676
1677 if (is_multicast_ether_addr(dest)) {
1678 u16 type;
1679
1680 /* ignores USB_CDC_PACKET_TYPE_MULTICAST and host
1681 * SET_ETHERNET_MULTICAST_FILTERS requests
1682 */
1683 if (is_broadcast_ether_addr(dest))
1684 type = USB_CDC_PACKET_TYPE_BROADCAST;
1685 else
1686 type = USB_CDC_PACKET_TYPE_ALL_MULTICAST;
1687 if (!(dev->cdc_filter & type)) {
1688 dev_kfree_skb_any (skb);
1689 return 0;
1690 }
1691 }
1692 /* ignores USB_CDC_PACKET_TYPE_DIRECTED */
1693 }
1694
1695 spin_lock_irqsave(&dev->req_lock, flags);
1696 /*
1697 * this freelist can be empty if an interrupt triggered disconnect()
1698 * and reconfigured the gadget (shutting down this queue) after the
1699 * network stack decided to xmit but before we got the spinlock.
1700 */
1701 if (list_empty(&dev->tx_reqs)) {
1702 spin_unlock_irqrestore(&dev->req_lock, flags);
1703 return 1;
1704 }
1705
1706 req = container_of (dev->tx_reqs.next, struct usb_request, list);
1707 list_del (&req->list);
1708
1709 /* temporarily stop TX queue when the freelist empties */
1710 if (list_empty (&dev->tx_reqs))
1711 netif_stop_queue (net);
1712 spin_unlock_irqrestore(&dev->req_lock, flags);
1713
1714 /* no buffer copies needed, unless the network stack did it
1715 * or the hardware can't use skb buffers.
1716 * or there's not enough space for any RNDIS headers we need
1717 */
1718 if (rndis_active(dev)) {
1719 struct sk_buff *skb_rndis;
1720
1721 skb_rndis = skb_realloc_headroom (skb,
1722 sizeof (struct rndis_packet_msg_type));
1723 if (!skb_rndis)
1724 goto drop;
1725
1726 dev_kfree_skb_any (skb);
1727 skb = skb_rndis;
1728 rndis_add_hdr (skb);
1729 length = skb->len;
1730 }
1731 req->buf = skb->data;
1732 req->context = skb;
1733 req->complete = tx_complete;
1734
1735 /* use zlp framing on tx for strict CDC-Ether conformance,
1736 * though any robust network rx path ignores extra padding.
1737 * and some hardware doesn't like to write zlps.
1738 */
1739 req->zero = 1;
1740 if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0)
1741 length++;
1742
1743 req->length = length;
1744
1745 /* throttle highspeed IRQ rate back slightly */
1746 if (gadget_is_dualspeed(dev->gadget))
1747 req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH)
1748 ? ((atomic_read(&dev->tx_qlen) % qmult) != 0)
1749 : 0;
1750
1751 retval = usb_ep_queue (dev->in_ep, req, GFP_ATOMIC);
1752 switch (retval) {
1753 default:
1754 DEBUG (dev, "tx queue err %d\n", retval);
1755 break;
1756 case 0:
1757 net->trans_start = jiffies;
1758 atomic_inc (&dev->tx_qlen);
1759 }
1760
1761 if (retval) {
1762drop:
1763 dev->stats.tx_dropped++;
1764 dev_kfree_skb_any (skb);
1765 spin_lock_irqsave(&dev->req_lock, flags);
1766 if (list_empty (&dev->tx_reqs))
1767 netif_start_queue (net);
1768 list_add (&req->list, &dev->tx_reqs);
1769 spin_unlock_irqrestore(&dev->req_lock, flags);
1770 }
1771 return 0;
1772}
1773
1774/*-------------------------------------------------------------------------*/
1775#endif
1776
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001777static void eth_unbind(struct usb_gadget *gadget)
Remy Bohmerdf063442009-07-29 18:18:43 +02001778{
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001779 struct eth_dev *dev = get_gadget_data(gadget);
Remy Bohmerdf063442009-07-29 18:18:43 +02001780
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001781 debug("%s...\n", __func__);
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001782 rndis_deregister(dev->rndis_config);
1783 rndis_exit();
Remy Bohmerdf063442009-07-29 18:18:43 +02001784
Vitaly Kuzmichev90670702010-08-13 17:00:16 +04001785 /* we've already been disconnected ... no i/o is active */
1786 if (dev->req) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001787 usb_ep_free_request(gadget->ep0, dev->req);
Vitaly Kuzmichev90670702010-08-13 17:00:16 +04001788 dev->req = NULL;
1789 }
Remy Bohmerdf063442009-07-29 18:18:43 +02001790 if (dev->stat_req) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001791 usb_ep_free_request(dev->status_ep, dev->stat_req);
Remy Bohmerdf063442009-07-29 18:18:43 +02001792 dev->stat_req = NULL;
1793 }
1794
1795 if (dev->tx_req) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001796 usb_ep_free_request(dev->in_ep, dev->tx_req);
1797 dev->tx_req = NULL;
Remy Bohmerdf063442009-07-29 18:18:43 +02001798 }
1799
1800 if (dev->rx_req) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001801 usb_ep_free_request(dev->out_ep, dev->rx_req);
1802 dev->rx_req = NULL;
Remy Bohmerdf063442009-07-29 18:18:43 +02001803 }
1804
1805/* unregister_netdev (dev->net);*/
1806/* free_netdev(dev->net);*/
1807
Lei Wen4af32732010-12-01 23:43:43 +08001808 dev->gadget = NULL;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001809 set_gadget_data(gadget, NULL);
Remy Bohmerdf063442009-07-29 18:18:43 +02001810}
1811
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001812static void eth_disconnect(struct usb_gadget *gadget)
Remy Bohmerdf063442009-07-29 18:18:43 +02001813{
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001814 eth_reset_config(get_gadget_data(gadget));
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001815 /* FIXME RNDIS should enter RNDIS_UNINITIALIZED */
Remy Bohmerdf063442009-07-29 18:18:43 +02001816}
1817
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001818static void eth_suspend(struct usb_gadget *gadget)
Remy Bohmerdf063442009-07-29 18:18:43 +02001819{
1820 /* Not used */
1821}
1822
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001823static void eth_resume(struct usb_gadget *gadget)
Remy Bohmerdf063442009-07-29 18:18:43 +02001824{
1825 /* Not used */
1826}
1827
1828/*-------------------------------------------------------------------------*/
1829
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001830#ifdef CONFIG_USB_ETH_RNDIS
1831
1832/*
1833 * The interrupt endpoint is used in RNDIS to notify the host when messages
1834 * other than data packets are available ... notably the REMOTE_NDIS_*_CMPLT
1835 * messages, but also REMOTE_NDIS_INDICATE_STATUS_MSG and potentially even
1836 * REMOTE_NDIS_KEEPALIVE_MSG.
1837 *
1838 * The RNDIS control queue is processed by GET_ENCAPSULATED_RESPONSE, and
1839 * normally just one notification will be queued.
1840 */
1841
1842static void rndis_control_ack_complete(struct usb_ep *ep,
1843 struct usb_request *req)
1844{
1845 struct eth_dev *dev = ep->driver_data;
1846
1847 debug("%s...\n", __func__);
1848 if (req->status || req->actual != req->length)
1849 debug("rndis control ack complete --> %d, %d/%d\n",
1850 req->status, req->actual, req->length);
1851
1852 if (!l_ethdev.network_started) {
1853 if (rndis_get_state(dev->rndis_config)
1854 == RNDIS_DATA_INITIALIZED) {
1855 l_ethdev.network_started = 1;
1856 printf("USB RNDIS network up!\n");
1857 }
1858 }
1859
1860 req->context = NULL;
1861
1862 if (req != dev->stat_req)
1863 usb_ep_free_request(ep, req);
1864}
1865
1866static char rndis_resp_buf[8] __attribute__((aligned(sizeof(__le32))));
1867
1868static int rndis_control_ack(struct eth_device *net)
1869{
1870 struct eth_dev *dev = &l_ethdev;
1871 int length;
1872 struct usb_request *resp = dev->stat_req;
1873
1874 /* in case RNDIS calls this after disconnect */
1875 if (!dev->status) {
1876 debug("status ENODEV\n");
1877 return -ENODEV;
1878 }
1879
1880 /* in case queue length > 1 */
1881 if (resp->context) {
1882 resp = usb_ep_alloc_request(dev->status_ep, GFP_ATOMIC);
1883 if (!resp)
1884 return -ENOMEM;
1885 resp->buf = rndis_resp_buf;
1886 }
1887
1888 /*
1889 * Send RNDIS RESPONSE_AVAILABLE notification;
1890 * USB_CDC_NOTIFY_RESPONSE_AVAILABLE should work too
1891 */
1892 resp->length = 8;
1893 resp->complete = rndis_control_ack_complete;
1894 resp->context = dev;
1895
1896 *((__le32 *) resp->buf) = __constant_cpu_to_le32(1);
1897 *((__le32 *) (resp->buf + 4)) = __constant_cpu_to_le32(0);
1898
1899 length = usb_ep_queue(dev->status_ep, resp, GFP_ATOMIC);
1900 if (length < 0) {
1901 resp->status = 0;
1902 rndis_control_ack_complete(dev->status_ep, resp);
1903 }
1904
1905 return 0;
1906}
1907
1908#else
1909
1910#define rndis_control_ack NULL
1911
1912#endif /* RNDIS */
1913
1914static void eth_start(struct eth_dev *dev, gfp_t gfp_flags)
1915{
1916 if (rndis_active(dev)) {
1917 rndis_set_param_medium(dev->rndis_config,
1918 NDIS_MEDIUM_802_3,
1919 BITRATE(dev->gadget)/100);
1920 rndis_signal_connect(dev->rndis_config);
1921 }
1922}
1923
1924static int eth_stop(struct eth_dev *dev)
1925{
Vitaly Kuzmichev3433c332011-02-11 18:18:35 +03001926#ifdef RNDIS_COMPLETE_SIGNAL_DISCONNECT
1927 unsigned long ts;
1928 unsigned long timeout = CONFIG_SYS_HZ; /* 1 sec to stop RNDIS */
1929#endif
1930
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001931 if (rndis_active(dev)) {
1932 rndis_set_param_medium(dev->rndis_config, NDIS_MEDIUM_802_3, 0);
1933 rndis_signal_disconnect(dev->rndis_config);
1934
Vitaly Kuzmichev3433c332011-02-11 18:18:35 +03001935#ifdef RNDIS_COMPLETE_SIGNAL_DISCONNECT
1936 /* Wait until host receives OID_GEN_MEDIA_CONNECT_STATUS */
1937 ts = get_timer(0);
1938 while (get_timer(ts) < timeout)
1939 usb_gadget_handle_interrupts();
1940#endif
1941
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001942 rndis_uninit(dev->rndis_config);
1943 dev->rndis = 0;
1944 }
1945
1946 return 0;
1947}
1948
1949/*-------------------------------------------------------------------------*/
1950
Remy Bohmerdf063442009-07-29 18:18:43 +02001951static int is_eth_addr_valid(char *str)
1952{
1953 if (strlen(str) == 17) {
1954 int i;
1955 char *p, *q;
1956 uchar ea[6];
1957
1958 /* see if it looks like an ethernet address */
1959
1960 p = str;
1961
1962 for (i = 0; i < 6; i++) {
1963 char term = (i == 5 ? '\0' : ':');
1964
1965 ea[i] = simple_strtol(p, &q, 16);
1966
1967 if ((q - p) != 2 || *q++ != term)
1968 break;
1969
1970 p = q;
1971 }
1972
Tom Riniadd21ca2012-10-31 13:30:41 +00001973 /* Now check the contents. */
1974 return is_valid_ether_addr(ea);
Remy Bohmerdf063442009-07-29 18:18:43 +02001975 }
1976 return 0;
1977}
1978
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001979static u8 nibble(unsigned char c)
Remy Bohmerdf063442009-07-29 18:18:43 +02001980{
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001981 if (likely(isdigit(c)))
Remy Bohmerdf063442009-07-29 18:18:43 +02001982 return c - '0';
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001983 c = toupper(c);
1984 if (likely(isxdigit(c)))
Remy Bohmerdf063442009-07-29 18:18:43 +02001985 return 10 + c - 'A';
1986 return 0;
1987}
1988
1989static int get_ether_addr(const char *str, u8 *dev_addr)
1990{
1991 if (str) {
1992 unsigned i;
1993
1994 for (i = 0; i < 6; i++) {
1995 unsigned char num;
1996
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001997 if ((*str == '.') || (*str == ':'))
Remy Bohmerdf063442009-07-29 18:18:43 +02001998 str++;
1999 num = nibble(*str++) << 4;
2000 num |= (nibble(*str++));
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002001 dev_addr[i] = num;
Remy Bohmerdf063442009-07-29 18:18:43 +02002002 }
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002003 if (is_valid_ether_addr(dev_addr))
Remy Bohmerdf063442009-07-29 18:18:43 +02002004 return 0;
2005 }
2006 return 1;
2007}
2008
2009static int eth_bind(struct usb_gadget *gadget)
2010{
2011 struct eth_dev *dev = &l_ethdev;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002012 u8 cdc = 1, zlp = 1, rndis = 1;
Remy Bohmerdf063442009-07-29 18:18:43 +02002013 struct usb_ep *in_ep, *out_ep, *status_ep = NULL;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002014 int status = -ENOMEM;
Remy Bohmerdf063442009-07-29 18:18:43 +02002015 int gcnum;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002016 u8 tmp[7];
Remy Bohmerdf063442009-07-29 18:18:43 +02002017
2018 /* these flags are only ever cleared; compiler take note */
Lukasz Dalek284c4cf2012-10-02 17:04:31 +02002019#ifndef CONFIG_USB_ETH_CDC
Remy Bohmerdf063442009-07-29 18:18:43 +02002020 cdc = 0;
2021#endif
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002022#ifndef CONFIG_USB_ETH_RNDIS
2023 rndis = 0;
2024#endif
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002025 /*
2026 * Because most host side USB stacks handle CDC Ethernet, that
Remy Bohmerdf063442009-07-29 18:18:43 +02002027 * standard protocol is _strongly_ preferred for interop purposes.
2028 * (By everyone except Microsoft.)
2029 */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002030 if (gadget_is_pxa(gadget)) {
Remy Bohmerdf063442009-07-29 18:18:43 +02002031 /* pxa doesn't support altsettings */
2032 cdc = 0;
2033 } else if (gadget_is_musbhdrc(gadget)) {
2034 /* reduce tx dma overhead by avoiding special cases */
2035 zlp = 0;
2036 } else if (gadget_is_sh(gadget)) {
2037 /* sh doesn't support multiple interfaces or configs */
2038 cdc = 0;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002039 rndis = 0;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002040 } else if (gadget_is_sa1100(gadget)) {
Remy Bohmerdf063442009-07-29 18:18:43 +02002041 /* hardware can't write zlps */
2042 zlp = 0;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002043 /*
2044 * sa1100 CAN do CDC, without status endpoint ... we use
Remy Bohmerdf063442009-07-29 18:18:43 +02002045 * non-CDC to be compatible with ARM Linux-2.4 "usb-eth".
2046 */
2047 cdc = 0;
2048 }
2049
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002050 gcnum = usb_gadget_controller_number(gadget);
Remy Bohmerdf063442009-07-29 18:18:43 +02002051 if (gcnum >= 0)
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002052 device_desc.bcdDevice = cpu_to_le16(0x0300 + gcnum);
Remy Bohmerdf063442009-07-29 18:18:43 +02002053 else {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002054 /*
2055 * can't assume CDC works. don't want to default to
Remy Bohmerdf063442009-07-29 18:18:43 +02002056 * anything less functional on CDC-capable hardware,
2057 * so we fail in this case.
2058 */
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04002059 error("controller '%s' not recognized",
Remy Bohmerdf063442009-07-29 18:18:43 +02002060 gadget->name);
2061 return -ENODEV;
2062 }
2063
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002064 /*
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002065 * If there's an RNDIS configuration, that's what Windows wants to
2066 * be using ... so use these product IDs here and in the "linux.inf"
2067 * needed to install MSFT drivers. Current Linux kernels will use
2068 * the second configuration if it's CDC Ethernet, and need some help
2069 * to choose the right configuration otherwise.
Remy Bohmerdf063442009-07-29 18:18:43 +02002070 */
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002071 if (rndis) {
2072#if defined(CONFIG_USB_RNDIS_VENDOR_ID) && defined(CONFIG_USB_RNDIS_PRODUCT_ID)
Remy Bohmerdf063442009-07-29 18:18:43 +02002073 device_desc.idVendor =
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002074 __constant_cpu_to_le16(CONFIG_USB_RNDIS_VENDOR_ID);
Remy Bohmerdf063442009-07-29 18:18:43 +02002075 device_desc.idProduct =
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002076 __constant_cpu_to_le16(CONFIG_USB_RNDIS_PRODUCT_ID);
2077#else
2078 device_desc.idVendor =
2079 __constant_cpu_to_le16(RNDIS_VENDOR_NUM);
2080 device_desc.idProduct =
2081 __constant_cpu_to_le16(RNDIS_PRODUCT_NUM);
2082#endif
2083 sprintf(product_desc, "RNDIS/%s", driver_desc);
Remy Bohmerdf063442009-07-29 18:18:43 +02002084
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002085 /*
2086 * CDC subset ... recognized by Linux since 2.4.10, but Windows
2087 * drivers aren't widely available. (That may be improved by
2088 * supporting one submode of the "SAFE" variant of MDLM.)
2089 */
2090 } else {
Remy Bohmerdf063442009-07-29 18:18:43 +02002091#if defined(CONFIG_USB_CDC_VENDOR_ID) && defined(CONFIG_USB_CDC_PRODUCT_ID)
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002092 device_desc.idVendor = cpu_to_le16(CONFIG_USB_CDC_VENDOR_ID);
2093 device_desc.idProduct = cpu_to_le16(CONFIG_USB_CDC_PRODUCT_ID);
2094#else
2095 if (!cdc) {
2096 device_desc.idVendor =
2097 __constant_cpu_to_le16(SIMPLE_VENDOR_NUM);
2098 device_desc.idProduct =
2099 __constant_cpu_to_le16(SIMPLE_PRODUCT_NUM);
2100 }
Remy Bohmerdf063442009-07-29 18:18:43 +02002101#endif
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002102 }
2103 /* support optional vendor/distro customization */
Remy Bohmerdf063442009-07-29 18:18:43 +02002104 if (bcdDevice)
2105 device_desc.bcdDevice = cpu_to_le16(bcdDevice);
2106 if (iManufacturer)
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002107 strlcpy(manufacturer, iManufacturer, sizeof manufacturer);
Remy Bohmerdf063442009-07-29 18:18:43 +02002108 if (iProduct)
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002109 strlcpy(product_desc, iProduct, sizeof product_desc);
Remy Bohmerdf063442009-07-29 18:18:43 +02002110 if (iSerialNumber) {
2111 device_desc.iSerialNumber = STRING_SERIALNUMBER,
Vitaly Kuzmichev214d7b02010-08-13 17:00:45 +04002112 strlcpy(serial_number, iSerialNumber, sizeof serial_number);
Remy Bohmerdf063442009-07-29 18:18:43 +02002113 }
2114
2115 /* all we really need is bulk IN/OUT */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002116 usb_ep_autoconfig_reset(gadget);
2117 in_ep = usb_ep_autoconfig(gadget, &fs_source_desc);
Remy Bohmerdf063442009-07-29 18:18:43 +02002118 if (!in_ep) {
2119autoconf_fail:
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04002120 error("can't autoconfigure on %s\n",
Remy Bohmerdf063442009-07-29 18:18:43 +02002121 gadget->name);
2122 return -ENODEV;
2123 }
2124 in_ep->driver_data = in_ep; /* claim */
2125
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002126 out_ep = usb_ep_autoconfig(gadget, &fs_sink_desc);
Remy Bohmerdf063442009-07-29 18:18:43 +02002127 if (!out_ep)
2128 goto autoconf_fail;
2129 out_ep->driver_data = out_ep; /* claim */
2130
Lukasz Dalek284c4cf2012-10-02 17:04:31 +02002131#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002132 /*
2133 * CDC Ethernet control interface doesn't require a status endpoint.
Remy Bohmerdf063442009-07-29 18:18:43 +02002134 * Since some hosts expect one, try to allocate one anyway.
2135 */
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002136 if (cdc || rndis) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002137 status_ep = usb_ep_autoconfig(gadget, &fs_status_desc);
Remy Bohmerdf063442009-07-29 18:18:43 +02002138 if (status_ep) {
2139 status_ep->driver_data = status_ep; /* claim */
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002140 } else if (rndis) {
2141 error("can't run RNDIS on %s", gadget->name);
2142 return -ENODEV;
Lukasz Dalek284c4cf2012-10-02 17:04:31 +02002143#ifdef CONFIG_USB_ETH_CDC
Remy Bohmerdf063442009-07-29 18:18:43 +02002144 } else if (cdc) {
2145 control_intf.bNumEndpoints = 0;
2146 /* FIXME remove endpoint from descriptor list */
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002147#endif
Remy Bohmerdf063442009-07-29 18:18:43 +02002148 }
2149 }
2150#endif
2151
2152 /* one config: cdc, else minimal subset */
2153 if (!cdc) {
2154 eth_config.bNumInterfaces = 1;
2155 eth_config.iConfiguration = STRING_SUBSET;
2156
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002157 /*
2158 * use functions to set these up, in case we're built to work
Remy Bohmerdf063442009-07-29 18:18:43 +02002159 * with multiple controllers and must override CDC Ethernet.
2160 */
2161 fs_subset_descriptors();
2162 hs_subset_descriptors();
2163 }
2164
2165 device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002166 usb_gadget_set_selfpowered(gadget);
Remy Bohmerdf063442009-07-29 18:18:43 +02002167
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002168 /* For now RNDIS is always a second config */
2169 if (rndis)
2170 device_desc.bNumConfigurations = 2;
2171
Remy Bohmerdf063442009-07-29 18:18:43 +02002172 if (gadget_is_dualspeed(gadget)) {
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002173 if (rndis)
2174 dev_qualifier.bNumConfigurations = 2;
2175 else if (!cdc)
Remy Bohmerdf063442009-07-29 18:18:43 +02002176 dev_qualifier.bDeviceClass = USB_CLASS_VENDOR_SPEC;
2177
2178 /* assumes ep0 uses the same value for both speeds ... */
2179 dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0;
2180
2181 /* and that all endpoints are dual-speed */
2182 hs_source_desc.bEndpointAddress =
2183 fs_source_desc.bEndpointAddress;
2184 hs_sink_desc.bEndpointAddress =
2185 fs_sink_desc.bEndpointAddress;
Lukasz Dalek284c4cf2012-10-02 17:04:31 +02002186#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Remy Bohmerdf063442009-07-29 18:18:43 +02002187 if (status_ep)
2188 hs_status_desc.bEndpointAddress =
2189 fs_status_desc.bEndpointAddress;
2190#endif
2191 }
2192
2193 if (gadget_is_otg(gadget)) {
2194 otg_descriptor.bmAttributes |= USB_OTG_HNP,
2195 eth_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
2196 eth_config.bMaxPower = 4;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002197#ifdef CONFIG_USB_ETH_RNDIS
2198 rndis_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
2199 rndis_config.bMaxPower = 4;
2200#endif
Remy Bohmerdf063442009-07-29 18:18:43 +02002201 }
2202
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002203
2204 /* network device setup */
Remy Bohmerdf063442009-07-29 18:18:43 +02002205 dev->net = &l_netdev;
Remy Bohmerdf063442009-07-29 18:18:43 +02002206
2207 dev->cdc = cdc;
2208 dev->zlp = zlp;
2209
2210 dev->in_ep = in_ep;
2211 dev->out_ep = out_ep;
2212 dev->status_ep = status_ep;
2213
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002214 /*
2215 * Module params for these addresses should come from ID proms.
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002216 * The host side address is used with CDC and RNDIS, and commonly
Remy Bohmerdf063442009-07-29 18:18:43 +02002217 * ends up in a persistent config database. It's not clear if
2218 * host side code for the SAFE thing cares -- its original BLAN
2219 * thing didn't, Sharp never assigned those addresses on Zaurii.
2220 */
2221 get_ether_addr(dev_addr, dev->net->enetaddr);
2222
2223 memset(tmp, 0, sizeof(tmp));
2224 memcpy(tmp, dev->net->enetaddr, sizeof(dev->net->enetaddr));
2225
2226 get_ether_addr(host_addr, dev->host_mac);
2227
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002228 sprintf(ethaddr, "%02X%02X%02X%02X%02X%02X",
2229 dev->host_mac[0], dev->host_mac[1],
2230 dev->host_mac[2], dev->host_mac[3],
2231 dev->host_mac[4], dev->host_mac[5]);
Remy Bohmerdf063442009-07-29 18:18:43 +02002232
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002233 if (rndis) {
2234 status = rndis_init();
2235 if (status < 0) {
2236 error("can't init RNDIS, %d", status);
2237 goto fail;
2238 }
Remy Bohmerdf063442009-07-29 18:18:43 +02002239 }
2240
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002241 /*
2242 * use PKTSIZE (or aligned... from u-boot) and set
2243 * wMaxSegmentSize accordingly
2244 */
Remy Bohmerdf063442009-07-29 18:18:43 +02002245 dev->mtu = PKTSIZE_ALIGN; /* RNDIS does not like this, only 1514, TODO*/
2246
2247 /* preallocate control message data and buffer */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002248 dev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
Remy Bohmerdf063442009-07-29 18:18:43 +02002249 if (!dev->req)
2250 goto fail;
2251 dev->req->buf = control_req;
2252 dev->req->complete = eth_setup_complete;
2253
2254 /* ... and maybe likewise for status transfer */
Lukasz Dalek284c4cf2012-10-02 17:04:31 +02002255#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Remy Bohmerdf063442009-07-29 18:18:43 +02002256 if (dev->status_ep) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002257 dev->stat_req = usb_ep_alloc_request(dev->status_ep,
2258 GFP_KERNEL);
Remy Bohmerdf063442009-07-29 18:18:43 +02002259 if (!dev->stat_req) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002260 usb_ep_free_request(dev->status_ep, dev->req);
Remy Bohmerdf063442009-07-29 18:18:43 +02002261
2262 goto fail;
2263 }
Vitaly Kuzmichevef8d8d72010-08-13 17:01:06 +04002264 dev->stat_req->buf = status_req;
Remy Bohmerdf063442009-07-29 18:18:43 +02002265 dev->stat_req->context = NULL;
2266 }
2267#endif
2268
2269 /* finish hookup to lower layer ... */
2270 dev->gadget = gadget;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002271 set_gadget_data(gadget, dev);
Remy Bohmerdf063442009-07-29 18:18:43 +02002272 gadget->ep0->driver_data = dev;
2273
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002274 /*
2275 * two kinds of host-initiated state changes:
Remy Bohmerdf063442009-07-29 18:18:43 +02002276 * - iff DATA transfer is active, carrier is "on"
2277 * - tx queueing enabled if open *and* carrier is "on"
2278 */
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002279
2280 printf("using %s, OUT %s IN %s%s%s\n", gadget->name,
2281 out_ep->name, in_ep->name,
2282 status_ep ? " STATUS " : "",
2283 status_ep ? status_ep->name : ""
2284 );
2285 printf("MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
2286 dev->net->enetaddr[0], dev->net->enetaddr[1],
2287 dev->net->enetaddr[2], dev->net->enetaddr[3],
2288 dev->net->enetaddr[4], dev->net->enetaddr[5]);
2289
2290 if (cdc || rndis)
2291 printf("HOST MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
2292 dev->host_mac[0], dev->host_mac[1],
2293 dev->host_mac[2], dev->host_mac[3],
2294 dev->host_mac[4], dev->host_mac[5]);
2295
2296 if (rndis) {
2297 u32 vendorID = 0;
2298
2299 /* FIXME RNDIS vendor id == "vendor NIC code" == ? */
2300
2301 dev->rndis_config = rndis_register(rndis_control_ack);
2302 if (dev->rndis_config < 0) {
2303fail0:
2304 eth_unbind(gadget);
2305 debug("RNDIS setup failed\n");
2306 status = -ENODEV;
2307 goto fail;
2308 }
2309
2310 /* these set up a lot of the OIDs that RNDIS needs */
2311 rndis_set_host_mac(dev->rndis_config, dev->host_mac);
2312 if (rndis_set_param_dev(dev->rndis_config, dev->net, dev->mtu,
2313 &dev->stats, &dev->cdc_filter))
2314 goto fail0;
2315 if (rndis_set_param_vendor(dev->rndis_config, vendorID,
2316 manufacturer))
2317 goto fail0;
2318 if (rndis_set_param_medium(dev->rndis_config,
2319 NDIS_MEDIUM_802_3, 0))
2320 goto fail0;
2321 printf("RNDIS ready\n");
2322 }
Remy Bohmerdf063442009-07-29 18:18:43 +02002323 return 0;
2324
2325fail:
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002326 error("%s failed, status = %d", __func__, status);
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002327 eth_unbind(gadget);
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002328 return status;
Remy Bohmerdf063442009-07-29 18:18:43 +02002329}
2330
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002331/*-------------------------------------------------------------------------*/
2332
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002333static int usb_eth_init(struct eth_device *netdev, bd_t *bd)
Remy Bohmerdf063442009-07-29 18:18:43 +02002334{
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002335 struct eth_dev *dev = &l_ethdev;
Remy Bohmerdf063442009-07-29 18:18:43 +02002336 struct usb_gadget *gadget;
2337 unsigned long ts;
2338 unsigned long timeout = USB_CONNECT_TIMEOUT;
2339
2340 if (!netdev) {
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04002341 error("received NULL ptr");
Remy Bohmerdf063442009-07-29 18:18:43 +02002342 goto fail;
2343 }
Vitaly Kuzmichevbe0afbf2010-12-28 16:59:32 +03002344
2345 /* Configure default mac-addresses for the USB ethernet device */
2346#ifdef CONFIG_USBNET_DEV_ADDR
2347 strlcpy(dev_addr, CONFIG_USBNET_DEV_ADDR, sizeof(dev_addr));
2348#endif
2349#ifdef CONFIG_USBNET_HOST_ADDR
2350 strlcpy(host_addr, CONFIG_USBNET_HOST_ADDR, sizeof(host_addr));
2351#endif
2352 /* Check if the user overruled the MAC addresses */
2353 if (getenv("usbnet_devaddr"))
2354 strlcpy(dev_addr, getenv("usbnet_devaddr"),
2355 sizeof(dev_addr));
2356
2357 if (getenv("usbnet_hostaddr"))
2358 strlcpy(host_addr, getenv("usbnet_hostaddr"),
2359 sizeof(host_addr));
2360
2361 if (!is_eth_addr_valid(dev_addr)) {
2362 error("Need valid 'usbnet_devaddr' to be set");
2363 goto fail;
2364 }
2365 if (!is_eth_addr_valid(host_addr)) {
2366 error("Need valid 'usbnet_hostaddr' to be set");
2367 goto fail;
2368 }
2369
Lei Wen4af32732010-12-01 23:43:43 +08002370 if (usb_gadget_register_driver(&eth_driver) < 0)
2371 goto fail;
Remy Bohmerdf063442009-07-29 18:18:43 +02002372
2373 dev->network_started = 0;
Remy Bohmerdf063442009-07-29 18:18:43 +02002374
2375 packet_received = 0;
2376 packet_sent = 0;
2377
2378 gadget = dev->gadget;
2379 usb_gadget_connect(gadget);
2380
2381 if (getenv("cdc_connect_timeout"))
2382 timeout = simple_strtoul(getenv("cdc_connect_timeout"),
2383 NULL, 10) * CONFIG_SYS_HZ;
2384 ts = get_timer(0);
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002385 while (!l_ethdev.network_started) {
Remy Bohmerdf063442009-07-29 18:18:43 +02002386 /* Handle control-c and timeouts */
2387 if (ctrlc() || (get_timer(ts) > timeout)) {
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04002388 error("The remote end did not respond in time.");
Remy Bohmerdf063442009-07-29 18:18:43 +02002389 goto fail;
2390 }
2391 usb_gadget_handle_interrupts();
2392 }
2393
Vitaly Kuzmicheve4bd3862010-09-22 13:13:56 +04002394 packet_received = 0;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002395 rx_submit(dev, dev->rx_req, 0);
Remy Bohmerdf063442009-07-29 18:18:43 +02002396 return 0;
2397fail:
2398 return -1;
2399}
2400
Joe Hershbergere4e04882012-05-22 18:36:19 +00002401static int usb_eth_send(struct eth_device *netdev, void *packet, int length)
Remy Bohmerdf063442009-07-29 18:18:43 +02002402{
2403 int retval;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002404 void *rndis_pkt = NULL;
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04002405 struct eth_dev *dev = &l_ethdev;
Vitaly Kuzmichev2907b2c2010-09-22 13:13:55 +04002406 struct usb_request *req = dev->tx_req;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002407 unsigned long ts;
2408 unsigned long timeout = USB_CONNECT_TIMEOUT;
Remy Bohmerdf063442009-07-29 18:18:43 +02002409
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04002410 debug("%s:...\n", __func__);
Remy Bohmerdf063442009-07-29 18:18:43 +02002411
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002412 /* new buffer is needed to include RNDIS header */
2413 if (rndis_active(dev)) {
2414 rndis_pkt = malloc(length +
2415 sizeof(struct rndis_packet_msg_type));
2416 if (!rndis_pkt) {
2417 error("No memory to alloc RNDIS packet");
2418 goto drop;
2419 }
2420 rndis_add_hdr(rndis_pkt, length);
2421 memcpy(rndis_pkt + sizeof(struct rndis_packet_msg_type),
Joe Hershbergere4e04882012-05-22 18:36:19 +00002422 packet, length);
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002423 packet = rndis_pkt;
2424 length += sizeof(struct rndis_packet_msg_type);
2425 }
Joe Hershbergere4e04882012-05-22 18:36:19 +00002426 req->buf = packet;
Remy Bohmerdf063442009-07-29 18:18:43 +02002427 req->context = NULL;
2428 req->complete = tx_complete;
2429
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002430 /*
2431 * use zlp framing on tx for strict CDC-Ether conformance,
Remy Bohmerdf063442009-07-29 18:18:43 +02002432 * though any robust network rx path ignores extra padding.
2433 * and some hardware doesn't like to write zlps.
2434 */
2435 req->zero = 1;
2436 if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0)
2437 length++;
2438
2439 req->length = length;
2440#if 0
2441 /* throttle highspeed IRQ rate back slightly */
2442 if (gadget_is_dualspeed(dev->gadget))
2443 req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH)
2444 ? ((dev->tx_qlen % qmult) != 0) : 0;
2445#endif
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002446 dev->tx_qlen = 1;
2447 ts = get_timer(0);
2448 packet_sent = 0;
Remy Bohmerdf063442009-07-29 18:18:43 +02002449
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002450 retval = usb_ep_queue(dev->in_ep, req, GFP_ATOMIC);
Remy Bohmerdf063442009-07-29 18:18:43 +02002451
2452 if (!retval)
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04002453 debug("%s: packet queued\n", __func__);
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002454 while (!packet_sent) {
2455 if (get_timer(ts) > timeout) {
2456 printf("timeout sending packets to usb ethernet\n");
2457 return -1;
2458 }
2459 usb_gadget_handle_interrupts();
Remy Bohmerdf063442009-07-29 18:18:43 +02002460 }
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002461 if (rndis_pkt)
2462 free(rndis_pkt);
Remy Bohmerdf063442009-07-29 18:18:43 +02002463
2464 return 0;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002465drop:
2466 dev->stats.tx_dropped++;
2467 return -ENOMEM;
Remy Bohmerdf063442009-07-29 18:18:43 +02002468}
2469
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002470static int usb_eth_recv(struct eth_device *netdev)
Remy Bohmerdf063442009-07-29 18:18:43 +02002471{
2472 struct eth_dev *dev = &l_ethdev;
2473
2474 usb_gadget_handle_interrupts();
2475
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002476 if (packet_received) {
2477 debug("%s: packet received\n", __func__);
2478 if (dev->rx_req) {
2479 NetReceive(NetRxPackets[0], dev->rx_req->length);
2480 packet_received = 0;
Remy Bohmerdf063442009-07-29 18:18:43 +02002481
Vitaly Kuzmichev2907b2c2010-09-22 13:13:55 +04002482 rx_submit(dev, dev->rx_req, 0);
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002483 } else
2484 error("dev->rx_req invalid");
Remy Bohmerdf063442009-07-29 18:18:43 +02002485 }
2486 return 0;
2487}
2488
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002489void usb_eth_halt(struct eth_device *netdev)
Remy Bohmerdf063442009-07-29 18:18:43 +02002490{
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002491 struct eth_dev *dev = &l_ethdev;
Remy Bohmerdf063442009-07-29 18:18:43 +02002492
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002493 if (!netdev) {
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04002494 error("received NULL ptr");
Remy Bohmerdf063442009-07-29 18:18:43 +02002495 return;
2496 }
2497
Lei Wen4af32732010-12-01 23:43:43 +08002498 /* If the gadget not registered, simple return */
2499 if (!dev->gadget)
2500 return;
2501
Vitaly Kuzmichev3433c332011-02-11 18:18:35 +03002502 /*
2503 * Some USB controllers may need additional deinitialization here
2504 * before dropping pull-up (also due to hardware issues).
2505 * For example: unhandled interrupt with status stage started may
2506 * bring the controller to fully broken state (until board reset).
2507 * There are some variants to debug and fix such cases:
2508 * 1) In the case of RNDIS connection eth_stop can perform additional
2509 * interrupt handling. See RNDIS_COMPLETE_SIGNAL_DISCONNECT definition.
2510 * 2) 'pullup' callback in your UDC driver can be improved to perform
2511 * this deinitialization.
2512 */
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002513 eth_stop(dev);
2514
Remy Bohmerdf063442009-07-29 18:18:43 +02002515 usb_gadget_disconnect(dev->gadget);
Vitaly Kuzmichev08d4dd72011-02-11 18:18:31 +03002516
2517 /* Clear pending interrupt */
2518 if (dev->network_started) {
2519 usb_gadget_handle_interrupts();
2520 dev->network_started = 0;
2521 }
2522
Lei Wen4af32732010-12-01 23:43:43 +08002523 usb_gadget_unregister_driver(&eth_driver);
Remy Bohmerdf063442009-07-29 18:18:43 +02002524}
2525
2526static struct usb_gadget_driver eth_driver = {
2527 .speed = DEVSPEED,
2528
2529 .bind = eth_bind,
2530 .unbind = eth_unbind,
2531
2532 .setup = eth_setup,
2533 .disconnect = eth_disconnect,
2534
2535 .suspend = eth_suspend,
2536 .resume = eth_resume,
2537};
2538
2539int usb_eth_initialize(bd_t *bi)
2540{
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002541 struct eth_device *netdev = &l_netdev;
Remy Bohmerdf063442009-07-29 18:18:43 +02002542
Vitaly Kuzmicheva36cff12010-12-28 16:59:31 +03002543 strlcpy(netdev->name, USB_NET_NAME, sizeof(netdev->name));
Remy Bohmerdf063442009-07-29 18:18:43 +02002544
2545 netdev->init = usb_eth_init;
2546 netdev->send = usb_eth_send;
2547 netdev->recv = usb_eth_recv;
2548 netdev->halt = usb_eth_halt;
2549
2550#ifdef CONFIG_MCAST_TFTP
2551 #error not supported
2552#endif
Remy Bohmerdf063442009-07-29 18:18:43 +02002553 eth_register(netdev);
2554 return 0;
Remy Bohmerdf063442009-07-29 18:18:43 +02002555}