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