Merge /spare/repo/linux-2.6/
[linux-2.6] / drivers / usb / net / pegasus.c
1 /*
2  *  Copyright (c) 1999-2005 Petko Manolov (petkan@users.sourceforge.net)
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  *      ChangeLog:
9  *              ....    Most of the time spent on reading sources & docs.
10  *              v0.2.x  First official release for the Linux kernel.
11  *              v0.3.0  Beutified and structured, some bugs fixed.
12  *              v0.3.x  URBifying bulk requests and bugfixing. First relatively
13  *                      stable release. Still can touch device's registers only
14  *                      from top-halves.
15  *              v0.4.0  Control messages remained unurbified are now URBs.
16  *                      Now we can touch the HW at any time.
17  *              v0.4.9  Control urbs again use process context to wait. Argh...
18  *                      Some long standing bugs (enable_net_traffic) fixed.
19  *                      Also nasty trick about resubmiting control urb from
20  *                      interrupt context used. Please let me know how it
21  *                      behaves. Pegasus II support added since this version.
22  *                      TODO: suppressing HCD warnings spewage on disconnect.
23  *              v0.4.13 Ethernet address is now set at probe(), not at open()
24  *                      time as this seems to break dhcpd. 
25  *              v0.5.0  branch to 2.5.x kernels
26  *              v0.5.1  ethtool support added
27  *              v0.5.5  rx socket buffers are in a pool and the their allocation
28  *                      is out of the interrupt routine.
29  */
30
31 #undef  DEBUG
32
33 #include <linux/sched.h>
34 #include <linux/slab.h>
35 #include <linux/init.h>
36 #include <linux/delay.h>
37 #include <linux/netdevice.h>
38 #include <linux/etherdevice.h>
39 #include <linux/ethtool.h>
40 #include <linux/mii.h>
41 #include <linux/usb.h>
42 #include <linux/module.h>
43 #include <asm/byteorder.h>
44 #include <asm/uaccess.h>
45 #include "pegasus.h"
46
47 /*
48  * Version Information
49  */
50 #define DRIVER_VERSION "v0.6.12 (2005/01/13)"
51 #define DRIVER_AUTHOR "Petko Manolov <petkan@users.sourceforge.net>"
52 #define DRIVER_DESC "Pegasus/Pegasus II USB Ethernet driver"
53
54 static const char driver_name[] = "pegasus";
55
56 #undef  PEGASUS_WRITE_EEPROM
57 #define BMSR_MEDIA      (BMSR_10HALF | BMSR_10FULL | BMSR_100HALF | \
58                         BMSR_100FULL | BMSR_ANEGCAPABLE)
59
60 static int loopback = 0;
61 static int mii_mode = 0;
62
63 static struct usb_eth_dev usb_dev_id[] = {
64 #define PEGASUS_DEV(pn, vid, pid, flags)        \
65         {.name = pn, .vendor = vid, .device = pid, .private = flags},
66 #include "pegasus.h"
67 #undef  PEGASUS_DEV
68         {NULL, 0, 0, 0}
69 };
70
71 static struct usb_device_id pegasus_ids[] = {
72 #define PEGASUS_DEV(pn, vid, pid, flags) \
73         {.match_flags = USB_DEVICE_ID_MATCH_DEVICE, .idVendor = vid, .idProduct = pid},
74 #include "pegasus.h"
75 #undef  PEGASUS_DEV
76         {}
77 };
78
79 MODULE_AUTHOR(DRIVER_AUTHOR);
80 MODULE_DESCRIPTION(DRIVER_DESC);
81 MODULE_LICENSE("GPL");
82 module_param(loopback, bool, 0);
83 module_param(mii_mode, bool, 0);
84 MODULE_PARM_DESC(loopback, "Enable MAC loopback mode (bit 0)");
85 MODULE_PARM_DESC(mii_mode, "Enable HomePNA mode (bit 0),default=MII mode = 0");
86
87 /* use ethtool to change the level for any given device */
88 static int msg_level = -1;
89 module_param (msg_level, int, 0);
90 MODULE_PARM_DESC (msg_level, "Override default message level");
91
92 MODULE_DEVICE_TABLE(usb, pegasus_ids);
93
94 static int update_eth_regs_async(pegasus_t *);
95 /* Aargh!!! I _really_ hate such tweaks */
96 static void ctrl_callback(struct urb *urb, struct pt_regs *regs)
97 {
98         pegasus_t *pegasus = urb->context;
99
100         if (!pegasus)
101                 return;
102
103         switch (urb->status) {
104         case 0:
105                 if (pegasus->flags & ETH_REGS_CHANGE) {
106                         pegasus->flags &= ~ETH_REGS_CHANGE;
107                         pegasus->flags |= ETH_REGS_CHANGED;
108                         update_eth_regs_async(pegasus);
109                         return;
110                 }
111                 break;
112         case -EINPROGRESS:
113                 return;
114         case -ENOENT:
115                 break;
116         default:
117                 if (netif_msg_drv(pegasus))
118                         dev_err(&pegasus->intf->dev, "%s, status %d\n",
119                                 __FUNCTION__, urb->status);
120         }
121         pegasus->flags &= ~ETH_REGS_CHANGED;
122         wake_up(&pegasus->ctrl_wait);
123 }
124
125 static int get_registers(pegasus_t * pegasus, __u16 indx, __u16 size,
126                          void *data)
127 {
128         int ret;
129         char *buffer;
130         DECLARE_WAITQUEUE(wait, current);
131
132         buffer = kmalloc(size, GFP_KERNEL);
133         if (!buffer) {
134                 if (netif_msg_drv(pegasus))
135                         dev_warn(&pegasus->intf->dev, "out of memory in %s\n",
136                                         __FUNCTION__);
137                 return -ENOMEM;
138         }
139         add_wait_queue(&pegasus->ctrl_wait, &wait);
140         set_current_state(TASK_UNINTERRUPTIBLE);
141         while (pegasus->flags & ETH_REGS_CHANGED)
142                 schedule();
143         remove_wait_queue(&pegasus->ctrl_wait, &wait);
144         set_current_state(TASK_RUNNING);
145
146         pegasus->dr.bRequestType = PEGASUS_REQT_READ;
147         pegasus->dr.bRequest = PEGASUS_REQ_GET_REGS;
148         pegasus->dr.wValue = cpu_to_le16(0);
149         pegasus->dr.wIndex = cpu_to_le16p(&indx);
150         pegasus->dr.wLength = cpu_to_le16p(&size);
151         pegasus->ctrl_urb->transfer_buffer_length = size;
152
153         usb_fill_control_urb(pegasus->ctrl_urb, pegasus->usb,
154                              usb_rcvctrlpipe(pegasus->usb, 0),
155                              (char *) &pegasus->dr,
156                              buffer, size, ctrl_callback, pegasus);
157
158         add_wait_queue(&pegasus->ctrl_wait, &wait);
159         set_current_state(TASK_UNINTERRUPTIBLE);
160
161         /* using ATOMIC, we'd never wake up if we slept */
162         if ((ret = usb_submit_urb(pegasus->ctrl_urb, GFP_ATOMIC))) {
163                 if (netif_msg_drv(pegasus))
164                         dev_err(&pegasus->intf->dev, "%s, status %d\n",
165                                         __FUNCTION__, ret);
166                 goto out;
167         }
168
169         schedule();
170 out:
171         remove_wait_queue(&pegasus->ctrl_wait, &wait);
172         memcpy(data, buffer, size);
173         kfree(buffer);
174
175         return ret;
176 }
177
178 static int set_registers(pegasus_t * pegasus, __u16 indx, __u16 size,
179                          void *data)
180 {
181         int ret;
182         char *buffer;
183         DECLARE_WAITQUEUE(wait, current);
184
185         buffer = kmalloc(size, GFP_KERNEL);
186         if (!buffer) {
187                 if (netif_msg_drv(pegasus))
188                         dev_warn(&pegasus->intf->dev, "out of memory in %s\n",
189                                         __FUNCTION__);
190                 return -ENOMEM;
191         }
192         memcpy(buffer, data, size);
193
194         add_wait_queue(&pegasus->ctrl_wait, &wait);
195         set_current_state(TASK_UNINTERRUPTIBLE);
196         while (pegasus->flags & ETH_REGS_CHANGED)
197                 schedule();
198         remove_wait_queue(&pegasus->ctrl_wait, &wait);
199         set_current_state(TASK_RUNNING);
200
201         pegasus->dr.bRequestType = PEGASUS_REQT_WRITE;
202         pegasus->dr.bRequest = PEGASUS_REQ_SET_REGS;
203         pegasus->dr.wValue = cpu_to_le16(0);
204         pegasus->dr.wIndex = cpu_to_le16p(&indx);
205         pegasus->dr.wLength = cpu_to_le16p(&size);
206         pegasus->ctrl_urb->transfer_buffer_length = size;
207
208         usb_fill_control_urb(pegasus->ctrl_urb, pegasus->usb,
209                              usb_sndctrlpipe(pegasus->usb, 0),
210                              (char *) &pegasus->dr,
211                              buffer, size, ctrl_callback, pegasus);
212
213         add_wait_queue(&pegasus->ctrl_wait, &wait);
214         set_current_state(TASK_UNINTERRUPTIBLE);
215
216         if ((ret = usb_submit_urb(pegasus->ctrl_urb, GFP_ATOMIC))) {
217                 if (netif_msg_drv(pegasus))
218                         dev_err(&pegasus->intf->dev, "%s, status %d\n",
219                                         __FUNCTION__, ret);
220                 goto out;
221         }
222
223         schedule();
224 out:
225         remove_wait_queue(&pegasus->ctrl_wait, &wait);
226         kfree(buffer);
227
228         return ret;
229 }
230
231 static int set_register(pegasus_t * pegasus, __u16 indx, __u8 data)
232 {
233         int ret;
234         char *tmp;
235         DECLARE_WAITQUEUE(wait, current);
236
237         tmp = kmalloc(1, GFP_KERNEL);
238         if (!tmp) {
239                 if (netif_msg_drv(pegasus))
240                         dev_warn(&pegasus->intf->dev, "out of memory in %s\n",
241                                         __FUNCTION__);
242                 return -ENOMEM;
243         }
244         memcpy(tmp, &data, 1);
245         add_wait_queue(&pegasus->ctrl_wait, &wait);
246         set_current_state(TASK_UNINTERRUPTIBLE);
247         while (pegasus->flags & ETH_REGS_CHANGED)
248                 schedule();
249         remove_wait_queue(&pegasus->ctrl_wait, &wait);
250         set_current_state(TASK_RUNNING);
251
252         pegasus->dr.bRequestType = PEGASUS_REQT_WRITE;
253         pegasus->dr.bRequest = PEGASUS_REQ_SET_REG;
254         pegasus->dr.wValue = cpu_to_le16(data);
255         pegasus->dr.wIndex = cpu_to_le16p(&indx);
256         pegasus->dr.wLength = cpu_to_le16(1);
257         pegasus->ctrl_urb->transfer_buffer_length = 1;
258
259         usb_fill_control_urb(pegasus->ctrl_urb, pegasus->usb,
260                              usb_sndctrlpipe(pegasus->usb, 0),
261                              (char *) &pegasus->dr,
262                              &tmp, 1, ctrl_callback, pegasus);
263
264         add_wait_queue(&pegasus->ctrl_wait, &wait);
265         set_current_state(TASK_UNINTERRUPTIBLE);
266
267         if ((ret = usb_submit_urb(pegasus->ctrl_urb, GFP_ATOMIC))) {
268                 if (netif_msg_drv(pegasus))
269                         dev_err(&pegasus->intf->dev, "%s, status %d\n",
270                                         __FUNCTION__, ret);
271                 goto out;
272         }
273
274         schedule();
275 out:
276         remove_wait_queue(&pegasus->ctrl_wait, &wait);
277         kfree(tmp);
278
279         return ret;
280 }
281
282 static int update_eth_regs_async(pegasus_t * pegasus)
283 {
284         int ret;
285
286         pegasus->dr.bRequestType = PEGASUS_REQT_WRITE;
287         pegasus->dr.bRequest = PEGASUS_REQ_SET_REGS;
288         pegasus->dr.wValue = 0;
289         pegasus->dr.wIndex = cpu_to_le16(EthCtrl0);
290         pegasus->dr.wLength = cpu_to_le16(3);
291         pegasus->ctrl_urb->transfer_buffer_length = 3;
292
293         usb_fill_control_urb(pegasus->ctrl_urb, pegasus->usb,
294                              usb_sndctrlpipe(pegasus->usb, 0),
295                              (char *) &pegasus->dr,
296                              pegasus->eth_regs, 3, ctrl_callback, pegasus);
297
298         if ((ret = usb_submit_urb(pegasus->ctrl_urb, GFP_ATOMIC)))
299                 if (netif_msg_drv(pegasus))
300                         dev_err(&pegasus->intf->dev, "%s, status %d\n",
301                                         __FUNCTION__, ret);
302
303         return ret;
304 }
305
306 static int read_mii_word(pegasus_t * pegasus, __u8 phy, __u8 indx, __u16 * regd)
307 {
308         int i;
309         __u8 data[4] = { phy, 0, 0, indx };
310         __le16 regdi;
311         int ret;
312
313         ret = set_register(pegasus, PhyCtrl, 0);
314         ret = set_registers(pegasus, PhyAddr, sizeof (data), data);
315         ret = set_register(pegasus, PhyCtrl, (indx | PHY_READ));
316         for (i = 0; i < REG_TIMEOUT; i++) {
317                 ret = get_registers(pegasus, PhyCtrl, 1, data);
318                 if (data[0] & PHY_DONE)
319                         break;
320         }
321         if (i < REG_TIMEOUT) {
322                 ret = get_registers(pegasus, PhyData, 2, &regdi);
323                 *regd = le16_to_cpu(regdi);
324                 return 1;
325         }
326         if (netif_msg_drv(pegasus))
327                 dev_warn(&pegasus->intf->dev, "fail %s\n", __FUNCTION__);
328
329         return 0;
330 }
331
332 static int mdio_read(struct net_device *dev, int phy_id, int loc)
333 {
334         pegasus_t *pegasus = (pegasus_t *) netdev_priv(dev);
335         u16 res;
336
337         read_mii_word(pegasus, phy_id, loc, &res);
338         return (int)res;
339 }
340
341 static int write_mii_word(pegasus_t * pegasus, __u8 phy, __u8 indx, __u16 regd)
342 {
343         int i;
344         __u8 data[4] = { phy, 0, 0, indx };
345         int ret;
346
347         data[1] = (u8) regd;
348         data[2] = (u8) (regd >> 8);
349         ret = set_register(pegasus, PhyCtrl, 0);
350         ret = set_registers(pegasus, PhyAddr, sizeof(data), data);
351         ret = set_register(pegasus, PhyCtrl, (indx | PHY_WRITE));
352         for (i = 0; i < REG_TIMEOUT; i++) {
353                 ret = get_registers(pegasus, PhyCtrl, 1, data);
354                 if (data[0] & PHY_DONE)
355                         break;
356         }
357         if (i < REG_TIMEOUT)
358                 return 0;
359
360         if (netif_msg_drv(pegasus))
361                 dev_warn(&pegasus->intf->dev, "fail %s\n", __FUNCTION__);
362         return 1;
363 }
364
365 static void mdio_write(struct net_device *dev, int phy_id, int loc, int val)
366 {
367         pegasus_t *pegasus = (pegasus_t *) netdev_priv(dev);
368
369         write_mii_word(pegasus, phy_id, loc, val);
370 }
371
372 static int read_eprom_word(pegasus_t * pegasus, __u8 index, __u16 * retdata)
373 {
374         int i;
375         __u8 tmp;
376         __le16 retdatai;
377         int ret;
378
379         ret = set_register(pegasus, EpromCtrl, 0);
380         ret = set_register(pegasus, EpromOffset, index);
381         ret = set_register(pegasus, EpromCtrl, EPROM_READ);
382
383         for (i = 0; i < REG_TIMEOUT; i++) {
384                 ret = get_registers(pegasus, EpromCtrl, 1, &tmp);
385                 if (tmp & EPROM_DONE)
386                         break;
387         }
388         if (i < REG_TIMEOUT) {
389                 ret = get_registers(pegasus, EpromData, 2, &retdatai);
390                 *retdata = le16_to_cpu(retdatai);
391                 return 0;
392         }
393
394         if (netif_msg_drv(pegasus))
395                 dev_warn(&pegasus->intf->dev, "fail %s\n", __FUNCTION__);
396         return -1;
397 }
398
399 #ifdef  PEGASUS_WRITE_EEPROM
400 static inline void enable_eprom_write(pegasus_t * pegasus)
401 {
402         __u8 tmp;
403         int ret;
404
405         ret = get_registers(pegasus, EthCtrl2, 1, &tmp);
406         ret = set_register(pegasus, EthCtrl2, tmp | EPROM_WR_ENABLE);
407 }
408
409 static inline void disable_eprom_write(pegasus_t * pegasus)
410 {
411         __u8 tmp;
412         int ret;
413
414         ret = get_registers(pegasus, EthCtrl2, 1, &tmp);
415         ret = set_register(pegasus, EpromCtrl, 0);
416         ret = set_register(pegasus, EthCtrl2, tmp & ~EPROM_WR_ENABLE);
417 }
418
419 static int write_eprom_word(pegasus_t * pegasus, __u8 index, __u16 data)
420 {
421         int i;
422         __u8 tmp, d[4] = { 0x3f, 0, 0, EPROM_WRITE };
423         int ret;
424
425         ret = set_registers(pegasus, EpromOffset, 4, d);
426         enable_eprom_write(pegasus);
427         ret = set_register(pegasus, EpromOffset, index);
428         ret = set_registers(pegasus, EpromData, 2, &data);
429         ret = set_register(pegasus, EpromCtrl, EPROM_WRITE);
430
431         for (i = 0; i < REG_TIMEOUT; i++) {
432                 ret = get_registers(pegasus, EpromCtrl, 1, &tmp);
433                 if (tmp & EPROM_DONE)
434                         break;
435         }
436         disable_eprom_write(pegasus);
437         if (i < REG_TIMEOUT)
438                 return 0;
439         if (netif_msg_drv(pegasus))
440                 dev_warn(&pegasus->intf->dev, "fail %s\n", __FUNCTION__);
441         return -1;
442 }
443 #endif                          /* PEGASUS_WRITE_EEPROM */
444
445 static inline void get_node_id(pegasus_t * pegasus, __u8 * id)
446 {
447         int i;
448         __u16 w16;
449
450         for (i = 0; i < 3; i++) {
451                 read_eprom_word(pegasus, i, &w16);
452                 ((__le16 *) id)[i] = cpu_to_le16p(&w16);
453         }
454 }
455
456 static void set_ethernet_addr(pegasus_t * pegasus)
457 {
458         __u8 node_id[6];
459         int ret;
460
461         get_node_id(pegasus, node_id);
462         ret = set_registers(pegasus, EthID, sizeof (node_id), node_id);
463         memcpy(pegasus->net->dev_addr, node_id, sizeof (node_id));
464 }
465
466 static inline int reset_mac(pegasus_t * pegasus)
467 {
468         __u8 data = 0x8;
469         int i;
470         int ret;
471
472         ret = set_register(pegasus, EthCtrl1, data);
473         for (i = 0; i < REG_TIMEOUT; i++) {
474                 ret = get_registers(pegasus, EthCtrl1, 1, &data);
475                 if (~data & 0x08) {
476                         if (loopback & 1)
477                                 break;
478                         if (mii_mode && (pegasus->features & HAS_HOME_PNA))
479                                 ret = set_register(pegasus, Gpio1, 0x34);
480                         else
481                                 ret = set_register(pegasus, Gpio1, 0x26);
482                         ret = set_register(pegasus, Gpio0, pegasus->features);
483                         ret = set_register(pegasus, Gpio0, DEFAULT_GPIO_SET);
484                         break;
485                 }
486         }
487         if (i == REG_TIMEOUT)
488                 return 1;
489
490         if (usb_dev_id[pegasus->dev_index].vendor == VENDOR_LINKSYS ||
491             usb_dev_id[pegasus->dev_index].vendor == VENDOR_DLINK) {
492                 ret = set_register(pegasus, Gpio0, 0x24);
493                 ret = set_register(pegasus, Gpio0, 0x26);
494         }
495         if (usb_dev_id[pegasus->dev_index].vendor == VENDOR_ELCON) {
496                 __u16 auxmode;
497                 read_mii_word(pegasus, 3, 0x1b, &auxmode);
498                 write_mii_word(pegasus, 3, 0x1b, auxmode | 4);
499         }
500
501         return 0;
502 }
503
504 static int enable_net_traffic(struct net_device *dev, struct usb_device *usb)
505 {
506         __u16 linkpart;
507         __u8 data[4];
508         pegasus_t *pegasus = netdev_priv(dev);
509         int ret;
510
511         read_mii_word(pegasus, pegasus->phy, MII_LPA, &linkpart);
512         data[0] = 0xc9;
513         data[1] = 0;
514         if (linkpart & (ADVERTISE_100FULL | ADVERTISE_10FULL))
515                 data[1] |= 0x20;        /* set full duplex */
516         if (linkpart & (ADVERTISE_100FULL | ADVERTISE_100HALF))
517                 data[1] |= 0x10;        /* set 100 Mbps */
518         if (mii_mode)
519                 data[1] = 0;
520         data[2] = (loopback & 1) ? 0x09 : 0x01;
521
522         memcpy(pegasus->eth_regs, data, sizeof (data));
523         ret = set_registers(pegasus, EthCtrl0, 3, data);
524
525         if (usb_dev_id[pegasus->dev_index].vendor == VENDOR_LINKSYS ||
526             usb_dev_id[pegasus->dev_index].vendor == VENDOR_DLINK) {
527                 u16 auxmode;
528                 read_mii_word(pegasus, 0, 0x1b, &auxmode);
529                 write_mii_word(pegasus, 0, 0x1b, auxmode | 4);
530         }
531
532         return 0;
533 }
534
535 static void fill_skb_pool(pegasus_t * pegasus)
536 {
537         int i;
538
539         for (i = 0; i < RX_SKBS; i++) {
540                 if (pegasus->rx_pool[i])
541                         continue;
542                 pegasus->rx_pool[i] = dev_alloc_skb(PEGASUS_MTU + 2);
543                 /*
544                  ** we give up if the allocation fail. the tasklet will be
545                  ** rescheduled again anyway...
546                  */
547                 if (pegasus->rx_pool[i] == NULL)
548                         return;
549                 pegasus->rx_pool[i]->dev = pegasus->net;
550                 skb_reserve(pegasus->rx_pool[i], 2);
551         }
552 }
553
554 static void free_skb_pool(pegasus_t * pegasus)
555 {
556         int i;
557
558         for (i = 0; i < RX_SKBS; i++) {
559                 if (pegasus->rx_pool[i]) {
560                         dev_kfree_skb(pegasus->rx_pool[i]);
561                         pegasus->rx_pool[i] = NULL;
562                 }
563         }
564 }
565
566 static inline struct sk_buff *pull_skb(pegasus_t * pegasus)
567 {
568         int i;
569         struct sk_buff *skb;
570
571         for (i = 0; i < RX_SKBS; i++) {
572                 if (likely(pegasus->rx_pool[i] != NULL)) {
573                         skb = pegasus->rx_pool[i];
574                         pegasus->rx_pool[i] = NULL;
575                         return skb;
576                 }
577         }
578         return NULL;
579 }
580
581 static void read_bulk_callback(struct urb *urb, struct pt_regs *regs)
582 {
583         pegasus_t *pegasus = urb->context;
584         struct net_device *net;
585         int rx_status, count = urb->actual_length;
586         u8 *buf = urb->transfer_buffer;
587         __u16 pkt_len;
588
589         if (!pegasus)
590                 return;
591
592         net = pegasus->net;
593         if (!netif_device_present(net) || !netif_running(net))
594                 return;
595
596         switch (urb->status) {
597         case 0:
598                 break;
599         case -ETIMEDOUT:
600                 if (netif_msg_rx_err(pegasus))
601                         pr_debug("%s: reset MAC\n", net->name);
602                 pegasus->flags &= ~PEGASUS_RX_BUSY;
603                 break;
604         case -EPIPE:            /* stall, or disconnect from TT */
605                 /* FIXME schedule work to clear the halt */
606                 if (netif_msg_rx_err(pegasus))
607                         printk(KERN_WARNING "%s: no rx stall recovery\n",
608                                         net->name);
609                 return;
610         case -ENOENT:
611         case -ECONNRESET:
612         case -ESHUTDOWN:
613                 if (netif_msg_ifdown(pegasus))
614                         pr_debug("%s: rx unlink, %d\n", net->name, urb->status);
615                 return;
616         default:
617                 if (netif_msg_rx_err(pegasus))
618                         pr_debug("%s: RX status %d\n", net->name, urb->status);
619                 goto goon;
620         }
621
622         if (!count || count < 4)
623                 goto goon;
624
625         rx_status = buf[count - 2];
626         if (rx_status & 0x1e) {
627                 if (netif_msg_rx_err(pegasus))
628                         pr_debug("%s: RX packet error %x\n",
629                                         net->name, rx_status);
630                 pegasus->stats.rx_errors++;
631                 if (rx_status & 0x06)   // long or runt
632                         pegasus->stats.rx_length_errors++;
633                 if (rx_status & 0x08)
634                         pegasus->stats.rx_crc_errors++;
635                 if (rx_status & 0x10)   // extra bits
636                         pegasus->stats.rx_frame_errors++;
637                 goto goon;
638         }
639         if (pegasus->chip == 0x8513) {
640                 pkt_len = le32_to_cpu(*(__le32 *)urb->transfer_buffer);
641                 pkt_len &= 0x0fff;
642                 pegasus->rx_skb->data += 2;
643         } else {
644                 pkt_len = buf[count - 3] << 8;
645                 pkt_len += buf[count - 4];
646                 pkt_len &= 0xfff;
647                 pkt_len -= 8;
648         }
649
650         /*
651          * If the packet is unreasonably long, quietly drop it rather than
652          * kernel panicing by calling skb_put.
653          */
654         if (pkt_len > PEGASUS_MTU)
655                 goto goon;
656
657         /*
658          * at this point we are sure pegasus->rx_skb != NULL
659          * so we go ahead and pass up the packet.
660          */
661         skb_put(pegasus->rx_skb, pkt_len);
662         pegasus->rx_skb->protocol = eth_type_trans(pegasus->rx_skb, net);
663         netif_rx(pegasus->rx_skb);
664         pegasus->stats.rx_packets++;
665         pegasus->stats.rx_bytes += pkt_len;
666
667         if (pegasus->flags & PEGASUS_UNPLUG)
668                 return;
669
670         spin_lock(&pegasus->rx_pool_lock);
671         pegasus->rx_skb = pull_skb(pegasus);
672         spin_unlock(&pegasus->rx_pool_lock);
673
674         if (pegasus->rx_skb == NULL)
675                 goto tl_sched;
676 goon:
677         usb_fill_bulk_urb(pegasus->rx_urb, pegasus->usb,
678                           usb_rcvbulkpipe(pegasus->usb, 1),
679                           pegasus->rx_skb->data, PEGASUS_MTU + 8,
680                           read_bulk_callback, pegasus);
681         if (usb_submit_urb(pegasus->rx_urb, GFP_ATOMIC)) {
682                 pegasus->flags |= PEGASUS_RX_URB_FAIL;
683                 goto tl_sched;
684         } else {
685                 pegasus->flags &= ~PEGASUS_RX_URB_FAIL;
686         }
687
688         return;
689
690 tl_sched:
691         tasklet_schedule(&pegasus->rx_tl);
692 }
693
694 static void rx_fixup(unsigned long data)
695 {
696         pegasus_t *pegasus;
697         unsigned long flags;
698
699         pegasus = (pegasus_t *) data;
700         if (pegasus->flags & PEGASUS_UNPLUG)
701                 return;
702
703         spin_lock_irqsave(&pegasus->rx_pool_lock, flags);
704         fill_skb_pool(pegasus);
705         if (pegasus->flags & PEGASUS_RX_URB_FAIL)
706                 if (pegasus->rx_skb)
707                         goto try_again;
708         if (pegasus->rx_skb == NULL) {
709                 pegasus->rx_skb = pull_skb(pegasus);
710         }
711         if (pegasus->rx_skb == NULL) {
712                 if (netif_msg_rx_err(pegasus))
713                         printk(KERN_WARNING "%s: low on memory\n",
714                                         pegasus->net->name);
715                 tasklet_schedule(&pegasus->rx_tl);
716                 goto done;
717         }
718         usb_fill_bulk_urb(pegasus->rx_urb, pegasus->usb,
719                           usb_rcvbulkpipe(pegasus->usb, 1),
720                           pegasus->rx_skb->data, PEGASUS_MTU + 8,
721                           read_bulk_callback, pegasus);
722 try_again:
723         if (usb_submit_urb(pegasus->rx_urb, GFP_ATOMIC)) {
724                 pegasus->flags |= PEGASUS_RX_URB_FAIL;
725                 tasklet_schedule(&pegasus->rx_tl);
726         } else {
727                 pegasus->flags &= ~PEGASUS_RX_URB_FAIL;
728         }
729 done:
730         spin_unlock_irqrestore(&pegasus->rx_pool_lock, flags);
731 }
732
733 static void write_bulk_callback(struct urb *urb, struct pt_regs *regs)
734 {
735         pegasus_t *pegasus = urb->context;
736         struct net_device *net = pegasus->net;
737
738         if (!pegasus)
739                 return;
740
741         if (!netif_device_present(net) || !netif_running(net))
742                 return;
743
744         switch (urb->status) {
745         case -EPIPE:
746                 /* FIXME schedule_work() to clear the tx halt */
747                 netif_stop_queue(net);
748                 if (netif_msg_tx_err(pegasus))
749                         printk(KERN_WARNING "%s: no tx stall recovery\n",
750                                         net->name);
751                 return;
752         case -ENOENT:
753         case -ECONNRESET:
754         case -ESHUTDOWN:
755                 if (netif_msg_ifdown(pegasus))
756                         pr_debug("%s: tx unlink, %d\n", net->name, urb->status);
757                 return;
758         default:
759                 if (netif_msg_tx_err(pegasus))
760                         pr_info("%s: TX status %d\n", net->name, urb->status);
761                 /* FALL THROUGH */
762         case 0:
763                 break;
764         }
765
766         net->trans_start = jiffies;
767         netif_wake_queue(net);
768 }
769
770 static void intr_callback(struct urb *urb, struct pt_regs *regs)
771 {
772         pegasus_t *pegasus = urb->context;
773         struct net_device *net;
774         int status;
775
776         if (!pegasus)
777                 return;
778         net = pegasus->net;
779
780         switch (urb->status) {
781         case 0:
782                 break;
783         case -ECONNRESET:       /* unlink */
784         case -ENOENT:
785         case -ESHUTDOWN:
786                 return;
787         default:
788                 /* some Pegasus-I products report LOTS of data
789                  * toggle errors... avoid log spamming
790                  */
791                 if (netif_msg_timer(pegasus))
792                         pr_debug("%s: intr status %d\n", net->name,
793                                         urb->status);
794         }
795
796         if (urb->actual_length >= 6) {
797                 u8      * d = urb->transfer_buffer;
798
799                 /* byte 0 == tx_status1, reg 2B */
800                 if (d[0] & (TX_UNDERRUN|EXCESSIVE_COL
801                                         |LATE_COL|JABBER_TIMEOUT)) {
802                         pegasus->stats.tx_errors++;
803                         if (d[0] & TX_UNDERRUN)
804                                 pegasus->stats.tx_fifo_errors++;
805                         if (d[0] & (EXCESSIVE_COL | JABBER_TIMEOUT))
806                                 pegasus->stats.tx_aborted_errors++;
807                         if (d[0] & LATE_COL)
808                                 pegasus->stats.tx_window_errors++;
809                 }
810
811                 /* d[5].LINK_STATUS lies on some adapters.
812                  * d[0].NO_CARRIER kicks in only with failed TX.
813                  * ... so monitoring with MII may be safest.
814                  */
815                 if (d[0] & NO_CARRIER)
816                         netif_carrier_off(net); 
817                 else
818                         netif_carrier_on(net);
819
820                 /* bytes 3-4 == rx_lostpkt, reg 2E/2F */
821                 pegasus->stats.rx_missed_errors += ((d[3] & 0x7f) << 8) | d[4];
822         }
823
824         status = usb_submit_urb(urb, SLAB_ATOMIC);
825         if (status && netif_msg_timer(pegasus))
826                 printk(KERN_ERR "%s: can't resubmit interrupt urb, %d\n",
827                                 net->name, status);
828 }
829
830 static void pegasus_tx_timeout(struct net_device *net)
831 {
832         pegasus_t *pegasus = netdev_priv(net);
833         if (netif_msg_timer(pegasus))
834                 printk(KERN_WARNING "%s: tx timeout\n", net->name);
835         usb_unlink_urb(pegasus->tx_urb);
836         pegasus->stats.tx_errors++;
837 }
838
839 static int pegasus_start_xmit(struct sk_buff *skb, struct net_device *net)
840 {
841         pegasus_t *pegasus = netdev_priv(net);
842         int count = ((skb->len + 2) & 0x3f) ? skb->len + 2 : skb->len + 3;
843         int res;
844         __u16 l16 = skb->len;
845
846         netif_stop_queue(net);
847
848         ((__le16 *) pegasus->tx_buff)[0] = cpu_to_le16(l16);
849         memcpy(pegasus->tx_buff + 2, skb->data, skb->len);
850         usb_fill_bulk_urb(pegasus->tx_urb, pegasus->usb,
851                           usb_sndbulkpipe(pegasus->usb, 2),
852                           pegasus->tx_buff, count,
853                           write_bulk_callback, pegasus);
854         if ((res = usb_submit_urb(pegasus->tx_urb, GFP_ATOMIC))) {
855                 if (netif_msg_tx_err(pegasus))
856                         printk(KERN_WARNING "%s: fail tx, %d\n",
857                                         net->name, res);
858                 switch (res) {
859                 case -EPIPE:            /* stall, or disconnect from TT */
860                         /* cleanup should already have been scheduled */
861                         break;
862                 case -ENODEV:           /* disconnect() upcoming */
863                         break;
864                 default:
865                         pegasus->stats.tx_errors++;
866                         netif_start_queue(net);
867                 }
868         } else {
869                 pegasus->stats.tx_packets++;
870                 pegasus->stats.tx_bytes += skb->len;
871                 net->trans_start = jiffies;
872         }
873         dev_kfree_skb(skb);
874
875         return 0;
876 }
877
878 static struct net_device_stats *pegasus_netdev_stats(struct net_device *dev)
879 {
880         return &((pegasus_t *) netdev_priv(dev))->stats;
881 }
882
883 static inline void disable_net_traffic(pegasus_t * pegasus)
884 {
885         int tmp = 0;
886         int ret;
887
888         ret = set_registers(pegasus, EthCtrl0, 2, &tmp);
889 }
890
891 static inline void get_interrupt_interval(pegasus_t * pegasus)
892 {
893         __u8 data[2];
894
895         read_eprom_word(pegasus, 4, (__u16 *) data);
896         if (pegasus->usb->speed != USB_SPEED_HIGH) {
897                 if (data[1] < 0x80) {
898                         if (netif_msg_timer(pegasus))
899                                 dev_info(&pegasus->intf->dev, "intr interval "
900                                         "changed from %ums to %ums\n",
901                                         data[1], 0x80);
902                         data[1] = 0x80;
903 #ifdef PEGASUS_WRITE_EEPROM
904                         write_eprom_word(pegasus, 4, *(__u16 *) data);
905 #endif
906                 }
907         }
908         pegasus->intr_interval = data[1];
909 }
910
911 static void set_carrier(struct net_device *net)
912 {
913         pegasus_t *pegasus = netdev_priv(net);
914         u16 tmp;
915
916         if (!read_mii_word(pegasus, pegasus->phy, MII_BMSR, &tmp))
917                 return;
918
919         if (tmp & BMSR_LSTATUS)
920                 netif_carrier_on(net);
921         else
922                 netif_carrier_off(net);
923 }
924
925 static void free_all_urbs(pegasus_t * pegasus)
926 {
927         usb_free_urb(pegasus->intr_urb);
928         usb_free_urb(pegasus->tx_urb);
929         usb_free_urb(pegasus->rx_urb);
930         usb_free_urb(pegasus->ctrl_urb);
931 }
932
933 static void unlink_all_urbs(pegasus_t * pegasus)
934 {
935         usb_kill_urb(pegasus->intr_urb);
936         usb_kill_urb(pegasus->tx_urb);
937         usb_kill_urb(pegasus->rx_urb);
938         usb_kill_urb(pegasus->ctrl_urb);
939 }
940
941 static int alloc_urbs(pegasus_t * pegasus)
942 {
943         pegasus->ctrl_urb = usb_alloc_urb(0, GFP_KERNEL);
944         if (!pegasus->ctrl_urb) {
945                 return 0;
946         }
947         pegasus->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
948         if (!pegasus->rx_urb) {
949                 usb_free_urb(pegasus->ctrl_urb);
950                 return 0;
951         }
952         pegasus->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
953         if (!pegasus->tx_urb) {
954                 usb_free_urb(pegasus->rx_urb);
955                 usb_free_urb(pegasus->ctrl_urb);
956                 return 0;
957         }
958         pegasus->intr_urb = usb_alloc_urb(0, GFP_KERNEL);
959         if (!pegasus->intr_urb) {
960                 usb_free_urb(pegasus->tx_urb);
961                 usb_free_urb(pegasus->rx_urb);
962                 usb_free_urb(pegasus->ctrl_urb);
963                 return 0;
964         }
965
966         return 1;
967 }
968
969 static int pegasus_open(struct net_device *net)
970 {
971         pegasus_t *pegasus = netdev_priv(net);
972         int res;
973
974         if (pegasus->rx_skb == NULL)
975                 pegasus->rx_skb = pull_skb(pegasus);
976         /*
977          ** Note: no point to free the pool.  it is empty :-)
978          */
979         if (!pegasus->rx_skb)
980                 return -ENOMEM;
981
982         res = set_registers(pegasus, EthID, 6, net->dev_addr);
983         
984         usb_fill_bulk_urb(pegasus->rx_urb, pegasus->usb,
985                           usb_rcvbulkpipe(pegasus->usb, 1),
986                           pegasus->rx_skb->data, PEGASUS_MTU + 8,
987                           read_bulk_callback, pegasus);
988         if ((res = usb_submit_urb(pegasus->rx_urb, GFP_KERNEL))) {
989                 if (netif_msg_ifup(pegasus))
990                         pr_debug("%s: failed rx_urb, %d", net->name, res);
991                 goto exit;
992         }
993
994         usb_fill_int_urb(pegasus->intr_urb, pegasus->usb,
995                          usb_rcvintpipe(pegasus->usb, 3),
996                          pegasus->intr_buff, sizeof (pegasus->intr_buff),
997                          intr_callback, pegasus, pegasus->intr_interval);
998         if ((res = usb_submit_urb(pegasus->intr_urb, GFP_KERNEL))) {
999                 if (netif_msg_ifup(pegasus))
1000                         pr_debug("%s: failed intr_urb, %d\n", net->name, res);
1001                 usb_kill_urb(pegasus->rx_urb);
1002                 goto exit;
1003         }
1004         if ((res = enable_net_traffic(net, pegasus->usb))) {
1005                 if (netif_msg_ifup(pegasus))
1006                         pr_debug("%s: can't enable_net_traffic() - %d\n",
1007                                         net->name, res);
1008                 res = -EIO;
1009                 usb_kill_urb(pegasus->rx_urb);
1010                 usb_kill_urb(pegasus->intr_urb);
1011                 free_skb_pool(pegasus);
1012                 goto exit;
1013         }
1014         set_carrier(net);
1015         netif_start_queue(net);
1016         if (netif_msg_ifup(pegasus))
1017                 pr_debug("%s: open\n", net->name);
1018         res = 0;
1019 exit:
1020         return res;
1021 }
1022
1023 static int pegasus_close(struct net_device *net)
1024 {
1025         pegasus_t *pegasus = netdev_priv(net);
1026
1027         netif_stop_queue(net);
1028         if (!(pegasus->flags & PEGASUS_UNPLUG))
1029                 disable_net_traffic(pegasus);
1030         tasklet_kill(&pegasus->rx_tl);
1031         unlink_all_urbs(pegasus);
1032
1033         return 0;
1034 }
1035
1036 static void pegasus_get_drvinfo(struct net_device *dev,
1037                                 struct ethtool_drvinfo *info)
1038 {
1039         pegasus_t *pegasus = netdev_priv(dev);
1040         strncpy(info->driver, driver_name, sizeof (info->driver) - 1);
1041         strncpy(info->version, DRIVER_VERSION, sizeof (info->version) - 1);
1042         usb_make_path(pegasus->usb, info->bus_info, sizeof (info->bus_info));
1043 }
1044
1045 /* also handles three patterns of some kind in hardware */
1046 #define WOL_SUPPORTED   (WAKE_MAGIC|WAKE_PHY)
1047
1048 static void
1049 pegasus_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1050 {
1051         pegasus_t       *pegasus = netdev_priv(dev);
1052
1053         wol->supported = WAKE_MAGIC | WAKE_PHY;
1054         wol->wolopts = pegasus->wolopts;
1055 }
1056
1057 static int
1058 pegasus_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1059 {
1060         pegasus_t       *pegasus = netdev_priv(dev);
1061         u8              reg78 = 0x04;
1062         
1063         if (wol->wolopts & ~WOL_SUPPORTED)
1064                 return -EINVAL;
1065
1066         if (wol->wolopts & WAKE_MAGIC)
1067                 reg78 |= 0x80;
1068         if (wol->wolopts & WAKE_PHY)
1069                 reg78 |= 0x40;
1070         /* FIXME this 0x10 bit still needs to get set in the chip... */
1071         if (wol->wolopts)
1072                 pegasus->eth_regs[0] |= 0x10;
1073         else
1074                 pegasus->eth_regs[0] &= ~0x10;
1075         pegasus->wolopts = wol->wolopts;
1076         return set_register(pegasus, WakeupControl, reg78);
1077 }
1078
1079 static inline void pegasus_reset_wol(struct net_device *dev)
1080 {
1081         struct ethtool_wolinfo wol;
1082         
1083         memset(&wol, 0, sizeof wol);
1084         (void) pegasus_set_wol(dev, &wol);
1085 }
1086
1087 static int
1088 pegasus_get_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
1089 {
1090         pegasus_t *pegasus;
1091
1092         if (in_atomic())
1093                 return 0;
1094
1095         pegasus = netdev_priv(dev);
1096         mii_ethtool_gset(&pegasus->mii, ecmd);
1097
1098         return 0;
1099 }
1100
1101 static int
1102 pegasus_set_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
1103 {
1104         pegasus_t *pegasus = netdev_priv(dev);
1105         return mii_ethtool_sset(&pegasus->mii, ecmd);
1106 }
1107
1108 static int pegasus_nway_reset(struct net_device *dev)
1109 {
1110         pegasus_t *pegasus = netdev_priv(dev);
1111         return mii_nway_restart(&pegasus->mii);
1112 }
1113
1114 static u32 pegasus_get_link(struct net_device *dev)
1115 {
1116         pegasus_t *pegasus = netdev_priv(dev);
1117         return mii_link_ok(&pegasus->mii);
1118 }
1119
1120 static u32 pegasus_get_msglevel(struct net_device *dev)
1121 {
1122         pegasus_t *pegasus = netdev_priv(dev);
1123         return pegasus->msg_enable;
1124 }
1125
1126 static void pegasus_set_msglevel(struct net_device *dev, u32 v)
1127 {
1128         pegasus_t *pegasus = netdev_priv(dev);
1129         pegasus->msg_enable = v;
1130 }
1131
1132 static struct ethtool_ops ops = {
1133         .get_drvinfo = pegasus_get_drvinfo,
1134         .get_settings = pegasus_get_settings,
1135         .set_settings = pegasus_set_settings,
1136         .nway_reset = pegasus_nway_reset,
1137         .get_link = pegasus_get_link,
1138         .get_msglevel = pegasus_get_msglevel,
1139         .set_msglevel = pegasus_set_msglevel,
1140         .get_wol = pegasus_get_wol,
1141         .set_wol = pegasus_set_wol,
1142 };
1143
1144 static int pegasus_ioctl(struct net_device *net, struct ifreq *rq, int cmd)
1145 {
1146         __u16 *data = (__u16 *) & rq->ifr_ifru;
1147         pegasus_t *pegasus = netdev_priv(net);
1148         int res;
1149
1150         switch (cmd) {
1151         case SIOCDEVPRIVATE:
1152                 data[0] = pegasus->phy;
1153         case SIOCDEVPRIVATE + 1:
1154                 read_mii_word(pegasus, data[0], data[1] & 0x1f, &data[3]);
1155                 res = 0;
1156                 break;
1157         case SIOCDEVPRIVATE + 2:
1158                 if (!capable(CAP_NET_ADMIN))
1159                         return -EPERM;
1160                 write_mii_word(pegasus, pegasus->phy, data[1] & 0x1f, data[2]);
1161                 res = 0;
1162                 break;
1163         default:
1164                 res = -EOPNOTSUPP;
1165         }
1166         return res;
1167 }
1168
1169 static void pegasus_set_multicast(struct net_device *net)
1170 {
1171         pegasus_t *pegasus = netdev_priv(net);
1172
1173         if (net->flags & IFF_PROMISC) {
1174                 pegasus->eth_regs[EthCtrl2] |= RX_PROMISCUOUS;
1175                 if (netif_msg_link(pegasus))
1176                         pr_info("%s: Promiscuous mode enabled.\n", net->name);
1177         } else if (net->mc_count ||
1178                    (net->flags & IFF_ALLMULTI)) {
1179                 pegasus->eth_regs[EthCtrl0] |= RX_MULTICAST;
1180                 pegasus->eth_regs[EthCtrl2] &= ~RX_PROMISCUOUS;
1181                 if (netif_msg_link(pegasus))
1182                         pr_info("%s: set allmulti\n", net->name);
1183         } else {
1184                 pegasus->eth_regs[EthCtrl0] &= ~RX_MULTICAST;
1185                 pegasus->eth_regs[EthCtrl2] &= ~RX_PROMISCUOUS;
1186         }
1187
1188         pegasus->flags |= ETH_REGS_CHANGE;
1189         ctrl_callback(pegasus->ctrl_urb, NULL);
1190 }
1191
1192 static __u8 mii_phy_probe(pegasus_t * pegasus)
1193 {
1194         int i;
1195         __u16 tmp;
1196
1197         for (i = 0; i < 32; i++) {
1198                 read_mii_word(pegasus, i, MII_BMSR, &tmp);
1199                 if (tmp == 0 || tmp == 0xffff || (tmp & BMSR_MEDIA) == 0)
1200                         continue;
1201                 else
1202                         return i;
1203         }
1204
1205         return 0xff;
1206 }
1207
1208 static inline void setup_pegasus_II(pegasus_t * pegasus)
1209 {
1210         __u8 data = 0xa5;
1211         int ret;
1212         
1213         ret = set_register(pegasus, Reg1d, 0);
1214         ret = set_register(pegasus, Reg7b, 1);
1215         mdelay(100);
1216         if ((pegasus->features & HAS_HOME_PNA) && mii_mode)
1217                 ret = set_register(pegasus, Reg7b, 0);
1218         else
1219                 ret = set_register(pegasus, Reg7b, 2);
1220
1221         ret = set_register(pegasus, 0x83, data);
1222         ret = get_registers(pegasus, 0x83, 1, &data);
1223
1224         if (data == 0xa5) {
1225                 pegasus->chip = 0x8513;
1226         } else {
1227                 pegasus->chip = 0;
1228         }
1229
1230         ret = set_register(pegasus, 0x80, 0xc0);
1231         ret = set_register(pegasus, 0x83, 0xff);
1232         ret = set_register(pegasus, 0x84, 0x01);
1233         
1234         if (pegasus->features & HAS_HOME_PNA && mii_mode)
1235                 ret = set_register(pegasus, Reg81, 6);
1236         else
1237                 ret = set_register(pegasus, Reg81, 2);
1238 }
1239
1240
1241 static struct workqueue_struct *pegasus_workqueue = NULL;
1242 #define CARRIER_CHECK_DELAY (2 * HZ)
1243
1244 static void check_carrier(void *data)
1245 {
1246         pegasus_t *pegasus = data;
1247         set_carrier(pegasus->net);
1248         if (!(pegasus->flags & PEGASUS_UNPLUG)) {
1249                 queue_delayed_work(pegasus_workqueue, &pegasus->carrier_check,
1250                         CARRIER_CHECK_DELAY);
1251         }
1252 }
1253
1254 static int pegasus_probe(struct usb_interface *intf,
1255                          const struct usb_device_id *id)
1256 {
1257         struct usb_device *dev = interface_to_usbdev(intf);
1258         struct net_device *net;
1259         pegasus_t *pegasus;
1260         int dev_index = id - pegasus_ids;
1261         int res = -ENOMEM;
1262
1263         usb_get_dev(dev);
1264         net = alloc_etherdev(sizeof(struct pegasus));
1265         if (!net) {
1266                 dev_err(&intf->dev, "can't allocate %s\n", "device");
1267                 goto out;
1268         }
1269
1270         pegasus = netdev_priv(net);
1271         memset(pegasus, 0, sizeof (struct pegasus));
1272         pegasus->dev_index = dev_index;
1273         init_waitqueue_head(&pegasus->ctrl_wait);
1274
1275         if (!alloc_urbs(pegasus)) {
1276                 dev_err(&intf->dev, "can't allocate %s\n", "urbs");
1277                 goto out1;
1278         }
1279
1280         tasklet_init(&pegasus->rx_tl, rx_fixup, (unsigned long) pegasus);
1281
1282         INIT_WORK(&pegasus->carrier_check, check_carrier, pegasus);
1283
1284         pegasus->intf = intf;
1285         pegasus->usb = dev;
1286         pegasus->net = net;
1287         SET_MODULE_OWNER(net);
1288         net->open = pegasus_open;
1289         net->stop = pegasus_close;
1290         net->watchdog_timeo = PEGASUS_TX_TIMEOUT;
1291         net->tx_timeout = pegasus_tx_timeout;
1292         net->do_ioctl = pegasus_ioctl;
1293         net->hard_start_xmit = pegasus_start_xmit;
1294         net->set_multicast_list = pegasus_set_multicast;
1295         net->get_stats = pegasus_netdev_stats;
1296         SET_ETHTOOL_OPS(net, &ops);
1297         pegasus->mii.dev = net;
1298         pegasus->mii.mdio_read = mdio_read;
1299         pegasus->mii.mdio_write = mdio_write;
1300         pegasus->mii.phy_id_mask = 0x1f;
1301         pegasus->mii.reg_num_mask = 0x1f;
1302         spin_lock_init(&pegasus->rx_pool_lock);
1303         pegasus->msg_enable = netif_msg_init (msg_level, NETIF_MSG_DRV
1304                                 | NETIF_MSG_PROBE | NETIF_MSG_LINK);
1305
1306         pegasus->features = usb_dev_id[dev_index].private;
1307         get_interrupt_interval(pegasus);
1308         if (reset_mac(pegasus)) {
1309                 dev_err(&intf->dev, "can't reset MAC\n");
1310                 res = -EIO;
1311                 goto out2;
1312         }
1313         set_ethernet_addr(pegasus);
1314         fill_skb_pool(pegasus);
1315         if (pegasus->features & PEGASUS_II) {
1316                 dev_info(&intf->dev, "setup Pegasus II specific registers\n");
1317                 setup_pegasus_II(pegasus);
1318         }
1319         pegasus->phy = mii_phy_probe(pegasus);
1320         if (pegasus->phy == 0xff) {
1321                 dev_warn(&intf->dev, "can't locate MII phy, using default\n");
1322                 pegasus->phy = 1;
1323         }
1324         pegasus->mii.phy_id = pegasus->phy;
1325         usb_set_intfdata(intf, pegasus);
1326         SET_NETDEV_DEV(net, &intf->dev);
1327         pegasus_reset_wol(net);
1328         res = register_netdev(net);
1329         if (res)
1330                 goto out3;
1331         queue_delayed_work(pegasus_workqueue, &pegasus->carrier_check,
1332                                 CARRIER_CHECK_DELAY);
1333
1334         dev_info(&intf->dev, "%s, %s, %02x:%02x:%02x:%02x:%02x:%02x\n",
1335                 net->name,
1336                 usb_dev_id[dev_index].name,
1337                 net->dev_addr [0], net->dev_addr [1],
1338                 net->dev_addr [2], net->dev_addr [3],
1339                 net->dev_addr [4], net->dev_addr [5]);
1340         return 0;
1341
1342 out3:
1343         usb_set_intfdata(intf, NULL);
1344         free_skb_pool(pegasus);
1345 out2:
1346         free_all_urbs(pegasus);
1347 out1:
1348         free_netdev(net);
1349 out:
1350         usb_put_dev(dev);
1351         return res;
1352 }
1353
1354 static void pegasus_disconnect(struct usb_interface *intf)
1355 {
1356         struct pegasus *pegasus = usb_get_intfdata(intf);
1357
1358         usb_set_intfdata(intf, NULL);
1359         if (!pegasus) {
1360                 dev_dbg(&intf->dev, "unregistering non-bound device?\n");
1361                 return;
1362         }
1363
1364         pegasus->flags |= PEGASUS_UNPLUG;
1365         cancel_delayed_work(&pegasus->carrier_check);
1366         unregister_netdev(pegasus->net);
1367         usb_put_dev(interface_to_usbdev(intf));
1368         unlink_all_urbs(pegasus);
1369         free_all_urbs(pegasus);
1370         free_skb_pool(pegasus);
1371         if (pegasus->rx_skb)
1372                 dev_kfree_skb(pegasus->rx_skb);
1373         free_netdev(pegasus->net);
1374 }
1375
1376 static int pegasus_suspend (struct usb_interface *intf, pm_message_t message)
1377 {
1378         struct pegasus *pegasus = usb_get_intfdata(intf);
1379         
1380         netif_device_detach (pegasus->net);
1381         if (netif_running(pegasus->net)) {
1382                 cancel_delayed_work(&pegasus->carrier_check);
1383
1384                 usb_kill_urb(pegasus->rx_urb);
1385                 usb_kill_urb(pegasus->intr_urb);
1386         }
1387         intf->dev.power.power_state = PMSG_SUSPEND;
1388         return 0;
1389 }
1390
1391 static int pegasus_resume (struct usb_interface *intf)
1392 {
1393         struct pegasus *pegasus = usb_get_intfdata(intf);
1394
1395         intf->dev.power.power_state = PMSG_ON;
1396         netif_device_attach (pegasus->net);
1397         if (netif_running(pegasus->net)) {
1398                 pegasus->rx_urb->status = 0;
1399                 pegasus->rx_urb->actual_length = 0;
1400                 read_bulk_callback(pegasus->rx_urb, NULL);
1401
1402                 pegasus->intr_urb->status = 0;
1403                 pegasus->intr_urb->actual_length = 0;
1404                 intr_callback(pegasus->intr_urb, NULL);
1405
1406                 queue_delayed_work(pegasus_workqueue, &pegasus->carrier_check,
1407                                         CARRIER_CHECK_DELAY);
1408         }
1409         return 0;
1410 }
1411
1412 static struct usb_driver pegasus_driver = {
1413         .name = driver_name,
1414         .probe = pegasus_probe,
1415         .disconnect = pegasus_disconnect,
1416         .id_table = pegasus_ids,
1417         .suspend = pegasus_suspend,
1418         .resume = pegasus_resume,
1419 };
1420
1421 static int __init pegasus_init(void)
1422 {
1423         pr_info("%s: %s, " DRIVER_DESC "\n", driver_name, DRIVER_VERSION);
1424         pegasus_workqueue = create_singlethread_workqueue("pegasus");
1425         if (!pegasus_workqueue)
1426                 return -ENOMEM;
1427         return usb_register(&pegasus_driver);
1428 }
1429
1430 static void __exit pegasus_exit(void)
1431 {
1432         destroy_workqueue(pegasus_workqueue);
1433         usb_deregister(&pegasus_driver);
1434 }
1435
1436 module_init(pegasus_init);
1437 module_exit(pegasus_exit);