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