root/lm-sensors/trunk/prog/dump/i2cdump.c @ 4080

Revision 4080, 10.8 KB (checked in by khali, 7 years ago)

prog/dump/*.c, prog/detect/*.c: Handle possible error in fgets().

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/*
2    i2cdump.c - a user-space program to dump I2C registers
3    Copyright (C) 2002-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
31void help(void)
32{
33        fprintf(stderr, "Syntax: i2cdump [-y] I2CBUS ADDRESS [MODE] [BANK "
34                "[BANKREG]]\n"
35                "        i2cdump -V\n"
36                "  MODE is one of:\n"
37                "    b (byte, default)\n"
38                "    w (word)\n"
39                "    W (word on even register addresses)\n"
40                "    s (SMBus block)\n"
41                "    i (I2C block)\n"
42                "    c (consecutive byte)\n"
43                "    Append 'p' to 'b', 'w', 's' or 'c' for PEC checking\n"
44                "  I2CBUS is an integer\n"
45                "  ADDRESS is an integer 0x00 - 0x7f\n"
46                "  BANK and BANKREG are for byte and word accesses (default "
47                "bank 0, reg 0x4e)\n"
48                "  BANK is the command for smbusblock accesses (default 0)\n");
49        print_i2c_busses(0);
50}
51
52int main(int argc, char *argv[])
53{
54        char *end;
55        int i, j, res, i2cbus, address, size, file;
56        int bank = 0, bankreg = 0x4E, old_bank = 0;
57        char filename[20];
58        long funcs;
59        int block[256];
60        int pec = 0, even = 0;
61        int flags = 0;
62        int yes = 0, version = 0;
63
64        /* handle (optional) flags first */
65        while (1+flags < argc && argv[1+flags][0] == '-') {
66                switch (argv[1+flags][1]) {
67                case 'V': version = 1; break;
68                case 'y': yes = 1; break;
69                default:
70                        fprintf(stderr, "Warning: Unsupported flag "
71                                "\"-%c\"!\n", argv[1+flags][1]);
72                        help();
73                        exit(1);
74                }
75                flags++;
76        }
77
78        if (version) {
79                fprintf(stderr, "i2cdump version %s\n", LM_VERSION);
80                exit(0);
81        }
82
83        if (argc < flags + 2) {
84                fprintf(stderr, "Error: No i2c-bus specified!\n");
85                help();
86                exit(1);
87        }
88        i2cbus = strtol(argv[flags+1], &end, 0);
89        if (*end) {
90                fprintf(stderr, "Error: First argument not a number!\n");
91                help();
92                exit(1);
93        }
94        if (i2cbus < 0 || i2cbus > 0xff) {
95                fprintf(stderr, "Error: I2CBUS argument out of range!\n");
96                help();
97                exit(1);
98        }
99
100        if (argc < flags + 3) {
101                fprintf(stderr, "Error: No address specified!\n");
102                help();
103                exit(1);
104        }
105        address = strtol(argv[flags+2], &end, 0);
106        if (*end) {
107                fprintf(stderr, "Error: Second argument not a number!\n");
108                help();
109                exit(1);
110        }
111        if (address < 0 || address > 0x7f) {
112                fprintf(stderr, "Error: Address out of range!\n");
113                help();
114                exit(1);
115        }
116
117        if (argc < flags + 4) {
118                fprintf(stderr, "No size specified (using byte-data access)\n");
119                size = I2C_SMBUS_BYTE_DATA;
120        } else if (!strncmp(argv[flags+3], "b", 1)) {
121                size = I2C_SMBUS_BYTE_DATA;
122                pec = argv[flags+3][1] == 'p';
123        } else if (!strncmp(argv[flags+3], "w", 1)) {
124                size = I2C_SMBUS_WORD_DATA;
125                pec = argv[flags+3][1] == 'p';
126        } else if (!strncmp(argv[flags+3], "W", 1)) {
127                size = I2C_SMBUS_WORD_DATA;
128                even = 1;
129        } else if (!strncmp(argv[flags+3], "s", 1)) {
130                size = I2C_SMBUS_BLOCK_DATA;
131                pec = argv[flags+3][1] == 'p';
132        } else if (!strncmp(argv[flags+3], "c", 1)) {
133                size = I2C_SMBUS_BYTE;
134                pec = argv[flags+3][1] == 'p';
135        } else if (!strcmp(argv[flags+3], "i"))
136                size = I2C_SMBUS_I2C_BLOCK_DATA;
137        else {
138                fprintf(stderr, "Error: Invalid mode!\n");
139                help();
140                exit(1);
141        }
142
143        if (argc > flags + 4) {
144                bank = strtol(argv[flags+4], &end, 0);
145                if (*end || size == I2C_SMBUS_I2C_BLOCK_DATA) {
146                        fprintf(stderr, "Error: Invalid bank number!\n");
147                        help();
148                        exit(1);
149                }
150                if ((size == I2C_SMBUS_BYTE_DATA || size == I2C_SMBUS_WORD_DATA)
151                 && (bank < 0 || bank > 15)) {
152                        fprintf(stderr, "Error: bank out of range!\n");
153                        help();
154                        exit(1);
155                }
156                if (size == I2C_SMBUS_BLOCK_DATA
157                 && (bank < 0 || bank > 0xff)) {
158                        fprintf(stderr, "Error: block command out of range!\n");
159                        help();
160                        exit(1);
161                }
162
163                if (argc > flags + 5) {
164                        bankreg = strtol(argv[flags+5], &end, 0);
165                        if (*end || size == I2C_SMBUS_BLOCK_DATA) {
166                                fprintf(stderr, "Error: Invalid bank register "
167                                        "number!\n");
168                                help();
169                                exit(1);
170                        }
171                        if (bankreg < 0 || bankreg > 0xff) {
172                                fprintf(stderr, "Error: bank out of range "
173                                        "(0-0xff)!\n");
174                                help();
175                                exit(1);
176                        }
177                }
178        }
179
180        file = open_i2c_dev(i2cbus, filename, 0);
181        if (file < 0) {
182                exit(1);
183        }
184
185        /* check adapter functionality */
186        if (ioctl(file, I2C_FUNCS, &funcs) < 0) {
187                fprintf(stderr, "Error: Could not get the adapter "
188                        "functionality matrix: %s\n", strerror(errno));
189                exit(1);
190        }
191
192        switch(size) {
193        case I2C_SMBUS_BYTE:
194                if (!((funcs & I2C_FUNC_SMBUS_BYTE) == I2C_FUNC_SMBUS_BYTE)) {
195                        fprintf(stderr, "Error: Adapter for i2c bus %d does "
196                                "not have byte capability\n", i2cbus);
197                        exit(1);
198                }
199                break;
200
201        case I2C_SMBUS_BYTE_DATA:
202                if (!(funcs & I2C_FUNC_SMBUS_READ_BYTE_DATA)) {
203                        fprintf(stderr, "Error: Adapter for i2c bus %d does "
204                                "not have byte read capability\n", i2cbus);
205                        exit(1);
206                }
207                break;
208
209        case I2C_SMBUS_WORD_DATA:
210                if (!(funcs & I2C_FUNC_SMBUS_READ_WORD_DATA)) {
211                        fprintf(stderr, "Error: Adapter for i2c bus %d does "
212                                "not have word read capability\n", i2cbus);
213                        exit(1);
214                }
215                break;
216
217        case I2C_SMBUS_BLOCK_DATA:
218                if (!(funcs & I2C_FUNC_SMBUS_READ_BLOCK_DATA)) {
219                        fprintf(stderr, "Error: Adapter for i2c bus %d does "
220                                "not have smbus block read capability\n",
221                                i2cbus);
222                        exit(1);
223                }
224                break;
225
226        case I2C_SMBUS_I2C_BLOCK_DATA:
227                if (!(funcs & I2C_FUNC_SMBUS_READ_I2C_BLOCK)) {
228                        fprintf(stderr, "Error: Adapter for i2c bus %d does "
229                                "not have i2c block read capability\n",
230                                i2cbus);
231                        exit(1);
232                }
233                break;
234        }
235
236        /* use FORCE so that we can look at registers even when
237           a driver is also running */
238        if (ioctl(file, I2C_SLAVE_FORCE, address) < 0) {
239                fprintf(stderr, "Error: Could not set address to %d: %s\n",
240                        address, strerror(errno));
241                exit(1);
242        }
243
244        if (pec) {
245                if (ioctl(file, I2C_PEC, 1) < 0) {
246                        fprintf(stderr, "Error: Could not set PEC: %s\n",
247                                strerror(errno));
248                        exit(1);
249                }
250                if (!(funcs & (I2C_FUNC_SMBUS_HWPEC_CALC | I2C_FUNC_I2C))) {
251                        fprintf(stderr, "Warning: Adapter for i2c bus %d does "
252                                "not seem to actually support PEC\n", i2cbus);
253                }
254        }
255
256        if (!yes) {
257                char s[2];
258
259                fprintf(stderr, "WARNING! This program can confuse your I2C "
260                        "bus, cause data loss and worse!\n");
261
262                fprintf(stderr, "I will probe file %s, address 0x%x, mode "
263                        "%s\n", filename, address,
264                        size == I2C_SMBUS_BLOCK_DATA ? "smbus block" :
265                        size == I2C_SMBUS_I2C_BLOCK_DATA ? "i2c block" :
266                        size == I2C_SMBUS_BYTE ? "byte consecutive read" :
267                        size == I2C_SMBUS_BYTE_DATA ? "byte" : "word");
268                if (pec)
269                        fprintf(stderr, "PEC checking enabled.\n");
270                if (even)
271                        fprintf(stderr, "Only probing even register "
272                                "addresses.\n");
273                if (bank) {
274                        if (size == I2C_SMBUS_BLOCK_DATA)
275                                fprintf(stderr, "Using command 0x%02x.\n",
276                                        bank);
277                        else
278                                fprintf(stderr, "Probing bank %d using bank "
279                                        "register 0x%02x.\n", bank, bankreg);
280                }
281
282                fprintf(stderr, "Continue? [Y/n] ");
283                fflush(stderr);
284                if (!fgets(s, 2, stdin)
285                 || (s[0] != '\n' && s[0] != 'y' && s[0] != 'Y')) {
286                        fprintf(stderr, "Aborting on user request.\n");
287                        exit(0);
288                }
289        }
290
291        /* See Winbond w83781d data sheet for bank details */
292        if (bank && size != I2C_SMBUS_BLOCK_DATA) {
293                old_bank = i2c_smbus_read_byte_data(file, bankreg);
294                if (old_bank >= 0)
295                        res = i2c_smbus_write_byte_data(file, bankreg,
296                                bank | (old_bank & 0xf0));
297                if (old_bank < 0 || res < 0) {
298                        fprintf(stderr, "Error: Bank switching failed\n");
299                        exit(1);
300                }
301        }
302
303        /* handle all but word data */
304        if (size != I2C_SMBUS_WORD_DATA || even) {
305                /* do the block transaction */
306                if (size == I2C_SMBUS_BLOCK_DATA
307                 || size == I2C_SMBUS_I2C_BLOCK_DATA) {
308                        unsigned char cblock[288];
309
310                        if (size == I2C_SMBUS_BLOCK_DATA) {
311                                res = i2c_smbus_read_block_data(file, bank,
312                                      cblock);
313                        } else {
314                                for (res = 0; res < 256; res += i) {
315                                        i = i2c_smbus_read_i2c_block_data(file,
316                                                res, cblock + res);
317                                        if (i <= 0)
318                                                break;
319                                }
320                        }
321                        if (res <= 0) {
322                                fprintf(stderr, "Error: Block read failed, "
323                                        "return code %d\n", res);
324                                exit(1);
325                        }
326                        if (res >= 256)
327                                res = 256;
328                        for (i = 0; i < res; i++)
329                                block[i] = cblock[i];
330                        for (i = res; i < 256; i++)
331                                block[i] = -1;
332                }
333
334                if (size == I2C_SMBUS_BYTE) {
335                        res = i2c_smbus_write_byte(file, 0);
336                        if(res != 0) {
337                                fprintf(stderr, "Error: Write start address "
338                                        "failed, return code %d\n", res);
339                                exit(1);
340                        }
341                }
342
343                printf("     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f"
344                       "    0123456789abcdef\n");
345                for (i = 0; i < 256; i+=16) {
346                        printf("%02x: ", i);
347                        for (j = 0; j < 16; j++) {
348                                if (size == I2C_SMBUS_BYTE_DATA) {
349                                        block[i+j] = res =
350                                          i2c_smbus_read_byte_data(file, i+j);
351                                } else if (size == I2C_SMBUS_WORD_DATA) {
352                                        res = i2c_smbus_read_word_data(file,
353                                                                       i+j);
354                                        if (res < 0) {
355                                                block[i+j] = res;
356                                                block[i+j+1] = res;
357                                        } else {
358                                                block[i+j] = res & 0xff;
359                                                block[i+j+1] = res >> 8;
360                                        }
361                                } else if (size == I2C_SMBUS_BYTE) {
362                                        block[i+j] = res =
363                                          i2c_smbus_read_byte(file);
364                                } else
365                                        res = block[i+j];
366
367                                if (res < 0) {
368                                        printf("XX ");
369                                        if (size == I2C_SMBUS_WORD_DATA)
370                                                printf("XX ");
371                                } else {
372                                        printf("%02x ", block[i+j]);
373                                        if (size == I2C_SMBUS_WORD_DATA)
374                                                printf("%02x ", block[i+j+1]);
375                                }
376                                if (size == I2C_SMBUS_WORD_DATA)
377                                        j++;
378                        }
379                        printf("   ");
380                        for (j = 0; j < 16; j++) {
381                                res = block[i+j];
382                                if (res < 0)
383                                        printf("X");
384                                else
385                                if ((res & 0xff) == 0x00
386                                 || (res & 0xff) == 0xff)
387                                        printf(".");
388                                else
389                                if ((res & 0xff) < 32
390                                 || (res & 0xff) >= 127)
391                                        printf("?");
392                                else
393                                        printf("%c", res & 0xff);
394                        }
395                        printf("\n");
396                        if (size == I2C_SMBUS_BLOCK_DATA && i == 16)
397                                break;
398                }
399        } else {
400                printf("     0,8  1,9  2,a  3,b  4,c  5,d  6,e  7,f\n");
401                for (i = 0; i < 256; i+=8) {
402                        printf("%02x: ", i);
403                        for (j = 0; j < 8; j++) {
404                                res = i2c_smbus_read_word_data(file, i+j);
405                                if (res < 0)
406                                        printf("XXXX ");
407                                else
408                                        printf("%04x ", res & 0xffff);
409                        }
410                        printf("\n");
411                }
412        }
413        if (bank && size != I2C_SMBUS_BLOCK_DATA) {
414                i2c_smbus_write_byte_data(file, bankreg, old_bank);
415        }
416        exit(0);
417}
Note: See TracBrowser for help on using the browser.