eeprom.c 23 KB

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