eeprom.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983
  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 <linux/export.h>
  26. #include "p54.h"
  27. #include "eeprom.h"
  28. #include "lmac.h"
  29. static struct ieee80211_rate p54_bgrates[] = {
  30. { .bitrate = 10, .hw_value = 0, },
  31. { .bitrate = 20, .hw_value = 1, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  32. { .bitrate = 55, .hw_value = 2, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  33. { .bitrate = 110, .hw_value = 3, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  34. { .bitrate = 60, .hw_value = 4, },
  35. { .bitrate = 90, .hw_value = 5, },
  36. { .bitrate = 120, .hw_value = 6, },
  37. { .bitrate = 180, .hw_value = 7, },
  38. { .bitrate = 240, .hw_value = 8, },
  39. { .bitrate = 360, .hw_value = 9, },
  40. { .bitrate = 480, .hw_value = 10, },
  41. { .bitrate = 540, .hw_value = 11, },
  42. };
  43. static struct ieee80211_rate p54_arates[] = {
  44. { .bitrate = 60, .hw_value = 4, },
  45. { .bitrate = 90, .hw_value = 5, },
  46. { .bitrate = 120, .hw_value = 6, },
  47. { .bitrate = 180, .hw_value = 7, },
  48. { .bitrate = 240, .hw_value = 8, },
  49. { .bitrate = 360, .hw_value = 9, },
  50. { .bitrate = 480, .hw_value = 10, },
  51. { .bitrate = 540, .hw_value = 11, },
  52. };
  53. static struct p54_rssi_db_entry p54_rssi_default = {
  54. /*
  55. * The defaults are taken from usb-logs of the
  56. * vendor driver. So, they should be safe to
  57. * use in case we can't get a match from the
  58. * rssi <-> dBm conversion database.
  59. */
  60. .mul = 130,
  61. .add = -398,
  62. };
  63. #define CHAN_HAS_CAL BIT(0)
  64. #define CHAN_HAS_LIMIT BIT(1)
  65. #define CHAN_HAS_CURVE BIT(2)
  66. #define CHAN_HAS_ALL (CHAN_HAS_CAL | CHAN_HAS_LIMIT | CHAN_HAS_CURVE)
  67. struct p54_channel_entry {
  68. u16 freq;
  69. u16 data;
  70. int index;
  71. int max_power;
  72. enum ieee80211_band band;
  73. };
  74. struct p54_channel_list {
  75. struct p54_channel_entry *channels;
  76. size_t entries;
  77. size_t max_entries;
  78. size_t band_channel_num[IEEE80211_NUM_BANDS];
  79. };
  80. static int p54_get_band_from_freq(u16 freq)
  81. {
  82. /* FIXME: sync these values with the 802.11 spec */
  83. if ((freq >= 2412) && (freq <= 2484))
  84. return IEEE80211_BAND_2GHZ;
  85. if ((freq >= 4920) && (freq <= 5825))
  86. return IEEE80211_BAND_5GHZ;
  87. return -1;
  88. }
  89. static int same_band(u16 freq, u16 freq2)
  90. {
  91. return p54_get_band_from_freq(freq) == p54_get_band_from_freq(freq2);
  92. }
  93. static int p54_compare_channels(const void *_a,
  94. const void *_b)
  95. {
  96. const struct p54_channel_entry *a = _a;
  97. const struct p54_channel_entry *b = _b;
  98. return a->freq - b->freq;
  99. }
  100. static int p54_compare_rssichan(const void *_a,
  101. const void *_b)
  102. {
  103. const struct p54_rssi_db_entry *a = _a;
  104. const struct p54_rssi_db_entry *b = _b;
  105. return a->freq - b->freq;
  106. }
  107. static int p54_fill_band_bitrates(struct ieee80211_hw *dev,
  108. struct ieee80211_supported_band *band_entry,
  109. enum ieee80211_band band)
  110. {
  111. /* TODO: generate rate array dynamically */
  112. switch (band) {
  113. case IEEE80211_BAND_2GHZ:
  114. band_entry->bitrates = p54_bgrates;
  115. band_entry->n_bitrates = ARRAY_SIZE(p54_bgrates);
  116. break;
  117. case IEEE80211_BAND_5GHZ:
  118. band_entry->bitrates = p54_arates;
  119. band_entry->n_bitrates = ARRAY_SIZE(p54_arates);
  120. break;
  121. default:
  122. return -EINVAL;
  123. }
  124. return 0;
  125. }
  126. static int p54_generate_band(struct ieee80211_hw *dev,
  127. struct p54_channel_list *list,
  128. unsigned int *chan_num,
  129. enum ieee80211_band band)
  130. {
  131. struct p54_common *priv = dev->priv;
  132. struct ieee80211_supported_band *tmp, *old;
  133. unsigned int i, j;
  134. int ret = -ENOMEM;
  135. if ((!list->entries) || (!list->band_channel_num[band]))
  136. return -EINVAL;
  137. tmp = kzalloc(sizeof(*tmp), GFP_KERNEL);
  138. if (!tmp)
  139. goto err_out;
  140. tmp->channels = kzalloc(sizeof(struct ieee80211_channel) *
  141. list->band_channel_num[band], 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_CHANNEL_TIME |
  171. SURVEY_INFO_CHANNEL_TIME_BUSY |
  172. SURVEY_INFO_CHANNEL_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 == IEEE80211_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 = kzalloc(sizeof(struct survey_info) * max_channel_num,
  299. GFP_KERNEL);
  300. if (!priv->survey) {
  301. ret = -ENOMEM;
  302. goto free;
  303. }
  304. list->max_entries = max_channel_num;
  305. list->channels = kzalloc(sizeof(struct p54_channel_entry) *
  306. max_channel_num, GFP_KERNEL);
  307. if (!list->channels) {
  308. ret = -ENOMEM;
  309. goto free;
  310. }
  311. for (i = 0; i < max_channel_num; i++) {
  312. if (i < priv->iq_autocal_len) {
  313. freq = le16_to_cpu(priv->iq_autocal[i].freq);
  314. p54_update_channel_param(list, freq, CHAN_HAS_CAL);
  315. }
  316. if (i < priv->output_limit->entries) {
  317. struct p54_channel_entry *tmp;
  318. void *data = (void *) ((unsigned long) i *
  319. priv->output_limit->entry_size +
  320. priv->output_limit->offset +
  321. priv->output_limit->data);
  322. freq = le16_to_cpup((__le16 *) data);
  323. tmp = p54_update_channel_param(list, freq,
  324. CHAN_HAS_LIMIT);
  325. if (tmp) {
  326. tmp->max_power = p54_get_maxpower(priv, data);
  327. }
  328. }
  329. if (i < priv->curve_data->entries) {
  330. freq = le16_to_cpup((__le16 *) (i *
  331. priv->curve_data->entry_size +
  332. priv->curve_data->offset +
  333. priv->curve_data->data));
  334. p54_update_channel_param(list, freq, CHAN_HAS_CURVE);
  335. }
  336. }
  337. /* sort the channel list by frequency */
  338. sort(list->channels, list->entries, sizeof(struct p54_channel_entry),
  339. p54_compare_channels, NULL);
  340. k = 0;
  341. for (i = 0, j = 0; i < IEEE80211_NUM_BANDS; i++) {
  342. if (p54_generate_band(dev, list, &k, i) == 0)
  343. j++;
  344. }
  345. if (j == 0) {
  346. /* no useable band available. */
  347. ret = -EINVAL;
  348. }
  349. free:
  350. if (list) {
  351. kfree(list->channels);
  352. kfree(list);
  353. }
  354. if (ret) {
  355. kfree(priv->survey);
  356. priv->survey = NULL;
  357. }
  358. return ret;
  359. }
  360. static int p54_convert_rev0(struct ieee80211_hw *dev,
  361. struct pda_pa_curve_data *curve_data)
  362. {
  363. struct p54_common *priv = dev->priv;
  364. struct p54_pa_curve_data_sample *dst;
  365. struct pda_pa_curve_data_sample_rev0 *src;
  366. size_t cd_len = sizeof(*curve_data) +
  367. (curve_data->points_per_channel*sizeof(*dst) + 2) *
  368. curve_data->channels;
  369. unsigned int i, j;
  370. void *source, *target;
  371. priv->curve_data = kmalloc(sizeof(*priv->curve_data) + cd_len,
  372. GFP_KERNEL);
  373. if (!priv->curve_data)
  374. return -ENOMEM;
  375. priv->curve_data->entries = curve_data->channels;
  376. priv->curve_data->entry_size = sizeof(__le16) +
  377. sizeof(*dst) * curve_data->points_per_channel;
  378. priv->curve_data->offset = offsetof(struct pda_pa_curve_data, data);
  379. priv->curve_data->len = cd_len;
  380. memcpy(priv->curve_data->data, curve_data, sizeof(*curve_data));
  381. source = curve_data->data;
  382. target = ((struct pda_pa_curve_data *) priv->curve_data->data)->data;
  383. for (i = 0; i < curve_data->channels; i++) {
  384. __le16 *freq = source;
  385. source += sizeof(__le16);
  386. *((__le16 *)target) = *freq;
  387. target += sizeof(__le16);
  388. for (j = 0; j < curve_data->points_per_channel; j++) {
  389. dst = target;
  390. src = source;
  391. dst->rf_power = src->rf_power;
  392. dst->pa_detector = src->pa_detector;
  393. dst->data_64qam = src->pcv;
  394. /* "invent" the points for the other modulations */
  395. #define SUB(x, y) (u8)(((x) - (y)) > (x) ? 0 : (x) - (y))
  396. dst->data_16qam = SUB(src->pcv, 12);
  397. dst->data_qpsk = SUB(dst->data_16qam, 12);
  398. dst->data_bpsk = SUB(dst->data_qpsk, 12);
  399. dst->data_barker = SUB(dst->data_bpsk, 14);
  400. #undef SUB
  401. target += sizeof(*dst);
  402. source += sizeof(*src);
  403. }
  404. }
  405. return 0;
  406. }
  407. static int p54_convert_rev1(struct ieee80211_hw *dev,
  408. struct pda_pa_curve_data *curve_data)
  409. {
  410. struct p54_common *priv = dev->priv;
  411. struct p54_pa_curve_data_sample *dst;
  412. struct pda_pa_curve_data_sample_rev1 *src;
  413. size_t cd_len = sizeof(*curve_data) +
  414. (curve_data->points_per_channel*sizeof(*dst) + 2) *
  415. curve_data->channels;
  416. unsigned int i, j;
  417. void *source, *target;
  418. priv->curve_data = kzalloc(cd_len + sizeof(*priv->curve_data),
  419. GFP_KERNEL);
  420. if (!priv->curve_data)
  421. return -ENOMEM;
  422. priv->curve_data->entries = curve_data->channels;
  423. priv->curve_data->entry_size = sizeof(__le16) +
  424. sizeof(*dst) * curve_data->points_per_channel;
  425. priv->curve_data->offset = offsetof(struct pda_pa_curve_data, data);
  426. priv->curve_data->len = cd_len;
  427. memcpy(priv->curve_data->data, curve_data, sizeof(*curve_data));
  428. source = curve_data->data;
  429. target = ((struct pda_pa_curve_data *) priv->curve_data->data)->data;
  430. for (i = 0; i < curve_data->channels; i++) {
  431. __le16 *freq = source;
  432. source += sizeof(__le16);
  433. *((__le16 *)target) = *freq;
  434. target += sizeof(__le16);
  435. for (j = 0; j < curve_data->points_per_channel; j++) {
  436. memcpy(target, source, sizeof(*src));
  437. target += sizeof(*dst);
  438. source += sizeof(*src);
  439. }
  440. source++;
  441. }
  442. return 0;
  443. }
  444. static const char *p54_rf_chips[] = { "INVALID-0", "Duette3", "Duette2",
  445. "Frisbee", "Xbow", "Longbow", "INVALID-6", "INVALID-7" };
  446. static int p54_parse_rssical(struct ieee80211_hw *dev,
  447. u8 *data, int len, u16 type)
  448. {
  449. struct p54_common *priv = dev->priv;
  450. struct p54_rssi_db_entry *entry;
  451. size_t db_len, entries;
  452. int offset = 0, i;
  453. if (type != PDR_RSSI_LINEAR_APPROXIMATION_EXTENDED) {
  454. entries = (type == PDR_RSSI_LINEAR_APPROXIMATION) ? 1 : 2;
  455. if (len != sizeof(struct pda_rssi_cal_entry) * entries) {
  456. wiphy_err(dev->wiphy, "rssical size mismatch.\n");
  457. goto err_data;
  458. }
  459. } else {
  460. /*
  461. * Some devices (Dell 1450 USB, Xbow 5GHz card, etc...)
  462. * have an empty two byte header.
  463. */
  464. if (*((__le16 *)&data[offset]) == cpu_to_le16(0))
  465. offset += 2;
  466. entries = (len - offset) /
  467. sizeof(struct pda_rssi_cal_ext_entry);
  468. if (len < offset ||
  469. (len - offset) % sizeof(struct pda_rssi_cal_ext_entry) ||
  470. entries == 0) {
  471. wiphy_err(dev->wiphy, "invalid rssi database.\n");
  472. goto err_data;
  473. }
  474. }
  475. db_len = sizeof(*entry) * entries;
  476. priv->rssi_db = kzalloc(db_len + sizeof(*priv->rssi_db), GFP_KERNEL);
  477. if (!priv->rssi_db)
  478. return -ENOMEM;
  479. priv->rssi_db->offset = 0;
  480. priv->rssi_db->entries = entries;
  481. priv->rssi_db->entry_size = sizeof(*entry);
  482. priv->rssi_db->len = db_len;
  483. entry = (void *)((unsigned long)priv->rssi_db->data + priv->rssi_db->offset);
  484. if (type == PDR_RSSI_LINEAR_APPROXIMATION_EXTENDED) {
  485. struct pda_rssi_cal_ext_entry *cal = (void *) &data[offset];
  486. for (i = 0; i < entries; i++) {
  487. entry[i].freq = le16_to_cpu(cal[i].freq);
  488. entry[i].mul = (s16) le16_to_cpu(cal[i].mul);
  489. entry[i].add = (s16) le16_to_cpu(cal[i].add);
  490. }
  491. } else {
  492. struct pda_rssi_cal_entry *cal = (void *) &data[offset];
  493. for (i = 0; i < entries; i++) {
  494. u16 freq = 0;
  495. switch (i) {
  496. case IEEE80211_BAND_2GHZ:
  497. freq = 2437;
  498. break;
  499. case IEEE80211_BAND_5GHZ:
  500. freq = 5240;
  501. break;
  502. }
  503. entry[i].freq = freq;
  504. entry[i].mul = (s16) le16_to_cpu(cal[i].mul);
  505. entry[i].add = (s16) le16_to_cpu(cal[i].add);
  506. }
  507. }
  508. /* sort the list by channel frequency */
  509. sort(entry, entries, sizeof(*entry), p54_compare_rssichan, NULL);
  510. return 0;
  511. err_data:
  512. wiphy_err(dev->wiphy,
  513. "rssi calibration data packing type:(%x) len:%d.\n",
  514. type, len);
  515. print_hex_dump_bytes("rssical:", DUMP_PREFIX_NONE, data, len);
  516. wiphy_err(dev->wiphy, "please report this issue.\n");
  517. return -EINVAL;
  518. }
  519. struct p54_rssi_db_entry *p54_rssi_find(struct p54_common *priv, const u16 freq)
  520. {
  521. struct p54_rssi_db_entry *entry;
  522. int i, found = -1;
  523. if (!priv->rssi_db)
  524. return &p54_rssi_default;
  525. entry = (void *)(priv->rssi_db->data + priv->rssi_db->offset);
  526. for (i = 0; i < priv->rssi_db->entries; i++) {
  527. if (!same_band(freq, entry[i].freq))
  528. continue;
  529. if (found == -1) {
  530. found = i;
  531. continue;
  532. }
  533. /* nearest match */
  534. if (abs(freq - entry[i].freq) <
  535. abs(freq - entry[found].freq)) {
  536. found = i;
  537. continue;
  538. } else {
  539. break;
  540. }
  541. }
  542. return found < 0 ? &p54_rssi_default : &entry[found];
  543. }
  544. static void p54_parse_default_country(struct ieee80211_hw *dev,
  545. void *data, int len)
  546. {
  547. struct pda_country *country;
  548. if (len != sizeof(*country)) {
  549. wiphy_err(dev->wiphy,
  550. "found possible invalid default country eeprom entry. (entry size: %d)\n",
  551. len);
  552. print_hex_dump_bytes("country:", DUMP_PREFIX_NONE,
  553. data, len);
  554. wiphy_err(dev->wiphy, "please report this issue.\n");
  555. return;
  556. }
  557. country = (struct pda_country *) data;
  558. if (country->flags == PDR_COUNTRY_CERT_CODE_PSEUDO)
  559. regulatory_hint(dev->wiphy, country->alpha2);
  560. else {
  561. /* TODO:
  562. * write a shared/common function that converts
  563. * "Regulatory domain codes" (802.11-2007 14.8.2.2)
  564. * into ISO/IEC 3166-1 alpha2 for regulatory_hint.
  565. */
  566. }
  567. }
  568. static int p54_convert_output_limits(struct ieee80211_hw *dev,
  569. u8 *data, size_t len)
  570. {
  571. struct p54_common *priv = dev->priv;
  572. if (len < 2)
  573. return -EINVAL;
  574. if (data[0] != 0) {
  575. wiphy_err(dev->wiphy, "unknown output power db revision:%x\n",
  576. data[0]);
  577. return -EINVAL;
  578. }
  579. if (2 + data[1] * sizeof(struct pda_channel_output_limit) > len)
  580. return -EINVAL;
  581. priv->output_limit = kmalloc(data[1] *
  582. sizeof(struct pda_channel_output_limit) +
  583. sizeof(*priv->output_limit), GFP_KERNEL);
  584. if (!priv->output_limit)
  585. return -ENOMEM;
  586. priv->output_limit->offset = 0;
  587. priv->output_limit->entries = data[1];
  588. priv->output_limit->entry_size =
  589. sizeof(struct pda_channel_output_limit);
  590. priv->output_limit->len = priv->output_limit->entry_size *
  591. priv->output_limit->entries +
  592. priv->output_limit->offset;
  593. memcpy(priv->output_limit->data, &data[2],
  594. data[1] * sizeof(struct pda_channel_output_limit));
  595. return 0;
  596. }
  597. static struct p54_cal_database *p54_convert_db(struct pda_custom_wrapper *src,
  598. size_t total_len)
  599. {
  600. struct p54_cal_database *dst;
  601. size_t payload_len, entries, entry_size, offset;
  602. payload_len = le16_to_cpu(src->len);
  603. entries = le16_to_cpu(src->entries);
  604. entry_size = le16_to_cpu(src->entry_size);
  605. offset = le16_to_cpu(src->offset);
  606. if (((entries * entry_size + offset) != payload_len) ||
  607. (payload_len + sizeof(*src) != total_len))
  608. return NULL;
  609. dst = kmalloc(sizeof(*dst) + payload_len, GFP_KERNEL);
  610. if (!dst)
  611. return NULL;
  612. dst->entries = entries;
  613. dst->entry_size = entry_size;
  614. dst->offset = offset;
  615. dst->len = payload_len;
  616. memcpy(dst->data, src->data, payload_len);
  617. return dst;
  618. }
  619. int p54_parse_eeprom(struct ieee80211_hw *dev, void *eeprom, int len)
  620. {
  621. struct p54_common *priv = dev->priv;
  622. struct eeprom_pda_wrap *wrap;
  623. struct pda_entry *entry;
  624. unsigned int data_len, entry_len;
  625. void *tmp;
  626. int err;
  627. u8 *end = (u8 *)eeprom + len;
  628. u16 synth = 0;
  629. u16 crc16 = ~0;
  630. wrap = (struct eeprom_pda_wrap *) eeprom;
  631. entry = (void *)wrap->data + le16_to_cpu(wrap->len);
  632. /* verify that at least the entry length/code fits */
  633. while ((u8 *)entry <= end - sizeof(*entry)) {
  634. entry_len = le16_to_cpu(entry->len);
  635. data_len = ((entry_len - 1) << 1);
  636. /* abort if entry exceeds whole structure */
  637. if ((u8 *)entry + sizeof(*entry) + data_len > end)
  638. break;
  639. switch (le16_to_cpu(entry->code)) {
  640. case PDR_MAC_ADDRESS:
  641. if (data_len != ETH_ALEN)
  642. break;
  643. SET_IEEE80211_PERM_ADDR(dev, entry->data);
  644. break;
  645. case PDR_PRISM_PA_CAL_OUTPUT_POWER_LIMITS:
  646. if (priv->output_limit)
  647. break;
  648. err = p54_convert_output_limits(dev, entry->data,
  649. data_len);
  650. if (err)
  651. goto err;
  652. break;
  653. case PDR_PRISM_PA_CAL_CURVE_DATA: {
  654. struct pda_pa_curve_data *curve_data =
  655. (struct pda_pa_curve_data *)entry->data;
  656. if (data_len < sizeof(*curve_data)) {
  657. err = -EINVAL;
  658. goto err;
  659. }
  660. switch (curve_data->cal_method_rev) {
  661. case 0:
  662. err = p54_convert_rev0(dev, curve_data);
  663. break;
  664. case 1:
  665. err = p54_convert_rev1(dev, curve_data);
  666. break;
  667. default:
  668. wiphy_err(dev->wiphy,
  669. "unknown curve data revision %d\n",
  670. curve_data->cal_method_rev);
  671. err = -ENODEV;
  672. break;
  673. }
  674. if (err)
  675. goto err;
  676. }
  677. break;
  678. case PDR_PRISM_ZIF_TX_IQ_CALIBRATION:
  679. priv->iq_autocal = kmemdup(entry->data, data_len,
  680. GFP_KERNEL);
  681. if (!priv->iq_autocal) {
  682. err = -ENOMEM;
  683. goto err;
  684. }
  685. priv->iq_autocal_len = data_len / sizeof(struct pda_iq_autocal_entry);
  686. break;
  687. case PDR_DEFAULT_COUNTRY:
  688. p54_parse_default_country(dev, entry->data, data_len);
  689. break;
  690. case PDR_INTERFACE_LIST:
  691. tmp = entry->data;
  692. while ((u8 *)tmp < entry->data + data_len) {
  693. struct exp_if *exp_if = tmp;
  694. if (exp_if->if_id == cpu_to_le16(IF_ID_ISL39000))
  695. synth = le16_to_cpu(exp_if->variant);
  696. tmp += sizeof(*exp_if);
  697. }
  698. break;
  699. case PDR_HARDWARE_PLATFORM_COMPONENT_ID:
  700. if (data_len < 2)
  701. break;
  702. priv->version = *(u8 *)(entry->data + 1);
  703. break;
  704. case PDR_RSSI_LINEAR_APPROXIMATION:
  705. case PDR_RSSI_LINEAR_APPROXIMATION_DUAL_BAND:
  706. case PDR_RSSI_LINEAR_APPROXIMATION_EXTENDED:
  707. err = p54_parse_rssical(dev, entry->data, data_len,
  708. le16_to_cpu(entry->code));
  709. if (err)
  710. goto err;
  711. break;
  712. case PDR_RSSI_LINEAR_APPROXIMATION_CUSTOMV2: {
  713. struct pda_custom_wrapper *pda = (void *) entry->data;
  714. __le16 *src;
  715. u16 *dst;
  716. int i;
  717. if (priv->rssi_db || data_len < sizeof(*pda))
  718. break;
  719. priv->rssi_db = p54_convert_db(pda, data_len);
  720. if (!priv->rssi_db)
  721. break;
  722. src = (void *) priv->rssi_db->data;
  723. dst = (void *) priv->rssi_db->data;
  724. for (i = 0; i < priv->rssi_db->entries; i++)
  725. *(dst++) = (s16) le16_to_cpu(*(src++));
  726. }
  727. break;
  728. case PDR_PRISM_PA_CAL_OUTPUT_POWER_LIMITS_CUSTOM: {
  729. struct pda_custom_wrapper *pda = (void *) entry->data;
  730. if (priv->output_limit || data_len < sizeof(*pda))
  731. break;
  732. priv->output_limit = p54_convert_db(pda, data_len);
  733. }
  734. break;
  735. case PDR_PRISM_PA_CAL_CURVE_DATA_CUSTOM: {
  736. struct pda_custom_wrapper *pda = (void *) entry->data;
  737. if (priv->curve_data || data_len < sizeof(*pda))
  738. break;
  739. priv->curve_data = p54_convert_db(pda, data_len);
  740. }
  741. break;
  742. case PDR_END:
  743. crc16 = ~crc_ccitt(crc16, (u8 *) entry, sizeof(*entry));
  744. if (crc16 != le16_to_cpup((__le16 *)entry->data)) {
  745. wiphy_err(dev->wiphy, "eeprom failed checksum "
  746. "test!\n");
  747. err = -ENOMSG;
  748. goto err;
  749. } else {
  750. goto good_eeprom;
  751. }
  752. break;
  753. default:
  754. break;
  755. }
  756. crc16 = crc_ccitt(crc16, (u8 *)entry, (entry_len + 1) * 2);
  757. entry = (void *)entry + (entry_len + 1) * 2;
  758. }
  759. wiphy_err(dev->wiphy, "unexpected end of eeprom data.\n");
  760. err = -ENODATA;
  761. goto err;
  762. good_eeprom:
  763. if (!synth || !priv->iq_autocal || !priv->output_limit ||
  764. !priv->curve_data) {
  765. wiphy_err(dev->wiphy,
  766. "not all required entries found in eeprom!\n");
  767. err = -EINVAL;
  768. goto err;
  769. }
  770. priv->rxhw = synth & PDR_SYNTH_FRONTEND_MASK;
  771. err = p54_generate_channel_lists(dev);
  772. if (err)
  773. goto err;
  774. if (priv->rxhw == PDR_SYNTH_FRONTEND_XBOW)
  775. p54_init_xbow_synth(priv);
  776. if (!(synth & PDR_SYNTH_24_GHZ_DISABLED))
  777. dev->wiphy->bands[IEEE80211_BAND_2GHZ] =
  778. priv->band_table[IEEE80211_BAND_2GHZ];
  779. if (!(synth & PDR_SYNTH_5_GHZ_DISABLED))
  780. dev->wiphy->bands[IEEE80211_BAND_5GHZ] =
  781. priv->band_table[IEEE80211_BAND_5GHZ];
  782. if ((synth & PDR_SYNTH_RX_DIV_MASK) == PDR_SYNTH_RX_DIV_SUPPORTED)
  783. priv->rx_diversity_mask = 3;
  784. if ((synth & PDR_SYNTH_TX_DIV_MASK) == PDR_SYNTH_TX_DIV_SUPPORTED)
  785. priv->tx_diversity_mask = 3;
  786. if (!is_valid_ether_addr(dev->wiphy->perm_addr)) {
  787. u8 perm_addr[ETH_ALEN];
  788. wiphy_warn(dev->wiphy,
  789. "Invalid hwaddr! Using randomly generated MAC addr\n");
  790. eth_random_addr(perm_addr);
  791. SET_IEEE80211_PERM_ADDR(dev, perm_addr);
  792. }
  793. priv->cur_rssi = &p54_rssi_default;
  794. wiphy_info(dev->wiphy, "hwaddr %pM, MAC:isl38%02x RF:%s\n",
  795. dev->wiphy->perm_addr, priv->version,
  796. p54_rf_chips[priv->rxhw]);
  797. return 0;
  798. err:
  799. kfree(priv->iq_autocal);
  800. kfree(priv->output_limit);
  801. kfree(priv->curve_data);
  802. kfree(priv->rssi_db);
  803. kfree(priv->survey);
  804. priv->iq_autocal = NULL;
  805. priv->output_limit = NULL;
  806. priv->curve_data = NULL;
  807. priv->rssi_db = NULL;
  808. priv->survey = NULL;
  809. wiphy_err(dev->wiphy, "eeprom parse failed!\n");
  810. return err;
  811. }
  812. EXPORT_SYMBOL_GPL(p54_parse_eeprom);
  813. int p54_read_eeprom(struct ieee80211_hw *dev)
  814. {
  815. struct p54_common *priv = dev->priv;
  816. size_t eeprom_size = 0x2020, offset = 0, blocksize, maxblocksize;
  817. int ret = -ENOMEM;
  818. void *eeprom;
  819. maxblocksize = EEPROM_READBACK_LEN;
  820. if (priv->fw_var >= 0x509)
  821. maxblocksize -= 0xc;
  822. else
  823. maxblocksize -= 0x4;
  824. eeprom = kzalloc(eeprom_size, GFP_KERNEL);
  825. if (unlikely(!eeprom))
  826. goto free;
  827. while (eeprom_size) {
  828. blocksize = min(eeprom_size, maxblocksize);
  829. ret = p54_download_eeprom(priv, eeprom + offset,
  830. offset, blocksize);
  831. if (unlikely(ret))
  832. goto free;
  833. offset += blocksize;
  834. eeprom_size -= blocksize;
  835. }
  836. ret = p54_parse_eeprom(dev, eeprom, offset);
  837. free:
  838. kfree(eeprom);
  839. return ret;
  840. }
  841. EXPORT_SYMBOL_GPL(p54_read_eeprom);