Shiyu Sun | 9d67dc6 | 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 | 1aa6780 | 2020-12-05 03:33:44 +0300 | [diff] [blame] | 20 | #include <fcntl.h> |
Shiyu Sun | 9d67dc6 | 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" |
Angel Pons | e0be7a0 | 2021-05-02 18:56:45 +0200 | [diff] [blame] | 30 | #include "programmer.h" |
Shiyu Sun | 9d67dc6 | 2020-03-19 16:59:52 +1100 | [diff] [blame] | 31 | |
Angel Pons | e82ffda | 2021-04-16 12:16:00 +0200 | [diff] [blame] | 32 | /* Null characters are placeholders for bus number digits */ |
| 33 | #define I2C_DEV_PREFIX "/dev/i2c-\0\0\0" |
Shiyu Sun | 9d67dc6 | 2020-03-19 16:59:52 +1100 | [diff] [blame] | 34 | #define I2C_MAX_BUS 255 |
| 35 | |
| 36 | int i2c_close(int fd) |
| 37 | { |
| 38 | return fd == -1 ? 0 : close(fd); |
| 39 | } |
| 40 | |
Peter Marheine | 659f7b4 | 2021-03-31 11:28:11 +1100 | [diff] [blame] | 41 | int i2c_open_path(const char *path, uint16_t addr, int force) |
| 42 | { |
| 43 | int fd = open(path, O_RDWR); |
| 44 | if (fd < 0) { |
| 45 | msg_perr("Unable to open I2C device %s: %s.\n", path, strerror(errno)); |
| 46 | return fd; |
| 47 | } |
| 48 | |
| 49 | int request = force ? I2C_SLAVE_FORCE : I2C_SLAVE; |
| 50 | int ret = ioctl(fd, request, addr); |
| 51 | if (ret < 0) { |
| 52 | msg_perr("Unable to set I2C slave address to 0x%02x: %s.\n", addr, strerror(errno)); |
| 53 | i2c_close(fd); |
Angel Pons | b60151e | 2021-05-02 19:01:13 +0200 | [diff] [blame] | 54 | return ret; |
Peter Marheine | 659f7b4 | 2021-03-31 11:28:11 +1100 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | return fd; |
| 58 | } |
| 59 | |
Shiyu Sun | 9d67dc6 | 2020-03-19 16:59:52 +1100 | [diff] [blame] | 60 | int i2c_open(int bus, uint16_t addr, int force) |
| 61 | { |
Angel Pons | e82ffda | 2021-04-16 12:16:00 +0200 | [diff] [blame] | 62 | char dev[sizeof(I2C_DEV_PREFIX)] = {0}; |
| 63 | |
Shiyu Sun | 9d67dc6 | 2020-03-19 16:59:52 +1100 | [diff] [blame] | 64 | int ret = -1; |
Shiyu Sun | 9d67dc6 | 2020-03-19 16:59:52 +1100 | [diff] [blame] | 65 | |
| 66 | if (bus < 0 || bus > I2C_MAX_BUS) { |
| 67 | msg_perr("Invalid I2C bus %d.\n", bus); |
| 68 | return ret; |
| 69 | } |
| 70 | |
Angel Pons | e82ffda | 2021-04-16 12:16:00 +0200 | [diff] [blame] | 71 | ret = snprintf(dev, sizeof(dev), "%s%d", I2C_DEV_PREFIX, bus); |
Shiyu Sun | 9d67dc6 | 2020-03-19 16:59:52 +1100 | [diff] [blame] | 72 | if (ret < 0) { |
| 73 | msg_perr("Unable to join bus number to device name: %s.\n", strerror(errno)); |
Angel Pons | e82ffda | 2021-04-16 12:16:00 +0200 | [diff] [blame] | 74 | return ret; |
Shiyu Sun | 9d67dc6 | 2020-03-19 16:59:52 +1100 | [diff] [blame] | 75 | } |
| 76 | |
Peter Marheine | 659f7b4 | 2021-03-31 11:28:11 +1100 | [diff] [blame] | 77 | return i2c_open_path(dev, addr, force); |
Shiyu Sun | 9d67dc6 | 2020-03-19 16:59:52 +1100 | [diff] [blame] | 78 | } |
| 79 | |
Angel Pons | e0be7a0 | 2021-05-02 18:56:45 +0200 | [diff] [blame] | 80 | static int get_bus_number(char *bus_str) |
| 81 | { |
| 82 | char *bus_suffix; |
| 83 | errno = 0; |
| 84 | int bus = (int)strtol(bus_str, &bus_suffix, 10); |
| 85 | if (errno != 0 || bus_str == bus_suffix) { |
| 86 | msg_perr("%s: Could not convert 'bus'.\n", __func__); |
| 87 | return -1; |
| 88 | } |
| 89 | |
| 90 | if (strlen(bus_suffix) > 0) { |
| 91 | msg_perr("%s: Garbage following 'bus' value.\n", __func__); |
| 92 | return -1; |
| 93 | } |
| 94 | |
| 95 | msg_pinfo("Using I2C bus %d.\n", bus); |
| 96 | return bus; |
| 97 | } |
| 98 | |
| 99 | int i2c_open_from_programmer_params(uint16_t addr, int force) |
| 100 | { |
| 101 | int fd = -1; |
| 102 | |
| 103 | char *bus_str = extract_programmer_param("bus"); |
| 104 | char *device_path = extract_programmer_param("devpath"); |
| 105 | |
| 106 | if (device_path != NULL && bus_str != NULL) { |
| 107 | msg_perr("%s: only one of bus and devpath may be specified\n", __func__); |
| 108 | goto out; |
| 109 | } |
| 110 | if (device_path == NULL && bus_str == NULL) { |
| 111 | msg_perr("%s: one of bus and devpath must be specified\n", __func__); |
| 112 | goto out; |
| 113 | } |
| 114 | |
| 115 | if (device_path != NULL) |
| 116 | fd = i2c_open_path(device_path, addr, force); |
| 117 | else |
| 118 | fd = i2c_open(get_bus_number(bus_str), addr, force); |
| 119 | |
| 120 | out: |
| 121 | free(bus_str); |
| 122 | free(device_path); |
| 123 | return fd; |
| 124 | } |
| 125 | |
Shiyu Sun | 9d67dc6 | 2020-03-19 16:59:52 +1100 | [diff] [blame] | 126 | int i2c_read(int fd, uint16_t addr, i2c_buffer_t *buf) |
| 127 | { |
| 128 | if (buf->len == 0) |
| 129 | return 0; |
| 130 | |
| 131 | int ret = ioctl(fd, I2C_SLAVE, addr); |
| 132 | if (ret < 0) { |
| 133 | msg_perr("Unable to set I2C slave address to 0x%02x: %s.\n", addr, strerror(errno)); |
| 134 | return ret; |
| 135 | } |
| 136 | |
| 137 | return read(fd, buf->buf, buf->len); |
| 138 | } |
| 139 | |
| 140 | int i2c_write(int fd, uint16_t addr, const i2c_buffer_t *buf) |
| 141 | { |
| 142 | if (buf->len == 0) |
| 143 | return 0; |
| 144 | |
| 145 | int ret = ioctl(fd, I2C_SLAVE, addr); |
| 146 | if (ret < 0) { |
| 147 | msg_perr("Unable to set I2C slave address to 0x%02x: %s.\n", addr, strerror(errno)); |
| 148 | return ret; |
| 149 | } |
| 150 | |
| 151 | return write(fd, buf->buf, buf->len); |
| 152 | } |