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