linux_i2c.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * Copyright (C) 2012 Google Inc.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are
  6. * met:
  7. *
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above
  11. * copyright notice, this list of conditions and the following
  12. * disclaimer in the documentation and/or other materials provided
  13. * with the distribution.
  14. * * Neither the name of Google Inc. nor the names of its
  15. * contributors may be used to endorse or promote products derived
  16. * from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #include <errno.h>
  31. #include <inttypes.h>
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #include <string.h>
  35. #include <sys/fcntl.h>
  36. #include <sys/ioctl.h>
  37. #include <unistd.h>
  38. #include <linux/i2c.h>
  39. #include <linux/i2c-dev.h>
  40. #include "file.h"
  41. #include "flash.h"
  42. #include "programmer.h"
  43. #define I2C_DEV_PREFIX "/dev/i2c-"
  44. #define I2C_MAX_ADAPTER 255
  45. static int fd = -1;
  46. int linux_i2c_shutdown(void *data)
  47. {
  48. if (fd != -1) {
  49. close(fd);
  50. fd = -1;
  51. }
  52. return 0;
  53. }
  54. int linux_i2c_init(void)
  55. {
  56. /* TODO: Eventually this should contain things like
  57. command-line parsing similar to linux_spi. */
  58. return 0;
  59. }
  60. int linux_i2c_open(int bus, int addr, int force)
  61. {
  62. char *dev = NULL;
  63. int ret = 1;
  64. int path_len = strlen(I2C_DEV_PREFIX) + 4;
  65. int request = force ? I2C_SLAVE_FORCE : I2C_SLAVE;
  66. if (bus < 0 || bus > I2C_MAX_ADAPTER) {
  67. msg_perr("Invalid I2C bus %d\n", bus);
  68. goto linux_i2c_open_done;
  69. }
  70. dev = malloc(path_len);
  71. if (!dev)
  72. return 1;
  73. snprintf(dev, path_len, "%s%d", I2C_DEV_PREFIX, bus);
  74. fd = open(dev, O_RDONLY);
  75. if (fd < 0) {
  76. msg_perr("Unable to open I2C device %s: %s\n",
  77. dev, strerror(errno));
  78. goto linux_i2c_open_done;
  79. }
  80. #if defined (__linux__)
  81. if (ioctl(fd, request, addr) < 0) {
  82. msg_perr("Unable to set I2C slave address to 0x%02x\n", addr);
  83. close(fd);
  84. goto linux_i2c_open_done;
  85. }
  86. #else
  87. ret = -ENOSYS;
  88. #endif
  89. ret = 0;
  90. linux_i2c_open_done:
  91. free(dev);
  92. return ret;
  93. }
  94. void linux_i2c_close(void)
  95. {
  96. close(fd);
  97. }
  98. /* returns <0 to indicate failure */
  99. int linux_i2c_xfer(int bus, int addr, const void *inbuf,
  100. int insize, const void *outbuf, int outsize)
  101. {
  102. int ret = -1;
  103. struct i2c_rdwr_ioctl_data data;
  104. struct i2c_msg *msg = NULL;
  105. data.nmsgs = 0;
  106. if (outsize) {
  107. msg = realloc(msg, sizeof(*msg) * (data.nmsgs + 1));
  108. msg[data.nmsgs].addr = addr;
  109. msg[data.nmsgs].flags = 0;
  110. msg[data.nmsgs].len = outsize;
  111. msg[data.nmsgs].buf = (void *)outbuf;
  112. data.nmsgs++;
  113. }
  114. if (insize) {
  115. msg = realloc(msg, sizeof(*msg) * (data.nmsgs + 1));
  116. msg[data.nmsgs].addr = addr;
  117. msg[data.nmsgs].flags = I2C_M_RD;
  118. msg[data.nmsgs].len = insize;
  119. msg[data.nmsgs].buf = (void *)inbuf;
  120. data.nmsgs++;
  121. }
  122. data.msgs = msg;
  123. /* send command to EC and read answer */
  124. #if defined (__linux__)
  125. /* ioctl returns negative errno, else the number of messages executed */
  126. ret = ioctl(fd, I2C_RDWR, &data);
  127. #else
  128. ret = -ENOSYS;
  129. #endif
  130. if (ret != data.nmsgs) {
  131. msg_perr("i2c transfer failed: %d (err: %d, %s)\n",
  132. ret, errno, strerror(errno));
  133. } else {
  134. ret = 0;
  135. }
  136. free(msg);
  137. return ret;
  138. }