mirror of
https://review.coreboot.org/flashrom.git
synced 2025-04-27 07:02:34 +02:00

Some callers may find it easier to provide the path to an I2C device at which to communicate with the device, rather than specify the bus number- doing so might involve trying to parse a path to extract the number only for flashrom to do the reverse, which is error-prone and unnecessary. This change adds support for a `devpath` option, continuing to allow `bus` and requiring only one of them be specified. TEST=Verified --flash-size outputs correct values with both devpath=/dev/i2c-7 and bus=7, as well as noting that one is required if neither is specified and only one may be specified if both are given. Signed-off-by: Peter Marheine <pmarheine@chromium.org> Change-Id: Id2adf8a307b9205ce5e5804a6c3e22f19d0c34c9 Reviewed-on: https://review.coreboot.org/c/flashrom/+/51967 Reviewed-by: Edward O'Callaghan <quasisec@chromium.org> Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
107 lines
2.4 KiB
C
107 lines
2.4 KiB
C
/*
|
|
* This file is part of the flashrom project.
|
|
*
|
|
* Copyright (C) 2020 The Chromium OS Authors
|
|
*
|
|
* This program is free software; you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License as published by
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*/
|
|
|
|
#include <errno.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <fcntl.h>
|
|
#include <sys/ioctl.h>
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
|
#include <linux/i2c.h>
|
|
#include <linux/i2c-dev.h>
|
|
|
|
#include "flash.h"
|
|
#include "i2c_helper.h"
|
|
|
|
/* Null characters are placeholders for bus number digits */
|
|
#define I2C_DEV_PREFIX "/dev/i2c-\0\0\0"
|
|
#define I2C_MAX_BUS 255
|
|
|
|
int i2c_close(int fd)
|
|
{
|
|
return fd == -1 ? 0 : close(fd);
|
|
}
|
|
|
|
int i2c_open_path(const char *path, uint16_t addr, int force)
|
|
{
|
|
int fd = open(path, O_RDWR);
|
|
if (fd < 0) {
|
|
msg_perr("Unable to open I2C device %s: %s.\n", path, strerror(errno));
|
|
return fd;
|
|
}
|
|
|
|
int request = force ? I2C_SLAVE_FORCE : I2C_SLAVE;
|
|
int ret = ioctl(fd, request, addr);
|
|
if (ret < 0) {
|
|
msg_perr("Unable to set I2C slave address to 0x%02x: %s.\n", addr, strerror(errno));
|
|
i2c_close(fd);
|
|
return ret;
|
|
}
|
|
|
|
return fd;
|
|
}
|
|
|
|
|
|
int i2c_open(int bus, uint16_t addr, int force)
|
|
{
|
|
char dev[sizeof(I2C_DEV_PREFIX)] = {0};
|
|
|
|
int ret = -1;
|
|
|
|
if (bus < 0 || bus > I2C_MAX_BUS) {
|
|
msg_perr("Invalid I2C bus %d.\n", bus);
|
|
return ret;
|
|
}
|
|
|
|
ret = snprintf(dev, sizeof(dev), "%s%d", I2C_DEV_PREFIX, bus);
|
|
if (ret < 0) {
|
|
msg_perr("Unable to join bus number to device name: %s.\n", strerror(errno));
|
|
return ret;
|
|
}
|
|
|
|
return i2c_open_path(dev, addr, force);
|
|
}
|
|
|
|
int i2c_read(int fd, uint16_t addr, i2c_buffer_t *buf)
|
|
{
|
|
if (buf->len == 0)
|
|
return 0;
|
|
|
|
int ret = ioctl(fd, I2C_SLAVE, addr);
|
|
if (ret < 0) {
|
|
msg_perr("Unable to set I2C slave address to 0x%02x: %s.\n", addr, strerror(errno));
|
|
return ret;
|
|
}
|
|
|
|
return read(fd, buf->buf, buf->len);
|
|
}
|
|
|
|
int i2c_write(int fd, uint16_t addr, const i2c_buffer_t *buf)
|
|
{
|
|
if (buf->len == 0)
|
|
return 0;
|
|
|
|
int ret = ioctl(fd, I2C_SLAVE, addr);
|
|
if (ret < 0) {
|
|
msg_perr("Unable to set I2C slave address to 0x%02x: %s.\n", addr, strerror(errno));
|
|
return ret;
|
|
}
|
|
|
|
return write(fd, buf->buf, buf->len);
|
|
}
|