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))) {
167 netif_device_detach(pegasus->net);
168 if (netif_msg_drv(pegasus))
169 dev_err(&pegasus->intf->dev, "%s, status %d\n",
176 remove_wait_queue(&pegasus->ctrl_wait, &wait);
177 memcpy(data, buffer, size);
183 static int set_registers(pegasus_t * pegasus, __u16 indx, __u16 size,
188 DECLARE_WAITQUEUE(wait, current);
190 buffer = kmalloc(size, GFP_KERNEL);
192 if (netif_msg_drv(pegasus))
193 dev_warn(&pegasus->intf->dev, "out of memory in %s\n",
197 memcpy(buffer, data, size);
199 add_wait_queue(&pegasus->ctrl_wait, &wait);
200 set_current_state(TASK_UNINTERRUPTIBLE);
201 while (pegasus->flags & ETH_REGS_CHANGED)
203 remove_wait_queue(&pegasus->ctrl_wait, &wait);
204 set_current_state(TASK_RUNNING);
206 pegasus->dr.bRequestType = PEGASUS_REQT_WRITE;
207 pegasus->dr.bRequest = PEGASUS_REQ_SET_REGS;
208 pegasus->dr.wValue = cpu_to_le16(0);
209 pegasus->dr.wIndex = cpu_to_le16p(&indx);
210 pegasus->dr.wLength = cpu_to_le16p(&size);
211 pegasus->ctrl_urb->transfer_buffer_length = size;
213 usb_fill_control_urb(pegasus->ctrl_urb, pegasus->usb,
214 usb_sndctrlpipe(pegasus->usb, 0),
215 (char *) &pegasus->dr,
216 buffer, size, ctrl_callback, pegasus);
218 add_wait_queue(&pegasus->ctrl_wait, &wait);
219 set_current_state(TASK_UNINTERRUPTIBLE);
221 if ((ret = usb_submit_urb(pegasus->ctrl_urb, GFP_ATOMIC))) {
223 netif_device_detach(pegasus->net);
224 if (netif_msg_drv(pegasus))
225 dev_err(&pegasus->intf->dev, "%s, status %d\n",
232 remove_wait_queue(&pegasus->ctrl_wait, &wait);
238 static int set_register(pegasus_t * pegasus, __u16 indx, __u8 data)
242 DECLARE_WAITQUEUE(wait, current);
244 tmp = kmalloc(1, GFP_KERNEL);
246 if (netif_msg_drv(pegasus))
247 dev_warn(&pegasus->intf->dev, "out of memory in %s\n",
251 memcpy(tmp, &data, 1);
252 add_wait_queue(&pegasus->ctrl_wait, &wait);
253 set_current_state(TASK_UNINTERRUPTIBLE);
254 while (pegasus->flags & ETH_REGS_CHANGED)
256 remove_wait_queue(&pegasus->ctrl_wait, &wait);
257 set_current_state(TASK_RUNNING);
259 pegasus->dr.bRequestType = PEGASUS_REQT_WRITE;
260 pegasus->dr.bRequest = PEGASUS_REQ_SET_REG;
261 pegasus->dr.wValue = cpu_to_le16(data);
262 pegasus->dr.wIndex = cpu_to_le16p(&indx);
263 pegasus->dr.wLength = cpu_to_le16(1);
264 pegasus->ctrl_urb->transfer_buffer_length = 1;
266 usb_fill_control_urb(pegasus->ctrl_urb, pegasus->usb,
267 usb_sndctrlpipe(pegasus->usb, 0),
268 (char *) &pegasus->dr,
269 tmp, 1, ctrl_callback, pegasus);
271 add_wait_queue(&pegasus->ctrl_wait, &wait);
272 set_current_state(TASK_UNINTERRUPTIBLE);
274 if ((ret = usb_submit_urb(pegasus->ctrl_urb, GFP_ATOMIC))) {
276 netif_device_detach(pegasus->net);
277 if (netif_msg_drv(pegasus))
278 dev_err(&pegasus->intf->dev, "%s, status %d\n",
285 remove_wait_queue(&pegasus->ctrl_wait, &wait);
291 static int update_eth_regs_async(pegasus_t * pegasus)
295 pegasus->dr.bRequestType = PEGASUS_REQT_WRITE;
296 pegasus->dr.bRequest = PEGASUS_REQ_SET_REGS;
297 pegasus->dr.wValue = 0;
298 pegasus->dr.wIndex = cpu_to_le16(EthCtrl0);
299 pegasus->dr.wLength = cpu_to_le16(3);
300 pegasus->ctrl_urb->transfer_buffer_length = 3;
302 usb_fill_control_urb(pegasus->ctrl_urb, pegasus->usb,
303 usb_sndctrlpipe(pegasus->usb, 0),
304 (char *) &pegasus->dr,
305 pegasus->eth_regs, 3, ctrl_callback, pegasus);
307 if ((ret = usb_submit_urb(pegasus->ctrl_urb, GFP_ATOMIC))) {
309 netif_device_detach(pegasus->net);
310 if (netif_msg_drv(pegasus))
311 dev_err(&pegasus->intf->dev, "%s, status %d\n",
318 static int read_mii_word(pegasus_t * pegasus, __u8 phy, __u8 indx, __u16 * regd)
321 __u8 data[4] = { phy, 0, 0, indx };
325 set_register(pegasus, PhyCtrl, 0);
326 set_registers(pegasus, PhyAddr, sizeof (data), data);
327 set_register(pegasus, PhyCtrl, (indx | PHY_READ));
328 for (i = 0; i < REG_TIMEOUT; i++) {
329 ret = get_registers(pegasus, PhyCtrl, 1, data);
330 if (ret == -ESHUTDOWN)
332 if (data[0] & PHY_DONE)
335 if (i < REG_TIMEOUT) {
336 ret = get_registers(pegasus, PhyData, 2, ®di);
337 *regd = le16_to_cpu(regdi);
341 if (netif_msg_drv(pegasus))
342 dev_warn(&pegasus->intf->dev, "fail %s\n", __FUNCTION__);
347 static int mdio_read(struct net_device *dev, int phy_id, int loc)
349 pegasus_t *pegasus = (pegasus_t *) netdev_priv(dev);
352 read_mii_word(pegasus, phy_id, loc, &res);
356 static int write_mii_word(pegasus_t * pegasus, __u8 phy, __u8 indx, __u16 regd)
359 __u8 data[4] = { phy, 0, 0, indx };
363 data[2] = (u8) (regd >> 8);
364 set_register(pegasus, PhyCtrl, 0);
365 set_registers(pegasus, PhyAddr, sizeof(data), data);
366 set_register(pegasus, PhyCtrl, (indx | PHY_WRITE));
367 for (i = 0; i < REG_TIMEOUT; i++) {
368 ret = get_registers(pegasus, PhyCtrl, 1, data);
369 if (ret == -ESHUTDOWN)
371 if (data[0] & PHY_DONE)
378 if (netif_msg_drv(pegasus))
379 dev_warn(&pegasus->intf->dev, "fail %s\n", __FUNCTION__);
383 static void mdio_write(struct net_device *dev, int phy_id, int loc, int val)
385 pegasus_t *pegasus = (pegasus_t *) netdev_priv(dev);
387 write_mii_word(pegasus, phy_id, loc, val);
390 static int read_eprom_word(pegasus_t * pegasus, __u8 index, __u16 * retdata)
397 set_register(pegasus, EpromCtrl, 0);
398 set_register(pegasus, EpromOffset, index);
399 set_register(pegasus, EpromCtrl, EPROM_READ);
401 for (i = 0; i < REG_TIMEOUT; i++) {
402 ret = get_registers(pegasus, EpromCtrl, 1, &tmp);
403 if (tmp & EPROM_DONE)
405 if (ret == -ESHUTDOWN)
408 if (i < REG_TIMEOUT) {
409 ret = get_registers(pegasus, EpromData, 2, &retdatai);
410 *retdata = le16_to_cpu(retdatai);
415 if (netif_msg_drv(pegasus))
416 dev_warn(&pegasus->intf->dev, "fail %s\n", __FUNCTION__);
420 #ifdef PEGASUS_WRITE_EEPROM
421 static inline void enable_eprom_write(pegasus_t * pegasus)
426 get_registers(pegasus, EthCtrl2, 1, &tmp);
427 set_register(pegasus, EthCtrl2, tmp | EPROM_WR_ENABLE);
430 static inline void disable_eprom_write(pegasus_t * pegasus)
435 get_registers(pegasus, EthCtrl2, 1, &tmp);
436 set_register(pegasus, EpromCtrl, 0);
437 set_register(pegasus, EthCtrl2, tmp & ~EPROM_WR_ENABLE);
440 static int write_eprom_word(pegasus_t * pegasus, __u8 index, __u16 data)
443 __u8 tmp, d[4] = { 0x3f, 0, 0, EPROM_WRITE };
446 set_registers(pegasus, EpromOffset, 4, d);
447 enable_eprom_write(pegasus);
448 set_register(pegasus, EpromOffset, index);
449 set_registers(pegasus, EpromData, 2, &data);
450 set_register(pegasus, EpromCtrl, EPROM_WRITE);
452 for (i = 0; i < REG_TIMEOUT; i++) {
453 ret = get_registers(pegasus, EpromCtrl, 1, &tmp);
454 if (ret == -ESHUTDOWN)
456 if (tmp & EPROM_DONE)
459 disable_eprom_write(pegasus);
463 if (netif_msg_drv(pegasus))
464 dev_warn(&pegasus->intf->dev, "fail %s\n", __FUNCTION__);
467 #endif /* PEGASUS_WRITE_EEPROM */
469 static inline void get_node_id(pegasus_t * pegasus, __u8 * id)
474 for (i = 0; i < 3; i++) {
475 read_eprom_word(pegasus, i, &w16);
476 ((__le16 *) id)[i] = cpu_to_le16p(&w16);
480 static void set_ethernet_addr(pegasus_t * pegasus)
484 get_node_id(pegasus, node_id);
485 set_registers(pegasus, EthID, sizeof (node_id), node_id);
486 memcpy(pegasus->net->dev_addr, node_id, sizeof (node_id));
489 static inline int reset_mac(pegasus_t * pegasus)
494 set_register(pegasus, EthCtrl1, data);
495 for (i = 0; i < REG_TIMEOUT; i++) {
496 get_registers(pegasus, EthCtrl1, 1, &data);
500 if (mii_mode && (pegasus->features & HAS_HOME_PNA))
501 set_register(pegasus, Gpio1, 0x34);
503 set_register(pegasus, Gpio1, 0x26);
504 set_register(pegasus, Gpio0, pegasus->features);
505 set_register(pegasus, Gpio0, DEFAULT_GPIO_SET);
509 if (i == REG_TIMEOUT)
512 if (usb_dev_id[pegasus->dev_index].vendor == VENDOR_LINKSYS ||
513 usb_dev_id[pegasus->dev_index].vendor == VENDOR_DLINK) {
514 set_register(pegasus, Gpio0, 0x24);
515 set_register(pegasus, Gpio0, 0x26);
517 if (usb_dev_id[pegasus->dev_index].vendor == VENDOR_ELCON) {
519 read_mii_word(pegasus, 3, 0x1b, &auxmode);
520 write_mii_word(pegasus, 3, 0x1b, auxmode | 4);
526 static int enable_net_traffic(struct net_device *dev, struct usb_device *usb)
530 pegasus_t *pegasus = netdev_priv(dev);
533 read_mii_word(pegasus, pegasus->phy, MII_LPA, &linkpart);
536 if (linkpart & (ADVERTISE_100FULL | ADVERTISE_10FULL))
537 data[1] |= 0x20; /* set full duplex */
538 if (linkpart & (ADVERTISE_100FULL | ADVERTISE_100HALF))
539 data[1] |= 0x10; /* set 100 Mbps */
542 data[2] = (loopback & 1) ? 0x09 : 0x01;
544 memcpy(pegasus->eth_regs, data, sizeof (data));
545 ret = set_registers(pegasus, EthCtrl0, 3, data);
547 if (usb_dev_id[pegasus->dev_index].vendor == VENDOR_LINKSYS ||
548 usb_dev_id[pegasus->dev_index].vendor == VENDOR_LINKSYS2 ||
549 usb_dev_id[pegasus->dev_index].vendor == VENDOR_DLINK) {
551 read_mii_word(pegasus, 0, 0x1b, &auxmode);
552 write_mii_word(pegasus, 0, 0x1b, auxmode | 4);
558 static void fill_skb_pool(pegasus_t * pegasus)
562 for (i = 0; i < RX_SKBS; i++) {
563 if (pegasus->rx_pool[i])
565 pegasus->rx_pool[i] = dev_alloc_skb(PEGASUS_MTU + 2);
567 ** we give up if the allocation fail. the tasklet will be
568 ** rescheduled again anyway...
570 if (pegasus->rx_pool[i] == NULL)
572 pegasus->rx_pool[i]->dev = pegasus->net;
573 skb_reserve(pegasus->rx_pool[i], 2);
577 static void free_skb_pool(pegasus_t * pegasus)
581 for (i = 0; i < RX_SKBS; i++) {
582 if (pegasus->rx_pool[i]) {
583 dev_kfree_skb(pegasus->rx_pool[i]);
584 pegasus->rx_pool[i] = NULL;
589 static inline struct sk_buff *pull_skb(pegasus_t * pegasus)
594 for (i = 0; i < RX_SKBS; i++) {
595 if (likely(pegasus->rx_pool[i] != NULL)) {
596 skb = pegasus->rx_pool[i];
597 pegasus->rx_pool[i] = NULL;
604 static void read_bulk_callback(struct urb *urb, struct pt_regs *regs)
606 pegasus_t *pegasus = urb->context;
607 struct net_device *net;
608 int rx_status, count = urb->actual_length;
609 u8 *buf = urb->transfer_buffer;
616 if (!netif_device_present(net) || !netif_running(net))
619 switch (urb->status) {
623 if (netif_msg_rx_err(pegasus))
624 pr_debug("%s: reset MAC\n", net->name);
625 pegasus->flags &= ~PEGASUS_RX_BUSY;
627 case -EPIPE: /* stall, or disconnect from TT */
628 /* FIXME schedule work to clear the halt */
629 if (netif_msg_rx_err(pegasus))
630 printk(KERN_WARNING "%s: no rx stall recovery\n",
636 if (netif_msg_ifdown(pegasus))
637 pr_debug("%s: rx unlink, %d\n", net->name, urb->status);
640 if (netif_msg_rx_err(pegasus))
641 pr_debug("%s: RX status %d\n", net->name, urb->status);
645 if (!count || count < 4)
648 rx_status = buf[count - 2];
649 if (rx_status & 0x1e) {
650 if (netif_msg_rx_err(pegasus))
651 pr_debug("%s: RX packet error %x\n",
652 net->name, rx_status);
653 pegasus->stats.rx_errors++;
654 if (rx_status & 0x06) // long or runt
655 pegasus->stats.rx_length_errors++;
656 if (rx_status & 0x08)
657 pegasus->stats.rx_crc_errors++;
658 if (rx_status & 0x10) // extra bits
659 pegasus->stats.rx_frame_errors++;
662 if (pegasus->chip == 0x8513) {
663 pkt_len = le32_to_cpu(*(__le32 *)urb->transfer_buffer);
665 pegasus->rx_skb->data += 2;
667 pkt_len = buf[count - 3] << 8;
668 pkt_len += buf[count - 4];
674 * If the packet is unreasonably long, quietly drop it rather than
675 * kernel panicing by calling skb_put.
677 if (pkt_len > PEGASUS_MTU)
681 * at this point we are sure pegasus->rx_skb != NULL
682 * so we go ahead and pass up the packet.
684 skb_put(pegasus->rx_skb, pkt_len);
685 pegasus->rx_skb->protocol = eth_type_trans(pegasus->rx_skb, net);
686 netif_rx(pegasus->rx_skb);
687 pegasus->stats.rx_packets++;
688 pegasus->stats.rx_bytes += pkt_len;
690 if (pegasus->flags & PEGASUS_UNPLUG)
693 spin_lock(&pegasus->rx_pool_lock);
694 pegasus->rx_skb = pull_skb(pegasus);
695 spin_unlock(&pegasus->rx_pool_lock);
697 if (pegasus->rx_skb == NULL)
700 usb_fill_bulk_urb(pegasus->rx_urb, pegasus->usb,
701 usb_rcvbulkpipe(pegasus->usb, 1),
702 pegasus->rx_skb->data, PEGASUS_MTU + 8,
703 read_bulk_callback, pegasus);
704 rx_status = usb_submit_urb(pegasus->rx_urb, GFP_ATOMIC);
705 if (rx_status == -ENODEV)
706 netif_device_detach(pegasus->net);
707 else if (rx_status) {
708 pegasus->flags |= PEGASUS_RX_URB_FAIL;
711 pegasus->flags &= ~PEGASUS_RX_URB_FAIL;
717 tasklet_schedule(&pegasus->rx_tl);
720 static void rx_fixup(unsigned long data)
726 pegasus = (pegasus_t *) data;
727 if (pegasus->flags & PEGASUS_UNPLUG)
730 spin_lock_irqsave(&pegasus->rx_pool_lock, flags);
731 fill_skb_pool(pegasus);
732 if (pegasus->flags & PEGASUS_RX_URB_FAIL)
735 if (pegasus->rx_skb == NULL) {
736 pegasus->rx_skb = pull_skb(pegasus);
738 if (pegasus->rx_skb == NULL) {
739 if (netif_msg_rx_err(pegasus))
740 printk(KERN_WARNING "%s: low on memory\n",
742 tasklet_schedule(&pegasus->rx_tl);
745 usb_fill_bulk_urb(pegasus->rx_urb, pegasus->usb,
746 usb_rcvbulkpipe(pegasus->usb, 1),
747 pegasus->rx_skb->data, PEGASUS_MTU + 8,
748 read_bulk_callback, pegasus);
750 status = usb_submit_urb(pegasus->rx_urb, GFP_ATOMIC);
751 if (status == -ENODEV)
752 netif_device_detach(pegasus->net);
754 pegasus->flags |= PEGASUS_RX_URB_FAIL;
755 tasklet_schedule(&pegasus->rx_tl);
757 pegasus->flags &= ~PEGASUS_RX_URB_FAIL;
760 spin_unlock_irqrestore(&pegasus->rx_pool_lock, flags);
763 static void write_bulk_callback(struct urb *urb, struct pt_regs *regs)
765 pegasus_t *pegasus = urb->context;
766 struct net_device *net = pegasus->net;
771 if (!netif_device_present(net) || !netif_running(net))
774 switch (urb->status) {
776 /* FIXME schedule_work() to clear the tx halt */
777 netif_stop_queue(net);
778 if (netif_msg_tx_err(pegasus))
779 printk(KERN_WARNING "%s: no tx stall recovery\n",
785 if (netif_msg_ifdown(pegasus))
786 pr_debug("%s: tx unlink, %d\n", net->name, urb->status);
789 if (netif_msg_tx_err(pegasus))
790 pr_info("%s: TX status %d\n", net->name, urb->status);
796 net->trans_start = jiffies;
797 netif_wake_queue(net);
800 static void intr_callback(struct urb *urb, struct pt_regs *regs)
802 pegasus_t *pegasus = urb->context;
803 struct net_device *net;
810 switch (urb->status) {
813 case -ECONNRESET: /* unlink */
818 /* some Pegasus-I products report LOTS of data
819 * toggle errors... avoid log spamming
821 if (netif_msg_timer(pegasus))
822 pr_debug("%s: intr status %d\n", net->name,
826 if (urb->actual_length >= 6) {
827 u8 * d = urb->transfer_buffer;
829 /* byte 0 == tx_status1, reg 2B */
830 if (d[0] & (TX_UNDERRUN|EXCESSIVE_COL
831 |LATE_COL|JABBER_TIMEOUT)) {
832 pegasus->stats.tx_errors++;
833 if (d[0] & TX_UNDERRUN)
834 pegasus->stats.tx_fifo_errors++;
835 if (d[0] & (EXCESSIVE_COL | JABBER_TIMEOUT))
836 pegasus->stats.tx_aborted_errors++;
838 pegasus->stats.tx_window_errors++;
841 /* d[5].LINK_STATUS lies on some adapters.
842 * d[0].NO_CARRIER kicks in only with failed TX.
843 * ... so monitoring with MII may be safest.
845 if (d[0] & NO_CARRIER)
846 netif_carrier_off(net);
848 netif_carrier_on(net);
850 /* bytes 3-4 == rx_lostpkt, reg 2E/2F */
851 pegasus->stats.rx_missed_errors += ((d[3] & 0x7f) << 8) | d[4];
854 status = usb_submit_urb(urb, SLAB_ATOMIC);
855 if (status == -ENODEV)
856 netif_device_detach(pegasus->net);
857 if (status && netif_msg_timer(pegasus))
858 printk(KERN_ERR "%s: can't resubmit interrupt urb, %d\n",
862 static void pegasus_tx_timeout(struct net_device *net)
864 pegasus_t *pegasus = netdev_priv(net);
865 if (netif_msg_timer(pegasus))
866 printk(KERN_WARNING "%s: tx timeout\n", net->name);
867 usb_unlink_urb(pegasus->tx_urb);
868 pegasus->stats.tx_errors++;
871 static int pegasus_start_xmit(struct sk_buff *skb, struct net_device *net)
873 pegasus_t *pegasus = netdev_priv(net);
874 int count = ((skb->len + 2) & 0x3f) ? skb->len + 2 : skb->len + 3;
876 __u16 l16 = skb->len;
878 netif_stop_queue(net);
880 ((__le16 *) pegasus->tx_buff)[0] = cpu_to_le16(l16);
881 memcpy(pegasus->tx_buff + 2, skb->data, skb->len);
882 usb_fill_bulk_urb(pegasus->tx_urb, pegasus->usb,
883 usb_sndbulkpipe(pegasus->usb, 2),
884 pegasus->tx_buff, count,
885 write_bulk_callback, pegasus);
886 if ((res = usb_submit_urb(pegasus->tx_urb, GFP_ATOMIC))) {
887 if (netif_msg_tx_err(pegasus))
888 printk(KERN_WARNING "%s: fail tx, %d\n",
891 case -EPIPE: /* stall, or disconnect from TT */
892 /* cleanup should already have been scheduled */
894 case -ENODEV: /* disconnect() upcoming */
895 netif_device_detach(pegasus->net);
898 pegasus->stats.tx_errors++;
899 netif_start_queue(net);
902 pegasus->stats.tx_packets++;
903 pegasus->stats.tx_bytes += skb->len;
904 net->trans_start = jiffies;
911 static struct net_device_stats *pegasus_netdev_stats(struct net_device *dev)
913 return &((pegasus_t *) netdev_priv(dev))->stats;
916 static inline void disable_net_traffic(pegasus_t * pegasus)
920 set_registers(pegasus, EthCtrl0, 2, &tmp);
923 static inline void get_interrupt_interval(pegasus_t * pegasus)
927 read_eprom_word(pegasus, 4, (__u16 *) data);
928 if (pegasus->usb->speed != USB_SPEED_HIGH) {
929 if (data[1] < 0x80) {
930 if (netif_msg_timer(pegasus))
931 dev_info(&pegasus->intf->dev, "intr interval "
932 "changed from %ums to %ums\n",
935 #ifdef PEGASUS_WRITE_EEPROM
936 write_eprom_word(pegasus, 4, *(__u16 *) data);
940 pegasus->intr_interval = data[1];
943 static void set_carrier(struct net_device *net)
945 pegasus_t *pegasus = netdev_priv(net);
948 if (!read_mii_word(pegasus, pegasus->phy, MII_BMSR, &tmp))
951 if (tmp & BMSR_LSTATUS)
952 netif_carrier_on(net);
954 netif_carrier_off(net);
957 static void free_all_urbs(pegasus_t * pegasus)
959 usb_free_urb(pegasus->intr_urb);
960 usb_free_urb(pegasus->tx_urb);
961 usb_free_urb(pegasus->rx_urb);
962 usb_free_urb(pegasus->ctrl_urb);
965 static void unlink_all_urbs(pegasus_t * pegasus)
967 usb_kill_urb(pegasus->intr_urb);
968 usb_kill_urb(pegasus->tx_urb);
969 usb_kill_urb(pegasus->rx_urb);
970 usb_kill_urb(pegasus->ctrl_urb);
973 static int alloc_urbs(pegasus_t * pegasus)
975 pegasus->ctrl_urb = usb_alloc_urb(0, GFP_KERNEL);
976 if (!pegasus->ctrl_urb) {
979 pegasus->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
980 if (!pegasus->rx_urb) {
981 usb_free_urb(pegasus->ctrl_urb);
984 pegasus->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
985 if (!pegasus->tx_urb) {
986 usb_free_urb(pegasus->rx_urb);
987 usb_free_urb(pegasus->ctrl_urb);
990 pegasus->intr_urb = usb_alloc_urb(0, GFP_KERNEL);
991 if (!pegasus->intr_urb) {
992 usb_free_urb(pegasus->tx_urb);
993 usb_free_urb(pegasus->rx_urb);
994 usb_free_urb(pegasus->ctrl_urb);
1001 static int pegasus_open(struct net_device *net)
1003 pegasus_t *pegasus = netdev_priv(net);
1006 if (pegasus->rx_skb == NULL)
1007 pegasus->rx_skb = pull_skb(pegasus);
1009 ** Note: no point to free the pool. it is empty :-)
1011 if (!pegasus->rx_skb)
1014 res = set_registers(pegasus, EthID, 6, net->dev_addr);
1016 usb_fill_bulk_urb(pegasus->rx_urb, pegasus->usb,
1017 usb_rcvbulkpipe(pegasus->usb, 1),
1018 pegasus->rx_skb->data, PEGASUS_MTU + 8,
1019 read_bulk_callback, pegasus);
1020 if ((res = usb_submit_urb(pegasus->rx_urb, GFP_KERNEL))) {
1022 netif_device_detach(pegasus->net);
1023 if (netif_msg_ifup(pegasus))
1024 pr_debug("%s: failed rx_urb, %d", net->name, res);
1028 usb_fill_int_urb(pegasus->intr_urb, pegasus->usb,
1029 usb_rcvintpipe(pegasus->usb, 3),
1030 pegasus->intr_buff, sizeof (pegasus->intr_buff),
1031 intr_callback, pegasus, pegasus->intr_interval);
1032 if ((res = usb_submit_urb(pegasus->intr_urb, GFP_KERNEL))) {
1034 netif_device_detach(pegasus->net);
1035 if (netif_msg_ifup(pegasus))
1036 pr_debug("%s: failed intr_urb, %d\n", net->name, res);
1037 usb_kill_urb(pegasus->rx_urb);
1040 if ((res = enable_net_traffic(net, pegasus->usb))) {
1041 if (netif_msg_ifup(pegasus))
1042 pr_debug("%s: can't enable_net_traffic() - %d\n",
1045 usb_kill_urb(pegasus->rx_urb);
1046 usb_kill_urb(pegasus->intr_urb);
1047 free_skb_pool(pegasus);
1051 netif_start_queue(net);
1052 if (netif_msg_ifup(pegasus))
1053 pr_debug("%s: open\n", net->name);
1059 static int pegasus_close(struct net_device *net)
1061 pegasus_t *pegasus = netdev_priv(net);
1063 netif_stop_queue(net);
1064 if (!(pegasus->flags & PEGASUS_UNPLUG))
1065 disable_net_traffic(pegasus);
1066 tasklet_kill(&pegasus->rx_tl);
1067 unlink_all_urbs(pegasus);
1072 static void pegasus_get_drvinfo(struct net_device *dev,
1073 struct ethtool_drvinfo *info)
1075 pegasus_t *pegasus = netdev_priv(dev);
1076 strncpy(info->driver, driver_name, sizeof (info->driver) - 1);
1077 strncpy(info->version, DRIVER_VERSION, sizeof (info->version) - 1);
1078 usb_make_path(pegasus->usb, info->bus_info, sizeof (info->bus_info));
1081 /* also handles three patterns of some kind in hardware */
1082 #define WOL_SUPPORTED (WAKE_MAGIC|WAKE_PHY)
1085 pegasus_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1087 pegasus_t *pegasus = netdev_priv(dev);
1089 wol->supported = WAKE_MAGIC | WAKE_PHY;
1090 wol->wolopts = pegasus->wolopts;
1094 pegasus_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1096 pegasus_t *pegasus = netdev_priv(dev);
1099 if (wol->wolopts & ~WOL_SUPPORTED)
1102 if (wol->wolopts & WAKE_MAGIC)
1104 if (wol->wolopts & WAKE_PHY)
1106 /* FIXME this 0x10 bit still needs to get set in the chip... */
1108 pegasus->eth_regs[0] |= 0x10;
1110 pegasus->eth_regs[0] &= ~0x10;
1111 pegasus->wolopts = wol->wolopts;
1112 return set_register(pegasus, WakeupControl, reg78);
1115 static inline void pegasus_reset_wol(struct net_device *dev)
1117 struct ethtool_wolinfo wol;
1119 memset(&wol, 0, sizeof wol);
1120 (void) pegasus_set_wol(dev, &wol);
1124 pegasus_get_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
1131 pegasus = netdev_priv(dev);
1132 mii_ethtool_gset(&pegasus->mii, ecmd);
1138 pegasus_set_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
1140 pegasus_t *pegasus = netdev_priv(dev);
1141 return mii_ethtool_sset(&pegasus->mii, ecmd);
1144 static int pegasus_nway_reset(struct net_device *dev)
1146 pegasus_t *pegasus = netdev_priv(dev);
1147 return mii_nway_restart(&pegasus->mii);
1150 static u32 pegasus_get_link(struct net_device *dev)
1152 pegasus_t *pegasus = netdev_priv(dev);
1153 return mii_link_ok(&pegasus->mii);
1156 static u32 pegasus_get_msglevel(struct net_device *dev)
1158 pegasus_t *pegasus = netdev_priv(dev);
1159 return pegasus->msg_enable;
1162 static void pegasus_set_msglevel(struct net_device *dev, u32 v)
1164 pegasus_t *pegasus = netdev_priv(dev);
1165 pegasus->msg_enable = v;
1168 static struct ethtool_ops ops = {
1169 .get_drvinfo = pegasus_get_drvinfo,
1170 .get_settings = pegasus_get_settings,
1171 .set_settings = pegasus_set_settings,
1172 .nway_reset = pegasus_nway_reset,
1173 .get_link = pegasus_get_link,
1174 .get_msglevel = pegasus_get_msglevel,
1175 .set_msglevel = pegasus_set_msglevel,
1176 .get_wol = pegasus_get_wol,
1177 .set_wol = pegasus_set_wol,
1180 static int pegasus_ioctl(struct net_device *net, struct ifreq *rq, int cmd)
1182 __u16 *data = (__u16 *) & rq->ifr_ifru;
1183 pegasus_t *pegasus = netdev_priv(net);
1187 case SIOCDEVPRIVATE:
1188 data[0] = pegasus->phy;
1189 case SIOCDEVPRIVATE + 1:
1190 read_mii_word(pegasus, data[0], data[1] & 0x1f, &data[3]);
1193 case SIOCDEVPRIVATE + 2:
1194 if (!capable(CAP_NET_ADMIN))
1196 write_mii_word(pegasus, pegasus->phy, data[1] & 0x1f, data[2]);
1205 static void pegasus_set_multicast(struct net_device *net)
1207 pegasus_t *pegasus = netdev_priv(net);
1209 if (net->flags & IFF_PROMISC) {
1210 pegasus->eth_regs[EthCtrl2] |= RX_PROMISCUOUS;
1211 if (netif_msg_link(pegasus))
1212 pr_info("%s: Promiscuous mode enabled.\n", net->name);
1213 } else if (net->mc_count ||
1214 (net->flags & IFF_ALLMULTI)) {
1215 pegasus->eth_regs[EthCtrl0] |= RX_MULTICAST;
1216 pegasus->eth_regs[EthCtrl2] &= ~RX_PROMISCUOUS;
1217 if (netif_msg_link(pegasus))
1218 pr_info("%s: set allmulti\n", net->name);
1220 pegasus->eth_regs[EthCtrl0] &= ~RX_MULTICAST;
1221 pegasus->eth_regs[EthCtrl2] &= ~RX_PROMISCUOUS;
1224 pegasus->flags |= ETH_REGS_CHANGE;
1225 ctrl_callback(pegasus->ctrl_urb, NULL);
1228 static __u8 mii_phy_probe(pegasus_t * pegasus)
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)
1244 static inline void setup_pegasus_II(pegasus_t * pegasus)
1248 set_register(pegasus, Reg1d, 0);
1249 set_register(pegasus, Reg7b, 1);
1251 if ((pegasus->features & HAS_HOME_PNA) && mii_mode)
1252 set_register(pegasus, Reg7b, 0);
1254 set_register(pegasus, Reg7b, 2);
1256 set_register(pegasus, 0x83, data);
1257 get_registers(pegasus, 0x83, 1, &data);
1260 pegasus->chip = 0x8513;
1265 set_register(pegasus, 0x80, 0xc0);
1266 set_register(pegasus, 0x83, 0xff);
1267 set_register(pegasus, 0x84, 0x01);
1269 if (pegasus->features & HAS_HOME_PNA && mii_mode)
1270 set_register(pegasus, Reg81, 6);
1272 set_register(pegasus, Reg81, 2);
1276 static struct workqueue_struct *pegasus_workqueue = NULL;
1277 #define CARRIER_CHECK_DELAY (2 * HZ)
1279 static void check_carrier(void *data)
1281 pegasus_t *pegasus = data;
1282 set_carrier(pegasus->net);
1283 if (!(pegasus->flags & PEGASUS_UNPLUG)) {
1284 queue_delayed_work(pegasus_workqueue, &pegasus->carrier_check,
1285 CARRIER_CHECK_DELAY);
1289 static int pegasus_probe(struct usb_interface *intf,
1290 const struct usb_device_id *id)
1292 struct usb_device *dev = interface_to_usbdev(intf);
1293 struct net_device *net;
1295 int dev_index = id - pegasus_ids;
1299 net = alloc_etherdev(sizeof(struct pegasus));
1301 dev_err(&intf->dev, "can't allocate %s\n", "device");
1305 pegasus = netdev_priv(net);
1306 memset(pegasus, 0, sizeof (struct pegasus));
1307 pegasus->dev_index = dev_index;
1308 init_waitqueue_head(&pegasus->ctrl_wait);
1310 if (!alloc_urbs(pegasus)) {
1311 dev_err(&intf->dev, "can't allocate %s\n", "urbs");
1315 tasklet_init(&pegasus->rx_tl, rx_fixup, (unsigned long) pegasus);
1317 INIT_WORK(&pegasus->carrier_check, check_carrier, pegasus);
1319 pegasus->intf = intf;
1322 SET_MODULE_OWNER(net);
1323 net->open = pegasus_open;
1324 net->stop = pegasus_close;
1325 net->watchdog_timeo = PEGASUS_TX_TIMEOUT;
1326 net->tx_timeout = pegasus_tx_timeout;
1327 net->do_ioctl = pegasus_ioctl;
1328 net->hard_start_xmit = pegasus_start_xmit;
1329 net->set_multicast_list = pegasus_set_multicast;
1330 net->get_stats = pegasus_netdev_stats;
1331 SET_ETHTOOL_OPS(net, &ops);
1332 pegasus->mii.dev = net;
1333 pegasus->mii.mdio_read = mdio_read;
1334 pegasus->mii.mdio_write = mdio_write;
1335 pegasus->mii.phy_id_mask = 0x1f;
1336 pegasus->mii.reg_num_mask = 0x1f;
1337 spin_lock_init(&pegasus->rx_pool_lock);
1338 pegasus->msg_enable = netif_msg_init (msg_level, NETIF_MSG_DRV
1339 | NETIF_MSG_PROBE | NETIF_MSG_LINK);
1341 pegasus->features = usb_dev_id[dev_index].private;
1342 get_interrupt_interval(pegasus);
1343 if (reset_mac(pegasus)) {
1344 dev_err(&intf->dev, "can't reset MAC\n");
1348 set_ethernet_addr(pegasus);
1349 fill_skb_pool(pegasus);
1350 if (pegasus->features & PEGASUS_II) {
1351 dev_info(&intf->dev, "setup Pegasus II specific registers\n");
1352 setup_pegasus_II(pegasus);
1354 pegasus->phy = mii_phy_probe(pegasus);
1355 if (pegasus->phy == 0xff) {
1356 dev_warn(&intf->dev, "can't locate MII phy, using default\n");
1359 pegasus->mii.phy_id = pegasus->phy;
1360 usb_set_intfdata(intf, pegasus);
1361 SET_NETDEV_DEV(net, &intf->dev);
1362 pegasus_reset_wol(net);
1363 res = register_netdev(net);
1366 queue_delayed_work(pegasus_workqueue, &pegasus->carrier_check,
1367 CARRIER_CHECK_DELAY);
1369 dev_info(&intf->dev, "%s, %s, %02x:%02x:%02x:%02x:%02x:%02x\n",
1371 usb_dev_id[dev_index].name,
1372 net->dev_addr [0], net->dev_addr [1],
1373 net->dev_addr [2], net->dev_addr [3],
1374 net->dev_addr [4], net->dev_addr [5]);
1378 usb_set_intfdata(intf, NULL);
1379 free_skb_pool(pegasus);
1381 free_all_urbs(pegasus);
1389 static void pegasus_disconnect(struct usb_interface *intf)
1391 struct pegasus *pegasus = usb_get_intfdata(intf);
1393 usb_set_intfdata(intf, NULL);
1395 dev_dbg(&intf->dev, "unregistering non-bound device?\n");
1399 pegasus->flags |= PEGASUS_UNPLUG;
1400 cancel_delayed_work(&pegasus->carrier_check);
1401 unregister_netdev(pegasus->net);
1402 usb_put_dev(interface_to_usbdev(intf));
1403 unlink_all_urbs(pegasus);
1404 free_all_urbs(pegasus);
1405 free_skb_pool(pegasus);
1406 if (pegasus->rx_skb)
1407 dev_kfree_skb(pegasus->rx_skb);
1408 free_netdev(pegasus->net);
1411 static int pegasus_suspend (struct usb_interface *intf, pm_message_t message)
1413 struct pegasus *pegasus = usb_get_intfdata(intf);
1415 netif_device_detach (pegasus->net);
1416 cancel_delayed_work(&pegasus->carrier_check);
1417 if (netif_running(pegasus->net)) {
1418 usb_kill_urb(pegasus->rx_urb);
1419 usb_kill_urb(pegasus->intr_urb);
1424 static int pegasus_resume (struct usb_interface *intf)
1426 struct pegasus *pegasus = usb_get_intfdata(intf);
1428 netif_device_attach (pegasus->net);
1429 if (netif_running(pegasus->net)) {
1430 pegasus->rx_urb->status = 0;
1431 pegasus->rx_urb->actual_length = 0;
1432 read_bulk_callback(pegasus->rx_urb, NULL);
1434 pegasus->intr_urb->status = 0;
1435 pegasus->intr_urb->actual_length = 0;
1436 intr_callback(pegasus->intr_urb, NULL);
1438 queue_delayed_work(pegasus_workqueue, &pegasus->carrier_check,
1439 CARRIER_CHECK_DELAY);
1443 static struct usb_driver pegasus_driver = {
1444 .name = driver_name,
1445 .probe = pegasus_probe,
1446 .disconnect = pegasus_disconnect,
1447 .id_table = pegasus_ids,
1448 .suspend = pegasus_suspend,
1449 .resume = pegasus_resume,
1452 static void parse_id(char *id)
1454 unsigned int vendor_id=0, device_id=0, flags=0, i=0;
1455 char *token, *name=NULL;
1457 if ((token = strsep(&id, ":")) != NULL)
1459 /* name now points to a null terminated string*/
1460 if ((token = strsep(&id, ":")) != NULL)
1461 vendor_id = simple_strtoul(token, NULL, 16);
1462 if ((token = strsep(&id, ":")) != NULL)
1463 device_id = simple_strtoul(token, NULL, 16);
1464 flags = simple_strtoul(id, NULL, 16);
1465 pr_info("%s: new device %s, vendor ID 0x%04x, device ID 0x%04x, flags: 0x%x\n",
1466 driver_name, name, vendor_id, device_id, flags);
1468 if (vendor_id > 0x10000 || vendor_id == 0)
1470 if (device_id > 0x10000 || device_id == 0)
1473 for (i=0; usb_dev_id[i].name; i++);
1474 usb_dev_id[i].name = name;
1475 usb_dev_id[i].vendor = vendor_id;
1476 usb_dev_id[i].device = device_id;
1477 usb_dev_id[i].private = flags;
1478 pegasus_ids[i].match_flags = USB_DEVICE_ID_MATCH_DEVICE;
1479 pegasus_ids[i].idVendor = vendor_id;
1480 pegasus_ids[i].idProduct = device_id;
1483 static int __init pegasus_init(void)
1485 pr_info("%s: %s, " DRIVER_DESC "\n", driver_name, DRIVER_VERSION);
1488 pegasus_workqueue = create_singlethread_workqueue("pegasus");
1489 if (!pegasus_workqueue)
1491 return usb_register(&pegasus_driver);
1494 static void __exit pegasus_exit(void)
1496 destroy_workqueue(pegasus_workqueue);
1497 usb_deregister(&pegasus_driver);
1500 module_init(pegasus_init);
1501 module_exit(pegasus_exit);