xref: /linux/drivers/net/wireless/intersil/p54/eeprom.c (revision b5a051f6b840d48f159166ef073d3021989bfb50)
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,size_t len)416 static int p54_convert_rev0(struct ieee80211_hw *dev,
417 			    struct pda_pa_curve_data *curve_data, size_t len)
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 needed = curve_data->channels *
423 		(sizeof(*src) * curve_data->points_per_channel + 2);
424 	size_t cd_len = sizeof(*curve_data) +
425 		(curve_data->points_per_channel*sizeof(*dst) + 2) *
426 		 curve_data->channels;
427 	unsigned int i, j;
428 	void *source, *target;
429 
430 	if (len < sizeof(*curve_data) + needed)
431 		return -EINVAL;
432 
433 	priv->curve_data = kmalloc(sizeof(*priv->curve_data) + cd_len,
434 				   GFP_KERNEL);
435 	if (!priv->curve_data)
436 		return -ENOMEM;
437 
438 	priv->curve_data->entries = curve_data->channels;
439 	priv->curve_data->entry_size = sizeof(__le16) +
440 		sizeof(*dst) * curve_data->points_per_channel;
441 	priv->curve_data->offset = offsetof(struct pda_pa_curve_data, data);
442 	priv->curve_data->len = cd_len;
443 	memcpy(priv->curve_data->data, curve_data, sizeof(*curve_data));
444 	source = curve_data->data;
445 	target = ((struct pda_pa_curve_data *) priv->curve_data->data)->data;
446 	for (i = 0; i < curve_data->channels; i++) {
447 		__le16 *freq = source;
448 		source += sizeof(__le16);
449 		*((__le16 *)target) = *freq;
450 		target += sizeof(__le16);
451 		for (j = 0; j < curve_data->points_per_channel; j++) {
452 			dst = target;
453 			src = source;
454 
455 			dst->rf_power = src->rf_power;
456 			dst->pa_detector = src->pa_detector;
457 			dst->data_64qam = src->pcv;
458 			/* "invent" the points for the other modulations */
459 #define SUB(x, y) (u8)(((x) - (y)) > (x) ? 0 : (x) - (y))
460 			dst->data_16qam = SUB(src->pcv, 12);
461 			dst->data_qpsk = SUB(dst->data_16qam, 12);
462 			dst->data_bpsk = SUB(dst->data_qpsk, 12);
463 			dst->data_barker = SUB(dst->data_bpsk, 14);
464 #undef SUB
465 			target += sizeof(*dst);
466 			source += sizeof(*src);
467 		}
468 	}
469 
470 	return 0;
471 }
472 
p54_convert_rev1(struct ieee80211_hw * dev,struct pda_pa_curve_data * curve_data,size_t len)473 static int p54_convert_rev1(struct ieee80211_hw *dev,
474 			    struct pda_pa_curve_data *curve_data, size_t len)
475 {
476 	struct p54_common *priv = dev->priv;
477 	struct p54_pa_curve_data_sample *dst;
478 	struct pda_pa_curve_data_sample_rev1 *src;
479 	size_t needed = curve_data->channels *
480 		(sizeof(*src) * curve_data->points_per_channel + 3);
481 	size_t cd_len = sizeof(*curve_data) +
482 		(curve_data->points_per_channel*sizeof(*dst) + 2) *
483 		 curve_data->channels;
484 	unsigned int i, j;
485 	void *source, *target;
486 
487 	if (len < sizeof(*curve_data) + needed)
488 		return -EINVAL;
489 
490 	priv->curve_data = kzalloc(cd_len + sizeof(*priv->curve_data),
491 				   GFP_KERNEL);
492 	if (!priv->curve_data)
493 		return -ENOMEM;
494 
495 	priv->curve_data->entries = curve_data->channels;
496 	priv->curve_data->entry_size = sizeof(__le16) +
497 		sizeof(*dst) * curve_data->points_per_channel;
498 	priv->curve_data->offset = offsetof(struct pda_pa_curve_data, data);
499 	priv->curve_data->len = cd_len;
500 	memcpy(priv->curve_data->data, curve_data, sizeof(*curve_data));
501 	source = curve_data->data;
502 	target = ((struct pda_pa_curve_data *) priv->curve_data->data)->data;
503 	for (i = 0; i < curve_data->channels; i++) {
504 		__le16 *freq = source;
505 		source += sizeof(__le16);
506 		*((__le16 *)target) = *freq;
507 		target += sizeof(__le16);
508 		for (j = 0; j < curve_data->points_per_channel; j++) {
509 			memcpy(target, source, sizeof(*src));
510 
511 			target += sizeof(*dst);
512 			source += sizeof(*src);
513 		}
514 		source++;
515 	}
516 
517 	return 0;
518 }
519 
520 static const char *p54_rf_chips[] = { "INVALID-0", "Duette3", "Duette2",
521 	"Frisbee", "Xbow", "Longbow", "INVALID-6", "INVALID-7" };
522 
p54_parse_rssical(struct ieee80211_hw * dev,u8 * data,int len,u16 type)523 static int p54_parse_rssical(struct ieee80211_hw *dev,
524 			     u8 *data, int len, u16 type)
525 {
526 	struct p54_common *priv = dev->priv;
527 	struct p54_rssi_db_entry *entry;
528 	size_t db_len, entries;
529 	int offset = 0, i;
530 
531 	if (type != PDR_RSSI_LINEAR_APPROXIMATION_EXTENDED) {
532 		entries = (type == PDR_RSSI_LINEAR_APPROXIMATION) ? 1 : 2;
533 		if (len != sizeof(struct pda_rssi_cal_entry) * entries) {
534 			wiphy_err(dev->wiphy, "rssical size mismatch.\n");
535 			goto err_data;
536 		}
537 	} else {
538 		/*
539 		 * Some devices (Dell 1450 USB, Xbow 5GHz card, etc...)
540 		 * have an empty two byte header.
541 		 */
542 		if (*((__le16 *)&data[offset]) == cpu_to_le16(0))
543 			offset += 2;
544 
545 		entries = (len - offset) /
546 			sizeof(struct pda_rssi_cal_ext_entry);
547 
548 		if (len < offset ||
549 		    (len - offset) % sizeof(struct pda_rssi_cal_ext_entry) ||
550 		    entries == 0) {
551 			wiphy_err(dev->wiphy, "invalid rssi database.\n");
552 			goto err_data;
553 		}
554 	}
555 
556 	db_len = sizeof(*entry) * entries;
557 	priv->rssi_db = kzalloc(db_len + sizeof(*priv->rssi_db), GFP_KERNEL);
558 	if (!priv->rssi_db)
559 		return -ENOMEM;
560 
561 	priv->rssi_db->offset = 0;
562 	priv->rssi_db->entries = entries;
563 	priv->rssi_db->entry_size = sizeof(*entry);
564 	priv->rssi_db->len = db_len;
565 
566 	entry = (void *)((unsigned long)priv->rssi_db->data + priv->rssi_db->offset);
567 	if (type == PDR_RSSI_LINEAR_APPROXIMATION_EXTENDED) {
568 		struct pda_rssi_cal_ext_entry *cal = (void *) &data[offset];
569 
570 		for (i = 0; i < entries; i++) {
571 			entry[i].freq = le16_to_cpu(cal[i].freq);
572 			entry[i].mul = (s16) le16_to_cpu(cal[i].mul);
573 			entry[i].add = (s16) le16_to_cpu(cal[i].add);
574 		}
575 	} else {
576 		struct pda_rssi_cal_entry *cal = (void *) &data[offset];
577 
578 		for (i = 0; i < entries; i++) {
579 			u16 freq = 0;
580 			switch (i) {
581 			case NL80211_BAND_2GHZ:
582 				freq = 2437;
583 				break;
584 			case NL80211_BAND_5GHZ:
585 				freq = 5240;
586 				break;
587 			}
588 
589 			entry[i].freq = freq;
590 			entry[i].mul = (s16) le16_to_cpu(cal[i].mul);
591 			entry[i].add = (s16) le16_to_cpu(cal[i].add);
592 		}
593 	}
594 
595 	/* sort the list by channel frequency */
596 	sort(entry, entries, sizeof(*entry), p54_compare_rssichan, NULL);
597 	return 0;
598 
599 err_data:
600 	wiphy_err(dev->wiphy,
601 		  "rssi calibration data packing type:(%x) len:%d.\n",
602 		  type, len);
603 
604 	print_hex_dump_bytes("rssical:", DUMP_PREFIX_NONE, data, len);
605 
606 	wiphy_err(dev->wiphy, "please report this issue.\n");
607 	return -EINVAL;
608 }
609 
p54_rssi_find(struct p54_common * priv,const u16 freq)610 struct p54_rssi_db_entry *p54_rssi_find(struct p54_common *priv, const u16 freq)
611 {
612 	struct p54_rssi_db_entry *entry;
613 	int i, found = -1;
614 
615 	if (!priv->rssi_db)
616 		return &p54_rssi_default;
617 
618 	entry = (void *)(priv->rssi_db->data + priv->rssi_db->offset);
619 	for (i = 0; i < priv->rssi_db->entries; i++) {
620 		if (!same_band(freq, entry[i].freq))
621 			continue;
622 
623 		if (found == -1) {
624 			found = i;
625 			continue;
626 		}
627 
628 		/* nearest match */
629 		if (abs(freq - entry[i].freq) <
630 		    abs(freq - entry[found].freq)) {
631 			found = i;
632 			continue;
633 		} else {
634 			break;
635 		}
636 	}
637 
638 	return found < 0 ? &p54_rssi_default : &entry[found];
639 }
640 
p54_parse_default_country(struct ieee80211_hw * dev,void * data,int len)641 static void p54_parse_default_country(struct ieee80211_hw *dev,
642 				      void *data, int len)
643 {
644 	struct pda_country *country;
645 
646 	if (len != sizeof(*country)) {
647 		wiphy_err(dev->wiphy,
648 			  "found possible invalid default country eeprom entry. (entry size: %d)\n",
649 			  len);
650 
651 		print_hex_dump_bytes("country:", DUMP_PREFIX_NONE,
652 				     data, len);
653 
654 		wiphy_err(dev->wiphy, "please report this issue.\n");
655 		return;
656 	}
657 
658 	country = (struct pda_country *) data;
659 	if (country->flags == PDR_COUNTRY_CERT_CODE_PSEUDO)
660 		regulatory_hint(dev->wiphy, country->alpha2);
661 	else {
662 		/* TODO:
663 		 * write a shared/common function that converts
664 		 * "Regulatory domain codes" (802.11-2007 14.8.2.2)
665 		 * into ISO/IEC 3166-1 alpha2 for regulatory_hint.
666 		 */
667 	}
668 }
669 
p54_convert_output_limits(struct ieee80211_hw * dev,u8 * data,size_t len)670 static int p54_convert_output_limits(struct ieee80211_hw *dev,
671 				     u8 *data, size_t len)
672 {
673 	struct p54_common *priv = dev->priv;
674 
675 	if (len < 2)
676 		return -EINVAL;
677 
678 	if (data[0] != 0) {
679 		wiphy_err(dev->wiphy, "unknown output power db revision:%x\n",
680 			  data[0]);
681 		return -EINVAL;
682 	}
683 
684 	if (2 + data[1] * sizeof(struct pda_channel_output_limit) > len)
685 		return -EINVAL;
686 
687 	priv->output_limit = kmalloc(data[1] *
688 		sizeof(struct pda_channel_output_limit) +
689 		sizeof(*priv->output_limit), GFP_KERNEL);
690 
691 	if (!priv->output_limit)
692 		return -ENOMEM;
693 
694 	priv->output_limit->offset = 0;
695 	priv->output_limit->entries = data[1];
696 	priv->output_limit->entry_size =
697 		sizeof(struct pda_channel_output_limit);
698 	priv->output_limit->len = priv->output_limit->entry_size *
699 				  priv->output_limit->entries +
700 				  priv->output_limit->offset;
701 
702 	memcpy(priv->output_limit->data, &data[2],
703 	       data[1] * sizeof(struct pda_channel_output_limit));
704 
705 	return 0;
706 }
707 
p54_convert_db(struct pda_custom_wrapper * src,size_t total_len)708 static struct p54_cal_database *p54_convert_db(struct pda_custom_wrapper *src,
709 					       size_t total_len)
710 {
711 	struct p54_cal_database *dst;
712 	size_t payload_len, entries, entry_size, offset;
713 
714 	payload_len = le16_to_cpu(src->len);
715 	entries = le16_to_cpu(src->entries);
716 	entry_size = le16_to_cpu(src->entry_size);
717 	offset = le16_to_cpu(src->offset);
718 	if (((entries * entry_size + offset) != payload_len) ||
719 	     (payload_len + sizeof(*src) != total_len))
720 		return NULL;
721 
722 	dst = kmalloc(sizeof(*dst) + payload_len, GFP_KERNEL);
723 	if (!dst)
724 		return NULL;
725 
726 	dst->entries = entries;
727 	dst->entry_size = entry_size;
728 	dst->offset = offset;
729 	dst->len = payload_len;
730 
731 	memcpy(dst->data, src->data, payload_len);
732 	return dst;
733 }
734 
p54_parse_eeprom(struct ieee80211_hw * dev,void * eeprom,int len)735 int p54_parse_eeprom(struct ieee80211_hw *dev, void *eeprom, int len)
736 {
737 	struct p54_common *priv = dev->priv;
738 	struct eeprom_pda_wrap *wrap;
739 	struct pda_entry *entry;
740 	unsigned int data_len, entry_len;
741 	void *tmp;
742 	int err;
743 	u8 *end = (u8 *)eeprom + len;
744 	u16 synth = 0;
745 	u16 crc16 = ~0;
746 
747 	wrap = (struct eeprom_pda_wrap *) eeprom;
748 	entry = (void *)wrap->data + le16_to_cpu(wrap->len);
749 
750 	/* verify that at least the entry length/code fits */
751 	while ((u8 *)entry <= end - sizeof(*entry)) {
752 		entry_len = le16_to_cpu(entry->len);
753 		data_len = ((entry_len - 1) << 1);
754 
755 		/* abort if entry exceeds whole structure */
756 		if ((u8 *)entry + sizeof(*entry) + data_len > end)
757 			break;
758 
759 		switch (le16_to_cpu(entry->code)) {
760 		case PDR_MAC_ADDRESS:
761 			if (data_len != ETH_ALEN)
762 				break;
763 			SET_IEEE80211_PERM_ADDR(dev, entry->data);
764 			break;
765 		case PDR_PRISM_PA_CAL_OUTPUT_POWER_LIMITS:
766 			if (priv->output_limit)
767 				break;
768 			err = p54_convert_output_limits(dev, entry->data,
769 							data_len);
770 			if (err)
771 				goto err;
772 			break;
773 		case PDR_PRISM_PA_CAL_CURVE_DATA: {
774 			struct pda_pa_curve_data *curve_data =
775 				(struct pda_pa_curve_data *)entry->data;
776 
777 			if (data_len < sizeof(*curve_data)) {
778 				err = -EINVAL;
779 				goto err;
780 			}
781 
782 			switch (curve_data->cal_method_rev) {
783 			case 0:
784 				err = p54_convert_rev0(dev, curve_data, data_len);
785 				break;
786 			case 1:
787 				err = p54_convert_rev1(dev, curve_data, data_len);
788 				break;
789 			default:
790 				wiphy_err(dev->wiphy,
791 					  "unknown curve data revision %d\n",
792 					  curve_data->cal_method_rev);
793 				err = -ENODEV;
794 				break;
795 			}
796 			if (err)
797 				goto err;
798 			}
799 			break;
800 		case PDR_PRISM_ZIF_TX_IQ_CALIBRATION:
801 			priv->iq_autocal = kmemdup(entry->data, data_len,
802 						   GFP_KERNEL);
803 			if (!priv->iq_autocal) {
804 				err = -ENOMEM;
805 				goto err;
806 			}
807 
808 			priv->iq_autocal_len = data_len / sizeof(struct pda_iq_autocal_entry);
809 			break;
810 		case PDR_DEFAULT_COUNTRY:
811 			p54_parse_default_country(dev, entry->data, data_len);
812 			break;
813 		case PDR_INTERFACE_LIST:
814 			tmp = entry->data;
815 			while ((u8 *)tmp + sizeof(struct exp_if) <=
816 			       entry->data + data_len) {
817 				struct exp_if *exp_if = tmp;
818 				if (exp_if->if_id == cpu_to_le16(IF_ID_ISL39000))
819 					synth = le16_to_cpu(exp_if->variant);
820 				tmp += sizeof(*exp_if);
821 			}
822 			break;
823 		case PDR_HARDWARE_PLATFORM_COMPONENT_ID:
824 			if (data_len < 2)
825 				break;
826 			priv->version = *(u8 *)(entry->data + 1);
827 			break;
828 		case PDR_RSSI_LINEAR_APPROXIMATION:
829 		case PDR_RSSI_LINEAR_APPROXIMATION_DUAL_BAND:
830 		case PDR_RSSI_LINEAR_APPROXIMATION_EXTENDED:
831 			err = p54_parse_rssical(dev, entry->data, data_len,
832 						le16_to_cpu(entry->code));
833 			if (err)
834 				goto err;
835 			break;
836 		case PDR_RSSI_LINEAR_APPROXIMATION_CUSTOMV2: {
837 			struct pda_custom_wrapper *pda = (void *) entry->data;
838 			__le16 *src;
839 			u16 *dst;
840 			int i;
841 
842 			if (priv->rssi_db || data_len < sizeof(*pda))
843 				break;
844 
845 			priv->rssi_db = p54_convert_db(pda, data_len);
846 			if (!priv->rssi_db)
847 				break;
848 
849 			src = (void *) priv->rssi_db->data;
850 			dst = (void *) priv->rssi_db->data;
851 
852 			for (i = 0; i < priv->rssi_db->entries; i++)
853 				*(dst++) = (s16) le16_to_cpu(*(src++));
854 
855 			}
856 			break;
857 		case PDR_PRISM_PA_CAL_OUTPUT_POWER_LIMITS_CUSTOM: {
858 			struct pda_custom_wrapper *pda = (void *) entry->data;
859 			if (priv->output_limit || data_len < sizeof(*pda))
860 				break;
861 			priv->output_limit = p54_convert_db(pda, data_len);
862 			}
863 			break;
864 		case PDR_PRISM_PA_CAL_CURVE_DATA_CUSTOM: {
865 			struct pda_custom_wrapper *pda = (void *) entry->data;
866 			if (priv->curve_data || data_len < sizeof(*pda))
867 				break;
868 			priv->curve_data = p54_convert_db(pda, data_len);
869 			}
870 			break;
871 		case PDR_END:
872 			crc16 = ~crc_ccitt(crc16, (u8 *) entry, sizeof(*entry));
873 			if (crc16 != le16_to_cpup((__le16 *)entry->data)) {
874 				wiphy_err(dev->wiphy, "eeprom failed checksum "
875 					 "test!\n");
876 				err = -ENOMSG;
877 				goto err;
878 			} else {
879 				goto good_eeprom;
880 			}
881 			break;
882 		default:
883 			break;
884 		}
885 
886 		crc16 = crc_ccitt(crc16, (u8 *)entry, (entry_len + 1) * 2);
887 		entry = (void *)entry + (entry_len + 1) * 2;
888 	}
889 
890 	wiphy_err(dev->wiphy, "unexpected end of eeprom data.\n");
891 	err = -ENODATA;
892 	goto err;
893 
894 good_eeprom:
895 	if (!synth || !priv->iq_autocal || !priv->output_limit ||
896 	    !priv->curve_data) {
897 		wiphy_err(dev->wiphy,
898 			  "not all required entries found in eeprom!\n");
899 		err = -EINVAL;
900 		goto err;
901 	}
902 
903 	priv->rxhw = synth & PDR_SYNTH_FRONTEND_MASK;
904 
905 	err = p54_generate_channel_lists(dev);
906 	if (err)
907 		goto err;
908 
909 	if (priv->rxhw == PDR_SYNTH_FRONTEND_XBOW)
910 		p54_init_xbow_synth(priv);
911 	if (!(synth & PDR_SYNTH_24_GHZ_DISABLED))
912 		dev->wiphy->bands[NL80211_BAND_2GHZ] =
913 			priv->band_table[NL80211_BAND_2GHZ];
914 	if (!(synth & PDR_SYNTH_5_GHZ_DISABLED))
915 		dev->wiphy->bands[NL80211_BAND_5GHZ] =
916 			priv->band_table[NL80211_BAND_5GHZ];
917 	if ((synth & PDR_SYNTH_RX_DIV_MASK) == PDR_SYNTH_RX_DIV_SUPPORTED)
918 		priv->rx_diversity_mask = 3;
919 	if ((synth & PDR_SYNTH_TX_DIV_MASK) == PDR_SYNTH_TX_DIV_SUPPORTED)
920 		priv->tx_diversity_mask = 3;
921 
922 	if (!is_valid_ether_addr(dev->wiphy->perm_addr)) {
923 		u8 perm_addr[ETH_ALEN];
924 
925 		wiphy_warn(dev->wiphy,
926 			   "Invalid hwaddr! Using randomly generated MAC addr\n");
927 		eth_random_addr(perm_addr);
928 		SET_IEEE80211_PERM_ADDR(dev, perm_addr);
929 	}
930 
931 	priv->cur_rssi = &p54_rssi_default;
932 
933 	wiphy_info(dev->wiphy, "hwaddr %pM, MAC:isl38%02x RF:%s\n",
934 		   dev->wiphy->perm_addr, priv->version,
935 		   p54_rf_chips[priv->rxhw]);
936 
937 	return 0;
938 
939 err:
940 	kfree(priv->iq_autocal);
941 	kfree(priv->output_limit);
942 	kfree(priv->curve_data);
943 	kfree(priv->rssi_db);
944 	kfree(priv->survey);
945 	priv->iq_autocal = NULL;
946 	priv->output_limit = NULL;
947 	priv->curve_data = NULL;
948 	priv->rssi_db = NULL;
949 	priv->survey = NULL;
950 
951 	wiphy_err(dev->wiphy, "eeprom parse failed!\n");
952 	return err;
953 }
954 EXPORT_SYMBOL_GPL(p54_parse_eeprom);
955 
p54_read_eeprom(struct ieee80211_hw * dev)956 int p54_read_eeprom(struct ieee80211_hw *dev)
957 {
958 	struct p54_common *priv = dev->priv;
959 	size_t eeprom_size = 0x2020, offset = 0, blocksize, maxblocksize;
960 	int ret = -ENOMEM;
961 	void *eeprom;
962 
963 	maxblocksize = EEPROM_READBACK_LEN;
964 	if (priv->fw_var >= 0x509)
965 		maxblocksize -= 0xc;
966 	else
967 		maxblocksize -= 0x4;
968 
969 	eeprom = kzalloc(eeprom_size, GFP_KERNEL);
970 	if (unlikely(!eeprom))
971 		goto free;
972 
973 	while (eeprom_size) {
974 		blocksize = min(eeprom_size, maxblocksize);
975 		ret = p54_download_eeprom(priv, eeprom + offset,
976 					  offset, blocksize);
977 		if (unlikely(ret))
978 			goto free;
979 
980 		offset += blocksize;
981 		eeprom_size -= blocksize;
982 	}
983 
984 	ret = p54_parse_eeprom(dev, eeprom, offset);
985 free:
986 	kfree(eeprom);
987 	return ret;
988 }
989 EXPORT_SYMBOL_GPL(p54_read_eeprom);
990