blob: 96df8413f39178c138053ff65895d55600afbdf7 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/module.h>
27#include <linux/kernel.h>
28#include <linux/delay.h>
29#include <linux/ioport.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/slab.h>
31#include <linux/smp_lock.h>
32#include <linux/errno.h>
33#include <linux/init.h>
34#include <linux/timer.h>
35#include <linux/list.h>
36#include <linux/interrupt.h>
37#include <linux/utsname.h>
38#include <linux/device.h>
39#include <linux/moduleparam.h>
40#include <linux/ctype.h>
41
42#include <asm/byteorder.h>
43#include <asm/io.h>
44#include <asm/irq.h>
45#include <asm/system.h>
46#include <asm/uaccess.h>
47#include <asm/unaligned.h>
48
David Brownell5f848132006-12-16 15:34:53 -080049#include <linux/usb/ch9.h>
David Brownella8c28f22006-06-13 09:57:47 -070050#include <linux/usb/cdc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/usb_gadget.h>
52
53#include <linux/random.h>
54#include <linux/netdevice.h>
55#include <linux/etherdevice.h>
56#include <linux/ethtool.h>
57
58#include "gadget_chips.h"
59
60/*-------------------------------------------------------------------------*/
61
62/*
63 * Ethernet gadget driver -- with CDC and non-CDC options
64 * Builds on hardware support for a full duplex link.
65 *
66 * CDC Ethernet is the standard USB solution for sending Ethernet frames
67 * using USB. Real hardware tends to use the same framing protocol but look
68 * different for control features. This driver strongly prefers to use
69 * this USB-IF standard as its open-systems interoperability solution;
70 * most host side USB stacks (except from Microsoft) support it.
71 *
72 * There's some hardware that can't talk CDC. We make that hardware
73 * implement a "minimalist" vendor-agnostic CDC core: same framing, but
David Brownell11d54892006-12-11 15:59:04 -080074 * link-level setup only requires activating the configuration. Only the
75 * endpoint descriptors, and product/vendor IDs, are relevant; no control
76 * operations are available. Linux supports it, but other host operating
77 * systems may not. (This is a subset of CDC Ethernet.)
78 *
79 * It turns out that if you add a few descriptors to that "CDC Subset",
80 * (Windows) host side drivers from MCCI can treat it as one submode of
81 * a proprietary scheme called "SAFE" ... without needing to know about
82 * specific product/vendor IDs. So we do that, making it easier to use
83 * those MS-Windows drivers. Those added descriptors make it resemble a
84 * CDC MDLM device, but they don't change device behavior at all. (See
85 * MCCI Engineering report 950198 "SAFE Networking Functions".)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 *
87 * A third option is also in use. Rather than CDC Ethernet, or something
88 * simpler, Microsoft pushes their own approach: RNDIS. The published
89 * RNDIS specs are ambiguous and appear to be incomplete, and are also
90 * needlessly complex.
91 */
92
93#define DRIVER_DESC "Ethernet Gadget"
David Brownell907cba32005-04-28 13:48:09 -070094#define DRIVER_VERSION "May Day 2005"
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
96static const char shortname [] = "ether";
97static const char driver_desc [] = DRIVER_DESC;
98
99#define RX_EXTRA 20 /* guard against rx overflows */
100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101#include "rndis.h"
David Brownell45e45ab2005-05-16 08:26:38 -0700102
103#ifndef CONFIG_USB_ETH_RNDIS
104#define rndis_uninit(x) do{}while(0)
105#define rndis_deregister(c) do{}while(0)
106#define rndis_exit() do{}while(0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107#endif
108
109/* CDC and RNDIS support the same host-chosen outgoing packet filters. */
110#define DEFAULT_FILTER (USB_CDC_PACKET_TYPE_BROADCAST \
David Brownell7e27f182006-06-13 09:54:40 -0700111 |USB_CDC_PACKET_TYPE_ALL_MULTICAST \
112 |USB_CDC_PACKET_TYPE_PROMISCUOUS \
113 |USB_CDC_PACKET_TYPE_DIRECTED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
115
116/*-------------------------------------------------------------------------*/
117
118struct eth_dev {
119 spinlock_t lock;
120 struct usb_gadget *gadget;
121 struct usb_request *req; /* for control responses */
122 struct usb_request *stat_req; /* for cdc & rndis status */
123
124 u8 config;
125 struct usb_ep *in_ep, *out_ep, *status_ep;
126 const struct usb_endpoint_descriptor
127 *in, *out, *status;
David Brownell789851c2006-08-21 15:26:38 -0700128
129 spinlock_t req_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 struct list_head tx_reqs, rx_reqs;
131
132 struct net_device *net;
133 struct net_device_stats stats;
134 atomic_t tx_qlen;
135
136 struct work_struct work;
137 unsigned zlp:1;
138 unsigned cdc:1;
139 unsigned rndis:1;
140 unsigned suspended:1;
141 u16 cdc_filter;
142 unsigned long todo;
143#define WORK_RX_MEMORY 0
144 int rndis_config;
145 u8 host_mac [ETH_ALEN];
146};
147
148/* This version autoconfigures as much as possible at run-time.
149 *
150 * It also ASSUMES a self-powered device, without remote wakeup,
151 * although remote wakeup support would make sense.
152 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
154/*-------------------------------------------------------------------------*/
155
156/* DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
157 * Instead: allocate your own, using normal USB-IF procedures.
158 */
159
160/* Thanks to NetChip Technologies for donating this product ID.
161 * It's for devices with only CDC Ethernet configurations.
162 */
163#define CDC_VENDOR_NUM 0x0525 /* NetChip */
164#define CDC_PRODUCT_NUM 0xa4a1 /* Linux-USB Ethernet Gadget */
165
166/* For hardware that can't talk CDC, we use the same vendor ID that
167 * ARM Linux has used for ethernet-over-usb, both with sa1100 and
168 * with pxa250. We're protocol-compatible, if the host-side drivers
169 * use the endpoint descriptors. bcdDevice (version) is nonzero, so
170 * drivers that need to hard-wire endpoint numbers have a hook.
171 *
172 * The protocol is a minimal subset of CDC Ether, which works on any bulk
173 * hardware that's not deeply broken ... even on hardware that can't talk
174 * RNDIS (like SA-1100, with no interrupt endpoint, or anything that
175 * doesn't handle control-OUT).
176 */
177#define SIMPLE_VENDOR_NUM 0x049f
178#define SIMPLE_PRODUCT_NUM 0x505a
179
180/* For hardware that can talk RNDIS and either of the above protocols,
181 * use this ID ... the windows INF files will know it. Unless it's
182 * used with CDC Ethernet, Linux 2.4 hosts will need updates to choose
183 * the non-RNDIS configuration.
184 */
185#define RNDIS_VENDOR_NUM 0x0525 /* NetChip */
186#define RNDIS_PRODUCT_NUM 0xa4a2 /* Ethernet/RNDIS Gadget */
187
188
189/* Some systems will want different product identifers published in the
190 * device descriptor, either numbers or strings or both. These string
191 * parameters are in UTF-8 (superset of ASCII's 7 bit characters).
192 */
193
Aras Vaichas1afc64a2006-02-18 12:31:23 -0800194static ushort idVendor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195module_param(idVendor, ushort, S_IRUGO);
196MODULE_PARM_DESC(idVendor, "USB Vendor ID");
197
Aras Vaichas1afc64a2006-02-18 12:31:23 -0800198static ushort idProduct;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199module_param(idProduct, ushort, S_IRUGO);
200MODULE_PARM_DESC(idProduct, "USB Product ID");
201
Aras Vaichas1afc64a2006-02-18 12:31:23 -0800202static ushort bcdDevice;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203module_param(bcdDevice, ushort, S_IRUGO);
204MODULE_PARM_DESC(bcdDevice, "USB Device version (BCD)");
205
Aras Vaichas1afc64a2006-02-18 12:31:23 -0800206static char *iManufacturer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207module_param(iManufacturer, charp, S_IRUGO);
208MODULE_PARM_DESC(iManufacturer, "USB Manufacturer string");
209
Aras Vaichas1afc64a2006-02-18 12:31:23 -0800210static char *iProduct;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211module_param(iProduct, charp, S_IRUGO);
212MODULE_PARM_DESC(iProduct, "USB Product string");
213
Aras Vaichas1afc64a2006-02-18 12:31:23 -0800214static char *iSerialNumber;
215module_param(iSerialNumber, charp, S_IRUGO);
216MODULE_PARM_DESC(iSerialNumber, "SerialNumber");
217
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218/* initial value, changed by "ifconfig usb0 hw ether xx:xx:xx:xx:xx:xx" */
Aras Vaichas1afc64a2006-02-18 12:31:23 -0800219static char *dev_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220module_param(dev_addr, charp, S_IRUGO);
221MODULE_PARM_DESC(dev_addr, "Device Ethernet Address");
222
223/* this address is invisible to ifconfig */
Aras Vaichas1afc64a2006-02-18 12:31:23 -0800224static char *host_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225module_param(host_addr, charp, S_IRUGO);
226MODULE_PARM_DESC(host_addr, "Host Ethernet Address");
227
228
229/*-------------------------------------------------------------------------*/
230
231/* Include CDC support if we could run on CDC-capable hardware. */
232
233#ifdef CONFIG_USB_GADGET_NET2280
234#define DEV_CONFIG_CDC
235#endif
236
237#ifdef CONFIG_USB_GADGET_DUMMY_HCD
238#define DEV_CONFIG_CDC
239#endif
240
241#ifdef CONFIG_USB_GADGET_GOKU
242#define DEV_CONFIG_CDC
243#endif
244
245#ifdef CONFIG_USB_GADGET_LH7A40X
246#define DEV_CONFIG_CDC
247#endif
248
249#ifdef CONFIG_USB_GADGET_MQ11XX
250#define DEV_CONFIG_CDC
251#endif
252
253#ifdef CONFIG_USB_GADGET_OMAP
254#define DEV_CONFIG_CDC
255#endif
256
257#ifdef CONFIG_USB_GADGET_N9604
258#define DEV_CONFIG_CDC
259#endif
260
261#ifdef CONFIG_USB_GADGET_PXA27X
262#define DEV_CONFIG_CDC
263#endif
264
David Brownell11d54892006-12-11 15:59:04 -0800265#ifdef CONFIG_USB_GADGET_S3C2410
266#define DEV_CONFIG_CDC
267#endif
268
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269#ifdef CONFIG_USB_GADGET_AT91
270#define DEV_CONFIG_CDC
271#endif
272
David Brownell1c05ad42006-01-25 08:45:59 -0800273#ifdef CONFIG_USB_GADGET_MUSBHSFC
274#define DEV_CONFIG_CDC
275#endif
276
Tony Lindgrenbfb2c962006-06-29 22:27:36 -0700277#ifdef CONFIG_USB_GADGET_MUSB_HDRC
David Brownell1c05ad42006-01-25 08:45:59 -0800278#define DEV_CONFIG_CDC
279#endif
280
HÃ¥vard Skinnemoenef3ff462006-02-27 18:15:04 +0100281#ifdef CONFIG_USB_GADGET_HUSB2DEV
282#define DEV_CONFIG_CDC
283#endif
284
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
286/* For CDC-incapable hardware, choose the simple cdc subset.
287 * Anything that talks bulk (without notable bugs) can do this.
288 */
289#ifdef CONFIG_USB_GADGET_PXA2XX
290#define DEV_CONFIG_SUBSET
291#endif
292
293#ifdef CONFIG_USB_GADGET_SH
294#define DEV_CONFIG_SUBSET
295#endif
296
297#ifdef CONFIG_USB_GADGET_SA1100
298/* use non-CDC for backwards compatibility */
299#define DEV_CONFIG_SUBSET
300#endif
301
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
303/*-------------------------------------------------------------------------*/
304
305/* "main" config is either CDC, or its simple subset */
306static inline int is_cdc(struct eth_dev *dev)
307{
308#if !defined(DEV_CONFIG_SUBSET)
309 return 1; /* only cdc possible */
310#elif !defined (DEV_CONFIG_CDC)
311 return 0; /* only subset possible */
312#else
313 return dev->cdc; /* depends on what hardware we found */
314#endif
315}
316
317/* "secondary" RNDIS config may sometimes be activated */
318static inline int rndis_active(struct eth_dev *dev)
319{
320#ifdef CONFIG_USB_ETH_RNDIS
321 return dev->rndis;
322#else
323 return 0;
324#endif
325}
326
327#define subset_active(dev) (!is_cdc(dev) && !rndis_active(dev))
328#define cdc_active(dev) ( is_cdc(dev) && !rndis_active(dev))
329
330
331
332#define DEFAULT_QLEN 2 /* double buffering by default */
333
334/* peak bulk transfer bits-per-second */
David Brownell7e27f182006-06-13 09:54:40 -0700335#define HS_BPS (13 * 512 * 8 * 1000 * 8)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336#define FS_BPS (19 * 64 * 1 * 1000 * 8)
337
338#ifdef CONFIG_USB_GADGET_DUALSPEED
David Brownell907cba32005-04-28 13:48:09 -0700339#define DEVSPEED USB_SPEED_HIGH
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
341static unsigned qmult = 5;
342module_param (qmult, uint, S_IRUGO|S_IWUSR);
343
344
345/* for dual-speed hardware, use deeper queues at highspeed */
346#define qlen(gadget) \
347 (DEFAULT_QLEN*((gadget->speed == USB_SPEED_HIGH) ? qmult : 1))
348
349/* also defer IRQs on highspeed TX */
350#define TX_DELAY qmult
351
David Brownell6cdee102005-04-18 17:39:34 -0700352static inline int BITRATE(struct usb_gadget *g)
353{
354 return (g->speed == USB_SPEED_HIGH) ? HS_BPS : FS_BPS;
355}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
357#else /* full speed (low speed doesn't do bulk) */
David Brownell907cba32005-04-28 13:48:09 -0700358#define DEVSPEED USB_SPEED_FULL
359
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360#define qlen(gadget) DEFAULT_QLEN
361
David Brownell6cdee102005-04-18 17:39:34 -0700362static inline int BITRATE(struct usb_gadget *g)
363{
364 return FS_BPS;
365}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366#endif
367
368
369/*-------------------------------------------------------------------------*/
370
371#define xprintk(d,level,fmt,args...) \
372 printk(level "%s: " fmt , (d)->net->name , ## args)
373
374#ifdef DEBUG
375#undef DEBUG
376#define DEBUG(dev,fmt,args...) \
377 xprintk(dev , KERN_DEBUG , fmt , ## args)
378#else
379#define DEBUG(dev,fmt,args...) \
380 do { } while (0)
381#endif /* DEBUG */
382
383#ifdef VERBOSE
384#define VDEBUG DEBUG
385#else
386#define VDEBUG(dev,fmt,args...) \
387 do { } while (0)
388#endif /* DEBUG */
389
390#define ERROR(dev,fmt,args...) \
391 xprintk(dev , KERN_ERR , fmt , ## args)
392#define WARN(dev,fmt,args...) \
393 xprintk(dev , KERN_WARNING , fmt , ## args)
394#define INFO(dev,fmt,args...) \
395 xprintk(dev , KERN_INFO , fmt , ## args)
396
397/*-------------------------------------------------------------------------*/
398
399/* USB DRIVER HOOKUP (to the hardware driver, below us), mostly
400 * ep0 implementation: descriptors, config management, setup().
401 * also optional class-specific notification interrupt transfer.
402 */
403
404/*
405 * DESCRIPTORS ... most are static, but strings and (full) configuration
406 * descriptors are built on demand. For now we do either full CDC, or
407 * our simple subset, with RNDIS as an optional second configuration.
408 *
409 * RNDIS includes some CDC ACM descriptors ... like CDC Ethernet. But
410 * the class descriptors match a modem (they're ignored; it's really just
411 * Ethernet functionality), they don't need the NOP altsetting, and the
412 * status transfer endpoint isn't optional.
413 */
414
415#define STRING_MANUFACTURER 1
416#define STRING_PRODUCT 2
417#define STRING_ETHADDR 3
418#define STRING_DATA 4
419#define STRING_CONTROL 5
420#define STRING_RNDIS_CONTROL 6
421#define STRING_CDC 7
422#define STRING_SUBSET 8
423#define STRING_RNDIS 9
Aras Vaichas1afc64a2006-02-18 12:31:23 -0800424#define STRING_SERIALNUMBER 10
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
David Brownell340600a2005-04-28 13:45:25 -0700426/* holds our biggest descriptor (or RNDIS response) */
427#define USB_BUFSIZ 256
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
429/*
430 * This device advertises one configuration, eth_config, unless RNDIS
431 * is enabled (rndis_config) on hardware supporting at least two configs.
432 *
433 * NOTE: Controllers like superh_udc should probably be able to use
434 * an RNDIS-only configuration.
435 *
436 * FIXME define some higher-powered configurations to make it easier
437 * to recharge batteries ...
438 */
439
440#define DEV_CONFIG_VALUE 1 /* cdc or subset */
441#define DEV_RNDIS_CONFIG_VALUE 2 /* rndis; optional */
442
443static struct usb_device_descriptor
444device_desc = {
445 .bLength = sizeof device_desc,
446 .bDescriptorType = USB_DT_DEVICE,
447
448 .bcdUSB = __constant_cpu_to_le16 (0x0200),
449
450 .bDeviceClass = USB_CLASS_COMM,
451 .bDeviceSubClass = 0,
452 .bDeviceProtocol = 0,
453
454 .idVendor = __constant_cpu_to_le16 (CDC_VENDOR_NUM),
455 .idProduct = __constant_cpu_to_le16 (CDC_PRODUCT_NUM),
456 .iManufacturer = STRING_MANUFACTURER,
457 .iProduct = STRING_PRODUCT,
458 .bNumConfigurations = 1,
459};
460
461static struct usb_otg_descriptor
462otg_descriptor = {
463 .bLength = sizeof otg_descriptor,
464 .bDescriptorType = USB_DT_OTG,
465
466 .bmAttributes = USB_OTG_SRP,
467};
468
469static struct usb_config_descriptor
470eth_config = {
471 .bLength = sizeof eth_config,
472 .bDescriptorType = USB_DT_CONFIG,
473
474 /* compute wTotalLength on the fly */
475 .bNumInterfaces = 2,
476 .bConfigurationValue = DEV_CONFIG_VALUE,
477 .iConfiguration = STRING_CDC,
478 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
479 .bMaxPower = 50,
480};
481
482#ifdef CONFIG_USB_ETH_RNDIS
David Brownell7e27f182006-06-13 09:54:40 -0700483static struct usb_config_descriptor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484rndis_config = {
485 .bLength = sizeof rndis_config,
486 .bDescriptorType = USB_DT_CONFIG,
487
488 /* compute wTotalLength on the fly */
489 .bNumInterfaces = 2,
490 .bConfigurationValue = DEV_RNDIS_CONFIG_VALUE,
491 .iConfiguration = STRING_RNDIS,
492 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
493 .bMaxPower = 50,
494};
495#endif
496
497/*
498 * Compared to the simple CDC subset, the full CDC Ethernet model adds
499 * three class descriptors, two interface descriptors, optional status
500 * endpoint. Both have a "data" interface and two bulk endpoints.
501 * There are also differences in how control requests are handled.
502 *
David Brownell11d54892006-12-11 15:59:04 -0800503 * RNDIS shares a lot with CDC-Ethernet, since it's a variant of the
504 * CDC-ACM (modem) spec. Unfortunately MSFT's RNDIS driver is buggy; it
505 * may hang or oops. Since bugfixes (or accurate specs, letting Linux
506 * work around those bugs) are unlikely to ever come from MSFT, you may
507 * wish to avoid using RNDIS.
508 *
509 * MCCI offers an alternative to RNDIS if you need to connect to Windows
510 * but have hardware that can't support CDC Ethernet. We add descriptors
511 * to present the CDC Subset as a (nonconformant) CDC MDLM variant called
512 * "SAFE". That borrows from both CDC Ethernet and CDC MDLM. You can
513 * get those drivers from MCCI, or bundled with various products.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 */
515
516#ifdef DEV_CONFIG_CDC
517static struct usb_interface_descriptor
518control_intf = {
519 .bLength = sizeof control_intf,
520 .bDescriptorType = USB_DT_INTERFACE,
521
522 .bInterfaceNumber = 0,
523 /* status endpoint is optional; this may be patched later */
524 .bNumEndpoints = 1,
525 .bInterfaceClass = USB_CLASS_COMM,
526 .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET,
527 .bInterfaceProtocol = USB_CDC_PROTO_NONE,
528 .iInterface = STRING_CONTROL,
529};
530#endif
531
532#ifdef CONFIG_USB_ETH_RNDIS
533static const struct usb_interface_descriptor
534rndis_control_intf = {
535 .bLength = sizeof rndis_control_intf,
536 .bDescriptorType = USB_DT_INTERFACE,
David Brownell7e27f182006-06-13 09:54:40 -0700537
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 .bInterfaceNumber = 0,
539 .bNumEndpoints = 1,
540 .bInterfaceClass = USB_CLASS_COMM,
541 .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
542 .bInterfaceProtocol = USB_CDC_ACM_PROTO_VENDOR,
543 .iInterface = STRING_RNDIS_CONTROL,
544};
545#endif
546
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547static const struct usb_cdc_header_desc header_desc = {
548 .bLength = sizeof header_desc,
549 .bDescriptorType = USB_DT_CS_INTERFACE,
550 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
551
552 .bcdCDC = __constant_cpu_to_le16 (0x0110),
553};
554
David Brownell11d54892006-12-11 15:59:04 -0800555#if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
556
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557static const struct usb_cdc_union_desc union_desc = {
558 .bLength = sizeof union_desc,
559 .bDescriptorType = USB_DT_CS_INTERFACE,
560 .bDescriptorSubType = USB_CDC_UNION_TYPE,
561
562 .bMasterInterface0 = 0, /* index of control interface */
563 .bSlaveInterface0 = 1, /* index of DATA interface */
564};
565
566#endif /* CDC || RNDIS */
567
568#ifdef CONFIG_USB_ETH_RNDIS
569
570static const struct usb_cdc_call_mgmt_descriptor call_mgmt_descriptor = {
David Brownell7e27f182006-06-13 09:54:40 -0700571 .bLength = sizeof call_mgmt_descriptor,
572 .bDescriptorType = USB_DT_CS_INTERFACE,
573 .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
David Brownell7e27f182006-06-13 09:54:40 -0700575 .bmCapabilities = 0x00,
576 .bDataInterface = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577};
578
David Brownell907cba32005-04-28 13:48:09 -0700579static const struct usb_cdc_acm_descriptor acm_descriptor = {
David Brownell7e27f182006-06-13 09:54:40 -0700580 .bLength = sizeof acm_descriptor,
581 .bDescriptorType = USB_DT_CS_INTERFACE,
582 .bDescriptorSubType = USB_CDC_ACM_TYPE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
David Brownell7e27f182006-06-13 09:54:40 -0700584 .bmCapabilities = 0x00,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585};
586
587#endif
588
David Brownell11d54892006-12-11 15:59:04 -0800589#ifndef DEV_CONFIG_CDC
590
591/* "SAFE" loosely follows CDC WMC MDLM, violating the spec in various
592 * ways: data endpoints live in the control interface, there's no data
593 * interface, and it's not used to talk to a cell phone radio.
594 */
595
596static const struct usb_cdc_mdlm_desc mdlm_desc = {
597 .bLength = sizeof mdlm_desc,
598 .bDescriptorType = USB_DT_CS_INTERFACE,
599 .bDescriptorSubType = USB_CDC_MDLM_TYPE,
600
601 .bcdVersion = __constant_cpu_to_le16(0x0100),
602 .bGUID = {
603 0x5d, 0x34, 0xcf, 0x66, 0x11, 0x18, 0x11, 0xd6,
604 0xa2, 0x1a, 0x00, 0x01, 0x02, 0xca, 0x9a, 0x7f,
605 },
606};
607
608/* since "usb_cdc_mdlm_detail_desc" is a variable length structure, we
609 * can't really use its struct. All we do here is say that we're using
610 * the submode of "SAFE" which directly matches the CDC Subset.
611 */
612static const u8 mdlm_detail_desc[] = {
613 6,
614 USB_DT_CS_INTERFACE,
615 USB_CDC_MDLM_DETAIL_TYPE,
616
617 0, /* "SAFE" */
618 0, /* network control capabilities (none) */
619 0, /* network data capabilities ("raw" encapsulation) */
620};
621
622#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
624static const struct usb_cdc_ether_desc ether_desc = {
625 .bLength = sizeof ether_desc,
626 .bDescriptorType = USB_DT_CS_INTERFACE,
627 .bDescriptorSubType = USB_CDC_ETHERNET_TYPE,
628
629 /* this descriptor actually adds value, surprise! */
630 .iMACAddress = STRING_ETHADDR,
631 .bmEthernetStatistics = __constant_cpu_to_le32 (0), /* no statistics */
632 .wMaxSegmentSize = __constant_cpu_to_le16 (ETH_FRAME_LEN),
633 .wNumberMCFilters = __constant_cpu_to_le16 (0),
634 .bNumberPowerFilters = 0,
635};
636
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
638#if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
639
640/* include the status endpoint if we can, even where it's optional.
641 * use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one
Steven Cole093cf722005-05-03 19:07:24 -0600642 * packet, to simplify cancellation; and a big transfer interval, to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 * waste less bandwidth.
644 *
645 * some drivers (like Linux 2.4 cdc-ether!) "need" it to exist even
646 * if they ignore the connect/disconnect notifications that real aether
647 * can provide. more advanced cdc configurations might want to support
648 * encapsulated commands (vendor-specific, using control-OUT).
649 *
650 * RNDIS requires the status endpoint, since it uses that encapsulation
651 * mechanism for its funky RPC scheme.
652 */
David Brownell7e27f182006-06-13 09:54:40 -0700653
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654#define LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */
655#define STATUS_BYTECOUNT 16 /* 8 byte header + data */
656
657static struct usb_endpoint_descriptor
658fs_status_desc = {
659 .bLength = USB_DT_ENDPOINT_SIZE,
660 .bDescriptorType = USB_DT_ENDPOINT,
661
662 .bEndpointAddress = USB_DIR_IN,
663 .bmAttributes = USB_ENDPOINT_XFER_INT,
664 .wMaxPacketSize = __constant_cpu_to_le16 (STATUS_BYTECOUNT),
665 .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC,
666};
667#endif
668
669#ifdef DEV_CONFIG_CDC
670
671/* the default data interface has no endpoints ... */
672
673static const struct usb_interface_descriptor
674data_nop_intf = {
675 .bLength = sizeof data_nop_intf,
676 .bDescriptorType = USB_DT_INTERFACE,
677
678 .bInterfaceNumber = 1,
679 .bAlternateSetting = 0,
680 .bNumEndpoints = 0,
681 .bInterfaceClass = USB_CLASS_CDC_DATA,
682 .bInterfaceSubClass = 0,
683 .bInterfaceProtocol = 0,
684};
685
686/* ... but the "real" data interface has two bulk endpoints */
687
688static const struct usb_interface_descriptor
689data_intf = {
690 .bLength = sizeof data_intf,
691 .bDescriptorType = USB_DT_INTERFACE,
692
693 .bInterfaceNumber = 1,
694 .bAlternateSetting = 1,
695 .bNumEndpoints = 2,
696 .bInterfaceClass = USB_CLASS_CDC_DATA,
697 .bInterfaceSubClass = 0,
698 .bInterfaceProtocol = 0,
699 .iInterface = STRING_DATA,
700};
701
702#endif
703
704#ifdef CONFIG_USB_ETH_RNDIS
705
706/* RNDIS doesn't activate by changing to the "real" altsetting */
707
708static const struct usb_interface_descriptor
709rndis_data_intf = {
710 .bLength = sizeof rndis_data_intf,
711 .bDescriptorType = USB_DT_INTERFACE,
712
713 .bInterfaceNumber = 1,
714 .bAlternateSetting = 0,
715 .bNumEndpoints = 2,
716 .bInterfaceClass = USB_CLASS_CDC_DATA,
717 .bInterfaceSubClass = 0,
718 .bInterfaceProtocol = 0,
719 .iInterface = STRING_DATA,
720};
721
722#endif
723
724#ifdef DEV_CONFIG_SUBSET
725
726/*
727 * "Simple" CDC-subset option is a simple vendor-neutral model that most
728 * full speed controllers can handle: one interface, two bulk endpoints.
David Brownell11d54892006-12-11 15:59:04 -0800729 *
730 * To assist host side drivers, we fancy it up a bit, and add descriptors
731 * so some host side drivers will understand it as a "SAFE" variant.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 */
733
734static const struct usb_interface_descriptor
735subset_data_intf = {
736 .bLength = sizeof subset_data_intf,
737 .bDescriptorType = USB_DT_INTERFACE,
738
739 .bInterfaceNumber = 0,
740 .bAlternateSetting = 0,
741 .bNumEndpoints = 2,
David Brownell11d54892006-12-11 15:59:04 -0800742 .bInterfaceClass = USB_CLASS_COMM,
743 .bInterfaceSubClass = USB_CDC_SUBCLASS_MDLM,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 .bInterfaceProtocol = 0,
745 .iInterface = STRING_DATA,
746};
747
748#endif /* SUBSET */
749
750
751static struct usb_endpoint_descriptor
752fs_source_desc = {
753 .bLength = USB_DT_ENDPOINT_SIZE,
754 .bDescriptorType = USB_DT_ENDPOINT,
755
756 .bEndpointAddress = USB_DIR_IN,
757 .bmAttributes = USB_ENDPOINT_XFER_BULK,
758};
759
760static struct usb_endpoint_descriptor
761fs_sink_desc = {
762 .bLength = USB_DT_ENDPOINT_SIZE,
763 .bDescriptorType = USB_DT_ENDPOINT,
764
765 .bEndpointAddress = USB_DIR_OUT,
766 .bmAttributes = USB_ENDPOINT_XFER_BULK,
767};
768
769static const struct usb_descriptor_header *fs_eth_function [11] = {
770 (struct usb_descriptor_header *) &otg_descriptor,
771#ifdef DEV_CONFIG_CDC
772 /* "cdc" mode descriptors */
773 (struct usb_descriptor_header *) &control_intf,
774 (struct usb_descriptor_header *) &header_desc,
775 (struct usb_descriptor_header *) &union_desc,
776 (struct usb_descriptor_header *) &ether_desc,
777 /* NOTE: status endpoint may need to be removed */
778 (struct usb_descriptor_header *) &fs_status_desc,
779 /* data interface, with altsetting */
780 (struct usb_descriptor_header *) &data_nop_intf,
781 (struct usb_descriptor_header *) &data_intf,
782 (struct usb_descriptor_header *) &fs_source_desc,
783 (struct usb_descriptor_header *) &fs_sink_desc,
784 NULL,
785#endif /* DEV_CONFIG_CDC */
786};
787
788static inline void __init fs_subset_descriptors(void)
789{
790#ifdef DEV_CONFIG_SUBSET
David Brownell11d54892006-12-11 15:59:04 -0800791 /* behavior is "CDC Subset"; extra descriptors say "SAFE" */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 fs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
David Brownell11d54892006-12-11 15:59:04 -0800793 fs_eth_function[2] = (struct usb_descriptor_header *) &header_desc;
794 fs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc;
795 fs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc;
796 fs_eth_function[5] = (struct usb_descriptor_header *) &ether_desc;
797 fs_eth_function[6] = (struct usb_descriptor_header *) &fs_source_desc;
798 fs_eth_function[7] = (struct usb_descriptor_header *) &fs_sink_desc;
799 fs_eth_function[8] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800#else
801 fs_eth_function[1] = NULL;
802#endif
803}
804
805#ifdef CONFIG_USB_ETH_RNDIS
806static const struct usb_descriptor_header *fs_rndis_function [] = {
807 (struct usb_descriptor_header *) &otg_descriptor,
808 /* control interface matches ACM, not Ethernet */
809 (struct usb_descriptor_header *) &rndis_control_intf,
810 (struct usb_descriptor_header *) &header_desc,
811 (struct usb_descriptor_header *) &call_mgmt_descriptor,
812 (struct usb_descriptor_header *) &acm_descriptor,
813 (struct usb_descriptor_header *) &union_desc,
814 (struct usb_descriptor_header *) &fs_status_desc,
815 /* data interface has no altsetting */
816 (struct usb_descriptor_header *) &rndis_data_intf,
817 (struct usb_descriptor_header *) &fs_source_desc,
818 (struct usb_descriptor_header *) &fs_sink_desc,
819 NULL,
820};
821#endif
822
823#ifdef CONFIG_USB_GADGET_DUALSPEED
824
825/*
826 * usb 2.0 devices need to expose both high speed and full speed
827 * descriptors, unless they only run at full speed.
828 */
829
830#if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
831static struct usb_endpoint_descriptor
832hs_status_desc = {
833 .bLength = USB_DT_ENDPOINT_SIZE,
834 .bDescriptorType = USB_DT_ENDPOINT,
835
836 .bmAttributes = USB_ENDPOINT_XFER_INT,
837 .wMaxPacketSize = __constant_cpu_to_le16 (STATUS_BYTECOUNT),
838 .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4,
839};
840#endif /* DEV_CONFIG_CDC */
841
842static struct usb_endpoint_descriptor
843hs_source_desc = {
844 .bLength = USB_DT_ENDPOINT_SIZE,
845 .bDescriptorType = USB_DT_ENDPOINT,
846
847 .bmAttributes = USB_ENDPOINT_XFER_BULK,
848 .wMaxPacketSize = __constant_cpu_to_le16 (512),
849};
850
851static struct usb_endpoint_descriptor
852hs_sink_desc = {
853 .bLength = USB_DT_ENDPOINT_SIZE,
854 .bDescriptorType = USB_DT_ENDPOINT,
855
856 .bmAttributes = USB_ENDPOINT_XFER_BULK,
857 .wMaxPacketSize = __constant_cpu_to_le16 (512),
858};
859
860static struct usb_qualifier_descriptor
861dev_qualifier = {
862 .bLength = sizeof dev_qualifier,
863 .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
864
865 .bcdUSB = __constant_cpu_to_le16 (0x0200),
866 .bDeviceClass = USB_CLASS_COMM,
867
868 .bNumConfigurations = 1,
869};
870
871static const struct usb_descriptor_header *hs_eth_function [11] = {
872 (struct usb_descriptor_header *) &otg_descriptor,
873#ifdef DEV_CONFIG_CDC
874 /* "cdc" mode descriptors */
875 (struct usb_descriptor_header *) &control_intf,
876 (struct usb_descriptor_header *) &header_desc,
877 (struct usb_descriptor_header *) &union_desc,
878 (struct usb_descriptor_header *) &ether_desc,
879 /* NOTE: status endpoint may need to be removed */
880 (struct usb_descriptor_header *) &hs_status_desc,
881 /* data interface, with altsetting */
882 (struct usb_descriptor_header *) &data_nop_intf,
883 (struct usb_descriptor_header *) &data_intf,
884 (struct usb_descriptor_header *) &hs_source_desc,
885 (struct usb_descriptor_header *) &hs_sink_desc,
886 NULL,
887#endif /* DEV_CONFIG_CDC */
888};
889
890static inline void __init hs_subset_descriptors(void)
891{
892#ifdef DEV_CONFIG_SUBSET
David Brownell11d54892006-12-11 15:59:04 -0800893 /* behavior is "CDC Subset"; extra descriptors say "SAFE" */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 hs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
David Brownell11d54892006-12-11 15:59:04 -0800895 hs_eth_function[2] = (struct usb_descriptor_header *) &header_desc;
896 hs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc;
897 hs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc;
898 hs_eth_function[5] = (struct usb_descriptor_header *) &ether_desc;
899 hs_eth_function[6] = (struct usb_descriptor_header *) &hs_source_desc;
900 hs_eth_function[7] = (struct usb_descriptor_header *) &hs_sink_desc;
901 hs_eth_function[8] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902#else
903 hs_eth_function[1] = NULL;
904#endif
905}
906
907#ifdef CONFIG_USB_ETH_RNDIS
908static const struct usb_descriptor_header *hs_rndis_function [] = {
909 (struct usb_descriptor_header *) &otg_descriptor,
910 /* control interface matches ACM, not Ethernet */
911 (struct usb_descriptor_header *) &rndis_control_intf,
912 (struct usb_descriptor_header *) &header_desc,
913 (struct usb_descriptor_header *) &call_mgmt_descriptor,
914 (struct usb_descriptor_header *) &acm_descriptor,
915 (struct usb_descriptor_header *) &union_desc,
916 (struct usb_descriptor_header *) &hs_status_desc,
917 /* data interface has no altsetting */
918 (struct usb_descriptor_header *) &rndis_data_intf,
919 (struct usb_descriptor_header *) &hs_source_desc,
920 (struct usb_descriptor_header *) &hs_sink_desc,
921 NULL,
922};
923#endif
924
925
926/* maxpacket and other transfer characteristics vary by speed. */
927#define ep_desc(g,hs,fs) (((g)->speed==USB_SPEED_HIGH)?(hs):(fs))
928
929#else
930
931/* if there's no high speed support, maxpacket doesn't change. */
David Brownell907cba32005-04-28 13:48:09 -0700932#define ep_desc(g,hs,fs) (((void)(g)), (fs))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
934static inline void __init hs_subset_descriptors(void)
935{
936}
937
938#endif /* !CONFIG_USB_GADGET_DUALSPEED */
939
940/*-------------------------------------------------------------------------*/
941
942/* descriptors that are built on-demand */
943
944static char manufacturer [50];
945static char product_desc [40] = DRIVER_DESC;
Aras Vaichas1afc64a2006-02-18 12:31:23 -0800946static char serial_number [20];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948/* address that the host will use ... usually assigned at random */
949static char ethaddr [2 * ETH_ALEN + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
951/* static strings, in UTF-8 */
952static struct usb_string strings [] = {
953 { STRING_MANUFACTURER, manufacturer, },
954 { STRING_PRODUCT, product_desc, },
Aras Vaichas1afc64a2006-02-18 12:31:23 -0800955 { STRING_SERIALNUMBER, serial_number, },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 { STRING_DATA, "Ethernet Data", },
David Brownell11d54892006-12-11 15:59:04 -0800957 { STRING_ETHADDR, ethaddr, },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958#ifdef DEV_CONFIG_CDC
959 { STRING_CDC, "CDC Ethernet", },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 { STRING_CONTROL, "CDC Communications Control", },
961#endif
962#ifdef DEV_CONFIG_SUBSET
963 { STRING_SUBSET, "CDC Ethernet Subset", },
964#endif
965#ifdef CONFIG_USB_ETH_RNDIS
966 { STRING_RNDIS, "RNDIS", },
967 { STRING_RNDIS_CONTROL, "RNDIS Communications Control", },
968#endif
969 { } /* end of list */
970};
971
972static struct usb_gadget_strings stringtab = {
973 .language = 0x0409, /* en-us */
974 .strings = strings,
975};
976
977/*
978 * one config, two interfaces: control, data.
979 * complications: class descriptors, and an altsetting.
980 */
981static int
982config_buf (enum usb_device_speed speed,
983 u8 *buf, u8 type,
984 unsigned index, int is_otg)
985{
986 int len;
987 const struct usb_config_descriptor *config;
988 const struct usb_descriptor_header **function;
989#ifdef CONFIG_USB_GADGET_DUALSPEED
990 int hs = (speed == USB_SPEED_HIGH);
991
992 if (type == USB_DT_OTHER_SPEED_CONFIG)
993 hs = !hs;
994#define which_fn(t) (hs ? hs_ ## t ## _function : fs_ ## t ## _function)
995#else
996#define which_fn(t) (fs_ ## t ## _function)
997#endif
998
999 if (index >= device_desc.bNumConfigurations)
1000 return -EINVAL;
1001
1002#ifdef CONFIG_USB_ETH_RNDIS
1003 /* list the RNDIS config first, to make Microsoft's drivers
1004 * happy. DOCSIS 1.0 needs this too.
1005 */
1006 if (device_desc.bNumConfigurations == 2 && index == 0) {
1007 config = &rndis_config;
1008 function = which_fn (rndis);
1009 } else
1010#endif
1011 {
1012 config = &eth_config;
1013 function = which_fn (eth);
1014 }
1015
1016 /* for now, don't advertise srp-only devices */
1017 if (!is_otg)
1018 function++;
1019
1020 len = usb_gadget_config_buf (config, buf, USB_BUFSIZ, function);
1021 if (len < 0)
1022 return len;
1023 ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
1024 return len;
1025}
1026
1027/*-------------------------------------------------------------------------*/
1028
Al Viro55016f12005-10-21 03:21:58 -04001029static void eth_start (struct eth_dev *dev, gfp_t gfp_flags);
1030static int alloc_requests (struct eth_dev *dev, unsigned n, gfp_t gfp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
David Brownell907cba32005-04-28 13:48:09 -07001032static int
Al Viro55016f12005-10-21 03:21:58 -04001033set_ether_config (struct eth_dev *dev, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034{
David Brownell907cba32005-04-28 13:48:09 -07001035 int result = 0;
1036 struct usb_gadget *gadget = dev->gadget;
1037
Ian Campbelle8282642005-06-29 10:20:29 +01001038#if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
David Brownell907cba32005-04-28 13:48:09 -07001039 /* status endpoint used for RNDIS and (optionally) CDC */
1040 if (!subset_active(dev) && dev->status_ep) {
1041 dev->status = ep_desc (gadget, &hs_status_desc,
1042 &fs_status_desc);
1043 dev->status_ep->driver_data = dev;
1044
1045 result = usb_ep_enable (dev->status_ep, dev->status);
1046 if (result != 0) {
David Brownell7e27f182006-06-13 09:54:40 -07001047 DEBUG (dev, "enable %s --> %d\n",
David Brownell907cba32005-04-28 13:48:09 -07001048 dev->status_ep->name, result);
1049 goto done;
1050 }
1051 }
Ian Campbelle8282642005-06-29 10:20:29 +01001052#endif
David Brownell907cba32005-04-28 13:48:09 -07001053
David Brownell11d54892006-12-11 15:59:04 -08001054 dev->in = ep_desc(gadget, &hs_source_desc, &fs_source_desc);
David Brownell907cba32005-04-28 13:48:09 -07001055 dev->in_ep->driver_data = dev;
1056
David Brownell11d54892006-12-11 15:59:04 -08001057 dev->out = ep_desc(gadget, &hs_sink_desc, &fs_sink_desc);
David Brownell907cba32005-04-28 13:48:09 -07001058 dev->out_ep->driver_data = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
1060 /* With CDC, the host isn't allowed to use these two data
1061 * endpoints in the default altsetting for the interface.
1062 * so we don't activate them yet. Reset from SET_INTERFACE.
1063 *
1064 * Strictly speaking RNDIS should work the same: activation is
1065 * a side effect of setting a packet filter. Deactivation is
1066 * from REMOTE_NDIS_HALT_MSG, reset from REMOTE_NDIS_RESET_MSG.
1067 */
David Brownell907cba32005-04-28 13:48:09 -07001068 if (!cdc_active(dev)) {
1069 result = usb_ep_enable (dev->in_ep, dev->in);
1070 if (result != 0) {
David Brownell7e27f182006-06-13 09:54:40 -07001071 DEBUG(dev, "enable %s --> %d\n",
David Brownell907cba32005-04-28 13:48:09 -07001072 dev->in_ep->name, result);
1073 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 }
1075
David Brownell907cba32005-04-28 13:48:09 -07001076 result = usb_ep_enable (dev->out_ep, dev->out);
1077 if (result != 0) {
David Brownell7e27f182006-06-13 09:54:40 -07001078 DEBUG (dev, "enable %s --> %d\n",
Matt Reimer4186c292006-06-07 11:46:13 -07001079 dev->out_ep->name, result);
David Brownell907cba32005-04-28 13:48:09 -07001080 goto done;
1081 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083
David Brownell907cba32005-04-28 13:48:09 -07001084done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 if (result == 0)
1086 result = alloc_requests (dev, qlen (gadget), gfp_flags);
1087
1088 /* on error, disable any endpoints */
1089 if (result < 0) {
David Brownell907cba32005-04-28 13:48:09 -07001090 if (!subset_active(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 (void) usb_ep_disable (dev->status_ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 dev->status = NULL;
David Brownell907cba32005-04-28 13:48:09 -07001093 (void) usb_ep_disable (dev->in_ep);
1094 (void) usb_ep_disable (dev->out_ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 dev->in = NULL;
1096 dev->out = NULL;
1097 } else
1098
1099 /* activate non-CDC configs right away
1100 * this isn't strictly according to the RNDIS spec
1101 */
David Brownell907cba32005-04-28 13:48:09 -07001102 if (!cdc_active (dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 netif_carrier_on (dev->net);
1104 if (netif_running (dev->net)) {
1105 spin_unlock (&dev->lock);
1106 eth_start (dev, GFP_ATOMIC);
1107 spin_lock (&dev->lock);
1108 }
1109 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110
1111 if (result == 0)
1112 DEBUG (dev, "qlen %d\n", qlen (gadget));
1113
1114 /* caller is responsible for cleanup on error */
1115 return result;
1116}
1117
1118static void eth_reset_config (struct eth_dev *dev)
1119{
1120 struct usb_request *req;
1121
1122 if (dev->config == 0)
1123 return;
1124
1125 DEBUG (dev, "%s\n", __FUNCTION__);
1126
1127 netif_stop_queue (dev->net);
1128 netif_carrier_off (dev->net);
David Brownell340600a2005-04-28 13:45:25 -07001129 rndis_uninit(dev->rndis_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130
1131 /* disable endpoints, forcing (synchronous) completion of
1132 * pending i/o. then free the requests.
1133 */
1134 if (dev->in) {
1135 usb_ep_disable (dev->in_ep);
David Brownell789851c2006-08-21 15:26:38 -07001136 spin_lock(&dev->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 while (likely (!list_empty (&dev->tx_reqs))) {
1138 req = container_of (dev->tx_reqs.next,
1139 struct usb_request, list);
1140 list_del (&req->list);
David Brownell789851c2006-08-21 15:26:38 -07001141
1142 spin_unlock(&dev->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 usb_ep_free_request (dev->in_ep, req);
David Brownell789851c2006-08-21 15:26:38 -07001144 spin_lock(&dev->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 }
David Brownell789851c2006-08-21 15:26:38 -07001146 spin_unlock(&dev->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 }
1148 if (dev->out) {
1149 usb_ep_disable (dev->out_ep);
David Brownell789851c2006-08-21 15:26:38 -07001150 spin_lock(&dev->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 while (likely (!list_empty (&dev->rx_reqs))) {
1152 req = container_of (dev->rx_reqs.next,
1153 struct usb_request, list);
1154 list_del (&req->list);
David Brownell789851c2006-08-21 15:26:38 -07001155
1156 spin_unlock(&dev->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 usb_ep_free_request (dev->out_ep, req);
David Brownell789851c2006-08-21 15:26:38 -07001158 spin_lock(&dev->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 }
David Brownell789851c2006-08-21 15:26:38 -07001160 spin_unlock(&dev->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 }
1162
1163 if (dev->status) {
1164 usb_ep_disable (dev->status_ep);
1165 }
David Brownell907cba32005-04-28 13:48:09 -07001166 dev->rndis = 0;
1167 dev->cdc_filter = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 dev->config = 0;
1169}
1170
1171/* change our operational config. must agree with the code
1172 * that returns config descriptors, and altsetting code.
1173 */
1174static int
Al Viro55016f12005-10-21 03:21:58 -04001175eth_set_config (struct eth_dev *dev, unsigned number, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176{
1177 int result = 0;
1178 struct usb_gadget *gadget = dev->gadget;
1179
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 if (gadget_is_sa1100 (gadget)
1181 && dev->config
1182 && atomic_read (&dev->tx_qlen) != 0) {
1183 /* tx fifo is full, but we can't clear it...*/
1184 INFO (dev, "can't change configurations\n");
1185 return -ESPIPE;
1186 }
1187 eth_reset_config (dev);
1188
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 switch (number) {
1190 case DEV_CONFIG_VALUE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 result = set_ether_config (dev, gfp_flags);
1192 break;
1193#ifdef CONFIG_USB_ETH_RNDIS
1194 case DEV_RNDIS_CONFIG_VALUE:
1195 dev->rndis = 1;
1196 result = set_ether_config (dev, gfp_flags);
1197 break;
1198#endif
1199 default:
1200 result = -EINVAL;
1201 /* FALL THROUGH */
1202 case 0:
1203 break;
1204 }
1205
1206 if (result) {
1207 if (number)
1208 eth_reset_config (dev);
1209 usb_gadget_vbus_draw(dev->gadget,
1210 dev->gadget->is_otg ? 8 : 100);
1211 } else {
1212 char *speed;
1213 unsigned power;
1214
1215 power = 2 * eth_config.bMaxPower;
1216 usb_gadget_vbus_draw(dev->gadget, power);
1217
1218 switch (gadget->speed) {
1219 case USB_SPEED_FULL: speed = "full"; break;
1220#ifdef CONFIG_USB_GADGET_DUALSPEED
1221 case USB_SPEED_HIGH: speed = "high"; break;
1222#endif
David Brownell7e27f182006-06-13 09:54:40 -07001223 default: speed = "?"; break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 }
1225
1226 dev->config = number;
1227 INFO (dev, "%s speed config #%d: %d mA, %s, using %s\n",
1228 speed, number, power, driver_desc,
David Brownell45e45ab2005-05-16 08:26:38 -07001229 rndis_active(dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 ? "RNDIS"
David Brownell45e45ab2005-05-16 08:26:38 -07001231 : (cdc_active(dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 ? "CDC Ethernet"
1233 : "CDC Ethernet Subset"));
1234 }
1235 return result;
1236}
1237
1238/*-------------------------------------------------------------------------*/
1239
1240#ifdef DEV_CONFIG_CDC
1241
David Brownell907cba32005-04-28 13:48:09 -07001242/* The interrupt endpoint is used in CDC networking models (Ethernet, ATM)
1243 * only to notify the host about link status changes (which we support) or
1244 * report completion of some encapsulated command (as used in RNDIS). Since
1245 * we want this CDC Ethernet code to be vendor-neutral, we don't use that
1246 * command mechanism; and only one status request is ever queued.
1247 */
1248
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249static void eth_status_complete (struct usb_ep *ep, struct usb_request *req)
1250{
1251 struct usb_cdc_notification *event = req->buf;
1252 int value = req->status;
1253 struct eth_dev *dev = ep->driver_data;
1254
1255 /* issue the second notification if host reads the first */
1256 if (event->bNotificationType == USB_CDC_NOTIFY_NETWORK_CONNECTION
1257 && value == 0) {
1258 __le32 *data = req->buf + sizeof *event;
1259
1260 event->bmRequestType = 0xA1;
1261 event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE;
1262 event->wValue = __constant_cpu_to_le16 (0);
1263 event->wIndex = __constant_cpu_to_le16 (1);
1264 event->wLength = __constant_cpu_to_le16 (8);
1265
1266 /* SPEED_CHANGE data is up/down speeds in bits/sec */
1267 data [0] = data [1] = cpu_to_le32 (BITRATE (dev->gadget));
1268
1269 req->length = STATUS_BYTECOUNT;
1270 value = usb_ep_queue (ep, req, GFP_ATOMIC);
1271 DEBUG (dev, "send SPEED_CHANGE --> %d\n", value);
1272 if (value == 0)
1273 return;
1274 } else if (value != -ECONNRESET)
1275 DEBUG (dev, "event %02x --> %d\n",
1276 event->bNotificationType, value);
David Brownell907cba32005-04-28 13:48:09 -07001277 req->context = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278}
1279
1280static void issue_start_status (struct eth_dev *dev)
1281{
1282 struct usb_request *req = dev->stat_req;
1283 struct usb_cdc_notification *event;
1284 int value;
David Brownell7e27f182006-06-13 09:54:40 -07001285
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 DEBUG (dev, "%s, flush old status first\n", __FUNCTION__);
1287
1288 /* flush old status
1289 *
1290 * FIXME ugly idiom, maybe we'd be better with just
1291 * a "cancel the whole queue" primitive since any
1292 * unlink-one primitive has way too many error modes.
1293 * here, we "know" toggle is already clear...
David Brownell907cba32005-04-28 13:48:09 -07001294 *
1295 * FIXME iff req->context != null just dequeue it
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 */
1297 usb_ep_disable (dev->status_ep);
1298 usb_ep_enable (dev->status_ep, dev->status);
1299
1300 /* 3.8.1 says to issue first NETWORK_CONNECTION, then
1301 * a SPEED_CHANGE. could be useful in some configs.
1302 */
1303 event = req->buf;
1304 event->bmRequestType = 0xA1;
1305 event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION;
1306 event->wValue = __constant_cpu_to_le16 (1); /* connected */
1307 event->wIndex = __constant_cpu_to_le16 (1);
1308 event->wLength = 0;
1309
1310 req->length = sizeof *event;
1311 req->complete = eth_status_complete;
David Brownell907cba32005-04-28 13:48:09 -07001312 req->context = dev;
1313
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 value = usb_ep_queue (dev->status_ep, req, GFP_ATOMIC);
1315 if (value < 0)
1316 DEBUG (dev, "status buf queue --> %d\n", value);
1317}
1318
1319#endif
1320
1321/*-------------------------------------------------------------------------*/
1322
1323static void eth_setup_complete (struct usb_ep *ep, struct usb_request *req)
1324{
1325 if (req->status || req->actual != req->length)
1326 DEBUG ((struct eth_dev *) ep->driver_data,
1327 "setup complete --> %d, %d/%d\n",
1328 req->status, req->actual, req->length);
1329}
1330
1331#ifdef CONFIG_USB_ETH_RNDIS
1332
1333static void rndis_response_complete (struct usb_ep *ep, struct usb_request *req)
1334{
1335 if (req->status || req->actual != req->length)
1336 DEBUG ((struct eth_dev *) ep->driver_data,
1337 "rndis response complete --> %d, %d/%d\n",
1338 req->status, req->actual, req->length);
1339
1340 /* done sending after USB_CDC_GET_ENCAPSULATED_RESPONSE */
1341}
1342
1343static void rndis_command_complete (struct usb_ep *ep, struct usb_request *req)
1344{
1345 struct eth_dev *dev = ep->driver_data;
1346 int status;
David Brownell7e27f182006-06-13 09:54:40 -07001347
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 /* received RNDIS command from USB_CDC_SEND_ENCAPSULATED_COMMAND */
1349 spin_lock(&dev->lock);
1350 status = rndis_msg_parser (dev->rndis_config, (u8 *) req->buf);
1351 if (status < 0)
1352 ERROR(dev, "%s: rndis parse error %d\n", __FUNCTION__, status);
1353 spin_unlock(&dev->lock);
1354}
1355
1356#endif /* RNDIS */
1357
1358/*
1359 * The setup() callback implements all the ep0 functionality that's not
1360 * handled lower down. CDC has a number of less-common features:
1361 *
1362 * - two interfaces: control, and ethernet data
1363 * - Ethernet data interface has two altsettings: default, and active
1364 * - class-specific descriptors for the control interface
1365 * - class-specific control requests
1366 */
1367static int
1368eth_setup (struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
1369{
1370 struct eth_dev *dev = get_gadget_data (gadget);
1371 struct usb_request *req = dev->req;
1372 int value = -EOPNOTSUPP;
David Brownell1bbc1692005-05-07 13:05:13 -07001373 u16 wIndex = le16_to_cpu(ctrl->wIndex);
1374 u16 wValue = le16_to_cpu(ctrl->wValue);
1375 u16 wLength = le16_to_cpu(ctrl->wLength);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376
1377 /* descriptors just go into the pre-allocated ep0 buffer,
1378 * while config change events may enable network traffic.
1379 */
1380 req->complete = eth_setup_complete;
1381 switch (ctrl->bRequest) {
1382
1383 case USB_REQ_GET_DESCRIPTOR:
1384 if (ctrl->bRequestType != USB_DIR_IN)
1385 break;
1386 switch (wValue >> 8) {
1387
1388 case USB_DT_DEVICE:
1389 value = min (wLength, (u16) sizeof device_desc);
1390 memcpy (req->buf, &device_desc, value);
1391 break;
1392#ifdef CONFIG_USB_GADGET_DUALSPEED
1393 case USB_DT_DEVICE_QUALIFIER:
1394 if (!gadget->is_dualspeed)
1395 break;
1396 value = min (wLength, (u16) sizeof dev_qualifier);
1397 memcpy (req->buf, &dev_qualifier, value);
1398 break;
1399
1400 case USB_DT_OTHER_SPEED_CONFIG:
1401 if (!gadget->is_dualspeed)
1402 break;
1403 // FALLTHROUGH
1404#endif /* CONFIG_USB_GADGET_DUALSPEED */
1405 case USB_DT_CONFIG:
1406 value = config_buf (gadget->speed, req->buf,
1407 wValue >> 8,
1408 wValue & 0xff,
1409 gadget->is_otg);
1410 if (value >= 0)
1411 value = min (wLength, (u16) value);
1412 break;
1413
1414 case USB_DT_STRING:
1415 value = usb_gadget_get_string (&stringtab,
1416 wValue & 0xff, req->buf);
1417 if (value >= 0)
1418 value = min (wLength, (u16) value);
1419 break;
1420 }
1421 break;
1422
1423 case USB_REQ_SET_CONFIGURATION:
1424 if (ctrl->bRequestType != 0)
1425 break;
1426 if (gadget->a_hnp_support)
1427 DEBUG (dev, "HNP available\n");
1428 else if (gadget->a_alt_hnp_support)
1429 DEBUG (dev, "HNP needs a different root port\n");
1430 spin_lock (&dev->lock);
1431 value = eth_set_config (dev, wValue, GFP_ATOMIC);
1432 spin_unlock (&dev->lock);
1433 break;
1434 case USB_REQ_GET_CONFIGURATION:
1435 if (ctrl->bRequestType != USB_DIR_IN)
1436 break;
1437 *(u8 *)req->buf = dev->config;
1438 value = min (wLength, (u16) 1);
1439 break;
1440
1441 case USB_REQ_SET_INTERFACE:
1442 if (ctrl->bRequestType != USB_RECIP_INTERFACE
1443 || !dev->config
1444 || wIndex > 1)
1445 break;
David Brownell45e45ab2005-05-16 08:26:38 -07001446 if (!cdc_active(dev) && wIndex != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 break;
1448 spin_lock (&dev->lock);
1449
1450 /* PXA hardware partially handles SET_INTERFACE;
1451 * we need to kluge around that interference.
1452 */
1453 if (gadget_is_pxa (gadget)) {
1454 value = eth_set_config (dev, DEV_CONFIG_VALUE,
1455 GFP_ATOMIC);
1456 goto done_set_intf;
1457 }
1458
1459#ifdef DEV_CONFIG_CDC
1460 switch (wIndex) {
1461 case 0: /* control/master intf */
1462 if (wValue != 0)
1463 break;
1464 if (dev->status) {
1465 usb_ep_disable (dev->status_ep);
1466 usb_ep_enable (dev->status_ep, dev->status);
1467 }
1468 value = 0;
1469 break;
1470 case 1: /* data intf */
1471 if (wValue > 1)
1472 break;
1473 usb_ep_disable (dev->in_ep);
1474 usb_ep_disable (dev->out_ep);
1475
1476 /* CDC requires the data transfers not be done from
1477 * the default interface setting ... also, setting
David Brownell907cba32005-04-28 13:48:09 -07001478 * the non-default interface resets filters etc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 */
1480 if (wValue == 1) {
David Brownell907cba32005-04-28 13:48:09 -07001481 if (!cdc_active (dev))
1482 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 usb_ep_enable (dev->in_ep, dev->in);
1484 usb_ep_enable (dev->out_ep, dev->out);
1485 dev->cdc_filter = DEFAULT_FILTER;
1486 netif_carrier_on (dev->net);
1487 if (dev->status)
1488 issue_start_status (dev);
1489 if (netif_running (dev->net)) {
1490 spin_unlock (&dev->lock);
1491 eth_start (dev, GFP_ATOMIC);
1492 spin_lock (&dev->lock);
1493 }
1494 } else {
1495 netif_stop_queue (dev->net);
1496 netif_carrier_off (dev->net);
1497 }
1498 value = 0;
1499 break;
1500 }
1501#else
1502 /* FIXME this is wrong, as is the assumption that
1503 * all non-PXA hardware talks real CDC ...
1504 */
1505 dev_warn (&gadget->dev, "set_interface ignored!\n");
1506#endif /* DEV_CONFIG_CDC */
1507
1508done_set_intf:
1509 spin_unlock (&dev->lock);
1510 break;
1511 case USB_REQ_GET_INTERFACE:
1512 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE)
1513 || !dev->config
1514 || wIndex > 1)
1515 break;
David Brownell45e45ab2005-05-16 08:26:38 -07001516 if (!(cdc_active(dev) || rndis_active(dev)) && wIndex != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 break;
1518
1519 /* for CDC, iff carrier is on, data interface is active. */
David Brownell45e45ab2005-05-16 08:26:38 -07001520 if (rndis_active(dev) || wIndex != 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 *(u8 *)req->buf = 0;
1522 else
1523 *(u8 *)req->buf = netif_carrier_ok (dev->net) ? 1 : 0;
1524 value = min (wLength, (u16) 1);
1525 break;
1526
1527#ifdef DEV_CONFIG_CDC
1528 case USB_CDC_SET_ETHERNET_PACKET_FILTER:
1529 /* see 6.2.30: no data, wIndex = interface,
1530 * wValue = packet filter bitmap
1531 */
1532 if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
David Brownell45e45ab2005-05-16 08:26:38 -07001533 || !cdc_active(dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 || wLength != 0
1535 || wIndex > 1)
1536 break;
1537 DEBUG (dev, "packet filter %02x\n", wValue);
1538 dev->cdc_filter = wValue;
1539 value = 0;
1540 break;
1541
1542 /* and potentially:
1543 * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS:
1544 * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER:
1545 * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER:
1546 * case USB_CDC_GET_ETHERNET_STATISTIC:
1547 */
1548
1549#endif /* DEV_CONFIG_CDC */
1550
David Brownell7e27f182006-06-13 09:54:40 -07001551#ifdef CONFIG_USB_ETH_RNDIS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 /* RNDIS uses the CDC command encapsulation mechanism to implement
1553 * an RPC scheme, with much getting/setting of attributes by OID.
1554 */
1555 case USB_CDC_SEND_ENCAPSULATED_COMMAND:
1556 if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
David Brownell45e45ab2005-05-16 08:26:38 -07001557 || !rndis_active(dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 || wLength > USB_BUFSIZ
1559 || wValue
1560 || rndis_control_intf.bInterfaceNumber
1561 != wIndex)
1562 break;
1563 /* read the request, then process it */
1564 value = wLength;
1565 req->complete = rndis_command_complete;
1566 /* later, rndis_control_ack () sends a notification */
1567 break;
David Brownell7e27f182006-06-13 09:54:40 -07001568
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 case USB_CDC_GET_ENCAPSULATED_RESPONSE:
1570 if ((USB_DIR_IN|USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1571 == ctrl->bRequestType
David Brownell45e45ab2005-05-16 08:26:38 -07001572 && rndis_active(dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 // && wLength >= 0x0400
1574 && !wValue
1575 && rndis_control_intf.bInterfaceNumber
1576 == wIndex) {
1577 u8 *buf;
1578
1579 /* return the result */
1580 buf = rndis_get_next_response (dev->rndis_config,
1581 &value);
1582 if (buf) {
1583 memcpy (req->buf, buf, value);
1584 req->complete = rndis_response_complete;
1585 rndis_free_response(dev->rndis_config, buf);
1586 }
1587 /* else stalls ... spec says to avoid that */
1588 }
1589 break;
1590#endif /* RNDIS */
1591
1592 default:
1593 VDEBUG (dev,
1594 "unknown control req%02x.%02x v%04x i%04x l%d\n",
1595 ctrl->bRequestType, ctrl->bRequest,
1596 wValue, wIndex, wLength);
1597 }
1598
1599 /* respond with data transfer before status phase? */
1600 if (value >= 0) {
1601 req->length = value;
1602 req->zero = value < wLength
1603 && (value % gadget->ep0->maxpacket) == 0;
1604 value = usb_ep_queue (gadget->ep0, req, GFP_ATOMIC);
1605 if (value < 0) {
1606 DEBUG (dev, "ep_queue --> %d\n", value);
1607 req->status = 0;
1608 eth_setup_complete (gadget->ep0, req);
1609 }
1610 }
1611
1612 /* host either stalls (value < 0) or reports success */
1613 return value;
1614}
1615
1616static void
1617eth_disconnect (struct usb_gadget *gadget)
1618{
1619 struct eth_dev *dev = get_gadget_data (gadget);
1620 unsigned long flags;
1621
1622 spin_lock_irqsave (&dev->lock, flags);
1623 netif_stop_queue (dev->net);
1624 netif_carrier_off (dev->net);
1625 eth_reset_config (dev);
1626 spin_unlock_irqrestore (&dev->lock, flags);
1627
1628 /* FIXME RNDIS should enter RNDIS_UNINITIALIZED */
1629
1630 /* next we may get setup() calls to enumerate new connections;
1631 * or an unbind() during shutdown (including removing module).
1632 */
1633}
1634
1635/*-------------------------------------------------------------------------*/
1636
1637/* NETWORK DRIVER HOOKUP (to the layer above this driver) */
1638
1639static int eth_change_mtu (struct net_device *net, int new_mtu)
1640{
1641 struct eth_dev *dev = netdev_priv(net);
1642
David Brownell7802ac52006-01-22 10:33:27 -08001643 if (dev->rndis)
1644 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645
1646 if (new_mtu <= ETH_HLEN || new_mtu > ETH_FRAME_LEN)
1647 return -ERANGE;
1648 /* no zero-length packet read wanted after mtu-sized packets */
1649 if (((new_mtu + sizeof (struct ethhdr)) % dev->in_ep->maxpacket) == 0)
1650 return -EDOM;
1651 net->mtu = new_mtu;
1652 return 0;
1653}
1654
1655static struct net_device_stats *eth_get_stats (struct net_device *net)
1656{
1657 return &((struct eth_dev *)netdev_priv(net))->stats;
1658}
1659
1660static void eth_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *p)
1661{
1662 struct eth_dev *dev = netdev_priv(net);
1663 strlcpy(p->driver, shortname, sizeof p->driver);
1664 strlcpy(p->version, DRIVER_VERSION, sizeof p->version);
1665 strlcpy(p->fw_version, dev->gadget->name, sizeof p->fw_version);
1666 strlcpy (p->bus_info, dev->gadget->dev.bus_id, sizeof p->bus_info);
1667}
1668
1669static u32 eth_get_link(struct net_device *net)
1670{
1671 struct eth_dev *dev = netdev_priv(net);
1672 return dev->gadget->speed != USB_SPEED_UNKNOWN;
1673}
1674
1675static struct ethtool_ops ops = {
1676 .get_drvinfo = eth_get_drvinfo,
1677 .get_link = eth_get_link
1678};
1679
1680static void defer_kevent (struct eth_dev *dev, int flag)
1681{
1682 if (test_and_set_bit (flag, &dev->todo))
1683 return;
1684 if (!schedule_work (&dev->work))
1685 ERROR (dev, "kevent %d may have been dropped\n", flag);
1686 else
1687 DEBUG (dev, "kevent %d scheduled\n", flag);
1688}
1689
1690static void rx_complete (struct usb_ep *ep, struct usb_request *req);
1691
1692static int
Al Viro55016f12005-10-21 03:21:58 -04001693rx_submit (struct eth_dev *dev, struct usb_request *req, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694{
1695 struct sk_buff *skb;
1696 int retval = -ENOMEM;
1697 size_t size;
1698
1699 /* Padding up to RX_EXTRA handles minor disagreements with host.
1700 * Normally we use the USB "terminate on short read" convention;
1701 * so allow up to (N*maxpacket), since that memory is normally
1702 * already allocated. Some hardware doesn't deal well with short
1703 * reads (e.g. DMA must be N*maxpacket), so for now don't trim a
1704 * byte off the end (to force hardware errors on overflow).
1705 *
1706 * RNDIS uses internal framing, and explicitly allows senders to
1707 * pad to end-of-packet. That's potentially nice for speed,
1708 * but means receivers can't recover synch on their own.
1709 */
1710 size = (sizeof (struct ethhdr) + dev->net->mtu + RX_EXTRA);
1711 size += dev->out_ep->maxpacket - 1;
David Brownell907cba32005-04-28 13:48:09 -07001712 if (rndis_active(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 size += sizeof (struct rndis_packet_msg_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 size -= size % dev->out_ep->maxpacket;
1715
1716 if ((skb = alloc_skb (size + NET_IP_ALIGN, gfp_flags)) == 0) {
1717 DEBUG (dev, "no rx skb\n");
1718 goto enomem;
1719 }
David Brownell7e27f182006-06-13 09:54:40 -07001720
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 /* Some platforms perform better when IP packets are aligned,
1722 * but on at least one, checksumming fails otherwise. Note:
David Brownell6cdee102005-04-18 17:39:34 -07001723 * RNDIS headers involve variable numbers of LE32 values.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 */
1725 skb_reserve(skb, NET_IP_ALIGN);
1726
1727 req->buf = skb->data;
1728 req->length = size;
1729 req->complete = rx_complete;
1730 req->context = skb;
1731
1732 retval = usb_ep_queue (dev->out_ep, req, gfp_flags);
1733 if (retval == -ENOMEM)
1734enomem:
1735 defer_kevent (dev, WORK_RX_MEMORY);
1736 if (retval) {
1737 DEBUG (dev, "rx submit --> %d\n", retval);
Erik Hovlandb8d297c2007-04-23 10:50:15 -07001738 if (skb)
1739 dev_kfree_skb_any(skb);
David Brownell789851c2006-08-21 15:26:38 -07001740 spin_lock(&dev->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 list_add (&req->list, &dev->rx_reqs);
David Brownell789851c2006-08-21 15:26:38 -07001742 spin_unlock(&dev->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 }
1744 return retval;
1745}
1746
1747static void rx_complete (struct usb_ep *ep, struct usb_request *req)
1748{
1749 struct sk_buff *skb = req->context;
1750 struct eth_dev *dev = ep->driver_data;
1751 int status = req->status;
1752
1753 switch (status) {
1754
1755 /* normal completion */
1756 case 0:
1757 skb_put (skb, req->actual);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 /* we know MaxPacketsPerTransfer == 1 here */
David Brownell45e45ab2005-05-16 08:26:38 -07001759 if (rndis_active(dev))
David Brownell6cdee102005-04-18 17:39:34 -07001760 status = rndis_rm_hdr (skb);
David Brownell6cdee102005-04-18 17:39:34 -07001761 if (status < 0
1762 || ETH_HLEN > skb->len
1763 || skb->len > ETH_FRAME_LEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 dev->stats.rx_errors++;
1765 dev->stats.rx_length_errors++;
1766 DEBUG (dev, "rx length %d\n", skb->len);
1767 break;
1768 }
1769
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 skb->protocol = eth_type_trans (skb, dev->net);
1771 dev->stats.rx_packets++;
1772 dev->stats.rx_bytes += skb->len;
1773
1774 /* no buffer copies needed, unless hardware can't
1775 * use skb buffers.
1776 */
1777 status = netif_rx (skb);
1778 skb = NULL;
1779 break;
1780
1781 /* software-driven interface shutdown */
1782 case -ECONNRESET: // unlink
1783 case -ESHUTDOWN: // disconnect etc
1784 VDEBUG (dev, "rx shutdown, code %d\n", status);
1785 goto quiesce;
1786
1787 /* for hardware automagic (such as pxa) */
1788 case -ECONNABORTED: // endpoint reset
1789 DEBUG (dev, "rx %s reset\n", ep->name);
1790 defer_kevent (dev, WORK_RX_MEMORY);
1791quiesce:
1792 dev_kfree_skb_any (skb);
1793 goto clean;
1794
1795 /* data overrun */
1796 case -EOVERFLOW:
1797 dev->stats.rx_over_errors++;
1798 // FALLTHROUGH
David Brownell7e27f182006-06-13 09:54:40 -07001799
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 default:
1801 dev->stats.rx_errors++;
1802 DEBUG (dev, "rx status %d\n", status);
1803 break;
1804 }
1805
1806 if (skb)
1807 dev_kfree_skb_any (skb);
1808 if (!netif_running (dev->net)) {
1809clean:
David Brownell789851c2006-08-21 15:26:38 -07001810 spin_lock(&dev->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 list_add (&req->list, &dev->rx_reqs);
David Brownell789851c2006-08-21 15:26:38 -07001812 spin_unlock(&dev->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 req = NULL;
1814 }
1815 if (req)
1816 rx_submit (dev, req, GFP_ATOMIC);
1817}
1818
1819static int prealloc (struct list_head *list, struct usb_ep *ep,
Al Viro55016f12005-10-21 03:21:58 -04001820 unsigned n, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821{
1822 unsigned i;
1823 struct usb_request *req;
1824
1825 if (!n)
1826 return -ENOMEM;
1827
1828 /* queue/recycle up to N requests */
1829 i = n;
1830 list_for_each_entry (req, list, list) {
1831 if (i-- == 0)
1832 goto extra;
1833 }
1834 while (i--) {
1835 req = usb_ep_alloc_request (ep, gfp_flags);
1836 if (!req)
1837 return list_empty (list) ? -ENOMEM : 0;
1838 list_add (&req->list, list);
1839 }
1840 return 0;
1841
1842extra:
1843 /* free extras */
1844 for (;;) {
1845 struct list_head *next;
1846
1847 next = req->list.next;
1848 list_del (&req->list);
1849 usb_ep_free_request (ep, req);
1850
1851 if (next == list)
1852 break;
1853
1854 req = container_of (next, struct usb_request, list);
1855 }
1856 return 0;
1857}
1858
Al Viro55016f12005-10-21 03:21:58 -04001859static int alloc_requests (struct eth_dev *dev, unsigned n, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860{
1861 int status;
1862
David Brownell789851c2006-08-21 15:26:38 -07001863 spin_lock(&dev->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 status = prealloc (&dev->tx_reqs, dev->in_ep, n, gfp_flags);
1865 if (status < 0)
1866 goto fail;
1867 status = prealloc (&dev->rx_reqs, dev->out_ep, n, gfp_flags);
1868 if (status < 0)
1869 goto fail;
David Brownell789851c2006-08-21 15:26:38 -07001870 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871fail:
1872 DEBUG (dev, "can't alloc requests\n");
David Brownell789851c2006-08-21 15:26:38 -07001873done:
1874 spin_unlock(&dev->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 return status;
1876}
1877
Al Viro55016f12005-10-21 03:21:58 -04001878static void rx_fill (struct eth_dev *dev, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879{
1880 struct usb_request *req;
1881 unsigned long flags;
1882
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 /* fill unused rxq slots with some skb */
David Brownell789851c2006-08-21 15:26:38 -07001884 spin_lock_irqsave(&dev->req_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 while (!list_empty (&dev->rx_reqs)) {
1886 req = container_of (dev->rx_reqs.next,
1887 struct usb_request, list);
1888 list_del_init (&req->list);
David Brownell789851c2006-08-21 15:26:38 -07001889 spin_unlock_irqrestore(&dev->req_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890
1891 if (rx_submit (dev, req, gfp_flags) < 0) {
1892 defer_kevent (dev, WORK_RX_MEMORY);
1893 return;
1894 }
1895
David Brownell789851c2006-08-21 15:26:38 -07001896 spin_lock_irqsave(&dev->req_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 }
David Brownell789851c2006-08-21 15:26:38 -07001898 spin_unlock_irqrestore(&dev->req_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899}
1900
David Howellsc4028952006-11-22 14:57:56 +00001901static void eth_work (struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902{
David Howellsc4028952006-11-22 14:57:56 +00001903 struct eth_dev *dev = container_of(work, struct eth_dev, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904
David Brownell907cba32005-04-28 13:48:09 -07001905 if (test_and_clear_bit (WORK_RX_MEMORY, &dev->todo)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 if (netif_running (dev->net))
1907 rx_fill (dev, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 }
1909
1910 if (dev->todo)
1911 DEBUG (dev, "work done, flags = 0x%lx\n", dev->todo);
1912}
1913
1914static void tx_complete (struct usb_ep *ep, struct usb_request *req)
1915{
1916 struct sk_buff *skb = req->context;
1917 struct eth_dev *dev = ep->driver_data;
1918
1919 switch (req->status) {
1920 default:
1921 dev->stats.tx_errors++;
1922 VDEBUG (dev, "tx err %d\n", req->status);
1923 /* FALLTHROUGH */
1924 case -ECONNRESET: // unlink
1925 case -ESHUTDOWN: // disconnect etc
1926 break;
1927 case 0:
1928 dev->stats.tx_bytes += skb->len;
1929 }
1930 dev->stats.tx_packets++;
1931
David Brownell789851c2006-08-21 15:26:38 -07001932 spin_lock(&dev->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 list_add (&req->list, &dev->tx_reqs);
David Brownell789851c2006-08-21 15:26:38 -07001934 spin_unlock(&dev->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 dev_kfree_skb_any (skb);
1936
1937 atomic_dec (&dev->tx_qlen);
1938 if (netif_carrier_ok (dev->net))
1939 netif_wake_queue (dev->net);
1940}
1941
1942static inline int eth_is_promisc (struct eth_dev *dev)
1943{
1944 /* no filters for the CDC subset; always promisc */
1945 if (subset_active (dev))
1946 return 1;
1947 return dev->cdc_filter & USB_CDC_PACKET_TYPE_PROMISCUOUS;
1948}
1949
1950static int eth_start_xmit (struct sk_buff *skb, struct net_device *net)
1951{
1952 struct eth_dev *dev = netdev_priv(net);
1953 int length = skb->len;
1954 int retval;
1955 struct usb_request *req = NULL;
1956 unsigned long flags;
1957
1958 /* apply outgoing CDC or RNDIS filters */
1959 if (!eth_is_promisc (dev)) {
1960 u8 *dest = skb->data;
1961
David Brownelld8126a02006-11-12 18:09:44 -08001962 if (is_multicast_ether_addr(dest)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 u16 type;
1964
1965 /* ignores USB_CDC_PACKET_TYPE_MULTICAST and host
1966 * SET_ETHERNET_MULTICAST_FILTERS requests
1967 */
David Brownelld8126a02006-11-12 18:09:44 -08001968 if (is_broadcast_ether_addr(dest))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 type = USB_CDC_PACKET_TYPE_BROADCAST;
1970 else
1971 type = USB_CDC_PACKET_TYPE_ALL_MULTICAST;
1972 if (!(dev->cdc_filter & type)) {
1973 dev_kfree_skb_any (skb);
1974 return 0;
1975 }
1976 }
1977 /* ignores USB_CDC_PACKET_TYPE_DIRECTED */
1978 }
1979
David Brownell789851c2006-08-21 15:26:38 -07001980 spin_lock_irqsave(&dev->req_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 req = container_of (dev->tx_reqs.next, struct usb_request, list);
1982 list_del (&req->list);
1983 if (list_empty (&dev->tx_reqs))
1984 netif_stop_queue (net);
David Brownell789851c2006-08-21 15:26:38 -07001985 spin_unlock_irqrestore(&dev->req_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986
1987 /* no buffer copies needed, unless the network stack did it
1988 * or the hardware can't use skb buffers.
1989 * or there's not enough space for any RNDIS headers we need
1990 */
David Brownell45e45ab2005-05-16 08:26:38 -07001991 if (rndis_active(dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 struct sk_buff *skb_rndis;
1993
1994 skb_rndis = skb_realloc_headroom (skb,
1995 sizeof (struct rndis_packet_msg_type));
1996 if (!skb_rndis)
1997 goto drop;
David Brownell7e27f182006-06-13 09:54:40 -07001998
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 dev_kfree_skb_any (skb);
2000 skb = skb_rndis;
2001 rndis_add_hdr (skb);
2002 length = skb->len;
2003 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 req->buf = skb->data;
2005 req->context = skb;
2006 req->complete = tx_complete;
2007
2008 /* use zlp framing on tx for strict CDC-Ether conformance,
2009 * though any robust network rx path ignores extra padding.
2010 * and some hardware doesn't like to write zlps.
2011 */
2012 req->zero = 1;
2013 if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0)
2014 length++;
2015
2016 req->length = length;
2017
2018#ifdef CONFIG_USB_GADGET_DUALSPEED
2019 /* throttle highspeed IRQ rate back slightly */
2020 req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH)
2021 ? ((atomic_read (&dev->tx_qlen) % TX_DELAY) != 0)
2022 : 0;
2023#endif
2024
2025 retval = usb_ep_queue (dev->in_ep, req, GFP_ATOMIC);
2026 switch (retval) {
2027 default:
2028 DEBUG (dev, "tx queue err %d\n", retval);
2029 break;
2030 case 0:
2031 net->trans_start = jiffies;
2032 atomic_inc (&dev->tx_qlen);
2033 }
2034
2035 if (retval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036drop:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 dev->stats.tx_dropped++;
2038 dev_kfree_skb_any (skb);
David Brownell789851c2006-08-21 15:26:38 -07002039 spin_lock_irqsave(&dev->req_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 if (list_empty (&dev->tx_reqs))
2041 netif_start_queue (net);
2042 list_add (&req->list, &dev->tx_reqs);
David Brownell789851c2006-08-21 15:26:38 -07002043 spin_unlock_irqrestore(&dev->req_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 }
2045 return 0;
2046}
2047
2048/*-------------------------------------------------------------------------*/
2049
2050#ifdef CONFIG_USB_ETH_RNDIS
2051
David Brownell907cba32005-04-28 13:48:09 -07002052/* The interrupt endpoint is used in RNDIS to notify the host when messages
2053 * other than data packets are available ... notably the REMOTE_NDIS_*_CMPLT
2054 * messages, but also REMOTE_NDIS_INDICATE_STATUS_MSG and potentially even
2055 * REMOTE_NDIS_KEEPALIVE_MSG.
2056 *
2057 * The RNDIS control queue is processed by GET_ENCAPSULATED_RESPONSE, and
2058 * normally just one notification will be queued.
2059 */
2060
Al Viro55016f12005-10-21 03:21:58 -04002061static struct usb_request *eth_req_alloc (struct usb_ep *, unsigned, gfp_t);
David Brownell907cba32005-04-28 13:48:09 -07002062static void eth_req_free (struct usb_ep *ep, struct usb_request *req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063
2064static void
2065rndis_control_ack_complete (struct usb_ep *ep, struct usb_request *req)
2066{
David Brownell907cba32005-04-28 13:48:09 -07002067 struct eth_dev *dev = ep->driver_data;
2068
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 if (req->status || req->actual != req->length)
David Brownell907cba32005-04-28 13:48:09 -07002070 DEBUG (dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 "rndis control ack complete --> %d, %d/%d\n",
2072 req->status, req->actual, req->length);
David Brownell907cba32005-04-28 13:48:09 -07002073 req->context = NULL;
2074
2075 if (req != dev->stat_req)
2076 eth_req_free(ep, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077}
2078
2079static int rndis_control_ack (struct net_device *net)
2080{
2081 struct eth_dev *dev = netdev_priv(net);
Eric Sesterhenn55359022006-08-21 15:31:05 -07002082 int length;
David Brownell6cdee102005-04-18 17:39:34 -07002083 struct usb_request *resp = dev->stat_req;
David Brownell7e27f182006-06-13 09:54:40 -07002084
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 /* in case RNDIS calls this after disconnect */
David Brownell6cdee102005-04-18 17:39:34 -07002086 if (!dev->status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 DEBUG (dev, "status ENODEV\n");
2088 return -ENODEV;
2089 }
2090
David Brownell907cba32005-04-28 13:48:09 -07002091 /* in case queue length > 1 */
2092 if (resp->context) {
2093 resp = eth_req_alloc (dev->status_ep, 8, GFP_ATOMIC);
2094 if (!resp)
2095 return -ENOMEM;
2096 }
2097
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 /* Send RNDIS RESPONSE_AVAILABLE notification;
2099 * USB_CDC_NOTIFY_RESPONSE_AVAILABLE should work too
2100 */
2101 resp->length = 8;
2102 resp->complete = rndis_control_ack_complete;
David Brownell907cba32005-04-28 13:48:09 -07002103 resp->context = dev;
David Brownell7e27f182006-06-13 09:54:40 -07002104
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 *((__le32 *) resp->buf) = __constant_cpu_to_le32 (1);
2106 *((__le32 *) resp->buf + 1) = __constant_cpu_to_le32 (0);
David Brownell7e27f182006-06-13 09:54:40 -07002107
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 length = usb_ep_queue (dev->status_ep, resp, GFP_ATOMIC);
2109 if (length < 0) {
2110 resp->status = 0;
2111 rndis_control_ack_complete (dev->status_ep, resp);
2112 }
David Brownell7e27f182006-06-13 09:54:40 -07002113
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 return 0;
2115}
2116
David Brownell45e45ab2005-05-16 08:26:38 -07002117#else
2118
2119#define rndis_control_ack NULL
2120
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121#endif /* RNDIS */
2122
Al Viro55016f12005-10-21 03:21:58 -04002123static void eth_start (struct eth_dev *dev, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124{
2125 DEBUG (dev, "%s\n", __FUNCTION__);
2126
2127 /* fill the rx queue */
2128 rx_fill (dev, gfp_flags);
2129
David Brownell7e27f182006-06-13 09:54:40 -07002130 /* and open the tx floodgates */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 atomic_set (&dev->tx_qlen, 0);
2132 netif_wake_queue (dev->net);
David Brownell45e45ab2005-05-16 08:26:38 -07002133 if (rndis_active(dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 rndis_set_param_medium (dev->rndis_config,
2135 NDIS_MEDIUM_802_3,
David Brownell6cdee102005-04-18 17:39:34 -07002136 BITRATE(dev->gadget)/100);
David Brownell907cba32005-04-28 13:48:09 -07002137 (void) rndis_signal_connect (dev->rndis_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139}
2140
2141static int eth_open (struct net_device *net)
2142{
2143 struct eth_dev *dev = netdev_priv(net);
2144
2145 DEBUG (dev, "%s\n", __FUNCTION__);
2146 if (netif_carrier_ok (dev->net))
2147 eth_start (dev, GFP_KERNEL);
2148 return 0;
2149}
2150
2151static int eth_stop (struct net_device *net)
2152{
2153 struct eth_dev *dev = netdev_priv(net);
2154
2155 VDEBUG (dev, "%s\n", __FUNCTION__);
2156 netif_stop_queue (net);
2157
2158 DEBUG (dev, "stop stats: rx/tx %ld/%ld, errs %ld/%ld\n",
David Brownell7e27f182006-06-13 09:54:40 -07002159 dev->stats.rx_packets, dev->stats.tx_packets,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 dev->stats.rx_errors, dev->stats.tx_errors
2161 );
2162
2163 /* ensure there are no more active requests */
2164 if (dev->config) {
2165 usb_ep_disable (dev->in_ep);
2166 usb_ep_disable (dev->out_ep);
2167 if (netif_carrier_ok (dev->net)) {
2168 DEBUG (dev, "host still using in/out endpoints\n");
2169 // FIXME idiom may leave toggle wrong here
2170 usb_ep_enable (dev->in_ep, dev->in);
2171 usb_ep_enable (dev->out_ep, dev->out);
2172 }
2173 if (dev->status_ep) {
2174 usb_ep_disable (dev->status_ep);
2175 usb_ep_enable (dev->status_ep, dev->status);
2176 }
2177 }
David Brownell7e27f182006-06-13 09:54:40 -07002178
David Brownell45e45ab2005-05-16 08:26:38 -07002179 if (rndis_active(dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 rndis_set_param_medium (dev->rndis_config,
2181 NDIS_MEDIUM_802_3, 0);
David Brownell907cba32005-04-28 13:48:09 -07002182 (void) rndis_signal_disconnect (dev->rndis_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184
2185 return 0;
2186}
2187
2188/*-------------------------------------------------------------------------*/
2189
David Brownell907cba32005-04-28 13:48:09 -07002190static struct usb_request *
Al Viro55016f12005-10-21 03:21:58 -04002191eth_req_alloc (struct usb_ep *ep, unsigned size, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192{
2193 struct usb_request *req;
2194
David Brownell907cba32005-04-28 13:48:09 -07002195 req = usb_ep_alloc_request (ep, gfp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 if (!req)
2197 return NULL;
2198
David Brownell907cba32005-04-28 13:48:09 -07002199 req->buf = kmalloc (size, gfp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 if (!req->buf) {
2201 usb_ep_free_request (ep, req);
2202 req = NULL;
2203 }
2204 return req;
2205}
2206
2207static void
2208eth_req_free (struct usb_ep *ep, struct usb_request *req)
2209{
2210 kfree (req->buf);
2211 usb_ep_free_request (ep, req);
2212}
2213
2214
David Brownella3536782006-07-06 15:48:53 -07002215static void /* __init_or_exit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216eth_unbind (struct usb_gadget *gadget)
2217{
2218 struct eth_dev *dev = get_gadget_data (gadget);
2219
2220 DEBUG (dev, "unbind\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 rndis_deregister (dev->rndis_config);
2222 rndis_exit ();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223
2224 /* we've already been disconnected ... no i/o is active */
2225 if (dev->req) {
2226 eth_req_free (gadget->ep0, dev->req);
2227 dev->req = NULL;
2228 }
2229 if (dev->stat_req) {
2230 eth_req_free (dev->status_ep, dev->stat_req);
2231 dev->stat_req = NULL;
2232 }
2233
2234 unregister_netdev (dev->net);
2235 free_netdev(dev->net);
2236
2237 /* assuming we used keventd, it must quiesce too */
2238 flush_scheduled_work ();
2239 set_gadget_data (gadget, NULL);
2240}
2241
David Brownella3536782006-07-06 15:48:53 -07002242static u8 __devinit nibble (unsigned char c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243{
2244 if (likely (isdigit (c)))
2245 return c - '0';
2246 c = toupper (c);
2247 if (likely (isxdigit (c)))
2248 return 10 + c - 'A';
2249 return 0;
2250}
2251
David Brownella3536782006-07-06 15:48:53 -07002252static int __devinit get_ether_addr(const char *str, u8 *dev_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253{
2254 if (str) {
2255 unsigned i;
2256
2257 for (i = 0; i < 6; i++) {
2258 unsigned char num;
2259
2260 if((*str == '.') || (*str == ':'))
2261 str++;
2262 num = nibble(*str++) << 4;
2263 num |= (nibble(*str++));
2264 dev_addr [i] = num;
2265 }
2266 if (is_valid_ether_addr (dev_addr))
Aras Vaichas1afc64a2006-02-18 12:31:23 -08002267 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 }
2269 random_ether_addr(dev_addr);
Aras Vaichas1afc64a2006-02-18 12:31:23 -08002270 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271}
2272
David Brownella3536782006-07-06 15:48:53 -07002273static int __devinit
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274eth_bind (struct usb_gadget *gadget)
2275{
2276 struct eth_dev *dev;
2277 struct net_device *net;
2278 u8 cdc = 1, zlp = 1, rndis = 1;
2279 struct usb_ep *in_ep, *out_ep, *status_ep = NULL;
2280 int status = -ENOMEM;
David Brownell91e79c92005-07-13 15:18:30 -07002281 int gcnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282
2283 /* these flags are only ever cleared; compiler take note */
2284#ifndef DEV_CONFIG_CDC
2285 cdc = 0;
2286#endif
2287#ifndef CONFIG_USB_ETH_RNDIS
2288 rndis = 0;
2289#endif
2290
2291 /* Because most host side USB stacks handle CDC Ethernet, that
2292 * standard protocol is _strongly_ preferred for interop purposes.
2293 * (By everyone except Microsoft.)
2294 */
David Brownell91e79c92005-07-13 15:18:30 -07002295 if (gadget_is_pxa (gadget)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 /* pxa doesn't support altsettings */
2297 cdc = 0;
David Brownell729ed6d2006-08-30 13:24:56 -07002298 } else if (gadget_is_musbhdrc(gadget)) {
2299 /* reduce tx dma overhead by avoiding special cases */
2300 zlp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 } else if (gadget_is_sh(gadget)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 /* sh doesn't support multiple interfaces or configs */
2303 cdc = 0;
2304 rndis = 0;
2305 } else if (gadget_is_sa1100 (gadget)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 /* hardware can't write zlps */
2307 zlp = 0;
2308 /* sa1100 CAN do CDC, without status endpoint ... we use
2309 * non-CDC to be compatible with ARM Linux-2.4 "usb-eth".
2310 */
2311 cdc = 0;
David Brownell91e79c92005-07-13 15:18:30 -07002312 }
2313
2314 gcnum = usb_gadget_controller_number (gadget);
2315 if (gcnum >= 0)
2316 device_desc.bcdDevice = cpu_to_le16 (0x0200 + gcnum);
2317 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 /* can't assume CDC works. don't want to default to
2319 * anything less functional on CDC-capable hardware,
2320 * so we fail in this case.
2321 */
2322 dev_err (&gadget->dev,
2323 "controller '%s' not recognized\n",
2324 gadget->name);
2325 return -ENODEV;
2326 }
2327 snprintf (manufacturer, sizeof manufacturer, "%s %s/%s",
Serge E. Hallyn96b644b2006-10-02 02:18:13 -07002328 init_utsname()->sysname, init_utsname()->release,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 gadget->name);
2330
2331 /* If there's an RNDIS configuration, that's what Windows wants to
2332 * be using ... so use these product IDs here and in the "linux.inf"
2333 * needed to install MSFT drivers. Current Linux kernels will use
2334 * the second configuration if it's CDC Ethernet, and need some help
2335 * to choose the right configuration otherwise.
2336 */
2337 if (rndis) {
2338 device_desc.idVendor =
2339 __constant_cpu_to_le16(RNDIS_VENDOR_NUM);
2340 device_desc.idProduct =
2341 __constant_cpu_to_le16(RNDIS_PRODUCT_NUM);
2342 snprintf (product_desc, sizeof product_desc,
2343 "RNDIS/%s", driver_desc);
2344
2345 /* CDC subset ... recognized by Linux since 2.4.10, but Windows
David Brownell11d54892006-12-11 15:59:04 -08002346 * drivers aren't widely available. (That may be improved by
2347 * supporting one submode of the "SAFE" variant of MDLM.)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 */
2349 } else if (!cdc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 device_desc.idVendor =
2351 __constant_cpu_to_le16(SIMPLE_VENDOR_NUM);
2352 device_desc.idProduct =
2353 __constant_cpu_to_le16(SIMPLE_PRODUCT_NUM);
2354 }
2355
2356 /* support optional vendor/distro customization */
2357 if (idVendor) {
2358 if (!idProduct) {
2359 dev_err (&gadget->dev, "idVendor needs idProduct!\n");
2360 return -ENODEV;
2361 }
2362 device_desc.idVendor = cpu_to_le16(idVendor);
2363 device_desc.idProduct = cpu_to_le16(idProduct);
2364 if (bcdDevice)
2365 device_desc.bcdDevice = cpu_to_le16(bcdDevice);
2366 }
2367 if (iManufacturer)
2368 strlcpy (manufacturer, iManufacturer, sizeof manufacturer);
2369 if (iProduct)
2370 strlcpy (product_desc, iProduct, sizeof product_desc);
Aras Vaichas1afc64a2006-02-18 12:31:23 -08002371 if (iSerialNumber) {
2372 device_desc.iSerialNumber = STRING_SERIALNUMBER,
2373 strlcpy(serial_number, iSerialNumber, sizeof serial_number);
2374 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375
2376 /* all we really need is bulk IN/OUT */
2377 usb_ep_autoconfig_reset (gadget);
2378 in_ep = usb_ep_autoconfig (gadget, &fs_source_desc);
2379 if (!in_ep) {
2380autoconf_fail:
2381 dev_err (&gadget->dev,
2382 "can't autoconfigure on %s\n",
2383 gadget->name);
2384 return -ENODEV;
2385 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 in_ep->driver_data = in_ep; /* claim */
David Brownell7e27f182006-06-13 09:54:40 -07002387
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 out_ep = usb_ep_autoconfig (gadget, &fs_sink_desc);
2389 if (!out_ep)
2390 goto autoconf_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 out_ep->driver_data = out_ep; /* claim */
2392
2393#if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
2394 /* CDC Ethernet control interface doesn't require a status endpoint.
2395 * Since some hosts expect one, try to allocate one anyway.
2396 */
2397 if (cdc || rndis) {
2398 status_ep = usb_ep_autoconfig (gadget, &fs_status_desc);
2399 if (status_ep) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 status_ep->driver_data = status_ep; /* claim */
2401 } else if (rndis) {
2402 dev_err (&gadget->dev,
2403 "can't run RNDIS on %s\n",
2404 gadget->name);
2405 return -ENODEV;
2406#ifdef DEV_CONFIG_CDC
2407 /* pxa25x only does CDC subset; often used with RNDIS */
2408 } else if (cdc) {
2409 control_intf.bNumEndpoints = 0;
2410 /* FIXME remove endpoint from descriptor list */
2411#endif
2412 }
2413 }
2414#endif
2415
2416 /* one config: cdc, else minimal subset */
2417 if (!cdc) {
2418 eth_config.bNumInterfaces = 1;
2419 eth_config.iConfiguration = STRING_SUBSET;
David Brownell11d54892006-12-11 15:59:04 -08002420
2421 /* use functions to set these up, in case we're built to work
2422 * with multiple controllers and must override CDC Ethernet.
2423 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 fs_subset_descriptors();
2425 hs_subset_descriptors();
2426 }
2427
David Brownelle1394b42006-04-02 10:20:43 -08002428 device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
2429 usb_gadget_set_selfpowered (gadget);
2430
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 /* For now RNDIS is always a second config */
2432 if (rndis)
2433 device_desc.bNumConfigurations = 2;
2434
2435#ifdef CONFIG_USB_GADGET_DUALSPEED
2436 if (rndis)
2437 dev_qualifier.bNumConfigurations = 2;
2438 else if (!cdc)
2439 dev_qualifier.bDeviceClass = USB_CLASS_VENDOR_SPEC;
2440
2441 /* assumes ep0 uses the same value for both speeds ... */
2442 dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0;
2443
2444 /* and that all endpoints are dual-speed */
2445 hs_source_desc.bEndpointAddress = fs_source_desc.bEndpointAddress;
2446 hs_sink_desc.bEndpointAddress = fs_sink_desc.bEndpointAddress;
2447#if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
David Brownell907cba32005-04-28 13:48:09 -07002448 if (status_ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 hs_status_desc.bEndpointAddress =
2450 fs_status_desc.bEndpointAddress;
2451#endif
2452#endif /* DUALSPEED */
2453
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 if (gadget->is_otg) {
2455 otg_descriptor.bmAttributes |= USB_OTG_HNP,
2456 eth_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
2457 eth_config.bMaxPower = 4;
2458#ifdef CONFIG_USB_ETH_RNDIS
2459 rndis_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
2460 rndis_config.bMaxPower = 4;
2461#endif
2462 }
2463
David Brownell7e27f182006-06-13 09:54:40 -07002464 net = alloc_etherdev (sizeof *dev);
2465 if (!net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 return status;
2467 dev = netdev_priv(net);
2468 spin_lock_init (&dev->lock);
David Brownell789851c2006-08-21 15:26:38 -07002469 spin_lock_init (&dev->req_lock);
David Howellsc4028952006-11-22 14:57:56 +00002470 INIT_WORK (&dev->work, eth_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 INIT_LIST_HEAD (&dev->tx_reqs);
2472 INIT_LIST_HEAD (&dev->rx_reqs);
2473
2474 /* network device setup */
2475 dev->net = net;
2476 SET_MODULE_OWNER (net);
2477 strcpy (net->name, "usb%d");
2478 dev->cdc = cdc;
2479 dev->zlp = zlp;
2480
2481 dev->in_ep = in_ep;
2482 dev->out_ep = out_ep;
2483 dev->status_ep = status_ep;
2484
2485 /* Module params for these addresses should come from ID proms.
2486 * The host side address is used with CDC and RNDIS, and commonly
David Brownell11d54892006-12-11 15:59:04 -08002487 * ends up in a persistent config database. It's not clear if
2488 * host side code for the SAFE thing cares -- its original BLAN
2489 * thing didn't, Sharp never assigned those addresses on Zaurii.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 */
Aras Vaichas1afc64a2006-02-18 12:31:23 -08002491 if (get_ether_addr(dev_addr, net->dev_addr))
2492 dev_warn(&gadget->dev,
2493 "using random %s ethernet address\n", "self");
David Brownell11d54892006-12-11 15:59:04 -08002494 if (get_ether_addr(host_addr, dev->host_mac))
2495 dev_warn(&gadget->dev,
2496 "using random %s ethernet address\n", "host");
2497 snprintf (ethaddr, sizeof ethaddr, "%02X%02X%02X%02X%02X%02X",
2498 dev->host_mac [0], dev->host_mac [1],
2499 dev->host_mac [2], dev->host_mac [3],
2500 dev->host_mac [4], dev->host_mac [5]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501
2502 if (rndis) {
2503 status = rndis_init();
2504 if (status < 0) {
2505 dev_err (&gadget->dev, "can't init RNDIS, %d\n",
2506 status);
2507 goto fail;
2508 }
2509 }
2510
2511 net->change_mtu = eth_change_mtu;
2512 net->get_stats = eth_get_stats;
2513 net->hard_start_xmit = eth_start_xmit;
2514 net->open = eth_open;
2515 net->stop = eth_stop;
2516 // watchdog_timeo, tx_timeout ...
2517 // set_multicast_list
2518 SET_ETHTOOL_OPS(net, &ops);
2519
2520 /* preallocate control message data and buffer */
David Brownell907cba32005-04-28 13:48:09 -07002521 dev->req = eth_req_alloc (gadget->ep0, USB_BUFSIZ, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 if (!dev->req)
2523 goto fail;
2524 dev->req->complete = eth_setup_complete;
2525
2526 /* ... and maybe likewise for status transfer */
Ian Campbell05f33402005-06-29 10:15:32 +01002527#if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 if (dev->status_ep) {
2529 dev->stat_req = eth_req_alloc (dev->status_ep,
David Brownell907cba32005-04-28 13:48:09 -07002530 STATUS_BYTECOUNT, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 if (!dev->stat_req) {
2532 eth_req_free (gadget->ep0, dev->req);
2533 goto fail;
2534 }
David Brownell907cba32005-04-28 13:48:09 -07002535 dev->stat_req->context = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 }
David Brownell822e14a2005-06-13 06:55:03 -07002537#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538
2539 /* finish hookup to lower layer ... */
2540 dev->gadget = gadget;
2541 set_gadget_data (gadget, dev);
2542 gadget->ep0->driver_data = dev;
David Brownell7e27f182006-06-13 09:54:40 -07002543
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 /* two kinds of host-initiated state changes:
2545 * - iff DATA transfer is active, carrier is "on"
2546 * - tx queueing enabled if open *and* carrier is "on"
2547 */
2548 netif_stop_queue (dev->net);
2549 netif_carrier_off (dev->net);
2550
David Brownell7e27f182006-06-13 09:54:40 -07002551 SET_NETDEV_DEV (dev->net, &gadget->dev);
2552 status = register_netdev (dev->net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 if (status < 0)
2554 goto fail1;
2555
2556 INFO (dev, "%s, version: " DRIVER_VERSION "\n", driver_desc);
2557 INFO (dev, "using %s, OUT %s IN %s%s%s\n", gadget->name,
David Brownell907cba32005-04-28 13:48:09 -07002558 out_ep->name, in_ep->name,
2559 status_ep ? " STATUS " : "",
2560 status_ep ? status_ep->name : ""
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 );
2562 INFO (dev, "MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
2563 net->dev_addr [0], net->dev_addr [1],
2564 net->dev_addr [2], net->dev_addr [3],
2565 net->dev_addr [4], net->dev_addr [5]);
2566
2567 if (cdc || rndis)
2568 INFO (dev, "HOST MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
2569 dev->host_mac [0], dev->host_mac [1],
2570 dev->host_mac [2], dev->host_mac [3],
2571 dev->host_mac [4], dev->host_mac [5]);
2572
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 if (rndis) {
2574 u32 vendorID = 0;
2575
2576 /* FIXME RNDIS vendor id == "vendor NIC code" == ? */
David Brownell7e27f182006-06-13 09:54:40 -07002577
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 dev->rndis_config = rndis_register (rndis_control_ack);
2579 if (dev->rndis_config < 0) {
2580fail0:
2581 unregister_netdev (dev->net);
2582 status = -ENODEV;
2583 goto fail;
2584 }
David Brownell7e27f182006-06-13 09:54:40 -07002585
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 /* these set up a lot of the OIDs that RNDIS needs */
2587 rndis_set_host_mac (dev->rndis_config, dev->host_mac);
2588 if (rndis_set_param_dev (dev->rndis_config, dev->net,
David Brownell340600a2005-04-28 13:45:25 -07002589 &dev->stats, &dev->cdc_filter))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 goto fail0;
2591 if (rndis_set_param_vendor (dev->rndis_config, vendorID,
2592 manufacturer))
2593 goto fail0;
2594 if (rndis_set_param_medium (dev->rndis_config,
2595 NDIS_MEDIUM_802_3,
2596 0))
2597 goto fail0;
2598 INFO (dev, "RNDIS ready\n");
2599 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600
2601 return status;
2602
2603fail1:
2604 dev_dbg(&gadget->dev, "register_netdev failed, %d\n", status);
2605fail:
2606 eth_unbind (gadget);
2607 return status;
2608}
2609
2610/*-------------------------------------------------------------------------*/
2611
2612static void
2613eth_suspend (struct usb_gadget *gadget)
2614{
2615 struct eth_dev *dev = get_gadget_data (gadget);
2616
2617 DEBUG (dev, "suspend\n");
2618 dev->suspended = 1;
2619}
2620
2621static void
2622eth_resume (struct usb_gadget *gadget)
2623{
2624 struct eth_dev *dev = get_gadget_data (gadget);
2625
2626 DEBUG (dev, "resume\n");
2627 dev->suspended = 0;
2628}
2629
2630/*-------------------------------------------------------------------------*/
2631
2632static struct usb_gadget_driver eth_driver = {
David Brownell907cba32005-04-28 13:48:09 -07002633 .speed = DEVSPEED,
2634
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635 .function = (char *) driver_desc,
2636 .bind = eth_bind,
Tony Lindgrenbfb2c962006-06-29 22:27:36 -07002637 .unbind = eth_unbind,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638
2639 .setup = eth_setup,
2640 .disconnect = eth_disconnect,
2641
2642 .suspend = eth_suspend,
2643 .resume = eth_resume,
2644
David Brownell7e27f182006-06-13 09:54:40 -07002645 .driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 .name = (char *) shortname,
Ben Dooksd0d50492005-10-10 10:52:33 +01002647 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 },
2649};
2650
2651MODULE_DESCRIPTION (DRIVER_DESC);
2652MODULE_AUTHOR ("David Brownell, Benedikt Spanger");
2653MODULE_LICENSE ("GPL");
2654
2655
2656static int __init init (void)
2657{
2658 return usb_gadget_register_driver (&eth_driver);
2659}
2660module_init (init);
2661
2662static void __exit cleanup (void)
2663{
2664 usb_gadget_unregister_driver (&eth_driver);
2665}
2666module_exit (cleanup);
2667