31 #include <linux/sched.h>
32 #include <linux/slab.h>
35 #include <linux/netdevice.h>
37 #include <linux/ethtool.h>
38 #include <linux/mii.h>
40 #include <linux/module.h>
41 #include <asm/byteorder.h>
42 #include <asm/uaccess.h>
48 #define DRIVER_VERSION "v0.6.14 (2006/09/27)"
50 #define DRIVER_DESC "Pegasus/Pegasus II USB Ethernet driver"
54 #undef PEGASUS_WRITE_EEPROM
55 #define BMSR_MEDIA (BMSR_10HALF | BMSR_10FULL | BMSR_100HALF | \
56 BMSR_100FULL | BMSR_ANEGCAPABLE)
63 #define PEGASUS_DEV(pn, vid, pid, flags) \
64 {.name = pn, .vendor = vid, .device = pid, .private = flags},
65 #define PEGASUS_DEV_CLASS(pn, vid, pid, dclass, flags) \
66 PEGASUS_DEV(pn, vid, pid, flags)
69 #undef PEGASUS_DEV_CLASS
75 #define PEGASUS_DEV(pn, vid, pid, flags) \
76 {.match_flags = USB_DEVICE_ID_MATCH_DEVICE, .idVendor = vid, .idProduct = pid},
83 #define PEGASUS_DEV_CLASS(pn, vid, pid, dclass, flags) \
84 {.match_flags = (USB_DEVICE_ID_MATCH_DEVICE | USB_DEVICE_ID_MATCH_DEV_CLASS), \
85 .idVendor = vid, .idProduct = pid, .bDeviceClass = dclass},
88 #undef PEGASUS_DEV_CLASS
100 MODULE_PARM_DESC(mii_mode,
"Enable HomePNA mode (bit 0),default=MII mode = 0");
104 static int msg_level = -1;
111 static int update_eth_regs_async(
pegasus_t *);
113 static void ctrl_callback(
struct urb *
urb)
126 update_eth_regs_async(pegasus);
137 "%s, status %d\n", __func__, status);
154 "out of memory in %s\n", __func__);
171 usb_fill_control_urb(pegasus->
ctrl_urb, pegasus->
usb,
172 usb_rcvctrlpipe(pegasus->
usb, 0),
173 (
char *) &pegasus->
dr,
174 buffer, size, ctrl_callback, pegasus);
186 "%s, status %d\n", __func__, ret);
193 memcpy(data, buffer, size);
209 "out of memory in %s\n", __func__);
227 usb_fill_control_urb(pegasus->
ctrl_urb, pegasus->
usb,
228 usb_sndctrlpipe(pegasus->
usb, 0),
229 (
char *) &pegasus->
dr,
230 buffer, size, ctrl_callback, pegasus);
239 "%s, status %d\n", __func__, ret);
260 "out of memory in %s\n", __func__);
275 pegasus->
ctrl_urb->transfer_buffer_length = 1;
277 usb_fill_control_urb(pegasus->
ctrl_urb, pegasus->
usb,
278 usb_sndctrlpipe(pegasus->
usb, 0),
279 (
char *) &pegasus->
dr,
280 tmp, 1, ctrl_callback, pegasus);
290 "%s, status %d\n", __func__, ret);
302 static int update_eth_regs_async(
pegasus_t *pegasus)
311 pegasus->
ctrl_urb->transfer_buffer_length = 3;
313 usb_fill_control_urb(pegasus->
ctrl_urb, pegasus->
usb,
314 usb_sndctrlpipe(pegasus->
usb, 0),
315 (
char *) &pegasus->
dr,
316 pegasus->
eth_regs, 3, ctrl_callback, pegasus);
322 "%s, status %d\n", __func__, ret);
332 __u8 data[4] = {
phy, 0, 0, indx };
336 set_register(pegasus,
PhyCtrl, 0);
337 set_registers(pegasus,
PhyAddr,
sizeof(data), data);
340 ret = get_registers(pegasus,
PhyCtrl, 1, data);
347 if (i >= REG_TIMEOUT)
350 ret = get_registers(pegasus,
PhyData, 2, ®di);
355 netif_warn(pegasus, drv, pegasus->
net,
"%s failed\n", __func__);
365 read_mii_word(pegasus, phy_id, loc, &res);
372 __u8 data[4] = {
phy, 0, 0, indx };
376 data[2] = (
u8) (regd >> 8);
377 set_register(pegasus,
PhyCtrl, 0);
378 set_registers(pegasus,
PhyAddr,
sizeof(data), data);
381 ret = get_registers(pegasus,
PhyCtrl, 1, data);
388 if (i >= REG_TIMEOUT)
394 netif_warn(pegasus, drv, pegasus->
net,
"%s failed\n", __func__);
398 static void mdio_write(
struct net_device *dev,
int phy_id,
int loc,
int val)
402 write_mii_word(pegasus, phy_id, loc, val);
417 ret = get_registers(pegasus,
EpromCtrl, 1, &tmp);
423 if (i >= REG_TIMEOUT)
426 ret = get_registers(pegasus,
EpromData, 2, &retdatai);
431 netif_warn(pegasus, drv, pegasus->
net,
"%s failed\n", __func__);
435 #ifdef PEGASUS_WRITE_EEPROM
436 static inline void enable_eprom_write(
pegasus_t *pegasus)
441 get_registers(pegasus,
EthCtrl2, 1, &tmp);
445 static inline void disable_eprom_write(
pegasus_t *pegasus)
450 get_registers(pegasus,
EthCtrl2, 1, &tmp);
463 enable_eprom_write(pegasus);
465 set_registers(pegasus,
EpromData, 2, &le_data);
469 ret = get_registers(pegasus,
EpromCtrl, 1, &tmp);
472 if (tmp & EPROM_DONE)
475 disable_eprom_write(pegasus);
476 if (i >= REG_TIMEOUT)
482 netif_warn(pegasus, drv, pegasus->
net,
"%s failed\n", __func__);
492 for (i = 0; i < 3; i++) {
493 read_eprom_word(pegasus, i, &w16);
498 static void set_ethernet_addr(
pegasus_t *pegasus)
503 get_registers(pegasus, 0x10,
sizeof(node_id), node_id);
505 get_node_id(pegasus, node_id);
506 set_registers(pegasus,
EthID,
sizeof(node_id), node_id);
508 memcpy(pegasus->
net->dev_addr, node_id,
sizeof(node_id));
511 static inline int reset_mac(
pegasus_t *pegasus)
516 set_register(pegasus,
EthCtrl1, data);
518 get_registers(pegasus,
EthCtrl1, 1, &data);
523 set_register(pegasus,
Gpio1, 0x34);
525 set_register(pegasus,
Gpio1, 0x26);
531 if (i == REG_TIMEOUT)
536 set_register(pegasus,
Gpio0, 0x24);
537 set_register(pegasus,
Gpio0, 0x26);
541 read_mii_word(pegasus, 3, 0x1b, &auxmode);
542 write_mii_word(pegasus, 3, 0x1b, auxmode | 4);
548 static int enable_net_traffic(
struct net_device *dev,
struct usb_device *
usb)
555 read_mii_word(pegasus, pegasus->
phy,
MII_LPA, &linkpart);
567 ret = set_registers(pegasus,
EthCtrl0, 3, data);
573 read_mii_word(pegasus, 0, 0x1b, &auxmode);
574 write_mii_word(pegasus, 0, 0x1b, auxmode | 4);
580 static void fill_skb_pool(
pegasus_t *pegasus)
584 for (i = 0; i <
RX_SKBS; i++) {
594 skb_reserve(pegasus->
rx_pool[i], 2);
598 static void free_skb_pool(
pegasus_t *pegasus)
602 for (i = 0; i <
RX_SKBS; i++) {
604 dev_kfree_skb(pegasus->
rx_pool[i]);
615 for (i = 0; i <
RX_SKBS; i++) {
625 static void read_bulk_callback(
struct urb *urb)
630 int status = urb->status;
631 u8 *
buf = urb->transfer_buffer;
638 if (!netif_device_present(net) || !netif_running(net))
645 netif_dbg(pegasus, rx_err, net,
"reset MAC\n");
650 netif_warn(pegasus, rx_err, net,
"no rx stall recovery\n");
655 netif_dbg(pegasus, ifdown, net,
"rx unlink, %d\n", status);
658 netif_dbg(pegasus, rx_err, net,
"RX status %d\n", status);
662 if (!count || count < 4)
665 rx_status = buf[count - 2];
666 if (rx_status & 0x1e) {
668 "RX packet error %x\n", rx_status);
669 pegasus->
stats.rx_errors++;
670 if (rx_status & 0x06)
671 pegasus->
stats.rx_length_errors++;
672 if (rx_status & 0x08)
673 pegasus->
stats.rx_crc_errors++;
674 if (rx_status & 0x10)
675 pegasus->
stats.rx_frame_errors++;
678 if (pegasus->
chip == 0x8513) {
681 pegasus->
rx_skb->data += 2;
683 pkt_len = buf[count - 3] << 8;
684 pkt_len += buf[count - 4];
703 pegasus->
stats.rx_packets++;
710 pegasus->
rx_skb = pull_skb(pegasus);
716 usb_fill_bulk_urb(pegasus->
rx_urb, pegasus->
usb,
717 usb_rcvbulkpipe(pegasus->
usb, 1),
719 read_bulk_callback, pegasus);
723 else if (rx_status) {
733 tasklet_schedule(&pegasus->
rx_tl);
736 static void rx_fixup(
unsigned long data)
747 fill_skb_pool(pegasus);
752 pegasus->
rx_skb = pull_skb(pegasus);
754 netif_warn(pegasus, rx_err, pegasus->
net,
"low on memory\n");
755 tasklet_schedule(&pegasus->
rx_tl);
758 usb_fill_bulk_urb(pegasus->
rx_urb, pegasus->
usb,
759 usb_rcvbulkpipe(pegasus->
usb, 1),
761 read_bulk_callback, pegasus);
768 tasklet_schedule(&pegasus->
rx_tl);
776 static void write_bulk_callback(
struct urb *urb)
780 int status = urb->status;
787 if (!netif_device_present(net) || !netif_running(net))
793 netif_stop_queue(net);
794 netif_warn(pegasus, tx_err, net,
"no tx stall recovery\n");
799 netif_dbg(pegasus, ifdown, net,
"tx unlink, %d\n", status);
802 netif_info(pegasus, tx_err, net,
"TX status %d\n", status);
809 netif_wake_queue(net);
812 static void intr_callback(
struct urb *urb)
816 int res, status = urb->status;
836 if (urb->actual_length >= 6) {
837 u8 *d = urb->transfer_buffer;
842 pegasus->
stats.tx_errors++;
844 pegasus->
stats.tx_fifo_errors++;
846 pegasus->
stats.tx_aborted_errors++;
848 pegasus->
stats.tx_window_errors++;
857 pegasus->
stats.rx_missed_errors += ((d[3] & 0x7f) << 8) | d[4];
865 "can't resubmit interrupt urb, %d\n", res);
868 static void pegasus_tx_timeout(
struct net_device *net)
873 pegasus->
stats.tx_errors++;
880 int count = ((skb->
len + 2) & 0x3f) ? skb->
len + 2 : skb->
len + 3;
884 netif_stop_queue(net);
887 skb_copy_from_linear_data(skb, pegasus->
tx_buff + 2, skb->
len);
888 usb_fill_bulk_urb(pegasus->
tx_urb, pegasus->
usb,
889 usb_sndbulkpipe(pegasus->
usb, 2),
891 write_bulk_callback, pegasus);
893 netif_warn(pegasus, tx_err, net,
"fail tx, %d\n", res);
903 pegasus->
stats.tx_errors++;
904 netif_start_queue(net);
907 pegasus->
stats.tx_packets++;
908 pegasus->
stats.tx_bytes += skb->
len;
920 static inline void disable_net_traffic(
pegasus_t *pegasus)
924 set_registers(pegasus,
EthCtrl0,
sizeof(tmp), &tmp);
927 static inline void get_interrupt_interval(
pegasus_t *pegasus)
932 read_eprom_word(pegasus, 4, &data);
933 interval = data >> 8;
935 if (interval < 0x80) {
937 "intr interval changed from %ums to %ums\n",
940 data = (data & 0x00FF) | ((
u16)interval << 8);
941 #ifdef PEGASUS_WRITE_EEPROM
942 write_eprom_word(pegasus, 4, data);
949 static void set_carrier(
struct net_device *net)
954 if (read_mii_word(pegasus, pegasus->
phy,
MII_BMSR, &tmp))
963 static void free_all_urbs(
pegasus_t *pegasus)
971 static void unlink_all_urbs(
pegasus_t *pegasus)
979 static int alloc_urbs(
pegasus_t *pegasus)
1006 static int pegasus_open(
struct net_device *net)
1012 pegasus->
rx_skb = pull_skb(pegasus);
1021 usb_fill_bulk_urb(pegasus->
rx_urb, pegasus->
usb,
1022 usb_rcvbulkpipe(pegasus->
usb, 1),
1024 read_bulk_callback, pegasus);
1028 netif_dbg(pegasus, ifup, net,
"failed rx_urb, %d\n", res);
1033 usb_rcvintpipe(pegasus->
usb, 3),
1039 netif_dbg(pegasus, ifup, net,
"failed intr_urb, %d\n", res);
1043 if ((res = enable_net_traffic(net, pegasus->
usb))) {
1045 "can't enable_net_traffic() - %d\n", res);
1049 free_skb_pool(pegasus);
1053 netif_start_queue(net);
1054 netif_dbg(pegasus, ifup, net,
"open\n");
1060 static int pegasus_close(
struct net_device *net)
1064 netif_stop_queue(net);
1066 disable_net_traffic(pegasus);
1068 unlink_all_urbs(pegasus);
1073 static void pegasus_get_drvinfo(
struct net_device *dev,
1083 #define WOL_SUPPORTED (WAKE_MAGIC|WAKE_PHY)
1116 static inline void pegasus_reset_wol(
struct net_device *dev)
1120 memset(&wol, 0,
sizeof wol);
1121 (
void) pegasus_set_wol(dev, &wol);
1129 pegasus = netdev_priv(dev);
1141 static int pegasus_nway_reset(
struct net_device *dev)
1166 .get_drvinfo = pegasus_get_drvinfo,
1167 .get_settings = pegasus_get_settings,
1168 .set_settings = pegasus_set_settings,
1169 .nway_reset = pegasus_nway_reset,
1170 .get_link = pegasus_get_link,
1171 .get_msglevel = pegasus_get_msglevel,
1172 .set_msglevel = pegasus_set_msglevel,
1173 .get_wol = pegasus_get_wol,
1174 .set_wol = pegasus_set_wol,
1185 data[0] = pegasus->
phy;
1187 read_mii_word(pegasus, data[0], data[1] & 0x1f, &data[3]);
1193 write_mii_word(pegasus, pegasus->
phy, data[1] & 0x1f, data[2]);
1202 static void pegasus_set_multicast(
struct net_device *net)
1229 for (i = 0; i < 32; i++) {
1230 read_mii_word(pegasus, i,
MII_BMSR, &tmp);
1231 if (tmp == 0 || tmp == 0xffff || (tmp &
BMSR_MEDIA) == 0)
1240 static inline void setup_pegasus_II(
pegasus_t *pegasus)
1244 set_register(pegasus,
Reg1d, 0);
1245 set_register(pegasus,
Reg7b, 1);
1248 set_register(pegasus,
Reg7b, 0);
1250 set_register(pegasus,
Reg7b, 2);
1252 set_register(pegasus, 0x83, data);
1253 get_registers(pegasus, 0x83, 1, &data);
1256 pegasus->
chip = 0x8513;
1260 set_register(pegasus, 0x80, 0xc0);
1261 set_register(pegasus, 0x83, 0xff);
1262 set_register(pegasus, 0x84, 0x01);
1265 set_register(pegasus,
Reg81, 6);
1267 set_register(pegasus,
Reg81, 2);
1271 static int pegasus_count;
1273 #define CARRIER_CHECK_DELAY (2 * HZ)
1278 set_carrier(pegasus->
net);
1285 static int pegasus_blacklisted(
struct usb_device *
udev)
1304 static void pegasus_dec_workqueue(
void)
1307 if (pegasus_count == 0) {
1309 pegasus_workqueue =
NULL;
1316 struct usb_device *dev = interface_to_usbdev(intf);
1319 int dev_index =
id - pegasus_ids;
1322 if (pegasus_blacklisted(dev))
1325 if (pegasus_count == 0) {
1327 if (!pegasus_workqueue)
1332 net = alloc_etherdev(
sizeof(
struct pegasus));
1336 pegasus = netdev_priv(net);
1340 if (!alloc_urbs(pegasus)) {
1341 dev_err(&intf->dev,
"can't allocate %s\n",
"urbs");
1357 pegasus->
mii.dev = net;
1358 pegasus->
mii.mdio_read = mdio_read;
1359 pegasus->
mii.mdio_write = mdio_write;
1360 pegasus->
mii.phy_id_mask = 0x1f;
1361 pegasus->
mii.reg_num_mask = 0x1f;
1367 get_interrupt_interval(pegasus);
1368 if (reset_mac(pegasus)) {
1369 dev_err(&intf->dev,
"can't reset MAC\n");
1373 set_ethernet_addr(pegasus);
1374 fill_skb_pool(pegasus);
1376 dev_info(&intf->dev,
"setup Pegasus II specific registers\n");
1377 setup_pegasus_II(pegasus);
1379 pegasus->
phy = mii_phy_probe(pegasus);
1380 if (pegasus->
phy == 0xff) {
1381 dev_warn(&intf->dev,
"can't locate MII phy, using default\n");
1384 pegasus->
mii.phy_id = pegasus->
phy;
1385 usb_set_intfdata(intf, pegasus);
1387 pegasus_reset_wol(net);
1394 dev_info(&intf->dev,
"%s, %s, %pM\n",
1396 usb_dev_id[dev_index].
name,
1401 usb_set_intfdata(intf,
NULL);
1402 free_skb_pool(pegasus);
1404 free_all_urbs(pegasus);
1408 pegasus_dec_workqueue();
1414 struct pegasus *pegasus = usb_get_intfdata(intf);
1416 usb_set_intfdata(intf,
NULL);
1418 dev_dbg(&intf->dev,
"unregistering non-bound device?\n");
1425 unlink_all_urbs(pegasus);
1426 free_all_urbs(pegasus);
1427 free_skb_pool(pegasus);
1429 dev_kfree_skb(pegasus->
rx_skb);
1433 pegasus_dec_workqueue();
1438 struct pegasus *pegasus = usb_get_intfdata(intf);
1442 if (netif_running(pegasus->
net)) {
1451 struct pegasus *pegasus = usb_get_intfdata(intf);
1454 if (netif_running(pegasus->
net)) {
1455 pegasus->
rx_urb->status = 0;
1456 pegasus->
rx_urb->actual_length = 0;
1457 read_bulk_callback(pegasus->
rx_urb);
1460 pegasus->
intr_urb->actual_length = 0;
1469 .ndo_open = pegasus_open,
1470 .ndo_stop = pegasus_close,
1471 .ndo_do_ioctl = pegasus_ioctl,
1472 .ndo_start_xmit = pegasus_start_xmit,
1473 .ndo_set_rx_mode = pegasus_set_multicast,
1474 .ndo_get_stats = pegasus_netdev_stats,
1475 .ndo_tx_timeout = pegasus_tx_timeout,
1481 static struct usb_driver pegasus_driver = {
1483 .probe = pegasus_probe,
1484 .disconnect = pegasus_disconnect,
1485 .id_table = pegasus_ids,
1486 .suspend = pegasus_suspend,
1487 .resume = pegasus_resume,
1488 .disable_hub_initiated_lpm = 1,
1491 static void __init parse_id(
char *
id)
1504 pr_info(
"%s: new device %s, vendor ID 0x%04x, device ID 0x%04x, flags: 0x%x\n",
1507 if (vendor_id > 0x10000 || vendor_id == 0)
1512 for (i = 0; usb_dev_id[
i].
name; i++);
1517 pegasus_ids[
i].match_flags = USB_DEVICE_ID_MATCH_DEVICE;
1522 static int __init pegasus_init(
void)
1527 return usb_register(&pegasus_driver);
1530 static void __exit pegasus_exit(
void)