blob: f54e15fcd623e3e62dea6eccde604cee6759d9de [file] [log] [blame]
Christian Lamparter4c8a32f2009-06-23 10:36:26 -05001/*
2 * EEPROM parser code for mac80211 Prism54 drivers
3 *
4 * Copyright (c) 2006, Michael Wu <flamingice@sourmilk.net>
5 * Copyright (c) 2007-2009, Christian Lamparter <chunkeey@web.de>
6 * Copyright 2008, Johannes Berg <johannes@sipsolutions.net>
7 *
8 * Based on:
9 * - the islsm (softmac prism54) driver, which is:
10 * Copyright 2004-2006 Jean-Baptiste Note <jbnote@gmail.com>, et al.
11 * - stlc45xx driver
12 * Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies).
13 *
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License version 2 as
16 * published by the Free Software Foundation.
17 */
18
19#include <linux/init.h>
20#include <linux/firmware.h>
21#include <linux/etherdevice.h>
Christian Lamparter1a9b6672009-07-11 01:22:26 +020022#include <linux/sort.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
Christian Lamparter4c8a32f2009-06-23 10:36:26 -050024
25#include <net/mac80211.h>
Christian Lamparterd7eb50c2010-08-17 01:16:58 +020026#include <linux/crc-ccitt.h>
Christian Lamparter4c8a32f2009-06-23 10:36:26 -050027
28#include "p54.h"
29#include "eeprom.h"
30#include "lmac.h"
31
32static struct ieee80211_rate p54_bgrates[] = {
33 { .bitrate = 10, .hw_value = 0, },
34 { .bitrate = 20, .hw_value = 1, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
35 { .bitrate = 55, .hw_value = 2, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
36 { .bitrate = 110, .hw_value = 3, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
37 { .bitrate = 60, .hw_value = 4, },
38 { .bitrate = 90, .hw_value = 5, },
39 { .bitrate = 120, .hw_value = 6, },
40 { .bitrate = 180, .hw_value = 7, },
41 { .bitrate = 240, .hw_value = 8, },
42 { .bitrate = 360, .hw_value = 9, },
43 { .bitrate = 480, .hw_value = 10, },
44 { .bitrate = 540, .hw_value = 11, },
45};
46
Christian Lamparter4c8a32f2009-06-23 10:36:26 -050047static struct ieee80211_rate p54_arates[] = {
48 { .bitrate = 60, .hw_value = 4, },
49 { .bitrate = 90, .hw_value = 5, },
50 { .bitrate = 120, .hw_value = 6, },
51 { .bitrate = 180, .hw_value = 7, },
52 { .bitrate = 240, .hw_value = 8, },
53 { .bitrate = 360, .hw_value = 9, },
54 { .bitrate = 480, .hw_value = 10, },
55 { .bitrate = 540, .hw_value = 11, },
56};
57
Christian Lamparter7a047f42011-02-12 22:32:49 +010058static struct p54_rssi_db_entry p54_rssi_default = {
59 /*
60 * The defaults are taken from usb-logs of the
61 * vendor driver. So, they should be safe to
62 * use in case we can't get a match from the
63 * rssi <-> dBm conversion database.
64 */
65 .mul = 130,
66 .add = -398,
67};
68
Christian Lamparter1a9b6672009-07-11 01:22:26 +020069#define CHAN_HAS_CAL BIT(0)
70#define CHAN_HAS_LIMIT BIT(1)
71#define CHAN_HAS_CURVE BIT(2)
72#define CHAN_HAS_ALL (CHAN_HAS_CAL | CHAN_HAS_LIMIT | CHAN_HAS_CURVE)
73
74struct p54_channel_entry {
75 u16 freq;
76 u16 data;
77 int index;
78 enum ieee80211_band band;
Christian Lamparter4c8a32f2009-06-23 10:36:26 -050079};
80
Christian Lamparter1a9b6672009-07-11 01:22:26 +020081struct p54_channel_list {
82 struct p54_channel_entry *channels;
83 size_t entries;
84 size_t max_entries;
85 size_t band_channel_num[IEEE80211_NUM_BANDS];
Christian Lamparter4c8a32f2009-06-23 10:36:26 -050086};
87
Christian Lamparter1a9b6672009-07-11 01:22:26 +020088static int p54_get_band_from_freq(u16 freq)
89{
90 /* FIXME: sync these values with the 802.11 spec */
91
92 if ((freq >= 2412) && (freq <= 2484))
93 return IEEE80211_BAND_2GHZ;
94
95 if ((freq >= 4920) && (freq <= 5825))
96 return IEEE80211_BAND_5GHZ;
97
98 return -1;
99}
100
Christian Lamparter7a047f42011-02-12 22:32:49 +0100101static int same_band(u16 freq, u16 freq2)
102{
103 return p54_get_band_from_freq(freq) == p54_get_band_from_freq(freq2);
104}
105
Christian Lamparter1a9b6672009-07-11 01:22:26 +0200106static int p54_compare_channels(const void *_a,
107 const void *_b)
108{
109 const struct p54_channel_entry *a = _a;
110 const struct p54_channel_entry *b = _b;
111
Christian Lamparter192abec2011-02-12 21:49:38 +0100112 return a->freq - b->freq;
Christian Lamparter1a9b6672009-07-11 01:22:26 +0200113}
114
Christian Lamparter7a047f42011-02-12 22:32:49 +0100115static int p54_compare_rssichan(const void *_a,
116 const void *_b)
117{
118 const struct p54_rssi_db_entry *a = _a;
119 const struct p54_rssi_db_entry *b = _b;
120
121 return a->freq - b->freq;
122}
123
Christian Lamparter1a9b6672009-07-11 01:22:26 +0200124static int p54_fill_band_bitrates(struct ieee80211_hw *dev,
125 struct ieee80211_supported_band *band_entry,
126 enum ieee80211_band band)
127{
128 /* TODO: generate rate array dynamically */
129
130 switch (band) {
131 case IEEE80211_BAND_2GHZ:
132 band_entry->bitrates = p54_bgrates;
133 band_entry->n_bitrates = ARRAY_SIZE(p54_bgrates);
134 break;
135 case IEEE80211_BAND_5GHZ:
136 band_entry->bitrates = p54_arates;
137 band_entry->n_bitrates = ARRAY_SIZE(p54_arates);
138 break;
139 default:
140 return -EINVAL;
141 }
142
143 return 0;
144}
145
146static int p54_generate_band(struct ieee80211_hw *dev,
147 struct p54_channel_list *list,
148 enum ieee80211_band band)
149{
150 struct p54_common *priv = dev->priv;
151 struct ieee80211_supported_band *tmp, *old;
152 unsigned int i, j;
153 int ret = -ENOMEM;
154
155 if ((!list->entries) || (!list->band_channel_num[band]))
Christian Lamparter93a59d72009-10-31 22:59:27 +0100156 return -EINVAL;
Christian Lamparter1a9b6672009-07-11 01:22:26 +0200157
158 tmp = kzalloc(sizeof(*tmp), GFP_KERNEL);
159 if (!tmp)
160 goto err_out;
161
162 tmp->channels = kzalloc(sizeof(struct ieee80211_channel) *
163 list->band_channel_num[band], GFP_KERNEL);
164 if (!tmp->channels)
165 goto err_out;
166
167 ret = p54_fill_band_bitrates(dev, tmp, band);
168 if (ret)
169 goto err_out;
170
171 for (i = 0, j = 0; (j < list->band_channel_num[band]) &&
172 (i < list->entries); i++) {
Christian Lampartera3162ee2011-02-12 22:14:38 +0100173 struct p54_channel_entry *chan = &list->channels[i];
Christian Lamparter1a9b6672009-07-11 01:22:26 +0200174
Christian Lampartera3162ee2011-02-12 22:14:38 +0100175 if (chan->band != band)
Christian Lamparter1a9b6672009-07-11 01:22:26 +0200176 continue;
177
Christian Lampartera3162ee2011-02-12 22:14:38 +0100178 if (chan->data != CHAN_HAS_ALL) {
179 wiphy_err(dev->wiphy, "%s%s%s is/are missing for "
180 "channel:%d [%d MHz].\n",
181 (chan->data & CHAN_HAS_CAL ? "" :
Joe Perchesc96c31e2010-07-26 14:39:58 -0700182 " [iqauto calibration data]"),
Christian Lampartera3162ee2011-02-12 22:14:38 +0100183 (chan->data & CHAN_HAS_LIMIT ? "" :
Joe Perchesc96c31e2010-07-26 14:39:58 -0700184 " [output power limits]"),
Christian Lampartera3162ee2011-02-12 22:14:38 +0100185 (chan->data & CHAN_HAS_CURVE ? "" :
Joe Perchesc96c31e2010-07-26 14:39:58 -0700186 " [curve data]"),
Christian Lampartera3162ee2011-02-12 22:14:38 +0100187 chan->index, chan->freq);
Christian Lamparter93a59d72009-10-31 22:59:27 +0100188 continue;
Christian Lamparter1a9b6672009-07-11 01:22:26 +0200189 }
190
Christian Lampartera3162ee2011-02-12 22:14:38 +0100191 tmp->channels[j].band = chan->band;
192 tmp->channels[j].center_freq = chan->freq;
Christian Lamparter1a9b6672009-07-11 01:22:26 +0200193 j++;
194 }
195
Christian Lamparter93a59d72009-10-31 22:59:27 +0100196 if (j == 0) {
Joe Perches5db55842010-08-11 19:11:19 -0700197 wiphy_err(dev->wiphy, "Disabling totally damaged %d GHz band\n",
Joe Perchesc96c31e2010-07-26 14:39:58 -0700198 (band == IEEE80211_BAND_2GHZ) ? 2 : 5);
Christian Lamparter93a59d72009-10-31 22:59:27 +0100199
200 ret = -ENODATA;
201 goto err_out;
202 }
203
204 tmp->n_channels = j;
Christian Lamparter1a9b6672009-07-11 01:22:26 +0200205 old = priv->band_table[band];
206 priv->band_table[band] = tmp;
207 if (old) {
208 kfree(old->channels);
209 kfree(old);
210 }
211
212 return 0;
213
214err_out:
215 if (tmp) {
216 kfree(tmp->channels);
217 kfree(tmp);
218 }
219
220 return ret;
221}
222
223static void p54_update_channel_param(struct p54_channel_list *list,
224 u16 freq, u16 data)
225{
226 int band, i;
227
228 /*
229 * usually all lists in the eeprom are mostly sorted.
230 * so it's very likely that the entry we are looking for
231 * is right at the end of the list
232 */
233 for (i = list->entries; i >= 0; i--) {
234 if (freq == list->channels[i].freq) {
235 list->channels[i].data |= data;
236 break;
237 }
238 }
239
240 if ((i < 0) && (list->entries < list->max_entries)) {
241 /* entry does not exist yet. Initialize a new one. */
242 band = p54_get_band_from_freq(freq);
243
244 /*
245 * filter out frequencies which don't belong into
246 * any supported band.
247 */
248 if (band < 0)
249 return ;
250
251 i = list->entries++;
252 list->band_channel_num[band]++;
253
254 list->channels[i].freq = freq;
255 list->channels[i].data = data;
256 list->channels[i].band = band;
257 list->channels[i].index = ieee80211_frequency_to_channel(freq);
258 /* TODO: parse output_limit and fill max_power */
259 }
260}
261
262static int p54_generate_channel_lists(struct ieee80211_hw *dev)
263{
264 struct p54_common *priv = dev->priv;
265 struct p54_channel_list *list;
266 unsigned int i, j, max_channel_num;
Christian Lamparter93a59d72009-10-31 22:59:27 +0100267 int ret = 0;
Christian Lamparter1a9b6672009-07-11 01:22:26 +0200268 u16 freq;
269
270 if ((priv->iq_autocal_len != priv->curve_data->entries) ||
271 (priv->iq_autocal_len != priv->output_limit->entries))
Joe Perchesc96c31e2010-07-26 14:39:58 -0700272 wiphy_err(dev->wiphy,
273 "Unsupported or damaged EEPROM detected. "
274 "You may not be able to use all channels.\n");
Christian Lamparter1a9b6672009-07-11 01:22:26 +0200275
276 max_channel_num = max_t(unsigned int, priv->output_limit->entries,
277 priv->iq_autocal_len);
278 max_channel_num = max_t(unsigned int, max_channel_num,
279 priv->curve_data->entries);
280
281 list = kzalloc(sizeof(*list), GFP_KERNEL);
Christian Lamparter93a59d72009-10-31 22:59:27 +0100282 if (!list) {
283 ret = -ENOMEM;
Christian Lamparter1a9b6672009-07-11 01:22:26 +0200284 goto free;
Christian Lamparter93a59d72009-10-31 22:59:27 +0100285 }
Christian Lamparter1a9b6672009-07-11 01:22:26 +0200286
287 list->max_entries = max_channel_num;
288 list->channels = kzalloc(sizeof(struct p54_channel_entry) *
289 max_channel_num, GFP_KERNEL);
Julia Lawall0d91f222010-10-15 15:00:06 +0200290 if (!list->channels) {
291 ret = -ENOMEM;
Christian Lamparter1a9b6672009-07-11 01:22:26 +0200292 goto free;
Julia Lawall0d91f222010-10-15 15:00:06 +0200293 }
Christian Lamparter1a9b6672009-07-11 01:22:26 +0200294
295 for (i = 0; i < max_channel_num; i++) {
296 if (i < priv->iq_autocal_len) {
297 freq = le16_to_cpu(priv->iq_autocal[i].freq);
298 p54_update_channel_param(list, freq, CHAN_HAS_CAL);
299 }
300
301 if (i < priv->output_limit->entries) {
302 freq = le16_to_cpup((__le16 *) (i *
303 priv->output_limit->entry_size +
304 priv->output_limit->offset +
305 priv->output_limit->data));
306
307 p54_update_channel_param(list, freq, CHAN_HAS_LIMIT);
308 }
309
310 if (i < priv->curve_data->entries) {
311 freq = le16_to_cpup((__le16 *) (i *
312 priv->curve_data->entry_size +
313 priv->curve_data->offset +
314 priv->curve_data->data));
315
316 p54_update_channel_param(list, freq, CHAN_HAS_CURVE);
317 }
318 }
319
Christian Lamparter192abec2011-02-12 21:49:38 +0100320 /* sort the channel list by frequency */
Christian Lamparter1a9b6672009-07-11 01:22:26 +0200321 sort(list->channels, list->entries, sizeof(struct p54_channel_entry),
322 p54_compare_channels, NULL);
323
324 for (i = 0, j = 0; i < IEEE80211_NUM_BANDS; i++) {
Christian Lamparter93a59d72009-10-31 22:59:27 +0100325 if (p54_generate_band(dev, list, i) == 0)
Christian Lamparter1a9b6672009-07-11 01:22:26 +0200326 j++;
Christian Lamparter1a9b6672009-07-11 01:22:26 +0200327 }
328 if (j == 0) {
329 /* no useable band available. */
330 ret = -EINVAL;
331 }
332
333free:
334 if (list) {
335 kfree(list->channels);
336 kfree(list);
337 }
338
339 return ret;
340}
341
Christian Lamparter4c8a32f2009-06-23 10:36:26 -0500342static int p54_convert_rev0(struct ieee80211_hw *dev,
343 struct pda_pa_curve_data *curve_data)
344{
345 struct p54_common *priv = dev->priv;
346 struct p54_pa_curve_data_sample *dst;
347 struct pda_pa_curve_data_sample_rev0 *src;
348 size_t cd_len = sizeof(*curve_data) +
349 (curve_data->points_per_channel*sizeof(*dst) + 2) *
350 curve_data->channels;
351 unsigned int i, j;
352 void *source, *target;
353
354 priv->curve_data = kmalloc(sizeof(*priv->curve_data) + cd_len,
355 GFP_KERNEL);
356 if (!priv->curve_data)
357 return -ENOMEM;
358
359 priv->curve_data->entries = curve_data->channels;
360 priv->curve_data->entry_size = sizeof(__le16) +
361 sizeof(*dst) * curve_data->points_per_channel;
362 priv->curve_data->offset = offsetof(struct pda_pa_curve_data, data);
363 priv->curve_data->len = cd_len;
364 memcpy(priv->curve_data->data, curve_data, sizeof(*curve_data));
365 source = curve_data->data;
366 target = ((struct pda_pa_curve_data *) priv->curve_data->data)->data;
367 for (i = 0; i < curve_data->channels; i++) {
368 __le16 *freq = source;
369 source += sizeof(__le16);
370 *((__le16 *)target) = *freq;
371 target += sizeof(__le16);
372 for (j = 0; j < curve_data->points_per_channel; j++) {
373 dst = target;
374 src = source;
375
376 dst->rf_power = src->rf_power;
377 dst->pa_detector = src->pa_detector;
378 dst->data_64qam = src->pcv;
379 /* "invent" the points for the other modulations */
380#define SUB(x, y) (u8)(((x) - (y)) > (x) ? 0 : (x) - (y))
381 dst->data_16qam = SUB(src->pcv, 12);
382 dst->data_qpsk = SUB(dst->data_16qam, 12);
383 dst->data_bpsk = SUB(dst->data_qpsk, 12);
384 dst->data_barker = SUB(dst->data_bpsk, 14);
385#undef SUB
386 target += sizeof(*dst);
387 source += sizeof(*src);
388 }
389 }
390
391 return 0;
392}
393
394static int p54_convert_rev1(struct ieee80211_hw *dev,
395 struct pda_pa_curve_data *curve_data)
396{
397 struct p54_common *priv = dev->priv;
398 struct p54_pa_curve_data_sample *dst;
399 struct pda_pa_curve_data_sample_rev1 *src;
400 size_t cd_len = sizeof(*curve_data) +
401 (curve_data->points_per_channel*sizeof(*dst) + 2) *
402 curve_data->channels;
403 unsigned int i, j;
404 void *source, *target;
405
406 priv->curve_data = kzalloc(cd_len + sizeof(*priv->curve_data),
407 GFP_KERNEL);
408 if (!priv->curve_data)
409 return -ENOMEM;
410
411 priv->curve_data->entries = curve_data->channels;
412 priv->curve_data->entry_size = sizeof(__le16) +
413 sizeof(*dst) * curve_data->points_per_channel;
414 priv->curve_data->offset = offsetof(struct pda_pa_curve_data, data);
415 priv->curve_data->len = cd_len;
416 memcpy(priv->curve_data->data, curve_data, sizeof(*curve_data));
417 source = curve_data->data;
418 target = ((struct pda_pa_curve_data *) priv->curve_data->data)->data;
419 for (i = 0; i < curve_data->channels; i++) {
420 __le16 *freq = source;
421 source += sizeof(__le16);
422 *((__le16 *)target) = *freq;
423 target += sizeof(__le16);
424 for (j = 0; j < curve_data->points_per_channel; j++) {
425 memcpy(target, source, sizeof(*src));
426
427 target += sizeof(*dst);
428 source += sizeof(*src);
429 }
430 source++;
431 }
432
433 return 0;
434}
435
436static const char *p54_rf_chips[] = { "INVALID-0", "Duette3", "Duette2",
437 "Frisbee", "Xbow", "Longbow", "INVALID-6", "INVALID-7" };
438
Christian Lamparter7a047f42011-02-12 22:32:49 +0100439static int p54_parse_rssical(struct ieee80211_hw *dev,
440 u8 *data, int len, u16 type)
Christian Lamparter4c8a32f2009-06-23 10:36:26 -0500441{
442 struct p54_common *priv = dev->priv;
Christian Lamparter7a047f42011-02-12 22:32:49 +0100443 struct p54_rssi_db_entry *entry;
444 size_t db_len, entries;
445 int offset = 0, i;
Christian Lamparter4c8a32f2009-06-23 10:36:26 -0500446
Christian Lamparter7a047f42011-02-12 22:32:49 +0100447 if (type != PDR_RSSI_LINEAR_APPROXIMATION_EXTENDED) {
448 entries = (type == PDR_RSSI_LINEAR_APPROXIMATION) ? 1 : 2;
449 if (len != sizeof(struct pda_rssi_cal_entry) * entries) {
450 wiphy_err(dev->wiphy, "rssical size mismatch.\n");
451 goto err_data;
452 }
453 } else {
454 /*
455 * Some devices (Dell 1450 USB, Xbow 5GHz card, etc...)
456 * have an empty two byte header.
457 */
458 if (*((__le16 *)&data[offset]) == cpu_to_le16(0))
459 offset += 2;
Christian Lamparter4c8a32f2009-06-23 10:36:26 -0500460
Christian Lamparter7a047f42011-02-12 22:32:49 +0100461 entries = (len - offset) /
462 sizeof(struct pda_rssi_cal_ext_entry);
Christian Lamparter4c8a32f2009-06-23 10:36:26 -0500463
Christian Lamparter7a047f42011-02-12 22:32:49 +0100464 if ((len - offset) % sizeof(struct pda_rssi_cal_ext_entry) ||
465 entries <= 0) {
466 wiphy_err(dev->wiphy, "invalid rssi database.\n");
467 goto err_data;
468 }
Christian Lamparter4c8a32f2009-06-23 10:36:26 -0500469 }
470
Christian Lamparter7a047f42011-02-12 22:32:49 +0100471 db_len = sizeof(*entry) * entries;
472 priv->rssi_db = kzalloc(db_len + sizeof(*priv->rssi_db), GFP_KERNEL);
473 if (!priv->rssi_db)
474 return -ENOMEM;
475
476 priv->rssi_db->offset = 0;
477 priv->rssi_db->entries = entries;
478 priv->rssi_db->entry_size = sizeof(*entry);
479 priv->rssi_db->len = db_len;
480
481 entry = (void *)((unsigned long)priv->rssi_db->data + priv->rssi_db->offset);
482 if (type == PDR_RSSI_LINEAR_APPROXIMATION_EXTENDED) {
483 struct pda_rssi_cal_ext_entry *cal = (void *) &data[offset];
484
485 for (i = 0; i < entries; i++) {
486 entry[i].freq = le16_to_cpu(cal[i].freq);
487 entry[i].mul = (s16) le16_to_cpu(cal[i].mul);
488 entry[i].add = (s16) le16_to_cpu(cal[i].add);
489 }
490 } else {
491 struct pda_rssi_cal_entry *cal = (void *) &data[offset];
492
493 for (i = 0; i < entries; i++) {
494 u16 freq;
495 switch (i) {
496 case IEEE80211_BAND_2GHZ:
497 freq = 2437;
498 break;
499 case IEEE80211_BAND_5GHZ:
500 freq = 5240;
501 break;
502 }
503
504 entry[i].freq = freq;
505 entry[i].mul = (s16) le16_to_cpu(cal[i].mul);
506 entry[i].add = (s16) le16_to_cpu(cal[i].add);
507 }
Christian Lamparter4c8a32f2009-06-23 10:36:26 -0500508 }
Christian Lamparter7a047f42011-02-12 22:32:49 +0100509
510 /* sort the list by channel frequency */
511 sort(entry, entries, sizeof(*entry), p54_compare_rssichan, NULL);
512 return 0;
513
514err_data:
515 wiphy_err(dev->wiphy,
516 "rssi calibration data packing type:(%x) len:%d.\n",
517 type, len);
518
519 print_hex_dump_bytes("rssical:", DUMP_PREFIX_NONE, data, len);
520
521 wiphy_err(dev->wiphy, "please report this issue.\n");
522 return -EINVAL;
523}
524
525struct p54_rssi_db_entry *p54_rssi_find(struct p54_common *priv, const u16 freq)
526{
527 struct p54_rssi_db_entry *entry = (void *)(priv->rssi_db->data +
528 priv->rssi_db->offset);
529 int i, found = -1;
530
531 for (i = 0; i < priv->rssi_db->entries; i++) {
532 if (!same_band(freq, entry[i].freq))
533 continue;
534
535 if (found == -1) {
536 found = i;
537 continue;
538 }
539
540 /* nearest match */
541 if (abs(freq - entry[i].freq) <
542 abs(freq - entry[found].freq)) {
543 found = i;
544 continue;
545 } else {
546 break;
547 }
548 }
549
550 return found < 0 ? &p54_rssi_default : &entry[found];
Christian Lamparter4c8a32f2009-06-23 10:36:26 -0500551}
552
553static void p54_parse_default_country(struct ieee80211_hw *dev,
554 void *data, int len)
555{
556 struct pda_country *country;
557
558 if (len != sizeof(*country)) {
Joe Perchesc96c31e2010-07-26 14:39:58 -0700559 wiphy_err(dev->wiphy,
560 "found possible invalid default country eeprom entry. (entry size: %d)\n",
561 len);
Christian Lamparter4c8a32f2009-06-23 10:36:26 -0500562
563 print_hex_dump_bytes("country:", DUMP_PREFIX_NONE,
564 data, len);
565
Joe Perchesc96c31e2010-07-26 14:39:58 -0700566 wiphy_err(dev->wiphy, "please report this issue.\n");
Christian Lamparter4c8a32f2009-06-23 10:36:26 -0500567 return;
568 }
569
570 country = (struct pda_country *) data;
571 if (country->flags == PDR_COUNTRY_CERT_CODE_PSEUDO)
572 regulatory_hint(dev->wiphy, country->alpha2);
573 else {
574 /* TODO:
575 * write a shared/common function that converts
576 * "Regulatory domain codes" (802.11-2007 14.8.2.2)
577 * into ISO/IEC 3166-1 alpha2 for regulatory_hint.
578 */
579 }
580}
581
582static int p54_convert_output_limits(struct ieee80211_hw *dev,
583 u8 *data, size_t len)
584{
585 struct p54_common *priv = dev->priv;
586
587 if (len < 2)
588 return -EINVAL;
589
590 if (data[0] != 0) {
Joe Perchesc96c31e2010-07-26 14:39:58 -0700591 wiphy_err(dev->wiphy, "unknown output power db revision:%x\n",
592 data[0]);
Christian Lamparter4c8a32f2009-06-23 10:36:26 -0500593 return -EINVAL;
594 }
595
596 if (2 + data[1] * sizeof(struct pda_channel_output_limit) > len)
597 return -EINVAL;
598
599 priv->output_limit = kmalloc(data[1] *
600 sizeof(struct pda_channel_output_limit) +
601 sizeof(*priv->output_limit), GFP_KERNEL);
602
603 if (!priv->output_limit)
604 return -ENOMEM;
605
606 priv->output_limit->offset = 0;
607 priv->output_limit->entries = data[1];
608 priv->output_limit->entry_size =
609 sizeof(struct pda_channel_output_limit);
610 priv->output_limit->len = priv->output_limit->entry_size *
611 priv->output_limit->entries +
612 priv->output_limit->offset;
613
614 memcpy(priv->output_limit->data, &data[2],
615 data[1] * sizeof(struct pda_channel_output_limit));
616
617 return 0;
618}
619
620static struct p54_cal_database *p54_convert_db(struct pda_custom_wrapper *src,
621 size_t total_len)
622{
623 struct p54_cal_database *dst;
624 size_t payload_len, entries, entry_size, offset;
625
626 payload_len = le16_to_cpu(src->len);
627 entries = le16_to_cpu(src->entries);
628 entry_size = le16_to_cpu(src->entry_size);
629 offset = le16_to_cpu(src->offset);
630 if (((entries * entry_size + offset) != payload_len) ||
631 (payload_len + sizeof(*src) != total_len))
632 return NULL;
633
634 dst = kmalloc(sizeof(*dst) + payload_len, GFP_KERNEL);
635 if (!dst)
636 return NULL;
637
638 dst->entries = entries;
639 dst->entry_size = entry_size;
640 dst->offset = offset;
641 dst->len = payload_len;
642
643 memcpy(dst->data, src->data, payload_len);
644 return dst;
645}
646
647int p54_parse_eeprom(struct ieee80211_hw *dev, void *eeprom, int len)
648{
649 struct p54_common *priv = dev->priv;
Larry Fingere6a3f552009-07-19 21:53:14 -0500650 struct eeprom_pda_wrap *wrap;
Christian Lamparter4c8a32f2009-06-23 10:36:26 -0500651 struct pda_entry *entry;
652 unsigned int data_len, entry_len;
653 void *tmp;
654 int err;
655 u8 *end = (u8 *)eeprom + len;
656 u16 synth = 0;
Christian Lamparterd7eb50c2010-08-17 01:16:58 +0200657 u16 crc16 = ~0;
Christian Lamparter4c8a32f2009-06-23 10:36:26 -0500658
659 wrap = (struct eeprom_pda_wrap *) eeprom;
660 entry = (void *)wrap->data + le16_to_cpu(wrap->len);
661
662 /* verify that at least the entry length/code fits */
663 while ((u8 *)entry <= end - sizeof(*entry)) {
664 entry_len = le16_to_cpu(entry->len);
665 data_len = ((entry_len - 1) << 1);
666
667 /* abort if entry exceeds whole structure */
668 if ((u8 *)entry + sizeof(*entry) + data_len > end)
669 break;
670
671 switch (le16_to_cpu(entry->code)) {
672 case PDR_MAC_ADDRESS:
673 if (data_len != ETH_ALEN)
674 break;
675 SET_IEEE80211_PERM_ADDR(dev, entry->data);
676 break;
677 case PDR_PRISM_PA_CAL_OUTPUT_POWER_LIMITS:
678 if (priv->output_limit)
679 break;
680 err = p54_convert_output_limits(dev, entry->data,
681 data_len);
682 if (err)
683 goto err;
684 break;
685 case PDR_PRISM_PA_CAL_CURVE_DATA: {
686 struct pda_pa_curve_data *curve_data =
687 (struct pda_pa_curve_data *)entry->data;
688 if (data_len < sizeof(*curve_data)) {
689 err = -EINVAL;
690 goto err;
691 }
692
693 switch (curve_data->cal_method_rev) {
694 case 0:
695 err = p54_convert_rev0(dev, curve_data);
696 break;
697 case 1:
698 err = p54_convert_rev1(dev, curve_data);
699 break;
700 default:
Joe Perchesc96c31e2010-07-26 14:39:58 -0700701 wiphy_err(dev->wiphy,
702 "unknown curve data revision %d\n",
703 curve_data->cal_method_rev);
Christian Lamparter4c8a32f2009-06-23 10:36:26 -0500704 err = -ENODEV;
705 break;
706 }
707 if (err)
708 goto err;
709 }
710 break;
711 case PDR_PRISM_ZIF_TX_IQ_CALIBRATION:
Julia Lawall27b81bb2010-05-15 23:22:55 +0200712 priv->iq_autocal = kmemdup(entry->data, data_len,
713 GFP_KERNEL);
Christian Lamparter4c8a32f2009-06-23 10:36:26 -0500714 if (!priv->iq_autocal) {
715 err = -ENOMEM;
716 goto err;
717 }
718
Christian Lamparter4c8a32f2009-06-23 10:36:26 -0500719 priv->iq_autocal_len = data_len / sizeof(struct pda_iq_autocal_entry);
720 break;
721 case PDR_DEFAULT_COUNTRY:
722 p54_parse_default_country(dev, entry->data, data_len);
723 break;
724 case PDR_INTERFACE_LIST:
725 tmp = entry->data;
726 while ((u8 *)tmp < entry->data + data_len) {
727 struct exp_if *exp_if = tmp;
728 if (exp_if->if_id == cpu_to_le16(IF_ID_ISL39000))
729 synth = le16_to_cpu(exp_if->variant);
730 tmp += sizeof(*exp_if);
731 }
732 break;
733 case PDR_HARDWARE_PLATFORM_COMPONENT_ID:
734 if (data_len < 2)
735 break;
736 priv->version = *(u8 *)(entry->data + 1);
737 break;
738 case PDR_RSSI_LINEAR_APPROXIMATION:
739 case PDR_RSSI_LINEAR_APPROXIMATION_DUAL_BAND:
740 case PDR_RSSI_LINEAR_APPROXIMATION_EXTENDED:
Christian Lamparter7a047f42011-02-12 22:32:49 +0100741 err = p54_parse_rssical(dev, entry->data, data_len,
742 le16_to_cpu(entry->code));
743 if (err)
744 goto err;
Christian Lamparter4c8a32f2009-06-23 10:36:26 -0500745 break;
Christian Lamparter7a047f42011-02-12 22:32:49 +0100746 case PDR_RSSI_LINEAR_APPROXIMATION_CUSTOMV2: {
747 struct pda_custom_wrapper *pda = (void *) entry->data;
748 __le16 *src;
749 u16 *dst;
Christian Lamparter4c8a32f2009-06-23 10:36:26 -0500750 int i;
751
Christian Lamparter7a047f42011-02-12 22:32:49 +0100752 if (priv->rssi_db || data_len < sizeof(*pda))
753 break;
754
755 priv->rssi_db = p54_convert_db(pda, data_len);
756 if (!priv->rssi_db)
757 break;
758
759 src = (void *) priv->rssi_db->data;
760 dst = (void *) priv->rssi_db->data;
761
762 for (i = 0; i < priv->rssi_db->entries; i++)
Christian Lamparter4c8a32f2009-06-23 10:36:26 -0500763 *(dst++) = (s16) le16_to_cpu(*(src++));
Christian Lamparter7a047f42011-02-12 22:32:49 +0100764
Christian Lamparter4c8a32f2009-06-23 10:36:26 -0500765 }
766 break;
767 case PDR_PRISM_PA_CAL_OUTPUT_POWER_LIMITS_CUSTOM: {
768 struct pda_custom_wrapper *pda = (void *) entry->data;
769 if (priv->output_limit || data_len < sizeof(*pda))
770 break;
771 priv->output_limit = p54_convert_db(pda, data_len);
772 }
773 break;
774 case PDR_PRISM_PA_CAL_CURVE_DATA_CUSTOM: {
775 struct pda_custom_wrapper *pda = (void *) entry->data;
776 if (priv->curve_data || data_len < sizeof(*pda))
777 break;
778 priv->curve_data = p54_convert_db(pda, data_len);
779 }
780 break;
781 case PDR_END:
Christian Lamparterd7eb50c2010-08-17 01:16:58 +0200782 crc16 = ~crc_ccitt(crc16, (u8 *) entry, sizeof(*entry));
783 if (crc16 != le16_to_cpup((__le16 *)entry->data)) {
784 wiphy_err(dev->wiphy, "eeprom failed checksum "
785 "test!\n");
786 err = -ENOMSG;
787 goto err;
788 } else {
789 goto good_eeprom;
790 }
Christian Lamparter4c8a32f2009-06-23 10:36:26 -0500791 break;
792 default:
793 break;
794 }
795
Christian Lamparterd7eb50c2010-08-17 01:16:58 +0200796 crc16 = crc_ccitt(crc16, (u8 *)entry, (entry_len + 1) * 2);
797 entry = (void *)entry + (entry_len + 1) * 2;
Christian Lamparter4c8a32f2009-06-23 10:36:26 -0500798 }
799
Christian Lamparterd7eb50c2010-08-17 01:16:58 +0200800 wiphy_err(dev->wiphy, "unexpected end of eeprom data.\n");
801 err = -ENODATA;
802 goto err;
803
804good_eeprom:
Christian Lamparter4c8a32f2009-06-23 10:36:26 -0500805 if (!synth || !priv->iq_autocal || !priv->output_limit ||
806 !priv->curve_data) {
Joe Perchesc96c31e2010-07-26 14:39:58 -0700807 wiphy_err(dev->wiphy,
808 "not all required entries found in eeprom!\n");
Christian Lamparter4c8a32f2009-06-23 10:36:26 -0500809 err = -EINVAL;
810 goto err;
811 }
812
Christian Lamparter1a9b6672009-07-11 01:22:26 +0200813 err = p54_generate_channel_lists(dev);
814 if (err)
815 goto err;
816
Christian Lamparter4c8a32f2009-06-23 10:36:26 -0500817 priv->rxhw = synth & PDR_SYNTH_FRONTEND_MASK;
818 if (priv->rxhw == PDR_SYNTH_FRONTEND_XBOW)
819 p54_init_xbow_synth(priv);
820 if (!(synth & PDR_SYNTH_24_GHZ_DISABLED))
Christian Lamparter1a9b6672009-07-11 01:22:26 +0200821 dev->wiphy->bands[IEEE80211_BAND_2GHZ] =
822 priv->band_table[IEEE80211_BAND_2GHZ];
Christian Lamparter4c8a32f2009-06-23 10:36:26 -0500823 if (!(synth & PDR_SYNTH_5_GHZ_DISABLED))
Christian Lamparter1a9b6672009-07-11 01:22:26 +0200824 dev->wiphy->bands[IEEE80211_BAND_5GHZ] =
825 priv->band_table[IEEE80211_BAND_5GHZ];
Christian Lamparter4c8a32f2009-06-23 10:36:26 -0500826 if ((synth & PDR_SYNTH_RX_DIV_MASK) == PDR_SYNTH_RX_DIV_SUPPORTED)
827 priv->rx_diversity_mask = 3;
828 if ((synth & PDR_SYNTH_TX_DIV_MASK) == PDR_SYNTH_TX_DIV_SUPPORTED)
829 priv->tx_diversity_mask = 3;
830
831 if (!is_valid_ether_addr(dev->wiphy->perm_addr)) {
832 u8 perm_addr[ETH_ALEN];
833
Joe Perchesc96c31e2010-07-26 14:39:58 -0700834 wiphy_warn(dev->wiphy,
Joe Perches5db55842010-08-11 19:11:19 -0700835 "Invalid hwaddr! Using randomly generated MAC addr\n");
Christian Lamparter4c8a32f2009-06-23 10:36:26 -0500836 random_ether_addr(perm_addr);
837 SET_IEEE80211_PERM_ADDR(dev, perm_addr);
838 }
839
Christian Lamparter7a047f42011-02-12 22:32:49 +0100840 priv->cur_rssi = &p54_rssi_default;
841
Joe Perches5db55842010-08-11 19:11:19 -0700842 wiphy_info(dev->wiphy, "hwaddr %pM, MAC:isl38%02x RF:%s\n",
Joe Perchesc96c31e2010-07-26 14:39:58 -0700843 dev->wiphy->perm_addr, priv->version,
844 p54_rf_chips[priv->rxhw]);
Christian Lamparter4c8a32f2009-06-23 10:36:26 -0500845
846 return 0;
847
848err:
849 kfree(priv->iq_autocal);
850 kfree(priv->output_limit);
851 kfree(priv->curve_data);
Christian Lamparter7a047f42011-02-12 22:32:49 +0100852 kfree(priv->rssi_db);
Christian Lamparter4c8a32f2009-06-23 10:36:26 -0500853 priv->iq_autocal = NULL;
854 priv->output_limit = NULL;
855 priv->curve_data = NULL;
Christian Lamparter7a047f42011-02-12 22:32:49 +0100856 priv->rssi_db = NULL;
Christian Lamparter4c8a32f2009-06-23 10:36:26 -0500857
Joe Perchesc96c31e2010-07-26 14:39:58 -0700858 wiphy_err(dev->wiphy, "eeprom parse failed!\n");
Christian Lamparter4c8a32f2009-06-23 10:36:26 -0500859 return err;
860}
861EXPORT_SYMBOL_GPL(p54_parse_eeprom);
862
863int p54_read_eeprom(struct ieee80211_hw *dev)
864{
865 struct p54_common *priv = dev->priv;
866 size_t eeprom_size = 0x2020, offset = 0, blocksize, maxblocksize;
867 int ret = -ENOMEM;
Larry Fingere6a3f552009-07-19 21:53:14 -0500868 void *eeprom;
Christian Lamparter4c8a32f2009-06-23 10:36:26 -0500869
870 maxblocksize = EEPROM_READBACK_LEN;
871 if (priv->fw_var >= 0x509)
872 maxblocksize -= 0xc;
873 else
874 maxblocksize -= 0x4;
875
876 eeprom = kzalloc(eeprom_size, GFP_KERNEL);
877 if (unlikely(!eeprom))
878 goto free;
879
880 while (eeprom_size) {
881 blocksize = min(eeprom_size, maxblocksize);
882 ret = p54_download_eeprom(priv, (void *) (eeprom + offset),
883 offset, blocksize);
884 if (unlikely(ret))
885 goto free;
886
887 offset += blocksize;
888 eeprom_size -= blocksize;
889 }
890
891 ret = p54_parse_eeprom(dev, eeprom, offset);
892free:
893 kfree(eeprom);
894 return ret;
895}
896EXPORT_SYMBOL_GPL(p54_read_eeprom);