p54common.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491
  1. /*
  2. * Common code for mac80211 Prism54 drivers
  3. *
  4. * Copyright (c) 2006, Michael Wu <flamingice@sourmilk.net>
  5. * Copyright (c) 2007, Christian Lamparter <chunkeey@web.de>
  6. * Copyright 2008, Johannes Berg <johannes@sipsolutions.net>
  7. *
  8. * Based on the islsm (softmac prism54) driver, which is:
  9. * Copyright 2004-2006 Jean-Baptiste Note <jbnote@gmail.com>, et al.
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License version 2 as
  13. * published by the Free Software Foundation.
  14. */
  15. #include <linux/init.h>
  16. #include <linux/firmware.h>
  17. #include <linux/etherdevice.h>
  18. #include <net/mac80211.h>
  19. #include "p54.h"
  20. #include "p54common.h"
  21. MODULE_AUTHOR("Michael Wu <flamingice@sourmilk.net>");
  22. MODULE_DESCRIPTION("Softmac Prism54 common code");
  23. MODULE_LICENSE("GPL");
  24. MODULE_ALIAS("prism54common");
  25. static struct ieee80211_rate p54_bgrates[] = {
  26. { .bitrate = 10, .hw_value = 0, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  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_channel p54_bgchannels[] = {
  40. { .center_freq = 2412, .hw_value = 1, },
  41. { .center_freq = 2417, .hw_value = 2, },
  42. { .center_freq = 2422, .hw_value = 3, },
  43. { .center_freq = 2427, .hw_value = 4, },
  44. { .center_freq = 2432, .hw_value = 5, },
  45. { .center_freq = 2437, .hw_value = 6, },
  46. { .center_freq = 2442, .hw_value = 7, },
  47. { .center_freq = 2447, .hw_value = 8, },
  48. { .center_freq = 2452, .hw_value = 9, },
  49. { .center_freq = 2457, .hw_value = 10, },
  50. { .center_freq = 2462, .hw_value = 11, },
  51. { .center_freq = 2467, .hw_value = 12, },
  52. { .center_freq = 2472, .hw_value = 13, },
  53. { .center_freq = 2484, .hw_value = 14, },
  54. };
  55. static struct ieee80211_supported_band band_2GHz = {
  56. .channels = p54_bgchannels,
  57. .n_channels = ARRAY_SIZE(p54_bgchannels),
  58. .bitrates = p54_bgrates,
  59. .n_bitrates = ARRAY_SIZE(p54_bgrates),
  60. };
  61. static struct ieee80211_rate p54_arates[] = {
  62. { .bitrate = 60, .hw_value = 4, },
  63. { .bitrate = 90, .hw_value = 5, },
  64. { .bitrate = 120, .hw_value = 6, },
  65. { .bitrate = 180, .hw_value = 7, },
  66. { .bitrate = 240, .hw_value = 8, },
  67. { .bitrate = 360, .hw_value = 9, },
  68. { .bitrate = 480, .hw_value = 10, },
  69. { .bitrate = 540, .hw_value = 11, },
  70. };
  71. static struct ieee80211_channel p54_achannels[] = {
  72. { .center_freq = 4920 },
  73. { .center_freq = 4940 },
  74. { .center_freq = 4960 },
  75. { .center_freq = 4980 },
  76. { .center_freq = 5040 },
  77. { .center_freq = 5060 },
  78. { .center_freq = 5080 },
  79. { .center_freq = 5170 },
  80. { .center_freq = 5180 },
  81. { .center_freq = 5190 },
  82. { .center_freq = 5200 },
  83. { .center_freq = 5210 },
  84. { .center_freq = 5220 },
  85. { .center_freq = 5230 },
  86. { .center_freq = 5240 },
  87. { .center_freq = 5260 },
  88. { .center_freq = 5280 },
  89. { .center_freq = 5300 },
  90. { .center_freq = 5320 },
  91. { .center_freq = 5500 },
  92. { .center_freq = 5520 },
  93. { .center_freq = 5540 },
  94. { .center_freq = 5560 },
  95. { .center_freq = 5580 },
  96. { .center_freq = 5600 },
  97. { .center_freq = 5620 },
  98. { .center_freq = 5640 },
  99. { .center_freq = 5660 },
  100. { .center_freq = 5680 },
  101. { .center_freq = 5700 },
  102. { .center_freq = 5745 },
  103. { .center_freq = 5765 },
  104. { .center_freq = 5785 },
  105. { .center_freq = 5805 },
  106. { .center_freq = 5825 },
  107. };
  108. static struct ieee80211_supported_band band_5GHz = {
  109. .channels = p54_achannels,
  110. .n_channels = ARRAY_SIZE(p54_achannels),
  111. .bitrates = p54_arates,
  112. .n_bitrates = ARRAY_SIZE(p54_arates),
  113. };
  114. int p54_parse_firmware(struct ieee80211_hw *dev, const struct firmware *fw)
  115. {
  116. struct p54_common *priv = dev->priv;
  117. struct bootrec_exp_if *exp_if;
  118. struct bootrec *bootrec;
  119. u32 *data = (u32 *)fw->data;
  120. u32 *end_data = (u32 *)fw->data + (fw->size >> 2);
  121. u8 *fw_version = NULL;
  122. size_t len;
  123. int i;
  124. if (priv->rx_start)
  125. return 0;
  126. while (data < end_data && *data)
  127. data++;
  128. while (data < end_data && !*data)
  129. data++;
  130. bootrec = (struct bootrec *) data;
  131. while (bootrec->data <= end_data &&
  132. (bootrec->data + (len = le32_to_cpu(bootrec->len))) <= end_data) {
  133. u32 code = le32_to_cpu(bootrec->code);
  134. switch (code) {
  135. case BR_CODE_COMPONENT_ID:
  136. priv->fw_interface = be32_to_cpup((__be32 *)
  137. bootrec->data);
  138. switch (priv->fw_interface) {
  139. case FW_FMAC:
  140. printk(KERN_INFO "p54: FreeMAC firmware\n");
  141. break;
  142. case FW_LM20:
  143. printk(KERN_INFO "p54: LM20 firmware\n");
  144. break;
  145. case FW_LM86:
  146. printk(KERN_INFO "p54: LM86 firmware\n");
  147. break;
  148. case FW_LM87:
  149. printk(KERN_INFO "p54: LM87 firmware\n");
  150. break;
  151. default:
  152. printk(KERN_INFO "p54: unknown firmware\n");
  153. break;
  154. }
  155. break;
  156. case BR_CODE_COMPONENT_VERSION:
  157. /* 24 bytes should be enough for all firmwares */
  158. if (strnlen((unsigned char*)bootrec->data, 24) < 24)
  159. fw_version = (unsigned char*)bootrec->data;
  160. break;
  161. case BR_CODE_DESCR: {
  162. struct bootrec_desc *desc =
  163. (struct bootrec_desc *)bootrec->data;
  164. priv->rx_start = le32_to_cpu(desc->rx_start);
  165. /* FIXME add sanity checking */
  166. priv->rx_end = le32_to_cpu(desc->rx_end) - 0x3500;
  167. priv->headroom = desc->headroom;
  168. priv->tailroom = desc->tailroom;
  169. if (le32_to_cpu(bootrec->len) == 11)
  170. priv->rx_mtu = le16_to_cpu(desc->rx_mtu);
  171. else
  172. priv->rx_mtu = (size_t)
  173. 0x620 - priv->tx_hdr_len;
  174. break;
  175. }
  176. case BR_CODE_EXPOSED_IF:
  177. exp_if = (struct bootrec_exp_if *) bootrec->data;
  178. for (i = 0; i < (len * sizeof(*exp_if) / 4); i++)
  179. if (exp_if[i].if_id == cpu_to_le16(0x1a))
  180. priv->fw_var = le16_to_cpu(exp_if[i].variant);
  181. break;
  182. case BR_CODE_DEPENDENT_IF:
  183. break;
  184. case BR_CODE_END_OF_BRA:
  185. case LEGACY_BR_CODE_END_OF_BRA:
  186. end_data = NULL;
  187. break;
  188. default:
  189. break;
  190. }
  191. bootrec = (struct bootrec *)&bootrec->data[len];
  192. }
  193. if (fw_version)
  194. printk(KERN_INFO "p54: FW rev %s - Softmac protocol %x.%x\n",
  195. fw_version, priv->fw_var >> 8, priv->fw_var & 0xff);
  196. if (priv->fw_var >= 0x300) {
  197. /* Firmware supports QoS, use it! */
  198. priv->tx_stats[4].limit = 3;
  199. priv->tx_stats[5].limit = 4;
  200. priv->tx_stats[6].limit = 3;
  201. priv->tx_stats[7].limit = 1;
  202. dev->queues = 4;
  203. }
  204. return 0;
  205. }
  206. EXPORT_SYMBOL_GPL(p54_parse_firmware);
  207. static int p54_convert_rev0(struct ieee80211_hw *dev,
  208. struct pda_pa_curve_data *curve_data)
  209. {
  210. struct p54_common *priv = dev->priv;
  211. struct p54_pa_curve_data_sample *dst;
  212. struct pda_pa_curve_data_sample_rev0 *src;
  213. size_t cd_len = sizeof(*curve_data) +
  214. (curve_data->points_per_channel*sizeof(*dst) + 2) *
  215. curve_data->channels;
  216. unsigned int i, j;
  217. void *source, *target;
  218. priv->curve_data = kmalloc(cd_len, GFP_KERNEL);
  219. if (!priv->curve_data)
  220. return -ENOMEM;
  221. memcpy(priv->curve_data, curve_data, sizeof(*curve_data));
  222. source = curve_data->data;
  223. target = priv->curve_data->data;
  224. for (i = 0; i < curve_data->channels; i++) {
  225. __le16 *freq = source;
  226. source += sizeof(__le16);
  227. *((__le16 *)target) = *freq;
  228. target += sizeof(__le16);
  229. for (j = 0; j < curve_data->points_per_channel; j++) {
  230. dst = target;
  231. src = source;
  232. dst->rf_power = src->rf_power;
  233. dst->pa_detector = src->pa_detector;
  234. dst->data_64qam = src->pcv;
  235. /* "invent" the points for the other modulations */
  236. #define SUB(x,y) (u8)((x) - (y)) > (x) ? 0 : (x) - (y)
  237. dst->data_16qam = SUB(src->pcv, 12);
  238. dst->data_qpsk = SUB(dst->data_16qam, 12);
  239. dst->data_bpsk = SUB(dst->data_qpsk, 12);
  240. dst->data_barker = SUB(dst->data_bpsk, 14);
  241. #undef SUB
  242. target += sizeof(*dst);
  243. source += sizeof(*src);
  244. }
  245. }
  246. return 0;
  247. }
  248. static int p54_convert_rev1(struct ieee80211_hw *dev,
  249. struct pda_pa_curve_data *curve_data)
  250. {
  251. struct p54_common *priv = dev->priv;
  252. struct p54_pa_curve_data_sample *dst;
  253. struct pda_pa_curve_data_sample_rev1 *src;
  254. size_t cd_len = sizeof(*curve_data) +
  255. (curve_data->points_per_channel*sizeof(*dst) + 2) *
  256. curve_data->channels;
  257. unsigned int i, j;
  258. void *source, *target;
  259. priv->curve_data = kmalloc(cd_len, GFP_KERNEL);
  260. if (!priv->curve_data)
  261. return -ENOMEM;
  262. memcpy(priv->curve_data, curve_data, sizeof(*curve_data));
  263. source = curve_data->data;
  264. target = priv->curve_data->data;
  265. for (i = 0; i < curve_data->channels; i++) {
  266. __le16 *freq = source;
  267. source += sizeof(__le16);
  268. *((__le16 *)target) = *freq;
  269. target += sizeof(__le16);
  270. for (j = 0; j < curve_data->points_per_channel; j++) {
  271. memcpy(target, source, sizeof(*src));
  272. target += sizeof(*dst);
  273. source += sizeof(*src);
  274. }
  275. source++;
  276. }
  277. return 0;
  278. }
  279. static const char *p54_rf_chips[] = { "NULL", "Duette3", "Duette2",
  280. "Frisbee", "Xbow", "Longbow", "NULL", "NULL" };
  281. static int p54_init_xbow_synth(struct ieee80211_hw *dev);
  282. static int p54_parse_eeprom(struct ieee80211_hw *dev, void *eeprom, int len)
  283. {
  284. struct p54_common *priv = dev->priv;
  285. struct eeprom_pda_wrap *wrap = NULL;
  286. struct pda_entry *entry;
  287. unsigned int data_len, entry_len;
  288. void *tmp;
  289. int err;
  290. u8 *end = (u8 *)eeprom + len;
  291. u16 synth = 0;
  292. wrap = (struct eeprom_pda_wrap *) eeprom;
  293. entry = (void *)wrap->data + le16_to_cpu(wrap->len);
  294. /* verify that at least the entry length/code fits */
  295. while ((u8 *)entry <= end - sizeof(*entry)) {
  296. entry_len = le16_to_cpu(entry->len);
  297. data_len = ((entry_len - 1) << 1);
  298. /* abort if entry exceeds whole structure */
  299. if ((u8 *)entry + sizeof(*entry) + data_len > end)
  300. break;
  301. switch (le16_to_cpu(entry->code)) {
  302. case PDR_MAC_ADDRESS:
  303. SET_IEEE80211_PERM_ADDR(dev, entry->data);
  304. break;
  305. case PDR_PRISM_PA_CAL_OUTPUT_POWER_LIMITS:
  306. if (data_len < 2) {
  307. err = -EINVAL;
  308. goto err;
  309. }
  310. if (2 + entry->data[1]*sizeof(*priv->output_limit) > data_len) {
  311. err = -EINVAL;
  312. goto err;
  313. }
  314. priv->output_limit = kmalloc(entry->data[1] *
  315. sizeof(*priv->output_limit), GFP_KERNEL);
  316. if (!priv->output_limit) {
  317. err = -ENOMEM;
  318. goto err;
  319. }
  320. memcpy(priv->output_limit, &entry->data[2],
  321. entry->data[1]*sizeof(*priv->output_limit));
  322. priv->output_limit_len = entry->data[1];
  323. break;
  324. case PDR_PRISM_PA_CAL_CURVE_DATA: {
  325. struct pda_pa_curve_data *curve_data =
  326. (struct pda_pa_curve_data *)entry->data;
  327. if (data_len < sizeof(*curve_data)) {
  328. err = -EINVAL;
  329. goto err;
  330. }
  331. switch (curve_data->cal_method_rev) {
  332. case 0:
  333. err = p54_convert_rev0(dev, curve_data);
  334. break;
  335. case 1:
  336. err = p54_convert_rev1(dev, curve_data);
  337. break;
  338. default:
  339. printk(KERN_ERR "p54: unknown curve data "
  340. "revision %d\n",
  341. curve_data->cal_method_rev);
  342. err = -ENODEV;
  343. break;
  344. }
  345. if (err)
  346. goto err;
  347. }
  348. case PDR_PRISM_ZIF_TX_IQ_CALIBRATION:
  349. priv->iq_autocal = kmalloc(data_len, GFP_KERNEL);
  350. if (!priv->iq_autocal) {
  351. err = -ENOMEM;
  352. goto err;
  353. }
  354. memcpy(priv->iq_autocal, entry->data, data_len);
  355. priv->iq_autocal_len = data_len / sizeof(struct pda_iq_autocal_entry);
  356. break;
  357. case PDR_INTERFACE_LIST:
  358. tmp = entry->data;
  359. while ((u8 *)tmp < entry->data + data_len) {
  360. struct bootrec_exp_if *exp_if = tmp;
  361. if (le16_to_cpu(exp_if->if_id) == 0xf)
  362. synth = le16_to_cpu(exp_if->variant);
  363. tmp += sizeof(struct bootrec_exp_if);
  364. }
  365. break;
  366. case PDR_HARDWARE_PLATFORM_COMPONENT_ID:
  367. priv->version = *(u8 *)(entry->data + 1);
  368. break;
  369. case PDR_END:
  370. /* make it overrun */
  371. entry_len = len;
  372. break;
  373. default:
  374. printk(KERN_INFO "p54: unknown eeprom code : 0x%x\n",
  375. le16_to_cpu(entry->code));
  376. break;
  377. }
  378. entry = (void *)entry + (entry_len + 1)*2;
  379. }
  380. if (!synth || !priv->iq_autocal || !priv->output_limit ||
  381. !priv->curve_data) {
  382. printk(KERN_ERR "p54: not all required entries found in eeprom!\n");
  383. err = -EINVAL;
  384. goto err;
  385. }
  386. priv->rxhw = synth & 0x07;
  387. if (priv->rxhw == 4)
  388. p54_init_xbow_synth(dev);
  389. if (!(synth & 0x40))
  390. dev->wiphy->bands[IEEE80211_BAND_2GHZ] = &band_2GHz;
  391. if (!(synth & 0x80))
  392. dev->wiphy->bands[IEEE80211_BAND_5GHZ] = &band_5GHz;
  393. if (!is_valid_ether_addr(dev->wiphy->perm_addr)) {
  394. u8 perm_addr[ETH_ALEN];
  395. printk(KERN_WARNING "%s: Invalid hwaddr! Using randomly generated MAC addr\n",
  396. wiphy_name(dev->wiphy));
  397. random_ether_addr(perm_addr);
  398. SET_IEEE80211_PERM_ADDR(dev, perm_addr);
  399. }
  400. printk(KERN_INFO "%s: hwaddr %pM, MAC:isl38%02x RF:%s\n",
  401. wiphy_name(dev->wiphy),
  402. dev->wiphy->perm_addr,
  403. priv->version, p54_rf_chips[priv->rxhw]);
  404. return 0;
  405. err:
  406. if (priv->iq_autocal) {
  407. kfree(priv->iq_autocal);
  408. priv->iq_autocal = NULL;
  409. }
  410. if (priv->output_limit) {
  411. kfree(priv->output_limit);
  412. priv->output_limit = NULL;
  413. }
  414. if (priv->curve_data) {
  415. kfree(priv->curve_data);
  416. priv->curve_data = NULL;
  417. }
  418. printk(KERN_ERR "p54: eeprom parse failed!\n");
  419. return err;
  420. }
  421. static int p54_rssi_to_dbm(struct ieee80211_hw *dev, int rssi)
  422. {
  423. /* TODO: get the rssi_add & rssi_mul data from the eeprom */
  424. return ((rssi * 0x83) / 64 - 400) / 4;
  425. }
  426. static int p54_rx_data(struct ieee80211_hw *dev, struct sk_buff *skb)
  427. {
  428. struct p54_common *priv = dev->priv;
  429. struct p54_rx_hdr *hdr = (struct p54_rx_hdr *) skb->data;
  430. struct ieee80211_rx_status rx_status = {0};
  431. u16 freq = le16_to_cpu(hdr->freq);
  432. size_t header_len = sizeof(*hdr);
  433. u32 tsf32;
  434. if (!(hdr->magic & cpu_to_le16(0x0001))) {
  435. if (priv->filter_flags & FIF_FCSFAIL)
  436. rx_status.flag |= RX_FLAG_FAILED_FCS_CRC;
  437. else
  438. return 0;
  439. }
  440. rx_status.signal = p54_rssi_to_dbm(dev, hdr->rssi);
  441. rx_status.noise = priv->noise;
  442. /* XX correct? */
  443. rx_status.qual = (100 * hdr->rssi) / 127;
  444. rx_status.rate_idx = (dev->conf.channel->band == IEEE80211_BAND_2GHZ ?
  445. hdr->rate : (hdr->rate - 4)) & 0xf;
  446. rx_status.freq = freq;
  447. rx_status.band = dev->conf.channel->band;
  448. rx_status.antenna = hdr->antenna;
  449. tsf32 = le32_to_cpu(hdr->tsf32);
  450. if (tsf32 < priv->tsf_low32)
  451. priv->tsf_high32++;
  452. rx_status.mactime = ((u64)priv->tsf_high32) << 32 | tsf32;
  453. priv->tsf_low32 = tsf32;
  454. rx_status.flag |= RX_FLAG_TSFT;
  455. if (hdr->magic & cpu_to_le16(0x4000))
  456. header_len += hdr->align[0];
  457. skb_pull(skb, header_len);
  458. skb_trim(skb, le16_to_cpu(hdr->len));
  459. ieee80211_rx_irqsafe(dev, skb, &rx_status);
  460. return -1;
  461. }
  462. static void inline p54_wake_free_queues(struct ieee80211_hw *dev)
  463. {
  464. struct p54_common *priv = dev->priv;
  465. int i;
  466. for (i = 0; i < dev->queues; i++)
  467. if (priv->tx_stats[i + 4].len < priv->tx_stats[i + 4].limit)
  468. ieee80211_wake_queue(dev, i);
  469. }
  470. static void p54_rx_frame_sent(struct ieee80211_hw *dev, struct sk_buff *skb)
  471. {
  472. struct p54_common *priv = dev->priv;
  473. struct p54_control_hdr *hdr = (struct p54_control_hdr *) skb->data;
  474. struct p54_frame_sent_hdr *payload = (struct p54_frame_sent_hdr *) hdr->data;
  475. struct sk_buff *entry = (struct sk_buff *) priv->tx_queue.next;
  476. u32 addr = le32_to_cpu(hdr->req_id) - priv->headroom;
  477. struct memrecord *range = NULL;
  478. u32 freed = 0;
  479. u32 last_addr = priv->rx_start;
  480. unsigned long flags;
  481. int count, idx;
  482. spin_lock_irqsave(&priv->tx_queue.lock, flags);
  483. while (entry != (struct sk_buff *)&priv->tx_queue) {
  484. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(entry);
  485. range = (void *)info->rate_driver_data;
  486. if (range->start_addr == addr) {
  487. struct p54_control_hdr *entry_hdr;
  488. struct p54_tx_control_allocdata *entry_data;
  489. int pad = 0;
  490. if (entry->next != (struct sk_buff *)&priv->tx_queue) {
  491. struct ieee80211_tx_info *ni;
  492. struct memrecord *mr;
  493. ni = IEEE80211_SKB_CB(entry->next);
  494. mr = (struct memrecord *)ni->rate_driver_data;
  495. freed = mr->start_addr - last_addr;
  496. } else
  497. freed = priv->rx_end - last_addr;
  498. last_addr = range->end_addr;
  499. __skb_unlink(entry, &priv->tx_queue);
  500. spin_unlock_irqrestore(&priv->tx_queue.lock, flags);
  501. /*
  502. * Clear manually, ieee80211_tx_info_clear_status would
  503. * clear the counts too and we need them.
  504. */
  505. memset(&info->status.ampdu_ack_len, 0,
  506. sizeof(struct ieee80211_tx_info) -
  507. offsetof(struct ieee80211_tx_info, status.ampdu_ack_len));
  508. BUILD_BUG_ON(offsetof(struct ieee80211_tx_info,
  509. status.ampdu_ack_len) != 23);
  510. entry_hdr = (struct p54_control_hdr *) entry->data;
  511. entry_data = (struct p54_tx_control_allocdata *) entry_hdr->data;
  512. if ((entry_hdr->magic1 & cpu_to_le16(0x4000)) != 0)
  513. pad = entry_data->align[0];
  514. /* walk through the rates array and adjust the counts */
  515. count = payload->retries;
  516. for (idx = 0; idx < 4; idx++) {
  517. if (count >= info->status.rates[idx].count) {
  518. count -= info->status.rates[idx].count;
  519. } else if (count > 0) {
  520. info->status.rates[idx].count = count;
  521. count = 0;
  522. } else {
  523. info->status.rates[idx].idx = -1;
  524. info->status.rates[idx].count = 0;
  525. }
  526. }
  527. priv->tx_stats[entry_data->hw_queue].len--;
  528. if (!(info->flags & IEEE80211_TX_CTL_NO_ACK) &&
  529. !(payload->status & 0x01))
  530. info->flags |= IEEE80211_TX_STAT_ACK;
  531. info->status.ack_signal = p54_rssi_to_dbm(dev,
  532. le16_to_cpu(payload->ack_rssi));
  533. skb_pull(entry, sizeof(*hdr) + pad + sizeof(*entry_data));
  534. ieee80211_tx_status_irqsafe(dev, entry);
  535. goto out;
  536. } else
  537. last_addr = range->end_addr;
  538. entry = entry->next;
  539. }
  540. spin_unlock_irqrestore(&priv->tx_queue.lock, flags);
  541. out:
  542. if (freed >= IEEE80211_MAX_RTS_THRESHOLD + 0x170 +
  543. sizeof(struct p54_control_hdr))
  544. p54_wake_free_queues(dev);
  545. }
  546. static void p54_rx_eeprom_readback(struct ieee80211_hw *dev,
  547. struct sk_buff *skb)
  548. {
  549. struct p54_control_hdr *hdr = (struct p54_control_hdr *) skb->data;
  550. struct p54_eeprom_lm86 *eeprom = (struct p54_eeprom_lm86 *) hdr->data;
  551. struct p54_common *priv = dev->priv;
  552. if (!priv->eeprom)
  553. return ;
  554. memcpy(priv->eeprom, eeprom->data, le16_to_cpu(eeprom->len));
  555. complete(&priv->eeprom_comp);
  556. }
  557. static void p54_rx_stats(struct ieee80211_hw *dev, struct sk_buff *skb)
  558. {
  559. struct p54_common *priv = dev->priv;
  560. struct p54_control_hdr *hdr = (struct p54_control_hdr *) skb->data;
  561. struct p54_statistics *stats = (struct p54_statistics *) hdr->data;
  562. u32 tsf32 = le32_to_cpu(stats->tsf32);
  563. if (tsf32 < priv->tsf_low32)
  564. priv->tsf_high32++;
  565. priv->tsf_low32 = tsf32;
  566. priv->stats.dot11RTSFailureCount = le32_to_cpu(stats->rts_fail);
  567. priv->stats.dot11RTSSuccessCount = le32_to_cpu(stats->rts_success);
  568. priv->stats.dot11FCSErrorCount = le32_to_cpu(stats->rx_bad_fcs);
  569. priv->noise = p54_rssi_to_dbm(dev, le32_to_cpu(stats->noise));
  570. complete(&priv->stats_comp);
  571. mod_timer(&priv->stats_timer, jiffies + 5 * HZ);
  572. }
  573. static int p54_rx_control(struct ieee80211_hw *dev, struct sk_buff *skb)
  574. {
  575. struct p54_control_hdr *hdr = (struct p54_control_hdr *) skb->data;
  576. switch (le16_to_cpu(hdr->type)) {
  577. case P54_CONTROL_TYPE_TXDONE:
  578. p54_rx_frame_sent(dev, skb);
  579. break;
  580. case P54_CONTROL_TYPE_BBP:
  581. break;
  582. case P54_CONTROL_TYPE_STAT_READBACK:
  583. p54_rx_stats(dev, skb);
  584. break;
  585. case P54_CONTROL_TYPE_EEPROM_READBACK:
  586. p54_rx_eeprom_readback(dev, skb);
  587. break;
  588. default:
  589. printk(KERN_DEBUG "%s: not handling 0x%02x type control frame\n",
  590. wiphy_name(dev->wiphy), le16_to_cpu(hdr->type));
  591. break;
  592. }
  593. return 0;
  594. }
  595. /* returns zero if skb can be reused */
  596. int p54_rx(struct ieee80211_hw *dev, struct sk_buff *skb)
  597. {
  598. u8 type = le16_to_cpu(*((__le16 *)skb->data)) >> 8;
  599. if (type == 0x80)
  600. return p54_rx_control(dev, skb);
  601. else
  602. return p54_rx_data(dev, skb);
  603. }
  604. EXPORT_SYMBOL_GPL(p54_rx);
  605. /*
  606. * So, the firmware is somewhat stupid and doesn't know what places in its
  607. * memory incoming data should go to. By poking around in the firmware, we
  608. * can find some unused memory to upload our packets to. However, data that we
  609. * want the card to TX needs to stay intact until the card has told us that
  610. * it is done with it. This function finds empty places we can upload to and
  611. * marks allocated areas as reserved if necessary. p54_rx_frame_sent frees
  612. * allocated areas.
  613. */
  614. static void p54_assign_address(struct ieee80211_hw *dev, struct sk_buff *skb,
  615. struct p54_control_hdr *data, u32 len)
  616. {
  617. struct p54_common *priv = dev->priv;
  618. struct sk_buff *entry = priv->tx_queue.next;
  619. struct sk_buff *target_skb = NULL;
  620. u32 last_addr = priv->rx_start;
  621. u32 largest_hole = 0;
  622. u32 target_addr = priv->rx_start;
  623. unsigned long flags;
  624. unsigned int left;
  625. len = (len + priv->headroom + priv->tailroom + 3) & ~0x3;
  626. spin_lock_irqsave(&priv->tx_queue.lock, flags);
  627. left = skb_queue_len(&priv->tx_queue);
  628. while (left--) {
  629. u32 hole_size;
  630. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(entry);
  631. struct memrecord *range = (void *)info->rate_driver_data;
  632. hole_size = range->start_addr - last_addr;
  633. if (!target_skb && hole_size >= len) {
  634. target_skb = entry->prev;
  635. hole_size -= len;
  636. target_addr = last_addr;
  637. }
  638. largest_hole = max(largest_hole, hole_size);
  639. last_addr = range->end_addr;
  640. entry = entry->next;
  641. }
  642. if (!target_skb && priv->rx_end - last_addr >= len) {
  643. target_skb = priv->tx_queue.prev;
  644. largest_hole = max(largest_hole, priv->rx_end - last_addr - len);
  645. if (!skb_queue_empty(&priv->tx_queue)) {
  646. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(target_skb);
  647. struct memrecord *range = (void *)info->rate_driver_data;
  648. target_addr = range->end_addr;
  649. }
  650. } else
  651. largest_hole = max(largest_hole, priv->rx_end - last_addr);
  652. if (skb) {
  653. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  654. struct memrecord *range = (void *)info->rate_driver_data;
  655. range->start_addr = target_addr;
  656. range->end_addr = target_addr + len;
  657. __skb_queue_after(&priv->tx_queue, target_skb, skb);
  658. if (largest_hole < priv->rx_mtu + priv->headroom +
  659. priv->tailroom +
  660. sizeof(struct p54_control_hdr))
  661. ieee80211_stop_queues(dev);
  662. }
  663. spin_unlock_irqrestore(&priv->tx_queue.lock, flags);
  664. data->req_id = cpu_to_le32(target_addr + priv->headroom);
  665. }
  666. int p54_read_eeprom(struct ieee80211_hw *dev)
  667. {
  668. struct p54_common *priv = dev->priv;
  669. struct p54_control_hdr *hdr = NULL;
  670. struct p54_eeprom_lm86 *eeprom_hdr;
  671. size_t eeprom_size = 0x2020, offset = 0, blocksize;
  672. int ret = -ENOMEM;
  673. void *eeprom = NULL;
  674. hdr = (struct p54_control_hdr *)kzalloc(sizeof(*hdr) +
  675. sizeof(*eeprom_hdr) + EEPROM_READBACK_LEN, GFP_KERNEL);
  676. if (!hdr)
  677. goto free;
  678. priv->eeprom = kzalloc(EEPROM_READBACK_LEN, GFP_KERNEL);
  679. if (!priv->eeprom)
  680. goto free;
  681. eeprom = kzalloc(eeprom_size, GFP_KERNEL);
  682. if (!eeprom)
  683. goto free;
  684. hdr->magic1 = cpu_to_le16(0x8000);
  685. hdr->type = cpu_to_le16(P54_CONTROL_TYPE_EEPROM_READBACK);
  686. hdr->retry1 = hdr->retry2 = 0;
  687. eeprom_hdr = (struct p54_eeprom_lm86 *) hdr->data;
  688. while (eeprom_size) {
  689. blocksize = min(eeprom_size, (size_t)EEPROM_READBACK_LEN);
  690. hdr->len = cpu_to_le16(blocksize + sizeof(*eeprom_hdr));
  691. eeprom_hdr->offset = cpu_to_le16(offset);
  692. eeprom_hdr->len = cpu_to_le16(blocksize);
  693. p54_assign_address(dev, NULL, hdr, le16_to_cpu(hdr->len) +
  694. sizeof(*hdr));
  695. priv->tx(dev, hdr, le16_to_cpu(hdr->len) + sizeof(*hdr), 0);
  696. if (!wait_for_completion_interruptible_timeout(&priv->eeprom_comp, HZ)) {
  697. printk(KERN_ERR "%s: device does not respond!\n",
  698. wiphy_name(dev->wiphy));
  699. ret = -EBUSY;
  700. goto free;
  701. }
  702. memcpy(eeprom + offset, priv->eeprom, blocksize);
  703. offset += blocksize;
  704. eeprom_size -= blocksize;
  705. }
  706. ret = p54_parse_eeprom(dev, eeprom, offset);
  707. free:
  708. kfree(priv->eeprom);
  709. priv->eeprom = NULL;
  710. kfree(hdr);
  711. kfree(eeprom);
  712. return ret;
  713. }
  714. EXPORT_SYMBOL_GPL(p54_read_eeprom);
  715. static int p54_tx(struct ieee80211_hw *dev, struct sk_buff *skb)
  716. {
  717. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  718. struct ieee80211_tx_queue_stats *current_queue;
  719. struct p54_common *priv = dev->priv;
  720. struct p54_control_hdr *hdr;
  721. struct p54_tx_control_allocdata *txhdr;
  722. size_t padding, len;
  723. int i, j, ridx;
  724. u8 rate;
  725. u8 cts_rate = 0x20;
  726. u8 rc_flags;
  727. u8 calculated_tries[4];
  728. u8 nrates = 0, nremaining = 8;
  729. current_queue = &priv->tx_stats[skb_get_queue_mapping(skb) + 4];
  730. if (unlikely(current_queue->len > current_queue->limit))
  731. return NETDEV_TX_BUSY;
  732. current_queue->len++;
  733. current_queue->count++;
  734. if (current_queue->len == current_queue->limit)
  735. ieee80211_stop_queue(dev, skb_get_queue_mapping(skb));
  736. padding = (unsigned long)(skb->data - (sizeof(*hdr) + sizeof(*txhdr))) & 3;
  737. len = skb->len;
  738. txhdr = (struct p54_tx_control_allocdata *)
  739. skb_push(skb, sizeof(*txhdr) + padding);
  740. hdr = (struct p54_control_hdr *) skb_push(skb, sizeof(*hdr));
  741. if (padding)
  742. hdr->magic1 = cpu_to_le16(0x4010);
  743. else
  744. hdr->magic1 = cpu_to_le16(0x0010);
  745. hdr->len = cpu_to_le16(len);
  746. hdr->type = (info->flags & IEEE80211_TX_CTL_NO_ACK) ? 0 : cpu_to_le16(1);
  747. hdr->retry1 = info->control.rates[0].count;
  748. /*
  749. * we register the rates in perfect order, and
  750. * RTS/CTS won't happen on 5 GHz
  751. */
  752. cts_rate = info->control.rts_cts_rate_idx;
  753. memset(&txhdr->rateset, 0, sizeof(txhdr->rateset));
  754. /* see how many rates got used */
  755. for (i = 0; i < 4; i++) {
  756. if (info->control.rates[i].idx < 0)
  757. break;
  758. nrates++;
  759. }
  760. /* limit tries to 8/nrates per rate */
  761. for (i = 0; i < nrates; i++) {
  762. /*
  763. * The magic expression here is equivalent to 8/nrates for
  764. * all values that matter, but avoids division and jumps.
  765. * Note that nrates can only take the values 1 through 4.
  766. */
  767. calculated_tries[i] = min_t(int, ((15 >> nrates) | 1) + 1,
  768. info->control.rates[i].count);
  769. nremaining -= calculated_tries[i];
  770. }
  771. /* if there are tries left, distribute from back to front */
  772. for (i = nrates - 1; nremaining > 0 && i >= 0; i--) {
  773. int tmp = info->control.rates[i].count - calculated_tries[i];
  774. if (tmp <= 0)
  775. continue;
  776. /* RC requested more tries at this rate */
  777. tmp = min_t(int, tmp, nremaining);
  778. calculated_tries[i] += tmp;
  779. nremaining -= tmp;
  780. }
  781. ridx = 0;
  782. for (i = 0; i < nrates && ridx < 8; i++) {
  783. /* we register the rates in perfect order */
  784. rate = info->control.rates[i].idx;
  785. if (info->band == IEEE80211_BAND_5GHZ)
  786. rate += 4;
  787. /* store the count we actually calculated for TX status */
  788. info->control.rates[i].count = calculated_tries[i];
  789. rc_flags = info->control.rates[i].flags;
  790. if (rc_flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE) {
  791. rate |= 0x10;
  792. cts_rate |= 0x10;
  793. }
  794. if (rc_flags & IEEE80211_TX_RC_USE_RTS_CTS)
  795. rate |= 0x40;
  796. else if (rc_flags & IEEE80211_TX_RC_USE_CTS_PROTECT)
  797. rate |= 0x20;
  798. for (j = 0; j < calculated_tries[i] && ridx < 8; j++) {
  799. txhdr->rateset[ridx] = rate;
  800. ridx++;
  801. }
  802. }
  803. hdr->retry2 = ridx;
  804. txhdr->key_type = 0;
  805. txhdr->key_len = 0;
  806. txhdr->hw_queue = skb_get_queue_mapping(skb) + 4;
  807. txhdr->tx_antenna = (info->antenna_sel_tx == 0) ?
  808. 2 : info->antenna_sel_tx - 1;
  809. txhdr->output_power = priv->output_power;
  810. txhdr->cts_rate = (info->flags & IEEE80211_TX_CTL_NO_ACK) ?
  811. 0 : cts_rate;
  812. if (padding)
  813. txhdr->align[0] = padding;
  814. /* modifies skb->cb and with it info, so must be last! */
  815. p54_assign_address(dev, skb, hdr, skb->len);
  816. priv->tx(dev, hdr, skb->len, 0);
  817. return 0;
  818. }
  819. static int p54_set_filter(struct ieee80211_hw *dev, u16 filter_type,
  820. const u8 *bssid)
  821. {
  822. struct p54_common *priv = dev->priv;
  823. struct p54_control_hdr *hdr;
  824. struct p54_tx_control_filter *filter;
  825. size_t data_len;
  826. hdr = kzalloc(sizeof(*hdr) + sizeof(*filter) +
  827. priv->tx_hdr_len, GFP_ATOMIC);
  828. if (!hdr)
  829. return -ENOMEM;
  830. hdr = (void *)hdr + priv->tx_hdr_len;
  831. filter = (struct p54_tx_control_filter *) hdr->data;
  832. hdr->magic1 = cpu_to_le16(0x8001);
  833. hdr->type = cpu_to_le16(P54_CONTROL_TYPE_FILTER_SET);
  834. priv->filter_type = filter->filter_type = cpu_to_le16(filter_type);
  835. memcpy(filter->mac_addr, priv->mac_addr, ETH_ALEN);
  836. if (!bssid)
  837. memset(filter->bssid, ~0, ETH_ALEN);
  838. else
  839. memcpy(filter->bssid, bssid, ETH_ALEN);
  840. filter->rx_antenna = priv->rx_antenna;
  841. if (priv->fw_var < 0x500) {
  842. data_len = P54_TX_CONTROL_FILTER_V1_LEN;
  843. filter->v1.basic_rate_mask = cpu_to_le32(0x15f);
  844. filter->v1.rx_addr = cpu_to_le32(priv->rx_end);
  845. filter->v1.max_rx = cpu_to_le16(priv->rx_mtu);
  846. filter->v1.rxhw = cpu_to_le16(priv->rxhw);
  847. filter->v1.wakeup_timer = cpu_to_le16(500);
  848. } else {
  849. data_len = P54_TX_CONTROL_FILTER_V2_LEN;
  850. filter->v2.rx_addr = cpu_to_le32(priv->rx_end);
  851. filter->v2.max_rx = cpu_to_le16(priv->rx_mtu);
  852. filter->v2.rxhw = cpu_to_le16(priv->rxhw);
  853. filter->v2.timer = cpu_to_le16(1000);
  854. }
  855. hdr->len = cpu_to_le16(data_len);
  856. p54_assign_address(dev, NULL, hdr, sizeof(*hdr) + data_len);
  857. priv->tx(dev, hdr, sizeof(*hdr) + data_len, 1);
  858. return 0;
  859. }
  860. static int p54_set_freq(struct ieee80211_hw *dev, __le16 freq)
  861. {
  862. struct p54_common *priv = dev->priv;
  863. struct p54_control_hdr *hdr;
  864. struct p54_tx_control_channel *chan;
  865. unsigned int i;
  866. size_t data_len;
  867. void *entry;
  868. hdr = kzalloc(sizeof(*hdr) + sizeof(*chan) +
  869. priv->tx_hdr_len, GFP_KERNEL);
  870. if (!hdr)
  871. return -ENOMEM;
  872. hdr = (void *)hdr + priv->tx_hdr_len;
  873. chan = (struct p54_tx_control_channel *) hdr->data;
  874. hdr->magic1 = cpu_to_le16(0x8001);
  875. hdr->type = cpu_to_le16(P54_CONTROL_TYPE_CHANNEL_CHANGE);
  876. chan->flags = cpu_to_le16(0x1);
  877. chan->dwell = cpu_to_le16(0x0);
  878. for (i = 0; i < priv->iq_autocal_len; i++) {
  879. if (priv->iq_autocal[i].freq != freq)
  880. continue;
  881. memcpy(&chan->iq_autocal, &priv->iq_autocal[i],
  882. sizeof(*priv->iq_autocal));
  883. break;
  884. }
  885. if (i == priv->iq_autocal_len)
  886. goto err;
  887. for (i = 0; i < priv->output_limit_len; i++) {
  888. if (priv->output_limit[i].freq != freq)
  889. continue;
  890. chan->val_barker = 0x38;
  891. chan->val_bpsk = chan->dup_bpsk =
  892. priv->output_limit[i].val_bpsk;
  893. chan->val_qpsk = chan->dup_qpsk =
  894. priv->output_limit[i].val_qpsk;
  895. chan->val_16qam = chan->dup_16qam =
  896. priv->output_limit[i].val_16qam;
  897. chan->val_64qam = chan->dup_64qam =
  898. priv->output_limit[i].val_64qam;
  899. break;
  900. }
  901. if (i == priv->output_limit_len)
  902. goto err;
  903. entry = priv->curve_data->data;
  904. for (i = 0; i < priv->curve_data->channels; i++) {
  905. if (*((__le16 *)entry) != freq) {
  906. entry += sizeof(__le16);
  907. entry += sizeof(struct p54_pa_curve_data_sample) *
  908. priv->curve_data->points_per_channel;
  909. continue;
  910. }
  911. entry += sizeof(__le16);
  912. chan->pa_points_per_curve =
  913. min(priv->curve_data->points_per_channel, (u8) 8);
  914. memcpy(chan->curve_data, entry, sizeof(*chan->curve_data) *
  915. chan->pa_points_per_curve);
  916. break;
  917. }
  918. if (priv->fw_var < 0x500) {
  919. data_len = P54_TX_CONTROL_CHANNEL_V1_LEN;
  920. chan->v1.rssical_mul = cpu_to_le16(130);
  921. chan->v1.rssical_add = cpu_to_le16(0xfe70);
  922. } else {
  923. data_len = P54_TX_CONTROL_CHANNEL_V2_LEN;
  924. chan->v2.rssical_mul = cpu_to_le16(130);
  925. chan->v2.rssical_add = cpu_to_le16(0xfe70);
  926. chan->v2.basic_rate_mask = cpu_to_le32(0x15f);
  927. }
  928. hdr->len = cpu_to_le16(data_len);
  929. p54_assign_address(dev, NULL, hdr, sizeof(*hdr) + data_len);
  930. priv->tx(dev, hdr, sizeof(*hdr) + data_len, 1);
  931. return 0;
  932. err:
  933. printk(KERN_ERR "%s: frequency change failed\n", wiphy_name(dev->wiphy));
  934. kfree(hdr);
  935. return -EINVAL;
  936. }
  937. static int p54_set_leds(struct ieee80211_hw *dev, int mode, int link, int act)
  938. {
  939. struct p54_common *priv = dev->priv;
  940. struct p54_control_hdr *hdr;
  941. struct p54_tx_control_led *led;
  942. hdr = kzalloc(sizeof(*hdr) + sizeof(*led) +
  943. priv->tx_hdr_len, GFP_KERNEL);
  944. if (!hdr)
  945. return -ENOMEM;
  946. hdr = (void *)hdr + priv->tx_hdr_len;
  947. hdr->magic1 = cpu_to_le16(0x8001);
  948. hdr->len = cpu_to_le16(sizeof(*led));
  949. hdr->type = cpu_to_le16(P54_CONTROL_TYPE_LED);
  950. p54_assign_address(dev, NULL, hdr, sizeof(*hdr) + sizeof(*led));
  951. led = (struct p54_tx_control_led *) hdr->data;
  952. led->mode = cpu_to_le16(mode);
  953. led->led_permanent = cpu_to_le16(link);
  954. led->led_temporary = cpu_to_le16(act);
  955. led->duration = cpu_to_le16(1000);
  956. priv->tx(dev, hdr, sizeof(*hdr) + sizeof(*led), 1);
  957. return 0;
  958. }
  959. #define P54_SET_QUEUE(queue, ai_fs, cw_min, cw_max, _txop) \
  960. do { \
  961. queue.aifs = cpu_to_le16(ai_fs); \
  962. queue.cwmin = cpu_to_le16(cw_min); \
  963. queue.cwmax = cpu_to_le16(cw_max); \
  964. queue.txop = cpu_to_le16(_txop); \
  965. } while(0)
  966. static int p54_set_edcf(struct ieee80211_hw *dev)
  967. {
  968. struct p54_common *priv = dev->priv;
  969. struct p54_control_hdr *hdr;
  970. struct p54_edcf *edcf;
  971. hdr = kzalloc(priv->tx_hdr_len + sizeof(*hdr) + sizeof(*edcf),
  972. GFP_ATOMIC);
  973. if (!hdr)
  974. return -ENOMEM;
  975. hdr = (void *)hdr + priv->tx_hdr_len;
  976. hdr->magic1 = cpu_to_le16(0x8001);
  977. hdr->len = cpu_to_le16(sizeof(*edcf));
  978. hdr->type = cpu_to_le16(P54_CONTROL_TYPE_DCFINIT);
  979. hdr->retry1 = hdr->retry2 = 0;
  980. edcf = (struct p54_edcf *)hdr->data;
  981. if (priv->use_short_slot) {
  982. edcf->slottime = 9;
  983. edcf->sifs = 0x10;
  984. edcf->eofpad = 0x00;
  985. } else {
  986. edcf->slottime = 20;
  987. edcf->sifs = 0x0a;
  988. edcf->eofpad = 0x06;
  989. }
  990. /* (see prism54/isl_oid.h for further details) */
  991. edcf->frameburst = cpu_to_le16(0);
  992. edcf->round_trip_delay = cpu_to_le16(0);
  993. memset(edcf->mapping, 0, sizeof(edcf->mapping));
  994. memcpy(edcf->queue, priv->qos_params, sizeof(edcf->queue));
  995. p54_assign_address(dev, NULL, hdr, sizeof(*hdr) + sizeof(*edcf));
  996. priv->tx(dev, hdr, sizeof(*hdr) + sizeof(*edcf), 1);
  997. return 0;
  998. }
  999. static int p54_start(struct ieee80211_hw *dev)
  1000. {
  1001. struct p54_common *priv = dev->priv;
  1002. int err;
  1003. if (!priv->cached_stats) {
  1004. priv->cached_stats = kzalloc(sizeof(struct p54_statistics) +
  1005. priv->tx_hdr_len + sizeof(struct p54_control_hdr),
  1006. GFP_KERNEL);
  1007. if (!priv->cached_stats)
  1008. return -ENOMEM;
  1009. }
  1010. err = priv->open(dev);
  1011. if (!err)
  1012. priv->mode = NL80211_IFTYPE_MONITOR;
  1013. P54_SET_QUEUE(priv->qos_params[0], 0x0002, 0x0003, 0x0007, 47);
  1014. P54_SET_QUEUE(priv->qos_params[1], 0x0002, 0x0007, 0x000f, 94);
  1015. P54_SET_QUEUE(priv->qos_params[2], 0x0003, 0x000f, 0x03ff, 0);
  1016. P54_SET_QUEUE(priv->qos_params[3], 0x0007, 0x000f, 0x03ff, 0);
  1017. err = p54_set_edcf(dev);
  1018. if (!err)
  1019. mod_timer(&priv->stats_timer, jiffies + HZ);
  1020. return err;
  1021. }
  1022. static void p54_stop(struct ieee80211_hw *dev)
  1023. {
  1024. struct p54_common *priv = dev->priv;
  1025. struct sk_buff *skb;
  1026. del_timer(&priv->stats_timer);
  1027. while ((skb = skb_dequeue(&priv->tx_queue)))
  1028. kfree_skb(skb);
  1029. priv->stop(dev);
  1030. priv->tsf_high32 = priv->tsf_low32 = 0;
  1031. priv->mode = NL80211_IFTYPE_UNSPECIFIED;
  1032. }
  1033. static int p54_add_interface(struct ieee80211_hw *dev,
  1034. struct ieee80211_if_init_conf *conf)
  1035. {
  1036. struct p54_common *priv = dev->priv;
  1037. if (priv->mode != NL80211_IFTYPE_MONITOR)
  1038. return -EOPNOTSUPP;
  1039. switch (conf->type) {
  1040. case NL80211_IFTYPE_STATION:
  1041. priv->mode = conf->type;
  1042. break;
  1043. default:
  1044. return -EOPNOTSUPP;
  1045. }
  1046. memcpy(priv->mac_addr, conf->mac_addr, ETH_ALEN);
  1047. p54_set_filter(dev, 0, NULL);
  1048. switch (conf->type) {
  1049. case NL80211_IFTYPE_STATION:
  1050. p54_set_filter(dev, 1, NULL);
  1051. break;
  1052. default:
  1053. BUG(); /* impossible */
  1054. break;
  1055. }
  1056. p54_set_leds(dev, 1, 0, 0);
  1057. return 0;
  1058. }
  1059. static void p54_remove_interface(struct ieee80211_hw *dev,
  1060. struct ieee80211_if_init_conf *conf)
  1061. {
  1062. struct p54_common *priv = dev->priv;
  1063. priv->mode = NL80211_IFTYPE_MONITOR;
  1064. memset(priv->mac_addr, 0, ETH_ALEN);
  1065. p54_set_filter(dev, 0, NULL);
  1066. }
  1067. static int p54_config(struct ieee80211_hw *dev, u32 changed)
  1068. {
  1069. int ret;
  1070. struct p54_common *priv = dev->priv;
  1071. struct ieee80211_conf *conf = &dev->conf;
  1072. mutex_lock(&priv->conf_mutex);
  1073. priv->rx_antenna = 2; /* automatic */
  1074. priv->output_power = conf->power_level << 2;
  1075. ret = p54_set_freq(dev, cpu_to_le16(conf->channel->center_freq));
  1076. if (!ret)
  1077. ret = p54_set_edcf(dev);
  1078. mutex_unlock(&priv->conf_mutex);
  1079. return ret;
  1080. }
  1081. static int p54_config_interface(struct ieee80211_hw *dev,
  1082. struct ieee80211_vif *vif,
  1083. struct ieee80211_if_conf *conf)
  1084. {
  1085. struct p54_common *priv = dev->priv;
  1086. mutex_lock(&priv->conf_mutex);
  1087. p54_set_filter(dev, 0, conf->bssid);
  1088. p54_set_leds(dev, 1, !is_multicast_ether_addr(conf->bssid), 0);
  1089. memcpy(priv->bssid, conf->bssid, ETH_ALEN);
  1090. mutex_unlock(&priv->conf_mutex);
  1091. return 0;
  1092. }
  1093. static void p54_configure_filter(struct ieee80211_hw *dev,
  1094. unsigned int changed_flags,
  1095. unsigned int *total_flags,
  1096. int mc_count, struct dev_mc_list *mclist)
  1097. {
  1098. struct p54_common *priv = dev->priv;
  1099. *total_flags &= FIF_BCN_PRBRESP_PROMISC |
  1100. FIF_PROMISC_IN_BSS |
  1101. FIF_FCSFAIL;
  1102. priv->filter_flags = *total_flags;
  1103. if (changed_flags & FIF_BCN_PRBRESP_PROMISC) {
  1104. if (*total_flags & FIF_BCN_PRBRESP_PROMISC)
  1105. p54_set_filter(dev, le16_to_cpu(priv->filter_type),
  1106. NULL);
  1107. else
  1108. p54_set_filter(dev, le16_to_cpu(priv->filter_type),
  1109. priv->bssid);
  1110. }
  1111. if (changed_flags & FIF_PROMISC_IN_BSS) {
  1112. if (*total_flags & FIF_PROMISC_IN_BSS)
  1113. p54_set_filter(dev, le16_to_cpu(priv->filter_type) |
  1114. 0x8, NULL);
  1115. else
  1116. p54_set_filter(dev, le16_to_cpu(priv->filter_type) &
  1117. ~0x8, priv->bssid);
  1118. }
  1119. }
  1120. static int p54_conf_tx(struct ieee80211_hw *dev, u16 queue,
  1121. const struct ieee80211_tx_queue_params *params)
  1122. {
  1123. struct p54_common *priv = dev->priv;
  1124. if ((params) && !(queue > 4)) {
  1125. P54_SET_QUEUE(priv->qos_params[queue], params->aifs,
  1126. params->cw_min, params->cw_max, params->txop);
  1127. } else
  1128. return -EINVAL;
  1129. return p54_set_edcf(dev);
  1130. }
  1131. static int p54_init_xbow_synth(struct ieee80211_hw *dev)
  1132. {
  1133. struct p54_common *priv = dev->priv;
  1134. struct p54_control_hdr *hdr;
  1135. struct p54_tx_control_xbow_synth *xbow;
  1136. hdr = kzalloc(sizeof(*hdr) + sizeof(*xbow) +
  1137. priv->tx_hdr_len, GFP_KERNEL);
  1138. if (!hdr)
  1139. return -ENOMEM;
  1140. hdr = (void *)hdr + priv->tx_hdr_len;
  1141. hdr->magic1 = cpu_to_le16(0x8001);
  1142. hdr->len = cpu_to_le16(sizeof(*xbow));
  1143. hdr->type = cpu_to_le16(P54_CONTROL_TYPE_XBOW_SYNTH_CFG);
  1144. p54_assign_address(dev, NULL, hdr, sizeof(*hdr) + sizeof(*xbow));
  1145. xbow = (struct p54_tx_control_xbow_synth *) hdr->data;
  1146. xbow->magic1 = cpu_to_le16(0x1);
  1147. xbow->magic2 = cpu_to_le16(0x2);
  1148. xbow->freq = cpu_to_le16(5390);
  1149. priv->tx(dev, hdr, sizeof(*hdr) + sizeof(*xbow), 1);
  1150. return 0;
  1151. }
  1152. static void p54_statistics_timer(unsigned long data)
  1153. {
  1154. struct ieee80211_hw *dev = (struct ieee80211_hw *) data;
  1155. struct p54_common *priv = dev->priv;
  1156. struct p54_control_hdr *hdr;
  1157. struct p54_statistics *stats;
  1158. BUG_ON(!priv->cached_stats);
  1159. hdr = (void *)priv->cached_stats + priv->tx_hdr_len;
  1160. hdr->magic1 = cpu_to_le16(0x8000);
  1161. hdr->len = cpu_to_le16(sizeof(*stats));
  1162. hdr->type = cpu_to_le16(P54_CONTROL_TYPE_STAT_READBACK);
  1163. p54_assign_address(dev, NULL, hdr, sizeof(*hdr) + sizeof(*stats));
  1164. priv->tx(dev, hdr, sizeof(*hdr) + sizeof(*stats), 0);
  1165. }
  1166. static int p54_get_stats(struct ieee80211_hw *dev,
  1167. struct ieee80211_low_level_stats *stats)
  1168. {
  1169. struct p54_common *priv = dev->priv;
  1170. del_timer(&priv->stats_timer);
  1171. p54_statistics_timer((unsigned long)dev);
  1172. if (!wait_for_completion_interruptible_timeout(&priv->stats_comp, HZ)) {
  1173. printk(KERN_ERR "%s: device does not respond!\n",
  1174. wiphy_name(dev->wiphy));
  1175. return -EBUSY;
  1176. }
  1177. memcpy(stats, &priv->stats, sizeof(*stats));
  1178. return 0;
  1179. }
  1180. static int p54_get_tx_stats(struct ieee80211_hw *dev,
  1181. struct ieee80211_tx_queue_stats *stats)
  1182. {
  1183. struct p54_common *priv = dev->priv;
  1184. memcpy(stats, &priv->tx_stats[4], sizeof(stats[0]) * dev->queues);
  1185. return 0;
  1186. }
  1187. static void p54_bss_info_changed(struct ieee80211_hw *dev,
  1188. struct ieee80211_vif *vif,
  1189. struct ieee80211_bss_conf *info,
  1190. u32 changed)
  1191. {
  1192. struct p54_common *priv = dev->priv;
  1193. if (changed & BSS_CHANGED_ERP_SLOT) {
  1194. priv->use_short_slot = info->use_short_slot;
  1195. p54_set_edcf(dev);
  1196. }
  1197. }
  1198. static const struct ieee80211_ops p54_ops = {
  1199. .tx = p54_tx,
  1200. .start = p54_start,
  1201. .stop = p54_stop,
  1202. .add_interface = p54_add_interface,
  1203. .remove_interface = p54_remove_interface,
  1204. .config = p54_config,
  1205. .config_interface = p54_config_interface,
  1206. .bss_info_changed = p54_bss_info_changed,
  1207. .configure_filter = p54_configure_filter,
  1208. .conf_tx = p54_conf_tx,
  1209. .get_stats = p54_get_stats,
  1210. .get_tx_stats = p54_get_tx_stats
  1211. };
  1212. struct ieee80211_hw *p54_init_common(size_t priv_data_len)
  1213. {
  1214. struct ieee80211_hw *dev;
  1215. struct p54_common *priv;
  1216. dev = ieee80211_alloc_hw(priv_data_len, &p54_ops);
  1217. if (!dev)
  1218. return NULL;
  1219. priv = dev->priv;
  1220. priv->mode = NL80211_IFTYPE_UNSPECIFIED;
  1221. skb_queue_head_init(&priv->tx_queue);
  1222. dev->flags = IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING | /* not sure */
  1223. IEEE80211_HW_RX_INCLUDES_FCS |
  1224. IEEE80211_HW_SIGNAL_DBM |
  1225. IEEE80211_HW_NOISE_DBM;
  1226. /*
  1227. * XXX: when this driver gets support for any mode that
  1228. * requires beacons (AP, MESH, IBSS) then it must
  1229. * implement IEEE80211_TX_CTL_ASSIGN_SEQ.
  1230. */
  1231. dev->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION);
  1232. dev->channel_change_time = 1000; /* TODO: find actual value */
  1233. priv->tx_stats[0].limit = 1;
  1234. priv->tx_stats[1].limit = 1;
  1235. priv->tx_stats[2].limit = 1;
  1236. priv->tx_stats[3].limit = 1;
  1237. priv->tx_stats[4].limit = 5;
  1238. dev->queues = 1;
  1239. priv->noise = -94;
  1240. /*
  1241. * We support at most 8 tries no matter which rate they're at,
  1242. * we cannot support max_rates * max_rate_tries as we set it
  1243. * here, but setting it correctly to 4/2 or so would limit us
  1244. * artificially if the RC algorithm wants just two rates, so
  1245. * let's say 4/7, we'll redistribute it at TX time, see the
  1246. * comments there.
  1247. */
  1248. dev->max_rates = 4;
  1249. dev->max_rate_tries = 7;
  1250. dev->extra_tx_headroom = sizeof(struct p54_control_hdr) + 4 +
  1251. sizeof(struct p54_tx_control_allocdata);
  1252. mutex_init(&priv->conf_mutex);
  1253. init_completion(&priv->eeprom_comp);
  1254. init_completion(&priv->stats_comp);
  1255. setup_timer(&priv->stats_timer, p54_statistics_timer,
  1256. (unsigned long)dev);
  1257. return dev;
  1258. }
  1259. EXPORT_SYMBOL_GPL(p54_init_common);
  1260. void p54_free_common(struct ieee80211_hw *dev)
  1261. {
  1262. struct p54_common *priv = dev->priv;
  1263. kfree(priv->cached_stats);
  1264. kfree(priv->iq_autocal);
  1265. kfree(priv->output_limit);
  1266. kfree(priv->curve_data);
  1267. }
  1268. EXPORT_SYMBOL_GPL(p54_free_common);
  1269. static int __init p54_init(void)
  1270. {
  1271. return 0;
  1272. }
  1273. static void __exit p54_exit(void)
  1274. {
  1275. }
  1276. module_init(p54_init);
  1277. module_exit(p54_exit);