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