main.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845
  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/slab.h>
  20. #include <linux/firmware.h>
  21. #include <linux/etherdevice.h>
  22. #include <linux/module.h>
  23. #include <net/mac80211.h>
  24. #include "p54.h"
  25. #include "lmac.h"
  26. static int modparam_nohwcrypt;
  27. module_param_named(nohwcrypt, modparam_nohwcrypt, bool, S_IRUGO);
  28. MODULE_PARM_DESC(nohwcrypt, "Disable hardware encryption.");
  29. MODULE_AUTHOR("Michael Wu <flamingice@sourmilk.net>");
  30. MODULE_DESCRIPTION("Softmac Prism54 common code");
  31. MODULE_LICENSE("GPL");
  32. MODULE_ALIAS("prism54common");
  33. static int p54_sta_add_remove(struct ieee80211_hw *hw,
  34. struct ieee80211_vif *vif,
  35. struct ieee80211_sta *sta)
  36. {
  37. struct p54_common *priv = hw->priv;
  38. /*
  39. * Notify the firmware that we don't want or we don't
  40. * need to buffer frames for this station anymore.
  41. */
  42. p54_sta_unlock(priv, sta->addr);
  43. return 0;
  44. }
  45. static void p54_sta_notify(struct ieee80211_hw *dev, struct ieee80211_vif *vif,
  46. enum sta_notify_cmd notify_cmd,
  47. struct ieee80211_sta *sta)
  48. {
  49. struct p54_common *priv = dev->priv;
  50. switch (notify_cmd) {
  51. case STA_NOTIFY_AWAKE:
  52. /* update the firmware's filter table */
  53. p54_sta_unlock(priv, sta->addr);
  54. break;
  55. default:
  56. break;
  57. }
  58. }
  59. static int p54_set_tim(struct ieee80211_hw *dev, struct ieee80211_sta *sta,
  60. bool set)
  61. {
  62. struct p54_common *priv = dev->priv;
  63. return p54_update_beacon_tim(priv, sta->aid, set);
  64. }
  65. u8 *p54_find_ie(struct sk_buff *skb, u8 ie)
  66. {
  67. struct ieee80211_mgmt *mgmt = (void *)skb->data;
  68. u8 *pos, *end;
  69. if (skb->len <= sizeof(mgmt))
  70. return NULL;
  71. pos = (u8 *)mgmt->u.beacon.variable;
  72. end = skb->data + skb->len;
  73. while (pos < end) {
  74. if (pos + 2 + pos[1] > end)
  75. return NULL;
  76. if (pos[0] == ie)
  77. return pos;
  78. pos += 2 + pos[1];
  79. }
  80. return NULL;
  81. }
  82. static int p54_beacon_format_ie_tim(struct sk_buff *skb)
  83. {
  84. /*
  85. * the good excuse for this mess is ... the firmware.
  86. * The dummy TIM MUST be at the end of the beacon frame,
  87. * because it'll be overwritten!
  88. */
  89. u8 *tim;
  90. u8 dtim_len;
  91. u8 dtim_period;
  92. u8 *next;
  93. tim = p54_find_ie(skb, WLAN_EID_TIM);
  94. if (!tim)
  95. return 0;
  96. dtim_len = tim[1];
  97. dtim_period = tim[3];
  98. next = tim + 2 + dtim_len;
  99. if (dtim_len < 3)
  100. return -EINVAL;
  101. memmove(tim, next, skb_tail_pointer(skb) - next);
  102. tim = skb_tail_pointer(skb) - (dtim_len + 2);
  103. /* add the dummy at the end */
  104. tim[0] = WLAN_EID_TIM;
  105. tim[1] = 3;
  106. tim[2] = 0;
  107. tim[3] = dtim_period;
  108. tim[4] = 0;
  109. if (dtim_len > 3)
  110. skb_trim(skb, skb->len - (dtim_len - 3));
  111. return 0;
  112. }
  113. static int p54_beacon_update(struct p54_common *priv,
  114. struct ieee80211_vif *vif)
  115. {
  116. struct sk_buff *beacon;
  117. int ret;
  118. beacon = ieee80211_beacon_get(priv->hw, vif);
  119. if (!beacon)
  120. return -ENOMEM;
  121. ret = p54_beacon_format_ie_tim(beacon);
  122. if (ret)
  123. return ret;
  124. /*
  125. * During operation, the firmware takes care of beaconing.
  126. * The driver only needs to upload a new beacon template, once
  127. * the template was changed by the stack or userspace.
  128. *
  129. * LMAC API 3.2.2 also specifies that the driver does not need
  130. * to cancel the old beacon template by hand, instead the firmware
  131. * will release the previous one through the feedback mechanism.
  132. */
  133. p54_tx_80211(priv->hw, beacon);
  134. priv->tsf_high32 = 0;
  135. priv->tsf_low32 = 0;
  136. return 0;
  137. }
  138. static int p54_start(struct ieee80211_hw *dev)
  139. {
  140. struct p54_common *priv = dev->priv;
  141. int err;
  142. mutex_lock(&priv->conf_mutex);
  143. err = priv->open(dev);
  144. if (err)
  145. goto out;
  146. P54_SET_QUEUE(priv->qos_params[0], 0x0002, 0x0003, 0x0007, 47);
  147. P54_SET_QUEUE(priv->qos_params[1], 0x0002, 0x0007, 0x000f, 94);
  148. P54_SET_QUEUE(priv->qos_params[2], 0x0003, 0x000f, 0x03ff, 0);
  149. P54_SET_QUEUE(priv->qos_params[3], 0x0007, 0x000f, 0x03ff, 0);
  150. err = p54_set_edcf(priv);
  151. if (err)
  152. goto out;
  153. memset(priv->bssid, ~0, ETH_ALEN);
  154. priv->mode = NL80211_IFTYPE_MONITOR;
  155. err = p54_setup_mac(priv);
  156. if (err) {
  157. priv->mode = NL80211_IFTYPE_UNSPECIFIED;
  158. goto out;
  159. }
  160. ieee80211_queue_delayed_work(dev, &priv->work, 0);
  161. priv->softled_state = 0;
  162. err = p54_set_leds(priv);
  163. out:
  164. mutex_unlock(&priv->conf_mutex);
  165. return err;
  166. }
  167. static void p54_stop(struct ieee80211_hw *dev)
  168. {
  169. struct p54_common *priv = dev->priv;
  170. int i;
  171. priv->mode = NL80211_IFTYPE_UNSPECIFIED;
  172. priv->softled_state = 0;
  173. cancel_delayed_work_sync(&priv->work);
  174. mutex_lock(&priv->conf_mutex);
  175. p54_set_leds(priv);
  176. priv->stop(dev);
  177. skb_queue_purge(&priv->tx_pending);
  178. skb_queue_purge(&priv->tx_queue);
  179. for (i = 0; i < P54_QUEUE_NUM; i++) {
  180. priv->tx_stats[i].count = 0;
  181. priv->tx_stats[i].len = 0;
  182. }
  183. priv->beacon_req_id = cpu_to_le32(0);
  184. priv->tsf_high32 = priv->tsf_low32 = 0;
  185. mutex_unlock(&priv->conf_mutex);
  186. }
  187. static int p54_add_interface(struct ieee80211_hw *dev,
  188. struct ieee80211_vif *vif)
  189. {
  190. struct p54_common *priv = dev->priv;
  191. mutex_lock(&priv->conf_mutex);
  192. if (priv->mode != NL80211_IFTYPE_MONITOR) {
  193. mutex_unlock(&priv->conf_mutex);
  194. return -EOPNOTSUPP;
  195. }
  196. priv->vif = vif;
  197. switch (vif->type) {
  198. case NL80211_IFTYPE_STATION:
  199. case NL80211_IFTYPE_ADHOC:
  200. case NL80211_IFTYPE_AP:
  201. case NL80211_IFTYPE_MESH_POINT:
  202. priv->mode = vif->type;
  203. break;
  204. default:
  205. mutex_unlock(&priv->conf_mutex);
  206. return -EOPNOTSUPP;
  207. }
  208. memcpy(priv->mac_addr, vif->addr, ETH_ALEN);
  209. p54_setup_mac(priv);
  210. mutex_unlock(&priv->conf_mutex);
  211. return 0;
  212. }
  213. static void p54_remove_interface(struct ieee80211_hw *dev,
  214. struct ieee80211_vif *vif)
  215. {
  216. struct p54_common *priv = dev->priv;
  217. mutex_lock(&priv->conf_mutex);
  218. priv->vif = NULL;
  219. /*
  220. * LMAC API 3.2.2 states that any active beacon template must be
  221. * canceled by the driver before attempting a mode transition.
  222. */
  223. if (le32_to_cpu(priv->beacon_req_id) != 0) {
  224. p54_tx_cancel(priv, priv->beacon_req_id);
  225. wait_for_completion_interruptible_timeout(&priv->beacon_comp, HZ);
  226. }
  227. priv->mode = NL80211_IFTYPE_MONITOR;
  228. memset(priv->mac_addr, 0, ETH_ALEN);
  229. memset(priv->bssid, 0, ETH_ALEN);
  230. p54_setup_mac(priv);
  231. mutex_unlock(&priv->conf_mutex);
  232. }
  233. static int p54_wait_for_stats(struct ieee80211_hw *dev)
  234. {
  235. struct p54_common *priv = dev->priv;
  236. int ret;
  237. priv->update_stats = true;
  238. ret = p54_fetch_statistics(priv);
  239. if (ret)
  240. return ret;
  241. ret = wait_for_completion_interruptible_timeout(&priv->stat_comp, HZ);
  242. if (ret == 0)
  243. return -ETIMEDOUT;
  244. return 0;
  245. }
  246. static void p54_reset_stats(struct p54_common *priv)
  247. {
  248. struct ieee80211_channel *chan = priv->curchan;
  249. if (chan) {
  250. struct survey_info *info = &priv->survey[chan->hw_value];
  251. /* only reset channel statistics, don't touch .filled, etc. */
  252. info->channel_time = 0;
  253. info->channel_time_busy = 0;
  254. info->channel_time_tx = 0;
  255. }
  256. priv->update_stats = true;
  257. priv->survey_raw.active = 0;
  258. priv->survey_raw.cca = 0;
  259. priv->survey_raw.tx = 0;
  260. }
  261. static int p54_config(struct ieee80211_hw *dev, u32 changed)
  262. {
  263. int ret = 0;
  264. struct p54_common *priv = dev->priv;
  265. struct ieee80211_conf *conf = &dev->conf;
  266. mutex_lock(&priv->conf_mutex);
  267. if (changed & IEEE80211_CONF_CHANGE_POWER)
  268. priv->output_power = conf->power_level << 2;
  269. if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
  270. struct ieee80211_channel *oldchan;
  271. WARN_ON(p54_wait_for_stats(dev));
  272. oldchan = priv->curchan;
  273. priv->curchan = NULL;
  274. ret = p54_scan(priv, P54_SCAN_EXIT, 0);
  275. if (ret) {
  276. priv->curchan = oldchan;
  277. goto out;
  278. }
  279. /*
  280. * TODO: Use the LM_SCAN_TRAP to determine the current
  281. * operating channel.
  282. */
  283. priv->curchan = priv->hw->conf.channel;
  284. p54_reset_stats(priv);
  285. WARN_ON(p54_fetch_statistics(priv));
  286. }
  287. if (changed & IEEE80211_CONF_CHANGE_PS) {
  288. WARN_ON(p54_wait_for_stats(dev));
  289. ret = p54_set_ps(priv);
  290. if (ret)
  291. goto out;
  292. WARN_ON(p54_wait_for_stats(dev));
  293. }
  294. if (changed & IEEE80211_CONF_CHANGE_IDLE) {
  295. WARN_ON(p54_wait_for_stats(dev));
  296. ret = p54_setup_mac(priv);
  297. if (ret)
  298. goto out;
  299. WARN_ON(p54_wait_for_stats(dev));
  300. }
  301. out:
  302. mutex_unlock(&priv->conf_mutex);
  303. return ret;
  304. }
  305. static u64 p54_prepare_multicast(struct ieee80211_hw *dev,
  306. struct netdev_hw_addr_list *mc_list)
  307. {
  308. struct p54_common *priv = dev->priv;
  309. struct netdev_hw_addr *ha;
  310. int i;
  311. BUILD_BUG_ON(ARRAY_SIZE(priv->mc_maclist) !=
  312. ARRAY_SIZE(((struct p54_group_address_table *)NULL)->mac_list));
  313. /*
  314. * The first entry is reserved for the global broadcast MAC.
  315. * Otherwise the firmware will drop it and ARP will no longer work.
  316. */
  317. i = 1;
  318. priv->mc_maclist_num = netdev_hw_addr_list_count(mc_list) + i;
  319. netdev_hw_addr_list_for_each(ha, mc_list) {
  320. memcpy(&priv->mc_maclist[i], ha->addr, ETH_ALEN);
  321. i++;
  322. if (i >= ARRAY_SIZE(priv->mc_maclist))
  323. break;
  324. }
  325. return 1; /* update */
  326. }
  327. static void p54_configure_filter(struct ieee80211_hw *dev,
  328. unsigned int changed_flags,
  329. unsigned int *total_flags,
  330. u64 multicast)
  331. {
  332. struct p54_common *priv = dev->priv;
  333. *total_flags &= FIF_PROMISC_IN_BSS |
  334. FIF_ALLMULTI |
  335. FIF_OTHER_BSS;
  336. priv->filter_flags = *total_flags;
  337. if (changed_flags & (FIF_PROMISC_IN_BSS | FIF_OTHER_BSS))
  338. p54_setup_mac(priv);
  339. if (changed_flags & FIF_ALLMULTI || multicast)
  340. p54_set_groupfilter(priv);
  341. }
  342. static int p54_conf_tx(struct ieee80211_hw *dev,
  343. struct ieee80211_vif *vif, u16 queue,
  344. const struct ieee80211_tx_queue_params *params)
  345. {
  346. struct p54_common *priv = dev->priv;
  347. int ret;
  348. mutex_lock(&priv->conf_mutex);
  349. if (queue < dev->queues) {
  350. P54_SET_QUEUE(priv->qos_params[queue], params->aifs,
  351. params->cw_min, params->cw_max, params->txop);
  352. ret = p54_set_edcf(priv);
  353. } else
  354. ret = -EINVAL;
  355. mutex_unlock(&priv->conf_mutex);
  356. return ret;
  357. }
  358. static void p54_work(struct work_struct *work)
  359. {
  360. struct p54_common *priv = container_of(work, struct p54_common,
  361. work.work);
  362. if (unlikely(priv->mode == NL80211_IFTYPE_UNSPECIFIED))
  363. return ;
  364. /*
  365. * TODO: walk through tx_queue and do the following tasks
  366. * 1. initiate bursts.
  367. * 2. cancel stuck frames / reset the device if necessary.
  368. */
  369. mutex_lock(&priv->conf_mutex);
  370. WARN_ON_ONCE(p54_fetch_statistics(priv));
  371. mutex_unlock(&priv->conf_mutex);
  372. }
  373. static int p54_get_stats(struct ieee80211_hw *dev,
  374. struct ieee80211_low_level_stats *stats)
  375. {
  376. struct p54_common *priv = dev->priv;
  377. memcpy(stats, &priv->stats, sizeof(*stats));
  378. return 0;
  379. }
  380. static void p54_bss_info_changed(struct ieee80211_hw *dev,
  381. struct ieee80211_vif *vif,
  382. struct ieee80211_bss_conf *info,
  383. u32 changed)
  384. {
  385. struct p54_common *priv = dev->priv;
  386. mutex_lock(&priv->conf_mutex);
  387. if (changed & BSS_CHANGED_BSSID) {
  388. memcpy(priv->bssid, info->bssid, ETH_ALEN);
  389. p54_setup_mac(priv);
  390. }
  391. if (changed & BSS_CHANGED_BEACON) {
  392. p54_scan(priv, P54_SCAN_EXIT, 0);
  393. p54_setup_mac(priv);
  394. p54_beacon_update(priv, vif);
  395. p54_set_edcf(priv);
  396. }
  397. if (changed & (BSS_CHANGED_ERP_SLOT | BSS_CHANGED_BEACON)) {
  398. priv->use_short_slot = info->use_short_slot;
  399. p54_set_edcf(priv);
  400. }
  401. if (changed & BSS_CHANGED_BASIC_RATES) {
  402. if (dev->conf.channel->band == IEEE80211_BAND_5GHZ)
  403. priv->basic_rate_mask = (info->basic_rates << 4);
  404. else
  405. priv->basic_rate_mask = info->basic_rates;
  406. p54_setup_mac(priv);
  407. if (priv->fw_var >= 0x500)
  408. p54_scan(priv, P54_SCAN_EXIT, 0);
  409. }
  410. if (changed & BSS_CHANGED_ASSOC) {
  411. if (info->assoc) {
  412. priv->aid = info->aid;
  413. priv->wakeup_timer = info->beacon_int *
  414. info->dtim_period * 5;
  415. p54_setup_mac(priv);
  416. } else {
  417. priv->wakeup_timer = 500;
  418. priv->aid = 0;
  419. }
  420. }
  421. mutex_unlock(&priv->conf_mutex);
  422. }
  423. static int p54_set_key(struct ieee80211_hw *dev, enum set_key_cmd cmd,
  424. struct ieee80211_vif *vif, struct ieee80211_sta *sta,
  425. struct ieee80211_key_conf *key)
  426. {
  427. struct p54_common *priv = dev->priv;
  428. int slot, ret = 0;
  429. u8 algo = 0;
  430. u8 *addr = NULL;
  431. if (modparam_nohwcrypt)
  432. return -EOPNOTSUPP;
  433. mutex_lock(&priv->conf_mutex);
  434. if (cmd == SET_KEY) {
  435. switch (key->cipher) {
  436. case WLAN_CIPHER_SUITE_TKIP:
  437. if (!(priv->privacy_caps & (BR_DESC_PRIV_CAP_MICHAEL |
  438. BR_DESC_PRIV_CAP_TKIP))) {
  439. ret = -EOPNOTSUPP;
  440. goto out_unlock;
  441. }
  442. key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  443. algo = P54_CRYPTO_TKIPMICHAEL;
  444. break;
  445. case WLAN_CIPHER_SUITE_WEP40:
  446. case WLAN_CIPHER_SUITE_WEP104:
  447. if (!(priv->privacy_caps & BR_DESC_PRIV_CAP_WEP)) {
  448. ret = -EOPNOTSUPP;
  449. goto out_unlock;
  450. }
  451. key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  452. algo = P54_CRYPTO_WEP;
  453. break;
  454. case WLAN_CIPHER_SUITE_CCMP:
  455. if (!(priv->privacy_caps & BR_DESC_PRIV_CAP_AESCCMP)) {
  456. ret = -EOPNOTSUPP;
  457. goto out_unlock;
  458. }
  459. key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  460. algo = P54_CRYPTO_AESCCMP;
  461. break;
  462. default:
  463. ret = -EOPNOTSUPP;
  464. goto out_unlock;
  465. }
  466. slot = bitmap_find_free_region(priv->used_rxkeys,
  467. priv->rx_keycache_size, 0);
  468. if (slot < 0) {
  469. /*
  470. * The device supports the chosen algorithm, but the
  471. * firmware does not provide enough key slots to store
  472. * all of them.
  473. * But encryption offload for outgoing frames is always
  474. * possible, so we just pretend that the upload was
  475. * successful and do the decryption in software.
  476. */
  477. /* mark the key as invalid. */
  478. key->hw_key_idx = 0xff;
  479. goto out_unlock;
  480. }
  481. } else {
  482. slot = key->hw_key_idx;
  483. if (slot == 0xff) {
  484. /* This key was not uploaded into the rx key cache. */
  485. goto out_unlock;
  486. }
  487. bitmap_release_region(priv->used_rxkeys, slot, 0);
  488. algo = 0;
  489. }
  490. if (sta)
  491. addr = sta->addr;
  492. ret = p54_upload_key(priv, algo, slot, key->keyidx,
  493. key->keylen, addr, key->key);
  494. if (ret) {
  495. bitmap_release_region(priv->used_rxkeys, slot, 0);
  496. ret = -EOPNOTSUPP;
  497. goto out_unlock;
  498. }
  499. key->hw_key_idx = slot;
  500. out_unlock:
  501. mutex_unlock(&priv->conf_mutex);
  502. return ret;
  503. }
  504. static int p54_get_survey(struct ieee80211_hw *dev, int idx,
  505. struct survey_info *survey)
  506. {
  507. struct p54_common *priv = dev->priv;
  508. struct ieee80211_channel *chan;
  509. int err, tries;
  510. bool in_use = false;
  511. if (idx >= priv->chan_num)
  512. return -ENOENT;
  513. #define MAX_TRIES 1
  514. for (tries = 0; tries < MAX_TRIES; tries++) {
  515. chan = priv->curchan;
  516. if (chan && chan->hw_value == idx) {
  517. mutex_lock(&priv->conf_mutex);
  518. err = p54_wait_for_stats(dev);
  519. mutex_unlock(&priv->conf_mutex);
  520. if (err)
  521. return err;
  522. in_use = true;
  523. }
  524. memcpy(survey, &priv->survey[idx], sizeof(*survey));
  525. if (in_use) {
  526. /* test if the reported statistics are valid. */
  527. if (survey->channel_time != 0) {
  528. survey->filled |= SURVEY_INFO_IN_USE;
  529. } else {
  530. /*
  531. * hw/fw has not accumulated enough sample sets.
  532. * Wait for 100ms, this ought to be enough to
  533. * to get at least one non-null set of channel
  534. * usage statistics.
  535. */
  536. msleep(100);
  537. continue;
  538. }
  539. }
  540. return 0;
  541. }
  542. return -ETIMEDOUT;
  543. #undef MAX_TRIES
  544. }
  545. static unsigned int p54_flush_count(struct p54_common *priv)
  546. {
  547. unsigned int total = 0, i;
  548. BUILD_BUG_ON(P54_QUEUE_NUM > ARRAY_SIZE(priv->tx_stats));
  549. /*
  550. * Because the firmware has the sole control over any frames
  551. * in the P54_QUEUE_BEACON or P54_QUEUE_SCAN queues, they
  552. * don't really count as pending or active.
  553. */
  554. for (i = P54_QUEUE_MGMT; i < P54_QUEUE_NUM; i++)
  555. total += priv->tx_stats[i].len;
  556. return total;
  557. }
  558. static void p54_flush(struct ieee80211_hw *dev, bool drop)
  559. {
  560. struct p54_common *priv = dev->priv;
  561. unsigned int total, i;
  562. /*
  563. * Currently, it wouldn't really matter if we wait for one second
  564. * or 15 minutes. But once someone gets around and completes the
  565. * TODOs [ancel stuck frames / reset device] in p54_work, it will
  566. * suddenly make sense to wait that long.
  567. */
  568. i = P54_STATISTICS_UPDATE * 2 / 20;
  569. /*
  570. * In this case no locking is required because as we speak the
  571. * queues have already been stopped and no new frames can sneak
  572. * up from behind.
  573. */
  574. while ((total = p54_flush_count(priv) && i--)) {
  575. /* waste time */
  576. msleep(20);
  577. }
  578. WARN(total, "tx flush timeout, unresponsive firmware");
  579. }
  580. static void p54_set_coverage_class(struct ieee80211_hw *dev, u8 coverage_class)
  581. {
  582. struct p54_common *priv = dev->priv;
  583. mutex_lock(&priv->conf_mutex);
  584. /* support all coverage class values as in 802.11-2007 Table 7-27 */
  585. priv->coverage_class = clamp_t(u8, coverage_class, 0, 31);
  586. p54_set_edcf(priv);
  587. mutex_unlock(&priv->conf_mutex);
  588. }
  589. static const struct ieee80211_ops p54_ops = {
  590. .tx = p54_tx_80211,
  591. .start = p54_start,
  592. .stop = p54_stop,
  593. .add_interface = p54_add_interface,
  594. .remove_interface = p54_remove_interface,
  595. .set_tim = p54_set_tim,
  596. .sta_notify = p54_sta_notify,
  597. .sta_add = p54_sta_add_remove,
  598. .sta_remove = p54_sta_add_remove,
  599. .set_key = p54_set_key,
  600. .config = p54_config,
  601. .flush = p54_flush,
  602. .bss_info_changed = p54_bss_info_changed,
  603. .prepare_multicast = p54_prepare_multicast,
  604. .configure_filter = p54_configure_filter,
  605. .conf_tx = p54_conf_tx,
  606. .get_stats = p54_get_stats,
  607. .get_survey = p54_get_survey,
  608. .set_coverage_class = p54_set_coverage_class,
  609. };
  610. struct ieee80211_hw *p54_init_common(size_t priv_data_len)
  611. {
  612. struct ieee80211_hw *dev;
  613. struct p54_common *priv;
  614. dev = ieee80211_alloc_hw(priv_data_len, &p54_ops);
  615. if (!dev)
  616. return NULL;
  617. priv = dev->priv;
  618. priv->hw = dev;
  619. priv->mode = NL80211_IFTYPE_UNSPECIFIED;
  620. priv->basic_rate_mask = 0x15f;
  621. spin_lock_init(&priv->tx_stats_lock);
  622. skb_queue_head_init(&priv->tx_queue);
  623. skb_queue_head_init(&priv->tx_pending);
  624. dev->flags = IEEE80211_HW_RX_INCLUDES_FCS |
  625. IEEE80211_HW_SIGNAL_DBM |
  626. IEEE80211_HW_SUPPORTS_PS |
  627. IEEE80211_HW_PS_NULLFUNC_STACK |
  628. IEEE80211_HW_BEACON_FILTER |
  629. IEEE80211_HW_REPORTS_TX_ACK_STATUS;
  630. dev->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
  631. BIT(NL80211_IFTYPE_ADHOC) |
  632. BIT(NL80211_IFTYPE_AP) |
  633. BIT(NL80211_IFTYPE_MESH_POINT);
  634. dev->channel_change_time = 1000; /* TODO: find actual value */
  635. priv->beacon_req_id = cpu_to_le32(0);
  636. priv->tx_stats[P54_QUEUE_BEACON].limit = 1;
  637. priv->tx_stats[P54_QUEUE_FWSCAN].limit = 1;
  638. priv->tx_stats[P54_QUEUE_MGMT].limit = 3;
  639. priv->tx_stats[P54_QUEUE_CAB].limit = 3;
  640. priv->tx_stats[P54_QUEUE_DATA].limit = 5;
  641. dev->queues = 1;
  642. priv->noise = -94;
  643. /*
  644. * We support at most 8 tries no matter which rate they're at,
  645. * we cannot support max_rates * max_rate_tries as we set it
  646. * here, but setting it correctly to 4/2 or so would limit us
  647. * artificially if the RC algorithm wants just two rates, so
  648. * let's say 4/7, we'll redistribute it at TX time, see the
  649. * comments there.
  650. */
  651. dev->max_rates = 4;
  652. dev->max_rate_tries = 7;
  653. dev->extra_tx_headroom = sizeof(struct p54_hdr) + 4 +
  654. sizeof(struct p54_tx_data);
  655. /*
  656. * For now, disable PS by default because it affects
  657. * link stability significantly.
  658. */
  659. dev->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
  660. mutex_init(&priv->conf_mutex);
  661. mutex_init(&priv->eeprom_mutex);
  662. init_completion(&priv->stat_comp);
  663. init_completion(&priv->eeprom_comp);
  664. init_completion(&priv->beacon_comp);
  665. INIT_DELAYED_WORK(&priv->work, p54_work);
  666. memset(&priv->mc_maclist[0], ~0, ETH_ALEN);
  667. priv->curchan = NULL;
  668. p54_reset_stats(priv);
  669. return dev;
  670. }
  671. EXPORT_SYMBOL_GPL(p54_init_common);
  672. int p54_register_common(struct ieee80211_hw *dev, struct device *pdev)
  673. {
  674. struct p54_common __maybe_unused *priv = dev->priv;
  675. int err;
  676. err = ieee80211_register_hw(dev);
  677. if (err) {
  678. dev_err(pdev, "Cannot register device (%d).\n", err);
  679. return err;
  680. }
  681. #ifdef CONFIG_P54_LEDS
  682. err = p54_init_leds(priv);
  683. if (err)
  684. return err;
  685. #endif /* CONFIG_P54_LEDS */
  686. dev_info(pdev, "is registered as '%s'\n", wiphy_name(dev->wiphy));
  687. return 0;
  688. }
  689. EXPORT_SYMBOL_GPL(p54_register_common);
  690. void p54_free_common(struct ieee80211_hw *dev)
  691. {
  692. struct p54_common *priv = dev->priv;
  693. unsigned int i;
  694. for (i = 0; i < IEEE80211_NUM_BANDS; i++)
  695. kfree(priv->band_table[i]);
  696. kfree(priv->iq_autocal);
  697. kfree(priv->output_limit);
  698. kfree(priv->curve_data);
  699. kfree(priv->rssi_db);
  700. kfree(priv->used_rxkeys);
  701. kfree(priv->survey);
  702. priv->iq_autocal = NULL;
  703. priv->output_limit = NULL;
  704. priv->curve_data = NULL;
  705. priv->rssi_db = NULL;
  706. priv->used_rxkeys = NULL;
  707. priv->survey = NULL;
  708. ieee80211_free_hw(dev);
  709. }
  710. EXPORT_SYMBOL_GPL(p54_free_common);
  711. void p54_unregister_common(struct ieee80211_hw *dev)
  712. {
  713. struct p54_common *priv = dev->priv;
  714. #ifdef CONFIG_P54_LEDS
  715. p54_unregister_leds(priv);
  716. #endif /* CONFIG_P54_LEDS */
  717. ieee80211_unregister_hw(dev);
  718. mutex_destroy(&priv->conf_mutex);
  719. mutex_destroy(&priv->eeprom_mutex);
  720. }
  721. EXPORT_SYMBOL_GPL(p54_unregister_common);