blob: d975fb680e3ce11d62b807ee845c1427887d9751 [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
47#define DEV_CONFIG_CDC 1
48#define GFP_ATOMIC ((gfp_t) 0)
49#define GFP_KERNEL ((gfp_t) 0)
50
51/*
52 * Ethernet gadget driver -- with CDC and non-CDC options
53 * Builds on hardware support for a full duplex link.
54 *
55 * CDC Ethernet is the standard USB solution for sending Ethernet frames
56 * using USB. Real hardware tends to use the same framing protocol but look
57 * different for control features. This driver strongly prefers to use
58 * this USB-IF standard as its open-systems interoperability solution;
59 * most host side USB stacks (except from Microsoft) support it.
60 *
61 * This is sometimes called "CDC ECM" (Ethernet Control Model) to support
62 * TLA-soup. "CDC ACM" (Abstract Control Model) is for modems, and a new
63 * "CDC EEM" (Ethernet Emulation Model) is starting to spread.
64 *
65 * There's some hardware that can't talk CDC ECM. We make that hardware
66 * implement a "minimalist" vendor-agnostic CDC core: same framing, but
67 * link-level setup only requires activating the configuration. Only the
68 * endpoint descriptors, and product/vendor IDs, are relevant; no control
69 * operations are available. Linux supports it, but other host operating
70 * systems may not. (This is a subset of CDC Ethernet.)
71 *
72 * It turns out that if you add a few descriptors to that "CDC Subset",
73 * (Windows) host side drivers from MCCI can treat it as one submode of
74 * a proprietary scheme called "SAFE" ... without needing to know about
75 * specific product/vendor IDs. So we do that, making it easier to use
76 * those MS-Windows drivers. Those added descriptors make it resemble a
77 * CDC MDLM device, but they don't change device behavior at all. (See
78 * MCCI Engineering report 950198 "SAFE Networking Functions".)
79 *
80 * A third option is also in use. Rather than CDC Ethernet, or something
81 * simpler, Microsoft pushes their own approach: RNDIS. The published
82 * RNDIS specs are ambiguous and appear to be incomplete, and are also
83 * needlessly complex. They borrow more from CDC ACM than CDC ECM.
84 */
85#define ETH_ALEN 6 /* Octets in one ethernet addr */
86#define ETH_HLEN 14 /* Total octets in header. */
87#define ETH_ZLEN 60 /* Min. octets in frame sans FCS */
88#define ETH_DATA_LEN 1500 /* Max. octets in payload */
89#define ETH_FRAME_LEN PKTSIZE_ALIGN /* Max. octets in frame sans FCS */
90#define ETH_FCS_LEN 4 /* Octets in the FCS */
91
92#define DRIVER_DESC "Ethernet Gadget"
93/* Based on linux 2.6.27 version */
94#define DRIVER_VERSION "May Day 2005"
95
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +040096static const char shortname[] = "ether";
97static const char driver_desc[] = DRIVER_DESC;
Remy Bohmerdf063442009-07-29 18:18:43 +020098
99#define RX_EXTRA 20 /* guard against rx overflows */
100
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300101#ifndef CONFIG_USB_ETH_RNDIS
102#define rndis_uninit(x) do {} while (0)
103#define rndis_deregister(c) do {} while (0)
104#define rndis_exit() do {} while (0)
105#endif
106
107/* CDC and RNDIS support the same host-chosen outgoing packet filters. */
Remy Bohmerdf063442009-07-29 18:18:43 +0200108#define DEFAULT_FILTER (USB_CDC_PACKET_TYPE_BROADCAST \
109 |USB_CDC_PACKET_TYPE_ALL_MULTICAST \
110 |USB_CDC_PACKET_TYPE_PROMISCUOUS \
111 |USB_CDC_PACKET_TYPE_DIRECTED)
112
113#define USB_CONNECT_TIMEOUT (3 * CONFIG_SYS_HZ)
114
115/*-------------------------------------------------------------------------*/
Vitaly Kuzmichev737f6b22011-02-11 18:18:33 +0300116
117struct eth_dev {
118 struct usb_gadget *gadget;
119 struct usb_request *req; /* for control responses */
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300120 struct usb_request *stat_req; /* for cdc & rndis status */
Vitaly Kuzmichev737f6b22011-02-11 18:18:33 +0300121
122 u8 config;
123 struct usb_ep *in_ep, *out_ep, *status_ep;
124 const struct usb_endpoint_descriptor
125 *in, *out, *status;
126
127 struct usb_request *tx_req, *rx_req;
128
129 struct eth_device *net;
130 struct net_device_stats stats;
131 unsigned int tx_qlen;
132
133 unsigned zlp:1;
134 unsigned cdc:1;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300135 unsigned rndis:1;
Vitaly Kuzmichev737f6b22011-02-11 18:18:33 +0300136 unsigned suspended:1;
137 unsigned network_started:1;
138 u16 cdc_filter;
139 unsigned long todo;
140 int mtu;
141#define WORK_RX_MEMORY 0
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300142 int rndis_config;
Vitaly Kuzmichev737f6b22011-02-11 18:18:33 +0300143 u8 host_mac[ETH_ALEN];
144};
145
146/*
147 * This version autoconfigures as much as possible at run-time.
148 *
149 * It also ASSUMES a self-powered device, without remote wakeup,
150 * although remote wakeup support would make sense.
151 */
152
153/*-------------------------------------------------------------------------*/
Remy Bohmerdf063442009-07-29 18:18:43 +0200154static struct eth_dev l_ethdev;
155static struct eth_device l_netdev;
156static struct usb_gadget_driver eth_driver;
157
158/*-------------------------------------------------------------------------*/
159
160/* "main" config is either CDC, or its simple subset */
161static inline int is_cdc(struct eth_dev *dev)
162{
163#if !defined(DEV_CONFIG_SUBSET)
164 return 1; /* only cdc possible */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400165#elif !defined(DEV_CONFIG_CDC)
Remy Bohmerdf063442009-07-29 18:18:43 +0200166 return 0; /* only subset possible */
167#else
168 return dev->cdc; /* depends on what hardware we found */
169#endif
170}
171
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300172/* "secondary" RNDIS config may sometimes be activated */
173static inline int rndis_active(struct eth_dev *dev)
174{
175#ifdef CONFIG_USB_ETH_RNDIS
176 return dev->rndis;
177#else
178 return 0;
179#endif
180}
181
182#define subset_active(dev) (!is_cdc(dev) && !rndis_active(dev))
183#define cdc_active(dev) (is_cdc(dev) && !rndis_active(dev))
Remy Bohmerdf063442009-07-29 18:18:43 +0200184
185#define DEFAULT_QLEN 2 /* double buffering by default */
186
187/* peak bulk transfer bits-per-second */
188#define HS_BPS (13 * 512 * 8 * 1000 * 8)
189#define FS_BPS (19 * 64 * 1 * 1000 * 8)
190
191#ifdef CONFIG_USB_GADGET_DUALSPEED
192#define DEVSPEED USB_SPEED_HIGH
193
Vitaly Kuzmichevaec83b82010-08-12 16:44:40 +0400194#ifdef CONFIG_USB_ETH_QMULT
195#define qmult CONFIG_USB_ETH_QMULT
196#else
197#define qmult 5
198#endif
199
Remy Bohmerdf063442009-07-29 18:18:43 +0200200/* for dual-speed hardware, use deeper queues at highspeed */
201#define qlen(gadget) \
202 (DEFAULT_QLEN*((gadget->speed == USB_SPEED_HIGH) ? qmult : 1))
203
204static inline int BITRATE(struct usb_gadget *g)
205{
206 return (g->speed == USB_SPEED_HIGH) ? HS_BPS : FS_BPS;
207}
208
209#else /* full speed (low speed doesn't do bulk) */
210
211#define qmult 1
212
213#define DEVSPEED USB_SPEED_FULL
214
215#define qlen(gadget) DEFAULT_QLEN
216
217static inline int BITRATE(struct usb_gadget *g)
218{
219 return FS_BPS;
220}
221#endif
222
Remy Bohmerdf063442009-07-29 18:18:43 +0200223/*-------------------------------------------------------------------------*/
224
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400225/*
226 * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
Remy Bohmerdf063442009-07-29 18:18:43 +0200227 * Instead: allocate your own, using normal USB-IF procedures.
228 */
229
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400230/*
231 * Thanks to NetChip Technologies for donating this product ID.
Remy Bohmerdf063442009-07-29 18:18:43 +0200232 * It's for devices with only CDC Ethernet configurations.
233 */
234#define CDC_VENDOR_NUM 0x0525 /* NetChip */
235#define CDC_PRODUCT_NUM 0xa4a1 /* Linux-USB Ethernet Gadget */
236
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400237/*
238 * For hardware that can't talk CDC, we use the same vendor ID that
Remy Bohmerdf063442009-07-29 18:18:43 +0200239 * ARM Linux has used for ethernet-over-usb, both with sa1100 and
240 * with pxa250. We're protocol-compatible, if the host-side drivers
241 * use the endpoint descriptors. bcdDevice (version) is nonzero, so
242 * drivers that need to hard-wire endpoint numbers have a hook.
243 *
244 * The protocol is a minimal subset of CDC Ether, which works on any bulk
245 * hardware that's not deeply broken ... even on hardware that can't talk
246 * RNDIS (like SA-1100, with no interrupt endpoint, or anything that
247 * doesn't handle control-OUT).
248 */
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300249#define SIMPLE_VENDOR_NUM 0x049f /* Compaq Computer Corp. */
250#define SIMPLE_PRODUCT_NUM 0x505a /* Linux-USB "CDC Subset" Device */
Remy Bohmerdf063442009-07-29 18:18:43 +0200251
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400252/*
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300253 * For hardware that can talk RNDIS and either of the above protocols,
254 * use this ID ... the windows INF files will know it. Unless it's
255 * used with CDC Ethernet, Linux 2.4 hosts will need updates to choose
256 * the non-RNDIS configuration.
257 */
258#define RNDIS_VENDOR_NUM 0x0525 /* NetChip */
259#define RNDIS_PRODUCT_NUM 0xa4a2 /* Ethernet/RNDIS Gadget */
260
261/*
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400262 * Some systems will want different product identifers published in the
Remy Bohmerdf063442009-07-29 18:18:43 +0200263 * device descriptor, either numbers or strings or both. These string
264 * parameters are in UTF-8 (superset of ASCII's 7 bit characters).
265 */
266
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300267/*
268 * Emulating them in eth_bind:
269 * static ushort idVendor;
270 * static ushort idProduct;
271 */
272
Remy Bohmerdf063442009-07-29 18:18:43 +0200273#if defined(CONFIG_USBNET_MANUFACTURER)
274static char *iManufacturer = CONFIG_USBNET_MANUFACTURER;
275#else
276static char *iManufacturer = "U-boot";
277#endif
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300278
279/* These probably need to be configurable. */
280static ushort bcdDevice;
Remy Bohmerdf063442009-07-29 18:18:43 +0200281static char *iProduct;
282static char *iSerialNumber;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300283
Remy Bohmerdf063442009-07-29 18:18:43 +0200284static char dev_addr[18];
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300285
Remy Bohmerdf063442009-07-29 18:18:43 +0200286static char host_addr[18];
287
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300288
Remy Bohmerdf063442009-07-29 18:18:43 +0200289/*-------------------------------------------------------------------------*/
290
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400291/*
292 * USB DRIVER HOOKUP (to the hardware driver, below us), mostly
Remy Bohmerdf063442009-07-29 18:18:43 +0200293 * ep0 implementation: descriptors, config management, setup().
294 * also optional class-specific notification interrupt transfer.
295 */
296
297/*
298 * DESCRIPTORS ... most are static, but strings and (full) configuration
299 * descriptors are built on demand. For now we do either full CDC, or
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300300 * our simple subset, with RNDIS as an optional second configuration.
301 *
302 * RNDIS includes some CDC ACM descriptors ... like CDC Ethernet. But
303 * the class descriptors match a modem (they're ignored; it's really just
304 * Ethernet functionality), they don't need the NOP altsetting, and the
305 * status transfer endpoint isn't optional.
Remy Bohmerdf063442009-07-29 18:18:43 +0200306 */
307
308#define STRING_MANUFACTURER 1
309#define STRING_PRODUCT 2
310#define STRING_ETHADDR 3
311#define STRING_DATA 4
312#define STRING_CONTROL 5
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300313#define STRING_RNDIS_CONTROL 6
Remy Bohmerdf063442009-07-29 18:18:43 +0200314#define STRING_CDC 7
315#define STRING_SUBSET 8
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300316#define STRING_RNDIS 9
Remy Bohmerdf063442009-07-29 18:18:43 +0200317#define STRING_SERIALNUMBER 10
318
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300319/* holds our biggest descriptor (or RNDIS response) */
Remy Bohmerdf063442009-07-29 18:18:43 +0200320#define USB_BUFSIZ 256
321
322/*
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300323 * This device advertises one configuration, eth_config, unless RNDIS
324 * is enabled (rndis_config) on hardware supporting at least two configs.
325 *
326 * NOTE: Controllers like superh_udc should probably be able to use
327 * an RNDIS-only configuration.
Remy Bohmerdf063442009-07-29 18:18:43 +0200328 *
329 * FIXME define some higher-powered configurations to make it easier
330 * to recharge batteries ...
331 */
332
333#define DEV_CONFIG_VALUE 1 /* cdc or subset */
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300334#define DEV_RNDIS_CONFIG_VALUE 2 /* rndis; optional */
Remy Bohmerdf063442009-07-29 18:18:43 +0200335
336static struct usb_device_descriptor
337device_desc = {
338 .bLength = sizeof device_desc,
339 .bDescriptorType = USB_DT_DEVICE,
340
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400341 .bcdUSB = __constant_cpu_to_le16(0x0200),
Remy Bohmerdf063442009-07-29 18:18:43 +0200342
343 .bDeviceClass = USB_CLASS_COMM,
344 .bDeviceSubClass = 0,
345 .bDeviceProtocol = 0,
346
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400347 .idVendor = __constant_cpu_to_le16(CDC_VENDOR_NUM),
348 .idProduct = __constant_cpu_to_le16(CDC_PRODUCT_NUM),
Remy Bohmerdf063442009-07-29 18:18:43 +0200349 .iManufacturer = STRING_MANUFACTURER,
350 .iProduct = STRING_PRODUCT,
351 .bNumConfigurations = 1,
352};
353
354static struct usb_otg_descriptor
355otg_descriptor = {
356 .bLength = sizeof otg_descriptor,
357 .bDescriptorType = USB_DT_OTG,
358
359 .bmAttributes = USB_OTG_SRP,
360};
361
362static struct usb_config_descriptor
363eth_config = {
364 .bLength = sizeof eth_config,
365 .bDescriptorType = USB_DT_CONFIG,
366
367 /* compute wTotalLength on the fly */
368 .bNumInterfaces = 2,
369 .bConfigurationValue = DEV_CONFIG_VALUE,
370 .iConfiguration = STRING_CDC,
371 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
372 .bMaxPower = 1,
373};
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300374
375#ifdef CONFIG_USB_ETH_RNDIS
376static struct usb_config_descriptor
377rndis_config = {
378 .bLength = sizeof rndis_config,
379 .bDescriptorType = USB_DT_CONFIG,
380
381 /* compute wTotalLength on the fly */
382 .bNumInterfaces = 2,
383 .bConfigurationValue = DEV_RNDIS_CONFIG_VALUE,
384 .iConfiguration = STRING_RNDIS,
385 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
386 .bMaxPower = 1,
387};
388#endif
Remy Bohmerdf063442009-07-29 18:18:43 +0200389
390/*
391 * Compared to the simple CDC subset, the full CDC Ethernet model adds
392 * three class descriptors, two interface descriptors, optional status
393 * endpoint. Both have a "data" interface and two bulk endpoints.
394 * There are also differences in how control requests are handled.
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300395 *
396 * RNDIS shares a lot with CDC-Ethernet, since it's a variant of the
397 * CDC-ACM (modem) spec. Unfortunately MSFT's RNDIS driver is buggy; it
398 * may hang or oops. Since bugfixes (or accurate specs, letting Linux
399 * work around those bugs) are unlikely to ever come from MSFT, you may
400 * wish to avoid using RNDIS.
401 *
402 * MCCI offers an alternative to RNDIS if you need to connect to Windows
403 * but have hardware that can't support CDC Ethernet. We add descriptors
404 * to present the CDC Subset as a (nonconformant) CDC MDLM variant called
405 * "SAFE". That borrows from both CDC Ethernet and CDC MDLM. You can
406 * get those drivers from MCCI, or bundled with various products.
Remy Bohmerdf063442009-07-29 18:18:43 +0200407 */
408
409#ifdef DEV_CONFIG_CDC
410static struct usb_interface_descriptor
411control_intf = {
412 .bLength = sizeof control_intf,
413 .bDescriptorType = USB_DT_INTERFACE,
414
415 .bInterfaceNumber = 0,
416 /* status endpoint is optional; this may be patched later */
417 .bNumEndpoints = 1,
418 .bInterfaceClass = USB_CLASS_COMM,
419 .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET,
420 .bInterfaceProtocol = USB_CDC_PROTO_NONE,
421 .iInterface = STRING_CONTROL,
422};
423#endif
424
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300425#ifdef CONFIG_USB_ETH_RNDIS
426static const struct usb_interface_descriptor
427rndis_control_intf = {
428 .bLength = sizeof rndis_control_intf,
429 .bDescriptorType = USB_DT_INTERFACE,
430
431 .bInterfaceNumber = 0,
432 .bNumEndpoints = 1,
433 .bInterfaceClass = USB_CLASS_COMM,
434 .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
435 .bInterfaceProtocol = USB_CDC_ACM_PROTO_VENDOR,
436 .iInterface = STRING_RNDIS_CONTROL,
437};
438#endif
439
Remy Bohmerdf063442009-07-29 18:18:43 +0200440static const struct usb_cdc_header_desc header_desc = {
441 .bLength = sizeof header_desc,
442 .bDescriptorType = USB_DT_CS_INTERFACE,
443 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
444
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400445 .bcdCDC = __constant_cpu_to_le16(0x0110),
Remy Bohmerdf063442009-07-29 18:18:43 +0200446};
447
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300448#if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Remy Bohmerdf063442009-07-29 18:18:43 +0200449
450static const struct usb_cdc_union_desc union_desc = {
451 .bLength = sizeof union_desc,
452 .bDescriptorType = USB_DT_CS_INTERFACE,
453 .bDescriptorSubType = USB_CDC_UNION_TYPE,
454
455 .bMasterInterface0 = 0, /* index of control interface */
456 .bSlaveInterface0 = 1, /* index of DATA interface */
457};
458
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300459#endif /* CDC || RNDIS */
460
461#ifdef CONFIG_USB_ETH_RNDIS
462
463static const struct usb_cdc_call_mgmt_descriptor call_mgmt_descriptor = {
464 .bLength = sizeof call_mgmt_descriptor,
465 .bDescriptorType = USB_DT_CS_INTERFACE,
466 .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE,
467
468 .bmCapabilities = 0x00,
469 .bDataInterface = 0x01,
470};
471
472static const struct usb_cdc_acm_descriptor acm_descriptor = {
473 .bLength = sizeof acm_descriptor,
474 .bDescriptorType = USB_DT_CS_INTERFACE,
475 .bDescriptorSubType = USB_CDC_ACM_TYPE,
476
477 .bmCapabilities = 0x00,
478};
479
480#endif
Remy Bohmerdf063442009-07-29 18:18:43 +0200481
482#ifndef DEV_CONFIG_CDC
483
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400484/*
485 * "SAFE" loosely follows CDC WMC MDLM, violating the spec in various
Remy Bohmerdf063442009-07-29 18:18:43 +0200486 * ways: data endpoints live in the control interface, there's no data
487 * interface, and it's not used to talk to a cell phone radio.
488 */
489
490static const struct usb_cdc_mdlm_desc mdlm_desc = {
491 .bLength = sizeof mdlm_desc,
492 .bDescriptorType = USB_DT_CS_INTERFACE,
493 .bDescriptorSubType = USB_CDC_MDLM_TYPE,
494
495 .bcdVersion = __constant_cpu_to_le16(0x0100),
496 .bGUID = {
497 0x5d, 0x34, 0xcf, 0x66, 0x11, 0x18, 0x11, 0xd6,
498 0xa2, 0x1a, 0x00, 0x01, 0x02, 0xca, 0x9a, 0x7f,
499 },
500};
501
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400502/*
503 * since "usb_cdc_mdlm_detail_desc" is a variable length structure, we
Remy Bohmerdf063442009-07-29 18:18:43 +0200504 * can't really use its struct. All we do here is say that we're using
505 * the submode of "SAFE" which directly matches the CDC Subset.
506 */
507static const u8 mdlm_detail_desc[] = {
508 6,
509 USB_DT_CS_INTERFACE,
510 USB_CDC_MDLM_DETAIL_TYPE,
511
512 0, /* "SAFE" */
513 0, /* network control capabilities (none) */
514 0, /* network data capabilities ("raw" encapsulation) */
515};
516
517#endif
518
Remy Bohmerdf063442009-07-29 18:18:43 +0200519static const struct usb_cdc_ether_desc ether_desc = {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400520 .bLength = sizeof(ether_desc),
Remy Bohmerdf063442009-07-29 18:18:43 +0200521 .bDescriptorType = USB_DT_CS_INTERFACE,
522 .bDescriptorSubType = USB_CDC_ETHERNET_TYPE,
523
524 /* this descriptor actually adds value, surprise! */
525 .iMACAddress = STRING_ETHADDR,
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400526 .bmEthernetStatistics = __constant_cpu_to_le32(0), /* no statistics */
527 .wMaxSegmentSize = __constant_cpu_to_le16(ETH_FRAME_LEN),
528 .wNumberMCFilters = __constant_cpu_to_le16(0),
Remy Bohmerdf063442009-07-29 18:18:43 +0200529 .bNumberPowerFilters = 0,
530};
531
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300532#if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Remy Bohmerdf063442009-07-29 18:18:43 +0200533
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400534/*
535 * include the status endpoint if we can, even where it's optional.
Remy Bohmerdf063442009-07-29 18:18:43 +0200536 * use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one
537 * packet, to simplify cancellation; and a big transfer interval, to
538 * waste less bandwidth.
539 *
540 * some drivers (like Linux 2.4 cdc-ether!) "need" it to exist even
541 * if they ignore the connect/disconnect notifications that real aether
542 * can provide. more advanced cdc configurations might want to support
543 * encapsulated commands (vendor-specific, using control-OUT).
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300544 *
545 * RNDIS requires the status endpoint, since it uses that encapsulation
546 * mechanism for its funky RPC scheme.
Remy Bohmerdf063442009-07-29 18:18:43 +0200547 */
548
549#define LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */
550#define STATUS_BYTECOUNT 16 /* 8 byte header + data */
551
552static struct usb_endpoint_descriptor
553fs_status_desc = {
554 .bLength = USB_DT_ENDPOINT_SIZE,
555 .bDescriptorType = USB_DT_ENDPOINT,
556
557 .bEndpointAddress = USB_DIR_IN,
558 .bmAttributes = USB_ENDPOINT_XFER_INT,
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400559 .wMaxPacketSize = __constant_cpu_to_le16(STATUS_BYTECOUNT),
Remy Bohmerdf063442009-07-29 18:18:43 +0200560 .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC,
561};
562#endif
563
564#ifdef DEV_CONFIG_CDC
565
566/* the default data interface has no endpoints ... */
567
568static const struct usb_interface_descriptor
569data_nop_intf = {
570 .bLength = sizeof data_nop_intf,
571 .bDescriptorType = USB_DT_INTERFACE,
572
573 .bInterfaceNumber = 1,
574 .bAlternateSetting = 0,
575 .bNumEndpoints = 0,
576 .bInterfaceClass = USB_CLASS_CDC_DATA,
577 .bInterfaceSubClass = 0,
578 .bInterfaceProtocol = 0,
579};
580
581/* ... but the "real" data interface has two bulk endpoints */
582
583static const struct usb_interface_descriptor
584data_intf = {
585 .bLength = sizeof data_intf,
586 .bDescriptorType = USB_DT_INTERFACE,
587
588 .bInterfaceNumber = 1,
589 .bAlternateSetting = 1,
590 .bNumEndpoints = 2,
591 .bInterfaceClass = USB_CLASS_CDC_DATA,
592 .bInterfaceSubClass = 0,
593 .bInterfaceProtocol = 0,
594 .iInterface = STRING_DATA,
595};
596
597#endif
598
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300599#ifdef CONFIG_USB_ETH_RNDIS
600
601/* RNDIS doesn't activate by changing to the "real" altsetting */
602
603static const struct usb_interface_descriptor
604rndis_data_intf = {
605 .bLength = sizeof rndis_data_intf,
606 .bDescriptorType = USB_DT_INTERFACE,
607
608 .bInterfaceNumber = 1,
609 .bAlternateSetting = 0,
610 .bNumEndpoints = 2,
611 .bInterfaceClass = USB_CLASS_CDC_DATA,
612 .bInterfaceSubClass = 0,
613 .bInterfaceProtocol = 0,
614 .iInterface = STRING_DATA,
615};
616
617#endif
618
Remy Bohmerdf063442009-07-29 18:18:43 +0200619#ifdef DEV_CONFIG_SUBSET
620
621/*
622 * "Simple" CDC-subset option is a simple vendor-neutral model that most
623 * full speed controllers can handle: one interface, two bulk endpoints.
624 *
625 * To assist host side drivers, we fancy it up a bit, and add descriptors
626 * so some host side drivers will understand it as a "SAFE" variant.
627 */
628
629static const struct usb_interface_descriptor
630subset_data_intf = {
631 .bLength = sizeof subset_data_intf,
632 .bDescriptorType = USB_DT_INTERFACE,
633
634 .bInterfaceNumber = 0,
635 .bAlternateSetting = 0,
636 .bNumEndpoints = 2,
637 .bInterfaceClass = USB_CLASS_COMM,
638 .bInterfaceSubClass = USB_CDC_SUBCLASS_MDLM,
639 .bInterfaceProtocol = 0,
640 .iInterface = STRING_DATA,
641};
642
643#endif /* SUBSET */
644
Remy Bohmerdf063442009-07-29 18:18:43 +0200645static struct usb_endpoint_descriptor
646fs_source_desc = {
647 .bLength = USB_DT_ENDPOINT_SIZE,
648 .bDescriptorType = USB_DT_ENDPOINT,
649
650 .bEndpointAddress = USB_DIR_IN,
651 .bmAttributes = USB_ENDPOINT_XFER_BULK,
652};
653
654static struct usb_endpoint_descriptor
655fs_sink_desc = {
656 .bLength = USB_DT_ENDPOINT_SIZE,
657 .bDescriptorType = USB_DT_ENDPOINT,
658
659 .bEndpointAddress = USB_DIR_OUT,
660 .bmAttributes = USB_ENDPOINT_XFER_BULK,
661};
662
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400663static const struct usb_descriptor_header *fs_eth_function[11] = {
Remy Bohmerdf063442009-07-29 18:18:43 +0200664 (struct usb_descriptor_header *) &otg_descriptor,
665#ifdef DEV_CONFIG_CDC
666 /* "cdc" mode descriptors */
667 (struct usb_descriptor_header *) &control_intf,
668 (struct usb_descriptor_header *) &header_desc,
669 (struct usb_descriptor_header *) &union_desc,
670 (struct usb_descriptor_header *) &ether_desc,
671 /* NOTE: status endpoint may need to be removed */
672 (struct usb_descriptor_header *) &fs_status_desc,
673 /* data interface, with altsetting */
674 (struct usb_descriptor_header *) &data_nop_intf,
675 (struct usb_descriptor_header *) &data_intf,
676 (struct usb_descriptor_header *) &fs_source_desc,
677 (struct usb_descriptor_header *) &fs_sink_desc,
678 NULL,
679#endif /* DEV_CONFIG_CDC */
680};
681
682static inline void fs_subset_descriptors(void)
683{
684#ifdef DEV_CONFIG_SUBSET
685 /* behavior is "CDC Subset"; extra descriptors say "SAFE" */
686 fs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
687 fs_eth_function[2] = (struct usb_descriptor_header *) &header_desc;
688 fs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc;
689 fs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc;
690 fs_eth_function[5] = (struct usb_descriptor_header *) &ether_desc;
691 fs_eth_function[6] = (struct usb_descriptor_header *) &fs_source_desc;
692 fs_eth_function[7] = (struct usb_descriptor_header *) &fs_sink_desc;
693 fs_eth_function[8] = NULL;
694#else
695 fs_eth_function[1] = NULL;
696#endif
697}
698
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300699#ifdef CONFIG_USB_ETH_RNDIS
700static const struct usb_descriptor_header *fs_rndis_function[] = {
701 (struct usb_descriptor_header *) &otg_descriptor,
702 /* control interface matches ACM, not Ethernet */
703 (struct usb_descriptor_header *) &rndis_control_intf,
704 (struct usb_descriptor_header *) &header_desc,
705 (struct usb_descriptor_header *) &call_mgmt_descriptor,
706 (struct usb_descriptor_header *) &acm_descriptor,
707 (struct usb_descriptor_header *) &union_desc,
708 (struct usb_descriptor_header *) &fs_status_desc,
709 /* data interface has no altsetting */
710 (struct usb_descriptor_header *) &rndis_data_intf,
711 (struct usb_descriptor_header *) &fs_source_desc,
712 (struct usb_descriptor_header *) &fs_sink_desc,
713 NULL,
714};
715#endif
716
Remy Bohmerdf063442009-07-29 18:18:43 +0200717/*
718 * usb 2.0 devices need to expose both high speed and full speed
719 * descriptors, unless they only run at full speed.
720 */
721
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300722#if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Remy Bohmerdf063442009-07-29 18:18:43 +0200723static struct usb_endpoint_descriptor
724hs_status_desc = {
725 .bLength = USB_DT_ENDPOINT_SIZE,
726 .bDescriptorType = USB_DT_ENDPOINT,
727
728 .bmAttributes = USB_ENDPOINT_XFER_INT,
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400729 .wMaxPacketSize = __constant_cpu_to_le16(STATUS_BYTECOUNT),
Remy Bohmerdf063442009-07-29 18:18:43 +0200730 .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4,
731};
732#endif /* DEV_CONFIG_CDC */
733
734static struct usb_endpoint_descriptor
735hs_source_desc = {
736 .bLength = USB_DT_ENDPOINT_SIZE,
737 .bDescriptorType = USB_DT_ENDPOINT,
738
739 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400740 .wMaxPacketSize = __constant_cpu_to_le16(512),
Remy Bohmerdf063442009-07-29 18:18:43 +0200741};
742
743static struct usb_endpoint_descriptor
744hs_sink_desc = {
745 .bLength = USB_DT_ENDPOINT_SIZE,
746 .bDescriptorType = USB_DT_ENDPOINT,
747
748 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400749 .wMaxPacketSize = __constant_cpu_to_le16(512),
Remy Bohmerdf063442009-07-29 18:18:43 +0200750};
751
752static struct usb_qualifier_descriptor
753dev_qualifier = {
754 .bLength = sizeof dev_qualifier,
755 .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
756
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400757 .bcdUSB = __constant_cpu_to_le16(0x0200),
Remy Bohmerdf063442009-07-29 18:18:43 +0200758 .bDeviceClass = USB_CLASS_COMM,
759
760 .bNumConfigurations = 1,
761};
762
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400763static const struct usb_descriptor_header *hs_eth_function[11] = {
Remy Bohmerdf063442009-07-29 18:18:43 +0200764 (struct usb_descriptor_header *) &otg_descriptor,
765#ifdef DEV_CONFIG_CDC
766 /* "cdc" mode descriptors */
767 (struct usb_descriptor_header *) &control_intf,
768 (struct usb_descriptor_header *) &header_desc,
769 (struct usb_descriptor_header *) &union_desc,
770 (struct usb_descriptor_header *) &ether_desc,
771 /* NOTE: status endpoint may need to be removed */
772 (struct usb_descriptor_header *) &hs_status_desc,
773 /* data interface, with altsetting */
774 (struct usb_descriptor_header *) &data_nop_intf,
775 (struct usb_descriptor_header *) &data_intf,
776 (struct usb_descriptor_header *) &hs_source_desc,
777 (struct usb_descriptor_header *) &hs_sink_desc,
778 NULL,
779#endif /* DEV_CONFIG_CDC */
780};
781
782static inline void hs_subset_descriptors(void)
783{
784#ifdef DEV_CONFIG_SUBSET
785 /* behavior is "CDC Subset"; extra descriptors say "SAFE" */
786 hs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
787 hs_eth_function[2] = (struct usb_descriptor_header *) &header_desc;
788 hs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc;
789 hs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc;
790 hs_eth_function[5] = (struct usb_descriptor_header *) &ether_desc;
791 hs_eth_function[6] = (struct usb_descriptor_header *) &hs_source_desc;
792 hs_eth_function[7] = (struct usb_descriptor_header *) &hs_sink_desc;
793 hs_eth_function[8] = NULL;
794#else
795 hs_eth_function[1] = NULL;
796#endif
797}
798
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300799#ifdef CONFIG_USB_ETH_RNDIS
800static const struct usb_descriptor_header *hs_rndis_function[] = {
801 (struct usb_descriptor_header *) &otg_descriptor,
802 /* control interface matches ACM, not Ethernet */
803 (struct usb_descriptor_header *) &rndis_control_intf,
804 (struct usb_descriptor_header *) &header_desc,
805 (struct usb_descriptor_header *) &call_mgmt_descriptor,
806 (struct usb_descriptor_header *) &acm_descriptor,
807 (struct usb_descriptor_header *) &union_desc,
808 (struct usb_descriptor_header *) &hs_status_desc,
809 /* data interface has no altsetting */
810 (struct usb_descriptor_header *) &rndis_data_intf,
811 (struct usb_descriptor_header *) &hs_source_desc,
812 (struct usb_descriptor_header *) &hs_sink_desc,
813 NULL,
814};
815#endif
816
817
Remy Bohmerdf063442009-07-29 18:18:43 +0200818/* maxpacket and other transfer characteristics vary by speed. */
819static inline struct usb_endpoint_descriptor *
820ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *hs,
821 struct usb_endpoint_descriptor *fs)
822{
823 if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
824 return hs;
825 return fs;
826}
827
Remy Bohmerdf063442009-07-29 18:18:43 +0200828/*-------------------------------------------------------------------------*/
829
830/* descriptors that are built on-demand */
831
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400832static char manufacturer[50];
833static char product_desc[40] = DRIVER_DESC;
834static char serial_number[20];
Remy Bohmerdf063442009-07-29 18:18:43 +0200835
836/* address that the host will use ... usually assigned at random */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400837static char ethaddr[2 * ETH_ALEN + 1];
Remy Bohmerdf063442009-07-29 18:18:43 +0200838
839/* static strings, in UTF-8 */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400840static struct usb_string strings[] = {
Remy Bohmerdf063442009-07-29 18:18:43 +0200841 { STRING_MANUFACTURER, manufacturer, },
842 { STRING_PRODUCT, product_desc, },
843 { STRING_SERIALNUMBER, serial_number, },
844 { STRING_DATA, "Ethernet Data", },
845 { STRING_ETHADDR, ethaddr, },
846#ifdef DEV_CONFIG_CDC
847 { STRING_CDC, "CDC Ethernet", },
848 { STRING_CONTROL, "CDC Communications Control", },
849#endif
850#ifdef DEV_CONFIG_SUBSET
851 { STRING_SUBSET, "CDC Ethernet Subset", },
852#endif
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300853#ifdef CONFIG_USB_ETH_RNDIS
854 { STRING_RNDIS, "RNDIS", },
855 { STRING_RNDIS_CONTROL, "RNDIS Communications Control", },
856#endif
Remy Bohmerdf063442009-07-29 18:18:43 +0200857 { } /* end of list */
858};
859
860static struct usb_gadget_strings stringtab = {
861 .language = 0x0409, /* en-us */
862 .strings = strings,
863};
864
Remy Bohmerdf063442009-07-29 18:18:43 +0200865/*============================================================================*/
866static u8 control_req[USB_BUFSIZ];
Stefano Babic76e3d4e2010-08-15 14:18:59 +0200867static u8 status_req[STATUS_BYTECOUNT] __attribute__ ((aligned(4)));
Remy Bohmerdf063442009-07-29 18:18:43 +0200868
869
Remy Bohmerdf063442009-07-29 18:18:43 +0200870/**
871 * strlcpy - Copy a %NUL terminated string into a sized buffer
872 * @dest: Where to copy the string to
873 * @src: Where to copy the string from
874 * @size: size of destination buffer
875 *
876 * Compatible with *BSD: the result is always a valid
877 * NUL-terminated string that fits in the buffer (unless,
878 * of course, the buffer size is zero). It does not pad
879 * out the result like strncpy() does.
880 */
881size_t strlcpy(char *dest, const char *src, size_t size)
882{
883 size_t ret = strlen(src);
884
885 if (size) {
886 size_t len = (ret >= size) ? size - 1 : ret;
887 memcpy(dest, src, len);
888 dest[len] = '\0';
889 }
890 return ret;
891}
892
Remy Bohmerdf063442009-07-29 18:18:43 +0200893/*============================================================================*/
894
895/*
896 * one config, two interfaces: control, data.
897 * complications: class descriptors, and an altsetting.
898 */
899static int
900config_buf(struct usb_gadget *g, u8 *buf, u8 type, unsigned index, int is_otg)
901{
902 int len;
903 const struct usb_config_descriptor *config;
904 const struct usb_descriptor_header **function;
905 int hs = 0;
906
907 if (gadget_is_dualspeed(g)) {
908 hs = (g->speed == USB_SPEED_HIGH);
909 if (type == USB_DT_OTHER_SPEED_CONFIG)
910 hs = !hs;
911 }
912#define which_fn(t) (hs ? hs_ ## t ## _function : fs_ ## t ## _function)
913
914 if (index >= device_desc.bNumConfigurations)
915 return -EINVAL;
916
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300917#ifdef CONFIG_USB_ETH_RNDIS
918 /*
919 * list the RNDIS config first, to make Microsoft's drivers
920 * happy. DOCSIS 1.0 needs this too.
921 */
922 if (device_desc.bNumConfigurations == 2 && index == 0) {
923 config = &rndis_config;
924 function = which_fn(rndis);
925 } else
926#endif
927 {
928 config = &eth_config;
929 function = which_fn(eth);
930 }
Remy Bohmerdf063442009-07-29 18:18:43 +0200931
932 /* for now, don't advertise srp-only devices */
933 if (!is_otg)
934 function++;
935
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400936 len = usb_gadget_config_buf(config, buf, USB_BUFSIZ, function);
Remy Bohmerdf063442009-07-29 18:18:43 +0200937 if (len < 0)
938 return len;
939 ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
940 return len;
941}
942
943/*-------------------------------------------------------------------------*/
944
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300945static void eth_start(struct eth_dev *dev, gfp_t gfp_flags);
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400946static int alloc_requests(struct eth_dev *dev, unsigned n, gfp_t gfp_flags);
Remy Bohmerdf063442009-07-29 18:18:43 +0200947
948static int
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400949set_ether_config(struct eth_dev *dev, gfp_t gfp_flags)
Remy Bohmerdf063442009-07-29 18:18:43 +0200950{
951 int result = 0;
952 struct usb_gadget *gadget = dev->gadget;
953
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300954#if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
955 /* status endpoint used for RNDIS and (optionally) CDC */
Remy Bohmerdf063442009-07-29 18:18:43 +0200956 if (!subset_active(dev) && dev->status_ep) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400957 dev->status = ep_desc(gadget, &hs_status_desc,
Remy Bohmerdf063442009-07-29 18:18:43 +0200958 &fs_status_desc);
959 dev->status_ep->driver_data = dev;
960
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400961 result = usb_ep_enable(dev->status_ep, dev->status);
Remy Bohmerdf063442009-07-29 18:18:43 +0200962 if (result != 0) {
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +0400963 debug("enable %s --> %d\n",
Remy Bohmerdf063442009-07-29 18:18:43 +0200964 dev->status_ep->name, result);
965 goto done;
966 }
967 }
968#endif
969
970 dev->in = ep_desc(gadget, &hs_source_desc, &fs_source_desc);
971 dev->in_ep->driver_data = dev;
972
973 dev->out = ep_desc(gadget, &hs_sink_desc, &fs_sink_desc);
974 dev->out_ep->driver_data = dev;
975
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400976 /*
977 * With CDC, the host isn't allowed to use these two data
Remy Bohmerdf063442009-07-29 18:18:43 +0200978 * endpoints in the default altsetting for the interface.
979 * so we don't activate them yet. Reset from SET_INTERFACE.
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300980 *
981 * Strictly speaking RNDIS should work the same: activation is
982 * a side effect of setting a packet filter. Deactivation is
983 * from REMOTE_NDIS_HALT_MSG, reset from REMOTE_NDIS_RESET_MSG.
Remy Bohmerdf063442009-07-29 18:18:43 +0200984 */
985 if (!cdc_active(dev)) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400986 result = usb_ep_enable(dev->in_ep, dev->in);
Remy Bohmerdf063442009-07-29 18:18:43 +0200987 if (result != 0) {
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +0400988 debug("enable %s --> %d\n",
Remy Bohmerdf063442009-07-29 18:18:43 +0200989 dev->in_ep->name, result);
990 goto done;
991 }
992
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400993 result = usb_ep_enable(dev->out_ep, dev->out);
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->out_ep->name, result);
997 goto done;
998 }
999 }
1000
1001done:
1002 if (result == 0)
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001003 result = alloc_requests(dev, qlen(gadget), gfp_flags);
Remy Bohmerdf063442009-07-29 18:18:43 +02001004
1005 /* on error, disable any endpoints */
1006 if (result < 0) {
Vitaly Kuzmichev750be4c2010-08-13 17:02:29 +04001007 if (!subset_active(dev) && dev->status_ep)
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001008 (void) usb_ep_disable(dev->status_ep);
Remy Bohmerdf063442009-07-29 18:18:43 +02001009 dev->status = NULL;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001010 (void) usb_ep_disable(dev->in_ep);
1011 (void) usb_ep_disable(dev->out_ep);
Remy Bohmerdf063442009-07-29 18:18:43 +02001012 dev->in = NULL;
1013 dev->out = NULL;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001014 } else if (!cdc_active(dev)) {
1015 /*
1016 * activate non-CDC configs right away
1017 * this isn't strictly according to the RNDIS spec
1018 */
1019 eth_start(dev, GFP_ATOMIC);
Remy Bohmerdf063442009-07-29 18:18:43 +02001020 }
1021
1022 /* caller is responsible for cleanup on error */
1023 return result;
1024}
1025
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001026static void eth_reset_config(struct eth_dev *dev)
Remy Bohmerdf063442009-07-29 18:18:43 +02001027{
1028 if (dev->config == 0)
1029 return;
1030
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001031 debug("%s\n", __func__);
1032
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001033 rndis_uninit(dev->rndis_config);
1034
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001035 /*
1036 * disable endpoints, forcing (synchronous) completion of
Remy Bohmerdf063442009-07-29 18:18:43 +02001037 * pending i/o. then free the requests.
1038 */
1039
1040 if (dev->in) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001041 usb_ep_disable(dev->in_ep);
Remy Bohmerdf063442009-07-29 18:18:43 +02001042 if (dev->tx_req) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001043 usb_ep_free_request(dev->in_ep, dev->tx_req);
1044 dev->tx_req = NULL;
Remy Bohmerdf063442009-07-29 18:18:43 +02001045 }
1046 }
1047 if (dev->out) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001048 usb_ep_disable(dev->out_ep);
Remy Bohmerdf063442009-07-29 18:18:43 +02001049 if (dev->rx_req) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001050 usb_ep_free_request(dev->out_ep, dev->rx_req);
1051 dev->rx_req = NULL;
Remy Bohmerdf063442009-07-29 18:18:43 +02001052 }
1053 }
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001054 if (dev->status)
1055 usb_ep_disable(dev->status_ep);
1056
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001057 dev->rndis = 0;
Remy Bohmerdf063442009-07-29 18:18:43 +02001058 dev->cdc_filter = 0;
1059 dev->config = 0;
1060}
1061
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001062/*
1063 * change our operational config. must agree with the code
Remy Bohmerdf063442009-07-29 18:18:43 +02001064 * that returns config descriptors, and altsetting code.
1065 */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001066static int eth_set_config(struct eth_dev *dev, unsigned number,
1067 gfp_t gfp_flags)
Remy Bohmerdf063442009-07-29 18:18:43 +02001068{
1069 int result = 0;
1070 struct usb_gadget *gadget = dev->gadget;
1071
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001072 if (gadget_is_sa1100(gadget)
Remy Bohmerdf063442009-07-29 18:18:43 +02001073 && dev->config
1074 && dev->tx_qlen != 0) {
1075 /* tx fifo is full, but we can't clear it...*/
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001076 error("can't change configurations");
Remy Bohmerdf063442009-07-29 18:18:43 +02001077 return -ESPIPE;
1078 }
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001079 eth_reset_config(dev);
Remy Bohmerdf063442009-07-29 18:18:43 +02001080
1081 switch (number) {
1082 case DEV_CONFIG_VALUE:
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001083 result = set_ether_config(dev, gfp_flags);
Remy Bohmerdf063442009-07-29 18:18:43 +02001084 break;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001085#ifdef CONFIG_USB_ETH_RNDIS
1086 case DEV_RNDIS_CONFIG_VALUE:
1087 dev->rndis = 1;
1088 result = set_ether_config(dev, gfp_flags);
1089 break;
1090#endif
Remy Bohmerdf063442009-07-29 18:18:43 +02001091 default:
1092 result = -EINVAL;
1093 /* FALL THROUGH */
1094 case 0:
1095 break;
1096 }
1097
1098 if (result) {
1099 if (number)
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001100 eth_reset_config(dev);
Remy Bohmerdf063442009-07-29 18:18:43 +02001101 usb_gadget_vbus_draw(dev->gadget,
1102 gadget_is_otg(dev->gadget) ? 8 : 100);
1103 } else {
1104 char *speed;
1105 unsigned power;
1106
1107 power = 2 * eth_config.bMaxPower;
1108 usb_gadget_vbus_draw(dev->gadget, power);
1109
1110 switch (gadget->speed) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001111 case USB_SPEED_FULL:
1112 speed = "full"; break;
Remy Bohmerdf063442009-07-29 18:18:43 +02001113#ifdef CONFIG_USB_GADGET_DUALSPEED
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001114 case USB_SPEED_HIGH:
1115 speed = "high"; break;
Remy Bohmerdf063442009-07-29 18:18:43 +02001116#endif
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001117 default:
1118 speed = "?"; break;
Remy Bohmerdf063442009-07-29 18:18:43 +02001119 }
1120
1121 dev->config = number;
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001122 printf("%s speed config #%d: %d mA, %s, using %s\n",
Remy Bohmerdf063442009-07-29 18:18:43 +02001123 speed, number, power, driver_desc,
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001124 rndis_active(dev)
1125 ? "RNDIS"
1126 : (cdc_active(dev)
1127 ? "CDC Ethernet"
Remy Bohmerdf063442009-07-29 18:18:43 +02001128 : "CDC Ethernet Subset"));
1129 }
1130 return result;
1131}
1132
1133/*-------------------------------------------------------------------------*/
1134
1135#ifdef DEV_CONFIG_CDC
1136
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001137/*
1138 * The interrupt endpoint is used in CDC networking models (Ethernet, ATM)
Remy Bohmerdf063442009-07-29 18:18:43 +02001139 * only to notify the host about link status changes (which we support) or
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001140 * report completion of some encapsulated command (as used in RNDIS). Since
Remy Bohmerdf063442009-07-29 18:18:43 +02001141 * we want this CDC Ethernet code to be vendor-neutral, we don't use that
1142 * command mechanism; and only one status request is ever queued.
1143 */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001144static void eth_status_complete(struct usb_ep *ep, struct usb_request *req)
Remy Bohmerdf063442009-07-29 18:18:43 +02001145{
1146 struct usb_cdc_notification *event = req->buf;
1147 int value = req->status;
1148 struct eth_dev *dev = ep->driver_data;
1149
1150 /* issue the second notification if host reads the first */
1151 if (event->bNotificationType == USB_CDC_NOTIFY_NETWORK_CONNECTION
1152 && value == 0) {
1153 __le32 *data = req->buf + sizeof *event;
1154
1155 event->bmRequestType = 0xA1;
1156 event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001157 event->wValue = __constant_cpu_to_le16(0);
1158 event->wIndex = __constant_cpu_to_le16(1);
1159 event->wLength = __constant_cpu_to_le16(8);
Remy Bohmerdf063442009-07-29 18:18:43 +02001160
1161 /* SPEED_CHANGE data is up/down speeds in bits/sec */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001162 data[0] = data[1] = cpu_to_le32(BITRATE(dev->gadget));
Remy Bohmerdf063442009-07-29 18:18:43 +02001163
1164 req->length = STATUS_BYTECOUNT;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001165 value = usb_ep_queue(ep, req, GFP_ATOMIC);
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001166 debug("send SPEED_CHANGE --> %d\n", value);
Remy Bohmerdf063442009-07-29 18:18:43 +02001167 if (value == 0)
1168 return;
1169 } else if (value != -ECONNRESET) {
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001170 debug("event %02x --> %d\n",
Remy Bohmerdf063442009-07-29 18:18:43 +02001171 event->bNotificationType, value);
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001172 if (event->bNotificationType ==
1173 USB_CDC_NOTIFY_SPEED_CHANGE) {
1174 l_ethdev.network_started = 1;
Remy Bohmerdf063442009-07-29 18:18:43 +02001175 printf("USB network up!\n");
1176 }
1177 }
1178 req->context = NULL;
1179}
1180
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001181static void issue_start_status(struct eth_dev *dev)
Remy Bohmerdf063442009-07-29 18:18:43 +02001182{
1183 struct usb_request *req = dev->stat_req;
1184 struct usb_cdc_notification *event;
1185 int value;
1186
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001187 /*
1188 * flush old status
Remy Bohmerdf063442009-07-29 18:18:43 +02001189 *
1190 * FIXME ugly idiom, maybe we'd be better with just
1191 * a "cancel the whole queue" primitive since any
1192 * unlink-one primitive has way too many error modes.
1193 * here, we "know" toggle is already clear...
1194 *
1195 * FIXME iff req->context != null just dequeue it
1196 */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001197 usb_ep_disable(dev->status_ep);
1198 usb_ep_enable(dev->status_ep, dev->status);
Remy Bohmerdf063442009-07-29 18:18:43 +02001199
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001200 /*
1201 * 3.8.1 says to issue first NETWORK_CONNECTION, then
Remy Bohmerdf063442009-07-29 18:18:43 +02001202 * a SPEED_CHANGE. could be useful in some configs.
1203 */
1204 event = req->buf;
1205 event->bmRequestType = 0xA1;
1206 event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001207 event->wValue = __constant_cpu_to_le16(1); /* connected */
1208 event->wIndex = __constant_cpu_to_le16(1);
Remy Bohmerdf063442009-07-29 18:18:43 +02001209 event->wLength = 0;
1210
1211 req->length = sizeof *event;
1212 req->complete = eth_status_complete;
1213 req->context = dev;
1214
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001215 value = usb_ep_queue(dev->status_ep, req, GFP_ATOMIC);
Remy Bohmerdf063442009-07-29 18:18:43 +02001216 if (value < 0)
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001217 debug("status buf queue --> %d\n", value);
Remy Bohmerdf063442009-07-29 18:18:43 +02001218}
1219
1220#endif
1221
1222/*-------------------------------------------------------------------------*/
1223
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001224static void eth_setup_complete(struct usb_ep *ep, struct usb_request *req)
Remy Bohmerdf063442009-07-29 18:18:43 +02001225{
1226 if (req->status || req->actual != req->length)
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001227 debug("setup complete --> %d, %d/%d\n",
Remy Bohmerdf063442009-07-29 18:18:43 +02001228 req->status, req->actual, req->length);
1229}
1230
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001231#ifdef CONFIG_USB_ETH_RNDIS
1232
1233static void rndis_response_complete(struct usb_ep *ep, struct usb_request *req)
1234{
1235 if (req->status || req->actual != req->length)
1236 debug("rndis response complete --> %d, %d/%d\n",
1237 req->status, req->actual, req->length);
1238
1239 /* done sending after USB_CDC_GET_ENCAPSULATED_RESPONSE */
1240}
1241
1242static void rndis_command_complete(struct usb_ep *ep, struct usb_request *req)
1243{
1244 struct eth_dev *dev = ep->driver_data;
1245 int status;
1246
1247 /* received RNDIS command from USB_CDC_SEND_ENCAPSULATED_COMMAND */
1248 status = rndis_msg_parser(dev->rndis_config, (u8 *) req->buf);
1249 if (status < 0)
1250 error("%s: rndis parse error %d", __func__, status);
1251}
1252
1253#endif /* RNDIS */
1254
Remy Bohmerdf063442009-07-29 18:18:43 +02001255/*
1256 * The setup() callback implements all the ep0 functionality that's not
1257 * handled lower down. CDC has a number of less-common features:
1258 *
1259 * - two interfaces: control, and ethernet data
1260 * - Ethernet data interface has two altsettings: default, and active
1261 * - class-specific descriptors for the control interface
1262 * - class-specific control requests
1263 */
1264static int
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001265eth_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
Remy Bohmerdf063442009-07-29 18:18:43 +02001266{
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001267 struct eth_dev *dev = get_gadget_data(gadget);
Remy Bohmerdf063442009-07-29 18:18:43 +02001268 struct usb_request *req = dev->req;
1269 int value = -EOPNOTSUPP;
1270 u16 wIndex = le16_to_cpu(ctrl->wIndex);
1271 u16 wValue = le16_to_cpu(ctrl->wValue);
1272 u16 wLength = le16_to_cpu(ctrl->wLength);
1273
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001274 /*
1275 * descriptors just go into the pre-allocated ep0 buffer,
Remy Bohmerdf063442009-07-29 18:18:43 +02001276 * while config change events may enable network traffic.
1277 */
1278
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001279 debug("%s\n", __func__);
Remy Bohmerdf063442009-07-29 18:18:43 +02001280
1281 req->complete = eth_setup_complete;
1282 switch (ctrl->bRequest) {
1283
1284 case USB_REQ_GET_DESCRIPTOR:
1285 if (ctrl->bRequestType != USB_DIR_IN)
1286 break;
1287 switch (wValue >> 8) {
1288
1289 case USB_DT_DEVICE:
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001290 value = min(wLength, (u16) sizeof device_desc);
1291 memcpy(req->buf, &device_desc, value);
Remy Bohmerdf063442009-07-29 18:18:43 +02001292 break;
1293 case USB_DT_DEVICE_QUALIFIER:
1294 if (!gadget_is_dualspeed(gadget))
1295 break;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001296 value = min(wLength, (u16) sizeof dev_qualifier);
1297 memcpy(req->buf, &dev_qualifier, value);
Remy Bohmerdf063442009-07-29 18:18:43 +02001298 break;
1299
1300 case USB_DT_OTHER_SPEED_CONFIG:
1301 if (!gadget_is_dualspeed(gadget))
1302 break;
1303 /* FALLTHROUGH */
1304 case USB_DT_CONFIG:
1305 value = config_buf(gadget, req->buf,
1306 wValue >> 8,
1307 wValue & 0xff,
1308 gadget_is_otg(gadget));
1309 if (value >= 0)
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001310 value = min(wLength, (u16) value);
Remy Bohmerdf063442009-07-29 18:18:43 +02001311 break;
1312
1313 case USB_DT_STRING:
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001314 value = usb_gadget_get_string(&stringtab,
Remy Bohmerdf063442009-07-29 18:18:43 +02001315 wValue & 0xff, req->buf);
1316
1317 if (value >= 0)
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001318 value = min(wLength, (u16) value);
Remy Bohmerdf063442009-07-29 18:18:43 +02001319
1320 break;
1321 }
1322 break;
1323
1324 case USB_REQ_SET_CONFIGURATION:
1325 if (ctrl->bRequestType != 0)
1326 break;
1327 if (gadget->a_hnp_support)
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001328 debug("HNP available\n");
Remy Bohmerdf063442009-07-29 18:18:43 +02001329 else if (gadget->a_alt_hnp_support)
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001330 debug("HNP needs a different root port\n");
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001331 value = eth_set_config(dev, wValue, GFP_ATOMIC);
Remy Bohmerdf063442009-07-29 18:18:43 +02001332 break;
1333 case USB_REQ_GET_CONFIGURATION:
1334 if (ctrl->bRequestType != USB_DIR_IN)
1335 break;
1336 *(u8 *)req->buf = dev->config;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001337 value = min(wLength, (u16) 1);
Remy Bohmerdf063442009-07-29 18:18:43 +02001338 break;
1339
1340 case USB_REQ_SET_INTERFACE:
1341 if (ctrl->bRequestType != USB_RECIP_INTERFACE
1342 || !dev->config
1343 || wIndex > 1)
1344 break;
1345 if (!cdc_active(dev) && wIndex != 0)
1346 break;
1347
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001348 /*
1349 * PXA hardware partially handles SET_INTERFACE;
Remy Bohmerdf063442009-07-29 18:18:43 +02001350 * we need to kluge around that interference.
1351 */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001352 if (gadget_is_pxa(gadget)) {
1353 value = eth_set_config(dev, DEV_CONFIG_VALUE,
Remy Bohmerdf063442009-07-29 18:18:43 +02001354 GFP_ATOMIC);
1355 goto done_set_intf;
1356 }
1357
1358#ifdef DEV_CONFIG_CDC
1359 switch (wIndex) {
1360 case 0: /* control/master intf */
1361 if (wValue != 0)
1362 break;
1363 if (dev->status) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001364 usb_ep_disable(dev->status_ep);
1365 usb_ep_enable(dev->status_ep, dev->status);
Remy Bohmerdf063442009-07-29 18:18:43 +02001366 }
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001367
Remy Bohmerdf063442009-07-29 18:18:43 +02001368 value = 0;
1369 break;
1370 case 1: /* data intf */
1371 if (wValue > 1)
1372 break;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001373 usb_ep_disable(dev->in_ep);
1374 usb_ep_disable(dev->out_ep);
Remy Bohmerdf063442009-07-29 18:18:43 +02001375
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001376 /*
1377 * CDC requires the data transfers not be done from
Remy Bohmerdf063442009-07-29 18:18:43 +02001378 * the default interface setting ... also, setting
1379 * the non-default interface resets filters etc.
1380 */
1381 if (wValue == 1) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001382 if (!cdc_active(dev))
Remy Bohmerdf063442009-07-29 18:18:43 +02001383 break;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001384 usb_ep_enable(dev->in_ep, dev->in);
1385 usb_ep_enable(dev->out_ep, dev->out);
Remy Bohmerdf063442009-07-29 18:18:43 +02001386 dev->cdc_filter = DEFAULT_FILTER;
1387 if (dev->status)
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001388 issue_start_status(dev);
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001389 eth_start(dev, GFP_ATOMIC);
Remy Bohmerdf063442009-07-29 18:18:43 +02001390 }
Remy Bohmerdf063442009-07-29 18:18:43 +02001391 value = 0;
1392 break;
1393 }
1394#else
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001395 /*
1396 * FIXME this is wrong, as is the assumption that
Remy Bohmerdf063442009-07-29 18:18:43 +02001397 * all non-PXA hardware talks real CDC ...
1398 */
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001399 debug("set_interface ignored!\n");
Remy Bohmerdf063442009-07-29 18:18:43 +02001400#endif /* DEV_CONFIG_CDC */
1401
1402done_set_intf:
1403 break;
1404 case USB_REQ_GET_INTERFACE:
1405 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE)
1406 || !dev->config
1407 || wIndex > 1)
1408 break;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001409 if (!(cdc_active(dev) || rndis_active(dev)) && wIndex != 0)
Remy Bohmerdf063442009-07-29 18:18:43 +02001410 break;
1411
1412 /* for CDC, iff carrier is on, data interface is active. */
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001413 if (rndis_active(dev) || wIndex != 1)
Remy Bohmerdf063442009-07-29 18:18:43 +02001414 *(u8 *)req->buf = 0;
1415 else {
1416 /* *(u8 *)req->buf = netif_carrier_ok (dev->net) ? 1 : 0; */
1417 /* carrier always ok ...*/
1418 *(u8 *)req->buf = 1 ;
1419 }
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001420 value = min(wLength, (u16) 1);
Remy Bohmerdf063442009-07-29 18:18:43 +02001421 break;
1422
1423#ifdef DEV_CONFIG_CDC
1424 case USB_CDC_SET_ETHERNET_PACKET_FILTER:
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001425 /*
1426 * see 6.2.30: no data, wIndex = interface,
Remy Bohmerdf063442009-07-29 18:18:43 +02001427 * wValue = packet filter bitmap
1428 */
1429 if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1430 || !cdc_active(dev)
1431 || wLength != 0
1432 || wIndex > 1)
1433 break;
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001434 debug("packet filter %02x\n", wValue);
Remy Bohmerdf063442009-07-29 18:18:43 +02001435 dev->cdc_filter = wValue;
1436 value = 0;
1437 break;
1438
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001439 /*
1440 * and potentially:
Remy Bohmerdf063442009-07-29 18:18:43 +02001441 * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS:
1442 * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER:
1443 * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER:
1444 * case USB_CDC_GET_ETHERNET_STATISTIC:
1445 */
1446
1447#endif /* DEV_CONFIG_CDC */
1448
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001449#ifdef CONFIG_USB_ETH_RNDIS
1450 /*
1451 * RNDIS uses the CDC command encapsulation mechanism to implement
1452 * an RPC scheme, with much getting/setting of attributes by OID.
1453 */
1454 case USB_CDC_SEND_ENCAPSULATED_COMMAND:
1455 if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1456 || !rndis_active(dev)
1457 || wLength > USB_BUFSIZ
1458 || wValue
1459 || rndis_control_intf.bInterfaceNumber
1460 != wIndex)
1461 break;
1462 /* read the request, then process it */
1463 value = wLength;
1464 req->complete = rndis_command_complete;
1465 /* later, rndis_control_ack () sends a notification */
1466 break;
1467
1468 case USB_CDC_GET_ENCAPSULATED_RESPONSE:
1469 if ((USB_DIR_IN|USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1470 == ctrl->bRequestType
1471 && rndis_active(dev)
1472 /* && wLength >= 0x0400 */
1473 && !wValue
1474 && rndis_control_intf.bInterfaceNumber
1475 == wIndex) {
1476 u8 *buf;
1477 u32 n;
1478
1479 /* return the result */
1480 buf = rndis_get_next_response(dev->rndis_config, &n);
1481 if (buf) {
1482 memcpy(req->buf, buf, n);
1483 req->complete = rndis_response_complete;
1484 rndis_free_response(dev->rndis_config, buf);
1485 value = n;
1486 }
1487 /* else stalls ... spec says to avoid that */
1488 }
1489 break;
1490#endif /* RNDIS */
1491
Remy Bohmerdf063442009-07-29 18:18:43 +02001492 default:
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001493 debug("unknown control req%02x.%02x v%04x i%04x l%d\n",
Remy Bohmerdf063442009-07-29 18:18:43 +02001494 ctrl->bRequestType, ctrl->bRequest,
1495 wValue, wIndex, wLength);
1496 }
1497
1498 /* respond with data transfer before status phase? */
1499 if (value >= 0) {
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001500 debug("respond with data transfer before status phase\n");
Remy Bohmerdf063442009-07-29 18:18:43 +02001501 req->length = value;
1502 req->zero = value < wLength
1503 && (value % gadget->ep0->maxpacket) == 0;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001504 value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
Remy Bohmerdf063442009-07-29 18:18:43 +02001505 if (value < 0) {
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001506 debug("ep_queue --> %d\n", value);
Remy Bohmerdf063442009-07-29 18:18:43 +02001507 req->status = 0;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001508 eth_setup_complete(gadget->ep0, req);
Remy Bohmerdf063442009-07-29 18:18:43 +02001509 }
1510 }
1511
1512 /* host either stalls (value < 0) or reports success */
1513 return value;
1514}
1515
Remy Bohmerdf063442009-07-29 18:18:43 +02001516/*-------------------------------------------------------------------------*/
1517
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001518static void rx_complete(struct usb_ep *ep, struct usb_request *req);
Remy Bohmerdf063442009-07-29 18:18:43 +02001519
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001520static int rx_submit(struct eth_dev *dev, struct usb_request *req,
Remy Bohmerdf063442009-07-29 18:18:43 +02001521 gfp_t gfp_flags)
1522{
1523 int retval = -ENOMEM;
1524 size_t size;
1525
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001526 /*
1527 * Padding up to RX_EXTRA handles minor disagreements with host.
Remy Bohmerdf063442009-07-29 18:18:43 +02001528 * Normally we use the USB "terminate on short read" convention;
1529 * so allow up to (N*maxpacket), since that memory is normally
1530 * already allocated. Some hardware doesn't deal well with short
1531 * reads (e.g. DMA must be N*maxpacket), so for now don't trim a
1532 * byte off the end (to force hardware errors on overflow).
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001533 *
1534 * RNDIS uses internal framing, and explicitly allows senders to
1535 * pad to end-of-packet. That's potentially nice for speed,
1536 * but means receivers can't recover synch on their own.
Remy Bohmerdf063442009-07-29 18:18:43 +02001537 */
1538
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001539 debug("%s\n", __func__);
Remy Bohmerdf063442009-07-29 18:18:43 +02001540
1541 size = (ETHER_HDR_SIZE + dev->mtu + RX_EXTRA);
1542 size += dev->out_ep->maxpacket - 1;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001543 if (rndis_active(dev))
1544 size += sizeof(struct rndis_packet_msg_type);
Remy Bohmerdf063442009-07-29 18:18:43 +02001545 size -= size % dev->out_ep->maxpacket;
1546
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001547 /*
1548 * Some platforms perform better when IP packets are aligned,
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001549 * but on at least one, checksumming fails otherwise. Note:
1550 * RNDIS headers involve variable numbers of LE32 values.
Remy Bohmerdf063442009-07-29 18:18:43 +02001551 */
1552
1553 req->buf = (u8 *) NetRxPackets[0];
1554 req->length = size;
1555 req->complete = rx_complete;
1556
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001557 retval = usb_ep_queue(dev->out_ep, req, gfp_flags);
Remy Bohmerdf063442009-07-29 18:18:43 +02001558
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001559 if (retval)
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001560 error("rx submit --> %d", retval);
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001561
Remy Bohmerdf063442009-07-29 18:18:43 +02001562 return retval;
1563}
1564
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001565static void rx_complete(struct usb_ep *ep, struct usb_request *req)
Remy Bohmerdf063442009-07-29 18:18:43 +02001566{
1567 struct eth_dev *dev = ep->driver_data;
1568
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001569 debug("%s: status %d\n", __func__, req->status);
Vitaly Kuzmichev24fcb472011-02-11 18:18:32 +03001570 switch (req->status) {
1571 /* normal completion */
1572 case 0:
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001573 if (rndis_active(dev)) {
1574 /* we know MaxPacketsPerTransfer == 1 here */
1575 int length = rndis_rm_hdr(req->buf, req->actual);
1576 if (length < 0)
1577 goto length_err;
1578 req->length -= length;
1579 req->actual -= length;
1580 }
1581 if (req->actual < ETH_HLEN || ETH_FRAME_LEN < req->actual) {
1582length_err:
1583 dev->stats.rx_errors++;
1584 dev->stats.rx_length_errors++;
1585 debug("rx length %d\n", req->length);
1586 break;
1587 }
1588
Vitaly Kuzmichev24fcb472011-02-11 18:18:32 +03001589 dev->stats.rx_packets++;
1590 dev->stats.rx_bytes += req->length;
1591 break;
1592
1593 /* software-driven interface shutdown */
1594 case -ECONNRESET: /* unlink */
1595 case -ESHUTDOWN: /* disconnect etc */
1596 /* for hardware automagic (such as pxa) */
1597 case -ECONNABORTED: /* endpoint reset */
1598 break;
1599
1600 /* data overrun */
1601 case -EOVERFLOW:
1602 dev->stats.rx_over_errors++;
1603 /* FALLTHROUGH */
1604 default:
1605 dev->stats.rx_errors++;
1606 break;
1607 }
Remy Bohmerdf063442009-07-29 18:18:43 +02001608
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001609 packet_received = 1;
Remy Bohmerdf063442009-07-29 18:18:43 +02001610}
1611
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001612static int alloc_requests(struct eth_dev *dev, unsigned n, gfp_t gfp_flags)
Remy Bohmerdf063442009-07-29 18:18:43 +02001613{
1614
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001615 dev->tx_req = usb_ep_alloc_request(dev->in_ep, 0);
Remy Bohmerdf063442009-07-29 18:18:43 +02001616
1617 if (!dev->tx_req)
Vitaly Kuzmichev2907b2c2010-09-22 13:13:55 +04001618 goto fail1;
Remy Bohmerdf063442009-07-29 18:18:43 +02001619
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001620 dev->rx_req = usb_ep_alloc_request(dev->out_ep, 0);
Remy Bohmerdf063442009-07-29 18:18:43 +02001621
1622 if (!dev->rx_req)
Vitaly Kuzmichev2907b2c2010-09-22 13:13:55 +04001623 goto fail2;
Remy Bohmerdf063442009-07-29 18:18:43 +02001624
1625 return 0;
1626
Vitaly Kuzmichev2907b2c2010-09-22 13:13:55 +04001627fail2:
1628 usb_ep_free_request(dev->in_ep, dev->tx_req);
1629fail1:
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001630 error("can't alloc requests");
Remy Bohmerdf063442009-07-29 18:18:43 +02001631 return -1;
1632}
1633
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001634static void tx_complete(struct usb_ep *ep, struct usb_request *req)
Remy Bohmerdf063442009-07-29 18:18:43 +02001635{
Vitaly Kuzmichev24fcb472011-02-11 18:18:32 +03001636 struct eth_dev *dev = ep->driver_data;
1637
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001638 debug("%s: status %s\n", __func__, (req->status) ? "failed" : "ok");
Vitaly Kuzmichev24fcb472011-02-11 18:18:32 +03001639 switch (req->status) {
1640 default:
1641 dev->stats.tx_errors++;
1642 debug("tx err %d\n", req->status);
1643 /* FALLTHROUGH */
1644 case -ECONNRESET: /* unlink */
1645 case -ESHUTDOWN: /* disconnect etc */
1646 break;
1647 case 0:
1648 dev->stats.tx_bytes += req->length;
1649 }
1650 dev->stats.tx_packets++;
1651
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001652 packet_sent = 1;
Remy Bohmerdf063442009-07-29 18:18:43 +02001653}
1654
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001655static inline int eth_is_promisc(struct eth_dev *dev)
Remy Bohmerdf063442009-07-29 18:18:43 +02001656{
1657 /* no filters for the CDC subset; always promisc */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001658 if (subset_active(dev))
Remy Bohmerdf063442009-07-29 18:18:43 +02001659 return 1;
1660 return dev->cdc_filter & USB_CDC_PACKET_TYPE_PROMISCUOUS;
1661}
1662
1663#if 0
1664static int eth_start_xmit (struct sk_buff *skb, struct net_device *net)
1665{
1666 struct eth_dev *dev = netdev_priv(net);
1667 int length = skb->len;
1668 int retval;
1669 struct usb_request *req = NULL;
1670 unsigned long flags;
1671
1672 /* apply outgoing CDC or RNDIS filters */
1673 if (!eth_is_promisc (dev)) {
1674 u8 *dest = skb->data;
1675
1676 if (is_multicast_ether_addr(dest)) {
1677 u16 type;
1678
1679 /* ignores USB_CDC_PACKET_TYPE_MULTICAST and host
1680 * SET_ETHERNET_MULTICAST_FILTERS requests
1681 */
1682 if (is_broadcast_ether_addr(dest))
1683 type = USB_CDC_PACKET_TYPE_BROADCAST;
1684 else
1685 type = USB_CDC_PACKET_TYPE_ALL_MULTICAST;
1686 if (!(dev->cdc_filter & type)) {
1687 dev_kfree_skb_any (skb);
1688 return 0;
1689 }
1690 }
1691 /* ignores USB_CDC_PACKET_TYPE_DIRECTED */
1692 }
1693
1694 spin_lock_irqsave(&dev->req_lock, flags);
1695 /*
1696 * this freelist can be empty if an interrupt triggered disconnect()
1697 * and reconfigured the gadget (shutting down this queue) after the
1698 * network stack decided to xmit but before we got the spinlock.
1699 */
1700 if (list_empty(&dev->tx_reqs)) {
1701 spin_unlock_irqrestore(&dev->req_lock, flags);
1702 return 1;
1703 }
1704
1705 req = container_of (dev->tx_reqs.next, struct usb_request, list);
1706 list_del (&req->list);
1707
1708 /* temporarily stop TX queue when the freelist empties */
1709 if (list_empty (&dev->tx_reqs))
1710 netif_stop_queue (net);
1711 spin_unlock_irqrestore(&dev->req_lock, flags);
1712
1713 /* no buffer copies needed, unless the network stack did it
1714 * or the hardware can't use skb buffers.
1715 * or there's not enough space for any RNDIS headers we need
1716 */
1717 if (rndis_active(dev)) {
1718 struct sk_buff *skb_rndis;
1719
1720 skb_rndis = skb_realloc_headroom (skb,
1721 sizeof (struct rndis_packet_msg_type));
1722 if (!skb_rndis)
1723 goto drop;
1724
1725 dev_kfree_skb_any (skb);
1726 skb = skb_rndis;
1727 rndis_add_hdr (skb);
1728 length = skb->len;
1729 }
1730 req->buf = skb->data;
1731 req->context = skb;
1732 req->complete = tx_complete;
1733
1734 /* use zlp framing on tx for strict CDC-Ether conformance,
1735 * though any robust network rx path ignores extra padding.
1736 * and some hardware doesn't like to write zlps.
1737 */
1738 req->zero = 1;
1739 if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0)
1740 length++;
1741
1742 req->length = length;
1743
1744 /* throttle highspeed IRQ rate back slightly */
1745 if (gadget_is_dualspeed(dev->gadget))
1746 req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH)
1747 ? ((atomic_read(&dev->tx_qlen) % qmult) != 0)
1748 : 0;
1749
1750 retval = usb_ep_queue (dev->in_ep, req, GFP_ATOMIC);
1751 switch (retval) {
1752 default:
1753 DEBUG (dev, "tx queue err %d\n", retval);
1754 break;
1755 case 0:
1756 net->trans_start = jiffies;
1757 atomic_inc (&dev->tx_qlen);
1758 }
1759
1760 if (retval) {
1761drop:
1762 dev->stats.tx_dropped++;
1763 dev_kfree_skb_any (skb);
1764 spin_lock_irqsave(&dev->req_lock, flags);
1765 if (list_empty (&dev->tx_reqs))
1766 netif_start_queue (net);
1767 list_add (&req->list, &dev->tx_reqs);
1768 spin_unlock_irqrestore(&dev->req_lock, flags);
1769 }
1770 return 0;
1771}
1772
1773/*-------------------------------------------------------------------------*/
1774#endif
1775
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001776static void eth_unbind(struct usb_gadget *gadget)
Remy Bohmerdf063442009-07-29 18:18:43 +02001777{
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001778 struct eth_dev *dev = get_gadget_data(gadget);
Remy Bohmerdf063442009-07-29 18:18:43 +02001779
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001780 debug("%s...\n", __func__);
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001781 rndis_deregister(dev->rndis_config);
1782 rndis_exit();
Remy Bohmerdf063442009-07-29 18:18:43 +02001783
Vitaly Kuzmichev90670702010-08-13 17:00:16 +04001784 /* we've already been disconnected ... no i/o is active */
1785 if (dev->req) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001786 usb_ep_free_request(gadget->ep0, dev->req);
Vitaly Kuzmichev90670702010-08-13 17:00:16 +04001787 dev->req = NULL;
1788 }
Remy Bohmerdf063442009-07-29 18:18:43 +02001789 if (dev->stat_req) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001790 usb_ep_free_request(dev->status_ep, dev->stat_req);
Remy Bohmerdf063442009-07-29 18:18:43 +02001791 dev->stat_req = NULL;
1792 }
1793
1794 if (dev->tx_req) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001795 usb_ep_free_request(dev->in_ep, dev->tx_req);
1796 dev->tx_req = NULL;
Remy Bohmerdf063442009-07-29 18:18:43 +02001797 }
1798
1799 if (dev->rx_req) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001800 usb_ep_free_request(dev->out_ep, dev->rx_req);
1801 dev->rx_req = NULL;
Remy Bohmerdf063442009-07-29 18:18:43 +02001802 }
1803
1804/* unregister_netdev (dev->net);*/
1805/* free_netdev(dev->net);*/
1806
Lei Wen4af32732010-12-01 23:43:43 +08001807 dev->gadget = NULL;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001808 set_gadget_data(gadget, NULL);
Remy Bohmerdf063442009-07-29 18:18:43 +02001809}
1810
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001811static void eth_disconnect(struct usb_gadget *gadget)
Remy Bohmerdf063442009-07-29 18:18:43 +02001812{
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001813 eth_reset_config(get_gadget_data(gadget));
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001814 /* FIXME RNDIS should enter RNDIS_UNINITIALIZED */
Remy Bohmerdf063442009-07-29 18:18:43 +02001815}
1816
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001817static void eth_suspend(struct usb_gadget *gadget)
Remy Bohmerdf063442009-07-29 18:18:43 +02001818{
1819 /* Not used */
1820}
1821
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001822static void eth_resume(struct usb_gadget *gadget)
Remy Bohmerdf063442009-07-29 18:18:43 +02001823{
1824 /* Not used */
1825}
1826
1827/*-------------------------------------------------------------------------*/
1828
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001829#ifdef CONFIG_USB_ETH_RNDIS
1830
1831/*
1832 * The interrupt endpoint is used in RNDIS to notify the host when messages
1833 * other than data packets are available ... notably the REMOTE_NDIS_*_CMPLT
1834 * messages, but also REMOTE_NDIS_INDICATE_STATUS_MSG and potentially even
1835 * REMOTE_NDIS_KEEPALIVE_MSG.
1836 *
1837 * The RNDIS control queue is processed by GET_ENCAPSULATED_RESPONSE, and
1838 * normally just one notification will be queued.
1839 */
1840
1841static void rndis_control_ack_complete(struct usb_ep *ep,
1842 struct usb_request *req)
1843{
1844 struct eth_dev *dev = ep->driver_data;
1845
1846 debug("%s...\n", __func__);
1847 if (req->status || req->actual != req->length)
1848 debug("rndis control ack complete --> %d, %d/%d\n",
1849 req->status, req->actual, req->length);
1850
1851 if (!l_ethdev.network_started) {
1852 if (rndis_get_state(dev->rndis_config)
1853 == RNDIS_DATA_INITIALIZED) {
1854 l_ethdev.network_started = 1;
1855 printf("USB RNDIS network up!\n");
1856 }
1857 }
1858
1859 req->context = NULL;
1860
1861 if (req != dev->stat_req)
1862 usb_ep_free_request(ep, req);
1863}
1864
1865static char rndis_resp_buf[8] __attribute__((aligned(sizeof(__le32))));
1866
1867static int rndis_control_ack(struct eth_device *net)
1868{
1869 struct eth_dev *dev = &l_ethdev;
1870 int length;
1871 struct usb_request *resp = dev->stat_req;
1872
1873 /* in case RNDIS calls this after disconnect */
1874 if (!dev->status) {
1875 debug("status ENODEV\n");
1876 return -ENODEV;
1877 }
1878
1879 /* in case queue length > 1 */
1880 if (resp->context) {
1881 resp = usb_ep_alloc_request(dev->status_ep, GFP_ATOMIC);
1882 if (!resp)
1883 return -ENOMEM;
1884 resp->buf = rndis_resp_buf;
1885 }
1886
1887 /*
1888 * Send RNDIS RESPONSE_AVAILABLE notification;
1889 * USB_CDC_NOTIFY_RESPONSE_AVAILABLE should work too
1890 */
1891 resp->length = 8;
1892 resp->complete = rndis_control_ack_complete;
1893 resp->context = dev;
1894
1895 *((__le32 *) resp->buf) = __constant_cpu_to_le32(1);
1896 *((__le32 *) (resp->buf + 4)) = __constant_cpu_to_le32(0);
1897
1898 length = usb_ep_queue(dev->status_ep, resp, GFP_ATOMIC);
1899 if (length < 0) {
1900 resp->status = 0;
1901 rndis_control_ack_complete(dev->status_ep, resp);
1902 }
1903
1904 return 0;
1905}
1906
1907#else
1908
1909#define rndis_control_ack NULL
1910
1911#endif /* RNDIS */
1912
1913static void eth_start(struct eth_dev *dev, gfp_t gfp_flags)
1914{
1915 if (rndis_active(dev)) {
1916 rndis_set_param_medium(dev->rndis_config,
1917 NDIS_MEDIUM_802_3,
1918 BITRATE(dev->gadget)/100);
1919 rndis_signal_connect(dev->rndis_config);
1920 }
1921}
1922
1923static int eth_stop(struct eth_dev *dev)
1924{
Vitaly Kuzmichev3433c332011-02-11 18:18:35 +03001925#ifdef RNDIS_COMPLETE_SIGNAL_DISCONNECT
1926 unsigned long ts;
1927 unsigned long timeout = CONFIG_SYS_HZ; /* 1 sec to stop RNDIS */
1928#endif
1929
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001930 if (rndis_active(dev)) {
1931 rndis_set_param_medium(dev->rndis_config, NDIS_MEDIUM_802_3, 0);
1932 rndis_signal_disconnect(dev->rndis_config);
1933
Vitaly Kuzmichev3433c332011-02-11 18:18:35 +03001934#ifdef RNDIS_COMPLETE_SIGNAL_DISCONNECT
1935 /* Wait until host receives OID_GEN_MEDIA_CONNECT_STATUS */
1936 ts = get_timer(0);
1937 while (get_timer(ts) < timeout)
1938 usb_gadget_handle_interrupts();
1939#endif
1940
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001941 rndis_uninit(dev->rndis_config);
1942 dev->rndis = 0;
1943 }
1944
1945 return 0;
1946}
1947
1948/*-------------------------------------------------------------------------*/
1949
Remy Bohmerdf063442009-07-29 18:18:43 +02001950static int is_eth_addr_valid(char *str)
1951{
1952 if (strlen(str) == 17) {
1953 int i;
1954 char *p, *q;
1955 uchar ea[6];
1956
1957 /* see if it looks like an ethernet address */
1958
1959 p = str;
1960
1961 for (i = 0; i < 6; i++) {
1962 char term = (i == 5 ? '\0' : ':');
1963
1964 ea[i] = simple_strtol(p, &q, 16);
1965
1966 if ((q - p) != 2 || *q++ != term)
1967 break;
1968
1969 p = q;
1970 }
1971
1972 if (i == 6) /* it looks ok */
1973 return 1;
1974 }
1975 return 0;
1976}
1977
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001978static u8 nibble(unsigned char c)
Remy Bohmerdf063442009-07-29 18:18:43 +02001979{
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001980 if (likely(isdigit(c)))
Remy Bohmerdf063442009-07-29 18:18:43 +02001981 return c - '0';
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001982 c = toupper(c);
1983 if (likely(isxdigit(c)))
Remy Bohmerdf063442009-07-29 18:18:43 +02001984 return 10 + c - 'A';
1985 return 0;
1986}
1987
1988static int get_ether_addr(const char *str, u8 *dev_addr)
1989{
1990 if (str) {
1991 unsigned i;
1992
1993 for (i = 0; i < 6; i++) {
1994 unsigned char num;
1995
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001996 if ((*str == '.') || (*str == ':'))
Remy Bohmerdf063442009-07-29 18:18:43 +02001997 str++;
1998 num = nibble(*str++) << 4;
1999 num |= (nibble(*str++));
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002000 dev_addr[i] = num;
Remy Bohmerdf063442009-07-29 18:18:43 +02002001 }
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002002 if (is_valid_ether_addr(dev_addr))
Remy Bohmerdf063442009-07-29 18:18:43 +02002003 return 0;
2004 }
2005 return 1;
2006}
2007
2008static int eth_bind(struct usb_gadget *gadget)
2009{
2010 struct eth_dev *dev = &l_ethdev;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002011 u8 cdc = 1, zlp = 1, rndis = 1;
Remy Bohmerdf063442009-07-29 18:18:43 +02002012 struct usb_ep *in_ep, *out_ep, *status_ep = NULL;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002013 int status = -ENOMEM;
Remy Bohmerdf063442009-07-29 18:18:43 +02002014 int gcnum;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002015 u8 tmp[7];
Remy Bohmerdf063442009-07-29 18:18:43 +02002016
2017 /* these flags are only ever cleared; compiler take note */
2018#ifndef DEV_CONFIG_CDC
2019 cdc = 0;
2020#endif
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002021#ifndef CONFIG_USB_ETH_RNDIS
2022 rndis = 0;
2023#endif
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002024 /*
2025 * Because most host side USB stacks handle CDC Ethernet, that
Remy Bohmerdf063442009-07-29 18:18:43 +02002026 * standard protocol is _strongly_ preferred for interop purposes.
2027 * (By everyone except Microsoft.)
2028 */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002029 if (gadget_is_pxa(gadget)) {
Remy Bohmerdf063442009-07-29 18:18:43 +02002030 /* pxa doesn't support altsettings */
2031 cdc = 0;
2032 } else if (gadget_is_musbhdrc(gadget)) {
2033 /* reduce tx dma overhead by avoiding special cases */
2034 zlp = 0;
2035 } else if (gadget_is_sh(gadget)) {
2036 /* sh doesn't support multiple interfaces or configs */
2037 cdc = 0;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002038 rndis = 0;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002039 } else if (gadget_is_sa1100(gadget)) {
Remy Bohmerdf063442009-07-29 18:18:43 +02002040 /* hardware can't write zlps */
2041 zlp = 0;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002042 /*
2043 * sa1100 CAN do CDC, without status endpoint ... we use
Remy Bohmerdf063442009-07-29 18:18:43 +02002044 * non-CDC to be compatible with ARM Linux-2.4 "usb-eth".
2045 */
2046 cdc = 0;
2047 }
2048
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002049 gcnum = usb_gadget_controller_number(gadget);
Remy Bohmerdf063442009-07-29 18:18:43 +02002050 if (gcnum >= 0)
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002051 device_desc.bcdDevice = cpu_to_le16(0x0300 + gcnum);
Remy Bohmerdf063442009-07-29 18:18:43 +02002052 else {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002053 /*
2054 * can't assume CDC works. don't want to default to
Remy Bohmerdf063442009-07-29 18:18:43 +02002055 * anything less functional on CDC-capable hardware,
2056 * so we fail in this case.
2057 */
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04002058 error("controller '%s' not recognized",
Remy Bohmerdf063442009-07-29 18:18:43 +02002059 gadget->name);
2060 return -ENODEV;
2061 }
2062
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002063 /*
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002064 * If there's an RNDIS configuration, that's what Windows wants to
2065 * be using ... so use these product IDs here and in the "linux.inf"
2066 * needed to install MSFT drivers. Current Linux kernels will use
2067 * the second configuration if it's CDC Ethernet, and need some help
2068 * to choose the right configuration otherwise.
Remy Bohmerdf063442009-07-29 18:18:43 +02002069 */
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002070 if (rndis) {
2071#if defined(CONFIG_USB_RNDIS_VENDOR_ID) && defined(CONFIG_USB_RNDIS_PRODUCT_ID)
Remy Bohmerdf063442009-07-29 18:18:43 +02002072 device_desc.idVendor =
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002073 __constant_cpu_to_le16(CONFIG_USB_RNDIS_VENDOR_ID);
Remy Bohmerdf063442009-07-29 18:18:43 +02002074 device_desc.idProduct =
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002075 __constant_cpu_to_le16(CONFIG_USB_RNDIS_PRODUCT_ID);
2076#else
2077 device_desc.idVendor =
2078 __constant_cpu_to_le16(RNDIS_VENDOR_NUM);
2079 device_desc.idProduct =
2080 __constant_cpu_to_le16(RNDIS_PRODUCT_NUM);
2081#endif
2082 sprintf(product_desc, "RNDIS/%s", driver_desc);
Remy Bohmerdf063442009-07-29 18:18:43 +02002083
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002084 /*
2085 * CDC subset ... recognized by Linux since 2.4.10, but Windows
2086 * drivers aren't widely available. (That may be improved by
2087 * supporting one submode of the "SAFE" variant of MDLM.)
2088 */
2089 } else {
Remy Bohmerdf063442009-07-29 18:18:43 +02002090#if defined(CONFIG_USB_CDC_VENDOR_ID) && defined(CONFIG_USB_CDC_PRODUCT_ID)
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002091 device_desc.idVendor = cpu_to_le16(CONFIG_USB_CDC_VENDOR_ID);
2092 device_desc.idProduct = cpu_to_le16(CONFIG_USB_CDC_PRODUCT_ID);
2093#else
2094 if (!cdc) {
2095 device_desc.idVendor =
2096 __constant_cpu_to_le16(SIMPLE_VENDOR_NUM);
2097 device_desc.idProduct =
2098 __constant_cpu_to_le16(SIMPLE_PRODUCT_NUM);
2099 }
Remy Bohmerdf063442009-07-29 18:18:43 +02002100#endif
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002101 }
2102 /* support optional vendor/distro customization */
Remy Bohmerdf063442009-07-29 18:18:43 +02002103 if (bcdDevice)
2104 device_desc.bcdDevice = cpu_to_le16(bcdDevice);
2105 if (iManufacturer)
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002106 strlcpy(manufacturer, iManufacturer, sizeof manufacturer);
Remy Bohmerdf063442009-07-29 18:18:43 +02002107 if (iProduct)
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002108 strlcpy(product_desc, iProduct, sizeof product_desc);
Remy Bohmerdf063442009-07-29 18:18:43 +02002109 if (iSerialNumber) {
2110 device_desc.iSerialNumber = STRING_SERIALNUMBER,
Vitaly Kuzmichev214d7b02010-08-13 17:00:45 +04002111 strlcpy(serial_number, iSerialNumber, sizeof serial_number);
Remy Bohmerdf063442009-07-29 18:18:43 +02002112 }
2113
2114 /* all we really need is bulk IN/OUT */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002115 usb_ep_autoconfig_reset(gadget);
2116 in_ep = usb_ep_autoconfig(gadget, &fs_source_desc);
Remy Bohmerdf063442009-07-29 18:18:43 +02002117 if (!in_ep) {
2118autoconf_fail:
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04002119 error("can't autoconfigure on %s\n",
Remy Bohmerdf063442009-07-29 18:18:43 +02002120 gadget->name);
2121 return -ENODEV;
2122 }
2123 in_ep->driver_data = in_ep; /* claim */
2124
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002125 out_ep = usb_ep_autoconfig(gadget, &fs_sink_desc);
Remy Bohmerdf063442009-07-29 18:18:43 +02002126 if (!out_ep)
2127 goto autoconf_fail;
2128 out_ep->driver_data = out_ep; /* claim */
2129
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002130#if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002131 /*
2132 * CDC Ethernet control interface doesn't require a status endpoint.
Remy Bohmerdf063442009-07-29 18:18:43 +02002133 * Since some hosts expect one, try to allocate one anyway.
2134 */
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002135 if (cdc || rndis) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002136 status_ep = usb_ep_autoconfig(gadget, &fs_status_desc);
Remy Bohmerdf063442009-07-29 18:18:43 +02002137 if (status_ep) {
2138 status_ep->driver_data = status_ep; /* claim */
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002139 } else if (rndis) {
2140 error("can't run RNDIS on %s", gadget->name);
2141 return -ENODEV;
2142#ifdef DEV_CONFIG_CDC
Remy Bohmerdf063442009-07-29 18:18:43 +02002143 } else if (cdc) {
2144 control_intf.bNumEndpoints = 0;
2145 /* FIXME remove endpoint from descriptor list */
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002146#endif
Remy Bohmerdf063442009-07-29 18:18:43 +02002147 }
2148 }
2149#endif
2150
2151 /* one config: cdc, else minimal subset */
2152 if (!cdc) {
2153 eth_config.bNumInterfaces = 1;
2154 eth_config.iConfiguration = STRING_SUBSET;
2155
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002156 /*
2157 * use functions to set these up, in case we're built to work
Remy Bohmerdf063442009-07-29 18:18:43 +02002158 * with multiple controllers and must override CDC Ethernet.
2159 */
2160 fs_subset_descriptors();
2161 hs_subset_descriptors();
2162 }
2163
2164 device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002165 usb_gadget_set_selfpowered(gadget);
Remy Bohmerdf063442009-07-29 18:18:43 +02002166
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002167 /* For now RNDIS is always a second config */
2168 if (rndis)
2169 device_desc.bNumConfigurations = 2;
2170
Remy Bohmerdf063442009-07-29 18:18:43 +02002171 if (gadget_is_dualspeed(gadget)) {
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002172 if (rndis)
2173 dev_qualifier.bNumConfigurations = 2;
2174 else if (!cdc)
Remy Bohmerdf063442009-07-29 18:18:43 +02002175 dev_qualifier.bDeviceClass = USB_CLASS_VENDOR_SPEC;
2176
2177 /* assumes ep0 uses the same value for both speeds ... */
2178 dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0;
2179
2180 /* and that all endpoints are dual-speed */
2181 hs_source_desc.bEndpointAddress =
2182 fs_source_desc.bEndpointAddress;
2183 hs_sink_desc.bEndpointAddress =
2184 fs_sink_desc.bEndpointAddress;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002185#if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Remy Bohmerdf063442009-07-29 18:18:43 +02002186 if (status_ep)
2187 hs_status_desc.bEndpointAddress =
2188 fs_status_desc.bEndpointAddress;
2189#endif
2190 }
2191
2192 if (gadget_is_otg(gadget)) {
2193 otg_descriptor.bmAttributes |= USB_OTG_HNP,
2194 eth_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
2195 eth_config.bMaxPower = 4;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002196#ifdef CONFIG_USB_ETH_RNDIS
2197 rndis_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
2198 rndis_config.bMaxPower = 4;
2199#endif
Remy Bohmerdf063442009-07-29 18:18:43 +02002200 }
2201
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002202
2203 /* network device setup */
Remy Bohmerdf063442009-07-29 18:18:43 +02002204 dev->net = &l_netdev;
Remy Bohmerdf063442009-07-29 18:18:43 +02002205
2206 dev->cdc = cdc;
2207 dev->zlp = zlp;
2208
2209 dev->in_ep = in_ep;
2210 dev->out_ep = out_ep;
2211 dev->status_ep = status_ep;
2212
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002213 /*
2214 * Module params for these addresses should come from ID proms.
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002215 * The host side address is used with CDC and RNDIS, and commonly
Remy Bohmerdf063442009-07-29 18:18:43 +02002216 * ends up in a persistent config database. It's not clear if
2217 * host side code for the SAFE thing cares -- its original BLAN
2218 * thing didn't, Sharp never assigned those addresses on Zaurii.
2219 */
2220 get_ether_addr(dev_addr, dev->net->enetaddr);
2221
2222 memset(tmp, 0, sizeof(tmp));
2223 memcpy(tmp, dev->net->enetaddr, sizeof(dev->net->enetaddr));
2224
2225 get_ether_addr(host_addr, dev->host_mac);
2226
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002227 sprintf(ethaddr, "%02X%02X%02X%02X%02X%02X",
2228 dev->host_mac[0], dev->host_mac[1],
2229 dev->host_mac[2], dev->host_mac[3],
2230 dev->host_mac[4], dev->host_mac[5]);
Remy Bohmerdf063442009-07-29 18:18:43 +02002231
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002232 if (rndis) {
2233 status = rndis_init();
2234 if (status < 0) {
2235 error("can't init RNDIS, %d", status);
2236 goto fail;
2237 }
Remy Bohmerdf063442009-07-29 18:18:43 +02002238 }
2239
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002240 /*
2241 * use PKTSIZE (or aligned... from u-boot) and set
2242 * wMaxSegmentSize accordingly
2243 */
Remy Bohmerdf063442009-07-29 18:18:43 +02002244 dev->mtu = PKTSIZE_ALIGN; /* RNDIS does not like this, only 1514, TODO*/
2245
2246 /* preallocate control message data and buffer */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002247 dev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
Remy Bohmerdf063442009-07-29 18:18:43 +02002248 if (!dev->req)
2249 goto fail;
2250 dev->req->buf = control_req;
2251 dev->req->complete = eth_setup_complete;
2252
2253 /* ... and maybe likewise for status transfer */
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002254#if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Remy Bohmerdf063442009-07-29 18:18:43 +02002255 if (dev->status_ep) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002256 dev->stat_req = usb_ep_alloc_request(dev->status_ep,
2257 GFP_KERNEL);
Remy Bohmerdf063442009-07-29 18:18:43 +02002258 if (!dev->stat_req) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002259 usb_ep_free_request(dev->status_ep, dev->req);
Remy Bohmerdf063442009-07-29 18:18:43 +02002260
2261 goto fail;
2262 }
Vitaly Kuzmichevef8d8d72010-08-13 17:01:06 +04002263 dev->stat_req->buf = status_req;
Remy Bohmerdf063442009-07-29 18:18:43 +02002264 dev->stat_req->context = NULL;
2265 }
2266#endif
2267
2268 /* finish hookup to lower layer ... */
2269 dev->gadget = gadget;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002270 set_gadget_data(gadget, dev);
Remy Bohmerdf063442009-07-29 18:18:43 +02002271 gadget->ep0->driver_data = dev;
2272
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002273 /*
2274 * two kinds of host-initiated state changes:
Remy Bohmerdf063442009-07-29 18:18:43 +02002275 * - iff DATA transfer is active, carrier is "on"
2276 * - tx queueing enabled if open *and* carrier is "on"
2277 */
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002278
2279 printf("using %s, OUT %s IN %s%s%s\n", gadget->name,
2280 out_ep->name, in_ep->name,
2281 status_ep ? " STATUS " : "",
2282 status_ep ? status_ep->name : ""
2283 );
2284 printf("MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
2285 dev->net->enetaddr[0], dev->net->enetaddr[1],
2286 dev->net->enetaddr[2], dev->net->enetaddr[3],
2287 dev->net->enetaddr[4], dev->net->enetaddr[5]);
2288
2289 if (cdc || rndis)
2290 printf("HOST MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
2291 dev->host_mac[0], dev->host_mac[1],
2292 dev->host_mac[2], dev->host_mac[3],
2293 dev->host_mac[4], dev->host_mac[5]);
2294
2295 if (rndis) {
2296 u32 vendorID = 0;
2297
2298 /* FIXME RNDIS vendor id == "vendor NIC code" == ? */
2299
2300 dev->rndis_config = rndis_register(rndis_control_ack);
2301 if (dev->rndis_config < 0) {
2302fail0:
2303 eth_unbind(gadget);
2304 debug("RNDIS setup failed\n");
2305 status = -ENODEV;
2306 goto fail;
2307 }
2308
2309 /* these set up a lot of the OIDs that RNDIS needs */
2310 rndis_set_host_mac(dev->rndis_config, dev->host_mac);
2311 if (rndis_set_param_dev(dev->rndis_config, dev->net, dev->mtu,
2312 &dev->stats, &dev->cdc_filter))
2313 goto fail0;
2314 if (rndis_set_param_vendor(dev->rndis_config, vendorID,
2315 manufacturer))
2316 goto fail0;
2317 if (rndis_set_param_medium(dev->rndis_config,
2318 NDIS_MEDIUM_802_3, 0))
2319 goto fail0;
2320 printf("RNDIS ready\n");
2321 }
Remy Bohmerdf063442009-07-29 18:18:43 +02002322 return 0;
2323
2324fail:
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002325 error("%s failed, status = %d", __func__, status);
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002326 eth_unbind(gadget);
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002327 return status;
Remy Bohmerdf063442009-07-29 18:18:43 +02002328}
2329
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002330/*-------------------------------------------------------------------------*/
2331
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002332static int usb_eth_init(struct eth_device *netdev, bd_t *bd)
Remy Bohmerdf063442009-07-29 18:18:43 +02002333{
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002334 struct eth_dev *dev = &l_ethdev;
Remy Bohmerdf063442009-07-29 18:18:43 +02002335 struct usb_gadget *gadget;
2336 unsigned long ts;
2337 unsigned long timeout = USB_CONNECT_TIMEOUT;
2338
2339 if (!netdev) {
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04002340 error("received NULL ptr");
Remy Bohmerdf063442009-07-29 18:18:43 +02002341 goto fail;
2342 }
Vitaly Kuzmichevbe0afbf2010-12-28 16:59:32 +03002343
2344 /* Configure default mac-addresses for the USB ethernet device */
2345#ifdef CONFIG_USBNET_DEV_ADDR
2346 strlcpy(dev_addr, CONFIG_USBNET_DEV_ADDR, sizeof(dev_addr));
2347#endif
2348#ifdef CONFIG_USBNET_HOST_ADDR
2349 strlcpy(host_addr, CONFIG_USBNET_HOST_ADDR, sizeof(host_addr));
2350#endif
2351 /* Check if the user overruled the MAC addresses */
2352 if (getenv("usbnet_devaddr"))
2353 strlcpy(dev_addr, getenv("usbnet_devaddr"),
2354 sizeof(dev_addr));
2355
2356 if (getenv("usbnet_hostaddr"))
2357 strlcpy(host_addr, getenv("usbnet_hostaddr"),
2358 sizeof(host_addr));
2359
2360 if (!is_eth_addr_valid(dev_addr)) {
2361 error("Need valid 'usbnet_devaddr' to be set");
2362 goto fail;
2363 }
2364 if (!is_eth_addr_valid(host_addr)) {
2365 error("Need valid 'usbnet_hostaddr' to be set");
2366 goto fail;
2367 }
2368
Lei Wen4af32732010-12-01 23:43:43 +08002369 if (usb_gadget_register_driver(&eth_driver) < 0)
2370 goto fail;
Remy Bohmerdf063442009-07-29 18:18:43 +02002371
2372 dev->network_started = 0;
Remy Bohmerdf063442009-07-29 18:18:43 +02002373
2374 packet_received = 0;
2375 packet_sent = 0;
2376
2377 gadget = dev->gadget;
2378 usb_gadget_connect(gadget);
2379
2380 if (getenv("cdc_connect_timeout"))
2381 timeout = simple_strtoul(getenv("cdc_connect_timeout"),
2382 NULL, 10) * CONFIG_SYS_HZ;
2383 ts = get_timer(0);
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002384 while (!l_ethdev.network_started) {
Remy Bohmerdf063442009-07-29 18:18:43 +02002385 /* Handle control-c and timeouts */
2386 if (ctrlc() || (get_timer(ts) > timeout)) {
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04002387 error("The remote end did not respond in time.");
Remy Bohmerdf063442009-07-29 18:18:43 +02002388 goto fail;
2389 }
2390 usb_gadget_handle_interrupts();
2391 }
2392
Vitaly Kuzmicheve4bd3862010-09-22 13:13:56 +04002393 packet_received = 0;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002394 rx_submit(dev, dev->rx_req, 0);
Remy Bohmerdf063442009-07-29 18:18:43 +02002395 return 0;
2396fail:
2397 return -1;
2398}
2399
Joe Hershbergere4e04882012-05-22 18:36:19 +00002400static int usb_eth_send(struct eth_device *netdev, void *packet, int length)
Remy Bohmerdf063442009-07-29 18:18:43 +02002401{
2402 int retval;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002403 void *rndis_pkt = NULL;
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04002404 struct eth_dev *dev = &l_ethdev;
Vitaly Kuzmichev2907b2c2010-09-22 13:13:55 +04002405 struct usb_request *req = dev->tx_req;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002406 unsigned long ts;
2407 unsigned long timeout = USB_CONNECT_TIMEOUT;
Remy Bohmerdf063442009-07-29 18:18:43 +02002408
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04002409 debug("%s:...\n", __func__);
Remy Bohmerdf063442009-07-29 18:18:43 +02002410
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002411 /* new buffer is needed to include RNDIS header */
2412 if (rndis_active(dev)) {
2413 rndis_pkt = malloc(length +
2414 sizeof(struct rndis_packet_msg_type));
2415 if (!rndis_pkt) {
2416 error("No memory to alloc RNDIS packet");
2417 goto drop;
2418 }
2419 rndis_add_hdr(rndis_pkt, length);
2420 memcpy(rndis_pkt + sizeof(struct rndis_packet_msg_type),
Joe Hershbergere4e04882012-05-22 18:36:19 +00002421 packet, length);
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002422 packet = rndis_pkt;
2423 length += sizeof(struct rndis_packet_msg_type);
2424 }
Joe Hershbergere4e04882012-05-22 18:36:19 +00002425 req->buf = packet;
Remy Bohmerdf063442009-07-29 18:18:43 +02002426 req->context = NULL;
2427 req->complete = tx_complete;
2428
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002429 /*
2430 * use zlp framing on tx for strict CDC-Ether conformance,
Remy Bohmerdf063442009-07-29 18:18:43 +02002431 * though any robust network rx path ignores extra padding.
2432 * and some hardware doesn't like to write zlps.
2433 */
2434 req->zero = 1;
2435 if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0)
2436 length++;
2437
2438 req->length = length;
2439#if 0
2440 /* throttle highspeed IRQ rate back slightly */
2441 if (gadget_is_dualspeed(dev->gadget))
2442 req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH)
2443 ? ((dev->tx_qlen % qmult) != 0) : 0;
2444#endif
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002445 dev->tx_qlen = 1;
2446 ts = get_timer(0);
2447 packet_sent = 0;
Remy Bohmerdf063442009-07-29 18:18:43 +02002448
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002449 retval = usb_ep_queue(dev->in_ep, req, GFP_ATOMIC);
Remy Bohmerdf063442009-07-29 18:18:43 +02002450
2451 if (!retval)
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04002452 debug("%s: packet queued\n", __func__);
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002453 while (!packet_sent) {
2454 if (get_timer(ts) > timeout) {
2455 printf("timeout sending packets to usb ethernet\n");
2456 return -1;
2457 }
2458 usb_gadget_handle_interrupts();
Remy Bohmerdf063442009-07-29 18:18:43 +02002459 }
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002460 if (rndis_pkt)
2461 free(rndis_pkt);
Remy Bohmerdf063442009-07-29 18:18:43 +02002462
2463 return 0;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002464drop:
2465 dev->stats.tx_dropped++;
2466 return -ENOMEM;
Remy Bohmerdf063442009-07-29 18:18:43 +02002467}
2468
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002469static int usb_eth_recv(struct eth_device *netdev)
Remy Bohmerdf063442009-07-29 18:18:43 +02002470{
2471 struct eth_dev *dev = &l_ethdev;
2472
2473 usb_gadget_handle_interrupts();
2474
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002475 if (packet_received) {
2476 debug("%s: packet received\n", __func__);
2477 if (dev->rx_req) {
2478 NetReceive(NetRxPackets[0], dev->rx_req->length);
2479 packet_received = 0;
Remy Bohmerdf063442009-07-29 18:18:43 +02002480
Vitaly Kuzmichev2907b2c2010-09-22 13:13:55 +04002481 rx_submit(dev, dev->rx_req, 0);
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002482 } else
2483 error("dev->rx_req invalid");
Remy Bohmerdf063442009-07-29 18:18:43 +02002484 }
2485 return 0;
2486}
2487
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002488void usb_eth_halt(struct eth_device *netdev)
Remy Bohmerdf063442009-07-29 18:18:43 +02002489{
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002490 struct eth_dev *dev = &l_ethdev;
Remy Bohmerdf063442009-07-29 18:18:43 +02002491
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002492 if (!netdev) {
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04002493 error("received NULL ptr");
Remy Bohmerdf063442009-07-29 18:18:43 +02002494 return;
2495 }
2496
Lei Wen4af32732010-12-01 23:43:43 +08002497 /* If the gadget not registered, simple return */
2498 if (!dev->gadget)
2499 return;
2500
Vitaly Kuzmichev3433c332011-02-11 18:18:35 +03002501 /*
2502 * Some USB controllers may need additional deinitialization here
2503 * before dropping pull-up (also due to hardware issues).
2504 * For example: unhandled interrupt with status stage started may
2505 * bring the controller to fully broken state (until board reset).
2506 * There are some variants to debug and fix such cases:
2507 * 1) In the case of RNDIS connection eth_stop can perform additional
2508 * interrupt handling. See RNDIS_COMPLETE_SIGNAL_DISCONNECT definition.
2509 * 2) 'pullup' callback in your UDC driver can be improved to perform
2510 * this deinitialization.
2511 */
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002512 eth_stop(dev);
2513
Remy Bohmerdf063442009-07-29 18:18:43 +02002514 usb_gadget_disconnect(dev->gadget);
Vitaly Kuzmichev08d4dd72011-02-11 18:18:31 +03002515
2516 /* Clear pending interrupt */
2517 if (dev->network_started) {
2518 usb_gadget_handle_interrupts();
2519 dev->network_started = 0;
2520 }
2521
Lei Wen4af32732010-12-01 23:43:43 +08002522 usb_gadget_unregister_driver(&eth_driver);
Remy Bohmerdf063442009-07-29 18:18:43 +02002523}
2524
2525static struct usb_gadget_driver eth_driver = {
2526 .speed = DEVSPEED,
2527
2528 .bind = eth_bind,
2529 .unbind = eth_unbind,
2530
2531 .setup = eth_setup,
2532 .disconnect = eth_disconnect,
2533
2534 .suspend = eth_suspend,
2535 .resume = eth_resume,
2536};
2537
2538int usb_eth_initialize(bd_t *bi)
2539{
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002540 struct eth_device *netdev = &l_netdev;
Remy Bohmerdf063442009-07-29 18:18:43 +02002541
Vitaly Kuzmicheva36cff12010-12-28 16:59:31 +03002542 strlcpy(netdev->name, USB_NET_NAME, sizeof(netdev->name));
Remy Bohmerdf063442009-07-29 18:18:43 +02002543
2544 netdev->init = usb_eth_init;
2545 netdev->send = usb_eth_send;
2546 netdev->recv = usb_eth_recv;
2547 netdev->halt = usb_eth_halt;
2548
2549#ifdef CONFIG_MCAST_TFTP
2550 #error not supported
2551#endif
Remy Bohmerdf063442009-07-29 18:18:43 +02002552 eth_register(netdev);
2553 return 0;
Remy Bohmerdf063442009-07-29 18:18:43 +02002554}