| Christian Lamparter | 4c8a32f | 2009-06-23 10:36:26 -0500 | [diff] [blame] | 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> | 
| Christian Lamparter | 1a9b667 | 2009-07-11 01:22:26 +0200 | [diff] [blame] | 22 | #include <linux/sort.h> | 
| Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 23 | #include <linux/slab.h> | 
| Christian Lamparter | 4c8a32f | 2009-06-23 10:36:26 -0500 | [diff] [blame] | 24 |  | 
|  | 25 | #include <net/mac80211.h> | 
|  | 26 |  | 
|  | 27 | #include "p54.h" | 
|  | 28 | #include "eeprom.h" | 
|  | 29 | #include "lmac.h" | 
|  | 30 |  | 
|  | 31 | static struct ieee80211_rate p54_bgrates[] = { | 
|  | 32 | { .bitrate = 10, .hw_value = 0, }, | 
|  | 33 | { .bitrate = 20, .hw_value = 1, .flags = IEEE80211_RATE_SHORT_PREAMBLE }, | 
|  | 34 | { .bitrate = 55, .hw_value = 2, .flags = IEEE80211_RATE_SHORT_PREAMBLE }, | 
|  | 35 | { .bitrate = 110, .hw_value = 3, .flags = IEEE80211_RATE_SHORT_PREAMBLE }, | 
|  | 36 | { .bitrate = 60, .hw_value = 4, }, | 
|  | 37 | { .bitrate = 90, .hw_value = 5, }, | 
|  | 38 | { .bitrate = 120, .hw_value = 6, }, | 
|  | 39 | { .bitrate = 180, .hw_value = 7, }, | 
|  | 40 | { .bitrate = 240, .hw_value = 8, }, | 
|  | 41 | { .bitrate = 360, .hw_value = 9, }, | 
|  | 42 | { .bitrate = 480, .hw_value = 10, }, | 
|  | 43 | { .bitrate = 540, .hw_value = 11, }, | 
|  | 44 | }; | 
|  | 45 |  | 
| Christian Lamparter | 4c8a32f | 2009-06-23 10:36:26 -0500 | [diff] [blame] | 46 | static struct ieee80211_rate p54_arates[] = { | 
|  | 47 | { .bitrate = 60, .hw_value = 4, }, | 
|  | 48 | { .bitrate = 90, .hw_value = 5, }, | 
|  | 49 | { .bitrate = 120, .hw_value = 6, }, | 
|  | 50 | { .bitrate = 180, .hw_value = 7, }, | 
|  | 51 | { .bitrate = 240, .hw_value = 8, }, | 
|  | 52 | { .bitrate = 360, .hw_value = 9, }, | 
|  | 53 | { .bitrate = 480, .hw_value = 10, }, | 
|  | 54 | { .bitrate = 540, .hw_value = 11, }, | 
|  | 55 | }; | 
|  | 56 |  | 
| Christian Lamparter | 1a9b667 | 2009-07-11 01:22:26 +0200 | [diff] [blame] | 57 | #define CHAN_HAS_CAL		BIT(0) | 
|  | 58 | #define CHAN_HAS_LIMIT		BIT(1) | 
|  | 59 | #define CHAN_HAS_CURVE		BIT(2) | 
|  | 60 | #define CHAN_HAS_ALL		(CHAN_HAS_CAL | CHAN_HAS_LIMIT | CHAN_HAS_CURVE) | 
|  | 61 |  | 
|  | 62 | struct p54_channel_entry { | 
|  | 63 | u16 freq; | 
|  | 64 | u16 data; | 
|  | 65 | int index; | 
|  | 66 | enum ieee80211_band band; | 
| Christian Lamparter | 4c8a32f | 2009-06-23 10:36:26 -0500 | [diff] [blame] | 67 | }; | 
|  | 68 |  | 
| Christian Lamparter | 1a9b667 | 2009-07-11 01:22:26 +0200 | [diff] [blame] | 69 | struct p54_channel_list { | 
|  | 70 | struct p54_channel_entry *channels; | 
|  | 71 | size_t entries; | 
|  | 72 | size_t max_entries; | 
|  | 73 | size_t band_channel_num[IEEE80211_NUM_BANDS]; | 
| Christian Lamparter | 4c8a32f | 2009-06-23 10:36:26 -0500 | [diff] [blame] | 74 | }; | 
|  | 75 |  | 
| Christian Lamparter | 1a9b667 | 2009-07-11 01:22:26 +0200 | [diff] [blame] | 76 | static int p54_get_band_from_freq(u16 freq) | 
|  | 77 | { | 
|  | 78 | /* FIXME: sync these values with the 802.11 spec */ | 
|  | 79 |  | 
|  | 80 | if ((freq >= 2412) && (freq <= 2484)) | 
|  | 81 | return IEEE80211_BAND_2GHZ; | 
|  | 82 |  | 
|  | 83 | if ((freq >= 4920) && (freq <= 5825)) | 
|  | 84 | return IEEE80211_BAND_5GHZ; | 
|  | 85 |  | 
|  | 86 | return -1; | 
|  | 87 | } | 
|  | 88 |  | 
|  | 89 | static int p54_compare_channels(const void *_a, | 
|  | 90 | const void *_b) | 
|  | 91 | { | 
|  | 92 | const struct p54_channel_entry *a = _a; | 
|  | 93 | const struct p54_channel_entry *b = _b; | 
|  | 94 |  | 
|  | 95 | return a->index - b->index; | 
|  | 96 | } | 
|  | 97 |  | 
|  | 98 | static int p54_fill_band_bitrates(struct ieee80211_hw *dev, | 
|  | 99 | struct ieee80211_supported_band *band_entry, | 
|  | 100 | enum ieee80211_band band) | 
|  | 101 | { | 
|  | 102 | /* TODO: generate rate array dynamically */ | 
|  | 103 |  | 
|  | 104 | switch (band) { | 
|  | 105 | case IEEE80211_BAND_2GHZ: | 
|  | 106 | band_entry->bitrates = p54_bgrates; | 
|  | 107 | band_entry->n_bitrates = ARRAY_SIZE(p54_bgrates); | 
|  | 108 | break; | 
|  | 109 | case IEEE80211_BAND_5GHZ: | 
|  | 110 | band_entry->bitrates = p54_arates; | 
|  | 111 | band_entry->n_bitrates = ARRAY_SIZE(p54_arates); | 
|  | 112 | break; | 
|  | 113 | default: | 
|  | 114 | return -EINVAL; | 
|  | 115 | } | 
|  | 116 |  | 
|  | 117 | return 0; | 
|  | 118 | } | 
|  | 119 |  | 
|  | 120 | static int p54_generate_band(struct ieee80211_hw *dev, | 
|  | 121 | struct p54_channel_list *list, | 
|  | 122 | enum ieee80211_band band) | 
|  | 123 | { | 
|  | 124 | struct p54_common *priv = dev->priv; | 
|  | 125 | struct ieee80211_supported_band *tmp, *old; | 
|  | 126 | unsigned int i, j; | 
|  | 127 | int ret = -ENOMEM; | 
|  | 128 |  | 
|  | 129 | if ((!list->entries) || (!list->band_channel_num[band])) | 
| Christian Lamparter | 93a59d7 | 2009-10-31 22:59:27 +0100 | [diff] [blame] | 130 | return -EINVAL; | 
| Christian Lamparter | 1a9b667 | 2009-07-11 01:22:26 +0200 | [diff] [blame] | 131 |  | 
|  | 132 | tmp = kzalloc(sizeof(*tmp), GFP_KERNEL); | 
|  | 133 | if (!tmp) | 
|  | 134 | goto err_out; | 
|  | 135 |  | 
|  | 136 | tmp->channels = kzalloc(sizeof(struct ieee80211_channel) * | 
|  | 137 | list->band_channel_num[band], GFP_KERNEL); | 
|  | 138 | if (!tmp->channels) | 
|  | 139 | goto err_out; | 
|  | 140 |  | 
|  | 141 | ret = p54_fill_band_bitrates(dev, tmp, band); | 
|  | 142 | if (ret) | 
|  | 143 | goto err_out; | 
|  | 144 |  | 
|  | 145 | for (i = 0, j = 0; (j < list->band_channel_num[band]) && | 
|  | 146 | (i < list->entries); i++) { | 
|  | 147 |  | 
|  | 148 | if (list->channels[i].band != band) | 
|  | 149 | continue; | 
|  | 150 |  | 
|  | 151 | if (list->channels[i].data != CHAN_HAS_ALL) { | 
|  | 152 | printk(KERN_ERR "%s:%s%s%s is/are missing for " | 
|  | 153 | "channel:%d [%d MHz].\n", | 
|  | 154 | wiphy_name(dev->wiphy), | 
|  | 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 ? "" : | 
|  | 160 | " [curve data]"), | 
|  | 161 | list->channels[i].index, list->channels[i].freq); | 
| Christian Lamparter | 93a59d7 | 2009-10-31 22:59:27 +0100 | [diff] [blame] | 162 | continue; | 
| Christian Lamparter | 1a9b667 | 2009-07-11 01:22:26 +0200 | [diff] [blame] | 163 | } | 
|  | 164 |  | 
|  | 165 | tmp->channels[j].band = list->channels[i].band; | 
|  | 166 | tmp->channels[j].center_freq = list->channels[i].freq; | 
|  | 167 | j++; | 
|  | 168 | } | 
|  | 169 |  | 
| Christian Lamparter | 93a59d7 | 2009-10-31 22:59:27 +0100 | [diff] [blame] | 170 | if (j == 0) { | 
|  | 171 | printk(KERN_ERR "%s: Disabling totally damaged %s band.\n", | 
|  | 172 | wiphy_name(dev->wiphy), (band == IEEE80211_BAND_2GHZ) ? | 
|  | 173 | "2 GHz" : "5 GHz"); | 
|  | 174 |  | 
|  | 175 | ret = -ENODATA; | 
|  | 176 | goto err_out; | 
|  | 177 | } | 
|  | 178 |  | 
|  | 179 | tmp->n_channels = j; | 
| Christian Lamparter | 1a9b667 | 2009-07-11 01:22:26 +0200 | [diff] [blame] | 180 | old = priv->band_table[band]; | 
|  | 181 | priv->band_table[band] = tmp; | 
|  | 182 | if (old) { | 
|  | 183 | kfree(old->channels); | 
|  | 184 | kfree(old); | 
|  | 185 | } | 
|  | 186 |  | 
|  | 187 | return 0; | 
|  | 188 |  | 
|  | 189 | err_out: | 
|  | 190 | if (tmp) { | 
|  | 191 | kfree(tmp->channels); | 
|  | 192 | kfree(tmp); | 
|  | 193 | } | 
|  | 194 |  | 
|  | 195 | return ret; | 
|  | 196 | } | 
|  | 197 |  | 
|  | 198 | static void p54_update_channel_param(struct p54_channel_list *list, | 
|  | 199 | u16 freq, u16 data) | 
|  | 200 | { | 
|  | 201 | int band, i; | 
|  | 202 |  | 
|  | 203 | /* | 
|  | 204 | * usually all lists in the eeprom are mostly sorted. | 
|  | 205 | * so it's very likely that the entry we are looking for | 
|  | 206 | * is right at the end of the list | 
|  | 207 | */ | 
|  | 208 | for (i = list->entries; i >= 0; i--) { | 
|  | 209 | if (freq == list->channels[i].freq) { | 
|  | 210 | list->channels[i].data |= data; | 
|  | 211 | break; | 
|  | 212 | } | 
|  | 213 | } | 
|  | 214 |  | 
|  | 215 | if ((i < 0) && (list->entries < list->max_entries)) { | 
|  | 216 | /* entry does not exist yet. Initialize a new one. */ | 
|  | 217 | band = p54_get_band_from_freq(freq); | 
|  | 218 |  | 
|  | 219 | /* | 
|  | 220 | * filter out frequencies which don't belong into | 
|  | 221 | * any supported band. | 
|  | 222 | */ | 
|  | 223 | if (band < 0) | 
|  | 224 | return ; | 
|  | 225 |  | 
|  | 226 | i = list->entries++; | 
|  | 227 | list->band_channel_num[band]++; | 
|  | 228 |  | 
|  | 229 | list->channels[i].freq = freq; | 
|  | 230 | list->channels[i].data = data; | 
|  | 231 | list->channels[i].band = band; | 
|  | 232 | list->channels[i].index = ieee80211_frequency_to_channel(freq); | 
|  | 233 | /* TODO: parse output_limit and fill max_power */ | 
|  | 234 | } | 
|  | 235 | } | 
|  | 236 |  | 
|  | 237 | static int p54_generate_channel_lists(struct ieee80211_hw *dev) | 
|  | 238 | { | 
|  | 239 | struct p54_common *priv = dev->priv; | 
|  | 240 | struct p54_channel_list *list; | 
|  | 241 | unsigned int i, j, max_channel_num; | 
| Christian Lamparter | 93a59d7 | 2009-10-31 22:59:27 +0100 | [diff] [blame] | 242 | int ret = 0; | 
| Christian Lamparter | 1a9b667 | 2009-07-11 01:22:26 +0200 | [diff] [blame] | 243 | u16 freq; | 
|  | 244 |  | 
|  | 245 | if ((priv->iq_autocal_len != priv->curve_data->entries) || | 
|  | 246 | (priv->iq_autocal_len != priv->output_limit->entries)) | 
| Christian Lamparter | 93a59d7 | 2009-10-31 22:59:27 +0100 | [diff] [blame] | 247 | printk(KERN_ERR "%s: Unsupported or damaged EEPROM detected. " | 
|  | 248 | "You may not be able to use all channels.\n", | 
| Christian Lamparter | 1a9b667 | 2009-07-11 01:22:26 +0200 | [diff] [blame] | 249 | wiphy_name(dev->wiphy)); | 
|  | 250 |  | 
|  | 251 | max_channel_num = max_t(unsigned int, priv->output_limit->entries, | 
|  | 252 | priv->iq_autocal_len); | 
|  | 253 | max_channel_num = max_t(unsigned int, max_channel_num, | 
|  | 254 | priv->curve_data->entries); | 
|  | 255 |  | 
|  | 256 | list = kzalloc(sizeof(*list), GFP_KERNEL); | 
| Christian Lamparter | 93a59d7 | 2009-10-31 22:59:27 +0100 | [diff] [blame] | 257 | if (!list) { | 
|  | 258 | ret = -ENOMEM; | 
| Christian Lamparter | 1a9b667 | 2009-07-11 01:22:26 +0200 | [diff] [blame] | 259 | goto free; | 
| Christian Lamparter | 93a59d7 | 2009-10-31 22:59:27 +0100 | [diff] [blame] | 260 | } | 
| Christian Lamparter | 1a9b667 | 2009-07-11 01:22:26 +0200 | [diff] [blame] | 261 |  | 
|  | 262 | list->max_entries = max_channel_num; | 
|  | 263 | list->channels = kzalloc(sizeof(struct p54_channel_entry) * | 
|  | 264 | max_channel_num, GFP_KERNEL); | 
|  | 265 | if (!list->channels) | 
|  | 266 | goto free; | 
|  | 267 |  | 
|  | 268 | for (i = 0; i < max_channel_num; i++) { | 
|  | 269 | if (i < priv->iq_autocal_len) { | 
|  | 270 | freq = le16_to_cpu(priv->iq_autocal[i].freq); | 
|  | 271 | p54_update_channel_param(list, freq, CHAN_HAS_CAL); | 
|  | 272 | } | 
|  | 273 |  | 
|  | 274 | if (i < priv->output_limit->entries) { | 
|  | 275 | freq = le16_to_cpup((__le16 *) (i * | 
|  | 276 | priv->output_limit->entry_size + | 
|  | 277 | priv->output_limit->offset + | 
|  | 278 | priv->output_limit->data)); | 
|  | 279 |  | 
|  | 280 | p54_update_channel_param(list, freq, CHAN_HAS_LIMIT); | 
|  | 281 | } | 
|  | 282 |  | 
|  | 283 | if (i < priv->curve_data->entries) { | 
|  | 284 | freq = le16_to_cpup((__le16 *) (i * | 
|  | 285 | priv->curve_data->entry_size + | 
|  | 286 | priv->curve_data->offset + | 
|  | 287 | priv->curve_data->data)); | 
|  | 288 |  | 
|  | 289 | p54_update_channel_param(list, freq, CHAN_HAS_CURVE); | 
|  | 290 | } | 
|  | 291 | } | 
|  | 292 |  | 
|  | 293 | /* sort the list by the channel index */ | 
|  | 294 | sort(list->channels, list->entries, sizeof(struct p54_channel_entry), | 
|  | 295 | p54_compare_channels, NULL); | 
|  | 296 |  | 
|  | 297 | for (i = 0, j = 0; i < IEEE80211_NUM_BANDS; i++) { | 
| Christian Lamparter | 93a59d7 | 2009-10-31 22:59:27 +0100 | [diff] [blame] | 298 | if (p54_generate_band(dev, list, i) == 0) | 
| Christian Lamparter | 1a9b667 | 2009-07-11 01:22:26 +0200 | [diff] [blame] | 299 | j++; | 
| Christian Lamparter | 1a9b667 | 2009-07-11 01:22:26 +0200 | [diff] [blame] | 300 | } | 
|  | 301 | if (j == 0) { | 
|  | 302 | /* no useable band available. */ | 
|  | 303 | ret = -EINVAL; | 
|  | 304 | } | 
|  | 305 |  | 
|  | 306 | free: | 
|  | 307 | if (list) { | 
|  | 308 | kfree(list->channels); | 
|  | 309 | kfree(list); | 
|  | 310 | } | 
|  | 311 |  | 
|  | 312 | return ret; | 
|  | 313 | } | 
|  | 314 |  | 
| Christian Lamparter | 4c8a32f | 2009-06-23 10:36:26 -0500 | [diff] [blame] | 315 | static int p54_convert_rev0(struct ieee80211_hw *dev, | 
|  | 316 | struct pda_pa_curve_data *curve_data) | 
|  | 317 | { | 
|  | 318 | struct p54_common *priv = dev->priv; | 
|  | 319 | struct p54_pa_curve_data_sample *dst; | 
|  | 320 | struct pda_pa_curve_data_sample_rev0 *src; | 
|  | 321 | size_t cd_len = sizeof(*curve_data) + | 
|  | 322 | (curve_data->points_per_channel*sizeof(*dst) + 2) * | 
|  | 323 | curve_data->channels; | 
|  | 324 | unsigned int i, j; | 
|  | 325 | void *source, *target; | 
|  | 326 |  | 
|  | 327 | priv->curve_data = kmalloc(sizeof(*priv->curve_data) + cd_len, | 
|  | 328 | GFP_KERNEL); | 
|  | 329 | if (!priv->curve_data) | 
|  | 330 | return -ENOMEM; | 
|  | 331 |  | 
|  | 332 | priv->curve_data->entries = curve_data->channels; | 
|  | 333 | priv->curve_data->entry_size = sizeof(__le16) + | 
|  | 334 | sizeof(*dst) * curve_data->points_per_channel; | 
|  | 335 | priv->curve_data->offset = offsetof(struct pda_pa_curve_data, data); | 
|  | 336 | priv->curve_data->len = cd_len; | 
|  | 337 | memcpy(priv->curve_data->data, curve_data, sizeof(*curve_data)); | 
|  | 338 | source = curve_data->data; | 
|  | 339 | target = ((struct pda_pa_curve_data *) priv->curve_data->data)->data; | 
|  | 340 | for (i = 0; i < curve_data->channels; i++) { | 
|  | 341 | __le16 *freq = source; | 
|  | 342 | source += sizeof(__le16); | 
|  | 343 | *((__le16 *)target) = *freq; | 
|  | 344 | target += sizeof(__le16); | 
|  | 345 | for (j = 0; j < curve_data->points_per_channel; j++) { | 
|  | 346 | dst = target; | 
|  | 347 | src = source; | 
|  | 348 |  | 
|  | 349 | dst->rf_power = src->rf_power; | 
|  | 350 | dst->pa_detector = src->pa_detector; | 
|  | 351 | dst->data_64qam = src->pcv; | 
|  | 352 | /* "invent" the points for the other modulations */ | 
|  | 353 | #define SUB(x, y) (u8)(((x) - (y)) > (x) ? 0 : (x) - (y)) | 
|  | 354 | dst->data_16qam = SUB(src->pcv, 12); | 
|  | 355 | dst->data_qpsk = SUB(dst->data_16qam, 12); | 
|  | 356 | dst->data_bpsk = SUB(dst->data_qpsk, 12); | 
|  | 357 | dst->data_barker = SUB(dst->data_bpsk, 14); | 
|  | 358 | #undef SUB | 
|  | 359 | target += sizeof(*dst); | 
|  | 360 | source += sizeof(*src); | 
|  | 361 | } | 
|  | 362 | } | 
|  | 363 |  | 
|  | 364 | return 0; | 
|  | 365 | } | 
|  | 366 |  | 
|  | 367 | static int p54_convert_rev1(struct ieee80211_hw *dev, | 
|  | 368 | struct pda_pa_curve_data *curve_data) | 
|  | 369 | { | 
|  | 370 | struct p54_common *priv = dev->priv; | 
|  | 371 | struct p54_pa_curve_data_sample *dst; | 
|  | 372 | struct pda_pa_curve_data_sample_rev1 *src; | 
|  | 373 | size_t cd_len = sizeof(*curve_data) + | 
|  | 374 | (curve_data->points_per_channel*sizeof(*dst) + 2) * | 
|  | 375 | curve_data->channels; | 
|  | 376 | unsigned int i, j; | 
|  | 377 | void *source, *target; | 
|  | 378 |  | 
|  | 379 | priv->curve_data = kzalloc(cd_len + sizeof(*priv->curve_data), | 
|  | 380 | GFP_KERNEL); | 
|  | 381 | if (!priv->curve_data) | 
|  | 382 | return -ENOMEM; | 
|  | 383 |  | 
|  | 384 | priv->curve_data->entries = curve_data->channels; | 
|  | 385 | priv->curve_data->entry_size = sizeof(__le16) + | 
|  | 386 | sizeof(*dst) * curve_data->points_per_channel; | 
|  | 387 | priv->curve_data->offset = offsetof(struct pda_pa_curve_data, data); | 
|  | 388 | priv->curve_data->len = cd_len; | 
|  | 389 | memcpy(priv->curve_data->data, curve_data, sizeof(*curve_data)); | 
|  | 390 | source = curve_data->data; | 
|  | 391 | target = ((struct pda_pa_curve_data *) priv->curve_data->data)->data; | 
|  | 392 | for (i = 0; i < curve_data->channels; i++) { | 
|  | 393 | __le16 *freq = source; | 
|  | 394 | source += sizeof(__le16); | 
|  | 395 | *((__le16 *)target) = *freq; | 
|  | 396 | target += sizeof(__le16); | 
|  | 397 | for (j = 0; j < curve_data->points_per_channel; j++) { | 
|  | 398 | memcpy(target, source, sizeof(*src)); | 
|  | 399 |  | 
|  | 400 | target += sizeof(*dst); | 
|  | 401 | source += sizeof(*src); | 
|  | 402 | } | 
|  | 403 | source++; | 
|  | 404 | } | 
|  | 405 |  | 
|  | 406 | return 0; | 
|  | 407 | } | 
|  | 408 |  | 
|  | 409 | static const char *p54_rf_chips[] = { "INVALID-0", "Duette3", "Duette2", | 
|  | 410 | "Frisbee", "Xbow", "Longbow", "INVALID-6", "INVALID-7" }; | 
|  | 411 |  | 
|  | 412 | static void p54_parse_rssical(struct ieee80211_hw *dev, void *data, int len, | 
|  | 413 | u16 type) | 
|  | 414 | { | 
|  | 415 | struct p54_common *priv = dev->priv; | 
|  | 416 | int offset = (type == PDR_RSSI_LINEAR_APPROXIMATION_EXTENDED) ? 2 : 0; | 
|  | 417 | int entry_size = sizeof(struct pda_rssi_cal_entry) + offset; | 
|  | 418 | int num_entries = (type == PDR_RSSI_LINEAR_APPROXIMATION) ? 1 : 2; | 
|  | 419 | int i; | 
|  | 420 |  | 
|  | 421 | if (len != (entry_size * num_entries)) { | 
|  | 422 | printk(KERN_ERR "%s: unknown rssi calibration data packing " | 
|  | 423 | " type:(%x) len:%d.\n", | 
|  | 424 | wiphy_name(dev->wiphy), type, len); | 
|  | 425 |  | 
|  | 426 | print_hex_dump_bytes("rssical:", DUMP_PREFIX_NONE, | 
|  | 427 | data, len); | 
|  | 428 |  | 
|  | 429 | printk(KERN_ERR "%s: please report this issue.\n", | 
|  | 430 | wiphy_name(dev->wiphy)); | 
|  | 431 | return; | 
|  | 432 | } | 
|  | 433 |  | 
|  | 434 | for (i = 0; i < num_entries; i++) { | 
|  | 435 | struct pda_rssi_cal_entry *cal = data + | 
|  | 436 | (offset + i * entry_size); | 
|  | 437 | priv->rssical_db[i].mul = (s16) le16_to_cpu(cal->mul); | 
|  | 438 | priv->rssical_db[i].add = (s16) le16_to_cpu(cal->add); | 
|  | 439 | } | 
|  | 440 | } | 
|  | 441 |  | 
|  | 442 | static void p54_parse_default_country(struct ieee80211_hw *dev, | 
|  | 443 | void *data, int len) | 
|  | 444 | { | 
|  | 445 | struct pda_country *country; | 
|  | 446 |  | 
|  | 447 | if (len != sizeof(*country)) { | 
|  | 448 | printk(KERN_ERR "%s: found possible invalid default country " | 
|  | 449 | "eeprom entry. (entry size: %d)\n", | 
|  | 450 | wiphy_name(dev->wiphy), len); | 
|  | 451 |  | 
|  | 452 | print_hex_dump_bytes("country:", DUMP_PREFIX_NONE, | 
|  | 453 | data, len); | 
|  | 454 |  | 
|  | 455 | printk(KERN_ERR "%s: please report this issue.\n", | 
|  | 456 | wiphy_name(dev->wiphy)); | 
|  | 457 | return; | 
|  | 458 | } | 
|  | 459 |  | 
|  | 460 | country = (struct pda_country *) data; | 
|  | 461 | if (country->flags == PDR_COUNTRY_CERT_CODE_PSEUDO) | 
|  | 462 | regulatory_hint(dev->wiphy, country->alpha2); | 
|  | 463 | else { | 
|  | 464 | /* TODO: | 
|  | 465 | * write a shared/common function that converts | 
|  | 466 | * "Regulatory domain codes" (802.11-2007 14.8.2.2) | 
|  | 467 | * into ISO/IEC 3166-1 alpha2 for regulatory_hint. | 
|  | 468 | */ | 
|  | 469 | } | 
|  | 470 | } | 
|  | 471 |  | 
|  | 472 | static int p54_convert_output_limits(struct ieee80211_hw *dev, | 
|  | 473 | u8 *data, size_t len) | 
|  | 474 | { | 
|  | 475 | struct p54_common *priv = dev->priv; | 
|  | 476 |  | 
|  | 477 | if (len < 2) | 
|  | 478 | return -EINVAL; | 
|  | 479 |  | 
|  | 480 | if (data[0] != 0) { | 
|  | 481 | printk(KERN_ERR "%s: unknown output power db revision:%x\n", | 
|  | 482 | wiphy_name(dev->wiphy), data[0]); | 
|  | 483 | return -EINVAL; | 
|  | 484 | } | 
|  | 485 |  | 
|  | 486 | if (2 + data[1] * sizeof(struct pda_channel_output_limit) > len) | 
|  | 487 | return -EINVAL; | 
|  | 488 |  | 
|  | 489 | priv->output_limit = kmalloc(data[1] * | 
|  | 490 | sizeof(struct pda_channel_output_limit) + | 
|  | 491 | sizeof(*priv->output_limit), GFP_KERNEL); | 
|  | 492 |  | 
|  | 493 | if (!priv->output_limit) | 
|  | 494 | return -ENOMEM; | 
|  | 495 |  | 
|  | 496 | priv->output_limit->offset = 0; | 
|  | 497 | priv->output_limit->entries = data[1]; | 
|  | 498 | priv->output_limit->entry_size = | 
|  | 499 | sizeof(struct pda_channel_output_limit); | 
|  | 500 | priv->output_limit->len = priv->output_limit->entry_size * | 
|  | 501 | priv->output_limit->entries + | 
|  | 502 | priv->output_limit->offset; | 
|  | 503 |  | 
|  | 504 | memcpy(priv->output_limit->data, &data[2], | 
|  | 505 | data[1] * sizeof(struct pda_channel_output_limit)); | 
|  | 506 |  | 
|  | 507 | return 0; | 
|  | 508 | } | 
|  | 509 |  | 
|  | 510 | static struct p54_cal_database *p54_convert_db(struct pda_custom_wrapper *src, | 
|  | 511 | size_t total_len) | 
|  | 512 | { | 
|  | 513 | struct p54_cal_database *dst; | 
|  | 514 | size_t payload_len, entries, entry_size, offset; | 
|  | 515 |  | 
|  | 516 | payload_len = le16_to_cpu(src->len); | 
|  | 517 | entries = le16_to_cpu(src->entries); | 
|  | 518 | entry_size = le16_to_cpu(src->entry_size); | 
|  | 519 | offset = le16_to_cpu(src->offset); | 
|  | 520 | if (((entries * entry_size + offset) != payload_len) || | 
|  | 521 | (payload_len + sizeof(*src) != total_len)) | 
|  | 522 | return NULL; | 
|  | 523 |  | 
|  | 524 | dst = kmalloc(sizeof(*dst) + payload_len, GFP_KERNEL); | 
|  | 525 | if (!dst) | 
|  | 526 | return NULL; | 
|  | 527 |  | 
|  | 528 | dst->entries = entries; | 
|  | 529 | dst->entry_size = entry_size; | 
|  | 530 | dst->offset = offset; | 
|  | 531 | dst->len = payload_len; | 
|  | 532 |  | 
|  | 533 | memcpy(dst->data, src->data, payload_len); | 
|  | 534 | return dst; | 
|  | 535 | } | 
|  | 536 |  | 
|  | 537 | int p54_parse_eeprom(struct ieee80211_hw *dev, void *eeprom, int len) | 
|  | 538 | { | 
|  | 539 | struct p54_common *priv = dev->priv; | 
| Larry Finger | e6a3f55 | 2009-07-19 21:53:14 -0500 | [diff] [blame] | 540 | struct eeprom_pda_wrap *wrap; | 
| Christian Lamparter | 4c8a32f | 2009-06-23 10:36:26 -0500 | [diff] [blame] | 541 | struct pda_entry *entry; | 
|  | 542 | unsigned int data_len, entry_len; | 
|  | 543 | void *tmp; | 
|  | 544 | int err; | 
|  | 545 | u8 *end = (u8 *)eeprom + len; | 
|  | 546 | u16 synth = 0; | 
|  | 547 |  | 
|  | 548 | wrap = (struct eeprom_pda_wrap *) eeprom; | 
|  | 549 | entry = (void *)wrap->data + le16_to_cpu(wrap->len); | 
|  | 550 |  | 
|  | 551 | /* verify that at least the entry length/code fits */ | 
|  | 552 | while ((u8 *)entry <= end - sizeof(*entry)) { | 
|  | 553 | entry_len = le16_to_cpu(entry->len); | 
|  | 554 | data_len = ((entry_len - 1) << 1); | 
|  | 555 |  | 
|  | 556 | /* abort if entry exceeds whole structure */ | 
|  | 557 | if ((u8 *)entry + sizeof(*entry) + data_len > end) | 
|  | 558 | break; | 
|  | 559 |  | 
|  | 560 | switch (le16_to_cpu(entry->code)) { | 
|  | 561 | case PDR_MAC_ADDRESS: | 
|  | 562 | if (data_len != ETH_ALEN) | 
|  | 563 | break; | 
|  | 564 | SET_IEEE80211_PERM_ADDR(dev, entry->data); | 
|  | 565 | break; | 
|  | 566 | case PDR_PRISM_PA_CAL_OUTPUT_POWER_LIMITS: | 
|  | 567 | if (priv->output_limit) | 
|  | 568 | break; | 
|  | 569 | err = p54_convert_output_limits(dev, entry->data, | 
|  | 570 | data_len); | 
|  | 571 | if (err) | 
|  | 572 | goto err; | 
|  | 573 | break; | 
|  | 574 | case PDR_PRISM_PA_CAL_CURVE_DATA: { | 
|  | 575 | struct pda_pa_curve_data *curve_data = | 
|  | 576 | (struct pda_pa_curve_data *)entry->data; | 
|  | 577 | if (data_len < sizeof(*curve_data)) { | 
|  | 578 | err = -EINVAL; | 
|  | 579 | goto err; | 
|  | 580 | } | 
|  | 581 |  | 
|  | 582 | switch (curve_data->cal_method_rev) { | 
|  | 583 | case 0: | 
|  | 584 | err = p54_convert_rev0(dev, curve_data); | 
|  | 585 | break; | 
|  | 586 | case 1: | 
|  | 587 | err = p54_convert_rev1(dev, curve_data); | 
|  | 588 | break; | 
|  | 589 | default: | 
|  | 590 | printk(KERN_ERR "%s: unknown curve data " | 
|  | 591 | "revision %d\n", | 
|  | 592 | wiphy_name(dev->wiphy), | 
|  | 593 | curve_data->cal_method_rev); | 
|  | 594 | err = -ENODEV; | 
|  | 595 | break; | 
|  | 596 | } | 
|  | 597 | if (err) | 
|  | 598 | goto err; | 
|  | 599 | } | 
|  | 600 | break; | 
|  | 601 | case PDR_PRISM_ZIF_TX_IQ_CALIBRATION: | 
|  | 602 | priv->iq_autocal = kmalloc(data_len, GFP_KERNEL); | 
|  | 603 | if (!priv->iq_autocal) { | 
|  | 604 | err = -ENOMEM; | 
|  | 605 | goto err; | 
|  | 606 | } | 
|  | 607 |  | 
|  | 608 | memcpy(priv->iq_autocal, entry->data, data_len); | 
|  | 609 | priv->iq_autocal_len = data_len / sizeof(struct pda_iq_autocal_entry); | 
|  | 610 | break; | 
|  | 611 | case PDR_DEFAULT_COUNTRY: | 
|  | 612 | p54_parse_default_country(dev, entry->data, data_len); | 
|  | 613 | break; | 
|  | 614 | case PDR_INTERFACE_LIST: | 
|  | 615 | tmp = entry->data; | 
|  | 616 | while ((u8 *)tmp < entry->data + data_len) { | 
|  | 617 | struct exp_if *exp_if = tmp; | 
|  | 618 | if (exp_if->if_id == cpu_to_le16(IF_ID_ISL39000)) | 
|  | 619 | synth = le16_to_cpu(exp_if->variant); | 
|  | 620 | tmp += sizeof(*exp_if); | 
|  | 621 | } | 
|  | 622 | break; | 
|  | 623 | case PDR_HARDWARE_PLATFORM_COMPONENT_ID: | 
|  | 624 | if (data_len < 2) | 
|  | 625 | break; | 
|  | 626 | priv->version = *(u8 *)(entry->data + 1); | 
|  | 627 | break; | 
|  | 628 | case PDR_RSSI_LINEAR_APPROXIMATION: | 
|  | 629 | case PDR_RSSI_LINEAR_APPROXIMATION_DUAL_BAND: | 
|  | 630 | case PDR_RSSI_LINEAR_APPROXIMATION_EXTENDED: | 
|  | 631 | p54_parse_rssical(dev, entry->data, data_len, | 
|  | 632 | le16_to_cpu(entry->code)); | 
|  | 633 | break; | 
|  | 634 | case PDR_RSSI_LINEAR_APPROXIMATION_CUSTOM: { | 
|  | 635 | __le16 *src = (void *) entry->data; | 
|  | 636 | s16 *dst = (void *) &priv->rssical_db; | 
|  | 637 | int i; | 
|  | 638 |  | 
|  | 639 | if (data_len != sizeof(priv->rssical_db)) { | 
|  | 640 | err = -EINVAL; | 
|  | 641 | goto err; | 
|  | 642 | } | 
|  | 643 | for (i = 0; i < sizeof(priv->rssical_db) / | 
|  | 644 | sizeof(*src); i++) | 
|  | 645 | *(dst++) = (s16) le16_to_cpu(*(src++)); | 
|  | 646 | } | 
|  | 647 | break; | 
|  | 648 | case PDR_PRISM_PA_CAL_OUTPUT_POWER_LIMITS_CUSTOM: { | 
|  | 649 | struct pda_custom_wrapper *pda = (void *) entry->data; | 
|  | 650 | if (priv->output_limit || data_len < sizeof(*pda)) | 
|  | 651 | break; | 
|  | 652 | priv->output_limit = p54_convert_db(pda, data_len); | 
|  | 653 | } | 
|  | 654 | break; | 
|  | 655 | case PDR_PRISM_PA_CAL_CURVE_DATA_CUSTOM: { | 
|  | 656 | struct pda_custom_wrapper *pda = (void *) entry->data; | 
|  | 657 | if (priv->curve_data || data_len < sizeof(*pda)) | 
|  | 658 | break; | 
|  | 659 | priv->curve_data = p54_convert_db(pda, data_len); | 
|  | 660 | } | 
|  | 661 | break; | 
|  | 662 | case PDR_END: | 
|  | 663 | /* make it overrun */ | 
|  | 664 | entry_len = len; | 
|  | 665 | break; | 
|  | 666 | default: | 
|  | 667 | break; | 
|  | 668 | } | 
|  | 669 |  | 
|  | 670 | entry = (void *)entry + (entry_len + 1)*2; | 
|  | 671 | } | 
|  | 672 |  | 
|  | 673 | if (!synth || !priv->iq_autocal || !priv->output_limit || | 
|  | 674 | !priv->curve_data) { | 
|  | 675 | printk(KERN_ERR "%s: not all required entries found in eeprom!\n", | 
|  | 676 | wiphy_name(dev->wiphy)); | 
|  | 677 | err = -EINVAL; | 
|  | 678 | goto err; | 
|  | 679 | } | 
|  | 680 |  | 
| Christian Lamparter | 1a9b667 | 2009-07-11 01:22:26 +0200 | [diff] [blame] | 681 | err = p54_generate_channel_lists(dev); | 
|  | 682 | if (err) | 
|  | 683 | goto err; | 
|  | 684 |  | 
| Christian Lamparter | 4c8a32f | 2009-06-23 10:36:26 -0500 | [diff] [blame] | 685 | priv->rxhw = synth & PDR_SYNTH_FRONTEND_MASK; | 
|  | 686 | if (priv->rxhw == PDR_SYNTH_FRONTEND_XBOW) | 
|  | 687 | p54_init_xbow_synth(priv); | 
|  | 688 | if (!(synth & PDR_SYNTH_24_GHZ_DISABLED)) | 
| Christian Lamparter | 1a9b667 | 2009-07-11 01:22:26 +0200 | [diff] [blame] | 689 | dev->wiphy->bands[IEEE80211_BAND_2GHZ] = | 
|  | 690 | priv->band_table[IEEE80211_BAND_2GHZ]; | 
| Christian Lamparter | 4c8a32f | 2009-06-23 10:36:26 -0500 | [diff] [blame] | 691 | if (!(synth & PDR_SYNTH_5_GHZ_DISABLED)) | 
| Christian Lamparter | 1a9b667 | 2009-07-11 01:22:26 +0200 | [diff] [blame] | 692 | dev->wiphy->bands[IEEE80211_BAND_5GHZ] = | 
|  | 693 | priv->band_table[IEEE80211_BAND_5GHZ]; | 
| Christian Lamparter | 4c8a32f | 2009-06-23 10:36:26 -0500 | [diff] [blame] | 694 | if ((synth & PDR_SYNTH_RX_DIV_MASK) == PDR_SYNTH_RX_DIV_SUPPORTED) | 
|  | 695 | priv->rx_diversity_mask = 3; | 
|  | 696 | if ((synth & PDR_SYNTH_TX_DIV_MASK) == PDR_SYNTH_TX_DIV_SUPPORTED) | 
|  | 697 | priv->tx_diversity_mask = 3; | 
|  | 698 |  | 
|  | 699 | if (!is_valid_ether_addr(dev->wiphy->perm_addr)) { | 
|  | 700 | u8 perm_addr[ETH_ALEN]; | 
|  | 701 |  | 
|  | 702 | printk(KERN_WARNING "%s: Invalid hwaddr! Using randomly generated MAC addr\n", | 
|  | 703 | wiphy_name(dev->wiphy)); | 
|  | 704 | random_ether_addr(perm_addr); | 
|  | 705 | SET_IEEE80211_PERM_ADDR(dev, perm_addr); | 
|  | 706 | } | 
|  | 707 |  | 
|  | 708 | printk(KERN_INFO "%s: hwaddr %pM, MAC:isl38%02x RF:%s\n", | 
|  | 709 | wiphy_name(dev->wiphy),	dev->wiphy->perm_addr, priv->version, | 
|  | 710 | p54_rf_chips[priv->rxhw]); | 
|  | 711 |  | 
|  | 712 | return 0; | 
|  | 713 |  | 
|  | 714 | err: | 
|  | 715 | kfree(priv->iq_autocal); | 
|  | 716 | kfree(priv->output_limit); | 
|  | 717 | kfree(priv->curve_data); | 
|  | 718 | priv->iq_autocal = NULL; | 
|  | 719 | priv->output_limit = NULL; | 
|  | 720 | priv->curve_data = NULL; | 
|  | 721 |  | 
|  | 722 | printk(KERN_ERR "%s: eeprom parse failed!\n", | 
|  | 723 | wiphy_name(dev->wiphy)); | 
|  | 724 | return err; | 
|  | 725 | } | 
|  | 726 | EXPORT_SYMBOL_GPL(p54_parse_eeprom); | 
|  | 727 |  | 
|  | 728 | int p54_read_eeprom(struct ieee80211_hw *dev) | 
|  | 729 | { | 
|  | 730 | struct p54_common *priv = dev->priv; | 
|  | 731 | size_t eeprom_size = 0x2020, offset = 0, blocksize, maxblocksize; | 
|  | 732 | int ret = -ENOMEM; | 
| Larry Finger | e6a3f55 | 2009-07-19 21:53:14 -0500 | [diff] [blame] | 733 | void *eeprom; | 
| Christian Lamparter | 4c8a32f | 2009-06-23 10:36:26 -0500 | [diff] [blame] | 734 |  | 
|  | 735 | maxblocksize = EEPROM_READBACK_LEN; | 
|  | 736 | if (priv->fw_var >= 0x509) | 
|  | 737 | maxblocksize -= 0xc; | 
|  | 738 | else | 
|  | 739 | maxblocksize -= 0x4; | 
|  | 740 |  | 
|  | 741 | eeprom = kzalloc(eeprom_size, GFP_KERNEL); | 
|  | 742 | if (unlikely(!eeprom)) | 
|  | 743 | goto free; | 
|  | 744 |  | 
|  | 745 | while (eeprom_size) { | 
|  | 746 | blocksize = min(eeprom_size, maxblocksize); | 
|  | 747 | ret = p54_download_eeprom(priv, (void *) (eeprom + offset), | 
|  | 748 | offset, blocksize); | 
|  | 749 | if (unlikely(ret)) | 
|  | 750 | goto free; | 
|  | 751 |  | 
|  | 752 | offset += blocksize; | 
|  | 753 | eeprom_size -= blocksize; | 
|  | 754 | } | 
|  | 755 |  | 
|  | 756 | ret = p54_parse_eeprom(dev, eeprom, offset); | 
|  | 757 | free: | 
|  | 758 | kfree(eeprom); | 
|  | 759 | return ret; | 
|  | 760 | } | 
|  | 761 | EXPORT_SYMBOL_GPL(p54_read_eeprom); |