Shiyu Sun | dc2c83b | 2020-03-19 16:59:52 +1100 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the flashrom project. |
| 3 | * |
| 4 | * Copyright (C) 2020 The Chromium OS Authors |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | */ |
| 16 | |
| 17 | #include <errno.h> |
| 18 | #include <stdio.h> |
| 19 | #include <string.h> |
Evgeny Zinoviev | d870a35 | 2020-12-05 03:33:44 +0300 | [diff] [blame^] | 20 | #include <fcntl.h> |
Shiyu Sun | dc2c83b | 2020-03-19 16:59:52 +1100 | [diff] [blame] | 21 | #include <sys/ioctl.h> |
| 22 | #include <unistd.h> |
| 23 | |
| 24 | #include <stdlib.h> |
| 25 | #include <linux/i2c.h> |
| 26 | #include <linux/i2c-dev.h> |
| 27 | |
| 28 | #include "flash.h" |
| 29 | #include "i2c_helper.h" |
| 30 | |
| 31 | #define I2C_DEV_PREFIX "/dev/i2c-" |
| 32 | #define I2C_MAX_BUS 255 |
| 33 | |
| 34 | int i2c_close(int fd) |
| 35 | { |
| 36 | return fd == -1 ? 0 : close(fd); |
| 37 | } |
| 38 | |
| 39 | int i2c_open(int bus, uint16_t addr, int force) |
| 40 | { |
| 41 | int ret = -1; |
| 42 | int fd = -1; |
| 43 | |
| 44 | /* Maximum i2c bus number is 255(3 char), +1 for null terminated string. */ |
| 45 | int path_len = strlen(I2C_DEV_PREFIX) + 4; |
| 46 | int request = force ? I2C_SLAVE_FORCE : I2C_SLAVE; |
| 47 | |
| 48 | if (bus < 0 || bus > I2C_MAX_BUS) { |
| 49 | msg_perr("Invalid I2C bus %d.\n", bus); |
| 50 | return ret; |
| 51 | } |
| 52 | |
| 53 | char *dev = calloc(1, path_len); |
| 54 | if (!dev) { |
| 55 | msg_perr("Unable to allocate space for device name of len %d: %s.\n", |
| 56 | path_len, strerror(errno)); |
| 57 | goto linux_i2c_open_err; |
| 58 | } |
| 59 | |
| 60 | ret = snprintf(dev, path_len, "%s%d", I2C_DEV_PREFIX, bus); |
| 61 | if (ret < 0) { |
| 62 | msg_perr("Unable to join bus number to device name: %s.\n", strerror(errno)); |
| 63 | goto linux_i2c_open_err; |
| 64 | } |
| 65 | |
| 66 | fd = open(dev, O_RDWR); |
| 67 | if (fd < 0) { |
| 68 | msg_perr("Unable to open I2C device %s: %s.\n", dev, strerror(errno)); |
| 69 | ret = fd; |
| 70 | goto linux_i2c_open_err; |
| 71 | } |
| 72 | |
| 73 | ret = ioctl(fd, request, addr); |
| 74 | if (ret < 0) { |
| 75 | msg_perr("Unable to set I2C slave address to 0x%02x: %s.\n", addr, strerror(errno)); |
| 76 | i2c_close(fd); |
| 77 | goto linux_i2c_open_err; |
| 78 | } |
| 79 | |
| 80 | linux_i2c_open_err: |
| 81 | if (dev) |
| 82 | free(dev); |
| 83 | |
| 84 | return ret ? ret : fd; |
| 85 | } |
| 86 | |
| 87 | int i2c_read(int fd, uint16_t addr, i2c_buffer_t *buf) |
| 88 | { |
| 89 | if (buf->len == 0) |
| 90 | return 0; |
| 91 | |
| 92 | int ret = ioctl(fd, I2C_SLAVE, addr); |
| 93 | if (ret < 0) { |
| 94 | msg_perr("Unable to set I2C slave address to 0x%02x: %s.\n", addr, strerror(errno)); |
| 95 | return ret; |
| 96 | } |
| 97 | |
| 98 | return read(fd, buf->buf, buf->len); |
| 99 | } |
| 100 | |
| 101 | int i2c_write(int fd, uint16_t addr, const i2c_buffer_t *buf) |
| 102 | { |
| 103 | if (buf->len == 0) |
| 104 | return 0; |
| 105 | |
| 106 | int ret = ioctl(fd, I2C_SLAVE, addr); |
| 107 | if (ret < 0) { |
| 108 | msg_perr("Unable to set I2C slave address to 0x%02x: %s.\n", addr, strerror(errno)); |
| 109 | return ret; |
| 110 | } |
| 111 | |
| 112 | return write(fd, buf->buf, buf->len); |
| 113 | } |