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.

Support CNX Software - Donate via PayPal or become a Patron on Patreon

9
Leave a Reply

avatar
9 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
4 Comment authors
TBS5520 Multi-Standard Tuner Box and Linux DriversHow to Install Ubuntu 14.04 on Acer Aspire E5 LaptopSigrok and Pulseview in Ubuntu 14.04 with UNI-T UT61E Digital MultimeterzoobabRadtoo Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
Karl Johnson
Guest
Karl Johnson

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

Zoobab
Guest

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
Guest
Radtoo

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

zoobab
Guest

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

https://wiki.debian.org/ModuleAssistant

zoobab
Guest

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.”

trackback

[…] there’s now a patch, and I’ve shown how to build the driver for Ubuntu 14.04 with Linux kernel 3.18, and that part is working […]

trackback

[…] a USB mouse to the laptop, I also decided to upgrade the kernel, as I explained in my “build a kernel module in Ubuntu” post. I started with Linux kernel 3.19.2, as it was the latest stable kernel at the time, […]

trackback

[…] basically followed the instructions I wrote in “How to build a single kernel driver in Ubuntu“. I had already installed Linux 3.18.0 for this, but I first upgraded to a more recent Linux […]