1718229f627861993cf15223075367cdbb8e63e9
[sfrench/cifs-2.6.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  * You should have received a copy of the GNU General Public License along with
15  * this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
17  *
18  *
19  ******************************************************************************/
20
21 #include <linux/module.h>
22 #include <linux/netdevice.h>
23
24 #include <rtw_android.h>
25 #include <osdep_service.h>
26 #include <rtw_debug.h>
27 #include <rtw_ioctl_set.h>
28
29 static const char *android_wifi_cmd_str[ANDROID_WIFI_CMD_MAX] = {
30         "START",
31         "STOP",
32         "SCAN-ACTIVE",
33         "SCAN-PASSIVE",
34         "RSSI",
35         "LINKSPEED",
36         "RXFILTER-START",
37         "RXFILTER-STOP",
38         "RXFILTER-ADD",
39         "RXFILTER-REMOVE",
40         "BTCOEXSCAN-START",
41         "BTCOEXSCAN-STOP",
42         "BTCOEXMODE",
43         "SETSUSPENDOPT",
44         "P2P_DEV_ADDR",
45         "SETFWPATH",
46         "SETBAND",
47         "GETBAND",
48         "COUNTRY",
49         "P2P_SET_NOA",
50         "P2P_GET_NOA",
51         "P2P_SET_PS",
52         "SET_AP_WPS_P2P_IE",
53         "MACADDR",
54         "BLOCK",
55         "WFD-ENABLE",
56         "WFD-DISABLE",
57         "WFD-SET-TCPPORT",
58         "WFD-SET-MAXTPUT",
59         "WFD-SET-DEVTYPE",
60 };
61
62 struct android_wifi_priv_cmd {
63         const char __user *buf;
64         int used_len;
65         int total_len;
66 };
67
68 /**
69  * Local (static) functions and variables
70  */
71
72 /* Initialize g_wifi_on to 1 so dhd_bus_start will be called for the first
73  * time (only) in dhd_open, subsequential wifi on will be handled by
74  * wl_android_wifi_on
75  */
76 static int g_wifi_on = true;
77
78 int rtw_android_cmdstr_to_num(char *cmdstr)
79 {
80         int cmd_num;
81         for (cmd_num = 0; cmd_num < ANDROID_WIFI_CMD_MAX; cmd_num++)
82                 if (0 == strnicmp(cmdstr , android_wifi_cmd_str[cmd_num],
83                                   strlen(android_wifi_cmd_str[cmd_num])))
84                         break;
85         return cmd_num;
86 }
87
88 static int rtw_android_get_rssi(struct net_device *net, char *command,
89                                 int total_len)
90 {
91         struct adapter *padapter = (struct adapter *)rtw_netdev_priv(net);
92         struct  mlme_priv       *pmlmepriv = &(padapter->mlmepriv);
93         struct  wlan_network    *pcur_network = &pmlmepriv->cur_network;
94         int bytes_written = 0;
95
96         if (check_fwstate(pmlmepriv, _FW_LINKED)) {
97                 bytes_written += snprintf(&command[bytes_written], total_len,
98                                           "%s rssi %d",
99                                           pcur_network->network.Ssid.Ssid,
100                                           padapter->recvpriv.rssi);
101         }
102         return bytes_written;
103 }
104
105 static int rtw_android_get_link_speed(struct net_device *net, char *command,
106                                       int total_len)
107 {
108         struct adapter *padapter = (struct adapter *)rtw_netdev_priv(net);
109         u16 link_speed;
110
111         link_speed = rtw_get_cur_max_rate(padapter) / 10;
112         return snprintf(command, total_len, "LinkSpeed %d",
113                                  link_speed);
114 }
115
116 static int rtw_android_get_macaddr(struct net_device *net, char *command,
117                                    int total_len)
118 {
119         return snprintf(command, total_len, "Macaddr = %pM",
120                                  net->dev_addr);
121 }
122
123 static int android_set_cntry(struct net_device *net, char *command,
124                              int total_len)
125 {
126         struct adapter *adapter = (struct adapter *)rtw_netdev_priv(net);
127         char *country_code = command + strlen(android_wifi_cmd_str[ANDROID_WIFI_CMD_COUNTRY]) + 1;
128         int ret;
129
130         ret = rtw_set_country(adapter, country_code);
131         return (ret == _SUCCESS) ? 0 : -1;
132 }
133
134 static int android_get_p2p_addr(struct net_device *net, char *command,
135                                         int total_len)
136 {
137         /* We use the same address as our HW MAC address */
138         memcpy(command, net->dev_addr, ETH_ALEN);
139         return ETH_ALEN;
140 }
141
142 static int rtw_android_set_block(struct net_device *net, char *command,
143                                  int total_len)
144 {
145         return 0;
146 }
147
148 int rtw_android_priv_cmd(struct net_device *net, struct ifreq *ifr, int cmd)
149 {
150         int ret = 0;
151         char *command = NULL;
152         int cmd_num;
153         int bytes_written = 0;
154         struct android_wifi_priv_cmd priv_cmd;
155
156         if (!ifr->ifr_data) {
157                 ret = -EINVAL;
158                 goto exit;
159         }
160         if (copy_from_user(&priv_cmd, ifr->ifr_data,
161                            sizeof(struct android_wifi_priv_cmd))) {
162                 ret = -EFAULT;
163                 goto exit;
164         }
165         command = kmalloc(priv_cmd.total_len, GFP_KERNEL);
166         if (!command) {
167                 DBG_88E("%s: failed to allocate memory\n", __func__);
168                 ret = -ENOMEM;
169                 goto exit;
170         }
171         if (!access_ok(VERIFY_READ, priv_cmd.buf, priv_cmd.total_len)) {
172                 DBG_88E("%s: failed to access memory\n", __func__);
173                 ret = -EFAULT;
174                 goto exit;
175         }
176         if (copy_from_user(command, (char __user *)priv_cmd.buf,
177                            priv_cmd.total_len)) {
178                 ret = -EFAULT;
179                 goto exit;
180         }
181         DBG_88E("%s: Android private cmd \"%s\" on %s\n",
182                 __func__, command, ifr->ifr_name);
183         cmd_num = rtw_android_cmdstr_to_num(command);
184         switch (cmd_num) {
185         case ANDROID_WIFI_CMD_START:
186                 goto response;
187         case ANDROID_WIFI_CMD_SETFWPATH:
188                 goto response;
189         }
190         if (!g_wifi_on) {
191                 DBG_88E("%s: Ignore private cmd \"%s\" - iface %s is down\n",
192                         __func__, command, ifr->ifr_name);
193                 ret = 0;
194                 goto exit;
195         }
196         switch (cmd_num) {
197         case ANDROID_WIFI_CMD_STOP:
198                 break;
199         case ANDROID_WIFI_CMD_SCAN_ACTIVE:
200                 break;
201         case ANDROID_WIFI_CMD_SCAN_PASSIVE:
202                 break;
203         case ANDROID_WIFI_CMD_RSSI:
204                 bytes_written = rtw_android_get_rssi(net, command,
205                                                      priv_cmd.total_len);
206                 break;
207         case ANDROID_WIFI_CMD_LINKSPEED:
208                 bytes_written = rtw_android_get_link_speed(net, command,
209                                                            priv_cmd.total_len);
210                 break;
211         case ANDROID_WIFI_CMD_MACADDR:
212                 bytes_written = rtw_android_get_macaddr(net, command,
213                                                         priv_cmd.total_len);
214                 break;
215         case ANDROID_WIFI_CMD_BLOCK:
216                 bytes_written = rtw_android_set_block(net, command,
217                                                       priv_cmd.total_len);
218                 break;
219         case ANDROID_WIFI_CMD_RXFILTER_START:
220                 break;
221         case ANDROID_WIFI_CMD_RXFILTER_STOP:
222                 break;
223         case ANDROID_WIFI_CMD_RXFILTER_ADD:
224                 break;
225         case ANDROID_WIFI_CMD_RXFILTER_REMOVE:
226                 break;
227         case ANDROID_WIFI_CMD_BTCOEXSCAN_START:
228                 /* TBD: BTCOEXSCAN-START */
229                 break;
230         case ANDROID_WIFI_CMD_BTCOEXSCAN_STOP:
231                 /* TBD: BTCOEXSCAN-STOP */
232                 break;
233         case ANDROID_WIFI_CMD_BTCOEXMODE:
234                 break;
235         case ANDROID_WIFI_CMD_SETSUSPENDOPT:
236                 break;
237         case ANDROID_WIFI_CMD_SETBAND:
238                 break;
239         case ANDROID_WIFI_CMD_GETBAND:
240                 break;
241         case ANDROID_WIFI_CMD_COUNTRY:
242                 bytes_written = android_set_cntry(net, command,
243                                                   priv_cmd.total_len);
244                 break;
245         case ANDROID_WIFI_CMD_P2P_DEV_ADDR:
246                 bytes_written = android_get_p2p_addr(net, command,
247                                                      priv_cmd.total_len);
248                 break;
249         case ANDROID_WIFI_CMD_P2P_SET_NOA:
250                 break;
251         case ANDROID_WIFI_CMD_P2P_GET_NOA:
252                 break;
253         case ANDROID_WIFI_CMD_P2P_SET_PS:
254                 break;
255         default:
256                 DBG_88E("Unknown PRIVATE command %s - ignored\n", command);
257                 snprintf(command, 3, "OK");
258                 bytes_written = strlen("OK");
259         }
260
261 response:
262         if (bytes_written >= 0) {
263                 if ((bytes_written == 0) && (priv_cmd.total_len > 0))
264                         command[0] = '\0';
265                 if (bytes_written >= priv_cmd.total_len) {
266                         DBG_88E("%s: bytes_written = %d\n", __func__,
267                                 bytes_written);
268                         bytes_written = priv_cmd.total_len;
269                 } else {
270                         bytes_written++;
271                 }
272                 priv_cmd.used_len = bytes_written;
273                 if (copy_to_user((char __user *)priv_cmd.buf, command,
274                                  bytes_written)) {
275                         DBG_88E("%s: failed to copy data to user buffer\n",
276                                 __func__);
277                         ret = -EFAULT;
278                 }
279         } else {
280                 ret = bytes_written;
281         }
282 exit:
283         kfree(command);
284         return ret;
285 }