From owner-svn-src-stable-11@freebsd.org Thu Oct 19 08:17:33 2017 Return-Path: Delivered-To: svn-src-stable-11@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 8E886E32EB1; Thu, 19 Oct 2017 08:17:33 +0000 (UTC) (envelope-from avg@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 684C9400; Thu, 19 Oct 2017 08:17:33 +0000 (UTC) (envelope-from avg@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id v9J8HWZ2026730; Thu, 19 Oct 2017 08:17:32 GMT (envelope-from avg@FreeBSD.org) Received: (from avg@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id v9J8HWVf026728; Thu, 19 Oct 2017 08:17:32 GMT (envelope-from avg@FreeBSD.org) Message-Id: <201710190817.v9J8HWVf026728@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: avg set sender to avg@FreeBSD.org using -f From: Andriy Gapon Date: Thu, 19 Oct 2017 08:17:32 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-11@freebsd.org Subject: svn commit: r324751 - stable/11/usr.sbin/i2c X-SVN-Group: stable-11 X-SVN-Commit-Author: avg X-SVN-Commit-Paths: stable/11/usr.sbin/i2c X-SVN-Commit-Revision: 324751 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-stable-11@freebsd.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: SVN commit messages for only the 11-stable src tree List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 19 Oct 2017 08:17:33 -0000 Author: avg Date: Thu Oct 19 08:17:32 2017 New Revision: 324751 URL: https://svnweb.freebsd.org/changeset/base/324751 Log: MFC r324590: i2c(8): clean up and clarify read operation Modified: stable/11/usr.sbin/i2c/i2c.8 stable/11/usr.sbin/i2c/i2c.c Directory Properties: stable/11/ (props changed) Modified: stable/11/usr.sbin/i2c/i2c.8 ============================================================================== --- stable/11/usr.sbin/i2c/i2c.8 Thu Oct 19 08:15:41 2017 (r324750) +++ stable/11/usr.sbin/i2c/i2c.8 Thu Oct 19 08:17:32 2017 (r324751) @@ -83,6 +83,10 @@ using selected addresses 'a:b:c'. This option is avail used. .It Fl o Ar offset offset within the device for data transfer (hex). +The default is zero. +Use +.Dq -w 0 +to disable writing of the offset to the slave. .It Fl r reset the controller. .It Fl s @@ -91,6 +95,12 @@ scan the bus for devices. be verbose. .It Fl w Ar 0|8|16 device addressing width (in bits). +This is used to determine how to pass +.Ar offset +specified with +.Fl o +to the slave. +Zero means that the offset is ignored and not passed to the slave at all. .El .Sh WARNINGS Great care must be taken when manipulating slave I2C devices with the Modified: stable/11/usr.sbin/i2c/i2c.c ============================================================================== --- stable/11/usr.sbin/i2c/i2c.c Thu Oct 19 08:15:41 2017 (r324750) +++ stable/11/usr.sbin/i2c/i2c.c Thu Oct 19 08:17:32 2017 (r324751) @@ -212,15 +212,14 @@ start_over: use_read_xfer = 1; goto start_over; } - cmd.slave = i << 1; - cmd.last = 1; - ioctl(fd, I2CSTOP, &cmd); + ioctl(fd, I2CSTOP); } if (error == 0) { ++num_found; printf("%02x ", i); } } + /* * If we found nothing, maybe START is not supported and returns a * generic error code such as EIO or ENXIO, so try again using reads. @@ -355,7 +354,7 @@ i2c_write(char *dev, struct options i2c_opt, char *i2c } } - error = ioctl(fd, I2CSTOP, &cmd); + error = ioctl(fd, I2CSTOP); if (error == -1) { err_msg = "ioctl: error sending stop condition"; goto err2; @@ -439,8 +438,7 @@ i2c_write(char *dev, struct options i2c_opt, char *i2c } break; } - cmd.slave = i2c_opt.addr; - error = ioctl(fd, I2CSTOP, &cmd); + error = ioctl(fd, I2CSTOP); if (error == -1) { err_msg = "ioctl: error sending stop condition"; goto err2; @@ -450,8 +448,7 @@ i2c_write(char *dev, struct options i2c_opt, char *i2c return (0); err1: - cmd.slave = i2c_opt.addr; - error = ioctl(fd, I2CSTOP, &cmd); + error = ioctl(fd, I2CSTOP); if (error == -1) fprintf(stderr, "error sending stop condition\n"); err2: @@ -466,7 +463,7 @@ static int i2c_read(char *dev, struct options i2c_opt, char *i2c_buf) { struct iiccmd cmd; - int i, fd, error, bufsize; + int fd, error, bufsize; char *err_msg, data = 0, *buf; fd = open(dev, O_RDWR); @@ -503,23 +500,22 @@ i2c_read(char *dev, struct options i2c_opt, char *i2c_ } if (i2c_opt.mode == I2C_MODE_STOP_START) { - cmd.slave = i2c_opt.addr; - error = ioctl(fd, I2CSTOP, &cmd); + error = ioctl(fd, I2CSTOP); if (error == -1) { err_msg = "error sending stop condition"; goto err2; } } } - cmd.slave = i2c_opt.addr; + cmd.slave = i2c_opt.addr | 1; cmd.count = 1; cmd.last = 0; cmd.buf = &data; - if (i2c_opt.mode == I2C_MODE_STOP_START) { + if (i2c_opt.mode == I2C_MODE_STOP_START || i2c_opt.width == 0) { error = ioctl(fd, I2CSTART, &cmd); if (error == -1) { err_msg = "ioctl: error sending start condition"; - goto err1; + goto err2; } } else if (i2c_opt.mode == I2C_MODE_REPEATED_START) { error = ioctl(fd, I2CRPTSTART, &cmd); @@ -529,26 +525,27 @@ i2c_read(char *dev, struct options i2c_opt, char *i2c_ goto err1; } } - error = ioctl(fd, I2CSTOP, &cmd); + + cmd.count = i2c_opt.count; + cmd.buf = i2c_buf; + cmd.last = 1; + error = ioctl(fd, I2CREAD, &cmd); if (error == -1) { - err_msg = "error sending stop condition"; - goto err2; + err_msg = "ioctl: error while reading"; + goto err1; } - for (i = 0; i < i2c_opt.count; i++) { - error = read(fd, &i2c_buf[i], 1); - if (error == -1) { - err_msg = "ioctl: error while reading"; - goto err1; - } + error = ioctl(fd, I2CSTOP); + if (error == -1) { + err_msg = "error sending stop condtion\n"; + goto err2; } close(fd); return (0); err1: - cmd.slave = i2c_opt.addr; - error = ioctl(fd, I2CSTOP, &cmd); + error = ioctl(fd, I2CSTOP); if (error == -1) fprintf(stderr, "error sending stop condition\n"); err2: