reg.c 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533
  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 (freq_range->max_bandwidth_khz < MHZ_TO_KHZ(80))
  749. bw_flags |= IEEE80211_CHAN_NO_80MHZ;
  750. if (freq_range->max_bandwidth_khz < MHZ_TO_KHZ(160))
  751. bw_flags |= IEEE80211_CHAN_NO_160MHZ;
  752. if (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
  753. request_wiphy && request_wiphy == wiphy &&
  754. request_wiphy->flags & WIPHY_FLAG_STRICT_REGULATORY) {
  755. /*
  756. * This guarantees the driver's requested regulatory domain
  757. * will always be used as a base for further regulatory
  758. * settings
  759. */
  760. chan->flags = chan->orig_flags =
  761. map_regdom_flags(reg_rule->flags) | bw_flags;
  762. chan->max_antenna_gain = chan->orig_mag =
  763. (int) MBI_TO_DBI(power_rule->max_antenna_gain);
  764. chan->max_power = chan->orig_mpwr =
  765. (int) MBM_TO_DBM(power_rule->max_eirp);
  766. return;
  767. }
  768. chan->beacon_found = false;
  769. chan->flags = flags | bw_flags | map_regdom_flags(reg_rule->flags);
  770. chan->max_antenna_gain = min(chan->orig_mag,
  771. (int) MBI_TO_DBI(power_rule->max_antenna_gain));
  772. chan->max_reg_power = (int) MBM_TO_DBM(power_rule->max_eirp);
  773. if (chan->orig_mpwr) {
  774. /*
  775. * Devices that use NL80211_COUNTRY_IE_FOLLOW_POWER will always
  776. * follow the passed country IE power settings.
  777. */
  778. if (initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE &&
  779. wiphy->country_ie_pref & NL80211_COUNTRY_IE_FOLLOW_POWER)
  780. chan->max_power = chan->max_reg_power;
  781. else
  782. chan->max_power = min(chan->orig_mpwr,
  783. chan->max_reg_power);
  784. } else
  785. chan->max_power = chan->max_reg_power;
  786. }
  787. static void handle_band(struct wiphy *wiphy,
  788. enum ieee80211_band band,
  789. enum nl80211_reg_initiator initiator)
  790. {
  791. unsigned int i;
  792. struct ieee80211_supported_band *sband;
  793. BUG_ON(!wiphy->bands[band]);
  794. sband = wiphy->bands[band];
  795. for (i = 0; i < sband->n_channels; i++)
  796. handle_channel(wiphy, initiator, band, i);
  797. }
  798. static bool ignore_reg_update(struct wiphy *wiphy,
  799. enum nl80211_reg_initiator initiator)
  800. {
  801. if (!last_request) {
  802. REG_DBG_PRINT("Ignoring regulatory request %s since "
  803. "last_request is not set\n",
  804. reg_initiator_name(initiator));
  805. return true;
  806. }
  807. if (initiator == NL80211_REGDOM_SET_BY_CORE &&
  808. wiphy->flags & WIPHY_FLAG_CUSTOM_REGULATORY) {
  809. REG_DBG_PRINT("Ignoring regulatory request %s "
  810. "since the driver uses its own custom "
  811. "regulatory domain\n",
  812. reg_initiator_name(initiator));
  813. return true;
  814. }
  815. /*
  816. * wiphy->regd will be set once the device has its own
  817. * desired regulatory domain set
  818. */
  819. if (wiphy->flags & WIPHY_FLAG_STRICT_REGULATORY && !wiphy->regd &&
  820. initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
  821. !is_world_regdom(last_request->alpha2)) {
  822. REG_DBG_PRINT("Ignoring regulatory request %s "
  823. "since the driver requires its own regulatory "
  824. "domain to be set first\n",
  825. reg_initiator_name(initiator));
  826. return true;
  827. }
  828. return false;
  829. }
  830. static void handle_reg_beacon(struct wiphy *wiphy,
  831. unsigned int chan_idx,
  832. struct reg_beacon *reg_beacon)
  833. {
  834. struct ieee80211_supported_band *sband;
  835. struct ieee80211_channel *chan;
  836. bool channel_changed = false;
  837. struct ieee80211_channel chan_before;
  838. assert_cfg80211_lock();
  839. sband = wiphy->bands[reg_beacon->chan.band];
  840. chan = &sband->channels[chan_idx];
  841. if (likely(chan->center_freq != reg_beacon->chan.center_freq))
  842. return;
  843. if (chan->beacon_found)
  844. return;
  845. chan->beacon_found = true;
  846. if (wiphy->flags & WIPHY_FLAG_DISABLE_BEACON_HINTS)
  847. return;
  848. chan_before.center_freq = chan->center_freq;
  849. chan_before.flags = chan->flags;
  850. if (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN) {
  851. chan->flags &= ~IEEE80211_CHAN_PASSIVE_SCAN;
  852. channel_changed = true;
  853. }
  854. if (chan->flags & IEEE80211_CHAN_NO_IBSS) {
  855. chan->flags &= ~IEEE80211_CHAN_NO_IBSS;
  856. channel_changed = true;
  857. }
  858. if (channel_changed)
  859. nl80211_send_beacon_hint_event(wiphy, &chan_before, chan);
  860. }
  861. /*
  862. * Called when a scan on a wiphy finds a beacon on
  863. * new channel
  864. */
  865. static void wiphy_update_new_beacon(struct wiphy *wiphy,
  866. struct reg_beacon *reg_beacon)
  867. {
  868. unsigned int i;
  869. struct ieee80211_supported_band *sband;
  870. assert_cfg80211_lock();
  871. if (!wiphy->bands[reg_beacon->chan.band])
  872. return;
  873. sband = wiphy->bands[reg_beacon->chan.band];
  874. for (i = 0; i < sband->n_channels; i++)
  875. handle_reg_beacon(wiphy, i, reg_beacon);
  876. }
  877. /*
  878. * Called upon reg changes or a new wiphy is added
  879. */
  880. static void wiphy_update_beacon_reg(struct wiphy *wiphy)
  881. {
  882. unsigned int i;
  883. struct ieee80211_supported_band *sband;
  884. struct reg_beacon *reg_beacon;
  885. assert_cfg80211_lock();
  886. if (list_empty(&reg_beacon_list))
  887. return;
  888. list_for_each_entry(reg_beacon, &reg_beacon_list, list) {
  889. if (!wiphy->bands[reg_beacon->chan.band])
  890. continue;
  891. sband = wiphy->bands[reg_beacon->chan.band];
  892. for (i = 0; i < sband->n_channels; i++)
  893. handle_reg_beacon(wiphy, i, reg_beacon);
  894. }
  895. }
  896. static bool reg_is_world_roaming(struct wiphy *wiphy)
  897. {
  898. if (is_world_regdom(cfg80211_regdomain->alpha2) ||
  899. (wiphy->regd && is_world_regdom(wiphy->regd->alpha2)))
  900. return true;
  901. if (last_request &&
  902. last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
  903. wiphy->flags & WIPHY_FLAG_CUSTOM_REGULATORY)
  904. return true;
  905. return false;
  906. }
  907. /* Reap the advantages of previously found beacons */
  908. static void reg_process_beacons(struct wiphy *wiphy)
  909. {
  910. /*
  911. * Means we are just firing up cfg80211, so no beacons would
  912. * have been processed yet.
  913. */
  914. if (!last_request)
  915. return;
  916. if (!reg_is_world_roaming(wiphy))
  917. return;
  918. wiphy_update_beacon_reg(wiphy);
  919. }
  920. static bool is_ht40_not_allowed(struct ieee80211_channel *chan)
  921. {
  922. if (!chan)
  923. return true;
  924. if (chan->flags & IEEE80211_CHAN_DISABLED)
  925. return true;
  926. /* This would happen when regulatory rules disallow HT40 completely */
  927. if (IEEE80211_CHAN_NO_HT40 == (chan->flags & (IEEE80211_CHAN_NO_HT40)))
  928. return true;
  929. return false;
  930. }
  931. static void reg_process_ht_flags_channel(struct wiphy *wiphy,
  932. enum ieee80211_band band,
  933. unsigned int chan_idx)
  934. {
  935. struct ieee80211_supported_band *sband;
  936. struct ieee80211_channel *channel;
  937. struct ieee80211_channel *channel_before = NULL, *channel_after = NULL;
  938. unsigned int i;
  939. assert_cfg80211_lock();
  940. sband = wiphy->bands[band];
  941. BUG_ON(chan_idx >= sband->n_channels);
  942. channel = &sband->channels[chan_idx];
  943. if (is_ht40_not_allowed(channel)) {
  944. channel->flags |= IEEE80211_CHAN_NO_HT40;
  945. return;
  946. }
  947. /*
  948. * We need to ensure the extension channels exist to
  949. * be able to use HT40- or HT40+, this finds them (or not)
  950. */
  951. for (i = 0; i < sband->n_channels; i++) {
  952. struct ieee80211_channel *c = &sband->channels[i];
  953. if (c->center_freq == (channel->center_freq - 20))
  954. channel_before = c;
  955. if (c->center_freq == (channel->center_freq + 20))
  956. channel_after = c;
  957. }
  958. /*
  959. * Please note that this assumes target bandwidth is 20 MHz,
  960. * if that ever changes we also need to change the below logic
  961. * to include that as well.
  962. */
  963. if (is_ht40_not_allowed(channel_before))
  964. channel->flags |= IEEE80211_CHAN_NO_HT40MINUS;
  965. else
  966. channel->flags &= ~IEEE80211_CHAN_NO_HT40MINUS;
  967. if (is_ht40_not_allowed(channel_after))
  968. channel->flags |= IEEE80211_CHAN_NO_HT40PLUS;
  969. else
  970. channel->flags &= ~IEEE80211_CHAN_NO_HT40PLUS;
  971. }
  972. static void reg_process_ht_flags_band(struct wiphy *wiphy,
  973. enum ieee80211_band band)
  974. {
  975. unsigned int i;
  976. struct ieee80211_supported_band *sband;
  977. BUG_ON(!wiphy->bands[band]);
  978. sband = wiphy->bands[band];
  979. for (i = 0; i < sband->n_channels; i++)
  980. reg_process_ht_flags_channel(wiphy, band, i);
  981. }
  982. static void reg_process_ht_flags(struct wiphy *wiphy)
  983. {
  984. enum ieee80211_band band;
  985. if (!wiphy)
  986. return;
  987. for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
  988. if (wiphy->bands[band])
  989. reg_process_ht_flags_band(wiphy, band);
  990. }
  991. }
  992. static void wiphy_update_regulatory(struct wiphy *wiphy,
  993. enum nl80211_reg_initiator initiator)
  994. {
  995. enum ieee80211_band band;
  996. assert_reg_lock();
  997. if (ignore_reg_update(wiphy, initiator))
  998. return;
  999. last_request->dfs_region = cfg80211_regdomain->dfs_region;
  1000. for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
  1001. if (wiphy->bands[band])
  1002. handle_band(wiphy, band, initiator);
  1003. }
  1004. reg_process_beacons(wiphy);
  1005. reg_process_ht_flags(wiphy);
  1006. if (wiphy->reg_notifier)
  1007. wiphy->reg_notifier(wiphy, last_request);
  1008. }
  1009. void regulatory_update(struct wiphy *wiphy,
  1010. enum nl80211_reg_initiator setby)
  1011. {
  1012. mutex_lock(&reg_mutex);
  1013. if (last_request)
  1014. wiphy_update_regulatory(wiphy, last_request->initiator);
  1015. else
  1016. wiphy_update_regulatory(wiphy, setby);
  1017. mutex_unlock(&reg_mutex);
  1018. }
  1019. static void update_all_wiphy_regulatory(enum nl80211_reg_initiator initiator)
  1020. {
  1021. struct cfg80211_registered_device *rdev;
  1022. struct wiphy *wiphy;
  1023. list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
  1024. wiphy = &rdev->wiphy;
  1025. wiphy_update_regulatory(wiphy, initiator);
  1026. /*
  1027. * Regulatory updates set by CORE are ignored for custom
  1028. * regulatory cards. Let us notify the changes to the driver,
  1029. * as some drivers used this to restore its orig_* reg domain.
  1030. */
  1031. if (initiator == NL80211_REGDOM_SET_BY_CORE &&
  1032. wiphy->flags & WIPHY_FLAG_CUSTOM_REGULATORY &&
  1033. wiphy->reg_notifier)
  1034. wiphy->reg_notifier(wiphy, last_request);
  1035. }
  1036. }
  1037. static void handle_channel_custom(struct wiphy *wiphy,
  1038. enum ieee80211_band band,
  1039. unsigned int chan_idx,
  1040. const struct ieee80211_regdomain *regd)
  1041. {
  1042. int r;
  1043. u32 desired_bw_khz = MHZ_TO_KHZ(20);
  1044. u32 bw_flags = 0;
  1045. const struct ieee80211_reg_rule *reg_rule = NULL;
  1046. const struct ieee80211_power_rule *power_rule = NULL;
  1047. const struct ieee80211_freq_range *freq_range = NULL;
  1048. struct ieee80211_supported_band *sband;
  1049. struct ieee80211_channel *chan;
  1050. assert_reg_lock();
  1051. sband = wiphy->bands[band];
  1052. BUG_ON(chan_idx >= sband->n_channels);
  1053. chan = &sband->channels[chan_idx];
  1054. r = freq_reg_info_regd(wiphy,
  1055. MHZ_TO_KHZ(chan->center_freq),
  1056. desired_bw_khz,
  1057. &reg_rule,
  1058. regd);
  1059. if (r) {
  1060. REG_DBG_PRINT("Disabling freq %d MHz as custom "
  1061. "regd has no rule that fits a %d MHz "
  1062. "wide channel\n",
  1063. chan->center_freq,
  1064. KHZ_TO_MHZ(desired_bw_khz));
  1065. chan->orig_flags |= IEEE80211_CHAN_DISABLED;
  1066. chan->flags = chan->orig_flags;
  1067. return;
  1068. }
  1069. chan_reg_rule_print_dbg(chan, desired_bw_khz, reg_rule);
  1070. power_rule = &reg_rule->power_rule;
  1071. freq_range = &reg_rule->freq_range;
  1072. if (freq_range->max_bandwidth_khz < MHZ_TO_KHZ(40))
  1073. bw_flags = IEEE80211_CHAN_NO_HT40;
  1074. if (freq_range->max_bandwidth_khz < MHZ_TO_KHZ(80))
  1075. bw_flags |= IEEE80211_CHAN_NO_80MHZ;
  1076. if (freq_range->max_bandwidth_khz < MHZ_TO_KHZ(160))
  1077. bw_flags |= IEEE80211_CHAN_NO_160MHZ;
  1078. chan->flags |= map_regdom_flags(reg_rule->flags) | bw_flags;
  1079. chan->max_antenna_gain = (int) MBI_TO_DBI(power_rule->max_antenna_gain);
  1080. chan->max_power = (int) MBM_TO_DBM(power_rule->max_eirp);
  1081. }
  1082. static void handle_band_custom(struct wiphy *wiphy, enum ieee80211_band band,
  1083. const struct ieee80211_regdomain *regd)
  1084. {
  1085. unsigned int i;
  1086. struct ieee80211_supported_band *sband;
  1087. BUG_ON(!wiphy->bands[band]);
  1088. sband = wiphy->bands[band];
  1089. for (i = 0; i < sband->n_channels; i++)
  1090. handle_channel_custom(wiphy, band, i, regd);
  1091. }
  1092. /* Used by drivers prior to wiphy registration */
  1093. void wiphy_apply_custom_regulatory(struct wiphy *wiphy,
  1094. const struct ieee80211_regdomain *regd)
  1095. {
  1096. enum ieee80211_band band;
  1097. unsigned int bands_set = 0;
  1098. mutex_lock(&reg_mutex);
  1099. for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
  1100. if (!wiphy->bands[band])
  1101. continue;
  1102. handle_band_custom(wiphy, band, regd);
  1103. bands_set++;
  1104. }
  1105. mutex_unlock(&reg_mutex);
  1106. /*
  1107. * no point in calling this if it won't have any effect
  1108. * on your device's supportd bands.
  1109. */
  1110. WARN_ON(!bands_set);
  1111. }
  1112. EXPORT_SYMBOL(wiphy_apply_custom_regulatory);
  1113. /*
  1114. * Return value which can be used by ignore_request() to indicate
  1115. * it has been determined we should intersect two regulatory domains
  1116. */
  1117. #define REG_INTERSECT 1
  1118. /* This has the logic which determines when a new request
  1119. * should be ignored. */
  1120. static int ignore_request(struct wiphy *wiphy,
  1121. struct regulatory_request *pending_request)
  1122. {
  1123. struct wiphy *last_wiphy = NULL;
  1124. assert_cfg80211_lock();
  1125. /* All initial requests are respected */
  1126. if (!last_request)
  1127. return 0;
  1128. switch (pending_request->initiator) {
  1129. case NL80211_REGDOM_SET_BY_CORE:
  1130. return 0;
  1131. case NL80211_REGDOM_SET_BY_COUNTRY_IE:
  1132. if (wiphy->country_ie_pref & NL80211_COUNTRY_IE_IGNORE_CORE)
  1133. return -EALREADY;
  1134. last_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
  1135. if (unlikely(!is_an_alpha2(pending_request->alpha2)))
  1136. return -EINVAL;
  1137. if (last_request->initiator ==
  1138. NL80211_REGDOM_SET_BY_COUNTRY_IE) {
  1139. if (last_wiphy != wiphy) {
  1140. /*
  1141. * Two cards with two APs claiming different
  1142. * Country IE alpha2s. We could
  1143. * intersect them, but that seems unlikely
  1144. * to be correct. Reject second one for now.
  1145. */
  1146. if (regdom_changes(pending_request->alpha2))
  1147. return -EOPNOTSUPP;
  1148. return -EALREADY;
  1149. }
  1150. /*
  1151. * Two consecutive Country IE hints on the same wiphy.
  1152. * This should be picked up early by the driver/stack
  1153. */
  1154. if (WARN_ON(regdom_changes(pending_request->alpha2)))
  1155. return 0;
  1156. return -EALREADY;
  1157. }
  1158. return 0;
  1159. case NL80211_REGDOM_SET_BY_DRIVER:
  1160. if (last_request->initiator == NL80211_REGDOM_SET_BY_CORE) {
  1161. if (regdom_changes(pending_request->alpha2))
  1162. return 0;
  1163. return -EALREADY;
  1164. }
  1165. /*
  1166. * This would happen if you unplug and plug your card
  1167. * back in or if you add a new device for which the previously
  1168. * loaded card also agrees on the regulatory domain.
  1169. */
  1170. if (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
  1171. !regdom_changes(pending_request->alpha2))
  1172. return -EALREADY;
  1173. return REG_INTERSECT;
  1174. case NL80211_REGDOM_SET_BY_USER:
  1175. if (last_request->initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE)
  1176. return REG_INTERSECT;
  1177. /*
  1178. * If the user knows better the user should set the regdom
  1179. * to their country before the IE is picked up
  1180. */
  1181. if (last_request->initiator == NL80211_REGDOM_SET_BY_USER &&
  1182. last_request->intersect)
  1183. return -EOPNOTSUPP;
  1184. /*
  1185. * Process user requests only after previous user/driver/core
  1186. * requests have been processed
  1187. */
  1188. if ((last_request->initiator == NL80211_REGDOM_SET_BY_CORE ||
  1189. last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER ||
  1190. last_request->initiator == NL80211_REGDOM_SET_BY_USER)) {
  1191. if (last_request->intersect) {
  1192. if (!is_cfg80211_regdom_intersected())
  1193. return -EAGAIN;
  1194. } else if (regdom_changes(last_request->alpha2)) {
  1195. return -EAGAIN;
  1196. }
  1197. }
  1198. if (!regdom_changes(pending_request->alpha2))
  1199. return -EALREADY;
  1200. return 0;
  1201. }
  1202. return -EINVAL;
  1203. }
  1204. static void reg_set_request_processed(void)
  1205. {
  1206. bool need_more_processing = false;
  1207. #ifdef CONFIG_CFG80211_REG_NOT_UPDATED
  1208. /*
  1209. * SAMSUNG FIX : Regulatory Configuration was update
  1210. * via WIPHY_FLAG_CUSTOM_REGULATORY of Wi-Fi Driver.
  1211. * Regulation should not updated even if device found other country Access Point Beacon once
  1212. * since device should find around other Access Points.
  1213. * 2014.1.8 Convergence Wi-Fi Core
  1214. */
  1215. printk("regulatory is not upadted via %s.\n", __func__);
  1216. return;
  1217. #endif
  1218. last_request->processed = true;
  1219. spin_lock(&reg_requests_lock);
  1220. if (!list_empty(&reg_requests_list))
  1221. need_more_processing = true;
  1222. spin_unlock(&reg_requests_lock);
  1223. if (last_request->initiator == NL80211_REGDOM_SET_BY_USER)
  1224. cancel_delayed_work(&reg_timeout);
  1225. if (need_more_processing)
  1226. schedule_work(&reg_work);
  1227. }
  1228. /**
  1229. * __regulatory_hint - hint to the wireless core a regulatory domain
  1230. * @wiphy: if the hint comes from country information from an AP, this
  1231. * is required to be set to the wiphy that received the information
  1232. * @pending_request: the regulatory request currently being processed
  1233. *
  1234. * The Wireless subsystem can use this function to hint to the wireless core
  1235. * what it believes should be the current regulatory domain.
  1236. *
  1237. * Returns zero if all went fine, %-EALREADY if a regulatory domain had
  1238. * already been set or other standard error codes.
  1239. *
  1240. * Caller must hold &cfg80211_mutex and &reg_mutex
  1241. */
  1242. static int __regulatory_hint(struct wiphy *wiphy,
  1243. struct regulatory_request *pending_request)
  1244. {
  1245. bool intersect = false;
  1246. int r = 0;
  1247. assert_cfg80211_lock();
  1248. r = ignore_request(wiphy, pending_request);
  1249. if (r == REG_INTERSECT) {
  1250. if (pending_request->initiator ==
  1251. NL80211_REGDOM_SET_BY_DRIVER) {
  1252. r = reg_copy_regd(&wiphy->regd, cfg80211_regdomain);
  1253. if (r) {
  1254. kfree(pending_request);
  1255. return r;
  1256. }
  1257. }
  1258. intersect = true;
  1259. } else if (r) {
  1260. /*
  1261. * If the regulatory domain being requested by the
  1262. * driver has already been set just copy it to the
  1263. * wiphy
  1264. */
  1265. if (r == -EALREADY &&
  1266. pending_request->initiator ==
  1267. NL80211_REGDOM_SET_BY_DRIVER) {
  1268. r = reg_copy_regd(&wiphy->regd, cfg80211_regdomain);
  1269. if (r) {
  1270. kfree(pending_request);
  1271. return r;
  1272. }
  1273. r = -EALREADY;
  1274. goto new_request;
  1275. }
  1276. kfree(pending_request);
  1277. return r;
  1278. }
  1279. new_request:
  1280. if (last_request != &core_request_world)
  1281. kfree(last_request);
  1282. last_request = pending_request;
  1283. last_request->intersect = intersect;
  1284. pending_request = NULL;
  1285. if (last_request->initiator == NL80211_REGDOM_SET_BY_USER) {
  1286. user_alpha2[0] = last_request->alpha2[0];
  1287. user_alpha2[1] = last_request->alpha2[1];
  1288. }
  1289. /* When r == REG_INTERSECT we do need to call CRDA */
  1290. if (r < 0) {
  1291. /*
  1292. * Since CRDA will not be called in this case as we already
  1293. * have applied the requested regulatory domain before we just
  1294. * inform userspace we have processed the request
  1295. */
  1296. if (r == -EALREADY) {
  1297. nl80211_send_reg_change_event(last_request);
  1298. reg_set_request_processed();
  1299. }
  1300. return r;
  1301. }
  1302. return call_crda(last_request->alpha2);
  1303. }
  1304. /* This processes *all* regulatory hints */
  1305. static void reg_process_hint(struct regulatory_request *reg_request,
  1306. enum nl80211_reg_initiator reg_initiator)
  1307. {
  1308. int r = 0;
  1309. struct wiphy *wiphy = NULL;
  1310. BUG_ON(!reg_request->alpha2);
  1311. if (wiphy_idx_valid(reg_request->wiphy_idx))
  1312. wiphy = wiphy_idx_to_wiphy(reg_request->wiphy_idx);
  1313. if ((reg_initiator == NL80211_REGDOM_SET_BY_DRIVER ||
  1314. reg_initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE) && !wiphy) {
  1315. kfree(reg_request);
  1316. return;
  1317. }
  1318. r = __regulatory_hint(wiphy, reg_request);
  1319. /* This is required so that the orig_* parameters are saved */
  1320. if (r == -EALREADY && wiphy &&
  1321. wiphy->flags & WIPHY_FLAG_STRICT_REGULATORY) {
  1322. wiphy_update_regulatory(wiphy, reg_initiator);
  1323. return;
  1324. }
  1325. /*
  1326. * We only time out user hints, given that they should be the only
  1327. * source of bogus requests.
  1328. */
  1329. if (r != -EALREADY &&
  1330. reg_initiator == NL80211_REGDOM_SET_BY_USER)
  1331. schedule_delayed_work(&reg_timeout, msecs_to_jiffies(3142));
  1332. }
  1333. /*
  1334. * Processes regulatory hints, this is all the NL80211_REGDOM_SET_BY_*
  1335. * Regulatory hints come on a first come first serve basis and we
  1336. * must process each one atomically.
  1337. */
  1338. static void reg_process_pending_hints(void)
  1339. {
  1340. struct regulatory_request *reg_request;
  1341. mutex_lock(&cfg80211_mutex);
  1342. mutex_lock(&reg_mutex);
  1343. /* When last_request->processed becomes true this will be rescheduled */
  1344. if (last_request && !last_request->processed) {
  1345. REG_DBG_PRINT("Pending regulatory request, waiting "
  1346. "for it to be processed...\n");
  1347. goto out;
  1348. }
  1349. spin_lock(&reg_requests_lock);
  1350. if (list_empty(&reg_requests_list)) {
  1351. spin_unlock(&reg_requests_lock);
  1352. goto out;
  1353. }
  1354. reg_request = list_first_entry(&reg_requests_list,
  1355. struct regulatory_request,
  1356. list);
  1357. list_del_init(&reg_request->list);
  1358. spin_unlock(&reg_requests_lock);
  1359. reg_process_hint(reg_request, reg_request->initiator);
  1360. out:
  1361. mutex_unlock(&reg_mutex);
  1362. mutex_unlock(&cfg80211_mutex);
  1363. }
  1364. /* Processes beacon hints -- this has nothing to do with country IEs */
  1365. static void reg_process_pending_beacon_hints(void)
  1366. {
  1367. struct cfg80211_registered_device *rdev;
  1368. struct reg_beacon *pending_beacon, *tmp;
  1369. /*
  1370. * No need to hold the reg_mutex here as we just touch wiphys
  1371. * and do not read or access regulatory variables.
  1372. */
  1373. mutex_lock(&cfg80211_mutex);
  1374. /* This goes through the _pending_ beacon list */
  1375. spin_lock_bh(&reg_pending_beacons_lock);
  1376. if (list_empty(&reg_pending_beacons)) {
  1377. spin_unlock_bh(&reg_pending_beacons_lock);
  1378. goto out;
  1379. }
  1380. list_for_each_entry_safe(pending_beacon, tmp,
  1381. &reg_pending_beacons, list) {
  1382. list_del_init(&pending_beacon->list);
  1383. /* Applies the beacon hint to current wiphys */
  1384. list_for_each_entry(rdev, &cfg80211_rdev_list, list)
  1385. wiphy_update_new_beacon(&rdev->wiphy, pending_beacon);
  1386. /* Remembers the beacon hint for new wiphys or reg changes */
  1387. list_add_tail(&pending_beacon->list, &reg_beacon_list);
  1388. }
  1389. spin_unlock_bh(&reg_pending_beacons_lock);
  1390. out:
  1391. mutex_unlock(&cfg80211_mutex);
  1392. }
  1393. static void reg_todo(struct work_struct *work)
  1394. {
  1395. reg_process_pending_hints();
  1396. reg_process_pending_beacon_hints();
  1397. }
  1398. static void queue_regulatory_request(struct regulatory_request *request)
  1399. {
  1400. #ifdef CONFIG_CFG80211_REG_NOT_UPDATED
  1401. /*
  1402. * SAMSUNG FIX : Regulatory Configuration was update
  1403. * via WIPHY_FLAG_CUSTOM_REGULATORY of Wi-Fi Driver.
  1404. * Regulation should not updated even if device found other country Access Point Beacon once
  1405. * since device should find around other Access Points.
  1406. * 2014.1.8 Convergence Wi-Fi Core
  1407. */
  1408. printk("regulatory is not upadted via %s.\n", __func__);
  1409. if (request)
  1410. kfree(request);
  1411. return;
  1412. #endif
  1413. if (isalpha(request->alpha2[0]))
  1414. request->alpha2[0] = toupper(request->alpha2[0]);
  1415. if (isalpha(request->alpha2[1]))
  1416. request->alpha2[1] = toupper(request->alpha2[1]);
  1417. spin_lock(&reg_requests_lock);
  1418. list_add_tail(&request->list, &reg_requests_list);
  1419. spin_unlock(&reg_requests_lock);
  1420. schedule_work(&reg_work);
  1421. }
  1422. /*
  1423. * Core regulatory hint -- happens during cfg80211_init()
  1424. * and when we restore regulatory settings.
  1425. */
  1426. static int regulatory_hint_core(const char *alpha2)
  1427. {
  1428. struct regulatory_request *request;
  1429. request = kzalloc(sizeof(struct regulatory_request),
  1430. GFP_KERNEL);
  1431. if (!request)
  1432. return -ENOMEM;
  1433. request->alpha2[0] = alpha2[0];
  1434. request->alpha2[1] = alpha2[1];
  1435. request->initiator = NL80211_REGDOM_SET_BY_CORE;
  1436. #ifdef CONFIG_WCNSS_CORE
  1437. /* FIXME workaround */
  1438. request->processed = true;
  1439. #endif
  1440. queue_regulatory_request(request);
  1441. return 0;
  1442. }
  1443. /* User hints */
  1444. int regulatory_hint_user(const char *alpha2)
  1445. {
  1446. struct regulatory_request *request;
  1447. BUG_ON(!alpha2);
  1448. if (!is_world_regdom(alpha2) && !is_an_alpha2(alpha2))
  1449. return -EINVAL;
  1450. request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
  1451. if (!request)
  1452. return -ENOMEM;
  1453. request->wiphy_idx = WIPHY_IDX_STALE;
  1454. request->alpha2[0] = alpha2[0];
  1455. request->alpha2[1] = alpha2[1];
  1456. request->initiator = NL80211_REGDOM_SET_BY_USER;
  1457. queue_regulatory_request(request);
  1458. return 0;
  1459. }
  1460. EXPORT_SYMBOL(regulatory_hint_user);
  1461. /* Driver hints */
  1462. int regulatory_hint(struct wiphy *wiphy, const char *alpha2)
  1463. {
  1464. struct regulatory_request *request;
  1465. BUG_ON(!alpha2);
  1466. BUG_ON(!wiphy);
  1467. request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
  1468. if (!request)
  1469. return -ENOMEM;
  1470. request->wiphy_idx = get_wiphy_idx(wiphy);
  1471. /* Must have registered wiphy first */
  1472. BUG_ON(!wiphy_idx_valid(request->wiphy_idx));
  1473. request->alpha2[0] = alpha2[0];
  1474. request->alpha2[1] = alpha2[1];
  1475. request->initiator = NL80211_REGDOM_SET_BY_DRIVER;
  1476. queue_regulatory_request(request);
  1477. return 0;
  1478. }
  1479. EXPORT_SYMBOL(regulatory_hint);
  1480. /*
  1481. * We hold wdev_lock() here so we cannot hold cfg80211_mutex() and
  1482. * therefore cannot iterate over the rdev list here.
  1483. */
  1484. void regulatory_hint_11d(struct wiphy *wiphy,
  1485. enum ieee80211_band band,
  1486. u8 *country_ie,
  1487. u8 country_ie_len)
  1488. {
  1489. char alpha2[2];
  1490. enum environment_cap env = ENVIRON_ANY;
  1491. struct regulatory_request *request;
  1492. mutex_lock(&reg_mutex);
  1493. if (unlikely(!last_request))
  1494. goto out;
  1495. /* IE len must be evenly divisible by 2 */
  1496. if (country_ie_len & 0x01)
  1497. goto out;
  1498. if (country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN)
  1499. goto out;
  1500. alpha2[0] = country_ie[0];
  1501. alpha2[1] = country_ie[1];
  1502. if (country_ie[2] == 'I')
  1503. env = ENVIRON_INDOOR;
  1504. else if (country_ie[2] == 'O')
  1505. env = ENVIRON_OUTDOOR;
  1506. /*
  1507. * We will run this only upon a successful connection on cfg80211.
  1508. * We leave conflict resolution to the workqueue, where can hold
  1509. * cfg80211_mutex.
  1510. */
  1511. if (likely(last_request->initiator ==
  1512. NL80211_REGDOM_SET_BY_COUNTRY_IE &&
  1513. wiphy_idx_valid(last_request->wiphy_idx)))
  1514. goto out;
  1515. request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
  1516. if (!request)
  1517. goto out;
  1518. request->wiphy_idx = get_wiphy_idx(wiphy);
  1519. request->alpha2[0] = alpha2[0];
  1520. request->alpha2[1] = alpha2[1];
  1521. request->initiator = NL80211_REGDOM_SET_BY_COUNTRY_IE;
  1522. request->country_ie_env = env;
  1523. mutex_unlock(&reg_mutex);
  1524. queue_regulatory_request(request);
  1525. return;
  1526. out:
  1527. mutex_unlock(&reg_mutex);
  1528. }
  1529. static void restore_alpha2(char *alpha2, bool reset_user)
  1530. {
  1531. /* indicates there is no alpha2 to consider for restoration */
  1532. alpha2[0] = '9';
  1533. alpha2[1] = '7';
  1534. /* The user setting has precedence over the module parameter */
  1535. if (is_user_regdom_saved()) {
  1536. /* Unless we're asked to ignore it and reset it */
  1537. if (reset_user) {
  1538. REG_DBG_PRINT("Restoring regulatory settings "
  1539. "including user preference\n");
  1540. user_alpha2[0] = '9';
  1541. user_alpha2[1] = '7';
  1542. /*
  1543. * If we're ignoring user settings, we still need to
  1544. * check the module parameter to ensure we put things
  1545. * back as they were for a full restore.
  1546. */
  1547. if (!is_world_regdom(ieee80211_regdom)) {
  1548. REG_DBG_PRINT("Keeping preference on "
  1549. "module parameter ieee80211_regdom: %c%c\n",
  1550. ieee80211_regdom[0],
  1551. ieee80211_regdom[1]);
  1552. alpha2[0] = ieee80211_regdom[0];
  1553. alpha2[1] = ieee80211_regdom[1];
  1554. }
  1555. } else {
  1556. REG_DBG_PRINT("Restoring regulatory settings "
  1557. "while preserving user preference for: %c%c\n",
  1558. user_alpha2[0],
  1559. user_alpha2[1]);
  1560. alpha2[0] = user_alpha2[0];
  1561. alpha2[1] = user_alpha2[1];
  1562. }
  1563. } else if (!is_world_regdom(ieee80211_regdom)) {
  1564. REG_DBG_PRINT("Keeping preference on "
  1565. "module parameter ieee80211_regdom: %c%c\n",
  1566. ieee80211_regdom[0],
  1567. ieee80211_regdom[1]);
  1568. alpha2[0] = ieee80211_regdom[0];
  1569. alpha2[1] = ieee80211_regdom[1];
  1570. } else
  1571. REG_DBG_PRINT("Restoring regulatory settings\n");
  1572. }
  1573. static void restore_custom_reg_settings(struct wiphy *wiphy)
  1574. {
  1575. struct ieee80211_supported_band *sband;
  1576. enum ieee80211_band band;
  1577. struct ieee80211_channel *chan;
  1578. int i;
  1579. for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
  1580. sband = wiphy->bands[band];
  1581. if (!sband)
  1582. continue;
  1583. for (i = 0; i < sband->n_channels; i++) {
  1584. chan = &sband->channels[i];
  1585. chan->flags = chan->orig_flags;
  1586. chan->max_antenna_gain = chan->orig_mag;
  1587. chan->max_power = chan->orig_mpwr;
  1588. }
  1589. }
  1590. }
  1591. /*
  1592. * Restoring regulatory settings involves ingoring any
  1593. * possibly stale country IE information and user regulatory
  1594. * settings if so desired, this includes any beacon hints
  1595. * learned as we could have traveled outside to another country
  1596. * after disconnection. To restore regulatory settings we do
  1597. * exactly what we did at bootup:
  1598. *
  1599. * - send a core regulatory hint
  1600. * - send a user regulatory hint if applicable
  1601. *
  1602. * Device drivers that send a regulatory hint for a specific country
  1603. * keep their own regulatory domain on wiphy->regd so that does does
  1604. * not need to be remembered.
  1605. */
  1606. static void restore_regulatory_settings(bool reset_user)
  1607. {
  1608. char alpha2[2];
  1609. char world_alpha2[2];
  1610. struct reg_beacon *reg_beacon, *btmp;
  1611. struct regulatory_request *reg_request, *tmp;
  1612. LIST_HEAD(tmp_reg_req_list);
  1613. struct cfg80211_registered_device *rdev;
  1614. #ifdef CONFIG_CFG80211_REG_NOT_UPDATED
  1615. /*
  1616. * SAMSUNG FIX : Regulatory Configuration was update
  1617. * via WIPHY_FLAG_CUSTOM_REGULATORY of Wi-Fi Driver.
  1618. * Regulation should not updated even if device found other country Access Point Beacon once
  1619. * since device should find around other Access Points.
  1620. * 2014.1.8 Convergence Wi-Fi Core
  1621. */
  1622. printk("regulatory is not upadted via %s.\n", __func__);
  1623. return;
  1624. #endif
  1625. mutex_lock(&cfg80211_mutex);
  1626. mutex_lock(&reg_mutex);
  1627. reset_regdomains(true);
  1628. restore_alpha2(alpha2, reset_user);
  1629. /*
  1630. * If there's any pending requests we simply
  1631. * stash them to a temporary pending queue and
  1632. * add then after we've restored regulatory
  1633. * settings.
  1634. */
  1635. spin_lock(&reg_requests_lock);
  1636. if (!list_empty(&reg_requests_list)) {
  1637. list_for_each_entry_safe(reg_request, tmp,
  1638. &reg_requests_list, list) {
  1639. if (reg_request->initiator !=
  1640. NL80211_REGDOM_SET_BY_USER)
  1641. continue;
  1642. list_del(&reg_request->list);
  1643. list_add_tail(&reg_request->list, &tmp_reg_req_list);
  1644. }
  1645. }
  1646. spin_unlock(&reg_requests_lock);
  1647. /* Clear beacon hints */
  1648. spin_lock_bh(&reg_pending_beacons_lock);
  1649. if (!list_empty(&reg_pending_beacons)) {
  1650. list_for_each_entry_safe(reg_beacon, btmp,
  1651. &reg_pending_beacons, list) {
  1652. list_del(&reg_beacon->list);
  1653. kfree(reg_beacon);
  1654. }
  1655. }
  1656. spin_unlock_bh(&reg_pending_beacons_lock);
  1657. if (!list_empty(&reg_beacon_list)) {
  1658. list_for_each_entry_safe(reg_beacon, btmp,
  1659. &reg_beacon_list, list) {
  1660. list_del(&reg_beacon->list);
  1661. kfree(reg_beacon);
  1662. }
  1663. }
  1664. /* First restore to the basic regulatory settings */
  1665. cfg80211_regdomain = cfg80211_world_regdom;
  1666. world_alpha2[0] = cfg80211_regdomain->alpha2[0];
  1667. world_alpha2[1] = cfg80211_regdomain->alpha2[1];
  1668. list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
  1669. if (rdev->wiphy.flags & WIPHY_FLAG_CUSTOM_REGULATORY)
  1670. restore_custom_reg_settings(&rdev->wiphy);
  1671. }
  1672. mutex_unlock(&reg_mutex);
  1673. mutex_unlock(&cfg80211_mutex);
  1674. regulatory_hint_core(world_alpha2);
  1675. /*
  1676. * This restores the ieee80211_regdom module parameter
  1677. * preference or the last user requested regulatory
  1678. * settings, user regulatory settings takes precedence.
  1679. */
  1680. if (is_an_alpha2(alpha2))
  1681. regulatory_hint_user(user_alpha2);
  1682. if (list_empty(&tmp_reg_req_list))
  1683. return;
  1684. mutex_lock(&cfg80211_mutex);
  1685. mutex_lock(&reg_mutex);
  1686. spin_lock(&reg_requests_lock);
  1687. list_for_each_entry_safe(reg_request, tmp, &tmp_reg_req_list, list) {
  1688. REG_DBG_PRINT("Adding request for country %c%c back "
  1689. "into the queue\n",
  1690. reg_request->alpha2[0],
  1691. reg_request->alpha2[1]);
  1692. list_del(&reg_request->list);
  1693. list_add_tail(&reg_request->list, &reg_requests_list);
  1694. }
  1695. spin_unlock(&reg_requests_lock);
  1696. mutex_unlock(&reg_mutex);
  1697. mutex_unlock(&cfg80211_mutex);
  1698. REG_DBG_PRINT("Kicking the queue\n");
  1699. schedule_work(&reg_work);
  1700. }
  1701. void regulatory_hint_disconnect(void)
  1702. {
  1703. REG_DBG_PRINT("All devices are disconnected, going to "
  1704. "restore regulatory settings\n");
  1705. restore_regulatory_settings(false);
  1706. }
  1707. static bool freq_is_chan_12_13_14(u16 freq)
  1708. {
  1709. if (freq == ieee80211_channel_to_frequency(12, IEEE80211_BAND_2GHZ) ||
  1710. freq == ieee80211_channel_to_frequency(13, IEEE80211_BAND_2GHZ) ||
  1711. freq == ieee80211_channel_to_frequency(14, IEEE80211_BAND_2GHZ))
  1712. return true;
  1713. return false;
  1714. }
  1715. int regulatory_hint_found_beacon(struct wiphy *wiphy,
  1716. struct ieee80211_channel *beacon_chan,
  1717. gfp_t gfp)
  1718. {
  1719. struct reg_beacon *reg_beacon;
  1720. #ifdef CONFIG_CFG80211_REG_NOT_UPDATED
  1721. /*
  1722. * SAMSUNG FIX : Regulatory Configuration was update
  1723. * via WIPHY_FLAG_CUSTOM_REGULATORY of Wi-Fi Driver.
  1724. * Regulation should not updated even if device found other country Access Point Beacon once
  1725. * since device should find around other Access Points.
  1726. * 2014.1.8 Convergence Wi-Fi Core
  1727. */
  1728. return 0;
  1729. #endif
  1730. if (likely((beacon_chan->beacon_found ||
  1731. (beacon_chan->flags & IEEE80211_CHAN_RADAR) ||
  1732. (beacon_chan->band == IEEE80211_BAND_2GHZ &&
  1733. !freq_is_chan_12_13_14(beacon_chan->center_freq)))))
  1734. return 0;
  1735. reg_beacon = kzalloc(sizeof(struct reg_beacon), gfp);
  1736. if (!reg_beacon)
  1737. return -ENOMEM;
  1738. REG_DBG_PRINT("Found new beacon on "
  1739. "frequency: %d MHz (Ch %d) on %s\n",
  1740. beacon_chan->center_freq,
  1741. ieee80211_frequency_to_channel(beacon_chan->center_freq),
  1742. wiphy_name(wiphy));
  1743. memcpy(&reg_beacon->chan, beacon_chan,
  1744. sizeof(struct ieee80211_channel));
  1745. /*
  1746. * Since we can be called from BH or and non-BH context
  1747. * we must use spin_lock_bh()
  1748. */
  1749. spin_lock_bh(&reg_pending_beacons_lock);
  1750. list_add_tail(&reg_beacon->list, &reg_pending_beacons);
  1751. spin_unlock_bh(&reg_pending_beacons_lock);
  1752. schedule_work(&reg_work);
  1753. return 0;
  1754. }
  1755. static void print_rd_rules(const struct ieee80211_regdomain *rd)
  1756. {
  1757. unsigned int i;
  1758. const struct ieee80211_reg_rule *reg_rule = NULL;
  1759. const struct ieee80211_freq_range *freq_range = NULL;
  1760. const struct ieee80211_power_rule *power_rule = NULL;
  1761. pr_info(" (start_freq - end_freq @ bandwidth), (max_antenna_gain, max_eirp)\n");
  1762. for (i = 0; i < rd->n_reg_rules; i++) {
  1763. reg_rule = &rd->reg_rules[i];
  1764. freq_range = &reg_rule->freq_range;
  1765. power_rule = &reg_rule->power_rule;
  1766. /*
  1767. * There may not be documentation for max antenna gain
  1768. * in certain regions
  1769. */
  1770. if (power_rule->max_antenna_gain)
  1771. pr_info(" (%d KHz - %d KHz @ %d KHz), (%d mBi, %d mBm)\n",
  1772. freq_range->start_freq_khz,
  1773. freq_range->end_freq_khz,
  1774. freq_range->max_bandwidth_khz,
  1775. power_rule->max_antenna_gain,
  1776. power_rule->max_eirp);
  1777. else
  1778. pr_info(" (%d KHz - %d KHz @ %d KHz), (N/A, %d mBm)\n",
  1779. freq_range->start_freq_khz,
  1780. freq_range->end_freq_khz,
  1781. freq_range->max_bandwidth_khz,
  1782. power_rule->max_eirp);
  1783. }
  1784. }
  1785. bool reg_supported_dfs_region(u8 dfs_region)
  1786. {
  1787. switch (dfs_region) {
  1788. case NL80211_DFS_UNSET:
  1789. case NL80211_DFS_FCC:
  1790. case NL80211_DFS_ETSI:
  1791. case NL80211_DFS_JP:
  1792. return true;
  1793. default:
  1794. REG_DBG_PRINT("Ignoring uknown DFS master region: %d\n",
  1795. dfs_region);
  1796. return false;
  1797. }
  1798. }
  1799. static void print_dfs_region(u8 dfs_region)
  1800. {
  1801. if (!dfs_region)
  1802. return;
  1803. switch (dfs_region) {
  1804. case NL80211_DFS_FCC:
  1805. pr_info(" DFS Master region FCC");
  1806. break;
  1807. case NL80211_DFS_ETSI:
  1808. pr_info(" DFS Master region ETSI");
  1809. break;
  1810. case NL80211_DFS_JP:
  1811. pr_info(" DFS Master region JP");
  1812. break;
  1813. default:
  1814. pr_info(" DFS Master region Uknown");
  1815. break;
  1816. }
  1817. }
  1818. static void print_regdomain(const struct ieee80211_regdomain *rd)
  1819. {
  1820. if (is_intersected_alpha2(rd->alpha2)) {
  1821. if (last_request->initiator ==
  1822. NL80211_REGDOM_SET_BY_COUNTRY_IE) {
  1823. struct cfg80211_registered_device *rdev;
  1824. rdev = cfg80211_rdev_by_wiphy_idx(
  1825. last_request->wiphy_idx);
  1826. if (rdev) {
  1827. pr_info("Current regulatory domain updated by AP to: %c%c\n",
  1828. rdev->country_ie_alpha2[0],
  1829. rdev->country_ie_alpha2[1]);
  1830. } else
  1831. pr_info("Current regulatory domain intersected:\n");
  1832. } else
  1833. pr_info("Current regulatory domain intersected:\n");
  1834. } else if (is_world_regdom(rd->alpha2))
  1835. pr_info("World regulatory domain updated:\n");
  1836. else {
  1837. if (is_unknown_alpha2(rd->alpha2))
  1838. pr_info("Regulatory domain changed to driver built-in settings (unknown country)\n");
  1839. else
  1840. pr_info("Regulatory domain changed to country: %c%c\n",
  1841. rd->alpha2[0], rd->alpha2[1]);
  1842. }
  1843. print_dfs_region(rd->dfs_region);
  1844. print_rd_rules(rd);
  1845. }
  1846. static void print_regdomain_info(const struct ieee80211_regdomain *rd)
  1847. {
  1848. pr_info("Regulatory domain: %c%c\n", rd->alpha2[0], rd->alpha2[1]);
  1849. print_rd_rules(rd);
  1850. }
  1851. /* Takes ownership of rd only if it doesn't fail */
  1852. static int __set_regdom(const struct ieee80211_regdomain *rd)
  1853. {
  1854. const struct ieee80211_regdomain *intersected_rd = NULL;
  1855. struct cfg80211_registered_device *rdev = NULL;
  1856. struct wiphy *request_wiphy;
  1857. /* Some basic sanity checks first */
  1858. if (is_world_regdom(rd->alpha2)) {
  1859. if (WARN_ON(!reg_is_valid_request(rd->alpha2)))
  1860. return -EINVAL;
  1861. update_world_regdomain(rd);
  1862. return 0;
  1863. }
  1864. if (!is_alpha2_set(rd->alpha2) && !is_an_alpha2(rd->alpha2) &&
  1865. !is_unknown_alpha2(rd->alpha2))
  1866. return -EINVAL;
  1867. if (!last_request)
  1868. return -EINVAL;
  1869. /*
  1870. * Lets only bother proceeding on the same alpha2 if the current
  1871. * rd is non static (it means CRDA was present and was used last)
  1872. * and the pending request came in from a country IE
  1873. */
  1874. if (last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE) {
  1875. /*
  1876. * If someone else asked us to change the rd lets only bother
  1877. * checking if the alpha2 changes if CRDA was already called
  1878. */
  1879. if (!regdom_changes(rd->alpha2))
  1880. return -EALREADY;
  1881. }
  1882. /*
  1883. * Now lets set the regulatory domain, update all driver channels
  1884. * and finally inform them of what we have done, in case they want
  1885. * to review or adjust their own settings based on their own
  1886. * internal EEPROM data
  1887. */
  1888. if (WARN_ON(!reg_is_valid_request(rd->alpha2)))
  1889. return -EINVAL;
  1890. if (!is_valid_rd(rd)) {
  1891. pr_err("Invalid regulatory domain detected:\n");
  1892. print_regdomain_info(rd);
  1893. return -EINVAL;
  1894. }
  1895. request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
  1896. if (!request_wiphy &&
  1897. (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER ||
  1898. last_request->initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE)) {
  1899. schedule_delayed_work(&reg_timeout, 0);
  1900. return -ENODEV;
  1901. }
  1902. if (!last_request->intersect) {
  1903. int r;
  1904. if (last_request->initiator != NL80211_REGDOM_SET_BY_DRIVER) {
  1905. reset_regdomains(false);
  1906. cfg80211_regdomain = rd;
  1907. return 0;
  1908. }
  1909. /*
  1910. * For a driver hint, lets copy the regulatory domain the
  1911. * driver wanted to the wiphy to deal with conflicts
  1912. */
  1913. /*
  1914. * Userspace could have sent two replies with only
  1915. * one kernel request.
  1916. */
  1917. if (request_wiphy->regd)
  1918. return -EALREADY;
  1919. r = reg_copy_regd(&request_wiphy->regd, rd);
  1920. if (r)
  1921. return r;
  1922. reset_regdomains(false);
  1923. cfg80211_regdomain = rd;
  1924. return 0;
  1925. }
  1926. /* Intersection requires a bit more work */
  1927. if (last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE) {
  1928. intersected_rd = regdom_intersect(rd, cfg80211_regdomain);
  1929. if (!intersected_rd)
  1930. return -EINVAL;
  1931. /*
  1932. * We can trash what CRDA provided now.
  1933. * However if a driver requested this specific regulatory
  1934. * domain we keep it for its private use
  1935. */
  1936. if (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER) {
  1937. const struct ieee80211_regdomain *tmp;
  1938. tmp = request_wiphy->regd;
  1939. request_wiphy->regd = rd;
  1940. kfree(tmp);
  1941. } else {
  1942. kfree(rd);
  1943. }
  1944. rd = NULL;
  1945. reset_regdomains(false);
  1946. cfg80211_regdomain = intersected_rd;
  1947. return 0;
  1948. }
  1949. if (!intersected_rd)
  1950. return -EINVAL;
  1951. rdev = wiphy_to_dev(request_wiphy);
  1952. rdev->country_ie_alpha2[0] = rd->alpha2[0];
  1953. rdev->country_ie_alpha2[1] = rd->alpha2[1];
  1954. rdev->env = last_request->country_ie_env;
  1955. BUG_ON(intersected_rd == rd);
  1956. kfree(rd);
  1957. rd = NULL;
  1958. reset_regdomains(false);
  1959. cfg80211_regdomain = intersected_rd;
  1960. return 0;
  1961. }
  1962. /*
  1963. * Use this call to set the current regulatory domain. Conflicts with
  1964. * multiple drivers can be ironed out later. Caller must've already
  1965. * kmalloc'd the rd structure. Caller must hold cfg80211_mutex
  1966. */
  1967. int set_regdom(const struct ieee80211_regdomain *rd)
  1968. {
  1969. int r;
  1970. assert_cfg80211_lock();
  1971. mutex_lock(&reg_mutex);
  1972. /* Note that this doesn't update the wiphys, this is done below */
  1973. r = __set_regdom(rd);
  1974. if (r) {
  1975. if (r == -EALREADY)
  1976. reg_set_request_processed();
  1977. kfree(rd);
  1978. mutex_unlock(&reg_mutex);
  1979. return r;
  1980. }
  1981. /* This would make this whole thing pointless */
  1982. if (!last_request->intersect)
  1983. BUG_ON(rd != cfg80211_regdomain);
  1984. /* update all wiphys now with the new established regulatory domain */
  1985. update_all_wiphy_regulatory(last_request->initiator);
  1986. print_regdomain(cfg80211_regdomain);
  1987. nl80211_send_reg_change_event(last_request);
  1988. reg_set_request_processed();
  1989. mutex_unlock(&reg_mutex);
  1990. return r;
  1991. }
  1992. #ifdef CONFIG_HOTPLUG
  1993. int reg_device_uevent(struct device *dev, struct kobj_uevent_env *env)
  1994. {
  1995. if (last_request && !last_request->processed) {
  1996. if (add_uevent_var(env, "COUNTRY=%c%c",
  1997. last_request->alpha2[0],
  1998. last_request->alpha2[1]))
  1999. return -ENOMEM;
  2000. }
  2001. return 0;
  2002. }
  2003. #else
  2004. int reg_device_uevent(struct device *dev, struct kobj_uevent_env *env)
  2005. {
  2006. return -ENODEV;
  2007. }
  2008. #endif /* CONFIG_HOTPLUG */
  2009. /* Caller must hold cfg80211_mutex */
  2010. void reg_device_remove(struct wiphy *wiphy)
  2011. {
  2012. struct wiphy *request_wiphy = NULL;
  2013. assert_cfg80211_lock();
  2014. mutex_lock(&reg_mutex);
  2015. kfree(wiphy->regd);
  2016. if (last_request)
  2017. request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
  2018. if (!request_wiphy || request_wiphy != wiphy)
  2019. goto out;
  2020. last_request->wiphy_idx = WIPHY_IDX_STALE;
  2021. last_request->country_ie_env = ENVIRON_ANY;
  2022. out:
  2023. mutex_unlock(&reg_mutex);
  2024. }
  2025. static void reg_timeout_work(struct work_struct *work)
  2026. {
  2027. REG_DBG_PRINT("Timeout while waiting for CRDA to reply, "
  2028. "restoring regulatory settings\n");
  2029. restore_regulatory_settings(true);
  2030. }
  2031. int __init regulatory_init(void)
  2032. {
  2033. int err = 0;
  2034. reg_pdev = platform_device_register_simple("regulatory", 0, NULL, 0);
  2035. if (IS_ERR(reg_pdev))
  2036. return PTR_ERR(reg_pdev);
  2037. reg_pdev->dev.type = &reg_device_type;
  2038. spin_lock_init(&reg_requests_lock);
  2039. spin_lock_init(&reg_pending_beacons_lock);
  2040. reg_regdb_size_check();
  2041. cfg80211_regdomain = cfg80211_world_regdom;
  2042. user_alpha2[0] = '9';
  2043. user_alpha2[1] = '7';
  2044. /* We always try to get an update for the static regdomain */
  2045. err = regulatory_hint_core(cfg80211_regdomain->alpha2);
  2046. if (err) {
  2047. if (err == -ENOMEM)
  2048. return err;
  2049. /*
  2050. * N.B. kobject_uevent_env() can fail mainly for when we're out
  2051. * memory which is handled and propagated appropriately above
  2052. * but it can also fail during a netlink_broadcast() or during
  2053. * early boot for call_usermodehelper(). For now treat these
  2054. * errors as non-fatal.
  2055. */
  2056. pr_err("kobject_uevent_env() was unable to call CRDA during init\n");
  2057. #ifdef CONFIG_CFG80211_REG_DEBUG
  2058. /* We want to find out exactly why when debugging */
  2059. WARN_ON(err);
  2060. #endif
  2061. }
  2062. /*
  2063. * Finally, if the user set the module parameter treat it
  2064. * as a user hint.
  2065. */
  2066. if (!is_world_regdom(ieee80211_regdom))
  2067. regulatory_hint_user(ieee80211_regdom);
  2068. return 0;
  2069. }
  2070. void /* __init_or_exit */ regulatory_exit(void)
  2071. {
  2072. struct regulatory_request *reg_request, *tmp;
  2073. struct reg_beacon *reg_beacon, *btmp;
  2074. cancel_work_sync(&reg_work);
  2075. cancel_delayed_work_sync(&reg_timeout);
  2076. mutex_lock(&cfg80211_mutex);
  2077. mutex_lock(&reg_mutex);
  2078. reset_regdomains(true);
  2079. dev_set_uevent_suppress(&reg_pdev->dev, true);
  2080. platform_device_unregister(reg_pdev);
  2081. spin_lock_bh(&reg_pending_beacons_lock);
  2082. if (!list_empty(&reg_pending_beacons)) {
  2083. list_for_each_entry_safe(reg_beacon, btmp,
  2084. &reg_pending_beacons, list) {
  2085. list_del(&reg_beacon->list);
  2086. kfree(reg_beacon);
  2087. }
  2088. }
  2089. spin_unlock_bh(&reg_pending_beacons_lock);
  2090. if (!list_empty(&reg_beacon_list)) {
  2091. list_for_each_entry_safe(reg_beacon, btmp,
  2092. &reg_beacon_list, list) {
  2093. list_del(&reg_beacon->list);
  2094. kfree(reg_beacon);
  2095. }
  2096. }
  2097. spin_lock(&reg_requests_lock);
  2098. if (!list_empty(&reg_requests_list)) {
  2099. list_for_each_entry_safe(reg_request, tmp,
  2100. &reg_requests_list, list) {
  2101. list_del(&reg_request->list);
  2102. kfree(reg_request);
  2103. }
  2104. }
  2105. spin_unlock(&reg_requests_lock);
  2106. mutex_unlock(&reg_mutex);
  2107. mutex_unlock(&cfg80211_mutex);
  2108. }