| 1 | /* |
|---|
| 2 | i2cset.c - A user-space program to write an I2C register. |
|---|
| 3 | Copyright (C) 2001-2003 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 | #include <errno.h> |
|---|
| 23 | #include <string.h> |
|---|
| 24 | #include <stdio.h> |
|---|
| 25 | #include <stdlib.h> |
|---|
| 26 | #include <unistd.h> |
|---|
| 27 | #include "i2cbusses.h" |
|---|
| 28 | #include "i2c-dev.h" |
|---|
| 29 | #include "version.h" |
|---|
| 30 | |
|---|
| 31 | void help(void) __attribute__ ((noreturn)); |
|---|
| 32 | |
|---|
| 33 | void help(void) |
|---|
| 34 | { |
|---|
| 35 | fprintf(stderr, "Syntax: i2cset [-y] I2CBUS CHIP-ADDRESS DATA-ADDRESS " |
|---|
| 36 | "VALUE [MODE] [MASK]\n" |
|---|
| 37 | " i2cset -V\n" |
|---|
| 38 | " MODE is 'b[yte]' or 'w[ord]' (default b)\n" |
|---|
| 39 | " I2CBUS is an integer\n"); |
|---|
| 40 | print_i2c_busses(0); |
|---|
| 41 | exit(1); |
|---|
| 42 | } |
|---|
| 43 | |
|---|
| 44 | int main(int argc, char *argv[]) |
|---|
| 45 | { |
|---|
| 46 | char *end; |
|---|
| 47 | int res, i2cbus, address, size, file; |
|---|
| 48 | int value, daddress, vmask = 0; |
|---|
| 49 | int e1; |
|---|
| 50 | char filename[20]; |
|---|
| 51 | long funcs; |
|---|
| 52 | int flags = 0; |
|---|
| 53 | int yes = 0, version = 0; |
|---|
| 54 | |
|---|
| 55 | /* handle (optional) flags first */ |
|---|
| 56 | while (1+flags < argc && argv[1+flags][0] == '-') { |
|---|
| 57 | switch (argv[1+flags][1]) { |
|---|
| 58 | case 'V': version = 1; break; |
|---|
| 59 | case 'y': yes = 1; break; |
|---|
| 60 | default: |
|---|
| 61 | fprintf(stderr, "Warning: Unsupported flag " |
|---|
| 62 | "\"-%c\"!\n", argv[1+flags][1]); |
|---|
| 63 | help(); |
|---|
| 64 | exit(1); |
|---|
| 65 | } |
|---|
| 66 | flags++; |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | if (version) { |
|---|
| 70 | fprintf(stderr, "i2cset version %s\n", LM_VERSION); |
|---|
| 71 | exit(0); |
|---|
| 72 | } |
|---|
| 73 | |
|---|
| 74 | if (argc + flags < 5) |
|---|
| 75 | help(); |
|---|
| 76 | |
|---|
| 77 | i2cbus = strtol(argv[flags+1], &end, 0); |
|---|
| 78 | if (*end || i2cbus < 0 || i2cbus > 0x3f) { |
|---|
| 79 | fprintf(stderr, "Error: I2CBUS argument invalid!\n"); |
|---|
| 80 | help(); |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | address = strtol(argv[flags+2], &end, 0); |
|---|
| 84 | if (*end || address < 0 || address > 0x7f) { |
|---|
| 85 | fprintf(stderr, "Error: Chip address invalid!\n"); |
|---|
| 86 | help(); |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | daddress = strtol(argv[flags+3], &end, 0); |
|---|
| 90 | if (*end || daddress < 0 || daddress > 0xff) { |
|---|
| 91 | fprintf(stderr, "Error: Data address invalid!\n"); |
|---|
| 92 | help(); |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | value = strtol(argv[flags+4], &end, 0); |
|---|
| 96 | if (*end) { |
|---|
| 97 | fprintf(stderr, "Error: Data value invalid!\n"); |
|---|
| 98 | help(); |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | if (argc < flags + 6) { |
|---|
| 102 | fprintf(stderr, "No size specified (using byte-data access)\n"); |
|---|
| 103 | size = I2C_SMBUS_BYTE_DATA; |
|---|
| 104 | } else if (!strcmp(argv[flags+5], "b")) |
|---|
| 105 | size = I2C_SMBUS_BYTE_DATA; |
|---|
| 106 | else if (!strcmp(argv[flags+5], "w")) |
|---|
| 107 | size = I2C_SMBUS_WORD_DATA; |
|---|
| 108 | else { |
|---|
| 109 | fprintf(stderr, "Error: Invalid mode!\n"); |
|---|
| 110 | help(); |
|---|
| 111 | } |
|---|
| 112 | |
|---|
| 113 | if (argc >= flags + 7) { |
|---|
| 114 | vmask = strtol(argv[flags+6], &end, 0); |
|---|
| 115 | if (*end || vmask == 0) { |
|---|
| 116 | fprintf(stderr, "Error: Data value mask invalid!\n"); |
|---|
| 117 | help(); |
|---|
| 118 | } |
|---|
| 119 | } |
|---|
| 120 | |
|---|
| 121 | if (value < 0 |
|---|
| 122 | || (size == I2C_SMBUS_BYTE_DATA && value > 0xff) |
|---|
| 123 | || (size == I2C_SMBUS_WORD_DATA && value > 0xffff)) { |
|---|
| 124 | fprintf(stderr, "Error: Data value out of range!\n"); |
|---|
| 125 | help(); |
|---|
| 126 | } |
|---|
| 127 | |
|---|
| 128 | file = open_i2c_dev(i2cbus, filename, 0); |
|---|
| 129 | if (file < 0) { |
|---|
| 130 | exit(1); |
|---|
| 131 | } |
|---|
| 132 | |
|---|
| 133 | /* check adapter functionality */ |
|---|
| 134 | if (ioctl(file, I2C_FUNCS, &funcs) < 0) { |
|---|
| 135 | fprintf(stderr, "Error: Could not get the adapter " |
|---|
| 136 | "functionality matrix: %s\n", strerror(errno)); |
|---|
| 137 | exit(1); |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | switch (size) { |
|---|
| 141 | case I2C_SMBUS_BYTE_DATA: |
|---|
| 142 | if (!(funcs & I2C_FUNC_SMBUS_WRITE_BYTE_DATA)) { |
|---|
| 143 | fprintf(stderr, "Error: Adapter for i2c bus %d does " |
|---|
| 144 | "not have byte write capability\n", i2cbus); |
|---|
| 145 | exit(1); |
|---|
| 146 | } |
|---|
| 147 | break; |
|---|
| 148 | |
|---|
| 149 | case I2C_SMBUS_WORD_DATA: |
|---|
| 150 | if (!(funcs & I2C_FUNC_SMBUS_WRITE_WORD_DATA)) { |
|---|
| 151 | fprintf(stderr, "Error: Adapter for i2c bus %d does " |
|---|
| 152 | "not have word write capability\n", i2cbus); |
|---|
| 153 | exit(1); |
|---|
| 154 | } |
|---|
| 155 | break; |
|---|
| 156 | } |
|---|
| 157 | |
|---|
| 158 | /* use FORCE so that we can write registers even when |
|---|
| 159 | a driver is also running */ |
|---|
| 160 | if (ioctl(file, I2C_SLAVE_FORCE, address) < 0) { |
|---|
| 161 | fprintf(stderr, "Error: Could not set address to %d: %s\n", |
|---|
| 162 | address, strerror(errno)); |
|---|
| 163 | exit(1); |
|---|
| 164 | } |
|---|
| 165 | |
|---|
| 166 | if (!yes) { |
|---|
| 167 | char s[2]; |
|---|
| 168 | int dont = 0; |
|---|
| 169 | |
|---|
| 170 | fprintf(stderr, "WARNING! This program can confuse your I2C " |
|---|
| 171 | "bus, cause data loss and worse!\n"); |
|---|
| 172 | |
|---|
| 173 | if (address >= 0x50 && address <= 0x57) { |
|---|
| 174 | fprintf(stderr, "DANGEROUS! Writing to a serial " |
|---|
| 175 | "EEPROM on a memory DIMM\nmay render your " |
|---|
| 176 | "memory USELESS and make your system " |
|---|
| 177 | "UNBOOTABLE!\n"); |
|---|
| 178 | dont = 1; |
|---|
| 179 | } |
|---|
| 180 | |
|---|
| 181 | fprintf(stderr, "I will write to device file %s, chip address " |
|---|
| 182 | "0x%02x, data address\n0x%02x, data 0x%02x%s, mode " |
|---|
| 183 | "%s.\n", filename, address, daddress, value, |
|---|
| 184 | vmask ? " (masked)" : "", |
|---|
| 185 | size == I2C_SMBUS_BYTE_DATA ? "byte" : "word"); |
|---|
| 186 | |
|---|
| 187 | fprintf(stderr, "Continue? [%s] ", dont ? "y/N" : "Y/n"); |
|---|
| 188 | fflush(stderr); |
|---|
| 189 | fgets(s, 2, stdin); |
|---|
| 190 | if ((s[0] != '\n' || dont) && s[0] != 'y' && s[0] != 'Y') { |
|---|
| 191 | fprintf(stderr, "Aborting on user request.\n"); |
|---|
| 192 | exit(0); |
|---|
| 193 | } |
|---|
| 194 | } |
|---|
| 195 | |
|---|
| 196 | if (vmask) { |
|---|
| 197 | int oldvalue; |
|---|
| 198 | |
|---|
| 199 | if (size == I2C_SMBUS_WORD_DATA) { |
|---|
| 200 | oldvalue = i2c_smbus_read_word_data(file, daddress); |
|---|
| 201 | } else { |
|---|
| 202 | oldvalue = i2c_smbus_read_byte_data(file, daddress); |
|---|
| 203 | } |
|---|
| 204 | |
|---|
| 205 | if (oldvalue < 0) { |
|---|
| 206 | fprintf(stderr, "Error: Failed to read old value\n"); |
|---|
| 207 | exit(1); |
|---|
| 208 | } |
|---|
| 209 | |
|---|
| 210 | value = (value & vmask) | (oldvalue & ~vmask); |
|---|
| 211 | |
|---|
| 212 | if (!yes) { |
|---|
| 213 | char s[2]; |
|---|
| 214 | |
|---|
| 215 | fprintf(stderr, "Old value 0x%0*x, write mask " |
|---|
| 216 | "0x%0*x: Will write 0x%0*x to register " |
|---|
| 217 | "0x%02x\n", |
|---|
| 218 | size == I2C_SMBUS_WORD_DATA ? 4 : 2, oldvalue, |
|---|
| 219 | size == I2C_SMBUS_WORD_DATA ? 4 : 2, vmask, |
|---|
| 220 | size == I2C_SMBUS_WORD_DATA ? 4 : 2, value, |
|---|
| 221 | daddress); |
|---|
| 222 | |
|---|
| 223 | fprintf(stderr, "Continue? [Y/n] "); |
|---|
| 224 | fflush(stderr); |
|---|
| 225 | fgets(s, 2, stdin); |
|---|
| 226 | if (s[0] != '\n' && s[0] != 'y' && s[0] != 'Y') { |
|---|
| 227 | fprintf(stderr, "Aborting on user request.\n"); |
|---|
| 228 | exit(0); |
|---|
| 229 | } |
|---|
| 230 | } |
|---|
| 231 | } |
|---|
| 232 | |
|---|
| 233 | e1 = 0; |
|---|
| 234 | if (size == I2C_SMBUS_WORD_DATA) { |
|---|
| 235 | res = i2c_smbus_write_word_data(file, daddress, value); |
|---|
| 236 | } else { |
|---|
| 237 | res = i2c_smbus_write_byte_data(file, daddress, value); |
|---|
| 238 | } |
|---|
| 239 | if (res < 0) { |
|---|
| 240 | fprintf(stderr, "Warning - write failed\n"); |
|---|
| 241 | e1++; |
|---|
| 242 | } |
|---|
| 243 | |
|---|
| 244 | if (size == I2C_SMBUS_WORD_DATA) { |
|---|
| 245 | res = i2c_smbus_read_word_data(file, daddress); |
|---|
| 246 | } else { |
|---|
| 247 | res = i2c_smbus_read_byte_data(file, daddress); |
|---|
| 248 | } |
|---|
| 249 | |
|---|
| 250 | if (res < 0) { |
|---|
| 251 | fprintf(stderr, "Warning - readback failed\n"); |
|---|
| 252 | e1++; |
|---|
| 253 | } else |
|---|
| 254 | if (res != value) { |
|---|
| 255 | e1++; |
|---|
| 256 | fprintf(stderr, "Warning - data mismatch - wrote " |
|---|
| 257 | "0x%0*x, read back 0x%0*x\n", |
|---|
| 258 | size == I2C_SMBUS_WORD_DATA ? 4 : 2, value, |
|---|
| 259 | size == I2C_SMBUS_WORD_DATA ? 4 : 2, res); |
|---|
| 260 | } else { |
|---|
| 261 | fprintf(stderr, "Value 0x%0*x written, readback matched\n", |
|---|
| 262 | size == I2C_SMBUS_WORD_DATA ? 4 : 2, value); |
|---|
| 263 | } |
|---|
| 264 | |
|---|
| 265 | exit(e1); |
|---|
| 266 | } |
|---|