91569407be83be196e8e393420e3dc4a2adb0073
[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         if(type == WIFI_RK901) {
21             count = sprintf(_buf, "%s", "APRK901");
22             printk("Current WiFi chip is APRK901.\n");
23         }
24
25         if(type == WIFI_RK903) {
26             count = sprintf(_buf, "%s", "APRK903");
27             printk("Current WiFi chip is APRK903.\n");
28         }
29                 
30         if(type == WIFI_AP6181) {
31             count = sprintf(_buf, "%s", "AP6181");
32             printk("Current WiFi chip is AP6181.\n");
33         }
34
35         if(type == WIFI_AP6210) {
36             count = sprintf(_buf, "%s", "AP6210");
37             printk("Current WiFi chip is AP6210.\n");
38         }
39
40         if(type == WIFI_AP6212) {
41             count = sprintf(_buf, "%s", "AP6212");
42             printk("Current WiFi chip is AP6212.\n");
43         }
44         
45         if(type == WIFI_AP6234) {
46             count = sprintf(_buf, "%s", "AP6234");
47             printk("Current WiFi chip is AP6234.\n");
48         }
49         
50         if(type == WIFI_AP6330) {
51             count = sprintf(_buf, "%s", "AP6330");
52             printk("Current WiFi chip is AP6330.\n");
53         }
54         
55         if(type == WIFI_AP6335) {
56             count = sprintf(_buf, "%s", "AP6335");
57             printk("Current WiFi chip is AP6335.\n");
58         }
59
60         if(type == WIFI_AP6354) {
61             count = sprintf(_buf, "%s", "AP6354");
62             printk("Current WiFi chip is AP6354.\n");
63         }
64
65         if(type == WIFI_AP6441) {
66             count = sprintf(_buf, "%s", "AP6441");
67             printk("Current WiFi chip is AP6441.\n");
68         }
69
70         if(type == WIFI_AP6476) {
71             count = sprintf(_buf, "%s", "AP6476");
72             printk("Current WiFi chip is AP6476.\n");
73         }
74
75         if(type == WIFI_AP6493) {
76             count = sprintf(_buf, "%s", "AP6493");
77             printk("Current WiFi chip is AP6493.\n");
78         }
79
80         if(type == WIFI_RTL8188EU) {
81             count = sprintf(_buf, "%s", "RTL8188EU");
82             printk("Current WiFi chip is RTL8188EU.\n");
83         }
84         
85         if(type == WIFI_RTL8723BS) {
86             count = sprintf(_buf, "%s", "RTL8723BS");
87             printk("Current WiFi chip is RTL8723BS.\n");
88         }
89
90         if(type == WIFI_RTL8723AS) {
91             count = sprintf(_buf, "%s", "RTL8723AS");
92             printk("Current WiFi chip is RTL8723AS.\n");
93         }       
94         
95         if(type == WIFI_RTL8723BS_VQ0) {
96             count = sprintf(_buf, "%s", "RTL8723BS_VQ0");
97             printk("Current WiFi chip is RTL8723BS_VQ0.\n");
98         }               
99         
100         if(type == WIFI_RTL8723BU) {
101             count = sprintf(_buf, "%s", "RTL8723BU");
102             printk("Current WiFi chip is RTL8723BU.\n");
103         }
104
105         if(type == WIFI_RTL8723AU) {
106             count = sprintf(_buf, "%s", "RTL8723AU");
107             printk("Current WiFi chip is RTL8723AU.\n");
108         }                                                       
109
110         if(type == WIFI_RTL8189ES) {
111             count = sprintf(_buf, "%s", "RTL8189ES");
112             printk("Current WiFi chip is RTL8189ES.\n");
113         }       
114
115         if(type == WIFI_ESP8089) {
116             count = sprintf(_buf, "%s", "ESP8089");
117             printk("Current WiFi chip is ESP8089.\n");
118         }
119
120     return count;
121 }
122
123 static ssize_t wifi_power_write(struct class *cls, struct class_attribute *attr, const char *_buf, size_t _count)
124 {
125     int poweren = 0;
126     poweren = simple_strtol(_buf, NULL, 10);
127     printk("%s: poweren = %d\n", __func__, poweren);
128     if(poweren > 0) {
129         rockchip_wifi_power(1);
130     } else {
131         rockchip_wifi_power(0);
132     }
133
134 return _count;
135 }
136
137 #ifdef CONFIG_WIFI_NONE
138 int rockchip_wifi_init_module(void) {return 0;}
139 void rockchip_wifi_exit_module(void) {return;}
140 #else
141 extern int rockchip_wifi_init_module(void);
142 extern void rockchip_wifi_exit_module(void);
143 extern int rockchip_wifi_init_module_rkwifi(void);
144 extern void rockchip_wifi_exit_module_rkwifi(void);
145 extern int rockchip_wifi_init_module_rtkwifi(void);
146 extern void rockchip_wifi_exit_module_rtkwifi(void);
147 extern int rockchip_wifi_init_module_esp8089(void);
148 extern void rockchip_wifi_exit_module_esp8089(void);
149 #endif
150 static struct semaphore driver_sem;
151 static int wifi_driver_insmod = 0;
152
153 static int wifi_init_exit_module(int enable)
154 {
155         int ret = 0;
156         int type = 0;
157 #ifdef CONFIG_WIFI_LOAD_DRIVER_WHEN_KERNEL_BOOTUP
158         type = get_wifi_chip_type();
159 #else
160         type = get_wifi_chip_type();
161 //#ifdef CONFIG_RKWIFI
162         if (type < WIFI_AP6XXX_SERIES) {
163         if (enable > 0)
164             ret = rockchip_wifi_init_module_rkwifi();
165         else
166             rockchip_wifi_exit_module_rkwifi();
167         return ret;
168     }
169 //#endif
170 //#ifdef CONFIG_RTL_WIRELESS_SOLUTION
171     if (type < WIFI_RTL_SERIES) {
172         if (enable > 0)
173             ret = rockchip_wifi_init_module_rtkwifi();
174         else
175             rockchip_wifi_exit_module_rtkwifi();
176         return ret;
177     }
178 //#endif
179 #endif
180     return ret;
181 }
182
183 static ssize_t wifi_driver_write(struct class *cls, struct class_attribute *attr, const char *_buf, size_t _count)
184 {
185     int enable = 0, ret = 0;
186     
187     down(&driver_sem);
188     enable = simple_strtol(_buf, NULL, 10);
189     //printk("%s: enable = %d\n", __func__, enable);
190     if (wifi_driver_insmod == enable) {
191         printk("%s: wifi driver already %s\n", __func__, enable? "insmod":"rmmod");
192         up(&driver_sem);
193         return _count;
194     }
195     if(enable > 0) {
196         ret = wifi_init_exit_module(enable);
197         if (ret >= 0)
198             wifi_driver_insmod = enable;
199     } else {
200         wifi_init_exit_module(enable);
201         wifi_driver_insmod = enable;
202     }   
203
204     up(&driver_sem);
205     //printk("%s: ret = %d\n", __func__, ret);
206     return _count; 
207 }
208
209 static struct class *rkwifi_class = NULL;
210 static CLASS_ATTR(chip, 0664, wifi_chip_read, NULL);
211 static CLASS_ATTR(power, 0660, NULL, wifi_power_write);
212 static CLASS_ATTR(driver, 0660, NULL, wifi_driver_write);
213 #ifdef CONFIG_WIFI_LOAD_DRIVER_WHEN_KERNEL_BOOTUP
214 static CLASS_ATTR(preload, 0660, NULL, NULL);
215 #endif
216
217 int rkwifi_sysif_init(void)
218 {
219     int ret;
220     
221     printk("Rockchip WiFi SYS interface (V1.00) ... \n");
222     
223     rkwifi_class = NULL;
224     
225     rkwifi_class = class_create(THIS_MODULE, "rkwifi");
226     if (IS_ERR(rkwifi_class)) 
227     {   
228         printk("Create class rkwifi_class failed.\n");
229         return -ENOMEM;
230     }
231     
232     ret =  class_create_file(rkwifi_class, &class_attr_chip);
233     ret =  class_create_file(rkwifi_class, &class_attr_power);
234     ret =  class_create_file(rkwifi_class, &class_attr_driver);
235 #ifdef CONFIG_WIFI_LOAD_DRIVER_WHEN_KERNEL_BOOTUP
236     ret =  class_create_file(rkwifi_class, &class_attr_preload);
237 #endif
238     sema_init(&driver_sem, 1);
239     
240     return 0;
241 }
242
243 void rkwifi_sysif_exit(void)
244 {
245     // need to remove the sys files and class
246     class_remove_file(rkwifi_class, &class_attr_chip);
247     class_remove_file(rkwifi_class, &class_attr_power);
248     class_remove_file(rkwifi_class, &class_attr_driver);
249 #ifdef CONFIG_WIFI_LOAD_DRIVER_WHEN_KERNEL_BOOTUP
250     class_remove_file(rkwifi_class, &class_attr_preload);
251 #endif
252     class_destroy(rkwifi_class);
253     
254     rkwifi_class = NULL;
255 }
256
257 module_init(rkwifi_sysif_init);
258 module_exit(rkwifi_sysif_exit);
259
260 MODULE_AUTHOR("Yongle Lai & gwl");
261 MODULE_DESCRIPTION("WiFi SYS @ Rockchip");
262 MODULE_LICENSE("GPL");
263