init.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  1. /*
  2. * (c) Copyright 2002-2010, Ralink Technology, Inc.
  3. * Copyright (C) 2014 Felix Fietkau <nbd@openwrt.org>
  4. * Copyright (C) 2015 Jakub Kicinski <kubakici@wp.pl>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2
  8. * as published by the Free Software Foundation
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include "mt7601u.h"
  16. #include "eeprom.h"
  17. #include "trace.h"
  18. #include "mcu.h"
  19. #include "initvals.h"
  20. static void
  21. mt7601u_set_wlan_state(struct mt7601u_dev *dev, u32 val, bool enable)
  22. {
  23. int i;
  24. /* Note: we don't turn off WLAN_CLK because that makes the device
  25. * not respond properly on the probe path.
  26. * In case anyone (PSM?) wants to use this function we can
  27. * bring the clock stuff back and fixup the probe path.
  28. */
  29. if (enable)
  30. val |= (MT_WLAN_FUN_CTRL_WLAN_EN |
  31. MT_WLAN_FUN_CTRL_WLAN_CLK_EN);
  32. else
  33. val &= ~(MT_WLAN_FUN_CTRL_WLAN_EN);
  34. mt7601u_wr(dev, MT_WLAN_FUN_CTRL, val);
  35. udelay(20);
  36. if (enable) {
  37. set_bit(MT7601U_STATE_WLAN_RUNNING, &dev->state);
  38. } else {
  39. clear_bit(MT7601U_STATE_WLAN_RUNNING, &dev->state);
  40. return;
  41. }
  42. for (i = 200; i; i--) {
  43. val = mt7601u_rr(dev, MT_CMB_CTRL);
  44. if (val & MT_CMB_CTRL_XTAL_RDY && val & MT_CMB_CTRL_PLL_LD)
  45. break;
  46. udelay(20);
  47. }
  48. /* Note: vendor driver tries to disable/enable wlan here and retry
  49. * but the code which does it is so buggy it must have never
  50. * triggered, so don't bother.
  51. */
  52. if (!i)
  53. dev_err(dev->dev, "Error: PLL and XTAL check failed!\n");
  54. }
  55. static void mt7601u_chip_onoff(struct mt7601u_dev *dev, bool enable, bool reset)
  56. {
  57. u32 val;
  58. mutex_lock(&dev->hw_atomic_mutex);
  59. val = mt7601u_rr(dev, MT_WLAN_FUN_CTRL);
  60. if (reset) {
  61. val |= MT_WLAN_FUN_CTRL_GPIO_OUT_EN;
  62. val &= ~MT_WLAN_FUN_CTRL_FRC_WL_ANT_SEL;
  63. if (val & MT_WLAN_FUN_CTRL_WLAN_EN) {
  64. val |= (MT_WLAN_FUN_CTRL_WLAN_RESET |
  65. MT_WLAN_FUN_CTRL_WLAN_RESET_RF);
  66. mt7601u_wr(dev, MT_WLAN_FUN_CTRL, val);
  67. udelay(20);
  68. val &= ~(MT_WLAN_FUN_CTRL_WLAN_RESET |
  69. MT_WLAN_FUN_CTRL_WLAN_RESET_RF);
  70. }
  71. }
  72. mt7601u_wr(dev, MT_WLAN_FUN_CTRL, val);
  73. udelay(20);
  74. mt7601u_set_wlan_state(dev, val, enable);
  75. mutex_unlock(&dev->hw_atomic_mutex);
  76. }
  77. static void mt7601u_reset_csr_bbp(struct mt7601u_dev *dev)
  78. {
  79. mt7601u_wr(dev, MT_MAC_SYS_CTRL, (MT_MAC_SYS_CTRL_RESET_CSR |
  80. MT_MAC_SYS_CTRL_RESET_BBP));
  81. mt7601u_wr(dev, MT_USB_DMA_CFG, 0);
  82. msleep(1);
  83. mt7601u_wr(dev, MT_MAC_SYS_CTRL, 0);
  84. }
  85. static void mt7601u_init_usb_dma(struct mt7601u_dev *dev)
  86. {
  87. u32 val;
  88. val = MT76_SET(MT_USB_DMA_CFG_RX_BULK_AGG_TOUT, MT_USB_AGGR_TIMEOUT) |
  89. MT76_SET(MT_USB_DMA_CFG_RX_BULK_AGG_LMT, MT_USB_AGGR_SIZE_LIMIT) |
  90. MT_USB_DMA_CFG_RX_BULK_EN |
  91. MT_USB_DMA_CFG_TX_BULK_EN;
  92. if (dev->in_max_packet == 512)
  93. val |= MT_USB_DMA_CFG_RX_BULK_AGG_EN;
  94. mt7601u_wr(dev, MT_USB_DMA_CFG, val);
  95. val |= MT_USB_DMA_CFG_UDMA_RX_WL_DROP;
  96. mt7601u_wr(dev, MT_USB_DMA_CFG, val);
  97. val &= ~MT_USB_DMA_CFG_UDMA_RX_WL_DROP;
  98. mt7601u_wr(dev, MT_USB_DMA_CFG, val);
  99. }
  100. static int mt7601u_init_bbp(struct mt7601u_dev *dev)
  101. {
  102. int ret;
  103. ret = mt7601u_wait_bbp_ready(dev);
  104. if (ret)
  105. return ret;
  106. ret = mt7601u_write_reg_pairs(dev, MT_MCU_MEMMAP_BBP, bbp_common_vals,
  107. ARRAY_SIZE(bbp_common_vals));
  108. if (ret)
  109. return ret;
  110. return mt7601u_write_reg_pairs(dev, MT_MCU_MEMMAP_BBP, bbp_chip_vals,
  111. ARRAY_SIZE(bbp_chip_vals));
  112. }
  113. static void
  114. mt76_init_beacon_offsets(struct mt7601u_dev *dev)
  115. {
  116. u16 base = MT_BEACON_BASE;
  117. u32 regs[4] = {};
  118. int i;
  119. for (i = 0; i < 16; i++) {
  120. u16 addr = dev->beacon_offsets[i];
  121. regs[i / 4] |= ((addr - base) / 64) << (8 * (i % 4));
  122. }
  123. for (i = 0; i < 4; i++)
  124. mt7601u_wr(dev, MT_BCN_OFFSET(i), regs[i]);
  125. }
  126. static int mt7601u_write_mac_initvals(struct mt7601u_dev *dev)
  127. {
  128. int ret;
  129. ret = mt7601u_write_reg_pairs(dev, MT_MCU_MEMMAP_WLAN, mac_common_vals,
  130. ARRAY_SIZE(mac_common_vals));
  131. if (ret)
  132. return ret;
  133. ret = mt7601u_write_reg_pairs(dev, MT_MCU_MEMMAP_WLAN,
  134. mac_chip_vals, ARRAY_SIZE(mac_chip_vals));
  135. if (ret)
  136. return ret;
  137. mt76_init_beacon_offsets(dev);
  138. mt7601u_wr(dev, MT_AUX_CLK_CFG, 0);
  139. return 0;
  140. }
  141. static int mt7601u_init_wcid_mem(struct mt7601u_dev *dev)
  142. {
  143. u32 *vals;
  144. int i, ret;
  145. vals = kmalloc(sizeof(*vals) * N_WCIDS * 2, GFP_KERNEL);
  146. if (!vals)
  147. return -ENOMEM;
  148. for (i = 0; i < N_WCIDS; i++) {
  149. vals[i * 2] = 0xffffffff;
  150. vals[i * 2 + 1] = 0x00ffffff;
  151. }
  152. ret = mt7601u_burst_write_regs(dev, MT_WCID_ADDR_BASE,
  153. vals, N_WCIDS * 2);
  154. kfree(vals);
  155. return ret;
  156. }
  157. static int mt7601u_init_key_mem(struct mt7601u_dev *dev)
  158. {
  159. u32 vals[4] = {};
  160. return mt7601u_burst_write_regs(dev, MT_SKEY_MODE_BASE_0,
  161. vals, ARRAY_SIZE(vals));
  162. }
  163. static int mt7601u_init_wcid_attr_mem(struct mt7601u_dev *dev)
  164. {
  165. u32 *vals;
  166. int i, ret;
  167. vals = kmalloc(sizeof(*vals) * N_WCIDS * 2, GFP_KERNEL);
  168. if (!vals)
  169. return -ENOMEM;
  170. for (i = 0; i < N_WCIDS * 2; i++)
  171. vals[i] = 1;
  172. ret = mt7601u_burst_write_regs(dev, MT_WCID_ATTR_BASE,
  173. vals, N_WCIDS * 2);
  174. kfree(vals);
  175. return ret;
  176. }
  177. static void mt7601u_reset_counters(struct mt7601u_dev *dev)
  178. {
  179. mt7601u_rr(dev, MT_RX_STA_CNT0);
  180. mt7601u_rr(dev, MT_RX_STA_CNT1);
  181. mt7601u_rr(dev, MT_RX_STA_CNT2);
  182. mt7601u_rr(dev, MT_TX_STA_CNT0);
  183. mt7601u_rr(dev, MT_TX_STA_CNT1);
  184. mt7601u_rr(dev, MT_TX_STA_CNT2);
  185. }
  186. int mt7601u_mac_start(struct mt7601u_dev *dev)
  187. {
  188. mt7601u_wr(dev, MT_MAC_SYS_CTRL, MT_MAC_SYS_CTRL_ENABLE_TX);
  189. if (!mt76_poll(dev, MT_WPDMA_GLO_CFG, MT_WPDMA_GLO_CFG_TX_DMA_BUSY |
  190. MT_WPDMA_GLO_CFG_RX_DMA_BUSY, 0, 200000))
  191. return -ETIMEDOUT;
  192. dev->rxfilter = MT_RX_FILTR_CFG_CRC_ERR |
  193. MT_RX_FILTR_CFG_PHY_ERR | MT_RX_FILTR_CFG_PROMISC |
  194. MT_RX_FILTR_CFG_VER_ERR | MT_RX_FILTR_CFG_DUP |
  195. MT_RX_FILTR_CFG_CFACK | MT_RX_FILTR_CFG_CFEND |
  196. MT_RX_FILTR_CFG_ACK | MT_RX_FILTR_CFG_CTS |
  197. MT_RX_FILTR_CFG_RTS | MT_RX_FILTR_CFG_PSPOLL |
  198. MT_RX_FILTR_CFG_BA | MT_RX_FILTR_CFG_CTRL_RSV;
  199. mt7601u_wr(dev, MT_RX_FILTR_CFG, dev->rxfilter);
  200. mt7601u_wr(dev, MT_MAC_SYS_CTRL,
  201. MT_MAC_SYS_CTRL_ENABLE_TX | MT_MAC_SYS_CTRL_ENABLE_RX);
  202. if (!mt76_poll(dev, MT_WPDMA_GLO_CFG, MT_WPDMA_GLO_CFG_TX_DMA_BUSY |
  203. MT_WPDMA_GLO_CFG_RX_DMA_BUSY, 0, 50))
  204. return -ETIMEDOUT;
  205. return 0;
  206. }
  207. static void mt7601u_mac_stop_hw(struct mt7601u_dev *dev)
  208. {
  209. int i, ok;
  210. if (test_bit(MT7601U_STATE_REMOVED, &dev->state))
  211. return;
  212. mt76_clear(dev, MT_BEACON_TIME_CFG, MT_BEACON_TIME_CFG_TIMER_EN |
  213. MT_BEACON_TIME_CFG_SYNC_MODE | MT_BEACON_TIME_CFG_TBTT_EN |
  214. MT_BEACON_TIME_CFG_BEACON_TX);
  215. if (!mt76_poll(dev, MT_USB_DMA_CFG, MT_USB_DMA_CFG_TX_BUSY, 0, 1000))
  216. dev_warn(dev->dev, "Warning: TX DMA did not stop!\n");
  217. /* Page count on TxQ */
  218. i = 200;
  219. while (i-- && ((mt76_rr(dev, 0x0438) & 0xffffffff) ||
  220. (mt76_rr(dev, 0x0a30) & 0x000000ff) ||
  221. (mt76_rr(dev, 0x0a34) & 0x00ff00ff)))
  222. msleep(10);
  223. if (!mt76_poll(dev, MT_MAC_STATUS, MT_MAC_STATUS_TX, 0, 1000))
  224. dev_warn(dev->dev, "Warning: MAC TX did not stop!\n");
  225. mt76_clear(dev, MT_MAC_SYS_CTRL, MT_MAC_SYS_CTRL_ENABLE_RX |
  226. MT_MAC_SYS_CTRL_ENABLE_TX);
  227. /* Page count on RxQ */
  228. ok = 0;
  229. i = 200;
  230. while (i--) {
  231. if ((mt76_rr(dev, 0x0430) & 0x00ff0000) ||
  232. (mt76_rr(dev, 0x0a30) & 0xffffffff) ||
  233. (mt76_rr(dev, 0x0a34) & 0xffffffff))
  234. ok++;
  235. if (ok > 6)
  236. break;
  237. msleep(1);
  238. }
  239. if (!mt76_poll(dev, MT_MAC_STATUS, MT_MAC_STATUS_RX, 0, 1000))
  240. dev_warn(dev->dev, "Warning: MAC RX did not stop!\n");
  241. if (!mt76_poll(dev, MT_USB_DMA_CFG, MT_USB_DMA_CFG_RX_BUSY, 0, 1000))
  242. dev_warn(dev->dev, "Warning: RX DMA did not stop!\n");
  243. }
  244. void mt7601u_mac_stop(struct mt7601u_dev *dev)
  245. {
  246. mt7601u_mac_stop_hw(dev);
  247. flush_delayed_work(&dev->stat_work);
  248. cancel_delayed_work_sync(&dev->stat_work);
  249. }
  250. static void mt7601u_stop_hardware(struct mt7601u_dev *dev)
  251. {
  252. mt7601u_chip_onoff(dev, false, false);
  253. }
  254. int mt7601u_init_hardware(struct mt7601u_dev *dev)
  255. {
  256. static const u16 beacon_offsets[16] = {
  257. /* 512 byte per beacon */
  258. 0xc000, 0xc200, 0xc400, 0xc600,
  259. 0xc800, 0xca00, 0xcc00, 0xce00,
  260. 0xd000, 0xd200, 0xd400, 0xd600,
  261. 0xd800, 0xda00, 0xdc00, 0xde00
  262. };
  263. int ret;
  264. dev->beacon_offsets = beacon_offsets;
  265. mt7601u_chip_onoff(dev, true, false);
  266. ret = mt7601u_wait_asic_ready(dev);
  267. if (ret)
  268. goto err;
  269. ret = mt7601u_mcu_init(dev);
  270. if (ret)
  271. goto err;
  272. if (!mt76_poll_msec(dev, MT_WPDMA_GLO_CFG,
  273. MT_WPDMA_GLO_CFG_TX_DMA_BUSY |
  274. MT_WPDMA_GLO_CFG_RX_DMA_BUSY, 0, 100)) {
  275. ret = -EIO;
  276. goto err;
  277. }
  278. /* Wait for ASIC ready after FW load. */
  279. ret = mt7601u_wait_asic_ready(dev);
  280. if (ret)
  281. goto err;
  282. mt7601u_reset_csr_bbp(dev);
  283. mt7601u_init_usb_dma(dev);
  284. ret = mt7601u_mcu_cmd_init(dev);
  285. if (ret)
  286. goto err;
  287. ret = mt7601u_dma_init(dev);
  288. if (ret)
  289. goto err_mcu;
  290. ret = mt7601u_write_mac_initvals(dev);
  291. if (ret)
  292. goto err_rx;
  293. if (!mt76_poll_msec(dev, MT_MAC_STATUS,
  294. MT_MAC_STATUS_TX | MT_MAC_STATUS_RX, 0, 100)) {
  295. ret = -EIO;
  296. goto err_rx;
  297. }
  298. ret = mt7601u_init_bbp(dev);
  299. if (ret)
  300. goto err_rx;
  301. ret = mt7601u_init_wcid_mem(dev);
  302. if (ret)
  303. goto err_rx;
  304. ret = mt7601u_init_key_mem(dev);
  305. if (ret)
  306. goto err_rx;
  307. ret = mt7601u_init_wcid_attr_mem(dev);
  308. if (ret)
  309. goto err_rx;
  310. mt76_clear(dev, MT_BEACON_TIME_CFG, (MT_BEACON_TIME_CFG_TIMER_EN |
  311. MT_BEACON_TIME_CFG_SYNC_MODE |
  312. MT_BEACON_TIME_CFG_TBTT_EN |
  313. MT_BEACON_TIME_CFG_BEACON_TX));
  314. mt7601u_reset_counters(dev);
  315. mt7601u_rmw(dev, MT_US_CYC_CFG, MT_US_CYC_CNT, 0x1e);
  316. mt7601u_wr(dev, MT_TXOP_CTRL_CFG, MT76_SET(MT_TXOP_TRUN_EN, 0x3f) |
  317. MT76_SET(MT_TXOP_EXT_CCA_DLY, 0x58));
  318. ret = mt7601u_eeprom_init(dev);
  319. if (ret)
  320. goto err_rx;
  321. ret = mt7601u_phy_init(dev);
  322. if (ret)
  323. goto err_rx;
  324. mt7601u_set_rx_path(dev, 0);
  325. mt7601u_set_tx_dac(dev, 0);
  326. mt7601u_mac_set_ctrlch(dev, false);
  327. mt7601u_bbp_set_ctrlch(dev, false);
  328. mt7601u_bbp_set_bw(dev, MT_BW_20);
  329. return 0;
  330. err_rx:
  331. mt7601u_dma_cleanup(dev);
  332. err_mcu:
  333. mt7601u_mcu_cmd_deinit(dev);
  334. err:
  335. mt7601u_chip_onoff(dev, false, false);
  336. return ret;
  337. }
  338. void mt7601u_cleanup(struct mt7601u_dev *dev)
  339. {
  340. if (!test_and_clear_bit(MT7601U_STATE_INITIALIZED, &dev->state))
  341. return;
  342. mt7601u_stop_hardware(dev);
  343. mt7601u_dma_cleanup(dev);
  344. mt7601u_mcu_cmd_deinit(dev);
  345. }
  346. struct mt7601u_dev *mt7601u_alloc_device(struct device *pdev)
  347. {
  348. struct ieee80211_hw *hw;
  349. struct mt7601u_dev *dev;
  350. hw = ieee80211_alloc_hw(sizeof(*dev), &mt7601u_ops);
  351. if (!hw)
  352. return NULL;
  353. dev = hw->priv;
  354. dev->dev = pdev;
  355. dev->hw = hw;
  356. mutex_init(&dev->vendor_req_mutex);
  357. mutex_init(&dev->reg_atomic_mutex);
  358. mutex_init(&dev->hw_atomic_mutex);
  359. mutex_init(&dev->mutex);
  360. spin_lock_init(&dev->tx_lock);
  361. spin_lock_init(&dev->rx_lock);
  362. spin_lock_init(&dev->lock);
  363. spin_lock_init(&dev->mac_lock);
  364. spin_lock_init(&dev->con_mon_lock);
  365. atomic_set(&dev->avg_ampdu_len, 1);
  366. skb_queue_head_init(&dev->tx_skb_done);
  367. dev->stat_wq = alloc_workqueue("mt7601u", WQ_UNBOUND, 0);
  368. if (!dev->stat_wq) {
  369. ieee80211_free_hw(hw);
  370. return NULL;
  371. }
  372. return dev;
  373. }
  374. #define CHAN2G(_idx, _freq) { \
  375. .band = IEEE80211_BAND_2GHZ, \
  376. .center_freq = (_freq), \
  377. .hw_value = (_idx), \
  378. .max_power = 30, \
  379. }
  380. static const struct ieee80211_channel mt76_channels_2ghz[] = {
  381. CHAN2G(1, 2412),
  382. CHAN2G(2, 2417),
  383. CHAN2G(3, 2422),
  384. CHAN2G(4, 2427),
  385. CHAN2G(5, 2432),
  386. CHAN2G(6, 2437),
  387. CHAN2G(7, 2442),
  388. CHAN2G(8, 2447),
  389. CHAN2G(9, 2452),
  390. CHAN2G(10, 2457),
  391. CHAN2G(11, 2462),
  392. CHAN2G(12, 2467),
  393. CHAN2G(13, 2472),
  394. CHAN2G(14, 2484),
  395. };
  396. #define CCK_RATE(_idx, _rate) { \
  397. .bitrate = _rate, \
  398. .flags = IEEE80211_RATE_SHORT_PREAMBLE, \
  399. .hw_value = (MT_PHY_TYPE_CCK << 8) | _idx, \
  400. .hw_value_short = (MT_PHY_TYPE_CCK << 8) | (8 + _idx), \
  401. }
  402. #define OFDM_RATE(_idx, _rate) { \
  403. .bitrate = _rate, \
  404. .hw_value = (MT_PHY_TYPE_OFDM << 8) | _idx, \
  405. .hw_value_short = (MT_PHY_TYPE_OFDM << 8) | _idx, \
  406. }
  407. static struct ieee80211_rate mt76_rates[] = {
  408. CCK_RATE(0, 10),
  409. CCK_RATE(1, 20),
  410. CCK_RATE(2, 55),
  411. CCK_RATE(3, 110),
  412. OFDM_RATE(0, 60),
  413. OFDM_RATE(1, 90),
  414. OFDM_RATE(2, 120),
  415. OFDM_RATE(3, 180),
  416. OFDM_RATE(4, 240),
  417. OFDM_RATE(5, 360),
  418. OFDM_RATE(6, 480),
  419. OFDM_RATE(7, 540),
  420. };
  421. static int
  422. mt76_init_sband(struct mt7601u_dev *dev, struct ieee80211_supported_band *sband,
  423. const struct ieee80211_channel *chan, int n_chan,
  424. struct ieee80211_rate *rates, int n_rates)
  425. {
  426. struct ieee80211_sta_ht_cap *ht_cap;
  427. void *chanlist;
  428. int size;
  429. size = n_chan * sizeof(*chan);
  430. chanlist = devm_kmemdup(dev->dev, chan, size, GFP_KERNEL);
  431. if (!chanlist)
  432. return -ENOMEM;
  433. sband->channels = chanlist;
  434. sband->n_channels = n_chan;
  435. sband->bitrates = rates;
  436. sband->n_bitrates = n_rates;
  437. ht_cap = &sband->ht_cap;
  438. ht_cap->ht_supported = true;
  439. ht_cap->cap = IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
  440. IEEE80211_HT_CAP_GRN_FLD |
  441. IEEE80211_HT_CAP_SGI_20 |
  442. IEEE80211_HT_CAP_SGI_40 |
  443. (1 << IEEE80211_HT_CAP_RX_STBC_SHIFT);
  444. ht_cap->mcs.rx_mask[0] = 0xff;
  445. ht_cap->mcs.rx_mask[4] = 0x1;
  446. ht_cap->mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
  447. ht_cap->ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
  448. ht_cap->ampdu_density = IEEE80211_HT_MPDU_DENSITY_2;
  449. dev->chandef.chan = &sband->channels[0];
  450. return 0;
  451. }
  452. static int
  453. mt76_init_sband_2g(struct mt7601u_dev *dev)
  454. {
  455. dev->sband_2g = devm_kzalloc(dev->dev, sizeof(*dev->sband_2g),
  456. GFP_KERNEL);
  457. dev->hw->wiphy->bands[IEEE80211_BAND_2GHZ] = dev->sband_2g;
  458. WARN_ON(dev->ee->reg.start - 1 + dev->ee->reg.num >
  459. ARRAY_SIZE(mt76_channels_2ghz));
  460. return mt76_init_sband(dev, dev->sband_2g,
  461. &mt76_channels_2ghz[dev->ee->reg.start - 1],
  462. dev->ee->reg.num,
  463. mt76_rates, ARRAY_SIZE(mt76_rates));
  464. }
  465. int mt7601u_register_device(struct mt7601u_dev *dev)
  466. {
  467. struct ieee80211_hw *hw = dev->hw;
  468. struct wiphy *wiphy = hw->wiphy;
  469. int ret;
  470. /* Reserve WCID 0 for mcast - thanks to this APs WCID will go to
  471. * entry no. 1 like it does in the vendor driver.
  472. */
  473. dev->wcid_mask[0] |= 1;
  474. /* init fake wcid for monitor interfaces */
  475. dev->mon_wcid = devm_kmalloc(dev->dev, sizeof(*dev->mon_wcid),
  476. GFP_KERNEL);
  477. if (!dev->mon_wcid)
  478. return -ENOMEM;
  479. dev->mon_wcid->idx = 0xff;
  480. dev->mon_wcid->hw_key_idx = -1;
  481. SET_IEEE80211_DEV(hw, dev->dev);
  482. hw->queues = 4;
  483. ieee80211_hw_set(hw, SIGNAL_DBM);
  484. ieee80211_hw_set(hw, PS_NULLFUNC_STACK);
  485. ieee80211_hw_set(hw, SUPPORTS_HT_CCK_RATES);
  486. ieee80211_hw_set(hw, AMPDU_AGGREGATION);
  487. ieee80211_hw_set(hw, SUPPORTS_RC_TABLE);
  488. hw->max_rates = 1;
  489. hw->max_report_rates = 7;
  490. hw->max_rate_tries = 1;
  491. hw->sta_data_size = sizeof(struct mt76_sta);
  492. hw->vif_data_size = sizeof(struct mt76_vif);
  493. SET_IEEE80211_PERM_ADDR(hw, dev->macaddr);
  494. wiphy->features |= NL80211_FEATURE_ACTIVE_MONITOR;
  495. wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION);
  496. ret = mt76_init_sband_2g(dev);
  497. if (ret)
  498. return ret;
  499. INIT_DELAYED_WORK(&dev->mac_work, mt7601u_mac_work);
  500. INIT_DELAYED_WORK(&dev->stat_work, mt7601u_tx_stat);
  501. ret = ieee80211_register_hw(hw);
  502. if (ret)
  503. return ret;
  504. mt7601u_init_debugfs(dev);
  505. return 0;
  506. }