main.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  1. /*
  2. * mac80211 glue code for mac80211 Prism54 drivers
  3. *
  4. * Copyright (c) 2006, Michael Wu <flamingice@sourmilk.net>
  5. * Copyright (c) 2007-2009, Christian Lamparter <chunkeey@web.de>
  6. * Copyright 2008, Johannes Berg <johannes@sipsolutions.net>
  7. *
  8. * Based on:
  9. * - the islsm (softmac prism54) driver, which is:
  10. * Copyright 2004-2006 Jean-Baptiste Note <jbnote@gmail.com>, et al.
  11. * - stlc45xx driver
  12. * Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies).
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License version 2 as
  16. * published by the Free Software Foundation.
  17. */
  18. #include <linux/init.h>
  19. #include <linux/firmware.h>
  20. #include <linux/etherdevice.h>
  21. #include <net/mac80211.h>
  22. #include "p54.h"
  23. #include "lmac.h"
  24. static int modparam_nohwcrypt;
  25. module_param_named(nohwcrypt, modparam_nohwcrypt, bool, S_IRUGO);
  26. MODULE_PARM_DESC(nohwcrypt, "Disable hardware encryption.");
  27. MODULE_AUTHOR("Michael Wu <flamingice@sourmilk.net>");
  28. MODULE_DESCRIPTION("Softmac Prism54 common code");
  29. MODULE_LICENSE("GPL");
  30. MODULE_ALIAS("prism54common");
  31. static void p54_sta_notify(struct ieee80211_hw *dev, struct ieee80211_vif *vif,
  32. enum sta_notify_cmd notify_cmd,
  33. struct ieee80211_sta *sta)
  34. {
  35. struct p54_common *priv = dev->priv;
  36. switch (notify_cmd) {
  37. case STA_NOTIFY_ADD:
  38. case STA_NOTIFY_REMOVE:
  39. /*
  40. * Notify the firmware that we don't want or we don't
  41. * need to buffer frames for this station anymore.
  42. */
  43. p54_sta_unlock(priv, sta->addr);
  44. break;
  45. case STA_NOTIFY_AWAKE:
  46. /* update the firmware's filter table */
  47. p54_sta_unlock(priv, sta->addr);
  48. break;
  49. default:
  50. break;
  51. }
  52. }
  53. static int p54_set_tim(struct ieee80211_hw *dev, struct ieee80211_sta *sta,
  54. bool set)
  55. {
  56. struct p54_common *priv = dev->priv;
  57. return p54_update_beacon_tim(priv, sta->aid, set);
  58. }
  59. static int p54_beacon_format_ie_tim(struct sk_buff *skb)
  60. {
  61. /*
  62. * the good excuse for this mess is ... the firmware.
  63. * The dummy TIM MUST be at the end of the beacon frame,
  64. * because it'll be overwritten!
  65. */
  66. struct ieee80211_mgmt *mgmt = (void *)skb->data;
  67. u8 *pos, *end;
  68. if (skb->len <= sizeof(mgmt))
  69. return -EINVAL;
  70. pos = (u8 *)mgmt->u.beacon.variable;
  71. end = skb->data + skb->len;
  72. while (pos < end) {
  73. if (pos + 2 + pos[1] > end)
  74. return -EINVAL;
  75. if (pos[0] == WLAN_EID_TIM) {
  76. u8 dtim_len = pos[1];
  77. u8 dtim_period = pos[3];
  78. u8 *next = pos + 2 + dtim_len;
  79. if (dtim_len < 3)
  80. return -EINVAL;
  81. memmove(pos, next, end - next);
  82. if (dtim_len > 3)
  83. skb_trim(skb, skb->len - (dtim_len - 3));
  84. pos = end - (dtim_len + 2);
  85. /* add the dummy at the end */
  86. pos[0] = WLAN_EID_TIM;
  87. pos[1] = 3;
  88. pos[2] = 0;
  89. pos[3] = dtim_period;
  90. pos[4] = 0;
  91. return 0;
  92. }
  93. pos += 2 + pos[1];
  94. }
  95. return 0;
  96. }
  97. static int p54_beacon_update(struct p54_common *priv,
  98. struct ieee80211_vif *vif)
  99. {
  100. struct sk_buff *beacon;
  101. __le32 old_beacon_req_id;
  102. int ret;
  103. beacon = ieee80211_beacon_get(priv->hw, vif);
  104. if (!beacon)
  105. return -ENOMEM;
  106. ret = p54_beacon_format_ie_tim(beacon);
  107. if (ret)
  108. return ret;
  109. old_beacon_req_id = priv->beacon_req_id;
  110. priv->beacon_req_id = GET_REQ_ID(beacon);
  111. ret = p54_tx_80211(priv->hw, beacon);
  112. if (ret) {
  113. priv->beacon_req_id = old_beacon_req_id;
  114. return -ENOSPC;
  115. }
  116. priv->tsf_high32 = 0;
  117. priv->tsf_low32 = 0;
  118. return 0;
  119. }
  120. static int p54_start(struct ieee80211_hw *dev)
  121. {
  122. struct p54_common *priv = dev->priv;
  123. int err;
  124. mutex_lock(&priv->conf_mutex);
  125. err = priv->open(dev);
  126. if (err)
  127. goto out;
  128. P54_SET_QUEUE(priv->qos_params[0], 0x0002, 0x0003, 0x0007, 47);
  129. P54_SET_QUEUE(priv->qos_params[1], 0x0002, 0x0007, 0x000f, 94);
  130. P54_SET_QUEUE(priv->qos_params[2], 0x0003, 0x000f, 0x03ff, 0);
  131. P54_SET_QUEUE(priv->qos_params[3], 0x0007, 0x000f, 0x03ff, 0);
  132. err = p54_set_edcf(priv);
  133. if (err)
  134. goto out;
  135. memset(priv->bssid, ~0, ETH_ALEN);
  136. priv->mode = NL80211_IFTYPE_MONITOR;
  137. err = p54_setup_mac(priv);
  138. if (err) {
  139. priv->mode = NL80211_IFTYPE_UNSPECIFIED;
  140. goto out;
  141. }
  142. queue_delayed_work(dev->workqueue, &priv->work, 0);
  143. priv->softled_state = 0;
  144. err = p54_set_leds(priv);
  145. out:
  146. mutex_unlock(&priv->conf_mutex);
  147. return err;
  148. }
  149. static void p54_stop(struct ieee80211_hw *dev)
  150. {
  151. struct p54_common *priv = dev->priv;
  152. int i;
  153. mutex_lock(&priv->conf_mutex);
  154. priv->mode = NL80211_IFTYPE_UNSPECIFIED;
  155. priv->softled_state = 0;
  156. p54_set_leds(priv);
  157. cancel_delayed_work_sync(&priv->work);
  158. priv->stop(dev);
  159. skb_queue_purge(&priv->tx_pending);
  160. skb_queue_purge(&priv->tx_queue);
  161. for (i = 0; i < P54_QUEUE_NUM; i++) {
  162. priv->tx_stats[i].count = 0;
  163. priv->tx_stats[i].len = 0;
  164. }
  165. priv->beacon_req_id = cpu_to_le32(0);
  166. priv->tsf_high32 = priv->tsf_low32 = 0;
  167. mutex_unlock(&priv->conf_mutex);
  168. }
  169. static int p54_add_interface(struct ieee80211_hw *dev,
  170. struct ieee80211_if_init_conf *conf)
  171. {
  172. struct p54_common *priv = dev->priv;
  173. mutex_lock(&priv->conf_mutex);
  174. if (priv->mode != NL80211_IFTYPE_MONITOR) {
  175. mutex_unlock(&priv->conf_mutex);
  176. return -EOPNOTSUPP;
  177. }
  178. priv->vif = conf->vif;
  179. switch (conf->type) {
  180. case NL80211_IFTYPE_STATION:
  181. case NL80211_IFTYPE_ADHOC:
  182. case NL80211_IFTYPE_AP:
  183. case NL80211_IFTYPE_MESH_POINT:
  184. priv->mode = conf->type;
  185. break;
  186. default:
  187. mutex_unlock(&priv->conf_mutex);
  188. return -EOPNOTSUPP;
  189. }
  190. memcpy(priv->mac_addr, conf->mac_addr, ETH_ALEN);
  191. p54_setup_mac(priv);
  192. mutex_unlock(&priv->conf_mutex);
  193. return 0;
  194. }
  195. static void p54_remove_interface(struct ieee80211_hw *dev,
  196. struct ieee80211_if_init_conf *conf)
  197. {
  198. struct p54_common *priv = dev->priv;
  199. mutex_lock(&priv->conf_mutex);
  200. priv->vif = NULL;
  201. if (priv->beacon_req_id) {
  202. p54_tx_cancel(priv, priv->beacon_req_id);
  203. priv->beacon_req_id = cpu_to_le32(0);
  204. }
  205. priv->mode = NL80211_IFTYPE_MONITOR;
  206. memset(priv->mac_addr, 0, ETH_ALEN);
  207. memset(priv->bssid, 0, ETH_ALEN);
  208. p54_setup_mac(priv);
  209. mutex_unlock(&priv->conf_mutex);
  210. }
  211. static int p54_config(struct ieee80211_hw *dev, u32 changed)
  212. {
  213. int ret = 0;
  214. struct p54_common *priv = dev->priv;
  215. struct ieee80211_conf *conf = &dev->conf;
  216. mutex_lock(&priv->conf_mutex);
  217. if (changed & IEEE80211_CONF_CHANGE_POWER)
  218. priv->output_power = conf->power_level << 2;
  219. if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
  220. ret = p54_scan(priv, P54_SCAN_EXIT, 0);
  221. if (ret)
  222. goto out;
  223. }
  224. if (changed & IEEE80211_CONF_CHANGE_PS) {
  225. ret = p54_set_ps(priv);
  226. if (ret)
  227. goto out;
  228. }
  229. out:
  230. mutex_unlock(&priv->conf_mutex);
  231. return ret;
  232. }
  233. static void p54_configure_filter(struct ieee80211_hw *dev,
  234. unsigned int changed_flags,
  235. unsigned int *total_flags,
  236. int mc_count, struct dev_mc_list *mclist)
  237. {
  238. struct p54_common *priv = dev->priv;
  239. *total_flags &= FIF_PROMISC_IN_BSS |
  240. FIF_OTHER_BSS;
  241. priv->filter_flags = *total_flags;
  242. if (changed_flags & (FIF_PROMISC_IN_BSS | FIF_OTHER_BSS))
  243. p54_setup_mac(priv);
  244. }
  245. static int p54_conf_tx(struct ieee80211_hw *dev, u16 queue,
  246. const struct ieee80211_tx_queue_params *params)
  247. {
  248. struct p54_common *priv = dev->priv;
  249. int ret;
  250. mutex_lock(&priv->conf_mutex);
  251. if ((params) && !(queue > 4)) {
  252. P54_SET_QUEUE(priv->qos_params[queue], params->aifs,
  253. params->cw_min, params->cw_max, params->txop);
  254. ret = p54_set_edcf(priv);
  255. } else
  256. ret = -EINVAL;
  257. mutex_unlock(&priv->conf_mutex);
  258. return ret;
  259. }
  260. static void p54_work(struct work_struct *work)
  261. {
  262. struct p54_common *priv = container_of(work, struct p54_common,
  263. work.work);
  264. if (unlikely(priv->mode == NL80211_IFTYPE_UNSPECIFIED))
  265. return ;
  266. /*
  267. * TODO: walk through tx_queue and do the following tasks
  268. * 1. initiate bursts.
  269. * 2. cancel stuck frames / reset the device if necessary.
  270. */
  271. p54_fetch_statistics(priv);
  272. }
  273. static int p54_get_stats(struct ieee80211_hw *dev,
  274. struct ieee80211_low_level_stats *stats)
  275. {
  276. struct p54_common *priv = dev->priv;
  277. memcpy(stats, &priv->stats, sizeof(*stats));
  278. return 0;
  279. }
  280. static int p54_get_tx_stats(struct ieee80211_hw *dev,
  281. struct ieee80211_tx_queue_stats *stats)
  282. {
  283. struct p54_common *priv = dev->priv;
  284. memcpy(stats, &priv->tx_stats[P54_QUEUE_DATA],
  285. sizeof(stats[0]) * dev->queues);
  286. return 0;
  287. }
  288. static void p54_bss_info_changed(struct ieee80211_hw *dev,
  289. struct ieee80211_vif *vif,
  290. struct ieee80211_bss_conf *info,
  291. u32 changed)
  292. {
  293. struct p54_common *priv = dev->priv;
  294. mutex_lock(&priv->conf_mutex);
  295. if (changed & BSS_CHANGED_BSSID) {
  296. memcpy(priv->bssid, info->bssid, ETH_ALEN);
  297. p54_setup_mac(priv);
  298. }
  299. if (changed & BSS_CHANGED_BEACON) {
  300. p54_scan(priv, P54_SCAN_EXIT, 0);
  301. p54_setup_mac(priv);
  302. p54_beacon_update(priv, vif);
  303. p54_set_edcf(priv);
  304. }
  305. if (changed & (BSS_CHANGED_ERP_SLOT | BSS_CHANGED_BEACON)) {
  306. priv->use_short_slot = info->use_short_slot;
  307. p54_set_edcf(priv);
  308. }
  309. if (changed & BSS_CHANGED_BASIC_RATES) {
  310. if (dev->conf.channel->band == IEEE80211_BAND_5GHZ)
  311. priv->basic_rate_mask = (info->basic_rates << 4);
  312. else
  313. priv->basic_rate_mask = info->basic_rates;
  314. p54_setup_mac(priv);
  315. if (priv->fw_var >= 0x500)
  316. p54_scan(priv, P54_SCAN_EXIT, 0);
  317. }
  318. if (changed & BSS_CHANGED_ASSOC) {
  319. if (info->assoc) {
  320. priv->aid = info->aid;
  321. priv->wakeup_timer = info->beacon_int *
  322. info->dtim_period * 5;
  323. p54_setup_mac(priv);
  324. }
  325. }
  326. mutex_unlock(&priv->conf_mutex);
  327. }
  328. static int p54_set_key(struct ieee80211_hw *dev, enum set_key_cmd cmd,
  329. struct ieee80211_vif *vif, struct ieee80211_sta *sta,
  330. struct ieee80211_key_conf *key)
  331. {
  332. struct p54_common *priv = dev->priv;
  333. int slot, ret = 0;
  334. u8 algo = 0;
  335. u8 *addr = NULL;
  336. if (modparam_nohwcrypt)
  337. return -EOPNOTSUPP;
  338. mutex_lock(&priv->conf_mutex);
  339. if (cmd == SET_KEY) {
  340. switch (key->alg) {
  341. case ALG_TKIP:
  342. if (!(priv->privacy_caps & (BR_DESC_PRIV_CAP_MICHAEL |
  343. BR_DESC_PRIV_CAP_TKIP))) {
  344. ret = -EOPNOTSUPP;
  345. goto out_unlock;
  346. }
  347. key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  348. algo = P54_CRYPTO_TKIPMICHAEL;
  349. break;
  350. case ALG_WEP:
  351. if (!(priv->privacy_caps & BR_DESC_PRIV_CAP_WEP)) {
  352. ret = -EOPNOTSUPP;
  353. goto out_unlock;
  354. }
  355. key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  356. algo = P54_CRYPTO_WEP;
  357. break;
  358. case ALG_CCMP:
  359. if (!(priv->privacy_caps & BR_DESC_PRIV_CAP_AESCCMP)) {
  360. ret = -EOPNOTSUPP;
  361. goto out_unlock;
  362. }
  363. key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  364. algo = P54_CRYPTO_AESCCMP;
  365. break;
  366. default:
  367. ret = -EOPNOTSUPP;
  368. goto out_unlock;
  369. }
  370. slot = bitmap_find_free_region(priv->used_rxkeys,
  371. priv->rx_keycache_size, 0);
  372. if (slot < 0) {
  373. /*
  374. * The device supports the choosen algorithm, but the
  375. * firmware does not provide enough key slots to store
  376. * all of them.
  377. * But encryption offload for outgoing frames is always
  378. * possible, so we just pretend that the upload was
  379. * successful and do the decryption in software.
  380. */
  381. /* mark the key as invalid. */
  382. key->hw_key_idx = 0xff;
  383. goto out_unlock;
  384. }
  385. } else {
  386. slot = key->hw_key_idx;
  387. if (slot == 0xff) {
  388. /* This key was not uploaded into the rx key cache. */
  389. goto out_unlock;
  390. }
  391. bitmap_release_region(priv->used_rxkeys, slot, 0);
  392. algo = 0;
  393. }
  394. if (sta)
  395. addr = sta->addr;
  396. ret = p54_upload_key(priv, algo, slot, key->keyidx,
  397. key->keylen, addr, key->key);
  398. if (ret) {
  399. bitmap_release_region(priv->used_rxkeys, slot, 0);
  400. ret = -EOPNOTSUPP;
  401. goto out_unlock;
  402. }
  403. key->hw_key_idx = slot;
  404. out_unlock:
  405. mutex_unlock(&priv->conf_mutex);
  406. return ret;
  407. }
  408. static const struct ieee80211_ops p54_ops = {
  409. .tx = p54_tx_80211,
  410. .start = p54_start,
  411. .stop = p54_stop,
  412. .add_interface = p54_add_interface,
  413. .remove_interface = p54_remove_interface,
  414. .set_tim = p54_set_tim,
  415. .sta_notify = p54_sta_notify,
  416. .set_key = p54_set_key,
  417. .config = p54_config,
  418. .bss_info_changed = p54_bss_info_changed,
  419. .configure_filter = p54_configure_filter,
  420. .conf_tx = p54_conf_tx,
  421. .get_stats = p54_get_stats,
  422. .get_tx_stats = p54_get_tx_stats
  423. };
  424. struct ieee80211_hw *p54_init_common(size_t priv_data_len)
  425. {
  426. struct ieee80211_hw *dev;
  427. struct p54_common *priv;
  428. dev = ieee80211_alloc_hw(priv_data_len, &p54_ops);
  429. if (!dev)
  430. return NULL;
  431. priv = dev->priv;
  432. priv->hw = dev;
  433. priv->mode = NL80211_IFTYPE_UNSPECIFIED;
  434. priv->basic_rate_mask = 0x15f;
  435. spin_lock_init(&priv->tx_stats_lock);
  436. skb_queue_head_init(&priv->tx_queue);
  437. skb_queue_head_init(&priv->tx_pending);
  438. dev->flags = IEEE80211_HW_RX_INCLUDES_FCS |
  439. IEEE80211_HW_SIGNAL_DBM |
  440. IEEE80211_HW_NOISE_DBM;
  441. dev->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
  442. BIT(NL80211_IFTYPE_ADHOC) |
  443. BIT(NL80211_IFTYPE_AP) |
  444. BIT(NL80211_IFTYPE_MESH_POINT);
  445. dev->channel_change_time = 1000; /* TODO: find actual value */
  446. priv->tx_stats[P54_QUEUE_BEACON].limit = 1;
  447. priv->tx_stats[P54_QUEUE_FWSCAN].limit = 1;
  448. priv->tx_stats[P54_QUEUE_MGMT].limit = 3;
  449. priv->tx_stats[P54_QUEUE_CAB].limit = 3;
  450. priv->tx_stats[P54_QUEUE_DATA].limit = 5;
  451. dev->queues = 1;
  452. priv->noise = -94;
  453. /*
  454. * We support at most 8 tries no matter which rate they're at,
  455. * we cannot support max_rates * max_rate_tries as we set it
  456. * here, but setting it correctly to 4/2 or so would limit us
  457. * artificially if the RC algorithm wants just two rates, so
  458. * let's say 4/7, we'll redistribute it at TX time, see the
  459. * comments there.
  460. */
  461. dev->max_rates = 4;
  462. dev->max_rate_tries = 7;
  463. dev->extra_tx_headroom = sizeof(struct p54_hdr) + 4 +
  464. sizeof(struct p54_tx_data);
  465. mutex_init(&priv->conf_mutex);
  466. mutex_init(&priv->eeprom_mutex);
  467. init_completion(&priv->eeprom_comp);
  468. INIT_DELAYED_WORK(&priv->work, p54_work);
  469. return dev;
  470. }
  471. EXPORT_SYMBOL_GPL(p54_init_common);
  472. int p54_register_common(struct ieee80211_hw *dev, struct device *pdev)
  473. {
  474. struct p54_common *priv = dev->priv;
  475. int err;
  476. err = ieee80211_register_hw(dev);
  477. if (err) {
  478. dev_err(pdev, "Cannot register device (%d).\n", err);
  479. return err;
  480. }
  481. #ifdef CONFIG_P54_LEDS
  482. err = p54_init_leds(priv);
  483. if (err)
  484. return err;
  485. #endif /* CONFIG_P54_LEDS */
  486. dev_info(pdev, "is registered as '%s'\n", wiphy_name(dev->wiphy));
  487. return 0;
  488. }
  489. EXPORT_SYMBOL_GPL(p54_register_common);
  490. void p54_free_common(struct ieee80211_hw *dev)
  491. {
  492. struct p54_common *priv = dev->priv;
  493. kfree(priv->iq_autocal);
  494. kfree(priv->output_limit);
  495. kfree(priv->curve_data);
  496. kfree(priv->used_rxkeys);
  497. priv->iq_autocal = NULL;
  498. priv->output_limit = NULL;
  499. priv->curve_data = NULL;
  500. priv->used_rxkeys = NULL;
  501. ieee80211_free_hw(dev);
  502. }
  503. EXPORT_SYMBOL_GPL(p54_free_common);
  504. void p54_unregister_common(struct ieee80211_hw *dev)
  505. {
  506. struct p54_common *priv = dev->priv;
  507. #ifdef CONFIG_P54_LEDS
  508. p54_unregister_leds(priv);
  509. #endif /* CONFIG_P54_LEDS */
  510. ieee80211_unregister_hw(dev);
  511. mutex_destroy(&priv->conf_mutex);
  512. mutex_destroy(&priv->eeprom_mutex);
  513. }
  514. EXPORT_SYMBOL_GPL(p54_unregister_common);