Skip to content

Instantly share code, notes, and snippets.

@lvnilesh
Last active December 5, 2017 00:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lvnilesh/c9236a611a868ae192c64f44a96f92d0 to your computer and use it in GitHub Desktop.
Save lvnilesh/c9236a611a868ae192c64f44a96f92d0 to your computer and use it in GitHub Desktop.
fedora 25 workstation on macbook pro screen brightness, hybernation, suspend

https://www.denniskanbier.nl/blog/redhat/fedora-24-on-a-macbook-pro-115-custom-kernel/

Part Two – Patch a kernel module and testing it

This part describes the steps you need to take to apply a patch to a kernel module. In this example we’ll use the brightness key patch from https://bugzilla.kernel.org/show_bug.cgi?id=105051#c32

This tells us we want to apply this code to the file apple-gmux.c.orig.

We need the kernel source RPM (SRPM) from our current kernel. These can be found on the koji website.

To find out the current kernel:

uname -a

Linux pro 4.8.12-300.fc25.x86_64 #1 SMP Fri Dec 2 17:52:11 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux

In this case we need the SRPM for 4.8.12-300.fc25. Searching the koji page, we can find the SRPM on this page:

http://koji.fedoraproject.org/koji/buildinfo

In your homedir, create a directory structure to install the SRPM in:

mkdir -p rpmbuild/{BUILD,RPMS,SOURCES,SPECS,SRPMS}

Now download the SRPM file and install it:

wget https://kojipkgs.fedoraproject.org/packages/kernel/4.8.12/300.fc25/src/kernel-4.8.12-300.fc25.src.rpm
rpm -ivh kernel-4.8.12-300.fc25.src.rpm

This will install several files in the rpmbuild directory structure we created earlier, I won’t go over all the contents in this part of the post. Instead we where hunting for the apple-gmux.c.orig file.

Please note that the file we actually looking for is called apple-gmux.c, not apple-gmux.c.orig as in the patch file. This was a backup made by the user who submitted the patch.

There will be a linux-4.8.tar.xz file in rpmbuild/SOURCES. The apple-gmux.c file is in there, let’s extract it:

cd rpmbuild/SOURCES
tar -xvJf linux-4.8.tar.xz linux-4.8/drivers/platform/x86/apple-gmux.c

Now we have file that we need to apply the patch on. Save the above patch in the SOURCES directory and apply it to the apple-gmux.c file:

wget -O apple-gmux.patch https://bugzilla.kernel.org/attachment.cgi?id=218051&action=diff&context=patch&collapsed=&headers=1&format=raw
patch -p1 linux-4.8/drivers/platform/x86/apple-gmux.c apple-gmux.patch 

Now we have the patched source file of the module. To actually be able to test it we need to build the module and load it into the kernel.

To build the patched module, we need to create a Makefile in the directory which holds the module we want to build:

cd linux-4.8/drivers/platform/x86/
vi Makefile

Add this to the Makefile, replacing [TAB} with an actual TAB:

obj-m := apple-gmux.o

KDIR := /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)

default:
[TAB]$(MAKE) -C $(KDIR) M=$(PWD) modules

With the Makefile in place you can compile the module:

make

Time to test the module. Unload the current module and insert the one we just patched:

sudo rmmod apple-gmux
sudo insmod ./apple-gmux.ko

The brightness keys should be working now. Sometimes you only see the brightness logo on the screen without getting any changes to the actual brightness. This means we need a reboot, and to be sure the patched module gets loaded we need to move it to the appropiate location.

Modules are compressed by default on fedora, so let’s compress the module and replace the existing one.

tar cvJf apple-gmux.ko.xz apple-gmux.ko
mv /lib/modules/4.8.12-300.fc25.x86_64/kernel/drivers/platform/x86/apple-gmux.ko.xz apple-gmux.ko.xz.orig
cp apple-gmux.ko.xz /lib/modules/4.8.12-300.fc25.x86_64/kernel/drivers/platform/x86/
sudo reboot

Now reboot and your brightness keys should be working properly now.

@ministoat
Copy link

Is this applicable to Fedora 26?

@Christopher-Chianelli
Copy link

@ministoat tested on Fedora 27, it works (you need to change the version numbers though)

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