net:wireless:Support eswin usb wifi ECR6600U
[platform/kernel/linux-starfive.git] / drivers / net / wireless / eswin / fullmac / ecrnx_debugfs_custom.c
1 #include "ecrnx_debugfs_custom.h"
2 #include "ecrnx_debugfs_func.h"
3 #include "slave_log_buf.h"
4 #include <linux/file.h>
5
6 #ifdef CONFIG_ECRNX_DEBUGFS
7 #define DEFINE_SHOW_ATTRIBUTE(__name)                    \
8 static int __name ## _open(struct inode *inode, struct file *file)    \
9 {                                    \
10     return single_open(file, __name ## _show, inode->i_private);    \
11 }                                    \
12                                     \
13 static const struct file_operations __name ## _fops = {            \
14     .owner        = THIS_MODULE,                  \
15     .open        = __name ## _open,               \
16     .read        = seq_read,                      \
17     .llseek        = seq_lseek,                   \
18     .release    = single_release,                 \
19 }
20
21 #define DEFINE_SHOW_ATTRIBUTE_EX(__name)                    \
22 static int __name ## _open(struct inode *inode, struct file *file)    \
23 {                                    \
24     return single_open(file, __name ## _show, inode->i_private);    \
25 }                                    \
26                                     \
27 static const struct file_operations __name ## _fops = {            \
28     .owner        = THIS_MODULE,                  \
29     .open        = __name ## _open,               \
30     .read        = seq_read,                      \
31     .write        = __name ## _write,             \
32     .llseek        = seq_lseek,                   \
33     .release    = single_release,                 \
34 }
35
36 static long custom_sys_write(unsigned int fd, const char __user *buf, size_t count)
37 {
38    long ret = -EBADF;
39    struct file *file = NULL;
40    mm_segment_t status;
41    loff_t offset;
42
43    if (!buf) {
44        printk("Write buffer was empty.\n");
45        return ret;
46    }
47
48 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 10, 0)
49    file = fget(fd);
50    if (file) {
51        offset = file->f_pos;
52 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 19, 0)
53         ret = kernel_write(file, buf, count, (loff_t *)&offset);
54 #else
55         ret = kernel_write(file, buf, count, 0);
56 #endif
57        file->f_pos = offset;
58        fput(file);
59    }
60 #else
61    print_hex_dump(KERN_DEBUG, DBG_PREFIX, DUMP_PREFIX_NONE, 16, 1, buf, count, false);
62 #endif
63    return ret;
64 }
65
66
67 void seq_reg_display_u32(struct seq_file *seq, u32 addr, u32 *reg, u32 len)
68 {
69     u32 i;
70
71     for (i=0; i<len; i++)
72     {
73         if (i%4 == 0)
74             seq_printf(seq, "0x%08X  ", (addr+ 4*i));
75
76         seq_printf(seq, " 0x%08X", reg[i]);
77
78         if (i%4 == 3)
79             seq_printf(seq, "\n");
80     }
81
82     if (len % 4)
83         seq_printf(seq, "\n");
84 }
85
86 void seq_reg_display_u8(struct seq_file *seq, u32 addr, u8 *reg, u32 len)
87 {
88     u32 i;
89
90     for (i=0; i<len; i++)
91     {
92         if (i%16 == 0)
93             seq_printf(seq, "0x%04X  ", (addr+i)&0xFFFF);
94
95         seq_printf(seq, " %02X", reg[i]);
96
97         if (i%16 == 7)
98             seq_printf(seq, "  ");
99
100         if (i%16 == 15)
101             seq_printf(seq, "\n");
102     }
103
104     if (len % 16)
105         seq_printf(seq, "\n");
106 }
107 void read_reg_display_u32(u32 addr, u32 *reg, u32 len)
108 {
109     u32 i;
110     u32 size = len*11 + (len + 3)/4*16 + len/4;
111     u32 point = 0;
112     char * buf = NULL;
113
114     buf = kmalloc(size, GFP_ATOMIC);
115     if(buf == NULL)
116     {
117         return;
118     }
119     for (i=0; i<len; i++)
120     {
121         if (i%4 == 0)
122         {
123             sprintf(buf+point,"addr:0x%08X", (addr+ 4*i));
124             point += 15;
125             buf[point] = '\n';
126             point += 1;
127
128         }
129
130         sprintf(buf+point,"0x%08X", reg[i]);
131         point += 10;
132         buf[point] = '\n';
133         point += 1;
134
135
136         if (i%4 == 3)
137         {
138             buf[point] = '\n';
139             point += 1;
140         }
141     }
142     custom_sys_write(0,buf,size);
143     if(buf != NULL)
144     {
145         kfree(buf);
146         buf = NULL;
147     }
148
149 }
150 /*
151 void reg_display_u32(u32 addr, u32 *reg, u32 len)
152 {
153     u32 i;
154
155     for (i=0; i<len; i++)
156     {
157         if (i%4 == 0)
158             ECRNX_PRINT("0x%08X", (addr+ 4*i));
159
160         ECRNX_PRINT("0x%08X", reg[i]);
161
162         if (i%4 == 3)
163             printk("\n");
164     }
165
166     if (len % 4)
167         printk("\n");
168 }
169 */
170 void reg_display_u8(u32 addr, u8 *reg, u32 len)
171 {
172     u32 i;
173
174     for (i=0; i<len; i++)
175     {
176         if (i%16 == 0)
177             ECRNX_PRINT("0x%04X  ", (addr+i)&0xFFFF);
178
179         ECRNX_PRINT(" %02X", reg[i]);
180
181         if (i%16 == 7)
182             printk("  ");
183
184         if (i%16 == 15)
185             printk("\n");
186     }
187
188     if (len % 16)
189         printk("\n");
190 }
191
192 static int drv_cfg_show(struct seq_file *seq, void *v)
193 {
194 //#define DRV_CFG_DETAILS
195
196     u8 host_ver[32];
197
198     ecrnx_host_ver_get(host_ver);
199
200     seq_printf(seq, "Kernel Version : %d.%d.%d\n", LINUX_VERSION_CODE >> 16, (LINUX_VERSION_CODE >> 8)&0xFF, LINUX_VERSION_CODE&0xFF);
201     seq_printf(seq, "Driver Version : %s\n", host_ver);
202     seq_printf(seq, "------------------------------------------------\n");
203
204 #ifdef CONFIG_ECRNX_ESWIN
205     seq_printf(seq, "CONFIG_ECRNX_ESWIN=1\n");
206 #else
207     #ifdef DRV_CFG_DETAILS
208         seq_printf(seq, "CONFIG_ECRNX_ESWIN=0\n");
209     #endif
210 #endif
211
212
213 #ifdef CONFIG_ECRNX_FULLMAC
214     seq_printf(seq, "CONFIG_ECRNX_FULLMAC=1\n");
215 #else
216     #ifdef DRV_CFG_DETAILS
217         seq_printf(seq, "CONFIG_ECRNX_FULLMAC=0\n");
218     #endif
219 #endif
220
221
222 #ifdef CONFIG_ECRNX_SOFTMAC
223     seq_printf(seq, "CONFIG_ECRNX_SOFTMAC=1\n");
224 #else
225     #ifdef DRV_CFG_DETAILS
226         seq_printf(seq, "CONFIG_ECRNX_SOFTMAC=0\n");
227     #endif
228 #endif
229
230
231 #ifdef CONFIG_ECRNX_ESWIN_USB
232     seq_printf(seq, "CONFIG_ECRNX_ESWIN_USB=1\n");
233 #else
234     #ifdef DRV_CFG_DETAILS
235         seq_printf(seq, "CONFIG_ECRNX_ESWIN_USB=0\n");
236     #endif
237 #endif
238
239
240 #ifdef CONFIG_ECRNX_ESWIN_SDIO
241     seq_printf(seq, "CONFIG_ECRNX_ESWIN_SDIO=1\n");
242 #else
243     #ifdef DRV_CFG_DETAILS
244         seq_printf(seq, "CONFIG_ECRNX_ESWIN_SDIO=0\n");
245     #endif
246 #endif
247
248
249 #ifdef CONFIG_ECRNX_HE
250     seq_printf(seq, "CONFIG_ECRNX_HE=1\n");
251 #else
252     #ifdef DRV_CFG_DETAILS
253         seq_printf(seq, "CONFIG_ECRNX_HE=0\n");
254     #endif
255 #endif
256
257
258 #ifdef CONFIG_ECRNX_P2P
259     seq_printf(seq, "CONFIG_ECRNX_P2P=1\n");
260 #else
261     #ifdef DRV_CFG_DETAILS
262         seq_printf(seq, "CONFIG_ECRNX_P2P=0\n");
263     #endif
264 #endif
265
266
267 #ifdef CONFIG_ECRNX_FHOST
268         seq_printf(seq, "CONFIG_ECRNX_FHOST=1\n");
269 #else
270     #ifdef DRV_CFG_DETAILS
271             seq_printf(seq, "CONFIG_ECRNX_FHOST=0\n");
272     #endif
273 #endif
274
275
276 #ifdef CONFIG_ECRNX_SDM
277         seq_printf(seq, "CONFIG_ECRNX_SDM=1\n");
278 #else
279     #ifdef DRV_CFG_DETAILS
280             seq_printf(seq, "CONFIG_ECRNX_SDM=0\n");
281     #endif
282 #endif
283
284
285 #ifdef CONFIG_ECRNX_TL4
286         seq_printf(seq, "CONFIG_ECRNX_TL4=1\n");
287 #else
288     #ifdef DRV_CFG_DETAILS
289             seq_printf(seq, "CONFIG_ECRNX_TL4=0\n");
290     #endif
291 #endif
292
293
294 #ifdef CONFIG_ECRNX_MUMIMO_TX
295         seq_printf(seq, "CONFIG_ECRNX_MUMIMO_TX=1\n");
296 #else
297     #ifdef DRV_CFG_DETAILS
298             seq_printf(seq, "CONFIG_ECRNX_MUMIMO_TX=0\n");
299     #endif
300 #endif
301
302
303 #ifdef CONFIG_ECRNX_RADAR
304         seq_printf(seq, "CONFIG_ECRNX_RADAR=1\n");
305 #else
306     #ifdef DRV_CFG_DETAILS
307             seq_printf(seq, "CONFIG_ECRNX_RADAR=0\n");
308     #endif
309 #endif
310
311
312 #ifdef CONFIG_ECRNX_BCMC
313         seq_printf(seq, "CONFIG_ECRNX_BCMC=1\n");
314 #else
315     #ifdef DRV_CFG_DETAILS
316             seq_printf(seq, "CONFIG_ECRNX_BCMC=0\n");
317     #endif
318 #endif
319
320
321 #ifdef CONFIG_ECRNX_MON_DATA
322         seq_printf(seq, "CONFIG_ECRNX_MON_DATA=1\n");
323 #else
324     #ifdef DRV_CFG_DETAILS
325             seq_printf(seq, "CONFIG_ECRNX_MON_DATA=0\n");
326     #endif
327 #endif
328
329
330 #ifdef CONFIG_ECRNX_SW_PROFILING
331         seq_printf(seq, "CONFIG_ECRNX_SW_PROFILING=1\n");
332 #else
333     #ifdef DRV_CFG_DETAILS
334             seq_printf(seq, "CONFIG_ECRNX_SW_PROFILING=0\n");
335     #endif
336 #endif
337
338
339 #ifdef CONFIG_ECRNX_DBG
340         seq_printf(seq, "CONFIG_ECRNX_DBG=1\n");
341 #else
342     #ifdef DRV_CFG_DETAILS
343             seq_printf(seq, "CONFIG_ECRNX_DBG=0\n");
344     #endif
345 #endif
346
347
348 #ifdef CFG_WOW_SUPPORT
349             seq_printf(seq, "CFG_WOW_SUPPORT=1\n");
350 #else
351     #ifdef DRV_CFG_DETAILS
352                 seq_printf(seq, "CFG_WOW_SUPPORT=0\n");
353     #endif
354 #endif
355
356     return 0;
357 }
358 DEFINE_SHOW_ATTRIBUTE(drv_cfg);
359
360
361 static int log_level_show(struct seq_file *seq, void *v)
362 {
363     LOG_CTL_ST log;
364
365     if (0 == ecrnx_log_level_get(&log))
366         seq_printf(seq, "log_level:%d  dir:%d\n", log.level, log.dir);
367     else
368         seq_printf(seq, "\n");
369
370     return 0;
371 }
372
373 static ssize_t log_level_write(struct file *filp, const char __user *buffer,
374                             size_t count, loff_t *ppos)
375 {
376     u8 kbuf[128]={0};
377     u32 level,dir;
378
379     if (count > 6)  // 255:0\n
380         return -1;
381
382     if (0 != copy_from_user(kbuf, buffer, count))
383         return -1;
384     sscanf(kbuf, "%d:%d\n", &level, &dir);
385
386     if (dir > 1)
387         return -1;
388
389     ecrnx_fw_log_level_set(level, dir);
390
391     return count;
392 }
393 DEFINE_SHOW_ATTRIBUTE_EX(log_level);
394
395 static ssize_t mac_log_read(struct file *file , char __user *user_buf,
396                                    size_t count, loff_t *ppos)
397 {
398     struct ring_buffer *buf_handle;
399     int len = 0;
400     char buf[512] = {0};
401     if (false == ecrnx_log_host_enable())
402         return 0;
403 #ifdef CONFIG_ECRNX_ESWIN_USB
404     buf_handle = usb_dbg_buf_get();
405     ring_buffer_scrolling_display(buf_handle,true);
406     while(buf_handle->show)
407     {
408         len = ring_buffer_len(buf_handle);
409         len = min(len, 512);
410         //ECRNX_PRINT("mac_log_read len:%d %d %d", len,buf_handle->write_point, buf_handle->read_point);
411         if(len > 0)
412         {
413             ring_buffer_get(buf_handle,buf,len);
414             custom_sys_write(0,buf,len);
415             msleep(35);
416         }
417         else
418         {
419             msleep(500);
420         }
421         cond_resched();
422     }
423 #endif
424
425     return count;
426 }
427
428 static ssize_t mac_log_write(struct file *filp, const char __user *buffer,
429                             size_t count, loff_t *ppos)
430 {
431     u8 kbuf[128]={0};
432     u32 show;
433     struct ring_buffer *buf_handle;
434
435     if (count > 2)  // 255:0\n
436         return -1;
437
438     if (0 != copy_from_user(kbuf, buffer, count))
439         return -1;
440     sscanf(kbuf, "%d:%d\n", &show);
441 #ifdef CONFIG_ECRNX_ESWIN_USB
442     if (show == 0)
443     {
444         buf_handle = usb_dbg_buf_get();
445         ring_buffer_scrolling_display(buf_handle, false);
446     }
447 #endif
448
449     return count;
450 }
451
452 static const struct file_operations new_mac_log_fops = { \
453     .read   = mac_log_read,                         \
454     .write  = mac_log_write,                         \
455 };
456
457 static int ver_info_show(struct seq_file *seq, void *v)
458 {
459     u8 host_ver[32];
460     u8 build_time[32];
461
462     ecrnx_host_ver_get(host_ver);
463     ecrnx_build_time_get(build_time);
464
465     seq_printf(seq, "%s\n", host_ver);
466     seq_printf(seq, "build time: %s\n", build_time);
467
468     return 0;
469 }
470 DEFINE_SHOW_ATTRIBUTE(ver_info);
471
472 extern bin_head_data head;
473 static int fw_info_show(struct seq_file *seq, void *v)
474 {
475     struct tm timenow = {0};
476
477     localtime(&timenow, head.UTC_time);
478     if(head.fw_Info != NULL)
479     {
480         seq_printf(seq, "sdk_verson:%s\n", head.fw_Info);
481     }
482     seq_printf(seq, "fw_crc:0x%8x\n", head.crc32);
483     seq_printf(seq, "fw_build_time: %04d-%02d-%02d %02d:%02d:%02d\n",(int)timenow.tm_year,timenow.tm_mon,timenow.tm_mday,timenow.tm_hour,timenow.tm_min,timenow.tm_sec);
484     return 0;
485 }
486
487 DEFINE_SHOW_ATTRIBUTE(fw_info);
488
489 struct dentry *ecr6600u_dir = NULL;
490 struct dentry *drv_cfg, *log_level, *mac_log, *ver_info, *fw_info;
491
492 int ecrnx_debugfs_info_init(void      *private_data)
493 {
494     ecr6600u_dir = debugfs_create_dir("ecr6600u", NULL);
495     if (!ecr6600u_dir)
496         goto Fail;
497
498     drv_cfg = debugfs_create_file("drv_cfg", 0444, ecr6600u_dir, private_data, &drv_cfg_fops);
499     if (!drv_cfg)
500         goto Fail;
501
502     log_level = debugfs_create_file("log_level", 0666, ecr6600u_dir, private_data, &log_level_fops);
503     if (!log_level)
504         goto Fail;
505
506     mac_log = debugfs_create_file("maclog", 0444, ecr6600u_dir, private_data, &new_mac_log_fops);
507     if (!mac_log)
508         goto Fail;
509
510     ver_info = debugfs_create_file("ver_info", 0444, ecr6600u_dir, private_data, &ver_info_fops);
511     if (!ver_info)
512         goto Fail;
513
514     fw_info = debugfs_create_file("fw_info", 0444, ecr6600u_dir, private_data, &fw_info_fops);
515     if (!fw_info)
516         goto Fail;
517     return 0;
518
519 Fail:
520     return -ENOENT;
521 }
522
523
524 static int rf_info_show(struct seq_file *seq, void *v)
525 {
526     RF_INFO_ST cur, oper;
527
528     if (0 == ecrnx_rf_info_get(seq, IF_STA, &cur, &oper))
529     {
530         seq_printf(seq, "cur_ch=%d, cur_bw=%d, cur_ch_offset=%d\n", cur.ch, cur.bw, cur.ch_offset);
531         // seq_printf(seq, "oper_ch=%d, oper_bw=%d, oper_ch_offset=%d\n", oper.ch, oper.bw, oper.ch_offset);
532     }
533
534     return 0;
535 }
536 DEFINE_SHOW_ATTRIBUTE(rf_info);
537
538 static int mac_reg_dump_show(struct seq_file *seq, void *v)
539 {
540     ecrnx_mac_reg_dump(seq);
541
542     return 0;
543 }
544 DEFINE_SHOW_ATTRIBUTE(mac_reg_dump);
545
546 static int rf_reg_dump_show(struct seq_file *seq, void *v)
547 {
548     ecrnx_rf_reg_dump(seq);
549
550     return 0;
551 }
552 DEFINE_SHOW_ATTRIBUTE(rf_reg_dump);
553
554 static int bb_reg_dump_show(struct seq_file *seq, void *v)
555 {
556     ecrnx_bb_reg_dump(seq);
557
558     return 0;
559 }
560 DEFINE_SHOW_ATTRIBUTE(bb_reg_dump);
561
562 static int country_code_show(struct seq_file *seq, void *v)
563 {
564     char country_code[3];
565     if (0 == ecrnx_country_code_get(seq, country_code))
566     {
567         seq_printf(seq, "%s\n", country_code);
568     }
569     else
570     {
571         seq_printf(seq, "UNSPECIFIED\n");
572     }
573
574     return 0;
575 }
576 DEFINE_SHOW_ATTRIBUTE(country_code);
577
578 static int mac_addr_show(struct seq_file *seq, void *v)
579 {
580     u8 mac_addr_info[128];
581
582     if (0 == ecrnx_mac_addr_get_ex(seq, mac_addr_info))
583     {
584         seq_printf(seq, "%s", mac_addr_info);
585     }
586     else
587     {
588         seq_printf(seq, "--\n");
589     }
590
591     return 0;
592 }
593 DEFINE_SHOW_ATTRIBUTE(mac_addr);
594
595 static int efuse_map_show(struct seq_file *seq, void *v)
596 {
597     ecrnx_efuse_map_dump(seq);
598
599     return 0;
600 }
601 DEFINE_SHOW_ATTRIBUTE(efuse_map);
602
603 static int read_reg_show(struct seq_file *seq, void *v)
604 {
605     return 0;
606 }
607 static ssize_t read_reg_write(struct file *filp, const char __user *buffer,
608                             size_t count, loff_t *ppos)
609 {
610     u8 kbuf[128]={0};
611     u32 addr, len;
612     int ret_code;
613     char * read_reg_buf = NULL;
614
615     if (count > sizeof(kbuf))
616         return -1;
617
618     if (0 != copy_from_user(kbuf, buffer, count))
619         return -1;
620
621     ret_code = sscanf(kbuf, "0x%x %d\n", &addr, &len);
622     if (ret_code != 2)
623     {
624         ECRNX_ERR("Parameter error.\n");
625         return -1;
626     }
627
628     if ( addr%4 )
629     {
630         ECRNX_ERR("Address is invalid.\n");
631         return -1;
632     }
633
634     if (0 == ecrnx_slave_reg_read(addr, reg_buf, len * sizeof(reg_buf[0])))
635         read_reg_display_u32(addr, reg_buf, len);
636     return count;
637 }
638 DEFINE_SHOW_ATTRIBUTE_EX(read_reg);
639 static int write_reg_show(struct seq_file *seq, void *v)
640 {
641     return 0;
642 }
643
644 static ssize_t write_reg_write(struct file *filp, const char __user *buffer,
645                             size_t count, loff_t *ppos)
646 {
647     u8 kbuf[128]={0};
648     u32 addr, data;
649     int ret_code,ret = count;
650     char* write_reg_result = NULL;
651
652     if (count > sizeof(kbuf))
653         ret = -1;
654
655     if (0 != copy_from_user(kbuf, buffer, count))
656         ret = -1;
657
658     ret_code = sscanf(kbuf, "0x%x 0x%x\n", &addr, &data);
659     if (ret_code != 2)
660     {
661         write_reg_result = "Parameter error.\n";
662         ret = -1;
663     }
664
665     if ( addr%4 )
666     {
667         write_reg_result = "Address is invalid.\n";
668         ret = -1;
669     }
670
671     if (0 != ecrnx_slave_reg_write(addr, data, 1))
672     {
673         write_reg_result = "Write error.\n";
674         ret = -1;
675     }
676     else
677         write_reg_result = "Write success.\n";
678     if(write_reg_result != NULL)
679     {
680         custom_sys_write(0,write_reg_result,strlen(write_reg_result));
681     }
682     return ret;
683 }
684 DEFINE_SHOW_ATTRIBUTE_EX(write_reg);
685
686 static int rf_tx_rx_info_show(struct seq_file *seq, void *v)
687 {
688 //    u8 *resp = NULL;
689 //    u8 cli[]="rf_get_rssi 0 1 2";
690
691 //    ecrnx_slave_cli_send(cli, resp);
692
693     return 0;
694 }
695
696 static ssize_t rf_tx_rx_info_write(struct file *filp, const char __user *buffer,
697                             size_t count, loff_t *ppos)
698 {
699     u8 kbuf[128]={0};
700
701     if (count > sizeof(kbuf))
702         return -1;
703
704     if (0 != copy_from_user(kbuf, buffer, count))
705         return -1;
706
707     cli_cmd_parse(kbuf);
708
709     return count;
710 }
711
712 DEFINE_SHOW_ATTRIBUTE_EX(rf_tx_rx_info);
713
714 struct dentry *hw_dir = NULL;
715 struct dentry *hw_rf_info, *hw_mac_reg_dump, *hw_rf_reg_dump, *hw_bb_reg_dump, *hw_country_code, *hw_mac_addr;
716 struct dentry *hw_efuse_map, *hw_read_reg, *hw_write_reg, *hw_rf_tx_rx_info;
717
718 int ecrnx_debugfs_hw_init(void *private_data)
719 {
720     if (!ecr6600u_dir)
721         return -ENOENT;
722
723     hw_dir = debugfs_create_dir("hw", ecr6600u_dir);
724     if (!hw_dir)
725         return -ENOENT;
726
727     hw_rf_info = debugfs_create_file("rf_info", 0444, hw_dir, private_data, &rf_info_fops);
728     if (!hw_rf_info)
729         goto Fail;
730
731     hw_mac_reg_dump = debugfs_create_file("mac_reg_dump", 0444, hw_dir, private_data, &mac_reg_dump_fops);
732     if (!hw_mac_reg_dump)
733         goto Fail;
734
735     hw_rf_reg_dump = debugfs_create_file("rf_reg_dump", 0444, hw_dir, private_data, &rf_reg_dump_fops);
736     if (!hw_rf_reg_dump)
737         goto Fail;
738
739     hw_bb_reg_dump = debugfs_create_file("bb_reg_dump", 0444, hw_dir, private_data, &bb_reg_dump_fops);
740     if (!hw_bb_reg_dump)
741         goto Fail;
742
743     hw_country_code = debugfs_create_file("country_code", 0444, hw_dir, private_data, &country_code_fops);
744     if (!hw_country_code)
745         goto Fail;
746
747     hw_mac_addr = debugfs_create_file("mac_addr", 0444, hw_dir, private_data, &mac_addr_fops);
748     if (!hw_mac_addr)
749         goto Fail;
750
751     hw_efuse_map = debugfs_create_file("efuse_map", 0444, hw_dir, private_data, &efuse_map_fops);
752     if (!hw_efuse_map)
753         goto Fail;
754
755     hw_read_reg = debugfs_create_file("read_reg", 0222, hw_dir, private_data, &read_reg_fops);
756     if (!hw_read_reg)
757         goto Fail;
758
759     hw_write_reg = debugfs_create_file("write_reg", 0222, hw_dir, private_data, &write_reg_fops);
760     if (!hw_write_reg)
761         goto Fail;
762
763     hw_rf_tx_rx_info = debugfs_create_file("rf_tx_rx_info", 0666, hw_dir, private_data, &rf_tx_rx_info_fops);
764     if (!hw_rf_tx_rx_info)
765         goto Fail;
766
767     return 0;
768
769 Fail:
770     debugfs_remove_recursive(hw_dir);
771     hw_dir = NULL;
772     return -ENOENT;
773 }
774
775 #ifndef MAC_FMT
776 #define MAC_FMT "%02x:%02x:%02x:%02x:%02x:%02x"
777 #endif
778 #ifndef MAC_ARG
779 #define MAC_ARG(x) ((u8 *)(x))[0], ((u8 *)(x))[1], ((u8 *)(x))[2], ((u8 *)(x))[3], ((u8 *)(x))[4], ((u8 *)(x))[5]
780 #endif
781
782 static int survey_info_show(struct seq_file *seq, void *v)
783 {
784   int index = 0;
785   struct ecrnx_hw *ecrnx_hw;
786   struct ecrnx_debugfs_survey_info_tbl *entry;
787
788   if (seq->private != NULL)
789       ecrnx_hw = seq->private;
790   else
791       return -1;
792   
793 //seq_printf(seq, "index  bssid  ch  RSSI  SdBm  Noise    age          flag          ssid \n");
794   seq_printf(seq, "%5s  %-17s  %3s  %-3s  %-4s  %-4s  %5s  %32s  %32s\n", "index", "bssid", "ch", "RSSI", "SdBm", "Noise", "age", "flag", "ssid");
795   
796   list_for_each_entry(entry, &ecrnx_hw->debugfs_survey_info_tbl_ptr, list) {
797       seq_printf(seq, "%5d  "MAC_FMT"  %3d  %3d  %4d  %4d  %5d    %32s    %32s\n",
798           ++index,
799           MAC_ARG(entry->bssid),
800           entry->ch,
801           entry->rssi,
802           entry->sdbm,
803           entry->noise,
804           entry->age,
805           entry->flag,
806           entry->ssid);
807   }
808
809     return 0;
810 }
811 DEFINE_SHOW_ATTRIBUTE(survey_info);
812
813 static int sta_info_show(struct seq_file *seq, void *v)
814 {
815     s8 noise_dbm;
816     struct cfg80211_chan_def chandef;
817     u32 bw_array[]={20, 20, 40, 80, 80, 160, 5, 10};
818
819     if (0 == ecrnx_signal_level_get(seq, IF_STA, &noise_dbm))
820         seq_printf(seq, "signal level : %d dBm\n", noise_dbm);
821     else
822         seq_printf(seq, "signal level : \n");
823
824     if (0 == ecrnx_channel_get(seq, IF_STA, &chandef))
825     {
826         seq_printf(seq, "frequency    : %d MHz\n", chandef.center_freq1);
827         seq_printf(seq, "bandwidth    : %d MHz\n", bw_array[chandef.width]);
828     }
829     else
830     {
831         seq_printf(seq, "frequency    : \n");
832         seq_printf(seq, "bandwidth    : \n");
833     }
834
835     return 0;
836 }
837 DEFINE_SHOW_ATTRIBUTE(sta_info);
838
839 static int ht_info_show(struct seq_file *seq, void *v)
840 {
841     struct cfg80211_chan_def chandef;
842     u32 bw_array[]={20, 20, 40, 80, 80, 160, 5, 10};
843
844     if (0 == ecrnx_channel_get(seq, IF_STA, &chandef))
845         seq_printf(seq, "bandwidth    : %d MHz\n", bw_array[chandef.width]);
846     else
847         seq_printf(seq, "bandwidth    : \n");
848
849     return 0;
850 }
851 DEFINE_SHOW_ATTRIBUTE(ht_info);
852
853
854 struct dentry *wlan0_dir = NULL;
855 struct dentry *wlan0_survey_info, *wlan0_sta_info, *wlan0_ht_info;;
856
857 int ecrnx_debugfs_wlan0_init(void *private_data)
858 {
859     if (!ecr6600u_dir)
860         return -ENOENT;
861
862     wlan0_dir = debugfs_create_dir("wlan0", ecr6600u_dir);
863     if (!wlan0_dir)
864         return -ENOENT;
865
866     wlan0_survey_info = debugfs_create_file("survey_info", 0444, wlan0_dir, private_data, &survey_info_fops);
867     if (!wlan0_survey_info)
868         goto Fail;
869
870     wlan0_sta_info = debugfs_create_file("sta_info", 0444, wlan0_dir, private_data, &sta_info_fops);
871     if (!wlan0_sta_info)
872         goto Fail;
873
874     wlan0_ht_info = debugfs_create_file("ht_info", 0444, wlan0_dir, private_data, &ht_info_fops);
875     if (!wlan0_ht_info)
876         goto Fail;
877
878     return 0;
879
880 Fail:
881     debugfs_remove_recursive(wlan0_dir);
882     wlan0_dir = NULL;
883     return -ENOENT;
884 }
885
886 static int ap_info_show(struct seq_file *seq, void *v)
887 {
888     struct cfg80211_chan_def chandef;
889     u32 bw_array[]={20, 20, 40, 80, 80, 160, 5, 10};
890     u8 ssid[IEEE80211_MAX_SSID_LEN];
891     u8 sta_mac[NX_REMOTE_STA_MAX][ETH_ALEN+1];
892     u32 i;
893
894     seq_printf(seq, "AP Vendor ESWIN\n");
895
896     seq_printf(seq, "ap info\n");
897     if (0 == ecrnx_ssid_get(seq, IF_AP, ssid))
898         seq_printf(seq, "ssid         : %s\n", ssid);
899     else
900         seq_printf(seq, "ssid         : \n");
901
902     if (0 == ecrnx_channel_get(seq, IF_AP, &chandef))
903     {
904         seq_printf(seq, "cur_channel=%d, cur_bwmode=%d(%dMHz), cur_ch_offset=0\n",
905             (chandef.center_freq1 - 2412)/5 + 1, chandef.width, bw_array[chandef.width]);
906     }
907     else
908     {
909         seq_printf(seq, "cur_channel=, cur_bwmode=(MHz), cur_ch_offset=\n");
910     }
911 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 0, 0))
912     if ((chandef.width <= NL80211_CHAN_WIDTH_10) && (chandef.width >= NL80211_CHAN_WIDTH_20_NOHT))
913     {
914         if (NL80211_CHAN_WIDTH_20_NOHT == chandef.width)
915         {
916             seq_printf(seq, "ht_en=0\n");
917         }
918         else
919         {
920             seq_printf(seq, "ht_en=1\n");
921         }
922     }
923     else
924     {
925         seq_printf(seq, "ht_en=0\n");
926     }
927 #endif
928
929     seq_printf(seq, "wireless_mode=0xb(B/G/N), rtsen=0, cts2slef=0\n");
930     seq_printf(seq, "state=0x10, aid=0, macid=32, raid=0\n");
931     seq_printf(seq, "qos_en=1, init_rate=0\n");
932     seq_printf(seq, "bwmode=0, ch_offset=0, sgi_20m=1,sgi_40m=1\n");
933     seq_printf(seq, "ampdu_enable = 1\n");
934     seq_printf(seq, "agg_enable_bitmap=0, candidate_tid_bitmap=0\n");
935     seq_printf(seq, "ldpc_cap=0x0, stbc_cap=0x0, beamform_cap=0x0\n");
936
937
938     seq_printf(seq, "\n");
939     seq_printf(seq, "station info\n");
940     memset(sta_mac, 0x00, sizeof(sta_mac));
941     if (0 == ecrnx_sta_mac_get(seq, IF_AP, sta_mac))
942     {
943         for (i=0; i<NX_REMOTE_STA_MAX; i++)
944         {
945             if (sta_mac[i][0])
946                 seq_printf(seq, "sta's macaddr:%02X:%02X:%02X:%02X:%02X:%02X\n", sta_mac[i][1], sta_mac[i][2],
947                                 sta_mac[i][3], sta_mac[i][4], sta_mac[i][5], sta_mac[i][6]);
948         }
949     }
950
951     return 0;
952 }
953 DEFINE_SHOW_ATTRIBUTE(ap_info);
954
955 static int sta_info_in_ap_show(struct seq_file *seq, void *v)
956 {
957         struct ecrnx_debugfs_sta *sta;
958         
959         if (seq->private == NULL)
960                 ECRNX_ERR("error sta_info_in_ap_show\n");
961
962         sta = seq->private;
963         
964         seq_printf(seq, "cur_channel=%d, cur_bwmode=%d\n", sta->ch_idx, sta->width);
965         //seq_printf(m, "wireless_mode=0x%x(%s), rtsen=%d, cts2slef=%d\n", psta->wireless_mode, wl_mode, psta->rtsen, psta->cts2self);
966         seq_printf(seq, "aid=%d\n", sta->aid);
967
968         seq_printf(seq, "qos_en=%d, ht_en=%d\n", sta->qos, sta->ht);
969         seq_printf(seq, "sgi_20m=%d,sgi_40m=%d\n", sta->sgi_20m, sta->sgi_40m);
970         seq_printf(seq, "ampdu_enable = %d\n", sta->ampdu_enable);
971         seq_printf(seq, "agg_enable_bitmap=%x, candidate_tid_bitmap=%x\n", sta->agg_enable_bitmap, sta->candidate_tid_bitmap);
972         seq_printf(seq, "ldpc_cap=0x%x, stbc_cap=0x%x, beamform_cap=0x%x\n", sta->ldpc_cap, sta->stbc_cap, sta->beamform_cap);
973
974         return 0;
975 }
976 DEFINE_SHOW_ATTRIBUTE(sta_info_in_ap);
977
978 struct dentry *ap0_dir = NULL;
979 struct dentry *ap0_ap_info;
980 struct dentry *stas[NX_REMOTE_STA_MAX];
981
982 int ecrnx_debugfs_ap0_init(void *private_data)
983 {
984     if (!ecr6600u_dir)
985         return -ENOENT;
986
987     ap0_dir = debugfs_create_dir("ap0", ecr6600u_dir);
988     if (!ap0_dir)
989         return -ENOENT;
990
991     ap0_ap_info = debugfs_create_file("ap_info", 0444, ap0_dir, private_data, &ap_info_fops);
992     if (!ap0_ap_info)
993         goto Fail;
994
995     return 0;
996
997 Fail:
998     debugfs_remove_recursive(ap0_dir);
999     ap0_dir = NULL;
1000     return -ENOENT;
1001 }
1002
1003 void ecrnx_debugfs_sta_in_ap_init(void *private_data)
1004 {
1005         int index;
1006         char file_name[19];
1007         struct ecrnx_debugfs_sta *sta;
1008
1009         sta = private_data;
1010         index = sta->sta_idx;
1011
1012         sprintf(file_name, "%02x-%02x-%02x-%02x-%02x-%02x", MAC_ARG(sta->mac_addr));
1013         stas[index] = debugfs_create_file(file_name, 0444, ap0_dir, private_data, &sta_info_in_ap_fops);
1014         ECRNX_PRINT("%s:file_name: %s, sta idx:%d,stas:0x%p \n", __func__, file_name, sta->sta_idx, stas[index]);
1015 }
1016
1017 void ecrnx_debugfs_sta_in_ap_del(u8 sta_idx)
1018 {
1019     struct dentry *dentry_sta;
1020     struct ecrnx_debugfs_sta *debugfs_sta;
1021
1022     ECRNX_PRINT("%s: sta_idx:%d, stas:0x%p \n", __func__, sta_idx, stas[sta_idx]);
1023     dentry_sta = stas[sta_idx];
1024     if(!dentry_sta)
1025     {
1026         ECRNX_ERR("error sta_idx!!!\n");
1027         return;
1028     }
1029
1030     if (dentry_sta->d_inode)
1031     {
1032         debugfs_sta = dentry_sta->d_inode->i_private;
1033         ECRNX_DBG("%s: dentry_sta->d_inode:0x%p, debugfs_sta:0x%p \n", __func__, dentry_sta->d_inode, debugfs_sta);
1034         if (debugfs_sta)
1035         {
1036             debugfs_remove(dentry_sta);
1037             kfree(debugfs_sta);
1038         }
1039     }
1040
1041     stas[sta_idx] = NULL;
1042 }
1043
1044 static int p2p0_survey_info_show(struct seq_file *seq, void *v)
1045 {
1046         survey_info_show(seq, v);
1047
1048     return 0;
1049 }
1050 DEFINE_SHOW_ATTRIBUTE(p2p0_survey_info);
1051
1052 static int p2p_info_show(struct seq_file *seq, void *v)
1053 {
1054     s8 noise_dbm;
1055     struct cfg80211_chan_def chandef;
1056     u32 bw_array[]={20, 20, 40, 80, 80, 160, 5, 10};
1057
1058     if (0 == ecrnx_signal_level_get(seq, IF_P2P, &noise_dbm))
1059         seq_printf(seq, "signal level : %d dBm\n", noise_dbm);
1060     else
1061         seq_printf(seq, "signal level : \n");
1062
1063     if (0 == ecrnx_channel_get(seq, IF_P2P, &chandef))
1064     {
1065         seq_printf(seq, "frequency    : %d MHz\n", chandef.center_freq1);
1066         seq_printf(seq, "bandwidth    : %d MHz\n", bw_array[chandef.width]);
1067     }
1068     else
1069     {
1070         seq_printf(seq, "frequency    : \n");
1071         seq_printf(seq, "bandwidth    : \n");
1072     }
1073
1074     if (NL80211_IFTYPE_P2P_CLIENT == ecrnx_p2p_role_get(seq, IF_P2P))
1075     {
1076         seq_printf(seq, "type         : P2P-client\n");
1077     }
1078     else if(NL80211_IFTYPE_P2P_GO == ecrnx_p2p_role_get(seq, IF_P2P))
1079     {
1080         seq_printf(seq, "type         : P2P-GO\n");
1081     }
1082     else
1083     {
1084         seq_printf(seq, "type         : \n");
1085     }
1086
1087     return 0;
1088 }
1089 DEFINE_SHOW_ATTRIBUTE(p2p_info);
1090
1091 static int p2p0_ht_info_show(struct seq_file *seq, void *v)
1092 {
1093     struct cfg80211_chan_def chandef;
1094     u32 bw_array[]={20, 20, 40, 80, 80, 160, 5, 10};
1095
1096     if (0 == ecrnx_channel_get(seq, IF_P2P, &chandef))
1097         seq_printf(seq, "bandwidth    : %d MHz\n", bw_array[chandef.width]);
1098     else
1099         seq_printf(seq, "bandwidth    : \n");
1100
1101     return 0;
1102 }
1103 DEFINE_SHOW_ATTRIBUTE(p2p0_ht_info);
1104
1105 struct dentry *p2p0_dir = NULL;
1106 struct dentry *p2p0_survey_info, *p2p0_p2p_info, *p2p0_ht_info;
1107
1108 int ecrnx_debugfs_p2p0_init(void *private_data)
1109 {
1110     if (!ecr6600u_dir)
1111         return -ENOENT;
1112
1113     p2p0_dir = debugfs_create_dir("p2p0", ecr6600u_dir);
1114     if (!p2p0_dir)
1115         return -ENOENT;
1116
1117     p2p0_survey_info = debugfs_create_file("survey_info", 0444, p2p0_dir, private_data, &p2p0_survey_info_fops);
1118     if (!p2p0_survey_info)
1119         goto Fail;
1120
1121     p2p0_p2p_info = debugfs_create_file("p2p_info", 0444, p2p0_dir, private_data, &p2p_info_fops);
1122     if (!p2p0_p2p_info)
1123         goto Fail;
1124
1125     p2p0_ht_info = debugfs_create_file("ht_info", 0444, p2p0_dir, private_data, &p2p0_ht_info_fops);
1126     if (!p2p0_ht_info)
1127         goto Fail;
1128
1129     return 0;
1130
1131 Fail:
1132     debugfs_remove_recursive(p2p0_dir);
1133     p2p0_dir = NULL;
1134     return -ENOENT;
1135 }
1136
1137 void exrnx_debugfs_sruvey_info_tbl_init(void *private_data)
1138 {
1139         struct ecrnx_hw *ecrnx_hw = (struct ecrnx_hw*)private_data;
1140         INIT_LIST_HEAD(&ecrnx_hw->debugfs_survey_info_tbl_ptr);
1141 }
1142
1143 int ecrnx_debugfs_init(void     *private_data)
1144 {
1145     ecrnx_debugfs_info_init(private_data);
1146     ecrnx_debugfs_hw_init(private_data);
1147     ecrnx_debugfs_wlan0_init(private_data);
1148     ecrnx_debugfs_ap0_init(private_data);
1149     ecrnx_debugfs_p2p0_init(private_data);
1150     exrnx_debugfs_sruvey_info_tbl_init(private_data);
1151
1152     memset(&debugfs_info, 0, sizeof(debugfs_info_t));
1153     init_waitqueue_head(&debugfs_resp.rxdataq);
1154
1155     return 0;
1156 }
1157 #ifdef CONFIG_ECRNX_ESWIN_USB
1158 extern struct ring_buffer buf_handle;
1159 #endif
1160
1161 void ecrnx_debugfs_exit(void)  
1162 {
1163     if (ecr6600u_dir != NULL)
1164         debugfs_remove_recursive(ecr6600u_dir);
1165 #ifdef CONFIG_ECRNX_ESWIN_USB
1166     ring_buffer_deinit(&buf_handle);
1167 #endif
1168
1169 }
1170 #endif
1171