xref: /openwifi/driver/sdr.c (revision 2ee67178825ee52f380c2f72b7135d15ddadca60)
1 //Author: Xianjun Jiao. [email protected]; [email protected]
2 
3 #include <linux/bitops.h>
4 #include <linux/dmapool.h>
5 #include <linux/io.h>
6 #include <linux/iopoll.h>
7 #include <linux/of_address.h>
8 #include <linux/of_platform.h>
9 #include <linux/of_irq.h>
10 #include <linux/slab.h>
11 #include <linux/clk.h>
12 #include <linux/io-64-nonatomic-lo-hi.h>
13 
14 #include <linux/delay.h>
15 #include <linux/interrupt.h>
16 
17 #include <linux/dmaengine.h>
18 #include <linux/slab.h>
19 #include <linux/delay.h>
20 #include <linux/etherdevice.h>
21 
22 #include <linux/init.h>
23 #include <linux/kthread.h>
24 #include <linux/module.h>
25 #include <linux/of_dma.h>
26 #include <linux/platform_device.h>
27 #include <linux/random.h>
28 #include <linux/slab.h>
29 #include <linux/wait.h>
30 #include <linux/sched/task.h>
31 #include <linux/dma/xilinx_dma.h>
32 #include <linux/spi/spi.h>
33 #include <net/mac80211.h>
34 
35 #include <linux/clk.h>
36 #include <linux/clkdev.h>
37 #include <linux/clk-provider.h>
38 
39 #include <linux/iio/iio.h>
40 #include <linux/iio/sysfs.h>
41 
42 #include <linux/gpio.h>
43 #include <linux/leds.h>
44 
45 #define IIO_AD9361_USE_PRIVATE_H_
46 #include "ad9361/ad9361_regs.h"
47 #include "ad9361/ad9361.h"
48 #include "ad9361/ad9361_private.h"
49 
50 #include <../../drivers/iio/frequency/cf_axi_dds.h>
51 
52 #include "../user_space/sdrctl_src/nl80211_testmode_def.h"
53 #include "hw_def.h"
54 #include "sdr.h"
55 
56 // driver API of component driver
57 extern struct tx_intf_driver_api *tx_intf_api;
58 extern struct rx_intf_driver_api *rx_intf_api;
59 extern struct openofdm_tx_driver_api *openofdm_tx_api;
60 extern struct openofdm_rx_driver_api *openofdm_rx_api;
61 extern struct xpu_driver_api *xpu_api;
62 
63 static int test_mode = 0; // 0 normal; 1 rx test
64 
65 MODULE_AUTHOR("Xianjun Jiao");
66 MODULE_DESCRIPTION("SDR driver");
67 MODULE_LICENSE("GPL v2");
68 
69 module_param(test_mode, int, 0);
70 MODULE_PARM_DESC(myint, "test_mode. 0 normal; 1 rx test");
71 
72 // ---------------rfkill---------------------------------------
73 static bool openwifi_is_radio_enabled(struct openwifi_priv *priv)
74 {
75 	int reg;
76 
77 	if (priv->tx_intf_cfg == TX_INTF_BW_20MHZ_AT_N_10MHZ_ANT1)
78 		reg = ad9361_get_tx_atten(priv->ad9361_phy, 2);
79 	else
80 		reg = ad9361_get_tx_atten(priv->ad9361_phy, 1);
81 
82 	if (reg == AD9361_RADIO_ON_TX_ATT)
83 		return true;// 0 off, 1 on
84 	return false;
85 }
86 
87 void openwifi_rfkill_init(struct ieee80211_hw *hw)
88 {
89 	struct openwifi_priv *priv = hw->priv;
90 
91 	priv->rfkill_off = openwifi_is_radio_enabled(priv);
92 	printk("%s openwifi_rfkill_init: wireless switch is %s\n", sdr_compatible_str, priv->rfkill_off ? "on" : "off");
93 	wiphy_rfkill_set_hw_state(hw->wiphy, !priv->rfkill_off);
94 	wiphy_rfkill_start_polling(hw->wiphy);
95 }
96 
97 void openwifi_rfkill_poll(struct ieee80211_hw *hw)
98 {
99 	bool enabled;
100 	struct openwifi_priv *priv = hw->priv;
101 
102 	enabled = openwifi_is_radio_enabled(priv);
103 	printk("%s openwifi_rfkill_poll: wireless radio switch turned %s\n", sdr_compatible_str, enabled ? "on" : "off");
104 	if (unlikely(enabled != priv->rfkill_off)) {
105 		priv->rfkill_off = enabled;
106 		printk("%s openwifi_rfkill_poll: WARNING wireless radio switch turned %s\n", sdr_compatible_str, enabled ? "on" : "off");
107 		wiphy_rfkill_set_hw_state(hw->wiphy, !enabled);
108 	}
109 }
110 
111 void openwifi_rfkill_exit(struct ieee80211_hw *hw)
112 {
113 	printk("%s openwifi_rfkill_exit\n", sdr_compatible_str);
114 	wiphy_rfkill_stop_polling(hw->wiphy);
115 }
116 //----------------rfkill end-----------------------------------
117 
118 //static void ad9361_rf_init(void);
119 //static void ad9361_rf_stop(void);
120 //static void ad9361_rf_calc_rssi(void);
121 static void ad9361_rf_set_channel(struct ieee80211_hw *dev,
122 				  struct ieee80211_conf *conf)
123 {
124 	struct openwifi_priv *priv = dev->priv;
125 	u32 actual_rx_lo = conf->chandef.chan->center_freq - priv->rx_freq_offset_to_lo_MHz;
126 	u32 actual_tx_lo;
127 	bool change_flag = (actual_rx_lo != priv->actual_rx_lo);
128 
129 	if (change_flag) {
130 		priv->actual_rx_lo = actual_rx_lo;
131 
132 		actual_tx_lo = conf->chandef.chan->center_freq - priv->tx_freq_offset_to_lo_MHz;
133 
134 		ad9361_clk_set_rate(priv->ad9361_phy->clks[RX_RFPLL], ( ((u64)1000000ull)*((u64)actual_rx_lo )>>1) );
135 		ad9361_clk_set_rate(priv->ad9361_phy->clks[TX_RFPLL], ( ((u64)1000000ull)*((u64)actual_tx_lo )>>1) );
136 
137 		if (actual_rx_lo<2412) {
138 			priv->rssi_correction = 153;
139 		} else if (actual_rx_lo<=2484) {
140 			priv->rssi_correction = 153;
141 		} else if (actual_rx_lo<5160) {
142 			priv->rssi_correction = 153;
143 		} else if (actual_rx_lo<=5240) {
144 			priv->rssi_correction = 145;
145 		} else if (actual_rx_lo<=5320) {
146 			priv->rssi_correction = 148;
147 		} else {
148 			priv->rssi_correction = 148;
149 		}
150 		xpu_api->XPU_REG_LBT_TH_write((priv->rssi_correction-62)<<1);
151 
152 		if (actual_rx_lo < 2500) {
153 			//priv->slot_time = 20; //20 is default slot time in ERP(OFDM)/11g 2.4G; short one is 9.
154 			//xpu_api->XPU_REG_BAND_CHANNEL_write(BAND_2_4GHZ<<16);
155 			if (priv->band != BAND_2_4GHZ) {
156 				priv->band = BAND_2_4GHZ;
157 				xpu_api->XPU_REG_BAND_CHANNEL_write( (priv->use_short_slot<<24)|(priv->band<<16) );
158 			}
159 			// //xpu_api->XPU_REG_RECV_ACK_COUNT_TOP_write( (((45+2)*200)<<16) | 200 ); // high 16 bits to cover sig valid of ACK packet, low 16 bits is adjustment of fcs valid waiting time.  let's add 2us for those device that is really "slow"!
160 			// xpu_api->XPU_REG_RECV_ACK_COUNT_TOP_write( (((45+2+2)*200)<<16) | 200 );//add 2us for longer fir. BUT corrding to FPGA probing test, we do not need this
161 			// xpu_api->XPU_REG_SEND_ACK_WAIT_TOP_write( 0 );
162 			// tx_intf_api->TX_INTF_REG_CTS_TOSELF_WAIT_SIFS_TOP_write(((10)*200)<<16);
163 		}
164 		else {
165 			//priv->slot_time = 9; //default slot time of OFDM PHY (OFDM by default means 5GHz)
166 			// xpu_api->XPU_REG_BAND_CHANNEL_write(BAND_5_8GHZ<<16);
167 			if (priv->band != BAND_5_8GHZ) {
168 				priv->band = BAND_5_8GHZ;
169 				xpu_api->XPU_REG_BAND_CHANNEL_write( (priv->use_short_slot<<24)|(priv->band<<16) );
170 			}
171 			// //xpu_api->XPU_REG_RECV_ACK_COUNT_TOP_write( (((51+2)*200)<<16) | 200 ); // because 5GHz needs longer SIFS (16 instead of 10), we need 58 instead of 48 for XPU low mac setting.  let's add 2us for those device that is really "slow"!
172 			// xpu_api->XPU_REG_RECV_ACK_COUNT_TOP_write( (((51+2+2)*200)<<16) | 200 );//add 2us for longer fir.  BUT corrding to FPGA probing test, we do not need this
173 			// //xpu_api->XPU_REG_SEND_ACK_WAIT_TOP_write( 1200 );
174 			// xpu_api->XPU_REG_SEND_ACK_WAIT_TOP_write( 1000 );// for longer fir we need this delay 1us shorter
175 			// tx_intf_api->TX_INTF_REG_CTS_TOSELF_WAIT_SIFS_TOP_write(((16)*200)<<16);
176 		}
177 		//printk("%s ad9361_rf_set_channel %dM rssi_correction %d\n", sdr_compatible_str,conf->chandef.chan->center_freq,priv->rssi_correction);
178 		// //-- use less
179 		//clk_prepare_enable(priv->ad9361_phy->clks[RX_RFPLL]);
180 		//printk("%s ad9361_rf_set_channel tune to %d read back %llu\n", sdr_compatible_str,conf->chandef.chan->center_freq,2*priv->ad9361_phy->state->current_rx_lo_freq);
181 		//ad9361_set_trx_clock_chain_default(priv->ad9361_phy);
182 		//printk("%s ad9361_rf_set_channel tune to %d read back %llu\n", sdr_compatible_str,conf->chandef.chan->center_freq,2*priv->ad9361_phy->state->current_rx_lo_freq);
183 	}
184 	printk("%s ad9361_rf_set_channel %dM rssi_correction %d (change flag %d)\n", sdr_compatible_str,conf->chandef.chan->center_freq,priv->rssi_correction,change_flag);
185 }
186 
187 const struct openwifi_rf_ops ad9361_rf_ops = {
188 	.name		= "ad9361",
189 //	.init		= ad9361_rf_init,
190 //	.stop		= ad9361_rf_stop,
191 	.set_chan	= ad9361_rf_set_channel,
192 //	.calc_rssi	= ad9361_rf_calc_rssi,
193 };
194 
195 u16 reverse16(u16 d) {
196 	union u16_byte2 tmp0, tmp1;
197 	tmp0.a = d;
198 	tmp1.c[0] = tmp0.c[1];
199 	tmp1.c[1] = tmp0.c[0];
200 	return(tmp1.a);
201 }
202 
203 u32 reverse32(u32 d) {
204 	union u32_byte4 tmp0, tmp1;
205 	tmp0.a = d;
206 	tmp1.c[0] = tmp0.c[3];
207 	tmp1.c[1] = tmp0.c[2];
208 	tmp1.c[2] = tmp0.c[1];
209 	tmp1.c[3] = tmp0.c[0];
210 	return(tmp1.a);
211 }
212 
213 static int openwifi_init_tx_ring(struct openwifi_priv *priv)
214 {
215 	struct openwifi_ring *ring = &(priv->tx_ring);
216 	int i;
217 
218 	priv->tx_queue_stopped = false;
219 	ring->bd_wr_idx = 0;
220 	ring->bd_rd_idx = 0;
221 	ring->bds = kmalloc(sizeof(struct openwifi_buffer_descriptor)*NUM_TX_BD,GFP_KERNEL);
222 	if (ring->bds==NULL) {
223 		printk("%s openwifi_init_tx_ring: WARNING Cannot allocate TX ring\n",sdr_compatible_str);
224 		return -ENOMEM;
225 	}
226 
227 	for (i = 0; i < NUM_TX_BD; i++) {
228 		ring->bds[i].num_dma_byte=0;
229 		ring->bds[i].sn=0;
230 		ring->bds[i].hw_queue_idx=0;
231 		ring->bds[i].retry_limit=0;
232 		ring->bds[i].need_ack=0;
233 
234 		ring->bds[i].skb_linked=0; // for tx, skb is from upper layer
235 		//at frist right after skb allocated, head, data, tail are the same.
236 		ring->bds[i].dma_mapping_addr = 0; // for tx, mapping is done after skb is received from uppler layer in tx routine
237 	}
238 
239 	return 0;
240 }
241 
242 static void openwifi_free_tx_ring(struct openwifi_priv *priv)
243 {
244 	struct openwifi_ring *ring = &(priv->tx_ring);
245 	int i;
246 
247 	ring->bd_wr_idx = 0;
248 	ring->bd_rd_idx = 0;
249 	for (i = 0; i < NUM_TX_BD; i++) {
250 		ring->bds[i].num_dma_byte=0;
251 		ring->bds[i].sn=0;
252 		ring->bds[i].hw_queue_idx=0;
253 		ring->bds[i].retry_limit=0;
254 		ring->bds[i].need_ack=0;
255 
256 		if (ring->bds[i].skb_linked == 0 && ring->bds[i].dma_mapping_addr == 0)
257 			continue;
258 		if (ring->bds[i].dma_mapping_addr != 0)
259 			dma_unmap_single(priv->tx_chan->device->dev, ring->bds[i].dma_mapping_addr,ring->bds[i].num_dma_byte, DMA_MEM_TO_DEV);
260 //		if (ring->bds[i].skb_linked!=NULL)
261 //			dev_kfree_skb(ring->bds[i].skb_linked);
262 		if ( (ring->bds[i].dma_mapping_addr != 0 && ring->bds[i].skb_linked == 0) ||
263 		     (ring->bds[i].dma_mapping_addr == 0 && ring->bds[i].skb_linked != 0))
264 			printk("%s openwifi_free_tx_ring: WARNING %d skb_linked %08x dma_mapping_addr %08x\n", sdr_compatible_str, i, (u32)(ring->bds[i].skb_linked), ring->bds[i].dma_mapping_addr);
265 
266 		ring->bds[i].skb_linked=0;
267 		ring->bds[i].dma_mapping_addr = 0;
268 	}
269 	if (ring->bds)
270 		kfree(ring->bds);
271 	ring->bds = NULL;
272 }
273 
274 static int openwifi_init_rx_ring(struct openwifi_priv *priv)
275 {
276 	priv->rx_cyclic_buf = dma_alloc_coherent(priv->rx_chan->device->dev,RX_BD_BUF_SIZE*NUM_RX_BD,&priv->rx_cyclic_buf_dma_mapping_addr,GFP_KERNEL);
277 	if (!priv->rx_cyclic_buf) {
278 		printk("%s openwifi_init_rx_ring: WARNING dma_alloc_coherent failed!\n", sdr_compatible_str);
279 		dma_free_coherent(priv->rx_chan->device->dev,RX_BD_BUF_SIZE*NUM_RX_BD,priv->rx_cyclic_buf,priv->rx_cyclic_buf_dma_mapping_addr);
280 		return(-1);
281 	}
282 	return 0;
283 }
284 
285 static void openwifi_free_rx_ring(struct openwifi_priv *priv)
286 {
287 	if (priv->rx_cyclic_buf)
288 		dma_free_coherent(priv->rx_chan->device->dev,RX_BD_BUF_SIZE*NUM_RX_BD,priv->rx_cyclic_buf,priv->rx_cyclic_buf_dma_mapping_addr);
289 
290 	priv->rx_cyclic_buf_dma_mapping_addr = 0;
291 	priv->rx_cyclic_buf = 0;
292 }
293 
294 static int rx_dma_setup(struct ieee80211_hw *dev){
295 	struct openwifi_priv *priv = dev->priv;
296 	struct dma_device *rx_dev = priv->rx_chan->device;
297 
298 	priv->rxd = rx_dev->device_prep_dma_cyclic(priv->rx_chan,priv->rx_cyclic_buf_dma_mapping_addr,RX_BD_BUF_SIZE*NUM_RX_BD,RX_BD_BUF_SIZE,DMA_DEV_TO_MEM,DMA_CTRL_ACK|DMA_PREP_INTERRUPT);
299 	if (!(priv->rxd)) {
300 		openwifi_free_rx_ring(priv);
301 		printk("%s rx_dma_setup: WARNING rx_dev->device_prep_dma_cyclic %d\n", sdr_compatible_str, (u32)(priv->rxd));
302 		return(-1);
303 	}
304 	priv->rxd->callback = 0;
305 	priv->rxd->callback_param = 0;
306 
307 	priv->rx_cookie = priv->rxd->tx_submit(priv->rxd);
308 
309 	if (dma_submit_error(priv->rx_cookie)) {
310 		printk("%s rx_dma_setup: WARNING dma_submit_error(rx_cookie) %d\n", sdr_compatible_str, (u32)(priv->rx_cookie));
311 		return(-1);
312 	}
313 
314 	dma_async_issue_pending(priv->rx_chan);
315 	return(0);
316 }
317 
318 static irqreturn_t openwifi_rx_interrupt(int irq, void *dev_id)
319 {
320 	struct ieee80211_hw *dev = dev_id;
321 	struct openwifi_priv *priv = dev->priv;
322 	struct ieee80211_rx_status rx_status = {0};
323 	struct sk_buff *skb;
324 	struct ieee80211_hdr *hdr;
325 	u32 addr1_low32=0, addr2_low32=0, addr3_low32=0, len, rate_idx, ht_flag, tsft_low, tsft_high;//, fc_di;
326 	u32 dma_driver_buf_idx_mod;
327 	u8 *pdata_tmp, fcs_ok, phy_rx_sn_hw, target_buf_idx;
328 	s8 signal;
329 	u16 rssi_val, addr1_high16=0, addr2_high16=0, addr3_high16=0, sc=0;
330 	bool content_ok = false;
331 	struct dma_tx_state state;
332 	static u8 target_buf_idx_old = 0xFF;
333 
334 	spin_lock(&priv->lock);
335 	priv->rx_chan->device->device_tx_status(priv->rx_chan,priv->rx_cookie,&state);
336 	target_buf_idx = ((state.residue-1)&(NUM_RX_BD-1));
337 	if (target_buf_idx==target_buf_idx_old) {
338 		//printk("%s openwifi_rx_interrupt: WARNING same idx %d\n", sdr_compatible_str,target_buf_idx);
339 		goto openwifi_rx_interrupt_out;
340 	}
341 	if ( ((target_buf_idx-target_buf_idx_old)&(NUM_RX_BD-1))!=1 )
342 		printk("%s openwifi_rx_interrupt: WARNING jump idx target %d old %d diff %02x\n", sdr_compatible_str,target_buf_idx,target_buf_idx_old,((target_buf_idx-target_buf_idx_old)&(NUM_RX_BD-1)));
343 	target_buf_idx_old = target_buf_idx;
344 
345 	pdata_tmp = priv->rx_cyclic_buf + target_buf_idx*RX_BD_BUF_SIZE; // our header insertion is at the beginning
346 	tsft_low =     (*((u32*)(pdata_tmp+0 )));
347 	tsft_high =    (*((u32*)(pdata_tmp+4 )));
348 	rssi_val =     (*((u16*)(pdata_tmp+8 )));
349 	len =          (*((u16*)(pdata_tmp+12)));
350 	//len_new = (((len>>3) + ((len&0x7)!=0))<<3);
351 	rate_idx =     (*((u16*)(pdata_tmp+14)));
352 
353 	// fc_di =        (*((u32*)(pdata_tmp+16)));
354 	// addr1_high16 = (*((u16*)(pdata_tmp+16+4)));
355 	// addr1_low32  = (*((u32*)(pdata_tmp+16+4+2)));
356 	// addr2_high16 = (*((u16*)(pdata_tmp+16+6+4)));
357 	// addr2_low32  = (*((u32*)(pdata_tmp+16+6+4+2)));
358 	// addr3_high16 = (*((u16*)(pdata_tmp+16+12+4)));
359 	// addr3_low32  = (*((u32*)(pdata_tmp+16+12+4+2)));
360 	hdr = (struct ieee80211_hdr *)(pdata_tmp+16);
361 	addr1_low32  = *((u32*)(hdr->addr1+2));
362 	addr1_high16 = *((u16*)(hdr->addr1));
363 	if (len>=20) {
364 		addr2_low32  = *((u32*)(hdr->addr2+2));
365 		addr2_high16 = *((u16*)(hdr->addr2));
366 	}
367 	if (len>=26) {
368 		addr3_low32  = *((u32*)(hdr->addr3+2));
369 		addr3_high16 = *((u16*)(hdr->addr3));
370 	}
371 	if (len>=28)
372 		sc = hdr->seq_ctrl;
373 
374 	fcs_ok =       (*(( u8*)(pdata_tmp+16+len-1)));
375 
376 	phy_rx_sn_hw = (fcs_ok&0x7f);//0x7f is FPGA limitation
377 	dma_driver_buf_idx_mod = (state.residue&0x7f);
378 	//phy_rx_sn_hw = (fcs_ok&(NUM_RX_BD-1));
379 	fcs_ok = ((fcs_ok&0x80)!=0);
380 	ht_flag = ((rate_idx&0x10)!=0);
381 	rate_idx = (rate_idx&0xF);
382 
383 	if ( (len>=14 && len<=8191) && (rate_idx>=8 && rate_idx<=15)) {
384 		// if ( phy_rx_sn_hw!=dma_driver_buf_idx_mod) {
385 		// 	printk("%s openwifi_rx_interrupt: WARNING sn %d next buf_idx %d!\n", sdr_compatible_str,phy_rx_sn_hw,dma_driver_buf_idx_mod);
386 		// }
387 		content_ok = true;
388 	} else {
389 		printk("%s openwifi_rx_interrupt: WARNING content!\n", sdr_compatible_str);
390 		content_ok = false;
391 	}
392 
393 	rssi_val = (rssi_val>>1);
394 	if ( (rssi_val+128)<priv->rssi_correction )
395 		signal = -128;
396 	else
397 		signal = rssi_val - priv->rssi_correction;
398 	if (addr1_low32!=0xffffffff && addr1_high16!=0xffff)
399 	printk("%s openwifi_rx_interrupt:%4dbytes ht%d %2dM FC%04x DI%04x addr1/2/3:%04x%08x/%04x%08x/%04x%08x SC%04x fcs%d sn%d i%d %ddBm\n", sdr_compatible_str,
400 	len, ht_flag, wifi_rate_table[rate_idx], hdr->frame_control,hdr->duration_id,
401 	reverse16(addr1_high16), reverse32(addr1_low32), reverse16(addr2_high16), reverse32(addr2_low32), reverse16(addr3_high16), reverse32(addr3_low32),
402 	sc,fcs_ok, phy_rx_sn_hw,dma_driver_buf_idx_mod,signal);
403 
404 	// priv->phy_rx_sn_hw_old = phy_rx_sn_hw;
405 	if (content_ok) {
406 		skb = dev_alloc_skb(len);
407 		if (skb) {
408 			skb_put_data(skb,pdata_tmp+16,len);
409 
410 			rx_status.antenna = 0;
411 			// def in ieee80211_rate openwifi_rates 0~11. 0~3 11b(1M~11M), 4~11 11a/g(6M~54M)
412 			rx_status.rate_idx = wifi_rate_table_mapping[rate_idx];
413 			rx_status.signal = signal;
414 			rx_status.freq = dev->conf.chandef.chan->center_freq;
415 			rx_status.band = dev->conf.chandef.chan->band;
416 			rx_status.mactime = ( ( (u64)tsft_low ) | ( ((u64)tsft_high)<<32 ) );
417 			rx_status.flag |= RX_FLAG_MACTIME_START;
418 			if (!fcs_ok)
419 				rx_status.flag |= RX_FLAG_FAILED_FCS_CRC;
420 			rx_status.encoding = RX_ENC_LEGACY;
421 			rx_status.bw = RATE_INFO_BW_20;
422 
423 			memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status)); // put rx_status into skb->cb, from now on skb->cb is not dma_dsts any more.
424 			ieee80211_rx_irqsafe(dev, skb); // call mac80211 function
425 		} else
426 			printk("%s openwifi_rx_interrupt: WARNING skb!\n", sdr_compatible_str);
427 	}
428 openwifi_rx_interrupt_out:
429 	spin_unlock(&priv->lock);
430 	return IRQ_HANDLED;
431 }
432 
433 static irqreturn_t openwifi_tx_interrupt(int irq, void *dev_id)
434 {
435 	struct ieee80211_hw *dev = dev_id;
436 	struct openwifi_priv *priv = dev->priv;
437 	struct openwifi_ring *ring = &(priv->tx_ring);
438 	struct sk_buff *skb;
439 	struct ieee80211_tx_info *info;
440 	u32 reg_val,ring_len, ring_room_left, just_wr_idx, current_rd_idx; //queue_idx_hw, ;
441 	u32 num_dma_byte_hw;
442 	u32 phy_tx_sn_hw;
443 	u8 tx_result;
444 
445 	spin_lock(&priv->lock);
446 
447 	tx_result = xpu_api->XPU_REG_TX_RESULT_read();
448 	reg_val = tx_intf_api->TX_INTF_REG_PKT_INFO_read();// current interrupt is the end of phy_tx_sn_hw pkt transmitting.
449 	num_dma_byte_hw = (reg_val&0xFFFF);
450 	phy_tx_sn_hw = ((reg_val>>16)&MAX_PHY_TX_SN);
451 	//queue_idx_hw = (reg_val&(MAX_NUM_HW_QUEUE-1));
452 
453 	//just_wr_idx = (ring->bd_wr_idx==0?(NUM_TX_BD-1):(ring->bd_wr_idx-1));
454 	just_wr_idx = ((ring->bd_wr_idx-1)&(NUM_TX_BD-1));
455 	while(1) {
456 		current_rd_idx = ring->bd_rd_idx;
457 
458 		dma_unmap_single(priv->tx_chan->device->dev,ring->bds[current_rd_idx].dma_mapping_addr,
459 				ring->bds[current_rd_idx].num_dma_byte, DMA_MEM_TO_DEV);
460 
461 		if (phy_tx_sn_hw != ring->bds[current_rd_idx].sn) {
462 			ring->bd_rd_idx = ((ring->bd_rd_idx+1)&(NUM_TX_BD-1));
463 			if (current_rd_idx == just_wr_idx) {
464 				printk("%s openwifi_tx_interrupt: WARNING can not find hw sn %d in driver! curr rd %d just wr %d\n", sdr_compatible_str,phy_tx_sn_hw,current_rd_idx,just_wr_idx);
465 				break;
466 			} else
467 				continue;
468 		}
469 
470 		// a know bd has just been sent to the air
471 		if (num_dma_byte_hw!=ring->bds[current_rd_idx].num_dma_byte) {
472 			ring->bd_rd_idx = ((ring->bd_rd_idx+1)&(NUM_TX_BD-1));
473 			printk("%s openwifi_tx_interrupt: WARNING num_dma_byte is different %d VS %d at sn %d curr rd %d just wr %d\n", sdr_compatible_str,num_dma_byte_hw,ring->bds[current_rd_idx].num_dma_byte,phy_tx_sn_hw,current_rd_idx,just_wr_idx);
474 			if (current_rd_idx == just_wr_idx)
475 				break;
476 			else
477 				continue;
478 		}
479 
480 		// num_dma_byte_hw is correct
481 		skb = ring->bds[current_rd_idx].skb_linked;
482 		// dma_buf = skb->data;
483 		//phy_tx_sn_skb = (*((u16*)(dma_buf+6)));
484 		//num_dma_byte_skb = (*((u32*)(dma_buf+8)));
485 		//num_byte_pad_skb = (*((u32*)(dma_buf+12)));
486 
487 		//if ( phy_tx_sn_hw!=phy_tx_sn_entry || phy_tx_sn_hw!=phy_tx_sn_skb || phy_tx_sn_entry!=phy_tx_sn_skb )
488 		//	printk("%s openwifi_tx_interrupt: WARNING hw/entry/skb num byte %d/%d/%d pkt sn %d/%d/%d pad %d\n", sdr_compatible_str,
489 		//	num_dma_byte_hw, num_dma_byte_entry, num_dma_byte_skb, phy_tx_sn_hw, phy_tx_sn_entry, phy_tx_sn_skb, num_byte_pad_skb);
490 
491 		skb_pull(skb, LEN_PHY_HEADER);
492 		//skb_trim(skb, num_byte_pad_skb);
493 		info = IEEE80211_SKB_CB(skb);
494 		ieee80211_tx_info_clear_status(info);
495 
496 		if ( !(info->flags & IEEE80211_TX_CTL_NO_ACK) ) {
497 			if ((tx_result&0x10)==0)
498 				info->flags |= IEEE80211_TX_STAT_ACK;
499 
500 			// printk("%s openwifi_tx_interrupt: rate&try: %d %d %03x; %d %d %03x; %d %d %03x; %d %d %03x\n", sdr_compatible_str,
501 			// 	info->status.rates[0].idx,info->status.rates[0].count,info->status.rates[0].flags,
502 			// 	info->status.rates[1].idx,info->status.rates[1].count,info->status.rates[1].flags,
503 			// 	info->status.rates[2].idx,info->status.rates[2].count,info->status.rates[2].flags,
504 			// 	info->status.rates[3].idx,info->status.rates[3].count,info->status.rates[3].flags);
505 		}
506 
507 		info->status.rates[0].count = (tx_result&0xF) + 1; //according to our test, the 1st rate is the most important. we only do retry on the 1st rate
508 		info->status.rates[1].idx = -1;
509 		info->status.rates[2].idx = -1;
510 		info->status.rates[3].idx = -1;//in mac80211.h: #define IEEE80211_TX_MAX_RATES	4
511 		if (tx_result&0x10)
512 			printk("%s openwifi_tx_interrupt: WARNING tx_result %02x phy_tx_sn_hw %d. curr rd %d just wr %d\n", sdr_compatible_str,tx_result,phy_tx_sn_hw,current_rd_idx,just_wr_idx);
513 
514 		ieee80211_tx_status_irqsafe(dev, skb);
515 		//ring_len = (just_wr_idx>=current_rd_idx)?(just_wr_idx-current_rd_idx):(just_wr_idx+NUM_TX_BD-current_rd_idx);
516 		ring_len = ((just_wr_idx-current_rd_idx)&(NUM_TX_BD-1));
517 		ring_room_left = NUM_TX_BD - ring_len;
518 		if (ring_room_left > 2 && priv->tx_queue_stopped) {
519 			unsigned int prio = skb_get_queue_mapping(skb);
520 			ieee80211_wake_queue(dev, prio);
521 			printk("%s openwifi_tx_interrupt: WARNING ieee80211_wake_queue. ring_room_left %d prio %d curr rd %d just wr %d\n", sdr_compatible_str,ring_room_left,prio,current_rd_idx,just_wr_idx);
522 			priv->tx_queue_stopped = false;
523 		}
524 
525 		ring->bd_rd_idx = ((ring->bd_rd_idx+1)&(NUM_TX_BD-1));
526 
527 		//if (current_rd_idx == just_wr_idx)
528 			break; // we have hit the sn, we should break
529 	}
530 
531 	spin_unlock(&priv->lock);
532 
533 	return IRQ_HANDLED;
534 }
535 
536 u32 gen_parity(u32 v){
537 	v ^= v >> 1;
538 	v ^= v >> 2;
539 	v = (v & 0x11111111U) * 0x11111111U;
540 	return (v >> 28) & 1;
541 }
542 
543 u32 calc_phy_header(u8 rate_hw_value, u32 len, u8 *bytes){
544 	//u32 signal_word = 0 ;
545 	u8  SIG_RATE = 0 ;
546 	u8	len_2to0, len_10to3, len_msb,b0,b1,b2, header_parity ;
547 
548 	// rate_hw_value = (rate_hw_value<=4?0:(rate_hw_value-4));
549 	// SIG_RATE = wifi_mcs_table_phy_tx[rate_hw_value];
550 	SIG_RATE = wifi_mcs_table_11b_force_up[rate_hw_value];
551 
552 	len_2to0 = len & 0x07 ;
553 	len_10to3 = (len >> 3 ) & 0xFF ;
554 	len_msb = (len >> 11) & 0x01 ;
555 
556 	b0=SIG_RATE | (len_2to0 << 5) ;
557 	b1 = len_10to3 ;
558 	header_parity = gen_parity((len_msb << 16)| (b1<<8) | b0) ;
559 	b2 = ( len_msb | (header_parity << 1) ) ;
560 
561 	memset(bytes,0,16);
562 	bytes[0] = b0 ;
563 	bytes[1] = b1 ;
564     bytes[2] = b2;
565 	//signal_word = b0+(b1<<8)+(b2<<16) ;
566 	//return signal_word;
567 	return(SIG_RATE);
568 }
569 
570 static inline struct gpio_led_data * //please align with the implementation in leds-gpio.c
571 			cdev_to_gpio_led_data(struct led_classdev *led_cdev)
572 {
573 	return container_of(led_cdev, struct gpio_led_data, cdev);
574 }
575 
576 static void openwifi_tx(struct ieee80211_hw *dev,
577 		       struct ieee80211_tx_control *control,
578 		       struct sk_buff *skb)
579 {
580 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
581 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
582 	struct openwifi_priv *priv = dev->priv;
583 	struct openwifi_ring *ring = &(priv->tx_ring);
584 	dma_addr_t dma_mapping_addr;
585 	unsigned long flags;
586 	unsigned int prio, i;
587 	u32 num_dma_symbol, len_mac_pdu, num_dma_byte, len_phy_packet, num_byte_pad;
588 	u32 rate_signal_value,rate_hw_value,ack_flag;
589 	u32 pkt_need_ack, addr1_low32=0, addr2_low32=0, addr3_low32=0, queue_idx=2, ring_len, ring_room_left, dma_reg, cts_reg;//, openofdm_state_history;
590 	u16 addr1_high16=0, addr2_high16=0, addr3_high16=0, sc=0, cts_duration=0, cts_rate_hw_value = 0, cts_rate_signal_value=0, sifs, ack_duration=0, traffic_pkt_duration;
591 	u8 fc_flag,fc_type,fc_subtype,retry_limit_raw,*dma_buf,retry_limit_hw_value,rc_flags;
592 	bool use_rts_cts, use_cts_protect, force_use_cts_protect=false, addr_flag, cts_use_traffic_rate;
593 	__le16 frame_control,duration_id;
594 	// static u32 openofdm_state_history_old=0;
595 	// static bool led_status=0;
596 	// struct gpio_led_data *led_dat = cdev_to_gpio_led_data(priv->led[3]);
597 
598 	// if ( (priv->phy_tx_sn&7) ==0 ) {
599 	// 	openofdm_state_history = openofdm_rx_api->OPENOFDM_RX_REG_STATE_HISTORY_read();
600 	// 	if (openofdm_state_history!=openofdm_state_history_old){
601 	// 		led_status = (~led_status);
602 	// 		openofdm_state_history_old = openofdm_state_history;
603 	// 		gpiod_set_value(led_dat->gpiod, led_status);
604 	// 	}
605 	// }
606 
607 	if (test_mode==1){
608 		printk("%s openwifi_tx: test_mode==1\n", sdr_compatible_str);
609 		goto openwifi_tx_early_out;
610 	}
611 	if (skb->data_len>0)// more data are not in linear data area skb->data
612 		goto openwifi_tx_early_out;
613 
614 	len_mac_pdu = skb->len;
615 	len_phy_packet = len_mac_pdu + LEN_PHY_HEADER;
616 	num_dma_symbol = (len_phy_packet>>TX_INTF_NUM_BYTE_PER_DMA_SYMBOL_IN_BITS) + ((len_phy_packet&(TX_INTF_NUM_BYTE_PER_DMA_SYMBOL-1))!=0);
617 	num_dma_byte = (num_dma_symbol<<TX_INTF_NUM_BYTE_PER_DMA_SYMBOL_IN_BITS);
618 	if (num_dma_byte > TX_BD_BUF_SIZE) {
619 		dev_err(priv->tx_chan->device->dev, "WARNING num_dma_byte > TX_BD_BUF_SIZE\n");
620 		goto openwifi_tx_early_out;
621 	}
622 	num_byte_pad = num_dma_byte-len_phy_packet;
623 
624 	// -----------preprocess some info from header and skb----------------
625 	prio = skb_get_queue_mapping(skb);
626 	if (prio) {
627 		printk("%s openwifi_tx: WARNING prio %d\n", sdr_compatible_str, prio);
628 	}
629 
630 	rate_hw_value = ieee80211_get_tx_rate(dev, info)->hw_value;
631 
632 	addr1_low32  = *((u32*)(hdr->addr1+2));
633 	addr1_high16 = *((u16*)(hdr->addr1));
634 	if (len_mac_pdu>=20) {
635 		addr2_low32  = *((u32*)(hdr->addr2+2));
636 		addr2_high16 = *((u16*)(hdr->addr2));
637 	}
638 	if (len_mac_pdu>=26) {
639 		addr3_low32  = *((u32*)(hdr->addr3+2));
640 		addr3_high16 = *((u16*)(hdr->addr3));
641 	}
642 	if (len_mac_pdu>=28)
643 		sc = hdr->seq_ctrl;
644 
645 	duration_id = hdr->duration_id;
646 	frame_control=hdr->frame_control;
647 	ack_flag = (info->flags&IEEE80211_TX_CTL_NO_ACK);
648 	fc_type = ((frame_control)>>2)&3;
649 	fc_subtype = ((frame_control)>>4)&0xf;
650 	fc_flag = ( fc_type==2 || fc_type==0 || (fc_type==1 && (fc_subtype==8 || fc_subtype==9 || fc_subtype==10) ) );
651 	//if it is broadcasting or multicasting addr
652 	addr_flag = ( (addr1_low32==0 && addr1_high16==0) ||
653 	              (addr1_low32==0xFFFFFFFF && addr1_high16==0xFFFF) ||
654 				  (addr1_high16==0x3333) ||
655 				  (addr1_high16==0x0001 && hdr->addr1[2]==0x5E)  );
656 	if ( fc_flag && ( !addr_flag ) && (!ack_flag) ) { // unicast data frame
657 		pkt_need_ack = 1; //FPGA need to wait ACK after this pkt sent
658 	} else {
659 		pkt_need_ack = 0;
660 	}
661 
662 	//rate_hw_value = 10; //4:6M, 5:9M, 6:12M, 7:18M, 8:24M, 9:36M, 10:48M, 11:54M
663 	if (priv->drv_tx_reg_val[0]>0 && fc_type==2 && (!addr_flag))
664 		rate_hw_value = priv->drv_tx_reg_val[0];
665 
666 	// check current packet belonging to which slice/hw-queue
667 	for (i=0; i<MAX_NUM_HW_QUEUE; i++) {
668 		if ( priv->dest_mac_addr_queue_map[i] == addr1_low32 && ( !addr_flag ) ) {
669 			break;
670 		}
671 	}
672 	queue_idx = i;
673 	if (i>=MAX_NUM_HW_QUEUE)
674 		queue_idx = 0;
675 
676 	retry_limit_raw = info->control.rates[0].count;
677 
678 	rc_flags = info->control.rates[0].flags;
679 	use_rts_cts = ((rc_flags&IEEE80211_TX_RC_USE_RTS_CTS)!=0);
680 	use_cts_protect = ((rc_flags&IEEE80211_TX_RC_USE_CTS_PROTECT)!=0);
681 
682 	if (use_rts_cts)
683 		printk("%s openwifi_tx: WARNING use_rts_cts is not supported!\n", sdr_compatible_str);
684 
685 	cts_use_traffic_rate = false;
686 	force_use_cts_protect = false;
687 	if (use_cts_protect) {
688 		cts_rate_hw_value = ieee80211_get_rts_cts_rate(dev, info)->hw_value;
689 		cts_duration = le16_to_cpu(ieee80211_ctstoself_duration(dev,info->control.vif,len_mac_pdu,info));
690 	} else if (force_use_cts_protect) { // could override mac80211 setting here.
691 		cts_rate_hw_value = 4; //wifi_mcs_table_11b_force_up[] translate it to 1011(6M)
692 		sifs = (priv->actual_rx_lo<2500?10:16);
693 		if (pkt_need_ack)
694 			ack_duration = 44;//assume the ack we wait use 6Mbps: 4*ceil((22+14*8)/24) + 20(preamble+SIGNAL)
695 		traffic_pkt_duration = 20 + 4*(((22+len_mac_pdu*8)/wifi_n_dbps_table[rate_hw_value])+1);
696 		cts_duration = traffic_pkt_duration + sifs + pkt_need_ack*(sifs+ack_duration);
697 	}
698 
699 	//if (addr1_low32!=0xffffffff && addr1_high16!=0xffff)
700 	if ( !addr_flag ) {
701 		printk("%s openwifi_tx: %4dbytes %2dM FC%04x DI%04x addr1/2/3:%04x%08x/%04x%08x/%04x%08x SC%04x flag%08x retry%d ack%d q%d sn%04d R/CTS %d%d %dM %dus wr/rd %d/%d\n", sdr_compatible_str,
702 			len_mac_pdu, wifi_rate_all[rate_hw_value],frame_control,duration_id,
703 			reverse16(addr1_high16), reverse32(addr1_low32), reverse16(addr2_high16), reverse32(addr2_low32), reverse16(addr3_high16), reverse32(addr3_low32),
704 			sc,info->flags,retry_limit_raw,pkt_need_ack,queue_idx,priv->phy_tx_sn,
705 			use_rts_cts,use_cts_protect|force_use_cts_protect,wifi_rate_all[cts_rate_hw_value],cts_duration,
706 			ring->bd_wr_idx,ring->bd_rd_idx);
707 		// printk("%s openwifi_tx: rate&try: %d %d %03x; %d %d %03x; %d %d %03x; %d %d %03x\n", sdr_compatible_str,
708 		// 	info->status.rates[0].idx,info->status.rates[0].count,info->status.rates[0].flags,
709 		// 	info->status.rates[1].idx,info->status.rates[1].count,info->status.rates[1].flags,
710 		// 	info->status.rates[2].idx,info->status.rates[2].count,info->status.rates[2].flags,
711 		// 	info->status.rates[3].idx,info->status.rates[3].count,info->status.rates[3].flags);
712 	}
713 
714 // this is 11b stuff
715 //	if (info->flags&IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
716 //		printk("%s openwifi_tx: WARNING IEEE80211_TX_RC_USE_SHORT_PREAMBLE\n", sdr_compatible_str);
717 
718 	if (info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
719 		if (info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT)
720 			priv->seqno += 0x10;
721 		hdr->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
722 		hdr->seq_ctrl |= cpu_to_le16(priv->seqno);
723 	}
724 	// -----------end of preprocess some info from header and skb----------------
725 
726 	// /* HW will perform RTS-CTS when only RTS flags is set.
727 	//  * HW will perform CTS-to-self when both RTS and CTS flags are set.
728 	//  * RTS rate and RTS duration will be used also for CTS-to-self.
729 	//  */
730 	// if (rc_flags & IEEE80211_TX_RC_USE_RTS_CTS) {
731 	// 	tx_flags |= ieee80211_get_rts_cts_rate(dev, info)->hw_value << 19;
732 	// 	rts_duration = ieee80211_rts_duration(dev, priv->vif[0], // assume all vif have the same config
733 	// 					len_mac_pdu, info);
734 	// 	printk("%s openwifi_tx: rc_flags & IEEE80211_TX_RC_USE_RTS_CTS\n", sdr_compatible_str);
735 	// } else if (rc_flags & IEEE80211_TX_RC_USE_CTS_PROTECT) {
736 	// 	tx_flags |= ieee80211_get_rts_cts_rate(dev, info)->hw_value << 19;
737 	// 	rts_duration = ieee80211_ctstoself_duration(dev, priv->vif[0], // assume all vif have the same config
738 	// 					len_mac_pdu, info);
739 	// 	printk("%s openwifi_tx: rc_flags & IEEE80211_TX_RC_USE_CTS_PROTECT\n", sdr_compatible_str);
740 	// }
741 
742 	// when skb does not have enough headroom, skb_push will cause kernel panic. headroom needs to be extended if necessary
743 	if (skb_headroom(skb)<LEN_PHY_HEADER) {
744 		struct sk_buff *skb_new; // in case original skb headroom is not enough to host phy header needed by FPGA IP core
745 		if ((skb_new = skb_realloc_headroom(skb, LEN_PHY_HEADER)) == NULL) {
746 			printk("%s openwifi_tx: WARNING skb_realloc_headroom failed!\n", sdr_compatible_str);
747 			goto openwifi_tx_early_out;
748 		}
749 		if (skb->sk != NULL)
750 			skb_set_owner_w(skb_new, skb->sk);
751 		dev_kfree_skb(skb);
752 		skb = skb_new;
753 	}
754 
755 	skb_push( skb, LEN_PHY_HEADER );
756 	rate_signal_value = calc_phy_header(rate_hw_value, len_mac_pdu+LEN_PHY_CRC, skb->data); //fill the phy header
757 	//make sure dma length is integer times of DDC_NUM_BYTE_PER_DMA_SYMBOL
758 	if (skb_tailroom(skb)<num_byte_pad) {
759 		printk("%s openwifi_tx: WARNING skb_tailroom(skb)<num_byte_pad!\n", sdr_compatible_str);
760 		goto openwifi_tx_early_out;
761 	}
762 	skb_put( skb, num_byte_pad );
763 
764 	retry_limit_hw_value = (retry_limit_raw - 1)&0xF;
765 	dma_buf = skb->data;
766 	//(*((u16*)(dma_buf+6))) = priv->phy_tx_sn;
767 	//(*((u32*)(dma_buf+8))) = num_dma_byte;
768 	//(*((u32*)(dma_buf+12))) = num_byte_pad;
769 
770 	cts_rate_signal_value = wifi_mcs_table_11b_force_up[cts_rate_hw_value];
771 	cts_reg = (((use_cts_protect|force_use_cts_protect)<<31)|(cts_use_traffic_rate<<30)|(cts_duration<<8)|(cts_rate_signal_value<<4)|rate_signal_value);
772 	dma_reg = ( (( ((priv->phy_tx_sn<<NUM_BIT_MAX_NUM_HW_QUEUE)|queue_idx) )<<18)|(retry_limit_hw_value<<14)|(pkt_need_ack<<13)|num_dma_symbol );
773 	spin_lock_irqsave(&priv->lock, flags); // from now on, we'd better avoid interrupt because wr/rd idx will matter
774 
775 	//ring_len = (ring->bd_wr_idx>=ring->bd_rd_idx)?(ring->bd_wr_idx-ring->bd_rd_idx):(ring->bd_wr_idx+NUM_TX_BD-ring->bd_rd_idx);
776 	ring_len = ((ring->bd_wr_idx-ring->bd_rd_idx)&(NUM_TX_BD-1));
777 	ring_room_left = NUM_TX_BD - ring_len;
778 	if (ring_len>12)
779 		printk("%s openwifi_tx: WARNING ring len %d\n", sdr_compatible_str,ring_len);
780 //		printk("%s openwifi_tx: WARNING ring len %d HW fifo %d q %d\n", sdr_compatible_str,ring_len,tx_intf_api->TX_INTF_REG_S_AXIS_FIFO_DATA_COUNT_read()&0xFFFF, ((tx_intf_api->TX_INTF_REG_PHY_QUEUE_TX_SN_read())>>16)&0xFF );
781 
782 	if (ring_room_left <= 2 && priv->tx_queue_stopped == false) {
783 		ieee80211_stop_queue(dev, prio);
784 		printk("%s openwifi_tx: WARNING ieee80211_stop_queue. ring_room_left %d!\n", sdr_compatible_str,ring_room_left);
785 		priv->tx_queue_stopped = true;
786 		spin_unlock_irqrestore(&priv->lock, flags);
787 		goto openwifi_tx_early_out;
788 	}
789 
790 	/* We must be sure that tx_flags is written last because the HW
791 	 * looks at it to check if the rest of data is valid or not
792 	 */
793 	//wmb();
794 	// entry->flags = cpu_to_le32(tx_flags);
795 	/* We must be sure this has been written before followings HW
796 	 * register write, because this write will made the HW attempts
797 	 * to DMA the just-written data
798 	 */
799 	//wmb();
800 
801 	//__skb_queue_tail(&ring->queue, skb);
802 
803 //-------------------------fire skb DMA to hardware----------------------------------
804 	dma_mapping_addr = dma_map_single(priv->tx_chan->device->dev, dma_buf,
805 				 num_dma_byte, DMA_MEM_TO_DEV);
806 
807 	if (dma_mapping_error(priv->tx_chan->device->dev,dma_mapping_addr)) {
808 		dev_err(priv->tx_chan->device->dev, "WARNING TX DMA mapping error\n");
809 		goto openwifi_tx_skb_drop_out;
810 	}
811 
812 	sg_init_table(&(priv->tx_sg), 1);
813 
814 	sg_dma_address( &(priv->tx_sg) ) = dma_mapping_addr;
815 	sg_dma_len( &(priv->tx_sg) ) = num_dma_byte;
816 
817 	tx_intf_api->TX_INTF_REG_CTS_TOSELF_CONFIG_write(cts_reg);
818 	tx_intf_api->TX_INTF_REG_NUM_DMA_SYMBOL_TO_PL_write(dma_reg);
819 	priv->txd = priv->tx_chan->device->device_prep_slave_sg(priv->tx_chan, &(priv->tx_sg),1,DMA_MEM_TO_DEV, DMA_CTRL_ACK | DMA_PREP_INTERRUPT, NULL);
820 	if (!(priv->txd)) {
821 		printk("%s openwifi_tx: WARNING device_prep_slave_sg %d\n", sdr_compatible_str, (u32)(priv->txd));
822 		goto openwifi_tx_after_dma_mapping;
823 	}
824 
825 	//we use interrupt instead of dma callback
826 	priv->txd->callback = 0;
827 	priv->txd->callback_param = 0;
828 	priv->tx_cookie = priv->txd->tx_submit(priv->txd);
829 
830 	if (dma_submit_error(priv->tx_cookie)) {
831 		printk("%s openwifi_tx: WARNING dma_submit_error(tx_cookie) %d\n", sdr_compatible_str, (u32)(priv->tx_cookie));
832 		goto openwifi_tx_after_dma_mapping;
833 	}
834 
835 	// seems everything ok. let's mark this pkt in bd descriptor ring
836 	ring->bds[ring->bd_wr_idx].num_dma_byte=num_dma_byte;
837 	ring->bds[ring->bd_wr_idx].sn=priv->phy_tx_sn;
838 	// ring->bds[ring->bd_wr_idx].hw_queue_idx=queue_idx;
839 	// ring->bds[ring->bd_wr_idx].retry_limit=retry_limit_hw_value;
840 	// ring->bds[ring->bd_wr_idx].need_ack=pkt_need_ack;
841 	ring->bds[ring->bd_wr_idx].skb_linked = skb;
842 	ring->bds[ring->bd_wr_idx].dma_mapping_addr = dma_mapping_addr;
843 
844 	ring->bd_wr_idx = ((ring->bd_wr_idx+1)&(NUM_TX_BD-1));
845 	priv->phy_tx_sn = ( (priv->phy_tx_sn+1)&MAX_PHY_TX_SN );
846 
847 	dma_async_issue_pending(priv->tx_chan);
848 
849 	spin_unlock_irqrestore(&priv->lock, flags);
850 
851 	return;
852 
853 openwifi_tx_after_dma_mapping:
854 	printk("%s openwifi_tx: WARNING openwifi_tx_after_dma_mapping phy_tx_sn %d queue %d\n", sdr_compatible_str,priv->phy_tx_sn,queue_idx);
855 	dma_unmap_single(priv->tx_chan->device->dev, dma_mapping_addr, num_dma_byte, DMA_MEM_TO_DEV);
856 	spin_unlock_irqrestore(&priv->lock, flags);
857 
858 openwifi_tx_skb_drop_out:
859 	printk("%s openwifi_tx: WARNING openwifi_tx_skb_drop_out phy_tx_sn %d queue %d\n", sdr_compatible_str,priv->phy_tx_sn,queue_idx);
860 	spin_unlock_irqrestore(&priv->lock, flags);
861 
862 openwifi_tx_early_out:
863 	dev_kfree_skb(skb);
864 	printk("%s openwifi_tx: WARNING openwifi_tx_early_out phy_tx_sn %d queue %d\n", sdr_compatible_str,priv->phy_tx_sn,queue_idx);
865 }
866 
867 static int openwifi_start(struct ieee80211_hw *dev)
868 {
869 	struct openwifi_priv *priv = dev->priv;
870 	int ret, i, rssi_half_db_offset, agc_gain_delay;//rssi_half_db_th,
871 	u32 reg;
872 
873 	for (i=0; i<MAX_NUM_VIF; i++) {
874 		priv->vif[i] = NULL;
875 	}
876 
877 	//turn on radio
878 	if (priv->tx_intf_cfg == TX_INTF_BW_20MHZ_AT_N_10MHZ_ANT1) {
879 		ad9361_set_tx_atten(priv->ad9361_phy, AD9361_RADIO_ON_TX_ATT, false, true, true); // AD9361_RADIO_ON_TX_ATT 3000 means 3dB, 0 means 0dB
880 		reg = ad9361_get_tx_atten(priv->ad9361_phy, 2);
881 	} else {
882 		ad9361_set_tx_atten(priv->ad9361_phy, AD9361_RADIO_ON_TX_ATT, true, false, true); // AD9361_RADIO_ON_TX_ATT 3000 means 3dB, 0 means 0dB
883 		reg = ad9361_get_tx_atten(priv->ad9361_phy, 1);
884 	}
885 	if (reg == AD9361_RADIO_ON_TX_ATT) {
886 		priv->rfkill_off = 1;// 0 off, 1 on
887 		printk("%s openwifi_start: rfkill radio on\n",sdr_compatible_str);
888 	}
889 	else
890 		printk("%s openwifi_start: WARNING rfkill radio on failed. tx att read %d require %d\n",sdr_compatible_str, reg, AD9361_RADIO_ON_TX_ATT);
891 
892 	if (priv->rx_intf_cfg == RX_INTF_BW_20MHZ_AT_0MHZ_ANT0)
893 		priv->ctrl_out.index=0x16;
894 	else
895 		priv->ctrl_out.index=0x17;
896 
897 	ret = ad9361_ctrl_outs_setup(priv->ad9361_phy, &(priv->ctrl_out));
898 	if (ret < 0) {
899 		printk("%s openwifi_start: WARNING ad9361_ctrl_outs_setup %d\n",sdr_compatible_str, ret);
900 	} else {
901 		printk("%s openwifi_start: ad9361_ctrl_outs_setup en_mask 0x%02x index 0x%02x\n",sdr_compatible_str, priv->ctrl_out.en_mask, priv->ctrl_out.index);
902 	}
903 
904 	priv->rx_freq_offset_to_lo_MHz = rx_intf_fo_mapping[priv->rx_intf_cfg];
905 	priv->tx_freq_offset_to_lo_MHz = tx_intf_fo_mapping[priv->tx_intf_cfg];
906 
907 	rx_intf_api->hw_init(priv->rx_intf_cfg,8,8);
908 	tx_intf_api->hw_init(priv->tx_intf_cfg,8,8);
909 	openofdm_tx_api->hw_init(priv->openofdm_tx_cfg);
910 	openofdm_rx_api->hw_init(priv->openofdm_rx_cfg);
911 	xpu_api->hw_init(priv->xpu_cfg);
912 
913 	agc_gain_delay = 50; //samples
914 	rssi_half_db_offset = 150;
915 	xpu_api->XPU_REG_RSSI_DB_CFG_write(0x80000000|((rssi_half_db_offset<<16)|agc_gain_delay) );
916 	xpu_api->XPU_REG_RSSI_DB_CFG_write((~0x80000000)&((rssi_half_db_offset<<16)|agc_gain_delay) );
917 
918 	openofdm_rx_api->OPENOFDM_RX_REG_POWER_THRES_write(0);
919 	// rssi_half_db_th = 87<<1; // -62dBm // will settup in runtime in _rf_set_channel
920 	// xpu_api->XPU_REG_LBT_TH_write(rssi_half_db_th); // set IQ rssi th step .5dB to xxx and enable it
921 
922 	// // xpu_api->XPU_REG_CSMA_CFG_write(3); // cw_min
923 	// xpu_api->XPU_REG_CSMA_CFG_write(3);
924 
925 	//xpu_api->XPU_REG_SEND_ACK_WAIT_TOP_write( (1200<<16)|0 );//high 16bit 5GHz; low 16 bit 2.4GHz
926 	xpu_api->XPU_REG_SEND_ACK_WAIT_TOP_write( ((1030-238)<<16)|0 );//high 16bit 5GHz; low 16 bit 2.4GHz (Attention, current tx core has around 1.19us starting delay that makes the ack fall behind 10us SIFS in 2.4GHz! Need to improve TX in 2.4GHz!)
927 
928 	//xpu_api->XPU_REG_RECV_ACK_COUNT_TOP0_write( (((45+2+2)*200)<<16) | 400 );//2.4GHz
929 	//xpu_api->XPU_REG_RECV_ACK_COUNT_TOP1_write( (((51+2+2)*200)<<16) | 400 );//5GHz
930 
931 	// // value from openwifi-pre0 csma_test branch
932 	xpu_api->XPU_REG_RECV_ACK_COUNT_TOP0_write( (((45+2+2)*200)<<16) | 200 );//2.4GHz
933 	xpu_api->XPU_REG_RECV_ACK_COUNT_TOP1_write( (((51+2+2)*200)<<16) | 200 );//5GHz
934 
935 	tx_intf_api->TX_INTF_REG_CTS_TOSELF_WAIT_SIFS_TOP_write( ((16*200)<<16)|(10*200) );//high 16bit 5GHz; low 16 bit 2.4GHz
936 
937 	//xpu_api->XPU_REG_BB_RF_DELAY_write(1020); // fine tuned value at 0.005us. old: dac-->ant port: 0.6us, 57 taps fir at 40MHz: 1.425us; round trip: 2*(0.6+1.425)=4.05us; 4.05*200=810
938 	xpu_api->XPU_REG_BB_RF_DELAY_write(975);//add .5us for slightly longer fir
939 	xpu_api->XPU_REG_MAC_ADDR_write(priv->mac_addr);
940 
941 	xpu_api->XPU_REG_SLICE_COUNT_TOTAL0_write(50000-1); // total 50ms.
942 	xpu_api->XPU_REG_SLICE_COUNT_START0_write(0); //start 0ms
943 	xpu_api->XPU_REG_SLICE_COUNT_END0_write(50000-1); //end 50ms
944 	xpu_api->XPU_REG_SLICE_COUNT_TOTAL1_write(50000-1); // total 50ms
945 	xpu_api->XPU_REG_SLICE_COUNT_START1_write(49000); //start 49ms
946 	xpu_api->XPU_REG_SLICE_COUNT_END1_write(50000-1); //end 50ms
947 
948 	//xpu_api->XPU_REG_MAC_ADDR_HIGH_write( (*( (u16*)(priv->mac_addr + 4) )) );
949 	printk("%s openwifi_start: rx_intf_cfg %d openofdm_rx_cfg %d tx_intf_cfg %d openofdm_tx_cfg %d\n",sdr_compatible_str, priv->rx_intf_cfg, priv->openofdm_rx_cfg, priv->tx_intf_cfg, priv->openofdm_tx_cfg);
950 	printk("%s openwifi_start: rx_freq_offset_to_lo_MHz %d tx_freq_offset_to_lo_MHz %d\n",sdr_compatible_str, priv->rx_freq_offset_to_lo_MHz, priv->tx_freq_offset_to_lo_MHz);
951 
952 	tx_intf_api->TX_INTF_REG_INTERRUPT_SEL_write(0x30040); //disable tx interrupt
953 	rx_intf_api->RX_INTF_REG_INTERRUPT_TEST_write(0x100); // disable rx interrupt by interrupt test mode
954 	rx_intf_api->RX_INTF_REG_M_AXIS_RST_write(1); // hold M AXIS in reset status
955 
956 	if (test_mode==1) {
957 		printk("%s openwifi_start: test_mode==1\n",sdr_compatible_str);
958 		goto normal_out;
959 	}
960 
961 	priv->rx_chan = dma_request_slave_channel(&(priv->pdev->dev), "rx_dma_s2mm");
962 	if (IS_ERR(priv->rx_chan)) {
963 		ret = PTR_ERR(priv->rx_chan);
964 		pr_err("%s openwifi_start: No Rx channel %d\n",sdr_compatible_str,ret);
965 		goto err_dma;
966 		//goto err_free_reg;
967 		//goto err_free_dev;
968 	}
969 
970 	priv->tx_chan = dma_request_slave_channel(&(priv->pdev->dev), "tx_dma_mm2s");
971 	if (IS_ERR(priv->tx_chan)) {
972 		ret = PTR_ERR(priv->tx_chan);
973 		pr_err("%s openwifi_start: No Tx channel %d\n",sdr_compatible_str,ret);
974 		goto err_dma;
975 		//goto err_free_reg;
976 		//goto err_free_dev;
977 	}
978 	printk("%s openwifi_start: DMA channel setup successfully.\n",sdr_compatible_str);
979 
980 	ret = openwifi_init_rx_ring(priv);
981 	if (ret) {
982 		printk("%s openwifi_start: openwifi_init_rx_ring ret %d\n", sdr_compatible_str,ret);
983 		goto err_free_rings;
984 	}
985 
986 	priv->seqno=0;
987 	priv->phy_tx_sn=0;
988 	if ((ret = openwifi_init_tx_ring(priv))) {
989 		printk("%s openwifi_start: openwifi_init_tx_ring ret %d\n", sdr_compatible_str,ret);
990 		goto err_free_rings;
991 	}
992 
993 	if ( (ret = rx_dma_setup(dev)) ) {
994 		printk("%s openwifi_start: rx_dma_setup ret %d\n", sdr_compatible_str,ret);
995 		goto err_free_rings;
996 	}
997 
998 	priv->irq_rx = irq_of_parse_and_map(priv->pdev->dev.of_node, 1);
999 	ret = request_irq(priv->irq_rx, openwifi_rx_interrupt,
1000 			IRQF_SHARED, "sdr,rx_pkt_intr", dev);
1001 	if (ret) {
1002 		wiphy_err(dev->wiphy, "openwifi_start:failed to register IRQ handler openwifi_rx_interrupt\n");
1003 		goto err_free_rings;
1004 	} else {
1005 		printk("%s openwifi_start: irq_rx %d\n", sdr_compatible_str, priv->irq_rx);
1006 	}
1007 
1008 	priv->irq_tx = irq_of_parse_and_map(priv->pdev->dev.of_node, 3);
1009 	ret = request_irq(priv->irq_tx, openwifi_tx_interrupt,
1010 			IRQF_SHARED, "sdr,tx_itrpt1", dev);
1011 	if (ret) {
1012 		wiphy_err(dev->wiphy, "openwifi_start: failed to register IRQ handler openwifi_tx_interrupt\n");
1013 		goto err_free_rings;
1014 	} else {
1015 		printk("%s openwifi_start: irq_tx %d\n", sdr_compatible_str, priv->irq_tx);
1016 	}
1017 
1018 	rx_intf_api->RX_INTF_REG_INTERRUPT_TEST_write(0x000); // enable rx interrupt get normal fcs valid pass through ddc to ARM
1019 	tx_intf_api->TX_INTF_REG_INTERRUPT_SEL_write(0x40); //enable tx interrupt
1020 	rx_intf_api->RX_INTF_REG_M_AXIS_RST_write(0); // release M AXIS
1021 	xpu_api->XPU_REG_TSF_LOAD_VAL_write(0,0); // reset tsf timer
1022 
1023 	//ieee80211_wake_queue(dev, 0);
1024 
1025 normal_out:
1026 	printk("%s openwifi_start: normal end\n", sdr_compatible_str);
1027 	return 0;
1028 
1029 err_free_rings:
1030 	openwifi_free_rx_ring(priv);
1031 	openwifi_free_tx_ring(priv);
1032 
1033 err_dma:
1034 	ret = -1;
1035 	printk("%s openwifi_start: abnormal end ret %d\n", sdr_compatible_str, ret);
1036 	return ret;
1037 }
1038 
1039 static void openwifi_stop(struct ieee80211_hw *dev)
1040 {
1041 	struct openwifi_priv *priv = dev->priv;
1042 	u32 reg, reg1;
1043 	int i;
1044 
1045 	if (test_mode==1){
1046 		pr_info("%s openwifi_stop: test_mode==1\n", sdr_compatible_str);
1047 		goto normal_out;
1048 	}
1049 
1050 	//turn off radio
1051 	#if 1
1052 	ad9361_tx_mute(priv->ad9361_phy, 1);
1053 	reg = ad9361_get_tx_atten(priv->ad9361_phy, 2);
1054 	reg1 = ad9361_get_tx_atten(priv->ad9361_phy, 1);
1055 	if (reg == AD9361_RADIO_OFF_TX_ATT && reg1 == AD9361_RADIO_OFF_TX_ATT ) {
1056 		priv->rfkill_off = 0;// 0 off, 1 on
1057 		printk("%s openwifi_stop: rfkill radio off\n",sdr_compatible_str);
1058 	}
1059 	else
1060 		printk("%s openwifi_stop: WARNING rfkill radio off failed. tx att read %d %d require %d\n",sdr_compatible_str, reg, reg1, AD9361_RADIO_OFF_TX_ATT);
1061 	#endif
1062 
1063 	//ieee80211_stop_queue(dev, 0);
1064 
1065 	tx_intf_api->TX_INTF_REG_INTERRUPT_SEL_write(0x30040); //disable tx interrupt
1066 	rx_intf_api->RX_INTF_REG_INTERRUPT_TEST_write(0x100); // disable fcs_valid by interrupt test mode
1067 	rx_intf_api->RX_INTF_REG_M_AXIS_RST_write(1); // hold M AXIS in reset status
1068 
1069 	for (i=0; i<MAX_NUM_VIF; i++) {
1070 		priv->vif[i] = NULL;
1071 	}
1072 
1073 	openwifi_free_rx_ring(priv);
1074 	openwifi_free_tx_ring(priv);
1075 
1076 	pr_info("%s openwifi_stop: dropped channel %s\n", sdr_compatible_str, dma_chan_name(priv->rx_chan));
1077 	dmaengine_terminate_all(priv->rx_chan);
1078 	dma_release_channel(priv->rx_chan);
1079 	pr_info("%s openwifi_stop: dropped channel %s\n", sdr_compatible_str, dma_chan_name(priv->tx_chan));
1080 	dmaengine_terminate_all(priv->tx_chan);
1081 	dma_release_channel(priv->tx_chan);
1082 
1083 	//priv->rf->stop(dev);
1084 
1085 	free_irq(priv->irq_rx, dev);
1086 	free_irq(priv->irq_tx, dev);
1087 
1088 normal_out:
1089 	printk("%s openwifi_stop\n", sdr_compatible_str);
1090 }
1091 
1092 static u64 openwifi_get_tsf(struct ieee80211_hw *dev,
1093 			   struct ieee80211_vif *vif)
1094 {
1095 	u32 tsft_low, tsft_high;
1096 
1097 	tsft_low = xpu_api->XPU_REG_TSF_RUNTIME_VAL_LOW_read();
1098 	tsft_high = xpu_api->XPU_REG_TSF_RUNTIME_VAL_HIGH_read();
1099 	//printk("%s openwifi_get_tsf: %08x%08x\n", sdr_compatible_str,tsft_high,tsft_low);
1100 	return( ( (u64)tsft_low ) | ( ((u64)tsft_high)<<32 ) );
1101 }
1102 
1103 static void openwifi_set_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif, u64 tsf)
1104 {
1105 	u32 tsft_high = ((tsf >> 32)&0xffffffff);
1106 	u32 tsft_low  = (tsf&0xffffffff);
1107 	xpu_api->XPU_REG_TSF_LOAD_VAL_write(tsft_high,tsft_low);
1108 	printk("%s openwifi_set_tsf: %08x%08x\n", sdr_compatible_str,tsft_high,tsft_low);
1109 }
1110 
1111 static void openwifi_reset_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1112 {
1113 	xpu_api->XPU_REG_TSF_LOAD_VAL_write(0,0);
1114 	printk("%s openwifi_reset_tsf\n", sdr_compatible_str);
1115 }
1116 
1117 static int openwifi_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
1118 {
1119 	printk("%s openwifi_set_rts_threshold WARNING value %d\n", sdr_compatible_str,value);
1120 	return(0);
1121 }
1122 
1123 static void openwifi_beacon_work(struct work_struct *work)
1124 {
1125 	struct openwifi_vif *vif_priv =
1126 		container_of(work, struct openwifi_vif, beacon_work.work);
1127 	struct ieee80211_vif *vif =
1128 		container_of((void *)vif_priv, struct ieee80211_vif, drv_priv);
1129 	struct ieee80211_hw *dev = vif_priv->dev;
1130 	struct ieee80211_mgmt *mgmt;
1131 	struct sk_buff *skb;
1132 
1133 	/* don't overflow the tx ring */
1134 	if (ieee80211_queue_stopped(dev, 0))
1135 		goto resched;
1136 
1137 	/* grab a fresh beacon */
1138 	skb = ieee80211_beacon_get(dev, vif);
1139 	if (!skb)
1140 		goto resched;
1141 
1142 	/*
1143 	 * update beacon timestamp w/ TSF value
1144 	 * TODO: make hardware update beacon timestamp
1145 	 */
1146 	mgmt = (struct ieee80211_mgmt *)skb->data;
1147 	mgmt->u.beacon.timestamp = cpu_to_le64(openwifi_get_tsf(dev, vif));
1148 
1149 	/* TODO: use actual beacon queue */
1150 	skb_set_queue_mapping(skb, 0);
1151 	openwifi_tx(dev, NULL, skb);
1152 
1153 resched:
1154 	/*
1155 	 * schedule next beacon
1156 	 * TODO: use hardware support for beacon timing
1157 	 */
1158 	schedule_delayed_work(&vif_priv->beacon_work,
1159 			usecs_to_jiffies(1024 * vif->bss_conf.beacon_int));
1160 }
1161 
1162 static int openwifi_add_interface(struct ieee80211_hw *dev,
1163 				 struct ieee80211_vif *vif)
1164 {
1165 	int i;
1166 	struct openwifi_priv *priv = dev->priv;
1167 	struct openwifi_vif *vif_priv;
1168 
1169 	switch (vif->type) {
1170 	case NL80211_IFTYPE_AP:
1171 	case NL80211_IFTYPE_STATION:
1172 	case NL80211_IFTYPE_ADHOC:
1173 	case NL80211_IFTYPE_MONITOR:
1174 	case NL80211_IFTYPE_MESH_POINT:
1175 		break;
1176 	default:
1177 		return -EOPNOTSUPP;
1178 	}
1179 	// let's support more than 1 interface
1180 	for (i=0; i<MAX_NUM_VIF; i++) {
1181 		if (priv->vif[i] == NULL)
1182 			break;
1183 	}
1184 
1185 	printk("%s openwifi_add_interface start. vif for loop result %d\n", sdr_compatible_str, i);
1186 
1187 	if (i==MAX_NUM_VIF)
1188 		return -EBUSY;
1189 
1190 	priv->vif[i] = vif;
1191 
1192 	/* Initialize driver private area */
1193 	vif_priv = (struct openwifi_vif *)&vif->drv_priv;
1194 	vif_priv->idx = i;
1195 
1196 	vif_priv->dev = dev;
1197 	INIT_DELAYED_WORK(&vif_priv->beacon_work, openwifi_beacon_work);
1198 	vif_priv->enable_beacon = false;
1199 
1200 	printk("%s openwifi_add_interface end with vif idx %d\n", sdr_compatible_str,vif_priv->idx);
1201 
1202 	return 0;
1203 }
1204 
1205 static void openwifi_remove_interface(struct ieee80211_hw *dev,
1206 				     struct ieee80211_vif *vif)
1207 {
1208 	struct openwifi_vif *vif_priv;
1209 	struct openwifi_priv *priv = dev->priv;
1210 
1211 	vif_priv = (struct openwifi_vif *)&vif->drv_priv;
1212 	priv->vif[vif_priv->idx] = NULL;
1213 	printk("%s openwifi_remove_interface vif idx %d\n", sdr_compatible_str, vif_priv->idx);
1214 }
1215 
1216 static int openwifi_config(struct ieee80211_hw *dev, u32 changed)
1217 {
1218 	struct openwifi_priv *priv = dev->priv;
1219 	struct ieee80211_conf *conf = &dev->conf;
1220 
1221 	if (changed & IEEE80211_CONF_CHANGE_CHANNEL)
1222 		priv->rf->set_chan(dev, conf);
1223 	else
1224 		printk("%s openwifi_config changed flag %08x\n", sdr_compatible_str, changed);
1225 
1226 	return 0;
1227 }
1228 
1229 static void openwifi_bss_info_changed(struct ieee80211_hw *dev,
1230 				     struct ieee80211_vif *vif,
1231 				     struct ieee80211_bss_conf *info,
1232 				     u32 changed)
1233 {
1234 	struct openwifi_priv *priv = dev->priv;
1235 	struct openwifi_vif *vif_priv;
1236 	u32 bssid_low, bssid_high;
1237 
1238 	vif_priv = (struct openwifi_vif *)&vif->drv_priv;
1239 
1240 	//be careful: we don have valid chip, so registers addresses in priv->map->BSSID[0] are not valid! should not print it!
1241 	//printk("%s openwifi_bss_info_changed map bssid %02x%02x%02x%02x%02x%02x\n",sdr_compatible_str,priv->map->BSSID[0],priv->map->BSSID[1],priv->map->BSSID[2],priv->map->BSSID[3],priv->map->BSSID[4],priv->map->BSSID[5]);
1242 	if (changed & BSS_CHANGED_BSSID) {
1243 		printk("%s openwifi_bss_info_changed BSS_CHANGED_BSSID %02x%02x%02x%02x%02x%02x\n",sdr_compatible_str,info->bssid[0],info->bssid[1],info->bssid[2],info->bssid[3],info->bssid[4],info->bssid[5]);
1244 		// write new bssid to our HW, and do not change bssid filter
1245 		//u32 bssid_filter_high = xpu_api->XPU_REG_BSSID_FILTER_HIGH_read();
1246 		bssid_low = ( *( (u32*)(info->bssid) ) );
1247 		bssid_high = ( *( (u16*)(info->bssid+4) ) );
1248 
1249 		//bssid_filter_high = (bssid_filter_high&0x80000000);
1250 		//bssid_high = (bssid_high|bssid_filter_high);
1251 		xpu_api->XPU_REG_BSSID_FILTER_LOW_write(bssid_low);
1252 		xpu_api->XPU_REG_BSSID_FILTER_HIGH_write(bssid_high);
1253 	}
1254 
1255 	if (changed & BSS_CHANGED_BEACON_INT) {
1256 		printk("%s openwifi_bss_info_changed WARNING BSS_CHANGED_BEACON_INT %x\n",sdr_compatible_str,info->beacon_int);
1257 	}
1258 
1259 	if (changed & BSS_CHANGED_TXPOWER)
1260 		printk("%s openwifi_bss_info_changed WARNING BSS_CHANGED_TXPOWER %x\n",sdr_compatible_str,info->txpower);
1261 
1262 	if (changed & BSS_CHANGED_ERP_CTS_PROT)
1263 		printk("%s openwifi_bss_info_changed WARNING BSS_CHANGED_ERP_CTS_PROT %x\n",sdr_compatible_str,info->use_cts_prot);
1264 
1265 	if (changed & BSS_CHANGED_BASIC_RATES)
1266 		printk("%s openwifi_bss_info_changed WARNING BSS_CHANGED_BASIC_RATES %x\n",sdr_compatible_str,info->basic_rates);
1267 
1268 	if (changed & (BSS_CHANGED_ERP_SLOT | BSS_CHANGED_ERP_PREAMBLE)) {
1269 		printk("%s openwifi_bss_info_changed WARNING BSS_CHANGED_ERP_SLOT %d BSS_CHANGED_ERP_PREAMBLE %d short slot %d\n",sdr_compatible_str,
1270 		changed&BSS_CHANGED_ERP_SLOT,changed&BSS_CHANGED_ERP_PREAMBLE,info->use_short_slot);
1271 		if (info->use_short_slot && priv->use_short_slot==false) {
1272 			priv->use_short_slot=true;
1273 			xpu_api->XPU_REG_BAND_CHANNEL_write( (priv->use_short_slot<<24)|(priv->band<<16) );
1274 		} else if ((!info->use_short_slot) && priv->use_short_slot==true) {
1275 			priv->use_short_slot=false;
1276 			xpu_api->XPU_REG_BAND_CHANNEL_write( (priv->use_short_slot<<24)|(priv->band<<16) );
1277 		}
1278 	}
1279 
1280 	if (changed & BSS_CHANGED_BEACON_ENABLED) {
1281 		printk("%s openwifi_bss_info_changed WARNING BSS_CHANGED_BEACON_ENABLED\n",sdr_compatible_str);
1282 		vif_priv->enable_beacon = info->enable_beacon;
1283 	}
1284 
1285 	if (changed & (BSS_CHANGED_BEACON_ENABLED | BSS_CHANGED_BEACON)) {
1286 		cancel_delayed_work_sync(&vif_priv->beacon_work);
1287 		if (vif_priv->enable_beacon)
1288 			schedule_work(&vif_priv->beacon_work.work);
1289 		printk("%s openwifi_bss_info_changed WARNING BSS_CHANGED_BEACON_ENABLED %d BSS_CHANGED_BEACON %d\n",sdr_compatible_str,
1290 		changed&BSS_CHANGED_BEACON_ENABLED,changed&BSS_CHANGED_BEACON);
1291 	}
1292 }
1293 
1294 static int openwifi_conf_tx(struct ieee80211_hw *hw, struct ieee80211_vif *vif, u16 queue,
1295 	      const struct ieee80211_tx_queue_params *params)
1296 {
1297 	printk("%s openwifi_conf_tx: WARNING [queue %d], aifs: %d, cw_min: %d, cw_max: %d, txop: %d\n",
1298 		  sdr_compatible_str,queue,params->aifs,params->cw_min,params->cw_max,params->txop);
1299 	return(0);
1300 }
1301 
1302 static u64 openwifi_prepare_multicast(struct ieee80211_hw *dev,
1303 				     struct netdev_hw_addr_list *mc_list)
1304 {
1305 	printk("%s openwifi_prepare_multicast\n", sdr_compatible_str);
1306 	return netdev_hw_addr_list_count(mc_list);
1307 }
1308 
1309 static void openwifi_configure_filter(struct ieee80211_hw *dev,
1310 				     unsigned int changed_flags,
1311 				     unsigned int *total_flags,
1312 				     u64 multicast)
1313 {
1314 	struct openwifi_priv *priv = dev->priv;
1315 	u32 filter_flag;
1316 
1317 	(*total_flags) &= SDR_SUPPORTED_FILTERS;
1318 //	(*total_flags) |= FIF_ALLMULTI; //because we always pass all multicast (no matter it is for us or not) to upper layer
1319 
1320 	filter_flag = (*total_flags);
1321 
1322 	filter_flag = (filter_flag|UNICAST_FOR_US|BROADCAST_ALL_ONE|BROADCAST_ALL_ZERO);
1323 	//filter_flag = (filter_flag|UNICAST_FOR_US|BROADCAST_ALL_ONE|BROADCAST_ALL_ZERO|MONITOR_ALL); // all pkt will be delivered to arm
1324 
1325 	if (priv->vif[0]->type == NL80211_IFTYPE_MONITOR)
1326 		filter_flag = (filter_flag|MONITOR_ALL);
1327 
1328 	if ( (priv->vif[0]->type == NL80211_IFTYPE_STATION) && !(filter_flag&FIF_BCN_PRBRESP_PROMISC) )
1329 		filter_flag = (filter_flag|MY_BEACON);
1330 
1331 	if (priv->vif[0]->type == NL80211_IFTYPE_AP)
1332 		filter_flag = (filter_flag|FIF_PSPOLL);
1333 
1334 	xpu_api->XPU_REG_FILTER_FLAG_write(filter_flag|HIGH_PRIORITY_DISCARD_FLAG);
1335 	//xpu_api->XPU_REG_FILTER_FLAG_write(filter_flag); //do not discard any pkt
1336 
1337 	printk("%s openwifi_configure_filter MON %d M_BCN %d BST0 %d BST1 %d UST %d PB_RQ %d PS_PL %d O_BSS %d CTL %d BCN_PRP %d PCP_FL %d FCS_FL %d ALL_MUT %d\n", sdr_compatible_str,
1338 	(filter_flag>>13)&1,(filter_flag>>12)&1,(filter_flag>>11)&1,(filter_flag>>10)&1,(filter_flag>>9)&1,(filter_flag>>8)&1,(filter_flag>>7)&1,(filter_flag>>6)&1,(filter_flag>>5)&1,(filter_flag>>4)&1,(filter_flag>>3)&1,(filter_flag>>2)&1,(filter_flag>>1)&1);
1339 }
1340 
1341 static int openwifi_testmode_cmd(struct ieee80211_hw *hw, struct ieee80211_vif *vif, void *data, int len)
1342 {
1343 	struct openwifi_priv *priv = hw->priv;
1344 	struct nlattr *tb[OPENWIFI_ATTR_MAX + 1];
1345 	struct sk_buff *skb;
1346 	int err;
1347 	u32 tmp=-1, reg_cat, reg_addr, reg_val, reg_addr_idx;
1348 
1349 	err = nla_parse(tb, OPENWIFI_ATTR_MAX, data, len, openwifi_testmode_policy, NULL);
1350 	if (err)
1351 		return err;
1352 
1353 	if (!tb[OPENWIFI_ATTR_CMD])
1354 		return -EINVAL;
1355 
1356 	switch (nla_get_u32(tb[OPENWIFI_ATTR_CMD])) {
1357 	case OPENWIFI_CMD_SET_GAP:
1358 		if (!tb[OPENWIFI_ATTR_GAP])
1359 			return -EINVAL;
1360 		tmp = nla_get_u32(tb[OPENWIFI_ATTR_GAP]);
1361 		printk("%s openwifi radio inter frame gap set to %d usec\n", sdr_compatible_str, tmp);
1362 		xpu_api->XPU_REG_CSMA_CFG_write(tmp); // unit us
1363 		return 0;
1364 	case OPENWIFI_CMD_GET_GAP:
1365 		skb = cfg80211_testmode_alloc_reply_skb(hw->wiphy, nla_total_size(sizeof(u32)));
1366 		if (!skb)
1367 			return -ENOMEM;
1368 		tmp = xpu_api->XPU_REG_CSMA_CFG_read();
1369 		if (nla_put_u32(skb, OPENWIFI_ATTR_GAP, tmp))
1370 			goto nla_put_failure;
1371 		return cfg80211_testmode_reply(skb);
1372 	case OPENWIFI_CMD_SET_ADDR0:
1373 		if (!tb[OPENWIFI_ATTR_ADDR0])
1374 			return -EINVAL;
1375 		tmp = nla_get_u32(tb[OPENWIFI_ATTR_ADDR0]);
1376 		printk("%s set openwifi slice0_target_mac_addr(low32) in hex: %08x\n", sdr_compatible_str, tmp);
1377 		priv->dest_mac_addr_queue_map[0] = reverse32(tmp);
1378 		return 0;
1379 	case OPENWIFI_CMD_GET_ADDR0:
1380 		skb = cfg80211_testmode_alloc_reply_skb(hw->wiphy, nla_total_size(sizeof(u32)));
1381 		if (!skb)
1382 			return -ENOMEM;
1383 		tmp = reverse32(priv->dest_mac_addr_queue_map[0]);
1384 		if (nla_put_u32(skb, OPENWIFI_ATTR_ADDR0, tmp))
1385 			goto nla_put_failure;
1386 		printk("%s get openwifi slice0_target_mac_addr(low32) in hex: %08x\n", sdr_compatible_str, tmp);
1387 		return cfg80211_testmode_reply(skb);
1388 	case OPENWIFI_CMD_SET_ADDR1:
1389 		if (!tb[OPENWIFI_ATTR_ADDR1])
1390 			return -EINVAL;
1391 		tmp = nla_get_u32(tb[OPENWIFI_ATTR_ADDR1]);
1392 		printk("%s set openwifi slice1_target_mac_addr(low32) in hex: %08x\n", sdr_compatible_str, tmp);
1393 		priv->dest_mac_addr_queue_map[1] = reverse32(tmp);
1394 		return 0;
1395 	case OPENWIFI_CMD_GET_ADDR1:
1396 		skb = cfg80211_testmode_alloc_reply_skb(hw->wiphy, nla_total_size(sizeof(u32)));
1397 		if (!skb)
1398 			return -ENOMEM;
1399 		tmp = reverse32(priv->dest_mac_addr_queue_map[1]);
1400 		if (nla_put_u32(skb, OPENWIFI_ATTR_ADDR1, tmp))
1401 			goto nla_put_failure;
1402 		printk("%s get openwifi slice1_target_mac_addr(low32) in hex: %08x\n", sdr_compatible_str, tmp);
1403 		return cfg80211_testmode_reply(skb);
1404 
1405 	case OPENWIFI_CMD_SET_SLICE_TOTAL0:
1406 		if (!tb[OPENWIFI_ATTR_SLICE_TOTAL0])
1407 			return -EINVAL;
1408 		tmp = nla_get_u32(tb[OPENWIFI_ATTR_SLICE_TOTAL0]);
1409 		printk("%s set SLICE_TOTAL0(duration) to %d usec\n", sdr_compatible_str, tmp);
1410 		xpu_api->XPU_REG_SLICE_COUNT_TOTAL0_write(tmp);
1411 		return 0;
1412 	case OPENWIFI_CMD_GET_SLICE_TOTAL0:
1413 		skb = cfg80211_testmode_alloc_reply_skb(hw->wiphy, nla_total_size(sizeof(u32)));
1414 		if (!skb)
1415 			return -ENOMEM;
1416 		tmp = (xpu_api->XPU_REG_SLICE_COUNT_TOTAL0_read());
1417 		if (nla_put_u32(skb, OPENWIFI_ATTR_SLICE_TOTAL0, tmp))
1418 			goto nla_put_failure;
1419 		return cfg80211_testmode_reply(skb);
1420 
1421 	case OPENWIFI_CMD_SET_SLICE_START0:
1422 		if (!tb[OPENWIFI_ATTR_SLICE_START0])
1423 			return -EINVAL;
1424 		tmp = nla_get_u32(tb[OPENWIFI_ATTR_SLICE_START0]);
1425 		printk("%s set SLICE_START0(duration) to %d usec\n", sdr_compatible_str, tmp);
1426 		xpu_api->XPU_REG_SLICE_COUNT_START0_write(tmp);
1427 		return 0;
1428 	case OPENWIFI_CMD_GET_SLICE_START0:
1429 		skb = cfg80211_testmode_alloc_reply_skb(hw->wiphy, nla_total_size(sizeof(u32)));
1430 		if (!skb)
1431 			return -ENOMEM;
1432 		tmp = (xpu_api->XPU_REG_SLICE_COUNT_START0_read());
1433 		if (nla_put_u32(skb, OPENWIFI_ATTR_SLICE_START0, tmp))
1434 			goto nla_put_failure;
1435 		return cfg80211_testmode_reply(skb);
1436 
1437 	case OPENWIFI_CMD_SET_SLICE_END0:
1438 		if (!tb[OPENWIFI_ATTR_SLICE_END0])
1439 			return -EINVAL;
1440 		tmp = nla_get_u32(tb[OPENWIFI_ATTR_SLICE_END0]);
1441 		printk("%s set SLICE_END0(duration) to %d usec\n", sdr_compatible_str, tmp);
1442 		xpu_api->XPU_REG_SLICE_COUNT_END0_write(tmp);
1443 		return 0;
1444 	case OPENWIFI_CMD_GET_SLICE_END0:
1445 		skb = cfg80211_testmode_alloc_reply_skb(hw->wiphy, nla_total_size(sizeof(u32)));
1446 		if (!skb)
1447 			return -ENOMEM;
1448 		tmp = (xpu_api->XPU_REG_SLICE_COUNT_END0_read());
1449 		if (nla_put_u32(skb, OPENWIFI_ATTR_SLICE_END0, tmp))
1450 			goto nla_put_failure;
1451 		return cfg80211_testmode_reply(skb);
1452 
1453 	case OPENWIFI_CMD_SET_SLICE_TOTAL1:
1454 		if (!tb[OPENWIFI_ATTR_SLICE_TOTAL1])
1455 			return -EINVAL;
1456 		tmp = nla_get_u32(tb[OPENWIFI_ATTR_SLICE_TOTAL1]);
1457 		printk("%s set SLICE_TOTAL1(duration) to %d usec\n", sdr_compatible_str, tmp);
1458 		xpu_api->XPU_REG_SLICE_COUNT_TOTAL1_write(tmp);
1459 		return 0;
1460 	case OPENWIFI_CMD_GET_SLICE_TOTAL1:
1461 		skb = cfg80211_testmode_alloc_reply_skb(hw->wiphy, nla_total_size(sizeof(u32)));
1462 		if (!skb)
1463 			return -ENOMEM;
1464 		tmp = (xpu_api->XPU_REG_SLICE_COUNT_TOTAL1_read());
1465 		if (nla_put_u32(skb, OPENWIFI_ATTR_SLICE_TOTAL1, tmp))
1466 			goto nla_put_failure;
1467 		return cfg80211_testmode_reply(skb);
1468 
1469 	case OPENWIFI_CMD_SET_SLICE_START1:
1470 		if (!tb[OPENWIFI_ATTR_SLICE_START1])
1471 			return -EINVAL;
1472 		tmp = nla_get_u32(tb[OPENWIFI_ATTR_SLICE_START1]);
1473 		printk("%s set SLICE_START1(duration) to %d usec\n", sdr_compatible_str, tmp);
1474 		xpu_api->XPU_REG_SLICE_COUNT_START1_write(tmp);
1475 		return 0;
1476 	case OPENWIFI_CMD_GET_SLICE_START1:
1477 		skb = cfg80211_testmode_alloc_reply_skb(hw->wiphy, nla_total_size(sizeof(u32)));
1478 		if (!skb)
1479 			return -ENOMEM;
1480 		tmp = (xpu_api->XPU_REG_SLICE_COUNT_START1_read());
1481 		if (nla_put_u32(skb, OPENWIFI_ATTR_SLICE_START1, tmp))
1482 			goto nla_put_failure;
1483 		return cfg80211_testmode_reply(skb);
1484 
1485 	case OPENWIFI_CMD_SET_SLICE_END1:
1486 		if (!tb[OPENWIFI_ATTR_SLICE_END1])
1487 			return -EINVAL;
1488 		tmp = nla_get_u32(tb[OPENWIFI_ATTR_SLICE_END1]);
1489 		printk("%s set SLICE_END1(duration) to %d usec\n", sdr_compatible_str, tmp);
1490 		xpu_api->XPU_REG_SLICE_COUNT_END1_write(tmp);
1491 		return 0;
1492 	case OPENWIFI_CMD_GET_SLICE_END1:
1493 		skb = cfg80211_testmode_alloc_reply_skb(hw->wiphy, nla_total_size(sizeof(u32)));
1494 		if (!skb)
1495 			return -ENOMEM;
1496 		tmp = (xpu_api->XPU_REG_SLICE_COUNT_END1_read());
1497 		if (nla_put_u32(skb, OPENWIFI_ATTR_SLICE_END1, tmp))
1498 			goto nla_put_failure;
1499 		return cfg80211_testmode_reply(skb);
1500 
1501 	case OPENWIFI_CMD_SET_RSSI_TH:
1502 		if (!tb[OPENWIFI_ATTR_RSSI_TH])
1503 			return -EINVAL;
1504 		tmp = nla_get_u32(tb[OPENWIFI_ATTR_RSSI_TH]);
1505 		printk("%s set RSSI_TH to %d\n", sdr_compatible_str, tmp);
1506 		xpu_api->XPU_REG_LBT_TH_write(tmp);
1507 		return 0;
1508 	case OPENWIFI_CMD_GET_RSSI_TH:
1509 		skb = cfg80211_testmode_alloc_reply_skb(hw->wiphy, nla_total_size(sizeof(u32)));
1510 		if (!skb)
1511 			return -ENOMEM;
1512 		tmp = xpu_api->XPU_REG_LBT_TH_read();
1513 		if (nla_put_u32(skb, OPENWIFI_ATTR_RSSI_TH, tmp))
1514 			goto nla_put_failure;
1515 		return cfg80211_testmode_reply(skb);
1516 
1517 	case REG_CMD_SET:
1518 		if ( (!tb[REG_ATTR_ADDR]) || (!tb[REG_ATTR_VAL]) )
1519 			return -EINVAL;
1520 		reg_addr = nla_get_u32(tb[REG_ATTR_ADDR]);
1521 		reg_val  = nla_get_u32(tb[REG_ATTR_VAL]);
1522 		reg_cat = ((reg_addr>>16)&0xFFFF);
1523 		reg_addr = (reg_addr&0xFFFF);
1524 		reg_addr_idx = (reg_addr>>2);
1525 		printk("%s recv set cmd reg cat %d addr %08x val %08x idx %d\n", sdr_compatible_str, reg_cat, reg_addr, reg_val, reg_addr_idx);
1526 		if (reg_cat==1)
1527 			printk("%s reg cat 1 (rf) is not supported yet!\n", sdr_compatible_str);
1528 		else if (reg_cat==2)
1529 			rx_intf_api->reg_write(reg_addr,reg_val);
1530 		else if (reg_cat==3)
1531 			tx_intf_api->reg_write(reg_addr,reg_val);
1532 		else if (reg_cat==4)
1533 			openofdm_rx_api->reg_write(reg_addr,reg_val);
1534 		else if (reg_cat==5)
1535 			openofdm_tx_api->reg_write(reg_addr,reg_val);
1536 		else if (reg_cat==6)
1537 			xpu_api->reg_write(reg_addr,reg_val);
1538 		else if (reg_cat==7) {
1539 			priv->drv_rx_reg_val[reg_addr_idx]=reg_val;
1540 			if (reg_addr_idx==1) {
1541 				if (reg_val==0)
1542 					priv->rx_intf_cfg = RX_INTF_BW_20MHZ_AT_0MHZ_ANT0;
1543 				else
1544 					priv->rx_intf_cfg = RX_INTF_BW_20MHZ_AT_0MHZ_ANT1;
1545 
1546 				priv->rx_freq_offset_to_lo_MHz = rx_intf_fo_mapping[priv->rx_intf_cfg];
1547 				//priv->tx_freq_offset_to_lo_MHz = tx_intf_fo_mapping[priv->tx_intf_cfg];
1548 			}
1549 		}
1550 		else if (reg_cat==8) {
1551 			priv->drv_tx_reg_val[reg_addr_idx]=reg_val;
1552 			if (reg_addr_idx==1) {
1553 				if (reg_val==0) {
1554 					priv->tx_intf_cfg = TX_INTF_BW_20MHZ_AT_N_10MHZ_ANT0;
1555 					ad9361_set_tx_atten(priv->ad9361_phy, AD9361_RADIO_ON_TX_ATT, true, false, true);
1556 				} else {
1557 					priv->tx_intf_cfg = TX_INTF_BW_20MHZ_AT_N_10MHZ_ANT1;
1558 					ad9361_set_tx_atten(priv->ad9361_phy, AD9361_RADIO_ON_TX_ATT, false, true, true);
1559 				}
1560 
1561 				//priv->rx_freq_offset_to_lo_MHz = rx_intf_fo_mapping[priv->rx_intf_cfg];
1562 				priv->tx_freq_offset_to_lo_MHz = tx_intf_fo_mapping[priv->tx_intf_cfg];
1563 			}
1564 		}
1565 		else if (reg_cat==9) {
1566 			priv->drv_xpu_reg_val[reg_addr_idx]=reg_val;
1567 		}
1568 		else
1569 			printk("%s reg cat %d is not supported yet!\n", sdr_compatible_str, reg_cat);
1570 
1571 		return 0;
1572 	case REG_CMD_GET:
1573 		skb = cfg80211_testmode_alloc_reply_skb(hw->wiphy, nla_total_size(sizeof(u32)));
1574 		if (!skb)
1575 			return -ENOMEM;
1576 		reg_addr = nla_get_u32(tb[REG_ATTR_ADDR]);
1577 		reg_cat = ((reg_addr>>16)&0xFFFF);
1578 		reg_addr = (reg_addr&0xFFFF);
1579 		reg_addr_idx = (reg_addr>>2);
1580 		printk("%s recv get cmd reg cat %d addr %08x idx %d\n", sdr_compatible_str, reg_cat, reg_addr, reg_addr_idx);
1581 		if (reg_cat==1) {
1582 			printk("%s reg cat 1 (rf) is not supported yet!\n", sdr_compatible_str);
1583 			tmp = 0xFFFFFFFF;
1584 		}
1585 		else if (reg_cat==2)
1586 			tmp = rx_intf_api->reg_read(reg_addr);
1587 		else if (reg_cat==3)
1588 			tmp = tx_intf_api->reg_read(reg_addr);
1589 		else if (reg_cat==4)
1590 			tmp = openofdm_rx_api->reg_read(reg_addr);
1591 		else if (reg_cat==5)
1592 			tmp = openofdm_tx_api->reg_read(reg_addr);
1593 		else if (reg_cat==6)
1594 			tmp = xpu_api->reg_read(reg_addr);
1595 		else if (reg_cat==7) {
1596 			if (reg_addr_idx==1) {
1597 				priv->rx_freq_offset_to_lo_MHz = rx_intf_fo_mapping[priv->rx_intf_cfg];
1598 				//priv->tx_freq_offset_to_lo_MHz = tx_intf_fo_mapping[priv->tx_intf_cfg];
1599 
1600 				if (priv->rx_intf_cfg == RX_INTF_BW_20MHZ_AT_0MHZ_ANT0)
1601 					priv->drv_rx_reg_val[reg_addr_idx]=0;
1602 				else if	(priv->rx_intf_cfg == RX_INTF_BW_20MHZ_AT_0MHZ_ANT1)
1603 					priv->drv_rx_reg_val[reg_addr_idx]=1;
1604 			}
1605 			tmp = priv->drv_rx_reg_val[reg_addr_idx];
1606 		}
1607 		else if (reg_cat==8) {
1608 			if (reg_addr_idx==1) {
1609 				//priv->rx_freq_offset_to_lo_MHz = rx_intf_fo_mapping[priv->rx_intf_cfg];
1610 				priv->tx_freq_offset_to_lo_MHz = tx_intf_fo_mapping[priv->tx_intf_cfg];
1611 				if (priv->tx_intf_cfg == TX_INTF_BW_20MHZ_AT_N_10MHZ_ANT0)
1612 					priv->drv_tx_reg_val[reg_addr_idx]=0;
1613 				else if	(priv->tx_intf_cfg == TX_INTF_BW_20MHZ_AT_N_10MHZ_ANT1)
1614 					priv->drv_tx_reg_val[reg_addr_idx]=1;
1615 			}
1616 			tmp = priv->drv_tx_reg_val[reg_addr_idx];
1617 		}
1618 		else if (reg_cat==9) {
1619 			tmp = priv->drv_xpu_reg_val[reg_addr_idx];
1620 		}
1621 		else
1622 			printk("%s reg cat %d is not supported yet!\n", sdr_compatible_str, reg_cat);
1623 
1624 		if (nla_put_u32(skb, REG_ATTR_VAL, tmp))
1625 			goto nla_put_failure;
1626 		return cfg80211_testmode_reply(skb);
1627 
1628 	default:
1629 		return -EOPNOTSUPP;
1630 	}
1631 
1632  nla_put_failure:
1633 	dev_kfree_skb(skb);
1634 	return -ENOBUFS;
1635 }
1636 
1637 static const struct ieee80211_ops openwifi_ops = {
1638 	.tx			       = openwifi_tx,
1639 	.start			   = openwifi_start,
1640 	.stop			   = openwifi_stop,
1641 	.add_interface	   = openwifi_add_interface,
1642 	.remove_interface  = openwifi_remove_interface,
1643 	.config			   = openwifi_config,
1644 	.bss_info_changed  = openwifi_bss_info_changed,
1645 	.conf_tx		   = openwifi_conf_tx,
1646 	.prepare_multicast = openwifi_prepare_multicast,
1647 	.configure_filter  = openwifi_configure_filter,
1648 	.rfkill_poll	   = openwifi_rfkill_poll,
1649 	.get_tsf		   = openwifi_get_tsf,
1650 	.set_tsf		   = openwifi_set_tsf,
1651 	.reset_tsf		   = openwifi_reset_tsf,
1652 	.set_rts_threshold = openwifi_set_rts_threshold,
1653 	.testmode_cmd	   = openwifi_testmode_cmd,
1654 };
1655 
1656 static const struct of_device_id openwifi_dev_of_ids[] = {
1657 	{ .compatible = "sdr,sdr", },
1658 	{}
1659 };
1660 MODULE_DEVICE_TABLE(of, openwifi_dev_of_ids);
1661 
1662 static int custom_match_spi_dev(struct device *dev, void *data)
1663 {
1664     const char *name = data;
1665 
1666 	bool ret = sysfs_streq(name, dev->of_node->name);
1667 	printk("%s custom_match_spi_dev %s %s %d\n", sdr_compatible_str,name, dev->of_node->name, ret);
1668 	return ret;
1669 }
1670 
1671 static int custom_match_platform_dev(struct device *dev, void *data)
1672 {
1673 	struct platform_device *plat_dev = to_platform_device(dev);
1674 	const char *name = data;
1675 	char *name_in_sys_bus_platform_devices = strstr(plat_dev->name, name);
1676 	bool match_flag = (name_in_sys_bus_platform_devices != NULL);
1677 
1678 	if (match_flag) {
1679 		printk("%s custom_match_platform_dev %s\n", sdr_compatible_str,plat_dev->name);
1680 	}
1681 	return(match_flag);
1682 }
1683 
1684 static int openwifi_dev_probe(struct platform_device *pdev)
1685 {
1686 	struct ieee80211_hw *dev;
1687 	struct openwifi_priv *priv;
1688 	int err=1, rand_val;
1689 	const char *chip_name;
1690 	u32 reg;//, reg1;
1691 
1692 	struct device_node *np = pdev->dev.of_node;
1693 
1694 	struct device *tmp_dev;
1695 	struct platform_device *tmp_pdev;
1696 	struct iio_dev *tmp_indio_dev;
1697 	// struct gpio_leds_priv *tmp_led_priv;
1698 
1699 	printk("\n");
1700 
1701 	if (np) {
1702 		const struct of_device_id *match;
1703 
1704 		match = of_match_node(openwifi_dev_of_ids, np);
1705 		if (match) {
1706 			printk("%s openwifi_dev_probe: match!\n", sdr_compatible_str);
1707 			err = 0;
1708 		}
1709 	}
1710 
1711 	if (err)
1712 		return err;
1713 
1714 	dev = ieee80211_alloc_hw(sizeof(*priv), &openwifi_ops);
1715 	if (!dev) {
1716 		printk(KERN_ERR "%s openwifi_dev_probe: ieee80211 alloc failed\n",sdr_compatible_str);
1717 		err = -ENOMEM;
1718 		goto err_free_dev;
1719 	}
1720 
1721 	priv = dev->priv;
1722 	priv->pdev = pdev;
1723 
1724 	// //-------------find ad9361-phy driver for lo/channel control---------------
1725 	priv->actual_rx_lo = 0;
1726 	tmp_dev = bus_find_device( &spi_bus_type, NULL, "ad9361-phy", custom_match_spi_dev );
1727 	if (!tmp_dev) {
1728 		printk(KERN_ERR "%s find_dev ad9361-phy failed\n",sdr_compatible_str);
1729 		err = -ENOMEM;
1730 		goto err_free_dev;
1731 	}
1732 	priv->ad9361_phy = ad9361_spi_to_phy((struct spi_device*)tmp_dev);
1733 	if (!(priv->ad9361_phy)) {
1734 		printk(KERN_ERR "%s ad9361_spi_to_phy failed\n",sdr_compatible_str);
1735 		err = -ENOMEM;
1736 		goto err_free_dev;
1737 	}
1738 
1739 	priv->ctrl_out.en_mask=0xFF;
1740 	priv->ctrl_out.index=0x16;
1741 	err = ad9361_ctrl_outs_setup(priv->ad9361_phy, &(priv->ctrl_out));
1742 	if (err < 0) {
1743 		printk("%s openwifi_dev_probe: WARNING ad9361_ctrl_outs_setup %d\n",sdr_compatible_str, err);
1744 	} else {
1745 		printk("%s openwifi_dev_probe: ad9361_ctrl_outs_setup en_mask 0x%02x index 0x%02x\n",sdr_compatible_str, priv->ctrl_out.en_mask, priv->ctrl_out.index);
1746 	}
1747 
1748 	reg = ad9361_spi_read(priv->ad9361_phy->spi, REG_CTRL_OUTPUT_POINTER);
1749 	printk("%s openwifi_dev_probe: ad9361_spi_read REG_CTRL_OUTPUT_POINTER 0x%02x\n",sdr_compatible_str, reg);
1750 	reg = ad9361_spi_read(priv->ad9361_phy->spi, REG_CTRL_OUTPUT_ENABLE);
1751 	printk("%s openwifi_dev_probe: ad9361_spi_read REG_CTRL_OUTPUT_ENABLE 0x%02x\n",sdr_compatible_str, reg);
1752 
1753 	// //-------------find driver: axi_ad9361 hdl ref design module, dac channel---------------
1754 	tmp_dev = bus_find_device( &platform_bus_type, NULL, "cf-ad9361-dds-core-lpc", custom_match_platform_dev );
1755 	if (!tmp_dev) {
1756 		printk(KERN_ERR "%s bus_find_device platform_bus_type cf-ad9361-dds-core-lpc failed\n",sdr_compatible_str);
1757 		err = -ENOMEM;
1758 		goto err_free_dev;
1759 	}
1760 
1761 	tmp_pdev = to_platform_device(tmp_dev);
1762 	if (!tmp_pdev) {
1763 		printk(KERN_ERR "%s to_platform_device failed\n",sdr_compatible_str);
1764 		err = -ENOMEM;
1765 		goto err_free_dev;
1766 	}
1767 
1768 	tmp_indio_dev = platform_get_drvdata(tmp_pdev);
1769 	if (!tmp_indio_dev) {
1770 		printk(KERN_ERR "%s platform_get_drvdata failed\n",sdr_compatible_str);
1771 		err = -ENOMEM;
1772 		goto err_free_dev;
1773 	}
1774 
1775 	priv->dds_st = iio_priv(tmp_indio_dev);
1776 	if (!(priv->dds_st)) {
1777 		printk(KERN_ERR "%s iio_priv failed\n",sdr_compatible_str);
1778 		err = -ENOMEM;
1779 		goto err_free_dev;
1780 	}
1781 	printk("%s openwifi_dev_probe: cf-ad9361-dds-core-lpc dds_st->version %08x chip_info->name %s\n",sdr_compatible_str,priv->dds_st->version,priv->dds_st->chip_info->name);
1782 	cf_axi_dds_datasel(priv->dds_st, -1, DATA_SEL_DMA);
1783 	printk("%s openwifi_dev_probe: cf_axi_dds_datasel DATA_SEL_DMA\n",sdr_compatible_str);
1784 
1785 	// //-------------find driver: axi_ad9361 hdl ref design module, adc channel---------------
1786 	// turn off radio by muting tx
1787 	// ad9361_tx_mute(priv->ad9361_phy, 1);
1788 	// reg = ad9361_get_tx_atten(priv->ad9361_phy, 2);
1789 	// reg1 = ad9361_get_tx_atten(priv->ad9361_phy, 1);
1790 	// if (reg == AD9361_RADIO_OFF_TX_ATT && reg1 == AD9361_RADIO_OFF_TX_ATT ) {
1791 	// 	priv->rfkill_off = 0;// 0 off, 1 on
1792 	// 	printk("%s openwifi_dev_probe: rfkill radio off\n",sdr_compatible_str);
1793 	// }
1794 	// else
1795 	// 	printk("%s openwifi_dev_probe: WARNING rfkill radio off failed. tx att read %d %d require %d\n",sdr_compatible_str, reg, reg1, AD9361_RADIO_OFF_TX_ATT);
1796 
1797 	priv->rssi_correction = 43;//this will be set in real-time by _rf_set_channel()
1798 
1799 	//priv->rf_bw = 20000000; // Signal quality issue! NOT use for now. 20MHz or 40MHz. 40MHz need ddc/duc. 20MHz works in bypass mode
1800 	priv->rf_bw = 40000000; // 20MHz or 40MHz. 40MHz need ddc/duc. 20MHz works in bypass mode
1801 
1802 	priv->xpu_cfg = XPU_NORMAL;
1803 
1804 	priv->openofdm_tx_cfg = OPENOFDM_TX_NORMAL;
1805 	priv->openofdm_rx_cfg = OPENOFDM_RX_NORMAL;
1806 
1807 	printk("%s openwifi_dev_probe: priv->rf_bw == %dHz. bool for 20000000 %d, 40000000 %d\n",sdr_compatible_str, priv->rf_bw, (priv->rf_bw==20000000) , (priv->rf_bw==40000000) );
1808 	if (priv->rf_bw == 20000000) {
1809 		priv->rx_intf_cfg = RX_INTF_BYPASS;
1810 		priv->tx_intf_cfg = TX_INTF_BYPASS;
1811 		//priv->rx_freq_offset_to_lo_MHz = 0;
1812 		//priv->tx_freq_offset_to_lo_MHz = 0;
1813 	} else if (priv->rf_bw == 40000000) {
1814 		//priv->rx_intf_cfg = RX_INTF_BW_20MHZ_AT_P_10MHZ; //work
1815 		//priv->tx_intf_cfg = TX_INTF_BW_20MHZ_AT_N_10MHZ_ANT1; //work
1816 
1817 		// // test ddc at central, duc at central+10M. It works. And also change rx BW from 40MHz to 20MHz in rf_init.sh. Rx sampling rate is still 40Msps
1818 		priv->rx_intf_cfg = RX_INTF_BW_20MHZ_AT_0MHZ_ANT0;
1819 		priv->tx_intf_cfg = TX_INTF_BW_20MHZ_AT_N_10MHZ_ANT1;
1820 		// // try another antenna option
1821 		//priv->rx_intf_cfg = RX_INTF_BW_20MHZ_AT_0MHZ_ANT1;
1822 		//priv->tx_intf_cfg = TX_INTF_BW_20MHZ_AT_N_10MHZ_ANT0;
1823 
1824 		#if 0
1825 		if (priv->rx_intf_cfg == DDC_BW_20MHZ_AT_N_10MHZ) {
1826 			priv->rx_freq_offset_to_lo_MHz = -10;
1827 		} else if (priv->rx_intf_cfg == DDC_BW_20MHZ_AT_P_10MHZ) {
1828 			priv->rx_freq_offset_to_lo_MHz = 10;
1829 		} else if (priv->rx_intf_cfg == DDC_BW_20MHZ_AT_0MHZ) {
1830 			priv->rx_freq_offset_to_lo_MHz = 0;
1831 		} else {
1832 			printk("%s openwifi_dev_probe: Warning! priv->rx_intf_cfg == %d\n",sdr_compatible_str,priv->rx_intf_cfg);
1833 		}
1834 		#endif
1835 	} else {
1836 		printk("%s openwifi_dev_probe: Warning! priv->rf_bw == %dHz (should be 20000000 or 40000000)\n",sdr_compatible_str, priv->rf_bw);
1837 	}
1838 	priv->rx_freq_offset_to_lo_MHz = rx_intf_fo_mapping[priv->rx_intf_cfg];
1839 	priv->tx_freq_offset_to_lo_MHz = tx_intf_fo_mapping[priv->tx_intf_cfg];
1840 	printk("%s openwifi_dev_probe: test_mode %d\n", sdr_compatible_str, test_mode);
1841 
1842 	//let's by default turn radio on when probing
1843 	if (priv->tx_intf_cfg == TX_INTF_BW_20MHZ_AT_N_10MHZ_ANT1) {
1844 		ad9361_set_tx_atten(priv->ad9361_phy, AD9361_RADIO_ON_TX_ATT, false, true, true); // AD9361_RADIO_ON_TX_ATT 3000 means 3dB, 0 means 0dB
1845 		reg = ad9361_get_tx_atten(priv->ad9361_phy, 2);
1846 	} else {
1847 		ad9361_set_tx_atten(priv->ad9361_phy, AD9361_RADIO_ON_TX_ATT, true, false, true); // AD9361_RADIO_ON_TX_ATT 3000 means 3dB, 0 means 0dB
1848 		reg = ad9361_get_tx_atten(priv->ad9361_phy, 1);
1849 	}
1850 	if (reg == AD9361_RADIO_ON_TX_ATT) {
1851 		priv->rfkill_off = 1;// 0 off, 1 on
1852 		printk("%s openwifi_dev_probe: rfkill radio on\n",sdr_compatible_str);
1853 	}
1854 	else
1855 		printk("%s openwifi_dev_probe: WARNING rfkill radio on failed. tx att read %d require %d\n",sdr_compatible_str, reg, AD9361_RADIO_ON_TX_ATT);
1856 
1857 	memset(priv->drv_rx_reg_val,0,sizeof(priv->drv_rx_reg_val));
1858 	memset(priv->drv_tx_reg_val,0,sizeof(priv->drv_tx_reg_val));
1859 	memset(priv->drv_xpu_reg_val,0,sizeof(priv->drv_xpu_reg_val));
1860 
1861 	// //set ad9361 in certain mode
1862 	#if 0
1863 	err = ad9361_set_trx_clock_chain_freq(priv->ad9361_phy,priv->rf_bw);
1864 	printk("%s openwifi_dev_probe: ad9361_set_trx_clock_chain_freq %dHz err %d\n",sdr_compatible_str, priv->rf_bw,err);
1865 	err = ad9361_update_rf_bandwidth(priv->ad9361_phy,priv->rf_bw,priv->rf_bw);
1866 	printk("%s openwifi_dev_probe: ad9361_update_rf_bandwidth %dHz err %d\n",sdr_compatible_str, priv->rf_bw,err);
1867 
1868 	rx_intf_api->hw_init(priv->rx_intf_cfg,8,8);
1869 	tx_intf_api->hw_init(priv->tx_intf_cfg,8,8);
1870 	openofdm_tx_api->hw_init(priv->openofdm_tx_cfg);
1871 	openofdm_rx_api->hw_init(priv->openofdm_rx_cfg);
1872 	printk("%s openwifi_dev_probe: rx_intf_cfg %d openofdm_rx_cfg %d tx_intf_cfg %d openofdm_tx_cfg %d\n",sdr_compatible_str, priv->rx_intf_cfg, priv->openofdm_rx_cfg, priv->tx_intf_cfg, priv->openofdm_tx_cfg);
1873 	printk("%s openwifi_dev_probe: rx_freq_offset_to_lo_MHz %d tx_freq_offset_to_lo_MHz %d\n",sdr_compatible_str, priv->rx_freq_offset_to_lo_MHz, priv->tx_freq_offset_to_lo_MHz);
1874 	#endif
1875 
1876 	dev->max_rates = 1; //maximum number of alternate rate retry stages the hw can handle.
1877 
1878 	SET_IEEE80211_DEV(dev, &pdev->dev);
1879 	platform_set_drvdata(pdev, dev);
1880 
1881 	BUILD_BUG_ON(sizeof(priv->rates_2GHz) != sizeof(openwifi_2GHz_rates));
1882 	BUILD_BUG_ON(sizeof(priv->rates_5GHz) != sizeof(openwifi_5GHz_rates));
1883 	BUILD_BUG_ON(sizeof(priv->channels_2GHz) != sizeof(openwifi_2GHz_channels));
1884 	BUILD_BUG_ON(sizeof(priv->channels_5GHz) != sizeof(openwifi_5GHz_channels));
1885 
1886 	memcpy(priv->rates_2GHz, openwifi_2GHz_rates, sizeof(openwifi_2GHz_rates));
1887 	memcpy(priv->rates_5GHz, openwifi_5GHz_rates, sizeof(openwifi_5GHz_rates));
1888 	memcpy(priv->channels_2GHz, openwifi_2GHz_channels, sizeof(openwifi_2GHz_channels));
1889 	memcpy(priv->channels_5GHz, openwifi_5GHz_channels, sizeof(openwifi_5GHz_channels));
1890 
1891 	priv->band = BAND_5_8GHZ; //this can be changed by band _rf_set_channel() (2.4GHz ERP(OFDM)) (5GHz OFDM)
1892 	priv->channel = 44;  //currently useless. this can be changed by band _rf_set_channel()
1893 	priv->use_short_slot = false; //this can be changed by openwifi_bss_info_changed: BSS_CHANGED_ERP_SLOT
1894 
1895 	priv->band_2GHz.band = NL80211_BAND_2GHZ;
1896 	priv->band_2GHz.channels = priv->channels_2GHz;
1897 	priv->band_2GHz.n_channels = ARRAY_SIZE(priv->channels_2GHz);
1898 	priv->band_2GHz.bitrates = priv->rates_2GHz;
1899 	priv->band_2GHz.n_bitrates = ARRAY_SIZE(priv->rates_2GHz);
1900 	dev->wiphy->bands[NL80211_BAND_2GHZ] = &(priv->band_2GHz);
1901 
1902 	priv->band_5GHz.band = NL80211_BAND_5GHZ;
1903 	priv->band_5GHz.channels = priv->channels_5GHz;
1904 	priv->band_5GHz.n_channels = ARRAY_SIZE(priv->channels_5GHz);
1905 	priv->band_5GHz.bitrates = priv->rates_5GHz;
1906 	priv->band_5GHz.n_bitrates = ARRAY_SIZE(priv->rates_5GHz);
1907 	dev->wiphy->bands[NL80211_BAND_5GHZ] = &(priv->band_5GHz);
1908 
1909 	printk("%s openwifi_dev_probe: band_2GHz.n_channels %d n_bitrates %d band_5GHz.n_channels %d n_bitrates %d\n",sdr_compatible_str,
1910 	priv->band_2GHz.n_channels,priv->band_2GHz.n_bitrates,priv->band_5GHz.n_channels,priv->band_5GHz.n_bitrates);
1911 
1912 	ieee80211_hw_set(dev, HOST_BROADCAST_PS_BUFFERING);
1913 	ieee80211_hw_set(dev, RX_INCLUDES_FCS);
1914 	ieee80211_hw_set(dev, BEACON_TX_STATUS);
1915 
1916 	dev->vif_data_size = sizeof(struct openwifi_vif);
1917 	dev->wiphy->interface_modes =
1918 			BIT(NL80211_IFTYPE_MONITOR)|
1919 			BIT(NL80211_IFTYPE_P2P_GO) |
1920 			BIT(NL80211_IFTYPE_P2P_CLIENT) |
1921 			BIT(NL80211_IFTYPE_AP) |
1922 			BIT(NL80211_IFTYPE_STATION) |
1923 			BIT(NL80211_IFTYPE_ADHOC) |
1924 			BIT(NL80211_IFTYPE_MESH_POINT) |
1925 			BIT(NL80211_IFTYPE_OCB);
1926 	dev->wiphy->iface_combinations = &openwifi_if_comb;
1927 	dev->wiphy->n_iface_combinations = 1;
1928 
1929 	dev->wiphy->regulatory_flags = (REGULATORY_STRICT_REG|REGULATORY_CUSTOM_REG); // use our own config within strict regulation
1930 	//dev->wiphy->regulatory_flags = REGULATORY_CUSTOM_REG; // use our own config
1931 	wiphy_apply_custom_regulatory(dev->wiphy, &sdr_regd);
1932 
1933 	chip_name = "ZYNQ";
1934 
1935 	/* we declare to MAC80211 all the queues except for beacon queue
1936 	 * that will be eventually handled by DRV.
1937 	 * TX rings are arranged in such a way that lower is the IDX,
1938 	 * higher is the priority, in order to achieve direct mapping
1939 	 * with mac80211, however the beacon queue is an exception and it
1940 	 * is mapped on the highst tx ring IDX.
1941 	 */
1942 	dev->queues = 1;
1943 
1944 	ieee80211_hw_set(dev, SIGNAL_DBM);
1945 
1946 	wiphy_ext_feature_set(dev->wiphy, NL80211_EXT_FEATURE_CQM_RSSI_LIST);
1947 
1948 	priv->rf = &ad9361_rf_ops;
1949 
1950 	memset(priv->dest_mac_addr_queue_map,0,sizeof(priv->dest_mac_addr_queue_map));
1951 
1952 	get_random_bytes(&rand_val, sizeof(rand_val));
1953     rand_val%=250;
1954 	priv->mac_addr[0]=0x66;	priv->mac_addr[1]=0x55;	priv->mac_addr[2]=0x44;	priv->mac_addr[3]=0x33;	priv->mac_addr[4]=0x22;
1955 	priv->mac_addr[5]=rand_val+1;
1956 	//priv->mac_addr[5]=0x11;
1957 	if (!is_valid_ether_addr(priv->mac_addr)) {
1958 		printk(KERN_WARNING "%s openwifi_dev_probe: WARNING Invalid hwaddr! Using randomly generated MAC addr\n",sdr_compatible_str);
1959 		eth_random_addr(priv->mac_addr);
1960 	} else {
1961 		printk("%s openwifi_dev_probe: mac_addr %02x:%02x:%02x:%02x:%02x:%02x\n",sdr_compatible_str,priv->mac_addr[0],priv->mac_addr[1],priv->mac_addr[2],priv->mac_addr[3],priv->mac_addr[4],priv->mac_addr[5]);
1962 	}
1963 	SET_IEEE80211_PERM_ADDR(dev, priv->mac_addr);
1964 
1965 	spin_lock_init(&priv->lock);
1966 
1967 	err = ieee80211_register_hw(dev);
1968 	if (err) {
1969 		pr_err(KERN_ERR "%s openwifi_dev_probe: WARNING Cannot register device\n",sdr_compatible_str);
1970 		goto err_free_dev;
1971 	} else {
1972 		printk("%s openwifi_dev_probe: ieee80211_register_hw %d\n",sdr_compatible_str, err);
1973 	}
1974 
1975 	// // //--------------------hook leds (not complete yet)--------------------------------
1976 	// tmp_dev = bus_find_device( &platform_bus_type, NULL, "leds", custom_match_platform_dev ); //leds is the name in devicetree, not "compatiable" field
1977 	// if (!tmp_dev) {
1978 	// 	printk(KERN_ERR "%s bus_find_device platform_bus_type leds-gpio failed\n",sdr_compatible_str);
1979 	// 	err = -ENOMEM;
1980 	// 	goto err_free_dev;
1981 	// }
1982 
1983 	// tmp_pdev = to_platform_device(tmp_dev);
1984 	// if (!tmp_pdev) {
1985 	// 	printk(KERN_ERR "%s to_platform_device failed for leds-gpio\n",sdr_compatible_str);
1986 	// 	err = -ENOMEM;
1987 	// 	goto err_free_dev;
1988 	// }
1989 
1990 	// tmp_led_priv = platform_get_drvdata(tmp_pdev);
1991 	// if (!tmp_led_priv) {
1992 	// 	printk(KERN_ERR "%s platform_get_drvdata failed for leds-gpio\n",sdr_compatible_str);
1993 	// 	err = -ENOMEM;
1994 	// 	goto err_free_dev;
1995 	// }
1996 	// printk("%s openwifi_dev_probe: leds-gpio detect %d leds!\n",sdr_compatible_str, tmp_led_priv->num_leds);
1997 	// if (tmp_led_priv->num_leds!=4){
1998 	// 	printk(KERN_ERR "%s WARNING we expect 4 leds, but actual %d leds\n",sdr_compatible_str,tmp_led_priv->num_leds);
1999 	// 	err = -ENOMEM;
2000 	// 	goto err_free_dev;
2001 	// }
2002 	// gpiod_set_value(tmp_led_priv->leds[0].gpiod, 1);//light it
2003 	// gpiod_set_value(tmp_led_priv->leds[3].gpiod, 0);//black it
2004 	// priv->num_led = tmp_led_priv->num_leds;
2005 	// priv->led[0] = &(tmp_led_priv->leds[0].cdev);
2006 	// priv->led[1] = &(tmp_led_priv->leds[1].cdev);
2007 	// priv->led[2] = &(tmp_led_priv->leds[2].cdev);
2008 	// priv->led[3] = &(tmp_led_priv->leds[3].cdev);
2009 
2010 	// snprintf(priv->led_name[0], OPENWIFI_LED_MAX_NAME_LEN, "openwifi-%s::radio", wiphy_name(dev->wiphy));
2011 	// snprintf(priv->led_name[1], OPENWIFI_LED_MAX_NAME_LEN, "openwifi-%s::assoc", wiphy_name(dev->wiphy));
2012 	// snprintf(priv->led_name[2], OPENWIFI_LED_MAX_NAME_LEN, "openwifi-%s::tx", wiphy_name(dev->wiphy));
2013 	// snprintf(priv->led_name[3], OPENWIFI_LED_MAX_NAME_LEN, "openwifi-%s::rx", wiphy_name(dev->wiphy));
2014 
2015 	wiphy_info(dev->wiphy, "hwaddr %pm, %s + %s\n",
2016 		   priv->mac_addr, chip_name, priv->rf->name);
2017 
2018 	openwifi_rfkill_init(dev);
2019 	return 0;
2020 
2021  err_free_dev:
2022 	ieee80211_free_hw(dev);
2023 
2024 	return err;
2025 }
2026 
2027 static int openwifi_dev_remove(struct platform_device *pdev)
2028 {
2029 	struct ieee80211_hw *dev = platform_get_drvdata(pdev);
2030 
2031 	if (!dev) {
2032 		pr_info("%s openwifi_dev_remove: dev %d\n", sdr_compatible_str, (u32)dev);
2033 		return(-1);
2034 	}
2035 
2036 	openwifi_rfkill_exit(dev);
2037 	ieee80211_unregister_hw(dev);
2038 	ieee80211_free_hw(dev);
2039 	return(0);
2040 }
2041 
2042 static struct platform_driver openwifi_dev_driver = {
2043 	.driver = {
2044 		.name = "sdr,sdr",
2045 		.owner = THIS_MODULE,
2046 		.of_match_table = openwifi_dev_of_ids,
2047 	},
2048 	.probe = openwifi_dev_probe,
2049 	.remove = openwifi_dev_remove,
2050 };
2051 
2052 module_platform_driver(openwifi_dev_driver);
2053