Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 | /*
* ASIX AX8817x USB 2.0 10/100/HomePNA Ethernet controller driver
*
* $Id: ax8817x.c,v 1.15 2003/06/15 18:45:21 dhollis Exp $
*
* Copyright (c) 2002-2003 TiVo Inc.
*
* This software may be used and distributed according to the terms
* of the GNU General Public License, incorporated herein by reference.
*
* History
*
* 2003-06-28 - Dave Hollis <dhollis@davehollis.com> 1.0.2
* * Added support for Intellinet
* 2003-06-12 - Dave Hollis <dhollis@davehollis.com> 1.0.1
* * use usb_make_path for ethtool info
* * Use crc32.h for crc functions
* * Additional callback cases for other host ctrlrs
* * Force minimum default rcv buffers
*
* 2003-06-12 - Dave Hollis <dhollis@davehollis.com> 1.0.0
* * Backport removal of multiple tx_urb / lengthy
* start_xmit routines, etc.
* * ethtool driver name returns driver name, not
* long description
*
* 2003-06-05 - Dave Hollis <dhollis@davehollis.com> 0.9.9
* * Cleanup unnecessary #if 0
* * Fix coding style to match kernel style
*
* 2003-05-31 - Dave Hollis <dhollis@davehollis.com> 0.9.8
* * Don't stop/start the queue in start_xmit
* * Swallow URB status upon hard removal
* * Cleanup remaining comments (kill // style)
*
* 2003-05-29 - Dave Hollis <dhollis@davehollis.com> 0.9.7
* * Set module owner
* * Follow-up on suggestions from David Brownell &
* Oliver Neukum which should help with robustness
* * Use ether_crc from stock kernel if available
*
* 2003-05-28 - Dave Hollis <dhollis@davehollis.com> 0.9.6
* * Added basic ethtool & mii support
*
* 2003-05-28 - Dave Hollis <dhollis@davehollis.com> 0.9.5
* * Workout devrequest change to usb_ctrlrequest structure
* * Replace FILL_BULK_URB macros to non-deprecated
* usb_fill_bulk_urb macros
* * Replace printks with equivalent macros
* * Use defines for module description, version, author to
* simplify future changes
*
* Known Issues
* usb-uhci.c: process_transfer: fixed toggle message to console
* Possible bug in usb-uhci or bug in this driver that
* makes it spit that out. Doesn't seem to have harmful
* effects.
*
* Todo
* Fix mii/ethtool output
*/
#include <linux/slab.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/usb.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/ethtool.h>
#include <linux/skbuff.h>
#include <linux/mii.h>
#include <asm/uaccess.h>
#include <linux/crc32.h>
/* Version Information */
#define DRIVER_VERSION "v1.0.0"
#define DRIVER_AUTHOR "TiVo, Inc."
#define DRIVER_DESC "ASIX AX8817x USB Ethernet driver"
MODULE_DESCRIPTION(DRIVER_DESC);
MODULE_AUTHOR(DRIVER_AUTHOR);
MODULE_LICENSE("GPL");
#define AX_REQ_READ ( USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE )
#define AX_REQ_WRITE ( USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE )
#define AX_CMD_SET_SW_MII 0x06
#define AX_CMD_READ_MII_REG 0x07
#define AX_CMD_WRITE_MII_REG 0x08
#define AX_CMD_SET_HW_MII 0x0a
#define AX_CMD_WRITE_RX_CTL 0x10
#define AX_CMD_WRITE_MULTI_FILTER 0x16
#define AX_CMD_READ_NODE_ID 0x17
#define AX_CMD_READ_PHY_ID 0x19
#define AX_CMD_WRITE_MEDIUM_MODE 0x1b
#define AX_CMD_WRITE_GPIOS 0x1f
#define AX_RX_MAX ETH_FRAME_LEN
#define AX_TIMEOUT_CMD ( HZ / 10 )
#define AX_TIMEOUT_TX ( HZ * 2 )
#define AX_MAX_MCAST 64
#define AX_DRV_STATE_INITIALIZING 0x00
#define AX_DRV_STATE_RUNNING 0x01
#define AX_DRV_STATE_EXITING 0x02
#define AX_PHY_STATE_INITIALIZING 0x00
#define AX_PHY_STATE_NO_LINK 0x01
#define AX_PHY_STATE_POLLING_1 0x02
#define AX_PHY_STATE_POLLING_2 0x03
#define AX_PHY_STATE_POLLING_3 0x04
#define AX_PHY_STATE_POLLING_4 0x05
#define AX_PHY_STATE_SETTING_MAC 0x06
#define AX_PHY_STATE_LINK 0x07
#define AX_PHY_STATE_ABORT_POLL 0x08
#define AX_PHY_STATE_ABORTING 0x09
#define AX_MAX_PHY_RETRY 50
#define AX_RX_URBS_DEFAULT 2
static const char driver_name[] = "ax8817x";
static int n_rx_urbs = AX_RX_URBS_DEFAULT;
MODULE_PARM(n_rx_urbs, "i");
MODULE_PARM_DESC(n_rx_urbs,
"Number of rx buffers to queue at once (def 2)");
struct ax8817x_info;
struct ax_cmd_req;
typedef int (*ax_cmd_callback_t) (struct ax8817x_info *,
struct ax_cmd_req *);
struct ax_cmd_req {
struct list_head list;
ax_cmd_callback_t cmd_callback;
void *priv;
int status;
void *data;
int data_size;
int timeout;
struct usb_ctrlrequest devreq;
};
struct ax8817x_info {
struct usb_device *usb;
struct net_device *net;
struct urb **rx_urbs;
struct urb *tx_urb;
struct urb *int_urb;
u8 *int_buf;
struct urb *ctl_urb;
struct list_head ctl_queue;
spinlock_t ctl_lock;
atomic_t rx_refill_cnt;
int tx_head;
int tx_tail;
spinlock_t tx_lock;
struct net_device_stats stats;
struct ax_cmd_req phy_req;
u8 phy_id;
u8 phy_state;
u8 drv_state;
};
const struct usb_device_id ax8817x_id_table[] __devinitdata = {
/* Linksys USB200M */
{USB_DEVICE(0x077b, 0x2226), driver_info:0x00130103},
/* Hawking UF200, TRENDnet TU2-ET100 */
{USB_DEVICE(0x07b8, 0x420a), driver_info:0x001f1d1f},
/* NETGEAR FA120 */
{USB_DEVICE(0x0846, 0x1040), driver_info:0x00130103},
/* D-Link DUB-E100 */
{USB_DEVICE(0x2001, 0x1a00), driver_info:0x009f9d9f},
/* Intellinet USB Ethernet*/
{USB_DEVICE(0x0b95, 0x1720), driver_info:0x00130103},
{}
};
MODULE_DEVICE_TABLE(usb, ax8817x_id_table);
static void ax_run_ctl_queue(struct ax8817x_info *, struct ax_cmd_req *,
int);
static void ax_rx_callback(struct urb *urb);
static void ax_ctl_callback(struct urb *urb)
{
struct ax8817x_info *ax_info =
(struct ax8817x_info *) urb->context;
ax_run_ctl_queue(ax_info, NULL,
urb->status ? urb->status : urb->actual_length);
}
/*
* Queue a new ctl request, or dequeue the first in the list
*/
static void ax_run_ctl_queue(struct ax8817x_info *ax_info,
struct ax_cmd_req *req, int status)
{
struct ax_cmd_req *next_req = NULL;
struct ax_cmd_req *last_req = NULL;
unsigned long flags;
/* Need to lock around queue list manipulation */
spin_lock_irqsave(&ax_info->ctl_lock, flags);
if (req == NULL) {
last_req =
list_entry(ax_info->ctl_queue.next, struct ax_cmd_req,
list);
} else {
if (list_empty(&ax_info->ctl_queue)) {
next_req = req;
}
req->status = -EINPROGRESS;
list_add_tail(&req->list, &ax_info->ctl_queue);
}
while (1) {
if (last_req != NULL) {
/* dequeue completed entry */
list_del(&last_req->list);
last_req->status = status;
if (last_req->cmd_callback(ax_info, last_req)) {
/* requeue if told to do so */
last_req->status = -EINPROGRESS;
list_add_tail(&last_req->list,
&ax_info->ctl_queue);
}
if (list_empty(&ax_info->ctl_queue)) {
next_req = NULL;
} else {
next_req =
list_entry(ax_info->ctl_queue.next,
struct ax_cmd_req, list);
}
}
spin_unlock_irqrestore(&ax_info->ctl_lock, flags);
if (next_req == NULL) {
break;
}
/* XXX: do something with timeout */
usb_fill_control_urb(ax_info->ctl_urb, ax_info->usb,
next_req->devreq.
bRequestType & USB_DIR_IN ?
usb_rcvctrlpipe(ax_info->usb,
0) :
usb_sndctrlpipe(ax_info->usb, 0),
(void *) &next_req->devreq,
next_req->data, next_req->data_size,
ax_ctl_callback, ax_info);
status = usb_submit_urb(ax_info->ctl_urb);
if (status >= 0) {
break;
}
last_req = next_req;
spin_lock_irqsave(&ax_info->ctl_lock, flags);
}
}
static int ax_sync_cmd_callback(struct ax8817x_info *unused,
struct ax_cmd_req *req)
{
wait_queue_head_t *wq = (wait_queue_head_t *) req->priv;
wake_up(wq);
return 0;
}
static int ax_async_cmd_callback(struct ax8817x_info *unused,
struct ax_cmd_req *req)
{
if (req->status < 0) {
err("%s: Async command %d failed: %d\n", __FUNCTION__,
req->devreq.bRequest, req->status);
}
/* Nothing else to do here, just need to free the request (and its
allocated data) */
if (req->data != NULL) {
kfree(req->data);
}
kfree(req);
return 0;
}
/*
* This is mostly the same as usb_control_msg(), except that it is able
* to queue control messages
*/
static int ax_control_msg(struct ax8817x_info *ax_info, u8 requesttype,
u8 request, u16 value, u16 index, void *data,
u16 size, int timeout)
{
struct ax_cmd_req *req;
DECLARE_WAIT_QUEUE_HEAD(wq);
DECLARE_WAITQUEUE(wait, current);
int ret;
req = kmalloc(sizeof(struct ax_cmd_req), GFP_KERNEL);
if (req == NULL) {
return -ENOMEM;
}
req->devreq.bRequestType = requesttype;
req->devreq.bRequest = request;
req->devreq.wValue = cpu_to_le16(value);
req->devreq.wIndex = cpu_to_le16(index);
req->devreq.wLength = cpu_to_le16(size);
req->data = data;
req->data_size = size;
req->timeout = timeout;
req->priv = &wq;
set_current_state(TASK_UNINTERRUPTIBLE);
add_wait_queue(&wq, &wait);
req->cmd_callback = ax_sync_cmd_callback;
ax_run_ctl_queue(ax_info, req, 0);
schedule();
ret = req->status;
kfree(req);
return ret;
}
/*
* Same, but can be used asynchronously, may fail, and returns no exit
* status
*/
static void ax_control_msg_async(struct ax8817x_info *ax_info,
u8 requesttype, u8 request, u16 value,
u16 index, void *data, u16 size,
int timeout)
{
struct ax_cmd_req *req;
req = kmalloc(sizeof(struct ax_cmd_req), GFP_ATOMIC);
if (req == NULL) {
/* There's not much else we can do here... */
err("%s: Failed alloc\n", __FUNCTION__);
return;
}
req->devreq.bRequestType = requesttype;
req->devreq.bRequest = request;
req->devreq.wValue = cpu_to_le16(value);
req->devreq.wIndex = cpu_to_le16(index);
req->devreq.wLength = cpu_to_le16(size);
req->data = data;
req->data_size = size;
req->timeout = timeout;
req->cmd_callback = ax_async_cmd_callback;
ax_run_ctl_queue(ax_info, req, 0);
}
static inline int ax_read_cmd(struct ax8817x_info *ax_info, u8 cmd,
u16 value, u16 index, u16 size, void *data)
{
return ax_control_msg(ax_info, AX_REQ_READ, cmd, value, index,
data, size, AX_TIMEOUT_CMD);
}
static inline int ax_write_cmd(struct ax8817x_info *ax_info, u8 cmd,
u16 value, u16 index, u16 size, void *data)
{
return ax_control_msg(ax_info, AX_REQ_WRITE, cmd, value, index,
data, size, AX_TIMEOUT_CMD);
}
static inline void ax_write_cmd_async(struct ax8817x_info *ax_info, u8 cmd,
u16 value, u16 index, u16 size,
void *data)
{
ax_control_msg_async(ax_info, AX_REQ_WRITE, cmd, value, index,
data, size, AX_TIMEOUT_CMD);
}
static int ax_refill_rx_urb(struct ax8817x_info *ax_info, struct urb *urb)
{
struct sk_buff *skb;
int ret;
skb = dev_alloc_skb(AX_RX_MAX + 2);
if (skb != NULL) {
skb_reserve(skb, 2); /* for IP header alignment */
skb->dev = ax_info->net;
usb_fill_bulk_urb(urb, ax_info->usb,
usb_rcvbulkpipe(ax_info->usb, 3),
skb->data, AX_RX_MAX, ax_rx_callback,
skb);
ret = usb_submit_urb(urb);
if (ret < 0) {
err("Failed submit rx URB (%d)\n", ret);
dev_kfree_skb_irq(skb);
urb->context = NULL;
} else {
ret = 0;
}
} else {
/* this just means we're low on memory at the moment. Try to
handle it gracefully. */
urb->context = NULL;
ret = 1;
}
return ret;
}
static int ax_phy_cmd_callback(struct ax8817x_info *ax_info,
struct ax_cmd_req *req)
{
int full_duplex;
int flow_control;
u16 mii_data_le;
if (req->status < 0) {
err("%s: Failed at state %d: %d\n", __FUNCTION__,
ax_info->phy_state, req->status);
/* Not sure what else we can do, so just bail */
ax_info->phy_state = AX_PHY_STATE_ABORTING;
}
switch (ax_info->phy_state) {
/* Now that we're in software MII mode, read the BMSR */
case AX_PHY_STATE_POLLING_1:
ax_info->phy_state = AX_PHY_STATE_POLLING_2;
req->devreq.bRequestType = AX_REQ_READ;
req->devreq.bRequest = AX_CMD_READ_MII_REG;
req->devreq.wValue = cpu_to_le16(ax_info->phy_id);
req->devreq.wIndex = cpu_to_le16(MII_BMSR);
req->devreq.wLength = cpu_to_le16(2);
req->data_size = 2;
(long) req->priv = 0; /* This is the retry count */
return 1;
/* Done reading BMSR */
case AX_PHY_STATE_POLLING_2:
mii_data_le = *(u16 *) req->data;
if ((mii_data_le &
cpu_to_le16(BMSR_LSTATUS | BMSR_ANEGCAPABLE))
== cpu_to_le16(BMSR_LSTATUS | BMSR_ANEGCAPABLE)) {
if (mii_data_le & cpu_to_le16(BMSR_ANEGCOMPLETE)) {
/* Autonegotiation done, go on to read LPA */
ax_info->phy_state =
AX_PHY_STATE_POLLING_3;
req->devreq.wIndex = cpu_to_le16(MII_LPA);
return 1;
} else if ((long) req->priv++ < AX_MAX_PHY_RETRY) {
/* Reread BMSR if it's still autonegotiating. This is
probably unnecessary logic, I've never seen it take
more than 1 try... */
return 1;
}
/* else fall through to abort */
}
/* XXX: should probably handle auto-neg failure better,
by reverting to manual setting of something safe. (?) */
ax_info->phy_state = AX_PHY_STATE_ABORT_POLL;
/* and then fall through to set hw MII */
/* Got what we needed from PHY, set back to hardware MII mode
(Do same for abort in mid-poll) */
case AX_PHY_STATE_POLLING_3:
case AX_PHY_STATE_ABORT_POLL:
ax_info->phy_state += 1;
req->devreq.bRequestType = AX_REQ_WRITE;
req->devreq.bRequest = AX_CMD_SET_HW_MII;
req->devreq.wValue = cpu_to_le16(0);
req->devreq.wIndex = cpu_to_le16(0);
req->devreq.wLength = cpu_to_le16(0);
req->data_size = 0;
return 1;
/* The end result, set the right duplex and flow control mode in the
MAC (based on the PHY's LPA reg, which should still be in the data
buffer) */
case AX_PHY_STATE_POLLING_4:
mii_data_le = *(u16 *) req->data;
ax_info->phy_state = AX_PHY_STATE_SETTING_MAC;
req->devreq.bRequest = AX_CMD_WRITE_MEDIUM_MODE;
full_duplex = mii_data_le & cpu_to_le16(LPA_DUPLEX);
flow_control = full_duplex &&
(mii_data_le & cpu_to_le16(0x0400));
req->devreq.wValue = cpu_to_le16(0x04) |
(full_duplex ? cpu_to_le16(0x02) : 0) |
(flow_control ? cpu_to_le16(0x10) : 0);
info("%s: Link established, %s duplex, flow control %sabled\n", ax_info->net->name, full_duplex ? "full" : "half", flow_control ? "en" : "dis");
return 1;
/* All done */
case AX_PHY_STATE_SETTING_MAC:
ax_info->phy_state = AX_PHY_STATE_LINK;
netif_carrier_on(ax_info->net);
return 0;
default:
err("%s: Unknown state %d\n", __FUNCTION__,
ax_info->phy_state);
/* fall through */
case AX_PHY_STATE_ABORTING:
ax_info->phy_state = AX_PHY_STATE_NO_LINK;
return 0;
}
}
static void ax_int_callback(struct urb *urb)
{
struct ax8817x_info *ax_info =
(struct ax8817x_info *) urb->context;
u8 phy_link;
if (ax_info->drv_state == AX_DRV_STATE_EXITING ||
urb->actual_length < 3) {
return;
}
/* Ignore the first PHY link report, it will sometimes be reported as
link active, even though we just told the PHY to reset. If it
really has link, we'll pick it up next int callback.
*/
if (ax_info->phy_state == AX_PHY_STATE_INITIALIZING) {
netif_carrier_off(ax_info->net);
ax_info->phy_state = AX_PHY_STATE_NO_LINK;
return;
}
/* Assume we're only interested in the primary PHY for now. */
phy_link = ax_info->int_buf[2] & 1;
if (phy_link ==
(ax_info->phy_state == AX_PHY_STATE_NO_LINK) ? 0 : 1) {
/* Common case, no change */
return;
}
if (phy_link == 0) {
netif_carrier_off(ax_info->net);
/* Abort an in-progress poll of the PHY if necessary */
switch (ax_info->phy_state) {
case AX_PHY_STATE_POLLING_1:
case AX_PHY_STATE_POLLING_2:
case AX_PHY_STATE_POLLING_3:
ax_info->phy_state = AX_PHY_STATE_ABORT_POLL;
break;
case AX_PHY_STATE_POLLING_4:
case AX_PHY_STATE_SETTING_MAC:
ax_info->phy_state = AX_PHY_STATE_ABORTING;
break;
case AX_PHY_STATE_LINK:
ax_info->phy_state = AX_PHY_STATE_NO_LINK;
break;
default:
/* If we're already aborting, continue aborting */
break;
}
} else {
/* Note that we only fall into this case if previous phy_state was
AX_PHY_STATE_NO_LINK. When the link is reported active while
we're still polling, or when we're aborting, the logic above
will just return, and we'll check again next int callback. */
ax_info->phy_state = AX_PHY_STATE_POLLING_1;
ax_info->phy_req.devreq.bRequestType = AX_REQ_WRITE;
ax_info->phy_req.devreq.bRequest = AX_CMD_SET_SW_MII;
ax_info->phy_req.devreq.wValue = cpu_to_le16(0);
ax_info->phy_req.devreq.wIndex = cpu_to_le16(0);
ax_info->phy_req.devreq.wLength = cpu_to_le16(0);
ax_info->phy_req.data_size = 0;
ax_info->phy_req.timeout = AX_TIMEOUT_CMD;
ax_info->phy_req.cmd_callback = ax_phy_cmd_callback;
ax_run_ctl_queue(ax_info, &ax_info->phy_req, 0);
}
}
static void ax_rx_callback(struct urb *urb)
{
struct sk_buff *skb = (struct sk_buff *) urb->context;
struct net_device *net = skb->dev;
struct ax8817x_info *ax_info = (struct ax8817x_info *) net->priv;
int ret, len, refill;
switch (urb->status) {
case 0:
break;
default:
err("%s: URB status %d\n", __FUNCTION__, urb->status);
/* It's not clear that we can do much in this case, the rx pipe
doesn't ever seem to stall, so if we got -ETIMEDOUT, that
usually means the device was unplugged, and we just haven't
noticed yet.
Just fall through and free skb without resubmitting urb. */
case -ENOENT: /* */
case -ECONNRESET: /* Async unlink */
case -ESHUTDOWN: /* Hardware gone */
case -EILSEQ: /* Get this when you yank it out on UHCI */
case -ETIMEDOUT: /* OHCI */
case -EPROTO: /* EHCI */
case -EPIPE:
dev_kfree_skb_any(skb);
urb->context = NULL;
return;
}
if (ax_info->drv_state == AX_DRV_STATE_INITIALIZING) {
/* Not really expecting this to ever happen, since we haven't yet
enabled receive in the rx_ctl register, but ya never know... */
goto refill_same;
} else if (ax_info->drv_state == AX_DRV_STATE_EXITING) {
dev_kfree_skb_any(skb);
urb->context = NULL;
return;
}
len = urb->actual_length;
if (len == 0) {
/* this shouldn't happen... */
goto refill_same;
}
refill = ax_refill_rx_urb(ax_info, urb);
if (refill == 0
|| atomic_read(&ax_info->rx_refill_cnt) < n_rx_urbs) {
/* Send the receive buffer up the network stack */
skb_put(skb, len);
skb->protocol = eth_type_trans(skb, net);
net->last_rx = jiffies;
ax_info->stats.rx_packets++;
ax_info->stats.rx_bytes += len;
netif_rx(skb);
if (refill == 0) {
int i;
/* This is the common case. This URB got refilled OK, and
no other URBs need to be refilled. */
if (atomic_read(&ax_info->rx_refill_cnt) == 0) {
return;
}
for (i = 0; i < n_rx_urbs; i++) {
struct urb *urb = ax_info->rx_urbs[i];
if (urb->context == NULL) {
if (ax_refill_rx_urb(ax_info, urb)
== 0) {
atomic_dec(&ax_info->
rx_refill_cnt);
} else {
break;
}
}
}
} else {
/* remember to refill this one later */
atomic_inc(&ax_info->rx_refill_cnt);
}
return;
} else {
ax_info->stats.rx_dropped++;
if (refill < 0) {
/* the error code was already printk'ed in ax_refill_rx_urb()
so just note the consequences here: */
warn("Halting rx due to error\n");
return;
}
/* fall through to resubmit this URB with the existing skb
will try to reallocate skb's on next rx callback */
}
refill_same:
usb_fill_bulk_urb(urb, ax_info->usb,
usb_rcvbulkpipe(ax_info->usb, 3), skb->data,
AX_RX_MAX, ax_rx_callback, skb);
ret = usb_submit_urb(urb);
if (ret < 0) {
err("Failed submit rx URB (%d)\n", ret);
}
}
static int ax8817x_open(struct net_device *net)
{
struct ax8817x_info *ax_info = (struct ax8817x_info *) net->priv;
u8 buf[4];
int i, ret;
ret = ax_write_cmd(ax_info, AX_CMD_WRITE_RX_CTL, 0x80, 0, 0, buf);
if (ret < 0) {
return ret;
}
ret = 0;
ax_info->tx_urb = usb_alloc_urb(0);
if (ax_info->tx_urb == NULL)
ret = -ENOMEM;
atomic_set(&ax_info->rx_refill_cnt, 0);
for (i = 0; i < n_rx_urbs && ret == 0; i++) {
struct urb *urb = ax_info->rx_urbs[i];
if (urb == NULL) {
urb = ax_info->rx_urbs[i] = usb_alloc_urb(0);
if (urb == NULL) {
ret = -ENOMEM;
break;
}
if (n_rx_urbs > 1) {
urb->transfer_flags |= USB_QUEUE_BULK;
}
}
ret = ax_refill_rx_urb(ax_info, urb);
if (ret == 1) {
atomic_inc(&ax_info->rx_refill_cnt);
ret = 0;
}
}
/* XXX: should handle the case where we couldn't allocate any skb's
better. They get allocated with GFP_ATOMIC, so they may all fail... */
if (ret == 0 && atomic_read(&ax_info->rx_refill_cnt) < n_rx_urbs) {
netif_start_queue(net);
} else {
/* Error: clean up anything we allocated and bail. */
usb_free_urb(ax_info->tx_urb);
for (i = 0; i < n_rx_urbs; i++) {
struct urb *urb = ax_info->rx_urbs[i];
if (urb != NULL) {
/* skb gets freed in the URB callback */
usb_unlink_urb(urb);
usb_free_urb(urb);
}
}
err("%s: Failed start rx queue (%d)\n", __FUNCTION__, ret);
}
return ret;
}
static int ax8817x_stop(struct net_device *net)
{
struct ax8817x_info *ax_info = (struct ax8817x_info *) net->priv;
u8 buf[4];
int i, ret;
netif_stop_queue(net);
ret = ax_write_cmd(ax_info, AX_CMD_WRITE_RX_CTL, 0x80, 0, 0, buf);
if (ret < 0 && ax_info->drv_state != AX_DRV_STATE_EXITING) {
err("%s: Failed cmd (%d)\n", __FUNCTION__, ret);
}
if (ax_info->tx_urb != NULL) {
usb_unlink_urb(ax_info->tx_urb);
usb_free_urb(ax_info->tx_urb);
ax_info->tx_urb = NULL;
}
for (i = 0; i < n_rx_urbs; i++) {
struct urb *urb = ax_info->rx_urbs[i];
if (urb != NULL) {
/* skb gets freed in the URB callback */
usb_unlink_urb(urb);
usb_free_urb(urb);
ax_info->rx_urbs[i] = NULL;
}
}
return 0;
}
static void write_bulk_callback(struct urb *urb)
{
struct ax8817x_info *ax_info = urb->context;
if (!ax_info || (ax_info->drv_state == AX_DRV_STATE_EXITING))
return;
if (!netif_device_present(ax_info->net))
return;
if (urb->status)
info("%s: TX status %d", ax_info->net->name, urb->status);
ax_info->net->trans_start = jiffies;
netif_wake_queue(ax_info->net);
}
static int ax8817x_start_xmit(struct sk_buff *skb, struct net_device *net)
{
struct ax8817x_info *ax_info = net->priv;
int res;
netif_stop_queue(net);
ax_info->tx_urb->transfer_flags |= USB_ZERO_PACKET;
usb_fill_bulk_urb(ax_info->tx_urb, ax_info->usb,
usb_sndbulkpipe(ax_info->usb, 2),
skb->data, skb->len, write_bulk_callback,
ax_info);
if ((res = usb_submit_urb(ax_info->tx_urb))) {
warn("Failed tx_urb %d", res);
ax_info->stats.tx_errors++;
netif_start_queue(net);
} else {
ax_info->stats.tx_packets++;
ax_info->stats.tx_bytes += skb->len;
net->trans_start = jiffies;
}
dev_kfree_skb(skb);
return 0;
}
static void ax8817x_tx_timeout(struct net_device *net)
{
struct ax8817x_info *ax_info = net->priv;
if (!ax_info)
return;
warn("%s: Tx timed out.", net->name);
ax_info->tx_urb->transfer_flags |= USB_ASYNC_UNLINK;
usb_unlink_urb(ax_info->tx_urb);
ax_info->stats.tx_errors++;
}
static struct net_device_stats *ax8817x_stats(struct net_device *net)
{
struct ax8817x_info *ax_info = (struct ax8817x_info *) net->priv;
return &ax_info->stats;
}
static void ax8817x_set_multicast(struct net_device *net)
{
struct ax8817x_info *ax_info = (struct ax8817x_info *) net->priv;
u8 rx_ctl = 0x8c;
if (net->flags & IFF_PROMISC) {
rx_ctl |= 0x01;
} else if (net->flags & IFF_ALLMULTI
|| net->mc_count > AX_MAX_MCAST) {
rx_ctl |= 0x02;
} else if (net->mc_count == 0) {
/* just broadcast and directed */
} else {
struct dev_mc_list *mc_list = net->mc_list;
u8 *multi_filter;
u32 crc_bits;
int i;
multi_filter = kmalloc(8, GFP_ATOMIC);
if (multi_filter == NULL) {
/* Oops, couldn't allocate a DMA buffer for setting the multicast
filter. Try all multi mode, although the ax_write_cmd_async
will almost certainly fail, too... (but it will printk). */
rx_ctl |= 0x02;
} else {
memset(multi_filter, 0, 8);
/* Build the multicast hash filter. */
for (i = 0; i < net->mc_count; i++) {
crc_bits =
ether_crc(ETH_ALEN,
mc_list->dmi_addr) >> 26;
multi_filter[crc_bits >> 3] |=
1 << (crc_bits & 7);
mc_list = mc_list->next;
}
ax_write_cmd_async(ax_info,
AX_CMD_WRITE_MULTI_FILTER, 0, 0,
8, multi_filter);
rx_ctl |= 0x10;
}
}
ax_write_cmd_async(ax_info, AX_CMD_WRITE_RX_CTL, rx_ctl, 0, 0,
NULL);
}
static int ax8817x_ethtool_ioctl(struct net_device *net, void *uaddr)
{
struct ax8817x_info *ax_info;
int cmd;
ax_info = net->priv;
if (get_user(cmd, (int *) uaddr))
return -EFAULT;
switch (cmd) {
case ETHTOOL_GDRVINFO:{
struct ethtool_drvinfo info = { ETHTOOL_GDRVINFO };
strncpy(info.driver, driver_name,
ETHTOOL_BUSINFO_LEN);
strncpy(info.version, DRIVER_VERSION,
ETHTOOL_BUSINFO_LEN);
usb_make_path(ax_info->usb, info.bus_info,
sizeof info.bus_info);
if (copy_to_user(uaddr, &info, sizeof(info)))
return -EFAULT;
return 0;
}
case ETHTOOL_GSET:{
struct ethtool_cmd ecmd;
if (copy_from_user(&ecmd, uaddr, sizeof(ecmd)))
return -EFAULT;
ecmd.supported = (SUPPORTED_10baseT_Half |
SUPPORTED_10baseT_Full |
SUPPORTED_100baseT_Half |
SUPPORTED_100baseT_Full |
SUPPORTED_Autoneg |
SUPPORTED_TP | SUPPORTED_MII);
ecmd.port = PORT_TP;
ecmd.transceiver = XCVR_INTERNAL;
ecmd.phy_address = 0; /* FIXME */
if (copy_to_user(uaddr, &ecmd, sizeof(ecmd)))
return -EFAULT;
return 0;
}
case ETHTOOL_SSET:
return -ENOTSUPP;
case ETHTOOL_GLINK:{
struct ethtool_value edata = { ETHTOOL_GLINK };
edata.data = netif_carrier_ok(net);
if (copy_to_user(uaddr, &edata, sizeof(edata)))
return -EFAULT;
return 0;
}
default:
return -EOPNOTSUPP;
}
}
static int ax8817x_mii_ioctl(struct net_device *net, struct ifreq *ifr,
int cmd)
{
struct ax8817x_info *ax_info;
struct mii_ioctl_data *data_ptr =
(struct mii_ioctl_data *) &(ifr->ifr_data);
ax_info = net->priv;
switch (cmd) {
case SIOCGMIIPHY:
data_ptr->phy_id = ax_info->phy_id;
break;
case SIOCGMIIREG:
if (!capable(CAP_NET_ADMIN))
return -EPERM;
ax_read_cmd(ax_info, AX_CMD_READ_MII_REG, 0,
data_ptr->reg_num & 0x1f, 2,
&(data_ptr->val_out));
break;
default:
return -EOPNOTSUPP;
}
return 0;
}
static int ax8817x_ioctl(struct net_device *net, struct ifreq *ifr,
int cmd)
{
struct ax8817x_info *ax_info;
int res;
ax_info = net->priv;
res = 0;
switch (cmd) {
case SIOCETHTOOL:
res = ax8817x_ethtool_ioctl(net, ifr->ifr_data);
break;
case SIOCGMIIPHY: /* Get address of PHY in use */
case SIOCGMIIREG: /* Read from MII PHY register */
case SIOCSMIIREG: /* Write to MII PHY register */
return ax8817x_mii_ioctl(net, ifr, cmd);
default:
res = -EOPNOTSUPP;
}
return res;
}
static int ax8817x_net_init(struct net_device *net)
{
struct ax8817x_info *ax_info = (struct ax8817x_info *) net->priv;
u8 buf[6];
u16 *buf16 = (u16 *) buf;
int ret;
spin_lock_init(&ax_info->tx_lock);
ret = ax_write_cmd(ax_info, AX_CMD_WRITE_RX_CTL, 0x80, 0, 0, buf);
if (ret < 0) {
return ret;
}
memset(buf, 0, 6);
/* Get the MAC address */
ret = ax_read_cmd(ax_info, AX_CMD_READ_NODE_ID, 0, 0, 6, buf);
if (ret < 0) {
return ret;
}
memcpy(net->dev_addr, buf, 6);
/* Get the PHY id */
ret = ax_read_cmd(ax_info, AX_CMD_READ_PHY_ID, 0, 0, 2, buf);
if (ret < 0) {
return ret;
} else if (ret < 2) {
/* this should always return 2 bytes */
return -EIO;
}
/* Reset the PHY, and drop it into auto-negotiation mode */
ax_info->phy_id = buf[1];
ax_info->phy_state = AX_PHY_STATE_INITIALIZING;
ret = ax_write_cmd(ax_info, AX_CMD_SET_SW_MII, 0, 0, 0, &buf);
if (ret < 0) {
return ret;
}
*buf16 = cpu_to_le16(BMCR_RESET);
ret = ax_write_cmd(ax_info, AX_CMD_WRITE_MII_REG,
ax_info->phy_id, MII_BMCR, 2, buf16);
if (ret < 0) {
return ret;
}
/* Advertise that we can do full-duplex pause */
*buf16 = cpu_to_le16(ADVERTISE_ALL | ADVERTISE_CSMA | 0x0400);
ret = ax_write_cmd(ax_info, AX_CMD_WRITE_MII_REG,
ax_info->phy_id, MII_ADVERTISE, 2, buf16);
if (ret < 0) {
return ret;
}
*buf16 = cpu_to_le16(BMCR_ANENABLE | BMCR_ANRESTART);
ret = ax_write_cmd(ax_info, AX_CMD_WRITE_MII_REG,
ax_info->phy_id, MII_BMCR, 2, buf16);
if (ret < 0) {
return ret;
}
ret = ax_write_cmd(ax_info, AX_CMD_SET_HW_MII, 0, 0, 0, &buf);
if (ret < 0) {
return ret;
}
net->open = ax8817x_open;
net->stop = ax8817x_stop;
net->hard_start_xmit = ax8817x_start_xmit;
net->tx_timeout = ax8817x_tx_timeout;
net->watchdog_timeo = AX_TIMEOUT_TX;
net->get_stats = ax8817x_stats;
net->do_ioctl = ax8817x_ioctl;
net->set_multicast_list = ax8817x_set_multicast;
return 0;
}
static void *ax8817x_bind(struct usb_device *usb, unsigned intf,
const struct usb_device_id *id)
{
struct ax8817x_info *ax_info;
struct net_device *net;
int i, ret;
unsigned long gpio_bits = id->driver_info;
u8 buf[2];
/* Allocate the URB lists along with the device info struct */
ax_info = kmalloc(sizeof(struct ax8817x_info) +
n_rx_urbs * sizeof(struct urb *), GFP_KERNEL);
if (ax_info == NULL) {
err("%s: Failed ax alloc\n", __FUNCTION__);
goto exit_err;
}
memset(ax_info, 0, sizeof(struct ax8817x_info) +
n_rx_urbs * sizeof(struct urb *));
ax_info->drv_state = AX_DRV_STATE_INITIALIZING;
ax_info->rx_urbs = (struct urb **) (ax_info + 1);
ax_info->usb = usb;
/* Set up the control URB queue */
INIT_LIST_HEAD(&ax_info->ctl_queue);
spin_lock_init(&ax_info->ctl_lock);
ax_info->ctl_urb = usb_alloc_urb(0);
if (ax_info->ctl_urb == NULL) {
goto exit_err_free_ax;
}
/* Toggle the GPIOs in a manufacturer/model specific way */
for (i = 2; i >= 0; i--) {
ret = ax_write_cmd(ax_info, AX_CMD_WRITE_GPIOS,
(gpio_bits >> (i * 8)) & 0xff, 0, 0,
buf);
if (ret < 0) {
goto exit_err_free_ax;
}
wait_ms(5);
}
/* Set up the net device */
net = alloc_etherdev(0);
if (net == NULL) {
err("%s: Failed net alloc\n", __FUNCTION__);
goto exit_err_free_ax;
}
ax_info->net = net;
SET_MODULE_OWNER(net);
net->init = ax8817x_net_init;
net->priv = ax_info;
ret = register_netdev(net);
if (ret < 0) {
err("%s: Failed net init (%d)\n", __FUNCTION__, ret);
goto exit_err_free_net;
}
/* Set up the interrupt URB, and start PHY state monitoring */
ax_info->int_urb = usb_alloc_urb(0);
if (ax_info->int_urb == NULL) {
goto exit_err_unregister_net;
}
ax_info->int_buf = kmalloc(8, GFP_KERNEL);
if (ax_info->int_buf == NULL) {
goto exit_err_free_int_urb;
}
ax_info->phy_req.data = kmalloc(2, GFP_KERNEL);
if (ax_info->phy_req.data == NULL) {
goto exit_err_free_int_buf;
}
usb_fill_int_urb(ax_info->int_urb, usb, usb_rcvintpipe(usb, 1),
ax_info->int_buf, 8, ax_int_callback, ax_info,
100);
ret = usb_submit_urb(ax_info->int_urb);
if (ret < 0) {
err("%s: Failed int URB submit (%d)\n", __FUNCTION__, ret);
goto exit_err_free_phy_buf;
}
ax_info->drv_state = AX_DRV_STATE_RUNNING;
return ax_info;
exit_err_free_phy_buf:
kfree(ax_info->phy_req.data);
exit_err_free_int_buf:
kfree(ax_info->int_buf);
exit_err_free_int_urb:
usb_free_urb(ax_info->int_urb);
exit_err_unregister_net:
ax_info->drv_state = AX_DRV_STATE_EXITING;
unregister_netdev(net);
exit_err_free_net:
kfree(net);
exit_err_free_ax:
if (ax_info->ctl_urb != NULL) {
/* no need to unlink, since there should not be any ctl URBs
pending at this point */
usb_free_urb(ax_info->ctl_urb);
}
kfree(ax_info);
exit_err:
err("%s: Failed to initialize\n", __FUNCTION__);
return NULL;
}
static void ax8817x_disconnect(struct usb_device *usb, void *p)
{
struct ax8817x_info *ax_info = (struct ax8817x_info *) p;
ax_info->drv_state = AX_DRV_STATE_EXITING;
if (ax_info->int_urb != NULL) {
usb_unlink_urb(ax_info->int_urb);
usb_free_urb(ax_info->int_urb);
kfree(ax_info->int_buf);
}
unregister_netdev(ax_info->net);
/* XXX: hmmm... need to go through and clear out the ctl queue, too... */
if (ax_info->ctl_urb != NULL) {
usb_unlink_urb(ax_info->ctl_urb);
usb_free_urb(ax_info->ctl_urb);
}
kfree(ax_info);
}
static struct usb_driver ax8817x_driver = {
.owner = THIS_MODULE,
.name = driver_name,
.probe = ax8817x_bind,
.disconnect = ax8817x_disconnect,
.id_table = ax8817x_id_table,
};
static int __init ax8817x_init(void)
{
int ret;
if (n_rx_urbs < 1)
n_rx_urbs = AX_RX_URBS_DEFAULT;
ret = usb_register(&ax8817x_driver);
if (ret < 0) {
err("%s: Failed to register\n", __FUNCTION__);
} else {
info(DRIVER_DESC " " DRIVER_VERSION);
}
return ret;
}
static void __exit ax8817x_exit(void)
{
usb_deregister(&ax8817x_driver);
}
module_init(ax8817x_init);
module_exit(ax8817x_exit);
EXPORT_NO_SYMBOLS;
|