Merge branch 'x86-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[deliverable/linux.git] / drivers / net / wireless / p54 / eeprom.c
CommitLineData
4c8a32f5
CL
1/*
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>
1a9b6679 22#include <linux/sort.h>
5a0e3ad6 23#include <linux/slab.h>
4c8a32f5
CL
24
25#include <net/mac80211.h>
d7eb50c0 26#include <linux/crc-ccitt.h>
ee40fa06 27#include <linux/export.h>
4c8a32f5
CL
28
29#include "p54.h"
30#include "eeprom.h"
31#include "lmac.h"
32
33static struct ieee80211_rate p54_bgrates[] = {
34 { .bitrate = 10, .hw_value = 0, },
35 { .bitrate = 20, .hw_value = 1, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
36 { .bitrate = 55, .hw_value = 2, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
37 { .bitrate = 110, .hw_value = 3, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
38 { .bitrate = 60, .hw_value = 4, },
39 { .bitrate = 90, .hw_value = 5, },
40 { .bitrate = 120, .hw_value = 6, },
41 { .bitrate = 180, .hw_value = 7, },
42 { .bitrate = 240, .hw_value = 8, },
43 { .bitrate = 360, .hw_value = 9, },
44 { .bitrate = 480, .hw_value = 10, },
45 { .bitrate = 540, .hw_value = 11, },
46};
47
4c8a32f5
CL
48static struct ieee80211_rate p54_arates[] = {
49 { .bitrate = 60, .hw_value = 4, },
50 { .bitrate = 90, .hw_value = 5, },
51 { .bitrate = 120, .hw_value = 6, },
52 { .bitrate = 180, .hw_value = 7, },
53 { .bitrate = 240, .hw_value = 8, },
54 { .bitrate = 360, .hw_value = 9, },
55 { .bitrate = 480, .hw_value = 10, },
56 { .bitrate = 540, .hw_value = 11, },
57};
58
7a047f4f
CL
59static struct p54_rssi_db_entry p54_rssi_default = {
60 /*
61 * The defaults are taken from usb-logs of the
62 * vendor driver. So, they should be safe to
63 * use in case we can't get a match from the
64 * rssi <-> dBm conversion database.
65 */
66 .mul = 130,
67 .add = -398,
68};
69
1a9b6679
CL
70#define CHAN_HAS_CAL BIT(0)
71#define CHAN_HAS_LIMIT BIT(1)
72#define CHAN_HAS_CURVE BIT(2)
73#define CHAN_HAS_ALL (CHAN_HAS_CAL | CHAN_HAS_LIMIT | CHAN_HAS_CURVE)
74
75struct p54_channel_entry {
76 u16 freq;
77 u16 data;
78 int index;
9bc63816 79 int max_power;
1a9b6679 80 enum ieee80211_band band;
4c8a32f5
CL
81};
82
1a9b6679
CL
83struct p54_channel_list {
84 struct p54_channel_entry *channels;
85 size_t entries;
86 size_t max_entries;
87 size_t band_channel_num[IEEE80211_NUM_BANDS];
4c8a32f5
CL
88};
89
1a9b6679
CL
90static int p54_get_band_from_freq(u16 freq)
91{
92 /* FIXME: sync these values with the 802.11 spec */
93
94 if ((freq >= 2412) && (freq <= 2484))
95 return IEEE80211_BAND_2GHZ;
96
97 if ((freq >= 4920) && (freq <= 5825))
98 return IEEE80211_BAND_5GHZ;
99
100 return -1;
101}
102
7a047f4f
CL
103static int same_band(u16 freq, u16 freq2)
104{
105 return p54_get_band_from_freq(freq) == p54_get_band_from_freq(freq2);
106}
107
1a9b6679
CL
108static int p54_compare_channels(const void *_a,
109 const void *_b)
110{
111 const struct p54_channel_entry *a = _a;
112 const struct p54_channel_entry *b = _b;
113
192abece 114 return a->freq - b->freq;
1a9b6679
CL
115}
116
7a047f4f
CL
117static int p54_compare_rssichan(const void *_a,
118 const void *_b)
119{
120 const struct p54_rssi_db_entry *a = _a;
121 const struct p54_rssi_db_entry *b = _b;
122
123 return a->freq - b->freq;
124}
125
1a9b6679
CL
126static int p54_fill_band_bitrates(struct ieee80211_hw *dev,
127 struct ieee80211_supported_band *band_entry,
128 enum ieee80211_band band)
129{
130 /* TODO: generate rate array dynamically */
131
132 switch (band) {
133 case IEEE80211_BAND_2GHZ:
134 band_entry->bitrates = p54_bgrates;
135 band_entry->n_bitrates = ARRAY_SIZE(p54_bgrates);
136 break;
137 case IEEE80211_BAND_5GHZ:
138 band_entry->bitrates = p54_arates;
139 band_entry->n_bitrates = ARRAY_SIZE(p54_arates);
140 break;
141 default:
142 return -EINVAL;
143 }
144
145 return 0;
146}
147
148static int p54_generate_band(struct ieee80211_hw *dev,
149 struct p54_channel_list *list,
0d78156e 150 unsigned int *chan_num,
1a9b6679
CL
151 enum ieee80211_band band)
152{
153 struct p54_common *priv = dev->priv;
154 struct ieee80211_supported_band *tmp, *old;
155 unsigned int i, j;
156 int ret = -ENOMEM;
157
158 if ((!list->entries) || (!list->band_channel_num[band]))
93a59d75 159 return -EINVAL;
1a9b6679
CL
160
161 tmp = kzalloc(sizeof(*tmp), GFP_KERNEL);
162 if (!tmp)
163 goto err_out;
164
165 tmp->channels = kzalloc(sizeof(struct ieee80211_channel) *
166 list->band_channel_num[band], GFP_KERNEL);
167 if (!tmp->channels)
168 goto err_out;
169
170 ret = p54_fill_band_bitrates(dev, tmp, band);
171 if (ret)
172 goto err_out;
173
174 for (i = 0, j = 0; (j < list->band_channel_num[band]) &&
175 (i < list->entries); i++) {
a3162eed 176 struct p54_channel_entry *chan = &list->channels[i];
9bc63816 177 struct ieee80211_channel *dest = &tmp->channels[j];
1a9b6679 178
a3162eed 179 if (chan->band != band)
1a9b6679
CL
180 continue;
181
a3162eed
CL
182 if (chan->data != CHAN_HAS_ALL) {
183 wiphy_err(dev->wiphy, "%s%s%s is/are missing for "
184 "channel:%d [%d MHz].\n",
185 (chan->data & CHAN_HAS_CAL ? "" :
c96c31e4 186 " [iqauto calibration data]"),
a3162eed 187 (chan->data & CHAN_HAS_LIMIT ? "" :
c96c31e4 188 " [output power limits]"),
a3162eed 189 (chan->data & CHAN_HAS_CURVE ? "" :
c96c31e4 190 " [curve data]"),
a3162eed 191 chan->index, chan->freq);
93a59d75 192 continue;
1a9b6679
CL
193 }
194
9bc63816
CL
195 dest->band = chan->band;
196 dest->center_freq = chan->freq;
197 dest->max_power = chan->max_power;
0d78156e
CL
198 priv->survey[*chan_num].channel = &tmp->channels[j];
199 priv->survey[*chan_num].filled = SURVEY_INFO_NOISE_DBM |
200 SURVEY_INFO_CHANNEL_TIME |
201 SURVEY_INFO_CHANNEL_TIME_BUSY |
202 SURVEY_INFO_CHANNEL_TIME_TX;
9bc63816 203 dest->hw_value = (*chan_num);
1a9b6679 204 j++;
0d78156e 205 (*chan_num)++;
1a9b6679
CL
206 }
207
93a59d75 208 if (j == 0) {
5db55844 209 wiphy_err(dev->wiphy, "Disabling totally damaged %d GHz band\n",
c96c31e4 210 (band == IEEE80211_BAND_2GHZ) ? 2 : 5);
93a59d75
CL
211
212 ret = -ENODATA;
213 goto err_out;
214 }
215
216 tmp->n_channels = j;
1a9b6679
CL
217 old = priv->band_table[band];
218 priv->band_table[band] = tmp;
219 if (old) {
220 kfree(old->channels);
221 kfree(old);
222 }
223
224 return 0;
225
226err_out:
227 if (tmp) {
228 kfree(tmp->channels);
229 kfree(tmp);
230 }
231
232 return ret;
233}
234
9bc63816
CL
235static struct p54_channel_entry *p54_update_channel_param(struct p54_channel_list *list,
236 u16 freq, u16 data)
1a9b6679 237{
9bc63816
CL
238 int i;
239 struct p54_channel_entry *entry = NULL;
1a9b6679
CL
240
241 /*
242 * usually all lists in the eeprom are mostly sorted.
243 * so it's very likely that the entry we are looking for
244 * is right at the end of the list
245 */
246 for (i = list->entries; i >= 0; i--) {
247 if (freq == list->channels[i].freq) {
9bc63816 248 entry = &list->channels[i];
1a9b6679
CL
249 break;
250 }
251 }
252
253 if ((i < 0) && (list->entries < list->max_entries)) {
254 /* entry does not exist yet. Initialize a new one. */
9bc63816 255 int band = p54_get_band_from_freq(freq);
1a9b6679
CL
256
257 /*
258 * filter out frequencies which don't belong into
259 * any supported band.
260 */
9bc63816
CL
261 if (band >= 0) {
262 i = list->entries++;
263 list->band_channel_num[band]++;
264
265 entry = &list->channels[i];
266 entry->freq = freq;
267 entry->band = band;
268 entry->index = ieee80211_frequency_to_channel(freq);
269 entry->max_power = 0;
270 entry->data = 0;
271 }
272 }
1a9b6679 273
9bc63816
CL
274 if (entry)
275 entry->data |= data;
1a9b6679 276
9bc63816
CL
277 return entry;
278}
279
280static int p54_get_maxpower(struct p54_common *priv, void *data)
281{
282 switch (priv->rxhw & PDR_SYNTH_FRONTEND_MASK) {
283 case PDR_SYNTH_FRONTEND_LONGBOW: {
284 struct pda_channel_output_limit_longbow *pda = data;
285 int j;
286 u16 rawpower = 0;
287 pda = data;
288 for (j = 0; j < ARRAY_SIZE(pda->point); j++) {
289 struct pda_channel_output_limit_point_longbow *point =
290 &pda->point[j];
22c5649e
CL
291 rawpower = max_t(u16,
292 rawpower, le16_to_cpu(point->val_qpsk));
293 rawpower = max_t(u16,
294 rawpower, le16_to_cpu(point->val_bpsk));
295 rawpower = max_t(u16,
296 rawpower, le16_to_cpu(point->val_16qam));
297 rawpower = max_t(u16,
298 rawpower, le16_to_cpu(point->val_64qam));
9bc63816
CL
299 }
300 /* longbow seems to use 1/16 dBm units */
301 return rawpower / 16;
302 }
303
304 case PDR_SYNTH_FRONTEND_DUETTE3:
305 case PDR_SYNTH_FRONTEND_DUETTE2:
306 case PDR_SYNTH_FRONTEND_FRISBEE:
307 case PDR_SYNTH_FRONTEND_XBOW: {
308 struct pda_channel_output_limit *pda = data;
309 u8 rawpower = 0;
310 rawpower = max(rawpower, pda->val_qpsk);
311 rawpower = max(rawpower, pda->val_bpsk);
312 rawpower = max(rawpower, pda->val_16qam);
313 rawpower = max(rawpower, pda->val_64qam);
314 /* raw values are in 1/4 dBm units */
315 return rawpower / 4;
316 }
317
318 default:
319 return 20;
1a9b6679
CL
320 }
321}
322
323static int p54_generate_channel_lists(struct ieee80211_hw *dev)
324{
325 struct p54_common *priv = dev->priv;
326 struct p54_channel_list *list;
0d78156e 327 unsigned int i, j, k, max_channel_num;
93a59d75 328 int ret = 0;
1a9b6679
CL
329 u16 freq;
330
331 if ((priv->iq_autocal_len != priv->curve_data->entries) ||
332 (priv->iq_autocal_len != priv->output_limit->entries))
c96c31e4
JP
333 wiphy_err(dev->wiphy,
334 "Unsupported or damaged EEPROM detected. "
335 "You may not be able to use all channels.\n");
1a9b6679
CL
336
337 max_channel_num = max_t(unsigned int, priv->output_limit->entries,
338 priv->iq_autocal_len);
339 max_channel_num = max_t(unsigned int, max_channel_num,
340 priv->curve_data->entries);
341
342 list = kzalloc(sizeof(*list), GFP_KERNEL);
93a59d75
CL
343 if (!list) {
344 ret = -ENOMEM;
1a9b6679 345 goto free;
93a59d75 346 }
0d78156e
CL
347 priv->chan_num = max_channel_num;
348 priv->survey = kzalloc(sizeof(struct survey_info) * max_channel_num,
349 GFP_KERNEL);
350 if (!priv->survey) {
351 ret = -ENOMEM;
352 goto free;
353 }
1a9b6679
CL
354
355 list->max_entries = max_channel_num;
356 list->channels = kzalloc(sizeof(struct p54_channel_entry) *
357 max_channel_num, GFP_KERNEL);
0d91f22b
JL
358 if (!list->channels) {
359 ret = -ENOMEM;
1a9b6679 360 goto free;
0d91f22b 361 }
1a9b6679
CL
362
363 for (i = 0; i < max_channel_num; i++) {
364 if (i < priv->iq_autocal_len) {
365 freq = le16_to_cpu(priv->iq_autocal[i].freq);
366 p54_update_channel_param(list, freq, CHAN_HAS_CAL);
367 }
368
369 if (i < priv->output_limit->entries) {
9bc63816
CL
370 struct p54_channel_entry *tmp;
371
372 void *data = (void *) ((unsigned long) i *
373 priv->output_limit->entry_size +
374 priv->output_limit->offset +
375 priv->output_limit->data);
376
377 freq = le16_to_cpup((__le16 *) data);
378 tmp = p54_update_channel_param(list, freq,
379 CHAN_HAS_LIMIT);
380 if (tmp) {
381 tmp->max_power = p54_get_maxpower(priv, data);
382 }
1a9b6679
CL
383 }
384
385 if (i < priv->curve_data->entries) {
386 freq = le16_to_cpup((__le16 *) (i *
387 priv->curve_data->entry_size +
388 priv->curve_data->offset +
389 priv->curve_data->data));
390
391 p54_update_channel_param(list, freq, CHAN_HAS_CURVE);
392 }
393 }
394
192abece 395 /* sort the channel list by frequency */
1a9b6679
CL
396 sort(list->channels, list->entries, sizeof(struct p54_channel_entry),
397 p54_compare_channels, NULL);
398
0d78156e 399 k = 0;
1a9b6679 400 for (i = 0, j = 0; i < IEEE80211_NUM_BANDS; i++) {
0d78156e 401 if (p54_generate_band(dev, list, &k, i) == 0)
1a9b6679 402 j++;
1a9b6679
CL
403 }
404 if (j == 0) {
405 /* no useable band available. */
406 ret = -EINVAL;
407 }
408
409free:
410 if (list) {
411 kfree(list->channels);
412 kfree(list);
413 }
0d78156e
CL
414 if (ret) {
415 kfree(priv->survey);
416 priv->survey = NULL;
417 }
1a9b6679
CL
418
419 return ret;
420}
421
4c8a32f5
CL
422static int p54_convert_rev0(struct ieee80211_hw *dev,
423 struct pda_pa_curve_data *curve_data)
424{
425 struct p54_common *priv = dev->priv;
426 struct p54_pa_curve_data_sample *dst;
427 struct pda_pa_curve_data_sample_rev0 *src;
428 size_t cd_len = sizeof(*curve_data) +
429 (curve_data->points_per_channel*sizeof(*dst) + 2) *
430 curve_data->channels;
431 unsigned int i, j;
432 void *source, *target;
433
434 priv->curve_data = kmalloc(sizeof(*priv->curve_data) + cd_len,
435 GFP_KERNEL);
436 if (!priv->curve_data)
437 return -ENOMEM;
438
439 priv->curve_data->entries = curve_data->channels;
440 priv->curve_data->entry_size = sizeof(__le16) +
441 sizeof(*dst) * curve_data->points_per_channel;
442 priv->curve_data->offset = offsetof(struct pda_pa_curve_data, data);
443 priv->curve_data->len = cd_len;
444 memcpy(priv->curve_data->data, curve_data, sizeof(*curve_data));
445 source = curve_data->data;
446 target = ((struct pda_pa_curve_data *) priv->curve_data->data)->data;
447 for (i = 0; i < curve_data->channels; i++) {
448 __le16 *freq = source;
449 source += sizeof(__le16);
450 *((__le16 *)target) = *freq;
451 target += sizeof(__le16);
452 for (j = 0; j < curve_data->points_per_channel; j++) {
453 dst = target;
454 src = source;
455
456 dst->rf_power = src->rf_power;
457 dst->pa_detector = src->pa_detector;
458 dst->data_64qam = src->pcv;
459 /* "invent" the points for the other modulations */
460#define SUB(x, y) (u8)(((x) - (y)) > (x) ? 0 : (x) - (y))
461 dst->data_16qam = SUB(src->pcv, 12);
462 dst->data_qpsk = SUB(dst->data_16qam, 12);
463 dst->data_bpsk = SUB(dst->data_qpsk, 12);
464 dst->data_barker = SUB(dst->data_bpsk, 14);
465#undef SUB
466 target += sizeof(*dst);
467 source += sizeof(*src);
468 }
469 }
470
471 return 0;
472}
473
474static int p54_convert_rev1(struct ieee80211_hw *dev,
475 struct pda_pa_curve_data *curve_data)
476{
477 struct p54_common *priv = dev->priv;
478 struct p54_pa_curve_data_sample *dst;
479 struct pda_pa_curve_data_sample_rev1 *src;
480 size_t cd_len = sizeof(*curve_data) +
481 (curve_data->points_per_channel*sizeof(*dst) + 2) *
482 curve_data->channels;
483 unsigned int i, j;
484 void *source, *target;
485
486 priv->curve_data = kzalloc(cd_len + sizeof(*priv->curve_data),
487 GFP_KERNEL);
488 if (!priv->curve_data)
489 return -ENOMEM;
490
491 priv->curve_data->entries = curve_data->channels;
492 priv->curve_data->entry_size = sizeof(__le16) +
493 sizeof(*dst) * curve_data->points_per_channel;
494 priv->curve_data->offset = offsetof(struct pda_pa_curve_data, data);
495 priv->curve_data->len = cd_len;
496 memcpy(priv->curve_data->data, curve_data, sizeof(*curve_data));
497 source = curve_data->data;
498 target = ((struct pda_pa_curve_data *) priv->curve_data->data)->data;
499 for (i = 0; i < curve_data->channels; i++) {
500 __le16 *freq = source;
501 source += sizeof(__le16);
502 *((__le16 *)target) = *freq;
503 target += sizeof(__le16);
504 for (j = 0; j < curve_data->points_per_channel; j++) {
505 memcpy(target, source, sizeof(*src));
506
507 target += sizeof(*dst);
508 source += sizeof(*src);
509 }
510 source++;
511 }
512
513 return 0;
514}
515
516static const char *p54_rf_chips[] = { "INVALID-0", "Duette3", "Duette2",
517 "Frisbee", "Xbow", "Longbow", "INVALID-6", "INVALID-7" };
518
7a047f4f
CL
519static int p54_parse_rssical(struct ieee80211_hw *dev,
520 u8 *data, int len, u16 type)
4c8a32f5
CL
521{
522 struct p54_common *priv = dev->priv;
7a047f4f
CL
523 struct p54_rssi_db_entry *entry;
524 size_t db_len, entries;
525 int offset = 0, i;
526
527 if (type != PDR_RSSI_LINEAR_APPROXIMATION_EXTENDED) {
528 entries = (type == PDR_RSSI_LINEAR_APPROXIMATION) ? 1 : 2;
529 if (len != sizeof(struct pda_rssi_cal_entry) * entries) {
530 wiphy_err(dev->wiphy, "rssical size mismatch.\n");
531 goto err_data;
532 }
533 } else {
534 /*
535 * Some devices (Dell 1450 USB, Xbow 5GHz card, etc...)
536 * have an empty two byte header.
537 */
538 if (*((__le16 *)&data[offset]) == cpu_to_le16(0))
539 offset += 2;
4c8a32f5 540
7a047f4f
CL
541 entries = (len - offset) /
542 sizeof(struct pda_rssi_cal_ext_entry);
4c8a32f5 543
6dac3447
DC
544 if (len < offset ||
545 (len - offset) % sizeof(struct pda_rssi_cal_ext_entry) ||
546 entries == 0) {
7a047f4f
CL
547 wiphy_err(dev->wiphy, "invalid rssi database.\n");
548 goto err_data;
549 }
550 }
4c8a32f5 551
7a047f4f
CL
552 db_len = sizeof(*entry) * entries;
553 priv->rssi_db = kzalloc(db_len + sizeof(*priv->rssi_db), GFP_KERNEL);
554 if (!priv->rssi_db)
555 return -ENOMEM;
556
557 priv->rssi_db->offset = 0;
558 priv->rssi_db->entries = entries;
559 priv->rssi_db->entry_size = sizeof(*entry);
560 priv->rssi_db->len = db_len;
561
562 entry = (void *)((unsigned long)priv->rssi_db->data + priv->rssi_db->offset);
563 if (type == PDR_RSSI_LINEAR_APPROXIMATION_EXTENDED) {
564 struct pda_rssi_cal_ext_entry *cal = (void *) &data[offset];
565
566 for (i = 0; i < entries; i++) {
567 entry[i].freq = le16_to_cpu(cal[i].freq);
568 entry[i].mul = (s16) le16_to_cpu(cal[i].mul);
569 entry[i].add = (s16) le16_to_cpu(cal[i].add);
570 }
571 } else {
572 struct pda_rssi_cal_entry *cal = (void *) &data[offset];
573
574 for (i = 0; i < entries; i++) {
ce6cac88 575 u16 freq = 0;
7a047f4f
CL
576 switch (i) {
577 case IEEE80211_BAND_2GHZ:
578 freq = 2437;
579 break;
580 case IEEE80211_BAND_5GHZ:
581 freq = 5240;
582 break;
583 }
584
585 entry[i].freq = freq;
586 entry[i].mul = (s16) le16_to_cpu(cal[i].mul);
587 entry[i].add = (s16) le16_to_cpu(cal[i].add);
588 }
4c8a32f5
CL
589 }
590
7a047f4f
CL
591 /* sort the list by channel frequency */
592 sort(entry, entries, sizeof(*entry), p54_compare_rssichan, NULL);
593 return 0;
594
595err_data:
596 wiphy_err(dev->wiphy,
597 "rssi calibration data packing type:(%x) len:%d.\n",
598 type, len);
599
600 print_hex_dump_bytes("rssical:", DUMP_PREFIX_NONE, data, len);
601
602 wiphy_err(dev->wiphy, "please report this issue.\n");
603 return -EINVAL;
604}
605
606struct p54_rssi_db_entry *p54_rssi_find(struct p54_common *priv, const u16 freq)
607{
a5a7103f 608 struct p54_rssi_db_entry *entry;
7a047f4f
CL
609 int i, found = -1;
610
a5a7103f
FF
611 if (!priv->rssi_db)
612 return &p54_rssi_default;
613
614 entry = (void *)(priv->rssi_db->data + priv->rssi_db->offset);
7a047f4f
CL
615 for (i = 0; i < priv->rssi_db->entries; i++) {
616 if (!same_band(freq, entry[i].freq))
617 continue;
618
619 if (found == -1) {
620 found = i;
621 continue;
622 }
623
624 /* nearest match */
625 if (abs(freq - entry[i].freq) <
626 abs(freq - entry[found].freq)) {
627 found = i;
628 continue;
629 } else {
630 break;
631 }
4c8a32f5 632 }
7a047f4f
CL
633
634 return found < 0 ? &p54_rssi_default : &entry[found];
4c8a32f5
CL
635}
636
637static void p54_parse_default_country(struct ieee80211_hw *dev,
638 void *data, int len)
639{
640 struct pda_country *country;
641
642 if (len != sizeof(*country)) {
c96c31e4
JP
643 wiphy_err(dev->wiphy,
644 "found possible invalid default country eeprom entry. (entry size: %d)\n",
645 len);
4c8a32f5
CL
646
647 print_hex_dump_bytes("country:", DUMP_PREFIX_NONE,
648 data, len);
649
c96c31e4 650 wiphy_err(dev->wiphy, "please report this issue.\n");
4c8a32f5
CL
651 return;
652 }
653
654 country = (struct pda_country *) data;
655 if (country->flags == PDR_COUNTRY_CERT_CODE_PSEUDO)
656 regulatory_hint(dev->wiphy, country->alpha2);
657 else {
658 /* TODO:
659 * write a shared/common function that converts
660 * "Regulatory domain codes" (802.11-2007 14.8.2.2)
661 * into ISO/IEC 3166-1 alpha2 for regulatory_hint.
662 */
663 }
664}
665
666static int p54_convert_output_limits(struct ieee80211_hw *dev,
667 u8 *data, size_t len)
668{
669 struct p54_common *priv = dev->priv;
670
671 if (len < 2)
672 return -EINVAL;
673
674 if (data[0] != 0) {
c96c31e4
JP
675 wiphy_err(dev->wiphy, "unknown output power db revision:%x\n",
676 data[0]);
4c8a32f5
CL
677 return -EINVAL;
678 }
679
680 if (2 + data[1] * sizeof(struct pda_channel_output_limit) > len)
681 return -EINVAL;
682
683 priv->output_limit = kmalloc(data[1] *
684 sizeof(struct pda_channel_output_limit) +
685 sizeof(*priv->output_limit), GFP_KERNEL);
686
687 if (!priv->output_limit)
688 return -ENOMEM;
689
690 priv->output_limit->offset = 0;
691 priv->output_limit->entries = data[1];
692 priv->output_limit->entry_size =
693 sizeof(struct pda_channel_output_limit);
694 priv->output_limit->len = priv->output_limit->entry_size *
695 priv->output_limit->entries +
696 priv->output_limit->offset;
697
698 memcpy(priv->output_limit->data, &data[2],
699 data[1] * sizeof(struct pda_channel_output_limit));
700
701 return 0;
702}
703
704static struct p54_cal_database *p54_convert_db(struct pda_custom_wrapper *src,
705 size_t total_len)
706{
707 struct p54_cal_database *dst;
708 size_t payload_len, entries, entry_size, offset;
709
710 payload_len = le16_to_cpu(src->len);
711 entries = le16_to_cpu(src->entries);
712 entry_size = le16_to_cpu(src->entry_size);
713 offset = le16_to_cpu(src->offset);
714 if (((entries * entry_size + offset) != payload_len) ||
715 (payload_len + sizeof(*src) != total_len))
716 return NULL;
717
718 dst = kmalloc(sizeof(*dst) + payload_len, GFP_KERNEL);
719 if (!dst)
720 return NULL;
721
722 dst->entries = entries;
723 dst->entry_size = entry_size;
724 dst->offset = offset;
725 dst->len = payload_len;
726
727 memcpy(dst->data, src->data, payload_len);
728 return dst;
729}
730
731int p54_parse_eeprom(struct ieee80211_hw *dev, void *eeprom, int len)
732{
733 struct p54_common *priv = dev->priv;
e6a3f551 734 struct eeprom_pda_wrap *wrap;
4c8a32f5
CL
735 struct pda_entry *entry;
736 unsigned int data_len, entry_len;
737 void *tmp;
738 int err;
739 u8 *end = (u8 *)eeprom + len;
740 u16 synth = 0;
d7eb50c0 741 u16 crc16 = ~0;
4c8a32f5
CL
742
743 wrap = (struct eeprom_pda_wrap *) eeprom;
744 entry = (void *)wrap->data + le16_to_cpu(wrap->len);
745
746 /* verify that at least the entry length/code fits */
747 while ((u8 *)entry <= end - sizeof(*entry)) {
748 entry_len = le16_to_cpu(entry->len);
749 data_len = ((entry_len - 1) << 1);
750
751 /* abort if entry exceeds whole structure */
752 if ((u8 *)entry + sizeof(*entry) + data_len > end)
753 break;
754
755 switch (le16_to_cpu(entry->code)) {
756 case PDR_MAC_ADDRESS:
757 if (data_len != ETH_ALEN)
758 break;
759 SET_IEEE80211_PERM_ADDR(dev, entry->data);
760 break;
761 case PDR_PRISM_PA_CAL_OUTPUT_POWER_LIMITS:
762 if (priv->output_limit)
763 break;
764 err = p54_convert_output_limits(dev, entry->data,
765 data_len);
766 if (err)
767 goto err;
768 break;
769 case PDR_PRISM_PA_CAL_CURVE_DATA: {
770 struct pda_pa_curve_data *curve_data =
771 (struct pda_pa_curve_data *)entry->data;
772 if (data_len < sizeof(*curve_data)) {
773 err = -EINVAL;
774 goto err;
775 }
776
777 switch (curve_data->cal_method_rev) {
778 case 0:
779 err = p54_convert_rev0(dev, curve_data);
780 break;
781 case 1:
782 err = p54_convert_rev1(dev, curve_data);
783 break;
784 default:
c96c31e4
JP
785 wiphy_err(dev->wiphy,
786 "unknown curve data revision %d\n",
787 curve_data->cal_method_rev);
4c8a32f5
CL
788 err = -ENODEV;
789 break;
790 }
791 if (err)
792 goto err;
793 }
794 break;
795 case PDR_PRISM_ZIF_TX_IQ_CALIBRATION:
27b81bbe
JL
796 priv->iq_autocal = kmemdup(entry->data, data_len,
797 GFP_KERNEL);
4c8a32f5
CL
798 if (!priv->iq_autocal) {
799 err = -ENOMEM;
800 goto err;
801 }
802
4c8a32f5
CL
803 priv->iq_autocal_len = data_len / sizeof(struct pda_iq_autocal_entry);
804 break;
805 case PDR_DEFAULT_COUNTRY:
806 p54_parse_default_country(dev, entry->data, data_len);
807 break;
808 case PDR_INTERFACE_LIST:
809 tmp = entry->data;
810 while ((u8 *)tmp < entry->data + data_len) {
811 struct exp_if *exp_if = tmp;
812 if (exp_if->if_id == cpu_to_le16(IF_ID_ISL39000))
813 synth = le16_to_cpu(exp_if->variant);
814 tmp += sizeof(*exp_if);
815 }
816 break;
817 case PDR_HARDWARE_PLATFORM_COMPONENT_ID:
818 if (data_len < 2)
819 break;
820 priv->version = *(u8 *)(entry->data + 1);
821 break;
822 case PDR_RSSI_LINEAR_APPROXIMATION:
823 case PDR_RSSI_LINEAR_APPROXIMATION_DUAL_BAND:
824 case PDR_RSSI_LINEAR_APPROXIMATION_EXTENDED:
7a047f4f
CL
825 err = p54_parse_rssical(dev, entry->data, data_len,
826 le16_to_cpu(entry->code));
827 if (err)
828 goto err;
4c8a32f5 829 break;
7a047f4f
CL
830 case PDR_RSSI_LINEAR_APPROXIMATION_CUSTOMV2: {
831 struct pda_custom_wrapper *pda = (void *) entry->data;
832 __le16 *src;
833 u16 *dst;
4c8a32f5
CL
834 int i;
835
7a047f4f
CL
836 if (priv->rssi_db || data_len < sizeof(*pda))
837 break;
838
839 priv->rssi_db = p54_convert_db(pda, data_len);
840 if (!priv->rssi_db)
841 break;
842
843 src = (void *) priv->rssi_db->data;
844 dst = (void *) priv->rssi_db->data;
845
846 for (i = 0; i < priv->rssi_db->entries; i++)
4c8a32f5 847 *(dst++) = (s16) le16_to_cpu(*(src++));
7a047f4f 848
4c8a32f5
CL
849 }
850 break;
851 case PDR_PRISM_PA_CAL_OUTPUT_POWER_LIMITS_CUSTOM: {
852 struct pda_custom_wrapper *pda = (void *) entry->data;
853 if (priv->output_limit || data_len < sizeof(*pda))
854 break;
855 priv->output_limit = p54_convert_db(pda, data_len);
856 }
857 break;
858 case PDR_PRISM_PA_CAL_CURVE_DATA_CUSTOM: {
859 struct pda_custom_wrapper *pda = (void *) entry->data;
860 if (priv->curve_data || data_len < sizeof(*pda))
861 break;
862 priv->curve_data = p54_convert_db(pda, data_len);
863 }
864 break;
865 case PDR_END:
d7eb50c0
CL
866 crc16 = ~crc_ccitt(crc16, (u8 *) entry, sizeof(*entry));
867 if (crc16 != le16_to_cpup((__le16 *)entry->data)) {
868 wiphy_err(dev->wiphy, "eeprom failed checksum "
869 "test!\n");
870 err = -ENOMSG;
871 goto err;
872 } else {
873 goto good_eeprom;
874 }
4c8a32f5
CL
875 break;
876 default:
877 break;
878 }
879
d7eb50c0
CL
880 crc16 = crc_ccitt(crc16, (u8 *)entry, (entry_len + 1) * 2);
881 entry = (void *)entry + (entry_len + 1) * 2;
4c8a32f5
CL
882 }
883
d7eb50c0
CL
884 wiphy_err(dev->wiphy, "unexpected end of eeprom data.\n");
885 err = -ENODATA;
886 goto err;
887
888good_eeprom:
4c8a32f5
CL
889 if (!synth || !priv->iq_autocal || !priv->output_limit ||
890 !priv->curve_data) {
c96c31e4
JP
891 wiphy_err(dev->wiphy,
892 "not all required entries found in eeprom!\n");
4c8a32f5
CL
893 err = -EINVAL;
894 goto err;
895 }
896
9bc63816
CL
897 priv->rxhw = synth & PDR_SYNTH_FRONTEND_MASK;
898
1a9b6679
CL
899 err = p54_generate_channel_lists(dev);
900 if (err)
901 goto err;
902
4c8a32f5
CL
903 if (priv->rxhw == PDR_SYNTH_FRONTEND_XBOW)
904 p54_init_xbow_synth(priv);
905 if (!(synth & PDR_SYNTH_24_GHZ_DISABLED))
1a9b6679
CL
906 dev->wiphy->bands[IEEE80211_BAND_2GHZ] =
907 priv->band_table[IEEE80211_BAND_2GHZ];
4c8a32f5 908 if (!(synth & PDR_SYNTH_5_GHZ_DISABLED))
1a9b6679
CL
909 dev->wiphy->bands[IEEE80211_BAND_5GHZ] =
910 priv->band_table[IEEE80211_BAND_5GHZ];
4c8a32f5
CL
911 if ((synth & PDR_SYNTH_RX_DIV_MASK) == PDR_SYNTH_RX_DIV_SUPPORTED)
912 priv->rx_diversity_mask = 3;
913 if ((synth & PDR_SYNTH_TX_DIV_MASK) == PDR_SYNTH_TX_DIV_SUPPORTED)
914 priv->tx_diversity_mask = 3;
915
916 if (!is_valid_ether_addr(dev->wiphy->perm_addr)) {
917 u8 perm_addr[ETH_ALEN];
918
c96c31e4 919 wiphy_warn(dev->wiphy,
5db55844 920 "Invalid hwaddr! Using randomly generated MAC addr\n");
f4f7f414 921 eth_random_addr(perm_addr);
4c8a32f5
CL
922 SET_IEEE80211_PERM_ADDR(dev, perm_addr);
923 }
924
7a047f4f
CL
925 priv->cur_rssi = &p54_rssi_default;
926
5db55844 927 wiphy_info(dev->wiphy, "hwaddr %pM, MAC:isl38%02x RF:%s\n",
c96c31e4
JP
928 dev->wiphy->perm_addr, priv->version,
929 p54_rf_chips[priv->rxhw]);
4c8a32f5
CL
930
931 return 0;
932
933err:
934 kfree(priv->iq_autocal);
935 kfree(priv->output_limit);
936 kfree(priv->curve_data);
7a047f4f 937 kfree(priv->rssi_db);
0d78156e 938 kfree(priv->survey);
4c8a32f5
CL
939 priv->iq_autocal = NULL;
940 priv->output_limit = NULL;
941 priv->curve_data = NULL;
7a047f4f 942 priv->rssi_db = NULL;
0d78156e 943 priv->survey = NULL;
4c8a32f5 944
c96c31e4 945 wiphy_err(dev->wiphy, "eeprom parse failed!\n");
4c8a32f5
CL
946 return err;
947}
948EXPORT_SYMBOL_GPL(p54_parse_eeprom);
949
950int p54_read_eeprom(struct ieee80211_hw *dev)
951{
952 struct p54_common *priv = dev->priv;
953 size_t eeprom_size = 0x2020, offset = 0, blocksize, maxblocksize;
954 int ret = -ENOMEM;
e6a3f551 955 void *eeprom;
4c8a32f5
CL
956
957 maxblocksize = EEPROM_READBACK_LEN;
958 if (priv->fw_var >= 0x509)
959 maxblocksize -= 0xc;
960 else
961 maxblocksize -= 0x4;
962
963 eeprom = kzalloc(eeprom_size, GFP_KERNEL);
964 if (unlikely(!eeprom))
965 goto free;
966
967 while (eeprom_size) {
968 blocksize = min(eeprom_size, maxblocksize);
2c208890 969 ret = p54_download_eeprom(priv, eeprom + offset,
4c8a32f5
CL
970 offset, blocksize);
971 if (unlikely(ret))
972 goto free;
973
974 offset += blocksize;
975 eeprom_size -= blocksize;
976 }
977
978 ret = p54_parse_eeprom(dev, eeprom, offset);
979free:
980 kfree(eeprom);
981 return ret;
982}
983EXPORT_SYMBOL_GPL(p54_read_eeprom);
This page took 0.654434 seconds and 5 git commands to generate.