PageRenderTime 69ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 1ms

/drivers/net/wireless/libertas/cfg.c

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