eeprom.c 20 KB

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