conformance_testing: mii-reg.c

File mii-reg.c, 1.5 KB (added by trac, 7 years ago)
Line 
1/*
2 * mii-reg.c - Tim Harvey <tharvey@gateworks.com>
3 *
4 * Simple example of how to read/write MII registers using
5 * SIOCGMIIREG/SIOCSMIIREG ioctl
6 *
7 * Note that your network device driver must support mdio_read/mdio_write
8 *
9 * Usage:
10 * mii-reg <iface> <reg> [<value>]
11 *
12 */
13
14#include <errno.h>
15#include <stdio.h>
16#include <stdlib.h>
17#include <string.h>
18#include <unistd.h>
19
20#include <sys/socket.h>
21#include <sys/ioctl.h>
22#include <net/if.h>
23#include <linux/mii.h>
24#include <linux/sockios.h>
25
26int main(int argc, char **argv)
27{
28 char *iface;
29 int sock;
30 struct ifreq ifr;
31 struct mii_ioctl_data *mii = (struct mii_ioctl_data*)(&ifr.ifr_data);
32 int reg;
33
34 if (argc < 3) {
35 fprintf(stderr, "Usage: %s <iface> <reg> [<value>]\n",
36 argv[0]);
37 exit(1);
38 }
39 iface = argv[1];
40 reg = strtol(argv[2], NULL, 0);
41
42 /* open socket */
43 sock = socket(AF_INET, SOCK_DGRAM, 0);
44 if (sock < 0) {
45 perror("socket");
46 exit(1);
47 }
48
49 /* get the details of the iface */
50 memset(&ifr, 0, sizeof(ifr));
51 strncpy(ifr.ifr_name, iface, IFNAMSIZ);
52 if (ioctl(sock, SIOCGMIIPHY, &ifr) < 0) {
53 perror("SIOCGMIIPHY");
54 exit(1);
55 }
56
57 mii->reg_num = reg;
58 if (argc > 3) {
59 mii->val_in = strtol(argv[3], NULL, 0);
60 if (ioctl(sock, SIOCSMIIREG, &ifr) < 0) {
61 perror("SIOCSMIIREG");
62 exit(1);
63 }
64 }
65 mii->val_in = 0;
66 mii->val_out = 0;
67 if (ioctl(sock, SIOCGMIIREG, &ifr) < 0) {
68 perror("SIOCGMIIREG");
69 exit(1);
70 }
71 printf("%s phyid=0x%02x reg=0x%02x val=0x%04x\n",
72 iface, mii->phy_id, mii->reg_num, mii->val_out);
73
74 /* read */
75 close(sock);
76
77 return 0;
78}