| 1 | /* |
|---|
| 2 | isaset.c - isaset, a user-space program to write ISA registers |
|---|
| 3 | Copyright (C) 2000 Frodo Looijaard <frodol@dds.nl>, and |
|---|
| 4 | Mark D. Studebaker <mdsxyz123@yahoo.com> |
|---|
| 5 | Copyright (C) 2004 The lm_sensors group |
|---|
| 6 | |
|---|
| 7 | This program is free software; you can redistribute it and/or modify |
|---|
| 8 | it under the terms of the GNU General Public License as published by |
|---|
| 9 | the Free Software Foundation; either version 2 of the License, or |
|---|
| 10 | (at your option) any later version. |
|---|
| 11 | |
|---|
| 12 | This program is distributed in the hope that it will be useful, |
|---|
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 15 | GNU General Public License for more details. |
|---|
| 16 | |
|---|
| 17 | You should have received a copy of the GNU General Public License |
|---|
| 18 | along with this program; if not, write to the Free Software |
|---|
| 19 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
|---|
| 20 | */ |
|---|
| 21 | |
|---|
| 22 | /* |
|---|
| 23 | Typical usage: |
|---|
| 24 | isaset 0x295 0x296 0x10 0x12 Write 0x12 to address 0x10 using address/data registers |
|---|
| 25 | isaset -f 0x5010 0x12 Write 0x12 to location 0x5010 |
|---|
| 26 | */ |
|---|
| 27 | |
|---|
| 28 | #include <stdio.h> |
|---|
| 29 | #include <stdlib.h> |
|---|
| 30 | #include <unistd.h> |
|---|
| 31 | #include <string.h> |
|---|
| 32 | #include "util.h" |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | /* To keep glibc2 happy */ |
|---|
| 36 | #if defined(__GLIBC__) && __GLIBC__ == 2 && __GLIBC_MINOR__ >= 0 |
|---|
| 37 | #include <sys/io.h> |
|---|
| 38 | #else |
|---|
| 39 | #include <asm/io.h> |
|---|
| 40 | #endif |
|---|
| 41 | |
|---|
| 42 | #ifdef __powerpc__ |
|---|
| 43 | unsigned long isa_io_base = 0; /* XXX for now */ |
|---|
| 44 | #endif /* __powerpc__ */ |
|---|
| 45 | |
|---|
| 46 | static void help(void) |
|---|
| 47 | { |
|---|
| 48 | fprintf(stderr, |
|---|
| 49 | "Syntax for I2C-like access:\n" |
|---|
| 50 | " isaset [-y] ADDRREG DATAREG ADDRESS VALUE [MASK]\n" |
|---|
| 51 | "Syntax for flat address space:\n" |
|---|
| 52 | " isaset [-y] -f ADDRESS VALUE [MASK]\n"); |
|---|
| 53 | } |
|---|
| 54 | |
|---|
| 55 | int main(int argc, char *argv[]) |
|---|
| 56 | { |
|---|
| 57 | int addrreg, datareg = 0, value, addr = 0, vmask = 0; |
|---|
| 58 | unsigned char res; |
|---|
| 59 | int flags = 0; |
|---|
| 60 | int flat = 0, yes = 0; |
|---|
| 61 | char *end; |
|---|
| 62 | |
|---|
| 63 | /* handle (optional) flags first */ |
|---|
| 64 | while (1+flags < argc && argv[1+flags][0] == '-') { |
|---|
| 65 | switch (argv[1+flags][1]) { |
|---|
| 66 | case 'f': flat = 1; break; |
|---|
| 67 | case 'y': yes = 1; break; |
|---|
| 68 | default: |
|---|
| 69 | fprintf(stderr, "Warning: Unsupported flag " |
|---|
| 70 | "\"-%c\"!\n", argv[1+flags][1]); |
|---|
| 71 | help(); |
|---|
| 72 | exit(1); |
|---|
| 73 | } |
|---|
| 74 | flags++; |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | /* verify that the argument count is correct */ |
|---|
| 78 | if ((!flat && (argc < 1+flags+4 || argc > 1+flags+5)) |
|---|
| 79 | || (flat && (argc < 1+flags+2 || argc > 1+flags+3))) { |
|---|
| 80 | help(); |
|---|
| 81 | exit(1); |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | addrreg = strtol(argv[1+flags], &end, 0); |
|---|
| 85 | if (*end) { |
|---|
| 86 | fprintf(stderr, "Error: Invalid address!\n"); |
|---|
| 87 | help(); |
|---|
| 88 | exit(1); |
|---|
| 89 | } |
|---|
| 90 | if (addrreg < 0 || addrreg > (flat?0xffff:0x3fff)) { |
|---|
| 91 | fprintf(stderr, |
|---|
| 92 | "Error: Address out of range (0x0000-0x%04x)!\n", |
|---|
| 93 | flat?0xffff:0x3fff); |
|---|
| 94 | help(); |
|---|
| 95 | exit(1); |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | if (!flat) { |
|---|
| 99 | datareg = strtol(argv[1+flags+1], &end, 0); |
|---|
| 100 | if (*end) { |
|---|
| 101 | fprintf(stderr, "Error: Invalid data register!\n"); |
|---|
| 102 | help(); |
|---|
| 103 | exit(1); |
|---|
| 104 | } |
|---|
| 105 | if (datareg < 0 || datareg > 0x3fff) { |
|---|
| 106 | fprintf(stderr, "Error: Data register out of range " |
|---|
| 107 | "(0x0000-0x3fff)!\n"); |
|---|
| 108 | help(); |
|---|
| 109 | exit(1); |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | addr = strtol(argv[1+flags+2], &end, 0); |
|---|
| 113 | if (*end) { |
|---|
| 114 | fprintf(stderr, "Error: Invalid address!\n"); |
|---|
| 115 | help(); |
|---|
| 116 | exit(1); |
|---|
| 117 | } |
|---|
| 118 | if (addr < 0 || addr > 0xff) { |
|---|
| 119 | fprintf(stderr, "Error: Address out of range " |
|---|
| 120 | "(0x00-0xff)!\n"); |
|---|
| 121 | help(); |
|---|
| 122 | exit(1); |
|---|
| 123 | } |
|---|
| 124 | } |
|---|
| 125 | |
|---|
| 126 | /* rest is the same for both modes so we cheat on flags */ |
|---|
| 127 | if (!flat) |
|---|
| 128 | flags += 2; |
|---|
| 129 | |
|---|
| 130 | value = strtol(argv[flags+2], &end, 0); |
|---|
| 131 | if (*end) { |
|---|
| 132 | fprintf(stderr, "Error: Invalid value!\n"); |
|---|
| 133 | help(); |
|---|
| 134 | exit(1); |
|---|
| 135 | } |
|---|
| 136 | if (value < 0 || value > 0xff) { |
|---|
| 137 | fprintf(stderr, "Error: Value out of range " |
|---|
| 138 | "(0x00-0xff)!\n"); |
|---|
| 139 | help(); |
|---|
| 140 | exit(1); |
|---|
| 141 | } |
|---|
| 142 | |
|---|
| 143 | if (flags+3 < argc) { |
|---|
| 144 | vmask = strtol(argv[flags+3], &end, 0); |
|---|
| 145 | if (*end) { |
|---|
| 146 | fprintf(stderr, "Error: Invalid mask!\n"); |
|---|
| 147 | help(); |
|---|
| 148 | exit(1); |
|---|
| 149 | } |
|---|
| 150 | if (vmask < 0 || vmask > 0xff) { |
|---|
| 151 | fprintf(stderr, "Error: Mask out of range " |
|---|
| 152 | "(0x00-0xff)!\n"); |
|---|
| 153 | help(); |
|---|
| 154 | exit(1); |
|---|
| 155 | } |
|---|
| 156 | } |
|---|
| 157 | |
|---|
| 158 | if (getuid()) { |
|---|
| 159 | fprintf(stderr, "Error: Can only be run as root " |
|---|
| 160 | "(or make it suid root)\n"); |
|---|
| 161 | exit(1); |
|---|
| 162 | } |
|---|
| 163 | |
|---|
| 164 | if (!yes) { |
|---|
| 165 | fprintf(stderr, "WARNING! Running this program can cause " |
|---|
| 166 | "system crashes, data loss and worse!\n"); |
|---|
| 167 | |
|---|
| 168 | if (flat) |
|---|
| 169 | fprintf(stderr, "I will write value 0x%02x%s to address " |
|---|
| 170 | "0x%x.\n", value, vmask ? " (masked)" : "", |
|---|
| 171 | addrreg); |
|---|
| 172 | else |
|---|
| 173 | fprintf(stderr, "I will write value 0x%02x%s to address " |
|---|
| 174 | "0x%02x of chip with address register 0x%x\n" |
|---|
| 175 | "and data register 0x%x.\n", |
|---|
| 176 | value, vmask ? " (masked)" : "", addr, |
|---|
| 177 | addrreg, datareg); |
|---|
| 178 | |
|---|
| 179 | fprintf(stderr, "Continue? [Y/n] "); |
|---|
| 180 | fflush(stderr); |
|---|
| 181 | if (!user_ack(1)) { |
|---|
| 182 | fprintf(stderr, "Aborting on user request.\n"); |
|---|
| 183 | exit(0); |
|---|
| 184 | } |
|---|
| 185 | } |
|---|
| 186 | |
|---|
| 187 | #ifndef __powerpc__ |
|---|
| 188 | if (!flat && datareg < 0x400 && addrreg < 0x400) { |
|---|
| 189 | if (ioperm(datareg, 1, 1)) { |
|---|
| 190 | fprintf(stderr, "Error: Could not ioperm() data " |
|---|
| 191 | "register!\n"); |
|---|
| 192 | exit(1); |
|---|
| 193 | } |
|---|
| 194 | if (ioperm(addrreg, 1, 1)) { |
|---|
| 195 | fprintf(stderr, "Error: Could not ioperm() address " |
|---|
| 196 | "register!\n"); |
|---|
| 197 | exit(1); |
|---|
| 198 | } |
|---|
| 199 | } else { |
|---|
| 200 | if (iopl(3)) { |
|---|
| 201 | fprintf(stderr, "Error: Could not do iopl(3)!\n"); |
|---|
| 202 | exit(1); |
|---|
| 203 | } |
|---|
| 204 | } |
|---|
| 205 | #endif |
|---|
| 206 | |
|---|
| 207 | if (vmask) { |
|---|
| 208 | int oldvalue; |
|---|
| 209 | |
|---|
| 210 | if (flat) { |
|---|
| 211 | oldvalue = inb(addrreg); |
|---|
| 212 | } else { |
|---|
| 213 | outb(addr, addrreg); |
|---|
| 214 | oldvalue = inb(datareg); |
|---|
| 215 | } |
|---|
| 216 | |
|---|
| 217 | if (oldvalue < 0) { |
|---|
| 218 | fprintf(stderr, "Error: Failed to read old value\n"); |
|---|
| 219 | exit(1); |
|---|
| 220 | } |
|---|
| 221 | |
|---|
| 222 | value = (value & vmask) | (oldvalue & ~vmask); |
|---|
| 223 | |
|---|
| 224 | if (!yes) { |
|---|
| 225 | fprintf(stderr, "Old value 0x%02x, write mask " |
|---|
| 226 | "0x%02x: Will write 0x%02x to %s " |
|---|
| 227 | "0x%02x\n", oldvalue, vmask, value, |
|---|
| 228 | flat ? "address" : "register", |
|---|
| 229 | flat ? addrreg : addr); |
|---|
| 230 | |
|---|
| 231 | fprintf(stderr, "Continue? [Y/n] "); |
|---|
| 232 | fflush(stderr); |
|---|
| 233 | if (!user_ack(1)) { |
|---|
| 234 | fprintf(stderr, "Aborting on user request.\n"); |
|---|
| 235 | exit(0); |
|---|
| 236 | } |
|---|
| 237 | } |
|---|
| 238 | } |
|---|
| 239 | |
|---|
| 240 | /* do the real thing */ |
|---|
| 241 | if (flat) { |
|---|
| 242 | /* write */ |
|---|
| 243 | outb(value, addrreg); |
|---|
| 244 | /* readback */ |
|---|
| 245 | res = inb(addrreg); |
|---|
| 246 | } else { |
|---|
| 247 | /* write */ |
|---|
| 248 | outb(addr, addrreg); |
|---|
| 249 | outb(value, datareg); |
|---|
| 250 | /* readback */ |
|---|
| 251 | res = inb(datareg); |
|---|
| 252 | } |
|---|
| 253 | |
|---|
| 254 | if (res != value) { |
|---|
| 255 | fprintf(stderr, "Data mismatch, wrote 0x%02x, " |
|---|
| 256 | "read 0x%02x back.\n", value, res); |
|---|
| 257 | } |
|---|
| 258 | |
|---|
| 259 | exit(0); |
|---|
| 260 | } |
|---|