| 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 | #ifndef CONFIG_SUPPORT_OPENWRT | 
 | 20 | #define ETH_DEVNAME "eth2" | 
 | 21 | #else | 
 | 22 | #define ETH_DEVNAME "eth0" | 
 | 23 | #endif | 
 | 24 | #include "ra_ioctl.h" | 
 | 25 |  | 
 | 26 | void show_usage(void) | 
 | 27 | { | 
 | 28 | #ifndef CONFIG_RT2860V2_AP_MEMORY_OPTIMIZATION | 
 | 29 | 	printf("Get: mii_mgr_cl45 -g -p [port number] -d [dev number] -r [register number]\n"); | 
 | 30 | 	printf("Example: mii_mgr_cl45 -g -p 3 -d 0x5 -r 0x4\n\n"); | 
 | 31 | 	printf("Set: mii_mgr_cl45 -s -p [port number] -d [dev number] -r [register number] -v [value]\n"); | 
 | 32 | 	printf("Example: mii_mgr_cl45 -s -p 4 -d 0x6 -r 0x1 -v 0xff11\n\n"); | 
 | 33 | #endif | 
 | 34 | } | 
 | 35 |  | 
 | 36 | int main(int argc, char *argv[]) | 
 | 37 | { | 
 | 38 | 	int sk, opt, ret = 0; | 
 | 39 | 	char options[] = "gsp:d:r:v:?t"; | 
 | 40 | 	int method = 0; | 
 | 41 | 	struct ifreq ifr; | 
 | 42 | 	struct ra_mii_ioctl_data mii; | 
 | 43 |  | 
 | 44 | 	if (argc < 8) { | 
 | 45 | 		show_usage(); | 
 | 46 | 		return 0; | 
 | 47 | 	} | 
 | 48 |  | 
 | 49 | 	sk = socket(AF_INET, SOCK_DGRAM, 0); | 
 | 50 | 	if (sk < 0) { | 
 | 51 | 		printf("Open socket failed\n"); | 
 | 52 | 		return -1; | 
 | 53 | 	} | 
 | 54 |  | 
 | 55 | 	strncpy(ifr.ifr_name, ETH_DEVNAME, 5); | 
 | 56 | 	ifr.ifr_data = &mii; | 
 | 57 |  | 
 | 58 | 	while ((opt = getopt(argc, argv, options)) != -1) { | 
 | 59 | 		switch (opt) { | 
 | 60 | 			case 'g': | 
 | 61 | 				method = RAETH_MII_READ_CL45; | 
 | 62 | 				break; | 
 | 63 | 			case 's': | 
 | 64 | 				method = RAETH_MII_WRITE_CL45; | 
 | 65 | 				break; | 
 | 66 | 			case 'p': | 
 | 67 | 				mii.port_num = strtoul(optarg, NULL, 16); | 
 | 68 | 				break; | 
 | 69 | 			case 'd': | 
 | 70 | 				mii.dev_addr = strtoul(optarg, NULL, 16); | 
 | 71 | 				break; | 
 | 72 | 			case 'r': | 
 | 73 | 				mii.reg_addr = strtol(optarg, NULL, 16); | 
 | 74 | 				break; | 
 | 75 | 			case 'v': | 
 | 76 | 				mii.val_in = strtol(optarg, NULL, 16); | 
 | 77 | 				break; | 
 | 78 | 			case '?': | 
 | 79 | 				show_usage(); | 
 | 80 | 				break; | 
 | 81 | 		} | 
 | 82 | 	} | 
 | 83 |  | 
 | 84 | 	if ((method == RAETH_MII_READ_CL45) || (method == RAETH_MII_WRITE_CL45)){ | 
 | 85 | 		ret = ioctl(sk, method, &ifr); | 
 | 86 | 		if (ret < 0) { | 
 | 87 | 			printf("mii_mgr_cl45: ioctl error\n"); | 
 | 88 | 		} | 
 | 89 | 		else | 
 | 90 | 			switch (method) { | 
 | 91 | 				case RAETH_MII_READ_CL45: | 
 | 92 | 					printf("Get: port%d dev%Xh_reg%Xh = 0x%04X\n", | 
 | 93 | 							mii.port_num, mii.dev_addr, mii.reg_addr, mii.val_out); | 
 | 94 | 					break; | 
 | 95 | 				case RAETH_MII_WRITE_CL45: | 
 | 96 | 					printf("Set: port%d dev%Xh_reg%Xh = 0x%04X\n", | 
 | 97 | 							mii.port_num, mii.dev_addr, mii.reg_addr, mii.val_in); | 
 | 98 | 					break; | 
 | 99 | 			} | 
 | 100 | 	} | 
 | 101 | 	close(sk); | 
 | 102 | 	return ret; | 
 | 103 | } |