twisteroidambassador / arch-linux-ath-user-regd

Arch Linux kernel with patched Atheros drivers to ignore regulatory domain in EEPROM.
24 stars 4 forks source link

ath10k can be patched, NO NEED to compile the entire kernel #1

Open ghost opened 6 years ago

ghost commented 6 years ago

I have found a site [1] which contains instructions on how to patch ath10k to ignore regulatory domain in EEPROM. Since I have a Qualcomm Atheros card which is affected by the problem, I tried the solution and it worked.

For me, I do the following steps in /tmp/ because it is faster, especially when extracting the kernel source code: Step 1. Get the kernel source code, e.g. https://www.kernel.org/pub/linux/kernel/v4.x/linux-4.14.14.tar.xz (Download the appropriate version according to the result of uname -r). Step 2. Extract it, e.g. tar -xvJf linux-4.14.14.tar.xz Step 3. Edit drivers/net/wireless/ath/regd.c in the extracted directory: There are four parts to be patched in the aforementioned file: Part 1:

static void  
ath_reg_apply_beaconing_flags(struct wiphy *wiphy,  
                              struct ath_regulatory *reg,
                              enum nl80211_reg_initiator initiator)
{

        enum nl80211_band band;
        struct ieee80211_supported_band *sband;
        struct ieee80211_channel *ch;
        unsigned int i;

       +#ifdef CONFIG_ATH_USER_REGD
       +    return;
       +#endif

        for (band = 0; band < NUM_NL80211_BANDS; band++) {
                if (!wiphy->bands[band])
                        continue;
                sband = wiphy->bands[band];
                for (i = 0; i < sband->n_channels; i++) {
                        ch = &sband->channels[i];
                        __ath_reg_apply_beaconing_flags(wiphy, reg,
                                                        initiator, ch);
                }
        }
}

Part 2:

static void  
ath_reg_apply_ir_flags(struct wiphy *wiphy,  
                       struct ath_regulatory *reg,
                       enum nl80211_reg_initiator initiator)
{
        struct ieee80211_supported_band *sband;

        +#ifdef CONFIG_ATH_USER_REGD
        +    return;
        +#endif

