cfg.c 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198
  1. /*
  2. * Implement cfg80211 ("iw") support.
  3. *
  4. * Copyright (C) 2009 M&N Solutions GmbH, 61191 Rosbach, Germany
  5. * Holger Schurig <hs4233@mail.mn-solutions.de>
  6. *
  7. */
  8. #include <linux/slab.h>
  9. #include <linux/if_arp.h>
  10. #include <linux/ieee80211.h>
  11. #include <net/cfg80211.h>
  12. #include <asm/unaligned.h>
  13. #include "decl.h"
  14. #include "cfg.h"
  15. #include "cmd.h"
  16. #define CHAN2G(_channel, _freq, _flags) { \
  17. .band = IEEE80211_BAND_2GHZ, \
  18. .center_freq = (_freq), \
  19. .hw_value = (_channel), \
  20. .flags = (_flags), \
  21. .max_antenna_gain = 0, \
  22. .max_power = 30, \
  23. }
  24. static struct ieee80211_channel lbs_2ghz_channels[] = {
  25. CHAN2G(1, 2412, 0),
  26. CHAN2G(2, 2417, 0),
  27. CHAN2G(3, 2422, 0),
  28. CHAN2G(4, 2427, 0),
  29. CHAN2G(5, 2432, 0),
  30. CHAN2G(6, 2437, 0),
  31. CHAN2G(7, 2442, 0),
  32. CHAN2G(8, 2447, 0),
  33. CHAN2G(9, 2452, 0),
  34. CHAN2G(10, 2457, 0),
  35. CHAN2G(11, 2462, 0),
  36. CHAN2G(12, 2467, 0),
  37. CHAN2G(13, 2472, 0),
  38. CHAN2G(14, 2484, 0),
  39. };
  40. #define RATETAB_ENT(_rate, _hw_value, _flags) { \
  41. .bitrate = (_rate), \
  42. .hw_value = (_hw_value), \
  43. .flags = (_flags), \
  44. }
  45. /* Table 6 in section 3.2.1.1 */
  46. static struct ieee80211_rate lbs_rates[] = {
  47. RATETAB_ENT(10, 0, 0),
  48. RATETAB_ENT(20, 1, 0),
  49. RATETAB_ENT(55, 2, 0),
  50. RATETAB_ENT(110, 3, 0),
  51. RATETAB_ENT(60, 9, 0),
  52. RATETAB_ENT(90, 6, 0),
  53. RATETAB_ENT(120, 7, 0),
  54. RATETAB_ENT(180, 8, 0),
  55. RATETAB_ENT(240, 9, 0),
  56. RATETAB_ENT(360, 10, 0),
  57. RATETAB_ENT(480, 11, 0),
  58. RATETAB_ENT(540, 12, 0),
  59. };
  60. static struct ieee80211_supported_band lbs_band_2ghz = {
  61. .channels = lbs_2ghz_channels,
  62. .n_channels = ARRAY_SIZE(lbs_2ghz_channels),
  63. .bitrates = lbs_rates,
  64. .n_bitrates = ARRAY_SIZE(lbs_rates),
  65. };
  66. static const u32 cipher_suites[] = {
  67. WLAN_CIPHER_SUITE_WEP40,
  68. WLAN_CIPHER_SUITE_WEP104,
  69. WLAN_CIPHER_SUITE_TKIP,
  70. WLAN_CIPHER_SUITE_CCMP,
  71. };
  72. /* Time to stay on the channel */
  73. #define LBS_DWELL_PASSIVE 100
  74. #define LBS_DWELL_ACTIVE 40
  75. /***************************************************************************
  76. * Misc utility functions
  77. *
  78. * TLVs are Marvell specific. They are very similar to IEs, they have the
  79. * same structure: type, length, data*. The only difference: for IEs, the
  80. * type and length are u8, but for TLVs they're __le16.
  81. */
  82. /*
  83. * Convert NL80211's auth_type to the one from Libertas, see chapter 5.9.1
  84. * in the firmware spec
  85. */
  86. static u8 lbs_auth_to_authtype(enum nl80211_auth_type auth_type)
  87. {
  88. int ret = -ENOTSUPP;
  89. switch (auth_type) {
  90. case NL80211_AUTHTYPE_OPEN_SYSTEM:
  91. case NL80211_AUTHTYPE_SHARED_KEY:
  92. ret = auth_type;
  93. break;
  94. case NL80211_AUTHTYPE_AUTOMATIC:
  95. ret = NL80211_AUTHTYPE_OPEN_SYSTEM;
  96. break;
  97. case NL80211_AUTHTYPE_NETWORK_EAP:
  98. ret = 0x80;
  99. break;
  100. default:
  101. /* silence compiler */
  102. break;
  103. }
  104. return ret;
  105. }
  106. /* Various firmware commands need the list of supported rates, but with
  107. the hight-bit set for basic rates */
  108. static int lbs_add_rates(u8 *rates)
  109. {
  110. size_t i;
  111. for (i = 0; i < ARRAY_SIZE(lbs_rates); i++) {
  112. u8 rate = lbs_rates[i].bitrate / 5;
  113. if (rate == 0x02 || rate == 0x04 ||
  114. rate == 0x0b || rate == 0x16)
  115. rate |= 0x80;
  116. rates[i] = rate;
  117. }
  118. return ARRAY_SIZE(lbs_rates);
  119. }
  120. /***************************************************************************
  121. * TLV utility functions
  122. *
  123. * TLVs are Marvell specific. They are very similar to IEs, they have the
  124. * same structure: type, length, data*. The only difference: for IEs, the
  125. * type and length are u8, but for TLVs they're __le16.
  126. */
  127. /*
  128. * Add ssid TLV
  129. */
  130. #define LBS_MAX_SSID_TLV_SIZE \
  131. (sizeof(struct mrvl_ie_header) \
  132. + IEEE80211_MAX_SSID_LEN)
  133. static int lbs_add_ssid_tlv(u8 *tlv, const u8 *ssid, int ssid_len)
  134. {
  135. struct mrvl_ie_ssid_param_set *ssid_tlv = (void *)tlv;
  136. /*
  137. * TLV-ID SSID 00 00
  138. * length 06 00
  139. * ssid 4d 4e 54 45 53 54
  140. */
  141. ssid_tlv->header.type = cpu_to_le16(TLV_TYPE_SSID);
  142. ssid_tlv->header.len = cpu_to_le16(ssid_len);
  143. memcpy(ssid_tlv->ssid, ssid, ssid_len);
  144. return sizeof(ssid_tlv->header) + ssid_len;
  145. }
  146. /*
  147. * Add channel list TLV (section 8.4.2)
  148. *
  149. * Actual channel data comes from priv->wdev->wiphy->channels.
  150. */
  151. #define LBS_MAX_CHANNEL_LIST_TLV_SIZE \
  152. (sizeof(struct mrvl_ie_header) \
  153. + (LBS_SCAN_BEFORE_NAP * sizeof(struct chanscanparamset)))
  154. static int lbs_add_channel_list_tlv(struct lbs_private *priv, u8 *tlv,
  155. int last_channel, int active_scan)
  156. {
  157. int chanscanparamsize = sizeof(struct chanscanparamset) *
  158. (last_channel - priv->scan_channel);
  159. struct mrvl_ie_header *header = (void *) tlv;
  160. /*
  161. * TLV-ID CHANLIST 01 01
  162. * length 0e 00
  163. * channel 00 01 00 00 00 64 00
  164. * radio type 00
  165. * channel 01
  166. * scan type 00
  167. * min scan time 00 00
  168. * max scan time 64 00
  169. * channel 2 00 02 00 00 00 64 00
  170. *
  171. */
  172. header->type = cpu_to_le16(TLV_TYPE_CHANLIST);
  173. header->len = cpu_to_le16(chanscanparamsize);
  174. tlv += sizeof(struct mrvl_ie_header);
  175. /* lbs_deb_scan("scan: channels %d to %d\n", priv->scan_channel,
  176. last_channel); */
  177. memset(tlv, 0, chanscanparamsize);
  178. while (priv->scan_channel < last_channel) {
  179. struct chanscanparamset *param = (void *) tlv;
  180. param->radiotype = CMD_SCAN_RADIO_TYPE_BG;
  181. param->channumber =
  182. priv->scan_req->channels[priv->scan_channel]->hw_value;
  183. if (active_scan) {
  184. param->maxscantime = cpu_to_le16(LBS_DWELL_ACTIVE);
  185. } else {
  186. param->chanscanmode.passivescan = 1;
  187. param->maxscantime = cpu_to_le16(LBS_DWELL_PASSIVE);
  188. }
  189. tlv += sizeof(struct chanscanparamset);
  190. priv->scan_channel++;
  191. }
  192. return sizeof(struct mrvl_ie_header) + chanscanparamsize;
  193. }
  194. /*
  195. * Add rates TLV
  196. *
  197. * The rates are in lbs_bg_rates[], but for the 802.11b
  198. * rates the high bit is set. We add this TLV only because
  199. * there's a firmware which otherwise doesn't report all
  200. * APs in range.
  201. */
  202. #define LBS_MAX_RATES_TLV_SIZE \
  203. (sizeof(struct mrvl_ie_header) \
  204. + (ARRAY_SIZE(lbs_rates)))
  205. /* Adds a TLV with all rates the hardware supports */
  206. static int lbs_add_supported_rates_tlv(u8 *tlv)
  207. {
  208. size_t i;
  209. struct mrvl_ie_rates_param_set *rate_tlv = (void *)tlv;
  210. /*
  211. * TLV-ID RATES 01 00
  212. * length 0e 00
  213. * rates 82 84 8b 96 0c 12 18 24 30 48 60 6c
  214. */
  215. rate_tlv->header.type = cpu_to_le16(TLV_TYPE_RATES);
  216. tlv += sizeof(rate_tlv->header);
  217. i = lbs_add_rates(tlv);
  218. tlv += i;
  219. rate_tlv->header.len = cpu_to_le16(i);
  220. return sizeof(rate_tlv->header) + i;
  221. }
  222. /*
  223. * Adds a TLV with all rates the hardware *and* BSS supports.
  224. */
  225. static int lbs_add_common_rates_tlv(u8 *tlv, struct cfg80211_bss *bss)
  226. {
  227. struct mrvl_ie_rates_param_set *rate_tlv = (void *)tlv;
  228. const u8 *rates_eid = ieee80211_bss_get_ie(bss, WLAN_EID_SUPP_RATES);
  229. int n;
  230. /*
  231. * 01 00 TLV_TYPE_RATES
  232. * 04 00 len
  233. * 82 84 8b 96 rates
  234. */
  235. rate_tlv->header.type = cpu_to_le16(TLV_TYPE_RATES);
  236. tlv += sizeof(rate_tlv->header);
  237. if (!rates_eid) {
  238. /* Fallback: add basic 802.11b rates */
  239. *tlv++ = 0x82;
  240. *tlv++ = 0x84;
  241. *tlv++ = 0x8b;
  242. *tlv++ = 0x96;
  243. n = 4;
  244. } else {
  245. int hw, ap;
  246. u8 ap_max = rates_eid[1];
  247. n = 0;
  248. for (hw = 0; hw < ARRAY_SIZE(lbs_rates); hw++) {
  249. u8 hw_rate = lbs_rates[hw].bitrate / 5;
  250. for (ap = 0; ap < ap_max; ap++) {
  251. if (hw_rate == (rates_eid[ap+2] & 0x7f)) {
  252. *tlv++ = rates_eid[ap+2];
  253. n++;
  254. }
  255. }
  256. }
  257. }
  258. rate_tlv->header.len = cpu_to_le16(n);
  259. return sizeof(rate_tlv->header) + n;
  260. }
  261. /*
  262. * Add auth type TLV.
  263. *
  264. * This is only needed for newer firmware (V9 and up).
  265. */
  266. #define LBS_MAX_AUTH_TYPE_TLV_SIZE \
  267. sizeof(struct mrvl_ie_auth_type)
  268. static int lbs_add_auth_type_tlv(u8 *tlv, enum nl80211_auth_type auth_type)
  269. {
  270. struct mrvl_ie_auth_type *auth = (void *) tlv;
  271. /*
  272. * 1f 01 TLV_TYPE_AUTH_TYPE
  273. * 01 00 len
  274. * 01 auth type
  275. */
  276. auth->header.type = cpu_to_le16(TLV_TYPE_AUTH_TYPE);
  277. auth->header.len = cpu_to_le16(sizeof(*auth)-sizeof(auth->header));
  278. auth->auth = cpu_to_le16(lbs_auth_to_authtype(auth_type));
  279. return sizeof(*auth);
  280. }
  281. /*
  282. * Add channel (phy ds) TLV
  283. */
  284. #define LBS_MAX_CHANNEL_TLV_SIZE \
  285. sizeof(struct mrvl_ie_header)
  286. static int lbs_add_channel_tlv(u8 *tlv, u8 channel)
  287. {
  288. struct mrvl_ie_ds_param_set *ds = (void *) tlv;
  289. /*
  290. * 03 00 TLV_TYPE_PHY_DS
  291. * 01 00 len
  292. * 06 channel
  293. */
  294. ds->header.type = cpu_to_le16(TLV_TYPE_PHY_DS);
  295. ds->header.len = cpu_to_le16(sizeof(*ds)-sizeof(ds->header));
  296. ds->channel = channel;
  297. return sizeof(*ds);
  298. }
  299. /*
  300. * Add (empty) CF param TLV of the form:
  301. */
  302. #define LBS_MAX_CF_PARAM_TLV_SIZE \
  303. sizeof(struct mrvl_ie_header)
  304. static int lbs_add_cf_param_tlv(u8 *tlv)
  305. {
  306. struct mrvl_ie_cf_param_set *cf = (void *)tlv;
  307. /*
  308. * 04 00 TLV_TYPE_CF
  309. * 06 00 len
  310. * 00 cfpcnt
  311. * 00 cfpperiod
  312. * 00 00 cfpmaxduration
  313. * 00 00 cfpdurationremaining
  314. */
  315. cf->header.type = cpu_to_le16(TLV_TYPE_CF);
  316. cf->header.len = cpu_to_le16(sizeof(*cf)-sizeof(cf->header));
  317. return sizeof(*cf);
  318. }
  319. /*
  320. * Add WPA TLV
  321. */
  322. #define LBS_MAX_WPA_TLV_SIZE \
  323. (sizeof(struct mrvl_ie_header) \
  324. + 128 /* TODO: I guessed the size */)
  325. static int lbs_add_wpa_tlv(u8 *tlv, const u8 *ie, u8 ie_len)
  326. {
  327. size_t tlv_len;
  328. /*
  329. * We need just convert an IE to an TLV. IEs use u8 for the header,
  330. * u8 type
  331. * u8 len
  332. * u8[] data
  333. * but TLVs use __le16 instead:
  334. * __le16 type
  335. * __le16 len
  336. * u8[] data
  337. */
  338. *tlv++ = *ie++;
  339. *tlv++ = 0;
  340. tlv_len = *tlv++ = *ie++;
  341. *tlv++ = 0;
  342. while (tlv_len--)
  343. *tlv++ = *ie++;
  344. /* the TLV is two bytes larger than the IE */
  345. return ie_len + 2;
  346. }
  347. /***************************************************************************
  348. * Set Channel
  349. */
  350. static int lbs_cfg_set_channel(struct wiphy *wiphy,
  351. struct net_device *netdev,
  352. struct ieee80211_channel *channel,
  353. enum nl80211_channel_type channel_type)
  354. {
  355. struct lbs_private *priv = wiphy_priv(wiphy);
  356. int ret = -ENOTSUPP;
  357. lbs_deb_enter_args(LBS_DEB_CFG80211, "freq %d, type %d",
  358. channel->center_freq, channel_type);
  359. if (channel_type != NL80211_CHAN_NO_HT)
  360. goto out;
  361. ret = lbs_set_channel(priv, channel->hw_value);
  362. out:
  363. lbs_deb_leave_args(LBS_DEB_CFG80211, "ret %d", ret);
  364. return ret;
  365. }
  366. /***************************************************************************
  367. * Scanning
  368. */
  369. /*
  370. * When scanning, the firmware doesn't send a nul packet with the power-safe
  371. * bit to the AP. So we cannot stay away from our current channel too long,
  372. * otherwise we loose data. So take a "nap" while scanning every other
  373. * while.
  374. */
  375. #define LBS_SCAN_BEFORE_NAP 4
  376. /*
  377. * When the firmware reports back a scan-result, it gives us an "u8 rssi",
  378. * which isn't really an RSSI, as it becomes larger when moving away from
  379. * the AP. Anyway, we need to convert that into mBm.
  380. */
  381. #define LBS_SCAN_RSSI_TO_MBM(rssi) \
  382. ((-(int)rssi + 3)*100)
  383. static int lbs_ret_scan(struct lbs_private *priv, unsigned long dummy,
  384. struct cmd_header *resp)
  385. {
  386. struct cmd_ds_802_11_scan_rsp *scanresp = (void *)resp;
  387. int bsssize;
  388. const u8 *pos;
  389. u16 nr_sets;
  390. const u8 *tsfdesc;
  391. int tsfsize;
  392. int i;
  393. int ret = -EILSEQ;
  394. lbs_deb_enter(LBS_DEB_CFG80211);
  395. bsssize = get_unaligned_le16(&scanresp->bssdescriptsize);
  396. nr_sets = le16_to_cpu(resp->size);
  397. /*
  398. * The general layout of the scan response is described in chapter
  399. * 5.7.1. Basically we have a common part, then any number of BSS
  400. * descriptor sections. Finally we have section with the same number
  401. * of TSFs.
  402. *
  403. * cmd_ds_802_11_scan_rsp
  404. * cmd_header
  405. * pos_size
  406. * nr_sets
  407. * bssdesc 1
  408. * bssid
  409. * rssi
  410. * timestamp
  411. * intvl
  412. * capa
  413. * IEs
  414. * bssdesc 2
  415. * bssdesc n
  416. * MrvlIEtypes_TsfFimestamp_t
  417. * TSF for BSS 1
  418. * TSF for BSS 2
  419. * TSF for BSS n
  420. */
  421. pos = scanresp->bssdesc_and_tlvbuffer;
  422. tsfdesc = pos + bsssize;
  423. tsfsize = 4 + 8 * scanresp->nr_sets;
  424. /* Validity check: we expect a Marvell-Local TLV */
  425. i = get_unaligned_le16(tsfdesc);
  426. tsfdesc += 2;
  427. if (i != TLV_TYPE_TSFTIMESTAMP)
  428. goto done;
  429. /* Validity check: the TLV holds TSF values with 8 bytes each, so
  430. * the size in the TLV must match the nr_sets value */
  431. i = get_unaligned_le16(tsfdesc);
  432. tsfdesc += 2;
  433. if (i / 8 != scanresp->nr_sets)
  434. goto done;
  435. for (i = 0; i < scanresp->nr_sets; i++) {
  436. const u8 *bssid;
  437. const u8 *ie;
  438. int left;
  439. int ielen;
  440. int rssi;
  441. u16 intvl;
  442. u16 capa;
  443. int chan_no = -1;
  444. const u8 *ssid = NULL;
  445. u8 ssid_len = 0;
  446. DECLARE_SSID_BUF(ssid_buf);
  447. int len = get_unaligned_le16(pos);
  448. pos += 2;
  449. /* BSSID */
  450. bssid = pos;
  451. pos += ETH_ALEN;
  452. /* RSSI */
  453. rssi = *pos++;
  454. /* Packet time stamp */
  455. pos += 8;
  456. /* Beacon interval */
  457. intvl = get_unaligned_le16(pos);
  458. pos += 2;
  459. /* Capabilities */
  460. capa = get_unaligned_le16(pos);
  461. pos += 2;
  462. /* To find out the channel, we must parse the IEs */
  463. ie = pos;
  464. /* 6+1+8+2+2: size of BSSID, RSSI, time stamp, beacon
  465. interval, capabilities */
  466. ielen = left = len - (6 + 1 + 8 + 2 + 2);
  467. while (left >= 2) {
  468. u8 id, elen;
  469. id = *pos++;
  470. elen = *pos++;
  471. left -= 2;
  472. if (elen > left || elen == 0)
  473. goto done;
  474. if (id == WLAN_EID_DS_PARAMS)
  475. chan_no = *pos;
  476. if (id == WLAN_EID_SSID) {
  477. ssid = pos;
  478. ssid_len = elen;
  479. }
  480. left -= elen;
  481. pos += elen;
  482. }
  483. /* No channel, no luck */
  484. if (chan_no != -1) {
  485. struct wiphy *wiphy = priv->wdev->wiphy;
  486. int freq = ieee80211_channel_to_frequency(chan_no);
  487. struct ieee80211_channel *channel =
  488. ieee80211_get_channel(wiphy, freq);
  489. lbs_deb_scan("scan: %pM, capa %04x, chan %2d, %s, "
  490. "%d dBm\n",
  491. bssid, capa, chan_no,
  492. print_ssid(ssid_buf, ssid, ssid_len),
  493. LBS_SCAN_RSSI_TO_MBM(rssi)/100);
  494. if (channel ||
  495. !(channel->flags & IEEE80211_CHAN_DISABLED))
  496. cfg80211_inform_bss(wiphy, channel,
  497. bssid, le64_to_cpu(*(__le64 *)tsfdesc),
  498. capa, intvl, ie, ielen,
  499. LBS_SCAN_RSSI_TO_MBM(rssi),
  500. GFP_KERNEL);
  501. }
  502. tsfdesc += 8;
  503. }
  504. ret = 0;
  505. done:
  506. lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
  507. return ret;
  508. }
  509. /*
  510. * Our scan command contains a TLV, consting of a SSID TLV, a channel list
  511. * TLV and a rates TLV. Determine the maximum size of them:
  512. */
  513. #define LBS_SCAN_MAX_CMD_SIZE \
  514. (sizeof(struct cmd_ds_802_11_scan) \
  515. + LBS_MAX_SSID_TLV_SIZE \
  516. + LBS_MAX_CHANNEL_LIST_TLV_SIZE \
  517. + LBS_MAX_RATES_TLV_SIZE)
  518. /*
  519. * Assumes priv->scan_req is initialized and valid
  520. * Assumes priv->scan_channel is initialized
  521. */
  522. static void lbs_scan_worker(struct work_struct *work)
  523. {
  524. struct lbs_private *priv =
  525. container_of(work, struct lbs_private, scan_work.work);
  526. struct cmd_ds_802_11_scan *scan_cmd;
  527. u8 *tlv; /* pointer into our current, growing TLV storage area */
  528. int last_channel;
  529. int running, carrier;
  530. lbs_deb_enter(LBS_DEB_SCAN);
  531. scan_cmd = kzalloc(LBS_SCAN_MAX_CMD_SIZE, GFP_KERNEL);
  532. if (scan_cmd == NULL)
  533. goto out_no_scan_cmd;
  534. /* prepare fixed part of scan command */
  535. scan_cmd->bsstype = CMD_BSS_TYPE_ANY;
  536. /* stop network while we're away from our main channel */
  537. running = !netif_queue_stopped(priv->dev);
  538. carrier = netif_carrier_ok(priv->dev);
  539. if (running)
  540. netif_stop_queue(priv->dev);
  541. if (carrier)
  542. netif_carrier_off(priv->dev);
  543. /* prepare fixed part of scan command */
  544. tlv = scan_cmd->tlvbuffer;
  545. /* add SSID TLV */
  546. if (priv->scan_req->n_ssids)
  547. tlv += lbs_add_ssid_tlv(tlv,
  548. priv->scan_req->ssids[0].ssid,
  549. priv->scan_req->ssids[0].ssid_len);
  550. /* add channel TLVs */
  551. last_channel = priv->scan_channel + LBS_SCAN_BEFORE_NAP;
  552. if (last_channel > priv->scan_req->n_channels)
  553. last_channel = priv->scan_req->n_channels;
  554. tlv += lbs_add_channel_list_tlv(priv, tlv, last_channel,
  555. priv->scan_req->n_ssids);
  556. /* add rates TLV */
  557. tlv += lbs_add_supported_rates_tlv(tlv);
  558. if (priv->scan_channel < priv->scan_req->n_channels) {
  559. cancel_delayed_work(&priv->scan_work);
  560. queue_delayed_work(priv->work_thread, &priv->scan_work,
  561. msecs_to_jiffies(300));
  562. }
  563. /* This is the final data we are about to send */
  564. scan_cmd->hdr.size = cpu_to_le16(tlv - (u8 *)scan_cmd);
  565. lbs_deb_hex(LBS_DEB_SCAN, "SCAN_CMD", (void *)scan_cmd,
  566. sizeof(*scan_cmd));
  567. lbs_deb_hex(LBS_DEB_SCAN, "SCAN_TLV", scan_cmd->tlvbuffer,
  568. tlv - scan_cmd->tlvbuffer);
  569. __lbs_cmd(priv, CMD_802_11_SCAN, &scan_cmd->hdr,
  570. le16_to_cpu(scan_cmd->hdr.size),
  571. lbs_ret_scan, 0);
  572. if (priv->scan_channel >= priv->scan_req->n_channels) {
  573. /* Mark scan done */
  574. cfg80211_scan_done(priv->scan_req, false);
  575. priv->scan_req = NULL;
  576. }
  577. /* Restart network */
  578. if (carrier)
  579. netif_carrier_on(priv->dev);
  580. if (running && !priv->tx_pending_len)
  581. netif_wake_queue(priv->dev);
  582. kfree(scan_cmd);
  583. out_no_scan_cmd:
  584. lbs_deb_leave(LBS_DEB_SCAN);
  585. }
  586. static int lbs_cfg_scan(struct wiphy *wiphy,
  587. struct net_device *dev,
  588. struct cfg80211_scan_request *request)
  589. {
  590. struct lbs_private *priv = wiphy_priv(wiphy);
  591. int ret = 0;
  592. lbs_deb_enter(LBS_DEB_CFG80211);
  593. if (priv->scan_req || delayed_work_pending(&priv->scan_work)) {
  594. /* old scan request not yet processed */
  595. ret = -EAGAIN;
  596. goto out;
  597. }
  598. lbs_deb_scan("scan: ssids %d, channels %d, ie_len %zd\n",
  599. request->n_ssids, request->n_channels, request->ie_len);
  600. priv->scan_channel = 0;
  601. queue_delayed_work(priv->work_thread, &priv->scan_work,
  602. msecs_to_jiffies(50));
  603. if (priv->surpriseremoved)
  604. ret = -EIO;
  605. priv->scan_req = request;
  606. out:
  607. lbs_deb_leave_args(LBS_DEB_CFG80211, "ret %d", ret);
  608. return ret;
  609. }
  610. /***************************************************************************
  611. * Events
  612. */
  613. void lbs_send_disconnect_notification(struct lbs_private *priv)
  614. {
  615. lbs_deb_enter(LBS_DEB_CFG80211);
  616. cfg80211_disconnected(priv->dev,
  617. 0,
  618. NULL, 0,
  619. GFP_KERNEL);
  620. lbs_deb_leave(LBS_DEB_CFG80211);
  621. }
  622. void lbs_send_mic_failureevent(struct lbs_private *priv, u32 event)
  623. {
  624. lbs_deb_enter(LBS_DEB_CFG80211);
  625. cfg80211_michael_mic_failure(priv->dev,
  626. priv->assoc_bss,
  627. event == MACREG_INT_CODE_MIC_ERR_MULTICAST ?
  628. NL80211_KEYTYPE_GROUP :
  629. NL80211_KEYTYPE_PAIRWISE,
  630. -1,
  631. NULL,
  632. GFP_KERNEL);
  633. lbs_deb_leave(LBS_DEB_CFG80211);
  634. }
  635. /***************************************************************************
  636. * Connect/disconnect
  637. */
  638. /*
  639. * This removes all WEP keys
  640. */
  641. static int lbs_remove_wep_keys(struct lbs_private *priv)
  642. {
  643. struct cmd_ds_802_11_set_wep cmd;
  644. int ret;
  645. lbs_deb_enter(LBS_DEB_CFG80211);
  646. memset(&cmd, 0, sizeof(cmd));
  647. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  648. cmd.keyindex = cpu_to_le16(priv->wep_tx_key);
  649. cmd.action = cpu_to_le16(CMD_ACT_REMOVE);
  650. ret = lbs_cmd_with_response(priv, CMD_802_11_SET_WEP, &cmd);
  651. lbs_deb_leave(LBS_DEB_CFG80211);
  652. return ret;
  653. }
  654. /*
  655. * Set WEP keys
  656. */
  657. static int lbs_set_wep_keys(struct lbs_private *priv)
  658. {
  659. struct cmd_ds_802_11_set_wep cmd;
  660. int i;
  661. int ret;
  662. lbs_deb_enter(LBS_DEB_CFG80211);
  663. /*
  664. * command 13 00
  665. * size 50 00
  666. * sequence xx xx
  667. * result 00 00
  668. * action 02 00 ACT_ADD
  669. * transmit key 00 00
  670. * type for key 1 01 WEP40
  671. * type for key 2 00
  672. * type for key 3 00
  673. * type for key 4 00
  674. * key 1 39 39 39 39 39 00 00 00
  675. * 00 00 00 00 00 00 00 00
  676. * key 2 00 00 00 00 00 00 00 00
  677. * 00 00 00 00 00 00 00 00
  678. * key 3 00 00 00 00 00 00 00 00
  679. * 00 00 00 00 00 00 00 00
  680. * key 4 00 00 00 00 00 00 00 00
  681. */
  682. if (priv->wep_key_len[0] || priv->wep_key_len[1] ||
  683. priv->wep_key_len[2] || priv->wep_key_len[3]) {
  684. /* Only set wep keys if we have at least one of them */
  685. memset(&cmd, 0, sizeof(cmd));
  686. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  687. cmd.keyindex = cpu_to_le16(priv->wep_tx_key);
  688. cmd.action = cpu_to_le16(CMD_ACT_ADD);
  689. for (i = 0; i < 4; i++) {
  690. switch (priv->wep_key_len[i]) {
  691. case WLAN_KEY_LEN_WEP40:
  692. cmd.keytype[i] = CMD_TYPE_WEP_40_BIT;
  693. break;
  694. case WLAN_KEY_LEN_WEP104:
  695. cmd.keytype[i] = CMD_TYPE_WEP_104_BIT;
  696. break;
  697. default:
  698. cmd.keytype[i] = 0;
  699. break;
  700. }
  701. memcpy(cmd.keymaterial[i], priv->wep_key[i],
  702. priv->wep_key_len[i]);
  703. }
  704. ret = lbs_cmd_with_response(priv, CMD_802_11_SET_WEP, &cmd);
  705. } else {
  706. /* Otherwise remove all wep keys */
  707. ret = lbs_remove_wep_keys(priv);
  708. }
  709. lbs_deb_leave(LBS_DEB_CFG80211);
  710. return ret;
  711. }
  712. /*
  713. * Enable/Disable RSN status
  714. */
  715. static int lbs_enable_rsn(struct lbs_private *priv, int enable)
  716. {
  717. struct cmd_ds_802_11_enable_rsn cmd;
  718. int ret;
  719. lbs_deb_enter_args(LBS_DEB_CFG80211, "%d", enable);
  720. /*
  721. * cmd 2f 00
  722. * size 0c 00
  723. * sequence xx xx
  724. * result 00 00
  725. * action 01 00 ACT_SET
  726. * enable 01 00
  727. */
  728. memset(&cmd, 0, sizeof(cmd));
  729. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  730. cmd.action = cpu_to_le16(CMD_ACT_SET);
  731. cmd.enable = cpu_to_le16(enable);
  732. ret = lbs_cmd_with_response(priv, CMD_802_11_ENABLE_RSN, &cmd);
  733. lbs_deb_leave(LBS_DEB_CFG80211);
  734. return ret;
  735. }
  736. /*
  737. * Set WPA/WPA key material
  738. */
  739. /* like "struct cmd_ds_802_11_key_material", but with cmd_header. Once we
  740. * get rid of WEXT, this should go into host.h */
  741. struct cmd_key_material {
  742. struct cmd_header hdr;
  743. __le16 action;
  744. struct MrvlIEtype_keyParamSet param;
  745. } __attribute__ ((packed));
  746. static int lbs_set_key_material(struct lbs_private *priv,
  747. int key_type,
  748. int key_info,
  749. u8 *key, u16 key_len)
  750. {
  751. struct cmd_key_material cmd;
  752. int ret;
  753. lbs_deb_enter(LBS_DEB_CFG80211);
  754. /*
  755. * Example for WPA (TKIP):
  756. *
  757. * cmd 5e 00
  758. * size 34 00
  759. * sequence xx xx
  760. * result 00 00
  761. * action 01 00
  762. * TLV type 00 01 key param
  763. * length 00 26
  764. * key type 01 00 TKIP
  765. * key info 06 00 UNICAST | ENABLED
  766. * key len 20 00
  767. * key 32 bytes
  768. */
  769. memset(&cmd, 0, sizeof(cmd));
  770. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  771. cmd.action = cpu_to_le16(CMD_ACT_SET);
  772. cmd.param.type = cpu_to_le16(TLV_TYPE_KEY_MATERIAL);
  773. cmd.param.length = cpu_to_le16(sizeof(cmd.param) - 4);
  774. cmd.param.keytypeid = cpu_to_le16(key_type);
  775. cmd.param.keyinfo = cpu_to_le16(key_info);
  776. cmd.param.keylen = cpu_to_le16(key_len);
  777. if (key && key_len)
  778. memcpy(cmd.param.key, key, key_len);
  779. ret = lbs_cmd_with_response(priv, CMD_802_11_KEY_MATERIAL, &cmd);
  780. lbs_deb_leave(LBS_DEB_CFG80211);
  781. return ret;
  782. }
  783. /*
  784. * Sets the auth type (open, shared, etc) in the firmware. That
  785. * we use CMD_802_11_AUTHENTICATE is misleading, this firmware
  786. * command doesn't send an authentication frame at all, it just
  787. * stores the auth_type.
  788. */
  789. static int lbs_set_authtype(struct lbs_private *priv,
  790. struct cfg80211_connect_params *sme)
  791. {
  792. struct cmd_ds_802_11_authenticate cmd;
  793. int ret;
  794. lbs_deb_enter_args(LBS_DEB_CFG80211, "%d", sme->auth_type);
  795. /*
  796. * cmd 11 00
  797. * size 19 00
  798. * sequence xx xx
  799. * result 00 00
  800. * BSS id 00 13 19 80 da 30
  801. * auth type 00
  802. * reserved 00 00 00 00 00 00 00 00 00 00
  803. */
  804. memset(&cmd, 0, sizeof(cmd));
  805. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  806. if (sme->bssid)
  807. memcpy(cmd.bssid, sme->bssid, ETH_ALEN);
  808. /* convert auth_type */
  809. ret = lbs_auth_to_authtype(sme->auth_type);
  810. if (ret < 0)
  811. goto done;
  812. cmd.authtype = ret;
  813. ret = lbs_cmd_with_response(priv, CMD_802_11_AUTHENTICATE, &cmd);
  814. done:
  815. lbs_deb_leave_args(LBS_DEB_CFG80211, "ret %d", ret);
  816. return ret;
  817. }
  818. /*
  819. * Create association request
  820. */
  821. #define LBS_ASSOC_MAX_CMD_SIZE \
  822. (sizeof(struct cmd_ds_802_11_associate) \
  823. - 512 /* cmd_ds_802_11_associate.iebuf */ \
  824. + LBS_MAX_SSID_TLV_SIZE \
  825. + LBS_MAX_CHANNEL_TLV_SIZE \
  826. + LBS_MAX_CF_PARAM_TLV_SIZE \
  827. + LBS_MAX_AUTH_TYPE_TLV_SIZE \
  828. + LBS_MAX_WPA_TLV_SIZE)
  829. static int lbs_associate(struct lbs_private *priv,
  830. struct cfg80211_bss *bss,
  831. struct cfg80211_connect_params *sme)
  832. {
  833. struct cmd_ds_802_11_associate_response *resp;
  834. struct cmd_ds_802_11_associate *cmd = kzalloc(LBS_ASSOC_MAX_CMD_SIZE,
  835. GFP_KERNEL);
  836. const u8 *ssid_eid;
  837. size_t len, resp_ie_len;
  838. int status;
  839. int ret;
  840. u8 *pos = &(cmd->iebuf[0]);
  841. lbs_deb_enter(LBS_DEB_CFG80211);
  842. if (!cmd) {
  843. ret = -ENOMEM;
  844. goto done;
  845. }
  846. /*
  847. * cmd 50 00
  848. * length 34 00
  849. * sequence xx xx
  850. * result 00 00
  851. * BSS id 00 13 19 80 da 30
  852. * capabilities 11 00
  853. * listen interval 0a 00
  854. * beacon interval 00 00
  855. * DTIM period 00
  856. * TLVs xx (up to 512 bytes)
  857. */
  858. cmd->hdr.command = cpu_to_le16(CMD_802_11_ASSOCIATE);
  859. /* Fill in static fields */
  860. memcpy(cmd->bssid, bss->bssid, ETH_ALEN);
  861. cmd->listeninterval = cpu_to_le16(MRVDRV_DEFAULT_LISTEN_INTERVAL);
  862. cmd->capability = cpu_to_le16(bss->capability);
  863. /* add SSID TLV */
  864. ssid_eid = ieee80211_bss_get_ie(bss, WLAN_EID_SSID);
  865. if (ssid_eid)
  866. pos += lbs_add_ssid_tlv(pos, ssid_eid + 2, ssid_eid[1]);
  867. else
  868. lbs_deb_assoc("no SSID\n");
  869. /* add DS param TLV */
  870. if (bss->channel)
  871. pos += lbs_add_channel_tlv(pos, bss->channel->hw_value);
  872. else
  873. lbs_deb_assoc("no channel\n");
  874. /* add (empty) CF param TLV */
  875. pos += lbs_add_cf_param_tlv(pos);
  876. /* add rates TLV */
  877. pos += lbs_add_common_rates_tlv(pos, bss);
  878. /* add auth type TLV */
  879. if (priv->fwrelease >= 0x09000000)
  880. pos += lbs_add_auth_type_tlv(pos, sme->auth_type);
  881. /* add WPA/WPA2 TLV */
  882. if (sme->ie && sme->ie_len)
  883. pos += lbs_add_wpa_tlv(pos, sme->ie, sme->ie_len);
  884. len = (sizeof(*cmd) - sizeof(cmd->iebuf)) +
  885. (u16)(pos - (u8 *) &cmd->iebuf);
  886. cmd->hdr.size = cpu_to_le16(len);
  887. /* store for later use */
  888. memcpy(priv->assoc_bss, bss->bssid, ETH_ALEN);
  889. ret = lbs_cmd_with_response(priv, CMD_802_11_ASSOCIATE, cmd);
  890. if (ret)
  891. goto done;
  892. /* generate connect message to cfg80211 */
  893. resp = (void *) cmd; /* recast for easier field access */
  894. status = le16_to_cpu(resp->statuscode);
  895. /* Convert statis code of old firmware */
  896. if (priv->fwrelease < 0x09000000)
  897. switch (status) {
  898. case 0:
  899. break;
  900. case 1:
  901. lbs_deb_assoc("invalid association parameters\n");
  902. status = WLAN_STATUS_CAPS_UNSUPPORTED;
  903. break;
  904. case 2:
  905. lbs_deb_assoc("timer expired while waiting for AP\n");
  906. status = WLAN_STATUS_AUTH_TIMEOUT;
  907. break;
  908. case 3:
  909. lbs_deb_assoc("association refused by AP\n");
  910. status = WLAN_STATUS_ASSOC_DENIED_UNSPEC;
  911. break;
  912. case 4:
  913. lbs_deb_assoc("authentication refused by AP\n");
  914. status = WLAN_STATUS_UNKNOWN_AUTH_TRANSACTION;
  915. break;
  916. default:
  917. lbs_deb_assoc("association failure %d\n", status);
  918. status = WLAN_STATUS_UNSPECIFIED_FAILURE;
  919. }
  920. lbs_deb_assoc("status %d, capability 0x%04x\n", status,
  921. le16_to_cpu(resp->capability));
  922. resp_ie_len = le16_to_cpu(resp->hdr.size)
  923. - sizeof(resp->hdr)
  924. - 6;
  925. cfg80211_connect_result(priv->dev,
  926. priv->assoc_bss,
  927. sme->ie, sme->ie_len,
  928. resp->iebuf, resp_ie_len,
  929. status,
  930. GFP_KERNEL);
  931. if (status == 0) {
  932. /* TODO: get rid of priv->connect_status */
  933. priv->connect_status = LBS_CONNECTED;
  934. netif_carrier_on(priv->dev);
  935. if (!priv->tx_pending_len)
  936. netif_tx_wake_all_queues(priv->dev);
  937. }
  938. done:
  939. lbs_deb_leave_args(LBS_DEB_CFG80211, "ret %d", ret);
  940. return ret;
  941. }
  942. static int lbs_cfg_connect(struct wiphy *wiphy, struct net_device *dev,
  943. struct cfg80211_connect_params *sme)
  944. {
  945. struct lbs_private *priv = wiphy_priv(wiphy);
  946. struct cfg80211_bss *bss = NULL;
  947. int ret = 0;
  948. u8 preamble = RADIO_PREAMBLE_SHORT;
  949. lbs_deb_enter(LBS_DEB_CFG80211);
  950. if (sme->bssid) {
  951. bss = cfg80211_get_bss(wiphy, sme->channel, sme->bssid,
  952. sme->ssid, sme->ssid_len,
  953. WLAN_CAPABILITY_ESS, WLAN_CAPABILITY_ESS);
  954. } else {
  955. /*
  956. * Here we have an impedance mismatch. The firmware command
  957. * CMD_802_11_ASSOCIATE always needs a BSSID, it cannot
  958. * connect otherwise. However, for the connect-API of
  959. * cfg80211 the bssid is purely optional. We don't get one,
  960. * except the user specifies one on the "iw" command line.
  961. *
  962. * If we don't got one, we could initiate a scan and look
  963. * for the best matching cfg80211_bss entry.
  964. *
  965. * Or, better yet, net/wireless/sme.c get's rewritten into
  966. * something more generally useful.
  967. */
  968. lbs_pr_err("TODO: no BSS specified\n");
  969. ret = -ENOTSUPP;
  970. goto done;
  971. }
  972. if (!bss) {
  973. lbs_pr_err("assicate: bss %pM not in scan results\n",
  974. sme->bssid);
  975. ret = -ENOENT;
  976. goto done;
  977. }
  978. lbs_deb_assoc("trying %pM", sme->bssid);
  979. lbs_deb_assoc("cipher 0x%x, key index %d, key len %d\n",
  980. sme->crypto.cipher_group,
  981. sme->key_idx, sme->key_len);
  982. /* As this is a new connection, clear locally stored WEP keys */
  983. priv->wep_tx_key = 0;
  984. memset(priv->wep_key, 0, sizeof(priv->wep_key));
  985. memset(priv->wep_key_len, 0, sizeof(priv->wep_key_len));
  986. /* set/remove WEP keys */
  987. switch (sme->crypto.cipher_group) {
  988. case WLAN_CIPHER_SUITE_WEP40:
  989. case WLAN_CIPHER_SUITE_WEP104:
  990. /* Store provided WEP keys in priv-> */
  991. priv->wep_tx_key = sme->key_idx;
  992. priv->wep_key_len[sme->key_idx] = sme->key_len;
  993. memcpy(priv->wep_key[sme->key_idx], sme->key, sme->key_len);
  994. /* Set WEP keys and WEP mode */
  995. lbs_set_wep_keys(priv);
  996. priv->mac_control |= CMD_ACT_MAC_WEP_ENABLE;
  997. lbs_set_mac_control(priv);
  998. /* No RSN mode for WEP */
  999. lbs_enable_rsn(priv, 0);
  1000. break;
  1001. case 0: /* there's no WLAN_CIPHER_SUITE_NONE definition */
  1002. /*
  1003. * If we don't have no WEP, no WPA and no WPA2,
  1004. * we remove all keys like in the WPA/WPA2 setup,
  1005. * we just don't set RSN.
  1006. *
  1007. * Therefore: fall-throught
  1008. */
  1009. case WLAN_CIPHER_SUITE_TKIP:
  1010. case WLAN_CIPHER_SUITE_CCMP:
  1011. /* Remove WEP keys and WEP mode */
  1012. lbs_remove_wep_keys(priv);
  1013. priv->mac_control &= ~CMD_ACT_MAC_WEP_ENABLE;
  1014. lbs_set_mac_control(priv);
  1015. /* clear the WPA/WPA2 keys */
  1016. lbs_set_key_material(priv,
  1017. KEY_TYPE_ID_WEP, /* doesn't matter */
  1018. KEY_INFO_WPA_UNICAST,
  1019. NULL, 0);
  1020. lbs_set_key_material(priv,
  1021. KEY_TYPE_ID_WEP, /* doesn't matter */
  1022. KEY_INFO_WPA_MCAST,
  1023. NULL, 0);
  1024. /* RSN mode for WPA/WPA2 */
  1025. lbs_enable_rsn(priv, sme->crypto.cipher_group != 0);
  1026. break;
  1027. default:
  1028. lbs_pr_err("unsupported cipher group 0x%x\n",
  1029. sme->crypto.cipher_group);
  1030. ret = -ENOTSUPP;
  1031. goto done;
  1032. }
  1033. lbs_set_authtype(priv, sme);
  1034. lbs_set_radio(priv, preamble, 1);
  1035. /* Do the actual association */
  1036. lbs_associate(priv, bss, sme);
  1037. done:
  1038. if (bss)
  1039. cfg80211_put_bss(bss);
  1040. lbs_deb_leave_args(LBS_DEB_CFG80211, "ret %d", ret);
  1041. return ret;
  1042. }
  1043. static int lbs_cfg_disconnect(struct wiphy *wiphy, struct net_device *dev,
  1044. u16 reason_code)
  1045. {
  1046. struct lbs_private *priv = wiphy_priv(wiphy);
  1047. struct cmd_ds_802_11_deauthenticate cmd;
  1048. lbs_deb_enter_args(LBS_DEB_CFG80211, "reason_code %d", reason_code);
  1049. /* store for lbs_cfg_ret_disconnect() */
  1050. priv->disassoc_reason = reason_code;
  1051. memset(&cmd, 0, sizeof(cmd));
  1052. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  1053. /* Mildly ugly to use a locally store my own BSSID ... */
  1054. memcpy(cmd.macaddr, &priv->assoc_bss, ETH_ALEN);
  1055. cmd.reasoncode = cpu_to_le16(reason_code);
  1056. if (lbs_cmd_with_response(priv, CMD_802_11_DEAUTHENTICATE, &cmd))
  1057. return -EFAULT;
  1058. cfg80211_disconnected(priv->dev,
  1059. priv->disassoc_reason,
  1060. NULL, 0,
  1061. GFP_KERNEL);
  1062. priv->connect_status = LBS_DISCONNECTED;
  1063. return 0;
  1064. }
  1065. static int lbs_cfg_set_default_key(struct wiphy *wiphy,
  1066. struct net_device *netdev,
  1067. u8 key_index)
  1068. {
  1069. struct lbs_private *priv = wiphy_priv(wiphy);
  1070. lbs_deb_enter(LBS_DEB_CFG80211);
  1071. if (key_index != priv->wep_tx_key) {
  1072. lbs_deb_assoc("set_default_key: to %d\n", key_index);
  1073. priv->wep_tx_key = key_index;
  1074. lbs_set_wep_keys(priv);
  1075. }
  1076. return 0;
  1077. }
  1078. static int lbs_cfg_add_key(struct wiphy *wiphy, struct net_device *netdev,
  1079. u8 idx, const u8 *mac_addr,
  1080. struct key_params *params)
  1081. {
  1082. struct lbs_private *priv = wiphy_priv(wiphy);
  1083. u16 key_info;
  1084. u16 key_type;
  1085. int ret = 0;
  1086. lbs_deb_enter(LBS_DEB_CFG80211);
  1087. lbs_deb_assoc("add_key: cipher 0x%x, mac_addr %pM\n",
  1088. params->cipher, mac_addr);
  1089. lbs_deb_assoc("add_key: key index %d, key len %d\n",
  1090. idx, params->key_len);
  1091. if (params->key_len)
  1092. lbs_deb_hex(LBS_DEB_CFG80211, "KEY",
  1093. params->key, params->key_len);
  1094. lbs_deb_assoc("add_key: seq len %d\n", params->seq_len);
  1095. if (params->seq_len)
  1096. lbs_deb_hex(LBS_DEB_CFG80211, "SEQ",
  1097. params->seq, params->seq_len);
  1098. switch (params->cipher) {
  1099. case WLAN_CIPHER_SUITE_WEP40:
  1100. case WLAN_CIPHER_SUITE_WEP104:
  1101. /* actually compare if something has changed ... */
  1102. if ((priv->wep_key_len[idx] != params->key_len) ||
  1103. memcmp(priv->wep_key[idx],
  1104. params->key, params->key_len) != 0) {
  1105. priv->wep_key_len[idx] = params->key_len;
  1106. memcpy(priv->wep_key[idx],
  1107. params->key, params->key_len);
  1108. lbs_set_wep_keys(priv);
  1109. }
  1110. break;
  1111. case WLAN_CIPHER_SUITE_TKIP:
  1112. case WLAN_CIPHER_SUITE_CCMP:
  1113. key_info = KEY_INFO_WPA_ENABLED | ((idx == 0)
  1114. ? KEY_INFO_WPA_UNICAST
  1115. : KEY_INFO_WPA_MCAST);
  1116. key_type = (params->cipher == WLAN_CIPHER_SUITE_TKIP)
  1117. ? KEY_TYPE_ID_TKIP
  1118. : KEY_TYPE_ID_AES;
  1119. lbs_set_key_material(priv,
  1120. key_type,
  1121. key_info,
  1122. params->key, params->key_len);
  1123. break;
  1124. default:
  1125. lbs_pr_err("unhandled cipher 0x%x\n", params->cipher);
  1126. ret = -ENOTSUPP;
  1127. break;
  1128. }
  1129. return ret;
  1130. }
  1131. static int lbs_cfg_del_key(struct wiphy *wiphy, struct net_device *netdev,
  1132. u8 key_index, const u8 *mac_addr)
  1133. {
  1134. lbs_deb_enter(LBS_DEB_CFG80211);
  1135. lbs_deb_assoc("del_key: key_idx %d, mac_addr %pM\n",
  1136. key_index, mac_addr);
  1137. #ifdef TODO
  1138. struct lbs_private *priv = wiphy_priv(wiphy);
  1139. /*
  1140. * I think can keep this a NO-OP, because:
  1141. * - we clear all keys whenever we do lbs_cfg_connect() anyway
  1142. * - neither "iw" nor "wpa_supplicant" won't call this during
  1143. * an ongoing connection
  1144. * - TODO: but I have to check if this is still true when
  1145. * I set the AP to periodic re-keying
  1146. * - we've not kzallec() something when we've added a key at
  1147. * lbs_cfg_connect() or lbs_cfg_add_key().
  1148. *
  1149. * This causes lbs_cfg_del_key() only called at disconnect time,
  1150. * where we'd just waste time deleting a key that is not going
  1151. * to be used anyway.
  1152. */
  1153. if (key_index < 3 && priv->wep_key_len[key_index]) {
  1154. priv->wep_key_len[key_index] = 0;
  1155. lbs_set_wep_keys(priv);
  1156. }
  1157. #endif
  1158. return 0;
  1159. }
  1160. /***************************************************************************
  1161. * Monitor mode
  1162. */
  1163. /* like "struct cmd_ds_802_11_monitor_mode", but with cmd_header. Once we
  1164. * get rid of WEXT, this should go into host.h */
  1165. struct cmd_monitor_mode {
  1166. struct cmd_header hdr;
  1167. __le16 action;
  1168. __le16 mode;
  1169. } __attribute__ ((packed));
  1170. static int lbs_enable_monitor_mode(struct lbs_private *priv, int mode)
  1171. {
  1172. struct cmd_monitor_mode cmd;
  1173. int ret;
  1174. lbs_deb_enter(LBS_DEB_CFG80211);
  1175. /*
  1176. * cmd 98 00
  1177. * size 0c 00
  1178. * sequence xx xx
  1179. * result 00 00
  1180. * action 01 00 ACT_SET
  1181. * enable 01 00
  1182. */
  1183. memset(&cmd, 0, sizeof(cmd));
  1184. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  1185. cmd.action = cpu_to_le16(CMD_ACT_SET);
  1186. cmd.mode = cpu_to_le16(mode);
  1187. ret = lbs_cmd_with_response(priv, CMD_802_11_MONITOR_MODE, &cmd);
  1188. if (ret == 0)
  1189. priv->dev->type = ARPHRD_IEEE80211_RADIOTAP;
  1190. else
  1191. priv->dev->type = ARPHRD_ETHER;
  1192. lbs_deb_leave(LBS_DEB_CFG80211);
  1193. return ret;
  1194. }
  1195. /***************************************************************************
  1196. * Get station
  1197. */
  1198. /*
  1199. * Returns the signal or 0 in case of an error.
  1200. */
  1201. /* like "struct cmd_ds_802_11_rssi", but with cmd_header. Once we get rid
  1202. * of WEXT, this should go into host.h */
  1203. struct cmd_rssi {
  1204. struct cmd_header hdr;
  1205. __le16 n_or_snr;
  1206. __le16 nf;
  1207. __le16 avg_snr;
  1208. __le16 avg_nf;
  1209. } __attribute__ ((packed));
  1210. static int lbs_get_signal(struct lbs_private *priv, s8 *signal, s8 *noise)
  1211. {
  1212. struct cmd_rssi cmd;
  1213. int ret;
  1214. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  1215. cmd.n_or_snr = cpu_to_le16(DEFAULT_BCN_AVG_FACTOR);
  1216. ret = lbs_cmd_with_response(priv, CMD_802_11_RSSI, &cmd);
  1217. if (ret == 0) {
  1218. *signal = CAL_RSSI(le16_to_cpu(cmd.n_or_snr),
  1219. le16_to_cpu(cmd.nf));
  1220. *noise = CAL_NF(le16_to_cpu(cmd.nf));
  1221. }
  1222. return ret;
  1223. }
  1224. static int lbs_cfg_get_station(struct wiphy *wiphy, struct net_device *dev,
  1225. u8 *mac, struct station_info *sinfo)
  1226. {
  1227. struct lbs_private *priv = wiphy_priv(wiphy);
  1228. s8 signal, noise;
  1229. int ret;
  1230. size_t i;
  1231. lbs_deb_enter(LBS_DEB_CFG80211);
  1232. sinfo->filled |= STATION_INFO_TX_BYTES |
  1233. STATION_INFO_TX_PACKETS |
  1234. STATION_INFO_RX_BYTES |
  1235. STATION_INFO_RX_PACKETS;
  1236. sinfo->tx_bytes = priv->dev->stats.tx_bytes;
  1237. sinfo->tx_packets = priv->dev->stats.tx_packets;
  1238. sinfo->rx_bytes = priv->dev->stats.rx_bytes;
  1239. sinfo->rx_packets = priv->dev->stats.rx_packets;
  1240. /* Get current RSSI */
  1241. ret = lbs_get_signal(priv, &signal, &noise);
  1242. if (ret == 0) {
  1243. sinfo->signal = signal;
  1244. sinfo->filled |= STATION_INFO_SIGNAL;
  1245. }
  1246. /* Convert priv->cur_rate from hw_value to NL80211 value */
  1247. for (i = 0; i < ARRAY_SIZE(lbs_rates); i++) {
  1248. if (priv->cur_rate == lbs_rates[i].hw_value) {
  1249. sinfo->txrate.legacy = lbs_rates[i].bitrate;
  1250. sinfo->filled |= STATION_INFO_TX_BITRATE;
  1251. break;
  1252. }
  1253. }
  1254. return 0;
  1255. }
  1256. /***************************************************************************
  1257. * "Site survey", here just current channel and noise level
  1258. */
  1259. static int lbs_get_survey(struct wiphy *wiphy, struct net_device *dev,
  1260. int idx, struct survey_info *survey)
  1261. {
  1262. struct lbs_private *priv = wiphy_priv(wiphy);
  1263. s8 signal, noise;
  1264. int ret;
  1265. if (idx != 0)
  1266. ret = -ENOENT;
  1267. lbs_deb_enter(LBS_DEB_CFG80211);
  1268. survey->channel = ieee80211_get_channel(wiphy,
  1269. ieee80211_channel_to_frequency(priv->channel));
  1270. ret = lbs_get_signal(priv, &signal, &noise);
  1271. if (ret == 0) {
  1272. survey->filled = SURVEY_INFO_NOISE_DBM;
  1273. survey->noise = noise;
  1274. }
  1275. lbs_deb_leave_args(LBS_DEB_CFG80211, "ret %d", ret);
  1276. return ret;
  1277. }
  1278. /***************************************************************************
  1279. * Change interface
  1280. */
  1281. static int lbs_change_intf(struct wiphy *wiphy, struct net_device *dev,
  1282. enum nl80211_iftype type, u32 *flags,
  1283. struct vif_params *params)
  1284. {
  1285. struct lbs_private *priv = wiphy_priv(wiphy);
  1286. int ret = 0;
  1287. lbs_deb_enter(LBS_DEB_CFG80211);
  1288. switch (type) {
  1289. case NL80211_IFTYPE_MONITOR:
  1290. ret = lbs_enable_monitor_mode(priv, 1);
  1291. break;
  1292. case NL80211_IFTYPE_STATION:
  1293. if (priv->wdev->iftype == NL80211_IFTYPE_MONITOR)
  1294. ret = lbs_enable_monitor_mode(priv, 0);
  1295. if (!ret)
  1296. ret = lbs_set_snmp_mib(priv, SNMP_MIB_OID_BSS_TYPE, 1);
  1297. break;
  1298. case NL80211_IFTYPE_ADHOC:
  1299. if (priv->wdev->iftype == NL80211_IFTYPE_MONITOR)
  1300. ret = lbs_enable_monitor_mode(priv, 0);
  1301. if (!ret)
  1302. ret = lbs_set_snmp_mib(priv, SNMP_MIB_OID_BSS_TYPE, 2);
  1303. break;
  1304. default:
  1305. ret = -ENOTSUPP;
  1306. }
  1307. if (!ret)
  1308. priv->wdev->iftype = type;
  1309. lbs_deb_leave_args(LBS_DEB_CFG80211, "ret %d", ret);
  1310. return ret;
  1311. }
  1312. /***************************************************************************
  1313. * IBSS (Ad-Hoc)
  1314. */
  1315. /* The firmware needs the following bits masked out of the beacon-derived
  1316. * capability field when associating/joining to a BSS:
  1317. * 9 (QoS), 11 (APSD), 12 (unused), 14 (unused), 15 (unused)
  1318. */
  1319. #define CAPINFO_MASK (~(0xda00))
  1320. static void lbs_join_post(struct lbs_private *priv,
  1321. struct cfg80211_ibss_params *params,
  1322. u8 *bssid, u16 capability)
  1323. {
  1324. u8 fake_ie[2 + IEEE80211_MAX_SSID_LEN + /* ssid */
  1325. 2 + 4 + /* basic rates */
  1326. 2 + 1 + /* DS parameter */
  1327. 2 + 2 + /* atim */
  1328. 2 + 8]; /* extended rates */
  1329. u8 *fake = fake_ie;
  1330. lbs_deb_enter(LBS_DEB_CFG80211);
  1331. /*
  1332. * For cfg80211_inform_bss, we'll need a fake IE, as we can't get
  1333. * the real IE from the firmware. So we fabricate a fake IE based on
  1334. * what the firmware actually sends (sniffed with wireshark).
  1335. */
  1336. /* Fake SSID IE */
  1337. *fake++ = WLAN_EID_SSID;
  1338. *fake++ = params->ssid_len;
  1339. memcpy(fake, params->ssid, params->ssid_len);
  1340. fake += params->ssid_len;
  1341. /* Fake supported basic rates IE */
  1342. *fake++ = WLAN_EID_SUPP_RATES;
  1343. *fake++ = 4;
  1344. *fake++ = 0x82;
  1345. *fake++ = 0x84;
  1346. *fake++ = 0x8b;
  1347. *fake++ = 0x96;
  1348. /* Fake DS channel IE */
  1349. *fake++ = WLAN_EID_DS_PARAMS;
  1350. *fake++ = 1;
  1351. *fake++ = params->channel->hw_value;
  1352. /* Fake IBSS params IE */
  1353. *fake++ = WLAN_EID_IBSS_PARAMS;
  1354. *fake++ = 2;
  1355. *fake++ = 0; /* ATIM=0 */
  1356. *fake++ = 0;
  1357. /* Fake extended rates IE, TODO: don't add this for 802.11b only,
  1358. * but I don't know how this could be checked */
  1359. *fake++ = WLAN_EID_EXT_SUPP_RATES;
  1360. *fake++ = 8;
  1361. *fake++ = 0x0c;
  1362. *fake++ = 0x12;
  1363. *fake++ = 0x18;
  1364. *fake++ = 0x24;
  1365. *fake++ = 0x30;
  1366. *fake++ = 0x48;
  1367. *fake++ = 0x60;
  1368. *fake++ = 0x6c;
  1369. lbs_deb_hex(LBS_DEB_CFG80211, "IE", fake_ie, fake - fake_ie);
  1370. cfg80211_inform_bss(priv->wdev->wiphy,
  1371. params->channel,
  1372. bssid,
  1373. 0,
  1374. capability,
  1375. params->beacon_interval,
  1376. fake_ie, fake - fake_ie,
  1377. 0, GFP_KERNEL);
  1378. memcpy(priv->wdev->ssid, params->ssid, params->ssid_len);
  1379. priv->wdev->ssid_len = params->ssid_len;
  1380. cfg80211_ibss_joined(priv->dev, bssid, GFP_KERNEL);
  1381. /* TODO: consider doing this at MACREG_INT_CODE_LINK_SENSED time */
  1382. priv->connect_status = LBS_CONNECTED;
  1383. netif_carrier_on(priv->dev);
  1384. if (!priv->tx_pending_len)
  1385. netif_wake_queue(priv->dev);
  1386. lbs_deb_leave(LBS_DEB_CFG80211);
  1387. }
  1388. static int lbs_ibss_join_existing(struct lbs_private *priv,
  1389. struct cfg80211_ibss_params *params,
  1390. struct cfg80211_bss *bss)
  1391. {
  1392. const u8 *rates_eid = ieee80211_bss_get_ie(bss, WLAN_EID_SUPP_RATES);
  1393. struct cmd_ds_802_11_ad_hoc_join cmd;
  1394. u8 preamble = RADIO_PREAMBLE_SHORT;
  1395. int ret = 0;
  1396. lbs_deb_enter(LBS_DEB_CFG80211);
  1397. /* TODO: set preamble based on scan result */
  1398. ret = lbs_set_radio(priv, preamble, 1);
  1399. if (ret)
  1400. goto out;
  1401. /*
  1402. * Example CMD_802_11_AD_HOC_JOIN command:
  1403. *
  1404. * command 2c 00 CMD_802_11_AD_HOC_JOIN
  1405. * size 65 00
  1406. * sequence xx xx
  1407. * result 00 00
  1408. * bssid 02 27 27 97 2f 96
  1409. * ssid 49 42 53 53 00 00 00 00
  1410. * 00 00 00 00 00 00 00 00
  1411. * 00 00 00 00 00 00 00 00
  1412. * 00 00 00 00 00 00 00 00
  1413. * type 02 CMD_BSS_TYPE_IBSS
  1414. * beacon period 64 00
  1415. * dtim period 00
  1416. * timestamp 00 00 00 00 00 00 00 00
  1417. * localtime 00 00 00 00 00 00 00 00
  1418. * IE DS 03
  1419. * IE DS len 01
  1420. * IE DS channel 01
  1421. * reserveed 00 00 00 00
  1422. * IE IBSS 06
  1423. * IE IBSS len 02
  1424. * IE IBSS atim 00 00
  1425. * reserved 00 00 00 00
  1426. * capability 02 00
  1427. * rates 82 84 8b 96 0c 12 18 24 30 48 60 6c 00
  1428. * fail timeout ff 00
  1429. * probe delay 00 00
  1430. */
  1431. memset(&cmd, 0, sizeof(cmd));
  1432. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  1433. memcpy(cmd.bss.bssid, bss->bssid, ETH_ALEN);
  1434. memcpy(cmd.bss.ssid, params->ssid, params->ssid_len);
  1435. cmd.bss.type = CMD_BSS_TYPE_IBSS;
  1436. cmd.bss.beaconperiod = cpu_to_le16(params->beacon_interval);
  1437. cmd.bss.ds.header.id = WLAN_EID_DS_PARAMS;
  1438. cmd.bss.ds.header.len = 1;
  1439. cmd.bss.ds.channel = params->channel->hw_value;
  1440. cmd.bss.ibss.header.id = WLAN_EID_IBSS_PARAMS;
  1441. cmd.bss.ibss.header.len = 2;
  1442. cmd.bss.ibss.atimwindow = 0;
  1443. cmd.bss.capability = cpu_to_le16(bss->capability & CAPINFO_MASK);
  1444. /* set rates to the intersection of our rates and the rates in the
  1445. bss */
  1446. if (!rates_eid) {
  1447. lbs_add_rates(cmd.bss.rates);
  1448. } else {
  1449. int hw, i;
  1450. u8 rates_max = rates_eid[1];
  1451. u8 *rates = cmd.bss.rates;
  1452. for (hw = 0; hw < ARRAY_SIZE(lbs_rates); hw++) {
  1453. u8 hw_rate = lbs_rates[hw].bitrate / 5;
  1454. for (i = 0; i < rates_max; i++) {
  1455. if (hw_rate == (rates_eid[i+2] & 0x7f)) {
  1456. u8 rate = rates_eid[i+2];
  1457. if (rate == 0x02 || rate == 0x04 ||
  1458. rate == 0x0b || rate == 0x16)
  1459. rate |= 0x80;
  1460. *rates++ = rate;
  1461. }
  1462. }
  1463. }
  1464. }
  1465. /* Only v8 and below support setting this */
  1466. if (MRVL_FW_MAJOR_REV(priv->fwrelease) <= 8) {
  1467. cmd.failtimeout = cpu_to_le16(MRVDRV_ASSOCIATION_TIME_OUT);
  1468. cmd.probedelay = cpu_to_le16(CMD_SCAN_PROBE_DELAY_TIME);
  1469. }
  1470. ret = lbs_cmd_with_response(priv, CMD_802_11_AD_HOC_JOIN, &cmd);
  1471. if (ret)
  1472. goto out;
  1473. /*
  1474. * This is a sample response to CMD_802_11_AD_HOC_JOIN:
  1475. *
  1476. * response 2c 80
  1477. * size 09 00
  1478. * sequence xx xx
  1479. * result 00 00
  1480. * reserved 00
  1481. */
  1482. lbs_join_post(priv, params, bss->bssid, bss->capability);
  1483. out:
  1484. lbs_deb_leave_args(LBS_DEB_CFG80211, "ret %d", ret);
  1485. return ret;
  1486. }
  1487. static int lbs_ibss_start_new(struct lbs_private *priv,
  1488. struct cfg80211_ibss_params *params)
  1489. {
  1490. struct cmd_ds_802_11_ad_hoc_start cmd;
  1491. struct cmd_ds_802_11_ad_hoc_result *resp =
  1492. (struct cmd_ds_802_11_ad_hoc_result *) &cmd;
  1493. u8 preamble = RADIO_PREAMBLE_SHORT;
  1494. int ret = 0;
  1495. u16 capability;
  1496. lbs_deb_enter(LBS_DEB_CFG80211);
  1497. ret = lbs_set_radio(priv, preamble, 1);
  1498. if (ret)
  1499. goto out;
  1500. /*
  1501. * Example CMD_802_11_AD_HOC_START command:
  1502. *
  1503. * command 2b 00 CMD_802_11_AD_HOC_START
  1504. * size b1 00
  1505. * sequence xx xx
  1506. * result 00 00
  1507. * ssid 54 45 53 54 00 00 00 00
  1508. * 00 00 00 00 00 00 00 00
  1509. * 00 00 00 00 00 00 00 00
  1510. * 00 00 00 00 00 00 00 00
  1511. * bss type 02
  1512. * beacon period 64 00
  1513. * dtim period 00
  1514. * IE IBSS 06
  1515. * IE IBSS len 02
  1516. * IE IBSS atim 00 00
  1517. * reserved 00 00 00 00
  1518. * IE DS 03
  1519. * IE DS len 01
  1520. * IE DS channel 01
  1521. * reserved 00 00 00 00
  1522. * probe delay 00 00
  1523. * capability 02 00
  1524. * rates 82 84 8b 96 (basic rates with have bit 7 set)
  1525. * 0c 12 18 24 30 48 60 6c
  1526. * padding 100 bytes
  1527. */
  1528. memset(&cmd, 0, sizeof(cmd));
  1529. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  1530. memcpy(cmd.ssid, params->ssid, params->ssid_len);
  1531. cmd.bsstype = CMD_BSS_TYPE_IBSS;
  1532. cmd.beaconperiod = cpu_to_le16(params->beacon_interval);
  1533. cmd.ibss.header.id = WLAN_EID_IBSS_PARAMS;
  1534. cmd.ibss.header.len = 2;
  1535. cmd.ibss.atimwindow = 0;
  1536. cmd.ds.header.id = WLAN_EID_DS_PARAMS;
  1537. cmd.ds.header.len = 1;
  1538. cmd.ds.channel = params->channel->hw_value;
  1539. /* Only v8 and below support setting probe delay */
  1540. if (MRVL_FW_MAJOR_REV(priv->fwrelease) <= 8)
  1541. cmd.probedelay = cpu_to_le16(CMD_SCAN_PROBE_DELAY_TIME);
  1542. /* TODO: mix in WLAN_CAPABILITY_PRIVACY */
  1543. capability = WLAN_CAPABILITY_IBSS;
  1544. cmd.capability = cpu_to_le16(capability);
  1545. lbs_add_rates(cmd.rates);
  1546. ret = lbs_cmd_with_response(priv, CMD_802_11_AD_HOC_START, &cmd);
  1547. if (ret)
  1548. goto out;
  1549. /*
  1550. * This is a sample response to CMD_802_11_AD_HOC_JOIN:
  1551. *
  1552. * response 2b 80
  1553. * size 14 00
  1554. * sequence xx xx
  1555. * result 00 00
  1556. * reserved 00
  1557. * bssid 02 2b 7b 0f 86 0e
  1558. */
  1559. lbs_join_post(priv, params, resp->bssid, capability);
  1560. out:
  1561. lbs_deb_leave_args(LBS_DEB_CFG80211, "ret %d", ret);
  1562. return ret;
  1563. }
  1564. static int lbs_join_ibss(struct wiphy *wiphy, struct net_device *dev,
  1565. struct cfg80211_ibss_params *params)
  1566. {
  1567. struct lbs_private *priv = wiphy_priv(wiphy);
  1568. int ret = 0;
  1569. struct cfg80211_bss *bss;
  1570. DECLARE_SSID_BUF(ssid_buf);
  1571. lbs_deb_enter(LBS_DEB_CFG80211);
  1572. if (!params->channel) {
  1573. ret = -ENOTSUPP;
  1574. goto out;
  1575. }
  1576. ret = lbs_set_channel(priv, params->channel->hw_value);
  1577. if (ret)
  1578. goto out;
  1579. /* Search if someone is beaconing. This assumes that the
  1580. * bss list is populated already */
  1581. bss = cfg80211_get_bss(wiphy, params->channel, params->bssid,
  1582. params->ssid, params->ssid_len,
  1583. WLAN_CAPABILITY_IBSS, WLAN_CAPABILITY_IBSS);
  1584. if (bss) {
  1585. ret = lbs_ibss_join_existing(priv, params, bss);
  1586. cfg80211_put_bss(bss);
  1587. } else
  1588. ret = lbs_ibss_start_new(priv, params);
  1589. out:
  1590. lbs_deb_leave_args(LBS_DEB_CFG80211, "ret %d", ret);
  1591. return ret;
  1592. }
  1593. static int lbs_leave_ibss(struct wiphy *wiphy, struct net_device *dev)
  1594. {
  1595. struct lbs_private *priv = wiphy_priv(wiphy);
  1596. struct cmd_ds_802_11_ad_hoc_stop cmd;
  1597. int ret = 0;
  1598. lbs_deb_enter(LBS_DEB_CFG80211);
  1599. memset(&cmd, 0, sizeof(cmd));
  1600. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  1601. ret = lbs_cmd_with_response(priv, CMD_802_11_AD_HOC_STOP, &cmd);
  1602. /* TODO: consider doing this at MACREG_INT_CODE_ADHOC_BCN_LOST time */
  1603. lbs_mac_event_disconnected(priv);
  1604. lbs_deb_leave_args(LBS_DEB_CFG80211, "ret %d", ret);
  1605. return ret;
  1606. }
  1607. /***************************************************************************
  1608. * Initialization
  1609. */
  1610. static struct cfg80211_ops lbs_cfg80211_ops = {
  1611. .set_channel = lbs_cfg_set_channel,
  1612. .scan = lbs_cfg_scan,
  1613. .connect = lbs_cfg_connect,
  1614. .disconnect = lbs_cfg_disconnect,
  1615. .add_key = lbs_cfg_add_key,
  1616. .del_key = lbs_cfg_del_key,
  1617. .set_default_key = lbs_cfg_set_default_key,
  1618. .get_station = lbs_cfg_get_station,
  1619. .dump_survey = lbs_get_survey,
  1620. .change_virtual_intf = lbs_change_intf,
  1621. .join_ibss = lbs_join_ibss,
  1622. .leave_ibss = lbs_leave_ibss,
  1623. };
  1624. /*
  1625. * At this time lbs_private *priv doesn't even exist, so we just allocate
  1626. * memory and don't initialize the wiphy further. This is postponed until we
  1627. * can talk to the firmware and happens at registration time in
  1628. * lbs_cfg_wiphy_register().
  1629. */
  1630. struct wireless_dev *lbs_cfg_alloc(struct device *dev)
  1631. {
  1632. int ret = 0;
  1633. struct wireless_dev *wdev;
  1634. lbs_deb_enter(LBS_DEB_CFG80211);
  1635. wdev = kzalloc(sizeof(struct wireless_dev), GFP_KERNEL);
  1636. if (!wdev) {
  1637. dev_err(dev, "cannot allocate wireless device\n");
  1638. return ERR_PTR(-ENOMEM);
  1639. }
  1640. wdev->wiphy = wiphy_new(&lbs_cfg80211_ops, sizeof(struct lbs_private));
  1641. if (!wdev->wiphy) {
  1642. dev_err(dev, "cannot allocate wiphy\n");
  1643. ret = -ENOMEM;
  1644. goto err_wiphy_new;
  1645. }
  1646. lbs_deb_leave(LBS_DEB_CFG80211);
  1647. return wdev;
  1648. err_wiphy_new:
  1649. kfree(wdev);
  1650. lbs_deb_leave_args(LBS_DEB_CFG80211, "ret %d", ret);
  1651. return ERR_PTR(ret);
  1652. }
  1653. static void lbs_cfg_set_regulatory_hint(struct lbs_private *priv)
  1654. {
  1655. struct region_code_mapping {
  1656. const char *cn;
  1657. int code;
  1658. };
  1659. /* Section 5.17.2 */
  1660. static struct region_code_mapping regmap[] = {
  1661. {"US ", 0x10}, /* US FCC */
  1662. {"CA ", 0x20}, /* Canada */
  1663. {"EU ", 0x30}, /* ETSI */
  1664. {"ES ", 0x31}, /* Spain */
  1665. {"FR ", 0x32}, /* France */
  1666. {"JP ", 0x40}, /* Japan */
  1667. };
  1668. size_t i;
  1669. lbs_deb_enter(LBS_DEB_CFG80211);
  1670. for (i = 0; i < ARRAY_SIZE(regmap); i++)
  1671. if (regmap[i].code == priv->regioncode) {
  1672. regulatory_hint(priv->wdev->wiphy, regmap[i].cn);
  1673. break;
  1674. }
  1675. lbs_deb_leave(LBS_DEB_CFG80211);
  1676. }
  1677. /*
  1678. * This function get's called after lbs_setup_firmware() determined the
  1679. * firmware capabities. So we can setup the wiphy according to our
  1680. * hardware/firmware.
  1681. */
  1682. int lbs_cfg_register(struct lbs_private *priv)
  1683. {
  1684. struct wireless_dev *wdev = priv->wdev;
  1685. int ret;
  1686. lbs_deb_enter(LBS_DEB_CFG80211);
  1687. wdev->wiphy->max_scan_ssids = 1;
  1688. wdev->wiphy->signal_type = CFG80211_SIGNAL_TYPE_MBM;
  1689. wdev->wiphy->interface_modes =
  1690. BIT(NL80211_IFTYPE_STATION) |
  1691. BIT(NL80211_IFTYPE_ADHOC);
  1692. if (lbs_rtap_supported(priv))
  1693. wdev->wiphy->interface_modes |= BIT(NL80211_IFTYPE_MONITOR);
  1694. wdev->wiphy->bands[IEEE80211_BAND_2GHZ] = &lbs_band_2ghz;
  1695. /*
  1696. * We could check priv->fwcapinfo && FW_CAPINFO_WPA, but I have
  1697. * never seen a firmware without WPA
  1698. */
  1699. wdev->wiphy->cipher_suites = cipher_suites;
  1700. wdev->wiphy->n_cipher_suites = ARRAY_SIZE(cipher_suites);
  1701. wdev->wiphy->reg_notifier = lbs_reg_notifier;
  1702. ret = wiphy_register(wdev->wiphy);
  1703. if (ret < 0)
  1704. lbs_pr_err("cannot register wiphy device\n");
  1705. priv->wiphy_registered = true;
  1706. ret = register_netdev(priv->dev);
  1707. if (ret)
  1708. lbs_pr_err("cannot register network device\n");
  1709. INIT_DELAYED_WORK(&priv->scan_work, lbs_scan_worker);
  1710. lbs_cfg_set_regulatory_hint(priv);
  1711. lbs_deb_leave_args(LBS_DEB_CFG80211, "ret %d", ret);
  1712. return ret;
  1713. }
  1714. /**
  1715. * @brief This function sets DOMAIN INFO to FW
  1716. * @param priv pointer to struct lbs_private
  1717. * @return 0; -1
  1718. */
  1719. static int lbs_11d_set_domain_info(struct lbs_private *priv)
  1720. {
  1721. int ret;
  1722. ret = lbs_prepare_and_send_command(priv, CMD_802_11D_DOMAIN_INFO,
  1723. CMD_ACT_SET,
  1724. CMD_OPTION_WAITFORRSP, 0, NULL);
  1725. if (ret)
  1726. lbs_deb_11d("fail to dnld domain info\n");
  1727. return ret;
  1728. }
  1729. static void lbs_send_domain_info_cmd_fw(struct wiphy *wiphy,
  1730. struct regulatory_request *request)
  1731. {
  1732. u8 no_of_triplet = 0;
  1733. u8 no_of_parsed_chan = 0;
  1734. u8 first_channel = 0, next_chan = 0, max_pwr = 0;
  1735. u8 i, flag = 0;
  1736. enum ieee80211_band band;
  1737. struct ieee80211_supported_band *sband;
  1738. struct ieee80211_channel *ch;
  1739. struct lbs_private *priv = wiphy_priv(wiphy);
  1740. struct lbs_802_11d_domain_reg *domain_info = &priv->domain_reg;
  1741. int ret = 0;
  1742. lbs_deb_enter(LBS_DEB_CFG80211);
  1743. /* Set country code */
  1744. domain_info->country_code[0] = request->alpha2[0];
  1745. domain_info->country_code[1] = request->alpha2[1];
  1746. domain_info->country_code[2] = ' ';
  1747. for (band = 0; band < IEEE80211_NUM_BANDS ; band++) {
  1748. if (!wiphy->bands[band])
  1749. continue;
  1750. sband = wiphy->bands[band];
  1751. for (i = 0; i < sband->n_channels ; i++) {
  1752. ch = &sband->channels[i];
  1753. if (ch->flags & IEEE80211_CHAN_DISABLED)
  1754. continue;
  1755. if (!flag) {
  1756. flag = 1;
  1757. next_chan = first_channel = (u32) ch->hw_value;
  1758. max_pwr = ch->max_power;
  1759. no_of_parsed_chan = 1;
  1760. continue;
  1761. }
  1762. if (ch->hw_value == next_chan + 1 &&
  1763. ch->max_power == max_pwr) {
  1764. next_chan++;
  1765. no_of_parsed_chan++;
  1766. } else {
  1767. domain_info->triplet[no_of_triplet]
  1768. .chans.first_channel = first_channel;
  1769. domain_info->triplet[no_of_triplet]
  1770. .chans.num_channels = no_of_parsed_chan;
  1771. domain_info->triplet[no_of_triplet]
  1772. .chans.max_power = max_pwr;
  1773. no_of_triplet++;
  1774. flag = 0;
  1775. }
  1776. }
  1777. if (flag) {
  1778. domain_info->triplet[no_of_triplet]
  1779. .chans.first_channel = first_channel;
  1780. domain_info->triplet[no_of_triplet]
  1781. .chans.num_channels = no_of_parsed_chan;
  1782. domain_info->triplet[no_of_triplet]
  1783. .chans.max_power = max_pwr;
  1784. no_of_triplet++;
  1785. }
  1786. }
  1787. domain_info->no_triplet = no_of_triplet;
  1788. /* Set domain info */
  1789. ret = lbs_11d_set_domain_info(priv);
  1790. if (ret)
  1791. lbs_pr_err("11D: error setting domain info in FW\n");
  1792. lbs_deb_leave(LBS_DEB_CFG80211);
  1793. }
  1794. int lbs_reg_notifier(struct wiphy *wiphy,
  1795. struct regulatory_request *request)
  1796. {
  1797. lbs_deb_enter_args(LBS_DEB_CFG80211, "cfg80211 regulatory domain "
  1798. "callback for domain %c%c\n", request->alpha2[0],
  1799. request->alpha2[1]);
  1800. lbs_send_domain_info_cmd_fw(wiphy, request);
  1801. lbs_deb_leave(LBS_DEB_CFG80211);
  1802. return 0;
  1803. }
  1804. void lbs_scan_deinit(struct lbs_private *priv)
  1805. {
  1806. lbs_deb_enter(LBS_DEB_CFG80211);
  1807. cancel_delayed_work_sync(&priv->scan_work);
  1808. }
  1809. void lbs_cfg_free(struct lbs_private *priv)
  1810. {
  1811. struct wireless_dev *wdev = priv->wdev;
  1812. lbs_deb_enter(LBS_DEB_CFG80211);
  1813. if (!wdev)
  1814. return;
  1815. if (priv->wiphy_registered)
  1816. wiphy_unregister(wdev->wiphy);
  1817. if (wdev->wiphy)
  1818. wiphy_free(wdev->wiphy);
  1819. kfree(wdev);
  1820. }