# Copyright 2018 syzkaller project authors. All rights reserved.
# Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.

include <uapi/linux/i2c-dev.h>
include <uapi/linux/i2c.h>

define I2C_MAX_BLOCK	(I2C_SMBUS_BLOCK_MAX + 2)

resource fd_i2c[fd]

syz_open_dev$I2C(dev ptr[in, string["/dev/i2c-#"]], id intptr, flags flags[open_flags]) fd_i2c

ioctl$I2C_RETRIES(fd fd_i2c, cmd const[I2C_RETRIES], arg intptr)
ioctl$I2C_TIMEOUT(fd fd_i2c, cmd const[I2C_TIMEOUT], arg intptr)
ioctl$I2C_SLAVE(fd fd_i2c, cmd const[I2C_SLAVE], arg intptr[0:0x3ff])
ioctl$I2C_SLAVE_FORCE(fd fd_i2c, cmd const[I2C_SLAVE_FORCE], arg intptr[0:0x3ff])
ioctl$I2C_TENBIT(fd fd_i2c, cmd const[I2C_TENBIT], arg boolptr)
ioctl$I2C_PEC(fd fd_i2c, cmd const[I2C_PEC], arg intptr)

ioctl$I2C_FUNCS(fd fd_i2c, cmd const[I2C_FUNCS], arg ptr[in, intptr])
ioctl$I2C_RDWR(fd fd_i2c, cmd const[I2C_RDWR], arg ptr[in, i2c_rdwr_ioctl_data])
ioctl$I2C_SMBUS(fd fd_i2c, cmd const[I2C_SMBUS], arg ptr[in, i2c_smbus_ioctl_data])

i2c_smbus_ioctl_data {
	read_write	bool8
	command		int8
	size		int32[I2C_SMBUS_QUICK:I2C_SMBUS_I2C_BLOCK_DATA]
	data		ptr[in, i2c_smbus_data]
}

i2c_smbus_data [
	byte	int8
	word	int16
	block	array[int8, I2C_MAX_BLOCK]
]

i2c_rdwr_ioctl_data {
	msgs	ptr[in, array[i2c_msg]]
	nmsgs	len[msgs, int32]
}

i2c_msg {
	addr	int16
	flags	flags[i2c_msg_flags, int16]
	len	len[buf, int16]
	buf	ptr[in, array[int8]]
}

i2c_msg_flags = I2C_M_RD, I2C_M_TEN, I2C_M_DMA_SAFE, I2C_M_RECV_LEN, I2C_M_NO_RD_ACK, I2C_M_IGNORE_NAK, I2C_M_REV_DIR_ADDR, I2C_M_NOSTART, I2C_M_STOP