wifi->esp8089:
[firefly-linux-kernel-4.4.55.git] / drivers / net / wireless / rockchip_wlan / esp8089 / esp_driver / testmode.c
1 /*
2  * Copyright (c) 2011-2014 Espressif System.
3  *
4  * test mode    
5  */
6
7 #ifdef TEST_MODE
8
9 #include <linux/kernel.h>
10 #include <linux/etherdevice.h>
11 #include <linux/workqueue.h>
12 #include <linux/completion.h>
13 #include <linux/nl80211.h>
14 #include <linux/ieee80211.h>
15 #include <linux/slab.h>
16 #include <linux/sched.h>
17 #include <linux/kthread.h>
18 #include <linux/mmc/host.h>
19 #include <net/cfg80211.h>
20 #include <net/mac80211.h>
21 #include <net/genetlink.h>
22 #include "esp_pub.h"
23 #include "esp_sip.h"
24 #include "esp_ctrl.h"
25 #include "esp_sif.h"
26 #include "esp_debug.h"
27 #include "esp_wl.h"
28 #include "testmode.h"
29 #include "esp_path.h"
30 #include "esp_file.h"
31 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 31))
32     #include <net/regulatory.h>
33 #endif
34
35 static int queue_flag = 0;
36
37 static u32 connected_nl;
38 static struct genl_info info_copy;
39 static struct esp_sip *sip_copy = NULL;
40 static u8 *sdio_buff = NULL;
41 static struct sdiotest_param sdioTest;
42 static u8 *sdiotest_buf = NULL;
43
44 #define SIP sip_copy
45 #define OUT_DONE() \
46         do { \
47              printk(KERN_DEBUG "esp_sdio: error occured in %s\n", __func__); \
48         } while(0)
49
50 /* ESP TEST netlinf family */
51 static struct genl_family test_genl_family = {
52         .id = GENL_ID_GENERATE,
53         .hdrsize = 0,
54         .name = "esp_sdio",
55         .version = 1,
56         .maxattr = TEST_ATTR_MAX,
57 };
58
59 struct loopback_param_s {
60         u32 packet_num;
61         u32 packet_id;
62 };
63
64 static struct loopback_param_s loopback_param;
65 u32 get_loopback_num()
66 {
67         return loopback_param.packet_num;
68 }
69
70 u32 get_loopback_id()
71 {
72         return loopback_param.packet_id;
73 }
74
75 void inc_loopback_id()
76 {
77         loopback_param.packet_id++;
78 }
79
80 #define REGISTER_REPLY(info) \
81         memcpy((char *)&info_copy, (char *)(info), sizeof(struct genl_info))
82
83
84 static void sip_send_test_cmd(struct esp_sip *sip, struct sk_buff *skb)
85 {
86         if (queue_flag == 0)
87                 skb_queue_tail(&sip->epub->txq, skb);
88         else
89                 skb_queue_head(&sip->epub->txq, skb);
90
91 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 32)
92         if(sif_get_ate_config() == 0){
93             ieee80211_queue_work(sip->epub->hw, &sip->epub->tx_work);
94         } else {
95             queue_work(sip->epub->esp_wkq, &sip->epub->tx_work);
96         } 
97 #else
98         queue_work(sip->epub->esp_wkq, &sip->epub->tx_work);
99 #endif
100
101 }
102
103 static int esp_test_cmd_reply(struct genl_info *info, u32 cmd_type, char *reply_info)
104 {
105         struct sk_buff *skb;
106         void *hdr;
107
108         /*directly send ask_info to target, and waiting for report*/
109         skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
110         if (skb == NULL)
111                 goto out;
112
113 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 8, 0))
114         hdr = genlmsg_put(skb,  info->snd_portid, info->snd_seq, &test_genl_family, 0, cmd_type);
115 #else
116         hdr = genlmsg_put(skb,  info->snd_pid, info->snd_seq, &test_genl_family, 0, cmd_type);
117 #endif
118         if (hdr == NULL)
119                 goto nla_put_failure;
120
121 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 6, 0))
122         nla_put_string(skb, TEST_ATTR_STR, reply_info);
123 #else
124         NLA_PUT_STRING(skb, TEST_ATTR_STR, reply_info);
125 #endif
126         genlmsg_end(skb, hdr);
127         genlmsg_reply(skb, info);
128         return 0;
129
130 nla_put_failure:
131         nlmsg_free(skb);
132 out:
133         OUT_DONE();
134         return -EINVAL;
135 }
136
137 static int esp_test_echo(struct sk_buff *skb_2,
138                          struct genl_info *info)
139 {
140         char *echo_info;
141         int res;
142
143         if (info == NULL)
144                 goto out;
145
146 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 8, 0))
147         connected_nl = info->snd_portid;
148         printk(KERN_DEBUG "esp_sdio: received a echo, "
149                "from portid %d\n", info->snd_portid);
150 #else
151         connected_nl = info->snd_pid;
152         esp_dbg(ESP_DBG_ERROR, "esp_sdio: received a echo, "
153                "from pid %d\n", info->snd_pid);
154 #endif
155         sip_debug_show(SIP);
156         
157         /*get echo info*/
158         echo_info = nla_data(info->attrs[TEST_ATTR_STR]);
159
160         if (strncmp(echo_info, "queue_head", 10) == 0) {
161                 esp_dbg(ESP_DBG_ERROR, "echo : change to queue head");
162                 queue_flag = 1;
163         }
164         if (strncmp(echo_info, "queue_tail", 10) == 0) {
165                 esp_dbg(ESP_DBG_ERROR, "echo : change to queue head");
166                 queue_flag = 0;
167         }
168
169         res=esp_test_cmd_reply(info, TEST_CMD_ECHO, echo_info);
170         return res;
171 out:
172         OUT_DONE();
173         return -EINVAL;
174 }
175
176 static int esp_test_sdiospeed(struct sk_buff *skb_2,
177                               struct genl_info *info)
178 {
179         char *speed_info;
180         int res;
181
182         if (info == NULL)
183                 goto out;
184
185 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 8, 0))
186         connected_nl = info->snd_portid;
187 #else
188         connected_nl = info->snd_pid;
189 #endif
190         /*get echo info*/
191         speed_info = nla_data(info->attrs[TEST_ATTR_STR]);
192 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 8, 0))
193         esp_dbg(ESP_DBG_ERROR, "esp_sdio: received a sdio speed %s, "
194                "from portid %d\n", speed_info, info->snd_portid);
195 #else
196         esp_dbg(ESP_DBG_ERROR, "esp_sdio: received a sdio speed %s, "
197                "from pid %d\n", speed_info, info->snd_pid);
198 #endif
199         if (!strcmp(speed_info, "high")) {
200                 sif_platform_target_speed(1);
201         } else if (!strcmp(speed_info, "low")) {
202                 sif_platform_target_speed(0);
203         } else {
204                 esp_dbg(ESP_DBG_ERROR, "%s:  %s unsupported\n", __func__, speed_info);
205         }
206
207         res=esp_test_cmd_reply(info, TEST_CMD_SDIOSPEED, speed_info);
208         return res;
209 out:
210         OUT_DONE();
211         return -EINVAL;
212 }
213
214 static void * ate_done_data;
215 static char ate_reply_str[128];
216 void esp_test_ate_done_cb(char *ep)
217 {
218         memset(ate_reply_str, 0, sizeof(ate_reply_str));
219         strcpy(ate_reply_str, ep);
220
221         esp_dbg(ESP_DBG_ERROR, "%s %s\n", __func__, ate_reply_str);
222
223         if (ate_done_data)
224                 complete(ate_done_data);
225 }
226
227 static int esp_test_ate(struct sk_buff *skb_2,
228                          struct genl_info *info)
229 {
230         char *ate_info = NULL;
231         u16 len = 0;
232         int res = 0;
233         struct sk_buff *skb;
234         char *str;
235         bool stop_sdt = false;
236
237         DECLARE_COMPLETION_ONSTACK(complete);
238
239         if (info == NULL)
240                 goto out;
241
242
243         ate_done_data = &complete;
244 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 8, 0))
245         connected_nl = info->snd_portid;
246 #else
247         connected_nl = info->snd_pid;
248 #endif
249
250         //esp_dbg(ESP_DBG_ERROR, "esp_sdio: received a ate cmd, "
251         //       "from pid %d\n", info->snd_pid);
252
253         /*get echo info*/
254         ate_info = nla_data(info->attrs[TEST_ATTR_STR]);
255         if (!ate_info)
256                 goto out;
257
258         len = nla_len(info->attrs[TEST_ATTR_STR]);
259
260         //esp_dbg(ESP_DBG_ERROR, "%s str %s len %d cmd %d\n", __func__, ate_info, len, SIP_CMD_ATE);
261
262         skb = sip_alloc_ctrl_skbuf(SIP, (sizeof(struct sip_hdr)+len), SIP_CMD_ATE);
263         if (!skb)
264                 goto out;
265
266         //atecmd = (struct sip_cmd_ate*)(skb->data + sizeof(struct sip_hdr));
267         str = (char *)(skb->data + sizeof(struct sip_hdr));
268         //atecmd->len = len;
269         strcpy(str, ate_info);
270         //esp_dbg(ESP_DBG_ERROR, "%s cmdstr %s \n", __func__, str);
271
272         if (atomic_read(&sdioTest.start)) {
273                 atomic_set(&sdioTest.start, 0);
274                 stop_sdt = true;
275                 msleep(10);
276                 //esp_dbg(ESP_DBG_ERROR, "%s stop sdt \n", __func__);
277         }
278
279         sip_send_test_cmd(SIP, skb);
280         //esp_dbg(ESP_DBG_ERROR, "%s sent cmd \n", __func__);
281
282         wait_for_completion(&complete);
283         
284         //esp_dbg(ESP_DBG_ERROR, "%s completed \n", __func__);
285
286         esp_test_cmd_reply(info, TEST_CMD_ATE, ate_reply_str);
287
288         if (stop_sdt) {
289                 //esp_dbg(ESP_DBG_ERROR, "%s restart sdt \n", __func__);
290                 atomic_set(&sdioTest.start, 1);
291                 wake_up_process(sdioTest.thread);
292         }
293
294         return res;
295 out:
296         OUT_DONE();
297         return -EINVAL;
298 }
299
300 static int esp_process_sdio_test(struct sdiotest_param *param)
301 {
302 //#define sdiotest_BASE_ADDR 0x7f80 //MAC 5.0
303 #define SDIOTEST_FLAG_ADDR 0xc000
304 #define SDIOTEST_BASE_ADDR (SDIOTEST_FLAG_ADDR+4) //MAC 6.0
305 #define SDIO_BUF_SIZE (16*1024-4)
306
307         int ret = 0;
308         static int counter = 0;
309
310         //esp_dbg(ESP_DBG_ERROR, "idle_period %d mode %d addr 0x%08x\n", param->idle_period,
311 //                                                      param->mode, param->addr);
312         if (sdioTest.mode == 1) { //read mode
313                 ret = esp_common_read_with_addr(SIP->epub, SDIOTEST_BASE_ADDR, sdiotest_buf, SDIO_BUF_SIZE, ESP_SIF_SYNC);
314
315         } else if ((sdioTest.mode >= 3)&&(sdioTest.mode <= 7)) { //write mode
316                 ret = esp_common_write_with_addr(SIP->epub, SDIOTEST_BASE_ADDR, sdiotest_buf, SDIO_BUF_SIZE, ESP_SIF_SYNC);
317 /*
318         } else if (sdioTest.mode == 3) { //read & write mode
319                 ret = esp_common_read_with_addr(SIP->epub, SDIOTEST_BASE_ADDR, sdiotest_buf, 16*1024, ESP_SIF_SYNC);
320                 sdiotest_buf[0]++;
321                 ret = esp_common_write_with_addr(SIP->epub, SDIOTEST_BASE_ADDR, sdiotest_buf, 16*1024, ESP_SIF_SYNC);
322 */
323         } else if (sdioTest.mode == 2) { //byte read mode(to test sdio_cmd)
324                 ret = esp_common_read_with_addr(SIP->epub, SDIOTEST_BASE_ADDR, sdiotest_buf, 8, ESP_SIF_SYNC);
325         }
326         
327         if (sdioTest.idle_period > 1000) {
328                 show_buf(sdiotest_buf, 128);
329         } else if (sdioTest.idle_period == 0) {
330                 if (sdioTest.mode == 1) {//read mode
331                         if (!(counter++%5000)) {
332                                 esp_dbg(ESP_DBG_ERROR, "%s %d\n", __func__, counter);
333                         }
334                 } else if (sdioTest.mode == 2) { //byte read mode
335                         if (!(counter++%5000)) {
336                                 esp_dbg(ESP_DBG_ERROR, "%s %d\n", __func__, counter);
337                         }
338                 } else {//write mode
339                         //msleep(3);
340                         if (!(counter++%30000)) {
341                                 esp_dbg(ESP_DBG_ERROR, "%s %d\n", __func__, counter);
342                         }
343                 }
344         } else {
345                 if (!(counter++%1000)) {
346                         esp_dbg(ESP_DBG_ERROR, "%s %d\n", __func__, counter);
347                 }
348         }
349
350
351         if (ret)
352                 esp_dbg(ESP_DBG_ERROR, "%s mode %d err %d \n", __func__, sdioTest.mode, ret);   
353
354         return ret;
355 }       
356
357 static int esp_test_sdiotest_thread(void *param)
358 {
359         struct sdiotest_param *testParam = (struct sdiotest_param *)param;
360         struct sched_param schedParam = { .sched_priority = 1 };
361         unsigned long idle_period = MAX_SCHEDULE_TIMEOUT;
362         int ret = 0;
363
364         sched_setscheduler(current, SCHED_FIFO, &schedParam);
365
366         while (!kthread_should_stop()) {
367                 
368                 if (0 == atomic_read(&testParam->start)) {
369                         esp_dbg(ESP_DBG_ERROR, "%s suspend\n", __func__);
370                         set_current_state(TASK_INTERRUPTIBLE);
371                         if (!kthread_should_stop())
372                                 schedule_timeout(MAX_SCHEDULE_TIMEOUT);
373                         set_current_state(TASK_RUNNING);
374                 }
375
376                 if (testParam->idle_period)
377                         idle_period = msecs_to_jiffies(testParam->idle_period);
378                 else 
379                         idle_period = 0;
380
381                 ret = esp_process_sdio_test(testParam);
382
383                 /*
384                  * Give other threads a chance to run in the presence of
385                  * errors.
386                  */
387                 if (ret < 0) {
388                         set_current_state(TASK_INTERRUPTIBLE);
389                         if (!kthread_should_stop())
390                                 schedule_timeout(2*HZ);
391                         set_current_state(TASK_RUNNING);
392                         atomic_set(&testParam->start, 0);
393                 }
394
395                 //esp_dbg(ESP_DBG_ERROR, "%s idle_period %lu\n", __func__, idle_period);
396                 if (idle_period) {
397                         set_current_state(TASK_INTERRUPTIBLE);
398                         if (!kthread_should_stop()) {
399                                 schedule_timeout(idle_period);
400                         }
401                         set_current_state(TASK_RUNNING);
402                 }
403         };
404
405
406         esp_dbg(ESP_DBG_ERROR, "%s exit\n", __func__);
407
408         return ret;
409
410 }
411
412 static int esp_test_sdiotest(struct sk_buff *skb_2,
413                          struct genl_info *info)
414 {
415         int res = 0;
416         char reply_str[32];
417         u32 start = 0;
418         int para_num = 0;
419         int i;
420         u32 data_mask = 0xffffffff;
421         u8 * sdio_test_flag = NULL;
422         int ret = 0;
423         bool stop_sdt = false;
424
425         if (info == NULL)
426                 goto out;
427
428         start = nla_get_u32(info->attrs[TEST_ATTR_PARA0]);
429         esp_dbg(ESP_DBG_ERROR, "%s start 0x%08x\n", __func__, start);
430         para_num = start & 0xf;
431         start >>= 31;
432         if (!start)
433                 goto _turnoff;
434
435         esp_dbg(ESP_DBG_ERROR, "%s paranum %d start %u\n", __func__, para_num, start);
436         para_num--;
437
438         do {
439                 if ((para_num--) > 0) {
440                         sdioTest.mode = nla_get_u32(info->attrs[TEST_ATTR_PARA1]);
441                         if ((sdioTest.mode >= 3)&&(sdioTest.mode <= 7)) { // write mode, fill the test buf
442                                 data_mask = (sdioTest.mode == 3)? 0xffffffff : (0x11111111<<(sdioTest.mode-4));
443                                 for (i = 0; i<SDIO_BUF_SIZE/ 4; i++) {
444 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 10, 0))
445
446                                         *(u32 *)&sdiotest_buf[i*4] = get_random_int() & data_mask;
447 #else
448                                         *(u32 *)&sdiotest_buf[i*4] = random32() & data_mask;
449 #endif
450                                 }
451                         }
452                         esp_dbg(ESP_DBG_ERROR, "%s mode %d \n", __func__, sdioTest.mode);
453                 }
454
455                 if ((para_num--) > 0) {
456                         sdioTest.addr = nla_get_u32(info->attrs[TEST_ATTR_PARA2]);
457                         esp_dbg(ESP_DBG_ERROR, "%s addr 0x%x \n", __func__, sdioTest.addr);
458                 }
459
460                 if ((para_num--) > 0) {
461                         sdioTest.idle_period = nla_get_u32(info->attrs[TEST_ATTR_PARA3]);
462                         esp_dbg(ESP_DBG_ERROR, "%s idle_period %u \n", __func__, sdioTest.idle_period);
463                 }
464         } while (0);
465         
466         esp_test_cmd_reply(info, TEST_CMD_SDIOTEST, reply_str);
467         msleep(10);
468   
469 #if 1
470         if (start == 1) {
471                 //esp_dbg(ESP_DBG_ERROR, "%s start== 1\n", __func__);
472                 
473                 if((sdioTest.mode == 8)|(sdioTest.mode == 9)) {
474                    
475                    if (atomic_read(&sdioTest.start)) {
476                       atomic_set(&sdioTest.start, 0);
477                       stop_sdt = true;
478                       msleep(10);
479                    }
480
481                    sdio_test_flag = kzalloc(4, GFP_KERNEL);
482                    if (sdio_test_flag == NULL) {
483                       esp_dbg(ESP_DBG_ERROR, "no mem for sdio_tst_flag!!\n");
484                       goto out;
485                    }
486
487                    if(sdioTest.mode == 8){
488                       ret = esp_common_read_with_addr(SIP->epub, sdioTest.addr, sdio_test_flag, 4, ESP_SIF_SYNC);
489
490                       esp_dbg(ESP_DBG_ERROR, "%s sdio read: 0x%x 0x%x\n", __func__, sdioTest.addr, *(u32 *)sdio_test_flag);
491                    }else{
492
493                       *(u32 *)sdio_test_flag = sdioTest.idle_period;
494
495                       ret = esp_common_write_with_addr(SIP->epub, sdioTest.addr, sdio_test_flag, 4, ESP_SIF_SYNC);
496                       
497                       esp_dbg(ESP_DBG_ERROR, "%s sdio : write 0x%x 0x%x done!\n", __func__, sdioTest.addr, *(u32 *)sdio_test_flag);
498                    }
499
500                    kfree(sdio_test_flag);
501                    sdio_test_flag = NULL; 
502
503                 } else {
504                    //set flag to inform firmware sdio-test start
505                    sdio_test_flag = kzalloc(4, GFP_KERNEL);
506                    if (sdio_test_flag == NULL) {
507                         esp_dbg(ESP_DBG_ERROR, "no mem for sdio_tst_flag!!\n");
508                         goto out;
509                    } 
510                
511                    *(u32 *)sdio_test_flag = 0x56781234;
512                    if (atomic_read(&sdioTest.start)) {
513                       atomic_set(&sdioTest.start, 0);
514                       stop_sdt = true;
515                       msleep(10);
516                       //esp_dbg(ESP_DBG_ERROR, "%s stop sdt \n", __func__);
517                    }
518                    ret = esp_common_write_with_addr(SIP->epub, SDIOTEST_FLAG_ADDR, sdio_test_flag, 4, ESP_SIF_SYNC);
519 /*
520                 if (stop_sdt) {
521                    //esp_dbg(ESP_DBG_ERROR, "%s restart sdt \n", __func__);
522                    atomic_set(&sdioTest.start, 1);
523                    wake_up_process(sdioTest.thread);
524                 }
525 */
526                 //esp_dbg(ESP_SHOW, "%s sdio_test_flag sent to target \n", __func__);
527                 //esp_dbg(ESP_DBG_ERROR, "%s sdio_test_flag sent to target \n", __func__);
528                    kfree(sdio_test_flag);
529                    sdio_test_flag = NULL;
530
531                    atomic_set(&sdioTest.start, 1);
532                    if (sdioTest.thread == NULL) {
533                         sdioTest.thread = kthread_run(esp_test_sdiotest_thread, 
534                                 &sdioTest, 
535                                 "kespsdiotestd");
536                    } else {
537                         wake_up_process(sdioTest.thread);
538                    }
539                    strcpy(reply_str, "sdt started\n");
540                 }
541         } else {
542                 //esp_dbg(ESP_DBG_ERROR, "%s start== 0\n", __func__);
543 _turnoff:               
544                 atomic_set(&sdioTest.start, 0);
545                 strcpy(reply_str, "sdt stopped\n");
546         }
547 #endif
548
549
550         return res;
551 out:
552         OUT_DONE();
553         return -EINVAL;
554 }
555
556
557 static int esp_test_ask(struct sk_buff *skb_2,
558                         struct genl_info *info)
559 {
560         char *ask_info;
561         int res;
562
563         if (info == NULL)
564                 goto out;
565
566         /*get echo info*/
567         ask_info = nla_data(info->attrs[TEST_ATTR_STR]);
568
569         /*directly send ask_info to target, and waiting for report*/
570         res=esp_test_cmd_reply(info, TEST_CMD_ASK, "ok");
571         return res;
572 out:
573         OUT_DONE();
574         return -EINVAL;
575 }
576
577 static int esp_test_sleep(struct sk_buff *skb_2,
578                           struct genl_info *info)
579 {
580         struct sip_cmd_sleep *sleepcmd;
581         struct sk_buff *skb = NULL;
582         int res;
583
584         if (info == NULL)
585                 goto out;
586
587         skb = sip_alloc_ctrl_skbuf(SIP, sizeof(struct sip_cmd_sleep), SIP_CMD_SLEEP);
588         if (!skb)
589                 goto out;
590
591         sleepcmd = (struct sip_cmd_sleep *)(skb->data + sizeof(struct sip_tx_info));
592         sleepcmd->sleep_mode       =  nla_get_u32(info->attrs[TEST_ATTR_PARA0]);
593         sleepcmd->sleep_tm_ms     =  nla_get_u32(info->attrs[TEST_ATTR_PARA1]);
594         sleepcmd->wakeup_tm_ms  =  nla_get_u32(info->attrs[TEST_ATTR_PARA2]);
595         sleepcmd->sleep_times       =  nla_get_u32(info->attrs[TEST_ATTR_PARA3]);
596
597         sip_send_test_cmd(SIP, skb);
598
599         /*directly send ask_info to target, and waiting for report*/
600         res=esp_test_cmd_reply(info, TEST_CMD_SLEEP, "ok");
601         return res;
602 out:
603         OUT_DONE();
604         return -EINVAL;
605 }
606
607 static int esp_test_wakeup(struct sk_buff *skb_2,
608                            struct genl_info *info)
609 {
610         struct sip_cmd_wakeup *wakeupcmd;
611         struct sk_buff *skb = NULL;
612         //int res;
613
614         if (info == NULL)
615                 goto out;
616
617         skb = sip_alloc_ctrl_skbuf(SIP, sizeof(struct sip_cmd_wakeup), SIP_CMD_WAKEUP);
618         if (!skb)
619                 goto out;
620         wakeupcmd = (struct sip_cmd_wakeup *)(skb->data + sizeof(struct sip_tx_info));
621         wakeupcmd->check_data = nla_get_u32(info->attrs[TEST_ATTR_PARA0]);
622
623         /*directly send reply_info to target, and waiting for report*/
624         REGISTER_REPLY(info);
625         //res=esp_test_cmd_reply(info, TEST_CMD_WAKEUP, "ok");
626
627         sip_send_test_cmd(SIP, skb);
628         return 0;
629 out:
630         OUT_DONE();
631         return -EINVAL;
632 }
633
634 static int esp_test_loopback(struct sk_buff *skb_2,
635                              struct genl_info *info)
636 {
637         u32 txpacket_len;
638         u32 rxpacket_len;
639
640         if (info == NULL)
641                 goto out;
642
643         txpacket_len   = nla_get_u32(info->attrs[TEST_ATTR_PARA0]);
644         rxpacket_len   = nla_get_u32(info->attrs[TEST_ATTR_PARA1]);
645         loopback_param.packet_num    = nla_get_u32(info->attrs[TEST_ATTR_PARA2]);
646         loopback_param.packet_id=0;
647         REGISTER_REPLY(info);
648         return sip_send_loopback_mblk(SIP, txpacket_len, rxpacket_len, 0);
649 out:
650         OUT_DONE();
651         return -EINVAL;
652 }
653
654 /*
655 u8 probe_req_frm[] = {0x40,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x00,0x03,0x8F,0x11,0x22,0x88,
656                       0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00,0x01,0x08,0x82,0x84,0x8B,0x96,
657                       0x0C,0x12,0x18,0x24,0x32,0x04,0x30,0x48,0x60,0x6C
658                      };
659 */
660
661 static int sip_send_tx_frame(struct esp_sip *sip, u32 packet_len)
662 {
663
664         struct sk_buff *skb = NULL;
665         u8 *ptr = NULL;
666         int i;
667
668         skb = alloc_skb(packet_len, GFP_KERNEL);
669         skb->len = packet_len;
670         ptr = skb->data;
671         /* fill up pkt payload */
672         for (i = 0; i < skb->len; i++) {
673                 ptr[i] = i;
674         }
675
676                 if(sif_get_ate_config() == 0){
677                         sip_tx_data_pkt_enqueue(sip->epub, skb);
678 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 32))
679                 ieee80211_queue_work(sip->epub->hw, &sip->epub->tx_work);
680 #else
681                 queue_work(sip->epub->esp_wkq, &sip->epub->tx_work);
682 #endif
683         } else {
684                 skb_queue_tail(&sip->epub->txq, skb);
685             queue_work(sip->epub->esp_wkq, &sip->epub->tx_work);
686         }
687     
688         return 0;
689 }
690
691 static int esp_test_tx(struct sk_buff *skb_2,
692                        struct genl_info *info)
693 {
694         u32 txpacket_len;
695         u32 res;
696
697         if (info == NULL)
698                 goto out;
699
700         txpacket_len     = nla_get_u32(info->attrs[TEST_ATTR_PARA0]);
701         REGISTER_REPLY(info);
702         sip_send_tx_frame(SIP, txpacket_len);
703         res=esp_test_cmd_reply(info, TEST_CMD_TX, "tx out");
704         return res;
705 out:
706         OUT_DONE();
707         return -EINVAL;
708 }
709
710 static int esp_test_genl(struct sk_buff *skb_2,
711                          struct genl_info *info)
712 {
713         struct sip_cmd_debug *dbgcmd;
714         struct sk_buff *skb = NULL;
715         int i;
716
717         if (info == NULL)
718                 goto out;
719
720         skb = sip_alloc_ctrl_skbuf(SIP, sizeof(struct sip_hdr) + sizeof(struct sip_cmd_debug), SIP_CMD_DEBUG);
721         if (!skb)
722                 goto out;
723
724         dbgcmd = (struct sip_cmd_debug *)(skb->data + sizeof(struct sip_hdr));
725         dbgcmd->cmd_type = nla_get_u32(info->attrs[TEST_ATTR_CMD_TYPE]);
726         dbgcmd->para_num = nla_get_u32(info->attrs[TEST_ATTR_PARA_NUM]);
727         esp_dbg(ESP_DBG_ERROR, "%s dbgcmdType %d paraNum %d\n", __func__, dbgcmd->cmd_type, dbgcmd->para_num);
728         for (i=0; i<dbgcmd->para_num; i++)
729                 dbgcmd->para[i] = nla_get_u32(info->attrs[TEST_ATTR_PARA(i)]);
730
731         /*directly send reply_info to target, and waiting for report*/
732         REGISTER_REPLY(info);
733
734         sip_send_test_cmd(SIP, skb);
735         return 0;
736 out:
737         OUT_DONE();
738         return -EINVAL;
739 }
740
741 static int esp_test_sdio_wr(struct sk_buff *skb_2,
742                             struct genl_info *info)
743 {
744         int res;
745         u32 func_no, addr, value;
746
747         if (info == NULL)
748                 goto out;
749
750         func_no = nla_get_u32(info->attrs[TEST_ATTR_PARA0]);
751         addr = nla_get_u32(info->attrs[TEST_ATTR_PARA1]);
752         value = nla_get_u32(info->attrs[TEST_ATTR_PARA2]);
753
754         if(!func_no) {
755                 res = esp_common_writebyte_with_addr(SIP->epub, addr, (u8)value, ESP_SIF_SYNC);
756         } else {
757                 memcpy(sdio_buff, (u8 *)&value, 4);
758                 res = esp_common_write_with_addr(SIP->epub, addr, sdio_buff, 4, ESP_SIF_SYNC);
759         }
760
761         /*directly send reply_info to target, and waiting for report*/
762         REGISTER_REPLY(info);
763         if (!res)
764                 esp_test_cmd_reply(info, TEST_CMD_SDIO_WR, "write ok!");
765         else
766                 esp_test_cmd_reply(info, TEST_CMD_SDIO_WR, "write fail!");
767
768 out:
769         OUT_DONE();
770         return -EINVAL;
771 }
772
773 static int esp_test_sdio_rd(struct sk_buff *skb_2,
774                             struct genl_info *info)
775 {
776         int res;
777         u32 func_no, addr, value;
778         char value_str[12];
779
780         if (info == NULL)
781                 goto out;
782
783         func_no = nla_get_u32(info->attrs[TEST_ATTR_PARA0]);
784         addr = nla_get_u32(info->attrs[TEST_ATTR_PARA1]);
785
786         if(!func_no) {
787                 memset(sdio_buff, 0, 4);
788                 res = esp_common_readbyte_with_addr(SIP->epub, addr, &sdio_buff[0], ESP_SIF_SYNC);
789         } else {
790                 res = esp_common_read_with_addr(SIP->epub, addr, sdio_buff, 4, ESP_SIF_SYNC);
791         }
792         memcpy((u8 *)&value, sdio_buff, 4);
793
794         /*directly send reply_info to target, and waiting for report*/
795         REGISTER_REPLY(info);
796         if (!res) {
797                 sprintf((char *)&value_str, "0x%x", value);
798                 esp_test_cmd_reply(info, TEST_CMD_SDIO_RD, value_str);
799         } else
800                 esp_test_cmd_reply(info, TEST_CMD_SDIO_RD, "read fail!");
801
802 out:
803         OUT_DONE();
804         return -EINVAL;
805 }
806
807 /* TEST_CMD netlink policy */
808
809 static struct nla_policy test_genl_policy[TEST_ATTR_MAX + 1] = {
810         [TEST_ATTR_CMD_NAME]= { .type = NLA_NUL_STRING, .len = GENL_NAMSIZ - 1 },
811         [TEST_ATTR_CMD_TYPE] = { .type = NLA_U32 },
812         [TEST_ATTR_PARA_NUM] = { .type = NLA_U32 },
813         [TEST_ATTR_PARA0] = { .type = NLA_U32 },
814         [TEST_ATTR_PARA1] = { .type = NLA_U32 },
815         [TEST_ATTR_PARA2] = { .type = NLA_U32 },
816         [TEST_ATTR_PARA3] = { .type = NLA_U32 },
817         [TEST_ATTR_PARA4] = { .type = NLA_U32 },
818         [TEST_ATTR_PARA5] = { .type = NLA_U32 },
819         [TEST_ATTR_PARA6] = { .type = NLA_U32 },
820         [TEST_ATTR_PARA7] = { .type = NLA_U32 },
821         [TEST_ATTR_STR] = { .type = NLA_NUL_STRING, .len = 256-1 },
822 };
823
824 /* Generic Netlink operations array */
825 static struct genl_ops esp_test_ops[] = {
826         {
827                 .cmd = TEST_CMD_ECHO,
828                 .policy = test_genl_policy,
829                 .doit = esp_test_echo,
830                 .flags = GENL_ADMIN_PERM,
831         },
832         {
833                 .cmd = TEST_CMD_ASK,
834                 .policy = test_genl_policy,
835                 .doit = esp_test_ask,
836                 .flags = GENL_ADMIN_PERM,
837         },
838         {
839                 .cmd = TEST_CMD_SLEEP,
840                 .policy = test_genl_policy,
841                 .doit = esp_test_sleep,
842                 .flags = GENL_ADMIN_PERM,
843         },
844         {
845                 .cmd = TEST_CMD_WAKEUP,
846                 .policy = test_genl_policy,
847                 .doit = esp_test_wakeup,
848                 .flags = GENL_ADMIN_PERM,
849         },
850         {
851                 .cmd = TEST_CMD_LOOPBACK,
852                 .policy = test_genl_policy,
853                 .doit = esp_test_loopback,
854                 .flags = GENL_ADMIN_PERM,
855         },
856         {
857                 .cmd = TEST_CMD_TX,
858                 .policy = test_genl_policy,
859                 .doit = esp_test_tx,
860                 .flags = GENL_ADMIN_PERM,
861         },
862         {
863                 .cmd = TEST_CMD_DEBUG,
864                 .policy = test_genl_policy,
865                 .doit = esp_test_genl,
866                 .flags = GENL_ADMIN_PERM,
867         },
868         {
869                 .cmd = TEST_CMD_SDIO_WR,
870                 .policy = test_genl_policy,
871                 .doit = esp_test_sdio_wr,
872                 .flags = GENL_ADMIN_PERM,
873         },
874         {
875                 .cmd = TEST_CMD_SDIO_RD,
876                 .policy = test_genl_policy,
877                 .doit = esp_test_sdio_rd,
878                 .flags = GENL_ADMIN_PERM,
879         },
880             {
881                 .cmd = TEST_CMD_ATE,
882                 .policy = test_genl_policy,
883                 .doit = esp_test_ate,
884                 .flags = GENL_ADMIN_PERM,
885         },
886         {
887                 .cmd = TEST_CMD_SDIOTEST,
888                 .policy = test_genl_policy,
889                 .doit = esp_test_sdiotest,
890                 .flags = GENL_ADMIN_PERM,
891         },
892         {
893                 .cmd = TEST_CMD_SDIOSPEED,
894                 .policy = test_genl_policy,
895                 .doit = esp_test_sdiospeed,
896                 .flags = GENL_ADMIN_PERM,
897         },
898 };
899
900 static int esp_test_netlink_notify(struct notifier_block *nb,
901                                    unsigned long state,
902                                    void *_notify)
903 {
904         struct netlink_notify *notify = _notify;
905
906         if (state != NETLINK_URELEASE)
907                 return NOTIFY_DONE;
908
909 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 8, 0))
910         if (notify->portid == connected_nl) {
911 #else
912         if (notify->pid == connected_nl) {
913 #endif
914                 esp_dbg(ESP_DBG_ERROR, "esp_sdio: user released netlink"
915                        " socket \n");
916                 connected_nl = 0;
917         }
918         return NOTIFY_DONE;
919
920 }
921
922 static struct notifier_block test_netlink_notifier = {
923         .notifier_call = esp_test_netlink_notify,
924 };
925
926 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 31))
927 /**
928  * copy from net/netlink/genetlink.c(linux kernel 2.6.32)
929  *
930  * genl_register_family_with_ops - register a generic netlink family
931  * @family: generic netlink family
932  * @ops: operations to be registered
933  * @n_ops: number of elements to register
934  * 
935  * Registers the specified family and operations from the specified table.
936  * Only one family may be registered with the same family name or identifier.
937  * 
938  * The family id may equal GENL_ID_GENERATE causing an unique id to
939  * be automatically generated and assigned.
940  * 
941  * Either a doit or dumpit callback must be specified for every registered
942  * operation or the function will fail. Only one operation structure per
943  * command identifier may be registered.
944  * 
945  * See include/net/genetlink.h for more documenation on the operations
946  * structure.
947  * 
948  * This is equivalent to calling genl_register_family() followed by
949  * genl_register_ops() for every operation entry in the table taking
950  * care to unregister the family on error path.
951  * 
952  * Return 0 on success or a negative error code.
953  */
954 int genl_register_family_with_ops(struct genl_family *family,
955         struct genl_ops *ops, size_t n_ops)
956 {
957     int err, i;
958
959     err = genl_register_family(family);
960     if (err)
961         return err;
962
963     for (i = 0; i < n_ops; ++i, ++ops) {
964         err = genl_register_ops(family, ops);
965         if (err)
966             goto err_out;
967     }
968     return 0;
969 err_out:
970     genl_unregister_family(family);
971     return err;
972 }
973 #endif
974
975 int test_init_netlink(struct esp_sip *sip)
976 {
977         int rc;
978         esp_dbg(ESP_DBG_ERROR, "esp_sdio: initializing netlink\n");
979
980         sip_copy=sip;
981         /* temp buffer for sdio test */
982         sdio_buff = kzalloc(8, GFP_KERNEL);
983             sdiotest_buf = kzalloc(16*1024, GFP_KERNEL);
984
985         rc = genl_register_family_with_ops(&test_genl_family,
986                                            esp_test_ops, ARRAY_SIZE(esp_test_ops));
987         if (rc)
988                 goto failure;
989
990         rc = netlink_register_notifier(&test_netlink_notifier);
991         if (rc)
992                 goto failure;
993
994         return 0;
995
996 failure:
997         esp_dbg(ESP_DBG_ERROR, "esp_sdio: error occured in %s\n", __func__);
998         if (sdio_buff) {
999                 kfree(sdio_buff);
1000                 sdio_buff = NULL;
1001         }
1002         if (sdiotest_buf) {
1003                 kfree(sdiotest_buf);
1004                 sdiotest_buf = NULL;
1005         }
1006         return -EINVAL;
1007 }
1008
1009 void test_exit_netlink(void)
1010 {
1011         int ret;
1012
1013         if (sdio_buff == NULL)
1014                 return;
1015
1016     kfree(sdio_buff);
1017         sdio_buff = NULL;
1018     sip_copy = NULL;
1019
1020         if (sdioTest.thread) {
1021                 kthread_stop(sdioTest.thread);
1022                 sdioTest.thread = NULL;
1023         }
1024         if (sdiotest_buf) {
1025                 kfree(sdiotest_buf);
1026                 sdiotest_buf = NULL;
1027         }
1028         esp_dbg(ESP_DBG_ERROR, "esp_sdio: closing netlink\n");
1029         /* unregister the notifier */
1030         netlink_unregister_notifier(&test_netlink_notifier);
1031         /* unregister the family */
1032         ret = genl_unregister_family(&test_genl_family);
1033         if (ret)
1034                 esp_dbg(ESP_DBG_ERROR, "esp_sdio: "
1035                        "unregister family %i\n", ret);
1036 }
1037 void esp_test_cmd_event(u32 cmd_type, char *reply_info)
1038 {
1039         esp_test_cmd_reply(&info_copy, cmd_type, reply_info);
1040 }
1041
1042 void esp_stability_test(char *filename,struct esp_pub *epub)
1043 {
1044     int i = 0;
1045     int count =32;
1046     int m = 36;
1047     int n = 100;
1048     int s = 10;
1049     char test_res_str[560];
1050     char *tx_buf =  kzalloc(512*32, GFP_KERNEL);
1051     char *rx_buf =  kzalloc(512*32, GFP_KERNEL);
1052 #ifdef ESP_USE_SPI
1053     struct esp_spi_ctrl *sctrl = NULL;
1054     struct spi_device *spi = NULL;
1055     if (epub == NULL) {
1056         ESSERT(0);
1057         return;
1058     }
1059     sctrl = (struct esp_spi_ctrl *)epub->sif;
1060     spi = sctrl->spi;
1061     if (spi == NULL) {
1062         ESSERT(0);
1063         return;
1064     }
1065 #endif
1066
1067     while(--m)
1068     {
1069         if(m >32)
1070         {
1071             count = count *2;
1072         }else
1073         {
1074             count = 512*(33-m);
1075         }
1076         n =255;
1077
1078         while(n--)
1079         {
1080             s =100;
1081             while(s--)
1082             {
1083
1084                 for(i = 0;i<count;i++)
1085                 {
1086                     tx_buf[i]=i^0X5A;
1087                 }
1088 #ifdef ESP_USE_SPI
1089                 if(count == 512)
1090                 {
1091                     spi_bus_lock(spi->master);
1092                     sif_spi_write_bytes(spi, 0x8010, tx_buf, count, NOT_DUMMYMODE);
1093                     spi_bus_unlock(spi->master);
1094                     memset(rx_buf,0xff,count);
1095
1096                     spi_bus_lock(spi->master);
1097                     sif_spi_read_bytes(spi, 0x8010, tx_buf, count, NOT_DUMMYMODE);
1098                     spi_bus_unlock(spi->master);
1099                 }
1100 #endif
1101                 esp_common_write_with_addr((epub), 0x8010, tx_buf, count,  ESP_SIF_SYNC);
1102                 memset(rx_buf,0xff,count);
1103                 esp_common_read_with_addr((epub), 0x8010, rx_buf, count,  ESP_SIF_SYNC);
1104
1105                 for(i =0;i<count;i++)
1106                 {
1107                     if(tx_buf[i] !=rx_buf[i])
1108                     {
1109                         printk("-----------tx_buf--------------\n");
1110                         show_buf(tx_buf,count);
1111                         printk("-----------rx_buf-------------\n");
1112                         show_buf(rx_buf,count);
1113                         printk("--rx_buf != tx_buf----i= %d----\n",i);
1114
1115                         sprintf(test_res_str, "error, count = %d !!!\n", count);
1116
1117                         printk("%s\n", test_res_str);
1118
1119                         esp_readwrite_file(filename, NULL, test_res_str, strlen(test_res_str));
1120
1121                         goto _out;
1122                     }
1123                 }
1124
1125             }
1126
1127             request_init_conf();
1128
1129             if(sif_get_ate_config() == 0)
1130             {
1131
1132                 sprintf(test_res_str, "ok, count = %d !!!\n", count);
1133
1134                 printk("%s\n", test_res_str);
1135
1136                 esp_readwrite_file(filename, NULL, test_res_str, strlen(test_res_str));
1137
1138                 goto _out;
1139             }
1140
1141             //for apk test
1142             if(sif_get_ate_config() == 3)
1143             {
1144                 sprintf(test_res_str, "error, count = %d !!!\n", count);
1145
1146                 printk("%s\n", test_res_str);
1147
1148                 esp_readwrite_file(filename, NULL, test_res_str, strlen(test_res_str));
1149
1150                 goto _out;
1151             }
1152
1153         }
1154     }
1155
1156     sprintf(test_res_str, "ok, count = %d !!!\n", count);
1157     printk("%s\n", test_res_str);
1158
1159     esp_readwrite_file(filename, NULL, test_res_str, strlen(test_res_str));
1160
1161 _out:
1162     kfree(rx_buf);
1163     kfree(tx_buf);
1164 }
1165
1166 void esp_rate_test(char *filename,struct esp_pub *epub)
1167 {
1168     char *tx_buf =  kzalloc(512*32, GFP_KERNEL);
1169     char *rx_buf =  kzalloc(512*32, GFP_KERNEL);
1170     char test_res_str[560];
1171     unsigned long jiffies_start_rw;
1172     unsigned long jiffies_end_rw;   
1173     unsigned long test_time_rw;
1174     unsigned long jiffies_start_read;
1175     unsigned long jiffies_end_read;   
1176     unsigned long test_time_read;
1177     unsigned long jiffies_start_write;
1178     unsigned long jiffies_end_write;   
1179     unsigned long test_time_write;
1180     int n = 1024;
1181
1182     memset(tx_buf,0x0,512*32);
1183
1184     esp_common_write_with_addr((epub), 0x8010, tx_buf, 512*32, ESP_SIF_SYNC);
1185     jiffies_start_rw = jiffies;
1186
1187     while(n--){
1188         esp_common_write_with_addr((epub), 0x8010, tx_buf, 512*32, ESP_SIF_SYNC);
1189         esp_common_read_with_addr((epub), 0x8010, rx_buf, 512*32,  ESP_SIF_SYNC);
1190     }
1191     jiffies_end_rw = jiffies;
1192     test_time_rw = jiffies_to_msecs(jiffies_end_rw - jiffies_start_rw);
1193
1194     n = 1024;
1195     jiffies_start_read = jiffies;
1196     while(n--){
1197         esp_common_read_with_addr((epub), 0x8010, rx_buf, 512*32,  ESP_SIF_SYNC);
1198         esp_common_read_with_addr((epub), 0x8010, rx_buf, 512*32,  ESP_SIF_SYNC);
1199     }
1200     jiffies_end_read = jiffies;
1201     test_time_read = jiffies_to_msecs(jiffies_end_read - jiffies_start_read);
1202
1203     n= 1024;  
1204     jiffies_start_write = jiffies;
1205     while(n--){
1206         esp_common_write_with_addr((epub), 0x8010, tx_buf, 512*32,  ESP_SIF_SYNC);
1207         esp_common_write_with_addr((epub), 0x8010, tx_buf, 512*32,  ESP_SIF_SYNC);
1208     }
1209     jiffies_end_write = jiffies;
1210     test_time_write = jiffies_to_msecs(jiffies_end_write - jiffies_start_write);
1211
1212     sprintf(test_res_str, "ok,rw_time=%lu,read_time=%lu,write_time=%lu !!!\n",test_time_rw,test_time_read,test_time_write);
1213
1214     printk("%s\n", test_res_str);
1215
1216     esp_readwrite_file(filename, NULL, test_res_str, strlen(test_res_str));
1217
1218     kfree(tx_buf);
1219     kfree(rx_buf);
1220 }
1221
1222 #ifdef ESP_USE_SPI
1223 void esp_resp_test(char *filename,struct esp_pub *epub)
1224 {
1225     int i = 0;
1226     int count =32;
1227     int m = 36;
1228     int n = 10;
1229     int s = 3;
1230     char *test_res_str = kzalloc(1024,GFP_KERNEL);
1231     char *tx_buf =  kzalloc(512*32, GFP_KERNEL);
1232     char *rx_buf =  kzalloc(512*32, GFP_KERNEL);
1233     struct esp_spi_ctrl *sctrl = NULL;
1234     struct spi_device *spi = NULL;
1235     struct esp_spi_resp *spi_resp;
1236     if (epub == NULL) {
1237         ESSERT(0);
1238         return;
1239     }
1240     sctrl = (struct esp_spi_ctrl *)epub->sif;
1241     spi = sctrl->spi;
1242     if (spi == NULL) {
1243         ESSERT(0);
1244         return;
1245     }
1246
1247     while(m--)
1248     {
1249         if(m >32)
1250         {
1251             count = 64;
1252         }else
1253         {
1254             count = 512*(33-m);
1255         }
1256         n =10;
1257
1258         while(n--)
1259         {
1260             s =3;
1261             while(s--)
1262             {
1263
1264                 for(i = 0;i<count;i++)
1265                 {
1266                     tx_buf[i]=i^0X5A;
1267                 }
1268                 if(count == 512)
1269                 {
1270                     spi_bus_lock(spi->master);
1271                     sif_spi_write_bytes(spi, 0x8010, tx_buf, count, NOT_DUMMYMODE);
1272                     spi_bus_unlock(spi->master);
1273                     memset(rx_buf,0xff,count);
1274
1275                     spi_bus_lock(spi->master);
1276                     sif_spi_read_bytes(spi, 0x8010, rx_buf, count, NOT_DUMMYMODE);
1277                     spi_bus_unlock(spi->master);
1278                     for(i =0;i<count;i++)
1279                     {
1280                         if(tx_buf[i] !=rx_buf[i])
1281                         {
1282                             printk("-----------tx_buf------\n");
1283                             show_buf(tx_buf,count);
1284                             printk("-----------rx_buf------\n");
1285                             show_buf(rx_buf,count);
1286                             printk("------rx_buf != tx_buf--i = %d----\n",i);
1287
1288                             sprintf(test_res_str, "error, count = %d !!!\n", count);
1289
1290                             printk("%s\n", test_res_str);
1291
1292                             esp_readwrite_file(filename, NULL, test_res_str, strlen(test_res_str));
1293
1294                             goto _out;
1295                         }
1296                     }
1297
1298
1299                 }
1300                 esp_common_write_with_addr((epub), 0x8010, tx_buf, count,  ESP_SIF_SYNC);
1301                 memset(rx_buf,0xff,count);
1302                 esp_common_read_with_addr((epub), 0x8010, rx_buf, count,  ESP_SIF_SYNC);
1303
1304                 for(i =0;i<count;i++)
1305                 {
1306                     if(tx_buf[i] !=rx_buf[i])
1307                     {
1308                         printk("-----------tx_buf--------------\n");
1309                         show_buf(tx_buf,count);
1310                         printk("-----------rx_buf-------------\n");
1311                         show_buf(rx_buf,count);
1312                         printk("------------------rx_buf != tx_buf------i = %d-------------\n",i);
1313
1314                         sprintf(test_res_str, "error, count = %d !!!\n", count);
1315
1316                         printk("%s\n", test_res_str);
1317
1318                         esp_readwrite_file(filename, NULL, test_res_str, strlen(test_res_str));
1319
1320                         goto _out;
1321                     }
1322                 }
1323
1324             }
1325
1326         }
1327     }
1328     spi_resp = sif_get_spi_resp();
1329     
1330     sprintf(test_res_str, "ok, max_dataW_resp_size=%d--max_dataR_resp_size=%d--max_block_dataW_resp_size=%d--max_block_dataR_resp_size=%d--max_cmd_resp_size=%d-- !!!\n",
1331               spi_resp->max_dataW_resp_size,spi_resp->max_dataR_resp_size,spi_resp->max_block_dataW_resp_size,spi_resp->max_block_dataR_resp_size,spi_resp->max_cmd_resp_size);
1332
1333     printk("%s\n", test_res_str);
1334
1335     esp_readwrite_file(filename, NULL, test_res_str, strlen(test_res_str));
1336
1337 _out:
1338     kfree(rx_buf);
1339     kfree(tx_buf);
1340     kfree(test_res_str);
1341 }
1342 #endif
1343
1344 void esp_noisefloor_test(char *filename,struct esp_pub *epub)
1345 {
1346     char *tx_buf =  kzalloc(512*32, GFP_KERNEL);
1347     char *rx_buf =  kzalloc(512*32, GFP_KERNEL);
1348     char *res_buf = kzalloc(128, GFP_KERNEL);
1349     char test_res_str[560];
1350     unsigned long jiffies_start;   
1351     int i = 0;
1352
1353     for(i = 0;i<512*32;i++)
1354     {
1355         tx_buf[i]=(i*11)^0X5A;
1356     }
1357
1358     jiffies_start = jiffies;
1359
1360     while(jiffies_to_msecs(jiffies - jiffies_start) < 6000){
1361         esp_common_write_with_addr((epub), 0x9010, tx_buf, 512*32, ESP_SIF_SYNC);
1362         memset(rx_buf,0xff,512*32);
1363         esp_common_read_with_addr((epub), 0x9010, rx_buf, 512*32,  ESP_SIF_SYNC);
1364     }
1365
1366     //read result
1367     esp_common_read_with_addr((epub), 0x14, res_buf,128 ,  ESP_SIF_SYNC); 
1368
1369     if ( (res_buf[38] && 0xf)  == (0xa && 0xf) )
1370     {
1371         sprintf(test_res_str, "ok, APST RN:%d,%d; %d,%d; %d,%d; %d,%d; %d,%d; %d,%d; %d,%d; %d,%d; %d,%d; %d,%d; %d,%d; %d,%d; %d,%d; %d,%d; APED\n",
1372                 res_buf[40]-400,res_buf[0]-400, //chanell 1 
1373                 res_buf[41]-400,res_buf[1]-400,
1374                 res_buf[42]-400,res_buf[2]-400,
1375                 res_buf[43]-400,res_buf[3]-400,
1376                 res_buf[100]-400,res_buf[4]-400,
1377                 res_buf[101]-400,res_buf[5]-400,
1378                 res_buf[102]-400,res_buf[6]-400,
1379                 res_buf[103]-400,res_buf[7]-400,
1380                 res_buf[104]-400,res_buf[16]-400,
1381                 res_buf[105]-400,res_buf[17]-400,
1382                 res_buf[106]-400,res_buf[18]-400,
1383                 res_buf[107]-400,res_buf[19]-400,
1384                 res_buf[36]-400,res_buf[20]-400,
1385                 res_buf[37]-400,res_buf[21]-400);
1386
1387     } else
1388     {
1389         sprintf(test_res_str, "error !!!\n");
1390     }
1391
1392     printk("%s\n", test_res_str);
1393
1394     esp_readwrite_file(filename, NULL, test_res_str, strlen(test_res_str));
1395
1396     kfree(res_buf);
1397     kfree(tx_buf);
1398     kfree(rx_buf);
1399 }
1400
1401 void esp_test_init(struct esp_pub *epub)
1402 {
1403     char filename[256];
1404
1405     if (mod_eagle_path_get() == NULL)
1406         sprintf(filename, "%s/%s", FWPATH, "test_results");
1407     else
1408         sprintf(filename, "%s/%s", mod_eagle_path_get(), "test_results");
1409
1410     sif_lock_bus(epub);
1411     sif_had_io_enable(epub);
1412     sif_unlock_bus(epub);
1413
1414     if(sif_get_ate_config() == 2){
1415         esp_stability_test(filename,epub);
1416
1417     } else if(sif_get_ate_config() == 4){
1418         esp_rate_test(filename,epub);
1419     }
1420 #ifdef ESP_USE_SPI
1421     else if(sif_get_ate_config() == 5){
1422         esp_resp_test(filename,epub);
1423     }        
1424 #endif
1425     else if(sif_get_ate_config() == 6){
1426         esp_noisefloor_test(filename,epub);
1427     }
1428 }
1429
1430 #endif  //ifdef TEST_MODE
1431