Skip to content
This repository was archived by the owner on Oct 5, 2021. It is now read-only.

Commit 6271863

Browse files
committed
i2c: check controller capability for I2C_FUNC_I2C
Some controller only supports SMBUS and cannot be used for STM32 bootloader. Check controller capability and print error message. Signed-off-by: Antonio Borneo <borneo.antonio@gmail.com>
1 parent 50b7567 commit 6271863

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

i2c.c

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,11 @@ struct port_interface port_i2c = {
4949

5050
#ifdef __ANDROID__
5151
#define I2C_SLAVE 0x0703 /* Use this slave address */
52+
#define I2C_FUNCS 0x0705 /* Get the adapter functionality mask */
53+
/* To determine what functionality is present */
54+
#define I2C_FUNC_I2C 0x00000001
5255
#else
56+
#include <linux/i2c.h>
5357
#include <linux/i2c-dev.h>
5458
#endif
5559

@@ -65,6 +69,7 @@ static port_err_t i2c_open(struct port_interface *port,
6569
{
6670
struct i2c_priv *h;
6771
int fd, addr, ret;
72+
unsigned long funcs;
6873

6974
/* 1. check device name match */
7075
if (strncmp(ops->device, "/dev/i2c-", strlen("/dev/i2c-")))
@@ -91,10 +96,25 @@ static port_err_t i2c_open(struct port_interface *port,
9196
return PORT_ERR_UNKNOWN;
9297
}
9398

99+
/* 3.5. Check capabilities */
100+
ret = ioctl(fd, I2C_FUNCS, &funcs);
101+
if (ret < 0) {
102+
fprintf(stderr, "I2C ioctl(funcs) error %d\n", errno);
103+
close(fd);
104+
free(h);
105+
return PORT_ERR_UNKNOWN;
106+
}
107+
if ((funcs & I2C_FUNC_I2C) == 0) {
108+
fprintf(stderr, "Error: controller is not I2C, only SMBUS.\n");
109+
close(fd);
110+
free(h);
111+
return PORT_ERR_UNKNOWN;
112+
}
113+
94114
/* 4. set options */
95115
ret = ioctl(fd, I2C_SLAVE, addr);
96116
if (ret < 0) {
97-
fprintf(stderr, "I2C ioctl error %d\n", errno);
117+
fprintf(stderr, "I2C ioctl(slave) error %d\n", errno);
98118
close(fd);
99119
free(h);
100120
return PORT_ERR_UNKNOWN;

0 commit comments

Comments
 (0)