main.c 21 KB

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