2 * Copyright (c) 2002 Petko Manolov (petkan@users.sourceforge.net)
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * version 2 as published by the Free Software Foundation.
9 #include <linux/config.h>
10 #include <linux/sched.h>
11 #include <linux/signal.h>
12 #include <linux/slab.h>
13 #include <linux/module.h>
14 #include <linux/netdevice.h>
15 #include <linux/etherdevice.h>
16 #include <linux/mii.h>
17 #include <linux/ethtool.h>
18 #include <linux/devfs_fs_kernel.h>
19 #include <linux/usb.h>
20 #include <linux/init.h>
21 #include <asm/uaccess.h>
23 /* Version Information */
24 #define DRIVER_VERSION "v0.4.3 (2002/12/31)"
25 #define DRIVER_AUTHOR "Petko Manolov <petkan@users.sourceforge.net>"
26 #define DRIVER_DESC "rtl8150 based usb-ethernet driver"
48 #define IDR_EEPROM 0x1202
51 #define PHY_WRITE 0x20
54 #define RTL8150_REQT_READ 0xc0
55 #define RTL8150_REQT_WRITE 0x40
56 #define RTL8150_REQ_GET_REGS 0x05
57 #define RTL8150_REQ_SET_REGS 0x05
59 #define RTL8150_MTU 1500
60 #define RTL8150_MAX_MTU 1536
62 #define RTL8150_TX_TIMEOUT (HZ)
65 #define RTL8150_FLAG_HWCRC 0
67 #define RTL8150_UNPLUG 2
69 /* Define these values to match your device */
70 #define VENDOR_ID_REALTEK 0x0bda
71 #define VENDOR_ID_MELCO 0x0411
73 #define PRODUCT_ID_RTL8150 0x8150
74 #define PRODUCT_ID_LUAKTX 0x0012
78 /* table of devices that work with this driver */
79 static struct usb_device_id rtl8150_table[] = {
80 {USB_DEVICE(VENDOR_ID_REALTEK, PRODUCT_ID_RTL8150)},
81 {USB_DEVICE(VENDOR_ID_MELCO, PRODUCT_ID_LUAKTX)},
85 MODULE_DEVICE_TABLE(usb, rtl8150_table);
89 struct usb_device *udev;
90 struct usb_interface *interface;
92 struct net_device_stats stats;
93 struct net_device *netdev;
94 struct urb *rx_urb, *tx_urb, *intr_urb, *ctrl_urb;
95 struct usb_ctrlrequest dr;
98 u8 rx_buff[RTL8150_MAX_MTU];
99 u8 tx_buff[RTL8150_MAX_MTU];
104 typedef struct rtl8150 rtl8150_t;
106 /* the global usb devfs handle */
107 extern devfs_handle_t usb_devfs_handle;
108 unsigned long multicast_filter_limit = 32;
110 static void rtl8150_disconnect(struct usb_device *dev, void *ptr);
111 static void *rtl8150_probe(struct usb_device *dev, unsigned int ifnum,
112 const struct usb_device_id *id);
114 static const char driver_name[] = "rtl8150";
116 static struct usb_driver rtl8150_driver = {
118 probe: rtl8150_probe,
119 disconnect: rtl8150_disconnect,
120 id_table: rtl8150_table,
125 ** device related part of the code
128 static int get_registers(rtl8150_t * dev, u16 indx, u16 size, void *data)
130 return usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0),
131 RTL8150_REQ_GET_REGS, RTL8150_REQT_READ,
132 indx, 0, data, size, HZ / 2);
135 static int set_registers(rtl8150_t * dev, u16 indx, u16 size, void *data)
137 return usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
138 RTL8150_REQ_SET_REGS, RTL8150_REQT_WRITE,
139 indx, 0, data, size, HZ / 2);
142 static void ctrl_callback(struct urb *urb)
146 switch (urb->status) {
154 warn("ctrl urb status %d", urb->status);
157 clear_bit(RX_REG_SET, &dev->flags);
160 static int async_set_registers(rtl8150_t * dev, u16 indx, u16 size)
164 if (test_bit(RX_REG_SET, &dev->flags))
167 dev->dr.bRequestType = RTL8150_REQT_WRITE;
168 dev->dr.bRequest = RTL8150_REQ_SET_REGS;
169 dev->dr.wValue = cpu_to_le16(indx);
171 dev->dr.wLength = cpu_to_le16(2);
172 dev->ctrl_urb->transfer_buffer_length = 2;
173 FILL_CONTROL_URB(dev->ctrl_urb, dev->udev,
174 usb_sndctrlpipe(dev->udev, 0), (char *) &dev->dr,
175 &dev->rx_creg, 2, ctrl_callback, dev);
176 if ((ret = usb_submit_urb(dev->ctrl_urb)))
177 err("control request submission failed: %d", ret);
179 set_bit(RX_REG_SET, &dev->flags);
184 static int read_mii_word(rtl8150_t * dev, u8 phy, __u8 indx, u16 * reg)
190 data[1] = data[2] = 0;
191 tmp = indx | PHY_READ | PHY_GO;
194 set_registers(dev, PHYADD, sizeof(data), data);
195 set_registers(dev, PHYCNT, 1, &tmp);
197 get_registers(dev, PHYCNT, 1, data);
198 } while ((data[0] & PHY_GO) && (i++ < HZ));
201 get_registers(dev, PHYDAT, 2, data);
202 *reg = le16_to_cpup((u16 *)data);
208 static int write_mii_word(rtl8150_t * dev, u8 phy, __u8 indx, u16 reg)
214 *(data + 1) = cpu_to_le16p(®);
215 tmp = indx | PHY_WRITE | PHY_GO;
218 set_registers(dev, PHYADD, sizeof(data), data);
219 set_registers(dev, PHYCNT, 1, &tmp);
221 get_registers(dev, PHYCNT, 1, data);
222 } while ((data[0] & PHY_GO) && (i++ < HZ));
230 static inline void set_ethernet_addr(rtl8150_t * dev)
234 get_registers(dev, IDR, sizeof(node_id), node_id);
235 memcpy(dev->netdev->dev_addr, node_id, sizeof(node_id));
238 static int rtl8150_set_mac_address(struct net_device *netdev, void *p)
240 struct sockaddr *addr = p;
244 if (netif_running(netdev))
250 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
251 dbg("%s: Setting MAC address to ", netdev->name);
252 for (i = 0; i < 5; i++)
253 printk("%02X:", netdev->dev_addr[i]);
254 dbg("%02X\n", netdev->dev_addr[i]);
255 /* Set the IDR registers. */
256 set_registers(dev, IDR, sizeof(netdev->dev_addr), netdev->dev_addr);
260 /* Get the CR contents. */
261 get_registers(dev, CR, 1, &cr);
262 /* Set the WEPROM bit (eeprom write enable). */
264 set_registers(dev, CR, 1, &cr);
265 /* Write the MAC address into eeprom. Eeprom writes must be word-sized,
266 so we need to split them up. */
267 for (i = 0; i * 2 < netdev->addr_len; i++) {
268 set_registers(dev, IDR_EEPROM + (i * 2), 2,
269 netdev->dev_addr + (i * 2));
271 /* Clear the WEPROM bit (preventing accidental eeprom writes). */
273 set_registers(dev, CR, 1, &cr);
279 static int rtl8150_reset(rtl8150_t * dev)
284 set_registers(dev, CR, 1, &data);
286 get_registers(dev, CR, 1, &data);
287 } while ((data & 0x10) && --i);
289 return (i > 0) ? 0 : -1;
292 static int alloc_all_urbs(rtl8150_t * dev)
294 dev->rx_urb = usb_alloc_urb(0);
297 dev->tx_urb = usb_alloc_urb(0);
299 usb_free_urb(dev->rx_urb);
302 dev->intr_urb = usb_alloc_urb(0);
303 if (!dev->intr_urb) {
304 usb_free_urb(dev->rx_urb);
305 usb_free_urb(dev->tx_urb);
308 dev->ctrl_urb = usb_alloc_urb(0);
309 if (!dev->ctrl_urb) {
310 usb_free_urb(dev->rx_urb);
311 usb_free_urb(dev->tx_urb);
312 usb_free_urb(dev->intr_urb);
319 static void free_all_urbs(rtl8150_t * dev)
321 usb_free_urb(dev->rx_urb);
322 usb_free_urb(dev->tx_urb);
323 usb_free_urb(dev->intr_urb);
324 usb_free_urb(dev->ctrl_urb);
327 static void unlink_all_urbs(rtl8150_t * dev)
329 usb_unlink_urb(dev->rx_urb);
330 usb_unlink_urb(dev->tx_urb);
331 usb_unlink_urb(dev->intr_urb);
332 usb_unlink_urb(dev->ctrl_urb);
335 static void read_bulk_callback(struct urb *urb)
340 struct net_device *netdev;
348 netdev = dev->netdev;
349 if (!netif_device_present(netdev)) {
350 warn("netdev is not present");
353 switch (urb->status) {
359 warn("need a device reset?..");
362 warn("Rx status %d", urb->status);
366 pkt_len = urb->actual_length - 4;
367 rx_stat = le16_to_cpu(*(u16 *) (dev->rx_buff + pkt_len));
369 if (!(skb = dev_alloc_skb(pkt_len + 2)))
373 eth_copy_and_sum(skb, dev->rx_buff, pkt_len, 0);
374 skb_put(skb, pkt_len);
375 skb->protocol = eth_type_trans(skb, netdev);
377 dev->stats.rx_packets++;
378 dev->stats.rx_bytes += pkt_len;
380 FILL_BULK_URB(dev->rx_urb, dev->udev, usb_rcvbulkpipe(dev->udev, 1),
381 dev->rx_buff, RTL8150_MAX_MTU, read_bulk_callback, dev);
382 if ((res = usb_submit_urb(dev->rx_urb)))
383 warn("%s: Rx urb submission failed %d", netdev->name, res);
386 static void write_bulk_callback(struct urb *urb)
393 if (!netif_device_present(dev->netdev))
396 info("%s: Tx status %d", dev->netdev->name, urb->status);
397 dev->netdev->trans_start = jiffies;
398 netif_wake_queue(dev->netdev);
401 void intr_callback(struct urb *urb)
408 switch (urb->status) {
414 info("%s: intr status %d", dev->netdev->name, urb->status);
420 ** network related part of the code
424 static int enable_net_traffic(rtl8150_t * dev)
426 u8 cr, tcr, rcr, msr;
428 if (rtl8150_reset(dev)) {
429 warn("%s - device reset failed", __FUNCTION__);
431 rcr = 0x9e; /* bit7=1 attach Rx info at the end */
432 dev->rx_creg = cpu_to_le16(rcr);
435 set_registers(dev, RCR, 1, &rcr);
436 set_registers(dev, TCR, 1, &tcr);
437 set_registers(dev, CR, 1, &cr);
438 get_registers(dev, MSR, 1, &msr);
443 static void disable_net_traffic(rtl8150_t * dev)
447 get_registers(dev, CR, 1, &cr);
449 set_registers(dev, CR, 1, &cr);
452 static struct net_device_stats *rtl8150_netdev_stats(struct net_device *dev)
454 return &((rtl8150_t *) dev->priv)->stats;
457 static void rtl8150_tx_timeout(struct net_device *netdev)
464 warn("%s: Tx timeout.", netdev->name);
465 dev->tx_urb->transfer_flags |= USB_ASYNC_UNLINK;
466 usb_unlink_urb(dev->tx_urb);
467 dev->stats.tx_errors++;
470 static void rtl8150_set_multicast(struct net_device *netdev)
475 netif_stop_queue(netdev);
476 if (netdev->flags & IFF_PROMISC) {
477 dev->rx_creg |= cpu_to_le16(0x0001);
478 info("%s: promiscuous mode", netdev->name);
479 } else if ((netdev->mc_count > multicast_filter_limit) ||
480 (netdev->flags & IFF_ALLMULTI)) {
481 dev->rx_creg &= cpu_to_le16(0xfffe);
482 dev->rx_creg |= cpu_to_le16(0x0002);
483 info("%s: allmulti set", netdev->name);
485 /* ~RX_MULTICAST, ~RX_PROMISCUOUS */
486 dev->rx_creg &= cpu_to_le16(0x00fc);
488 async_set_registers(dev, RCR, 2);
489 netif_wake_queue(netdev);
492 static int rtl8150_start_xmit(struct sk_buff *skb, struct net_device *netdev)
497 netif_stop_queue(netdev);
499 count = (skb->len < 60) ? 60 : skb->len;
500 count = (count & 0x3f) ? count : count + 1;
501 memcpy(dev->tx_buff, skb->data, skb->len);
502 FILL_BULK_URB(dev->tx_urb, dev->udev, usb_sndbulkpipe(dev->udev, 2),
503 dev->tx_buff, count, write_bulk_callback, dev);
504 dev->tx_urb->transfer_buffer_length = count;
506 if ((res = usb_submit_urb(dev->tx_urb))) {
507 warn("failed tx_urb %d\n", res);
508 dev->stats.tx_errors++;
509 netif_start_queue(netdev);
511 dev->stats.tx_packets++;
512 dev->stats.tx_bytes += skb->len;
513 netdev->trans_start = jiffies;
520 static int rtl8150_open(struct net_device *netdev)
532 set_registers(dev, IDR, 6, netdev->dev_addr);
534 FILL_BULK_URB(dev->rx_urb, dev->udev, usb_rcvbulkpipe(dev->udev, 1),
535 dev->rx_buff, RTL8150_MAX_MTU, read_bulk_callback, dev);
536 if ((res = usb_submit_urb(dev->rx_urb)))
537 warn("%s: rx_urb submit failed: %d", __FUNCTION__, res);
538 FILL_INT_URB(dev->intr_urb, dev->udev, usb_rcvintpipe(dev->udev, 3),
539 dev->intr_buff, sizeof(dev->intr_buff), intr_callback,
540 dev, dev->intr_interval);
541 if ((res = usb_submit_urb(dev->intr_urb)))
542 warn("%s: intr_urb submit failed: %d", __FUNCTION__, res);
543 netif_start_queue(netdev);
544 enable_net_traffic(dev);
550 static int rtl8150_close(struct net_device *netdev)
560 if (!test_bit(RTL8150_UNPLUG, &dev->flags))
561 disable_net_traffic(dev);
562 unlink_all_urbs(dev);
563 netif_stop_queue(netdev);
569 static int rtl8150_ethtool_ioctl(struct net_device *netdev, void *uaddr)
576 if (get_user(cmd, (int *) uaddr))
580 case ETHTOOL_GDRVINFO:{
581 struct ethtool_drvinfo info = { ETHTOOL_GDRVINFO };
583 strncpy(info.driver, driver_name, ETHTOOL_BUSINFO_LEN);
584 strncpy(info.version, DRIVER_VERSION,
585 ETHTOOL_BUSINFO_LEN);
586 sprintf(tmp, "usb%d:%d", dev->udev->bus->busnum,
588 strncpy(info.bus_info, tmp, ETHTOOL_BUSINFO_LEN);
589 if (copy_to_user(uaddr, &info, sizeof(info)))
594 struct ethtool_cmd ecmd;
597 if (copy_from_user(&ecmd, uaddr, sizeof(ecmd)))
599 ecmd.supported = (SUPPORTED_10baseT_Half |
600 SUPPORTED_10baseT_Full |
601 SUPPORTED_100baseT_Half |
602 SUPPORTED_100baseT_Full |
604 SUPPORTED_TP | SUPPORTED_MII);
606 ecmd.transceiver = XCVR_INTERNAL;
607 ecmd.phy_address = dev->phy;
608 get_registers(dev, BMCR, 2, &bmcr);
609 get_registers(dev, ANLP, 2, &lpa);
610 if (bmcr & BMCR_ANENABLE) {
611 ecmd.autoneg = AUTONEG_ENABLE;
613 (lpa & (LPA_100HALF | LPA_100FULL)) ?
614 SPEED_100 : SPEED_10;
615 if (ecmd.speed == SPEED_100)
616 ecmd.duplex = (lpa & LPA_100FULL) ?
617 DUPLEX_FULL : DUPLEX_HALF;
619 ecmd.duplex = (lpa & LPA_10FULL) ?
620 DUPLEX_FULL : DUPLEX_HALF;
622 ecmd.autoneg = AUTONEG_DISABLE;
623 ecmd.speed = (bmcr & BMCR_SPEED100) ?
624 SPEED_100 : SPEED_10;
625 ecmd.duplex = (bmcr & BMCR_FULLDPLX) ?
626 DUPLEX_FULL : DUPLEX_HALF;
628 if (copy_to_user(uaddr, &ecmd, sizeof(ecmd)))
635 struct ethtool_value edata = { ETHTOOL_GLINK };
637 edata.data = netif_carrier_ok(netdev);
638 if (copy_to_user(uaddr, &edata, sizeof(edata)))
647 static int rtl8150_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd)
654 data = (u16 *) & rq->ifr_data;
660 res = rtl8150_ethtool_ioctl(netdev, rq->ifr_data);
664 case SIOCDEVPRIVATE + 1:
665 read_mii_word(dev, dev->phy, (data[1] & 0x1f), &data[3]);
667 case SIOCDEVPRIVATE + 2:
668 if (!capable(CAP_NET_ADMIN)) {
672 write_mii_word(dev, dev->phy, (data[1] & 0x1f), data[2]);
682 static void *rtl8150_probe(struct usb_device *udev, unsigned int ifnum,
683 const struct usb_device_id *id)
686 struct net_device *netdev;
688 udev->config[0].bConfigurationValue = 1;
689 if (usb_set_configuration(udev, udev->config[0].bConfigurationValue)) {
690 err("usb_set_configuration() failed");
693 dev = kmalloc(sizeof(rtl8150_t), GFP_KERNEL);
695 err("Out of memory");
698 memset(dev, 0, sizeof(rtl8150_t));
700 netdev = init_etherdev(NULL, 0);
703 err("Oh boy, out of memory again?!?");
708 init_MUTEX(&dev->sem);
710 dev->netdev = netdev;
711 SET_MODULE_OWNER(netdev);
713 netdev->open = rtl8150_open;
714 netdev->stop = rtl8150_close;
715 netdev->do_ioctl = rtl8150_ioctl;
716 netdev->watchdog_timeo = RTL8150_TX_TIMEOUT;
717 netdev->tx_timeout = rtl8150_tx_timeout;
718 netdev->hard_start_xmit = rtl8150_start_xmit;
719 netdev->set_multicast_list = rtl8150_set_multicast;
720 netdev->set_mac_address = rtl8150_set_mac_address;
721 netdev->get_stats = rtl8150_netdev_stats;
722 netdev->mtu = RTL8150_MTU;
723 dev->intr_interval = 100; /* 100ms */
725 if (rtl8150_reset(dev) || !alloc_all_urbs(dev)) {
726 err("couldn't reset the device");
728 unregister_netdev(dev->netdev);
735 set_ethernet_addr(dev);
736 /* let's not be very nasty :-) */
737 info("%s: rtl8150 is detected", netdev->name);
742 static void rtl8150_disconnect(struct usb_device *udev, void *ptr)
747 set_bit(RTL8150_UNPLUG, &dev->flags);
748 unregister_netdev(dev->netdev);
749 unlink_all_urbs(dev);
757 static int __init usb_rtl8150_init(void)
759 info(DRIVER_DESC " " DRIVER_VERSION);
760 return usb_register(&rtl8150_driver);
763 static void __exit usb_rtl8150_exit(void)
765 usb_deregister(&rtl8150_driver);
768 module_init(usb_rtl8150_init);
769 module_exit(usb_rtl8150_exit);
771 MODULE_AUTHOR(DRIVER_AUTHOR);
772 MODULE_DESCRIPTION(DRIVER_DESC);
773 MODULE_LICENSE("GPL");