2 * EEPROM parser code for mac80211 Prism54 drivers
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>
9 * - the islsm (softmac prism54) driver, which is:
10 * Copyright 2004-2006 Jean-Baptiste Note <jbnote@gmail.com>, et al.
12 * Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies).
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.
19 #include <linux/init.h>
20 #include <linux/firmware.h>
21 #include <linux/etherdevice.h>
22 #include <linux/sort.h>
23 #include <linux/slab.h>
25 #include <net/mac80211.h>
26 #include <linux/crc-ccitt.h>
32 static struct ieee80211_rate p54_bgrates[] = {
33 { .bitrate = 10, .hw_value = 0, },
34 { .bitrate = 20, .hw_value = 1, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
35 { .bitrate = 55, .hw_value = 2, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
36 { .bitrate = 110, .hw_value = 3, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
37 { .bitrate = 60, .hw_value = 4, },
38 { .bitrate = 90, .hw_value = 5, },
39 { .bitrate = 120, .hw_value = 6, },
40 { .bitrate = 180, .hw_value = 7, },
41 { .bitrate = 240, .hw_value = 8, },
42 { .bitrate = 360, .hw_value = 9, },
43 { .bitrate = 480, .hw_value = 10, },
44 { .bitrate = 540, .hw_value = 11, },
47 static struct ieee80211_rate p54_arates[] = {
48 { .bitrate = 60, .hw_value = 4, },
49 { .bitrate = 90, .hw_value = 5, },
50 { .bitrate = 120, .hw_value = 6, },
51 { .bitrate = 180, .hw_value = 7, },
52 { .bitrate = 240, .hw_value = 8, },
53 { .bitrate = 360, .hw_value = 9, },
54 { .bitrate = 480, .hw_value = 10, },
55 { .bitrate = 540, .hw_value = 11, },
58 #define CHAN_HAS_CAL BIT(0)
59 #define CHAN_HAS_LIMIT BIT(1)
60 #define CHAN_HAS_CURVE BIT(2)
61 #define CHAN_HAS_ALL (CHAN_HAS_CAL | CHAN_HAS_LIMIT | CHAN_HAS_CURVE)
63 struct p54_channel_entry {
67 enum ieee80211_band band;
70 struct p54_channel_list {
71 struct p54_channel_entry *channels;
74 size_t band_channel_num[IEEE80211_NUM_BANDS];
77 static int p54_get_band_from_freq(u16 freq)
79 /* FIXME: sync these values with the 802.11 spec */
81 if ((freq >= 2412) && (freq <= 2484))
82 return IEEE80211_BAND_2GHZ;
84 if ((freq >= 4920) && (freq <= 5825))
85 return IEEE80211_BAND_5GHZ;
90 static int p54_compare_channels(const void *_a,
93 const struct p54_channel_entry *a = _a;
94 const struct p54_channel_entry *b = _b;
96 return a->index - b->index;
99 static int p54_fill_band_bitrates(struct ieee80211_hw *dev,
100 struct ieee80211_supported_band *band_entry,
101 enum ieee80211_band band)
103 /* TODO: generate rate array dynamically */
106 case IEEE80211_BAND_2GHZ:
107 band_entry->bitrates = p54_bgrates;
108 band_entry->n_bitrates = ARRAY_SIZE(p54_bgrates);
110 case IEEE80211_BAND_5GHZ:
111 band_entry->bitrates = p54_arates;
112 band_entry->n_bitrates = ARRAY_SIZE(p54_arates);
121 static int p54_generate_band(struct ieee80211_hw *dev,
122 struct p54_channel_list *list,
123 enum ieee80211_band band)
125 struct p54_common *priv = dev->priv;
126 struct ieee80211_supported_band *tmp, *old;
130 if ((!list->entries) || (!list->band_channel_num[band]))
133 tmp = kzalloc(sizeof(*tmp), GFP_KERNEL);
137 tmp->channels = kzalloc(sizeof(struct ieee80211_channel) *
138 list->band_channel_num[band], GFP_KERNEL);
142 ret = p54_fill_band_bitrates(dev, tmp, band);
146 for (i = 0, j = 0; (j < list->band_channel_num[band]) &&
147 (i < list->entries); i++) {
149 if (list->channels[i].band != band)
152 if (list->channels[i].data != CHAN_HAS_ALL) {
153 wiphy_err(dev->wiphy,
154 "%s%s%s is/are missing for channel:%d [%d MHz].\n",
155 (list->channels[i].data & CHAN_HAS_CAL ? "" :
156 " [iqauto calibration data]"),
157 (list->channels[i].data & CHAN_HAS_LIMIT ? "" :
158 " [output power limits]"),
159 (list->channels[i].data & CHAN_HAS_CURVE ? "" :
161 list->channels[i].index, list->channels[i].freq);
165 tmp->channels[j].band = list->channels[i].band;
166 tmp->channels[j].center_freq = list->channels[i].freq;
171 wiphy_err(dev->wiphy, "Disabling totally damaged %d GHz band\n",
172 (band == IEEE80211_BAND_2GHZ) ? 2 : 5);
179 old = priv->band_table[band];
180 priv->band_table[band] = tmp;
182 kfree(old->channels);
190 kfree(tmp->channels);
197 static void p54_update_channel_param(struct p54_channel_list *list,
203 * usually all lists in the eeprom are mostly sorted.
204 * so it's very likely that the entry we are looking for
205 * is right at the end of the list
207 for (i = list->entries; i >= 0; i--) {
208 if (freq == list->channels[i].freq) {
209 list->channels[i].data |= data;
214 if ((i < 0) && (list->entries < list->max_entries)) {
215 /* entry does not exist yet. Initialize a new one. */
216 band = p54_get_band_from_freq(freq);
219 * filter out frequencies which don't belong into
220 * any supported band.
226 list->band_channel_num[band]++;
228 list->channels[i].freq = freq;
229 list->channels[i].data = data;
230 list->channels[i].band = band;
231 list->channels[i].index = ieee80211_frequency_to_channel(freq);
232 /* TODO: parse output_limit and fill max_power */
236 static int p54_generate_channel_lists(struct ieee80211_hw *dev)
238 struct p54_common *priv = dev->priv;
239 struct p54_channel_list *list;
240 unsigned int i, j, max_channel_num;
244 if ((priv->iq_autocal_len != priv->curve_data->entries) ||
245 (priv->iq_autocal_len != priv->output_limit->entries))
246 wiphy_err(dev->wiphy,
247 "Unsupported or damaged EEPROM detected. "
248 "You may not be able to use all channels.\n");
250 max_channel_num = max_t(unsigned int, priv->output_limit->entries,
251 priv->iq_autocal_len);
252 max_channel_num = max_t(unsigned int, max_channel_num,
253 priv->curve_data->entries);
255 list = kzalloc(sizeof(*list), GFP_KERNEL);
261 list->max_entries = max_channel_num;
262 list->channels = kzalloc(sizeof(struct p54_channel_entry) *
263 max_channel_num, GFP_KERNEL);
267 for (i = 0; i < max_channel_num; i++) {
268 if (i < priv->iq_autocal_len) {
269 freq = le16_to_cpu(priv->iq_autocal[i].freq);
270 p54_update_channel_param(list, freq, CHAN_HAS_CAL);
273 if (i < priv->output_limit->entries) {
274 freq = le16_to_cpup((__le16 *) (i *
275 priv->output_limit->entry_size +
276 priv->output_limit->offset +
277 priv->output_limit->data));
279 p54_update_channel_param(list, freq, CHAN_HAS_LIMIT);
282 if (i < priv->curve_data->entries) {
283 freq = le16_to_cpup((__le16 *) (i *
284 priv->curve_data->entry_size +
285 priv->curve_data->offset +
286 priv->curve_data->data));
288 p54_update_channel_param(list, freq, CHAN_HAS_CURVE);
292 /* sort the list by the channel index */
293 sort(list->channels, list->entries, sizeof(struct p54_channel_entry),
294 p54_compare_channels, NULL);
296 for (i = 0, j = 0; i < IEEE80211_NUM_BANDS; i++) {
297 if (p54_generate_band(dev, list, i) == 0)
301 /* no useable band available. */
307 kfree(list->channels);
314 static int p54_convert_rev0(struct ieee80211_hw *dev,
315 struct pda_pa_curve_data *curve_data)
317 struct p54_common *priv = dev->priv;
318 struct p54_pa_curve_data_sample *dst;
319 struct pda_pa_curve_data_sample_rev0 *src;
320 size_t cd_len = sizeof(*curve_data) +
321 (curve_data->points_per_channel*sizeof(*dst) + 2) *
322 curve_data->channels;
324 void *source, *target;
326 priv->curve_data = kmalloc(sizeof(*priv->curve_data) + cd_len,
328 if (!priv->curve_data)
331 priv->curve_data->entries = curve_data->channels;
332 priv->curve_data->entry_size = sizeof(__le16) +
333 sizeof(*dst) * curve_data->points_per_channel;
334 priv->curve_data->offset = offsetof(struct pda_pa_curve_data, data);
335 priv->curve_data->len = cd_len;
336 memcpy(priv->curve_data->data, curve_data, sizeof(*curve_data));
337 source = curve_data->data;
338 target = ((struct pda_pa_curve_data *) priv->curve_data->data)->data;
339 for (i = 0; i < curve_data->channels; i++) {
340 __le16 *freq = source;
341 source += sizeof(__le16);
342 *((__le16 *)target) = *freq;
343 target += sizeof(__le16);
344 for (j = 0; j < curve_data->points_per_channel; j++) {
348 dst->rf_power = src->rf_power;
349 dst->pa_detector = src->pa_detector;
350 dst->data_64qam = src->pcv;
351 /* "invent" the points for the other modulations */
352 #define SUB(x, y) (u8)(((x) - (y)) > (x) ? 0 : (x) - (y))
353 dst->data_16qam = SUB(src->pcv, 12);
354 dst->data_qpsk = SUB(dst->data_16qam, 12);
355 dst->data_bpsk = SUB(dst->data_qpsk, 12);
356 dst->data_barker = SUB(dst->data_bpsk, 14);
358 target += sizeof(*dst);
359 source += sizeof(*src);
366 static int p54_convert_rev1(struct ieee80211_hw *dev,
367 struct pda_pa_curve_data *curve_data)
369 struct p54_common *priv = dev->priv;
370 struct p54_pa_curve_data_sample *dst;
371 struct pda_pa_curve_data_sample_rev1 *src;
372 size_t cd_len = sizeof(*curve_data) +
373 (curve_data->points_per_channel*sizeof(*dst) + 2) *
374 curve_data->channels;
376 void *source, *target;
378 priv->curve_data = kzalloc(cd_len + sizeof(*priv->curve_data),
380 if (!priv->curve_data)
383 priv->curve_data->entries = curve_data->channels;
384 priv->curve_data->entry_size = sizeof(__le16) +
385 sizeof(*dst) * curve_data->points_per_channel;
386 priv->curve_data->offset = offsetof(struct pda_pa_curve_data, data);
387 priv->curve_data->len = cd_len;
388 memcpy(priv->curve_data->data, curve_data, sizeof(*curve_data));
389 source = curve_data->data;
390 target = ((struct pda_pa_curve_data *) priv->curve_data->data)->data;
391 for (i = 0; i < curve_data->channels; i++) {
392 __le16 *freq = source;
393 source += sizeof(__le16);
394 *((__le16 *)target) = *freq;
395 target += sizeof(__le16);
396 for (j = 0; j < curve_data->points_per_channel; j++) {
397 memcpy(target, source, sizeof(*src));
399 target += sizeof(*dst);
400 source += sizeof(*src);
408 static const char *p54_rf_chips[] = { "INVALID-0", "Duette3", "Duette2",
409 "Frisbee", "Xbow", "Longbow", "INVALID-6", "INVALID-7" };
411 static void p54_parse_rssical(struct ieee80211_hw *dev, void *data, int len,
414 struct p54_common *priv = dev->priv;
415 int offset = (type == PDR_RSSI_LINEAR_APPROXIMATION_EXTENDED) ? 2 : 0;
416 int entry_size = sizeof(struct pda_rssi_cal_entry) + offset;
417 int num_entries = (type == PDR_RSSI_LINEAR_APPROXIMATION) ? 1 : 2;
420 if (len != (entry_size * num_entries)) {
421 wiphy_err(dev->wiphy,
422 "unknown rssi calibration data packing type:(%x) len:%d.\n",
425 print_hex_dump_bytes("rssical:", DUMP_PREFIX_NONE,
428 wiphy_err(dev->wiphy, "please report this issue.\n");
432 for (i = 0; i < num_entries; i++) {
433 struct pda_rssi_cal_entry *cal = data +
434 (offset + i * entry_size);
435 priv->rssical_db[i].mul = (s16) le16_to_cpu(cal->mul);
436 priv->rssical_db[i].add = (s16) le16_to_cpu(cal->add);
440 static void p54_parse_default_country(struct ieee80211_hw *dev,
443 struct pda_country *country;
445 if (len != sizeof(*country)) {
446 wiphy_err(dev->wiphy,
447 "found possible invalid default country eeprom entry. (entry size: %d)\n",
450 print_hex_dump_bytes("country:", DUMP_PREFIX_NONE,
453 wiphy_err(dev->wiphy, "please report this issue.\n");
457 country = (struct pda_country *) data;
458 if (country->flags == PDR_COUNTRY_CERT_CODE_PSEUDO)
459 regulatory_hint(dev->wiphy, country->alpha2);
462 * write a shared/common function that converts
463 * "Regulatory domain codes" (802.11-2007 14.8.2.2)
464 * into ISO/IEC 3166-1 alpha2 for regulatory_hint.
469 static int p54_convert_output_limits(struct ieee80211_hw *dev,
470 u8 *data, size_t len)
472 struct p54_common *priv = dev->priv;
478 wiphy_err(dev->wiphy, "unknown output power db revision:%x\n",
483 if (2 + data[1] * sizeof(struct pda_channel_output_limit) > len)
486 priv->output_limit = kmalloc(data[1] *
487 sizeof(struct pda_channel_output_limit) +
488 sizeof(*priv->output_limit), GFP_KERNEL);
490 if (!priv->output_limit)
493 priv->output_limit->offset = 0;
494 priv->output_limit->entries = data[1];
495 priv->output_limit->entry_size =
496 sizeof(struct pda_channel_output_limit);
497 priv->output_limit->len = priv->output_limit->entry_size *
498 priv->output_limit->entries +
499 priv->output_limit->offset;
501 memcpy(priv->output_limit->data, &data[2],
502 data[1] * sizeof(struct pda_channel_output_limit));
507 static struct p54_cal_database *p54_convert_db(struct pda_custom_wrapper *src,
510 struct p54_cal_database *dst;
511 size_t payload_len, entries, entry_size, offset;
513 payload_len = le16_to_cpu(src->len);
514 entries = le16_to_cpu(src->entries);
515 entry_size = le16_to_cpu(src->entry_size);
516 offset = le16_to_cpu(src->offset);
517 if (((entries * entry_size + offset) != payload_len) ||
518 (payload_len + sizeof(*src) != total_len))
521 dst = kmalloc(sizeof(*dst) + payload_len, GFP_KERNEL);
525 dst->entries = entries;
526 dst->entry_size = entry_size;
527 dst->offset = offset;
528 dst->len = payload_len;
530 memcpy(dst->data, src->data, payload_len);
534 int p54_parse_eeprom(struct ieee80211_hw *dev, void *eeprom, int len)
536 struct p54_common *priv = dev->priv;
537 struct eeprom_pda_wrap *wrap;
538 struct pda_entry *entry;
539 unsigned int data_len, entry_len;
542 u8 *end = (u8 *)eeprom + len;
546 wrap = (struct eeprom_pda_wrap *) eeprom;
547 entry = (void *)wrap->data + le16_to_cpu(wrap->len);
549 /* verify that at least the entry length/code fits */
550 while ((u8 *)entry <= end - sizeof(*entry)) {
551 entry_len = le16_to_cpu(entry->len);
552 data_len = ((entry_len - 1) << 1);
554 /* abort if entry exceeds whole structure */
555 if ((u8 *)entry + sizeof(*entry) + data_len > end)
558 switch (le16_to_cpu(entry->code)) {
559 case PDR_MAC_ADDRESS:
560 if (data_len != ETH_ALEN)
562 SET_IEEE80211_PERM_ADDR(dev, entry->data);
564 case PDR_PRISM_PA_CAL_OUTPUT_POWER_LIMITS:
565 if (priv->output_limit)
567 err = p54_convert_output_limits(dev, entry->data,
572 case PDR_PRISM_PA_CAL_CURVE_DATA: {
573 struct pda_pa_curve_data *curve_data =
574 (struct pda_pa_curve_data *)entry->data;
575 if (data_len < sizeof(*curve_data)) {
580 switch (curve_data->cal_method_rev) {
582 err = p54_convert_rev0(dev, curve_data);
585 err = p54_convert_rev1(dev, curve_data);
588 wiphy_err(dev->wiphy,
589 "unknown curve data revision %d\n",
590 curve_data->cal_method_rev);
598 case PDR_PRISM_ZIF_TX_IQ_CALIBRATION:
599 priv->iq_autocal = kmemdup(entry->data, data_len,
601 if (!priv->iq_autocal) {
606 priv->iq_autocal_len = data_len / sizeof(struct pda_iq_autocal_entry);
608 case PDR_DEFAULT_COUNTRY:
609 p54_parse_default_country(dev, entry->data, data_len);
611 case PDR_INTERFACE_LIST:
613 while ((u8 *)tmp < entry->data + data_len) {
614 struct exp_if *exp_if = tmp;
615 if (exp_if->if_id == cpu_to_le16(IF_ID_ISL39000))
616 synth = le16_to_cpu(exp_if->variant);
617 tmp += sizeof(*exp_if);
620 case PDR_HARDWARE_PLATFORM_COMPONENT_ID:
623 priv->version = *(u8 *)(entry->data + 1);
625 case PDR_RSSI_LINEAR_APPROXIMATION:
626 case PDR_RSSI_LINEAR_APPROXIMATION_DUAL_BAND:
627 case PDR_RSSI_LINEAR_APPROXIMATION_EXTENDED:
628 p54_parse_rssical(dev, entry->data, data_len,
629 le16_to_cpu(entry->code));
631 case PDR_RSSI_LINEAR_APPROXIMATION_CUSTOM: {
632 __le16 *src = (void *) entry->data;
633 s16 *dst = (void *) &priv->rssical_db;
636 if (data_len != sizeof(priv->rssical_db)) {
640 for (i = 0; i < sizeof(priv->rssical_db) /
642 *(dst++) = (s16) le16_to_cpu(*(src++));
645 case PDR_PRISM_PA_CAL_OUTPUT_POWER_LIMITS_CUSTOM: {
646 struct pda_custom_wrapper *pda = (void *) entry->data;
647 if (priv->output_limit || data_len < sizeof(*pda))
649 priv->output_limit = p54_convert_db(pda, data_len);
652 case PDR_PRISM_PA_CAL_CURVE_DATA_CUSTOM: {
653 struct pda_custom_wrapper *pda = (void *) entry->data;
654 if (priv->curve_data || data_len < sizeof(*pda))
656 priv->curve_data = p54_convert_db(pda, data_len);
660 crc16 = ~crc_ccitt(crc16, (u8 *) entry, sizeof(*entry));
661 if (crc16 != le16_to_cpup((__le16 *)entry->data)) {
662 wiphy_err(dev->wiphy, "eeprom failed checksum "
674 crc16 = crc_ccitt(crc16, (u8 *)entry, (entry_len + 1) * 2);
675 entry = (void *)entry + (entry_len + 1) * 2;
678 wiphy_err(dev->wiphy, "unexpected end of eeprom data.\n");
683 if (!synth || !priv->iq_autocal || !priv->output_limit ||
685 wiphy_err(dev->wiphy,
686 "not all required entries found in eeprom!\n");
691 err = p54_generate_channel_lists(dev);
695 priv->rxhw = synth & PDR_SYNTH_FRONTEND_MASK;
696 if (priv->rxhw == PDR_SYNTH_FRONTEND_XBOW)
697 p54_init_xbow_synth(priv);
698 if (!(synth & PDR_SYNTH_24_GHZ_DISABLED))
699 dev->wiphy->bands[IEEE80211_BAND_2GHZ] =
700 priv->band_table[IEEE80211_BAND_2GHZ];
701 if (!(synth & PDR_SYNTH_5_GHZ_DISABLED))
702 dev->wiphy->bands[IEEE80211_BAND_5GHZ] =
703 priv->band_table[IEEE80211_BAND_5GHZ];
704 if ((synth & PDR_SYNTH_RX_DIV_MASK) == PDR_SYNTH_RX_DIV_SUPPORTED)
705 priv->rx_diversity_mask = 3;
706 if ((synth & PDR_SYNTH_TX_DIV_MASK) == PDR_SYNTH_TX_DIV_SUPPORTED)
707 priv->tx_diversity_mask = 3;
709 if (!is_valid_ether_addr(dev->wiphy->perm_addr)) {
710 u8 perm_addr[ETH_ALEN];
712 wiphy_warn(dev->wiphy,
713 "Invalid hwaddr! Using randomly generated MAC addr\n");
714 random_ether_addr(perm_addr);
715 SET_IEEE80211_PERM_ADDR(dev, perm_addr);
718 wiphy_info(dev->wiphy, "hwaddr %pM, MAC:isl38%02x RF:%s\n",
719 dev->wiphy->perm_addr, priv->version,
720 p54_rf_chips[priv->rxhw]);
725 kfree(priv->iq_autocal);
726 kfree(priv->output_limit);
727 kfree(priv->curve_data);
728 priv->iq_autocal = NULL;
729 priv->output_limit = NULL;
730 priv->curve_data = NULL;
732 wiphy_err(dev->wiphy, "eeprom parse failed!\n");
735 EXPORT_SYMBOL_GPL(p54_parse_eeprom);
737 int p54_read_eeprom(struct ieee80211_hw *dev)
739 struct p54_common *priv = dev->priv;
740 size_t eeprom_size = 0x2020, offset = 0, blocksize, maxblocksize;
744 maxblocksize = EEPROM_READBACK_LEN;
745 if (priv->fw_var >= 0x509)
750 eeprom = kzalloc(eeprom_size, GFP_KERNEL);
751 if (unlikely(!eeprom))
754 while (eeprom_size) {
755 blocksize = min(eeprom_size, maxblocksize);
756 ret = p54_download_eeprom(priv, (void *) (eeprom + offset),
762 eeprom_size -= blocksize;
765 ret = p54_parse_eeprom(dev, eeprom, offset);
770 EXPORT_SYMBOL_GPL(p54_read_eeprom);