2 * Copyright (C) 2014 Felix Fietkau <nbd@openwrt.org>
3 * Copyright (C) 2015 Jakub Kicinski <kubakici@wp.pl>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2
7 * as published by the Free Software Foundation
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
18 #include <linux/kernel.h>
19 #include <linux/device.h>
20 #include <linux/mutex.h>
21 #include <linux/usb.h>
22 #include <linux/completion.h>
23 #include <net/mac80211.h>
24 #include <linux/debugfs.h>
29 #define MT_CALIBRATE_INTERVAL (4 * HZ)
31 #define MT_FREQ_CAL_INIT_DELAY (30 * HZ)
32 #define MT_FREQ_CAL_CHECK_INTERVAL (10 * HZ)
33 #define MT_FREQ_CAL_ADJ_INTERVAL (HZ / 2)
35 #define MT_BBP_REG_VERSION 0x00
37 #define MT_USB_AGGR_SIZE_LIMIT 28 /* * 1024B */
38 #define MT_USB_AGGR_TIMEOUT 0x80 /* * 33ns */
40 #define MT_RX_URB_SIZE (PAGE_SIZE << MT_RX_ORDER)
42 struct mt7601u_dma_buf
{
54 struct mt7601u_dma_buf resp
;
55 struct completion resp_cmpl
;
58 struct mt7601u_freq_cal
{
59 struct delayed_work work
;
73 #define N_RX_ENTRIES 16
74 struct mt7601u_rx_queue
{
75 struct mt7601u_dev
*dev
;
77 struct mt7601u_dma_buf_rx
{
88 #define N_TX_ENTRIES 64
90 struct mt7601u_tx_queue
{
91 struct mt7601u_dev
*dev
;
93 struct mt7601u_dma_buf_tx
{
100 unsigned int entries
;
102 unsigned int fifo_seq
;
113 #define GROUP_WCID(idx) (N_WCIDS - 2 - idx)
115 struct mt7601u_eeprom_params
;
117 #define MT_EE_TEMPERATURE_SLOPE 39
118 #define MT_FREQ_OFFSET_INVALID -128
132 MT7601U_STATE_INITIALIZED
,
133 MT7601U_STATE_REMOVED
,
134 MT7601U_STATE_WLAN_RUNNING
,
135 MT7601U_STATE_MCU_RUNNING
,
136 MT7601U_STATE_SCANNING
,
137 MT7601U_STATE_READING_STATS
,
138 MT7601U_STATE_MORE_STATS
,
142 * struct mt7601u_dev - adapter structure
143 * @lock: protects @wcid->tx_rate.
144 * @mac_lock: locks out mac80211's tx status and rx paths.
145 * @tx_lock: protects @tx_q and changes of MT7601U_STATE_*_STATS
147 * @rx_lock: protects @rx_q.
148 * @con_mon_lock: protects @ap_bssid, @bcn_*, @avg_rssi.
149 * @mutex: ensures exclusive access from mac80211 callbacks.
150 * @vendor_req_mutex: protects @vend_buf, ensures atomicity of split writes.
151 * @reg_atomic_mutex: ensures atomicity of indirect register accesses
152 * (accesses to RF and BBP).
153 * @hw_atomic_mutex: ensures exclusive access to HW during critical
154 * operations (power management, channel switch).
157 struct ieee80211_hw
*hw
;
164 unsigned long wcid_mask
[N_WCIDS
/ BITS_PER_LONG
];
166 struct cfg80211_chan_def chandef
;
167 struct ieee80211_supported_band
*sband_2g
;
169 struct mt7601u_mcu mcu
;
171 struct delayed_work cal_work
;
172 struct delayed_work mac_work
;
174 struct workqueue_struct
*stat_wq
;
175 struct delayed_work stat_work
;
177 struct mt76_wcid
*mon_wcid
;
178 struct mt76_wcid __rcu
*wcid
[N_WCIDS
];
183 const u16
*beacon_offsets
;
185 u8 macaddr
[ETH_ALEN
];
186 struct mt7601u_eeprom_params
*ee
;
188 struct mutex vendor_req_mutex
;
191 struct mutex reg_atomic_mutex
;
192 struct mutex hw_atomic_mutex
;
204 struct tasklet_struct tx_tasklet
;
205 struct mt7601u_tx_queue
*tx_q
;
206 struct sk_buff_head tx_skb_done
;
208 atomic_t avg_ampdu_len
;
212 struct tasklet_struct rx_tasklet
;
213 struct mt7601u_rx_queue rx_q
;
215 /* Connection monitoring things */
216 spinlock_t con_mon_lock
;
217 u8 ap_bssid
[ETH_ALEN
];
222 int avg_rssi
; /* starts at 0 and converges */
226 struct mt7601u_freq_cal freq_cal
;
232 s16 tssi_init_hvga_offset_db
;
236 enum mt_temp_mode temp_mode
;
240 bool pll_lock_protect
;
248 struct mac_stats stats
;
251 struct mt7601u_tssi_params
{
268 struct mt76_wcid group_wcid
;
272 struct mt76_wcid wcid
;
273 u16 agg_ssn
[IEEE80211_NUM_TIDS
];
276 struct mt76_reg_pair
{
283 extern const struct ieee80211_ops mt7601u_ops
;
285 void mt7601u_init_debugfs(struct mt7601u_dev
*dev
);
287 u32
mt7601u_rr(struct mt7601u_dev
*dev
, u32 offset
);
288 void mt7601u_wr(struct mt7601u_dev
*dev
, u32 offset
, u32 val
);
289 u32
mt7601u_rmw(struct mt7601u_dev
*dev
, u32 offset
, u32 mask
, u32 val
);
290 u32
mt7601u_rmc(struct mt7601u_dev
*dev
, u32 offset
, u32 mask
, u32 val
);
291 void mt7601u_wr_copy(struct mt7601u_dev
*dev
, u32 offset
,
292 const void *data
, int len
);
294 int mt7601u_wait_asic_ready(struct mt7601u_dev
*dev
);
295 bool mt76_poll(struct mt7601u_dev
*dev
, u32 offset
, u32 mask
, u32 val
,
297 bool mt76_poll_msec(struct mt7601u_dev
*dev
, u32 offset
, u32 mask
, u32 val
,
300 /* Compatibility with mt76 */
301 #define mt76_rmw_field(_dev, _reg, _field, _val) \
302 mt76_rmw(_dev, _reg, _field, MT76_SET(_field, _val))
304 static inline u32
mt76_rr(struct mt7601u_dev
*dev
, u32 offset
)
306 return mt7601u_rr(dev
, offset
);
309 static inline void mt76_wr(struct mt7601u_dev
*dev
, u32 offset
, u32 val
)
311 return mt7601u_wr(dev
, offset
, val
);
315 mt76_rmw(struct mt7601u_dev
*dev
, u32 offset
, u32 mask
, u32 val
)
317 return mt7601u_rmw(dev
, offset
, mask
, val
);
320 static inline u32
mt76_set(struct mt7601u_dev
*dev
, u32 offset
, u32 val
)
322 return mt76_rmw(dev
, offset
, 0, val
);
325 static inline u32
mt76_clear(struct mt7601u_dev
*dev
, u32 offset
, u32 val
)
327 return mt76_rmw(dev
, offset
, val
, 0);
330 int mt7601u_write_reg_pairs(struct mt7601u_dev
*dev
, u32 base
,
331 const struct mt76_reg_pair
*data
, int len
);
332 int mt7601u_burst_write_regs(struct mt7601u_dev
*dev
, u32 offset
,
333 const u32
*data
, int n
);
334 void mt7601u_addr_wr(struct mt7601u_dev
*dev
, const u32 offset
, const u8
*addr
);
337 struct mt7601u_dev
*mt7601u_alloc_device(struct device
*dev
);
338 int mt7601u_init_hardware(struct mt7601u_dev
*dev
);
339 int mt7601u_register_device(struct mt7601u_dev
*dev
);
340 void mt7601u_cleanup(struct mt7601u_dev
*dev
);
342 int mt7601u_mac_start(struct mt7601u_dev
*dev
);
343 void mt7601u_mac_stop(struct mt7601u_dev
*dev
);
346 int mt7601u_phy_init(struct mt7601u_dev
*dev
);
347 int mt7601u_wait_bbp_ready(struct mt7601u_dev
*dev
);
348 void mt7601u_set_rx_path(struct mt7601u_dev
*dev
, u8 path
);
349 void mt7601u_set_tx_dac(struct mt7601u_dev
*dev
, u8 path
);
350 int mt7601u_bbp_set_bw(struct mt7601u_dev
*dev
, int bw
);
351 void mt7601u_agc_save(struct mt7601u_dev
*dev
);
352 void mt7601u_agc_restore(struct mt7601u_dev
*dev
);
353 int mt7601u_phy_set_channel(struct mt7601u_dev
*dev
,
354 struct cfg80211_chan_def
*chandef
);
355 void mt7601u_phy_recalibrate_after_assoc(struct mt7601u_dev
*dev
);
356 int mt7601u_phy_get_rssi(struct mt7601u_dev
*dev
,
357 struct mt7601u_rxwi
*rxwi
, u16 rate
);
358 void mt7601u_phy_con_cal_onoff(struct mt7601u_dev
*dev
,
359 struct ieee80211_bss_conf
*info
);
362 void mt7601u_mac_work(struct work_struct
*work
);
363 void mt7601u_mac_set_protection(struct mt7601u_dev
*dev
, bool legacy_prot
,
365 void mt7601u_mac_set_short_preamble(struct mt7601u_dev
*dev
, bool short_preamb
);
366 void mt7601u_mac_config_tsf(struct mt7601u_dev
*dev
, bool enable
, int interval
);
368 mt7601u_mac_wcid_setup(struct mt7601u_dev
*dev
, u8 idx
, u8 vif_idx
, u8
*mac
);
369 void mt7601u_mac_set_ampdu_factor(struct mt7601u_dev
*dev
);
372 void mt7601u_tx(struct ieee80211_hw
*hw
, struct ieee80211_tx_control
*control
,
373 struct sk_buff
*skb
);
374 int mt7601u_conf_tx(struct ieee80211_hw
*hw
, struct ieee80211_vif
*vif
,
375 u16 queue
, const struct ieee80211_tx_queue_params
*params
);
376 void mt7601u_tx_status(struct mt7601u_dev
*dev
, struct sk_buff
*skb
);
377 void mt7601u_tx_stat(struct work_struct
*work
);
380 void mt76_remove_hdr_pad(struct sk_buff
*skb
);
381 int mt76_insert_hdr_pad(struct sk_buff
*skb
);
383 u32
mt7601u_bbp_set_ctrlch(struct mt7601u_dev
*dev
, bool below
);
385 static inline u32
mt7601u_mac_set_ctrlch(struct mt7601u_dev
*dev
, bool below
)
387 return mt7601u_rmc(dev
, MT_TX_BAND_CFG
, 1, below
);
390 int mt7601u_dma_init(struct mt7601u_dev
*dev
);
391 void mt7601u_dma_cleanup(struct mt7601u_dev
*dev
);
393 int mt7601u_dma_enqueue_tx(struct mt7601u_dev
*dev
, struct sk_buff
*skb
,
394 struct mt76_wcid
*wcid
, int hw_q
);