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