| 0/0 |
2026/01/30 14:35 |
flow |
patching |
18m
Results: map[KernelBranch:master KernelCommit:63804fed149a6750ffd28610c5c1c98cce6bd377 KernelRepo:git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git PatchDescription:HID: ft260: fix stack-out-of-bounds in ft260_smbus_xfer
The ft260_smbus_xfer function in drivers/hid/hid-ft260.c handles SMBus
block transfers by using the length byte provided by the user
(data->block[0]). However, it fails to validate that this length is
within the valid range (<= I2C_SMBUS_BLOCK_MAX).
This missing check allows a user to supply a large length (e.g., 42),
which leads to a stack-out-of-bounds read in ft260_smbus_write when
processing an I2C_SMBUS_I2C_BLOCK_DATA or I2C_SMBUS_BLOCK_DATA
transaction. The data->block buffer is part of the union i2c_smbus_data
allocated on the stack in i2cdev_ioctl_smbus, so reading past it
triggers KASAN.
Fix this by ensuring data->block[0] does not exceed I2C_SMBUS_BLOCK_MAX
(32) before proceeding with the transfer. This validation is added for
both read and write operations of block and I2C block transactions. PatchDiff:diff --git a/drivers/hid/hid-ft260.c b/drivers/hid/hid-ft260.c
index 333341e80..e97a8472a 100644
--- a/drivers/hid/hid-ft260.c
+++ b/drivers/hid/hid-ft260.c
@@ -708,6 +708,11 @@ static int ft260_smbus_xfer(struct i2c_adapter *adapter, u16 addr, u16 flags,
break;
case I2C_SMBUS_BLOCK_DATA:
if (read_write == I2C_SMBUS_READ) {
+ if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
+ ret = -EINVAL;
+ goto smbus_exit;
+ }
+
ret = ft260_smbus_write(dev, addr, cmd, NULL, 0,
FT260_FLAG_START);
if (ret)
@@ -717,6 +722,11 @@ static int ft260_smbus_xfer(struct i2c_adapter *adapter, u16 addr, u16 flags,
data->block[0] + 1,
FT260_FLAG_START_STOP_REPEATED);
} else {
+ if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
+ ret = -EINVAL;
+ goto smbus_exit;
+ }
+
ret = ft260_smbus_write(dev, addr, cmd, data->block,
data->block[0] + 1,
FT260_FLAG_START_STOP);
@@ -724,6 +734,11 @@ static int ft260_smbus_xfer(struct i2c_adapter *adapter, u16 addr, u16 flags,
break;
case I2C_SMBUS_I2C_BLOCK_DATA:
if (read_write == I2C_SMBUS_READ) {
+ if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
+ ret = -EINVAL;
+ goto smbus_exit;
+ }
+
ret = ft260_smbus_write(dev, addr, cmd, NULL, 0,
FT260_FLAG_START);
if (ret)
@@ -733,6 +748,11 @@ static int ft260_smbus_xfer(struct i2c_adapter *adapter, u16 addr, u16 flags,
data->block[0],
FT260_FLAG_START_STOP_REPEATED);
} else {
+ if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
+ ret = -EINVAL;
+ goto smbus_exit;
+ }
+
ret = ft260_smbus_write(dev, addr, cmd, data->block + 1,
data->block[0],
FT260_FLAG_START_STOP);
Recipients:[map[Email:bentiss@kernel.org Name:Benjamin Tissoires To:true] map[Email:jikos@kernel.org Name:Jiri Kosina To:true] map[Email:linux-i2c@vger.kernel.org Name: To:true] map[Email:linux-input@vger.kernel.org Name: To:true] map[Email:linux-kernel@vger.kernel.org Name: To:false] map[Email:michael.zaidman@gmail.com Name:Michael Zaidman To:true]]]
|
| 1/1 |
2026/01/30 14:35 |
action |
base-commit-picker |
0m
Results: map[KernelBranch:master KernelCommit:63804fed149a6750ffd28610c5c1c98cce6bd377 KernelRepo:git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git]
|
| 2/1 |
2026/01/30 14:35 |
action |
kernel-checkouter |
0m
Results: map[KernelSrc:/usr/local/google/home/dvyukov/syzkaller/agent/workdir/cache/src/8c64ca34582568f44cbf475e4f60a6f0b9d98e80]
|
| 3/1 |
2026/01/30 14:35 |
action |
kernel-builder |
0m
Results: map[KernelObj:/usr/local/google/home/dvyukov/syzkaller/agent/workdir/cache/build/075051b8b4ae86406308820d3c12d14af032f80b]
|
| 4/1 |
2026/01/30 14:35 |
action |
crash-reproducer |
0m
Results: map[BugTitle:KASAN: stack-out-of-bounds Read in ft260_smbus_write CrashReport:==================================================================
BUG: KASAN: stack-out-of-bounds in ft260_smbus_write+0x156/0x2f0 drivers/hid/hid-ft260.c:486
Read of size 42 at addr ffffc90002417d21 by task syz-executor165/6009
CPU: 0 UID: 0 PID: 6009 Comm: syz-executor165 Not tainted syzkaller #1 PREEMPT(full)
Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.17.0-debian-1.17.0-1 04/01/2014
Call Trace:
<TASK>
__dump_stack lib/dump_stack.c:94 [inline]
dump_stack_lvl+0x10e/0x190 lib/dump_stack.c:120
print_address_description mm/kasan/report.c:378 [inline]
print_report+0x17e/0x810 mm/kasan/report.c:482
kasan_report+0x147/0x180 mm/kasan/report.c:595
check_region_inline mm/kasan/generic.c:-1 [inline]
kasan_check_range+0x2b0/0x2c0 mm/kasan/generic.c:200
__asan_memcpy+0x29/0x70 mm/kasan/shadow.c:105
ft260_smbus_write+0x156/0x2f0 drivers/hid/hid-ft260.c:486
ft260_smbus_xfer+0x8bc/0xdd0 drivers/hid/hid-ft260.c:-1
__i2c_smbus_xfer+0x557/0x1e20 drivers/i2c/i2c-core-smbus.c:591
i2c_smbus_xfer+0x1f4/0x310 drivers/i2c/i2c-core-smbus.c:546
i2cdev_ioctl_smbus+0x3db/0x750 drivers/i2c/i2c-dev.c:389
i2cdev_ioctl+0x5d3/0x820 drivers/i2c/i2c-dev.c:478
vfs_ioctl fs/ioctl.c:51 [inline]
__do_sys_ioctl fs/ioctl.c:597 [inline]
__se_sys_ioctl+0xfc/0x170 fs/ioctl.c:583
do_syscall_x64 arch/x86/entry/syscall_64.c:63 [inline]
do_syscall_64+0xe8/0xf80 arch/x86/entry/syscall_64.c:94
entry_SYSCALL_64_after_hwframe+0x77/0x7f
RIP: 0033:0x7f1925b454f9
Code: c0 79 93 eb d5 48 8d 7c 1d 00 eb 99 0f 1f 44 00 00 48 89 f8 48 89 f7 48 89 d6 48 89 ca 4d 89 c2 4d 89 c8 4c 8b 4c 24 08 0f 05 <48> 3d 01 f0 ff ff 73 01 c3 48 c7 c1 d8 ff ff ff f7 d8 64 89 01 48
RSP: 002b:00007ffee7bc0ab8 EFLAGS: 00000246 ORIG_RAX: 0000000000000010
RAX: ffffffffffffffda RBX: 00007ffee7bc0c48 RCX: 00007f1925b454f9
RDX: 0000200000000200 RSI: 0000000000000720 RDI: 0000000000000004
RBP: 0000000000000001 R08: 0000000000000000 R09: 0000000000000009
R10: 0000000000000000 R11: 0000000000000246 R12: 00007ffee7bc0ae0
R13: 00007ffee7bc0ad0 R14: 00007f1925bd8c40 R15: 0000000000000000
</TASK>
The buggy address belongs to stack of task syz-executor165/6009
and is located at offset 33 in frame:
i2cdev_ioctl_smbus+0x0/0x750 drivers/i2c/i2c-dev.c:317
This frame has 1 object:
[32, 66) 'temp'
The buggy address belongs to a 8-page vmalloc region starting at 0xffffc90002410000 allocated at copy_process+0x4e7/0x3950 kernel/fork.c:2052
The buggy address belongs to the physical page:
page: refcount:1 mapcount:0 mapping:0000000000000000 index:0x0 pfn:0x213ff
flags: 0xfff00000000000(node=0|zone=1|lastcpupid=0x7ff)
raw: 00fff00000000000 0000000000000000 ffffea000084ffc8 0000000000000000
raw: 0000000000000000 0000000000000000 00000001ffffffff 0000000000000000
page dumped because: kasan: bad access detected
page_owner tracks the page as allocated
page last allocated via order 0, migratetype Unmovable, gfp_mask 0x29c2(GFP_NOWAIT|__GFP_HIGHMEM|__GFP_IO|__GFP_FS|__GFP_ZERO), pid 6007, tgid 6007 (syz-executor165), ts 90494270799, free_ts 87604917166
set_page_owner include/linux/page_owner.h:32 [inline]
post_alloc_hook+0x234/0x290 mm/page_alloc.c:1884
prep_new_page mm/page_alloc.c:1892 [inline]
get_page_from_freelist+0x24e4/0x2580 mm/page_alloc.c:3945
__alloc_frozen_pages_noprof+0x181/0x370 mm/page_alloc.c:5240
alloc_pages_mpol+0x232/0x4a0 mm/mempolicy.c:2486
alloc_frozen_pages_noprof mm/mempolicy.c:2557 [inline]
alloc_pages_noprof+0xa9/0x190 mm/mempolicy.c:2577
vm_area_alloc_pages mm/vmalloc.c:3649 [inline]
__vmalloc_area_node mm/vmalloc.c:3863 [inline]
__vmalloc_node_range_noprof+0x919/0x16a0 mm/vmalloc.c:4051
__vmalloc_node_noprof+0xc2/0x110 mm/vmalloc.c:4111
alloc_thread_stack_node kernel/fork.c:354 [inline]
dup_task_struct+0x250/0x9b0 kernel/fork.c:923
copy_process+0x4e7/0x3950 kernel/fork.c:2052
kernel_clone+0x21e/0x820 kernel/fork.c:2651
__do_sys_clone kernel/fork.c:2792 [inline]
__se_sys_clone kernel/fork.c:2776 [inline]
__x64_sys_clone+0x190/0x1f0 kernel/fork.c:2776
do_syscall_x64 arch/x86/entry/syscall_64.c:63 [inline]
do_syscall_64+0xe8/0xf80 arch/x86/entry/syscall_64.c:94
entry_SYSCALL_64_after_hwframe+0x77/0x7f
page last free pid 5954 tgid 5954 stack trace:
reset_page_owner include/linux/page_owner.h:25 [inline]
free_pages_prepare mm/page_alloc.c:1433 [inline]
__free_frozen_pages+0xbc4/0xd40 mm/page_alloc.c:2973
discard_slab mm/slub.c:3346 [inline]
__put_partials+0x146/0x170 mm/slub.c:3886
__slab_free+0x294/0x320 mm/slub.c:5956
qlink_free mm/kasan/quarantine.c:163 [inline]
qlist_free_all+0x97/0x100 mm/kasan/quarantine.c:179
kasan_quarantine_reduce+0x148/0x160 mm/kasan/quarantine.c:286
__kasan_slab_alloc+0x22/0x80 mm/kasan/common.c:350
kasan_slab_alloc include/linux/kasan.h:253 [inline]
slab_post_alloc_hook mm/slub.c:4953 [inline]
slab_alloc_node mm/slub.c:5263 [inline]
kmem_cache_alloc_noprof+0x37d/0x710 mm/slub.c:5270
getname_flags+0xb8/0x540 fs/namei.c:146
getname include/linux/fs.h:2498 [inline]
getname_maybe_null include/linux/fs.h:2505 [inline]
vfs_fstatat+0x43/0x170 fs/stat.c:370
__do_sys_newfstatat fs/stat.c:542 [inline]
__se_sys_newfstatat fs/stat.c:536 [inline]
__x64_sys_newfstatat+0x116/0x190 fs/stat.c:536
do_syscall_x64 arch/x86/entry/syscall_64.c:63 [inline]
do_syscall_64+0xe8/0xf80 arch/x86/entry/syscall_64.c:94
entry_SYSCALL_64_after_hwframe+0x77/0x7f
Memory state around the buggy address:
ffffc90002417c00: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
ffffc90002417c80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
>ffffc90002417d00: f1 f1 f1 f1 00 00 00 00 02 f3 f3 f3 f3 f3 f3 f3
^
ffffc90002417d80: 00 00 00 00 00 00 00 00 00 00 00 00 f1 f1 f1 f1
ffffc90002417e00: 04 f2 00 00 f2 f2 00 00 f3 f3 f3 f3 00 00 00 00
==================================================================
]
|
| 5/1 |
2026/01/30 14:35 |
action |
codesearch-prepare |
0m
Results: map[Index:codesearch-index]
|
| 6/1 |
2026/01/30 14:35 |
agent |
debugger |
3m
Model:
Instruction:
You are an experienced Linux kernel developer tasked with debugging a kernel crash root cause.
You need to provide a detailed explanation of the root cause for another developer to be
able to write a fix for the bug based on your explanation. Include all relevant details
into the response: function/struct/field/etc names, code snippets, line numbers,
macro/enum values, etc.
Prefer calling several tools at the same time to save round-trips.
Prompt:
The crash is:
==================================================================
BUG: KASAN: stack-out-of-bounds in ft260_smbus_write+0x156/0x2f0 drivers/hid/hid-ft260.c:486
Read of size 42 at addr ffffc90002417d21 by task syz-executor165/6009
CPU: 0 UID: 0 PID: 6009 Comm: syz-executor165 Not tainted syzkaller #1 PREEMPT(full)
Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.17.0-debian-1.17.0-1 04/01/2014
Call Trace:
<TASK>
__dump_stack lib/dump_stack.c:94 [inline]
dump_stack_lvl+0x10e/0x190 lib/dump_stack.c:120
print_address_description mm/kasan/report.c:378 [inline]
print_report+0x17e/0x810 mm/kasan/report.c:482
kasan_report+0x147/0x180 mm/kasan/report.c:595
check_region_inline mm/kasan/generic.c:-1 [inline]
kasan_check_range+0x2b0/0x2c0 mm/kasan/generic.c:200
__asan_memcpy+0x29/0x70 mm/kasan/shadow.c:105
ft260_smbus_write+0x156/0x2f0 drivers/hid/hid-ft260.c:486
ft260_smbus_xfer+0x8bc/0xdd0 drivers/hid/hid-ft260.c:-1
__i2c_smbus_xfer+0x557/0x1e20 drivers/i2c/i2c-core-smbus.c:591
i2c_smbus_xfer+0x1f4/0x310 drivers/i2c/i2c-core-smbus.c:546
i2cdev_ioctl_smbus+0x3db/0x750 drivers/i2c/i2c-dev.c:389
i2cdev_ioctl+0x5d3/0x820 drivers/i2c/i2c-dev.c:478
vfs_ioctl fs/ioctl.c:51 [inline]
__do_sys_ioctl fs/ioctl.c:597 [inline]
__se_sys_ioctl+0xfc/0x170 fs/ioctl.c:583
do_syscall_x64 arch/x86/entry/syscall_64.c:63 [inline]
do_syscall_64+0xe8/0xf80 arch/x86/entry/syscall_64.c:94
entry_SYSCALL_64_after_hwframe+0x77/0x7f
RIP: 0033:0x7f1925b454f9
Code: c0 79 93 eb d5 48 8d 7c 1d 00 eb 99 0f 1f 44 00 00 48 89 f8 48 89 f7 48 89 d6 48 89 ca 4d 89 c2 4d 89 c8 4c 8b 4c 24 08 0f 05 <48> 3d 01 f0 ff ff 73 01 c3 48 c7 c1 d8 ff ff ff f7 d8 64 89 01 48
RSP: 002b:00007ffee7bc0ab8 EFLAGS: 00000246 ORIG_RAX: 0000000000000010
RAX: ffffffffffffffda RBX: 00007ffee7bc0c48 RCX: 00007f1925b454f9
RDX: 0000200000000200 RSI: 0000000000000720 RDI: 0000000000000004
RBP: 0000000000000001 R08: 0000000000000000 R09: 0000000000000009
R10: 0000000000000000 R11: 0000000000000246 R12: 00007ffee7bc0ae0
R13: 00007ffee7bc0ad0 R14: 00007f1925bd8c40 R15: 0000000000000000
</TASK>
The buggy address belongs to stack of task syz-executor165/6009
and is located at offset 33 in frame:
i2cdev_ioctl_smbus+0x0/0x750 drivers/i2c/i2c-dev.c:317
This frame has 1 object:
[32, 66) 'temp'
The buggy address belongs to a 8-page vmalloc region starting at 0xffffc90002410000 allocated at copy_process+0x4e7/0x3950 kernel/fork.c:2052
The buggy address belongs to the physical page:
page: refcount:1 mapcount:0 mapping:0000000000000000 index:0x0 pfn:0x213ff
flags: 0xfff00000000000(node=0|zone=1|lastcpupid=0x7ff)
raw: 00fff00000000000 0000000000000000 ffffea000084ffc8 0000000000000000
raw: 0000000000000000 0000000000000000 00000001ffffffff 0000000000000000
page dumped because: kasan: bad access detected
page_owner tracks the page as allocated
page last allocated via order 0, migratetype Unmovable, gfp_mask 0x29c2(GFP_NOWAIT|__GFP_HIGHMEM|__GFP_IO|__GFP_FS|__GFP_ZERO), pid 6007, tgid 6007 (syz-executor165), ts 90494270799, free_ts 87604917166
set_page_owner include/linux/page_owner.h:32 [inline]
post_alloc_hook+0x234/0x290 mm/page_alloc.c:1884
prep_new_page mm/page_alloc.c:1892 [inline]
get_page_from_freelist+0x24e4/0x2580 mm/page_alloc.c:3945
__alloc_frozen_pages_noprof+0x181/0x370 mm/page_alloc.c:5240
alloc_pages_mpol+0x232/0x4a0 mm/mempolicy.c:2486
alloc_frozen_pages_noprof mm/mempolicy.c:2557 [inline]
alloc_pages_noprof+0xa9/0x190 mm/mempolicy.c:2577
vm_area_alloc_pages mm/vmalloc.c:3649 [inline]
__vmalloc_area_node mm/vmalloc.c:3863 [inline]
__vmalloc_node_range_noprof+0x919/0x16a0 mm/vmalloc.c:4051
__vmalloc_node_noprof+0xc2/0x110 mm/vmalloc.c:4111
alloc_thread_stack_node kernel/fork.c:354 [inline]
dup_task_struct+0x250/0x9b0 kernel/fork.c:923
copy_process+0x4e7/0x3950 kernel/fork.c:2052
kernel_clone+0x21e/0x820 kernel/fork.c:2651
__do_sys_clone kernel/fork.c:2792 [inline]
__se_sys_clone kernel/fork.c:2776 [inline]
__x64_sys_clone+0x190/0x1f0 kernel/fork.c:2776
do_syscall_x64 arch/x86/entry/syscall_64.c:63 [inline]
do_syscall_64+0xe8/0xf80 arch/x86/entry/syscall_64.c:94
entry_SYSCALL_64_after_hwframe+0x77/0x7f
page last free pid 5954 tgid 5954 stack trace:
reset_page_owner include/linux/page_owner.h:25 [inline]
free_pages_prepare mm/page_alloc.c:1433 [inline]
__free_frozen_pages+0xbc4/0xd40 mm/page_alloc.c:2973
discard_slab mm/slub.c:3346 [inline]
__put_partials+0x146/0x170 mm/slub.c:3886
__slab_free+0x294/0x320 mm/slub.c:5956
qlink_free mm/kasan/quarantine.c:163 [inline]
qlist_free_all+0x97/0x100 mm/kasan/quarantine.c:179
kasan_quarantine_reduce+0x148/0x160 mm/kasan/quarantine.c:286
__kasan_slab_alloc+0x22/0x80 mm/kasan/common.c:350
kasan_slab_alloc include/linux/kasan.h:253 [inline]
slab_post_alloc_hook mm/slub.c:4953 [inline]
slab_alloc_node mm/slub.c:5263 [inline]
kmem_cache_alloc_noprof+0x37d/0x710 mm/slub.c:5270
getname_flags+0xb8/0x540 fs/namei.c:146
getname include/linux/fs.h:2498 [inline]
getname_maybe_null include/linux/fs.h:2505 [inline]
vfs_fstatat+0x43/0x170 fs/stat.c:370
__do_sys_newfstatat fs/stat.c:542 [inline]
__se_sys_newfstatat fs/stat.c:536 [inline]
__x64_sys_newfstatat+0x116/0x190 fs/stat.c:536
do_syscall_x64 arch/x86/entry/syscall_64.c:63 [inline]
do_syscall_64+0xe8/0xf80 arch/x86/entry/syscall_64.c:94
entry_SYSCALL_64_after_hwframe+0x77/0x7f
Memory state around the buggy address:
ffffc90002417c00: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
ffffc90002417c80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
>ffffc90002417d00: f1 f1 f1 f1 00 00 00 00 02 f3 f3 f3 f3 f3 f3 f3
^
ffffc90002417d80: 00 00 00 00 00 00 00 00 00 00 00 00 f1 f1 f1 f1
ffffc90002417e00: 04 f2 00 00 f2 f2 00 00 f3 f3 f3 f3 00 00 00 00
==================================================================
Reply: The root cause of the KASAN stack-out-of-bounds crash is a missing validation of the input data length in the `ft260_smbus_xfer` function in `drivers/hid/hid-ft260.c`.
When handling `I2C_SMBUS_BLOCK_DATA` or `I2C_SMBUS_I2C_BLOCK_DATA` transactions, the function uses the length byte provided by the user (stored in `data->block[0]`) to determine how much data to copy. However, it fails to ensure that this length does not exceed the size of the `data->block` array (or `I2C_SMBUS_BLOCK_MAX`), which is backed by the `union i2c_smbus_data` on the stack of the calling function `i2cdev_ioctl_smbus`.
The `union i2c_smbus_data` has a size of 34 bytes (`I2C_SMBUS_BLOCK_MAX` is 32, plus 2 bytes). The `block` field is defined as `__u8 block[I2C_SMBUS_BLOCK_MAX + 2];`.
In the crash scenario (`I2C_SMBUS_I2C_BLOCK_DATA`), the code does:
```c
736: ret = ft260_smbus_write(dev, addr, cmd, data->block + 1,
737: data->block[0],
738: FT260_FLAG_START_STOP);
```
Here, `data->block[0]` (controlled by the user) is passed as `data_len`. If the user provides a length (e.g., 42) that is larger than the remaining size of the buffer (which is 33 bytes, starting from `data->block[1]`), `ft260_smbus_write` will read out of bounds.
In `ft260_smbus_write`:
```c
486: memcpy(&rep->data[1], data, data_len);
```
It performs a `memcpy` from the stack buffer `data` (which is `&temp.block[1]`) with size 42. Since `temp.block` is only 34 bytes total, reading 42 bytes from offset 1 accesses invalid stack memory, triggering KASAN.
**Fix:**
Validate that `data->block[0]` is less than or equal to `I2C_SMBUS_BLOCK_MAX` (32) in `ft260_smbus_xfer` before processing block transfers.
**Code Snippet to fix in `drivers/hid/hid-ft260.c`:**
```c
static int ft260_smbus_xfer(struct i2c_adapter *adapter, u16 addr, u16 flags,
char read_write, u8 cmd, int size,
union i2c_smbus_data *data)
{
// ...
switch (size) {
// ...
case I2C_SMBUS_BLOCK_DATA:
if (read_write == I2C_SMBUS_READ) {
// ...
} else {
if (data->block[0] > I2C_SMBUS_BLOCK_MAX)
return -EINVAL; // Add this check
ret = ft260_smbus_write(dev, addr, cmd, data->block,
data->block[0] + 1,
FT260_FLAG_START_STOP);
}
break;
case I2C_SMBUS_I2C_BLOCK_DATA:
if (read_write == I2C_SMBUS_READ) {
// ...
} else {
if (data->block[0] > I2C_SMBUS_BLOCK_MAX)
return -EINVAL; // Add this check
ret = ft260_smbus_write(dev, addr, cmd, data->block + 1,
data->block[0],
FT260_FLAG_START_STOP);
}
break;
// ...
}
// ...
}
```
|
| 7/2 |
2026/01/30 14:35 |
llm |
debugger |
0m
Model:
Tokens:
input: 5750
output: 101
thoughts: 466
Thoughts: **Analyzing a KASAN Crash in ft260_smbus_write**
Okay, here's what I'm dealing with. I have a KASAN stack-out-of-bounds crash report, specifically in `ft260_smbus_write`, and it's pinpointed at `drivers/hid/hid-ft260.c:486`. The crash report tells me a "Read of size 42 at addr ffffc90002417d21" is the culprit. This address lives on the stack of `syz-executor165/6009`.
The frame where this object is allocated is `i2cdev_ioctl_smbus+0x0/0x750`, specifically in `drivers/i2c/i2c-dev.c:317`. The problematic object is called `temp`, and its size is 34 bytes (calculated as `[32, 66)`, so 66-32 = 34). The buggy access is located at offset 33 within this frame, which means it starts at the base of the frame, `frame_base`, plus 33 bytes. Since the object `temp` starts at `frame_base + 32`, this means the access is happening at `temp + 1`.
The read size is 42 bytes. So, we're trying to read from `temp + 1` all the way to `temp + 1 + 42 = temp + 43`. Now, since `temp` is only 34 bytes in size, this is clearly a stack buffer overflowβspecifically, a read beyond the allocated memory. `34 < 43`, so it does read past the allocated `temp`.
Now, let's trace the execution path. The crash happens in `ft260_smbus_write`, which is called through the following chain: `i2cdev_ioctl_smbus` -> `i2c_smbus_xfer` -> `__i2c_smbus_xfer` -> `ft260_smbus_xfer` -> `ft260_smbus_write`.
My next step is to carefully examine `ft260_smbus_write` in `drivers/hid/hid-ft260.c` to see how it's trying to access the stack and compare that to `i2cdev_ioctl_smbus` in `drivers/i2c/i2c-dev.c` to understand where `temp` is populated and how the `i2c_smbus_xfer` is passing the data. Let's start by getting the code for `ft260_smbus_write`.
|
| 8/2 |
2026/01/30 14:35 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:drivers/hid/hid-ft260.c IncludeLines:true Name:ft260_smbus_write]
Results: map[SourceCode: 465: static int ft260_smbus_write(struct ft260_device *dev, u8 addr, u8 cmd,
466: u8 *data, u8 data_len, u8 flag)
467: {
468: int ret = 0;
469: int len = 4;
470:
471: struct ft260_i2c_write_request_report *rep =
472: (struct ft260_i2c_write_request_report *)dev->write_buf;
473:
474: if (data_len >= sizeof(rep->data))
475: return -EINVAL;
476:
477: rep->address = addr;
478: rep->data[0] = cmd;
479: rep->length = data_len + 1;
480: rep->flag = flag;
481: len += rep->length;
482:
483: rep->report = FT260_I2C_DATA_REPORT_ID(len);
484:
485: if (data_len > 0)
486: memcpy(&rep->data[1], data, data_len);
487:
488: ft260_dbg("rep %#02x addr %#02x cmd %#02x datlen %d replen %d\n",
489: rep->report, addr, cmd, rep->length, len);
490:
491: ret = ft260_hid_output_report_check_status(dev, (u8 *)rep, len);
492:
493: return ret;
494: }
SourceFile:drivers/hid/hid-ft260.c]
|
| 9/2 |
2026/01/30 14:35 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:drivers/i2c/i2c-dev.c IncludeLines:true Name:i2cdev_ioctl_smbus]
Results: map[SourceCode: 319: static noinline int i2cdev_ioctl_smbus(struct i2c_client *client,
320: u8 read_write, u8 command, u32 size,
321: union i2c_smbus_data __user *data)
322: {
323: union i2c_smbus_data temp = {};
324: int datasize, res;
325:
326: if ((size != I2C_SMBUS_BYTE) &&
327: (size != I2C_SMBUS_QUICK) &&
328: (size != I2C_SMBUS_BYTE_DATA) &&
329: (size != I2C_SMBUS_WORD_DATA) &&
330: (size != I2C_SMBUS_PROC_CALL) &&
331: (size != I2C_SMBUS_BLOCK_DATA) &&
332: (size != I2C_SMBUS_I2C_BLOCK_BROKEN) &&
333: (size != I2C_SMBUS_I2C_BLOCK_DATA) &&
334: (size != I2C_SMBUS_BLOCK_PROC_CALL)) {
335: dev_dbg(&client->adapter->dev,
336: "size out of range (%x) in ioctl I2C_SMBUS.\n",
337: size);
338: return -EINVAL;
339: }
340: /* Note that I2C_SMBUS_READ and I2C_SMBUS_WRITE are 0 and 1,
341: so the check is valid if size==I2C_SMBUS_QUICK too. */
342: if ((read_write != I2C_SMBUS_READ) &&
343: (read_write != I2C_SMBUS_WRITE)) {
344: dev_dbg(&client->adapter->dev,
345: "read_write out of range (%x) in ioctl I2C_SMBUS.\n",
346: read_write);
347: return -EINVAL;
348: }
349:
350: /* Note that command values are always valid! */
351:
352: if ((size == I2C_SMBUS_QUICK) ||
353: ((size == I2C_SMBUS_BYTE) &&
354: (read_write == I2C_SMBUS_WRITE)))
355: /* These are special: we do not use data */
356: return i2c_smbus_xfer(client->adapter, client->addr,
357: client->flags, read_write,
358: command, size, NULL);
359:
360: if (data == NULL) {
361: dev_dbg(&client->adapter->dev,
362: "data is NULL pointer in ioctl I2C_SMBUS.\n");
363: return -EINVAL;
364: }
365:
366: if ((size == I2C_SMBUS_BYTE_DATA) ||
367: (size == I2C_SMBUS_BYTE))
368: datasize = sizeof(data->byte);
369: else if ((size == I2C_SMBUS_WORD_DATA) ||
370: (size == I2C_SMBUS_PROC_CALL))
371: datasize = sizeof(data->word);
372: else /* size == smbus block, i2c block, or block proc. call */
373: datasize = sizeof(data->block);
374:
375: if ((size == I2C_SMBUS_PROC_CALL) ||
376: (size == I2C_SMBUS_BLOCK_PROC_CALL) ||
377: (size == I2C_SMBUS_I2C_BLOCK_DATA) ||
378: (read_write == I2C_SMBUS_WRITE)) {
379: if (copy_from_user(&temp, data, datasize))
380: return -EFAULT;
381: }
382: if (size == I2C_SMBUS_I2C_BLOCK_BROKEN) {
383: /* Convert old I2C block commands to the new
384: convention. This preserves binary compatibility. */
385: size = I2C_SMBUS_I2C_BLOCK_DATA;
386: if (read_write == I2C_SMBUS_READ)
387: temp.block[0] = I2C_SMBUS_BLOCK_MAX;
388: }
389: res = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
390: read_write, command, size, &temp);
391: if (!res && ((size == I2C_SMBUS_PROC_CALL) ||
392: (size == I2C_SMBUS_BLOCK_PROC_CALL) ||
393: (read_write == I2C_SMBUS_READ))) {
394: if (copy_to_user(data, &temp, datasize))
395: return -EFAULT;
396: }
397: return res;
398: }
SourceFile:drivers/i2c/i2c-dev.c]
|
| 10/2 |
2026/01/30 14:35 |
llm |
debugger |
0m
Model:
Tokens:
input: 8972
output: 52
thoughts: 258
Thoughts: **Debugging a `memcpy` Crash in ft260_smbus_write**
Okay, so I'm digging into this crash report from the FT260 driver, specifically within the `ft260_smbus_write` function. The key line, line 486, is a `memcpy` call: `memcpy(&rep->data[1], data, data_len);`. The KASAN report helpfully tells me it's a read issue β a read of size 42 at address `ffffc90002417d21`. That's where things are going sideways.
Clearly, this `memcpy` is pulling data from the `data` buffer. `data` is a `u8 *`, and `data_len` is a `u8`, so the size is the problem. It seems the source buffer is the culprit. Where is `data` coming from? Ah, it originates from the stack of `i2cdev_ioctl_smbus`. This function declares a `union i2c_smbus_data temp = {};`. I need to examine that `union` definition now. My suspicion is that `data_len` is larger than the space allocated for the `temp` buffer, hence the out-of-bounds read during the `memcpy`. Time to dig deeper into the structure definition to confirm my suspicions.
|
| 11/2 |
2026/01/30 14:36 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:include/uapi/linux/i2c-smbus.h IncludeLines:false Name:i2c_smbus_data]
Results: map[SourceCode:union i2c_smbus_data {
__u8 byte;
__u16 word;
__u8 block[I2C_SMBUS_BLOCK_MAX + 2]; /* block[0] is used for length */
/* and one more for user-space compatibility */
};
SourceFile:include/uapi/linux/i2c.h]
|
| 12/2 |
2026/01/30 14:36 |
llm |
debugger |
0m
Model:
Tokens:
input: 9690
output: 54
thoughts: 28
Thoughts: Okay, here's my interpretation of those thoughts, tailored for an expert audience, presented in the first person:
**Quick Check: I2C Block Size Maximum**
Right, before I proceed, I need to confirm the maximum block size for my I2C SMBus operations. I can't just assume anything here. The typical maximum is 32 bytes, which is `I2C_SMBUS_BLOCK_MAX`. Need to double check that I haven't missed something or got the wrong variable set in this code. It's usually 32 but I want to make sure I don't introduce a subtle bug.
|
| 13/2 |
2026/01/30 14:36 |
tool |
codesearch-definition-source |
0m
Error:
Args: map[ContextFile:include/uapi/linux/i2c-smbus.h IncludeLines:false Name:I2C_SMBUS_BLOCK_MAX]
Results: map[SourceCode: SourceFile:]
|
| 14/2 |
2026/01/30 14:36 |
llm |
debugger |
0m
Model:
Tokens:
input: 9928
output: 27
thoughts: 35
Thoughts: **Finding the I2C Block Transfer Limit**
Okay, so I need to track down the maximum block size permitted for I2C SMBus transfers. My first instinct, as always, is to dive right into the kernel headers. Given that I'm working with the user-space I2C interface, the logical place to start looking is `include/uapi/linux/i2c.h` or something similarly named. I'm expecting to find a preprocessor macro defined there, probably something called `I2C_SMBUS_BLOCK_MAX` or something very close. It's almost certain that the definition will be within the Linux kernel's user-space API headers. Gotta check there first. Let's get to it!
|
| 15/2 |
2026/01/30 14:36 |
tool |
read-file |
0m
Args: map[File:include/uapi/linux/i2c.h]
Results: map[Contents:/* SPDX-License-Identifier: GPL-2.0+ WITH Linux-syscall-note */
/*
* i2c.h - definitions for the I2C bus interface
*
* Copyright (C) 1995-2000 Simon G. Vogl
* With some changes from KyΓΆsti MΓ€lkki <kmalkki@cc.hut.fi> and
* Frodo Looijaard <frodol@dds.nl>
*/
#ifndef _UAPI_LINUX_I2C_H
#define _UAPI_LINUX_I2C_H
#include <linux/types.h>
/**
* struct i2c_msg - an I2C transaction segment beginning with START
*
* @addr: Slave address, either 7 or 10 bits. When this is a 10 bit address,
* %I2C_M_TEN must be set in @flags and the adapter must support
* %I2C_FUNC_10BIT_ADDR.
*
* @flags:
* Supported by all adapters:
* %I2C_M_RD: read data (from slave to master). Guaranteed to be 0x0001! If
* not set, the transaction is interpreted as write.
*
* Optional:
* %I2C_M_DMA_SAFE: the buffer of this message is DMA safe. Makes only sense
* in kernelspace, because userspace buffers are copied anyway
*
* Only if I2C_FUNC_10BIT_ADDR is set:
* %I2C_M_TEN: this is a 10 bit chip address
*
* Only if I2C_FUNC_SMBUS_READ_BLOCK_DATA is set:
* %I2C_M_RECV_LEN: message length will be first received byte
*
* Only if I2C_FUNC_NOSTART is set:
* %I2C_M_NOSTART: skip repeated start sequence
*
* Only if I2C_FUNC_PROTOCOL_MANGLING is set:
* %I2C_M_NO_RD_ACK: in a read message, master ACK/NACK bit is skipped
* %I2C_M_IGNORE_NAK: treat NACK from client as ACK
* %I2C_M_REV_DIR_ADDR: toggles the Rd/Wr bit
* %I2C_M_STOP: force a STOP condition after the message
*
* @len: Number of data bytes in @buf being read from or written to the I2C
* slave address. For read transactions where %I2C_M_RECV_LEN is set, the
* caller guarantees that this buffer can hold up to %I2C_SMBUS_BLOCK_MAX
* bytes in addition to the initial length byte sent by the slave (plus,
* if used, the SMBus PEC); and this value will be incremented by the number
* of block data bytes received.
*
* @buf: The buffer into which data is read, or from which it's written.
*
* An i2c_msg is the low level representation of one segment of an I2C
* transaction. It is visible to drivers in the @i2c_transfer() procedure,
* to userspace from i2c-dev, and to I2C adapter drivers through the
* @i2c_adapter.@master_xfer() method.
*
* Except when I2C "protocol mangling" is used, all I2C adapters implement
* the standard rules for I2C transactions. Each transaction begins with a
* START. That is followed by the slave address, and a bit encoding read
* versus write. Then follow all the data bytes, possibly including a byte
* with SMBus PEC. The transfer terminates with a NAK, or when all those
* bytes have been transferred and ACKed. If this is the last message in a
* group, it is followed by a STOP. Otherwise it is followed by the next
* @i2c_msg transaction segment, beginning with a (repeated) START.
*
* Alternatively, when the adapter supports %I2C_FUNC_PROTOCOL_MANGLING then
* passing certain @flags may have changed those standard protocol behaviors.
* Those flags are only for use with broken/nonconforming slaves, and with
* adapters which are known to support the specific mangling options they need.
*/
struct i2c_msg {
__u16 addr;
__u16 flags;
#define I2C_M_RD 0x0001 /* guaranteed to be 0x0001! */
#define I2C_M_TEN 0x0010 /* use only if I2C_FUNC_10BIT_ADDR */
#define I2C_M_DMA_SAFE 0x0200 /* use only in kernel space */
#define I2C_M_RECV_LEN 0x0400 /* use only if I2C_FUNC_SMBUS_READ_BLOCK_DATA */
#define I2C_M_NO_RD_ACK 0x0800 /* use only if I2C_FUNC_PROTOCOL_MANGLING */
#define I2C_M_IGNORE_NAK 0x1000 /* use only if I2C_FUNC_PROTOCOL_MANGLING */
#define I2C_M_REV_DIR_ADDR 0x2000 /* use only if I2C_FUNC_PROTOCOL_MANGLING */
#define I2C_M_NOSTART 0x4000 /* use only if I2C_FUNC_NOSTART */
#define I2C_M_STOP 0x8000 /* use only if I2C_FUNC_PROTOCOL_MANGLING */
__u16 len;
__u8 *buf;
};
/* To determine what functionality is present */
#define I2C_FUNC_I2C 0x00000001
#define I2C_FUNC_10BIT_ADDR 0x00000002 /* required for I2C_M_TEN */
#define I2C_FUNC_PROTOCOL_MANGLING 0x00000004 /* required for I2C_M_IGNORE_NAK etc. */
#define I2C_FUNC_SMBUS_PEC 0x00000008
#define I2C_FUNC_NOSTART 0x00000010 /* required for I2C_M_NOSTART */
#define I2C_FUNC_SLAVE 0x00000020
#define I2C_FUNC_SMBUS_BLOCK_PROC_CALL 0x00008000 /* SMBus 2.0 or later */
#define I2C_FUNC_SMBUS_QUICK 0x00010000
#define I2C_FUNC_SMBUS_READ_BYTE 0x00020000
#define I2C_FUNC_SMBUS_WRITE_BYTE 0x00040000
#define I2C_FUNC_SMBUS_READ_BYTE_DATA 0x00080000
#define I2C_FUNC_SMBUS_WRITE_BYTE_DATA 0x00100000
#define I2C_FUNC_SMBUS_READ_WORD_DATA 0x00200000
#define I2C_FUNC_SMBUS_WRITE_WORD_DATA 0x00400000
#define I2C_FUNC_SMBUS_PROC_CALL 0x00800000
#define I2C_FUNC_SMBUS_READ_BLOCK_DATA 0x01000000 /* required for I2C_M_RECV_LEN */
#define I2C_FUNC_SMBUS_WRITE_BLOCK_DATA 0x02000000
#define I2C_FUNC_SMBUS_READ_I2C_BLOCK 0x04000000 /* I2C-like block xfer */
#define I2C_FUNC_SMBUS_WRITE_I2C_BLOCK 0x08000000 /* w/ 1-byte reg. addr. */
#define I2C_FUNC_SMBUS_HOST_NOTIFY 0x10000000 /* SMBus 2.0 or later */
#define I2C_FUNC_SMBUS_BYTE (I2C_FUNC_SMBUS_READ_BYTE | \
I2C_FUNC_SMBUS_WRITE_BYTE)
#define I2C_FUNC_SMBUS_BYTE_DATA (I2C_FUNC_SMBUS_READ_BYTE_DATA | \
I2C_FUNC_SMBUS_WRITE_BYTE_DATA)
#define I2C_FUNC_SMBUS_WORD_DATA (I2C_FUNC_SMBUS_READ_WORD_DATA | \
I2C_FUNC_SMBUS_WRITE_WORD_DATA)
#define I2C_FUNC_SMBUS_BLOCK_DATA (I2C_FUNC_SMBUS_READ_BLOCK_DATA | \
I2C_FUNC_SMBUS_WRITE_BLOCK_DATA)
#define I2C_FUNC_SMBUS_I2C_BLOCK (I2C_FUNC_SMBUS_READ_I2C_BLOCK | \
I2C_FUNC_SMBUS_WRITE_I2C_BLOCK)
#define I2C_FUNC_SMBUS_EMUL (I2C_FUNC_SMBUS_QUICK | \
I2C_FUNC_SMBUS_BYTE | \
I2C_FUNC_SMBUS_BYTE_DATA | \
I2C_FUNC_SMBUS_WORD_DATA | \
I2C_FUNC_SMBUS_PROC_CALL | \
I2C_FUNC_SMBUS_WRITE_BLOCK_DATA | \
I2C_FUNC_SMBUS_I2C_BLOCK | \
I2C_FUNC_SMBUS_PEC)
/* if I2C_M_RECV_LEN is also supported */
#define I2C_FUNC_SMBUS_EMUL_ALL (I2C_FUNC_SMBUS_EMUL | \
I2C_FUNC_SMBUS_READ_BLOCK_DATA | \
I2C_FUNC_SMBUS_BLOCK_PROC_CALL)
/*
* Data for SMBus Messages
*/
#define I2C_SMBUS_BLOCK_MAX 32 /* As specified in SMBus standard */
union i2c_smbus_data {
__u8 byte;
__u16 word;
__u8 block[I2C_SMBUS_BLOCK_MAX + 2]; /* block[0] is used for length */
/* and one more for user-space compatibility */
};
/* i2c_smbus_xfer read or write markers */
#define I2C_SMBUS_READ 1
#define I2C_SMBUS_WRITE 0
/* SMBus transaction types (size parameter in the above functions)
Note: these no longer correspond to the (arbitrary) PIIX4 internal codes! */
#define I2C_SMBUS_QUICK 0
#define I2C_SMBUS_BYTE 1
#define I2C_SMBUS_BYTE_DATA 2
#define I2C_SMBUS_WORD_DATA 3
#define I2C_SMBUS_PROC_CALL 4
#define I2C_SMBUS_BLOCK_DATA 5
#define I2C_SMBUS_I2C_BLOCK_BROKEN 6
#define I2C_SMBUS_BLOCK_PROC_CALL 7 /* SMBus 2.0 */
#define I2C_SMBUS_I2C_BLOCK_DATA 8
#endif /* _UAPI_LINUX_I2C_H */
]
|
| 16/2 |
2026/01/30 14:36 |
llm |
debugger |
0m
Model:
Tokens:
input: 13006
output: 51
thoughts: 258
Thoughts: **Analyzing I2C SMBus Data Transfer**
Okay, so I'm looking at this I2C SMBus interaction, and a few things are immediately apparent. The maximum block size for SMBus is defined as `I2C_SMBUS_BLOCK_MAX`, which is 32 bytes. Now, the `block` variable, which is declared as `__u8 block[32 + 2]`, is 34 bytes in size. This makes sense because it accommodates the data and any necessary overhead, like potentially a length byte or checksum.
Then there is the `union i2c_smbus_data`, and its size also comes out to 34 bytes.
Specifically, within the `i2cdev_ioctl_smbus` function, I see this:
```c
union i2c_smbus_data temp = {};
```
`temp` is declared on the stack, and KASAN (Kernel Address Sanitizer) flags this section `[32, 66)`, also implying a size of 34 bytes.
The critical part of the data transfer is the function call chain: `i2cdev_ioctl_smbus` calls `i2c_smbus_xfer`, which in turn calls `__i2c_smbus_xfer`, and finally, that calls `adapter->algo->smbus_xfer`. In this instance, based on the stack trace that I have, it's pretty clear that `ft260_smbus_xfer` is the specific function handling the transfer because it includes `ft260_smbus_write`.
Now, I'm going to dig deeper into the `ft260_smbus_xfer` function to understand how the driver is handling this transfer and to ensure there is no memory corruption going on. I will look for potential buffer overflows or other issues.
|
| 17/2 |
2026/01/30 14:36 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:drivers/hid/hid-ft260.c IncludeLines:true Name:ft260_smbus_xfer]
Results: map[SourceCode: 652: static int ft260_smbus_xfer(struct i2c_adapter *adapter, u16 addr, u16 flags,
653: char read_write, u8 cmd, int size,
654: union i2c_smbus_data *data)
655: {
656: int ret;
657: struct ft260_device *dev = i2c_get_adapdata(adapter);
658: struct hid_device *hdev = dev->hdev;
659:
660: ft260_dbg("smbus size %d\n", size);
661:
662: mutex_lock(&dev->lock);
663:
664: ret = hid_hw_power(hdev, PM_HINT_FULLON);
665: if (ret < 0) {
666: hid_err(hdev, "power management error: %d\n", ret);
667: mutex_unlock(&dev->lock);
668: return ret;
669: }
670:
671: switch (size) {
672: case I2C_SMBUS_BYTE:
673: if (read_write == I2C_SMBUS_READ)
674: ret = ft260_i2c_read(dev, addr, &data->byte, 1,
675: FT260_FLAG_START_STOP);
676: else
677: ret = ft260_smbus_write(dev, addr, cmd, NULL, 0,
678: FT260_FLAG_START_STOP);
679: break;
680: case I2C_SMBUS_BYTE_DATA:
681: if (read_write == I2C_SMBUS_READ) {
682: ret = ft260_smbus_write(dev, addr, cmd, NULL, 0,
683: FT260_FLAG_START);
684: if (ret)
685: goto smbus_exit;
686:
687: ret = ft260_i2c_read(dev, addr, &data->byte, 1,
688: FT260_FLAG_START_STOP_REPEATED);
689: } else {
690: ret = ft260_smbus_write(dev, addr, cmd, &data->byte, 1,
691: FT260_FLAG_START_STOP);
692: }
693: break;
694: case I2C_SMBUS_WORD_DATA:
695: if (read_write == I2C_SMBUS_READ) {
696: ret = ft260_smbus_write(dev, addr, cmd, NULL, 0,
697: FT260_FLAG_START);
698: if (ret)
699: goto smbus_exit;
700:
701: ret = ft260_i2c_read(dev, addr, (u8 *)&data->word, 2,
702: FT260_FLAG_START_STOP_REPEATED);
703: } else {
704: ret = ft260_smbus_write(dev, addr, cmd,
705: (u8 *)&data->word, 2,
706: FT260_FLAG_START_STOP);
707: }
708: break;
709: case I2C_SMBUS_BLOCK_DATA:
710: if (read_write == I2C_SMBUS_READ) {
711: ret = ft260_smbus_write(dev, addr, cmd, NULL, 0,
712: FT260_FLAG_START);
713: if (ret)
714: goto smbus_exit;
715:
716: ret = ft260_i2c_read(dev, addr, data->block,
717: data->block[0] + 1,
718: FT260_FLAG_START_STOP_REPEATED);
719: } else {
720: ret = ft260_smbus_write(dev, addr, cmd, data->block,
721: data->block[0] + 1,
722: FT260_FLAG_START_STOP);
723: }
724: break;
725: case I2C_SMBUS_I2C_BLOCK_DATA:
726: if (read_write == I2C_SMBUS_READ) {
727: ret = ft260_smbus_write(dev, addr, cmd, NULL, 0,
728: FT260_FLAG_START);
729: if (ret)
730: goto smbus_exit;
731:
732: ret = ft260_i2c_read(dev, addr, data->block + 1,
733: data->block[0],
734: FT260_FLAG_START_STOP_REPEATED);
735: } else {
736: ret = ft260_smbus_write(dev, addr, cmd, data->block + 1,
737: data->block[0],
738: FT260_FLAG_START_STOP);
739: }
740: break;
741: default:
742: hid_err(hdev, "unsupported smbus transaction size %d\n", size);
743: ret = -EOPNOTSUPP;
744: }
745:
746: smbus_exit:
747: hid_hw_power(hdev, PM_HINT_NORMAL);
748: mutex_unlock(&dev->lock);
749: return ret;
750: }
SourceFile:drivers/hid/hid-ft260.c]
|
| 18/2 |
2026/01/30 14:36 |
llm |
debugger |
0m
Model:
Tokens:
input: 15452
output: 54
thoughts: 764
Thoughts: **Analyzing a Potential Out-of-Bounds Read in ft260_smbus_xfer**
Okay, let's break this down. In `ft260_smbus_xfer`, the `data` parameter is a `union i2c_smbus_data *`. This `data` pointer is derived from the `&temp` variable in `i2cdev_ioctl_smbus`. Now, if we're dealing with a write operation (`read_write == I2C_SMBUS_WRITE`) and the transfer size is a block (`size == I2C_SMBUS_BLOCK_DATA`), we call `ft260_smbus_write`. Crucially, the data length (`data_len`) passed to `ft260_smbus_write` is calculated as `data->block[0] + 1`. This `data->block[0]` represents the length byte provided in the block data itself.
The vulnerability stems from how this length byte influences the `memcpy` call within `ft260_smbus_write`. We see `memcpy(&rep->data[1], data, data_len);`. This copies `data_len` bytes from `data` (which points to `data->block`) into `rep->data`. The problem is that `data->block` is only 34 bytes large, defined within the union.
So, if a user crafts the `data->block[0]` value to be large, say 255, then `data_len` becomes 256. `memcpy` would then attempt to read 256 bytes from `data->block`, which is only 34 bytes, causing a classic out-of-bounds read. The fact that KASAN reports a read of size 42 indicates that the block size is fixed. If the read is 42 bytes, then `data->block[0]` would be 41.
Now, the important question is: is `data->block[0]` sanitized before being used? We have to check if there is any bound checks to see if this value is controlled or sanitized.
Let's look at `i2cdev_ioctl_smbus`. Here's the relevant code:
```c
if ((size == I2C_SMBUS_PROC_CALL) ||
(size == I2C_SMBUS_BLOCK_PROC_CALL) ||
(size == I2C_SMBUS_I2C_BLOCK_DATA) ||
(read_write == I2C_SMBUS_WRITE)) {
if (copy_from_user(&temp, data, datasize))
return -EFAULT;
}
```
If we're dealing with `I2C_SMBUS_BLOCK_DATA` (which has a value of 5), `datasize` is `sizeof(data->block)`, which is 34. This means the user-supplied `temp.block` (and, consequently, the dangerous `data->block[0]`) is copied directly from user space without validation. This is exactly where the user controls the `data->block[0]` value and can trigger the overflow.
Therefore, since the data is copied without bounds checking, and the length is used in the `memcpy`, this is an out-of-bounds read vulnerability.
I'll now need to check how `i2c_smbus_xfer` handles block lengths in `drivers/i2c/i2c-core-smbus.c` to see if there's any mitigation in the core SMBus handling.
|
| 19/2 |
2026/01/30 14:36 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:drivers/i2c/i2c-core-smbus.c IncludeLines:true Name:i2c_smbus_xfer]
Results: map[SourceCode: 523: /**
524: * i2c_smbus_xfer - execute SMBus protocol operations
525: * @adapter: Handle to I2C bus
526: * @addr: Address of SMBus slave on that bus
527: * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC)
528: * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE
529: * @command: Byte interpreted by slave, for protocols which use such bytes
530: * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL
531: * @data: Data to be read or written
532: *
533: * This executes an SMBus protocol operation, and returns a negative
534: * errno code else zero on success.
535: */
536: s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr,
537: unsigned short flags, char read_write,
538: u8 command, int protocol, union i2c_smbus_data *data)
539: {
540: s32 res;
541:
542: res = __i2c_lock_bus_helper(adapter);
543: if (res)
544: return res;
545:
546: res = __i2c_smbus_xfer(adapter, addr, flags, read_write,
547: command, protocol, data);
548: i2c_unlock_bus(adapter, I2C_LOCK_SEGMENT);
549:
550: return res;
551: }
SourceFile:drivers/i2c/i2c-core-smbus.c]
|
| 20/2 |
2026/01/30 14:36 |
llm |
debugger |
0m
Model:
Tokens:
input: 17598
output: 55
thoughts: 14
Thoughts: Okay, here's my interpretation of those thoughts, crafted with the understanding that I'm speaking to a fellow expert:
**Navigating the SMBus Landscape**
Alright, so the focus is clearly on SMBus communication. When I see that fragmented thought, specifically `__i2c_smbus_xfer`, my mind immediately jumps to the underlying mechanisms. It's that core function, the linchpin, isn't it? The one that encapsulates the low-level I2C transactions tailored for the SMBus protocol. I'm thinking about its role in handling the specific SMBus protocols, the block transfers, byte reads, writes. It's about data integrity, the error handling, and the timing considerations, those critical factors that are so easily overlooked but so essential. I'm starting to think about the intricacies of the driver that depends on this method. What platform are we discussing? What are the quirks of this specific implementation? I'm already mentally sketching out potential optimizations, the threading concerns, the interrupt handling. It all hinges on that one function, doesn't it?
|
| 21/2 |
2026/01/30 14:36 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:drivers/i2c/i2c-core-smbus.c IncludeLines:true Name:__i2c_smbus_xfer]
Results: map[SourceCode: 554: s32 __i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr,
555: unsigned short flags, char read_write,
556: u8 command, int protocol, union i2c_smbus_data *data)
557: {
558: int (*xfer_func)(struct i2c_adapter *adap, u16 addr,
559: unsigned short flags, char read_write,
560: u8 command, int size, union i2c_smbus_data *data);
561: unsigned long orig_jiffies;
562: int try;
563: s32 res;
564:
565: res = __i2c_check_suspended(adapter);
566: if (res)
567: return res;
568:
569: /* If enabled, the following two tracepoints are conditional on
570: * read_write and protocol.
571: */
572: trace_smbus_write(adapter, addr, flags, read_write,
573: command, protocol, data);
574: trace_smbus_read(adapter, addr, flags, read_write,
575: command, protocol);
576:
577: flags &= I2C_M_TEN | I2C_CLIENT_PEC | I2C_CLIENT_SCCB;
578:
579: xfer_func = adapter->algo->smbus_xfer;
580: if (i2c_in_atomic_xfer_mode()) {
581: if (adapter->algo->smbus_xfer_atomic)
582: xfer_func = adapter->algo->smbus_xfer_atomic;
583: else if (adapter->algo->master_xfer_atomic)
584: xfer_func = NULL; /* fallback to I2C emulation */
585: }
586:
587: if (xfer_func) {
588: /* Retry automatically on arbitration loss */
589: orig_jiffies = jiffies;
590: for (res = 0, try = 0; try <= adapter->retries; try++) {
591: res = xfer_func(adapter, addr, flags, read_write,
592: command, protocol, data);
593: if (res != -EAGAIN)
594: break;
595: if (time_after(jiffies,
596: orig_jiffies + adapter->timeout))
597: break;
598: }
599:
600: if (res != -EOPNOTSUPP || !adapter->algo->master_xfer)
601: goto trace;
602: /*
603: * Fall back to i2c_smbus_xfer_emulated if the adapter doesn't
604: * implement native support for the SMBus operation.
605: */
606: }
607:
608: res = i2c_smbus_xfer_emulated(adapter, addr, flags, read_write,
609: command, protocol, data);
610:
611: trace:
612: /* If enabled, the reply tracepoint is conditional on read_write. */
613: trace_smbus_reply(adapter, addr, flags, read_write,
614: command, protocol, data, res);
615: trace_smbus_result(adapter, addr, flags, read_write,
616: command, protocol, res);
617:
618: return res;
619: }
SourceFile:drivers/i2c/i2c-core-smbus.c]
|
| 22/2 |
2026/01/30 14:36 |
llm |
debugger |
0m
Model:
Tokens:
input: 18993
output: 55
thoughts: 824
Thoughts: **Vulnerability Assessment: I2C SMBus Block Data Handling in ft260 Driver**
Okay, so I'm digging into this I2C SMBus transaction code, specifically looking at the `__i2c_smbus_xfer` function. My initial impression is that it doesn't seem to be sanitizing the `data` pointer properly. It essentially hands off control to `ft260_smbus_xfer`, which in turn calls `ft260_smbus_write`.
The core issue lies within `ft260_smbus_xfer` in `drivers/hid/hid-ft260.c`. When the `protocol` (or size, as I'm thinking of it) is `I2C_SMBUS_BLOCK_DATA`, it uses `data->block`. Crucially, it uses `data->block[0]` to determine the data length, but there's *no* validation to ensure this length is within acceptable bounds, like `I2C_SMBUS_BLOCK_MAX`.
This is a red flag. If an attacker can control `data->block[0]` and provide a value larger than `I2C_SMBUS_BLOCK_MAX` (which is 32), the subsequent call to `ft260_smbus_write` is going to attempt to read beyond the intended buffer size.
Let's look at `ft260_smbus_write` more closely. In this function, the `memcpy` operation is the point of interest. It copies `data_len` bytes from `data` (which is `data->block`) to `&rep->data[1]`. `data_len` is calculated as `data->block[0] + 1`, and `data` is, again, `data->block`. So if `data->block[0]` is, say, 41, `data_len` becomes 42.
The kicker is this: `data->block` is part of a union `i2c_smbus_data`, which I'm pretty sure is limited to 34 bytes. The `memcpy` is therefore reading 42 bytes from `data->block`. This means a heap buffer overflow *is* present.
Now, let's consider the memory layout. The `union i2c_smbus_data`, named `temp`, is located on the stack within `i2cdev_ioctl_smbus`. It's followed by `int datasize, res;`. Let me think... assuming the typical x86_64 alignment, the `temp` union (34 bytes) takes up the first chunk. `datasize` and `res` are probably 4-byte integers each.
The crash report mentions an address offset of 33 within the stack frame. It says, "Read of size 42 at addr ffffc90002417d21". I am seeing that the offset doesn't match my understanding. `temp` starts at offset 0, and the `data->block` element is at offset 0 in the union, so the `memcpy` is supposed to read from the union, not to a place on the stack after it.
Ah, I've got it. The `memcpy` takes the arguments in the order of destination, source, length: `memcpy(dest, src, n)`. The `memcpy` inside `ft260_smbus_write` copies data from `data` which points to `data->block`, which is at offset 0 in `temp` (union). Therefore the `src` argument of `memcpy` is `temp`.
Okay, I need to check something else. I need to understand what `rep` is.
|
| 23/2 |
2026/01/30 14:37 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:drivers/hid/hid-ft260.c IncludeLines:true Name:ft260_i2c_write_request_report]
Results: map[SourceCode: 208: struct ft260_i2c_write_request_report {
209: u8 report; /* FT260_I2C_REPORT */
210: u8 address; /* 7-bit I2C address */
211: u8 flag; /* I2C transaction condition */
212: u8 length; /* data payload length */
213: u8 data[FT260_WR_DATA_MAX]; /* data payload */
214: } __packed;
SourceFile:drivers/hid/hid-ft260.c]
|
| 24/2 |
2026/01/30 14:37 |
llm |
debugger |
0m
Model:
Tokens:
input: 20865
output: 51
thoughts: 14
Thoughts: Okay, here's my interpretation of that thought process, tailored for an expert audience, and written in the first person:
**Navigating Data Limits**
Hmm, so I'm thinking about the upper bound on writes to the FT260 device. My focus immediately lands on the `FT260_WR_DATA_MAX` constant. That's the key variable here, it dictates the maximum data payload size that can be reliably sent to the FT260 in a single write operation. Knowing this limit is fundamental to avoiding buffer overflows or other data corruption issues. I need to keep this value front of mind. Ensuring I'm not overstepping that boundary is an essential design constraint, not just something to check later; this needs to be integrated into any data-transfer logic, so it doesn't become a bottleneck at runtime. I need to check how to handle this in my current code, and the implications of this constant on any potential protocols.
|
| 25/2 |
2026/01/30 14:37 |
tool |
codesearch-definition-source |
0m
Error:
Args: map[ContextFile:drivers/hid/hid-ft260.c IncludeLines:false Name:FT260_WR_DATA_MAX]
Results: map[SourceCode: SourceFile:]
|
| 26/2 |
2026/01/30 14:37 |
llm |
debugger |
0m
Model:
Tokens:
input: 21152
output: 27
thoughts: 8
Thoughts: Okay, let's do this. Here's a summary of my (hypothetical) thoughts, rendered in the first person, assuming I'm a subject matter expert, and starting with a bolded header as requested.
**Initial Assessment: File X**
Alright, let's dive into this file. My initial instinct is to approach this systematically. I'll need to quickly assess the file's structure, identify the key data points or relevant sections. I'm looking for anything that deviates from established norms within this format, or within my understanding of the problem space. I will mentally create a roadmap of what needs my immediate attention.
|
| 27/2 |
2026/01/30 14:37 |
tool |
read-file |
0m
Args: map[File:drivers/hid/hid-ft260.c]
Results: map[Contents:// SPDX-License-Identifier: GPL-2.0-only
/*
* hid-ft260.c - FTDI FT260 USB HID to I2C host bridge
*
* Copyright (c) 2021, Michael Zaidman <michaelz@xsightlabs.com>
*
* Data Sheet:
* https://www.ftdichip.com/Support/Documents/DataSheets/ICs/DS_FT260.pdf
*/
#include "hid-ids.h"
#include <linux/hidraw.h>
#include <linux/i2c.h>
#include <linux/module.h>
#include <linux/usb.h>
#ifdef DEBUG
static int ft260_debug = 1;
#else
static int ft260_debug;
#endif
module_param_named(debug, ft260_debug, int, 0600);
MODULE_PARM_DESC(debug, "Toggle FT260 debugging messages");
#define ft260_dbg(format, arg...) \
do { \
if (ft260_debug) \
pr_info("%s: " format, __func__, ##arg); \
} while (0)
#define FT260_REPORT_MAX_LENGTH (64)
#define FT260_I2C_DATA_REPORT_ID(len) (FT260_I2C_REPORT_MIN + (len - 1) / 4)
#define FT260_WAKEUP_NEEDED_AFTER_MS (4800) /* 5s minus 200ms margin */
/*
* The ft260 input report format defines 62 bytes for the data payload, but
* when requested 62 bytes, the controller returns 60 and 2 in separate input
* reports. To achieve better performance with the multi-report read data
* transfers, we set the maximum read payload length to a multiple of 60.
* With a 100 kHz I2C clock, one 240 bytes read takes about 1/27 second,
* which is excessive; On the other hand, some higher layer drivers like at24
* or optoe limit the i2c reads to 128 bytes. To not block other drivers out
* of I2C for potentially troublesome amounts of time, we select the maximum
* read payload length to be 180 bytes.
*/
#define FT260_RD_DATA_MAX (180)
#define FT260_WR_DATA_MAX (60)
/*
* Device interface configuration.
* The FT260 has 2 interfaces that are controlled by DCNF0 and DCNF1 pins.
* First implementes USB HID to I2C bridge function and
* second - USB HID to UART bridge function.
*/
enum {
FT260_MODE_ALL = 0x00,
FT260_MODE_I2C = 0x01,
FT260_MODE_UART = 0x02,
FT260_MODE_BOTH = 0x03,
};
/* Control pipe */
enum {
FT260_GET_RQST_TYPE = 0xA1,
FT260_GET_REPORT = 0x01,
FT260_SET_RQST_TYPE = 0x21,
FT260_SET_REPORT = 0x09,
FT260_FEATURE = 0x03,
};
/* Report IDs / Feature In */
enum {
FT260_CHIP_VERSION = 0xA0,
FT260_SYSTEM_SETTINGS = 0xA1,
FT260_I2C_STATUS = 0xC0,
FT260_I2C_READ_REQ = 0xC2,
FT260_I2C_REPORT_MIN = 0xD0,
FT260_I2C_REPORT_MAX = 0xDE,
FT260_GPIO = 0xB0,
FT260_UART_INTERRUPT_STATUS = 0xB1,
FT260_UART_STATUS = 0xE0,
FT260_UART_RI_DCD_STATUS = 0xE1,
FT260_UART_REPORT = 0xF0,
};
/* Feature Out */
enum {
FT260_SET_CLOCK = 0x01,
FT260_SET_I2C_MODE = 0x02,
FT260_SET_UART_MODE = 0x03,
FT260_ENABLE_INTERRUPT = 0x05,
FT260_SELECT_GPIO2_FUNC = 0x06,
FT260_ENABLE_UART_DCD_RI = 0x07,
FT260_SELECT_GPIOA_FUNC = 0x08,
FT260_SELECT_GPIOG_FUNC = 0x09,
FT260_SET_INTERRUPT_TRIGGER = 0x0A,
FT260_SET_SUSPEND_OUT_POLAR = 0x0B,
FT260_ENABLE_UART_RI_WAKEUP = 0x0C,
FT260_SET_UART_RI_WAKEUP_CFG = 0x0D,
FT260_SET_I2C_RESET = 0x20,
FT260_SET_I2C_CLOCK_SPEED = 0x22,
FT260_SET_UART_RESET = 0x40,
FT260_SET_UART_CONFIG = 0x41,
FT260_SET_UART_BAUD_RATE = 0x42,
FT260_SET_UART_DATA_BIT = 0x43,
FT260_SET_UART_PARITY = 0x44,
FT260_SET_UART_STOP_BIT = 0x45,
FT260_SET_UART_BREAKING = 0x46,
FT260_SET_UART_XON_XOFF = 0x49,
};
/* Response codes in I2C status report */
enum {
FT260_I2C_STATUS_SUCCESS = 0x00,
FT260_I2C_STATUS_CTRL_BUSY = 0x01,
FT260_I2C_STATUS_ERROR = 0x02,
FT260_I2C_STATUS_ADDR_NO_ACK = 0x04,
FT260_I2C_STATUS_DATA_NO_ACK = 0x08,
FT260_I2C_STATUS_ARBITR_LOST = 0x10,
FT260_I2C_STATUS_CTRL_IDLE = 0x20,
FT260_I2C_STATUS_BUS_BUSY = 0x40,
};
/* I2C Conditions flags */
enum {
FT260_FLAG_NONE = 0x00,
FT260_FLAG_START = 0x02,
FT260_FLAG_START_REPEATED = 0x03,
FT260_FLAG_STOP = 0x04,
FT260_FLAG_START_STOP = 0x06,
FT260_FLAG_START_STOP_REPEATED = 0x07,
};
#define FT260_SET_REQUEST_VALUE(report_id) ((FT260_FEATURE << 8) | report_id)
/* Feature In reports */
struct ft260_get_chip_version_report {
u8 report; /* FT260_CHIP_VERSION */
u8 chip_code[4]; /* FTDI chip identification code */
u8 reserved[8];
} __packed;
struct ft260_get_system_status_report {
u8 report; /* FT260_SYSTEM_SETTINGS */
u8 chip_mode; /* DCNF0 and DCNF1 status, bits 0-1 */
u8 clock_ctl; /* 0 - 12MHz, 1 - 24MHz, 2 - 48MHz */
u8 suspend_status; /* 0 - not suspended, 1 - suspended */
u8 pwren_status; /* 0 - FT260 is not ready, 1 - ready */
u8 i2c_enable; /* 0 - disabled, 1 - enabled */
u8 uart_mode; /* 0 - OFF; 1 - RTS_CTS, 2 - DTR_DSR, */
/* 3 - XON_XOFF, 4 - No flow control */
u8 hid_over_i2c_en; /* 0 - disabled, 1 - enabled */
u8 gpio2_function; /* 0 - GPIO, 1 - SUSPOUT, */
/* 2 - PWREN, 4 - TX_LED */
u8 gpioA_function; /* 0 - GPIO, 3 - TX_ACTIVE, 4 - TX_LED */
u8 gpioG_function; /* 0 - GPIO, 2 - PWREN, */
/* 5 - RX_LED, 6 - BCD_DET */
u8 suspend_out_pol; /* 0 - active-high, 1 - active-low */
u8 enable_wakeup_int; /* 0 - disabled, 1 - enabled */
u8 intr_cond; /* Interrupt trigger conditions */
u8 power_saving_en; /* 0 - disabled, 1 - enabled */
u8 reserved[10];
} __packed;
struct ft260_get_i2c_status_report {
u8 report; /* FT260_I2C_STATUS */
u8 bus_status; /* I2C bus status */
__le16 clock; /* I2C bus clock in range 60-3400 KHz */
u8 reserved;
} __packed;
/* Feature Out reports */
struct ft260_set_system_clock_report {
u8 report; /* FT260_SYSTEM_SETTINGS */
u8 request; /* FT260_SET_CLOCK */
u8 clock_ctl; /* 0 - 12MHz, 1 - 24MHz, 2 - 48MHz */
} __packed;
struct ft260_set_i2c_mode_report {
u8 report; /* FT260_SYSTEM_SETTINGS */
u8 request; /* FT260_SET_I2C_MODE */
u8 i2c_enable; /* 0 - disabled, 1 - enabled */
} __packed;
struct ft260_set_uart_mode_report {
u8 report; /* FT260_SYSTEM_SETTINGS */
u8 request; /* FT260_SET_UART_MODE */
u8 uart_mode; /* 0 - OFF; 1 - RTS_CTS, 2 - DTR_DSR, */
/* 3 - XON_XOFF, 4 - No flow control */
} __packed;
struct ft260_set_i2c_reset_report {
u8 report; /* FT260_SYSTEM_SETTINGS */
u8 request; /* FT260_SET_I2C_RESET */
} __packed;
struct ft260_set_i2c_speed_report {
u8 report; /* FT260_SYSTEM_SETTINGS */
u8 request; /* FT260_SET_I2C_CLOCK_SPEED */
__le16 clock; /* I2C bus clock in range 60-3400 KHz */
} __packed;
/* Data transfer reports */
struct ft260_i2c_write_request_report {
u8 report; /* FT260_I2C_REPORT */
u8 address; /* 7-bit I2C address */
u8 flag; /* I2C transaction condition */
u8 length; /* data payload length */
u8 data[FT260_WR_DATA_MAX]; /* data payload */
} __packed;
struct ft260_i2c_read_request_report {
u8 report; /* FT260_I2C_READ_REQ */
u8 address; /* 7-bit I2C address */
u8 flag; /* I2C transaction condition */
__le16 length; /* data payload length */
} __packed;
struct ft260_i2c_input_report {
u8 report; /* FT260_I2C_REPORT */
u8 length; /* data payload length */
u8 data[2]; /* data payload */
} __packed;
static const struct hid_device_id ft260_devices[] = {
{ HID_USB_DEVICE(USB_VENDOR_ID_FUTURE_TECHNOLOGY,
USB_DEVICE_ID_FT260) },
{ /* END OF LIST */ }
};
MODULE_DEVICE_TABLE(hid, ft260_devices);
struct ft260_device {
struct i2c_adapter adap;
struct hid_device *hdev;
struct completion wait;
struct mutex lock;
u8 write_buf[FT260_REPORT_MAX_LENGTH];
unsigned long need_wakeup_at;
u8 *read_buf;
u16 read_idx;
u16 read_len;
u16 clock;
};
static int ft260_hid_feature_report_get(struct hid_device *hdev,
unsigned char report_id, u8 *data,
size_t len)
{
u8 *buf;
int ret;
buf = kmalloc(len, GFP_KERNEL);
if (!buf)
return -ENOMEM;
ret = hid_hw_raw_request(hdev, report_id, buf, len, HID_FEATURE_REPORT,
HID_REQ_GET_REPORT);
if (likely(ret == len))
memcpy(data, buf, len);
else if (ret >= 0)
ret = -EIO;
kfree(buf);
return ret;
}
static int ft260_hid_feature_report_set(struct hid_device *hdev, u8 *data,
size_t len)
{
u8 *buf;
int ret;
buf = kmemdup(data, len, GFP_KERNEL);
if (!buf)
return -ENOMEM;
buf[0] = FT260_SYSTEM_SETTINGS;
ret = hid_hw_raw_request(hdev, buf[0], buf, len, HID_FEATURE_REPORT,
HID_REQ_SET_REPORT);
kfree(buf);
return ret;
}
static int ft260_i2c_reset(struct hid_device *hdev)
{
struct ft260_set_i2c_reset_report report;
int ret;
report.request = FT260_SET_I2C_RESET;
ret = ft260_hid_feature_report_set(hdev, (u8 *)&report, sizeof(report));
if (ret < 0) {
hid_err(hdev, "failed to reset I2C controller: %d\n", ret);
return ret;
}
ft260_dbg("done\n");
return ret;
}
static int ft260_xfer_status(struct ft260_device *dev, u8 bus_busy)
{
struct hid_device *hdev = dev->hdev;
struct ft260_get_i2c_status_report report;
int ret;
if (time_is_before_jiffies(dev->need_wakeup_at)) {
ret = ft260_hid_feature_report_get(hdev, FT260_I2C_STATUS,
(u8 *)&report, sizeof(report));
if (unlikely(ret < 0)) {
hid_err(hdev, "failed to retrieve status: %d, no wakeup\n",
ret);
} else {
dev->need_wakeup_at = jiffies +
msecs_to_jiffies(FT260_WAKEUP_NEEDED_AFTER_MS);
ft260_dbg("bus_status %#02x, wakeup\n",
report.bus_status);
}
}
ret = ft260_hid_feature_report_get(hdev, FT260_I2C_STATUS,
(u8 *)&report, sizeof(report));
if (unlikely(ret < 0)) {
hid_err(hdev, "failed to retrieve status: %d\n", ret);
return ret;
}
dev->clock = le16_to_cpu(report.clock);
ft260_dbg("bus_status %#02x, clock %u\n", report.bus_status,
dev->clock);
if (report.bus_status & (FT260_I2C_STATUS_CTRL_BUSY | bus_busy))
return -EAGAIN;
/*
* The error condition (bit 1) is a status bit reflecting any
* error conditions. When any of the bits 2, 3, or 4 are raised
* to 1, bit 1 is also set to 1.
*/
if (report.bus_status & FT260_I2C_STATUS_ERROR) {
hid_err(hdev, "i2c bus error: %#02x\n", report.bus_status);
return -EIO;
}
return 0;
}
static int ft260_hid_output_report(struct hid_device *hdev, u8 *data,
size_t len)
{
u8 *buf;
int ret;
buf = kmemdup(data, len, GFP_KERNEL);
if (!buf)
return -ENOMEM;
ret = hid_hw_output_report(hdev, buf, len);
kfree(buf);
return ret;
}
static int ft260_hid_output_report_check_status(struct ft260_device *dev,
u8 *data, int len)
{
u8 bus_busy;
int ret, usec, try = 100;
struct hid_device *hdev = dev->hdev;
struct ft260_i2c_write_request_report *rep =
(struct ft260_i2c_write_request_report *)data;
ret = ft260_hid_output_report(hdev, data, len);
if (ret < 0) {
hid_err(hdev, "%s: failed to start transfer, ret %d\n",
__func__, ret);
ft260_i2c_reset(hdev);
return ret;
}
/* transfer time = 1 / clock(KHz) * 9 bits * bytes */
usec = len * 9000 / dev->clock;
if (usec > 2000) {
usec -= 1500;
usleep_range(usec, usec + 100);
ft260_dbg("wait %d usec, len %d\n", usec, len);
}
/*
* Do not check the busy bit for combined transactions
* since the controller keeps the bus busy between writing
* and reading IOs to ensure an atomic operation.
*/
if (rep->flag == FT260_FLAG_START)
bus_busy = 0;
else
bus_busy = FT260_I2C_STATUS_BUS_BUSY;
do {
ret = ft260_xfer_status(dev, bus_busy);
if (ret != -EAGAIN)
break;
} while (--try);
if (ret == 0)
return 0;
ft260_i2c_reset(hdev);
return -EIO;
}
static int ft260_i2c_write(struct ft260_device *dev, u8 addr, u8 *data,
int len, u8 flag)
{
int ret, wr_len, idx = 0;
struct hid_device *hdev = dev->hdev;
struct ft260_i2c_write_request_report *rep =
(struct ft260_i2c_write_request_report *)dev->write_buf;
if (len < 1)
return -EINVAL;
rep->flag = FT260_FLAG_START;
do {
if (len <= FT260_WR_DATA_MAX) {
wr_len = len;
if (flag == FT260_FLAG_START_STOP)
rep->flag |= FT260_FLAG_STOP;
} else {
wr_len = FT260_WR_DATA_MAX;
}
rep->report = FT260_I2C_DATA_REPORT_ID(wr_len);
rep->address = addr;
rep->length = wr_len;
memcpy(rep->data, &data[idx], wr_len);
ft260_dbg("rep %#02x addr %#02x off %d len %d wlen %d flag %#x d[0] %#02x\n",
rep->report, addr, idx, len, wr_len,
rep->flag, data[0]);
ret = ft260_hid_output_report_check_status(dev, (u8 *)rep,
wr_len + 4);
if (ret < 0) {
hid_err(hdev, "%s: failed with %d\n", __func__, ret);
return ret;
}
len -= wr_len;
idx += wr_len;
rep->flag = 0;
} while (len > 0);
return 0;
}
static int ft260_smbus_write(struct ft260_device *dev, u8 addr, u8 cmd,
u8 *data, u8 data_len, u8 flag)
{
int ret = 0;
int len = 4;
struct ft260_i2c_write_request_report *rep =
(struct ft260_i2c_write_request_report *)dev->write_buf;
if (data_len >= sizeof(rep->data))
return -EINVAL;
rep->address = addr;
rep->data[0] = cmd;
rep->length = data_len + 1;
rep->flag = flag;
len += rep->length;
rep->report = FT260_I2C_DATA_REPORT_ID(len);
if (data_len > 0)
memcpy(&rep->data[1], data, data_len);
ft260_dbg("rep %#02x addr %#02x cmd %#02x datlen %d replen %d\n",
rep->report, addr, cmd, rep->length, len);
ret = ft260_hid_output_report_check_status(dev, (u8 *)rep, len);
return ret;
}
static int ft260_i2c_read(struct ft260_device *dev, u8 addr, u8 *data,
u16 len, u8 flag)
{
u16 rd_len;
u16 rd_data_max = 60;
int timeout, ret = 0;
struct ft260_i2c_read_request_report rep;
struct hid_device *hdev = dev->hdev;
u8 bus_busy = 0;
if ((flag & FT260_FLAG_START_REPEATED) == FT260_FLAG_START_REPEATED)
flag = FT260_FLAG_START_REPEATED;
else
flag = FT260_FLAG_START;
do {
if (len <= rd_data_max) {
rd_len = len;
flag |= FT260_FLAG_STOP;
} else {
rd_len = rd_data_max;
}
rd_data_max = FT260_RD_DATA_MAX;
rep.report = FT260_I2C_READ_REQ;
rep.length = cpu_to_le16(rd_len);
rep.address = addr;
rep.flag = flag;
ft260_dbg("rep %#02x addr %#02x len %d rlen %d flag %#x\n",
rep.report, rep.address, len, rd_len, flag);
reinit_completion(&dev->wait);
dev->read_idx = 0;
dev->read_buf = data;
dev->read_len = rd_len;
ret = ft260_hid_output_report(hdev, (u8 *)&rep, sizeof(rep));
if (ret < 0) {
hid_err(hdev, "%s: failed with %d\n", __func__, ret);
goto ft260_i2c_read_exit;
}
timeout = msecs_to_jiffies(5000);
if (!wait_for_completion_timeout(&dev->wait, timeout)) {
ret = -ETIMEDOUT;
ft260_i2c_reset(hdev);
goto ft260_i2c_read_exit;
}
dev->read_buf = NULL;
if (flag & FT260_FLAG_STOP)
bus_busy = FT260_I2C_STATUS_BUS_BUSY;
ret = ft260_xfer_status(dev, bus_busy);
if (ret < 0) {
ret = -EIO;
ft260_i2c_reset(hdev);
goto ft260_i2c_read_exit;
}
len -= rd_len;
data += rd_len;
flag = 0;
} while (len > 0);
ft260_i2c_read_exit:
dev->read_buf = NULL;
return ret;
}
/*
* A random read operation is implemented as a dummy write operation, followed
* by a current address read operation. The dummy write operation is used to
* load the target byte address into the current byte address counter, from
* which the subsequent current address read operation then reads.
*/
static int ft260_i2c_write_read(struct ft260_device *dev, struct i2c_msg *msgs)
{
int ret;
int wr_len = msgs[0].len;
int rd_len = msgs[1].len;
struct hid_device *hdev = dev->hdev;
u8 addr = msgs[0].addr;
u16 read_off = 0;
if (wr_len > 2) {
hid_err(hdev, "%s: invalid wr_len: %d\n", __func__, wr_len);
return -EOPNOTSUPP;
}
if (ft260_debug) {
if (wr_len == 2)
read_off = be16_to_cpu(*(__be16 *)msgs[0].buf);
else
read_off = *msgs[0].buf;
pr_info("%s: off %#x rlen %d wlen %d\n", __func__,
read_off, rd_len, wr_len);
}
ret = ft260_i2c_write(dev, addr, msgs[0].buf, wr_len,
FT260_FLAG_START);
if (ret < 0)
return ret;
ret = ft260_i2c_read(dev, addr, msgs[1].buf, rd_len,
FT260_FLAG_START_STOP_REPEATED);
if (ret < 0)
return ret;
return 0;
}
static int ft260_i2c_xfer(struct i2c_adapter *adapter, struct i2c_msg *msgs,
int num)
{
int ret;
struct ft260_device *dev = i2c_get_adapdata(adapter);
struct hid_device *hdev = dev->hdev;
mutex_lock(&dev->lock);
ret = hid_hw_power(hdev, PM_HINT_FULLON);
if (ret < 0) {
hid_err(hdev, "failed to enter FULLON power mode: %d\n", ret);
mutex_unlock(&dev->lock);
return ret;
}
if (num == 1) {
if (msgs->flags & I2C_M_RD)
ret = ft260_i2c_read(dev, msgs->addr, msgs->buf,
msgs->len, FT260_FLAG_START_STOP);
else
ret = ft260_i2c_write(dev, msgs->addr, msgs->buf,
msgs->len, FT260_FLAG_START_STOP);
if (ret < 0)
goto i2c_exit;
} else {
/* Combined write then read message */
ret = ft260_i2c_write_read(dev, msgs);
if (ret < 0)
goto i2c_exit;
}
ret = num;
i2c_exit:
hid_hw_power(hdev, PM_HINT_NORMAL);
mutex_unlock(&dev->lock);
return ret;
}
static int ft260_smbus_xfer(struct i2c_adapter *adapter, u16 addr, u16 flags,
char read_write, u8 cmd, int size,
union i2c_smbus_data *data)
{
int ret;
struct ft260_device *dev = i2c_get_adapdata(adapter);
struct hid_device *hdev = dev->hdev;
ft260_dbg("smbus size %d\n", size);
mutex_lock(&dev->lock);
ret = hid_hw_power(hdev, PM_HINT_FULLON);
if (ret < 0) {
hid_err(hdev, "power management error: %d\n", ret);
mutex_unlock(&dev->lock);
return ret;
}
switch (size) {
case I2C_SMBUS_BYTE:
if (read_write == I2C_SMBUS_READ)
ret = ft260_i2c_read(dev, addr, &data->byte, 1,
FT260_FLAG_START_STOP);
else
ret = ft260_smbus_write(dev, addr, cmd, NULL, 0,
FT260_FLAG_START_STOP);
break;
case I2C_SMBUS_BYTE_DATA:
if (read_write == I2C_SMBUS_READ) {
ret = ft260_smbus_write(dev, addr, cmd, NULL, 0,
FT260_FLAG_START);
if (ret)
goto smbus_exit;
ret = ft260_i2c_read(dev, addr, &data->byte, 1,
FT260_FLAG_START_STOP_REPEATED);
} else {
ret = ft260_smbus_write(dev, addr, cmd, &data->byte, 1,
FT260_FLAG_START_STOP);
}
break;
case I2C_SMBUS_WORD_DATA:
if (read_write == I2C_SMBUS_READ) {
ret = ft260_smbus_write(dev, addr, cmd, NULL, 0,
FT260_FLAG_START);
if (ret)
goto smbus_exit;
ret = ft260_i2c_read(dev, addr, (u8 *)&data->word, 2,
FT260_FLAG_START_STOP_REPEATED);
} else {
ret = ft260_smbus_write(dev, addr, cmd,
(u8 *)&data->word, 2,
FT260_FLAG_START_STOP);
}
break;
case I2C_SMBUS_BLOCK_DATA:
if (read_write == I2C_SMBUS_READ) {
ret = ft260_smbus_write(dev, addr, cmd, NULL, 0,
FT260_FLAG_START);
if (ret)
goto smbus_exit;
ret = ft260_i2c_read(dev, addr, data->block,
data->block[0] + 1,
FT260_FLAG_START_STOP_REPEATED);
} else {
ret = ft260_smbus_write(dev, addr, cmd, data->block,
data->block[0] + 1,
FT260_FLAG_START_STOP);
}
break;
case I2C_SMBUS_I2C_BLOCK_DATA:
if (read_write == I2C_SMBUS_READ) {
ret = ft260_smbus_write(dev, addr, cmd, NULL, 0,
FT260_FLAG_START);
if (ret)
goto smbus_exit;
ret = ft260_i2c_read(dev, addr, data->block + 1,
data->block[0],
FT260_FLAG_START_STOP_REPEATED);
} else {
ret = ft260_smbus_write(dev, addr, cmd, data->block + 1,
data->block[0],
FT260_FLAG_START_STOP);
}
break;
default:
hid_err(hdev, "unsupported smbus transaction size %d\n", size);
ret = -EOPNOTSUPP;
}
smbus_exit:
hid_hw_power(hdev, PM_HINT_NORMAL);
mutex_unlock(&dev->lock);
return ret;
}
static u32 ft260_functionality(struct i2c_adapter *adap)
{
return I2C_FUNC_I2C | I2C_FUNC_SMBUS_BYTE |
I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
I2C_FUNC_SMBUS_BLOCK_DATA | I2C_FUNC_SMBUS_I2C_BLOCK;
}
static const struct i2c_adapter_quirks ft260_i2c_quirks = {
.flags = I2C_AQ_COMB_WRITE_THEN_READ,
.max_comb_1st_msg_len = 2,
};
static const struct i2c_algorithm ft260_i2c_algo = {
.master_xfer = ft260_i2c_xfer,
.smbus_xfer = ft260_smbus_xfer,
.functionality = ft260_functionality,
};
static int ft260_get_system_config(struct hid_device *hdev,
struct ft260_get_system_status_report *cfg)
{
int ret;
int len = sizeof(struct ft260_get_system_status_report);
ret = ft260_hid_feature_report_get(hdev, FT260_SYSTEM_SETTINGS,
(u8 *)cfg, len);
if (ret < 0) {
hid_err(hdev, "failed to retrieve system status\n");
return ret;
}
return 0;
}
static int ft260_is_interface_enabled(struct hid_device *hdev)
{
struct ft260_get_system_status_report cfg;
struct usb_interface *usbif = to_usb_interface(hdev->dev.parent);
int interface = usbif->cur_altsetting->desc.bInterfaceNumber;
int ret;
ret = ft260_get_system_config(hdev, &cfg);
if (ret < 0)
return ret;
ft260_dbg("interface: 0x%02x\n", interface);
ft260_dbg("chip mode: 0x%02x\n", cfg.chip_mode);
ft260_dbg("clock_ctl: 0x%02x\n", cfg.clock_ctl);
ft260_dbg("i2c_enable: 0x%02x\n", cfg.i2c_enable);
ft260_dbg("uart_mode: 0x%02x\n", cfg.uart_mode);
switch (cfg.chip_mode) {
case FT260_MODE_ALL:
case FT260_MODE_BOTH:
if (interface == 1)
hid_info(hdev, "uart interface is not supported\n");
else
ret = 1;
break;
case FT260_MODE_UART:
hid_info(hdev, "uart interface is not supported\n");
break;
case FT260_MODE_I2C:
ret = 1;
break;
}
return ret;
}
static int ft260_byte_show(struct hid_device *hdev, int id, u8 *cfg, int len,
u8 *field, u8 *buf)
{
int ret;
ret = ft260_hid_feature_report_get(hdev, id, cfg, len);
if (ret < 0)
return ret;
return scnprintf(buf, PAGE_SIZE, "%d\n", *field);
}
static int ft260_word_show(struct hid_device *hdev, int id, u8 *cfg, int len,
__le16 *field, u8 *buf)
{
int ret;
ret = ft260_hid_feature_report_get(hdev, id, cfg, len);
if (ret < 0)
return ret;
return scnprintf(buf, PAGE_SIZE, "%d\n", le16_to_cpu(*field));
}
#define FT260_ATTR_SHOW(name, reptype, id, type, func) \
static ssize_t name##_show(struct device *kdev, \
struct device_attribute *attr, char *buf) \
{ \
struct reptype rep; \
struct hid_device *hdev = to_hid_device(kdev); \
type *field = &rep.name; \
int len = sizeof(rep); \
\
return func(hdev, id, (u8 *)&rep, len, field, buf); \
}
#define FT260_SSTAT_ATTR_SHOW(name) \
FT260_ATTR_SHOW(name, ft260_get_system_status_report, \
FT260_SYSTEM_SETTINGS, u8, ft260_byte_show)
#define FT260_I2CST_ATTR_SHOW(name) \
FT260_ATTR_SHOW(name, ft260_get_i2c_status_report, \
FT260_I2C_STATUS, __le16, ft260_word_show)
#define FT260_ATTR_STORE(name, reptype, id, req, type, ctype, func) \
static ssize_t name##_store(struct device *kdev, \
struct device_attribute *attr, \
const char *buf, size_t count) \
{ \
struct reptype rep; \
struct hid_device *hdev = to_hid_device(kdev); \
type name; \
int ret; \
\
if (!func(buf, 10, (ctype *)&name)) { \
rep.name = name; \
rep.report = id; \
rep.request = req; \
ret = ft260_hid_feature_report_set(hdev, (u8 *)&rep, \
sizeof(rep)); \
if (!ret) \
ret = count; \
} else { \
ret = -EINVAL; \
} \
return ret; \
}
#define FT260_BYTE_ATTR_STORE(name, reptype, req) \
FT260_ATTR_STORE(name, reptype, FT260_SYSTEM_SETTINGS, req, \
u8, u8, kstrtou8)
#define FT260_WORD_ATTR_STORE(name, reptype, req) \
FT260_ATTR_STORE(name, reptype, FT260_SYSTEM_SETTINGS, req, \
__le16, u16, kstrtou16)
FT260_SSTAT_ATTR_SHOW(chip_mode);
static DEVICE_ATTR_RO(chip_mode);
FT260_SSTAT_ATTR_SHOW(pwren_status);
static DEVICE_ATTR_RO(pwren_status);
FT260_SSTAT_ATTR_SHOW(suspend_status);
static DEVICE_ATTR_RO(suspend_status);
FT260_SSTAT_ATTR_SHOW(hid_over_i2c_en);
static DEVICE_ATTR_RO(hid_over_i2c_en);
FT260_SSTAT_ATTR_SHOW(power_saving_en);
static DEVICE_ATTR_RO(power_saving_en);
FT260_SSTAT_ATTR_SHOW(i2c_enable);
FT260_BYTE_ATTR_STORE(i2c_enable, ft260_set_i2c_mode_report,
FT260_SET_I2C_MODE);
static DEVICE_ATTR_RW(i2c_enable);
FT260_SSTAT_ATTR_SHOW(uart_mode);
FT260_BYTE_ATTR_STORE(uart_mode, ft260_set_uart_mode_report,
FT260_SET_UART_MODE);
static DEVICE_ATTR_RW(uart_mode);
FT260_SSTAT_ATTR_SHOW(clock_ctl);
FT260_BYTE_ATTR_STORE(clock_ctl, ft260_set_system_clock_report,
FT260_SET_CLOCK);
static DEVICE_ATTR_RW(clock_ctl);
FT260_I2CST_ATTR_SHOW(clock);
FT260_WORD_ATTR_STORE(clock, ft260_set_i2c_speed_report,
FT260_SET_I2C_CLOCK_SPEED);
static DEVICE_ATTR_RW(clock);
static ssize_t i2c_reset_store(struct device *kdev,
struct device_attribute *attr, const char *buf,
size_t count)
{
struct hid_device *hdev = to_hid_device(kdev);
int ret = ft260_i2c_reset(hdev);
if (ret)
return ret;
return count;
}
static DEVICE_ATTR_WO(i2c_reset);
static const struct attribute_group ft260_attr_group = {
.attrs = (struct attribute *[]) {
&dev_attr_chip_mode.attr,
&dev_attr_pwren_status.attr,
&dev_attr_suspend_status.attr,
&dev_attr_hid_over_i2c_en.attr,
&dev_attr_power_saving_en.attr,
&dev_attr_i2c_enable.attr,
&dev_attr_uart_mode.attr,
&dev_attr_clock_ctl.attr,
&dev_attr_i2c_reset.attr,
&dev_attr_clock.attr,
NULL
}
};
static int ft260_probe(struct hid_device *hdev, const struct hid_device_id *id)
{
struct ft260_device *dev;
struct ft260_get_chip_version_report version;
int ret;
if (!hid_is_usb(hdev))
return -EINVAL;
dev = devm_kzalloc(&hdev->dev, sizeof(*dev), GFP_KERNEL);
if (!dev)
return -ENOMEM;
ret = hid_parse(hdev);
if (ret) {
hid_err(hdev, "failed to parse HID\n");
return ret;
}
ret = hid_hw_start(hdev, 0);
if (ret) {
hid_err(hdev, "failed to start HID HW\n");
return ret;
}
ret = hid_hw_open(hdev);
if (ret) {
hid_err(hdev, "failed to open HID HW\n");
goto err_hid_stop;
}
ret = ft260_hid_feature_report_get(hdev, FT260_CHIP_VERSION,
(u8 *)&version, sizeof(version));
if (ret < 0) {
hid_err(hdev, "failed to retrieve chip version\n");
goto err_hid_close;
}
hid_info(hdev, "chip code: %02x%02x %02x%02x\n",
version.chip_code[0], version.chip_code[1],
version.chip_code[2], version.chip_code[3]);
ret = ft260_is_interface_enabled(hdev);
if (ret <= 0)
goto err_hid_close;
hid_info(hdev, "USB HID v%x.%02x Device [%s] on %s\n",
hdev->version >> 8, hdev->version & 0xff, hdev->name,
hdev->phys);
hid_set_drvdata(hdev, dev);
dev->hdev = hdev;
dev->adap.owner = THIS_MODULE;
dev->adap.class = I2C_CLASS_HWMON;
dev->adap.algo = &ft260_i2c_algo;
dev->adap.quirks = &ft260_i2c_quirks;
dev->adap.dev.parent = &hdev->dev;
snprintf(dev->adap.name, sizeof(dev->adap.name),
"FT260 usb-i2c bridge");
mutex_init(&dev->lock);
init_completion(&dev->wait);
ret = ft260_xfer_status(dev, FT260_I2C_STATUS_BUS_BUSY);
if (ret)
ft260_i2c_reset(hdev);
i2c_set_adapdata(&dev->adap, dev);
ret = i2c_add_adapter(&dev->adap);
if (ret) {
hid_err(hdev, "failed to add i2c adapter\n");
goto err_hid_close;
}
ret = sysfs_create_group(&hdev->dev.kobj, &ft260_attr_group);
if (ret < 0) {
hid_err(hdev, "failed to create sysfs attrs\n");
goto err_i2c_free;
}
return 0;
err_i2c_free:
i2c_del_adapter(&dev->adap);
err_hid_close:
hid_hw_close(hdev);
err_hid_stop:
hid_hw_stop(hdev);
return ret;
}
static void ft260_remove(struct hid_device *hdev)
{
struct ft260_device *dev = hid_get_drvdata(hdev);
if (!dev)
return;
sysfs_remove_group(&hdev->dev.kobj, &ft260_attr_group);
i2c_del_adapter(&dev->adap);
hid_hw_close(hdev);
hid_hw_stop(hdev);
}
static int ft260_raw_event(struct hid_device *hdev, struct hid_report *report,
u8 *data, int size)
{
struct ft260_device *dev = hid_get_drvdata(hdev);
struct ft260_i2c_input_report *xfer = (void *)data;
if (xfer->report >= FT260_I2C_REPORT_MIN &&
xfer->report <= FT260_I2C_REPORT_MAX) {
ft260_dbg("i2c resp: rep %#02x len %d\n", xfer->report,
xfer->length);
if ((dev->read_buf == NULL) ||
(xfer->length > dev->read_len - dev->read_idx)) {
hid_err(hdev, "unexpected report %#02x, length %d\n",
xfer->report, xfer->length);
return -1;
}
memcpy(&dev->read_buf[dev->read_idx], &xfer->data,
xfer->length);
dev->read_idx += xfer->length;
if (dev->read_idx == dev->read_len)
complete(&dev->wait);
} else {
hid_err(hdev, "unhandled report %#02x\n", xfer->report);
}
return 0;
}
static struct hid_driver ft260_driver = {
.name = "ft260",
.id_table = ft260_devices,
.probe = ft260_probe,
.remove = ft260_remove,
.raw_event = ft260_raw_event,
};
module_hid_driver(ft260_driver);
MODULE_DESCRIPTION("FTDI FT260 USB HID to I2C host bridge");
MODULE_AUTHOR("Michael Zaidman <michael.zaidman@gmail.com>");
MODULE_LICENSE("GPL v2");
]
|
| 28/2 |
2026/01/30 14:37 |
llm |
debugger |
1m
Model:
Tokens:
input: 34170
output: 975
thoughts: 6283
Thoughts: **Analysis of the FT260 SMBus Out-of-Bounds Read Vulnerability**
Okay, so here's the deal. I've been digging into this crash report, and it seems the root cause lies in a missing validation within the `ft260_smbus_xfer` function, specifically in the `drivers/hid/hid-ft260.c` file. The crash itself happens during a `memcpy` operation inside `ft260_smbus_write`, but the problem originates earlier, during the data transfer initiated via `i2cdev_ioctl_smbus` through `i2c_smbus_xfer`.
The core issue is that the `ft260_smbus_xfer` function doesn't adequately check the length field (`data->block[0]`) of the SMBus block data. This data is coming directly from user space, passed through `i2cdev_ioctl_smbus`, and crucially, it's placed on the stack within a `union i2c_smbus_data` structure. This union is only 34 bytes large, as defined by `I2C_SMBUS_BLOCK_MAX + 2`. The issue is most prominent with the `I2C_SMBUS_I2C_BLOCK_DATA` transaction type.
Let me break down the scenario based on the crash report. A user provides a malicious length value, say, 42, within the `temp.block[0]` field, which, in our case is `data->block[0]`. In the vulnerable `I2C_SMBUS_I2C_BLOCK_DATA` case, `ft260_smbus_xfer` then calls `ft260_smbus_write`. It passes `data->block + 1` which is a pointer to the start of the data and `data->block[0]` for the length (which is 42). `ft260_smbus_write` copies this data, using `memcpy`. `data` within `ft260_smbus_write` is actually `&temp.block[1]`, the data from user space. In this specific case, if `data->block[0]` is 42, `memcpy` reads 42 bytes from `&temp.block[1]`. `&temp.block[1]` points to the 33 bytes of valid data. This means `memcpy` copies past the end of the stack buffer which is only 33 bytes. This causes an out-of-bounds read, and in this specific case, KASAN flags it as a stack out of bounds read, precisely what we observed with the crash report. The stack offset of 33 matches perfectly with the location of the error within the stack frame, confirming my hypothesis.
The vulnerability stems from the fact that `ft260_smbus_write` itself doesn't validate the length (`data_len`), because of the generic nature of the function, while the caller, `ft260_smbus_xfer`, is the one with the specific knowledge of data source.
Specifically, look at these lines of code:
In `drivers/hid/hid-ft260.c`:
```c
736: ret = ft260_smbus_write(dev, addr, cmd, data->block + 1,
737: data->block[0],
738: FT260_FLAG_START_STOP);
```
The fix is straightforward. We need to add a check in `ft260_smbus_xfer` *before* calling `ft260_smbus_write` to ensure that `data->block[0]` (the length provided by the user) does not exceed `I2C_SMBUS_BLOCK_MAX`, which is 32. This prevents the `memcpy` in `ft260_smbus_write` from reading beyond the valid buffer. The relevant check in `ft260_smbus_xfer` is required in the `I2C_SMBUS_BLOCK_DATA` case as well.
So the fix is to add validation for `data->block[0]` and enforce that it be at most `I2C_SMBUS_BLOCK_MAX` before passing it to `ft260_smbus_write` for both `I2C_SMBUS_BLOCK_DATA` and `I2C_SMBUS_I2C_BLOCK_DATA` cases. The vulnerable line is drivers/hid/hid-ft260.c:736 and drivers/hid/hid-ft2
|
| 29/1 |
2026/01/30 14:38 |
action |
kernel-scratch-checkouter |
0m
Results: map[KernelScratchSrc:/usr/local/google/home/dvyukov/syzkaller/agent/workdir/cache/tmp/tmp876098146]
|
| 30/1 |
2026/01/30 14:39 |
loop |
|
13m
|
| 31/2 |
2026/01/30 14:39 |
iteration |
0 |
13m
|
| 32/3 |
2026/01/30 14:39 |
agent |
patch-generator |
3m
Model:
Instruction:
You are an experienced Linux kernel developer tasked with creating a fix for a kernel bug.
You will be given a crash report, and an initial explanation of the root cause done by another
kernel expert.
Use the codeedit tool to do code edits.
Note: you will not see your changes when looking at the code using codesearch tools.
Your final reply should contain explanation of what you did in the patch and why
(details not present in the initial explanation of the bug).
Your fix must not just prevent the given crash, but also be the best fix for the underlying
root cause from the software engineering point of view. There can be several ways to fix the
same bug. Consider alternatives, and pick the best one. For example, additional checks may be
added at different locations/functions, it's usually better to place them earlier in the
execution to avoid multiple checks at various locations later.
Frequently the same coding mistake is done in several locations in the source code.
Check if your fix should be extended/applied to similar cases around to fix other similar bugs.
But don't go too wide, don't try to fix problems kernel-wide, fix similar issues
in the same file only.
If you are changing post-conditions of a function, consider all callers of the functions,
and if they need to be updated to handle new post-conditions. For example, if you make
a function that previously never returned a NULL, return NULL, consider if callers
need to be updated to handle NULL return value.
Prefer calling several tools at the same time to save round-trips.
Prompt:
The crash that corresponds to the bug is:
==================================================================
BUG: KASAN: stack-out-of-bounds in ft260_smbus_write+0x156/0x2f0 drivers/hid/hid-ft260.c:486
Read of size 42 at addr ffffc90002417d21 by task syz-executor165/6009
CPU: 0 UID: 0 PID: 6009 Comm: syz-executor165 Not tainted syzkaller #1 PREEMPT(full)
Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.17.0-debian-1.17.0-1 04/01/2014
Call Trace:
<TASK>
__dump_stack lib/dump_stack.c:94 [inline]
dump_stack_lvl+0x10e/0x190 lib/dump_stack.c:120
print_address_description mm/kasan/report.c:378 [inline]
print_report+0x17e/0x810 mm/kasan/report.c:482
kasan_report+0x147/0x180 mm/kasan/report.c:595
check_region_inline mm/kasan/generic.c:-1 [inline]
kasan_check_range+0x2b0/0x2c0 mm/kasan/generic.c:200
__asan_memcpy+0x29/0x70 mm/kasan/shadow.c:105
ft260_smbus_write+0x156/0x2f0 drivers/hid/hid-ft260.c:486
ft260_smbus_xfer+0x8bc/0xdd0 drivers/hid/hid-ft260.c:-1
__i2c_smbus_xfer+0x557/0x1e20 drivers/i2c/i2c-core-smbus.c:591
i2c_smbus_xfer+0x1f4/0x310 drivers/i2c/i2c-core-smbus.c:546
i2cdev_ioctl_smbus+0x3db/0x750 drivers/i2c/i2c-dev.c:389
i2cdev_ioctl+0x5d3/0x820 drivers/i2c/i2c-dev.c:478
vfs_ioctl fs/ioctl.c:51 [inline]
__do_sys_ioctl fs/ioctl.c:597 [inline]
__se_sys_ioctl+0xfc/0x170 fs/ioctl.c:583
do_syscall_x64 arch/x86/entry/syscall_64.c:63 [inline]
do_syscall_64+0xe8/0xf80 arch/x86/entry/syscall_64.c:94
entry_SYSCALL_64_after_hwframe+0x77/0x7f
RIP: 0033:0x7f1925b454f9
Code: c0 79 93 eb d5 48 8d 7c 1d 00 eb 99 0f 1f 44 00 00 48 89 f8 48 89 f7 48 89 d6 48 89 ca 4d 89 c2 4d 89 c8 4c 8b 4c 24 08 0f 05 <48> 3d 01 f0 ff ff 73 01 c3 48 c7 c1 d8 ff ff ff f7 d8 64 89 01 48
RSP: 002b:00007ffee7bc0ab8 EFLAGS: 00000246 ORIG_RAX: 0000000000000010
RAX: ffffffffffffffda RBX: 00007ffee7bc0c48 RCX: 00007f1925b454f9
RDX: 0000200000000200 RSI: 0000000000000720 RDI: 0000000000000004
RBP: 0000000000000001 R08: 0000000000000000 R09: 0000000000000009
R10: 0000000000000000 R11: 0000000000000246 R12: 00007ffee7bc0ae0
R13: 00007ffee7bc0ad0 R14: 00007f1925bd8c40 R15: 0000000000000000
</TASK>
The buggy address belongs to stack of task syz-executor165/6009
and is located at offset 33 in frame:
i2cdev_ioctl_smbus+0x0/0x750 drivers/i2c/i2c-dev.c:317
This frame has 1 object:
[32, 66) 'temp'
The buggy address belongs to a 8-page vmalloc region starting at 0xffffc90002410000 allocated at copy_process+0x4e7/0x3950 kernel/fork.c:2052
The buggy address belongs to the physical page:
page: refcount:1 mapcount:0 mapping:0000000000000000 index:0x0 pfn:0x213ff
flags: 0xfff00000000000(node=0|zone=1|lastcpupid=0x7ff)
raw: 00fff00000000000 0000000000000000 ffffea000084ffc8 0000000000000000
raw: 0000000000000000 0000000000000000 00000001ffffffff 0000000000000000
page dumped because: kasan: bad access detected
page_owner tracks the page as allocated
page last allocated via order 0, migratetype Unmovable, gfp_mask 0x29c2(GFP_NOWAIT|__GFP_HIGHMEM|__GFP_IO|__GFP_FS|__GFP_ZERO), pid 6007, tgid 6007 (syz-executor165), ts 90494270799, free_ts 87604917166
set_page_owner include/linux/page_owner.h:32 [inline]
post_alloc_hook+0x234/0x290 mm/page_alloc.c:1884
prep_new_page mm/page_alloc.c:1892 [inline]
get_page_from_freelist+0x24e4/0x2580 mm/page_alloc.c:3945
__alloc_frozen_pages_noprof+0x181/0x370 mm/page_alloc.c:5240
alloc_pages_mpol+0x232/0x4a0 mm/mempolicy.c:2486
alloc_frozen_pages_noprof mm/mempolicy.c:2557 [inline]
alloc_pages_noprof+0xa9/0x190 mm/mempolicy.c:2577
vm_area_alloc_pages mm/vmalloc.c:3649 [inline]
__vmalloc_area_node mm/vmalloc.c:3863 [inline]
__vmalloc_node_range_noprof+0x919/0x16a0 mm/vmalloc.c:4051
__vmalloc_node_noprof+0xc2/0x110 mm/vmalloc.c:4111
alloc_thread_stack_node kernel/fork.c:354 [inline]
dup_task_struct+0x250/0x9b0 kernel/fork.c:923
copy_process+0x4e7/0x3950 kernel/fork.c:2052
kernel_clone+0x21e/0x820 kernel/fork.c:2651
__do_sys_clone kernel/fork.c:2792 [inline]
__se_sys_clone kernel/fork.c:2776 [inline]
__x64_sys_clone+0x190/0x1f0 kernel/fork.c:2776
do_syscall_x64 arch/x86/entry/syscall_64.c:63 [inline]
do_syscall_64+0xe8/0xf80 arch/x86/entry/syscall_64.c:94
entry_SYSCALL_64_after_hwframe+0x77/0x7f
page last free pid 5954 tgid 5954 stack trace:
reset_page_owner include/linux/page_owner.h:25 [inline]
free_pages_prepare mm/page_alloc.c:1433 [inline]
__free_frozen_pages+0xbc4/0xd40 mm/page_alloc.c:2973
discard_slab mm/slub.c:3346 [inline]
__put_partials+0x146/0x170 mm/slub.c:3886
__slab_free+0x294/0x320 mm/slub.c:5956
qlink_free mm/kasan/quarantine.c:163 [inline]
qlist_free_all+0x97/0x100 mm/kasan/quarantine.c:179
kasan_quarantine_reduce+0x148/0x160 mm/kasan/quarantine.c:286
__kasan_slab_alloc+0x22/0x80 mm/kasan/common.c:350
kasan_slab_alloc include/linux/kasan.h:253 [inline]
slab_post_alloc_hook mm/slub.c:4953 [inline]
slab_alloc_node mm/slub.c:5263 [inline]
kmem_cache_alloc_noprof+0x37d/0x710 mm/slub.c:5270
getname_flags+0xb8/0x540 fs/namei.c:146
getname include/linux/fs.h:2498 [inline]
getname_maybe_null include/linux/fs.h:2505 [inline]
vfs_fstatat+0x43/0x170 fs/stat.c:370
__do_sys_newfstatat fs/stat.c:542 [inline]
__se_sys_newfstatat fs/stat.c:536 [inline]
__x64_sys_newfstatat+0x116/0x190 fs/stat.c:536
do_syscall_x64 arch/x86/entry/syscall_64.c:63 [inline]
do_syscall_64+0xe8/0xf80 arch/x86/entry/syscall_64.c:94
entry_SYSCALL_64_after_hwframe+0x77/0x7f
Memory state around the buggy address:
ffffc90002417c00: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
ffffc90002417c80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
>ffffc90002417d00: f1 f1 f1 f1 00 00 00 00 02 f3 f3 f3 f3 f3 f3 f3
^
ffffc90002417d80: 00 00 00 00 00 00 00 00 00 00 00 00 f1 f1 f1 f1
ffffc90002417e00: 04 f2 00 00 f2 f2 00 00 f3 f3 f3 f3 00 00 00 00
==================================================================
The explanation of the root cause of the bug is:
The root cause of the KASAN stack-out-of-bounds crash is a missing validation of the input data length in the `ft260_smbus_xfer` function in `drivers/hid/hid-ft260.c`.
When handling `I2C_SMBUS_BLOCK_DATA` or `I2C_SMBUS_I2C_BLOCK_DATA` transactions, the function uses the length byte provided by the user (stored in `data->block[0]`) to determine how much data to copy. However, it fails to ensure that this length does not exceed the size of the `data->block` array (or `I2C_SMBUS_BLOCK_MAX`), which is backed by the `union i2c_smbus_data` on the stack of the calling function `i2cdev_ioctl_smbus`.
The `union i2c_smbus_data` has a size of 34 bytes (`I2C_SMBUS_BLOCK_MAX` is 32, plus 2 bytes). The `block` field is defined as `__u8 block[I2C_SMBUS_BLOCK_MAX + 2];`.
In the crash scenario (`I2C_SMBUS_I2C_BLOCK_DATA`), the code does:
```c
736: ret = ft260_smbus_write(dev, addr, cmd, data->block + 1,
737: data->block[0],
738: FT260_FLAG_START_STOP);
```
Here, `data->block[0]` (controlled by the user) is passed as `data_len`. If the user provides a length (e.g., 42) that is larger than the remaining size of the buffer (which is 33 bytes, starting from `data->block[1]`), `ft260_smbus_write` will read out of bounds.
In `ft260_smbus_write`:
```c
486: memcpy(&rep->data[1], data, data_len);
```
It performs a `memcpy` from the stack buffer `data` (which is `&temp.block[1]`) with size 42. Since `temp.block` is only 34 bytes total, reading 42 bytes from offset 1 accesses invalid stack memory, triggering KASAN.
**Fix:**
Validate that `data->block[0]` is less than or equal to `I2C_SMBUS_BLOCK_MAX` (32) in `ft260_smbus_xfer` before processing block transfers.
**Code Snippet to fix in `drivers/hid/hid-ft260.c`:**
```c
static int ft260_smbus_xfer(struct i2c_adapter *adapter, u16 addr, u16 flags,
char read_write, u8 cmd, int size,
union i2c_smbus_data *data)
{
// ...
switch (size) {
// ...
case I2C_SMBUS_BLOCK_DATA:
if (read_write == I2C_SMBUS_READ) {
// ...
} else {
if (data->block[0] > I2C_SMBUS_BLOCK_MAX)
return -EINVAL; // Add this check
ret = ft260_smbus_write(dev, addr, cmd, data->block,
data->block[0] + 1,
FT260_FLAG_START_STOP);
}
break;
case I2C_SMBUS_I2C_BLOCK_DATA:
if (read_write == I2C_SMBUS_READ) {
// ...
} else {
if (data->block[0] > I2C_SMBUS_BLOCK_MAX)
return -EINVAL; // Add this check
ret = ft260_smbus_write(dev, addr, cmd, data->block + 1,
data->block[0],
FT260_FLAG_START_STOP);
}
break;
// ...
}
// ...
}
```
Reply: The KASAN crash (stack-out-of-bounds read) occurs in `ft260_smbus_write` because the `ft260_smbus_xfer` function does not validate the length of the data supplied by the user (`data->block[0]`) against the size of the backing buffer (`I2C_SMBUS_BLOCK_MAX`).
When `I2C_SMBUS_BLOCK_DATA` or `I2C_SMBUS_I2C_BLOCK_DATA` transaction is requested, the user-supplied length in `data->block[0]` is passed to `ft260_smbus_write`, which then performs a `memcpy` from the stack buffer `data->block`. Since `data->block` is part of `union i2c_smbus_data` on the stack (size 34 bytes), providing a length larger than 32 (plus protocol overhead) leads to reading beyond the stack variable boundaries.
I have fixed the issue by adding validation checks for `data->block[0] > I2C_SMBUS_BLOCK_MAX` (32) in `ft260_smbus_xfer`. The validation is added for both `I2C_SMBUS_BLOCK_DATA` and `I2C_SMBUS_I2C_BLOCK_DATA`, and for both READ and WRITE operations. This not only fixes the reported crash (WRITE path), but also prevents potential stack buffer overflows in the READ path (where `ft260_i2c_read` writes to the buffer), ensuring the driver operates within safe memory bounds.
The fix handles the error by returning `-EINVAL` and jumping to `smbus_exit` to correctly release the mutex.
```c
static int ft260_smbus_xfer(struct i2c_adapter *adapter, u16 addr, u16 flags,
char read_write, u8 cmd, int size,
union i2c_smbus_data *data)
{
// ...
case I2C_SMBUS_BLOCK_DATA:
if (read_write == I2C_SMBUS_READ) {
if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
ret = -EINVAL;
goto smbus_exit;
}
ret = ft260_smbus_write(dev, addr, cmd, NULL, 0,
FT260_FLAG_START);
if (ret)
goto smbus_exit;
ret = ft260_i2c_read(dev, addr, data->block,
data->block[0] + 1,
FT260_FLAG_START_STOP_REPEATED);
} else {
if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
ret = -EINVAL;
goto smbus_exit;
}
ret = ft260_smbus_write(dev, addr, cmd, data->block,
data->block[0] + 1,
FT260_FLAG_START_STOP);
}
break;
case I2C_SMBUS_I2C_BLOCK_DATA:
if (read_write == I2C_SMBUS_READ) {
if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
ret = -EINVAL;
goto smbus_exit;
}
ret = ft260_smbus_write(dev, addr, cmd, NULL, 0,
FT260_FLAG_START);
if (ret)
goto smbus_exit;
ret = ft260_i2c_read(dev, addr, data->block + 1,
data->block[0],
FT260_FLAG_START_STOP_REPEATED);
} else {
if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
ret = -EINVAL;
goto smbus_exit;
}
ret = ft260_smbus_write(dev, addr, cmd, data->block + 1,
data->block[0],
FT260_FLAG_START_STOP);
}
break;
// ...
}
```
|
| 33/4 |
2026/01/30 14:39 |
llm |
patch-generator |
0m
Model:
Tokens:
input: 7205
output: 27
thoughts: 287
Thoughts: **Fixing a KASAN Stack-Out-of-Bounds Bug in hid-ft260.c**
Okay, so I've got a KASAN stack-out-of-bounds bug on my hands in the `hid-ft260.c` driver, specifically within the `ft260_smbus_xfer` function. The problem boils down to a potential vulnerability where `data->block[0]` is used to determine the data length in a write operation without proper validation. It seems that if this value exceeds `I2C_SMBUS_BLOCK_MAX`, `ft260_smbus_write` will end up reading data out of bounds from `data->block`, leading to this memory corruption.
My task now is to dive into `drivers/hid/hid-ft260.c` and isolate the problematic code. The suggested fix appears straightforward β I need to add a check for `data->block[0] > I2C_SMBUS_BLOCK_MAX` within the `I2C_SMBUS_BLOCK_DATA` and `I2C_SMBUS_I2C_BLOCK_DATA` cases, but only when it's a write operation (`read_write == I2C_SMBUS_WRITE`). This is the *else* branch of the `if (read_write == I2C_SMBUS_READ)` statement.
First things first, I'll need to thoroughly review `drivers/hid/hid-ft260.c` to get a good understanding of the surrounding code and the function's overall purpose. I need to make sure I grasp the full context before making the required changes.
|
| 34/4 |
2026/01/30 14:39 |
tool |
read-file |
0m
Args: map[File:drivers/hid/hid-ft260.c]
Results: map[Contents:// SPDX-License-Identifier: GPL-2.0-only
/*
* hid-ft260.c - FTDI FT260 USB HID to I2C host bridge
*
* Copyright (c) 2021, Michael Zaidman <michaelz@xsightlabs.com>
*
* Data Sheet:
* https://www.ftdichip.com/Support/Documents/DataSheets/ICs/DS_FT260.pdf
*/
#include "hid-ids.h"
#include <linux/hidraw.h>
#include <linux/i2c.h>
#include <linux/module.h>
#include <linux/usb.h>
#ifdef DEBUG
static int ft260_debug = 1;
#else
static int ft260_debug;
#endif
module_param_named(debug, ft260_debug, int, 0600);
MODULE_PARM_DESC(debug, "Toggle FT260 debugging messages");
#define ft260_dbg(format, arg...) \
do { \
if (ft260_debug) \
pr_info("%s: " format, __func__, ##arg); \
} while (0)
#define FT260_REPORT_MAX_LENGTH (64)
#define FT260_I2C_DATA_REPORT_ID(len) (FT260_I2C_REPORT_MIN + (len - 1) / 4)
#define FT260_WAKEUP_NEEDED_AFTER_MS (4800) /* 5s minus 200ms margin */
/*
* The ft260 input report format defines 62 bytes for the data payload, but
* when requested 62 bytes, the controller returns 60 and 2 in separate input
* reports. To achieve better performance with the multi-report read data
* transfers, we set the maximum read payload length to a multiple of 60.
* With a 100 kHz I2C clock, one 240 bytes read takes about 1/27 second,
* which is excessive; On the other hand, some higher layer drivers like at24
* or optoe limit the i2c reads to 128 bytes. To not block other drivers out
* of I2C for potentially troublesome amounts of time, we select the maximum
* read payload length to be 180 bytes.
*/
#define FT260_RD_DATA_MAX (180)
#define FT260_WR_DATA_MAX (60)
/*
* Device interface configuration.
* The FT260 has 2 interfaces that are controlled by DCNF0 and DCNF1 pins.
* First implementes USB HID to I2C bridge function and
* second - USB HID to UART bridge function.
*/
enum {
FT260_MODE_ALL = 0x00,
FT260_MODE_I2C = 0x01,
FT260_MODE_UART = 0x02,
FT260_MODE_BOTH = 0x03,
};
/* Control pipe */
enum {
FT260_GET_RQST_TYPE = 0xA1,
FT260_GET_REPORT = 0x01,
FT260_SET_RQST_TYPE = 0x21,
FT260_SET_REPORT = 0x09,
FT260_FEATURE = 0x03,
};
/* Report IDs / Feature In */
enum {
FT260_CHIP_VERSION = 0xA0,
FT260_SYSTEM_SETTINGS = 0xA1,
FT260_I2C_STATUS = 0xC0,
FT260_I2C_READ_REQ = 0xC2,
FT260_I2C_REPORT_MIN = 0xD0,
FT260_I2C_REPORT_MAX = 0xDE,
FT260_GPIO = 0xB0,
FT260_UART_INTERRUPT_STATUS = 0xB1,
FT260_UART_STATUS = 0xE0,
FT260_UART_RI_DCD_STATUS = 0xE1,
FT260_UART_REPORT = 0xF0,
};
/* Feature Out */
enum {
FT260_SET_CLOCK = 0x01,
FT260_SET_I2C_MODE = 0x02,
FT260_SET_UART_MODE = 0x03,
FT260_ENABLE_INTERRUPT = 0x05,
FT260_SELECT_GPIO2_FUNC = 0x06,
FT260_ENABLE_UART_DCD_RI = 0x07,
FT260_SELECT_GPIOA_FUNC = 0x08,
FT260_SELECT_GPIOG_FUNC = 0x09,
FT260_SET_INTERRUPT_TRIGGER = 0x0A,
FT260_SET_SUSPEND_OUT_POLAR = 0x0B,
FT260_ENABLE_UART_RI_WAKEUP = 0x0C,
FT260_SET_UART_RI_WAKEUP_CFG = 0x0D,
FT260_SET_I2C_RESET = 0x20,
FT260_SET_I2C_CLOCK_SPEED = 0x22,
FT260_SET_UART_RESET = 0x40,
FT260_SET_UART_CONFIG = 0x41,
FT260_SET_UART_BAUD_RATE = 0x42,
FT260_SET_UART_DATA_BIT = 0x43,
FT260_SET_UART_PARITY = 0x44,
FT260_SET_UART_STOP_BIT = 0x45,
FT260_SET_UART_BREAKING = 0x46,
FT260_SET_UART_XON_XOFF = 0x49,
};
/* Response codes in I2C status report */
enum {
FT260_I2C_STATUS_SUCCESS = 0x00,
FT260_I2C_STATUS_CTRL_BUSY = 0x01,
FT260_I2C_STATUS_ERROR = 0x02,
FT260_I2C_STATUS_ADDR_NO_ACK = 0x04,
FT260_I2C_STATUS_DATA_NO_ACK = 0x08,
FT260_I2C_STATUS_ARBITR_LOST = 0x10,
FT260_I2C_STATUS_CTRL_IDLE = 0x20,
FT260_I2C_STATUS_BUS_BUSY = 0x40,
};
/* I2C Conditions flags */
enum {
FT260_FLAG_NONE = 0x00,
FT260_FLAG_START = 0x02,
FT260_FLAG_START_REPEATED = 0x03,
FT260_FLAG_STOP = 0x04,
FT260_FLAG_START_STOP = 0x06,
FT260_FLAG_START_STOP_REPEATED = 0x07,
};
#define FT260_SET_REQUEST_VALUE(report_id) ((FT260_FEATURE << 8) | report_id)
/* Feature In reports */
struct ft260_get_chip_version_report {
u8 report; /* FT260_CHIP_VERSION */
u8 chip_code[4]; /* FTDI chip identification code */
u8 reserved[8];
} __packed;
struct ft260_get_system_status_report {
u8 report; /* FT260_SYSTEM_SETTINGS */
u8 chip_mode; /* DCNF0 and DCNF1 status, bits 0-1 */
u8 clock_ctl; /* 0 - 12MHz, 1 - 24MHz, 2 - 48MHz */
u8 suspend_status; /* 0 - not suspended, 1 - suspended */
u8 pwren_status; /* 0 - FT260 is not ready, 1 - ready */
u8 i2c_enable; /* 0 - disabled, 1 - enabled */
u8 uart_mode; /* 0 - OFF; 1 - RTS_CTS, 2 - DTR_DSR, */
/* 3 - XON_XOFF, 4 - No flow control */
u8 hid_over_i2c_en; /* 0 - disabled, 1 - enabled */
u8 gpio2_function; /* 0 - GPIO, 1 - SUSPOUT, */
/* 2 - PWREN, 4 - TX_LED */
u8 gpioA_function; /* 0 - GPIO, 3 - TX_ACTIVE, 4 - TX_LED */
u8 gpioG_function; /* 0 - GPIO, 2 - PWREN, */
/* 5 - RX_LED, 6 - BCD_DET */
u8 suspend_out_pol; /* 0 - active-high, 1 - active-low */
u8 enable_wakeup_int; /* 0 - disabled, 1 - enabled */
u8 intr_cond; /* Interrupt trigger conditions */
u8 power_saving_en; /* 0 - disabled, 1 - enabled */
u8 reserved[10];
} __packed;
struct ft260_get_i2c_status_report {
u8 report; /* FT260_I2C_STATUS */
u8 bus_status; /* I2C bus status */
__le16 clock; /* I2C bus clock in range 60-3400 KHz */
u8 reserved;
} __packed;
/* Feature Out reports */
struct ft260_set_system_clock_report {
u8 report; /* FT260_SYSTEM_SETTINGS */
u8 request; /* FT260_SET_CLOCK */
u8 clock_ctl; /* 0 - 12MHz, 1 - 24MHz, 2 - 48MHz */
} __packed;
struct ft260_set_i2c_mode_report {
u8 report; /* FT260_SYSTEM_SETTINGS */
u8 request; /* FT260_SET_I2C_MODE */
u8 i2c_enable; /* 0 - disabled, 1 - enabled */
} __packed;
struct ft260_set_uart_mode_report {
u8 report; /* FT260_SYSTEM_SETTINGS */
u8 request; /* FT260_SET_UART_MODE */
u8 uart_mode; /* 0 - OFF; 1 - RTS_CTS, 2 - DTR_DSR, */
/* 3 - XON_XOFF, 4 - No flow control */
} __packed;
struct ft260_set_i2c_reset_report {
u8 report; /* FT260_SYSTEM_SETTINGS */
u8 request; /* FT260_SET_I2C_RESET */
} __packed;
struct ft260_set_i2c_speed_report {
u8 report; /* FT260_SYSTEM_SETTINGS */
u8 request; /* FT260_SET_I2C_CLOCK_SPEED */
__le16 clock; /* I2C bus clock in range 60-3400 KHz */
} __packed;
/* Data transfer reports */
struct ft260_i2c_write_request_report {
u8 report; /* FT260_I2C_REPORT */
u8 address; /* 7-bit I2C address */
u8 flag; /* I2C transaction condition */
u8 length; /* data payload length */
u8 data[FT260_WR_DATA_MAX]; /* data payload */
} __packed;
struct ft260_i2c_read_request_report {
u8 report; /* FT260_I2C_READ_REQ */
u8 address; /* 7-bit I2C address */
u8 flag; /* I2C transaction condition */
__le16 length; /* data payload length */
} __packed;
struct ft260_i2c_input_report {
u8 report; /* FT260_I2C_REPORT */
u8 length; /* data payload length */
u8 data[2]; /* data payload */
} __packed;
static const struct hid_device_id ft260_devices[] = {
{ HID_USB_DEVICE(USB_VENDOR_ID_FUTURE_TECHNOLOGY,
USB_DEVICE_ID_FT260) },
{ /* END OF LIST */ }
};
MODULE_DEVICE_TABLE(hid, ft260_devices);
struct ft260_device {
struct i2c_adapter adap;
struct hid_device *hdev;
struct completion wait;
struct mutex lock;
u8 write_buf[FT260_REPORT_MAX_LENGTH];
unsigned long need_wakeup_at;
u8 *read_buf;
u16 read_idx;
u16 read_len;
u16 clock;
};
static int ft260_hid_feature_report_get(struct hid_device *hdev,
unsigned char report_id, u8 *data,
size_t len)
{
u8 *buf;
int ret;
buf = kmalloc(len, GFP_KERNEL);
if (!buf)
return -ENOMEM;
ret = hid_hw_raw_request(hdev, report_id, buf, len, HID_FEATURE_REPORT,
HID_REQ_GET_REPORT);
if (likely(ret == len))
memcpy(data, buf, len);
else if (ret >= 0)
ret = -EIO;
kfree(buf);
return ret;
}
static int ft260_hid_feature_report_set(struct hid_device *hdev, u8 *data,
size_t len)
{
u8 *buf;
int ret;
buf = kmemdup(data, len, GFP_KERNEL);
if (!buf)
return -ENOMEM;
buf[0] = FT260_SYSTEM_SETTINGS;
ret = hid_hw_raw_request(hdev, buf[0], buf, len, HID_FEATURE_REPORT,
HID_REQ_SET_REPORT);
kfree(buf);
return ret;
}
static int ft260_i2c_reset(struct hid_device *hdev)
{
struct ft260_set_i2c_reset_report report;
int ret;
report.request = FT260_SET_I2C_RESET;
ret = ft260_hid_feature_report_set(hdev, (u8 *)&report, sizeof(report));
if (ret < 0) {
hid_err(hdev, "failed to reset I2C controller: %d\n", ret);
return ret;
}
ft260_dbg("done\n");
return ret;
}
static int ft260_xfer_status(struct ft260_device *dev, u8 bus_busy)
{
struct hid_device *hdev = dev->hdev;
struct ft260_get_i2c_status_report report;
int ret;
if (time_is_before_jiffies(dev->need_wakeup_at)) {
ret = ft260_hid_feature_report_get(hdev, FT260_I2C_STATUS,
(u8 *)&report, sizeof(report));
if (unlikely(ret < 0)) {
hid_err(hdev, "failed to retrieve status: %d, no wakeup\n",
ret);
} else {
dev->need_wakeup_at = jiffies +
msecs_to_jiffies(FT260_WAKEUP_NEEDED_AFTER_MS);
ft260_dbg("bus_status %#02x, wakeup\n",
report.bus_status);
}
}
ret = ft260_hid_feature_report_get(hdev, FT260_I2C_STATUS,
(u8 *)&report, sizeof(report));
if (unlikely(ret < 0)) {
hid_err(hdev, "failed to retrieve status: %d\n", ret);
return ret;
}
dev->clock = le16_to_cpu(report.clock);
ft260_dbg("bus_status %#02x, clock %u\n", report.bus_status,
dev->clock);
if (report.bus_status & (FT260_I2C_STATUS_CTRL_BUSY | bus_busy))
return -EAGAIN;
/*
* The error condition (bit 1) is a status bit reflecting any
* error conditions. When any of the bits 2, 3, or 4 are raised
* to 1, bit 1 is also set to 1.
*/
if (report.bus_status & FT260_I2C_STATUS_ERROR) {
hid_err(hdev, "i2c bus error: %#02x\n", report.bus_status);
return -EIO;
}
return 0;
}
static int ft260_hid_output_report(struct hid_device *hdev, u8 *data,
size_t len)
{
u8 *buf;
int ret;
buf = kmemdup(data, len, GFP_KERNEL);
if (!buf)
return -ENOMEM;
ret = hid_hw_output_report(hdev, buf, len);
kfree(buf);
return ret;
}
static int ft260_hid_output_report_check_status(struct ft260_device *dev,
u8 *data, int len)
{
u8 bus_busy;
int ret, usec, try = 100;
struct hid_device *hdev = dev->hdev;
struct ft260_i2c_write_request_report *rep =
(struct ft260_i2c_write_request_report *)data;
ret = ft260_hid_output_report(hdev, data, len);
if (ret < 0) {
hid_err(hdev, "%s: failed to start transfer, ret %d\n",
__func__, ret);
ft260_i2c_reset(hdev);
return ret;
}
/* transfer time = 1 / clock(KHz) * 9 bits * bytes */
usec = len * 9000 / dev->clock;
if (usec > 2000) {
usec -= 1500;
usleep_range(usec, usec + 100);
ft260_dbg("wait %d usec, len %d\n", usec, len);
}
/*
* Do not check the busy bit for combined transactions
* since the controller keeps the bus busy between writing
* and reading IOs to ensure an atomic operation.
*/
if (rep->flag == FT260_FLAG_START)
bus_busy = 0;
else
bus_busy = FT260_I2C_STATUS_BUS_BUSY;
do {
ret = ft260_xfer_status(dev, bus_busy);
if (ret != -EAGAIN)
break;
} while (--try);
if (ret == 0)
return 0;
ft260_i2c_reset(hdev);
return -EIO;
}
static int ft260_i2c_write(struct ft260_device *dev, u8 addr, u8 *data,
int len, u8 flag)
{
int ret, wr_len, idx = 0;
struct hid_device *hdev = dev->hdev;
struct ft260_i2c_write_request_report *rep =
(struct ft260_i2c_write_request_report *)dev->write_buf;
if (len < 1)
return -EINVAL;
rep->flag = FT260_FLAG_START;
do {
if (len <= FT260_WR_DATA_MAX) {
wr_len = len;
if (flag == FT260_FLAG_START_STOP)
rep->flag |= FT260_FLAG_STOP;
} else {
wr_len = FT260_WR_DATA_MAX;
}
rep->report = FT260_I2C_DATA_REPORT_ID(wr_len);
rep->address = addr;
rep->length = wr_len;
memcpy(rep->data, &data[idx], wr_len);
ft260_dbg("rep %#02x addr %#02x off %d len %d wlen %d flag %#x d[0] %#02x\n",
rep->report, addr, idx, len, wr_len,
rep->flag, data[0]);
ret = ft260_hid_output_report_check_status(dev, (u8 *)rep,
wr_len + 4);
if (ret < 0) {
hid_err(hdev, "%s: failed with %d\n", __func__, ret);
return ret;
}
len -= wr_len;
idx += wr_len;
rep->flag = 0;
} while (len > 0);
return 0;
}
static int ft260_smbus_write(struct ft260_device *dev, u8 addr, u8 cmd,
u8 *data, u8 data_len, u8 flag)
{
int ret = 0;
int len = 4;
struct ft260_i2c_write_request_report *rep =
(struct ft260_i2c_write_request_report *)dev->write_buf;
if (data_len >= sizeof(rep->data))
return -EINVAL;
rep->address = addr;
rep->data[0] = cmd;
rep->length = data_len + 1;
rep->flag = flag;
len += rep->length;
rep->report = FT260_I2C_DATA_REPORT_ID(len);
if (data_len > 0)
memcpy(&rep->data[1], data, data_len);
ft260_dbg("rep %#02x addr %#02x cmd %#02x datlen %d replen %d\n",
rep->report, addr, cmd, rep->length, len);
ret = ft260_hid_output_report_check_status(dev, (u8 *)rep, len);
return ret;
}
static int ft260_i2c_read(struct ft260_device *dev, u8 addr, u8 *data,
u16 len, u8 flag)
{
u16 rd_len;
u16 rd_data_max = 60;
int timeout, ret = 0;
struct ft260_i2c_read_request_report rep;
struct hid_device *hdev = dev->hdev;
u8 bus_busy = 0;
if ((flag & FT260_FLAG_START_REPEATED) == FT260_FLAG_START_REPEATED)
flag = FT260_FLAG_START_REPEATED;
else
flag = FT260_FLAG_START;
do {
if (len <= rd_data_max) {
rd_len = len;
flag |= FT260_FLAG_STOP;
} else {
rd_len = rd_data_max;
}
rd_data_max = FT260_RD_DATA_MAX;
rep.report = FT260_I2C_READ_REQ;
rep.length = cpu_to_le16(rd_len);
rep.address = addr;
rep.flag = flag;
ft260_dbg("rep %#02x addr %#02x len %d rlen %d flag %#x\n",
rep.report, rep.address, len, rd_len, flag);
reinit_completion(&dev->wait);
dev->read_idx = 0;
dev->read_buf = data;
dev->read_len = rd_len;
ret = ft260_hid_output_report(hdev, (u8 *)&rep, sizeof(rep));
if (ret < 0) {
hid_err(hdev, "%s: failed with %d\n", __func__, ret);
goto ft260_i2c_read_exit;
}
timeout = msecs_to_jiffies(5000);
if (!wait_for_completion_timeout(&dev->wait, timeout)) {
ret = -ETIMEDOUT;
ft260_i2c_reset(hdev);
goto ft260_i2c_read_exit;
}
dev->read_buf = NULL;
if (flag & FT260_FLAG_STOP)
bus_busy = FT260_I2C_STATUS_BUS_BUSY;
ret = ft260_xfer_status(dev, bus_busy);
if (ret < 0) {
ret = -EIO;
ft260_i2c_reset(hdev);
goto ft260_i2c_read_exit;
}
len -= rd_len;
data += rd_len;
flag = 0;
} while (len > 0);
ft260_i2c_read_exit:
dev->read_buf = NULL;
return ret;
}
/*
* A random read operation is implemented as a dummy write operation, followed
* by a current address read operation. The dummy write operation is used to
* load the target byte address into the current byte address counter, from
* which the subsequent current address read operation then reads.
*/
static int ft260_i2c_write_read(struct ft260_device *dev, struct i2c_msg *msgs)
{
int ret;
int wr_len = msgs[0].len;
int rd_len = msgs[1].len;
struct hid_device *hdev = dev->hdev;
u8 addr = msgs[0].addr;
u16 read_off = 0;
if (wr_len > 2) {
hid_err(hdev, "%s: invalid wr_len: %d\n", __func__, wr_len);
return -EOPNOTSUPP;
}
if (ft260_debug) {
if (wr_len == 2)
read_off = be16_to_cpu(*(__be16 *)msgs[0].buf);
else
read_off = *msgs[0].buf;
pr_info("%s: off %#x rlen %d wlen %d\n", __func__,
read_off, rd_len, wr_len);
}
ret = ft260_i2c_write(dev, addr, msgs[0].buf, wr_len,
FT260_FLAG_START);
if (ret < 0)
return ret;
ret = ft260_i2c_read(dev, addr, msgs[1].buf, rd_len,
FT260_FLAG_START_STOP_REPEATED);
if (ret < 0)
return ret;
return 0;
}
static int ft260_i2c_xfer(struct i2c_adapter *adapter, struct i2c_msg *msgs,
int num)
{
int ret;
struct ft260_device *dev = i2c_get_adapdata(adapter);
struct hid_device *hdev = dev->hdev;
mutex_lock(&dev->lock);
ret = hid_hw_power(hdev, PM_HINT_FULLON);
if (ret < 0) {
hid_err(hdev, "failed to enter FULLON power mode: %d\n", ret);
mutex_unlock(&dev->lock);
return ret;
}
if (num == 1) {
if (msgs->flags & I2C_M_RD)
ret = ft260_i2c_read(dev, msgs->addr, msgs->buf,
msgs->len, FT260_FLAG_START_STOP);
else
ret = ft260_i2c_write(dev, msgs->addr, msgs->buf,
msgs->len, FT260_FLAG_START_STOP);
if (ret < 0)
goto i2c_exit;
} else {
/* Combined write then read message */
ret = ft260_i2c_write_read(dev, msgs);
if (ret < 0)
goto i2c_exit;
}
ret = num;
i2c_exit:
hid_hw_power(hdev, PM_HINT_NORMAL);
mutex_unlock(&dev->lock);
return ret;
}
static int ft260_smbus_xfer(struct i2c_adapter *adapter, u16 addr, u16 flags,
char read_write, u8 cmd, int size,
union i2c_smbus_data *data)
{
int ret;
struct ft260_device *dev = i2c_get_adapdata(adapter);
struct hid_device *hdev = dev->hdev;
ft260_dbg("smbus size %d\n", size);
mutex_lock(&dev->lock);
ret = hid_hw_power(hdev, PM_HINT_FULLON);
if (ret < 0) {
hid_err(hdev, "power management error: %d\n", ret);
mutex_unlock(&dev->lock);
return ret;
}
switch (size) {
case I2C_SMBUS_BYTE:
if (read_write == I2C_SMBUS_READ)
ret = ft260_i2c_read(dev, addr, &data->byte, 1,
FT260_FLAG_START_STOP);
else
ret = ft260_smbus_write(dev, addr, cmd, NULL, 0,
FT260_FLAG_START_STOP);
break;
case I2C_SMBUS_BYTE_DATA:
if (read_write == I2C_SMBUS_READ) {
ret = ft260_smbus_write(dev, addr, cmd, NULL, 0,
FT260_FLAG_START);
if (ret)
goto smbus_exit;
ret = ft260_i2c_read(dev, addr, &data->byte, 1,
FT260_FLAG_START_STOP_REPEATED);
} else {
ret = ft260_smbus_write(dev, addr, cmd, &data->byte, 1,
FT260_FLAG_START_STOP);
}
break;
case I2C_SMBUS_WORD_DATA:
if (read_write == I2C_SMBUS_READ) {
ret = ft260_smbus_write(dev, addr, cmd, NULL, 0,
FT260_FLAG_START);
if (ret)
goto smbus_exit;
ret = ft260_i2c_read(dev, addr, (u8 *)&data->word, 2,
FT260_FLAG_START_STOP_REPEATED);
} else {
ret = ft260_smbus_write(dev, addr, cmd,
(u8 *)&data->word, 2,
FT260_FLAG_START_STOP);
}
break;
case I2C_SMBUS_BLOCK_DATA:
if (read_write == I2C_SMBUS_READ) {
ret = ft260_smbus_write(dev, addr, cmd, NULL, 0,
FT260_FLAG_START);
if (ret)
goto smbus_exit;
ret = ft260_i2c_read(dev, addr, data->block,
data->block[0] + 1,
FT260_FLAG_START_STOP_REPEATED);
} else {
ret = ft260_smbus_write(dev, addr, cmd, data->block,
data->block[0] + 1,
FT260_FLAG_START_STOP);
}
break;
case I2C_SMBUS_I2C_BLOCK_DATA:
if (read_write == I2C_SMBUS_READ) {
ret = ft260_smbus_write(dev, addr, cmd, NULL, 0,
FT260_FLAG_START);
if (ret)
goto smbus_exit;
ret = ft260_i2c_read(dev, addr, data->block + 1,
data->block[0],
FT260_FLAG_START_STOP_REPEATED);
} else {
ret = ft260_smbus_write(dev, addr, cmd, data->block + 1,
data->block[0],
FT260_FLAG_START_STOP);
}
break;
default:
hid_err(hdev, "unsupported smbus transaction size %d\n", size);
ret = -EOPNOTSUPP;
}
smbus_exit:
hid_hw_power(hdev, PM_HINT_NORMAL);
mutex_unlock(&dev->lock);
return ret;
}
static u32 ft260_functionality(struct i2c_adapter *adap)
{
return I2C_FUNC_I2C | I2C_FUNC_SMBUS_BYTE |
I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
I2C_FUNC_SMBUS_BLOCK_DATA | I2C_FUNC_SMBUS_I2C_BLOCK;
}
static const struct i2c_adapter_quirks ft260_i2c_quirks = {
.flags = I2C_AQ_COMB_WRITE_THEN_READ,
.max_comb_1st_msg_len = 2,
};
static const struct i2c_algorithm ft260_i2c_algo = {
.master_xfer = ft260_i2c_xfer,
.smbus_xfer = ft260_smbus_xfer,
.functionality = ft260_functionality,
};
static int ft260_get_system_config(struct hid_device *hdev,
struct ft260_get_system_status_report *cfg)
{
int ret;
int len = sizeof(struct ft260_get_system_status_report);
ret = ft260_hid_feature_report_get(hdev, FT260_SYSTEM_SETTINGS,
(u8 *)cfg, len);
if (ret < 0) {
hid_err(hdev, "failed to retrieve system status\n");
return ret;
}
return 0;
}
static int ft260_is_interface_enabled(struct hid_device *hdev)
{
struct ft260_get_system_status_report cfg;
struct usb_interface *usbif = to_usb_interface(hdev->dev.parent);
int interface = usbif->cur_altsetting->desc.bInterfaceNumber;
int ret;
ret = ft260_get_system_config(hdev, &cfg);
if (ret < 0)
return ret;
ft260_dbg("interface: 0x%02x\n", interface);
ft260_dbg("chip mode: 0x%02x\n", cfg.chip_mode);
ft260_dbg("clock_ctl: 0x%02x\n", cfg.clock_ctl);
ft260_dbg("i2c_enable: 0x%02x\n", cfg.i2c_enable);
ft260_dbg("uart_mode: 0x%02x\n", cfg.uart_mode);
switch (cfg.chip_mode) {
case FT260_MODE_ALL:
case FT260_MODE_BOTH:
if (interface == 1)
hid_info(hdev, "uart interface is not supported\n");
else
ret = 1;
break;
case FT260_MODE_UART:
hid_info(hdev, "uart interface is not supported\n");
break;
case FT260_MODE_I2C:
ret = 1;
break;
}
return ret;
}
static int ft260_byte_show(struct hid_device *hdev, int id, u8 *cfg, int len,
u8 *field, u8 *buf)
{
int ret;
ret = ft260_hid_feature_report_get(hdev, id, cfg, len);
if (ret < 0)
return ret;
return scnprintf(buf, PAGE_SIZE, "%d\n", *field);
}
static int ft260_word_show(struct hid_device *hdev, int id, u8 *cfg, int len,
__le16 *field, u8 *buf)
{
int ret;
ret = ft260_hid_feature_report_get(hdev, id, cfg, len);
if (ret < 0)
return ret;
return scnprintf(buf, PAGE_SIZE, "%d\n", le16_to_cpu(*field));
}
#define FT260_ATTR_SHOW(name, reptype, id, type, func) \
static ssize_t name##_show(struct device *kdev, \
struct device_attribute *attr, char *buf) \
{ \
struct reptype rep; \
struct hid_device *hdev = to_hid_device(kdev); \
type *field = &rep.name; \
int len = sizeof(rep); \
\
return func(hdev, id, (u8 *)&rep, len, field, buf); \
}
#define FT260_SSTAT_ATTR_SHOW(name) \
FT260_ATTR_SHOW(name, ft260_get_system_status_report, \
FT260_SYSTEM_SETTINGS, u8, ft260_byte_show)
#define FT260_I2CST_ATTR_SHOW(name) \
FT260_ATTR_SHOW(name, ft260_get_i2c_status_report, \
FT260_I2C_STATUS, __le16, ft260_word_show)
#define FT260_ATTR_STORE(name, reptype, id, req, type, ctype, func) \
static ssize_t name##_store(struct device *kdev, \
struct device_attribute *attr, \
const char *buf, size_t count) \
{ \
struct reptype rep; \
struct hid_device *hdev = to_hid_device(kdev); \
type name; \
int ret; \
\
if (!func(buf, 10, (ctype *)&name)) { \
rep.name = name; \
rep.report = id; \
rep.request = req; \
ret = ft260_hid_feature_report_set(hdev, (u8 *)&rep, \
sizeof(rep)); \
if (!ret) \
ret = count; \
} else { \
ret = -EINVAL; \
} \
return ret; \
}
#define FT260_BYTE_ATTR_STORE(name, reptype, req) \
FT260_ATTR_STORE(name, reptype, FT260_SYSTEM_SETTINGS, req, \
u8, u8, kstrtou8)
#define FT260_WORD_ATTR_STORE(name, reptype, req) \
FT260_ATTR_STORE(name, reptype, FT260_SYSTEM_SETTINGS, req, \
__le16, u16, kstrtou16)
FT260_SSTAT_ATTR_SHOW(chip_mode);
static DEVICE_ATTR_RO(chip_mode);
FT260_SSTAT_ATTR_SHOW(pwren_status);
static DEVICE_ATTR_RO(pwren_status);
FT260_SSTAT_ATTR_SHOW(suspend_status);
static DEVICE_ATTR_RO(suspend_status);
FT260_SSTAT_ATTR_SHOW(hid_over_i2c_en);
static DEVICE_ATTR_RO(hid_over_i2c_en);
FT260_SSTAT_ATTR_SHOW(power_saving_en);
static DEVICE_ATTR_RO(power_saving_en);
FT260_SSTAT_ATTR_SHOW(i2c_enable);
FT260_BYTE_ATTR_STORE(i2c_enable, ft260_set_i2c_mode_report,
FT260_SET_I2C_MODE);
static DEVICE_ATTR_RW(i2c_enable);
FT260_SSTAT_ATTR_SHOW(uart_mode);
FT260_BYTE_ATTR_STORE(uart_mode, ft260_set_uart_mode_report,
FT260_SET_UART_MODE);
static DEVICE_ATTR_RW(uart_mode);
FT260_SSTAT_ATTR_SHOW(clock_ctl);
FT260_BYTE_ATTR_STORE(clock_ctl, ft260_set_system_clock_report,
FT260_SET_CLOCK);
static DEVICE_ATTR_RW(clock_ctl);
FT260_I2CST_ATTR_SHOW(clock);
FT260_WORD_ATTR_STORE(clock, ft260_set_i2c_speed_report,
FT260_SET_I2C_CLOCK_SPEED);
static DEVICE_ATTR_RW(clock);
static ssize_t i2c_reset_store(struct device *kdev,
struct device_attribute *attr, const char *buf,
size_t count)
{
struct hid_device *hdev = to_hid_device(kdev);
int ret = ft260_i2c_reset(hdev);
if (ret)
return ret;
return count;
}
static DEVICE_ATTR_WO(i2c_reset);
static const struct attribute_group ft260_attr_group = {
.attrs = (struct attribute *[]) {
&dev_attr_chip_mode.attr,
&dev_attr_pwren_status.attr,
&dev_attr_suspend_status.attr,
&dev_attr_hid_over_i2c_en.attr,
&dev_attr_power_saving_en.attr,
&dev_attr_i2c_enable.attr,
&dev_attr_uart_mode.attr,
&dev_attr_clock_ctl.attr,
&dev_attr_i2c_reset.attr,
&dev_attr_clock.attr,
NULL
}
};
static int ft260_probe(struct hid_device *hdev, const struct hid_device_id *id)
{
struct ft260_device *dev;
struct ft260_get_chip_version_report version;
int ret;
if (!hid_is_usb(hdev))
return -EINVAL;
dev = devm_kzalloc(&hdev->dev, sizeof(*dev), GFP_KERNEL);
if (!dev)
return -ENOMEM;
ret = hid_parse(hdev);
if (ret) {
hid_err(hdev, "failed to parse HID\n");
return ret;
}
ret = hid_hw_start(hdev, 0);
if (ret) {
hid_err(hdev, "failed to start HID HW\n");
return ret;
}
ret = hid_hw_open(hdev);
if (ret) {
hid_err(hdev, "failed to open HID HW\n");
goto err_hid_stop;
}
ret = ft260_hid_feature_report_get(hdev, FT260_CHIP_VERSION,
(u8 *)&version, sizeof(version));
if (ret < 0) {
hid_err(hdev, "failed to retrieve chip version\n");
goto err_hid_close;
}
hid_info(hdev, "chip code: %02x%02x %02x%02x\n",
version.chip_code[0], version.chip_code[1],
version.chip_code[2], version.chip_code[3]);
ret = ft260_is_interface_enabled(hdev);
if (ret <= 0)
goto err_hid_close;
hid_info(hdev, "USB HID v%x.%02x Device [%s] on %s\n",
hdev->version >> 8, hdev->version & 0xff, hdev->name,
hdev->phys);
hid_set_drvdata(hdev, dev);
dev->hdev = hdev;
dev->adap.owner = THIS_MODULE;
dev->adap.class = I2C_CLASS_HWMON;
dev->adap.algo = &ft260_i2c_algo;
dev->adap.quirks = &ft260_i2c_quirks;
dev->adap.dev.parent = &hdev->dev;
snprintf(dev->adap.name, sizeof(dev->adap.name),
"FT260 usb-i2c bridge");
mutex_init(&dev->lock);
init_completion(&dev->wait);
ret = ft260_xfer_status(dev, FT260_I2C_STATUS_BUS_BUSY);
if (ret)
ft260_i2c_reset(hdev);
i2c_set_adapdata(&dev->adap, dev);
ret = i2c_add_adapter(&dev->adap);
if (ret) {
hid_err(hdev, "failed to add i2c adapter\n");
goto err_hid_close;
}
ret = sysfs_create_group(&hdev->dev.kobj, &ft260_attr_group);
if (ret < 0) {
hid_err(hdev, "failed to create sysfs attrs\n");
goto err_i2c_free;
}
return 0;
err_i2c_free:
i2c_del_adapter(&dev->adap);
err_hid_close:
hid_hw_close(hdev);
err_hid_stop:
hid_hw_stop(hdev);
return ret;
}
static void ft260_remove(struct hid_device *hdev)
{
struct ft260_device *dev = hid_get_drvdata(hdev);
if (!dev)
return;
sysfs_remove_group(&hdev->dev.kobj, &ft260_attr_group);
i2c_del_adapter(&dev->adap);
hid_hw_close(hdev);
hid_hw_stop(hdev);
}
static int ft260_raw_event(struct hid_device *hdev, struct hid_report *report,
u8 *data, int size)
{
struct ft260_device *dev = hid_get_drvdata(hdev);
struct ft260_i2c_input_report *xfer = (void *)data;
if (xfer->report >= FT260_I2C_REPORT_MIN &&
xfer->report <= FT260_I2C_REPORT_MAX) {
ft260_dbg("i2c resp: rep %#02x len %d\n", xfer->report,
xfer->length);
if ((dev->read_buf == NULL) ||
(xfer->length > dev->read_len - dev->read_idx)) {
hid_err(hdev, "unexpected report %#02x, length %d\n",
xfer->report, xfer->length);
return -1;
}
memcpy(&dev->read_buf[dev->read_idx], &xfer->data,
xfer->length);
dev->read_idx += xfer->length;
if (dev->read_idx == dev->read_len)
complete(&dev->wait);
} else {
hid_err(hdev, "unhandled report %#02x\n", xfer->report);
}
return 0;
}
static struct hid_driver ft260_driver = {
.name = "ft260",
.id_table = ft260_devices,
.probe = ft260_probe,
.remove = ft260_remove,
.raw_event = ft260_raw_event,
};
module_hid_driver(ft260_driver);
MODULE_DESCRIPTION("FTDI FT260 USB HID to I2C host bridge");
MODULE_AUTHOR("Michael Zaidman <michael.zaidman@gmail.com>");
MODULE_LICENSE("GPL v2");
]
|
| 35/4 |
2026/01/30 14:39 |
llm |
patch-generator |
0m
Model:
Tokens:
input: 20730
output: 49
thoughts: 1824
Thoughts: **Analyzing and Correcting the ft260_smbus_xfer Function**
Okay, I'm looking at the `ft260_smbus_xfer` function within the `hid-ft260.c` driver, specifically around line 719. My goal is to address the potential buffer overflow in the SMBus block data transfer. The bug report points out the need to validate the block length.
I can see the `I2C_SMBUS_BLOCK_DATA` and `I2C_SMBUS_I2C_BLOCK_DATA` cases within the `switch` statement. In these cases, when performing a write operation ( `read_write == I2C_SMBUS_WRITE` ), the code doesn't currently check the size of the block data being written. The reported issue is to validate the length against `I2C_SMBUS_BLOCK_MAX`, which I know is typically 32, and I'll confirm that definition in `include/uapi/linux/i2c.h` to be certain.
My initial thought was to simply add `if (data->block[0] > I2C_SMBUS_BLOCK_MAX) return -EINVAL;` directly before the `ft260_smbus_write` call. However, I need to be careful with that approach because `ft260_smbus_xfer` uses a mutex lock to protect shared resources, and I need to ensure the lock is released. Returning early like that would cause a lock leak, and also the power hint won't be cleared.
Therefore, I need to modify the code to set `ret = -EINVAL;` and then `goto smbus_exit;` to ensure the lock is released properly. This will allow the function to exit gracefully, unlock the mutex, and return the error.
I should also ensure that I understand the correct offsets and lengths being passed to the `ft260_smbus_write` function. Specifically, I need to double-check that the `data->block` pointer and the `data->block[0]` value is being used correctly, as the length needs to be validated.
In the case of `I2C_SMBUS_BLOCK_DATA`, I see that `data->block[0]` is the length, and the data buffer starts at `data->block`. So `data->block[0] + 1` bytes are written to the bus.
For `I2C_SMBUS_I2C_BLOCK_DATA`, the data buffer starts at `data->block + 1`, and the length is `data->block[0]`.
Now, let's verify if `ft260_smbus_write` is safe.
The signature is `static int ft260_smbus_write(struct ft260_device *dev, u8 addr, u8 cmd, u8 *data, u8 data_len, u8 flag)`.
Internally, there's a check `if (data_len >= sizeof(rep->data)) return -EINVAL;` inside `ft260_smbus_write` and also it is copied `memcpy(&rep->data[1], data, data_len);`.
I also need to verify the size of `data->block` and length being sent.
The `data->block` is of size 34 bytes (32 + 2), and `ft260_smbus_write` has its internal buffer of `FT260_WR_DATA_MAX` size of 60.
If `data->block[0]` is 32 (`I2C_SMBUS_BLOCK_MAX`), `data_len` is 33. We copy 33 bytes from `data->block`, which is within `data->block` size.
If `data->block[0]` is > 32, we should return error.
So, I'm confident in the proposed fix: within the write cases of `I2C_SMBUS_BLOCK_DATA` and `I2C_SMBUS_I2C_BLOCK_DATA`, I'll add a check `if (data->block[0] > I2C_SMBUS_BLOCK_MAX) { ret = -EINVAL; goto smbus_exit; }` to protect against buffer overflows and then return the error appropriately. This will validate the data length before making the `ft260_smbus_write` call, and will make sure that the mutex is always released.
|
| 36/4 |
2026/01/30 14:39 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:drivers/hid/hid-ft260.c IncludeLines:false Name:i2c_smbus_data]
Results: map[SourceCode:union i2c_smbus_data {
__u8 byte;
__u16 word;
__u8 block[I2C_SMBUS_BLOCK_MAX + 2]; /* block[0] is used for length */
/* and one more for user-space compatibility */
};
SourceFile:include/uapi/linux/i2c.h]
|
| 37/4 |
2026/01/30 14:39 |
llm |
patch-generator |
0m
Model:
Tokens:
input: 23703
output: 27
thoughts: 1094
Thoughts: **Analyzing the ft260 SMBus Block Data Vulnerability**
Okay, let's break this down. `I2C_SMBUS_BLOCK_MAX` is defined as 32, which means the `block` array is 34 bytes to include the length byte. I'm focusing on the `ft260_smbus_write` calls within the `I2C_SMBUS_BLOCK_DATA` and `I2C_SMBUS_I2C_BLOCK_DATA` cases.
In the `I2C_SMBUS_BLOCK_DATA` case, the code passes `data->block` and `data->block[0] + 1` to `ft260_smbus_write`. Essentially, `memcpy` will read `data->block[0] + 1` bytes from `data->block`. Since the max read size from `data->block` is 34 bytes, `data->block[0]` can be at most 33. The intent of `I2C_SMBUS_BLOCK_DATA` implies a length of at most 32. If `data->block[0]` is, say, 40, we're trying to read 41 bytes, which is a buffer overflow. Therefore, the check `data->block[0] > I2C_SMBUS_BLOCK_MAX` is correct to limit the size to 32. This maintains memory safety (32 + 1 <= 34) and adheres to the SMBus spec.
Now, let's look at `I2C_SMBUS_I2C_BLOCK_DATA`. Here, `data->block + 1` and `data->block[0]` are passed to `ft260_smbus_write`. This means `memcpy` reads `data->block[0]` bytes from `data->block + 1`. `data->block + 1` points to the 2nd byte of the `block` array. We have 33 bytes remaining that are valid (34 - 1). Therefore, `data->block[0]` must be <= 33. Limiting it to 32 is both safe (32 <= 33) and correct according to the SMBus specification, so this limit provides the same amount of safety and correct operation.
The crash report points to a read of size 42 at the `ft260_smbus_write` function, specifically at line 486 which is `memcpy(&rep->data[1], data, data_len);`. It's clear that the crash occurs in `I2C_SMBUS_I2C_BLOCK_DATA` because `data` is `data->block + 1` and `data_len` is `data->block[0]`. If `data->block[0]` is 42, we are reading 42 bytes from a buffer that's only 33 bytes in size. This confirms the out-of-bounds (OOB) read.
So, the fix is definitely to add a check against `I2C_SMBUS_BLOCK_MAX` (32) to `data->block[0]`. This will effectively bound the block length to what's safe. I'll also make sure to use `goto smbus_exit` instead of just `return -EINVAL` so we're sure to exit the SMBus operation cleanly.
I need to re-examine the `ft260_smbus_xfer` function to make sure I am inserting the fix in the right place, specifically in both `I2C_SMBUS_BLOCK_DATA` and `I2C_SMBUS_I2C_BLOCK_DATA` cases, and now I have to locate line 760 of the driver in order to verify the location of the fix.
|
| 38/4 |
2026/01/30 14:39 |
tool |
read-file |
0m
Args: map[File:drivers/hid/hid-ft260.c]
Results: map[Contents:// SPDX-License-Identifier: GPL-2.0-only
/*
* hid-ft260.c - FTDI FT260 USB HID to I2C host bridge
*
* Copyright (c) 2021, Michael Zaidman <michaelz@xsightlabs.com>
*
* Data Sheet:
* https://www.ftdichip.com/Support/Documents/DataSheets/ICs/DS_FT260.pdf
*/
#include "hid-ids.h"
#include <linux/hidraw.h>
#include <linux/i2c.h>
#include <linux/module.h>
#include <linux/usb.h>
#ifdef DEBUG
static int ft260_debug = 1;
#else
static int ft260_debug;
#endif
module_param_named(debug, ft260_debug, int, 0600);
MODULE_PARM_DESC(debug, "Toggle FT260 debugging messages");
#define ft260_dbg(format, arg...) \
do { \
if (ft260_debug) \
pr_info("%s: " format, __func__, ##arg); \
} while (0)
#define FT260_REPORT_MAX_LENGTH (64)
#define FT260_I2C_DATA_REPORT_ID(len) (FT260_I2C_REPORT_MIN + (len - 1) / 4)
#define FT260_WAKEUP_NEEDED_AFTER_MS (4800) /* 5s minus 200ms margin */
/*
* The ft260 input report format defines 62 bytes for the data payload, but
* when requested 62 bytes, the controller returns 60 and 2 in separate input
* reports. To achieve better performance with the multi-report read data
* transfers, we set the maximum read payload length to a multiple of 60.
* With a 100 kHz I2C clock, one 240 bytes read takes about 1/27 second,
* which is excessive; On the other hand, some higher layer drivers like at24
* or optoe limit the i2c reads to 128 bytes. To not block other drivers out
* of I2C for potentially troublesome amounts of time, we select the maximum
* read payload length to be 180 bytes.
*/
#define FT260_RD_DATA_MAX (180)
#define FT260_WR_DATA_MAX (60)
/*
* Device interface configuration.
* The FT260 has 2 interfaces that are controlled by DCNF0 and DCNF1 pins.
* First implementes USB HID to I2C bridge function and
* second - USB HID to UART bridge function.
*/
enum {
FT260_MODE_ALL = 0x00,
FT260_MODE_I2C = 0x01,
FT260_MODE_UART = 0x02,
FT260_MODE_BOTH = 0x03,
};
/* Control pipe */
enum {
FT260_GET_RQST_TYPE = 0xA1,
FT260_GET_REPORT = 0x01,
FT260_SET_RQST_TYPE = 0x21,
FT260_SET_REPORT = 0x09,
FT260_FEATURE = 0x03,
};
/* Report IDs / Feature In */
enum {
FT260_CHIP_VERSION = 0xA0,
FT260_SYSTEM_SETTINGS = 0xA1,
FT260_I2C_STATUS = 0xC0,
FT260_I2C_READ_REQ = 0xC2,
FT260_I2C_REPORT_MIN = 0xD0,
FT260_I2C_REPORT_MAX = 0xDE,
FT260_GPIO = 0xB0,
FT260_UART_INTERRUPT_STATUS = 0xB1,
FT260_UART_STATUS = 0xE0,
FT260_UART_RI_DCD_STATUS = 0xE1,
FT260_UART_REPORT = 0xF0,
};
/* Feature Out */
enum {
FT260_SET_CLOCK = 0x01,
FT260_SET_I2C_MODE = 0x02,
FT260_SET_UART_MODE = 0x03,
FT260_ENABLE_INTERRUPT = 0x05,
FT260_SELECT_GPIO2_FUNC = 0x06,
FT260_ENABLE_UART_DCD_RI = 0x07,
FT260_SELECT_GPIOA_FUNC = 0x08,
FT260_SELECT_GPIOG_FUNC = 0x09,
FT260_SET_INTERRUPT_TRIGGER = 0x0A,
FT260_SET_SUSPEND_OUT_POLAR = 0x0B,
FT260_ENABLE_UART_RI_WAKEUP = 0x0C,
FT260_SET_UART_RI_WAKEUP_CFG = 0x0D,
FT260_SET_I2C_RESET = 0x20,
FT260_SET_I2C_CLOCK_SPEED = 0x22,
FT260_SET_UART_RESET = 0x40,
FT260_SET_UART_CONFIG = 0x41,
FT260_SET_UART_BAUD_RATE = 0x42,
FT260_SET_UART_DATA_BIT = 0x43,
FT260_SET_UART_PARITY = 0x44,
FT260_SET_UART_STOP_BIT = 0x45,
FT260_SET_UART_BREAKING = 0x46,
FT260_SET_UART_XON_XOFF = 0x49,
};
/* Response codes in I2C status report */
enum {
FT260_I2C_STATUS_SUCCESS = 0x00,
FT260_I2C_STATUS_CTRL_BUSY = 0x01,
FT260_I2C_STATUS_ERROR = 0x02,
FT260_I2C_STATUS_ADDR_NO_ACK = 0x04,
FT260_I2C_STATUS_DATA_NO_ACK = 0x08,
FT260_I2C_STATUS_ARBITR_LOST = 0x10,
FT260_I2C_STATUS_CTRL_IDLE = 0x20,
FT260_I2C_STATUS_BUS_BUSY = 0x40,
};
/* I2C Conditions flags */
enum {
FT260_FLAG_NONE = 0x00,
FT260_FLAG_START = 0x02,
FT260_FLAG_START_REPEATED = 0x03,
FT260_FLAG_STOP = 0x04,
FT260_FLAG_START_STOP = 0x06,
FT260_FLAG_START_STOP_REPEATED = 0x07,
};
#define FT260_SET_REQUEST_VALUE(report_id) ((FT260_FEATURE << 8) | report_id)
/* Feature In reports */
struct ft260_get_chip_version_report {
u8 report; /* FT260_CHIP_VERSION */
u8 chip_code[4]; /* FTDI chip identification code */
u8 reserved[8];
} __packed;
struct ft260_get_system_status_report {
u8 report; /* FT260_SYSTEM_SETTINGS */
u8 chip_mode; /* DCNF0 and DCNF1 status, bits 0-1 */
u8 clock_ctl; /* 0 - 12MHz, 1 - 24MHz, 2 - 48MHz */
u8 suspend_status; /* 0 - not suspended, 1 - suspended */
u8 pwren_status; /* 0 - FT260 is not ready, 1 - ready */
u8 i2c_enable; /* 0 - disabled, 1 - enabled */
u8 uart_mode; /* 0 - OFF; 1 - RTS_CTS, 2 - DTR_DSR, */
/* 3 - XON_XOFF, 4 - No flow control */
u8 hid_over_i2c_en; /* 0 - disabled, 1 - enabled */
u8 gpio2_function; /* 0 - GPIO, 1 - SUSPOUT, */
/* 2 - PWREN, 4 - TX_LED */
u8 gpioA_function; /* 0 - GPIO, 3 - TX_ACTIVE, 4 - TX_LED */
u8 gpioG_function; /* 0 - GPIO, 2 - PWREN, */
/* 5 - RX_LED, 6 - BCD_DET */
u8 suspend_out_pol; /* 0 - active-high, 1 - active-low */
u8 enable_wakeup_int; /* 0 - disabled, 1 - enabled */
u8 intr_cond; /* Interrupt trigger conditions */
u8 power_saving_en; /* 0 - disabled, 1 - enabled */
u8 reserved[10];
} __packed;
struct ft260_get_i2c_status_report {
u8 report; /* FT260_I2C_STATUS */
u8 bus_status; /* I2C bus status */
__le16 clock; /* I2C bus clock in range 60-3400 KHz */
u8 reserved;
} __packed;
/* Feature Out reports */
struct ft260_set_system_clock_report {
u8 report; /* FT260_SYSTEM_SETTINGS */
u8 request; /* FT260_SET_CLOCK */
u8 clock_ctl; /* 0 - 12MHz, 1 - 24MHz, 2 - 48MHz */
} __packed;
struct ft260_set_i2c_mode_report {
u8 report; /* FT260_SYSTEM_SETTINGS */
u8 request; /* FT260_SET_I2C_MODE */
u8 i2c_enable; /* 0 - disabled, 1 - enabled */
} __packed;
struct ft260_set_uart_mode_report {
u8 report; /* FT260_SYSTEM_SETTINGS */
u8 request; /* FT260_SET_UART_MODE */
u8 uart_mode; /* 0 - OFF; 1 - RTS_CTS, 2 - DTR_DSR, */
/* 3 - XON_XOFF, 4 - No flow control */
} __packed;
struct ft260_set_i2c_reset_report {
u8 report; /* FT260_SYSTEM_SETTINGS */
u8 request; /* FT260_SET_I2C_RESET */
} __packed;
struct ft260_set_i2c_speed_report {
u8 report; /* FT260_SYSTEM_SETTINGS */
u8 request; /* FT260_SET_I2C_CLOCK_SPEED */
__le16 clock; /* I2C bus clock in range 60-3400 KHz */
} __packed;
/* Data transfer reports */
struct ft260_i2c_write_request_report {
u8 report; /* FT260_I2C_REPORT */
u8 address; /* 7-bit I2C address */
u8 flag; /* I2C transaction condition */
u8 length; /* data payload length */
u8 data[FT260_WR_DATA_MAX]; /* data payload */
} __packed;
struct ft260_i2c_read_request_report {
u8 report; /* FT260_I2C_READ_REQ */
u8 address; /* 7-bit I2C address */
u8 flag; /* I2C transaction condition */
__le16 length; /* data payload length */
} __packed;
struct ft260_i2c_input_report {
u8 report; /* FT260_I2C_REPORT */
u8 length; /* data payload length */
u8 data[2]; /* data payload */
} __packed;
static const struct hid_device_id ft260_devices[] = {
{ HID_USB_DEVICE(USB_VENDOR_ID_FUTURE_TECHNOLOGY,
USB_DEVICE_ID_FT260) },
{ /* END OF LIST */ }
};
MODULE_DEVICE_TABLE(hid, ft260_devices);
struct ft260_device {
struct i2c_adapter adap;
struct hid_device *hdev;
struct completion wait;
struct mutex lock;
u8 write_buf[FT260_REPORT_MAX_LENGTH];
unsigned long need_wakeup_at;
u8 *read_buf;
u16 read_idx;
u16 read_len;
u16 clock;
};
static int ft260_hid_feature_report_get(struct hid_device *hdev,
unsigned char report_id, u8 *data,
size_t len)
{
u8 *buf;
int ret;
buf = kmalloc(len, GFP_KERNEL);
if (!buf)
return -ENOMEM;
ret = hid_hw_raw_request(hdev, report_id, buf, len, HID_FEATURE_REPORT,
HID_REQ_GET_REPORT);
if (likely(ret == len))
memcpy(data, buf, len);
else if (ret >= 0)
ret = -EIO;
kfree(buf);
return ret;
}
static int ft260_hid_feature_report_set(struct hid_device *hdev, u8 *data,
size_t len)
{
u8 *buf;
int ret;
buf = kmemdup(data, len, GFP_KERNEL);
if (!buf)
return -ENOMEM;
buf[0] = FT260_SYSTEM_SETTINGS;
ret = hid_hw_raw_request(hdev, buf[0], buf, len, HID_FEATURE_REPORT,
HID_REQ_SET_REPORT);
kfree(buf);
return ret;
}
static int ft260_i2c_reset(struct hid_device *hdev)
{
struct ft260_set_i2c_reset_report report;
int ret;
report.request = FT260_SET_I2C_RESET;
ret = ft260_hid_feature_report_set(hdev, (u8 *)&report, sizeof(report));
if (ret < 0) {
hid_err(hdev, "failed to reset I2C controller: %d\n", ret);
return ret;
}
ft260_dbg("done\n");
return ret;
}
static int ft260_xfer_status(struct ft260_device *dev, u8 bus_busy)
{
struct hid_device *hdev = dev->hdev;
struct ft260_get_i2c_status_report report;
int ret;
if (time_is_before_jiffies(dev->need_wakeup_at)) {
ret = ft260_hid_feature_report_get(hdev, FT260_I2C_STATUS,
(u8 *)&report, sizeof(report));
if (unlikely(ret < 0)) {
hid_err(hdev, "failed to retrieve status: %d, no wakeup\n",
ret);
} else {
dev->need_wakeup_at = jiffies +
msecs_to_jiffies(FT260_WAKEUP_NEEDED_AFTER_MS);
ft260_dbg("bus_status %#02x, wakeup\n",
report.bus_status);
}
}
ret = ft260_hid_feature_report_get(hdev, FT260_I2C_STATUS,
(u8 *)&report, sizeof(report));
if (unlikely(ret < 0)) {
hid_err(hdev, "failed to retrieve status: %d\n", ret);
return ret;
}
dev->clock = le16_to_cpu(report.clock);
ft260_dbg("bus_status %#02x, clock %u\n", report.bus_status,
dev->clock);
if (report.bus_status & (FT260_I2C_STATUS_CTRL_BUSY | bus_busy))
return -EAGAIN;
/*
* The error condition (bit 1) is a status bit reflecting any
* error conditions. When any of the bits 2, 3, or 4 are raised
* to 1, bit 1 is also set to 1.
*/
if (report.bus_status & FT260_I2C_STATUS_ERROR) {
hid_err(hdev, "i2c bus error: %#02x\n", report.bus_status);
return -EIO;
}
return 0;
}
static int ft260_hid_output_report(struct hid_device *hdev, u8 *data,
size_t len)
{
u8 *buf;
int ret;
buf = kmemdup(data, len, GFP_KERNEL);
if (!buf)
return -ENOMEM;
ret = hid_hw_output_report(hdev, buf, len);
kfree(buf);
return ret;
}
static int ft260_hid_output_report_check_status(struct ft260_device *dev,
u8 *data, int len)
{
u8 bus_busy;
int ret, usec, try = 100;
struct hid_device *hdev = dev->hdev;
struct ft260_i2c_write_request_report *rep =
(struct ft260_i2c_write_request_report *)data;
ret = ft260_hid_output_report(hdev, data, len);
if (ret < 0) {
hid_err(hdev, "%s: failed to start transfer, ret %d\n",
__func__, ret);
ft260_i2c_reset(hdev);
return ret;
}
/* transfer time = 1 / clock(KHz) * 9 bits * bytes */
usec = len * 9000 / dev->clock;
if (usec > 2000) {
usec -= 1500;
usleep_range(usec, usec + 100);
ft260_dbg("wait %d usec, len %d\n", usec, len);
}
/*
* Do not check the busy bit for combined transactions
* since the controller keeps the bus busy between writing
* and reading IOs to ensure an atomic operation.
*/
if (rep->flag == FT260_FLAG_START)
bus_busy = 0;
else
bus_busy = FT260_I2C_STATUS_BUS_BUSY;
do {
ret = ft260_xfer_status(dev, bus_busy);
if (ret != -EAGAIN)
break;
} while (--try);
if (ret == 0)
return 0;
ft260_i2c_reset(hdev);
return -EIO;
}
static int ft260_i2c_write(struct ft260_device *dev, u8 addr, u8 *data,
int len, u8 flag)
{
int ret, wr_len, idx = 0;
struct hid_device *hdev = dev->hdev;
struct ft260_i2c_write_request_report *rep =
(struct ft260_i2c_write_request_report *)dev->write_buf;
if (len < 1)
return -EINVAL;
rep->flag = FT260_FLAG_START;
do {
if (len <= FT260_WR_DATA_MAX) {
wr_len = len;
if (flag == FT260_FLAG_START_STOP)
rep->flag |= FT260_FLAG_STOP;
} else {
wr_len = FT260_WR_DATA_MAX;
}
rep->report = FT260_I2C_DATA_REPORT_ID(wr_len);
rep->address = addr;
rep->length = wr_len;
memcpy(rep->data, &data[idx], wr_len);
ft260_dbg("rep %#02x addr %#02x off %d len %d wlen %d flag %#x d[0] %#02x\n",
rep->report, addr, idx, len, wr_len,
rep->flag, data[0]);
ret = ft260_hid_output_report_check_status(dev, (u8 *)rep,
wr_len + 4);
if (ret < 0) {
hid_err(hdev, "%s: failed with %d\n", __func__, ret);
return ret;
}
len -= wr_len;
idx += wr_len;
rep->flag = 0;
} while (len > 0);
return 0;
}
static int ft260_smbus_write(struct ft260_device *dev, u8 addr, u8 cmd,
u8 *data, u8 data_len, u8 flag)
{
int ret = 0;
int len = 4;
struct ft260_i2c_write_request_report *rep =
(struct ft260_i2c_write_request_report *)dev->write_buf;
if (data_len >= sizeof(rep->data))
return -EINVAL;
rep->address = addr;
rep->data[0] = cmd;
rep->length = data_len + 1;
rep->flag = flag;
len += rep->length;
rep->report = FT260_I2C_DATA_REPORT_ID(len);
if (data_len > 0)
memcpy(&rep->data[1], data, data_len);
ft260_dbg("rep %#02x addr %#02x cmd %#02x datlen %d replen %d\n",
rep->report, addr, cmd, rep->length, len);
ret = ft260_hid_output_report_check_status(dev, (u8 *)rep, len);
return ret;
}
static int ft260_i2c_read(struct ft260_device *dev, u8 addr, u8 *data,
u16 len, u8 flag)
{
u16 rd_len;
u16 rd_data_max = 60;
int timeout, ret = 0;
struct ft260_i2c_read_request_report rep;
struct hid_device *hdev = dev->hdev;
u8 bus_busy = 0;
if ((flag & FT260_FLAG_START_REPEATED) == FT260_FLAG_START_REPEATED)
flag = FT260_FLAG_START_REPEATED;
else
flag = FT260_FLAG_START;
do {
if (len <= rd_data_max) {
rd_len = len;
flag |= FT260_FLAG_STOP;
} else {
rd_len = rd_data_max;
}
rd_data_max = FT260_RD_DATA_MAX;
rep.report = FT260_I2C_READ_REQ;
rep.length = cpu_to_le16(rd_len);
rep.address = addr;
rep.flag = flag;
ft260_dbg("rep %#02x addr %#02x len %d rlen %d flag %#x\n",
rep.report, rep.address, len, rd_len, flag);
reinit_completion(&dev->wait);
dev->read_idx = 0;
dev->read_buf = data;
dev->read_len = rd_len;
ret = ft260_hid_output_report(hdev, (u8 *)&rep, sizeof(rep));
if (ret < 0) {
hid_err(hdev, "%s: failed with %d\n", __func__, ret);
goto ft260_i2c_read_exit;
}
timeout = msecs_to_jiffies(5000);
if (!wait_for_completion_timeout(&dev->wait, timeout)) {
ret = -ETIMEDOUT;
ft260_i2c_reset(hdev);
goto ft260_i2c_read_exit;
}
dev->read_buf = NULL;
if (flag & FT260_FLAG_STOP)
bus_busy = FT260_I2C_STATUS_BUS_BUSY;
ret = ft260_xfer_status(dev, bus_busy);
if (ret < 0) {
ret = -EIO;
ft260_i2c_reset(hdev);
goto ft260_i2c_read_exit;
}
len -= rd_len;
data += rd_len;
flag = 0;
} while (len > 0);
ft260_i2c_read_exit:
dev->read_buf = NULL;
return ret;
}
/*
* A random read operation is implemented as a dummy write operation, followed
* by a current address read operation. The dummy write operation is used to
* load the target byte address into the current byte address counter, from
* which the subsequent current address read operation then reads.
*/
static int ft260_i2c_write_read(struct ft260_device *dev, struct i2c_msg *msgs)
{
int ret;
int wr_len = msgs[0].len;
int rd_len = msgs[1].len;
struct hid_device *hdev = dev->hdev;
u8 addr = msgs[0].addr;
u16 read_off = 0;
if (wr_len > 2) {
hid_err(hdev, "%s: invalid wr_len: %d\n", __func__, wr_len);
return -EOPNOTSUPP;
}
if (ft260_debug) {
if (wr_len == 2)
read_off = be16_to_cpu(*(__be16 *)msgs[0].buf);
else
read_off = *msgs[0].buf;
pr_info("%s: off %#x rlen %d wlen %d\n", __func__,
read_off, rd_len, wr_len);
}
ret = ft260_i2c_write(dev, addr, msgs[0].buf, wr_len,
FT260_FLAG_START);
if (ret < 0)
return ret;
ret = ft260_i2c_read(dev, addr, msgs[1].buf, rd_len,
FT260_FLAG_START_STOP_REPEATED);
if (ret < 0)
return ret;
return 0;
}
static int ft260_i2c_xfer(struct i2c_adapter *adapter, struct i2c_msg *msgs,
int num)
{
int ret;
struct ft260_device *dev = i2c_get_adapdata(adapter);
struct hid_device *hdev = dev->hdev;
mutex_lock(&dev->lock);
ret = hid_hw_power(hdev, PM_HINT_FULLON);
if (ret < 0) {
hid_err(hdev, "failed to enter FULLON power mode: %d\n", ret);
mutex_unlock(&dev->lock);
return ret;
}
if (num == 1) {
if (msgs->flags & I2C_M_RD)
ret = ft260_i2c_read(dev, msgs->addr, msgs->buf,
msgs->len, FT260_FLAG_START_STOP);
else
ret = ft260_i2c_write(dev, msgs->addr, msgs->buf,
msgs->len, FT260_FLAG_START_STOP);
if (ret < 0)
goto i2c_exit;
} else {
/* Combined write then read message */
ret = ft260_i2c_write_read(dev, msgs);
if (ret < 0)
goto i2c_exit;
}
ret = num;
i2c_exit:
hid_hw_power(hdev, PM_HINT_NORMAL);
mutex_unlock(&dev->lock);
return ret;
}
static int ft260_smbus_xfer(struct i2c_adapter *adapter, u16 addr, u16 flags,
char read_write, u8 cmd, int size,
union i2c_smbus_data *data)
{
int ret;
struct ft260_device *dev = i2c_get_adapdata(adapter);
struct hid_device *hdev = dev->hdev;
ft260_dbg("smbus size %d\n", size);
mutex_lock(&dev->lock);
ret = hid_hw_power(hdev, PM_HINT_FULLON);
if (ret < 0) {
hid_err(hdev, "power management error: %d\n", ret);
mutex_unlock(&dev->lock);
return ret;
}
switch (size) {
case I2C_SMBUS_BYTE:
if (read_write == I2C_SMBUS_READ)
ret = ft260_i2c_read(dev, addr, &data->byte, 1,
FT260_FLAG_START_STOP);
else
ret = ft260_smbus_write(dev, addr, cmd, NULL, 0,
FT260_FLAG_START_STOP);
break;
case I2C_SMBUS_BYTE_DATA:
if (read_write == I2C_SMBUS_READ) {
ret = ft260_smbus_write(dev, addr, cmd, NULL, 0,
FT260_FLAG_START);
if (ret)
goto smbus_exit;
ret = ft260_i2c_read(dev, addr, &data->byte, 1,
FT260_FLAG_START_STOP_REPEATED);
} else {
ret = ft260_smbus_write(dev, addr, cmd, &data->byte, 1,
FT260_FLAG_START_STOP);
}
break;
case I2C_SMBUS_WORD_DATA:
if (read_write == I2C_SMBUS_READ) {
ret = ft260_smbus_write(dev, addr, cmd, NULL, 0,
FT260_FLAG_START);
if (ret)
goto smbus_exit;
ret = ft260_i2c_read(dev, addr, (u8 *)&data->word, 2,
FT260_FLAG_START_STOP_REPEATED);
} else {
ret = ft260_smbus_write(dev, addr, cmd,
(u8 *)&data->word, 2,
FT260_FLAG_START_STOP);
}
break;
case I2C_SMBUS_BLOCK_DATA:
if (read_write == I2C_SMBUS_READ) {
ret = ft260_smbus_write(dev, addr, cmd, NULL, 0,
FT260_FLAG_START);
if (ret)
goto smbus_exit;
ret = ft260_i2c_read(dev, addr, data->block,
data->block[0] + 1,
FT260_FLAG_START_STOP_REPEATED);
} else {
ret = ft260_smbus_write(dev, addr, cmd, data->block,
data->block[0] + 1,
FT260_FLAG_START_STOP);
}
break;
case I2C_SMBUS_I2C_BLOCK_DATA:
if (read_write == I2C_SMBUS_READ) {
ret = ft260_smbus_write(dev, addr, cmd, NULL, 0,
FT260_FLAG_START);
if (ret)
goto smbus_exit;
ret = ft260_i2c_read(dev, addr, data->block + 1,
data->block[0],
FT260_FLAG_START_STOP_REPEATED);
} else {
ret = ft260_smbus_write(dev, addr, cmd, data->block + 1,
data->block[0],
FT260_FLAG_START_STOP);
}
break;
default:
hid_err(hdev, "unsupported smbus transaction size %d\n", size);
ret = -EOPNOTSUPP;
}
smbus_exit:
hid_hw_power(hdev, PM_HINT_NORMAL);
mutex_unlock(&dev->lock);
return ret;
}
static u32 ft260_functionality(struct i2c_adapter *adap)
{
return I2C_FUNC_I2C | I2C_FUNC_SMBUS_BYTE |
I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
I2C_FUNC_SMBUS_BLOCK_DATA | I2C_FUNC_SMBUS_I2C_BLOCK;
}
static const struct i2c_adapter_quirks ft260_i2c_quirks = {
.flags = I2C_AQ_COMB_WRITE_THEN_READ,
.max_comb_1st_msg_len = 2,
};
static const struct i2c_algorithm ft260_i2c_algo = {
.master_xfer = ft260_i2c_xfer,
.smbus_xfer = ft260_smbus_xfer,
.functionality = ft260_functionality,
};
static int ft260_get_system_config(struct hid_device *hdev,
struct ft260_get_system_status_report *cfg)
{
int ret;
int len = sizeof(struct ft260_get_system_status_report);
ret = ft260_hid_feature_report_get(hdev, FT260_SYSTEM_SETTINGS,
(u8 *)cfg, len);
if (ret < 0) {
hid_err(hdev, "failed to retrieve system status\n");
return ret;
}
return 0;
}
static int ft260_is_interface_enabled(struct hid_device *hdev)
{
struct ft260_get_system_status_report cfg;
struct usb_interface *usbif = to_usb_interface(hdev->dev.parent);
int interface = usbif->cur_altsetting->desc.bInterfaceNumber;
int ret;
ret = ft260_get_system_config(hdev, &cfg);
if (ret < 0)
return ret;
ft260_dbg("interface: 0x%02x\n", interface);
ft260_dbg("chip mode: 0x%02x\n", cfg.chip_mode);
ft260_dbg("clock_ctl: 0x%02x\n", cfg.clock_ctl);
ft260_dbg("i2c_enable: 0x%02x\n", cfg.i2c_enable);
ft260_dbg("uart_mode: 0x%02x\n", cfg.uart_mode);
switch (cfg.chip_mode) {
case FT260_MODE_ALL:
case FT260_MODE_BOTH:
if (interface == 1)
hid_info(hdev, "uart interface is not supported\n");
else
ret = 1;
break;
case FT260_MODE_UART:
hid_info(hdev, "uart interface is not supported\n");
break;
case FT260_MODE_I2C:
ret = 1;
break;
}
return ret;
}
static int ft260_byte_show(struct hid_device *hdev, int id, u8 *cfg, int len,
u8 *field, u8 *buf)
{
int ret;
ret = ft260_hid_feature_report_get(hdev, id, cfg, len);
if (ret < 0)
return ret;
return scnprintf(buf, PAGE_SIZE, "%d\n", *field);
}
static int ft260_word_show(struct hid_device *hdev, int id, u8 *cfg, int len,
__le16 *field, u8 *buf)
{
int ret;
ret = ft260_hid_feature_report_get(hdev, id, cfg, len);
if (ret < 0)
return ret;
return scnprintf(buf, PAGE_SIZE, "%d\n", le16_to_cpu(*field));
}
#define FT260_ATTR_SHOW(name, reptype, id, type, func) \
static ssize_t name##_show(struct device *kdev, \
struct device_attribute *attr, char *buf) \
{ \
struct reptype rep; \
struct hid_device *hdev = to_hid_device(kdev); \
type *field = &rep.name; \
int len = sizeof(rep); \
\
return func(hdev, id, (u8 *)&rep, len, field, buf); \
}
#define FT260_SSTAT_ATTR_SHOW(name) \
FT260_ATTR_SHOW(name, ft260_get_system_status_report, \
FT260_SYSTEM_SETTINGS, u8, ft260_byte_show)
#define FT260_I2CST_ATTR_SHOW(name) \
FT260_ATTR_SHOW(name, ft260_get_i2c_status_report, \
FT260_I2C_STATUS, __le16, ft260_word_show)
#define FT260_ATTR_STORE(name, reptype, id, req, type, ctype, func) \
static ssize_t name##_store(struct device *kdev, \
struct device_attribute *attr, \
const char *buf, size_t count) \
{ \
struct reptype rep; \
struct hid_device *hdev = to_hid_device(kdev); \
type name; \
int ret; \
\
if (!func(buf, 10, (ctype *)&name)) { \
rep.name = name; \
rep.report = id; \
rep.request = req; \
ret = ft260_hid_feature_report_set(hdev, (u8 *)&rep, \
sizeof(rep)); \
if (!ret) \
ret = count; \
} else { \
ret = -EINVAL; \
} \
return ret; \
}
#define FT260_BYTE_ATTR_STORE(name, reptype, req) \
FT260_ATTR_STORE(name, reptype, FT260_SYSTEM_SETTINGS, req, \
u8, u8, kstrtou8)
#define FT260_WORD_ATTR_STORE(name, reptype, req) \
FT260_ATTR_STORE(name, reptype, FT260_SYSTEM_SETTINGS, req, \
__le16, u16, kstrtou16)
FT260_SSTAT_ATTR_SHOW(chip_mode);
static DEVICE_ATTR_RO(chip_mode);
FT260_SSTAT_ATTR_SHOW(pwren_status);
static DEVICE_ATTR_RO(pwren_status);
FT260_SSTAT_ATTR_SHOW(suspend_status);
static DEVICE_ATTR_RO(suspend_status);
FT260_SSTAT_ATTR_SHOW(hid_over_i2c_en);
static DEVICE_ATTR_RO(hid_over_i2c_en);
FT260_SSTAT_ATTR_SHOW(power_saving_en);
static DEVICE_ATTR_RO(power_saving_en);
FT260_SSTAT_ATTR_SHOW(i2c_enable);
FT260_BYTE_ATTR_STORE(i2c_enable, ft260_set_i2c_mode_report,
FT260_SET_I2C_MODE);
static DEVICE_ATTR_RW(i2c_enable);
FT260_SSTAT_ATTR_SHOW(uart_mode);
FT260_BYTE_ATTR_STORE(uart_mode, ft260_set_uart_mode_report,
FT260_SET_UART_MODE);
static DEVICE_ATTR_RW(uart_mode);
FT260_SSTAT_ATTR_SHOW(clock_ctl);
FT260_BYTE_ATTR_STORE(clock_ctl, ft260_set_system_clock_report,
FT260_SET_CLOCK);
static DEVICE_ATTR_RW(clock_ctl);
FT260_I2CST_ATTR_SHOW(clock);
FT260_WORD_ATTR_STORE(clock, ft260_set_i2c_speed_report,
FT260_SET_I2C_CLOCK_SPEED);
static DEVICE_ATTR_RW(clock);
static ssize_t i2c_reset_store(struct device *kdev,
struct device_attribute *attr, const char *buf,
size_t count)
{
struct hid_device *hdev = to_hid_device(kdev);
int ret = ft260_i2c_reset(hdev);
if (ret)
return ret;
return count;
}
static DEVICE_ATTR_WO(i2c_reset);
static const struct attribute_group ft260_attr_group = {
.attrs = (struct attribute *[]) {
&dev_attr_chip_mode.attr,
&dev_attr_pwren_status.attr,
&dev_attr_suspend_status.attr,
&dev_attr_hid_over_i2c_en.attr,
&dev_attr_power_saving_en.attr,
&dev_attr_i2c_enable.attr,
&dev_attr_uart_mode.attr,
&dev_attr_clock_ctl.attr,
&dev_attr_i2c_reset.attr,
&dev_attr_clock.attr,
NULL
}
};
static int ft260_probe(struct hid_device *hdev, const struct hid_device_id *id)
{
struct ft260_device *dev;
struct ft260_get_chip_version_report version;
int ret;
if (!hid_is_usb(hdev))
return -EINVAL;
dev = devm_kzalloc(&hdev->dev, sizeof(*dev), GFP_KERNEL);
if (!dev)
return -ENOMEM;
ret = hid_parse(hdev);
if (ret) {
hid_err(hdev, "failed to parse HID\n");
return ret;
}
ret = hid_hw_start(hdev, 0);
if (ret) {
hid_err(hdev, "failed to start HID HW\n");
return ret;
}
ret = hid_hw_open(hdev);
if (ret) {
hid_err(hdev, "failed to open HID HW\n");
goto err_hid_stop;
}
ret = ft260_hid_feature_report_get(hdev, FT260_CHIP_VERSION,
(u8 *)&version, sizeof(version));
if (ret < 0) {
hid_err(hdev, "failed to retrieve chip version\n");
goto err_hid_close;
}
hid_info(hdev, "chip code: %02x%02x %02x%02x\n",
version.chip_code[0], version.chip_code[1],
version.chip_code[2], version.chip_code[3]);
ret = ft260_is_interface_enabled(hdev);
if (ret <= 0)
goto err_hid_close;
hid_info(hdev, "USB HID v%x.%02x Device [%s] on %s\n",
hdev->version >> 8, hdev->version & 0xff, hdev->name,
hdev->phys);
hid_set_drvdata(hdev, dev);
dev->hdev = hdev;
dev->adap.owner = THIS_MODULE;
dev->adap.class = I2C_CLASS_HWMON;
dev->adap.algo = &ft260_i2c_algo;
dev->adap.quirks = &ft260_i2c_quirks;
dev->adap.dev.parent = &hdev->dev;
snprintf(dev->adap.name, sizeof(dev->adap.name),
"FT260 usb-i2c bridge");
mutex_init(&dev->lock);
init_completion(&dev->wait);
ret = ft260_xfer_status(dev, FT260_I2C_STATUS_BUS_BUSY);
if (ret)
ft260_i2c_reset(hdev);
i2c_set_adapdata(&dev->adap, dev);
ret = i2c_add_adapter(&dev->adap);
if (ret) {
hid_err(hdev, "failed to add i2c adapter\n");
goto err_hid_close;
}
ret = sysfs_create_group(&hdev->dev.kobj, &ft260_attr_group);
if (ret < 0) {
hid_err(hdev, "failed to create sysfs attrs\n");
goto err_i2c_free;
}
return 0;
err_i2c_free:
i2c_del_adapter(&dev->adap);
err_hid_close:
hid_hw_close(hdev);
err_hid_stop:
hid_hw_stop(hdev);
return ret;
}
static void ft260_remove(struct hid_device *hdev)
{
struct ft260_device *dev = hid_get_drvdata(hdev);
if (!dev)
return;
sysfs_remove_group(&hdev->dev.kobj, &ft260_attr_group);
i2c_del_adapter(&dev->adap);
hid_hw_close(hdev);
hid_hw_stop(hdev);
}
static int ft260_raw_event(struct hid_device *hdev, struct hid_report *report,
u8 *data, int size)
{
struct ft260_device *dev = hid_get_drvdata(hdev);
struct ft260_i2c_input_report *xfer = (void *)data;
if (xfer->report >= FT260_I2C_REPORT_MIN &&
xfer->report <= FT260_I2C_REPORT_MAX) {
ft260_dbg("i2c resp: rep %#02x len %d\n", xfer->report,
xfer->length);
if ((dev->read_buf == NULL) ||
(xfer->length > dev->read_len - dev->read_idx)) {
hid_err(hdev, "unexpected report %#02x, length %d\n",
xfer->report, xfer->length);
return -1;
}
memcpy(&dev->read_buf[dev->read_idx], &xfer->data,
xfer->length);
dev->read_idx += xfer->length;
if (dev->read_idx == dev->read_len)
complete(&dev->wait);
} else {
hid_err(hdev, "unhandled report %#02x\n", xfer->report);
}
return 0;
}
static struct hid_driver ft260_driver = {
.name = "ft260",
.id_table = ft260_devices,
.probe = ft260_probe,
.remove = ft260_remove,
.raw_event = ft260_raw_event,
};
module_hid_driver(ft260_driver);
MODULE_DESCRIPTION("FTDI FT260 USB HID to I2C host bridge");
MODULE_AUTHOR("Michael Zaidman <michael.zaidman@gmail.com>");
MODULE_LICENSE("GPL v2");
]
|
| 39/4 |
2026/01/30 14:39 |
llm |
patch-generator |
0m
Model:
Tokens:
input: 38522
output: 913
thoughts: 1134
Thoughts: **Analyzing and Implementing a Fix for an I2C SMBus Block Data Vulnerability**
Okay, so I've been looking at this I2C SMBus block data issue, and I've zeroed in on the potential for a buffer overflow. Specifically, the vulnerability lies in the handling of the `I2C_SMBUS_BLOCK_DATA` and `I2C_SMBUS_I2C_BLOCK_DATA` cases.
The core of the problem stems from the length byte, `data->block[0]`. When writing block data, the `ft260_smbus_write` function uses this length to determine how much data to copy. `I2C_SMBUS_BLOCK_MAX` is defined as 32.
Let's break down the `I2C_SMBUS_BLOCK_DATA` case. If `data->block[0]` (the length) is greater than 32, we're passing a length value to `ft260_smbus_write` that will lead to a copy exceeding the allocated buffer size. Even if it is 33, we are passing 34 bytes to `ft260_smbus_write`, but the buffer `data->block` is 34 bytes long. If it is 34, then we pass 35 bytes. This is where the overflow happens. The source buffer `data->block` is 34 bytes. A check for values greater than 32 addresses this overflow directly.
Now, considering `I2C_SMBUS_I2C_BLOCK_DATA`, we pass `data->block + 1` to `ft260_smbus_write`. The copy length is from `data->block[0]`. If `data->block[0]` is > 32, such as 33, it's safe to copy, as `data->block + 1` has a size of 33 bytes and a total length of 34 bytes. If it's 34, however, we attempt to copy 34 bytes from an object with only 33 bytes. This will overflow. So, the strict check for `data->block[0] > I2C_SMBUS_BLOCK_MAX` is correct in both cases, preventing the unsafe `memcpy` calls.
The bug report aligns perfectly with my analysis: the `ft260_smbus_write` function is where `memcpy` is being called, and the stack frame indicates that the `temp` variable within `i2cdev_ioctl_smbus` is the source of the data, which I interpreted as `union i2c_smbus_data`. So, I'm confident in my understanding of the root cause.
Therefore, the fix is straightforward: adding checks within the `I2C_SMBUS_BLOCK_DATA` and `I2C_SMBUS_I2C_BLOCK_DATA` cases to ensure that `data->block[0]` does not exceed `I2C_SMBUS_BLOCK_MAX`. I'll implement the necessary check:
```c
if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
ret = -EINVAL;
goto smbus_exit;
}
```
I'll verify that `linux/i2c.h` is included, and then I'll use `codeeditor` to apply these edits.
|
| 40/4 |
2026/01/30 14:40 |
tool |
codeeditor |
0m
Args: map[CurrentCode: case I2C_SMBUS_BLOCK_DATA:
if (read_write == I2C_SMBUS_READ) {
ret = ft260_smbus_write(dev, addr, cmd, NULL, 0,
FT260_FLAG_START);
if (ret)
goto smbus_exit;
ret = ft260_i2c_read(dev, addr, data->block,
data->block[0] + 1,
FT260_FLAG_START_STOP_REPEATED);
} else {
ret = ft260_smbus_write(dev, addr, cmd, data->block,
data->block[0] + 1,
FT260_FLAG_START_STOP);
}
break;
case I2C_SMBUS_I2C_BLOCK_DATA:
if (read_write == I2C_SMBUS_READ) {
ret = ft260_smbus_write(dev, addr, cmd, NULL, 0,
FT260_FLAG_START);
if (ret)
goto smbus_exit;
ret = ft260_i2c_read(dev, addr, data->block + 1,
data->block[0],
FT260_FLAG_START_STOP_REPEATED);
} else {
ret = ft260_smbus_write(dev, addr, cmd, data->block + 1,
data->block[0],
FT260_FLAG_START_STOP);
}
break; NewCode: case I2C_SMBUS_BLOCK_DATA:
if (read_write == I2C_SMBUS_READ) {
ret = ft260_smbus_write(dev, addr, cmd, NULL, 0,
FT260_FLAG_START);
if (ret)
goto smbus_exit;
ret = ft260_i2c_read(dev, addr, data->block,
data->block[0] + 1,
FT260_FLAG_START_STOP_REPEATED);
} else {
if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
ret = -EINVAL;
goto smbus_exit;
}
ret = ft260_smbus_write(dev, addr, cmd, data->block,
data->block[0] + 1,
FT260_FLAG_START_STOP);
}
break;
case I2C_SMBUS_I2C_BLOCK_DATA:
if (read_write == I2C_SMBUS_READ) {
ret = ft260_smbus_write(dev, addr, cmd, NULL, 0,
FT260_FLAG_START);
if (ret)
goto smbus_exit;
ret = ft260_i2c_read(dev, addr, data->block + 1,
data->block[0],
FT260_FLAG_START_STOP_REPEATED);
} else {
if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
ret = -EINVAL;
goto smbus_exit;
}
ret = ft260_smbus_write(dev, addr, cmd, data->block + 1,
data->block[0],
FT260_FLAG_START_STOP);
}
break; SourceFile:drivers/hid/hid-ft260.c]
Results:
|
| 41/4 |
2026/01/30 14:40 |
llm |
patch-generator |
0m
Model:
Tokens:
input: 41332
output: 28
thoughts: 2690
Thoughts: **Analysis of I2C/SMBus Block Data Handling in the FT260 Driver**
Okay, so I've applied the fix, and verified the lock handling using `goto smbus_exit` and the bounds check logic. I've addressed both the `I2C_SMBUS_BLOCK_DATA` and `I2C_SMBUS_I2C_BLOCK_DATA` cases. Now, I'm taking a closer look at the `I2C_SMBUS_BLOCK_DATA` read operation because of how `data->block[0]` is used, and I'm realizing there's a subtle but critical issue here.
In the read case, the code uses `data->block[0]` both as input and to determine the length:
```c
ret = ft260_i2c_read(dev, addr, data->block,
data->block[0] + 1,
FT260_FLAG_START_STOP_REPEATED);
```
For SMBus Block Read, the length is typically received from the device itself. The `i2c_smbus_xfer` function in the core I2C layer passes the `data` structure, which might contain initial values. The standard SMBus Block Read protocol would be: Write Command -> Restart -> Read Byte Count -> Read Data. The `ft260_i2c_read` function expects the length as an argument. If `data->block[0]` is used here, it implies that the caller should set the expected length. But in SMBus, we don't know the length in advance. However, some implementations *might* use `data->block[0]` as a hint or a limit.
Looking at `drivers/i2c/i2c-core-smbus.c`, in the `i2c_smbus_xfer_emulated` function:
```c
case I2C_SMBUS_BLOCK_DATA:
if (read_write == I2C_SMBUS_READ) {
msg[1].flags |= I2C_M_RECV_LEN;
msg[1].len = 1; /* block length will be added by
the underlying bus driver */
} else {
msg[0].len = data->block[0] + 2;
// ...
```
Here, the `hid-ft260` driver implements `smbus_xfer` directly, and in `ft260_smbus_xfer`:
```c
if (read_write == I2C_SMBUS_READ) {
// ...
ret = ft260_i2c_read(dev, addr, data->block,
data->block[0] + 1,
FT260_FLAG_START_STOP_REPEATED);
```
Here, `data->block[0]` *comes directly from the user*. If a malicious user sets garbage in `data->block[0]`, `ft260_i2c_read` will attempt to read that many bytes into `data->block`. Since `data->block` is 34 bytes long, if `data->block[0]` is, say, 100, `ft260_i2c_read` will try to read 101 bytes and overwrite our buffer!
So, I need to add a check for `data->block[0]` in the READ case as well. For standard SMBus Block Read, the length isn't known in advance. Typically, `data->block[0]` is ignored on input for Block Read in the Linux I2C subsystem. If `ft260` uses it, it trusts it. If `ft260_i2c_read` blindly reads `len` bytes, it expects `len` to be correct. If `ft260` relies on `data->block[0]` to determine how many bytes to read, then it treats it as "expected length" or "buffer size". The SMBus Block Read is variable length. Does `ft260` support `I2C_M_RECV_LEN`? It seems like `ft260_smbus_xfer` is implementing SMBus on top of `ft260_i2c_read/write`. If the `ft260` controller does not support "read until stop" or "read length byte then data", then the driver must know the length, but the SMBus Block Read protocol says the length is the first byte received.
If `ft260_
|
| 42/4 |
2026/01/30 14:40 |
tool |
read-file |
0m
Args: map[File:drivers/i2c/i2c-dev.c]
Results: map[Contents:// SPDX-License-Identifier: GPL-2.0-or-later
/*
i2c-dev.c - i2c-bus driver, char device interface
Copyright (C) 1995-97 Simon G. Vogl
Copyright (C) 1998-99 Frodo Looijaard <frodol@dds.nl>
Copyright (C) 2003 Greg Kroah-Hartman <greg@kroah.com>
*/
/* Note that this is a complete rewrite of Simon Vogl's i2c-dev module.
But I have used so much of his original code and ideas that it seems
only fair to recognize him as co-author -- Frodo */
/* The I2C_RDWR ioctl code is written by Kolja Waschk <waschk@telos.de> */
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#include <linux/cdev.h>
#include <linux/compat.h>
#include <linux/device.h>
#include <linux/fs.h>
#include <linux/i2c-dev.h>
#include <linux/i2c.h>
#include <linux/init.h>
#include <linux/jiffies.h>
#include <linux/kernel.h>
#include <linux/list.h>
#include <linux/module.h>
#include <linux/notifier.h>
#include <linux/slab.h>
#include <linux/uaccess.h>
/*
* An i2c_dev represents an i2c_adapter ... an I2C or SMBus master, not a
* slave (i2c_client) with which messages will be exchanged. It's coupled
* with a character special file which is accessed by user mode drivers.
*
* The list of i2c_dev structures is parallel to the i2c_adapter lists
* maintained by the driver model, and is updated using bus notifications.
*/
struct i2c_dev {
struct list_head list;
struct i2c_adapter *adap;
struct device dev;
struct cdev cdev;
};
#define I2C_MINORS (MINORMASK + 1)
static LIST_HEAD(i2c_dev_list);
static DEFINE_SPINLOCK(i2c_dev_list_lock);
static struct i2c_dev *i2c_dev_get_by_minor(unsigned index)
{
struct i2c_dev *i2c_dev;
spin_lock(&i2c_dev_list_lock);
list_for_each_entry(i2c_dev, &i2c_dev_list, list) {
if (i2c_dev->adap->nr == index)
goto found;
}
i2c_dev = NULL;
found:
spin_unlock(&i2c_dev_list_lock);
return i2c_dev;
}
static struct i2c_dev *get_free_i2c_dev(struct i2c_adapter *adap)
{
struct i2c_dev *i2c_dev;
if (adap->nr >= I2C_MINORS) {
pr_err("Out of device minors (%d)\n", adap->nr);
return ERR_PTR(-ENODEV);
}
i2c_dev = kzalloc(sizeof(*i2c_dev), GFP_KERNEL);
if (!i2c_dev)
return ERR_PTR(-ENOMEM);
i2c_dev->adap = adap;
spin_lock(&i2c_dev_list_lock);
list_add_tail(&i2c_dev->list, &i2c_dev_list);
spin_unlock(&i2c_dev_list_lock);
return i2c_dev;
}
static void put_i2c_dev(struct i2c_dev *i2c_dev, bool del_cdev)
{
spin_lock(&i2c_dev_list_lock);
list_del(&i2c_dev->list);
spin_unlock(&i2c_dev_list_lock);
if (del_cdev)
cdev_device_del(&i2c_dev->cdev, &i2c_dev->dev);
put_device(&i2c_dev->dev);
}
static ssize_t name_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct i2c_dev *i2c_dev = i2c_dev_get_by_minor(MINOR(dev->devt));
if (!i2c_dev)
return -ENODEV;
return sysfs_emit(buf, "%s\n", i2c_dev->adap->name);
}
static DEVICE_ATTR_RO(name);
static struct attribute *i2c_attrs[] = {
&dev_attr_name.attr,
NULL,
};
ATTRIBUTE_GROUPS(i2c);
/* ------------------------------------------------------------------------- */
/*
* After opening an instance of this character special file, a file
* descriptor starts out associated only with an i2c_adapter (and bus).
*
* Using the I2C_RDWR ioctl(), you can then *immediately* issue i2c_msg
* traffic to any devices on the bus used by that adapter. That's because
* the i2c_msg vectors embed all the addressing information they need, and
* are submitted directly to an i2c_adapter. However, SMBus-only adapters
* don't support that interface.
*
* To use read()/write() system calls on that file descriptor, or to use
* SMBus interfaces (and work with SMBus-only hosts!), you must first issue
* an I2C_SLAVE (or I2C_SLAVE_FORCE) ioctl. That configures an anonymous
* (never registered) i2c_client so it holds the addressing information
* needed by those system calls and by this SMBus interface.
*/
static ssize_t i2cdev_read(struct file *file, char __user *buf, size_t count,
loff_t *offset)
{
char *tmp;
int ret;
struct i2c_client *client = file->private_data;
/* Adapter must support I2C transfers */
if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
return -EOPNOTSUPP;
if (count > 8192)
count = 8192;
tmp = kzalloc(count, GFP_KERNEL);
if (tmp == NULL)
return -ENOMEM;
pr_debug("i2c-%d reading %zu bytes.\n", iminor(file_inode(file)), count);
ret = i2c_master_recv(client, tmp, count);
if (ret >= 0)
if (copy_to_user(buf, tmp, ret))
ret = -EFAULT;
kfree(tmp);
return ret;
}
static ssize_t i2cdev_write(struct file *file, const char __user *buf,
size_t count, loff_t *offset)
{
int ret;
char *tmp;
struct i2c_client *client = file->private_data;
/* Adapter must support I2C transfers */
if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
return -EOPNOTSUPP;
if (count > 8192)
count = 8192;
tmp = memdup_user(buf, count);
if (IS_ERR(tmp))
return PTR_ERR(tmp);
pr_debug("i2c-%d writing %zu bytes.\n", iminor(file_inode(file)), count);
ret = i2c_master_send(client, tmp, count);
kfree(tmp);
return ret;
}
static int i2cdev_check(struct device *dev, void *addrp)
{
struct i2c_client *client = i2c_verify_client(dev);
if (!client || client->addr != *(unsigned int *)addrp)
return 0;
return dev->driver ? -EBUSY : 0;
}
/* walk up mux tree */
static int i2cdev_check_mux_parents(struct i2c_adapter *adapter, int addr)
{
struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
int result;
result = device_for_each_child(&adapter->dev, &addr, i2cdev_check);
if (!result && parent)
result = i2cdev_check_mux_parents(parent, addr);
return result;
}
/* recurse down mux tree */
static int i2cdev_check_mux_children(struct device *dev, void *addrp)
{
int result;
if (dev->type == &i2c_adapter_type)
result = device_for_each_child(dev, addrp,
i2cdev_check_mux_children);
else
result = i2cdev_check(dev, addrp);
return result;
}
/* This address checking function differs from the one in i2c-core
in that it considers an address with a registered device, but no
driver bound to it, as NOT busy. */
static int i2cdev_check_addr(struct i2c_adapter *adapter, unsigned int addr)
{
struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
int result = 0;
if (parent)
result = i2cdev_check_mux_parents(parent, addr);
if (!result)
result = device_for_each_child(&adapter->dev, &addr,
i2cdev_check_mux_children);
return result;
}
static noinline int i2cdev_ioctl_rdwr(struct i2c_client *client,
unsigned nmsgs, struct i2c_msg *msgs)
{
u8 __user **data_ptrs;
int i, res;
/* Adapter must support I2C transfers */
if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
return -EOPNOTSUPP;
data_ptrs = kmalloc_array(nmsgs, sizeof(u8 __user *), GFP_KERNEL);
if (!data_ptrs)
return -ENOMEM;
res = 0;
for (i = 0; i < nmsgs; i++) {
/* Limit the size of the message to a sane amount */
if (msgs[i].len > 8192) {
res = -EINVAL;
break;
}
data_ptrs[i] = (u8 __user *)msgs[i].buf;
msgs[i].buf = memdup_user(data_ptrs[i], msgs[i].len);
if (IS_ERR(msgs[i].buf)) {
res = PTR_ERR(msgs[i].buf);
break;
}
/* memdup_user allocates with GFP_KERNEL, so DMA is ok */
msgs[i].flags |= I2C_M_DMA_SAFE;
/*
* If the message length is received from the slave (similar
* to SMBus block read), we must ensure that the buffer will
* be large enough to cope with a message length of
* I2C_SMBUS_BLOCK_MAX as this is the maximum underlying bus
* drivers allow. The first byte in the buffer must be
* pre-filled with the number of extra bytes, which must be
* at least one to hold the message length, but can be
* greater (for example to account for a checksum byte at
* the end of the message.)
*/
if (msgs[i].flags & I2C_M_RECV_LEN) {
if (!(msgs[i].flags & I2C_M_RD) ||
msgs[i].len < 1 || msgs[i].buf[0] < 1 ||
msgs[i].len < msgs[i].buf[0] +
I2C_SMBUS_BLOCK_MAX) {
i++;
res = -EINVAL;
break;
}
msgs[i].len = msgs[i].buf[0];
}
}
if (res < 0) {
int j;
for (j = 0; j < i; ++j)
kfree(msgs[j].buf);
kfree(data_ptrs);
return res;
}
res = i2c_transfer(client->adapter, msgs, nmsgs);
while (i-- > 0) {
if (res >= 0 && (msgs[i].flags & I2C_M_RD)) {
if (copy_to_user(data_ptrs[i], msgs[i].buf,
msgs[i].len))
res = -EFAULT;
}
kfree(msgs[i].buf);
}
kfree(data_ptrs);
return res;
}
static noinline int i2cdev_ioctl_smbus(struct i2c_client *client,
u8 read_write, u8 command, u32 size,
union i2c_smbus_data __user *data)
{
union i2c_smbus_data temp = {};
int datasize, res;
if ((size != I2C_SMBUS_BYTE) &&
(size != I2C_SMBUS_QUICK) &&
(size != I2C_SMBUS_BYTE_DATA) &&
(size != I2C_SMBUS_WORD_DATA) &&
(size != I2C_SMBUS_PROC_CALL) &&
(size != I2C_SMBUS_BLOCK_DATA) &&
(size != I2C_SMBUS_I2C_BLOCK_BROKEN) &&
(size != I2C_SMBUS_I2C_BLOCK_DATA) &&
(size != I2C_SMBUS_BLOCK_PROC_CALL)) {
dev_dbg(&client->adapter->dev,
"size out of range (%x) in ioctl I2C_SMBUS.\n",
size);
return -EINVAL;
}
/* Note that I2C_SMBUS_READ and I2C_SMBUS_WRITE are 0 and 1,
so the check is valid if size==I2C_SMBUS_QUICK too. */
if ((read_write != I2C_SMBUS_READ) &&
(read_write != I2C_SMBUS_WRITE)) {
dev_dbg(&client->adapter->dev,
"read_write out of range (%x) in ioctl I2C_SMBUS.\n",
read_write);
return -EINVAL;
}
/* Note that command values are always valid! */
if ((size == I2C_SMBUS_QUICK) ||
((size == I2C_SMBUS_BYTE) &&
(read_write == I2C_SMBUS_WRITE)))
/* These are special: we do not use data */
return i2c_smbus_xfer(client->adapter, client->addr,
client->flags, read_write,
command, size, NULL);
if (data == NULL) {
dev_dbg(&client->adapter->dev,
"data is NULL pointer in ioctl I2C_SMBUS.\n");
return -EINVAL;
}
if ((size == I2C_SMBUS_BYTE_DATA) ||
(size == I2C_SMBUS_BYTE))
datasize = sizeof(data->byte);
else if ((size == I2C_SMBUS_WORD_DATA) ||
(size == I2C_SMBUS_PROC_CALL))
datasize = sizeof(data->word);
else /* size == smbus block, i2c block, or block proc. call */
datasize = sizeof(data->block);
if ((size == I2C_SMBUS_PROC_CALL) ||
(size == I2C_SMBUS_BLOCK_PROC_CALL) ||
(size == I2C_SMBUS_I2C_BLOCK_DATA) ||
(read_write == I2C_SMBUS_WRITE)) {
if (copy_from_user(&temp, data, datasize))
return -EFAULT;
}
if (size == I2C_SMBUS_I2C_BLOCK_BROKEN) {
/* Convert old I2C block commands to the new
convention. This preserves binary compatibility. */
size = I2C_SMBUS_I2C_BLOCK_DATA;
if (read_write == I2C_SMBUS_READ)
temp.block[0] = I2C_SMBUS_BLOCK_MAX;
}
res = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
read_write, command, size, &temp);
if (!res && ((size == I2C_SMBUS_PROC_CALL) ||
(size == I2C_SMBUS_BLOCK_PROC_CALL) ||
(read_write == I2C_SMBUS_READ))) {
if (copy_to_user(data, &temp, datasize))
return -EFAULT;
}
return res;
}
static long i2cdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
struct i2c_client *client = file->private_data;
unsigned long funcs;
dev_dbg(&client->adapter->dev, "ioctl, cmd=0x%02x, arg=0x%02lx\n",
cmd, arg);
switch (cmd) {
case I2C_SLAVE:
case I2C_SLAVE_FORCE:
if ((arg > 0x3ff) ||
(((client->flags & I2C_M_TEN) == 0) && arg > 0x7f))
return -EINVAL;
if (cmd == I2C_SLAVE && i2cdev_check_addr(client->adapter, arg))
return -EBUSY;
/* REVISIT: address could become busy later */
client->addr = arg;
return 0;
case I2C_TENBIT:
if (arg)
client->flags |= I2C_M_TEN;
else
client->flags &= ~I2C_M_TEN;
return 0;
case I2C_PEC:
/*
* Setting the PEC flag here won't affect kernel drivers,
* which will be using the i2c_client node registered with
* the driver model core. Likewise, when that client has
* the PEC flag already set, the i2c-dev driver won't see
* (or use) this setting.
*/
if (arg)
client->flags |= I2C_CLIENT_PEC;
else
client->flags &= ~I2C_CLIENT_PEC;
return 0;
case I2C_FUNCS:
funcs = i2c_get_functionality(client->adapter);
return put_user(funcs, (unsigned long __user *)arg);
case I2C_RDWR: {
struct i2c_rdwr_ioctl_data rdwr_arg;
struct i2c_msg *rdwr_pa;
int res;
if (copy_from_user(&rdwr_arg,
(struct i2c_rdwr_ioctl_data __user *)arg,
sizeof(rdwr_arg)))
return -EFAULT;
if (!rdwr_arg.msgs || rdwr_arg.nmsgs == 0)
return -EINVAL;
/*
* Put an arbitrary limit on the number of messages that can
* be sent at once
*/
if (rdwr_arg.nmsgs > I2C_RDWR_IOCTL_MAX_MSGS)
return -EINVAL;
rdwr_pa = memdup_array_user(rdwr_arg.msgs,
rdwr_arg.nmsgs, sizeof(struct i2c_msg));
if (IS_ERR(rdwr_pa))
return PTR_ERR(rdwr_pa);
res = i2cdev_ioctl_rdwr(client, rdwr_arg.nmsgs, rdwr_pa);
kfree(rdwr_pa);
return res;
}
case I2C_SMBUS: {
struct i2c_smbus_ioctl_data data_arg;
if (copy_from_user(&data_arg,
(struct i2c_smbus_ioctl_data __user *) arg,
sizeof(struct i2c_smbus_ioctl_data)))
return -EFAULT;
return i2cdev_ioctl_smbus(client, data_arg.read_write,
data_arg.command,
data_arg.size,
data_arg.data);
}
case I2C_RETRIES:
if (arg > INT_MAX)
return -EINVAL;
client->adapter->retries = arg;
break;
case I2C_TIMEOUT:
if (arg > INT_MAX)
return -EINVAL;
/* For historical reasons, user-space sets the timeout
* value in units of 10 ms.
*/
client->adapter->timeout = msecs_to_jiffies(arg * 10);
break;
default:
/* NOTE: returning a fault code here could cause trouble
* in buggy userspace code. Some old kernel bugs returned
* zero in this case, and userspace code might accidentally
* have depended on that bug.
*/
return -ENOTTY;
}
return 0;
}
#ifdef CONFIG_COMPAT
struct i2c_smbus_ioctl_data32 {
u8 read_write;
u8 command;
u32 size;
compat_caddr_t data; /* union i2c_smbus_data *data */
};
struct i2c_msg32 {
u16 addr;
u16 flags;
u16 len;
compat_caddr_t buf;
};
struct i2c_rdwr_ioctl_data32 {
compat_caddr_t msgs; /* struct i2c_msg __user *msgs */
u32 nmsgs;
};
static long compat_i2cdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
struct i2c_client *client = file->private_data;
unsigned long funcs;
switch (cmd) {
case I2C_FUNCS:
funcs = i2c_get_functionality(client->adapter);
return put_user(funcs, (compat_ulong_t __user *)arg);
case I2C_RDWR: {
struct i2c_rdwr_ioctl_data32 rdwr_arg;
struct i2c_msg32 __user *p;
struct i2c_msg *rdwr_pa;
int i, res;
if (copy_from_user(&rdwr_arg,
(struct i2c_rdwr_ioctl_data32 __user *)arg,
sizeof(rdwr_arg)))
return -EFAULT;
if (!rdwr_arg.msgs || rdwr_arg.nmsgs == 0)
return -EINVAL;
if (rdwr_arg.nmsgs > I2C_RDWR_IOCTL_MAX_MSGS)
return -EINVAL;
rdwr_pa = kmalloc_array(rdwr_arg.nmsgs, sizeof(struct i2c_msg),
GFP_KERNEL);
if (!rdwr_pa)
return -ENOMEM;
p = compat_ptr(rdwr_arg.msgs);
for (i = 0; i < rdwr_arg.nmsgs; i++) {
struct i2c_msg32 umsg;
if (copy_from_user(&umsg, p + i, sizeof(umsg))) {
kfree(rdwr_pa);
return -EFAULT;
}
rdwr_pa[i] = (struct i2c_msg) {
.addr = umsg.addr,
.flags = umsg.flags,
.len = umsg.len,
.buf = (__force __u8 *)compat_ptr(umsg.buf),
};
}
res = i2cdev_ioctl_rdwr(client, rdwr_arg.nmsgs, rdwr_pa);
kfree(rdwr_pa);
return res;
}
case I2C_SMBUS: {
struct i2c_smbus_ioctl_data32 data32;
if (copy_from_user(&data32,
(void __user *) arg,
sizeof(data32)))
return -EFAULT;
return i2cdev_ioctl_smbus(client, data32.read_write,
data32.command,
data32.size,
compat_ptr(data32.data));
}
default:
return i2cdev_ioctl(file, cmd, arg);
}
}
#else
#define compat_i2cdev_ioctl NULL
#endif
static int i2cdev_open(struct inode *inode, struct file *file)
{
unsigned int minor = iminor(inode);
struct i2c_client *client;
struct i2c_adapter *adap;
adap = i2c_get_adapter(minor);
if (!adap)
return -ENODEV;
/* This creates an anonymous i2c_client, which may later be
* pointed to some address using I2C_SLAVE or I2C_SLAVE_FORCE.
*
* This client is ** NEVER REGISTERED ** with the driver model
* or I2C core code!! It just holds private copies of addressing
* information and maybe a PEC flag.
*/
client = kzalloc(sizeof(*client), GFP_KERNEL);
if (!client) {
i2c_put_adapter(adap);
return -ENOMEM;
}
snprintf(client->name, I2C_NAME_SIZE, "i2c-dev %d", adap->nr);
client->adapter = adap;
file->private_data = client;
return 0;
}
static int i2cdev_release(struct inode *inode, struct file *file)
{
struct i2c_client *client = file->private_data;
i2c_put_adapter(client->adapter);
kfree(client);
file->private_data = NULL;
return 0;
}
static const struct file_operations i2cdev_fops = {
.owner = THIS_MODULE,
.read = i2cdev_read,
.write = i2cdev_write,
.unlocked_ioctl = i2cdev_ioctl,
.compat_ioctl = compat_i2cdev_ioctl,
.open = i2cdev_open,
.release = i2cdev_release,
};
/* ------------------------------------------------------------------------- */
static const struct class i2c_dev_class = {
.name = "i2c-dev",
.dev_groups = i2c_groups,
};
static void i2cdev_dev_release(struct device *dev)
{
struct i2c_dev *i2c_dev;
i2c_dev = container_of(dev, struct i2c_dev, dev);
kfree(i2c_dev);
}
static int i2cdev_attach_adapter(struct device *dev)
{
struct i2c_adapter *adap;
struct i2c_dev *i2c_dev;
int res;
if (dev->type != &i2c_adapter_type)
return NOTIFY_DONE;
adap = to_i2c_adapter(dev);
i2c_dev = get_free_i2c_dev(adap);
if (IS_ERR(i2c_dev))
return NOTIFY_DONE;
cdev_init(&i2c_dev->cdev, &i2cdev_fops);
i2c_dev->cdev.owner = THIS_MODULE;
device_initialize(&i2c_dev->dev);
i2c_dev->dev.devt = MKDEV(I2C_MAJOR, adap->nr);
i2c_dev->dev.class = &i2c_dev_class;
i2c_dev->dev.parent = &adap->dev;
i2c_dev->dev.release = i2cdev_dev_release;
res = dev_set_name(&i2c_dev->dev, "i2c-%d", adap->nr);
if (res)
goto err_put_i2c_dev;
res = cdev_device_add(&i2c_dev->cdev, &i2c_dev->dev);
if (res)
goto err_put_i2c_dev;
pr_debug("adapter [%s] registered as minor %d\n", adap->name, adap->nr);
return NOTIFY_OK;
err_put_i2c_dev:
put_i2c_dev(i2c_dev, false);
return NOTIFY_DONE;
}
static int i2cdev_detach_adapter(struct device *dev)
{
struct i2c_adapter *adap;
struct i2c_dev *i2c_dev;
if (dev->type != &i2c_adapter_type)
return NOTIFY_DONE;
adap = to_i2c_adapter(dev);
i2c_dev = i2c_dev_get_by_minor(adap->nr);
if (!i2c_dev) /* attach_adapter must have failed */
return NOTIFY_DONE;
put_i2c_dev(i2c_dev, true);
pr_debug("adapter [%s] unregistered\n", adap->name);
return NOTIFY_OK;
}
static int i2cdev_notifier_call(struct notifier_block *nb, unsigned long action,
void *data)
{
struct device *dev = data;
switch (action) {
case BUS_NOTIFY_ADD_DEVICE:
return i2cdev_attach_adapter(dev);
case BUS_NOTIFY_DEL_DEVICE:
return i2cdev_detach_adapter(dev);
}
return NOTIFY_DONE;
}
static struct notifier_block i2cdev_notifier = {
.notifier_call = i2cdev_notifier_call,
};
/* ------------------------------------------------------------------------- */
static int __init i2c_dev_attach_adapter(struct device *dev, void *dummy)
{
i2cdev_attach_adapter(dev);
return 0;
}
static int __exit i2c_dev_detach_adapter(struct device *dev, void *dummy)
{
i2cdev_detach_adapter(dev);
return 0;
}
/*
* module load/unload record keeping
*/
static int __init i2c_dev_init(void)
{
int res;
pr_info("i2c /dev entries driver\n");
res = register_chrdev_region(MKDEV(I2C_MAJOR, 0), I2C_MINORS, "i2c");
if (res)
goto out;
res = class_register(&i2c_dev_class);
if (res)
goto out_unreg_chrdev;
/* Keep track of adapters which will be added or removed later */
res = bus_register_notifier(&i2c_bus_type, &i2cdev_notifier);
if (res)
goto out_unreg_class;
/* Bind to already existing adapters right away */
i2c_for_each_dev(NULL, i2c_dev_attach_adapter);
return 0;
out_unreg_class:
class_unregister(&i2c_dev_class);
out_unreg_chrdev:
unregister_chrdev_region(MKDEV(I2C_MAJOR, 0), I2C_MINORS);
out:
pr_err("Driver Initialisation failed\n");
return res;
}
static void __exit i2c_dev_exit(void)
{
bus_unregister_notifier(&i2c_bus_type, &i2cdev_notifier);
i2c_for_each_dev(NULL, i2c_dev_detach_adapter);
class_unregister(&i2c_dev_class);
unregister_chrdev_region(MKDEV(I2C_MAJOR, 0), I2C_MINORS);
}
MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>");
MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
MODULE_DESCRIPTION("I2C /dev entries driver");
MODULE_LICENSE("GPL");
module_init(i2c_dev_init);
module_exit(i2c_dev_exit);
]
|
| 43/4 |
2026/01/30 14:40 |
llm |
patch-generator |
0m
Model:
Tokens:
input: 53377
output: 1081
thoughts: 4204
Thoughts: **Analysis of I2C SMBus Block Data Vulnerability and Proposed Fixes**
Okay, so here's the deal. I've been digging into this I2C driver code, specifically the `ft260` driver, and it's become clear there's a potential buffer overflow vulnerability lurking within the `i2cdev_ioctl_smbus` implementation. It looks like the kernel's I2C driver, in its `i2cdev_ioctl_smbus` function, doesn't always check the size parameter, `data->block[0]`, when handling SMBus block transfers. I had to double check, but after seeing the implementation, I need to check the size, in case it is misused.
Initially, I looked for that check within the `i2c-dev.c` output, but it doesn't appear to be there, and this makes the driver code the only place where the fix can reside. If it isn't in `i2c-dev.c`, the drivers using the implementation need to check it and use a safe value. I had to double-check my work, and it's clear the check is not there.
This lack of size validation leads to a crash, because the `ft260_i2c_read` function, the `ft260` driver uses to implement reads, trusts this size without validation and can overflow a buffer if an attacker provides a maliciously large size value. The kernel crash log confirms that the issue is inside `i2cdev_ioctl_smbus`.
The vulnerability manifests in both read and write operations, and also in the standard SMBus Block Data and I2C Block Data variants. Specifically, for SMBus block reads, `data->block[0]` is supposed to be the *length* of the read, and is unchecked in both standard `I2C_SMBUS_BLOCK_DATA` and `I2C_SMBUS_I2C_BLOCK_DATA`. It will read N+1 bytes if not properly checked. For block *writes*, the user controls `data->block[0]` in both types.
The core problem, I've confirmed, is that `ft260_i2c_read` doesn't know the size of the buffer it's writing to. It *trusts* the `len` value passed to it, which is derived from the user-provided size `data->block[0]`.
To mitigate this, I need to add checks *everywhere* the length is used. In all cases (Read/Write for both Block/I2C Block data), I need to check `data->block[0]` against a safe maximum value, which in this case will be `I2C_SMBUS_BLOCK_MAX`.
Specifically, my changes will include this added check in the following four locations:
1. `I2C_SMBUS_BLOCK_DATA` READ
2. `I2C_SMBUS_BLOCK_DATA` WRITE
3. `I2C_SMBUS_I2C_BLOCK_DATA` READ
4. `I2C_SMBUS_I2C_BLOCK_DATA` WRITE
Before calling any of the `ft260` functions with the data length, I will check if the user-provided `data->block[0]` is greater than `I2C_SMBUS_BLOCK_MAX`. If it is, return -EINVAL immediately.
Here's the final code block I will implement for safety:
```c
case I2C_SMBUS_BLOCK_DATA:
if (read_write == I2C_SMBUS_READ) {
if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
ret = -EINVAL;
goto smbus_exit;
}
ret = ft260_smbus_write(dev, addr, cmd, NULL, 0,
FT260_FLAG_START);
if (ret)
goto smbus_exit;
ret = ft260_i2c_read(dev, addr, data->block,
data->block[0] + 1,
FT260_FLAG_START_STOP_REPEATED);
} else {
if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
ret = -EINVAL;
goto smbus_exit;
}
ret = ft260_smbus_write(dev, addr, cmd, data->block,
data->block[0] + 1,
FT26
|
| 44/4 |
2026/01/30 14:41 |
tool |
codeeditor |
0m
Args: map[CurrentCode: case I2C_SMBUS_BLOCK_DATA:
if (read_write == I2C_SMBUS_READ) {
ret = ft260_smbus_write(dev, addr, cmd, NULL, 0,
FT260_FLAG_START);
if (ret)
goto smbus_exit;
ret = ft260_i2c_read(dev, addr, data->block,
data->block[0] + 1,
FT260_FLAG_START_STOP_REPEATED);
} else {
if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
ret = -EINVAL;
goto smbus_exit;
}
ret = ft260_smbus_write(dev, addr, cmd, data->block,
data->block[0] + 1,
FT260_FLAG_START_STOP);
}
break;
case I2C_SMBUS_I2C_BLOCK_DATA:
if (read_write == I2C_SMBUS_READ) {
ret = ft260_smbus_write(dev, addr, cmd, NULL, 0,
FT260_FLAG_START);
if (ret)
goto smbus_exit;
ret = ft260_i2c_read(dev, addr, data->block + 1,
data->block[0],
FT260_FLAG_START_STOP_REPEATED);
} else {
if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
ret = -EINVAL;
goto smbus_exit;
}
ret = ft260_smbus_write(dev, addr, cmd, data->block + 1,
data->block[0],
FT260_FLAG_START_STOP);
}
break; NewCode: case I2C_SMBUS_BLOCK_DATA:
if (read_write == I2C_SMBUS_READ) {
if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
ret = -EINVAL;
goto smbus_exit;
}
ret = ft260_smbus_write(dev, addr, cmd, NULL, 0,
FT260_FLAG_START);
if (ret)
goto smbus_exit;
ret = ft260_i2c_read(dev, addr, data->block,
data->block[0] + 1,
FT260_FLAG_START_STOP_REPEATED);
} else {
if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
ret = -EINVAL;
goto smbus_exit;
}
ret = ft260_smbus_write(dev, addr, cmd, data->block,
data->block[0] + 1,
FT260_FLAG_START_STOP);
}
break;
case I2C_SMBUS_I2C_BLOCK_DATA:
if (read_write == I2C_SMBUS_READ) {
if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
ret = -EINVAL;
goto smbus_exit;
}
ret = ft260_smbus_write(dev, addr, cmd, NULL, 0,
FT260_FLAG_START);
if (ret)
goto smbus_exit;
ret = ft260_i2c_read(dev, addr, data->block + 1,
data->block[0],
FT260_FLAG_START_STOP_REPEATED);
} else {
if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
ret = -EINVAL;
goto smbus_exit;
}
ret = ft260_smbus_write(dev, addr, cmd, data->block + 1,
data->block[0],
FT260_FLAG_START_STOP);
}
break; SourceFile:drivers/hid/hid-ft260.c]
Results:
|
| 45/4 |
2026/01/30 14:41 |
llm |
patch-generator |
0m
Model:
Tokens:
input: 59694
output: 1023
thoughts: 2999
Thoughts: **Analysis and Solution for ft260_smbus_xfer Buffer Overflow**
Okay, so I've finalized the fix. I've addressed a potential buffer overflow vulnerability within the `ft260_smbus_xfer` function, stemming from a lack of validation on the `data->block[0]` value, which determines the data block's length. This value, crucially, comes from user space.
Here's my thought process: The core issue is that `ft260_smbus_xfer` doesn't adequately check the size provided in `data->block[0]`. This value is then used as a length in subsequent operations such as reads and writes via `ft260_smbus_write` and `ft260_i2c_read`, which can lead to out-of-bounds access on the stack if a malicious or erroneous length is provided, specifically in the kernel stack variable `temp.block`. The `temp.block` is copied from user space for writes or zeroed for reads, in the `i2cdev_ioctl_smbus` function.
To resolve this, I've inserted a check: `if (data->block[0] > I2C_SMBUS_BLOCK_MAX)`. If this user-provided block length is greater than `I2C_SMBUS_BLOCK_MAX` (which is 32), then I immediately return an `-EINVAL` error and jump to `smbus_exit`. Critically, this ensures the mutex is unlocked.
I've strategically placed this check in four key locations within the `ft260_smbus_xfer` function:
1. **`I2C_SMBUS_BLOCK_DATA` READ**
2. **`I2C_SMBUS_BLOCK_DATA` WRITE**
3. **`I2C_SMBUS_I2C_BLOCK_DATA` READ**
4. **`I2C_SMBUS_I2C_BLOCK_DATA` WRITE**
Why so many? Because a defense-in-depth approach is always best. These checks are now present for BOTH read and write operations, preventing both potential buffer overflows in write operations *and* preventing stack overflows from `ft260_i2c_read`, which could write beyond the allocated stack buffer.
Regarding the details, I've confirmed that the array size of `data->block` is 34 bytes (due to `I2C_SMBUS_BLOCK_MAX + 2`). This means that a length of 32 or less is safe. While a length of 33 might technically fit in certain cases, it's safer and conforms to the `I2C_SMBUS_BLOCK_MAX` standard to strictly enforce a limit of 32. This also aligns with the intended purpose of `I2C_SMBUS_BLOCK_MAX`.
Specifically, in `I2C_SMBUS_I2C_BLOCK_DATA` the read and write operations that I've focused on, access is on `data->block+1`. So the array for this case is really 33 bytes. This means that a length value of 33 is also safe. But `I2C_SMBUS_BLOCK_MAX` is the standard.
Iβve also confirmed that the `read_write` variable is validated by the `i2c-core` and `i2cdev_ioctl_smbus` code before reaching `ft260_smbus_xfer` so this is safe.
I have validated that the fix is correct, and I am ready to implement the fix.
|
| 46/3 |
2026/01/30 14:42 |
action |
test-patch |
10m
Results: map[PatchDiff:diff --git a/drivers/hid/hid-ft260.c b/drivers/hid/hid-ft260.c
index 333341e80..e97a8472a 100644
--- a/drivers/hid/hid-ft260.c
+++ b/drivers/hid/hid-ft260.c
@@ -708,6 +708,11 @@ static int ft260_smbus_xfer(struct i2c_adapter *adapter, u16 addr, u16 flags,
break;
case I2C_SMBUS_BLOCK_DATA:
if (read_write == I2C_SMBUS_READ) {
+ if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
+ ret = -EINVAL;
+ goto smbus_exit;
+ }
+
ret = ft260_smbus_write(dev, addr, cmd, NULL, 0,
FT260_FLAG_START);
if (ret)
@@ -717,6 +722,11 @@ static int ft260_smbus_xfer(struct i2c_adapter *adapter, u16 addr, u16 flags,
data->block[0] + 1,
FT260_FLAG_START_STOP_REPEATED);
} else {
+ if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
+ ret = -EINVAL;
+ goto smbus_exit;
+ }
+
ret = ft260_smbus_write(dev, addr, cmd, data->block,
data->block[0] + 1,
FT260_FLAG_START_STOP);
@@ -724,6 +734,11 @@ static int ft260_smbus_xfer(struct i2c_adapter *adapter, u16 addr, u16 flags,
break;
case I2C_SMBUS_I2C_BLOCK_DATA:
if (read_write == I2C_SMBUS_READ) {
+ if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
+ ret = -EINVAL;
+ goto smbus_exit;
+ }
+
ret = ft260_smbus_write(dev, addr, cmd, NULL, 0,
FT260_FLAG_START);
if (ret)
@@ -733,6 +748,11 @@ static int ft260_smbus_xfer(struct i2c_adapter *adapter, u16 addr, u16 flags,
data->block[0],
FT260_FLAG_START_STOP_REPEATED);
} else {
+ if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
+ ret = -EINVAL;
+ goto smbus_exit;
+ }
+
ret = ft260_smbus_write(dev, addr, cmd, data->block + 1,
data->block[0],
FT260_FLAG_START_STOP);
TestError:]
|
| 47/1 |
2026/01/30 14:52 |
action |
get-maintainers |
0m
Results: map[Recipients:[map[Email:bentiss@kernel.org Name:Benjamin Tissoires To:true] map[Email:jikos@kernel.org Name:Jiri Kosina To:true] map[Email:linux-i2c@vger.kernel.org Name: To:true] map[Email:linux-input@vger.kernel.org Name: To:true] map[Email:linux-kernel@vger.kernel.org Name: To:false] map[Email:michael.zaidman@gmail.com Name:Michael Zaidman To:true]]]
|
| 48/1 |
2026/01/30 14:52 |
agent |
description-generator |
1m
Model:
Instruction:
You are an experienced Linux kernel developer tasked with writing a commit description for
a kernel bug fixing commit. The description should start with a one-line summary,
and then include description of the bug being fixed, and how it's fixed by the provided patch.
Your final reply should contain only the text of the commit description.
Phrase the one-line summary so that it is not longer than 72 characters.
The rest of the description must be word-wrapped at 72 characters.
Prefer calling several tools at the same time to save round-trips.
Prompt:
The crash that corresponds to the bug is:
==================================================================
BUG: KASAN: stack-out-of-bounds in ft260_smbus_write+0x156/0x2f0 drivers/hid/hid-ft260.c:486
Read of size 42 at addr ffffc90002417d21 by task syz-executor165/6009
CPU: 0 UID: 0 PID: 6009 Comm: syz-executor165 Not tainted syzkaller #1 PREEMPT(full)
Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.17.0-debian-1.17.0-1 04/01/2014
Call Trace:
<TASK>
__dump_stack lib/dump_stack.c:94 [inline]
dump_stack_lvl+0x10e/0x190 lib/dump_stack.c:120
print_address_description mm/kasan/report.c:378 [inline]
print_report+0x17e/0x810 mm/kasan/report.c:482
kasan_report+0x147/0x180 mm/kasan/report.c:595
check_region_inline mm/kasan/generic.c:-1 [inline]
kasan_check_range+0x2b0/0x2c0 mm/kasan/generic.c:200
__asan_memcpy+0x29/0x70 mm/kasan/shadow.c:105
ft260_smbus_write+0x156/0x2f0 drivers/hid/hid-ft260.c:486
ft260_smbus_xfer+0x8bc/0xdd0 drivers/hid/hid-ft260.c:-1
__i2c_smbus_xfer+0x557/0x1e20 drivers/i2c/i2c-core-smbus.c:591
i2c_smbus_xfer+0x1f4/0x310 drivers/i2c/i2c-core-smbus.c:546
i2cdev_ioctl_smbus+0x3db/0x750 drivers/i2c/i2c-dev.c:389
i2cdev_ioctl+0x5d3/0x820 drivers/i2c/i2c-dev.c:478
vfs_ioctl fs/ioctl.c:51 [inline]
__do_sys_ioctl fs/ioctl.c:597 [inline]
__se_sys_ioctl+0xfc/0x170 fs/ioctl.c:583
do_syscall_x64 arch/x86/entry/syscall_64.c:63 [inline]
do_syscall_64+0xe8/0xf80 arch/x86/entry/syscall_64.c:94
entry_SYSCALL_64_after_hwframe+0x77/0x7f
RIP: 0033:0x7f1925b454f9
Code: c0 79 93 eb d5 48 8d 7c 1d 00 eb 99 0f 1f 44 00 00 48 89 f8 48 89 f7 48 89 d6 48 89 ca 4d 89 c2 4d 89 c8 4c 8b 4c 24 08 0f 05 <48> 3d 01 f0 ff ff 73 01 c3 48 c7 c1 d8 ff ff ff f7 d8 64 89 01 48
RSP: 002b:00007ffee7bc0ab8 EFLAGS: 00000246 ORIG_RAX: 0000000000000010
RAX: ffffffffffffffda RBX: 00007ffee7bc0c48 RCX: 00007f1925b454f9
RDX: 0000200000000200 RSI: 0000000000000720 RDI: 0000000000000004
RBP: 0000000000000001 R08: 0000000000000000 R09: 0000000000000009
R10: 0000000000000000 R11: 0000000000000246 R12: 00007ffee7bc0ae0
R13: 00007ffee7bc0ad0 R14: 00007f1925bd8c40 R15: 0000000000000000
</TASK>
The buggy address belongs to stack of task syz-executor165/6009
and is located at offset 33 in frame:
i2cdev_ioctl_smbus+0x0/0x750 drivers/i2c/i2c-dev.c:317
This frame has 1 object:
[32, 66) 'temp'
The buggy address belongs to a 8-page vmalloc region starting at 0xffffc90002410000 allocated at copy_process+0x4e7/0x3950 kernel/fork.c:2052
The buggy address belongs to the physical page:
page: refcount:1 mapcount:0 mapping:0000000000000000 index:0x0 pfn:0x213ff
flags: 0xfff00000000000(node=0|zone=1|lastcpupid=0x7ff)
raw: 00fff00000000000 0000000000000000 ffffea000084ffc8 0000000000000000
raw: 0000000000000000 0000000000000000 00000001ffffffff 0000000000000000
page dumped because: kasan: bad access detected
page_owner tracks the page as allocated
page last allocated via order 0, migratetype Unmovable, gfp_mask 0x29c2(GFP_NOWAIT|__GFP_HIGHMEM|__GFP_IO|__GFP_FS|__GFP_ZERO), pid 6007, tgid 6007 (syz-executor165), ts 90494270799, free_ts 87604917166
set_page_owner include/linux/page_owner.h:32 [inline]
post_alloc_hook+0x234/0x290 mm/page_alloc.c:1884
prep_new_page mm/page_alloc.c:1892 [inline]
get_page_from_freelist+0x24e4/0x2580 mm/page_alloc.c:3945
__alloc_frozen_pages_noprof+0x181/0x370 mm/page_alloc.c:5240
alloc_pages_mpol+0x232/0x4a0 mm/mempolicy.c:2486
alloc_frozen_pages_noprof mm/mempolicy.c:2557 [inline]
alloc_pages_noprof+0xa9/0x190 mm/mempolicy.c:2577
vm_area_alloc_pages mm/vmalloc.c:3649 [inline]
__vmalloc_area_node mm/vmalloc.c:3863 [inline]
__vmalloc_node_range_noprof+0x919/0x16a0 mm/vmalloc.c:4051
__vmalloc_node_noprof+0xc2/0x110 mm/vmalloc.c:4111
alloc_thread_stack_node kernel/fork.c:354 [inline]
dup_task_struct+0x250/0x9b0 kernel/fork.c:923
copy_process+0x4e7/0x3950 kernel/fork.c:2052
kernel_clone+0x21e/0x820 kernel/fork.c:2651
__do_sys_clone kernel/fork.c:2792 [inline]
__se_sys_clone kernel/fork.c:2776 [inline]
__x64_sys_clone+0x190/0x1f0 kernel/fork.c:2776
do_syscall_x64 arch/x86/entry/syscall_64.c:63 [inline]
do_syscall_64+0xe8/0xf80 arch/x86/entry/syscall_64.c:94
entry_SYSCALL_64_after_hwframe+0x77/0x7f
page last free pid 5954 tgid 5954 stack trace:
reset_page_owner include/linux/page_owner.h:25 [inline]
free_pages_prepare mm/page_alloc.c:1433 [inline]
__free_frozen_pages+0xbc4/0xd40 mm/page_alloc.c:2973
discard_slab mm/slub.c:3346 [inline]
__put_partials+0x146/0x170 mm/slub.c:3886
__slab_free+0x294/0x320 mm/slub.c:5956
qlink_free mm/kasan/quarantine.c:163 [inline]
qlist_free_all+0x97/0x100 mm/kasan/quarantine.c:179
kasan_quarantine_reduce+0x148/0x160 mm/kasan/quarantine.c:286
__kasan_slab_alloc+0x22/0x80 mm/kasan/common.c:350
kasan_slab_alloc include/linux/kasan.h:253 [inline]
slab_post_alloc_hook mm/slub.c:4953 [inline]
slab_alloc_node mm/slub.c:5263 [inline]
kmem_cache_alloc_noprof+0x37d/0x710 mm/slub.c:5270
getname_flags+0xb8/0x540 fs/namei.c:146
getname include/linux/fs.h:2498 [inline]
getname_maybe_null include/linux/fs.h:2505 [inline]
vfs_fstatat+0x43/0x170 fs/stat.c:370
__do_sys_newfstatat fs/stat.c:542 [inline]
__se_sys_newfstatat fs/stat.c:536 [inline]
__x64_sys_newfstatat+0x116/0x190 fs/stat.c:536
do_syscall_x64 arch/x86/entry/syscall_64.c:63 [inline]
do_syscall_64+0xe8/0xf80 arch/x86/entry/syscall_64.c:94
entry_SYSCALL_64_after_hwframe+0x77/0x7f
Memory state around the buggy address:
ffffc90002417c00: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
ffffc90002417c80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
>ffffc90002417d00: f1 f1 f1 f1 00 00 00 00 02 f3 f3 f3 f3 f3 f3 f3
^
ffffc90002417d80: 00 00 00 00 00 00 00 00 00 00 00 00 f1 f1 f1 f1
ffffc90002417e00: 04 f2 00 00 f2 f2 00 00 f3 f3 f3 f3 00 00 00 00
==================================================================
The explanation of the root cause of the bug is:
The root cause of the KASAN stack-out-of-bounds crash is a missing validation of the input data length in the `ft260_smbus_xfer` function in `drivers/hid/hid-ft260.c`.
When handling `I2C_SMBUS_BLOCK_DATA` or `I2C_SMBUS_I2C_BLOCK_DATA` transactions, the function uses the length byte provided by the user (stored in `data->block[0]`) to determine how much data to copy. However, it fails to ensure that this length does not exceed the size of the `data->block` array (or `I2C_SMBUS_BLOCK_MAX`), which is backed by the `union i2c_smbus_data` on the stack of the calling function `i2cdev_ioctl_smbus`.
The `union i2c_smbus_data` has a size of 34 bytes (`I2C_SMBUS_BLOCK_MAX` is 32, plus 2 bytes). The `block` field is defined as `__u8 block[I2C_SMBUS_BLOCK_MAX + 2];`.
In the crash scenario (`I2C_SMBUS_I2C_BLOCK_DATA`), the code does:
```c
736: ret = ft260_smbus_write(dev, addr, cmd, data->block + 1,
737: data->block[0],
738: FT260_FLAG_START_STOP);
```
Here, `data->block[0]` (controlled by the user) is passed as `data_len`. If the user provides a length (e.g., 42) that is larger than the remaining size of the buffer (which is 33 bytes, starting from `data->block[1]`), `ft260_smbus_write` will read out of bounds.
In `ft260_smbus_write`:
```c
486: memcpy(&rep->data[1], data, data_len);
```
It performs a `memcpy` from the stack buffer `data` (which is `&temp.block[1]`) with size 42. Since `temp.block` is only 34 bytes total, reading 42 bytes from offset 1 accesses invalid stack memory, triggering KASAN.
**Fix:**
Validate that `data->block[0]` is less than or equal to `I2C_SMBUS_BLOCK_MAX` (32) in `ft260_smbus_xfer` before processing block transfers.
**Code Snippet to fix in `drivers/hid/hid-ft260.c`:**
```c
static int ft260_smbus_xfer(struct i2c_adapter *adapter, u16 addr, u16 flags,
char read_write, u8 cmd, int size,
union i2c_smbus_data *data)
{
// ...
switch (size) {
// ...
case I2C_SMBUS_BLOCK_DATA:
if (read_write == I2C_SMBUS_READ) {
// ...
} else {
if (data->block[0] > I2C_SMBUS_BLOCK_MAX)
return -EINVAL; // Add this check
ret = ft260_smbus_write(dev, addr, cmd, data->block,
data->block[0] + 1,
FT260_FLAG_START_STOP);
}
break;
case I2C_SMBUS_I2C_BLOCK_DATA:
if (read_write == I2C_SMBUS_READ) {
// ...
} else {
if (data->block[0] > I2C_SMBUS_BLOCK_MAX)
return -EINVAL; // Add this check
ret = ft260_smbus_write(dev, addr, cmd, data->block + 1,
data->block[0],
FT260_FLAG_START_STOP);
}
break;
// ...
}
// ...
}
```
The diff of the bug fix is:
diff --git a/drivers/hid/hid-ft260.c b/drivers/hid/hid-ft260.c
index 333341e80..e97a8472a 100644
--- a/drivers/hid/hid-ft260.c
+++ b/drivers/hid/hid-ft260.c
@@ -708,6 +708,11 @@ static int ft260_smbus_xfer(struct i2c_adapter *adapter, u16 addr, u16 flags,
break;
case I2C_SMBUS_BLOCK_DATA:
if (read_write == I2C_SMBUS_READ) {
+ if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
+ ret = -EINVAL;
+ goto smbus_exit;
+ }
+
ret = ft260_smbus_write(dev, addr, cmd, NULL, 0,
FT260_FLAG_START);
if (ret)
@@ -717,6 +722,11 @@ static int ft260_smbus_xfer(struct i2c_adapter *adapter, u16 addr, u16 flags,
data->block[0] + 1,
FT260_FLAG_START_STOP_REPEATED);
} else {
+ if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
+ ret = -EINVAL;
+ goto smbus_exit;
+ }
+
ret = ft260_smbus_write(dev, addr, cmd, data->block,
data->block[0] + 1,
FT260_FLAG_START_STOP);
@@ -724,6 +734,11 @@ static int ft260_smbus_xfer(struct i2c_adapter *adapter, u16 addr, u16 flags,
break;
case I2C_SMBUS_I2C_BLOCK_DATA:
if (read_write == I2C_SMBUS_READ) {
+ if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
+ ret = -EINVAL;
+ goto smbus_exit;
+ }
+
ret = ft260_smbus_write(dev, addr, cmd, NULL, 0,
FT260_FLAG_START);
if (ret)
@@ -733,6 +748,11 @@ static int ft260_smbus_xfer(struct i2c_adapter *adapter, u16 addr, u16 flags,
data->block[0],
FT260_FLAG_START_STOP_REPEATED);
} else {
+ if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
+ ret = -EINVAL;
+ goto smbus_exit;
+ }
+
ret = ft260_smbus_write(dev, addr, cmd, data->block + 1,
data->block[0],
FT260_FLAG_START_STOP);
Additional description of the patch:
The KASAN crash (stack-out-of-bounds read) occurs in `ft260_smbus_write` because the `ft260_smbus_xfer` function does not validate the length of the data supplied by the user (`data->block[0]`) against the size of the backing buffer (`I2C_SMBUS_BLOCK_MAX`).
When `I2C_SMBUS_BLOCK_DATA` or `I2C_SMBUS_I2C_BLOCK_DATA` transaction is requested, the user-supplied length in `data->block[0]` is passed to `ft260_smbus_write`, which then performs a `memcpy` from the stack buffer `data->block`. Since `data->block` is part of `union i2c_smbus_data` on the stack (size 34 bytes), providing a length larger than 32 (plus protocol overhead) leads to reading beyond the stack variable boundaries.
I have fixed the issue by adding validation checks for `data->block[0] > I2C_SMBUS_BLOCK_MAX` (32) in `ft260_smbus_xfer`. The validation is added for both `I2C_SMBUS_BLOCK_DATA` and `I2C_SMBUS_I2C_BLOCK_DATA`, and for both READ and WRITE operations. This not only fixes the reported crash (WRITE path), but also prevents potential stack buffer overflows in the READ path (where `ft260_i2c_read` writes to the buffer), ensuring the driver operates within safe memory bounds.
The fix handles the error by returning `-EINVAL` and jumping to `smbus_exit` to correctly release the mutex.
```c
static int ft260_smbus_xfer(struct i2c_adapter *adapter, u16 addr, u16 flags,
char read_write, u8 cmd, int size,
union i2c_smbus_data *data)
{
// ...
case I2C_SMBUS_BLOCK_DATA:
if (read_write == I2C_SMBUS_READ) {
if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
ret = -EINVAL;
goto smbus_exit;
}
ret = ft260_smbus_write(dev, addr, cmd, NULL, 0,
FT260_FLAG_START);
if (ret)
goto smbus_exit;
ret = ft260_i2c_read(dev, addr, data->block,
data->block[0] + 1,
FT260_FLAG_START_STOP_REPEATED);
} else {
if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
ret = -EINVAL;
goto smbus_exit;
}
ret = ft260_smbus_write(dev, addr, cmd, data->block,
data->block[0] + 1,
FT260_FLAG_START_STOP);
}
break;
case I2C_SMBUS_I2C_BLOCK_DATA:
if (read_write == I2C_SMBUS_READ) {
if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
ret = -EINVAL;
goto smbus_exit;
}
ret = ft260_smbus_write(dev, addr, cmd, NULL, 0,
FT260_FLAG_START);
if (ret)
goto smbus_exit;
ret = ft260_i2c_read(dev, addr, data->block + 1,
data->block[0],
FT260_FLAG_START_STOP_REPEATED);
} else {
if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
ret = -EINVAL;
goto smbus_exit;
}
ret = ft260_smbus_write(dev, addr, cmd, data->block + 1,
data->block[0],
FT260_FLAG_START_STOP);
}
break;
// ...
}
```
Reply: HID: ft260: fix stack-out-of-bounds in ft260_smbus_xfer
The ft260_smbus_xfer function in drivers/hid/hid-ft260.c handles SMBus
block transfers by using the length byte provided by the user
(data->block[0]). However, it fails to validate that this length is
within the valid range (<= I2C_SMBUS_BLOCK_MAX).
This missing check allows a user to supply a large length (e.g., 42),
which leads to a stack-out-of-bounds read in ft260_smbus_write when
processing an I2C_SMBUS_I2C_BLOCK_DATA or I2C_SMBUS_BLOCK_DATA
transaction. The data->block buffer is part of the union i2c_smbus_data
allocated on the stack in i2cdev_ioctl_smbus, so reading past it
triggers KASAN.
Fix this by ensuring data->block[0] does not exceed I2C_SMBUS_BLOCK_MAX
(32) before proceeding with the transfer. This validation is added for
both read and write operations of block and I2C block transactions.
|
| 49/2 |
2026/01/30 14:52 |
llm |
description-generator |
0m
Model:
Tokens:
input: 8634
output: 151
thoughts: 547
Thoughts: **Kernel Bug Fix Commit Description Thoughts**
Okay, so I need to craft a precise commit description for this kernel bug fix. It's a KASAN stack-out-of-bounds read in `ft260_smbus_write` that's being called from `ft260_smbus_xfer`. I've got the crash report, my root cause analysis, and the fix, along with the diff. This should be straightforward, but I want to make sure I get the details spot on.
First things first, I need to verify a few assumptions before putting the commit message together. I'm going to pull up `drivers/hid/hid-ft260.c` and make sure my understanding of the relevant code around `ft260_smbus_xfer` is accurate and that I have the right context. I also need to confirm the definition of `union i2c_smbus_data`, especially the size and layout of the `block` field. Then, I should check the value of `I2C_SMBUS_BLOCK_MAX`; I'm pretty sure it's 32, but it's always good to be certain.
Most importantly, I need to be completely clear on the role of `data->block[0]`. The report says it's used as the length byte, but I need to fully understand if that's *always* true, and how itβs used in both read and write operations. The root cause analysis indicates:
* For `I2C_SMBUS_I2C_BLOCK_DATA` WRITE, `data->block[0]` is indeed the length. `data->block[1...]` is the data itself.
* For `I2C_SMBUS_BLOCK_DATA` WRITE, the documentation states that the actual `data->block` is passed, implying length is included in the write buffer. In this case, `data->block[0]` is also the length.
Now, the fix also adds checks for READ. I need to be sure what `data->block[0]` means here as well. Is it the expected length to read for `I2C_SMBUS_I2C_BLOCK_DATA`, or is it the length of the command itself? I need to clarify this. Maybe looking into the documentation for `i2c_smbus_xfer`, or the implementation in `i2c-core-smbus.c`, will help clear it up.
Alright, let's start with `drivers/hid/hid-ft260.c` and go from there.
|
| 50/2 |
2026/01/30 14:53 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:drivers/hid/hid-ft260.c IncludeLines:true Name:ft260_smbus_xfer]
Results: map[SourceCode: 652: static int ft260_smbus_xfer(struct i2c_adapter *adapter, u16 addr, u16 flags,
653: char read_write, u8 cmd, int size,
654: union i2c_smbus_data *data)
655: {
656: int ret;
657: struct ft260_device *dev = i2c_get_adapdata(adapter);
658: struct hid_device *hdev = dev->hdev;
659:
660: ft260_dbg("smbus size %d\n", size);
661:
662: mutex_lock(&dev->lock);
663:
664: ret = hid_hw_power(hdev, PM_HINT_FULLON);
665: if (ret < 0) {
666: hid_err(hdev, "power management error: %d\n", ret);
667: mutex_unlock(&dev->lock);
668: return ret;
669: }
670:
671: switch (size) {
672: case I2C_SMBUS_BYTE:
673: if (read_write == I2C_SMBUS_READ)
674: ret = ft260_i2c_read(dev, addr, &data->byte, 1,
675: FT260_FLAG_START_STOP);
676: else
677: ret = ft260_smbus_write(dev, addr, cmd, NULL, 0,
678: FT260_FLAG_START_STOP);
679: break;
680: case I2C_SMBUS_BYTE_DATA:
681: if (read_write == I2C_SMBUS_READ) {
682: ret = ft260_smbus_write(dev, addr, cmd, NULL, 0,
683: FT260_FLAG_START);
684: if (ret)
685: goto smbus_exit;
686:
687: ret = ft260_i2c_read(dev, addr, &data->byte, 1,
688: FT260_FLAG_START_STOP_REPEATED);
689: } else {
690: ret = ft260_smbus_write(dev, addr, cmd, &data->byte, 1,
691: FT260_FLAG_START_STOP);
692: }
693: break;
694: case I2C_SMBUS_WORD_DATA:
695: if (read_write == I2C_SMBUS_READ) {
696: ret = ft260_smbus_write(dev, addr, cmd, NULL, 0,
697: FT260_FLAG_START);
698: if (ret)
699: goto smbus_exit;
700:
701: ret = ft260_i2c_read(dev, addr, (u8 *)&data->word, 2,
702: FT260_FLAG_START_STOP_REPEATED);
703: } else {
704: ret = ft260_smbus_write(dev, addr, cmd,
705: (u8 *)&data->word, 2,
706: FT260_FLAG_START_STOP);
707: }
708: break;
709: case I2C_SMBUS_BLOCK_DATA:
710: if (read_write == I2C_SMBUS_READ) {
711: ret = ft260_smbus_write(dev, addr, cmd, NULL, 0,
712: FT260_FLAG_START);
713: if (ret)
714: goto smbus_exit;
715:
716: ret = ft260_i2c_read(dev, addr, data->block,
717: data->block[0] + 1,
718: FT260_FLAG_START_STOP_REPEATED);
719: } else {
720: ret = ft260_smbus_write(dev, addr, cmd, data->block,
721: data->block[0] + 1,
722: FT260_FLAG_START_STOP);
723: }
724: break;
725: case I2C_SMBUS_I2C_BLOCK_DATA:
726: if (read_write == I2C_SMBUS_READ) {
727: ret = ft260_smbus_write(dev, addr, cmd, NULL, 0,
728: FT260_FLAG_START);
729: if (ret)
730: goto smbus_exit;
731:
732: ret = ft260_i2c_read(dev, addr, data->block + 1,
733: data->block[0],
734: FT260_FLAG_START_STOP_REPEATED);
735: } else {
736: ret = ft260_smbus_write(dev, addr, cmd, data->block + 1,
737: data->block[0],
738: FT260_FLAG_START_STOP);
739: }
740: break;
741: default:
742: hid_err(hdev, "unsupported smbus transaction size %d\n", size);
743: ret = -EOPNOTSUPP;
744: }
745:
746: smbus_exit:
747: hid_hw_power(hdev, PM_HINT_NORMAL);
748: mutex_unlock(&dev->lock);
749: return ret;
750: }
SourceFile:drivers/hid/hid-ft260.c]
|
| 51/2 |
2026/01/30 14:53 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:include/uapi/linux/i2c.h IncludeLines:true Name:i2c_smbus_data]
Results: map[SourceCode: 142: union i2c_smbus_data {
143: __u8 byte;
144: __u16 word;
145: __u8 block[I2C_SMBUS_BLOCK_MAX + 2]; /* block[0] is used for length */
146: /* and one more for user-space compatibility */
147: };
SourceFile:include/uapi/linux/i2c.h]
|
| 52/2 |
2026/01/30 14:53 |
tool |
codesearch-definition-source |
0m
Error:
Args: map[ContextFile:include/uapi/linux/i2c.h IncludeLines:true Name:I2C_SMBUS_BLOCK_MAX]
Results: map[SourceCode: SourceFile:]
|
| 53/2 |
2026/01/30 14:53 |
llm |
description-generator |
0m
Model:
Tokens:
input: 11779
output: 278
thoughts: 4103
Thoughts: **Analysis of the FT260 SMBus Block Transfer Vulnerability**
Okay, so the issue here is with the FT260 driver's handling of SMBus block transfers. `I2C_SMBUS_BLOCK_MAX` is typically 32 bytes, as confirmed by the header file and the `i2c_smbus_data` structure. The crash is happening in `ft260_smbus_write` which uses `memcpy`, and the `ft260_smbus_xfer` function takes a `union i2c_smbus_data`. The crash report shows a read of size 42, pointing to an out-of-bounds access.
The core of the problem lies in `ft260_smbus_xfer`. It uses `data->block[0]` as the length of the block data for both `I2C_SMBUS_BLOCK_DATA` and `I2C_SMBUS_I2C_BLOCK_DATA`. The issue is, it *doesn't* validate this length. In the `I2C_SMBUS_I2C_BLOCK_DATA` case, it's writing to the device, but in the `I2C_SMBUS_BLOCK_DATA` read, the driver *trusts* the user provided length, `data->block[0]`, which leads to an out-of-bounds write on the stack in `ft260_i2c_read`.
If `data->block[0]` is larger than `I2C_SMBUS_BLOCK_MAX`, which is 32, the driver will try to read past the end of the `block` buffer, which is 34 bytes in the `union i2c_smbus_data`. It is a write out of bounds on the stack in `ft260_i2c_read`. It can be reproduced by a large write to the device too.
The fix, and a good one at that, is to validate that `data->block[0]` is <= `I2C_SMBUS_BLOCK_MAX` before the transfer. This needs to be done for *both* read and write operations in both cases. The crash report mentioned a read, which is indeed a memcpy (a write).
This is a user input validation issue. The code needs to check that `data->block[0]` is a reasonable value. The `data->block[0]` value comes from user space, because of how `i2cdev_ioctl_smbus` works.
I've verified that the fix makes this check. It covers both read and write and it correctly uses `I2C_SMBUS_BLOCK_MAX`. It protects against potential corruption and the reported crash. It will prevent a stack buffer overflow.
|