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