eeprom.c 25 KB

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