root/lm-sensors/trunk/prog/dump/i2cset.c @ 4256

Revision 4256, 7.4 KB (checked in by khali, 6 years ago)

prog/dump/*: More robust user input handling.

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