developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame^] | 1 | #include <errno.h> |
| 2 | #include <stdint.h> |
| 3 | #include <stdio.h> |
| 4 | #include <stdlib.h> |
| 5 | #include <string.h> |
| 6 | #include <unistd.h> |
| 7 | |
| 8 | #include <sys/ioctl.h> |
| 9 | #include <net/if.h> |
| 10 | #include <sys/socket.h> |
| 11 | #include <netinet/in.h> |
| 12 | #include <arpa/inet.h> |
| 13 | |
| 14 | #include <linux/ethtool.h> |
| 15 | #include <linux/mdio.h> |
| 16 | #include <linux/sockios.h> |
| 17 | |
| 18 | void show_usage(void) |
| 19 | { |
| 20 | printf("mii_mgr -g -i [ifname] -p [phy number] -r [register number]\n"); |
| 21 | printf(" Get: mii_mgr -g -p 3 -r 4\n\n"); |
| 22 | printf("mii_mgr -s -p [phy number] -r [register number] -v [0xvalue]\n"); |
| 23 | printf(" Set: mii_mgr -s -p 4 -r 1 -v 0xff11\n"); |
| 24 | printf("#NOTE: Without -i , eth0 is default ifname!\n"); |
| 25 | printf("----------------------------------------------------------------------------------------\n"); |
| 26 | printf("Get: mii_mgr_cl45 -g -p [port number] -d [dev number] -r [register number]\n"); |
| 27 | printf("Example: mii_mgr_cl45 -g -p 3 -d 0x5 -r 0x4\n\n"); |
| 28 | printf("Set: mii_mgr_cl45 -s -p [port number] -d [dev number] -r [register number] -v [value]\n"); |
| 29 | printf("Example: mii_mgr_cl45 -s -p 4 -d 0x6 -r 0x1 -v 0xff11\n\n"); |
| 30 | } |
| 31 | |
| 32 | static int __phy_op(char *ifname,uint16_t phy_id,uint16_t reg_num, uint16_t *val, int cmd) |
| 33 | { |
| 34 | static int sd = -1; |
| 35 | |
| 36 | struct ifreq ifr; |
| 37 | struct mii_ioctl_data* mii = (struct mii_ioctl_data *)(&ifr.ifr_data); |
| 38 | int err; |
| 39 | |
| 40 | if (sd < 0) |
| 41 | sd = socket(AF_INET, SOCK_DGRAM, 0); |
| 42 | |
| 43 | if (sd < 0) |
| 44 | return sd; |
| 45 | |
| 46 | strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name)); |
| 47 | |
| 48 | mii->phy_id = phy_id; |
| 49 | mii->reg_num = reg_num; |
| 50 | mii->val_in = *val; |
| 51 | mii->val_out = 0; |
| 52 | |
| 53 | err = ioctl(sd, cmd, &ifr); |
| 54 | if (err) |
| 55 | return -errno; |
| 56 | |
| 57 | *val = mii->val_out; |
| 58 | return 0; |
| 59 | } |
| 60 | |
| 61 | int main(int argc, char *argv[]) |
| 62 | { |
| 63 | int opt; |
| 64 | char options[] = "gsi:p:d:r:v:?t"; |
| 65 | int is_write = 0,is_cl45 = 0; |
| 66 | unsigned int port=0, dev=0,reg_num=0,val=0; |
| 67 | char ifname[IFNAMSIZ]="eth0"; |
| 68 | uint16_t phy_id=0; |
| 69 | |
| 70 | |
| 71 | if (argc < 6) { |
| 72 | show_usage(); |
| 73 | return 0; |
| 74 | } |
| 75 | |
| 76 | while ((opt = getopt(argc, argv, options)) != -1) { |
| 77 | switch (opt) { |
| 78 | case 'g': |
| 79 | is_write=0; |
| 80 | break; |
| 81 | case 's': |
| 82 | is_write=1; |
| 83 | break; |
| 84 | case 'i': |
| 85 | strncpy(ifname,optarg, 5); |
| 86 | break; |
| 87 | case 'p': |
| 88 | port = strtoul(optarg, NULL, 16); |
| 89 | break; |
| 90 | case 'd': |
| 91 | dev = strtoul(optarg, NULL, 16); |
| 92 | is_cl45 = 1; |
| 93 | break; |
| 94 | case 'r': |
| 95 | reg_num = strtoul(optarg, NULL, 16); |
| 96 | break; |
| 97 | |
| 98 | case 'v': |
| 99 | val = strtoul(optarg, NULL, 16); |
| 100 | break; |
| 101 | case '?': |
| 102 | show_usage(); |
| 103 | break; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | if(is_cl45) |
| 108 | phy_id = mdio_phy_id_c45(port, dev); |
| 109 | else |
| 110 | phy_id = port; |
| 111 | |
| 112 | if(is_write) { |
| 113 | __phy_op(ifname,phy_id,reg_num,(uint16_t *)&val,SIOCSMIIREG); |
| 114 | |
| 115 | if(is_cl45) |
| 116 | printf("Set: port%x dev%Xh_reg%Xh = 0x%04X\n",port, dev, reg_num, val); |
| 117 | else |
| 118 | printf("Set: phy[%x].reg[%x] = %04x\n",port, reg_num, val); |
| 119 | } |
| 120 | else { |
| 121 | __phy_op(ifname,phy_id,reg_num,(uint16_t *)&val,SIOCGMIIREG); |
| 122 | |
| 123 | if(is_cl45) |
| 124 | printf("Get: port%x dev%Xh_reg%Xh = 0x%04X\n",port, dev, reg_num, val); |
| 125 | else |
| 126 | printf("Get: phy[%x].reg[%x] = %04x\n",port, reg_num, val); |
| 127 | |
| 128 | } |
| 129 | |
| 130 | return 0; |
| 131 | } |