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