Linux 驱动
CH9344SER_LINUX/LINUX$ tree
.
├── demo
│ └── ch9344_demo_gpio.c
├── driver
│ ├── ch9344.c
│ ├── ch9344.h
│ └── Makefile
├── lib
│ ├── ch9344_lib.c
│ └── ch9344_lib.h
└── README.md
驱动下载
sudo snap install curl
curl -o CH9344SER_LINUX.ZIP http://rk3588image.wif.ink/driver/ch348/CH9344SER_LINUX.ZIP
unzip CH9344SER_LINUX.ZIP
内核升级
sudo yum update kernel
reboot
编译环境准备
sudo yum install kernel-headers-3.10.0-1160.119.1.el7.x86_64
sudo yum install kernel-devel-3.10.0-1160.119.1.el7.x86_64
编译失败 1
[root@localhost driver]# make
make -C /lib/modules/3.10.0-1160.119.1.el7.x86_64/build M=/root/LINUX/driver
make[1]: 进入目录“/usr/src/kernels/3.10.0-1160.119.1.el7.x86_64”
arch/x86/Makefile:96: stack-protector enabled but compiler support broken
arch/x86/Makefile:169: *** CONFIG_RETPOLINE=y, but not supported by the compiler. Compiler update recommended.。 停止。
make[1]: 离开目录“/usr/src/kernels/3.10.0-1160.119.1.el7.x86_64”
解决方案
vim /usr/src/kernels/3.10.0-1160.119.1.el7.x86_64/include/config/auto.conf
vim /usr/src/kernels/3.10.0-1160.119.1.el7.x86_64/.config
注释掉 CONFIG_RETPOLINE=y
编译失败 2
[root@localhost driver]# make
make -C /lib/modules/3.10.0-1160.119.1.el7.x86_64/build M=/root/LINUX/driver
make[1]: 进入目录“/usr/src/kernels/3.10.0-1160.119.1.el7.x86_64”
arch/x86/Makefile:96: stack-protector enabled but compiler support broken
Makefile:648: Cannot use CONFIG_CC_STACKPROTECTOR_STRONG: -fstack-protector-strong not supported by compiler
make[1]: gcc:命令未找到
LD /root/LINUX/driver/built-in.o
CC [M] /root/LINUX/driver/ch9344.o
/bin/sh: gcc: 未找到命令
make[2]: *** [/root/LINUX/driver/ch9344.o] 错误 127
make[1]: *** [_module_/root/LINUX/driver] 错误 2
make[1]: 离开目录“/usr/src/kernels/3.10.0-1160.119.1.el7.x86_64”
make: *** [default] 错误 2
解决方案
sudo yum -y install gcc
Centos 7 驱动安装失败
[root@localhost driver]# make install
make -C /lib/modules/3.10.0-957.el7.x86_64/build M=/root/LINUX/driver
make[1]: 进入目录“/usr/src/kernels/3.10.0-1160.119.1.el7.x86_64”
Building modules, stage 2.
MODPOST 1 modules
make[1]: 离开目录“/usr/src/kernels/3.10.0-1160.119.1.el7.x86_64”
insmod ch9344.ko || true
insmod: ERROR: could not insert module ch9344.ko: Invalid parameters
mkdir -p /lib/modules/3.10.0-957.el7.x86_64/kernel/drivers/usb/serial/ || true
cp -f ./ch9344.ko /lib/modules/3.10.0-957.el7.x86_64/kernel/drivers/usb/serial/ || true
depmod -a
depmod: ERROR: fstatat(3, buildbak): No such file or directory
问题原因: 当前系统内核版本和编译驱动使用的内核版本不一致导致
- 当前运行的内核版本: 3.10.0-957.el7.x86_64
- 编译驱动使用的内核版本: 3.10.0-1160.119.1.el7.x86_64
[root@localhost lib]# rpm -qa | grep kernel
kernel-headers-3.10.0-1160.119.1.el7.x86_64
kernel-tools-3.10.0-957.el7.x86_64
kernel-3.10.0-957.el7.x86_64
kernel-devel-3.10.0-1160.119.1.el7.x86_64
abrt-addon-kerneloops-2.1.11-52.el7.centos.x86_64
kernel-tools-libs-3.10.0-957.el7.x86_64
解决方案: 升级内核版本
[root@localhost lib]# sudo yum update kernel
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* centos-sclo-rh: mirrors.aliyun.com
* centos-sclo-sclo: mirrors.aliyun.com
* epel: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
正在解决依赖关系
--> 正在检查事务
---> 软件包 kernel.x86_64.0.3.10.0-1160.119.1.el7 将被 安装
升级后命令查看内核版本
[root@localhost ~]# rpm -qa | grep kernel
kernel-headers-3.10.0-1160.119.1.el7.x86_64
kernel-tools-3.10.0-957.el7.x86_64
kernel-3.10.0-1160.119.1.el7.x86_64
kernel-3.10.0-957.el7.x86_64
kernel-devel-3.10.0-1160.119.1.el7.x86_64
abrt-addon-kerneloops-2.1.11-52.el7.centos.x86_64
kernel-tools-libs-3.10.0-957.el7.x86_64
编译驱动并安装
[root@localhost driver]# make clean
rm -rf *.mk .tmp_versions Module.symvers *.mod.c *.o *.ko .*.cmd Module.markers modules.order *.a *.mod
[root@localhost driver]# make
make -C /lib/modules/3.10.0-1160.119.1.el7.x86_64/build M=/root/LINUX/driver
make[1]: 进入目录“/usr/src/kernels/3.10.0-1160.119.1.el7.x86_64”
LD /root/LINUX/driver/built-in.o
CC [M] /root/LINUX/driver/ch9344.o
Building modules, stage 2.
MODPOST 1 modules
CC /root/LINUX/driver/ch9344.mod.o
LD [M] /root/LINUX/driver/ch9344.ko
make[1]: 离开目录“/usr/src/kernels/3.10.0-1160.119.1.el7.x86_64”
[root@localhost driver]# make install
make -C /lib/modules/3.10.0-1160.119.1.el7.x86_64/build M=/root/LINUX/driver
make[1]: 进入目录“/usr/src/kernels/3.10.0-1160.119.1.el7.x86_64”
Building modules, stage 2.
MODPOST 1 modules
make[1]: 离开目录“/usr/src/kernels/3.10.0-1160.119.1.el7.x86_64”
insmod ch9344.ko || true
mkdir -p /lib/modules/3.10.0-1160.119.1.el7.x86_64/kernel/drivers/usb/serial/ || true
cp -f ./ch9344.ko /lib/modules/3.10.0-1160.119.1.el7.x86_64/kernel/drivers/usb/serial/ || true
depmod -a
[root@localhost ~]# ls /dev/ttyCH9344USB*
/dev/ttyCH9344USB0 /dev/ttyCH9344USB1 /dev/ttyCH9344USB2 /dev/ttyCH9344USB3 /dev/ttyCH9344USB4 /dev/ttyCH9344USB5 /dev/ttyCH9344USB6 /dev/ttyCH9344USB7
配置设备重启后驱动自动加载
sudo nano /etc/systemd/system/ch9344.service
[Unit]
Description=Load Ch9344 Kernel Module
After=local-fs.target
[Service]
Type=oneshot
ExecStart=/sbin/modprobe ch9344
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target
sudo systemctl daemon-reload
sudo systemctl enable ch9344.service
sudo systemctl start ch9344.service
[root@localhost modules]# sudo systemctl daemon-reload
[root@localhost modules]# sudo systemctl enable ch9344.service
Created symlink from /etc/systemd/system/multi-user.target.wants/ch9344.service to /etc/systemd/system/ch9344.service.
ubuntu 22.04
sudo apt-get install build-essential linux-headers-$(uname -r)
报错1
make install
make -C /lib/modules/6.1.57/build M=/home/pi/LINUX/driver
make[1]: *** /lib/modules/6.1.57/build: No such file or directory. Stop.
make: *** [Makefile:5: default] Error 2
报错2
pi@bogon:~/LINUX/driver$ sudo apt-get install build-essential linux-headers-$(uname -r)
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
E: Unable to locate package linux-headers-6.1.57
E: Couldn't find any package by glob 'linux-headers-6.1.57'
E: Couldn't find any package by regex 'linux-headers-6.1.57'
最终失败的解决方案
安装linux-headers-6.1.57
sudo apt update
sudo apt upgrade
wget https://cdn.kernel.org/pub/linux/kernel/v6.x/linux-6.1.57.tar.xz
wget http://rk3588image.wif.ink/driver/linux-6.1.57.tar.xz
sudo cp linux-6.1.57.tar.xz /usr/src/
cd /usr/src/
sudo tar xvf linux-6.1.57.tar.xz
sudo make olddefconfig
pi@bogon:~/LINUX/linux-6.1.57$ make olddefconfig
LEX scripts/kconfig/lexer.lex.c
/bin/sh: 1: flex: not found
make[1]: *** [scripts/Makefile.host:9: scripts/kconfig/lexer.lex.c] Error 127
make: *** [Makefile:704: olddefconfig] Error 2
sudo apt-get install flex
sudo apt-get install bison
sudo apt-get install libssl-dev
sudo make modules_prepare
sudo make headers
sudo apt-get install rsync
sudo make headers_install
sudo ln -sf /usr/src/linux-6.1.57/ /lib/modules/$(uname -r)/build
编译警告
pi@NanoPi-R6C:~/LINUX/driver$ make
make -C /lib/modules/6.1.57/build M=/home/pi/LINUX/driver
make[1]: Entering directory '/usr/src/linux-6.1.57'
CC [M] /home/pi/LINUX/driver/ch9344.o
MODPOST /home/pi/LINUX/driver/Module.symvers
WARNING: Module.symvers is missing.
Modules may not have dependencies or modversions.
You may get many unresolved symbol warnings.
WARNING: modpost: "_raw_spin_lock_irqsave" [/home/pi/LINUX/driver/ch9344.ko] undefined!
WARNING: modpost: "_raw_spin_unlock_irqrestore" [/home/pi/LINUX/driver/ch9344.ko] undefined!
WARNING: modpost: "__tty_alloc_driver" [/home/pi/LINUX/driver/ch9344.ko] undefined!
WARNING: modpost: "tty_std_termios" [/home/pi/LINUX/driver/ch9344.ko] undefined!
WARNING: modpost: "tty_register_driver" [/home/pi/LINUX/driver/ch9344.ko] undefined!
WARNING: modpost: "usb_register_driver" [/home/pi/LINUX/driver/ch9344.ko] undefined!
WARNING: modpost: "tty_unregister_driver" [/home/pi/LINUX/driver/ch9344.ko] undefined!
WARNING: modpost: "tty_driver_kref_put" [/home/pi/LINUX/driver/ch9344.ko] undefined!
WARNING: modpost: "_printk" [/home/pi/LINUX/driver/ch9344.ko] undefined!
WARNING: modpost: "usb_bulk_msg" [/home/pi/LINUX/driver/ch9344.ko] undefined!
WARNING: modpost: suppressed 81 unresolved symbol warnings because there were too many)
CC [M] /home/pi/LINUX/driver/ch9344.mod.o
LD [M] /home/pi/LINUX/driver/ch9344.ko
make[1]: Leaving directory '/usr/src/linux-6.1.57'
最终因为头文件编译不完整导致此方案失败
友善官方文档提供的解决方案
更换软件源为国内镜像源
sudo cp /etc/apt/sources.list /etc/apt/sources.list.org
sudo sed -i -e ‘s/ports.ubuntu.com/mirrors.ustc.edu.cn/g’ /etc/apt/sources.list
更新软件包列表
sudo apt-get update
安装软件中心
sudo apt-get install snapd sudo snap install snap-store
安装内核头文件
sudo dpkg -i /opt/archives/linux-headers-6.1.57_6.1.57-13_arm64.deb