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

Revision 5242, 6.4 kB (checked in by khali, 7 months ago)

Use consistent transaction names (based on the SMBus specification)
when complaining about a missing adapter functionality.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 /*
2     i2cget.c - A user-space program to read an I2C register.
3     Copyright (C) 2005-2008  Jean Delvare <khali@linux-fr.org>
4
5     Based on i2cset.c:
6     Copyright (C) 2001-2003  Frodo Looijaard <frodol@dds.nl>, and
7                              Mark D. Studebaker <mdsxyz123@yahoo.com>
8     Copyright (C) 2004-2005  Jean Delvare <khali@linux-fr.org>
9
10     This program is free software; you can redistribute it and/or modify
11     it under the terms of the GNU General Public License as published by
12     the Free Software Foundation; either version 2 of the License, or
13     (at your option) any later version.
14
15     This program is distributed in the hope that it will be useful,
16     but WITHOUT ANY WARRANTY; without even the implied warranty of
17     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18     GNU General Public License for more details.
19
20     You should have received a copy of the GNU General Public License
21     along with this program; if not, write to the Free Software
22     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
23     MA 02110-1301 USA.
24 */
25
26 #include <errno.h>
27 #include <string.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <unistd.h>
31 #include <linux/i2c-dev.h>
32 #include "i2cbusses.h"
33 #include "util.h"
34 #include "../version.h"
35
36 static void help(void) __attribute__ ((noreturn));
37
38 static void help(void)
39 {
40         fprintf(stderr,
41                 "Usage: i2cget [-f] [-y] I2CBUS CHIP-ADDRESS [DATA-ADDRESS [MODE]]\n"
42                 "  I2CBUS is an integer or an I2C bus name\n"
43                 "  ADDRESS is an integer (0x03 - 0x77)\n"
44                 "  MODE is one of:\n"
45                 "    b (read byte data, default)\n"
46                 "    w (read word data)\n"
47                 "    c (write byte/read byte)\n"
48                 "    Append p for SMBus PEC\n");
49         exit(1);
50 }
51
52 static int check_funcs(int file, int size, int daddress, int pec)
53 {
54         unsigned long funcs;
55
56         /* check adapter functionality */
57         if (ioctl(file, I2C_FUNCS, &funcs) < 0) {
58                 fprintf(stderr, "Error: Could not get the adapter "
59                         "functionality matrix: %s\n", strerror(errno));
60                 return -1;
61         }
62
63         switch (size) {
64         case I2C_SMBUS_BYTE:
65                 if (!(funcs & I2C_FUNC_SMBUS_READ_BYTE)) {
66                         fprintf(stderr, MISSING_FUNC_FMT, "SMBus receive byte");
67                         return -1;
68                 }
69                 if (daddress >= 0
70                  && !(funcs & I2C_FUNC_SMBUS_WRITE_BYTE)) {
71                         fprintf(stderr, MISSING_FUNC_FMT, "SMBus send byte");
72                         return -1;
73                 }
74                 break;
75
76         case I2C_SMBUS_BYTE_DATA:
77                 if (!(funcs & I2C_FUNC_SMBUS_READ_BYTE_DATA)) {
78                         fprintf(stderr, MISSING_FUNC_FMT, "SMBus read byte");
79                         return -1;
80                 }
81                 break;
82
83         case I2C_SMBUS_WORD_DATA:
84                 if (!(funcs & I2C_FUNC_SMBUS_READ_WORD_DATA)) {
85                         fprintf(stderr, MISSING_FUNC_FMT, "SMBus read word");
86                         return -1;
87                 }
88                 break;
89         }
90
91         if (pec
92          && !(funcs & (I2C_FUNC_SMBUS_PEC | I2C_FUNC_I2C))) {
93                 fprintf(stderr, "Warning: Adapter does "
94                         "not seem to support PEC\n");
95         }
96
97         return 0;
98 }
99
100 static int confirm(const char *filename, int address, int size, int daddress,
101                    int pec)
102 {
103         int dont = 0;
104
105         fprintf(stderr, "WARNING! This program can confuse your I2C "
106                 "bus, cause data loss and worse!\n");
107
108         /* Don't let the user break his/her EEPROMs */
109         if (address >= 0x50 && address <= 0x57 && pec) {
110                 fprintf(stderr, "STOP! EEPROMs are I2C devices, not "
111                         "SMBus devices. Using PEC\non I2C devices may "
112                         "result in unexpected results, such as\n"
113                         "trashing the contents of EEPROMs. We can't "
114                         "let you do that, sorry.\n");
115                 return 0;
116         }
117
118         if (size == I2C_SMBUS_BYTE && daddress >= 0 && pec) {
119                 fprintf(stderr, "WARNING! All I2C chips and some SMBus chips "
120                         "will interpret a write\nbyte command with PEC as a"
121                         "write byte data command, effectively writing a\n"
122                         "value into a register!\n");
123                 dont++;
124         }
125
126         fprintf(stderr, "I will read from device file %s, chip "
127                 "address 0x%02x, ", filename, address);
128         if (daddress < 0)
129                 fprintf(stderr, "current data\naddress");
130         else
131                 fprintf(stderr, "data address\n0x%02x", daddress);
132         fprintf(stderr, ", using %s.\n",
133                 size == I2C_SMBUS_BYTE ? (daddress < 0 ?
134                 "read byte" : "write byte/read byte") :
135                 size == I2C_SMBUS_BYTE_DATA ? "read byte data" :
136                 "read word data");
137         if (pec)
138                 fprintf(stderr, "PEC checking enabled.\n");
139
140         fprintf(stderr, "Continue? [%s] ", dont ? "y/N" : "Y/n");
141         fflush(stderr);
142         if (!user_ack(!dont)) {
143                 fprintf(stderr, "Aborting on user request.\n");
144                 return 0;
145         }
146
147         return 1;
148 }
149
150 int main(int argc, char *argv[])
151 {
152         char *end;
153         int res, i2cbus, address, size, file;
154         int daddress;
155         char filename[20];
156         int pec = 0;
157         int flags = 0;
158         int force = 0, yes = 0, version = 0;
159
160         /* handle (optional) flags first */
161         while (1+flags < argc && argv[1+flags][0] == '-') {
162                 switch (argv[1+flags][1]) {
163                 case 'V': version = 1; break;
164                 case 'f': force = 1; break;
165                 case 'y': yes = 1; break;
166                 default:
167                         fprintf(stderr, "Error: Unsupported option "
168                                 "\"%s\"!\n", argv[1+flags]);
169                         help();
170                         exit(1);
171                 }
172                 flags++;
173         }
174
175         if (version) {
176                 fprintf(stderr, "i2cget version %s\n", VERSION);
177                 exit(0);
178         }
179
180         if (argc < flags + 3)
181                 help();
182
183         i2cbus = lookup_i2c_bus(argv[flags+1]);
184         if (i2cbus < 0)
185                 help();
186
187         address = parse_i2c_address(argv[flags+2]);
188         if (address < 0)
189                 help();
190
191         if (argc > flags + 3) {
192                 size = I2C_SMBUS_BYTE_DATA;
193                 daddress = strtol(argv[flags+3], &end, 0);
194                 if (*end || daddress < 0 || daddress > 0xff) {
195                         fprintf(stderr, "Error: Data address invalid!\n");
196                         help();
197                 }
198         } else {
199                 size = I2C_SMBUS_BYTE;
200                 daddress = -1;
201         }
202
203         if (argc > flags + 4) {
204                 switch (argv[flags+4][0]) {
205                 case 'b': size = I2C_SMBUS_BYTE_DATA; break;
206                 case 'w': size = I2C_SMBUS_WORD_DATA; break;
207                 case 'c': size = I2C_SMBUS_BYTE; break;
208                 default:
209                         fprintf(stderr, "Error: Invalid mode!\n");
210                         help();
211                 }
212                 pec = argv[flags+4][1] == 'p';
213         }
214
215         file = open_i2c_dev(i2cbus, filename, 0);
216         if (file < 0
217          || check_funcs(file, size, daddress, pec)
218          || set_slave_addr(file, address, force))
219                 exit(1);
220
221         if (!yes && !confirm(filename, address, size, daddress, pec))
222                 exit(0);
223
224         if (pec && ioctl(file, I2C_PEC, 1) < 0) {
225                 fprintf(stderr, "Error: Could not set PEC: %s\n",
226                         strerror(errno));
227                 close(file);
228                 exit(1);
229         }
230
231         switch (size) {
232         case I2C_SMBUS_BYTE:
233                 if (daddress >= 0) {
234                         res = i2c_smbus_write_byte(file, daddress);
235                         if (res < 0)
236                                 fprintf(stderr, "Warning - write failed\n");
237                 }
238                 res = i2c_smbus_read_byte(file);
239                 break;
240         case I2C_SMBUS_WORD_DATA:
241                 res = i2c_smbus_read_word_data(file, daddress);
242                 break;
243         default: /* I2C_SMBUS_BYTE_DATA */
244                 res = i2c_smbus_read_byte_data(file, daddress);
245         }
246         close(file);
247
248         if (res < 0) {
249                 fprintf(stderr, "Error: Read failed\n");
250                 exit(2);
251         }
252
253         printf("0x%0*x\n", size == I2C_SMBUS_WORD_DATA ? 4 : 2, res);
254
255         exit(0);
256 }
Note: See TracBrowser for help on using the browser.