eeprom.c 23 KB

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