blob: 8d61ea67a8174a801d1ee0f8e902b2239ff53b70 [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
David Brownell0cf4f2d2007-08-02 00:00:38 -070022/* #define VERBOSE_DEBUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/utsname.h>
26#include <linux/device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/ctype.h>
David Brownell0cf4f2d2007-08-02 00:00:38 -070028#include <linux/etherdevice.h>
29#include <linux/ethtool.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
David Brownell5f848132006-12-16 15:34:53 -080031#include <linux/usb/ch9.h>
David Brownella8c28f22006-06-13 09:57:47 -070032#include <linux/usb/cdc.h>
David Brownell9454a572007-10-04 18:05:17 -070033#include <linux/usb/gadget.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "gadget_chips.h"
36
37/*-------------------------------------------------------------------------*/
38
39/*
40 * Ethernet gadget driver -- with CDC and non-CDC options
41 * Builds on hardware support for a full duplex link.
42 *
43 * CDC Ethernet is the standard USB solution for sending Ethernet frames
44 * using USB. Real hardware tends to use the same framing protocol but look
45 * different for control features. This driver strongly prefers to use
46 * this USB-IF standard as its open-systems interoperability solution;
47 * most host side USB stacks (except from Microsoft) support it.
48 *
49 * There's some hardware that can't talk CDC. We make that hardware
50 * implement a "minimalist" vendor-agnostic CDC core: same framing, but
David Brownell11d54892006-12-11 15:59:04 -080051 * link-level setup only requires activating the configuration. Only the
52 * endpoint descriptors, and product/vendor IDs, are relevant; no control
53 * operations are available. Linux supports it, but other host operating
54 * systems may not. (This is a subset of CDC Ethernet.)
55 *
56 * It turns out that if you add a few descriptors to that "CDC Subset",
57 * (Windows) host side drivers from MCCI can treat it as one submode of
58 * a proprietary scheme called "SAFE" ... without needing to know about
59 * specific product/vendor IDs. So we do that, making it easier to use
60 * those MS-Windows drivers. Those added descriptors make it resemble a
61 * CDC MDLM device, but they don't change device behavior at all. (See
62 * MCCI Engineering report 950198 "SAFE Networking Functions".)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 *
64 * A third option is also in use. Rather than CDC Ethernet, or something
65 * simpler, Microsoft pushes their own approach: RNDIS. The published
66 * RNDIS specs are ambiguous and appear to be incomplete, and are also
67 * needlessly complex.
68 */
69
70#define DRIVER_DESC "Ethernet Gadget"
David Brownell907cba32005-04-28 13:48:09 -070071#define DRIVER_VERSION "May Day 2005"
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73static const char shortname [] = "ether";
74static const char driver_desc [] = DRIVER_DESC;
75
76#define RX_EXTRA 20 /* guard against rx overflows */
77
Linus Torvalds1da177e2005-04-16 15:20:36 -070078#include "rndis.h"
David Brownell45e45ab2005-05-16 08:26:38 -070079
80#ifndef CONFIG_USB_ETH_RNDIS
81#define rndis_uninit(x) do{}while(0)
82#define rndis_deregister(c) do{}while(0)
83#define rndis_exit() do{}while(0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070084#endif
85
86/* CDC and RNDIS support the same host-chosen outgoing packet filters. */
87#define DEFAULT_FILTER (USB_CDC_PACKET_TYPE_BROADCAST \
David Brownell7e27f182006-06-13 09:54:40 -070088 |USB_CDC_PACKET_TYPE_ALL_MULTICAST \
89 |USB_CDC_PACKET_TYPE_PROMISCUOUS \
90 |USB_CDC_PACKET_TYPE_DIRECTED)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92
93/*-------------------------------------------------------------------------*/
94
95struct eth_dev {
96 spinlock_t lock;
97 struct usb_gadget *gadget;
98 struct usb_request *req; /* for control responses */
99 struct usb_request *stat_req; /* for cdc & rndis status */
100
101 u8 config;
102 struct usb_ep *in_ep, *out_ep, *status_ep;
103 const struct usb_endpoint_descriptor
104 *in, *out, *status;
David Brownell789851c2006-08-21 15:26:38 -0700105
106 spinlock_t req_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 struct list_head tx_reqs, rx_reqs;
108
109 struct net_device *net;
110 struct net_device_stats stats;
111 atomic_t tx_qlen;
112
113 struct work_struct work;
114 unsigned zlp:1;
115 unsigned cdc:1;
116 unsigned rndis:1;
117 unsigned suspended:1;
118 u16 cdc_filter;
119 unsigned long todo;
120#define WORK_RX_MEMORY 0
121 int rndis_config;
122 u8 host_mac [ETH_ALEN];
123};
124
125/* This version autoconfigures as much as possible at run-time.
126 *
127 * It also ASSUMES a self-powered device, without remote wakeup,
128 * although remote wakeup support would make sense.
129 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
131/*-------------------------------------------------------------------------*/
132
133/* DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
134 * Instead: allocate your own, using normal USB-IF procedures.
135 */
136
137/* Thanks to NetChip Technologies for donating this product ID.
138 * It's for devices with only CDC Ethernet configurations.
139 */
140#define CDC_VENDOR_NUM 0x0525 /* NetChip */
141#define CDC_PRODUCT_NUM 0xa4a1 /* Linux-USB Ethernet Gadget */
142
143/* For hardware that can't talk CDC, we use the same vendor ID that
144 * ARM Linux has used for ethernet-over-usb, both with sa1100 and
145 * with pxa250. We're protocol-compatible, if the host-side drivers
146 * use the endpoint descriptors. bcdDevice (version) is nonzero, so
147 * drivers that need to hard-wire endpoint numbers have a hook.
148 *
149 * The protocol is a minimal subset of CDC Ether, which works on any bulk
150 * hardware that's not deeply broken ... even on hardware that can't talk
151 * RNDIS (like SA-1100, with no interrupt endpoint, or anything that
152 * doesn't handle control-OUT).
153 */
154#define SIMPLE_VENDOR_NUM 0x049f
155#define SIMPLE_PRODUCT_NUM 0x505a
156
157/* For hardware that can talk RNDIS and either of the above protocols,
158 * use this ID ... the windows INF files will know it. Unless it's
159 * used with CDC Ethernet, Linux 2.4 hosts will need updates to choose
160 * the non-RNDIS configuration.
161 */
162#define RNDIS_VENDOR_NUM 0x0525 /* NetChip */
163#define RNDIS_PRODUCT_NUM 0xa4a2 /* Ethernet/RNDIS Gadget */
164
165
166/* Some systems will want different product identifers published in the
167 * device descriptor, either numbers or strings or both. These string
168 * parameters are in UTF-8 (superset of ASCII's 7 bit characters).
169 */
170
Aras Vaichas1afc64a2006-02-18 12:31:23 -0800171static ushort idVendor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172module_param(idVendor, ushort, S_IRUGO);
173MODULE_PARM_DESC(idVendor, "USB Vendor ID");
174
Aras Vaichas1afc64a2006-02-18 12:31:23 -0800175static ushort idProduct;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176module_param(idProduct, ushort, S_IRUGO);
177MODULE_PARM_DESC(idProduct, "USB Product ID");
178
Aras Vaichas1afc64a2006-02-18 12:31:23 -0800179static ushort bcdDevice;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180module_param(bcdDevice, ushort, S_IRUGO);
181MODULE_PARM_DESC(bcdDevice, "USB Device version (BCD)");
182
Aras Vaichas1afc64a2006-02-18 12:31:23 -0800183static char *iManufacturer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184module_param(iManufacturer, charp, S_IRUGO);
185MODULE_PARM_DESC(iManufacturer, "USB Manufacturer string");
186
Aras Vaichas1afc64a2006-02-18 12:31:23 -0800187static char *iProduct;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188module_param(iProduct, charp, S_IRUGO);
189MODULE_PARM_DESC(iProduct, "USB Product string");
190
Aras Vaichas1afc64a2006-02-18 12:31:23 -0800191static char *iSerialNumber;
192module_param(iSerialNumber, charp, S_IRUGO);
193MODULE_PARM_DESC(iSerialNumber, "SerialNumber");
194
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195/* initial value, changed by "ifconfig usb0 hw ether xx:xx:xx:xx:xx:xx" */
Aras Vaichas1afc64a2006-02-18 12:31:23 -0800196static char *dev_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197module_param(dev_addr, charp, S_IRUGO);
198MODULE_PARM_DESC(dev_addr, "Device Ethernet Address");
199
200/* this address is invisible to ifconfig */
Aras Vaichas1afc64a2006-02-18 12:31:23 -0800201static char *host_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202module_param(host_addr, charp, S_IRUGO);
203MODULE_PARM_DESC(host_addr, "Host Ethernet Address");
204
205
206/*-------------------------------------------------------------------------*/
207
208/* Include CDC support if we could run on CDC-capable hardware. */
209
210#ifdef CONFIG_USB_GADGET_NET2280
211#define DEV_CONFIG_CDC
212#endif
213
214#ifdef CONFIG_USB_GADGET_DUMMY_HCD
215#define DEV_CONFIG_CDC
216#endif
217
218#ifdef CONFIG_USB_GADGET_GOKU
219#define DEV_CONFIG_CDC
220#endif
221
222#ifdef CONFIG_USB_GADGET_LH7A40X
223#define DEV_CONFIG_CDC
224#endif
225
226#ifdef CONFIG_USB_GADGET_MQ11XX
227#define DEV_CONFIG_CDC
228#endif
229
230#ifdef CONFIG_USB_GADGET_OMAP
231#define DEV_CONFIG_CDC
232#endif
233
234#ifdef CONFIG_USB_GADGET_N9604
235#define DEV_CONFIG_CDC
236#endif
237
David Brownell11d54892006-12-11 15:59:04 -0800238#ifdef CONFIG_USB_GADGET_S3C2410
239#define DEV_CONFIG_CDC
240#endif
241
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242#ifdef CONFIG_USB_GADGET_AT91
243#define DEV_CONFIG_CDC
244#endif
245
David Brownell1c05ad42006-01-25 08:45:59 -0800246#ifdef CONFIG_USB_GADGET_MUSBHSFC
247#define DEV_CONFIG_CDC
248#endif
249
Tony Lindgrenbfb2c962006-06-29 22:27:36 -0700250#ifdef CONFIG_USB_GADGET_MUSB_HDRC
David Brownell1c05ad42006-01-25 08:45:59 -0800251#define DEV_CONFIG_CDC
252#endif
253
Haavard Skinnemoen55b3fd42007-06-14 18:01:45 +0200254#ifdef CONFIG_USB_GADGET_ATMEL_USBA
HÃ¥vard Skinnemoenef3ff462006-02-27 18:15:04 +0100255#define DEV_CONFIG_CDC
256#endif
257
Li Yangd2eef1f2007-04-23 10:37:36 -0700258#ifdef CONFIG_USB_GADGET_FSL_USB2
259#define DEV_CONFIG_CDC
260#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
262/* For CDC-incapable hardware, choose the simple cdc subset.
263 * Anything that talks bulk (without notable bugs) can do this.
264 */
265#ifdef CONFIG_USB_GADGET_PXA2XX
266#define DEV_CONFIG_SUBSET
267#endif
268
Robert Jarzmikd75379a2008-04-18 15:56:49 -0700269#ifdef CONFIG_USB_GADGET_PXA27X
270#define DEV_CONFIG_SUBSET
271#endif
272
David Brownell7f9985c2007-05-08 21:01:30 -0700273#ifdef CONFIG_USB_GADGET_SUPERH
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274#define DEV_CONFIG_SUBSET
275#endif
276
277#ifdef CONFIG_USB_GADGET_SA1100
278/* use non-CDC for backwards compatibility */
279#define DEV_CONFIG_SUBSET
280#endif
281
Yoshihiro Shimoda4cf25032007-05-10 13:18:23 +0900282#ifdef CONFIG_USB_GADGET_M66592
283#define DEV_CONFIG_CDC
284#endif
285
Thomas Dahlmann55d402d2007-07-16 21:40:54 -0700286#ifdef CONFIG_USB_GADGET_AMD5536UDC
287#define DEV_CONFIG_CDC
288#endif
289
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
291/*-------------------------------------------------------------------------*/
292
293/* "main" config is either CDC, or its simple subset */
294static inline int is_cdc(struct eth_dev *dev)
295{
296#if !defined(DEV_CONFIG_SUBSET)
297 return 1; /* only cdc possible */
298#elif !defined (DEV_CONFIG_CDC)
299 return 0; /* only subset possible */
300#else
301 return dev->cdc; /* depends on what hardware we found */
302#endif
303}
304
305/* "secondary" RNDIS config may sometimes be activated */
306static inline int rndis_active(struct eth_dev *dev)
307{
308#ifdef CONFIG_USB_ETH_RNDIS
309 return dev->rndis;
310#else
311 return 0;
312#endif
313}
314
315#define subset_active(dev) (!is_cdc(dev) && !rndis_active(dev))
316#define cdc_active(dev) ( is_cdc(dev) && !rndis_active(dev))
317
318
319
320#define DEFAULT_QLEN 2 /* double buffering by default */
321
322/* peak bulk transfer bits-per-second */
David Brownell7e27f182006-06-13 09:54:40 -0700323#define HS_BPS (13 * 512 * 8 * 1000 * 8)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324#define FS_BPS (19 * 64 * 1 * 1000 * 8)
325
326#ifdef CONFIG_USB_GADGET_DUALSPEED
David Brownell907cba32005-04-28 13:48:09 -0700327#define DEVSPEED USB_SPEED_HIGH
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
329static unsigned qmult = 5;
330module_param (qmult, uint, S_IRUGO|S_IWUSR);
331
332
333/* for dual-speed hardware, use deeper queues at highspeed */
334#define qlen(gadget) \
335 (DEFAULT_QLEN*((gadget->speed == USB_SPEED_HIGH) ? qmult : 1))
336
David Brownell6cdee102005-04-18 17:39:34 -0700337static inline int BITRATE(struct usb_gadget *g)
338{
339 return (g->speed == USB_SPEED_HIGH) ? HS_BPS : FS_BPS;
340}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
342#else /* full speed (low speed doesn't do bulk) */
David Brownell0cf4f2d2007-08-02 00:00:38 -0700343
344#define qmult 1
345
David Brownell907cba32005-04-28 13:48:09 -0700346#define DEVSPEED USB_SPEED_FULL
347
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348#define qlen(gadget) DEFAULT_QLEN
349
David Brownell6cdee102005-04-18 17:39:34 -0700350static inline int BITRATE(struct usb_gadget *g)
351{
352 return FS_BPS;
353}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354#endif
355
356
357/*-------------------------------------------------------------------------*/
358
359#define xprintk(d,level,fmt,args...) \
360 printk(level "%s: " fmt , (d)->net->name , ## args)
361
362#ifdef DEBUG
363#undef DEBUG
364#define DEBUG(dev,fmt,args...) \
365 xprintk(dev , KERN_DEBUG , fmt , ## args)
366#else
367#define DEBUG(dev,fmt,args...) \
368 do { } while (0)
369#endif /* DEBUG */
370
David Brownell0cf4f2d2007-08-02 00:00:38 -0700371#ifdef VERBOSE_DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372#define VDEBUG DEBUG
373#else
374#define VDEBUG(dev,fmt,args...) \
375 do { } while (0)
376#endif /* DEBUG */
377
378#define ERROR(dev,fmt,args...) \
379 xprintk(dev , KERN_ERR , fmt , ## args)
380#define WARN(dev,fmt,args...) \
381 xprintk(dev , KERN_WARNING , fmt , ## args)
382#define INFO(dev,fmt,args...) \
383 xprintk(dev , KERN_INFO , fmt , ## args)
384
385/*-------------------------------------------------------------------------*/
386
387/* USB DRIVER HOOKUP (to the hardware driver, below us), mostly
388 * ep0 implementation: descriptors, config management, setup().
389 * also optional class-specific notification interrupt transfer.
390 */
391
392/*
393 * DESCRIPTORS ... most are static, but strings and (full) configuration
394 * descriptors are built on demand. For now we do either full CDC, or
395 * our simple subset, with RNDIS as an optional second configuration.
396 *
397 * RNDIS includes some CDC ACM descriptors ... like CDC Ethernet. But
398 * the class descriptors match a modem (they're ignored; it's really just
399 * Ethernet functionality), they don't need the NOP altsetting, and the
400 * status transfer endpoint isn't optional.
401 */
402
403#define STRING_MANUFACTURER 1
404#define STRING_PRODUCT 2
405#define STRING_ETHADDR 3
406#define STRING_DATA 4
407#define STRING_CONTROL 5
408#define STRING_RNDIS_CONTROL 6
409#define STRING_CDC 7
410#define STRING_SUBSET 8
411#define STRING_RNDIS 9
Aras Vaichas1afc64a2006-02-18 12:31:23 -0800412#define STRING_SERIALNUMBER 10
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
David Brownell340600a2005-04-28 13:45:25 -0700414/* holds our biggest descriptor (or RNDIS response) */
415#define USB_BUFSIZ 256
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
417/*
418 * This device advertises one configuration, eth_config, unless RNDIS
419 * is enabled (rndis_config) on hardware supporting at least two configs.
420 *
421 * NOTE: Controllers like superh_udc should probably be able to use
422 * an RNDIS-only configuration.
423 *
424 * FIXME define some higher-powered configurations to make it easier
425 * to recharge batteries ...
426 */
427
428#define DEV_CONFIG_VALUE 1 /* cdc or subset */
429#define DEV_RNDIS_CONFIG_VALUE 2 /* rndis; optional */
430
431static struct usb_device_descriptor
432device_desc = {
433 .bLength = sizeof device_desc,
434 .bDescriptorType = USB_DT_DEVICE,
435
436 .bcdUSB = __constant_cpu_to_le16 (0x0200),
437
438 .bDeviceClass = USB_CLASS_COMM,
439 .bDeviceSubClass = 0,
440 .bDeviceProtocol = 0,
441
442 .idVendor = __constant_cpu_to_le16 (CDC_VENDOR_NUM),
443 .idProduct = __constant_cpu_to_le16 (CDC_PRODUCT_NUM),
444 .iManufacturer = STRING_MANUFACTURER,
445 .iProduct = STRING_PRODUCT,
446 .bNumConfigurations = 1,
447};
448
449static struct usb_otg_descriptor
450otg_descriptor = {
451 .bLength = sizeof otg_descriptor,
452 .bDescriptorType = USB_DT_OTG,
453
454 .bmAttributes = USB_OTG_SRP,
455};
456
457static struct usb_config_descriptor
458eth_config = {
459 .bLength = sizeof eth_config,
460 .bDescriptorType = USB_DT_CONFIG,
461
462 /* compute wTotalLength on the fly */
463 .bNumInterfaces = 2,
464 .bConfigurationValue = DEV_CONFIG_VALUE,
465 .iConfiguration = STRING_CDC,
466 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
467 .bMaxPower = 50,
468};
469
470#ifdef CONFIG_USB_ETH_RNDIS
David Brownell7e27f182006-06-13 09:54:40 -0700471static struct usb_config_descriptor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472rndis_config = {
473 .bLength = sizeof rndis_config,
474 .bDescriptorType = USB_DT_CONFIG,
475
476 /* compute wTotalLength on the fly */
477 .bNumInterfaces = 2,
478 .bConfigurationValue = DEV_RNDIS_CONFIG_VALUE,
479 .iConfiguration = STRING_RNDIS,
480 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
481 .bMaxPower = 50,
482};
483#endif
484
485/*
486 * Compared to the simple CDC subset, the full CDC Ethernet model adds
487 * three class descriptors, two interface descriptors, optional status
488 * endpoint. Both have a "data" interface and two bulk endpoints.
489 * There are also differences in how control requests are handled.
490 *
David Brownell11d54892006-12-11 15:59:04 -0800491 * RNDIS shares a lot with CDC-Ethernet, since it's a variant of the
492 * CDC-ACM (modem) spec. Unfortunately MSFT's RNDIS driver is buggy; it
493 * may hang or oops. Since bugfixes (or accurate specs, letting Linux
494 * work around those bugs) are unlikely to ever come from MSFT, you may
495 * wish to avoid using RNDIS.
496 *
497 * MCCI offers an alternative to RNDIS if you need to connect to Windows
498 * but have hardware that can't support CDC Ethernet. We add descriptors
499 * to present the CDC Subset as a (nonconformant) CDC MDLM variant called
500 * "SAFE". That borrows from both CDC Ethernet and CDC MDLM. You can
501 * get those drivers from MCCI, or bundled with various products.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 */
503
504#ifdef DEV_CONFIG_CDC
505static struct usb_interface_descriptor
506control_intf = {
507 .bLength = sizeof control_intf,
508 .bDescriptorType = USB_DT_INTERFACE,
509
510 .bInterfaceNumber = 0,
511 /* status endpoint is optional; this may be patched later */
512 .bNumEndpoints = 1,
513 .bInterfaceClass = USB_CLASS_COMM,
514 .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET,
515 .bInterfaceProtocol = USB_CDC_PROTO_NONE,
516 .iInterface = STRING_CONTROL,
517};
518#endif
519
520#ifdef CONFIG_USB_ETH_RNDIS
521static const struct usb_interface_descriptor
522rndis_control_intf = {
523 .bLength = sizeof rndis_control_intf,
524 .bDescriptorType = USB_DT_INTERFACE,
David Brownell7e27f182006-06-13 09:54:40 -0700525
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 .bInterfaceNumber = 0,
527 .bNumEndpoints = 1,
528 .bInterfaceClass = USB_CLASS_COMM,
529 .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
530 .bInterfaceProtocol = USB_CDC_ACM_PROTO_VENDOR,
531 .iInterface = STRING_RNDIS_CONTROL,
532};
533#endif
534
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535static const struct usb_cdc_header_desc header_desc = {
536 .bLength = sizeof header_desc,
537 .bDescriptorType = USB_DT_CS_INTERFACE,
538 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
539
540 .bcdCDC = __constant_cpu_to_le16 (0x0110),
541};
542
David Brownell11d54892006-12-11 15:59:04 -0800543#if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
544
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545static const struct usb_cdc_union_desc union_desc = {
546 .bLength = sizeof union_desc,
547 .bDescriptorType = USB_DT_CS_INTERFACE,
548 .bDescriptorSubType = USB_CDC_UNION_TYPE,
549
550 .bMasterInterface0 = 0, /* index of control interface */
551 .bSlaveInterface0 = 1, /* index of DATA interface */
552};
553
554#endif /* CDC || RNDIS */
555
556#ifdef CONFIG_USB_ETH_RNDIS
557
558static const struct usb_cdc_call_mgmt_descriptor call_mgmt_descriptor = {
David Brownell7e27f182006-06-13 09:54:40 -0700559 .bLength = sizeof call_mgmt_descriptor,
560 .bDescriptorType = USB_DT_CS_INTERFACE,
561 .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
David Brownell7e27f182006-06-13 09:54:40 -0700563 .bmCapabilities = 0x00,
564 .bDataInterface = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565};
566
David Brownell907cba32005-04-28 13:48:09 -0700567static const struct usb_cdc_acm_descriptor acm_descriptor = {
David Brownell7e27f182006-06-13 09:54:40 -0700568 .bLength = sizeof acm_descriptor,
569 .bDescriptorType = USB_DT_CS_INTERFACE,
570 .bDescriptorSubType = USB_CDC_ACM_TYPE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
David Brownell7e27f182006-06-13 09:54:40 -0700572 .bmCapabilities = 0x00,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573};
574
575#endif
576
David Brownell11d54892006-12-11 15:59:04 -0800577#ifndef DEV_CONFIG_CDC
578
579/* "SAFE" loosely follows CDC WMC MDLM, violating the spec in various
580 * ways: data endpoints live in the control interface, there's no data
581 * interface, and it's not used to talk to a cell phone radio.
582 */
583
584static const struct usb_cdc_mdlm_desc mdlm_desc = {
585 .bLength = sizeof mdlm_desc,
586 .bDescriptorType = USB_DT_CS_INTERFACE,
587 .bDescriptorSubType = USB_CDC_MDLM_TYPE,
588
589 .bcdVersion = __constant_cpu_to_le16(0x0100),
590 .bGUID = {
591 0x5d, 0x34, 0xcf, 0x66, 0x11, 0x18, 0x11, 0xd6,
592 0xa2, 0x1a, 0x00, 0x01, 0x02, 0xca, 0x9a, 0x7f,
593 },
594};
595
596/* since "usb_cdc_mdlm_detail_desc" is a variable length structure, we
597 * can't really use its struct. All we do here is say that we're using
598 * the submode of "SAFE" which directly matches the CDC Subset.
599 */
600static const u8 mdlm_detail_desc[] = {
601 6,
602 USB_DT_CS_INTERFACE,
603 USB_CDC_MDLM_DETAIL_TYPE,
604
605 0, /* "SAFE" */
606 0, /* network control capabilities (none) */
607 0, /* network data capabilities ("raw" encapsulation) */
608};
609
610#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
612static const struct usb_cdc_ether_desc ether_desc = {
613 .bLength = sizeof ether_desc,
614 .bDescriptorType = USB_DT_CS_INTERFACE,
615 .bDescriptorSubType = USB_CDC_ETHERNET_TYPE,
616
617 /* this descriptor actually adds value, surprise! */
618 .iMACAddress = STRING_ETHADDR,
619 .bmEthernetStatistics = __constant_cpu_to_le32 (0), /* no statistics */
620 .wMaxSegmentSize = __constant_cpu_to_le16 (ETH_FRAME_LEN),
621 .wNumberMCFilters = __constant_cpu_to_le16 (0),
622 .bNumberPowerFilters = 0,
623};
624
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
626#if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
627
628/* include the status endpoint if we can, even where it's optional.
629 * use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one
Steven Cole093cf722005-05-03 19:07:24 -0600630 * packet, to simplify cancellation; and a big transfer interval, to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 * waste less bandwidth.
632 *
633 * some drivers (like Linux 2.4 cdc-ether!) "need" it to exist even
634 * if they ignore the connect/disconnect notifications that real aether
635 * can provide. more advanced cdc configurations might want to support
636 * encapsulated commands (vendor-specific, using control-OUT).
637 *
638 * RNDIS requires the status endpoint, since it uses that encapsulation
639 * mechanism for its funky RPC scheme.
640 */
David Brownell7e27f182006-06-13 09:54:40 -0700641
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642#define LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */
643#define STATUS_BYTECOUNT 16 /* 8 byte header + data */
644
645static struct usb_endpoint_descriptor
646fs_status_desc = {
647 .bLength = USB_DT_ENDPOINT_SIZE,
648 .bDescriptorType = USB_DT_ENDPOINT,
649
650 .bEndpointAddress = USB_DIR_IN,
651 .bmAttributes = USB_ENDPOINT_XFER_INT,
652 .wMaxPacketSize = __constant_cpu_to_le16 (STATUS_BYTECOUNT),
653 .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC,
654};
655#endif
656
657#ifdef DEV_CONFIG_CDC
658
659/* the default data interface has no endpoints ... */
660
661static const struct usb_interface_descriptor
662data_nop_intf = {
663 .bLength = sizeof data_nop_intf,
664 .bDescriptorType = USB_DT_INTERFACE,
665
666 .bInterfaceNumber = 1,
667 .bAlternateSetting = 0,
668 .bNumEndpoints = 0,
669 .bInterfaceClass = USB_CLASS_CDC_DATA,
670 .bInterfaceSubClass = 0,
671 .bInterfaceProtocol = 0,
672};
673
674/* ... but the "real" data interface has two bulk endpoints */
675
676static const struct usb_interface_descriptor
677data_intf = {
678 .bLength = sizeof data_intf,
679 .bDescriptorType = USB_DT_INTERFACE,
680
681 .bInterfaceNumber = 1,
682 .bAlternateSetting = 1,
683 .bNumEndpoints = 2,
684 .bInterfaceClass = USB_CLASS_CDC_DATA,
685 .bInterfaceSubClass = 0,
686 .bInterfaceProtocol = 0,
687 .iInterface = STRING_DATA,
688};
689
690#endif
691
692#ifdef CONFIG_USB_ETH_RNDIS
693
694/* RNDIS doesn't activate by changing to the "real" altsetting */
695
696static const struct usb_interface_descriptor
697rndis_data_intf = {
698 .bLength = sizeof rndis_data_intf,
699 .bDescriptorType = USB_DT_INTERFACE,
700
701 .bInterfaceNumber = 1,
702 .bAlternateSetting = 0,
703 .bNumEndpoints = 2,
704 .bInterfaceClass = USB_CLASS_CDC_DATA,
705 .bInterfaceSubClass = 0,
706 .bInterfaceProtocol = 0,
707 .iInterface = STRING_DATA,
708};
709
710#endif
711
712#ifdef DEV_CONFIG_SUBSET
713
714/*
715 * "Simple" CDC-subset option is a simple vendor-neutral model that most
716 * full speed controllers can handle: one interface, two bulk endpoints.
David Brownell11d54892006-12-11 15:59:04 -0800717 *
718 * To assist host side drivers, we fancy it up a bit, and add descriptors
719 * so some host side drivers will understand it as a "SAFE" variant.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 */
721
722static const struct usb_interface_descriptor
723subset_data_intf = {
724 .bLength = sizeof subset_data_intf,
725 .bDescriptorType = USB_DT_INTERFACE,
726
727 .bInterfaceNumber = 0,
728 .bAlternateSetting = 0,
729 .bNumEndpoints = 2,
David Brownell11d54892006-12-11 15:59:04 -0800730 .bInterfaceClass = USB_CLASS_COMM,
731 .bInterfaceSubClass = USB_CDC_SUBCLASS_MDLM,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 .bInterfaceProtocol = 0,
733 .iInterface = STRING_DATA,
734};
735
736#endif /* SUBSET */
737
738
739static struct usb_endpoint_descriptor
740fs_source_desc = {
741 .bLength = USB_DT_ENDPOINT_SIZE,
742 .bDescriptorType = USB_DT_ENDPOINT,
743
744 .bEndpointAddress = USB_DIR_IN,
745 .bmAttributes = USB_ENDPOINT_XFER_BULK,
746};
747
748static struct usb_endpoint_descriptor
749fs_sink_desc = {
750 .bLength = USB_DT_ENDPOINT_SIZE,
751 .bDescriptorType = USB_DT_ENDPOINT,
752
753 .bEndpointAddress = USB_DIR_OUT,
754 .bmAttributes = USB_ENDPOINT_XFER_BULK,
755};
756
757static const struct usb_descriptor_header *fs_eth_function [11] = {
758 (struct usb_descriptor_header *) &otg_descriptor,
759#ifdef DEV_CONFIG_CDC
760 /* "cdc" mode descriptors */
761 (struct usb_descriptor_header *) &control_intf,
762 (struct usb_descriptor_header *) &header_desc,
763 (struct usb_descriptor_header *) &union_desc,
764 (struct usb_descriptor_header *) &ether_desc,
765 /* NOTE: status endpoint may need to be removed */
766 (struct usb_descriptor_header *) &fs_status_desc,
767 /* data interface, with altsetting */
768 (struct usb_descriptor_header *) &data_nop_intf,
769 (struct usb_descriptor_header *) &data_intf,
770 (struct usb_descriptor_header *) &fs_source_desc,
771 (struct usb_descriptor_header *) &fs_sink_desc,
772 NULL,
773#endif /* DEV_CONFIG_CDC */
774};
775
776static inline void __init fs_subset_descriptors(void)
777{
778#ifdef DEV_CONFIG_SUBSET
David Brownell11d54892006-12-11 15:59:04 -0800779 /* behavior is "CDC Subset"; extra descriptors say "SAFE" */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 fs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
David Brownell11d54892006-12-11 15:59:04 -0800781 fs_eth_function[2] = (struct usb_descriptor_header *) &header_desc;
782 fs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc;
783 fs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc;
784 fs_eth_function[5] = (struct usb_descriptor_header *) &ether_desc;
785 fs_eth_function[6] = (struct usb_descriptor_header *) &fs_source_desc;
786 fs_eth_function[7] = (struct usb_descriptor_header *) &fs_sink_desc;
787 fs_eth_function[8] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788#else
789 fs_eth_function[1] = NULL;
790#endif
791}
792
793#ifdef CONFIG_USB_ETH_RNDIS
794static const struct usb_descriptor_header *fs_rndis_function [] = {
795 (struct usb_descriptor_header *) &otg_descriptor,
796 /* control interface matches ACM, not Ethernet */
797 (struct usb_descriptor_header *) &rndis_control_intf,
798 (struct usb_descriptor_header *) &header_desc,
799 (struct usb_descriptor_header *) &call_mgmt_descriptor,
800 (struct usb_descriptor_header *) &acm_descriptor,
801 (struct usb_descriptor_header *) &union_desc,
802 (struct usb_descriptor_header *) &fs_status_desc,
803 /* data interface has no altsetting */
804 (struct usb_descriptor_header *) &rndis_data_intf,
805 (struct usb_descriptor_header *) &fs_source_desc,
806 (struct usb_descriptor_header *) &fs_sink_desc,
807 NULL,
808};
809#endif
810
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811/*
812 * usb 2.0 devices need to expose both high speed and full speed
813 * descriptors, unless they only run at full speed.
814 */
815
816#if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
817static struct usb_endpoint_descriptor
818hs_status_desc = {
819 .bLength = USB_DT_ENDPOINT_SIZE,
820 .bDescriptorType = USB_DT_ENDPOINT,
821
822 .bmAttributes = USB_ENDPOINT_XFER_INT,
823 .wMaxPacketSize = __constant_cpu_to_le16 (STATUS_BYTECOUNT),
824 .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4,
825};
826#endif /* DEV_CONFIG_CDC */
827
828static struct usb_endpoint_descriptor
829hs_source_desc = {
830 .bLength = USB_DT_ENDPOINT_SIZE,
831 .bDescriptorType = USB_DT_ENDPOINT,
832
833 .bmAttributes = USB_ENDPOINT_XFER_BULK,
834 .wMaxPacketSize = __constant_cpu_to_le16 (512),
835};
836
837static struct usb_endpoint_descriptor
838hs_sink_desc = {
839 .bLength = USB_DT_ENDPOINT_SIZE,
840 .bDescriptorType = USB_DT_ENDPOINT,
841
842 .bmAttributes = USB_ENDPOINT_XFER_BULK,
843 .wMaxPacketSize = __constant_cpu_to_le16 (512),
844};
845
846static struct usb_qualifier_descriptor
847dev_qualifier = {
848 .bLength = sizeof dev_qualifier,
849 .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
850
851 .bcdUSB = __constant_cpu_to_le16 (0x0200),
852 .bDeviceClass = USB_CLASS_COMM,
853
854 .bNumConfigurations = 1,
855};
856
857static const struct usb_descriptor_header *hs_eth_function [11] = {
858 (struct usb_descriptor_header *) &otg_descriptor,
859#ifdef DEV_CONFIG_CDC
860 /* "cdc" mode descriptors */
861 (struct usb_descriptor_header *) &control_intf,
862 (struct usb_descriptor_header *) &header_desc,
863 (struct usb_descriptor_header *) &union_desc,
864 (struct usb_descriptor_header *) &ether_desc,
865 /* NOTE: status endpoint may need to be removed */
866 (struct usb_descriptor_header *) &hs_status_desc,
867 /* data interface, with altsetting */
868 (struct usb_descriptor_header *) &data_nop_intf,
869 (struct usb_descriptor_header *) &data_intf,
870 (struct usb_descriptor_header *) &hs_source_desc,
871 (struct usb_descriptor_header *) &hs_sink_desc,
872 NULL,
873#endif /* DEV_CONFIG_CDC */
874};
875
876static inline void __init hs_subset_descriptors(void)
877{
878#ifdef DEV_CONFIG_SUBSET
David Brownell11d54892006-12-11 15:59:04 -0800879 /* behavior is "CDC Subset"; extra descriptors say "SAFE" */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 hs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
David Brownell11d54892006-12-11 15:59:04 -0800881 hs_eth_function[2] = (struct usb_descriptor_header *) &header_desc;
882 hs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc;
883 hs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc;
884 hs_eth_function[5] = (struct usb_descriptor_header *) &ether_desc;
885 hs_eth_function[6] = (struct usb_descriptor_header *) &hs_source_desc;
886 hs_eth_function[7] = (struct usb_descriptor_header *) &hs_sink_desc;
887 hs_eth_function[8] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888#else
889 hs_eth_function[1] = NULL;
890#endif
891}
892
893#ifdef CONFIG_USB_ETH_RNDIS
894static const struct usb_descriptor_header *hs_rndis_function [] = {
895 (struct usb_descriptor_header *) &otg_descriptor,
896 /* control interface matches ACM, not Ethernet */
897 (struct usb_descriptor_header *) &rndis_control_intf,
898 (struct usb_descriptor_header *) &header_desc,
899 (struct usb_descriptor_header *) &call_mgmt_descriptor,
900 (struct usb_descriptor_header *) &acm_descriptor,
901 (struct usb_descriptor_header *) &union_desc,
902 (struct usb_descriptor_header *) &hs_status_desc,
903 /* data interface has no altsetting */
904 (struct usb_descriptor_header *) &rndis_data_intf,
905 (struct usb_descriptor_header *) &hs_source_desc,
906 (struct usb_descriptor_header *) &hs_sink_desc,
907 NULL,
908};
909#endif
910
911
912/* maxpacket and other transfer characteristics vary by speed. */
David Brownell0cf4f2d2007-08-02 00:00:38 -0700913static inline struct usb_endpoint_descriptor *
914ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *hs,
915 struct usb_endpoint_descriptor *fs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916{
David Brownell0cf4f2d2007-08-02 00:00:38 -0700917 if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
918 return hs;
919 return fs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920}
921
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
923/*-------------------------------------------------------------------------*/
924
925/* descriptors that are built on-demand */
926
927static char manufacturer [50];
928static char product_desc [40] = DRIVER_DESC;
Aras Vaichas1afc64a2006-02-18 12:31:23 -0800929static char serial_number [20];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931/* address that the host will use ... usually assigned at random */
932static char ethaddr [2 * ETH_ALEN + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
934/* static strings, in UTF-8 */
935static struct usb_string strings [] = {
936 { STRING_MANUFACTURER, manufacturer, },
937 { STRING_PRODUCT, product_desc, },
Aras Vaichas1afc64a2006-02-18 12:31:23 -0800938 { STRING_SERIALNUMBER, serial_number, },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 { STRING_DATA, "Ethernet Data", },
David Brownell11d54892006-12-11 15:59:04 -0800940 { STRING_ETHADDR, ethaddr, },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941#ifdef DEV_CONFIG_CDC
942 { STRING_CDC, "CDC Ethernet", },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 { STRING_CONTROL, "CDC Communications Control", },
944#endif
945#ifdef DEV_CONFIG_SUBSET
946 { STRING_SUBSET, "CDC Ethernet Subset", },
947#endif
948#ifdef CONFIG_USB_ETH_RNDIS
949 { STRING_RNDIS, "RNDIS", },
950 { STRING_RNDIS_CONTROL, "RNDIS Communications Control", },
951#endif
952 { } /* end of list */
953};
954
955static struct usb_gadget_strings stringtab = {
956 .language = 0x0409, /* en-us */
957 .strings = strings,
958};
959
960/*
961 * one config, two interfaces: control, data.
962 * complications: class descriptors, and an altsetting.
963 */
964static int
David Brownell0cf4f2d2007-08-02 00:00:38 -0700965config_buf(struct usb_gadget *g, u8 *buf, u8 type, unsigned index, int is_otg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966{
967 int len;
968 const struct usb_config_descriptor *config;
969 const struct usb_descriptor_header **function;
David Brownell0cf4f2d2007-08-02 00:00:38 -0700970 int hs = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
David Brownell0cf4f2d2007-08-02 00:00:38 -0700972 if (gadget_is_dualspeed(g)) {
973 hs = (g->speed == USB_SPEED_HIGH);
974 if (type == USB_DT_OTHER_SPEED_CONFIG)
975 hs = !hs;
976 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977#define which_fn(t) (hs ? hs_ ## t ## _function : fs_ ## t ## _function)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978
979 if (index >= device_desc.bNumConfigurations)
980 return -EINVAL;
981
982#ifdef CONFIG_USB_ETH_RNDIS
983 /* list the RNDIS config first, to make Microsoft's drivers
984 * happy. DOCSIS 1.0 needs this too.
985 */
986 if (device_desc.bNumConfigurations == 2 && index == 0) {
987 config = &rndis_config;
988 function = which_fn (rndis);
989 } else
990#endif
991 {
992 config = &eth_config;
993 function = which_fn (eth);
994 }
995
996 /* for now, don't advertise srp-only devices */
997 if (!is_otg)
998 function++;
999
1000 len = usb_gadget_config_buf (config, buf, USB_BUFSIZ, function);
1001 if (len < 0)
1002 return len;
1003 ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
1004 return len;
1005}
1006
1007/*-------------------------------------------------------------------------*/
1008
Al Viro55016f12005-10-21 03:21:58 -04001009static void eth_start (struct eth_dev *dev, gfp_t gfp_flags);
1010static int alloc_requests (struct eth_dev *dev, unsigned n, gfp_t gfp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
David Brownell907cba32005-04-28 13:48:09 -07001012static int
Al Viro55016f12005-10-21 03:21:58 -04001013set_ether_config (struct eth_dev *dev, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014{
David Brownell907cba32005-04-28 13:48:09 -07001015 int result = 0;
1016 struct usb_gadget *gadget = dev->gadget;
1017
Ian Campbelle8282642005-06-29 10:20:29 +01001018#if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
David Brownell907cba32005-04-28 13:48:09 -07001019 /* status endpoint used for RNDIS and (optionally) CDC */
1020 if (!subset_active(dev) && dev->status_ep) {
1021 dev->status = ep_desc (gadget, &hs_status_desc,
1022 &fs_status_desc);
1023 dev->status_ep->driver_data = dev;
1024
1025 result = usb_ep_enable (dev->status_ep, dev->status);
1026 if (result != 0) {
David Brownell7e27f182006-06-13 09:54:40 -07001027 DEBUG (dev, "enable %s --> %d\n",
David Brownell907cba32005-04-28 13:48:09 -07001028 dev->status_ep->name, result);
1029 goto done;
1030 }
1031 }
Ian Campbelle8282642005-06-29 10:20:29 +01001032#endif
David Brownell907cba32005-04-28 13:48:09 -07001033
David Brownell11d54892006-12-11 15:59:04 -08001034 dev->in = ep_desc(gadget, &hs_source_desc, &fs_source_desc);
David Brownell907cba32005-04-28 13:48:09 -07001035 dev->in_ep->driver_data = dev;
1036
David Brownell11d54892006-12-11 15:59:04 -08001037 dev->out = ep_desc(gadget, &hs_sink_desc, &fs_sink_desc);
David Brownell907cba32005-04-28 13:48:09 -07001038 dev->out_ep->driver_data = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
1040 /* With CDC, the host isn't allowed to use these two data
1041 * endpoints in the default altsetting for the interface.
1042 * so we don't activate them yet. Reset from SET_INTERFACE.
1043 *
1044 * Strictly speaking RNDIS should work the same: activation is
1045 * a side effect of setting a packet filter. Deactivation is
1046 * from REMOTE_NDIS_HALT_MSG, reset from REMOTE_NDIS_RESET_MSG.
1047 */
David Brownell907cba32005-04-28 13:48:09 -07001048 if (!cdc_active(dev)) {
1049 result = usb_ep_enable (dev->in_ep, dev->in);
1050 if (result != 0) {
David Brownell7e27f182006-06-13 09:54:40 -07001051 DEBUG(dev, "enable %s --> %d\n",
David Brownell907cba32005-04-28 13:48:09 -07001052 dev->in_ep->name, result);
1053 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 }
1055
David Brownell907cba32005-04-28 13:48:09 -07001056 result = usb_ep_enable (dev->out_ep, dev->out);
1057 if (result != 0) {
David Brownell7e27f182006-06-13 09:54:40 -07001058 DEBUG (dev, "enable %s --> %d\n",
Matt Reimer4186c292006-06-07 11:46:13 -07001059 dev->out_ep->name, result);
David Brownell907cba32005-04-28 13:48:09 -07001060 goto done;
1061 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063
David Brownell907cba32005-04-28 13:48:09 -07001064done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 if (result == 0)
1066 result = alloc_requests (dev, qlen (gadget), gfp_flags);
1067
1068 /* on error, disable any endpoints */
1069 if (result < 0) {
David Brownelle7b13ec2007-12-19 11:27:51 -08001070 if (!subset_active(dev) && dev->status_ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 (void) usb_ep_disable (dev->status_ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 dev->status = NULL;
David Brownell907cba32005-04-28 13:48:09 -07001073 (void) usb_ep_disable (dev->in_ep);
1074 (void) usb_ep_disable (dev->out_ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 dev->in = NULL;
1076 dev->out = NULL;
David Brownelle7b13ec2007-12-19 11:27:51 -08001077 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
1079 /* activate non-CDC configs right away
1080 * this isn't strictly according to the RNDIS spec
1081 */
David Brownelle7b13ec2007-12-19 11:27:51 -08001082 else if (!cdc_active (dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 netif_carrier_on (dev->net);
1084 if (netif_running (dev->net)) {
1085 spin_unlock (&dev->lock);
1086 eth_start (dev, GFP_ATOMIC);
1087 spin_lock (&dev->lock);
1088 }
1089 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
1091 if (result == 0)
1092 DEBUG (dev, "qlen %d\n", qlen (gadget));
1093
1094 /* caller is responsible for cleanup on error */
1095 return result;
1096}
1097
1098static void eth_reset_config (struct eth_dev *dev)
1099{
1100 struct usb_request *req;
1101
1102 if (dev->config == 0)
1103 return;
1104
Harvey Harrison441b62c2008-03-03 16:08:34 -08001105 DEBUG (dev, "%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
1107 netif_stop_queue (dev->net);
1108 netif_carrier_off (dev->net);
David Brownell340600a2005-04-28 13:45:25 -07001109 rndis_uninit(dev->rndis_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110
1111 /* disable endpoints, forcing (synchronous) completion of
1112 * pending i/o. then free the requests.
1113 */
1114 if (dev->in) {
1115 usb_ep_disable (dev->in_ep);
David Brownell789851c2006-08-21 15:26:38 -07001116 spin_lock(&dev->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 while (likely (!list_empty (&dev->tx_reqs))) {
1118 req = container_of (dev->tx_reqs.next,
1119 struct usb_request, list);
1120 list_del (&req->list);
David Brownell789851c2006-08-21 15:26:38 -07001121
1122 spin_unlock(&dev->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 usb_ep_free_request (dev->in_ep, req);
David Brownell789851c2006-08-21 15:26:38 -07001124 spin_lock(&dev->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 }
David Brownell789851c2006-08-21 15:26:38 -07001126 spin_unlock(&dev->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 }
1128 if (dev->out) {
1129 usb_ep_disable (dev->out_ep);
David Brownell789851c2006-08-21 15:26:38 -07001130 spin_lock(&dev->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 while (likely (!list_empty (&dev->rx_reqs))) {
1132 req = container_of (dev->rx_reqs.next,
1133 struct usb_request, list);
1134 list_del (&req->list);
David Brownell789851c2006-08-21 15:26:38 -07001135
1136 spin_unlock(&dev->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 usb_ep_free_request (dev->out_ep, req);
David Brownell789851c2006-08-21 15:26:38 -07001138 spin_lock(&dev->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 }
David Brownell789851c2006-08-21 15:26:38 -07001140 spin_unlock(&dev->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 }
1142
1143 if (dev->status) {
1144 usb_ep_disable (dev->status_ep);
1145 }
David Brownell907cba32005-04-28 13:48:09 -07001146 dev->rndis = 0;
1147 dev->cdc_filter = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 dev->config = 0;
1149}
1150
1151/* change our operational config. must agree with the code
1152 * that returns config descriptors, and altsetting code.
1153 */
1154static int
Al Viro55016f12005-10-21 03:21:58 -04001155eth_set_config (struct eth_dev *dev, unsigned number, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156{
1157 int result = 0;
1158 struct usb_gadget *gadget = dev->gadget;
1159
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 if (gadget_is_sa1100 (gadget)
1161 && dev->config
1162 && atomic_read (&dev->tx_qlen) != 0) {
1163 /* tx fifo is full, but we can't clear it...*/
1164 INFO (dev, "can't change configurations\n");
1165 return -ESPIPE;
1166 }
1167 eth_reset_config (dev);
1168
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 switch (number) {
1170 case DEV_CONFIG_VALUE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 result = set_ether_config (dev, gfp_flags);
1172 break;
1173#ifdef CONFIG_USB_ETH_RNDIS
1174 case DEV_RNDIS_CONFIG_VALUE:
1175 dev->rndis = 1;
1176 result = set_ether_config (dev, gfp_flags);
1177 break;
1178#endif
1179 default:
1180 result = -EINVAL;
1181 /* FALL THROUGH */
1182 case 0:
1183 break;
1184 }
1185
1186 if (result) {
1187 if (number)
1188 eth_reset_config (dev);
1189 usb_gadget_vbus_draw(dev->gadget,
David Brownell0cf4f2d2007-08-02 00:00:38 -07001190 gadget_is_otg(dev->gadget) ? 8 : 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 } else {
1192 char *speed;
1193 unsigned power;
1194
1195 power = 2 * eth_config.bMaxPower;
1196 usb_gadget_vbus_draw(dev->gadget, power);
1197
1198 switch (gadget->speed) {
1199 case USB_SPEED_FULL: speed = "full"; break;
1200#ifdef CONFIG_USB_GADGET_DUALSPEED
1201 case USB_SPEED_HIGH: speed = "high"; break;
1202#endif
David Brownell7e27f182006-06-13 09:54:40 -07001203 default: speed = "?"; break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 }
1205
1206 dev->config = number;
1207 INFO (dev, "%s speed config #%d: %d mA, %s, using %s\n",
1208 speed, number, power, driver_desc,
David Brownell45e45ab2005-05-16 08:26:38 -07001209 rndis_active(dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 ? "RNDIS"
David Brownell45e45ab2005-05-16 08:26:38 -07001211 : (cdc_active(dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 ? "CDC Ethernet"
1213 : "CDC Ethernet Subset"));
1214 }
1215 return result;
1216}
1217
1218/*-------------------------------------------------------------------------*/
1219
1220#ifdef DEV_CONFIG_CDC
1221
David Brownell907cba32005-04-28 13:48:09 -07001222/* The interrupt endpoint is used in CDC networking models (Ethernet, ATM)
1223 * only to notify the host about link status changes (which we support) or
1224 * report completion of some encapsulated command (as used in RNDIS). Since
1225 * we want this CDC Ethernet code to be vendor-neutral, we don't use that
1226 * command mechanism; and only one status request is ever queued.
1227 */
1228
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229static void eth_status_complete (struct usb_ep *ep, struct usb_request *req)
1230{
1231 struct usb_cdc_notification *event = req->buf;
1232 int value = req->status;
1233 struct eth_dev *dev = ep->driver_data;
1234
1235 /* issue the second notification if host reads the first */
1236 if (event->bNotificationType == USB_CDC_NOTIFY_NETWORK_CONNECTION
1237 && value == 0) {
1238 __le32 *data = req->buf + sizeof *event;
1239
1240 event->bmRequestType = 0xA1;
1241 event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE;
1242 event->wValue = __constant_cpu_to_le16 (0);
1243 event->wIndex = __constant_cpu_to_le16 (1);
1244 event->wLength = __constant_cpu_to_le16 (8);
1245
1246 /* SPEED_CHANGE data is up/down speeds in bits/sec */
1247 data [0] = data [1] = cpu_to_le32 (BITRATE (dev->gadget));
1248
1249 req->length = STATUS_BYTECOUNT;
1250 value = usb_ep_queue (ep, req, GFP_ATOMIC);
1251 DEBUG (dev, "send SPEED_CHANGE --> %d\n", value);
1252 if (value == 0)
1253 return;
1254 } else if (value != -ECONNRESET)
1255 DEBUG (dev, "event %02x --> %d\n",
1256 event->bNotificationType, value);
David Brownell907cba32005-04-28 13:48:09 -07001257 req->context = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258}
1259
1260static void issue_start_status (struct eth_dev *dev)
1261{
1262 struct usb_request *req = dev->stat_req;
1263 struct usb_cdc_notification *event;
1264 int value;
David Brownell7e27f182006-06-13 09:54:40 -07001265
Harvey Harrison441b62c2008-03-03 16:08:34 -08001266 DEBUG (dev, "%s, flush old status first\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
1268 /* flush old status
1269 *
1270 * FIXME ugly idiom, maybe we'd be better with just
1271 * a "cancel the whole queue" primitive since any
1272 * unlink-one primitive has way too many error modes.
1273 * here, we "know" toggle is already clear...
David Brownell907cba32005-04-28 13:48:09 -07001274 *
1275 * FIXME iff req->context != null just dequeue it
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 */
1277 usb_ep_disable (dev->status_ep);
1278 usb_ep_enable (dev->status_ep, dev->status);
1279
1280 /* 3.8.1 says to issue first NETWORK_CONNECTION, then
1281 * a SPEED_CHANGE. could be useful in some configs.
1282 */
1283 event = req->buf;
1284 event->bmRequestType = 0xA1;
1285 event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION;
1286 event->wValue = __constant_cpu_to_le16 (1); /* connected */
1287 event->wIndex = __constant_cpu_to_le16 (1);
1288 event->wLength = 0;
1289
1290 req->length = sizeof *event;
1291 req->complete = eth_status_complete;
David Brownell907cba32005-04-28 13:48:09 -07001292 req->context = dev;
1293
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 value = usb_ep_queue (dev->status_ep, req, GFP_ATOMIC);
1295 if (value < 0)
1296 DEBUG (dev, "status buf queue --> %d\n", value);
1297}
1298
1299#endif
1300
1301/*-------------------------------------------------------------------------*/
1302
1303static void eth_setup_complete (struct usb_ep *ep, struct usb_request *req)
1304{
1305 if (req->status || req->actual != req->length)
1306 DEBUG ((struct eth_dev *) ep->driver_data,
1307 "setup complete --> %d, %d/%d\n",
1308 req->status, req->actual, req->length);
1309}
1310
1311#ifdef CONFIG_USB_ETH_RNDIS
1312
1313static void rndis_response_complete (struct usb_ep *ep, struct usb_request *req)
1314{
1315 if (req->status || req->actual != req->length)
1316 DEBUG ((struct eth_dev *) ep->driver_data,
1317 "rndis response complete --> %d, %d/%d\n",
1318 req->status, req->actual, req->length);
1319
1320 /* done sending after USB_CDC_GET_ENCAPSULATED_RESPONSE */
1321}
1322
1323static void rndis_command_complete (struct usb_ep *ep, struct usb_request *req)
1324{
1325 struct eth_dev *dev = ep->driver_data;
1326 int status;
David Brownell7e27f182006-06-13 09:54:40 -07001327
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 /* received RNDIS command from USB_CDC_SEND_ENCAPSULATED_COMMAND */
1329 spin_lock(&dev->lock);
1330 status = rndis_msg_parser (dev->rndis_config, (u8 *) req->buf);
1331 if (status < 0)
Harvey Harrison441b62c2008-03-03 16:08:34 -08001332 ERROR(dev, "%s: rndis parse error %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 spin_unlock(&dev->lock);
1334}
1335
1336#endif /* RNDIS */
1337
1338/*
1339 * The setup() callback implements all the ep0 functionality that's not
1340 * handled lower down. CDC has a number of less-common features:
1341 *
1342 * - two interfaces: control, and ethernet data
1343 * - Ethernet data interface has two altsettings: default, and active
1344 * - class-specific descriptors for the control interface
1345 * - class-specific control requests
1346 */
1347static int
1348eth_setup (struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
1349{
1350 struct eth_dev *dev = get_gadget_data (gadget);
1351 struct usb_request *req = dev->req;
1352 int value = -EOPNOTSUPP;
David Brownell1bbc1692005-05-07 13:05:13 -07001353 u16 wIndex = le16_to_cpu(ctrl->wIndex);
1354 u16 wValue = le16_to_cpu(ctrl->wValue);
1355 u16 wLength = le16_to_cpu(ctrl->wLength);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
1357 /* descriptors just go into the pre-allocated ep0 buffer,
1358 * while config change events may enable network traffic.
1359 */
1360 req->complete = eth_setup_complete;
1361 switch (ctrl->bRequest) {
1362
1363 case USB_REQ_GET_DESCRIPTOR:
1364 if (ctrl->bRequestType != USB_DIR_IN)
1365 break;
1366 switch (wValue >> 8) {
1367
1368 case USB_DT_DEVICE:
1369 value = min (wLength, (u16) sizeof device_desc);
1370 memcpy (req->buf, &device_desc, value);
1371 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 case USB_DT_DEVICE_QUALIFIER:
David Brownell0cf4f2d2007-08-02 00:00:38 -07001373 if (!gadget_is_dualspeed(gadget))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 break;
1375 value = min (wLength, (u16) sizeof dev_qualifier);
1376 memcpy (req->buf, &dev_qualifier, value);
1377 break;
1378
1379 case USB_DT_OTHER_SPEED_CONFIG:
David Brownell0cf4f2d2007-08-02 00:00:38 -07001380 if (!gadget_is_dualspeed(gadget))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 break;
1382 // FALLTHROUGH
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 case USB_DT_CONFIG:
David Brownell0cf4f2d2007-08-02 00:00:38 -07001384 value = config_buf(gadget, req->buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 wValue >> 8,
1386 wValue & 0xff,
David Brownell0cf4f2d2007-08-02 00:00:38 -07001387 gadget_is_otg(gadget));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 if (value >= 0)
1389 value = min (wLength, (u16) value);
1390 break;
1391
1392 case USB_DT_STRING:
1393 value = usb_gadget_get_string (&stringtab,
1394 wValue & 0xff, req->buf);
1395 if (value >= 0)
1396 value = min (wLength, (u16) value);
1397 break;
1398 }
1399 break;
1400
1401 case USB_REQ_SET_CONFIGURATION:
1402 if (ctrl->bRequestType != 0)
1403 break;
1404 if (gadget->a_hnp_support)
1405 DEBUG (dev, "HNP available\n");
1406 else if (gadget->a_alt_hnp_support)
1407 DEBUG (dev, "HNP needs a different root port\n");
1408 spin_lock (&dev->lock);
1409 value = eth_set_config (dev, wValue, GFP_ATOMIC);
1410 spin_unlock (&dev->lock);
1411 break;
1412 case USB_REQ_GET_CONFIGURATION:
1413 if (ctrl->bRequestType != USB_DIR_IN)
1414 break;
1415 *(u8 *)req->buf = dev->config;
1416 value = min (wLength, (u16) 1);
1417 break;
1418
1419 case USB_REQ_SET_INTERFACE:
1420 if (ctrl->bRequestType != USB_RECIP_INTERFACE
1421 || !dev->config
1422 || wIndex > 1)
1423 break;
David Brownell45e45ab2005-05-16 08:26:38 -07001424 if (!cdc_active(dev) && wIndex != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 break;
1426 spin_lock (&dev->lock);
1427
1428 /* PXA hardware partially handles SET_INTERFACE;
1429 * we need to kluge around that interference.
1430 */
1431 if (gadget_is_pxa (gadget)) {
1432 value = eth_set_config (dev, DEV_CONFIG_VALUE,
1433 GFP_ATOMIC);
1434 goto done_set_intf;
1435 }
1436
1437#ifdef DEV_CONFIG_CDC
1438 switch (wIndex) {
1439 case 0: /* control/master intf */
1440 if (wValue != 0)
1441 break;
1442 if (dev->status) {
1443 usb_ep_disable (dev->status_ep);
1444 usb_ep_enable (dev->status_ep, dev->status);
1445 }
1446 value = 0;
1447 break;
1448 case 1: /* data intf */
1449 if (wValue > 1)
1450 break;
1451 usb_ep_disable (dev->in_ep);
1452 usb_ep_disable (dev->out_ep);
1453
1454 /* CDC requires the data transfers not be done from
1455 * the default interface setting ... also, setting
David Brownell907cba32005-04-28 13:48:09 -07001456 * the non-default interface resets filters etc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 */
1458 if (wValue == 1) {
David Brownell907cba32005-04-28 13:48:09 -07001459 if (!cdc_active (dev))
1460 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 usb_ep_enable (dev->in_ep, dev->in);
1462 usb_ep_enable (dev->out_ep, dev->out);
1463 dev->cdc_filter = DEFAULT_FILTER;
1464 netif_carrier_on (dev->net);
1465 if (dev->status)
1466 issue_start_status (dev);
1467 if (netif_running (dev->net)) {
1468 spin_unlock (&dev->lock);
1469 eth_start (dev, GFP_ATOMIC);
1470 spin_lock (&dev->lock);
1471 }
1472 } else {
1473 netif_stop_queue (dev->net);
1474 netif_carrier_off (dev->net);
1475 }
1476 value = 0;
1477 break;
1478 }
1479#else
1480 /* FIXME this is wrong, as is the assumption that
1481 * all non-PXA hardware talks real CDC ...
1482 */
1483 dev_warn (&gadget->dev, "set_interface ignored!\n");
1484#endif /* DEV_CONFIG_CDC */
1485
1486done_set_intf:
1487 spin_unlock (&dev->lock);
1488 break;
1489 case USB_REQ_GET_INTERFACE:
1490 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE)
1491 || !dev->config
1492 || wIndex > 1)
1493 break;
David Brownell45e45ab2005-05-16 08:26:38 -07001494 if (!(cdc_active(dev) || rndis_active(dev)) && wIndex != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 break;
1496
1497 /* for CDC, iff carrier is on, data interface is active. */
David Brownell45e45ab2005-05-16 08:26:38 -07001498 if (rndis_active(dev) || wIndex != 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 *(u8 *)req->buf = 0;
1500 else
1501 *(u8 *)req->buf = netif_carrier_ok (dev->net) ? 1 : 0;
1502 value = min (wLength, (u16) 1);
1503 break;
1504
1505#ifdef DEV_CONFIG_CDC
1506 case USB_CDC_SET_ETHERNET_PACKET_FILTER:
1507 /* see 6.2.30: no data, wIndex = interface,
1508 * wValue = packet filter bitmap
1509 */
1510 if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
David Brownell45e45ab2005-05-16 08:26:38 -07001511 || !cdc_active(dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 || wLength != 0
1513 || wIndex > 1)
1514 break;
1515 DEBUG (dev, "packet filter %02x\n", wValue);
1516 dev->cdc_filter = wValue;
1517 value = 0;
1518 break;
1519
1520 /* and potentially:
1521 * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS:
1522 * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER:
1523 * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER:
1524 * case USB_CDC_GET_ETHERNET_STATISTIC:
1525 */
1526
1527#endif /* DEV_CONFIG_CDC */
1528
David Brownell7e27f182006-06-13 09:54:40 -07001529#ifdef CONFIG_USB_ETH_RNDIS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 /* RNDIS uses the CDC command encapsulation mechanism to implement
1531 * an RPC scheme, with much getting/setting of attributes by OID.
1532 */
1533 case USB_CDC_SEND_ENCAPSULATED_COMMAND:
1534 if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
David Brownell45e45ab2005-05-16 08:26:38 -07001535 || !rndis_active(dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 || wLength > USB_BUFSIZ
1537 || wValue
1538 || rndis_control_intf.bInterfaceNumber
1539 != wIndex)
1540 break;
1541 /* read the request, then process it */
1542 value = wLength;
1543 req->complete = rndis_command_complete;
1544 /* later, rndis_control_ack () sends a notification */
1545 break;
David Brownell7e27f182006-06-13 09:54:40 -07001546
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 case USB_CDC_GET_ENCAPSULATED_RESPONSE:
1548 if ((USB_DIR_IN|USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1549 == ctrl->bRequestType
David Brownell45e45ab2005-05-16 08:26:38 -07001550 && rndis_active(dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 // && wLength >= 0x0400
1552 && !wValue
1553 && rndis_control_intf.bInterfaceNumber
1554 == wIndex) {
1555 u8 *buf;
David Brownell0cf4f2d2007-08-02 00:00:38 -07001556 u32 n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557
1558 /* return the result */
David Brownell0cf4f2d2007-08-02 00:00:38 -07001559 buf = rndis_get_next_response(dev->rndis_config, &n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 if (buf) {
David Brownell0cf4f2d2007-08-02 00:00:38 -07001561 memcpy(req->buf, buf, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 req->complete = rndis_response_complete;
1563 rndis_free_response(dev->rndis_config, buf);
Jan Altenberg41566bc2008-02-19 01:44:50 +01001564 value = n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 }
1566 /* else stalls ... spec says to avoid that */
1567 }
1568 break;
1569#endif /* RNDIS */
1570
1571 default:
1572 VDEBUG (dev,
1573 "unknown control req%02x.%02x v%04x i%04x l%d\n",
1574 ctrl->bRequestType, ctrl->bRequest,
1575 wValue, wIndex, wLength);
1576 }
1577
1578 /* respond with data transfer before status phase? */
1579 if (value >= 0) {
1580 req->length = value;
1581 req->zero = value < wLength
1582 && (value % gadget->ep0->maxpacket) == 0;
1583 value = usb_ep_queue (gadget->ep0, req, GFP_ATOMIC);
1584 if (value < 0) {
1585 DEBUG (dev, "ep_queue --> %d\n", value);
1586 req->status = 0;
1587 eth_setup_complete (gadget->ep0, req);
1588 }
1589 }
1590
1591 /* host either stalls (value < 0) or reports success */
1592 return value;
1593}
1594
1595static void
1596eth_disconnect (struct usb_gadget *gadget)
1597{
1598 struct eth_dev *dev = get_gadget_data (gadget);
1599 unsigned long flags;
1600
1601 spin_lock_irqsave (&dev->lock, flags);
1602 netif_stop_queue (dev->net);
1603 netif_carrier_off (dev->net);
1604 eth_reset_config (dev);
1605 spin_unlock_irqrestore (&dev->lock, flags);
1606
1607 /* FIXME RNDIS should enter RNDIS_UNINITIALIZED */
1608
1609 /* next we may get setup() calls to enumerate new connections;
1610 * or an unbind() during shutdown (including removing module).
1611 */
1612}
1613
1614/*-------------------------------------------------------------------------*/
1615
1616/* NETWORK DRIVER HOOKUP (to the layer above this driver) */
1617
1618static int eth_change_mtu (struct net_device *net, int new_mtu)
1619{
1620 struct eth_dev *dev = netdev_priv(net);
1621
David Brownell7802ac52006-01-22 10:33:27 -08001622 if (dev->rndis)
1623 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624
1625 if (new_mtu <= ETH_HLEN || new_mtu > ETH_FRAME_LEN)
1626 return -ERANGE;
1627 /* no zero-length packet read wanted after mtu-sized packets */
1628 if (((new_mtu + sizeof (struct ethhdr)) % dev->in_ep->maxpacket) == 0)
1629 return -EDOM;
1630 net->mtu = new_mtu;
1631 return 0;
1632}
1633
1634static struct net_device_stats *eth_get_stats (struct net_device *net)
1635{
1636 return &((struct eth_dev *)netdev_priv(net))->stats;
1637}
1638
1639static void eth_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *p)
1640{
1641 struct eth_dev *dev = netdev_priv(net);
1642 strlcpy(p->driver, shortname, sizeof p->driver);
1643 strlcpy(p->version, DRIVER_VERSION, sizeof p->version);
1644 strlcpy(p->fw_version, dev->gadget->name, sizeof p->fw_version);
1645 strlcpy (p->bus_info, dev->gadget->dev.bus_id, sizeof p->bus_info);
1646}
1647
1648static u32 eth_get_link(struct net_device *net)
1649{
1650 struct eth_dev *dev = netdev_priv(net);
1651 return dev->gadget->speed != USB_SPEED_UNKNOWN;
1652}
1653
1654static struct ethtool_ops ops = {
1655 .get_drvinfo = eth_get_drvinfo,
1656 .get_link = eth_get_link
1657};
1658
1659static void defer_kevent (struct eth_dev *dev, int flag)
1660{
1661 if (test_and_set_bit (flag, &dev->todo))
1662 return;
1663 if (!schedule_work (&dev->work))
1664 ERROR (dev, "kevent %d may have been dropped\n", flag);
1665 else
1666 DEBUG (dev, "kevent %d scheduled\n", flag);
1667}
1668
1669static void rx_complete (struct usb_ep *ep, struct usb_request *req);
1670
1671static int
Al Viro55016f12005-10-21 03:21:58 -04001672rx_submit (struct eth_dev *dev, struct usb_request *req, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673{
1674 struct sk_buff *skb;
1675 int retval = -ENOMEM;
1676 size_t size;
1677
1678 /* Padding up to RX_EXTRA handles minor disagreements with host.
1679 * Normally we use the USB "terminate on short read" convention;
1680 * so allow up to (N*maxpacket), since that memory is normally
1681 * already allocated. Some hardware doesn't deal well with short
1682 * reads (e.g. DMA must be N*maxpacket), so for now don't trim a
1683 * byte off the end (to force hardware errors on overflow).
1684 *
1685 * RNDIS uses internal framing, and explicitly allows senders to
1686 * pad to end-of-packet. That's potentially nice for speed,
1687 * but means receivers can't recover synch on their own.
1688 */
1689 size = (sizeof (struct ethhdr) + dev->net->mtu + RX_EXTRA);
1690 size += dev->out_ep->maxpacket - 1;
David Brownell907cba32005-04-28 13:48:09 -07001691 if (rndis_active(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 size += sizeof (struct rndis_packet_msg_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 size -= size % dev->out_ep->maxpacket;
1694
David Brownella9475222007-07-30 12:31:07 -07001695 skb = alloc_skb(size + NET_IP_ALIGN, gfp_flags);
1696 if (skb == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 DEBUG (dev, "no rx skb\n");
1698 goto enomem;
1699 }
David Brownell7e27f182006-06-13 09:54:40 -07001700
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 /* Some platforms perform better when IP packets are aligned,
1702 * but on at least one, checksumming fails otherwise. Note:
David Brownell6cdee102005-04-18 17:39:34 -07001703 * RNDIS headers involve variable numbers of LE32 values.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 */
1705 skb_reserve(skb, NET_IP_ALIGN);
1706
1707 req->buf = skb->data;
1708 req->length = size;
1709 req->complete = rx_complete;
1710 req->context = skb;
1711
1712 retval = usb_ep_queue (dev->out_ep, req, gfp_flags);
1713 if (retval == -ENOMEM)
1714enomem:
1715 defer_kevent (dev, WORK_RX_MEMORY);
1716 if (retval) {
1717 DEBUG (dev, "rx submit --> %d\n", retval);
Erik Hovlandb8d297c2007-04-23 10:50:15 -07001718 if (skb)
1719 dev_kfree_skb_any(skb);
David Brownell789851c2006-08-21 15:26:38 -07001720 spin_lock(&dev->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 list_add (&req->list, &dev->rx_reqs);
David Brownell789851c2006-08-21 15:26:38 -07001722 spin_unlock(&dev->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 }
1724 return retval;
1725}
1726
1727static void rx_complete (struct usb_ep *ep, struct usb_request *req)
1728{
1729 struct sk_buff *skb = req->context;
1730 struct eth_dev *dev = ep->driver_data;
1731 int status = req->status;
1732
1733 switch (status) {
1734
1735 /* normal completion */
1736 case 0:
1737 skb_put (skb, req->actual);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 /* we know MaxPacketsPerTransfer == 1 here */
David Brownell45e45ab2005-05-16 08:26:38 -07001739 if (rndis_active(dev))
David Brownell6cdee102005-04-18 17:39:34 -07001740 status = rndis_rm_hdr (skb);
David Brownell6cdee102005-04-18 17:39:34 -07001741 if (status < 0
1742 || ETH_HLEN > skb->len
1743 || skb->len > ETH_FRAME_LEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 dev->stats.rx_errors++;
1745 dev->stats.rx_length_errors++;
1746 DEBUG (dev, "rx length %d\n", skb->len);
1747 break;
1748 }
1749
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 skb->protocol = eth_type_trans (skb, dev->net);
1751 dev->stats.rx_packets++;
1752 dev->stats.rx_bytes += skb->len;
1753
1754 /* no buffer copies needed, unless hardware can't
1755 * use skb buffers.
1756 */
1757 status = netif_rx (skb);
1758 skb = NULL;
1759 break;
1760
1761 /* software-driven interface shutdown */
1762 case -ECONNRESET: // unlink
1763 case -ESHUTDOWN: // disconnect etc
1764 VDEBUG (dev, "rx shutdown, code %d\n", status);
1765 goto quiesce;
1766
1767 /* for hardware automagic (such as pxa) */
1768 case -ECONNABORTED: // endpoint reset
1769 DEBUG (dev, "rx %s reset\n", ep->name);
1770 defer_kevent (dev, WORK_RX_MEMORY);
1771quiesce:
1772 dev_kfree_skb_any (skb);
1773 goto clean;
1774
1775 /* data overrun */
1776 case -EOVERFLOW:
1777 dev->stats.rx_over_errors++;
1778 // FALLTHROUGH
David Brownell7e27f182006-06-13 09:54:40 -07001779
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 default:
1781 dev->stats.rx_errors++;
1782 DEBUG (dev, "rx status %d\n", status);
1783 break;
1784 }
1785
1786 if (skb)
1787 dev_kfree_skb_any (skb);
1788 if (!netif_running (dev->net)) {
1789clean:
David Brownell789851c2006-08-21 15:26:38 -07001790 spin_lock(&dev->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 list_add (&req->list, &dev->rx_reqs);
David Brownell789851c2006-08-21 15:26:38 -07001792 spin_unlock(&dev->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 req = NULL;
1794 }
1795 if (req)
1796 rx_submit (dev, req, GFP_ATOMIC);
1797}
1798
1799static int prealloc (struct list_head *list, struct usb_ep *ep,
Al Viro55016f12005-10-21 03:21:58 -04001800 unsigned n, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801{
1802 unsigned i;
1803 struct usb_request *req;
1804
1805 if (!n)
1806 return -ENOMEM;
1807
1808 /* queue/recycle up to N requests */
1809 i = n;
1810 list_for_each_entry (req, list, list) {
1811 if (i-- == 0)
1812 goto extra;
1813 }
1814 while (i--) {
1815 req = usb_ep_alloc_request (ep, gfp_flags);
1816 if (!req)
1817 return list_empty (list) ? -ENOMEM : 0;
1818 list_add (&req->list, list);
1819 }
1820 return 0;
1821
1822extra:
1823 /* free extras */
1824 for (;;) {
1825 struct list_head *next;
1826
1827 next = req->list.next;
1828 list_del (&req->list);
1829 usb_ep_free_request (ep, req);
1830
1831 if (next == list)
1832 break;
1833
1834 req = container_of (next, struct usb_request, list);
1835 }
1836 return 0;
1837}
1838
Al Viro55016f12005-10-21 03:21:58 -04001839static int alloc_requests (struct eth_dev *dev, unsigned n, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840{
1841 int status;
1842
David Brownell789851c2006-08-21 15:26:38 -07001843 spin_lock(&dev->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 status = prealloc (&dev->tx_reqs, dev->in_ep, n, gfp_flags);
1845 if (status < 0)
1846 goto fail;
1847 status = prealloc (&dev->rx_reqs, dev->out_ep, n, gfp_flags);
1848 if (status < 0)
1849 goto fail;
David Brownell789851c2006-08-21 15:26:38 -07001850 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851fail:
1852 DEBUG (dev, "can't alloc requests\n");
David Brownell789851c2006-08-21 15:26:38 -07001853done:
1854 spin_unlock(&dev->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 return status;
1856}
1857
Al Viro55016f12005-10-21 03:21:58 -04001858static void rx_fill (struct eth_dev *dev, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859{
1860 struct usb_request *req;
1861 unsigned long flags;
1862
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 /* fill unused rxq slots with some skb */
David Brownell789851c2006-08-21 15:26:38 -07001864 spin_lock_irqsave(&dev->req_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 while (!list_empty (&dev->rx_reqs)) {
1866 req = container_of (dev->rx_reqs.next,
1867 struct usb_request, list);
1868 list_del_init (&req->list);
David Brownell789851c2006-08-21 15:26:38 -07001869 spin_unlock_irqrestore(&dev->req_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870
1871 if (rx_submit (dev, req, gfp_flags) < 0) {
1872 defer_kevent (dev, WORK_RX_MEMORY);
1873 return;
1874 }
1875
David Brownell789851c2006-08-21 15:26:38 -07001876 spin_lock_irqsave(&dev->req_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 }
David Brownell789851c2006-08-21 15:26:38 -07001878 spin_unlock_irqrestore(&dev->req_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879}
1880
David Howellsc4028952006-11-22 14:57:56 +00001881static void eth_work (struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882{
David Howellsc4028952006-11-22 14:57:56 +00001883 struct eth_dev *dev = container_of(work, struct eth_dev, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884
David Brownell907cba32005-04-28 13:48:09 -07001885 if (test_and_clear_bit (WORK_RX_MEMORY, &dev->todo)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 if (netif_running (dev->net))
1887 rx_fill (dev, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 }
1889
1890 if (dev->todo)
1891 DEBUG (dev, "work done, flags = 0x%lx\n", dev->todo);
1892}
1893
1894static void tx_complete (struct usb_ep *ep, struct usb_request *req)
1895{
1896 struct sk_buff *skb = req->context;
1897 struct eth_dev *dev = ep->driver_data;
1898
1899 switch (req->status) {
1900 default:
1901 dev->stats.tx_errors++;
1902 VDEBUG (dev, "tx err %d\n", req->status);
1903 /* FALLTHROUGH */
1904 case -ECONNRESET: // unlink
1905 case -ESHUTDOWN: // disconnect etc
1906 break;
1907 case 0:
1908 dev->stats.tx_bytes += skb->len;
1909 }
1910 dev->stats.tx_packets++;
1911
David Brownell789851c2006-08-21 15:26:38 -07001912 spin_lock(&dev->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 list_add (&req->list, &dev->tx_reqs);
David Brownell789851c2006-08-21 15:26:38 -07001914 spin_unlock(&dev->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 dev_kfree_skb_any (skb);
1916
1917 atomic_dec (&dev->tx_qlen);
1918 if (netif_carrier_ok (dev->net))
1919 netif_wake_queue (dev->net);
1920}
1921
1922static inline int eth_is_promisc (struct eth_dev *dev)
1923{
1924 /* no filters for the CDC subset; always promisc */
1925 if (subset_active (dev))
1926 return 1;
1927 return dev->cdc_filter & USB_CDC_PACKET_TYPE_PROMISCUOUS;
1928}
1929
1930static int eth_start_xmit (struct sk_buff *skb, struct net_device *net)
1931{
1932 struct eth_dev *dev = netdev_priv(net);
1933 int length = skb->len;
1934 int retval;
1935 struct usb_request *req = NULL;
1936 unsigned long flags;
1937
1938 /* apply outgoing CDC or RNDIS filters */
1939 if (!eth_is_promisc (dev)) {
1940 u8 *dest = skb->data;
1941
David Brownelld8126a02006-11-12 18:09:44 -08001942 if (is_multicast_ether_addr(dest)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 u16 type;
1944
1945 /* ignores USB_CDC_PACKET_TYPE_MULTICAST and host
1946 * SET_ETHERNET_MULTICAST_FILTERS requests
1947 */
David Brownelld8126a02006-11-12 18:09:44 -08001948 if (is_broadcast_ether_addr(dest))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 type = USB_CDC_PACKET_TYPE_BROADCAST;
1950 else
1951 type = USB_CDC_PACKET_TYPE_ALL_MULTICAST;
1952 if (!(dev->cdc_filter & type)) {
1953 dev_kfree_skb_any (skb);
1954 return 0;
1955 }
1956 }
1957 /* ignores USB_CDC_PACKET_TYPE_DIRECTED */
1958 }
1959
David Brownell789851c2006-08-21 15:26:38 -07001960 spin_lock_irqsave(&dev->req_lock, flags);
Benedikt Spranger53953532007-10-02 14:40:48 -07001961 /*
1962 * this freelist can be empty if an interrupt triggered disconnect()
1963 * and reconfigured the gadget (shutting down this queue) after the
1964 * network stack decided to xmit but before we got the spinlock.
1965 */
1966 if (list_empty(&dev->tx_reqs)) {
1967 spin_unlock_irqrestore(&dev->req_lock, flags);
1968 return 1;
1969 }
1970
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 req = container_of (dev->tx_reqs.next, struct usb_request, list);
1972 list_del (&req->list);
Benedikt Spranger53953532007-10-02 14:40:48 -07001973
1974 /* temporarily stop TX queue when the freelist empties */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 if (list_empty (&dev->tx_reqs))
1976 netif_stop_queue (net);
David Brownell789851c2006-08-21 15:26:38 -07001977 spin_unlock_irqrestore(&dev->req_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978
1979 /* no buffer copies needed, unless the network stack did it
1980 * or the hardware can't use skb buffers.
1981 * or there's not enough space for any RNDIS headers we need
1982 */
David Brownell45e45ab2005-05-16 08:26:38 -07001983 if (rndis_active(dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 struct sk_buff *skb_rndis;
1985
1986 skb_rndis = skb_realloc_headroom (skb,
1987 sizeof (struct rndis_packet_msg_type));
1988 if (!skb_rndis)
1989 goto drop;
David Brownell7e27f182006-06-13 09:54:40 -07001990
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 dev_kfree_skb_any (skb);
1992 skb = skb_rndis;
1993 rndis_add_hdr (skb);
1994 length = skb->len;
1995 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 req->buf = skb->data;
1997 req->context = skb;
1998 req->complete = tx_complete;
1999
2000 /* use zlp framing on tx for strict CDC-Ether conformance,
2001 * though any robust network rx path ignores extra padding.
2002 * and some hardware doesn't like to write zlps.
2003 */
2004 req->zero = 1;
2005 if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0)
2006 length++;
2007
2008 req->length = length;
2009
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 /* throttle highspeed IRQ rate back slightly */
David Brownell0cf4f2d2007-08-02 00:00:38 -07002011 if (gadget_is_dualspeed(dev->gadget))
2012 req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH)
2013 ? ((atomic_read(&dev->tx_qlen) % qmult) != 0)
2014 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015
2016 retval = usb_ep_queue (dev->in_ep, req, GFP_ATOMIC);
2017 switch (retval) {
2018 default:
2019 DEBUG (dev, "tx queue err %d\n", retval);
2020 break;
2021 case 0:
2022 net->trans_start = jiffies;
2023 atomic_inc (&dev->tx_qlen);
2024 }
2025
2026 if (retval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027drop:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 dev->stats.tx_dropped++;
2029 dev_kfree_skb_any (skb);
David Brownell789851c2006-08-21 15:26:38 -07002030 spin_lock_irqsave(&dev->req_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 if (list_empty (&dev->tx_reqs))
2032 netif_start_queue (net);
2033 list_add (&req->list, &dev->tx_reqs);
David Brownell789851c2006-08-21 15:26:38 -07002034 spin_unlock_irqrestore(&dev->req_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 }
2036 return 0;
2037}
2038
2039/*-------------------------------------------------------------------------*/
2040
2041#ifdef CONFIG_USB_ETH_RNDIS
2042
David Brownell907cba32005-04-28 13:48:09 -07002043/* The interrupt endpoint is used in RNDIS to notify the host when messages
2044 * other than data packets are available ... notably the REMOTE_NDIS_*_CMPLT
2045 * messages, but also REMOTE_NDIS_INDICATE_STATUS_MSG and potentially even
2046 * REMOTE_NDIS_KEEPALIVE_MSG.
2047 *
2048 * The RNDIS control queue is processed by GET_ENCAPSULATED_RESPONSE, and
2049 * normally just one notification will be queued.
2050 */
2051
Al Viro55016f12005-10-21 03:21:58 -04002052static struct usb_request *eth_req_alloc (struct usb_ep *, unsigned, gfp_t);
David Brownell907cba32005-04-28 13:48:09 -07002053static void eth_req_free (struct usb_ep *ep, struct usb_request *req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054
2055static void
2056rndis_control_ack_complete (struct usb_ep *ep, struct usb_request *req)
2057{
David Brownell907cba32005-04-28 13:48:09 -07002058 struct eth_dev *dev = ep->driver_data;
2059
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 if (req->status || req->actual != req->length)
David Brownell907cba32005-04-28 13:48:09 -07002061 DEBUG (dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 "rndis control ack complete --> %d, %d/%d\n",
2063 req->status, req->actual, req->length);
David Brownell907cba32005-04-28 13:48:09 -07002064 req->context = NULL;
2065
2066 if (req != dev->stat_req)
2067 eth_req_free(ep, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068}
2069
2070static int rndis_control_ack (struct net_device *net)
2071{
2072 struct eth_dev *dev = netdev_priv(net);
Eric Sesterhenn55359022006-08-21 15:31:05 -07002073 int length;
David Brownell6cdee102005-04-18 17:39:34 -07002074 struct usb_request *resp = dev->stat_req;
David Brownell7e27f182006-06-13 09:54:40 -07002075
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 /* in case RNDIS calls this after disconnect */
David Brownell6cdee102005-04-18 17:39:34 -07002077 if (!dev->status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 DEBUG (dev, "status ENODEV\n");
2079 return -ENODEV;
2080 }
2081
David Brownell907cba32005-04-28 13:48:09 -07002082 /* in case queue length > 1 */
2083 if (resp->context) {
2084 resp = eth_req_alloc (dev->status_ep, 8, GFP_ATOMIC);
2085 if (!resp)
2086 return -ENOMEM;
2087 }
2088
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 /* Send RNDIS RESPONSE_AVAILABLE notification;
2090 * USB_CDC_NOTIFY_RESPONSE_AVAILABLE should work too
2091 */
2092 resp->length = 8;
2093 resp->complete = rndis_control_ack_complete;
David Brownell907cba32005-04-28 13:48:09 -07002094 resp->context = dev;
David Brownell7e27f182006-06-13 09:54:40 -07002095
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 *((__le32 *) resp->buf) = __constant_cpu_to_le32 (1);
2097 *((__le32 *) resp->buf + 1) = __constant_cpu_to_le32 (0);
David Brownell7e27f182006-06-13 09:54:40 -07002098
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 length = usb_ep_queue (dev->status_ep, resp, GFP_ATOMIC);
2100 if (length < 0) {
2101 resp->status = 0;
2102 rndis_control_ack_complete (dev->status_ep, resp);
2103 }
David Brownell7e27f182006-06-13 09:54:40 -07002104
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 return 0;
2106}
2107
David Brownell45e45ab2005-05-16 08:26:38 -07002108#else
2109
2110#define rndis_control_ack NULL
2111
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112#endif /* RNDIS */
2113
Al Viro55016f12005-10-21 03:21:58 -04002114static void eth_start (struct eth_dev *dev, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115{
Harvey Harrison441b62c2008-03-03 16:08:34 -08002116 DEBUG (dev, "%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117
2118 /* fill the rx queue */
2119 rx_fill (dev, gfp_flags);
2120
David Brownell7e27f182006-06-13 09:54:40 -07002121 /* and open the tx floodgates */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 atomic_set (&dev->tx_qlen, 0);
2123 netif_wake_queue (dev->net);
David Brownell45e45ab2005-05-16 08:26:38 -07002124 if (rndis_active(dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 rndis_set_param_medium (dev->rndis_config,
2126 NDIS_MEDIUM_802_3,
David Brownell6cdee102005-04-18 17:39:34 -07002127 BITRATE(dev->gadget)/100);
David Brownell907cba32005-04-28 13:48:09 -07002128 (void) rndis_signal_connect (dev->rndis_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130}
2131
2132static int eth_open (struct net_device *net)
2133{
2134 struct eth_dev *dev = netdev_priv(net);
2135
Harvey Harrison441b62c2008-03-03 16:08:34 -08002136 DEBUG (dev, "%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 if (netif_carrier_ok (dev->net))
2138 eth_start (dev, GFP_KERNEL);
2139 return 0;
2140}
2141
2142static int eth_stop (struct net_device *net)
2143{
2144 struct eth_dev *dev = netdev_priv(net);
2145
Harvey Harrison441b62c2008-03-03 16:08:34 -08002146 VDEBUG (dev, "%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 netif_stop_queue (net);
2148
2149 DEBUG (dev, "stop stats: rx/tx %ld/%ld, errs %ld/%ld\n",
David Brownell7e27f182006-06-13 09:54:40 -07002150 dev->stats.rx_packets, dev->stats.tx_packets,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 dev->stats.rx_errors, dev->stats.tx_errors
2152 );
2153
2154 /* ensure there are no more active requests */
2155 if (dev->config) {
2156 usb_ep_disable (dev->in_ep);
2157 usb_ep_disable (dev->out_ep);
2158 if (netif_carrier_ok (dev->net)) {
2159 DEBUG (dev, "host still using in/out endpoints\n");
2160 // FIXME idiom may leave toggle wrong here
2161 usb_ep_enable (dev->in_ep, dev->in);
2162 usb_ep_enable (dev->out_ep, dev->out);
2163 }
2164 if (dev->status_ep) {
2165 usb_ep_disable (dev->status_ep);
2166 usb_ep_enable (dev->status_ep, dev->status);
2167 }
2168 }
David Brownell7e27f182006-06-13 09:54:40 -07002169
David Brownell45e45ab2005-05-16 08:26:38 -07002170 if (rndis_active(dev)) {
David Brownell0cf4f2d2007-08-02 00:00:38 -07002171 rndis_set_param_medium(dev->rndis_config, NDIS_MEDIUM_802_3, 0);
David Brownell907cba32005-04-28 13:48:09 -07002172 (void) rndis_signal_disconnect (dev->rndis_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174
2175 return 0;
2176}
2177
2178/*-------------------------------------------------------------------------*/
2179
David Brownell907cba32005-04-28 13:48:09 -07002180static struct usb_request *
Al Viro55016f12005-10-21 03:21:58 -04002181eth_req_alloc (struct usb_ep *ep, unsigned size, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182{
2183 struct usb_request *req;
2184
David Brownell907cba32005-04-28 13:48:09 -07002185 req = usb_ep_alloc_request (ep, gfp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 if (!req)
2187 return NULL;
2188
David Brownell907cba32005-04-28 13:48:09 -07002189 req->buf = kmalloc (size, gfp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 if (!req->buf) {
2191 usb_ep_free_request (ep, req);
2192 req = NULL;
2193 }
2194 return req;
2195}
2196
2197static void
2198eth_req_free (struct usb_ep *ep, struct usb_request *req)
2199{
2200 kfree (req->buf);
2201 usb_ep_free_request (ep, req);
2202}
2203
2204
David Brownella3536782006-07-06 15:48:53 -07002205static void /* __init_or_exit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206eth_unbind (struct usb_gadget *gadget)
2207{
2208 struct eth_dev *dev = get_gadget_data (gadget);
2209
2210 DEBUG (dev, "unbind\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 rndis_deregister (dev->rndis_config);
2212 rndis_exit ();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213
2214 /* we've already been disconnected ... no i/o is active */
2215 if (dev->req) {
2216 eth_req_free (gadget->ep0, dev->req);
2217 dev->req = NULL;
2218 }
2219 if (dev->stat_req) {
2220 eth_req_free (dev->status_ep, dev->stat_req);
2221 dev->stat_req = NULL;
2222 }
2223
2224 unregister_netdev (dev->net);
2225 free_netdev(dev->net);
2226
2227 /* assuming we used keventd, it must quiesce too */
2228 flush_scheduled_work ();
2229 set_gadget_data (gadget, NULL);
2230}
2231
David Brownell0e530b42008-04-05 14:17:14 -07002232static u8 __init nibble (unsigned char c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233{
2234 if (likely (isdigit (c)))
2235 return c - '0';
2236 c = toupper (c);
2237 if (likely (isxdigit (c)))
2238 return 10 + c - 'A';
2239 return 0;
2240}
2241
David Brownell0e530b42008-04-05 14:17:14 -07002242static int __init get_ether_addr(const char *str, u8 *dev_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243{
2244 if (str) {
2245 unsigned i;
2246
2247 for (i = 0; i < 6; i++) {
2248 unsigned char num;
2249
2250 if((*str == '.') || (*str == ':'))
2251 str++;
2252 num = nibble(*str++) << 4;
2253 num |= (nibble(*str++));
2254 dev_addr [i] = num;
2255 }
2256 if (is_valid_ether_addr (dev_addr))
Aras Vaichas1afc64a2006-02-18 12:31:23 -08002257 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 }
2259 random_ether_addr(dev_addr);
Aras Vaichas1afc64a2006-02-18 12:31:23 -08002260 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261}
2262
David Brownell0e530b42008-04-05 14:17:14 -07002263static int __init
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264eth_bind (struct usb_gadget *gadget)
2265{
2266 struct eth_dev *dev;
2267 struct net_device *net;
2268 u8 cdc = 1, zlp = 1, rndis = 1;
2269 struct usb_ep *in_ep, *out_ep, *status_ep = NULL;
2270 int status = -ENOMEM;
David Brownell91e79c92005-07-13 15:18:30 -07002271 int gcnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272
2273 /* these flags are only ever cleared; compiler take note */
2274#ifndef DEV_CONFIG_CDC
2275 cdc = 0;
2276#endif
2277#ifndef CONFIG_USB_ETH_RNDIS
2278 rndis = 0;
2279#endif
2280
2281 /* Because most host side USB stacks handle CDC Ethernet, that
2282 * standard protocol is _strongly_ preferred for interop purposes.
2283 * (By everyone except Microsoft.)
2284 */
David Brownell91e79c92005-07-13 15:18:30 -07002285 if (gadget_is_pxa (gadget)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 /* pxa doesn't support altsettings */
2287 cdc = 0;
David Brownell729ed6d2006-08-30 13:24:56 -07002288 } else if (gadget_is_musbhdrc(gadget)) {
2289 /* reduce tx dma overhead by avoiding special cases */
2290 zlp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 } else if (gadget_is_sh(gadget)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 /* sh doesn't support multiple interfaces or configs */
2293 cdc = 0;
2294 rndis = 0;
2295 } else if (gadget_is_sa1100 (gadget)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 /* hardware can't write zlps */
2297 zlp = 0;
2298 /* sa1100 CAN do CDC, without status endpoint ... we use
2299 * non-CDC to be compatible with ARM Linux-2.4 "usb-eth".
2300 */
2301 cdc = 0;
David Brownell91e79c92005-07-13 15:18:30 -07002302 }
2303
2304 gcnum = usb_gadget_controller_number (gadget);
2305 if (gcnum >= 0)
2306 device_desc.bcdDevice = cpu_to_le16 (0x0200 + gcnum);
2307 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 /* can't assume CDC works. don't want to default to
2309 * anything less functional on CDC-capable hardware,
2310 * so we fail in this case.
2311 */
2312 dev_err (&gadget->dev,
2313 "controller '%s' not recognized\n",
2314 gadget->name);
2315 return -ENODEV;
2316 }
2317 snprintf (manufacturer, sizeof manufacturer, "%s %s/%s",
Serge E. Hallyn96b644b2006-10-02 02:18:13 -07002318 init_utsname()->sysname, init_utsname()->release,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 gadget->name);
2320
2321 /* If there's an RNDIS configuration, that's what Windows wants to
2322 * be using ... so use these product IDs here and in the "linux.inf"
2323 * needed to install MSFT drivers. Current Linux kernels will use
2324 * the second configuration if it's CDC Ethernet, and need some help
2325 * to choose the right configuration otherwise.
2326 */
2327 if (rndis) {
2328 device_desc.idVendor =
2329 __constant_cpu_to_le16(RNDIS_VENDOR_NUM);
2330 device_desc.idProduct =
2331 __constant_cpu_to_le16(RNDIS_PRODUCT_NUM);
2332 snprintf (product_desc, sizeof product_desc,
2333 "RNDIS/%s", driver_desc);
2334
2335 /* CDC subset ... recognized by Linux since 2.4.10, but Windows
David Brownell11d54892006-12-11 15:59:04 -08002336 * drivers aren't widely available. (That may be improved by
2337 * supporting one submode of the "SAFE" variant of MDLM.)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 */
2339 } else if (!cdc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 device_desc.idVendor =
2341 __constant_cpu_to_le16(SIMPLE_VENDOR_NUM);
2342 device_desc.idProduct =
2343 __constant_cpu_to_le16(SIMPLE_PRODUCT_NUM);
2344 }
2345
2346 /* support optional vendor/distro customization */
2347 if (idVendor) {
2348 if (!idProduct) {
2349 dev_err (&gadget->dev, "idVendor needs idProduct!\n");
2350 return -ENODEV;
2351 }
2352 device_desc.idVendor = cpu_to_le16(idVendor);
2353 device_desc.idProduct = cpu_to_le16(idProduct);
2354 if (bcdDevice)
2355 device_desc.bcdDevice = cpu_to_le16(bcdDevice);
2356 }
2357 if (iManufacturer)
2358 strlcpy (manufacturer, iManufacturer, sizeof manufacturer);
2359 if (iProduct)
2360 strlcpy (product_desc, iProduct, sizeof product_desc);
Aras Vaichas1afc64a2006-02-18 12:31:23 -08002361 if (iSerialNumber) {
2362 device_desc.iSerialNumber = STRING_SERIALNUMBER,
2363 strlcpy(serial_number, iSerialNumber, sizeof serial_number);
2364 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365
2366 /* all we really need is bulk IN/OUT */
2367 usb_ep_autoconfig_reset (gadget);
2368 in_ep = usb_ep_autoconfig (gadget, &fs_source_desc);
2369 if (!in_ep) {
2370autoconf_fail:
2371 dev_err (&gadget->dev,
2372 "can't autoconfigure on %s\n",
2373 gadget->name);
2374 return -ENODEV;
2375 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 in_ep->driver_data = in_ep; /* claim */
David Brownell7e27f182006-06-13 09:54:40 -07002377
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 out_ep = usb_ep_autoconfig (gadget, &fs_sink_desc);
2379 if (!out_ep)
2380 goto autoconf_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 out_ep->driver_data = out_ep; /* claim */
2382
2383#if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
2384 /* CDC Ethernet control interface doesn't require a status endpoint.
2385 * Since some hosts expect one, try to allocate one anyway.
2386 */
2387 if (cdc || rndis) {
2388 status_ep = usb_ep_autoconfig (gadget, &fs_status_desc);
2389 if (status_ep) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 status_ep->driver_data = status_ep; /* claim */
2391 } else if (rndis) {
2392 dev_err (&gadget->dev,
2393 "can't run RNDIS on %s\n",
2394 gadget->name);
2395 return -ENODEV;
2396#ifdef DEV_CONFIG_CDC
2397 /* pxa25x only does CDC subset; often used with RNDIS */
2398 } else if (cdc) {
2399 control_intf.bNumEndpoints = 0;
2400 /* FIXME remove endpoint from descriptor list */
2401#endif
2402 }
2403 }
2404#endif
2405
2406 /* one config: cdc, else minimal subset */
2407 if (!cdc) {
2408 eth_config.bNumInterfaces = 1;
2409 eth_config.iConfiguration = STRING_SUBSET;
David Brownell11d54892006-12-11 15:59:04 -08002410
2411 /* use functions to set these up, in case we're built to work
2412 * with multiple controllers and must override CDC Ethernet.
2413 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 fs_subset_descriptors();
2415 hs_subset_descriptors();
2416 }
2417
David Brownelle1394b42006-04-02 10:20:43 -08002418 device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
2419 usb_gadget_set_selfpowered (gadget);
2420
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 /* For now RNDIS is always a second config */
2422 if (rndis)
2423 device_desc.bNumConfigurations = 2;
2424
David Brownell0cf4f2d2007-08-02 00:00:38 -07002425 if (gadget_is_dualspeed(gadget)) {
2426 if (rndis)
2427 dev_qualifier.bNumConfigurations = 2;
2428 else if (!cdc)
2429 dev_qualifier.bDeviceClass = USB_CLASS_VENDOR_SPEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430
David Brownell0cf4f2d2007-08-02 00:00:38 -07002431 /* assumes ep0 uses the same value for both speeds ... */
2432 dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433
David Brownell0cf4f2d2007-08-02 00:00:38 -07002434 /* and that all endpoints are dual-speed */
2435 hs_source_desc.bEndpointAddress =
2436 fs_source_desc.bEndpointAddress;
2437 hs_sink_desc.bEndpointAddress =
2438 fs_sink_desc.bEndpointAddress;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439#if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
David Brownell0cf4f2d2007-08-02 00:00:38 -07002440 if (status_ep)
2441 hs_status_desc.bEndpointAddress =
2442 fs_status_desc.bEndpointAddress;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443#endif
David Brownell0cf4f2d2007-08-02 00:00:38 -07002444 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445
David Brownell0cf4f2d2007-08-02 00:00:38 -07002446 if (gadget_is_otg(gadget)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 otg_descriptor.bmAttributes |= USB_OTG_HNP,
2448 eth_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
2449 eth_config.bMaxPower = 4;
2450#ifdef CONFIG_USB_ETH_RNDIS
2451 rndis_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
2452 rndis_config.bMaxPower = 4;
2453#endif
2454 }
2455
David Brownell7e27f182006-06-13 09:54:40 -07002456 net = alloc_etherdev (sizeof *dev);
2457 if (!net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 return status;
2459 dev = netdev_priv(net);
2460 spin_lock_init (&dev->lock);
David Brownell789851c2006-08-21 15:26:38 -07002461 spin_lock_init (&dev->req_lock);
David Howellsc4028952006-11-22 14:57:56 +00002462 INIT_WORK (&dev->work, eth_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 INIT_LIST_HEAD (&dev->tx_reqs);
2464 INIT_LIST_HEAD (&dev->rx_reqs);
2465
2466 /* network device setup */
2467 dev->net = net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 strcpy (net->name, "usb%d");
2469 dev->cdc = cdc;
2470 dev->zlp = zlp;
2471
2472 dev->in_ep = in_ep;
2473 dev->out_ep = out_ep;
2474 dev->status_ep = status_ep;
2475
2476 /* Module params for these addresses should come from ID proms.
2477 * The host side address is used with CDC and RNDIS, and commonly
David Brownell11d54892006-12-11 15:59:04 -08002478 * ends up in a persistent config database. It's not clear if
2479 * host side code for the SAFE thing cares -- its original BLAN
2480 * thing didn't, Sharp never assigned those addresses on Zaurii.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 */
Aras Vaichas1afc64a2006-02-18 12:31:23 -08002482 if (get_ether_addr(dev_addr, net->dev_addr))
2483 dev_warn(&gadget->dev,
2484 "using random %s ethernet address\n", "self");
David Brownell11d54892006-12-11 15:59:04 -08002485 if (get_ether_addr(host_addr, dev->host_mac))
2486 dev_warn(&gadget->dev,
2487 "using random %s ethernet address\n", "host");
2488 snprintf (ethaddr, sizeof ethaddr, "%02X%02X%02X%02X%02X%02X",
2489 dev->host_mac [0], dev->host_mac [1],
2490 dev->host_mac [2], dev->host_mac [3],
2491 dev->host_mac [4], dev->host_mac [5]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492
2493 if (rndis) {
2494 status = rndis_init();
2495 if (status < 0) {
2496 dev_err (&gadget->dev, "can't init RNDIS, %d\n",
2497 status);
2498 goto fail;
2499 }
2500 }
2501
2502 net->change_mtu = eth_change_mtu;
2503 net->get_stats = eth_get_stats;
2504 net->hard_start_xmit = eth_start_xmit;
2505 net->open = eth_open;
2506 net->stop = eth_stop;
2507 // watchdog_timeo, tx_timeout ...
2508 // set_multicast_list
2509 SET_ETHTOOL_OPS(net, &ops);
2510
2511 /* preallocate control message data and buffer */
David Brownell907cba32005-04-28 13:48:09 -07002512 dev->req = eth_req_alloc (gadget->ep0, USB_BUFSIZ, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 if (!dev->req)
2514 goto fail;
2515 dev->req->complete = eth_setup_complete;
2516
2517 /* ... and maybe likewise for status transfer */
Ian Campbell05f33402005-06-29 10:15:32 +01002518#if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 if (dev->status_ep) {
2520 dev->stat_req = eth_req_alloc (dev->status_ep,
David Brownell907cba32005-04-28 13:48:09 -07002521 STATUS_BYTECOUNT, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 if (!dev->stat_req) {
2523 eth_req_free (gadget->ep0, dev->req);
2524 goto fail;
2525 }
David Brownell907cba32005-04-28 13:48:09 -07002526 dev->stat_req->context = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 }
David Brownell822e14a2005-06-13 06:55:03 -07002528#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529
2530 /* finish hookup to lower layer ... */
2531 dev->gadget = gadget;
2532 set_gadget_data (gadget, dev);
2533 gadget->ep0->driver_data = dev;
David Brownell7e27f182006-06-13 09:54:40 -07002534
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 /* two kinds of host-initiated state changes:
2536 * - iff DATA transfer is active, carrier is "on"
2537 * - tx queueing enabled if open *and* carrier is "on"
2538 */
2539 netif_stop_queue (dev->net);
2540 netif_carrier_off (dev->net);
2541
David Brownell7e27f182006-06-13 09:54:40 -07002542 SET_NETDEV_DEV (dev->net, &gadget->dev);
2543 status = register_netdev (dev->net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 if (status < 0)
2545 goto fail1;
2546
2547 INFO (dev, "%s, version: " DRIVER_VERSION "\n", driver_desc);
2548 INFO (dev, "using %s, OUT %s IN %s%s%s\n", gadget->name,
David Brownell907cba32005-04-28 13:48:09 -07002549 out_ep->name, in_ep->name,
2550 status_ep ? " STATUS " : "",
2551 status_ep ? status_ep->name : ""
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 );
2553 INFO (dev, "MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
2554 net->dev_addr [0], net->dev_addr [1],
2555 net->dev_addr [2], net->dev_addr [3],
2556 net->dev_addr [4], net->dev_addr [5]);
2557
2558 if (cdc || rndis)
2559 INFO (dev, "HOST MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
2560 dev->host_mac [0], dev->host_mac [1],
2561 dev->host_mac [2], dev->host_mac [3],
2562 dev->host_mac [4], dev->host_mac [5]);
2563
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564 if (rndis) {
2565 u32 vendorID = 0;
2566
2567 /* FIXME RNDIS vendor id == "vendor NIC code" == ? */
David Brownell7e27f182006-06-13 09:54:40 -07002568
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 dev->rndis_config = rndis_register (rndis_control_ack);
2570 if (dev->rndis_config < 0) {
2571fail0:
2572 unregister_netdev (dev->net);
2573 status = -ENODEV;
2574 goto fail;
2575 }
David Brownell7e27f182006-06-13 09:54:40 -07002576
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 /* these set up a lot of the OIDs that RNDIS needs */
2578 rndis_set_host_mac (dev->rndis_config, dev->host_mac);
2579 if (rndis_set_param_dev (dev->rndis_config, dev->net,
David Brownell340600a2005-04-28 13:45:25 -07002580 &dev->stats, &dev->cdc_filter))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 goto fail0;
David Brownell0cf4f2d2007-08-02 00:00:38 -07002582 if (rndis_set_param_vendor(dev->rndis_config, vendorID,
2583 manufacturer))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584 goto fail0;
David Brownell0cf4f2d2007-08-02 00:00:38 -07002585 if (rndis_set_param_medium(dev->rndis_config,
2586 NDIS_MEDIUM_802_3, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 goto fail0;
2588 INFO (dev, "RNDIS ready\n");
2589 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590
2591 return status;
2592
2593fail1:
2594 dev_dbg(&gadget->dev, "register_netdev failed, %d\n", status);
2595fail:
2596 eth_unbind (gadget);
2597 return status;
2598}
2599
2600/*-------------------------------------------------------------------------*/
2601
2602static void
2603eth_suspend (struct usb_gadget *gadget)
2604{
2605 struct eth_dev *dev = get_gadget_data (gadget);
2606
2607 DEBUG (dev, "suspend\n");
2608 dev->suspended = 1;
2609}
2610
2611static void
2612eth_resume (struct usb_gadget *gadget)
2613{
2614 struct eth_dev *dev = get_gadget_data (gadget);
2615
2616 DEBUG (dev, "resume\n");
2617 dev->suspended = 0;
2618}
2619
2620/*-------------------------------------------------------------------------*/
2621
2622static struct usb_gadget_driver eth_driver = {
David Brownell907cba32005-04-28 13:48:09 -07002623 .speed = DEVSPEED,
2624
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 .function = (char *) driver_desc,
2626 .bind = eth_bind,
Tony Lindgrenbfb2c962006-06-29 22:27:36 -07002627 .unbind = eth_unbind,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628
2629 .setup = eth_setup,
2630 .disconnect = eth_disconnect,
2631
2632 .suspend = eth_suspend,
2633 .resume = eth_resume,
2634
David Brownell7e27f182006-06-13 09:54:40 -07002635 .driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 .name = (char *) shortname,
Ben Dooksd0d50492005-10-10 10:52:33 +01002637 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 },
2639};
2640
2641MODULE_DESCRIPTION (DRIVER_DESC);
2642MODULE_AUTHOR ("David Brownell, Benedikt Spanger");
2643MODULE_LICENSE ("GPL");
2644
2645
2646static int __init init (void)
2647{
2648 return usb_gadget_register_driver (&eth_driver);
2649}
2650module_init (init);
2651
2652static void __exit cleanup (void)
2653{
2654 usb_gadget_unregister_driver (&eth_driver);
2655}
2656module_exit (cleanup);
2657