reg.c 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522
  1. /*
  2. * Copyright 2002-2005, Instant802 Networks, Inc.
  3. * Copyright 2005-2006, Devicescape Software, Inc.
  4. * Copyright 2007 Johannes Berg <johannes@sipsolutions.net>
  5. * Copyright 2008-2011 Luis R. Rodriguez <mcgrof@qca.qualcomm.com>
  6. *
  7. * Permission to use, copy, modify, and/or distribute this software for any
  8. * purpose with or without fee is hereby granted, provided that the above
  9. * copyright notice and this permission notice appear in all copies.
  10. *
  11. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  12. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  13. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  14. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  15. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  16. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  17. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  18. */
  19. /**
  20. * DOC: Wireless regulatory infrastructure
  21. *
  22. * The usual implementation is for a driver to read a device EEPROM to
  23. * determine which regulatory domain it should be operating under, then
  24. * looking up the allowable channels in a driver-local table and finally
  25. * registering those channels in the wiphy structure.
  26. *
  27. * Another set of compliance enforcement is for drivers to use their
  28. * own compliance limits which can be stored on the EEPROM. The host
  29. * driver or firmware may ensure these are used.
  30. *
  31. * In addition to all this we provide an extra layer of regulatory
  32. * conformance. For drivers which do not have any regulatory
  33. * information CRDA provides the complete regulatory solution.
  34. * For others it provides a community effort on further restrictions
  35. * to enhance compliance.
  36. *
  37. * Note: When number of rules --> infinity we will not be able to
  38. * index on alpha2 any more, instead we'll probably have to
  39. * rely on some SHA1 checksum of the regdomain for example.
  40. *
  41. */
  42. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  43. #include <linux/kernel.h>
  44. #include <linux/export.h>
  45. #include <linux/slab.h>
  46. #include <linux/list.h>
  47. #include <linux/random.h>
  48. #include <linux/ctype.h>
  49. #include <linux/nl80211.h>
  50. #include <linux/platform_device.h>
  51. #include <linux/moduleparam.h>
  52. #include <net/cfg80211.h>
  53. #include "core.h"
  54. #include "reg.h"
  55. #include "regdb.h"
  56. #include "nl80211.h"
  57. #ifdef CONFIG_CFG80211_REG_DEBUG
  58. #define REG_DBG_PRINT(format, args...) \
  59. printk(KERN_DEBUG pr_fmt(format), ##args)
  60. #else
  61. #define REG_DBG_PRINT(args...)
  62. #endif
  63. static struct regulatory_request core_request_world = {
  64. .initiator = NL80211_REGDOM_SET_BY_CORE,
  65. .alpha2[0] = '0',
  66. .alpha2[1] = '0',
  67. .intersect = false,
  68. .processed = true,
  69. .country_ie_env = ENVIRON_ANY,
  70. };
  71. /* Receipt of information from last regulatory request */
  72. static struct regulatory_request *last_request = &core_request_world;
  73. /* To trigger userspace events */
  74. static struct platform_device *reg_pdev;
  75. static struct device_type reg_device_type = {
  76. .uevent = reg_device_uevent,
  77. };
  78. /*
  79. * Central wireless core regulatory domains, we only need two,
  80. * the current one and a world regulatory domain in case we have no
  81. * information to give us an alpha2
  82. */
  83. const struct ieee80211_regdomain *cfg80211_regdomain;
  84. /*
  85. * Protects static reg.c components:
  86. * - cfg80211_world_regdom
  87. * - cfg80211_regdom
  88. * - last_request
  89. */
  90. static DEFINE_MUTEX(reg_mutex);
  91. static inline void assert_reg_lock(void)
  92. {
  93. lockdep_assert_held(&reg_mutex);
  94. }
  95. /* Used to queue up regulatory hints */
  96. static LIST_HEAD(reg_requests_list);
  97. static spinlock_t reg_requests_lock;
  98. /* Used to queue up beacon hints for review */
  99. static LIST_HEAD(reg_pending_beacons);
  100. static spinlock_t reg_pending_beacons_lock;
  101. /* Used to keep track of processed beacon hints */
  102. static LIST_HEAD(reg_beacon_list);
  103. struct reg_beacon {
  104. struct list_head list;
  105. struct ieee80211_channel chan;
  106. };
  107. static void reg_todo(struct work_struct *work);
  108. static DECLARE_WORK(reg_work, reg_todo);
  109. static void reg_timeout_work(struct work_struct *work);
  110. static DECLARE_DELAYED_WORK(reg_timeout, reg_timeout_work);
  111. /* We keep a static world regulatory domain in case of the absence of CRDA */
  112. static const struct ieee80211_regdomain world_regdom = {
  113. .n_reg_rules = 5,
  114. .alpha2 = "00",
  115. .reg_rules = {
  116. /* IEEE 802.11b/g, channels 1..11 */
  117. REG_RULE(2412-10, 2462+10, 40, 6, 20, 0),
  118. /* IEEE 802.11b/g, channels 12..13. */
  119. REG_RULE(2467-10, 2472+10, 40, 6, 20,
  120. NL80211_RRF_PASSIVE_SCAN |
  121. NL80211_RRF_NO_IBSS),
  122. /* IEEE 802.11 channel 14 - Only JP enables
  123. * this and for 802.11b only */
  124. REG_RULE(2484-10, 2484+10, 20, 6, 20,
  125. NL80211_RRF_PASSIVE_SCAN |
  126. NL80211_RRF_NO_IBSS |
  127. NL80211_RRF_NO_OFDM),
  128. /* IEEE 802.11a, channel 36..48 */
  129. REG_RULE(5180-10, 5240+10, 40, 6, 20,
  130. NL80211_RRF_PASSIVE_SCAN |
  131. NL80211_RRF_NO_IBSS),
  132. /* NB: 5260 MHz - 5700 MHz requies DFS */
  133. /* IEEE 802.11a, channel 149..165 */
  134. REG_RULE(5745-10, 5825+10, 40, 6, 20,
  135. NL80211_RRF_PASSIVE_SCAN |
  136. NL80211_RRF_NO_IBSS),
  137. }
  138. };
  139. static const struct ieee80211_regdomain *cfg80211_world_regdom =
  140. &world_regdom;
  141. static char *ieee80211_regdom = "00";
  142. static char user_alpha2[2];
  143. module_param(ieee80211_regdom, charp, 0444);
  144. MODULE_PARM_DESC(ieee80211_regdom, "IEEE 802.11 regulatory domain code");
  145. static void reset_regdomains(bool full_reset)
  146. {
  147. /* avoid freeing static information or freeing something twice */
  148. if (cfg80211_regdomain == cfg80211_world_regdom)
  149. cfg80211_regdomain = NULL;
  150. if (cfg80211_world_regdom == &world_regdom)
  151. cfg80211_world_regdom = NULL;
  152. if (cfg80211_regdomain == &world_regdom)
  153. cfg80211_regdomain = NULL;
  154. kfree(cfg80211_regdomain);
  155. kfree(cfg80211_world_regdom);
  156. cfg80211_world_regdom = &world_regdom;
  157. cfg80211_regdomain = NULL;
  158. if (!full_reset)
  159. return;
  160. if (last_request != &core_request_world)
  161. kfree(last_request);
  162. last_request = &core_request_world;
  163. }
  164. /*
  165. * Dynamic world regulatory domain requested by the wireless
  166. * core upon initialization
  167. */
  168. static void update_world_regdomain(const struct ieee80211_regdomain *rd)
  169. {
  170. BUG_ON(!last_request);
  171. reset_regdomains(false);
  172. cfg80211_world_regdom = rd;
  173. cfg80211_regdomain = rd;
  174. }
  175. bool is_world_regdom(const char *alpha2)
  176. {
  177. if (!alpha2)
  178. return false;
  179. if (alpha2[0] == '0' && alpha2[1] == '0')
  180. return true;
  181. return false;
  182. }
  183. static bool is_alpha2_set(const char *alpha2)
  184. {
  185. if (!alpha2)
  186. return false;
  187. if (alpha2[0] != 0 && alpha2[1] != 0)
  188. return true;
  189. return false;
  190. }
  191. static bool is_unknown_alpha2(const char *alpha2)
  192. {
  193. if (!alpha2)
  194. return false;
  195. /*
  196. * Special case where regulatory domain was built by driver
  197. * but a specific alpha2 cannot be determined
  198. */
  199. if (alpha2[0] == '9' && alpha2[1] == '9')
  200. return true;
  201. return false;
  202. }
  203. static bool is_intersected_alpha2(const char *alpha2)
  204. {
  205. if (!alpha2)
  206. return false;
  207. /*
  208. * Special case where regulatory domain is the
  209. * result of an intersection between two regulatory domain
  210. * structures
  211. */
  212. if (alpha2[0] == '9' && alpha2[1] == '8')
  213. return true;
  214. return false;
  215. }
  216. static bool is_an_alpha2(const char *alpha2)
  217. {
  218. if (!alpha2)
  219. return false;
  220. if (isalpha(alpha2[0]) && isalpha(alpha2[1]))
  221. return true;
  222. return false;
  223. }
  224. static bool alpha2_equal(const char *alpha2_x, const char *alpha2_y)
  225. {
  226. if (!alpha2_x || !alpha2_y)
  227. return false;
  228. if (alpha2_x[0] == alpha2_y[0] &&
  229. alpha2_x[1] == alpha2_y[1])
  230. return true;
  231. return false;
  232. }
  233. static bool regdom_changes(const char *alpha2)
  234. {
  235. assert_cfg80211_lock();
  236. if (!cfg80211_regdomain)
  237. return true;
  238. if (alpha2_equal(cfg80211_regdomain->alpha2, alpha2))
  239. return false;
  240. return true;
  241. }
  242. /*
  243. * The NL80211_REGDOM_SET_BY_USER regdom alpha2 is cached, this lets
  244. * you know if a valid regulatory hint with NL80211_REGDOM_SET_BY_USER
  245. * has ever been issued.
  246. */
  247. static bool is_user_regdom_saved(void)
  248. {
  249. if (user_alpha2[0] == '9' && user_alpha2[1] == '7')
  250. return false;
  251. /* This would indicate a mistake on the design */
  252. if (WARN((!is_world_regdom(user_alpha2) &&
  253. !is_an_alpha2(user_alpha2)),
  254. "Unexpected user alpha2: %c%c\n",
  255. user_alpha2[0],
  256. user_alpha2[1]))
  257. return false;
  258. return true;
  259. }
  260. static bool is_cfg80211_regdom_intersected(void)
  261. {
  262. return is_intersected_alpha2(cfg80211_regdomain->alpha2);
  263. }
  264. static int reg_copy_regd(const struct ieee80211_regdomain **dst_regd,
  265. const struct ieee80211_regdomain *src_regd)
  266. {
  267. struct ieee80211_regdomain *regd;
  268. int size_of_regd = 0;
  269. unsigned int i;
  270. size_of_regd = sizeof(struct ieee80211_regdomain) +
  271. ((src_regd->n_reg_rules + 1) * sizeof(struct ieee80211_reg_rule));
  272. regd = kzalloc(size_of_regd, GFP_KERNEL);
  273. if (!regd)
  274. return -ENOMEM;
  275. memcpy(regd, src_regd, sizeof(struct ieee80211_regdomain));
  276. for (i = 0; i < src_regd->n_reg_rules; i++)
  277. memcpy(&regd->reg_rules[i], &src_regd->reg_rules[i],
  278. sizeof(struct ieee80211_reg_rule));
  279. *dst_regd = regd;
  280. return 0;
  281. }
  282. #ifdef CONFIG_CFG80211_INTERNAL_REGDB
  283. struct reg_regdb_search_request {
  284. char alpha2[2];
  285. struct list_head list;
  286. };
  287. static LIST_HEAD(reg_regdb_search_list);
  288. static DEFINE_MUTEX(reg_regdb_search_mutex);
  289. static void reg_regdb_search(struct work_struct *work)
  290. {
  291. struct reg_regdb_search_request *request;
  292. const struct ieee80211_regdomain *curdom, *regdom;
  293. int i, r;
  294. bool set_reg = false;
  295. mutex_lock(&cfg80211_mutex);
  296. mutex_lock(&reg_regdb_search_mutex);
  297. while (!list_empty(&reg_regdb_search_list)) {
  298. request = list_first_entry(&reg_regdb_search_list,
  299. struct reg_regdb_search_request,
  300. list);
  301. list_del(&request->list);
  302. for (i=0; i<reg_regdb_size; i++) {
  303. curdom = reg_regdb[i];
  304. if (!memcmp(request->alpha2, curdom->alpha2, 2)) {
  305. r = reg_copy_regd(&regdom, curdom);
  306. if (r)
  307. break;
  308. set_reg = true;
  309. break;
  310. }
  311. }
  312. kfree(request);
  313. }
  314. mutex_unlock(&reg_regdb_search_mutex);
  315. if (set_reg)
  316. set_regdom(regdom);
  317. mutex_unlock(&cfg80211_mutex);
  318. }
  319. static DECLARE_WORK(reg_regdb_work, reg_regdb_search);
  320. static void reg_regdb_query(const char *alpha2)
  321. {
  322. struct reg_regdb_search_request *request;
  323. if (!alpha2)
  324. return;
  325. request = kzalloc(sizeof(struct reg_regdb_search_request), GFP_KERNEL);
  326. if (!request)
  327. return;
  328. memcpy(request->alpha2, alpha2, 2);
  329. mutex_lock(&reg_regdb_search_mutex);
  330. list_add_tail(&request->list, &reg_regdb_search_list);
  331. mutex_unlock(&reg_regdb_search_mutex);
  332. schedule_work(&reg_regdb_work);
  333. }
  334. /* Feel free to add any other sanity checks here */
  335. static void reg_regdb_size_check(void)
  336. {
  337. /* We should ideally BUILD_BUG_ON() but then random builds would fail */
  338. WARN_ONCE(!reg_regdb_size, "db.txt is empty, you should update it...");
  339. }
  340. #else
  341. static inline void reg_regdb_size_check(void) {}
  342. static inline void reg_regdb_query(const char *alpha2) {}
  343. #endif /* CONFIG_CFG80211_INTERNAL_REGDB */
  344. /*
  345. * This lets us keep regulatory code which is updated on a regulatory
  346. * basis in userspace. Country information is filled in by
  347. * reg_device_uevent
  348. */
  349. static int call_crda(const char *alpha2)
  350. {
  351. if (!is_world_regdom((char *) alpha2))
  352. pr_info("Calling CRDA for country: %c%c\n",
  353. alpha2[0], alpha2[1]);
  354. else
  355. pr_info("Calling CRDA to update world regulatory domain\n");
  356. /* query internal regulatory database (if it exists) */
  357. reg_regdb_query(alpha2);
  358. return kobject_uevent(&reg_pdev->dev.kobj, KOBJ_CHANGE);
  359. }
  360. /* Used by nl80211 before kmalloc'ing our regulatory domain */
  361. bool reg_is_valid_request(const char *alpha2)
  362. {
  363. assert_cfg80211_lock();
  364. if (!last_request)
  365. return false;
  366. return alpha2_equal(last_request->alpha2, alpha2);
  367. }
  368. /* Sanity check on a regulatory rule */
  369. static bool is_valid_reg_rule(const struct ieee80211_reg_rule *rule)
  370. {
  371. const struct ieee80211_freq_range *freq_range = &rule->freq_range;
  372. u32 freq_diff;
  373. if (freq_range->start_freq_khz <= 0 || freq_range->end_freq_khz <= 0)
  374. return false;
  375. if (freq_range->start_freq_khz > freq_range->end_freq_khz)
  376. return false;
  377. freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
  378. if (freq_range->end_freq_khz <= freq_range->start_freq_khz ||
  379. freq_range->max_bandwidth_khz > freq_diff)
  380. return false;
  381. return true;
  382. }
  383. static bool is_valid_rd(const struct ieee80211_regdomain *rd)
  384. {
  385. const struct ieee80211_reg_rule *reg_rule = NULL;
  386. unsigned int i;
  387. if (!rd->n_reg_rules)
  388. return false;
  389. if (WARN_ON(rd->n_reg_rules > NL80211_MAX_SUPP_REG_RULES))
  390. return false;
  391. for (i = 0; i < rd->n_reg_rules; i++) {
  392. reg_rule = &rd->reg_rules[i];
  393. if (!is_valid_reg_rule(reg_rule))
  394. return false;
  395. }
  396. return true;
  397. }
  398. static bool reg_does_bw_fit(const struct ieee80211_freq_range *freq_range,
  399. u32 center_freq_khz,
  400. u32 bw_khz)
  401. {
  402. u32 start_freq_khz, end_freq_khz;
  403. start_freq_khz = center_freq_khz - (bw_khz/2);
  404. end_freq_khz = center_freq_khz + (bw_khz/2);
  405. if (start_freq_khz >= freq_range->start_freq_khz &&
  406. end_freq_khz <= freq_range->end_freq_khz)
  407. return true;
  408. return false;
  409. }
  410. /**
  411. * freq_in_rule_band - tells us if a frequency is in a frequency band
  412. * @freq_range: frequency rule we want to query
  413. * @freq_khz: frequency we are inquiring about
  414. *
  415. * This lets us know if a specific frequency rule is or is not relevant to
  416. * a specific frequency's band. Bands are device specific and artificial
  417. * definitions (the "2.4 GHz band" and the "5 GHz band"), however it is
  418. * safe for now to assume that a frequency rule should not be part of a
  419. * frequency's band if the start freq or end freq are off by more than 2 GHz.
  420. * This resolution can be lowered and should be considered as we add
  421. * regulatory rule support for other "bands".
  422. **/
  423. static bool freq_in_rule_band(const struct ieee80211_freq_range *freq_range,
  424. u32 freq_khz)
  425. {
  426. #define ONE_GHZ_IN_KHZ 1000000
  427. if (abs(freq_khz - freq_range->start_freq_khz) <= (2 * ONE_GHZ_IN_KHZ))
  428. return true;
  429. if (abs(freq_khz - freq_range->end_freq_khz) <= (2 * ONE_GHZ_IN_KHZ))
  430. return true;
  431. return false;
  432. #undef ONE_GHZ_IN_KHZ
  433. }
  434. /*
  435. * Helper for regdom_intersect(), this does the real
  436. * mathematical intersection fun
  437. */
  438. static int reg_rules_intersect(
  439. const struct ieee80211_reg_rule *rule1,
  440. const struct ieee80211_reg_rule *rule2,
  441. struct ieee80211_reg_rule *intersected_rule)
  442. {
  443. const struct ieee80211_freq_range *freq_range1, *freq_range2;
  444. struct ieee80211_freq_range *freq_range;
  445. const struct ieee80211_power_rule *power_rule1, *power_rule2;
  446. struct ieee80211_power_rule *power_rule;
  447. u32 freq_diff;
  448. freq_range1 = &rule1->freq_range;
  449. freq_range2 = &rule2->freq_range;
  450. freq_range = &intersected_rule->freq_range;
  451. power_rule1 = &rule1->power_rule;
  452. power_rule2 = &rule2->power_rule;
  453. power_rule = &intersected_rule->power_rule;
  454. freq_range->start_freq_khz = max(freq_range1->start_freq_khz,
  455. freq_range2->start_freq_khz);
  456. freq_range->end_freq_khz = min(freq_range1->end_freq_khz,
  457. freq_range2->end_freq_khz);
  458. freq_range->max_bandwidth_khz = min(freq_range1->max_bandwidth_khz,
  459. freq_range2->max_bandwidth_khz);
  460. freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
  461. if (freq_range->max_bandwidth_khz > freq_diff)
  462. freq_range->max_bandwidth_khz = freq_diff;
  463. power_rule->max_eirp = min(power_rule1->max_eirp,
  464. power_rule2->max_eirp);
  465. power_rule->max_antenna_gain = min(power_rule1->max_antenna_gain,
  466. power_rule2->max_antenna_gain);
  467. intersected_rule->flags = (rule1->flags | rule2->flags);
  468. if (!is_valid_reg_rule(intersected_rule))
  469. return -EINVAL;
  470. return 0;
  471. }
  472. /**
  473. * regdom_intersect - do the intersection between two regulatory domains
  474. * @rd1: first regulatory domain
  475. * @rd2: second regulatory domain
  476. *
  477. * Use this function to get the intersection between two regulatory domains.
  478. * Once completed we will mark the alpha2 for the rd as intersected, "98",
  479. * as no one single alpha2 can represent this regulatory domain.
  480. *
  481. * Returns a pointer to the regulatory domain structure which will hold the
  482. * resulting intersection of rules between rd1 and rd2. We will
  483. * kzalloc() this structure for you.
  484. */
  485. static struct ieee80211_regdomain *regdom_intersect(
  486. const struct ieee80211_regdomain *rd1,
  487. const struct ieee80211_regdomain *rd2)
  488. {
  489. int r, size_of_regd;
  490. unsigned int x, y;
  491. unsigned int num_rules = 0, rule_idx = 0;
  492. const struct ieee80211_reg_rule *rule1, *rule2;
  493. struct ieee80211_reg_rule *intersected_rule;
  494. struct ieee80211_regdomain *rd;
  495. /* This is just a dummy holder to help us count */
  496. struct ieee80211_reg_rule irule;
  497. /* Uses the stack temporarily for counter arithmetic */
  498. intersected_rule = &irule;
  499. memset(intersected_rule, 0, sizeof(struct ieee80211_reg_rule));
  500. if (!rd1 || !rd2)
  501. return NULL;
  502. /*
  503. * First we get a count of the rules we'll need, then we actually
  504. * build them. This is to so we can malloc() and free() a
  505. * regdomain once. The reason we use reg_rules_intersect() here
  506. * is it will return -EINVAL if the rule computed makes no sense.
  507. * All rules that do check out OK are valid.
  508. */
  509. for (x = 0; x < rd1->n_reg_rules; x++) {
  510. rule1 = &rd1->reg_rules[x];
  511. for (y = 0; y < rd2->n_reg_rules; y++) {
  512. rule2 = &rd2->reg_rules[y];
  513. if (!reg_rules_intersect(rule1, rule2,
  514. intersected_rule))
  515. num_rules++;
  516. memset(intersected_rule, 0,
  517. sizeof(struct ieee80211_reg_rule));
  518. }
  519. }
  520. if (!num_rules)
  521. return NULL;
  522. size_of_regd = sizeof(struct ieee80211_regdomain) +
  523. ((num_rules + 1) * sizeof(struct ieee80211_reg_rule));
  524. rd = kzalloc(size_of_regd, GFP_KERNEL);
  525. if (!rd)
  526. return NULL;
  527. for (x = 0; x < rd1->n_reg_rules; x++) {
  528. rule1 = &rd1->reg_rules[x];
  529. for (y = 0; y < rd2->n_reg_rules; y++) {
  530. rule2 = &rd2->reg_rules[y];
  531. /*
  532. * This time around instead of using the stack lets
  533. * write to the target rule directly saving ourselves
  534. * a memcpy()
  535. */
  536. intersected_rule = &rd->reg_rules[rule_idx];
  537. r = reg_rules_intersect(rule1, rule2,
  538. intersected_rule);
  539. /*
  540. * No need to memset here the intersected rule here as
  541. * we're not using the stack anymore
  542. */
  543. if (r)
  544. continue;
  545. rule_idx++;
  546. }
  547. }
  548. if (rule_idx != num_rules) {
  549. kfree(rd);
  550. return NULL;
  551. }
  552. rd->n_reg_rules = num_rules;
  553. rd->alpha2[0] = '9';
  554. rd->alpha2[1] = '8';
  555. return rd;
  556. }
  557. /*
  558. * XXX: add support for the rest of enum nl80211_reg_rule_flags, we may
  559. * want to just have the channel structure use these
  560. */
  561. static u32 map_regdom_flags(u32 rd_flags)
  562. {
  563. u32 channel_flags = 0;
  564. if (rd_flags & NL80211_RRF_PASSIVE_SCAN)
  565. channel_flags |= IEEE80211_CHAN_PASSIVE_SCAN;
  566. if (rd_flags & NL80211_RRF_NO_IBSS)
  567. channel_flags |= IEEE80211_CHAN_NO_IBSS;
  568. if (rd_flags & NL80211_RRF_DFS)
  569. channel_flags |= IEEE80211_CHAN_RADAR;
  570. return channel_flags;
  571. }
  572. static int freq_reg_info_regd(struct wiphy *wiphy,
  573. u32 center_freq,
  574. u32 desired_bw_khz,
  575. const struct ieee80211_reg_rule **reg_rule,
  576. const struct ieee80211_regdomain *custom_regd)
  577. {
  578. int i;
  579. bool band_rule_found = false;
  580. const struct ieee80211_regdomain *regd;
  581. bool bw_fits = false;
  582. if (!desired_bw_khz)
  583. desired_bw_khz = MHZ_TO_KHZ(20);
  584. regd = custom_regd ? custom_regd : cfg80211_regdomain;
  585. /*
  586. * Follow the driver's regulatory domain, if present, unless a country
  587. * IE has been processed or a user wants to help complaince further
  588. */
  589. if (!custom_regd &&
  590. last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
  591. last_request->initiator != NL80211_REGDOM_SET_BY_USER &&
  592. wiphy->regd)
  593. regd = wiphy->regd;
  594. if (!regd)
  595. return -EINVAL;
  596. for (i = 0; i < regd->n_reg_rules; i++) {
  597. const struct ieee80211_reg_rule *rr;
  598. const struct ieee80211_freq_range *fr = NULL;
  599. rr = &regd->reg_rules[i];
  600. fr = &rr->freq_range;
  601. /*
  602. * We only need to know if one frequency rule was
  603. * was in center_freq's band, that's enough, so lets
  604. * not overwrite it once found
  605. */
  606. if (!band_rule_found)
  607. band_rule_found = freq_in_rule_band(fr, center_freq);
  608. bw_fits = reg_does_bw_fit(fr,
  609. center_freq,
  610. desired_bw_khz);
  611. if (band_rule_found && bw_fits) {
  612. *reg_rule = rr;
  613. return 0;
  614. }
  615. }
  616. if (!band_rule_found)
  617. return -ERANGE;
  618. return -EINVAL;
  619. }
  620. int freq_reg_info(struct wiphy *wiphy,
  621. u32 center_freq,
  622. u32 desired_bw_khz,
  623. const struct ieee80211_reg_rule **reg_rule)
  624. {
  625. assert_cfg80211_lock();
  626. return freq_reg_info_regd(wiphy,
  627. center_freq,
  628. desired_bw_khz,
  629. reg_rule,
  630. NULL);
  631. }
  632. EXPORT_SYMBOL(freq_reg_info);
  633. #ifdef CONFIG_CFG80211_REG_DEBUG
  634. static const char *reg_initiator_name(enum nl80211_reg_initiator initiator)
  635. {
  636. switch (initiator) {
  637. case NL80211_REGDOM_SET_BY_CORE:
  638. return "Set by core";
  639. case NL80211_REGDOM_SET_BY_USER:
  640. return "Set by user";
  641. case NL80211_REGDOM_SET_BY_DRIVER:
  642. return "Set by driver";
  643. case NL80211_REGDOM_SET_BY_COUNTRY_IE:
  644. return "Set by country IE";
  645. default:
  646. WARN_ON(1);
  647. return "Set by bug";
  648. }
  649. }
  650. static void chan_reg_rule_print_dbg(struct ieee80211_channel *chan,
  651. u32 desired_bw_khz,
  652. const struct ieee80211_reg_rule *reg_rule)
  653. {
  654. const struct ieee80211_power_rule *power_rule;
  655. const struct ieee80211_freq_range *freq_range;
  656. char max_antenna_gain[32];
  657. power_rule = &reg_rule->power_rule;
  658. freq_range = &reg_rule->freq_range;
  659. if (!power_rule->max_antenna_gain)
  660. snprintf(max_antenna_gain, 32, "N/A");
  661. else
  662. snprintf(max_antenna_gain, 32, "%d", power_rule->max_antenna_gain);
  663. REG_DBG_PRINT("Updating information on frequency %d MHz "
  664. "for a %d MHz width channel with regulatory rule:\n",
  665. chan->center_freq,
  666. KHZ_TO_MHZ(desired_bw_khz));
  667. REG_DBG_PRINT("%d KHz - %d KHz @ %d KHz), (%s mBi, %d mBm)\n",
  668. freq_range->start_freq_khz,
  669. freq_range->end_freq_khz,
  670. freq_range->max_bandwidth_khz,
  671. max_antenna_gain,
  672. power_rule->max_eirp);
  673. }
  674. #else
  675. static void chan_reg_rule_print_dbg(struct ieee80211_channel *chan,
  676. u32 desired_bw_khz,
  677. const struct ieee80211_reg_rule *reg_rule)
  678. {
  679. return;
  680. }
  681. #endif
  682. /*
  683. * Note that right now we assume the desired channel bandwidth
  684. * is always 20 MHz for each individual channel (HT40 uses 20 MHz
  685. * per channel, the primary and the extension channel). To support
  686. * smaller custom bandwidths such as 5 MHz or 10 MHz we'll need a
  687. * new ieee80211_channel.target_bw and re run the regulatory check
  688. * on the wiphy with the target_bw specified. Then we can simply use
  689. * that below for the desired_bw_khz below.
  690. */
  691. static void handle_channel(struct wiphy *wiphy,
  692. enum nl80211_reg_initiator initiator,
  693. enum ieee80211_band band,
  694. unsigned int chan_idx)
  695. {
  696. int r;
  697. u32 flags, bw_flags = 0;
  698. u32 desired_bw_khz = MHZ_TO_KHZ(20);
  699. const struct ieee80211_reg_rule *reg_rule = NULL;
  700. const struct ieee80211_power_rule *power_rule = NULL;
  701. const struct ieee80211_freq_range *freq_range = NULL;
  702. struct ieee80211_supported_band *sband;
  703. struct ieee80211_channel *chan;
  704. struct wiphy *request_wiphy = NULL;
  705. assert_cfg80211_lock();
  706. request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
  707. sband = wiphy->bands[band];
  708. BUG_ON(chan_idx >= sband->n_channels);
  709. chan = &sband->channels[chan_idx];
  710. flags = chan->orig_flags;
  711. r = freq_reg_info(wiphy,
  712. MHZ_TO_KHZ(chan->center_freq),
  713. desired_bw_khz,
  714. &reg_rule);
  715. if (r) {
  716. /*
  717. * We will disable all channels that do not match our
  718. * received regulatory rule unless the hint is coming
  719. * from a Country IE and the Country IE had no information
  720. * about a band. The IEEE 802.11 spec allows for an AP
  721. * to send only a subset of the regulatory rules allowed,
  722. * so an AP in the US that only supports 2.4 GHz may only send
  723. * a country IE with information for the 2.4 GHz band
  724. * while 5 GHz is still supported.
  725. */
  726. if (initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE &&
  727. r == -ERANGE)
  728. return;
  729. if (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
  730. request_wiphy && request_wiphy == wiphy &&
  731. request_wiphy->flags & WIPHY_FLAG_STRICT_REGULATORY) {
  732. REG_DBG_PRINT("Disabling freq %d MHz for good\n",
  733. chan->center_freq);
  734. chan->orig_flags |= IEEE80211_CHAN_DISABLED;
  735. chan->flags = chan->orig_flags;
  736. } else {
  737. REG_DBG_PRINT("Disabling freq %d MHz\n",
  738. chan->center_freq);
  739. chan->flags |= IEEE80211_CHAN_DISABLED;
  740. }
  741. return;
  742. }
  743. chan_reg_rule_print_dbg(chan, desired_bw_khz, reg_rule);
  744. power_rule = &reg_rule->power_rule;
  745. freq_range = &reg_rule->freq_range;
  746. if (freq_range->max_bandwidth_khz < MHZ_TO_KHZ(40))
  747. bw_flags = IEEE80211_CHAN_NO_HT40;
  748. if (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
  749. request_wiphy && request_wiphy == wiphy &&
  750. request_wiphy->flags & WIPHY_FLAG_STRICT_REGULATORY) {
  751. /*
  752. * This guarantees the driver's requested regulatory domain
  753. * will always be used as a base for further regulatory
  754. * settings
  755. */
  756. chan->flags = chan->orig_flags =
  757. map_regdom_flags(reg_rule->flags) | bw_flags;
  758. chan->max_antenna_gain = chan->orig_mag =
  759. (int) MBI_TO_DBI(power_rule->max_antenna_gain);
  760. chan->max_power = chan->orig_mpwr =
  761. (int) MBM_TO_DBM(power_rule->max_eirp);
  762. return;
  763. }
  764. chan->beacon_found = false;
  765. chan->flags = flags | bw_flags | map_regdom_flags(reg_rule->flags);
  766. chan->max_antenna_gain = min(chan->orig_mag,
  767. (int) MBI_TO_DBI(power_rule->max_antenna_gain));
  768. chan->max_reg_power = (int) MBM_TO_DBM(power_rule->max_eirp);
  769. if (chan->orig_mpwr) {
  770. /*
  771. * Devices that use NL80211_COUNTRY_IE_FOLLOW_POWER will always
  772. * follow the passed country IE power settings.
  773. */
  774. if (initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE &&
  775. wiphy->country_ie_pref & NL80211_COUNTRY_IE_FOLLOW_POWER)
  776. chan->max_power = chan->max_reg_power;
  777. else
  778. chan->max_power = min(chan->orig_mpwr,
  779. chan->max_reg_power);
  780. } else
  781. chan->max_power = chan->max_reg_power;
  782. }
  783. static void handle_band(struct wiphy *wiphy,
  784. enum ieee80211_band band,
  785. enum nl80211_reg_initiator initiator)
  786. {
  787. unsigned int i;
  788. struct ieee80211_supported_band *sband;
  789. BUG_ON(!wiphy->bands[band]);
  790. sband = wiphy->bands[band];
  791. for (i = 0; i < sband->n_channels; i++)
  792. handle_channel(wiphy, initiator, band, i);
  793. }
  794. static bool ignore_reg_update(struct wiphy *wiphy,
  795. enum nl80211_reg_initiator initiator)
  796. {
  797. if (!last_request) {
  798. REG_DBG_PRINT("Ignoring regulatory request %s since "
  799. "last_request is not set\n",
  800. reg_initiator_name(initiator));
  801. return true;
  802. }
  803. if (initiator == NL80211_REGDOM_SET_BY_CORE &&
  804. wiphy->flags & WIPHY_FLAG_CUSTOM_REGULATORY) {
  805. REG_DBG_PRINT("Ignoring regulatory request %s "
  806. "since the driver uses its own custom "
  807. "regulatory domain\n",
  808. reg_initiator_name(initiator));
  809. return true;
  810. }
  811. /*
  812. * wiphy->regd will be set once the device has its own
  813. * desired regulatory domain set
  814. */
  815. if (wiphy->flags & WIPHY_FLAG_STRICT_REGULATORY && !wiphy->regd &&
  816. initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
  817. !is_world_regdom(last_request->alpha2)) {
  818. REG_DBG_PRINT("Ignoring regulatory request %s "
  819. "since the driver requires its own regulatory "
  820. "domain to be set first\n",
  821. reg_initiator_name(initiator));
  822. return true;
  823. }
  824. return false;
  825. }
  826. static void handle_reg_beacon(struct wiphy *wiphy,
  827. unsigned int chan_idx,
  828. struct reg_beacon *reg_beacon)
  829. {
  830. struct ieee80211_supported_band *sband;
  831. struct ieee80211_channel *chan;
  832. bool channel_changed = false;
  833. struct ieee80211_channel chan_before;
  834. assert_cfg80211_lock();
  835. sband = wiphy->bands[reg_beacon->chan.band];
  836. chan = &sband->channels[chan_idx];
  837. if (likely(chan->center_freq != reg_beacon->chan.center_freq))
  838. return;
  839. if (chan->beacon_found)
  840. return;
  841. chan->beacon_found = true;
  842. if (wiphy->flags & WIPHY_FLAG_DISABLE_BEACON_HINTS)
  843. return;
  844. chan_before.center_freq = chan->center_freq;
  845. chan_before.flags = chan->flags;
  846. if (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN) {
  847. chan->flags &= ~IEEE80211_CHAN_PASSIVE_SCAN;
  848. channel_changed = true;
  849. }
  850. if (chan->flags & IEEE80211_CHAN_NO_IBSS) {
  851. chan->flags &= ~IEEE80211_CHAN_NO_IBSS;
  852. channel_changed = true;
  853. }
  854. if (channel_changed)
  855. nl80211_send_beacon_hint_event(wiphy, &chan_before, chan);
  856. }
  857. /*
  858. * Called when a scan on a wiphy finds a beacon on
  859. * new channel
  860. */
  861. static void wiphy_update_new_beacon(struct wiphy *wiphy,
  862. struct reg_beacon *reg_beacon)
  863. {
  864. unsigned int i;
  865. struct ieee80211_supported_band *sband;
  866. assert_cfg80211_lock();
  867. if (!wiphy->bands[reg_beacon->chan.band])
  868. return;
  869. sband = wiphy->bands[reg_beacon->chan.band];
  870. for (i = 0; i < sband->n_channels; i++)
  871. handle_reg_beacon(wiphy, i, reg_beacon);
  872. }
  873. /*
  874. * Called upon reg changes or a new wiphy is added
  875. */
  876. static void wiphy_update_beacon_reg(struct wiphy *wiphy)
  877. {
  878. unsigned int i;
  879. struct ieee80211_supported_band *sband;
  880. struct reg_beacon *reg_beacon;
  881. assert_cfg80211_lock();
  882. if (list_empty(&reg_beacon_list))
  883. return;
  884. list_for_each_entry(reg_beacon, &reg_beacon_list, list) {
  885. if (!wiphy->bands[reg_beacon->chan.band])
  886. continue;
  887. sband = wiphy->bands[reg_beacon->chan.band];
  888. for (i = 0; i < sband->n_channels; i++)
  889. handle_reg_beacon(wiphy, i, reg_beacon);
  890. }
  891. }
  892. static bool reg_is_world_roaming(struct wiphy *wiphy)
  893. {
  894. if (is_world_regdom(cfg80211_regdomain->alpha2) ||
  895. (wiphy->regd && is_world_regdom(wiphy->regd->alpha2)))
  896. return true;
  897. if (last_request &&
  898. last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
  899. wiphy->flags & WIPHY_FLAG_CUSTOM_REGULATORY)
  900. return true;
  901. return false;
  902. }
  903. /* Reap the advantages of previously found beacons */
  904. static void reg_process_beacons(struct wiphy *wiphy)
  905. {
  906. /*
  907. * Means we are just firing up cfg80211, so no beacons would
  908. * have been processed yet.
  909. */
  910. if (!last_request)
  911. return;
  912. if (!reg_is_world_roaming(wiphy))
  913. return;
  914. wiphy_update_beacon_reg(wiphy);
  915. }
  916. static bool is_ht40_not_allowed(struct ieee80211_channel *chan)
  917. {
  918. if (!chan)
  919. return true;
  920. if (chan->flags & IEEE80211_CHAN_DISABLED)
  921. return true;
  922. /* This would happen when regulatory rules disallow HT40 completely */
  923. if (IEEE80211_CHAN_NO_HT40 == (chan->flags & (IEEE80211_CHAN_NO_HT40)))
  924. return true;
  925. return false;
  926. }
  927. static void reg_process_ht_flags_channel(struct wiphy *wiphy,
  928. enum ieee80211_band band,
  929. unsigned int chan_idx)
  930. {
  931. struct ieee80211_supported_band *sband;
  932. struct ieee80211_channel *channel;
  933. struct ieee80211_channel *channel_before = NULL, *channel_after = NULL;
  934. unsigned int i;
  935. assert_cfg80211_lock();
  936. sband = wiphy->bands[band];
  937. BUG_ON(chan_idx >= sband->n_channels);
  938. channel = &sband->channels[chan_idx];
  939. if (is_ht40_not_allowed(channel)) {
  940. channel->flags |= IEEE80211_CHAN_NO_HT40;
  941. return;
  942. }
  943. /*
  944. * We need to ensure the extension channels exist to
  945. * be able to use HT40- or HT40+, this finds them (or not)
  946. */
  947. for (i = 0; i < sband->n_channels; i++) {
  948. struct ieee80211_channel *c = &sband->channels[i];
  949. if (c->center_freq == (channel->center_freq - 20))
  950. channel_before = c;
  951. if (c->center_freq == (channel->center_freq + 20))
  952. channel_after = c;
  953. }
  954. /*
  955. * Please note that this assumes target bandwidth is 20 MHz,
  956. * if that ever changes we also need to change the below logic
  957. * to include that as well.
  958. */
  959. if (is_ht40_not_allowed(channel_before))
  960. channel->flags |= IEEE80211_CHAN_NO_HT40MINUS;
  961. else
  962. channel->flags &= ~IEEE80211_CHAN_NO_HT40MINUS;
  963. if (is_ht40_not_allowed(channel_after))
  964. channel->flags |= IEEE80211_CHAN_NO_HT40PLUS;
  965. else
  966. channel->flags &= ~IEEE80211_CHAN_NO_HT40PLUS;
  967. }
  968. static void reg_process_ht_flags_band(struct wiphy *wiphy,
  969. enum ieee80211_band band)
  970. {
  971. unsigned int i;
  972. struct ieee80211_supported_band *sband;
  973. BUG_ON(!wiphy->bands[band]);
  974. sband = wiphy->bands[band];
  975. for (i = 0; i < sband->n_channels; i++)
  976. reg_process_ht_flags_channel(wiphy, band, i);
  977. }
  978. static void reg_process_ht_flags(struct wiphy *wiphy)
  979. {
  980. enum ieee80211_band band;
  981. if (!wiphy)
  982. return;
  983. for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
  984. if (wiphy->bands[band])
  985. reg_process_ht_flags_band(wiphy, band);
  986. }
  987. }
  988. static void wiphy_update_regulatory(struct wiphy *wiphy,
  989. enum nl80211_reg_initiator initiator)
  990. {
  991. enum ieee80211_band band;
  992. assert_reg_lock();
  993. if (ignore_reg_update(wiphy, initiator))
  994. return;
  995. last_request->dfs_region = cfg80211_regdomain->dfs_region;
  996. for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
  997. if (wiphy->bands[band])
  998. handle_band(wiphy, band, initiator);
  999. }
  1000. reg_process_beacons(wiphy);
  1001. reg_process_ht_flags(wiphy);
  1002. if (wiphy->reg_notifier)
  1003. wiphy->reg_notifier(wiphy, last_request);
  1004. }
  1005. void regulatory_update(struct wiphy *wiphy,
  1006. enum nl80211_reg_initiator setby)
  1007. {
  1008. mutex_lock(&reg_mutex);
  1009. if (last_request)
  1010. wiphy_update_regulatory(wiphy, last_request->initiator);
  1011. else
  1012. wiphy_update_regulatory(wiphy, setby);
  1013. mutex_unlock(&reg_mutex);
  1014. }
  1015. static void update_all_wiphy_regulatory(enum nl80211_reg_initiator initiator)
  1016. {
  1017. struct cfg80211_registered_device *rdev;
  1018. struct wiphy *wiphy;
  1019. list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
  1020. wiphy = &rdev->wiphy;
  1021. wiphy_update_regulatory(wiphy, initiator);
  1022. /*
  1023. * Regulatory updates set by CORE are ignored for custom
  1024. * regulatory cards. Let us notify the changes to the driver,
  1025. * as some drivers used this to restore its orig_* reg domain.
  1026. */
  1027. if (initiator == NL80211_REGDOM_SET_BY_CORE &&
  1028. wiphy->flags & WIPHY_FLAG_CUSTOM_REGULATORY &&
  1029. wiphy->reg_notifier)
  1030. wiphy->reg_notifier(wiphy, last_request);
  1031. }
  1032. }
  1033. static void handle_channel_custom(struct wiphy *wiphy,
  1034. enum ieee80211_band band,
  1035. unsigned int chan_idx,
  1036. const struct ieee80211_regdomain *regd)
  1037. {
  1038. int r;
  1039. u32 desired_bw_khz = MHZ_TO_KHZ(20);
  1040. u32 bw_flags = 0;
  1041. const struct ieee80211_reg_rule *reg_rule = NULL;
  1042. const struct ieee80211_power_rule *power_rule = NULL;
  1043. const struct ieee80211_freq_range *freq_range = NULL;
  1044. struct ieee80211_supported_band *sband;
  1045. struct ieee80211_channel *chan;
  1046. assert_reg_lock();
  1047. sband = wiphy->bands[band];
  1048. BUG_ON(chan_idx >= sband->n_channels);
  1049. chan = &sband->channels[chan_idx];
  1050. r = freq_reg_info_regd(wiphy,
  1051. MHZ_TO_KHZ(chan->center_freq),
  1052. desired_bw_khz,
  1053. &reg_rule,
  1054. regd);
  1055. if (r) {
  1056. REG_DBG_PRINT("Disabling freq %d MHz as custom "
  1057. "regd has no rule that fits a %d MHz "
  1058. "wide channel\n",
  1059. chan->center_freq,
  1060. KHZ_TO_MHZ(desired_bw_khz));
  1061. chan->orig_flags |= IEEE80211_CHAN_DISABLED;
  1062. chan->flags = chan->orig_flags;
  1063. return;
  1064. }
  1065. chan_reg_rule_print_dbg(chan, desired_bw_khz, reg_rule);
  1066. power_rule = &reg_rule->power_rule;
  1067. freq_range = &reg_rule->freq_range;
  1068. if (freq_range->max_bandwidth_khz < MHZ_TO_KHZ(40))
  1069. bw_flags = IEEE80211_CHAN_NO_HT40;
  1070. chan->flags |= map_regdom_flags(reg_rule->flags) | bw_flags;
  1071. chan->max_antenna_gain = (int) MBI_TO_DBI(power_rule->max_antenna_gain);
  1072. chan->max_power = (int) MBM_TO_DBM(power_rule->max_eirp);
  1073. }
  1074. static void handle_band_custom(struct wiphy *wiphy, enum ieee80211_band band,
  1075. const struct ieee80211_regdomain *regd)
  1076. {
  1077. unsigned int i;
  1078. struct ieee80211_supported_band *sband;
  1079. BUG_ON(!wiphy->bands[band]);
  1080. sband = wiphy->bands[band];
  1081. for (i = 0; i < sband->n_channels; i++)
  1082. handle_channel_custom(wiphy, band, i, regd);
  1083. }
  1084. /* Used by drivers prior to wiphy registration */
  1085. void wiphy_apply_custom_regulatory(struct wiphy *wiphy,
  1086. const struct ieee80211_regdomain *regd)
  1087. {
  1088. enum ieee80211_band band;
  1089. unsigned int bands_set = 0;
  1090. mutex_lock(&reg_mutex);
  1091. for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
  1092. if (!wiphy->bands[band])
  1093. continue;
  1094. handle_band_custom(wiphy, band, regd);
  1095. bands_set++;
  1096. }
  1097. mutex_unlock(&reg_mutex);
  1098. /*
  1099. * no point in calling this if it won't have any effect
  1100. * on your device's supportd bands.
  1101. */
  1102. WARN_ON(!bands_set);
  1103. }
  1104. EXPORT_SYMBOL(wiphy_apply_custom_regulatory);
  1105. /*
  1106. * Return value which can be used by ignore_request() to indicate
  1107. * it has been determined we should intersect two regulatory domains
  1108. */
  1109. #define REG_INTERSECT 1
  1110. /* This has the logic which determines when a new request
  1111. * should be ignored. */
  1112. static int ignore_request(struct wiphy *wiphy,
  1113. struct regulatory_request *pending_request)
  1114. {
  1115. struct wiphy *last_wiphy = NULL;
  1116. assert_cfg80211_lock();
  1117. /* All initial requests are respected */
  1118. if (!last_request)
  1119. return 0;
  1120. switch (pending_request->initiator) {
  1121. case NL80211_REGDOM_SET_BY_CORE:
  1122. return 0;
  1123. case NL80211_REGDOM_SET_BY_COUNTRY_IE:
  1124. if (wiphy->country_ie_pref & NL80211_COUNTRY_IE_IGNORE_CORE)
  1125. return -EALREADY;
  1126. last_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
  1127. if (unlikely(!is_an_alpha2(pending_request->alpha2)))
  1128. return -EINVAL;
  1129. if (last_request->initiator ==
  1130. NL80211_REGDOM_SET_BY_COUNTRY_IE) {
  1131. if (last_wiphy != wiphy) {
  1132. /*
  1133. * Two cards with two APs claiming different
  1134. * Country IE alpha2s. We could
  1135. * intersect them, but that seems unlikely
  1136. * to be correct. Reject second one for now.
  1137. */
  1138. if (regdom_changes(pending_request->alpha2))
  1139. return -EOPNOTSUPP;
  1140. return -EALREADY;
  1141. }
  1142. /*
  1143. * Two consecutive Country IE hints on the same wiphy.
  1144. * This should be picked up early by the driver/stack
  1145. */
  1146. if (WARN_ON(regdom_changes(pending_request->alpha2)))
  1147. return 0;
  1148. return -EALREADY;
  1149. }
  1150. return 0;
  1151. case NL80211_REGDOM_SET_BY_DRIVER:
  1152. if (last_request->initiator == NL80211_REGDOM_SET_BY_CORE) {
  1153. if (regdom_changes(pending_request->alpha2))
  1154. return 0;
  1155. return -EALREADY;
  1156. }
  1157. /*
  1158. * This would happen if you unplug and plug your card
  1159. * back in or if you add a new device for which the previously
  1160. * loaded card also agrees on the regulatory domain.
  1161. */
  1162. if (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
  1163. !regdom_changes(pending_request->alpha2))
  1164. return -EALREADY;
  1165. return REG_INTERSECT;
  1166. case NL80211_REGDOM_SET_BY_USER:
  1167. if (last_request->initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE)
  1168. return REG_INTERSECT;
  1169. /*
  1170. * If the user knows better the user should set the regdom
  1171. * to their country before the IE is picked up
  1172. */
  1173. if (last_request->initiator == NL80211_REGDOM_SET_BY_USER &&
  1174. last_request->intersect)
  1175. return -EOPNOTSUPP;
  1176. /*
  1177. * Process user requests only after previous user/driver/core
  1178. * requests have been processed
  1179. */
  1180. if ((last_request->initiator == NL80211_REGDOM_SET_BY_CORE ||
  1181. last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER ||
  1182. last_request->initiator == NL80211_REGDOM_SET_BY_USER)) {
  1183. if (last_request->intersect) {
  1184. if (!is_cfg80211_regdom_intersected())
  1185. return -EAGAIN;
  1186. } else if (regdom_changes(last_request->alpha2)) {
  1187. return -EAGAIN;
  1188. }
  1189. }
  1190. if (!regdom_changes(pending_request->alpha2))
  1191. return -EALREADY;
  1192. return 0;
  1193. }
  1194. return -EINVAL;
  1195. }
  1196. static void reg_set_request_processed(void)
  1197. {
  1198. bool need_more_processing = false;
  1199. #ifdef CONFIG_CFG80211_REG_NOT_UPDATED
  1200. /*
  1201. * SAMSUNG FIX : Regulatory Configuration was update
  1202. * via WIPHY_FLAG_CUSTOM_REGULATORY of Wi-Fi Driver.
  1203. * Regulation should not updated even if device found other country Access Point Beacon once
  1204. * since device should find around other Access Points.
  1205. * 2014.1.8 Convergence Wi-Fi Core
  1206. */
  1207. printk("regulatory is not upadted via %s.\n", __func__);
  1208. return;
  1209. #endif
  1210. last_request->processed = true;
  1211. spin_lock(&reg_requests_lock);
  1212. if (!list_empty(&reg_requests_list))
  1213. need_more_processing = true;
  1214. spin_unlock(&reg_requests_lock);
  1215. if (last_request->initiator == NL80211_REGDOM_SET_BY_USER)
  1216. cancel_delayed_work(&reg_timeout);
  1217. if (need_more_processing)
  1218. schedule_work(&reg_work);
  1219. }
  1220. /**
  1221. * __regulatory_hint - hint to the wireless core a regulatory domain
  1222. * @wiphy: if the hint comes from country information from an AP, this
  1223. * is required to be set to the wiphy that received the information
  1224. * @pending_request: the regulatory request currently being processed
  1225. *
  1226. * The Wireless subsystem can use this function to hint to the wireless core
  1227. * what it believes should be the current regulatory domain.
  1228. *
  1229. * Returns zero if all went fine, %-EALREADY if a regulatory domain had
  1230. * already been set or other standard error codes.
  1231. *
  1232. * Caller must hold &cfg80211_mutex and &reg_mutex
  1233. */
  1234. static int __regulatory_hint(struct wiphy *wiphy,
  1235. struct regulatory_request *pending_request)
  1236. {
  1237. bool intersect = false;
  1238. int r = 0;
  1239. assert_cfg80211_lock();
  1240. r = ignore_request(wiphy, pending_request);
  1241. if (r == REG_INTERSECT) {
  1242. if (pending_request->initiator ==
  1243. NL80211_REGDOM_SET_BY_DRIVER) {
  1244. r = reg_copy_regd(&wiphy->regd, cfg80211_regdomain);
  1245. if (r) {
  1246. kfree(pending_request);
  1247. return r;
  1248. }
  1249. }
  1250. intersect = true;
  1251. } else if (r) {
  1252. /*
  1253. * If the regulatory domain being requested by the
  1254. * driver has already been set just copy it to the
  1255. * wiphy
  1256. */
  1257. if (r == -EALREADY &&
  1258. pending_request->initiator ==
  1259. NL80211_REGDOM_SET_BY_DRIVER) {
  1260. r = reg_copy_regd(&wiphy->regd, cfg80211_regdomain);
  1261. if (r) {
  1262. kfree(pending_request);
  1263. return r;
  1264. }
  1265. r = -EALREADY;
  1266. goto new_request;
  1267. }
  1268. kfree(pending_request);
  1269. return r;
  1270. }
  1271. new_request:
  1272. if (last_request != &core_request_world)
  1273. kfree(last_request);
  1274. last_request = pending_request;
  1275. last_request->intersect = intersect;
  1276. pending_request = NULL;
  1277. if (last_request->initiator == NL80211_REGDOM_SET_BY_USER) {
  1278. user_alpha2[0] = last_request->alpha2[0];
  1279. user_alpha2[1] = last_request->alpha2[1];
  1280. }
  1281. /* When r == REG_INTERSECT we do need to call CRDA */
  1282. if (r < 0) {
  1283. /*
  1284. * Since CRDA will not be called in this case as we already
  1285. * have applied the requested regulatory domain before we just
  1286. * inform userspace we have processed the request
  1287. */
  1288. if (r == -EALREADY) {
  1289. nl80211_send_reg_change_event(last_request);
  1290. reg_set_request_processed();
  1291. }
  1292. return r;
  1293. }
  1294. return call_crda(last_request->alpha2);
  1295. }
  1296. /* This processes *all* regulatory hints */
  1297. static void reg_process_hint(struct regulatory_request *reg_request,
  1298. enum nl80211_reg_initiator reg_initiator)
  1299. {
  1300. int r = 0;
  1301. struct wiphy *wiphy = NULL;
  1302. BUG_ON(!reg_request->alpha2);
  1303. if (wiphy_idx_valid(reg_request->wiphy_idx))
  1304. wiphy = wiphy_idx_to_wiphy(reg_request->wiphy_idx);
  1305. if ((reg_initiator == NL80211_REGDOM_SET_BY_DRIVER ||
  1306. reg_initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE) && !wiphy) {
  1307. kfree(reg_request);
  1308. return;
  1309. }
  1310. r = __regulatory_hint(wiphy, reg_request);
  1311. /* This is required so that the orig_* parameters are saved */
  1312. if (r == -EALREADY && wiphy &&
  1313. wiphy->flags & WIPHY_FLAG_STRICT_REGULATORY) {
  1314. wiphy_update_regulatory(wiphy, reg_initiator);
  1315. return;
  1316. }
  1317. /*
  1318. * We only time out user hints, given that they should be the only
  1319. * source of bogus requests.
  1320. */
  1321. if (r != -EALREADY &&
  1322. reg_initiator == NL80211_REGDOM_SET_BY_USER)
  1323. schedule_delayed_work(&reg_timeout, msecs_to_jiffies(3142));
  1324. }
  1325. /*
  1326. * Processes regulatory hints, this is all the NL80211_REGDOM_SET_BY_*
  1327. * Regulatory hints come on a first come first serve basis and we
  1328. * must process each one atomically.
  1329. */
  1330. static void reg_process_pending_hints(void)
  1331. {
  1332. struct regulatory_request *reg_request;
  1333. mutex_lock(&cfg80211_mutex);
  1334. mutex_lock(&reg_mutex);
  1335. /* When last_request->processed becomes true this will be rescheduled */
  1336. if (last_request && !last_request->processed) {
  1337. REG_DBG_PRINT("Pending regulatory request, waiting "
  1338. "for it to be processed...\n");
  1339. goto out;
  1340. }
  1341. spin_lock(&reg_requests_lock);
  1342. if (list_empty(&reg_requests_list)) {
  1343. spin_unlock(&reg_requests_lock);
  1344. goto out;
  1345. }
  1346. reg_request = list_first_entry(&reg_requests_list,
  1347. struct regulatory_request,
  1348. list);
  1349. list_del_init(&reg_request->list);
  1350. spin_unlock(&reg_requests_lock);
  1351. reg_process_hint(reg_request, reg_request->initiator);
  1352. out:
  1353. mutex_unlock(&reg_mutex);
  1354. mutex_unlock(&cfg80211_mutex);
  1355. }
  1356. /* Processes beacon hints -- this has nothing to do with country IEs */
  1357. static void reg_process_pending_beacon_hints(void)
  1358. {
  1359. struct cfg80211_registered_device *rdev;
  1360. struct reg_beacon *pending_beacon, *tmp;
  1361. /*
  1362. * No need to hold the reg_mutex here as we just touch wiphys
  1363. * and do not read or access regulatory variables.
  1364. */
  1365. mutex_lock(&cfg80211_mutex);
  1366. /* This goes through the _pending_ beacon list */
  1367. spin_lock_bh(&reg_pending_beacons_lock);
  1368. if (list_empty(&reg_pending_beacons)) {
  1369. spin_unlock_bh(&reg_pending_beacons_lock);
  1370. goto out;
  1371. }
  1372. list_for_each_entry_safe(pending_beacon, tmp,
  1373. &reg_pending_beacons, list) {
  1374. list_del_init(&pending_beacon->list);
  1375. /* Applies the beacon hint to current wiphys */
  1376. list_for_each_entry(rdev, &cfg80211_rdev_list, list)
  1377. wiphy_update_new_beacon(&rdev->wiphy, pending_beacon);
  1378. /* Remembers the beacon hint for new wiphys or reg changes */
  1379. list_add_tail(&pending_beacon->list, &reg_beacon_list);
  1380. }
  1381. spin_unlock_bh(&reg_pending_beacons_lock);
  1382. out:
  1383. mutex_unlock(&cfg80211_mutex);
  1384. }
  1385. static void reg_todo(struct work_struct *work)
  1386. {
  1387. reg_process_pending_hints();
  1388. reg_process_pending_beacon_hints();
  1389. }
  1390. static void queue_regulatory_request(struct regulatory_request *request)
  1391. {
  1392. #ifdef CONFIG_CFG80211_REG_NOT_UPDATED
  1393. /*
  1394. * SAMSUNG FIX : Regulatory Configuration was update
  1395. * via WIPHY_FLAG_CUSTOM_REGULATORY of Wi-Fi Driver.
  1396. * Regulation should not updated even if device found other country Access Point Beacon once
  1397. * since device should find around other Access Points.
  1398. * 2014.1.8 Convergence Wi-Fi Core
  1399. */
  1400. printk("regulatory is not upadted via %s.\n", __func__);
  1401. if (request)
  1402. kfree(request);
  1403. return;
  1404. #endif
  1405. if (isalpha(request->alpha2[0]))
  1406. request->alpha2[0] = toupper(request->alpha2[0]);
  1407. if (isalpha(request->alpha2[1]))
  1408. request->alpha2[1] = toupper(request->alpha2[1]);
  1409. spin_lock(&reg_requests_lock);
  1410. list_add_tail(&request->list, &reg_requests_list);
  1411. spin_unlock(&reg_requests_lock);
  1412. schedule_work(&reg_work);
  1413. }
  1414. /*
  1415. * Core regulatory hint -- happens during cfg80211_init()
  1416. * and when we restore regulatory settings.
  1417. */
  1418. static int regulatory_hint_core(const char *alpha2)
  1419. {
  1420. struct regulatory_request *request;
  1421. request = kzalloc(sizeof(struct regulatory_request),
  1422. GFP_KERNEL);
  1423. if (!request)
  1424. return -ENOMEM;
  1425. request->alpha2[0] = alpha2[0];
  1426. request->alpha2[1] = alpha2[1];
  1427. request->initiator = NL80211_REGDOM_SET_BY_CORE;
  1428. #ifdef CONFIG_WCNSS_CORE
  1429. /* FIXME workaround */
  1430. request->processed = true;
  1431. #endif
  1432. queue_regulatory_request(request);
  1433. return 0;
  1434. }
  1435. /* User hints */
  1436. int regulatory_hint_user(const char *alpha2)
  1437. {
  1438. struct regulatory_request *request;
  1439. BUG_ON(!alpha2);
  1440. request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
  1441. if (!request)
  1442. return -ENOMEM;
  1443. request->wiphy_idx = WIPHY_IDX_STALE;
  1444. request->alpha2[0] = alpha2[0];
  1445. request->alpha2[1] = alpha2[1];
  1446. request->initiator = NL80211_REGDOM_SET_BY_USER;
  1447. queue_regulatory_request(request);
  1448. return 0;
  1449. }
  1450. EXPORT_SYMBOL(regulatory_hint_user);
  1451. /* Driver hints */
  1452. int regulatory_hint(struct wiphy *wiphy, const char *alpha2)
  1453. {
  1454. struct regulatory_request *request;
  1455. BUG_ON(!alpha2);
  1456. BUG_ON(!wiphy);
  1457. request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
  1458. if (!request)
  1459. return -ENOMEM;
  1460. request->wiphy_idx = get_wiphy_idx(wiphy);
  1461. /* Must have registered wiphy first */
  1462. BUG_ON(!wiphy_idx_valid(request->wiphy_idx));
  1463. request->alpha2[0] = alpha2[0];
  1464. request->alpha2[1] = alpha2[1];
  1465. request->initiator = NL80211_REGDOM_SET_BY_DRIVER;
  1466. queue_regulatory_request(request);
  1467. return 0;
  1468. }
  1469. EXPORT_SYMBOL(regulatory_hint);
  1470. /*
  1471. * We hold wdev_lock() here so we cannot hold cfg80211_mutex() and
  1472. * therefore cannot iterate over the rdev list here.
  1473. */
  1474. void regulatory_hint_11d(struct wiphy *wiphy,
  1475. enum ieee80211_band band,
  1476. u8 *country_ie,
  1477. u8 country_ie_len)
  1478. {
  1479. char alpha2[2];
  1480. enum environment_cap env = ENVIRON_ANY;
  1481. struct regulatory_request *request;
  1482. mutex_lock(&reg_mutex);
  1483. if (unlikely(!last_request))
  1484. goto out;
  1485. /* IE len must be evenly divisible by 2 */
  1486. if (country_ie_len & 0x01)
  1487. goto out;
  1488. if (country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN)
  1489. goto out;
  1490. alpha2[0] = country_ie[0];
  1491. alpha2[1] = country_ie[1];
  1492. if (country_ie[2] == 'I')
  1493. env = ENVIRON_INDOOR;
  1494. else if (country_ie[2] == 'O')
  1495. env = ENVIRON_OUTDOOR;
  1496. /*
  1497. * We will run this only upon a successful connection on cfg80211.
  1498. * We leave conflict resolution to the workqueue, where can hold
  1499. * cfg80211_mutex.
  1500. */
  1501. if (likely(last_request->initiator ==
  1502. NL80211_REGDOM_SET_BY_COUNTRY_IE &&
  1503. wiphy_idx_valid(last_request->wiphy_idx)))
  1504. goto out;
  1505. request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
  1506. if (!request)
  1507. goto out;
  1508. request->wiphy_idx = get_wiphy_idx(wiphy);
  1509. request->alpha2[0] = alpha2[0];
  1510. request->alpha2[1] = alpha2[1];
  1511. request->initiator = NL80211_REGDOM_SET_BY_COUNTRY_IE;
  1512. request->country_ie_env = env;
  1513. mutex_unlock(&reg_mutex);
  1514. queue_regulatory_request(request);
  1515. return;
  1516. out:
  1517. mutex_unlock(&reg_mutex);
  1518. }
  1519. static void restore_alpha2(char *alpha2, bool reset_user)
  1520. {
  1521. /* indicates there is no alpha2 to consider for restoration */
  1522. alpha2[0] = '9';
  1523. alpha2[1] = '7';
  1524. /* The user setting has precedence over the module parameter */
  1525. if (is_user_regdom_saved()) {
  1526. /* Unless we're asked to ignore it and reset it */
  1527. if (reset_user) {
  1528. REG_DBG_PRINT("Restoring regulatory settings "
  1529. "including user preference\n");
  1530. user_alpha2[0] = '9';
  1531. user_alpha2[1] = '7';
  1532. /*
  1533. * If we're ignoring user settings, we still need to
  1534. * check the module parameter to ensure we put things
  1535. * back as they were for a full restore.
  1536. */
  1537. if (!is_world_regdom(ieee80211_regdom)) {
  1538. REG_DBG_PRINT("Keeping preference on "
  1539. "module parameter ieee80211_regdom: %c%c\n",
  1540. ieee80211_regdom[0],
  1541. ieee80211_regdom[1]);
  1542. alpha2[0] = ieee80211_regdom[0];
  1543. alpha2[1] = ieee80211_regdom[1];
  1544. }
  1545. } else {
  1546. REG_DBG_PRINT("Restoring regulatory settings "
  1547. "while preserving user preference for: %c%c\n",
  1548. user_alpha2[0],
  1549. user_alpha2[1]);
  1550. alpha2[0] = user_alpha2[0];
  1551. alpha2[1] = user_alpha2[1];
  1552. }
  1553. } else if (!is_world_regdom(ieee80211_regdom)) {
  1554. REG_DBG_PRINT("Keeping preference on "
  1555. "module parameter ieee80211_regdom: %c%c\n",
  1556. ieee80211_regdom[0],
  1557. ieee80211_regdom[1]);
  1558. alpha2[0] = ieee80211_regdom[0];
  1559. alpha2[1] = ieee80211_regdom[1];
  1560. } else
  1561. REG_DBG_PRINT("Restoring regulatory settings\n");
  1562. }
  1563. static void restore_custom_reg_settings(struct wiphy *wiphy)
  1564. {
  1565. struct ieee80211_supported_band *sband;
  1566. enum ieee80211_band band;
  1567. struct ieee80211_channel *chan;
  1568. int i;
  1569. for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
  1570. sband = wiphy->bands[band];
  1571. if (!sband)
  1572. continue;
  1573. for (i = 0; i < sband->n_channels; i++) {
  1574. chan = &sband->channels[i];
  1575. chan->flags = chan->orig_flags;
  1576. chan->max_antenna_gain = chan->orig_mag;
  1577. chan->max_power = chan->orig_mpwr;
  1578. }
  1579. }
  1580. }
  1581. /*
  1582. * Restoring regulatory settings involves ingoring any
  1583. * possibly stale country IE information and user regulatory
  1584. * settings if so desired, this includes any beacon hints
  1585. * learned as we could have traveled outside to another country
  1586. * after disconnection. To restore regulatory settings we do
  1587. * exactly what we did at bootup:
  1588. *
  1589. * - send a core regulatory hint
  1590. * - send a user regulatory hint if applicable
  1591. *
  1592. * Device drivers that send a regulatory hint for a specific country
  1593. * keep their own regulatory domain on wiphy->regd so that does does
  1594. * not need to be remembered.
  1595. */
  1596. static void restore_regulatory_settings(bool reset_user)
  1597. {
  1598. char alpha2[2];
  1599. char world_alpha2[2];
  1600. struct reg_beacon *reg_beacon, *btmp;
  1601. struct regulatory_request *reg_request, *tmp;
  1602. LIST_HEAD(tmp_reg_req_list);
  1603. struct cfg80211_registered_device *rdev;
  1604. #ifdef CONFIG_CFG80211_REG_NOT_UPDATED
  1605. /*
  1606. * SAMSUNG FIX : Regulatory Configuration was update
  1607. * via WIPHY_FLAG_CUSTOM_REGULATORY of Wi-Fi Driver.
  1608. * Regulation should not updated even if device found other country Access Point Beacon once
  1609. * since device should find around other Access Points.
  1610. * 2014.1.8 Convergence Wi-Fi Core
  1611. */
  1612. printk("regulatory is not upadted via %s.\n", __func__);
  1613. return;
  1614. #endif
  1615. mutex_lock(&cfg80211_mutex);
  1616. mutex_lock(&reg_mutex);
  1617. reset_regdomains(true);
  1618. restore_alpha2(alpha2, reset_user);
  1619. /*
  1620. * If there's any pending requests we simply
  1621. * stash them to a temporary pending queue and
  1622. * add then after we've restored regulatory
  1623. * settings.
  1624. */
  1625. spin_lock(&reg_requests_lock);
  1626. if (!list_empty(&reg_requests_list)) {
  1627. list_for_each_entry_safe(reg_request, tmp,
  1628. &reg_requests_list, list) {
  1629. if (reg_request->initiator !=
  1630. NL80211_REGDOM_SET_BY_USER)
  1631. continue;
  1632. list_del(&reg_request->list);
  1633. list_add_tail(&reg_request->list, &tmp_reg_req_list);
  1634. }
  1635. }
  1636. spin_unlock(&reg_requests_lock);
  1637. /* Clear beacon hints */
  1638. spin_lock_bh(&reg_pending_beacons_lock);
  1639. if (!list_empty(&reg_pending_beacons)) {
  1640. list_for_each_entry_safe(reg_beacon, btmp,
  1641. &reg_pending_beacons, list) {
  1642. list_del(&reg_beacon->list);
  1643. kfree(reg_beacon);
  1644. }
  1645. }
  1646. spin_unlock_bh(&reg_pending_beacons_lock);
  1647. if (!list_empty(&reg_beacon_list)) {
  1648. list_for_each_entry_safe(reg_beacon, btmp,
  1649. &reg_beacon_list, list) {
  1650. list_del(&reg_beacon->list);
  1651. kfree(reg_beacon);
  1652. }
  1653. }
  1654. /* First restore to the basic regulatory settings */
  1655. cfg80211_regdomain = cfg80211_world_regdom;
  1656. world_alpha2[0] = cfg80211_regdomain->alpha2[0];
  1657. world_alpha2[1] = cfg80211_regdomain->alpha2[1];
  1658. list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
  1659. if (rdev->wiphy.flags & WIPHY_FLAG_CUSTOM_REGULATORY)
  1660. restore_custom_reg_settings(&rdev->wiphy);
  1661. }
  1662. mutex_unlock(&reg_mutex);
  1663. mutex_unlock(&cfg80211_mutex);
  1664. regulatory_hint_core(world_alpha2);
  1665. /*
  1666. * This restores the ieee80211_regdom module parameter
  1667. * preference or the last user requested regulatory
  1668. * settings, user regulatory settings takes precedence.
  1669. */
  1670. if (is_an_alpha2(alpha2))
  1671. regulatory_hint_user(user_alpha2);
  1672. if (list_empty(&tmp_reg_req_list))
  1673. return;
  1674. mutex_lock(&cfg80211_mutex);
  1675. mutex_lock(&reg_mutex);
  1676. spin_lock(&reg_requests_lock);
  1677. list_for_each_entry_safe(reg_request, tmp, &tmp_reg_req_list, list) {
  1678. REG_DBG_PRINT("Adding request for country %c%c back "
  1679. "into the queue\n",
  1680. reg_request->alpha2[0],
  1681. reg_request->alpha2[1]);
  1682. list_del(&reg_request->list);
  1683. list_add_tail(&reg_request->list, &reg_requests_list);
  1684. }
  1685. spin_unlock(&reg_requests_lock);
  1686. mutex_unlock(&reg_mutex);
  1687. mutex_unlock(&cfg80211_mutex);
  1688. REG_DBG_PRINT("Kicking the queue\n");
  1689. schedule_work(&reg_work);
  1690. }
  1691. void regulatory_hint_disconnect(void)
  1692. {
  1693. REG_DBG_PRINT("All devices are disconnected, going to "
  1694. "restore regulatory settings\n");
  1695. restore_regulatory_settings(false);
  1696. }
  1697. static bool freq_is_chan_12_13_14(u16 freq)
  1698. {
  1699. if (freq == ieee80211_channel_to_frequency(12, IEEE80211_BAND_2GHZ) ||
  1700. freq == ieee80211_channel_to_frequency(13, IEEE80211_BAND_2GHZ) ||
  1701. freq == ieee80211_channel_to_frequency(14, IEEE80211_BAND_2GHZ))
  1702. return true;
  1703. return false;
  1704. }
  1705. int regulatory_hint_found_beacon(struct wiphy *wiphy,
  1706. struct ieee80211_channel *beacon_chan,
  1707. gfp_t gfp)
  1708. {
  1709. struct reg_beacon *reg_beacon;
  1710. #ifdef CONFIG_CFG80211_REG_NOT_UPDATED
  1711. /*
  1712. * SAMSUNG FIX : Regulatory Configuration was update
  1713. * via WIPHY_FLAG_CUSTOM_REGULATORY of Wi-Fi Driver.
  1714. * Regulation should not updated even if device found other country Access Point Beacon once
  1715. * since device should find around other Access Points.
  1716. * 2014.1.8 Convergence Wi-Fi Core
  1717. */
  1718. return 0;
  1719. #endif
  1720. if (likely((beacon_chan->beacon_found ||
  1721. (beacon_chan->flags & IEEE80211_CHAN_RADAR) ||
  1722. (beacon_chan->band == IEEE80211_BAND_2GHZ &&
  1723. !freq_is_chan_12_13_14(beacon_chan->center_freq)))))
  1724. return 0;
  1725. reg_beacon = kzalloc(sizeof(struct reg_beacon), gfp);
  1726. if (!reg_beacon)
  1727. return -ENOMEM;
  1728. REG_DBG_PRINT("Found new beacon on "
  1729. "frequency: %d MHz (Ch %d) on %s\n",
  1730. beacon_chan->center_freq,
  1731. ieee80211_frequency_to_channel(beacon_chan->center_freq),
  1732. wiphy_name(wiphy));
  1733. memcpy(&reg_beacon->chan, beacon_chan,
  1734. sizeof(struct ieee80211_channel));
  1735. /*
  1736. * Since we can be called from BH or and non-BH context
  1737. * we must use spin_lock_bh()
  1738. */
  1739. spin_lock_bh(&reg_pending_beacons_lock);
  1740. list_add_tail(&reg_beacon->list, &reg_pending_beacons);
  1741. spin_unlock_bh(&reg_pending_beacons_lock);
  1742. schedule_work(&reg_work);
  1743. return 0;
  1744. }
  1745. static void print_rd_rules(const struct ieee80211_regdomain *rd)
  1746. {
  1747. unsigned int i;
  1748. const struct ieee80211_reg_rule *reg_rule = NULL;
  1749. const struct ieee80211_freq_range *freq_range = NULL;
  1750. const struct ieee80211_power_rule *power_rule = NULL;
  1751. pr_info(" (start_freq - end_freq @ bandwidth), (max_antenna_gain, max_eirp)\n");
  1752. for (i = 0; i < rd->n_reg_rules; i++) {
  1753. reg_rule = &rd->reg_rules[i];
  1754. freq_range = &reg_rule->freq_range;
  1755. power_rule = &reg_rule->power_rule;
  1756. /*
  1757. * There may not be documentation for max antenna gain
  1758. * in certain regions
  1759. */
  1760. if (power_rule->max_antenna_gain)
  1761. pr_info(" (%d KHz - %d KHz @ %d KHz), (%d mBi, %d mBm)\n",
  1762. freq_range->start_freq_khz,
  1763. freq_range->end_freq_khz,
  1764. freq_range->max_bandwidth_khz,
  1765. power_rule->max_antenna_gain,
  1766. power_rule->max_eirp);
  1767. else
  1768. pr_info(" (%d KHz - %d KHz @ %d KHz), (N/A, %d mBm)\n",
  1769. freq_range->start_freq_khz,
  1770. freq_range->end_freq_khz,
  1771. freq_range->max_bandwidth_khz,
  1772. power_rule->max_eirp);
  1773. }
  1774. }
  1775. bool reg_supported_dfs_region(u8 dfs_region)
  1776. {
  1777. switch (dfs_region) {
  1778. case NL80211_DFS_UNSET:
  1779. case NL80211_DFS_FCC:
  1780. case NL80211_DFS_ETSI:
  1781. case NL80211_DFS_JP:
  1782. return true;
  1783. default:
  1784. REG_DBG_PRINT("Ignoring uknown DFS master region: %d\n",
  1785. dfs_region);
  1786. return false;
  1787. }
  1788. }
  1789. static void print_dfs_region(u8 dfs_region)
  1790. {
  1791. if (!dfs_region)
  1792. return;
  1793. switch (dfs_region) {
  1794. case NL80211_DFS_FCC:
  1795. pr_info(" DFS Master region FCC");
  1796. break;
  1797. case NL80211_DFS_ETSI:
  1798. pr_info(" DFS Master region ETSI");
  1799. break;
  1800. case NL80211_DFS_JP:
  1801. pr_info(" DFS Master region JP");
  1802. break;
  1803. default:
  1804. pr_info(" DFS Master region Uknown");
  1805. break;
  1806. }
  1807. }
  1808. static void print_regdomain(const struct ieee80211_regdomain *rd)
  1809. {
  1810. if (is_intersected_alpha2(rd->alpha2)) {
  1811. if (last_request->initiator ==
  1812. NL80211_REGDOM_SET_BY_COUNTRY_IE) {
  1813. struct cfg80211_registered_device *rdev;
  1814. rdev = cfg80211_rdev_by_wiphy_idx(
  1815. last_request->wiphy_idx);
  1816. if (rdev) {
  1817. pr_info("Current regulatory domain updated by AP to: %c%c\n",
  1818. rdev->country_ie_alpha2[0],
  1819. rdev->country_ie_alpha2[1]);
  1820. } else
  1821. pr_info("Current regulatory domain intersected:\n");
  1822. } else
  1823. pr_info("Current regulatory domain intersected:\n");
  1824. } else if (is_world_regdom(rd->alpha2))
  1825. pr_info("World regulatory domain updated:\n");
  1826. else {
  1827. if (is_unknown_alpha2(rd->alpha2))
  1828. pr_info("Regulatory domain changed to driver built-in settings (unknown country)\n");
  1829. else
  1830. pr_info("Regulatory domain changed to country: %c%c\n",
  1831. rd->alpha2[0], rd->alpha2[1]);
  1832. }
  1833. print_dfs_region(rd->dfs_region);
  1834. print_rd_rules(rd);
  1835. }
  1836. static void print_regdomain_info(const struct ieee80211_regdomain *rd)
  1837. {
  1838. pr_info("Regulatory domain: %c%c\n", rd->alpha2[0], rd->alpha2[1]);
  1839. print_rd_rules(rd);
  1840. }
  1841. /* Takes ownership of rd only if it doesn't fail */
  1842. static int __set_regdom(const struct ieee80211_regdomain *rd)
  1843. {
  1844. const struct ieee80211_regdomain *intersected_rd = NULL;
  1845. struct cfg80211_registered_device *rdev = NULL;
  1846. struct wiphy *request_wiphy;
  1847. /* Some basic sanity checks first */
  1848. if (is_world_regdom(rd->alpha2)) {
  1849. if (WARN_ON(!reg_is_valid_request(rd->alpha2)))
  1850. return -EINVAL;
  1851. update_world_regdomain(rd);
  1852. return 0;
  1853. }
  1854. if (!is_alpha2_set(rd->alpha2) && !is_an_alpha2(rd->alpha2) &&
  1855. !is_unknown_alpha2(rd->alpha2))
  1856. return -EINVAL;
  1857. if (!last_request)
  1858. return -EINVAL;
  1859. /*
  1860. * Lets only bother proceeding on the same alpha2 if the current
  1861. * rd is non static (it means CRDA was present and was used last)
  1862. * and the pending request came in from a country IE
  1863. */
  1864. if (last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE) {
  1865. /*
  1866. * If someone else asked us to change the rd lets only bother
  1867. * checking if the alpha2 changes if CRDA was already called
  1868. */
  1869. if (!regdom_changes(rd->alpha2))
  1870. return -EALREADY;
  1871. }
  1872. /*
  1873. * Now lets set the regulatory domain, update all driver channels
  1874. * and finally inform them of what we have done, in case they want
  1875. * to review or adjust their own settings based on their own
  1876. * internal EEPROM data
  1877. */
  1878. if (WARN_ON(!reg_is_valid_request(rd->alpha2)))
  1879. return -EINVAL;
  1880. if (!is_valid_rd(rd)) {
  1881. pr_err("Invalid regulatory domain detected:\n");
  1882. print_regdomain_info(rd);
  1883. return -EINVAL;
  1884. }
  1885. request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
  1886. if (!request_wiphy &&
  1887. (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER ||
  1888. last_request->initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE)) {
  1889. schedule_delayed_work(&reg_timeout, 0);
  1890. return -ENODEV;
  1891. }
  1892. if (!last_request->intersect) {
  1893. int r;
  1894. if (last_request->initiator != NL80211_REGDOM_SET_BY_DRIVER) {
  1895. reset_regdomains(false);
  1896. cfg80211_regdomain = rd;
  1897. return 0;
  1898. }
  1899. /*
  1900. * For a driver hint, lets copy the regulatory domain the
  1901. * driver wanted to the wiphy to deal with conflicts
  1902. */
  1903. /*
  1904. * Userspace could have sent two replies with only
  1905. * one kernel request.
  1906. */
  1907. if (request_wiphy->regd)
  1908. return -EALREADY;
  1909. r = reg_copy_regd(&request_wiphy->regd, rd);
  1910. if (r)
  1911. return r;
  1912. reset_regdomains(false);
  1913. cfg80211_regdomain = rd;
  1914. return 0;
  1915. }
  1916. /* Intersection requires a bit more work */
  1917. if (last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE) {
  1918. intersected_rd = regdom_intersect(rd, cfg80211_regdomain);
  1919. if (!intersected_rd)
  1920. return -EINVAL;
  1921. /*
  1922. * We can trash what CRDA provided now.
  1923. * However if a driver requested this specific regulatory
  1924. * domain we keep it for its private use
  1925. */
  1926. if (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER) {
  1927. const struct ieee80211_regdomain *tmp;
  1928. tmp = request_wiphy->regd;
  1929. request_wiphy->regd = rd;
  1930. kfree(tmp);
  1931. } else {
  1932. kfree(rd);
  1933. }
  1934. rd = NULL;
  1935. reset_regdomains(false);
  1936. cfg80211_regdomain = intersected_rd;
  1937. return 0;
  1938. }
  1939. if (!intersected_rd)
  1940. return -EINVAL;
  1941. rdev = wiphy_to_dev(request_wiphy);
  1942. rdev->country_ie_alpha2[0] = rd->alpha2[0];
  1943. rdev->country_ie_alpha2[1] = rd->alpha2[1];
  1944. rdev->env = last_request->country_ie_env;
  1945. BUG_ON(intersected_rd == rd);
  1946. kfree(rd);
  1947. rd = NULL;
  1948. reset_regdomains(false);
  1949. cfg80211_regdomain = intersected_rd;
  1950. return 0;
  1951. }
  1952. /*
  1953. * Use this call to set the current regulatory domain. Conflicts with
  1954. * multiple drivers can be ironed out later. Caller must've already
  1955. * kmalloc'd the rd structure. Caller must hold cfg80211_mutex
  1956. */
  1957. int set_regdom(const struct ieee80211_regdomain *rd)
  1958. {
  1959. int r;
  1960. assert_cfg80211_lock();
  1961. mutex_lock(&reg_mutex);
  1962. /* Note that this doesn't update the wiphys, this is done below */
  1963. r = __set_regdom(rd);
  1964. if (r) {
  1965. if (r == -EALREADY)
  1966. reg_set_request_processed();
  1967. kfree(rd);
  1968. mutex_unlock(&reg_mutex);
  1969. return r;
  1970. }
  1971. /* This would make this whole thing pointless */
  1972. if (!last_request->intersect)
  1973. BUG_ON(rd != cfg80211_regdomain);
  1974. /* update all wiphys now with the new established regulatory domain */
  1975. update_all_wiphy_regulatory(last_request->initiator);
  1976. print_regdomain(cfg80211_regdomain);
  1977. nl80211_send_reg_change_event(last_request);
  1978. reg_set_request_processed();
  1979. mutex_unlock(&reg_mutex);
  1980. return r;
  1981. }
  1982. #ifdef CONFIG_HOTPLUG
  1983. int reg_device_uevent(struct device *dev, struct kobj_uevent_env *env)
  1984. {
  1985. if (last_request && !last_request->processed) {
  1986. if (add_uevent_var(env, "COUNTRY=%c%c",
  1987. last_request->alpha2[0],
  1988. last_request->alpha2[1]))
  1989. return -ENOMEM;
  1990. }
  1991. return 0;
  1992. }
  1993. #else
  1994. int reg_device_uevent(struct device *dev, struct kobj_uevent_env *env)
  1995. {
  1996. return -ENODEV;
  1997. }
  1998. #endif /* CONFIG_HOTPLUG */
  1999. /* Caller must hold cfg80211_mutex */
  2000. void reg_device_remove(struct wiphy *wiphy)
  2001. {
  2002. struct wiphy *request_wiphy = NULL;
  2003. assert_cfg80211_lock();
  2004. mutex_lock(&reg_mutex);
  2005. kfree(wiphy->regd);
  2006. if (last_request)
  2007. request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
  2008. if (!request_wiphy || request_wiphy != wiphy)
  2009. goto out;
  2010. last_request->wiphy_idx = WIPHY_IDX_STALE;
  2011. last_request->country_ie_env = ENVIRON_ANY;
  2012. out:
  2013. mutex_unlock(&reg_mutex);
  2014. }
  2015. static void reg_timeout_work(struct work_struct *work)
  2016. {
  2017. REG_DBG_PRINT("Timeout while waiting for CRDA to reply, "
  2018. "restoring regulatory settings\n");
  2019. restore_regulatory_settings(true);
  2020. }
  2021. int __init regulatory_init(void)
  2022. {
  2023. int err = 0;
  2024. reg_pdev = platform_device_register_simple("regulatory", 0, NULL, 0);
  2025. if (IS_ERR(reg_pdev))
  2026. return PTR_ERR(reg_pdev);
  2027. reg_pdev->dev.type = &reg_device_type;
  2028. spin_lock_init(&reg_requests_lock);
  2029. spin_lock_init(&reg_pending_beacons_lock);
  2030. reg_regdb_size_check();
  2031. cfg80211_regdomain = cfg80211_world_regdom;
  2032. user_alpha2[0] = '9';
  2033. user_alpha2[1] = '7';
  2034. /* We always try to get an update for the static regdomain */
  2035. err = regulatory_hint_core(cfg80211_regdomain->alpha2);
  2036. if (err) {
  2037. if (err == -ENOMEM)
  2038. return err;
  2039. /*
  2040. * N.B. kobject_uevent_env() can fail mainly for when we're out
  2041. * memory which is handled and propagated appropriately above
  2042. * but it can also fail during a netlink_broadcast() or during
  2043. * early boot for call_usermodehelper(). For now treat these
  2044. * errors as non-fatal.
  2045. */
  2046. pr_err("kobject_uevent_env() was unable to call CRDA during init\n");
  2047. #ifdef CONFIG_CFG80211_REG_DEBUG
  2048. /* We want to find out exactly why when debugging */
  2049. WARN_ON(err);
  2050. #endif
  2051. }
  2052. /*
  2053. * Finally, if the user set the module parameter treat it
  2054. * as a user hint.
  2055. */
  2056. if (!is_world_regdom(ieee80211_regdom))
  2057. regulatory_hint_user(ieee80211_regdom);
  2058. return 0;
  2059. }
  2060. void /* __init_or_exit */ regulatory_exit(void)
  2061. {
  2062. struct regulatory_request *reg_request, *tmp;
  2063. struct reg_beacon *reg_beacon, *btmp;
  2064. cancel_work_sync(&reg_work);
  2065. cancel_delayed_work_sync(&reg_timeout);
  2066. mutex_lock(&cfg80211_mutex);
  2067. mutex_lock(&reg_mutex);
  2068. reset_regdomains(true);
  2069. dev_set_uevent_suppress(&reg_pdev->dev, true);
  2070. platform_device_unregister(reg_pdev);
  2071. spin_lock_bh(&reg_pending_beacons_lock);
  2072. if (!list_empty(&reg_pending_beacons)) {
  2073. list_for_each_entry_safe(reg_beacon, btmp,
  2074. &reg_pending_beacons, list) {
  2075. list_del(&reg_beacon->list);
  2076. kfree(reg_beacon);
  2077. }
  2078. }
  2079. spin_unlock_bh(&reg_pending_beacons_lock);
  2080. if (!list_empty(&reg_beacon_list)) {
  2081. list_for_each_entry_safe(reg_beacon, btmp,
  2082. &reg_beacon_list, list) {
  2083. list_del(&reg_beacon->list);
  2084. kfree(reg_beacon);
  2085. }
  2086. }
  2087. spin_lock(&reg_requests_lock);
  2088. if (!list_empty(&reg_requests_list)) {
  2089. list_for_each_entry_safe(reg_request, tmp,
  2090. &reg_requests_list, list) {
  2091. list_del(&reg_request->list);
  2092. kfree(reg_request);
  2093. }
  2094. }
  2095. spin_unlock(&reg_requests_lock);
  2096. mutex_unlock(&reg_mutex);
  2097. mutex_unlock(&cfg80211_mutex);
  2098. }