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