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