blob: de880ffeb8a6e9c1821ce88ada68134f214fa410 [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>
27#include <linux/usb/cdc.h>
28#include <linux/usb/gadget.h>
29#include <net.h>
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +030030#include <malloc.h>
Remy Bohmerdf063442009-07-29 18:18:43 +020031#include <linux/ctype.h>
32
33#include "gadget_chips.h"
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +030034#include "rndis.h"
Remy Bohmerdf063442009-07-29 18:18:43 +020035
Vitaly Kuzmicheva36cff12010-12-28 16:59:31 +030036#define USB_NET_NAME "usb_ether"
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +040037
Remy Bohmerdf063442009-07-29 18:18:43 +020038#define atomic_read
39extern struct platform_data brd;
40#define spin_lock(x)
41#define spin_unlock(x)
42
43
44unsigned packet_received, packet_sent;
45
Remy Bohmerdf063442009-07-29 18:18:43 +020046#define GFP_ATOMIC ((gfp_t) 0)
47#define GFP_KERNEL ((gfp_t) 0)
48
49/*
50 * Ethernet gadget driver -- with CDC and non-CDC options
51 * Builds on hardware support for a full duplex link.
52 *
53 * CDC Ethernet is the standard USB solution for sending Ethernet frames
54 * using USB. Real hardware tends to use the same framing protocol but look
55 * different for control features. This driver strongly prefers to use
56 * this USB-IF standard as its open-systems interoperability solution;
57 * most host side USB stacks (except from Microsoft) support it.
58 *
59 * This is sometimes called "CDC ECM" (Ethernet Control Model) to support
60 * TLA-soup. "CDC ACM" (Abstract Control Model) is for modems, and a new
61 * "CDC EEM" (Ethernet Emulation Model) is starting to spread.
62 *
63 * There's some hardware that can't talk CDC ECM. We make that hardware
64 * implement a "minimalist" vendor-agnostic CDC core: same framing, but
65 * link-level setup only requires activating the configuration. Only the
66 * endpoint descriptors, and product/vendor IDs, are relevant; no control
67 * operations are available. Linux supports it, but other host operating
68 * systems may not. (This is a subset of CDC Ethernet.)
69 *
70 * It turns out that if you add a few descriptors to that "CDC Subset",
71 * (Windows) host side drivers from MCCI can treat it as one submode of
72 * a proprietary scheme called "SAFE" ... without needing to know about
73 * specific product/vendor IDs. So we do that, making it easier to use
74 * those MS-Windows drivers. Those added descriptors make it resemble a
75 * CDC MDLM device, but they don't change device behavior at all. (See
76 * MCCI Engineering report 950198 "SAFE Networking Functions".)
77 *
78 * A third option is also in use. Rather than CDC Ethernet, or something
79 * simpler, Microsoft pushes their own approach: RNDIS. The published
80 * RNDIS specs are ambiguous and appear to be incomplete, and are also
81 * needlessly complex. They borrow more from CDC ACM than CDC ECM.
82 */
83#define ETH_ALEN 6 /* Octets in one ethernet addr */
84#define ETH_HLEN 14 /* Total octets in header. */
85#define ETH_ZLEN 60 /* Min. octets in frame sans FCS */
86#define ETH_DATA_LEN 1500 /* Max. octets in payload */
87#define ETH_FRAME_LEN PKTSIZE_ALIGN /* Max. octets in frame sans FCS */
88#define ETH_FCS_LEN 4 /* Octets in the FCS */
89
90#define DRIVER_DESC "Ethernet Gadget"
91/* Based on linux 2.6.27 version */
92#define DRIVER_VERSION "May Day 2005"
93
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +040094static const char shortname[] = "ether";
95static const char driver_desc[] = DRIVER_DESC;
Remy Bohmerdf063442009-07-29 18:18:43 +020096
97#define RX_EXTRA 20 /* guard against rx overflows */
98
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +030099#ifndef CONFIG_USB_ETH_RNDIS
100#define rndis_uninit(x) do {} while (0)
101#define rndis_deregister(c) do {} while (0)
102#define rndis_exit() do {} while (0)
103#endif
104
105/* CDC and RNDIS support the same host-chosen outgoing packet filters. */
Remy Bohmerdf063442009-07-29 18:18:43 +0200106#define DEFAULT_FILTER (USB_CDC_PACKET_TYPE_BROADCAST \
107 |USB_CDC_PACKET_TYPE_ALL_MULTICAST \
108 |USB_CDC_PACKET_TYPE_PROMISCUOUS \
109 |USB_CDC_PACKET_TYPE_DIRECTED)
110
111#define USB_CONNECT_TIMEOUT (3 * CONFIG_SYS_HZ)
112
113/*-------------------------------------------------------------------------*/
Vitaly Kuzmichev737f6b22011-02-11 18:18:33 +0300114
115struct eth_dev {
116 struct usb_gadget *gadget;
117 struct usb_request *req; /* for control responses */
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300118 struct usb_request *stat_req; /* for cdc & rndis status */
Vitaly Kuzmichev737f6b22011-02-11 18:18:33 +0300119
120 u8 config;
121 struct usb_ep *in_ep, *out_ep, *status_ep;
122 const struct usb_endpoint_descriptor
123 *in, *out, *status;
124
125 struct usb_request *tx_req, *rx_req;
126
127 struct eth_device *net;
128 struct net_device_stats stats;
129 unsigned int tx_qlen;
130
131 unsigned zlp:1;
132 unsigned cdc:1;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300133 unsigned rndis:1;
Vitaly Kuzmichev737f6b22011-02-11 18:18:33 +0300134 unsigned suspended:1;
135 unsigned network_started:1;
136 u16 cdc_filter;
137 unsigned long todo;
138 int mtu;
139#define WORK_RX_MEMORY 0
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300140 int rndis_config;
Vitaly Kuzmichev737f6b22011-02-11 18:18:33 +0300141 u8 host_mac[ETH_ALEN];
142};
143
144/*
145 * This version autoconfigures as much as possible at run-time.
146 *
147 * It also ASSUMES a self-powered device, without remote wakeup,
148 * although remote wakeup support would make sense.
149 */
150
151/*-------------------------------------------------------------------------*/
Remy Bohmerdf063442009-07-29 18:18:43 +0200152static struct eth_dev l_ethdev;
153static struct eth_device l_netdev;
154static struct usb_gadget_driver eth_driver;
155
156/*-------------------------------------------------------------------------*/
157
158/* "main" config is either CDC, or its simple subset */
159static inline int is_cdc(struct eth_dev *dev)
160{
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200161#if !defined(CONFIG_USB_ETH_SUBSET)
Remy Bohmerdf063442009-07-29 18:18:43 +0200162 return 1; /* only cdc possible */
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200163#elif !defined(CONFIG_USB_ETH_CDC)
Remy Bohmerdf063442009-07-29 18:18:43 +0200164 return 0; /* only subset possible */
165#else
166 return dev->cdc; /* depends on what hardware we found */
167#endif
168}
169
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300170/* "secondary" RNDIS config may sometimes be activated */
171static inline int rndis_active(struct eth_dev *dev)
172{
173#ifdef CONFIG_USB_ETH_RNDIS
174 return dev->rndis;
175#else
176 return 0;
177#endif
178}
179
180#define subset_active(dev) (!is_cdc(dev) && !rndis_active(dev))
181#define cdc_active(dev) (is_cdc(dev) && !rndis_active(dev))
Remy Bohmerdf063442009-07-29 18:18:43 +0200182
183#define DEFAULT_QLEN 2 /* double buffering by default */
184
185/* peak bulk transfer bits-per-second */
186#define HS_BPS (13 * 512 * 8 * 1000 * 8)
187#define FS_BPS (19 * 64 * 1 * 1000 * 8)
188
189#ifdef CONFIG_USB_GADGET_DUALSPEED
190#define DEVSPEED USB_SPEED_HIGH
191
Vitaly Kuzmichevaec83b82010-08-12 16:44:40 +0400192#ifdef CONFIG_USB_ETH_QMULT
193#define qmult CONFIG_USB_ETH_QMULT
194#else
195#define qmult 5
196#endif
197
Remy Bohmerdf063442009-07-29 18:18:43 +0200198/* for dual-speed hardware, use deeper queues at highspeed */
199#define qlen(gadget) \
200 (DEFAULT_QLEN*((gadget->speed == USB_SPEED_HIGH) ? qmult : 1))
201
202static inline int BITRATE(struct usb_gadget *g)
203{
204 return (g->speed == USB_SPEED_HIGH) ? HS_BPS : FS_BPS;
205}
206
207#else /* full speed (low speed doesn't do bulk) */
208
209#define qmult 1
210
211#define DEVSPEED USB_SPEED_FULL
212
213#define qlen(gadget) DEFAULT_QLEN
214
215static inline int BITRATE(struct usb_gadget *g)
216{
217 return FS_BPS;
218}
219#endif
220
Remy Bohmerdf063442009-07-29 18:18:43 +0200221/*-------------------------------------------------------------------------*/
222
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400223/*
224 * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
Remy Bohmerdf063442009-07-29 18:18:43 +0200225 * Instead: allocate your own, using normal USB-IF procedures.
226 */
227
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400228/*
229 * Thanks to NetChip Technologies for donating this product ID.
Remy Bohmerdf063442009-07-29 18:18:43 +0200230 * It's for devices with only CDC Ethernet configurations.
231 */
232#define CDC_VENDOR_NUM 0x0525 /* NetChip */
233#define CDC_PRODUCT_NUM 0xa4a1 /* Linux-USB Ethernet Gadget */
234
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400235/*
236 * For hardware that can't talk CDC, we use the same vendor ID that
Remy Bohmerdf063442009-07-29 18:18:43 +0200237 * ARM Linux has used for ethernet-over-usb, both with sa1100 and
238 * with pxa250. We're protocol-compatible, if the host-side drivers
239 * use the endpoint descriptors. bcdDevice (version) is nonzero, so
240 * drivers that need to hard-wire endpoint numbers have a hook.
241 *
242 * The protocol is a minimal subset of CDC Ether, which works on any bulk
243 * hardware that's not deeply broken ... even on hardware that can't talk
244 * RNDIS (like SA-1100, with no interrupt endpoint, or anything that
245 * doesn't handle control-OUT).
246 */
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300247#define SIMPLE_VENDOR_NUM 0x049f /* Compaq Computer Corp. */
248#define SIMPLE_PRODUCT_NUM 0x505a /* Linux-USB "CDC Subset" Device */
Remy Bohmerdf063442009-07-29 18:18:43 +0200249
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400250/*
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300251 * For hardware that can talk RNDIS and either of the above protocols,
252 * use this ID ... the windows INF files will know it. Unless it's
253 * used with CDC Ethernet, Linux 2.4 hosts will need updates to choose
254 * the non-RNDIS configuration.
255 */
256#define RNDIS_VENDOR_NUM 0x0525 /* NetChip */
257#define RNDIS_PRODUCT_NUM 0xa4a2 /* Ethernet/RNDIS Gadget */
258
259/*
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400260 * Some systems will want different product identifers published in the
Remy Bohmerdf063442009-07-29 18:18:43 +0200261 * device descriptor, either numbers or strings or both. These string
262 * parameters are in UTF-8 (superset of ASCII's 7 bit characters).
263 */
264
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300265/*
266 * Emulating them in eth_bind:
267 * static ushort idVendor;
268 * static ushort idProduct;
269 */
270
Remy Bohmerdf063442009-07-29 18:18:43 +0200271#if defined(CONFIG_USBNET_MANUFACTURER)
272static char *iManufacturer = CONFIG_USBNET_MANUFACTURER;
273#else
274static char *iManufacturer = "U-boot";
275#endif
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300276
277/* These probably need to be configurable. */
278static ushort bcdDevice;
Remy Bohmerdf063442009-07-29 18:18:43 +0200279static char *iProduct;
280static char *iSerialNumber;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300281
Remy Bohmerdf063442009-07-29 18:18:43 +0200282static char dev_addr[18];
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300283
Remy Bohmerdf063442009-07-29 18:18:43 +0200284static char host_addr[18];
285
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300286
Remy Bohmerdf063442009-07-29 18:18:43 +0200287/*-------------------------------------------------------------------------*/
288
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400289/*
290 * USB DRIVER HOOKUP (to the hardware driver, below us), mostly
Remy Bohmerdf063442009-07-29 18:18:43 +0200291 * ep0 implementation: descriptors, config management, setup().
292 * also optional class-specific notification interrupt transfer.
293 */
294
295/*
296 * DESCRIPTORS ... most are static, but strings and (full) configuration
297 * descriptors are built on demand. For now we do either full CDC, or
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300298 * our simple subset, with RNDIS as an optional second configuration.
299 *
300 * RNDIS includes some CDC ACM descriptors ... like CDC Ethernet. But
301 * the class descriptors match a modem (they're ignored; it's really just
302 * Ethernet functionality), they don't need the NOP altsetting, and the
303 * status transfer endpoint isn't optional.
Remy Bohmerdf063442009-07-29 18:18:43 +0200304 */
305
306#define STRING_MANUFACTURER 1
307#define STRING_PRODUCT 2
308#define STRING_ETHADDR 3
309#define STRING_DATA 4
310#define STRING_CONTROL 5
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300311#define STRING_RNDIS_CONTROL 6
Remy Bohmerdf063442009-07-29 18:18:43 +0200312#define STRING_CDC 7
313#define STRING_SUBSET 8
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300314#define STRING_RNDIS 9
Remy Bohmerdf063442009-07-29 18:18:43 +0200315#define STRING_SERIALNUMBER 10
316
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300317/* holds our biggest descriptor (or RNDIS response) */
Remy Bohmerdf063442009-07-29 18:18:43 +0200318#define USB_BUFSIZ 256
319
320/*
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300321 * This device advertises one configuration, eth_config, unless RNDIS
322 * is enabled (rndis_config) on hardware supporting at least two configs.
323 *
324 * NOTE: Controllers like superh_udc should probably be able to use
325 * an RNDIS-only configuration.
Remy Bohmerdf063442009-07-29 18:18:43 +0200326 *
327 * FIXME define some higher-powered configurations to make it easier
328 * to recharge batteries ...
329 */
330
331#define DEV_CONFIG_VALUE 1 /* cdc or subset */
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300332#define DEV_RNDIS_CONFIG_VALUE 2 /* rndis; optional */
Remy Bohmerdf063442009-07-29 18:18:43 +0200333
334static struct usb_device_descriptor
335device_desc = {
336 .bLength = sizeof device_desc,
337 .bDescriptorType = USB_DT_DEVICE,
338
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400339 .bcdUSB = __constant_cpu_to_le16(0x0200),
Remy Bohmerdf063442009-07-29 18:18:43 +0200340
341 .bDeviceClass = USB_CLASS_COMM,
342 .bDeviceSubClass = 0,
343 .bDeviceProtocol = 0,
344
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400345 .idVendor = __constant_cpu_to_le16(CDC_VENDOR_NUM),
346 .idProduct = __constant_cpu_to_le16(CDC_PRODUCT_NUM),
Remy Bohmerdf063442009-07-29 18:18:43 +0200347 .iManufacturer = STRING_MANUFACTURER,
348 .iProduct = STRING_PRODUCT,
349 .bNumConfigurations = 1,
350};
351
352static struct usb_otg_descriptor
353otg_descriptor = {
354 .bLength = sizeof otg_descriptor,
355 .bDescriptorType = USB_DT_OTG,
356
357 .bmAttributes = USB_OTG_SRP,
358};
359
360static struct usb_config_descriptor
361eth_config = {
362 .bLength = sizeof eth_config,
363 .bDescriptorType = USB_DT_CONFIG,
364
365 /* compute wTotalLength on the fly */
366 .bNumInterfaces = 2,
367 .bConfigurationValue = DEV_CONFIG_VALUE,
368 .iConfiguration = STRING_CDC,
369 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
370 .bMaxPower = 1,
371};
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300372
373#ifdef CONFIG_USB_ETH_RNDIS
374static struct usb_config_descriptor
375rndis_config = {
376 .bLength = sizeof rndis_config,
377 .bDescriptorType = USB_DT_CONFIG,
378
379 /* compute wTotalLength on the fly */
380 .bNumInterfaces = 2,
381 .bConfigurationValue = DEV_RNDIS_CONFIG_VALUE,
382 .iConfiguration = STRING_RNDIS,
383 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
384 .bMaxPower = 1,
385};
386#endif
Remy Bohmerdf063442009-07-29 18:18:43 +0200387
388/*
389 * Compared to the simple CDC subset, the full CDC Ethernet model adds
390 * three class descriptors, two interface descriptors, optional status
391 * endpoint. Both have a "data" interface and two bulk endpoints.
392 * There are also differences in how control requests are handled.
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300393 *
394 * RNDIS shares a lot with CDC-Ethernet, since it's a variant of the
395 * CDC-ACM (modem) spec. Unfortunately MSFT's RNDIS driver is buggy; it
396 * may hang or oops. Since bugfixes (or accurate specs, letting Linux
397 * work around those bugs) are unlikely to ever come from MSFT, you may
398 * wish to avoid using RNDIS.
399 *
400 * MCCI offers an alternative to RNDIS if you need to connect to Windows
401 * but have hardware that can't support CDC Ethernet. We add descriptors
402 * to present the CDC Subset as a (nonconformant) CDC MDLM variant called
403 * "SAFE". That borrows from both CDC Ethernet and CDC MDLM. You can
404 * get those drivers from MCCI, or bundled with various products.
Remy Bohmerdf063442009-07-29 18:18:43 +0200405 */
406
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200407#ifdef CONFIG_USB_ETH_CDC
Remy Bohmerdf063442009-07-29 18:18:43 +0200408static struct usb_interface_descriptor
409control_intf = {
410 .bLength = sizeof control_intf,
411 .bDescriptorType = USB_DT_INTERFACE,
412
413 .bInterfaceNumber = 0,
414 /* status endpoint is optional; this may be patched later */
415 .bNumEndpoints = 1,
416 .bInterfaceClass = USB_CLASS_COMM,
417 .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET,
418 .bInterfaceProtocol = USB_CDC_PROTO_NONE,
419 .iInterface = STRING_CONTROL,
420};
421#endif
422
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300423#ifdef CONFIG_USB_ETH_RNDIS
424static const struct usb_interface_descriptor
425rndis_control_intf = {
426 .bLength = sizeof rndis_control_intf,
427 .bDescriptorType = USB_DT_INTERFACE,
428
429 .bInterfaceNumber = 0,
430 .bNumEndpoints = 1,
431 .bInterfaceClass = USB_CLASS_COMM,
432 .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
433 .bInterfaceProtocol = USB_CDC_ACM_PROTO_VENDOR,
434 .iInterface = STRING_RNDIS_CONTROL,
435};
436#endif
437
Remy Bohmerdf063442009-07-29 18:18:43 +0200438static const struct usb_cdc_header_desc header_desc = {
439 .bLength = sizeof header_desc,
440 .bDescriptorType = USB_DT_CS_INTERFACE,
441 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
442
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400443 .bcdCDC = __constant_cpu_to_le16(0x0110),
Remy Bohmerdf063442009-07-29 18:18:43 +0200444};
445
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200446#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Remy Bohmerdf063442009-07-29 18:18:43 +0200447
448static const struct usb_cdc_union_desc union_desc = {
449 .bLength = sizeof union_desc,
450 .bDescriptorType = USB_DT_CS_INTERFACE,
451 .bDescriptorSubType = USB_CDC_UNION_TYPE,
452
453 .bMasterInterface0 = 0, /* index of control interface */
454 .bSlaveInterface0 = 1, /* index of DATA interface */
455};
456
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300457#endif /* CDC || RNDIS */
458
459#ifdef CONFIG_USB_ETH_RNDIS
460
461static const struct usb_cdc_call_mgmt_descriptor call_mgmt_descriptor = {
462 .bLength = sizeof call_mgmt_descriptor,
463 .bDescriptorType = USB_DT_CS_INTERFACE,
464 .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE,
465
466 .bmCapabilities = 0x00,
467 .bDataInterface = 0x01,
468};
469
470static const struct usb_cdc_acm_descriptor acm_descriptor = {
471 .bLength = sizeof acm_descriptor,
472 .bDescriptorType = USB_DT_CS_INTERFACE,
473 .bDescriptorSubType = USB_CDC_ACM_TYPE,
474
475 .bmCapabilities = 0x00,
476};
477
478#endif
Remy Bohmerdf063442009-07-29 18:18:43 +0200479
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200480#ifndef CONFIG_USB_ETH_CDC
Remy Bohmerdf063442009-07-29 18:18:43 +0200481
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400482/*
483 * "SAFE" loosely follows CDC WMC MDLM, violating the spec in various
Remy Bohmerdf063442009-07-29 18:18:43 +0200484 * ways: data endpoints live in the control interface, there's no data
485 * interface, and it's not used to talk to a cell phone radio.
486 */
487
488static const struct usb_cdc_mdlm_desc mdlm_desc = {
489 .bLength = sizeof mdlm_desc,
490 .bDescriptorType = USB_DT_CS_INTERFACE,
491 .bDescriptorSubType = USB_CDC_MDLM_TYPE,
492
493 .bcdVersion = __constant_cpu_to_le16(0x0100),
494 .bGUID = {
495 0x5d, 0x34, 0xcf, 0x66, 0x11, 0x18, 0x11, 0xd6,
496 0xa2, 0x1a, 0x00, 0x01, 0x02, 0xca, 0x9a, 0x7f,
497 },
498};
499
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400500/*
501 * since "usb_cdc_mdlm_detail_desc" is a variable length structure, we
Remy Bohmerdf063442009-07-29 18:18:43 +0200502 * can't really use its struct. All we do here is say that we're using
503 * the submode of "SAFE" which directly matches the CDC Subset.
504 */
505static const u8 mdlm_detail_desc[] = {
506 6,
507 USB_DT_CS_INTERFACE,
508 USB_CDC_MDLM_DETAIL_TYPE,
509
510 0, /* "SAFE" */
511 0, /* network control capabilities (none) */
512 0, /* network data capabilities ("raw" encapsulation) */
513};
514
515#endif
516
Remy Bohmerdf063442009-07-29 18:18:43 +0200517static const struct usb_cdc_ether_desc ether_desc = {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400518 .bLength = sizeof(ether_desc),
Remy Bohmerdf063442009-07-29 18:18:43 +0200519 .bDescriptorType = USB_DT_CS_INTERFACE,
520 .bDescriptorSubType = USB_CDC_ETHERNET_TYPE,
521
522 /* this descriptor actually adds value, surprise! */
523 .iMACAddress = STRING_ETHADDR,
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400524 .bmEthernetStatistics = __constant_cpu_to_le32(0), /* no statistics */
525 .wMaxSegmentSize = __constant_cpu_to_le16(ETH_FRAME_LEN),
526 .wNumberMCFilters = __constant_cpu_to_le16(0),
Remy Bohmerdf063442009-07-29 18:18:43 +0200527 .bNumberPowerFilters = 0,
528};
529
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200530#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Remy Bohmerdf063442009-07-29 18:18:43 +0200531
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400532/*
533 * include the status endpoint if we can, even where it's optional.
Remy Bohmerdf063442009-07-29 18:18:43 +0200534 * use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one
535 * packet, to simplify cancellation; and a big transfer interval, to
536 * waste less bandwidth.
537 *
538 * some drivers (like Linux 2.4 cdc-ether!) "need" it to exist even
539 * if they ignore the connect/disconnect notifications that real aether
540 * can provide. more advanced cdc configurations might want to support
541 * encapsulated commands (vendor-specific, using control-OUT).
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300542 *
543 * RNDIS requires the status endpoint, since it uses that encapsulation
544 * mechanism for its funky RPC scheme.
Remy Bohmerdf063442009-07-29 18:18:43 +0200545 */
546
547#define LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */
548#define STATUS_BYTECOUNT 16 /* 8 byte header + data */
549
550static struct usb_endpoint_descriptor
551fs_status_desc = {
552 .bLength = USB_DT_ENDPOINT_SIZE,
553 .bDescriptorType = USB_DT_ENDPOINT,
554
555 .bEndpointAddress = USB_DIR_IN,
556 .bmAttributes = USB_ENDPOINT_XFER_INT,
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400557 .wMaxPacketSize = __constant_cpu_to_le16(STATUS_BYTECOUNT),
Remy Bohmerdf063442009-07-29 18:18:43 +0200558 .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC,
559};
560#endif
561
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200562#ifdef CONFIG_USB_ETH_CDC
Remy Bohmerdf063442009-07-29 18:18:43 +0200563
564/* the default data interface has no endpoints ... */
565
566static const struct usb_interface_descriptor
567data_nop_intf = {
568 .bLength = sizeof data_nop_intf,
569 .bDescriptorType = USB_DT_INTERFACE,
570
571 .bInterfaceNumber = 1,
572 .bAlternateSetting = 0,
573 .bNumEndpoints = 0,
574 .bInterfaceClass = USB_CLASS_CDC_DATA,
575 .bInterfaceSubClass = 0,
576 .bInterfaceProtocol = 0,
577};
578
579/* ... but the "real" data interface has two bulk endpoints */
580
581static const struct usb_interface_descriptor
582data_intf = {
583 .bLength = sizeof data_intf,
584 .bDescriptorType = USB_DT_INTERFACE,
585
586 .bInterfaceNumber = 1,
587 .bAlternateSetting = 1,
588 .bNumEndpoints = 2,
589 .bInterfaceClass = USB_CLASS_CDC_DATA,
590 .bInterfaceSubClass = 0,
591 .bInterfaceProtocol = 0,
592 .iInterface = STRING_DATA,
593};
594
595#endif
596
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300597#ifdef CONFIG_USB_ETH_RNDIS
598
599/* RNDIS doesn't activate by changing to the "real" altsetting */
600
601static const struct usb_interface_descriptor
602rndis_data_intf = {
603 .bLength = sizeof rndis_data_intf,
604 .bDescriptorType = USB_DT_INTERFACE,
605
606 .bInterfaceNumber = 1,
607 .bAlternateSetting = 0,
608 .bNumEndpoints = 2,
609 .bInterfaceClass = USB_CLASS_CDC_DATA,
610 .bInterfaceSubClass = 0,
611 .bInterfaceProtocol = 0,
612 .iInterface = STRING_DATA,
613};
614
615#endif
616
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200617#ifdef CONFIG_USB_ETH_SUBSET
Remy Bohmerdf063442009-07-29 18:18:43 +0200618
619/*
620 * "Simple" CDC-subset option is a simple vendor-neutral model that most
621 * full speed controllers can handle: one interface, two bulk endpoints.
622 *
623 * To assist host side drivers, we fancy it up a bit, and add descriptors
624 * so some host side drivers will understand it as a "SAFE" variant.
625 */
626
627static const struct usb_interface_descriptor
628subset_data_intf = {
629 .bLength = sizeof subset_data_intf,
630 .bDescriptorType = USB_DT_INTERFACE,
631
632 .bInterfaceNumber = 0,
633 .bAlternateSetting = 0,
634 .bNumEndpoints = 2,
635 .bInterfaceClass = USB_CLASS_COMM,
636 .bInterfaceSubClass = USB_CDC_SUBCLASS_MDLM,
637 .bInterfaceProtocol = 0,
638 .iInterface = STRING_DATA,
639};
640
641#endif /* SUBSET */
642
Remy Bohmerdf063442009-07-29 18:18:43 +0200643static struct usb_endpoint_descriptor
644fs_source_desc = {
645 .bLength = USB_DT_ENDPOINT_SIZE,
646 .bDescriptorType = USB_DT_ENDPOINT,
647
648 .bEndpointAddress = USB_DIR_IN,
649 .bmAttributes = USB_ENDPOINT_XFER_BULK,
650};
651
652static struct usb_endpoint_descriptor
653fs_sink_desc = {
654 .bLength = USB_DT_ENDPOINT_SIZE,
655 .bDescriptorType = USB_DT_ENDPOINT,
656
657 .bEndpointAddress = USB_DIR_OUT,
658 .bmAttributes = USB_ENDPOINT_XFER_BULK,
659};
660
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400661static const struct usb_descriptor_header *fs_eth_function[11] = {
Remy Bohmerdf063442009-07-29 18:18:43 +0200662 (struct usb_descriptor_header *) &otg_descriptor,
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200663#ifdef CONFIG_USB_ETH_CDC
Remy Bohmerdf063442009-07-29 18:18:43 +0200664 /* "cdc" mode descriptors */
665 (struct usb_descriptor_header *) &control_intf,
666 (struct usb_descriptor_header *) &header_desc,
667 (struct usb_descriptor_header *) &union_desc,
668 (struct usb_descriptor_header *) &ether_desc,
669 /* NOTE: status endpoint may need to be removed */
670 (struct usb_descriptor_header *) &fs_status_desc,
671 /* data interface, with altsetting */
672 (struct usb_descriptor_header *) &data_nop_intf,
673 (struct usb_descriptor_header *) &data_intf,
674 (struct usb_descriptor_header *) &fs_source_desc,
675 (struct usb_descriptor_header *) &fs_sink_desc,
676 NULL,
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200677#endif /* CONFIG_USB_ETH_CDC */
Remy Bohmerdf063442009-07-29 18:18:43 +0200678};
679
680static inline void fs_subset_descriptors(void)
681{
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200682#ifdef CONFIG_USB_ETH_SUBSET
Remy Bohmerdf063442009-07-29 18:18:43 +0200683 /* behavior is "CDC Subset"; extra descriptors say "SAFE" */
684 fs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
685 fs_eth_function[2] = (struct usb_descriptor_header *) &header_desc;
686 fs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc;
687 fs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc;
688 fs_eth_function[5] = (struct usb_descriptor_header *) &ether_desc;
689 fs_eth_function[6] = (struct usb_descriptor_header *) &fs_source_desc;
690 fs_eth_function[7] = (struct usb_descriptor_header *) &fs_sink_desc;
691 fs_eth_function[8] = NULL;
692#else
693 fs_eth_function[1] = NULL;
694#endif
695}
696
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300697#ifdef CONFIG_USB_ETH_RNDIS
698static const struct usb_descriptor_header *fs_rndis_function[] = {
699 (struct usb_descriptor_header *) &otg_descriptor,
700 /* control interface matches ACM, not Ethernet */
701 (struct usb_descriptor_header *) &rndis_control_intf,
702 (struct usb_descriptor_header *) &header_desc,
703 (struct usb_descriptor_header *) &call_mgmt_descriptor,
704 (struct usb_descriptor_header *) &acm_descriptor,
705 (struct usb_descriptor_header *) &union_desc,
706 (struct usb_descriptor_header *) &fs_status_desc,
707 /* data interface has no altsetting */
708 (struct usb_descriptor_header *) &rndis_data_intf,
709 (struct usb_descriptor_header *) &fs_source_desc,
710 (struct usb_descriptor_header *) &fs_sink_desc,
711 NULL,
712};
713#endif
714
Remy Bohmerdf063442009-07-29 18:18:43 +0200715/*
716 * usb 2.0 devices need to expose both high speed and full speed
717 * descriptors, unless they only run at full speed.
718 */
719
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200720#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Remy Bohmerdf063442009-07-29 18:18:43 +0200721static struct usb_endpoint_descriptor
722hs_status_desc = {
723 .bLength = USB_DT_ENDPOINT_SIZE,
724 .bDescriptorType = USB_DT_ENDPOINT,
725
726 .bmAttributes = USB_ENDPOINT_XFER_INT,
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400727 .wMaxPacketSize = __constant_cpu_to_le16(STATUS_BYTECOUNT),
Remy Bohmerdf063442009-07-29 18:18:43 +0200728 .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4,
729};
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200730#endif /* CONFIG_USB_ETH_CDC */
Remy Bohmerdf063442009-07-29 18:18:43 +0200731
732static struct usb_endpoint_descriptor
733hs_source_desc = {
734 .bLength = USB_DT_ENDPOINT_SIZE,
735 .bDescriptorType = USB_DT_ENDPOINT,
736
737 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400738 .wMaxPacketSize = __constant_cpu_to_le16(512),
Remy Bohmerdf063442009-07-29 18:18:43 +0200739};
740
741static struct usb_endpoint_descriptor
742hs_sink_desc = {
743 .bLength = USB_DT_ENDPOINT_SIZE,
744 .bDescriptorType = USB_DT_ENDPOINT,
745
746 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400747 .wMaxPacketSize = __constant_cpu_to_le16(512),
Remy Bohmerdf063442009-07-29 18:18:43 +0200748};
749
750static struct usb_qualifier_descriptor
751dev_qualifier = {
752 .bLength = sizeof dev_qualifier,
753 .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
754
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400755 .bcdUSB = __constant_cpu_to_le16(0x0200),
Remy Bohmerdf063442009-07-29 18:18:43 +0200756 .bDeviceClass = USB_CLASS_COMM,
757
758 .bNumConfigurations = 1,
759};
760
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400761static const struct usb_descriptor_header *hs_eth_function[11] = {
Remy Bohmerdf063442009-07-29 18:18:43 +0200762 (struct usb_descriptor_header *) &otg_descriptor,
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200763#ifdef CONFIG_USB_ETH_CDC
Remy Bohmerdf063442009-07-29 18:18:43 +0200764 /* "cdc" mode descriptors */
765 (struct usb_descriptor_header *) &control_intf,
766 (struct usb_descriptor_header *) &header_desc,
767 (struct usb_descriptor_header *) &union_desc,
768 (struct usb_descriptor_header *) &ether_desc,
769 /* NOTE: status endpoint may need to be removed */
770 (struct usb_descriptor_header *) &hs_status_desc,
771 /* data interface, with altsetting */
772 (struct usb_descriptor_header *) &data_nop_intf,
773 (struct usb_descriptor_header *) &data_intf,
774 (struct usb_descriptor_header *) &hs_source_desc,
775 (struct usb_descriptor_header *) &hs_sink_desc,
776 NULL,
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200777#endif /* CONFIG_USB_ETH_CDC */
Remy Bohmerdf063442009-07-29 18:18:43 +0200778};
779
780static inline void hs_subset_descriptors(void)
781{
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200782#ifdef CONFIG_USB_ETH_SUBSET
Remy Bohmerdf063442009-07-29 18:18:43 +0200783 /* behavior is "CDC Subset"; extra descriptors say "SAFE" */
784 hs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
785 hs_eth_function[2] = (struct usb_descriptor_header *) &header_desc;
786 hs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc;
787 hs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc;
788 hs_eth_function[5] = (struct usb_descriptor_header *) &ether_desc;
789 hs_eth_function[6] = (struct usb_descriptor_header *) &hs_source_desc;
790 hs_eth_function[7] = (struct usb_descriptor_header *) &hs_sink_desc;
791 hs_eth_function[8] = NULL;
792#else
793 hs_eth_function[1] = NULL;
794#endif
795}
796
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300797#ifdef CONFIG_USB_ETH_RNDIS
798static const struct usb_descriptor_header *hs_rndis_function[] = {
799 (struct usb_descriptor_header *) &otg_descriptor,
800 /* control interface matches ACM, not Ethernet */
801 (struct usb_descriptor_header *) &rndis_control_intf,
802 (struct usb_descriptor_header *) &header_desc,
803 (struct usb_descriptor_header *) &call_mgmt_descriptor,
804 (struct usb_descriptor_header *) &acm_descriptor,
805 (struct usb_descriptor_header *) &union_desc,
806 (struct usb_descriptor_header *) &hs_status_desc,
807 /* data interface has no altsetting */
808 (struct usb_descriptor_header *) &rndis_data_intf,
809 (struct usb_descriptor_header *) &hs_source_desc,
810 (struct usb_descriptor_header *) &hs_sink_desc,
811 NULL,
812};
813#endif
814
815
Remy Bohmerdf063442009-07-29 18:18:43 +0200816/* maxpacket and other transfer characteristics vary by speed. */
817static inline struct usb_endpoint_descriptor *
818ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *hs,
819 struct usb_endpoint_descriptor *fs)
820{
821 if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
822 return hs;
823 return fs;
824}
825
Remy Bohmerdf063442009-07-29 18:18:43 +0200826/*-------------------------------------------------------------------------*/
827
828/* descriptors that are built on-demand */
829
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400830static char manufacturer[50];
831static char product_desc[40] = DRIVER_DESC;
832static char serial_number[20];
Remy Bohmerdf063442009-07-29 18:18:43 +0200833
834/* address that the host will use ... usually assigned at random */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400835static char ethaddr[2 * ETH_ALEN + 1];
Remy Bohmerdf063442009-07-29 18:18:43 +0200836
837/* static strings, in UTF-8 */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400838static struct usb_string strings[] = {
Remy Bohmerdf063442009-07-29 18:18:43 +0200839 { STRING_MANUFACTURER, manufacturer, },
840 { STRING_PRODUCT, product_desc, },
841 { STRING_SERIALNUMBER, serial_number, },
842 { STRING_DATA, "Ethernet Data", },
843 { STRING_ETHADDR, ethaddr, },
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200844#ifdef CONFIG_USB_ETH_CDC
Remy Bohmerdf063442009-07-29 18:18:43 +0200845 { STRING_CDC, "CDC Ethernet", },
846 { STRING_CONTROL, "CDC Communications Control", },
847#endif
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200848#ifdef CONFIG_USB_ETH_SUBSET
Remy Bohmerdf063442009-07-29 18:18:43 +0200849 { STRING_SUBSET, "CDC Ethernet Subset", },
850#endif
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300851#ifdef CONFIG_USB_ETH_RNDIS
852 { STRING_RNDIS, "RNDIS", },
853 { STRING_RNDIS_CONTROL, "RNDIS Communications Control", },
854#endif
Remy Bohmerdf063442009-07-29 18:18:43 +0200855 { } /* end of list */
856};
857
858static struct usb_gadget_strings stringtab = {
859 .language = 0x0409, /* en-us */
860 .strings = strings,
861};
862
Remy Bohmerdf063442009-07-29 18:18:43 +0200863/*============================================================================*/
864static u8 control_req[USB_BUFSIZ];
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200865#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Stefano Babic76e3d4e2010-08-15 14:18:59 +0200866static u8 status_req[STATUS_BYTECOUNT] __attribute__ ((aligned(4)));
Lukasz Dalek6ca42dd2012-10-02 17:04:29 +0200867#endif
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
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200954#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300955 /* 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
Lukasz Dalek284c4cf2012-10-02 17:04:31 +02001135#ifdef CONFIG_USB_ETH_CDC
Remy Bohmerdf063442009-07-29 18:18:43 +02001136
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);
Lukasz Dalek6ca42dd2012-10-02 17:04:29 +02001355 /*
1356 * PXA25x driver use non-CDC ethernet gadget.
1357 * But only _CDC and _RNDIS code can signalize
1358 * that network is working. So we signalize it
1359 * here.
1360 */
1361 l_ethdev.network_started = 1;
1362 debug("USB network up!\n");
Remy Bohmerdf063442009-07-29 18:18:43 +02001363 goto done_set_intf;
1364 }
1365
Lukasz Dalek284c4cf2012-10-02 17:04:31 +02001366#ifdef CONFIG_USB_ETH_CDC
Remy Bohmerdf063442009-07-29 18:18:43 +02001367 switch (wIndex) {
1368 case 0: /* control/master intf */
1369 if (wValue != 0)
1370 break;
1371 if (dev->status) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001372 usb_ep_disable(dev->status_ep);
1373 usb_ep_enable(dev->status_ep, dev->status);
Remy Bohmerdf063442009-07-29 18:18:43 +02001374 }
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001375
Remy Bohmerdf063442009-07-29 18:18:43 +02001376 value = 0;
1377 break;
1378 case 1: /* data intf */
1379 if (wValue > 1)
1380 break;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001381 usb_ep_disable(dev->in_ep);
1382 usb_ep_disable(dev->out_ep);
Remy Bohmerdf063442009-07-29 18:18:43 +02001383
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001384 /*
1385 * CDC requires the data transfers not be done from
Remy Bohmerdf063442009-07-29 18:18:43 +02001386 * the default interface setting ... also, setting
1387 * the non-default interface resets filters etc.
1388 */
1389 if (wValue == 1) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001390 if (!cdc_active(dev))
Remy Bohmerdf063442009-07-29 18:18:43 +02001391 break;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001392 usb_ep_enable(dev->in_ep, dev->in);
1393 usb_ep_enable(dev->out_ep, dev->out);
Remy Bohmerdf063442009-07-29 18:18:43 +02001394 dev->cdc_filter = DEFAULT_FILTER;
1395 if (dev->status)
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001396 issue_start_status(dev);
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001397 eth_start(dev, GFP_ATOMIC);
Remy Bohmerdf063442009-07-29 18:18:43 +02001398 }
Remy Bohmerdf063442009-07-29 18:18:43 +02001399 value = 0;
1400 break;
1401 }
1402#else
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001403 /*
1404 * FIXME this is wrong, as is the assumption that
Remy Bohmerdf063442009-07-29 18:18:43 +02001405 * all non-PXA hardware talks real CDC ...
1406 */
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001407 debug("set_interface ignored!\n");
Lukasz Dalek284c4cf2012-10-02 17:04:31 +02001408#endif /* CONFIG_USB_ETH_CDC */
Remy Bohmerdf063442009-07-29 18:18:43 +02001409
1410done_set_intf:
1411 break;
1412 case USB_REQ_GET_INTERFACE:
1413 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE)
1414 || !dev->config
1415 || wIndex > 1)
1416 break;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001417 if (!(cdc_active(dev) || rndis_active(dev)) && wIndex != 0)
Remy Bohmerdf063442009-07-29 18:18:43 +02001418 break;
1419
1420 /* for CDC, iff carrier is on, data interface is active. */
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001421 if (rndis_active(dev) || wIndex != 1)
Remy Bohmerdf063442009-07-29 18:18:43 +02001422 *(u8 *)req->buf = 0;
1423 else {
1424 /* *(u8 *)req->buf = netif_carrier_ok (dev->net) ? 1 : 0; */
1425 /* carrier always ok ...*/
1426 *(u8 *)req->buf = 1 ;
1427 }
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001428 value = min(wLength, (u16) 1);
Remy Bohmerdf063442009-07-29 18:18:43 +02001429 break;
1430
Lukasz Dalek284c4cf2012-10-02 17:04:31 +02001431#ifdef CONFIG_USB_ETH_CDC
Remy Bohmerdf063442009-07-29 18:18:43 +02001432 case USB_CDC_SET_ETHERNET_PACKET_FILTER:
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001433 /*
1434 * see 6.2.30: no data, wIndex = interface,
Remy Bohmerdf063442009-07-29 18:18:43 +02001435 * wValue = packet filter bitmap
1436 */
1437 if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1438 || !cdc_active(dev)
1439 || wLength != 0
1440 || wIndex > 1)
1441 break;
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001442 debug("packet filter %02x\n", wValue);
Remy Bohmerdf063442009-07-29 18:18:43 +02001443 dev->cdc_filter = wValue;
1444 value = 0;
1445 break;
1446
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001447 /*
1448 * and potentially:
Remy Bohmerdf063442009-07-29 18:18:43 +02001449 * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS:
1450 * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER:
1451 * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER:
1452 * case USB_CDC_GET_ETHERNET_STATISTIC:
1453 */
1454
Lukasz Dalek284c4cf2012-10-02 17:04:31 +02001455#endif /* CONFIG_USB_ETH_CDC */
Remy Bohmerdf063442009-07-29 18:18:43 +02001456
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001457#ifdef CONFIG_USB_ETH_RNDIS
1458 /*
1459 * RNDIS uses the CDC command encapsulation mechanism to implement
1460 * an RPC scheme, with much getting/setting of attributes by OID.
1461 */
1462 case USB_CDC_SEND_ENCAPSULATED_COMMAND:
1463 if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1464 || !rndis_active(dev)
1465 || wLength > USB_BUFSIZ
1466 || wValue
1467 || rndis_control_intf.bInterfaceNumber
1468 != wIndex)
1469 break;
1470 /* read the request, then process it */
1471 value = wLength;
1472 req->complete = rndis_command_complete;
1473 /* later, rndis_control_ack () sends a notification */
1474 break;
1475
1476 case USB_CDC_GET_ENCAPSULATED_RESPONSE:
1477 if ((USB_DIR_IN|USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1478 == ctrl->bRequestType
1479 && rndis_active(dev)
1480 /* && wLength >= 0x0400 */
1481 && !wValue
1482 && rndis_control_intf.bInterfaceNumber
1483 == wIndex) {
1484 u8 *buf;
1485 u32 n;
1486
1487 /* return the result */
1488 buf = rndis_get_next_response(dev->rndis_config, &n);
1489 if (buf) {
1490 memcpy(req->buf, buf, n);
1491 req->complete = rndis_response_complete;
1492 rndis_free_response(dev->rndis_config, buf);
1493 value = n;
1494 }
1495 /* else stalls ... spec says to avoid that */
1496 }
1497 break;
1498#endif /* RNDIS */
1499
Remy Bohmerdf063442009-07-29 18:18:43 +02001500 default:
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001501 debug("unknown control req%02x.%02x v%04x i%04x l%d\n",
Remy Bohmerdf063442009-07-29 18:18:43 +02001502 ctrl->bRequestType, ctrl->bRequest,
1503 wValue, wIndex, wLength);
1504 }
1505
1506 /* respond with data transfer before status phase? */
1507 if (value >= 0) {
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001508 debug("respond with data transfer before status phase\n");
Remy Bohmerdf063442009-07-29 18:18:43 +02001509 req->length = value;
1510 req->zero = value < wLength
1511 && (value % gadget->ep0->maxpacket) == 0;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001512 value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
Remy Bohmerdf063442009-07-29 18:18:43 +02001513 if (value < 0) {
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001514 debug("ep_queue --> %d\n", value);
Remy Bohmerdf063442009-07-29 18:18:43 +02001515 req->status = 0;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001516 eth_setup_complete(gadget->ep0, req);
Remy Bohmerdf063442009-07-29 18:18:43 +02001517 }
1518 }
1519
1520 /* host either stalls (value < 0) or reports success */
1521 return value;
1522}
1523
Remy Bohmerdf063442009-07-29 18:18:43 +02001524/*-------------------------------------------------------------------------*/
1525
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001526static void rx_complete(struct usb_ep *ep, struct usb_request *req);
Remy Bohmerdf063442009-07-29 18:18:43 +02001527
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001528static int rx_submit(struct eth_dev *dev, struct usb_request *req,
Remy Bohmerdf063442009-07-29 18:18:43 +02001529 gfp_t gfp_flags)
1530{
1531 int retval = -ENOMEM;
1532 size_t size;
1533
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001534 /*
1535 * Padding up to RX_EXTRA handles minor disagreements with host.
Remy Bohmerdf063442009-07-29 18:18:43 +02001536 * Normally we use the USB "terminate on short read" convention;
1537 * so allow up to (N*maxpacket), since that memory is normally
1538 * already allocated. Some hardware doesn't deal well with short
1539 * reads (e.g. DMA must be N*maxpacket), so for now don't trim a
1540 * byte off the end (to force hardware errors on overflow).
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001541 *
1542 * RNDIS uses internal framing, and explicitly allows senders to
1543 * pad to end-of-packet. That's potentially nice for speed,
1544 * but means receivers can't recover synch on their own.
Remy Bohmerdf063442009-07-29 18:18:43 +02001545 */
1546
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001547 debug("%s\n", __func__);
Remy Bohmerdf063442009-07-29 18:18:43 +02001548
1549 size = (ETHER_HDR_SIZE + dev->mtu + RX_EXTRA);
1550 size += dev->out_ep->maxpacket - 1;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001551 if (rndis_active(dev))
1552 size += sizeof(struct rndis_packet_msg_type);
Remy Bohmerdf063442009-07-29 18:18:43 +02001553 size -= size % dev->out_ep->maxpacket;
1554
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001555 /*
1556 * Some platforms perform better when IP packets are aligned,
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001557 * but on at least one, checksumming fails otherwise. Note:
1558 * RNDIS headers involve variable numbers of LE32 values.
Remy Bohmerdf063442009-07-29 18:18:43 +02001559 */
1560
1561 req->buf = (u8 *) NetRxPackets[0];
1562 req->length = size;
1563 req->complete = rx_complete;
1564
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001565 retval = usb_ep_queue(dev->out_ep, req, gfp_flags);
Remy Bohmerdf063442009-07-29 18:18:43 +02001566
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001567 if (retval)
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001568 error("rx submit --> %d", retval);
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001569
Remy Bohmerdf063442009-07-29 18:18:43 +02001570 return retval;
1571}
1572
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001573static void rx_complete(struct usb_ep *ep, struct usb_request *req)
Remy Bohmerdf063442009-07-29 18:18:43 +02001574{
1575 struct eth_dev *dev = ep->driver_data;
1576
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001577 debug("%s: status %d\n", __func__, req->status);
Vitaly Kuzmichev24fcb472011-02-11 18:18:32 +03001578 switch (req->status) {
1579 /* normal completion */
1580 case 0:
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001581 if (rndis_active(dev)) {
1582 /* we know MaxPacketsPerTransfer == 1 here */
1583 int length = rndis_rm_hdr(req->buf, req->actual);
1584 if (length < 0)
1585 goto length_err;
1586 req->length -= length;
1587 req->actual -= length;
1588 }
1589 if (req->actual < ETH_HLEN || ETH_FRAME_LEN < req->actual) {
1590length_err:
1591 dev->stats.rx_errors++;
1592 dev->stats.rx_length_errors++;
1593 debug("rx length %d\n", req->length);
1594 break;
1595 }
1596
Vitaly Kuzmichev24fcb472011-02-11 18:18:32 +03001597 dev->stats.rx_packets++;
1598 dev->stats.rx_bytes += req->length;
1599 break;
1600
1601 /* software-driven interface shutdown */
1602 case -ECONNRESET: /* unlink */
1603 case -ESHUTDOWN: /* disconnect etc */
1604 /* for hardware automagic (such as pxa) */
1605 case -ECONNABORTED: /* endpoint reset */
1606 break;
1607
1608 /* data overrun */
1609 case -EOVERFLOW:
1610 dev->stats.rx_over_errors++;
1611 /* FALLTHROUGH */
1612 default:
1613 dev->stats.rx_errors++;
1614 break;
1615 }
Remy Bohmerdf063442009-07-29 18:18:43 +02001616
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001617 packet_received = 1;
Remy Bohmerdf063442009-07-29 18:18:43 +02001618}
1619
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001620static int alloc_requests(struct eth_dev *dev, unsigned n, gfp_t gfp_flags)
Remy Bohmerdf063442009-07-29 18:18:43 +02001621{
1622
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001623 dev->tx_req = usb_ep_alloc_request(dev->in_ep, 0);
Remy Bohmerdf063442009-07-29 18:18:43 +02001624
1625 if (!dev->tx_req)
Vitaly Kuzmichev2907b2c2010-09-22 13:13:55 +04001626 goto fail1;
Remy Bohmerdf063442009-07-29 18:18:43 +02001627
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001628 dev->rx_req = usb_ep_alloc_request(dev->out_ep, 0);
Remy Bohmerdf063442009-07-29 18:18:43 +02001629
1630 if (!dev->rx_req)
Vitaly Kuzmichev2907b2c2010-09-22 13:13:55 +04001631 goto fail2;
Remy Bohmerdf063442009-07-29 18:18:43 +02001632
1633 return 0;
1634
Vitaly Kuzmichev2907b2c2010-09-22 13:13:55 +04001635fail2:
1636 usb_ep_free_request(dev->in_ep, dev->tx_req);
1637fail1:
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001638 error("can't alloc requests");
Remy Bohmerdf063442009-07-29 18:18:43 +02001639 return -1;
1640}
1641
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001642static void tx_complete(struct usb_ep *ep, struct usb_request *req)
Remy Bohmerdf063442009-07-29 18:18:43 +02001643{
Vitaly Kuzmichev24fcb472011-02-11 18:18:32 +03001644 struct eth_dev *dev = ep->driver_data;
1645
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001646 debug("%s: status %s\n", __func__, (req->status) ? "failed" : "ok");
Vitaly Kuzmichev24fcb472011-02-11 18:18:32 +03001647 switch (req->status) {
1648 default:
1649 dev->stats.tx_errors++;
1650 debug("tx err %d\n", req->status);
1651 /* FALLTHROUGH */
1652 case -ECONNRESET: /* unlink */
1653 case -ESHUTDOWN: /* disconnect etc */
1654 break;
1655 case 0:
1656 dev->stats.tx_bytes += req->length;
1657 }
1658 dev->stats.tx_packets++;
1659
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001660 packet_sent = 1;
Remy Bohmerdf063442009-07-29 18:18:43 +02001661}
1662
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001663static inline int eth_is_promisc(struct eth_dev *dev)
Remy Bohmerdf063442009-07-29 18:18:43 +02001664{
1665 /* no filters for the CDC subset; always promisc */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001666 if (subset_active(dev))
Remy Bohmerdf063442009-07-29 18:18:43 +02001667 return 1;
1668 return dev->cdc_filter & USB_CDC_PACKET_TYPE_PROMISCUOUS;
1669}
1670
1671#if 0
1672static int eth_start_xmit (struct sk_buff *skb, struct net_device *net)
1673{
1674 struct eth_dev *dev = netdev_priv(net);
1675 int length = skb->len;
1676 int retval;
1677 struct usb_request *req = NULL;
1678 unsigned long flags;
1679
1680 /* apply outgoing CDC or RNDIS filters */
1681 if (!eth_is_promisc (dev)) {
1682 u8 *dest = skb->data;
1683
1684 if (is_multicast_ether_addr(dest)) {
1685 u16 type;
1686
1687 /* ignores USB_CDC_PACKET_TYPE_MULTICAST and host
1688 * SET_ETHERNET_MULTICAST_FILTERS requests
1689 */
1690 if (is_broadcast_ether_addr(dest))
1691 type = USB_CDC_PACKET_TYPE_BROADCAST;
1692 else
1693 type = USB_CDC_PACKET_TYPE_ALL_MULTICAST;
1694 if (!(dev->cdc_filter & type)) {
1695 dev_kfree_skb_any (skb);
1696 return 0;
1697 }
1698 }
1699 /* ignores USB_CDC_PACKET_TYPE_DIRECTED */
1700 }
1701
1702 spin_lock_irqsave(&dev->req_lock, flags);
1703 /*
1704 * this freelist can be empty if an interrupt triggered disconnect()
1705 * and reconfigured the gadget (shutting down this queue) after the
1706 * network stack decided to xmit but before we got the spinlock.
1707 */
1708 if (list_empty(&dev->tx_reqs)) {
1709 spin_unlock_irqrestore(&dev->req_lock, flags);
1710 return 1;
1711 }
1712
1713 req = container_of (dev->tx_reqs.next, struct usb_request, list);
1714 list_del (&req->list);
1715
1716 /* temporarily stop TX queue when the freelist empties */
1717 if (list_empty (&dev->tx_reqs))
1718 netif_stop_queue (net);
1719 spin_unlock_irqrestore(&dev->req_lock, flags);
1720
1721 /* no buffer copies needed, unless the network stack did it
1722 * or the hardware can't use skb buffers.
1723 * or there's not enough space for any RNDIS headers we need
1724 */
1725 if (rndis_active(dev)) {
1726 struct sk_buff *skb_rndis;
1727
1728 skb_rndis = skb_realloc_headroom (skb,
1729 sizeof (struct rndis_packet_msg_type));
1730 if (!skb_rndis)
1731 goto drop;
1732
1733 dev_kfree_skb_any (skb);
1734 skb = skb_rndis;
1735 rndis_add_hdr (skb);
1736 length = skb->len;
1737 }
1738 req->buf = skb->data;
1739 req->context = skb;
1740 req->complete = tx_complete;
1741
1742 /* use zlp framing on tx for strict CDC-Ether conformance,
1743 * though any robust network rx path ignores extra padding.
1744 * and some hardware doesn't like to write zlps.
1745 */
1746 req->zero = 1;
1747 if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0)
1748 length++;
1749
1750 req->length = length;
1751
1752 /* throttle highspeed IRQ rate back slightly */
1753 if (gadget_is_dualspeed(dev->gadget))
1754 req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH)
1755 ? ((atomic_read(&dev->tx_qlen) % qmult) != 0)
1756 : 0;
1757
1758 retval = usb_ep_queue (dev->in_ep, req, GFP_ATOMIC);
1759 switch (retval) {
1760 default:
1761 DEBUG (dev, "tx queue err %d\n", retval);
1762 break;
1763 case 0:
1764 net->trans_start = jiffies;
1765 atomic_inc (&dev->tx_qlen);
1766 }
1767
1768 if (retval) {
1769drop:
1770 dev->stats.tx_dropped++;
1771 dev_kfree_skb_any (skb);
1772 spin_lock_irqsave(&dev->req_lock, flags);
1773 if (list_empty (&dev->tx_reqs))
1774 netif_start_queue (net);
1775 list_add (&req->list, &dev->tx_reqs);
1776 spin_unlock_irqrestore(&dev->req_lock, flags);
1777 }
1778 return 0;
1779}
1780
1781/*-------------------------------------------------------------------------*/
1782#endif
1783
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001784static void eth_unbind(struct usb_gadget *gadget)
Remy Bohmerdf063442009-07-29 18:18:43 +02001785{
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001786 struct eth_dev *dev = get_gadget_data(gadget);
Remy Bohmerdf063442009-07-29 18:18:43 +02001787
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001788 debug("%s...\n", __func__);
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001789 rndis_deregister(dev->rndis_config);
1790 rndis_exit();
Remy Bohmerdf063442009-07-29 18:18:43 +02001791
Vitaly Kuzmichev90670702010-08-13 17:00:16 +04001792 /* we've already been disconnected ... no i/o is active */
1793 if (dev->req) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001794 usb_ep_free_request(gadget->ep0, dev->req);
Vitaly Kuzmichev90670702010-08-13 17:00:16 +04001795 dev->req = NULL;
1796 }
Remy Bohmerdf063442009-07-29 18:18:43 +02001797 if (dev->stat_req) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001798 usb_ep_free_request(dev->status_ep, dev->stat_req);
Remy Bohmerdf063442009-07-29 18:18:43 +02001799 dev->stat_req = NULL;
1800 }
1801
1802 if (dev->tx_req) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001803 usb_ep_free_request(dev->in_ep, dev->tx_req);
1804 dev->tx_req = NULL;
Remy Bohmerdf063442009-07-29 18:18:43 +02001805 }
1806
1807 if (dev->rx_req) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001808 usb_ep_free_request(dev->out_ep, dev->rx_req);
1809 dev->rx_req = NULL;
Remy Bohmerdf063442009-07-29 18:18:43 +02001810 }
1811
1812/* unregister_netdev (dev->net);*/
1813/* free_netdev(dev->net);*/
1814
Lei Wen4af32732010-12-01 23:43:43 +08001815 dev->gadget = NULL;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001816 set_gadget_data(gadget, NULL);
Remy Bohmerdf063442009-07-29 18:18:43 +02001817}
1818
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001819static void eth_disconnect(struct usb_gadget *gadget)
Remy Bohmerdf063442009-07-29 18:18:43 +02001820{
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001821 eth_reset_config(get_gadget_data(gadget));
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001822 /* FIXME RNDIS should enter RNDIS_UNINITIALIZED */
Remy Bohmerdf063442009-07-29 18:18:43 +02001823}
1824
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001825static void eth_suspend(struct usb_gadget *gadget)
Remy Bohmerdf063442009-07-29 18:18:43 +02001826{
1827 /* Not used */
1828}
1829
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001830static void eth_resume(struct usb_gadget *gadget)
Remy Bohmerdf063442009-07-29 18:18:43 +02001831{
1832 /* Not used */
1833}
1834
1835/*-------------------------------------------------------------------------*/
1836
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001837#ifdef CONFIG_USB_ETH_RNDIS
1838
1839/*
1840 * The interrupt endpoint is used in RNDIS to notify the host when messages
1841 * other than data packets are available ... notably the REMOTE_NDIS_*_CMPLT
1842 * messages, but also REMOTE_NDIS_INDICATE_STATUS_MSG and potentially even
1843 * REMOTE_NDIS_KEEPALIVE_MSG.
1844 *
1845 * The RNDIS control queue is processed by GET_ENCAPSULATED_RESPONSE, and
1846 * normally just one notification will be queued.
1847 */
1848
1849static void rndis_control_ack_complete(struct usb_ep *ep,
1850 struct usb_request *req)
1851{
1852 struct eth_dev *dev = ep->driver_data;
1853
1854 debug("%s...\n", __func__);
1855 if (req->status || req->actual != req->length)
1856 debug("rndis control ack complete --> %d, %d/%d\n",
1857 req->status, req->actual, req->length);
1858
1859 if (!l_ethdev.network_started) {
1860 if (rndis_get_state(dev->rndis_config)
1861 == RNDIS_DATA_INITIALIZED) {
1862 l_ethdev.network_started = 1;
1863 printf("USB RNDIS network up!\n");
1864 }
1865 }
1866
1867 req->context = NULL;
1868
1869 if (req != dev->stat_req)
1870 usb_ep_free_request(ep, req);
1871}
1872
1873static char rndis_resp_buf[8] __attribute__((aligned(sizeof(__le32))));
1874
1875static int rndis_control_ack(struct eth_device *net)
1876{
1877 struct eth_dev *dev = &l_ethdev;
1878 int length;
1879 struct usb_request *resp = dev->stat_req;
1880
1881 /* in case RNDIS calls this after disconnect */
1882 if (!dev->status) {
1883 debug("status ENODEV\n");
1884 return -ENODEV;
1885 }
1886
1887 /* in case queue length > 1 */
1888 if (resp->context) {
1889 resp = usb_ep_alloc_request(dev->status_ep, GFP_ATOMIC);
1890 if (!resp)
1891 return -ENOMEM;
1892 resp->buf = rndis_resp_buf;
1893 }
1894
1895 /*
1896 * Send RNDIS RESPONSE_AVAILABLE notification;
1897 * USB_CDC_NOTIFY_RESPONSE_AVAILABLE should work too
1898 */
1899 resp->length = 8;
1900 resp->complete = rndis_control_ack_complete;
1901 resp->context = dev;
1902
1903 *((__le32 *) resp->buf) = __constant_cpu_to_le32(1);
1904 *((__le32 *) (resp->buf + 4)) = __constant_cpu_to_le32(0);
1905
1906 length = usb_ep_queue(dev->status_ep, resp, GFP_ATOMIC);
1907 if (length < 0) {
1908 resp->status = 0;
1909 rndis_control_ack_complete(dev->status_ep, resp);
1910 }
1911
1912 return 0;
1913}
1914
1915#else
1916
1917#define rndis_control_ack NULL
1918
1919#endif /* RNDIS */
1920
1921static void eth_start(struct eth_dev *dev, gfp_t gfp_flags)
1922{
1923 if (rndis_active(dev)) {
1924 rndis_set_param_medium(dev->rndis_config,
1925 NDIS_MEDIUM_802_3,
1926 BITRATE(dev->gadget)/100);
1927 rndis_signal_connect(dev->rndis_config);
1928 }
1929}
1930
1931static int eth_stop(struct eth_dev *dev)
1932{
Vitaly Kuzmichev3433c332011-02-11 18:18:35 +03001933#ifdef RNDIS_COMPLETE_SIGNAL_DISCONNECT
1934 unsigned long ts;
1935 unsigned long timeout = CONFIG_SYS_HZ; /* 1 sec to stop RNDIS */
1936#endif
1937
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001938 if (rndis_active(dev)) {
1939 rndis_set_param_medium(dev->rndis_config, NDIS_MEDIUM_802_3, 0);
1940 rndis_signal_disconnect(dev->rndis_config);
1941
Vitaly Kuzmichev3433c332011-02-11 18:18:35 +03001942#ifdef RNDIS_COMPLETE_SIGNAL_DISCONNECT
1943 /* Wait until host receives OID_GEN_MEDIA_CONNECT_STATUS */
1944 ts = get_timer(0);
1945 while (get_timer(ts) < timeout)
1946 usb_gadget_handle_interrupts();
1947#endif
1948
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001949 rndis_uninit(dev->rndis_config);
1950 dev->rndis = 0;
1951 }
1952
1953 return 0;
1954}
1955
1956/*-------------------------------------------------------------------------*/
1957
Remy Bohmerdf063442009-07-29 18:18:43 +02001958static int is_eth_addr_valid(char *str)
1959{
1960 if (strlen(str) == 17) {
1961 int i;
1962 char *p, *q;
1963 uchar ea[6];
1964
1965 /* see if it looks like an ethernet address */
1966
1967 p = str;
1968
1969 for (i = 0; i < 6; i++) {
1970 char term = (i == 5 ? '\0' : ':');
1971
1972 ea[i] = simple_strtol(p, &q, 16);
1973
1974 if ((q - p) != 2 || *q++ != term)
1975 break;
1976
1977 p = q;
1978 }
1979
Tom Riniadd21ca2012-10-31 13:30:41 +00001980 /* Now check the contents. */
1981 return is_valid_ether_addr(ea);
Remy Bohmerdf063442009-07-29 18:18:43 +02001982 }
1983 return 0;
1984}
1985
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001986static u8 nibble(unsigned char c)
Remy Bohmerdf063442009-07-29 18:18:43 +02001987{
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001988 if (likely(isdigit(c)))
Remy Bohmerdf063442009-07-29 18:18:43 +02001989 return c - '0';
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001990 c = toupper(c);
1991 if (likely(isxdigit(c)))
Remy Bohmerdf063442009-07-29 18:18:43 +02001992 return 10 + c - 'A';
1993 return 0;
1994}
1995
1996static int get_ether_addr(const char *str, u8 *dev_addr)
1997{
1998 if (str) {
1999 unsigned i;
2000
2001 for (i = 0; i < 6; i++) {
2002 unsigned char num;
2003
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002004 if ((*str == '.') || (*str == ':'))
Remy Bohmerdf063442009-07-29 18:18:43 +02002005 str++;
2006 num = nibble(*str++) << 4;
2007 num |= (nibble(*str++));
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002008 dev_addr[i] = num;
Remy Bohmerdf063442009-07-29 18:18:43 +02002009 }
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002010 if (is_valid_ether_addr(dev_addr))
Remy Bohmerdf063442009-07-29 18:18:43 +02002011 return 0;
2012 }
2013 return 1;
2014}
2015
2016static int eth_bind(struct usb_gadget *gadget)
2017{
2018 struct eth_dev *dev = &l_ethdev;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002019 u8 cdc = 1, zlp = 1, rndis = 1;
Remy Bohmerdf063442009-07-29 18:18:43 +02002020 struct usb_ep *in_ep, *out_ep, *status_ep = NULL;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002021 int status = -ENOMEM;
Remy Bohmerdf063442009-07-29 18:18:43 +02002022 int gcnum;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002023 u8 tmp[7];
Remy Bohmerdf063442009-07-29 18:18:43 +02002024
2025 /* these flags are only ever cleared; compiler take note */
Lukasz Dalek284c4cf2012-10-02 17:04:31 +02002026#ifndef CONFIG_USB_ETH_CDC
Remy Bohmerdf063442009-07-29 18:18:43 +02002027 cdc = 0;
2028#endif
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002029#ifndef CONFIG_USB_ETH_RNDIS
2030 rndis = 0;
2031#endif
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002032 /*
2033 * Because most host side USB stacks handle CDC Ethernet, that
Remy Bohmerdf063442009-07-29 18:18:43 +02002034 * standard protocol is _strongly_ preferred for interop purposes.
2035 * (By everyone except Microsoft.)
2036 */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002037 if (gadget_is_pxa(gadget)) {
Remy Bohmerdf063442009-07-29 18:18:43 +02002038 /* pxa doesn't support altsettings */
2039 cdc = 0;
2040 } else if (gadget_is_musbhdrc(gadget)) {
2041 /* reduce tx dma overhead by avoiding special cases */
2042 zlp = 0;
2043 } else if (gadget_is_sh(gadget)) {
2044 /* sh doesn't support multiple interfaces or configs */
2045 cdc = 0;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002046 rndis = 0;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002047 } else if (gadget_is_sa1100(gadget)) {
Remy Bohmerdf063442009-07-29 18:18:43 +02002048 /* hardware can't write zlps */
2049 zlp = 0;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002050 /*
2051 * sa1100 CAN do CDC, without status endpoint ... we use
Remy Bohmerdf063442009-07-29 18:18:43 +02002052 * non-CDC to be compatible with ARM Linux-2.4 "usb-eth".
2053 */
2054 cdc = 0;
2055 }
2056
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002057 gcnum = usb_gadget_controller_number(gadget);
Remy Bohmerdf063442009-07-29 18:18:43 +02002058 if (gcnum >= 0)
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002059 device_desc.bcdDevice = cpu_to_le16(0x0300 + gcnum);
Remy Bohmerdf063442009-07-29 18:18:43 +02002060 else {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002061 /*
2062 * can't assume CDC works. don't want to default to
Remy Bohmerdf063442009-07-29 18:18:43 +02002063 * anything less functional on CDC-capable hardware,
2064 * so we fail in this case.
2065 */
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04002066 error("controller '%s' not recognized",
Remy Bohmerdf063442009-07-29 18:18:43 +02002067 gadget->name);
2068 return -ENODEV;
2069 }
2070
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002071 /*
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002072 * If there's an RNDIS configuration, that's what Windows wants to
2073 * be using ... so use these product IDs here and in the "linux.inf"
2074 * needed to install MSFT drivers. Current Linux kernels will use
2075 * the second configuration if it's CDC Ethernet, and need some help
2076 * to choose the right configuration otherwise.
Remy Bohmerdf063442009-07-29 18:18:43 +02002077 */
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002078 if (rndis) {
2079#if defined(CONFIG_USB_RNDIS_VENDOR_ID) && defined(CONFIG_USB_RNDIS_PRODUCT_ID)
Remy Bohmerdf063442009-07-29 18:18:43 +02002080 device_desc.idVendor =
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002081 __constant_cpu_to_le16(CONFIG_USB_RNDIS_VENDOR_ID);
Remy Bohmerdf063442009-07-29 18:18:43 +02002082 device_desc.idProduct =
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002083 __constant_cpu_to_le16(CONFIG_USB_RNDIS_PRODUCT_ID);
2084#else
2085 device_desc.idVendor =
2086 __constant_cpu_to_le16(RNDIS_VENDOR_NUM);
2087 device_desc.idProduct =
2088 __constant_cpu_to_le16(RNDIS_PRODUCT_NUM);
2089#endif
2090 sprintf(product_desc, "RNDIS/%s", driver_desc);
Remy Bohmerdf063442009-07-29 18:18:43 +02002091
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002092 /*
2093 * CDC subset ... recognized by Linux since 2.4.10, but Windows
2094 * drivers aren't widely available. (That may be improved by
2095 * supporting one submode of the "SAFE" variant of MDLM.)
2096 */
2097 } else {
Remy Bohmerdf063442009-07-29 18:18:43 +02002098#if defined(CONFIG_USB_CDC_VENDOR_ID) && defined(CONFIG_USB_CDC_PRODUCT_ID)
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002099 device_desc.idVendor = cpu_to_le16(CONFIG_USB_CDC_VENDOR_ID);
2100 device_desc.idProduct = cpu_to_le16(CONFIG_USB_CDC_PRODUCT_ID);
2101#else
2102 if (!cdc) {
2103 device_desc.idVendor =
2104 __constant_cpu_to_le16(SIMPLE_VENDOR_NUM);
2105 device_desc.idProduct =
2106 __constant_cpu_to_le16(SIMPLE_PRODUCT_NUM);
2107 }
Remy Bohmerdf063442009-07-29 18:18:43 +02002108#endif
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002109 }
2110 /* support optional vendor/distro customization */
Remy Bohmerdf063442009-07-29 18:18:43 +02002111 if (bcdDevice)
2112 device_desc.bcdDevice = cpu_to_le16(bcdDevice);
2113 if (iManufacturer)
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002114 strlcpy(manufacturer, iManufacturer, sizeof manufacturer);
Remy Bohmerdf063442009-07-29 18:18:43 +02002115 if (iProduct)
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002116 strlcpy(product_desc, iProduct, sizeof product_desc);
Remy Bohmerdf063442009-07-29 18:18:43 +02002117 if (iSerialNumber) {
2118 device_desc.iSerialNumber = STRING_SERIALNUMBER,
Vitaly Kuzmichev214d7b02010-08-13 17:00:45 +04002119 strlcpy(serial_number, iSerialNumber, sizeof serial_number);
Remy Bohmerdf063442009-07-29 18:18:43 +02002120 }
2121
2122 /* all we really need is bulk IN/OUT */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002123 usb_ep_autoconfig_reset(gadget);
2124 in_ep = usb_ep_autoconfig(gadget, &fs_source_desc);
Remy Bohmerdf063442009-07-29 18:18:43 +02002125 if (!in_ep) {
2126autoconf_fail:
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04002127 error("can't autoconfigure on %s\n",
Remy Bohmerdf063442009-07-29 18:18:43 +02002128 gadget->name);
2129 return -ENODEV;
2130 }
2131 in_ep->driver_data = in_ep; /* claim */
2132
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002133 out_ep = usb_ep_autoconfig(gadget, &fs_sink_desc);
Remy Bohmerdf063442009-07-29 18:18:43 +02002134 if (!out_ep)
2135 goto autoconf_fail;
2136 out_ep->driver_data = out_ep; /* claim */
2137
Lukasz Dalek284c4cf2012-10-02 17:04:31 +02002138#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002139 /*
2140 * CDC Ethernet control interface doesn't require a status endpoint.
Remy Bohmerdf063442009-07-29 18:18:43 +02002141 * Since some hosts expect one, try to allocate one anyway.
2142 */
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002143 if (cdc || rndis) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002144 status_ep = usb_ep_autoconfig(gadget, &fs_status_desc);
Remy Bohmerdf063442009-07-29 18:18:43 +02002145 if (status_ep) {
2146 status_ep->driver_data = status_ep; /* claim */
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002147 } else if (rndis) {
2148 error("can't run RNDIS on %s", gadget->name);
2149 return -ENODEV;
Lukasz Dalek284c4cf2012-10-02 17:04:31 +02002150#ifdef CONFIG_USB_ETH_CDC
Remy Bohmerdf063442009-07-29 18:18:43 +02002151 } else if (cdc) {
2152 control_intf.bNumEndpoints = 0;
2153 /* FIXME remove endpoint from descriptor list */
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002154#endif
Remy Bohmerdf063442009-07-29 18:18:43 +02002155 }
2156 }
2157#endif
2158
2159 /* one config: cdc, else minimal subset */
2160 if (!cdc) {
2161 eth_config.bNumInterfaces = 1;
2162 eth_config.iConfiguration = STRING_SUBSET;
2163
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002164 /*
2165 * use functions to set these up, in case we're built to work
Remy Bohmerdf063442009-07-29 18:18:43 +02002166 * with multiple controllers and must override CDC Ethernet.
2167 */
2168 fs_subset_descriptors();
2169 hs_subset_descriptors();
2170 }
2171
2172 device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002173 usb_gadget_set_selfpowered(gadget);
Remy Bohmerdf063442009-07-29 18:18:43 +02002174
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002175 /* For now RNDIS is always a second config */
2176 if (rndis)
2177 device_desc.bNumConfigurations = 2;
2178
Remy Bohmerdf063442009-07-29 18:18:43 +02002179 if (gadget_is_dualspeed(gadget)) {
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002180 if (rndis)
2181 dev_qualifier.bNumConfigurations = 2;
2182 else if (!cdc)
Remy Bohmerdf063442009-07-29 18:18:43 +02002183 dev_qualifier.bDeviceClass = USB_CLASS_VENDOR_SPEC;
2184
2185 /* assumes ep0 uses the same value for both speeds ... */
2186 dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0;
2187
2188 /* and that all endpoints are dual-speed */
2189 hs_source_desc.bEndpointAddress =
2190 fs_source_desc.bEndpointAddress;
2191 hs_sink_desc.bEndpointAddress =
2192 fs_sink_desc.bEndpointAddress;
Lukasz Dalek284c4cf2012-10-02 17:04:31 +02002193#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Remy Bohmerdf063442009-07-29 18:18:43 +02002194 if (status_ep)
2195 hs_status_desc.bEndpointAddress =
2196 fs_status_desc.bEndpointAddress;
2197#endif
2198 }
2199
2200 if (gadget_is_otg(gadget)) {
2201 otg_descriptor.bmAttributes |= USB_OTG_HNP,
2202 eth_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
2203 eth_config.bMaxPower = 4;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002204#ifdef CONFIG_USB_ETH_RNDIS
2205 rndis_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
2206 rndis_config.bMaxPower = 4;
2207#endif
Remy Bohmerdf063442009-07-29 18:18:43 +02002208 }
2209
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002210
2211 /* network device setup */
Remy Bohmerdf063442009-07-29 18:18:43 +02002212 dev->net = &l_netdev;
Remy Bohmerdf063442009-07-29 18:18:43 +02002213
2214 dev->cdc = cdc;
2215 dev->zlp = zlp;
2216
2217 dev->in_ep = in_ep;
2218 dev->out_ep = out_ep;
2219 dev->status_ep = status_ep;
2220
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002221 /*
2222 * Module params for these addresses should come from ID proms.
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002223 * The host side address is used with CDC and RNDIS, and commonly
Remy Bohmerdf063442009-07-29 18:18:43 +02002224 * ends up in a persistent config database. It's not clear if
2225 * host side code for the SAFE thing cares -- its original BLAN
2226 * thing didn't, Sharp never assigned those addresses on Zaurii.
2227 */
2228 get_ether_addr(dev_addr, dev->net->enetaddr);
2229
2230 memset(tmp, 0, sizeof(tmp));
2231 memcpy(tmp, dev->net->enetaddr, sizeof(dev->net->enetaddr));
2232
2233 get_ether_addr(host_addr, dev->host_mac);
2234
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002235 sprintf(ethaddr, "%02X%02X%02X%02X%02X%02X",
2236 dev->host_mac[0], dev->host_mac[1],
2237 dev->host_mac[2], dev->host_mac[3],
2238 dev->host_mac[4], dev->host_mac[5]);
Remy Bohmerdf063442009-07-29 18:18:43 +02002239
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002240 if (rndis) {
2241 status = rndis_init();
2242 if (status < 0) {
2243 error("can't init RNDIS, %d", status);
2244 goto fail;
2245 }
Remy Bohmerdf063442009-07-29 18:18:43 +02002246 }
2247
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002248 /*
2249 * use PKTSIZE (or aligned... from u-boot) and set
2250 * wMaxSegmentSize accordingly
2251 */
Remy Bohmerdf063442009-07-29 18:18:43 +02002252 dev->mtu = PKTSIZE_ALIGN; /* RNDIS does not like this, only 1514, TODO*/
2253
2254 /* preallocate control message data and buffer */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002255 dev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
Remy Bohmerdf063442009-07-29 18:18:43 +02002256 if (!dev->req)
2257 goto fail;
2258 dev->req->buf = control_req;
2259 dev->req->complete = eth_setup_complete;
2260
2261 /* ... and maybe likewise for status transfer */
Lukasz Dalek284c4cf2012-10-02 17:04:31 +02002262#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Remy Bohmerdf063442009-07-29 18:18:43 +02002263 if (dev->status_ep) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002264 dev->stat_req = usb_ep_alloc_request(dev->status_ep,
2265 GFP_KERNEL);
Remy Bohmerdf063442009-07-29 18:18:43 +02002266 if (!dev->stat_req) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002267 usb_ep_free_request(dev->status_ep, dev->req);
Remy Bohmerdf063442009-07-29 18:18:43 +02002268
2269 goto fail;
2270 }
Vitaly Kuzmichevef8d8d72010-08-13 17:01:06 +04002271 dev->stat_req->buf = status_req;
Remy Bohmerdf063442009-07-29 18:18:43 +02002272 dev->stat_req->context = NULL;
2273 }
2274#endif
2275
2276 /* finish hookup to lower layer ... */
2277 dev->gadget = gadget;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002278 set_gadget_data(gadget, dev);
Remy Bohmerdf063442009-07-29 18:18:43 +02002279 gadget->ep0->driver_data = dev;
2280
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002281 /*
2282 * two kinds of host-initiated state changes:
Remy Bohmerdf063442009-07-29 18:18:43 +02002283 * - iff DATA transfer is active, carrier is "on"
2284 * - tx queueing enabled if open *and* carrier is "on"
2285 */
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002286
2287 printf("using %s, OUT %s IN %s%s%s\n", gadget->name,
2288 out_ep->name, in_ep->name,
2289 status_ep ? " STATUS " : "",
2290 status_ep ? status_ep->name : ""
2291 );
2292 printf("MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
2293 dev->net->enetaddr[0], dev->net->enetaddr[1],
2294 dev->net->enetaddr[2], dev->net->enetaddr[3],
2295 dev->net->enetaddr[4], dev->net->enetaddr[5]);
2296
2297 if (cdc || rndis)
2298 printf("HOST MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
2299 dev->host_mac[0], dev->host_mac[1],
2300 dev->host_mac[2], dev->host_mac[3],
2301 dev->host_mac[4], dev->host_mac[5]);
2302
2303 if (rndis) {
2304 u32 vendorID = 0;
2305
2306 /* FIXME RNDIS vendor id == "vendor NIC code" == ? */
2307
2308 dev->rndis_config = rndis_register(rndis_control_ack);
2309 if (dev->rndis_config < 0) {
2310fail0:
2311 eth_unbind(gadget);
2312 debug("RNDIS setup failed\n");
2313 status = -ENODEV;
2314 goto fail;
2315 }
2316
2317 /* these set up a lot of the OIDs that RNDIS needs */
2318 rndis_set_host_mac(dev->rndis_config, dev->host_mac);
2319 if (rndis_set_param_dev(dev->rndis_config, dev->net, dev->mtu,
2320 &dev->stats, &dev->cdc_filter))
2321 goto fail0;
2322 if (rndis_set_param_vendor(dev->rndis_config, vendorID,
2323 manufacturer))
2324 goto fail0;
2325 if (rndis_set_param_medium(dev->rndis_config,
2326 NDIS_MEDIUM_802_3, 0))
2327 goto fail0;
2328 printf("RNDIS ready\n");
2329 }
Remy Bohmerdf063442009-07-29 18:18:43 +02002330 return 0;
2331
2332fail:
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002333 error("%s failed, status = %d", __func__, status);
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002334 eth_unbind(gadget);
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002335 return status;
Remy Bohmerdf063442009-07-29 18:18:43 +02002336}
2337
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002338/*-------------------------------------------------------------------------*/
2339
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002340static int usb_eth_init(struct eth_device *netdev, bd_t *bd)
Remy Bohmerdf063442009-07-29 18:18:43 +02002341{
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002342 struct eth_dev *dev = &l_ethdev;
Remy Bohmerdf063442009-07-29 18:18:43 +02002343 struct usb_gadget *gadget;
2344 unsigned long ts;
2345 unsigned long timeout = USB_CONNECT_TIMEOUT;
2346
2347 if (!netdev) {
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04002348 error("received NULL ptr");
Remy Bohmerdf063442009-07-29 18:18:43 +02002349 goto fail;
2350 }
Vitaly Kuzmichevbe0afbf2010-12-28 16:59:32 +03002351
2352 /* Configure default mac-addresses for the USB ethernet device */
2353#ifdef CONFIG_USBNET_DEV_ADDR
2354 strlcpy(dev_addr, CONFIG_USBNET_DEV_ADDR, sizeof(dev_addr));
2355#endif
2356#ifdef CONFIG_USBNET_HOST_ADDR
2357 strlcpy(host_addr, CONFIG_USBNET_HOST_ADDR, sizeof(host_addr));
2358#endif
2359 /* Check if the user overruled the MAC addresses */
2360 if (getenv("usbnet_devaddr"))
2361 strlcpy(dev_addr, getenv("usbnet_devaddr"),
2362 sizeof(dev_addr));
2363
2364 if (getenv("usbnet_hostaddr"))
2365 strlcpy(host_addr, getenv("usbnet_hostaddr"),
2366 sizeof(host_addr));
2367
2368 if (!is_eth_addr_valid(dev_addr)) {
2369 error("Need valid 'usbnet_devaddr' to be set");
2370 goto fail;
2371 }
2372 if (!is_eth_addr_valid(host_addr)) {
2373 error("Need valid 'usbnet_hostaddr' to be set");
2374 goto fail;
2375 }
2376
Lei Wen4af32732010-12-01 23:43:43 +08002377 if (usb_gadget_register_driver(&eth_driver) < 0)
2378 goto fail;
Remy Bohmerdf063442009-07-29 18:18:43 +02002379
2380 dev->network_started = 0;
Remy Bohmerdf063442009-07-29 18:18:43 +02002381
2382 packet_received = 0;
2383 packet_sent = 0;
2384
2385 gadget = dev->gadget;
2386 usb_gadget_connect(gadget);
2387
2388 if (getenv("cdc_connect_timeout"))
2389 timeout = simple_strtoul(getenv("cdc_connect_timeout"),
2390 NULL, 10) * CONFIG_SYS_HZ;
2391 ts = get_timer(0);
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002392 while (!l_ethdev.network_started) {
Remy Bohmerdf063442009-07-29 18:18:43 +02002393 /* Handle control-c and timeouts */
2394 if (ctrlc() || (get_timer(ts) > timeout)) {
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04002395 error("The remote end did not respond in time.");
Remy Bohmerdf063442009-07-29 18:18:43 +02002396 goto fail;
2397 }
2398 usb_gadget_handle_interrupts();
2399 }
2400
Vitaly Kuzmicheve4bd3862010-09-22 13:13:56 +04002401 packet_received = 0;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002402 rx_submit(dev, dev->rx_req, 0);
Remy Bohmerdf063442009-07-29 18:18:43 +02002403 return 0;
2404fail:
2405 return -1;
2406}
2407
Joe Hershbergere4e04882012-05-22 18:36:19 +00002408static int usb_eth_send(struct eth_device *netdev, void *packet, int length)
Remy Bohmerdf063442009-07-29 18:18:43 +02002409{
2410 int retval;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002411 void *rndis_pkt = NULL;
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04002412 struct eth_dev *dev = &l_ethdev;
Vitaly Kuzmichev2907b2c2010-09-22 13:13:55 +04002413 struct usb_request *req = dev->tx_req;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002414 unsigned long ts;
2415 unsigned long timeout = USB_CONNECT_TIMEOUT;
Remy Bohmerdf063442009-07-29 18:18:43 +02002416
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04002417 debug("%s:...\n", __func__);
Remy Bohmerdf063442009-07-29 18:18:43 +02002418
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002419 /* new buffer is needed to include RNDIS header */
2420 if (rndis_active(dev)) {
2421 rndis_pkt = malloc(length +
2422 sizeof(struct rndis_packet_msg_type));
2423 if (!rndis_pkt) {
2424 error("No memory to alloc RNDIS packet");
2425 goto drop;
2426 }
2427 rndis_add_hdr(rndis_pkt, length);
2428 memcpy(rndis_pkt + sizeof(struct rndis_packet_msg_type),
Joe Hershbergere4e04882012-05-22 18:36:19 +00002429 packet, length);
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002430 packet = rndis_pkt;
2431 length += sizeof(struct rndis_packet_msg_type);
2432 }
Joe Hershbergere4e04882012-05-22 18:36:19 +00002433 req->buf = packet;
Remy Bohmerdf063442009-07-29 18:18:43 +02002434 req->context = NULL;
2435 req->complete = tx_complete;
2436
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002437 /*
2438 * use zlp framing on tx for strict CDC-Ether conformance,
Remy Bohmerdf063442009-07-29 18:18:43 +02002439 * though any robust network rx path ignores extra padding.
2440 * and some hardware doesn't like to write zlps.
2441 */
2442 req->zero = 1;
2443 if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0)
2444 length++;
2445
2446 req->length = length;
2447#if 0
2448 /* throttle highspeed IRQ rate back slightly */
2449 if (gadget_is_dualspeed(dev->gadget))
2450 req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH)
2451 ? ((dev->tx_qlen % qmult) != 0) : 0;
2452#endif
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002453 dev->tx_qlen = 1;
2454 ts = get_timer(0);
2455 packet_sent = 0;
Remy Bohmerdf063442009-07-29 18:18:43 +02002456
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002457 retval = usb_ep_queue(dev->in_ep, req, GFP_ATOMIC);
Remy Bohmerdf063442009-07-29 18:18:43 +02002458
2459 if (!retval)
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04002460 debug("%s: packet queued\n", __func__);
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002461 while (!packet_sent) {
2462 if (get_timer(ts) > timeout) {
2463 printf("timeout sending packets to usb ethernet\n");
2464 return -1;
2465 }
2466 usb_gadget_handle_interrupts();
Remy Bohmerdf063442009-07-29 18:18:43 +02002467 }
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002468 if (rndis_pkt)
2469 free(rndis_pkt);
Remy Bohmerdf063442009-07-29 18:18:43 +02002470
2471 return 0;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002472drop:
2473 dev->stats.tx_dropped++;
2474 return -ENOMEM;
Remy Bohmerdf063442009-07-29 18:18:43 +02002475}
2476
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002477static int usb_eth_recv(struct eth_device *netdev)
Remy Bohmerdf063442009-07-29 18:18:43 +02002478{
2479 struct eth_dev *dev = &l_ethdev;
2480
2481 usb_gadget_handle_interrupts();
2482
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002483 if (packet_received) {
2484 debug("%s: packet received\n", __func__);
2485 if (dev->rx_req) {
2486 NetReceive(NetRxPackets[0], dev->rx_req->length);
2487 packet_received = 0;
Remy Bohmerdf063442009-07-29 18:18:43 +02002488
Vitaly Kuzmichev2907b2c2010-09-22 13:13:55 +04002489 rx_submit(dev, dev->rx_req, 0);
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002490 } else
2491 error("dev->rx_req invalid");
Remy Bohmerdf063442009-07-29 18:18:43 +02002492 }
2493 return 0;
2494}
2495
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002496void usb_eth_halt(struct eth_device *netdev)
Remy Bohmerdf063442009-07-29 18:18:43 +02002497{
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002498 struct eth_dev *dev = &l_ethdev;
Remy Bohmerdf063442009-07-29 18:18:43 +02002499
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002500 if (!netdev) {
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04002501 error("received NULL ptr");
Remy Bohmerdf063442009-07-29 18:18:43 +02002502 return;
2503 }
2504
Lei Wen4af32732010-12-01 23:43:43 +08002505 /* If the gadget not registered, simple return */
2506 if (!dev->gadget)
2507 return;
2508
Vitaly Kuzmichev3433c332011-02-11 18:18:35 +03002509 /*
2510 * Some USB controllers may need additional deinitialization here
2511 * before dropping pull-up (also due to hardware issues).
2512 * For example: unhandled interrupt with status stage started may
2513 * bring the controller to fully broken state (until board reset).
2514 * There are some variants to debug and fix such cases:
2515 * 1) In the case of RNDIS connection eth_stop can perform additional
2516 * interrupt handling. See RNDIS_COMPLETE_SIGNAL_DISCONNECT definition.
2517 * 2) 'pullup' callback in your UDC driver can be improved to perform
2518 * this deinitialization.
2519 */
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002520 eth_stop(dev);
2521
Remy Bohmerdf063442009-07-29 18:18:43 +02002522 usb_gadget_disconnect(dev->gadget);
Vitaly Kuzmichev08d4dd72011-02-11 18:18:31 +03002523
2524 /* Clear pending interrupt */
2525 if (dev->network_started) {
2526 usb_gadget_handle_interrupts();
2527 dev->network_started = 0;
2528 }
2529
Lei Wen4af32732010-12-01 23:43:43 +08002530 usb_gadget_unregister_driver(&eth_driver);
Remy Bohmerdf063442009-07-29 18:18:43 +02002531}
2532
2533static struct usb_gadget_driver eth_driver = {
2534 .speed = DEVSPEED,
2535
2536 .bind = eth_bind,
2537 .unbind = eth_unbind,
2538
2539 .setup = eth_setup,
2540 .disconnect = eth_disconnect,
2541
2542 .suspend = eth_suspend,
2543 .resume = eth_resume,
2544};
2545
2546int usb_eth_initialize(bd_t *bi)
2547{
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002548 struct eth_device *netdev = &l_netdev;
Remy Bohmerdf063442009-07-29 18:18:43 +02002549
Vitaly Kuzmicheva36cff12010-12-28 16:59:31 +03002550 strlcpy(netdev->name, USB_NET_NAME, sizeof(netdev->name));
Remy Bohmerdf063442009-07-29 18:18:43 +02002551
2552 netdev->init = usb_eth_init;
2553 netdev->send = usb_eth_send;
2554 netdev->recv = usb_eth_recv;
2555 netdev->halt = usb_eth_halt;
2556
2557#ifdef CONFIG_MCAST_TFTP
2558 #error not supported
2559#endif
Remy Bohmerdf063442009-07-29 18:18:43 +02002560 eth_register(netdev);
2561 return 0;
Remy Bohmerdf063442009-07-29 18:18:43 +02002562}