blob: 51f50060a0ea6ffd8a9619b5c7526f7c6497fbb5 [file] [log] [blame]
Remy Bohmer23cd1382009-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>
25#include <linux/usb/ch9.h>
26#include <linux/usb/cdc.h>
27#include <linux/usb/gadget.h>
28#include <net.h>
29#include <linux/ctype.h>
30
31#include "gadget_chips.h"
32
33#define USB_NET_NAME "usb0"
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +040034
Remy Bohmer23cd1382009-07-29 18:18:43 +020035#define atomic_read
36extern struct platform_data brd;
37#define spin_lock(x)
38#define spin_unlock(x)
39
40
41unsigned packet_received, packet_sent;
42
43#define DEV_CONFIG_CDC 1
44#define GFP_ATOMIC ((gfp_t) 0)
45#define GFP_KERNEL ((gfp_t) 0)
46
47/*
48 * Ethernet gadget driver -- with CDC and non-CDC options
49 * Builds on hardware support for a full duplex link.
50 *
51 * CDC Ethernet is the standard USB solution for sending Ethernet frames
52 * using USB. Real hardware tends to use the same framing protocol but look
53 * different for control features. This driver strongly prefers to use
54 * this USB-IF standard as its open-systems interoperability solution;
55 * most host side USB stacks (except from Microsoft) support it.
56 *
57 * This is sometimes called "CDC ECM" (Ethernet Control Model) to support
58 * TLA-soup. "CDC ACM" (Abstract Control Model) is for modems, and a new
59 * "CDC EEM" (Ethernet Emulation Model) is starting to spread.
60 *
61 * There's some hardware that can't talk CDC ECM. We make that hardware
62 * implement a "minimalist" vendor-agnostic CDC core: same framing, but
63 * link-level setup only requires activating the configuration. Only the
64 * endpoint descriptors, and product/vendor IDs, are relevant; no control
65 * operations are available. Linux supports it, but other host operating
66 * systems may not. (This is a subset of CDC Ethernet.)
67 *
68 * It turns out that if you add a few descriptors to that "CDC Subset",
69 * (Windows) host side drivers from MCCI can treat it as one submode of
70 * a proprietary scheme called "SAFE" ... without needing to know about
71 * specific product/vendor IDs. So we do that, making it easier to use
72 * those MS-Windows drivers. Those added descriptors make it resemble a
73 * CDC MDLM device, but they don't change device behavior at all. (See
74 * MCCI Engineering report 950198 "SAFE Networking Functions".)
75 *
76 * A third option is also in use. Rather than CDC Ethernet, or something
77 * simpler, Microsoft pushes their own approach: RNDIS. The published
78 * RNDIS specs are ambiguous and appear to be incomplete, and are also
79 * needlessly complex. They borrow more from CDC ACM than CDC ECM.
80 */
81#define ETH_ALEN 6 /* Octets in one ethernet addr */
82#define ETH_HLEN 14 /* Total octets in header. */
83#define ETH_ZLEN 60 /* Min. octets in frame sans FCS */
84#define ETH_DATA_LEN 1500 /* Max. octets in payload */
85#define ETH_FRAME_LEN PKTSIZE_ALIGN /* Max. octets in frame sans FCS */
86#define ETH_FCS_LEN 4 /* Octets in the FCS */
87
88#define DRIVER_DESC "Ethernet Gadget"
89/* Based on linux 2.6.27 version */
90#define DRIVER_VERSION "May Day 2005"
91
92static const char shortname [] = "ether";
93static const char driver_desc [] = DRIVER_DESC;
94
95#define RX_EXTRA 20 /* guard against rx overflows */
96
97/* CDC support the same host-chosen outgoing packet filters. */
98#define DEFAULT_FILTER (USB_CDC_PACKET_TYPE_BROADCAST \
99 |USB_CDC_PACKET_TYPE_ALL_MULTICAST \
100 |USB_CDC_PACKET_TYPE_PROMISCUOUS \
101 |USB_CDC_PACKET_TYPE_DIRECTED)
102
103#define USB_CONNECT_TIMEOUT (3 * CONFIG_SYS_HZ)
104
105/*-------------------------------------------------------------------------*/
106static struct eth_dev l_ethdev;
107static struct eth_device l_netdev;
108static struct usb_gadget_driver eth_driver;
109
110/*-------------------------------------------------------------------------*/
111
112/* "main" config is either CDC, or its simple subset */
113static inline int is_cdc(struct eth_dev *dev)
114{
115#if !defined(DEV_CONFIG_SUBSET)
116 return 1; /* only cdc possible */
117#elif !defined (DEV_CONFIG_CDC)
118 return 0; /* only subset possible */
119#else
120 return dev->cdc; /* depends on what hardware we found */
121#endif
122}
123
124#define subset_active(dev) (!is_cdc(dev))
125#define cdc_active(dev) ( is_cdc(dev))
126
127#define DEFAULT_QLEN 2 /* double buffering by default */
128
129/* peak bulk transfer bits-per-second */
130#define HS_BPS (13 * 512 * 8 * 1000 * 8)
131#define FS_BPS (19 * 64 * 1 * 1000 * 8)
132
133#ifdef CONFIG_USB_GADGET_DUALSPEED
134#define DEVSPEED USB_SPEED_HIGH
135
Vitaly Kuzmichev2721dbf2010-08-12 16:44:40 +0400136#ifdef CONFIG_USB_ETH_QMULT
137#define qmult CONFIG_USB_ETH_QMULT
138#else
139#define qmult 5
140#endif
141
Remy Bohmer23cd1382009-07-29 18:18:43 +0200142/* for dual-speed hardware, use deeper queues at highspeed */
143#define qlen(gadget) \
144 (DEFAULT_QLEN*((gadget->speed == USB_SPEED_HIGH) ? qmult : 1))
145
146static inline int BITRATE(struct usb_gadget *g)
147{
148 return (g->speed == USB_SPEED_HIGH) ? HS_BPS : FS_BPS;
149}
150
151#else /* full speed (low speed doesn't do bulk) */
152
153#define qmult 1
154
155#define DEVSPEED USB_SPEED_FULL
156
157#define qlen(gadget) DEFAULT_QLEN
158
159static inline int BITRATE(struct usb_gadget *g)
160{
161 return FS_BPS;
162}
163#endif
164
165struct eth_dev {
166 struct usb_gadget *gadget;
167 struct usb_request *req; /* for control responses */
168 struct usb_request *stat_req; /* for cdc status */
169
170 u8 config;
171 struct usb_ep *in_ep, *out_ep, *status_ep;
172 const struct usb_endpoint_descriptor
173 *in, *out, *status;
174
175 struct usb_request *tx_req, *rx_req;
176
177 struct eth_device *net;
178 unsigned int tx_qlen;
179
180 unsigned zlp:1;
181 unsigned cdc:1;
182 unsigned suspended:1;
183 unsigned network_started:1;
184 u16 cdc_filter;
185 unsigned long todo;
186 int mtu;
187#define WORK_RX_MEMORY 0
188 u8 host_mac [ETH_ALEN];
189};
190
191/* This version autoconfigures as much as possible at run-time.
192 *
193 * It also ASSUMES a self-powered device, without remote wakeup,
194 * although remote wakeup support would make sense.
195 */
196
197/*-------------------------------------------------------------------------*/
198
199/* DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
200 * Instead: allocate your own, using normal USB-IF procedures.
201 */
202
203/* Thanks to NetChip Technologies for donating this product ID.
204 * It's for devices with only CDC Ethernet configurations.
205 */
206#define CDC_VENDOR_NUM 0x0525 /* NetChip */
207#define CDC_PRODUCT_NUM 0xa4a1 /* Linux-USB Ethernet Gadget */
208
209/* For hardware that can't talk CDC, we use the same vendor ID that
210 * ARM Linux has used for ethernet-over-usb, both with sa1100 and
211 * with pxa250. We're protocol-compatible, if the host-side drivers
212 * use the endpoint descriptors. bcdDevice (version) is nonzero, so
213 * drivers that need to hard-wire endpoint numbers have a hook.
214 *
215 * The protocol is a minimal subset of CDC Ether, which works on any bulk
216 * hardware that's not deeply broken ... even on hardware that can't talk
217 * RNDIS (like SA-1100, with no interrupt endpoint, or anything that
218 * doesn't handle control-OUT).
219 */
220#define SIMPLE_VENDOR_NUM 0x049f
221#define SIMPLE_PRODUCT_NUM 0x505a
222
223/* Some systems will want different product identifers published in the
224 * device descriptor, either numbers or strings or both. These string
225 * parameters are in UTF-8 (superset of ASCII's 7 bit characters).
226 */
227
228static ushort bcdDevice;
229#if defined(CONFIG_USBNET_MANUFACTURER)
230static char *iManufacturer = CONFIG_USBNET_MANUFACTURER;
231#else
232static char *iManufacturer = "U-boot";
233#endif
234static char *iProduct;
235static char *iSerialNumber;
236static char dev_addr[18];
237static char host_addr[18];
238
239/*-------------------------------------------------------------------------*/
240
241/* USB DRIVER HOOKUP (to the hardware driver, below us), mostly
242 * ep0 implementation: descriptors, config management, setup().
243 * also optional class-specific notification interrupt transfer.
244 */
245
246/*
247 * DESCRIPTORS ... most are static, but strings and (full) configuration
248 * descriptors are built on demand. For now we do either full CDC, or
249 * our simple subset.
250 */
251
252#define STRING_MANUFACTURER 1
253#define STRING_PRODUCT 2
254#define STRING_ETHADDR 3
255#define STRING_DATA 4
256#define STRING_CONTROL 5
257#define STRING_CDC 7
258#define STRING_SUBSET 8
259#define STRING_SERIALNUMBER 10
260
261/* holds our biggest descriptor */
262#define USB_BUFSIZ 256
263
264/*
265 * This device advertises one configuration, eth_config,
266 * on hardware supporting at least two configs.
267 *
268 * FIXME define some higher-powered configurations to make it easier
269 * to recharge batteries ...
270 */
271
272#define DEV_CONFIG_VALUE 1 /* cdc or subset */
273
274static struct usb_device_descriptor
275device_desc = {
276 .bLength = sizeof device_desc,
277 .bDescriptorType = USB_DT_DEVICE,
278
279 .bcdUSB = __constant_cpu_to_le16 (0x0200),
280
281 .bDeviceClass = USB_CLASS_COMM,
282 .bDeviceSubClass = 0,
283 .bDeviceProtocol = 0,
284
285 .idVendor = __constant_cpu_to_le16 (CDC_VENDOR_NUM),
286 .idProduct = __constant_cpu_to_le16 (CDC_PRODUCT_NUM),
287 .iManufacturer = STRING_MANUFACTURER,
288 .iProduct = STRING_PRODUCT,
289 .bNumConfigurations = 1,
290};
291
292static struct usb_otg_descriptor
293otg_descriptor = {
294 .bLength = sizeof otg_descriptor,
295 .bDescriptorType = USB_DT_OTG,
296
297 .bmAttributes = USB_OTG_SRP,
298};
299
300static struct usb_config_descriptor
301eth_config = {
302 .bLength = sizeof eth_config,
303 .bDescriptorType = USB_DT_CONFIG,
304
305 /* compute wTotalLength on the fly */
306 .bNumInterfaces = 2,
307 .bConfigurationValue = DEV_CONFIG_VALUE,
308 .iConfiguration = STRING_CDC,
309 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
310 .bMaxPower = 1,
311};
312
313/*
314 * Compared to the simple CDC subset, the full CDC Ethernet model adds
315 * three class descriptors, two interface descriptors, optional status
316 * endpoint. Both have a "data" interface and two bulk endpoints.
317 * There are also differences in how control requests are handled.
318 */
319
320#ifdef DEV_CONFIG_CDC
321static struct usb_interface_descriptor
322control_intf = {
323 .bLength = sizeof control_intf,
324 .bDescriptorType = USB_DT_INTERFACE,
325
326 .bInterfaceNumber = 0,
327 /* status endpoint is optional; this may be patched later */
328 .bNumEndpoints = 1,
329 .bInterfaceClass = USB_CLASS_COMM,
330 .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET,
331 .bInterfaceProtocol = USB_CDC_PROTO_NONE,
332 .iInterface = STRING_CONTROL,
333};
334#endif
335
336static const struct usb_cdc_header_desc header_desc = {
337 .bLength = sizeof header_desc,
338 .bDescriptorType = USB_DT_CS_INTERFACE,
339 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
340
341 .bcdCDC = __constant_cpu_to_le16 (0x0110),
342};
343
344#if defined(DEV_CONFIG_CDC)
345
346static const struct usb_cdc_union_desc union_desc = {
347 .bLength = sizeof union_desc,
348 .bDescriptorType = USB_DT_CS_INTERFACE,
349 .bDescriptorSubType = USB_CDC_UNION_TYPE,
350
351 .bMasterInterface0 = 0, /* index of control interface */
352 .bSlaveInterface0 = 1, /* index of DATA interface */
353};
354
355#endif /* CDC */
356
357#ifndef DEV_CONFIG_CDC
358
359/* "SAFE" loosely follows CDC WMC MDLM, violating the spec in various
360 * ways: data endpoints live in the control interface, there's no data
361 * interface, and it's not used to talk to a cell phone radio.
362 */
363
364static const struct usb_cdc_mdlm_desc mdlm_desc = {
365 .bLength = sizeof mdlm_desc,
366 .bDescriptorType = USB_DT_CS_INTERFACE,
367 .bDescriptorSubType = USB_CDC_MDLM_TYPE,
368
369 .bcdVersion = __constant_cpu_to_le16(0x0100),
370 .bGUID = {
371 0x5d, 0x34, 0xcf, 0x66, 0x11, 0x18, 0x11, 0xd6,
372 0xa2, 0x1a, 0x00, 0x01, 0x02, 0xca, 0x9a, 0x7f,
373 },
374};
375
376/* since "usb_cdc_mdlm_detail_desc" is a variable length structure, we
377 * can't really use its struct. All we do here is say that we're using
378 * the submode of "SAFE" which directly matches the CDC Subset.
379 */
380static const u8 mdlm_detail_desc[] = {
381 6,
382 USB_DT_CS_INTERFACE,
383 USB_CDC_MDLM_DETAIL_TYPE,
384
385 0, /* "SAFE" */
386 0, /* network control capabilities (none) */
387 0, /* network data capabilities ("raw" encapsulation) */
388};
389
390#endif
391
392
393static const struct usb_cdc_ether_desc ether_desc = {
394 .bLength = sizeof (ether_desc),
395 .bDescriptorType = USB_DT_CS_INTERFACE,
396 .bDescriptorSubType = USB_CDC_ETHERNET_TYPE,
397
398 /* this descriptor actually adds value, surprise! */
399 .iMACAddress = STRING_ETHADDR,
400 .bmEthernetStatistics = __constant_cpu_to_le32 (0), /* no statistics */
401 .wMaxSegmentSize = __constant_cpu_to_le16 (ETH_FRAME_LEN),
402 .wNumberMCFilters = __constant_cpu_to_le16 (0),
403 .bNumberPowerFilters = 0,
404};
405
406
407#if defined(DEV_CONFIG_CDC)
408
409/* include the status endpoint if we can, even where it's optional.
410 * use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one
411 * packet, to simplify cancellation; and a big transfer interval, to
412 * waste less bandwidth.
413 *
414 * some drivers (like Linux 2.4 cdc-ether!) "need" it to exist even
415 * if they ignore the connect/disconnect notifications that real aether
416 * can provide. more advanced cdc configurations might want to support
417 * encapsulated commands (vendor-specific, using control-OUT).
418 */
419
420#define LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */
421#define STATUS_BYTECOUNT 16 /* 8 byte header + data */
422
423static struct usb_endpoint_descriptor
424fs_status_desc = {
425 .bLength = USB_DT_ENDPOINT_SIZE,
426 .bDescriptorType = USB_DT_ENDPOINT,
427
428 .bEndpointAddress = USB_DIR_IN,
429 .bmAttributes = USB_ENDPOINT_XFER_INT,
430 .wMaxPacketSize = __constant_cpu_to_le16 (STATUS_BYTECOUNT),
431 .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC,
432};
433#endif
434
435#ifdef DEV_CONFIG_CDC
436
437/* the default data interface has no endpoints ... */
438
439static const struct usb_interface_descriptor
440data_nop_intf = {
441 .bLength = sizeof data_nop_intf,
442 .bDescriptorType = USB_DT_INTERFACE,
443
444 .bInterfaceNumber = 1,
445 .bAlternateSetting = 0,
446 .bNumEndpoints = 0,
447 .bInterfaceClass = USB_CLASS_CDC_DATA,
448 .bInterfaceSubClass = 0,
449 .bInterfaceProtocol = 0,
450};
451
452/* ... but the "real" data interface has two bulk endpoints */
453
454static const struct usb_interface_descriptor
455data_intf = {
456 .bLength = sizeof data_intf,
457 .bDescriptorType = USB_DT_INTERFACE,
458
459 .bInterfaceNumber = 1,
460 .bAlternateSetting = 1,
461 .bNumEndpoints = 2,
462 .bInterfaceClass = USB_CLASS_CDC_DATA,
463 .bInterfaceSubClass = 0,
464 .bInterfaceProtocol = 0,
465 .iInterface = STRING_DATA,
466};
467
468#endif
469
470#ifdef DEV_CONFIG_SUBSET
471
472/*
473 * "Simple" CDC-subset option is a simple vendor-neutral model that most
474 * full speed controllers can handle: one interface, two bulk endpoints.
475 *
476 * To assist host side drivers, we fancy it up a bit, and add descriptors
477 * so some host side drivers will understand it as a "SAFE" variant.
478 */
479
480static const struct usb_interface_descriptor
481subset_data_intf = {
482 .bLength = sizeof subset_data_intf,
483 .bDescriptorType = USB_DT_INTERFACE,
484
485 .bInterfaceNumber = 0,
486 .bAlternateSetting = 0,
487 .bNumEndpoints = 2,
488 .bInterfaceClass = USB_CLASS_COMM,
489 .bInterfaceSubClass = USB_CDC_SUBCLASS_MDLM,
490 .bInterfaceProtocol = 0,
491 .iInterface = STRING_DATA,
492};
493
494#endif /* SUBSET */
495
496
497static struct usb_endpoint_descriptor
498fs_source_desc = {
499 .bLength = USB_DT_ENDPOINT_SIZE,
500 .bDescriptorType = USB_DT_ENDPOINT,
501
502 .bEndpointAddress = USB_DIR_IN,
503 .bmAttributes = USB_ENDPOINT_XFER_BULK,
504};
505
506static struct usb_endpoint_descriptor
507fs_sink_desc = {
508 .bLength = USB_DT_ENDPOINT_SIZE,
509 .bDescriptorType = USB_DT_ENDPOINT,
510
511 .bEndpointAddress = USB_DIR_OUT,
512 .bmAttributes = USB_ENDPOINT_XFER_BULK,
513};
514
515static const struct usb_descriptor_header *fs_eth_function [11] = {
516 (struct usb_descriptor_header *) &otg_descriptor,
517#ifdef DEV_CONFIG_CDC
518 /* "cdc" mode descriptors */
519 (struct usb_descriptor_header *) &control_intf,
520 (struct usb_descriptor_header *) &header_desc,
521 (struct usb_descriptor_header *) &union_desc,
522 (struct usb_descriptor_header *) &ether_desc,
523 /* NOTE: status endpoint may need to be removed */
524 (struct usb_descriptor_header *) &fs_status_desc,
525 /* data interface, with altsetting */
526 (struct usb_descriptor_header *) &data_nop_intf,
527 (struct usb_descriptor_header *) &data_intf,
528 (struct usb_descriptor_header *) &fs_source_desc,
529 (struct usb_descriptor_header *) &fs_sink_desc,
530 NULL,
531#endif /* DEV_CONFIG_CDC */
532};
533
534static inline void fs_subset_descriptors(void)
535{
536#ifdef DEV_CONFIG_SUBSET
537 /* behavior is "CDC Subset"; extra descriptors say "SAFE" */
538 fs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
539 fs_eth_function[2] = (struct usb_descriptor_header *) &header_desc;
540 fs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc;
541 fs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc;
542 fs_eth_function[5] = (struct usb_descriptor_header *) &ether_desc;
543 fs_eth_function[6] = (struct usb_descriptor_header *) &fs_source_desc;
544 fs_eth_function[7] = (struct usb_descriptor_header *) &fs_sink_desc;
545 fs_eth_function[8] = NULL;
546#else
547 fs_eth_function[1] = NULL;
548#endif
549}
550
551/*
552 * usb 2.0 devices need to expose both high speed and full speed
553 * descriptors, unless they only run at full speed.
554 */
555
556#if defined(DEV_CONFIG_CDC)
557static struct usb_endpoint_descriptor
558hs_status_desc = {
559 .bLength = USB_DT_ENDPOINT_SIZE,
560 .bDescriptorType = USB_DT_ENDPOINT,
561
562 .bmAttributes = USB_ENDPOINT_XFER_INT,
563 .wMaxPacketSize = __constant_cpu_to_le16 (STATUS_BYTECOUNT),
564 .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4,
565};
566#endif /* DEV_CONFIG_CDC */
567
568static struct usb_endpoint_descriptor
569hs_source_desc = {
570 .bLength = USB_DT_ENDPOINT_SIZE,
571 .bDescriptorType = USB_DT_ENDPOINT,
572
573 .bmAttributes = USB_ENDPOINT_XFER_BULK,
574 .wMaxPacketSize = __constant_cpu_to_le16 (512),
575};
576
577static struct usb_endpoint_descriptor
578hs_sink_desc = {
579 .bLength = USB_DT_ENDPOINT_SIZE,
580 .bDescriptorType = USB_DT_ENDPOINT,
581
582 .bmAttributes = USB_ENDPOINT_XFER_BULK,
583 .wMaxPacketSize = __constant_cpu_to_le16 (512),
584};
585
586static struct usb_qualifier_descriptor
587dev_qualifier = {
588 .bLength = sizeof dev_qualifier,
589 .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
590
591 .bcdUSB = __constant_cpu_to_le16 (0x0200),
592 .bDeviceClass = USB_CLASS_COMM,
593
594 .bNumConfigurations = 1,
595};
596
597static const struct usb_descriptor_header *hs_eth_function [11] = {
598 (struct usb_descriptor_header *) &otg_descriptor,
599#ifdef DEV_CONFIG_CDC
600 /* "cdc" mode descriptors */
601 (struct usb_descriptor_header *) &control_intf,
602 (struct usb_descriptor_header *) &header_desc,
603 (struct usb_descriptor_header *) &union_desc,
604 (struct usb_descriptor_header *) &ether_desc,
605 /* NOTE: status endpoint may need to be removed */
606 (struct usb_descriptor_header *) &hs_status_desc,
607 /* data interface, with altsetting */
608 (struct usb_descriptor_header *) &data_nop_intf,
609 (struct usb_descriptor_header *) &data_intf,
610 (struct usb_descriptor_header *) &hs_source_desc,
611 (struct usb_descriptor_header *) &hs_sink_desc,
612 NULL,
613#endif /* DEV_CONFIG_CDC */
614};
615
616static inline void hs_subset_descriptors(void)
617{
618#ifdef DEV_CONFIG_SUBSET
619 /* behavior is "CDC Subset"; extra descriptors say "SAFE" */
620 hs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
621 hs_eth_function[2] = (struct usb_descriptor_header *) &header_desc;
622 hs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc;
623 hs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc;
624 hs_eth_function[5] = (struct usb_descriptor_header *) &ether_desc;
625 hs_eth_function[6] = (struct usb_descriptor_header *) &hs_source_desc;
626 hs_eth_function[7] = (struct usb_descriptor_header *) &hs_sink_desc;
627 hs_eth_function[8] = NULL;
628#else
629 hs_eth_function[1] = NULL;
630#endif
631}
632
633/* maxpacket and other transfer characteristics vary by speed. */
634static inline struct usb_endpoint_descriptor *
635ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *hs,
636 struct usb_endpoint_descriptor *fs)
637{
638 if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
639 return hs;
640 return fs;
641}
642
643
644/*-------------------------------------------------------------------------*/
645
646/* descriptors that are built on-demand */
647
648static char manufacturer [50];
649static char product_desc [40] = DRIVER_DESC;
650static char serial_number [20];
651
652/* address that the host will use ... usually assigned at random */
653static char ethaddr [2 * ETH_ALEN + 1];
654
655/* static strings, in UTF-8 */
656static struct usb_string strings [] = {
657 { STRING_MANUFACTURER, manufacturer, },
658 { STRING_PRODUCT, product_desc, },
659 { STRING_SERIALNUMBER, serial_number, },
660 { STRING_DATA, "Ethernet Data", },
661 { STRING_ETHADDR, ethaddr, },
662#ifdef DEV_CONFIG_CDC
663 { STRING_CDC, "CDC Ethernet", },
664 { STRING_CONTROL, "CDC Communications Control", },
665#endif
666#ifdef DEV_CONFIG_SUBSET
667 { STRING_SUBSET, "CDC Ethernet Subset", },
668#endif
669 { } /* end of list */
670};
671
672static struct usb_gadget_strings stringtab = {
673 .language = 0x0409, /* en-us */
674 .strings = strings,
675};
676
677
678/*============================================================================*/
679static u8 control_req[USB_BUFSIZ];
680static u8 status_req[STATUS_BYTECOUNT];
681
682
683
684/**
685 * strlcpy - Copy a %NUL terminated string into a sized buffer
686 * @dest: Where to copy the string to
687 * @src: Where to copy the string from
688 * @size: size of destination buffer
689 *
690 * Compatible with *BSD: the result is always a valid
691 * NUL-terminated string that fits in the buffer (unless,
692 * of course, the buffer size is zero). It does not pad
693 * out the result like strncpy() does.
694 */
695size_t strlcpy(char *dest, const char *src, size_t size)
696{
697 size_t ret = strlen(src);
698
699 if (size) {
700 size_t len = (ret >= size) ? size - 1 : ret;
701 memcpy(dest, src, len);
702 dest[len] = '\0';
703 }
704 return ret;
705}
706
707
708/*============================================================================*/
709
710/*
711 * one config, two interfaces: control, data.
712 * complications: class descriptors, and an altsetting.
713 */
714static int
715config_buf(struct usb_gadget *g, u8 *buf, u8 type, unsigned index, int is_otg)
716{
717 int len;
718 const struct usb_config_descriptor *config;
719 const struct usb_descriptor_header **function;
720 int hs = 0;
721
722 if (gadget_is_dualspeed(g)) {
723 hs = (g->speed == USB_SPEED_HIGH);
724 if (type == USB_DT_OTHER_SPEED_CONFIG)
725 hs = !hs;
726 }
727#define which_fn(t) (hs ? hs_ ## t ## _function : fs_ ## t ## _function)
728
729 if (index >= device_desc.bNumConfigurations)
730 return -EINVAL;
731
732 config = &eth_config;
733 function = which_fn (eth);
734
735 /* for now, don't advertise srp-only devices */
736 if (!is_otg)
737 function++;
738
739 len = usb_gadget_config_buf (config, buf, USB_BUFSIZ, function);
740 if (len < 0)
741 return len;
742 ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
743 return len;
744}
745
746/*-------------------------------------------------------------------------*/
747
748static int alloc_requests (struct eth_dev *dev, unsigned n, gfp_t gfp_flags);
749
750static int
751set_ether_config (struct eth_dev *dev, gfp_t gfp_flags)
752{
753 int result = 0;
754 struct usb_gadget *gadget = dev->gadget;
755
756#if defined(DEV_CONFIG_CDC)
757 /* status endpoint used for (optionally) CDC */
758 if (!subset_active(dev) && dev->status_ep) {
759 dev->status = ep_desc (gadget, &hs_status_desc,
760 &fs_status_desc);
761 dev->status_ep->driver_data = dev;
762
763 result = usb_ep_enable (dev->status_ep, dev->status);
764 if (result != 0) {
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +0400765 debug("enable %s --> %d\n",
Remy Bohmer23cd1382009-07-29 18:18:43 +0200766 dev->status_ep->name, result);
767 goto done;
768 }
769 }
770#endif
771
772 dev->in = ep_desc(gadget, &hs_source_desc, &fs_source_desc);
773 dev->in_ep->driver_data = dev;
774
775 dev->out = ep_desc(gadget, &hs_sink_desc, &fs_sink_desc);
776 dev->out_ep->driver_data = dev;
777
778 /* With CDC, the host isn't allowed to use these two data
779 * endpoints in the default altsetting for the interface.
780 * so we don't activate them yet. Reset from SET_INTERFACE.
781 */
782 if (!cdc_active(dev)) {
783 result = usb_ep_enable (dev->in_ep, dev->in);
784 if (result != 0) {
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +0400785 debug("enable %s --> %d\n",
Remy Bohmer23cd1382009-07-29 18:18:43 +0200786 dev->in_ep->name, result);
787 goto done;
788 }
789
790 result = usb_ep_enable (dev->out_ep, dev->out);
791 if (result != 0) {
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +0400792 debug("enable %s --> %d\n",
Remy Bohmer23cd1382009-07-29 18:18:43 +0200793 dev->out_ep->name, result);
794 goto done;
795 }
796 }
797
798done:
799 if (result == 0)
800 result = alloc_requests (dev, qlen (gadget), gfp_flags);
801
802 /* on error, disable any endpoints */
803 if (result < 0) {
804 if (!subset_active(dev))
805 (void) usb_ep_disable (dev->status_ep);
806 dev->status = NULL;
807 (void) usb_ep_disable (dev->in_ep);
808 (void) usb_ep_disable (dev->out_ep);
809 dev->in = NULL;
810 dev->out = NULL;
811 }
812
813 /* caller is responsible for cleanup on error */
814 return result;
815}
816
817
818static void eth_reset_config (struct eth_dev *dev)
819{
820 if (dev->config == 0)
821 return;
822
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +0400823 debug("%s\n", __func__);
824
Remy Bohmer23cd1382009-07-29 18:18:43 +0200825 /* disable endpoints, forcing (synchronous) completion of
826 * pending i/o. then free the requests.
827 */
828
829 if (dev->in) {
830 usb_ep_disable (dev->in_ep);
831 if (dev->tx_req) {
832 usb_ep_free_request (dev->in_ep, dev->tx_req);
833 dev->tx_req=NULL;
834 }
835 }
836 if (dev->out) {
837 usb_ep_disable (dev->out_ep);
838 if (dev->rx_req) {
Vitaly Kuzmichev0129e322010-08-13 17:00:16 +0400839 usb_ep_free_request (dev->out_ep, dev->rx_req);
Remy Bohmer23cd1382009-07-29 18:18:43 +0200840 dev->rx_req=NULL;
841 }
842 }
843 if (dev->status) {
844 usb_ep_disable (dev->status_ep);
845 }
846 dev->cdc_filter = 0;
847 dev->config = 0;
848}
849
850/* change our operational config. must agree with the code
851 * that returns config descriptors, and altsetting code.
852 */
853static int eth_set_config (struct eth_dev *dev, unsigned number, gfp_t gfp_flags)
854{
855 int result = 0;
856 struct usb_gadget *gadget = dev->gadget;
857
858 if (gadget_is_sa1100 (gadget)
859 && dev->config
860 && dev->tx_qlen != 0) {
861 /* tx fifo is full, but we can't clear it...*/
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +0400862 error("can't change configurations");
Remy Bohmer23cd1382009-07-29 18:18:43 +0200863 return -ESPIPE;
864 }
865 eth_reset_config (dev);
866
867 switch (number) {
868 case DEV_CONFIG_VALUE:
869 result = set_ether_config (dev, gfp_flags);
870 break;
871 default:
872 result = -EINVAL;
873 /* FALL THROUGH */
874 case 0:
875 break;
876 }
877
878 if (result) {
879 if (number)
880 eth_reset_config (dev);
881 usb_gadget_vbus_draw(dev->gadget,
882 gadget_is_otg(dev->gadget) ? 8 : 100);
883 } else {
884 char *speed;
885 unsigned power;
886
887 power = 2 * eth_config.bMaxPower;
888 usb_gadget_vbus_draw(dev->gadget, power);
889
890 switch (gadget->speed) {
891 case USB_SPEED_FULL: speed = "full"; break;
892#ifdef CONFIG_USB_GADGET_DUALSPEED
893 case USB_SPEED_HIGH: speed = "high"; break;
894#endif
895 default: speed = "?"; break;
896 }
897
898 dev->config = number;
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +0400899 printf("%s speed config #%d: %d mA, %s, using %s\n",
Remy Bohmer23cd1382009-07-29 18:18:43 +0200900 speed, number, power, driver_desc,
901 (cdc_active(dev)? "CDC Ethernet"
902 : "CDC Ethernet Subset"));
903 }
904 return result;
905}
906
907/*-------------------------------------------------------------------------*/
908
909#ifdef DEV_CONFIG_CDC
910
911/* The interrupt endpoint is used in CDC networking models (Ethernet, ATM)
912 * only to notify the host about link status changes (which we support) or
913 * report completion of some encapsulated command. Since
914 * we want this CDC Ethernet code to be vendor-neutral, we don't use that
915 * command mechanism; and only one status request is ever queued.
916 */
917static void eth_status_complete (struct usb_ep *ep, struct usb_request *req)
918{
919 struct usb_cdc_notification *event = req->buf;
920 int value = req->status;
921 struct eth_dev *dev = ep->driver_data;
922
923 /* issue the second notification if host reads the first */
924 if (event->bNotificationType == USB_CDC_NOTIFY_NETWORK_CONNECTION
925 && value == 0) {
926 __le32 *data = req->buf + sizeof *event;
927
928 event->bmRequestType = 0xA1;
929 event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE;
930 event->wValue = __constant_cpu_to_le16 (0);
931 event->wIndex = __constant_cpu_to_le16 (1);
932 event->wLength = __constant_cpu_to_le16 (8);
933
934 /* SPEED_CHANGE data is up/down speeds in bits/sec */
935 data [0] = data [1] = cpu_to_le32 (BITRATE (dev->gadget));
936
937 req->length = STATUS_BYTECOUNT;
938 value = usb_ep_queue (ep, req, GFP_ATOMIC);
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +0400939 debug("send SPEED_CHANGE --> %d\n", value);
Remy Bohmer23cd1382009-07-29 18:18:43 +0200940 if (value == 0)
941 return;
942 } else if (value != -ECONNRESET) {
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +0400943 debug("event %02x --> %d\n",
Remy Bohmer23cd1382009-07-29 18:18:43 +0200944 event->bNotificationType, value);
945 if (event->bNotificationType==
946 USB_CDC_NOTIFY_SPEED_CHANGE)
947 {
948 l_ethdev.network_started=1;
949 printf("USB network up!\n");
950 }
951 }
952 req->context = NULL;
953}
954
955static void issue_start_status (struct eth_dev *dev)
956{
957 struct usb_request *req = dev->stat_req;
958 struct usb_cdc_notification *event;
959 int value;
960
961 /* flush old status
962 *
963 * FIXME ugly idiom, maybe we'd be better with just
964 * a "cancel the whole queue" primitive since any
965 * unlink-one primitive has way too many error modes.
966 * here, we "know" toggle is already clear...
967 *
968 * FIXME iff req->context != null just dequeue it
969 */
970 usb_ep_disable (dev->status_ep);
971 usb_ep_enable (dev->status_ep, dev->status);
972
973 /* 3.8.1 says to issue first NETWORK_CONNECTION, then
974 * a SPEED_CHANGE. could be useful in some configs.
975 */
976 event = req->buf;
977 event->bmRequestType = 0xA1;
978 event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION;
979 event->wValue = __constant_cpu_to_le16 (1); /* connected */
980 event->wIndex = __constant_cpu_to_le16 (1);
981 event->wLength = 0;
982
983 req->length = sizeof *event;
984 req->complete = eth_status_complete;
985 req->context = dev;
986
987 value = usb_ep_queue (dev->status_ep, req, GFP_ATOMIC);
988 if (value < 0)
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +0400989 debug("status buf queue --> %d\n", value);
Remy Bohmer23cd1382009-07-29 18:18:43 +0200990}
991
992#endif
993
994/*-------------------------------------------------------------------------*/
995
996static void eth_setup_complete (struct usb_ep *ep, struct usb_request *req)
997{
998 if (req->status || req->actual != req->length)
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +0400999 debug("setup complete --> %d, %d/%d\n",
Remy Bohmer23cd1382009-07-29 18:18:43 +02001000 req->status, req->actual, req->length);
1001}
1002
1003/*
1004 * The setup() callback implements all the ep0 functionality that's not
1005 * handled lower down. CDC has a number of less-common features:
1006 *
1007 * - two interfaces: control, and ethernet data
1008 * - Ethernet data interface has two altsettings: default, and active
1009 * - class-specific descriptors for the control interface
1010 * - class-specific control requests
1011 */
1012static int
1013eth_setup (struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
1014{
1015 struct eth_dev *dev = get_gadget_data (gadget);
1016 struct usb_request *req = dev->req;
1017 int value = -EOPNOTSUPP;
1018 u16 wIndex = le16_to_cpu(ctrl->wIndex);
1019 u16 wValue = le16_to_cpu(ctrl->wValue);
1020 u16 wLength = le16_to_cpu(ctrl->wLength);
1021
1022 /* descriptors just go into the pre-allocated ep0 buffer,
1023 * while config change events may enable network traffic.
1024 */
1025
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001026 debug("%s\n", __func__);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001027
1028 req->complete = eth_setup_complete;
1029 switch (ctrl->bRequest) {
1030
1031 case USB_REQ_GET_DESCRIPTOR:
1032 if (ctrl->bRequestType != USB_DIR_IN)
1033 break;
1034 switch (wValue >> 8) {
1035
1036 case USB_DT_DEVICE:
1037 value = min (wLength, (u16) sizeof device_desc);
1038 memcpy (req->buf, &device_desc, value);
1039 break;
1040 case USB_DT_DEVICE_QUALIFIER:
1041 if (!gadget_is_dualspeed(gadget))
1042 break;
1043 value = min (wLength, (u16) sizeof dev_qualifier);
1044 memcpy (req->buf, &dev_qualifier, value);
1045 break;
1046
1047 case USB_DT_OTHER_SPEED_CONFIG:
1048 if (!gadget_is_dualspeed(gadget))
1049 break;
1050 /* FALLTHROUGH */
1051 case USB_DT_CONFIG:
1052 value = config_buf(gadget, req->buf,
1053 wValue >> 8,
1054 wValue & 0xff,
1055 gadget_is_otg(gadget));
1056 if (value >= 0)
1057 value = min (wLength, (u16) value);
1058 break;
1059
1060 case USB_DT_STRING:
1061 value = usb_gadget_get_string (&stringtab,
1062 wValue & 0xff, req->buf);
1063
1064 if (value >= 0)
1065 value = min (wLength, (u16) value);
1066
1067 break;
1068 }
1069 break;
1070
1071 case USB_REQ_SET_CONFIGURATION:
1072 if (ctrl->bRequestType != 0)
1073 break;
1074 if (gadget->a_hnp_support)
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001075 debug("HNP available\n");
Remy Bohmer23cd1382009-07-29 18:18:43 +02001076 else if (gadget->a_alt_hnp_support)
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001077 debug("HNP needs a different root port\n");
Remy Bohmer23cd1382009-07-29 18:18:43 +02001078 value = eth_set_config (dev, wValue, GFP_ATOMIC);
1079 break;
1080 case USB_REQ_GET_CONFIGURATION:
1081 if (ctrl->bRequestType != USB_DIR_IN)
1082 break;
1083 *(u8 *)req->buf = dev->config;
1084 value = min (wLength, (u16) 1);
1085 break;
1086
1087 case USB_REQ_SET_INTERFACE:
1088 if (ctrl->bRequestType != USB_RECIP_INTERFACE
1089 || !dev->config
1090 || wIndex > 1)
1091 break;
1092 if (!cdc_active(dev) && wIndex != 0)
1093 break;
1094
1095 /* PXA hardware partially handles SET_INTERFACE;
1096 * we need to kluge around that interference.
1097 */
1098 if (gadget_is_pxa (gadget)) {
1099 value = eth_set_config (dev, DEV_CONFIG_VALUE,
1100 GFP_ATOMIC);
1101 goto done_set_intf;
1102 }
1103
1104#ifdef DEV_CONFIG_CDC
1105 switch (wIndex) {
1106 case 0: /* control/master intf */
1107 if (wValue != 0)
1108 break;
1109 if (dev->status) {
1110 usb_ep_disable (dev->status_ep);
1111 usb_ep_enable (dev->status_ep, dev->status);
1112 }
1113 value = 0;
1114 break;
1115 case 1: /* data intf */
1116 if (wValue > 1)
1117 break;
1118 usb_ep_disable (dev->in_ep);
1119 usb_ep_disable (dev->out_ep);
1120
1121 /* CDC requires the data transfers not be done from
1122 * the default interface setting ... also, setting
1123 * the non-default interface resets filters etc.
1124 */
1125 if (wValue == 1) {
1126 if (!cdc_active (dev))
1127 break;
1128 usb_ep_enable (dev->in_ep, dev->in);
1129 usb_ep_enable (dev->out_ep, dev->out);
1130 dev->cdc_filter = DEFAULT_FILTER;
1131 if (dev->status)
1132 issue_start_status (dev);
1133 }
1134
1135 value = 0;
1136 break;
1137 }
1138#else
1139 /* FIXME this is wrong, as is the assumption that
1140 * all non-PXA hardware talks real CDC ...
1141 */
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001142 debug("set_interface ignored!\n");
Remy Bohmer23cd1382009-07-29 18:18:43 +02001143#endif /* DEV_CONFIG_CDC */
1144
1145done_set_intf:
1146 break;
1147 case USB_REQ_GET_INTERFACE:
1148 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE)
1149 || !dev->config
1150 || wIndex > 1)
1151 break;
1152 if (!(cdc_active(dev)) && wIndex != 0)
1153 break;
1154
1155 /* for CDC, iff carrier is on, data interface is active. */
1156 if (wIndex != 1)
1157 *(u8 *)req->buf = 0;
1158 else {
1159 /* *(u8 *)req->buf = netif_carrier_ok (dev->net) ? 1 : 0; */
1160 /* carrier always ok ...*/
1161 *(u8 *)req->buf = 1 ;
1162 }
1163 value = min (wLength, (u16) 1);
1164 break;
1165
1166#ifdef DEV_CONFIG_CDC
1167 case USB_CDC_SET_ETHERNET_PACKET_FILTER:
1168 /* see 6.2.30: no data, wIndex = interface,
1169 * wValue = packet filter bitmap
1170 */
1171 if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1172 || !cdc_active(dev)
1173 || wLength != 0
1174 || wIndex > 1)
1175 break;
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001176 debug("packet filter %02x\n", wValue);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001177 dev->cdc_filter = wValue;
1178 value = 0;
1179 break;
1180
1181 /* and potentially:
1182 * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS:
1183 * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER:
1184 * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER:
1185 * case USB_CDC_GET_ETHERNET_STATISTIC:
1186 */
1187
1188#endif /* DEV_CONFIG_CDC */
1189
1190 default:
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001191 debug("unknown control req%02x.%02x v%04x i%04x l%d\n",
Remy Bohmer23cd1382009-07-29 18:18:43 +02001192 ctrl->bRequestType, ctrl->bRequest,
1193 wValue, wIndex, wLength);
1194 }
1195
1196 /* respond with data transfer before status phase? */
1197 if (value >= 0) {
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001198 debug("respond with data transfer before status phase\n");
Remy Bohmer23cd1382009-07-29 18:18:43 +02001199 req->length = value;
1200 req->zero = value < wLength
1201 && (value % gadget->ep0->maxpacket) == 0;
1202 value = usb_ep_queue (gadget->ep0, req, GFP_ATOMIC);
1203 if (value < 0) {
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001204 debug("ep_queue --> %d\n", value);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001205 req->status = 0;
1206 eth_setup_complete (gadget->ep0, req);
1207 }
1208 }
1209
1210 /* host either stalls (value < 0) or reports success */
1211 return value;
1212}
1213
1214
1215/*-------------------------------------------------------------------------*/
1216
1217static void rx_complete (struct usb_ep *ep, struct usb_request *req);
1218
1219static int rx_submit ( struct eth_dev *dev, struct usb_request *req, \
1220 gfp_t gfp_flags)
1221{
1222 int retval = -ENOMEM;
1223 size_t size;
1224
1225 /* Padding up to RX_EXTRA handles minor disagreements with host.
1226 * Normally we use the USB "terminate on short read" convention;
1227 * so allow up to (N*maxpacket), since that memory is normally
1228 * already allocated. Some hardware doesn't deal well with short
1229 * reads (e.g. DMA must be N*maxpacket), so for now don't trim a
1230 * byte off the end (to force hardware errors on overflow).
1231 */
1232
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001233 debug("%s\n", __func__);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001234
1235 size = (ETHER_HDR_SIZE + dev->mtu + RX_EXTRA);
1236 size += dev->out_ep->maxpacket - 1;
1237 size -= size % dev->out_ep->maxpacket;
1238
1239
1240 /* Some platforms perform better when IP packets are aligned,
1241 * but on at least one, checksumming fails otherwise.
1242 */
1243
1244 req->buf = (u8 *) NetRxPackets[0];
1245 req->length = size;
1246 req->complete = rx_complete;
1247
1248 retval = usb_ep_queue (dev->out_ep, req, gfp_flags);
1249
1250 if (retval) {
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001251 error("rx submit --> %d", retval);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001252 }
1253 return retval;
1254}
1255
1256
1257static void rx_complete (struct usb_ep *ep, struct usb_request *req)
1258{
1259 struct eth_dev *dev = ep->driver_data;
1260
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001261 debug("%s: status %d\n", __func__, req->status);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001262
1263 packet_received=1;
1264
1265 if (req)
1266 dev->rx_req=req;
1267}
1268
1269
1270static int alloc_requests (struct eth_dev *dev, unsigned n, gfp_t gfp_flags)
1271{
1272
1273 dev->tx_req = usb_ep_alloc_request (dev->in_ep, 0);
1274
1275 if (!dev->tx_req)
1276 goto fail;
1277
1278 dev->rx_req = usb_ep_alloc_request (dev->out_ep, 0);
1279
1280 if (!dev->rx_req)
1281 goto fail;
1282
1283 return 0;
1284
1285fail:
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001286 error("can't alloc requests");
Remy Bohmer23cd1382009-07-29 18:18:43 +02001287 return -1;
1288}
1289
1290
1291static void tx_complete (struct usb_ep *ep, struct usb_request *req)
1292{
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001293 debug("%s: status %s\n", __func__, (req->status)?"failed":"ok");
Remy Bohmer23cd1382009-07-29 18:18:43 +02001294 packet_sent=1;
1295}
1296
1297static inline int eth_is_promisc (struct eth_dev *dev)
1298{
1299 /* no filters for the CDC subset; always promisc */
1300 if (subset_active (dev))
1301 return 1;
1302 return dev->cdc_filter & USB_CDC_PACKET_TYPE_PROMISCUOUS;
1303}
1304
1305#if 0
1306static int eth_start_xmit (struct sk_buff *skb, struct net_device *net)
1307{
1308 struct eth_dev *dev = netdev_priv(net);
1309 int length = skb->len;
1310 int retval;
1311 struct usb_request *req = NULL;
1312 unsigned long flags;
1313
1314 /* apply outgoing CDC or RNDIS filters */
1315 if (!eth_is_promisc (dev)) {
1316 u8 *dest = skb->data;
1317
1318 if (is_multicast_ether_addr(dest)) {
1319 u16 type;
1320
1321 /* ignores USB_CDC_PACKET_TYPE_MULTICAST and host
1322 * SET_ETHERNET_MULTICAST_FILTERS requests
1323 */
1324 if (is_broadcast_ether_addr(dest))
1325 type = USB_CDC_PACKET_TYPE_BROADCAST;
1326 else
1327 type = USB_CDC_PACKET_TYPE_ALL_MULTICAST;
1328 if (!(dev->cdc_filter & type)) {
1329 dev_kfree_skb_any (skb);
1330 return 0;
1331 }
1332 }
1333 /* ignores USB_CDC_PACKET_TYPE_DIRECTED */
1334 }
1335
1336 spin_lock_irqsave(&dev->req_lock, flags);
1337 /*
1338 * this freelist can be empty if an interrupt triggered disconnect()
1339 * and reconfigured the gadget (shutting down this queue) after the
1340 * network stack decided to xmit but before we got the spinlock.
1341 */
1342 if (list_empty(&dev->tx_reqs)) {
1343 spin_unlock_irqrestore(&dev->req_lock, flags);
1344 return 1;
1345 }
1346
1347 req = container_of (dev->tx_reqs.next, struct usb_request, list);
1348 list_del (&req->list);
1349
1350 /* temporarily stop TX queue when the freelist empties */
1351 if (list_empty (&dev->tx_reqs))
1352 netif_stop_queue (net);
1353 spin_unlock_irqrestore(&dev->req_lock, flags);
1354
1355 /* no buffer copies needed, unless the network stack did it
1356 * or the hardware can't use skb buffers.
1357 * or there's not enough space for any RNDIS headers we need
1358 */
1359 if (rndis_active(dev)) {
1360 struct sk_buff *skb_rndis;
1361
1362 skb_rndis = skb_realloc_headroom (skb,
1363 sizeof (struct rndis_packet_msg_type));
1364 if (!skb_rndis)
1365 goto drop;
1366
1367 dev_kfree_skb_any (skb);
1368 skb = skb_rndis;
1369 rndis_add_hdr (skb);
1370 length = skb->len;
1371 }
1372 req->buf = skb->data;
1373 req->context = skb;
1374 req->complete = tx_complete;
1375
1376 /* use zlp framing on tx for strict CDC-Ether conformance,
1377 * though any robust network rx path ignores extra padding.
1378 * and some hardware doesn't like to write zlps.
1379 */
1380 req->zero = 1;
1381 if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0)
1382 length++;
1383
1384 req->length = length;
1385
1386 /* throttle highspeed IRQ rate back slightly */
1387 if (gadget_is_dualspeed(dev->gadget))
1388 req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH)
1389 ? ((atomic_read(&dev->tx_qlen) % qmult) != 0)
1390 : 0;
1391
1392 retval = usb_ep_queue (dev->in_ep, req, GFP_ATOMIC);
1393 switch (retval) {
1394 default:
1395 DEBUG (dev, "tx queue err %d\n", retval);
1396 break;
1397 case 0:
1398 net->trans_start = jiffies;
1399 atomic_inc (&dev->tx_qlen);
1400 }
1401
1402 if (retval) {
1403drop:
1404 dev->stats.tx_dropped++;
1405 dev_kfree_skb_any (skb);
1406 spin_lock_irqsave(&dev->req_lock, flags);
1407 if (list_empty (&dev->tx_reqs))
1408 netif_start_queue (net);
1409 list_add (&req->list, &dev->tx_reqs);
1410 spin_unlock_irqrestore(&dev->req_lock, flags);
1411 }
1412 return 0;
1413}
1414
1415/*-------------------------------------------------------------------------*/
1416#endif
1417
1418static void eth_unbind (struct usb_gadget *gadget)
1419{
1420 struct eth_dev *dev = get_gadget_data (gadget);
1421
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001422 debug("%s...\n", __func__);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001423
Vitaly Kuzmichev0129e322010-08-13 17:00:16 +04001424 /* we've already been disconnected ... no i/o is active */
1425 if (dev->req) {
1426 usb_ep_free_request (gadget->ep0, dev->req);
1427 dev->req = NULL;
1428 }
Remy Bohmer23cd1382009-07-29 18:18:43 +02001429 if (dev->stat_req) {
1430 usb_ep_free_request (dev->status_ep, dev->stat_req);
1431 dev->stat_req = NULL;
1432 }
1433
1434 if (dev->tx_req) {
1435 usb_ep_free_request (dev->in_ep, dev->tx_req);
1436 dev->tx_req=NULL;
1437 }
1438
1439 if (dev->rx_req) {
Vitaly Kuzmichev0129e322010-08-13 17:00:16 +04001440 usb_ep_free_request (dev->out_ep, dev->rx_req);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001441 dev->rx_req=NULL;
1442 }
1443
1444/* unregister_netdev (dev->net);*/
1445/* free_netdev(dev->net);*/
1446
1447 set_gadget_data (gadget, NULL);
1448}
1449
1450static void eth_disconnect (struct usb_gadget *gadget)
1451{
1452 eth_reset_config (get_gadget_data (gadget));
1453}
1454
1455static void eth_suspend (struct usb_gadget *gadget)
1456{
1457 /* Not used */
1458}
1459
1460static void eth_resume (struct usb_gadget *gadget)
1461{
1462 /* Not used */
1463}
1464
1465/*-------------------------------------------------------------------------*/
1466
1467static int is_eth_addr_valid(char *str)
1468{
1469 if (strlen(str) == 17) {
1470 int i;
1471 char *p, *q;
1472 uchar ea[6];
1473
1474 /* see if it looks like an ethernet address */
1475
1476 p = str;
1477
1478 for (i = 0; i < 6; i++) {
1479 char term = (i == 5 ? '\0' : ':');
1480
1481 ea[i] = simple_strtol(p, &q, 16);
1482
1483 if ((q - p) != 2 || *q++ != term)
1484 break;
1485
1486 p = q;
1487 }
1488
1489 if (i == 6) /* it looks ok */
1490 return 1;
1491 }
1492 return 0;
1493}
1494
1495static u8 nibble (unsigned char c)
1496{
1497 if (likely (isdigit (c)))
1498 return c - '0';
1499 c = toupper (c);
1500 if (likely (isxdigit (c)))
1501 return 10 + c - 'A';
1502 return 0;
1503}
1504
1505static int get_ether_addr(const char *str, u8 *dev_addr)
1506{
1507 if (str) {
1508 unsigned i;
1509
1510 for (i = 0; i < 6; i++) {
1511 unsigned char num;
1512
1513 if((*str == '.') || (*str == ':'))
1514 str++;
1515 num = nibble(*str++) << 4;
1516 num |= (nibble(*str++));
1517 dev_addr [i] = num;
1518 }
1519 if (is_valid_ether_addr (dev_addr))
1520 return 0;
1521 }
1522 return 1;
1523}
1524
1525static int eth_bind(struct usb_gadget *gadget)
1526{
1527 struct eth_dev *dev = &l_ethdev;
1528 u8 cdc = 1, zlp = 1;
1529 struct usb_ep *in_ep, *out_ep, *status_ep = NULL;
1530 int gcnum;
1531 u8 tmp[7];
1532
1533 /* these flags are only ever cleared; compiler take note */
1534#ifndef DEV_CONFIG_CDC
1535 cdc = 0;
1536#endif
1537 /* Because most host side USB stacks handle CDC Ethernet, that
1538 * standard protocol is _strongly_ preferred for interop purposes.
1539 * (By everyone except Microsoft.)
1540 */
1541 if (gadget_is_pxa (gadget)) {
1542 /* pxa doesn't support altsettings */
1543 cdc = 0;
1544 } else if (gadget_is_musbhdrc(gadget)) {
1545 /* reduce tx dma overhead by avoiding special cases */
1546 zlp = 0;
1547 } else if (gadget_is_sh(gadget)) {
1548 /* sh doesn't support multiple interfaces or configs */
1549 cdc = 0;
1550 } else if (gadget_is_sa1100 (gadget)) {
1551 /* hardware can't write zlps */
1552 zlp = 0;
1553 /* sa1100 CAN do CDC, without status endpoint ... we use
1554 * non-CDC to be compatible with ARM Linux-2.4 "usb-eth".
1555 */
1556 cdc = 0;
1557 }
1558
1559 gcnum = usb_gadget_controller_number (gadget);
1560 if (gcnum >= 0)
1561 device_desc.bcdDevice = cpu_to_le16 (0x0300 + gcnum);
1562 else {
1563 /* can't assume CDC works. don't want to default to
1564 * anything less functional on CDC-capable hardware,
1565 * so we fail in this case.
1566 */
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001567 error("controller '%s' not recognized",
Remy Bohmer23cd1382009-07-29 18:18:43 +02001568 gadget->name);
1569 return -ENODEV;
1570 }
1571
1572 /* CDC subset ... recognized by Linux since 2.4.10, but Windows
1573 * drivers aren't widely available. (That may be improved by
1574 * supporting one submode of the "SAFE" variant of MDLM.)
1575 */
1576 if (!cdc) {
1577 device_desc.idVendor =
1578 __constant_cpu_to_le16(SIMPLE_VENDOR_NUM);
1579 device_desc.idProduct =
1580 __constant_cpu_to_le16(SIMPLE_PRODUCT_NUM);
1581 }
1582
1583 /* support optional vendor/distro customization */
1584#if defined(CONFIG_USB_CDC_VENDOR_ID) && defined(CONFIG_USB_CDC_PRODUCT_ID)
1585 device_desc.idVendor = cpu_to_le16(CONFIG_USB_CDC_VENDOR_ID);
1586 device_desc.idProduct = cpu_to_le16(CONFIG_USB_CDC_PRODUCT_ID);
1587#endif
1588 if (bcdDevice)
1589 device_desc.bcdDevice = cpu_to_le16(bcdDevice);
1590 if (iManufacturer)
1591 strcpy (manufacturer, iManufacturer);
1592 if (iProduct)
1593 strcpy (product_desc, iProduct);
1594 if (iSerialNumber) {
1595 device_desc.iSerialNumber = STRING_SERIALNUMBER,
1596 strcpy(serial_number, iSerialNumber);
1597 }
1598
1599 /* all we really need is bulk IN/OUT */
1600 usb_ep_autoconfig_reset (gadget);
1601 in_ep = usb_ep_autoconfig (gadget, &fs_source_desc);
1602 if (!in_ep) {
1603autoconf_fail:
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001604 error("can't autoconfigure on %s\n",
Remy Bohmer23cd1382009-07-29 18:18:43 +02001605 gadget->name);
1606 return -ENODEV;
1607 }
1608 in_ep->driver_data = in_ep; /* claim */
1609
1610 out_ep = usb_ep_autoconfig (gadget, &fs_sink_desc);
1611 if (!out_ep)
1612 goto autoconf_fail;
1613 out_ep->driver_data = out_ep; /* claim */
1614
1615#if defined(DEV_CONFIG_CDC)
1616 /* CDC Ethernet control interface doesn't require a status endpoint.
1617 * Since some hosts expect one, try to allocate one anyway.
1618 */
1619 if (cdc) {
1620 status_ep = usb_ep_autoconfig (gadget, &fs_status_desc);
1621 if (status_ep) {
1622 status_ep->driver_data = status_ep; /* claim */
1623 } else if (cdc) {
1624 control_intf.bNumEndpoints = 0;
1625 /* FIXME remove endpoint from descriptor list */
1626 }
1627 }
1628#endif
1629
1630 /* one config: cdc, else minimal subset */
1631 if (!cdc) {
1632 eth_config.bNumInterfaces = 1;
1633 eth_config.iConfiguration = STRING_SUBSET;
1634
1635 /* use functions to set these up, in case we're built to work
1636 * with multiple controllers and must override CDC Ethernet.
1637 */
1638 fs_subset_descriptors();
1639 hs_subset_descriptors();
1640 }
1641
1642 device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
1643 usb_gadget_set_selfpowered (gadget);
1644
1645 if (gadget_is_dualspeed(gadget)) {
1646 if (!cdc)
1647 dev_qualifier.bDeviceClass = USB_CLASS_VENDOR_SPEC;
1648
1649 /* assumes ep0 uses the same value for both speeds ... */
1650 dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0;
1651
1652 /* and that all endpoints are dual-speed */
1653 hs_source_desc.bEndpointAddress =
1654 fs_source_desc.bEndpointAddress;
1655 hs_sink_desc.bEndpointAddress =
1656 fs_sink_desc.bEndpointAddress;
1657#if defined(DEV_CONFIG_CDC)
1658 if (status_ep)
1659 hs_status_desc.bEndpointAddress =
1660 fs_status_desc.bEndpointAddress;
1661#endif
1662 }
1663
1664 if (gadget_is_otg(gadget)) {
1665 otg_descriptor.bmAttributes |= USB_OTG_HNP,
1666 eth_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
1667 eth_config.bMaxPower = 4;
1668 }
1669
1670 dev->net = &l_netdev;
1671 strcpy (dev->net->name, USB_NET_NAME);
1672
1673 dev->cdc = cdc;
1674 dev->zlp = zlp;
1675
1676 dev->in_ep = in_ep;
1677 dev->out_ep = out_ep;
1678 dev->status_ep = status_ep;
1679
1680 /* Module params for these addresses should come from ID proms.
1681 * The host side address is used with CDC, and commonly
1682 * ends up in a persistent config database. It's not clear if
1683 * host side code for the SAFE thing cares -- its original BLAN
1684 * thing didn't, Sharp never assigned those addresses on Zaurii.
1685 */
1686 get_ether_addr(dev_addr, dev->net->enetaddr);
1687
1688 memset(tmp, 0, sizeof(tmp));
1689 memcpy(tmp, dev->net->enetaddr, sizeof(dev->net->enetaddr));
1690
1691 get_ether_addr(host_addr, dev->host_mac);
1692
1693 sprintf (ethaddr, "%02X%02X%02X%02X%02X%02X",
1694 dev->host_mac [0], dev->host_mac [1],
1695 dev->host_mac [2], dev->host_mac [3],
1696 dev->host_mac [4], dev->host_mac [5]);
1697
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001698 printf("using %s, OUT %s IN %s%s%s\n", gadget->name,
Remy Bohmer23cd1382009-07-29 18:18:43 +02001699 out_ep->name, in_ep->name,
1700 status_ep ? " STATUS " : "",
1701 status_ep ? status_ep->name : ""
1702 );
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001703 printf("MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
Remy Bohmer23cd1382009-07-29 18:18:43 +02001704 dev->net->enetaddr [0], dev->net->enetaddr [1],
1705 dev->net->enetaddr [2], dev->net->enetaddr [3],
1706 dev->net->enetaddr [4], dev->net->enetaddr [5]);
1707
1708 if (cdc) {
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001709 printf("HOST MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
Remy Bohmer23cd1382009-07-29 18:18:43 +02001710 dev->host_mac [0], dev->host_mac [1],
1711 dev->host_mac [2], dev->host_mac [3],
1712 dev->host_mac [4], dev->host_mac [5]);
1713 }
1714
1715 /* use PKTSIZE (or aligned... from u-boot) and set
1716 * wMaxSegmentSize accordingly*/
1717 dev->mtu = PKTSIZE_ALIGN; /* RNDIS does not like this, only 1514, TODO*/
1718
1719 /* preallocate control message data and buffer */
1720 dev->req = usb_ep_alloc_request (gadget->ep0, GFP_KERNEL);
1721 if (!dev->req)
1722 goto fail;
1723 dev->req->buf = control_req;
1724 dev->req->complete = eth_setup_complete;
1725
1726 /* ... and maybe likewise for status transfer */
1727#if defined(DEV_CONFIG_CDC)
1728 if (dev->status_ep) {
1729 dev->stat_req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
1730 dev->stat_req->buf = status_req;
1731 if (!dev->stat_req) {
1732 dev->stat_req->buf=NULL;
1733 usb_ep_free_request (gadget->ep0, dev->req);
1734
1735 goto fail;
1736 }
1737 dev->stat_req->context = NULL;
1738 }
1739#endif
1740
1741 /* finish hookup to lower layer ... */
1742 dev->gadget = gadget;
1743 set_gadget_data (gadget, dev);
1744 gadget->ep0->driver_data = dev;
1745
1746 /* two kinds of host-initiated state changes:
1747 * - iff DATA transfer is active, carrier is "on"
1748 * - tx queueing enabled if open *and* carrier is "on"
1749 */
1750 return 0;
1751
1752fail:
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001753 error("%s failed", __func__);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001754 eth_unbind (gadget);
1755 return -ENOMEM;
1756}
1757
1758static int usb_eth_init(struct eth_device* netdev, bd_t* bd)
1759{
1760 struct eth_dev *dev=&l_ethdev;
1761 struct usb_gadget *gadget;
1762 unsigned long ts;
1763 unsigned long timeout = USB_CONNECT_TIMEOUT;
1764
1765 if (!netdev) {
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001766 error("received NULL ptr");
Remy Bohmer23cd1382009-07-29 18:18:43 +02001767 goto fail;
1768 }
1769
1770 dev->network_started = 0;
1771 dev->tx_req = NULL;
1772 dev->rx_req = NULL;
1773
1774 packet_received = 0;
1775 packet_sent = 0;
1776
1777 gadget = dev->gadget;
1778 usb_gadget_connect(gadget);
1779
1780 if (getenv("cdc_connect_timeout"))
1781 timeout = simple_strtoul(getenv("cdc_connect_timeout"),
1782 NULL, 10) * CONFIG_SYS_HZ;
1783 ts = get_timer(0);
1784 while (!l_ethdev.network_started)
1785 {
1786 /* Handle control-c and timeouts */
1787 if (ctrlc() || (get_timer(ts) > timeout)) {
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001788 error("The remote end did not respond in time.");
Remy Bohmer23cd1382009-07-29 18:18:43 +02001789 goto fail;
1790 }
1791 usb_gadget_handle_interrupts();
1792 }
1793
1794 rx_submit (dev, dev->rx_req, 0);
1795 return 0;
1796fail:
1797 return -1;
1798}
1799
1800static int usb_eth_send(struct eth_device* netdev, volatile void* packet, int length)
1801{
1802 int retval;
1803 struct usb_request *req = NULL;
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001804 struct eth_dev *dev = &l_ethdev;
Remy Bohmer23cd1382009-07-29 18:18:43 +02001805
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001806 debug("%s:...\n", __func__);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001807
1808 req = dev->tx_req;
1809
1810 req->buf = (void *)packet;
1811 req->context = NULL;
1812 req->complete = tx_complete;
1813
1814 /* use zlp framing on tx for strict CDC-Ether conformance,
1815 * though any robust network rx path ignores extra padding.
1816 * and some hardware doesn't like to write zlps.
1817 */
1818 req->zero = 1;
1819 if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0)
1820 length++;
1821
1822 req->length = length;
1823#if 0
1824 /* throttle highspeed IRQ rate back slightly */
1825 if (gadget_is_dualspeed(dev->gadget))
1826 req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH)
1827 ? ((dev->tx_qlen % qmult) != 0) : 0;
1828#endif
1829 dev->tx_qlen=1;
1830
1831 retval = usb_ep_queue (dev->in_ep, req, GFP_ATOMIC);
1832
1833 if (!retval)
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001834 debug("%s: packet queued\n", __func__);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001835 while(!packet_sent)
1836 {
1837 packet_sent=0;
1838 }
1839
1840 return 0;
1841}
1842
1843static int usb_eth_recv(struct eth_device* netdev)
1844{
1845 struct eth_dev *dev = &l_ethdev;
1846
1847 usb_gadget_handle_interrupts();
1848
1849 if (packet_received)
1850 {
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001851 debug("%s: packet received \n", __func__);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001852 if (dev->rx_req)
1853 {
1854 NetReceive(NetRxPackets[0],dev->rx_req->length);
1855 packet_received=0;
1856
1857 if (dev->rx_req)
1858 rx_submit (dev, dev->rx_req, 0);
1859 }
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001860 else error("dev->rx_req invalid");
Remy Bohmer23cd1382009-07-29 18:18:43 +02001861 }
1862 return 0;
1863}
1864
1865void usb_eth_halt(struct eth_device* netdev)
1866{
1867 struct eth_dev *dev =&l_ethdev;
1868
1869 if (!netdev)
1870 {
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001871 error("received NULL ptr");
Remy Bohmer23cd1382009-07-29 18:18:43 +02001872 return;
1873 }
1874
1875 usb_gadget_disconnect(dev->gadget);
1876}
1877
1878static struct usb_gadget_driver eth_driver = {
1879 .speed = DEVSPEED,
1880
1881 .bind = eth_bind,
1882 .unbind = eth_unbind,
1883
1884 .setup = eth_setup,
1885 .disconnect = eth_disconnect,
1886
1887 .suspend = eth_suspend,
1888 .resume = eth_resume,
1889};
1890
1891int usb_eth_initialize(bd_t *bi)
1892{
1893 int status = 0;
1894 struct eth_device *netdev=&l_netdev;
1895
1896 sprintf(netdev->name,"usb_ether");
1897
1898 netdev->init = usb_eth_init;
1899 netdev->send = usb_eth_send;
1900 netdev->recv = usb_eth_recv;
1901 netdev->halt = usb_eth_halt;
1902
1903#ifdef CONFIG_MCAST_TFTP
1904 #error not supported
1905#endif
1906 /* Configure default mac-addresses for the USB ethernet device */
1907#ifdef CONFIG_USBNET_DEV_ADDR
1908 strncpy(dev_addr, CONFIG_USBNET_DEV_ADDR, sizeof(dev_addr));
1909#endif
1910#ifdef CONFIG_USBNET_HOST_ADDR
1911 strncpy(host_addr, CONFIG_USBNET_HOST_ADDR, sizeof(host_addr));
1912#endif
1913 /* Check if the user overruled the MAC addresses */
1914 if (getenv("usbnet_devaddr"))
1915 strncpy(dev_addr, getenv("usbnet_devaddr"),
1916 sizeof(dev_addr));
1917
1918 if (getenv("usbnet_hostaddr"))
1919 strncpy(host_addr, getenv("usbnet_hostaddr"),
1920 sizeof(host_addr));
1921
1922 /* Make sure both strings are terminated */
1923 dev_addr[sizeof(dev_addr)-1] = '\0';
1924 host_addr[sizeof(host_addr)-1] = '\0';
1925
1926 if (!is_eth_addr_valid(dev_addr)) {
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001927 error("Need valid 'usbnet_devaddr' to be set");
Remy Bohmer23cd1382009-07-29 18:18:43 +02001928 status = -1;
1929 }
1930 if (!is_eth_addr_valid(host_addr)) {
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001931 error("Need valid 'usbnet_hostaddr' to be set");
Remy Bohmer23cd1382009-07-29 18:18:43 +02001932 status = -1;
1933 }
1934 if (status)
1935 goto fail;
1936
1937 status = usb_gadget_register_driver(&eth_driver);
1938 if (status < 0)
1939 goto fail;
1940
1941 eth_register(netdev);
1942 return 0;
1943
1944fail:
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001945 error("%s failed. error = %d", __func__, status);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001946 return status;
1947}
1948