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

Revision 5885, 8.5 KB (checked in by khali, 2 years ago)

Use a 20-bit limit for the i2c bus number.
Use snprintf for the i2c dev node name.
Update copyright years.

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