]> bbs.cooldavid.org Git - net-next-2.6.git/blob - drivers/net/usb/pegasus.c
net: trans_start cleanups
[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; /* prevent tx timeout */
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         }
913         dev_kfree_skb(skb);
914
915         return NETDEV_TX_OK;
916 }
917
918 static struct net_device_stats *pegasus_netdev_stats(struct net_device *dev)
919 {
920         return &((pegasus_t *) netdev_priv(dev))->stats;
921 }
922
923 static inline void disable_net_traffic(pegasus_t * pegasus)
924 {
925         __le16 tmp = cpu_to_le16(0);
926
927         set_registers(pegasus, EthCtrl0, sizeof(tmp), &tmp);
928 }
929
930 static inline void get_interrupt_interval(pegasus_t * pegasus)
931 {
932         u16 data;
933         u8 interval;
934
935         read_eprom_word(pegasus, 4, &data);
936         interval = data >> 8;
937         if (pegasus->usb->speed != USB_SPEED_HIGH) {
938                 if (interval < 0x80) {
939                         netif_info(pegasus, timer, pegasus->net,
940                                    "intr interval changed from %ums to %ums\n",
941                                    interval, 0x80);
942                         interval = 0x80;
943                         data = (data & 0x00FF) | ((u16)interval << 8);
944 #ifdef PEGASUS_WRITE_EEPROM
945                         write_eprom_word(pegasus, 4, data);
946 #endif
947                 }
948         }
949         pegasus->intr_interval = interval;
950 }
951
952 static void set_carrier(struct net_device *net)
953 {
954         pegasus_t *pegasus = netdev_priv(net);
955         u16 tmp;
956
957         if (read_mii_word(pegasus, pegasus->phy, MII_BMSR, &tmp))
958                 return;
959
960         if (tmp & BMSR_LSTATUS)
961                 netif_carrier_on(net);
962         else
963                 netif_carrier_off(net);
964 }
965
966 static void free_all_urbs(pegasus_t * pegasus)
967 {
968         usb_free_urb(pegasus->intr_urb);
969         usb_free_urb(pegasus->tx_urb);
970         usb_free_urb(pegasus->rx_urb);
971         usb_free_urb(pegasus->ctrl_urb);
972 }
973
974 static void unlink_all_urbs(pegasus_t * pegasus)
975 {
976         usb_kill_urb(pegasus->intr_urb);
977         usb_kill_urb(pegasus->tx_urb);
978         usb_kill_urb(pegasus->rx_urb);
979         usb_kill_urb(pegasus->ctrl_urb);
980 }
981
982 static int alloc_urbs(pegasus_t * pegasus)
983 {
984         pegasus->ctrl_urb = usb_alloc_urb(0, GFP_KERNEL);
985         if (!pegasus->ctrl_urb) {
986                 return 0;
987         }
988         pegasus->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
989         if (!pegasus->rx_urb) {
990                 usb_free_urb(pegasus->ctrl_urb);
991                 return 0;
992         }
993         pegasus->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
994         if (!pegasus->tx_urb) {
995                 usb_free_urb(pegasus->rx_urb);
996                 usb_free_urb(pegasus->ctrl_urb);
997                 return 0;
998         }
999         pegasus->intr_urb = usb_alloc_urb(0, GFP_KERNEL);
1000         if (!pegasus->intr_urb) {
1001                 usb_free_urb(pegasus->tx_urb);
1002                 usb_free_urb(pegasus->rx_urb);
1003                 usb_free_urb(pegasus->ctrl_urb);
1004                 return 0;
1005         }
1006
1007         return 1;
1008 }
1009
1010 static int pegasus_open(struct net_device *net)
1011 {
1012         pegasus_t *pegasus = netdev_priv(net);
1013         int res;
1014
1015         if (pegasus->rx_skb == NULL)
1016                 pegasus->rx_skb = pull_skb(pegasus);
1017         /*
1018          ** Note: no point to free the pool.  it is empty :-)
1019          */
1020         if (!pegasus->rx_skb)
1021                 return -ENOMEM;
1022
1023         res = set_registers(pegasus, EthID, 6, net->dev_addr);
1024         
1025         usb_fill_bulk_urb(pegasus->rx_urb, pegasus->usb,
1026                           usb_rcvbulkpipe(pegasus->usb, 1),
1027                           pegasus->rx_skb->data, PEGASUS_MTU + 8,
1028                           read_bulk_callback, pegasus);
1029         if ((res = usb_submit_urb(pegasus->rx_urb, GFP_KERNEL))) {
1030                 if (res == -ENODEV)
1031                         netif_device_detach(pegasus->net);
1032                 netif_dbg(pegasus, ifup, net, "failed rx_urb, %d\n", res);
1033                 goto exit;
1034         }
1035
1036         usb_fill_int_urb(pegasus->intr_urb, pegasus->usb,
1037                          usb_rcvintpipe(pegasus->usb, 3),
1038                          pegasus->intr_buff, sizeof (pegasus->intr_buff),
1039                          intr_callback, pegasus, pegasus->intr_interval);
1040         if ((res = usb_submit_urb(pegasus->intr_urb, GFP_KERNEL))) {
1041                 if (res == -ENODEV)
1042                         netif_device_detach(pegasus->net);
1043                 netif_dbg(pegasus, ifup, net, "failed intr_urb, %d\n", res);
1044                 usb_kill_urb(pegasus->rx_urb);
1045                 goto exit;
1046         }
1047         if ((res = enable_net_traffic(net, pegasus->usb))) {
1048                 netif_dbg(pegasus, ifup, net,
1049                           "can't enable_net_traffic() - %d\n", res);
1050                 res = -EIO;
1051                 usb_kill_urb(pegasus->rx_urb);
1052                 usb_kill_urb(pegasus->intr_urb);
1053                 free_skb_pool(pegasus);
1054                 goto exit;
1055         }
1056         set_carrier(net);
1057         netif_start_queue(net);
1058         netif_dbg(pegasus, ifup, net, "open\n");
1059         res = 0;
1060 exit:
1061         return res;
1062 }
1063
1064 static int pegasus_close(struct net_device *net)
1065 {
1066         pegasus_t *pegasus = netdev_priv(net);
1067
1068         netif_stop_queue(net);
1069         if (!(pegasus->flags & PEGASUS_UNPLUG))
1070                 disable_net_traffic(pegasus);
1071         tasklet_kill(&pegasus->rx_tl);
1072         unlink_all_urbs(pegasus);
1073
1074         return 0;
1075 }
1076
1077 static void pegasus_get_drvinfo(struct net_device *dev,
1078                                 struct ethtool_drvinfo *info)
1079 {
1080         pegasus_t *pegasus = netdev_priv(dev);
1081         strncpy(info->driver, driver_name, sizeof (info->driver) - 1);
1082         strncpy(info->version, DRIVER_VERSION, sizeof (info->version) - 1);
1083         usb_make_path(pegasus->usb, info->bus_info, sizeof (info->bus_info));
1084 }
1085
1086 /* also handles three patterns of some kind in hardware */
1087 #define WOL_SUPPORTED   (WAKE_MAGIC|WAKE_PHY)
1088
1089 static void
1090 pegasus_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1091 {
1092         pegasus_t       *pegasus = netdev_priv(dev);
1093
1094         wol->supported = WAKE_MAGIC | WAKE_PHY;
1095         wol->wolopts = pegasus->wolopts;
1096 }
1097
1098 static int
1099 pegasus_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1100 {
1101         pegasus_t       *pegasus = netdev_priv(dev);
1102         u8              reg78 = 0x04;
1103         
1104         if (wol->wolopts & ~WOL_SUPPORTED)
1105                 return -EINVAL;
1106
1107         if (wol->wolopts & WAKE_MAGIC)
1108                 reg78 |= 0x80;
1109         if (wol->wolopts & WAKE_PHY)
1110                 reg78 |= 0x40;
1111         /* FIXME this 0x10 bit still needs to get set in the chip... */
1112         if (wol->wolopts)
1113                 pegasus->eth_regs[0] |= 0x10;
1114         else
1115                 pegasus->eth_regs[0] &= ~0x10;
1116         pegasus->wolopts = wol->wolopts;
1117         return set_register(pegasus, WakeupControl, reg78);
1118 }
1119
1120 static inline void pegasus_reset_wol(struct net_device *dev)
1121 {
1122         struct ethtool_wolinfo wol;
1123         
1124         memset(&wol, 0, sizeof wol);
1125         (void) pegasus_set_wol(dev, &wol);
1126 }
1127
1128 static int
1129 pegasus_get_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
1130 {
1131         pegasus_t *pegasus;
1132
1133         pegasus = netdev_priv(dev);
1134         mii_ethtool_gset(&pegasus->mii, ecmd);
1135         return 0;
1136 }
1137
1138 static int
1139 pegasus_set_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
1140 {
1141         pegasus_t *pegasus = netdev_priv(dev);
1142         return mii_ethtool_sset(&pegasus->mii, ecmd);
1143 }
1144
1145 static int pegasus_nway_reset(struct net_device *dev)
1146 {
1147         pegasus_t *pegasus = netdev_priv(dev);
1148         return mii_nway_restart(&pegasus->mii);
1149 }
1150
1151 static u32 pegasus_get_link(struct net_device *dev)
1152 {
1153         pegasus_t *pegasus = netdev_priv(dev);
1154         return mii_link_ok(&pegasus->mii);
1155 }
1156
1157 static u32 pegasus_get_msglevel(struct net_device *dev)
1158 {
1159         pegasus_t *pegasus = netdev_priv(dev);
1160         return pegasus->msg_enable;
1161 }
1162
1163 static void pegasus_set_msglevel(struct net_device *dev, u32 v)
1164 {
1165         pegasus_t *pegasus = netdev_priv(dev);
1166         pegasus->msg_enable = v;
1167 }
1168
1169 static const struct ethtool_ops ops = {
1170         .get_drvinfo = pegasus_get_drvinfo,
1171         .get_settings = pegasus_get_settings,
1172         .set_settings = pegasus_set_settings,
1173         .nway_reset = pegasus_nway_reset,
1174         .get_link = pegasus_get_link,
1175         .get_msglevel = pegasus_get_msglevel,
1176         .set_msglevel = pegasus_set_msglevel,
1177         .get_wol = pegasus_get_wol,
1178         .set_wol = pegasus_set_wol,
1179 };
1180
1181 static int pegasus_ioctl(struct net_device *net, struct ifreq *rq, int cmd)
1182 {
1183         __u16 *data = (__u16 *) & rq->ifr_ifru;
1184         pegasus_t *pegasus = netdev_priv(net);
1185         int res;
1186
1187         switch (cmd) {
1188         case SIOCDEVPRIVATE:
1189                 data[0] = pegasus->phy;
1190         case SIOCDEVPRIVATE + 1:
1191                 read_mii_word(pegasus, data[0], data[1] & 0x1f, &data[3]);
1192                 res = 0;
1193                 break;
1194         case SIOCDEVPRIVATE + 2:
1195                 if (!capable(CAP_NET_ADMIN))
1196                         return -EPERM;
1197                 write_mii_word(pegasus, pegasus->phy, data[1] & 0x1f, data[2]);
1198                 res = 0;
1199                 break;
1200         default:
1201                 res = -EOPNOTSUPP;
1202         }
1203         return res;
1204 }
1205
1206 static void pegasus_set_multicast(struct net_device *net)
1207 {
1208         pegasus_t *pegasus = netdev_priv(net);
1209
1210         if (net->flags & IFF_PROMISC) {
1211                 pegasus->eth_regs[EthCtrl2] |= RX_PROMISCUOUS;
1212                 netif_info(pegasus, link, net, "Promiscuous mode enabled\n");
1213         } else if (!netdev_mc_empty(net) || (net->flags & IFF_ALLMULTI)) {
1214                 pegasus->eth_regs[EthCtrl0] |= RX_MULTICAST;
1215                 pegasus->eth_regs[EthCtrl2] &= ~RX_PROMISCUOUS;
1216                 netif_dbg(pegasus, link, net, "set allmulti\n");
1217         } else {
1218                 pegasus->eth_regs[EthCtrl0] &= ~RX_MULTICAST;
1219                 pegasus->eth_regs[EthCtrl2] &= ~RX_PROMISCUOUS;
1220         }
1221
1222         pegasus->ctrl_urb->status = 0;
1223
1224         pegasus->flags |= ETH_REGS_CHANGE;
1225         ctrl_callback(pegasus->ctrl_urb);
1226 }
1227
1228 static __u8 mii_phy_probe(pegasus_t * pegasus)
1229 {
1230         int i;
1231         __u16 tmp;
1232
1233         for (i = 0; i < 32; i++) {
1234                 read_mii_word(pegasus, i, MII_BMSR, &tmp);
1235                 if (tmp == 0 || tmp == 0xffff || (tmp & BMSR_MEDIA) == 0)
1236                         continue;
1237                 else
1238                         return i;
1239         }
1240
1241         return 0xff;
1242 }
1243
1244 static inline void setup_pegasus_II(pegasus_t * pegasus)
1245 {
1246         __u8 data = 0xa5;
1247         
1248         set_register(pegasus, Reg1d, 0);
1249         set_register(pegasus, Reg7b, 1);
1250         mdelay(100);
1251         if ((pegasus->features & HAS_HOME_PNA) && mii_mode)
1252                 set_register(pegasus, Reg7b, 0);
1253         else
1254                 set_register(pegasus, Reg7b, 2);
1255
1256         set_register(pegasus, 0x83, data);
1257         get_registers(pegasus, 0x83, 1, &data);
1258
1259         if (data == 0xa5) {
1260                 pegasus->chip = 0x8513;
1261         } else {
1262                 pegasus->chip = 0;
1263         }
1264
1265         set_register(pegasus, 0x80, 0xc0);
1266         set_register(pegasus, 0x83, 0xff);
1267         set_register(pegasus, 0x84, 0x01);
1268         
1269         if (pegasus->features & HAS_HOME_PNA && mii_mode)
1270                 set_register(pegasus, Reg81, 6);
1271         else
1272                 set_register(pegasus, Reg81, 2);
1273 }
1274
1275
1276 static int pegasus_count;
1277 static struct workqueue_struct *pegasus_workqueue = NULL;
1278 #define CARRIER_CHECK_DELAY (2 * HZ)
1279
1280 static void check_carrier(struct work_struct *work)
1281 {
1282         pegasus_t *pegasus = container_of(work, pegasus_t, carrier_check.work);
1283         set_carrier(pegasus->net);
1284         if (!(pegasus->flags & PEGASUS_UNPLUG)) {
1285                 queue_delayed_work(pegasus_workqueue, &pegasus->carrier_check,
1286                         CARRIER_CHECK_DELAY);
1287         }
1288 }
1289
1290 static int pegasus_blacklisted(struct usb_device *udev)
1291 {
1292         struct usb_device_descriptor *udd = &udev->descriptor;
1293
1294         /* Special quirk to keep the driver from handling the Belkin Bluetooth
1295          * dongle which happens to have the same ID.
1296          */
1297         if ((udd->idVendor == cpu_to_le16(VENDOR_BELKIN)) &&
1298             (udd->idProduct == cpu_to_le16(0x0121)) &&
1299             (udd->bDeviceClass == USB_CLASS_WIRELESS_CONTROLLER) &&
1300             (udd->bDeviceProtocol == 1))
1301                 return 1;
1302
1303         return 0;
1304 }
1305
1306 /* we rely on probe() and remove() being serialized so we
1307  * don't need extra locking on pegasus_count.
1308  */
1309 static void pegasus_dec_workqueue(void)
1310 {
1311         pegasus_count--;
1312         if (pegasus_count == 0) {
1313                 destroy_workqueue(pegasus_workqueue);
1314                 pegasus_workqueue = NULL;
1315         }
1316 }
1317
1318 static int pegasus_probe(struct usb_interface *intf,
1319                          const struct usb_device_id *id)
1320 {
1321         struct usb_device *dev = interface_to_usbdev(intf);
1322         struct net_device *net;
1323         pegasus_t *pegasus;
1324         int dev_index = id - pegasus_ids;
1325         int res = -ENOMEM;
1326
1327         if (pegasus_blacklisted(dev))
1328                 return -ENODEV;
1329
1330         if (pegasus_count == 0) {
1331                 pegasus_workqueue = create_singlethread_workqueue("pegasus");
1332                 if (!pegasus_workqueue)
1333                         return -ENOMEM;
1334         }
1335         pegasus_count++;
1336
1337         usb_get_dev(dev);
1338
1339         net = alloc_etherdev(sizeof(struct pegasus));
1340         if (!net) {
1341                 dev_err(&intf->dev, "can't allocate %s\n", "device");
1342                 goto out;
1343         }
1344
1345         pegasus = netdev_priv(net);
1346         pegasus->dev_index = dev_index;
1347         init_waitqueue_head(&pegasus->ctrl_wait);
1348
1349         if (!alloc_urbs(pegasus)) {
1350                 dev_err(&intf->dev, "can't allocate %s\n", "urbs");
1351                 goto out1;
1352         }
1353
1354         tasklet_init(&pegasus->rx_tl, rx_fixup, (unsigned long) pegasus);
1355
1356         INIT_DELAYED_WORK(&pegasus->carrier_check, check_carrier);
1357
1358         pegasus->intf = intf;
1359         pegasus->usb = dev;
1360         pegasus->net = net;
1361
1362
1363         net->watchdog_timeo = PEGASUS_TX_TIMEOUT;
1364         net->netdev_ops = &pegasus_netdev_ops;
1365         SET_ETHTOOL_OPS(net, &ops);
1366         pegasus->mii.dev = net;
1367         pegasus->mii.mdio_read = mdio_read;
1368         pegasus->mii.mdio_write = mdio_write;
1369         pegasus->mii.phy_id_mask = 0x1f;
1370         pegasus->mii.reg_num_mask = 0x1f;
1371         spin_lock_init(&pegasus->rx_pool_lock);
1372         pegasus->msg_enable = netif_msg_init (msg_level, NETIF_MSG_DRV
1373                                 | NETIF_MSG_PROBE | NETIF_MSG_LINK);
1374
1375         pegasus->features = usb_dev_id[dev_index].private;
1376         get_interrupt_interval(pegasus);
1377         if (reset_mac(pegasus)) {
1378                 dev_err(&intf->dev, "can't reset MAC\n");
1379                 res = -EIO;
1380                 goto out2;
1381         }
1382         set_ethernet_addr(pegasus);
1383         fill_skb_pool(pegasus);
1384         if (pegasus->features & PEGASUS_II) {
1385                 dev_info(&intf->dev, "setup Pegasus II specific registers\n");
1386                 setup_pegasus_II(pegasus);
1387         }
1388         pegasus->phy = mii_phy_probe(pegasus);
1389         if (pegasus->phy == 0xff) {
1390                 dev_warn(&intf->dev, "can't locate MII phy, using default\n");
1391                 pegasus->phy = 1;
1392         }
1393         pegasus->mii.phy_id = pegasus->phy;
1394         usb_set_intfdata(intf, pegasus);
1395         SET_NETDEV_DEV(net, &intf->dev);
1396         pegasus_reset_wol(net);
1397         res = register_netdev(net);
1398         if (res)
1399                 goto out3;
1400         queue_delayed_work(pegasus_workqueue, &pegasus->carrier_check,
1401                                 CARRIER_CHECK_DELAY);
1402
1403         dev_info(&intf->dev, "%s, %s, %pM\n",
1404                  net->name,
1405                  usb_dev_id[dev_index].name,
1406                  net->dev_addr);
1407         return 0;
1408
1409 out3:
1410         usb_set_intfdata(intf, NULL);
1411         free_skb_pool(pegasus);
1412 out2:
1413         free_all_urbs(pegasus);
1414 out1:
1415         free_netdev(net);
1416 out:
1417         usb_put_dev(dev);
1418         pegasus_dec_workqueue();
1419         return res;
1420 }
1421
1422 static void pegasus_disconnect(struct usb_interface *intf)
1423 {
1424         struct pegasus *pegasus = usb_get_intfdata(intf);
1425
1426         usb_set_intfdata(intf, NULL);
1427         if (!pegasus) {
1428                 dev_dbg(&intf->dev, "unregistering non-bound device?\n");
1429                 return;
1430         }
1431
1432         pegasus->flags |= PEGASUS_UNPLUG;
1433         cancel_delayed_work(&pegasus->carrier_check);
1434         unregister_netdev(pegasus->net);
1435         usb_put_dev(interface_to_usbdev(intf));
1436         unlink_all_urbs(pegasus);
1437         free_all_urbs(pegasus);
1438         free_skb_pool(pegasus);
1439         if (pegasus->rx_skb != NULL) {
1440                 dev_kfree_skb(pegasus->rx_skb);
1441                 pegasus->rx_skb = NULL;
1442         }
1443         free_netdev(pegasus->net);
1444         pegasus_dec_workqueue();
1445 }
1446
1447 static int pegasus_suspend (struct usb_interface *intf, pm_message_t message)
1448 {
1449         struct pegasus *pegasus = usb_get_intfdata(intf);
1450         
1451         netif_device_detach (pegasus->net);
1452         cancel_delayed_work(&pegasus->carrier_check);
1453         if (netif_running(pegasus->net)) {
1454                 usb_kill_urb(pegasus->rx_urb);
1455                 usb_kill_urb(pegasus->intr_urb);
1456         }
1457         return 0;
1458 }
1459
1460 static int pegasus_resume (struct usb_interface *intf)
1461 {
1462         struct pegasus *pegasus = usb_get_intfdata(intf);
1463
1464         netif_device_attach (pegasus->net);
1465         if (netif_running(pegasus->net)) {
1466                 pegasus->rx_urb->status = 0;
1467                 pegasus->rx_urb->actual_length = 0;
1468                 read_bulk_callback(pegasus->rx_urb);
1469
1470                 pegasus->intr_urb->status = 0;
1471                 pegasus->intr_urb->actual_length = 0;
1472                 intr_callback(pegasus->intr_urb);
1473         }
1474         queue_delayed_work(pegasus_workqueue, &pegasus->carrier_check,
1475                                 CARRIER_CHECK_DELAY);
1476         return 0;
1477 }
1478
1479 static const struct net_device_ops pegasus_netdev_ops = {
1480         .ndo_open =                     pegasus_open,
1481         .ndo_stop =                     pegasus_close,
1482         .ndo_do_ioctl =                 pegasus_ioctl,
1483         .ndo_start_xmit =               pegasus_start_xmit,
1484         .ndo_set_multicast_list =       pegasus_set_multicast,
1485         .ndo_get_stats =                pegasus_netdev_stats,
1486         .ndo_tx_timeout =               pegasus_tx_timeout,
1487         .ndo_change_mtu =               eth_change_mtu,
1488         .ndo_set_mac_address =          eth_mac_addr,
1489         .ndo_validate_addr =            eth_validate_addr,
1490 };
1491
1492 static struct usb_driver pegasus_driver = {
1493         .name = driver_name,
1494         .probe = pegasus_probe,
1495         .disconnect = pegasus_disconnect,
1496         .id_table = pegasus_ids,
1497         .suspend = pegasus_suspend,
1498         .resume = pegasus_resume,
1499 };
1500
1501 static void __init parse_id(char *id)
1502 {
1503         unsigned int vendor_id=0, device_id=0, flags=0, i=0;
1504         char *token, *name=NULL;
1505
1506         if ((token = strsep(&id, ":")) != NULL)
1507                 name = token;
1508         /* name now points to a null terminated string*/
1509         if ((token = strsep(&id, ":")) != NULL)
1510                 vendor_id = simple_strtoul(token, NULL, 16);
1511         if ((token = strsep(&id, ":")) != NULL)
1512                 device_id = simple_strtoul(token, NULL, 16);
1513         flags = simple_strtoul(id, NULL, 16);
1514         pr_info("%s: new device %s, vendor ID 0x%04x, device ID 0x%04x, flags: 0x%x\n",
1515                 driver_name, name, vendor_id, device_id, flags);
1516
1517         if (vendor_id > 0x10000 || vendor_id == 0)
1518                 return;
1519         if (device_id > 0x10000 || device_id == 0)
1520                 return;
1521
1522         for (i=0; usb_dev_id[i].name; i++);
1523         usb_dev_id[i].name = name;
1524         usb_dev_id[i].vendor = vendor_id;
1525         usb_dev_id[i].device = device_id;
1526         usb_dev_id[i].private = flags;
1527         pegasus_ids[i].match_flags = USB_DEVICE_ID_MATCH_DEVICE;
1528         pegasus_ids[i].idVendor = vendor_id;
1529         pegasus_ids[i].idProduct = device_id;
1530 }
1531
1532 static int __init pegasus_init(void)
1533 {
1534         pr_info("%s: %s, " DRIVER_DESC "\n", driver_name, DRIVER_VERSION);
1535         if (devid)
1536                 parse_id(devid);
1537         return usb_register(&pegasus_driver);
1538 }
1539
1540 static void __exit pegasus_exit(void)
1541 {
1542         usb_deregister(&pegasus_driver);
1543 }
1544
1545 module_init(pegasus_init);
1546 module_exit(pegasus_exit);