Skip to content

Instantly share code, notes, and snippets.

@dattasaurabh82
Last active September 17, 2024 21:52
Show Gist options
  • Save dattasaurabh82/082d13fd61c0d06c7a358c5e605ce4fd to your computer and use it in GitHub Desktop.
Save dattasaurabh82/082d13fd61c0d06c7a358c5e605ce4fd to your computer and use it in GitHub Desktop.
Fix Driver issues for CH340N usb serial chip in Linux (Ubuntu 22.04)

Tested on Ubuntu 20, 21 earlier and they were not working. So upgraded Ubutbu to latest 22 version.

The issue is: The built in Kernals of Ubuntu and many Linux Kernals (between ) have the CH340 drivers broken / have bugs and not fixed (from kernal 5.4.0-87 and above)

Note: I didn't try on linux kernal 5.4.0-86 my self. I though I will see how is the situation in the latest linux kernal (as of 16/05/2022)

Instructions for Ubuntu 22.04 LTS x86_64

Get the kernel verison installed:

sudo apt-get update -y
sudo apt-get install neofetch -y
neofetch

I think it came with Kernel: 5.13.8-...

Instal latest kernel (GUI solution):

sudo add-apt-repository ppa:cappelikan/ppa
sudo apt-get update -y
sudo apt-get install mainline -y
sudo apt autoremove -y

Run mainline and select the latest kernel (I tried, as of 16/05/2022, kernel version: 5.17.8-051708-generic)

After installation and reboot, run neofetch again and check if the latest linux kernel is loaded.

Install build tools (if not installed already):

sudo apt-get update -y
sudo apt-get install build-essential dwarves dkms make cmake -y
sudo apt autoremove -y

Install and fix CH34x drivers:

As per the guide here in sparkfun and https://github.com/juliagoda/CH341SER

git clone https://github.com/juliagoda/CH341SER
cd CH341SER
sudo make clean 

# The Makefile looks for vmlinux in a certain location (check that cat Makefile)
# we need to move that
# https://forum.proxmox.com/threads/kernel-module-not-found-when-compile-skipping-btf-generation.100974/
cp /sys/kernel/btf/vmlinux /usr/lib/modules/`uname -r`/build/

sudo make
sudo make load
lsmod | grep ch34*

# NOTE: if other/prev ch34x drivers are loaded (and you know they were not working), then you can unload them by:
sudo rmmod ch34x

# Plug and unplug your CH340 device again on the USB port
sudo dmesg

# plug un-plug your device and then verify:
ls /dev/tty*

# make it accessible
sudo usermod -a -G dialout $(whoami)
sudo chmod a+rw /dev/ttyUSB0 # if /dev/ttyUSB0 was assigned

# To unload:
sudo rmmod ch34x
# or 
sudo make unload

Disable some conflicting services.

It came out during my test, at least on my machine, a service for some external brail hardware was conflicting with the drivers. I saw that in sudo dmesg

... ch340 1-10:1.0: ch341-uart converter detected
... usb 1-10: ch341-uart converter now attached to ttyUSB0
.
.
.
... usb 1-10: usbfs: interface 0 claimed by ch341 while 'brltty' sets config #1
... ch340-uart ttyUSB0: ch341-uart converter now disconnected from ttyUSB0

It was apearing and dis-appearing. so I tried to hunt the service as such:

sudo systemctl list-units | grep brltty

I then disabled it:

sudo systemctl stop <brl service name or other service that is interferring and not needed>
sudo systemctl disable <brl service name or other service that is interferring and not needed>
sudo systemctl mask <brl service name or other service that is interferring and not needed>

for f in /usr/lib/udev/rules.d/*brltty*.rules; do
    sudo ln -s /dev/null "/etc/udev/rules.d/$(basename "$f")"
done
sudo udevadm control --reload-rules

Future note:

Automate loading and unloading ...

@krishnak
Copy link

For latest Ubuntu 22.04 version just do this as described below

https://askubuntu.com/questions/1403705/dev-ttyusb0-not-present-in-ubuntu-22-04

@Ruckus83
Copy link

Ruckus83 commented Dec 4, 2022

This thread looked like it was going to solve all my problems but I've hit a snag with the make file.

the cp function seemed to work, mine went to cp /sys/kernel/btf/vmlinux /usr/lib/modules/6.0.9-060009-generic/build/

but then sudo make results in errors

make -C /lib/modules/6.0.9-060009-generic/build M=/home/carterk/CH341SER
make[1]: Entering directory '/usr/src/linux-headers-6.0.9-060009-generic'
warning: the compiler differs from the one used to build the kernel
The kernel was built by: x86_64-linux-gnu-gcc-12 (Ubuntu 12.2.0-9ubuntu1) 12.2.0
You are using: gcc (Ubuntu 11.3.0-1ubuntu1~22.04) 11.3.0
CC [M] /home/carterk/CH341SER/ch34x.o
gcc: error: unrecognized command-line option ‘-ftrivial-auto-var-init=zero’
make[2]: *** [scripts/Makefile.build:249: /home/carterk/CH341SER/ch34x.o] Error 1
make[1]: *** [Makefile:1858: /home/carterk/CH341SER] Error 2
make[1]: Leaving directory '/usr/src/linux-headers-6.0.9-060009-generic'
make: *** [Makefile:7: default] Error 2

Which seems basically to be the exact same issue I've been battling with across two different machines and a couple of fresh installations. Suggestions?

@akafael
Copy link

akafael commented Jun 4, 2023

Thank you so much!

/dev/ttyUSB0 was not showing on Ubuntu 22.04 for me. This tutorial was quite helpful to find the solution.

I have used the same command sudo dmesh -c to detect the issue, and then I decided to just remove BRLTTY since it is something that I would not be using anyway:

sudo apt purge brltty
sudo rm -rv /var/lib/BrlAPI/

@Tezcan98
Copy link

thank you so much bro

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment