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++) { | |
148 | ||
149 | if (list->channels[i].band != band) | |
150 | continue; | |
151 | ||
152 | if (list->channels[i].data != CHAN_HAS_ALL) { | |
c96c31e4 JP |
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 ? "" : | |
160 | " [curve data]"), | |
161 | list->channels[i].index, list->channels[i].freq); | |
93a59d75 | 162 | continue; |
1a9b6679 CL |
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 | ||
93a59d75 | 170 | if (j == 0) { |
5db55844 | 171 | wiphy_err(dev->wiphy, "Disabling totally damaged %d GHz band\n", |
c96c31e4 | 172 | (band == IEEE80211_BAND_2GHZ) ? 2 : 5); |
93a59d75 CL |
173 | |
174 | ret = -ENODATA; | |
175 | goto err_out; | |
176 | } | |
177 | ||
178 | tmp->n_channels = j; | |
1a9b6679 CL |
179 | old = priv->band_table[band]; |
180 | priv->band_table[band] = tmp; | |
181 | if (old) { | |
182 | kfree(old->channels); | |
183 | kfree(old); | |
184 | } | |
185 | ||
186 | return 0; | |
187 | ||
188 | err_out: | |
189 | if (tmp) { | |
190 | kfree(tmp->channels); | |
191 | kfree(tmp); | |
192 | } | |
193 | ||
194 | return ret; | |
195 | } | |
196 | ||
197 | static void p54_update_channel_param(struct p54_channel_list *list, | |
198 | u16 freq, u16 data) | |
199 | { | |
200 | int band, i; | |
201 | ||
202 | /* | |
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 | |
206 | */ | |
207 | for (i = list->entries; i >= 0; i--) { | |
208 | if (freq == list->channels[i].freq) { | |
209 | list->channels[i].data |= data; | |
210 | break; | |
211 | } | |
212 | } | |
213 | ||
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); | |
217 | ||
218 | /* | |
219 | * filter out frequencies which don't belong into | |
220 | * any supported band. | |
221 | */ | |
222 | if (band < 0) | |
223 | return ; | |
224 | ||
225 | i = list->entries++; | |
226 | list->band_channel_num[band]++; | |
227 | ||
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 */ | |
233 | } | |
234 | } | |
235 | ||
236 | static int p54_generate_channel_lists(struct ieee80211_hw *dev) | |
237 | { | |
238 | struct p54_common *priv = dev->priv; | |
239 | struct p54_channel_list *list; | |
240 | unsigned int i, j, max_channel_num; | |
93a59d75 | 241 | int ret = 0; |
1a9b6679 CL |
242 | u16 freq; |
243 | ||
244 | if ((priv->iq_autocal_len != priv->curve_data->entries) || | |
245 | (priv->iq_autocal_len != priv->output_limit->entries)) | |
c96c31e4 JP |
246 | wiphy_err(dev->wiphy, |
247 | "Unsupported or damaged EEPROM detected. " | |
248 | "You may not be able to use all channels.\n"); | |
1a9b6679 CL |
249 | |
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); | |
254 | ||
255 | list = kzalloc(sizeof(*list), GFP_KERNEL); | |
93a59d75 CL |
256 | if (!list) { |
257 | ret = -ENOMEM; | |
1a9b6679 | 258 | goto free; |
93a59d75 | 259 | } |
1a9b6679 CL |
260 | |
261 | list->max_entries = max_channel_num; | |
262 | list->channels = kzalloc(sizeof(struct p54_channel_entry) * | |
263 | max_channel_num, GFP_KERNEL); | |
0d91f22b JL |
264 | if (!list->channels) { |
265 | ret = -ENOMEM; | |
1a9b6679 | 266 | goto free; |
0d91f22b | 267 | } |
1a9b6679 CL |
268 | |
269 | for (i = 0; i < max_channel_num; i++) { | |
270 | if (i < priv->iq_autocal_len) { | |
271 | freq = le16_to_cpu(priv->iq_autocal[i].freq); | |
272 | p54_update_channel_param(list, freq, CHAN_HAS_CAL); | |
273 | } | |
274 | ||
275 | if (i < priv->output_limit->entries) { | |
276 | freq = le16_to_cpup((__le16 *) (i * | |
277 | priv->output_limit->entry_size + | |
278 | priv->output_limit->offset + | |
279 | priv->output_limit->data)); | |
280 | ||
281 | p54_update_channel_param(list, freq, CHAN_HAS_LIMIT); | |
282 | } | |
283 | ||
284 | if (i < priv->curve_data->entries) { | |
285 | freq = le16_to_cpup((__le16 *) (i * | |
286 | priv->curve_data->entry_size + | |
287 | priv->curve_data->offset + | |
288 | priv->curve_data->data)); | |
289 | ||
290 | p54_update_channel_param(list, freq, CHAN_HAS_CURVE); | |
291 | } | |
292 | } | |
293 | ||
192abece | 294 | /* sort the channel list by frequency */ |
1a9b6679 CL |
295 | sort(list->channels, list->entries, sizeof(struct p54_channel_entry), |
296 | p54_compare_channels, NULL); | |
297 | ||
298 | for (i = 0, j = 0; i < IEEE80211_NUM_BANDS; i++) { | |
93a59d75 | 299 | if (p54_generate_band(dev, list, i) == 0) |
1a9b6679 | 300 | j++; |
1a9b6679 CL |
301 | } |
302 | if (j == 0) { | |
303 | /* no useable band available. */ | |
304 | ret = -EINVAL; | |
305 | } | |
306 | ||
307 | free: | |
308 | if (list) { | |
309 | kfree(list->channels); | |
310 | kfree(list); | |
311 | } | |
312 | ||
313 | return ret; | |
314 | } | |
315 | ||
4c8a32f5 CL |
316 | static int p54_convert_rev0(struct ieee80211_hw *dev, |
317 | struct pda_pa_curve_data *curve_data) | |
318 | { | |
319 | struct p54_common *priv = dev->priv; | |
320 | struct p54_pa_curve_data_sample *dst; | |
321 | struct pda_pa_curve_data_sample_rev0 *src; | |
322 | size_t cd_len = sizeof(*curve_data) + | |
323 | (curve_data->points_per_channel*sizeof(*dst) + 2) * | |
324 | curve_data->channels; | |
325 | unsigned int i, j; | |
326 | void *source, *target; | |
327 | ||
328 | priv->curve_data = kmalloc(sizeof(*priv->curve_data) + cd_len, | |
329 | GFP_KERNEL); | |
330 | if (!priv->curve_data) | |
331 | return -ENOMEM; | |
332 | ||
333 | priv->curve_data->entries = curve_data->channels; | |
334 | priv->curve_data->entry_size = sizeof(__le16) + | |
335 | sizeof(*dst) * curve_data->points_per_channel; | |
336 | priv->curve_data->offset = offsetof(struct pda_pa_curve_data, data); | |
337 | priv->curve_data->len = cd_len; | |
338 | memcpy(priv->curve_data->data, curve_data, sizeof(*curve_data)); | |
339 | source = curve_data->data; | |
340 | target = ((struct pda_pa_curve_data *) priv->curve_data->data)->data; | |
341 | for (i = 0; i < curve_data->channels; i++) { | |
342 | __le16 *freq = source; | |
343 | source += sizeof(__le16); | |
344 | *((__le16 *)target) = *freq; | |
345 | target += sizeof(__le16); | |
346 | for (j = 0; j < curve_data->points_per_channel; j++) { | |
347 | dst = target; | |
348 | src = source; | |
349 | ||
350 | dst->rf_power = src->rf_power; | |
351 | dst->pa_detector = src->pa_detector; | |
352 | dst->data_64qam = src->pcv; | |
353 | /* "invent" the points for the other modulations */ | |
354 | #define SUB(x, y) (u8)(((x) - (y)) > (x) ? 0 : (x) - (y)) | |
355 | dst->data_16qam = SUB(src->pcv, 12); | |
356 | dst->data_qpsk = SUB(dst->data_16qam, 12); | |
357 | dst->data_bpsk = SUB(dst->data_qpsk, 12); | |
358 | dst->data_barker = SUB(dst->data_bpsk, 14); | |
359 | #undef SUB | |
360 | target += sizeof(*dst); | |
361 | source += sizeof(*src); | |
362 | } | |
363 | } | |
364 | ||
365 | return 0; | |
366 | } | |
367 | ||
368 | static int p54_convert_rev1(struct ieee80211_hw *dev, | |
369 | struct pda_pa_curve_data *curve_data) | |
370 | { | |
371 | struct p54_common *priv = dev->priv; | |
372 | struct p54_pa_curve_data_sample *dst; | |
373 | struct pda_pa_curve_data_sample_rev1 *src; | |
374 | size_t cd_len = sizeof(*curve_data) + | |
375 | (curve_data->points_per_channel*sizeof(*dst) + 2) * | |
376 | curve_data->channels; | |
377 | unsigned int i, j; | |
378 | void *source, *target; | |
379 | ||
380 | priv->curve_data = kzalloc(cd_len + sizeof(*priv->curve_data), | |
381 | GFP_KERNEL); | |
382 | if (!priv->curve_data) | |
383 | return -ENOMEM; | |
384 | ||
385 | priv->curve_data->entries = curve_data->channels; | |
386 | priv->curve_data->entry_size = sizeof(__le16) + | |
387 | sizeof(*dst) * curve_data->points_per_channel; | |
388 | priv->curve_data->offset = offsetof(struct pda_pa_curve_data, data); | |
389 | priv->curve_data->len = cd_len; | |
390 | memcpy(priv->curve_data->data, curve_data, sizeof(*curve_data)); | |
391 | source = curve_data->data; | |
392 | target = ((struct pda_pa_curve_data *) priv->curve_data->data)->data; | |
393 | for (i = 0; i < curve_data->channels; i++) { | |
394 | __le16 *freq = source; | |
395 | source += sizeof(__le16); | |
396 | *((__le16 *)target) = *freq; | |
397 | target += sizeof(__le16); | |
398 | for (j = 0; j < curve_data->points_per_channel; j++) { | |
399 | memcpy(target, source, sizeof(*src)); | |
400 | ||
401 | target += sizeof(*dst); | |
402 | source += sizeof(*src); | |
403 | } | |
404 | source++; | |
405 | } | |
406 | ||
407 | return 0; | |
408 | } | |
409 | ||
410 | static const char *p54_rf_chips[] = { "INVALID-0", "Duette3", "Duette2", | |
411 | "Frisbee", "Xbow", "Longbow", "INVALID-6", "INVALID-7" }; | |
412 | ||
413 | static void p54_parse_rssical(struct ieee80211_hw *dev, void *data, int len, | |
414 | u16 type) | |
415 | { | |
416 | struct p54_common *priv = dev->priv; | |
417 | int offset = (type == PDR_RSSI_LINEAR_APPROXIMATION_EXTENDED) ? 2 : 0; | |
418 | int entry_size = sizeof(struct pda_rssi_cal_entry) + offset; | |
419 | int num_entries = (type == PDR_RSSI_LINEAR_APPROXIMATION) ? 1 : 2; | |
420 | int i; | |
421 | ||
422 | if (len != (entry_size * num_entries)) { | |
c96c31e4 JP |
423 | wiphy_err(dev->wiphy, |
424 | "unknown rssi calibration data packing type:(%x) len:%d.\n", | |
425 | type, len); | |
4c8a32f5 CL |
426 | |
427 | print_hex_dump_bytes("rssical:", DUMP_PREFIX_NONE, | |
428 | data, len); | |
429 | ||
c96c31e4 | 430 | wiphy_err(dev->wiphy, "please report this issue.\n"); |
4c8a32f5 CL |
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)) { | |
c96c31e4 JP |
448 | wiphy_err(dev->wiphy, |
449 | "found possible invalid default country eeprom entry. (entry size: %d)\n", | |
450 | len); | |
4c8a32f5 CL |
451 | |
452 | print_hex_dump_bytes("country:", DUMP_PREFIX_NONE, | |
453 | data, len); | |
454 | ||
c96c31e4 | 455 | wiphy_err(dev->wiphy, "please report this issue.\n"); |
4c8a32f5 CL |
456 | return; |
457 | } | |
458 | ||
459 | country = (struct pda_country *) data; | |
460 | if (country->flags == PDR_COUNTRY_CERT_CODE_PSEUDO) | |
461 | regulatory_hint(dev->wiphy, country->alpha2); | |
462 | else { | |
463 | /* TODO: | |
464 | * write a shared/common function that converts | |
465 | * "Regulatory domain codes" (802.11-2007 14.8.2.2) | |
466 | * into ISO/IEC 3166-1 alpha2 for regulatory_hint. | |
467 | */ | |
468 | } | |
469 | } | |
470 | ||
471 | static int p54_convert_output_limits(struct ieee80211_hw *dev, | |
472 | u8 *data, size_t len) | |
473 | { | |
474 | struct p54_common *priv = dev->priv; | |
475 | ||
476 | if (len < 2) | |
477 | return -EINVAL; | |
478 | ||
479 | if (data[0] != 0) { | |
c96c31e4 JP |
480 | wiphy_err(dev->wiphy, "unknown output power db revision:%x\n", |
481 | data[0]); | |
4c8a32f5 CL |
482 | return -EINVAL; |
483 | } | |
484 | ||
485 | if (2 + data[1] * sizeof(struct pda_channel_output_limit) > len) | |
486 | return -EINVAL; | |
487 | ||
488 | priv->output_limit = kmalloc(data[1] * | |
489 | sizeof(struct pda_channel_output_limit) + | |
490 | sizeof(*priv->output_limit), GFP_KERNEL); | |
491 | ||
492 | if (!priv->output_limit) | |
493 | return -ENOMEM; | |
494 | ||
495 | priv->output_limit->offset = 0; | |
496 | priv->output_limit->entries = data[1]; | |
497 | priv->output_limit->entry_size = | |
498 | sizeof(struct pda_channel_output_limit); | |
499 | priv->output_limit->len = priv->output_limit->entry_size * | |
500 | priv->output_limit->entries + | |
501 | priv->output_limit->offset; | |
502 | ||
503 | memcpy(priv->output_limit->data, &data[2], | |
504 | data[1] * sizeof(struct pda_channel_output_limit)); | |
505 | ||
506 | return 0; | |
507 | } | |
508 | ||
509 | static struct p54_cal_database *p54_convert_db(struct pda_custom_wrapper *src, | |
510 | size_t total_len) | |
511 | { | |
512 | struct p54_cal_database *dst; | |
513 | size_t payload_len, entries, entry_size, offset; | |
514 | ||
515 | payload_len = le16_to_cpu(src->len); | |
516 | entries = le16_to_cpu(src->entries); | |
517 | entry_size = le16_to_cpu(src->entry_size); | |
518 | offset = le16_to_cpu(src->offset); | |
519 | if (((entries * entry_size + offset) != payload_len) || | |
520 | (payload_len + sizeof(*src) != total_len)) | |
521 | return NULL; | |
522 | ||
523 | dst = kmalloc(sizeof(*dst) + payload_len, GFP_KERNEL); | |
524 | if (!dst) | |
525 | return NULL; | |
526 | ||
527 | dst->entries = entries; | |
528 | dst->entry_size = entry_size; | |
529 | dst->offset = offset; | |
530 | dst->len = payload_len; | |
531 | ||
532 | memcpy(dst->data, src->data, payload_len); | |
533 | return dst; | |
534 | } | |
535 | ||
536 | int p54_parse_eeprom(struct ieee80211_hw *dev, void *eeprom, int len) | |
537 | { | |
538 | struct p54_common *priv = dev->priv; | |
e6a3f551 | 539 | struct eeprom_pda_wrap *wrap; |
4c8a32f5 CL |
540 | struct pda_entry *entry; |
541 | unsigned int data_len, entry_len; | |
542 | void *tmp; | |
543 | int err; | |
544 | u8 *end = (u8 *)eeprom + len; | |
545 | u16 synth = 0; | |
d7eb50c0 | 546 | u16 crc16 = ~0; |
4c8a32f5 CL |
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: | |
c96c31e4 JP |
590 | wiphy_err(dev->wiphy, |
591 | "unknown curve data revision %d\n", | |
592 | curve_data->cal_method_rev); | |
4c8a32f5 CL |
593 | err = -ENODEV; |
594 | break; | |
595 | } | |
596 | if (err) | |
597 | goto err; | |
598 | } | |
599 | break; | |
600 | case PDR_PRISM_ZIF_TX_IQ_CALIBRATION: | |
27b81bbe JL |
601 | priv->iq_autocal = kmemdup(entry->data, data_len, |
602 | GFP_KERNEL); | |
4c8a32f5 CL |
603 | if (!priv->iq_autocal) { |
604 | err = -ENOMEM; | |
605 | goto err; | |
606 | } | |
607 | ||
4c8a32f5 CL |
608 | priv->iq_autocal_len = data_len / sizeof(struct pda_iq_autocal_entry); |
609 | break; | |
610 | case PDR_DEFAULT_COUNTRY: | |
611 | p54_parse_default_country(dev, entry->data, data_len); | |
612 | break; | |
613 | case PDR_INTERFACE_LIST: | |
614 | tmp = entry->data; | |
615 | while ((u8 *)tmp < entry->data + data_len) { | |
616 | struct exp_if *exp_if = tmp; | |
617 | if (exp_if->if_id == cpu_to_le16(IF_ID_ISL39000)) | |
618 | synth = le16_to_cpu(exp_if->variant); | |
619 | tmp += sizeof(*exp_if); | |
620 | } | |
621 | break; | |
622 | case PDR_HARDWARE_PLATFORM_COMPONENT_ID: | |
623 | if (data_len < 2) | |
624 | break; | |
625 | priv->version = *(u8 *)(entry->data + 1); | |
626 | break; | |
627 | case PDR_RSSI_LINEAR_APPROXIMATION: | |
628 | case PDR_RSSI_LINEAR_APPROXIMATION_DUAL_BAND: | |
629 | case PDR_RSSI_LINEAR_APPROXIMATION_EXTENDED: | |
630 | p54_parse_rssical(dev, entry->data, data_len, | |
631 | le16_to_cpu(entry->code)); | |
632 | break; | |
633 | case PDR_RSSI_LINEAR_APPROXIMATION_CUSTOM: { | |
634 | __le16 *src = (void *) entry->data; | |
635 | s16 *dst = (void *) &priv->rssical_db; | |
636 | int i; | |
637 | ||
638 | if (data_len != sizeof(priv->rssical_db)) { | |
639 | err = -EINVAL; | |
640 | goto err; | |
641 | } | |
642 | for (i = 0; i < sizeof(priv->rssical_db) / | |
643 | sizeof(*src); i++) | |
644 | *(dst++) = (s16) le16_to_cpu(*(src++)); | |
645 | } | |
646 | break; | |
647 | case PDR_PRISM_PA_CAL_OUTPUT_POWER_LIMITS_CUSTOM: { | |
648 | struct pda_custom_wrapper *pda = (void *) entry->data; | |
649 | if (priv->output_limit || data_len < sizeof(*pda)) | |
650 | break; | |
651 | priv->output_limit = p54_convert_db(pda, data_len); | |
652 | } | |
653 | break; | |
654 | case PDR_PRISM_PA_CAL_CURVE_DATA_CUSTOM: { | |
655 | struct pda_custom_wrapper *pda = (void *) entry->data; | |
656 | if (priv->curve_data || data_len < sizeof(*pda)) | |
657 | break; | |
658 | priv->curve_data = p54_convert_db(pda, data_len); | |
659 | } | |
660 | break; | |
661 | case PDR_END: | |
d7eb50c0 CL |
662 | crc16 = ~crc_ccitt(crc16, (u8 *) entry, sizeof(*entry)); |
663 | if (crc16 != le16_to_cpup((__le16 *)entry->data)) { | |
664 | wiphy_err(dev->wiphy, "eeprom failed checksum " | |
665 | "test!\n"); | |
666 | err = -ENOMSG; | |
667 | goto err; | |
668 | } else { | |
669 | goto good_eeprom; | |
670 | } | |
4c8a32f5 CL |
671 | break; |
672 | default: | |
673 | break; | |
674 | } | |
675 | ||
d7eb50c0 CL |
676 | crc16 = crc_ccitt(crc16, (u8 *)entry, (entry_len + 1) * 2); |
677 | entry = (void *)entry + (entry_len + 1) * 2; | |
4c8a32f5 CL |
678 | } |
679 | ||
d7eb50c0 CL |
680 | wiphy_err(dev->wiphy, "unexpected end of eeprom data.\n"); |
681 | err = -ENODATA; | |
682 | goto err; | |
683 | ||
684 | good_eeprom: | |
4c8a32f5 CL |
685 | if (!synth || !priv->iq_autocal || !priv->output_limit || |
686 | !priv->curve_data) { | |
c96c31e4 JP |
687 | wiphy_err(dev->wiphy, |
688 | "not all required entries found in eeprom!\n"); | |
4c8a32f5 CL |
689 | err = -EINVAL; |
690 | goto err; | |
691 | } | |
692 | ||
1a9b6679 CL |
693 | err = p54_generate_channel_lists(dev); |
694 | if (err) | |
695 | goto err; | |
696 | ||
4c8a32f5 CL |
697 | priv->rxhw = synth & PDR_SYNTH_FRONTEND_MASK; |
698 | if (priv->rxhw == PDR_SYNTH_FRONTEND_XBOW) | |
699 | p54_init_xbow_synth(priv); | |
700 | if (!(synth & PDR_SYNTH_24_GHZ_DISABLED)) | |
1a9b6679 CL |
701 | dev->wiphy->bands[IEEE80211_BAND_2GHZ] = |
702 | priv->band_table[IEEE80211_BAND_2GHZ]; | |
4c8a32f5 | 703 | if (!(synth & PDR_SYNTH_5_GHZ_DISABLED)) |
1a9b6679 CL |
704 | dev->wiphy->bands[IEEE80211_BAND_5GHZ] = |
705 | priv->band_table[IEEE80211_BAND_5GHZ]; | |
4c8a32f5 CL |
706 | if ((synth & PDR_SYNTH_RX_DIV_MASK) == PDR_SYNTH_RX_DIV_SUPPORTED) |
707 | priv->rx_diversity_mask = 3; | |
708 | if ((synth & PDR_SYNTH_TX_DIV_MASK) == PDR_SYNTH_TX_DIV_SUPPORTED) | |
709 | priv->tx_diversity_mask = 3; | |
710 | ||
711 | if (!is_valid_ether_addr(dev->wiphy->perm_addr)) { | |
712 | u8 perm_addr[ETH_ALEN]; | |
713 | ||
c96c31e4 | 714 | wiphy_warn(dev->wiphy, |
5db55844 | 715 | "Invalid hwaddr! Using randomly generated MAC addr\n"); |
4c8a32f5 CL |
716 | random_ether_addr(perm_addr); |
717 | SET_IEEE80211_PERM_ADDR(dev, perm_addr); | |
718 | } | |
719 | ||
5db55844 | 720 | wiphy_info(dev->wiphy, "hwaddr %pM, MAC:isl38%02x RF:%s\n", |
c96c31e4 JP |
721 | dev->wiphy->perm_addr, priv->version, |
722 | p54_rf_chips[priv->rxhw]); | |
4c8a32f5 CL |
723 | |
724 | return 0; | |
725 | ||
726 | err: | |
727 | kfree(priv->iq_autocal); | |
728 | kfree(priv->output_limit); | |
729 | kfree(priv->curve_data); | |
730 | priv->iq_autocal = NULL; | |
731 | priv->output_limit = NULL; | |
732 | priv->curve_data = NULL; | |
733 | ||
c96c31e4 | 734 | wiphy_err(dev->wiphy, "eeprom parse failed!\n"); |
4c8a32f5 CL |
735 | return err; |
736 | } | |
737 | EXPORT_SYMBOL_GPL(p54_parse_eeprom); | |
738 | ||
739 | int p54_read_eeprom(struct ieee80211_hw *dev) | |
740 | { | |
741 | struct p54_common *priv = dev->priv; | |
742 | size_t eeprom_size = 0x2020, offset = 0, blocksize, maxblocksize; | |
743 | int ret = -ENOMEM; | |
e6a3f551 | 744 | void *eeprom; |
4c8a32f5 CL |
745 | |
746 | maxblocksize = EEPROM_READBACK_LEN; | |
747 | if (priv->fw_var >= 0x509) | |
748 | maxblocksize -= 0xc; | |
749 | else | |
750 | maxblocksize -= 0x4; | |
751 | ||
752 | eeprom = kzalloc(eeprom_size, GFP_KERNEL); | |
753 | if (unlikely(!eeprom)) | |
754 | goto free; | |
755 | ||
756 | while (eeprom_size) { | |
757 | blocksize = min(eeprom_size, maxblocksize); | |
758 | ret = p54_download_eeprom(priv, (void *) (eeprom + offset), | |
759 | offset, blocksize); | |
760 | if (unlikely(ret)) | |
761 | goto free; | |
762 | ||
763 | offset += blocksize; | |
764 | eeprom_size -= blocksize; | |
765 | } | |
766 | ||
767 | ret = p54_parse_eeprom(dev, eeprom, offset); | |
768 | free: | |
769 | kfree(eeprom); | |
770 | return ret; | |
771 | } | |
772 | EXPORT_SYMBOL_GPL(p54_read_eeprom); |