2 * Copyright (c) 1999-2005 Petko Manolov (petkan@users.sourceforge.net)
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.
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
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.
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>
48 #define DRIVER_VERSION "v0.6.13 (2005/11/13)"
49 #define DRIVER_AUTHOR "Petko Manolov <petkan@users.sourceforge.net>"
50 #define DRIVER_DESC "Pegasus/Pegasus II USB Ethernet driver"
52 static const char driver_name[] = "pegasus";
54 #undef PEGASUS_WRITE_EEPROM
55 #define BMSR_MEDIA (BMSR_10HALF | BMSR_10FULL | BMSR_100HALF | \
56 BMSR_100FULL | BMSR_ANEGCAPABLE)
58 static int loopback = 0;
59 static int mii_mode = 0;
60 static char *devid=NULL;
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},
71 static struct usb_device_id pegasus_ids[] = {
72 #define PEGASUS_DEV(pn, vid, pid, flags) \
73 {.match_flags = USB_DEVICE_ID_MATCH_DEVICE, .idVendor = vid, .idProduct = pid},
80 MODULE_AUTHOR(DRIVER_AUTHOR);
81 MODULE_DESCRIPTION(DRIVER_DESC);
82 MODULE_LICENSE("GPL");
83 module_param(loopback, bool, 0);
84 module_param(mii_mode, bool, 0);
85 module_param(devid, charp, 0);
86 MODULE_PARM_DESC(loopback, "Enable MAC loopback mode (bit 0)");
87 MODULE_PARM_DESC(mii_mode, "Enable HomePNA mode (bit 0),default=MII mode = 0");
88 MODULE_PARM_DESC(devid, "The format is: 'DEV_name:VendorID:DeviceID:Flags'");
90 /* use ethtool to change the level for any given device */
91 static int msg_level = -1;
92 module_param (msg_level, int, 0);
93 MODULE_PARM_DESC (msg_level, "Override default message level");
95 MODULE_DEVICE_TABLE(usb, pegasus_ids);
97 static int update_eth_regs_async(pegasus_t *);
98 /* Aargh!!! I _really_ hate such tweaks */
99 static void ctrl_callback(struct urb *urb, struct pt_regs *regs)
101 pegasus_t *pegasus = urb->context;
106 switch (urb->status) {
108 if (pegasus->flags & ETH_REGS_CHANGE) {
109 pegasus->flags &= ~ETH_REGS_CHANGE;
110 pegasus->flags |= ETH_REGS_CHANGED;
111 update_eth_regs_async(pegasus);
120 if (netif_msg_drv(pegasus))
121 dev_dbg(&pegasus->intf->dev, "%s, status %d\n",
122 __FUNCTION__, urb->status);
124 pegasus->flags &= ~ETH_REGS_CHANGED;
125 wake_up(&pegasus->ctrl_wait);
128 static int get_registers(pegasus_t * pegasus, __u16 indx, __u16 size,
133 DECLARE_WAITQUEUE(wait, current);
135 buffer = kmalloc(size, GFP_KERNEL);
137 if (netif_msg_drv(pegasus))
138 dev_warn(&pegasus->intf->dev, "out of memory in %s\n",
142 add_wait_queue(&pegasus->ctrl_wait, &wait);
143 set_current_state(TASK_UNINTERRUPTIBLE);
144 while (pegasus->flags & ETH_REGS_CHANGED)
146 remove_wait_queue(&pegasus->ctrl_wait, &wait);
147 set_current_state(TASK_RUNNING);
149 pegasus->dr.bRequestType = PEGASUS_REQT_READ;
150 pegasus->dr.bRequest = PEGASUS_REQ_GET_REGS;
151 pegasus->dr.wValue = cpu_to_le16(0);
152 pegasus->dr.wIndex = cpu_to_le16p(&indx);
153 pegasus->dr.wLength = cpu_to_le16p(&size);
154 pegasus->ctrl_urb->transfer_buffer_length = size;
156 usb_fill_control_urb(pegasus->ctrl_urb, pegasus->usb,
157 usb_rcvctrlpipe(pegasus->usb, 0),
158 (char *) &pegasus->dr,
159 buffer, size, ctrl_callback, pegasus);
161 add_wait_queue(&pegasus->ctrl_wait, &wait);
162 set_current_state(TASK_UNINTERRUPTIBLE);
164 /* using ATOMIC, we'd never wake up if we slept */
165 if ((ret = usb_submit_urb(pegasus->ctrl_urb, GFP_ATOMIC))) {
166 if (netif_msg_drv(pegasus))
167 dev_err(&pegasus->intf->dev, "%s, status %d\n",
174 remove_wait_queue(&pegasus->ctrl_wait, &wait);
175 memcpy(data, buffer, size);
181 static int set_registers(pegasus_t * pegasus, __u16 indx, __u16 size,
186 DECLARE_WAITQUEUE(wait, current);
188 buffer = kmalloc(size, GFP_KERNEL);
190 if (netif_msg_drv(pegasus))
191 dev_warn(&pegasus->intf->dev, "out of memory in %s\n",
195 memcpy(buffer, data, size);
197 add_wait_queue(&pegasus->ctrl_wait, &wait);
198 set_current_state(TASK_UNINTERRUPTIBLE);
199 while (pegasus->flags & ETH_REGS_CHANGED)
201 remove_wait_queue(&pegasus->ctrl_wait, &wait);
202 set_current_state(TASK_RUNNING);
204 pegasus->dr.bRequestType = PEGASUS_REQT_WRITE;
205 pegasus->dr.bRequest = PEGASUS_REQ_SET_REGS;
206 pegasus->dr.wValue = cpu_to_le16(0);
207 pegasus->dr.wIndex = cpu_to_le16p(&indx);
208 pegasus->dr.wLength = cpu_to_le16p(&size);
209 pegasus->ctrl_urb->transfer_buffer_length = size;
211 usb_fill_control_urb(pegasus->ctrl_urb, pegasus->usb,
212 usb_sndctrlpipe(pegasus->usb, 0),
213 (char *) &pegasus->dr,
214 buffer, size, ctrl_callback, pegasus);
216 add_wait_queue(&pegasus->ctrl_wait, &wait);
217 set_current_state(TASK_UNINTERRUPTIBLE);
219 if ((ret = usb_submit_urb(pegasus->ctrl_urb, GFP_ATOMIC))) {
220 if (netif_msg_drv(pegasus))
221 dev_err(&pegasus->intf->dev, "%s, status %d\n",
228 remove_wait_queue(&pegasus->ctrl_wait, &wait);
234 static int set_register(pegasus_t * pegasus, __u16 indx, __u8 data)
238 DECLARE_WAITQUEUE(wait, current);
240 tmp = kmalloc(1, GFP_KERNEL);
242 if (netif_msg_drv(pegasus))
243 dev_warn(&pegasus->intf->dev, "out of memory in %s\n",
247 memcpy(tmp, &data, 1);
248 add_wait_queue(&pegasus->ctrl_wait, &wait);
249 set_current_state(TASK_UNINTERRUPTIBLE);
250 while (pegasus->flags & ETH_REGS_CHANGED)
252 remove_wait_queue(&pegasus->ctrl_wait, &wait);
253 set_current_state(TASK_RUNNING);
255 pegasus->dr.bRequestType = PEGASUS_REQT_WRITE;
256 pegasus->dr.bRequest = PEGASUS_REQ_SET_REG;
257 pegasus->dr.wValue = cpu_to_le16(data);
258 pegasus->dr.wIndex = cpu_to_le16p(&indx);
259 pegasus->dr.wLength = cpu_to_le16(1);
260 pegasus->ctrl_urb->transfer_buffer_length = 1;
262 usb_fill_control_urb(pegasus->ctrl_urb, pegasus->usb,
263 usb_sndctrlpipe(pegasus->usb, 0),
264 (char *) &pegasus->dr,
265 tmp, 1, ctrl_callback, pegasus);
267 add_wait_queue(&pegasus->ctrl_wait, &wait);
268 set_current_state(TASK_UNINTERRUPTIBLE);
270 if ((ret = usb_submit_urb(pegasus->ctrl_urb, GFP_ATOMIC))) {
271 if (netif_msg_drv(pegasus))
272 dev_err(&pegasus->intf->dev, "%s, status %d\n",
279 remove_wait_queue(&pegasus->ctrl_wait, &wait);
285 static int update_eth_regs_async(pegasus_t * pegasus)
289 pegasus->dr.bRequestType = PEGASUS_REQT_WRITE;
290 pegasus->dr.bRequest = PEGASUS_REQ_SET_REGS;
291 pegasus->dr.wValue = 0;
292 pegasus->dr.wIndex = cpu_to_le16(EthCtrl0);
293 pegasus->dr.wLength = cpu_to_le16(3);
294 pegasus->ctrl_urb->transfer_buffer_length = 3;
296 usb_fill_control_urb(pegasus->ctrl_urb, pegasus->usb,
297 usb_sndctrlpipe(pegasus->usb, 0),
298 (char *) &pegasus->dr,
299 pegasus->eth_regs, 3, ctrl_callback, pegasus);
301 if ((ret = usb_submit_urb(pegasus->ctrl_urb, GFP_ATOMIC)))
302 if (netif_msg_drv(pegasus))
303 dev_err(&pegasus->intf->dev, "%s, status %d\n",
309 static int read_mii_word(pegasus_t * pegasus, __u8 phy, __u8 indx, __u16 * regd)
312 __u8 data[4] = { phy, 0, 0, indx };
316 set_register(pegasus, PhyCtrl, 0);
317 set_registers(pegasus, PhyAddr, sizeof (data), data);
318 set_register(pegasus, PhyCtrl, (indx | PHY_READ));
319 for (i = 0; i < REG_TIMEOUT; i++) {
320 ret = get_registers(pegasus, PhyCtrl, 1, data);
321 if (ret == -ESHUTDOWN)
323 if (data[0] & PHY_DONE)
326 if (i < REG_TIMEOUT) {
327 ret = get_registers(pegasus, PhyData, 2, ®di);
328 *regd = le16_to_cpu(regdi);
332 if (netif_msg_drv(pegasus))
333 dev_warn(&pegasus->intf->dev, "fail %s\n", __FUNCTION__);
338 static int mdio_read(struct net_device *dev, int phy_id, int loc)
340 pegasus_t *pegasus = (pegasus_t *) netdev_priv(dev);
343 read_mii_word(pegasus, phy_id, loc, &res);
347 static int write_mii_word(pegasus_t * pegasus, __u8 phy, __u8 indx, __u16 regd)
350 __u8 data[4] = { phy, 0, 0, indx };
354 data[2] = (u8) (regd >> 8);
355 set_register(pegasus, PhyCtrl, 0);
356 set_registers(pegasus, PhyAddr, sizeof(data), data);
357 set_register(pegasus, PhyCtrl, (indx | PHY_WRITE));
358 for (i = 0; i < REG_TIMEOUT; i++) {
359 ret = get_registers(pegasus, PhyCtrl, 1, data);
360 if (ret == -ESHUTDOWN)
362 if (data[0] & PHY_DONE)
369 if (netif_msg_drv(pegasus))
370 dev_warn(&pegasus->intf->dev, "fail %s\n", __FUNCTION__);
374 static void mdio_write(struct net_device *dev, int phy_id, int loc, int val)
376 pegasus_t *pegasus = (pegasus_t *) netdev_priv(dev);
378 write_mii_word(pegasus, phy_id, loc, val);
381 static int read_eprom_word(pegasus_t * pegasus, __u8 index, __u16 * retdata)
388 set_register(pegasus, EpromCtrl, 0);
389 set_register(pegasus, EpromOffset, index);
390 set_register(pegasus, EpromCtrl, EPROM_READ);
392 for (i = 0; i < REG_TIMEOUT; i++) {
393 ret = get_registers(pegasus, EpromCtrl, 1, &tmp);
394 if (tmp & EPROM_DONE)
396 if (ret == -ESHUTDOWN)
399 if (i < REG_TIMEOUT) {
400 ret = get_registers(pegasus, EpromData, 2, &retdatai);
401 *retdata = le16_to_cpu(retdatai);
406 if (netif_msg_drv(pegasus))
407 dev_warn(&pegasus->intf->dev, "fail %s\n", __FUNCTION__);
411 #ifdef PEGASUS_WRITE_EEPROM
412 static inline void enable_eprom_write(pegasus_t * pegasus)
417 get_registers(pegasus, EthCtrl2, 1, &tmp);
418 set_register(pegasus, EthCtrl2, tmp | EPROM_WR_ENABLE);
421 static inline void disable_eprom_write(pegasus_t * pegasus)
426 get_registers(pegasus, EthCtrl2, 1, &tmp);
427 set_register(pegasus, EpromCtrl, 0);
428 set_register(pegasus, EthCtrl2, tmp & ~EPROM_WR_ENABLE);
431 static int write_eprom_word(pegasus_t * pegasus, __u8 index, __u16 data)
434 __u8 tmp, d[4] = { 0x3f, 0, 0, EPROM_WRITE };
437 set_registers(pegasus, EpromOffset, 4, d);
438 enable_eprom_write(pegasus);
439 set_register(pegasus, EpromOffset, index);
440 set_registers(pegasus, EpromData, 2, &data);
441 set_register(pegasus, EpromCtrl, EPROM_WRITE);
443 for (i = 0; i < REG_TIMEOUT; i++) {
444 ret = get_registers(pegasus, EpromCtrl, 1, &tmp);
445 if (ret == -ESHUTDOWN)
447 if (tmp & EPROM_DONE)
450 disable_eprom_write(pegasus);
454 if (netif_msg_drv(pegasus))
455 dev_warn(&pegasus->intf->dev, "fail %s\n", __FUNCTION__);
458 #endif /* PEGASUS_WRITE_EEPROM */
460 static inline void get_node_id(pegasus_t * pegasus, __u8 * id)
465 for (i = 0; i < 3; i++) {
466 read_eprom_word(pegasus, i, &w16);
467 ((__le16 *) id)[i] = cpu_to_le16p(&w16);
471 static void set_ethernet_addr(pegasus_t * pegasus)
475 get_node_id(pegasus, node_id);
476 set_registers(pegasus, EthID, sizeof (node_id), node_id);
477 memcpy(pegasus->net->dev_addr, node_id, sizeof (node_id));
480 static inline int reset_mac(pegasus_t * pegasus)
485 set_register(pegasus, EthCtrl1, data);
486 for (i = 0; i < REG_TIMEOUT; i++) {
487 get_registers(pegasus, EthCtrl1, 1, &data);
491 if (mii_mode && (pegasus->features & HAS_HOME_PNA))
492 set_register(pegasus, Gpio1, 0x34);
494 set_register(pegasus, Gpio1, 0x26);
495 set_register(pegasus, Gpio0, pegasus->features);
496 set_register(pegasus, Gpio0, DEFAULT_GPIO_SET);
500 if (i == REG_TIMEOUT)
503 if (usb_dev_id[pegasus->dev_index].vendor == VENDOR_LINKSYS ||
504 usb_dev_id[pegasus->dev_index].vendor == VENDOR_DLINK) {
505 set_register(pegasus, Gpio0, 0x24);
506 set_register(pegasus, Gpio0, 0x26);
508 if (usb_dev_id[pegasus->dev_index].vendor == VENDOR_ELCON) {
510 read_mii_word(pegasus, 3, 0x1b, &auxmode);
511 write_mii_word(pegasus, 3, 0x1b, auxmode | 4);
517 static int enable_net_traffic(struct net_device *dev, struct usb_device *usb)
521 pegasus_t *pegasus = netdev_priv(dev);
524 read_mii_word(pegasus, pegasus->phy, MII_LPA, &linkpart);
527 if (linkpart & (ADVERTISE_100FULL | ADVERTISE_10FULL))
528 data[1] |= 0x20; /* set full duplex */
529 if (linkpart & (ADVERTISE_100FULL | ADVERTISE_100HALF))
530 data[1] |= 0x10; /* set 100 Mbps */
533 data[2] = (loopback & 1) ? 0x09 : 0x01;
535 memcpy(pegasus->eth_regs, data, sizeof (data));
536 ret = set_registers(pegasus, EthCtrl0, 3, data);
538 if (usb_dev_id[pegasus->dev_index].vendor == VENDOR_LINKSYS ||
539 usb_dev_id[pegasus->dev_index].vendor == VENDOR_LINKSYS2 ||
540 usb_dev_id[pegasus->dev_index].vendor == VENDOR_DLINK) {
542 read_mii_word(pegasus, 0, 0x1b, &auxmode);
543 write_mii_word(pegasus, 0, 0x1b, auxmode | 4);
549 static void fill_skb_pool(pegasus_t * pegasus)
553 for (i = 0; i < RX_SKBS; i++) {
554 if (pegasus->rx_pool[i])
556 pegasus->rx_pool[i] = dev_alloc_skb(PEGASUS_MTU + 2);
558 ** we give up if the allocation fail. the tasklet will be
559 ** rescheduled again anyway...
561 if (pegasus->rx_pool[i] == NULL)
563 pegasus->rx_pool[i]->dev = pegasus->net;
564 skb_reserve(pegasus->rx_pool[i], 2);
568 static void free_skb_pool(pegasus_t * pegasus)
572 for (i = 0; i < RX_SKBS; i++) {
573 if (pegasus->rx_pool[i]) {
574 dev_kfree_skb(pegasus->rx_pool[i]);
575 pegasus->rx_pool[i] = NULL;
580 static inline struct sk_buff *pull_skb(pegasus_t * pegasus)
585 for (i = 0; i < RX_SKBS; i++) {
586 if (likely(pegasus->rx_pool[i] != NULL)) {
587 skb = pegasus->rx_pool[i];
588 pegasus->rx_pool[i] = NULL;
595 static void read_bulk_callback(struct urb *urb, struct pt_regs *regs)
597 pegasus_t *pegasus = urb->context;
598 struct net_device *net;
599 int rx_status, count = urb->actual_length;
600 u8 *buf = urb->transfer_buffer;
607 if (!netif_device_present(net) || !netif_running(net))
610 switch (urb->status) {
614 if (netif_msg_rx_err(pegasus))
615 pr_debug("%s: reset MAC\n", net->name);
616 pegasus->flags &= ~PEGASUS_RX_BUSY;
618 case -EPIPE: /* stall, or disconnect from TT */
619 /* FIXME schedule work to clear the halt */
620 if (netif_msg_rx_err(pegasus))
621 printk(KERN_WARNING "%s: no rx stall recovery\n",
627 if (netif_msg_ifdown(pegasus))
628 pr_debug("%s: rx unlink, %d\n", net->name, urb->status);
631 if (netif_msg_rx_err(pegasus))
632 pr_debug("%s: RX status %d\n", net->name, urb->status);
636 if (!count || count < 4)
639 rx_status = buf[count - 2];
640 if (rx_status & 0x1e) {
641 if (netif_msg_rx_err(pegasus))
642 pr_debug("%s: RX packet error %x\n",
643 net->name, rx_status);
644 pegasus->stats.rx_errors++;
645 if (rx_status & 0x06) // long or runt
646 pegasus->stats.rx_length_errors++;
647 if (rx_status & 0x08)
648 pegasus->stats.rx_crc_errors++;
649 if (rx_status & 0x10) // extra bits
650 pegasus->stats.rx_frame_errors++;
653 if (pegasus->chip == 0x8513) {
654 pkt_len = le32_to_cpu(*(__le32 *)urb->transfer_buffer);
656 pegasus->rx_skb->data += 2;
658 pkt_len = buf[count - 3] << 8;
659 pkt_len += buf[count - 4];
665 * If the packet is unreasonably long, quietly drop it rather than
666 * kernel panicing by calling skb_put.
668 if (pkt_len > PEGASUS_MTU)
672 * at this point we are sure pegasus->rx_skb != NULL
673 * so we go ahead and pass up the packet.
675 skb_put(pegasus->rx_skb, pkt_len);
676 pegasus->rx_skb->protocol = eth_type_trans(pegasus->rx_skb, net);
677 netif_rx(pegasus->rx_skb);
678 pegasus->stats.rx_packets++;
679 pegasus->stats.rx_bytes += pkt_len;
681 if (pegasus->flags & PEGASUS_UNPLUG)
684 spin_lock(&pegasus->rx_pool_lock);
685 pegasus->rx_skb = pull_skb(pegasus);
686 spin_unlock(&pegasus->rx_pool_lock);
688 if (pegasus->rx_skb == NULL)
691 usb_fill_bulk_urb(pegasus->rx_urb, pegasus->usb,
692 usb_rcvbulkpipe(pegasus->usb, 1),
693 pegasus->rx_skb->data, PEGASUS_MTU + 8,
694 read_bulk_callback, pegasus);
695 if (usb_submit_urb(pegasus->rx_urb, GFP_ATOMIC)) {
696 pegasus->flags |= PEGASUS_RX_URB_FAIL;
699 pegasus->flags &= ~PEGASUS_RX_URB_FAIL;
705 tasklet_schedule(&pegasus->rx_tl);
708 static void rx_fixup(unsigned long data)
713 pegasus = (pegasus_t *) data;
714 if (pegasus->flags & PEGASUS_UNPLUG)
717 spin_lock_irqsave(&pegasus->rx_pool_lock, flags);
718 fill_skb_pool(pegasus);
719 if (pegasus->flags & PEGASUS_RX_URB_FAIL)
722 if (pegasus->rx_skb == NULL) {
723 pegasus->rx_skb = pull_skb(pegasus);
725 if (pegasus->rx_skb == NULL) {
726 if (netif_msg_rx_err(pegasus))
727 printk(KERN_WARNING "%s: low on memory\n",
729 tasklet_schedule(&pegasus->rx_tl);
732 usb_fill_bulk_urb(pegasus->rx_urb, pegasus->usb,
733 usb_rcvbulkpipe(pegasus->usb, 1),
734 pegasus->rx_skb->data, PEGASUS_MTU + 8,
735 read_bulk_callback, pegasus);
737 if (usb_submit_urb(pegasus->rx_urb, GFP_ATOMIC)) {
738 pegasus->flags |= PEGASUS_RX_URB_FAIL;
739 tasklet_schedule(&pegasus->rx_tl);
741 pegasus->flags &= ~PEGASUS_RX_URB_FAIL;
744 spin_unlock_irqrestore(&pegasus->rx_pool_lock, flags);
747 static void write_bulk_callback(struct urb *urb, struct pt_regs *regs)
749 pegasus_t *pegasus = urb->context;
750 struct net_device *net = pegasus->net;
755 if (!netif_device_present(net) || !netif_running(net))
758 switch (urb->status) {
760 /* FIXME schedule_work() to clear the tx halt */
761 netif_stop_queue(net);
762 if (netif_msg_tx_err(pegasus))
763 printk(KERN_WARNING "%s: no tx stall recovery\n",
769 if (netif_msg_ifdown(pegasus))
770 pr_debug("%s: tx unlink, %d\n", net->name, urb->status);
773 if (netif_msg_tx_err(pegasus))
774 pr_info("%s: TX status %d\n", net->name, urb->status);
780 net->trans_start = jiffies;
781 netif_wake_queue(net);
784 static void intr_callback(struct urb *urb, struct pt_regs *regs)
786 pegasus_t *pegasus = urb->context;
787 struct net_device *net;
794 switch (urb->status) {
797 case -ECONNRESET: /* unlink */
802 /* some Pegasus-I products report LOTS of data
803 * toggle errors... avoid log spamming
805 if (netif_msg_timer(pegasus))
806 pr_debug("%s: intr status %d\n", net->name,
810 if (urb->actual_length >= 6) {
811 u8 * d = urb->transfer_buffer;
813 /* byte 0 == tx_status1, reg 2B */
814 if (d[0] & (TX_UNDERRUN|EXCESSIVE_COL
815 |LATE_COL|JABBER_TIMEOUT)) {
816 pegasus->stats.tx_errors++;
817 if (d[0] & TX_UNDERRUN)
818 pegasus->stats.tx_fifo_errors++;
819 if (d[0] & (EXCESSIVE_COL | JABBER_TIMEOUT))
820 pegasus->stats.tx_aborted_errors++;
822 pegasus->stats.tx_window_errors++;
825 /* d[5].LINK_STATUS lies on some adapters.
826 * d[0].NO_CARRIER kicks in only with failed TX.
827 * ... so monitoring with MII may be safest.
829 if (d[0] & NO_CARRIER)
830 netif_carrier_off(net);
832 netif_carrier_on(net);
834 /* bytes 3-4 == rx_lostpkt, reg 2E/2F */
835 pegasus->stats.rx_missed_errors += ((d[3] & 0x7f) << 8) | d[4];
838 status = usb_submit_urb(urb, SLAB_ATOMIC);
839 if (status && netif_msg_timer(pegasus))
840 printk(KERN_ERR "%s: can't resubmit interrupt urb, %d\n",
844 static void pegasus_tx_timeout(struct net_device *net)
846 pegasus_t *pegasus = netdev_priv(net);
847 if (netif_msg_timer(pegasus))
848 printk(KERN_WARNING "%s: tx timeout\n", net->name);
849 usb_unlink_urb(pegasus->tx_urb);
850 pegasus->stats.tx_errors++;
853 static int pegasus_start_xmit(struct sk_buff *skb, struct net_device *net)
855 pegasus_t *pegasus = netdev_priv(net);
856 int count = ((skb->len + 2) & 0x3f) ? skb->len + 2 : skb->len + 3;
858 __u16 l16 = skb->len;
860 netif_stop_queue(net);
862 ((__le16 *) pegasus->tx_buff)[0] = cpu_to_le16(l16);
863 memcpy(pegasus->tx_buff + 2, skb->data, skb->len);
864 usb_fill_bulk_urb(pegasus->tx_urb, pegasus->usb,
865 usb_sndbulkpipe(pegasus->usb, 2),
866 pegasus->tx_buff, count,
867 write_bulk_callback, pegasus);
868 if ((res = usb_submit_urb(pegasus->tx_urb, GFP_ATOMIC))) {
869 if (netif_msg_tx_err(pegasus))
870 printk(KERN_WARNING "%s: fail tx, %d\n",
873 case -EPIPE: /* stall, or disconnect from TT */
874 /* cleanup should already have been scheduled */
876 case -ENODEV: /* disconnect() upcoming */
879 pegasus->stats.tx_errors++;
880 netif_start_queue(net);
883 pegasus->stats.tx_packets++;
884 pegasus->stats.tx_bytes += skb->len;
885 net->trans_start = jiffies;
892 static struct net_device_stats *pegasus_netdev_stats(struct net_device *dev)
894 return &((pegasus_t *) netdev_priv(dev))->stats;
897 static inline void disable_net_traffic(pegasus_t * pegasus)
901 set_registers(pegasus, EthCtrl0, 2, &tmp);
904 static inline void get_interrupt_interval(pegasus_t * pegasus)
908 read_eprom_word(pegasus, 4, (__u16 *) data);
909 if (pegasus->usb->speed != USB_SPEED_HIGH) {
910 if (data[1] < 0x80) {
911 if (netif_msg_timer(pegasus))
912 dev_info(&pegasus->intf->dev, "intr interval "
913 "changed from %ums to %ums\n",
916 #ifdef PEGASUS_WRITE_EEPROM
917 write_eprom_word(pegasus, 4, *(__u16 *) data);
921 pegasus->intr_interval = data[1];
924 static void set_carrier(struct net_device *net)
926 pegasus_t *pegasus = netdev_priv(net);
929 if (!read_mii_word(pegasus, pegasus->phy, MII_BMSR, &tmp))
932 if (tmp & BMSR_LSTATUS)
933 netif_carrier_on(net);
935 netif_carrier_off(net);
938 static void free_all_urbs(pegasus_t * pegasus)
940 usb_free_urb(pegasus->intr_urb);
941 usb_free_urb(pegasus->tx_urb);
942 usb_free_urb(pegasus->rx_urb);
943 usb_free_urb(pegasus->ctrl_urb);
946 static void unlink_all_urbs(pegasus_t * pegasus)
948 usb_kill_urb(pegasus->intr_urb);
949 usb_kill_urb(pegasus->tx_urb);
950 usb_kill_urb(pegasus->rx_urb);
951 usb_kill_urb(pegasus->ctrl_urb);
954 static int alloc_urbs(pegasus_t * pegasus)
956 pegasus->ctrl_urb = usb_alloc_urb(0, GFP_KERNEL);
957 if (!pegasus->ctrl_urb) {
960 pegasus->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
961 if (!pegasus->rx_urb) {
962 usb_free_urb(pegasus->ctrl_urb);
965 pegasus->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
966 if (!pegasus->tx_urb) {
967 usb_free_urb(pegasus->rx_urb);
968 usb_free_urb(pegasus->ctrl_urb);
971 pegasus->intr_urb = usb_alloc_urb(0, GFP_KERNEL);
972 if (!pegasus->intr_urb) {
973 usb_free_urb(pegasus->tx_urb);
974 usb_free_urb(pegasus->rx_urb);
975 usb_free_urb(pegasus->ctrl_urb);
982 static int pegasus_open(struct net_device *net)
984 pegasus_t *pegasus = netdev_priv(net);
987 if (pegasus->rx_skb == NULL)
988 pegasus->rx_skb = pull_skb(pegasus);
990 ** Note: no point to free the pool. it is empty :-)
992 if (!pegasus->rx_skb)
995 res = set_registers(pegasus, EthID, 6, net->dev_addr);
997 usb_fill_bulk_urb(pegasus->rx_urb, pegasus->usb,
998 usb_rcvbulkpipe(pegasus->usb, 1),
999 pegasus->rx_skb->data, PEGASUS_MTU + 8,
1000 read_bulk_callback, pegasus);
1001 if ((res = usb_submit_urb(pegasus->rx_urb, GFP_KERNEL))) {
1002 if (netif_msg_ifup(pegasus))
1003 pr_debug("%s: failed rx_urb, %d", net->name, res);
1007 usb_fill_int_urb(pegasus->intr_urb, pegasus->usb,
1008 usb_rcvintpipe(pegasus->usb, 3),
1009 pegasus->intr_buff, sizeof (pegasus->intr_buff),
1010 intr_callback, pegasus, pegasus->intr_interval);
1011 if ((res = usb_submit_urb(pegasus->intr_urb, GFP_KERNEL))) {
1012 if (netif_msg_ifup(pegasus))
1013 pr_debug("%s: failed intr_urb, %d\n", net->name, res);
1014 usb_kill_urb(pegasus->rx_urb);
1017 if ((res = enable_net_traffic(net, pegasus->usb))) {
1018 if (netif_msg_ifup(pegasus))
1019 pr_debug("%s: can't enable_net_traffic() - %d\n",
1022 usb_kill_urb(pegasus->rx_urb);
1023 usb_kill_urb(pegasus->intr_urb);
1024 free_skb_pool(pegasus);
1028 netif_start_queue(net);
1029 if (netif_msg_ifup(pegasus))
1030 pr_debug("%s: open\n", net->name);
1036 static int pegasus_close(struct net_device *net)
1038 pegasus_t *pegasus = netdev_priv(net);
1040 netif_stop_queue(net);
1041 if (!(pegasus->flags & PEGASUS_UNPLUG))
1042 disable_net_traffic(pegasus);
1043 tasklet_kill(&pegasus->rx_tl);
1044 unlink_all_urbs(pegasus);
1049 static void pegasus_get_drvinfo(struct net_device *dev,
1050 struct ethtool_drvinfo *info)
1052 pegasus_t *pegasus = netdev_priv(dev);
1053 strncpy(info->driver, driver_name, sizeof (info->driver) - 1);
1054 strncpy(info->version, DRIVER_VERSION, sizeof (info->version) - 1);
1055 usb_make_path(pegasus->usb, info->bus_info, sizeof (info->bus_info));
1058 /* also handles three patterns of some kind in hardware */
1059 #define WOL_SUPPORTED (WAKE_MAGIC|WAKE_PHY)
1062 pegasus_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1064 pegasus_t *pegasus = netdev_priv(dev);
1066 wol->supported = WAKE_MAGIC | WAKE_PHY;
1067 wol->wolopts = pegasus->wolopts;
1071 pegasus_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1073 pegasus_t *pegasus = netdev_priv(dev);
1076 if (wol->wolopts & ~WOL_SUPPORTED)
1079 if (wol->wolopts & WAKE_MAGIC)
1081 if (wol->wolopts & WAKE_PHY)
1083 /* FIXME this 0x10 bit still needs to get set in the chip... */
1085 pegasus->eth_regs[0] |= 0x10;
1087 pegasus->eth_regs[0] &= ~0x10;
1088 pegasus->wolopts = wol->wolopts;
1089 return set_register(pegasus, WakeupControl, reg78);
1092 static inline void pegasus_reset_wol(struct net_device *dev)
1094 struct ethtool_wolinfo wol;
1096 memset(&wol, 0, sizeof wol);
1097 (void) pegasus_set_wol(dev, &wol);
1101 pegasus_get_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
1108 pegasus = netdev_priv(dev);
1109 mii_ethtool_gset(&pegasus->mii, ecmd);
1115 pegasus_set_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
1117 pegasus_t *pegasus = netdev_priv(dev);
1118 return mii_ethtool_sset(&pegasus->mii, ecmd);
1121 static int pegasus_nway_reset(struct net_device *dev)
1123 pegasus_t *pegasus = netdev_priv(dev);
1124 return mii_nway_restart(&pegasus->mii);
1127 static u32 pegasus_get_link(struct net_device *dev)
1129 pegasus_t *pegasus = netdev_priv(dev);
1130 return mii_link_ok(&pegasus->mii);
1133 static u32 pegasus_get_msglevel(struct net_device *dev)
1135 pegasus_t *pegasus = netdev_priv(dev);
1136 return pegasus->msg_enable;
1139 static void pegasus_set_msglevel(struct net_device *dev, u32 v)
1141 pegasus_t *pegasus = netdev_priv(dev);
1142 pegasus->msg_enable = v;
1145 static struct ethtool_ops ops = {
1146 .get_drvinfo = pegasus_get_drvinfo,
1147 .get_settings = pegasus_get_settings,
1148 .set_settings = pegasus_set_settings,
1149 .nway_reset = pegasus_nway_reset,
1150 .get_link = pegasus_get_link,
1151 .get_msglevel = pegasus_get_msglevel,
1152 .set_msglevel = pegasus_set_msglevel,
1153 .get_wol = pegasus_get_wol,
1154 .set_wol = pegasus_set_wol,
1157 static int pegasus_ioctl(struct net_device *net, struct ifreq *rq, int cmd)
1159 __u16 *data = (__u16 *) & rq->ifr_ifru;
1160 pegasus_t *pegasus = netdev_priv(net);
1164 case SIOCDEVPRIVATE:
1165 data[0] = pegasus->phy;
1166 case SIOCDEVPRIVATE + 1:
1167 read_mii_word(pegasus, data[0], data[1] & 0x1f, &data[3]);
1170 case SIOCDEVPRIVATE + 2:
1171 if (!capable(CAP_NET_ADMIN))
1173 write_mii_word(pegasus, pegasus->phy, data[1] & 0x1f, data[2]);
1182 static void pegasus_set_multicast(struct net_device *net)
1184 pegasus_t *pegasus = netdev_priv(net);
1186 if (net->flags & IFF_PROMISC) {
1187 pegasus->eth_regs[EthCtrl2] |= RX_PROMISCUOUS;
1188 if (netif_msg_link(pegasus))
1189 pr_info("%s: Promiscuous mode enabled.\n", net->name);
1190 } else if (net->mc_count ||
1191 (net->flags & IFF_ALLMULTI)) {
1192 pegasus->eth_regs[EthCtrl0] |= RX_MULTICAST;
1193 pegasus->eth_regs[EthCtrl2] &= ~RX_PROMISCUOUS;
1194 if (netif_msg_link(pegasus))
1195 pr_info("%s: set allmulti\n", net->name);
1197 pegasus->eth_regs[EthCtrl0] &= ~RX_MULTICAST;
1198 pegasus->eth_regs[EthCtrl2] &= ~RX_PROMISCUOUS;
1201 pegasus->flags |= ETH_REGS_CHANGE;
1202 ctrl_callback(pegasus->ctrl_urb, NULL);
1205 static __u8 mii_phy_probe(pegasus_t * pegasus)
1210 for (i = 0; i < 32; i++) {
1211 read_mii_word(pegasus, i, MII_BMSR, &tmp);
1212 if (tmp == 0 || tmp == 0xffff || (tmp & BMSR_MEDIA) == 0)
1221 static inline void setup_pegasus_II(pegasus_t * pegasus)
1225 set_register(pegasus, Reg1d, 0);
1226 set_register(pegasus, Reg7b, 1);
1228 if ((pegasus->features & HAS_HOME_PNA) && mii_mode)
1229 set_register(pegasus, Reg7b, 0);
1231 set_register(pegasus, Reg7b, 2);
1233 set_register(pegasus, 0x83, data);
1234 get_registers(pegasus, 0x83, 1, &data);
1237 pegasus->chip = 0x8513;
1242 set_register(pegasus, 0x80, 0xc0);
1243 set_register(pegasus, 0x83, 0xff);
1244 set_register(pegasus, 0x84, 0x01);
1246 if (pegasus->features & HAS_HOME_PNA && mii_mode)
1247 set_register(pegasus, Reg81, 6);
1249 set_register(pegasus, Reg81, 2);
1253 static struct workqueue_struct *pegasus_workqueue = NULL;
1254 #define CARRIER_CHECK_DELAY (2 * HZ)
1256 static void check_carrier(void *data)
1258 pegasus_t *pegasus = data;
1259 set_carrier(pegasus->net);
1260 if (!(pegasus->flags & PEGASUS_UNPLUG)) {
1261 queue_delayed_work(pegasus_workqueue, &pegasus->carrier_check,
1262 CARRIER_CHECK_DELAY);
1266 static int pegasus_probe(struct usb_interface *intf,
1267 const struct usb_device_id *id)
1269 struct usb_device *dev = interface_to_usbdev(intf);
1270 struct net_device *net;
1272 int dev_index = id - pegasus_ids;
1276 net = alloc_etherdev(sizeof(struct pegasus));
1278 dev_err(&intf->dev, "can't allocate %s\n", "device");
1282 pegasus = netdev_priv(net);
1283 memset(pegasus, 0, sizeof (struct pegasus));
1284 pegasus->dev_index = dev_index;
1285 init_waitqueue_head(&pegasus->ctrl_wait);
1287 if (!alloc_urbs(pegasus)) {
1288 dev_err(&intf->dev, "can't allocate %s\n", "urbs");
1292 tasklet_init(&pegasus->rx_tl, rx_fixup, (unsigned long) pegasus);
1294 INIT_WORK(&pegasus->carrier_check, check_carrier, pegasus);
1296 pegasus->intf = intf;
1299 SET_MODULE_OWNER(net);
1300 net->open = pegasus_open;
1301 net->stop = pegasus_close;
1302 net->watchdog_timeo = PEGASUS_TX_TIMEOUT;
1303 net->tx_timeout = pegasus_tx_timeout;
1304 net->do_ioctl = pegasus_ioctl;
1305 net->hard_start_xmit = pegasus_start_xmit;
1306 net->set_multicast_list = pegasus_set_multicast;
1307 net->get_stats = pegasus_netdev_stats;
1308 SET_ETHTOOL_OPS(net, &ops);
1309 pegasus->mii.dev = net;
1310 pegasus->mii.mdio_read = mdio_read;
1311 pegasus->mii.mdio_write = mdio_write;
1312 pegasus->mii.phy_id_mask = 0x1f;
1313 pegasus->mii.reg_num_mask = 0x1f;
1314 spin_lock_init(&pegasus->rx_pool_lock);
1315 pegasus->msg_enable = netif_msg_init (msg_level, NETIF_MSG_DRV
1316 | NETIF_MSG_PROBE | NETIF_MSG_LINK);
1318 pegasus->features = usb_dev_id[dev_index].private;
1319 get_interrupt_interval(pegasus);
1320 if (reset_mac(pegasus)) {
1321 dev_err(&intf->dev, "can't reset MAC\n");
1325 set_ethernet_addr(pegasus);
1326 fill_skb_pool(pegasus);
1327 if (pegasus->features & PEGASUS_II) {
1328 dev_info(&intf->dev, "setup Pegasus II specific registers\n");
1329 setup_pegasus_II(pegasus);
1331 pegasus->phy = mii_phy_probe(pegasus);
1332 if (pegasus->phy == 0xff) {
1333 dev_warn(&intf->dev, "can't locate MII phy, using default\n");
1336 pegasus->mii.phy_id = pegasus->phy;
1337 usb_set_intfdata(intf, pegasus);
1338 SET_NETDEV_DEV(net, &intf->dev);
1339 pegasus_reset_wol(net);
1340 res = register_netdev(net);
1343 queue_delayed_work(pegasus_workqueue, &pegasus->carrier_check,
1344 CARRIER_CHECK_DELAY);
1346 dev_info(&intf->dev, "%s, %s, %02x:%02x:%02x:%02x:%02x:%02x\n",
1348 usb_dev_id[dev_index].name,
1349 net->dev_addr [0], net->dev_addr [1],
1350 net->dev_addr [2], net->dev_addr [3],
1351 net->dev_addr [4], net->dev_addr [5]);
1355 usb_set_intfdata(intf, NULL);
1356 free_skb_pool(pegasus);
1358 free_all_urbs(pegasus);
1366 static void pegasus_disconnect(struct usb_interface *intf)
1368 struct pegasus *pegasus = usb_get_intfdata(intf);
1370 usb_set_intfdata(intf, NULL);
1372 dev_dbg(&intf->dev, "unregistering non-bound device?\n");
1376 pegasus->flags |= PEGASUS_UNPLUG;
1377 cancel_delayed_work(&pegasus->carrier_check);
1378 unregister_netdev(pegasus->net);
1379 usb_put_dev(interface_to_usbdev(intf));
1380 unlink_all_urbs(pegasus);
1381 free_all_urbs(pegasus);
1382 free_skb_pool(pegasus);
1383 if (pegasus->rx_skb)
1384 dev_kfree_skb(pegasus->rx_skb);
1385 free_netdev(pegasus->net);
1388 static int pegasus_suspend (struct usb_interface *intf, pm_message_t message)
1390 struct pegasus *pegasus = usb_get_intfdata(intf);
1392 netif_device_detach (pegasus->net);
1393 cancel_delayed_work(&pegasus->carrier_check);
1394 if (netif_running(pegasus->net)) {
1395 usb_kill_urb(pegasus->rx_urb);
1396 usb_kill_urb(pegasus->intr_urb);
1401 static int pegasus_resume (struct usb_interface *intf)
1403 struct pegasus *pegasus = usb_get_intfdata(intf);
1405 netif_device_attach (pegasus->net);
1406 if (netif_running(pegasus->net)) {
1407 pegasus->rx_urb->status = 0;
1408 pegasus->rx_urb->actual_length = 0;
1409 read_bulk_callback(pegasus->rx_urb, NULL);
1411 pegasus->intr_urb->status = 0;
1412 pegasus->intr_urb->actual_length = 0;
1413 intr_callback(pegasus->intr_urb, NULL);
1415 queue_delayed_work(pegasus_workqueue, &pegasus->carrier_check,
1416 CARRIER_CHECK_DELAY);
1420 static struct usb_driver pegasus_driver = {
1421 .name = driver_name,
1422 .probe = pegasus_probe,
1423 .disconnect = pegasus_disconnect,
1424 .id_table = pegasus_ids,
1425 .suspend = pegasus_suspend,
1426 .resume = pegasus_resume,
1429 static void parse_id(char *id)
1431 unsigned int vendor_id=0, device_id=0, flags=0, i=0;
1432 char *token, *name=NULL;
1434 if ((token = strsep(&id, ":")) != NULL)
1436 /* name now points to a null terminated string*/
1437 if ((token = strsep(&id, ":")) != NULL)
1438 vendor_id = simple_strtoul(token, NULL, 16);
1439 if ((token = strsep(&id, ":")) != NULL)
1440 device_id = simple_strtoul(token, NULL, 16);
1441 flags = simple_strtoul(id, NULL, 16);
1442 pr_info("%s: new device %s, vendor ID 0x%04x, device ID 0x%04x, flags: 0x%x\n",
1443 driver_name, name, vendor_id, device_id, flags);
1445 if (vendor_id > 0x10000 || vendor_id == 0)
1447 if (device_id > 0x10000 || device_id == 0)
1450 for (i=0; usb_dev_id[i].name; i++);
1451 usb_dev_id[i].name = name;
1452 usb_dev_id[i].vendor = vendor_id;
1453 usb_dev_id[i].device = device_id;
1454 usb_dev_id[i].private = flags;
1455 pegasus_ids[i].match_flags = USB_DEVICE_ID_MATCH_DEVICE;
1456 pegasus_ids[i].idVendor = vendor_id;
1457 pegasus_ids[i].idProduct = device_id;
1460 static int __init pegasus_init(void)
1462 pr_info("%s: %s, " DRIVER_DESC "\n", driver_name, DRIVER_VERSION);
1465 pegasus_workqueue = create_singlethread_workqueue("pegasus");
1466 if (!pegasus_workqueue)
1468 return usb_register(&pegasus_driver);
1471 static void __exit pegasus_exit(void)
1473 destroy_workqueue(pegasus_workqueue);
1474 usb_deregister(&pegasus_driver);
1477 module_init(pegasus_init);
1478 module_exit(pegasus_exit);