summaryrefslogtreecommitdiff
path: root/prog/dump/isaset.c
blob: e743755a2621e59751d17363df52025a10f16d87 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/*
    isaset.c - isaset, a user-space program to write ISA registers
    Copyright (C) 2000  Frodo Looijaard <frodol@dds.nl>, and
                        Mark D. Studebaker <mdsxyz123@yahoo.com>
    Copyright (C) 2004-2011  Jean Delvare <jdelvare@suse.de>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
    MA 02110-1301 USA.
*/

/*
	Typical usage:
	isaset 0x295 0x296 0x10 0x12	Write 0x12 to address 0x10 using address/data registers
	isaset -f 0x5010 0x12		Write 0x12 to location 0x5010
*/

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include "util.h"


/* To keep glibc2 happy */
#if defined(__GLIBC__) && __GLIBC__ == 2 && __GLIBC_MINOR__ >= 0
#include <sys/io.h>
#else
#include <asm/io.h>
#endif

#ifdef __powerpc__
unsigned long isa_io_base = 0; /* XXX for now */
#endif /* __powerpc__ */

static void help(void)
{
	fprintf(stderr,
	        "Syntax for I2C-like access:\n"
	        "  isaset [OPTIONS] ADDRREG DATAREG ADDRESS VALUE [MASK]\n"
	        "Syntax for flat address space:\n"
	        "  isaset -f [OPTIONS] ADDRESS VALUE [MASK]\n"
		"Options:\n"
		"  -f	Enable flat address space mode\n"
		"  -y	Assume affirmative answer to all questions\n"
		"  -W	Write a word (16-bit) value\n"
		"  -L	Write a long (32-bit) value\n");
}

