2 * Copyright (C) 2004 Joerg Wunsch
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * Send or receive messages over an SMBus.
42 #include <sys/types.h>
43 #include <sys/ioctl.h>
45 #include <dev/smbus/smb.h>
47 #include "pathnames.h"
49 static const char *dev = PATH_DEFAULTSMBDEV;
50 static const char *bytefmt = "0x%02x";
51 static const char *wordfmt = "0x%04x";
52 static const char *fmt;
54 static int fd; /* file descriptor for /dev/smbX */
55 static int cflag = -1; /* SMBus cmd */
56 static int iflag = -1; /* input data */
57 static int oflag = -1; /* output data */
58 static int pflag; /* probe bus */
59 static int slave = -1; /* slave address */
60 static int wflag; /* word IO */
62 static unsigned char ibuf[SMB_MAXBLOCKSIZE];
63 static unsigned char obuf[SMB_MAXBLOCKSIZE];
64 static unsigned short oword, iword;
67 * The I2C specs say that all addresses below 16 and above or equal
68 * 240 are reserved. Address 0 is the global address, but we do not
69 * care for this detail.
71 #define MIN_I2C_ADDR 16
72 #define MAX_I2C_ADDR 240
74 static int do_io(void);
75 static int getnum(const char *s);
76 static void probe_i2c(void);
77 static void usage(void);
83 "usage: smbmsg [-f dev] -p\n"
84 " smbmsg [-f dev] -s slave [-F fmt] [-c cmd] [-w] "
85 "[-i incnt] [-o outcnt] [outdata ...]\n");
95 l = strtoul(s, &endp, 0);
96 if (*s != '\0' && *endp == '\0')
106 #define IS_READABLE 1
107 #define IS_WRITEABLE 2
110 printf("Probing for devices on %s:\n", dev);
112 for (addr = MIN_I2C_ADDR; addr < MAX_I2C_ADDR; addr += 2) {
115 if (ioctl(fd, SMB_RECVB, &c) != -1)
117 if (ioctl(fd, SMB_QUICK_WRITE, &c) != -1)
118 flags |= IS_WRITEABLE;
120 printf("Device @0x%02x: ", addr);
121 if (flags & IS_READABLE)
123 if (flags & IS_WRITEABLE)
139 if (fmt == NULL && iflag > 0)
140 fmt = wflag? wordfmt: bytefmt;
143 /* operations that do not require a command byte */
144 if (iflag == -1 && oflag == 0)
145 /* 0 bytes output: quick write operation */
146 return (ioctl(fd, SMB_QUICK_WRITE, &c));
147 else if (iflag == 0 && oflag == -1)
148 /* 0 bytes input: quick read operation */
149 return (ioctl(fd, SMB_QUICK_READ, &c));
150 else if (iflag == 1 && oflag == -1) {
151 /* no command, 1 byte input: receive byte op. */
152 if (ioctl(fd, SMB_RECVB, &c) == -1)
154 printf(fmt, (unsigned char)c.cmd);
157 } else if (iflag == -1 && oflag == 1) {
158 /* no command, 1 byte output: send byte op. */
160 return (ioctl(fd, SMB_SENDB, &c));
164 if (iflag == 1 && oflag == -1) {
165 /* command + 1 byte input: read byte op. */
166 c.data.byte_ptr = ibuf;
167 if (ioctl(fd, SMB_READB, &c) == -1)
169 printf(fmt, (int)(unsigned char)ibuf[0]);
172 } else if (iflag == -1 && oflag == 1) {
173 /* command + 1 byte output: write byte op. */
174 c.data.byte = obuf[0];
175 return (ioctl(fd, SMB_WRITEB, &c));
176 } else if (wflag && iflag == 2 && oflag == -1) {
177 /* command + 2 bytes input: read word op. */
178 c.data.word_ptr = &iword;
179 if (ioctl(fd, SMB_READW, &c) == -1)
181 printf(fmt, (int)(unsigned short)iword);
184 } else if (wflag && iflag == -1 && oflag == 2) {
185 /* command + 2 bytes output: write word op. */
187 return (ioctl(fd, SMB_WRITEW, &c));
188 } else if (wflag && iflag == 2 && oflag == 2) {
190 * command + 2 bytes output + 2 bytes input:
193 c.data.process.sdata = oword;
194 c.data.process.rdata = &iword;
195 if (ioctl(fd, SMB_PCALL, &c) == -1)
197 printf(fmt, (int)(unsigned short)iword);
200 } else if (iflag > 1 && oflag == -1) {
201 /* command + > 1 bytes of input: block read */
202 c.data.byte_ptr = ibuf;
204 if (ioctl(fd, SMB_BREAD, &c) == -1)
206 for (i = 0; i < iflag; i++) {
209 printf(fmt, ibuf[i]);
213 } else if (iflag == -1 && oflag > 1) {
214 /* command + > 1 bytes of output: block write */
215 c.data.byte_ptr = obuf;
217 return (ioctl(fd, SMB_BWRITE, &c));
225 main(int argc, char **argv)
230 while ((i = getopt(argc, argv, "F:c:f:i:o:ps:w")) != -1)
237 if ((cflag = getnum(optarg)) == -1)
238 errx(EX_USAGE, "Invalid number: %s", optarg);
239 if (cflag < 0 || cflag >= 256)
241 "CMD out of range: %d",
250 if ((iflag = getnum(optarg)) == -1)
251 errx(EX_USAGE, "Invalid number: %s", optarg);
252 if (iflag < 0 || iflag > SMB_MAXBLOCKSIZE)
254 "# input bytes out of range: %d",
259 if ((oflag = getnum(optarg)) == -1)
260 errx(EX_USAGE, "Invalid number: %s", optarg);
261 if (oflag < 0 || oflag > SMB_MAXBLOCKSIZE)
263 "# output bytes out of range: %d",
272 if ((slave = getnum(optarg)) == -1)
273 errx(EX_USAGE, "Invalid number: %s", optarg);
275 if (slave < MIN_I2C_ADDR || slave >= MAX_I2C_ADDR)
277 "Slave address out of range: %d",
290 if (errs || (slave != -1 && pflag) || (slave == -1 && !pflag))
293 !((iflag == 2 && oflag == -1) ||
294 (iflag == -1 && oflag == 2) ||
295 (iflag == 2 && oflag == 2)))
296 errx(EX_USAGE, "Illegal # IO bytes for word IO");
297 if (!pflag && iflag == -1 && oflag == -1)
298 errx(EX_USAGE, "Nothing to do");
299 if (pflag && (cflag != -1 || iflag != -1 || oflag != -1 || wflag != 0))
302 if (oflag == 2 && wflag) {
304 errx(EX_USAGE, "Too few arguments for -o count");
305 if ((n = getnum(*argv)) == -1)
306 errx(EX_USAGE, "Invalid number: %s", *argv);
307 if (n < 0 || n >= 65535)
308 errx(EX_USAGE, "Value out of range: %d", n);
312 } else for (i = 0; i < oflag; i++, argv++, argc--) {
314 errx(EX_USAGE, "Too few arguments for -o count");
315 if ((n = getnum(*argv)) == -1)
316 errx(EX_USAGE, "Invalid number: %s", *argv);
317 if (n < 0 || n >= 256)
318 errx(EX_USAGE, "Value out of range: %d", n);
325 if ((fd = open(dev, O_RDWR)) == -1)
326 err(EX_UNAVAILABLE, "Cannot open %s", dev);
339 err(EX_UNAVAILABLE, "Error performing SMBus IO");
341 errx(EX_USAGE, "Invalid option combination");