]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - usr.sbin/smbmsg/smbmsg.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / usr.sbin / smbmsg / smbmsg.c
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 int      do_io(void);
75 static int      getnum(const char *s);
76 static void     probe_i2c(void);
77 static void     usage(void);
78
79 static void
80 usage(void)
81 {
82         fprintf(stderr,
83                 "usage: smbmsg [-f dev] -p\n"
84                 "       smbmsg [-f dev] -s slave [-F fmt] [-c cmd] [-w] "
85                 "[-i incnt] [-o outcnt] [outdata ...]\n");
86         exit(EX_USAGE);
87 }
88
89 static int
90 getnum(const char *s)
91 {
92         char *endp;
93         unsigned long l;
94
95         l = strtoul(s, &endp, 0);
96         if (*s != '\0' && *endp == '\0')
97                 return (int)l;
98         return (-1);
99 }
100
101 static void
102 probe_i2c(void)
103 {
104         unsigned char addr;
105         int flags;
106 #define IS_READABLE     1
107 #define IS_WRITEABLE    2
108         struct smbcmd c;
109
110         printf("Probing for devices on %s:\n", dev);
111
112         for (addr = MIN_I2C_ADDR; addr < MAX_I2C_ADDR; addr += 2) {
113                 c.slave = addr;
114                 flags = 0;
115                 if (ioctl(fd, SMB_RECVB, &c) != -1)
116                         flags = IS_READABLE;
117                 if (ioctl(fd, SMB_QUICK_WRITE, &c) != -1)
118                         flags |= IS_WRITEABLE;
119                 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
130 static int
131 do_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
224 int
225 main(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 }