FreeBSD Bugzilla – Attachment 154684 Details for
Bug 198817
[PATCH] Update i2c(8) to use the I2CRDRW ioctl
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
Remember
[x]
|
Forgot Password
Login:
[x]
[patch]
patch i2c(8)
patch_i2c.diff (text/plain), 8.16 KB, created by
Emmanuel Vadot
on 2015-03-23 02:05:57 UTC
(
hide
)
Description:
patch i2c(8)
Filename:
MIME Type:
Creator:
Emmanuel Vadot
Created:
2015-03-23 02:05:57 UTC
Size:
8.16 KB
patch
obsolete
>Index: usr.sbin/i2c/i2c.c >=================================================================== >--- usr.sbin/i2c/i2c.c (revision 280359) >+++ usr.sbin/i2c/i2c.c (working copy) >@@ -121,9 +121,11 @@ > static int > scan_bus(struct iiccmd cmd, char *dev, int skip, char *skip_addr) > { >+ struct iic_rdwr_data msg; > struct skip_range addr_range = { 0, 0 }; > int *tokens, fd, error, i, index, j; > int len = 0, do_skip = 0, no_range = 1; >+ char buf = 0; > > fd = open(dev, O_RDWR); > if (fd == -1) { >@@ -157,6 +159,13 @@ > } > > printf("Scanning I2C devices on %s: ", dev); >+ >+ msg.nmsgs = 1; >+ msg.msgs = calloc(1, sizeof(struct iic_msg)); >+ msg.msgs->buf = &buf; >+ msg.msgs->len = 1; >+ msg.msgs->flags = IIC_M_RD; >+ > for (i = 1; i < 127; i++) { > > if (skip && ( addr_range.start < addr_range.end)) { >@@ -176,36 +185,19 @@ > continue; > } > >- cmd.slave = i << 1; >- cmd.last = 1; >- cmd.count = 0; >- error = ioctl(fd, I2CRSTCARD, &cmd); >- if (error) >- goto out; >- >- cmd.slave = i << 1; >- cmd.last = 1; >- error = ioctl(fd, I2CSTART, &cmd); >- if (!error) >- printf("%x ", i); >- cmd.slave = i << 1; >- cmd.last = 1; >- error = ioctl(fd, I2CSTOP, &cmd); >+ msg.msgs->slave = i << 1; >+ error = ioctl(fd, I2CRDWR, &msg); >+ if (error != -1) >+ printf("0x%x ", i); > } > printf("\n"); > >- error = ioctl(fd, I2CRSTCARD, &cmd); > out: > close(fd); > if (skip && no_range) > free(tokens); > >- if (error) { >- fprintf(stderr, "Error scanning I2C controller (%s): %s\n", >- dev, strerror(errno)); >- return (EX_NOINPUT); >- } else >- return (EX_OK); >+ return (EX_OK); > } > > static int >@@ -253,211 +245,79 @@ > } > > static int >-i2c_write(char *dev, struct options i2c_opt, char *i2c_buf) >+i2c_read_write(char *dev, struct options i2c_opt, char *i2c_buf) > { >- struct iiccmd cmd; >- int ch, i, error, fd, bufsize; >- char *err_msg, *buf; >+ struct iic_rdwr_data msgs; >+ int fd, error, i; >+ char *err_msg, ch; > >- /* >- * Read data to be written to the chip from stdin >- */ >- if (i2c_opt.verbose && !i2c_opt.binary) >- fprintf(stderr, "Enter %u bytes of data: ", i2c_opt.count); >+ if (i2c_opt.dir == 'w') { >+ if (i2c_opt.verbose && !i2c_opt.binary) >+ fprintf(stderr, "Enter %u bytes of data: ", i2c_opt.count); > >- for (i = 0; i < i2c_opt.count; i++) { >- ch = getchar(); >- if (ch == EOF) { >- free(i2c_buf); >- err(1, "not enough data, exiting\n"); >+ for (i = 0; i < i2c_opt.count; i++) { >+ ch = getchar(); >+ if (ch == EOF) { >+ free(i2c_buf); >+ err(1, "not enough data, exiting\n"); >+ } >+ i2c_buf[i] = ch; > } >- i2c_buf[i] = ch; > } > > fd = open(dev, O_RDWR); >- if (fd == -1) { >- free(i2c_buf); >+ if (fd == -1) > err(1, "open failed"); >- } > >- /* >- * Write offset where the data will go >- */ >- cmd.slave = i2c_opt.addr; >- error = ioctl(fd, I2CSTART, &cmd); >- if (error == -1) { >- err_msg = "ioctl: error sending start condition"; >- goto err1; >- } >- >+ msgs.nmsgs = 1; > if (i2c_opt.width) { >- bufsize = i2c_opt.width / 8; >- buf = prepare_buf(bufsize, i2c_opt.off); >- if (buf == NULL) { >- err_msg = "error: offset malloc"; >- goto err1; >- } >+ msgs.nmsgs = 2; >+ msgs.msgs = calloc(2, sizeof(struct iic_msg)); >+ msgs.msgs[0].slave = i2c_opt.addr; >+ msgs.msgs[0].len = i2c_opt.width / 8; > >- cmd.count = bufsize; >- cmd.buf = buf; >- error = ioctl(fd, I2CWRITE, &cmd); >- free(buf); >- if (error == -1) { >- err_msg = "ioctl: error when write offset"; >- goto err1; >+ if ((msgs.msgs[0].buf = calloc(msgs.msgs[0].len, sizeof(uint8_t))) == NULL) { >+ err_msg = "can't malloc\n"; >+ goto err; > } >- } >- >- /* Mode - stop start */ >- if (i2c_opt.mode == I2C_MODE_STOP_START) { >- cmd.slave = i2c_opt.addr; >- error = ioctl(fd, I2CSTOP, &cmd); >- if (error == -1) { >- err_msg = "ioctl: error sending stop condition"; >- goto err2; >+ if (msgs.msgs[0].len == 1) >+ msgs.msgs[0].buf[0] = i2c_opt.off & 0xff; >+ else if (msgs.msgs[0].len == 2) { >+ msgs.msgs[0].buf[0] = (i2c_opt.off >> 8) & 0xff; >+ msgs.msgs[0].buf[1] = i2c_opt.off & 0xff; > } >- cmd.slave = i2c_opt.addr; >- error = ioctl(fd, I2CSTART, &cmd); >- if (error == -1) { >- err_msg = "ioctl: error sending start condition"; >- goto err1; >+ if (msgs.msgs[0].buf == NULL) { >+ err_msg = "error: offset malloc"; >+ goto err; > } >- } >- /* Mode - repeated start */ >- if (i2c_opt.mode == I2C_MODE_REPEATED_START) { >- cmd.slave = i2c_opt.addr; >- error = ioctl(fd, I2CRPTSTART, &cmd); >- if (error == -1) { >- err_msg = "ioctl: error sending repeated start " >- "condition"; >- goto err1; >- } >- } > >- /* >- * Write the data >- */ >- cmd.count = i2c_opt.count; >- cmd.buf = i2c_buf; >- cmd.last = 0; >- error = ioctl(fd, I2CWRITE, &cmd); >- if (error == -1) { >- err_msg = "ioctl: error when write"; >- goto err1; >+ msgs.msgs[0].flags = IIC_M_WR; >+ if (i2c_opt.mode == I2C_MODE_REPEATED_START || i2c_opt.mode == I2C_MODE_NONE) >+ msgs.msgs[0].flags |= IIC_M_NOSTOP; > } >- cmd.slave = i2c_opt.addr; >- error = ioctl(fd, I2CSTOP, &cmd); >- if (error == -1) { >- err_msg = "ioctl: error sending stop condition"; >- goto err2; >- } > >- close(fd); >- return (0); >+ msgs.msgs[msgs.nmsgs - 1].slave = i2c_opt.addr; >+ msgs.msgs[msgs.nmsgs - 1].len = i2c_opt.count; >+ msgs.msgs[msgs.nmsgs - 1].buf = i2c_buf; > >-err1: >- cmd.slave = i2c_opt.addr; >- error = ioctl(fd, I2CSTOP, &cmd); >- if (error == -1) >- fprintf(stderr, "error sending stop condtion\n"); >-err2: >- if (err_msg) >- fprintf(stderr, "%s", err_msg); >+ if (i2c_opt.dir == 'r') >+ msgs.msgs[msgs.nmsgs - 1].flags = IIC_M_RD; >+ else >+ msgs.msgs[msgs.nmsgs - 1].flags = IIC_M_WR; > >- close(fd); >- return (1); >-} >+ if (i2c_opt.mode == I2C_MODE_NONE) >+ msgs.msgs[msgs.nmsgs - 1].flags |= IIC_M_NOSTART; > >-static int >-i2c_read(char *dev, struct options i2c_opt, char *i2c_buf) >-{ >- struct iiccmd cmd; >- int i, fd, error, bufsize; >- char *err_msg, data = 0, *buf; >- >- fd = open(dev, O_RDWR); >- if (fd == -1) >- err(1, "open failed"); >- >- bzero(&cmd, sizeof(cmd)); >- >- if (i2c_opt.width) { >- cmd.slave = i2c_opt.addr; >- cmd.count = 1; >- cmd.last = 0; >- cmd.buf = &data; >- error = ioctl(fd, I2CSTART, &cmd); >- if (error == -1) { >- err_msg = "ioctl: error sending start condition"; >- goto err1; >- } >- bufsize = i2c_opt.width / 8; >- buf = prepare_buf(bufsize, i2c_opt.off); >- if (buf == NULL) { >- err_msg = "error: offset malloc"; >- goto err1; >- } >- >- cmd.count = bufsize; >- cmd.buf = buf; >- cmd.last = 0; >- error = ioctl(fd, I2CWRITE, &cmd); >- free(buf); >- if (error == -1) { >- err_msg = "ioctl: error when write offset"; >- goto err1; >- } >- >- if (i2c_opt.mode == I2C_MODE_STOP_START) { >- cmd.slave = i2c_opt.addr; >- error = ioctl(fd, I2CSTOP, &cmd); >- if (error == -1) { >- err_msg = "error sending stop condtion\n"; >- goto err2; >- } >- } >- } >- cmd.slave = i2c_opt.addr; >- cmd.count = 1; >- cmd.last = 0; >- cmd.buf = &data; >- if (i2c_opt.mode == I2C_MODE_STOP_START) { >- error = ioctl(fd, I2CSTART, &cmd); >- if (error == -1) { >- err_msg = "ioctl: error sending start condition"; >- goto err1; >- } >- } else if (i2c_opt.mode == I2C_MODE_REPEATED_START) { >- error = ioctl(fd, I2CRPTSTART, &cmd); >- if (error == -1) { >- err_msg = "ioctl: error sending repeated start " >- "condition"; >- goto err1; >- } >- } >- error = ioctl(fd, I2CSTOP, &cmd); >+ error = ioctl(fd, I2CRDWR, &msgs); > if (error == -1) { >- err_msg = "error sending stop condtion\n"; >- goto err2; >+ err_msg = "error sending i2c frame\n"; >+ goto err; > } > >- 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; >- } >- } >- > close(fd); > return (0); > >-err1: >- cmd.slave = i2c_opt.addr; >- error = ioctl(fd, I2CSTOP, &cmd); >- if (error == -1) >- fprintf(stderr, "error sending stop condtion\n"); >-err2: >+err: > if (err_msg) > fprintf(stderr, "%s", err_msg); > >@@ -594,20 +454,11 @@ > if (i2c_buf == NULL) > err(1, "data malloc"); > >- if (i2c_opt.dir == 'w') { >- error = i2c_write(dev, i2c_opt, i2c_buf); >- if (error) { >- free(i2c_buf); >- return (1); >- } >+ error = i2c_read_write(dev, i2c_opt, i2c_buf); >+ if (error) { >+ free(i2c_buf); >+ return (1); > } >- if (i2c_opt.dir == 'r') { >- error = i2c_read(dev, i2c_opt, i2c_buf); >- if (error) { >- free(i2c_buf); >- return (1); >- } >- } > > if (i2c_opt.verbose) > fprintf(stderr, "\nData %s (hex):\n", i2c_opt.dir == 'r' ?
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
Attachments on
bug 198817
: 154684 |
158368
|
158475