eeprom.c revision 4c8a32f57105a78c49e01f083717cdb531d3c2b9
1/*
2 * EEPROM parser code for mac80211 Prism54 drivers
3 *
4 * Copyright (c) 2006, Michael Wu <flamingice@sourmilk.net>
5 * Copyright (c) 2007-2009, Christian Lamparter <chunkeey@web.de>
6 * Copyright 2008, Johannes Berg <johannes@sipsolutions.net>
7 *
8 * Based on:
9 * - the islsm (softmac prism54) driver, which is:
10 *   Copyright 2004-2006 Jean-Baptiste Note <jbnote@gmail.com>, et al.
11 * - stlc45xx driver
12 *   Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies).
13 *
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License version 2 as
16 * published by the Free Software Foundation.
17 */
18
19#include <linux/init.h>
20#include <linux/firmware.h>
21#include <linux/etherdevice.h>
22
23#include <net/mac80211.h>
24
25#include "p54.h"
26#include "eeprom.h"
27#include "lmac.h"
28
29static 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
44static struct ieee80211_channel p54_bgchannels[] = {
45	{ .center_freq = 2412, .hw_value = 1, },
46	{ .center_freq = 2417, .hw_value = 2, },
47	{ .center_freq = 2422, .hw_value = 3, },
48	{ .center_freq = 2427, .hw_value = 4, },
49	{ .center_freq = 2432, .hw_value = 5, },
50	{ .center_freq = 2437, .hw_value = 6, },
51	{ .center_freq = 2442, .hw_value = 7, },
52	{ .center_freq = 2447, .hw_value = 8, },
53	{ .center_freq = 2452, .hw_value = 9, },
54	{ .center_freq = 2457, .hw_value = 10, },
55	{ .center_freq = 2462, .hw_value = 11, },
56	{ .center_freq = 2467, .hw_value = 12, },
57	{ .center_freq = 2472, .hw_value = 13, },
58	{ .center_freq = 2484, .hw_value = 14, },
59};
60
61static struct ieee80211_supported_band band_2GHz = {
62	.channels = p54_bgchannels,
63	.n_channels = ARRAY_SIZE(p54_bgchannels),
64	.bitrates = p54_bgrates,
65	.n_bitrates = ARRAY_SIZE(p54_bgrates),
66};
67
68static struct ieee80211_rate p54_arates[] = {
69	{ .bitrate = 60, .hw_value = 4, },
70	{ .bitrate = 90, .hw_value = 5, },
71	{ .bitrate = 120, .hw_value = 6, },
72	{ .bitrate = 180, .hw_value = 7, },
73	{ .bitrate = 240, .hw_value = 8, },
74	{ .bitrate = 360, .hw_value = 9, },
75	{ .bitrate = 480, .hw_value = 10, },
76	{ .bitrate = 540, .hw_value = 11, },
77};
78
79static struct ieee80211_channel p54_achannels[] = {
80	{ .center_freq = 4920 },
81	{ .center_freq = 4940 },
82	{ .center_freq = 4960 },
83	{ .center_freq = 4980 },
84	{ .center_freq = 5040 },
85	{ .center_freq = 5060 },
86	{ .center_freq = 5080 },
87	{ .center_freq = 5170 },
88	{ .center_freq = 5180 },
89	{ .center_freq = 5190 },
90	{ .center_freq = 5200 },
91	{ .center_freq = 5210 },
92	{ .center_freq = 5220 },
93	{ .center_freq = 5230 },
94	{ .center_freq = 5240 },
95	{ .center_freq = 5260 },
96	{ .center_freq = 5280 },
97	{ .center_freq = 5300 },
98	{ .center_freq = 5320 },
99	{ .center_freq = 5500 },
100	{ .center_freq = 5520 },
101	{ .center_freq = 5540 },
102	{ .center_freq = 5560 },
103	{ .center_freq = 5580 },
104	{ .center_freq = 5600 },
105	{ .center_freq = 5620 },
106	{ .center_freq = 5640 },
107	{ .center_freq = 5660 },
108	{ .center_freq = 5680 },
109	{ .center_freq = 5700 },
110	{ .center_freq = 5745 },
111	{ .center_freq = 5765 },
112	{ .center_freq = 5785 },
113	{ .center_freq = 5805 },
114	{ .center_freq = 5825 },
115};
116
117static struct ieee80211_supported_band band_5GHz = {
118	.channels = p54_achannels,
119	.n_channels = ARRAY_SIZE(p54_achannels),
120	.bitrates = p54_arates,
121	.n_bitrates = ARRAY_SIZE(p54_arates),
122};
123
124static int p54_convert_rev0(struct ieee80211_hw *dev,
125			    struct pda_pa_curve_data *curve_data)
126{
127	struct p54_common *priv = dev->priv;
128	struct p54_pa_curve_data_sample *dst;
129	struct pda_pa_curve_data_sample_rev0 *src;
130	size_t cd_len = sizeof(*curve_data) +
131		(curve_data->points_per_channel*sizeof(*dst) + 2) *
132		 curve_data->channels;
133	unsigned int i, j;
134	void *source, *target;
135
136	priv->curve_data = kmalloc(sizeof(*priv->curve_data) + cd_len,
137				   GFP_KERNEL);
138	if (!priv->curve_data)
139		return -ENOMEM;
140
141	priv->curve_data->entries = curve_data->channels;
142	priv->curve_data->entry_size = sizeof(__le16) +
143		sizeof(*dst) * curve_data->points_per_channel;
144	priv->curve_data->offset = offsetof(struct pda_pa_curve_data, data);
145	priv->curve_data->len = cd_len;
146	memcpy(priv->curve_data->data, curve_data, sizeof(*curve_data));
147	source = curve_data->data;
148	target = ((struct pda_pa_curve_data *) priv->curve_data->data)->data;
149	for (i = 0; i < curve_data->channels; i++) {
150		__le16 *freq = source;
151		source += sizeof(__le16);
152		*((__le16 *)target) = *freq;
153		target += sizeof(__le16);
154		for (j = 0; j < curve_data->points_per_channel; j++) {
155			dst = target;
156			src = source;
157
158			dst->rf_power = src->rf_power;
159			dst->pa_detector = src->pa_detector;
160			dst->data_64qam = src->pcv;
161			/* "invent" the points for the other modulations */
162#define SUB(x, y) (u8)(((x) - (y)) > (x) ? 0 : (x) - (y))
163			dst->data_16qam = SUB(src->pcv, 12);
164			dst->data_qpsk = SUB(dst->data_16qam, 12);
165			dst->data_bpsk = SUB(dst->data_qpsk, 12);
166			dst->data_barker = SUB(dst->data_bpsk, 14);
167#undef SUB
168			target += sizeof(*dst);
169			source += sizeof(*src);
170		}
171	}
172
173	return 0;
174}
175
176static int p54_convert_rev1(struct ieee80211_hw *dev,
177			    struct pda_pa_curve_data *curve_data)
178{
179	struct p54_common *priv = dev->priv;
180	struct p54_pa_curve_data_sample *dst;
181	struct pda_pa_curve_data_sample_rev1 *src;
182	size_t cd_len = sizeof(*curve_data) +
183		(curve_data->points_per_channel*sizeof(*dst) + 2) *
184		 curve_data->channels;
185	unsigned int i, j;
186	void *source, *target;
187
188	priv->curve_data = kzalloc(cd_len + sizeof(*priv->curve_data),
189				   GFP_KERNEL);
190	if (!priv->curve_data)
191		return -ENOMEM;
192
193	priv->curve_data->entries = curve_data->channels;
194	priv->curve_data->entry_size = sizeof(__le16) +
195		sizeof(*dst) * curve_data->points_per_channel;
196	priv->curve_data->offset = offsetof(struct pda_pa_curve_data, data);
197	priv->curve_data->len = cd_len;
198	memcpy(priv->curve_data->data, curve_data, sizeof(*curve_data));
199	source = curve_data->data;
200	target = ((struct pda_pa_curve_data *) priv->curve_data->data)->data;
201	for (i = 0; i < curve_data->channels; i++) {
202		__le16 *freq = source;
203		source += sizeof(__le16);
204		*((__le16 *)target) = *freq;
205		target += sizeof(__le16);
206		for (j = 0; j < curve_data->points_per_channel; j++) {
207			memcpy(target, source, sizeof(*src));
208
209			target += sizeof(*dst);
210			source += sizeof(*src);
211		}
212		source++;
213	}
214
215	return 0;
216}
217
218static const char *p54_rf_chips[] = { "INVALID-0", "Duette3", "Duette2",
219	"Frisbee", "Xbow", "Longbow", "INVALID-6", "INVALID-7" };
220
221static void p54_parse_rssical(struct ieee80211_hw *dev, void *data, int len,
222			     u16 type)
223{
224	struct p54_common *priv = dev->priv;
225	int offset = (type == PDR_RSSI_LINEAR_APPROXIMATION_EXTENDED) ? 2 : 0;
226	int entry_size = sizeof(struct pda_rssi_cal_entry) + offset;
227	int num_entries = (type == PDR_RSSI_LINEAR_APPROXIMATION) ? 1 : 2;
228	int i;
229
230	if (len != (entry_size * num_entries)) {
231		printk(KERN_ERR "%s: unknown rssi calibration data packing "
232				 " type:(%x) len:%d.\n",
233		       wiphy_name(dev->wiphy), type, len);
234
235		print_hex_dump_bytes("rssical:", DUMP_PREFIX_NONE,
236				     data, len);
237
238		printk(KERN_ERR "%s: please report this issue.\n",
239			wiphy_name(dev->wiphy));
240		return;
241	}
242
243	for (i = 0; i < num_entries; i++) {
244		struct pda_rssi_cal_entry *cal = data +
245						 (offset + i * entry_size);
246		priv->rssical_db[i].mul = (s16) le16_to_cpu(cal->mul);
247		priv->rssical_db[i].add = (s16) le16_to_cpu(cal->add);
248	}
249}
250
251static void p54_parse_default_country(struct ieee80211_hw *dev,
252				      void *data, int len)
253{
254	struct pda_country *country;
255
256	if (len != sizeof(*country)) {
257		printk(KERN_ERR "%s: found possible invalid default country "
258				"eeprom entry. (entry size: %d)\n",
259		       wiphy_name(dev->wiphy), len);
260
261		print_hex_dump_bytes("country:", DUMP_PREFIX_NONE,
262				     data, len);
263
264		printk(KERN_ERR "%s: please report this issue.\n",
265			wiphy_name(dev->wiphy));
266		return;
267	}
268
269	country = (struct pda_country *) data;
270	if (country->flags == PDR_COUNTRY_CERT_CODE_PSEUDO)
271		regulatory_hint(dev->wiphy, country->alpha2);
272	else {
273		/* TODO:
274		 * write a shared/common function that converts
275		 * "Regulatory domain codes" (802.11-2007 14.8.2.2)
276		 * into ISO/IEC 3166-1 alpha2 for regulatory_hint.
277		 */
278	}
279}
280
281static int p54_convert_output_limits(struct ieee80211_hw *dev,
282				     u8 *data, size_t len)
283{
284	struct p54_common *priv = dev->priv;
285
286	if (len < 2)
287		return -EINVAL;
288
289	if (data[0] != 0) {
290		printk(KERN_ERR "%s: unknown output power db revision:%x\n",
291		       wiphy_name(dev->wiphy), data[0]);
292		return -EINVAL;
293	}
294
295	if (2 + data[1] * sizeof(struct pda_channel_output_limit) > len)
296		return -EINVAL;
297
298	priv->output_limit = kmalloc(data[1] *
299		sizeof(struct pda_channel_output_limit) +
300		sizeof(*priv->output_limit), GFP_KERNEL);
301
302	if (!priv->output_limit)
303		return -ENOMEM;
304
305	priv->output_limit->offset = 0;
306	priv->output_limit->entries = data[1];
307	priv->output_limit->entry_size =
308		sizeof(struct pda_channel_output_limit);
309	priv->output_limit->len = priv->output_limit->entry_size *
310				  priv->output_limit->entries +
311				  priv->output_limit->offset;
312
313	memcpy(priv->output_limit->data, &data[2],
314	       data[1] * sizeof(struct pda_channel_output_limit));
315
316	return 0;
317}
318
319static struct p54_cal_database *p54_convert_db(struct pda_custom_wrapper *src,
320					       size_t total_len)
321{
322	struct p54_cal_database *dst;
323	size_t payload_len, entries, entry_size, offset;
324
325	payload_len = le16_to_cpu(src->len);
326	entries = le16_to_cpu(src->entries);
327	entry_size = le16_to_cpu(src->entry_size);
328	offset = le16_to_cpu(src->offset);
329	if (((entries * entry_size + offset) != payload_len) ||
330	     (payload_len + sizeof(*src) != total_len))
331		return NULL;
332
333	dst = kmalloc(sizeof(*dst) + payload_len, GFP_KERNEL);
334	if (!dst)
335		return NULL;
336
337	dst->entries = entries;
338	dst->entry_size = entry_size;
339	dst->offset = offset;
340	dst->len = payload_len;
341
342	memcpy(dst->data, src->data, payload_len);
343	return dst;
344}
345
346int p54_parse_eeprom(struct ieee80211_hw *dev, void *eeprom, int len)
347{
348	struct p54_common *priv = dev->priv;
349	struct eeprom_pda_wrap *wrap = NULL;
350	struct pda_entry *entry;
351	unsigned int data_len, entry_len;
352	void *tmp;
353	int err;
354	u8 *end = (u8 *)eeprom + len;
355	u16 synth = 0;
356
357	wrap = (struct eeprom_pda_wrap *) eeprom;
358	entry = (void *)wrap->data + le16_to_cpu(wrap->len);
359
360	/* verify that at least the entry length/code fits */
361	while ((u8 *)entry <= end - sizeof(*entry)) {
362		entry_len = le16_to_cpu(entry->len);
363		data_len = ((entry_len - 1) << 1);
364
365		/* abort if entry exceeds whole structure */
366		if ((u8 *)entry + sizeof(*entry) + data_len > end)
367			break;
368
369		switch (le16_to_cpu(entry->code)) {
370		case PDR_MAC_ADDRESS:
371			if (data_len != ETH_ALEN)
372				break;
373			SET_IEEE80211_PERM_ADDR(dev, entry->data);
374			break;
375		case PDR_PRISM_PA_CAL_OUTPUT_POWER_LIMITS:
376			if (priv->output_limit)
377				break;
378			err = p54_convert_output_limits(dev, entry->data,
379							data_len);
380			if (err)
381				goto err;
382			break;
383		case PDR_PRISM_PA_CAL_CURVE_DATA: {
384			struct pda_pa_curve_data *curve_data =
385				(struct pda_pa_curve_data *)entry->data;
386			if (data_len < sizeof(*curve_data)) {
387				err = -EINVAL;
388				goto err;
389			}
390
391			switch (curve_data->cal_method_rev) {
392			case 0:
393				err = p54_convert_rev0(dev, curve_data);
394				break;
395			case 1:
396				err = p54_convert_rev1(dev, curve_data);
397				break;
398			default:
399				printk(KERN_ERR "%s: unknown curve data "
400						"revision %d\n",
401						wiphy_name(dev->wiphy),
402						curve_data->cal_method_rev);
403				err = -ENODEV;
404				break;
405			}
406			if (err)
407				goto err;
408			}
409			break;
410		case PDR_PRISM_ZIF_TX_IQ_CALIBRATION:
411			priv->iq_autocal = kmalloc(data_len, GFP_KERNEL);
412			if (!priv->iq_autocal) {
413				err = -ENOMEM;
414				goto err;
415			}
416
417			memcpy(priv->iq_autocal, entry->data, data_len);
418			priv->iq_autocal_len = data_len / sizeof(struct pda_iq_autocal_entry);
419			break;
420		case PDR_DEFAULT_COUNTRY:
421			p54_parse_default_country(dev, entry->data, data_len);
422			break;
423		case PDR_INTERFACE_LIST:
424			tmp = entry->data;
425			while ((u8 *)tmp < entry->data + data_len) {
426				struct exp_if *exp_if = tmp;
427				if (exp_if->if_id == cpu_to_le16(IF_ID_ISL39000))
428					synth = le16_to_cpu(exp_if->variant);
429				tmp += sizeof(*exp_if);
430			}
431			break;
432		case PDR_HARDWARE_PLATFORM_COMPONENT_ID:
433			if (data_len < 2)
434				break;
435			priv->version = *(u8 *)(entry->data + 1);
436			break;
437		case PDR_RSSI_LINEAR_APPROXIMATION:
438		case PDR_RSSI_LINEAR_APPROXIMATION_DUAL_BAND:
439		case PDR_RSSI_LINEAR_APPROXIMATION_EXTENDED:
440			p54_parse_rssical(dev, entry->data, data_len,
441					  le16_to_cpu(entry->code));
442			break;
443		case PDR_RSSI_LINEAR_APPROXIMATION_CUSTOM: {
444			__le16 *src = (void *) entry->data;
445			s16 *dst = (void *) &priv->rssical_db;
446			int i;
447
448			if (data_len != sizeof(priv->rssical_db)) {
449				err = -EINVAL;
450				goto err;
451			}
452			for (i = 0; i < sizeof(priv->rssical_db) /
453					sizeof(*src); i++)
454				*(dst++) = (s16) le16_to_cpu(*(src++));
455			}
456			break;
457		case PDR_PRISM_PA_CAL_OUTPUT_POWER_LIMITS_CUSTOM: {
458			struct pda_custom_wrapper *pda = (void *) entry->data;
459			if (priv->output_limit || data_len < sizeof(*pda))
460				break;
461			priv->output_limit = p54_convert_db(pda, data_len);
462			}
463			break;
464		case PDR_PRISM_PA_CAL_CURVE_DATA_CUSTOM: {
465			struct pda_custom_wrapper *pda = (void *) entry->data;
466			if (priv->curve_data || data_len < sizeof(*pda))
467				break;
468			priv->curve_data = p54_convert_db(pda, data_len);
469			}
470			break;
471		case PDR_END:
472			/* make it overrun */
473			entry_len = len;
474			break;
475		default:
476			break;
477		}
478
479		entry = (void *)entry + (entry_len + 1)*2;
480	}
481
482	if (!synth || !priv->iq_autocal || !priv->output_limit ||
483	    !priv->curve_data) {
484		printk(KERN_ERR "%s: not all required entries found in eeprom!\n",
485			wiphy_name(dev->wiphy));
486		err = -EINVAL;
487		goto err;
488	}
489
490	priv->rxhw = synth & PDR_SYNTH_FRONTEND_MASK;
491	if (priv->rxhw == PDR_SYNTH_FRONTEND_XBOW)
492		p54_init_xbow_synth(priv);
493	if (!(synth & PDR_SYNTH_24_GHZ_DISABLED))
494		dev->wiphy->bands[IEEE80211_BAND_2GHZ] = &band_2GHz;
495	if (!(synth & PDR_SYNTH_5_GHZ_DISABLED))
496		dev->wiphy->bands[IEEE80211_BAND_5GHZ] = &band_5GHz;
497	if ((synth & PDR_SYNTH_RX_DIV_MASK) == PDR_SYNTH_RX_DIV_SUPPORTED)
498		priv->rx_diversity_mask = 3;
499	if ((synth & PDR_SYNTH_TX_DIV_MASK) == PDR_SYNTH_TX_DIV_SUPPORTED)
500		priv->tx_diversity_mask = 3;
501
502	if (!is_valid_ether_addr(dev->wiphy->perm_addr)) {
503		u8 perm_addr[ETH_ALEN];
504
505		printk(KERN_WARNING "%s: Invalid hwaddr! Using randomly generated MAC addr\n",
506			wiphy_name(dev->wiphy));
507		random_ether_addr(perm_addr);
508		SET_IEEE80211_PERM_ADDR(dev, perm_addr);
509	}
510
511	printk(KERN_INFO "%s: hwaddr %pM, MAC:isl38%02x RF:%s\n",
512		wiphy_name(dev->wiphy),	dev->wiphy->perm_addr, priv->version,
513		p54_rf_chips[priv->rxhw]);
514
515	return 0;
516
517err:
518	kfree(priv->iq_autocal);
519	kfree(priv->output_limit);
520	kfree(priv->curve_data);
521	priv->iq_autocal = NULL;
522	priv->output_limit = NULL;
523	priv->curve_data = NULL;
524
525	printk(KERN_ERR "%s: eeprom parse failed!\n",
526		wiphy_name(dev->wiphy));
527	return err;
528}
529EXPORT_SYMBOL_GPL(p54_parse_eeprom);
530
531int p54_read_eeprom(struct ieee80211_hw *dev)
532{
533	struct p54_common *priv = dev->priv;
534	size_t eeprom_size = 0x2020, offset = 0, blocksize, maxblocksize;
535	int ret = -ENOMEM;
536	void *eeprom = NULL;
537
538	maxblocksize = EEPROM_READBACK_LEN;
539	if (priv->fw_var >= 0x509)
540		maxblocksize -= 0xc;
541	else
542		maxblocksize -= 0x4;
543
544	eeprom = kzalloc(eeprom_size, GFP_KERNEL);
545	if (unlikely(!eeprom))
546		goto free;
547
548	while (eeprom_size) {
549		blocksize = min(eeprom_size, maxblocksize);
550		ret = p54_download_eeprom(priv, (void *) (eeprom + offset),
551					  offset, blocksize);
552		if (unlikely(ret))
553			goto free;
554
555		offset += blocksize;
556		eeprom_size -= blocksize;
557	}
558
559	ret = p54_parse_eeprom(dev, eeprom, offset);
560free:
561	kfree(eeprom);
562	return ret;
563}
564EXPORT_SYMBOL_GPL(p54_read_eeprom);
565