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>
20 #include <fcntl.h>
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 #include "programmer.h"
31
32 /* Null characters are placeholders for bus number digits */
33 #define I2C_DEV_PREFIX "/dev/i2c-\0\0\0"
34 #define I2C_MAX_BUS 255
35
i2c_close(int fd)36 int i2c_close(int fd)
37 {
38 return fd == -1 ? 0 : close(fd);
39 }
40
i2c_open_path(const char * path,uint16_t addr,int force)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);
54 return ret;
55 }
56
57 return fd;
58 }
59
i2c_open(int bus,uint16_t addr,int force)60 int i2c_open(int bus, uint16_t addr, int force)
61 {
62 char dev[sizeof(I2C_DEV_PREFIX)] = {0};
63
64 int ret = -1;
65
66 if (bus < 0 || bus > I2C_MAX_BUS) {
67 msg_perr("Invalid I2C bus %d.\n", bus);
68 return ret;
69 }
70
71 ret = snprintf(dev, sizeof(dev), "%s%d", I2C_DEV_PREFIX, bus);
72 if (ret < 0) {
73 msg_perr("Unable to join bus number to device name: %s.\n", strerror(errno));
74 return ret;
75 }
76
77 return i2c_open_path(dev, addr, force);
78 }
79
get_bus_number(char * bus_str)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
i2c_open_from_programmer_params(const struct programmer_cfg * cfg,uint16_t addr,int force)99 int i2c_open_from_programmer_params(const struct programmer_cfg *cfg, uint16_t addr, int force)
100 {
101 int fd = -1;
102
103 char *bus_str = extract_programmer_param_str(cfg, "bus");
104 char *device_path = extract_programmer_param_str(cfg, "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
i2c_read(int fd,uint16_t addr,i2c_buffer_t * buf)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
i2c_write(int fd,uint16_t addr,const i2c_buffer_t * buf)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 }
153