blob: f70055473a00b25b4b83cf873de8301d7264449b [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/errno.h>
32#include <linux/init.h>
33#include <linux/timer.h>
34#include <linux/list.h>
35#include <linux/interrupt.h>
36#include <linux/utsname.h>
37#include <linux/device.h>
38#include <linux/moduleparam.h>
39#include <linux/ctype.h>
40
41#include <asm/byteorder.h>
42#include <asm/io.h>
43#include <asm/irq.h>
44#include <asm/system.h>
45#include <asm/uaccess.h>
46#include <asm/unaligned.h>
47
David Brownell5f848132006-12-16 15:34:53 -080048#include <linux/usb/ch9.h>
David Brownella8c28f22006-06-13 09:57:47 -070049#include <linux/usb/cdc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <linux/usb_gadget.h>
51
52#include <linux/random.h>
53#include <linux/netdevice.h>
54#include <linux/etherdevice.h>
55#include <linux/ethtool.h>
56
57#include "gadget_chips.h"
58
59/*-------------------------------------------------------------------------*/
60
61/*
62 * Ethernet gadget driver -- with CDC and non-CDC options
63 * Builds on hardware support for a full duplex link.
64 *
65 * CDC Ethernet is the standard USB solution for sending Ethernet frames
66 * using USB. Real hardware tends to use the same framing protocol but look
67 * different for control features. This driver strongly prefers to use
68 * this USB-IF standard as its open-systems interoperability solution;
69 * most host side USB stacks (except from Microsoft) support it.
70 *
71 * There's some hardware that can't talk CDC. We make that hardware
72 * implement a "minimalist" vendor-agnostic CDC core: same framing, but
David Brownell11d54892006-12-11 15:59:04 -080073 * link-level setup only requires activating the configuration. Only the
74 * endpoint descriptors, and product/vendor IDs, are relevant; no control
75 * operations are available. Linux supports it, but other host operating
76 * systems may not. (This is a subset of CDC Ethernet.)
77 *
78 * It turns out that if you add a few descriptors to that "CDC Subset",
79 * (Windows) host side drivers from MCCI can treat it as one submode of
80 * a proprietary scheme called "SAFE" ... without needing to know about
81 * specific product/vendor IDs. So we do that, making it easier to use
82 * those MS-Windows drivers. Those added descriptors make it resemble a
83 * CDC MDLM device, but they don't change device behavior at all. (See
84 * MCCI Engineering report 950198 "SAFE Networking Functions".)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 *
86 * A third option is also in use. Rather than CDC Ethernet, or something
87 * simpler, Microsoft pushes their own approach: RNDIS. The published
88 * RNDIS specs are ambiguous and appear to be incomplete, and are also
89 * needlessly complex.
90 */
91
92#define DRIVER_DESC "Ethernet Gadget"
David Brownell907cba32005-04-28 13:48:09 -070093#define DRIVER_VERSION "May Day 2005"
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
95static const char shortname [] = "ether";
96static const char driver_desc [] = DRIVER_DESC;
97
98#define RX_EXTRA 20 /* guard against rx overflows */
99
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100#include "rndis.h"
David Brownell45e45ab2005-05-16 08:26:38 -0700101
102#ifndef CONFIG_USB_ETH_RNDIS
103#define rndis_uninit(x) do{}while(0)
104#define rndis_deregister(c) do{}while(0)
105#define rndis_exit() do{}while(0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106#endif
107
108/* CDC and RNDIS support the same host-chosen outgoing packet filters. */
109#define DEFAULT_FILTER (USB_CDC_PACKET_TYPE_BROADCAST \
David Brownell7e27f182006-06-13 09:54:40 -0700110 |USB_CDC_PACKET_TYPE_ALL_MULTICAST \
111 |USB_CDC_PACKET_TYPE_PROMISCUOUS \
112 |USB_CDC_PACKET_TYPE_DIRECTED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
114
115/*-------------------------------------------------------------------------*/
116
117struct eth_dev {
118 spinlock_t lock;
119 struct usb_gadget *gadget;
120 struct usb_request *req; /* for control responses */
121 struct usb_request *stat_req; /* for cdc & rndis status */
122
123 u8 config;
124 struct usb_ep *in_ep, *out_ep, *status_ep;
125 const struct usb_endpoint_descriptor
126 *in, *out, *status;
David Brownell789851c2006-08-21 15:26:38 -0700127
128 spinlock_t req_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 struct list_head tx_reqs, rx_reqs;
130
131 struct net_device *net;
132 struct net_device_stats stats;
133 atomic_t tx_qlen;
134
135 struct work_struct work;
136 unsigned zlp:1;
137 unsigned cdc:1;
138 unsigned rndis:1;
139 unsigned suspended:1;
140 u16 cdc_filter;
141 unsigned long todo;
142#define WORK_RX_MEMORY 0
143 int rndis_config;
144 u8 host_mac [ETH_ALEN];
145};
146
147/* This version autoconfigures as much as possible at run-time.
148 *
149 * It also ASSUMES a self-powered device, without remote wakeup,
150 * although remote wakeup support would make sense.
151 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
153/*-------------------------------------------------------------------------*/
154
155/* DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
156 * Instead: allocate your own, using normal USB-IF procedures.
157 */
158
159/* Thanks to NetChip Technologies for donating this product ID.
160 * It's for devices with only CDC Ethernet configurations.
161 */
162#define CDC_VENDOR_NUM 0x0525 /* NetChip */
163#define CDC_PRODUCT_NUM 0xa4a1 /* Linux-USB Ethernet Gadget */
164
165/* For hardware that can't talk CDC, we use the same vendor ID that
166 * ARM Linux has used for ethernet-over-usb, both with sa1100 and
167 * with pxa250. We're protocol-compatible, if the host-side drivers
168 * use the endpoint descriptors. bcdDevice (version) is nonzero, so
169 * drivers that need to hard-wire endpoint numbers have a hook.
170 *
171 * The protocol is a minimal subset of CDC Ether, which works on any bulk
172 * hardware that's not deeply broken ... even on hardware that can't talk
173 * RNDIS (like SA-1100, with no interrupt endpoint, or anything that
174 * doesn't handle control-OUT).
175 */
176#define SIMPLE_VENDOR_NUM 0x049f
177#define SIMPLE_PRODUCT_NUM 0x505a
178
179/* For hardware that can talk RNDIS and either of the above protocols,
180 * use this ID ... the windows INF files will know it. Unless it's
181 * used with CDC Ethernet, Linux 2.4 hosts will need updates to choose
182 * the non-RNDIS configuration.
183 */
184#define RNDIS_VENDOR_NUM 0x0525 /* NetChip */
185#define RNDIS_PRODUCT_NUM 0xa4a2 /* Ethernet/RNDIS Gadget */
186
187
188/* Some systems will want different product identifers published in the
189 * device descriptor, either numbers or strings or both. These string
190 * parameters are in UTF-8 (superset of ASCII's 7 bit characters).
191 */
192
Aras Vaichas1afc64a2006-02-18 12:31:23 -0800193static ushort idVendor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194module_param(idVendor, ushort, S_IRUGO);
195MODULE_PARM_DESC(idVendor, "USB Vendor ID");
196
Aras Vaichas1afc64a2006-02-18 12:31:23 -0800197static ushort idProduct;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198module_param(idProduct, ushort, S_IRUGO);
199MODULE_PARM_DESC(idProduct, "USB Product ID");
200
Aras Vaichas1afc64a2006-02-18 12:31:23 -0800201static ushort bcdDevice;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202module_param(bcdDevice, ushort, S_IRUGO);
203MODULE_PARM_DESC(bcdDevice, "USB Device version (BCD)");
204
Aras Vaichas1afc64a2006-02-18 12:31:23 -0800205static char *iManufacturer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206module_param(iManufacturer, charp, S_IRUGO);
207MODULE_PARM_DESC(iManufacturer, "USB Manufacturer string");
208
Aras Vaichas1afc64a2006-02-18 12:31:23 -0800209static char *iProduct;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210module_param(iProduct, charp, S_IRUGO);
211MODULE_PARM_DESC(iProduct, "USB Product string");
212
Aras Vaichas1afc64a2006-02-18 12:31:23 -0800213static char *iSerialNumber;
214module_param(iSerialNumber, charp, S_IRUGO);
215MODULE_PARM_DESC(iSerialNumber, "SerialNumber");
216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217/* initial value, changed by "ifconfig usb0 hw ether xx:xx:xx:xx:xx:xx" */
Aras Vaichas1afc64a2006-02-18 12:31:23 -0800218static char *dev_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219module_param(dev_addr, charp, S_IRUGO);
220MODULE_PARM_DESC(dev_addr, "Device Ethernet Address");
221
222/* this address is invisible to ifconfig */
Aras Vaichas1afc64a2006-02-18 12:31:23 -0800223static char *host_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224module_param(host_addr, charp, S_IRUGO);
225MODULE_PARM_DESC(host_addr, "Host Ethernet Address");
226
227
228/*-------------------------------------------------------------------------*/
229
230/* Include CDC support if we could run on CDC-capable hardware. */
231
232#ifdef CONFIG_USB_GADGET_NET2280
233#define DEV_CONFIG_CDC
234#endif
235
236#ifdef CONFIG_USB_GADGET_DUMMY_HCD
237#define DEV_CONFIG_CDC
238#endif
239
240#ifdef CONFIG_USB_GADGET_GOKU
241#define DEV_CONFIG_CDC
242#endif
243
244#ifdef CONFIG_USB_GADGET_LH7A40X
245#define DEV_CONFIG_CDC
246#endif
247
248#ifdef CONFIG_USB_GADGET_MQ11XX
249#define DEV_CONFIG_CDC
250#endif
251
252#ifdef CONFIG_USB_GADGET_OMAP
253#define DEV_CONFIG_CDC
254#endif
255
256#ifdef CONFIG_USB_GADGET_N9604
257#define DEV_CONFIG_CDC
258#endif
259
260#ifdef CONFIG_USB_GADGET_PXA27X
261#define DEV_CONFIG_CDC
262#endif
263
David Brownell11d54892006-12-11 15:59:04 -0800264#ifdef CONFIG_USB_GADGET_S3C2410
265#define DEV_CONFIG_CDC
266#endif
267
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268#ifdef CONFIG_USB_GADGET_AT91
269#define DEV_CONFIG_CDC
270#endif
271
David Brownell1c05ad42006-01-25 08:45:59 -0800272#ifdef CONFIG_USB_GADGET_MUSBHSFC
273#define DEV_CONFIG_CDC
274#endif
275
Tony Lindgrenbfb2c962006-06-29 22:27:36 -0700276#ifdef CONFIG_USB_GADGET_MUSB_HDRC
David Brownell1c05ad42006-01-25 08:45:59 -0800277#define DEV_CONFIG_CDC
278#endif
279
Haavard Skinnemoen55b3fd42007-06-14 18:01:45 +0200280#ifdef CONFIG_USB_GADGET_ATMEL_USBA
HÃ¥vard Skinnemoenef3ff462006-02-27 18:15:04 +0100281#define DEV_CONFIG_CDC
282#endif
283
Li Yangd2eef1f2007-04-23 10:37:36 -0700284#ifdef CONFIG_USB_GADGET_FSL_USB2
285#define DEV_CONFIG_CDC
286#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
288/* For CDC-incapable hardware, choose the simple cdc subset.
289 * Anything that talks bulk (without notable bugs) can do this.
290 */
291#ifdef CONFIG_USB_GADGET_PXA2XX
292#define DEV_CONFIG_SUBSET
293#endif
294
David Brownell7f9985c2007-05-08 21:01:30 -0700295#ifdef CONFIG_USB_GADGET_SUPERH
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296#define DEV_CONFIG_SUBSET
297#endif
298
299#ifdef CONFIG_USB_GADGET_SA1100
300/* use non-CDC for backwards compatibility */
301#define DEV_CONFIG_SUBSET
302#endif
303
Yoshihiro Shimoda4cf25032007-05-10 13:18:23 +0900304#ifdef CONFIG_USB_GADGET_M66592
305#define DEV_CONFIG_CDC
306#endif
307
Thomas Dahlmann55d402d2007-07-16 21:40:54 -0700308#ifdef CONFIG_USB_GADGET_AMD5536UDC
309#define DEV_CONFIG_CDC
310#endif
311
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
313/*-------------------------------------------------------------------------*/
314
315/* "main" config is either CDC, or its simple subset */
316static inline int is_cdc(struct eth_dev *dev)
317{
318#if !defined(DEV_CONFIG_SUBSET)
319 return 1; /* only cdc possible */
320#elif !defined (DEV_CONFIG_CDC)
321 return 0; /* only subset possible */
322#else
323 return dev->cdc; /* depends on what hardware we found */
324#endif
325}
326
327/* "secondary" RNDIS config may sometimes be activated */
328static inline int rndis_active(struct eth_dev *dev)
329{
330#ifdef CONFIG_USB_ETH_RNDIS
331 return dev->rndis;
332#else
333 return 0;
334#endif
335}
336
337#define subset_active(dev) (!is_cdc(dev) && !rndis_active(dev))
338#define cdc_active(dev) ( is_cdc(dev) && !rndis_active(dev))
339
340
341
342#define DEFAULT_QLEN 2 /* double buffering by default */
343
344/* peak bulk transfer bits-per-second */
David Brownell7e27f182006-06-13 09:54:40 -0700345#define HS_BPS (13 * 512 * 8 * 1000 * 8)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346#define FS_BPS (19 * 64 * 1 * 1000 * 8)
347
348#ifdef CONFIG_USB_GADGET_DUALSPEED
David Brownell907cba32005-04-28 13:48:09 -0700349#define DEVSPEED USB_SPEED_HIGH
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
351static unsigned qmult = 5;
352module_param (qmult, uint, S_IRUGO|S_IWUSR);
353
354
355/* for dual-speed hardware, use deeper queues at highspeed */
356#define qlen(gadget) \
357 (DEFAULT_QLEN*((gadget->speed == USB_SPEED_HIGH) ? qmult : 1))
358
359/* also defer IRQs on highspeed TX */
360#define TX_DELAY qmult
361
David Brownell6cdee102005-04-18 17:39:34 -0700362static inline int BITRATE(struct usb_gadget *g)
363{
364 return (g->speed == USB_SPEED_HIGH) ? HS_BPS : FS_BPS;
365}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
367#else /* full speed (low speed doesn't do bulk) */
David Brownell907cba32005-04-28 13:48:09 -0700368#define DEVSPEED USB_SPEED_FULL
369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370#define qlen(gadget) DEFAULT_QLEN
371
David Brownell6cdee102005-04-18 17:39:34 -0700372static inline int BITRATE(struct usb_gadget *g)
373{
374 return FS_BPS;
375}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376#endif
377
378
379/*-------------------------------------------------------------------------*/
380
381#define xprintk(d,level,fmt,args...) \
382 printk(level "%s: " fmt , (d)->net->name , ## args)
383
384#ifdef DEBUG
385#undef DEBUG
386#define DEBUG(dev,fmt,args...) \
387 xprintk(dev , KERN_DEBUG , fmt , ## args)
388#else
389#define DEBUG(dev,fmt,args...) \
390 do { } while (0)
391#endif /* DEBUG */
392
393#ifdef VERBOSE
394#define VDEBUG DEBUG
395#else
396#define VDEBUG(dev,fmt,args...) \
397 do { } while (0)
398#endif /* DEBUG */
399
400#define ERROR(dev,fmt,args...) \
401 xprintk(dev , KERN_ERR , fmt , ## args)
402#define WARN(dev,fmt,args...) \
403 xprintk(dev , KERN_WARNING , fmt , ## args)
404#define INFO(dev,fmt,args...) \
405 xprintk(dev , KERN_INFO , fmt , ## args)
406
407/*-------------------------------------------------------------------------*/
408
409/* USB DRIVER HOOKUP (to the hardware driver, below us), mostly
410 * ep0 implementation: descriptors, config management, setup().
411 * also optional class-specific notification interrupt transfer.
412 */
413
414/*
415 * DESCRIPTORS ... most are static, but strings and (full) configuration
416 * descriptors are built on demand. For now we do either full CDC, or
417 * our simple subset, with RNDIS as an optional second configuration.
418 *
419 * RNDIS includes some CDC ACM descriptors ... like CDC Ethernet. But
420 * the class descriptors match a modem (they're ignored; it's really just
421 * Ethernet functionality), they don't need the NOP altsetting, and the
422 * status transfer endpoint isn't optional.
423 */
424
425#define STRING_MANUFACTURER 1
426#define STRING_PRODUCT 2
427#define STRING_ETHADDR 3
428#define STRING_DATA 4
429#define STRING_CONTROL 5
430#define STRING_RNDIS_CONTROL 6
431#define STRING_CDC 7
432#define STRING_SUBSET 8
433#define STRING_RNDIS 9
Aras Vaichas1afc64a2006-02-18 12:31:23 -0800434#define STRING_SERIALNUMBER 10
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
David Brownell340600a2005-04-28 13:45:25 -0700436/* holds our biggest descriptor (or RNDIS response) */
437#define USB_BUFSIZ 256
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
439/*
440 * This device advertises one configuration, eth_config, unless RNDIS
441 * is enabled (rndis_config) on hardware supporting at least two configs.
442 *
443 * NOTE: Controllers like superh_udc should probably be able to use
444 * an RNDIS-only configuration.
445 *
446 * FIXME define some higher-powered configurations to make it easier
447 * to recharge batteries ...
448 */
449
450#define DEV_CONFIG_VALUE 1 /* cdc or subset */
451#define DEV_RNDIS_CONFIG_VALUE 2 /* rndis; optional */
452
453static struct usb_device_descriptor
454device_desc = {
455 .bLength = sizeof device_desc,
456 .bDescriptorType = USB_DT_DEVICE,
457
458 .bcdUSB = __constant_cpu_to_le16 (0x0200),
459
460 .bDeviceClass = USB_CLASS_COMM,
461 .bDeviceSubClass = 0,
462 .bDeviceProtocol = 0,
463
464 .idVendor = __constant_cpu_to_le16 (CDC_VENDOR_NUM),
465 .idProduct = __constant_cpu_to_le16 (CDC_PRODUCT_NUM),
466 .iManufacturer = STRING_MANUFACTURER,
467 .iProduct = STRING_PRODUCT,
468 .bNumConfigurations = 1,
469};
470
471static struct usb_otg_descriptor
472otg_descriptor = {
473 .bLength = sizeof otg_descriptor,
474 .bDescriptorType = USB_DT_OTG,
475
476 .bmAttributes = USB_OTG_SRP,
477};
478
479static struct usb_config_descriptor
480eth_config = {
481 .bLength = sizeof eth_config,
482 .bDescriptorType = USB_DT_CONFIG,
483
484 /* compute wTotalLength on the fly */
485 .bNumInterfaces = 2,
486 .bConfigurationValue = DEV_CONFIG_VALUE,
487 .iConfiguration = STRING_CDC,
488 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
489 .bMaxPower = 50,
490};
491
492#ifdef CONFIG_USB_ETH_RNDIS
David Brownell7e27f182006-06-13 09:54:40 -0700493static struct usb_config_descriptor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494rndis_config = {
495 .bLength = sizeof rndis_config,
496 .bDescriptorType = USB_DT_CONFIG,
497
498 /* compute wTotalLength on the fly */
499 .bNumInterfaces = 2,
500 .bConfigurationValue = DEV_RNDIS_CONFIG_VALUE,
501 .iConfiguration = STRING_RNDIS,
502 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
503 .bMaxPower = 50,
504};
505#endif
506
507/*
508 * Compared to the simple CDC subset, the full CDC Ethernet model adds
509 * three class descriptors, two interface descriptors, optional status
510 * endpoint. Both have a "data" interface and two bulk endpoints.
511 * There are also differences in how control requests are handled.
512 *
David Brownell11d54892006-12-11 15:59:04 -0800513 * RNDIS shares a lot with CDC-Ethernet, since it's a variant of the
514 * CDC-ACM (modem) spec. Unfortunately MSFT's RNDIS driver is buggy; it
515 * may hang or oops. Since bugfixes (or accurate specs, letting Linux
516 * work around those bugs) are unlikely to ever come from MSFT, you may
517 * wish to avoid using RNDIS.
518 *
519 * MCCI offers an alternative to RNDIS if you need to connect to Windows
520 * but have hardware that can't support CDC Ethernet. We add descriptors
521 * to present the CDC Subset as a (nonconformant) CDC MDLM variant called
522 * "SAFE". That borrows from both CDC Ethernet and CDC MDLM. You can
523 * get those drivers from MCCI, or bundled with various products.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 */
525
526#ifdef DEV_CONFIG_CDC
527static struct usb_interface_descriptor
528control_intf = {
529 .bLength = sizeof control_intf,
530 .bDescriptorType = USB_DT_INTERFACE,
531
532 .bInterfaceNumber = 0,
533 /* status endpoint is optional; this may be patched later */
534 .bNumEndpoints = 1,
535 .bInterfaceClass = USB_CLASS_COMM,
536 .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET,
537 .bInterfaceProtocol = USB_CDC_PROTO_NONE,
538 .iInterface = STRING_CONTROL,
539};
540#endif
541
542#ifdef CONFIG_USB_ETH_RNDIS
543static const struct usb_interface_descriptor
544rndis_control_intf = {
545 .bLength = sizeof rndis_control_intf,
546 .bDescriptorType = USB_DT_INTERFACE,
David Brownell7e27f182006-06-13 09:54:40 -0700547
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 .bInterfaceNumber = 0,
549 .bNumEndpoints = 1,
550 .bInterfaceClass = USB_CLASS_COMM,
551 .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
552 .bInterfaceProtocol = USB_CDC_ACM_PROTO_VENDOR,
553 .iInterface = STRING_RNDIS_CONTROL,
554};
555#endif
556
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557static const struct usb_cdc_header_desc header_desc = {
558 .bLength = sizeof header_desc,
559 .bDescriptorType = USB_DT_CS_INTERFACE,
560 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
561
562 .bcdCDC = __constant_cpu_to_le16 (0x0110),
563};
564
David Brownell11d54892006-12-11 15:59:04 -0800565#if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
566
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567static const struct usb_cdc_union_desc union_desc = {
568 .bLength = sizeof union_desc,
569 .bDescriptorType = USB_DT_CS_INTERFACE,
570 .bDescriptorSubType = USB_CDC_UNION_TYPE,
571
572 .bMasterInterface0 = 0, /* index of control interface */
573 .bSlaveInterface0 = 1, /* index of DATA interface */
574};
575
576#endif /* CDC || RNDIS */
577
578#ifdef CONFIG_USB_ETH_RNDIS
579
580static const struct usb_cdc_call_mgmt_descriptor call_mgmt_descriptor = {
David Brownell7e27f182006-06-13 09:54:40 -0700581 .bLength = sizeof call_mgmt_descriptor,
582 .bDescriptorType = USB_DT_CS_INTERFACE,
583 .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
David Brownell7e27f182006-06-13 09:54:40 -0700585 .bmCapabilities = 0x00,
586 .bDataInterface = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587};
588
David Brownell907cba32005-04-28 13:48:09 -0700589static const struct usb_cdc_acm_descriptor acm_descriptor = {
David Brownell7e27f182006-06-13 09:54:40 -0700590 .bLength = sizeof acm_descriptor,
591 .bDescriptorType = USB_DT_CS_INTERFACE,
592 .bDescriptorSubType = USB_CDC_ACM_TYPE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
David Brownell7e27f182006-06-13 09:54:40 -0700594 .bmCapabilities = 0x00,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595};
596
597#endif
598
David Brownell11d54892006-12-11 15:59:04 -0800599#ifndef DEV_CONFIG_CDC
600
601/* "SAFE" loosely follows CDC WMC MDLM, violating the spec in various
602 * ways: data endpoints live in the control interface, there's no data
603 * interface, and it's not used to talk to a cell phone radio.
604 */
605
606static const struct usb_cdc_mdlm_desc mdlm_desc = {
607 .bLength = sizeof mdlm_desc,
608 .bDescriptorType = USB_DT_CS_INTERFACE,
609 .bDescriptorSubType = USB_CDC_MDLM_TYPE,
610
611 .bcdVersion = __constant_cpu_to_le16(0x0100),
612 .bGUID = {
613 0x5d, 0x34, 0xcf, 0x66, 0x11, 0x18, 0x11, 0xd6,
614 0xa2, 0x1a, 0x00, 0x01, 0x02, 0xca, 0x9a, 0x7f,
615 },
616};
617
618/* since "usb_cdc_mdlm_detail_desc" is a variable length structure, we
619 * can't really use its struct. All we do here is say that we're using
620 * the submode of "SAFE" which directly matches the CDC Subset.
621 */
622static const u8 mdlm_detail_desc[] = {
623 6,
624 USB_DT_CS_INTERFACE,
625 USB_CDC_MDLM_DETAIL_TYPE,
626
627 0, /* "SAFE" */
628 0, /* network control capabilities (none) */
629 0, /* network data capabilities ("raw" encapsulation) */
630};
631
632#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
634static const struct usb_cdc_ether_desc ether_desc = {
635 .bLength = sizeof ether_desc,
636 .bDescriptorType = USB_DT_CS_INTERFACE,
637 .bDescriptorSubType = USB_CDC_ETHERNET_TYPE,
638
639 /* this descriptor actually adds value, surprise! */
640 .iMACAddress = STRING_ETHADDR,
641 .bmEthernetStatistics = __constant_cpu_to_le32 (0), /* no statistics */
642 .wMaxSegmentSize = __constant_cpu_to_le16 (ETH_FRAME_LEN),
643 .wNumberMCFilters = __constant_cpu_to_le16 (0),
644 .bNumberPowerFilters = 0,
645};
646
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
648#if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
649
650/* include the status endpoint if we can, even where it's optional.
651 * use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one
Steven Cole093cf722005-05-03 19:07:24 -0600652 * packet, to simplify cancellation; and a big transfer interval, to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 * waste less bandwidth.
654 *
655 * some drivers (like Linux 2.4 cdc-ether!) "need" it to exist even
656 * if they ignore the connect/disconnect notifications that real aether
657 * can provide. more advanced cdc configurations might want to support
658 * encapsulated commands (vendor-specific, using control-OUT).
659 *
660 * RNDIS requires the status endpoint, since it uses that encapsulation
661 * mechanism for its funky RPC scheme.
662 */
David Brownell7e27f182006-06-13 09:54:40 -0700663
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664#define LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */
665#define STATUS_BYTECOUNT 16 /* 8 byte header + data */
666
667static struct usb_endpoint_descriptor
668fs_status_desc = {
669 .bLength = USB_DT_ENDPOINT_SIZE,
670 .bDescriptorType = USB_DT_ENDPOINT,
671
672 .bEndpointAddress = USB_DIR_IN,
673 .bmAttributes = USB_ENDPOINT_XFER_INT,
674 .wMaxPacketSize = __constant_cpu_to_le16 (STATUS_BYTECOUNT),
675 .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC,
676};
677#endif
678
679#ifdef DEV_CONFIG_CDC
680
681/* the default data interface has no endpoints ... */
682
683static const struct usb_interface_descriptor
684data_nop_intf = {
685 .bLength = sizeof data_nop_intf,
686 .bDescriptorType = USB_DT_INTERFACE,
687
688 .bInterfaceNumber = 1,
689 .bAlternateSetting = 0,
690 .bNumEndpoints = 0,
691 .bInterfaceClass = USB_CLASS_CDC_DATA,
692 .bInterfaceSubClass = 0,
693 .bInterfaceProtocol = 0,
694};
695
696/* ... but the "real" data interface has two bulk endpoints */
697
698static const struct usb_interface_descriptor
699data_intf = {
700 .bLength = sizeof data_intf,
701 .bDescriptorType = USB_DT_INTERFACE,
702
703 .bInterfaceNumber = 1,
704 .bAlternateSetting = 1,
705 .bNumEndpoints = 2,
706 .bInterfaceClass = USB_CLASS_CDC_DATA,
707 .bInterfaceSubClass = 0,
708 .bInterfaceProtocol = 0,
709 .iInterface = STRING_DATA,
710};
711
712#endif
713
714#ifdef CONFIG_USB_ETH_RNDIS
715
716/* RNDIS doesn't activate by changing to the "real" altsetting */
717
718static const struct usb_interface_descriptor
719rndis_data_intf = {
720 .bLength = sizeof rndis_data_intf,
721 .bDescriptorType = USB_DT_INTERFACE,
722
723 .bInterfaceNumber = 1,
724 .bAlternateSetting = 0,
725 .bNumEndpoints = 2,
726 .bInterfaceClass = USB_CLASS_CDC_DATA,
727 .bInterfaceSubClass = 0,
728 .bInterfaceProtocol = 0,
729 .iInterface = STRING_DATA,
730};
731
732#endif
733
734#ifdef DEV_CONFIG_SUBSET
735
736/*
737 * "Simple" CDC-subset option is a simple vendor-neutral model that most
738 * full speed controllers can handle: one interface, two bulk endpoints.
David Brownell11d54892006-12-11 15:59:04 -0800739 *
740 * To assist host side drivers, we fancy it up a bit, and add descriptors
741 * so some host side drivers will understand it as a "SAFE" variant.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 */
743
744static const struct usb_interface_descriptor
745subset_data_intf = {
746 .bLength = sizeof subset_data_intf,
747 .bDescriptorType = USB_DT_INTERFACE,
748
749 .bInterfaceNumber = 0,
750 .bAlternateSetting = 0,
751 .bNumEndpoints = 2,
David Brownell11d54892006-12-11 15:59:04 -0800752 .bInterfaceClass = USB_CLASS_COMM,
753 .bInterfaceSubClass = USB_CDC_SUBCLASS_MDLM,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 .bInterfaceProtocol = 0,
755 .iInterface = STRING_DATA,
756};
757
758#endif /* SUBSET */
759
760
761static struct usb_endpoint_descriptor
762fs_source_desc = {
763 .bLength = USB_DT_ENDPOINT_SIZE,
764 .bDescriptorType = USB_DT_ENDPOINT,
765
766 .bEndpointAddress = USB_DIR_IN,
767 .bmAttributes = USB_ENDPOINT_XFER_BULK,
768};
769
770static struct usb_endpoint_descriptor
771fs_sink_desc = {
772 .bLength = USB_DT_ENDPOINT_SIZE,
773 .bDescriptorType = USB_DT_ENDPOINT,
774
775 .bEndpointAddress = USB_DIR_OUT,
776 .bmAttributes = USB_ENDPOINT_XFER_BULK,
777};
778
779static const struct usb_descriptor_header *fs_eth_function [11] = {
780 (struct usb_descriptor_header *) &otg_descriptor,
781#ifdef DEV_CONFIG_CDC
782 /* "cdc" mode descriptors */
783 (struct usb_descriptor_header *) &control_intf,
784 (struct usb_descriptor_header *) &header_desc,
785 (struct usb_descriptor_header *) &union_desc,
786 (struct usb_descriptor_header *) &ether_desc,
787 /* NOTE: status endpoint may need to be removed */
788 (struct usb_descriptor_header *) &fs_status_desc,
789 /* data interface, with altsetting */
790 (struct usb_descriptor_header *) &data_nop_intf,
791 (struct usb_descriptor_header *) &data_intf,
792 (struct usb_descriptor_header *) &fs_source_desc,
793 (struct usb_descriptor_header *) &fs_sink_desc,
794 NULL,
795#endif /* DEV_CONFIG_CDC */
796};
797
798static inline void __init fs_subset_descriptors(void)
799{
800#ifdef DEV_CONFIG_SUBSET
David Brownell11d54892006-12-11 15:59:04 -0800801 /* behavior is "CDC Subset"; extra descriptors say "SAFE" */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 fs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
David Brownell11d54892006-12-11 15:59:04 -0800803 fs_eth_function[2] = (struct usb_descriptor_header *) &header_desc;
804 fs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc;
805 fs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc;
806 fs_eth_function[5] = (struct usb_descriptor_header *) &ether_desc;
807 fs_eth_function[6] = (struct usb_descriptor_header *) &fs_source_desc;
808 fs_eth_function[7] = (struct usb_descriptor_header *) &fs_sink_desc;
809 fs_eth_function[8] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810#else
811 fs_eth_function[1] = NULL;
812#endif
813}
814
815#ifdef CONFIG_USB_ETH_RNDIS
816static const struct usb_descriptor_header *fs_rndis_function [] = {
817 (struct usb_descriptor_header *) &otg_descriptor,
818 /* control interface matches ACM, not Ethernet */
819 (struct usb_descriptor_header *) &rndis_control_intf,
820 (struct usb_descriptor_header *) &header_desc,
821 (struct usb_descriptor_header *) &call_mgmt_descriptor,
822 (struct usb_descriptor_header *) &acm_descriptor,
823 (struct usb_descriptor_header *) &union_desc,
824 (struct usb_descriptor_header *) &fs_status_desc,
825 /* data interface has no altsetting */
826 (struct usb_descriptor_header *) &rndis_data_intf,
827 (struct usb_descriptor_header *) &fs_source_desc,
828 (struct usb_descriptor_header *) &fs_sink_desc,
829 NULL,
830};
831#endif
832
833#ifdef CONFIG_USB_GADGET_DUALSPEED
834
835/*
836 * usb 2.0 devices need to expose both high speed and full speed
837 * descriptors, unless they only run at full speed.
838 */
839
840#if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
841static struct usb_endpoint_descriptor
842hs_status_desc = {
843 .bLength = USB_DT_ENDPOINT_SIZE,
844 .bDescriptorType = USB_DT_ENDPOINT,
845
846 .bmAttributes = USB_ENDPOINT_XFER_INT,
847 .wMaxPacketSize = __constant_cpu_to_le16 (STATUS_BYTECOUNT),
848 .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4,
849};
850#endif /* DEV_CONFIG_CDC */
851
852static struct usb_endpoint_descriptor
853hs_source_desc = {
854 .bLength = USB_DT_ENDPOINT_SIZE,
855 .bDescriptorType = USB_DT_ENDPOINT,
856
857 .bmAttributes = USB_ENDPOINT_XFER_BULK,
858 .wMaxPacketSize = __constant_cpu_to_le16 (512),
859};
860
861static struct usb_endpoint_descriptor
862hs_sink_desc = {
863 .bLength = USB_DT_ENDPOINT_SIZE,
864 .bDescriptorType = USB_DT_ENDPOINT,
865
866 .bmAttributes = USB_ENDPOINT_XFER_BULK,
867 .wMaxPacketSize = __constant_cpu_to_le16 (512),
868};
869
870static struct usb_qualifier_descriptor
871dev_qualifier = {
872 .bLength = sizeof dev_qualifier,
873 .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
874
875 .bcdUSB = __constant_cpu_to_le16 (0x0200),
876 .bDeviceClass = USB_CLASS_COMM,
877
878 .bNumConfigurations = 1,
879};
880
881static const struct usb_descriptor_header *hs_eth_function [11] = {
882 (struct usb_descriptor_header *) &otg_descriptor,
883#ifdef DEV_CONFIG_CDC
884 /* "cdc" mode descriptors */
885 (struct usb_descriptor_header *) &control_intf,
886 (struct usb_descriptor_header *) &header_desc,
887 (struct usb_descriptor_header *) &union_desc,
888 (struct usb_descriptor_header *) &ether_desc,
889 /* NOTE: status endpoint may need to be removed */
890 (struct usb_descriptor_header *) &hs_status_desc,
891 /* data interface, with altsetting */
892 (struct usb_descriptor_header *) &data_nop_intf,
893 (struct usb_descriptor_header *) &data_intf,
894 (struct usb_descriptor_header *) &hs_source_desc,
895 (struct usb_descriptor_header *) &hs_sink_desc,
896 NULL,
897#endif /* DEV_CONFIG_CDC */
898};
899
900static inline void __init hs_subset_descriptors(void)
901{
902#ifdef DEV_CONFIG_SUBSET
David Brownell11d54892006-12-11 15:59:04 -0800903 /* behavior is "CDC Subset"; extra descriptors say "SAFE" */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 hs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
David Brownell11d54892006-12-11 15:59:04 -0800905 hs_eth_function[2] = (struct usb_descriptor_header *) &header_desc;
906 hs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc;
907 hs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc;
908 hs_eth_function[5] = (struct usb_descriptor_header *) &ether_desc;
909 hs_eth_function[6] = (struct usb_descriptor_header *) &hs_source_desc;
910 hs_eth_function[7] = (struct usb_descriptor_header *) &hs_sink_desc;
911 hs_eth_function[8] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912#else
913 hs_eth_function[1] = NULL;
914#endif
915}
916
917#ifdef CONFIG_USB_ETH_RNDIS
918static const struct usb_descriptor_header *hs_rndis_function [] = {
919 (struct usb_descriptor_header *) &otg_descriptor,
920 /* control interface matches ACM, not Ethernet */
921 (struct usb_descriptor_header *) &rndis_control_intf,
922 (struct usb_descriptor_header *) &header_desc,
923 (struct usb_descriptor_header *) &call_mgmt_descriptor,
924 (struct usb_descriptor_header *) &acm_descriptor,
925 (struct usb_descriptor_header *) &union_desc,
926 (struct usb_descriptor_header *) &hs_status_desc,
927 /* data interface has no altsetting */
928 (struct usb_descriptor_header *) &rndis_data_intf,
929 (struct usb_descriptor_header *) &hs_source_desc,
930 (struct usb_descriptor_header *) &hs_sink_desc,
931 NULL,
932};
933#endif
934
935
936/* maxpacket and other transfer characteristics vary by speed. */
937#define ep_desc(g,hs,fs) (((g)->speed==USB_SPEED_HIGH)?(hs):(fs))
938
939#else
940
941/* if there's no high speed support, maxpacket doesn't change. */
David Brownell907cba32005-04-28 13:48:09 -0700942#define ep_desc(g,hs,fs) (((void)(g)), (fs))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
944static inline void __init hs_subset_descriptors(void)
945{
946}
947
948#endif /* !CONFIG_USB_GADGET_DUALSPEED */
949
950/*-------------------------------------------------------------------------*/
951
952/* descriptors that are built on-demand */
953
954static char manufacturer [50];
955static char product_desc [40] = DRIVER_DESC;
Aras Vaichas1afc64a2006-02-18 12:31:23 -0800956static char serial_number [20];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958/* address that the host will use ... usually assigned at random */
959static char ethaddr [2 * ETH_ALEN + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
961/* static strings, in UTF-8 */
962static struct usb_string strings [] = {
963 { STRING_MANUFACTURER, manufacturer, },
964 { STRING_PRODUCT, product_desc, },
Aras Vaichas1afc64a2006-02-18 12:31:23 -0800965 { STRING_SERIALNUMBER, serial_number, },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 { STRING_DATA, "Ethernet Data", },
David Brownell11d54892006-12-11 15:59:04 -0800967 { STRING_ETHADDR, ethaddr, },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968#ifdef DEV_CONFIG_CDC
969 { STRING_CDC, "CDC Ethernet", },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 { STRING_CONTROL, "CDC Communications Control", },
971#endif
972#ifdef DEV_CONFIG_SUBSET
973 { STRING_SUBSET, "CDC Ethernet Subset", },
974#endif
975#ifdef CONFIG_USB_ETH_RNDIS
976 { STRING_RNDIS, "RNDIS", },
977 { STRING_RNDIS_CONTROL, "RNDIS Communications Control", },
978#endif
979 { } /* end of list */
980};
981
982static struct usb_gadget_strings stringtab = {
983 .language = 0x0409, /* en-us */
984 .strings = strings,
985};
986
987/*
988 * one config, two interfaces: control, data.
989 * complications: class descriptors, and an altsetting.
990 */
991static int
992config_buf (enum usb_device_speed speed,
993 u8 *buf, u8 type,
994 unsigned index, int is_otg)
995{
996 int len;
997 const struct usb_config_descriptor *config;
998 const struct usb_descriptor_header **function;
999#ifdef CONFIG_USB_GADGET_DUALSPEED
1000 int hs = (speed == USB_SPEED_HIGH);
1001
1002 if (type == USB_DT_OTHER_SPEED_CONFIG)
1003 hs = !hs;
1004#define which_fn(t) (hs ? hs_ ## t ## _function : fs_ ## t ## _function)
1005#else
1006#define which_fn(t) (fs_ ## t ## _function)
1007#endif
1008
1009 if (index >= device_desc.bNumConfigurations)
1010 return -EINVAL;
1011
1012#ifdef CONFIG_USB_ETH_RNDIS
1013 /* list the RNDIS config first, to make Microsoft's drivers
1014 * happy. DOCSIS 1.0 needs this too.
1015 */
1016 if (device_desc.bNumConfigurations == 2 && index == 0) {
1017 config = &rndis_config;
1018 function = which_fn (rndis);
1019 } else
1020#endif
1021 {
1022 config = &eth_config;
1023 function = which_fn (eth);
1024 }
1025
1026 /* for now, don't advertise srp-only devices */
1027 if (!is_otg)
1028 function++;
1029
1030 len = usb_gadget_config_buf (config, buf, USB_BUFSIZ, function);
1031 if (len < 0)
1032 return len;
1033 ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
1034 return len;
1035}
1036
1037/*-------------------------------------------------------------------------*/
1038
Al Viro55016f12005-10-21 03:21:58 -04001039static void eth_start (struct eth_dev *dev, gfp_t gfp_flags);
1040static int alloc_requests (struct eth_dev *dev, unsigned n, gfp_t gfp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
David Brownell907cba32005-04-28 13:48:09 -07001042static int
Al Viro55016f12005-10-21 03:21:58 -04001043set_ether_config (struct eth_dev *dev, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044{
David Brownell907cba32005-04-28 13:48:09 -07001045 int result = 0;
1046 struct usb_gadget *gadget = dev->gadget;
1047
Ian Campbelle8282642005-06-29 10:20:29 +01001048#if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
David Brownell907cba32005-04-28 13:48:09 -07001049 /* status endpoint used for RNDIS and (optionally) CDC */
1050 if (!subset_active(dev) && dev->status_ep) {
1051 dev->status = ep_desc (gadget, &hs_status_desc,
1052 &fs_status_desc);
1053 dev->status_ep->driver_data = dev;
1054
1055 result = usb_ep_enable (dev->status_ep, dev->status);
1056 if (result != 0) {
David Brownell7e27f182006-06-13 09:54:40 -07001057 DEBUG (dev, "enable %s --> %d\n",
David Brownell907cba32005-04-28 13:48:09 -07001058 dev->status_ep->name, result);
1059 goto done;
1060 }
1061 }
Ian Campbelle8282642005-06-29 10:20:29 +01001062#endif
David Brownell907cba32005-04-28 13:48:09 -07001063
David Brownell11d54892006-12-11 15:59:04 -08001064 dev->in = ep_desc(gadget, &hs_source_desc, &fs_source_desc);
David Brownell907cba32005-04-28 13:48:09 -07001065 dev->in_ep->driver_data = dev;
1066
David Brownell11d54892006-12-11 15:59:04 -08001067 dev->out = ep_desc(gadget, &hs_sink_desc, &fs_sink_desc);
David Brownell907cba32005-04-28 13:48:09 -07001068 dev->out_ep->driver_data = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069
1070 /* With CDC, the host isn't allowed to use these two data
1071 * endpoints in the default altsetting for the interface.
1072 * so we don't activate them yet. Reset from SET_INTERFACE.
1073 *
1074 * Strictly speaking RNDIS should work the same: activation is
1075 * a side effect of setting a packet filter. Deactivation is
1076 * from REMOTE_NDIS_HALT_MSG, reset from REMOTE_NDIS_RESET_MSG.
1077 */
David Brownell907cba32005-04-28 13:48:09 -07001078 if (!cdc_active(dev)) {
1079 result = usb_ep_enable (dev->in_ep, dev->in);
1080 if (result != 0) {
David Brownell7e27f182006-06-13 09:54:40 -07001081 DEBUG(dev, "enable %s --> %d\n",
David Brownell907cba32005-04-28 13:48:09 -07001082 dev->in_ep->name, result);
1083 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 }
1085
David Brownell907cba32005-04-28 13:48:09 -07001086 result = usb_ep_enable (dev->out_ep, dev->out);
1087 if (result != 0) {
David Brownell7e27f182006-06-13 09:54:40 -07001088 DEBUG (dev, "enable %s --> %d\n",
Matt Reimer4186c292006-06-07 11:46:13 -07001089 dev->out_ep->name, result);
David Brownell907cba32005-04-28 13:48:09 -07001090 goto done;
1091 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093
David Brownell907cba32005-04-28 13:48:09 -07001094done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 if (result == 0)
1096 result = alloc_requests (dev, qlen (gadget), gfp_flags);
1097
1098 /* on error, disable any endpoints */
1099 if (result < 0) {
David Brownell907cba32005-04-28 13:48:09 -07001100 if (!subset_active(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 (void) usb_ep_disable (dev->status_ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 dev->status = NULL;
David Brownell907cba32005-04-28 13:48:09 -07001103 (void) usb_ep_disable (dev->in_ep);
1104 (void) usb_ep_disable (dev->out_ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 dev->in = NULL;
1106 dev->out = NULL;
1107 } else
1108
1109 /* activate non-CDC configs right away
1110 * this isn't strictly according to the RNDIS spec
1111 */
David Brownell907cba32005-04-28 13:48:09 -07001112 if (!cdc_active (dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 netif_carrier_on (dev->net);
1114 if (netif_running (dev->net)) {
1115 spin_unlock (&dev->lock);
1116 eth_start (dev, GFP_ATOMIC);
1117 spin_lock (&dev->lock);
1118 }
1119 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
1121 if (result == 0)
1122 DEBUG (dev, "qlen %d\n", qlen (gadget));
1123
1124 /* caller is responsible for cleanup on error */
1125 return result;
1126}
1127
1128static void eth_reset_config (struct eth_dev *dev)
1129{
1130 struct usb_request *req;
1131
1132 if (dev->config == 0)
1133 return;
1134
1135 DEBUG (dev, "%s\n", __FUNCTION__);
1136
1137 netif_stop_queue (dev->net);
1138 netif_carrier_off (dev->net);
David Brownell340600a2005-04-28 13:45:25 -07001139 rndis_uninit(dev->rndis_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
1141 /* disable endpoints, forcing (synchronous) completion of
1142 * pending i/o. then free the requests.
1143 */
1144 if (dev->in) {
1145 usb_ep_disable (dev->in_ep);
David Brownell789851c2006-08-21 15:26:38 -07001146 spin_lock(&dev->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 while (likely (!list_empty (&dev->tx_reqs))) {
1148 req = container_of (dev->tx_reqs.next,
1149 struct usb_request, list);
1150 list_del (&req->list);
David Brownell789851c2006-08-21 15:26:38 -07001151
1152 spin_unlock(&dev->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 usb_ep_free_request (dev->in_ep, req);
David Brownell789851c2006-08-21 15:26:38 -07001154 spin_lock(&dev->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 }
David Brownell789851c2006-08-21 15:26:38 -07001156 spin_unlock(&dev->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 }
1158 if (dev->out) {
1159 usb_ep_disable (dev->out_ep);
David Brownell789851c2006-08-21 15:26:38 -07001160 spin_lock(&dev->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 while (likely (!list_empty (&dev->rx_reqs))) {
1162 req = container_of (dev->rx_reqs.next,
1163 struct usb_request, list);
1164 list_del (&req->list);
David Brownell789851c2006-08-21 15:26:38 -07001165
1166 spin_unlock(&dev->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 usb_ep_free_request (dev->out_ep, req);
David Brownell789851c2006-08-21 15:26:38 -07001168 spin_lock(&dev->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 }
David Brownell789851c2006-08-21 15:26:38 -07001170 spin_unlock(&dev->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 }
1172
1173 if (dev->status) {
1174 usb_ep_disable (dev->status_ep);
1175 }
David Brownell907cba32005-04-28 13:48:09 -07001176 dev->rndis = 0;
1177 dev->cdc_filter = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 dev->config = 0;
1179}
1180
1181/* change our operational config. must agree with the code
1182 * that returns config descriptors, and altsetting code.
1183 */
1184static int
Al Viro55016f12005-10-21 03:21:58 -04001185eth_set_config (struct eth_dev *dev, unsigned number, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186{
1187 int result = 0;
1188 struct usb_gadget *gadget = dev->gadget;
1189
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 if (gadget_is_sa1100 (gadget)
1191 && dev->config
1192 && atomic_read (&dev->tx_qlen) != 0) {
1193 /* tx fifo is full, but we can't clear it...*/
1194 INFO (dev, "can't change configurations\n");
1195 return -ESPIPE;
1196 }
1197 eth_reset_config (dev);
1198
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 switch (number) {
1200 case DEV_CONFIG_VALUE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 result = set_ether_config (dev, gfp_flags);
1202 break;
1203#ifdef CONFIG_USB_ETH_RNDIS
1204 case DEV_RNDIS_CONFIG_VALUE:
1205 dev->rndis = 1;
1206 result = set_ether_config (dev, gfp_flags);
1207 break;
1208#endif
1209 default:
1210 result = -EINVAL;
1211 /* FALL THROUGH */
1212 case 0:
1213 break;
1214 }
1215
1216 if (result) {
1217 if (number)
1218 eth_reset_config (dev);
1219 usb_gadget_vbus_draw(dev->gadget,
1220 dev->gadget->is_otg ? 8 : 100);
1221 } else {
1222 char *speed;
1223 unsigned power;
1224
1225 power = 2 * eth_config.bMaxPower;
1226 usb_gadget_vbus_draw(dev->gadget, power);
1227
1228 switch (gadget->speed) {
1229 case USB_SPEED_FULL: speed = "full"; break;
1230#ifdef CONFIG_USB_GADGET_DUALSPEED
1231 case USB_SPEED_HIGH: speed = "high"; break;
1232#endif
David Brownell7e27f182006-06-13 09:54:40 -07001233 default: speed = "?"; break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 }
1235
1236 dev->config = number;
1237 INFO (dev, "%s speed config #%d: %d mA, %s, using %s\n",
1238 speed, number, power, driver_desc,
David Brownell45e45ab2005-05-16 08:26:38 -07001239 rndis_active(dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 ? "RNDIS"
David Brownell45e45ab2005-05-16 08:26:38 -07001241 : (cdc_active(dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 ? "CDC Ethernet"
1243 : "CDC Ethernet Subset"));
1244 }
1245 return result;
1246}
1247
1248/*-------------------------------------------------------------------------*/
1249
1250#ifdef DEV_CONFIG_CDC
1251
David Brownell907cba32005-04-28 13:48:09 -07001252/* The interrupt endpoint is used in CDC networking models (Ethernet, ATM)
1253 * only to notify the host about link status changes (which we support) or
1254 * report completion of some encapsulated command (as used in RNDIS). Since
1255 * we want this CDC Ethernet code to be vendor-neutral, we don't use that
1256 * command mechanism; and only one status request is ever queued.
1257 */
1258
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259static void eth_status_complete (struct usb_ep *ep, struct usb_request *req)
1260{
1261 struct usb_cdc_notification *event = req->buf;
1262 int value = req->status;
1263 struct eth_dev *dev = ep->driver_data;
1264
1265 /* issue the second notification if host reads the first */
1266 if (event->bNotificationType == USB_CDC_NOTIFY_NETWORK_CONNECTION
1267 && value == 0) {
1268 __le32 *data = req->buf + sizeof *event;
1269
1270 event->bmRequestType = 0xA1;
1271 event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE;
1272 event->wValue = __constant_cpu_to_le16 (0);
1273 event->wIndex = __constant_cpu_to_le16 (1);
1274 event->wLength = __constant_cpu_to_le16 (8);
1275
1276 /* SPEED_CHANGE data is up/down speeds in bits/sec */
1277 data [0] = data [1] = cpu_to_le32 (BITRATE (dev->gadget));
1278
1279 req->length = STATUS_BYTECOUNT;
1280 value = usb_ep_queue (ep, req, GFP_ATOMIC);
1281 DEBUG (dev, "send SPEED_CHANGE --> %d\n", value);
1282 if (value == 0)
1283 return;
1284 } else if (value != -ECONNRESET)
1285 DEBUG (dev, "event %02x --> %d\n",
1286 event->bNotificationType, value);
David Brownell907cba32005-04-28 13:48:09 -07001287 req->context = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288}
1289
1290static void issue_start_status (struct eth_dev *dev)
1291{
1292 struct usb_request *req = dev->stat_req;
1293 struct usb_cdc_notification *event;
1294 int value;
David Brownell7e27f182006-06-13 09:54:40 -07001295
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 DEBUG (dev, "%s, flush old status first\n", __FUNCTION__);
1297
1298 /* flush old status
1299 *
1300 * FIXME ugly idiom, maybe we'd be better with just
1301 * a "cancel the whole queue" primitive since any
1302 * unlink-one primitive has way too many error modes.
1303 * here, we "know" toggle is already clear...
David Brownell907cba32005-04-28 13:48:09 -07001304 *
1305 * FIXME iff req->context != null just dequeue it
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 */
1307 usb_ep_disable (dev->status_ep);
1308 usb_ep_enable (dev->status_ep, dev->status);
1309
1310 /* 3.8.1 says to issue first NETWORK_CONNECTION, then
1311 * a SPEED_CHANGE. could be useful in some configs.
1312 */
1313 event = req->buf;
1314 event->bmRequestType = 0xA1;
1315 event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION;
1316 event->wValue = __constant_cpu_to_le16 (1); /* connected */
1317 event->wIndex = __constant_cpu_to_le16 (1);
1318 event->wLength = 0;
1319
1320 req->length = sizeof *event;
1321 req->complete = eth_status_complete;
David Brownell907cba32005-04-28 13:48:09 -07001322 req->context = dev;
1323
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 value = usb_ep_queue (dev->status_ep, req, GFP_ATOMIC);
1325 if (value < 0)
1326 DEBUG (dev, "status buf queue --> %d\n", value);
1327}
1328
1329#endif
1330
1331/*-------------------------------------------------------------------------*/
1332
1333static void eth_setup_complete (struct usb_ep *ep, struct usb_request *req)
1334{
1335 if (req->status || req->actual != req->length)
1336 DEBUG ((struct eth_dev *) ep->driver_data,
1337 "setup complete --> %d, %d/%d\n",
1338 req->status, req->actual, req->length);
1339}
1340
1341#ifdef CONFIG_USB_ETH_RNDIS
1342
1343static void rndis_response_complete (struct usb_ep *ep, struct usb_request *req)
1344{
1345 if (req->status || req->actual != req->length)
1346 DEBUG ((struct eth_dev *) ep->driver_data,
1347 "rndis response complete --> %d, %d/%d\n",
1348 req->status, req->actual, req->length);
1349
1350 /* done sending after USB_CDC_GET_ENCAPSULATED_RESPONSE */
1351}
1352
1353static void rndis_command_complete (struct usb_ep *ep, struct usb_request *req)
1354{
1355 struct eth_dev *dev = ep->driver_data;
1356 int status;
David Brownell7e27f182006-06-13 09:54:40 -07001357
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 /* received RNDIS command from USB_CDC_SEND_ENCAPSULATED_COMMAND */
1359 spin_lock(&dev->lock);
1360 status = rndis_msg_parser (dev->rndis_config, (u8 *) req->buf);
1361 if (status < 0)
1362 ERROR(dev, "%s: rndis parse error %d\n", __FUNCTION__, status);
1363 spin_unlock(&dev->lock);
1364}
1365
1366#endif /* RNDIS */
1367
1368/*
1369 * The setup() callback implements all the ep0 functionality that's not
1370 * handled lower down. CDC has a number of less-common features:
1371 *
1372 * - two interfaces: control, and ethernet data
1373 * - Ethernet data interface has two altsettings: default, and active
1374 * - class-specific descriptors for the control interface
1375 * - class-specific control requests
1376 */
1377static int
1378eth_setup (struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
1379{
1380 struct eth_dev *dev = get_gadget_data (gadget);
1381 struct usb_request *req = dev->req;
1382 int value = -EOPNOTSUPP;
David Brownell1bbc1692005-05-07 13:05:13 -07001383 u16 wIndex = le16_to_cpu(ctrl->wIndex);
1384 u16 wValue = le16_to_cpu(ctrl->wValue);
1385 u16 wLength = le16_to_cpu(ctrl->wLength);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
1387 /* descriptors just go into the pre-allocated ep0 buffer,
1388 * while config change events may enable network traffic.
1389 */
1390 req->complete = eth_setup_complete;
1391 switch (ctrl->bRequest) {
1392
1393 case USB_REQ_GET_DESCRIPTOR:
1394 if (ctrl->bRequestType != USB_DIR_IN)
1395 break;
1396 switch (wValue >> 8) {
1397
1398 case USB_DT_DEVICE:
1399 value = min (wLength, (u16) sizeof device_desc);
1400 memcpy (req->buf, &device_desc, value);
1401 break;
1402#ifdef CONFIG_USB_GADGET_DUALSPEED
1403 case USB_DT_DEVICE_QUALIFIER:
1404 if (!gadget->is_dualspeed)
1405 break;
1406 value = min (wLength, (u16) sizeof dev_qualifier);
1407 memcpy (req->buf, &dev_qualifier, value);
1408 break;
1409
1410 case USB_DT_OTHER_SPEED_CONFIG:
1411 if (!gadget->is_dualspeed)
1412 break;
1413 // FALLTHROUGH
1414#endif /* CONFIG_USB_GADGET_DUALSPEED */
1415 case USB_DT_CONFIG:
1416 value = config_buf (gadget->speed, req->buf,
1417 wValue >> 8,
1418 wValue & 0xff,
1419 gadget->is_otg);
1420 if (value >= 0)
1421 value = min (wLength, (u16) value);
1422 break;
1423
1424 case USB_DT_STRING:
1425 value = usb_gadget_get_string (&stringtab,
1426 wValue & 0xff, req->buf);
1427 if (value >= 0)
1428 value = min (wLength, (u16) value);
1429 break;
1430 }
1431 break;
1432
1433 case USB_REQ_SET_CONFIGURATION:
1434 if (ctrl->bRequestType != 0)
1435 break;
1436 if (gadget->a_hnp_support)
1437 DEBUG (dev, "HNP available\n");
1438 else if (gadget->a_alt_hnp_support)
1439 DEBUG (dev, "HNP needs a different root port\n");
1440 spin_lock (&dev->lock);
1441 value = eth_set_config (dev, wValue, GFP_ATOMIC);
1442 spin_unlock (&dev->lock);
1443 break;
1444 case USB_REQ_GET_CONFIGURATION:
1445 if (ctrl->bRequestType != USB_DIR_IN)
1446 break;
1447 *(u8 *)req->buf = dev->config;
1448 value = min (wLength, (u16) 1);
1449 break;
1450
1451 case USB_REQ_SET_INTERFACE:
1452 if (ctrl->bRequestType != USB_RECIP_INTERFACE
1453 || !dev->config
1454 || wIndex > 1)
1455 break;
David Brownell45e45ab2005-05-16 08:26:38 -07001456 if (!cdc_active(dev) && wIndex != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 break;
1458 spin_lock (&dev->lock);
1459
1460 /* PXA hardware partially handles SET_INTERFACE;
1461 * we need to kluge around that interference.
1462 */
1463 if (gadget_is_pxa (gadget)) {
1464 value = eth_set_config (dev, DEV_CONFIG_VALUE,
1465 GFP_ATOMIC);
1466 goto done_set_intf;
1467 }
1468
1469#ifdef DEV_CONFIG_CDC
1470 switch (wIndex) {
1471 case 0: /* control/master intf */
1472 if (wValue != 0)
1473 break;
1474 if (dev->status) {
1475 usb_ep_disable (dev->status_ep);
1476 usb_ep_enable (dev->status_ep, dev->status);
1477 }
1478 value = 0;
1479 break;
1480 case 1: /* data intf */
1481 if (wValue > 1)
1482 break;
1483 usb_ep_disable (dev->in_ep);
1484 usb_ep_disable (dev->out_ep);
1485
1486 /* CDC requires the data transfers not be done from
1487 * the default interface setting ... also, setting
David Brownell907cba32005-04-28 13:48:09 -07001488 * the non-default interface resets filters etc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 */
1490 if (wValue == 1) {
David Brownell907cba32005-04-28 13:48:09 -07001491 if (!cdc_active (dev))
1492 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 usb_ep_enable (dev->in_ep, dev->in);
1494 usb_ep_enable (dev->out_ep, dev->out);
1495 dev->cdc_filter = DEFAULT_FILTER;
1496 netif_carrier_on (dev->net);
1497 if (dev->status)
1498 issue_start_status (dev);
1499 if (netif_running (dev->net)) {
1500 spin_unlock (&dev->lock);
1501 eth_start (dev, GFP_ATOMIC);
1502 spin_lock (&dev->lock);
1503 }
1504 } else {
1505 netif_stop_queue (dev->net);
1506 netif_carrier_off (dev->net);
1507 }
1508 value = 0;
1509 break;
1510 }
1511#else
1512 /* FIXME this is wrong, as is the assumption that
1513 * all non-PXA hardware talks real CDC ...
1514 */
1515 dev_warn (&gadget->dev, "set_interface ignored!\n");
1516#endif /* DEV_CONFIG_CDC */
1517
1518done_set_intf:
1519 spin_unlock (&dev->lock);
1520 break;
1521 case USB_REQ_GET_INTERFACE:
1522 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE)
1523 || !dev->config
1524 || wIndex > 1)
1525 break;
David Brownell45e45ab2005-05-16 08:26:38 -07001526 if (!(cdc_active(dev) || rndis_active(dev)) && wIndex != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 break;
1528
1529 /* for CDC, iff carrier is on, data interface is active. */
David Brownell45e45ab2005-05-16 08:26:38 -07001530 if (rndis_active(dev) || wIndex != 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 *(u8 *)req->buf = 0;
1532 else
1533 *(u8 *)req->buf = netif_carrier_ok (dev->net) ? 1 : 0;
1534 value = min (wLength, (u16) 1);
1535 break;
1536
1537#ifdef DEV_CONFIG_CDC
1538 case USB_CDC_SET_ETHERNET_PACKET_FILTER:
1539 /* see 6.2.30: no data, wIndex = interface,
1540 * wValue = packet filter bitmap
1541 */
1542 if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
David Brownell45e45ab2005-05-16 08:26:38 -07001543 || !cdc_active(dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 || wLength != 0
1545 || wIndex > 1)
1546 break;
1547 DEBUG (dev, "packet filter %02x\n", wValue);
1548 dev->cdc_filter = wValue;
1549 value = 0;
1550 break;
1551
1552 /* and potentially:
1553 * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS:
1554 * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER:
1555 * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER:
1556 * case USB_CDC_GET_ETHERNET_STATISTIC:
1557 */
1558
1559#endif /* DEV_CONFIG_CDC */
1560
David Brownell7e27f182006-06-13 09:54:40 -07001561#ifdef CONFIG_USB_ETH_RNDIS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 /* RNDIS uses the CDC command encapsulation mechanism to implement
1563 * an RPC scheme, with much getting/setting of attributes by OID.
1564 */
1565 case USB_CDC_SEND_ENCAPSULATED_COMMAND:
1566 if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
David Brownell45e45ab2005-05-16 08:26:38 -07001567 || !rndis_active(dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 || wLength > USB_BUFSIZ
1569 || wValue
1570 || rndis_control_intf.bInterfaceNumber
1571 != wIndex)
1572 break;
1573 /* read the request, then process it */
1574 value = wLength;
1575 req->complete = rndis_command_complete;
1576 /* later, rndis_control_ack () sends a notification */
1577 break;
David Brownell7e27f182006-06-13 09:54:40 -07001578
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 case USB_CDC_GET_ENCAPSULATED_RESPONSE:
1580 if ((USB_DIR_IN|USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1581 == ctrl->bRequestType
David Brownell45e45ab2005-05-16 08:26:38 -07001582 && rndis_active(dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 // && wLength >= 0x0400
1584 && !wValue
1585 && rndis_control_intf.bInterfaceNumber
1586 == wIndex) {
1587 u8 *buf;
1588
1589 /* return the result */
1590 buf = rndis_get_next_response (dev->rndis_config,
1591 &value);
1592 if (buf) {
1593 memcpy (req->buf, buf, value);
1594 req->complete = rndis_response_complete;
1595 rndis_free_response(dev->rndis_config, buf);
1596 }
1597 /* else stalls ... spec says to avoid that */
1598 }
1599 break;
1600#endif /* RNDIS */
1601
1602 default:
1603 VDEBUG (dev,
1604 "unknown control req%02x.%02x v%04x i%04x l%d\n",
1605 ctrl->bRequestType, ctrl->bRequest,
1606 wValue, wIndex, wLength);
1607 }
1608
1609 /* respond with data transfer before status phase? */
1610 if (value >= 0) {
1611 req->length = value;
1612 req->zero = value < wLength
1613 && (value % gadget->ep0->maxpacket) == 0;
1614 value = usb_ep_queue (gadget->ep0, req, GFP_ATOMIC);
1615 if (value < 0) {
1616 DEBUG (dev, "ep_queue --> %d\n", value);
1617 req->status = 0;
1618 eth_setup_complete (gadget->ep0, req);
1619 }
1620 }
1621
1622 /* host either stalls (value < 0) or reports success */
1623 return value;
1624}
1625
1626static void
1627eth_disconnect (struct usb_gadget *gadget)
1628{
1629 struct eth_dev *dev = get_gadget_data (gadget);
1630 unsigned long flags;
1631
1632 spin_lock_irqsave (&dev->lock, flags);
1633 netif_stop_queue (dev->net);
1634 netif_carrier_off (dev->net);
1635 eth_reset_config (dev);
1636 spin_unlock_irqrestore (&dev->lock, flags);
1637
1638 /* FIXME RNDIS should enter RNDIS_UNINITIALIZED */
1639
1640 /* next we may get setup() calls to enumerate new connections;
1641 * or an unbind() during shutdown (including removing module).
1642 */
1643}
1644
1645/*-------------------------------------------------------------------------*/
1646
1647/* NETWORK DRIVER HOOKUP (to the layer above this driver) */
1648
1649static int eth_change_mtu (struct net_device *net, int new_mtu)
1650{
1651 struct eth_dev *dev = netdev_priv(net);
1652
David Brownell7802ac52006-01-22 10:33:27 -08001653 if (dev->rndis)
1654 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655
1656 if (new_mtu <= ETH_HLEN || new_mtu > ETH_FRAME_LEN)
1657 return -ERANGE;
1658 /* no zero-length packet read wanted after mtu-sized packets */
1659 if (((new_mtu + sizeof (struct ethhdr)) % dev->in_ep->maxpacket) == 0)
1660 return -EDOM;
1661 net->mtu = new_mtu;
1662 return 0;
1663}
1664
1665static struct net_device_stats *eth_get_stats (struct net_device *net)
1666{
1667 return &((struct eth_dev *)netdev_priv(net))->stats;
1668}
1669
1670static void eth_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *p)
1671{
1672 struct eth_dev *dev = netdev_priv(net);
1673 strlcpy(p->driver, shortname, sizeof p->driver);
1674 strlcpy(p->version, DRIVER_VERSION, sizeof p->version);
1675 strlcpy(p->fw_version, dev->gadget->name, sizeof p->fw_version);
1676 strlcpy (p->bus_info, dev->gadget->dev.bus_id, sizeof p->bus_info);
1677}
1678
1679static u32 eth_get_link(struct net_device *net)
1680{
1681 struct eth_dev *dev = netdev_priv(net);
1682 return dev->gadget->speed != USB_SPEED_UNKNOWN;
1683}
1684
1685static struct ethtool_ops ops = {
1686 .get_drvinfo = eth_get_drvinfo,
1687 .get_link = eth_get_link
1688};
1689
1690static void defer_kevent (struct eth_dev *dev, int flag)
1691{
1692 if (test_and_set_bit (flag, &dev->todo))
1693 return;
1694 if (!schedule_work (&dev->work))
1695 ERROR (dev, "kevent %d may have been dropped\n", flag);
1696 else
1697 DEBUG (dev, "kevent %d scheduled\n", flag);
1698}
1699
1700static void rx_complete (struct usb_ep *ep, struct usb_request *req);
1701
1702static int
Al Viro55016f12005-10-21 03:21:58 -04001703rx_submit (struct eth_dev *dev, struct usb_request *req, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704{
1705 struct sk_buff *skb;
1706 int retval = -ENOMEM;
1707 size_t size;
1708
1709 /* Padding up to RX_EXTRA handles minor disagreements with host.
1710 * Normally we use the USB "terminate on short read" convention;
1711 * so allow up to (N*maxpacket), since that memory is normally
1712 * already allocated. Some hardware doesn't deal well with short
1713 * reads (e.g. DMA must be N*maxpacket), so for now don't trim a
1714 * byte off the end (to force hardware errors on overflow).
1715 *
1716 * RNDIS uses internal framing, and explicitly allows senders to
1717 * pad to end-of-packet. That's potentially nice for speed,
1718 * but means receivers can't recover synch on their own.
1719 */
1720 size = (sizeof (struct ethhdr) + dev->net->mtu + RX_EXTRA);
1721 size += dev->out_ep->maxpacket - 1;
David Brownell907cba32005-04-28 13:48:09 -07001722 if (rndis_active(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 size += sizeof (struct rndis_packet_msg_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 size -= size % dev->out_ep->maxpacket;
1725
David Brownella9475222007-07-30 12:31:07 -07001726 skb = alloc_skb(size + NET_IP_ALIGN, gfp_flags);
1727 if (skb == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 DEBUG (dev, "no rx skb\n");
1729 goto enomem;
1730 }
David Brownell7e27f182006-06-13 09:54:40 -07001731
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 /* Some platforms perform better when IP packets are aligned,
1733 * but on at least one, checksumming fails otherwise. Note:
David Brownell6cdee102005-04-18 17:39:34 -07001734 * RNDIS headers involve variable numbers of LE32 values.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 */
1736 skb_reserve(skb, NET_IP_ALIGN);
1737
1738 req->buf = skb->data;
1739 req->length = size;
1740 req->complete = rx_complete;
1741 req->context = skb;
1742
1743 retval = usb_ep_queue (dev->out_ep, req, gfp_flags);
1744 if (retval == -ENOMEM)
1745enomem:
1746 defer_kevent (dev, WORK_RX_MEMORY);
1747 if (retval) {
1748 DEBUG (dev, "rx submit --> %d\n", retval);
Erik Hovlandb8d297c2007-04-23 10:50:15 -07001749 if (skb)
1750 dev_kfree_skb_any(skb);
David Brownell789851c2006-08-21 15:26:38 -07001751 spin_lock(&dev->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 list_add (&req->list, &dev->rx_reqs);
David Brownell789851c2006-08-21 15:26:38 -07001753 spin_unlock(&dev->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 }
1755 return retval;
1756}
1757
1758static void rx_complete (struct usb_ep *ep, struct usb_request *req)
1759{
1760 struct sk_buff *skb = req->context;
1761 struct eth_dev *dev = ep->driver_data;
1762 int status = req->status;
1763
1764 switch (status) {
1765
1766 /* normal completion */
1767 case 0:
1768 skb_put (skb, req->actual);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 /* we know MaxPacketsPerTransfer == 1 here */
David Brownell45e45ab2005-05-16 08:26:38 -07001770 if (rndis_active(dev))
David Brownell6cdee102005-04-18 17:39:34 -07001771 status = rndis_rm_hdr (skb);
David Brownell6cdee102005-04-18 17:39:34 -07001772 if (status < 0
1773 || ETH_HLEN > skb->len
1774 || skb->len > ETH_FRAME_LEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 dev->stats.rx_errors++;
1776 dev->stats.rx_length_errors++;
1777 DEBUG (dev, "rx length %d\n", skb->len);
1778 break;
1779 }
1780
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 skb->protocol = eth_type_trans (skb, dev->net);
1782 dev->stats.rx_packets++;
1783 dev->stats.rx_bytes += skb->len;
1784
1785 /* no buffer copies needed, unless hardware can't
1786 * use skb buffers.
1787 */
1788 status = netif_rx (skb);
1789 skb = NULL;
1790 break;
1791
1792 /* software-driven interface shutdown */
1793 case -ECONNRESET: // unlink
1794 case -ESHUTDOWN: // disconnect etc
1795 VDEBUG (dev, "rx shutdown, code %d\n", status);
1796 goto quiesce;
1797
1798 /* for hardware automagic (such as pxa) */
1799 case -ECONNABORTED: // endpoint reset
1800 DEBUG (dev, "rx %s reset\n", ep->name);
1801 defer_kevent (dev, WORK_RX_MEMORY);
1802quiesce:
1803 dev_kfree_skb_any (skb);
1804 goto clean;
1805
1806 /* data overrun */
1807 case -EOVERFLOW:
1808 dev->stats.rx_over_errors++;
1809 // FALLTHROUGH
David Brownell7e27f182006-06-13 09:54:40 -07001810
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 default:
1812 dev->stats.rx_errors++;
1813 DEBUG (dev, "rx status %d\n", status);
1814 break;
1815 }
1816
1817 if (skb)
1818 dev_kfree_skb_any (skb);
1819 if (!netif_running (dev->net)) {
1820clean:
David Brownell789851c2006-08-21 15:26:38 -07001821 spin_lock(&dev->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 list_add (&req->list, &dev->rx_reqs);
David Brownell789851c2006-08-21 15:26:38 -07001823 spin_unlock(&dev->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 req = NULL;
1825 }
1826 if (req)
1827 rx_submit (dev, req, GFP_ATOMIC);
1828}
1829
1830static int prealloc (struct list_head *list, struct usb_ep *ep,
Al Viro55016f12005-10-21 03:21:58 -04001831 unsigned n, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832{
1833 unsigned i;
1834 struct usb_request *req;
1835
1836 if (!n)
1837 return -ENOMEM;
1838
1839 /* queue/recycle up to N requests */
1840 i = n;
1841 list_for_each_entry (req, list, list) {
1842 if (i-- == 0)
1843 goto extra;
1844 }
1845 while (i--) {
1846 req = usb_ep_alloc_request (ep, gfp_flags);
1847 if (!req)
1848 return list_empty (list) ? -ENOMEM : 0;
1849 list_add (&req->list, list);
1850 }
1851 return 0;
1852
1853extra:
1854 /* free extras */
1855 for (;;) {
1856 struct list_head *next;
1857
1858 next = req->list.next;
1859 list_del (&req->list);
1860 usb_ep_free_request (ep, req);
1861
1862 if (next == list)
1863 break;
1864
1865 req = container_of (next, struct usb_request, list);
1866 }
1867 return 0;
1868}
1869
Al Viro55016f12005-10-21 03:21:58 -04001870static int alloc_requests (struct eth_dev *dev, unsigned n, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871{
1872 int status;
1873
David Brownell789851c2006-08-21 15:26:38 -07001874 spin_lock(&dev->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 status = prealloc (&dev->tx_reqs, dev->in_ep, n, gfp_flags);
1876 if (status < 0)
1877 goto fail;
1878 status = prealloc (&dev->rx_reqs, dev->out_ep, n, gfp_flags);
1879 if (status < 0)
1880 goto fail;
David Brownell789851c2006-08-21 15:26:38 -07001881 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882fail:
1883 DEBUG (dev, "can't alloc requests\n");
David Brownell789851c2006-08-21 15:26:38 -07001884done:
1885 spin_unlock(&dev->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 return status;
1887}
1888
Al Viro55016f12005-10-21 03:21:58 -04001889static void rx_fill (struct eth_dev *dev, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890{
1891 struct usb_request *req;
1892 unsigned long flags;
1893
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 /* fill unused rxq slots with some skb */
David Brownell789851c2006-08-21 15:26:38 -07001895 spin_lock_irqsave(&dev->req_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 while (!list_empty (&dev->rx_reqs)) {
1897 req = container_of (dev->rx_reqs.next,
1898 struct usb_request, list);
1899 list_del_init (&req->list);
David Brownell789851c2006-08-21 15:26:38 -07001900 spin_unlock_irqrestore(&dev->req_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901
1902 if (rx_submit (dev, req, gfp_flags) < 0) {
1903 defer_kevent (dev, WORK_RX_MEMORY);
1904 return;
1905 }
1906
David Brownell789851c2006-08-21 15:26:38 -07001907 spin_lock_irqsave(&dev->req_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 }
David Brownell789851c2006-08-21 15:26:38 -07001909 spin_unlock_irqrestore(&dev->req_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910}
1911
David Howellsc4028952006-11-22 14:57:56 +00001912static void eth_work (struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913{
David Howellsc4028952006-11-22 14:57:56 +00001914 struct eth_dev *dev = container_of(work, struct eth_dev, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915
David Brownell907cba32005-04-28 13:48:09 -07001916 if (test_and_clear_bit (WORK_RX_MEMORY, &dev->todo)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 if (netif_running (dev->net))
1918 rx_fill (dev, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 }
1920
1921 if (dev->todo)
1922 DEBUG (dev, "work done, flags = 0x%lx\n", dev->todo);
1923}
1924
1925static void tx_complete (struct usb_ep *ep, struct usb_request *req)
1926{
1927 struct sk_buff *skb = req->context;
1928 struct eth_dev *dev = ep->driver_data;
1929
1930 switch (req->status) {
1931 default:
1932 dev->stats.tx_errors++;
1933 VDEBUG (dev, "tx err %d\n", req->status);
1934 /* FALLTHROUGH */
1935 case -ECONNRESET: // unlink
1936 case -ESHUTDOWN: // disconnect etc
1937 break;
1938 case 0:
1939 dev->stats.tx_bytes += skb->len;
1940 }
1941 dev->stats.tx_packets++;
1942
David Brownell789851c2006-08-21 15:26:38 -07001943 spin_lock(&dev->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 list_add (&req->list, &dev->tx_reqs);
David Brownell789851c2006-08-21 15:26:38 -07001945 spin_unlock(&dev->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 dev_kfree_skb_any (skb);
1947
1948 atomic_dec (&dev->tx_qlen);
1949 if (netif_carrier_ok (dev->net))
1950 netif_wake_queue (dev->net);
1951}
1952
1953static inline int eth_is_promisc (struct eth_dev *dev)
1954{
1955 /* no filters for the CDC subset; always promisc */
1956 if (subset_active (dev))
1957 return 1;
1958 return dev->cdc_filter & USB_CDC_PACKET_TYPE_PROMISCUOUS;
1959}
1960
1961static int eth_start_xmit (struct sk_buff *skb, struct net_device *net)
1962{
1963 struct eth_dev *dev = netdev_priv(net);
1964 int length = skb->len;
1965 int retval;
1966 struct usb_request *req = NULL;
1967 unsigned long flags;
1968
1969 /* apply outgoing CDC or RNDIS filters */
1970 if (!eth_is_promisc (dev)) {
1971 u8 *dest = skb->data;
1972
David Brownelld8126a02006-11-12 18:09:44 -08001973 if (is_multicast_ether_addr(dest)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 u16 type;
1975
1976 /* ignores USB_CDC_PACKET_TYPE_MULTICAST and host
1977 * SET_ETHERNET_MULTICAST_FILTERS requests
1978 */
David Brownelld8126a02006-11-12 18:09:44 -08001979 if (is_broadcast_ether_addr(dest))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 type = USB_CDC_PACKET_TYPE_BROADCAST;
1981 else
1982 type = USB_CDC_PACKET_TYPE_ALL_MULTICAST;
1983 if (!(dev->cdc_filter & type)) {
1984 dev_kfree_skb_any (skb);
1985 return 0;
1986 }
1987 }
1988 /* ignores USB_CDC_PACKET_TYPE_DIRECTED */
1989 }
1990
David Brownell789851c2006-08-21 15:26:38 -07001991 spin_lock_irqsave(&dev->req_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 req = container_of (dev->tx_reqs.next, struct usb_request, list);
1993 list_del (&req->list);
1994 if (list_empty (&dev->tx_reqs))
1995 netif_stop_queue (net);
David Brownell789851c2006-08-21 15:26:38 -07001996 spin_unlock_irqrestore(&dev->req_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997
1998 /* no buffer copies needed, unless the network stack did it
1999 * or the hardware can't use skb buffers.
2000 * or there's not enough space for any RNDIS headers we need
2001 */
David Brownell45e45ab2005-05-16 08:26:38 -07002002 if (rndis_active(dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 struct sk_buff *skb_rndis;
2004
2005 skb_rndis = skb_realloc_headroom (skb,
2006 sizeof (struct rndis_packet_msg_type));
2007 if (!skb_rndis)
2008 goto drop;
David Brownell7e27f182006-06-13 09:54:40 -07002009
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 dev_kfree_skb_any (skb);
2011 skb = skb_rndis;
2012 rndis_add_hdr (skb);
2013 length = skb->len;
2014 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 req->buf = skb->data;
2016 req->context = skb;
2017 req->complete = tx_complete;
2018
2019 /* use zlp framing on tx for strict CDC-Ether conformance,
2020 * though any robust network rx path ignores extra padding.
2021 * and some hardware doesn't like to write zlps.
2022 */
2023 req->zero = 1;
2024 if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0)
2025 length++;
2026
2027 req->length = length;
2028
2029#ifdef CONFIG_USB_GADGET_DUALSPEED
2030 /* throttle highspeed IRQ rate back slightly */
2031 req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH)
2032 ? ((atomic_read (&dev->tx_qlen) % TX_DELAY) != 0)
2033 : 0;
2034#endif
2035
2036 retval = usb_ep_queue (dev->in_ep, req, GFP_ATOMIC);
2037 switch (retval) {
2038 default:
2039 DEBUG (dev, "tx queue err %d\n", retval);
2040 break;
2041 case 0:
2042 net->trans_start = jiffies;
2043 atomic_inc (&dev->tx_qlen);
2044 }
2045
2046 if (retval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047drop:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 dev->stats.tx_dropped++;
2049 dev_kfree_skb_any (skb);
David Brownell789851c2006-08-21 15:26:38 -07002050 spin_lock_irqsave(&dev->req_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 if (list_empty (&dev->tx_reqs))
2052 netif_start_queue (net);
2053 list_add (&req->list, &dev->tx_reqs);
David Brownell789851c2006-08-21 15:26:38 -07002054 spin_unlock_irqrestore(&dev->req_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 }
2056 return 0;
2057}
2058
2059/*-------------------------------------------------------------------------*/
2060
2061#ifdef CONFIG_USB_ETH_RNDIS
2062
David Brownell907cba32005-04-28 13:48:09 -07002063/* The interrupt endpoint is used in RNDIS to notify the host when messages
2064 * other than data packets are available ... notably the REMOTE_NDIS_*_CMPLT
2065 * messages, but also REMOTE_NDIS_INDICATE_STATUS_MSG and potentially even
2066 * REMOTE_NDIS_KEEPALIVE_MSG.
2067 *
2068 * The RNDIS control queue is processed by GET_ENCAPSULATED_RESPONSE, and
2069 * normally just one notification will be queued.
2070 */
2071
Al Viro55016f12005-10-21 03:21:58 -04002072static struct usb_request *eth_req_alloc (struct usb_ep *, unsigned, gfp_t);
David Brownell907cba32005-04-28 13:48:09 -07002073static void eth_req_free (struct usb_ep *ep, struct usb_request *req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074
2075static void
2076rndis_control_ack_complete (struct usb_ep *ep, struct usb_request *req)
2077{
David Brownell907cba32005-04-28 13:48:09 -07002078 struct eth_dev *dev = ep->driver_data;
2079
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 if (req->status || req->actual != req->length)
David Brownell907cba32005-04-28 13:48:09 -07002081 DEBUG (dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 "rndis control ack complete --> %d, %d/%d\n",
2083 req->status, req->actual, req->length);
David Brownell907cba32005-04-28 13:48:09 -07002084 req->context = NULL;
2085
2086 if (req != dev->stat_req)
2087 eth_req_free(ep, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088}
2089
2090static int rndis_control_ack (struct net_device *net)
2091{
2092 struct eth_dev *dev = netdev_priv(net);
Eric Sesterhenn55359022006-08-21 15:31:05 -07002093 int length;
David Brownell6cdee102005-04-18 17:39:34 -07002094 struct usb_request *resp = dev->stat_req;
David Brownell7e27f182006-06-13 09:54:40 -07002095
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 /* in case RNDIS calls this after disconnect */
David Brownell6cdee102005-04-18 17:39:34 -07002097 if (!dev->status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 DEBUG (dev, "status ENODEV\n");
2099 return -ENODEV;
2100 }
2101
David Brownell907cba32005-04-28 13:48:09 -07002102 /* in case queue length > 1 */
2103 if (resp->context) {
2104 resp = eth_req_alloc (dev->status_ep, 8, GFP_ATOMIC);
2105 if (!resp)
2106 return -ENOMEM;
2107 }
2108
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 /* Send RNDIS RESPONSE_AVAILABLE notification;
2110 * USB_CDC_NOTIFY_RESPONSE_AVAILABLE should work too
2111 */
2112 resp->length = 8;
2113 resp->complete = rndis_control_ack_complete;
David Brownell907cba32005-04-28 13:48:09 -07002114 resp->context = dev;
David Brownell7e27f182006-06-13 09:54:40 -07002115
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 *((__le32 *) resp->buf) = __constant_cpu_to_le32 (1);
2117 *((__le32 *) resp->buf + 1) = __constant_cpu_to_le32 (0);
David Brownell7e27f182006-06-13 09:54:40 -07002118
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 length = usb_ep_queue (dev->status_ep, resp, GFP_ATOMIC);
2120 if (length < 0) {
2121 resp->status = 0;
2122 rndis_control_ack_complete (dev->status_ep, resp);
2123 }
David Brownell7e27f182006-06-13 09:54:40 -07002124
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 return 0;
2126}
2127
David Brownell45e45ab2005-05-16 08:26:38 -07002128#else
2129
2130#define rndis_control_ack NULL
2131
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132#endif /* RNDIS */
2133
Al Viro55016f12005-10-21 03:21:58 -04002134static void eth_start (struct eth_dev *dev, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135{
2136 DEBUG (dev, "%s\n", __FUNCTION__);
2137
2138 /* fill the rx queue */
2139 rx_fill (dev, gfp_flags);
2140
David Brownell7e27f182006-06-13 09:54:40 -07002141 /* and open the tx floodgates */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 atomic_set (&dev->tx_qlen, 0);
2143 netif_wake_queue (dev->net);
David Brownell45e45ab2005-05-16 08:26:38 -07002144 if (rndis_active(dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 rndis_set_param_medium (dev->rndis_config,
2146 NDIS_MEDIUM_802_3,
David Brownell6cdee102005-04-18 17:39:34 -07002147 BITRATE(dev->gadget)/100);
David Brownell907cba32005-04-28 13:48:09 -07002148 (void) rndis_signal_connect (dev->rndis_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150}
2151
2152static int eth_open (struct net_device *net)
2153{
2154 struct eth_dev *dev = netdev_priv(net);
2155
2156 DEBUG (dev, "%s\n", __FUNCTION__);
2157 if (netif_carrier_ok (dev->net))
2158 eth_start (dev, GFP_KERNEL);
2159 return 0;
2160}
2161
2162static int eth_stop (struct net_device *net)
2163{
2164 struct eth_dev *dev = netdev_priv(net);
2165
2166 VDEBUG (dev, "%s\n", __FUNCTION__);
2167 netif_stop_queue (net);
2168
2169 DEBUG (dev, "stop stats: rx/tx %ld/%ld, errs %ld/%ld\n",
David Brownell7e27f182006-06-13 09:54:40 -07002170 dev->stats.rx_packets, dev->stats.tx_packets,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 dev->stats.rx_errors, dev->stats.tx_errors
2172 );
2173
2174 /* ensure there are no more active requests */
2175 if (dev->config) {
2176 usb_ep_disable (dev->in_ep);
2177 usb_ep_disable (dev->out_ep);
2178 if (netif_carrier_ok (dev->net)) {
2179 DEBUG (dev, "host still using in/out endpoints\n");
2180 // FIXME idiom may leave toggle wrong here
2181 usb_ep_enable (dev->in_ep, dev->in);
2182 usb_ep_enable (dev->out_ep, dev->out);
2183 }
2184 if (dev->status_ep) {
2185 usb_ep_disable (dev->status_ep);
2186 usb_ep_enable (dev->status_ep, dev->status);
2187 }
2188 }
David Brownell7e27f182006-06-13 09:54:40 -07002189
David Brownell45e45ab2005-05-16 08:26:38 -07002190 if (rndis_active(dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 rndis_set_param_medium (dev->rndis_config,
2192 NDIS_MEDIUM_802_3, 0);
David Brownell907cba32005-04-28 13:48:09 -07002193 (void) rndis_signal_disconnect (dev->rndis_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195
2196 return 0;
2197}
2198
2199/*-------------------------------------------------------------------------*/
2200
David Brownell907cba32005-04-28 13:48:09 -07002201static struct usb_request *
Al Viro55016f12005-10-21 03:21:58 -04002202eth_req_alloc (struct usb_ep *ep, unsigned size, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203{
2204 struct usb_request *req;
2205
David Brownell907cba32005-04-28 13:48:09 -07002206 req = usb_ep_alloc_request (ep, gfp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 if (!req)
2208 return NULL;
2209
David Brownell907cba32005-04-28 13:48:09 -07002210 req->buf = kmalloc (size, gfp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 if (!req->buf) {
2212 usb_ep_free_request (ep, req);
2213 req = NULL;
2214 }
2215 return req;
2216}
2217
2218static void
2219eth_req_free (struct usb_ep *ep, struct usb_request *req)
2220{
2221 kfree (req->buf);
2222 usb_ep_free_request (ep, req);
2223}
2224
2225
David Brownella3536782006-07-06 15:48:53 -07002226static void /* __init_or_exit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227eth_unbind (struct usb_gadget *gadget)
2228{
2229 struct eth_dev *dev = get_gadget_data (gadget);
2230
2231 DEBUG (dev, "unbind\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 rndis_deregister (dev->rndis_config);
2233 rndis_exit ();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234
2235 /* we've already been disconnected ... no i/o is active */
2236 if (dev->req) {
2237 eth_req_free (gadget->ep0, dev->req);
2238 dev->req = NULL;
2239 }
2240 if (dev->stat_req) {
2241 eth_req_free (dev->status_ep, dev->stat_req);
2242 dev->stat_req = NULL;
2243 }
2244
2245 unregister_netdev (dev->net);
2246 free_netdev(dev->net);
2247
2248 /* assuming we used keventd, it must quiesce too */
2249 flush_scheduled_work ();
2250 set_gadget_data (gadget, NULL);
2251}
2252
David Brownella3536782006-07-06 15:48:53 -07002253static u8 __devinit nibble (unsigned char c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254{
2255 if (likely (isdigit (c)))
2256 return c - '0';
2257 c = toupper (c);
2258 if (likely (isxdigit (c)))
2259 return 10 + c - 'A';
2260 return 0;
2261}
2262
David Brownella3536782006-07-06 15:48:53 -07002263static int __devinit get_ether_addr(const char *str, u8 *dev_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264{
2265 if (str) {
2266 unsigned i;
2267
2268 for (i = 0; i < 6; i++) {
2269 unsigned char num;
2270
2271 if((*str == '.') || (*str == ':'))
2272 str++;
2273 num = nibble(*str++) << 4;
2274 num |= (nibble(*str++));
2275 dev_addr [i] = num;
2276 }
2277 if (is_valid_ether_addr (dev_addr))
Aras Vaichas1afc64a2006-02-18 12:31:23 -08002278 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 }
2280 random_ether_addr(dev_addr);
Aras Vaichas1afc64a2006-02-18 12:31:23 -08002281 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282}
2283
David Brownella3536782006-07-06 15:48:53 -07002284static int __devinit
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285eth_bind (struct usb_gadget *gadget)
2286{
2287 struct eth_dev *dev;
2288 struct net_device *net;
2289 u8 cdc = 1, zlp = 1, rndis = 1;
2290 struct usb_ep *in_ep, *out_ep, *status_ep = NULL;
2291 int status = -ENOMEM;
David Brownell91e79c92005-07-13 15:18:30 -07002292 int gcnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293
2294 /* these flags are only ever cleared; compiler take note */
2295#ifndef DEV_CONFIG_CDC
2296 cdc = 0;
2297#endif
2298#ifndef CONFIG_USB_ETH_RNDIS
2299 rndis = 0;
2300#endif
2301
2302 /* Because most host side USB stacks handle CDC Ethernet, that
2303 * standard protocol is _strongly_ preferred for interop purposes.
2304 * (By everyone except Microsoft.)
2305 */
David Brownell91e79c92005-07-13 15:18:30 -07002306 if (gadget_is_pxa (gadget)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 /* pxa doesn't support altsettings */
2308 cdc = 0;
David Brownell729ed6d2006-08-30 13:24:56 -07002309 } else if (gadget_is_musbhdrc(gadget)) {
2310 /* reduce tx dma overhead by avoiding special cases */
2311 zlp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 } else if (gadget_is_sh(gadget)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 /* sh doesn't support multiple interfaces or configs */
2314 cdc = 0;
2315 rndis = 0;
2316 } else if (gadget_is_sa1100 (gadget)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 /* hardware can't write zlps */
2318 zlp = 0;
2319 /* sa1100 CAN do CDC, without status endpoint ... we use
2320 * non-CDC to be compatible with ARM Linux-2.4 "usb-eth".
2321 */
2322 cdc = 0;
David Brownell91e79c92005-07-13 15:18:30 -07002323 }
2324
2325 gcnum = usb_gadget_controller_number (gadget);
2326 if (gcnum >= 0)
2327 device_desc.bcdDevice = cpu_to_le16 (0x0200 + gcnum);
2328 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 /* can't assume CDC works. don't want to default to
2330 * anything less functional on CDC-capable hardware,
2331 * so we fail in this case.
2332 */
2333 dev_err (&gadget->dev,
2334 "controller '%s' not recognized\n",
2335 gadget->name);
2336 return -ENODEV;
2337 }
2338 snprintf (manufacturer, sizeof manufacturer, "%s %s/%s",
Serge E. Hallyn96b644b2006-10-02 02:18:13 -07002339 init_utsname()->sysname, init_utsname()->release,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 gadget->name);
2341
2342 /* If there's an RNDIS configuration, that's what Windows wants to
2343 * be using ... so use these product IDs here and in the "linux.inf"
2344 * needed to install MSFT drivers. Current Linux kernels will use
2345 * the second configuration if it's CDC Ethernet, and need some help
2346 * to choose the right configuration otherwise.
2347 */
2348 if (rndis) {
2349 device_desc.idVendor =
2350 __constant_cpu_to_le16(RNDIS_VENDOR_NUM);
2351 device_desc.idProduct =
2352 __constant_cpu_to_le16(RNDIS_PRODUCT_NUM);
2353 snprintf (product_desc, sizeof product_desc,
2354 "RNDIS/%s", driver_desc);
2355
2356 /* CDC subset ... recognized by Linux since 2.4.10, but Windows
David Brownell11d54892006-12-11 15:59:04 -08002357 * drivers aren't widely available. (That may be improved by
2358 * supporting one submode of the "SAFE" variant of MDLM.)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 */
2360 } else if (!cdc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 device_desc.idVendor =
2362 __constant_cpu_to_le16(SIMPLE_VENDOR_NUM);
2363 device_desc.idProduct =
2364 __constant_cpu_to_le16(SIMPLE_PRODUCT_NUM);
2365 }
2366
2367 /* support optional vendor/distro customization */
2368 if (idVendor) {
2369 if (!idProduct) {
2370 dev_err (&gadget->dev, "idVendor needs idProduct!\n");
2371 return -ENODEV;
2372 }
2373 device_desc.idVendor = cpu_to_le16(idVendor);
2374 device_desc.idProduct = cpu_to_le16(idProduct);
2375 if (bcdDevice)
2376 device_desc.bcdDevice = cpu_to_le16(bcdDevice);
2377 }
2378 if (iManufacturer)
2379 strlcpy (manufacturer, iManufacturer, sizeof manufacturer);
2380 if (iProduct)
2381 strlcpy (product_desc, iProduct, sizeof product_desc);
Aras Vaichas1afc64a2006-02-18 12:31:23 -08002382 if (iSerialNumber) {
2383 device_desc.iSerialNumber = STRING_SERIALNUMBER,
2384 strlcpy(serial_number, iSerialNumber, sizeof serial_number);
2385 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386
2387 /* all we really need is bulk IN/OUT */
2388 usb_ep_autoconfig_reset (gadget);
2389 in_ep = usb_ep_autoconfig (gadget, &fs_source_desc);
2390 if (!in_ep) {
2391autoconf_fail:
2392 dev_err (&gadget->dev,
2393 "can't autoconfigure on %s\n",
2394 gadget->name);
2395 return -ENODEV;
2396 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 in_ep->driver_data = in_ep; /* claim */
David Brownell7e27f182006-06-13 09:54:40 -07002398
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 out_ep = usb_ep_autoconfig (gadget, &fs_sink_desc);
2400 if (!out_ep)
2401 goto autoconf_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 out_ep->driver_data = out_ep; /* claim */
2403
2404#if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
2405 /* CDC Ethernet control interface doesn't require a status endpoint.
2406 * Since some hosts expect one, try to allocate one anyway.
2407 */
2408 if (cdc || rndis) {
2409 status_ep = usb_ep_autoconfig (gadget, &fs_status_desc);
2410 if (status_ep) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 status_ep->driver_data = status_ep; /* claim */
2412 } else if (rndis) {
2413 dev_err (&gadget->dev,
2414 "can't run RNDIS on %s\n",
2415 gadget->name);
2416 return -ENODEV;
2417#ifdef DEV_CONFIG_CDC
2418 /* pxa25x only does CDC subset; often used with RNDIS */
2419 } else if (cdc) {
2420 control_intf.bNumEndpoints = 0;
2421 /* FIXME remove endpoint from descriptor list */
2422#endif
2423 }
2424 }
2425#endif
2426
2427 /* one config: cdc, else minimal subset */
2428 if (!cdc) {
2429 eth_config.bNumInterfaces = 1;
2430 eth_config.iConfiguration = STRING_SUBSET;
David Brownell11d54892006-12-11 15:59:04 -08002431
2432 /* use functions to set these up, in case we're built to work
2433 * with multiple controllers and must override CDC Ethernet.
2434 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 fs_subset_descriptors();
2436 hs_subset_descriptors();
2437 }
2438
David Brownelle1394b42006-04-02 10:20:43 -08002439 device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
2440 usb_gadget_set_selfpowered (gadget);
2441
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 /* For now RNDIS is always a second config */
2443 if (rndis)
2444 device_desc.bNumConfigurations = 2;
2445
2446#ifdef CONFIG_USB_GADGET_DUALSPEED
2447 if (rndis)
2448 dev_qualifier.bNumConfigurations = 2;
2449 else if (!cdc)
2450 dev_qualifier.bDeviceClass = USB_CLASS_VENDOR_SPEC;
2451
2452 /* assumes ep0 uses the same value for both speeds ... */
2453 dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0;
2454
2455 /* and that all endpoints are dual-speed */
2456 hs_source_desc.bEndpointAddress = fs_source_desc.bEndpointAddress;
2457 hs_sink_desc.bEndpointAddress = fs_sink_desc.bEndpointAddress;
2458#if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
David Brownell907cba32005-04-28 13:48:09 -07002459 if (status_ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 hs_status_desc.bEndpointAddress =
2461 fs_status_desc.bEndpointAddress;
2462#endif
2463#endif /* DUALSPEED */
2464
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 if (gadget->is_otg) {
2466 otg_descriptor.bmAttributes |= USB_OTG_HNP,
2467 eth_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
2468 eth_config.bMaxPower = 4;
2469#ifdef CONFIG_USB_ETH_RNDIS
2470 rndis_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
2471 rndis_config.bMaxPower = 4;
2472#endif
2473 }
2474
David Brownell7e27f182006-06-13 09:54:40 -07002475 net = alloc_etherdev (sizeof *dev);
2476 if (!net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 return status;
2478 dev = netdev_priv(net);
2479 spin_lock_init (&dev->lock);
David Brownell789851c2006-08-21 15:26:38 -07002480 spin_lock_init (&dev->req_lock);
David Howellsc4028952006-11-22 14:57:56 +00002481 INIT_WORK (&dev->work, eth_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 INIT_LIST_HEAD (&dev->tx_reqs);
2483 INIT_LIST_HEAD (&dev->rx_reqs);
2484
2485 /* network device setup */
2486 dev->net = net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 strcpy (net->name, "usb%d");
2488 dev->cdc = cdc;
2489 dev->zlp = zlp;
2490
2491 dev->in_ep = in_ep;
2492 dev->out_ep = out_ep;
2493 dev->status_ep = status_ep;
2494
2495 /* Module params for these addresses should come from ID proms.
2496 * The host side address is used with CDC and RNDIS, and commonly
David Brownell11d54892006-12-11 15:59:04 -08002497 * ends up in a persistent config database. It's not clear if
2498 * host side code for the SAFE thing cares -- its original BLAN
2499 * thing didn't, Sharp never assigned those addresses on Zaurii.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 */
Aras Vaichas1afc64a2006-02-18 12:31:23 -08002501 if (get_ether_addr(dev_addr, net->dev_addr))
2502 dev_warn(&gadget->dev,
2503 "using random %s ethernet address\n", "self");
David Brownell11d54892006-12-11 15:59:04 -08002504 if (get_ether_addr(host_addr, dev->host_mac))
2505 dev_warn(&gadget->dev,
2506 "using random %s ethernet address\n", "host");
2507 snprintf (ethaddr, sizeof ethaddr, "%02X%02X%02X%02X%02X%02X",
2508 dev->host_mac [0], dev->host_mac [1],
2509 dev->host_mac [2], dev->host_mac [3],
2510 dev->host_mac [4], dev->host_mac [5]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511
2512 if (rndis) {
2513 status = rndis_init();
2514 if (status < 0) {
2515 dev_err (&gadget->dev, "can't init RNDIS, %d\n",
2516 status);
2517 goto fail;
2518 }
2519 }
2520
2521 net->change_mtu = eth_change_mtu;
2522 net->get_stats = eth_get_stats;
2523 net->hard_start_xmit = eth_start_xmit;
2524 net->open = eth_open;
2525 net->stop = eth_stop;
2526 // watchdog_timeo, tx_timeout ...
2527 // set_multicast_list
2528 SET_ETHTOOL_OPS(net, &ops);
2529
2530 /* preallocate control message data and buffer */
David Brownell907cba32005-04-28 13:48:09 -07002531 dev->req = eth_req_alloc (gadget->ep0, USB_BUFSIZ, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 if (!dev->req)
2533 goto fail;
2534 dev->req->complete = eth_setup_complete;
2535
2536 /* ... and maybe likewise for status transfer */
Ian Campbell05f33402005-06-29 10:15:32 +01002537#if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 if (dev->status_ep) {
2539 dev->stat_req = eth_req_alloc (dev->status_ep,
David Brownell907cba32005-04-28 13:48:09 -07002540 STATUS_BYTECOUNT, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541 if (!dev->stat_req) {
2542 eth_req_free (gadget->ep0, dev->req);
2543 goto fail;
2544 }
David Brownell907cba32005-04-28 13:48:09 -07002545 dev->stat_req->context = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 }
David Brownell822e14a2005-06-13 06:55:03 -07002547#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548
2549 /* finish hookup to lower layer ... */
2550 dev->gadget = gadget;
2551 set_gadget_data (gadget, dev);
2552 gadget->ep0->driver_data = dev;
David Brownell7e27f182006-06-13 09:54:40 -07002553
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 /* two kinds of host-initiated state changes:
2555 * - iff DATA transfer is active, carrier is "on"
2556 * - tx queueing enabled if open *and* carrier is "on"
2557 */
2558 netif_stop_queue (dev->net);
2559 netif_carrier_off (dev->net);
2560
David Brownell7e27f182006-06-13 09:54:40 -07002561 SET_NETDEV_DEV (dev->net, &gadget->dev);
2562 status = register_netdev (dev->net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 if (status < 0)
2564 goto fail1;
2565
2566 INFO (dev, "%s, version: " DRIVER_VERSION "\n", driver_desc);
2567 INFO (dev, "using %s, OUT %s IN %s%s%s\n", gadget->name,
David Brownell907cba32005-04-28 13:48:09 -07002568 out_ep->name, in_ep->name,
2569 status_ep ? " STATUS " : "",
2570 status_ep ? status_ep->name : ""
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 );
2572 INFO (dev, "MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
2573 net->dev_addr [0], net->dev_addr [1],
2574 net->dev_addr [2], net->dev_addr [3],
2575 net->dev_addr [4], net->dev_addr [5]);
2576
2577 if (cdc || rndis)
2578 INFO (dev, "HOST MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
2579 dev->host_mac [0], dev->host_mac [1],
2580 dev->host_mac [2], dev->host_mac [3],
2581 dev->host_mac [4], dev->host_mac [5]);
2582
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 if (rndis) {
2584 u32 vendorID = 0;
2585
2586 /* FIXME RNDIS vendor id == "vendor NIC code" == ? */
David Brownell7e27f182006-06-13 09:54:40 -07002587
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 dev->rndis_config = rndis_register (rndis_control_ack);
2589 if (dev->rndis_config < 0) {
2590fail0:
2591 unregister_netdev (dev->net);
2592 status = -ENODEV;
2593 goto fail;
2594 }
David Brownell7e27f182006-06-13 09:54:40 -07002595
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 /* these set up a lot of the OIDs that RNDIS needs */
2597 rndis_set_host_mac (dev->rndis_config, dev->host_mac);
2598 if (rndis_set_param_dev (dev->rndis_config, dev->net,
David Brownell340600a2005-04-28 13:45:25 -07002599 &dev->stats, &dev->cdc_filter))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 goto fail0;
2601 if (rndis_set_param_vendor (dev->rndis_config, vendorID,
2602 manufacturer))
2603 goto fail0;
2604 if (rndis_set_param_medium (dev->rndis_config,
2605 NDIS_MEDIUM_802_3,
2606 0))
2607 goto fail0;
2608 INFO (dev, "RNDIS ready\n");
2609 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610
2611 return status;
2612
2613fail1:
2614 dev_dbg(&gadget->dev, "register_netdev failed, %d\n", status);
2615fail:
2616 eth_unbind (gadget);
2617 return status;
2618}
2619
2620/*-------------------------------------------------------------------------*/
2621
2622static void
2623eth_suspend (struct usb_gadget *gadget)
2624{
2625 struct eth_dev *dev = get_gadget_data (gadget);
2626
2627 DEBUG (dev, "suspend\n");
2628 dev->suspended = 1;
2629}
2630
2631static void
2632eth_resume (struct usb_gadget *gadget)
2633{
2634 struct eth_dev *dev = get_gadget_data (gadget);
2635
2636 DEBUG (dev, "resume\n");
2637 dev->suspended = 0;
2638}
2639
2640/*-------------------------------------------------------------------------*/
2641
2642static struct usb_gadget_driver eth_driver = {
David Brownell907cba32005-04-28 13:48:09 -07002643 .speed = DEVSPEED,
2644
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645 .function = (char *) driver_desc,
2646 .bind = eth_bind,
Tony Lindgrenbfb2c962006-06-29 22:27:36 -07002647 .unbind = eth_unbind,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648
2649 .setup = eth_setup,
2650 .disconnect = eth_disconnect,
2651
2652 .suspend = eth_suspend,
2653 .resume = eth_resume,
2654
David Brownell7e27f182006-06-13 09:54:40 -07002655 .driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 .name = (char *) shortname,
Ben Dooksd0d50492005-10-10 10:52:33 +01002657 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 },
2659};
2660
2661MODULE_DESCRIPTION (DRIVER_DESC);
2662MODULE_AUTHOR ("David Brownell, Benedikt Spanger");
2663MODULE_LICENSE ("GPL");
2664
2665
2666static int __init init (void)
2667{
2668 return usb_gadget_register_driver (&eth_driver);
2669}
2670module_init (init);
2671
2672static void __exit cleanup (void)
2673{
2674 usb_gadget_unregister_driver (&eth_driver);
2675}
2676module_exit (cleanup);
2677