]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/i2c/i2c.c
THIS BRANCH IS OBSOLETE, PLEASE READ:
[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 #define I2C_MODE_TRANSFER       4
51
52 struct options {
53         int     width;
54         int     count;
55         int     verbose;
56         int     addr_set;
57         int     binary;
58         int     scan;
59         int     skip;
60         int     reset;
61         int     mode;
62         char    dir;
63         uint32_t        addr;
64         uint32_t        off;
65 };
66
67 struct skip_range {
68         int     start;
69         int     end;
70 };
71
72 __dead2 static void
73 usage(void)
74 {
75
76         fprintf(stderr, "usage: %s -a addr [-f device] [-d [r|w]] [-o offset] "
77             "[-w [0|8|16]] [-c count] [-m [tr|ss|rs|no]] [-b] [-v]\n",
78             getprogname());
79         fprintf(stderr, "       %s -s [-f device] [-n skip_addr] -v\n",
80             getprogname());
81         fprintf(stderr, "       %s -r [-f device] -v\n", getprogname());
82         exit(EX_USAGE);
83 }
84
85 static struct skip_range
86 skip_get_range(char *skip_addr)
87 {
88         struct skip_range addr_range;
89         char *token;
90
91         addr_range.start = 0;
92         addr_range.end = 0;
93
94         token = strsep(&skip_addr, "..");
95         if (token) {
96                 addr_range.start = strtoul(token, 0, 16);
97                 token = strsep(&skip_addr, "..");
98                 if ((token != NULL) && !atoi(token)) {
99                         token = strsep(&skip_addr, "..");
100                         if (token)
101                                 addr_range.end = strtoul(token, 0, 16);
102                 }
103         }
104
105         return (addr_range);
106 }
107
108 /* Parse the string to get hex 7 bits addresses */
109 static int
110 skip_get_tokens(char *skip_addr, int *sk_addr, int max_index)
111 {
112         char *token;
113         int i;
114
115         for (i = 0; i < max_index; i++) {
116                 token = strsep(&skip_addr, ":");
117                 if (token == NULL)
118                         break;
119                 sk_addr[i] = strtoul(token, 0, 16);
120         }
121         return (i);
122 }
123
124 static int
125 scan_bus(struct iiccmd cmd, char *dev, int skip, char *skip_addr)
126 {
127         struct iic_msg rdmsg;
128         struct iic_rdwr_data rdwrdata;
129         struct skip_range addr_range = { 0, 0 };
130         int *tokens, fd, error, i, index, j;
131         int len = 0, do_skip = 0, no_range = 1, num_found = 0, use_read_xfer = 0;
132         uint8_t rdbyte;
133
134         fd = open(dev, O_RDWR);
135         if (fd == -1) {
136                 fprintf(stderr, "Error opening I2C controller (%s) for "
137                     "scanning: %s\n", dev, strerror(errno));
138                 return (EX_NOINPUT);
139         }
140
141         if (skip) {
142                 len = strlen(skip_addr);
143                 if (strstr(skip_addr, "..") != NULL) {
144                         addr_range = skip_get_range(skip_addr);
145                         no_range = 0;
146                 } else {
147                         tokens = (int *)malloc((len / 2 + 1) * sizeof(int));
148                         if (tokens == NULL) {
149                                 fprintf(stderr, "Error allocating tokens "
150                                     "buffer\n");
151                                 error = -1;
152                                 goto out;
153                         }
154                         index = skip_get_tokens(skip_addr, tokens,
155                             len / 2 + 1);
156                 }
157
158                 if (!no_range && (addr_range.start > addr_range.end)) {
159                         fprintf(stderr, "Skip address out of range\n");
160                         error = -1;
161                         goto out;
162                 }
163         }
164
165         printf("Scanning I2C devices on %s: ", dev);
166
167 start_over:
168         if (use_read_xfer) {
169                 fprintf(stderr, 
170                     "Hardware may not support START/STOP scanning; "
171                     "trying less-reliable read method.\n");
172         }
173
174         for (i = 1; i < 127; i++) {
175
176                 if (skip && ( addr_range.start < addr_range.end)) {
177                         if (i >= addr_range.start && i <= addr_range.end)
178                                 continue;
179
180                 } else if (skip && no_range)
181                         for (j = 0; j < index; j++) {
182                                 if (tokens[j] == i) {
183                                         do_skip = 1;
184                                         break;
185                                 }
186                         }
187
188                 if (do_skip) {
189                         do_skip = 0;
190                         continue;
191                 }
192
193                 cmd.slave = i << 1;
194                 cmd.last = 1;
195                 cmd.count = 0;
196                 error = ioctl(fd, I2CRSTCARD, &cmd);
197                 if (error) {
198                         fprintf(stderr, "Controller reset failed\n");
199                         goto out;
200                 }
201                 if (use_read_xfer) {
202                         rdmsg.buf = &rdbyte;
203                         rdmsg.len = 1;
204                         rdmsg.flags = IIC_M_RD;
205                         rdmsg.slave = i << 1;
206                         rdwrdata.msgs = &rdmsg;
207                         rdwrdata.nmsgs = 1;
208                         error = ioctl(fd, I2CRDWR, &rdwrdata);
209                 } else {
210                         cmd.slave = i << 1;
211                         cmd.last = 1;
212                         error = ioctl(fd, I2CSTART, &cmd);
213                         if (errno == ENODEV || errno == EOPNOTSUPP) {
214                                 /* If START not supported try reading. */
215                                 use_read_xfer = 1;
216                                 goto start_over;
217                         }
218                         ioctl(fd, I2CSTOP);
219                 }
220                 if (error == 0) {
221                         ++num_found;
222                         printf("%02x ", i);
223                 }
224         }
225
226         /*
227          * If we found nothing, maybe START is not supported and returns a
228          * generic error code such as EIO or ENXIO, so try again using reads.
229          */
230         if (num_found == 0) {
231                 if (!use_read_xfer) {
232                         use_read_xfer = 1;
233                         goto start_over;
234                 }
235                 printf("<none found>");
236         }
237         printf("\n");
238
239         error = ioctl(fd, I2CRSTCARD, &cmd);
240 out:
241         close(fd);
242         if (skip && no_range)
243                 free(tokens);
244
245         if (error) {
246                 fprintf(stderr, "Error scanning I2C controller (%s): %s\n",
247                     dev, strerror(errno));
248                 return (EX_NOINPUT);
249         } else
250                 return (EX_OK);
251 }
252
253 static int
254 reset_bus(struct iiccmd cmd, char *dev)
255 {
256         int fd, error;
257
258         fd = open(dev, O_RDWR);
259         if (fd == -1) {
260                 fprintf(stderr, "Error opening I2C controller (%s) for "
261                     "resetting: %s\n", dev, strerror(errno));
262                 return (EX_NOINPUT);
263         }
264
265         printf("Resetting I2C controller on %s: ", dev);
266         error = ioctl(fd, I2CRSTCARD, &cmd);
267         close (fd);
268
269         if (error) {
270                 printf("error: %s\n", strerror(errno));
271                 return (EX_IOERR);
272         } else {
273                 printf("OK\n");
274                 return (EX_OK);
275         }
276 }
277
278 static char *
279 prepare_buf(int size, uint32_t off)
280 {
281         char *buf;
282
283         buf = malloc(size);
284         if (buf == NULL)
285                 return (buf);
286
287         if (size == 1)
288                 buf[0] = off & 0xff;
289         else if (size == 2) {
290                 buf[0] = (off >> 8) & 0xff;
291                 buf[1] = off & 0xff;
292         }
293
294         return (buf);
295 }
296
297 static int
298 i2c_write(char *dev, struct options i2c_opt, char *i2c_buf)
299 {
300         struct iiccmd cmd;
301         int error, fd, bufsize;
302         char *err_msg, *buf;
303
304         fd = open(dev, O_RDWR);
305         if (fd == -1) {
306                 free(i2c_buf);
307                 err(1, "open failed");
308         }
309
310         cmd.slave = i2c_opt.addr;
311         error = ioctl(fd, I2CSTART, &cmd);
312         if (error == -1) {
313                 err_msg = "ioctl: error sending start condition";
314                 goto err1;
315         }
316
317         if (i2c_opt.width) {
318                 bufsize = i2c_opt.width / 8;
319                 buf = prepare_buf(bufsize, i2c_opt.off);
320                 if (buf == NULL) {
321                         err_msg = "error: offset malloc";
322                         goto err1;
323                 }
324         } else {
325                 bufsize = 0;
326                 buf = NULL;
327         }
328
329         switch(i2c_opt.mode) {
330         case I2C_MODE_STOP_START:
331                 /*
332                  * Write offset where the data will go
333                  */
334                 if (i2c_opt.width) {
335                         cmd.count = bufsize;
336                         cmd.buf = buf;
337                         error = ioctl(fd, I2CWRITE, &cmd);
338                         free(buf);
339                         if (error == -1) {
340                                 err_msg = "ioctl: error writing offset";
341                                 goto err1;
342                         }
343                 }
344
345                 error = ioctl(fd, I2CSTOP);
346                 if (error == -1) {
347                         err_msg = "ioctl: error sending stop condition";
348                         goto err2;
349                 }
350                 cmd.slave = i2c_opt.addr;
351                 error = ioctl(fd, I2CSTART, &cmd);
352                 if (error == -1) {
353                         err_msg = "ioctl: error sending start condition";
354                         goto err1;
355                 }
356
357                 /*
358                  * Write the data
359                  */
360                 cmd.count = i2c_opt.count;
361                 cmd.buf = i2c_buf;
362                 cmd.last = 0;
363                 error = ioctl(fd, I2CWRITE, &cmd);
364                 if (error == -1) {
365                         err_msg = "ioctl: error writing";
366                         goto err1;
367                 }
368                 break;
369
370         case I2C_MODE_REPEATED_START:
371                 /*
372                  * Write offset where the data will go
373                  */
374                 if (i2c_opt.width) {
375                         cmd.count = bufsize;
376                         cmd.buf = buf;
377                         error = ioctl(fd, I2CWRITE, &cmd);
378                         free(buf);
379                         if (error == -1) {
380                                 err_msg = "ioctl: error writing offset";
381                                 goto err1;
382                         }
383                 }
384
385                 cmd.slave = i2c_opt.addr;
386                 error = ioctl(fd, I2CRPTSTART, &cmd);
387                 if (error == -1) {
388                         err_msg = "ioctl: error sending repeated start "
389                             "condition";
390                         goto err1;
391                 }
392
393                 /*
394                  * Write the data
395                  */
396                 cmd.count = i2c_opt.count;
397                 cmd.buf = i2c_buf;
398                 cmd.last = 0;
399                 error = ioctl(fd, I2CWRITE, &cmd);
400                 if (error == -1) {
401                         err_msg = "ioctl: error writing";
402                         goto err1;
403                 }
404                 break;
405
406         case I2C_MODE_NONE: /* fall through */
407         default:                
408                 buf = realloc(buf, bufsize + i2c_opt.count);
409                 if (buf == NULL) {
410                         err_msg = "error: data malloc";
411                         goto err1;
412                 }
413
414                 memcpy(buf + bufsize, i2c_buf, i2c_opt.count);
415                 /*
416                  * Write offset and data
417                  */
418                 cmd.count = bufsize + i2c_opt.count;
419                 cmd.buf = buf;
420                 cmd.last = 0;
421                 error = ioctl(fd, I2CWRITE, &cmd);
422                 free(buf);
423                 if (error == -1) {
424                         err_msg = "ioctl: error writing";
425                         goto err1;
426                 }
427                 break;
428         }
429         error = ioctl(fd, I2CSTOP);
430         if (error == -1) {
431                 err_msg = "ioctl: error sending stop condition";
432                 goto err2;
433         }
434
435         close(fd);
436         return (0);
437
438 err1:
439         error = ioctl(fd, I2CSTOP);
440         if (error == -1)
441                 fprintf(stderr, "error sending stop condition\n");
442 err2:
443         if (err_msg)
444                 fprintf(stderr, "%s\n", err_msg);
445
446         close(fd);
447         return (1);
448 }
449
450 static int
451 i2c_read(char *dev, struct options i2c_opt, char *i2c_buf)
452 {
453         struct iiccmd cmd;
454         int fd, error, bufsize;
455         char *err_msg, data = 0, *buf;
456
457         fd = open(dev, O_RDWR);
458         if (fd == -1)
459                 err(1, "open failed");
460
461         bzero(&cmd, sizeof(cmd));
462
463         if (i2c_opt.width) {
464                 cmd.slave = i2c_opt.addr;
465                 cmd.count = 1;
466                 cmd.last = 0;
467                 cmd.buf = &data;
468                 error = ioctl(fd, I2CSTART, &cmd);
469                 if (error == -1) {
470                         err_msg = "ioctl: error sending start condition";
471                         goto err1;
472                 }
473                 bufsize = i2c_opt.width / 8;
474                 buf = prepare_buf(bufsize, i2c_opt.off);
475                 if (buf == NULL) {
476                         err_msg = "error: offset malloc";
477                         goto err1;
478                 }
479
480                 cmd.count = bufsize;
481                 cmd.buf = buf;
482                 cmd.last = 0;
483                 error = ioctl(fd, I2CWRITE, &cmd);
484                 free(buf);
485                 if (error == -1) {
486                         err_msg = "ioctl: error writing offset";
487                         goto err1;
488                 }
489
490                 if (i2c_opt.mode == I2C_MODE_STOP_START) {
491                         error = ioctl(fd, I2CSTOP);
492                         if (error == -1) {
493                                 err_msg = "error sending stop condition";
494                                 goto err2;
495                         }
496                 }
497         }
498         cmd.slave = i2c_opt.addr | 1;
499         cmd.count = 1;
500         cmd.last = 0;
501         cmd.buf = &data;
502         if (i2c_opt.mode == I2C_MODE_STOP_START || i2c_opt.width == 0) {
503                 error = ioctl(fd, I2CSTART, &cmd);
504                 if (error == -1) {
505                         err_msg = "ioctl: error sending start condition";
506                         goto err2;
507                 }
508         } else if (i2c_opt.mode == I2C_MODE_REPEATED_START) {
509                 error = ioctl(fd, I2CRPTSTART, &cmd);
510                 if (error == -1) {
511                         err_msg = "ioctl: error sending repeated start "
512                             "condition";
513                         goto err1;
514                 }
515         }
516
517         cmd.count = i2c_opt.count;
518         cmd.buf = i2c_buf;
519         cmd.last = 1;
520         error = ioctl(fd, I2CREAD, &cmd);
521         if (error == -1) {
522                 err_msg = "ioctl: error while reading";
523                 goto err1;
524         }
525
526         error = ioctl(fd, I2CSTOP);
527         if (error == -1) {
528                 err_msg = "error sending stop condtion\n";
529                 goto err2;
530         }
531
532         close(fd);
533         return (0);
534
535 err1:
536         error = ioctl(fd, I2CSTOP);
537         if (error == -1)
538                 fprintf(stderr, "error sending stop condition\n");
539 err2:
540         if (err_msg)
541                 fprintf(stderr, "%s\n", err_msg);
542
543         close(fd);
544         return (1);
545 }
546
547 /*
548  * i2c_rdwr_transfer() - use I2CRDWR to conduct a complete i2c transfer.
549  *
550  * Some i2c hardware is unable to provide direct control over START, REPEAT-
551  * START, and STOP operations.  Such hardware can only perform a complete
552  * START-<data>-STOP or START-<data>-REPEAT-START-<data>-STOP sequence as a
553  * single operation.  The driver framework refers to this sequence as a
554  * "transfer" so we call it "transfer mode".  We assemble either one or two
555  * iic_msg structures to describe the IO operations, and hand them off to the
556  * driver to be handled as a single transfer.
557  */
558 static int
559 i2c_rdwr_transfer(char *dev, struct options i2c_opt, char *i2c_buf)
560 {
561         struct iic_msg msgs[2];
562         struct iic_rdwr_data xfer;
563         int fd, i;
564         union {
565                 uint8_t  buf[2];
566                 uint8_t  off8;
567                 uint16_t off16;
568         } off;
569
570         i = 0;
571         if (i2c_opt.width > 0) {
572                 msgs[i].flags = IIC_M_WR | IIC_M_NOSTOP;
573                 msgs[i].slave = i2c_opt.addr;
574                 msgs[i].buf   = off.buf;
575                 if (i2c_opt.width == 8) {
576                         off.off8 = (uint8_t)i2c_opt.off;
577                         msgs[i].len = 1;
578                 } else {
579                         off.off16 = (uint16_t)i2c_opt.off;
580                         msgs[i].len = 2;
581                 }
582                 ++i;
583         }
584
585         /*
586          * If the transfer direction is write and we did a write of the offset
587          * above, then we need to elide the start; this transfer is just more
588          * writing that follows the one started above.  For a read, we always do
589          * a start; if we did an offset write above it'll be a repeat-start
590          * because of the NOSTOP flag used above.
591          */
592         if (i2c_opt.dir == 'w')
593                 msgs[i].flags = IIC_M_WR | ((i > 0) ? IIC_M_NOSTART : 0);
594         else
595                 msgs[i].flags = IIC_M_RD;
596         msgs[i].slave = i2c_opt.addr;
597         msgs[i].len   = i2c_opt.count;
598         msgs[i].buf   = i2c_buf;
599         ++i;
600
601         xfer.msgs = msgs;
602         xfer.nmsgs = i;
603
604         if ((fd = open(dev, O_RDWR)) == -1)
605                 err(1, "open(%s) failed", dev);
606         if (ioctl(fd, I2CRDWR, &xfer) == -1 )
607                 err(1, "ioctl(I2CRDWR) failed");
608         close(fd);
609
610         return (0);
611 }
612
613 int
614 main(int argc, char** argv)
615 {
616         struct iiccmd cmd;
617         struct options i2c_opt;
618         char *dev, *skip_addr, *i2c_buf;
619         int error, chunk_size, i, j, ch;
620
621         errno = 0;
622         error = 0;
623
624         /* Line-break the output every chunk_size bytes */
625         chunk_size = 16;
626
627         dev = I2C_DEV;
628
629         /* Default values */
630         i2c_opt.addr_set = 0;
631         i2c_opt.off = 0;
632         i2c_opt.verbose = 0;
633         i2c_opt.dir = 'r';      /* direction = read */
634         i2c_opt.width = 8;
635         i2c_opt.count = 1;
636         i2c_opt.binary = 0;     /* ASCII text output */
637         i2c_opt.scan = 0;       /* no bus scan */
638         i2c_opt.skip = 0;       /* scan all addresses */
639         i2c_opt.reset = 0;      /* no bus reset */
640         i2c_opt.mode = I2C_MODE_NOTSET;
641
642         while ((ch = getopt(argc, argv, "a:f:d:o:w:c:m:n:sbvrh")) != -1) {
643                 switch(ch) {
644                 case 'a':
645                         i2c_opt.addr = (strtoul(optarg, 0, 16) << 1);
646                         if (i2c_opt.addr == 0 && errno == EINVAL)
647                                 i2c_opt.addr_set = 0;
648                         else
649                                 i2c_opt.addr_set = 1;
650                         break;
651                 case 'f':
652                         dev = optarg;
653                         break;
654                 case 'd':
655                         i2c_opt.dir = optarg[0];
656                         break;
657                 case 'o':
658                         i2c_opt.off = strtoul(optarg, 0, 16);
659                         if (i2c_opt.off == 0 && errno == EINVAL)
660                                 error = 1;
661                         break;
662                 case 'w':
663                         i2c_opt.width = atoi(optarg);
664                         break;
665                 case 'c':
666                         i2c_opt.count = atoi(optarg);
667                         break;
668                 case 'm':
669                         if (!strcmp(optarg, "no"))
670                                 i2c_opt.mode = I2C_MODE_NONE;
671                         else if (!strcmp(optarg, "ss"))
672                                 i2c_opt.mode = I2C_MODE_STOP_START;
673                         else if (!strcmp(optarg, "rs"))
674                                 i2c_opt.mode = I2C_MODE_REPEATED_START;
675                         else if (!strcmp(optarg, "tr"))
676                                 i2c_opt.mode = I2C_MODE_TRANSFER;
677                         else
678                                 usage();
679                         break;
680                 case 'n':
681                         i2c_opt.skip = 1;
682                         skip_addr = optarg;
683                         break;
684                 case 's':
685                         i2c_opt.scan = 1;
686                         break;
687                 case 'b':
688                         i2c_opt.binary = 1;
689                         break;
690                 case 'v':
691                         i2c_opt.verbose = 1;
692                         break;
693                 case 'r':
694                         i2c_opt.reset = 1;
695                         break;
696                 case 'h':
697                 default:
698                         usage();
699                 }
700         }
701         argc -= optind;
702         argv += optind;
703
704         /* Set default mode if option -m is not specified */
705         if (i2c_opt.mode == I2C_MODE_NOTSET) {
706                 if (i2c_opt.dir == 'r')
707                         i2c_opt.mode = I2C_MODE_STOP_START;
708                 else if (i2c_opt.dir == 'w')
709                         i2c_opt.mode = I2C_MODE_NONE;
710         }
711
712         /* Basic sanity check of command line arguments */
713         if (i2c_opt.scan) {
714                 if (i2c_opt.addr_set)
715                         usage();
716         } else if (i2c_opt.reset) {
717                 if (i2c_opt.addr_set)
718                         usage();
719         } else if (error) {
720                 usage();
721         } else if ((i2c_opt.dir == 'r' || i2c_opt.dir == 'w')) {
722                 if ((i2c_opt.addr_set == 0) ||
723                     !(i2c_opt.width == 0 || i2c_opt.width == 8 ||
724                     i2c_opt.width == 16))
725                 usage();
726         }
727
728         if (i2c_opt.verbose)
729                 fprintf(stderr, "dev: %s, addr: 0x%x, r/w: %c, "
730                     "offset: 0x%02x, width: %u, count: %u\n", dev,
731                     i2c_opt.addr >> 1, i2c_opt.dir, i2c_opt.off,
732                     i2c_opt.width, i2c_opt.count);
733
734         if (i2c_opt.scan)
735                 exit(scan_bus(cmd, dev, i2c_opt.skip, skip_addr));
736
737         if (i2c_opt.reset)
738                 exit(reset_bus(cmd, dev));
739
740         i2c_buf = malloc(i2c_opt.count);
741         if (i2c_buf == NULL)
742                 err(1, "data malloc");
743
744         /*
745          * For a write, read the data to be written to the chip from stdin.
746          */
747         if (i2c_opt.dir == 'w') {
748                 if (i2c_opt.verbose && !i2c_opt.binary)
749                         fprintf(stderr, "Enter %u bytes of data: ",
750                             i2c_opt.count);
751                 for (i = 0; i < i2c_opt.count; i++) {
752                         ch = getchar();
753                         if (ch == EOF) {
754                                 free(i2c_buf);
755                                 err(1, "not enough data, exiting\n");
756                         }
757                         i2c_buf[i] = ch;
758                 }
759         }
760
761         if (i2c_opt.mode == I2C_MODE_TRANSFER)
762                 error = i2c_rdwr_transfer(dev, i2c_opt, i2c_buf);
763         else if (i2c_opt.dir == 'w')
764                 error = i2c_write(dev, i2c_opt, i2c_buf);
765         else
766                 error = i2c_read(dev, i2c_opt, i2c_buf);
767
768         if (error != 0) {
769                 free(i2c_buf);
770                 return (1);
771         }
772
773         if (i2c_opt.verbose)
774                 fprintf(stderr, "\nData %s (hex):\n", i2c_opt.dir == 'r' ?
775                     "read" : "written");
776
777         i = 0;
778         j = 0;
779         while (i < i2c_opt.count) {
780                 if (i2c_opt.verbose || (i2c_opt.dir == 'r' &&
781                     !i2c_opt.binary))
782                         fprintf (stderr, "%02hhx ", i2c_buf[i++]);
783
784                 if (i2c_opt.dir == 'r' && i2c_opt.binary) {
785                         fprintf(stdout, "%c", i2c_buf[j++]);
786                         if(!i2c_opt.verbose)
787                                 i++;
788                 }
789                 if (!i2c_opt.verbose && (i2c_opt.dir == 'w'))
790                         break;
791                 if ((i % chunk_size) == 0)
792                         fprintf(stderr, "\n");
793         }
794         if ((i % chunk_size) != 0)
795                 fprintf(stderr, "\n");
796
797         free(i2c_buf);
798         return (0);
799 }