Page 1 of 1

Need help compiling one module from source file

PostPosted: Apr 24th, '20, 01:04
by rolf
Howdy,
Code: Select all
[rolf@x570i kernel-5.5.15-3.mga7]$ uname -r
5.5.15-desktop-3.mga7
[rolf@x570i kernel-5.5.15-3.mga7]$ pwd
/usr/src/kernel-5.5.15-3.mga7
[rolf@x570i kernel-5.5.15-3.mga7]$ head Makefile
# SPDX-License-Identifier: GPL-2.0
VERSION = 5
PATCHLEVEL = 5
SUBLEVEL = 15
EXTRAVERSION = -desktop-3.mga7
NAME = Kleptomaniac Octopus

# *DOCUMENTATION*


I'm trying to patch cx23885-core.c and build the cx23885 kernel module used by my Hauppauge WinTV-QuadHD pcie tuner, as instructed by Sean Young:
https://www.spinics.net/lists/linux-med ... 68027.html

I've applied the patch and replaced /usr/src/kernel-5.5.15-3.mga7/drivers/media/pci/cx23885/cx23885-core.c with the patched version.

Searching online for a recipe and finding https://wiki.archlinux.org/index.php/Co ... ompilation , I've started by editing /usr/src/kernel-5.5.15-3.mga7/Makefile to try to get EXTRAVERSION what it needs to be, as above.

Now, I don't know if that is correct and don't know the next steps. I also found, by searching here, several posts with Stephane's looking closest to what I'm trying to do: viewtopic.php?f=8&t=11608&p=67657

So far, modest changes to cx23885-core.c and a Makefile that can be reverted and I think it's time to ask for help!
Thanks.

Re: Need help compiling one module from source file

PostPosted: Apr 25th, '20, 08:36
by jiml8
There are several ways to go about this and which is appropriate depends on your goal.

If you only want to fool around with this one driver, it might be worth your while to cd into the driver source directory and build a Makefile for it that will compile the driver and only the driver.

If on the other hand, you plan to fiddle with the kernel, you will need to set up the kernel build system. While this is simplest from a user perspective, it will take a long time to run (depending on your processor, could be hours) and will use a lot of hard drive space (many GB probably). At the end, though, you will have your module compiled, and you don't have to install the rest of the kernel :D

To build the entire kernel, as root, cd into the source directory, and setup the build system:
cd /usr/src/kernel-5.5.15-3.mga7
make menuconfig


This will give you a menu for a default config of the kernel. step through the various options to turn on what you want turn on. Make sure the module you want is turned on and specified to be built as a module. Save this config.

Then execute the command:
make modules


And wait. After awhile, all your modules will be tuilt, including the one you want. You can then move that module into /lib/modules and load it into your kernel. Note that if you have specified a customversion for your kernel, you will have to load the kernel you build, in which case you can't do just a make modules, you have to do a make as well (make && make modules) to get everything.

If I were doing it, I would just compile the driver. The Makefile in the driver directory won't let you do that, so you have to build one that will. Don't discard the one that is there, rename it. I would do something like "mv Makefile Makefile.orig", then create my new Makefile.

I looked in that directory, and there are a lot of source files. I don't know if you need all of them for your driver; that is for you to figure out. But, here is a Makefile that you can use as a template that will compile a driver by itself, and give you a module that can be loaded into your running kernel:
MODULE_NAME = asix

SRC := asix_devices.c asix_common.c ax88172a.c

KDIR := /lib/modules/$(shell uname -r)/build
obj-m := $(MODULE_NAME).o
$(MODULE_NAME)-objs = $(SRC:.c=.o)

PWD := $(shell pwd)
EXTRA_CFLAGS += -I${PWD}/include

all:
$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules



Obviously, this Makefile won't build your driver; it builds one that I modified extensively awhile back. However, this template is the one you need. Put your source files in here in place of the asix stuff and when you have it right your driver will build. From the existing Makefile in the directory I think you will wind up with 2 modules, cx23885.ko and altera-ci.ko

Truthfully, I have not built a linux kernel in a long time. Pretty much all the kernel work I do today is in freebsd, so I will tell you I am rusty at building linux.

But this should get you started, and should also provide you with enough fodder to find the details you need in google.

Re: Need help compiling one module from source file

PostPosted: Apr 26th, '20, 15:34
by rolf
Thanks, jiml8.
I'll follow up on your helpful direction shortly.

Re: Need help compiling one module from source file

PostPosted: Apr 26th, '20, 17:31
by rolf
Since the patch was to drivers/media/pci/cx23885/cx23885-core.c, I inferred this was the only file needed to be referenced in your template. I made a Makefile, below, and ran make in that directory. First, I had copied the directory from the kernel source tree to my home directory to see how that would go and got an error:
Code: Select all
Makefile:13: *** missing separator.  Stop.


