GNU Linux-libre 4.9-gnu1
[releases.git] / drivers / staging / rtl8188eu / os_dep / rtw_android.c
1 /******************************************************************************
2  *
3  * Copyright(c) 2007 - 2011 Realtek Corporation. All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of version 2 of the GNU General Public License as
7  * published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12  * more details.
13  *
14  ******************************************************************************/
15
16 #include <linux/module.h>
17 #include <linux/netdevice.h>
18
19 #include <rtw_android.h>
20 #include <osdep_service.h>
21 #include <rtw_debug.h>
22 #include <rtw_ioctl_set.h>
23
24 static const char *android_wifi_cmd_str[ANDROID_WIFI_CMD_MAX] = {
25         "START",
26         "STOP",
27         "SCAN-ACTIVE",
28         "SCAN-PASSIVE",
29         "RSSI",
30         "LINKSPEED",
31         "RXFILTER-START",
32         "RXFILTER-STOP",
33         "RXFILTER-ADD",
34         "RXFILTER-REMOVE",
35         "BTCOEXSCAN-START",
36         "BTCOEXSCAN-STOP",
37         "BTCOEXMODE",
38         "SETSUSPENDOPT",
39         "P2P_DEV_ADDR",
40         "SETFWPATH",
41         "SETBAND",
42         "GETBAND",
43         "COUNTRY",
44         "P2P_SET_NOA",
45         "P2P_GET_NOA",
46         "P2P_SET_PS",
47         "SET_AP_WPS_P2P_IE",
48         "MACADDR",
49         "BLOCK",
50         "WFD-ENABLE",
51         "WFD-DISABLE",
52         "WFD-SET-TCPPORT",
53         "WFD-SET-MAXTPUT",
54         "WFD-SET-DEVTYPE",
55 };
56
57 struct android_wifi_priv_cmd {
58         const char __user *buf;
59         int used_len;
60         int total_len;
61 };
62
63 /**
64  * Local (static) functions and variables
65  */
66
67 /* Initialize g_wifi_on to 1 so dhd_bus_start will be called for the first
68  * time (only) in dhd_open, subsequential wifi on will be handled by
69  * wl_android_wifi_on
70  */
71 static int g_wifi_on = true;
72
73 int rtw_android_cmdstr_to_num(char *cmdstr)
74 {
75         int cmd_num;
76         for (cmd_num = 0; cmd_num < ANDROID_WIFI_CMD_MAX; cmd_num++)
77                 if (0 == strncasecmp(cmdstr, android_wifi_cmd_str[cmd_num],
78                                   strlen(android_wifi_cmd_str[cmd_num])))
79                         break;
80         return cmd_num;
81 }
82
83 static int rtw_android_get_rssi(struct net_device *net, char *command,
84                                 int total_len)
85 {
86         struct adapter *padapter = (struct adapter *)rtw_netdev_priv(net);
87         struct  mlme_priv       *pmlmepriv = &(padapter->mlmepriv);
88         struct  wlan_network    *pcur_network = &pmlmepriv->cur_network;
89         int bytes_written = 0;
90
91         if (check_fwstate(pmlmepriv, _FW_LINKED)) {
92                 bytes_written += snprintf(&command[bytes_written], total_len,
93                                           "%s rssi %d",
94                                           pcur_network->network.Ssid.Ssid,
95                                           padapter->recvpriv.rssi);
96         }
97         return bytes_written;
98 }
99
100 static int rtw_android_get_link_speed(struct net_device *net, char *command,
101                                       int total_len)
102 {
103         struct adapter *padapter = (struct adapter *)rtw_netdev_priv(net);
104         u16 link_speed;
105
106         link_speed = rtw_get_cur_max_rate(padapter) / 10;
107         return snprintf(command, total_len, "LinkSpeed %d",
108                                  link_speed);
109 }
110
111 static int rtw_android_get_macaddr(struct net_device *net, char *command,
112                                    int total_len)
113 {
114         return snprintf(command, total_len, "Macaddr = %pM",
115                                  net->dev_addr);
116 }
117
118 static int android_set_cntry(struct net_device *net, char *command,
119                              int total_len)
120 {
121         struct adapter *adapter = (struct adapter *)rtw_netdev_priv(net);
122         char *country_code = command + strlen(android_wifi_cmd_str[ANDROID_WIFI_CMD_COUNTRY]) + 1;
123         int ret;
124
125         ret = rtw_set_country(adapter, country_code);
126         return (ret == _SUCCESS) ? 0 : -1;
127 }
128
129 static int android_get_p2p_addr(struct net_device *net, char *command,
130                                         int total_len)
131 {
132         /* We use the same address as our HW MAC address */
133         memcpy(command, net->dev_addr, ETH_ALEN);
134         return ETH_ALEN;
135 }
136
137 static int rtw_android_set_block(struct net_device *net, char *command,
138                                  int total_len)
139 {
140         return 0;
141 }
142
143 int rtw_android_priv_cmd(struct net_device *net, struct ifreq *ifr, int cmd)
144 {
145         int ret = 0;
146         char *command;
147         int cmd_num;
148         int bytes_written = 0;
149         struct android_wifi_priv_cmd priv_cmd;
150
151         if (!ifr->ifr_data)
152                 return -EINVAL;
153         if (copy_from_user(&priv_cmd, ifr->ifr_data, sizeof(priv_cmd)))
154                 return -EFAULT;
155         if (priv_cmd.total_len < 1)
156                 return -EINVAL;
157         command = memdup_user(priv_cmd.buf, priv_cmd.total_len);
158         if (IS_ERR(command))
159                 return PTR_ERR(command);
160         command[priv_cmd.total_len - 1] = 0;
161         DBG_88E("%s: Android private cmd \"%s\" on %s\n",
162                 __func__, command, ifr->ifr_name);
163         cmd_num = rtw_android_cmdstr_to_num(command);
164         switch (cmd_num) {
165         case ANDROID_WIFI_CMD_START:
166                 goto response;
167         case ANDROID_WIFI_CMD_SETFWPATH:
168                 goto response;
169         }
170         if (!g_wifi_on) {
171                 DBG_88E("%s: Ignore private cmd \"%s\" - iface %s is down\n",
172                         __func__, command, ifr->ifr_name);
173                 ret = 0;
174                 goto free;
175         }
176         switch (cmd_num) {
177         case ANDROID_WIFI_CMD_STOP:
178                 break;
179         case ANDROID_WIFI_CMD_SCAN_ACTIVE:
180                 break;
181         case ANDROID_WIFI_CMD_SCAN_PASSIVE:
182                 break;
183         case ANDROID_WIFI_CMD_RSSI:
184                 bytes_written = rtw_android_get_rssi(net, command,
185                                                      priv_cmd.total_len);
186                 break;
187         case ANDROID_WIFI_CMD_LINKSPEED:
188                 bytes_written = rtw_android_get_link_speed(net, command,
189                                                            priv_cmd.total_len);
190                 break;
191         case ANDROID_WIFI_CMD_MACADDR:
192                 bytes_written = rtw_android_get_macaddr(net, command,
193                                                         priv_cmd.total_len);
194                 break;
195         case ANDROID_WIFI_CMD_BLOCK:
196                 bytes_written = rtw_android_set_block(net, command,
197                                                       priv_cmd.total_len);
198                 break;
199         case ANDROID_WIFI_CMD_RXFILTER_START:
200                 break;
201         case ANDROID_WIFI_CMD_RXFILTER_STOP:
202                 break;
203         case ANDROID_WIFI_CMD_RXFILTER_ADD:
204                 break;
205         case ANDROID_WIFI_CMD_RXFILTER_REMOVE:
206                 break;
207         case ANDROID_WIFI_CMD_BTCOEXSCAN_START:
208                 /* TBD: BTCOEXSCAN-START */
209                 break;
210         case ANDROID_WIFI_CMD_BTCOEXSCAN_STOP:
211                 /* TBD: BTCOEXSCAN-STOP */
212                 break;
213         case ANDROID_WIFI_CMD_BTCOEXMODE:
214                 break;
215         case ANDROID_WIFI_CMD_SETSUSPENDOPT:
216                 break;
217         case ANDROID_WIFI_CMD_SETBAND:
218                 break;
219         case ANDROID_WIFI_CMD_GETBAND:
220                 break;
221         case ANDROID_WIFI_CMD_COUNTRY:
222                 bytes_written = android_set_cntry(net, command,
223                                                   priv_cmd.total_len);
224                 break;
225         case ANDROID_WIFI_CMD_P2P_DEV_ADDR:
226                 bytes_written = android_get_p2p_addr(net, command,
227                                                      priv_cmd.total_len);
228                 break;
229         case ANDROID_WIFI_CMD_P2P_SET_NOA:
230                 break;
231         case ANDROID_WIFI_CMD_P2P_GET_NOA:
232                 break;
233         case ANDROID_WIFI_CMD_P2P_SET_PS:
234                 break;
235         default:
236                 DBG_88E("Unknown PRIVATE command %s - ignored\n", command);
237                 snprintf(command, 3, "OK");
238                 bytes_written = strlen("OK");
239         }
240
241 response:
242         if (bytes_written >= 0) {
243                 if ((bytes_written == 0) && (priv_cmd.total_len > 0))
244                         command[0] = '\0';
245                 if (bytes_written >= priv_cmd.total_len) {
246                         DBG_88E("%s: bytes_written = %d\n", __func__,
247                                 bytes_written);
248                         bytes_written = priv_cmd.total_len;
249                 } else {
250                         bytes_written++;
251                 }
252                 priv_cmd.used_len = bytes_written;
253                 if (copy_to_user((char __user *)priv_cmd.buf, command,
254                                  bytes_written)) {
255                         DBG_88E("%s: failed to copy data to user buffer\n",
256                                 __func__);
257                         ret = -EFAULT;
258                 }
259         } else {
260                 ret = bytes_written;
261         }
262 free:
263         kfree(command);
264         return ret;
265 }