| @@ -1,6 +1,10 @@ | |||
| 1 | # Changelog | 1 | # Changelog |
| 2 | 2 | ||
| 3 | All notable changes to this project will be documented in this file. | 3 | All notable changes to this project will be documented in this file. |
| 4 | +## [1.0.10] - 2026-11-16 | ||
| 5 | + | ||
| 6 | +- 支持i2c驱动接口 | ||
| 7 | + | ||
| 4 | ## [1.0.9] - 2026-11-13 | 8 | ## [1.0.9] - 2026-11-13 |
| 5 | 9 | ||
| 6 | - 支持jtag驱动接口 | 10 | - 支持jtag驱动接口 |
| @@ -0,0 +1,426 @@ | |||||||||||||
| 1 | +/* | ||||||||||||
| 2 | + * Copyright (c) 2026 Huawei Technologies Co., Ltd. All rights reserved. | ||||||||||||
| 3 | + * | ||||||||||||
| 4 | + * Licensed under Mulan PSL v2. | ||||||||||||
| 5 | + * You can use this software according to the terms and conditions of the Mulan PSL v2. | ||||||||||||
| 6 | + * You may obtain a copy of Mulan PSL v2 at: | ||||||||||||
| 7 | + * http://license.coscl.org.cn/MulanPSL2 | ||||||||||||
| 8 | + * | ||||||||||||
| 9 | + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, | ||||||||||||
| 10 | + * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, | ||||||||||||
| 11 | + * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. | ||||||||||||
| 12 | + * See the Mulan PSL v2 for more details. | ||||||||||||
| 13 | + */ | ||||||||||||
| 14 | + | ||||||||||||
| 15 | +#include <bcal/i2c.h> | ||||||||||||
| 16 | + | ||||||||||||
| 17 | +#include <array> | ||||||||||||
| 18 | +#include <cerrno> | ||||||||||||
| 19 | +#include <chrono> | ||||||||||||
| 20 | +#include <cstdint> | ||||||||||||
| 21 | +#include <cstdio> | ||||||||||||
| 22 | +#include <cstring> | ||||||||||||
| 23 | +#include <fcntl.h> | ||||||||||||
| 24 | +#include <linux/i2c.h> | ||||||||||||
| 25 | +#include <linux/i2c-dev.h> | ||||||||||||
| 26 | +#include <mutex> | ||||||||||||
| 27 | +#include <optional> | ||||||||||||
| 28 | +#include <stdexcept> | ||||||||||||
| 29 | +#include <string> | ||||||||||||
| 30 | +#include <sys/ioctl.h> | ||||||||||||
| 31 | +#include <sys/types.h> | ||||||||||||
| 32 | +#include <tuple> | ||||||||||||
| 33 | +#include <unistd.h> | ||||||||||||
| 34 | +#include <vector> | ||||||||||||
| 35 | + | ||||||||||||
| 36 | +namespace BCAL_I2C_ASPEED { | ||||||||||||
| 37 | +using namespace bcal; | ||||||||||||
| 38 | +using namespace BCAL_DRIVER_I2C; | ||||||||||||
| 39 | + | ||||||||||||
| 40 | +namespace { | ||||||||||||
| 41 | + | ||||||||||||
| 42 | +constexpr const char* DRIVER_VERSION = "bcal_i2c_aspeed v1.0.0 (linux i2c-dev)"; | ||||||||||||
| 43 | +constexpr const char* DEV_PREFIX = "/dev/i2c-"; | ||||||||||||
| 44 | +constexpr const char* SYSFS_PREFIX = "/sys/bus/i2c/devices/"; | ||||||||||||
| 45 | +constexpr const char* SLAVE_EEPROM = "/slave-eeprom"; | ||||||||||||
| 46 | + | ||||||||||||
| 47 | +constexpr uint32_t MAX_BUS_NUM = 16; /* ast2600 i2c0..i2c15 */ | ||||||||||||
| 48 | +constexpr uint32_t MAX_XFER_LEN = 2048; /* 单次读/写 payload 上限 */ | ||||||||||||
| 49 | +constexpr uint32_t MAX_SLAVE_CACHE_LEN = 64 * 1024; /* 24c512 的容量上限 */ | ||||||||||||
| 50 | +constexpr uint32_t MAX_READ_RETRY = 100; /* read 重试次数上限,防止异常参数回绕或长时间占用总线 */ | ||||||||||||
| 51 | +constexpr useconds_t READ_RETRY_DELAY_US = 1000; /* 两次读重试之间的退避,避免瞬时错误在恢复前被连续打空 */ | ||||||||||||
| 52 | +/* 内核用 0x1000 标志区分 slave client,sysfs 目录名会变成 <bus>-10xx */ | ||||||||||||
| 53 | +constexpr uint32_t I2C_ADDR_OFFSET_SLAVE = 0x1000; | ||||||||||||
| 54 | + | ||||||||||||
| 55 | +/* 文件描述符 RAII:保证 open 与 close 之间的异常路径不会泄漏 fd */ | ||||||||||||
| 56 | +class FdGuard { | ||||||||||||
| 57 | +public: | ||||||||||||
| 58 | + explicit FdGuard(int fd) : m_fd(fd) {} | ||||||||||||
| 59 | + ~FdGuard() | ||||||||||||
| 60 | + { | ||||||||||||
| 61 | + if (m_fd >= 0) { | ||||||||||||
| 62 | + ::close(m_fd); | ||||||||||||
| 63 | + } | ||||||||||||
| 64 | + } | ||||||||||||
| 65 | + | ||||||||||||
| 66 | + FdGuard(const FdGuard&) = delete; | ||||||||||||
| 67 | + FdGuard& operator=(const FdGuard&) = delete; | ||||||||||||
| 68 | + | ||||||||||||
| 69 | + int get() const | ||||||||||||
| 70 | + { | ||||||||||||
| 71 | + return m_fd; | ||||||||||||
| 72 | + } | ||||||||||||
| 73 | + | ||||||||||||
| 74 | +private: | ||||||||||||
| 75 | + int m_fd; | ||||||||||||
| 76 | +}; | ||||||||||||
| 77 | + | ||||||||||||
| 78 | +struct Request { | ||||||||||||
| 79 | + uint8_t addr; /* 7bit 从机地址 */ | ||||||||||||
| 80 | + string_view_t payload; /* 地址字节之后的数据 */ | ||||||||||||
| 81 | +}; | ||||||||||||
| 82 | + | ||||||||||||
| 83 | +/* in_data[0] = (addr << 1) | rw,与 I2cWriteMsg / 通用驱动的报文格式一致 */ | ||||||||||||
| 84 | +bool parse_request(const string_view_t& in, Request& req) | ||||||||||||
| 85 | +{ | ||||||||||||
| 86 | + if (in.empty()) { | ||||||||||||
| 87 | + return false; | ||||||||||||
| 88 | + } | ||||||||||||
| 89 | + const uint8_t raw = static_cast<uint8_t>(in[0]); | ||||||||||||
| 90 | + req.addr = raw >> 1; | ||||||||||||
| 91 | + req.payload = in.substr(1); | ||||||||||||
| 92 | + /* 0 是广播地址;payload 上限与内核 i2c_msg.len 的实际使用范围对齐 */ | ||||||||||||
| 93 | + return req.addr != 0 && req.payload.size() <= MAX_XFER_LEN; | ||||||||||||
| 94 | +} | ||||||||||||
| 95 | + | ||||||||||||
| 96 | +string_t slave_dir_name(int32_t bus, uint32_t addr) | ||||||||||||
| 97 | +{ | ||||||||||||
| 98 | + char buffer[16] = {0}; | ||||||||||||
| 99 | + std::snprintf(buffer, sizeof(buffer), "%d-%04x", bus, static_cast<unsigned int>(addr)); | ||||||||||||
| 100 | + return string_t(buffer); | ||||||||||||
| 101 | +} | ||||||||||||
| 102 | + | ||||||||||||
| 103 | +} // namespace | ||||||||||||
| 104 | + | ||||||||||||
| 105 | +class I2cImpl : public I2c { | ||||||||||||
| 106 | +public: | ||||||||||||
| 107 | + explicit I2cImpl(int32_t index) : m_bus(index), m_dev(DEV_PREFIX + std::to_string(index)) | ||||||||||||
| 108 | + { | ||||||||||||
| 109 | + if (index < 0 || static_cast<uint32_t>(index) >= MAX_BUS_NUM) { | ||||||||||||
| 110 | + throw std::runtime_error("request error, index is invalid, index:" + std::to_string(index) + | ||||||||||||
| 111 | + ", expect 0.." + std::to_string(MAX_BUS_NUM - 1)); | ||||||||||||
| 112 | + } | ||||||||||||
| 113 | + | ||||||||||||
| 114 | + /* 构造期解析并打开设备;总线未使能/无权限等问题在 load 阶段暴露 */ | ||||||||||||
| 115 | + m_fd = ::open(m_dev.c_str(), O_RDWR | O_CLOEXEC); | ||||||||||||
| 116 | + if (m_fd < 0) { | ||||||||||||
| 117 | + const int error = errno; | ||||||||||||
| 118 | + throw std::runtime_error("I2C device " + m_dev + " open failed: " + std::strerror(error) + | ||||||||||||
| 119 | + "; check that the bus is enabled in the DTS, " | ||||||||||||
| 120 | + "CONFIG_I2C_CHARDEV is enabled, and the device is accessible"); | ||||||||||||
| 121 | + } | ||||||||||||
| 122 | + } | ||||||||||||
| 123 | + | ||||||||||||
| 124 | + ~I2cImpl() override { close_fd(); } | ||||||||||||
| 125 | + | ||||||||||||
| 126 | + void init(void* args, uint32_t size) override | ||||||||||||
| 127 | + { | ||||||||||||
| 128 | + if (args == nullptr || size != sizeof(I2cConfig)) { | ||||||||||||
| 129 | + throw std::runtime_error("i2c init config is invalid"); | ||||||||||||
| 130 | + } | ||||||||||||
| 131 | + | ||||||||||||
| 132 | + const auto* cfg = static_cast<const I2cConfig*>(args); | ||||||||||||
| 133 | + if (cfg->mode > 1) { | ||||||||||||
| 134 | + throw std::runtime_error("i2c mode is invalid: " + std::to_string(cfg->mode) + ", expect 0 or 1"); | ||||||||||||
| 135 | + } | ||||||||||||
| 136 | + | ||||||||||||
| 137 | + std::lock_guard<std::mutex> io_lock(m_io_mutex); | ||||||||||||
| 138 | + | ||||||||||||
| 139 | + /* 实际速率由 DTS bus-frequency 决定,用户态只记录 */ | ||||||||||||
| 140 | + m_speed = cfg->speed; | ||||||||||||
| 141 | + /* 重新初始化时先清空旧配置,避免失败后残留上一条从机路径 */ | ||||||||||||
| 142 | + m_slave_path.clear(); | ||||||||||||
| 143 | + | ||||||||||||
| 144 | + if (cfg->use_smbus != 0) { | ||||||||||||
| 145 | + std::fprintf(stderr, "[NOTICE] %s: use_smbus is ignored, raw I2C transfers are used\n", | ||||||||||||
| 146 | + get_driver_name().c_str()); | ||||||||||||
| 147 | + } | ||||||||||||
| 148 | + | ||||||||||||
| 149 | + if (cfg->mode == 0) { | ||||||||||||
| 150 | + return; | ||||||||||||
| 151 | + } | ||||||||||||
| 152 | + | ||||||||||||
| 153 | + if (cfg->addr < 0x01 || cfg->addr > 0x7F) { | ||||||||||||
| 154 | + throw std::runtime_error("i2c slave address is invalid: " + std::to_string(cfg->addr) + | ||||||||||||
| 155 | + ", expect 0x01..0x7F"); | ||||||||||||
| 156 | + } | ||||||||||||
| 157 | + | ||||||||||||
| 158 | + /* | ||||||||||||
| 159 | + * Linux 会把 I2C_CLIENT_SLAVE 编码进 device name: | ||||||||||||
| 160 | + * 地址 0x10 的 slave client 目录是 <bus>-1010,而不是 <bus>-0010。 | ||||||||||||
| 161 | + * 同时兼容 DTS/new_device 未带 slave 标志的情况。 | ||||||||||||
| 162 | + */ | ||||||||||||
| 163 | + const string_t slave_path = string_t(SYSFS_PREFIX) + slave_dir_name(m_bus, cfg->addr | I2C_ADDR_OFFSET_SLAVE) + | ||||||||||||
| 164 | + SLAVE_EEPROM; | ||||||||||||
| 165 | + const string_t plain_path = string_t(SYSFS_PREFIX) + slave_dir_name(m_bus, cfg->addr) + SLAVE_EEPROM; | ||||||||||||
| 166 | + if (::access(slave_path.c_str(), F_OK) == 0) { | ||||||||||||
| 167 | + m_slave_path = slave_path; | ||||||||||||
| 168 | + } else if (::access(plain_path.c_str(), F_OK) == 0) { | ||||||||||||
| 169 | + m_slave_path = plain_path; | ||||||||||||
| 170 | + } else { | ||||||||||||
| 171 | + const int error = errno; | ||||||||||||
| 172 | + throw std::runtime_error("slave-eeprom not found: tried " + slave_path + " and " + plain_path + ": " + | ||||||||||||
| 173 | + std::strerror(error) + | ||||||||||||
| 174 | + "; check the slave node in the DTS or instantiate it via new_device"); | ||||||||||||
| 175 | + } | ||||||||||||
| 176 | + } | ||||||||||||
| 177 | + | ||||||||||||
| 178 | + void lock(void) override { m_hw_mutex.lock(); } | ||||||||||||
| 179 | + | ||||||||||||
| 180 | + void unlock(void) override { m_hw_mutex.unlock(); } | ||||||||||||
| 181 | + | ||||||||||||
| 182 | + string_t get_version(void) override { return DRIVER_VERSION; } | ||||||||||||
| 183 | + | ||||||||||||
| 184 | + string_t get_driver_name(void) const override { return "i2c"; } | ||||||||||||
| 185 | + | ||||||||||||
| 186 | + /* 对齐 PWM:未定义的扩展命令返回 -1,不伪装成功 */ | ||||||||||||
| 187 | + int32_t control(uint32_t cmd, void* args_in, void* args_out) override | ||||||||||||
| 188 | + { | ||||||||||||
| 189 | + (void)cmd; | ||||||||||||
| 190 | + (void)args_in; | ||||||||||||
| 191 | + (void)args_out; | ||||||||||||
| 192 | + return -1; | ||||||||||||
| 193 | + } | ||||||||||||
| 194 | + | ||||||||||||
| 195 | + std::tuple<int32_t, std::optional<string_t>> read(uint32_t length, uint32_t time_out, uint32_t retry, | ||||||||||||
| 196 | + const string_view_t& in_data) override | ||||||||||||
| 197 | + { | ||||||||||||
| 198 | + if (length == 0 || length > MAX_XFER_LEN) { | ||||||||||||
| 199 | + throw std::runtime_error("i2c read length is invalid: " + std::to_string(length) + ", expect 1.." + | ||||||||||||
| 200 | + std::to_string(MAX_XFER_LEN)); | ||||||||||||
| 201 | + } | ||||||||||||
| 202 | + | ||||||||||||
| 203 | + Request req{}; | ||||||||||||
| 204 | + if (!parse_request(in_data, req)) { | ||||||||||||
| 205 | + throw std::runtime_error( | ||||||||||||
| 206 | + "i2c read in_data is invalid: first byte must be (addr7 << 1), payload must be <= 2048"); | ||||||||||||
| 207 | + } | ||||||||||||
| 208 | + /* retry 来自上层;负数被转换为 uint32_t 后会是 0xFFFFFFFF,必须显式拒绝, | ||||||||||||
| 209 | + * 否则 retry + 1 回绕为 0,会在未发起任何传输时返回误导性的 EIO。 */ | ||||||||||||
| 210 | + if (retry > MAX_READ_RETRY) { | ||||||||||||
| 211 | + throw std::runtime_error("i2c read retry is invalid: " + std::to_string(retry) + ", expect 0.." + | ||||||||||||
| 212 | + std::to_string(MAX_READ_RETRY)); | ||||||||||||
| 213 | + } | ||||||||||||
| 214 | + | ||||||||||||
| 215 | + std::lock_guard<std::mutex> io_lock(m_io_mutex); | ||||||||||||
| 216 | + /* 地址随 i2c_msg.addr 下发,不需要 I2C_SLAVE;该 ioctl 对已被内核驱动 | ||||||||||||
| 217 | + * 占用的地址会返回 EBUSY,而 I2C_RDWR 本身不做这个占用检查。 */ | ||||||||||||
| 218 | + | ||||||||||||
| 219 | + std::vector<uint8_t> rx(length); | ||||||||||||
| 220 | + std::array<struct i2c_msg, 2> msgs{}; | ||||||||||||
| 221 | + uint32_t n = 0; | ||||||||||||
| 222 | + if (!req.payload.empty()) { | ||||||||||||
| 223 | + msgs[n].addr = req.addr; | ||||||||||||
| 224 | + msgs[n].flags = 0; | ||||||||||||
| 225 | + msgs[n].len = static_cast<uint16_t>(req.payload.size()); | ||||||||||||
| 226 | + msgs[n].buf = const_cast<uint8_t*>(reinterpret_cast<const uint8_t*>(req.payload.data())); | ||||||||||||
| 227 | + ++n; | ||||||||||||
| 228 | + } | ||||||||||||
| 229 | + msgs[n].addr = req.addr; | ||||||||||||
| 230 | + msgs[n].flags = I2C_M_RD; | ||||||||||||
| 231 | + msgs[n].len = static_cast<uint16_t>(length); | ||||||||||||
| 232 | + msgs[n].buf = rx.data(); | ||||||||||||
| 233 | + ++n; | ||||||||||||
| 234 | + | ||||||||||||
| 235 | + /* 两段 msg 走一次 I2C_RDWR,写偏移 + 读数据之间是 repeated start */ | ||||||||||||
| 236 | + struct i2c_rdwr_ioctl_data xfer = {msgs.data(), n}; | ||||||||||||
| 237 | + const uint32_t attempts = retry + 1; | ||||||||||||
| 238 | + const auto deadline = std::chrono::steady_clock::now() + std::chrono::milliseconds(time_out); | ||||||||||||
| 239 | + int32_t last_error = EIO; | ||||||||||||
| 240 | + | ||||||||||||
| 241 | + for (uint32_t i = 0; i < attempts; ++i) { | ||||||||||||
| 242 | + if (::ioctl(m_fd, I2C_RDWR, &xfer) >= 0) { | ||||||||||||
| 243 | + return {0, string_t(reinterpret_cast<const char*>(rx.data()), rx.size())}; | ||||||||||||
| 244 | + } | ||||||||||||
| 245 | + last_error = errno; | ||||||||||||
| 246 | + if (time_out > 0 && std::chrono::steady_clock::now() >= deadline) { | ||||||||||||
| 247 | + break; | ||||||||||||
| 248 | + } | ||||||||||||
🔵 Low Priority 变更行:drivers/i2c/i2c.cpp:211-219,重试循环只在一次 ioctl 失败后检查 影响行为: 失败模式:对瞬时性错误重试完全无效。典型场景:EEPROM 写操作后立刻读(写周期约 5ms,期间从机会 NACK)或总线仲裁丢失/时钟拉伸超时,此时所有重试都挤在问题消失之前发生,全部失败并返回最后一次 errno; 建议:在 deadline 判断之后、循环末尾加入固定的短退避(如 usleep(1000)),使 time_out 窗口内真正分散重试;否则 retry 参数对瞬时错误不起作用。 Suggestions
![]() ![]() Incorrect? | |||||||||||||
| 249 | + /* 最后一次失败后不再等待;其余情况在两次尝试之间退避, | ||||||||||||
| 250 | + * 否则 retry 会在微秒级内全部打空,对 EEPROM 写周期、仲裁丢失等瞬时错误无效。 */ | ||||||||||||
| 251 | + if (i + 1 < attempts) { | ||||||||||||
| 252 | + ::usleep(READ_RETRY_DELAY_US); | ||||||||||||
| 253 | + } | ||||||||||||
| 254 | + } | ||||||||||||
| 255 | + return {last_error, std::nullopt}; | ||||||||||||
| 256 | + } | ||||||||||||
| 257 | + | ||||||||||||
| 258 | + int32_t write(const string_view_t& in_data, uint32_t time_out, uint8_t write_delay) override | ||||||||||||
| 259 | + { | ||||||||||||
| 260 | + (void)time_out; /* i2c-dev 无单次传输超时,写操作只尝试一次 */ | ||||||||||||
| 261 | + | ||||||||||||
| 262 | + Request req{}; | ||||||||||||
| 263 | + if (!parse_request(in_data, req)) { | ||||||||||||
| 264 | + throw std::runtime_error( | ||||||||||||
| 265 | + "i2c write in_data is invalid: first byte must be (addr7 << 1), payload must be <= 2048"); | ||||||||||||
| 266 | + } | ||||||||||||
| 267 | + | ||||||||||||
| 268 | + int32_t ret = 0; | ||||||||||||
| 269 | + { | ||||||||||||
| 270 | + std::lock_guard<std::mutex> io_lock(m_io_mutex); | ||||||||||||
| 271 | + /* 同 read:地址由 i2c_msg.addr 携带,不调用 I2C_SLAVE,避免 | ||||||||||||
| 272 | + * 已被内核驱动占用地址时误报 EBUSY。 */ | ||||||||||||
| 273 | + uint8_t dummy = 0; | ||||||||||||
| 274 | + struct i2c_msg msg{}; | ||||||||||||
| 275 | + msg.addr = req.addr; | ||||||||||||
| 276 | + msg.flags = 0; | ||||||||||||
| 277 | + msg.len = static_cast<uint16_t>(req.payload.size()); | ||||||||||||
| 278 | + /* payload 为空表示 SMBus Quick,内核按零长度消息处理 */ | ||||||||||||
| 279 | + msg.buf = req.payload.empty() | ||||||||||||
| 280 | + ? &dummy | ||||||||||||
| 281 | + : const_cast<uint8_t*>(reinterpret_cast<const uint8_t*>(req.payload.data())); | ||||||||||||
| 282 | + struct i2c_rdwr_ioctl_data xfer = {&msg, 1}; | ||||||||||||
| 283 | + if (::ioctl(m_fd, I2C_RDWR, &xfer) < 0) { | ||||||||||||
| 284 | + ret = errno; | ||||||||||||
| 285 | + } | ||||||||||||
| 286 | + } | ||||||||||||
| 287 | + | ||||||||||||
| 288 | + if (write_delay > 0) { | ||||||||||||
| 289 | + ::usleep(static_cast<useconds_t>(write_delay) * 1000); | ||||||||||||
| 290 | + } | ||||||||||||
| 291 | + return ret; | ||||||||||||
| 292 | + } | ||||||||||||
| 293 | + | ||||||||||||
| 294 | + std::string slave_cache_read(uint32_t offset, uint32_t size) override | ||||||||||||
| 295 | + { | ||||||||||||
| 296 | + if (size > MAX_SLAVE_CACHE_LEN) { | ||||||||||||
| 297 | + throw std::runtime_error("slave cache read size is too large: " + std::to_string(size)); | ||||||||||||
| 298 | + } | ||||||||||||
| 299 | + // 与 read() 的参数校验保持同一口径:越界在入口处报参数错误, | ||||||||||||
| 300 | + // 而不是把非法 offset 透传给 sysfs 后再收到 short read/EIO。 | ||||||||||||
| 301 | + if (size != 0 && offset > MAX_SLAVE_CACHE_LEN - size) { | ||||||||||||
| 302 | + throw std::runtime_error("slave cache read range is invalid: offset " + std::to_string(offset) + ", size " + | ||||||||||||
| 303 | + std::to_string(size) + ", expect offset + size <= " + | ||||||||||||
| 304 | + std::to_string(MAX_SLAVE_CACHE_LEN)); | ||||||||||||
| 305 | + } | ||||||||||||
| 306 | + | ||||||||||||
| 307 | + std::lock_guard<std::mutex> io_lock(m_io_mutex); | ||||||||||||
| 308 | + if (m_slave_path.empty()) { | ||||||||||||
| 309 | + throw std::runtime_error("slave-eeprom is not configured; call init(mode=1, addr) first"); | ||||||||||||
| 310 | + } | ||||||||||||
| 311 | + if (size == 0) { | ||||||||||||
| 312 | + return {}; | ||||||||||||
| 313 | + } | ||||||||||||
| 314 | + | ||||||||||||
| 315 | + const int fd = ::open(m_slave_path.c_str(), O_RDONLY | O_CLOEXEC); | ||||||||||||
🔵 Low Priority 变更行:i2c.cpp:315 问题: 这是可选优化(非必需修复),无功能错误:把 fd 在 建议:在 init() 解析出 m_slave_path 后按需打开一次并缓存 fd(成员 m_slave_fd),slave_cache_read/write 复用该 fd,在 init 重新配置或析构时关闭;避免每次读写都 open/close 同一 sysfs 节点。 ![]() ![]() Incorrect? | |||||||||||||
| 316 | + if (fd < 0) { | ||||||||||||
| 317 | + const int error = errno; | ||||||||||||
| 318 | + throw std::runtime_error("open " + m_slave_path + " failed: " + std::strerror(error)); | ||||||||||||
| 319 | + } | ||||||||||||
| 320 | + const FdGuard fd_guard(fd); | ||||||||||||
| 321 | + | ||||||||||||
| 322 | + std::vector<char> buffer(size); | ||||||||||||
| 323 | + const ssize_t n = ::pread(fd_guard.get(), buffer.data(), size, static_cast<off_t>(offset)); | ||||||||||||
| 324 | + const int error = errno; | ||||||||||||
| 325 | + if (n < 0) { | ||||||||||||
| 326 | + throw std::runtime_error("pread " + m_slave_path + " failed: " + std::strerror(error)); | ||||||||||||
| 327 | + } | ||||||||||||
| 328 | + if (static_cast<uint32_t>(n) != size) { | ||||||||||||
| 329 | + throw std::runtime_error("short read from " + m_slave_path + ": got " + std::to_string(n) + " of " + | ||||||||||||
| 330 | + std::to_string(size)); | ||||||||||||
| 331 | + } | ||||||||||||
🟡 Medium Priority 变更行:i2c.cpp:322-331(slave_cache_read 用一次 影响行为:入口校验允许 失败模式: 建议:把单次 pread/pwrite 改为 ≤4096 字节的分块循环(累加 offset 与已拷贝长度),或把允许的最大长度收敛到 PAGE_SIZE 并在参数校验处拒绝更大的 size,避免"校验通过但调用必失败"。 ![]() ![]() Incorrect? | |||||||||||||
| 332 | + return std::string(buffer.begin(), buffer.end()); | ||||||||||||
| 333 | + } | ||||||||||||
| 334 | + | ||||||||||||
| 335 | + int32_t slave_cache_write(uint32_t offset, const string_view_t& val) override | ||||||||||||
| 336 | + { | ||||||||||||
| 337 | + if (val.size() > MAX_SLAVE_CACHE_LEN) { | ||||||||||||
| 338 | + throw std::runtime_error("slave cache write size is too large: " + std::to_string(val.size())); | ||||||||||||
| 339 | + } | ||||||||||||
| 340 | + // 同 read:offset + size 越界在入口处直接报参数错误,避免 pwrite 透传后语义模糊。 | ||||||||||||
| 341 | + if (!val.empty() && offset > MAX_SLAVE_CACHE_LEN - static_cast<uint32_t>(val.size())) { | ||||||||||||
| 342 | + throw std::runtime_error("slave cache write range is invalid: offset " + std::to_string(offset) + | ||||||||||||
| 343 | + ", size " + std::to_string(val.size()) + ", expect offset + size <= " + | ||||||||||||
| 344 | + std::to_string(MAX_SLAVE_CACHE_LEN)); | ||||||||||||
| 345 | + } | ||||||||||||
| 346 | + | ||||||||||||
| 347 | + std::lock_guard<std::mutex> io_lock(m_io_mutex); | ||||||||||||
| 348 | + if (m_slave_path.empty()) { | ||||||||||||
| 349 | + throw std::runtime_error("slave-eeprom is not configured; call init(mode=1, addr) first"); | ||||||||||||
| 350 | + } | ||||||||||||
| 351 | + if (val.empty()) { | ||||||||||||
| 352 | + return 0; | ||||||||||||
| 353 | + } | ||||||||||||
| 354 | + | ||||||||||||
| 355 | + const int fd = ::open(m_slave_path.c_str(), O_WRONLY | O_CLOEXEC); | ||||||||||||
| 356 | + if (fd < 0) { | ||||||||||||
| 357 | + const int error = errno; | ||||||||||||
| 358 | + throw std::runtime_error("open " + m_slave_path + " failed: " + std::strerror(error)); | ||||||||||||
| 359 | + } | ||||||||||||
| 360 | + const FdGuard fd_guard(fd); | ||||||||||||
| 361 | + | ||||||||||||
| 362 | + const ssize_t n = ::pwrite(fd_guard.get(), val.data(), val.size(), static_cast<off_t>(offset)); | ||||||||||||
| 363 | + const int error = errno; | ||||||||||||
| 364 | + if (n < 0) { | ||||||||||||
| 365 | + throw std::runtime_error("pwrite " + m_slave_path + " failed: " + std::strerror(error)); | ||||||||||||
| 366 | + } | ||||||||||||
| 367 | + if (static_cast<size_t>(n) != val.size()) { | ||||||||||||
| 368 | + throw std::runtime_error("short write to " + m_slave_path + ": wrote " + std::to_string(n) + " of " + | ||||||||||||
| 369 | + std::to_string(val.size())); | ||||||||||||
| 370 | + } | ||||||||||||
| 371 | + return 0; | ||||||||||||
| 372 | + } | ||||||||||||
| 373 | + | ||||||||||||
| 374 | + void reset(void) override | ||||||||||||
| 375 | + { | ||||||||||||
| 376 | + std::fprintf(stderr, | ||||||||||||
| 377 | + "[NOTICE] %s: reset is not available via i2c-dev; fd unchanged, no controller reset; " | ||||||||||||
| 378 | + "the kernel will try SCL recovery on the next transfer\n", | ||||||||||||
| 379 | + get_driver_name().c_str()); | ||||||||||||
| 380 | + } | ||||||||||||
| 381 | + | ||||||||||||
| 382 | +private: | ||||||||||||
| 383 | + void close_fd() | ||||||||||||
| 384 | + { | ||||||||||||
| 385 | + if (m_fd >= 0) { | ||||||||||||
| 386 | + ::close(m_fd); | ||||||||||||
| 387 | + m_fd = -1; | ||||||||||||
| 388 | + } | ||||||||||||
| 389 | + } | ||||||||||||
| 390 | + | ||||||||||||
| 391 | + int32_t m_bus; | ||||||||||||
| 392 | + string_t m_dev; | ||||||||||||
| 393 | + string_t m_slave_path; | ||||||||||||
| 394 | + [[maybe_unused]] uint32_t m_speed{0}; | ||||||||||||
| 395 | + int m_fd{-1}; | ||||||||||||
| 396 | + std::mutex m_hw_mutex; /* lock()/unlock():调用方的多步事务锁 */ | ||||||||||||
| 397 | + std::mutex m_io_mutex; /* 单次 ioctl 的原子性,避免多次传输交错 */ | ||||||||||||
| 398 | +}; | ||||||||||||
| 399 | + | ||||||||||||
| 400 | +} // namespace BCAL_I2C_ASPEED | ||||||||||||
| 401 | + | ||||||||||||
| 402 | +extern "C" { | ||||||||||||
| 403 | + | ||||||||||||
| 404 | +__attribute__((visibility("default"))) | ||||||||||||
| 405 | +bcal::IDriver* create_driver(int32_t index) | ||||||||||||
| 406 | +{ | ||||||||||||
| 407 | + /* 构造函数在 bus 不存在/无权限时会抛异常;异常不能穿越 extern "C" 边界 */ | ||||||||||||
| 408 | + try { | ||||||||||||
| 409 | + return new BCAL_I2C_ASPEED::I2cImpl(index); | ||||||||||||
| 410 | + } catch (const std::exception& e) { | ||||||||||||
| 411 | + std::fprintf(stderr, "[ERROR] i2c: create driver(index=%d) failed: %s\n", static_cast<int>(index), e.what()); | ||||||||||||
| 412 | + return nullptr; | ||||||||||||
| 413 | + } catch (...) { | ||||||||||||
| 414 | + std::fprintf(stderr, "[ERROR] i2c: create driver(index=%d) failed: unknown exception\n", | ||||||||||||
| 415 | + static_cast<int>(index)); | ||||||||||||
| 416 | + return nullptr; | ||||||||||||
| 417 | + } | ||||||||||||
| 418 | +} | ||||||||||||
| 419 | + | ||||||||||||
| 420 | +__attribute__((visibility("default"))) | ||||||||||||
| 421 | +void destroy_driver(bcal::IDriver* driver) | ||||||||||||
| 422 | +{ | ||||||||||||
| 423 | + delete driver; | ||||||||||||
| 424 | +} | ||||||||||||
| 425 | + | ||||||||||||
| 426 | +} // extern "C" | ||||||||||||
| @@ -0,0 +1,12 @@ | |||
| 1 | +driver_name = 'i2c' | ||
| 2 | + | ||
| 3 | +sources = files('i2c.cpp') | ||
| 4 | + | ||
| 5 | +i2c_lib = shared_library(driver_name, | ||
| 6 | + sources, | ||
| 7 | + dependencies: [bcal_dep], | ||
| 8 | + # 默认隐藏,create_driver/destroy_driver 靠 visibility("default") 显式导出 | ||
| 9 | + cpp_args: ['-fvisibility=hidden', '-fvisibility-inlines-hidden'], | ||
| 10 | + install: true, | ||
| 11 | + install_dir: driver_install_dir | ||
| 12 | +) | ||
| @@ -6,3 +6,4 @@ subdir('adc') | |||
| 6 | subdir('pwm') | 6 | subdir('pwm') |
| 7 | subdir('wdt') | 7 | subdir('wdt') |
| 8 | subdir('jtag') | 8 | subdir('jtag') |
| 9 | +subdir('i2c') | ||
| @@ -2,7 +2,7 @@ | |||
| 2 | "name": "libsoc_adapter_aspeed", | 2 | "name": "libsoc_adapter_aspeed", |
| 3 | "type": "application", | 3 | "type": "application", |
| 4 | "deployConfig": "", | 4 | "deployConfig": "", |
| 5 | - "version": "1.0.9", | 5 | + "version": "1.0.10", |
| 6 | "license": "Mulan PSL v2", | 6 | "license": "Mulan PSL v2", |
| 7 | "description": "BMC chip adapter for Aspeed.", | 7 | "description": "BMC chip adapter for Aspeed.", |
| 8 | "dependencies": { | 8 | "dependencies": { |


🔵 Low Priority
变更行:i2c.cpp:133
if (cfg->mode > 1) {。问题:这里用"大于 1"来拒绝非法 mode,属于单边区间校验。若 bcal 的
I2cConfig::mode是有符号类型(该结构体同时含addr、speed等配置项,几种常见写法都是int32_t),传mode = -1时-1 > 1为 false,不会被拒绝;随后 149 行cfg->mode == 0也不成立,于是负值被当成"从机模式"继续往下走,去校验 addr 并拼<bus>-10xx/slave-eeprom路径。这与 134 行错误文案声明的 "expect 0 or 1" 不符:非法值没有被拒绝,而是被当作合法模式使用,最终报出的是 "slave-eeprom not found" 或 "slave address is invalid" 这类与真实原因无关的错误,误导现场定位。触发条件:上层(Lua 绑定/配置解析)传出负数 mode。若
mode为无符号类型则该路径不存在,故置信度较低。修复方向:改成双边白名单校验
if (cfg->mode != 0 && cfg->mode != 1),与错误文案声明完全一致。