1 /*
2  * Copyright (c) 2015-2016 Quantenna Communications, Inc.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  */
15 
16 #include <linux/nl80211.h>
17 
18 #include "qlink_util.h"
19 
20 u16 qlink_iface_type_to_nl_mask(u16 qlink_type)
21 {
22 	u16 result = 0;
23 
24 	switch (qlink_type) {
25 	case QLINK_IFTYPE_AP:
26 		result |= BIT(NL80211_IFTYPE_AP);
27 		break;
28 	case QLINK_IFTYPE_STATION:
29 		result |= BIT(NL80211_IFTYPE_STATION);
30 		break;
31 	case QLINK_IFTYPE_ADHOC:
32 		result |= BIT(NL80211_IFTYPE_ADHOC);
33 		break;
34 	case QLINK_IFTYPE_MONITOR:
35 		result |= BIT(NL80211_IFTYPE_MONITOR);
36 		break;
37 	case QLINK_IFTYPE_WDS:
38 		result |= BIT(NL80211_IFTYPE_WDS);
39 		break;
40 	case QLINK_IFTYPE_AP_VLAN:
41 		result |= BIT(NL80211_IFTYPE_AP_VLAN);
42 		break;
43 	}
44 
45 	return result;
46 }
47 
48 u8 qlink_chan_width_mask_to_nl(u16 qlink_mask)
49 {
50 	u8 result = 0;
51 
52 	if (qlink_mask & BIT(QLINK_CHAN_WIDTH_5))
53 		result |= BIT(NL80211_CHAN_WIDTH_5);
54 
55 	if (qlink_mask & BIT(QLINK_CHAN_WIDTH_10))
56 		result |= BIT(NL80211_CHAN_WIDTH_10);
57 
58 	if (qlink_mask & BIT(QLINK_CHAN_WIDTH_20_NOHT))
59 		result |= BIT(NL80211_CHAN_WIDTH_20_NOHT);
60 
61 	if (qlink_mask & BIT(QLINK_CHAN_WIDTH_20))
62 		result |= BIT(NL80211_CHAN_WIDTH_20);
63 
64 	if (qlink_mask & BIT(QLINK_CHAN_WIDTH_40))
65 		result |= BIT(NL80211_CHAN_WIDTH_40);
66 
67 	if (qlink_mask & BIT(QLINK_CHAN_WIDTH_80))
68 		result |= BIT(NL80211_CHAN_WIDTH_80);
69 
70 	if (qlink_mask & BIT(QLINK_CHAN_WIDTH_80P80))
71 		result |= BIT(NL80211_CHAN_WIDTH_80P80);
72 
73 	if (qlink_mask & BIT(QLINK_CHAN_WIDTH_160))
74 		result |= BIT(NL80211_CHAN_WIDTH_160);
75 
76 	return result;
77 }
78 
79 static enum nl80211_chan_width qlink_chanwidth_to_nl(u8 qlw)
80 {
81 	switch (qlw) {
82 	case QLINK_CHAN_WIDTH_20_NOHT:
83 		return NL80211_CHAN_WIDTH_20_NOHT;
84 	case QLINK_CHAN_WIDTH_20:
85 		return NL80211_CHAN_WIDTH_20;
86 	case QLINK_CHAN_WIDTH_40:
87 		return NL80211_CHAN_WIDTH_40;
88 	case QLINK_CHAN_WIDTH_80:
89 		return NL80211_CHAN_WIDTH_80;
90 	case QLINK_CHAN_WIDTH_80P80:
91 		return NL80211_CHAN_WIDTH_80P80;
92 	case QLINK_CHAN_WIDTH_160:
93 		return NL80211_CHAN_WIDTH_160;
94 	case QLINK_CHAN_WIDTH_5:
95 		return NL80211_CHAN_WIDTH_5;
96 	case QLINK_CHAN_WIDTH_10:
97 		return NL80211_CHAN_WIDTH_10;
98 	default:
99 		return -1;
100 	}
101 }
102 
103 static u8 qlink_chanwidth_nl_to_qlink(enum nl80211_chan_width nlwidth)
104 {
105 	switch (nlwidth) {
106 	case NL80211_CHAN_WIDTH_20_NOHT:
107 		return QLINK_CHAN_WIDTH_20_NOHT;
108 	case NL80211_CHAN_WIDTH_20:
109 		return QLINK_CHAN_WIDTH_20;
110 	case NL80211_CHAN_WIDTH_40:
111 		return QLINK_CHAN_WIDTH_40;
112 	case NL80211_CHAN_WIDTH_80:
113 		return QLINK_CHAN_WIDTH_80;
114 	case NL80211_CHAN_WIDTH_80P80:
115 		return QLINK_CHAN_WIDTH_80P80;
116 	case NL80211_CHAN_WIDTH_160:
117 		return QLINK_CHAN_WIDTH_160;
118 	case NL80211_CHAN_WIDTH_5:
119 		return QLINK_CHAN_WIDTH_5;
120 	case NL80211_CHAN_WIDTH_10:
121 		return QLINK_CHAN_WIDTH_10;
122 	default:
123 		return -1;
124 	}
125 }
126 
127 void qlink_chandef_q2cfg(struct wiphy *wiphy,
128 			 const struct qlink_chandef *qch,
129 			 struct cfg80211_chan_def *chdef)
130 {
131 	struct ieee80211_channel *chan;
132 
133 	chan = ieee80211_get_channel(wiphy, le16_to_cpu(qch->chan.center_freq));
134 
135 	chdef->chan = chan;
136 	chdef->center_freq1 = le16_to_cpu(qch->center_freq1);
137 	chdef->center_freq2 = le16_to_cpu(qch->center_freq2);
138 	chdef->width = qlink_chanwidth_to_nl(qch->width);
139 }
140 
141 void qlink_chandef_cfg2q(const struct cfg80211_chan_def *chdef,
142 			 struct qlink_chandef *qch)
143 {
144 	struct ieee80211_channel *chan = chdef->chan;
145 
146 	qch->chan.hw_value = cpu_to_le16(chan->hw_value);
147 	qch->chan.center_freq = cpu_to_le16(chan->center_freq);
148 	qch->chan.flags = cpu_to_le32(chan->flags);
149 
150 	qch->center_freq1 = cpu_to_le16(chdef->center_freq1);
151 	qch->center_freq2 = cpu_to_le16(chdef->center_freq2);
152 	qch->width = qlink_chanwidth_nl_to_qlink(chdef->width);
153 }
154 
155 enum qlink_hidden_ssid qlink_hidden_ssid_nl2q(enum nl80211_hidden_ssid nl_val)
156 {
157 	switch (nl_val) {
158 	case NL80211_HIDDEN_SSID_ZERO_LEN:
159 		return QLINK_HIDDEN_SSID_ZERO_LEN;
160 	case NL80211_HIDDEN_SSID_ZERO_CONTENTS:
161 		return QLINK_HIDDEN_SSID_ZERO_CONTENTS;
162 	case NL80211_HIDDEN_SSID_NOT_IN_USE:
163 	default:
164 		return QLINK_HIDDEN_SSID_NOT_IN_USE;
165 	}
166 }
167 
168 bool qtnf_utils_is_bit_set(const u8 *arr, unsigned int bit,
169 			   unsigned int arr_max_len)
170 {
171 	unsigned int idx = bit / BITS_PER_BYTE;
172 	u8 mask = 1 << (bit - (idx * BITS_PER_BYTE));
173 
174 	if (idx >= arr_max_len)
175 		return false;
176 
177 	return arr[idx] & mask;
178 }
179 
180 void qlink_acl_data_cfg2q(const struct cfg80211_acl_data *acl,
181 			  struct qlink_acl_data *qacl)
182 {
183 	switch (acl->acl_policy) {
184 	case NL80211_ACL_POLICY_ACCEPT_UNLESS_LISTED:
185 		qacl->policy =
186 			cpu_to_le32(QLINK_ACL_POLICY_ACCEPT_UNLESS_LISTED);
187 		break;
188 	case NL80211_ACL_POLICY_DENY_UNLESS_LISTED:
189 		qacl->policy = cpu_to_le32(QLINK_ACL_POLICY_DENY_UNLESS_LISTED);
190 		break;
191 	}
192 
193 	qacl->num_entries = cpu_to_le32(acl->n_acl_entries);
194 	memcpy(qacl->mac_addrs, acl->mac_addrs,
195 	       acl->n_acl_entries * sizeof(*qacl->mac_addrs));
196 }
197