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