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