1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (C) 2004 Joerg Wunsch 5 * All Rights Reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 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. 15 * 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 26 * SUCH DAMAGE. 27 * 28 * $FreeBSD$ 29 */ 30 31 /* 32 * Send or receive messages over an SMBus. 33 */ 34 35 #include <err.h> 36 #include <errno.h> 37 #include <fcntl.h> 38 #include <stdio.h> 39 #include <stdlib.h> 40 #include <string.h> 41 #include <sysexits.h> 42 #include <unistd.h> 43 44 #include <sys/types.h> 45 #include <sys/ioctl.h> 46 47 #include <dev/smbus/smb.h> 48 49 #include "pathnames.h" 50 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; 55 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 */ 63 64 static unsigned char ibuf[SMB_MAXBLOCKSIZE]; 65 static unsigned char obuf[SMB_MAXBLOCKSIZE]; 66 static unsigned short oword; 67 68 /* 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. 72 */ 73 #define MIN_I2C_ADDR 16 74 #define MAX_I2C_ADDR 240 75 76 static int do_io(void); 77 static int getnum(const char *s); 78 static void probe_i2c(void); 79 static void usage(void); 80 81 static void 82 usage(void) 83 { 84 fprintf(stderr, 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"); 88 exit(EX_USAGE); 89 } 90 91 static int 92 getnum(const char *s) 93 { 94 char *endp; 95 unsigned long l; 96 97 l = strtoul(s, &endp, 0); 98 if (*s != '\0' && *endp == '\0') 99 return (int)l; 100 return (-1); 101 } 102 103 static void 104 probe_i2c(void) 105 { 106 unsigned char addr; 107 int flags; 108 #define IS_READABLE 1 109 #define IS_WRITEABLE 2 110 struct smbcmd c; 111 112 printf("Probing for devices on %s:\n", dev); 113 114 for (addr = MIN_I2C_ADDR; addr < MAX_I2C_ADDR; addr += 2) { 115 c.slave = addr; 116 flags = 0; 117 if (ioctl(fd, SMB_RECVB, &c) != -1) 118 flags = IS_READABLE; 119 if (ioctl(fd, SMB_QUICK_WRITE, &c) != -1) 120 flags |= IS_WRITEABLE; 121 if (flags != 0) { 122 printf("Device @0x%02x: ", addr); 123 if (flags & IS_READABLE) 124 putchar('r'); 125 if (flags & IS_WRITEABLE) 126 putchar('w'); 127 putchar('\n'); 128 } 129 } 130 } 131 132 static int 133 do_io(void) 134 { 135 struct smbcmd c; 136 int i; 137 138 c.slave = slave; 139 c.cmd = cflag; 140 c.rcount = 0; 141 c.wcount = 0; 142 143 if (fmt == NULL && iflag > 0) 144 fmt = wflag? wordfmt: bytefmt; 145 146 if (cflag == -1) { 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) 157 return (-1); 158 printf(fmt, (unsigned char)c.cmd); 159 putchar('\n'); 160 return (0); 161 } else if (iflag == -1 && oflag == 1) { 162 /* no command, 1 byte output: send byte op. */ 163 c.cmd = obuf[0]; 164 return (ioctl(fd, SMB_SENDB, &c)); 165 } else 166 return (-2); 167 } 168 if (iflag == 1 && oflag == -1) { 169 /* command + 1 byte input: read byte op. */ 170 if (ioctl(fd, SMB_READB, &c) == -1) 171 return (-1); 172 printf(fmt, (unsigned char)c.rdata.byte); 173 putchar('\n'); 174 return (0); 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) 182 return (-1); 183 printf(fmt, (unsigned short)c.rdata.word); 184 putchar('\n'); 185 return (0); 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) { 191 /* 192 * command + 2 bytes output + 2 bytes input: 193 * "process call" op. 194 */ 195 c.wdata.word = oword; 196 if (ioctl(fd, SMB_PCALL, &c) == -1) 197 return (-1); 198 printf(fmt, (unsigned short)c.rdata.word); 199 putchar('\n'); 200 return (0); 201 } else if (iflag > 1 && oflag == -1) { 202 /* command + > 1 bytes of input: block read */ 203 c.rbuf = ibuf; 204 c.rcount = iflag; 205 if (ioctl(fd, SMB_BREAD, &c) == -1) 206 return (-1); 207 for (i = 0; i < c.rcount; i++) { 208 if (i != 0) 209 putchar(' '); 210 printf(fmt, ibuf[i]); 211 } 212 putchar('\n'); 213 return (0); 214 } else if (iflag == -1 && oflag > 1) { 215 /* command + > 1 bytes of output: block write */ 216 c.wbuf = obuf; 217 c.wcount = oflag; 218 return (ioctl(fd, SMB_BWRITE, &c)); 219 } 220 221 return (-2); 222 } 223 224 225 int 226 main(int argc, char **argv) 227 { 228 int i, n, errs = 0; 229 int savederrno; 230 231 while ((i = getopt(argc, argv, "F:c:f:i:o:ps:w")) != -1) 232 switch (i) { 233 case 'F': 234 fmt = optarg; 235 break; 236 237 case 'c': 238 if ((cflag = getnum(optarg)) == -1) 239 errx(EX_USAGE, "Invalid number: %s", optarg); 240 if (cflag < 0 || cflag >= 256) 241 errx(EX_USAGE, 242 "CMD out of range: %d", 243 cflag); 244 break; 245 246 case 'f': 247 dev = optarg; 248 break; 249 250 case 'i': 251 if ((iflag = getnum(optarg)) == -1) 252 errx(EX_USAGE, "Invalid number: %s", optarg); 253 if (iflag < 0 || iflag > SMB_MAXBLOCKSIZE) 254 errx(EX_USAGE, 255 "# input bytes out of range: %d", 256 iflag); 257 break; 258 259 case 'o': 260 if ((oflag = getnum(optarg)) == -1) 261 errx(EX_USAGE, "Invalid number: %s", optarg); 262 if (oflag < 0 || oflag > SMB_MAXBLOCKSIZE) 263 errx(EX_USAGE, 264 "# output bytes out of range: %d", 265 oflag); 266 break; 267 268 case 'p': 269 pflag = 1; 270 break; 271 272 case 's': 273 if ((slave = getnum(optarg)) == -1) 274 errx(EX_USAGE, "Invalid number: %s", optarg); 275 276 if (slave < MIN_I2C_ADDR || slave >= MAX_I2C_ADDR) 277 errx(EX_USAGE, 278 "Slave address out of range: %d", 279 slave); 280 break; 281 282 case 'w': 283 wflag = 1; 284 break; 285 286 default: 287 errs++; 288 } 289 argc -= optind; 290 argv += optind; 291 if (errs || (slave != -1 && pflag) || (slave == -1 && !pflag)) 292 usage(); 293 if (wflag && 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)) 301 usage(); 302 if (oflag > 0) { 303 if (oflag == 2 && wflag) { 304 if (argc == 0) 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); 310 oword = n; 311 argc--; 312 argv++; 313 } else for (i = 0; i < oflag; i++, argv++, argc--) { 314 if (argc == 0) 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); 320 obuf[i] = n; 321 } 322 } 323 if (argc != 0) 324 usage(); 325 326 if ((fd = open(dev, O_RDWR)) == -1) 327 err(EX_UNAVAILABLE, "Cannot open %s", dev); 328 329 i = 0; 330 if (pflag) 331 probe_i2c(); 332 else 333 i = do_io(); 334 335 savederrno = errno; 336 close(fd); 337 errno = savederrno; 338 339 if (i == -1) 340 err(EX_UNAVAILABLE, "Error performing SMBus IO"); 341 else if (i == -2) 342 errx(EX_USAGE, "Invalid option combination"); 343 344 return (0); 345 } 346