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