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