more changes on original files
[linux-2.4.git] / drivers / usb / pegasus.c
1 /*
2  *  Copyright (c) 1999-2003 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  *
9  *      ChangeLog:
10  *              ....    Most of the time spend reading sources & docs.
11  *              v0.2.x  First official release for the Linux kernel.
12  *              v0.3.0  Beutified and structured, some bugs fixed.
13  *              v0.3.x  URBifying bulk requests and bugfixing. First relatively
14  *                      stable release. Still can touch device's registers only
15  *                      from top-halves.
16  *              v0.4.0  Control messages remained unurbified are now URBs.
17  *                      Now we can touch the HW at any time.
18  *              v0.4.9  Control urbs again use process context to wait. Argh...
19  *                      Some long standing bugs (enable_net_traffic) fixed.
20  *                      Also nasty trick about resubmiting control urb from
21  *                      interrupt context used. Please let me know how it
22  *                      behaves. Pegasus II support added since this version.
23  *                      TODO: suppressing HCD warnings spewage on disconnect.
24  *              v0.4.13 Ethernet address is now set at probe(), not at open()
25  *                      time as this seems to break dhcpd. 
26  *              v0.4.25 ethtool support added.
27  */
28
29 #include <linux/sched.h>
30 #include <linux/slab.h>
31 #include <linux/init.h>
32 #include <linux/delay.h>
33 #include <linux/netdevice.h>
34 #include <linux/etherdevice.h>
35 #include <linux/ethtool.h>
36 #include <linux/mii.h>
37 #include <linux/usb.h>
38 #include <linux/module.h>
39 #include <asm/uaccess.h>
40 #include "pegasus.h"
41
42 /*
43  * Version Information
44  */
45 #define DRIVER_VERSION "v0.4.32 (2003/06/06)"
46 #define DRIVER_AUTHOR "Petko Manolov <petkan@users.sourceforge.net>"
47 #define DRIVER_DESC "Pegasus/Pegasus II USB Ethernet driver"
48
49 static const char driver_name[] = "pegasus";
50
51 #define PEGASUS_USE_INTR
52 #define PEGASUS_WRITE_EEPROM
53 #define BMSR_MEDIA      (BMSR_10HALF | BMSR_10FULL | BMSR_100HALF | \
54                         BMSR_100FULL | BMSR_ANEGCAPABLE)
55
56 static int loopback = 0;
57 static int mii_mode = 0;
58 static int multicast_filter_limit = 32;
59
60 static struct usb_eth_dev usb_dev_id[] = {
61 #define PEGASUS_DEV(pn, vid, pid, flags)        \
62         {name:pn, vendor:vid, device:pid, private:flags},
63 #include "pegasus.h"
64 #undef  PEGASUS_DEV
65         {NULL, 0, 0, 0}
66 };
67
68 static struct usb_device_id pegasus_ids[] = {
69 #define PEGASUS_DEV(pn, vid, pid, flags) \
70         {match_flags: USB_DEVICE_ID_MATCH_DEVICE, idVendor:vid, idProduct:pid},
71 #include "pegasus.h"
72 #undef  PEGASUS_DEV
73         {}
74 };
75
76 MODULE_AUTHOR(DRIVER_AUTHOR);
77 MODULE_DESCRIPTION(DRIVER_DESC);
78 MODULE_LICENSE("GPL");
79 MODULE_PARM(loopback, "i");
80 MODULE_PARM(mii_mode, "i");
81 MODULE_PARM_DESC(loopback, "Enable MAC loopback mode (bit 0)");
82 MODULE_PARM_DESC(mii_mode, "Enable HomePNA mode (bit 0),default=MII mode = 0");
83
84 MODULE_DEVICE_TABLE(usb, pegasus_ids);
85
86 static int update_eth_regs_async(pegasus_t *);
87 /* Aargh!!! I _really_ hate such tweaks */
88 static void ctrl_callback(struct urb *urb)
89 {
90         pegasus_t *pegasus = urb->context;
91
92         if (!pegasus)
93                 return;
94
95         switch (urb->status) {
96         case 0:
97                 if (pegasus->flags & ETH_REGS_CHANGE) {
98                         pegasus->flags &= ~ETH_REGS_CHANGE;
99                         pegasus->flags |= ETH_REGS_CHANGED;
100                         update_eth_regs_async(pegasus);
101                         return;
102                 }
103                 break;
104         case -EINPROGRESS:
105                 return;
106         case -ENOENT:
107                 break;
108         default:
109                 warn("%s: status %d", __FUNCTION__, urb->status);
110         }
111         pegasus->flags &= ~ETH_REGS_CHANGED;
112         wake_up(&pegasus->ctrl_wait);
113 }
114
115 static int get_registers(pegasus_t * pegasus, u16 indx, u16 size,
116                          void *data)
117 {
118         int ret;
119         char *buffer;
120         DECLARE_WAITQUEUE(wait, current);
121
122         buffer = kmalloc(size, GFP_DMA);
123         if (!buffer) {
124                 warn("%s: looks like we're out of memory", __FUNCTION__);
125                 return -ENOMEM;
126         }
127         add_wait_queue(&pegasus->ctrl_wait, &wait);
128         set_current_state(TASK_UNINTERRUPTIBLE);
129         while (pegasus->flags & ETH_REGS_CHANGED)
130                 schedule();
131         remove_wait_queue(&pegasus->ctrl_wait, &wait);
132         set_current_state(TASK_RUNNING);
133
134         pegasus->dr.bRequestType = PEGASUS_REQT_READ;
135         pegasus->dr.bRequest = PEGASUS_REQ_GET_REGS;
136         pegasus->dr.wValue = cpu_to_le16(0);
137         pegasus->dr.wIndex = cpu_to_le16p(&indx);
138         pegasus->dr.wLength = cpu_to_le16p(&size);
139         pegasus->ctrl_urb->transfer_buffer_length = size;
140
141         FILL_CONTROL_URB(pegasus->ctrl_urb, pegasus->usb,
142                          usb_rcvctrlpipe(pegasus->usb, 0),
143                          (char *) &pegasus->dr,
144                          buffer, size, ctrl_callback, pegasus);
145
146         add_wait_queue(&pegasus->ctrl_wait, &wait);
147         set_current_state(TASK_UNINTERRUPTIBLE);
148
149         if ((ret = usb_submit_urb(pegasus->ctrl_urb))) {
150                 set_current_state(TASK_RUNNING);
151                 err("%s: BAD CTRLs %d", __FUNCTION__, ret);
152                 goto out;
153         }
154
155         schedule();
156 out:
157         remove_wait_queue(&pegasus->ctrl_wait, &wait);
158         memcpy(data, buffer, size);
159         kfree(buffer);
160
161         return ret;
162 }
163
164 static int set_registers(pegasus_t * pegasus, u16 indx, u16 size,
165                          void *data)
166 {
167         int ret;
168         char *buffer;
169         DECLARE_WAITQUEUE(wait, current);
170
171         buffer = kmalloc(size, GFP_DMA);
172         if (!buffer) {
173                 warn("%s: looks like we're out of memory", __FUNCTION__);
174                 return -ENOMEM;
175         }
176         memcpy(buffer, data, size);
177
178         add_wait_queue(&pegasus->ctrl_wait, &wait);
179         set_current_state(TASK_UNINTERRUPTIBLE);
180         while (pegasus->flags & ETH_REGS_CHANGED)
181                 schedule();
182         remove_wait_queue(&pegasus->ctrl_wait, &wait);
183         set_current_state(TASK_RUNNING);
184
185         pegasus->dr.bRequestType = PEGASUS_REQT_WRITE;
186         pegasus->dr.bRequest = PEGASUS_REQ_SET_REGS;
187         pegasus->dr.wValue = cpu_to_le16(0);
188         pegasus->dr.wIndex = cpu_to_le16p(&indx);
189         pegasus->dr.wLength = cpu_to_le16p(&size);
190         pegasus->ctrl_urb->transfer_buffer_length = size;
191
192         FILL_CONTROL_URB(pegasus->ctrl_urb, pegasus->usb,
193                          usb_sndctrlpipe(pegasus->usb, 0),
194                          (char *) &pegasus->dr,
195                          buffer, size, ctrl_callback, pegasus);
196
197         add_wait_queue(&pegasus->ctrl_wait, &wait);
198         set_current_state(TASK_UNINTERRUPTIBLE);
199
200         if ((ret = usb_submit_urb(pegasus->ctrl_urb))) {
201                 set_current_state(TASK_RUNNING);
202                 err("%s: BAD CTRL %d", __FUNCTION__, ret);
203                 goto out;
204         }
205
206         schedule();
207 out:
208         remove_wait_queue(&pegasus->ctrl_wait, &wait);
209         kfree(buffer);
210
211         return ret;
212 }
213
214 static int set_register(pegasus_t * pegasus, u16 indx, u8 data)
215 {
216         int ret;
217         char *tmp;
218         DECLARE_WAITQUEUE(wait, current);
219
220         tmp = kmalloc(1, GFP_DMA);
221         if (!tmp) {
222                 warn("%s: looks like we're out of memory", __FUNCTION__);
223                 return -ENOMEM;
224         }
225         memcpy(tmp, &data, 1);
226         add_wait_queue(&pegasus->ctrl_wait, &wait);
227         set_current_state(TASK_UNINTERRUPTIBLE);
228         while (pegasus->flags & ETH_REGS_CHANGED)
229                 schedule();
230         remove_wait_queue(&pegasus->ctrl_wait, &wait);
231         set_current_state(TASK_RUNNING);
232
233         pegasus->dr.bRequestType = PEGASUS_REQT_WRITE;
234         pegasus->dr.bRequest = PEGASUS_REQ_SET_REG;
235         pegasus->dr.wValue = cpu_to_le16(data);
236         pegasus->dr.wIndex = cpu_to_le16p(&indx);
237         pegasus->dr.wLength = cpu_to_le16(1);
238         pegasus->ctrl_urb->transfer_buffer_length = 1;
239
240         FILL_CONTROL_URB(pegasus->ctrl_urb, pegasus->usb,
241                          usb_sndctrlpipe(pegasus->usb, 0),
242                          (char *) &pegasus->dr,
243                          tmp, 1, ctrl_callback, pegasus);
244
245         add_wait_queue(&pegasus->ctrl_wait, &wait);
246         set_current_state(TASK_UNINTERRUPTIBLE);
247
248         if ((ret = usb_submit_urb(pegasus->ctrl_urb))) {
249                 set_current_state(TASK_RUNNING);
250                 err("%s: BAD CTRL %d", __FUNCTION__, ret);
251                 goto out;
252         }
253
254         schedule();
255 out:
256         remove_wait_queue(&pegasus->ctrl_wait, &wait);
257         kfree(tmp);
258
259         return ret;
260 }
261
262 static int update_eth_regs_async(pegasus_t * pegasus)
263 {
264         int ret;
265
266         pegasus->dr.bRequestType = PEGASUS_REQT_WRITE;
267         pegasus->dr.bRequest = PEGASUS_REQ_SET_REGS;
268         pegasus->dr.wValue = 0;
269         pegasus->dr.wIndex = cpu_to_le16(EthCtrl0);
270         pegasus->dr.wLength = cpu_to_le16(3);
271         pegasus->ctrl_urb->transfer_buffer_length = 3;
272
273         FILL_CONTROL_URB(pegasus->ctrl_urb, pegasus->usb,
274                          usb_sndctrlpipe(pegasus->usb, 0),
275                          (char *) &pegasus->dr,
276                          pegasus->eth_regs, 3, ctrl_callback, pegasus);
277
278         if ((ret = usb_submit_urb(pegasus->ctrl_urb)))
279                 err("%s: BAD CTRL %d, flgs %x", __FUNCTION__, ret,
280                     pegasus->flags);
281
282         return ret;
283 }
284
285 static int read_mii_word(pegasus_t * pegasus, u8 phy, u8 indx, u16 * regd)
286 {
287         int i;
288         u8 data[4] = { phy, 0, 0, indx };
289         u16 regdi;
290
291         set_register(pegasus, PhyCtrl, 0);
292         set_registers(pegasus, PhyAddr, sizeof(data), data);
293         set_register(pegasus, PhyCtrl, (indx | PHY_READ));
294         for (i = 0; i < REG_TIMEOUT; i++) {
295                 get_registers(pegasus, PhyCtrl, 1, data);
296                 if (data[0] & PHY_DONE)
297                         break;
298         }
299         if (i < REG_TIMEOUT) {
300                 get_registers(pegasus, PhyData, 2, &regdi);
301                 *regd = le16_to_cpu(regdi);
302                 return 0;
303         }
304         warn("%s: failed", __FUNCTION__);
305
306         return 1;
307 }
308
309 static int mdio_read(struct net_device *dev, int phy_id, int loc)
310 {
311         pegasus_t *pegasus = (pegasus_t *) dev->priv;
312         int res;
313
314         read_mii_word(pegasus, phy_id, loc, (u16 *) & res);
315         return res & 0xffff;
316 }
317
318 static int write_mii_word(pegasus_t * pegasus, u8 phy, u8 indx, u16 regd)
319 {
320         int i;
321         u8 data[4] = { phy, 0, 0, indx };
322
323         *(data + 1) = cpu_to_le16p(&regd);
324         set_register(pegasus, PhyCtrl, 0);
325         set_registers(pegasus, PhyAddr, 4, data);
326         set_register(pegasus, PhyCtrl, (indx | PHY_WRITE));
327         for (i = 0; i < REG_TIMEOUT; i++) {
328                 get_registers(pegasus, PhyCtrl, 1, data);
329                 if (data[0] & PHY_DONE)
330                         break;
331         }
332         if (i < REG_TIMEOUT)
333                 return 0;
334         warn("%s: failed", __FUNCTION__);
335
336         return 1;
337 }
338
339 static void mdio_write(struct net_device *dev, int phy_id, int loc, int val)
340 {
341         pegasus_t *pegasus = (pegasus_t *) dev->priv;
342
343         write_mii_word(pegasus, phy_id, loc, val);
344 }
345
346 static int read_eprom_word(pegasus_t * pegasus, u8 index, u16 * retdata)
347 {
348         int i;
349         u8 tmp;
350         u16 retdatai;
351
352         set_register(pegasus, EpromCtrl, 0);
353         set_register(pegasus, EpromOffset, index);
354         set_register(pegasus, EpromCtrl, EPROM_READ);
355
356         for (i = 0; i < REG_TIMEOUT; i++) {
357                 get_registers(pegasus, EpromCtrl, 1, &tmp);
358                 if (tmp & EPROM_DONE)
359                         break;
360         }
361         if (i < REG_TIMEOUT) {
362                 get_registers(pegasus, EpromData, 2, &retdatai);
363                 *retdata = le16_to_cpu(retdatai);
364                 return 0;
365         }
366         warn("%s: failed", __FUNCTION__);
367
368         return -1;
369 }
370
371 #ifdef  PEGASUS_WRITE_EEPROM
372 static inline void enable_eprom_write(pegasus_t * pegasus)
373 {
374         u8 tmp;
375
376         get_registers(pegasus, EthCtrl2, 1, &tmp);
377         set_register(pegasus, EthCtrl2, tmp | EPROM_WR_ENABLE);
378 }
379
380 static inline void disable_eprom_write(pegasus_t * pegasus)
381 {
382         u8 tmp;
383
384         get_registers(pegasus, EthCtrl2, 1, &tmp);
385         set_register(pegasus, EpromCtrl, 0);
386         set_register(pegasus, EthCtrl2, tmp & ~EPROM_WR_ENABLE);
387 }
388
389 static int write_eprom_word(pegasus_t * pegasus, u8 index, u16 data)
390 {
391         int i, tmp;
392         u8 d[4] = { 0x3f, 0, 0, EPROM_WRITE };
393
394         set_registers(pegasus, EpromOffset, 4, d);
395         enable_eprom_write(pegasus);
396         set_register(pegasus, EpromOffset, index);
397         set_registers(pegasus, EpromData, 2, &data);
398         set_register(pegasus, EpromCtrl, EPROM_WRITE);
399
400         for (i = 0; i < REG_TIMEOUT; i++) {
401                 get_registers(pegasus, EpromCtrl, 1, &tmp);
402                 if (tmp & EPROM_DONE)
403                         break;
404         }
405         disable_eprom_write(pegasus);
406         if (i < REG_TIMEOUT)
407                 return 0;
408         warn("%s: failed", __FUNCTION__);
409         return -1;
410 }
411 #endif                          /* PEGASUS_WRITE_EEPROM */
412
413 static inline void get_node_id(pegasus_t * pegasus, u8 * id)
414 {
415         int i;
416         u16 w16;
417
418         for (i = 0; i < 3; i++) {
419                 read_eprom_word(pegasus, i, &w16);
420                 ((u16 *) id)[i] = cpu_to_le16p(&w16);
421         }
422 }
423
424 static void set_ethernet_addr(pegasus_t * pegasus)
425 {
426         u8 node_id[6];
427
428         get_node_id(pegasus, node_id);
429         set_registers(pegasus, EthID, sizeof(node_id), node_id);
430         memcpy(pegasus->net->dev_addr, node_id, sizeof(node_id));
431 }
432
433 static inline int reset_mac(pegasus_t * pegasus)
434 {
435         u8 data = 0x8;
436         int i;
437
438         set_register(pegasus, EthCtrl1, data);
439         for (i = 0; i < REG_TIMEOUT; i++) {
440                 get_registers(pegasus, EthCtrl1, 1, &data);
441                 if (~data & 0x08) {
442                         if (loopback & 1)
443                                 break;
444                         if (mii_mode && (pegasus->features & HAS_HOME_PNA))
445                                 set_register(pegasus, Gpio1, 0x34);
446                         else
447                                 set_register(pegasus, Gpio1, 0x26);
448                         set_register(pegasus, Gpio0, pegasus->features);
449                         set_register(pegasus, Gpio0, DEFAULT_GPIO_SET);
450                         break;
451                 }
452         }
453         if (i == REG_TIMEOUT)
454                 return 1;
455
456         if (usb_dev_id[pegasus->dev_index].vendor == VENDOR_LINKSYS ||
457             usb_dev_id[pegasus->dev_index].vendor == VENDOR_DLINK) {
458                 u16 auxmode;
459
460                 read_mii_word(pegasus, 1, 0x1b, &auxmode);
461                 write_mii_word(pegasus, 1, 0x1b, auxmode | 4);
462         }
463         if (usb_dev_id[pegasus->dev_index].vendor == VENDOR_ELCON) {
464                 u16 auxmode;
465                 read_mii_word(pegasus, 3, 0x1b, &auxmode);
466                 write_mii_word(pegasus, 3, 0x1b, auxmode | 4);
467         }
468         return 0;
469 }
470
471 static int enable_net_traffic(struct net_device *dev, struct usb_device *usb)
472 {
473         u16 linkpart, bmsr;
474         u8 data[4];
475         pegasus_t *pegasus = dev->priv;
476
477         /* read twice 'cos this is a latch bit */
478         read_mii_word(pegasus, pegasus->phy, MII_BMSR, &bmsr);
479         read_mii_word(pegasus, pegasus->phy, MII_BMSR, &bmsr);
480         if (read_mii_word(pegasus, pegasus->phy, MII_LPA, &linkpart))
481                 return 2;
482         if (!(linkpart & 1))
483                 warn("link partner stat %x", linkpart);
484
485         data[0] = 0xc9;
486         data[1] = 0;
487         if (linkpart & (ADVERTISE_100FULL | ADVERTISE_10FULL))
488                 data[1] |= 0x20;        /* set full duplex */
489         if (linkpart & (ADVERTISE_100FULL | ADVERTISE_100HALF))
490                 data[1] |= 0x10;        /* set 100 Mbps */
491         if (mii_mode)
492                 data[1] |= 1;
493         data[2] = (loopback & 1) ? 0x09 : 0x01;
494         memcpy(pegasus->eth_regs, data, sizeof(data));
495         set_registers(pegasus, EthCtrl0, 3, data);
496
497         return 0;
498 }
499
500 static void read_bulk_callback(struct urb *urb)
501 {
502         pegasus_t *pegasus = urb->context;
503         struct net_device *net;
504         int count = urb->actual_length, res;
505         int rx_status;
506         struct sk_buff *skb;
507         u16 pkt_len;
508
509         if (!pegasus || !(pegasus->flags & PEGASUS_RUNNING))
510                 return;
511
512         net = pegasus->net;
513         if (!netif_device_present(net))
514                 return;
515
516         if (pegasus->flags & PEGASUS_RX_BUSY) {
517                 pegasus->stats.rx_errors++;
518                 dbg("pegasus Rx busy");
519                 return;
520         }
521         pegasus->flags |= PEGASUS_RX_BUSY;
522
523         switch (urb->status) {
524         case 0:
525                 break;
526         case -ETIMEDOUT:
527                 dbg("reset MAC");
528                 pegasus->flags &= ~PEGASUS_RX_BUSY;
529                 break;
530         default:
531                 dbg("%s: RX status %d", net->name, urb->status);
532                 goto goon;
533         }
534
535         if (!count)
536                 goto goon;
537
538         rx_status = le32_to_cpu(*(int *) (pegasus->rx_buff + count - 4));
539         if (rx_status & 0x000e0000) {
540                 dbg("%s: RX packet error %x", net->name, rx_status & 0xe0000);
541                 pegasus->stats.rx_errors++;
542                 if (rx_status & 0x060000)
543                         pegasus->stats.rx_length_errors++;
544                 if (rx_status & 0x080000)
545                         pegasus->stats.rx_crc_errors++;
546                 if (rx_status & 0x100000)
547                         pegasus->stats.rx_frame_errors++;
548                 goto goon;
549         }
550
551         if (pegasus->chip == 0x8513) {
552                 pkt_len = le32_to_cpu(*(int *)pegasus->rx_buff);
553                 pkt_len &= 0x0fff;
554         } else {
555                 pkt_len = (rx_status & 0x0fff) - 8;
556         }
557
558         if (!(skb = dev_alloc_skb(pkt_len + 2)))
559                 goto goon;
560
561         skb->dev = net;
562         skb_reserve(skb, 2);
563         eth_copy_and_sum(skb, pegasus->rx_buff, pkt_len, 0);
564         skb_put(skb, pkt_len);
565
566         if (pegasus->chip == 0x8513) {
567                 skb->data += 2;
568         }
569
570         skb->protocol = eth_type_trans(skb, net);
571         netif_rx(skb);
572         pegasus->stats.rx_packets++;
573         pegasus->stats.rx_bytes += pkt_len;
574
575 goon:
576         FILL_BULK_URB(pegasus->rx_urb, pegasus->usb,
577                       usb_rcvbulkpipe(pegasus->usb, 1),
578                       pegasus->rx_buff, PEGASUS_MAX_MTU,
579                       read_bulk_callback, pegasus);
580         if ((res = usb_submit_urb(pegasus->rx_urb)))
581                 warn("%s: failed submint rx_urb %d", __FUNCTION__, res);
582         pegasus->flags &= ~PEGASUS_RX_BUSY;
583 }
584
585 static void write_bulk_callback(struct urb *urb)
586 {
587         pegasus_t *pegasus = urb->context;
588         struct net_device *net = pegasus->net;
589
590         if (!pegasus || !(pegasus->flags & PEGASUS_RUNNING))
591                 return;
592
593         if (!netif_device_present(net))
594                 return;
595
596         switch (urb->status) {
597         case -EPIPE:
598                 /* FIXME schedule_work() to clear the tx halt */
599                 netif_stop_queue(net);
600                 warn("%s: no tx stall recovery", net->name);
601                 return;
602         case -ENOENT:
603         case -ECONNRESET:
604         case -ESHUTDOWN:
605                 dbg("%s: tx unlink, %d", net->name, urb->status);
606                 return;
607         default:
608                 info("%s: TX status %d", net->name, urb->status);
609                 /* FALL THROUGH */
610         case 0:
611                 break;
612         }
613
614         net->trans_start = jiffies;
615         netif_wake_queue(net);
616 }
617
618 #ifdef  PEGASUS_USE_INTR
619 static void intr_callback(struct urb *urb)
620 {
621         pegasus_t *pegasus = urb->context;
622         struct net_device *net;
623         u8 *d;
624
625         if (!pegasus)
626                 return;
627
628         switch (urb->status) {
629         case 0:
630                 break;
631         case -ENOENT:
632                 return;
633         default:
634                 info("intr status %d", urb->status);
635         }
636
637         d = urb->transfer_buffer;
638         net = pegasus->net;
639         if (d[0] & 0xfc) {
640                 pegasus->stats.tx_errors++;
641                 if (d[0] & TX_UNDERRUN)
642                         pegasus->stats.tx_fifo_errors++;
643                 if (d[0] & (EXCESSIVE_COL | JABBER_TIMEOUT))
644                         pegasus->stats.tx_aborted_errors++;
645                 if (d[0] & LATE_COL)
646                         pegasus->stats.tx_window_errors++;
647                 if (d[5] & LINK_STATUS) {
648                         netif_carrier_on(net);
649                 } else {
650                         netif_carrier_off(net);
651                         pegasus->stats.tx_carrier_errors++;
652                 }
653         }
654 }
655 #endif
656
657 static void pegasus_tx_timeout(struct net_device *net)
658 {
659         pegasus_t *pegasus = net->priv;
660
661         if (!pegasus)
662                 return;
663
664         warn("%s: Tx timed out.", net->name);
665         pegasus->tx_urb->transfer_flags |= USB_ASYNC_UNLINK;
666         usb_unlink_urb(pegasus->tx_urb);
667         pegasus->stats.tx_errors++;
668 }
669
670 static int pegasus_start_xmit(struct sk_buff *skb, struct net_device *net)
671 {
672         pegasus_t *pegasus = net->priv;
673         int count = ((skb->len + 2) & 0x3f) ? skb->len + 2 : skb->len + 3;
674         int res;
675         u16 l16 = skb->len;
676
677         netif_stop_queue(net);
678
679         ((u16 *) pegasus->tx_buff)[0] = cpu_to_le16(l16);
680         memcpy(pegasus->tx_buff + 2, skb->data, skb->len);
681         FILL_BULK_URB(pegasus->tx_urb, pegasus->usb,
682                       usb_sndbulkpipe(pegasus->usb, 2),
683                       pegasus->tx_buff, PEGASUS_MAX_MTU,
684                       write_bulk_callback, pegasus);
685         pegasus->tx_urb->transfer_buffer_length = count;
686         if ((res = usb_submit_urb(pegasus->tx_urb))) {
687                 switch (res) {
688                 case -EPIPE:    /* stall, or disconnect from TT */
689                         /* cleanup should already have been scheduled */
690                         break;
691                 case -ENODEV:   /* disconnect() upcoming */
692                         break;
693                 default:
694                         pegasus->stats.tx_errors++;
695                         netif_start_queue(net);
696                 }
697         } else {
698                 pegasus->stats.tx_packets++;
699                 pegasus->stats.tx_bytes += skb->len;
700                 net->trans_start = jiffies;
701         }
702
703         dev_kfree_skb(skb);
704
705         return 0;
706 }
707
708 static struct net_device_stats *pegasus_netdev_stats(struct net_device *dev)
709 {
710         return &((pegasus_t *) dev->priv)->stats;
711 }
712
713 static inline void disable_net_traffic(pegasus_t * pegasus)
714 {
715         int tmp = 0;
716
717         set_registers(pegasus, EthCtrl0, 2, &tmp);
718 }
719
720 static inline void get_interrupt_interval(pegasus_t * pegasus)
721 {
722         u8 data[2];
723
724         read_eprom_word(pegasus, 4, (u16 *) data);
725         if (data[1] < 0x80) {
726                 info("intr interval will be changed from %ums to %ums",
727                      data[1], 0x80);
728                 data[1] = 0x80;
729 #ifdef  PEGASUS_WRITE_EEPROM
730                 write_eprom_word(pegasus, 4, *(u16 *) data);
731 #endif
732         }
733         pegasus->intr_interval = data[1];
734 }
735
736 static void set_carrier(struct net_device *net)
737 {
738         pegasus_t *pegasus;
739         short tmp;
740
741         pegasus = net->priv;
742         read_mii_word(pegasus, pegasus->phy, MII_BMSR, &tmp);
743         if (tmp & BMSR_LSTATUS)
744                 netif_carrier_on(net);
745         else
746                 netif_carrier_off(net);
747
748 }
749
750 static int pegasus_open(struct net_device *net)
751 {
752         pegasus_t *pegasus = (pegasus_t *) net->priv;
753         int res;
754
755         down(&pegasus->sem);
756
757         set_registers(pegasus, EthID, 6, net->dev_addr);
758         
759         FILL_BULK_URB(pegasus->rx_urb, pegasus->usb,
760                       usb_rcvbulkpipe(pegasus->usb, 1),
761                       pegasus->rx_buff, PEGASUS_MAX_MTU,
762                       read_bulk_callback, pegasus);
763         if ((res = usb_submit_urb(pegasus->rx_urb)))
764                 warn("%s: failed rx_urb %d", __FUNCTION__, res);
765 #ifdef  PEGASUS_USE_INTR
766         FILL_INT_URB(pegasus->intr_urb, pegasus->usb,
767                      usb_rcvintpipe(pegasus->usb, 3),
768                      pegasus->intr_buff, sizeof(pegasus->intr_buff),
769                      intr_callback, pegasus, pegasus->intr_interval);
770         if ((res = usb_submit_urb(pegasus->intr_urb)))
771                 warn("%s: failed intr_urb %d", __FUNCTION__, res);
772 #endif
773         netif_start_queue(net);
774         pegasus->flags |= PEGASUS_RUNNING;
775         if ((res = enable_net_traffic(net, pegasus->usb))) {
776                 err("can't enable_net_traffic() - %d", res);
777                 res = -EIO;
778                 goto exit;
779         }
780
781         set_carrier(net);
782         res = 0;
783 exit:
784         up(&pegasus->sem);
785
786         return res;
787 }
788
789 static int pegasus_close(struct net_device *net)
790 {
791         pegasus_t *pegasus = net->priv;
792
793         down(&pegasus->sem);
794         pegasus->flags &= ~PEGASUS_RUNNING;
795         netif_stop_queue(net);
796         if (!(pegasus->flags & PEGASUS_UNPLUG))
797                 disable_net_traffic(pegasus);
798
799         usb_unlink_urb(pegasus->rx_urb);
800         usb_unlink_urb(pegasus->tx_urb);
801         usb_unlink_urb(pegasus->ctrl_urb);
802 #ifdef  PEGASUS_USE_INTR
803         usb_unlink_urb(pegasus->intr_urb);
804 #endif
805         up(&pegasus->sem);
806
807         return 0;
808 }
809 #if CONFIG_MII
810 static int pegasus_ethtool_ioctl(struct net_device *dev, void *useraddr)
811 {
812
813         u32 ethcmd;
814         pegasus_t *pegasus = dev->priv;
815
816         if (copy_from_user(&ethcmd, useraddr, sizeof (ethcmd)))
817                 return -EFAULT;
818
819         switch (ethcmd) {
820         /* get driver-specific version/etc. info */
821         case ETHTOOL_GDRVINFO:{
822                         struct ethtool_drvinfo info;
823
824                         memset (&info, 0, sizeof info);
825                         info.cmd = ETHTOOL_GDRVINFO;
826                         strncpy(info.driver, driver_name,
827                                 sizeof (info.driver) - 1);
828                         strncpy(info.version, DRIVER_VERSION,
829                                 sizeof (info.version) - 1);
830                         usb_make_path(pegasus->usb, info.bus_info,
831                                       sizeof info.bus_info);
832                         if (copy_to_user(useraddr, &info, sizeof (info)))
833                                 return -EFAULT;
834                         return 0;
835                 }
836
837         /* get settings */
838         case ETHTOOL_GSET:{
839                         struct ethtool_cmd ecmd = { ETHTOOL_GSET };
840                         mii_ethtool_gset(&pegasus->mii, &ecmd);
841                         if (copy_to_user(useraddr, &ecmd, sizeof (ecmd)))
842                                 return -EFAULT;
843                         return 0;
844                 }
845         /* set settings */
846         case ETHTOOL_SSET:{
847                         int r;
848                         struct ethtool_cmd ecmd;
849                         if (copy_from_user(&ecmd, useraddr, sizeof (ecmd)))
850                                 return -EFAULT;
851                         r = mii_ethtool_sset(&pegasus->mii, &ecmd);
852                         return r;
853                 }
854         /* restart autonegotiation */
855         case ETHTOOL_NWAY_RST:{
856                         return mii_nway_restart(&pegasus->mii);
857                 }
858
859         /* get link status */
860         case ETHTOOL_GLINK:{
861                         struct ethtool_value edata = { ETHTOOL_GLINK };
862                         edata.data = mii_link_ok(&pegasus->mii);
863                         if (copy_to_user(useraddr, &edata, sizeof (edata)))
864                                 return -EFAULT;
865                         return 0;
866                 }
867         /* get message-level */
868         case ETHTOOL_GMSGLVL:{
869                         struct ethtool_value edata = { ETHTOOL_GMSGLVL };
870                         /* edata.data = pegasus->msg_enable; FIXME */
871                         if (copy_to_user(useraddr, &edata, sizeof (edata)))
872                                 return -EFAULT;
873                         return 0;
874                 }
875         /* set message-level */
876         case ETHTOOL_SMSGLVL:{
877                         struct ethtool_value edata;
878                         if (copy_from_user(&edata, useraddr, sizeof (edata)))
879                                 return -EFAULT;
880                         /* sp->msg_enable = edata.data; FIXME */
881                         return 0;
882                 }
883
884         }
885
886         return -EOPNOTSUPP;
887
888 }
889 #else
890 static int pegasus_ethtool_ioctl(struct net_device *net, void *uaddr)
891 {
892         pegasus_t *pegasus;
893         int cmd;
894
895         pegasus = net->priv;
896         if (get_user(cmd, (int *) uaddr))
897                 return -EFAULT;
898         switch (cmd) {
899         case ETHTOOL_GDRVINFO:{
900                         struct ethtool_drvinfo info;
901
902                         memset (&info, 0, sizeof info);
903                         info.cmd = ETHTOOL_GDRVINFO;
904                         strncpy(info.driver, driver_name,
905                                 sizeof (info.driver) - 1);
906                         strncpy(info.driver, DRIVER_DESC, ETHTOOL_BUSINFO_LEN);
907                         strncpy(info.version, DRIVER_VERSION,
908                                 sizeof (info.version) - 1);
909                         usb_make_path(pegasus->usb, info.bus_info,
910                                       sizeof (info.bus_info) -1);
911                         if (copy_to_user(uaddr, &info, sizeof(info)))
912                                 return -EFAULT;
913                         return 0;
914                 }
915         case ETHTOOL_GSET:{
916                         struct ethtool_cmd ecmd;
917                         short lpa, bmcr;
918                         u8 port;
919
920                         if (copy_from_user(&ecmd, uaddr, sizeof(ecmd)))
921                                 return -EFAULT;
922                         ecmd.supported = (SUPPORTED_10baseT_Half |
923                                           SUPPORTED_10baseT_Full |
924                                           SUPPORTED_100baseT_Half |
925                                           SUPPORTED_100baseT_Full |
926                                           SUPPORTED_Autoneg |
927                                           SUPPORTED_TP | SUPPORTED_MII);
928                         get_registers(pegasus, Reg7b, 1, &port);
929                         if (port == 0)
930                                 ecmd.port = PORT_MII;
931                         else
932                                 ecmd.port = PORT_TP;
933                         ecmd.transceiver = XCVR_INTERNAL;
934                         ecmd.phy_address = pegasus->phy;
935                         read_mii_word(pegasus, pegasus->phy, MII_BMCR, &bmcr);
936                         read_mii_word(pegasus, pegasus->phy, MII_LPA, &lpa);
937                         if (bmcr & BMCR_ANENABLE) {
938                                 ecmd.autoneg = AUTONEG_ENABLE;
939                                 ecmd.speed = lpa & (LPA_100HALF | LPA_100FULL) ?
940                                     SPEED_100 : SPEED_10;
941                                 if (ecmd.speed == SPEED_100)
942                                         ecmd.duplex = lpa & LPA_100FULL ?
943                                             DUPLEX_FULL : DUPLEX_HALF;
944                                 else
945                                         ecmd.duplex = lpa & LPA_10FULL ?
946                                             DUPLEX_FULL : DUPLEX_HALF;
947                         } else {
948                                 ecmd.autoneg = AUTONEG_DISABLE;
949                                 ecmd.speed = bmcr & BMCR_SPEED100 ?
950                                     SPEED_100 : SPEED_10;
951                                 ecmd.duplex = bmcr & BMCR_FULLDPLX ?
952                                     DUPLEX_FULL : DUPLEX_HALF;
953                         }
954                         if (copy_to_user(uaddr, &ecmd, sizeof(ecmd)))
955                                 return -EFAULT;
956
957                         return 0;
958                 }
959         case ETHTOOL_SSET:{
960                         return -EOPNOTSUPP;
961                 }
962         case ETHTOOL_GLINK:{
963                         struct ethtool_value edata = { ETHTOOL_GLINK };
964                         edata.data = netif_carrier_ok(net);
965                         if (copy_to_user(uaddr, &edata, sizeof(edata)))
966                                 return -EFAULT;
967                         return 0;
968                 }
969         default:
970                 return -EOPNOTSUPP;
971         }
972 }
973 #endif
974 static int pegasus_ioctl(struct net_device *net, struct ifreq *rq, int cmd)
975 {
976         u16 *data = (u16 *) & rq->ifr_data;
977         pegasus_t *pegasus = net->priv;
978         int res;
979
980         down(&pegasus->sem);
981         switch (cmd) {
982         case SIOCETHTOOL:
983                 res = pegasus_ethtool_ioctl(net, rq->ifr_data);
984                 break;
985         case SIOCDEVPRIVATE:
986                 data[0] = pegasus->phy;
987         case SIOCDEVPRIVATE + 1:
988                 read_mii_word(pegasus, data[0], data[1] & 0x1f, &data[3]);
989                 res = 0;
990                 break;
991         case SIOCDEVPRIVATE + 2:
992                 if (!capable(CAP_NET_ADMIN)) {
993                         up(&pegasus->sem);
994                         return -EPERM;
995                 }
996                 write_mii_word(pegasus, pegasus->phy, data[1] & 0x1f, data[2]);
997                 res = 0;
998                 break;
999         default:
1000                 res = -EOPNOTSUPP;
1001         }
1002         up(&pegasus->sem);
1003
1004         return res;
1005 }
1006
1007 static void pegasus_set_multicast(struct net_device *net)
1008 {
1009         pegasus_t *pegasus = net->priv;
1010
1011         netif_stop_queue(net);
1012
1013         if (net->flags & IFF_PROMISC) {
1014                 pegasus->eth_regs[EthCtrl2] |= RX_PROMISCUOUS;
1015                 info("%s: Promiscuous mode enabled", net->name);
1016         } else if ((net->mc_count > multicast_filter_limit) ||
1017                    (net->flags & IFF_ALLMULTI)) {
1018                 pegasus->eth_regs[EthCtrl0] |= RX_MULTICAST;
1019                 pegasus->eth_regs[EthCtrl2] &= ~RX_PROMISCUOUS;
1020                 info("%s set allmulti", net->name);
1021         } else {
1022                 pegasus->eth_regs[EthCtrl0] &= ~RX_MULTICAST;
1023                 pegasus->eth_regs[EthCtrl2] &= ~RX_PROMISCUOUS;
1024         }
1025
1026         pegasus->flags |= ETH_REGS_CHANGE;
1027         ctrl_callback(pegasus->ctrl_urb);
1028
1029         netif_wake_queue(net);
1030 }
1031
1032 static u8 mii_phy_probe(pegasus_t * pegasus)
1033 {
1034         int i;
1035         u16 tmp;
1036
1037         for (i = 0; i < 32; i++) {
1038                 read_mii_word(pegasus, i, MII_BMSR, &tmp);
1039                 if (tmp == 0 || tmp == 0xffff || (tmp & BMSR_MEDIA) == 0)
1040                         continue;
1041                 else
1042                         return i;
1043         }
1044
1045         return 0xff;
1046 }
1047
1048 static inline void setup_pegasus_II(pegasus_t * pegasus)
1049 {
1050         u16 data = 0xa5;
1051         
1052         set_register(pegasus, Reg1d, 0);
1053         set_register(pegasus, Reg7b, 1);
1054         mdelay(100);
1055         if ((pegasus->features & HAS_HOME_PNA) && mii_mode)
1056                 set_register(pegasus, Reg7b, 0);
1057         else
1058                 set_register(pegasus, Reg7b, 2);
1059         
1060         set_register(pegasus, 0x83, data);
1061         get_registers(pegasus, 0x83, 1, &data);
1062         
1063         if (data & 0xa5) {
1064                 pegasus->chip = 0x8513;
1065         } else {
1066                 pegasus->chip = 0;
1067         }
1068         
1069         set_register( pegasus, 0x80, 0xc0 );
1070         set_register( pegasus, 0x83, 0xff );
1071         set_register( pegasus, 0x84, 0x01 );
1072         
1073         if ((pegasus->features & HAS_HOME_PNA) && mii_mode)
1074                 set_register(pegasus, Reg81, 6);
1075         else
1076                 set_register(pegasus, Reg81, 2);
1077 }
1078
1079 static void *pegasus_probe(struct usb_device *dev, unsigned int ifnum,
1080                            const struct usb_device_id *id)
1081 {
1082         struct net_device *net;
1083         pegasus_t *pegasus;
1084         int dev_index = id - pegasus_ids;
1085
1086         if (usb_set_configuration(dev, dev->config[0].bConfigurationValue)) {
1087                 err("usb_set_configuration() failed");
1088                 return NULL;
1089         }
1090
1091         if (!(pegasus = kmalloc(sizeof(struct pegasus), GFP_KERNEL))) {
1092                 err("out of memory allocating device structure");
1093                 return NULL;
1094         }
1095
1096         usb_inc_dev_use(dev);
1097         memset(pegasus, 0, sizeof(struct pegasus));
1098         pegasus->dev_index = dev_index;
1099         init_waitqueue_head(&pegasus->ctrl_wait);
1100
1101         pegasus->ctrl_urb = usb_alloc_urb(0);
1102         if (!pegasus->ctrl_urb) {
1103                 kfree(pegasus);
1104                 return NULL;
1105         }
1106         pegasus->rx_urb = usb_alloc_urb(0);
1107         if (!pegasus->rx_urb) {
1108                 usb_free_urb(pegasus->ctrl_urb);
1109                 kfree(pegasus);
1110                 return NULL;
1111         }
1112         pegasus->tx_urb = usb_alloc_urb(0);
1113         if (!pegasus->tx_urb) {
1114                 usb_free_urb(pegasus->rx_urb);
1115                 usb_free_urb(pegasus->ctrl_urb);
1116                 kfree(pegasus);
1117                 return NULL;
1118         }
1119         pegasus->intr_urb = usb_alloc_urb(0);
1120         if (!pegasus->intr_urb) {
1121                 usb_free_urb(pegasus->tx_urb);
1122                 usb_free_urb(pegasus->rx_urb);
1123                 usb_free_urb(pegasus->ctrl_urb);
1124                 kfree(pegasus);
1125                 return NULL;
1126         }
1127
1128         net = init_etherdev(NULL, 0);
1129         if (!net) {
1130                 usb_free_urb(pegasus->tx_urb);
1131                 usb_free_urb(pegasus->rx_urb);
1132                 usb_free_urb(pegasus->ctrl_urb);
1133                 kfree(pegasus);
1134                 return NULL;
1135         }
1136
1137         init_MUTEX(&pegasus->sem);
1138         down(&pegasus->sem);
1139         pegasus->usb = dev;
1140         pegasus->net = net;
1141         SET_MODULE_OWNER(net);
1142         net->priv = pegasus;
1143         net->open = pegasus_open;
1144         net->stop = pegasus_close;
1145         net->watchdog_timeo = PEGASUS_TX_TIMEOUT;
1146         net->tx_timeout = pegasus_tx_timeout;
1147         net->do_ioctl = pegasus_ioctl;
1148         net->hard_start_xmit = pegasus_start_xmit;
1149         net->set_multicast_list = pegasus_set_multicast;
1150         net->get_stats = pegasus_netdev_stats;
1151         net->mtu = PEGASUS_MTU;
1152         pegasus->mii.dev = net;
1153         pegasus->mii.mdio_read = mdio_read;
1154         pegasus->mii.mdio_write = mdio_write;
1155         pegasus->mii.phy_id_mask = 0x1f;
1156         pegasus->mii.reg_num_mask = 0x1f;
1157
1158         pegasus->features = usb_dev_id[dev_index].private;
1159 #ifdef  PEGASUS_USE_INTR
1160         get_interrupt_interval(pegasus);
1161 #endif
1162         if (reset_mac(pegasus)) {
1163                 err("can't reset MAC");
1164                 unregister_netdev(pegasus->net);
1165                 usb_free_urb(pegasus->tx_urb);
1166                 usb_free_urb(pegasus->rx_urb);
1167                 usb_free_urb(pegasus->ctrl_urb);
1168                 kfree(pegasus->net);
1169                 kfree(pegasus);
1170                 pegasus = NULL;
1171                 goto exit;
1172         }
1173
1174         info("%s: %s", net->name, usb_dev_id[dev_index].name);
1175
1176         set_ethernet_addr(pegasus);
1177
1178         if (pegasus->features & PEGASUS_II) {
1179                 info("setup Pegasus II specific registers");
1180                 setup_pegasus_II(pegasus);
1181         }
1182
1183         pegasus->phy = mii_phy_probe(pegasus);
1184         if (pegasus->phy == 0xff) {
1185                 warn("can't locate MII phy, using default");
1186                 pegasus->phy = 1;
1187         }
1188 exit:
1189         up(&pegasus->sem);
1190         
1191         return pegasus;
1192 }
1193
1194 static void pegasus_disconnect(struct usb_device *dev, void *ptr)
1195 {
1196         struct pegasus *pegasus = ptr;
1197
1198         if (!pegasus) {
1199                 warn("unregistering non-existant device");
1200                 return;
1201         }
1202
1203         pegasus->flags |= PEGASUS_UNPLUG;
1204         unregister_netdev(pegasus->net);
1205         usb_dec_dev_use(dev);
1206         usb_unlink_urb(pegasus->intr_urb);
1207         usb_unlink_urb(pegasus->tx_urb);
1208         usb_unlink_urb(pegasus->rx_urb);
1209         usb_unlink_urb(pegasus->ctrl_urb);
1210         usb_free_urb(pegasus->intr_urb);
1211         usb_free_urb(pegasus->tx_urb);
1212         usb_free_urb(pegasus->rx_urb);
1213         usb_free_urb(pegasus->ctrl_urb);
1214         kfree(pegasus->net);
1215         kfree(pegasus);
1216         pegasus = NULL;
1217 }
1218
1219 static struct usb_driver pegasus_driver = {
1220         name:           driver_name,
1221         probe:          pegasus_probe,
1222         disconnect:     pegasus_disconnect,
1223         id_table:       pegasus_ids,
1224 };
1225
1226 int __init pegasus_init(void)
1227 {
1228         info(DRIVER_VERSION ":" DRIVER_DESC);
1229         return usb_register(&pegasus_driver);
1230 }
1231
1232 void __exit pegasus_exit(void)
1233 {
1234         usb_deregister(&pegasus_driver);
1235 }
1236
1237 module_init(pegasus_init);
1238 module_exit(pegasus_exit);