How to Build a Single Kernel Module (Driver) in Ubuntu

When I bought UNI-T UT61E digital multimeter,  I planned to to use the open source tool Sigrok to capture voltage, current or resistance data with my Ubuntu 14.04 machine. Unfortunately, it was just not working for me and I kept getting some “Invalid function byte” error messages, so I asked on Sigrok mailing list, and since it worked for others, I was in big troubles. It turned out the RS-232 to USB dongle I used:


was most likely to culprit, so I decided to buy another random USB to serial dongle on eBay, and after a few weeks I received it only to find out it had the exact same VID:PID, so I was out of luck. Finally, I gave up on Sigrok on Linux, and tested the power consumption of some Rockchip RK3288 & Amlogic S802 devices in a Windows 7 virtual machine running in my Ubuntu 14.04 host PC. A few days later, Karl Parsonn left a comment saying ch341 driver just ignored parity (UNI-T DMM use odd parity), but that he wrote a patch that should eventually make it to mainline.

Ubuntu_14.04_CH341_Driver

Since I did not want to wait I decided to build ch341.ko with the patched driver, and I can now use Sigrok with my HL-340 USB-Serial adapter successfully, but I’ll write more about that in another post, and today, I’ll document the steps I followed to build the driver for my machine.

Instructions to build a kernel module in Ubuntu are already available, but the patch for ch341 driver is for Linux 3.18.6, and Ubuntu 14.04 comes with Linux 3.13. So I had two choices:

  1. Backport the driver to Linux 3.13
  2. Upgrade my Ubuntu kernel to 3.18

I decided to go with option 1 at first, but I quickly changed my mind as there’s been a bit too many modifications between the two versions, and it started to be time consuming. So it was time for plan B, or rather option 2. I could probably have use the Karl’s Linux kernel, but I remembered having read that Canonical makes some changes to the Linux kernel, so instead I went to http://kernel.ubuntu.com/~kernel-ppa/mainline/ to download and install Linux 3.18 in my machine (This is 3.18.0, but with hindsights I should have gone with 3.18.6 instead, but it still worked):


This steps requires a reboot. In case it does not boot at all, you should be able to boot one of the older Linux kernel via grub, and remove the new mainline kernel, so I believe this step is relatively safe.

Then I checkout the Linux kernel and downloaded and applied Ubuntu specific patchsets:


Backup the current driver:


Then I mostly followed the build instructions found on Askubuntu to setup the kernel to build modules:


and update the code with the new driver, build and install the module:


Finally run depmod, unload the old module if needed, and load the newly built module.


Done. I’ve now got a CH341 driver that supports no parity or odd/even parities.

Share this:

Support CNX Software! Donate via cryptocurrencies, become a Patron on Patreon, or purchase goods on Amazon or Aliexpress

ROCK 5 ITX Rockchip RK3588 mini-ITX motherboard
Subscribe
Notify of
guest
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.
9 Comments
oldest
newest
Karl Johnson
Karl Johnson
9 years ago

I would really like to learn how to do this for my android boxes/sticks. Good stuff

Zoobab
9 years ago

This bug should fixed upstream. Actually I am reversing some chinese windows software in order to toggle gpios on ch341. There are plenty of bios flashers on aliexpress for some 3eur or so based on that chip, wheremost of the gpios are used. See also my page here:

http://www.zoobab.com/ch341-usb-spi-i2c-uart-isp-dongle

Radtoo
Radtoo
9 years ago

Thanks for the patch Zoobab + thanks for the post cnxsoft!

zoobab
9 years ago

This tool might be also of any help for Ubuntu/debian kernel modules:

https://wiki.debian.org/ModuleAssistant

zoobab
9 years ago

http://ftp.netbsd.org/pub/NetBSD/NetBSD-release-5/src/sys/dev/usb/uchcom.c

“driver for WinChipHead CH341/340, the worst USB-serial chip in the world.”

Boardcon Rockchip RK3588S SBC with 8K, WiFI 6, 4G LTE, NVME SSD, HDMI 2.1...