| 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-2008 Jean Delvare <khali@linux-fr.org> |
|---|
| 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., 51 Franklin Street, Fifth Floor, Boston, |
|---|
| 20 | MA 02110-1301 USA. |
|---|
| 21 | */ |
|---|
| 22 | |
|---|
| 23 | #include <errno.h> |
|---|
| 24 | #include <string.h> |
|---|
| 25 | #include <stdio.h> |
|---|
| 26 | #include <stdlib.h> |
|---|
| 27 | #include <unistd.h> |
|---|
| 28 | #include <linux/i2c-dev.h> |
|---|
| 29 | #include "i2cbusses.h" |
|---|
| 30 | #include "util.h" |
|---|
| 31 | #include "../version.h" |
|---|
| 32 | |
|---|
| 33 | static void help(void) __attribute__ ((noreturn)); |
|---|
| 34 | |
|---|
| 35 | static void help(void) |
|---|
| 36 | { |
|---|
| 37 | fprintf(stderr, |
|---|
| 38 | "Usage: i2cset [-f] [-y] [-m MASK] I2CBUS CHIP-ADDRESS DATA-ADDRESS [VALUE [MODE]]\n" |
|---|
| 39 | " I2CBUS is an integer or an I2C bus name\n" |
|---|
| 40 | " ADDRESS is an integer (0x03 - 0x77)\n" |
|---|
| 41 | " MODE is one of:\n" |
|---|
| 42 | " b (byte, default)\n" |
|---|
| 43 | " w (word)\n" |
|---|
| 44 | " Append p for SMBus PEC\n"); |
|---|
| 45 | exit(1); |
|---|
| 46 | } |
|---|
| 47 | |
|---|
| 48 | static int check_funcs(int file, int size, int pec) |
|---|
| 49 | { |
|---|
| 50 | unsigned long funcs; |
|---|
| 51 | |
|---|
| 52 | /* check adapter functionality */ |
|---|
| 53 | if (ioctl(file, I2C_FUNCS, &funcs) < 0) { |
|---|
| 54 | fprintf(stderr, "Error: Could not get the adapter " |
|---|
| 55 | "functionality matrix: %s\n", strerror(errno)); |
|---|
| 56 | return -1; |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | switch (size) { |
|---|
| 60 | case I2C_SMBUS_BYTE: |
|---|
| 61 | if (!(funcs & I2C_FUNC_SMBUS_WRITE_BYTE)) { |
|---|
| 62 | fprintf(stderr, MISSING_FUNC_FMT, "SMBus send byte"); |
|---|
| 63 | return -1; |
|---|
| 64 | } |
|---|
| 65 | break; |
|---|
| 66 | |
|---|
| 67 | case I2C_SMBUS_BYTE_DATA: |
|---|
| 68 | if (!(funcs & I2C_FUNC_SMBUS_WRITE_BYTE_DATA)) { |
|---|
| 69 | fprintf(stderr, MISSING_FUNC_FMT, "SMBus write byte"); |
|---|
| 70 | return -1; |
|---|
| 71 | } |
|---|
| 72 | break; |
|---|
| 73 | |
|---|
| 74 | case I2C_SMBUS_WORD_DATA: |
|---|
| 75 | if (!(funcs & I2C_FUNC_SMBUS_WRITE_WORD_DATA)) { |
|---|
| 76 | fprintf(stderr, MISSING_FUNC_FMT, "SMBus write word"); |
|---|
| 77 | return -1; |
|---|
| 78 | } |
|---|
| 79 | break; |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | if (pec |
|---|
| 83 | && !(funcs & (I2C_FUNC_SMBUS_PEC | I2C_FUNC_I2C))) { |
|---|
| 84 | fprintf(stderr, "Warning: Adapter does " |
|---|
| 85 | "not seem to support PEC\n"); |
|---|
| 86 | } |
|---|
| 87 | |
|---|
| 88 | return 0; |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | static int confirm(const char *filename, int address, int size, int daddress, |
|---|
| 92 | int value, int vmask, int pec) |
|---|
| 93 | { |
|---|
| 94 | int dont = 0; |
|---|
| 95 | |
|---|
| 96 | fprintf(stderr, "WARNING! This program can confuse your I2C " |
|---|
| 97 | "bus, cause data loss and worse!\n"); |
|---|
| 98 | |
|---|
| 99 | if (address >= 0x50 && address <= 0x57) { |
|---|
| 100 | fprintf(stderr, "DANGEROUS! Writing to a serial " |
|---|
| 101 | "EEPROM on a memory DIMM\nmay render your " |
|---|
| 102 | "memory USELESS and make your system " |
|---|
| 103 | "UNBOOTABLE!\n"); |
|---|
| 104 | dont++; |
|---|
| 105 | } |
|---|
| 106 | |
|---|
| 107 | fprintf(stderr, "I will write to device file %s, chip address " |
|---|
| 108 | "0x%02x, data address\n0x%02x, ", filename, address, daddress); |
|---|
| 109 | if (size == I2C_SMBUS_BYTE) |
|---|
| 110 | fprintf(stderr, "no data.\n"); |
|---|
| 111 | else |
|---|
| 112 | fprintf(stderr, "data 0x%02x%s, mode %s.\n", value, |
|---|
| 113 | vmask ? " (masked)" : "", |
|---|
| 114 | size == I2C_SMBUS_BYTE_DATA ? "byte" : "word"); |
|---|
| 115 | if (pec) |
|---|
| 116 | fprintf(stderr, "PEC checking enabled.\n"); |
|---|
| 117 | |
|---|
| 118 | fprintf(stderr, "Continue? [%s] ", dont ? "y/N" : "Y/n"); |
|---|
| 119 | fflush(stderr); |
|---|
| 120 | if (!user_ack(!dont)) { |
|---|
| 121 | fprintf(stderr, "Aborting on user request.\n"); |
|---|
| 122 | return 0; |
|---|
| 123 | } |
|---|
| 124 | |
|---|
| 125 | return 1; |
|---|
| 126 | } |
|---|
| 127 | |
|---|
| 128 | int main(int argc, char *argv[]) |
|---|
| 129 | { |
|---|
| 130 | char *end; |
|---|
| 131 | const char *maskp = NULL; |
|---|
| 132 | int res, i2cbus, address, size, file; |
|---|
| 133 | int value, daddress, vmask = 0; |
|---|
| 134 | char filename[20]; |
|---|
| 135 | int pec = 0; |
|---|
| 136 | int flags = 0; |
|---|
| 137 | int force = 0, yes = 0, version = 0, readback = 0; |
|---|
| 138 | |
|---|
| 139 | /* handle (optional) flags first */ |
|---|
| 140 | while (1+flags < argc && argv[1+flags][0] == '-') { |
|---|
| 141 | switch (argv[1+flags][1]) { |
|---|
| 142 | case 'V': version = 1; break; |
|---|
| 143 | case 'f': force = 1; break; |
|---|
| 144 | case 'y': yes = 1; break; |
|---|
| 145 | case 'm': |
|---|
| 146 | if (2+flags < argc) |
|---|
| 147 | maskp = argv[2+flags]; |
|---|
| 148 | flags++; |
|---|
| 149 | break; |
|---|
| 150 | case 'r': readback = 1; break; |
|---|
| 151 | default: |
|---|
| 152 | fprintf(stderr, "Error: Unsupported option " |
|---|
| 153 | "\"%s\"!\n", argv[1+flags]); |
|---|
| 154 | help(); |
|---|
| 155 | exit(1); |
|---|
| 156 | } |
|---|
| 157 | flags++; |
|---|
| 158 | } |
|---|
| 159 | |
|---|
| 160 | if (version) { |
|---|
| 161 | fprintf(stderr, "i2cset version %s\n", VERSION); |
|---|
| 162 | exit(0); |
|---|
| 163 | } |
|---|
| 164 | |
|---|
| 165 | if (argc < flags + 4) |
|---|
| 166 | help(); |
|---|
| 167 | |
|---|
| 168 | i2cbus = lookup_i2c_bus(argv[flags+1]); |
|---|
| 169 | if (i2cbus < 0) |
|---|
| 170 | help(); |
|---|
| 171 | |
|---|
| 172 | address = parse_i2c_address(argv[flags+2]); |
|---|
| 173 | if (address < 0) |
|---|
| 174 | help(); |
|---|
| 175 | |
|---|
| 176 | daddress = strtol(argv[flags+3], &end, 0); |
|---|
| 177 | if (*end || daddress < 0 || daddress > 0xff) { |
|---|
| 178 | fprintf(stderr, "Error: Data address invalid!\n"); |
|---|
| 179 | help(); |
|---|
| 180 | } |
|---|
| 181 | |
|---|
| 182 | if (argc > flags + 4) { |
|---|
| 183 | size = I2C_SMBUS_BYTE_DATA; |
|---|
| 184 | value = strtol(argv[flags+4], &end, 0); |
|---|
| 185 | if (*end || value < 0) { |
|---|
| 186 | fprintf(stderr, "Error: Data value invalid!\n"); |
|---|
| 187 | help(); |
|---|
| 188 | } |
|---|
| 189 | } else { |
|---|
| 190 | size = I2C_SMBUS_BYTE; |
|---|
| 191 | value = -1; |
|---|
| 192 | } |
|---|
| 193 | |
|---|
| 194 | if (argc > flags + 5) { |
|---|
| 195 | switch (argv[flags+5][0]) { |
|---|
| 196 | case 'b': size = I2C_SMBUS_BYTE_DATA; break; |
|---|
| 197 | case 'w': size = I2C_SMBUS_WORD_DATA; break; |
|---|
| 198 | default: |
|---|
| 199 | fprintf(stderr, "Error: Invalid mode!\n"); |
|---|
| 200 | help(); |
|---|
| 201 | } |
|---|
| 202 | pec = argv[flags+5][1] == 'p'; |
|---|
| 203 | } |
|---|
| 204 | |
|---|
| 205 | /* Old method to provide the value mask, deprecated and no longer |
|---|
| 206 | documented but still supported for compatibility */ |
|---|
| 207 | if (argc > flags + 6) { |
|---|
| 208 | if (maskp) { |
|---|
| 209 | fprintf(stderr, "Error: Data value mask provided twice!\n"); |
|---|
| 210 | help(); |
|---|
| 211 | } |
|---|
| 212 | fprintf(stderr, "Warning: Using deprecated way to set the data value mask!\n"); |
|---|
| 213 | fprintf(stderr, " Please switch to using -m.\n"); |
|---|
| 214 | maskp = argv[flags+6]; |
|---|
| 215 | } |
|---|
| 216 | |
|---|
| 217 | if (maskp) { |
|---|
| 218 | vmask = strtol(maskp, &end, 0); |
|---|
| 219 | if (*end || vmask == 0) { |
|---|
| 220 | fprintf(stderr, "Error: Data value mask invalid!\n"); |
|---|
| 221 | help(); |
|---|
| 222 | } |
|---|
| 223 | } |
|---|
| 224 | |
|---|
| 225 | if ((size == I2C_SMBUS_BYTE_DATA && value > 0xff) |
|---|
| 226 | || (size == I2C_SMBUS_WORD_DATA && value > 0xffff)) { |
|---|
| 227 | fprintf(stderr, "Error: Data value out of range!\n"); |
|---|
| 228 | help(); |
|---|
| 229 | } |
|---|
| 230 | |
|---|
| 231 | file = open_i2c_dev(i2cbus, filename, 0); |
|---|
| 232 | if (file < 0 |
|---|
| 233 | || check_funcs(file, size, pec) |
|---|
| 234 | || set_slave_addr(file, address, force)) |
|---|
| 235 | exit(1); |
|---|
| 236 | |
|---|
| 237 | if (!yes && !confirm(filename, address, size, daddress, |
|---|
| 238 | value, vmask, pec)) |
|---|
| 239 | exit(0); |
|---|
| 240 | |
|---|
| 241 | if (vmask) { |
|---|
| 242 | int oldvalue; |
|---|
| 243 | |
|---|
| 244 | switch (size) { |
|---|
| 245 | case I2C_SMBUS_BYTE: |
|---|
| 246 | oldvalue = i2c_smbus_read_byte(file); |
|---|
| 247 | break; |
|---|
| 248 | case I2C_SMBUS_WORD_DATA: |
|---|
| 249 | oldvalue = i2c_smbus_read_word_data(file, daddress); |
|---|
| 250 | break; |
|---|
| 251 | default: |
|---|
| 252 | oldvalue = i2c_smbus_read_byte_data(file, daddress); |
|---|
| 253 | } |
|---|
| 254 | |
|---|
| 255 | if (oldvalue < 0) { |
|---|
| 256 | fprintf(stderr, "Error: Failed to read old value\n"); |
|---|
| 257 | exit(1); |
|---|
| 258 | } |
|---|
| 259 | |
|---|
| 260 | value = (value & vmask) | (oldvalue & ~vmask); |
|---|
| 261 | |
|---|
| 262 | if (!yes) { |
|---|
| 263 | fprintf(stderr, "Old value 0x%0*x, write mask " |
|---|
| 264 | "0x%0*x: Will write 0x%0*x to register " |
|---|
| 265 | "0x%02x\n", |
|---|
| 266 | size == I2C_SMBUS_WORD_DATA ? 4 : 2, oldvalue, |
|---|
| 267 | size == I2C_SMBUS_WORD_DATA ? 4 : 2, vmask, |
|---|
| 268 | size == I2C_SMBUS_WORD_DATA ? 4 : 2, value, |
|---|
| 269 | daddress); |
|---|
| 270 | |
|---|
| 271 | fprintf(stderr, "Continue? [Y/n] "); |
|---|
| 272 | fflush(stderr); |
|---|
| 273 | if (!user_ack(1)) { |
|---|
| 274 | fprintf(stderr, "Aborting on user request.\n"); |
|---|
| 275 | exit(0); |
|---|
| 276 | } |
|---|
| 277 | } |
|---|
| 278 | } |
|---|
| 279 | |
|---|
| 280 | if (pec && ioctl(file, I2C_PEC, 1) < 0) { |
|---|
| 281 | fprintf(stderr, "Error: Could not set PEC: %s\n", |
|---|
| 282 | strerror(errno)); |
|---|
| 283 | close(file); |
|---|
| 284 | exit(1); |
|---|
| 285 | } |
|---|
| 286 | |
|---|
| 287 | switch (size) { |
|---|
| 288 | case I2C_SMBUS_BYTE: |
|---|
| 289 | res = i2c_smbus_write_byte(file, daddress); |
|---|
| 290 | break; |
|---|
| 291 | case I2C_SMBUS_WORD_DATA: |
|---|
| 292 | res = i2c_smbus_write_word_data(file, daddress, value); |
|---|
| 293 | break; |
|---|
| 294 | default: /* I2C_SMBUS_BYTE_DATA */ |
|---|
| 295 | res = i2c_smbus_write_byte_data(file, daddress, value); |
|---|
| 296 | } |
|---|
| 297 | if (res < 0) { |
|---|
| 298 | fprintf(stderr, "Error: Write failed\n"); |
|---|
| 299 | close(file); |
|---|
| 300 | exit(1); |
|---|
| 301 | } |
|---|
| 302 | |
|---|
| 303 | if (pec) { |
|---|
| 304 | if (ioctl(file, I2C_PEC, 0) < 0) { |
|---|
| 305 | fprintf(stderr, "Error: Could not clear PEC: %s\n", |
|---|
| 306 | strerror(errno)); |
|---|
| 307 | close(file); |
|---|
| 308 | exit(1); |
|---|
| 309 | } |
|---|
| 310 | } |
|---|
| 311 | |
|---|
| 312 | if (!readback) { /* We're done */ |
|---|
| 313 | close(file); |
|---|
| 314 | exit(0); |
|---|
| 315 | } |
|---|
| 316 | |
|---|
| 317 | switch (size) { |
|---|
| 318 | case I2C_SMBUS_BYTE: |
|---|
| 319 | res = i2c_smbus_read_byte(file); |
|---|
| 320 | value = daddress; |
|---|
| 321 | break; |
|---|
| 322 | case I2C_SMBUS_WORD_DATA: |
|---|
| 323 | res = i2c_smbus_read_word_data(file, daddress); |
|---|
| 324 | break; |
|---|
| 325 | default: /* I2C_SMBUS_BYTE_DATA */ |
|---|
| 326 | res = i2c_smbus_read_byte_data(file, daddress); |
|---|
| 327 | } |
|---|
| 328 | close(file); |
|---|
| 329 | |
|---|
| 330 | if (res < 0) { |
|---|
| 331 | printf("Warning - readback failed\n"); |
|---|
| 332 | } else |
|---|
| 333 | if (res != value) { |
|---|
| 334 | printf("Warning - data mismatch - wrote " |
|---|
| 335 | "0x%0*x, read back 0x%0*x\n", |
|---|
| 336 | size == I2C_SMBUS_WORD_DATA ? 4 : 2, value, |
|---|
| 337 | size == I2C_SMBUS_WORD_DATA ? 4 : 2, res); |
|---|
| 338 | } else { |
|---|
| 339 | printf("Value 0x%0*x written, readback matched\n", |
|---|
| 340 | size == I2C_SMBUS_WORD_DATA ? 4 : 2, value); |
|---|
| 341 | } |
|---|
| 342 | |
|---|
| 343 | exit(0); |
|---|
| 344 | } |
|---|