main.c 21 KB

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