]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/smbmsg/smbmsg.c
Import ClangFormat.cpp from ^/vendor/clang/clang-release_380-r262564
[FreeBSD/FreeBSD.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.rbuf = ibuf;
167                 c.rcount = iflag;
168                 if (ioctl(fd, SMB_READB, &c) == -1)
169                         return (-1);
170                 printf(fmt, (int)(unsigned char)ibuf[0]);
171                 putchar('\n');
172                 return (0);
173         } else if (iflag == -1 && oflag == 1) {
174                 /* command + 1 byte output: write byte op. */
175                 c.wdata.byte = obuf[0];
176                 return (ioctl(fd, SMB_WRITEB, &c));
177         } else if (wflag && iflag == 2 && oflag == -1) {
178                 /* command + 2 bytes input: read word op. */
179                 c.rbuf = (char*) &iword;
180                 c.rcount = iflag;
181                 if (ioctl(fd, SMB_READW, &c) == -1)
182                         return (-1);
183                 printf(fmt, (int)(unsigned short)iword);
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                 c.rbuf = (char*) &iword;
197                 c.rcount = iflag;
198                 if (ioctl(fd, SMB_PCALL, &c) == -1)
199                         return (-1);
200                 printf(fmt, (int)(unsigned short)iword);
201                 putchar('\n');
202                 return (0);
203         } else if (iflag > 1 && oflag == -1) {
204                 /* command + > 1 bytes of input: block read */
205                 c.rbuf = ibuf;
206                 c.rcount = iflag;
207                 if (ioctl(fd, SMB_BREAD, &c) == -1)
208                         return (-1);
209                 for (i = 0; i < iflag; i++) {
210                         if (i != 0)
211                                 putchar(' ');
212                         printf(fmt, ibuf[i]);
213                 }
214                 putchar('\n');
215                 return (0);
216         } else if (iflag == -1 && oflag > 1) {
217                 /* command + > 1 bytes of output: block write */
218                 c.wbuf = obuf;
219                 c.wcount = oflag;
220                 return (ioctl(fd, SMB_BWRITE, &c));
221         }
222
223         return (-2);
224 }
225
226
227 int
228 main(int argc, char **argv)
229 {
230         int i, n, errs = 0;
231         int savederrno;
232
233         while ((i = getopt(argc, argv, "F:c:f:i:o:ps:w")) != -1)
234                 switch (i) {
235                 case 'F':
236                         fmt = optarg;
237                         break;
238
239                 case 'c':
240                         if ((cflag = getnum(optarg)) == -1)
241                                 errx(EX_USAGE, "Invalid number: %s", optarg);
242                         if (cflag < 0 || cflag >= 256)
243                                 errx(EX_USAGE,
244                                      "CMD out of range: %d",
245                                      cflag);
246                         break;
247
248                 case 'f':
249                         dev = optarg;
250                         break;
251
252                 case 'i':
253                         if ((iflag = getnum(optarg)) == -1)
254                                 errx(EX_USAGE, "Invalid number: %s", optarg);
255                         if (iflag < 0 || iflag > SMB_MAXBLOCKSIZE)
256                                 errx(EX_USAGE,
257                                      "# input bytes out of range: %d",
258                                      iflag);
259                         break;
260
261                 case 'o':
262                         if ((oflag = getnum(optarg)) == -1)
263                                 errx(EX_USAGE, "Invalid number: %s", optarg);
264                         if (oflag < 0 || oflag > SMB_MAXBLOCKSIZE)
265                                 errx(EX_USAGE,
266                                      "# output bytes out of range: %d",
267                                      oflag);
268                         break;
269
270                 case 'p':
271                         pflag = 1;
272                         break;
273
274                 case 's':
275                         if ((slave = getnum(optarg)) == -1)
276                                 errx(EX_USAGE, "Invalid number: %s", optarg);
277
278                         if (slave < MIN_I2C_ADDR || slave >= MAX_I2C_ADDR)
279                                 errx(EX_USAGE,
280                                      "Slave address out of range: %d",
281                                      slave);
282                         break;
283
284                 case 'w':
285                         wflag = 1;
286                         break;
287
288                 default:
289                         errs++;
290                 }
291         argc -= optind;
292         argv += optind;
293         if (errs || (slave != -1 && pflag) || (slave == -1 && !pflag))
294                 usage();
295         if (wflag &&
296             !((iflag == 2 && oflag == -1) ||
297               (iflag == -1 && oflag == 2) ||
298               (iflag == 2 && oflag == 2)))
299                 errx(EX_USAGE, "Illegal # IO bytes for word IO");
300         if (!pflag && iflag == -1 && oflag == -1)
301                 errx(EX_USAGE, "Nothing to do");
302         if (pflag && (cflag != -1 || iflag != -1 || oflag != -1 || wflag != 0))
303                 usage();
304         if (oflag > 0) {
305                 if (oflag == 2 && wflag) {
306                         if (argc == 0)
307                                 errx(EX_USAGE, "Too few arguments for -o count");
308                         if ((n = getnum(*argv)) == -1)
309                                 errx(EX_USAGE, "Invalid number: %s", *argv);
310                         if (n < 0 || n >= 65535)
311                                 errx(EX_USAGE, "Value out of range: %d", n);
312                         oword = n;
313                         argc--;
314                         argv++;
315                 } else for (i = 0; i < oflag; i++, argv++, argc--) {
316                         if (argc == 0)
317                                 errx(EX_USAGE, "Too few arguments for -o count");
318                         if ((n = getnum(*argv)) == -1)
319                                 errx(EX_USAGE, "Invalid number: %s", *argv);
320                         if (n < 0 || n >= 256)
321                                 errx(EX_USAGE, "Value out of range: %d", n);
322                         obuf[i] = n;
323                 }
324         }
325         if (argc != 0)
326                 usage();
327
328         if ((fd = open(dev, O_RDWR)) == -1)
329                 err(EX_UNAVAILABLE, "Cannot open %s", dev);
330
331         i = 0;
332         if (pflag)
333                 probe_i2c();
334         else
335                 i = do_io();
336
337         savederrno = errno;
338         close(fd);
339         errno = savederrno;
340
341         if (i == -1)
342                 err(EX_UNAVAILABLE, "Error performing SMBus IO");
343         else if (i == -2)
344                 errx(EX_USAGE, "Invalid option combination");
345
346         return (0);
347 }