Time for the real magic, tweaking the kernel for that extra functionality. I’ll try doing it in stages with just small tweaks and test them out. First up are the HID patches for keyboard and mouse.
Patching
I started off this patching effort by looking at the already existing patches. I was hoping by picking something by the same manufacturer and same kernel version I wouldn’t have to do much.
Unfortunately that wasn’t the case. I started by looking at the msm8974 kernel_3.10 patch and while manually going through it noticed it was creating new functions that already existed, instead of just overwriting them. That should have been a red flag, but I carried on simply fixing this silly code and only keeping the new one.
When it came time to compile… it failed.
drivers/usb/gadget/f_hid.o: In function `hidg_bind_config':
/home/weekend/Code/android/lineage/kernel/motorola/msm8952/drivers/usb/gadget/f_hid.c:804: multiple definition of `hidg_bind_config'
drivers/usb/gadget/android.o:/home/weekend/Code/android/lineage/kernel/motorola/msm8952/drivers/usb/gadget/f_hid.c:804: first defined here
drivers/usb/gadget/f_hid.o: In function `ghid_setup':
/home/weekend/Code/android/lineage/out/target/product/athene/obj/KERNEL_OBJ/../../../../../../kernel/motorola/msm8952/drivers/usb/gadget/f_hid.c:860: multiple definition of `ghid_setup'
drivers/usb/gadget/android.o:/home/weekend/Code/android/lineage/out/target/product/athene/obj/KERNEL_OBJ/../../../../../../kernel/motorola/msm8952/drivers/usb/gadget/f_hid.c:860: first defined here
drivers/usb/gadget/f_hid.o: In function `ghid_cleanup':
/home/weekend/Code/android/lineage/out/target/product/athene/obj/KERNEL_OBJ/../../../../../../kernel/motorola/msm8952/drivers/usb/gadget/f_hid.c:876: multiple definition of `ghid_cleanup'
drivers/usb/gadget/android.o:/home/weekend/Code/android/lineage/out/target/product/athene/obj/KERNEL_OBJ/../../../../../../kernel/motorola/msm8952/drivers/usb/gadget/f_hid.c:876: first defined here
I had the order wrong in some parts of the code, causing me to use functions before they were defined. After I fixed that it still failed, I had forgotten one of the duplicate functions. After fixing that I wrongly thought that maybe the previous error got cached and was messing everything up because I kept seeing the above message about multiple definitions even though there were none in the code.
So naturally, not wanting to delve much deeper at the moment and with the wrong idea that the cache was messing things up I set out to resolve the wrong problem, this caused the most delays of all. Deleting already build stuff and clearing cache takes a very long time when its a project this size. And then I decided to undo my changes and sync back with the original code, again something that took a very very long time. In hindsight it was an obvious error, but at the time I just didn’t see it.
In the end I looked closer at the already existingcode again and at what the patch was doing, when I finally spotted it. Not only were some of these functions and some variables already defined in android.c but more importantly it was also already including a very relevant file.
While the patch called for creating a new f_hid.h file and:
So I was defining function prototypes in f_hid.h, and then defining them in the included f_hid.c AND also compiling f_hid.o (and thus defining it again just as the error tried to tell me)! I don’t usually include .c files in my .c files, so I can understand why I didn’t notice it right away. I even double checked to see if the f_hid.h had the proper #ifndef guards before I remembered I’m also creating the f_hid.o file. But I still think I should have caught it sooner.
To fix it I decided to not create a f_hid.h file(just like in the original code) and to not build a separate f_hid.o file. End result: a smaller patch is applied, without changing the MakeFile or creating a new f_hid.h file.
Testing
After all that and waiting forever for it to build again, I got no errors. I packaged up the kernel in a zip using the nethunter installer like before and tried it out. This time the USB keyboard app got a bit further, but it still wouldn’t let me use it as a keyboard or mouse. It was stuck on:
opening /dev/hidg0 opening /dev/hidg1
At least my phone booted and there actually was a /dev/hidg0 so we could overcome this problem too. It seemed like a permissions issue to me, and it was. I temporarily set selinux in permissive mode by using the adb shell with:
And now the app works, I could use it as a mouse and as a keyboard (though my machine not being qwerty and it of course just sending keycodes made it a bit annoying to type).
Improvements
I need to keep in mind when I have created and tested all my desired patches, that I have to set proper selinux stuff. I don’t want to manually set these things and I don’t want to allow all things just to be able to use some of my extra functionality. In fact, I don’t even want to be able to use setenforce 0 via adb or locally at all. So I should tweak my build type too at some point (no more -userdebug or -eng builds when this is all over!).
All the patches will go in this repo, and if I can I’ll also submit them upstream to the respective projects. But just because I think the syntax highlighting of diff’s are pretty, heres the patch:
side note: having such large projects open in my editor and other places meant I hit a limit and jekyll wouldn’t serve files locally instead giving this error:
FATAL: Listen error: unable to monitor directories for changes.
Visit https://github.com/guard/listen/wiki/Increasing-the-amount-of-inotify-watchers for info on how to fix this.
Sure I could increase the amount.. but maybe having “smart” editors keep track of entire kernel repository isn’t the best idea.