        sband = wiphy->bands[NL80211_BAND_2GHZ];
        if (!sband)
                return;

Part 3:

static void ath_reg_apply_radar_flags(struct wiphy *wiphy)  
{
        struct ieee80211_supported_band *sband;
        struct ieee80211_channel *ch;
        unsigned int i;

        +#ifdef CONFIG_ATH_USER_REGD
        +    return;
        +#endif

        if (!wiphy->bands[NL80211_BAND_5GHZ])
                return;

        sband = wiphy->bands[NL80211_BAND_5GHZ];

Part 4:

static int  
ath_regd_init_wiphy(struct ath_regulatory *reg,  
                    struct wiphy *wiphy,
                    void (*reg_notifier)(struct wiphy *wiphy,
                                         struct regulatory_request *request))
{
        const struct ieee80211_regdomain *regd;

        wiphy->reg_notifier = reg_notifier;

        +#ifdef CONFIG_ATH_USER_REGD
        +    return 0;
        +#endif

        wiphy->regulatory_flags |= REGULATORY_STRICT_REG |
                                   REGULATORY_CUSTOM_REG;

        if (ath_is_world_regd(reg)) {

Step 4. Edit drivers/net/wireless/ath/Kconfig in the extracted directory:

if WLAN_VENDOR_ATH

+config ATH_USER_REGD
+    bool "Do not enforce EEPROM regulatory restrictions"
+       default n

config ATH_DEBUG  
        bool "Atheros wireless debugging"
        ---help---
          Say Y, if you want to debug atheros wireless drivers.
          Right now only ath9k makes use of this.

Step 5. Run the following commands:

make clean && make mrproper  
cp /usr/lib/modules/$(uname -r)/build/Module.symvers ./  
# Copy current existing kernel configuration
zcat /proc/config.gz > .config  
make oldconfig && make prepare  

If the following appears:

Wireless LAN (WLAN) [Y/n/?] y  
  ADMtek devices (WLAN_VENDOR_ADMTEK) [Y/n/?] y
    ADMtek ADM8211 support (ADM8211) [M/n/?] m
  Atheros/Qualcomm devices (WLAN_VENDOR_ATH) [Y/n/?] y
    Do not enforce EEPROM regulatory restrictions (ATH_USER_REGD) [N/y]

Enter y. Then, start the compilation:

make scripts  
make M=drivers/net/wireless/ath

Step 6. Replace the kernel module by entering the following commands:

# Use xz. Although the website says to use gzip, which will not work. From the file list on https://www.archlinux.org/packages/core/x86_64/linux/ , one can see that xz is used.
xz drivers/net/wireless/ath/ath.ko 
sudo cp -f drivers/net/wireless/ath/ath.ko.xz /lib/modules/$(uname -r)/kernel/drivers/net/wireless/ath/ath.ko.xz
sudo depmod -a  

Step 7. Reboot.

The above method does not require the compilation of the entire kernel, which takes many hours.

[1] https://www.kdaye.com/ap-5ghz-ath10k/

TerrenceSun commented 6 years ago

The changes you mentioned for regd.c and Kconfig are included in openwrt-402-ath_regd_optional.patch. The building ath part is useful, thanks.

twisteroidambassador commented 6 years ago

If I'm reading this correctly, the procedure described boils down to:

If this works, then the procedure in the repo should also work, and the statement about ath10 not working is probably out of date. (Incidentally, linuxwireless.org seems to be gone, and the new wiki is at https://wireless.wiki.kernel.org/en/users/drivers/ath10k . It still has that statement that "applying ath9k regulatory domain hack patch from OpenWRT causes firmware crash", though.)

On the other hand, this bypasses the operating system's packaging system, and may have funny results when the kernel needs to be updated.

TerrenceSun commented 6 years ago

The third step should be 'Configure and compile ath driver', which would be less time consuming.

By flowing gojpdchx's guide, I get a ath10k card working on 5g AP. I suspect this method may work for all the atheros card, as it's ath.ko's position to apply the No IR flag.

ghost commented 6 years ago

@twisteroidambassador Yes, ath10k actually works fine with the regulatory domain patch (on LEDE and Arch Linux). openwrt-404-regd_no_assoc_hints.patch may not be needed.

On the other hand, this bypasses the operating system's packaging system, and may have funny results when the kernel needs to be updated.

An ideal way is to make use of scripts or DKMS to automatically recompile the module. When the kernel is updated, I believe that the patched module will be removed in the process, and replaced by an un-patched module targeting the updated kernel version.

@TerrenceSun

The third step should be 'Configure and compile ath driver', which would be less time consuming.

Yes, I opened this issue because it is unnecessary to recompile the whole kernel. Even on Open Build Service, it can take an hour to compile the entire kernel.

twisteroidambassador commented 6 years ago

Glad to hear that the procedure works for you.

I just Googled for a bit, and it seems DKMS does not really support patching and rebuilding an in-tree module.

Here's someone using a script to automatically extract module sources from kernel sources: https://www.reddit.com/r/linux/comments/6g4l7n/using_dkms_to_compile_patched_modules/

And here's someone claiming be able to "[Build] In Tree Kernel Modules Out of Tree", but the blog post is light on technical details, and links to sources are all dead: https://blog.kylemanna.com/linux/building-in-tree-kernel-modules-out-of-tree/

TerrenceSun commented 6 years ago

@twisteroidambassador Get some update about the crash ~

https://wireless.wiki.kernel.org/en/users/drivers/ath10k . It still has that statement that "applying ath9k regulatory domain hack patch from OpenWRT causes firmware crash", though

When I use country_code = GY and channel = 0, I encountered the crash of ath10k. But it works well with country_code = US and channel = 0.

nikall commented 5 years ago

Hi,

I found this post in my attempt to increase tx power and bypass regulatory domain restrictions for my ath9k (AR9271) based wifi adapter. I followed the instruction as given by the -unfortunately- deleted user but during drivers compiling I get the errors below

 CC [M]  drivers/net/wireless/ath/regd.o

drivers/net/wireless/ath/regd.c: In function ‘ath_reg_apply_beaconing_flags’:
drivers/net/wireless/ath/regd.c:349:9: error: stray ‘#’ in program
        +#ifdef CONFIG_ATH_USER_REGD
         ^
drivers/net/wireless/ath/regd.c:349:10: error: ‘ifdef’ undeclared (first use in this function); did you mean ‘ifreq’?
        +#ifdef CONFIG_ATH_USER_REGD
          ^~~~~
          ifreq
drivers/net/wireless/ath/regd.c:349:10: note: each undeclared identifier is reported only once for each function it appears in
drivers/net/wireless/ath/regd.c:349:15: error: expected ‘;’ before numeric constant
        +#ifdef CONFIG_ATH_USER_REGD
               ^
               ;
drivers/net/wireless/ath/regd.c:351:9: error: stray ‘#’ in program
        +#endif
         ^
drivers/net/wireless/ath/regd.c:351:10: error: ‘endif’ undeclared (first use in this function)
        +#endif
          ^~~~~
drivers/net/wireless/ath/regd.c:351:15: error: expected ‘;’ before ‘for’
        +#endif
               ^
               ;
drivers/net/wireless/ath/regd.c:353:9:
         for (band = 0; band < NUM_NL80211_BANDS; band++) {
         ~~~    
drivers/net/wireless/ath/regd.c:347:22: warning: unused variable ‘i’ [-Wunused-variable]
         unsigned int i;
                      ^
drivers/net/wireless/ath/regd.c:346:35: warning: unused variable ‘ch’ [-Wunused-variable]
         struct ieee80211_channel *ch;
                                   ^~
drivers/net/wireless/ath/regd.c:345:42: warning: unused variable ‘sband’ [-Wunused-variable]
         struct ieee80211_supported_band *sband;
                                          ^~~~~
drivers/net/wireless/ath/regd.c:344:27: warning: unused variable ‘band’ [-Wunused-variable]
         enum nl80211_band band;
                           ^~~~
drivers/net/wireless/ath/regd.c: In function ‘ath_reg_apply_ir_flags’:
drivers/net/wireless/ath/regd.c:386:10: error: stray ‘#’ in program
         +#ifdef CONFIG_ATH_USER_REGD
          ^
drivers/net/wireless/ath/regd.c:386:11: error: ‘ifdef’ undeclared (first use in this function); did you mean ‘ifreq’?
         +#ifdef CONFIG_ATH_USER_REGD
           ^~~~~
           ifreq
drivers/net/wireless/ath/regd.c:386:16: error: expected ‘;’ before numeric constant
         +#ifdef CONFIG_ATH_USER_REGD
                ^
                ;
drivers/net/wireless/ath/regd.c:388:10: error: stray ‘#’ in program
         +#endif
          ^
drivers/net/wireless/ath/regd.c:388:11: error: ‘endif’ undeclared (first use in this function)
         +#endif
           ^~~~~
drivers/net/wireless/ath/regd.c:388:16: error: expected ‘;’ before ‘sband’
         +#endif
                ^
                ;
drivers/net/wireless/ath/regd.c:390:9:
         sband = wiphy->bands[NL80211_BAND_2GHZ];
         ~~~~~   
drivers/net/wireless/ath/regd.c: In function ‘ath_reg_apply_radar_flags’:
drivers/net/wireless/ath/regd.c:418:10: error: stray ‘#’ in program
         +#ifdef CONFIG_ATH_USER_REGD
          ^
drivers/net/wireless/ath/regd.c:418:11: error: ‘ifdef’ undeclared (first use in this function); did you mean ‘ifreq’?
         +#ifdef CONFIG_ATH_USER_REGD
           ^~~~~
           ifreq
drivers/net/wireless/ath/regd.c:418:16: error: expected ‘;’ before numeric constant
         +#ifdef CONFIG_ATH_USER_REGD
                ^
                ;
drivers/net/wireless/ath/regd.c:420:10: error: stray ‘#’ in program
         +#endif
          ^
drivers/net/wireless/ath/regd.c:420:11: error: ‘endif’ undeclared (first use in this function)
         +#endif
           ^~~~~
drivers/net/wireless/ath/regd.c:420:16: error: expected ‘;’ before ‘if’
         +#endif
                ^
                ;
drivers/net/wireless/ath/regd.c:422:9:
         if (!wiphy->bands[NL80211_BAND_5GHZ])
         ~~      
drivers/net/wireless/ath/regd.c:429:43: error: ‘reg’ undeclared (first use in this function)
   if (!ath_is_radar_freq(ch->center_freq, reg))
                                           ^~~
drivers/net/wireless/ath/regd.c: In function ‘ath_reg_notifier_apply’:
drivers/net/wireless/ath/regd.c:526:2: error: too many arguments to function ‘ath_reg_apply_radar_flags’
  ath_reg_apply_radar_flags(wiphy, reg);
  ^~~~~~~~~~~~~~~~~~~~~~~~~
drivers/net/wireless/ath/regd.c:412:13: note: declared here
 static void ath_reg_apply_radar_flags(struct wiphy *wiphy)
             ^~~~~~~~~~~~~~~~~~~~~~~~~
drivers/net/wireless/ath/regd.c: In function ‘ath_regd_init_wiphy’:
drivers/net/wireless/ath/regd.c:655:10: error: stray ‘#’ in program
         +#ifdef CONFIG_ATH_USER_REGD
          ^
drivers/net/wireless/ath/regd.c:655:11: error: ‘ifdef’ undeclared (first use in this function); did you mean ‘ifreq’?
         +#ifdef CONFIG_ATH_USER_REGD
           ^~~~~
           ifreq
drivers/net/wireless/ath/regd.c:655:16: error: expected ‘;’ before numeric constant
         +#ifdef CONFIG_ATH_USER_REGD
                ^
                ;
drivers/net/wireless/ath/regd.c:657:10: error: stray ‘#’ in program
         +#endif
          ^
drivers/net/wireless/ath/regd.c:657:11: error: ‘endif’ undeclared (first use in this function)
         +#endif
           ^~~~~
drivers/net/wireless/ath/regd.c:657:16: error: expected ‘;’ before ‘wiphy’
         +#endif
                ^
                ;
drivers/net/wireless/ath/regd.c:659:9:
         wiphy->regulatory_flags |= REGULATORY_STRICT_REG |
         ~~~~~   
drivers/net/wireless/ath/regd.c:679:2: error: too many arguments to function ‘ath_reg_apply_radar_flags’
  ath_reg_apply_radar_flags(wiphy, reg);
  ^~~~~~~~~~~~~~~~~~~~~~~~~
drivers/net/wireless/ath/regd.c:412:13: note: declared here
 static void ath_reg_apply_radar_flags(struct wiphy *wiphy)
             ^~~~~~~~~~~~~~~~~~~~~~~~~
At top level:
drivers/net/wireless/ath/regd.c:308:1: warning: ‘__ath_reg_apply_beaconing_flags’ defined but not used [-Wunused-function]
 __ath_reg_apply_beaconing_flags(struct wiphy *wiphy,
 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
make[1]: *** [scripts/Makefile.build:309: drivers/net/wireless/ath/regd.o] Error 1
make: *** [Makefile:1539: _module_drivers/net/wireless/ath] Error 2

I'm not a programmer, I just followed the instructions. If anyone is willing to help me it will be more than welcomed.

My distro is Kali 2019.2 and the kernel version is 4.19.0-kali5-686-pae.

Thanks in advance

iclanzan commented 4 years ago

@nikall you need to remove all those + signs you added. Those + signs are supposed to indicate which lines are added. Similarly - would indicate lines that need to be removed, but there are none in this case.

chikko80 commented 4 years ago
  • openwrt-402-ath_regd_optional.patch

The third step should be 'Configure and compile ath driver', which would be less time consuming.

By flowing gojpdchx's guide, I get a ath10k card working on 5g AP. I suspect this method may work for all the atheros card, as it's ath.ko's position to apply the No IR flag.

I am trying to get it working atm, but the changes have no effect. Still have the IR-Flag. Where is the guide of gojpdchx? do you have a link. I already tried the guide of the ghost, but no effect. I also compiled a whole new kernel with the changes, but i still get the ir flags. I dont know why. I have a qca6174.

black-ish commented 3 years ago

I just found this and it won't work, compilw works fine, CONFIG_ATH_USER_REGD=y is in .config, it seems this does nothing now, maybe this has been "patched" to not work?

CodePhase commented 2 years ago

I just spent a bit of time getting this to work again. Instead of the original patch to regd.c, use this patch:

--- kernel/linux-4.18.0-348.7.1.el8_5/drivers/net/wireless/ath/regd.c 2022-01-19 16:45:57.670059663 -0500
+++ kernel/linux-4.18.0-348.7.1.el8_5/drivers/net/wireless/ath/regd.c   2022-01-20 14:56:50.649220347 -0500
@@ -43,12 +43,18 @@
                                         NL80211_RRF_NO_OFDM)

 /* We allow IBSS on these on a case by case basis by regulatory domain */
+#ifdef CONFIG_ATH_USER_REGD
+#define ATH_5GHZ_5150_5350     REG_RULE(5150-10, 5350+10, 80, 0, 30, 0)
+#define ATH_5GHZ_5470_5850     REG_RULE(5470-10, 5850+10, 80, 0, 30, 0)
+#define ATH_5GHZ_5725_5850     REG_RULE(5725-10, 5850+10, 80, 0, 30, 0)
+#else
 #define ATH_5GHZ_5150_5350     REG_RULE(5150-10, 5350+10, 80, 0, 30,\
                                         NL80211_RRF_NO_IR)
 #define ATH_5GHZ_5470_5850     REG_RULE(5470-10, 5850+10, 80, 0, 30,\
                                         NL80211_RRF_NO_IR)
 #define ATH_5GHZ_5725_5850     REG_RULE(5725-10, 5850+10, 80, 0, 30,\
                                         NL80211_RRF_NO_IR)
+#endif

 #define ATH_2GHZ_ALL           ATH_2GHZ_CH01_11, \
                                ATH_2GHZ_CH12_13, \

Everything else is the same: keep the original patch for drivers/net/wireless/ath/Kconfig, compile and install the same way, etc...

Just to note, I only tested this on a RHEL system kernel 4.18.0-348.7.1.el8_5.x86_64.

CodePhase commented 2 years ago

For those interested, I just created a new repository with these new patches and an automated installation script for rpm-based systems: https://github.com/CodePhase/patch-atheros-regdom

WIN32GG commented 2 years ago

Hello, thank you for this message it helped me solve my issue. For anyone passing by, I had the problem with ath9k on linux 6.0.6-arch1-1 where the country code was stuck to 99 on the phy. The first methods works to create a 5Ghz AP with Hostapd. Note that you have to run make modules_prepare before compiling the modules. It was also necessary to use zst instead of xz. The Hostapd config is

interface=wlan
hw_mode=a
country_code=FR

ieee80211n=1
require_ht=1

# AC
ieee80211ac=1
require_vht=1
ieee80211d=0
ieee80211h=0
channel=165
vht_oper_centr_freq_seg0_idx=42
wmm_enabled=1
ssid=SSID

auth_algs=1
... PSK setup

You can test the module with a simple modprobe ath9k after copying it to the module folders. Thanks again.

sassyn commented 1 year ago

The solution doesn't work for modules > 5.10. It seems to be broken. This seems to be interesting https://tildearrow.org/?p=post&month=7&year=2022&item=lar

anyone manage to get this working

PaulGrandperrin commented 11 months ago

@CodePhase The original patch from OpenWRT works well. I took the latest version from their repo, adapted it (s/CPTCFG_/CONFIG_/g) and it compiles and works well.

My package: https://github.com/PaulGrandperrin/nix-systems/blob/main/packages/kernel-module-ath-patched.nix