cfg.c 49 KB

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