| rjw | 2e8229f | 2022-02-15 21:08:12 +0800 | [diff] [blame] | 1 | #include <getopt.h> |
| 2 | #include <stdio.h> |
| 3 | #include <stdlib.h> |
| 4 | #include <string.h> |
| 5 | #include <sys/ioctl.h> |
| 6 | #include <sys/socket.h> |
| 7 | #include <sys/types.h> |
| 8 | #include <linux/if.h> |
| 9 | #include <linux/mii.h> |
| 10 | #include <linux/types.h> |
| 11 | #include <unistd.h> |
| 12 | |
| 13 | #ifndef CONFIG_SUPPORT_OPENWRT |
| 14 | #ifndef CONFIG_GLIBC_2_20 |
| 15 | #include <linux/autoconf.h> |
| 16 | #endif |
| 17 | #endif |
| 18 | |
| 19 | #include "ra_ioctl.h" |
| 20 | |
| 21 | #ifndef CONFIG_SUPPORT_OPENWRT |
| 22 | #define ETH_DEVNAME "eth2" |
| 23 | #else |
| 24 | #define ETH_DEVNAME "eth0" |
| 25 | #endif |
| 26 | |
| 27 | void show_usage(void) |
| 28 | { |
| 29 | #ifndef CONFIG_RT2860V2_AP_MEMORY_OPTIMIZATION |
| 30 | printf("mii_mgr -g -p [phy number] -r [register number]\n"); |
| 31 | printf(" Get: mii_mgr -g -p 3 -r 4\n\n"); |
| 32 | printf("mii_mgr -s -p [phy number] -r [register number] -v [0xvalue]\n"); |
| 33 | printf(" Set: mii_mgr -s -p 4 -r 1 -v 0xff11\n\n"); |
| 34 | #endif |
| 35 | } |
| 36 | |
| 37 | int main(int argc, char *argv[]) |
| 38 | { |
| 39 | int sk, opt, ret = 0; |
| 40 | char options[] = "gsp:r:v:L:G:?t"; |
| 41 | int method = 0; |
| 42 | struct ifreq ifr; |
| 43 | struct ra_mii_ioctl_data mii; |
| 44 | |
| 45 | #if defined (CONFIG_RALINK_MT7628) |
| 46 | struct ifreq ifr2; |
| 47 | struct ra_mii_ioctl_data mii2; |
| 48 | int page_select = 0; |
| 49 | int method2 = 0; |
| 50 | #endif |
| 51 | if (argc < 6) { |
| 52 | show_usage(); |
| 53 | return 0; |
| 54 | } |
| 55 | |
| 56 | sk = socket(AF_INET, SOCK_DGRAM, 0); |
| 57 | if (sk < 0) { |
| 58 | printf("Open socket failed\n"); |
| 59 | return -1; |
| 60 | } |
| 61 | |
| 62 | strncpy(ifr.ifr_name,ETH_DEVNAME, 5); |
| 63 | ifr.ifr_data = &mii; |
| 64 | #if defined (CONFIG_RALINK_MT7628) |
| 65 | strncpy(ifr2.ifr_name, ETH_DEVNAME , 5); |
| 66 | ifr2.ifr_data = &mii2; |
| 67 | #endif |
| 68 | |
| 69 | while ((opt = getopt(argc, argv, options)) != -1) { |
| 70 | switch (opt) { |
| 71 | case 'g': |
| 72 | method = RAETH_MII_READ; |
| 73 | break; |
| 74 | case 's': |
| 75 | method = RAETH_MII_WRITE; |
| 76 | break; |
| 77 | case 'p': |
| 78 | mii.phy_id = strtoul(optarg, NULL, 10); |
| 79 | #if defined (CONFIG_RALINK_MT7628) |
| 80 | mii2.phy_id = strtoul(optarg, NULL, 10); |
| 81 | #endif |
| 82 | break; |
| 83 | case 'r': |
| 84 | #if defined (CONFIG_RALINK_MT7621) || defined(CONFIG_MACH_MT7623) |
| 85 | if(mii.phy_id == 31) { |
| 86 | mii.reg_num = strtol(optarg, NULL, 16); |
| 87 | } else { |
| 88 | mii.reg_num = strtol(optarg, NULL, 10); |
| 89 | } |
| 90 | #else |
| 91 | mii.reg_num = strtol(optarg, NULL, 10); |
| 92 | #endif |
| 93 | break; |
| 94 | case 'L': |
| 95 | #if defined (CONFIG_RALINK_MT7628) |
| 96 | mii2.reg_num = 31; |
| 97 | mii2.val_in = (strtol(optarg, NULL, 16) << 12); |
| 98 | mii2.val_in |= 0x8000; |
| 99 | page_select = 1; |
| 100 | #endif |
| 101 | break; |
| 102 | case 'G': |
| 103 | #if defined (CONFIG_RALINK_MT7628) |
| 104 | mii2.reg_num = 31; |
| 105 | mii2.val_in = (strtol(optarg, NULL, 16) << 12); |
| 106 | page_select = 1; |
| 107 | #endif |
| 108 | break; |
| 109 | |
| 110 | case 'v': |
| 111 | mii.val_in = strtol(optarg, NULL, 16); |
| 112 | break; |
| 113 | case '?': |
| 114 | show_usage(); |
| 115 | break; |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | #if defined (CONFIG_RALINK_MT7628) |
| 120 | if(page_select){ |
| 121 | method2 = RAETH_MII_WRITE; |
| 122 | ret = ioctl(sk, method2, &ifr2); |
| 123 | if (ret < 0) { |
| 124 | printf("mii_mgr: ioctl error\n"); |
| 125 | } |
| 126 | else{ |
| 127 | printf("Set: phy[%d].reg[%d] = %04x\n", |
| 128 | mii2.phy_id, mii2.reg_num, mii2.val_in); |
| 129 | |
| 130 | } |
| 131 | } |
| 132 | #endif |
| 133 | if ((method == RAETH_MII_READ) || (method == RAETH_MII_WRITE)){ |
| 134 | ret = ioctl(sk, method, &ifr); |
| 135 | if (ret < 0) { |
| 136 | printf("mii_mgr: ioctl error\n"); |
| 137 | } |
| 138 | else |
| 139 | switch (method) { |
| 140 | case RAETH_MII_READ: |
| 141 | printf("Get: phy[%d].reg[%d] = %04x\n", |
| 142 | mii.phy_id, mii.reg_num, mii.val_out); |
| 143 | break; |
| 144 | case RAETH_MII_WRITE: |
| 145 | printf("Set: phy[%d].reg[%d] = %04x\n", |
| 146 | mii.phy_id, mii.reg_num, mii.val_in); |
| 147 | break; |
| 148 | } |
| 149 | } |
| 150 | close(sk); |
| 151 | return ret; |
| 152 | } |