1*5084cd4fSchristos /* $NetBSD: cnwctl.c,v 1.8 2013/10/19 17:05:58 christos Exp $ */
20641b081Sitojun
3876c4c29Sitojun /*
4876c4c29Sitojun * Copyright (c) 1997 Berkeley Software Design, Inc.
5876c4c29Sitojun * All rights reserved.
6876c4c29Sitojun *
7876c4c29Sitojun * Redistribution and use in source and binary forms, with or without
8876c4c29Sitojun * modification, are permitted provided that this notice is retained,
9876c4c29Sitojun * the conditions in the following notices are met, and terms applying
10876c4c29Sitojun * to contributors in the following notices also apply to Berkeley
11876c4c29Sitojun * Software Design, Inc.
12876c4c29Sitojun *
13876c4c29Sitojun * 1. Redistributions of source code must retain the above copyright
14876c4c29Sitojun * notice, this list of conditions and the following disclaimer.
15876c4c29Sitojun * 2. Redistributions in binary form must reproduce the above copyright
16876c4c29Sitojun * notice, this list of conditions and the following disclaimer in the
17876c4c29Sitojun * documentation and/or other materials provided with the distribution.
18876c4c29Sitojun * 3. All advertising materials mentioning features or use of this software
19876c4c29Sitojun * must display the following acknowledgement:
20876c4c29Sitojun * This product includes software developed by
21876c4c29Sitojun * Berkeley Software Design, Inc.
22876c4c29Sitojun * 4. Neither the name of the Berkeley Software Design, Inc. nor the names
23876c4c29Sitojun * of its contributors may be used to endorse or promote products derived
24876c4c29Sitojun * from this software without specific prior written permission.
25876c4c29Sitojun *
26876c4c29Sitojun * THIS SOFTWARE IS PROVIDED BY BERKELEY SOFTWARE DESIGN, INC. ``AS IS'' AND
27876c4c29Sitojun * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28876c4c29Sitojun * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29876c4c29Sitojun * ARE DISCLAIMED. IN NO EVENT SHALL BERKELEY SOFTWARE DESIGN, INC. BE LIABLE
30876c4c29Sitojun * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31876c4c29Sitojun * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32876c4c29Sitojun * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33876c4c29Sitojun * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34876c4c29Sitojun * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35876c4c29Sitojun * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36876c4c29Sitojun * SUCH DAMAGE.
37876c4c29Sitojun *
38876c4c29Sitojun * PAO2 Id: cnwctl.c,v 1.1.1.1 1997/12/11 14:46:06 itojun Exp
39876c4c29Sitojun */
40876c4c29Sitojun
41876c4c29Sitojun #include <sys/types.h>
42876c4c29Sitojun #include <sys/param.h>
43876c4c29Sitojun #include <sys/cdefs.h>
44876c4c29Sitojun #include <sys/errno.h>
45876c4c29Sitojun #include <sys/socket.h>
46876c4c29Sitojun #include <sys/ioctl.h>
47876c4c29Sitojun
48876c4c29Sitojun #include <net/if.h>
49876c4c29Sitojun
500641b081Sitojun #include <dev/pcmcia/if_cnwioctl.h>
51876c4c29Sitojun
52876c4c29Sitojun #include <err.h>
53876c4c29Sitojun #include <stdio.h>
54876c4c29Sitojun #include <stdlib.h>
55876c4c29Sitojun #include <string.h>
56876c4c29Sitojun #include <time.h>
57876c4c29Sitojun #include <unistd.h>
58876c4c29Sitojun
590641b081Sitojun int
main(int argc,char ** argv)60876c4c29Sitojun main(int argc, char **argv)
61876c4c29Sitojun {
62876c4c29Sitojun int c, domain, i, key, rate, sflag, Sflag, skt;
63b2bb6949Sxtraeme const char *interface;
64b2bb6949Sxtraeme char *e;
65876c4c29Sitojun struct ifreq ifr;
66876c4c29Sitojun struct cnwistats cnwis, onwis;
67876c4c29Sitojun struct cnwstatus cnws;
68876c4c29Sitojun struct ttysize ts;
69876c4c29Sitojun
70876c4c29Sitojun domain = -1;
71876c4c29Sitojun key = -1;
72876c4c29Sitojun Sflag = sflag = 0;
73876c4c29Sitojun rate = 0;
74876c4c29Sitojun interface = "cnw0";
75876c4c29Sitojun
76e3af9d1dSad while ((c = getopt(argc, argv, "d:i:k:sS")) != -1)
77876c4c29Sitojun switch (c) {
78876c4c29Sitojun case 'd':
79876c4c29Sitojun domain = strtol(optarg, &e, 0);
80876c4c29Sitojun if (e == optarg || *e || domain & ~ 0x1ff)
81876c4c29Sitojun errx(1, "%s: invalid domain", optarg);
82876c4c29Sitojun break;
83876c4c29Sitojun case 'i':
84876c4c29Sitojun interface = optarg;
85876c4c29Sitojun break;
86876c4c29Sitojun case 'k':
87876c4c29Sitojun key = strtol(optarg, &e, 0);
88876c4c29Sitojun if (e == optarg || *e || key & ~ 0xffff)
89876c4c29Sitojun errx(1, "%s: invalid scramble key", optarg);
90876c4c29Sitojun break;
91876c4c29Sitojun case 'S':
92876c4c29Sitojun ++Sflag;
93876c4c29Sitojun break;
94876c4c29Sitojun case 's':
95876c4c29Sitojun ++sflag;
96876c4c29Sitojun break;
97876c4c29Sitojun default: usage:
98876c4c29Sitojun fprintf(stderr, "usage: cnwctl [-i interface] [-d domain] [-k key] [-sS [rate]]\n");
99876c4c29Sitojun exit(1);
100876c4c29Sitojun }
101876c4c29Sitojun
102876c4c29Sitojun switch (argc - optind) {
103876c4c29Sitojun case 0:
104876c4c29Sitojun break;
105876c4c29Sitojun case 1:
106876c4c29Sitojun if (sflag == 0 && Sflag == 0)
107876c4c29Sitojun goto usage;
108876c4c29Sitojun if (sflag && Sflag)
109876c4c29Sitojun errx(1, "only one of -s and -S may be specified with a rate");
110876c4c29Sitojun rate = strtol(argv[optind], &e, 0);
111876c4c29Sitojun if (e == optarg || *e || rate < 1)
112876c4c29Sitojun errx(1, "%s: invalid rate", optarg);
113876c4c29Sitojun break;
114876c4c29Sitojun default:
115876c4c29Sitojun goto usage;
116876c4c29Sitojun }
117876c4c29Sitojun
118876c4c29Sitojun if ((skt = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
119876c4c29Sitojun err(1, "socket(AF_INET, SOCK_DGRAM)");
120876c4c29Sitojun
121876c4c29Sitojun if (key >= 0) {
122876c4c29Sitojun memset(&ifr, 0, sizeof(ifr));
123876c4c29Sitojun strncpy(ifr.ifr_name, interface, sizeof(ifr.ifr_name));
124876c4c29Sitojun ifr.ifr_key = key;
125876c4c29Sitojun if (ioctl(skt, SIOCSCNWKEY, (caddr_t)&ifr) < 0)
126876c4c29Sitojun err(1, "SIOCSCNWKEY");
127876c4c29Sitojun }
128876c4c29Sitojun
129876c4c29Sitojun if (domain >= 0) {
130876c4c29Sitojun memset(&ifr, 0, sizeof(ifr));
131876c4c29Sitojun strncpy(ifr.ifr_name, interface, sizeof(ifr.ifr_name));
132876c4c29Sitojun ifr.ifr_domain = domain;
133876c4c29Sitojun if (ioctl(skt, SIOCSCNWDOMAIN, (caddr_t)&ifr) < 0)
134876c4c29Sitojun err(1, "SIOCSCNWDOMAIN");
135876c4c29Sitojun }
136876c4c29Sitojun
137876c4c29Sitojun if (sflag == 0 && Sflag == 0)
138876c4c29Sitojun exit (0);
139876c4c29Sitojun
140876c4c29Sitojun if (Sflag) {
141876c4c29Sitojun memset(&cnws, 0, sizeof(cnws));
142876c4c29Sitojun strncpy(cnws.ifr.ifr_name, interface,
143876c4c29Sitojun sizeof(cnws.ifr.ifr_name));
144876c4c29Sitojun if (ioctl(skt, SIOCGCNWSTATUS, (caddr_t)&cnws) < 0)
145876c4c29Sitojun err(1, "SIOCGCNWSTATUS");
146876c4c29Sitojun }
147876c4c29Sitojun
148876c4c29Sitojun if (sflag) {
149876c4c29Sitojun memset(&cnwis, 0, sizeof(cnwis));
150876c4c29Sitojun strncpy(cnwis.ifr.ifr_name, interface,
151876c4c29Sitojun sizeof(cnwis.ifr.ifr_name));
152876c4c29Sitojun if (ioctl(skt, SIOCGCNWSTATS, (caddr_t)&cnwis) < 0)
153876c4c29Sitojun err(1, "SIOCGCNWSTATS");
154876c4c29Sitojun cnwis.stats.nws_txretries[0] = 0;
155876c4c29Sitojun
156876c4c29Sitojun for (i = 1; i < 16; ++i)
157876c4c29Sitojun cnwis.stats.nws_txretries[0] +=
158876c4c29Sitojun cnwis.stats.nws_txretries[i] * i;
159876c4c29Sitojun }
160876c4c29Sitojun
161876c4c29Sitojun if (rate == 0 && sflag) {
162876c4c29Sitojun memset(&ifr, 0, sizeof(ifr));
163876c4c29Sitojun strncpy(ifr.ifr_name, interface, sizeof(ifr.ifr_name));
164876c4c29Sitojun if (ioctl(skt, SIOCGCNWDOMAIN, (caddr_t)&ifr) < 0)
165876c4c29Sitojun err(1, "SIOCGCNWDOMAIN");
166876c4c29Sitojun printf("%s:\n 0x%03x domain\n", interface, ifr.ifr_domain);
167772dc3bcSsommerfeld printf("%10llu rx\n", (unsigned long long)cnwis.stats.nws_rx);
16871b188dbSsommerfeld printf("%10llu rxoverflow\n",
169772dc3bcSsommerfeld (unsigned long long)cnwis.stats.nws_rxoverflow);
17071b188dbSsommerfeld printf("%10llu rxoverrun\n",
171772dc3bcSsommerfeld (unsigned long long)cnwis.stats.nws_rxoverrun);
17271b188dbSsommerfeld printf("%10llu rxcrcerror\n",
173772dc3bcSsommerfeld (unsigned long long)cnwis.stats.nws_rxcrcerror);
17471b188dbSsommerfeld printf("%10llu rxframe\n",
175772dc3bcSsommerfeld (unsigned long long)cnwis.stats.nws_rxframe);
17671b188dbSsommerfeld printf("%10llu rxerrors\n",
177772dc3bcSsommerfeld (unsigned long long)cnwis.stats.nws_rxerrors);
178772dc3bcSsommerfeld printf("%10llu rxavail\n",
179772dc3bcSsommerfeld (unsigned long long)cnwis.stats.nws_rxavail);
180876c4c29Sitojun
181772dc3bcSsommerfeld printf("%10llu tx\n", (unsigned long long)cnwis.stats.nws_tx);
182772dc3bcSsommerfeld printf("%10llu txokay\n",
183772dc3bcSsommerfeld (unsigned long long)cnwis.stats.nws_txokay);
184772dc3bcSsommerfeld printf("%10llu txabort\n",
185772dc3bcSsommerfeld (unsigned long long)cnwis.stats.nws_txabort);
18671b188dbSsommerfeld printf("%10llu txlostcd\n",
187772dc3bcSsommerfeld (unsigned long long)cnwis.stats.nws_txlostcd);
18871b188dbSsommerfeld printf("%10llu txerrors\n",
189772dc3bcSsommerfeld (unsigned long long)cnwis.stats.nws_txerrors);
19071b188dbSsommerfeld printf("%10llu txretries\n",
191772dc3bcSsommerfeld (unsigned long long)cnwis.stats.nws_txretries[0]);
192876c4c29Sitojun for (i = 1; i < 16; ++i)
193876c4c29Sitojun if (cnwis.stats.nws_txretries[i])
19471b188dbSsommerfeld printf("%10s %10llu %dx retries\n", "",
195772dc3bcSsommerfeld (unsigned long long)
196772dc3bcSsommerfeld cnwis.stats.nws_txretries[i],
19771b188dbSsommerfeld i);
198876c4c29Sitojun }
199876c4c29Sitojun
200876c4c29Sitojun if (rate == 0 && Sflag) {
201876c4c29Sitojun printf(" 0x%02x link integrity field\n",
202876c4c29Sitojun cnws.data[0x4e]);
203876c4c29Sitojun printf(" 0x%02x connection quality\n",
204876c4c29Sitojun cnws.data[0x54]);
205876c4c29Sitojun printf(" 0x%02x spu\n",
206876c4c29Sitojun cnws.data[0x55]);
207876c4c29Sitojun printf(" 0x%02x link quality\n",
208876c4c29Sitojun cnws.data[0x56]);
209876c4c29Sitojun printf(" 0x%02x hhc\n",
210876c4c29Sitojun cnws.data[0x58]);
211876c4c29Sitojun printf(" 0x%02x mhs\n",
212876c4c29Sitojun cnws.data[0x6b]);
213*5084cd4fSchristos u_short x, y;
214*5084cd4fSchristos memcpy(&x, &cnws.data[0x66], sizeof(x));
215*5084cd4fSchristos memcpy(&y, &cnws.data[0x68], sizeof(y));
216*5084cd4fSchristos printf(" %04x %04x revision\n", x, y);
217876c4c29Sitojun printf(" %c%c id\n",
218876c4c29Sitojun cnws.data[0x6e], cnws.data[0x6f]);
219876c4c29Sitojun }
220876c4c29Sitojun
221876c4c29Sitojun if (rate == 0)
222876c4c29Sitojun exit (0);
223876c4c29Sitojun
224876c4c29Sitojun if (ioctl(0, TIOCGWINSZ, &ts) < 0)
225876c4c29Sitojun ts.ts_lines = 24;
226876c4c29Sitojun c = 0;
227876c4c29Sitojun
228876c4c29Sitojun if (Sflag) for (;;) {
229876c4c29Sitojun if (c-- == 0) {
230876c4c29Sitojun printf("lif cq spu lq hhc mhs\n");
231876c4c29Sitojun c = ts.ts_lines - 3;
232876c4c29Sitojun }
233876c4c29Sitojun printf(" %02x %02x %02x %02x %02x %02x\n",
234876c4c29Sitojun cnws.data[0x4e],
235876c4c29Sitojun cnws.data[0x54],
236876c4c29Sitojun cnws.data[0x55],
237876c4c29Sitojun cnws.data[0x56],
238876c4c29Sitojun cnws.data[0x58],
239876c4c29Sitojun cnws.data[0x6b]);
240876c4c29Sitojun fflush(stdout);
241876c4c29Sitojun if (ioctl(skt, SIOCGCNWSTATUS, (caddr_t)&cnws) < 0)
242876c4c29Sitojun err(1, "SIOCGCNWSTATUS");
243876c4c29Sitojun sleep (rate);
244876c4c29Sitojun }
245876c4c29Sitojun
246876c4c29Sitojun for (;;) {
247876c4c29Sitojun if (c-- == 0) {
248876c4c29Sitojun printf("%10s %10s %10s %10s %10s %10s\n",
249876c4c29Sitojun "tx-request", "tx-okay", "tx-error", "tx-retry",
250876c4c29Sitojun "rx", "rx-error");
251876c4c29Sitojun c = ts.ts_lines - 3;
252876c4c29Sitojun memset(&onwis, 0, sizeof(onwis));
253876c4c29Sitojun }
254772dc3bcSsommerfeld printf("%10llu ", (unsigned long long)
255772dc3bcSsommerfeld (cnwis.stats.nws_tx - onwis.stats.nws_tx));
256772dc3bcSsommerfeld printf("%10llu ", (unsigned long long)
257772dc3bcSsommerfeld (cnwis.stats.nws_txokay - onwis.stats.nws_txokay));
258772dc3bcSsommerfeld printf("%10llu ", (unsigned long long)
259772dc3bcSsommerfeld (cnwis.stats.nws_txerrors - onwis.stats.nws_txerrors));
260772dc3bcSsommerfeld printf("%10llu ", (unsigned long long)
261772dc3bcSsommerfeld (cnwis.stats.nws_txretries[0] -
262772dc3bcSsommerfeld onwis.stats.nws_txretries[0]));
263772dc3bcSsommerfeld printf("%10llu ", (unsigned long long)
264772dc3bcSsommerfeld (cnwis.stats.nws_rx - onwis.stats.nws_rx));
265772dc3bcSsommerfeld printf("%10llu\n", (unsigned long long)
266772dc3bcSsommerfeld (cnwis.stats.nws_rxerrors - onwis.stats.nws_rxerrors));
267876c4c29Sitojun fflush(stdout);
268876c4c29Sitojun sleep (rate);
269876c4c29Sitojun onwis = cnwis;
270876c4c29Sitojun
271876c4c29Sitojun if (ioctl(skt, SIOCGCNWSTATS, (caddr_t)&cnwis) < 0)
272876c4c29Sitojun err(1, "SIOCGCNWSTATS");
273876c4c29Sitojun cnwis.stats.nws_txretries[0] = 0;
274876c4c29Sitojun for (i = 1; i < 16; ++i)
275876c4c29Sitojun cnwis.stats.nws_txretries[0] +=
276876c4c29Sitojun cnwis.stats.nws_txretries[i] * i;
277876c4c29Sitojun }
278876c4c29Sitojun }
279