]> git.ipfire.org Git - thirdparty/u-boot.git/blame - drivers/usb/gadget/ether.c
Revert "Merge patch series "arm: dts: am62-beagleplay: Fix Beagleplay Ethernet""
[thirdparty/u-boot.git] / drivers / usb / gadget / ether.c
CommitLineData
83d290c5 1// SPDX-License-Identifier: GPL-2.0+
23cd1385
RB
2/*
3 * ether.c -- Ethernet gadget driver, with CDC and non-CDC options
4 *
5 * Copyright (C) 2003-2005,2008 David Brownell
6 * Copyright (C) 2003-2004 Robert Schwebel, Benedikt Spranger
7 * Copyright (C) 2008 Nokia Corporation
23cd1385
RB
8 */
9
d678a59d 10#include <common.h>
24b852a7 11#include <console.h>
9fb625ce 12#include <env.h>
f7ae49fc 13#include <log.h>
e6f6f9e6 14#include <part.h>
1221ce45 15#include <linux/errno.h>
c85d70ef 16#include <linux/netdevice.h>
1e94b46f 17#include <linux/printk.h>
23cd1385
RB
18#include <linux/usb/ch9.h>
19#include <linux/usb/cdc.h>
20#include <linux/usb/gadget.h>
21#include <net.h>
8bfc288c 22#include <usb.h>
7612a43d 23#include <malloc.h>
cf92e05c 24#include <memalign.h>
23cd1385
RB
25#include <linux/ctype.h>
26
27#include "gadget_chips.h"
7612a43d 28#include "rndis.h"
23cd1385 29
d4345aee 30#include <dm.h>
d4a37553 31#include <dm/lists.h>
d4345aee
M
32#include <dm/uclass-internal.h>
33#include <dm/device-internal.h>
34
8f7aa831 35#define USB_NET_NAME "usb_ether"
7de73185 36
23cd1385 37extern struct platform_data brd;
23cd1385
RB
38
39
40unsigned packet_received, packet_sent;
41
23cd1385
RB
42/*
43 * Ethernet gadget driver -- with CDC and non-CDC options
44 * Builds on hardware support for a full duplex link.
45 *
46 * CDC Ethernet is the standard USB solution for sending Ethernet frames
47 * using USB. Real hardware tends to use the same framing protocol but look
48 * different for control features. This driver strongly prefers to use
49 * this USB-IF standard as its open-systems interoperability solution;
50 * most host side USB stacks (except from Microsoft) support it.
51 *
52 * This is sometimes called "CDC ECM" (Ethernet Control Model) to support
53 * TLA-soup. "CDC ACM" (Abstract Control Model) is for modems, and a new
54 * "CDC EEM" (Ethernet Emulation Model) is starting to spread.
55 *
56 * There's some hardware that can't talk CDC ECM. We make that hardware
57 * implement a "minimalist" vendor-agnostic CDC core: same framing, but
58 * link-level setup only requires activating the configuration. Only the
59 * endpoint descriptors, and product/vendor IDs, are relevant; no control
60 * operations are available. Linux supports it, but other host operating
61 * systems may not. (This is a subset of CDC Ethernet.)
62 *
63 * It turns out that if you add a few descriptors to that "CDC Subset",
64 * (Windows) host side drivers from MCCI can treat it as one submode of
65 * a proprietary scheme called "SAFE" ... without needing to know about
66 * specific product/vendor IDs. So we do that, making it easier to use
67 * those MS-Windows drivers. Those added descriptors make it resemble a
68 * CDC MDLM device, but they don't change device behavior at all. (See
69 * MCCI Engineering report 950198 "SAFE Networking Functions".)
70 *
71 * A third option is also in use. Rather than CDC Ethernet, or something
72 * simpler, Microsoft pushes their own approach: RNDIS. The published
73 * RNDIS specs are ambiguous and appear to be incomplete, and are also
74 * needlessly complex. They borrow more from CDC ACM than CDC ECM.
75 */
23cd1385
RB
76
77#define DRIVER_DESC "Ethernet Gadget"
78/* Based on linux 2.6.27 version */
79#define DRIVER_VERSION "May Day 2005"
80
6142e0ae 81static const char driver_desc[] = DRIVER_DESC;
23cd1385
RB
82
83#define RX_EXTRA 20 /* guard against rx overflows */
84
7612a43d
VK
85#ifndef CONFIG_USB_ETH_RNDIS
86#define rndis_uninit(x) do {} while (0)
87#define rndis_deregister(c) do {} while (0)
88#define rndis_exit() do {} while (0)
89#endif
90
91/* CDC and RNDIS support the same host-chosen outgoing packet filters. */
23cd1385
RB
92#define DEFAULT_FILTER (USB_CDC_PACKET_TYPE_BROADCAST \
93 |USB_CDC_PACKET_TYPE_ALL_MULTICAST \
94 |USB_CDC_PACKET_TYPE_PROMISCUOUS \
95 |USB_CDC_PACKET_TYPE_DIRECTED)
96
97#define USB_CONNECT_TIMEOUT (3 * CONFIG_SYS_HZ)
98
8b6b66b4
VK
99/*-------------------------------------------------------------------------*/
100
101struct eth_dev {
102 struct usb_gadget *gadget;
103 struct usb_request *req; /* for control responses */
7612a43d 104 struct usb_request *stat_req; /* for cdc & rndis status */
8b6b66b4
VK
105
106 u8 config;
107 struct usb_ep *in_ep, *out_ep, *status_ep;
108 const struct usb_endpoint_descriptor
109 *in, *out, *status;
110
111 struct usb_request *tx_req, *rx_req;
112
d4a37553 113 struct udevice *net;
8b6b66b4
VK
114 struct net_device_stats stats;
115 unsigned int tx_qlen;
116
117 unsigned zlp:1;
118 unsigned cdc:1;
7612a43d 119 unsigned rndis:1;
8b6b66b4
VK
120 unsigned suspended:1;
121 unsigned network_started:1;
122 u16 cdc_filter;
123 unsigned long todo;
124 int mtu;
125#define WORK_RX_MEMORY 0
7612a43d 126 int rndis_config;
8b6b66b4
VK
127 u8 host_mac[ETH_ALEN];
128};
129
130/*
131 * This version autoconfigures as much as possible at run-time.
132 *
133 * It also ASSUMES a self-powered device, without remote wakeup,
134 * although remote wakeup support would make sense.
135 */
136
23cd1385 137/*-------------------------------------------------------------------------*/
5cb3b9d7
M
138struct ether_priv {
139 struct eth_dev ethdev;
d4a37553 140 struct udevice *netdev;
5cb3b9d7
M
141 struct usb_gadget_driver eth_driver;
142};
143
144struct ether_priv eth_priv;
145struct ether_priv *l_priv = &eth_priv;
23cd1385
RB
146
147/*-------------------------------------------------------------------------*/
148
149/* "main" config is either CDC, or its simple subset */
150static inline int is_cdc(struct eth_dev *dev)
151{
2bb37884 152#if !defined(CONFIG_USB_ETH_SUBSET)
23cd1385 153 return 1; /* only cdc possible */
2bb37884 154#elif !defined(CONFIG_USB_ETH_CDC)
23cd1385
RB
155 return 0; /* only subset possible */
156#else
157 return dev->cdc; /* depends on what hardware we found */
158#endif
159}
160
7612a43d
VK
161/* "secondary" RNDIS config may sometimes be activated */
162static inline int rndis_active(struct eth_dev *dev)
163{
164#ifdef CONFIG_USB_ETH_RNDIS
165 return dev->rndis;
166#else
167 return 0;
168#endif
169}
170
171#define subset_active(dev) (!is_cdc(dev) && !rndis_active(dev))
172#define cdc_active(dev) (is_cdc(dev) && !rndis_active(dev))
23cd1385
RB
173
174#define DEFAULT_QLEN 2 /* double buffering by default */
175
176/* peak bulk transfer bits-per-second */
177#define HS_BPS (13 * 512 * 8 * 1000 * 8)
178#define FS_BPS (19 * 64 * 1 * 1000 * 8)
179
180#ifdef CONFIG_USB_GADGET_DUALSPEED
181#define DEVSPEED USB_SPEED_HIGH
182
2721dbf1
VK
183#ifdef CONFIG_USB_ETH_QMULT
184#define qmult CONFIG_USB_ETH_QMULT
185#else
186#define qmult 5
187#endif
188
23cd1385
RB
189/* for dual-speed hardware, use deeper queues at highspeed */
190#define qlen(gadget) \
191 (DEFAULT_QLEN*((gadget->speed == USB_SPEED_HIGH) ? qmult : 1))
192
193static inline int BITRATE(struct usb_gadget *g)
194{
195 return (g->speed == USB_SPEED_HIGH) ? HS_BPS : FS_BPS;
196}
197
198#else /* full speed (low speed doesn't do bulk) */
199
200#define qmult 1
201
202#define DEVSPEED USB_SPEED_FULL
203
204#define qlen(gadget) DEFAULT_QLEN
205
206static inline int BITRATE(struct usb_gadget *g)
207{
208 return FS_BPS;
209}
210#endif
211
23cd1385
RB
212/*-------------------------------------------------------------------------*/
213
6142e0ae
VK
214/*
215 * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
23cd1385
RB
216 * Instead: allocate your own, using normal USB-IF procedures.
217 */
218
6142e0ae
VK
219/*
220 * Thanks to NetChip Technologies for donating this product ID.
23cd1385
RB
221 * It's for devices with only CDC Ethernet configurations.
222 */
223#define CDC_VENDOR_NUM 0x0525 /* NetChip */
224#define CDC_PRODUCT_NUM 0xa4a1 /* Linux-USB Ethernet Gadget */
225
6142e0ae
VK
226/*
227 * For hardware that can't talk CDC, we use the same vendor ID that
23cd1385
RB
228 * ARM Linux has used for ethernet-over-usb, both with sa1100 and
229 * with pxa250. We're protocol-compatible, if the host-side drivers
230 * use the endpoint descriptors. bcdDevice (version) is nonzero, so
231 * drivers that need to hard-wire endpoint numbers have a hook.
232 *
233 * The protocol is a minimal subset of CDC Ether, which works on any bulk
234 * hardware that's not deeply broken ... even on hardware that can't talk
235 * RNDIS (like SA-1100, with no interrupt endpoint, or anything that
236 * doesn't handle control-OUT).
237 */
7612a43d
VK
238#define SIMPLE_VENDOR_NUM 0x049f /* Compaq Computer Corp. */
239#define SIMPLE_PRODUCT_NUM 0x505a /* Linux-USB "CDC Subset" Device */
240
241/*
242 * For hardware that can talk RNDIS and either of the above protocols,
243 * use this ID ... the windows INF files will know it. Unless it's
244 * used with CDC Ethernet, Linux 2.4 hosts will need updates to choose
245 * the non-RNDIS configuration.
246 */
247#define RNDIS_VENDOR_NUM 0x0525 /* NetChip */
248#define RNDIS_PRODUCT_NUM 0xa4a2 /* Ethernet/RNDIS Gadget */
23cd1385 249
6142e0ae
VK
250/*
251 * Some systems will want different product identifers published in the
23cd1385
RB
252 * device descriptor, either numbers or strings or both. These string
253 * parameters are in UTF-8 (superset of ASCII's 7 bit characters).
254 */
255
7612a43d
VK
256/*
257 * Emulating them in eth_bind:
258 * static ushort idVendor;
259 * static ushort idProduct;
260 */
261
10ac57fd
MR
262#if defined(CONFIG_USB_GADGET_MANUFACTURER)
263static char *iManufacturer = CONFIG_USB_GADGET_MANUFACTURER;
23cd1385 264#else
a187559e 265static char *iManufacturer = "U-Boot";
23cd1385 266#endif
7612a43d
VK
267
268/* These probably need to be configurable. */
269static ushort bcdDevice;
23cd1385
RB
270static char *iProduct;
271static char *iSerialNumber;
7612a43d 272
23cd1385 273static char dev_addr[18];
7612a43d 274
23cd1385
RB
275static char host_addr[18];
276
7612a43d 277
23cd1385
RB
278/*-------------------------------------------------------------------------*/
279
6142e0ae
VK
280/*
281 * USB DRIVER HOOKUP (to the hardware driver, below us), mostly
23cd1385
RB
282 * ep0 implementation: descriptors, config management, setup().
283 * also optional class-specific notification interrupt transfer.
284 */
285
286/*
287 * DESCRIPTORS ... most are static, but strings and (full) configuration
288 * descriptors are built on demand. For now we do either full CDC, or
7612a43d
VK
289 * our simple subset, with RNDIS as an optional second configuration.
290 *
291 * RNDIS includes some CDC ACM descriptors ... like CDC Ethernet. But
292 * the class descriptors match a modem (they're ignored; it's really just
293 * Ethernet functionality), they don't need the NOP altsetting, and the
294 * status transfer endpoint isn't optional.
23cd1385
RB
295 */
296
297#define STRING_MANUFACTURER 1
298#define STRING_PRODUCT 2
299#define STRING_ETHADDR 3
300#define STRING_DATA 4
301#define STRING_CONTROL 5
7612a43d 302#define STRING_RNDIS_CONTROL 6
23cd1385
RB
303#define STRING_CDC 7
304#define STRING_SUBSET 8
7612a43d 305#define STRING_RNDIS 9
23cd1385
RB
306#define STRING_SERIALNUMBER 10
307
7612a43d 308/* holds our biggest descriptor (or RNDIS response) */
23cd1385
RB
309#define USB_BUFSIZ 256
310
311/*
7612a43d
VK
312 * This device advertises one configuration, eth_config, unless RNDIS
313 * is enabled (rndis_config) on hardware supporting at least two configs.
314 *
315 * NOTE: Controllers like superh_udc should probably be able to use
316 * an RNDIS-only configuration.
23cd1385
RB
317 *
318 * FIXME define some higher-powered configurations to make it easier
319 * to recharge batteries ...
320 */
321
322#define DEV_CONFIG_VALUE 1 /* cdc or subset */
7612a43d 323#define DEV_RNDIS_CONFIG_VALUE 2 /* rndis; optional */
23cd1385
RB
324
325static struct usb_device_descriptor
326device_desc = {
327 .bLength = sizeof device_desc,
328 .bDescriptorType = USB_DT_DEVICE,
329
6142e0ae 330 .bcdUSB = __constant_cpu_to_le16(0x0200),
23cd1385
RB
331
332 .bDeviceClass = USB_CLASS_COMM,
333 .bDeviceSubClass = 0,
334 .bDeviceProtocol = 0,
335
6142e0ae
VK
336 .idVendor = __constant_cpu_to_le16(CDC_VENDOR_NUM),
337 .idProduct = __constant_cpu_to_le16(CDC_PRODUCT_NUM),
23cd1385
RB
338 .iManufacturer = STRING_MANUFACTURER,
339 .iProduct = STRING_PRODUCT,
340 .bNumConfigurations = 1,
341};
342
343static struct usb_otg_descriptor
344otg_descriptor = {
345 .bLength = sizeof otg_descriptor,
346 .bDescriptorType = USB_DT_OTG,
347
348 .bmAttributes = USB_OTG_SRP,
349};
350
351static struct usb_config_descriptor
352eth_config = {
353 .bLength = sizeof eth_config,
354 .bDescriptorType = USB_DT_CONFIG,
355
356 /* compute wTotalLength on the fly */
357 .bNumInterfaces = 2,
358 .bConfigurationValue = DEV_CONFIG_VALUE,
359 .iConfiguration = STRING_CDC,
360 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
361 .bMaxPower = 1,
362};
363
7612a43d
VK
364#ifdef CONFIG_USB_ETH_RNDIS
365static struct usb_config_descriptor
366rndis_config = {
367 .bLength = sizeof rndis_config,
368 .bDescriptorType = USB_DT_CONFIG,
369
370 /* compute wTotalLength on the fly */
371 .bNumInterfaces = 2,
372 .bConfigurationValue = DEV_RNDIS_CONFIG_VALUE,
373 .iConfiguration = STRING_RNDIS,
374 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
375 .bMaxPower = 1,
376};
377#endif
378
23cd1385
RB
379/*
380 * Compared to the simple CDC subset, the full CDC Ethernet model adds
381 * three class descriptors, two interface descriptors, optional status
382 * endpoint. Both have a "data" interface and two bulk endpoints.
383 * There are also differences in how control requests are handled.
7612a43d
VK
384 *
385 * RNDIS shares a lot with CDC-Ethernet, since it's a variant of the
386 * CDC-ACM (modem) spec. Unfortunately MSFT's RNDIS driver is buggy; it
387 * may hang or oops. Since bugfixes (or accurate specs, letting Linux
388 * work around those bugs) are unlikely to ever come from MSFT, you may
389 * wish to avoid using RNDIS.
390 *
391 * MCCI offers an alternative to RNDIS if you need to connect to Windows
392 * but have hardware that can't support CDC Ethernet. We add descriptors
393 * to present the CDC Subset as a (nonconformant) CDC MDLM variant called
394 * "SAFE". That borrows from both CDC Ethernet and CDC MDLM. You can
395 * get those drivers from MCCI, or bundled with various products.
23cd1385
RB
396 */
397
2bb37884 398#ifdef CONFIG_USB_ETH_CDC
23cd1385
RB
399static struct usb_interface_descriptor
400control_intf = {
401 .bLength = sizeof control_intf,
402 .bDescriptorType = USB_DT_INTERFACE,
403
404 .bInterfaceNumber = 0,
405 /* status endpoint is optional; this may be patched later */
406 .bNumEndpoints = 1,
407 .bInterfaceClass = USB_CLASS_COMM,
408 .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET,
409 .bInterfaceProtocol = USB_CDC_PROTO_NONE,
410 .iInterface = STRING_CONTROL,
411};
412#endif
413
7612a43d
VK
414#ifdef CONFIG_USB_ETH_RNDIS
415static const struct usb_interface_descriptor
416rndis_control_intf = {
417 .bLength = sizeof rndis_control_intf,
418 .bDescriptorType = USB_DT_INTERFACE,
419
420 .bInterfaceNumber = 0,
421 .bNumEndpoints = 1,
422 .bInterfaceClass = USB_CLASS_COMM,
423 .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
424 .bInterfaceProtocol = USB_CDC_ACM_PROTO_VENDOR,
425 .iInterface = STRING_RNDIS_CONTROL,
426};
427#endif
428
23cd1385
RB
429static const struct usb_cdc_header_desc header_desc = {
430 .bLength = sizeof header_desc,
431 .bDescriptorType = USB_DT_CS_INTERFACE,
432 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
433
6142e0ae 434 .bcdCDC = __constant_cpu_to_le16(0x0110),
23cd1385
RB
435};
436
2bb37884 437#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
23cd1385
RB
438
439static const struct usb_cdc_union_desc union_desc = {
440 .bLength = sizeof union_desc,
441 .bDescriptorType = USB_DT_CS_INTERFACE,
442 .bDescriptorSubType = USB_CDC_UNION_TYPE,
443
444 .bMasterInterface0 = 0, /* index of control interface */
445 .bSlaveInterface0 = 1, /* index of DATA interface */
446};
447
7612a43d
VK
448#endif /* CDC || RNDIS */
449
450#ifdef CONFIG_USB_ETH_RNDIS
451
452static const struct usb_cdc_call_mgmt_descriptor call_mgmt_descriptor = {
453 .bLength = sizeof call_mgmt_descriptor,
454 .bDescriptorType = USB_DT_CS_INTERFACE,
455 .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE,
456
457 .bmCapabilities = 0x00,
458 .bDataInterface = 0x01,
459};
460
461static const struct usb_cdc_acm_descriptor acm_descriptor = {
462 .bLength = sizeof acm_descriptor,
463 .bDescriptorType = USB_DT_CS_INTERFACE,
464 .bDescriptorSubType = USB_CDC_ACM_TYPE,
465
466 .bmCapabilities = 0x00,
467};
468
469#endif
23cd1385 470
2bb37884 471#ifndef CONFIG_USB_ETH_CDC
23cd1385 472
6142e0ae
VK
473/*
474 * "SAFE" loosely follows CDC WMC MDLM, violating the spec in various
23cd1385
RB
475 * ways: data endpoints live in the control interface, there's no data
476 * interface, and it's not used to talk to a cell phone radio.
477 */
478
479static const struct usb_cdc_mdlm_desc mdlm_desc = {
480 .bLength = sizeof mdlm_desc,
481 .bDescriptorType = USB_DT_CS_INTERFACE,
482 .bDescriptorSubType = USB_CDC_MDLM_TYPE,
483
484 .bcdVersion = __constant_cpu_to_le16(0x0100),
485 .bGUID = {
486 0x5d, 0x34, 0xcf, 0x66, 0x11, 0x18, 0x11, 0xd6,
487 0xa2, 0x1a, 0x00, 0x01, 0x02, 0xca, 0x9a, 0x7f,
488 },
489};
490
6142e0ae
VK
491/*
492 * since "usb_cdc_mdlm_detail_desc" is a variable length structure, we
23cd1385
RB
493 * can't really use its struct. All we do here is say that we're using
494 * the submode of "SAFE" which directly matches the CDC Subset.
495 */
65c389d2 496#ifdef CONFIG_USB_ETH_SUBSET
23cd1385
RB
497static const u8 mdlm_detail_desc[] = {
498 6,
499 USB_DT_CS_INTERFACE,
500 USB_CDC_MDLM_DETAIL_TYPE,
501
502 0, /* "SAFE" */
503 0, /* network control capabilities (none) */
504 0, /* network data capabilities ("raw" encapsulation) */
505};
65c389d2 506#endif
23cd1385
RB
507
508#endif
509
23cd1385 510static const struct usb_cdc_ether_desc ether_desc = {
6142e0ae 511 .bLength = sizeof(ether_desc),
23cd1385
RB
512 .bDescriptorType = USB_DT_CS_INTERFACE,
513 .bDescriptorSubType = USB_CDC_ETHERNET_TYPE,
514
515 /* this descriptor actually adds value, surprise! */
516 .iMACAddress = STRING_ETHADDR,
6142e0ae 517 .bmEthernetStatistics = __constant_cpu_to_le32(0), /* no statistics */
dda52510 518 .wMaxSegmentSize = __constant_cpu_to_le16(PKTSIZE_ALIGN),
6142e0ae 519 .wNumberMCFilters = __constant_cpu_to_le16(0),
23cd1385
RB
520 .bNumberPowerFilters = 0,
521};
522
2bb37884 523#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
23cd1385 524
6142e0ae
VK
525/*
526 * include the status endpoint if we can, even where it's optional.
23cd1385
RB
527 * use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one
528 * packet, to simplify cancellation; and a big transfer interval, to
529 * waste less bandwidth.
530 *
531 * some drivers (like Linux 2.4 cdc-ether!) "need" it to exist even
532 * if they ignore the connect/disconnect notifications that real aether
533 * can provide. more advanced cdc configurations might want to support
534 * encapsulated commands (vendor-specific, using control-OUT).
7612a43d
VK
535 *
536 * RNDIS requires the status endpoint, since it uses that encapsulation
537 * mechanism for its funky RPC scheme.
23cd1385
RB
538 */
539
540#define LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */
541#define STATUS_BYTECOUNT 16 /* 8 byte header + data */
542
543static struct usb_endpoint_descriptor
544fs_status_desc = {
545 .bLength = USB_DT_ENDPOINT_SIZE,
546 .bDescriptorType = USB_DT_ENDPOINT,
547
548 .bEndpointAddress = USB_DIR_IN,
549 .bmAttributes = USB_ENDPOINT_XFER_INT,
6142e0ae 550 .wMaxPacketSize = __constant_cpu_to_le16(STATUS_BYTECOUNT),
23cd1385
RB
551 .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC,
552};
553#endif
554
2bb37884 555#ifdef CONFIG_USB_ETH_CDC
23cd1385
RB
556
557/* the default data interface has no endpoints ... */
558
559static const struct usb_interface_descriptor
560data_nop_intf = {
561 .bLength = sizeof data_nop_intf,
562 .bDescriptorType = USB_DT_INTERFACE,
563
564 .bInterfaceNumber = 1,
565 .bAlternateSetting = 0,
566 .bNumEndpoints = 0,
567 .bInterfaceClass = USB_CLASS_CDC_DATA,
568 .bInterfaceSubClass = 0,
569 .bInterfaceProtocol = 0,
570};
571
572/* ... but the "real" data interface has two bulk endpoints */
573
574static const struct usb_interface_descriptor
575data_intf = {
576 .bLength = sizeof data_intf,
577 .bDescriptorType = USB_DT_INTERFACE,
578
579 .bInterfaceNumber = 1,
580 .bAlternateSetting = 1,
581 .bNumEndpoints = 2,
582 .bInterfaceClass = USB_CLASS_CDC_DATA,
583 .bInterfaceSubClass = 0,
584 .bInterfaceProtocol = 0,
585 .iInterface = STRING_DATA,
586};
587
588#endif
589
7612a43d
VK
590#ifdef CONFIG_USB_ETH_RNDIS
591
592/* RNDIS doesn't activate by changing to the "real" altsetting */
593
594static const struct usb_interface_descriptor
595rndis_data_intf = {
596 .bLength = sizeof rndis_data_intf,
597 .bDescriptorType = USB_DT_INTERFACE,
598
599 .bInterfaceNumber = 1,
600 .bAlternateSetting = 0,
601 .bNumEndpoints = 2,
602 .bInterfaceClass = USB_CLASS_CDC_DATA,
603 .bInterfaceSubClass = 0,
604 .bInterfaceProtocol = 0,
605 .iInterface = STRING_DATA,
606};
607
608#endif
609
2bb37884 610#ifdef CONFIG_USB_ETH_SUBSET
23cd1385
RB
611
612/*
613 * "Simple" CDC-subset option is a simple vendor-neutral model that most
614 * full speed controllers can handle: one interface, two bulk endpoints.
615 *
616 * To assist host side drivers, we fancy it up a bit, and add descriptors
617 * so some host side drivers will understand it as a "SAFE" variant.
618 */
619
620static const struct usb_interface_descriptor
621subset_data_intf = {
622 .bLength = sizeof subset_data_intf,
623 .bDescriptorType = USB_DT_INTERFACE,
624
625 .bInterfaceNumber = 0,
626 .bAlternateSetting = 0,
627 .bNumEndpoints = 2,
628 .bInterfaceClass = USB_CLASS_COMM,
629 .bInterfaceSubClass = USB_CDC_SUBCLASS_MDLM,
630 .bInterfaceProtocol = 0,
631 .iInterface = STRING_DATA,
632};
633
634#endif /* SUBSET */
635
23cd1385
RB
636static struct usb_endpoint_descriptor
637fs_source_desc = {
638 .bLength = USB_DT_ENDPOINT_SIZE,
639 .bDescriptorType = USB_DT_ENDPOINT,
640
641 .bEndpointAddress = USB_DIR_IN,
642 .bmAttributes = USB_ENDPOINT_XFER_BULK,
43880ce5 643 .wMaxPacketSize = __constant_cpu_to_le16(64),
23cd1385
RB
644};
645
646static struct usb_endpoint_descriptor
647fs_sink_desc = {
648 .bLength = USB_DT_ENDPOINT_SIZE,
649 .bDescriptorType = USB_DT_ENDPOINT,
650
651 .bEndpointAddress = USB_DIR_OUT,
652 .bmAttributes = USB_ENDPOINT_XFER_BULK,
43880ce5 653 .wMaxPacketSize = __constant_cpu_to_le16(64),
23cd1385
RB
654};
655
6142e0ae 656static const struct usb_descriptor_header *fs_eth_function[11] = {
23cd1385 657 (struct usb_descriptor_header *) &otg_descriptor,
2bb37884 658#ifdef CONFIG_USB_ETH_CDC
23cd1385
RB
659 /* "cdc" mode descriptors */
660 (struct usb_descriptor_header *) &control_intf,
661 (struct usb_descriptor_header *) &header_desc,
662 (struct usb_descriptor_header *) &union_desc,
663 (struct usb_descriptor_header *) &ether_desc,
664 /* NOTE: status endpoint may need to be removed */
665 (struct usb_descriptor_header *) &fs_status_desc,
666 /* data interface, with altsetting */
667 (struct usb_descriptor_header *) &data_nop_intf,
668 (struct usb_descriptor_header *) &data_intf,
669 (struct usb_descriptor_header *) &fs_source_desc,
670 (struct usb_descriptor_header *) &fs_sink_desc,
671 NULL,
2bb37884 672#endif /* CONFIG_USB_ETH_CDC */
23cd1385
RB
673};
674
675static inline void fs_subset_descriptors(void)
676{
2bb37884 677#ifdef CONFIG_USB_ETH_SUBSET
23cd1385
RB
678 /* behavior is "CDC Subset"; extra descriptors say "SAFE" */
679 fs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
680 fs_eth_function[2] = (struct usb_descriptor_header *) &header_desc;
681 fs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc;
682 fs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc;
683 fs_eth_function[5] = (struct usb_descriptor_header *) &ether_desc;
684 fs_eth_function[6] = (struct usb_descriptor_header *) &fs_source_desc;
685 fs_eth_function[7] = (struct usb_descriptor_header *) &fs_sink_desc;
686 fs_eth_function[8] = NULL;
687#else
688 fs_eth_function[1] = NULL;
689#endif
690}
691
7612a43d
VK
692#ifdef CONFIG_USB_ETH_RNDIS
693static const struct usb_descriptor_header *fs_rndis_function[] = {
694 (struct usb_descriptor_header *) &otg_descriptor,
695 /* control interface matches ACM, not Ethernet */
696 (struct usb_descriptor_header *) &rndis_control_intf,
697 (struct usb_descriptor_header *) &header_desc,
698 (struct usb_descriptor_header *) &call_mgmt_descriptor,
699 (struct usb_descriptor_header *) &acm_descriptor,
700 (struct usb_descriptor_header *) &union_desc,
701 (struct usb_descriptor_header *) &fs_status_desc,
702 /* data interface has no altsetting */
703 (struct usb_descriptor_header *) &rndis_data_intf,
704 (struct usb_descriptor_header *) &fs_source_desc,
705 (struct usb_descriptor_header *) &fs_sink_desc,
706 NULL,
707};
708#endif
709
23cd1385
RB
710/*
711 * usb 2.0 devices need to expose both high speed and full speed
712 * descriptors, unless they only run at full speed.
713 */
714
2bb37884 715#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
23cd1385
RB
716static struct usb_endpoint_descriptor
717hs_status_desc = {
718 .bLength = USB_DT_ENDPOINT_SIZE,
719 .bDescriptorType = USB_DT_ENDPOINT,
720
721 .bmAttributes = USB_ENDPOINT_XFER_INT,
6142e0ae 722 .wMaxPacketSize = __constant_cpu_to_le16(STATUS_BYTECOUNT),
23cd1385
RB
723 .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4,
724};
2bb37884 725#endif /* CONFIG_USB_ETH_CDC */
23cd1385
RB
726
727static struct usb_endpoint_descriptor
728hs_source_desc = {
729 .bLength = USB_DT_ENDPOINT_SIZE,
730 .bDescriptorType = USB_DT_ENDPOINT,
731
732 .bmAttributes = USB_ENDPOINT_XFER_BULK,
6142e0ae 733 .wMaxPacketSize = __constant_cpu_to_le16(512),
23cd1385
RB
734};
735
736static struct usb_endpoint_descriptor
737hs_sink_desc = {
738 .bLength = USB_DT_ENDPOINT_SIZE,
739 .bDescriptorType = USB_DT_ENDPOINT,
740
741 .bmAttributes = USB_ENDPOINT_XFER_BULK,
6142e0ae 742 .wMaxPacketSize = __constant_cpu_to_le16(512),
23cd1385
RB
743};
744
745static struct usb_qualifier_descriptor
746dev_qualifier = {
747 .bLength = sizeof dev_qualifier,
748 .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
749
6142e0ae 750 .bcdUSB = __constant_cpu_to_le16(0x0200),
23cd1385
RB
751 .bDeviceClass = USB_CLASS_COMM,
752
753 .bNumConfigurations = 1,
754};
755
6142e0ae 756static const struct usb_descriptor_header *hs_eth_function[11] = {
23cd1385 757 (struct usb_descriptor_header *) &otg_descriptor,
2bb37884 758#ifdef CONFIG_USB_ETH_CDC
23cd1385
RB
759 /* "cdc" mode descriptors */
760 (struct usb_descriptor_header *) &control_intf,
761 (struct usb_descriptor_header *) &header_desc,
762 (struct usb_descriptor_header *) &union_desc,
763 (struct usb_descriptor_header *) &ether_desc,
764 /* NOTE: status endpoint may need to be removed */
765 (struct usb_descriptor_header *) &hs_status_desc,
766 /* data interface, with altsetting */
767 (struct usb_descriptor_header *) &data_nop_intf,
768 (struct usb_descriptor_header *) &data_intf,
769 (struct usb_descriptor_header *) &hs_source_desc,
770 (struct usb_descriptor_header *) &hs_sink_desc,
771 NULL,
2bb37884 772#endif /* CONFIG_USB_ETH_CDC */
23cd1385
RB
773};
774
775static inline void hs_subset_descriptors(void)
776{
2bb37884 777#ifdef CONFIG_USB_ETH_SUBSET
23cd1385
RB
778 /* behavior is "CDC Subset"; extra descriptors say "SAFE" */
779 hs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
780 hs_eth_function[2] = (struct usb_descriptor_header *) &header_desc;
781 hs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc;
782 hs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc;
783 hs_eth_function[5] = (struct usb_descriptor_header *) &ether_desc;
784 hs_eth_function[6] = (struct usb_descriptor_header *) &hs_source_desc;
785 hs_eth_function[7] = (struct usb_descriptor_header *) &hs_sink_desc;
786 hs_eth_function[8] = NULL;
787#else
788 hs_eth_function[1] = NULL;
789#endif
790}
791
7612a43d
VK
792#ifdef CONFIG_USB_ETH_RNDIS
793static const struct usb_descriptor_header *hs_rndis_function[] = {
794 (struct usb_descriptor_header *) &otg_descriptor,
795 /* control interface matches ACM, not Ethernet */
796 (struct usb_descriptor_header *) &rndis_control_intf,
797 (struct usb_descriptor_header *) &header_desc,
798 (struct usb_descriptor_header *) &call_mgmt_descriptor,
799 (struct usb_descriptor_header *) &acm_descriptor,
800 (struct usb_descriptor_header *) &union_desc,
801 (struct usb_descriptor_header *) &hs_status_desc,
802 /* data interface has no altsetting */
803 (struct usb_descriptor_header *) &rndis_data_intf,
804 (struct usb_descriptor_header *) &hs_source_desc,
805 (struct usb_descriptor_header *) &hs_sink_desc,
806 NULL,
807};
808#endif
809
810
23cd1385
RB
811/* maxpacket and other transfer characteristics vary by speed. */
812static inline struct usb_endpoint_descriptor *
813ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *hs,
814 struct usb_endpoint_descriptor *fs)
815{
816 if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
817 return hs;
818 return fs;
819}
820
23cd1385
RB
821/*-------------------------------------------------------------------------*/
822
823/* descriptors that are built on-demand */
824
6142e0ae
VK
825static char manufacturer[50];
826static char product_desc[40] = DRIVER_DESC;
827static char serial_number[20];
23cd1385
RB
828
829/* address that the host will use ... usually assigned at random */
6142e0ae 830static char ethaddr[2 * ETH_ALEN + 1];
23cd1385
RB
831
832/* static strings, in UTF-8 */
6142e0ae 833static struct usb_string strings[] = {
23cd1385
RB
834 { STRING_MANUFACTURER, manufacturer, },
835 { STRING_PRODUCT, product_desc, },
836 { STRING_SERIALNUMBER, serial_number, },
837 { STRING_DATA, "Ethernet Data", },
838 { STRING_ETHADDR, ethaddr, },
2bb37884 839#ifdef CONFIG_USB_ETH_CDC
23cd1385
RB
840 { STRING_CDC, "CDC Ethernet", },
841 { STRING_CONTROL, "CDC Communications Control", },
842#endif
2bb37884 843#ifdef CONFIG_USB_ETH_SUBSET
23cd1385 844 { STRING_SUBSET, "CDC Ethernet Subset", },
7612a43d
VK
845#endif
846#ifdef CONFIG_USB_ETH_RNDIS
847 { STRING_RNDIS, "RNDIS", },
848 { STRING_RNDIS_CONTROL, "RNDIS Communications Control", },
23cd1385
RB
849#endif
850 { } /* end of list */
851};
852
853static struct usb_gadget_strings stringtab = {
854 .language = 0x0409, /* en-us */
855 .strings = strings,
856};
857
23cd1385 858/*============================================================================*/
5290759c
JF
859DEFINE_CACHE_ALIGN_BUFFER(u8, control_req, USB_BUFSIZ);
860
2bb37884 861#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
5290759c 862DEFINE_CACHE_ALIGN_BUFFER(u8, status_req, STATUS_BYTECOUNT);
563aed25 863#endif
23cd1385 864
23cd1385
RB
865/*============================================================================*/
866
867/*
868 * one config, two interfaces: control, data.
869 * complications: class descriptors, and an altsetting.
870 */
871static int
872config_buf(struct usb_gadget *g, u8 *buf, u8 type, unsigned index, int is_otg)
873{
874 int len;
875 const struct usb_config_descriptor *config;
876 const struct usb_descriptor_header **function;
877 int hs = 0;
878
879 if (gadget_is_dualspeed(g)) {
880 hs = (g->speed == USB_SPEED_HIGH);
881 if (type == USB_DT_OTHER_SPEED_CONFIG)
882 hs = !hs;
883 }
884#define which_fn(t) (hs ? hs_ ## t ## _function : fs_ ## t ## _function)
885
886 if (index >= device_desc.bNumConfigurations)
887 return -EINVAL;
888
7612a43d
VK
889#ifdef CONFIG_USB_ETH_RNDIS
890 /*
891 * list the RNDIS config first, to make Microsoft's drivers
892 * happy. DOCSIS 1.0 needs this too.
893 */
894 if (device_desc.bNumConfigurations == 2 && index == 0) {
895 config = &rndis_config;
896 function = which_fn(rndis);
897 } else
898#endif
899 {
900 config = &eth_config;
901 function = which_fn(eth);
902 }
23cd1385
RB
903
904 /* for now, don't advertise srp-only devices */
905 if (!is_otg)
906 function++;
907
6142e0ae 908 len = usb_gadget_config_buf(config, buf, USB_BUFSIZ, function);
23cd1385
RB
909 if (len < 0)
910 return len;
911 ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
912 return len;
913}
914
915/*-------------------------------------------------------------------------*/
916
7612a43d 917static void eth_start(struct eth_dev *dev, gfp_t gfp_flags);
6142e0ae 918static int alloc_requests(struct eth_dev *dev, unsigned n, gfp_t gfp_flags);
23cd1385
RB
919
920static int
6142e0ae 921set_ether_config(struct eth_dev *dev, gfp_t gfp_flags)
23cd1385
RB
922{
923 int result = 0;
924 struct usb_gadget *gadget = dev->gadget;
925
2bb37884 926#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
7612a43d 927 /* status endpoint used for RNDIS and (optionally) CDC */
23cd1385 928 if (!subset_active(dev) && dev->status_ep) {
6142e0ae 929 dev->status = ep_desc(gadget, &hs_status_desc,
23cd1385
RB
930 &fs_status_desc);
931 dev->status_ep->driver_data = dev;
932
6142e0ae 933 result = usb_ep_enable(dev->status_ep, dev->status);
23cd1385 934 if (result != 0) {
7de73185 935 debug("enable %s --> %d\n",
23cd1385
RB
936 dev->status_ep->name, result);
937 goto done;
938 }
939 }
940#endif
941
942 dev->in = ep_desc(gadget, &hs_source_desc, &fs_source_desc);
943 dev->in_ep->driver_data = dev;
944
945 dev->out = ep_desc(gadget, &hs_sink_desc, &fs_sink_desc);
946 dev->out_ep->driver_data = dev;
947
6142e0ae
VK
948 /*
949 * With CDC, the host isn't allowed to use these two data
23cd1385
RB
950 * endpoints in the default altsetting for the interface.
951 * so we don't activate them yet. Reset from SET_INTERFACE.
7612a43d
VK
952 *
953 * Strictly speaking RNDIS should work the same: activation is
954 * a side effect of setting a packet filter. Deactivation is
955 * from REMOTE_NDIS_HALT_MSG, reset from REMOTE_NDIS_RESET_MSG.
23cd1385
RB
956 */
957 if (!cdc_active(dev)) {
6142e0ae 958 result = usb_ep_enable(dev->in_ep, dev->in);
23cd1385 959 if (result != 0) {
7de73185 960 debug("enable %s --> %d\n",
23cd1385
RB
961 dev->in_ep->name, result);
962 goto done;
963 }
964
6142e0ae 965 result = usb_ep_enable(dev->out_ep, dev->out);
23cd1385 966 if (result != 0) {
7de73185 967 debug("enable %s --> %d\n",
23cd1385
RB
968 dev->out_ep->name, result);
969 goto done;
970 }
971 }
972
973done:
974 if (result == 0)
6142e0ae 975 result = alloc_requests(dev, qlen(gadget), gfp_flags);
23cd1385
RB
976
977 /* on error, disable any endpoints */
978 if (result < 0) {
d5292c16 979 if (!subset_active(dev) && dev->status_ep)
6142e0ae 980 (void) usb_ep_disable(dev->status_ep);
23cd1385 981 dev->status = NULL;
6142e0ae
VK
982 (void) usb_ep_disable(dev->in_ep);
983 (void) usb_ep_disable(dev->out_ep);
23cd1385
RB
984 dev->in = NULL;
985 dev->out = NULL;
7612a43d
VK
986 } else if (!cdc_active(dev)) {
987 /*
988 * activate non-CDC configs right away
989 * this isn't strictly according to the RNDIS spec
990 */
991 eth_start(dev, GFP_ATOMIC);
23cd1385
RB
992 }
993
994 /* caller is responsible for cleanup on error */
995 return result;
996}
997
6142e0ae 998static void eth_reset_config(struct eth_dev *dev)
23cd1385
RB
999{
1000 if (dev->config == 0)
1001 return;
1002
7de73185
VK
1003 debug("%s\n", __func__);
1004
7612a43d
VK
1005 rndis_uninit(dev->rndis_config);
1006
6142e0ae
VK
1007 /*
1008 * disable endpoints, forcing (synchronous) completion of
23cd1385
RB
1009 * pending i/o. then free the requests.
1010 */
1011
1012 if (dev->in) {
6142e0ae 1013 usb_ep_disable(dev->in_ep);
23cd1385 1014 if (dev->tx_req) {
6142e0ae
VK
1015 usb_ep_free_request(dev->in_ep, dev->tx_req);
1016 dev->tx_req = NULL;
23cd1385
RB
1017 }
1018 }
1019 if (dev->out) {
6142e0ae 1020 usb_ep_disable(dev->out_ep);
23cd1385 1021 if (dev->rx_req) {
6142e0ae
VK
1022 usb_ep_free_request(dev->out_ep, dev->rx_req);
1023 dev->rx_req = NULL;
23cd1385
RB
1024 }
1025 }
6142e0ae
VK
1026 if (dev->status)
1027 usb_ep_disable(dev->status_ep);
1028
7612a43d 1029 dev->rndis = 0;
23cd1385
RB
1030 dev->cdc_filter = 0;
1031 dev->config = 0;
1032}
1033
6142e0ae
VK
1034/*
1035 * change our operational config. must agree with the code
23cd1385
RB
1036 * that returns config descriptors, and altsetting code.
1037 */
6142e0ae
VK
1038static int eth_set_config(struct eth_dev *dev, unsigned number,
1039 gfp_t gfp_flags)
23cd1385
RB
1040{
1041 int result = 0;
1042 struct usb_gadget *gadget = dev->gadget;
1043
6142e0ae 1044 eth_reset_config(dev);
23cd1385
RB
1045
1046 switch (number) {
1047 case DEV_CONFIG_VALUE:
6142e0ae 1048 result = set_ether_config(dev, gfp_flags);
23cd1385 1049 break;
7612a43d
VK
1050#ifdef CONFIG_USB_ETH_RNDIS
1051 case DEV_RNDIS_CONFIG_VALUE:
1052 dev->rndis = 1;
1053 result = set_ether_config(dev, gfp_flags);
1054 break;
1055#endif
23cd1385
RB
1056 default:
1057 result = -EINVAL;
1058 /* FALL THROUGH */
1059 case 0:
1060 break;
1061 }
1062
1063 if (result) {
1064 if (number)
6142e0ae 1065 eth_reset_config(dev);
23cd1385
RB
1066 usb_gadget_vbus_draw(dev->gadget,
1067 gadget_is_otg(dev->gadget) ? 8 : 100);
1068 } else {
1069 char *speed;
1070 unsigned power;
1071
1072 power = 2 * eth_config.bMaxPower;
1073 usb_gadget_vbus_draw(dev->gadget, power);
1074
1075 switch (gadget->speed) {
6142e0ae
VK
1076 case USB_SPEED_FULL:
1077 speed = "full"; break;
23cd1385 1078#ifdef CONFIG_USB_GADGET_DUALSPEED
6142e0ae
VK
1079 case USB_SPEED_HIGH:
1080 speed = "high"; break;
23cd1385 1081#endif
6142e0ae
VK
1082 default:
1083 speed = "?"; break;
23cd1385
RB
1084 }
1085
1086 dev->config = number;
7de73185 1087 printf("%s speed config #%d: %d mA, %s, using %s\n",
23cd1385 1088 speed, number, power, driver_desc,
7612a43d
VK
1089 rndis_active(dev)
1090 ? "RNDIS"
1091 : (cdc_active(dev)
1092 ? "CDC Ethernet"
23cd1385
RB
1093 : "CDC Ethernet Subset"));
1094 }
1095 return result;
1096}
1097
1098/*-------------------------------------------------------------------------*/
1099
2bb37884 1100#ifdef CONFIG_USB_ETH_CDC
23cd1385 1101
6142e0ae
VK
1102/*
1103 * The interrupt endpoint is used in CDC networking models (Ethernet, ATM)
23cd1385 1104 * only to notify the host about link status changes (which we support) or
7612a43d 1105 * report completion of some encapsulated command (as used in RNDIS). Since
23cd1385
RB
1106 * we want this CDC Ethernet code to be vendor-neutral, we don't use that
1107 * command mechanism; and only one status request is ever queued.
1108 */
6142e0ae 1109static void eth_status_complete(struct usb_ep *ep, struct usb_request *req)
23cd1385
RB
1110{
1111 struct usb_cdc_notification *event = req->buf;
1112 int value = req->status;
1113 struct eth_dev *dev = ep->driver_data;
1114
1115 /* issue the second notification if host reads the first */
1116 if (event->bNotificationType == USB_CDC_NOTIFY_NETWORK_CONNECTION
1117 && value == 0) {
1118 __le32 *data = req->buf + sizeof *event;
1119
1120 event->bmRequestType = 0xA1;
1121 event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE;
6142e0ae
VK
1122 event->wValue = __constant_cpu_to_le16(0);
1123 event->wIndex = __constant_cpu_to_le16(1);
1124 event->wLength = __constant_cpu_to_le16(8);
23cd1385
RB
1125
1126 /* SPEED_CHANGE data is up/down speeds in bits/sec */
6142e0ae 1127 data[0] = data[1] = cpu_to_le32(BITRATE(dev->gadget));
23cd1385
RB
1128
1129 req->length = STATUS_BYTECOUNT;
6142e0ae 1130 value = usb_ep_queue(ep, req, GFP_ATOMIC);
7de73185 1131 debug("send SPEED_CHANGE --> %d\n", value);
23cd1385
RB
1132 if (value == 0)
1133 return;
1134 } else if (value != -ECONNRESET) {
7de73185 1135 debug("event %02x --> %d\n",
23cd1385 1136 event->bNotificationType, value);
6142e0ae
VK
1137 if (event->bNotificationType ==
1138 USB_CDC_NOTIFY_SPEED_CHANGE) {
17b4f308 1139 dev->network_started = 1;
23cd1385
RB
1140 printf("USB network up!\n");
1141 }
1142 }
1143 req->context = NULL;
1144}
1145
6142e0ae 1146static void issue_start_status(struct eth_dev *dev)
23cd1385
RB
1147{
1148 struct usb_request *req = dev->stat_req;
1149 struct usb_cdc_notification *event;
1150 int value;
1151
6142e0ae
VK
1152 /*
1153 * flush old status
23cd1385
RB
1154 *
1155 * FIXME ugly idiom, maybe we'd be better with just
1156 * a "cancel the whole queue" primitive since any
1157 * unlink-one primitive has way too many error modes.
1158 * here, we "know" toggle is already clear...
1159 *
1160 * FIXME iff req->context != null just dequeue it
1161 */
6142e0ae
VK
1162 usb_ep_disable(dev->status_ep);
1163 usb_ep_enable(dev->status_ep, dev->status);
23cd1385 1164
6142e0ae
VK
1165 /*
1166 * 3.8.1 says to issue first NETWORK_CONNECTION, then
23cd1385
RB
1167 * a SPEED_CHANGE. could be useful in some configs.
1168 */
1169 event = req->buf;
1170 event->bmRequestType = 0xA1;
1171 event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION;
6142e0ae
VK
1172 event->wValue = __constant_cpu_to_le16(1); /* connected */
1173 event->wIndex = __constant_cpu_to_le16(1);
23cd1385
RB
1174 event->wLength = 0;
1175
1176 req->length = sizeof *event;
1177 req->complete = eth_status_complete;
1178 req->context = dev;
1179
6142e0ae 1180 value = usb_ep_queue(dev->status_ep, req, GFP_ATOMIC);
23cd1385 1181 if (value < 0)
7de73185 1182 debug("status buf queue --> %d\n", value);
23cd1385
RB
1183}
1184
1185#endif
1186
1187/*-------------------------------------------------------------------------*/
1188
6142e0ae 1189static void eth_setup_complete(struct usb_ep *ep, struct usb_request *req)
23cd1385
RB
1190{
1191 if (req->status || req->actual != req->length)
7de73185 1192 debug("setup complete --> %d, %d/%d\n",
23cd1385
RB
1193 req->status, req->actual, req->length);
1194}
1195
7612a43d
VK
1196#ifdef CONFIG_USB_ETH_RNDIS
1197
1198static void rndis_response_complete(struct usb_ep *ep, struct usb_request *req)
1199{
1200 if (req->status || req->actual != req->length)
1201 debug("rndis response complete --> %d, %d/%d\n",
1202 req->status, req->actual, req->length);
1203
1204 /* done sending after USB_CDC_GET_ENCAPSULATED_RESPONSE */
1205}
1206
1207static void rndis_command_complete(struct usb_ep *ep, struct usb_request *req)
1208{
1209 struct eth_dev *dev = ep->driver_data;
1210 int status;
1211
1212 /* received RNDIS command from USB_CDC_SEND_ENCAPSULATED_COMMAND */
1213 status = rndis_msg_parser(dev->rndis_config, (u8 *) req->buf);
1214 if (status < 0)
9b643e31 1215 pr_err("%s: rndis parse error %d", __func__, status);
7612a43d
VK
1216}
1217
1218#endif /* RNDIS */
1219
23cd1385
RB
1220/*
1221 * The setup() callback implements all the ep0 functionality that's not
1222 * handled lower down. CDC has a number of less-common features:
1223 *
1224 * - two interfaces: control, and ethernet data
1225 * - Ethernet data interface has two altsettings: default, and active
1226 * - class-specific descriptors for the control interface
1227 * - class-specific control requests
1228 */
1229static int
6142e0ae 1230eth_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
23cd1385 1231{
6142e0ae 1232 struct eth_dev *dev = get_gadget_data(gadget);
23cd1385
RB
1233 struct usb_request *req = dev->req;
1234 int value = -EOPNOTSUPP;
1235 u16 wIndex = le16_to_cpu(ctrl->wIndex);
1236 u16 wValue = le16_to_cpu(ctrl->wValue);
1237 u16 wLength = le16_to_cpu(ctrl->wLength);
1238
6142e0ae
VK
1239 /*
1240 * descriptors just go into the pre-allocated ep0 buffer,
23cd1385
RB
1241 * while config change events may enable network traffic.
1242 */
1243
7de73185 1244 debug("%s\n", __func__);
23cd1385
RB
1245
1246 req->complete = eth_setup_complete;
1247 switch (ctrl->bRequest) {
1248
1249 case USB_REQ_GET_DESCRIPTOR:
1250 if (ctrl->bRequestType != USB_DIR_IN)
1251 break;
1252 switch (wValue >> 8) {
1253
1254 case USB_DT_DEVICE:
04afd5b5 1255 device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
6142e0ae
VK
1256 value = min(wLength, (u16) sizeof device_desc);
1257 memcpy(req->buf, &device_desc, value);
23cd1385
RB
1258 break;
1259 case USB_DT_DEVICE_QUALIFIER:
1260 if (!gadget_is_dualspeed(gadget))
1261 break;
6142e0ae
VK
1262 value = min(wLength, (u16) sizeof dev_qualifier);
1263 memcpy(req->buf, &dev_qualifier, value);
23cd1385
RB
1264 break;
1265
1266 case USB_DT_OTHER_SPEED_CONFIG:
1267 if (!gadget_is_dualspeed(gadget))
1268 break;
1269 /* FALLTHROUGH */
1270 case USB_DT_CONFIG:
1271 value = config_buf(gadget, req->buf,
1272 wValue >> 8,
1273 wValue & 0xff,
1274 gadget_is_otg(gadget));
1275 if (value >= 0)
6142e0ae 1276 value = min(wLength, (u16) value);
23cd1385
RB
1277 break;
1278
1279 case USB_DT_STRING:
6142e0ae 1280 value = usb_gadget_get_string(&stringtab,
23cd1385
RB
1281 wValue & 0xff, req->buf);
1282
1283 if (value >= 0)
6142e0ae 1284 value = min(wLength, (u16) value);
23cd1385
RB
1285
1286 break;
1287 }
1288 break;
1289
1290 case USB_REQ_SET_CONFIGURATION:
1291 if (ctrl->bRequestType != 0)
1292 break;
1293 if (gadget->a_hnp_support)
7de73185 1294 debug("HNP available\n");
23cd1385 1295 else if (gadget->a_alt_hnp_support)
7de73185 1296 debug("HNP needs a different root port\n");
6142e0ae 1297 value = eth_set_config(dev, wValue, GFP_ATOMIC);
23cd1385
RB
1298 break;
1299 case USB_REQ_GET_CONFIGURATION:
1300 if (ctrl->bRequestType != USB_DIR_IN)
1301 break;
1302 *(u8 *)req->buf = dev->config;
6142e0ae 1303 value = min(wLength, (u16) 1);
23cd1385
RB
1304 break;
1305
1306 case USB_REQ_SET_INTERFACE:
1307 if (ctrl->bRequestType != USB_RECIP_INTERFACE
1308 || !dev->config
1309 || wIndex > 1)
1310 break;
1311 if (!cdc_active(dev) && wIndex != 0)
1312 break;
1313
2bb37884 1314#ifdef CONFIG_USB_ETH_CDC
23cd1385
RB
1315 switch (wIndex) {
1316 case 0: /* control/master intf */
1317 if (wValue != 0)
1318 break;
1319 if (dev->status) {
6142e0ae
VK
1320 usb_ep_disable(dev->status_ep);
1321 usb_ep_enable(dev->status_ep, dev->status);
23cd1385 1322 }
7612a43d 1323
23cd1385
RB
1324 value = 0;
1325 break;
1326 case 1: /* data intf */
1327 if (wValue > 1)
1328 break;
6142e0ae
VK
1329 usb_ep_disable(dev->in_ep);
1330 usb_ep_disable(dev->out_ep);
23cd1385 1331
6142e0ae
VK
1332 /*
1333 * CDC requires the data transfers not be done from
23cd1385
RB
1334 * the default interface setting ... also, setting
1335 * the non-default interface resets filters etc.
1336 */
1337 if (wValue == 1) {
6142e0ae 1338 if (!cdc_active(dev))
23cd1385 1339 break;
6142e0ae
VK
1340 usb_ep_enable(dev->in_ep, dev->in);
1341 usb_ep_enable(dev->out_ep, dev->out);
23cd1385
RB
1342 dev->cdc_filter = DEFAULT_FILTER;
1343 if (dev->status)
6142e0ae 1344 issue_start_status(dev);
7612a43d 1345 eth_start(dev, GFP_ATOMIC);
23cd1385 1346 }
23cd1385
RB
1347 value = 0;
1348 break;
1349 }
1350#else
6142e0ae
VK
1351 /*
1352 * FIXME this is wrong, as is the assumption that
23cd1385
RB
1353 * all non-PXA hardware talks real CDC ...
1354 */
7de73185 1355 debug("set_interface ignored!\n");
2bb37884 1356#endif /* CONFIG_USB_ETH_CDC */
23cd1385
RB
1357 break;
1358 case USB_REQ_GET_INTERFACE:
1359 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE)
1360 || !dev->config
1361 || wIndex > 1)
1362 break;
7612a43d 1363 if (!(cdc_active(dev) || rndis_active(dev)) && wIndex != 0)
23cd1385
RB
1364 break;
1365
1366 /* for CDC, iff carrier is on, data interface is active. */
7612a43d 1367 if (rndis_active(dev) || wIndex != 1)
23cd1385
RB
1368 *(u8 *)req->buf = 0;
1369 else {
1370 /* *(u8 *)req->buf = netif_carrier_ok (dev->net) ? 1 : 0; */
1371 /* carrier always ok ...*/
1372 *(u8 *)req->buf = 1 ;
1373 }
6142e0ae 1374 value = min(wLength, (u16) 1);
23cd1385
RB
1375 break;
1376
2bb37884 1377#ifdef CONFIG_USB_ETH_CDC
23cd1385 1378 case USB_CDC_SET_ETHERNET_PACKET_FILTER:
6142e0ae
VK
1379 /*
1380 * see 6.2.30: no data, wIndex = interface,
23cd1385
RB
1381 * wValue = packet filter bitmap
1382 */
1383 if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1384 || !cdc_active(dev)
1385 || wLength != 0
1386 || wIndex > 1)
1387 break;
7de73185 1388 debug("packet filter %02x\n", wValue);
23cd1385
RB
1389 dev->cdc_filter = wValue;
1390 value = 0;
1391 break;
1392
6142e0ae
VK
1393 /*
1394 * and potentially:
23cd1385
RB
1395 * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS:
1396 * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER:
1397 * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER:
1398 * case USB_CDC_GET_ETHERNET_STATISTIC:
1399 */
1400
2bb37884 1401#endif /* CONFIG_USB_ETH_CDC */
23cd1385 1402
7612a43d
VK
1403#ifdef CONFIG_USB_ETH_RNDIS
1404 /*
1405 * RNDIS uses the CDC command encapsulation mechanism to implement
1406 * an RPC scheme, with much getting/setting of attributes by OID.
1407 */
1408 case USB_CDC_SEND_ENCAPSULATED_COMMAND:
1409 if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1410 || !rndis_active(dev)
1411 || wLength > USB_BUFSIZ
1412 || wValue
1413 || rndis_control_intf.bInterfaceNumber
1414 != wIndex)
1415 break;
1416 /* read the request, then process it */
1417 value = wLength;
1418 req->complete = rndis_command_complete;
1419 /* later, rndis_control_ack () sends a notification */
1420 break;
1421
1422 case USB_CDC_GET_ENCAPSULATED_RESPONSE:
1423 if ((USB_DIR_IN|USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1424 == ctrl->bRequestType
1425 && rndis_active(dev)
1426 /* && wLength >= 0x0400 */
1427 && !wValue
1428 && rndis_control_intf.bInterfaceNumber
1429 == wIndex) {
1430 u8 *buf;
1431 u32 n;
1432
1433 /* return the result */
1434 buf = rndis_get_next_response(dev->rndis_config, &n);
1435 if (buf) {
1436 memcpy(req->buf, buf, n);
1437 req->complete = rndis_response_complete;
1438 rndis_free_response(dev->rndis_config, buf);
1439 value = n;
1440 }
1441 /* else stalls ... spec says to avoid that */
1442 }
1443 break;
1444#endif /* RNDIS */
1445
23cd1385 1446 default:
7de73185 1447 debug("unknown control req%02x.%02x v%04x i%04x l%d\n",
23cd1385
RB
1448 ctrl->bRequestType, ctrl->bRequest,
1449 wValue, wIndex, wLength);
1450 }
1451
1452 /* respond with data transfer before status phase? */
1453 if (value >= 0) {
7de73185 1454 debug("respond with data transfer before status phase\n");
23cd1385
RB
1455 req->length = value;
1456 req->zero = value < wLength
1457 && (value % gadget->ep0->maxpacket) == 0;
6142e0ae 1458 value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
23cd1385 1459 if (value < 0) {
7de73185 1460 debug("ep_queue --> %d\n", value);
23cd1385 1461 req->status = 0;
6142e0ae 1462 eth_setup_complete(gadget->ep0, req);
23cd1385
RB
1463 }
1464 }
1465
1466 /* host either stalls (value < 0) or reports success */
1467 return value;
1468}
1469
23cd1385
RB
1470/*-------------------------------------------------------------------------*/
1471
6142e0ae 1472static void rx_complete(struct usb_ep *ep, struct usb_request *req);
23cd1385 1473
6142e0ae 1474static int rx_submit(struct eth_dev *dev, struct usb_request *req,
23cd1385
RB
1475 gfp_t gfp_flags)
1476{
1477 int retval = -ENOMEM;
1478 size_t size;
1479
6142e0ae
VK
1480 /*
1481 * Padding up to RX_EXTRA handles minor disagreements with host.
23cd1385
RB
1482 * Normally we use the USB "terminate on short read" convention;
1483 * so allow up to (N*maxpacket), since that memory is normally
1484 * already allocated. Some hardware doesn't deal well with short
1485 * reads (e.g. DMA must be N*maxpacket), so for now don't trim a
1486 * byte off the end (to force hardware errors on overflow).
7612a43d
VK
1487 *
1488 * RNDIS uses internal framing, and explicitly allows senders to
1489 * pad to end-of-packet. That's potentially nice for speed,
1490 * but means receivers can't recover synch on their own.
23cd1385
RB
1491 */
1492
7de73185 1493 debug("%s\n", __func__);
71fc5f91
TK
1494 if (!req)
1495 return -EINVAL;
23cd1385
RB
1496
1497 size = (ETHER_HDR_SIZE + dev->mtu + RX_EXTRA);
1498 size += dev->out_ep->maxpacket - 1;
7612a43d
VK
1499 if (rndis_active(dev))
1500 size += sizeof(struct rndis_packet_msg_type);
23cd1385
RB
1501 size -= size % dev->out_ep->maxpacket;
1502
6142e0ae
VK
1503 /*
1504 * Some platforms perform better when IP packets are aligned,
7612a43d
VK
1505 * but on at least one, checksumming fails otherwise. Note:
1506 * RNDIS headers involve variable numbers of LE32 values.
23cd1385
RB
1507 */
1508
1fd92db8 1509 req->buf = (u8 *)net_rx_packets[0];
23cd1385
RB
1510 req->length = size;
1511 req->complete = rx_complete;
1512
6142e0ae 1513 retval = usb_ep_queue(dev->out_ep, req, gfp_flags);
23cd1385 1514
6142e0ae 1515 if (retval)
9b643e31 1516 pr_err("rx submit --> %d", retval);
6142e0ae 1517
23cd1385
RB
1518 return retval;
1519}
1520
6142e0ae 1521static void rx_complete(struct usb_ep *ep, struct usb_request *req)
23cd1385
RB
1522{
1523 struct eth_dev *dev = ep->driver_data;
1524
7de73185 1525 debug("%s: status %d\n", __func__, req->status);
c85d70ef
VK
1526 switch (req->status) {
1527 /* normal completion */
1528 case 0:
7612a43d
VK
1529 if (rndis_active(dev)) {
1530 /* we know MaxPacketsPerTransfer == 1 here */
1531 int length = rndis_rm_hdr(req->buf, req->actual);
1532 if (length < 0)
1533 goto length_err;
1534 req->length -= length;
1535 req->actual -= length;
1536 }
dda52510 1537 if (req->actual < ETH_HLEN || PKTSIZE_ALIGN < req->actual) {
7612a43d
VK
1538length_err:
1539 dev->stats.rx_errors++;
1540 dev->stats.rx_length_errors++;
1541 debug("rx length %d\n", req->length);
1542 break;
1543 }
1544
c85d70ef
VK
1545 dev->stats.rx_packets++;
1546 dev->stats.rx_bytes += req->length;
1547 break;
1548
1549 /* software-driven interface shutdown */
1550 case -ECONNRESET: /* unlink */
1551 case -ESHUTDOWN: /* disconnect etc */
1552 /* for hardware automagic (such as pxa) */
1553 case -ECONNABORTED: /* endpoint reset */
1554 break;
1555
1556 /* data overrun */
1557 case -EOVERFLOW:
1558 dev->stats.rx_over_errors++;
1559 /* FALLTHROUGH */
1560 default:
1561 dev->stats.rx_errors++;
1562 break;
1563 }
23cd1385 1564
6142e0ae 1565 packet_received = 1;
23cd1385
RB
1566}
1567
6142e0ae 1568static int alloc_requests(struct eth_dev *dev, unsigned n, gfp_t gfp_flags)
23cd1385
RB
1569{
1570
6142e0ae 1571 dev->tx_req = usb_ep_alloc_request(dev->in_ep, 0);
23cd1385
RB
1572
1573 if (!dev->tx_req)
ac5d32d1 1574 goto fail1;
23cd1385 1575
6142e0ae 1576 dev->rx_req = usb_ep_alloc_request(dev->out_ep, 0);
23cd1385
RB
1577
1578 if (!dev->rx_req)
ac5d32d1 1579 goto fail2;
23cd1385
RB
1580
1581 return 0;
1582
ac5d32d1
VK
1583fail2:
1584 usb_ep_free_request(dev->in_ep, dev->tx_req);
1585fail1:
9b643e31 1586 pr_err("can't alloc requests");
23cd1385
RB
1587 return -1;
1588}
1589
6142e0ae 1590static void tx_complete(struct usb_ep *ep, struct usb_request *req)
23cd1385 1591{
c85d70ef
VK
1592 struct eth_dev *dev = ep->driver_data;
1593
6142e0ae 1594 debug("%s: status %s\n", __func__, (req->status) ? "failed" : "ok");
c85d70ef
VK
1595 switch (req->status) {
1596 default:
1597 dev->stats.tx_errors++;
1598 debug("tx err %d\n", req->status);
1599 /* FALLTHROUGH */
1600 case -ECONNRESET: /* unlink */
1601 case -ESHUTDOWN: /* disconnect etc */
1602 break;
1603 case 0:
1604 dev->stats.tx_bytes += req->length;
1605 }
1606 dev->stats.tx_packets++;
1607
6142e0ae 1608 packet_sent = 1;
23cd1385
RB
1609}
1610
6142e0ae 1611static inline int eth_is_promisc(struct eth_dev *dev)
23cd1385
RB
1612{
1613 /* no filters for the CDC subset; always promisc */
6142e0ae 1614 if (subset_active(dev))
23cd1385
RB
1615 return 1;
1616 return dev->cdc_filter & USB_CDC_PACKET_TYPE_PROMISCUOUS;
1617}
1618
1619#if 0
1620static int eth_start_xmit (struct sk_buff *skb, struct net_device *net)
1621{
1622 struct eth_dev *dev = netdev_priv(net);
1623 int length = skb->len;
1624 int retval;
1625 struct usb_request *req = NULL;
1626 unsigned long flags;
1627
1628 /* apply outgoing CDC or RNDIS filters */
1629 if (!eth_is_promisc (dev)) {
1630 u8 *dest = skb->data;
1631
0adb5b76 1632 if (is_multicast_ethaddr(dest)) {
23cd1385
RB
1633 u16 type;
1634
1635 /* ignores USB_CDC_PACKET_TYPE_MULTICAST and host
1636 * SET_ETHERNET_MULTICAST_FILTERS requests
1637 */
0adb5b76 1638 if (is_broadcast_ethaddr(dest))
23cd1385
RB
1639 type = USB_CDC_PACKET_TYPE_BROADCAST;
1640 else
1641 type = USB_CDC_PACKET_TYPE_ALL_MULTICAST;
1642 if (!(dev->cdc_filter & type)) {
1643 dev_kfree_skb_any (skb);
1644 return 0;
1645 }
1646 }
1647 /* ignores USB_CDC_PACKET_TYPE_DIRECTED */
1648 }
1649
1650 spin_lock_irqsave(&dev->req_lock, flags);
1651 /*
1652 * this freelist can be empty if an interrupt triggered disconnect()
1653 * and reconfigured the gadget (shutting down this queue) after the
1654 * network stack decided to xmit but before we got the spinlock.
1655 */
1656 if (list_empty(&dev->tx_reqs)) {
1657 spin_unlock_irqrestore(&dev->req_lock, flags);
1658 return 1;
1659 }
1660
1661 req = container_of (dev->tx_reqs.next, struct usb_request, list);
1662 list_del (&req->list);
1663
1664 /* temporarily stop TX queue when the freelist empties */
1665 if (list_empty (&dev->tx_reqs))
1666 netif_stop_queue (net);
1667 spin_unlock_irqrestore(&dev->req_lock, flags);
1668
1669 /* no buffer copies needed, unless the network stack did it
1670 * or the hardware can't use skb buffers.
1671 * or there's not enough space for any RNDIS headers we need
1672 */
1673 if (rndis_active(dev)) {
1674 struct sk_buff *skb_rndis;
1675
1676 skb_rndis = skb_realloc_headroom (skb,
1677 sizeof (struct rndis_packet_msg_type));
1678 if (!skb_rndis)
1679 goto drop;
1680
1681 dev_kfree_skb_any (skb);
1682 skb = skb_rndis;
1683 rndis_add_hdr (skb);
1684 length = skb->len;
1685 }
1686 req->buf = skb->data;
1687 req->context = skb;
1688 req->complete = tx_complete;
1689
1690 /* use zlp framing on tx for strict CDC-Ether conformance,
1691 * though any robust network rx path ignores extra padding.
1692 * and some hardware doesn't like to write zlps.
1693 */
1694 req->zero = 1;
1695 if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0)
1696 length++;
1697
1698 req->length = length;
1699
1700 /* throttle highspeed IRQ rate back slightly */
1701 if (gadget_is_dualspeed(dev->gadget))
1702 req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH)
1703 ? ((atomic_read(&dev->tx_qlen) % qmult) != 0)
1704 : 0;
1705
1706 retval = usb_ep_queue (dev->in_ep, req, GFP_ATOMIC);
1707 switch (retval) {
1708 default:
1709 DEBUG (dev, "tx queue err %d\n", retval);
1710 break;
1711 case 0:
1712 net->trans_start = jiffies;
1713 atomic_inc (&dev->tx_qlen);
1714 }
1715
1716 if (retval) {
1717drop:
1718 dev->stats.tx_dropped++;
1719 dev_kfree_skb_any (skb);
1720 spin_lock_irqsave(&dev->req_lock, flags);
1721 if (list_empty (&dev->tx_reqs))
1722 netif_start_queue (net);
1723 list_add (&req->list, &dev->tx_reqs);
1724 spin_unlock_irqrestore(&dev->req_lock, flags);
1725 }
1726 return 0;
1727}
1728
1729/*-------------------------------------------------------------------------*/
1730#endif
1731
6142e0ae 1732static void eth_unbind(struct usb_gadget *gadget)
23cd1385 1733{
7612a43d 1734 struct eth_dev *dev = get_gadget_data(gadget);
23cd1385 1735
7de73185 1736 debug("%s...\n", __func__);
7612a43d
VK
1737 rndis_deregister(dev->rndis_config);
1738 rndis_exit();
23cd1385 1739
0129e327
VK
1740 /* we've already been disconnected ... no i/o is active */
1741 if (dev->req) {
6142e0ae 1742 usb_ep_free_request(gadget->ep0, dev->req);
0129e327
VK
1743 dev->req = NULL;
1744 }
23cd1385 1745 if (dev->stat_req) {
6142e0ae 1746 usb_ep_free_request(dev->status_ep, dev->stat_req);
23cd1385
RB
1747 dev->stat_req = NULL;
1748 }
1749
1750 if (dev->tx_req) {
6142e0ae
VK
1751 usb_ep_free_request(dev->in_ep, dev->tx_req);
1752 dev->tx_req = NULL;
23cd1385
RB
1753 }
1754
1755 if (dev->rx_req) {
6142e0ae
VK
1756 usb_ep_free_request(dev->out_ep, dev->rx_req);
1757 dev->rx_req = NULL;
23cd1385
RB
1758 }
1759
1760/* unregister_netdev (dev->net);*/
1761/* free_netdev(dev->net);*/
1762
988ee3e3 1763 dev->gadget = NULL;
6142e0ae 1764 set_gadget_data(gadget, NULL);
23cd1385
RB
1765}
1766
6142e0ae 1767static void eth_disconnect(struct usb_gadget *gadget)
23cd1385 1768{
6142e0ae 1769 eth_reset_config(get_gadget_data(gadget));
7612a43d 1770 /* FIXME RNDIS should enter RNDIS_UNINITIALIZED */
23cd1385
RB
1771}
1772
6142e0ae 1773static void eth_suspend(struct usb_gadget *gadget)
23cd1385
RB
1774{
1775 /* Not used */
1776}
1777
6142e0ae 1778static void eth_resume(struct usb_gadget *gadget)
23cd1385
RB
1779{
1780 /* Not used */
1781}
1782
1783/*-------------------------------------------------------------------------*/
1784
7612a43d
VK
1785#ifdef CONFIG_USB_ETH_RNDIS
1786
1787/*
1788 * The interrupt endpoint is used in RNDIS to notify the host when messages
1789 * other than data packets are available ... notably the REMOTE_NDIS_*_CMPLT
1790 * messages, but also REMOTE_NDIS_INDICATE_STATUS_MSG and potentially even
1791 * REMOTE_NDIS_KEEPALIVE_MSG.
1792 *
1793 * The RNDIS control queue is processed by GET_ENCAPSULATED_RESPONSE, and
1794 * normally just one notification will be queued.
1795 */
1796
1797static void rndis_control_ack_complete(struct usb_ep *ep,
1798 struct usb_request *req)
1799{
1800 struct eth_dev *dev = ep->driver_data;
1801
1802 debug("%s...\n", __func__);
1803 if (req->status || req->actual != req->length)
1804 debug("rndis control ack complete --> %d, %d/%d\n",
1805 req->status, req->actual, req->length);
1806
17b4f308 1807 if (!dev->network_started) {
7612a43d
VK
1808 if (rndis_get_state(dev->rndis_config)
1809 == RNDIS_DATA_INITIALIZED) {
17b4f308 1810 dev->network_started = 1;
7612a43d
VK
1811 printf("USB RNDIS network up!\n");
1812 }
1813 }
1814
1815 req->context = NULL;
1816
1817 if (req != dev->stat_req)
1818 usb_ep_free_request(ep, req);
1819}
1820
1821static char rndis_resp_buf[8] __attribute__((aligned(sizeof(__le32))));
1822
d4a37553 1823static int rndis_control_ack(struct udevice *net)
7612a43d 1824{
0fd3d911
SG
1825 struct ether_priv *priv;
1826 struct eth_dev *dev;
1827 int length;
1828 struct usb_request *resp;
1829
0fd3d911 1830 priv = dev_get_priv(net);
0fd3d911
SG
1831 dev = &priv->ethdev;
1832 resp = dev->stat_req;
7612a43d
VK
1833
1834 /* in case RNDIS calls this after disconnect */
1835 if (!dev->status) {
1836 debug("status ENODEV\n");
1837 return -ENODEV;
1838 }
1839
1840 /* in case queue length > 1 */
1841 if (resp->context) {
1842 resp = usb_ep_alloc_request(dev->status_ep, GFP_ATOMIC);
1843 if (!resp)
1844 return -ENOMEM;
1845 resp->buf = rndis_resp_buf;
1846 }
1847
1848 /*
1849 * Send RNDIS RESPONSE_AVAILABLE notification;
1850 * USB_CDC_NOTIFY_RESPONSE_AVAILABLE should work too
1851 */
1852 resp->length = 8;
1853 resp->complete = rndis_control_ack_complete;
1854 resp->context = dev;
1855
1856 *((__le32 *) resp->buf) = __constant_cpu_to_le32(1);
1857 *((__le32 *) (resp->buf + 4)) = __constant_cpu_to_le32(0);
1858
1859 length = usb_ep_queue(dev->status_ep, resp, GFP_ATOMIC);
1860 if (length < 0) {
1861 resp->status = 0;
1862 rndis_control_ack_complete(dev->status_ep, resp);
1863 }
1864
1865 return 0;
1866}
1867
1868#else
1869
1870#define rndis_control_ack NULL
1871
1872#endif /* RNDIS */
1873
1874static void eth_start(struct eth_dev *dev, gfp_t gfp_flags)
1875{
1876 if (rndis_active(dev)) {
1877 rndis_set_param_medium(dev->rndis_config,
1878 NDIS_MEDIUM_802_3,
1879 BITRATE(dev->gadget)/100);
1880 rndis_signal_connect(dev->rndis_config);
1881 }
1882}
1883
bfa352d1 1884static int eth_stop(struct udevice *udev)
7612a43d 1885{
bfa352d1
MV
1886 struct ether_priv *priv = dev_get_priv(udev);
1887 struct eth_dev *dev = &priv->ethdev;
e4ae6660
VK
1888#ifdef RNDIS_COMPLETE_SIGNAL_DISCONNECT
1889 unsigned long ts;
1890 unsigned long timeout = CONFIG_SYS_HZ; /* 1 sec to stop RNDIS */
1891#endif
1892
7612a43d
VK
1893 if (rndis_active(dev)) {
1894 rndis_set_param_medium(dev->rndis_config, NDIS_MEDIUM_802_3, 0);
1895 rndis_signal_disconnect(dev->rndis_config);
1896
e4ae6660
VK
1897#ifdef RNDIS_COMPLETE_SIGNAL_DISCONNECT
1898 /* Wait until host receives OID_GEN_MEDIA_CONNECT_STATUS */
1899 ts = get_timer(0);
1900 while (get_timer(ts) < timeout)
bfa352d1 1901 dm_usb_gadget_handle_interrupts(udev->parent);
e4ae6660
VK
1902#endif
1903
7612a43d
VK
1904 rndis_uninit(dev->rndis_config);
1905 dev->rndis = 0;
1906 }
1907
1908 return 0;
1909}
1910
1911/*-------------------------------------------------------------------------*/
1912
23cd1385
RB
1913static int is_eth_addr_valid(char *str)
1914{
1915 if (strlen(str) == 17) {
1916 int i;
1917 char *p, *q;
1918 uchar ea[6];
1919
1920 /* see if it looks like an ethernet address */
1921
1922 p = str;
1923
1924 for (i = 0; i < 6; i++) {
1925 char term = (i == 5 ? '\0' : ':');
1926
1927 ea[i] = simple_strtol(p, &q, 16);
1928
1929 if ((q - p) != 2 || *q++ != term)
1930 break;
1931
1932 p = q;
1933 }
1934
57a87a25 1935 /* Now check the contents. */
0adb5b76 1936 return is_valid_ethaddr(ea);
23cd1385
RB
1937 }
1938 return 0;
1939}
1940
6142e0ae 1941static u8 nibble(unsigned char c)
23cd1385 1942{
6142e0ae 1943 if (likely(isdigit(c)))
23cd1385 1944 return c - '0';
6142e0ae
VK
1945 c = toupper(c);
1946 if (likely(isxdigit(c)))
23cd1385
RB
1947 return 10 + c - 'A';
1948 return 0;
1949}
1950
1951static int get_ether_addr(const char *str, u8 *dev_addr)
1952{
1953 if (str) {
1954 unsigned i;
1955
1956 for (i = 0; i < 6; i++) {
1957 unsigned char num;
1958
6142e0ae 1959 if ((*str == '.') || (*str == ':'))
23cd1385
RB
1960 str++;
1961 num = nibble(*str++) << 4;
1962 num |= (nibble(*str++));
6142e0ae 1963 dev_addr[i] = num;
23cd1385 1964 }
0adb5b76 1965 if (is_valid_ethaddr(dev_addr))
23cd1385
RB
1966 return 0;
1967 }
1968 return 1;
1969}
1970
1971static int eth_bind(struct usb_gadget *gadget)
1972{
5cb3b9d7 1973 struct eth_dev *dev = &l_priv->ethdev;
7612a43d 1974 u8 cdc = 1, zlp = 1, rndis = 1;
23cd1385 1975 struct usb_ep *in_ep, *out_ep, *status_ep = NULL;
7612a43d 1976 int status = -ENOMEM;
23cd1385 1977 int gcnum;
6142e0ae 1978 u8 tmp[7];
c69cda25 1979 struct eth_pdata *pdata = dev_get_plat(l_priv->netdev);
23cd1385
RB
1980
1981 /* these flags are only ever cleared; compiler take note */
2bb37884 1982#ifndef CONFIG_USB_ETH_CDC
23cd1385 1983 cdc = 0;
7612a43d
VK
1984#endif
1985#ifndef CONFIG_USB_ETH_RNDIS
1986 rndis = 0;
23cd1385 1987#endif
6142e0ae
VK
1988 /*
1989 * Because most host side USB stacks handle CDC Ethernet, that
23cd1385
RB
1990 * standard protocol is _strongly_ preferred for interop purposes.
1991 * (By everyone except Microsoft.)
1992 */
a457ebd7 1993 if (gadget_is_musbhdrc(gadget)) {
23cd1385
RB
1994 /* reduce tx dma overhead by avoiding special cases */
1995 zlp = 0;
1996 } else if (gadget_is_sh(gadget)) {
1997 /* sh doesn't support multiple interfaces or configs */
1998 cdc = 0;
7612a43d 1999 rndis = 0;
23cd1385
RB
2000 }
2001
6142e0ae 2002 gcnum = usb_gadget_controller_number(gadget);
23cd1385 2003 if (gcnum >= 0)
6142e0ae 2004 device_desc.bcdDevice = cpu_to_le16(0x0300 + gcnum);
23cd1385 2005 else {
6142e0ae
VK
2006 /*
2007 * can't assume CDC works. don't want to default to
23cd1385
RB
2008 * anything less functional on CDC-capable hardware,
2009 * so we fail in this case.
2010 */
9b643e31 2011 pr_err("controller '%s' not recognized",
23cd1385
RB
2012 gadget->name);
2013 return -ENODEV;
2014 }
2015
6142e0ae 2016 /*
7612a43d
VK
2017 * If there's an RNDIS configuration, that's what Windows wants to
2018 * be using ... so use these product IDs here and in the "linux.inf"
2019 * needed to install MSFT drivers. Current Linux kernels will use
2020 * the second configuration if it's CDC Ethernet, and need some help
2021 * to choose the right configuration otherwise.
23cd1385 2022 */
7612a43d 2023 if (rndis) {
10ac57fd 2024#if defined(CONFIG_USB_GADGET_VENDOR_NUM) && defined(CONFIG_USB_GADGET_PRODUCT_NUM)
23cd1385 2025 device_desc.idVendor =
10ac57fd 2026 __constant_cpu_to_le16(CONFIG_USB_GADGET_VENDOR_NUM);
23cd1385 2027 device_desc.idProduct =
10ac57fd 2028 __constant_cpu_to_le16(CONFIG_USB_GADGET_PRODUCT_NUM);
7612a43d
VK
2029#else
2030 device_desc.idVendor =
2031 __constant_cpu_to_le16(RNDIS_VENDOR_NUM);
2032 device_desc.idProduct =
2033 __constant_cpu_to_le16(RNDIS_PRODUCT_NUM);
2034#endif
2035 sprintf(product_desc, "RNDIS/%s", driver_desc);
23cd1385 2036
7612a43d
VK
2037 /*
2038 * CDC subset ... recognized by Linux since 2.4.10, but Windows
2039 * drivers aren't widely available. (That may be improved by
2040 * supporting one submode of the "SAFE" variant of MDLM.)
2041 */
2042 } else {
10ac57fd
MR
2043#if defined(CONFIG_USB_GADGET_VENDOR_NUM) && defined(CONFIG_USB_GADGET_PRODUCT_NUM)
2044 device_desc.idVendor = cpu_to_le16(CONFIG_USB_GADGET_VENDOR_NUM);
2045 device_desc.idProduct = cpu_to_le16(CONFIG_USB_GADGET_PRODUCT_NUM);
7612a43d
VK
2046#else
2047 if (!cdc) {
2048 device_desc.idVendor =
2049 __constant_cpu_to_le16(SIMPLE_VENDOR_NUM);
2050 device_desc.idProduct =
2051 __constant_cpu_to_le16(SIMPLE_PRODUCT_NUM);
2052 }
23cd1385 2053#endif
7612a43d
VK
2054 }
2055 /* support optional vendor/distro customization */
23cd1385
RB
2056 if (bcdDevice)
2057 device_desc.bcdDevice = cpu_to_le16(bcdDevice);
2058 if (iManufacturer)
6142e0ae 2059 strlcpy(manufacturer, iManufacturer, sizeof manufacturer);
23cd1385 2060 if (iProduct)
6142e0ae 2061 strlcpy(product_desc, iProduct, sizeof product_desc);
23cd1385
RB
2062 if (iSerialNumber) {
2063 device_desc.iSerialNumber = STRING_SERIALNUMBER,
2e12abe6 2064 strlcpy(serial_number, iSerialNumber, sizeof serial_number);
23cd1385
RB
2065 }
2066
2067 /* all we really need is bulk IN/OUT */
6142e0ae
VK
2068 usb_ep_autoconfig_reset(gadget);
2069 in_ep = usb_ep_autoconfig(gadget, &fs_source_desc);
23cd1385
RB
2070 if (!in_ep) {
2071autoconf_fail:
9b643e31 2072 pr_err("can't autoconfigure on %s\n",
23cd1385
RB
2073 gadget->name);
2074 return -ENODEV;
2075 }
2076 in_ep->driver_data = in_ep; /* claim */
2077
6142e0ae 2078 out_ep = usb_ep_autoconfig(gadget, &fs_sink_desc);
23cd1385
RB
2079 if (!out_ep)
2080 goto autoconf_fail;
2081 out_ep->driver_data = out_ep; /* claim */
2082
2bb37884 2083#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
6142e0ae
VK
2084 /*
2085 * CDC Ethernet control interface doesn't require a status endpoint.
23cd1385
RB
2086 * Since some hosts expect one, try to allocate one anyway.
2087 */
7612a43d 2088 if (cdc || rndis) {
6142e0ae 2089 status_ep = usb_ep_autoconfig(gadget, &fs_status_desc);
23cd1385
RB
2090 if (status_ep) {
2091 status_ep->driver_data = status_ep; /* claim */
7612a43d 2092 } else if (rndis) {
9b643e31 2093 pr_err("can't run RNDIS on %s", gadget->name);
7612a43d 2094 return -ENODEV;
2bb37884 2095#ifdef CONFIG_USB_ETH_CDC
23cd1385
RB
2096 } else if (cdc) {
2097 control_intf.bNumEndpoints = 0;
2098 /* FIXME remove endpoint from descriptor list */
7612a43d 2099#endif
23cd1385
RB
2100 }
2101 }
2102#endif
2103
2104 /* one config: cdc, else minimal subset */
2105 if (!cdc) {
2106 eth_config.bNumInterfaces = 1;
2107 eth_config.iConfiguration = STRING_SUBSET;
2108
6142e0ae
VK
2109 /*
2110 * use functions to set these up, in case we're built to work
23cd1385
RB
2111 * with multiple controllers and must override CDC Ethernet.
2112 */
2113 fs_subset_descriptors();
2114 hs_subset_descriptors();
2115 }
2116
6142e0ae 2117 usb_gadget_set_selfpowered(gadget);
23cd1385 2118
7612a43d
VK
2119 /* For now RNDIS is always a second config */
2120 if (rndis)
2121 device_desc.bNumConfigurations = 2;
2122
23cd1385 2123 if (gadget_is_dualspeed(gadget)) {
7612a43d
VK
2124 if (rndis)
2125 dev_qualifier.bNumConfigurations = 2;
2126 else if (!cdc)
23cd1385
RB
2127 dev_qualifier.bDeviceClass = USB_CLASS_VENDOR_SPEC;
2128
2129 /* assumes ep0 uses the same value for both speeds ... */
2130 dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0;
2131
2132 /* and that all endpoints are dual-speed */
2133 hs_source_desc.bEndpointAddress =
2134 fs_source_desc.bEndpointAddress;
2135 hs_sink_desc.bEndpointAddress =
2136 fs_sink_desc.bEndpointAddress;
2bb37884 2137#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
23cd1385
RB
2138 if (status_ep)
2139 hs_status_desc.bEndpointAddress =
2140 fs_status_desc.bEndpointAddress;
2141#endif
2142 }
2143
2144 if (gadget_is_otg(gadget)) {
2145 otg_descriptor.bmAttributes |= USB_OTG_HNP,
2146 eth_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
2147 eth_config.bMaxPower = 4;
7612a43d
VK
2148#ifdef CONFIG_USB_ETH_RNDIS
2149 rndis_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
2150 rndis_config.bMaxPower = 4;
2151#endif
23cd1385
RB
2152 }
2153
7612a43d
VK
2154
2155 /* network device setup */
d4a37553 2156 dev->net = l_priv->netdev;
23cd1385
RB
2157
2158 dev->cdc = cdc;
2159 dev->zlp = zlp;
2160
2161 dev->in_ep = in_ep;
2162 dev->out_ep = out_ep;
2163 dev->status_ep = status_ep;
2164
d4a37553 2165 memset(tmp, 0, sizeof(tmp));
6142e0ae
VK
2166 /*
2167 * Module params for these addresses should come from ID proms.
7612a43d 2168 * The host side address is used with CDC and RNDIS, and commonly
23cd1385
RB
2169 * ends up in a persistent config database. It's not clear if
2170 * host side code for the SAFE thing cares -- its original BLAN
2171 * thing didn't, Sharp never assigned those addresses on Zaurii.
2172 */
d4a37553
M
2173 get_ether_addr(dev_addr, pdata->enetaddr);
2174 memcpy(tmp, pdata->enetaddr, sizeof(pdata->enetaddr));
23cd1385
RB
2175
2176 get_ether_addr(host_addr, dev->host_mac);
2177
6142e0ae
VK
2178 sprintf(ethaddr, "%02X%02X%02X%02X%02X%02X",
2179 dev->host_mac[0], dev->host_mac[1],
2180 dev->host_mac[2], dev->host_mac[3],
2181 dev->host_mac[4], dev->host_mac[5]);
23cd1385 2182
7612a43d
VK
2183 if (rndis) {
2184 status = rndis_init();
2185 if (status < 0) {
9b643e31 2186 pr_err("can't init RNDIS, %d", status);
7612a43d
VK
2187 goto fail;
2188 }
23cd1385
RB
2189 }
2190
6142e0ae
VK
2191 /*
2192 * use PKTSIZE (or aligned... from u-boot) and set
2193 * wMaxSegmentSize accordingly
2194 */
23cd1385
RB
2195 dev->mtu = PKTSIZE_ALIGN; /* RNDIS does not like this, only 1514, TODO*/
2196
2197 /* preallocate control message data and buffer */
6142e0ae 2198 dev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
23cd1385
RB
2199 if (!dev->req)
2200 goto fail;
2201 dev->req->buf = control_req;
2202 dev->req->complete = eth_setup_complete;
2203
2204 /* ... and maybe likewise for status transfer */
2bb37884 2205#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
23cd1385 2206 if (dev->status_ep) {
6142e0ae
VK
2207 dev->stat_req = usb_ep_alloc_request(dev->status_ep,
2208 GFP_KERNEL);
23cd1385 2209 if (!dev->stat_req) {
6142e0ae 2210 usb_ep_free_request(dev->status_ep, dev->req);
23cd1385
RB
2211
2212 goto fail;
2213 }
df559c1d 2214 dev->stat_req->buf = status_req;
23cd1385
RB
2215 dev->stat_req->context = NULL;
2216 }
2217#endif
2218
2219 /* finish hookup to lower layer ... */
2220 dev->gadget = gadget;
6142e0ae 2221 set_gadget_data(gadget, dev);
23cd1385
RB
2222 gadget->ep0->driver_data = dev;
2223
6142e0ae
VK
2224 /*
2225 * two kinds of host-initiated state changes:
23cd1385
RB
2226 * - iff DATA transfer is active, carrier is "on"
2227 * - tx queueing enabled if open *and* carrier is "on"
2228 */
7612a43d
VK
2229
2230 printf("using %s, OUT %s IN %s%s%s\n", gadget->name,
2231 out_ep->name, in_ep->name,
2232 status_ep ? " STATUS " : "",
2233 status_ep ? status_ep->name : ""
2234 );
d4a37553 2235 printf("MAC %pM\n", pdata->enetaddr);
7612a43d
VK
2236
2237 if (cdc || rndis)
2238 printf("HOST MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
2239 dev->host_mac[0], dev->host_mac[1],
2240 dev->host_mac[2], dev->host_mac[3],
2241 dev->host_mac[4], dev->host_mac[5]);
2242
2243 if (rndis) {
2244 u32 vendorID = 0;
2245
2246 /* FIXME RNDIS vendor id == "vendor NIC code" == ? */
2247
2248 dev->rndis_config = rndis_register(rndis_control_ack);
2249 if (dev->rndis_config < 0) {
2250fail0:
2251 eth_unbind(gadget);
2252 debug("RNDIS setup failed\n");
2253 status = -ENODEV;
2254 goto fail;
2255 }
2256
2257 /* these set up a lot of the OIDs that RNDIS needs */
2258 rndis_set_host_mac(dev->rndis_config, dev->host_mac);
2259 if (rndis_set_param_dev(dev->rndis_config, dev->net, dev->mtu,
2260 &dev->stats, &dev->cdc_filter))
2261 goto fail0;
2262 if (rndis_set_param_vendor(dev->rndis_config, vendorID,
2263 manufacturer))
2264 goto fail0;
2265 if (rndis_set_param_medium(dev->rndis_config,
2266 NDIS_MEDIUM_802_3, 0))
2267 goto fail0;
2268 printf("RNDIS ready\n");
2269 }
23cd1385
RB
2270 return 0;
2271
2272fail:
9b643e31 2273 pr_err("%s failed, status = %d", __func__, status);
6142e0ae 2274 eth_unbind(gadget);
7612a43d 2275 return status;
23cd1385
RB
2276}
2277
7612a43d 2278/*-------------------------------------------------------------------------*/
da768f9c 2279static void usb_eth_stop(struct udevice *dev);
1c1464c2 2280
da768f9c 2281static int usb_eth_start(struct udevice *udev)
23cd1385 2282{
da768f9c 2283 struct ether_priv *priv = dev_get_priv(udev);
ae70100c 2284 struct eth_dev *dev = &priv->ethdev;
23cd1385
RB
2285 struct usb_gadget *gadget;
2286 unsigned long ts;
2287 unsigned long timeout = USB_CONNECT_TIMEOUT;
2288
23cd1385 2289 dev->network_started = 0;
23cd1385
RB
2290
2291 packet_received = 0;
2292 packet_sent = 0;
2293
2294 gadget = dev->gadget;
2295 usb_gadget_connect(gadget);
2296
00caae6d 2297 if (env_get("cdc_connect_timeout"))
0b1284eb 2298 timeout = dectoul(env_get("cdc_connect_timeout"), NULL) * CONFIG_SYS_HZ;
23cd1385 2299 ts = get_timer(0);
17b4f308 2300 while (!dev->network_started) {
23cd1385
RB
2301 /* Handle control-c and timeouts */
2302 if (ctrlc() || (get_timer(ts) > timeout)) {
9b643e31 2303 pr_err("The remote end did not respond in time.");
23cd1385
RB
2304 goto fail;
2305 }
bfa352d1 2306 dm_usb_gadget_handle_interrupts(udev->parent);
23cd1385
RB
2307 }
2308
98fae970 2309 packet_received = 0;
6142e0ae 2310 rx_submit(dev, dev->rx_req, 0);
23cd1385
RB
2311 return 0;
2312fail:
da768f9c 2313 usb_eth_stop(udev);
23cd1385
RB
2314 return -1;
2315}
2316
da768f9c 2317static int usb_eth_send(struct udevice *udev, void *packet, int length)
23cd1385 2318{
da768f9c 2319 struct ether_priv *priv = dev_get_priv(udev);
23cd1385 2320 int retval;
7612a43d 2321 void *rndis_pkt = NULL;
ae70100c 2322 struct eth_dev *dev = &priv->ethdev;
ac5d32d1 2323 struct usb_request *req = dev->tx_req;
6142e0ae
VK
2324 unsigned long ts;
2325 unsigned long timeout = USB_CONNECT_TIMEOUT;
23cd1385 2326
7de73185 2327 debug("%s:...\n", __func__);
23cd1385 2328
7612a43d
VK
2329 /* new buffer is needed to include RNDIS header */
2330 if (rndis_active(dev)) {
2331 rndis_pkt = malloc(length +
2332 sizeof(struct rndis_packet_msg_type));
2333 if (!rndis_pkt) {
9b643e31 2334 pr_err("No memory to alloc RNDIS packet");
7612a43d
VK
2335 goto drop;
2336 }
2337 rndis_add_hdr(rndis_pkt, length);
2338 memcpy(rndis_pkt + sizeof(struct rndis_packet_msg_type),
10cbe3b6 2339 packet, length);
7612a43d
VK
2340 packet = rndis_pkt;
2341 length += sizeof(struct rndis_packet_msg_type);
2342 }
10cbe3b6 2343 req->buf = packet;
23cd1385
RB
2344 req->context = NULL;
2345 req->complete = tx_complete;
2346
6142e0ae
VK
2347 /*
2348 * use zlp framing on tx for strict CDC-Ether conformance,
23cd1385
RB
2349 * though any robust network rx path ignores extra padding.
2350 * and some hardware doesn't like to write zlps.
2351 */
2352 req->zero = 1;
2353 if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0)
2354 length++;
2355
2356 req->length = length;
2357#if 0
2358 /* throttle highspeed IRQ rate back slightly */
2359 if (gadget_is_dualspeed(dev->gadget))
2360 req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH)
2361 ? ((dev->tx_qlen % qmult) != 0) : 0;
2362#endif
6142e0ae
VK
2363 dev->tx_qlen = 1;
2364 ts = get_timer(0);
2365 packet_sent = 0;
23cd1385 2366
6142e0ae 2367 retval = usb_ep_queue(dev->in_ep, req, GFP_ATOMIC);
23cd1385
RB
2368
2369 if (!retval)
7de73185 2370 debug("%s: packet queued\n", __func__);
6142e0ae
VK
2371 while (!packet_sent) {
2372 if (get_timer(ts) > timeout) {
2373 printf("timeout sending packets to usb ethernet\n");
2374 return -1;
2375 }
bfa352d1 2376 dm_usb_gadget_handle_interrupts(udev->parent);
23cd1385 2377 }
3c425fc0 2378 free(rndis_pkt);
23cd1385
RB
2379
2380 return 0;
7612a43d
VK
2381drop:
2382 dev->stats.tx_dropped++;
2383 return -ENOMEM;
23cd1385
RB
2384}
2385
da768f9c 2386static void usb_eth_stop(struct udevice *udev)
23cd1385 2387{
da768f9c 2388 struct ether_priv *priv = dev_get_priv(udev);
ae70100c 2389 struct eth_dev *dev = &priv->ethdev;
23cd1385 2390
988ee3e3
LW
2391 /* If the gadget not registered, simple return */
2392 if (!dev->gadget)
2393 return;
2394
e4ae6660
VK
2395 /*
2396 * Some USB controllers may need additional deinitialization here
2397 * before dropping pull-up (also due to hardware issues).
2398 * For example: unhandled interrupt with status stage started may
2399 * bring the controller to fully broken state (until board reset).
2400 * There are some variants to debug and fix such cases:
2401 * 1) In the case of RNDIS connection eth_stop can perform additional
2402 * interrupt handling. See RNDIS_COMPLETE_SIGNAL_DISCONNECT definition.
2403 * 2) 'pullup' callback in your UDC driver can be improved to perform
2404 * this deinitialization.
2405 */
bfa352d1 2406 eth_stop(udev);
7612a43d 2407
23cd1385 2408 usb_gadget_disconnect(dev->gadget);
b3649f3b
VK
2409
2410 /* Clear pending interrupt */
2411 if (dev->network_started) {
bfa352d1 2412 dm_usb_gadget_handle_interrupts(udev->parent);
b3649f3b
VK
2413 dev->network_started = 0;
2414 }
23cd1385
RB
2415}
2416
d4a37553
M
2417static int usb_eth_recv(struct udevice *dev, int flags, uchar **packetp)
2418{
2419 struct ether_priv *priv = dev_get_priv(dev);
2420 struct eth_dev *ethdev = &priv->ethdev;
d4a37553 2421
bfa352d1 2422 dm_usb_gadget_handle_interrupts(dev->parent);
d4a37553
M
2423
2424 if (packet_received) {
2425 if (ethdev->rx_req) {
2426 *packetp = (uchar *)net_rx_packets[0];
2427 return ethdev->rx_req->length;
2428 } else {
9b643e31 2429 pr_err("dev->rx_req invalid");
d4a37553
M
2430 return -EFAULT;
2431 }
2432 }
2433
2434 return -EAGAIN;
2435}
2436
2437static int usb_eth_free_pkt(struct udevice *dev, uchar *packet,
2438 int length)
2439{
2440 struct ether_priv *priv = dev_get_priv(dev);
2441 struct eth_dev *ethdev = &priv->ethdev;
2442
2443 packet_received = 0;
2444
2445 return rx_submit(ethdev, ethdev->rx_req, 0);
2446}
2447
d4a37553
M
2448static const struct eth_ops usb_eth_ops = {
2449 .start = usb_eth_start,
2450 .send = usb_eth_send,
2451 .recv = usb_eth_recv,
2452 .free_pkt = usb_eth_free_pkt,
2453 .stop = usb_eth_stop,
2454};
2455
2456int usb_ether_init(void)
2457{
d4a37553
M
2458 struct udevice *usb_dev;
2459 int ret;
2460
2cb43ef1
MS
2461 uclass_first_device(UCLASS_USB_GADGET_GENERIC, &usb_dev);
2462 if (!usb_dev) {
9b643e31 2463 pr_err("No USB device found\n");
2cb43ef1 2464 return -ENODEV;
d4a37553
M
2465 }
2466
2cb43ef1
MS
2467 ret = device_bind_driver(usb_dev, "usb_ether", "usb_ether", NULL);
2468 if (ret) {
9b643e31 2469 pr_err("usb - not able to bind usb_ether device\n");
d4a37553
M
2470 return ret;
2471 }
2472
bfa352d1 2473 return 0;
d4a37553
M
2474}
2475
47b121f4
MV
2476static int usb_eth_probe(struct udevice *dev)
2477{
2478 struct ether_priv *priv = dev_get_priv(dev);
2479 struct eth_pdata *pdata = dev_get_plat(dev);
2480
2481 priv->netdev = dev;
2482 l_priv = priv;
2483
2484 get_ether_addr(CONFIG_USBNET_DEV_ADDR, pdata->enetaddr);
2485 eth_env_set_enetaddr("usbnet_devaddr", pdata->enetaddr);
2486
718f1d41
MV
2487 /* Configure default mac-addresses for the USB ethernet device */
2488#ifdef CONFIG_USBNET_DEV_ADDR
2489 strlcpy(dev_addr, CONFIG_USBNET_DEV_ADDR, sizeof(dev_addr));
2490#endif
2491#ifdef CONFIG_USBNET_HOST_ADDR
2492 strlcpy(host_addr, CONFIG_USBNET_HOST_ADDR, sizeof(host_addr));
2493#endif
2494 /* Check if the user overruled the MAC addresses */
2495 if (env_get("usbnet_devaddr"))
2496 strlcpy(dev_addr, env_get("usbnet_devaddr"),
2497 sizeof(dev_addr));
2498
2499 if (env_get("usbnet_hostaddr"))
2500 strlcpy(host_addr, env_get("usbnet_hostaddr"),
2501 sizeof(host_addr));
2502
2503 if (!is_eth_addr_valid(dev_addr)) {
2504 pr_err("Need valid 'usbnet_devaddr' to be set");
2505 return -EINVAL;
2506 }
2507 if (!is_eth_addr_valid(host_addr)) {
2508 pr_err("Need valid 'usbnet_hostaddr' to be set");
2509 return -EINVAL;
2510 }
2511
2512 priv->eth_driver.speed = DEVSPEED;
2513 priv->eth_driver.bind = eth_bind;
2514 priv->eth_driver.unbind = eth_unbind;
2515 priv->eth_driver.setup = eth_setup;
2516 priv->eth_driver.reset = eth_disconnect;
2517 priv->eth_driver.disconnect = eth_disconnect;
2518 priv->eth_driver.suspend = eth_suspend;
2519 priv->eth_driver.resume = eth_resume;
2520 return usb_gadget_register_driver(&priv->eth_driver);
2521}
2522
2523static int usb_eth_remove(struct udevice *dev)
2524{
2525 struct ether_priv *priv = dev_get_priv(dev);
2526
2527 usb_gadget_unregister_driver(&priv->eth_driver);
2528
2529 return 0;
2530}
2531
2532static int usb_eth_unbind(struct udevice *dev)
2533{
bfa352d1 2534 udc_device_put(dev->parent);
718f1d41 2535
47b121f4
MV
2536 return 0;
2537}
2538
d4a37553
M
2539U_BOOT_DRIVER(eth_usb) = {
2540 .name = "usb_ether",
2541 .id = UCLASS_ETH,
2542 .probe = usb_eth_probe,
718f1d41
MV
2543 .remove = usb_eth_remove,
2544 .unbind = usb_eth_unbind,
d4a37553 2545 .ops = &usb_eth_ops,
41575d8e 2546 .priv_auto = sizeof(struct ether_priv),
caa4daa2 2547 .plat_auto = sizeof(struct eth_pdata),
d4a37553
M
2548 .flags = DM_FLAG_ALLOC_PRIV_DMA,
2549};