Commit | Line | Data |
---|---|---|
4c8a32f5 CL |
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> | |
1a9b6679 | 22 | #include <linux/sort.h> |
5a0e3ad6 | 23 | #include <linux/slab.h> |
4c8a32f5 CL |
24 | |
25 | #include <net/mac80211.h> | |
d7eb50c0 | 26 | #include <linux/crc-ccitt.h> |
4c8a32f5 CL |
27 | |
28 | #include "p54.h" | |
29 | #include "eeprom.h" | |
30 | #include "lmac.h" | |
31 | ||
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, }, | |
45 | }; | |
46 | ||
4c8a32f5 CL |
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, }, | |
56 | }; | |
57 | ||
1a9b6679 CL |
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) | |
62 | ||
63 | struct p54_channel_entry { | |
64 | u16 freq; | |
65 | u16 data; | |
66 | int index; | |
67 | enum ieee80211_band band; | |
4c8a32f5 CL |
68 | }; |
69 | ||
1a9b6679 CL |
70 | struct p54_channel_list { |
71 | struct p54_channel_entry *channels; | |
72 | size_t entries; | |
73 | size_t max_entries; | |
74 | size_t band_channel_num[IEEE80211_NUM_BANDS]; | |
4c8a32f5 CL |
75 | }; |
76 | ||
1a9b6679 CL |
77 | static int p54_get_band_from_freq(u16 freq) |
78 | { | |
79 | /* FIXME: sync these values with the 802.11 spec */ | |
80 | ||
81 | if ((freq >= 2412) && (freq <= 2484)) | |
82 | return IEEE80211_BAND_2GHZ; | |
83 | ||
84 | if ((freq >= 4920) && (freq <= 5825)) | |
85 | return IEEE80211_BAND_5GHZ; | |
86 | ||
87 | return -1; | |
88 | } | |
89 | ||
90 | static int p54_compare_channels(const void *_a, | |
91 | const void *_b) | |
92 | { | |
93 | const struct p54_channel_entry *a = _a; | |
94 | const struct p54_channel_entry *b = _b; | |
95 | ||
192abece | 96 | return a->freq - b->freq; |
1a9b6679 CL |
97 | } |
98 | ||
99 | static int p54_fill_band_bitrates(struct ieee80211_hw *dev, | |
100 | struct ieee80211_supported_band *band_entry, | |
101 | enum ieee80211_band band) | |
102 | { | |
103 | /* TODO: generate rate array dynamically */ | |
104 | ||
105 | switch (band) { | |
106 | case IEEE80211_BAND_2GHZ: | |
107 | band_entry->bitrates = p54_bgrates; | |
108 | band_entry->n_bitrates = ARRAY_SIZE(p54_bgrates); | |
109 | break; | |
110 | case IEEE80211_BAND_5GHZ: | |
111 | band_entry->bitrates = p54_arates; | |
112 | band_entry->n_bitrates = ARRAY_SIZE(p54_arates); | |
113 | break; | |
114 | default: | |
115 | return -EINVAL; | |
116 | } | |
117 | ||
118 | return 0; | |
119 | } | |
120 | ||
121 | static int p54_generate_band(struct ieee80211_hw *dev, | |
122 | struct p54_channel_list *list, | |
123 | enum ieee80211_band band) | |
124 | { | |
125 | struct p54_common *priv = dev->priv; | |
126 | struct ieee80211_supported_band *tmp, *old; | |
127 | unsigned int i, j; | |
128 | int ret = -ENOMEM; | |
129 | ||
130 | if ((!list->entries) || (!list->band_channel_num[band])) | |
93a59d75 | 131 | return -EINVAL; |
1a9b6679 CL |
132 | |
133 | tmp = kzalloc(sizeof(*tmp), GFP_KERNEL); | |
134 | if (!tmp) | |
135 | goto err_out; | |
136 | ||
137 | tmp->channels = kzalloc(sizeof(struct ieee80211_channel) * | |
138 | list->band_channel_num[band], GFP_KERNEL); | |
139 | if (!tmp->channels) | |
140 | goto err_out; | |
141 | ||
142 | ret = p54_fill_band_bitrates(dev, tmp, band); | |
143 | if (ret) | |
144 | goto err_out; | |
145 | ||
146 | for (i = 0, j = 0; (j < list->band_channel_num[band]) && | |
147 | (i < list->entries); i++) { | |
a3162eed | 148 | struct p54_channel_entry *chan = &list->channels[i]; |
1a9b6679 | 149 | |
a3162eed | 150 | if (chan->band != band) |
1a9b6679 CL |
151 | continue; |
152 | ||
a3162eed CL |
153 | if (chan->data != CHAN_HAS_ALL) { |
154 | wiphy_err(dev->wiphy, "%s%s%s is/are missing for " | |
155 | "channel:%d [%d MHz].\n", | |
156 | (chan->data & CHAN_HAS_CAL ? "" : | |
c96c31e4 | 157 | " [iqauto calibration data]"), |
a3162eed | 158 | (chan->data & CHAN_HAS_LIMIT ? "" : |
c96c31e4 | 159 | " [output power limits]"), |
a3162eed | 160 | (chan->data & CHAN_HAS_CURVE ? "" : |
c96c31e4 | 161 | " [curve data]"), |
a3162eed | 162 | chan->index, chan->freq); |
93a59d75 | 163 | continue; |
1a9b6679 CL |
164 | } |
165 | ||
a3162eed CL |
166 | tmp->channels[j].band = chan->band; |
167 | tmp->channels[j].center_freq = chan->freq; | |
1a9b6679 CL |
168 | j++; |
169 | } | |
170 | ||
93a59d75 | 171 | if (j == 0) { |
5db55844 | 172 | wiphy_err(dev->wiphy, "Disabling totally damaged %d GHz band\n", |
c96c31e4 | 173 | (band == IEEE80211_BAND_2GHZ) ? 2 : 5); |
93a59d75 CL |
174 | |
175 | ret = -ENODATA; | |
176 | goto err_out; | |
177 | } | |
178 | ||
179 | tmp->n_channels = j; | |
1a9b6679 CL |
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; | |
93a59d75 | 242 | int ret = 0; |
1a9b6679 CL |
243 | u16 freq; |
244 | ||
245 | if ((priv->iq_autocal_len != priv->curve_data->entries) || | |
246 | (priv->iq_autocal_len != priv->output_limit->entries)) | |
c96c31e4 JP |
247 | wiphy_err(dev->wiphy, |
248 | "Unsupported or damaged EEPROM detected. " | |
249 | "You may not be able to use all channels.\n"); | |
1a9b6679 CL |
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); | |
93a59d75 CL |
257 | if (!list) { |
258 | ret = -ENOMEM; | |
1a9b6679 | 259 | goto free; |
93a59d75 | 260 | } |
1a9b6679 CL |
261 | |
262 | list->max_entries = max_channel_num; | |
263 | list->channels = kzalloc(sizeof(struct p54_channel_entry) * | |
264 | max_channel_num, GFP_KERNEL); | |
0d91f22b JL |
265 | if (!list->channels) { |
266 | ret = -ENOMEM; | |
1a9b6679 | 267 | goto free; |
0d91f22b | 268 | } |
1a9b6679 CL |
269 | |
270 | for (i = 0; i < max_channel_num; i++) { | |
271 | if (i < priv->iq_autocal_len) { | |
272 | freq = le16_to_cpu(priv->iq_autocal[i].freq); | |
273 | p54_update_channel_param(list, freq, CHAN_HAS_CAL); | |
274 | } | |
275 | ||
276 | if (i < priv->output_limit->entries) { | |
277 | freq = le16_to_cpup((__le16 *) (i * | |
278 | priv->output_limit->entry_size + | |
279 | priv->output_limit->offset + | |
280 | priv->output_limit->data)); | |
281 | ||
282 | p54_update_channel_param(list, freq, CHAN_HAS_LIMIT); | |
283 | } | |
284 | ||
285 | if (i < priv->curve_data->entries) { | |
286 | freq = le16_to_cpup((__le16 *) (i * | |
287 | priv->curve_data->entry_size + | |
288 | priv->curve_data->offset + | |
289 | priv->curve_data->data)); | |
290 | ||
291 | p54_update_channel_param(list, freq, CHAN_HAS_CURVE); | |
292 | } | |
293 | } | |
294 | ||
192abece | 295 | /* sort the channel list by frequency */ |
1a9b6679 CL |
296 | sort(list->channels, list->entries, sizeof(struct p54_channel_entry), |
297 | p54_compare_channels, NULL); | |
298 | ||
299 | for (i = 0, j = 0; i < IEEE80211_NUM_BANDS; i++) { | |
93a59d75 | 300 | if (p54_generate_band(dev, list, i) == 0) |
1a9b6679 | 301 | j++; |
1a9b6679 CL |
302 | } |
303 | if (j == 0) { | |
304 | /* no useable band available. */ | |
305 | ret = -EINVAL; | |
306 | } | |
307 | ||
308 | free: | |
309 | if (list) { | |
310 | kfree(list->channels); | |
311 | kfree(list); | |
312 | } | |
313 | ||
314 | return ret; | |
315 | } | |
316 | ||
4c8a32f5 CL |
317 | static int p54_convert_rev0(struct ieee80211_hw *dev, |
318 | struct pda_pa_curve_data *curve_data) | |
319 | { | |
320 | struct p54_common *priv = dev->priv; | |
321 | struct p54_pa_curve_data_sample *dst; | |
322 | struct pda_pa_curve_data_sample_rev0 *src; | |
323 | size_t cd_len = sizeof(*curve_data) + | |
324 | (curve_data->points_per_channel*sizeof(*dst) + 2) * | |
325 | curve_data->channels; | |
326 | unsigned int i, j; | |
327 | void *source, *target; | |
328 | ||
329 | priv->curve_data = kmalloc(sizeof(*priv->curve_data) + cd_len, | |
330 | GFP_KERNEL); | |
331 | if (!priv->curve_data) | |
332 | return -ENOMEM; | |
333 | ||
334 | priv->curve_data->entries = curve_data->channels; | |
335 | priv->curve_data->entry_size = sizeof(__le16) + | |
336 | sizeof(*dst) * curve_data->points_per_channel; | |
337 | priv->curve_data->offset = offsetof(struct pda_pa_curve_data, data); | |
338 | priv->curve_data->len = cd_len; | |
339 | memcpy(priv->curve_data->data, curve_data, sizeof(*curve_data)); | |
340 | source = curve_data->data; | |
341 | target = ((struct pda_pa_curve_data *) priv->curve_data->data)->data; | |
342 | for (i = 0; i < curve_data->channels; i++) { | |
343 | __le16 *freq = source; | |
344 | source += sizeof(__le16); | |
345 | *((__le16 *)target) = *freq; | |
346 | target += sizeof(__le16); | |
347 | for (j = 0; j < curve_data->points_per_channel; j++) { | |
348 | dst = target; | |
349 | src = source; | |
350 | ||
351 | dst->rf_power = src->rf_power; | |
352 | dst->pa_detector = src->pa_detector; | |
353 | dst->data_64qam = src->pcv; | |
354 | /* "invent" the points for the other modulations */ | |
355 | #define SUB(x, y) (u8)(((x) - (y)) > (x) ? 0 : (x) - (y)) | |
356 | dst->data_16qam = SUB(src->pcv, 12); | |
357 | dst->data_qpsk = SUB(dst->data_16qam, 12); | |
358 | dst->data_bpsk = SUB(dst->data_qpsk, 12); | |
359 | dst->data_barker = SUB(dst->data_bpsk, 14); | |
360 | #undef SUB | |
361 | target += sizeof(*dst); | |
362 | source += sizeof(*src); | |
363 | } | |
364 | } | |
365 | ||
366 | return 0; | |
367 | } | |
368 | ||
369 | static int p54_convert_rev1(struct ieee80211_hw *dev, | |
370 | struct pda_pa_curve_data *curve_data) | |
371 | { | |
372 | struct p54_common *priv = dev->priv; | |
373 | struct p54_pa_curve_data_sample *dst; | |
374 | struct pda_pa_curve_data_sample_rev1 *src; | |
375 | size_t cd_len = sizeof(*curve_data) + | |
376 | (curve_data->points_per_channel*sizeof(*dst) + 2) * | |
377 | curve_data->channels; | |
378 | unsigned int i, j; | |
379 | void *source, *target; | |
380 | ||
381 | priv->curve_data = kzalloc(cd_len + sizeof(*priv->curve_data), | |
382 | GFP_KERNEL); | |
383 | if (!priv->curve_data) | |
384 | return -ENOMEM; | |
385 | ||
386 | priv->curve_data->entries = curve_data->channels; | |
387 | priv->curve_data->entry_size = sizeof(__le16) + | |
388 | sizeof(*dst) * curve_data->points_per_channel; | |
389 | priv->curve_data->offset = offsetof(struct pda_pa_curve_data, data); | |
390 | priv->curve_data->len = cd_len; | |
391 | memcpy(priv->curve_data->data, curve_data, sizeof(*curve_data)); | |
392 | source = curve_data->data; | |
393 | target = ((struct pda_pa_curve_data *) priv->curve_data->data)->data; | |
394 | for (i = 0; i < curve_data->channels; i++) { | |
395 | __le16 *freq = source; | |
396 | source += sizeof(__le16); | |
397 | *((__le16 *)target) = *freq; | |
398 | target += sizeof(__le16); | |
399 | for (j = 0; j < curve_data->points_per_channel; j++) { | |
400 | memcpy(target, source, sizeof(*src)); | |
401 | ||
402 | target += sizeof(*dst); | |
403 | source += sizeof(*src); | |
404 | } | |
405 | source++; | |
406 | } | |
407 | ||
408 | return 0; | |
409 | } | |
410 | ||
411 | static const char *p54_rf_chips[] = { "INVALID-0", "Duette3", "Duette2", | |
412 | "Frisbee", "Xbow", "Longbow", "INVALID-6", "INVALID-7" }; | |
413 | ||
414 | static void p54_parse_rssical(struct ieee80211_hw *dev, void *data, int len, | |
415 | u16 type) | |
416 | { | |
417 | struct p54_common *priv = dev->priv; | |
418 | int offset = (type == PDR_RSSI_LINEAR_APPROXIMATION_EXTENDED) ? 2 : 0; | |
419 | int entry_size = sizeof(struct pda_rssi_cal_entry) + offset; | |
420 | int num_entries = (type == PDR_RSSI_LINEAR_APPROXIMATION) ? 1 : 2; | |
421 | int i; | |
422 | ||
423 | if (len != (entry_size * num_entries)) { | |
c96c31e4 JP |
424 | wiphy_err(dev->wiphy, |
425 | "unknown rssi calibration data packing type:(%x) len:%d.\n", | |
426 | type, len); | |
4c8a32f5 CL |
427 | |
428 | print_hex_dump_bytes("rssical:", DUMP_PREFIX_NONE, | |
429 | data, len); | |
430 | ||
c96c31e4 | 431 | wiphy_err(dev->wiphy, "please report this issue.\n"); |
4c8a32f5 CL |
432 | return; |
433 | } | |
434 | ||
435 | for (i = 0; i < num_entries; i++) { | |
436 | struct pda_rssi_cal_entry *cal = data + | |
437 | (offset + i * entry_size); | |
438 | priv->rssical_db[i].mul = (s16) le16_to_cpu(cal->mul); | |
439 | priv->rssical_db[i].add = (s16) le16_to_cpu(cal->add); | |
440 | } | |
441 | } | |
442 | ||
443 | static void p54_parse_default_country(struct ieee80211_hw *dev, | |
444 | void *data, int len) | |
445 | { | |
446 | struct pda_country *country; | |
447 | ||
448 | if (len != sizeof(*country)) { | |
c96c31e4 JP |
449 | wiphy_err(dev->wiphy, |
450 | "found possible invalid default country eeprom entry. (entry size: %d)\n", | |
451 | len); | |
4c8a32f5 CL |
452 | |
453 | print_hex_dump_bytes("country:", DUMP_PREFIX_NONE, | |
454 | data, len); | |
455 | ||
c96c31e4 | 456 | wiphy_err(dev->wiphy, "please report this issue.\n"); |
4c8a32f5 CL |
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) { | |
c96c31e4 JP |
481 | wiphy_err(dev->wiphy, "unknown output power db revision:%x\n", |
482 | data[0]); | |
4c8a32f5 CL |
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; | |
e6a3f551 | 540 | struct eeprom_pda_wrap *wrap; |
4c8a32f5 CL |
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; | |
d7eb50c0 | 547 | u16 crc16 = ~0; |
4c8a32f5 CL |
548 | |
549 | wrap = (struct eeprom_pda_wrap *) eeprom; | |
550 | entry = (void *)wrap->data + le16_to_cpu(wrap->len); | |
551 | ||
552 | /* verify that at least the entry length/code fits */ | |
553 | while ((u8 *)entry <= end - sizeof(*entry)) { | |
554 | entry_len = le16_to_cpu(entry->len); | |
555 | data_len = ((entry_len - 1) << 1); | |
556 | ||
557 | /* abort if entry exceeds whole structure */ | |
558 | if ((u8 *)entry + sizeof(*entry) + data_len > end) | |
559 | break; | |
560 | ||
561 | switch (le16_to_cpu(entry->code)) { | |
562 | case PDR_MAC_ADDRESS: | |
563 | if (data_len != ETH_ALEN) | |
564 | break; | |
565 | SET_IEEE80211_PERM_ADDR(dev, entry->data); | |
566 | break; | |
567 | case PDR_PRISM_PA_CAL_OUTPUT_POWER_LIMITS: | |
568 | if (priv->output_limit) | |
569 | break; | |
570 | err = p54_convert_output_limits(dev, entry->data, | |
571 | data_len); | |
572 | if (err) | |
573 | goto err; | |
574 | break; | |
575 | case PDR_PRISM_PA_CAL_CURVE_DATA: { | |
576 | struct pda_pa_curve_data *curve_data = | |
577 | (struct pda_pa_curve_data *)entry->data; | |
578 | if (data_len < sizeof(*curve_data)) { | |
579 | err = -EINVAL; | |
580 | goto err; | |
581 | } | |
582 | ||
583 | switch (curve_data->cal_method_rev) { | |
584 | case 0: | |
585 | err = p54_convert_rev0(dev, curve_data); | |
586 | break; | |
587 | case 1: | |
588 | err = p54_convert_rev1(dev, curve_data); | |
589 | break; | |
590 | default: | |
c96c31e4 JP |
591 | wiphy_err(dev->wiphy, |
592 | "unknown curve data revision %d\n", | |
593 | curve_data->cal_method_rev); | |
4c8a32f5 CL |
594 | err = -ENODEV; |
595 | break; | |
596 | } | |
597 | if (err) | |
598 | goto err; | |
599 | } | |
600 | break; | |
601 | case PDR_PRISM_ZIF_TX_IQ_CALIBRATION: | |
27b81bbe JL |
602 | priv->iq_autocal = kmemdup(entry->data, data_len, |
603 | GFP_KERNEL); | |
4c8a32f5 CL |
604 | if (!priv->iq_autocal) { |
605 | err = -ENOMEM; | |
606 | goto err; | |
607 | } | |
608 | ||
4c8a32f5 CL |
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: | |
d7eb50c0 CL |
663 | crc16 = ~crc_ccitt(crc16, (u8 *) entry, sizeof(*entry)); |
664 | if (crc16 != le16_to_cpup((__le16 *)entry->data)) { | |
665 | wiphy_err(dev->wiphy, "eeprom failed checksum " | |
666 | "test!\n"); | |
667 | err = -ENOMSG; | |
668 | goto err; | |
669 | } else { | |
670 | goto good_eeprom; | |
671 | } | |
4c8a32f5 CL |
672 | break; |
673 | default: | |
674 | break; | |
675 | } | |
676 | ||
d7eb50c0 CL |
677 | crc16 = crc_ccitt(crc16, (u8 *)entry, (entry_len + 1) * 2); |
678 | entry = (void *)entry + (entry_len + 1) * 2; | |
4c8a32f5 CL |
679 | } |
680 | ||
d7eb50c0 CL |
681 | wiphy_err(dev->wiphy, "unexpected end of eeprom data.\n"); |
682 | err = -ENODATA; | |
683 | goto err; | |
684 | ||
685 | good_eeprom: | |
4c8a32f5 CL |
686 | if (!synth || !priv->iq_autocal || !priv->output_limit || |
687 | !priv->curve_data) { | |
c96c31e4 JP |
688 | wiphy_err(dev->wiphy, |
689 | "not all required entries found in eeprom!\n"); | |
4c8a32f5 CL |
690 | err = -EINVAL; |
691 | goto err; | |
692 | } | |
693 | ||
1a9b6679 CL |
694 | err = p54_generate_channel_lists(dev); |
695 | if (err) | |
696 | goto err; | |
697 | ||
4c8a32f5 CL |
698 | priv->rxhw = synth & PDR_SYNTH_FRONTEND_MASK; |
699 | if (priv->rxhw == PDR_SYNTH_FRONTEND_XBOW) | |
700 | p54_init_xbow_synth(priv); | |
701 | if (!(synth & PDR_SYNTH_24_GHZ_DISABLED)) | |
1a9b6679 CL |
702 | dev->wiphy->bands[IEEE80211_BAND_2GHZ] = |
703 | priv->band_table[IEEE80211_BAND_2GHZ]; | |
4c8a32f5 | 704 | if (!(synth & PDR_SYNTH_5_GHZ_DISABLED)) |
1a9b6679 CL |
705 | dev->wiphy->bands[IEEE80211_BAND_5GHZ] = |
706 | priv->band_table[IEEE80211_BAND_5GHZ]; | |
4c8a32f5 CL |
707 | if ((synth & PDR_SYNTH_RX_DIV_MASK) == PDR_SYNTH_RX_DIV_SUPPORTED) |
708 | priv->rx_diversity_mask = 3; | |
709 | if ((synth & PDR_SYNTH_TX_DIV_MASK) == PDR_SYNTH_TX_DIV_SUPPORTED) | |
710 | priv->tx_diversity_mask = 3; | |
711 | ||
712 | if (!is_valid_ether_addr(dev->wiphy->perm_addr)) { | |
713 | u8 perm_addr[ETH_ALEN]; | |
714 | ||
c96c31e4 | 715 | wiphy_warn(dev->wiphy, |
5db55844 | 716 | "Invalid hwaddr! Using randomly generated MAC addr\n"); |
4c8a32f5 CL |
717 | random_ether_addr(perm_addr); |
718 | SET_IEEE80211_PERM_ADDR(dev, perm_addr); | |
719 | } | |
720 | ||
5db55844 | 721 | wiphy_info(dev->wiphy, "hwaddr %pM, MAC:isl38%02x RF:%s\n", |
c96c31e4 JP |
722 | dev->wiphy->perm_addr, priv->version, |
723 | p54_rf_chips[priv->rxhw]); | |
4c8a32f5 CL |
724 | |
725 | return 0; | |
726 | ||
727 | err: | |
728 | kfree(priv->iq_autocal); | |
729 | kfree(priv->output_limit); | |
730 | kfree(priv->curve_data); | |
731 | priv->iq_autocal = NULL; | |
732 | priv->output_limit = NULL; | |
733 | priv->curve_data = NULL; | |
734 | ||
c96c31e4 | 735 | wiphy_err(dev->wiphy, "eeprom parse failed!\n"); |
4c8a32f5 CL |
736 | return err; |
737 | } | |
738 | EXPORT_SYMBOL_GPL(p54_parse_eeprom); | |
739 | ||
740 | int p54_read_eeprom(struct ieee80211_hw *dev) | |
741 | { | |
742 | struct p54_common *priv = dev->priv; | |
743 | size_t eeprom_size = 0x2020, offset = 0, blocksize, maxblocksize; | |
744 | int ret = -ENOMEM; | |
e6a3f551 | 745 | void *eeprom; |
4c8a32f5 CL |
746 | |
747 | maxblocksize = EEPROM_READBACK_LEN; | |
748 | if (priv->fw_var >= 0x509) | |
749 | maxblocksize -= 0xc; | |
750 | else | |
751 | maxblocksize -= 0x4; | |
752 | ||
753 | eeprom = kzalloc(eeprom_size, GFP_KERNEL); | |
754 | if (unlikely(!eeprom)) | |
755 | goto free; | |
756 | ||
757 | while (eeprom_size) { | |
758 | blocksize = min(eeprom_size, maxblocksize); | |
759 | ret = p54_download_eeprom(priv, (void *) (eeprom + offset), | |
760 | offset, blocksize); | |
761 | if (unlikely(ret)) | |
762 | goto free; | |
763 | ||
764 | offset += blocksize; | |
765 | eeprom_size -= blocksize; | |
766 | } | |
767 | ||
768 | ret = p54_parse_eeprom(dev, eeprom, offset); | |
769 | free: | |
770 | kfree(eeprom); | |
771 | return ret; | |
772 | } | |
773 | EXPORT_SYMBOL_GPL(p54_read_eeprom); |