eeprom.c 23 KB

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