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