Products > Programming

Compile a Linux kernel module

(1/8) > >>

RoGeorge:
The goal is to make a very small change in a .c file about the driver of a soundcard (CA0132), then to compile and make the current kernel to load the modified module.

I've googled already but I don't have a bird-eye view of the whole process, so I can not make sense of the details I found online about how to recompile the kernel (or just a single loadable module).  I could try one of the many recipes out there, and eventually one will work, but I would like to also understand what I'm doing.

I'm confused about
- where from to get the sources+headers, and what versions
- what differences are between using dkms-get sources vs. apt install
- also about using git vs downloading an archive with the sources

The OS is Ubuntu 20.04 LTS, AMD64, and I want to compile the module for the stable kernel version that is already installed, not for the nightly builds or for beta versions of kernels.

I would prefer to use git, so to be able to make a pull request in case the changes will be successful.

So far my plan is to add a new quirk definition for the ID of my card, which is "1102 0011", and by looking inside the Windows drivers, it's name seems to be "Sound Blaster Recon3Di", the chip is ca0132 Creative Sound Core3D.  It's the onboard sound for the motherboard AsRock model "Fatal1ty Z97 Professional".  I could see a Quirk in the sources for another Recon3Di card with the ID 1102 0010, and want to try the same quirk (a quirk here = custom settings) for my card ID with the ID 1102 0011.  If I could only flip that bit  ;D

Also, I don't really know what I'm doing, so any other advice (except buy a new soundcard) is welcome.



--- Code: ---~$ sudo apt update
~$ uname -a
Linux zub 5.15.0-41-generic #44~20.04.1-Ubuntu SMP Fri Jun 24 13:27:29 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux

~$ find / -path "*/wd8TB/*" -prune -o -iname "*ca0132*.ko" -print 2>/dev/null
/usr/lib/modules/5.13.0-52-generic/kernel/sound/pci/hda/snd-hda-codec-ca0132.ko
/usr/lib/modules/5.15.0-41-generic/kernel/sound/pci/hda/snd-hda-codec-ca0132.ko

~$ apt list linux-source*
Listing... Done
linux-source-5.4.0/focal-updates,focal-updates,focal-security,focal-security,now 5.4.0-122.138 all [installed,automatic]
linux-source/focal-updates,focal-updates,focal-security,focal-security,now 5.4.0.122.123 all [installed]

~$ uname -r
5.15.0-41-generic

--- End code ---

The file I want to modify is related with this bug that was fixed by Connor McAdams https://bugzilla.kernel.org/show_bug.cgi?id=109191#c66 and his fix is already in the kernel, except that my card has a slightly different ID which I need to add.

To be more precise, the file I plan to change corresponds to this one (but for my Unbuntu kernel/version):
https://github.com/torvalds/linux/blob/master/sound/pci/hda/patch_ca0132.c


My understanding so far is that Ubuntu kernel has particular patches and configs that are specific to Ubuntu, so I can not just use any 5.15.0-41-generic Linux, it has to be from Ubuntu, but I'm not sure.

What sources do I need, from where, and with what command(s) do I get them please?

ve7xen:
Ubuntu does apply their own patch sets, but these shouldn't change the kernel ABI, so a module built against the official source tree should (I think) still load in the corresponding Ubuntu kernel.

The process to build a single in-tree module is pretty straightforward. You will need the kernel source tree (e.g. from apt source) and your running kernel configuration file (in Ubuntu I believe this should be available as /boot/config-<version>). Place the config file as .config in the root of the source tree. Then you can simply do (from the root of the kernel source tree):


--- Code: ---make prepare
make modules_prepare
make M=sound/pci/hda/

--- End code ---

And you will find the .ko files in sound/pci/hda, which you can load or install manually. I believe you can also do
--- Code: ---make M=sound/pci/hda/ modules_install
--- End code ---
to install just that module, but I'm not totally sure about that.

magic:

--- Quote from: RoGeorge on July 22, 2022, 06:54:31 pm ---If I could only flip that bit  ;D
--- End quote ---
You can, with a hex editor, which may be the fastest option. Note that you are on a little-endian machine, so byte order in those numbers may be reversed.

Obtaining the source corresponding to your installed package, kernel or otherwise, is a Ubuntu-specific question which you need to solve somehow.

Configuration of the running kernel may be available in /proc/config.gz; if not, see if it's included in the Ubuntu kernel source package/tarball/repository; or install linux-headers (make sure it's same version as your kernel) and then look for /lib/modules/`uname -r`/build/.config .

If you have the full kernel source unpacked somewhere, I think it should be as simple as putting the .config there and

--- Code: ---make sound/pci/hda/snd-hda-codec-ca0132.ko
--- End code ---

I wouldn't try to install the modified module, just reload. If it refuses to unload, close processes using it.

I'm not 100% if the instructions above are corrrect, it seems ve7xen tries to build a module inside the kernel source directory as an out-of-tree module, I have never tried such things. Not sure if modules_install will put it under /extra/ or overwrite the distribution's module in /kernel/ (both are subdirectories of /lib/modules/`uname -r`/). Did I say I would just reload it without installing?

langwadt:
https://smallbusiness.chron.com/compile-modules-ubuntu-52622.html ?

RoGeorge:
Not sure if poking bits inside binaries would still work.  I suspect the modules nowadays are cryptographically signed, but I digress.

Back to Ubuntu  sources, got them like this:

--- Code: ---        # https://wiki.ubuntu.com/Kernel/Dev/KernelGitGuide
       
        mkdir kernel
        cd kernel

        # Ubuntu 20.04 LTS is named Focal Fossa, so the /focal at the end
        git clone git://git.launchpad.net/~ubuntu-kernel/ubuntu/+source/linux/+git/focal
        # !!! git downloaded 1.95 GiB of sources       
        cd focal
       
        uname -r
            5.15.0-41-generic
        # each version has a tag, to list all available versions
        git tag -l *5.15.0-41*
            Ubuntu-hwe-5.15-5.15.0-41.44_20.04.1
       
        # make a new branch 'temp' with the desired version
        git checkout -b temp Ubuntu-hwe-5.15-5.15.0-41.44_20.04.1
       
        # search for the patch to be modified
        find ./ -iname *ca0132*
            ./sound/pci/hda/patch_ca0132.c
            ./sound/pci/hda/ca0132_regs.h

--- End code ---

Navigation

[0] Message Index

[#] Next page

There was an error while thanking
Thanking...
Go to full version
Powered by SMFPacks Advanced Attachments Uploader Mod