eeprom.c 25 KB

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