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 void qlink_chandef_q2cfg(struct wiphy *wiphy,
104 			 const struct qlink_chandef *qch,
105 			 struct cfg80211_chan_def *chdef)
106 {
107 	chdef->center_freq1 = le16_to_cpu(qch->center_freq1);
108 	chdef->center_freq2 = le16_to_cpu(qch->center_freq2);
109 	chdef->width = qlink_chanwidth_to_nl(qch->width);
110 
111 	switch (chdef->width) {
112 	case NL80211_CHAN_WIDTH_20_NOHT:
113 	case NL80211_CHAN_WIDTH_20:
114 	case NL80211_CHAN_WIDTH_5:
115 	case NL80211_CHAN_WIDTH_10:
116 		chdef->chan = ieee80211_get_channel(wiphy, chdef->center_freq1);
117 		break;
118 	case NL80211_CHAN_WIDTH_40:
119 	case NL80211_CHAN_WIDTH_80:
120 	case NL80211_CHAN_WIDTH_80P80:
121 	case NL80211_CHAN_WIDTH_160:
122 		chdef->chan = ieee80211_get_channel(wiphy,
123 						    chdef->center_freq1 - 10);
124 		break;
125 	default:
126 		chdef->chan = NULL;
127 		break;
128 	}
129 }
130 
131 static u8 qlink_chanwidth_nl_to_qlink(enum nl80211_chan_width nlwidth)
132 {
133 	switch (nlwidth) {
134 	case NL80211_CHAN_WIDTH_20_NOHT:
135 		return QLINK_CHAN_WIDTH_20_NOHT;
136 	case NL80211_CHAN_WIDTH_20:
137 		return QLINK_CHAN_WIDTH_20;
138 	case NL80211_CHAN_WIDTH_40:
139 		return QLINK_CHAN_WIDTH_40;
140 	case NL80211_CHAN_WIDTH_80:
141 		return QLINK_CHAN_WIDTH_80;
142 	case NL80211_CHAN_WIDTH_80P80:
143 		return QLINK_CHAN_WIDTH_80P80;
144 	case NL80211_CHAN_WIDTH_160:
145 		return QLINK_CHAN_WIDTH_160;
146 	case NL80211_CHAN_WIDTH_5:
147 		return QLINK_CHAN_WIDTH_5;
148 	case NL80211_CHAN_WIDTH_10:
149 		return QLINK_CHAN_WIDTH_10;
150 	default:
151 		return -1;
152 	}
153 }
154 
155 void qlink_chandef_cfg2q(const struct cfg80211_chan_def *chdef,
156 			 struct qlink_chandef *qch)
157 {
158 	qch->center_freq1 = cpu_to_le16(chdef->center_freq1);
159 	qch->center_freq2 = cpu_to_le16(chdef->center_freq2);
160 	qch->width = qlink_chanwidth_nl_to_qlink(chdef->width);
161 }
162 
163 enum qlink_hidden_ssid qlink_hidden_ssid_nl2q(enum nl80211_hidden_ssid nl_val)
164 {
165 	switch (nl_val) {
166 	case NL80211_HIDDEN_SSID_ZERO_LEN:
167 		return QLINK_HIDDEN_SSID_ZERO_LEN;
168 	case NL80211_HIDDEN_SSID_ZERO_CONTENTS:
169 		return QLINK_HIDDEN_SSID_ZERO_CONTENTS;
170 	case NL80211_HIDDEN_SSID_NOT_IN_USE:
171 	default:
172 		return QLINK_HIDDEN_SSID_NOT_IN_USE;
173 	}
174 }
175