factoryToolV4.0 support on rk3168,lcd,bL,codec,tp,usbwifi,battery control by parameter
[firefly-linux-kernel-4.4.55.git] / drivers / net / wireless / 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
12 #ifdef CONFIG_MACH_RK_FAC
13 #include <mach/config.h>
14 extern int wifi_type;
15 #endif 
16 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 37))
17 static ssize_t wifi_chip_read(struct class *cls, struct class_attribute *attr, char *_buf)
18 #else
19 static ssize_t wifi_chip_read(struct class *cls, char *_buf)
20 #endif
21 {
22     int count = 0;
23 #ifdef CONFIG_MACH_RK_CONFIG
24                 if(wifi_type==WIFI_TYPE_RTL8188CU)
25                 {
26                         count = sprintf(_buf, "%s", "RTL8188CU");
27                         printk("Current WiFi chip is RTL8188CU.\n");
28     }else       if(wifi_type==WIFI_TYPE_RTL8188EU){
29                         count = sprintf(_buf, "%s", "RTL8188EU");
30                         printk("Current WiFi chip is RTL8188EU.\n");
31     }else if(wifi_type==WIFI_TYPE_MT7601){
32                         count = sprintf(_buf, "%s", "MT7601");
33                         printk("Current WiFi chip is MT7601.\n");
34     }else if(wifi_type==WIFI_TYPE_RTL8188ETV)
35                 {
36                          count = sprintf(_buf, "%s", "RTL8188ETV");
37          printk("Current WiFi chip is RTL8188ETV.\n");
38     }else if(wifi_type==WIFI_TYPE_MT5370)
39                 {
40                          count = sprintf(_buf, "%s", "MT5370");
41          printk("Current WiFi chip is MT5370.\n");
42     }   
43                 else    
44           {
45                 printk("NOT surpport type %d\n",wifi_type);
46           }
47
48 #else
49 #ifdef CONFIG_BCM4329
50     count = sprintf(_buf, "%s", "BCM4329");
51     printk("Current WiFi chip is BCM4329.\n");
52 #endif
53
54 #ifdef CONFIG_RTL8192CU
55     count = sprintf(_buf, "%s", "RTL8188CU");
56     printk("Current WiFi chip is RTL8188CU.\n");
57 #endif
58
59 #ifdef CONFIG_RTL8188EU
60     count = sprintf(_buf, "%s", "RTL8188EU");
61     printk("Current WiFi chip is RTL8188EU.\n");
62 #endif
63
64 #ifdef CONFIG_RTL8723AU
65     count = sprintf(_buf, "%s", "RTL8723AU");
66     printk("Current WiFi chip is RTL8723AU.\n");
67 #endif
68
69 #ifdef CONFIG_RTL8189ES
70     count = sprintf(_buf, "%s", "RTL8189ES");
71     printk("Current WiFi chip is RTL8189ES.\n");
72 #endif
73
74 #ifdef CONFIG_BCM4330
75     count = sprintf(_buf, "%s", "BCM4330");
76     printk("Current WiFi chip is BCM4330.\n");
77 #endif
78
79 #ifdef CONFIG_RK901
80     count = sprintf(_buf, "%s", "RK901");
81     printk("Current WiFi chip is RK901.\n");
82 #endif
83
84 #ifdef CONFIG_RK903
85     count = sprintf(_buf, "%s", "RK903");
86     printk("Current WiFi chip is RK903.\n");
87 #endif
88
89 #ifdef CONFIG_AP6181
90     count = sprintf(_buf, "%s", "RK901");
91     printk("Current WiFi chip is AP6181.\n");
92 #endif
93
94 #ifdef CONFIG_AP6210
95     count = sprintf(_buf, "%s", "RK901");
96     printk("Current WiFi chip is AP6210.\n");
97 #endif
98
99 #ifdef CONFIG_AP6330
100     count = sprintf(_buf, "%s", "RK903");
101     printk("Current WiFi chip is AP6330.\n");
102 #endif
103
104 #ifdef CONFIG_AP6476
105     count = sprintf(_buf, "%s", "RK901");
106     printk("Current WiFi chip is AP6476.\n");
107 #endif
108
109 #ifdef CONFIG_AP6493
110     count = sprintf(_buf, "%s", "RK903");
111     printk("Current WiFi chip is AP6493.\n");
112 #endif
113
114 #ifdef CONFIG_GB86302I
115     count = sprintf(_buf, "%s", "RK903");
116     printk("Current WiFi chip is GB86302I.\n");
117 #endif
118
119 #ifdef CONFIG_MTK_COMBO
120         count = sprintf(_buf, "%s", "MT6620");
121         printk("Current WiFi chip is MT6620.\n");
122 #endif
123
124 #ifdef CONFIG_RT5370
125     count = sprintf(_buf, "%s", "RT5370");
126     printk("Current WiFi chip is RT5370.\n");
127 #endif
128
129 #ifdef CONFIG_MT5931
130     count = sprintf(_buf, "%s", "MT5931");
131     printk("Current WiFi chip is MT5931.\n");
132 #endif
133
134 #ifdef CONFIG_MT5931_MT6622
135     count = sprintf(_buf, "%s", "MT5931");
136     printk("Current WiFi chip is MT5931.\n");
137 #endif
138
139 #ifdef CONFIG_MT7601
140     count = sprintf(_buf, "%s", "MT7601");
141     printk("Current WiFi chip is MT7601.\n");
142 #endif
143
144
145 #ifdef CONFIG_RTL8723AS
146     count = sprintf(_buf, "%s", "RTL8723AS");
147     printk("Current WiFi chip is RTL8723AS.\n");
148 #endif
149 #endif
150     return count;
151 }
152
153 /*
154 static ssize_t wifi_channel_write(struct class *cls, const char *_buf, size_t _count)
155 {
156     int ret, channel;
157     
158     if (wifi_enabled == 0)
159     {
160         printk("WiFi is disabled.\n");
161         return _count;
162     }
163     
164     channel = simple_strtol(_buf, NULL, 10);
165     
166     ret = wifi_emi_set_channel(channel);
167     if (ret != 0)
168     {
169         //printk("Set channel=%d fail.\n", channel);
170     }
171     else
172     {
173         //printk("Set channel=%d successfully.\n", channel);
174         wifi_channel = channel;
175     }
176     
177     return _count;
178 }
179 */
180
181 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 37))
182 static ssize_t wifi_p2p_read(struct class *cls, struct class_attribute *attr, char *_buf)
183 #else
184 static ssize_t wifi_p2p_read(struct class *cls, char *_buf)
185 #endif
186 {
187         int count = 0;
188
189 #ifdef CONFIG_BCM4329
190     count = sprintf(_buf, "%s", "false");
191         printk("Current WiFi chip BCM4329 doesn't support direct.(%s)\n", _buf);
192 #endif
193
194 #ifdef CONFIG_RTL8192CU
195     count = sprintf(_buf, "%s", "false");
196         printk("Current WiFi chip RTL8188 support direct.(%s)\n", _buf);
197 #endif
198
199 #ifdef CONFIG_RK903
200     count = sprintf(_buf, "%s", "true");
201             printk("Current WiFi chip RK903 support direct.(%s)\n", _buf);
202 #endif
203
204 #ifdef CONFIG_BCM4330
205     count = sprintf(_buf, "%s", "true");
206         printk("Current WiFi chip BCM4330 support direct.(%s)\n", _buf);
207 #endif
208
209         return count;
210 }
211
212 int wifi_pcba_test = 0;
213
214 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 37))
215 static ssize_t wifi_pcba_read(struct class *cls, struct class_attribute *attr, char *_buf)
216 #else
217 static ssize_t wifi_pcba_read(struct class *cls, char *_buf)
218 #endif
219 {
220         int count = 0;
221
222         count = sprintf(_buf, "%d", wifi_pcba_test);
223         return count;
224 }
225
226 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 37))
227 static ssize_t wifi_pcba_write(struct class *cls, struct class_attribute *attr, const char *_buf, size_t _count)
228 #else
229 static ssize_t wifi_pcba_write(struct class *cls, char *_buf, size_t _count)
230 #endif 
231 {
232         wifi_pcba_test = simple_strtol(_buf, NULL, 10);
233         if(wifi_pcba_test > 0) {
234             wifi_pcba_test = 1;
235         }
236         return _count;
237 }
238
239 #ifdef CONFIG_AIDC
240 int check_wifi_type_from_id(int id, char * _buf) {
241         int count = 0;
242
243         switch(id) {
244                 case 0x8179:
245                         count = sprintf(_buf, "%s", "RTL8188EU");
246                 printk("Current WiFi chip is RTL8188EU.\n");
247                         break;
248                 case 0x0179:
249                         count = sprintf(_buf, "%s", "RTL8188EU");
250                 printk("Current WiFi chip is RTL8188ETV.\n");
251                         break;
252                 case 0x5370:
253                         count = sprintf(_buf, "%s", "RT5370");
254                 printk("Current WiFi chip is RT5370.\n");
255                         break;
256                 case 0x0724:
257                         count = sprintf(_buf, "%s", "RTL8723AU");
258                 printk("Current WiFi chip is RTL8723AU.\n");
259                         break;
260                 case 0x8176:
261                         count = sprintf(_buf, "%s", "RTL8188CU");
262                 printk("Current WiFi chip is RTL8188CU.\n");
263                         break;
264                 case 0x018A:
265                         count = sprintf(_buf, "%s", "RTL8188CU");
266                 printk("Current WiFi chip is RTL8188CTV.\n");
267                         break;
268                 default:
269                 printk("Unsupported usb wifi.............\n");
270         }       
271         return count;   
272 }
273
274 extern int rk29sdk_wifi_power(int on);
275 extern int wifi_activate_usb(void);
276 extern int wifi_deactivate_usb(void);
277 #define USB_IDP_SYS_PATCH_1 "/sys/bus/usb/devices/1-1/idProduct"
278 #define USB_IDP_SYS_PATCH_2 "/sys/bus/usb/devices/2-1/idProduct"
279 #define USB_IDV_SYS_PATCH_1 "/sys/bus/usb/devices/1-1/idVendor"
280 #define USB_IDV_SYS_PATCH_2 "/sys/bus/usb/devices/2-1/idVendor"
281 #define USB_PRODUCT_SYS_PATCH "/sys/bus/usb/devices/1-1/product"
282 //5370 802.11 n WLAN
283 //8723 802.11n WLAN Adapter
284 //8188eu 802.11n NIC
285 //8188cu 802.11n WLAN Adapter
286 char aidc_type[20] = {0};
287
288 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 37))
289 static ssize_t wifi_aidc_read(struct class *cls, struct class_attribute *attr, char *_buf)
290 #else
291 static ssize_t wifi_aidc_read(struct class *cls, char *_buf)
292 #endif
293 {
294         int count = 0, retry = 10, idP = 0;// idV = 0;
295                 ssize_t nread;
296                 loff_t pos = 0;
297                 char usbid[20] = {0};
298                 struct file *file = NULL;
299                 mm_segment_t old_fs;
300                  
301                 sprintf(_buf, "%s", "UNKNOW");
302                 wifi_activate_usb();
303                 msleep(2000);
304                 old_fs = get_fs();
305                 set_fs(KERNEL_DS);
306                 while(retry--) {
307                         file = filp_open(USB_IDP_SYS_PATCH_2, O_RDONLY, 0);
308                         if (IS_ERR(file)) {
309                                 printk("\nCannot open \"%s\", retry = %d\n", USB_IDP_SYS_PATCH_2, retry);
310                                 file = filp_open(USB_IDP_SYS_PATCH_1, O_RDONLY, 0);
311                                 if (IS_ERR(file)) {
312                                 printk("\nCannot open \"%s\", retry = %d\n", USB_IDP_SYS_PATCH_1, retry);
313                                         msleep(500);
314                                         continue;
315                                 }
316                         }
317                         break;
318         }
319                 if(retry <= 0) {
320                         set_fs(old_fs);
321                         return count;
322                 }
323                 nread = vfs_read(file, (char __user *)usbid, sizeof(usbid), &pos);
324                 set_fs(old_fs);
325                 filp_close(file, NULL);
326                 wifi_deactivate_usb();
327                 idP = simple_strtol(usbid, NULL, 16);
328                 printk("Get usb wifi idProduct = 0X%04X\n", idP);
329                 count = check_wifi_type_from_id(idP, _buf);
330
331         return count;
332 }
333 #endif //CONFIG_AIDC
334
335 static struct class *rkwifi_class = NULL;
336 static CLASS_ATTR(chip, 0664, wifi_chip_read, NULL);
337 static CLASS_ATTR(p2p, 0664, wifi_p2p_read, NULL);
338 static CLASS_ATTR(pcba, 0664, wifi_pcba_read, wifi_pcba_write);
339 #ifdef CONFIG_AIDC
340 static CLASS_ATTR(aidc, 0664, wifi_aidc_read, NULL);
341 #endif
342
343 int rkwifi_sysif_init(void)
344 {
345     int ret;
346     
347     printk("Rockchip WiFi SYS interface (V1.00) ... \n");
348     
349     rkwifi_class = NULL;
350     
351     rkwifi_class = class_create(THIS_MODULE, "rkwifi");
352     if (IS_ERR(rkwifi_class)) 
353     {   
354         printk("Create class rkwifi_class failed.\n");
355         return -ENOMEM;
356     }
357     
358     ret =  class_create_file(rkwifi_class, &class_attr_chip);
359     ret =  class_create_file(rkwifi_class, &class_attr_p2p);
360     ret =  class_create_file(rkwifi_class, &class_attr_pcba);
361 #ifdef CONFIG_AIDC
362     ret =  class_create_file(rkwifi_class, &class_attr_aidc);
363 #endif
364     
365     return 0;
366 }
367
368 void rkwifi_sysif_exit(void)
369 {
370     // need to remove the sys files and class
371     class_remove_file(rkwifi_class, &class_attr_chip);
372     class_remove_file(rkwifi_class, &class_attr_p2p);
373     class_remove_file(rkwifi_class, &class_attr_pcba);
374 #ifdef CONFIG_AIDC
375     class_remove_file(rkwifi_class, &class_attr_aidc);
376 #endif
377     class_destroy(rkwifi_class);
378     
379     rkwifi_class = NULL;
380 }
381
382 module_init(rkwifi_sysif_init);
383 module_exit(rkwifi_sysif_exit);
384
385 MODULE_AUTHOR("Yongle Lai");
386 MODULE_DESCRIPTION("WiFi SYS @ Rockchip");
387 MODULE_LICENSE("GPL");
388