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