1/*
2 * Driver interaction with extended Linux CFG8021
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * Alternatively, this software may be distributed under the terms of BSD
9 * license.
10 *
11 */
12
13#include "hardware_legacy/driver_nl80211.h"
14#include "wpa_supplicant_i.h"
15#include "config.h"
16#ifdef ANDROID
17#include "android_drv.h"
18#endif
19
20#define MAX_WPSP2PIE_CMD_SIZE		512
21
22typedef struct android_wifi_priv_cmd {
23	char *buf;
24	int used_len;
25	int total_len;
26} android_wifi_priv_cmd;
27
28static int drv_errors = 0;
29
30static void wpa_driver_send_hang_msg(struct wpa_driver_nl80211_data *drv)
31{
32	drv_errors++;
33	if (drv_errors > DRV_NUMBER_SEQUENTIAL_ERRORS) {
34		drv_errors = 0;
35		wpa_msg(drv->ctx, MSG_INFO, WPA_EVENT_DRIVER_STATE "HANGED");
36	}
37}
38
39int wpa_driver_nl80211_driver_cmd(void *priv, char *cmd, char *buf,
40				  size_t buf_len )
41{
42	struct i802_bss *bss = priv;
43	struct wpa_driver_nl80211_data *drv = bss->drv;
44	struct ifreq ifr;
45	android_wifi_priv_cmd priv_cmd;
46	int ret = 0;
47
48	if (os_strcasecmp(cmd, "STOP") == 0) {
49		linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 0);
50		wpa_msg(drv->ctx, MSG_INFO, WPA_EVENT_DRIVER_STATE "STOPPED");
51	} else if (os_strcasecmp(cmd, "START") == 0) {
52		linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1);
53		wpa_msg(drv->ctx, MSG_INFO, WPA_EVENT_DRIVER_STATE "STARTED");
54	} else if (os_strcasecmp(cmd, "MACADDR") == 0) {
55		u8 macaddr[ETH_ALEN] = {};
56
57		ret = linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname, macaddr);
58		if (!ret)
59			ret = os_snprintf(buf, buf_len,
60					  "Macaddr = " MACSTR "\n", MAC2STR(macaddr));
61	} else { /* Use private command */
62		os_memcpy(buf, cmd, strlen(cmd) + 1);
63		memset(&ifr, 0, sizeof(ifr));
64		memset(&priv_cmd, 0, sizeof(priv_cmd));
65		os_strncpy(ifr.ifr_name, bss->ifname, IFNAMSIZ);
66
67		priv_cmd.buf = buf;
68		priv_cmd.used_len = buf_len;
69		priv_cmd.total_len = buf_len;
70		ifr.ifr_data = &priv_cmd;
71
72		if ((ret = ioctl(drv->global->ioctl_sock, SIOCDEVPRIVATE + 1, &ifr)) < 0) {
73			wpa_printf(MSG_ERROR, "%s: failed to issue private commands\n", __func__);
74			wpa_driver_send_hang_msg(drv);
75		} else {
76			drv_errors = 0;
77			ret = 0;
78			if ((os_strcasecmp(cmd, "LINKSPEED") == 0) ||
79			    (os_strcasecmp(cmd, "RSSI") == 0) ||
80			    (os_strcasecmp(cmd, "GETBAND") == 0) ||
81			    (os_strncasecmp(cmd, "WLS_BATCHING", 12) == 0))
82				ret = strlen(buf);
83			else if ((os_strncasecmp(cmd, "COUNTRY", 7) == 0) ||
84				 (os_strncasecmp(cmd, "SETBAND", 7) == 0))
85				wpa_supplicant_event(drv->ctx,
86					EVENT_CHANNEL_LIST_CHANGED, NULL);
87			wpa_printf(MSG_DEBUG, "%s %s len = %d, %d", __func__, buf, ret, strlen(buf));
88		}
89	}
90	return ret;
91}
92
93int wpa_driver_set_p2p_noa(void *priv, u8 count, int start, int duration)
94{
95	char buf[MAX_DRV_CMD_SIZE];
96
97	memset(buf, 0, sizeof(buf));
98	wpa_printf(MSG_DEBUG, "%s: Entry", __func__);
99	snprintf(buf, sizeof(buf), "P2P_SET_NOA %d %d %d", count, start, duration);
100	return wpa_driver_nl80211_driver_cmd(priv, buf, buf, strlen(buf)+1);
101}
102
103int wpa_driver_get_p2p_noa(void *priv, u8 *buf, size_t len)
104{
105	/* Return 0 till we handle p2p_presence request completely in the driver */
106	return 0;
107}
108
109int wpa_driver_set_p2p_ps(void *priv, int legacy_ps, int opp_ps, int ctwindow)
110{
111	char buf[MAX_DRV_CMD_SIZE];
112
113	memset(buf, 0, sizeof(buf));
114	wpa_printf(MSG_DEBUG, "%s: Entry", __func__);
115	snprintf(buf, sizeof(buf), "P2P_SET_PS %d %d %d", legacy_ps, opp_ps, ctwindow);
116	return wpa_driver_nl80211_driver_cmd(priv, buf, buf, strlen(buf) + 1);
117}
118
119int wpa_driver_set_ap_wps_p2p_ie(void *priv, const struct wpabuf *beacon,
120				 const struct wpabuf *proberesp,
121				 const struct wpabuf *assocresp)
122{
123	char buf[MAX_WPSP2PIE_CMD_SIZE];
124	struct wpabuf *ap_wps_p2p_ie = NULL;
125	char *_cmd = "SET_AP_WPS_P2P_IE";
126	char *pbuf;
127	int ret = 0;
128	int i;
129	struct cmd_desc {
130		int cmd;
131		const struct wpabuf *src;
132	} cmd_arr[] = {
133		{0x1, beacon},
134		{0x2, proberesp},
135		{0x4, assocresp},
136		{-1, NULL}
137	};
138
139	wpa_printf(MSG_DEBUG, "%s: Entry", __func__);
140	for (i = 0; cmd_arr[i].cmd != -1; i++) {
141		os_memset(buf, 0, sizeof(buf));
142		pbuf = buf;
143		pbuf += sprintf(pbuf, "%s %d", _cmd, cmd_arr[i].cmd);
144		*pbuf++ = '\0';
145		ap_wps_p2p_ie = cmd_arr[i].src ?
146			wpabuf_dup(cmd_arr[i].src) : NULL;
147		if (ap_wps_p2p_ie) {
148			os_memcpy(pbuf, wpabuf_head(ap_wps_p2p_ie), wpabuf_len(ap_wps_p2p_ie));
149			ret = wpa_driver_nl80211_driver_cmd(priv, buf, buf,
150				strlen(_cmd) + 3 + wpabuf_len(ap_wps_p2p_ie));
151			wpabuf_free(ap_wps_p2p_ie);
152			if (ret < 0)
153				break;
154		}
155	}
156
157	return ret;
158}
159