Looking at google, I thought maybe putting a Tab at the beginning of the line was called for, did that, and moved to the kernel source tree, where I tried again, as root:

Code: Select all
[root@x570i cx23885]# pwd
/usr/src/kernel-5.5.15-3.mga7/drivers/media/pci/cx23885

[root@x570i cx23885]# ls
altera-ci.c    cx23885-alsa.c   cx23885-dvb.c   cx23885-input.c  cx23885-ir.h     cx23888-ir.c    netup-eeprom.h
altera-ci.h    cx23885-av.c     cx23885-f300.c  cx23885-input.h  cx23885-reg.h    cx23888-ir.h    netup-init.c
cimax2.c       cx23885-av.h     cx23885-f300.h  cx23885-ioctl.c  cx23885-vbi.c    Kconfig         netup-init.h
cimax2.h       cx23885-cards.c  cx23885.h       cx23885-ioctl.h  cx23885-video.c  Makefile
cx23885-417.c  cx23885-core.c   cx23885-i2c.c   cx23885-ir.c     cx23885-video.h  netup-eeprom.c

[root@x570i cx23885]# cat Makefile
MODULE_NAME = cx23885

SRC := cx23885-core.c

KDIR := /lib/modules/$(shell uname -r)/build
obj-m := $(MODULE_NAME).o
$(MODULE_NAME)-objs = $(SRC:.c=.o)

PWD := $(shell pwd)
EXTRA_CFLAGS += -I${PWD}/include

all:
        $(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules

[root@x570i cx23885]# make
make -C /lib/modules/5.5.15-desktop-3.mga7/build SUBDIRS=/usr/src/kernel-5.5.15-3.mga7/drivers/media/pci/cx23885 modules
make[1]: Entering directory '/usr/src/kernel-5.5.15-desktop-3.mga7'
Makefile:1387: *** insufficient number of arguments (1) to function 'addprefix'.  Stop.
make[1]: Leaving directory '/usr/src/kernel-5.5.15-desktop-3.mga7'
make: *** [Makefile:13: all] Error 2
[root@x570i cx23885]#


I hope the error of my ways is clear to those who speak the language. ;)

Back in the days of Tom Berger, I could follow his recipe to build a kernel from source to tweak a driver to work with my hardware. ISTR needing to do a step with kernel sources to "make it ready" to build modules but it's been 20 years and I haven't tried to build a kernel for just about that long.

Re: Need help compiling one module from source file

PostPosted: Apr 27th, '20, 00:39
by jiml8
The driver probably is built using multiple C files. You probably should compile all of them.
Makefile:13: *** missing separator. Stop.


This is a syntax error in your makefile.

Re: Need help compiling one module from source file

PostPosted: Apr 27th, '20, 03:05
by rolf
Ok. The Makefile is your template, which I copy/pasted and made some customizations, as I posted. Putting the tab at the beginning of line 13 got rid of that error.
Thanks.

Re: Need help compiling one module from source file

PostPosted: May 16th, '20, 00:05
by martinw
In case you're still stuck, here are the commands I recorded when I last needed to do this:
Code: Select all
mkdir -p ~/rpm/{BUILD,BUILDROOT,RPMS,SOURCES,SPECS,SRPMS}
rpm -i ftp://ftp.mirrorservice.org/pub/mageia/distrib/7/SRPMS/core/updates/kernel-5.4.2-1.mga7.src.rpm
cd ~/rpm/SPECS
rpmbuild -bp kernel.spec
cd ../BUILD/kernel-x86_64/linux-5.4/
# *** apply your patches here ***
cp /usr/src/kernel-5.4.2-desktop-1.mga7/.config .
cp /usr/src/kernel-5.4.2-desktop-1.mga7/Module.symvers .
sed -i 's/EXTRAVERSION =$/EXTRAVERSION = -desktop-1.mga7/' Makefile
make prepare
make modules_prepare
echo 5.4.2-desktop-1.mga7 > include/config/kernel.release
make -j8 -C . M=drivers/gpu/drm/nouveau
xz drivers/gpu/drm/nouveau/nouveau.ko
sudo cp drivers/gpu/drm/nouveau/nouveau.ko.xz /lib/modules/5.4.2-desktop-1.mga7/kernel/drivers/gpu/drm/nouveau/
sudo depmod -a
sudo dracut -f /boot/initrd-5.4.2-desktop-1.mga7.img 5.4.2-desktop-1.mga7

which saves the need to fiddle with makefiles and makes sure your new module has the correct vermagic string. Change kernel version and driver path/name to suit your needs.

Re: Need help compiling one module from source file

PostPosted: May 16th, '20, 01:11
by rolf
Yeah, I've been busy with a roof repair as well as stuck; will give this a go when time allows, thanks!