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