smbmsg.c revision 129353
1219019Sgabor/*-
2219019Sgabor * Copyright (C) 2004 Joerg Wunsch
3219019Sgabor * All Rights Reserved.
4219019Sgabor *
5219019Sgabor * Redistribution and use in source and binary forms, with or without
6219019Sgabor * modification, are permitted provided that the following conditions
7219019Sgabor * are met:
8219019Sgabor * 1. Redistributions of source code must retain the above copyright
9219019Sgabor *    notice, this list of conditions and the following disclaimer.
10219019Sgabor * 2. Redistributions in binary form must reproduce the above copyright
11219019Sgabor *    notice, this list of conditions and the following disclaimer in the
12219019Sgabor *    documentation and/or other materials provided with the distribution.
13219019Sgabor *
14219019Sgabor * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15219019Sgabor * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16219019Sgabor * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17219019Sgabor * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
18219019Sgabor * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19219019Sgabor * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20219019Sgabor * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21219019Sgabor * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22219019Sgabor * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23219019Sgabor * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24219019Sgabor * SUCH DAMAGE.
25219019Sgabor *
26219019Sgabor * $FreeBSD: head/usr.sbin/smbmsg/smbmsg.c 129353 2004-05-17 19:19:08Z joerg $
27219019Sgabor */
28219019Sgabor
29219019Sgabor/*
30219019Sgabor * Send or receive messages over an SMBus.
31219019Sgabor */
32219019Sgabor
33219019Sgabor#include <err.h>
34219019Sgabor#include <errno.h>
35219019Sgabor#include <fcntl.h>
36219019Sgabor#include <stdio.h>
37219019Sgabor#include <stdlib.h>
38219019Sgabor#include <string.h>
39219019Sgabor#include <sysexits.h>
40219019Sgabor#include <unistd.h>
41219019Sgabor
42219019Sgabor#include <sys/types.h>
43219019Sgabor#include <sys/ioctl.h>
44219019Sgabor
45219019Sgabor#include <dev/smbus/smb.h>
46219019Sgabor
47219019Sgabor#include "pathnames.h"
48219019Sgabor
49219019Sgaborstatic const char *dev = PATH_DEFAULTSMBDEV;
50219019Sgaborstatic const char *bytefmt = "0x%02x";
51219019Sgaborstatic const char *wordfmt = "0x%04x";
52219019Sgaborstatic const char *fmt;
53219019Sgabor
54219019Sgaborstatic int fd;			/* file descriptor for /dev/smbX */
55219019Sgaborstatic int cflag = -1;		/* SMBus cmd */
56219019Sgaborstatic int iflag = -1;		/* input data */
57219019Sgaborstatic int oflag = -1;		/* output data */
58219019Sgaborstatic int pflag;		/* probe bus */
59219019Sgaborstatic int slave = -1;		/* slave address */
60219019Sgaborstatic int wflag;		/* word IO */
61219019Sgabor
62219019Sgaborstatic unsigned char ibuf[SMB_MAXBLOCKSIZE];
63219019Sgaborstatic unsigned char obuf[SMB_MAXBLOCKSIZE];
64219019Sgaborstatic unsigned short oword, iword;
65219019Sgabor
66219019Sgabor/*
67219019Sgabor * The I2C specs say that all addresses below 16 and above or equal
68219019Sgabor * 240 are reserved.  Address 0 is the global address, but we do not
69219019Sgabor * care for this detail.
70219019Sgabor */
71219019Sgabor#define MIN_I2C_ADDR	16
72219019Sgabor#define MAX_I2C_ADDR	240
73219019Sgabor
74219019Sgaborstatic int	do_io(void);
75219019Sgaborstatic int	getnum(const char *s);
76219019Sgaborstatic void	probe_i2c(void);
77219019Sgaborstatic void	usage(void);
78219019Sgabor
79219019Sgaborstatic void
80219019Sgaborusage(void)
81219019Sgabor{
82219019Sgabor	fprintf(stderr,
83219019Sgabor		"usage: smbmsg [-f dev] -p\n"
84219019Sgabor		"       smbmsg [-f dev] -s slave [-F fmt] [-c cmd] [-w] "
85219019Sgabor		"[-i incnt] [-o outcnt] [outdata ...]\n");
86219019Sgabor	exit(EX_USAGE);
87219019Sgabor}
88219019Sgabor
89219019Sgaborstatic int
90219019Sgaborgetnum(const char *s)
91219019Sgabor{
92219019Sgabor	char *endp;
93219019Sgabor	unsigned long l;
94219019Sgabor
95219019Sgabor	l = strtoul(s, &endp, 0);
96219019Sgabor	if (*s != '\0' && *endp == '\0')
97219019Sgabor		return (int)l;
98219019Sgabor	return (-1);
99219019Sgabor}
100219019Sgabor
101219019Sgaborstatic void
102219019Sgaborprobe_i2c(void)
103219019Sgabor{
104219019Sgabor	unsigned char addr;
105219019Sgabor	int flags;
106219019Sgabor#define IS_READABLE	1
107219019Sgabor#define IS_WRITEABLE	2
108219019Sgabor	struct smbcmd c;
109219019Sgabor
110219019Sgabor	printf("Probing for devices on %s:\n", dev);
111219019Sgabor
112219019Sgabor	for (addr = MIN_I2C_ADDR; addr < MAX_I2C_ADDR; addr += 2) {
113219019Sgabor		c.slave = addr;
114219019Sgabor		flags = 0;
115219019Sgabor		if (ioctl(fd, SMB_RECVB, &c) != -1)
116219019Sgabor			flags = IS_READABLE;
117219019Sgabor		if (ioctl(fd, SMB_QUICK_WRITE, &c) != -1)
118219019Sgabor			flags |= IS_WRITEABLE;
119219019Sgabor		if (flags != 0) {
120			printf("Device @0x%02x: ", addr);
121			if (flags & IS_READABLE)
122				putchar('r');
123			if (flags & IS_WRITEABLE)
124				putchar('w');
125			putchar('\n');
126		}
127	}
128}
129
130static int
131do_io(void)
132{
133	struct smbcmd c;
134	int i;
135
136	c.slave = slave;
137	c.cmd = cflag;
138
139	if (fmt == NULL && iflag > 0)
140		fmt = wflag? wordfmt: bytefmt;
141
142	if (cflag == -1) {
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)
153				return (-1);
154			printf(fmt, (unsigned char)c.cmd);
155			putchar('\n');
156			return (0);
157		} else if (iflag == -1 && oflag == 1) {
158			/* no command, 1 byte output: send byte op. */
159			c.cmd = obuf[0];
160			return (ioctl(fd, SMB_SENDB, &c));
161		} else
162			return (-2);
163	}
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)
168			return (-1);
169		printf(fmt, (int)(unsigned char)ibuf[0]);
170		putchar('\n');
171		return (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)
180			return (-1);
181		printf(fmt, (int)(unsigned short)iword);
182		putchar('\n');
183		return (0);
184	} else if (wflag && iflag == -1 && oflag == 2) {
185		/* command + 2 bytes output: write word op. */
186		c.data.word = oword;
187		return (ioctl(fd, SMB_WRITEW, &c));
188	} else if (wflag && iflag == 2 && oflag == 2) {
189		/*
190		 * command + 2 bytes output + 2 bytes input:
191		 * "process call" op.
192		 */
193		c.data.process.sdata = oword;
194		c.data.process.rdata = &iword;
195		if (ioctl(fd, SMB_PCALL, &c) == -1)
196			return (-1);
197		printf(fmt, (int)(unsigned short)iword);
198		putchar('\n');
199		return (0);
200	} else if (iflag > 1 && oflag == -1) {
201		/* command + > 1 bytes of input: block read */
202		c.data.byte_ptr = ibuf;
203		c.count = iflag;
204		if (ioctl(fd, SMB_BREAD, &c) == -1)
205			return (-1);
206		for (i = 0; i < iflag; i++) {
207			if (i != 0)
208				putchar(' ');
209			printf(fmt, ibuf[i]);
210		}
211		putchar('\n');
212		return (0);
213	} else if (iflag == -1 && oflag > 1) {
214		/* command + > 1 bytes of output: block write */
215		c.data.byte_ptr = obuf;
216		c.count = oflag;
217		return (ioctl(fd, SMB_BWRITE, &c));
218	}
219
220	return (-2);
221}
222
223
224int
225main(int argc, char **argv)
226{
227	int i, n, errs = 0;
228	int savederrno;
229
230	while ((i = getopt(argc, argv, "F:c:f:i:o:ps:w")) != -1)
231		switch (i) {
232		case 'F':
233			fmt = optarg;
234			break;
235
236		case 'c':
237			if ((cflag = getnum(optarg)) == -1)
238				errx(EX_USAGE, "Invalid number: %s", optarg);
239			if (cflag < 0 || cflag >= 256)
240				errx(EX_USAGE,
241				     "CMD out of range: %d",
242				     cflag);
243			break;
244
245		case 'f':
246			dev = optarg;
247			break;
248
249		case 'i':
250			if ((iflag = getnum(optarg)) == -1)
251				errx(EX_USAGE, "Invalid number: %s", optarg);
252			if (iflag < 0 || iflag >= SMB_MAXBLOCKSIZE)
253				errx(EX_USAGE,
254				     "# input bytes out of range: %d",
255				     iflag);
256			break;
257
258		case 'o':
259			if ((oflag = getnum(optarg)) == -1)
260				errx(EX_USAGE, "Invalid number: %s", optarg);
261			if (oflag < 0 || oflag >= SMB_MAXBLOCKSIZE)
262				errx(EX_USAGE,
263				     "# output bytes out of range: %d",
264				     oflag);
265			break;
266
267		case 'p':
268			pflag = 1;
269			break;
270
271		case 's':
272			if ((slave = getnum(optarg)) == -1)
273				errx(EX_USAGE, "Invalid number: %s", optarg);
274
275			if (slave < MIN_I2C_ADDR || slave >= MAX_I2C_ADDR)
276				errx(EX_USAGE,
277				     "Slave address out of range: %d",
278				     slave);
279			break;
280
281		case 'w':
282			wflag = 1;
283			break;
284
285		default:
286			errs++;
287		}
288	argc -= optind;
289	argv += optind;
290	if (errs || (slave != -1 && pflag) || (slave == -1 && !pflag))
291		usage();
292	if (wflag &&
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))
300		usage();
301	if (oflag > 0) {
302		if (oflag == 2 && wflag) {
303			if (argc == 0)
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);
309			oword = n;
310			argc--;
311			argv++;
312		} else for (i = 0; i < oflag; i++, argv++, argc--) {
313			if (argc == 0)
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);
319			obuf[i] = n;
320		}
321	}
322	if (argc != 0)
323		usage();
324
325	if ((fd = open(dev, O_RDWR)) == -1)
326		err(EX_UNAVAILABLE, "Cannot open %s", dev);
327
328	i = 0;
329	if (pflag)
330		probe_i2c();
331	else
332		i = do_io();
333
334	savederrno = errno;
335	close(fd);
336	errno = savederrno;
337
338	if (i == -1)
339		err(EX_UNAVAILABLE, "Error performing SMBus IO");
340	else if (i == -2)
341		errx(EX_USAGE, "Invalid option combination");
342
343	return (0);
344}
345