交叉编译ARM下的CH9102X驱动

官方原始源码编译

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
make -C ~/linux  M=~/ch343ser_linux/driver modules
make[1]: Entering directory '~/linux'

CC [M] ~/ch343ser_linux/driver/ch343.o
~/ch343ser_linux/driver/ch343.c:1684:17: error: initialization of ‘unsigned int (*)(struct tty_struct *)’ from incompatible pointer type ‘int (*)(struct tty_struct *)’ [-Werror=incompatible-pointer-types]
1684 | .write_room = ch343_tty_write_room,
| ^~~~~~~~~~~~~~~~~~~~
~/ch343ser_linux/driver/ch343.c:1684:17: note: (near initialization for ‘ch343_ops.write_room’)
~/ch343ser_linux/driver/ch343.c:1686:21: error: initialization of ‘unsigned int (*)(struct tty_struct *)’ from incompatible pointer type ‘int (*)(struct tty_struct *)’ [-Werror=incompatible-pointer-types]
1686 | .chars_in_buffer = ch343_tty_chars_in_buffer,
| ^~~~~~~~~~~~~~~~~~~~~~~~~
~/ch343ser_linux/driver/ch343.c:1686:21: note: (near initialization for ‘ch343_ops.chars_in_buffer’)
~/ch343ser_linux/driver/ch343.c: In function ‘ch343_init’:
~/ch343ser_linux/driver/ch343.c:1699:21: error: implicit declaration of function ‘alloc_tty_driver’ [-Werror=implicit-function-declaration]
1699 | ch343_tty_driver = alloc_tty_driver(CH343_TTY_MINORS);
| ^~~~~~~~~~~~~~~~
~/ch343ser_linux/driver/ch343.c:1699:19: warning: assignment to ‘struct tty_driver *’ fromintmakes pointer from integer without a cast [-Wint-conversion]
1699 | ch343_tty_driver = alloc_tty_driver(CH343_TTY_MINORS);
| ^
~/ch343ser_linux/driver/ch343.c:1716:3: error: implicit declaration of function ‘put_tty_driver’ [-Werror=implicit-function-declaration]
1716 | put_tty_driver(ch343_tty_driver);
| ^~~~~~~~~~~~~~
cc1: some warnings being treated as errors
make[2]: *** [scripts/Makefile.build:277: ~/ch343ser_linux/driver/ch343.o] Error 1
make[1]: *** [Makefile:1868: ~/ch343ser_linux/driver] Error 2
make[1]: Leaving directory '~/linux'
make: *** [Makefile:6: default] Error 2

可以看到出现大量报错

WCH官方驱动 使用了废弃函数 会导致编译失败 这里使用Fork分支

修复分支编译

alloc_tty_driver -> tty_alloc_driver
put_tty_driver -> tty_driver_kref_put

驱动适用于 CH342/CH343/CH344/CH347/CH9101/CH9102/CH9103/CH9143
代码Git仓库地址 https://github.com/GreatestCapacity/ch343ser_linux

拉取分支

1
2
git clone https://github.com/GreatestCapacity/ch343ser_linux.git
cd ch343ser_linux

创建Makefile

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
KERNELDIR := ~/linux
# 使用你的Linux源码地址目录

PWD :=$(shell pwd)
obj-m := ch343.o

default:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
clean:
$(MAKE) -C $(KERNELDIR) M=$(PWD) clean
rm -rf *.mk .tmp_versions Module.symvers *.mod.c *.o *.ko .*.cmd Module.markers modules.order
load:
insmod ch343.ko
unload:
rmmod ch343
install: default
mkdir -p /lib/modules/$(shell uname -r)/kernel/drivers/usb/serial/
cp -f ./ch343.ko /lib/modules/$(shell uname -r)/kernel/drivers/usb/serial/
depmod -a
uninstall:
rm -rf /lib/modules/$(shell uname -r)/kernel/drivers/usb/serial/ch343.ko
depmod -a

执行编译

1
2
3
$ export CROSS_COMPILE=aarch64-linux-gnu-
$ export ARCH=arm64
$ make

载入文件

1
insmod ch343.ko

dmesg查看日志 可以看到已经成功载入并识别