cccdd328176fb1a9a81cce04d9f790b4ebc66be2
[firefly-linux-kernel-4.4.55.git] / drivers / net / wireless / rockchip_wlan / wifi_sys / rkwifi_sys_iface.c
1
2 #include <linux/kernel.h>
3 #include <linux/module.h>
4 #include <linux/init.h>
5 #include <linux/device.h>
6 #include <linux/err.h>
7 #include <linux/delay.h>
8 #include <linux/syscalls.h>
9 #include <linux/fs.h>
10 #include <asm/uaccess.h>
11 #include <linux/rfkill-wlan.h>
12
13 extern int get_wifi_chip_type(void);
14
15 static ssize_t wifi_chip_read(struct class *cls, struct class_attribute *attr, char *_buf)
16 {
17     int count = 0;
18     int type = get_wifi_chip_type();
19
20 #ifdef CONFIG_RTL8192CU
21     count = sprintf(_buf, "%s", "RTL8188CU");
22     printk("Current WiFi chip is RTL8188CU.\n");
23 #endif
24
25 #ifdef CONFIG_RTL8192DU
26     count = sprintf(_buf, "%s", "RTL8192DU");
27     printk("Current WiFi chip is RTL8192DU.\n");
28 #endif
29
30 if(type == WIFI_RTL8188EU) {
31 #ifdef CONFIG_RTL8188EU
32     count = sprintf(_buf, "%s", "RTL8188EU");
33     printk("Current WiFi chip is RTL8188EU.\n");
34 #endif
35 }
36
37 #ifdef CONFIG_RTL8723AU
38     count = sprintf(_buf, "%s", "RTL8723AU");
39     printk("Current WiFi chip is RTL8723AU.\n");
40 #endif
41
42 #ifdef CONFIG_RTL8723BS
43     count = sprintf(_buf, "%s", "RTL8723BS");
44     printk("Current WiFi chip is RTL8723BS.\n");
45 #endif
46
47 if(type == WIFI_RKWIFI) {
48 #ifdef CONFIG_BCM4330
49     count = sprintf(_buf, "%s", "BCM4330");
50     printk("Current WiFi chip is BCM4330.\n");
51 #endif
52
53 #ifdef CONFIG_RK901
54     count = sprintf(_buf, "%s", "RK901");
55     printk("Current WiFi chip is RK901.\n");
56 #endif
57
58 #ifdef CONFIG_RK903
59     count = sprintf(_buf, "%s", "RK903");
60     printk("Current WiFi chip is RK903.\n");
61 #endif
62
63 #ifdef CONFIG_AP6181
64     count = sprintf(_buf, "%s", "RK901");
65     printk("Current WiFi chip is AP6181.\n");
66 #endif
67
68 #ifdef CONFIG_AP6210
69     count = sprintf(_buf, "%s", "RK901");
70     printk("Current WiFi chip is AP6210.\n");
71 #endif
72
73 #ifdef CONFIG_AP6234
74     count = sprintf(_buf, "%s", "AP6234");
75     printk("Current WiFi chip is AP6234.\n");
76 #endif
77
78 #ifdef CONFIG_AP6330
79     count = sprintf(_buf, "%s", "RK903");
80     printk("Current WiFi chip is AP6330.\n");
81 #endif
82
83 #ifdef CONFIG_AP6335
84     count = sprintf(_buf, "%s", "AP6335");
85     printk("Current WiFi chip is AP6335.\n");
86 #endif
87
88 #ifdef CONFIG_AP6441
89     count = sprintf(_buf, "%s", "AP6441");
90     printk("Current WiFi chip is AP6441.\n");
91 #endif
92
93 #ifdef CONFIG_AP6476
94     count = sprintf(_buf, "%s", "RK901");
95     printk("Current WiFi chip is AP6476.\n");
96 #endif
97
98 #ifdef CONFIG_AP6493
99     count = sprintf(_buf, "%s", "RK903");
100     printk("Current WiFi chip is AP6493.\n");
101 #endif
102
103 #ifdef CONFIG_GB86302I
104     count = sprintf(_buf, "%s", "RK903");
105     printk("Current WiFi chip is GB86302I.\n");
106 #endif
107 }
108
109 #ifdef CONFIG_MTK_COMBO
110         count = sprintf(_buf, "%s", "MT6620");
111         printk("Current WiFi chip is MT6620.\n");
112 #endif
113
114 #ifdef CONFIG_MT5931
115     count = sprintf(_buf, "%s", "MT5931");
116     printk("Current WiFi chip is MT5931.\n");
117 #endif
118
119 #ifdef CONFIG_MT5931_MT6622
120     count = sprintf(_buf, "%s", "MT5931");
121     printk("Current WiFi chip is MT5931.\n");
122 #endif
123
124 #ifdef CONFIG_MTK_MT5931
125     count = sprintf(_buf, "%s", "MT5931");
126     printk("Current WiFi chip is MT5931.\n");
127 #endif
128
129 #ifdef CONFIG_MT7601
130     count = sprintf(_buf, "%s", "MT7601");
131     printk("Current WiFi chip is MT7601.\n");
132 #endif
133
134 if(type == WIFI_ESP8089) {
135 #ifdef CONFIG_ESP8089
136     count = sprintf(_buf, "%s", "ESP8089");
137     printk("Current WiFi chip is ESP8089.\n");
138 #endif
139 }
140
141     return count;
142 }
143
144 static ssize_t wifi_power_write(struct class *cls, struct class_attribute *attr, const char *_buf, size_t _count)
145 {
146     int poweren = 0;
147     poweren = simple_strtol(_buf, NULL, 10);
148     printk("%s: poweren = %d\n", __func__, poweren);
149     if(poweren > 0) {
150         rockchip_wifi_power(1);
151     } else {
152         rockchip_wifi_power(0);
153     }
154
155 return _count;
156 }
157
158 #ifdef CONFIG_WIFI_NONE
159 int rockchip_wifi_init_module(void) {return 0;}
160 void rockchip_wifi_exit_module(void) {return;}
161 #else
162 extern int rockchip_wifi_init_module(void);
163 extern void rockchip_wifi_exit_module(void);
164 extern int rockchip_wifi_init_module_rkwifi(void);
165 extern void rockchip_wifi_exit_module_rkwifi(void);
166 extern int rockchip_wifi_init_module_rtl8188eu(void);
167 extern void rockchip_wifi_exit_module_rtl8188eu(void);
168 extern int rockchip_wifi_init_module_esp8089(void);
169 extern void rockchip_wifi_exit_module_esp8089(void);
170 #endif
171 static struct semaphore driver_sem;
172 static int wifi_driver_insmod = 0;
173
174 static int wifi_init_exit_module(int enable)
175 {
176     int ret = 0;
177     int type = get_wifi_chip_type();
178 #ifdef CONFIG_RKWIFI
179     if (type == WIFI_RKWIFI) {
180         if (enable > 0)
181             ret = rockchip_wifi_init_module_rkwifi();
182         else
183             rockchip_wifi_exit_module_rkwifi();
184         return ret;
185     }
186 #endif
187 #ifdef CONFIG_RTL8188EU
188     if (type == WIFI_RTL8188EU) {
189         if (enable > 0) 
190             ret = rockchip_wifi_init_module_rtl8188eu();
191         else
192             rockchip_wifi_exit_module_rtl8188eu();
193         return ret;
194     }
195 #endif
196 #ifdef CONFIG_ESP8089
197     if (type == WIFI_ESP8089) {
198         if (enable > 0)  
199             ret = rockchip_wifi_init_module_esp8089();
200         else
201             rockchip_wifi_exit_module_esp8089();
202         return ret;
203     }
204 #endif
205
206 #if !defined(CONFIG_RKWIFI) && !defined(CONFIG_RTL8188EU) && !defined(CONFIG_ESP8089)
207     if (type >= 0) {
208         if (enable > 0)
209             ret = rockchip_wifi_init_module();
210         else
211             rockchip_wifi_exit_module();
212     }
213 #endif
214
215     return ret;
216 }
217
218 static ssize_t wifi_driver_write(struct class *cls, struct class_attribute *attr, const char *_buf, size_t _count)
219 {
220     int enable = 0, ret = 0;
221     
222     down(&driver_sem);
223     enable = simple_strtol(_buf, NULL, 10);
224     //printk("%s: enable = %d\n", __func__, enable);
225     if (wifi_driver_insmod == enable) {
226         printk("%s: wifi driver already %s\n", __func__, enable? "insmod":"rmmod");
227         up(&driver_sem);
228         return _count;
229     }
230     if(enable > 0) {
231         ret = wifi_init_exit_module(enable);
232         if (ret >= 0)
233             wifi_driver_insmod = enable;
234     } else {
235         wifi_init_exit_module(enable);
236         wifi_driver_insmod = enable;
237     }   
238
239     up(&driver_sem);
240     //printk("%s: ret = %d\n", __func__, ret);
241     return _count; 
242 }
243
244 static struct class *rkwifi_class = NULL;
245 static CLASS_ATTR(chip, 0664, wifi_chip_read, NULL);
246 static CLASS_ATTR(power, 0660, NULL, wifi_power_write);
247 static CLASS_ATTR(driver, 0660, NULL, wifi_driver_write);
248
249 int rkwifi_sysif_init(void)
250 {
251     int ret;
252     
253     printk("Rockchip WiFi SYS interface (V1.00) ... \n");
254     
255     rkwifi_class = NULL;
256     
257     rkwifi_class = class_create(THIS_MODULE, "rkwifi");
258     if (IS_ERR(rkwifi_class)) 
259     {   
260         printk("Create class rkwifi_class failed.\n");
261         return -ENOMEM;
262     }
263     
264     ret =  class_create_file(rkwifi_class, &class_attr_chip);
265     ret =  class_create_file(rkwifi_class, &class_attr_power);
266     ret =  class_create_file(rkwifi_class, &class_attr_driver);
267     sema_init(&driver_sem, 1);
268     
269     return 0;
270 }
271
272 void rkwifi_sysif_exit(void)
273 {
274     // need to remove the sys files and class
275     class_remove_file(rkwifi_class, &class_attr_chip);
276     class_remove_file(rkwifi_class, &class_attr_power);
277     class_remove_file(rkwifi_class, &class_attr_driver);
278     class_destroy(rkwifi_class);
279     
280     rkwifi_class = NULL;
281 }
282
283 module_init(rkwifi_sysif_init);
284 module_exit(rkwifi_sysif_exit);
285
286 MODULE_AUTHOR("Yongle Lai & gwl");
287 MODULE_DESCRIPTION("WiFi SYS @ Rockchip");
288 MODULE_LICENSE("GPL");
289