/*
* 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"
#include "programmer.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;