]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/i2c/i2c.c
Add two missing eventhandler.h headers
[FreeBSD/FreeBSD.git] / usr.sbin / i2c / i2c.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (C) 2008-2009 Semihalf, Michal Hajduk and Bartlomiej Sieka
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31
32 #include <err.h>
33 #include <errno.h>
34 #include <sysexits.h>
35 #include <fcntl.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <stdarg.h>
40 #include <unistd.h>
41 #include <sys/ioctl.h>
42
43 #include <dev/iicbus/iic.h>
44
45 #define I2C_DEV                 "/dev/iic0"
46 #define I2C_MODE_NOTSET         0
47 #define I2C_MODE_NONE           1
48 #define I2C_MODE_STOP_START     2
49 #define I2C_MODE_REPEATED_START 3
50
51 struct options {
52         int     width;
53         int     count;
54         int     verbose;
55         int     addr_set;
56         int     binary;
57         int     scan;
58         int     skip;
59         int     reset;
60         int     mode;
61         char    dir;
62         uint32_t        addr;
63         uint32_t        off;
64 };
65
66 struct skip_range {
67         int     start;
68         int     end;
69 };
70
71 __dead2 static void
72 usage(void)
73 {
74
75         fprintf(stderr, "usage: %s -a addr [-f device] [-d [r|w]] [-o offset] "
76             "[-w [0|8|16]] [-c count] [-m [ss|rs|no]] [-b] [-v]\n",
77             getprogname());
78         fprintf(stderr, "       %s -s [-f device] [-n skip_addr] -v\n",
79             getprogname());
80         fprintf(stderr, "       %s -r [-f device] -v\n", getprogname());
81         exit(EX_USAGE);
82 }
83
84 static struct skip_range
85 skip_get_range(char *skip_addr)
86 {
87         struct skip_range addr_range;
88         char *token;
89
90         addr_range.start = 0;
91         addr_range.end = 0;
92
93         token = strsep(&skip_addr, "..");
94         if (token) {
95                 addr_range.start = strtoul(token, 0, 16);
96                 token = strsep(&skip_addr, "..");
97                 if ((token != NULL) && !atoi(token)) {
98                         token = strsep(&skip_addr, "..");
99                         if (token)
100                                 addr_range.end = strtoul(token, 0, 16);
101                 }
102         }
103
104         return (addr_range);
105 }
106
107 /* Parse the string to get hex 7 bits addresses */
108 static int
109 skip_get_tokens(char *skip_addr, int *sk_addr, int max_index)
110 {
111         char *token;
112         int i;
113
114         for (i = 0; i < max_index; i++) {
115                 token = strsep(&skip_addr, ":");
116                 if (token == NULL)
117                         break;
118                 sk_addr[i] = strtoul(token, 0, 16);
119         }
120         return (i);
121 }
122
123 static int
124 scan_bus(struct iiccmd cmd, char *dev, int skip, char *skip_addr)
125 {
126         struct iic_msg rdmsg;
127         struct iic_rdwr_data rdwrdata;
128         struct skip_range addr_range = { 0, 0 };
129         int *tokens, fd, error, i, index, j;
130         int len = 0, do_skip = 0, no_range = 1, num_found = 0, use_read_xfer = 0;
131         uint8_t rdbyte;
132
133         fd = open(dev, O_RDWR);
134         if (fd == -1) {
135                 fprintf(stderr, "Error opening I2C controller (%s) for "
136                     "scanning: %s\n", dev, strerror(errno));
137                 return (EX_NOINPUT);
138         }
139
140         if (skip) {
141                 len = strlen(skip_addr);
142                 if (strstr(skip_addr, "..") != NULL) {
143                         addr_range = skip_get_range(skip_addr);
144                         no_range = 0;
145                 } else {
146                         tokens = (int *)malloc((len / 2 + 1) * sizeof(int));
147                         if (tokens == NULL) {
148                                 fprintf(stderr, "Error allocating tokens "
149                                     "buffer\n");
150                                 error = -1;
151                                 goto out;
152                         }
153                         index = skip_get_tokens(skip_addr, tokens,
154                             len / 2 + 1);
155                 }
156
157                 if (!no_range && (addr_range.start > addr_range.end)) {
158                         fprintf(stderr, "Skip address out of range\n");
159                         error = -1;
160                         goto out;
161                 }
162         }
163
164         printf("Scanning I2C devices on %s: ", dev);
165
166 start_over:
167         if (use_read_xfer) {
168                 fprintf(stderr, 
169                     "Hardware may not support START/STOP scanning; "
170                     "trying less-reliable read method.\n");
171         }
172
173         for (i = 1; i < 127; i++) {
174
175                 if (skip && ( addr_range.start < addr_range.end)) {
176                         if (i >= addr_range.start && i <= addr_range.end)
177                                 continue;
178
179                 } else if (skip && no_range)
180                         for (j = 0; j < index; j++) {
181                                 if (tokens[j] == i) {
182                                         do_skip = 1;
183                                         break;
184                                 }
185                         }
186
187                 if (do_skip) {
188                         do_skip = 0;
189                         continue;
190                 }
191
192                 cmd.slave = i << 1;
193                 cmd.last = 1;
194                 cmd.count = 0;
195                 error = ioctl(fd, I2CRSTCARD, &cmd);
196                 if (error) {
197                         fprintf(stderr, "Controller reset failed\n");
198                         goto out;
199                 }
200                 if (use_read_xfer) {
201                         rdmsg.buf = &rdbyte;
202                         rdmsg.len = 1;
203                         rdmsg.flags = IIC_M_RD;
204                         rdmsg.slave = i << 1;
205                         rdwrdata.msgs = &rdmsg;
206                         rdwrdata.nmsgs = 1;
207                         error = ioctl(fd, I2CRDWR, &rdwrdata);
208                 } else {
209                         cmd.slave = i << 1;
210                         cmd.last = 1;
211                         error = ioctl(fd, I2CSTART, &cmd);
212                         if (errno == ENODEV || errno == EOPNOTSUPP) {
213                                 /* If START not supported try reading. */
214                                 use_read_xfer = 1;
215                                 goto start_over;
216                         }
217                         ioctl(fd, I2CSTOP);
218                 }
219                 if (error == 0) {
220                         ++num_found;
221                         printf("%02x ", i);
222                 }
223         }
224
225         /*
226          * If we found nothing, maybe START is not supported and returns a
227          * generic error code such as EIO or ENXIO, so try again using reads.
228          */
229         if (num_found == 0) {
230                 if (!use_read_xfer) {
231                         use_read_xfer = 1;
232                         goto start_over;
233                 }
234                 printf("<none found>");
235         }
236         printf("\n");
237
238         error = ioctl(fd, I2CRSTCARD, &cmd);
239 out:
240         close(fd);
241         if (skip && no_range)
242                 free(tokens);
243
244         if (error) {
245                 fprintf(stderr, "Error scanning I2C controller (%s): %s\n",
246                     dev, strerror(errno));
247                 return (EX_NOINPUT);
248         } else
249                 return (EX_OK);
250 }
251
252 static int
253 reset_bus(struct iiccmd cmd, char *dev)
254 {
255         int fd, error;
256
257         fd = open(dev, O_RDWR);
258         if (fd == -1) {
259                 fprintf(stderr, "Error opening I2C controller (%s) for "
260                     "resetting: %s\n", dev, strerror(errno));
261                 return (EX_NOINPUT);
262         }
263
264         printf("Resetting I2C controller on %s: ", dev);
265         error = ioctl(fd, I2CRSTCARD, &cmd);
266         close (fd);
267
268         if (error) {
269                 printf("error: %s\n", strerror(errno));
270                 return (EX_IOERR);
271         } else {
272                 printf("OK\n");
273                 return (EX_OK);
274         }
275 }
276
277 static char *
278 prepare_buf(int size, uint32_t off)
279 {
280         char *buf;
281
282         buf = malloc(size);
283         if (buf == NULL)
284                 return (buf);
285
286         if (size == 1)
287                 buf[0] = off & 0xff;
288         else if (size == 2) {
289                 buf[0] = (off >> 8) & 0xff;
290                 buf[1] = off & 0xff;
291         }
292
293         return (buf);
294 }
295
296 static int
297 i2c_write(char *dev, struct options i2c_opt, char *i2c_buf)
298 {
299         struct iiccmd cmd;
300         int ch, i, error, fd, bufsize;
301         char *err_msg, *buf;
302
303         /*
304          * Read data to be written to the chip from stdin
305          */
306         if (i2c_opt.verbose && !i2c_opt.binary)
307                 fprintf(stderr, "Enter %u bytes of data: ", i2c_opt.count);
308
309         for (i = 0; i < i2c_opt.count; i++) {
310                 ch = getchar();
311                 if (ch == EOF) {
312                         free(i2c_buf);
313                         err(1, "not enough data, exiting\n");
314                 }
315                 i2c_buf[i] = ch;
316         }
317
318         fd = open(dev, O_RDWR);
319         if (fd == -1) {
320                 free(i2c_buf);
321                 err(1, "open failed");
322         }
323
324         cmd.slave = i2c_opt.addr;
325         error = ioctl(fd, I2CSTART, &cmd);
326         if (error == -1) {
327                 err_msg = "ioctl: error sending start condition";
328                 goto err1;
329         }
330
331         if (i2c_opt.width) {
332                 bufsize = i2c_opt.width / 8;
333                 buf = prepare_buf(bufsize, i2c_opt.off);
334                 if (buf == NULL) {
335                         err_msg = "error: offset malloc";
336                         goto err1;
337                 }
338         } else {
339                 bufsize = 0;
340                 buf = NULL;
341         }
342
343         switch(i2c_opt.mode) {
344         case I2C_MODE_STOP_START:
345                 /*
346                  * Write offset where the data will go
347                  */
348                 if (i2c_opt.width) {
349                         cmd.count = bufsize;
350                         cmd.buf = buf;
351                         error = ioctl(fd, I2CWRITE, &cmd);
352                         free(buf);
353                         if (error == -1) {
354                                 err_msg = "ioctl: error writing offset";
355                                 goto err1;
356                         }
357                 }
358
359                 error = ioctl(fd, I2CSTOP);
360                 if (error == -1) {
361                         err_msg = "ioctl: error sending stop condition";
362                         goto err2;
363                 }
364                 cmd.slave = i2c_opt.addr;
365                 error = ioctl(fd, I2CSTART, &cmd);
366                 if (error == -1) {
367                         err_msg = "ioctl: error sending start condition";
368                         goto err1;
369                 }
370
371                 /*
372                  * Write the data
373                  */
374                 cmd.count = i2c_opt.count;
375                 cmd.buf = i2c_buf;
376                 cmd.last = 0;
377                 error = ioctl(fd, I2CWRITE, &cmd);
378                 if (error == -1) {
379                         err_msg = "ioctl: error writing";
380                         goto err1;
381                 }
382                 break;
383
384         case I2C_MODE_REPEATED_START:
385                 /*
386                  * Write offset where the data will go
387                  */
388                 if (i2c_opt.width) {
389                         cmd.count = bufsize;
390                         cmd.buf = buf;
391                         error = ioctl(fd, I2CWRITE, &cmd);
392                         free(buf);
393                         if (error == -1) {
394                                 err_msg = "ioctl: error writing offset";
395                                 goto err1;
396                         }
397                 }
398
399                 cmd.slave = i2c_opt.addr;
400                 error = ioctl(fd, I2CRPTSTART, &cmd);
401                 if (error == -1) {
402                         err_msg = "ioctl: error sending repeated start "
403                             "condition";
404                         goto err1;
405                 }
406
407                 /*
408                  * Write the data
409                  */
410                 cmd.count = i2c_opt.count;
411                 cmd.buf = i2c_buf;
412                 cmd.last = 0;
413                 error = ioctl(fd, I2CWRITE, &cmd);
414                 if (error == -1) {
415                         err_msg = "ioctl: error writing";
416                         goto err1;
417                 }
418                 break;
419
420         case I2C_MODE_NONE: /* fall through */
421         default:                
422                 buf = realloc(buf, bufsize + i2c_opt.count);
423                 if (buf == NULL) {
424                         err_msg = "error: data malloc";
425                         goto err1;
426                 }
427
428                 memcpy(buf + bufsize, i2c_buf, i2c_opt.count);
429                 /*
430                  * Write offset and data
431                  */
432                 cmd.count = bufsize + i2c_opt.count;
433                 cmd.buf = buf;
434                 cmd.last = 0;
435                 error = ioctl(fd, I2CWRITE, &cmd);
436                 free(buf);
437                 if (error == -1) {
438                         err_msg = "ioctl: error writing";
439                         goto err1;
440                 }
441                 break;
442         }
443         error = ioctl(fd, I2CSTOP);
444         if (error == -1) {
445                 err_msg = "ioctl: error sending stop condition";
446                 goto err2;
447         }
448
449         close(fd);
450         return (0);
451
452 err1:
453         error = ioctl(fd, I2CSTOP);
454         if (error == -1)
455                 fprintf(stderr, "error sending stop condition\n");
456 err2:
457         if (err_msg)
458                 fprintf(stderr, "%s\n", err_msg);
459
460         close(fd);
461         return (1);
462 }
463
464 static int
465 i2c_read(char *dev, struct options i2c_opt, char *i2c_buf)
466 {
467         struct iiccmd cmd;
468         int fd, error, bufsize;
469         char *err_msg, data = 0, *buf;
470
471         fd = open(dev, O_RDWR);
472         if (fd == -1)
473                 err(1, "open failed");
474
475         bzero(&cmd, sizeof(cmd));
476
477         if (i2c_opt.width) {
478                 cmd.slave = i2c_opt.addr;
479                 cmd.count = 1;
480                 cmd.last = 0;
481                 cmd.buf = &data;
482                 error = ioctl(fd, I2CSTART, &cmd);
483                 if (error == -1) {
484                         err_msg = "ioctl: error sending start condition";
485                         goto err1;
486                 }
487                 bufsize = i2c_opt.width / 8;
488                 buf = prepare_buf(bufsize, i2c_opt.off);
489                 if (buf == NULL) {
490                         err_msg = "error: offset malloc";
491                         goto err1;
492                 }
493
494                 cmd.count = bufsize;
495                 cmd.buf = buf;
496                 cmd.last = 0;
497                 error = ioctl(fd, I2CWRITE, &cmd);
498                 free(buf);
499                 if (error == -1) {
500                         err_msg = "ioctl: error writing offset";
501                         goto err1;
502                 }
503
504                 if (i2c_opt.mode == I2C_MODE_STOP_START) {
505                         error = ioctl(fd, I2CSTOP);
506                         if (error == -1) {
507                                 err_msg = "error sending stop condition";
508                                 goto err2;
509                         }
510                 }
511         }
512         cmd.slave = i2c_opt.addr | 1;
513         cmd.count = 1;
514         cmd.last = 0;
515         cmd.buf = &data;
516         if (i2c_opt.mode == I2C_MODE_STOP_START || i2c_opt.width == 0) {
517                 error = ioctl(fd, I2CSTART, &cmd);
518                 if (error == -1) {
519                         err_msg = "ioctl: error sending start condition";
520                         goto err2;
521                 }
522         } else if (i2c_opt.mode == I2C_MODE_REPEATED_START) {
523                 error = ioctl(fd, I2CRPTSTART, &cmd);
524                 if (error == -1) {
525                         err_msg = "ioctl: error sending repeated start "
526                             "condition";
527                         goto err1;
528                 }
529         }
530
531         cmd.count = i2c_opt.count;
532         cmd.buf = i2c_buf;
533         cmd.last = 1;
534         error = ioctl(fd, I2CREAD, &cmd);
535         if (error == -1) {
536                 err_msg = "ioctl: error while reading";
537                 goto err1;
538         }
539
540         error = ioctl(fd, I2CSTOP);
541         if (error == -1) {
542                 err_msg = "error sending stop condtion\n";
543                 goto err2;
544         }
545
546         close(fd);
547         return (0);
548
549 err1:
550         error = ioctl(fd, I2CSTOP);
551         if (error == -1)
552                 fprintf(stderr, "error sending stop condition\n");
553 err2:
554         if (err_msg)
555                 fprintf(stderr, "%s\n", err_msg);
556
557         close(fd);
558         return (1);
559 }
560
561 int
562 main(int argc, char** argv)
563 {
564         struct iiccmd cmd;
565         struct options i2c_opt;
566         char *dev, *skip_addr, *i2c_buf;
567         int error, chunk_size, i, j, ch;
568
569         errno = 0;
570         error = 0;
571
572         /* Line-break the output every chunk_size bytes */
573         chunk_size = 16;
574
575         dev = I2C_DEV;
576
577         /* Default values */
578         i2c_opt.addr_set = 0;
579         i2c_opt.off = 0;
580         i2c_opt.verbose = 0;
581         i2c_opt.dir = 'r';      /* direction = read */
582         i2c_opt.width = 8;
583         i2c_opt.count = 1;
584         i2c_opt.binary = 0;     /* ASCII text output */
585         i2c_opt.scan = 0;       /* no bus scan */
586         i2c_opt.skip = 0;       /* scan all addresses */
587         i2c_opt.reset = 0;      /* no bus reset */
588         i2c_opt.mode = I2C_MODE_NOTSET;
589
590         while ((ch = getopt(argc, argv, "a:f:d:o:w:c:m:n:sbvrh")) != -1) {
591                 switch(ch) {
592                 case 'a':
593                         i2c_opt.addr = (strtoul(optarg, 0, 16) << 1);
594                         if (i2c_opt.addr == 0 && errno == EINVAL)
595                                 i2c_opt.addr_set = 0;
596                         else
597                                 i2c_opt.addr_set = 1;
598                         break;
599                 case 'f':
600                         dev = optarg;
601                         break;
602                 case 'd':
603                         i2c_opt.dir = optarg[0];
604                         break;
605                 case 'o':
606                         i2c_opt.off = strtoul(optarg, 0, 16);
607                         if (i2c_opt.off == 0 && errno == EINVAL)
608                                 error = 1;
609                         break;
610                 case 'w':
611                         i2c_opt.width = atoi(optarg);
612                         break;
613                 case 'c':
614                         i2c_opt.count = atoi(optarg);
615                         break;
616                 case 'm':
617                         if (!strcmp(optarg, "no"))
618                                 i2c_opt.mode = I2C_MODE_NONE;
619                         else if (!strcmp(optarg, "ss"))
620                                 i2c_opt.mode = I2C_MODE_STOP_START;
621                         else if (!strcmp(optarg, "rs"))
622                                 i2c_opt.mode = I2C_MODE_REPEATED_START;
623                         else
624                                 usage();
625                         break;
626                 case 'n':
627                         i2c_opt.skip = 1;
628                         skip_addr = optarg;
629                         break;
630                 case 's':
631                         i2c_opt.scan = 1;
632                         break;
633                 case 'b':
634                         i2c_opt.binary = 1;
635                         break;
636                 case 'v':
637                         i2c_opt.verbose = 1;
638                         break;
639                 case 'r':
640                         i2c_opt.reset = 1;
641                         break;
642                 case 'h':
643                 default:
644                         usage();
645                 }
646         }
647         argc -= optind;
648         argv += optind;
649
650         /* Set default mode if option -m is not specified */
651         if (i2c_opt.mode == I2C_MODE_NOTSET) {
652                 if (i2c_opt.dir == 'r')
653                         i2c_opt.mode = I2C_MODE_STOP_START;
654                 else if (i2c_opt.dir == 'w')
655                         i2c_opt.mode = I2C_MODE_NONE;
656         }
657
658         /* Basic sanity check of command line arguments */
659         if (i2c_opt.scan) {
660                 if (i2c_opt.addr_set)
661                         usage();
662         } else if (i2c_opt.reset) {
663                 if (i2c_opt.addr_set)
664                         usage();
665         } else if (error) {
666                 usage();
667         } else if ((i2c_opt.dir == 'r' || i2c_opt.dir == 'w')) {
668                 if ((i2c_opt.addr_set == 0) ||
669                     !(i2c_opt.width == 0 || i2c_opt.width == 8 ||
670                     i2c_opt.width == 16))
671                 usage();
672         }
673
674         if (i2c_opt.verbose)
675                 fprintf(stderr, "dev: %s, addr: 0x%x, r/w: %c, "
676                     "offset: 0x%02x, width: %u, count: %u\n", dev,
677                     i2c_opt.addr >> 1, i2c_opt.dir, i2c_opt.off,
678                     i2c_opt.width, i2c_opt.count);
679
680         if (i2c_opt.scan)
681                 exit(scan_bus(cmd, dev, i2c_opt.skip, skip_addr));
682
683         if (i2c_opt.reset)
684                 exit(reset_bus(cmd, dev));
685
686         i2c_buf = malloc(i2c_opt.count);
687         if (i2c_buf == NULL)
688                 err(1, "data malloc");
689
690         if (i2c_opt.dir == 'w') {
691                 error = i2c_write(dev, i2c_opt, i2c_buf);
692                 if (error) {
693                         free(i2c_buf);
694                         return (1);
695                 }
696         }
697         if (i2c_opt.dir == 'r') {
698                 error = i2c_read(dev, i2c_opt, i2c_buf);
699                 if (error) {
700                         free(i2c_buf);
701                         return (1);
702                 }
703         }
704
705         if (i2c_opt.verbose)
706                 fprintf(stderr, "\nData %s (hex):\n", i2c_opt.dir == 'r' ?
707                     "read" : "written");
708
709         i = 0;
710         j = 0;
711         while (i < i2c_opt.count) {
712                 if (i2c_opt.verbose || (i2c_opt.dir == 'r' &&
713                     !i2c_opt.binary))
714                         fprintf (stderr, "%02hhx ", i2c_buf[i++]);
715
716                 if (i2c_opt.dir == 'r' && i2c_opt.binary) {
717                         fprintf(stdout, "%c", i2c_buf[j++]);
718                         if(!i2c_opt.verbose)
719                                 i++;
720                 }
721                 if (!i2c_opt.verbose && (i2c_opt.dir == 'w'))
722                         break;
723                 if ((i % chunk_size) == 0)
724                         fprintf(stderr, "\n");
725         }
726         if ((i % chunk_size) != 0)
727                 fprintf(stderr, "\n");
728
729         free(i2c_buf);
730         return (0);
731 }