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