]> err.no Git - linux-2.6/blob - drivers/usb/net/usbnet.c
[PATCH] USB: usbnet (1/9) clean up framing
[linux-2.6] / drivers / usb / net / usbnet.c
1 /*
2  * USB Networking Links
3  * Copyright (C) 2000-2005 by David Brownell
4  * Copyright (C) 2002 Pavel Machek <pavel@ucw.cz>
5  * Copyright (C) 2003-2005 David Hollis <dhollis@davehollis.com>
6  * Copyright (C) 2005 Phil Chang <pchang23@sbcglobal.net>
7  * Copyright (c) 2002-2003 TiVo Inc.
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  */
23
24 /*
25  * This is a generic "USB networking" framework that works with several
26  * kinds of full and high speed networking devices:
27  *
28  *   + USB host-to-host "network cables", used for IP-over-USB links.
29  *     These are often used for Laplink style connectivity products.
30  *      - AnchorChip 2720
31  *      - Belkin, eTEK (interops with Win32 drivers)
32  *      - GeneSys GL620USB-A
33  *      - NetChip 1080 (interoperates with NetChip Win32 drivers)
34  *      - Prolific PL-2301/2302 (replaces "plusb" driver)
35  *      - KC Technology KC2190
36  *
37  *   + Smart USB devices can support such links directly, using Internet
38  *     standard protocols instead of proprietary host-to-device links.
39  *      - Linux PDAs like iPaq, Yopy, and Zaurus
40  *      - The BLOB boot loader (for diskless booting)
41  *      - Linux "gadgets", perhaps using PXA-2xx or Net2280 controllers
42  *      - Devices using EPSON's sample USB firmware
43  *      - CDC-Ethernet class devices, such as many cable modems
44  *
45  *   + Adapters to networks such as Ethernet.
46  *      - AX8817X based USB 2.0 products
47  *
48  * Links to these devices can be bridged using Linux Ethernet bridging.
49  * With minor exceptions, these all use similar USB framing for network
50  * traffic, but need different protocols for control traffic.
51  *
52  * USB devices can implement their side of this protocol at the cost
53  * of two bulk endpoints; it's not restricted to "cable" applications.
54  * See the SA1110, Zaurus, or EPSON device/client support in this driver;
55  * slave/target drivers such as "usb-eth" (on most SA-1100 PDAs) or
56  * "g_ether" (in the Linux "gadget" framework) implement that behavior
57  * within devices.
58  *
59  *
60  * CHANGELOG:
61  *
62  * 13-sep-2000  experimental, new
63  * 10-oct-2000  usb_device_id table created. 
64  * 28-oct-2000  misc fixes; mostly, discard more TTL-mangled rx packets.
65  * 01-nov-2000  usb_device_id table and probing api update by
66  *              Adam J. Richter <adam@yggdrasil.com>.
67  * 18-dec-2000  (db) tx watchdog, "net1080" renaming to "usbnet", device_info
68  *              and prolific support, isolate net1080-specific bits, cleanup.
69  *              fix unlink_urbs oops in D3 PM resume code path.
70  *
71  * 02-feb-2001  (db) fix tx skb sharing, packet length, match_flags, ...
72  * 08-feb-2001  stubbed in "linuxdev", maybe the SA-1100 folk can use it;
73  *              AnchorChips 2720 support (from spec) for testing;
74  *              fix bit-ordering problem with ethernet multicast addr
75  * 19-feb-2001  Support for clearing halt conditions. SA1100 UDC support
76  *              updates. Oleg Drokin (green@iXcelerator.com)
77  * 25-mar-2001  More SA-1100 updates, including workaround for ip problem
78  *              expecting cleared skb->cb and framing change to match latest
79  *              handhelds.org version (Oleg).  Enable device IDs from the
80  *              Win32 Belkin driver; other cleanups (db).
81  * 16-jul-2001  Bugfixes for uhci oops-on-unplug, Belkin support, various
82  *              cleanups for problems not yet seen in the field. (db)
83  * 17-oct-2001  Handle "Advance USBNET" product, like Belkin/eTEK devices,
84  *              from Ioannis Mavroukakis <i.mavroukakis@btinternet.com>;
85  *              rx unlinks somehow weren't async; minor cleanup.
86  * 03-nov-2001  Merged GeneSys driver; original code from Jiun-Jie Huang
87  *              <huangjj@genesyslogic.com.tw>, updated by Stanislav Brabec
88  *              <utx@penguin.cz>.  Made framing options (NetChip/GeneSys)
89  *              tie mostly to (sub)driver info.  Workaround some PL-2302
90  *              chips that seem to reject SET_INTERFACE requests.
91  *
92  * 06-apr-2002  Added ethtool support, based on a patch from Brad Hards.
93  *              Level of diagnostics is more configurable; they use device
94  *              location (usb_device->devpath) instead of address (2.5).
95  *              For tx_fixup, memflags can't be NOIO.
96  * 07-may-2002  Generalize/cleanup keventd support, handling rx stalls (mostly
97  *              for USB 2.0 TTs) and memory shortages (potential) too. (db)
98  *              Use "locally assigned" IEEE802 address space. (Brad Hards)
99  * 18-oct-2002  Support for Zaurus (Pavel Machek), related cleanup (db).
100  * 14-dec-2002  Remove Zaurus-private crc32 code (Pavel); 2.5 oops fix,
101  *              cleanups and stubbed PXA-250 support (db), fix for framing
102  *              issues on Z, net1080, and gl620a (Toby Milne)
103  *
104  * 31-mar-2003  Use endpoint descriptors:  high speed support, simpler sa1100
105  *              vs pxa25x, and CDC Ethernet.  Throttle down log floods on
106  *              disconnect; other cleanups. (db)  Flush net1080 fifos
107  *              after several sequential framing errors. (Johannes Erdfelt)
108  * 22-aug-2003  AX8817X support (Dave Hollis).
109  * 14-jun-2004  Trivial patch for AX8817X based Buffalo LUA-U2-KTX in Japan
110  *              (Neil Bortnak)
111  * 03-nov-2004  Trivial patch for KC2190 (KC-190) chip. (Jonathan McDowell)
112  *
113  * 01-feb-2005  AX88772 support (Phil Chang & Dave Hollis)
114  *-------------------------------------------------------------------------*/
115
116 // #define      DEBUG                   // error path messages, extra info
117 // #define      VERBOSE                 // more; success messages
118
119 #include <linux/config.h>
120 #ifdef  CONFIG_USB_DEBUG
121 #   define DEBUG
122 #endif
123 #include <linux/module.h>
124 #include <linux/kmod.h>
125 #include <linux/sched.h>
126 #include <linux/init.h>
127 #include <linux/netdevice.h>
128 #include <linux/etherdevice.h>
129 #include <linux/ethtool.h>
130 #include <linux/workqueue.h>
131 #include <linux/mii.h>
132 #include <linux/usb.h>
133 #include <linux/mm.h>
134 #include <linux/dma-mapping.h>
135
136 #include <asm/unaligned.h>
137
138 #include "usbnet.h"
139
140 #define DRIVER_VERSION          "22-Aug-2005"
141
142
143 /*-------------------------------------------------------------------------*/
144
145 /*
146  * Nineteen USB 1.1 max size bulk transactions per frame (ms), max.
147  * Several dozen bytes of IPv4 data can fit in two such transactions.
148  * One maximum size Ethernet packet takes twenty four of them.
149  * For high speed, each frame comfortably fits almost 36 max size
150  * Ethernet packets (so queues should be bigger).
151  *
152  * REVISIT qlens should be members of 'struct usbnet'; the goal is to
153  * let the USB host controller be busy for 5msec or more before an irq
154  * is required, under load.  Jumbograms change the equation.
155  */
156 #define RX_QLEN(dev) (((dev)->udev->speed == USB_SPEED_HIGH) ? 60 : 4)
157 #define TX_QLEN(dev) (((dev)->udev->speed == USB_SPEED_HIGH) ? 60 : 4)
158
159 /* packets are always ethernet, sometimes wrapped in other framing */
160 #define MIN_PACKET      sizeof(struct ethhdr)
161
162 // reawaken network queue this soon after stopping; else watchdog barks
163 #define TX_TIMEOUT_JIFFIES      (5*HZ)
164
165 // throttle rx/tx briefly after some faults, so khubd might disconnect()
166 // us (it polls at HZ/4 usually) before we report too many false errors.
167 #define THROTTLE_JIFFIES        (HZ/8)
168
169 // for vendor-specific control operations
170 #define CONTROL_TIMEOUT_MS      USB_CTRL_GET_TIMEOUT
171
172 // between wakeups
173 #define UNLINK_TIMEOUT_MS       3
174
175 /*-------------------------------------------------------------------------*/
176
177 // randomly generated ethernet address
178 static u8       node_id [ETH_ALEN];
179
180 static const char driver_name [] = "usbnet";
181
182 /* use ethtool to change the level for any given device */
183 static int msg_level = -1;
184 module_param (msg_level, int, 0);
185 MODULE_PARM_DESC (msg_level, "Override default message level");
186
187 /*-------------------------------------------------------------------------*/
188
189 static void usbnet_get_drvinfo (struct net_device *, struct ethtool_drvinfo *);
190 static u32 usbnet_get_link (struct net_device *);
191 static u32 usbnet_get_msglevel (struct net_device *);
192 static void usbnet_set_msglevel (struct net_device *, u32);
193 static void defer_kevent (struct usbnet *, int);
194
195 /* mostly for PDA style devices, which are always connected if present */
196 static int always_connected (struct usbnet *dev)
197 {
198         return 0;
199 }
200
201 /* handles CDC Ethernet and many other network "bulk data" interfaces */
202 static int
203 get_endpoints (struct usbnet *dev, struct usb_interface *intf)
204 {
205         int                             tmp;
206         struct usb_host_interface       *alt = NULL;
207         struct usb_host_endpoint        *in = NULL, *out = NULL;
208         struct usb_host_endpoint        *status = NULL;
209
210         for (tmp = 0; tmp < intf->num_altsetting; tmp++) {
211                 unsigned        ep;
212
213                 in = out = status = NULL;
214                 alt = intf->altsetting + tmp;
215
216                 /* take the first altsetting with in-bulk + out-bulk;
217                  * remember any status endpoint, just in case;
218                  * ignore other endpoints and altsetttings.
219                  */
220                 for (ep = 0; ep < alt->desc.bNumEndpoints; ep++) {
221                         struct usb_host_endpoint        *e;
222                         int                             intr = 0;
223
224                         e = alt->endpoint + ep;
225                         switch (e->desc.bmAttributes) {
226                         case USB_ENDPOINT_XFER_INT:
227                                 if (!(e->desc.bEndpointAddress & USB_DIR_IN))
228                                         continue;
229                                 intr = 1;
230                                 /* FALLTHROUGH */
231                         case USB_ENDPOINT_XFER_BULK:
232                                 break;
233                         default:
234                                 continue;
235                         }
236                         if (e->desc.bEndpointAddress & USB_DIR_IN) {
237                                 if (!intr && !in)
238                                         in = e;
239                                 else if (intr && !status)
240                                         status = e;
241                         } else {
242                                 if (!out)
243                                         out = e;
244                         }
245                 }
246                 if (in && out)
247                         break;
248         }
249         if (!alt || !in || !out)
250                 return -EINVAL;
251
252         if (alt->desc.bAlternateSetting != 0
253                         || !(dev->driver_info->flags & FLAG_NO_SETINT)) {
254                 tmp = usb_set_interface (dev->udev, alt->desc.bInterfaceNumber,
255                                 alt->desc.bAlternateSetting);
256                 if (tmp < 0)
257                         return tmp;
258         }
259         
260         dev->in = usb_rcvbulkpipe (dev->udev,
261                         in->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
262         dev->out = usb_sndbulkpipe (dev->udev,
263                         out->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
264         dev->status = status;
265         return 0;
266 }
267
268 static void intr_complete (struct urb *urb, struct pt_regs *regs);
269
270 static int init_status (struct usbnet *dev, struct usb_interface *intf)
271 {
272         char            *buf = NULL;
273         unsigned        pipe = 0;
274         unsigned        maxp;
275         unsigned        period;
276
277         if (!dev->driver_info->status)
278                 return 0;
279
280         pipe = usb_rcvintpipe (dev->udev,
281                         dev->status->desc.bEndpointAddress
282                                 & USB_ENDPOINT_NUMBER_MASK);
283         maxp = usb_maxpacket (dev->udev, pipe, 0);
284
285         /* avoid 1 msec chatter:  min 8 msec poll rate */
286         period = max ((int) dev->status->desc.bInterval,
287                 (dev->udev->speed == USB_SPEED_HIGH) ? 7 : 3);
288
289         buf = kmalloc (maxp, SLAB_KERNEL);
290         if (buf) {
291                 dev->interrupt = usb_alloc_urb (0, SLAB_KERNEL);
292                 if (!dev->interrupt) {
293                         kfree (buf);
294                         return -ENOMEM;
295                 } else {
296                         usb_fill_int_urb(dev->interrupt, dev->udev, pipe,
297                                 buf, maxp, intr_complete, dev, period);
298                         dev_dbg(&intf->dev,
299                                 "status ep%din, %d bytes period %d\n",
300                                 usb_pipeendpoint(pipe), maxp, period);
301                 }
302         }
303         return  0;
304 }
305
306 static void skb_return (struct usbnet *dev, struct sk_buff *skb)
307 {
308         int     status;
309
310         skb->dev = dev->net;
311         skb->protocol = eth_type_trans (skb, dev->net);
312         dev->stats.rx_packets++;
313         dev->stats.rx_bytes += skb->len;
314
315         if (netif_msg_rx_status (dev))
316                 devdbg (dev, "< rx, len %zu, type 0x%x",
317                         skb->len + sizeof (struct ethhdr), skb->protocol);
318         memset (skb->cb, 0, sizeof (struct skb_data));
319         status = netif_rx (skb);
320         if (status != NET_RX_SUCCESS && netif_msg_rx_err (dev))
321                 devdbg (dev, "netif_rx status %d", status);
322 }
323
324 \f
325 #ifdef  CONFIG_USB_ALI_M5632
326 #define HAVE_HARDWARE
327
328 /*-------------------------------------------------------------------------
329  *
330  * ALi M5632 driver ... does high speed
331  *
332  *-------------------------------------------------------------------------*/
333
334 static const struct driver_info ali_m5632_info = {
335         .description =  "ALi M5632",
336 };
337
338
339 #endif
340
341 \f
342 #ifdef  CONFIG_USB_AN2720
343 #define HAVE_HARDWARE
344
345 /*-------------------------------------------------------------------------
346  *
347  * AnchorChips 2720 driver ... http://www.cypress.com
348  *
349  * This doesn't seem to have a way to detect whether the peer is
350  * connected, or need any reset handshaking.  It's got pretty big
351  * internal buffers (handles most of a frame's worth of data).
352  * Chip data sheets don't describe any vendor control messages.
353  *
354  *-------------------------------------------------------------------------*/
355
356 static const struct driver_info an2720_info = {
357         .description =  "AnchorChips/Cypress 2720",
358         // no reset available!
359         // no check_connect available!
360
361         .in = 2, .out = 2,              // direction distinguishes these
362 };
363
364 #endif  /* CONFIG_USB_AN2720 */
365
366 \f
367 #ifdef CONFIG_USB_AX8817X
368 /* ASIX AX8817X based USB 2.0 Ethernet Devices */
369
370 #define HAVE_HARDWARE
371 #define NEED_MII
372
373 #include <linux/crc32.h>
374
375 #define AX_CMD_SET_SW_MII               0x06
376 #define AX_CMD_READ_MII_REG             0x07
377 #define AX_CMD_WRITE_MII_REG            0x08
378 #define AX_CMD_SET_HW_MII               0x0a
379 #define AX_CMD_READ_EEPROM              0x0b
380 #define AX_CMD_WRITE_EEPROM             0x0c
381 #define AX_CMD_WRITE_ENABLE             0x0d
382 #define AX_CMD_WRITE_DISABLE            0x0e
383 #define AX_CMD_WRITE_RX_CTL             0x10
384 #define AX_CMD_READ_IPG012              0x11
385 #define AX_CMD_WRITE_IPG0               0x12
386 #define AX_CMD_WRITE_IPG1               0x13
387 #define AX_CMD_WRITE_IPG2               0x14
388 #define AX_CMD_WRITE_MULTI_FILTER       0x16
389 #define AX_CMD_READ_NODE_ID             0x17
390 #define AX_CMD_READ_PHY_ID              0x19
391 #define AX_CMD_READ_MEDIUM_STATUS       0x1a
392 #define AX_CMD_WRITE_MEDIUM_MODE        0x1b
393 #define AX_CMD_READ_MONITOR_MODE        0x1c
394 #define AX_CMD_WRITE_MONITOR_MODE       0x1d
395 #define AX_CMD_WRITE_GPIOS              0x1f
396 #define AX_CMD_SW_RESET                 0x20
397 #define AX_CMD_SW_PHY_STATUS            0x21
398 #define AX_CMD_SW_PHY_SELECT            0x22
399 #define AX88772_CMD_READ_NODE_ID        0x13
400
401 #define AX_MONITOR_MODE                 0x01
402 #define AX_MONITOR_LINK                 0x02
403 #define AX_MONITOR_MAGIC                0x04
404 #define AX_MONITOR_HSFS                 0x10
405
406 /* AX88172 Medium Status Register values */
407 #define AX_MEDIUM_FULL_DUPLEX           0x02
408 #define AX_MEDIUM_TX_ABORT_ALLOW        0x04
409 #define AX_MEDIUM_FLOW_CONTROL_EN       0x10
410
411 #define AX_MCAST_FILTER_SIZE            8
412 #define AX_MAX_MCAST                    64
413
414 #define AX_EEPROM_LEN                   0x40
415
416 #define AX_SWRESET_CLEAR                0x00
417 #define AX_SWRESET_RR                   0x01
418 #define AX_SWRESET_RT                   0x02
419 #define AX_SWRESET_PRTE                 0x04
420 #define AX_SWRESET_PRL                  0x08
421 #define AX_SWRESET_BZ                   0x10
422 #define AX_SWRESET_IPRL                 0x20
423 #define AX_SWRESET_IPPD                 0x40
424
425 #define AX88772_IPG0_DEFAULT            0x15
426 #define AX88772_IPG1_DEFAULT            0x0c
427 #define AX88772_IPG2_DEFAULT            0x12
428
429 #define AX88772_MEDIUM_FULL_DUPLEX      0x0002
430 #define AX88772_MEDIUM_RESERVED         0x0004
431 #define AX88772_MEDIUM_RX_FC_ENABLE     0x0010
432 #define AX88772_MEDIUM_TX_FC_ENABLE     0x0020
433 #define AX88772_MEDIUM_PAUSE_FORMAT     0x0080
434 #define AX88772_MEDIUM_RX_ENABLE        0x0100
435 #define AX88772_MEDIUM_100MB            0x0200
436 #define AX88772_MEDIUM_DEFAULT  \
437         (AX88772_MEDIUM_FULL_DUPLEX | AX88772_MEDIUM_RX_FC_ENABLE | \
438          AX88772_MEDIUM_TX_FC_ENABLE | AX88772_MEDIUM_100MB | \
439          AX88772_MEDIUM_RESERVED | AX88772_MEDIUM_RX_ENABLE )
440
441 #define AX_EEPROM_MAGIC                 0xdeadbeef
442
443 /* This structure cannot exceed sizeof(unsigned long [5]) AKA 20 bytes */
444 struct ax8817x_data {
445         u8 multi_filter[AX_MCAST_FILTER_SIZE];
446 };
447
448 struct ax88172_int_data {
449         u16 res1;
450         u8 link;
451         u16 res2;
452         u8 status;
453         u16 res3;
454 } __attribute__ ((packed));
455
456 static int ax8817x_read_cmd(struct usbnet *dev, u8 cmd, u16 value, u16 index,
457                             u16 size, void *data)
458 {
459         return usb_control_msg(
460                 dev->udev,
461                 usb_rcvctrlpipe(dev->udev, 0),
462                 cmd,
463                 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
464                 value,
465                 index,
466                 data,
467                 size,
468                 CONTROL_TIMEOUT_MS);
469 }
470
471 static int ax8817x_write_cmd(struct usbnet *dev, u8 cmd, u16 value, u16 index,
472                              u16 size, void *data)
473 {
474         return usb_control_msg(
475                 dev->udev,
476                 usb_sndctrlpipe(dev->udev, 0),
477                 cmd,
478                 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
479                 value,
480                 index,
481                 data,
482                 size,
483                 CONTROL_TIMEOUT_MS);
484 }
485
486 static void ax8817x_async_cmd_callback(struct urb *urb, struct pt_regs *regs)
487 {
488         struct usb_ctrlrequest *req = (struct usb_ctrlrequest *)urb->context;
489
490         if (urb->status < 0)
491                 printk(KERN_DEBUG "ax8817x_async_cmd_callback() failed with %d",
492                         urb->status);
493
494         kfree(req);
495         usb_free_urb(urb);
496 }
497
498 static void ax8817x_status(struct usbnet *dev, struct urb *urb)
499 {
500         struct ax88172_int_data *event;
501         int link;
502
503         if (urb->actual_length < 8)
504                 return;
505
506         event = urb->transfer_buffer;
507         link = event->link & 0x01;
508         if (netif_carrier_ok(dev->net) != link) {
509                 if (link) {
510                         netif_carrier_on(dev->net);
511                         defer_kevent (dev, EVENT_LINK_RESET );
512                 } else
513                         netif_carrier_off(dev->net);
514                 devdbg(dev, "ax8817x - Link Status is: %d", link);
515         }
516 }
517
518 static void ax8817x_write_cmd_async(struct usbnet *dev, u8 cmd, u16 value, u16 index,
519                                     u16 size, void *data)
520 {
521         struct usb_ctrlrequest *req;
522         int status;
523         struct urb *urb;
524
525         if ((urb = usb_alloc_urb(0, GFP_ATOMIC)) == NULL) {
526                 devdbg(dev, "Error allocating URB in write_cmd_async!");
527                 return;
528         }
529
530         if ((req = kmalloc(sizeof(struct usb_ctrlrequest), GFP_ATOMIC)) == NULL) {
531                 deverr(dev, "Failed to allocate memory for control request");
532                 usb_free_urb(urb);
533                 return;
534         }
535
536         req->bRequestType = USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE;
537         req->bRequest = cmd;
538         req->wValue = cpu_to_le16(value);
539         req->wIndex = cpu_to_le16(index); 
540         req->wLength = cpu_to_le16(size);
541
542         usb_fill_control_urb(urb, dev->udev,
543                              usb_sndctrlpipe(dev->udev, 0),
544                              (void *)req, data, size,
545                              ax8817x_async_cmd_callback, req);
546
547         if((status = usb_submit_urb(urb, GFP_ATOMIC)) < 0) {
548                 deverr(dev, "Error submitting the control message: status=%d", status);
549                 kfree(req);
550                 usb_free_urb(urb);
551         }
552 }
553
554 static void ax8817x_set_multicast(struct net_device *net)
555 {
556         struct usbnet *dev = netdev_priv(net);
557         struct ax8817x_data *data = (struct ax8817x_data *)&dev->data;
558         u8 rx_ctl = 0x8c;
559
560         if (net->flags & IFF_PROMISC) {
561                 rx_ctl |= 0x01;
562         } else if (net->flags & IFF_ALLMULTI
563                    || net->mc_count > AX_MAX_MCAST) {
564                 rx_ctl |= 0x02;
565         } else if (net->mc_count == 0) {
566                 /* just broadcast and directed */
567         } else {
568                 /* We use the 20 byte dev->data
569                  * for our 8 byte filter buffer
570                  * to avoid allocating memory that
571                  * is tricky to free later */
572                 struct dev_mc_list *mc_list = net->mc_list;
573                 u32 crc_bits;
574                 int i;
575
576                 memset(data->multi_filter, 0, AX_MCAST_FILTER_SIZE);
577
578                 /* Build the multicast hash filter. */
579                 for (i = 0; i < net->mc_count; i++) {
580                         crc_bits =
581                             ether_crc(ETH_ALEN,
582                                       mc_list->dmi_addr) >> 26;
583                         data->multi_filter[crc_bits >> 3] |=
584                             1 << (crc_bits & 7);
585                         mc_list = mc_list->next;
586                 }
587
588                 ax8817x_write_cmd_async(dev, AX_CMD_WRITE_MULTI_FILTER, 0, 0,
589                                    AX_MCAST_FILTER_SIZE, data->multi_filter);
590
591                 rx_ctl |= 0x10;
592         }
593
594         ax8817x_write_cmd_async(dev, AX_CMD_WRITE_RX_CTL, rx_ctl, 0, 0, NULL);
595 }
596
597 static int ax8817x_mdio_read(struct net_device *netdev, int phy_id, int loc)
598 {
599         struct usbnet *dev = netdev_priv(netdev);
600         u16 res;
601         u8 buf[1];
602
603         ax8817x_write_cmd(dev, AX_CMD_SET_SW_MII, 0, 0, 0, &buf);
604         ax8817x_read_cmd(dev, AX_CMD_READ_MII_REG, phy_id, (__u16)loc, 2, (u16 *)&res);
605         ax8817x_write_cmd(dev, AX_CMD_SET_HW_MII, 0, 0, 0, &buf);
606
607         return res & 0xffff;
608 }
609
610 static void ax8817x_mdio_write(struct net_device *netdev, int phy_id, int loc, int val)
611 {
612         struct usbnet *dev = netdev_priv(netdev);
613         u16 res = val;
614         u8 buf[1];
615
616         ax8817x_write_cmd(dev, AX_CMD_SET_SW_MII, 0, 0, 0, &buf);
617         ax8817x_write_cmd(dev, AX_CMD_WRITE_MII_REG, phy_id, (__u16)loc, 2, (u16 *)&res);
618         ax8817x_write_cmd(dev, AX_CMD_SET_HW_MII, 0, 0, 0, &buf);
619 }
620
621 static int ax88172_link_reset(struct usbnet *dev)
622 {
623         u16 lpa;
624         u8 mode;
625
626         mode = AX_MEDIUM_TX_ABORT_ALLOW | AX_MEDIUM_FLOW_CONTROL_EN;
627         lpa = ax8817x_mdio_read(dev->net, dev->mii.phy_id, MII_LPA);
628         if (lpa & LPA_DUPLEX)
629                 mode |= AX_MEDIUM_FULL_DUPLEX;
630         ax8817x_write_cmd(dev, AX_CMD_WRITE_MEDIUM_MODE, mode, 0, 0, NULL);
631
632         return 0;
633 }
634
635 static void ax8817x_get_wol(struct net_device *net, struct ethtool_wolinfo *wolinfo)
636 {
637         struct usbnet *dev = netdev_priv(net);
638         u8 opt;
639
640         if (ax8817x_read_cmd(dev, AX_CMD_READ_MONITOR_MODE, 0, 0, 1, &opt) < 0) {
641                 wolinfo->supported = 0;
642                 wolinfo->wolopts = 0;
643                 return;
644         }
645         wolinfo->supported = WAKE_PHY | WAKE_MAGIC;
646         wolinfo->wolopts = 0;
647         if (opt & AX_MONITOR_MODE) {
648                 if (opt & AX_MONITOR_LINK)
649                         wolinfo->wolopts |= WAKE_PHY;
650                 if (opt & AX_MONITOR_MAGIC)
651                         wolinfo->wolopts |= WAKE_MAGIC;
652         }
653 }
654
655 static int ax8817x_set_wol(struct net_device *net, struct ethtool_wolinfo *wolinfo)
656 {
657         struct usbnet *dev = netdev_priv(net);
658         u8 opt = 0;
659         u8 buf[1];
660
661         if (wolinfo->wolopts & WAKE_PHY)
662                 opt |= AX_MONITOR_LINK;
663         if (wolinfo->wolopts & WAKE_MAGIC)
664                 opt |= AX_MONITOR_MAGIC;
665         if (opt != 0)
666                 opt |= AX_MONITOR_MODE;
667
668         if (ax8817x_write_cmd(dev, AX_CMD_WRITE_MONITOR_MODE,
669                               opt, 0, 0, &buf) < 0)
670                 return -EINVAL;
671
672         return 0;
673 }
674
675 static int ax8817x_get_eeprom_len(struct net_device *net)
676 {
677         return AX_EEPROM_LEN;
678 }
679
680 static int ax8817x_get_eeprom(struct net_device *net,
681                               struct ethtool_eeprom *eeprom, u8 *data)
682 {
683         struct usbnet *dev = netdev_priv(net);
684         u16 *ebuf = (u16 *)data;
685         int i;
686
687         /* Crude hack to ensure that we don't overwrite memory
688          * if an odd length is supplied
689          */
690         if (eeprom->len % 2)
691                 return -EINVAL;
692
693         eeprom->magic = AX_EEPROM_MAGIC;
694
695         /* ax8817x returns 2 bytes from eeprom on read */
696         for (i=0; i < eeprom->len / 2; i++) {
697                 if (ax8817x_read_cmd(dev, AX_CMD_READ_EEPROM, 
698                         eeprom->offset + i, 0, 2, &ebuf[i]) < 0)
699                         return -EINVAL;
700         }
701         return 0;
702 }
703
704 static void ax8817x_get_drvinfo (struct net_device *net,
705                                  struct ethtool_drvinfo *info)
706 {
707         /* Inherit standard device info */
708         usbnet_get_drvinfo(net, info);
709         info->eedump_len = 0x3e;
710 }
711
712 static int ax8817x_get_settings(struct net_device *net, struct ethtool_cmd *cmd)
713 {
714         struct usbnet *dev = netdev_priv(net);
715
716         return mii_ethtool_gset(&dev->mii,cmd);
717 }
718
719 static int ax8817x_set_settings(struct net_device *net, struct ethtool_cmd *cmd)
720 {
721         struct usbnet *dev = netdev_priv(net);
722
723         return mii_ethtool_sset(&dev->mii,cmd);
724 }
725
726 /* We need to override some ethtool_ops so we require our
727    own structure so we don't interfere with other usbnet
728    devices that may be connected at the same time. */
729 static struct ethtool_ops ax8817x_ethtool_ops = {
730         .get_drvinfo            = ax8817x_get_drvinfo,
731         .get_link               = ethtool_op_get_link,
732         .get_msglevel           = usbnet_get_msglevel,
733         .set_msglevel           = usbnet_set_msglevel,
734         .get_wol                = ax8817x_get_wol,
735         .set_wol                = ax8817x_set_wol,
736         .get_eeprom_len         = ax8817x_get_eeprom_len,
737         .get_eeprom             = ax8817x_get_eeprom,
738         .get_settings           = ax8817x_get_settings,
739         .set_settings           = ax8817x_set_settings,
740 };
741
742 static int ax8817x_bind(struct usbnet *dev, struct usb_interface *intf)
743 {
744         int ret = 0;
745         void *buf;
746         int i;
747         unsigned long gpio_bits = dev->driver_info->data;
748
749         get_endpoints(dev,intf);
750
751         buf = kmalloc(ETH_ALEN, GFP_KERNEL);
752         if(!buf) {
753                 ret = -ENOMEM;
754                 goto out1;
755         }
756
757         /* Toggle the GPIOs in a manufacturer/model specific way */
758         for (i = 2; i >= 0; i--) {
759                 if ((ret = ax8817x_write_cmd(dev, AX_CMD_WRITE_GPIOS,
760                                         (gpio_bits >> (i * 8)) & 0xff, 0, 0,
761                                         buf)) < 0)
762                         goto out2;
763                 msleep(5);
764         }
765
766         if ((ret = ax8817x_write_cmd(dev, AX_CMD_WRITE_RX_CTL, 0x80, 0, 0, buf)) < 0) {
767                 dbg("send AX_CMD_WRITE_RX_CTL failed: %d", ret);
768                 goto out2;
769         }
770
771         /* Get the MAC address */
772         memset(buf, 0, ETH_ALEN);
773         if ((ret = ax8817x_read_cmd(dev, AX_CMD_READ_NODE_ID, 0, 0, 6, buf)) < 0) {
774                 dbg("read AX_CMD_READ_NODE_ID failed: %d", ret);
775                 goto out2;
776         }
777         memcpy(dev->net->dev_addr, buf, ETH_ALEN);
778
779         /* Get the PHY id */
780         if ((ret = ax8817x_read_cmd(dev, AX_CMD_READ_PHY_ID, 0, 0, 2, buf)) < 0) {
781                 dbg("error on read AX_CMD_READ_PHY_ID: %02x", ret);
782                 goto out2;
783         } else if (ret < 2) {
784                 /* this should always return 2 bytes */
785                 dbg("AX_CMD_READ_PHY_ID returned less than 2 bytes: ret=%02x", ret);
786                 ret = -EIO;
787                 goto out2;
788         }
789
790         /* Initialize MII structure */
791         dev->mii.dev = dev->net;
792         dev->mii.mdio_read = ax8817x_mdio_read;
793         dev->mii.mdio_write = ax8817x_mdio_write;
794         dev->mii.phy_id_mask = 0x3f;
795         dev->mii.reg_num_mask = 0x1f;
796         dev->mii.phy_id = *((u8 *)buf + 1);
797
798         dev->net->set_multicast_list = ax8817x_set_multicast;
799         dev->net->ethtool_ops = &ax8817x_ethtool_ops;
800
801         ax8817x_mdio_write(dev->net, dev->mii.phy_id, MII_BMCR, BMCR_RESET);
802         ax8817x_mdio_write(dev->net, dev->mii.phy_id, MII_ADVERTISE,
803                 ADVERTISE_ALL | ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP);
804         mii_nway_restart(&dev->mii);
805
806         if (dev->driver_info->flags & FLAG_FRAMING_AX) {
807                 /* REVISIT:  adjust hard_header_len too */
808                 dev->hard_mtu = 2048;
809         }
810
811         return 0;
812 out2:
813         kfree(buf);
814 out1:
815         return ret;
816 }
817
818 static struct ethtool_ops ax88772_ethtool_ops = {
819         .get_drvinfo            = ax8817x_get_drvinfo,
820         .get_link               = ethtool_op_get_link,
821         .get_msglevel           = usbnet_get_msglevel,
822         .set_msglevel           = usbnet_set_msglevel,
823         .get_wol                = ax8817x_get_wol,
824         .set_wol                = ax8817x_set_wol,
825         .get_eeprom_len         = ax8817x_get_eeprom_len,
826         .get_eeprom             = ax8817x_get_eeprom,
827         .get_settings           = ax8817x_get_settings,
828         .set_settings           = ax8817x_set_settings,
829 };
830
831 static int ax88772_bind(struct usbnet *dev, struct usb_interface *intf)
832 {
833         int ret;
834         void *buf;
835
836         get_endpoints(dev,intf);
837
838         buf = kmalloc(6, GFP_KERNEL);
839         if(!buf) {
840                 dbg ("Cannot allocate memory for buffer");
841                 ret = -ENOMEM;
842                 goto out1;
843         }
844
845         if ((ret = ax8817x_write_cmd(dev, AX_CMD_WRITE_GPIOS,
846                                      0x00B0, 0, 0, buf)) < 0)
847                 goto out2;
848
849         msleep(5);
850         if ((ret = ax8817x_write_cmd(dev, AX_CMD_SW_PHY_SELECT, 0x0001, 0, 0, buf)) < 0) {
851                 dbg("Select PHY #1 failed: %d", ret);
852                 goto out2;
853         }
854
855         if ((ret =
856              ax8817x_write_cmd(dev, AX_CMD_SW_RESET, AX_SWRESET_IPPD, 0, 0, buf)) < 0) {
857                 dbg("Failed to power down internal PHY: %d", ret);
858                 goto out2;
859         }
860
861         msleep(150);
862         if ((ret =
863              ax8817x_write_cmd(dev, AX_CMD_SW_RESET, AX_SWRESET_CLEAR, 0, 0, buf)) < 0) {
864                 dbg("Failed to perform software reset: %d", ret);
865                 goto out2;
866         }
867
868         msleep(150);
869         if ((ret =
870              ax8817x_write_cmd(dev, AX_CMD_SW_RESET, AX_SWRESET_IPRL | AX_SWRESET_PRL, 0, 0, buf)) < 0) {
871                 dbg("Failed to set Internal/External PHY reset control: %d", ret);
872                 goto out2;
873         }
874
875         msleep(150);
876         if ((ret =
877              ax8817x_write_cmd(dev, AX_CMD_WRITE_RX_CTL, 0x0000, 0, 0,
878                                buf)) < 0) {
879                 dbg("Failed to reset RX_CTL: %d", ret);
880                 goto out2;
881         }
882
883         /* Get the MAC address */
884         memset(buf, 0, ETH_ALEN);
885         if ((ret = ax8817x_read_cmd(dev, AX88772_CMD_READ_NODE_ID, 0, 0, ETH_ALEN, buf)) < 0) {
886                 dbg("Failed to read MAC address: %d", ret);
887                 goto out2;
888         }
889         memcpy(dev->net->dev_addr, buf, ETH_ALEN);
890
891         if ((ret = ax8817x_write_cmd(dev, AX_CMD_SET_SW_MII, 0, 0, 0, buf)) < 0) {
892                 dbg("Enabling software MII failed: %d", ret);
893                 goto out2;
894         }
895
896         if (((ret =
897               ax8817x_read_cmd(dev, AX_CMD_READ_MII_REG, 0x0010, 2, 2, buf)) < 0)
898             || (*((u16 *)buf) != 0x003b)) {
899                 dbg("Read PHY register 2 must be 0x3b00: %d", ret);
900                 goto out2;
901         }
902
903         /* Initialize MII structure */
904         dev->mii.dev = dev->net;
905         dev->mii.mdio_read = ax8817x_mdio_read;
906         dev->mii.mdio_write = ax8817x_mdio_write;
907         dev->mii.phy_id_mask = 0xff;
908         dev->mii.reg_num_mask = 0xff;
909
910         /* Get the PHY id */
911         if ((ret = ax8817x_read_cmd(dev, AX_CMD_READ_PHY_ID, 0, 0, 2, buf)) < 0) {
912                 dbg("Error reading PHY ID: %02x", ret);
913                 goto out2;
914         } else if (ret < 2) {
915                 /* this should always return 2 bytes */
916                 dbg("AX_CMD_READ_PHY_ID returned less than 2 bytes: ret=%02x",
917                     ret);
918                 ret = -EIO;
919                 goto out2;
920         }
921         dev->mii.phy_id = *((u8 *)buf + 1);
922
923         if ((ret =
924              ax8817x_write_cmd(dev, AX_CMD_SW_RESET, AX_SWRESET_PRL, 0, 0, buf)) < 0) {
925                 dbg("Set external PHY reset pin level: %d", ret);
926                 goto out2;
927         }
928         msleep(150);
929         if ((ret =
930              ax8817x_write_cmd(dev, AX_CMD_SW_RESET, AX_SWRESET_IPRL | AX_SWRESET_PRL, 0, 0, buf)) < 0) {
931                 dbg("Set Internal/External PHY reset control: %d", ret);
932                 goto out2;
933         }
934         msleep(150);
935
936
937         dev->net->set_multicast_list = ax8817x_set_multicast;
938         dev->net->ethtool_ops = &ax88772_ethtool_ops;
939
940         ax8817x_mdio_write(dev->net, dev->mii.phy_id, MII_BMCR, BMCR_RESET);
941         ax8817x_mdio_write(dev->net, dev->mii.phy_id, MII_ADVERTISE,
942                         ADVERTISE_ALL | ADVERTISE_CSMA);
943         mii_nway_restart(&dev->mii);
944
945         if ((ret = ax8817x_write_cmd(dev, AX_CMD_WRITE_MEDIUM_MODE, AX88772_MEDIUM_DEFAULT, 0, 0, buf)) < 0) {
946                 dbg("Write medium mode register: %d", ret);
947                 goto out2;
948         }
949
950         if ((ret = ax8817x_write_cmd(dev, AX_CMD_WRITE_IPG0, AX88772_IPG0_DEFAULT | AX88772_IPG1_DEFAULT,AX88772_IPG2_DEFAULT, 0, buf)) < 0) {
951                 dbg("Write IPG,IPG1,IPG2 failed: %d", ret);
952                 goto out2;
953         }
954         if ((ret =
955              ax8817x_write_cmd(dev, AX_CMD_SET_HW_MII, 0, 0, 0, &buf)) < 0) {
956                 dbg("Failed to set hardware MII: %02x", ret);
957                 goto out2;
958         }
959
960         /* Set RX_CTL to default values with 2k buffer, and enable cactus */
961         if ((ret =
962              ax8817x_write_cmd(dev, AX_CMD_WRITE_RX_CTL, 0x0088, 0, 0,
963                                buf)) < 0) {
964                 dbg("Reset RX_CTL failed: %d", ret);
965                 goto out2;
966         }
967
968         kfree(buf);
969
970         return 0;
971
972 out2:
973         kfree(buf);
974 out1:
975         return ret;
976 }
977
978 static int ax88772_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
979 {
980         u32 *header;
981         char *packet;
982         struct sk_buff *ax_skb;
983         u16 size;
984
985         header = (u32 *) skb->data;
986         le32_to_cpus(header);
987         packet = (char *)(header + 1);
988
989         skb_pull(skb, 4);
990
991         while (skb->len > 0) {
992                 if ((short)(*header & 0x0000ffff) !=
993                     ~((short)((*header & 0xffff0000) >> 16))) {
994                         devdbg(dev,"header length data is error");
995                 }
996                 /* get the packet length */
997                 size = (u16) (*header & 0x0000ffff);
998
999                 if ((skb->len) - ((size + 1) & 0xfffe) == 0)
1000                         return 2;
1001                 if (size > ETH_FRAME_LEN) {
1002                         devdbg(dev,"invalid rx length %d", size);
1003                         return 0;
1004                 }
1005                 ax_skb = skb_clone(skb, GFP_ATOMIC);
1006                 if (ax_skb) {
1007                         ax_skb->len = size;
1008                         ax_skb->data = packet;
1009                         ax_skb->tail = packet + size;
1010                         skb_return(dev, ax_skb);
1011                 } else {
1012                         return 0;
1013                 }
1014
1015                 skb_pull(skb, (size + 1) & 0xfffe);
1016
1017                 if (skb->len == 0)
1018                         break;
1019
1020                 header = (u32 *) skb->data;
1021                 le32_to_cpus(header);
1022                 packet = (char *)(header + 1);
1023                 skb_pull(skb, 4);
1024         }
1025
1026         if (skb->len < 0) {
1027                 devdbg(dev,"invalid rx length %d", skb->len);
1028                 return 0;
1029         }
1030         return 1;
1031 }
1032
1033 static struct sk_buff *ax88772_tx_fixup(struct usbnet *dev, struct sk_buff *skb,
1034                                         unsigned flags)
1035 {
1036         int padlen;
1037         int headroom = skb_headroom(skb);
1038         int tailroom = skb_tailroom(skb);
1039         u32 *packet_len;
1040         u32 *padbytes_ptr;
1041
1042         padlen = ((skb->len + 4) % 512) ? 0 : 4;
1043
1044         if ((!skb_cloned(skb))
1045             && ((headroom + tailroom) >= (4 + padlen))) {
1046                 if ((headroom < 4) || (tailroom < padlen)) {
1047                         skb->data = memmove(skb->head + 4, skb->data, skb->len);
1048                         skb->tail = skb->data + skb->len;
1049                 }
1050         } else {
1051                 struct sk_buff *skb2;
1052                 skb2 = skb_copy_expand(skb, 4, padlen, flags);
1053                 dev_kfree_skb_any(skb);
1054                 skb = skb2;
1055                 if (!skb)
1056                         return NULL;
1057         }
1058
1059         packet_len = (u32 *) skb_push(skb, 4);
1060
1061         packet_len = (u32 *) skb->data;
1062         *packet_len = (((skb->len - 4) ^ 0x0000ffff) << 16) + (skb->len - 4);
1063
1064         if ((skb->len % 512) == 0) {
1065                 padbytes_ptr = (u32 *) skb->tail;
1066                 *padbytes_ptr = 0xffff0000;
1067                 skb_put(skb, padlen);
1068         }
1069         return skb;
1070 }
1071
1072 static int ax88772_link_reset(struct usbnet *dev)
1073 {
1074         u16 lpa;
1075         u16 mode;
1076
1077         mode = AX88772_MEDIUM_DEFAULT;
1078         lpa = ax8817x_mdio_read(dev->net, dev->mii.phy_id, MII_LPA);
1079
1080         if ((lpa & LPA_DUPLEX) == 0)
1081                 mode &= ~AX88772_MEDIUM_FULL_DUPLEX;
1082         if ((lpa & LPA_100) == 0)
1083                 mode &= ~AX88772_MEDIUM_100MB;
1084         ax8817x_write_cmd(dev, AX_CMD_WRITE_MEDIUM_MODE, mode, 0, 0, NULL);
1085
1086         return 0;
1087 }
1088
1089 static const struct driver_info ax8817x_info = {
1090         .description = "ASIX AX8817x USB 2.0 Ethernet",
1091         .bind = ax8817x_bind,
1092         .status = ax8817x_status,
1093         .link_reset = ax88172_link_reset,
1094         .reset = ax88172_link_reset,
1095         .flags =  FLAG_ETHER,
1096         .data = 0x00130103,
1097 };
1098
1099 static const struct driver_info dlink_dub_e100_info = {
1100         .description = "DLink DUB-E100 USB Ethernet",
1101         .bind = ax8817x_bind,
1102         .status = ax8817x_status,
1103         .link_reset = ax88172_link_reset,
1104         .reset = ax88172_link_reset,
1105         .flags =  FLAG_ETHER,
1106         .data = 0x009f9d9f,
1107 };
1108
1109 static const struct driver_info netgear_fa120_info = {
1110         .description = "Netgear FA-120 USB Ethernet",
1111         .bind = ax8817x_bind,
1112         .status = ax8817x_status,
1113         .link_reset = ax88172_link_reset,
1114         .reset = ax88172_link_reset,
1115         .flags =  FLAG_ETHER,
1116         .data = 0x00130103,
1117 };
1118
1119 static const struct driver_info hawking_uf200_info = {
1120         .description = "Hawking UF200 USB Ethernet",
1121         .bind = ax8817x_bind,
1122         .status = ax8817x_status,
1123         .link_reset = ax88172_link_reset,
1124         .reset = ax88172_link_reset,
1125         .flags =  FLAG_ETHER,
1126         .data = 0x001f1d1f,
1127 };
1128
1129 static const struct driver_info ax88772_info = {
1130         .description = "ASIX AX88772 USB 2.0 Ethernet",
1131         .bind = ax88772_bind,
1132         .status = ax8817x_status,
1133         .link_reset = ax88772_link_reset,
1134         .reset = ax88772_link_reset,
1135         .flags = FLAG_ETHER | FLAG_FRAMING_AX,
1136         .rx_fixup = ax88772_rx_fixup,
1137         .tx_fixup = ax88772_tx_fixup,
1138         .data = 0x00130103,
1139 };
1140
1141 #endif /* CONFIG_USB_AX8817X */
1142
1143
1144 \f
1145 #ifdef  CONFIG_USB_BELKIN
1146 #define HAVE_HARDWARE
1147
1148 /*-------------------------------------------------------------------------
1149  *
1150  * Belkin F5U104 ... two NetChip 2280 devices + Atmel microcontroller
1151  *
1152  * ... also two eTEK designs, including one sold as "Advance USBNET"
1153  *
1154  *-------------------------------------------------------------------------*/
1155
1156 static const struct driver_info belkin_info = {
1157         .description =  "Belkin, eTEK, or compatible",
1158 };
1159
1160 #endif  /* CONFIG_USB_BELKIN */
1161
1162
1163 \f
1164 /*-------------------------------------------------------------------------
1165  *
1166  * Communications Device Class declarations.
1167  * Used by CDC Ethernet, and some CDC variants
1168  *
1169  *-------------------------------------------------------------------------*/
1170
1171 #ifdef  CONFIG_USB_CDCETHER
1172 #define NEED_GENERIC_CDC
1173 #endif
1174
1175 #ifdef  CONFIG_USB_ZAURUS
1176 /* Ethernet variant uses funky framing, broken ethernet addressing */
1177 #define NEED_GENERIC_CDC
1178 #endif
1179
1180 #ifdef  CONFIG_USB_RNDIS
1181 /* ACM variant uses even funkier framing, complex control RPC scheme */
1182 #define NEED_GENERIC_CDC
1183 #endif
1184
1185
1186 #ifdef  NEED_GENERIC_CDC
1187
1188 #include <linux/usb_cdc.h>
1189
1190 struct cdc_state {
1191         struct usb_cdc_header_desc      *header;
1192         struct usb_cdc_union_desc       *u;
1193         struct usb_cdc_ether_desc       *ether;
1194         struct usb_interface            *control;
1195         struct usb_interface            *data;
1196 };
1197
1198 static struct usb_driver usbnet_driver;
1199
1200 /*
1201  * probes control interface, claims data interface, collects the bulk
1202  * endpoints, activates data interface (if needed), maybe sets MTU.
1203  * all pure cdc, except for certain firmware workarounds.
1204  */
1205 static int generic_cdc_bind (struct usbnet *dev, struct usb_interface *intf)
1206 {
1207         u8                              *buf = intf->cur_altsetting->extra;
1208         int                             len = intf->cur_altsetting->extralen;
1209         struct usb_interface_descriptor *d;
1210         struct cdc_state                *info = (void *) &dev->data;
1211         int                             status;
1212         int                             rndis;
1213
1214         if (sizeof dev->data < sizeof *info)
1215                 return -EDOM;
1216
1217         /* expect strict spec conformance for the descriptors, but
1218          * cope with firmware which stores them in the wrong place
1219          */
1220         if (len == 0 && dev->udev->actconfig->extralen) {
1221                 /* Motorola SB4100 (and others: Brad Hards says it's
1222                  * from a Broadcom design) put CDC descriptors here
1223                  */
1224                 buf = dev->udev->actconfig->extra;
1225                 len = dev->udev->actconfig->extralen;
1226                 if (len)
1227                         dev_dbg (&intf->dev,
1228                                 "CDC descriptors on config\n");
1229         }
1230
1231         /* this assumes that if there's a non-RNDIS vendor variant
1232          * of cdc-acm, it'll fail RNDIS requests cleanly.
1233          */
1234         rndis = (intf->cur_altsetting->desc.bInterfaceProtocol == 0xff);
1235
1236         memset (info, 0, sizeof *info);
1237         info->control = intf;
1238         while (len > 3) {
1239                 if (buf [1] != USB_DT_CS_INTERFACE)
1240                         goto next_desc;
1241
1242                 /* use bDescriptorSubType to identify the CDC descriptors.
1243                  * We expect devices with CDC header and union descriptors.
1244                  * For CDC Ethernet we need the ethernet descriptor.
1245                  * For RNDIS, ignore two (pointless) CDC modem descriptors
1246                  * in favor of a complicated OID-based RPC scheme doing what
1247                  * CDC Ethernet achieves with a simple descriptor.
1248                  */
1249                 switch (buf [2]) {
1250                 case USB_CDC_HEADER_TYPE:
1251                         if (info->header) {
1252                                 dev_dbg (&intf->dev, "extra CDC header\n");
1253                                 goto bad_desc;
1254                         }
1255                         info->header = (void *) buf;
1256                         if (info->header->bLength != sizeof *info->header) {
1257                                 dev_dbg (&intf->dev, "CDC header len %u\n",
1258                                         info->header->bLength);
1259                                 goto bad_desc;
1260                         }
1261                         break;
1262                 case USB_CDC_UNION_TYPE:
1263                         if (info->u) {
1264                                 dev_dbg (&intf->dev, "extra CDC union\n");
1265                                 goto bad_desc;
1266                         }
1267                         info->u = (void *) buf;
1268                         if (info->u->bLength != sizeof *info->u) {
1269                                 dev_dbg (&intf->dev, "CDC union len %u\n",
1270                                         info->u->bLength);
1271                                 goto bad_desc;
1272                         }
1273
1274                         /* we need a master/control interface (what we're
1275                          * probed with) and a slave/data interface; union
1276                          * descriptors sort this all out.
1277                          */
1278                         info->control = usb_ifnum_to_if(dev->udev,
1279                                                 info->u->bMasterInterface0);
1280                         info->data = usb_ifnum_to_if(dev->udev,
1281                                                 info->u->bSlaveInterface0);
1282                         if (!info->control || !info->data) {
1283                                 dev_dbg (&intf->dev,
1284                                         "master #%u/%p slave #%u/%p\n",
1285                                         info->u->bMasterInterface0,
1286                                         info->control,
1287                                         info->u->bSlaveInterface0,
1288                                         info->data);
1289                                 goto bad_desc;
1290                         }
1291                         if (info->control != intf) {
1292                                 dev_dbg (&intf->dev, "bogus CDC Union\n");
1293                                 /* Ambit USB Cable Modem (and maybe others)
1294                                  * interchanges master and slave interface.
1295                                  */
1296                                 if (info->data == intf) {
1297                                         info->data = info->control;
1298                                         info->control = intf;
1299                                 } else
1300                                         goto bad_desc;
1301                         }
1302
1303                         /* a data interface altsetting does the real i/o */
1304                         d = &info->data->cur_altsetting->desc;
1305                         if (d->bInterfaceClass != USB_CLASS_CDC_DATA) {
1306                                 dev_dbg (&intf->dev, "slave class %u\n",
1307                                         d->bInterfaceClass);
1308                                 goto bad_desc;
1309                         }
1310                         break;
1311                 case USB_CDC_ETHERNET_TYPE:
1312                         if (info->ether) {
1313                                 dev_dbg (&intf->dev, "extra CDC ether\n");
1314                                 goto bad_desc;
1315                         }
1316                         info->ether = (void *) buf;
1317                         if (info->ether->bLength != sizeof *info->ether) {
1318                                 dev_dbg (&intf->dev, "CDC ether len %u\n",
1319                                         info->ether->bLength);
1320                                 goto bad_desc;
1321                         }
1322                         dev->hard_mtu = le16_to_cpu(
1323                                                 info->ether->wMaxSegmentSize);
1324                         /* because of Zaurus, we may be ignoring the host
1325                          * side link address we were given.
1326                          */
1327                         break;
1328                 }
1329 next_desc:
1330                 len -= buf [0]; /* bLength */
1331                 buf += buf [0];
1332         }
1333
1334         if (!info->header || !info->u || (!rndis && !info->ether)) {
1335                 dev_dbg (&intf->dev, "missing cdc %s%s%sdescriptor\n",
1336                         info->header ? "" : "header ",
1337                         info->u ? "" : "union ",
1338                         info->ether ? "" : "ether ");
1339                 goto bad_desc;
1340         }
1341
1342         /* claim data interface and set it up ... with side effects.
1343          * network traffic can't flow until an altsetting is enabled.
1344          */
1345         status = usb_driver_claim_interface (&usbnet_driver, info->data, dev);
1346         if (status < 0)
1347                 return status;
1348         status = get_endpoints (dev, info->data);
1349         if (status < 0) {
1350                 /* ensure immediate exit from usbnet_disconnect */
1351                 usb_set_intfdata(info->data, NULL);
1352                 usb_driver_release_interface (&usbnet_driver, info->data);
1353                 return status;
1354         }
1355
1356         /* status endpoint: optional for CDC Ethernet, not RNDIS (or ACM) */
1357         dev->status = NULL;
1358         if (info->control->cur_altsetting->desc.bNumEndpoints == 1) {
1359                 struct usb_endpoint_descriptor  *desc;
1360
1361                 dev->status = &info->control->cur_altsetting->endpoint [0];
1362                 desc = &dev->status->desc;
1363                 if (desc->bmAttributes != USB_ENDPOINT_XFER_INT
1364                                 || !(desc->bEndpointAddress & USB_DIR_IN)
1365                                 || (le16_to_cpu(desc->wMaxPacketSize)
1366                                         < sizeof (struct usb_cdc_notification))
1367                                 || !desc->bInterval) {
1368                         dev_dbg (&intf->dev, "bad notification endpoint\n");
1369                         dev->status = NULL;
1370                 }
1371         }
1372         if (rndis && !dev->status) {
1373                 dev_dbg (&intf->dev, "missing RNDIS status endpoint\n");
1374                 usb_set_intfdata(info->data, NULL);
1375                 usb_driver_release_interface (&usbnet_driver, info->data);
1376                 return -ENODEV;
1377         }
1378         return 0;
1379
1380 bad_desc:
1381         dev_info (&dev->udev->dev, "bad CDC descriptors\n");
1382         return -ENODEV;
1383 }
1384
1385 static void cdc_unbind (struct usbnet *dev, struct usb_interface *intf)
1386 {
1387         struct cdc_state                *info = (void *) &dev->data;
1388
1389         /* disconnect master --> disconnect slave */
1390         if (intf == info->control && info->data) {
1391                 /* ensure immediate exit from usbnet_disconnect */
1392                 usb_set_intfdata(info->data, NULL);
1393                 usb_driver_release_interface (&usbnet_driver, info->data);
1394                 info->data = NULL;
1395         }
1396
1397         /* and vice versa (just in case) */
1398         else if (intf == info->data && info->control) {
1399                 /* ensure immediate exit from usbnet_disconnect */
1400                 usb_set_intfdata(info->control, NULL);
1401                 usb_driver_release_interface (&usbnet_driver, info->control);
1402                 info->control = NULL;
1403         }
1404 }
1405
1406 #endif  /* NEED_GENERIC_CDC */
1407
1408 \f
1409 #ifdef  CONFIG_USB_CDCETHER
1410 #define HAVE_HARDWARE
1411
1412 /*-------------------------------------------------------------------------
1413  *
1414  * Communications Device Class, Ethernet Control model
1415  *
1416  * Takes two interfaces.  The DATA interface is inactive till an altsetting
1417  * is selected.  Configuration data includes class descriptors.
1418  *
1419  * This should interop with whatever the 2.4 "CDCEther.c" driver
1420  * (by Brad Hards) talked with.
1421  *
1422  *-------------------------------------------------------------------------*/
1423
1424 #include <linux/ctype.h>
1425
1426
1427 static void dumpspeed (struct usbnet *dev, __le32 *speeds)
1428 {
1429         if (netif_msg_timer (dev))
1430                 devinfo (dev, "link speeds: %u kbps up, %u kbps down",
1431                         __le32_to_cpu(speeds[0]) / 1000,
1432                 __le32_to_cpu(speeds[1]) / 1000);
1433 }
1434
1435 static void cdc_status (struct usbnet *dev, struct urb *urb)
1436 {
1437         struct usb_cdc_notification     *event;
1438
1439         if (urb->actual_length < sizeof *event)
1440                 return;
1441         
1442         /* SPEED_CHANGE can get split into two 8-byte packets */
1443         if (test_and_clear_bit (EVENT_STS_SPLIT, &dev->flags)) {
1444                 dumpspeed (dev, (__le32 *) urb->transfer_buffer);
1445                 return;
1446         }
1447
1448         event = urb->transfer_buffer;
1449         switch (event->bNotificationType) {
1450         case USB_CDC_NOTIFY_NETWORK_CONNECTION:
1451                 if (netif_msg_timer (dev))
1452                         devdbg (dev, "CDC: carrier %s",
1453                                         event->wValue ? "on" : "off");
1454                 if (event->wValue)
1455                         netif_carrier_on(dev->net);
1456                 else
1457                         netif_carrier_off(dev->net);
1458                 break;
1459         case USB_CDC_NOTIFY_SPEED_CHANGE:       /* tx/rx rates */
1460                 if (netif_msg_timer (dev))
1461                         devdbg (dev, "CDC: speed change (len %d)",
1462                                         urb->actual_length);
1463                 if (urb->actual_length != (sizeof *event + 8))
1464                         set_bit (EVENT_STS_SPLIT, &dev->flags);
1465                 else
1466                         dumpspeed (dev, (__le32 *) &event[1]);
1467                 break;
1468         // case USB_CDC_NOTIFY_RESPONSE_AVAILABLE:      /* RNDIS; or unsolicited */
1469         default:
1470                 deverr (dev, "CDC: unexpected notification %02x!",
1471                                  event->bNotificationType);
1472                 break;
1473         }
1474 }
1475
1476 static u8 nibble (unsigned char c)
1477 {
1478         if (likely (isdigit (c)))
1479                 return c - '0';
1480         c = toupper (c);
1481         if (likely (isxdigit (c)))
1482                 return 10 + c - 'A';
1483         return 0;
1484 }
1485
1486 static inline int
1487 get_ethernet_addr (struct usbnet *dev, struct usb_cdc_ether_desc *e)
1488 {
1489         int             tmp, i;
1490         unsigned char   buf [13];
1491
1492         tmp = usb_string (dev->udev, e->iMACAddress, buf, sizeof buf);
1493         if (tmp != 12) {
1494                 dev_dbg (&dev->udev->dev,
1495                         "bad MAC string %d fetch, %d\n", e->iMACAddress, tmp);
1496                 if (tmp >= 0)
1497                         tmp = -EINVAL;
1498                 return tmp;
1499         }
1500         for (i = tmp = 0; i < 6; i++, tmp += 2)
1501                 dev->net->dev_addr [i] =
1502                          (nibble (buf [tmp]) << 4) + nibble (buf [tmp + 1]);
1503         return 0;
1504 }
1505
1506 static int cdc_bind (struct usbnet *dev, struct usb_interface *intf)
1507 {
1508         int                             status;
1509         struct cdc_state                *info = (void *) &dev->data;
1510
1511         status = generic_cdc_bind (dev, intf);
1512         if (status < 0)
1513                 return status;
1514
1515         status = get_ethernet_addr (dev, info->ether);
1516         if (status < 0) {
1517                 usb_set_intfdata(info->data, NULL);
1518                 usb_driver_release_interface (&usbnet_driver, info->data);
1519                 return status;
1520         }
1521
1522         /* FIXME cdc-ether has some multicast code too, though it complains
1523          * in routine cases.  info->ether describes the multicast support.
1524          */
1525         return 0;
1526 }
1527
1528 static const struct driver_info cdc_info = {
1529         .description =  "CDC Ethernet Device",
1530         .flags =        FLAG_ETHER,
1531         // .check_connect = cdc_check_connect,
1532         .bind =         cdc_bind,
1533         .unbind =       cdc_unbind,
1534         .status =       cdc_status,
1535 };
1536
1537 #endif  /* CONFIG_USB_CDCETHER */
1538
1539
1540 \f
1541 #ifdef  CONFIG_USB_EPSON2888
1542 #define HAVE_HARDWARE
1543
1544 /*-------------------------------------------------------------------------
1545  *
1546  * EPSON USB clients
1547  *
1548  * This is the same idea as Linux PDAs (below) except the firmware in the
1549  * device might not be Tux-powered.  Epson provides reference firmware that
1550  * implements this interface.  Product developers can reuse or modify that
1551  * code, such as by using their own product and vendor codes.
1552  *
1553  * Support was from Juro Bystricky <bystricky.juro@erd.epson.com>
1554  *
1555  *-------------------------------------------------------------------------*/
1556
1557 static const struct driver_info epson2888_info = {
1558         .description =  "Epson USB Device",
1559         .check_connect = always_connected,
1560
1561         .in = 4, .out = 3,
1562 };
1563
1564 #endif  /* CONFIG_USB_EPSON2888 */
1565
1566 \f
1567 #ifdef CONFIG_USB_GENESYS
1568 #define HAVE_HARDWARE
1569
1570 /*-------------------------------------------------------------------------
1571  *
1572  * GeneSys GL620USB-A (www.genesyslogic.com.tw)
1573  *
1574  * ... should partially interop with the Win32 driver for this hardware
1575  * The GeneSys docs imply there's some NDIS issue motivating this framing.
1576  *
1577  * Some info from GeneSys:
1578  *  - GL620USB-A is full duplex; GL620USB is only half duplex for bulk.
1579  *    (Some cables, like the BAFO-100c, use the half duplex version.)
1580  *  - For the full duplex model, the low bit of the version code says
1581  *    which side is which ("left/right").
1582  *  - For the half duplex type, a control/interrupt handshake settles
1583  *    the transfer direction.  (That's disabled here, partially coded.)
1584  *    A control URB would block until other side writes an interrupt.
1585  *
1586  * Original code from Jiun-Jie Huang <huangjj@genesyslogic.com.tw>
1587  * and merged into "usbnet" by Stanislav Brabec <utx@penguin.cz>.
1588  *
1589  *-------------------------------------------------------------------------*/
1590
1591 // control msg write command
1592 #define GENELINK_CONNECT_WRITE                  0xF0
1593 // interrupt pipe index
1594 #define GENELINK_INTERRUPT_PIPE                 0x03
1595 // interrupt read buffer size
1596 #define INTERRUPT_BUFSIZE                       0x08
1597 // interrupt pipe interval value
1598 #define GENELINK_INTERRUPT_INTERVAL             0x10
1599 // max transmit packet number per transmit
1600 #define GL_MAX_TRANSMIT_PACKETS                 32
1601 // max packet length
1602 #define GL_MAX_PACKET_LEN                       1514
1603 // max receive buffer size 
1604 #define GL_RCV_BUF_SIZE         \
1605         (((GL_MAX_PACKET_LEN + 4) * GL_MAX_TRANSMIT_PACKETS) + 4)
1606
1607 struct gl_packet {
1608         u32             packet_length;
1609         char            packet_data [1];
1610 };
1611
1612 struct gl_header {
1613         u32                     packet_count;
1614         struct gl_packet        packets;
1615 };
1616
1617 #ifdef  GENELINK_ACK
1618
1619 // FIXME:  this code is incomplete, not debugged; it doesn't
1620 // handle interrupts correctly.  interrupts should be generic
1621 // code like all other device I/O, anyway.
1622
1623 struct gl_priv { 
1624         struct urb      *irq_urb;
1625         char            irq_buf [INTERRUPT_BUFSIZE];
1626 };
1627
1628 static inline int gl_control_write (struct usbnet *dev, u8 request, u16 value)
1629 {
1630         int retval;
1631
1632         retval = usb_control_msg (dev->udev,
1633                       usb_sndctrlpipe (dev->udev, 0),
1634                       request,
1635                       USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
1636                       value, 
1637                       0,                        // index
1638                       0,                        // data buffer
1639                       0,                        // size
1640                       CONTROL_TIMEOUT_MS);
1641         return retval;
1642 }
1643
1644 static void gl_interrupt_complete (struct urb *urb, struct pt_regs *regs)
1645 {
1646         int status = urb->status;
1647         
1648         switch (status) {
1649         case 0:
1650                 /* success */
1651                 break;
1652         case -ECONNRESET:
1653         case -ENOENT:
1654         case -ESHUTDOWN:
1655                 /* this urb is terminated, clean up */
1656                 dbg("%s - urb shutting down with status: %d",
1657                                 __FUNCTION__, status);
1658                 return;
1659         default:
1660                 dbg("%s - nonzero urb status received: %d",
1661                                 __FUNCTION__, urb->status);
1662         }
1663
1664         status = usb_submit_urb (urb, GFP_ATOMIC);
1665         if (status)
1666                 err ("%s - usb_submit_urb failed with result %d",
1667                      __FUNCTION__, status);
1668 }
1669
1670 static int gl_interrupt_read (struct usbnet *dev)
1671 {
1672         struct gl_priv  *priv = dev->priv_data;
1673         int             retval;
1674
1675         // issue usb interrupt read
1676         if (priv && priv->irq_urb) {
1677                 // submit urb
1678                 if ((retval = usb_submit_urb (priv->irq_urb, GFP_KERNEL)) != 0)
1679                         dbg ("gl_interrupt_read: submit fail - %X...", retval);
1680                 else
1681                         dbg ("gl_interrupt_read: submit success...");
1682         }
1683
1684         return 0;
1685 }
1686
1687 // check whether another side is connected
1688 static int genelink_check_connect (struct usbnet *dev)
1689 {
1690         int                     retval;
1691
1692         dbg ("genelink_check_connect...");
1693
1694         // detect whether another side is connected
1695         if ((retval = gl_control_write (dev, GENELINK_CONNECT_WRITE, 0)) != 0) {
1696                 dbg ("%s: genelink_check_connect write fail - %X",
1697                         dev->net->name, retval);
1698                 return retval;
1699         }
1700
1701         // usb interrupt read to ack another side 
1702         if ((retval = gl_interrupt_read (dev)) != 0) {
1703                 dbg ("%s: genelink_check_connect read fail - %X",
1704                         dev->net->name, retval);
1705                 return retval;
1706         }
1707
1708         dbg ("%s: genelink_check_connect read success", dev->net->name);
1709         return 0;
1710 }
1711
1712 // allocate and initialize the private data for genelink
1713 static int genelink_init (struct usbnet *dev)
1714 {
1715         struct gl_priv *priv;
1716
1717         // allocate the private data structure
1718         if ((priv = kmalloc (sizeof *priv, GFP_KERNEL)) == 0) {
1719                 dbg ("%s: cannot allocate private data per device",
1720                         dev->net->name);
1721                 return -ENOMEM;
1722         }
1723
1724         // allocate irq urb
1725         if ((priv->irq_urb = usb_alloc_urb (0, GFP_KERNEL)) == 0) {
1726                 dbg ("%s: cannot allocate private irq urb per device",
1727                         dev->net->name);
1728                 kfree (priv);
1729                 return -ENOMEM;
1730         }
1731
1732         // fill irq urb
1733         usb_fill_int_urb (priv->irq_urb, dev->udev,
1734                 usb_rcvintpipe (dev->udev, GENELINK_INTERRUPT_PIPE),
1735                 priv->irq_buf, INTERRUPT_BUFSIZE,
1736                 gl_interrupt_complete, 0,
1737                 GENELINK_INTERRUPT_INTERVAL);
1738
1739         // set private data pointer
1740         dev->priv_data = priv;
1741
1742         return 0;
1743 }
1744
1745 // release the private data
1746 static int genelink_free (struct usbnet *dev)
1747 {
1748         struct gl_priv  *priv = dev->priv_data;
1749
1750         if (!priv) 
1751                 return 0;
1752
1753 // FIXME:  can't cancel here; it's synchronous, and
1754 // should have happened earlier in any case (interrupt
1755 // handling needs to be generic)
1756
1757         // cancel irq urb first
1758         usb_kill_urb (priv->irq_urb);
1759
1760         // free irq urb
1761         usb_free_urb (priv->irq_urb);
1762
1763         // free the private data structure
1764         kfree (priv);
1765
1766         return 0;
1767 }
1768
1769 #endif
1770
1771 static int genelink_rx_fixup (struct usbnet *dev, struct sk_buff *skb)
1772 {
1773         struct gl_header        *header;
1774         struct gl_packet        *packet;
1775         struct sk_buff          *gl_skb;
1776         u32                     size;
1777
1778         header = (struct gl_header *) skb->data;
1779
1780         // get the packet count of the received skb
1781         le32_to_cpus (&header->packet_count);
1782         if ((header->packet_count > GL_MAX_TRANSMIT_PACKETS)
1783                         || (header->packet_count < 0)) {
1784                 dbg ("genelink: invalid received packet count %d",
1785                         header->packet_count);
1786                 return 0;
1787         }
1788
1789         // set the current packet pointer to the first packet
1790         packet = &header->packets;
1791
1792         // decrement the length for the packet count size 4 bytes
1793         skb_pull (skb, 4);
1794
1795         while (header->packet_count > 1) {
1796                 // get the packet length
1797                 size = packet->packet_length;
1798
1799                 // this may be a broken packet
1800                 if (size > GL_MAX_PACKET_LEN) {
1801                         dbg ("genelink: invalid rx length %d", size);
1802                         return 0;
1803                 }
1804
1805                 // allocate the skb for the individual packet
1806                 gl_skb = alloc_skb (size, GFP_ATOMIC);
1807                 if (gl_skb) {
1808
1809                         // copy the packet data to the new skb
1810                         memcpy(skb_put(gl_skb, size), packet->packet_data, size);
1811                         skb_return (dev, gl_skb);
1812                 }
1813
1814                 // advance to the next packet
1815                 packet = (struct gl_packet *)
1816                         &packet->packet_data [size];
1817                 header->packet_count--;
1818
1819                 // shift the data pointer to the next gl_packet
1820                 skb_pull (skb, size + 4);
1821         }
1822
1823         // skip the packet length field 4 bytes
1824         skb_pull (skb, 4);
1825
1826         if (skb->len > GL_MAX_PACKET_LEN) {
1827                 dbg ("genelink: invalid rx length %d", skb->len);
1828                 return 0;
1829         }
1830         return 1;
1831 }
1832
1833 static struct sk_buff *
1834 genelink_tx_fixup (struct usbnet *dev, struct sk_buff *skb, unsigned flags)
1835 {
1836         int     padlen;
1837         int     length = skb->len;
1838         int     headroom = skb_headroom (skb);
1839         int     tailroom = skb_tailroom (skb);
1840         u32     *packet_count;
1841         u32     *packet_len;
1842
1843         // FIXME:  magic numbers, bleech
1844         padlen = ((skb->len + (4 + 4*1)) % 64) ? 0 : 1;
1845
1846         if ((!skb_cloned (skb))
1847                         && ((headroom + tailroom) >= (padlen + (4 + 4*1)))) {
1848                 if ((headroom < (4 + 4*1)) || (tailroom < padlen)) {
1849                         skb->data = memmove (skb->head + (4 + 4*1),
1850                                              skb->data, skb->len);
1851                         skb->tail = skb->data + skb->len;
1852                 }
1853         } else {
1854                 struct sk_buff  *skb2;
1855                 skb2 = skb_copy_expand (skb, (4 + 4*1) , padlen, flags);
1856                 dev_kfree_skb_any (skb);
1857                 skb = skb2;
1858                 if (!skb)
1859                         return NULL;
1860         }
1861
1862         // attach the packet count to the header
1863         packet_count = (u32 *) skb_push (skb, (4 + 4*1));
1864         packet_len = packet_count + 1;
1865
1866         // FIXME little endian?
1867         *packet_count = 1;
1868         *packet_len = length;
1869
1870         // add padding byte
1871         if ((skb->len % dev->maxpacket) == 0)
1872                 skb_put (skb, 1);
1873
1874         return skb;
1875 }
1876
1877 static int genelink_bind (struct usbnet *dev, struct usb_interface *intf)
1878 {
1879         dev->hard_mtu = GL_RCV_BUF_SIZE;
1880         dev->net->hard_header_len += 4;
1881         return 0;
1882 }
1883
1884 static const struct driver_info genelink_info = {
1885         .description =  "Genesys GeneLink",
1886         .flags =        FLAG_FRAMING_GL | FLAG_NO_SETINT,
1887         .bind =         genelink_bind,
1888         .rx_fixup =     genelink_rx_fixup,
1889         .tx_fixup =     genelink_tx_fixup,
1890
1891         .in = 1, .out = 2,
1892
1893 #ifdef  GENELINK_ACK
1894         .check_connect =genelink_check_connect,
1895 #endif
1896 };
1897
1898 #endif /* CONFIG_USB_GENESYS */
1899
1900
1901 \f
1902 #ifdef  CONFIG_USB_NET1080
1903 #define HAVE_HARDWARE
1904
1905 /*-------------------------------------------------------------------------
1906  *
1907  * Netchip 1080 driver ... http://www.netchip.com
1908  * Used in LapLink cables
1909  *
1910  *-------------------------------------------------------------------------*/
1911
1912 #define frame_errors    data[1]
1913
1914 /*
1915  * NetChip framing of ethernet packets, supporting additional error
1916  * checks for links that may drop bulk packets from inside messages.
1917  * Odd USB length == always short read for last usb packet.
1918  *      - nc_header
1919  *      - Ethernet header (14 bytes)
1920  *      - payload
1921  *      - (optional padding byte, if needed so length becomes odd)
1922  *      - nc_trailer
1923  *
1924  * This framing is to be avoided for non-NetChip devices.
1925  */
1926
1927 struct nc_header {              // packed:
1928         __le16  hdr_len;                // sizeof nc_header (LE, all)
1929         __le16  packet_len;             // payload size (including ethhdr)
1930         __le16  packet_id;              // detects dropped packets
1931 #define MIN_HEADER      6
1932
1933         // all else is optional, and must start with:
1934         // u16  vendorId;               // from usb-if
1935         // u16  productId;
1936 } __attribute__((__packed__));
1937
1938 #define PAD_BYTE        ((unsigned char)0xAC)
1939
1940 struct nc_trailer {
1941         __le16  packet_id;
1942 } __attribute__((__packed__));
1943
1944 // packets may use FLAG_FRAMING_NC and optional pad
1945 #define FRAMED_SIZE(mtu) (sizeof (struct nc_header) \
1946                                 + sizeof (struct ethhdr) \
1947                                 + (mtu) \
1948                                 + 1 \
1949                                 + sizeof (struct nc_trailer))
1950
1951 #define MIN_FRAMED      FRAMED_SIZE(0)
1952
1953 /* packets _could_ be up to 64KB... */
1954 #define NC_MAX_PACKET   32767
1955
1956
1957 /*
1958  * Zero means no timeout; else, how long a 64 byte bulk packet may be queued
1959  * before the hardware drops it.  If that's done, the driver will need to
1960  * frame network packets to guard against the dropped USB packets.  The win32
1961  * driver sets this for both sides of the link.
1962  */
1963 #define NC_READ_TTL_MS  ((u8)255)       // ms
1964
1965 /*
1966  * We ignore most registers and EEPROM contents.
1967  */
1968 #define REG_USBCTL      ((u8)0x04)
1969 #define REG_TTL         ((u8)0x10)
1970 #define REG_STATUS      ((u8)0x11)
1971
1972 /*
1973  * Vendor specific requests to read/write data
1974  */
1975 #define REQUEST_REGISTER        ((u8)0x10)
1976 #define REQUEST_EEPROM          ((u8)0x11)
1977
1978 static int
1979 nc_vendor_read (struct usbnet *dev, u8 req, u8 regnum, u16 *retval_ptr)
1980 {
1981         int status = usb_control_msg (dev->udev,
1982                 usb_rcvctrlpipe (dev->udev, 0),
1983                 req,
1984                 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
1985                 0, regnum,
1986                 retval_ptr, sizeof *retval_ptr,
1987                 CONTROL_TIMEOUT_MS);
1988         if (status > 0)
1989                 status = 0;
1990         if (!status)
1991                 le16_to_cpus (retval_ptr);
1992         return status;
1993 }
1994
1995 static inline int
1996 nc_register_read (struct usbnet *dev, u8 regnum, u16 *retval_ptr)
1997 {
1998         return nc_vendor_read (dev, REQUEST_REGISTER, regnum, retval_ptr);
1999 }
2000
2001 // no retval ... can become async, usable in_interrupt()
2002 static void
2003 nc_vendor_write (struct usbnet *dev, u8 req, u8 regnum, u16 value)
2004 {
2005         usb_control_msg (dev->udev,
2006                 usb_sndctrlpipe (dev->udev, 0),
2007                 req,
2008                 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
2009                 value, regnum,
2010                 NULL, 0,                        // data is in setup packet
2011                 CONTROL_TIMEOUT_MS);
2012 }
2013
2014 static inline void
2015 nc_register_write (struct usbnet *dev, u8 regnum, u16 value)
2016 {
2017         nc_vendor_write (dev, REQUEST_REGISTER, regnum, value);
2018 }
2019
2020
2021 #if 0
2022 static void nc_dump_registers (struct usbnet *dev)
2023 {
2024         u8      reg;
2025         u16     *vp = kmalloc (sizeof (u16));
2026
2027         if (!vp) {
2028                 dbg ("no memory?");
2029                 return;
2030         }
2031
2032         dbg ("%s registers:", dev->net->name);
2033         for (reg = 0; reg < 0x20; reg++) {
2034                 int retval;
2035
2036                 // reading some registers is trouble
2037                 if (reg >= 0x08 && reg <= 0xf)
2038                         continue;
2039                 if (reg >= 0x12 && reg <= 0x1e)
2040                         continue;
2041
2042                 retval = nc_register_read (dev, reg, vp);
2043                 if (retval < 0)
2044                         dbg ("%s reg [0x%x] ==> error %d",
2045                                 dev->net->name, reg, retval);
2046                 else
2047                         dbg ("%s reg [0x%x] = 0x%x",
2048                                 dev->net->name, reg, *vp);
2049         }
2050         kfree (vp);
2051 }
2052 #endif
2053
2054
2055 /*-------------------------------------------------------------------------*/
2056
2057 /*
2058  * Control register
2059  */
2060
2061 #define USBCTL_WRITABLE_MASK    0x1f0f
2062 // bits 15-13 reserved, r/o
2063 #define USBCTL_ENABLE_LANG      (1 << 12)
2064 #define USBCTL_ENABLE_MFGR      (1 << 11)
2065 #define USBCTL_ENABLE_PROD      (1 << 10)
2066 #define USBCTL_ENABLE_SERIAL    (1 << 9)
2067 #define USBCTL_ENABLE_DEFAULTS  (1 << 8)
2068 // bits 7-4 reserved, r/o
2069 #define USBCTL_FLUSH_OTHER      (1 << 3)
2070 #define USBCTL_FLUSH_THIS       (1 << 2)
2071 #define USBCTL_DISCONN_OTHER    (1 << 1)
2072 #define USBCTL_DISCONN_THIS     (1 << 0)
2073
2074 static inline void nc_dump_usbctl (struct usbnet *dev, u16 usbctl)
2075 {
2076         if (!netif_msg_link (dev))
2077                 return;
2078         devdbg (dev, "net1080 %s-%s usbctl 0x%x:%s%s%s%s%s;"
2079                         " this%s%s;"
2080                         " other%s%s; r/o 0x%x",
2081                 dev->udev->bus->bus_name, dev->udev->devpath,
2082                 usbctl,
2083                 (usbctl & USBCTL_ENABLE_LANG) ? " lang" : "",
2084                 (usbctl & USBCTL_ENABLE_MFGR) ? " mfgr" : "",
2085                 (usbctl & USBCTL_ENABLE_PROD) ? " prod" : "",
2086                 (usbctl & USBCTL_ENABLE_SERIAL) ? " serial" : "",
2087                 (usbctl & USBCTL_ENABLE_DEFAULTS) ? " defaults" : "",
2088
2089                 (usbctl & USBCTL_FLUSH_OTHER) ? " FLUSH" : "",
2090                 (usbctl & USBCTL_DISCONN_OTHER) ? " DIS" : "",
2091                 (usbctl & USBCTL_FLUSH_THIS) ? " FLUSH" : "",
2092                 (usbctl & USBCTL_DISCONN_THIS) ? " DIS" : "",
2093                 usbctl & ~USBCTL_WRITABLE_MASK
2094                 );
2095 }
2096
2097 /*-------------------------------------------------------------------------*/
2098
2099 /*
2100  * Status register
2101  */
2102
2103 #define STATUS_PORT_A           (1 << 15)
2104
2105 #define STATUS_CONN_OTHER       (1 << 14)
2106 #define STATUS_SUSPEND_OTHER    (1 << 13)
2107 #define STATUS_MAILBOX_OTHER    (1 << 12)
2108 #define STATUS_PACKETS_OTHER(n) (((n) >> 8) && 0x03)
2109
2110 #define STATUS_CONN_THIS        (1 << 6)
2111 #define STATUS_SUSPEND_THIS     (1 << 5)
2112 #define STATUS_MAILBOX_THIS     (1 << 4)
2113 #define STATUS_PACKETS_THIS(n)  (((n) >> 0) && 0x03)
2114
2115 #define STATUS_UNSPEC_MASK      0x0c8c
2116 #define STATUS_NOISE_MASK       ((u16)~(0x0303|STATUS_UNSPEC_MASK))
2117
2118
2119 static inline void nc_dump_status (struct usbnet *dev, u16 status)
2120 {
2121         if (!netif_msg_link (dev))
2122                 return;
2123         devdbg (dev, "net1080 %s-%s status 0x%x:"
2124                         " this (%c) PKT=%d%s%s%s;"
2125                         " other PKT=%d%s%s%s; unspec 0x%x",
2126                 dev->udev->bus->bus_name, dev->udev->devpath,
2127                 status,
2128
2129                 // XXX the packet counts don't seem right
2130                 // (1 at reset, not 0); maybe UNSPEC too
2131
2132                 (status & STATUS_PORT_A) ? 'A' : 'B',
2133                 STATUS_PACKETS_THIS (status),
2134                 (status & STATUS_CONN_THIS) ? " CON" : "",
2135                 (status & STATUS_SUSPEND_THIS) ? " SUS" : "",
2136                 (status & STATUS_MAILBOX_THIS) ? " MBOX" : "",
2137
2138                 STATUS_PACKETS_OTHER (status),
2139                 (status & STATUS_CONN_OTHER) ? " CON" : "",
2140                 (status & STATUS_SUSPEND_OTHER) ? " SUS" : "",
2141                 (status & STATUS_MAILBOX_OTHER) ? " MBOX" : "",
2142
2143                 status & STATUS_UNSPEC_MASK
2144                 );
2145 }
2146
2147 /*-------------------------------------------------------------------------*/
2148
2149 /*
2150  * TTL register
2151  */
2152
2153 #define TTL_THIS(ttl)   (0x00ff & ttl)
2154 #define TTL_OTHER(ttl)  (0x00ff & (ttl >> 8))
2155 #define MK_TTL(this,other)      ((u16)(((other)<<8)|(0x00ff&(this))))
2156
2157 static inline void nc_dump_ttl (struct usbnet *dev, u16 ttl)
2158 {
2159         if (netif_msg_link (dev))
2160                 devdbg (dev, "net1080 %s-%s ttl 0x%x this = %d, other = %d",
2161                         dev->udev->bus->bus_name, dev->udev->devpath,
2162                         ttl, TTL_THIS (ttl), TTL_OTHER (ttl));
2163 }
2164
2165 /*-------------------------------------------------------------------------*/
2166
2167 static int net1080_reset (struct usbnet *dev)
2168 {
2169         u16             usbctl, status, ttl;
2170         u16             *vp = kmalloc (sizeof (u16), GFP_KERNEL);
2171         int             retval;
2172
2173         if (!vp)
2174                 return -ENOMEM;
2175
2176         // nc_dump_registers (dev);
2177
2178         if ((retval = nc_register_read (dev, REG_STATUS, vp)) < 0) {
2179                 dbg ("can't read %s-%s status: %d",
2180                         dev->udev->bus->bus_name, dev->udev->devpath, retval);
2181                 goto done;
2182         }
2183         status = *vp;
2184         nc_dump_status (dev, status);
2185
2186         if ((retval = nc_register_read (dev, REG_USBCTL, vp)) < 0) {
2187                 dbg ("can't read USBCTL, %d", retval);
2188                 goto done;
2189         }
2190         usbctl = *vp;
2191         nc_dump_usbctl (dev, usbctl);
2192
2193         nc_register_write (dev, REG_USBCTL,
2194                         USBCTL_FLUSH_THIS | USBCTL_FLUSH_OTHER);
2195
2196         if ((retval = nc_register_read (dev, REG_TTL, vp)) < 0) {
2197                 dbg ("can't read TTL, %d", retval);
2198                 goto done;
2199         }
2200         ttl = *vp;
2201         // nc_dump_ttl (dev, ttl);
2202
2203         nc_register_write (dev, REG_TTL,
2204                         MK_TTL (NC_READ_TTL_MS, TTL_OTHER (ttl)) );
2205         dbg ("%s: assigned TTL, %d ms", dev->net->name, NC_READ_TTL_MS);
2206
2207         if (netif_msg_link (dev))
2208                 devinfo (dev, "port %c, peer %sconnected",
2209                         (status & STATUS_PORT_A) ? 'A' : 'B',
2210                         (status & STATUS_CONN_OTHER) ? "" : "dis"
2211                         );
2212         retval = 0;
2213
2214 done:
2215         kfree (vp);
2216         return retval;
2217 }
2218
2219 static int net1080_check_connect (struct usbnet *dev)
2220 {
2221         int                     retval;
2222         u16                     status;
2223         u16                     *vp = kmalloc (sizeof (u16), GFP_KERNEL);
2224
2225         if (!vp)
2226                 return -ENOMEM;
2227         retval = nc_register_read (dev, REG_STATUS, vp);
2228         status = *vp;
2229         kfree (vp);
2230         if (retval != 0) {
2231                 dbg ("%s net1080_check_conn read - %d", dev->net->name, retval);
2232                 return retval;
2233         }
2234         if ((status & STATUS_CONN_OTHER) != STATUS_CONN_OTHER)
2235                 return -ENOLINK;
2236         return 0;
2237 }
2238
2239 static void nc_flush_complete (struct urb *urb, struct pt_regs *regs)
2240 {
2241         kfree (urb->context);
2242         usb_free_urb(urb);
2243 }
2244
2245 static void nc_ensure_sync (struct usbnet *dev)
2246 {
2247         dev->frame_errors++;
2248         if (dev->frame_errors > 5) {
2249                 struct urb              *urb;
2250                 struct usb_ctrlrequest  *req;
2251                 int                     status;
2252
2253                 /* Send a flush */
2254                 urb = usb_alloc_urb (0, SLAB_ATOMIC);
2255                 if (!urb)
2256                         return;
2257
2258                 req = kmalloc (sizeof *req, GFP_ATOMIC);
2259                 if (!req) {
2260                         usb_free_urb (urb);
2261                         return;
2262                 }
2263
2264                 req->bRequestType = USB_DIR_OUT
2265                         | USB_TYPE_VENDOR
2266                         | USB_RECIP_DEVICE;
2267                 req->bRequest = REQUEST_REGISTER;
2268                 req->wValue = cpu_to_le16 (USBCTL_FLUSH_THIS
2269                                 | USBCTL_FLUSH_OTHER);
2270                 req->wIndex = cpu_to_le16 (REG_USBCTL);
2271                 req->wLength = cpu_to_le16 (0);
2272
2273                 /* queue an async control request, we don't need
2274                  * to do anything when it finishes except clean up.
2275                  */
2276                 usb_fill_control_urb (urb, dev->udev,
2277                         usb_sndctrlpipe (dev->udev, 0),
2278                         (unsigned char *) req,
2279                         NULL, 0,
2280                         nc_flush_complete, req);
2281                 status = usb_submit_urb (urb, GFP_ATOMIC);
2282                 if (status) {
2283                         kfree (req);
2284                         usb_free_urb (urb);
2285                         return;
2286                 }
2287
2288                 if (netif_msg_rx_err (dev))
2289                         devdbg (dev, "flush net1080; too many framing errors");
2290                 dev->frame_errors = 0;
2291         }
2292 }
2293
2294 static int net1080_rx_fixup (struct usbnet *dev, struct sk_buff *skb)
2295 {
2296         struct nc_header        *header;
2297         struct net_device       *net = dev->net;
2298         struct nc_trailer       *trailer;
2299         u16                     hdr_len, packet_len;
2300
2301         if (!(skb->len & 0x01)) {
2302                 dev->stats.rx_frame_errors++;
2303                 dbg ("rx framesize %d range %d..%d mtu %d", skb->len,
2304                         net->hard_header_len, dev->hard_mtu, net->mtu);
2305                 nc_ensure_sync (dev);
2306                 return 0;
2307         }
2308
2309         header = (struct nc_header *) skb->data;
2310         hdr_len = le16_to_cpup (&header->hdr_len);
2311         packet_len = le16_to_cpup (&header->packet_len);
2312         if (FRAMED_SIZE (packet_len) > NC_MAX_PACKET) {
2313                 dev->stats.rx_frame_errors++;
2314                 dbg ("packet too big, %d", packet_len);
2315                 nc_ensure_sync (dev);
2316                 return 0;
2317         } else if (hdr_len < MIN_HEADER) {
2318                 dev->stats.rx_frame_errors++;
2319                 dbg ("header too short, %d", hdr_len);
2320                 nc_ensure_sync (dev);
2321                 return 0;
2322         } else if (hdr_len > MIN_HEADER) {
2323                 // out of band data for us?
2324                 dbg ("header OOB, %d bytes", hdr_len - MIN_HEADER);
2325                 nc_ensure_sync (dev);
2326                 // switch (vendor/product ids) { ... }
2327         }
2328         skb_pull (skb, hdr_len);
2329
2330         trailer = (struct nc_trailer *)
2331                 (skb->data + skb->len - sizeof *trailer);
2332         skb_trim (skb, skb->len - sizeof *trailer);
2333
2334         if ((packet_len & 0x01) == 0) {
2335                 if (skb->data [packet_len] != PAD_BYTE) {
2336                         dev->stats.rx_frame_errors++;
2337                         dbg ("bad pad");
2338                         return 0;
2339                 }
2340                 skb_trim (skb, skb->len - 1);
2341         }
2342         if (skb->len != packet_len) {
2343                 dev->stats.rx_frame_errors++;
2344                 dbg ("bad packet len %d (expected %d)",
2345                         skb->len, packet_len);
2346                 nc_ensure_sync (dev);
2347                 return 0;
2348         }
2349         if (header->packet_id != get_unaligned (&trailer->packet_id)) {
2350                 dev->stats.rx_fifo_errors++;
2351                 dbg ("(2+ dropped) rx packet_id mismatch 0x%x 0x%x",
2352                         le16_to_cpu (header->packet_id),
2353                         le16_to_cpu (trailer->packet_id));
2354                 return 0;
2355         }
2356 #if 0
2357         devdbg (dev, "frame <rx h %d p %d id %d", header->hdr_len,
2358                 header->packet_len, header->packet_id);
2359 #endif
2360         dev->frame_errors = 0;
2361         return 1;
2362 }
2363
2364 static struct sk_buff *
2365 net1080_tx_fixup (struct usbnet *dev, struct sk_buff *skb, unsigned flags)
2366 {
2367         int                     padlen;
2368         struct sk_buff          *skb2;
2369
2370         padlen = ((skb->len + sizeof (struct nc_header)
2371                         + sizeof (struct nc_trailer)) & 0x01) ? 0 : 1;
2372         if (!skb_cloned (skb)) {
2373                 int     headroom = skb_headroom (skb);
2374                 int     tailroom = skb_tailroom (skb);
2375
2376                 if ((padlen + sizeof (struct nc_trailer)) <= tailroom
2377                             && sizeof (struct nc_header) <= headroom)
2378                         /* There's enough head and tail room */
2379                         return skb;
2380
2381                 if ((sizeof (struct nc_header) + padlen
2382                                         + sizeof (struct nc_trailer)) <
2383                                 (headroom + tailroom)) {
2384                         /* There's enough total room, so just readjust */
2385                         skb->data = memmove (skb->head
2386                                                 + sizeof (struct nc_header),
2387                                             skb->data, skb->len);
2388                         skb->tail = skb->data + skb->len;
2389                         return skb;
2390                 }
2391         }
2392
2393         /* Create a new skb to use with the correct size */
2394         skb2 = skb_copy_expand (skb,
2395                                 sizeof (struct nc_header),
2396                                 sizeof (struct nc_trailer) + padlen,
2397                                 flags);
2398         dev_kfree_skb_any (skb);
2399         return skb2;
2400 }
2401
2402 static int net1080_bind (struct usbnet *dev, struct usb_interface *intf)
2403 {
2404         unsigned        extra = sizeof (struct nc_header)
2405                                 + 1
2406                                 + sizeof (struct nc_trailer);
2407
2408         dev->net->hard_header_len += extra;
2409         dev->hard_mtu = NC_MAX_PACKET;
2410         return 0;
2411 }
2412
2413 static const struct driver_info net1080_info = {
2414         .description =  "NetChip TurboCONNECT",
2415         .flags =        FLAG_FRAMING_NC,
2416         .bind =         net1080_bind,
2417         .reset =        net1080_reset,
2418         .check_connect = net1080_check_connect,
2419         .rx_fixup =     net1080_rx_fixup,
2420         .tx_fixup =     net1080_tx_fixup,
2421 };
2422
2423 #endif /* CONFIG_USB_NET1080 */
2424
2425
2426 \f
2427 #ifdef CONFIG_USB_PL2301
2428 #define HAVE_HARDWARE
2429
2430 /*-------------------------------------------------------------------------
2431  *
2432  * Prolific PL-2301/PL-2302 driver ... http://www.prolifictech.com
2433  *
2434  * The protocol and handshaking used here should be bug-compatible
2435  * with the Linux 2.2 "plusb" driver, by Deti Fliegl.
2436  *
2437  *-------------------------------------------------------------------------*/
2438
2439 /*
2440  * Bits 0-4 can be used for software handshaking; they're set from
2441  * one end, cleared from the other, "read" with the interrupt byte.
2442  */
2443 #define PL_S_EN         (1<<7)          /* (feature only) suspend enable */
2444 /* reserved bit -- rx ready (6) ? */
2445 #define PL_TX_READY     (1<<5)          /* (interrupt only) transmit ready */
2446 #define PL_RESET_OUT    (1<<4)          /* reset output pipe */
2447 #define PL_RESET_IN     (1<<3)          /* reset input pipe */
2448 #define PL_TX_C         (1<<2)          /* transmission complete */
2449 #define PL_TX_REQ       (1<<1)          /* transmission received */
2450 #define PL_PEER_E       (1<<0)          /* peer exists */
2451
2452 static inline int
2453 pl_vendor_req (struct usbnet *dev, u8 req, u8 val, u8 index)
2454 {
2455         return usb_control_msg (dev->udev,
2456                 usb_rcvctrlpipe (dev->udev, 0),
2457                 req,
2458                 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
2459                 val, index,
2460                 NULL, 0,
2461                 CONTROL_TIMEOUT_MS);
2462 }
2463
2464 static inline int
2465 pl_clear_QuickLink_features (struct usbnet *dev, int val)
2466 {
2467         return pl_vendor_req (dev, 1, (u8) val, 0);
2468 }
2469
2470 static inline int
2471 pl_set_QuickLink_features (struct usbnet *dev, int val)
2472 {
2473         return pl_vendor_req (dev, 3, (u8) val, 0);
2474 }
2475
2476 /*-------------------------------------------------------------------------*/
2477
2478 static int pl_reset (struct usbnet *dev)
2479 {
2480         /* some units seem to need this reset, others reject it utterly.
2481          * FIXME be more like "naplink" or windows drivers.
2482          */
2483         (void) pl_set_QuickLink_features (dev,
2484                 PL_S_EN|PL_RESET_OUT|PL_RESET_IN|PL_PEER_E);
2485         return 0;
2486 }
2487
2488 static const struct driver_info prolific_info = {
2489         .description =  "Prolific PL-2301/PL-2302",
2490         .flags =        FLAG_NO_SETINT,
2491                 /* some PL-2302 versions seem to fail usb_set_interface() */
2492         .reset =        pl_reset,
2493 };
2494
2495 #endif /* CONFIG_USB_PL2301 */
2496
2497 \f
2498 #ifdef CONFIG_USB_KC2190
2499 #define HAVE_HARDWARE
2500 static const struct driver_info kc2190_info = {
2501         .description =  "KC Technology KC-190",
2502 };
2503 #endif /* CONFIG_USB_KC2190 */
2504
2505 \f
2506 #ifdef  CONFIG_USB_ARMLINUX
2507 #define HAVE_HARDWARE
2508
2509 /*-------------------------------------------------------------------------
2510  *
2511  * Intel's SA-1100 chip integrates basic USB support, and is used
2512  * in PDAs like some iPaqs, the Yopy, some Zaurus models, and more.
2513  * When they run Linux, arch/arm/mach-sa1100/usb-eth.c may be used to
2514  * network using minimal USB framing data.
2515  *
2516  * This describes the driver currently in standard ARM Linux kernels.
2517  * The Zaurus uses a different driver (see later).
2518  *
2519  * PXA25x and PXA210 use XScale cores (ARM v5TE) with better USB support
2520  * and different USB endpoint numbering than the SA1100 devices.  The
2521  * mach-pxa/usb-eth.c driver re-uses the device ids from mach-sa1100
2522  * so we rely on the endpoint descriptors.
2523  *
2524  *-------------------------------------------------------------------------*/
2525
2526 static const struct driver_info linuxdev_info = {
2527         .description =  "Linux Device",
2528         .check_connect = always_connected,
2529 };
2530
2531 static const struct driver_info yopy_info = {
2532         .description =  "Yopy",
2533         .check_connect = always_connected,
2534 };
2535
2536 static const struct driver_info blob_info = {
2537         .description =  "Boot Loader OBject",
2538         .check_connect = always_connected,
2539 };
2540
2541 #endif  /* CONFIG_USB_ARMLINUX */
2542
2543 \f
2544 #ifdef CONFIG_USB_ZAURUS
2545 #define HAVE_HARDWARE
2546
2547 #include <linux/crc32.h>
2548
2549 /*-------------------------------------------------------------------------
2550  *
2551  * Zaurus is also a SA-1110 based PDA, but one using a different driver
2552  * (and framing) for its USB slave/gadget controller than the case above.
2553  *
2554  * For the current version of that driver, the main way that framing is
2555  * nonstandard (also from perspective of the CDC ethernet model!) is a
2556  * crc32, added to help detect when some sa1100 usb-to-memory DMA errata
2557  * haven't been fully worked around.  Also, all Zaurii use the same
2558  * default Ethernet address.
2559  *
2560  * PXA based models use the same framing, and also can't implement
2561  * set_interface properly.
2562  *
2563  * All known Zaurii lie about their standards conformance.  Most lie by
2564  * saying they support CDC Ethernet.  Some lie and say they support CDC
2565  * MDLM (as if for access to cell phone modems).  Someone, please beat 
2566  * on Sharp (and other such vendors) for a while with a cluestick.
2567  *
2568  *-------------------------------------------------------------------------*/
2569
2570 static struct sk_buff *
2571 zaurus_tx_fixup (struct usbnet *dev, struct sk_buff *skb, unsigned flags)
2572 {
2573         int                     padlen;
2574         struct sk_buff          *skb2;
2575
2576         padlen = 2;
2577         if (!skb_cloned (skb)) {
2578                 int     tailroom = skb_tailroom (skb);
2579                 if ((padlen + 4) <= tailroom)
2580                         goto done;
2581         }
2582         skb2 = skb_copy_expand (skb, 0, 4 + padlen, flags);
2583         dev_kfree_skb_any (skb);
2584         skb = skb2;
2585         if (skb) {
2586                 u32             fcs;
2587 done:
2588                 fcs = crc32_le (~0, skb->data, skb->len);
2589                 fcs = ~fcs;
2590
2591                 *skb_put (skb, 1) = fcs       & 0xff;
2592                 *skb_put (skb, 1) = (fcs>> 8) & 0xff;
2593                 *skb_put (skb, 1) = (fcs>>16) & 0xff;
2594                 *skb_put (skb, 1) = (fcs>>24) & 0xff;
2595         }
2596         return skb;
2597 }
2598
2599 static int zaurus_bind (struct usbnet *dev, struct usb_interface *intf)
2600 {
2601         /* Belcarra's funky framing has other options; mostly
2602          * TRAILERS (!) with 4 bytes CRC, and maybe 2 pad bytes.
2603          */
2604         dev->net->hard_header_len += 6;
2605         return generic_cdc_bind(dev, intf);
2606 }
2607
2608 static const struct driver_info zaurus_sl5x00_info = {
2609         .description =  "Sharp Zaurus SL-5x00",
2610         .flags =        FLAG_FRAMING_Z,
2611         .check_connect = always_connected,
2612         .bind =         zaurus_bind,
2613         .unbind =       cdc_unbind,
2614         .tx_fixup =     zaurus_tx_fixup,
2615 };
2616 #define ZAURUS_STRONGARM_INFO   ((unsigned long)&zaurus_sl5x00_info)
2617
2618 static const struct driver_info zaurus_pxa_info = {
2619         .description =  "Sharp Zaurus, PXA-2xx based",
2620         .flags =        FLAG_FRAMING_Z,
2621         .check_connect = always_connected,
2622         .bind =         zaurus_bind,
2623         .unbind =       cdc_unbind,
2624         .tx_fixup =     zaurus_tx_fixup,
2625 };
2626 #define ZAURUS_PXA_INFO         ((unsigned long)&zaurus_pxa_info)
2627
2628 static const struct driver_info olympus_mxl_info = {
2629         .description =  "Olympus R1000",
2630         .flags =        FLAG_FRAMING_Z,
2631         .check_connect = always_connected,
2632         .bind =         zaurus_bind,
2633         .unbind =       cdc_unbind,
2634         .tx_fixup =     zaurus_tx_fixup,
2635 };
2636 #define OLYMPUS_MXL_INFO        ((unsigned long)&olympus_mxl_info)
2637
2638
2639 /* Some more recent products using Lineo/Belcarra code will wrongly claim
2640  * CDC MDLM conformance.  They aren't conformant:  data endpoints live
2641  * in the control interface, there's no data interface, and it's not used
2642  * to talk to a cell phone radio.  But at least we can detect these two
2643  * pseudo-classes, rather than growing this product list with entries for
2644  * each new nonconformant product (sigh).
2645  */
2646 static const u8 safe_guid[16] = {
2647         0x5d, 0x34, 0xcf, 0x66, 0x11, 0x18, 0x11, 0xd6,
2648         0xa2, 0x1a, 0x00, 0x01, 0x02, 0xca, 0x9a, 0x7f,
2649 };
2650 static const u8 blan_guid[16] = {
2651         0x74, 0xf0, 0x3d, 0xbd, 0x1e, 0xc1, 0x44, 0x70,
2652         0xa3, 0x67, 0x71, 0x34, 0xc9, 0xf5, 0x54, 0x37,
2653 };
2654
2655 static int blan_mdlm_bind (struct usbnet *dev, struct usb_interface *intf)
2656 {
2657         u8                              *buf = intf->cur_altsetting->extra;
2658         int                             len = intf->cur_altsetting->extralen;
2659         struct usb_cdc_mdlm_desc        *desc = NULL;
2660         struct usb_cdc_mdlm_detail_desc *detail = NULL;
2661
2662         while (len > 3) {
2663                 if (buf [1] != USB_DT_CS_INTERFACE)
2664                         goto next_desc;
2665
2666                 /* use bDescriptorSubType, and just verify that we get a
2667                  * "BLAN" (or "SAFE") descriptor.
2668                  */
2669                 switch (buf [2]) {
2670                 case USB_CDC_MDLM_TYPE:
2671                         if (desc) {
2672                                 dev_dbg (&intf->dev, "extra MDLM\n");
2673                                 goto bad_desc;
2674                         }
2675                         desc = (void *) buf;
2676                         if (desc->bLength != sizeof *desc) {
2677                                 dev_dbg (&intf->dev, "MDLM len %u\n",
2678                                         desc->bLength);
2679                                 goto bad_desc;
2680                         }
2681                         /* expect bcdVersion 1.0, ignore */
2682                         if (memcmp(&desc->bGUID, blan_guid, 16)
2683                                     && memcmp(&desc->bGUID, safe_guid, 16) ) {
2684                                 /* hey, this one might _really_ be MDLM! */
2685                                 dev_dbg (&intf->dev, "MDLM guid\n");
2686                                 goto bad_desc;
2687                         }
2688                         break;
2689                 case USB_CDC_MDLM_DETAIL_TYPE:
2690                         if (detail) {
2691                                 dev_dbg (&intf->dev, "extra MDLM detail\n");
2692                                 goto bad_desc;
2693                         }
2694                         detail = (void *) buf;
2695                         switch (detail->bGuidDescriptorType) {
2696                         case 0:                 /* "SAFE" */
2697                                 if (detail->bLength != (sizeof *detail + 2))
2698                                         goto bad_detail;
2699                                 break;
2700                         case 1:                 /* "BLAN" */
2701                                 if (detail->bLength != (sizeof *detail + 3))
2702                                         goto bad_detail;
2703                                 break;
2704                         default:
2705                                 goto bad_detail;
2706                         }
2707
2708                         /* assuming we either noticed BLAN already, or will
2709                          * find it soon, there are some data bytes here:
2710                          *  - bmNetworkCapabilities (unused)
2711                          *  - bmDataCapabilities (bits, see below)
2712                          *  - bPad (ignored, for PADAFTER -- BLAN-only)
2713                          * bits are:
2714                          *  - 0x01 -- Zaurus framing (add CRC)
2715                          *  - 0x02 -- PADBEFORE (CRC includes some padding)
2716                          *  - 0x04 -- PADAFTER (some padding after CRC)
2717                          *  - 0x08 -- "fermat" packet mangling (for hw bugs)
2718                          * the PADBEFORE appears not to matter; we interop
2719                          * with devices that use it and those that don't.
2720                          */
2721                         if ((detail->bDetailData[1] & ~02) != 0x01) {
2722                                 /* bmDataCapabilites == 0 would be fine too,
2723                                  * but framing is minidriver-coupled for now.
2724                                  */
2725 bad_detail:
2726                                 dev_dbg (&intf->dev,
2727                                                 "bad MDLM detail, %d %d %d\n",
2728                                                 detail->bLength,
2729                                                 detail->bDetailData[0],
2730                                                 detail->bDetailData[2]);
2731                                 goto bad_desc;
2732                         }
2733                         break;
2734                 }
2735 next_desc:
2736                 len -= buf [0]; /* bLength */
2737                 buf += buf [0];
2738         }
2739
2740         if (!desc || !detail) {
2741                 dev_dbg (&intf->dev, "missing cdc mdlm %s%sdescriptor\n",
2742                         desc ? "" : "func ",
2743                         detail ? "" : "detail ");
2744                 goto bad_desc;
2745         }
2746
2747         /* There's probably a CDC Ethernet descriptor there, but we can't
2748          * rely on the Ethernet address it provides since not all vendors
2749          * bother to make it unique.  Likewise there's no point in tracking
2750          * of the CDC event notifications.
2751          */
2752         return get_endpoints (dev, intf);
2753
2754 bad_desc:
2755         dev_info (&dev->udev->dev, "unsupported MDLM descriptors\n");
2756         return -ENODEV;
2757 }
2758
2759 static const struct driver_info bogus_mdlm_info = {
2760         .description =  "pseudo-MDLM (BLAN) device",
2761         .flags =        FLAG_FRAMING_Z,
2762         .check_connect = always_connected,
2763         .tx_fixup =     zaurus_tx_fixup,
2764         .bind =         blan_mdlm_bind,
2765 };
2766
2767 #else
2768
2769 /* blacklist all those devices */
2770 #define ZAURUS_STRONGARM_INFO   0
2771 #define ZAURUS_PXA_INFO         0
2772 #define OLYMPUS_MXL_INFO        0
2773
2774 #endif
2775
2776 \f
2777 /*-------------------------------------------------------------------------
2778  *
2779  * Network Device Driver (peer link to "Host Device", from USB host)
2780  *
2781  *-------------------------------------------------------------------------*/
2782
2783 static int usbnet_change_mtu (struct net_device *net, int new_mtu)
2784 {
2785         struct usbnet   *dev = netdev_priv(net);
2786         int             ll_mtu = new_mtu + net->hard_header_len;
2787
2788         if (new_mtu <= 0 || ll_mtu > dev->hard_mtu)
2789                 return -EINVAL;
2790         // no second zero-length packet read wanted after mtu-sized packets
2791         if ((ll_mtu % dev->maxpacket) == 0)
2792                 return -EDOM;
2793         net->mtu = new_mtu;
2794         return 0;
2795 }
2796
2797 /*-------------------------------------------------------------------------*/
2798
2799 static struct net_device_stats *usbnet_get_stats (struct net_device *net)
2800 {
2801         struct usbnet   *dev = netdev_priv(net);
2802         return &dev->stats;
2803 }
2804
2805 /*-------------------------------------------------------------------------*/
2806
2807 /* some LK 2.4 HCDs oopsed if we freed or resubmitted urbs from
2808  * completion callbacks.  2.5 should have fixed those bugs...
2809  */
2810
2811 static void defer_bh(struct usbnet *dev, struct sk_buff *skb, struct sk_buff_head *list)
2812 {
2813         unsigned long           flags;
2814
2815         spin_lock_irqsave(&list->lock, flags);
2816         __skb_unlink(skb, list);
2817         spin_unlock(&list->lock);
2818         spin_lock(&dev->done.lock);
2819         __skb_queue_tail(&dev->done, skb);
2820         if (dev->done.qlen == 1)
2821                 tasklet_schedule(&dev->bh);
2822         spin_unlock_irqrestore(&dev->done.lock, flags);
2823 }
2824
2825 /* some work can't be done in tasklets, so we use keventd
2826  *
2827  * NOTE:  annoying asymmetry:  if it's active, schedule_work() fails,
2828  * but tasklet_schedule() doesn't.  hope the failure is rare.
2829  */
2830 static void defer_kevent (struct usbnet *dev, int work)
2831 {
2832         set_bit (work, &dev->flags);
2833         if (!schedule_work (&dev->kevent))
2834                 deverr (dev, "kevent %d may have been dropped", work);
2835         else
2836                 devdbg (dev, "kevent %d scheduled", work);
2837 }
2838
2839 /*-------------------------------------------------------------------------*/
2840
2841 static void rx_complete (struct urb *urb, struct pt_regs *regs);
2842
2843 static void rx_submit (struct usbnet *dev, struct urb *urb, unsigned flags)
2844 {
2845         struct sk_buff          *skb;
2846         struct skb_data         *entry;
2847         int                     retval = 0;
2848         unsigned long           lockflags;
2849         size_t                  size;
2850
2851         size = max(dev->net->hard_header_len + dev->net->mtu,
2852                         (unsigned)ETH_FRAME_LEN);
2853         if ((skb = alloc_skb (size + NET_IP_ALIGN, flags)) == NULL) {
2854                 if (netif_msg_rx_err (dev))
2855                         devdbg (dev, "no rx skb");
2856                 defer_kevent (dev, EVENT_RX_MEMORY);
2857                 usb_free_urb (urb);
2858                 return;
2859         }
2860         skb_reserve (skb, NET_IP_ALIGN);
2861
2862         entry = (struct skb_data *) skb->cb;
2863         entry->urb = urb;
2864         entry->dev = dev;
2865         entry->state = rx_start;
2866         entry->length = 0;
2867
2868         usb_fill_bulk_urb (urb, dev->udev, dev->in,
2869                 skb->data, size, rx_complete, skb);
2870
2871         spin_lock_irqsave (&dev->rxq.lock, lockflags);
2872
2873         if (netif_running (dev->net)
2874                         && netif_device_present (dev->net)
2875                         && !test_bit (EVENT_RX_HALT, &dev->flags)) {
2876                 switch (retval = usb_submit_urb (urb, GFP_ATOMIC)){ 
2877                 case -EPIPE:
2878                         defer_kevent (dev, EVENT_RX_HALT);
2879                         break;
2880                 case -ENOMEM:
2881                         defer_kevent (dev, EVENT_RX_MEMORY);
2882                         break;
2883                 case -ENODEV:
2884                         if (netif_msg_ifdown (dev))
2885                                 devdbg (dev, "device gone");
2886                         netif_device_detach (dev->net);
2887                         break;
2888                 default:
2889                         if (netif_msg_rx_err (dev))
2890                                 devdbg (dev, "rx submit, %d", retval);
2891                         tasklet_schedule (&dev->bh);
2892                         break;
2893                 case 0:
2894                         __skb_queue_tail (&dev->rxq, skb);
2895                 }
2896         } else {
2897                 if (netif_msg_ifdown (dev))
2898                         devdbg (dev, "rx: stopped");
2899                 retval = -ENOLINK;
2900         }
2901         spin_unlock_irqrestore (&dev->rxq.lock, lockflags);
2902         if (retval) {
2903                 dev_kfree_skb_any (skb);
2904                 usb_free_urb (urb);
2905         }
2906 }
2907
2908
2909 /*-------------------------------------------------------------------------*/
2910
2911 static inline void rx_process (struct usbnet *dev, struct sk_buff *skb)
2912 {
2913         if (dev->driver_info->rx_fixup
2914                         && !dev->driver_info->rx_fixup (dev, skb))
2915                 goto error;
2916         // else network stack removes extra byte if we forced a short packet
2917
2918         if (skb->len)
2919                 skb_return (dev, skb);
2920         else {
2921                 if (netif_msg_rx_err (dev))
2922                         devdbg (dev, "drop");
2923 error:
2924                 dev->stats.rx_errors++;
2925                 skb_queue_tail (&dev->done, skb);
2926         }
2927 }
2928
2929 /*-------------------------------------------------------------------------*/
2930
2931 static void rx_complete (struct urb *urb, struct pt_regs *regs)
2932 {
2933         struct sk_buff          *skb = (struct sk_buff *) urb->context;
2934         struct skb_data         *entry = (struct skb_data *) skb->cb;
2935         struct usbnet           *dev = entry->dev;
2936         int                     urb_status = urb->status;
2937
2938         skb_put (skb, urb->actual_length);
2939         entry->state = rx_done;
2940         entry->urb = NULL;
2941
2942         switch (urb_status) {
2943             // success
2944             case 0:
2945                 if (skb->len < dev->net->hard_header_len) {
2946                         entry->state = rx_cleanup;
2947                         dev->stats.rx_errors++;
2948                         dev->stats.rx_length_errors++;
2949                         if (netif_msg_rx_err (dev))
2950                                 devdbg (dev, "rx length %d", skb->len);
2951                 }
2952                 break;
2953
2954             // stalls need manual reset. this is rare ... except that
2955             // when going through USB 2.0 TTs, unplug appears this way.
2956             // we avoid the highspeed version of the ETIMEOUT/EILSEQ
2957             // storm, recovering as needed.
2958             case -EPIPE:
2959                 dev->stats.rx_errors++;
2960                 defer_kevent (dev, EVENT_RX_HALT);
2961                 // FALLTHROUGH
2962
2963             // software-driven interface shutdown
2964             case -ECONNRESET:           // async unlink
2965             case -ESHUTDOWN:            // hardware gone
2966                 if (netif_msg_ifdown (dev))
2967                         devdbg (dev, "rx shutdown, code %d", urb_status);
2968                 goto block;
2969
2970             // we get controller i/o faults during khubd disconnect() delays.
2971             // throttle down resubmits, to avoid log floods; just temporarily,
2972             // so we still recover when the fault isn't a khubd delay.
2973             case -EPROTO:               // ehci
2974             case -ETIMEDOUT:            // ohci
2975             case -EILSEQ:               // uhci
2976                 dev->stats.rx_errors++;
2977                 if (!timer_pending (&dev->delay)) {
2978                         mod_timer (&dev->delay, jiffies + THROTTLE_JIFFIES);
2979                         if (netif_msg_link (dev))
2980                                 devdbg (dev, "rx throttle %d", urb_status);
2981                 }
2982 block:
2983                 entry->state = rx_cleanup;
2984                 entry->urb = urb;
2985                 urb = NULL;
2986                 break;
2987
2988             // data overrun ... flush fifo?
2989             case -EOVERFLOW:
2990                 dev->stats.rx_over_errors++;
2991                 // FALLTHROUGH
2992             
2993             default:
2994                 entry->state = rx_cleanup;
2995                 dev->stats.rx_errors++;
2996                 if (netif_msg_rx_err (dev))
2997                         devdbg (dev, "rx status %d", urb_status);
2998                 break;
2999         }
3000
3001         defer_bh(dev, skb, &dev->rxq);
3002
3003         if (urb) {
3004                 if (netif_running (dev->net)
3005                                 && !test_bit (EVENT_RX_HALT, &dev->flags)) {
3006                         rx_submit (dev, urb, GFP_ATOMIC);
3007                         return;
3008                 }
3009                 usb_free_urb (urb);
3010         }
3011         if (netif_msg_rx_err (dev))
3012                 devdbg (dev, "no read resubmitted");
3013 }
3014
3015 static void intr_complete (struct urb *urb, struct pt_regs *regs)
3016 {
3017         struct usbnet   *dev = urb->context;
3018         int             status = urb->status;
3019
3020         switch (status) {
3021             /* success */
3022             case 0:
3023                 dev->driver_info->status(dev, urb);
3024                 break;
3025
3026             /* software-driven interface shutdown */
3027             case -ENOENT:               // urb killed
3028             case -ESHUTDOWN:            // hardware gone
3029                 if (netif_msg_ifdown (dev))
3030                         devdbg (dev, "intr shutdown, code %d", status);
3031                 return;
3032
3033             /* NOTE:  not throttling like RX/TX, since this endpoint
3034              * already polls infrequently
3035              */
3036             default:
3037                 devdbg (dev, "intr status %d", status);
3038                 break;
3039         }
3040
3041         if (!netif_running (dev->net))
3042                 return;
3043
3044         memset(urb->transfer_buffer, 0, urb->transfer_buffer_length);
3045         status = usb_submit_urb (urb, GFP_ATOMIC);
3046         if (status != 0 && netif_msg_timer (dev))
3047                 deverr(dev, "intr resubmit --> %d", status);
3048 }
3049
3050 /*-------------------------------------------------------------------------*/
3051
3052 // unlink pending rx/tx; completion handlers do all other cleanup
3053
3054 static int unlink_urbs (struct usbnet *dev, struct sk_buff_head *q)
3055 {
3056         unsigned long           flags;
3057         struct sk_buff          *skb, *skbnext;
3058         int                     count = 0;
3059
3060         spin_lock_irqsave (&q->lock, flags);
3061         for (skb = q->next; skb != (struct sk_buff *) q; skb = skbnext) {
3062                 struct skb_data         *entry;
3063                 struct urb              *urb;
3064                 int                     retval;
3065
3066                 entry = (struct skb_data *) skb->cb;
3067                 urb = entry->urb;
3068                 skbnext = skb->next;
3069
3070                 // during some PM-driven resume scenarios,
3071                 // these (async) unlinks complete immediately
3072                 retval = usb_unlink_urb (urb);
3073                 if (retval != -EINPROGRESS && retval != 0)
3074                         devdbg (dev, "unlink urb err, %d", retval);
3075                 else
3076                         count++;
3077         }
3078         spin_unlock_irqrestore (&q->lock, flags);
3079         return count;
3080 }
3081
3082
3083 /*-------------------------------------------------------------------------*/
3084
3085 // precondition: never called in_interrupt
3086
3087 static int usbnet_stop (struct net_device *net)
3088 {
3089         struct usbnet           *dev = netdev_priv(net);
3090         int                     temp;
3091         DECLARE_WAIT_QUEUE_HEAD (unlink_wakeup); 
3092         DECLARE_WAITQUEUE (wait, current);
3093
3094         netif_stop_queue (net);
3095
3096         if (netif_msg_ifdown (dev))
3097                 devinfo (dev, "stop stats: rx/tx %ld/%ld, errs %ld/%ld",
3098                         dev->stats.rx_packets, dev->stats.tx_packets, 
3099                         dev->stats.rx_errors, dev->stats.tx_errors
3100                         );
3101
3102         // ensure there are no more active urbs
3103         add_wait_queue (&unlink_wakeup, &wait);
3104         dev->wait = &unlink_wakeup;
3105         temp = unlink_urbs (dev, &dev->txq) + unlink_urbs (dev, &dev->rxq);
3106
3107         // maybe wait for deletions to finish.
3108         while (!skb_queue_empty(&dev->rxq) &&
3109                !skb_queue_empty(&dev->txq) &&
3110                !skb_queue_empty(&dev->done)) {
3111                 msleep(UNLINK_TIMEOUT_MS);
3112                 if (netif_msg_ifdown (dev))
3113                         devdbg (dev, "waited for %d urb completions", temp);
3114         }
3115         dev->wait = NULL;
3116         remove_wait_queue (&unlink_wakeup, &wait); 
3117
3118         usb_kill_urb(dev->interrupt);
3119
3120         /* deferred work (task, timer, softirq) must also stop.
3121          * can't flush_scheduled_work() until we drop rtnl (later),
3122          * else workers could deadlock; so make workers a NOP.
3123          */
3124         dev->flags = 0;
3125         del_timer_sync (&dev->delay);
3126         tasklet_kill (&dev->bh);
3127
3128         return 0;
3129 }
3130
3131 /*-------------------------------------------------------------------------*/
3132
3133 // posts reads, and enables write queuing
3134
3135 // precondition: never called in_interrupt
3136
3137 static int usbnet_open (struct net_device *net)
3138 {
3139         struct usbnet           *dev = netdev_priv(net);
3140         int                     retval = 0;
3141         struct driver_info      *info = dev->driver_info;
3142
3143         // put into "known safe" state
3144         if (info->reset && (retval = info->reset (dev)) < 0) {
3145                 if (netif_msg_ifup (dev))
3146                         devinfo (dev,
3147                                 "open reset fail (%d) usbnet usb-%s-%s, %s",
3148                                 retval,
3149                                 dev->udev->bus->bus_name, dev->udev->devpath,
3150                         info->description);
3151                 goto done;
3152         }
3153
3154         // insist peer be connected
3155         if (info->check_connect && (retval = info->check_connect (dev)) < 0) {
3156                 if (netif_msg_ifup (dev))
3157                         devdbg (dev, "can't open; %d", retval);
3158                 goto done;
3159         }
3160
3161         /* start any status interrupt transfer */
3162         if (dev->interrupt) {
3163                 retval = usb_submit_urb (dev->interrupt, GFP_KERNEL);
3164                 if (retval < 0) {
3165                         if (netif_msg_ifup (dev))
3166                                 deverr (dev, "intr submit %d", retval);
3167                         goto done;
3168                 }
3169         }
3170
3171         netif_start_queue (net);
3172         if (netif_msg_ifup (dev)) {
3173                 char    *framing;
3174
3175                 if (dev->driver_info->flags & FLAG_FRAMING_NC)
3176                         framing = "NetChip";
3177                 else if (dev->driver_info->flags & FLAG_FRAMING_GL)
3178                         framing = "GeneSys";
3179                 else if (dev->driver_info->flags & FLAG_FRAMING_Z)
3180                         framing = "Zaurus";
3181                 else if (dev->driver_info->flags & FLAG_FRAMING_RN)
3182                         framing = "RNDIS";
3183                 else if (dev->driver_info->flags & FLAG_FRAMING_AX)
3184                         framing = "ASIX";
3185                 else
3186                         framing = "simple";
3187
3188                 devinfo (dev, "open: enable queueing "
3189                                 "(rx %d, tx %d) mtu %d %s framing",
3190                         RX_QLEN (dev), TX_QLEN (dev), dev->net->mtu,
3191                         framing);
3192         }
3193
3194         // delay posting reads until we're fully open
3195         tasklet_schedule (&dev->bh);
3196 done:
3197         return retval;
3198 }
3199
3200 /*-------------------------------------------------------------------------*/
3201
3202 static void usbnet_get_drvinfo (struct net_device *net, struct ethtool_drvinfo *info)
3203 {
3204         struct usbnet *dev = netdev_priv(net);
3205
3206         strncpy (info->driver, driver_name, sizeof info->driver);
3207         strncpy (info->version, DRIVER_VERSION, sizeof info->version);
3208         strncpy (info->fw_version, dev->driver_info->description,
3209                 sizeof info->fw_version);
3210         usb_make_path (dev->udev, info->bus_info, sizeof info->bus_info);
3211 }
3212
3213 static u32 usbnet_get_link (struct net_device *net)
3214 {
3215         struct usbnet *dev = netdev_priv(net);
3216
3217         /* If a check_connect is defined, return its result */
3218         if (dev->driver_info->check_connect)
3219                 return dev->driver_info->check_connect (dev) == 0;
3220
3221         /* Otherwise, say we're up (to avoid breaking scripts) */
3222         return 1;
3223 }
3224
3225 static u32 usbnet_get_msglevel (struct net_device *net)
3226 {
3227         struct usbnet *dev = netdev_priv(net);
3228
3229         return dev->msg_enable;
3230 }
3231
3232 static void usbnet_set_msglevel (struct net_device *net, u32 level)
3233 {
3234         struct usbnet *dev = netdev_priv(net);
3235
3236         dev->msg_enable = level;
3237 }
3238
3239 static int usbnet_ioctl (struct net_device *net, struct ifreq *rq, int cmd)
3240 {
3241 #ifdef NEED_MII
3242         {
3243         struct usbnet *dev = netdev_priv(net);
3244
3245         if (dev->mii.mdio_read != NULL && dev->mii.mdio_write != NULL)
3246                 return generic_mii_ioctl(&dev->mii, if_mii(rq), cmd, NULL);
3247         }
3248 #endif
3249         return -EOPNOTSUPP;
3250 }
3251
3252 /*-------------------------------------------------------------------------*/
3253
3254 /* work that cannot be done in interrupt context uses keventd.
3255  *
3256  * NOTE:  with 2.5 we could do more of this using completion callbacks,
3257  * especially now that control transfers can be queued.
3258  */
3259 static void
3260 kevent (void *data)
3261 {
3262         struct usbnet           *dev = data;
3263         int                     status;
3264
3265         /* usb_clear_halt() needs a thread context */
3266         if (test_bit (EVENT_TX_HALT, &dev->flags)) {
3267                 unlink_urbs (dev, &dev->txq);
3268                 status = usb_clear_halt (dev->udev, dev->out);
3269                 if (status < 0
3270                                 && status != -EPIPE
3271                                 && status != -ESHUTDOWN) {
3272                         if (netif_msg_tx_err (dev))
3273                                 deverr (dev, "can't clear tx halt, status %d",
3274                                         status);
3275                 } else {
3276                         clear_bit (EVENT_TX_HALT, &dev->flags);
3277                         if (status != -ESHUTDOWN)
3278                                 netif_wake_queue (dev->net);
3279                 }
3280         }
3281         if (test_bit (EVENT_RX_HALT, &dev->flags)) {
3282                 unlink_urbs (dev, &dev->rxq);
3283                 status = usb_clear_halt (dev->udev, dev->in);
3284                 if (status < 0
3285                                 && status != -EPIPE
3286                                 && status != -ESHUTDOWN) {
3287                         if (netif_msg_rx_err (dev))
3288                                 deverr (dev, "can't clear rx halt, status %d",
3289                                         status);
3290                 } else {
3291                         clear_bit (EVENT_RX_HALT, &dev->flags);
3292                         tasklet_schedule (&dev->bh);
3293                 }
3294         }
3295
3296         /* tasklet could resubmit itself forever if memory is tight */
3297         if (test_bit (EVENT_RX_MEMORY, &dev->flags)) {
3298                 struct urb      *urb = NULL;
3299
3300                 if (netif_running (dev->net))
3301                         urb = usb_alloc_urb (0, GFP_KERNEL);
3302                 else
3303                         clear_bit (EVENT_RX_MEMORY, &dev->flags);
3304                 if (urb != NULL) {
3305                         clear_bit (EVENT_RX_MEMORY, &dev->flags);
3306                         rx_submit (dev, urb, GFP_KERNEL);
3307                         tasklet_schedule (&dev->bh);
3308                 }
3309         }
3310
3311         if (test_bit (EVENT_LINK_RESET, &dev->flags)) {
3312                 struct driver_info      *info = dev->driver_info;
3313                 int                     retval = 0;
3314
3315                 clear_bit (EVENT_LINK_RESET, &dev->flags);
3316                 if(info->link_reset && (retval = info->link_reset(dev)) < 0) {
3317                         devinfo(dev, "link reset failed (%d) usbnet usb-%s-%s, %s",
3318                                 retval,
3319                                 dev->udev->bus->bus_name, dev->udev->devpath,
3320                                 info->description);
3321                 }
3322         }
3323
3324         if (dev->flags)
3325                 devdbg (dev, "kevent done, flags = 0x%lx",
3326                         dev->flags);
3327 }
3328
3329 /*-------------------------------------------------------------------------*/
3330
3331 static void tx_complete (struct urb *urb, struct pt_regs *regs)
3332 {
3333         struct sk_buff          *skb = (struct sk_buff *) urb->context;
3334         struct skb_data         *entry = (struct skb_data *) skb->cb;
3335         struct usbnet           *dev = entry->dev;
3336
3337         if (urb->status == 0) {
3338                 dev->stats.tx_packets++;
3339                 dev->stats.tx_bytes += entry->length;
3340         } else {
3341                 dev->stats.tx_errors++;
3342
3343                 switch (urb->status) {
3344                 case -EPIPE:
3345                         defer_kevent (dev, EVENT_TX_HALT);
3346                         break;
3347
3348                 /* software-driven interface shutdown */
3349                 case -ECONNRESET:               // async unlink
3350                 case -ESHUTDOWN:                // hardware gone
3351                         break;
3352
3353                 // like rx, tx gets controller i/o faults during khubd delays
3354                 // and so it uses the same throttling mechanism.
3355                 case -EPROTO:           // ehci
3356                 case -ETIMEDOUT:        // ohci
3357                 case -EILSEQ:           // uhci
3358                         if (!timer_pending (&dev->delay)) {
3359                                 mod_timer (&dev->delay,
3360                                         jiffies + THROTTLE_JIFFIES);
3361                                 if (netif_msg_link (dev))
3362                                         devdbg (dev, "tx throttle %d",
3363                                                         urb->status);
3364                         }
3365                         netif_stop_queue (dev->net);
3366                         break;
3367                 default:
3368                         if (netif_msg_tx_err (dev))
3369                                 devdbg (dev, "tx err %d", entry->urb->status);
3370                         break;
3371                 }
3372         }
3373
3374         urb->dev = NULL;
3375         entry->state = tx_done;
3376         defer_bh(dev, skb, &dev->txq);
3377 }
3378
3379 /*-------------------------------------------------------------------------*/
3380
3381 static void usbnet_tx_timeout (struct net_device *net)
3382 {
3383         struct usbnet           *dev = netdev_priv(net);
3384
3385         unlink_urbs (dev, &dev->txq);
3386         tasklet_schedule (&dev->bh);
3387
3388         // FIXME: device recovery -- reset?
3389 }
3390
3391 /*-------------------------------------------------------------------------*/
3392
3393 static int usbnet_start_xmit (struct sk_buff *skb, struct net_device *net)
3394 {
3395         struct usbnet           *dev = netdev_priv(net);
3396         int                     length;
3397         int                     retval = NET_XMIT_SUCCESS;
3398         struct urb              *urb = NULL;
3399         struct skb_data         *entry;
3400         struct driver_info      *info = dev->driver_info;
3401         unsigned long           flags;
3402 #ifdef  CONFIG_USB_NET1080
3403         struct nc_header        *header = NULL;
3404         struct nc_trailer       *trailer = NULL;
3405 #endif  /* CONFIG_USB_NET1080 */
3406
3407         // some devices want funky USB-level framing, for
3408         // win32 driver (usually) and/or hardware quirks
3409         if (info->tx_fixup) {
3410                 skb = info->tx_fixup (dev, skb, GFP_ATOMIC);
3411                 if (!skb) {
3412                         if (netif_msg_tx_err (dev))
3413                                 devdbg (dev, "can't tx_fixup skb");
3414                         goto drop;
3415                 }
3416         }
3417         length = skb->len;
3418
3419         if (!(urb = usb_alloc_urb (0, GFP_ATOMIC))) {
3420                 if (netif_msg_tx_err (dev))
3421                         devdbg (dev, "no urb");
3422                 goto drop;
3423         }
3424
3425         entry = (struct skb_data *) skb->cb;
3426         entry->urb = urb;
3427         entry->dev = dev;
3428         entry->state = tx_start;
3429         entry->length = length;
3430
3431         // FIXME: reorganize a bit, so that fixup() fills out NetChip
3432         // framing too. (Packet ID update needs the spinlock...)
3433         // [ BETTER:  we already own net->xmit_lock, that's enough ]
3434
3435 #ifdef  CONFIG_USB_NET1080
3436         if (info->flags & FLAG_FRAMING_NC) {
3437                 header = (struct nc_header *) skb_push (skb, sizeof *header);
3438                 header->hdr_len = cpu_to_le16 (sizeof (*header));
3439                 header->packet_len = cpu_to_le16 (length);
3440                 if (!((skb->len + sizeof *trailer) & 0x01))
3441                         *skb_put (skb, 1) = PAD_BYTE;
3442                 trailer = (struct nc_trailer *) skb_put (skb, sizeof *trailer);
3443         }
3444 #endif  /* CONFIG_USB_NET1080 */
3445
3446         usb_fill_bulk_urb (urb, dev->udev, dev->out,
3447                         skb->data, skb->len, tx_complete, skb);
3448
3449         /* don't assume the hardware handles USB_ZERO_PACKET
3450          * NOTE:  strictly conforming cdc-ether devices should expect
3451          * the ZLP here, but ignore the one-byte packet.
3452          *
3453          * FIXME zero that byte, if it doesn't require a new skb.
3454          */
3455         if ((length % dev->maxpacket) == 0)
3456                 urb->transfer_buffer_length++;
3457
3458         spin_lock_irqsave (&dev->txq.lock, flags);
3459
3460 #ifdef  CONFIG_USB_NET1080
3461         if (info->flags & FLAG_FRAMING_NC) {
3462                 header->packet_id = cpu_to_le16 ((u16)dev->xid++);
3463                 put_unaligned (header->packet_id, &trailer->packet_id);
3464 #if 0
3465                 devdbg (dev, "frame >tx h %d p %d id %d",
3466                         header->hdr_len, header->packet_len,
3467                         header->packet_id);
3468 #endif
3469         }
3470 #endif  /* CONFIG_USB_NET1080 */
3471
3472         switch ((retval = usb_submit_urb (urb, GFP_ATOMIC))) {
3473         case -EPIPE:
3474                 netif_stop_queue (net);
3475                 defer_kevent (dev, EVENT_TX_HALT);
3476                 break;
3477         default:
3478                 if (netif_msg_tx_err (dev))
3479                         devdbg (dev, "tx: submit urb err %d", retval);
3480                 break;
3481         case 0:
3482                 net->trans_start = jiffies;
3483                 __skb_queue_tail (&dev->txq, skb);
3484                 if (dev->txq.qlen >= TX_QLEN (dev))
3485                         netif_stop_queue (net);
3486         }
3487         spin_unlock_irqrestore (&dev->txq.lock, flags);
3488
3489         if (retval) {
3490                 if (netif_msg_tx_err (dev))
3491                         devdbg (dev, "drop, code %d", retval);
3492 drop:
3493                 retval = NET_XMIT_SUCCESS;
3494                 dev->stats.tx_dropped++;
3495                 if (skb)
3496                         dev_kfree_skb_any (skb);
3497                 usb_free_urb (urb);
3498         } else if (netif_msg_tx_queued (dev)) {
3499                 devdbg (dev, "> tx, len %d, type 0x%x",
3500                         length, skb->protocol);
3501         }
3502         return retval;
3503 }
3504
3505
3506 /*-------------------------------------------------------------------------*/
3507
3508 // tasklet (work deferred from completions, in_irq) or timer
3509
3510 static void usbnet_bh (unsigned long param)
3511 {
3512         struct usbnet           *dev = (struct usbnet *) param;
3513         struct sk_buff          *skb;
3514         struct skb_data         *entry;
3515
3516         while ((skb = skb_dequeue (&dev->done))) {
3517                 entry = (struct skb_data *) skb->cb;
3518                 switch (entry->state) {
3519                     case rx_done:
3520                         entry->state = rx_cleanup;
3521                         rx_process (dev, skb);
3522                         continue;
3523                     case tx_done:
3524                     case rx_cleanup:
3525                         usb_free_urb (entry->urb);
3526                         dev_kfree_skb (skb);
3527                         continue;
3528                     default:
3529                         devdbg (dev, "bogus skb state %d", entry->state);
3530                 }
3531         }
3532
3533         // waiting for all pending urbs to complete?
3534         if (dev->wait) {
3535                 if ((dev->txq.qlen + dev->rxq.qlen + dev->done.qlen) == 0) {
3536                         wake_up (dev->wait);
3537                 }
3538
3539         // or are we maybe short a few urbs?
3540         } else if (netif_running (dev->net)
3541                         && netif_device_present (dev->net)
3542                         && !timer_pending (&dev->delay)
3543                         && !test_bit (EVENT_RX_HALT, &dev->flags)) {
3544                 int     temp = dev->rxq.qlen;
3545                 int     qlen = RX_QLEN (dev);
3546
3547                 if (temp < qlen) {
3548                         struct urb      *urb;
3549                         int             i;
3550
3551                         // don't refill the queue all at once
3552                         for (i = 0; i < 10 && dev->rxq.qlen < qlen; i++) {
3553                                 urb = usb_alloc_urb (0, GFP_ATOMIC);
3554                                 if (urb != NULL)
3555                                         rx_submit (dev, urb, GFP_ATOMIC);
3556                         }
3557                         if (temp != dev->rxq.qlen && netif_msg_link (dev))
3558                                 devdbg (dev, "rxqlen %d --> %d",
3559                                                 temp, dev->rxq.qlen);
3560                         if (dev->rxq.qlen < qlen)
3561                                 tasklet_schedule (&dev->bh);
3562                 }
3563                 if (dev->txq.qlen < TX_QLEN (dev))
3564                         netif_wake_queue (dev->net);
3565         }
3566 }
3567
3568
3569 \f
3570 /*-------------------------------------------------------------------------
3571  *
3572  * USB Device Driver support
3573  *
3574  *-------------------------------------------------------------------------*/
3575  
3576 // precondition: never called in_interrupt
3577
3578 static void usbnet_disconnect (struct usb_interface *intf)
3579 {
3580         struct usbnet           *dev;
3581         struct usb_device       *xdev;
3582         struct net_device       *net;
3583
3584         dev = usb_get_intfdata(intf);
3585         usb_set_intfdata(intf, NULL);
3586         if (!dev)
3587                 return;
3588
3589         xdev = interface_to_usbdev (intf);
3590
3591         if (netif_msg_probe (dev))
3592                 devinfo (dev, "unregister usbnet usb-%s-%s, %s",
3593                         xdev->bus->bus_name, xdev->devpath,
3594                         dev->driver_info->description);
3595         
3596         net = dev->net;
3597         unregister_netdev (net);
3598
3599         /* we don't hold rtnl here ... */
3600         flush_scheduled_work ();
3601
3602         if (dev->driver_info->unbind)
3603                 dev->driver_info->unbind (dev, intf);
3604
3605         free_netdev(net);
3606         usb_put_dev (xdev);
3607 }
3608
3609
3610 /*-------------------------------------------------------------------------*/
3611
3612 static struct ethtool_ops usbnet_ethtool_ops;
3613
3614 // precondition: never called in_interrupt
3615
3616 static int
3617 usbnet_probe (struct usb_interface *udev, const struct usb_device_id *prod)
3618 {
3619         struct usbnet                   *dev;
3620         struct net_device               *net;
3621         struct usb_host_interface       *interface;
3622         struct driver_info              *info;
3623         struct usb_device               *xdev;
3624         int                             status;
3625
3626         info = (struct driver_info *) prod->driver_info;
3627         if (!info) {
3628                 dev_dbg (&udev->dev, "blacklisted by %s\n", driver_name);
3629                 return -ENODEV;
3630         }
3631         xdev = interface_to_usbdev (udev);
3632         interface = udev->cur_altsetting;
3633
3634         usb_get_dev (xdev);
3635
3636         status = -ENOMEM;
3637
3638         // set up our own records
3639         net = alloc_etherdev(sizeof(*dev));
3640         if (!net) {
3641                 dbg ("can't kmalloc dev");
3642                 goto out;
3643         }
3644
3645         dev = netdev_priv(net);
3646         dev->udev = xdev;
3647         dev->driver_info = info;
3648         dev->msg_enable = netif_msg_init (msg_level, NETIF_MSG_DRV
3649                                 | NETIF_MSG_PROBE | NETIF_MSG_LINK);
3650         skb_queue_head_init (&dev->rxq);
3651         skb_queue_head_init (&dev->txq);
3652         skb_queue_head_init (&dev->done);
3653         dev->bh.func = usbnet_bh;
3654         dev->bh.data = (unsigned long) dev;
3655         INIT_WORK (&dev->kevent, kevent, dev);
3656         dev->delay.function = usbnet_bh;
3657         dev->delay.data = (unsigned long) dev;
3658         init_timer (&dev->delay);
3659
3660         SET_MODULE_OWNER (net);
3661         dev->net = net;
3662         strcpy (net->name, "usb%d");
3663         memcpy (net->dev_addr, node_id, sizeof node_id);
3664         dev->hard_mtu = net->mtu + net->hard_header_len;
3665
3666 #if 0
3667 // dma_supported() is deeply broken on almost all architectures
3668         // possible with some EHCI controllers
3669         if (dma_supported (&udev->dev, DMA_64BIT_MASK))
3670                 net->features |= NETIF_F_HIGHDMA;
3671 #endif
3672
3673         net->change_mtu = usbnet_change_mtu;
3674         net->get_stats = usbnet_get_stats;
3675         net->hard_start_xmit = usbnet_start_xmit;
3676         net->open = usbnet_open;
3677         net->stop = usbnet_stop;
3678         net->watchdog_timeo = TX_TIMEOUT_JIFFIES;
3679         net->tx_timeout = usbnet_tx_timeout;
3680         net->do_ioctl = usbnet_ioctl;
3681         net->ethtool_ops = &usbnet_ethtool_ops;
3682
3683         // allow device-specific bind/init procedures
3684         // NOTE net->name still not usable ...
3685         if (info->bind) {
3686                 status = info->bind (dev, udev);
3687                 // heuristic:  "usb%d" for links we know are two-host,
3688                 // else "eth%d" when there's reasonable doubt.  userspace
3689                 // can rename the link if it knows better.
3690                 if ((dev->driver_info->flags & FLAG_ETHER) != 0
3691                                 && (net->dev_addr [0] & 0x02) == 0)
3692                         strcpy (net->name, "eth%d");
3693
3694                 /* maybe the remote can't receive an Ethernet MTU */
3695                 if (net->mtu > (dev->hard_mtu - net->hard_header_len))
3696                         net->mtu = dev->hard_mtu - net->hard_header_len;
3697         } else if (!info->in || info->out)
3698                 status = get_endpoints (dev, udev);
3699         else {
3700                 dev->in = usb_rcvbulkpipe (xdev, info->in);
3701                 dev->out = usb_sndbulkpipe (xdev, info->out);
3702                 if (!(info->flags & FLAG_NO_SETINT))
3703                         status = usb_set_interface (xdev,
3704                                 interface->desc.bInterfaceNumber,
3705                                 interface->desc.bAlternateSetting);
3706                 else
3707                         status = 0;
3708
3709         }
3710         if (status == 0 && dev->status)
3711                 status = init_status (dev, udev);
3712         if (status < 0)
3713                 goto out1;
3714
3715         dev->maxpacket = usb_maxpacket (dev->udev, dev->out, 1);
3716         
3717         SET_NETDEV_DEV(net, &udev->dev);
3718         status = register_netdev (net);
3719         if (status)
3720                 goto out3;
3721         if (netif_msg_probe (dev))
3722                 devinfo (dev, "register usbnet at usb-%s-%s, %s, "
3723                                 "%02x:%02x:%02x:%02x:%02x:%02x",
3724                         xdev->bus->bus_name, xdev->devpath,
3725                         dev->driver_info->description,
3726                         net->dev_addr [0], net->dev_addr [1],
3727                         net->dev_addr [2], net->dev_addr [3],
3728                         net->dev_addr [4], net->dev_addr [5]);
3729
3730         // ok, it's ready to go.
3731         usb_set_intfdata (udev, dev);
3732
3733         // start as if the link is up
3734         netif_device_attach (net);
3735
3736         return 0;
3737
3738 out3:
3739         if (info->unbind)
3740                 info->unbind (dev, udev);
3741 out1:
3742         free_netdev(net);
3743 out:
3744         usb_put_dev(xdev);
3745         return status;
3746 }
3747
3748 /*-------------------------------------------------------------------------*/
3749
3750 #ifdef  CONFIG_PM
3751
3752 static int usbnet_suspend (struct usb_interface *intf, pm_message_t message)
3753 {
3754         struct usbnet           *dev = usb_get_intfdata(intf);
3755         
3756         /* accelerate emptying of the rx and queues, to avoid
3757          * having everything error out.
3758          */
3759         netif_device_detach (dev->net);
3760         (void) unlink_urbs (dev, &dev->rxq);
3761         (void) unlink_urbs (dev, &dev->txq);
3762         intf->dev.power.power_state = PMSG_SUSPEND;
3763         return 0;
3764 }
3765
3766 static int usbnet_resume (struct usb_interface *intf)
3767 {
3768         struct usbnet           *dev = usb_get_intfdata(intf);
3769
3770         intf->dev.power.power_state = PMSG_ON;
3771         netif_device_attach (dev->net);
3772         tasklet_schedule (&dev->bh);
3773         return 0;
3774 }
3775
3776 #else   /* !CONFIG_PM */
3777
3778 #define usbnet_suspend  NULL
3779 #define usbnet_resume   NULL
3780
3781 #endif  /* CONFIG_PM */
3782
3783 /*-------------------------------------------------------------------------*/
3784
3785 #ifndef HAVE_HARDWARE
3786 #error You need to configure some hardware for this driver
3787 #endif
3788
3789 /*
3790  * chip vendor names won't normally be on the cables, and
3791  * may not be on the device.
3792  */
3793
3794 static const struct usb_device_id       products [] = {
3795
3796 #ifdef  CONFIG_USB_ALI_M5632
3797 {
3798         USB_DEVICE (0x0402, 0x5632),    // ALi defaults
3799         .driver_info =  (unsigned long) &ali_m5632_info,
3800 },
3801 #endif
3802
3803 #ifdef  CONFIG_USB_AN2720
3804 {
3805         USB_DEVICE (0x0547, 0x2720),    // AnchorChips defaults
3806         .driver_info =  (unsigned long) &an2720_info,
3807 }, {
3808         USB_DEVICE (0x0547, 0x2727),    // Xircom PGUNET
3809         .driver_info =  (unsigned long) &an2720_info,
3810 },
3811 #endif
3812
3813 #ifdef  CONFIG_USB_BELKIN
3814 {
3815         USB_DEVICE (0x050d, 0x0004),    // Belkin
3816         .driver_info =  (unsigned long) &belkin_info,
3817 }, {
3818         USB_DEVICE (0x056c, 0x8100),    // eTEK
3819         .driver_info =  (unsigned long) &belkin_info,
3820 }, {
3821         USB_DEVICE (0x0525, 0x9901),    // Advance USBNET (eTEK)
3822         .driver_info =  (unsigned long) &belkin_info,
3823 },
3824 #endif
3825
3826 #ifdef CONFIG_USB_AX8817X
3827 {
3828         // Linksys USB200M
3829         USB_DEVICE (0x077b, 0x2226),
3830         .driver_info =  (unsigned long) &ax8817x_info,
3831 }, {
3832         // Netgear FA120
3833         USB_DEVICE (0x0846, 0x1040),
3834         .driver_info =  (unsigned long) &netgear_fa120_info,
3835 }, {
3836         // DLink DUB-E100
3837         USB_DEVICE (0x2001, 0x1a00),
3838         .driver_info =  (unsigned long) &dlink_dub_e100_info,
3839 }, {
3840         // Intellinet, ST Lab USB Ethernet
3841         USB_DEVICE (0x0b95, 0x1720),
3842         .driver_info =  (unsigned long) &ax8817x_info,
3843 }, {
3844         // Hawking UF200, TrendNet TU2-ET100
3845         USB_DEVICE (0x07b8, 0x420a),
3846         .driver_info =  (unsigned long) &hawking_uf200_info,
3847 }, {
3848         // Billionton Systems, USB2AR 
3849         USB_DEVICE (0x08dd, 0x90ff),
3850         .driver_info =  (unsigned long) &ax8817x_info,
3851 }, {
3852         // ATEN UC210T
3853         USB_DEVICE (0x0557, 0x2009),
3854         .driver_info =  (unsigned long) &ax8817x_info,
3855 }, {
3856         // Buffalo LUA-U2-KTX
3857         USB_DEVICE (0x0411, 0x003d),
3858         .driver_info =  (unsigned long) &ax8817x_info,
3859 }, {
3860         // Sitecom LN-029 "USB 2.0 10/100 Ethernet adapter"
3861         USB_DEVICE (0x6189, 0x182d),
3862         .driver_info =  (unsigned long) &ax8817x_info,
3863 }, {
3864         // corega FEther USB2-TX
3865         USB_DEVICE (0x07aa, 0x0017),
3866         .driver_info =  (unsigned long) &ax8817x_info,
3867 }, {
3868         // Surecom EP-1427X-2
3869         USB_DEVICE (0x1189, 0x0893),
3870         .driver_info = (unsigned long) &ax8817x_info,
3871 }, {
3872         // goodway corp usb gwusb2e
3873         USB_DEVICE (0x1631, 0x6200),
3874         .driver_info = (unsigned long) &ax8817x_info,
3875 }, {
3876         // ASIX AX88772 10/100
3877         USB_DEVICE (0x0b95, 0x7720),
3878         .driver_info = (unsigned long) &ax88772_info,
3879 },
3880 #endif
3881
3882 #ifdef  CONFIG_USB_EPSON2888
3883 {
3884         USB_DEVICE (0x0525, 0x2888),    // EPSON USB client
3885         .driver_info    = (unsigned long) &epson2888_info,
3886 },
3887 #endif
3888
3889 #ifdef  CONFIG_USB_GENESYS
3890 {
3891         USB_DEVICE (0x05e3, 0x0502),    // GL620USB-A
3892         .driver_info =  (unsigned long) &genelink_info,
3893 },
3894         /* NOT: USB_DEVICE (0x05e3, 0x0501),    // GL620USB
3895          * that's half duplex, not currently supported
3896          */
3897 #endif
3898
3899 #ifdef  CONFIG_USB_NET1080
3900 {
3901         USB_DEVICE (0x0525, 0x1080),    // NetChip ref design
3902         .driver_info =  (unsigned long) &net1080_info,
3903 }, {
3904         USB_DEVICE (0x06D0, 0x0622),    // Laplink Gold
3905         .driver_info =  (unsigned long) &net1080_info,
3906 },
3907 #endif
3908
3909 #ifdef CONFIG_USB_PL2301
3910 {
3911         USB_DEVICE (0x067b, 0x0000),    // PL-2301
3912         .driver_info =  (unsigned long) &prolific_info,
3913 }, {
3914         USB_DEVICE (0x067b, 0x0001),    // PL-2302
3915         .driver_info =  (unsigned long) &prolific_info,
3916 },
3917 #endif
3918
3919 #ifdef CONFIG_USB_KC2190
3920 {
3921         USB_DEVICE (0x050f, 0x0190),    // KC-190
3922         .driver_info =  (unsigned long) &kc2190_info,
3923 },
3924 #endif
3925
3926 #ifdef  CONFIG_USB_RNDIS
3927 {
3928         /* RNDIS is MSFT's un-official variant of CDC ACM */
3929         USB_INTERFACE_INFO (USB_CLASS_COMM, 2 /* ACM */, 0x0ff),
3930         .driver_info = (unsigned long) &rndis_info,
3931 },
3932 #endif
3933
3934 #ifdef  CONFIG_USB_ARMLINUX
3935 /*
3936  * SA-1100 using standard ARM Linux kernels, or compatible.
3937  * Often used when talking to Linux PDAs (iPaq, Yopy, etc).
3938  * The sa-1100 "usb-eth" driver handles the basic framing.
3939  *
3940  * PXA25x or PXA210 ...  these use a "usb-eth" driver much like
3941  * the sa1100 one, but hardware uses different endpoint numbers.
3942  *
3943  * Or the Linux "Ethernet" gadget on hardware that can't talk
3944  * CDC Ethernet (e.g., no altsettings), in either of two modes:
3945  *  - acting just like the old "usb-eth" firmware, though
3946  *    the implementation is different 
3947  *  - supporting RNDIS as the first/default configuration for
3948  *    MS-Windows interop; Linux needs to use the other config
3949  */
3950 {
3951         // 1183 = 0x049F, both used as hex values?
3952         // Compaq "Itsy" vendor/product id
3953         USB_DEVICE (0x049F, 0x505A),    // usb-eth, or compatible
3954         .driver_info =  (unsigned long) &linuxdev_info,
3955 }, {
3956         USB_DEVICE (0x0E7E, 0x1001),    // G.Mate "Yopy"
3957         .driver_info =  (unsigned long) &yopy_info,
3958 }, {
3959         USB_DEVICE (0x8086, 0x07d3),    // "blob" bootloader
3960         .driver_info =  (unsigned long) &blob_info,
3961 }, {
3962         // Linux Ethernet/RNDIS gadget on pxa210/25x/26x
3963         // e.g. Gumstix, current OpenZaurus, ...
3964         USB_DEVICE_VER (0x0525, 0xa4a2, 0x0203, 0x0203),
3965         .driver_info =  (unsigned long) &linuxdev_info,
3966 }, 
3967 #endif
3968
3969 #if     defined(CONFIG_USB_ZAURUS) || defined(CONFIG_USB_CDCETHER)
3970 /*
3971  * SA-1100 based Sharp Zaurus ("collie"), or compatible.
3972  * Same idea as above, but different framing.
3973  *
3974  * PXA-2xx based models are also lying-about-cdc.
3975  * Some models don't even tell the same lies ...
3976  *
3977  * NOTE:  OpenZaurus versions with 2.6 kernels won't use these entries,
3978  * unlike the older ones with 2.4 "embedix" kernels.
3979  *
3980  * NOTE:  These entries do double-duty, serving as blacklist entries
3981  * whenever Zaurus support isn't enabled, but CDC Ethernet is.
3982  */
3983 #define ZAURUS_MASTER_INTERFACE \
3984         .bInterfaceClass        = USB_CLASS_COMM, \
3985         .bInterfaceSubClass     = USB_CDC_SUBCLASS_ETHERNET, \
3986         .bInterfaceProtocol     = USB_CDC_PROTO_NONE
3987 {
3988         .match_flags    =   USB_DEVICE_ID_MATCH_INT_INFO
3989                           | USB_DEVICE_ID_MATCH_DEVICE, 
3990         .idVendor               = 0x04DD,
3991         .idProduct              = 0x8004,
3992         ZAURUS_MASTER_INTERFACE,
3993         .driver_info = ZAURUS_STRONGARM_INFO,
3994 }, {
3995         .match_flags    =   USB_DEVICE_ID_MATCH_INT_INFO
3996                           | USB_DEVICE_ID_MATCH_DEVICE, 
3997         .idVendor               = 0x04DD,
3998         .idProduct              = 0x8005,       /* A-300 */
3999         ZAURUS_MASTER_INTERFACE,
4000         .driver_info = ZAURUS_PXA_INFO,
4001 }, {
4002         .match_flags    =   USB_DEVICE_ID_MATCH_INT_INFO
4003                           | USB_DEVICE_ID_MATCH_DEVICE, 
4004         .idVendor               = 0x04DD,
4005         .idProduct              = 0x8006,       /* B-500/SL-5600 */
4006         ZAURUS_MASTER_INTERFACE,
4007         .driver_info = ZAURUS_PXA_INFO,
4008 }, {
4009         .match_flags    =   USB_DEVICE_ID_MATCH_INT_INFO
4010                   | USB_DEVICE_ID_MATCH_DEVICE,
4011         .idVendor               = 0x04DD,
4012         .idProduct              = 0x8007,       /* C-700 */
4013         ZAURUS_MASTER_INTERFACE,
4014         .driver_info = ZAURUS_PXA_INFO,
4015 }, {
4016         .match_flags    =   USB_DEVICE_ID_MATCH_INT_INFO
4017                  | USB_DEVICE_ID_MATCH_DEVICE,
4018         .idVendor               = 0x04DD,
4019         .idProduct              = 0x9031,       /* C-750 C-760 */
4020         ZAURUS_MASTER_INTERFACE,
4021         .driver_info = ZAURUS_PXA_INFO,
4022 }, {
4023         .match_flags    =   USB_DEVICE_ID_MATCH_INT_INFO
4024                  | USB_DEVICE_ID_MATCH_DEVICE,
4025         .idVendor               = 0x04DD,
4026         .idProduct              = 0x9032,       /* SL-6000 */
4027         ZAURUS_MASTER_INTERFACE,
4028         .driver_info = ZAURUS_PXA_INFO,
4029 }, {
4030         .match_flags    =   USB_DEVICE_ID_MATCH_INT_INFO
4031                  | USB_DEVICE_ID_MATCH_DEVICE,
4032         .idVendor               = 0x04DD,
4033         /* reported with some C860 units */
4034         .idProduct              = 0x9050,       /* C-860 */
4035         ZAURUS_MASTER_INTERFACE,
4036         .driver_info = ZAURUS_PXA_INFO,
4037 },
4038
4039 #ifdef  CONFIG_USB_ZAURUS
4040         /* At least some (reports vary) PXA units have very different lies
4041          * about their standards support:  they claim to be cell phones with
4042          * direct access to their radios.  (They don't conform to CDC MDLM.)
4043          */
4044 {
4045         USB_INTERFACE_INFO (USB_CLASS_COMM, USB_CDC_SUBCLASS_MDLM,
4046                         USB_CDC_PROTO_NONE),
4047         .driver_info = (unsigned long) &bogus_mdlm_info,
4048 },
4049 #endif
4050
4051 /* Olympus has some models with a Zaurus-compatible option.
4052  * R-1000 uses a FreeScale i.MXL cpu (ARMv4T)
4053  */
4054 {
4055         .match_flags    =   USB_DEVICE_ID_MATCH_INT_INFO
4056                  | USB_DEVICE_ID_MATCH_DEVICE,
4057         .idVendor               = 0x07B4,
4058         .idProduct              = 0x0F02,       /* R-1000 */
4059         ZAURUS_MASTER_INTERFACE,
4060         .driver_info = OLYMPUS_MXL_INFO,
4061 },
4062 #endif
4063
4064 #ifdef  CONFIG_USB_CDCETHER
4065 {
4066         /* CDC Ether uses two interfaces, not necessarily consecutive.
4067          * We match the main interface, ignoring the optional device
4068          * class so we could handle devices that aren't exclusively
4069          * CDC ether.
4070          *
4071          * NOTE:  this match must come AFTER entries working around
4072          * bugs/quirks in a given product (like Zaurus, above).
4073          */
4074         USB_INTERFACE_INFO (USB_CLASS_COMM, USB_CDC_SUBCLASS_ETHERNET,
4075                         USB_CDC_PROTO_NONE),
4076         .driver_info = (unsigned long) &cdc_info,
4077 },
4078 #endif
4079
4080         { },            // END
4081 };
4082 MODULE_DEVICE_TABLE (usb, products);
4083
4084 static struct usb_driver usbnet_driver = {
4085         .owner =        THIS_MODULE,
4086         .name =         driver_name,
4087         .id_table =     products,
4088         .probe =        usbnet_probe,
4089         .disconnect =   usbnet_disconnect,
4090         .suspend =      usbnet_suspend,
4091         .resume =       usbnet_resume,
4092 };
4093
4094 /* Default ethtool_ops assigned.  Devices can override in their bind() routine */
4095 static struct ethtool_ops usbnet_ethtool_ops = {
4096         .get_drvinfo            = usbnet_get_drvinfo,
4097         .get_link               = usbnet_get_link,
4098         .get_msglevel           = usbnet_get_msglevel,
4099         .set_msglevel           = usbnet_set_msglevel,
4100 };
4101
4102 /*-------------------------------------------------------------------------*/
4103
4104 static int __init usbnet_init(void)
4105 {
4106         // compiler should optimize these out
4107         BUG_ON (sizeof (((struct sk_buff *)0)->cb)
4108                         < sizeof (struct skb_data));
4109 #ifdef  CONFIG_USB_CDCETHER
4110         BUG_ON ((sizeof (((struct usbnet *)0)->data)
4111                         < sizeof (struct cdc_state)));
4112 #endif
4113
4114         random_ether_addr(node_id);
4115
4116         return usb_register(&usbnet_driver);
4117 }
4118 module_init(usbnet_init);
4119
4120 static void __exit usbnet_exit(void)
4121 {
4122         usb_deregister(&usbnet_driver);
4123 }
4124 module_exit(usbnet_exit);
4125
4126 MODULE_AUTHOR("David Brownell");
4127 MODULE_DESCRIPTION("USB Host-to-Host Link Drivers (numerous vendors)");
4128 MODULE_LICENSE("GPL");