blob: 1dd8b57f4420cc7e13db3062c94d5803b60f9083 [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
Li Yangd2eef1f2007-04-23 10:37:36 -0700285#ifdef CONFIG_USB_GADGET_FSL_USB2
286#define DEV_CONFIG_CDC
287#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
289/* For CDC-incapable hardware, choose the simple cdc subset.
290 * Anything that talks bulk (without notable bugs) can do this.
291 */
292#ifdef CONFIG_USB_GADGET_PXA2XX
293#define DEV_CONFIG_SUBSET
294#endif
295
296#ifdef CONFIG_USB_GADGET_SH
297#define DEV_CONFIG_SUBSET
298#endif
299
300#ifdef CONFIG_USB_GADGET_SA1100
301/* use non-CDC for backwards compatibility */
302#define DEV_CONFIG_SUBSET
303#endif
304
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
306/*-------------------------------------------------------------------------*/
307
308/* "main" config is either CDC, or its simple subset */
309static inline int is_cdc(struct eth_dev *dev)
310{
311#if !defined(DEV_CONFIG_SUBSET)
312 return 1; /* only cdc possible */
313#elif !defined (DEV_CONFIG_CDC)
314 return 0; /* only subset possible */
315#else
316 return dev->cdc; /* depends on what hardware we found */
317#endif
318}
319
320/* "secondary" RNDIS config may sometimes be activated */
321static inline int rndis_active(struct eth_dev *dev)
322{
323#ifdef CONFIG_USB_ETH_RNDIS
324 return dev->rndis;
325#else
326 return 0;
327#endif
328}
329
330#define subset_active(dev) (!is_cdc(dev) && !rndis_active(dev))
331#define cdc_active(dev) ( is_cdc(dev) && !rndis_active(dev))
332
333
334
335#define DEFAULT_QLEN 2 /* double buffering by default */
336
337/* peak bulk transfer bits-per-second */
David Brownell7e27f182006-06-13 09:54:40 -0700338#define HS_BPS (13 * 512 * 8 * 1000 * 8)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339#define FS_BPS (19 * 64 * 1 * 1000 * 8)
340
341#ifdef CONFIG_USB_GADGET_DUALSPEED
David Brownell907cba32005-04-28 13:48:09 -0700342#define DEVSPEED USB_SPEED_HIGH
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
344static unsigned qmult = 5;
345module_param (qmult, uint, S_IRUGO|S_IWUSR);
346
347
348/* for dual-speed hardware, use deeper queues at highspeed */
349#define qlen(gadget) \
350 (DEFAULT_QLEN*((gadget->speed == USB_SPEED_HIGH) ? qmult : 1))
351
352/* also defer IRQs on highspeed TX */
353#define TX_DELAY qmult
354
David Brownell6cdee102005-04-18 17:39:34 -0700355static inline int BITRATE(struct usb_gadget *g)
356{
357 return (g->speed == USB_SPEED_HIGH) ? HS_BPS : FS_BPS;
358}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
360#else /* full speed (low speed doesn't do bulk) */
David Brownell907cba32005-04-28 13:48:09 -0700361#define DEVSPEED USB_SPEED_FULL
362
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363#define qlen(gadget) DEFAULT_QLEN
364
David Brownell6cdee102005-04-18 17:39:34 -0700365static inline int BITRATE(struct usb_gadget *g)
366{
367 return FS_BPS;
368}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369#endif
370
371
372/*-------------------------------------------------------------------------*/
373
374#define xprintk(d,level,fmt,args...) \
375 printk(level "%s: " fmt , (d)->net->name , ## args)
376
377#ifdef DEBUG
378#undef DEBUG
379#define DEBUG(dev,fmt,args...) \
380 xprintk(dev , KERN_DEBUG , fmt , ## args)
381#else
382#define DEBUG(dev,fmt,args...) \
383 do { } while (0)
384#endif /* DEBUG */
385
386#ifdef VERBOSE
387#define VDEBUG DEBUG
388#else
389#define VDEBUG(dev,fmt,args...) \
390 do { } while (0)
391#endif /* DEBUG */
392
393#define ERROR(dev,fmt,args...) \
394 xprintk(dev , KERN_ERR , fmt , ## args)
395#define WARN(dev,fmt,args...) \
396 xprintk(dev , KERN_WARNING , fmt , ## args)
397#define INFO(dev,fmt,args...) \
398 xprintk(dev , KERN_INFO , fmt , ## args)
399
400/*-------------------------------------------------------------------------*/
401
402/* USB DRIVER HOOKUP (to the hardware driver, below us), mostly
403 * ep0 implementation: descriptors, config management, setup().
404 * also optional class-specific notification interrupt transfer.
405 */
406
407/*
408 * DESCRIPTORS ... most are static, but strings and (full) configuration
409 * descriptors are built on demand. For now we do either full CDC, or
410 * our simple subset, with RNDIS as an optional second configuration.
411 *
412 * RNDIS includes some CDC ACM descriptors ... like CDC Ethernet. But
413 * the class descriptors match a modem (they're ignored; it's really just
414 * Ethernet functionality), they don't need the NOP altsetting, and the
415 * status transfer endpoint isn't optional.
416 */
417
418#define STRING_MANUFACTURER 1
419#define STRING_PRODUCT 2
420#define STRING_ETHADDR 3
421#define STRING_DATA 4
422#define STRING_CONTROL 5
423#define STRING_RNDIS_CONTROL 6
424#define STRING_CDC 7
425#define STRING_SUBSET 8
426#define STRING_RNDIS 9
Aras Vaichas1afc64a2006-02-18 12:31:23 -0800427#define STRING_SERIALNUMBER 10
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
David Brownell340600a2005-04-28 13:45:25 -0700429/* holds our biggest descriptor (or RNDIS response) */
430#define USB_BUFSIZ 256
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
432/*
433 * This device advertises one configuration, eth_config, unless RNDIS
434 * is enabled (rndis_config) on hardware supporting at least two configs.
435 *
436 * NOTE: Controllers like superh_udc should probably be able to use
437 * an RNDIS-only configuration.
438 *
439 * FIXME define some higher-powered configurations to make it easier
440 * to recharge batteries ...
441 */
442
443#define DEV_CONFIG_VALUE 1 /* cdc or subset */
444#define DEV_RNDIS_CONFIG_VALUE 2 /* rndis; optional */
445
446static struct usb_device_descriptor
447device_desc = {
448 .bLength = sizeof device_desc,
449 .bDescriptorType = USB_DT_DEVICE,
450
451 .bcdUSB = __constant_cpu_to_le16 (0x0200),
452
453 .bDeviceClass = USB_CLASS_COMM,
454 .bDeviceSubClass = 0,
455 .bDeviceProtocol = 0,
456
457 .idVendor = __constant_cpu_to_le16 (CDC_VENDOR_NUM),
458 .idProduct = __constant_cpu_to_le16 (CDC_PRODUCT_NUM),
459 .iManufacturer = STRING_MANUFACTURER,
460 .iProduct = STRING_PRODUCT,
461 .bNumConfigurations = 1,
462};
463
464static struct usb_otg_descriptor
465otg_descriptor = {
466 .bLength = sizeof otg_descriptor,
467 .bDescriptorType = USB_DT_OTG,
468
469 .bmAttributes = USB_OTG_SRP,
470};
471
472static struct usb_config_descriptor
473eth_config = {
474 .bLength = sizeof eth_config,
475 .bDescriptorType = USB_DT_CONFIG,
476
477 /* compute wTotalLength on the fly */
478 .bNumInterfaces = 2,
479 .bConfigurationValue = DEV_CONFIG_VALUE,
480 .iConfiguration = STRING_CDC,
481 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
482 .bMaxPower = 50,
483};
484
485#ifdef CONFIG_USB_ETH_RNDIS
David Brownell7e27f182006-06-13 09:54:40 -0700486static struct usb_config_descriptor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487rndis_config = {
488 .bLength = sizeof rndis_config,
489 .bDescriptorType = USB_DT_CONFIG,
490
491 /* compute wTotalLength on the fly */
492 .bNumInterfaces = 2,
493 .bConfigurationValue = DEV_RNDIS_CONFIG_VALUE,
494 .iConfiguration = STRING_RNDIS,
495 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
496 .bMaxPower = 50,
497};
498#endif
499
500/*
501 * Compared to the simple CDC subset, the full CDC Ethernet model adds
502 * three class descriptors, two interface descriptors, optional status
503 * endpoint. Both have a "data" interface and two bulk endpoints.
504 * There are also differences in how control requests are handled.
505 *
David Brownell11d54892006-12-11 15:59:04 -0800506 * RNDIS shares a lot with CDC-Ethernet, since it's a variant of the
507 * CDC-ACM (modem) spec. Unfortunately MSFT's RNDIS driver is buggy; it
508 * may hang or oops. Since bugfixes (or accurate specs, letting Linux
509 * work around those bugs) are unlikely to ever come from MSFT, you may
510 * wish to avoid using RNDIS.
511 *
512 * MCCI offers an alternative to RNDIS if you need to connect to Windows
513 * but have hardware that can't support CDC Ethernet. We add descriptors
514 * to present the CDC Subset as a (nonconformant) CDC MDLM variant called
515 * "SAFE". That borrows from both CDC Ethernet and CDC MDLM. You can
516 * get those drivers from MCCI, or bundled with various products.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 */
518
519#ifdef DEV_CONFIG_CDC
520static struct usb_interface_descriptor
521control_intf = {
522 .bLength = sizeof control_intf,
523 .bDescriptorType = USB_DT_INTERFACE,
524
525 .bInterfaceNumber = 0,
526 /* status endpoint is optional; this may be patched later */
527 .bNumEndpoints = 1,
528 .bInterfaceClass = USB_CLASS_COMM,
529 .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET,
530 .bInterfaceProtocol = USB_CDC_PROTO_NONE,
531 .iInterface = STRING_CONTROL,
532};
533#endif
534
535#ifdef CONFIG_USB_ETH_RNDIS
536static const struct usb_interface_descriptor
537rndis_control_intf = {
538 .bLength = sizeof rndis_control_intf,
539 .bDescriptorType = USB_DT_INTERFACE,
David Brownell7e27f182006-06-13 09:54:40 -0700540
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 .bInterfaceNumber = 0,
542 .bNumEndpoints = 1,
543 .bInterfaceClass = USB_CLASS_COMM,
544 .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
545 .bInterfaceProtocol = USB_CDC_ACM_PROTO_VENDOR,
546 .iInterface = STRING_RNDIS_CONTROL,
547};
548#endif
549
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550static const struct usb_cdc_header_desc header_desc = {
551 .bLength = sizeof header_desc,
552 .bDescriptorType = USB_DT_CS_INTERFACE,
553 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
554
555 .bcdCDC = __constant_cpu_to_le16 (0x0110),
556};
557
David Brownell11d54892006-12-11 15:59:04 -0800558#if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
559
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560static const struct usb_cdc_union_desc union_desc = {
561 .bLength = sizeof union_desc,
562 .bDescriptorType = USB_DT_CS_INTERFACE,
563 .bDescriptorSubType = USB_CDC_UNION_TYPE,
564
565 .bMasterInterface0 = 0, /* index of control interface */
566 .bSlaveInterface0 = 1, /* index of DATA interface */
567};
568
569#endif /* CDC || RNDIS */
570
571#ifdef CONFIG_USB_ETH_RNDIS
572
573static const struct usb_cdc_call_mgmt_descriptor call_mgmt_descriptor = {
David Brownell7e27f182006-06-13 09:54:40 -0700574 .bLength = sizeof call_mgmt_descriptor,
575 .bDescriptorType = USB_DT_CS_INTERFACE,
576 .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
David Brownell7e27f182006-06-13 09:54:40 -0700578 .bmCapabilities = 0x00,
579 .bDataInterface = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580};
581
David Brownell907cba32005-04-28 13:48:09 -0700582static const struct usb_cdc_acm_descriptor acm_descriptor = {
David Brownell7e27f182006-06-13 09:54:40 -0700583 .bLength = sizeof acm_descriptor,
584 .bDescriptorType = USB_DT_CS_INTERFACE,
585 .bDescriptorSubType = USB_CDC_ACM_TYPE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
David Brownell7e27f182006-06-13 09:54:40 -0700587 .bmCapabilities = 0x00,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588};
589
590#endif
591
David Brownell11d54892006-12-11 15:59:04 -0800592#ifndef DEV_CONFIG_CDC
593
594/* "SAFE" loosely follows CDC WMC MDLM, violating the spec in various
595 * ways: data endpoints live in the control interface, there's no data
596 * interface, and it's not used to talk to a cell phone radio.
597 */
598
599static const struct usb_cdc_mdlm_desc mdlm_desc = {
600 .bLength = sizeof mdlm_desc,
601 .bDescriptorType = USB_DT_CS_INTERFACE,
602 .bDescriptorSubType = USB_CDC_MDLM_TYPE,
603
604 .bcdVersion = __constant_cpu_to_le16(0x0100),
605 .bGUID = {
606 0x5d, 0x34, 0xcf, 0x66, 0x11, 0x18, 0x11, 0xd6,
607 0xa2, 0x1a, 0x00, 0x01, 0x02, 0xca, 0x9a, 0x7f,
608 },
609};
610
611/* since "usb_cdc_mdlm_detail_desc" is a variable length structure, we
612 * can't really use its struct. All we do here is say that we're using
613 * the submode of "SAFE" which directly matches the CDC Subset.
614 */
615static const u8 mdlm_detail_desc[] = {
616 6,
617 USB_DT_CS_INTERFACE,
618 USB_CDC_MDLM_DETAIL_TYPE,
619
620 0, /* "SAFE" */
621 0, /* network control capabilities (none) */
622 0, /* network data capabilities ("raw" encapsulation) */
623};
624
625#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
627static const struct usb_cdc_ether_desc ether_desc = {
628 .bLength = sizeof ether_desc,
629 .bDescriptorType = USB_DT_CS_INTERFACE,
630 .bDescriptorSubType = USB_CDC_ETHERNET_TYPE,
631
632 /* this descriptor actually adds value, surprise! */
633 .iMACAddress = STRING_ETHADDR,
634 .bmEthernetStatistics = __constant_cpu_to_le32 (0), /* no statistics */
635 .wMaxSegmentSize = __constant_cpu_to_le16 (ETH_FRAME_LEN),
636 .wNumberMCFilters = __constant_cpu_to_le16 (0),
637 .bNumberPowerFilters = 0,
638};
639
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
641#if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
642
643/* include the status endpoint if we can, even where it's optional.
644 * use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one
Steven Cole093cf722005-05-03 19:07:24 -0600645 * packet, to simplify cancellation; and a big transfer interval, to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 * waste less bandwidth.
647 *
648 * some drivers (like Linux 2.4 cdc-ether!) "need" it to exist even
649 * if they ignore the connect/disconnect notifications that real aether
650 * can provide. more advanced cdc configurations might want to support
651 * encapsulated commands (vendor-specific, using control-OUT).
652 *
653 * RNDIS requires the status endpoint, since it uses that encapsulation
654 * mechanism for its funky RPC scheme.
655 */
David Brownell7e27f182006-06-13 09:54:40 -0700656
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657#define LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */
658#define STATUS_BYTECOUNT 16 /* 8 byte header + data */
659
660static struct usb_endpoint_descriptor
661fs_status_desc = {
662 .bLength = USB_DT_ENDPOINT_SIZE,
663 .bDescriptorType = USB_DT_ENDPOINT,
664
665 .bEndpointAddress = USB_DIR_IN,
666 .bmAttributes = USB_ENDPOINT_XFER_INT,
667 .wMaxPacketSize = __constant_cpu_to_le16 (STATUS_BYTECOUNT),
668 .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC,
669};
670#endif
671
672#ifdef DEV_CONFIG_CDC
673
674/* the default data interface has no endpoints ... */
675
676static const struct usb_interface_descriptor
677data_nop_intf = {
678 .bLength = sizeof data_nop_intf,
679 .bDescriptorType = USB_DT_INTERFACE,
680
681 .bInterfaceNumber = 1,
682 .bAlternateSetting = 0,
683 .bNumEndpoints = 0,
684 .bInterfaceClass = USB_CLASS_CDC_DATA,
685 .bInterfaceSubClass = 0,
686 .bInterfaceProtocol = 0,
687};
688
689/* ... but the "real" data interface has two bulk endpoints */
690
691static const struct usb_interface_descriptor
692data_intf = {
693 .bLength = sizeof data_intf,
694 .bDescriptorType = USB_DT_INTERFACE,
695
696 .bInterfaceNumber = 1,
697 .bAlternateSetting = 1,
698 .bNumEndpoints = 2,
699 .bInterfaceClass = USB_CLASS_CDC_DATA,
700 .bInterfaceSubClass = 0,
701 .bInterfaceProtocol = 0,
702 .iInterface = STRING_DATA,
703};
704
705#endif
706
707#ifdef CONFIG_USB_ETH_RNDIS
708
709/* RNDIS doesn't activate by changing to the "real" altsetting */
710
711static const struct usb_interface_descriptor
712rndis_data_intf = {
713 .bLength = sizeof rndis_data_intf,
714 .bDescriptorType = USB_DT_INTERFACE,
715
716 .bInterfaceNumber = 1,
717 .bAlternateSetting = 0,
718 .bNumEndpoints = 2,
719 .bInterfaceClass = USB_CLASS_CDC_DATA,
720 .bInterfaceSubClass = 0,
721 .bInterfaceProtocol = 0,
722 .iInterface = STRING_DATA,
723};
724
725#endif
726
727#ifdef DEV_CONFIG_SUBSET
728
729/*
730 * "Simple" CDC-subset option is a simple vendor-neutral model that most
731 * full speed controllers can handle: one interface, two bulk endpoints.
David Brownell11d54892006-12-11 15:59:04 -0800732 *
733 * To assist host side drivers, we fancy it up a bit, and add descriptors
734 * so some host side drivers will understand it as a "SAFE" variant.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 */
736
737static const struct usb_interface_descriptor
738subset_data_intf = {
739 .bLength = sizeof subset_data_intf,
740 .bDescriptorType = USB_DT_INTERFACE,
741
742 .bInterfaceNumber = 0,
743 .bAlternateSetting = 0,
744 .bNumEndpoints = 2,
David Brownell11d54892006-12-11 15:59:04 -0800745 .bInterfaceClass = USB_CLASS_COMM,
746 .bInterfaceSubClass = USB_CDC_SUBCLASS_MDLM,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 .bInterfaceProtocol = 0,
748 .iInterface = STRING_DATA,
749};
750
751#endif /* SUBSET */
752
753
754static struct usb_endpoint_descriptor
755fs_source_desc = {
756 .bLength = USB_DT_ENDPOINT_SIZE,
757 .bDescriptorType = USB_DT_ENDPOINT,
758
759 .bEndpointAddress = USB_DIR_IN,
760 .bmAttributes = USB_ENDPOINT_XFER_BULK,
761};
762
763static struct usb_endpoint_descriptor
764fs_sink_desc = {
765 .bLength = USB_DT_ENDPOINT_SIZE,
766 .bDescriptorType = USB_DT_ENDPOINT,
767
768 .bEndpointAddress = USB_DIR_OUT,
769 .bmAttributes = USB_ENDPOINT_XFER_BULK,
770};
771
772static const struct usb_descriptor_header *fs_eth_function [11] = {
773 (struct usb_descriptor_header *) &otg_descriptor,
774#ifdef DEV_CONFIG_CDC
775 /* "cdc" mode descriptors */
776 (struct usb_descriptor_header *) &control_intf,
777 (struct usb_descriptor_header *) &header_desc,
778 (struct usb_descriptor_header *) &union_desc,
779 (struct usb_descriptor_header *) &ether_desc,
780 /* NOTE: status endpoint may need to be removed */
781 (struct usb_descriptor_header *) &fs_status_desc,
782 /* data interface, with altsetting */
783 (struct usb_descriptor_header *) &data_nop_intf,
784 (struct usb_descriptor_header *) &data_intf,
785 (struct usb_descriptor_header *) &fs_source_desc,
786 (struct usb_descriptor_header *) &fs_sink_desc,
787 NULL,
788#endif /* DEV_CONFIG_CDC */
789};
790
791static inline void __init fs_subset_descriptors(void)
792{
793#ifdef DEV_CONFIG_SUBSET
David Brownell11d54892006-12-11 15:59:04 -0800794 /* behavior is "CDC Subset"; extra descriptors say "SAFE" */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 fs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
David Brownell11d54892006-12-11 15:59:04 -0800796 fs_eth_function[2] = (struct usb_descriptor_header *) &header_desc;
797 fs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc;
798 fs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc;
799 fs_eth_function[5] = (struct usb_descriptor_header *) &ether_desc;
800 fs_eth_function[6] = (struct usb_descriptor_header *) &fs_source_desc;
801 fs_eth_function[7] = (struct usb_descriptor_header *) &fs_sink_desc;
802 fs_eth_function[8] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803#else
804 fs_eth_function[1] = NULL;
805#endif
806}
807
808#ifdef CONFIG_USB_ETH_RNDIS
809static const struct usb_descriptor_header *fs_rndis_function [] = {
810 (struct usb_descriptor_header *) &otg_descriptor,
811 /* control interface matches ACM, not Ethernet */
812 (struct usb_descriptor_header *) &rndis_control_intf,
813 (struct usb_descriptor_header *) &header_desc,
814 (struct usb_descriptor_header *) &call_mgmt_descriptor,
815 (struct usb_descriptor_header *) &acm_descriptor,
816 (struct usb_descriptor_header *) &union_desc,
817 (struct usb_descriptor_header *) &fs_status_desc,
818 /* data interface has no altsetting */
819 (struct usb_descriptor_header *) &rndis_data_intf,
820 (struct usb_descriptor_header *) &fs_source_desc,
821 (struct usb_descriptor_header *) &fs_sink_desc,
822 NULL,
823};
824#endif
825
826#ifdef CONFIG_USB_GADGET_DUALSPEED
827
828/*
829 * usb 2.0 devices need to expose both high speed and full speed
830 * descriptors, unless they only run at full speed.
831 */
832
833#if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
834static struct usb_endpoint_descriptor
835hs_status_desc = {
836 .bLength = USB_DT_ENDPOINT_SIZE,
837 .bDescriptorType = USB_DT_ENDPOINT,
838
839 .bmAttributes = USB_ENDPOINT_XFER_INT,
840 .wMaxPacketSize = __constant_cpu_to_le16 (STATUS_BYTECOUNT),
841 .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4,
842};
843#endif /* DEV_CONFIG_CDC */
844
845static struct usb_endpoint_descriptor
846hs_source_desc = {
847 .bLength = USB_DT_ENDPOINT_SIZE,
848 .bDescriptorType = USB_DT_ENDPOINT,
849
850 .bmAttributes = USB_ENDPOINT_XFER_BULK,
851 .wMaxPacketSize = __constant_cpu_to_le16 (512),
852};
853
854static struct usb_endpoint_descriptor
855hs_sink_desc = {
856 .bLength = USB_DT_ENDPOINT_SIZE,
857 .bDescriptorType = USB_DT_ENDPOINT,
858
859 .bmAttributes = USB_ENDPOINT_XFER_BULK,
860 .wMaxPacketSize = __constant_cpu_to_le16 (512),
861};
862
863static struct usb_qualifier_descriptor
864dev_qualifier = {
865 .bLength = sizeof dev_qualifier,
866 .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
867
868 .bcdUSB = __constant_cpu_to_le16 (0x0200),
869 .bDeviceClass = USB_CLASS_COMM,
870
871 .bNumConfigurations = 1,
872};
873
874static const struct usb_descriptor_header *hs_eth_function [11] = {
875 (struct usb_descriptor_header *) &otg_descriptor,
876#ifdef DEV_CONFIG_CDC
877 /* "cdc" mode descriptors */
878 (struct usb_descriptor_header *) &control_intf,
879 (struct usb_descriptor_header *) &header_desc,
880 (struct usb_descriptor_header *) &union_desc,
881 (struct usb_descriptor_header *) &ether_desc,
882 /* NOTE: status endpoint may need to be removed */
883 (struct usb_descriptor_header *) &hs_status_desc,
884 /* data interface, with altsetting */
885 (struct usb_descriptor_header *) &data_nop_intf,
886 (struct usb_descriptor_header *) &data_intf,
887 (struct usb_descriptor_header *) &hs_source_desc,
888 (struct usb_descriptor_header *) &hs_sink_desc,
889 NULL,
890#endif /* DEV_CONFIG_CDC */
891};
892
893static inline void __init hs_subset_descriptors(void)
894{
895#ifdef DEV_CONFIG_SUBSET
David Brownell11d54892006-12-11 15:59:04 -0800896 /* behavior is "CDC Subset"; extra descriptors say "SAFE" */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 hs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
David Brownell11d54892006-12-11 15:59:04 -0800898 hs_eth_function[2] = (struct usb_descriptor_header *) &header_desc;
899 hs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc;
900 hs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc;
901 hs_eth_function[5] = (struct usb_descriptor_header *) &ether_desc;
902 hs_eth_function[6] = (struct usb_descriptor_header *) &hs_source_desc;
903 hs_eth_function[7] = (struct usb_descriptor_header *) &hs_sink_desc;
904 hs_eth_function[8] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905#else
906 hs_eth_function[1] = NULL;
907#endif
908}
909
910#ifdef CONFIG_USB_ETH_RNDIS
911static const struct usb_descriptor_header *hs_rndis_function [] = {
912 (struct usb_descriptor_header *) &otg_descriptor,
913 /* control interface matches ACM, not Ethernet */
914 (struct usb_descriptor_header *) &rndis_control_intf,
915 (struct usb_descriptor_header *) &header_desc,
916 (struct usb_descriptor_header *) &call_mgmt_descriptor,
917 (struct usb_descriptor_header *) &acm_descriptor,
918 (struct usb_descriptor_header *) &union_desc,
919 (struct usb_descriptor_header *) &hs_status_desc,
920 /* data interface has no altsetting */
921 (struct usb_descriptor_header *) &rndis_data_intf,
922 (struct usb_descriptor_header *) &hs_source_desc,
923 (struct usb_descriptor_header *) &hs_sink_desc,
924 NULL,
925};
926#endif
927
928
929/* maxpacket and other transfer characteristics vary by speed. */
930#define ep_desc(g,hs,fs) (((g)->speed==USB_SPEED_HIGH)?(hs):(fs))
931
932#else
933
934/* if there's no high speed support, maxpacket doesn't change. */
David Brownell907cba32005-04-28 13:48:09 -0700935#define ep_desc(g,hs,fs) (((void)(g)), (fs))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
937static inline void __init hs_subset_descriptors(void)
938{
939}
940
941#endif /* !CONFIG_USB_GADGET_DUALSPEED */
942
943/*-------------------------------------------------------------------------*/
944
945/* descriptors that are built on-demand */
946
947static char manufacturer [50];
948static char product_desc [40] = DRIVER_DESC;
Aras Vaichas1afc64a2006-02-18 12:31:23 -0800949static char serial_number [20];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951/* address that the host will use ... usually assigned at random */
952static char ethaddr [2 * ETH_ALEN + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
954/* static strings, in UTF-8 */
955static struct usb_string strings [] = {
956 { STRING_MANUFACTURER, manufacturer, },
957 { STRING_PRODUCT, product_desc, },
Aras Vaichas1afc64a2006-02-18 12:31:23 -0800958 { STRING_SERIALNUMBER, serial_number, },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 { STRING_DATA, "Ethernet Data", },
David Brownell11d54892006-12-11 15:59:04 -0800960 { STRING_ETHADDR, ethaddr, },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961#ifdef DEV_CONFIG_CDC
962 { STRING_CDC, "CDC Ethernet", },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 { STRING_CONTROL, "CDC Communications Control", },
964#endif
965#ifdef DEV_CONFIG_SUBSET
966 { STRING_SUBSET, "CDC Ethernet Subset", },
967#endif
968#ifdef CONFIG_USB_ETH_RNDIS
969 { STRING_RNDIS, "RNDIS", },
970 { STRING_RNDIS_CONTROL, "RNDIS Communications Control", },
971#endif
972 { } /* end of list */
973};
974
975static struct usb_gadget_strings stringtab = {
976 .language = 0x0409, /* en-us */
977 .strings = strings,
978};
979
980/*
981 * one config, two interfaces: control, data.
982 * complications: class descriptors, and an altsetting.
983 */
984static int
985config_buf (enum usb_device_speed speed,
986 u8 *buf, u8 type,
987 unsigned index, int is_otg)
988{
989 int len;
990 const struct usb_config_descriptor *config;
991 const struct usb_descriptor_header **function;
992#ifdef CONFIG_USB_GADGET_DUALSPEED
993 int hs = (speed == USB_SPEED_HIGH);
994
995 if (type == USB_DT_OTHER_SPEED_CONFIG)
996 hs = !hs;
997#define which_fn(t) (hs ? hs_ ## t ## _function : fs_ ## t ## _function)
998#else
999#define which_fn(t) (fs_ ## t ## _function)
1000#endif
1001
1002 if (index >= device_desc.bNumConfigurations)
1003 return -EINVAL;
1004
1005#ifdef CONFIG_USB_ETH_RNDIS
1006 /* list the RNDIS config first, to make Microsoft's drivers
1007 * happy. DOCSIS 1.0 needs this too.
1008 */
1009 if (device_desc.bNumConfigurations == 2 && index == 0) {
1010 config = &rndis_config;
1011 function = which_fn (rndis);
1012 } else
1013#endif
1014 {
1015 config = &eth_config;
1016 function = which_fn (eth);
1017 }
1018
1019 /* for now, don't advertise srp-only devices */
1020 if (!is_otg)
1021 function++;
1022
1023 len = usb_gadget_config_buf (config, buf, USB_BUFSIZ, function);
1024 if (len < 0)
1025 return len;
1026 ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
1027 return len;
1028}
1029
1030/*-------------------------------------------------------------------------*/
1031
Al Viro55016f12005-10-21 03:21:58 -04001032static void eth_start (struct eth_dev *dev, gfp_t gfp_flags);
1033static int alloc_requests (struct eth_dev *dev, unsigned n, gfp_t gfp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
David Brownell907cba32005-04-28 13:48:09 -07001035static int
Al Viro55016f12005-10-21 03:21:58 -04001036set_ether_config (struct eth_dev *dev, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037{
David Brownell907cba32005-04-28 13:48:09 -07001038 int result = 0;
1039 struct usb_gadget *gadget = dev->gadget;
1040
Ian Campbelle8282642005-06-29 10:20:29 +01001041#if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
David Brownell907cba32005-04-28 13:48:09 -07001042 /* status endpoint used for RNDIS and (optionally) CDC */
1043 if (!subset_active(dev) && dev->status_ep) {
1044 dev->status = ep_desc (gadget, &hs_status_desc,
1045 &fs_status_desc);
1046 dev->status_ep->driver_data = dev;
1047
1048 result = usb_ep_enable (dev->status_ep, dev->status);
1049 if (result != 0) {
David Brownell7e27f182006-06-13 09:54:40 -07001050 DEBUG (dev, "enable %s --> %d\n",
David Brownell907cba32005-04-28 13:48:09 -07001051 dev->status_ep->name, result);
1052 goto done;
1053 }
1054 }
Ian Campbelle8282642005-06-29 10:20:29 +01001055#endif
David Brownell907cba32005-04-28 13:48:09 -07001056
David Brownell11d54892006-12-11 15:59:04 -08001057 dev->in = ep_desc(gadget, &hs_source_desc, &fs_source_desc);
David Brownell907cba32005-04-28 13:48:09 -07001058 dev->in_ep->driver_data = dev;
1059
David Brownell11d54892006-12-11 15:59:04 -08001060 dev->out = ep_desc(gadget, &hs_sink_desc, &fs_sink_desc);
David Brownell907cba32005-04-28 13:48:09 -07001061 dev->out_ep->driver_data = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
1063 /* With CDC, the host isn't allowed to use these two data
1064 * endpoints in the default altsetting for the interface.
1065 * so we don't activate them yet. Reset from SET_INTERFACE.
1066 *
1067 * Strictly speaking RNDIS should work the same: activation is
1068 * a side effect of setting a packet filter. Deactivation is
1069 * from REMOTE_NDIS_HALT_MSG, reset from REMOTE_NDIS_RESET_MSG.
1070 */
David Brownell907cba32005-04-28 13:48:09 -07001071 if (!cdc_active(dev)) {
1072 result = usb_ep_enable (dev->in_ep, dev->in);
1073 if (result != 0) {
David Brownell7e27f182006-06-13 09:54:40 -07001074 DEBUG(dev, "enable %s --> %d\n",
David Brownell907cba32005-04-28 13:48:09 -07001075 dev->in_ep->name, result);
1076 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 }
1078
David Brownell907cba32005-04-28 13:48:09 -07001079 result = usb_ep_enable (dev->out_ep, dev->out);
1080 if (result != 0) {
David Brownell7e27f182006-06-13 09:54:40 -07001081 DEBUG (dev, "enable %s --> %d\n",
Matt Reimer4186c292006-06-07 11:46:13 -07001082 dev->out_ep->name, result);
David Brownell907cba32005-04-28 13:48:09 -07001083 goto done;
1084 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
David Brownell907cba32005-04-28 13:48:09 -07001087done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 if (result == 0)
1089 result = alloc_requests (dev, qlen (gadget), gfp_flags);
1090
1091 /* on error, disable any endpoints */
1092 if (result < 0) {
David Brownell907cba32005-04-28 13:48:09 -07001093 if (!subset_active(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 (void) usb_ep_disable (dev->status_ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 dev->status = NULL;
David Brownell907cba32005-04-28 13:48:09 -07001096 (void) usb_ep_disable (dev->in_ep);
1097 (void) usb_ep_disable (dev->out_ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 dev->in = NULL;
1099 dev->out = NULL;
1100 } else
1101
1102 /* activate non-CDC configs right away
1103 * this isn't strictly according to the RNDIS spec
1104 */
David Brownell907cba32005-04-28 13:48:09 -07001105 if (!cdc_active (dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 netif_carrier_on (dev->net);
1107 if (netif_running (dev->net)) {
1108 spin_unlock (&dev->lock);
1109 eth_start (dev, GFP_ATOMIC);
1110 spin_lock (&dev->lock);
1111 }
1112 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113
1114 if (result == 0)
1115 DEBUG (dev, "qlen %d\n", qlen (gadget));
1116
1117 /* caller is responsible for cleanup on error */
1118 return result;
1119}
1120
1121static void eth_reset_config (struct eth_dev *dev)
1122{
1123 struct usb_request *req;
1124
1125 if (dev->config == 0)
1126 return;
1127
1128 DEBUG (dev, "%s\n", __FUNCTION__);
1129
1130 netif_stop_queue (dev->net);
1131 netif_carrier_off (dev->net);
David Brownell340600a2005-04-28 13:45:25 -07001132 rndis_uninit(dev->rndis_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
1134 /* disable endpoints, forcing (synchronous) completion of
1135 * pending i/o. then free the requests.
1136 */
1137 if (dev->in) {
1138 usb_ep_disable (dev->in_ep);
David Brownell789851c2006-08-21 15:26:38 -07001139 spin_lock(&dev->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 while (likely (!list_empty (&dev->tx_reqs))) {
1141 req = container_of (dev->tx_reqs.next,
1142 struct usb_request, list);
1143 list_del (&req->list);
David Brownell789851c2006-08-21 15:26:38 -07001144
1145 spin_unlock(&dev->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 usb_ep_free_request (dev->in_ep, req);
David Brownell789851c2006-08-21 15:26:38 -07001147 spin_lock(&dev->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 }
David Brownell789851c2006-08-21 15:26:38 -07001149 spin_unlock(&dev->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 }
1151 if (dev->out) {
1152 usb_ep_disable (dev->out_ep);
David Brownell789851c2006-08-21 15:26:38 -07001153 spin_lock(&dev->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 while (likely (!list_empty (&dev->rx_reqs))) {
1155 req = container_of (dev->rx_reqs.next,
1156 struct usb_request, list);
1157 list_del (&req->list);
David Brownell789851c2006-08-21 15:26:38 -07001158
1159 spin_unlock(&dev->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 usb_ep_free_request (dev->out_ep, req);
David Brownell789851c2006-08-21 15:26:38 -07001161 spin_lock(&dev->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 }
David Brownell789851c2006-08-21 15:26:38 -07001163 spin_unlock(&dev->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 }
1165
1166 if (dev->status) {
1167 usb_ep_disable (dev->status_ep);
1168 }
David Brownell907cba32005-04-28 13:48:09 -07001169 dev->rndis = 0;
1170 dev->cdc_filter = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 dev->config = 0;
1172}
1173
1174/* change our operational config. must agree with the code
1175 * that returns config descriptors, and altsetting code.
1176 */
1177static int
Al Viro55016f12005-10-21 03:21:58 -04001178eth_set_config (struct eth_dev *dev, unsigned number, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179{
1180 int result = 0;
1181 struct usb_gadget *gadget = dev->gadget;
1182
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 if (gadget_is_sa1100 (gadget)
1184 && dev->config
1185 && atomic_read (&dev->tx_qlen) != 0) {
1186 /* tx fifo is full, but we can't clear it...*/
1187 INFO (dev, "can't change configurations\n");
1188 return -ESPIPE;
1189 }
1190 eth_reset_config (dev);
1191
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 switch (number) {
1193 case DEV_CONFIG_VALUE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 result = set_ether_config (dev, gfp_flags);
1195 break;
1196#ifdef CONFIG_USB_ETH_RNDIS
1197 case DEV_RNDIS_CONFIG_VALUE:
1198 dev->rndis = 1;
1199 result = set_ether_config (dev, gfp_flags);
1200 break;
1201#endif
1202 default:
1203 result = -EINVAL;
1204 /* FALL THROUGH */
1205 case 0:
1206 break;
1207 }
1208
1209 if (result) {
1210 if (number)
1211 eth_reset_config (dev);
1212 usb_gadget_vbus_draw(dev->gadget,
1213 dev->gadget->is_otg ? 8 : 100);
1214 } else {
1215 char *speed;
1216 unsigned power;
1217
1218 power = 2 * eth_config.bMaxPower;
1219 usb_gadget_vbus_draw(dev->gadget, power);
1220
1221 switch (gadget->speed) {
1222 case USB_SPEED_FULL: speed = "full"; break;
1223#ifdef CONFIG_USB_GADGET_DUALSPEED
1224 case USB_SPEED_HIGH: speed = "high"; break;
1225#endif
David Brownell7e27f182006-06-13 09:54:40 -07001226 default: speed = "?"; break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 }
1228
1229 dev->config = number;
1230 INFO (dev, "%s speed config #%d: %d mA, %s, using %s\n",
1231 speed, number, power, driver_desc,
David Brownell45e45ab2005-05-16 08:26:38 -07001232 rndis_active(dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 ? "RNDIS"
David Brownell45e45ab2005-05-16 08:26:38 -07001234 : (cdc_active(dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 ? "CDC Ethernet"
1236 : "CDC Ethernet Subset"));
1237 }
1238 return result;
1239}
1240
1241/*-------------------------------------------------------------------------*/
1242
1243#ifdef DEV_CONFIG_CDC
1244
David Brownell907cba32005-04-28 13:48:09 -07001245/* The interrupt endpoint is used in CDC networking models (Ethernet, ATM)
1246 * only to notify the host about link status changes (which we support) or
1247 * report completion of some encapsulated command (as used in RNDIS). Since
1248 * we want this CDC Ethernet code to be vendor-neutral, we don't use that
1249 * command mechanism; and only one status request is ever queued.
1250 */
1251
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252static void eth_status_complete (struct usb_ep *ep, struct usb_request *req)
1253{
1254 struct usb_cdc_notification *event = req->buf;
1255 int value = req->status;
1256 struct eth_dev *dev = ep->driver_data;
1257
1258 /* issue the second notification if host reads the first */
1259 if (event->bNotificationType == USB_CDC_NOTIFY_NETWORK_CONNECTION
1260 && value == 0) {
1261 __le32 *data = req->buf + sizeof *event;
1262
1263 event->bmRequestType = 0xA1;
1264 event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE;
1265 event->wValue = __constant_cpu_to_le16 (0);
1266 event->wIndex = __constant_cpu_to_le16 (1);
1267 event->wLength = __constant_cpu_to_le16 (8);
1268
1269 /* SPEED_CHANGE data is up/down speeds in bits/sec */
1270 data [0] = data [1] = cpu_to_le32 (BITRATE (dev->gadget));
1271
1272 req->length = STATUS_BYTECOUNT;
1273 value = usb_ep_queue (ep, req, GFP_ATOMIC);
1274 DEBUG (dev, "send SPEED_CHANGE --> %d\n", value);
1275 if (value == 0)
1276 return;
1277 } else if (value != -ECONNRESET)
1278 DEBUG (dev, "event %02x --> %d\n",
1279 event->bNotificationType, value);
David Brownell907cba32005-04-28 13:48:09 -07001280 req->context = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281}
1282
1283static void issue_start_status (struct eth_dev *dev)
1284{
1285 struct usb_request *req = dev->stat_req;
1286 struct usb_cdc_notification *event;
1287 int value;
David Brownell7e27f182006-06-13 09:54:40 -07001288
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 DEBUG (dev, "%s, flush old status first\n", __FUNCTION__);
1290
1291 /* flush old status
1292 *
1293 * FIXME ugly idiom, maybe we'd be better with just
1294 * a "cancel the whole queue" primitive since any
1295 * unlink-one primitive has way too many error modes.
1296 * here, we "know" toggle is already clear...
David Brownell907cba32005-04-28 13:48:09 -07001297 *
1298 * FIXME iff req->context != null just dequeue it
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 */
1300 usb_ep_disable (dev->status_ep);
1301 usb_ep_enable (dev->status_ep, dev->status);
1302
1303 /* 3.8.1 says to issue first NETWORK_CONNECTION, then
1304 * a SPEED_CHANGE. could be useful in some configs.
1305 */
1306 event = req->buf;
1307 event->bmRequestType = 0xA1;
1308 event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION;
1309 event->wValue = __constant_cpu_to_le16 (1); /* connected */
1310 event->wIndex = __constant_cpu_to_le16 (1);
1311 event->wLength = 0;
1312
1313 req->length = sizeof *event;
1314 req->complete = eth_status_complete;
David Brownell907cba32005-04-28 13:48:09 -07001315 req->context = dev;
1316
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 value = usb_ep_queue (dev->status_ep, req, GFP_ATOMIC);
1318 if (value < 0)
1319 DEBUG (dev, "status buf queue --> %d\n", value);
1320}
1321
1322#endif
1323
1324/*-------------------------------------------------------------------------*/
1325
1326static void eth_setup_complete (struct usb_ep *ep, struct usb_request *req)
1327{
1328 if (req->status || req->actual != req->length)
1329 DEBUG ((struct eth_dev *) ep->driver_data,
1330 "setup complete --> %d, %d/%d\n",
1331 req->status, req->actual, req->length);
1332}
1333
1334#ifdef CONFIG_USB_ETH_RNDIS
1335
1336static void rndis_response_complete (struct usb_ep *ep, struct usb_request *req)
1337{
1338 if (req->status || req->actual != req->length)
1339 DEBUG ((struct eth_dev *) ep->driver_data,
1340 "rndis response complete --> %d, %d/%d\n",
1341 req->status, req->actual, req->length);
1342
1343 /* done sending after USB_CDC_GET_ENCAPSULATED_RESPONSE */
1344}
1345
1346static void rndis_command_complete (struct usb_ep *ep, struct usb_request *req)
1347{
1348 struct eth_dev *dev = ep->driver_data;
1349 int status;
David Brownell7e27f182006-06-13 09:54:40 -07001350
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 /* received RNDIS command from USB_CDC_SEND_ENCAPSULATED_COMMAND */
1352 spin_lock(&dev->lock);
1353 status = rndis_msg_parser (dev->rndis_config, (u8 *) req->buf);
1354 if (status < 0)
1355 ERROR(dev, "%s: rndis parse error %d\n", __FUNCTION__, status);
1356 spin_unlock(&dev->lock);
1357}
1358
1359#endif /* RNDIS */
1360
1361/*
1362 * The setup() callback implements all the ep0 functionality that's not
1363 * handled lower down. CDC has a number of less-common features:
1364 *
1365 * - two interfaces: control, and ethernet data
1366 * - Ethernet data interface has two altsettings: default, and active
1367 * - class-specific descriptors for the control interface
1368 * - class-specific control requests
1369 */
1370static int
1371eth_setup (struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
1372{
1373 struct eth_dev *dev = get_gadget_data (gadget);
1374 struct usb_request *req = dev->req;
1375 int value = -EOPNOTSUPP;
David Brownell1bbc1692005-05-07 13:05:13 -07001376 u16 wIndex = le16_to_cpu(ctrl->wIndex);
1377 u16 wValue = le16_to_cpu(ctrl->wValue);
1378 u16 wLength = le16_to_cpu(ctrl->wLength);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
1380 /* descriptors just go into the pre-allocated ep0 buffer,
1381 * while config change events may enable network traffic.
1382 */
1383 req->complete = eth_setup_complete;
1384 switch (ctrl->bRequest) {
1385
1386 case USB_REQ_GET_DESCRIPTOR:
1387 if (ctrl->bRequestType != USB_DIR_IN)
1388 break;
1389 switch (wValue >> 8) {
1390
1391 case USB_DT_DEVICE:
1392 value = min (wLength, (u16) sizeof device_desc);
1393 memcpy (req->buf, &device_desc, value);
1394 break;
1395#ifdef CONFIG_USB_GADGET_DUALSPEED
1396 case USB_DT_DEVICE_QUALIFIER:
1397 if (!gadget->is_dualspeed)
1398 break;
1399 value = min (wLength, (u16) sizeof dev_qualifier);
1400 memcpy (req->buf, &dev_qualifier, value);
1401 break;
1402
1403 case USB_DT_OTHER_SPEED_CONFIG:
1404 if (!gadget->is_dualspeed)
1405 break;
1406 // FALLTHROUGH
1407#endif /* CONFIG_USB_GADGET_DUALSPEED */
1408 case USB_DT_CONFIG:
1409 value = config_buf (gadget->speed, req->buf,
1410 wValue >> 8,
1411 wValue & 0xff,
1412 gadget->is_otg);
1413 if (value >= 0)
1414 value = min (wLength, (u16) value);
1415 break;
1416
1417 case USB_DT_STRING:
1418 value = usb_gadget_get_string (&stringtab,
1419 wValue & 0xff, req->buf);
1420 if (value >= 0)
1421 value = min (wLength, (u16) value);
1422 break;
1423 }
1424 break;
1425
1426 case USB_REQ_SET_CONFIGURATION:
1427 if (ctrl->bRequestType != 0)
1428 break;
1429 if (gadget->a_hnp_support)
1430 DEBUG (dev, "HNP available\n");
1431 else if (gadget->a_alt_hnp_support)
1432 DEBUG (dev, "HNP needs a different root port\n");
1433 spin_lock (&dev->lock);
1434 value = eth_set_config (dev, wValue, GFP_ATOMIC);
1435 spin_unlock (&dev->lock);
1436 break;
1437 case USB_REQ_GET_CONFIGURATION:
1438 if (ctrl->bRequestType != USB_DIR_IN)
1439 break;
1440 *(u8 *)req->buf = dev->config;
1441 value = min (wLength, (u16) 1);
1442 break;
1443
1444 case USB_REQ_SET_INTERFACE:
1445 if (ctrl->bRequestType != USB_RECIP_INTERFACE
1446 || !dev->config
1447 || wIndex > 1)
1448 break;
David Brownell45e45ab2005-05-16 08:26:38 -07001449 if (!cdc_active(dev) && wIndex != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 break;
1451 spin_lock (&dev->lock);
1452
1453 /* PXA hardware partially handles SET_INTERFACE;
1454 * we need to kluge around that interference.
1455 */
1456 if (gadget_is_pxa (gadget)) {
1457 value = eth_set_config (dev, DEV_CONFIG_VALUE,
1458 GFP_ATOMIC);
1459 goto done_set_intf;
1460 }
1461
1462#ifdef DEV_CONFIG_CDC
1463 switch (wIndex) {
1464 case 0: /* control/master intf */
1465 if (wValue != 0)
1466 break;
1467 if (dev->status) {
1468 usb_ep_disable (dev->status_ep);
1469 usb_ep_enable (dev->status_ep, dev->status);
1470 }
1471 value = 0;
1472 break;
1473 case 1: /* data intf */
1474 if (wValue > 1)
1475 break;
1476 usb_ep_disable (dev->in_ep);
1477 usb_ep_disable (dev->out_ep);
1478
1479 /* CDC requires the data transfers not be done from
1480 * the default interface setting ... also, setting
David Brownell907cba32005-04-28 13:48:09 -07001481 * the non-default interface resets filters etc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 */
1483 if (wValue == 1) {
David Brownell907cba32005-04-28 13:48:09 -07001484 if (!cdc_active (dev))
1485 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 usb_ep_enable (dev->in_ep, dev->in);
1487 usb_ep_enable (dev->out_ep, dev->out);
1488 dev->cdc_filter = DEFAULT_FILTER;
1489 netif_carrier_on (dev->net);
1490 if (dev->status)
1491 issue_start_status (dev);
1492 if (netif_running (dev->net)) {
1493 spin_unlock (&dev->lock);
1494 eth_start (dev, GFP_ATOMIC);
1495 spin_lock (&dev->lock);
1496 }
1497 } else {
1498 netif_stop_queue (dev->net);
1499 netif_carrier_off (dev->net);
1500 }
1501 value = 0;
1502 break;
1503 }
1504#else
1505 /* FIXME this is wrong, as is the assumption that
1506 * all non-PXA hardware talks real CDC ...
1507 */
1508 dev_warn (&gadget->dev, "set_interface ignored!\n");
1509#endif /* DEV_CONFIG_CDC */
1510
1511done_set_intf:
1512 spin_unlock (&dev->lock);
1513 break;
1514 case USB_REQ_GET_INTERFACE:
1515 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE)
1516 || !dev->config
1517 || wIndex > 1)
1518 break;
David Brownell45e45ab2005-05-16 08:26:38 -07001519 if (!(cdc_active(dev) || rndis_active(dev)) && wIndex != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 break;
1521
1522 /* for CDC, iff carrier is on, data interface is active. */
David Brownell45e45ab2005-05-16 08:26:38 -07001523 if (rndis_active(dev) || wIndex != 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 *(u8 *)req->buf = 0;
1525 else
1526 *(u8 *)req->buf = netif_carrier_ok (dev->net) ? 1 : 0;
1527 value = min (wLength, (u16) 1);
1528 break;
1529
1530#ifdef DEV_CONFIG_CDC
1531 case USB_CDC_SET_ETHERNET_PACKET_FILTER:
1532 /* see 6.2.30: no data, wIndex = interface,
1533 * wValue = packet filter bitmap
1534 */
1535 if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
David Brownell45e45ab2005-05-16 08:26:38 -07001536 || !cdc_active(dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 || wLength != 0
1538 || wIndex > 1)
1539 break;
1540 DEBUG (dev, "packet filter %02x\n", wValue);
1541 dev->cdc_filter = wValue;
1542 value = 0;
1543 break;
1544
1545 /* and potentially:
1546 * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS:
1547 * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER:
1548 * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER:
1549 * case USB_CDC_GET_ETHERNET_STATISTIC:
1550 */
1551
1552#endif /* DEV_CONFIG_CDC */
1553
David Brownell7e27f182006-06-13 09:54:40 -07001554#ifdef CONFIG_USB_ETH_RNDIS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 /* RNDIS uses the CDC command encapsulation mechanism to implement
1556 * an RPC scheme, with much getting/setting of attributes by OID.
1557 */
1558 case USB_CDC_SEND_ENCAPSULATED_COMMAND:
1559 if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
David Brownell45e45ab2005-05-16 08:26:38 -07001560 || !rndis_active(dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 || wLength > USB_BUFSIZ
1562 || wValue
1563 || rndis_control_intf.bInterfaceNumber
1564 != wIndex)
1565 break;
1566 /* read the request, then process it */
1567 value = wLength;
1568 req->complete = rndis_command_complete;
1569 /* later, rndis_control_ack () sends a notification */
1570 break;
David Brownell7e27f182006-06-13 09:54:40 -07001571
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 case USB_CDC_GET_ENCAPSULATED_RESPONSE:
1573 if ((USB_DIR_IN|USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1574 == ctrl->bRequestType
David Brownell45e45ab2005-05-16 08:26:38 -07001575 && rndis_active(dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 // && wLength >= 0x0400
1577 && !wValue
1578 && rndis_control_intf.bInterfaceNumber
1579 == wIndex) {
1580 u8 *buf;
1581
1582 /* return the result */
1583 buf = rndis_get_next_response (dev->rndis_config,
1584 &value);
1585 if (buf) {
1586 memcpy (req->buf, buf, value);
1587 req->complete = rndis_response_complete;
1588 rndis_free_response(dev->rndis_config, buf);
1589 }
1590 /* else stalls ... spec says to avoid that */
1591 }
1592 break;
1593#endif /* RNDIS */
1594
1595 default:
1596 VDEBUG (dev,
1597 "unknown control req%02x.%02x v%04x i%04x l%d\n",
1598 ctrl->bRequestType, ctrl->bRequest,
1599 wValue, wIndex, wLength);
1600 }
1601
1602 /* respond with data transfer before status phase? */
1603 if (value >= 0) {
1604 req->length = value;
1605 req->zero = value < wLength
1606 && (value % gadget->ep0->maxpacket) == 0;
1607 value = usb_ep_queue (gadget->ep0, req, GFP_ATOMIC);
1608 if (value < 0) {
1609 DEBUG (dev, "ep_queue --> %d\n", value);
1610 req->status = 0;
1611 eth_setup_complete (gadget->ep0, req);
1612 }
1613 }
1614
1615 /* host either stalls (value < 0) or reports success */
1616 return value;
1617}
1618
1619static void
1620eth_disconnect (struct usb_gadget *gadget)
1621{
1622 struct eth_dev *dev = get_gadget_data (gadget);
1623 unsigned long flags;
1624
1625 spin_lock_irqsave (&dev->lock, flags);
1626 netif_stop_queue (dev->net);
1627 netif_carrier_off (dev->net);
1628 eth_reset_config (dev);
1629 spin_unlock_irqrestore (&dev->lock, flags);
1630
1631 /* FIXME RNDIS should enter RNDIS_UNINITIALIZED */
1632
1633 /* next we may get setup() calls to enumerate new connections;
1634 * or an unbind() during shutdown (including removing module).
1635 */
1636}
1637
1638/*-------------------------------------------------------------------------*/
1639
1640/* NETWORK DRIVER HOOKUP (to the layer above this driver) */
1641
1642static int eth_change_mtu (struct net_device *net, int new_mtu)
1643{
1644 struct eth_dev *dev = netdev_priv(net);
1645
David Brownell7802ac52006-01-22 10:33:27 -08001646 if (dev->rndis)
1647 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648
1649 if (new_mtu <= ETH_HLEN || new_mtu > ETH_FRAME_LEN)
1650 return -ERANGE;
1651 /* no zero-length packet read wanted after mtu-sized packets */
1652 if (((new_mtu + sizeof (struct ethhdr)) % dev->in_ep->maxpacket) == 0)
1653 return -EDOM;
1654 net->mtu = new_mtu;
1655 return 0;
1656}
1657
1658static struct net_device_stats *eth_get_stats (struct net_device *net)
1659{
1660 return &((struct eth_dev *)netdev_priv(net))->stats;
1661}
1662
1663static void eth_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *p)
1664{
1665 struct eth_dev *dev = netdev_priv(net);
1666 strlcpy(p->driver, shortname, sizeof p->driver);
1667 strlcpy(p->version, DRIVER_VERSION, sizeof p->version);
1668 strlcpy(p->fw_version, dev->gadget->name, sizeof p->fw_version);
1669 strlcpy (p->bus_info, dev->gadget->dev.bus_id, sizeof p->bus_info);
1670}
1671
1672static u32 eth_get_link(struct net_device *net)
1673{
1674 struct eth_dev *dev = netdev_priv(net);
1675 return dev->gadget->speed != USB_SPEED_UNKNOWN;
1676}
1677
1678static struct ethtool_ops ops = {
1679 .get_drvinfo = eth_get_drvinfo,
1680 .get_link = eth_get_link
1681};
1682
1683static void defer_kevent (struct eth_dev *dev, int flag)
1684{
1685 if (test_and_set_bit (flag, &dev->todo))
1686 return;
1687 if (!schedule_work (&dev->work))
1688 ERROR (dev, "kevent %d may have been dropped\n", flag);
1689 else
1690 DEBUG (dev, "kevent %d scheduled\n", flag);
1691}
1692
1693static void rx_complete (struct usb_ep *ep, struct usb_request *req);
1694
1695static int
Al Viro55016f12005-10-21 03:21:58 -04001696rx_submit (struct eth_dev *dev, struct usb_request *req, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697{
1698 struct sk_buff *skb;
1699 int retval = -ENOMEM;
1700 size_t size;
1701
1702 /* Padding up to RX_EXTRA handles minor disagreements with host.
1703 * Normally we use the USB "terminate on short read" convention;
1704 * so allow up to (N*maxpacket), since that memory is normally
1705 * already allocated. Some hardware doesn't deal well with short
1706 * reads (e.g. DMA must be N*maxpacket), so for now don't trim a
1707 * byte off the end (to force hardware errors on overflow).
1708 *
1709 * RNDIS uses internal framing, and explicitly allows senders to
1710 * pad to end-of-packet. That's potentially nice for speed,
1711 * but means receivers can't recover synch on their own.
1712 */
1713 size = (sizeof (struct ethhdr) + dev->net->mtu + RX_EXTRA);
1714 size += dev->out_ep->maxpacket - 1;
David Brownell907cba32005-04-28 13:48:09 -07001715 if (rndis_active(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 size += sizeof (struct rndis_packet_msg_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 size -= size % dev->out_ep->maxpacket;
1718
1719 if ((skb = alloc_skb (size + NET_IP_ALIGN, gfp_flags)) == 0) {
1720 DEBUG (dev, "no rx skb\n");
1721 goto enomem;
1722 }
David Brownell7e27f182006-06-13 09:54:40 -07001723
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 /* Some platforms perform better when IP packets are aligned,
1725 * but on at least one, checksumming fails otherwise. Note:
David Brownell6cdee102005-04-18 17:39:34 -07001726 * RNDIS headers involve variable numbers of LE32 values.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 */
1728 skb_reserve(skb, NET_IP_ALIGN);
1729
1730 req->buf = skb->data;
1731 req->length = size;
1732 req->complete = rx_complete;
1733 req->context = skb;
1734
1735 retval = usb_ep_queue (dev->out_ep, req, gfp_flags);
1736 if (retval == -ENOMEM)
1737enomem:
1738 defer_kevent (dev, WORK_RX_MEMORY);
1739 if (retval) {
1740 DEBUG (dev, "rx submit --> %d\n", retval);
Erik Hovlandb8d297c2007-04-23 10:50:15 -07001741 if (skb)
1742 dev_kfree_skb_any(skb);
David Brownell789851c2006-08-21 15:26:38 -07001743 spin_lock(&dev->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 list_add (&req->list, &dev->rx_reqs);
David Brownell789851c2006-08-21 15:26:38 -07001745 spin_unlock(&dev->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 }
1747 return retval;
1748}
1749
1750static void rx_complete (struct usb_ep *ep, struct usb_request *req)
1751{
1752 struct sk_buff *skb = req->context;
1753 struct eth_dev *dev = ep->driver_data;
1754 int status = req->status;
1755
1756 switch (status) {
1757
1758 /* normal completion */
1759 case 0:
1760 skb_put (skb, req->actual);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 /* we know MaxPacketsPerTransfer == 1 here */
David Brownell45e45ab2005-05-16 08:26:38 -07001762 if (rndis_active(dev))
David Brownell6cdee102005-04-18 17:39:34 -07001763 status = rndis_rm_hdr (skb);
David Brownell6cdee102005-04-18 17:39:34 -07001764 if (status < 0
1765 || ETH_HLEN > skb->len
1766 || skb->len > ETH_FRAME_LEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 dev->stats.rx_errors++;
1768 dev->stats.rx_length_errors++;
1769 DEBUG (dev, "rx length %d\n", skb->len);
1770 break;
1771 }
1772
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 skb->protocol = eth_type_trans (skb, dev->net);
1774 dev->stats.rx_packets++;
1775 dev->stats.rx_bytes += skb->len;
1776
1777 /* no buffer copies needed, unless hardware can't
1778 * use skb buffers.
1779 */
1780 status = netif_rx (skb);
1781 skb = NULL;
1782 break;
1783
1784 /* software-driven interface shutdown */
1785 case -ECONNRESET: // unlink
1786 case -ESHUTDOWN: // disconnect etc
1787 VDEBUG (dev, "rx shutdown, code %d\n", status);
1788 goto quiesce;
1789
1790 /* for hardware automagic (such as pxa) */
1791 case -ECONNABORTED: // endpoint reset
1792 DEBUG (dev, "rx %s reset\n", ep->name);
1793 defer_kevent (dev, WORK_RX_MEMORY);
1794quiesce:
1795 dev_kfree_skb_any (skb);
1796 goto clean;
1797
1798 /* data overrun */
1799 case -EOVERFLOW:
1800 dev->stats.rx_over_errors++;
1801 // FALLTHROUGH
David Brownell7e27f182006-06-13 09:54:40 -07001802
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 default:
1804 dev->stats.rx_errors++;
1805 DEBUG (dev, "rx status %d\n", status);
1806 break;
1807 }
1808
1809 if (skb)
1810 dev_kfree_skb_any (skb);
1811 if (!netif_running (dev->net)) {
1812clean:
David Brownell789851c2006-08-21 15:26:38 -07001813 spin_lock(&dev->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 list_add (&req->list, &dev->rx_reqs);
David Brownell789851c2006-08-21 15:26:38 -07001815 spin_unlock(&dev->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 req = NULL;
1817 }
1818 if (req)
1819 rx_submit (dev, req, GFP_ATOMIC);
1820}
1821
1822static int prealloc (struct list_head *list, struct usb_ep *ep,
Al Viro55016f12005-10-21 03:21:58 -04001823 unsigned n, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824{
1825 unsigned i;
1826 struct usb_request *req;
1827
1828 if (!n)
1829 return -ENOMEM;
1830
1831 /* queue/recycle up to N requests */
1832 i = n;
1833 list_for_each_entry (req, list, list) {
1834 if (i-- == 0)
1835 goto extra;
1836 }
1837 while (i--) {
1838 req = usb_ep_alloc_request (ep, gfp_flags);
1839 if (!req)
1840 return list_empty (list) ? -ENOMEM : 0;
1841 list_add (&req->list, list);
1842 }
1843 return 0;
1844
1845extra:
1846 /* free extras */
1847 for (;;) {
1848 struct list_head *next;
1849
1850 next = req->list.next;
1851 list_del (&req->list);
1852 usb_ep_free_request (ep, req);
1853
1854 if (next == list)
1855 break;
1856
1857 req = container_of (next, struct usb_request, list);
1858 }
1859 return 0;
1860}
1861
Al Viro55016f12005-10-21 03:21:58 -04001862static int alloc_requests (struct eth_dev *dev, unsigned n, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863{
1864 int status;
1865
David Brownell789851c2006-08-21 15:26:38 -07001866 spin_lock(&dev->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 status = prealloc (&dev->tx_reqs, dev->in_ep, n, gfp_flags);
1868 if (status < 0)
1869 goto fail;
1870 status = prealloc (&dev->rx_reqs, dev->out_ep, n, gfp_flags);
1871 if (status < 0)
1872 goto fail;
David Brownell789851c2006-08-21 15:26:38 -07001873 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874fail:
1875 DEBUG (dev, "can't alloc requests\n");
David Brownell789851c2006-08-21 15:26:38 -07001876done:
1877 spin_unlock(&dev->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 return status;
1879}
1880
Al Viro55016f12005-10-21 03:21:58 -04001881static void rx_fill (struct eth_dev *dev, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882{
1883 struct usb_request *req;
1884 unsigned long flags;
1885
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 /* fill unused rxq slots with some skb */
David Brownell789851c2006-08-21 15:26:38 -07001887 spin_lock_irqsave(&dev->req_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 while (!list_empty (&dev->rx_reqs)) {
1889 req = container_of (dev->rx_reqs.next,
1890 struct usb_request, list);
1891 list_del_init (&req->list);
David Brownell789851c2006-08-21 15:26:38 -07001892 spin_unlock_irqrestore(&dev->req_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893
1894 if (rx_submit (dev, req, gfp_flags) < 0) {
1895 defer_kevent (dev, WORK_RX_MEMORY);
1896 return;
1897 }
1898
David Brownell789851c2006-08-21 15:26:38 -07001899 spin_lock_irqsave(&dev->req_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 }
David Brownell789851c2006-08-21 15:26:38 -07001901 spin_unlock_irqrestore(&dev->req_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902}
1903
David Howellsc4028952006-11-22 14:57:56 +00001904static void eth_work (struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905{
David Howellsc4028952006-11-22 14:57:56 +00001906 struct eth_dev *dev = container_of(work, struct eth_dev, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907
David Brownell907cba32005-04-28 13:48:09 -07001908 if (test_and_clear_bit (WORK_RX_MEMORY, &dev->todo)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 if (netif_running (dev->net))
1910 rx_fill (dev, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 }
1912
1913 if (dev->todo)
1914 DEBUG (dev, "work done, flags = 0x%lx\n", dev->todo);
1915}
1916
1917static void tx_complete (struct usb_ep *ep, struct usb_request *req)
1918{
1919 struct sk_buff *skb = req->context;
1920 struct eth_dev *dev = ep->driver_data;
1921
1922 switch (req->status) {
1923 default:
1924 dev->stats.tx_errors++;
1925 VDEBUG (dev, "tx err %d\n", req->status);
1926 /* FALLTHROUGH */
1927 case -ECONNRESET: // unlink
1928 case -ESHUTDOWN: // disconnect etc
1929 break;
1930 case 0:
1931 dev->stats.tx_bytes += skb->len;
1932 }
1933 dev->stats.tx_packets++;
1934
David Brownell789851c2006-08-21 15:26:38 -07001935 spin_lock(&dev->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 list_add (&req->list, &dev->tx_reqs);
David Brownell789851c2006-08-21 15:26:38 -07001937 spin_unlock(&dev->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 dev_kfree_skb_any (skb);
1939
1940 atomic_dec (&dev->tx_qlen);
1941 if (netif_carrier_ok (dev->net))
1942 netif_wake_queue (dev->net);
1943}
1944
1945static inline int eth_is_promisc (struct eth_dev *dev)
1946{
1947 /* no filters for the CDC subset; always promisc */
1948 if (subset_active (dev))
1949 return 1;
1950 return dev->cdc_filter & USB_CDC_PACKET_TYPE_PROMISCUOUS;
1951}
1952
1953static int eth_start_xmit (struct sk_buff *skb, struct net_device *net)
1954{
1955 struct eth_dev *dev = netdev_priv(net);
1956 int length = skb->len;
1957 int retval;
1958 struct usb_request *req = NULL;
1959 unsigned long flags;
1960
1961 /* apply outgoing CDC or RNDIS filters */
1962 if (!eth_is_promisc (dev)) {
1963 u8 *dest = skb->data;
1964
David Brownelld8126a02006-11-12 18:09:44 -08001965 if (is_multicast_ether_addr(dest)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 u16 type;
1967
1968 /* ignores USB_CDC_PACKET_TYPE_MULTICAST and host
1969 * SET_ETHERNET_MULTICAST_FILTERS requests
1970 */
David Brownelld8126a02006-11-12 18:09:44 -08001971 if (is_broadcast_ether_addr(dest))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 type = USB_CDC_PACKET_TYPE_BROADCAST;
1973 else
1974 type = USB_CDC_PACKET_TYPE_ALL_MULTICAST;
1975 if (!(dev->cdc_filter & type)) {
1976 dev_kfree_skb_any (skb);
1977 return 0;
1978 }
1979 }
1980 /* ignores USB_CDC_PACKET_TYPE_DIRECTED */
1981 }
1982
David Brownell789851c2006-08-21 15:26:38 -07001983 spin_lock_irqsave(&dev->req_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 req = container_of (dev->tx_reqs.next, struct usb_request, list);
1985 list_del (&req->list);
1986 if (list_empty (&dev->tx_reqs))
1987 netif_stop_queue (net);
David Brownell789851c2006-08-21 15:26:38 -07001988 spin_unlock_irqrestore(&dev->req_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989
1990 /* no buffer copies needed, unless the network stack did it
1991 * or the hardware can't use skb buffers.
1992 * or there's not enough space for any RNDIS headers we need
1993 */
David Brownell45e45ab2005-05-16 08:26:38 -07001994 if (rndis_active(dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 struct sk_buff *skb_rndis;
1996
1997 skb_rndis = skb_realloc_headroom (skb,
1998 sizeof (struct rndis_packet_msg_type));
1999 if (!skb_rndis)
2000 goto drop;
David Brownell7e27f182006-06-13 09:54:40 -07002001
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 dev_kfree_skb_any (skb);
2003 skb = skb_rndis;
2004 rndis_add_hdr (skb);
2005 length = skb->len;
2006 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 req->buf = skb->data;
2008 req->context = skb;
2009 req->complete = tx_complete;
2010
2011 /* use zlp framing on tx for strict CDC-Ether conformance,
2012 * though any robust network rx path ignores extra padding.
2013 * and some hardware doesn't like to write zlps.
2014 */
2015 req->zero = 1;
2016 if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0)
2017 length++;
2018
2019 req->length = length;
2020
2021#ifdef CONFIG_USB_GADGET_DUALSPEED
2022 /* throttle highspeed IRQ rate back slightly */
2023 req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH)
2024 ? ((atomic_read (&dev->tx_qlen) % TX_DELAY) != 0)
2025 : 0;
2026#endif
2027
2028 retval = usb_ep_queue (dev->in_ep, req, GFP_ATOMIC);
2029 switch (retval) {
2030 default:
2031 DEBUG (dev, "tx queue err %d\n", retval);
2032 break;
2033 case 0:
2034 net->trans_start = jiffies;
2035 atomic_inc (&dev->tx_qlen);
2036 }
2037
2038 if (retval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039drop:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 dev->stats.tx_dropped++;
2041 dev_kfree_skb_any (skb);
David Brownell789851c2006-08-21 15:26:38 -07002042 spin_lock_irqsave(&dev->req_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 if (list_empty (&dev->tx_reqs))
2044 netif_start_queue (net);
2045 list_add (&req->list, &dev->tx_reqs);
David Brownell789851c2006-08-21 15:26:38 -07002046 spin_unlock_irqrestore(&dev->req_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 }
2048 return 0;
2049}
2050
2051/*-------------------------------------------------------------------------*/
2052
2053#ifdef CONFIG_USB_ETH_RNDIS
2054
David Brownell907cba32005-04-28 13:48:09 -07002055/* The interrupt endpoint is used in RNDIS to notify the host when messages
2056 * other than data packets are available ... notably the REMOTE_NDIS_*_CMPLT
2057 * messages, but also REMOTE_NDIS_INDICATE_STATUS_MSG and potentially even
2058 * REMOTE_NDIS_KEEPALIVE_MSG.
2059 *
2060 * The RNDIS control queue is processed by GET_ENCAPSULATED_RESPONSE, and
2061 * normally just one notification will be queued.
2062 */
2063
Al Viro55016f12005-10-21 03:21:58 -04002064static struct usb_request *eth_req_alloc (struct usb_ep *, unsigned, gfp_t);
David Brownell907cba32005-04-28 13:48:09 -07002065static void eth_req_free (struct usb_ep *ep, struct usb_request *req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066
2067static void
2068rndis_control_ack_complete (struct usb_ep *ep, struct usb_request *req)
2069{
David Brownell907cba32005-04-28 13:48:09 -07002070 struct eth_dev *dev = ep->driver_data;
2071
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 if (req->status || req->actual != req->length)
David Brownell907cba32005-04-28 13:48:09 -07002073 DEBUG (dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 "rndis control ack complete --> %d, %d/%d\n",
2075 req->status, req->actual, req->length);
David Brownell907cba32005-04-28 13:48:09 -07002076 req->context = NULL;
2077
2078 if (req != dev->stat_req)
2079 eth_req_free(ep, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080}
2081
2082static int rndis_control_ack (struct net_device *net)
2083{
2084 struct eth_dev *dev = netdev_priv(net);
Eric Sesterhenn55359022006-08-21 15:31:05 -07002085 int length;
David Brownell6cdee102005-04-18 17:39:34 -07002086 struct usb_request *resp = dev->stat_req;
David Brownell7e27f182006-06-13 09:54:40 -07002087
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 /* in case RNDIS calls this after disconnect */
David Brownell6cdee102005-04-18 17:39:34 -07002089 if (!dev->status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 DEBUG (dev, "status ENODEV\n");
2091 return -ENODEV;
2092 }
2093
David Brownell907cba32005-04-28 13:48:09 -07002094 /* in case queue length > 1 */
2095 if (resp->context) {
2096 resp = eth_req_alloc (dev->status_ep, 8, GFP_ATOMIC);
2097 if (!resp)
2098 return -ENOMEM;
2099 }
2100
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 /* Send RNDIS RESPONSE_AVAILABLE notification;
2102 * USB_CDC_NOTIFY_RESPONSE_AVAILABLE should work too
2103 */
2104 resp->length = 8;
2105 resp->complete = rndis_control_ack_complete;
David Brownell907cba32005-04-28 13:48:09 -07002106 resp->context = dev;
David Brownell7e27f182006-06-13 09:54:40 -07002107
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 *((__le32 *) resp->buf) = __constant_cpu_to_le32 (1);
2109 *((__le32 *) resp->buf + 1) = __constant_cpu_to_le32 (0);
David Brownell7e27f182006-06-13 09:54:40 -07002110
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 length = usb_ep_queue (dev->status_ep, resp, GFP_ATOMIC);
2112 if (length < 0) {
2113 resp->status = 0;
2114 rndis_control_ack_complete (dev->status_ep, resp);
2115 }
David Brownell7e27f182006-06-13 09:54:40 -07002116
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 return 0;
2118}
2119
David Brownell45e45ab2005-05-16 08:26:38 -07002120#else
2121
2122#define rndis_control_ack NULL
2123
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124#endif /* RNDIS */
2125
Al Viro55016f12005-10-21 03:21:58 -04002126static void eth_start (struct eth_dev *dev, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127{
2128 DEBUG (dev, "%s\n", __FUNCTION__);
2129
2130 /* fill the rx queue */
2131 rx_fill (dev, gfp_flags);
2132
David Brownell7e27f182006-06-13 09:54:40 -07002133 /* and open the tx floodgates */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 atomic_set (&dev->tx_qlen, 0);
2135 netif_wake_queue (dev->net);
David Brownell45e45ab2005-05-16 08:26:38 -07002136 if (rndis_active(dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 rndis_set_param_medium (dev->rndis_config,
2138 NDIS_MEDIUM_802_3,
David Brownell6cdee102005-04-18 17:39:34 -07002139 BITRATE(dev->gadget)/100);
David Brownell907cba32005-04-28 13:48:09 -07002140 (void) rndis_signal_connect (dev->rndis_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142}
2143
2144static int eth_open (struct net_device *net)
2145{
2146 struct eth_dev *dev = netdev_priv(net);
2147
2148 DEBUG (dev, "%s\n", __FUNCTION__);
2149 if (netif_carrier_ok (dev->net))
2150 eth_start (dev, GFP_KERNEL);
2151 return 0;
2152}
2153
2154static int eth_stop (struct net_device *net)
2155{
2156 struct eth_dev *dev = netdev_priv(net);
2157
2158 VDEBUG (dev, "%s\n", __FUNCTION__);
2159 netif_stop_queue (net);
2160
2161 DEBUG (dev, "stop stats: rx/tx %ld/%ld, errs %ld/%ld\n",
David Brownell7e27f182006-06-13 09:54:40 -07002162 dev->stats.rx_packets, dev->stats.tx_packets,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 dev->stats.rx_errors, dev->stats.tx_errors
2164 );
2165
2166 /* ensure there are no more active requests */
2167 if (dev->config) {
2168 usb_ep_disable (dev->in_ep);
2169 usb_ep_disable (dev->out_ep);
2170 if (netif_carrier_ok (dev->net)) {
2171 DEBUG (dev, "host still using in/out endpoints\n");
2172 // FIXME idiom may leave toggle wrong here
2173 usb_ep_enable (dev->in_ep, dev->in);
2174 usb_ep_enable (dev->out_ep, dev->out);
2175 }
2176 if (dev->status_ep) {
2177 usb_ep_disable (dev->status_ep);
2178 usb_ep_enable (dev->status_ep, dev->status);
2179 }
2180 }
David Brownell7e27f182006-06-13 09:54:40 -07002181
David Brownell45e45ab2005-05-16 08:26:38 -07002182 if (rndis_active(dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 rndis_set_param_medium (dev->rndis_config,
2184 NDIS_MEDIUM_802_3, 0);
David Brownell907cba32005-04-28 13:48:09 -07002185 (void) rndis_signal_disconnect (dev->rndis_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187
2188 return 0;
2189}
2190
2191/*-------------------------------------------------------------------------*/
2192
David Brownell907cba32005-04-28 13:48:09 -07002193static struct usb_request *
Al Viro55016f12005-10-21 03:21:58 -04002194eth_req_alloc (struct usb_ep *ep, unsigned size, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195{
2196 struct usb_request *req;
2197
David Brownell907cba32005-04-28 13:48:09 -07002198 req = usb_ep_alloc_request (ep, gfp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 if (!req)
2200 return NULL;
2201
David Brownell907cba32005-04-28 13:48:09 -07002202 req->buf = kmalloc (size, gfp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 if (!req->buf) {
2204 usb_ep_free_request (ep, req);
2205 req = NULL;
2206 }
2207 return req;
2208}
2209
2210static void
2211eth_req_free (struct usb_ep *ep, struct usb_request *req)
2212{
2213 kfree (req->buf);
2214 usb_ep_free_request (ep, req);
2215}
2216
2217
David Brownella3536782006-07-06 15:48:53 -07002218static void /* __init_or_exit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219eth_unbind (struct usb_gadget *gadget)
2220{
2221 struct eth_dev *dev = get_gadget_data (gadget);
2222
2223 DEBUG (dev, "unbind\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 rndis_deregister (dev->rndis_config);
2225 rndis_exit ();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226
2227 /* we've already been disconnected ... no i/o is active */
2228 if (dev->req) {
2229 eth_req_free (gadget->ep0, dev->req);
2230 dev->req = NULL;
2231 }
2232 if (dev->stat_req) {
2233 eth_req_free (dev->status_ep, dev->stat_req);
2234 dev->stat_req = NULL;
2235 }
2236
2237 unregister_netdev (dev->net);
2238 free_netdev(dev->net);
2239
2240 /* assuming we used keventd, it must quiesce too */
2241 flush_scheduled_work ();
2242 set_gadget_data (gadget, NULL);
2243}
2244
David Brownella3536782006-07-06 15:48:53 -07002245static u8 __devinit nibble (unsigned char c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246{
2247 if (likely (isdigit (c)))
2248 return c - '0';
2249 c = toupper (c);
2250 if (likely (isxdigit (c)))
2251 return 10 + c - 'A';
2252 return 0;
2253}
2254
David Brownella3536782006-07-06 15:48:53 -07002255static int __devinit get_ether_addr(const char *str, u8 *dev_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256{
2257 if (str) {
2258 unsigned i;
2259
2260 for (i = 0; i < 6; i++) {
2261 unsigned char num;
2262
2263 if((*str == '.') || (*str == ':'))
2264 str++;
2265 num = nibble(*str++) << 4;
2266 num |= (nibble(*str++));
2267 dev_addr [i] = num;
2268 }
2269 if (is_valid_ether_addr (dev_addr))
Aras Vaichas1afc64a2006-02-18 12:31:23 -08002270 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 }
2272 random_ether_addr(dev_addr);
Aras Vaichas1afc64a2006-02-18 12:31:23 -08002273 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274}
2275
David Brownella3536782006-07-06 15:48:53 -07002276static int __devinit
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277eth_bind (struct usb_gadget *gadget)
2278{
2279 struct eth_dev *dev;
2280 struct net_device *net;
2281 u8 cdc = 1, zlp = 1, rndis = 1;
2282 struct usb_ep *in_ep, *out_ep, *status_ep = NULL;
2283 int status = -ENOMEM;
David Brownell91e79c92005-07-13 15:18:30 -07002284 int gcnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285
2286 /* these flags are only ever cleared; compiler take note */
2287#ifndef DEV_CONFIG_CDC
2288 cdc = 0;
2289#endif
2290#ifndef CONFIG_USB_ETH_RNDIS
2291 rndis = 0;
2292#endif
2293
2294 /* Because most host side USB stacks handle CDC Ethernet, that
2295 * standard protocol is _strongly_ preferred for interop purposes.
2296 * (By everyone except Microsoft.)
2297 */
David Brownell91e79c92005-07-13 15:18:30 -07002298 if (gadget_is_pxa (gadget)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 /* pxa doesn't support altsettings */
2300 cdc = 0;
David Brownell729ed6d2006-08-30 13:24:56 -07002301 } else if (gadget_is_musbhdrc(gadget)) {
2302 /* reduce tx dma overhead by avoiding special cases */
2303 zlp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 } else if (gadget_is_sh(gadget)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 /* sh doesn't support multiple interfaces or configs */
2306 cdc = 0;
2307 rndis = 0;
2308 } else if (gadget_is_sa1100 (gadget)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 /* hardware can't write zlps */
2310 zlp = 0;
2311 /* sa1100 CAN do CDC, without status endpoint ... we use
2312 * non-CDC to be compatible with ARM Linux-2.4 "usb-eth".
2313 */
2314 cdc = 0;
David Brownell91e79c92005-07-13 15:18:30 -07002315 }
2316
2317 gcnum = usb_gadget_controller_number (gadget);
2318 if (gcnum >= 0)
2319 device_desc.bcdDevice = cpu_to_le16 (0x0200 + gcnum);
2320 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 /* can't assume CDC works. don't want to default to
2322 * anything less functional on CDC-capable hardware,
2323 * so we fail in this case.
2324 */
2325 dev_err (&gadget->dev,
2326 "controller '%s' not recognized\n",
2327 gadget->name);
2328 return -ENODEV;
2329 }
2330 snprintf (manufacturer, sizeof manufacturer, "%s %s/%s",
Serge E. Hallyn96b644b2006-10-02 02:18:13 -07002331 init_utsname()->sysname, init_utsname()->release,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 gadget->name);
2333
2334 /* If there's an RNDIS configuration, that's what Windows wants to
2335 * be using ... so use these product IDs here and in the "linux.inf"
2336 * needed to install MSFT drivers. Current Linux kernels will use
2337 * the second configuration if it's CDC Ethernet, and need some help
2338 * to choose the right configuration otherwise.
2339 */
2340 if (rndis) {
2341 device_desc.idVendor =
2342 __constant_cpu_to_le16(RNDIS_VENDOR_NUM);
2343 device_desc.idProduct =
2344 __constant_cpu_to_le16(RNDIS_PRODUCT_NUM);
2345 snprintf (product_desc, sizeof product_desc,
2346 "RNDIS/%s", driver_desc);
2347
2348 /* CDC subset ... recognized by Linux since 2.4.10, but Windows
David Brownell11d54892006-12-11 15:59:04 -08002349 * drivers aren't widely available. (That may be improved by
2350 * supporting one submode of the "SAFE" variant of MDLM.)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 */
2352 } else if (!cdc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 device_desc.idVendor =
2354 __constant_cpu_to_le16(SIMPLE_VENDOR_NUM);
2355 device_desc.idProduct =
2356 __constant_cpu_to_le16(SIMPLE_PRODUCT_NUM);
2357 }
2358
2359 /* support optional vendor/distro customization */
2360 if (idVendor) {
2361 if (!idProduct) {
2362 dev_err (&gadget->dev, "idVendor needs idProduct!\n");
2363 return -ENODEV;
2364 }
2365 device_desc.idVendor = cpu_to_le16(idVendor);
2366 device_desc.idProduct = cpu_to_le16(idProduct);
2367 if (bcdDevice)
2368 device_desc.bcdDevice = cpu_to_le16(bcdDevice);
2369 }
2370 if (iManufacturer)
2371 strlcpy (manufacturer, iManufacturer, sizeof manufacturer);
2372 if (iProduct)
2373 strlcpy (product_desc, iProduct, sizeof product_desc);
Aras Vaichas1afc64a2006-02-18 12:31:23 -08002374 if (iSerialNumber) {
2375 device_desc.iSerialNumber = STRING_SERIALNUMBER,
2376 strlcpy(serial_number, iSerialNumber, sizeof serial_number);
2377 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378
2379 /* all we really need is bulk IN/OUT */
2380 usb_ep_autoconfig_reset (gadget);
2381 in_ep = usb_ep_autoconfig (gadget, &fs_source_desc);
2382 if (!in_ep) {
2383autoconf_fail:
2384 dev_err (&gadget->dev,
2385 "can't autoconfigure on %s\n",
2386 gadget->name);
2387 return -ENODEV;
2388 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 in_ep->driver_data = in_ep; /* claim */
David Brownell7e27f182006-06-13 09:54:40 -07002390
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 out_ep = usb_ep_autoconfig (gadget, &fs_sink_desc);
2392 if (!out_ep)
2393 goto autoconf_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 out_ep->driver_data = out_ep; /* claim */
2395
2396#if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
2397 /* CDC Ethernet control interface doesn't require a status endpoint.
2398 * Since some hosts expect one, try to allocate one anyway.
2399 */
2400 if (cdc || rndis) {
2401 status_ep = usb_ep_autoconfig (gadget, &fs_status_desc);
2402 if (status_ep) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 status_ep->driver_data = status_ep; /* claim */
2404 } else if (rndis) {
2405 dev_err (&gadget->dev,
2406 "can't run RNDIS on %s\n",
2407 gadget->name);
2408 return -ENODEV;
2409#ifdef DEV_CONFIG_CDC
2410 /* pxa25x only does CDC subset; often used with RNDIS */
2411 } else if (cdc) {
2412 control_intf.bNumEndpoints = 0;
2413 /* FIXME remove endpoint from descriptor list */
2414#endif
2415 }
2416 }
2417#endif
2418
2419 /* one config: cdc, else minimal subset */
2420 if (!cdc) {
2421 eth_config.bNumInterfaces = 1;
2422 eth_config.iConfiguration = STRING_SUBSET;
David Brownell11d54892006-12-11 15:59:04 -08002423
2424 /* use functions to set these up, in case we're built to work
2425 * with multiple controllers and must override CDC Ethernet.
2426 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 fs_subset_descriptors();
2428 hs_subset_descriptors();
2429 }
2430
David Brownelle1394b42006-04-02 10:20:43 -08002431 device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
2432 usb_gadget_set_selfpowered (gadget);
2433
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 /* For now RNDIS is always a second config */
2435 if (rndis)
2436 device_desc.bNumConfigurations = 2;
2437
2438#ifdef CONFIG_USB_GADGET_DUALSPEED
2439 if (rndis)
2440 dev_qualifier.bNumConfigurations = 2;
2441 else if (!cdc)
2442 dev_qualifier.bDeviceClass = USB_CLASS_VENDOR_SPEC;
2443
2444 /* assumes ep0 uses the same value for both speeds ... */
2445 dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0;
2446
2447 /* and that all endpoints are dual-speed */
2448 hs_source_desc.bEndpointAddress = fs_source_desc.bEndpointAddress;
2449 hs_sink_desc.bEndpointAddress = fs_sink_desc.bEndpointAddress;
2450#if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
David Brownell907cba32005-04-28 13:48:09 -07002451 if (status_ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 hs_status_desc.bEndpointAddress =
2453 fs_status_desc.bEndpointAddress;
2454#endif
2455#endif /* DUALSPEED */
2456
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 if (gadget->is_otg) {
2458 otg_descriptor.bmAttributes |= USB_OTG_HNP,
2459 eth_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
2460 eth_config.bMaxPower = 4;
2461#ifdef CONFIG_USB_ETH_RNDIS
2462 rndis_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
2463 rndis_config.bMaxPower = 4;
2464#endif
2465 }
2466
David Brownell7e27f182006-06-13 09:54:40 -07002467 net = alloc_etherdev (sizeof *dev);
2468 if (!net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 return status;
2470 dev = netdev_priv(net);
2471 spin_lock_init (&dev->lock);
David Brownell789851c2006-08-21 15:26:38 -07002472 spin_lock_init (&dev->req_lock);
David Howellsc4028952006-11-22 14:57:56 +00002473 INIT_WORK (&dev->work, eth_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 INIT_LIST_HEAD (&dev->tx_reqs);
2475 INIT_LIST_HEAD (&dev->rx_reqs);
2476
2477 /* network device setup */
2478 dev->net = net;
2479 SET_MODULE_OWNER (net);
2480 strcpy (net->name, "usb%d");
2481 dev->cdc = cdc;
2482 dev->zlp = zlp;
2483
2484 dev->in_ep = in_ep;
2485 dev->out_ep = out_ep;
2486 dev->status_ep = status_ep;
2487
2488 /* Module params for these addresses should come from ID proms.
2489 * The host side address is used with CDC and RNDIS, and commonly
David Brownell11d54892006-12-11 15:59:04 -08002490 * ends up in a persistent config database. It's not clear if
2491 * host side code for the SAFE thing cares -- its original BLAN
2492 * thing didn't, Sharp never assigned those addresses on Zaurii.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 */
Aras Vaichas1afc64a2006-02-18 12:31:23 -08002494 if (get_ether_addr(dev_addr, net->dev_addr))
2495 dev_warn(&gadget->dev,
2496 "using random %s ethernet address\n", "self");
David Brownell11d54892006-12-11 15:59:04 -08002497 if (get_ether_addr(host_addr, dev->host_mac))
2498 dev_warn(&gadget->dev,
2499 "using random %s ethernet address\n", "host");
2500 snprintf (ethaddr, sizeof ethaddr, "%02X%02X%02X%02X%02X%02X",
2501 dev->host_mac [0], dev->host_mac [1],
2502 dev->host_mac [2], dev->host_mac [3],
2503 dev->host_mac [4], dev->host_mac [5]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504
2505 if (rndis) {
2506 status = rndis_init();
2507 if (status < 0) {
2508 dev_err (&gadget->dev, "can't init RNDIS, %d\n",
2509 status);
2510 goto fail;
2511 }
2512 }
2513
2514 net->change_mtu = eth_change_mtu;
2515 net->get_stats = eth_get_stats;
2516 net->hard_start_xmit = eth_start_xmit;
2517 net->open = eth_open;
2518 net->stop = eth_stop;
2519 // watchdog_timeo, tx_timeout ...
2520 // set_multicast_list
2521 SET_ETHTOOL_OPS(net, &ops);
2522
2523 /* preallocate control message data and buffer */
David Brownell907cba32005-04-28 13:48:09 -07002524 dev->req = eth_req_alloc (gadget->ep0, USB_BUFSIZ, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 if (!dev->req)
2526 goto fail;
2527 dev->req->complete = eth_setup_complete;
2528
2529 /* ... and maybe likewise for status transfer */
Ian Campbell05f33402005-06-29 10:15:32 +01002530#if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 if (dev->status_ep) {
2532 dev->stat_req = eth_req_alloc (dev->status_ep,
David Brownell907cba32005-04-28 13:48:09 -07002533 STATUS_BYTECOUNT, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 if (!dev->stat_req) {
2535 eth_req_free (gadget->ep0, dev->req);
2536 goto fail;
2537 }
David Brownell907cba32005-04-28 13:48:09 -07002538 dev->stat_req->context = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 }
David Brownell822e14a2005-06-13 06:55:03 -07002540#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541
2542 /* finish hookup to lower layer ... */
2543 dev->gadget = gadget;
2544 set_gadget_data (gadget, dev);
2545 gadget->ep0->driver_data = dev;
David Brownell7e27f182006-06-13 09:54:40 -07002546
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 /* two kinds of host-initiated state changes:
2548 * - iff DATA transfer is active, carrier is "on"
2549 * - tx queueing enabled if open *and* carrier is "on"
2550 */
2551 netif_stop_queue (dev->net);
2552 netif_carrier_off (dev->net);
2553
David Brownell7e27f182006-06-13 09:54:40 -07002554 SET_NETDEV_DEV (dev->net, &gadget->dev);
2555 status = register_netdev (dev->net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 if (status < 0)
2557 goto fail1;
2558
2559 INFO (dev, "%s, version: " DRIVER_VERSION "\n", driver_desc);
2560 INFO (dev, "using %s, OUT %s IN %s%s%s\n", gadget->name,
David Brownell907cba32005-04-28 13:48:09 -07002561 out_ep->name, in_ep->name,
2562 status_ep ? " STATUS " : "",
2563 status_ep ? status_ep->name : ""
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564 );
2565 INFO (dev, "MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
2566 net->dev_addr [0], net->dev_addr [1],
2567 net->dev_addr [2], net->dev_addr [3],
2568 net->dev_addr [4], net->dev_addr [5]);
2569
2570 if (cdc || rndis)
2571 INFO (dev, "HOST MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
2572 dev->host_mac [0], dev->host_mac [1],
2573 dev->host_mac [2], dev->host_mac [3],
2574 dev->host_mac [4], dev->host_mac [5]);
2575
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 if (rndis) {
2577 u32 vendorID = 0;
2578
2579 /* FIXME RNDIS vendor id == "vendor NIC code" == ? */
David Brownell7e27f182006-06-13 09:54:40 -07002580
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 dev->rndis_config = rndis_register (rndis_control_ack);
2582 if (dev->rndis_config < 0) {
2583fail0:
2584 unregister_netdev (dev->net);
2585 status = -ENODEV;
2586 goto fail;
2587 }
David Brownell7e27f182006-06-13 09:54:40 -07002588
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 /* these set up a lot of the OIDs that RNDIS needs */
2590 rndis_set_host_mac (dev->rndis_config, dev->host_mac);
2591 if (rndis_set_param_dev (dev->rndis_config, dev->net,
David Brownell340600a2005-04-28 13:45:25 -07002592 &dev->stats, &dev->cdc_filter))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 goto fail0;
2594 if (rndis_set_param_vendor (dev->rndis_config, vendorID,
2595 manufacturer))
2596 goto fail0;
2597 if (rndis_set_param_medium (dev->rndis_config,
2598 NDIS_MEDIUM_802_3,
2599 0))
2600 goto fail0;
2601 INFO (dev, "RNDIS ready\n");
2602 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603
2604 return status;
2605
2606fail1:
2607 dev_dbg(&gadget->dev, "register_netdev failed, %d\n", status);
2608fail:
2609 eth_unbind (gadget);
2610 return status;
2611}
2612
2613/*-------------------------------------------------------------------------*/
2614
2615static void
2616eth_suspend (struct usb_gadget *gadget)
2617{
2618 struct eth_dev *dev = get_gadget_data (gadget);
2619
2620 DEBUG (dev, "suspend\n");
2621 dev->suspended = 1;
2622}
2623
2624static void
2625eth_resume (struct usb_gadget *gadget)
2626{
2627 struct eth_dev *dev = get_gadget_data (gadget);
2628
2629 DEBUG (dev, "resume\n");
2630 dev->suspended = 0;
2631}
2632
2633/*-------------------------------------------------------------------------*/
2634
2635static struct usb_gadget_driver eth_driver = {
David Brownell907cba32005-04-28 13:48:09 -07002636 .speed = DEVSPEED,
2637
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 .function = (char *) driver_desc,
2639 .bind = eth_bind,
Tony Lindgrenbfb2c962006-06-29 22:27:36 -07002640 .unbind = eth_unbind,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641
2642 .setup = eth_setup,
2643 .disconnect = eth_disconnect,
2644
2645 .suspend = eth_suspend,
2646 .resume = eth_resume,
2647
David Brownell7e27f182006-06-13 09:54:40 -07002648 .driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 .name = (char *) shortname,
Ben Dooksd0d50492005-10-10 10:52:33 +01002650 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651 },
2652};
2653
2654MODULE_DESCRIPTION (DRIVER_DESC);
2655MODULE_AUTHOR ("David Brownell, Benedikt Spanger");
2656MODULE_LICENSE ("GPL");
2657
2658
2659static int __init init (void)
2660{
2661 return usb_gadget_register_driver (&eth_driver);
2662}
2663module_init (init);
2664
2665static void __exit cleanup (void)
2666{
2667 usb_gadget_unregister_driver (&eth_driver);
2668}
2669module_exit (cleanup);
2670