2 * Copyright (C) 2016 Felix Fietkau <nbd@nbd.name>
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 #include <linux/device.h>
21 #include <linux/dma-mapping.h>
22 #include <linux/spinlock.h>
23 #include <linux/skbuff.h>
24 #include <linux/netdevice.h>
25 #include <linux/irq.h>
26 #include <linux/interrupt.h>
27 #include <linux/mutex.h>
28 #include <linux/bitops.h>
29 #include <linux/kfifo.h>
31 #define MT7662_FIRMWARE "mt7662.bin"
32 #define MT7662_ROM_PATCH "mt7662_rom_patch.bin"
33 #define MT7662_EEPROM_SIZE 512
35 #define MT76x2_RX_RING_SIZE 256
36 #define MT_RX_HEADROOM 32
38 #define MT_MAX_CHAINS 2
40 #define MT_CALIBRATE_INTERVAL HZ
43 #define MT_VIF_WCID(_n) (254 - ((_n) & 7))
46 #include "mt76x2_regs.h"
47 #include "mt76x2_mac.h"
48 #include "mt76x2_dfs.h"
53 wait_queue_head_t wait;
54 struct sk_buff_head res_q;
59 struct mt76x2_rx_freq_cal {
60 s8 high_gain[MT_MAX_CHAINS];
61 s8 rssi_offset[MT_MAX_CHAINS];
66 struct mt76x2_calibration {
67 struct mt76x2_rx_freq_cal rx;
69 u8 agc_gain_init[MT_MAX_CHAINS];
70 u8 agc_gain_cur[MT_MAX_CHAINS];
72 int avg_rssi[MT_MAX_CHAINS];
82 bool tssi_comp_pending;
84 bool channel_cal_done;
88 struct mt76_dev mt76; /* must be first */
90 struct mac_address macaddr_list[8];
94 const u16 *beacon_offsets;
95 unsigned long wcid_mask[128 / BITS_PER_LONG];
101 DECLARE_KFIFO_PTR(txstatus_fifo, struct mt76x2_tx_status);
103 struct mt76x2_mcu mcu;
104 struct sk_buff *rx_head;
106 struct tasklet_struct tx_tasklet;
107 struct tasklet_struct pre_tbtt_tasklet;
108 struct delayed_work cal_work;
109 struct delayed_work mac_work;
113 struct mt76_wcid global_wcid;
114 struct mt76_wcid __rcu *wcid[128];
119 struct sk_buff *beacons[8];
127 struct mt76x2_calibration cal;
130 s8 target_power_delta[2];
131 struct mt76_rate_power rate_power;
137 struct mt76x2_dfs_pattern_detector dfs_pd;
143 struct mt76_wcid group_wcid;
147 struct mt76_wcid wcid; /* must be first */
149 struct mt76x2_vif *vif;
150 struct mt76x2_tx_status status;
154 static inline bool is_mt7612(struct mt76x2_dev *dev)
156 return mt76_chip(&dev->mt76) == 0x7612;
159 void mt76x2_set_irq_mask(struct mt76x2_dev *dev, u32 clear, u32 set);
161 static inline void mt76x2_irq_enable(struct mt76x2_dev *dev, u32 mask)
163 mt76x2_set_irq_mask(dev, 0, mask);
166 static inline void mt76x2_irq_disable(struct mt76x2_dev *dev, u32 mask)
168 mt76x2_set_irq_mask(dev, mask, 0);
171 extern const struct ieee80211_ops mt76x2_ops;
173 struct mt76x2_dev *mt76x2_alloc_device(struct device *pdev);
174 int mt76x2_register_device(struct mt76x2_dev *dev);
175 void mt76x2_init_debugfs(struct mt76x2_dev *dev);
177 irqreturn_t mt76x2_irq_handler(int irq, void *dev_instance);
178 void mt76x2_phy_power_on(struct mt76x2_dev *dev);
179 int mt76x2_init_hardware(struct mt76x2_dev *dev);
180 void mt76x2_stop_hardware(struct mt76x2_dev *dev);
181 int mt76x2_eeprom_init(struct mt76x2_dev *dev);
182 int mt76x2_apply_calibration_data(struct mt76x2_dev *dev, int channel);
183 void mt76x2_set_tx_ackto(struct mt76x2_dev *dev);
185 void mt76x2_phy_set_antenna(struct mt76x2_dev *dev);
186 int mt76x2_phy_start(struct mt76x2_dev *dev);
187 int mt76x2_phy_set_channel(struct mt76x2_dev *dev,
188 struct cfg80211_chan_def *chandef);
189 int mt76x2_phy_get_rssi(struct mt76x2_dev *dev, s8 rssi, int chain);
190 void mt76x2_phy_calibrate(struct work_struct *work);
191 void mt76x2_phy_set_txpower(struct mt76x2_dev *dev);
193 int mt76x2_mcu_init(struct mt76x2_dev *dev);
194 int mt76x2_mcu_set_channel(struct mt76x2_dev *dev, u8 channel, u8 bw,
195 u8 bw_index, bool scan);
196 int mt76x2_mcu_set_radio_state(struct mt76x2_dev *dev, bool on);
197 int mt76x2_mcu_load_cr(struct mt76x2_dev *dev, u8 type, u8 temp_level,
199 int mt76x2_mcu_cleanup(struct mt76x2_dev *dev);
201 int mt76x2_dma_init(struct mt76x2_dev *dev);
202 void mt76x2_dma_cleanup(struct mt76x2_dev *dev);
204 void mt76x2_cleanup(struct mt76x2_dev *dev);
206 int mt76x2_tx_queue_mcu(struct mt76x2_dev *dev, enum mt76_txq_id qid,
207 struct sk_buff *skb, int cmd, int seq);
208 void mt76x2_tx(struct ieee80211_hw *hw, struct ieee80211_tx_control *control,
209 struct sk_buff *skb);
210 void mt76x2_tx_complete(struct mt76x2_dev *dev, struct sk_buff *skb);
211 int mt76x2_tx_prepare_skb(struct mt76_dev *mdev, void *txwi,
212 struct sk_buff *skb, struct mt76_queue *q,
213 struct mt76_wcid *wcid, struct ieee80211_sta *sta,
215 void mt76x2_tx_complete_skb(struct mt76_dev *mdev, struct mt76_queue *q,
216 struct mt76_queue_entry *e, bool flush);
218 void mt76x2_pre_tbtt_tasklet(unsigned long arg);
220 void mt76x2_rx_poll_complete(struct mt76_dev *mdev, enum mt76_rxq_id q);
221 void mt76x2_queue_rx_skb(struct mt76_dev *mdev, enum mt76_rxq_id q,
222 struct sk_buff *skb);
224 void mt76x2_sta_ps(struct mt76_dev *dev, struct ieee80211_sta *sta, bool ps);
226 void mt76x2_update_channel(struct mt76_dev *mdev);
228 s8 mt76x2_tx_get_max_txpwr_adj(struct mt76x2_dev *dev,
229 const struct ieee80211_tx_rate *rate);
230 s8 mt76x2_tx_get_txpwr_adj(struct mt76x2_dev *dev, s8 txpwr, s8 max_txpwr_adj);
231 void mt76x2_tx_set_txpwr_auto(struct mt76x2_dev *dev, s8 txpwr);