2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (C) 2004 Joerg Wunsch
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * Send or receive messages over an SMBus.
44 #include <sys/types.h>
45 #include <sys/ioctl.h>
47 #include <dev/smbus/smb.h>
49 #include "pathnames.h"
51 static const char *dev = PATH_DEFAULTSMBDEV;
52 static const char *bytefmt = "0x%02x";
53 static const char *wordfmt = "0x%04x";
54 static const char *fmt;
56 static int fd; /* file descriptor for /dev/smbX */
57 static int cflag = -1; /* SMBus cmd */
58 static int iflag = -1; /* input data */
59 static int oflag = -1; /* output data */
60 static int pflag; /* probe bus */
61 static int slave = -1; /* slave address */
62 static int wflag; /* word IO */
64 static unsigned char ibuf[SMB_MAXBLOCKSIZE];
65 static unsigned char obuf[SMB_MAXBLOCKSIZE];
66 static unsigned short oword;
69 * The I2C specs say that all addresses below 16 and above or equal
70 * 240 are reserved. Address 0 is the global address, but we do not
71 * care for this detail.
73 #define MIN_I2C_ADDR 16
74 #define MAX_I2C_ADDR 240
76 static int do_io(void);
77 static int getnum(const char *s);
78 static void probe_i2c(void);
79 static void usage(void);
85 "usage: smbmsg [-f dev] -p\n"
86 " smbmsg [-f dev] -s slave [-F fmt] [-c cmd] [-w] "
87 "[-i incnt] [-o outcnt] [outdata ...]\n");
97 l = strtoul(s, &endp, 0);
98 if (*s != '\0' && *endp == '\0')
108 #define IS_READABLE 1
109 #define IS_WRITEABLE 2
112 printf("Probing for devices on %s:\n", dev);
114 for (addr = MIN_I2C_ADDR; addr < MAX_I2C_ADDR; addr += 2) {
117 if (ioctl(fd, SMB_RECVB, &c) != -1)
119 if (ioctl(fd, SMB_QUICK_WRITE, &c) != -1)
120 flags |= IS_WRITEABLE;
122 printf("Device @0x%02x: ", addr);
123 if (flags & IS_READABLE)
125 if (flags & IS_WRITEABLE)
143 if (fmt == NULL && iflag > 0)
144 fmt = wflag? wordfmt: bytefmt;
147 /* operations that do not require a command byte */
148 if (iflag == -1 && oflag == 0)
149 /* 0 bytes output: quick write operation */
150 return (ioctl(fd, SMB_QUICK_WRITE, &c));
151 else if (iflag == 0 && oflag == -1)
152 /* 0 bytes input: quick read operation */
153 return (ioctl(fd, SMB_QUICK_READ, &c));
154 else if (iflag == 1 && oflag == -1) {
155 /* no command, 1 byte input: receive byte op. */
156 if (ioctl(fd, SMB_RECVB, &c) == -1)
158 printf(fmt, (unsigned char)c.cmd);
161 } else if (iflag == -1 && oflag == 1) {
162 /* no command, 1 byte output: send byte op. */
164 return (ioctl(fd, SMB_SENDB, &c));
168 if (iflag == 1 && oflag == -1) {
169 /* command + 1 byte input: read byte op. */
170 if (ioctl(fd, SMB_READB, &c) == -1)
172 printf(fmt, (unsigned char)c.rdata.byte);
175 } else if (iflag == -1 && oflag == 1) {
176 /* command + 1 byte output: write byte op. */
177 c.wdata.byte = obuf[0];
178 return (ioctl(fd, SMB_WRITEB, &c));
179 } else if (wflag && iflag == 2 && oflag == -1) {
180 /* command + 2 bytes input: read word op. */
181 if (ioctl(fd, SMB_READW, &c) == -1)
183 printf(fmt, (unsigned short)c.rdata.word);
186 } else if (wflag && iflag == -1 && oflag == 2) {
187 /* command + 2 bytes output: write word op. */
188 c.wdata.word = oword;
189 return (ioctl(fd, SMB_WRITEW, &c));
190 } else if (wflag && iflag == 2 && oflag == 2) {
192 * command + 2 bytes output + 2 bytes input:
195 c.wdata.word = oword;
196 if (ioctl(fd, SMB_PCALL, &c) == -1)
198 printf(fmt, (unsigned short)c.rdata.word);
201 } else if (iflag > 1 && oflag == -1) {
202 /* command + > 1 bytes of input: block read */
205 if (ioctl(fd, SMB_BREAD, &c) == -1)
207 for (i = 0; i < c.rcount; i++) {
210 printf(fmt, ibuf[i]);
214 } else if (iflag == -1 && oflag > 1) {
215 /* command + > 1 bytes of output: block write */
218 return (ioctl(fd, SMB_BWRITE, &c));
226 main(int argc, char **argv)
231 while ((i = getopt(argc, argv, "F:c:f:i:o:ps:w")) != -1)
238 if ((cflag = getnum(optarg)) == -1)
239 errx(EX_USAGE, "Invalid number: %s", optarg);
240 if (cflag < 0 || cflag >= 256)
242 "CMD out of range: %d",
251 if ((iflag = getnum(optarg)) == -1)
252 errx(EX_USAGE, "Invalid number: %s", optarg);
253 if (iflag < 0 || iflag > SMB_MAXBLOCKSIZE)
255 "# input bytes out of range: %d",
260 if ((oflag = getnum(optarg)) == -1)
261 errx(EX_USAGE, "Invalid number: %s", optarg);
262 if (oflag < 0 || oflag > SMB_MAXBLOCKSIZE)
264 "# output bytes out of range: %d",
273 if ((slave = getnum(optarg)) == -1)
274 errx(EX_USAGE, "Invalid number: %s", optarg);
276 if (slave < MIN_I2C_ADDR || slave >= MAX_I2C_ADDR)
278 "Slave address out of range: %d",
291 if (errs || (slave != -1 && pflag) || (slave == -1 && !pflag))
294 !((iflag == 2 && oflag == -1) ||
295 (iflag == -1 && oflag == 2) ||
296 (iflag == 2 && oflag == 2)))
297 errx(EX_USAGE, "Illegal # IO bytes for word IO");
298 if (!pflag && iflag == -1 && oflag == -1)
299 errx(EX_USAGE, "Nothing to do");
300 if (pflag && (cflag != -1 || iflag != -1 || oflag != -1 || wflag != 0))
303 if (oflag == 2 && wflag) {
305 errx(EX_USAGE, "Too few arguments for -o count");
306 if ((n = getnum(*argv)) == -1)
307 errx(EX_USAGE, "Invalid number: %s", *argv);
308 if (n < 0 || n >= 65535)
309 errx(EX_USAGE, "Value out of range: %d", n);
313 } else for (i = 0; i < oflag; i++, argv++, argc--) {
315 errx(EX_USAGE, "Too few arguments for -o count");
316 if ((n = getnum(*argv)) == -1)
317 errx(EX_USAGE, "Invalid number: %s", *argv);
318 if (n < 0 || n >= 256)
319 errx(EX_USAGE, "Value out of range: %d", n);
326 if ((fd = open(dev, O_RDWR)) == -1)
327 err(EX_UNAVAILABLE, "Cannot open %s", dev);
340 err(EX_UNAVAILABLE, "Error performing SMBus IO");
342 errx(EX_USAGE, "Invalid option combination");