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