blob: 9ce2c176033397bf8b7c06e5d6132700b790dd97 [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 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22
23#include <common.h>
24#include <asm/errno.h>
Vitaly Kuzmichev24fcb472011-02-11 18:18:32 +030025#include <linux/netdevice.h>
Remy Bohmerdf063442009-07-29 18:18:43 +020026#include <linux/usb/ch9.h>
Lukasz Majewski373cc992012-05-02 13:11:34 +020027#include <usbdescriptors.h>
Remy Bohmerdf063442009-07-29 18:18:43 +020028#include <linux/usb/cdc.h>
29#include <linux/usb/gadget.h>
30#include <net.h>
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +030031#include <malloc.h>
Remy Bohmerdf063442009-07-29 18:18:43 +020032#include <linux/ctype.h>
33
34#include "gadget_chips.h"
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +030035#include "rndis.h"
Remy Bohmerdf063442009-07-29 18:18:43 +020036
Vitaly Kuzmicheva36cff12010-12-28 16:59:31 +030037#define USB_NET_NAME "usb_ether"
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +040038
Remy Bohmerdf063442009-07-29 18:18:43 +020039#define atomic_read
40extern struct platform_data brd;
41#define spin_lock(x)
42#define spin_unlock(x)
43
44
45unsigned packet_received, packet_sent;
46
Lukasz Dalek6ca42dd2012-10-02 17:04:29 +020047#ifdef CONFIG_USB_GADGET_PXA2XX
48# undef DEV_CONFIG_CDC
49# define DEV_CONFIG_SUBSET 1
50#else
51# define DEV_CONFIG_CDC 1
52#endif
Remy Bohmerdf063442009-07-29 18:18:43 +020053#define GFP_ATOMIC ((gfp_t) 0)
54#define GFP_KERNEL ((gfp_t) 0)
55
56/*
57 * Ethernet gadget driver -- with CDC and non-CDC options
58 * Builds on hardware support for a full duplex link.
59 *
60 * CDC Ethernet is the standard USB solution for sending Ethernet frames
61 * using USB. Real hardware tends to use the same framing protocol but look
62 * different for control features. This driver strongly prefers to use
63 * this USB-IF standard as its open-systems interoperability solution;
64 * most host side USB stacks (except from Microsoft) support it.
65 *
66 * This is sometimes called "CDC ECM" (Ethernet Control Model) to support
67 * TLA-soup. "CDC ACM" (Abstract Control Model) is for modems, and a new
68 * "CDC EEM" (Ethernet Emulation Model) is starting to spread.
69 *
70 * There's some hardware that can't talk CDC ECM. We make that hardware
71 * implement a "minimalist" vendor-agnostic CDC core: same framing, but
72 * link-level setup only requires activating the configuration. Only the
73 * endpoint descriptors, and product/vendor IDs, are relevant; no control
74 * operations are available. Linux supports it, but other host operating
75 * systems may not. (This is a subset of CDC Ethernet.)
76 *
77 * It turns out that if you add a few descriptors to that "CDC Subset",
78 * (Windows) host side drivers from MCCI can treat it as one submode of
79 * a proprietary scheme called "SAFE" ... without needing to know about
80 * specific product/vendor IDs. So we do that, making it easier to use
81 * those MS-Windows drivers. Those added descriptors make it resemble a
82 * CDC MDLM device, but they don't change device behavior at all. (See
83 * MCCI Engineering report 950198 "SAFE Networking Functions".)
84 *
85 * A third option is also in use. Rather than CDC Ethernet, or something
86 * simpler, Microsoft pushes their own approach: RNDIS. The published
87 * RNDIS specs are ambiguous and appear to be incomplete, and are also
88 * needlessly complex. They borrow more from CDC ACM than CDC ECM.
89 */
90#define ETH_ALEN 6 /* Octets in one ethernet addr */
91#define ETH_HLEN 14 /* Total octets in header. */
92#define ETH_ZLEN 60 /* Min. octets in frame sans FCS */
93#define ETH_DATA_LEN 1500 /* Max. octets in payload */
94#define ETH_FRAME_LEN PKTSIZE_ALIGN /* Max. octets in frame sans FCS */
95#define ETH_FCS_LEN 4 /* Octets in the FCS */
96
97#define DRIVER_DESC "Ethernet Gadget"
98/* Based on linux 2.6.27 version */
99#define DRIVER_VERSION "May Day 2005"
100
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400101static const char shortname[] = "ether";
102static const char driver_desc[] = DRIVER_DESC;
Remy Bohmerdf063442009-07-29 18:18:43 +0200103
104#define RX_EXTRA 20 /* guard against rx overflows */
105
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300106#ifndef CONFIG_USB_ETH_RNDIS
107#define rndis_uninit(x) do {} while (0)
108#define rndis_deregister(c) do {} while (0)
109#define rndis_exit() do {} while (0)
110#endif
111
112/* CDC and RNDIS support the same host-chosen outgoing packet filters. */
Remy Bohmerdf063442009-07-29 18:18:43 +0200113#define DEFAULT_FILTER (USB_CDC_PACKET_TYPE_BROADCAST \
114 |USB_CDC_PACKET_TYPE_ALL_MULTICAST \
115 |USB_CDC_PACKET_TYPE_PROMISCUOUS \
116 |USB_CDC_PACKET_TYPE_DIRECTED)
117
118#define USB_CONNECT_TIMEOUT (3 * CONFIG_SYS_HZ)
119
120/*-------------------------------------------------------------------------*/
Vitaly Kuzmichev737f6b22011-02-11 18:18:33 +0300121
122struct eth_dev {
123 struct usb_gadget *gadget;
124 struct usb_request *req; /* for control responses */
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300125 struct usb_request *stat_req; /* for cdc & rndis status */
Vitaly Kuzmichev737f6b22011-02-11 18:18:33 +0300126
127 u8 config;
128 struct usb_ep *in_ep, *out_ep, *status_ep;
129 const struct usb_endpoint_descriptor
130 *in, *out, *status;
131
132 struct usb_request *tx_req, *rx_req;
133
134 struct eth_device *net;
135 struct net_device_stats stats;
136 unsigned int tx_qlen;
137
138 unsigned zlp:1;
139 unsigned cdc:1;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300140 unsigned rndis:1;
Vitaly Kuzmichev737f6b22011-02-11 18:18:33 +0300141 unsigned suspended:1;
142 unsigned network_started:1;
143 u16 cdc_filter;
144 unsigned long todo;
145 int mtu;
146#define WORK_RX_MEMORY 0
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300147 int rndis_config;
Vitaly Kuzmichev737f6b22011-02-11 18:18:33 +0300148 u8 host_mac[ETH_ALEN];
149};
150
151/*
152 * This version autoconfigures as much as possible at run-time.
153 *
154 * It also ASSUMES a self-powered device, without remote wakeup,
155 * although remote wakeup support would make sense.
156 */
157
158/*-------------------------------------------------------------------------*/
Remy Bohmerdf063442009-07-29 18:18:43 +0200159static struct eth_dev l_ethdev;
160static struct eth_device l_netdev;
161static struct usb_gadget_driver eth_driver;
162
163/*-------------------------------------------------------------------------*/
164
165/* "main" config is either CDC, or its simple subset */
166static inline int is_cdc(struct eth_dev *dev)
167{
168#if !defined(DEV_CONFIG_SUBSET)
169 return 1; /* only cdc possible */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400170#elif !defined(DEV_CONFIG_CDC)
Remy Bohmerdf063442009-07-29 18:18:43 +0200171 return 0; /* only subset possible */
172#else
173 return dev->cdc; /* depends on what hardware we found */
174#endif
175}
176
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300177/* "secondary" RNDIS config may sometimes be activated */
178static inline int rndis_active(struct eth_dev *dev)
179{
180#ifdef CONFIG_USB_ETH_RNDIS
181 return dev->rndis;
182#else
183 return 0;
184#endif
185}
186
187#define subset_active(dev) (!is_cdc(dev) && !rndis_active(dev))
188#define cdc_active(dev) (is_cdc(dev) && !rndis_active(dev))
Remy Bohmerdf063442009-07-29 18:18:43 +0200189
190#define DEFAULT_QLEN 2 /* double buffering by default */
191
192/* peak bulk transfer bits-per-second */
193#define HS_BPS (13 * 512 * 8 * 1000 * 8)
194#define FS_BPS (19 * 64 * 1 * 1000 * 8)
195
196#ifdef CONFIG_USB_GADGET_DUALSPEED
197#define DEVSPEED USB_SPEED_HIGH
198
Vitaly Kuzmichevaec83b82010-08-12 16:44:40 +0400199#ifdef CONFIG_USB_ETH_QMULT
200#define qmult CONFIG_USB_ETH_QMULT
201#else
202#define qmult 5
203#endif
204
Remy Bohmerdf063442009-07-29 18:18:43 +0200205/* for dual-speed hardware, use deeper queues at highspeed */
206#define qlen(gadget) \
207 (DEFAULT_QLEN*((gadget->speed == USB_SPEED_HIGH) ? qmult : 1))
208
209static inline int BITRATE(struct usb_gadget *g)
210{
211 return (g->speed == USB_SPEED_HIGH) ? HS_BPS : FS_BPS;
212}
213
214#else /* full speed (low speed doesn't do bulk) */
215
216#define qmult 1
217
218#define DEVSPEED USB_SPEED_FULL
219
220#define qlen(gadget) DEFAULT_QLEN
221
222static inline int BITRATE(struct usb_gadget *g)
223{
224 return FS_BPS;
225}
226#endif
227
Remy Bohmerdf063442009-07-29 18:18:43 +0200228/*-------------------------------------------------------------------------*/
229
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400230/*
231 * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
Remy Bohmerdf063442009-07-29 18:18:43 +0200232 * Instead: allocate your own, using normal USB-IF procedures.
233 */
234
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400235/*
236 * Thanks to NetChip Technologies for donating this product ID.
Remy Bohmerdf063442009-07-29 18:18:43 +0200237 * It's for devices with only CDC Ethernet configurations.
238 */
239#define CDC_VENDOR_NUM 0x0525 /* NetChip */
240#define CDC_PRODUCT_NUM 0xa4a1 /* Linux-USB Ethernet Gadget */
241
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400242/*
243 * For hardware that can't talk CDC, we use the same vendor ID that
Remy Bohmerdf063442009-07-29 18:18:43 +0200244 * ARM Linux has used for ethernet-over-usb, both with sa1100 and
245 * with pxa250. We're protocol-compatible, if the host-side drivers
246 * use the endpoint descriptors. bcdDevice (version) is nonzero, so
247 * drivers that need to hard-wire endpoint numbers have a hook.
248 *
249 * The protocol is a minimal subset of CDC Ether, which works on any bulk
250 * hardware that's not deeply broken ... even on hardware that can't talk
251 * RNDIS (like SA-1100, with no interrupt endpoint, or anything that
252 * doesn't handle control-OUT).
253 */
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300254#define SIMPLE_VENDOR_NUM 0x049f /* Compaq Computer Corp. */
255#define SIMPLE_PRODUCT_NUM 0x505a /* Linux-USB "CDC Subset" Device */
Remy Bohmerdf063442009-07-29 18:18:43 +0200256
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400257/*
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300258 * For hardware that can talk RNDIS and either of the above protocols,
259 * use this ID ... the windows INF files will know it. Unless it's
260 * used with CDC Ethernet, Linux 2.4 hosts will need updates to choose
261 * the non-RNDIS configuration.
262 */
263#define RNDIS_VENDOR_NUM 0x0525 /* NetChip */
264#define RNDIS_PRODUCT_NUM 0xa4a2 /* Ethernet/RNDIS Gadget */
265
266/*
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400267 * Some systems will want different product identifers published in the
Remy Bohmerdf063442009-07-29 18:18:43 +0200268 * device descriptor, either numbers or strings or both. These string
269 * parameters are in UTF-8 (superset of ASCII's 7 bit characters).
270 */
271
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300272/*
273 * Emulating them in eth_bind:
274 * static ushort idVendor;
275 * static ushort idProduct;
276 */
277
Remy Bohmerdf063442009-07-29 18:18:43 +0200278#if defined(CONFIG_USBNET_MANUFACTURER)
279static char *iManufacturer = CONFIG_USBNET_MANUFACTURER;
280#else
281static char *iManufacturer = "U-boot";
282#endif
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300283
284/* These probably need to be configurable. */
285static ushort bcdDevice;
Remy Bohmerdf063442009-07-29 18:18:43 +0200286static char *iProduct;
287static char *iSerialNumber;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300288
Remy Bohmerdf063442009-07-29 18:18:43 +0200289static char dev_addr[18];
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300290
Remy Bohmerdf063442009-07-29 18:18:43 +0200291static char host_addr[18];
292
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300293
Remy Bohmerdf063442009-07-29 18:18:43 +0200294/*-------------------------------------------------------------------------*/
295
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400296/*
297 * USB DRIVER HOOKUP (to the hardware driver, below us), mostly
Remy Bohmerdf063442009-07-29 18:18:43 +0200298 * ep0 implementation: descriptors, config management, setup().
299 * also optional class-specific notification interrupt transfer.
300 */
301
302/*
303 * DESCRIPTORS ... most are static, but strings and (full) configuration
304 * descriptors are built on demand. For now we do either full CDC, or
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300305 * our simple subset, with RNDIS as an optional second configuration.
306 *
307 * RNDIS includes some CDC ACM descriptors ... like CDC Ethernet. But
308 * the class descriptors match a modem (they're ignored; it's really just
309 * Ethernet functionality), they don't need the NOP altsetting, and the
310 * status transfer endpoint isn't optional.
Remy Bohmerdf063442009-07-29 18:18:43 +0200311 */
312
313#define STRING_MANUFACTURER 1
314#define STRING_PRODUCT 2
315#define STRING_ETHADDR 3
316#define STRING_DATA 4
317#define STRING_CONTROL 5
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300318#define STRING_RNDIS_CONTROL 6
Remy Bohmerdf063442009-07-29 18:18:43 +0200319#define STRING_CDC 7
320#define STRING_SUBSET 8
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300321#define STRING_RNDIS 9
Remy Bohmerdf063442009-07-29 18:18:43 +0200322#define STRING_SERIALNUMBER 10
323
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300324/* holds our biggest descriptor (or RNDIS response) */
Remy Bohmerdf063442009-07-29 18:18:43 +0200325#define USB_BUFSIZ 256
326
327/*
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300328 * This device advertises one configuration, eth_config, unless RNDIS
329 * is enabled (rndis_config) on hardware supporting at least two configs.
330 *
331 * NOTE: Controllers like superh_udc should probably be able to use
332 * an RNDIS-only configuration.
Remy Bohmerdf063442009-07-29 18:18:43 +0200333 *
334 * FIXME define some higher-powered configurations to make it easier
335 * to recharge batteries ...
336 */
337
338#define DEV_CONFIG_VALUE 1 /* cdc or subset */
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300339#define DEV_RNDIS_CONFIG_VALUE 2 /* rndis; optional */
Remy Bohmerdf063442009-07-29 18:18:43 +0200340
341static struct usb_device_descriptor
342device_desc = {
343 .bLength = sizeof device_desc,
344 .bDescriptorType = USB_DT_DEVICE,
345
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400346 .bcdUSB = __constant_cpu_to_le16(0x0200),
Remy Bohmerdf063442009-07-29 18:18:43 +0200347
348 .bDeviceClass = USB_CLASS_COMM,
349 .bDeviceSubClass = 0,
350 .bDeviceProtocol = 0,
351
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400352 .idVendor = __constant_cpu_to_le16(CDC_VENDOR_NUM),
353 .idProduct = __constant_cpu_to_le16(CDC_PRODUCT_NUM),
Remy Bohmerdf063442009-07-29 18:18:43 +0200354 .iManufacturer = STRING_MANUFACTURER,
355 .iProduct = STRING_PRODUCT,
356 .bNumConfigurations = 1,
357};
358
359static struct usb_otg_descriptor
360otg_descriptor = {
361 .bLength = sizeof otg_descriptor,
362 .bDescriptorType = USB_DT_OTG,
363
364 .bmAttributes = USB_OTG_SRP,
365};
366
367static struct usb_config_descriptor
368eth_config = {
369 .bLength = sizeof eth_config,
370 .bDescriptorType = USB_DT_CONFIG,
371
372 /* compute wTotalLength on the fly */
373 .bNumInterfaces = 2,
374 .bConfigurationValue = DEV_CONFIG_VALUE,
375 .iConfiguration = STRING_CDC,
376 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
377 .bMaxPower = 1,
378};
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300379
380#ifdef CONFIG_USB_ETH_RNDIS
381static struct usb_config_descriptor
382rndis_config = {
383 .bLength = sizeof rndis_config,
384 .bDescriptorType = USB_DT_CONFIG,
385
386 /* compute wTotalLength on the fly */
387 .bNumInterfaces = 2,
388 .bConfigurationValue = DEV_RNDIS_CONFIG_VALUE,
389 .iConfiguration = STRING_RNDIS,
390 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
391 .bMaxPower = 1,
392};
393#endif
Remy Bohmerdf063442009-07-29 18:18:43 +0200394
395/*
396 * Compared to the simple CDC subset, the full CDC Ethernet model adds
397 * three class descriptors, two interface descriptors, optional status
398 * endpoint. Both have a "data" interface and two bulk endpoints.
399 * There are also differences in how control requests are handled.
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300400 *
401 * RNDIS shares a lot with CDC-Ethernet, since it's a variant of the
402 * CDC-ACM (modem) spec. Unfortunately MSFT's RNDIS driver is buggy; it
403 * may hang or oops. Since bugfixes (or accurate specs, letting Linux
404 * work around those bugs) are unlikely to ever come from MSFT, you may
405 * wish to avoid using RNDIS.
406 *
407 * MCCI offers an alternative to RNDIS if you need to connect to Windows
408 * but have hardware that can't support CDC Ethernet. We add descriptors
409 * to present the CDC Subset as a (nonconformant) CDC MDLM variant called
410 * "SAFE". That borrows from both CDC Ethernet and CDC MDLM. You can
411 * get those drivers from MCCI, or bundled with various products.
Remy Bohmerdf063442009-07-29 18:18:43 +0200412 */
413
414#ifdef DEV_CONFIG_CDC
415static struct usb_interface_descriptor
416control_intf = {
417 .bLength = sizeof control_intf,
418 .bDescriptorType = USB_DT_INTERFACE,
419
420 .bInterfaceNumber = 0,
421 /* status endpoint is optional; this may be patched later */
422 .bNumEndpoints = 1,
423 .bInterfaceClass = USB_CLASS_COMM,
424 .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET,
425 .bInterfaceProtocol = USB_CDC_PROTO_NONE,
426 .iInterface = STRING_CONTROL,
427};
428#endif
429
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300430#ifdef CONFIG_USB_ETH_RNDIS
431static const struct usb_interface_descriptor
432rndis_control_intf = {
433 .bLength = sizeof rndis_control_intf,
434 .bDescriptorType = USB_DT_INTERFACE,
435
436 .bInterfaceNumber = 0,
437 .bNumEndpoints = 1,
438 .bInterfaceClass = USB_CLASS_COMM,
439 .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
440 .bInterfaceProtocol = USB_CDC_ACM_PROTO_VENDOR,
441 .iInterface = STRING_RNDIS_CONTROL,
442};
443#endif
444
Remy Bohmerdf063442009-07-29 18:18:43 +0200445static const struct usb_cdc_header_desc header_desc = {
446 .bLength = sizeof header_desc,
447 .bDescriptorType = USB_DT_CS_INTERFACE,
448 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
449
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400450 .bcdCDC = __constant_cpu_to_le16(0x0110),
Remy Bohmerdf063442009-07-29 18:18:43 +0200451};
452
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300453#if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Remy Bohmerdf063442009-07-29 18:18:43 +0200454
455static const struct usb_cdc_union_desc union_desc = {
456 .bLength = sizeof union_desc,
457 .bDescriptorType = USB_DT_CS_INTERFACE,
458 .bDescriptorSubType = USB_CDC_UNION_TYPE,
459
460 .bMasterInterface0 = 0, /* index of control interface */
461 .bSlaveInterface0 = 1, /* index of DATA interface */
462};
463
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300464#endif /* CDC || RNDIS */
465
466#ifdef CONFIG_USB_ETH_RNDIS
467
468static const struct usb_cdc_call_mgmt_descriptor call_mgmt_descriptor = {
469 .bLength = sizeof call_mgmt_descriptor,
470 .bDescriptorType = USB_DT_CS_INTERFACE,
471 .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE,
472
473 .bmCapabilities = 0x00,
474 .bDataInterface = 0x01,
475};
476
477static const struct usb_cdc_acm_descriptor acm_descriptor = {
478 .bLength = sizeof acm_descriptor,
479 .bDescriptorType = USB_DT_CS_INTERFACE,
480 .bDescriptorSubType = USB_CDC_ACM_TYPE,
481
482 .bmCapabilities = 0x00,
483};
484
485#endif
Remy Bohmerdf063442009-07-29 18:18:43 +0200486
487#ifndef DEV_CONFIG_CDC
488
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400489/*
490 * "SAFE" loosely follows CDC WMC MDLM, violating the spec in various
Remy Bohmerdf063442009-07-29 18:18:43 +0200491 * ways: data endpoints live in the control interface, there's no data
492 * interface, and it's not used to talk to a cell phone radio.
493 */
494
495static const struct usb_cdc_mdlm_desc mdlm_desc = {
496 .bLength = sizeof mdlm_desc,
497 .bDescriptorType = USB_DT_CS_INTERFACE,
498 .bDescriptorSubType = USB_CDC_MDLM_TYPE,
499
500 .bcdVersion = __constant_cpu_to_le16(0x0100),
501 .bGUID = {
502 0x5d, 0x34, 0xcf, 0x66, 0x11, 0x18, 0x11, 0xd6,
503 0xa2, 0x1a, 0x00, 0x01, 0x02, 0xca, 0x9a, 0x7f,
504 },
505};
506
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400507/*
508 * since "usb_cdc_mdlm_detail_desc" is a variable length structure, we
Remy Bohmerdf063442009-07-29 18:18:43 +0200509 * can't really use its struct. All we do here is say that we're using
510 * the submode of "SAFE" which directly matches the CDC Subset.
511 */
512static const u8 mdlm_detail_desc[] = {
513 6,
514 USB_DT_CS_INTERFACE,
515 USB_CDC_MDLM_DETAIL_TYPE,
516
517 0, /* "SAFE" */
518 0, /* network control capabilities (none) */
519 0, /* network data capabilities ("raw" encapsulation) */
520};
521
522#endif
523
Remy Bohmerdf063442009-07-29 18:18:43 +0200524static const struct usb_cdc_ether_desc ether_desc = {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400525 .bLength = sizeof(ether_desc),
Remy Bohmerdf063442009-07-29 18:18:43 +0200526 .bDescriptorType = USB_DT_CS_INTERFACE,
527 .bDescriptorSubType = USB_CDC_ETHERNET_TYPE,
528
529 /* this descriptor actually adds value, surprise! */
530 .iMACAddress = STRING_ETHADDR,
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400531 .bmEthernetStatistics = __constant_cpu_to_le32(0), /* no statistics */
532 .wMaxSegmentSize = __constant_cpu_to_le16(ETH_FRAME_LEN),
533 .wNumberMCFilters = __constant_cpu_to_le16(0),
Remy Bohmerdf063442009-07-29 18:18:43 +0200534 .bNumberPowerFilters = 0,
535};
536
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300537#if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Remy Bohmerdf063442009-07-29 18:18:43 +0200538
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400539/*
540 * include the status endpoint if we can, even where it's optional.
Remy Bohmerdf063442009-07-29 18:18:43 +0200541 * use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one
542 * packet, to simplify cancellation; and a big transfer interval, to
543 * waste less bandwidth.
544 *
545 * some drivers (like Linux 2.4 cdc-ether!) "need" it to exist even
546 * if they ignore the connect/disconnect notifications that real aether
547 * can provide. more advanced cdc configurations might want to support
548 * encapsulated commands (vendor-specific, using control-OUT).
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300549 *
550 * RNDIS requires the status endpoint, since it uses that encapsulation
551 * mechanism for its funky RPC scheme.
Remy Bohmerdf063442009-07-29 18:18:43 +0200552 */
553
554#define LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */
555#define STATUS_BYTECOUNT 16 /* 8 byte header + data */
556
557static struct usb_endpoint_descriptor
558fs_status_desc = {
559 .bLength = USB_DT_ENDPOINT_SIZE,
560 .bDescriptorType = USB_DT_ENDPOINT,
561
562 .bEndpointAddress = USB_DIR_IN,
563 .bmAttributes = USB_ENDPOINT_XFER_INT,
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400564 .wMaxPacketSize = __constant_cpu_to_le16(STATUS_BYTECOUNT),
Remy Bohmerdf063442009-07-29 18:18:43 +0200565 .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC,
566};
567#endif
568
569#ifdef DEV_CONFIG_CDC
570
571/* the default data interface has no endpoints ... */
572
573static const struct usb_interface_descriptor
574data_nop_intf = {
575 .bLength = sizeof data_nop_intf,
576 .bDescriptorType = USB_DT_INTERFACE,
577
578 .bInterfaceNumber = 1,
579 .bAlternateSetting = 0,
580 .bNumEndpoints = 0,
581 .bInterfaceClass = USB_CLASS_CDC_DATA,
582 .bInterfaceSubClass = 0,
583 .bInterfaceProtocol = 0,
584};
585
586/* ... but the "real" data interface has two bulk endpoints */
587
588static const struct usb_interface_descriptor
589data_intf = {
590 .bLength = sizeof data_intf,
591 .bDescriptorType = USB_DT_INTERFACE,
592
593 .bInterfaceNumber = 1,
594 .bAlternateSetting = 1,
595 .bNumEndpoints = 2,
596 .bInterfaceClass = USB_CLASS_CDC_DATA,
597 .bInterfaceSubClass = 0,
598 .bInterfaceProtocol = 0,
599 .iInterface = STRING_DATA,
600};
601
602#endif
603
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300604#ifdef CONFIG_USB_ETH_RNDIS
605
606/* RNDIS doesn't activate by changing to the "real" altsetting */
607
608static const struct usb_interface_descriptor
609rndis_data_intf = {
610 .bLength = sizeof rndis_data_intf,
611 .bDescriptorType = USB_DT_INTERFACE,
612
613 .bInterfaceNumber = 1,
614 .bAlternateSetting = 0,
615 .bNumEndpoints = 2,
616 .bInterfaceClass = USB_CLASS_CDC_DATA,
617 .bInterfaceSubClass = 0,
618 .bInterfaceProtocol = 0,
619 .iInterface = STRING_DATA,
620};
621
622#endif
623
Remy Bohmerdf063442009-07-29 18:18:43 +0200624#ifdef DEV_CONFIG_SUBSET
625
626/*
627 * "Simple" CDC-subset option is a simple vendor-neutral model that most
628 * full speed controllers can handle: one interface, two bulk endpoints.
629 *
630 * To assist host side drivers, we fancy it up a bit, and add descriptors
631 * so some host side drivers will understand it as a "SAFE" variant.
632 */
633
634static const struct usb_interface_descriptor
635subset_data_intf = {
636 .bLength = sizeof subset_data_intf,
637 .bDescriptorType = USB_DT_INTERFACE,
638
639 .bInterfaceNumber = 0,
640 .bAlternateSetting = 0,
641 .bNumEndpoints = 2,
642 .bInterfaceClass = USB_CLASS_COMM,
643 .bInterfaceSubClass = USB_CDC_SUBCLASS_MDLM,
644 .bInterfaceProtocol = 0,
645 .iInterface = STRING_DATA,
646};
647
648#endif /* SUBSET */
649
Remy Bohmerdf063442009-07-29 18:18:43 +0200650static struct usb_endpoint_descriptor
651fs_source_desc = {
652 .bLength = USB_DT_ENDPOINT_SIZE,
653 .bDescriptorType = USB_DT_ENDPOINT,
654
655 .bEndpointAddress = USB_DIR_IN,
656 .bmAttributes = USB_ENDPOINT_XFER_BULK,
657};
658
659static struct usb_endpoint_descriptor
660fs_sink_desc = {
661 .bLength = USB_DT_ENDPOINT_SIZE,
662 .bDescriptorType = USB_DT_ENDPOINT,
663
664 .bEndpointAddress = USB_DIR_OUT,
665 .bmAttributes = USB_ENDPOINT_XFER_BULK,
666};
667
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400668static const struct usb_descriptor_header *fs_eth_function[11] = {
Remy Bohmerdf063442009-07-29 18:18:43 +0200669 (struct usb_descriptor_header *) &otg_descriptor,
670#ifdef DEV_CONFIG_CDC
671 /* "cdc" mode descriptors */
672 (struct usb_descriptor_header *) &control_intf,
673 (struct usb_descriptor_header *) &header_desc,
674 (struct usb_descriptor_header *) &union_desc,
675 (struct usb_descriptor_header *) &ether_desc,
676 /* NOTE: status endpoint may need to be removed */
677 (struct usb_descriptor_header *) &fs_status_desc,
678 /* data interface, with altsetting */
679 (struct usb_descriptor_header *) &data_nop_intf,
680 (struct usb_descriptor_header *) &data_intf,
681 (struct usb_descriptor_header *) &fs_source_desc,
682 (struct usb_descriptor_header *) &fs_sink_desc,
683 NULL,
684#endif /* DEV_CONFIG_CDC */
685};
686
687static inline void fs_subset_descriptors(void)
688{
689#ifdef DEV_CONFIG_SUBSET
690 /* behavior is "CDC Subset"; extra descriptors say "SAFE" */
691 fs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
692 fs_eth_function[2] = (struct usb_descriptor_header *) &header_desc;
693 fs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc;
694 fs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc;
695 fs_eth_function[5] = (struct usb_descriptor_header *) &ether_desc;
696 fs_eth_function[6] = (struct usb_descriptor_header *) &fs_source_desc;
697 fs_eth_function[7] = (struct usb_descriptor_header *) &fs_sink_desc;
698 fs_eth_function[8] = NULL;
699#else
700 fs_eth_function[1] = NULL;
701#endif
702}
703
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300704#ifdef CONFIG_USB_ETH_RNDIS
705static const struct usb_descriptor_header *fs_rndis_function[] = {
706 (struct usb_descriptor_header *) &otg_descriptor,
707 /* control interface matches ACM, not Ethernet */
708 (struct usb_descriptor_header *) &rndis_control_intf,
709 (struct usb_descriptor_header *) &header_desc,
710 (struct usb_descriptor_header *) &call_mgmt_descriptor,
711 (struct usb_descriptor_header *) &acm_descriptor,
712 (struct usb_descriptor_header *) &union_desc,
713 (struct usb_descriptor_header *) &fs_status_desc,
714 /* data interface has no altsetting */
715 (struct usb_descriptor_header *) &rndis_data_intf,
716 (struct usb_descriptor_header *) &fs_source_desc,
717 (struct usb_descriptor_header *) &fs_sink_desc,
718 NULL,
719};
720#endif
721
Remy Bohmerdf063442009-07-29 18:18:43 +0200722/*
723 * usb 2.0 devices need to expose both high speed and full speed
724 * descriptors, unless they only run at full speed.
725 */
726
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300727#if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Remy Bohmerdf063442009-07-29 18:18:43 +0200728static struct usb_endpoint_descriptor
729hs_status_desc = {
730 .bLength = USB_DT_ENDPOINT_SIZE,
731 .bDescriptorType = USB_DT_ENDPOINT,
732
733 .bmAttributes = USB_ENDPOINT_XFER_INT,
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400734 .wMaxPacketSize = __constant_cpu_to_le16(STATUS_BYTECOUNT),
Remy Bohmerdf063442009-07-29 18:18:43 +0200735 .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4,
736};
737#endif /* DEV_CONFIG_CDC */
738
739static struct usb_endpoint_descriptor
740hs_source_desc = {
741 .bLength = USB_DT_ENDPOINT_SIZE,
742 .bDescriptorType = USB_DT_ENDPOINT,
743
744 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400745 .wMaxPacketSize = __constant_cpu_to_le16(512),
Remy Bohmerdf063442009-07-29 18:18:43 +0200746};
747
748static struct usb_endpoint_descriptor
749hs_sink_desc = {
750 .bLength = USB_DT_ENDPOINT_SIZE,
751 .bDescriptorType = USB_DT_ENDPOINT,
752
753 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400754 .wMaxPacketSize = __constant_cpu_to_le16(512),
Remy Bohmerdf063442009-07-29 18:18:43 +0200755};
756
757static struct usb_qualifier_descriptor
758dev_qualifier = {
759 .bLength = sizeof dev_qualifier,
760 .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
761
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400762 .bcdUSB = __constant_cpu_to_le16(0x0200),
Remy Bohmerdf063442009-07-29 18:18:43 +0200763 .bDeviceClass = USB_CLASS_COMM,
764
765 .bNumConfigurations = 1,
766};
767
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400768static const struct usb_descriptor_header *hs_eth_function[11] = {
Remy Bohmerdf063442009-07-29 18:18:43 +0200769 (struct usb_descriptor_header *) &otg_descriptor,
770#ifdef DEV_CONFIG_CDC
771 /* "cdc" mode descriptors */
772 (struct usb_descriptor_header *) &control_intf,
773 (struct usb_descriptor_header *) &header_desc,
774 (struct usb_descriptor_header *) &union_desc,
775 (struct usb_descriptor_header *) &ether_desc,
776 /* NOTE: status endpoint may need to be removed */
777 (struct usb_descriptor_header *) &hs_status_desc,
778 /* data interface, with altsetting */
779 (struct usb_descriptor_header *) &data_nop_intf,
780 (struct usb_descriptor_header *) &data_intf,
781 (struct usb_descriptor_header *) &hs_source_desc,
782 (struct usb_descriptor_header *) &hs_sink_desc,
783 NULL,
784#endif /* DEV_CONFIG_CDC */
785};
786
787static inline void hs_subset_descriptors(void)
788{
789#ifdef DEV_CONFIG_SUBSET
790 /* behavior is "CDC Subset"; extra descriptors say "SAFE" */
791 hs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
792 hs_eth_function[2] = (struct usb_descriptor_header *) &header_desc;
793 hs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc;
794 hs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc;
795 hs_eth_function[5] = (struct usb_descriptor_header *) &ether_desc;
796 hs_eth_function[6] = (struct usb_descriptor_header *) &hs_source_desc;
797 hs_eth_function[7] = (struct usb_descriptor_header *) &hs_sink_desc;
798 hs_eth_function[8] = NULL;
799#else
800 hs_eth_function[1] = NULL;
801#endif
802}
803
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300804#ifdef CONFIG_USB_ETH_RNDIS
805static const struct usb_descriptor_header *hs_rndis_function[] = {
806 (struct usb_descriptor_header *) &otg_descriptor,
807 /* control interface matches ACM, not Ethernet */
808 (struct usb_descriptor_header *) &rndis_control_intf,
809 (struct usb_descriptor_header *) &header_desc,
810 (struct usb_descriptor_header *) &call_mgmt_descriptor,
811 (struct usb_descriptor_header *) &acm_descriptor,
812 (struct usb_descriptor_header *) &union_desc,
813 (struct usb_descriptor_header *) &hs_status_desc,
814 /* data interface has no altsetting */
815 (struct usb_descriptor_header *) &rndis_data_intf,
816 (struct usb_descriptor_header *) &hs_source_desc,
817 (struct usb_descriptor_header *) &hs_sink_desc,
818 NULL,
819};
820#endif
821
822
Remy Bohmerdf063442009-07-29 18:18:43 +0200823/* maxpacket and other transfer characteristics vary by speed. */
824static inline struct usb_endpoint_descriptor *
825ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *hs,
826 struct usb_endpoint_descriptor *fs)
827{
828 if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
829 return hs;
830 return fs;
831}
832
Remy Bohmerdf063442009-07-29 18:18:43 +0200833/*-------------------------------------------------------------------------*/
834
835/* descriptors that are built on-demand */
836
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400837static char manufacturer[50];
838static char product_desc[40] = DRIVER_DESC;
839static char serial_number[20];
Remy Bohmerdf063442009-07-29 18:18:43 +0200840
841/* address that the host will use ... usually assigned at random */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400842static char ethaddr[2 * ETH_ALEN + 1];
Remy Bohmerdf063442009-07-29 18:18:43 +0200843
844/* static strings, in UTF-8 */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400845static struct usb_string strings[] = {
Remy Bohmerdf063442009-07-29 18:18:43 +0200846 { STRING_MANUFACTURER, manufacturer, },
847 { STRING_PRODUCT, product_desc, },
848 { STRING_SERIALNUMBER, serial_number, },
849 { STRING_DATA, "Ethernet Data", },
850 { STRING_ETHADDR, ethaddr, },
851#ifdef DEV_CONFIG_CDC
852 { STRING_CDC, "CDC Ethernet", },
853 { STRING_CONTROL, "CDC Communications Control", },
854#endif
855#ifdef DEV_CONFIG_SUBSET
856 { STRING_SUBSET, "CDC Ethernet Subset", },
857#endif
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300858#ifdef CONFIG_USB_ETH_RNDIS
859 { STRING_RNDIS, "RNDIS", },
860 { STRING_RNDIS_CONTROL, "RNDIS Communications Control", },
861#endif
Remy Bohmerdf063442009-07-29 18:18:43 +0200862 { } /* end of list */
863};
864
865static struct usb_gadget_strings stringtab = {
866 .language = 0x0409, /* en-us */
867 .strings = strings,
868};
869
Remy Bohmerdf063442009-07-29 18:18:43 +0200870/*============================================================================*/
871static u8 control_req[USB_BUFSIZ];
Lukasz Dalek6ca42dd2012-10-02 17:04:29 +0200872#if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Stefano Babic76e3d4e2010-08-15 14:18:59 +0200873static u8 status_req[STATUS_BYTECOUNT] __attribute__ ((aligned(4)));
Lukasz Dalek6ca42dd2012-10-02 17:04:29 +0200874#endif
Remy Bohmerdf063442009-07-29 18:18:43 +0200875
876
Remy Bohmerdf063442009-07-29 18:18:43 +0200877/**
878 * strlcpy - Copy a %NUL terminated string into a sized buffer
879 * @dest: Where to copy the string to
880 * @src: Where to copy the string from
881 * @size: size of destination buffer
882 *
883 * Compatible with *BSD: the result is always a valid
884 * NUL-terminated string that fits in the buffer (unless,
885 * of course, the buffer size is zero). It does not pad
886 * out the result like strncpy() does.
887 */
888size_t strlcpy(char *dest, const char *src, size_t size)
889{
890 size_t ret = strlen(src);
891
892 if (size) {
893 size_t len = (ret >= size) ? size - 1 : ret;
894 memcpy(dest, src, len);
895 dest[len] = '\0';
896 }
897 return ret;
898}
899
Remy Bohmerdf063442009-07-29 18:18:43 +0200900/*============================================================================*/
901
902/*
903 * one config, two interfaces: control, data.
904 * complications: class descriptors, and an altsetting.
905 */
906static int
907config_buf(struct usb_gadget *g, u8 *buf, u8 type, unsigned index, int is_otg)
908{
909 int len;
910 const struct usb_config_descriptor *config;
911 const struct usb_descriptor_header **function;
912 int hs = 0;
913
914 if (gadget_is_dualspeed(g)) {
915 hs = (g->speed == USB_SPEED_HIGH);
916 if (type == USB_DT_OTHER_SPEED_CONFIG)
917 hs = !hs;
918 }
919#define which_fn(t) (hs ? hs_ ## t ## _function : fs_ ## t ## _function)
920
921 if (index >= device_desc.bNumConfigurations)
922 return -EINVAL;
923
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300924#ifdef CONFIG_USB_ETH_RNDIS
925 /*
926 * list the RNDIS config first, to make Microsoft's drivers
927 * happy. DOCSIS 1.0 needs this too.
928 */
929 if (device_desc.bNumConfigurations == 2 && index == 0) {
930 config = &rndis_config;
931 function = which_fn(rndis);
932 } else
933#endif
934 {
935 config = &eth_config;
936 function = which_fn(eth);
937 }
Remy Bohmerdf063442009-07-29 18:18:43 +0200938
939 /* for now, don't advertise srp-only devices */
940 if (!is_otg)
941 function++;
942
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400943 len = usb_gadget_config_buf(config, buf, USB_BUFSIZ, function);
Remy Bohmerdf063442009-07-29 18:18:43 +0200944 if (len < 0)
945 return len;
946 ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
947 return len;
948}
949
950/*-------------------------------------------------------------------------*/
951
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300952static void eth_start(struct eth_dev *dev, gfp_t gfp_flags);
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400953static int alloc_requests(struct eth_dev *dev, unsigned n, gfp_t gfp_flags);
Remy Bohmerdf063442009-07-29 18:18:43 +0200954
955static int
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400956set_ether_config(struct eth_dev *dev, gfp_t gfp_flags)
Remy Bohmerdf063442009-07-29 18:18:43 +0200957{
958 int result = 0;
959 struct usb_gadget *gadget = dev->gadget;
960
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300961#if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
962 /* status endpoint used for RNDIS and (optionally) CDC */
Remy Bohmerdf063442009-07-29 18:18:43 +0200963 if (!subset_active(dev) && dev->status_ep) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400964 dev->status = ep_desc(gadget, &hs_status_desc,
Remy Bohmerdf063442009-07-29 18:18:43 +0200965 &fs_status_desc);
966 dev->status_ep->driver_data = dev;
967
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400968 result = usb_ep_enable(dev->status_ep, dev->status);
Remy Bohmerdf063442009-07-29 18:18:43 +0200969 if (result != 0) {
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +0400970 debug("enable %s --> %d\n",
Remy Bohmerdf063442009-07-29 18:18:43 +0200971 dev->status_ep->name, result);
972 goto done;
973 }
974 }
975#endif
976
977 dev->in = ep_desc(gadget, &hs_source_desc, &fs_source_desc);
978 dev->in_ep->driver_data = dev;
979
980 dev->out = ep_desc(gadget, &hs_sink_desc, &fs_sink_desc);
981 dev->out_ep->driver_data = dev;
982
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400983 /*
984 * With CDC, the host isn't allowed to use these two data
Remy Bohmerdf063442009-07-29 18:18:43 +0200985 * endpoints in the default altsetting for the interface.
986 * so we don't activate them yet. Reset from SET_INTERFACE.
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300987 *
988 * Strictly speaking RNDIS should work the same: activation is
989 * a side effect of setting a packet filter. Deactivation is
990 * from REMOTE_NDIS_HALT_MSG, reset from REMOTE_NDIS_RESET_MSG.
Remy Bohmerdf063442009-07-29 18:18:43 +0200991 */
992 if (!cdc_active(dev)) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400993 result = usb_ep_enable(dev->in_ep, dev->in);
Remy Bohmerdf063442009-07-29 18:18:43 +0200994 if (result != 0) {
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +0400995 debug("enable %s --> %d\n",
Remy Bohmerdf063442009-07-29 18:18:43 +0200996 dev->in_ep->name, result);
997 goto done;
998 }
999
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001000 result = usb_ep_enable(dev->out_ep, dev->out);
Remy Bohmerdf063442009-07-29 18:18:43 +02001001 if (result != 0) {
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001002 debug("enable %s --> %d\n",
Remy Bohmerdf063442009-07-29 18:18:43 +02001003 dev->out_ep->name, result);
1004 goto done;
1005 }
1006 }
1007
1008done:
1009 if (result == 0)
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001010 result = alloc_requests(dev, qlen(gadget), gfp_flags);
Remy Bohmerdf063442009-07-29 18:18:43 +02001011
1012 /* on error, disable any endpoints */
1013 if (result < 0) {
Vitaly Kuzmichev750be4c2010-08-13 17:02:29 +04001014 if (!subset_active(dev) && dev->status_ep)
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001015 (void) usb_ep_disable(dev->status_ep);
Remy Bohmerdf063442009-07-29 18:18:43 +02001016 dev->status = NULL;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001017 (void) usb_ep_disable(dev->in_ep);
1018 (void) usb_ep_disable(dev->out_ep);
Remy Bohmerdf063442009-07-29 18:18:43 +02001019 dev->in = NULL;
1020 dev->out = NULL;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001021 } else if (!cdc_active(dev)) {
1022 /*
1023 * activate non-CDC configs right away
1024 * this isn't strictly according to the RNDIS spec
1025 */
1026 eth_start(dev, GFP_ATOMIC);
Remy Bohmerdf063442009-07-29 18:18:43 +02001027 }
1028
1029 /* caller is responsible for cleanup on error */
1030 return result;
1031}
1032
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001033static void eth_reset_config(struct eth_dev *dev)
Remy Bohmerdf063442009-07-29 18:18:43 +02001034{
1035 if (dev->config == 0)
1036 return;
1037
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001038 debug("%s\n", __func__);
1039
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001040 rndis_uninit(dev->rndis_config);
1041
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001042 /*
1043 * disable endpoints, forcing (synchronous) completion of
Remy Bohmerdf063442009-07-29 18:18:43 +02001044 * pending i/o. then free the requests.
1045 */
1046
1047 if (dev->in) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001048 usb_ep_disable(dev->in_ep);
Remy Bohmerdf063442009-07-29 18:18:43 +02001049 if (dev->tx_req) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001050 usb_ep_free_request(dev->in_ep, dev->tx_req);
1051 dev->tx_req = NULL;
Remy Bohmerdf063442009-07-29 18:18:43 +02001052 }
1053 }
1054 if (dev->out) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001055 usb_ep_disable(dev->out_ep);
Remy Bohmerdf063442009-07-29 18:18:43 +02001056 if (dev->rx_req) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001057 usb_ep_free_request(dev->out_ep, dev->rx_req);
1058 dev->rx_req = NULL;
Remy Bohmerdf063442009-07-29 18:18:43 +02001059 }
1060 }
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001061 if (dev->status)
1062 usb_ep_disable(dev->status_ep);
1063
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001064 dev->rndis = 0;
Remy Bohmerdf063442009-07-29 18:18:43 +02001065 dev->cdc_filter = 0;
1066 dev->config = 0;
1067}
1068
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001069/*
1070 * change our operational config. must agree with the code
Remy Bohmerdf063442009-07-29 18:18:43 +02001071 * that returns config descriptors, and altsetting code.
1072 */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001073static int eth_set_config(struct eth_dev *dev, unsigned number,
1074 gfp_t gfp_flags)
Remy Bohmerdf063442009-07-29 18:18:43 +02001075{
1076 int result = 0;
1077 struct usb_gadget *gadget = dev->gadget;
1078
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001079 if (gadget_is_sa1100(gadget)
Remy Bohmerdf063442009-07-29 18:18:43 +02001080 && dev->config
1081 && dev->tx_qlen != 0) {
1082 /* tx fifo is full, but we can't clear it...*/
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001083 error("can't change configurations");
Remy Bohmerdf063442009-07-29 18:18:43 +02001084 return -ESPIPE;
1085 }
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001086 eth_reset_config(dev);
Remy Bohmerdf063442009-07-29 18:18:43 +02001087
1088 switch (number) {
1089 case DEV_CONFIG_VALUE:
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001090 result = set_ether_config(dev, gfp_flags);
Remy Bohmerdf063442009-07-29 18:18:43 +02001091 break;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001092#ifdef CONFIG_USB_ETH_RNDIS
1093 case DEV_RNDIS_CONFIG_VALUE:
1094 dev->rndis = 1;
1095 result = set_ether_config(dev, gfp_flags);
1096 break;
1097#endif
Remy Bohmerdf063442009-07-29 18:18:43 +02001098 default:
1099 result = -EINVAL;
1100 /* FALL THROUGH */
1101 case 0:
1102 break;
1103 }
1104
1105 if (result) {
1106 if (number)
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001107 eth_reset_config(dev);
Remy Bohmerdf063442009-07-29 18:18:43 +02001108 usb_gadget_vbus_draw(dev->gadget,
1109 gadget_is_otg(dev->gadget) ? 8 : 100);
1110 } else {
1111 char *speed;
1112 unsigned power;
1113
1114 power = 2 * eth_config.bMaxPower;
1115 usb_gadget_vbus_draw(dev->gadget, power);
1116
1117 switch (gadget->speed) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001118 case USB_SPEED_FULL:
1119 speed = "full"; break;
Remy Bohmerdf063442009-07-29 18:18:43 +02001120#ifdef CONFIG_USB_GADGET_DUALSPEED
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001121 case USB_SPEED_HIGH:
1122 speed = "high"; break;
Remy Bohmerdf063442009-07-29 18:18:43 +02001123#endif
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001124 default:
1125 speed = "?"; break;
Remy Bohmerdf063442009-07-29 18:18:43 +02001126 }
1127
1128 dev->config = number;
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001129 printf("%s speed config #%d: %d mA, %s, using %s\n",
Remy Bohmerdf063442009-07-29 18:18:43 +02001130 speed, number, power, driver_desc,
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001131 rndis_active(dev)
1132 ? "RNDIS"
1133 : (cdc_active(dev)
1134 ? "CDC Ethernet"
Remy Bohmerdf063442009-07-29 18:18:43 +02001135 : "CDC Ethernet Subset"));
1136 }
1137 return result;
1138}
1139
1140/*-------------------------------------------------------------------------*/
1141
1142#ifdef DEV_CONFIG_CDC
1143
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001144/*
1145 * The interrupt endpoint is used in CDC networking models (Ethernet, ATM)
Remy Bohmerdf063442009-07-29 18:18:43 +02001146 * only to notify the host about link status changes (which we support) or
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001147 * report completion of some encapsulated command (as used in RNDIS). Since
Remy Bohmerdf063442009-07-29 18:18:43 +02001148 * we want this CDC Ethernet code to be vendor-neutral, we don't use that
1149 * command mechanism; and only one status request is ever queued.
1150 */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001151static void eth_status_complete(struct usb_ep *ep, struct usb_request *req)
Remy Bohmerdf063442009-07-29 18:18:43 +02001152{
1153 struct usb_cdc_notification *event = req->buf;
1154 int value = req->status;
1155 struct eth_dev *dev = ep->driver_data;
1156
1157 /* issue the second notification if host reads the first */
1158 if (event->bNotificationType == USB_CDC_NOTIFY_NETWORK_CONNECTION
1159 && value == 0) {
1160 __le32 *data = req->buf + sizeof *event;
1161
1162 event->bmRequestType = 0xA1;
1163 event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001164 event->wValue = __constant_cpu_to_le16(0);
1165 event->wIndex = __constant_cpu_to_le16(1);
1166 event->wLength = __constant_cpu_to_le16(8);
Remy Bohmerdf063442009-07-29 18:18:43 +02001167
1168 /* SPEED_CHANGE data is up/down speeds in bits/sec */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001169 data[0] = data[1] = cpu_to_le32(BITRATE(dev->gadget));
Remy Bohmerdf063442009-07-29 18:18:43 +02001170
1171 req->length = STATUS_BYTECOUNT;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001172 value = usb_ep_queue(ep, req, GFP_ATOMIC);
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001173 debug("send SPEED_CHANGE --> %d\n", value);
Remy Bohmerdf063442009-07-29 18:18:43 +02001174 if (value == 0)
1175 return;
1176 } else if (value != -ECONNRESET) {
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001177 debug("event %02x --> %d\n",
Remy Bohmerdf063442009-07-29 18:18:43 +02001178 event->bNotificationType, value);
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001179 if (event->bNotificationType ==
1180 USB_CDC_NOTIFY_SPEED_CHANGE) {
1181 l_ethdev.network_started = 1;
Remy Bohmerdf063442009-07-29 18:18:43 +02001182 printf("USB network up!\n");
1183 }
1184 }
1185 req->context = NULL;
1186}
1187
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001188static void issue_start_status(struct eth_dev *dev)
Remy Bohmerdf063442009-07-29 18:18:43 +02001189{
1190 struct usb_request *req = dev->stat_req;
1191 struct usb_cdc_notification *event;
1192 int value;
1193
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001194 /*
1195 * flush old status
Remy Bohmerdf063442009-07-29 18:18:43 +02001196 *
1197 * FIXME ugly idiom, maybe we'd be better with just
1198 * a "cancel the whole queue" primitive since any
1199 * unlink-one primitive has way too many error modes.
1200 * here, we "know" toggle is already clear...
1201 *
1202 * FIXME iff req->context != null just dequeue it
1203 */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001204 usb_ep_disable(dev->status_ep);
1205 usb_ep_enable(dev->status_ep, dev->status);
Remy Bohmerdf063442009-07-29 18:18:43 +02001206
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001207 /*
1208 * 3.8.1 says to issue first NETWORK_CONNECTION, then
Remy Bohmerdf063442009-07-29 18:18:43 +02001209 * a SPEED_CHANGE. could be useful in some configs.
1210 */
1211 event = req->buf;
1212 event->bmRequestType = 0xA1;
1213 event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001214 event->wValue = __constant_cpu_to_le16(1); /* connected */
1215 event->wIndex = __constant_cpu_to_le16(1);
Remy Bohmerdf063442009-07-29 18:18:43 +02001216 event->wLength = 0;
1217
1218 req->length = sizeof *event;
1219 req->complete = eth_status_complete;
1220 req->context = dev;
1221
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001222 value = usb_ep_queue(dev->status_ep, req, GFP_ATOMIC);
Remy Bohmerdf063442009-07-29 18:18:43 +02001223 if (value < 0)
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001224 debug("status buf queue --> %d\n", value);
Remy Bohmerdf063442009-07-29 18:18:43 +02001225}
1226
1227#endif
1228
1229/*-------------------------------------------------------------------------*/
1230
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001231static void eth_setup_complete(struct usb_ep *ep, struct usb_request *req)
Remy Bohmerdf063442009-07-29 18:18:43 +02001232{
1233 if (req->status || req->actual != req->length)
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001234 debug("setup complete --> %d, %d/%d\n",
Remy Bohmerdf063442009-07-29 18:18:43 +02001235 req->status, req->actual, req->length);
1236}
1237
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001238#ifdef CONFIG_USB_ETH_RNDIS
1239
1240static void rndis_response_complete(struct usb_ep *ep, struct usb_request *req)
1241{
1242 if (req->status || req->actual != req->length)
1243 debug("rndis response complete --> %d, %d/%d\n",
1244 req->status, req->actual, req->length);
1245
1246 /* done sending after USB_CDC_GET_ENCAPSULATED_RESPONSE */
1247}
1248
1249static void rndis_command_complete(struct usb_ep *ep, struct usb_request *req)
1250{
1251 struct eth_dev *dev = ep->driver_data;
1252 int status;
1253
1254 /* received RNDIS command from USB_CDC_SEND_ENCAPSULATED_COMMAND */
1255 status = rndis_msg_parser(dev->rndis_config, (u8 *) req->buf);
1256 if (status < 0)
1257 error("%s: rndis parse error %d", __func__, status);
1258}
1259
1260#endif /* RNDIS */
1261
Remy Bohmerdf063442009-07-29 18:18:43 +02001262/*
1263 * The setup() callback implements all the ep0 functionality that's not
1264 * handled lower down. CDC has a number of less-common features:
1265 *
1266 * - two interfaces: control, and ethernet data
1267 * - Ethernet data interface has two altsettings: default, and active
1268 * - class-specific descriptors for the control interface
1269 * - class-specific control requests
1270 */
1271static int
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001272eth_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
Remy Bohmerdf063442009-07-29 18:18:43 +02001273{
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001274 struct eth_dev *dev = get_gadget_data(gadget);
Remy Bohmerdf063442009-07-29 18:18:43 +02001275 struct usb_request *req = dev->req;
1276 int value = -EOPNOTSUPP;
1277 u16 wIndex = le16_to_cpu(ctrl->wIndex);
1278 u16 wValue = le16_to_cpu(ctrl->wValue);
1279 u16 wLength = le16_to_cpu(ctrl->wLength);
1280
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001281 /*
1282 * descriptors just go into the pre-allocated ep0 buffer,
Remy Bohmerdf063442009-07-29 18:18:43 +02001283 * while config change events may enable network traffic.
1284 */
1285
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001286 debug("%s\n", __func__);
Remy Bohmerdf063442009-07-29 18:18:43 +02001287
1288 req->complete = eth_setup_complete;
1289 switch (ctrl->bRequest) {
1290
1291 case USB_REQ_GET_DESCRIPTOR:
1292 if (ctrl->bRequestType != USB_DIR_IN)
1293 break;
1294 switch (wValue >> 8) {
1295
1296 case USB_DT_DEVICE:
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001297 value = min(wLength, (u16) sizeof device_desc);
1298 memcpy(req->buf, &device_desc, value);
Remy Bohmerdf063442009-07-29 18:18:43 +02001299 break;
1300 case USB_DT_DEVICE_QUALIFIER:
1301 if (!gadget_is_dualspeed(gadget))
1302 break;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001303 value = min(wLength, (u16) sizeof dev_qualifier);
1304 memcpy(req->buf, &dev_qualifier, value);
Remy Bohmerdf063442009-07-29 18:18:43 +02001305 break;
1306
1307 case USB_DT_OTHER_SPEED_CONFIG:
1308 if (!gadget_is_dualspeed(gadget))
1309 break;
1310 /* FALLTHROUGH */
1311 case USB_DT_CONFIG:
1312 value = config_buf(gadget, req->buf,
1313 wValue >> 8,
1314 wValue & 0xff,
1315 gadget_is_otg(gadget));
1316 if (value >= 0)
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001317 value = min(wLength, (u16) value);
Remy Bohmerdf063442009-07-29 18:18:43 +02001318 break;
1319
1320 case USB_DT_STRING:
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001321 value = usb_gadget_get_string(&stringtab,
Remy Bohmerdf063442009-07-29 18:18:43 +02001322 wValue & 0xff, req->buf);
1323
1324 if (value >= 0)
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001325 value = min(wLength, (u16) value);
Remy Bohmerdf063442009-07-29 18:18:43 +02001326
1327 break;
1328 }
1329 break;
1330
1331 case USB_REQ_SET_CONFIGURATION:
1332 if (ctrl->bRequestType != 0)
1333 break;
1334 if (gadget->a_hnp_support)
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001335 debug("HNP available\n");
Remy Bohmerdf063442009-07-29 18:18:43 +02001336 else if (gadget->a_alt_hnp_support)
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001337 debug("HNP needs a different root port\n");
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001338 value = eth_set_config(dev, wValue, GFP_ATOMIC);
Remy Bohmerdf063442009-07-29 18:18:43 +02001339 break;
1340 case USB_REQ_GET_CONFIGURATION:
1341 if (ctrl->bRequestType != USB_DIR_IN)
1342 break;
1343 *(u8 *)req->buf = dev->config;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001344 value = min(wLength, (u16) 1);
Remy Bohmerdf063442009-07-29 18:18:43 +02001345 break;
1346
1347 case USB_REQ_SET_INTERFACE:
1348 if (ctrl->bRequestType != USB_RECIP_INTERFACE
1349 || !dev->config
1350 || wIndex > 1)
1351 break;
1352 if (!cdc_active(dev) && wIndex != 0)
1353 break;
1354
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001355 /*
1356 * PXA hardware partially handles SET_INTERFACE;
Remy Bohmerdf063442009-07-29 18:18:43 +02001357 * we need to kluge around that interference.
1358 */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001359 if (gadget_is_pxa(gadget)) {
1360 value = eth_set_config(dev, DEV_CONFIG_VALUE,
Remy Bohmerdf063442009-07-29 18:18:43 +02001361 GFP_ATOMIC);
Lukasz Dalek6ca42dd2012-10-02 17:04:29 +02001362 /*
1363 * PXA25x driver use non-CDC ethernet gadget.
1364 * But only _CDC and _RNDIS code can signalize
1365 * that network is working. So we signalize it
1366 * here.
1367 */
1368 l_ethdev.network_started = 1;
1369 debug("USB network up!\n");
Remy Bohmerdf063442009-07-29 18:18:43 +02001370 goto done_set_intf;
1371 }
1372
1373#ifdef DEV_CONFIG_CDC
1374 switch (wIndex) {
1375 case 0: /* control/master intf */
1376 if (wValue != 0)
1377 break;
1378 if (dev->status) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001379 usb_ep_disable(dev->status_ep);
1380 usb_ep_enable(dev->status_ep, dev->status);
Remy Bohmerdf063442009-07-29 18:18:43 +02001381 }
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001382
Remy Bohmerdf063442009-07-29 18:18:43 +02001383 value = 0;
1384 break;
1385 case 1: /* data intf */
1386 if (wValue > 1)
1387 break;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001388 usb_ep_disable(dev->in_ep);
1389 usb_ep_disable(dev->out_ep);
Remy Bohmerdf063442009-07-29 18:18:43 +02001390
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001391 /*
1392 * CDC requires the data transfers not be done from
Remy Bohmerdf063442009-07-29 18:18:43 +02001393 * the default interface setting ... also, setting
1394 * the non-default interface resets filters etc.
1395 */
1396 if (wValue == 1) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001397 if (!cdc_active(dev))
Remy Bohmerdf063442009-07-29 18:18:43 +02001398 break;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001399 usb_ep_enable(dev->in_ep, dev->in);
1400 usb_ep_enable(dev->out_ep, dev->out);
Remy Bohmerdf063442009-07-29 18:18:43 +02001401 dev->cdc_filter = DEFAULT_FILTER;
1402 if (dev->status)
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001403 issue_start_status(dev);
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001404 eth_start(dev, GFP_ATOMIC);
Remy Bohmerdf063442009-07-29 18:18:43 +02001405 }
Remy Bohmerdf063442009-07-29 18:18:43 +02001406 value = 0;
1407 break;
1408 }
1409#else
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001410 /*
1411 * FIXME this is wrong, as is the assumption that
Remy Bohmerdf063442009-07-29 18:18:43 +02001412 * all non-PXA hardware talks real CDC ...
1413 */
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001414 debug("set_interface ignored!\n");
Remy Bohmerdf063442009-07-29 18:18:43 +02001415#endif /* DEV_CONFIG_CDC */
1416
1417done_set_intf:
1418 break;
1419 case USB_REQ_GET_INTERFACE:
1420 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE)
1421 || !dev->config
1422 || wIndex > 1)
1423 break;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001424 if (!(cdc_active(dev) || rndis_active(dev)) && wIndex != 0)
Remy Bohmerdf063442009-07-29 18:18:43 +02001425 break;
1426
1427 /* for CDC, iff carrier is on, data interface is active. */
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001428 if (rndis_active(dev) || wIndex != 1)
Remy Bohmerdf063442009-07-29 18:18:43 +02001429 *(u8 *)req->buf = 0;
1430 else {
1431 /* *(u8 *)req->buf = netif_carrier_ok (dev->net) ? 1 : 0; */
1432 /* carrier always ok ...*/
1433 *(u8 *)req->buf = 1 ;
1434 }
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001435 value = min(wLength, (u16) 1);
Remy Bohmerdf063442009-07-29 18:18:43 +02001436 break;
1437
1438#ifdef DEV_CONFIG_CDC
1439 case USB_CDC_SET_ETHERNET_PACKET_FILTER:
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001440 /*
1441 * see 6.2.30: no data, wIndex = interface,
Remy Bohmerdf063442009-07-29 18:18:43 +02001442 * wValue = packet filter bitmap
1443 */
1444 if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1445 || !cdc_active(dev)
1446 || wLength != 0
1447 || wIndex > 1)
1448 break;
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001449 debug("packet filter %02x\n", wValue);
Remy Bohmerdf063442009-07-29 18:18:43 +02001450 dev->cdc_filter = wValue;
1451 value = 0;
1452 break;
1453
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001454 /*
1455 * and potentially:
Remy Bohmerdf063442009-07-29 18:18:43 +02001456 * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS:
1457 * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER:
1458 * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER:
1459 * case USB_CDC_GET_ETHERNET_STATISTIC:
1460 */
1461
1462#endif /* DEV_CONFIG_CDC */
1463
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001464#ifdef CONFIG_USB_ETH_RNDIS
1465 /*
1466 * RNDIS uses the CDC command encapsulation mechanism to implement
1467 * an RPC scheme, with much getting/setting of attributes by OID.
1468 */
1469 case USB_CDC_SEND_ENCAPSULATED_COMMAND:
1470 if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1471 || !rndis_active(dev)
1472 || wLength > USB_BUFSIZ
1473 || wValue
1474 || rndis_control_intf.bInterfaceNumber
1475 != wIndex)
1476 break;
1477 /* read the request, then process it */
1478 value = wLength;
1479 req->complete = rndis_command_complete;
1480 /* later, rndis_control_ack () sends a notification */
1481 break;
1482
1483 case USB_CDC_GET_ENCAPSULATED_RESPONSE:
1484 if ((USB_DIR_IN|USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1485 == ctrl->bRequestType
1486 && rndis_active(dev)
1487 /* && wLength >= 0x0400 */
1488 && !wValue
1489 && rndis_control_intf.bInterfaceNumber
1490 == wIndex) {
1491 u8 *buf;
1492 u32 n;
1493
1494 /* return the result */
1495 buf = rndis_get_next_response(dev->rndis_config, &n);
1496 if (buf) {
1497 memcpy(req->buf, buf, n);
1498 req->complete = rndis_response_complete;
1499 rndis_free_response(dev->rndis_config, buf);
1500 value = n;
1501 }
1502 /* else stalls ... spec says to avoid that */
1503 }
1504 break;
1505#endif /* RNDIS */
1506
Remy Bohmerdf063442009-07-29 18:18:43 +02001507 default:
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001508 debug("unknown control req%02x.%02x v%04x i%04x l%d\n",
Remy Bohmerdf063442009-07-29 18:18:43 +02001509 ctrl->bRequestType, ctrl->bRequest,
1510 wValue, wIndex, wLength);
1511 }
1512
1513 /* respond with data transfer before status phase? */
1514 if (value >= 0) {
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001515 debug("respond with data transfer before status phase\n");
Remy Bohmerdf063442009-07-29 18:18:43 +02001516 req->length = value;
1517 req->zero = value < wLength
1518 && (value % gadget->ep0->maxpacket) == 0;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001519 value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
Remy Bohmerdf063442009-07-29 18:18:43 +02001520 if (value < 0) {
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001521 debug("ep_queue --> %d\n", value);
Remy Bohmerdf063442009-07-29 18:18:43 +02001522 req->status = 0;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001523 eth_setup_complete(gadget->ep0, req);
Remy Bohmerdf063442009-07-29 18:18:43 +02001524 }
1525 }
1526
1527 /* host either stalls (value < 0) or reports success */
1528 return value;
1529}
1530
Remy Bohmerdf063442009-07-29 18:18:43 +02001531/*-------------------------------------------------------------------------*/
1532
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001533static void rx_complete(struct usb_ep *ep, struct usb_request *req);
Remy Bohmerdf063442009-07-29 18:18:43 +02001534
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001535static int rx_submit(struct eth_dev *dev, struct usb_request *req,
Remy Bohmerdf063442009-07-29 18:18:43 +02001536 gfp_t gfp_flags)
1537{
1538 int retval = -ENOMEM;
1539 size_t size;
1540
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001541 /*
1542 * Padding up to RX_EXTRA handles minor disagreements with host.
Remy Bohmerdf063442009-07-29 18:18:43 +02001543 * Normally we use the USB "terminate on short read" convention;
1544 * so allow up to (N*maxpacket), since that memory is normally
1545 * already allocated. Some hardware doesn't deal well with short
1546 * reads (e.g. DMA must be N*maxpacket), so for now don't trim a
1547 * byte off the end (to force hardware errors on overflow).
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001548 *
1549 * RNDIS uses internal framing, and explicitly allows senders to
1550 * pad to end-of-packet. That's potentially nice for speed,
1551 * but means receivers can't recover synch on their own.
Remy Bohmerdf063442009-07-29 18:18:43 +02001552 */
1553
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001554 debug("%s\n", __func__);
Remy Bohmerdf063442009-07-29 18:18:43 +02001555
1556 size = (ETHER_HDR_SIZE + dev->mtu + RX_EXTRA);
1557 size += dev->out_ep->maxpacket - 1;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001558 if (rndis_active(dev))
1559 size += sizeof(struct rndis_packet_msg_type);
Remy Bohmerdf063442009-07-29 18:18:43 +02001560 size -= size % dev->out_ep->maxpacket;
1561
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001562 /*
1563 * Some platforms perform better when IP packets are aligned,
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001564 * but on at least one, checksumming fails otherwise. Note:
1565 * RNDIS headers involve variable numbers of LE32 values.
Remy Bohmerdf063442009-07-29 18:18:43 +02001566 */
1567
1568 req->buf = (u8 *) NetRxPackets[0];
1569 req->length = size;
1570 req->complete = rx_complete;
1571
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001572 retval = usb_ep_queue(dev->out_ep, req, gfp_flags);
Remy Bohmerdf063442009-07-29 18:18:43 +02001573
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001574 if (retval)
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001575 error("rx submit --> %d", retval);
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001576
Remy Bohmerdf063442009-07-29 18:18:43 +02001577 return retval;
1578}
1579
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001580static void rx_complete(struct usb_ep *ep, struct usb_request *req)
Remy Bohmerdf063442009-07-29 18:18:43 +02001581{
1582 struct eth_dev *dev = ep->driver_data;
1583
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001584 debug("%s: status %d\n", __func__, req->status);
Vitaly Kuzmichev24fcb472011-02-11 18:18:32 +03001585 switch (req->status) {
1586 /* normal completion */
1587 case 0:
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001588 if (rndis_active(dev)) {
1589 /* we know MaxPacketsPerTransfer == 1 here */
1590 int length = rndis_rm_hdr(req->buf, req->actual);
1591 if (length < 0)
1592 goto length_err;
1593 req->length -= length;
1594 req->actual -= length;
1595 }
1596 if (req->actual < ETH_HLEN || ETH_FRAME_LEN < req->actual) {
1597length_err:
1598 dev->stats.rx_errors++;
1599 dev->stats.rx_length_errors++;
1600 debug("rx length %d\n", req->length);
1601 break;
1602 }
1603
Vitaly Kuzmichev24fcb472011-02-11 18:18:32 +03001604 dev->stats.rx_packets++;
1605 dev->stats.rx_bytes += req->length;
1606 break;
1607
1608 /* software-driven interface shutdown */
1609 case -ECONNRESET: /* unlink */
1610 case -ESHUTDOWN: /* disconnect etc */
1611 /* for hardware automagic (such as pxa) */
1612 case -ECONNABORTED: /* endpoint reset */
1613 break;
1614
1615 /* data overrun */
1616 case -EOVERFLOW:
1617 dev->stats.rx_over_errors++;
1618 /* FALLTHROUGH */
1619 default:
1620 dev->stats.rx_errors++;
1621 break;
1622 }
Remy Bohmerdf063442009-07-29 18:18:43 +02001623
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001624 packet_received = 1;
Remy Bohmerdf063442009-07-29 18:18:43 +02001625}
1626
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001627static int alloc_requests(struct eth_dev *dev, unsigned n, gfp_t gfp_flags)
Remy Bohmerdf063442009-07-29 18:18:43 +02001628{
1629
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001630 dev->tx_req = usb_ep_alloc_request(dev->in_ep, 0);
Remy Bohmerdf063442009-07-29 18:18:43 +02001631
1632 if (!dev->tx_req)
Vitaly Kuzmichev2907b2c2010-09-22 13:13:55 +04001633 goto fail1;
Remy Bohmerdf063442009-07-29 18:18:43 +02001634
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001635 dev->rx_req = usb_ep_alloc_request(dev->out_ep, 0);
Remy Bohmerdf063442009-07-29 18:18:43 +02001636
1637 if (!dev->rx_req)
Vitaly Kuzmichev2907b2c2010-09-22 13:13:55 +04001638 goto fail2;
Remy Bohmerdf063442009-07-29 18:18:43 +02001639
1640 return 0;
1641
Vitaly Kuzmichev2907b2c2010-09-22 13:13:55 +04001642fail2:
1643 usb_ep_free_request(dev->in_ep, dev->tx_req);
1644fail1:
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001645 error("can't alloc requests");
Remy Bohmerdf063442009-07-29 18:18:43 +02001646 return -1;
1647}
1648
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001649static void tx_complete(struct usb_ep *ep, struct usb_request *req)
Remy Bohmerdf063442009-07-29 18:18:43 +02001650{
Vitaly Kuzmichev24fcb472011-02-11 18:18:32 +03001651 struct eth_dev *dev = ep->driver_data;
1652
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001653 debug("%s: status %s\n", __func__, (req->status) ? "failed" : "ok");
Vitaly Kuzmichev24fcb472011-02-11 18:18:32 +03001654 switch (req->status) {
1655 default:
1656 dev->stats.tx_errors++;
1657 debug("tx err %d\n", req->status);
1658 /* FALLTHROUGH */
1659 case -ECONNRESET: /* unlink */
1660 case -ESHUTDOWN: /* disconnect etc */
1661 break;
1662 case 0:
1663 dev->stats.tx_bytes += req->length;
1664 }
1665 dev->stats.tx_packets++;
1666
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001667 packet_sent = 1;
Remy Bohmerdf063442009-07-29 18:18:43 +02001668}
1669
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001670static inline int eth_is_promisc(struct eth_dev *dev)
Remy Bohmerdf063442009-07-29 18:18:43 +02001671{
1672 /* no filters for the CDC subset; always promisc */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001673 if (subset_active(dev))
Remy Bohmerdf063442009-07-29 18:18:43 +02001674 return 1;
1675 return dev->cdc_filter & USB_CDC_PACKET_TYPE_PROMISCUOUS;
1676}
1677
1678#if 0
1679static int eth_start_xmit (struct sk_buff *skb, struct net_device *net)
1680{
1681 struct eth_dev *dev = netdev_priv(net);
1682 int length = skb->len;
1683 int retval;
1684 struct usb_request *req = NULL;
1685 unsigned long flags;
1686
1687 /* apply outgoing CDC or RNDIS filters */
1688 if (!eth_is_promisc (dev)) {
1689 u8 *dest = skb->data;
1690
1691 if (is_multicast_ether_addr(dest)) {
1692 u16 type;
1693
1694 /* ignores USB_CDC_PACKET_TYPE_MULTICAST and host
1695 * SET_ETHERNET_MULTICAST_FILTERS requests
1696 */
1697 if (is_broadcast_ether_addr(dest))
1698 type = USB_CDC_PACKET_TYPE_BROADCAST;
1699 else
1700 type = USB_CDC_PACKET_TYPE_ALL_MULTICAST;
1701 if (!(dev->cdc_filter & type)) {
1702 dev_kfree_skb_any (skb);
1703 return 0;
1704 }
1705 }
1706 /* ignores USB_CDC_PACKET_TYPE_DIRECTED */
1707 }
1708
1709 spin_lock_irqsave(&dev->req_lock, flags);
1710 /*
1711 * this freelist can be empty if an interrupt triggered disconnect()
1712 * and reconfigured the gadget (shutting down this queue) after the
1713 * network stack decided to xmit but before we got the spinlock.
1714 */
1715 if (list_empty(&dev->tx_reqs)) {
1716 spin_unlock_irqrestore(&dev->req_lock, flags);
1717 return 1;
1718 }
1719
1720 req = container_of (dev->tx_reqs.next, struct usb_request, list);
1721 list_del (&req->list);
1722
1723 /* temporarily stop TX queue when the freelist empties */
1724 if (list_empty (&dev->tx_reqs))
1725 netif_stop_queue (net);
1726 spin_unlock_irqrestore(&dev->req_lock, flags);
1727
1728 /* no buffer copies needed, unless the network stack did it
1729 * or the hardware can't use skb buffers.
1730 * or there's not enough space for any RNDIS headers we need
1731 */
1732 if (rndis_active(dev)) {
1733 struct sk_buff *skb_rndis;
1734
1735 skb_rndis = skb_realloc_headroom (skb,
1736 sizeof (struct rndis_packet_msg_type));
1737 if (!skb_rndis)
1738 goto drop;
1739
1740 dev_kfree_skb_any (skb);
1741 skb = skb_rndis;
1742 rndis_add_hdr (skb);
1743 length = skb->len;
1744 }
1745 req->buf = skb->data;
1746 req->context = skb;
1747 req->complete = tx_complete;
1748
1749 /* use zlp framing on tx for strict CDC-Ether conformance,
1750 * though any robust network rx path ignores extra padding.
1751 * and some hardware doesn't like to write zlps.
1752 */
1753 req->zero = 1;
1754 if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0)
1755 length++;
1756
1757 req->length = length;
1758
1759 /* throttle highspeed IRQ rate back slightly */
1760 if (gadget_is_dualspeed(dev->gadget))
1761 req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH)
1762 ? ((atomic_read(&dev->tx_qlen) % qmult) != 0)
1763 : 0;
1764
1765 retval = usb_ep_queue (dev->in_ep, req, GFP_ATOMIC);
1766 switch (retval) {
1767 default:
1768 DEBUG (dev, "tx queue err %d\n", retval);
1769 break;
1770 case 0:
1771 net->trans_start = jiffies;
1772 atomic_inc (&dev->tx_qlen);
1773 }
1774
1775 if (retval) {
1776drop:
1777 dev->stats.tx_dropped++;
1778 dev_kfree_skb_any (skb);
1779 spin_lock_irqsave(&dev->req_lock, flags);
1780 if (list_empty (&dev->tx_reqs))
1781 netif_start_queue (net);
1782 list_add (&req->list, &dev->tx_reqs);
1783 spin_unlock_irqrestore(&dev->req_lock, flags);
1784 }
1785 return 0;
1786}
1787
1788/*-------------------------------------------------------------------------*/
1789#endif
1790
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001791static void eth_unbind(struct usb_gadget *gadget)
Remy Bohmerdf063442009-07-29 18:18:43 +02001792{
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001793 struct eth_dev *dev = get_gadget_data(gadget);
Remy Bohmerdf063442009-07-29 18:18:43 +02001794
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001795 debug("%s...\n", __func__);
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001796 rndis_deregister(dev->rndis_config);
1797 rndis_exit();
Remy Bohmerdf063442009-07-29 18:18:43 +02001798
Vitaly Kuzmichev90670702010-08-13 17:00:16 +04001799 /* we've already been disconnected ... no i/o is active */
1800 if (dev->req) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001801 usb_ep_free_request(gadget->ep0, dev->req);
Vitaly Kuzmichev90670702010-08-13 17:00:16 +04001802 dev->req = NULL;
1803 }
Remy Bohmerdf063442009-07-29 18:18:43 +02001804 if (dev->stat_req) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001805 usb_ep_free_request(dev->status_ep, dev->stat_req);
Remy Bohmerdf063442009-07-29 18:18:43 +02001806 dev->stat_req = NULL;
1807 }
1808
1809 if (dev->tx_req) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001810 usb_ep_free_request(dev->in_ep, dev->tx_req);
1811 dev->tx_req = NULL;
Remy Bohmerdf063442009-07-29 18:18:43 +02001812 }
1813
1814 if (dev->rx_req) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001815 usb_ep_free_request(dev->out_ep, dev->rx_req);
1816 dev->rx_req = NULL;
Remy Bohmerdf063442009-07-29 18:18:43 +02001817 }
1818
1819/* unregister_netdev (dev->net);*/
1820/* free_netdev(dev->net);*/
1821
Lei Wen4af32732010-12-01 23:43:43 +08001822 dev->gadget = NULL;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001823 set_gadget_data(gadget, NULL);
Remy Bohmerdf063442009-07-29 18:18:43 +02001824}
1825
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001826static void eth_disconnect(struct usb_gadget *gadget)
Remy Bohmerdf063442009-07-29 18:18:43 +02001827{
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001828 eth_reset_config(get_gadget_data(gadget));
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001829 /* FIXME RNDIS should enter RNDIS_UNINITIALIZED */
Remy Bohmerdf063442009-07-29 18:18:43 +02001830}
1831
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001832static void eth_suspend(struct usb_gadget *gadget)
Remy Bohmerdf063442009-07-29 18:18:43 +02001833{
1834 /* Not used */
1835}
1836
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001837static void eth_resume(struct usb_gadget *gadget)
Remy Bohmerdf063442009-07-29 18:18:43 +02001838{
1839 /* Not used */
1840}
1841
1842/*-------------------------------------------------------------------------*/
1843
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001844#ifdef CONFIG_USB_ETH_RNDIS
1845
1846/*
1847 * The interrupt endpoint is used in RNDIS to notify the host when messages
1848 * other than data packets are available ... notably the REMOTE_NDIS_*_CMPLT
1849 * messages, but also REMOTE_NDIS_INDICATE_STATUS_MSG and potentially even
1850 * REMOTE_NDIS_KEEPALIVE_MSG.
1851 *
1852 * The RNDIS control queue is processed by GET_ENCAPSULATED_RESPONSE, and
1853 * normally just one notification will be queued.
1854 */
1855
1856static void rndis_control_ack_complete(struct usb_ep *ep,
1857 struct usb_request *req)
1858{
1859 struct eth_dev *dev = ep->driver_data;
1860
1861 debug("%s...\n", __func__);
1862 if (req->status || req->actual != req->length)
1863 debug("rndis control ack complete --> %d, %d/%d\n",
1864 req->status, req->actual, req->length);
1865
1866 if (!l_ethdev.network_started) {
1867 if (rndis_get_state(dev->rndis_config)
1868 == RNDIS_DATA_INITIALIZED) {
1869 l_ethdev.network_started = 1;
1870 printf("USB RNDIS network up!\n");
1871 }
1872 }
1873
1874 req->context = NULL;
1875
1876 if (req != dev->stat_req)
1877 usb_ep_free_request(ep, req);
1878}
1879
1880static char rndis_resp_buf[8] __attribute__((aligned(sizeof(__le32))));
1881
1882static int rndis_control_ack(struct eth_device *net)
1883{
1884 struct eth_dev *dev = &l_ethdev;
1885 int length;
1886 struct usb_request *resp = dev->stat_req;
1887
1888 /* in case RNDIS calls this after disconnect */
1889 if (!dev->status) {
1890 debug("status ENODEV\n");
1891 return -ENODEV;
1892 }
1893
1894 /* in case queue length > 1 */
1895 if (resp->context) {
1896 resp = usb_ep_alloc_request(dev->status_ep, GFP_ATOMIC);
1897 if (!resp)
1898 return -ENOMEM;
1899 resp->buf = rndis_resp_buf;
1900 }
1901
1902 /*
1903 * Send RNDIS RESPONSE_AVAILABLE notification;
1904 * USB_CDC_NOTIFY_RESPONSE_AVAILABLE should work too
1905 */
1906 resp->length = 8;
1907 resp->complete = rndis_control_ack_complete;
1908 resp->context = dev;
1909
1910 *((__le32 *) resp->buf) = __constant_cpu_to_le32(1);
1911 *((__le32 *) (resp->buf + 4)) = __constant_cpu_to_le32(0);
1912
1913 length = usb_ep_queue(dev->status_ep, resp, GFP_ATOMIC);
1914 if (length < 0) {
1915 resp->status = 0;
1916 rndis_control_ack_complete(dev->status_ep, resp);
1917 }
1918
1919 return 0;
1920}
1921
1922#else
1923
1924#define rndis_control_ack NULL
1925
1926#endif /* RNDIS */
1927
1928static void eth_start(struct eth_dev *dev, gfp_t gfp_flags)
1929{
1930 if (rndis_active(dev)) {
1931 rndis_set_param_medium(dev->rndis_config,
1932 NDIS_MEDIUM_802_3,
1933 BITRATE(dev->gadget)/100);
1934 rndis_signal_connect(dev->rndis_config);
1935 }
1936}
1937
1938static int eth_stop(struct eth_dev *dev)
1939{
Vitaly Kuzmichev3433c332011-02-11 18:18:35 +03001940#ifdef RNDIS_COMPLETE_SIGNAL_DISCONNECT
1941 unsigned long ts;
1942 unsigned long timeout = CONFIG_SYS_HZ; /* 1 sec to stop RNDIS */
1943#endif
1944
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001945 if (rndis_active(dev)) {
1946 rndis_set_param_medium(dev->rndis_config, NDIS_MEDIUM_802_3, 0);
1947 rndis_signal_disconnect(dev->rndis_config);
1948
Vitaly Kuzmichev3433c332011-02-11 18:18:35 +03001949#ifdef RNDIS_COMPLETE_SIGNAL_DISCONNECT
1950 /* Wait until host receives OID_GEN_MEDIA_CONNECT_STATUS */
1951 ts = get_timer(0);
1952 while (get_timer(ts) < timeout)
1953 usb_gadget_handle_interrupts();
1954#endif
1955
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001956 rndis_uninit(dev->rndis_config);
1957 dev->rndis = 0;
1958 }
1959
1960 return 0;
1961}
1962
1963/*-------------------------------------------------------------------------*/
1964
Remy Bohmerdf063442009-07-29 18:18:43 +02001965static int is_eth_addr_valid(char *str)
1966{
1967 if (strlen(str) == 17) {
1968 int i;
1969 char *p, *q;
1970 uchar ea[6];
1971
1972 /* see if it looks like an ethernet address */
1973
1974 p = str;
1975
1976 for (i = 0; i < 6; i++) {
1977 char term = (i == 5 ? '\0' : ':');
1978
1979 ea[i] = simple_strtol(p, &q, 16);
1980
1981 if ((q - p) != 2 || *q++ != term)
1982 break;
1983
1984 p = q;
1985 }
1986
1987 if (i == 6) /* it looks ok */
1988 return 1;
1989 }
1990 return 0;
1991}
1992
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001993static u8 nibble(unsigned char c)
Remy Bohmerdf063442009-07-29 18:18:43 +02001994{
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001995 if (likely(isdigit(c)))
Remy Bohmerdf063442009-07-29 18:18:43 +02001996 return c - '0';
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001997 c = toupper(c);
1998 if (likely(isxdigit(c)))
Remy Bohmerdf063442009-07-29 18:18:43 +02001999 return 10 + c - 'A';
2000 return 0;
2001}
2002
2003static int get_ether_addr(const char *str, u8 *dev_addr)
2004{
2005 if (str) {
2006 unsigned i;
2007
2008 for (i = 0; i < 6; i++) {
2009 unsigned char num;
2010
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002011 if ((*str == '.') || (*str == ':'))
Remy Bohmerdf063442009-07-29 18:18:43 +02002012 str++;
2013 num = nibble(*str++) << 4;
2014 num |= (nibble(*str++));
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002015 dev_addr[i] = num;
Remy Bohmerdf063442009-07-29 18:18:43 +02002016 }
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002017 if (is_valid_ether_addr(dev_addr))
Remy Bohmerdf063442009-07-29 18:18:43 +02002018 return 0;
2019 }
2020 return 1;
2021}
2022
2023static int eth_bind(struct usb_gadget *gadget)
2024{
2025 struct eth_dev *dev = &l_ethdev;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002026 u8 cdc = 1, zlp = 1, rndis = 1;
Remy Bohmerdf063442009-07-29 18:18:43 +02002027 struct usb_ep *in_ep, *out_ep, *status_ep = NULL;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002028 int status = -ENOMEM;
Remy Bohmerdf063442009-07-29 18:18:43 +02002029 int gcnum;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002030 u8 tmp[7];
Remy Bohmerdf063442009-07-29 18:18:43 +02002031
2032 /* these flags are only ever cleared; compiler take note */
2033#ifndef DEV_CONFIG_CDC
2034 cdc = 0;
2035#endif
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002036#ifndef CONFIG_USB_ETH_RNDIS
2037 rndis = 0;
2038#endif
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002039 /*
2040 * Because most host side USB stacks handle CDC Ethernet, that
Remy Bohmerdf063442009-07-29 18:18:43 +02002041 * standard protocol is _strongly_ preferred for interop purposes.
2042 * (By everyone except Microsoft.)
2043 */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002044 if (gadget_is_pxa(gadget)) {
Remy Bohmerdf063442009-07-29 18:18:43 +02002045 /* pxa doesn't support altsettings */
2046 cdc = 0;
2047 } else if (gadget_is_musbhdrc(gadget)) {
2048 /* reduce tx dma overhead by avoiding special cases */
2049 zlp = 0;
2050 } else if (gadget_is_sh(gadget)) {
2051 /* sh doesn't support multiple interfaces or configs */
2052 cdc = 0;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002053 rndis = 0;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002054 } else if (gadget_is_sa1100(gadget)) {
Remy Bohmerdf063442009-07-29 18:18:43 +02002055 /* hardware can't write zlps */
2056 zlp = 0;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002057 /*
2058 * sa1100 CAN do CDC, without status endpoint ... we use
Remy Bohmerdf063442009-07-29 18:18:43 +02002059 * non-CDC to be compatible with ARM Linux-2.4 "usb-eth".
2060 */
2061 cdc = 0;
2062 }
2063
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002064 gcnum = usb_gadget_controller_number(gadget);
Remy Bohmerdf063442009-07-29 18:18:43 +02002065 if (gcnum >= 0)
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002066 device_desc.bcdDevice = cpu_to_le16(0x0300 + gcnum);
Remy Bohmerdf063442009-07-29 18:18:43 +02002067 else {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002068 /*
2069 * can't assume CDC works. don't want to default to
Remy Bohmerdf063442009-07-29 18:18:43 +02002070 * anything less functional on CDC-capable hardware,
2071 * so we fail in this case.
2072 */
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04002073 error("controller '%s' not recognized",
Remy Bohmerdf063442009-07-29 18:18:43 +02002074 gadget->name);
2075 return -ENODEV;
2076 }
2077
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002078 /*
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002079 * If there's an RNDIS configuration, that's what Windows wants to
2080 * be using ... so use these product IDs here and in the "linux.inf"
2081 * needed to install MSFT drivers. Current Linux kernels will use
2082 * the second configuration if it's CDC Ethernet, and need some help
2083 * to choose the right configuration otherwise.
Remy Bohmerdf063442009-07-29 18:18:43 +02002084 */
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002085 if (rndis) {
2086#if defined(CONFIG_USB_RNDIS_VENDOR_ID) && defined(CONFIG_USB_RNDIS_PRODUCT_ID)
Remy Bohmerdf063442009-07-29 18:18:43 +02002087 device_desc.idVendor =
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002088 __constant_cpu_to_le16(CONFIG_USB_RNDIS_VENDOR_ID);
Remy Bohmerdf063442009-07-29 18:18:43 +02002089 device_desc.idProduct =
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002090 __constant_cpu_to_le16(CONFIG_USB_RNDIS_PRODUCT_ID);
2091#else
2092 device_desc.idVendor =
2093 __constant_cpu_to_le16(RNDIS_VENDOR_NUM);
2094 device_desc.idProduct =
2095 __constant_cpu_to_le16(RNDIS_PRODUCT_NUM);
2096#endif
2097 sprintf(product_desc, "RNDIS/%s", driver_desc);
Remy Bohmerdf063442009-07-29 18:18:43 +02002098
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002099 /*
2100 * CDC subset ... recognized by Linux since 2.4.10, but Windows
2101 * drivers aren't widely available. (That may be improved by
2102 * supporting one submode of the "SAFE" variant of MDLM.)
2103 */
2104 } else {
Remy Bohmerdf063442009-07-29 18:18:43 +02002105#if defined(CONFIG_USB_CDC_VENDOR_ID) && defined(CONFIG_USB_CDC_PRODUCT_ID)
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002106 device_desc.idVendor = cpu_to_le16(CONFIG_USB_CDC_VENDOR_ID);
2107 device_desc.idProduct = cpu_to_le16(CONFIG_USB_CDC_PRODUCT_ID);
2108#else
2109 if (!cdc) {
2110 device_desc.idVendor =
2111 __constant_cpu_to_le16(SIMPLE_VENDOR_NUM);
2112 device_desc.idProduct =
2113 __constant_cpu_to_le16(SIMPLE_PRODUCT_NUM);
2114 }
Remy Bohmerdf063442009-07-29 18:18:43 +02002115#endif
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002116 }
2117 /* support optional vendor/distro customization */
Remy Bohmerdf063442009-07-29 18:18:43 +02002118 if (bcdDevice)
2119 device_desc.bcdDevice = cpu_to_le16(bcdDevice);
2120 if (iManufacturer)
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002121 strlcpy(manufacturer, iManufacturer, sizeof manufacturer);
Remy Bohmerdf063442009-07-29 18:18:43 +02002122 if (iProduct)
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002123 strlcpy(product_desc, iProduct, sizeof product_desc);
Remy Bohmerdf063442009-07-29 18:18:43 +02002124 if (iSerialNumber) {
2125 device_desc.iSerialNumber = STRING_SERIALNUMBER,
Vitaly Kuzmichev214d7b02010-08-13 17:00:45 +04002126 strlcpy(serial_number, iSerialNumber, sizeof serial_number);
Remy Bohmerdf063442009-07-29 18:18:43 +02002127 }
2128
2129 /* all we really need is bulk IN/OUT */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002130 usb_ep_autoconfig_reset(gadget);
2131 in_ep = usb_ep_autoconfig(gadget, &fs_source_desc);
Remy Bohmerdf063442009-07-29 18:18:43 +02002132 if (!in_ep) {
2133autoconf_fail:
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04002134 error("can't autoconfigure on %s\n",
Remy Bohmerdf063442009-07-29 18:18:43 +02002135 gadget->name);
2136 return -ENODEV;
2137 }
2138 in_ep->driver_data = in_ep; /* claim */
2139
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002140 out_ep = usb_ep_autoconfig(gadget, &fs_sink_desc);
Remy Bohmerdf063442009-07-29 18:18:43 +02002141 if (!out_ep)
2142 goto autoconf_fail;
2143 out_ep->driver_data = out_ep; /* claim */
2144
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002145#if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002146 /*
2147 * CDC Ethernet control interface doesn't require a status endpoint.
Remy Bohmerdf063442009-07-29 18:18:43 +02002148 * Since some hosts expect one, try to allocate one anyway.
2149 */
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002150 if (cdc || rndis) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002151 status_ep = usb_ep_autoconfig(gadget, &fs_status_desc);
Remy Bohmerdf063442009-07-29 18:18:43 +02002152 if (status_ep) {
2153 status_ep->driver_data = status_ep; /* claim */
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002154 } else if (rndis) {
2155 error("can't run RNDIS on %s", gadget->name);
2156 return -ENODEV;
2157#ifdef DEV_CONFIG_CDC
Remy Bohmerdf063442009-07-29 18:18:43 +02002158 } else if (cdc) {
2159 control_intf.bNumEndpoints = 0;
2160 /* FIXME remove endpoint from descriptor list */
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002161#endif
Remy Bohmerdf063442009-07-29 18:18:43 +02002162 }
2163 }
2164#endif
2165
2166 /* one config: cdc, else minimal subset */
2167 if (!cdc) {
2168 eth_config.bNumInterfaces = 1;
2169 eth_config.iConfiguration = STRING_SUBSET;
2170
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002171 /*
2172 * use functions to set these up, in case we're built to work
Remy Bohmerdf063442009-07-29 18:18:43 +02002173 * with multiple controllers and must override CDC Ethernet.
2174 */
2175 fs_subset_descriptors();
2176 hs_subset_descriptors();
2177 }
2178
2179 device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002180 usb_gadget_set_selfpowered(gadget);
Remy Bohmerdf063442009-07-29 18:18:43 +02002181
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002182 /* For now RNDIS is always a second config */
2183 if (rndis)
2184 device_desc.bNumConfigurations = 2;
2185
Remy Bohmerdf063442009-07-29 18:18:43 +02002186 if (gadget_is_dualspeed(gadget)) {
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002187 if (rndis)
2188 dev_qualifier.bNumConfigurations = 2;
2189 else if (!cdc)
Remy Bohmerdf063442009-07-29 18:18:43 +02002190 dev_qualifier.bDeviceClass = USB_CLASS_VENDOR_SPEC;
2191
2192 /* assumes ep0 uses the same value for both speeds ... */
2193 dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0;
2194
2195 /* and that all endpoints are dual-speed */
2196 hs_source_desc.bEndpointAddress =
2197 fs_source_desc.bEndpointAddress;
2198 hs_sink_desc.bEndpointAddress =
2199 fs_sink_desc.bEndpointAddress;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002200#if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Remy Bohmerdf063442009-07-29 18:18:43 +02002201 if (status_ep)
2202 hs_status_desc.bEndpointAddress =
2203 fs_status_desc.bEndpointAddress;
2204#endif
2205 }
2206
2207 if (gadget_is_otg(gadget)) {
2208 otg_descriptor.bmAttributes |= USB_OTG_HNP,
2209 eth_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
2210 eth_config.bMaxPower = 4;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002211#ifdef CONFIG_USB_ETH_RNDIS
2212 rndis_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
2213 rndis_config.bMaxPower = 4;
2214#endif
Remy Bohmerdf063442009-07-29 18:18:43 +02002215 }
2216
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002217
2218 /* network device setup */
Remy Bohmerdf063442009-07-29 18:18:43 +02002219 dev->net = &l_netdev;
Remy Bohmerdf063442009-07-29 18:18:43 +02002220
2221 dev->cdc = cdc;
2222 dev->zlp = zlp;
2223
2224 dev->in_ep = in_ep;
2225 dev->out_ep = out_ep;
2226 dev->status_ep = status_ep;
2227
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002228 /*
2229 * Module params for these addresses should come from ID proms.
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002230 * The host side address is used with CDC and RNDIS, and commonly
Remy Bohmerdf063442009-07-29 18:18:43 +02002231 * ends up in a persistent config database. It's not clear if
2232 * host side code for the SAFE thing cares -- its original BLAN
2233 * thing didn't, Sharp never assigned those addresses on Zaurii.
2234 */
2235 get_ether_addr(dev_addr, dev->net->enetaddr);
2236
2237 memset(tmp, 0, sizeof(tmp));
2238 memcpy(tmp, dev->net->enetaddr, sizeof(dev->net->enetaddr));
2239
2240 get_ether_addr(host_addr, dev->host_mac);
2241
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002242 sprintf(ethaddr, "%02X%02X%02X%02X%02X%02X",
2243 dev->host_mac[0], dev->host_mac[1],
2244 dev->host_mac[2], dev->host_mac[3],
2245 dev->host_mac[4], dev->host_mac[5]);
Remy Bohmerdf063442009-07-29 18:18:43 +02002246
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002247 if (rndis) {
2248 status = rndis_init();
2249 if (status < 0) {
2250 error("can't init RNDIS, %d", status);
2251 goto fail;
2252 }
Remy Bohmerdf063442009-07-29 18:18:43 +02002253 }
2254
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002255 /*
2256 * use PKTSIZE (or aligned... from u-boot) and set
2257 * wMaxSegmentSize accordingly
2258 */
Remy Bohmerdf063442009-07-29 18:18:43 +02002259 dev->mtu = PKTSIZE_ALIGN; /* RNDIS does not like this, only 1514, TODO*/
2260
2261 /* preallocate control message data and buffer */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002262 dev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
Remy Bohmerdf063442009-07-29 18:18:43 +02002263 if (!dev->req)
2264 goto fail;
2265 dev->req->buf = control_req;
2266 dev->req->complete = eth_setup_complete;
2267
2268 /* ... and maybe likewise for status transfer */
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002269#if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Remy Bohmerdf063442009-07-29 18:18:43 +02002270 if (dev->status_ep) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002271 dev->stat_req = usb_ep_alloc_request(dev->status_ep,
2272 GFP_KERNEL);
Remy Bohmerdf063442009-07-29 18:18:43 +02002273 if (!dev->stat_req) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002274 usb_ep_free_request(dev->status_ep, dev->req);
Remy Bohmerdf063442009-07-29 18:18:43 +02002275
2276 goto fail;
2277 }
Vitaly Kuzmichevef8d8d72010-08-13 17:01:06 +04002278 dev->stat_req->buf = status_req;
Remy Bohmerdf063442009-07-29 18:18:43 +02002279 dev->stat_req->context = NULL;
2280 }
2281#endif
2282
2283 /* finish hookup to lower layer ... */
2284 dev->gadget = gadget;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002285 set_gadget_data(gadget, dev);
Remy Bohmerdf063442009-07-29 18:18:43 +02002286 gadget->ep0->driver_data = dev;
2287
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002288 /*
2289 * two kinds of host-initiated state changes:
Remy Bohmerdf063442009-07-29 18:18:43 +02002290 * - iff DATA transfer is active, carrier is "on"
2291 * - tx queueing enabled if open *and* carrier is "on"
2292 */
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002293
2294 printf("using %s, OUT %s IN %s%s%s\n", gadget->name,
2295 out_ep->name, in_ep->name,
2296 status_ep ? " STATUS " : "",
2297 status_ep ? status_ep->name : ""
2298 );
2299 printf("MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
2300 dev->net->enetaddr[0], dev->net->enetaddr[1],
2301 dev->net->enetaddr[2], dev->net->enetaddr[3],
2302 dev->net->enetaddr[4], dev->net->enetaddr[5]);
2303
2304 if (cdc || rndis)
2305 printf("HOST MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
2306 dev->host_mac[0], dev->host_mac[1],
2307 dev->host_mac[2], dev->host_mac[3],
2308 dev->host_mac[4], dev->host_mac[5]);
2309
2310 if (rndis) {
2311 u32 vendorID = 0;
2312
2313 /* FIXME RNDIS vendor id == "vendor NIC code" == ? */
2314
2315 dev->rndis_config = rndis_register(rndis_control_ack);
2316 if (dev->rndis_config < 0) {
2317fail0:
2318 eth_unbind(gadget);
2319 debug("RNDIS setup failed\n");
2320 status = -ENODEV;
2321 goto fail;
2322 }
2323
2324 /* these set up a lot of the OIDs that RNDIS needs */
2325 rndis_set_host_mac(dev->rndis_config, dev->host_mac);
2326 if (rndis_set_param_dev(dev->rndis_config, dev->net, dev->mtu,
2327 &dev->stats, &dev->cdc_filter))
2328 goto fail0;
2329 if (rndis_set_param_vendor(dev->rndis_config, vendorID,
2330 manufacturer))
2331 goto fail0;
2332 if (rndis_set_param_medium(dev->rndis_config,
2333 NDIS_MEDIUM_802_3, 0))
2334 goto fail0;
2335 printf("RNDIS ready\n");
2336 }
Remy Bohmerdf063442009-07-29 18:18:43 +02002337 return 0;
2338
2339fail:
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002340 error("%s failed, status = %d", __func__, status);
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002341 eth_unbind(gadget);
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002342 return status;
Remy Bohmerdf063442009-07-29 18:18:43 +02002343}
2344
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002345/*-------------------------------------------------------------------------*/
2346
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002347static int usb_eth_init(struct eth_device *netdev, bd_t *bd)
Remy Bohmerdf063442009-07-29 18:18:43 +02002348{
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002349 struct eth_dev *dev = &l_ethdev;
Remy Bohmerdf063442009-07-29 18:18:43 +02002350 struct usb_gadget *gadget;
2351 unsigned long ts;
2352 unsigned long timeout = USB_CONNECT_TIMEOUT;
2353
2354 if (!netdev) {
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04002355 error("received NULL ptr");
Remy Bohmerdf063442009-07-29 18:18:43 +02002356 goto fail;
2357 }
Vitaly Kuzmichevbe0afbf2010-12-28 16:59:32 +03002358
2359 /* Configure default mac-addresses for the USB ethernet device */
2360#ifdef CONFIG_USBNET_DEV_ADDR
2361 strlcpy(dev_addr, CONFIG_USBNET_DEV_ADDR, sizeof(dev_addr));
2362#endif
2363#ifdef CONFIG_USBNET_HOST_ADDR
2364 strlcpy(host_addr, CONFIG_USBNET_HOST_ADDR, sizeof(host_addr));
2365#endif
2366 /* Check if the user overruled the MAC addresses */
2367 if (getenv("usbnet_devaddr"))
2368 strlcpy(dev_addr, getenv("usbnet_devaddr"),
2369 sizeof(dev_addr));
2370
2371 if (getenv("usbnet_hostaddr"))
2372 strlcpy(host_addr, getenv("usbnet_hostaddr"),
2373 sizeof(host_addr));
2374
2375 if (!is_eth_addr_valid(dev_addr)) {
2376 error("Need valid 'usbnet_devaddr' to be set");
2377 goto fail;
2378 }
2379 if (!is_eth_addr_valid(host_addr)) {
2380 error("Need valid 'usbnet_hostaddr' to be set");
2381 goto fail;
2382 }
2383
Lei Wen4af32732010-12-01 23:43:43 +08002384 if (usb_gadget_register_driver(&eth_driver) < 0)
2385 goto fail;
Remy Bohmerdf063442009-07-29 18:18:43 +02002386
2387 dev->network_started = 0;
Remy Bohmerdf063442009-07-29 18:18:43 +02002388
2389 packet_received = 0;
2390 packet_sent = 0;
2391
2392 gadget = dev->gadget;
2393 usb_gadget_connect(gadget);
2394
2395 if (getenv("cdc_connect_timeout"))
2396 timeout = simple_strtoul(getenv("cdc_connect_timeout"),
2397 NULL, 10) * CONFIG_SYS_HZ;
2398 ts = get_timer(0);
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002399 while (!l_ethdev.network_started) {
Remy Bohmerdf063442009-07-29 18:18:43 +02002400 /* Handle control-c and timeouts */
2401 if (ctrlc() || (get_timer(ts) > timeout)) {
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04002402 error("The remote end did not respond in time.");
Remy Bohmerdf063442009-07-29 18:18:43 +02002403 goto fail;
2404 }
2405 usb_gadget_handle_interrupts();
2406 }
2407
Vitaly Kuzmicheve4bd3862010-09-22 13:13:56 +04002408 packet_received = 0;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002409 rx_submit(dev, dev->rx_req, 0);
Remy Bohmerdf063442009-07-29 18:18:43 +02002410 return 0;
2411fail:
2412 return -1;
2413}
2414
Joe Hershbergere4e04882012-05-22 18:36:19 +00002415static int usb_eth_send(struct eth_device *netdev, void *packet, int length)
Remy Bohmerdf063442009-07-29 18:18:43 +02002416{
2417 int retval;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002418 void *rndis_pkt = NULL;
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04002419 struct eth_dev *dev = &l_ethdev;
Vitaly Kuzmichev2907b2c2010-09-22 13:13:55 +04002420 struct usb_request *req = dev->tx_req;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002421 unsigned long ts;
2422 unsigned long timeout = USB_CONNECT_TIMEOUT;
Remy Bohmerdf063442009-07-29 18:18:43 +02002423
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04002424 debug("%s:...\n", __func__);
Remy Bohmerdf063442009-07-29 18:18:43 +02002425
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002426 /* new buffer is needed to include RNDIS header */
2427 if (rndis_active(dev)) {
2428 rndis_pkt = malloc(length +
2429 sizeof(struct rndis_packet_msg_type));
2430 if (!rndis_pkt) {
2431 error("No memory to alloc RNDIS packet");
2432 goto drop;
2433 }
2434 rndis_add_hdr(rndis_pkt, length);
2435 memcpy(rndis_pkt + sizeof(struct rndis_packet_msg_type),
Joe Hershbergere4e04882012-05-22 18:36:19 +00002436 packet, length);
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002437 packet = rndis_pkt;
2438 length += sizeof(struct rndis_packet_msg_type);
2439 }
Joe Hershbergere4e04882012-05-22 18:36:19 +00002440 req->buf = packet;
Remy Bohmerdf063442009-07-29 18:18:43 +02002441 req->context = NULL;
2442 req->complete = tx_complete;
2443
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002444 /*
2445 * use zlp framing on tx for strict CDC-Ether conformance,
Remy Bohmerdf063442009-07-29 18:18:43 +02002446 * though any robust network rx path ignores extra padding.
2447 * and some hardware doesn't like to write zlps.
2448 */
2449 req->zero = 1;
2450 if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0)
2451 length++;
2452
2453 req->length = length;
2454#if 0
2455 /* throttle highspeed IRQ rate back slightly */
2456 if (gadget_is_dualspeed(dev->gadget))
2457 req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH)
2458 ? ((dev->tx_qlen % qmult) != 0) : 0;
2459#endif
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002460 dev->tx_qlen = 1;
2461 ts = get_timer(0);
2462 packet_sent = 0;
Remy Bohmerdf063442009-07-29 18:18:43 +02002463
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002464 retval = usb_ep_queue(dev->in_ep, req, GFP_ATOMIC);
Remy Bohmerdf063442009-07-29 18:18:43 +02002465
2466 if (!retval)
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04002467 debug("%s: packet queued\n", __func__);
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002468 while (!packet_sent) {
2469 if (get_timer(ts) > timeout) {
2470 printf("timeout sending packets to usb ethernet\n");
2471 return -1;
2472 }
2473 usb_gadget_handle_interrupts();
Remy Bohmerdf063442009-07-29 18:18:43 +02002474 }
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002475 if (rndis_pkt)
2476 free(rndis_pkt);
Remy Bohmerdf063442009-07-29 18:18:43 +02002477
2478 return 0;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002479drop:
2480 dev->stats.tx_dropped++;
2481 return -ENOMEM;
Remy Bohmerdf063442009-07-29 18:18:43 +02002482}
2483
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002484static int usb_eth_recv(struct eth_device *netdev)
Remy Bohmerdf063442009-07-29 18:18:43 +02002485{
2486 struct eth_dev *dev = &l_ethdev;
2487
2488 usb_gadget_handle_interrupts();
2489
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002490 if (packet_received) {
2491 debug("%s: packet received\n", __func__);
2492 if (dev->rx_req) {
2493 NetReceive(NetRxPackets[0], dev->rx_req->length);
2494 packet_received = 0;
Remy Bohmerdf063442009-07-29 18:18:43 +02002495
Vitaly Kuzmichev2907b2c2010-09-22 13:13:55 +04002496 rx_submit(dev, dev->rx_req, 0);
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002497 } else
2498 error("dev->rx_req invalid");
Remy Bohmerdf063442009-07-29 18:18:43 +02002499 }
2500 return 0;
2501}
2502
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002503void usb_eth_halt(struct eth_device *netdev)
Remy Bohmerdf063442009-07-29 18:18:43 +02002504{
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002505 struct eth_dev *dev = &l_ethdev;
Remy Bohmerdf063442009-07-29 18:18:43 +02002506
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002507 if (!netdev) {
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04002508 error("received NULL ptr");
Remy Bohmerdf063442009-07-29 18:18:43 +02002509 return;
2510 }
2511
Lei Wen4af32732010-12-01 23:43:43 +08002512 /* If the gadget not registered, simple return */
2513 if (!dev->gadget)
2514 return;
2515
Vitaly Kuzmichev3433c332011-02-11 18:18:35 +03002516 /*
2517 * Some USB controllers may need additional deinitialization here
2518 * before dropping pull-up (also due to hardware issues).
2519 * For example: unhandled interrupt with status stage started may
2520 * bring the controller to fully broken state (until board reset).
2521 * There are some variants to debug and fix such cases:
2522 * 1) In the case of RNDIS connection eth_stop can perform additional
2523 * interrupt handling. See RNDIS_COMPLETE_SIGNAL_DISCONNECT definition.
2524 * 2) 'pullup' callback in your UDC driver can be improved to perform
2525 * this deinitialization.
2526 */
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002527 eth_stop(dev);
2528
Remy Bohmerdf063442009-07-29 18:18:43 +02002529 usb_gadget_disconnect(dev->gadget);
Vitaly Kuzmichev08d4dd72011-02-11 18:18:31 +03002530
2531 /* Clear pending interrupt */
2532 if (dev->network_started) {
2533 usb_gadget_handle_interrupts();
2534 dev->network_started = 0;
2535 }
2536
Lei Wen4af32732010-12-01 23:43:43 +08002537 usb_gadget_unregister_driver(&eth_driver);
Remy Bohmerdf063442009-07-29 18:18:43 +02002538}
2539
2540static struct usb_gadget_driver eth_driver = {
2541 .speed = DEVSPEED,
2542
2543 .bind = eth_bind,
2544 .unbind = eth_unbind,
2545
2546 .setup = eth_setup,
2547 .disconnect = eth_disconnect,
2548
2549 .suspend = eth_suspend,
2550 .resume = eth_resume,
2551};
2552
2553int usb_eth_initialize(bd_t *bi)
2554{
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002555 struct eth_device *netdev = &l_netdev;
Remy Bohmerdf063442009-07-29 18:18:43 +02002556
Vitaly Kuzmicheva36cff12010-12-28 16:59:31 +03002557 strlcpy(netdev->name, USB_NET_NAME, sizeof(netdev->name));
Remy Bohmerdf063442009-07-29 18:18:43 +02002558
2559 netdev->init = usb_eth_init;
2560 netdev->send = usb_eth_send;
2561 netdev->recv = usb_eth_recv;
2562 netdev->halt = usb_eth_halt;
2563
2564#ifdef CONFIG_MCAST_TFTP
2565 #error not supported
2566#endif
Remy Bohmerdf063442009-07-29 18:18:43 +02002567 eth_register(netdev);
2568 return 0;
Remy Bohmerdf063442009-07-29 18:18:43 +02002569}