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