root/i2c-tools/trunk/tools/i2cset.c

Revision 5927, 10.8 KB (checked in by groeck, 12 months ago)

i2cset: Check range for data value mask

  • 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-2010  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
33static void help(void) __attribute__ ((noreturn));
34
35static 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                "    c (byte, no value)\n"
43                "    b (byte data, default)\n"
44                "    w (word data)\n"
45                "    i (I2C block data)\n"
46                "    s (SMBus block data)\n"
47                "    Append p for SMBus PEC\n");
48        exit(1);
49}
50
51static int check_funcs(int file, int size, int pec)
52{
53        unsigned long funcs;
54
55        /* check adapter functionality */
56        if (ioctl(file, I2C_FUNCS, &funcs) < 0) {
57                fprintf(stderr, "Error: Could not get the adapter "
58                        "functionality matrix: %s\n", strerror(errno));
59                return -1;
60        }
61
62        switch (size) {
63        case I2C_SMBUS_BYTE:
64                if (!(funcs & I2C_FUNC_SMBUS_WRITE_BYTE)) {
65                        fprintf(stderr, MISSING_FUNC_FMT, "SMBus send byte");
66                        return -1;
67                }
68                break;
69
70        case I2C_SMBUS_BYTE_DATA:
71                if (!(funcs & I2C_FUNC_SMBUS_WRITE_BYTE_DATA)) {
72                        fprintf(stderr, MISSING_FUNC_FMT, "SMBus write byte");
73                        return -1;
74                }
75                break;
76
77        case I2C_SMBUS_WORD_DATA:
78                if (!(funcs & I2C_FUNC_SMBUS_WRITE_WORD_DATA)) {
79                        fprintf(stderr, MISSING_FUNC_FMT, "SMBus write word");
80                        return -1;
81                }
82                break;
83
84        case I2C_SMBUS_BLOCK_DATA:
85                if (!(funcs & I2C_FUNC_SMBUS_WRITE_BLOCK_DATA)) {
86                        fprintf(stderr, MISSING_FUNC_FMT, "SMBus block write");
87                        return -1;
88                }
89                break;
90        case I2C_SMBUS_I2C_BLOCK_DATA:
91                if (!(funcs & I2C_FUNC_SMBUS_WRITE_I2C_BLOCK)) {
92                        fprintf(stderr, MISSING_FUNC_FMT, "I2C block write");
93                        return -1;
94                }
95                break;
96        }
97
98        if (pec
99         && !(funcs & (I2C_FUNC_SMBUS_PEC | I2C_FUNC_I2C))) {
100                fprintf(stderr, "Warning: Adapter does "
101                        "not seem to support PEC\n");
102        }
103
104        return 0;
105}
106
107static int confirm(const char *filename, int address, int size, int daddress,
108                   int value, int vmask, const unsigned char *block, int len,
109                   int pec)
110{
111        int dont = 0;
112
113        fprintf(stderr, "WARNING! This program can confuse your I2C "
114                "bus, cause data loss and worse!\n");
115
116        if (address >= 0x50 && address <= 0x57) {
117                fprintf(stderr, "DANGEROUS! Writing to a serial "
118                        "EEPROM on a memory DIMM\nmay render your "
119                        "memory USELESS and make your system "
120                        "UNBOOTABLE!\n");
121                dont++;
122        }
123
124        fprintf(stderr, "I will write to device file %s, chip address "
125                "0x%02x, data address\n0x%02x, ", filename, address, daddress);
126        if (size == I2C_SMBUS_BYTE)
127                fprintf(stderr, "no data.\n");
128        else if (size == I2C_SMBUS_BLOCK_DATA ||
129                 size == I2C_SMBUS_I2C_BLOCK_DATA) {
130                int i;
131
132                fprintf(stderr, "data");
133                for (i = 0; i < len; i++)
134                        fprintf(stderr, " 0x%02x", block[i]);
135                fprintf(stderr, ", mode %s.\n", size == I2C_SMBUS_BLOCK_DATA
136                        ? "smbus block" : "i2c block");
137        } else
138                fprintf(stderr, "data 0x%02x%s, mode %s.\n", value,
139                        vmask ? " (masked)" : "",
140                        size == I2C_SMBUS_BYTE_DATA ? "byte" : "word");
141        if (pec)
142                fprintf(stderr, "PEC checking enabled.\n");
143
144        fprintf(stderr, "Continue? [%s] ", dont ? "y/N" : "Y/n");
145        fflush(stderr);
146        if (!user_ack(!dont)) {
147                fprintf(stderr, "Aborting on user request.\n");
148                return 0;
149        }
150
151        return 1;
152}
153
154int main(int argc, char *argv[])
155{
156        char *end;
157        const char *maskp = NULL;
158        int res, i2cbus, address, size, file;
159        int value, daddress, vmask = 0;
160        char filename[20];
161        int pec = 0;
162        int flags = 0;
163        int force = 0, yes = 0, version = 0, readback = 0;
164        unsigned char block[I2C_SMBUS_BLOCK_MAX];
165        int len;
166
167        /* handle (optional) flags first */
168        while (1+flags < argc && argv[1+flags][0] == '-') {
169                switch (argv[1+flags][1]) {
170                case 'V': version = 1; break;
171                case 'f': force = 1; break;
172                case 'y': yes = 1; break;
173                case 'm':
174                        if (2+flags < argc)
175                                maskp = argv[2+flags];
176                        flags++;
177                        break;
178                case 'r': readback = 1; break;
179                default:
180                        fprintf(stderr, "Error: Unsupported option "
181                                "\"%s\"!\n", argv[1+flags]);
182                        help();
183                        exit(1);
184                }
185                flags++;
186        }
187
188        if (version) {
189                fprintf(stderr, "i2cset version %s\n", VERSION);
190                exit(0);
191        }
192
193        if (argc < flags + 4)
194                help();
195
196        i2cbus = lookup_i2c_bus(argv[flags+1]);
197        if (i2cbus < 0)
198                help();
199
200        address = parse_i2c_address(argv[flags+2]);
201        if (address < 0)
202                help();
203
204        daddress = strtol(argv[flags+3], &end, 0);
205        if (*end || daddress < 0 || daddress > 0xff) {
206                fprintf(stderr, "Error: Data address invalid!\n");
207                help();
208        }
209
210        /* check for command/mode */
211        if (argc == flags + 4) {
212                /* Implicit "c" */
213                size = I2C_SMBUS_BYTE;
214        } else if (argc == flags + 5) {
215                /* "c", "cp",  or implicit "b" */
216                if (!strcmp(argv[flags+4], "c")
217                 || !strcmp(argv[flags+4], "cp")) {
218                        size = I2C_SMBUS_BYTE;
219                        pec = argv[flags+4][1] == 'p';
220                } else {
221                        size = I2C_SMBUS_BYTE_DATA;
222                }
223        } else {
224                /* All other commands */
225                if (strlen(argv[argc-1]) > 2
226                    || (strlen(argv[argc-1]) == 2 && argv[argc-1][1] != 'p')) {
227                        fprintf(stderr, "Error: Invalid mode '%s'!\n", argv[argc-1]);
228                        help();
229                }
230                switch (argv[argc-1][0]) {
231                case 'b': size = I2C_SMBUS_BYTE_DATA; break;
232                case 'w': size = I2C_SMBUS_WORD_DATA; break;
233                case 's': size = I2C_SMBUS_BLOCK_DATA; break;
234                case 'i': size = I2C_SMBUS_I2C_BLOCK_DATA; break;
235                default:
236                        fprintf(stderr, "Error: Invalid mode '%s'!\n", argv[argc-1]);
237                        help();
238                }
239                pec = argv[argc-1][1] == 'p';
240                if (size == I2C_SMBUS_BLOCK_DATA || size == I2C_SMBUS_I2C_BLOCK_DATA) {
241                        if (pec && size == I2C_SMBUS_I2C_BLOCK_DATA) {
242                                fprintf(stderr, "Error: PEC not supported for I2C block writes!\n");
243                                help();
244                        }
245                        if (maskp) {
246                                fprintf(stderr, "Error: Mask not supported for block writes!\n");
247                                help();
248                        }
249                        if (argc > (int)sizeof(block) + flags + 5) {
250                                fprintf(stderr, "Error: Too many arguments!\n");
251                                help();
252                        }
253                } else if (argc != flags + 6) {
254                        fprintf(stderr, "Error: Too many arguments!\n");
255                        help();
256                }
257        }
258
259        len = 0; /* Must always initialize len since it is passed to confirm() */
260
261        /* read values from command line */
262        switch (size) {
263        case I2C_SMBUS_BYTE_DATA:
264        case I2C_SMBUS_WORD_DATA:
265                value = strtol(argv[flags+4], &end, 0);
266                if (*end || value < 0) {
267                        fprintf(stderr, "Error: Data value invalid!\n");
268                        help();
269                }
270                if ((size == I2C_SMBUS_BYTE_DATA && value > 0xff)
271                    || (size == I2C_SMBUS_WORD_DATA && value > 0xffff)) {
272                        fprintf(stderr, "Error: Data value out of range!\n");
273                        help();
274                }
275                break;
276        case I2C_SMBUS_BLOCK_DATA:
277        case I2C_SMBUS_I2C_BLOCK_DATA:
278                for (len = 0; len + flags + 5 < argc; len++) {
279                        value = strtol(argv[flags + len + 4], &end, 0);
280                        if (*end || value < 0) {
281                                fprintf(stderr, "Error: Data value invalid!\n");
282                                help();
283                        }
284                        if (value > 0xff) {
285                                fprintf(stderr, "Error: Data value out of range!\n");
286                                help();
287                        }
288                        block[len] = value;
289                }
290                value = -1;
291                break;
292        default:
293                value = -1;
294                break;
295        }
296
297        if (maskp) {
298                vmask = strtol(maskp, &end, 0);
299                if (*end || vmask == 0) {
300                        fprintf(stderr, "Error: Data value mask invalid!\n");
301                        help();
302                }
303                if (((size == I2C_SMBUS_BYTE || size == I2C_SMBUS_BYTE_DATA)
304                     && vmask > 0xff) || vmask > 0xffff) {
305                        fprintf(stderr, "Error: Data value mask out of range!\n");
306                        help();
307                }
308        }
309
310        file = open_i2c_dev(i2cbus, filename, sizeof(filename), 0);
311        if (file < 0
312         || check_funcs(file, size, pec)
313         || set_slave_addr(file, address, force))
314                exit(1);
315
316        if (!yes && !confirm(filename, address, size, daddress,
317                             value, vmask, block, len, pec))
318                exit(0);
319
320        if (vmask) {
321                int oldvalue;
322
323                switch (size) {
324                case I2C_SMBUS_BYTE:
325                        oldvalue = i2c_smbus_read_byte(file);
326                        break;
327                case I2C_SMBUS_WORD_DATA:
328                        oldvalue = i2c_smbus_read_word_data(file, daddress);
329                        break;
330                default:
331                        oldvalue = i2c_smbus_read_byte_data(file, daddress);
332                }
333
334                if (oldvalue < 0) {
335                        fprintf(stderr, "Error: Failed to read old value\n");
336                        exit(1);
337                }
338
339                value = (value & vmask) | (oldvalue & ~vmask);
340
341                if (!yes) {
342                        fprintf(stderr, "Old value 0x%0*x, write mask "
343                                "0x%0*x: Will write 0x%0*x to register "
344                                "0x%02x\n",
345                                size == I2C_SMBUS_WORD_DATA ? 4 : 2, oldvalue,
346                                size == I2C_SMBUS_WORD_DATA ? 4 : 2, vmask,
347                                size == I2C_SMBUS_WORD_DATA ? 4 : 2, value,
348                                daddress);
349
350                        fprintf(stderr, "Continue? [Y/n] ");
351                        fflush(stderr);
352                        if (!user_ack(1)) {
353                                fprintf(stderr, "Aborting on user request.\n");
354                                exit(0);
355                        }
356                }
357        }
358
359        if (pec && ioctl(file, I2C_PEC, 1) < 0) {
360                fprintf(stderr, "Error: Could not set PEC: %s\n",
361                        strerror(errno));
362                close(file);
363                exit(1);
364        }
365
366        switch (size) {
367        case I2C_SMBUS_BYTE:
368                res = i2c_smbus_write_byte(file, daddress);
369                break;
370        case I2C_SMBUS_WORD_DATA:
371                res = i2c_smbus_write_word_data(file, daddress, value);
372                break;
373        case I2C_SMBUS_BLOCK_DATA:
374                res = i2c_smbus_write_block_data(file, daddress, len, block);
375                break;
376        case I2C_SMBUS_I2C_BLOCK_DATA:
377                res = i2c_smbus_write_i2c_block_data(file, daddress, len, block);
378                break;
379        default: /* I2C_SMBUS_BYTE_DATA */
380                res = i2c_smbus_write_byte_data(file, daddress, value);
381                break;
382        }
383        if (res < 0) {
384                fprintf(stderr, "Error: Write failed\n");
385                close(file);
386                exit(1);
387        }
388
389        if (pec) {
390                if (ioctl(file, I2C_PEC, 0) < 0) {
391                        fprintf(stderr, "Error: Could not clear PEC: %s\n",
392                                strerror(errno));
393                        close(file);
394                        exit(1);
395                }
396        }
397
398        if (!readback) { /* We're done */
399                close(file);
400                exit(0);
401        }
402
403        switch (size) {
404        case I2C_SMBUS_BYTE:
405                res = i2c_smbus_read_byte(file);
406                value = daddress;
407                break;
408        case I2C_SMBUS_WORD_DATA:
409                res = i2c_smbus_read_word_data(file, daddress);
410                break;
411        default: /* I2C_SMBUS_BYTE_DATA */
412                res = i2c_smbus_read_byte_data(file, daddress);
413        }
414        close(file);
415
416        if (res < 0) {
417                printf("Warning - readback failed\n");
418        } else
419        if (res != value) {
420                printf("Warning - data mismatch - wrote "
421                       "0x%0*x, read back 0x%0*x\n",
422                       size == I2C_SMBUS_WORD_DATA ? 4 : 2, value,
423                       size == I2C_SMBUS_WORD_DATA ? 4 : 2, res);
424        } else {
425                printf("Value 0x%0*x written, readback matched\n",
426                       size == I2C_SMBUS_WORD_DATA ? 4 : 2, value);
427        }
428
429        exit(0);
430}
Note: See TracBrowser for help on using the browser.