| #include <stdlib.h> |
| #include <fcntl.h> |
| #include <unistd.h> |
| #include <string.h> |
| #include <stdint.h> |
| #include <stdio.h> |
| |
| struct mdio_reg_op |
| { |
| uint32_t reg; |
| uint32_t val; |
| }; |
| |
| #define DEVICE_NAME "/dev/mdio_misc" |
| |
| int main(int argc, char*argv[]) |
| { |
| int fd; |
| ssize_t ret; |
| struct mdio_reg_op reg_op; |
| |
| if (argc != 2 && argc != 3) |
| { |
| fprintf(stderr, "Usage: %s <reg> [val]\n", argv[0]); |
| fprintf(stderr, " Read: %s 0x0 \n", argv[0]); |
| fprintf(stderr, " Write: %s 0x0 0x2100\n", argv[0]); |
| return -1; |
| } |
| |
| fd = open(DEVICE_NAME, O_RDWR); |
| if (fd < 0) |
| { |
| perror("Failed to open device"); |
| return -1; |
| } |
| |
| reg_op.reg = strtoul(argv[1], NULL, 16); |
| |
| if (argc == 2) |
| { |
| ret = read(fd, ®_op, sizeof(struct mdio_reg_op)); |
| if (ret != sizeof(struct mdio_reg_op)) |
| { |
| perror("Read operation failed"); |
| close(fd); |
| return -1; |
| } |
| printf("Read success: reg=0x%08X, val=0x%08X\n", reg_op.reg, reg_op.val); |
| } |
| else |
| { |
| reg_op.val = strtoul(argv[2], NULL, 16); |
| ret = write(fd, ®_op, sizeof(reg_op)); |
| if (ret != sizeof(reg_op)) |
| { |
| perror("Write operation failed"); |
| close(fd); |
| return -1; |
| } |
| printf("Write success: reg=0x%08X, val=0x%08X\n", reg_op.reg, reg_op.val); |
| } |
| |
| close(fd); |
| return 0; |
| } |