UBports Robot Logo UBports Forum
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Register
    • Login

    Adding a kernel module for USB wifi adapter

    Scheduled Pinned Locked Moved Unsolved Support
    28 Posts 3 Posters 4.6k Views 1 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
      Reply
      • Reply as topic
      Log in to reply
      This topic has been deleted. Only users with topic management privileges can see it.
      • ? Offline
        A Former User
        last edited by A Former User

        If I understand this correctly, there's a configuration I need to edit before building. I can do this with make ARCH=arm menuconfig in kernel/lge/hammerhead. There are options to select which drivers to include, but I can't seem to find the one I added. Maybe I'm still missing something.

        1 Reply Last reply Reply Quote 0
        • ? Offline
          A Former User
          last edited by

          I was missing a bunch of Kconfig and Makefile entries. Stole some from the Github Linux repo. Module's in the menu now, will try to build again.

          1 Reply Last reply Reply Quote 0
          • ? Offline
            A Former User
            last edited by

            It didn't work. Kernel module still didn't end up in the output files. Also, I managed to break my VM. First it was the menuconfig terminal interface getting flooded with "p" characters. Then errors related to memory allocation during the build. And after rebooting I couldn't open terminals anymore. So I'm reinstalling the system. Building GCC and Python again will take a while, so I'll get back to y'all later.

            1 Reply Last reply Reply Quote 0
            • ? Offline
              A Former User
              last edited by A Former User

              I am currently unable to build the kernel due to errors. This is my process after initializing and syncing ubp-5.1-allthefixings. As mentioned before, I'm following this official guide.

              1. Go to kernel/lge/hammerhead/
              2. Add driver files from torvalds/linux and add entries for them in Kconfig and Makefile files in drivers/net/wireless/
              	new file:   mediatek/Kconfig
              	new file:   mediatek/Makefile
              	new file:   mediatek/mt7601u/Kconfig
              	new file:   mediatek/mt7601u/Makefile
              	new file:   mediatek/mt7601u/core.c
              	new file:   mediatek/mt7601u/debugfs.c
              	new file:   mediatek/mt7601u/dma.c
              	new file:   mediatek/mt7601u/dma.h
              	new file:   mediatek/mt7601u/eeprom.c
              	new file:   mediatek/mt7601u/eeprom.h
              	new file:   mediatek/mt7601u/init.c
              	new file:   mediatek/mt7601u/initvals.h
              	new file:   mediatek/mt7601u/initvals_phy.h
              	new file:   mediatek/mt7601u/mac.c
              	new file:   mediatek/mt7601u/mac.h
              	new file:   mediatek/mt7601u/main.c
              	new file:   mediatek/mt7601u/mcu.c
              	new file:   mediatek/mt7601u/mcu.h
              	new file:   mediatek/mt7601u/mt7601u.h
              	new file:   mediatek/mt7601u/phy.c
              	new file:   mediatek/mt7601u/regs.h
              	new file:   mediatek/mt7601u/trace.c
              	new file:   mediatek/mt7601u/trace.h
              	new file:   mediatek/mt7601u/tx.c
              	new file:   mediatek/mt7601u/usb.c
              	new file:   mediatek/mt7601u/usb.h
              	new file:   mediatek/mt7601u/util.c
              
              diff --git a/drivers/net/wireless/Kconfig b/drivers/net/wireless/Kconfig
              index 5ccdd2a..2c6f2ab 100644
              --- a/drivers/net/wireless/Kconfig
              +++ b/drivers/net/wireless/Kconfig
              @@ -333,5 +333,6 @@ source "drivers/net/wireless/wl1251/Kconfig"
               source "drivers/net/wireless/wl12xx/Kconfig"
               source "drivers/net/wireless/zd1211rw/Kconfig"
               source "drivers/net/wireless/mwifiex/Kconfig"
              +source "drivers/net/wireless/mediatek/Kconfig"
               
               endif # WLAN
              diff --git a/drivers/net/wireless/Makefile b/drivers/net/wireless/Makefile
              index 9392311..437e6b3 100644
              --- a/drivers/net/wireless/Makefile
              +++ b/drivers/net/wireless/Makefile
              @@ -66,3 +66,5 @@ obj-$(CONFIG_BRCMSMAC)        += brcm80211/
               
               obj-$(CONFIG_LIBRA_SDIOIF)     += libra/
               obj-$(CONFIG_WCNSS_CORE)       += wcnss/
              +
              +obj-$(CONFIG_MEDIATEK) += mediatek/
              
              1. make ARCH=arm menuconfig. Confirm the driver appears. Note that it is selected by default. Exit picking "yes" when asked to save configuration.
              [*]   MediaTek devices (NEW)
              <M>     MediaTek MT7601U (USB) support
              
              1. Return to the top directory as required by the guide.
              2. source build/envsetup.sh. No errors.
              including device/oneplus/bacon/vendorsetup.sh
              including device/lge/hammerhead/vendorsetup.sh
              including device/lge/mako/vendorsetup.sh
              including device/fairphone/FP2/vendorsetup.sh
              including vendor/cm/vendorsetup.sh
              including sdk/bash_completion/adb.bash
              including vendor/cm/bash_completion/git.bash
              including vendor/cm/bash_completion/repo.bash
              
              1. lunch. The guide says to pick cm_...-userdebug. However, there is no such option for hammerhead, so I pick aosp_hammerhead-userdebug (number 5) instead.
              Which would you like? [aosp_arm-eng] 5
              Trying dependencies-only mode on a non-existing device tree?
              
              ============================================
              PLATFORM_VERSION_CODENAME=REL
              PLATFORM_VERSION=5.1.1
              CM_VERSION=
              TARGET_PRODUCT=aosp_hammerhead
              TARGET_BUILD_VARIANT=userdebug
              TARGET_BUILD_TYPE=release
              TARGET_BUILD_APPS=
              TARGET_ARCH=arm
              TARGET_ARCH_VARIANT=armv7-a-neon
              TARGET_CPU_VARIANT=krait
              TARGET_2ND_ARCH=
              TARGET_2ND_ARCH_VARIANT=
              TARGET_2ND_CPU_VARIANT=
              HOST_ARCH=x86_64
              HOST_OS=linux
              HOST_OS_EXTRA=Linux-4.4.0-210-generic-x86_64-with-glibc2.23
              HOST_BUILD_TYPE=release
              BUILD_ID=LMY48W
              OUT_DIR=/root/ubp-5.1/out
              ============================================
              
              1. Finally, mka. Previously (before I broke my old VM) I was able to build just fine, except the driver would not be included in the output for some reason. Now (on a fresh VM) I'm getting the following error:
              system/core/liblog/fake_log_device.c: In function ‘showLog.isra.0’:
              system/core/liblog/fake_log_device.c:463:30: error: argument 1 value ‘4294967288’ exceeds maximum object size 2147483647 [-Werror=alloc-size-larger-than=]
                       vec = (struct iovec*)malloc(sizeof(struct iovec)*numVecs);
                                            ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
              In file included from system/core/liblog/fake_log_device.c:27:
              /usr/include/stdlib.h:466:14: note: in a call to allocation function ‘malloc’ declared here
               extern void *malloc (size_t __size) __THROW __attribute_malloc__ __wur;
                            ^~~~~~
              cc1: all warnings being treated as errors
              build/core/binary.mk:699: recipe for target '/root/ubp-5.1/out/host/linux-x86/obj32/STATIC_LIBRARIES/liblog_intermediates/fake_log_device.o' failed
              
              ...
              
              #### make failed to build some targets (53 seconds) ####
              

              Additional information:

              root@ubports:~/ubp-5.1# cc --version
              cc (GCC) 8.5.0
              Copyright (C) 2018 Free Software Foundation, Inc.
              This is free software; see the source for copying conditions.  There is NO
              warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
              
              root@ubports:~/ubp-5.1# python --version
              Python 2.7.12
              
              root@ubports:~/ubp-5.1# uname -a
              Linux ubports 4.4.0-210-generic #242-Ubuntu SMP Fri Apr 16 09:57:56 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux
              
              root@ubports:~/ubp-5.1/kernel/lge/hammerhead# update-alternatives --list cc
              /usr/local/bin/x86_64-pc-linux-gnu-gcc-8.5.0
              
              root@ubports:~/ubp-5.1/kernel/lge/hammerhead# update-alternatives --list cc1
              /usr/local/libexec/gcc/x86_64-pc-linux-gnu/8.5.0/cc1
              
              

              Any help would be appreciated.

              1 Reply Last reply Reply Quote 0
              • ? Offline
                A Former User
                last edited by

                I was able to bypass this error by changing the offending line to vec = NULL;. There's a null check immediately after, so it shouldn't cause too much trouble (other than probably causing whatever logging this was supposed to do to stop working).

                However, now it complains about there being no GCC 7. What? I compiled it just fine with 8.5.0 before...

                I guess will try to compile 7.5.0 now.

                1 Reply Last reply Reply Quote 0
                • ? Offline
                  A Former User
                  last edited by A Former User

                  This issue disappears after make clean in the top directory. Looks like 7.5.0 isn't needed after all. Now I just need to fix cc1plus not being the right version.

                  E 1 Reply Last reply Reply Quote 0
                  • E Offline
                    Emphrath @Guest
                    last edited by Emphrath

                    @zehkira Thank you so much for sharing your process here, despite the lack of help. I can't be of any help myself since I've never built a kernel in my life, but the process itself will be useful for a lot of people, so thanks ! Quick side note: if you plan to flash this on an actual device, bear in mind that most ubports devices are flashed aiming at a fixed partition size, dependant on the partition layout that the installer expects at first flash. There are ways to flash a bigger system image (which likely is what your hack will result in) but I don't think it's straightforward and you should research the forum before you attempt it. Good luck and keep us posted !

                    1 Reply Last reply Reply Quote 0
                    • ? Offline
                      A Former User
                      last edited by A Former User

                      I'm finally able to build without errors again. I didn't do anything with menuconfig this time though, just make clean && source build/envsetup.sh && mka. Again, it looks like the driver did not end up in the output:

                      root@ubports:~/ubp-5.1# cout
                      root@ubports:~/ubp-5.1/out/target/product/hammerhead# find . |grep mt7601u
                      root@ubports:~/ubp-5.1/out/target/product/hammerhead#
                      

                      There is, however, obj/KERNEL_OBJ/include/config/wlan/vendor/mediatek.h. Which is empty.

                      Sidenote: I learned of the existence of ppa:ubuntu-toolchain-r/test, which I can use to install newer versions of GCC. No need to compile.

                      most ubports devices are flashed aiming at a fixed partition size, dependant on the partition layout that the installer expects at first flash.

                      Sounds like more pain.

                      1 Reply Last reply Reply Quote 0
                      • ? Offline
                        A Former User
                        last edited by

                        Running make ARCH=arm menuconfig and choosing to save on exit results in a .config file being created, which contains a setting to include the module I want. Will try to build with this file present now. So, immediately after the previous build I did this:

                        1. make clean in top directory to remove results of previous build.
                        2. make ARCH=arm menuconfig in kernel/lge/hammerhead.
                        3. mka in top directory to build again.
                        1 Reply Last reply Reply Quote 0
                        • ? Offline
                          A Former User
                          last edited by

                          It does not like that .config file being there.

                           Using /root/ubp-5.1/kernel/lge/hammerhead as source for kernel
                            /root/ubp-5.1/kernel/lge/hammerhead is not clean, please run 'make mrproper'
                            in the '/root/ubp-5.1/kernel/lge/hammerhead' directory.
                          Copy: /root/ubp-5.1/out/target/product/hammerhead/system/etc/audio_policy.conf
                          Copy xml: /root/ubp-5.1/out/target/product/hammerhead/system/etc/mixer_paths.xml
                          /root/ubp-5.1/kernel/lge/hammerhead/Makefile:976: recipe for target 'prepare3' failed
                          make[2]: *** [prepare3] Error 1
                          Makefile:130: recipe for target 'sub-make' failed
                          make[1]: *** [sub-make] Error 2
                          make[1]: Leaving directory '/root/ubp-5.1/kernel/lge/hammerhead'
                          Copy xml: /root/ubp-5.1/out/target/product/hammerhead/system/etc/media_codecs_google_audio.xml
                          build/core/tasks/kernel.mk:277: recipe for target 'TARGET_KERNEL_BINARIES' failed
                          

                          As further evidenced by what running the command it wants does:

                          root@ubports:~/ubp-5.1/kernel/lge/hammerhead# make mrproper
                            CLEAN   scripts/basic
                            CLEAN   scripts/kconfig
                            CLEAN   include/config
                            CLEAN   .config
                          

                          But that file is supposed to be there, and I am doing this correctly, according to this article on kernel configuration.

                          1 Reply Last reply Reply Quote 0
                          • ? Offline
                            A Former User
                            last edited by A Former User

                            This time I used the make_defconfig.sh script in hammerhead/ instead of make ARCH=arm menuconfig. This script wants a config file from arch/arm/configs/ to edit. The problem is that there are 3 different configs for hammerhead:

                            • cyanogenmod_hammerhead_defconfig
                            • hammerhead_defconfig
                            • tct_hammer_defconfig (I guess this one says "hammer", not "hammerhead")

                            When I do uname -a on my Nexus, it says cyanogenmod in there, so I picked that one. Building now.

                            1 Reply Last reply Reply Quote 0
                            • ? Offline
                              A Former User
                              last edited by

                              Build was successful. Still no driver in the output though.

                              root@ubports:~/ubp-5.1/out/target/product/hammerhead# find . |grep -i mt76
                              root@ubports:~/ubp-5.1/out/target/product/hammerhead# find . |grep -i mediatek
                              ./obj/KERNEL_OBJ/include/config/wlan/vendor/mediatek.h
                              root@ubports:~/ubp-5.1/out/target/product/hammerhead# cat ./obj/KERNEL_OBJ/include/config/wlan/vendor/mediatek.h
                              root@ubports:~/ubp-5.1/out/target/product/hammerhead# 
                              
                              1 Reply Last reply Reply Quote 0
                              • ? Offline
                                A Former User
                                last edited by

                                This time I did ./make_defconfig.sh hammerhead_defconfig. Note how the changes made to the cyanogenmod file remain despite make clean and make mrproper:

                                	modified:   arch/arm/configs/cyanogenmod_hammerhead_defconfig
                                	modified:   arch/arm/configs/hammerhead_defconfig
                                

                                Another potentially relevant thing I noticed: In the configuration menu provided by make_defconfig.sh, only a Mediatek group is shown. There is no entry for the mt7601u. This was not the case when I used make ARCH=arm menuconfig. Perhaps an indication of a configuration issue that is causing the driver to not be detected? But then why was it detected in the other case? Assuming this build fails to produce the desired result, I will investigate this next.

                                1 Reply Last reply Reply Quote 1
                                • ? Offline
                                  A Former User
                                  last edited by

                                  I assumed correctly. Here is the section referencing Mediatek in the .config that make_defconfig.sh would generate:

                                  CONFIG_WLAN_VENDOR_MEDIATEK=y
                                  

                                  Compare the Broadcom (I think) drivers section:

                                  CONFIG_BCMDHD=y
                                  CONFIG_BCM4339=y
                                  CONFIG_BCMDHD_FW_PATH="/vendor/firmware/fw_bcmdhd.bin"
                                  CONFIG_BCMDHD_NVRAM_PATH="/etc/wifi/bcmdhd.cal"
                                  

                                  And the Mediatek section in a .config I generated with make ARCH=arm menuconfig:

                                  CONFIG_WLAN_VENDOR_MEDIATEK=y
                                  CONFIG_MT7601U=m
                                  

                                  I'll try to simply manually add these two lines to hammerhead_defconfig and cyanogenmod_hammerhead_defconfig for the next build.

                                  1 Reply Last reply Reply Quote 0
                                  • ? Offline
                                    A Former User
                                    last edited by

                                    Found this in the mt7601u's Kconfig:

                                    	depends on MAC80211
                                    	depends on USB
                                    

                                    mac80211 is something that can be selected in the config menu in Networking support → Wireless. It is not selected by default. When you select it, the mt7601u driver appears in the Mediatek wireless driver section of the menu as it should.

                                    I edited both hammerhead_defconfig and cyanogenmod_hammerhead_defconfig like this (using make_defconfig.sh of course). Building again.

                                    1 Reply Last reply Reply Quote 0
                                    • ? Offline
                                      A Former User
                                      last edited by

                                      Build finished. Seemingly no change. Maybe out/ is supposed to be like this. Will flash tomorrow and check.

                                      1 Reply Last reply Reply Quote 0
                                      • ? Offline
                                        A Former User
                                        last edited by

                                        Flashed and checked with lsmod, modinfo and find. It looks like, despite everything, the module was still not installed.

                                        1 Reply Last reply Reply Quote 0
                                        • ? Offline
                                          A Former User
                                          last edited by

                                          device/lge/hammerhead/BoardConfig.mk confirms that the configuration file I've been working with is the right one:

                                          # Define kernel config for inline building
                                          TARGET_KERNEL_CONFIG := cyanogenmod_hammerhead_defconfig
                                          TARGET_KERNEL_SOURCE := kernel/lge/hammerhead
                                          

                                          device/lge/hammerhead/init.hammerhead.rc has something that could be relevant:

                                            # wifi
                                              chown wifi system /persist/wifi
                                              insmod /system/lib/modules/bcmdhd.ko
                                          

                                          I'll add another insmod line for mt7601u and see what happens. My guess is it will either break something or do nothing.

                                          1 Reply Last reply Reply Quote 0
                                          • ? Offline
                                            A Former User
                                            last edited by

                                            I tried mkap, which failed to push anything to the device, but it did create ./out/target/product/hammerhead/obj/KERNEL_OBJ/include/config/mt7601u.h. I also noticed that all the .h files in obj/KERNEL_OBJ/include/config/ are empty.

                                            Flashed, replaced system image, no change.

                                            1 Reply Last reply Reply Quote 0
                                            • ? Offline
                                              A Former User
                                              last edited by

                                              I am literally unable to tell you what happened next, because all my posts are being flagged as spam.

                                              E 1 Reply Last reply Reply Quote 0
                                              • First post
                                                Last post