main.c 21 KB

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