162306a36Sopenharmony_ci.. SPDX-License-Identifier: GPL-2.0 262306a36Sopenharmony_ci 362306a36Sopenharmony_ci======================================= 462306a36Sopenharmony_ciLinux wireless regulatory documentation 562306a36Sopenharmony_ci======================================= 662306a36Sopenharmony_ci 762306a36Sopenharmony_ciThis document gives a brief review over how the Linux wireless 862306a36Sopenharmony_ciregulatory infrastructure works. 962306a36Sopenharmony_ci 1062306a36Sopenharmony_ciMore up to date information can be obtained at the project's web page: 1162306a36Sopenharmony_ci 1262306a36Sopenharmony_cihttps://wireless.wiki.kernel.org/en/developers/Regulatory 1362306a36Sopenharmony_ci 1462306a36Sopenharmony_ciKeeping regulatory domains in userspace 1562306a36Sopenharmony_ci--------------------------------------- 1662306a36Sopenharmony_ci 1762306a36Sopenharmony_ciDue to the dynamic nature of regulatory domains we keep them 1862306a36Sopenharmony_ciin userspace and provide a framework for userspace to upload 1962306a36Sopenharmony_cito the kernel one regulatory domain to be used as the central 2062306a36Sopenharmony_cicore regulatory domain all wireless devices should adhere to. 2162306a36Sopenharmony_ci 2262306a36Sopenharmony_ciHow to get regulatory domains to the kernel 2362306a36Sopenharmony_ci------------------------------------------- 2462306a36Sopenharmony_ci 2562306a36Sopenharmony_ciWhen the regulatory domain is first set up, the kernel will request a 2662306a36Sopenharmony_cidatabase file (regulatory.db) containing all the regulatory rules. It 2762306a36Sopenharmony_ciwill then use that database when it needs to look up the rules for a 2862306a36Sopenharmony_cigiven country. 2962306a36Sopenharmony_ci 3062306a36Sopenharmony_ciHow to get regulatory domains to the kernel (old CRDA solution) 3162306a36Sopenharmony_ci--------------------------------------------------------------- 3262306a36Sopenharmony_ci 3362306a36Sopenharmony_ciUserspace gets a regulatory domain in the kernel by having 3462306a36Sopenharmony_cia userspace agent build it and send it via nl80211. Only 3562306a36Sopenharmony_ciexpected regulatory domains will be respected by the kernel. 3662306a36Sopenharmony_ci 3762306a36Sopenharmony_ciA currently available userspace agent which can accomplish this 3862306a36Sopenharmony_ciis CRDA - central regulatory domain agent. Its documented here: 3962306a36Sopenharmony_ci 4062306a36Sopenharmony_cihttps://wireless.wiki.kernel.org/en/developers/Regulatory/CRDA 4162306a36Sopenharmony_ci 4262306a36Sopenharmony_ciEssentially the kernel will send a udev event when it knows 4362306a36Sopenharmony_ciit needs a new regulatory domain. A udev rule can be put in place 4462306a36Sopenharmony_cito trigger crda to send the respective regulatory domain for a 4562306a36Sopenharmony_cispecific ISO/IEC 3166 alpha2. 4662306a36Sopenharmony_ci 4762306a36Sopenharmony_ciBelow is an example udev rule which can be used: 4862306a36Sopenharmony_ci 4962306a36Sopenharmony_ci# Example file, should be put in /etc/udev/rules.d/regulatory.rules 5062306a36Sopenharmony_ciKERNEL=="regulatory*", ACTION=="change", SUBSYSTEM=="platform", RUN+="/sbin/crda" 5162306a36Sopenharmony_ci 5262306a36Sopenharmony_ciThe alpha2 is passed as an environment variable under the variable COUNTRY. 5362306a36Sopenharmony_ci 5462306a36Sopenharmony_ciWho asks for regulatory domains? 5562306a36Sopenharmony_ci-------------------------------- 5662306a36Sopenharmony_ci 5762306a36Sopenharmony_ci* Users 5862306a36Sopenharmony_ci 5962306a36Sopenharmony_ciUsers can use iw: 6062306a36Sopenharmony_ci 6162306a36Sopenharmony_cihttps://wireless.wiki.kernel.org/en/users/Documentation/iw 6262306a36Sopenharmony_ci 6362306a36Sopenharmony_ciAn example:: 6462306a36Sopenharmony_ci 6562306a36Sopenharmony_ci # set regulatory domain to "Costa Rica" 6662306a36Sopenharmony_ci iw reg set CR 6762306a36Sopenharmony_ci 6862306a36Sopenharmony_ciThis will request the kernel to set the regulatory domain to 6962306a36Sopenharmony_cithe specified alpha2. The kernel in turn will then ask userspace 7062306a36Sopenharmony_cito provide a regulatory domain for the alpha2 specified by the user 7162306a36Sopenharmony_ciby sending a uevent. 7262306a36Sopenharmony_ci 7362306a36Sopenharmony_ci* Wireless subsystems for Country Information elements 7462306a36Sopenharmony_ci 7562306a36Sopenharmony_ciThe kernel will send a uevent to inform userspace a new 7662306a36Sopenharmony_ciregulatory domain is required. More on this to be added 7762306a36Sopenharmony_cias its integration is added. 7862306a36Sopenharmony_ci 7962306a36Sopenharmony_ci* Drivers 8062306a36Sopenharmony_ci 8162306a36Sopenharmony_ciIf drivers determine they need a specific regulatory domain 8262306a36Sopenharmony_ciset they can inform the wireless core using regulatory_hint(). 8362306a36Sopenharmony_ciThey have two options -- they either provide an alpha2 so that 8462306a36Sopenharmony_cicrda can provide back a regulatory domain for that country or 8562306a36Sopenharmony_cithey can build their own regulatory domain based on internal 8662306a36Sopenharmony_cicustom knowledge so the wireless core can respect it. 8762306a36Sopenharmony_ci 8862306a36Sopenharmony_ci*Most* drivers will rely on the first mechanism of providing a 8962306a36Sopenharmony_ciregulatory hint with an alpha2. For these drivers there is an additional 9062306a36Sopenharmony_cicheck that can be used to ensure compliance based on custom EEPROM 9162306a36Sopenharmony_ciregulatory data. This additional check can be used by drivers by 9262306a36Sopenharmony_ciregistering on its struct wiphy a reg_notifier() callback. This notifier 9362306a36Sopenharmony_ciis called when the core's regulatory domain has been changed. The driver 9462306a36Sopenharmony_cican use this to review the changes made and also review who made them 9562306a36Sopenharmony_ci(driver, user, country IE) and determine what to allow based on its 9662306a36Sopenharmony_ciinternal EEPROM data. Devices drivers wishing to be capable of world 9762306a36Sopenharmony_ciroaming should use this callback. More on world roaming will be 9862306a36Sopenharmony_ciadded to this document when its support is enabled. 9962306a36Sopenharmony_ci 10062306a36Sopenharmony_ciDevice drivers who provide their own built regulatory domain 10162306a36Sopenharmony_cido not need a callback as the channels registered by them are 10262306a36Sopenharmony_cithe only ones that will be allowed and therefore *additional* 10362306a36Sopenharmony_cichannels cannot be enabled. 10462306a36Sopenharmony_ci 10562306a36Sopenharmony_ciExample code - drivers hinting an alpha2: 10662306a36Sopenharmony_ci------------------------------------------ 10762306a36Sopenharmony_ci 10862306a36Sopenharmony_ciThis example comes from the zd1211rw device driver. You can start 10962306a36Sopenharmony_ciby having a mapping of your device's EEPROM country/regulatory 11062306a36Sopenharmony_cidomain value to a specific alpha2 as follows:: 11162306a36Sopenharmony_ci 11262306a36Sopenharmony_ci static struct zd_reg_alpha2_map reg_alpha2_map[] = { 11362306a36Sopenharmony_ci { ZD_REGDOMAIN_FCC, "US" }, 11462306a36Sopenharmony_ci { ZD_REGDOMAIN_IC, "CA" }, 11562306a36Sopenharmony_ci { ZD_REGDOMAIN_ETSI, "DE" }, /* Generic ETSI, use most restrictive */ 11662306a36Sopenharmony_ci { ZD_REGDOMAIN_JAPAN, "JP" }, 11762306a36Sopenharmony_ci { ZD_REGDOMAIN_JAPAN_ADD, "JP" }, 11862306a36Sopenharmony_ci { ZD_REGDOMAIN_SPAIN, "ES" }, 11962306a36Sopenharmony_ci { ZD_REGDOMAIN_FRANCE, "FR" }, 12062306a36Sopenharmony_ci 12162306a36Sopenharmony_ciThen you can define a routine to map your read EEPROM value to an alpha2, 12262306a36Sopenharmony_cias follows:: 12362306a36Sopenharmony_ci 12462306a36Sopenharmony_ci static int zd_reg2alpha2(u8 regdomain, char *alpha2) 12562306a36Sopenharmony_ci { 12662306a36Sopenharmony_ci unsigned int i; 12762306a36Sopenharmony_ci struct zd_reg_alpha2_map *reg_map; 12862306a36Sopenharmony_ci for (i = 0; i < ARRAY_SIZE(reg_alpha2_map); i++) { 12962306a36Sopenharmony_ci reg_map = ®_alpha2_map[i]; 13062306a36Sopenharmony_ci if (regdomain == reg_map->reg) { 13162306a36Sopenharmony_ci alpha2[0] = reg_map->alpha2[0]; 13262306a36Sopenharmony_ci alpha2[1] = reg_map->alpha2[1]; 13362306a36Sopenharmony_ci return 0; 13462306a36Sopenharmony_ci } 13562306a36Sopenharmony_ci } 13662306a36Sopenharmony_ci return 1; 13762306a36Sopenharmony_ci } 13862306a36Sopenharmony_ci 13962306a36Sopenharmony_ciLastly, you can then hint to the core of your discovered alpha2, if a match 14062306a36Sopenharmony_ciwas found. You need to do this after you have registered your wiphy. You 14162306a36Sopenharmony_ciare expected to do this during initialization. 14262306a36Sopenharmony_ci 14362306a36Sopenharmony_ci:: 14462306a36Sopenharmony_ci 14562306a36Sopenharmony_ci r = zd_reg2alpha2(mac->regdomain, alpha2); 14662306a36Sopenharmony_ci if (!r) 14762306a36Sopenharmony_ci regulatory_hint(hw->wiphy, alpha2); 14862306a36Sopenharmony_ci 14962306a36Sopenharmony_ciExample code - drivers providing a built in regulatory domain: 15062306a36Sopenharmony_ci-------------------------------------------------------------- 15162306a36Sopenharmony_ci 15262306a36Sopenharmony_ci[NOTE: This API is not currently available, it can be added when required] 15362306a36Sopenharmony_ci 15462306a36Sopenharmony_ciIf you have regulatory information you can obtain from your 15562306a36Sopenharmony_cidriver and you *need* to use this we let you build a regulatory domain 15662306a36Sopenharmony_cistructure and pass it to the wireless core. To do this you should 15762306a36Sopenharmony_cikmalloc() a structure big enough to hold your regulatory domain 15862306a36Sopenharmony_cistructure and you should then fill it with your data. Finally you simply 15962306a36Sopenharmony_cicall regulatory_hint() with the regulatory domain structure in it. 16062306a36Sopenharmony_ci 16162306a36Sopenharmony_ciBelow is a simple example, with a regulatory domain cached using the stack. 16262306a36Sopenharmony_ciYour implementation may vary (read EEPROM cache instead, for example). 16362306a36Sopenharmony_ci 16462306a36Sopenharmony_ciExample cache of some regulatory domain:: 16562306a36Sopenharmony_ci 16662306a36Sopenharmony_ci struct ieee80211_regdomain mydriver_jp_regdom = { 16762306a36Sopenharmony_ci .n_reg_rules = 3, 16862306a36Sopenharmony_ci .alpha2 = "JP", 16962306a36Sopenharmony_ci //.alpha2 = "99", /* If I have no alpha2 to map it to */ 17062306a36Sopenharmony_ci .reg_rules = { 17162306a36Sopenharmony_ci /* IEEE 802.11b/g, channels 1..14 */ 17262306a36Sopenharmony_ci REG_RULE(2412-10, 2484+10, 40, 6, 20, 0), 17362306a36Sopenharmony_ci /* IEEE 802.11a, channels 34..48 */ 17462306a36Sopenharmony_ci REG_RULE(5170-10, 5240+10, 40, 6, 20, 17562306a36Sopenharmony_ci NL80211_RRF_NO_IR), 17662306a36Sopenharmony_ci /* IEEE 802.11a, channels 52..64 */ 17762306a36Sopenharmony_ci REG_RULE(5260-10, 5320+10, 40, 6, 20, 17862306a36Sopenharmony_ci NL80211_RRF_NO_IR| 17962306a36Sopenharmony_ci NL80211_RRF_DFS), 18062306a36Sopenharmony_ci } 18162306a36Sopenharmony_ci }; 18262306a36Sopenharmony_ci 18362306a36Sopenharmony_ciThen in some part of your code after your wiphy has been registered:: 18462306a36Sopenharmony_ci 18562306a36Sopenharmony_ci struct ieee80211_regdomain *rd; 18662306a36Sopenharmony_ci int size_of_regd; 18762306a36Sopenharmony_ci int num_rules = mydriver_jp_regdom.n_reg_rules; 18862306a36Sopenharmony_ci unsigned int i; 18962306a36Sopenharmony_ci 19062306a36Sopenharmony_ci size_of_regd = sizeof(struct ieee80211_regdomain) + 19162306a36Sopenharmony_ci (num_rules * sizeof(struct ieee80211_reg_rule)); 19262306a36Sopenharmony_ci 19362306a36Sopenharmony_ci rd = kzalloc(size_of_regd, GFP_KERNEL); 19462306a36Sopenharmony_ci if (!rd) 19562306a36Sopenharmony_ci return -ENOMEM; 19662306a36Sopenharmony_ci 19762306a36Sopenharmony_ci memcpy(rd, &mydriver_jp_regdom, sizeof(struct ieee80211_regdomain)); 19862306a36Sopenharmony_ci 19962306a36Sopenharmony_ci for (i=0; i < num_rules; i++) 20062306a36Sopenharmony_ci memcpy(&rd->reg_rules[i], 20162306a36Sopenharmony_ci &mydriver_jp_regdom.reg_rules[i], 20262306a36Sopenharmony_ci sizeof(struct ieee80211_reg_rule)); 20362306a36Sopenharmony_ci regulatory_struct_hint(rd); 20462306a36Sopenharmony_ci 20562306a36Sopenharmony_ciStatically compiled regulatory database 20662306a36Sopenharmony_ci--------------------------------------- 20762306a36Sopenharmony_ci 20862306a36Sopenharmony_ciWhen a database should be fixed into the kernel, it can be provided as a 20962306a36Sopenharmony_cifirmware file at build time that is then linked into the kernel. 210