smbmsg.c revision 129330
1263320Sdim/*-
2263320Sdim * Copyright (C) 2004 Joerg Wunsch
3263320Sdim * All Rights Reserved.
4263320Sdim *
5269012Semaste * Redistribution and use in source and binary forms, with or without
6263320Sdim * modification, are permitted provided that the following conditions
7263320Sdim * are met:
8263320Sdim * 1. Redistributions of source code must retain the above copyright
9263320Sdim *    notice, this list of conditions and the following disclaimer.
10263320Sdim * 2. Redistributions in binary form must reproduce the above copyright
11263320Sdim *    notice, this list of conditions and the following disclaimer in the
12263320Sdim *    documentation and/or other materials provided with the distribution.
13263320Sdim *
14263320Sdim * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15263320Sdim * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16263320Sdim * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17263320Sdim * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
18263320Sdim * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19263320Sdim * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20263320Sdim * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21263320Sdim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22263320Sdim * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23263320Sdim * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24263320Sdim * SUCH DAMAGE.
25263320Sdim *
26263320Sdim * $FreeBSD: cvs2svn/branches/JOERG/usr.sbin/smbmsg/smbmsg.c 129330 2004-05-17 10:56:05Z joerg $
27263320Sdim */
28263320Sdim
29263320Sdim/*
30263320Sdim * Send or receive messages over an SMBus.
31263320Sdim */
32263320Sdim
33263320Sdim#include <err.h>
34263320Sdim#include <errno.h>
35263320Sdim#include <fcntl.h>
36263320Sdim#include <stdio.h>
37263320Sdim#include <stdlib.h>
38263320Sdim#include <string.h>
39263320Sdim#include <sysexits.h>
40263320Sdim#include <unistd.h>
41263320Sdim
42263320Sdim#include <sys/types.h>
43263320Sdim#include <sys/ioctl.h>
44263320Sdim
45263320Sdim#include <dev/smbus/smb.h>
46263320Sdim
47263320Sdim#include "pathnames.h"
48263320Sdim
49263320Sdimstatic const char *dev = PATH_DEFAULTSMBDEV;
50263320Sdimstatic const char *bytefmt = "0x%02x";
51263320Sdimstatic const char *wordfmt = "0x%04x";
52263320Sdimstatic const char *fmt;
53263320Sdim
54263320Sdimstatic int fd;			/* file descriptor for /dev/smbX */
55263320Sdimstatic int cflag = -1;		/* SMBus cmd */
56263320Sdimstatic int iflag = -1;		/* input data */
57263320Sdimstatic int oflag = -1;		/* output data */
58263320Sdimstatic int pflag;		/* probe bus */
59263320Sdimstatic int slave = -1;		/* slave address */
60263320Sdimstatic int wflag;		/* word IO */
61263320Sdim
62263320Sdimstatic unsigned char ibuf[SMB_MAXBLOCKSIZE];
63263320Sdimstatic unsigned char obuf[SMB_MAXBLOCKSIZE];
64263320Sdimstatic unsigned short oword, iword;
65263320Sdim
66263320Sdim/*
67263320Sdim * 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
74static void
75usage(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
84static int
85getnum(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
96static void
97probe_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
125static int
126do_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
219int
220main(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