int main(int argc, char *argv[])
{
	int addrreg, datareg = 0, addr = 0;
	unsigned long value, vmask = 0, maxval = 0xff, res;
	int flags = 0;
	int flat = 0, yes = 0, width = 1;
	char *end;

	/* handle (optional) flags first */
	while (1+flags < argc && argv[1+flags][0] == '-') {
		switch (argv[1+flags][1]) {
		case 'f': flat = 1; break;
		case 'y': yes = 1; break;
		case 'W': width = 2; maxval = 0xffff; break;
		case 'L': width = 4; maxval = 0xffffffff; break;
		default:
			fprintf(stderr, "Warning: Unsupported flag "
				"\"-%c\"!\n", argv[1+flags][1]);
			help();
			exit(1);
		}
		flags++;
	}

	/* verify that the argument count is correct */
	if ((!flat && (argc < 1+flags+4 || argc > 1+flags+5))
	 || (flat && (argc < 1+flags+2 || argc > 1+flags+3))) {
		help();
		exit(1);
	}

	addrreg = strtol(argv[1+flags], &end, 0);
	if (*end) {
		fprintf(stderr, "Error: Invalid address!\n");
		help();
		exit(1);
	}
	if (addrreg < 0 || addrreg > (flat?0xffff:0x3fff)) {
		fprintf(stderr,
		        "Error: Address out of range (0x0000-0x%04x)!\n",
			flat?0xffff:0x3fff);
		help();
		exit(1);
	}

	if (!flat) {
		datareg = strtol(argv[1+flags+1], &end, 0);
		if (*end) {
			fprintf(stderr, "Error: Invalid data register!\n");
			help();
			exit(1);
		}
		if (datareg < 0 || datareg > 0x3fff) {
			fprintf(stderr, "Error: Data register out of range "
			        "(0x0000-0x3fff)!\n");
			help();
			exit(1);
		}

		addr = strtol(argv[1+flags+2], &end, 0);
		if (*end) {
			fprintf(stderr, "Error: Invalid address!\n");
			help();
			exit(1);
		}
		if (addr < 0 || addr > 0xff) {
			fprintf(stderr, "Error: Address out of range "
			        "(0x00-0xff)!\n");
			help();
			exit(1);
		}
	}

	/* rest is the same for both modes so we cheat on flags */
	if (!flat)
		flags += 2;

	value = strtoul(argv[flags+2], &end, 0);
	if (*end) {
		fprintf(stderr, "Error: Invalid value!\n");
		help();
		exit(1);
	}
	if (value > maxval) {
		fprintf(stderr, "Error: Value out of range "
			"(0x%0*u-%0*lu)!\n", width * 2, 0, width * 2, maxval);
		help();
		exit(1);
	}

	if (flags+3 < argc) {
		vmask = strtoul(argv[flags+3], &end, 0);
		if (*end) {
			fprintf(stderr, "Error: Invalid mask!\n");
			help();
			exit(1);
		}
		if (vmask > maxval) {
			fprintf(stderr, "Error: Mask out of range "
				"(0x%0*u-%0*lu)!\n", width * 2, 0,
				width * 2, maxval);
			help();
			exit(1);
		}
	}

	if (geteuid()) {
		fprintf(stderr, "Error: Can only be run as root "
		        "(or make it suid root)\n");
		exit(1);
	}

	if (!yes) {
		fprintf(stderr, "WARNING! Running this program can cause "
		        "system crashes, data loss and worse!\n");

		if (flat)
			fprintf(stderr,
				"I will write value 0x%0*lx%s to address "
		                "0x%x.\n", width * 2, value,
				vmask ? " (masked)" : "", addrreg);
		else
			fprintf(stderr,
				"I will write value 0x%0*lx%s to address "
		                "0x%02x of chip with address register 0x%x\n"
		                "and data register 0x%x.\n", width * 2,
		                value, vmask ? " (masked)" : "", addr,
			        addrreg, datareg);

		fprintf(stderr, "Continue? [Y/n] ");
		fflush(stderr);
		if (!user_ack(1)) {
			fprintf(stderr, "Aborting on user request.\n");
			exit(0);
		}
	}

#ifndef __powerpc__
	if (!flat && datareg < 0x400 && addrreg < 0x400) {
		if (ioperm(datareg, 1, 1)) {
			fprintf(stderr, "Error: Could not ioperm() data "
			        "register!\n");
			exit(1);
		}
		if (ioperm(addrreg, 1, 1)) {
			fprintf(stderr, "Error: Could not ioperm() address "
		        	"register!\n");
			exit(1);
		}
	} else {
		if (iopl(3)) {
			fprintf(stderr, "Error: Could not do iopl(3)!\n");
			exit(1);
		}
	}
#endif

	if (vmask) {
		unsigned long oldvalue;

		if (flat) {
			oldvalue = inx(addrreg, width);
		} else {	
			outb(addr, addrreg);
			oldvalue = inx(datareg, width);
		}

		value = (value & vmask) | (oldvalue & ~vmask);

		if (!yes) {
			fprintf(stderr, "Old value 0x%0*lx, write mask "
				"0x%0*lx: Will write 0x%0*lx to %s "
				"0x%02x\n", width * 2, oldvalue,
				width * 2, vmask, width * 2, value,
				flat ? "address" : "register",
				flat ? addrreg : addr);

			fprintf(stderr, "Continue? [Y/n] ");
			fflush(stderr);
			if (!user_ack(1)) {
				fprintf(stderr, "Aborting on user request.\n");
				exit(0);
			}
		}
	}

	/* do the real thing */
	if (flat) {
		/* write */
		outx(value, addrreg, width);
		/* readback */
		res = inx(addrreg, width);
	} else {	
		/* write */
		outb(addr, addrreg);
		outx(value, datareg, width);
		/* readback */
		res = inx(datareg, width);
	}

	if (res != value) {
		fprintf(stderr, "Data mismatch, wrote 0x%0*lx, "
		        "read 0x%0*lx back.\n", width * 2, value,
			width * 2, res);
	}

	exit(0);
}