Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/linville/wirel...
[deliverable/linux.git] / drivers / net / wireless / iwlwifi / iwl-phy-db.c
1 /******************************************************************************
2 *
3 * This file is provided under a dual BSD/GPLv2 license. When using or
4 * redistributing this file, you may do so under either license.
5 *
6 * GPL LICENSE SUMMARY
7 *
8 * Copyright(c) 2007 - 2012 Intel Corporation. All rights reserved.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of version 2 of the GNU General Public License as
12 * published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
22 * USA
23 *
24 * The full GNU General Public License is included in this distribution
25 * in the file called LICENSE.GPL.
26 *
27 * Contact Information:
28 * Intel Linux Wireless <ilw@linux.intel.com>
29 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
30 *
31 * BSD LICENSE
32 *
33 * Copyright(c) 2005 - 2012 Intel Corporation. All rights reserved.
34 * All rights reserved.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 *
40 * * Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * * Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in
44 * the documentation and/or other materials provided with the
45 * distribution.
46 * * Neither the name Intel Corporation nor the names of its
47 * contributors may be used to endorse or promote products derived
48 * from this software without specific prior written permission.
49 *
50 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
51 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
52 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
53 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
54 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
55 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
56 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
57 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
58 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
59 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
60 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
61 *
62 *****************************************************************************/
63
64 #include <linux/slab.h>
65 #include <linux/string.h>
66
67 #include "iwl-debug.h"
68 #include "iwl-shared.h"
69 #include "iwl-dev.h"
70
71 #include "iwl-phy-db.h"
72
73 #define CHANNEL_NUM_SIZE 4 /* num of channels in calib_ch size */
74
75 struct iwl_phy_db *iwl_phy_db_init(struct iwl_shared *shrd)
76 {
77 struct iwl_phy_db *phy_db = kzalloc(sizeof(struct iwl_phy_db),
78 GFP_KERNEL);
79
80 if (!phy_db)
81 return phy_db;
82
83 phy_db->shrd = shrd;
84
85 /* TODO: add default values of the phy db. */
86 return phy_db;
87 }
88
89 /*
90 * get phy db section: returns a pointer to a phy db section specified by
91 * type and channel group id.
92 */
93 static struct iwl_phy_db_entry *
94 iwl_phy_db_get_section(struct iwl_phy_db *phy_db,
95 enum iwl_phy_db_section_type type,
96 u16 chg_id)
97 {
98 if (!phy_db || type < 0 || type >= IWL_PHY_DB_MAX)
99 return NULL;
100
101 switch (type) {
102 case IWL_PHY_DB_CFG:
103 return &phy_db->cfg;
104 case IWL_PHY_DB_CALIB_NCH:
105 return &phy_db->calib_nch;
106 case IWL_PHY_DB_CALIB_CH:
107 return &phy_db->calib_ch;
108 case IWL_PHY_DB_CALIB_CHG_PAPD:
109 if (chg_id < 0 || chg_id >= IWL_NUM_PAPD_CH_GROUPS)
110 return NULL;
111 return &phy_db->calib_ch_group_papd[chg_id];
112 case IWL_PHY_DB_CALIB_CHG_TXP:
113 if (chg_id < 0 || chg_id >= IWL_NUM_TXP_CH_GROUPS)
114 return NULL;
115 return &phy_db->calib_ch_group_txp[chg_id];
116 default:
117 return NULL;
118 }
119 return NULL;
120 }
121
122 static void iwl_phy_db_free_section(struct iwl_phy_db *phy_db,
123 enum iwl_phy_db_section_type type,
124 u16 chg_id)
125 {
126 struct iwl_phy_db_entry *entry =
127 iwl_phy_db_get_section(phy_db, type, chg_id);
128 if (!entry)
129 return;
130
131 kfree(entry->data);
132 entry->data = NULL;
133 entry->size = 0;
134 }
135
136 void iwl_phy_db_free(struct iwl_phy_db *phy_db)
137 {
138 int i;
139
140 if (!phy_db)
141 return;
142
143 iwl_phy_db_free_section(phy_db, IWL_PHY_DB_CFG, 0);
144 iwl_phy_db_free_section(phy_db, IWL_PHY_DB_CALIB_NCH, 0);
145 iwl_phy_db_free_section(phy_db, IWL_PHY_DB_CALIB_CH, 0);
146 for (i = 0; i < IWL_NUM_PAPD_CH_GROUPS; i++)
147 iwl_phy_db_free_section(phy_db, IWL_PHY_DB_CALIB_CHG_PAPD, i);
148 for (i = 0; i < IWL_NUM_TXP_CH_GROUPS; i++)
149 iwl_phy_db_free_section(phy_db, IWL_PHY_DB_CALIB_CHG_TXP, i);
150
151 kfree(phy_db);
152 }
153
154 int iwl_phy_db_set_section(struct iwl_phy_db *phy_db,
155 enum iwl_phy_db_section_type type, u8 *data,
156 u16 size, gfp_t alloc_ctx)
157 {
158 struct iwl_phy_db_entry *entry;
159 u16 chg_id = 0;
160
161 if (!phy_db)
162 return -EINVAL;
163
164 if (type == IWL_PHY_DB_CALIB_CHG_PAPD ||
165 type == IWL_PHY_DB_CALIB_CHG_TXP)
166 chg_id = le16_to_cpup((__le16 *)data);
167
168 entry = iwl_phy_db_get_section(phy_db, type, chg_id);
169 if (!entry)
170 return -EINVAL;
171
172 kfree(entry->data);
173 entry->data = kmemdup(data, size, alloc_ctx);
174 if (!entry->data) {
175 entry->size = 0;
176 return -ENOMEM;
177 }
178
179 entry->size = size;
180
181 if (type == IWL_PHY_DB_CALIB_CH) {
182 phy_db->channel_num = le32_to_cpup((__le32 *)data);
183 phy_db->channel_size =
184 (size - CHANNEL_NUM_SIZE) / phy_db->channel_num;
185 }
186
187 return 0;
188 }
189
190 static int is_valid_channel(u16 ch_id)
191 {
192 if (ch_id <= 14 ||
193 (36 <= ch_id && ch_id <= 64 && ch_id % 4 == 0) ||
194 (100 <= ch_id && ch_id <= 140 && ch_id % 4 == 0) ||
195 (145 <= ch_id && ch_id <= 165 && ch_id % 4 == 1))
196 return 1;
197 return 0;
198 }
199
200 static u8 ch_id_to_ch_index(u16 ch_id)
201 {
202 if (WARN_ON(!is_valid_channel(ch_id)))
203 return 0xff;
204
205 if (ch_id <= 14)
206 return ch_id - 1;
207 if (ch_id <= 64)
208 return (ch_id + 20) / 4;
209 if (ch_id <= 140)
210 return (ch_id - 12) / 4;
211 return (ch_id - 13) / 4;
212 }
213
214
215 static u16 channel_id_to_papd(u16 ch_id)
216 {
217 if (WARN_ON(!is_valid_channel(ch_id)))
218 return 0xff;
219
220 if (1 <= ch_id && ch_id <= 14)
221 return 0;
222 if (36 <= ch_id && ch_id <= 64)
223 return 1;
224 if (100 <= ch_id && ch_id <= 140)
225 return 2;
226 return 3;
227 }
228
229 static u16 channel_id_to_txp(struct iwl_phy_db *phy_db, u16 ch_id)
230 {
231 struct iwl_phy_db_chg_txp *txp_chg;
232 int i;
233 u8 ch_index = ch_id_to_ch_index(ch_id);
234 if (ch_index == 0xff)
235 return 0xff;
236
237 for (i = 0; i < IWL_NUM_TXP_CH_GROUPS; i++) {
238 txp_chg = (void *)phy_db->calib_ch_group_txp[i].data;
239 if (!txp_chg)
240 return 0xff;
241 /*
242 * Looking for the first channel group that its max channel is
243 * higher then wanted channel.
244 */
245 if (le16_to_cpu(txp_chg->max_channel_idx) >= ch_index)
246 return i;
247 }
248 return 0xff;
249 }
250
251 int iwl_phy_db_get_section_data(struct iwl_phy_db *phy_db,
252 enum iwl_phy_db_section_type type, u8 **data,
253 u16 *size, u16 ch_id)
254 {
255 struct iwl_phy_db_entry *entry;
256 u32 channel_num;
257 u32 channel_size;
258 u16 ch_group_id = 0;
259 u16 index;
260
261 if (!phy_db)
262 return -EINVAL;
263
264 /* find wanted channel group */
265 if (type == IWL_PHY_DB_CALIB_CHG_PAPD)
266 ch_group_id = channel_id_to_papd(ch_id);
267 else if (type == IWL_PHY_DB_CALIB_CHG_TXP)
268 ch_group_id = channel_id_to_txp(phy_db, ch_id);
269
270 entry = iwl_phy_db_get_section(phy_db, type, ch_group_id);
271 if (!entry)
272 return -EINVAL;
273
274 if (type == IWL_PHY_DB_CALIB_CH) {
275 index = ch_id_to_ch_index(ch_id);
276 channel_num = phy_db->channel_num;
277 channel_size = phy_db->channel_size;
278 if (index >= channel_num) {
279 IWL_ERR(phy_db, "Wrong channel number %d", ch_id);
280 return -EINVAL;
281 }
282 *data = entry->data + CHANNEL_NUM_SIZE + index * channel_size;
283 *size = channel_size;
284 } else {
285 *data = entry->data;
286 *size = entry->size;
287 }
288 return 0;
289 }
This page took 0.051365 seconds and 5 git commands to generate.