2009-01-26 14:00:50 +00:00
|
|
|
/*-
|
2017-11-27 15:37:16 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause-FreeBSD
|
|
|
|
*
|
2009-01-26 14:00:50 +00:00
|
|
|
* Copyright (C) 2008-2009 Semihalf, Michal Hajduk and Bartlomiej Sieka
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
2021-05-11 22:59:23 +00:00
|
|
|
#include <assert.h>
|
2021-05-13 10:55:37 +00:00
|
|
|
#include <ctype.h>
|
2009-01-26 14:00:50 +00:00
|
|
|
#include <err.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <sysexits.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
2021-05-11 23:07:06 +00:00
|
|
|
#include <sys/endian.h>
|
2009-01-26 14:00:50 +00:00
|
|
|
#include <sys/ioctl.h>
|
|
|
|
|
|
|
|
#include <dev/iicbus/iic.h>
|
|
|
|
|
|
|
|
#define I2C_DEV "/dev/iic0"
|
|
|
|
#define I2C_MODE_NOTSET 0
|
|
|
|
#define I2C_MODE_NONE 1
|
|
|
|
#define I2C_MODE_STOP_START 2
|
|
|
|
#define I2C_MODE_REPEATED_START 3
|
2019-05-22 21:06:10 +00:00
|
|
|
#define I2C_MODE_TRANSFER 4
|
2009-01-26 14:00:50 +00:00
|
|
|
|
|
|
|
struct options {
|
2021-05-12 20:01:14 +00:00
|
|
|
const char *width;
|
2021-05-13 11:58:50 +00:00
|
|
|
unsigned count;
|
2021-05-12 20:01:14 +00:00
|
|
|
int verbose;
|
|
|
|
int binary;
|
2021-05-13 10:55:37 +00:00
|
|
|
const char *skip;
|
2021-05-12 20:01:14 +00:00
|
|
|
int mode;
|
|
|
|
char dir;
|
2009-01-26 14:00:50 +00:00
|
|
|
uint32_t addr;
|
|
|
|
uint32_t off;
|
2021-05-12 20:01:14 +00:00
|
|
|
uint8_t off_buf[2];
|
|
|
|
size_t off_len;
|
2009-01-26 14:00:50 +00:00
|
|
|
};
|
|
|
|
|
2021-05-19 18:56:59 +00:00
|
|
|
#define N_FDCACHE 128
|
|
|
|
static int fd_cache[N_FDCACHE];
|
|
|
|
|
2009-01-26 14:00:50 +00:00
|
|
|
__dead2 static void
|
2021-05-12 21:34:58 +00:00
|
|
|
usage(const char *msg)
|
2009-01-26 14:00:50 +00:00
|
|
|
{
|
|
|
|
|
2021-05-12 21:34:58 +00:00
|
|
|
if (msg != NULL)
|
|
|
|
fprintf(stderr, "%s\n", msg);
|
2009-01-26 14:00:50 +00:00
|
|
|
fprintf(stderr, "usage: %s -a addr [-f device] [-d [r|w]] [-o offset] "
|
2021-05-12 21:37:00 +00:00
|
|
|
"[-w [0|8|16|16LE|16BE]] [-c count] [-m [tr|ss|rs|no]] [-b] [-v]\n",
|
2009-01-26 14:00:50 +00:00
|
|
|
getprogname());
|
|
|
|
fprintf(stderr, " %s -s [-f device] [-n skip_addr] -v\n",
|
|
|
|
getprogname());
|
|
|
|
fprintf(stderr, " %s -r [-f device] -v\n", getprogname());
|
|
|
|
exit(EX_USAGE);
|
|
|
|
}
|
|
|
|
|
2021-05-13 11:58:50 +00:00
|
|
|
static int
|
|
|
|
i2c_do_stop(int fd)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
i = ioctl(fd, I2CSTOP);
|
|
|
|
if (i < 0)
|
|
|
|
fprintf(stderr, "ioctl: error sending stop condition: %s\n",
|
|
|
|
strerror(errno));
|
|
|
|
return (i);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
i2c_do_start(int fd, struct iiccmd *cmd)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
i = ioctl(fd, I2CSTART, cmd);
|
|
|
|
if (i < 0)
|
|
|
|
fprintf(stderr, "ioctl: error sending start condition: %s\n",
|
|
|
|
strerror(errno));
|
|
|
|
return (i);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
i2c_do_repeatstart(int fd, struct iiccmd *cmd)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
i = ioctl(fd, I2CRPTSTART, cmd);
|
|
|
|
if (i < 0)
|
|
|
|
fprintf(stderr, "ioctl: error sending repeated start: %s\n",
|
|
|
|
strerror(errno));
|
|
|
|
return (i);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
i2c_do_write(int fd, struct iiccmd *cmd)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
i = ioctl(fd, I2CWRITE, cmd);
|
|
|
|
if (i < 0)
|
|
|
|
fprintf(stderr, "ioctl: error writing: %s\n",
|
|
|
|
strerror(errno));
|
|
|
|
return (i);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
i2c_do_read(int fd, struct iiccmd *cmd)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
i = ioctl(fd, I2CREAD, cmd);
|
|
|
|
if (i < 0)
|
|
|
|
fprintf(stderr, "ioctl: error reading: %s\n",
|
|
|
|
strerror(errno));
|
|
|
|
return (i);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
i2c_do_reset(int fd)
|
|
|
|
{
|
|
|
|
struct iiccmd cmd;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
memset(&cmd, 0, sizeof cmd);
|
|
|
|
i = ioctl(fd, I2CRSTCARD, &cmd);
|
|
|
|
if (i < 0)
|
|
|
|
fprintf(stderr, "ioctl: error resetting controller: %s\n",
|
|
|
|
strerror(errno));
|
|
|
|
return (i);
|
|
|
|
}
|
|
|
|
|
2021-05-13 10:55:37 +00:00
|
|
|
static void
|
|
|
|
parse_skip(const char *skip, char blacklist[128])
|
2009-01-26 14:00:50 +00:00
|
|
|
{
|
2021-05-13 10:55:37 +00:00
|
|
|
const char *p;
|
|
|
|
unsigned x, y;
|
|
|
|
|
|
|
|
for (p = skip; *p != '\0';) {
|
|
|
|
if (*p == '0' && p[1] == 'x')
|
|
|
|
p += 2;
|
|
|
|
if (!isxdigit(*p))
|
|
|
|
usage("Bad -n argument, expected (first) hex-digit");
|
|
|
|
x = digittoint(*p++) << 4;
|
|
|
|
if (!isxdigit(*p))
|
|
|
|
usage("Bad -n argument, expected (second) hex-digit");
|
|
|
|
x |= digittoint(*p++);
|
|
|
|
if (x == 0 || x > 0x7f)
|
|
|
|
usage("Bad -n argument, (01..7f)");
|
|
|
|
if (*p == ':' || *p == ',' || *p == '\0') {
|
|
|
|
blacklist[x] = 1;
|
|
|
|
if (*p != '\0')
|
|
|
|
p++;
|
|
|
|
continue;
|
2009-01-26 14:00:50 +00:00
|
|
|
}
|
2021-05-13 10:55:37 +00:00
|
|
|
if (*p == '-') {
|
|
|
|
p++;
|
|
|
|
} else if (*p == '.' && p[1] == '.') {
|
|
|
|
p += 2;
|
|
|
|
} else {
|
|
|
|
usage("Bad -n argument, ([:|,|..|-])");
|
|
|
|
}
|
|
|
|
if (*p == '0' && p[1] == 'x')
|
|
|
|
p += 2;
|
|
|
|
if (!isxdigit(*p))
|
|
|
|
usage("Bad -n argument, expected (first) hex-digit");
|
|
|
|
y = digittoint(*p++) << 4;
|
|
|
|
if (!isxdigit(*p))
|
|
|
|
usage("Bad -n argument, expected (second) hex-digit");
|
|
|
|
y |= digittoint(*p++);
|
|
|
|
if (y == 0 || y > 0x7f)
|
|
|
|
usage("Bad -n argument, (01..7f)");
|
|
|
|
if (y < x)
|
|
|
|
usage("Bad -n argument, (end < start)");
|
|
|
|
for (;x <= y; x++)
|
|
|
|
blacklist[x] = 1;
|
|
|
|
if (*p == ':' || *p == ',')
|
|
|
|
p++;
|
2009-01-26 14:00:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2021-05-13 11:58:50 +00:00
|
|
|
scan_bus(const char *dev, int fd, const char *skip, int verbose)
|
2009-01-26 14:00:50 +00:00
|
|
|
{
|
2021-05-11 22:59:23 +00:00
|
|
|
struct iiccmd cmd;
|
2017-09-11 21:49:38 +00:00
|
|
|
struct iic_msg rdmsg;
|
|
|
|
struct iic_rdwr_data rdwrdata;
|
2021-05-13 10:55:37 +00:00
|
|
|
int error;
|
|
|
|
unsigned u;
|
|
|
|
int num_found = 0, use_read_xfer;
|
2017-09-11 21:49:38 +00:00
|
|
|
uint8_t rdbyte;
|
2021-05-13 10:55:37 +00:00
|
|
|
char blacklist[128];
|
2021-05-13 11:58:50 +00:00
|
|
|
const char *sep = "";
|
2009-01-26 14:00:50 +00:00
|
|
|
|
2021-05-13 10:55:37 +00:00
|
|
|
memset(blacklist, 0, sizeof blacklist);
|
2017-09-11 21:49:38 +00:00
|
|
|
|
2021-05-13 10:55:37 +00:00
|
|
|
if (skip != NULL)
|
|
|
|
parse_skip(skip, blacklist);
|
2017-09-11 21:49:38 +00:00
|
|
|
|
2021-05-13 10:55:37 +00:00
|
|
|
for (use_read_xfer = 0; use_read_xfer < 2; use_read_xfer++) {
|
|
|
|
for (u = 1; u < 127; u++) {
|
|
|
|
if (blacklist[u])
|
2009-01-26 14:00:50 +00:00
|
|
|
continue;
|
|
|
|
|
2021-05-13 10:55:37 +00:00
|
|
|
cmd.slave = u << 1;
|
2017-09-11 21:49:38 +00:00
|
|
|
cmd.last = 1;
|
2021-05-13 10:55:37 +00:00
|
|
|
cmd.count = 0;
|
2021-05-13 11:58:50 +00:00
|
|
|
if (i2c_do_reset(fd))
|
2021-05-13 10:55:37 +00:00
|
|
|
return (EX_NOINPUT);
|
2021-05-13 11:58:50 +00:00
|
|
|
|
2021-05-13 10:55:37 +00:00
|
|
|
if (use_read_xfer) {
|
|
|
|
rdmsg.buf = &rdbyte;
|
|
|
|
rdmsg.len = 1;
|
|
|
|
rdmsg.flags = IIC_M_RD;
|
|
|
|
rdmsg.slave = u << 1;
|
|
|
|
rdwrdata.msgs = &rdmsg;
|
|
|
|
rdwrdata.nmsgs = 1;
|
|
|
|
error = ioctl(fd, I2CRDWR, &rdwrdata);
|
|
|
|
} else {
|
|
|
|
error = ioctl(fd, I2CSTART, &cmd);
|
|
|
|
if (errno == ENODEV || errno == EOPNOTSUPP)
|
|
|
|
break; /* Try reads instead */
|
|
|
|
(void)ioctl(fd, I2CSTOP);
|
|
|
|
}
|
|
|
|
if (error == 0) {
|
2021-05-13 11:58:50 +00:00
|
|
|
if (!num_found++ && verbose) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"Scanning I2C devices on %s:\n",
|
|
|
|
dev);
|
|
|
|
}
|
|
|
|
printf("%s%02x", sep, u);
|
|
|
|
sep = " ";
|
2017-09-11 21:49:38 +00:00
|
|
|
}
|
|
|
|
}
|
2021-05-13 10:55:37 +00:00
|
|
|
if (num_found > 0)
|
|
|
|
break;
|
2021-05-13 11:58:50 +00:00
|
|
|
if (verbose && !use_read_xfer)
|
|
|
|
fprintf(stderr,
|
|
|
|
"Hardware may not support START/STOP scanning; "
|
|
|
|
"trying less-reliable read method.\n");
|
2017-09-11 21:49:38 +00:00
|
|
|
}
|
2021-05-13 11:58:50 +00:00
|
|
|
if (num_found == 0 && verbose)
|
|
|
|
printf("<Nothing Found>");
|
2021-05-13 10:55:37 +00:00
|
|
|
|
2009-01-26 14:00:50 +00:00
|
|
|
printf("\n");
|
|
|
|
|
2021-05-13 11:58:50 +00:00
|
|
|
return (i2c_do_reset(fd));
|
2009-01-26 14:00:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2021-05-13 11:58:50 +00:00
|
|
|
reset_bus(const char *dev, int fd, int verbose)
|
2009-01-26 14:00:50 +00:00
|
|
|
{
|
|
|
|
|
2021-05-13 11:58:50 +00:00
|
|
|
if (verbose)
|
|
|
|
fprintf(stderr, "Resetting I2C controller on %s\n", dev);
|
|
|
|
return (i2c_do_reset(fd));
|
2009-01-26 14:00:50 +00:00
|
|
|
}
|
|
|
|
|
2021-05-12 20:01:14 +00:00
|
|
|
static const char *
|
2021-05-19 18:47:32 +00:00
|
|
|
encode_offset(const char *width, unsigned offset, uint8_t *dst, size_t *len)
|
2009-01-26 14:00:50 +00:00
|
|
|
{
|
|
|
|
|
2021-05-12 20:01:14 +00:00
|
|
|
if (!strcmp(width, "0")) {
|
|
|
|
*len = 0;
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
if (!strcmp(width, "8")) {
|
2021-05-19 18:47:32 +00:00
|
|
|
if (offset > 0xff)
|
|
|
|
return ("Invalid 8-bit offset\n");
|
|
|
|
*dst = offset;
|
2021-05-12 20:01:14 +00:00
|
|
|
*len = 1;
|
|
|
|
return (NULL);
|
2009-01-26 14:00:50 +00:00
|
|
|
}
|
2021-05-19 18:47:32 +00:00
|
|
|
if (offset > 0xffff)
|
|
|
|
return ("Invalid 16-bit offset\n");
|
2021-05-12 20:01:14 +00:00
|
|
|
if (!strcmp(width, "16LE") || !strcmp(width, "16")) {
|
2021-05-19 18:47:32 +00:00
|
|
|
le16enc(dst, offset);
|
2021-05-12 20:01:14 +00:00
|
|
|
*len = 2;
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
if (!strcmp(width, "16BE")) {
|
2021-05-19 18:47:32 +00:00
|
|
|
be16enc(dst, offset);
|
2021-05-12 20:01:14 +00:00
|
|
|
*len = 2;
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
return ("Invalid offset width, must be: 0|8|16|16LE|16BE\n");
|
|
|
|
}
|
2009-01-26 14:00:50 +00:00
|
|
|
|
2021-05-13 11:58:50 +00:00
|
|
|
static int
|
2021-05-12 20:01:14 +00:00
|
|
|
write_offset(int fd, struct options i2c_opt, struct iiccmd *cmd)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (i2c_opt.off_len > 0) {
|
|
|
|
cmd->count = i2c_opt.off_len;
|
2021-05-13 09:52:36 +00:00
|
|
|
cmd->buf = (void*)i2c_opt.off_buf;
|
2021-05-13 11:58:50 +00:00
|
|
|
return (i2c_do_write(fd, cmd));
|
2021-05-12 20:01:14 +00:00
|
|
|
}
|
2021-05-13 11:58:50 +00:00
|
|
|
return (0);
|
2009-01-26 14:00:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2021-05-13 11:58:50 +00:00
|
|
|
i2c_write(int fd, struct options i2c_opt, uint8_t *i2c_buf)
|
2009-01-26 14:00:50 +00:00
|
|
|
{
|
|
|
|
struct iiccmd cmd;
|
2021-05-13 11:58:50 +00:00
|
|
|
char buf[i2c_opt.off_len + i2c_opt.count];
|
2009-01-26 14:00:50 +00:00
|
|
|
|
2021-05-13 11:58:50 +00:00
|
|
|
memset(&cmd, 0, sizeof(cmd));
|
2009-01-26 14:00:50 +00:00
|
|
|
cmd.slave = i2c_opt.addr;
|
2021-05-13 11:58:50 +00:00
|
|
|
|
|
|
|
if (i2c_do_start(fd, &cmd))
|
|
|
|
return (i2c_do_stop(fd) | 1);
|
2009-01-26 14:00:50 +00:00
|
|
|
|
2015-09-15 11:21:16 +00:00
|
|
|
switch(i2c_opt.mode) {
|
|
|
|
case I2C_MODE_STOP_START:
|
2021-05-13 11:58:50 +00:00
|
|
|
if (write_offset(fd, i2c_opt, &cmd))
|
|
|
|
return (i2c_do_stop(fd) | 1);
|
|
|
|
|
|
|
|
if (i2c_do_stop(fd))
|
|
|
|
return (1);
|
|
|
|
|
|
|
|
if (i2c_do_start(fd, &cmd))
|
|
|
|
return (i2c_do_stop(fd) | 1);
|
2015-09-15 11:21:16 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Write the data
|
|
|
|
*/
|
|
|
|
cmd.count = i2c_opt.count;
|
2021-05-13 11:58:50 +00:00
|
|
|
cmd.buf = (void*)i2c_buf;
|
2015-09-15 11:21:16 +00:00
|
|
|
cmd.last = 0;
|
2021-05-13 11:58:50 +00:00
|
|
|
if (i2c_do_write(fd, &cmd))
|
|
|
|
return (i2c_do_stop(fd) | 1);
|
2015-09-15 11:21:16 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case I2C_MODE_REPEATED_START:
|
2021-05-13 11:58:50 +00:00
|
|
|
if (write_offset(fd, i2c_opt, &cmd))
|
|
|
|
return (i2c_do_stop(fd) | 1);
|
|
|
|
|
|
|
|
if (i2c_do_repeatstart(fd, &cmd))
|
|
|
|
return (i2c_do_stop(fd) | 1);
|
2009-01-26 14:00:50 +00:00
|
|
|
|
2015-09-15 11:21:16 +00:00
|
|
|
/*
|
|
|
|
* Write the data
|
|
|
|
*/
|
|
|
|
cmd.count = i2c_opt.count;
|
2021-05-13 11:58:50 +00:00
|
|
|
cmd.buf = (void*)i2c_buf;
|
2015-09-15 11:21:16 +00:00
|
|
|
cmd.last = 0;
|
2021-05-13 11:58:50 +00:00
|
|
|
if (i2c_do_write(fd, &cmd))
|
|
|
|
return (i2c_do_stop(fd) | 1);
|
2015-09-15 11:21:16 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case I2C_MODE_NONE: /* fall through */
|
2021-05-11 22:59:23 +00:00
|
|
|
default:
|
2021-05-12 20:01:14 +00:00
|
|
|
memcpy(buf, i2c_opt.off_buf, i2c_opt.off_len);
|
|
|
|
memcpy(buf + i2c_opt.off_len, i2c_buf, i2c_opt.count);
|
2015-09-15 11:21:16 +00:00
|
|
|
/*
|
|
|
|
* Write offset and data
|
|
|
|
*/
|
2021-05-12 20:01:14 +00:00
|
|
|
cmd.count = i2c_opt.off_len + i2c_opt.count;
|
2021-05-13 11:58:50 +00:00
|
|
|
cmd.buf = (void*)buf;
|
2015-09-15 11:21:16 +00:00
|
|
|
cmd.last = 0;
|
2021-05-13 11:58:50 +00:00
|
|
|
if (i2c_do_write(fd, &cmd))
|
|
|
|
return (i2c_do_stop(fd) | 1);
|
2015-09-15 11:21:16 +00:00
|
|
|
break;
|
2009-01-26 14:00:50 +00:00
|
|
|
}
|
|
|
|
|
2021-05-13 11:58:50 +00:00
|
|
|
return (i2c_do_stop(fd));
|
2009-01-26 14:00:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2021-05-13 11:58:50 +00:00
|
|
|
i2c_read(int fd, struct options i2c_opt, uint8_t *i2c_buf)
|
2009-01-26 14:00:50 +00:00
|
|
|
{
|
|
|
|
struct iiccmd cmd;
|
2021-05-12 20:01:14 +00:00
|
|
|
char data = 0;
|
2009-01-26 14:00:50 +00:00
|
|
|
|
2021-05-13 11:58:50 +00:00
|
|
|
memset(&cmd, 0, sizeof(cmd));
|
|
|
|
cmd.slave = i2c_opt.addr;
|
2009-01-26 14:00:50 +00:00
|
|
|
|
2021-05-12 20:01:14 +00:00
|
|
|
if (i2c_opt.off_len) {
|
2009-01-26 14:00:50 +00:00
|
|
|
cmd.count = 1;
|
|
|
|
cmd.last = 0;
|
|
|
|
cmd.buf = &data;
|
2021-05-13 11:58:50 +00:00
|
|
|
if (i2c_do_start(fd, &cmd))
|
|
|
|
return (i2c_do_stop(fd) | 1);
|
2009-01-26 14:00:50 +00:00
|
|
|
|
2021-05-13 11:58:50 +00:00
|
|
|
if (write_offset(fd, i2c_opt, &cmd))
|
|
|
|
return (i2c_do_stop(fd) | 1);
|
2009-01-26 14:00:50 +00:00
|
|
|
|
2021-05-13 11:58:50 +00:00
|
|
|
if (i2c_opt.mode == I2C_MODE_STOP_START && i2c_do_stop(fd))
|
|
|
|
return (1);
|
2009-01-26 14:00:50 +00:00
|
|
|
}
|
2017-10-13 09:21:41 +00:00
|
|
|
cmd.slave = i2c_opt.addr | 1;
|
2009-01-26 14:00:50 +00:00
|
|
|
cmd.count = 1;
|
|
|
|
cmd.last = 0;
|
|
|
|
cmd.buf = &data;
|
2021-05-12 20:01:14 +00:00
|
|
|
if (i2c_opt.mode == I2C_MODE_STOP_START || i2c_opt.off_len == 0) {
|
2021-05-13 11:58:50 +00:00
|
|
|
if (i2c_do_start(fd, &cmd))
|
|
|
|
return (i2c_do_stop(fd) | 1);
|
2009-01-26 14:00:50 +00:00
|
|
|
} else if (i2c_opt.mode == I2C_MODE_REPEATED_START) {
|
2021-05-13 11:58:50 +00:00
|
|
|
if (i2c_do_repeatstart(fd, &cmd))
|
|
|
|
return (i2c_do_stop(fd) | 1);
|
2009-01-26 14:00:50 +00:00
|
|
|
}
|
2017-10-13 09:21:41 +00:00
|
|
|
|
|
|
|
cmd.count = i2c_opt.count;
|
2021-05-13 11:58:50 +00:00
|
|
|
cmd.buf = (void*)i2c_buf;
|
2017-10-13 09:21:41 +00:00
|
|
|
cmd.last = 1;
|
2021-05-13 11:58:50 +00:00
|
|
|
if (i2c_do_read(fd, &cmd))
|
|
|
|
return (i2c_do_stop(fd) | 1);
|
2009-01-26 14:00:50 +00:00
|
|
|
|
2021-05-13 11:58:50 +00:00
|
|
|
return (i2c_do_stop(fd));
|
2009-01-26 14:00:50 +00:00
|
|
|
}
|
|
|
|
|
2019-05-22 21:06:10 +00:00
|
|
|
/*
|
|
|
|
* i2c_rdwr_transfer() - use I2CRDWR to conduct a complete i2c transfer.
|
|
|
|
*
|
|
|
|
* Some i2c hardware is unable to provide direct control over START, REPEAT-
|
|
|
|
* START, and STOP operations. Such hardware can only perform a complete
|
|
|
|
* START-<data>-STOP or START-<data>-REPEAT-START-<data>-STOP sequence as a
|
|
|
|
* single operation. The driver framework refers to this sequence as a
|
|
|
|
* "transfer" so we call it "transfer mode". We assemble either one or two
|
|
|
|
* iic_msg structures to describe the IO operations, and hand them off to the
|
|
|
|
* driver to be handled as a single transfer.
|
|
|
|
*/
|
|
|
|
static int
|
2021-05-13 11:58:50 +00:00
|
|
|
i2c_rdwr_transfer(int fd, struct options i2c_opt, uint8_t *i2c_buf)
|
2019-05-22 21:06:10 +00:00
|
|
|
{
|
2021-05-12 20:01:14 +00:00
|
|
|
struct iic_msg msgs[2], *msgp = msgs;
|
2019-05-22 21:06:10 +00:00
|
|
|
struct iic_rdwr_data xfer;
|
2021-05-12 20:01:14 +00:00
|
|
|
int flag = 0;
|
|
|
|
|
|
|
|
if (i2c_opt.off_len) {
|
|
|
|
msgp->flags = IIC_M_WR | IIC_M_NOSTOP;
|
|
|
|
msgp->slave = i2c_opt.addr;
|
|
|
|
msgp->buf = i2c_opt.off_buf;
|
|
|
|
msgp->len = i2c_opt.off_len;
|
|
|
|
msgp++;
|
|
|
|
flag = IIC_M_NOSTART;
|
2019-05-22 21:06:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the transfer direction is write and we did a write of the offset
|
|
|
|
* above, then we need to elide the start; this transfer is just more
|
|
|
|
* writing that follows the one started above. For a read, we always do
|
|
|
|
* a start; if we did an offset write above it'll be a repeat-start
|
|
|
|
* because of the NOSTOP flag used above.
|
|
|
|
*/
|
|
|
|
if (i2c_opt.dir == 'w')
|
2021-05-12 20:01:14 +00:00
|
|
|
msgp->flags = IIC_M_WR | flag;
|
2019-05-22 21:06:10 +00:00
|
|
|
else
|
2021-05-12 20:01:14 +00:00
|
|
|
msgp->flags = IIC_M_RD;
|
|
|
|
msgp->slave = i2c_opt.addr;
|
|
|
|
msgp->len = i2c_opt.count;
|
|
|
|
msgp->buf = i2c_buf;
|
|
|
|
msgp++;
|
2019-05-22 21:06:10 +00:00
|
|
|
|
|
|
|
xfer.msgs = msgs;
|
2021-05-12 20:01:14 +00:00
|
|
|
xfer.nmsgs = msgp - msgs;
|
2019-05-22 21:06:10 +00:00
|
|
|
|
|
|
|
if (ioctl(fd, I2CRDWR, &xfer) == -1 )
|
|
|
|
err(1, "ioctl(I2CRDWR) failed");
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2021-05-12 21:34:58 +00:00
|
|
|
static int
|
|
|
|
access_bus(int fd, struct options i2c_opt)
|
|
|
|
{
|
2021-05-13 11:58:50 +00:00
|
|
|
uint8_t i2c_buf[i2c_opt.count];
|
|
|
|
int error;
|
|
|
|
unsigned u, chunk_size = 16;
|
2021-05-12 21:34:58 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* For a write, read the data to be written to the chip from stdin.
|
|
|
|
*/
|
|
|
|
if (i2c_opt.dir == 'w') {
|
|
|
|
if (i2c_opt.verbose && !i2c_opt.binary)
|
2021-05-13 11:58:50 +00:00
|
|
|
fprintf(stderr, "Enter %u bytes of data: ",
|
2021-05-12 21:34:58 +00:00
|
|
|
i2c_opt.count);
|
2021-05-13 11:58:50 +00:00
|
|
|
if (fread(i2c_buf, 1, i2c_opt.count, stdin) != i2c_opt.count)
|
|
|
|
err(1, "not enough data, exiting\n");
|
2021-05-12 21:34:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (i2c_opt.mode == I2C_MODE_TRANSFER)
|
|
|
|
error = i2c_rdwr_transfer(fd, i2c_opt, i2c_buf);
|
|
|
|
else if (i2c_opt.dir == 'w')
|
|
|
|
error = i2c_write(fd, i2c_opt, i2c_buf);
|
|
|
|
else
|
|
|
|
error = i2c_read(fd, i2c_opt, i2c_buf);
|
|
|
|
|
|
|
|
if (error == 0) {
|
|
|
|
if (i2c_opt.dir == 'r' && i2c_opt.binary) {
|
|
|
|
(void)fwrite(i2c_buf, 1, i2c_opt.count, stdout);
|
|
|
|
} else if (i2c_opt.verbose || i2c_opt.dir == 'r') {
|
|
|
|
if (i2c_opt.verbose)
|
|
|
|
fprintf(stderr, "\nData %s (hex):\n",
|
|
|
|
i2c_opt.dir == 'r' ? "read" : "written");
|
|
|
|
|
2021-05-13 11:58:50 +00:00
|
|
|
for (u = 0; u < i2c_opt.count; u++) {
|
|
|
|
printf("%02hhx ", i2c_buf[u]);
|
|
|
|
if ((u % chunk_size) == chunk_size - 1)
|
|
|
|
printf("\n");
|
2021-05-12 21:34:58 +00:00
|
|
|
}
|
2021-05-13 11:58:50 +00:00
|
|
|
if ((u % chunk_size) != 0)
|
|
|
|
printf("\n");
|
2021-05-12 21:34:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
2021-05-19 18:56:59 +00:00
|
|
|
static const char *widths[] = {
|
|
|
|
"0",
|
|
|
|
"8",
|
|
|
|
"16LE",
|
|
|
|
"16BE",
|
|
|
|
"16",
|
|
|
|
NULL,
|
|
|
|
};
|
|
|
|
|
|
|
|
static int
|
|
|
|
command_bus(struct options i2c_opt, char *cmd)
|
|
|
|
{
|
|
|
|
int error, fd;
|
|
|
|
char devbuf[64];
|
|
|
|
uint8_t dbuf[BUFSIZ];
|
|
|
|
unsigned bus;
|
|
|
|
const char *width = NULL;
|
|
|
|
const char *err_msg;
|
|
|
|
unsigned offset;
|
|
|
|
unsigned u;
|
|
|
|
size_t length;
|
|
|
|
|
|
|
|
while (isspace(*cmd))
|
|
|
|
cmd++;
|
|
|
|
|
|
|
|
switch(*cmd) {
|
|
|
|
case 0:
|
|
|
|
case '#':
|
|
|
|
return (0);
|
|
|
|
case 'p':
|
|
|
|
case 'P':
|
|
|
|
printf("%s", cmd);
|
|
|
|
return (0);
|
|
|
|
case 'r':
|
|
|
|
case 'R':
|
|
|
|
i2c_opt.dir = 'r';
|
|
|
|
break;
|
|
|
|
case 'w':
|
|
|
|
case 'W':
|
|
|
|
i2c_opt.dir = 'w';
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fprintf(stderr,
|
|
|
|
"Did not understand command: 0x%02x ", *cmd);
|
|
|
|
if (isgraph(*cmd))
|
|
|
|
fprintf(stderr, "'%c'", *cmd);
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
cmd++;
|
|
|
|
|
|
|
|
bus = strtoul(cmd, &cmd, 0);
|
|
|
|
if (bus == 0 && errno == EINVAL) {
|
|
|
|
fprintf(stderr, "Could not translate bus number\n");
|
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
i2c_opt.addr = strtoul(cmd, &cmd, 0);
|
|
|
|
if (i2c_opt.addr == 0 && errno == EINVAL) {
|
|
|
|
fprintf(stderr, "Could not translate device\n");
|
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
if (i2c_opt.addr < 1 || i2c_opt.addr > 0x7f) {
|
|
|
|
fprintf(stderr, "Invalid device (0x%x)\n", i2c_opt.addr);
|
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
i2c_opt.addr <<= 1;
|
|
|
|
|
|
|
|
while(isspace(*cmd))
|
|
|
|
cmd++;
|
|
|
|
|
|
|
|
for(u = 0; widths[u]; u++) {
|
|
|
|
length = strlen(widths[u]);
|
|
|
|
if (memcmp(cmd, widths[u], length))
|
|
|
|
continue;
|
|
|
|
if (!isspace(cmd[length]))
|
|
|
|
continue;
|
|
|
|
width = widths[u];
|
|
|
|
cmd += length;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (width == NULL) {
|
|
|
|
fprintf(stderr, "Invalid width\n");
|
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
offset = strtoul(cmd, &cmd, 0);
|
|
|
|
if (offset == 0 && errno == EINVAL) {
|
|
|
|
fprintf(stderr, "Could not translate offset\n");
|
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
err_msg = encode_offset(width, offset,
|
|
|
|
i2c_opt.off_buf, &i2c_opt.off_len);
|
|
|
|
if (err_msg) {
|
|
|
|
fprintf(stderr, "%s", err_msg);
|
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i2c_opt.dir == 'r') {
|
|
|
|
i2c_opt.count = strtoul(cmd, &cmd, 0);
|
|
|
|
if (i2c_opt.count == 0 && errno == EINVAL) {
|
|
|
|
fprintf(stderr, "Could not translate length\n");
|
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
i2c_opt.count = 0;
|
|
|
|
while (1) {
|
|
|
|
while(isspace(*cmd))
|
|
|
|
cmd++;
|
|
|
|
if (!*cmd)
|
|
|
|
break;
|
|
|
|
if (!isxdigit(*cmd)) {
|
|
|
|
fprintf(stderr, "Not a hex digit.\n");
|
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
dbuf[i2c_opt.count] = digittoint(*cmd++) << 4;
|
|
|
|
while(isspace(*cmd))
|
|
|
|
cmd++;
|
|
|
|
if (!*cmd) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"Uneven number of hex digits.\n");
|
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
if (!isxdigit(*cmd)) {
|
|
|
|
fprintf(stderr, "Not a hex digit.\n");
|
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
dbuf[i2c_opt.count++] |= digittoint(*cmd++);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert(bus < N_FDCACHE);
|
|
|
|
fd = fd_cache[bus];
|
|
|
|
if (fd < 0) {
|
|
|
|
(void)sprintf(devbuf, "/dev/iic%u", bus);
|
|
|
|
fd = open(devbuf, O_RDWR);
|
|
|
|
if (fd == -1) {
|
|
|
|
fprintf(stderr, "Error opening I2C controller (%s): %s\n",
|
|
|
|
devbuf, strerror(errno));
|
|
|
|
return (EX_NOINPUT);
|
|
|
|
}
|
|
|
|
fd_cache[bus] = fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
error = i2c_rdwr_transfer(fd, i2c_opt, dbuf);
|
|
|
|
if (error)
|
|
|
|
return(-1);
|
|
|
|
|
|
|
|
if (i2c_opt.dir == 'r') {
|
|
|
|
for (u = 0; u < i2c_opt.count; u++)
|
|
|
|
printf("%02x", dbuf[u]);
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
exec_bus(struct options i2c_opt, char *cmd)
|
|
|
|
{
|
|
|
|
int error;
|
|
|
|
|
|
|
|
while (isspace(*cmd))
|
|
|
|
cmd++;
|
|
|
|
if (*cmd == '#' || *cmd == '\0')
|
|
|
|
return (0);
|
|
|
|
error = command_bus(i2c_opt, cmd);
|
|
|
|
if (i2c_opt.verbose) {
|
|
|
|
(void)fflush(stderr);
|
|
|
|
printf(error ? "ERROR\n" : "OK\n");
|
|
|
|
error = 0;
|
|
|
|
} else if (error) {
|
|
|
|
fprintf(stderr, " in: %s", cmd);
|
|
|
|
}
|
|
|
|
(void)fflush(stdout);
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
instruct_bus(struct options i2c_opt, int argc, char **argv)
|
|
|
|
{
|
|
|
|
char buf[BUFSIZ];
|
|
|
|
int rd_cmds = (argc == 0);
|
|
|
|
int error;
|
|
|
|
|
|
|
|
while (argc-- > 0) {
|
|
|
|
if (argc == 0 && !strcmp(*argv, "-")) {
|
|
|
|
rd_cmds = 1;
|
|
|
|
} else {
|
|
|
|
error = exec_bus(i2c_opt, *argv);
|
|
|
|
if (error)
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
argv++;
|
|
|
|
}
|
|
|
|
if (!rd_cmds)
|
|
|
|
return (0);
|
|
|
|
while (fgets(buf, sizeof buf, stdin) != NULL) {
|
|
|
|
error = exec_bus(i2c_opt, buf);
|
|
|
|
if (error)
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2009-01-26 14:00:50 +00:00
|
|
|
int
|
|
|
|
main(int argc, char** argv)
|
|
|
|
{
|
|
|
|
struct options i2c_opt;
|
2021-05-12 20:01:14 +00:00
|
|
|
const char *dev, *err_msg;
|
2021-05-13 09:52:36 +00:00
|
|
|
int fd, error = 0, ch;
|
|
|
|
const char *optflags = "a:f:d:o:w:c:m:n:sbvrh";
|
|
|
|
char do_what = 0;
|
2009-01-26 14:00:50 +00:00
|
|
|
|
|
|
|
dev = I2C_DEV;
|
2021-05-19 18:56:59 +00:00
|
|
|
for (ch = 0; ch < N_FDCACHE; ch++)
|
|
|
|
fd_cache[ch] = -1;
|
2009-01-26 14:00:50 +00:00
|
|
|
|
|
|
|
/* Default values */
|
|
|
|
i2c_opt.off = 0;
|
|
|
|
i2c_opt.verbose = 0;
|
|
|
|
i2c_opt.dir = 'r'; /* direction = read */
|
2021-05-12 20:01:14 +00:00
|
|
|
i2c_opt.width = "8";
|
2009-01-26 14:00:50 +00:00
|
|
|
i2c_opt.count = 1;
|
|
|
|
i2c_opt.binary = 0; /* ASCII text output */
|
2021-05-13 09:52:36 +00:00
|
|
|
i2c_opt.skip = NULL; /* scan all addresses */
|
2021-05-19 18:48:28 +00:00
|
|
|
i2c_opt.mode = I2C_MODE_TRANSFER;
|
2009-01-26 14:00:50 +00:00
|
|
|
|
2021-05-13 09:52:36 +00:00
|
|
|
/* Find out what we are going to do */
|
|
|
|
|
2021-05-19 18:56:59 +00:00
|
|
|
while ((ch = getopt(argc, argv, "a:f:d:iw:c:m:n:sbvrh")) != -1) {
|
2021-05-13 09:52:36 +00:00
|
|
|
switch(ch) {
|
|
|
|
case 'a':
|
2021-05-19 18:56:59 +00:00
|
|
|
case 'i':
|
2021-05-13 09:52:36 +00:00
|
|
|
case 'r':
|
|
|
|
case 's':
|
|
|
|
if (do_what)
|
|
|
|
usage("Only one of [-a|-h|-r|-s]");
|
|
|
|
do_what = ch;
|
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
usage("Help:");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Then handle the legal subset of arguments */
|
|
|
|
|
|
|
|
switch (do_what) {
|
2021-05-19 18:56:59 +00:00
|
|
|
case 0: usage("Pick one of [-a|-h|-i|-r|-s]"); break;
|
2021-05-13 09:52:36 +00:00
|
|
|
case 'a': optflags = "a:f:d:w:o:c:m:bv"; break;
|
2021-05-19 18:56:59 +00:00
|
|
|
case 'i': optflags = "iv"; break;
|
2021-05-13 09:52:36 +00:00
|
|
|
case 'r': optflags = "rf:v"; break;
|
|
|
|
case 's': optflags = "sf:n:v"; break;
|
|
|
|
default: assert("Bad do_what");
|
|
|
|
}
|
|
|
|
|
|
|
|
optreset = 1;
|
|
|
|
optind = 1;
|
|
|
|
|
|
|
|
while ((ch = getopt(argc, argv, optflags)) != -1) {
|
2009-01-26 14:00:50 +00:00
|
|
|
switch(ch) {
|
|
|
|
case 'a':
|
2021-05-13 10:55:37 +00:00
|
|
|
i2c_opt.addr = strtoul(optarg, 0, 16);
|
2009-01-26 14:00:50 +00:00
|
|
|
if (i2c_opt.addr == 0 && errno == EINVAL)
|
2021-05-12 21:34:58 +00:00
|
|
|
usage("Bad -a argument (hex)");
|
2021-05-13 10:55:37 +00:00
|
|
|
if (i2c_opt.addr == 0 || i2c_opt.addr > 0x7f)
|
|
|
|
usage("Bad -a argument (01..7f)");
|
|
|
|
i2c_opt.addr <<= 1;
|
2009-01-26 14:00:50 +00:00
|
|
|
break;
|
2021-05-19 18:47:32 +00:00
|
|
|
case 'b':
|
|
|
|
i2c_opt.binary = 1;
|
|
|
|
break;
|
|
|
|
case 'c':
|
|
|
|
i2c_opt.count = (strtoul(optarg, 0, 10));
|
|
|
|
if (i2c_opt.count == 0 && errno == EINVAL)
|
|
|
|
usage("Bad -c argument (decimal)");
|
2009-01-26 14:00:50 +00:00
|
|
|
break;
|
|
|
|
case 'd':
|
2021-05-13 09:52:36 +00:00
|
|
|
if (strcmp(optarg, "r") && strcmp(optarg, "w"))
|
|
|
|
usage("Bad -d argument ([r|w])");
|
2009-01-26 14:00:50 +00:00
|
|
|
i2c_opt.dir = optarg[0];
|
|
|
|
break;
|
2021-05-19 18:47:32 +00:00
|
|
|
case 'f':
|
|
|
|
dev = optarg;
|
2009-01-26 14:00:50 +00:00
|
|
|
break;
|
2021-05-19 18:56:59 +00:00
|
|
|
case 'i': break;
|
2009-01-26 14:00:50 +00:00
|
|
|
case 'm':
|
|
|
|
if (!strcmp(optarg, "no"))
|
|
|
|
i2c_opt.mode = I2C_MODE_NONE;
|
|
|
|
else if (!strcmp(optarg, "ss"))
|
|
|
|
i2c_opt.mode = I2C_MODE_STOP_START;
|
|
|
|
else if (!strcmp(optarg, "rs"))
|
|
|
|
i2c_opt.mode = I2C_MODE_REPEATED_START;
|
2019-05-22 21:06:10 +00:00
|
|
|
else if (!strcmp(optarg, "tr"))
|
|
|
|
i2c_opt.mode = I2C_MODE_TRANSFER;
|
2009-01-26 14:00:50 +00:00
|
|
|
else
|
2021-05-12 21:34:58 +00:00
|
|
|
usage("Bad -m argument ([no|ss|rs|tr])");
|
2009-01-26 14:00:50 +00:00
|
|
|
break;
|
|
|
|
case 'n':
|
2021-05-13 09:52:36 +00:00
|
|
|
i2c_opt.skip = optarg;
|
2009-01-26 14:00:50 +00:00
|
|
|
break;
|
2021-05-19 18:47:32 +00:00
|
|
|
case 'o':
|
|
|
|
i2c_opt.off = strtoul(optarg, 0, 16);
|
|
|
|
if (i2c_opt.off == 0 && errno == EINVAL)
|
|
|
|
usage("Bad -o argument (hex)");
|
2009-01-26 14:00:50 +00:00
|
|
|
break;
|
2021-05-19 18:47:32 +00:00
|
|
|
case 'r': break;
|
|
|
|
case 's': break;
|
2009-01-26 14:00:50 +00:00
|
|
|
case 'v':
|
|
|
|
i2c_opt.verbose = 1;
|
|
|
|
break;
|
2021-05-19 18:47:32 +00:00
|
|
|
case 'w':
|
|
|
|
i2c_opt.width = optarg; // checked later.
|
|
|
|
break;
|
2009-01-26 14:00:50 +00:00
|
|
|
default:
|
2021-05-13 09:52:36 +00:00
|
|
|
fprintf(stderr, "Illegal -%c option", ch);
|
|
|
|
usage(NULL);
|
2009-01-26 14:00:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
2021-05-19 18:56:59 +00:00
|
|
|
if (do_what == 'i')
|
|
|
|
return(instruct_bus(i2c_opt, argc, argv));
|
2021-05-12 21:34:58 +00:00
|
|
|
if (argc > 0)
|
|
|
|
usage("Too many arguments");
|
2009-01-26 14:00:50 +00:00
|
|
|
|
|
|
|
/* Set default mode if option -m is not specified */
|
|
|
|
if (i2c_opt.mode == I2C_MODE_NOTSET) {
|
|
|
|
if (i2c_opt.dir == 'r')
|
|
|
|
i2c_opt.mode = I2C_MODE_STOP_START;
|
|
|
|
else if (i2c_opt.dir == 'w')
|
|
|
|
i2c_opt.mode = I2C_MODE_NONE;
|
|
|
|
}
|
|
|
|
|
2021-05-12 21:34:58 +00:00
|
|
|
err_msg = encode_offset(i2c_opt.width, i2c_opt.off,
|
|
|
|
i2c_opt.off_buf, &i2c_opt.off_len);
|
|
|
|
if (err_msg != NULL) {
|
|
|
|
fprintf(stderr, "%s", err_msg);
|
2021-05-19 18:47:32 +00:00
|
|
|
return(EX_USAGE);
|
2021-05-12 20:01:14 +00:00
|
|
|
}
|
|
|
|
|
2009-01-26 14:00:50 +00:00
|
|
|
if (i2c_opt.verbose)
|
|
|
|
fprintf(stderr, "dev: %s, addr: 0x%x, r/w: %c, "
|
2021-05-13 11:58:50 +00:00
|
|
|
"offset: 0x%02x, width: %s, count: %u\n", dev,
|
2009-01-26 14:00:50 +00:00
|
|
|
i2c_opt.addr >> 1, i2c_opt.dir, i2c_opt.off,
|
|
|
|
i2c_opt.width, i2c_opt.count);
|
|
|
|
|
2021-05-12 19:43:44 +00:00
|
|
|
fd = open(dev, O_RDWR);
|
|
|
|
if (fd == -1) {
|
|
|
|
fprintf(stderr, "Error opening I2C controller (%s): %s\n",
|
|
|
|
dev, strerror(errno));
|
|
|
|
return (EX_NOINPUT);
|
|
|
|
}
|
|
|
|
|
2021-05-13 09:52:36 +00:00
|
|
|
switch (do_what) {
|
2021-05-19 18:47:32 +00:00
|
|
|
case 'a':
|
|
|
|
error = access_bus(fd, i2c_opt);
|
|
|
|
break;
|
2021-05-13 09:52:36 +00:00
|
|
|
case 's':
|
2021-05-13 11:58:50 +00:00
|
|
|
error = scan_bus(dev, fd, i2c_opt.skip, i2c_opt.verbose);
|
2021-05-13 09:52:36 +00:00
|
|
|
break;
|
|
|
|
case 'r':
|
2021-05-13 11:58:50 +00:00
|
|
|
error = reset_bus(dev, fd, i2c_opt.verbose);
|
2021-05-13 09:52:36 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert("Bad do_what");
|
|
|
|
}
|
2021-05-12 19:43:44 +00:00
|
|
|
|
2021-05-12 21:34:58 +00:00
|
|
|
ch = close(fd);
|
|
|
|
assert(ch == 0);
|
|
|
|
return (error);
|
2009-01-26 14:00:50 +00:00
|
|
|
}
|