p54common.c 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126
  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:
  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 <net/mac80211.h>
  22. #include "p54.h"
  23. #include "p54common.h"
  24. static int modparam_nohwcrypt;
  25. module_param_named(nohwcrypt, modparam_nohwcrypt, bool, S_IRUGO);
  26. MODULE_PARM_DESC(nohwcrypt, "Disable hardware encryption.");
  27. MODULE_AUTHOR("Michael Wu <flamingice@sourmilk.net>");
  28. MODULE_DESCRIPTION("Softmac Prism54 common code");
  29. MODULE_LICENSE("GPL");
  30. MODULE_ALIAS("prism54common");
  31. static struct ieee80211_rate p54_bgrates[] = {
  32. { .bitrate = 10, .hw_value = 0, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  33. { .bitrate = 20, .hw_value = 1, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  34. { .bitrate = 55, .hw_value = 2, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  35. { .bitrate = 110, .hw_value = 3, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  36. { .bitrate = 60, .hw_value = 4, },
  37. { .bitrate = 90, .hw_value = 5, },
  38. { .bitrate = 120, .hw_value = 6, },
  39. { .bitrate = 180, .hw_value = 7, },
  40. { .bitrate = 240, .hw_value = 8, },
  41. { .bitrate = 360, .hw_value = 9, },
  42. { .bitrate = 480, .hw_value = 10, },
  43. { .bitrate = 540, .hw_value = 11, },
  44. };
  45. static struct ieee80211_channel p54_bgchannels[] = {
  46. { .center_freq = 2412, .hw_value = 1, },
  47. { .center_freq = 2417, .hw_value = 2, },
  48. { .center_freq = 2422, .hw_value = 3, },
  49. { .center_freq = 2427, .hw_value = 4, },
  50. { .center_freq = 2432, .hw_value = 5, },
  51. { .center_freq = 2437, .hw_value = 6, },
  52. { .center_freq = 2442, .hw_value = 7, },
  53. { .center_freq = 2447, .hw_value = 8, },
  54. { .center_freq = 2452, .hw_value = 9, },
  55. { .center_freq = 2457, .hw_value = 10, },
  56. { .center_freq = 2462, .hw_value = 11, },
  57. { .center_freq = 2467, .hw_value = 12, },
  58. { .center_freq = 2472, .hw_value = 13, },
  59. { .center_freq = 2484, .hw_value = 14, },
  60. };
  61. static struct ieee80211_supported_band band_2GHz = {
  62. .channels = p54_bgchannels,
  63. .n_channels = ARRAY_SIZE(p54_bgchannels),
  64. .bitrates = p54_bgrates,
  65. .n_bitrates = ARRAY_SIZE(p54_bgrates),
  66. };
  67. static struct ieee80211_rate p54_arates[] = {
  68. { .bitrate = 60, .hw_value = 4, },
  69. { .bitrate = 90, .hw_value = 5, },
  70. { .bitrate = 120, .hw_value = 6, },
  71. { .bitrate = 180, .hw_value = 7, },
  72. { .bitrate = 240, .hw_value = 8, },
  73. { .bitrate = 360, .hw_value = 9, },
  74. { .bitrate = 480, .hw_value = 10, },
  75. { .bitrate = 540, .hw_value = 11, },
  76. };
  77. static struct ieee80211_channel p54_achannels[] = {
  78. { .center_freq = 4920 },
  79. { .center_freq = 4940 },
  80. { .center_freq = 4960 },
  81. { .center_freq = 4980 },
  82. { .center_freq = 5040 },
  83. { .center_freq = 5060 },
  84. { .center_freq = 5080 },
  85. { .center_freq = 5170 },
  86. { .center_freq = 5180 },
  87. { .center_freq = 5190 },
  88. { .center_freq = 5200 },
  89. { .center_freq = 5210 },
  90. { .center_freq = 5220 },
  91. { .center_freq = 5230 },
  92. { .center_freq = 5240 },
  93. { .center_freq = 5260 },
  94. { .center_freq = 5280 },
  95. { .center_freq = 5300 },
  96. { .center_freq = 5320 },
  97. { .center_freq = 5500 },
  98. { .center_freq = 5520 },
  99. { .center_freq = 5540 },
  100. { .center_freq = 5560 },
  101. { .center_freq = 5580 },
  102. { .center_freq = 5600 },
  103. { .center_freq = 5620 },
  104. { .center_freq = 5640 },
  105. { .center_freq = 5660 },
  106. { .center_freq = 5680 },
  107. { .center_freq = 5700 },
  108. { .center_freq = 5745 },
  109. { .center_freq = 5765 },
  110. { .center_freq = 5785 },
  111. { .center_freq = 5805 },
  112. { .center_freq = 5825 },
  113. };
  114. static struct ieee80211_supported_band band_5GHz = {
  115. .channels = p54_achannels,
  116. .n_channels = ARRAY_SIZE(p54_achannels),
  117. .bitrates = p54_arates,
  118. .n_bitrates = ARRAY_SIZE(p54_arates),
  119. };
  120. int p54_parse_firmware(struct ieee80211_hw *dev, const struct firmware *fw)
  121. {
  122. struct p54_common *priv = dev->priv;
  123. struct bootrec_exp_if *exp_if;
  124. struct bootrec *bootrec;
  125. u32 *data = (u32 *)fw->data;
  126. u32 *end_data = (u32 *)fw->data + (fw->size >> 2);
  127. u8 *fw_version = NULL;
  128. size_t len;
  129. int i;
  130. if (priv->rx_start)
  131. return 0;
  132. while (data < end_data && *data)
  133. data++;
  134. while (data < end_data && !*data)
  135. data++;
  136. bootrec = (struct bootrec *) data;
  137. while (bootrec->data <= end_data &&
  138. (bootrec->data + (len = le32_to_cpu(bootrec->len))) <= end_data) {
  139. u32 code = le32_to_cpu(bootrec->code);
  140. switch (code) {
  141. case BR_CODE_COMPONENT_ID:
  142. priv->fw_interface = be32_to_cpup((__be32 *)
  143. bootrec->data);
  144. switch (priv->fw_interface) {
  145. case FW_LM86:
  146. case FW_LM20:
  147. case FW_LM87: {
  148. char *iftype = (char *)bootrec->data;
  149. printk(KERN_INFO "%s: p54 detected a LM%c%c "
  150. "firmware\n",
  151. wiphy_name(dev->wiphy),
  152. iftype[2], iftype[3]);
  153. break;
  154. }
  155. case FW_FMAC:
  156. default:
  157. printk(KERN_ERR "%s: unsupported firmware\n",
  158. wiphy_name(dev->wiphy));
  159. return -ENODEV;
  160. }
  161. break;
  162. case BR_CODE_COMPONENT_VERSION:
  163. /* 24 bytes should be enough for all firmwares */
  164. if (strnlen((unsigned char*)bootrec->data, 24) < 24)
  165. fw_version = (unsigned char*)bootrec->data;
  166. break;
  167. case BR_CODE_DESCR: {
  168. struct bootrec_desc *desc =
  169. (struct bootrec_desc *)bootrec->data;
  170. priv->rx_start = le32_to_cpu(desc->rx_start);
  171. /* FIXME add sanity checking */
  172. priv->rx_end = le32_to_cpu(desc->rx_end) - 0x3500;
  173. priv->headroom = desc->headroom;
  174. priv->tailroom = desc->tailroom;
  175. priv->privacy_caps = desc->privacy_caps;
  176. priv->rx_keycache_size = desc->rx_keycache_size;
  177. if (le32_to_cpu(bootrec->len) == 11)
  178. priv->rx_mtu = le16_to_cpu(desc->rx_mtu);
  179. else
  180. priv->rx_mtu = (size_t)
  181. 0x620 - priv->tx_hdr_len;
  182. break;
  183. }
  184. case BR_CODE_EXPOSED_IF:
  185. exp_if = (struct bootrec_exp_if *) bootrec->data;
  186. for (i = 0; i < (len * sizeof(*exp_if) / 4); i++)
  187. if (exp_if[i].if_id == cpu_to_le16(0x1a))
  188. priv->fw_var = le16_to_cpu(exp_if[i].variant);
  189. break;
  190. case BR_CODE_DEPENDENT_IF:
  191. break;
  192. case BR_CODE_END_OF_BRA:
  193. case LEGACY_BR_CODE_END_OF_BRA:
  194. end_data = NULL;
  195. break;
  196. default:
  197. break;
  198. }
  199. bootrec = (struct bootrec *)&bootrec->data[len];
  200. }
  201. if (fw_version)
  202. printk(KERN_INFO "%s: FW rev %s - Softmac protocol %x.%x\n",
  203. wiphy_name(dev->wiphy), fw_version,
  204. priv->fw_var >> 8, priv->fw_var & 0xff);
  205. if (priv->fw_var < 0x500)
  206. printk(KERN_INFO "%s: you are using an obsolete firmware. "
  207. "visit http://wireless.kernel.org/en/users/Drivers/p54 "
  208. "and grab one for \"kernel >= 2.6.28\"!\n",
  209. wiphy_name(dev->wiphy));
  210. if (priv->fw_var >= 0x300) {
  211. /* Firmware supports QoS, use it! */
  212. priv->tx_stats[4].limit = 3; /* AC_VO */
  213. priv->tx_stats[5].limit = 4; /* AC_VI */
  214. priv->tx_stats[6].limit = 3; /* AC_BE */
  215. priv->tx_stats[7].limit = 2; /* AC_BK */
  216. dev->queues = 4;
  217. }
  218. if (!modparam_nohwcrypt)
  219. printk(KERN_INFO "%s: cryptographic accelerator "
  220. "WEP:%s, TKIP:%s, CCMP:%s\n",
  221. wiphy_name(dev->wiphy),
  222. (priv->privacy_caps & BR_DESC_PRIV_CAP_WEP) ? "YES" :
  223. "no", (priv->privacy_caps & (BR_DESC_PRIV_CAP_TKIP |
  224. BR_DESC_PRIV_CAP_MICHAEL)) ? "YES" : "no",
  225. (priv->privacy_caps & BR_DESC_PRIV_CAP_AESCCMP) ?
  226. "YES" : "no");
  227. return 0;
  228. }
  229. EXPORT_SYMBOL_GPL(p54_parse_firmware);
  230. static int p54_convert_rev0(struct ieee80211_hw *dev,
  231. struct pda_pa_curve_data *curve_data)
  232. {
  233. struct p54_common *priv = dev->priv;
  234. struct p54_pa_curve_data_sample *dst;
  235. struct pda_pa_curve_data_sample_rev0 *src;
  236. size_t cd_len = sizeof(*curve_data) +
  237. (curve_data->points_per_channel*sizeof(*dst) + 2) *
  238. curve_data->channels;
  239. unsigned int i, j;
  240. void *source, *target;
  241. priv->curve_data = kmalloc(cd_len, GFP_KERNEL);
  242. if (!priv->curve_data)
  243. return -ENOMEM;
  244. memcpy(priv->curve_data, curve_data, sizeof(*curve_data));
  245. source = curve_data->data;
  246. target = priv->curve_data->data;
  247. for (i = 0; i < curve_data->channels; i++) {
  248. __le16 *freq = source;
  249. source += sizeof(__le16);
  250. *((__le16 *)target) = *freq;
  251. target += sizeof(__le16);
  252. for (j = 0; j < curve_data->points_per_channel; j++) {
  253. dst = target;
  254. src = source;
  255. dst->rf_power = src->rf_power;
  256. dst->pa_detector = src->pa_detector;
  257. dst->data_64qam = src->pcv;
  258. /* "invent" the points for the other modulations */
  259. #define SUB(x,y) (u8)((x) - (y)) > (x) ? 0 : (x) - (y)
  260. dst->data_16qam = SUB(src->pcv, 12);
  261. dst->data_qpsk = SUB(dst->data_16qam, 12);
  262. dst->data_bpsk = SUB(dst->data_qpsk, 12);
  263. dst->data_barker = SUB(dst->data_bpsk, 14);
  264. #undef SUB
  265. target += sizeof(*dst);
  266. source += sizeof(*src);
  267. }
  268. }
  269. return 0;
  270. }
  271. static int p54_convert_rev1(struct ieee80211_hw *dev,
  272. struct pda_pa_curve_data *curve_data)
  273. {
  274. struct p54_common *priv = dev->priv;
  275. struct p54_pa_curve_data_sample *dst;
  276. struct pda_pa_curve_data_sample_rev1 *src;
  277. size_t cd_len = sizeof(*curve_data) +
  278. (curve_data->points_per_channel*sizeof(*dst) + 2) *
  279. curve_data->channels;
  280. unsigned int i, j;
  281. void *source, *target;
  282. priv->curve_data = kmalloc(cd_len, GFP_KERNEL);
  283. if (!priv->curve_data)
  284. return -ENOMEM;
  285. memcpy(priv->curve_data, curve_data, sizeof(*curve_data));
  286. source = curve_data->data;
  287. target = priv->curve_data->data;
  288. for (i = 0; i < curve_data->channels; i++) {
  289. __le16 *freq = source;
  290. source += sizeof(__le16);
  291. *((__le16 *)target) = *freq;
  292. target += sizeof(__le16);
  293. for (j = 0; j < curve_data->points_per_channel; j++) {
  294. memcpy(target, source, sizeof(*src));
  295. target += sizeof(*dst);
  296. source += sizeof(*src);
  297. }
  298. source++;
  299. }
  300. return 0;
  301. }
  302. static const char *p54_rf_chips[] = { "NULL", "Duette3", "Duette2",
  303. "Frisbee", "Xbow", "Longbow", "NULL", "NULL" };
  304. static int p54_init_xbow_synth(struct ieee80211_hw *dev);
  305. static int p54_parse_eeprom(struct ieee80211_hw *dev, void *eeprom, int len)
  306. {
  307. struct p54_common *priv = dev->priv;
  308. struct eeprom_pda_wrap *wrap = NULL;
  309. struct pda_entry *entry;
  310. unsigned int data_len, entry_len;
  311. void *tmp;
  312. int err;
  313. u8 *end = (u8 *)eeprom + len;
  314. u16 synth = 0;
  315. wrap = (struct eeprom_pda_wrap *) eeprom;
  316. entry = (void *)wrap->data + le16_to_cpu(wrap->len);
  317. /* verify that at least the entry length/code fits */
  318. while ((u8 *)entry <= end - sizeof(*entry)) {
  319. entry_len = le16_to_cpu(entry->len);
  320. data_len = ((entry_len - 1) << 1);
  321. /* abort if entry exceeds whole structure */
  322. if ((u8 *)entry + sizeof(*entry) + data_len > end)
  323. break;
  324. switch (le16_to_cpu(entry->code)) {
  325. case PDR_MAC_ADDRESS:
  326. SET_IEEE80211_PERM_ADDR(dev, entry->data);
  327. break;
  328. case PDR_PRISM_PA_CAL_OUTPUT_POWER_LIMITS:
  329. if (data_len < 2) {
  330. err = -EINVAL;
  331. goto err;
  332. }
  333. if (2 + entry->data[1]*sizeof(*priv->output_limit) > data_len) {
  334. err = -EINVAL;
  335. goto err;
  336. }
  337. priv->output_limit = kmalloc(entry->data[1] *
  338. sizeof(*priv->output_limit), GFP_KERNEL);
  339. if (!priv->output_limit) {
  340. err = -ENOMEM;
  341. goto err;
  342. }
  343. memcpy(priv->output_limit, &entry->data[2],
  344. entry->data[1]*sizeof(*priv->output_limit));
  345. priv->output_limit_len = entry->data[1];
  346. break;
  347. case PDR_PRISM_PA_CAL_CURVE_DATA: {
  348. struct pda_pa_curve_data *curve_data =
  349. (struct pda_pa_curve_data *)entry->data;
  350. if (data_len < sizeof(*curve_data)) {
  351. err = -EINVAL;
  352. goto err;
  353. }
  354. switch (curve_data->cal_method_rev) {
  355. case 0:
  356. err = p54_convert_rev0(dev, curve_data);
  357. break;
  358. case 1:
  359. err = p54_convert_rev1(dev, curve_data);
  360. break;
  361. default:
  362. printk(KERN_ERR "%s: unknown curve data "
  363. "revision %d\n",
  364. wiphy_name(dev->wiphy),
  365. curve_data->cal_method_rev);
  366. err = -ENODEV;
  367. break;
  368. }
  369. if (err)
  370. goto err;
  371. }
  372. case PDR_PRISM_ZIF_TX_IQ_CALIBRATION:
  373. priv->iq_autocal = kmalloc(data_len, GFP_KERNEL);
  374. if (!priv->iq_autocal) {
  375. err = -ENOMEM;
  376. goto err;
  377. }
  378. memcpy(priv->iq_autocal, entry->data, data_len);
  379. priv->iq_autocal_len = data_len / sizeof(struct pda_iq_autocal_entry);
  380. break;
  381. case PDR_INTERFACE_LIST:
  382. tmp = entry->data;
  383. while ((u8 *)tmp < entry->data + data_len) {
  384. struct bootrec_exp_if *exp_if = tmp;
  385. if (le16_to_cpu(exp_if->if_id) == 0xf)
  386. synth = le16_to_cpu(exp_if->variant);
  387. tmp += sizeof(struct bootrec_exp_if);
  388. }
  389. break;
  390. case PDR_HARDWARE_PLATFORM_COMPONENT_ID:
  391. priv->version = *(u8 *)(entry->data + 1);
  392. break;
  393. case PDR_END:
  394. /* make it overrun */
  395. entry_len = len;
  396. break;
  397. case PDR_MANUFACTURING_PART_NUMBER:
  398. case PDR_PDA_VERSION:
  399. case PDR_NIC_SERIAL_NUMBER:
  400. case PDR_REGULATORY_DOMAIN_LIST:
  401. case PDR_TEMPERATURE_TYPE:
  402. case PDR_PRISM_PCI_IDENTIFIER:
  403. case PDR_COUNTRY_INFORMATION:
  404. case PDR_OEM_NAME:
  405. case PDR_PRODUCT_NAME:
  406. case PDR_UTF8_OEM_NAME:
  407. case PDR_UTF8_PRODUCT_NAME:
  408. case PDR_COUNTRY_LIST:
  409. case PDR_DEFAULT_COUNTRY:
  410. case PDR_ANTENNA_GAIN:
  411. case PDR_PRISM_INDIGO_PA_CALIBRATION_DATA:
  412. case PDR_RSSI_LINEAR_APPROXIMATION:
  413. case PDR_RSSI_LINEAR_APPROXIMATION_DUAL_BAND:
  414. case PDR_REGULATORY_POWER_LIMITS:
  415. case PDR_RSSI_LINEAR_APPROXIMATION_EXTENDED:
  416. case PDR_RADIATED_TRANSMISSION_CORRECTION:
  417. case PDR_PRISM_TX_IQ_CALIBRATION:
  418. case PDR_BASEBAND_REGISTERS:
  419. case PDR_PER_CHANNEL_BASEBAND_REGISTERS:
  420. break;
  421. default:
  422. printk(KERN_INFO "%s: unknown eeprom code : 0x%x\n",
  423. wiphy_name(dev->wiphy),
  424. le16_to_cpu(entry->code));
  425. break;
  426. }
  427. entry = (void *)entry + (entry_len + 1)*2;
  428. }
  429. if (!synth || !priv->iq_autocal || !priv->output_limit ||
  430. !priv->curve_data) {
  431. printk(KERN_ERR "%s: not all required entries found in eeprom!\n",
  432. wiphy_name(dev->wiphy));
  433. err = -EINVAL;
  434. goto err;
  435. }
  436. priv->rxhw = synth & PDR_SYNTH_FRONTEND_MASK;
  437. if (priv->rxhw == 4)
  438. p54_init_xbow_synth(dev);
  439. if (!(synth & PDR_SYNTH_24_GHZ_DISABLED))
  440. dev->wiphy->bands[IEEE80211_BAND_2GHZ] = &band_2GHz;
  441. if (!(synth & PDR_SYNTH_5_GHZ_DISABLED))
  442. dev->wiphy->bands[IEEE80211_BAND_5GHZ] = &band_5GHz;
  443. if (!is_valid_ether_addr(dev->wiphy->perm_addr)) {
  444. u8 perm_addr[ETH_ALEN];
  445. printk(KERN_WARNING "%s: Invalid hwaddr! Using randomly generated MAC addr\n",
  446. wiphy_name(dev->wiphy));
  447. random_ether_addr(perm_addr);
  448. SET_IEEE80211_PERM_ADDR(dev, perm_addr);
  449. }
  450. printk(KERN_INFO "%s: hwaddr %pM, MAC:isl38%02x RF:%s\n",
  451. wiphy_name(dev->wiphy),
  452. dev->wiphy->perm_addr,
  453. priv->version, p54_rf_chips[priv->rxhw]);
  454. return 0;
  455. err:
  456. if (priv->iq_autocal) {
  457. kfree(priv->iq_autocal);
  458. priv->iq_autocal = NULL;
  459. }
  460. if (priv->output_limit) {
  461. kfree(priv->output_limit);
  462. priv->output_limit = NULL;
  463. }
  464. if (priv->curve_data) {
  465. kfree(priv->curve_data);
  466. priv->curve_data = NULL;
  467. }
  468. printk(KERN_ERR "%s: eeprom parse failed!\n",
  469. wiphy_name(dev->wiphy));
  470. return err;
  471. }
  472. static int p54_rssi_to_dbm(struct ieee80211_hw *dev, int rssi)
  473. {
  474. /* TODO: get the rssi_add & rssi_mul data from the eeprom */
  475. return ((rssi * 0x83) / 64 - 400) / 4;
  476. }
  477. static int p54_rx_data(struct ieee80211_hw *dev, struct sk_buff *skb)
  478. {
  479. struct p54_common *priv = dev->priv;
  480. struct p54_rx_data *hdr = (struct p54_rx_data *) skb->data;
  481. struct ieee80211_rx_status rx_status = {0};
  482. u16 freq = le16_to_cpu(hdr->freq);
  483. size_t header_len = sizeof(*hdr);
  484. u32 tsf32;
  485. if (!(hdr->flags & cpu_to_le16(P54_HDR_FLAG_DATA_IN_FCS_GOOD))) {
  486. if (priv->filter_flags & FIF_FCSFAIL)
  487. rx_status.flag |= RX_FLAG_FAILED_FCS_CRC;
  488. else
  489. return 0;
  490. }
  491. if (hdr->decrypt_status == P54_DECRYPT_OK)
  492. rx_status.flag |= RX_FLAG_DECRYPTED;
  493. if ((hdr->decrypt_status == P54_DECRYPT_FAIL_MICHAEL) ||
  494. (hdr->decrypt_status == P54_DECRYPT_FAIL_TKIP))
  495. rx_status.flag |= RX_FLAG_MMIC_ERROR;
  496. rx_status.signal = p54_rssi_to_dbm(dev, hdr->rssi);
  497. rx_status.noise = priv->noise;
  498. /* XX correct? */
  499. rx_status.qual = (100 * hdr->rssi) / 127;
  500. if (hdr->rate & 0x10)
  501. rx_status.flag |= RX_FLAG_SHORTPRE;
  502. rx_status.rate_idx = (dev->conf.channel->band == IEEE80211_BAND_2GHZ ?
  503. hdr->rate : (hdr->rate - 4)) & 0xf;
  504. rx_status.freq = freq;
  505. rx_status.band = dev->conf.channel->band;
  506. rx_status.antenna = hdr->antenna;
  507. tsf32 = le32_to_cpu(hdr->tsf32);
  508. if (tsf32 < priv->tsf_low32)
  509. priv->tsf_high32++;
  510. rx_status.mactime = ((u64)priv->tsf_high32) << 32 | tsf32;
  511. priv->tsf_low32 = tsf32;
  512. rx_status.flag |= RX_FLAG_TSFT;
  513. if (hdr->flags & cpu_to_le16(P54_HDR_FLAG_DATA_ALIGN))
  514. header_len += hdr->align[0];
  515. skb_pull(skb, header_len);
  516. skb_trim(skb, le16_to_cpu(hdr->len));
  517. ieee80211_rx_irqsafe(dev, skb, &rx_status);
  518. return -1;
  519. }
  520. static void inline p54_wake_free_queues(struct ieee80211_hw *dev)
  521. {
  522. struct p54_common *priv = dev->priv;
  523. int i;
  524. if (priv->mode == NL80211_IFTYPE_UNSPECIFIED)
  525. return ;
  526. for (i = 0; i < dev->queues; i++)
  527. if (priv->tx_stats[i + 4].len < priv->tx_stats[i + 4].limit)
  528. ieee80211_wake_queue(dev, i);
  529. }
  530. void p54_free_skb(struct ieee80211_hw *dev, struct sk_buff *skb)
  531. {
  532. struct p54_common *priv = dev->priv;
  533. struct ieee80211_tx_info *info;
  534. struct memrecord *range;
  535. unsigned long flags;
  536. u32 freed = 0, last_addr = priv->rx_start;
  537. if (unlikely(!skb || !dev || !skb_queue_len(&priv->tx_queue)))
  538. return;
  539. spin_lock_irqsave(&priv->tx_queue.lock, flags);
  540. info = IEEE80211_SKB_CB(skb);
  541. range = (void *)info->rate_driver_data;
  542. if (skb->prev != (struct sk_buff *)&priv->tx_queue) {
  543. struct ieee80211_tx_info *ni;
  544. struct memrecord *mr;
  545. ni = IEEE80211_SKB_CB(skb->prev);
  546. mr = (struct memrecord *)ni->rate_driver_data;
  547. last_addr = mr->end_addr;
  548. }
  549. if (skb->next != (struct sk_buff *)&priv->tx_queue) {
  550. struct ieee80211_tx_info *ni;
  551. struct memrecord *mr;
  552. ni = IEEE80211_SKB_CB(skb->next);
  553. mr = (struct memrecord *)ni->rate_driver_data;
  554. freed = mr->start_addr - last_addr;
  555. } else
  556. freed = priv->rx_end - last_addr;
  557. __skb_unlink(skb, &priv->tx_queue);
  558. spin_unlock_irqrestore(&priv->tx_queue.lock, flags);
  559. kfree_skb(skb);
  560. if (freed >= priv->headroom + sizeof(struct p54_hdr) + 48 +
  561. IEEE80211_MAX_RTS_THRESHOLD + priv->tailroom)
  562. p54_wake_free_queues(dev);
  563. }
  564. EXPORT_SYMBOL_GPL(p54_free_skb);
  565. static void p54_rx_frame_sent(struct ieee80211_hw *dev, struct sk_buff *skb)
  566. {
  567. struct p54_common *priv = dev->priv;
  568. struct p54_hdr *hdr = (struct p54_hdr *) skb->data;
  569. struct p54_frame_sent *payload = (struct p54_frame_sent *) hdr->data;
  570. struct sk_buff *entry = (struct sk_buff *) priv->tx_queue.next;
  571. u32 addr = le32_to_cpu(hdr->req_id) - priv->headroom;
  572. struct memrecord *range = NULL;
  573. u32 freed = 0;
  574. u32 last_addr = priv->rx_start;
  575. unsigned long flags;
  576. int count, idx;
  577. spin_lock_irqsave(&priv->tx_queue.lock, flags);
  578. while (entry != (struct sk_buff *)&priv->tx_queue) {
  579. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(entry);
  580. struct p54_hdr *entry_hdr;
  581. struct p54_tx_data *entry_data;
  582. int pad = 0;
  583. range = (void *)info->rate_driver_data;
  584. if (range->start_addr != addr) {
  585. last_addr = range->end_addr;
  586. entry = entry->next;
  587. continue;
  588. }
  589. if (entry->next != (struct sk_buff *)&priv->tx_queue) {
  590. struct ieee80211_tx_info *ni;
  591. struct memrecord *mr;
  592. ni = IEEE80211_SKB_CB(entry->next);
  593. mr = (struct memrecord *)ni->rate_driver_data;
  594. freed = mr->start_addr - last_addr;
  595. } else
  596. freed = priv->rx_end - last_addr;
  597. last_addr = range->end_addr;
  598. __skb_unlink(entry, &priv->tx_queue);
  599. spin_unlock_irqrestore(&priv->tx_queue.lock, flags);
  600. entry_hdr = (struct p54_hdr *) entry->data;
  601. entry_data = (struct p54_tx_data *) entry_hdr->data;
  602. priv->tx_stats[entry_data->hw_queue].len--;
  603. if (unlikely(entry == priv->cached_beacon)) {
  604. kfree_skb(entry);
  605. priv->cached_beacon = NULL;
  606. goto out;
  607. }
  608. /*
  609. * Clear manually, ieee80211_tx_info_clear_status would
  610. * clear the counts too and we need them.
  611. */
  612. memset(&info->status.ampdu_ack_len, 0,
  613. sizeof(struct ieee80211_tx_info) -
  614. offsetof(struct ieee80211_tx_info, status.ampdu_ack_len));
  615. BUILD_BUG_ON(offsetof(struct ieee80211_tx_info,
  616. status.ampdu_ack_len) != 23);
  617. if (entry_hdr->flags & cpu_to_le16(P54_HDR_FLAG_DATA_ALIGN))
  618. pad = entry_data->align[0];
  619. /* walk through the rates array and adjust the counts */
  620. count = payload->tries;
  621. for (idx = 0; idx < 4; idx++) {
  622. if (count >= info->status.rates[idx].count) {
  623. count -= info->status.rates[idx].count;
  624. } else if (count > 0) {
  625. info->status.rates[idx].count = count;
  626. count = 0;
  627. } else {
  628. info->status.rates[idx].idx = -1;
  629. info->status.rates[idx].count = 0;
  630. }
  631. }
  632. if (!(info->flags & IEEE80211_TX_CTL_NO_ACK) &&
  633. (!payload->status))
  634. info->flags |= IEEE80211_TX_STAT_ACK;
  635. if (payload->status & P54_TX_PSM_CANCELLED)
  636. info->flags |= IEEE80211_TX_STAT_TX_FILTERED;
  637. info->status.ack_signal = p54_rssi_to_dbm(dev,
  638. (int)payload->ack_rssi);
  639. skb_pull(entry, sizeof(*hdr) + pad + sizeof(*entry_data));
  640. ieee80211_tx_status_irqsafe(dev, entry);
  641. goto out;
  642. }
  643. spin_unlock_irqrestore(&priv->tx_queue.lock, flags);
  644. out:
  645. if (freed >= priv->headroom + sizeof(struct p54_hdr) + 48 +
  646. IEEE80211_MAX_RTS_THRESHOLD + priv->tailroom)
  647. p54_wake_free_queues(dev);
  648. }
  649. static void p54_rx_eeprom_readback(struct ieee80211_hw *dev,
  650. struct sk_buff *skb)
  651. {
  652. struct p54_hdr *hdr = (struct p54_hdr *) skb->data;
  653. struct p54_eeprom_lm86 *eeprom = (struct p54_eeprom_lm86 *) hdr->data;
  654. struct p54_common *priv = dev->priv;
  655. if (!priv->eeprom)
  656. return ;
  657. if (priv->fw_var >= 0x509) {
  658. memcpy(priv->eeprom, eeprom->v2.data,
  659. le16_to_cpu(eeprom->v2.len));
  660. } else {
  661. memcpy(priv->eeprom, eeprom->v1.data,
  662. le16_to_cpu(eeprom->v1.len));
  663. }
  664. complete(&priv->eeprom_comp);
  665. }
  666. static void p54_rx_stats(struct ieee80211_hw *dev, struct sk_buff *skb)
  667. {
  668. struct p54_common *priv = dev->priv;
  669. struct p54_hdr *hdr = (struct p54_hdr *) skb->data;
  670. struct p54_statistics *stats = (struct p54_statistics *) hdr->data;
  671. u32 tsf32 = le32_to_cpu(stats->tsf32);
  672. if (tsf32 < priv->tsf_low32)
  673. priv->tsf_high32++;
  674. priv->tsf_low32 = tsf32;
  675. priv->stats.dot11RTSFailureCount = le32_to_cpu(stats->rts_fail);
  676. priv->stats.dot11RTSSuccessCount = le32_to_cpu(stats->rts_success);
  677. priv->stats.dot11FCSErrorCount = le32_to_cpu(stats->rx_bad_fcs);
  678. priv->noise = p54_rssi_to_dbm(dev, le32_to_cpu(stats->noise));
  679. complete(&priv->stats_comp);
  680. mod_timer(&priv->stats_timer, jiffies + 5 * HZ);
  681. }
  682. static void p54_rx_trap(struct ieee80211_hw *dev, struct sk_buff *skb)
  683. {
  684. struct p54_hdr *hdr = (struct p54_hdr *) skb->data;
  685. struct p54_trap *trap = (struct p54_trap *) hdr->data;
  686. u16 event = le16_to_cpu(trap->event);
  687. u16 freq = le16_to_cpu(trap->frequency);
  688. switch (event) {
  689. case P54_TRAP_BEACON_TX:
  690. break;
  691. case P54_TRAP_RADAR:
  692. printk(KERN_INFO "%s: radar (freq:%d MHz)\n",
  693. wiphy_name(dev->wiphy), freq);
  694. break;
  695. case P54_TRAP_NO_BEACON:
  696. break;
  697. case P54_TRAP_SCAN:
  698. break;
  699. case P54_TRAP_TBTT:
  700. break;
  701. case P54_TRAP_TIMER:
  702. break;
  703. default:
  704. printk(KERN_INFO "%s: received event:%x freq:%d\n",
  705. wiphy_name(dev->wiphy), event, freq);
  706. break;
  707. }
  708. }
  709. static int p54_rx_control(struct ieee80211_hw *dev, struct sk_buff *skb)
  710. {
  711. struct p54_hdr *hdr = (struct p54_hdr *) skb->data;
  712. switch (le16_to_cpu(hdr->type)) {
  713. case P54_CONTROL_TYPE_TXDONE:
  714. p54_rx_frame_sent(dev, skb);
  715. break;
  716. case P54_CONTROL_TYPE_TRAP:
  717. p54_rx_trap(dev, skb);
  718. break;
  719. case P54_CONTROL_TYPE_BBP:
  720. break;
  721. case P54_CONTROL_TYPE_STAT_READBACK:
  722. p54_rx_stats(dev, skb);
  723. break;
  724. case P54_CONTROL_TYPE_EEPROM_READBACK:
  725. p54_rx_eeprom_readback(dev, skb);
  726. break;
  727. default:
  728. printk(KERN_DEBUG "%s: not handling 0x%02x type control frame\n",
  729. wiphy_name(dev->wiphy), le16_to_cpu(hdr->type));
  730. break;
  731. }
  732. return 0;
  733. }
  734. /* returns zero if skb can be reused */
  735. int p54_rx(struct ieee80211_hw *dev, struct sk_buff *skb)
  736. {
  737. u16 type = le16_to_cpu(*((__le16 *)skb->data));
  738. if (type & P54_HDR_FLAG_CONTROL)
  739. return p54_rx_control(dev, skb);
  740. else
  741. return p54_rx_data(dev, skb);
  742. }
  743. EXPORT_SYMBOL_GPL(p54_rx);
  744. /*
  745. * So, the firmware is somewhat stupid and doesn't know what places in its
  746. * memory incoming data should go to. By poking around in the firmware, we
  747. * can find some unused memory to upload our packets to. However, data that we
  748. * want the card to TX needs to stay intact until the card has told us that
  749. * it is done with it. This function finds empty places we can upload to and
  750. * marks allocated areas as reserved if necessary. p54_rx_frame_sent frees
  751. * allocated areas.
  752. */
  753. static int p54_assign_address(struct ieee80211_hw *dev, struct sk_buff *skb,
  754. struct p54_hdr *data, u32 len)
  755. {
  756. struct p54_common *priv = dev->priv;
  757. struct sk_buff *entry = priv->tx_queue.next;
  758. struct sk_buff *target_skb = NULL;
  759. struct ieee80211_tx_info *info;
  760. struct memrecord *range;
  761. u32 last_addr = priv->rx_start;
  762. u32 largest_hole = 0;
  763. u32 target_addr = priv->rx_start;
  764. unsigned long flags;
  765. unsigned int left;
  766. len = (len + priv->headroom + priv->tailroom + 3) & ~0x3;
  767. if (!skb)
  768. return -EINVAL;
  769. spin_lock_irqsave(&priv->tx_queue.lock, flags);
  770. left = skb_queue_len(&priv->tx_queue);
  771. if (unlikely(left >= 28)) {
  772. /*
  773. * The tx_queue is nearly full!
  774. * We have throttle normal data traffic, because we must
  775. * have a few spare slots for control frames left.
  776. */
  777. ieee80211_stop_queues(dev);
  778. if (unlikely(left == 32)) {
  779. /*
  780. * The tx_queue is now really full.
  781. *
  782. * TODO: check if the device has crashed and reset it.
  783. */
  784. spin_unlock_irqrestore(&priv->tx_queue.lock, flags);
  785. return -ENOSPC;
  786. }
  787. }
  788. while (left--) {
  789. u32 hole_size;
  790. info = IEEE80211_SKB_CB(entry);
  791. range = (void *)info->rate_driver_data;
  792. hole_size = range->start_addr - last_addr;
  793. if (!target_skb && hole_size >= len) {
  794. target_skb = entry->prev;
  795. hole_size -= len;
  796. target_addr = last_addr;
  797. }
  798. largest_hole = max(largest_hole, hole_size);
  799. last_addr = range->end_addr;
  800. entry = entry->next;
  801. }
  802. if (!target_skb && priv->rx_end - last_addr >= len) {
  803. target_skb = priv->tx_queue.prev;
  804. largest_hole = max(largest_hole, priv->rx_end - last_addr - len);
  805. if (!skb_queue_empty(&priv->tx_queue)) {
  806. info = IEEE80211_SKB_CB(target_skb);
  807. range = (void *)info->rate_driver_data;
  808. target_addr = range->end_addr;
  809. }
  810. } else
  811. largest_hole = max(largest_hole, priv->rx_end - last_addr);
  812. if (!target_skb) {
  813. spin_unlock_irqrestore(&priv->tx_queue.lock, flags);
  814. ieee80211_stop_queues(dev);
  815. return -ENOSPC;
  816. }
  817. info = IEEE80211_SKB_CB(skb);
  818. range = (void *)info->rate_driver_data;
  819. range->start_addr = target_addr;
  820. range->end_addr = target_addr + len;
  821. __skb_queue_after(&priv->tx_queue, target_skb, skb);
  822. spin_unlock_irqrestore(&priv->tx_queue.lock, flags);
  823. if (largest_hole < priv->headroom + sizeof(struct p54_hdr) +
  824. 48 + IEEE80211_MAX_RTS_THRESHOLD + priv->tailroom)
  825. ieee80211_stop_queues(dev);
  826. data->req_id = cpu_to_le32(target_addr + priv->headroom);
  827. return 0;
  828. }
  829. static struct sk_buff *p54_alloc_skb(struct ieee80211_hw *dev,
  830. u16 hdr_flags, u16 len, u16 type, gfp_t memflags)
  831. {
  832. struct p54_common *priv = dev->priv;
  833. struct p54_hdr *hdr;
  834. struct sk_buff *skb;
  835. skb = __dev_alloc_skb(len + priv->tx_hdr_len, memflags);
  836. if (!skb)
  837. return NULL;
  838. skb_reserve(skb, priv->tx_hdr_len);
  839. hdr = (struct p54_hdr *) skb_put(skb, sizeof(*hdr));
  840. hdr->flags = cpu_to_le16(hdr_flags);
  841. hdr->len = cpu_to_le16(len - sizeof(*hdr));
  842. hdr->type = cpu_to_le16(type);
  843. hdr->tries = hdr->rts_tries = 0;
  844. if (unlikely(p54_assign_address(dev, skb, hdr, len))) {
  845. kfree_skb(skb);
  846. return NULL;
  847. }
  848. return skb;
  849. }
  850. int p54_read_eeprom(struct ieee80211_hw *dev)
  851. {
  852. struct p54_common *priv = dev->priv;
  853. struct p54_hdr *hdr = NULL;
  854. struct p54_eeprom_lm86 *eeprom_hdr;
  855. struct sk_buff *skb;
  856. size_t eeprom_size = 0x2020, offset = 0, blocksize, maxblocksize;
  857. int ret = -ENOMEM;
  858. void *eeprom = NULL;
  859. maxblocksize = EEPROM_READBACK_LEN;
  860. if (priv->fw_var >= 0x509)
  861. maxblocksize -= 0xc;
  862. else
  863. maxblocksize -= 0x4;
  864. skb = p54_alloc_skb(dev, P54_HDR_FLAG_CONTROL, sizeof(*hdr) +
  865. sizeof(*eeprom_hdr) + maxblocksize,
  866. P54_CONTROL_TYPE_EEPROM_READBACK, GFP_KERNEL);
  867. if (!skb)
  868. goto free;
  869. priv->eeprom = kzalloc(EEPROM_READBACK_LEN, GFP_KERNEL);
  870. if (!priv->eeprom)
  871. goto free;
  872. eeprom = kzalloc(eeprom_size, GFP_KERNEL);
  873. if (!eeprom)
  874. goto free;
  875. eeprom_hdr = (struct p54_eeprom_lm86 *) skb_put(skb,
  876. sizeof(*eeprom_hdr) + maxblocksize);
  877. while (eeprom_size) {
  878. blocksize = min(eeprom_size, maxblocksize);
  879. if (priv->fw_var < 0x509) {
  880. eeprom_hdr->v1.offset = cpu_to_le16(offset);
  881. eeprom_hdr->v1.len = cpu_to_le16(blocksize);
  882. } else {
  883. eeprom_hdr->v2.offset = cpu_to_le32(offset);
  884. eeprom_hdr->v2.len = cpu_to_le16(blocksize);
  885. eeprom_hdr->v2.magic2 = 0xf;
  886. memcpy(eeprom_hdr->v2.magic, (const char *)"LOCK", 4);
  887. }
  888. priv->tx(dev, skb, 0);
  889. if (!wait_for_completion_interruptible_timeout(&priv->eeprom_comp, HZ)) {
  890. printk(KERN_ERR "%s: device does not respond!\n",
  891. wiphy_name(dev->wiphy));
  892. ret = -EBUSY;
  893. goto free;
  894. }
  895. memcpy(eeprom + offset, priv->eeprom, blocksize);
  896. offset += blocksize;
  897. eeprom_size -= blocksize;
  898. }
  899. ret = p54_parse_eeprom(dev, eeprom, offset);
  900. free:
  901. kfree(priv->eeprom);
  902. priv->eeprom = NULL;
  903. p54_free_skb(dev, skb);
  904. kfree(eeprom);
  905. return ret;
  906. }
  907. EXPORT_SYMBOL_GPL(p54_read_eeprom);
  908. static int p54_set_tim(struct ieee80211_hw *dev, struct ieee80211_sta *sta,
  909. bool set)
  910. {
  911. struct p54_common *priv = dev->priv;
  912. struct sk_buff *skb;
  913. struct p54_tim *tim;
  914. skb = p54_alloc_skb(dev, P54_HDR_FLAG_CONTROL_OPSET,
  915. sizeof(struct p54_hdr) + sizeof(*tim),
  916. P54_CONTROL_TYPE_TIM, GFP_KERNEL);
  917. if (!skb)
  918. return -ENOMEM;
  919. tim = (struct p54_tim *) skb_put(skb, sizeof(*tim));
  920. tim->count = 1;
  921. tim->entry[0] = cpu_to_le16(set ? (sta->aid | 0x8000) : sta->aid);
  922. priv->tx(dev, skb, 1);
  923. return 0;
  924. }
  925. static int p54_sta_unlock(struct ieee80211_hw *dev, u8 *addr)
  926. {
  927. struct p54_common *priv = dev->priv;
  928. struct sk_buff *skb;
  929. struct p54_sta_unlock *sta;
  930. skb = p54_alloc_skb(dev, P54_HDR_FLAG_CONTROL_OPSET,
  931. sizeof(struct p54_hdr) + sizeof(*sta),
  932. P54_CONTROL_TYPE_PSM_STA_UNLOCK, GFP_ATOMIC);
  933. if (!skb)
  934. return -ENOMEM;
  935. sta = (struct p54_sta_unlock *)skb_put(skb, sizeof(*sta));
  936. memcpy(sta->addr, addr, ETH_ALEN);
  937. priv->tx(dev, skb, 1);
  938. return 0;
  939. }
  940. static void p54_sta_notify(struct ieee80211_hw *dev, struct ieee80211_vif *vif,
  941. enum sta_notify_cmd notify_cmd,
  942. struct ieee80211_sta *sta)
  943. {
  944. switch (notify_cmd) {
  945. case STA_NOTIFY_ADD:
  946. case STA_NOTIFY_REMOVE:
  947. /*
  948. * Notify the firmware that we don't want or we don't
  949. * need to buffer frames for this station anymore.
  950. */
  951. p54_sta_unlock(dev, sta->addr);
  952. break;
  953. case STA_NOTIFY_AWAKE:
  954. /* update the firmware's filter table */
  955. p54_sta_unlock(dev, sta->addr);
  956. break;
  957. default:
  958. break;
  959. }
  960. }
  961. static int p54_tx_cancel(struct ieee80211_hw *dev, struct sk_buff *entry)
  962. {
  963. struct p54_common *priv = dev->priv;
  964. struct sk_buff *skb;
  965. struct p54_hdr *hdr;
  966. struct p54_txcancel *cancel;
  967. skb = p54_alloc_skb(dev, P54_HDR_FLAG_CONTROL_OPSET,
  968. sizeof(struct p54_hdr) + sizeof(*cancel),
  969. P54_CONTROL_TYPE_TXCANCEL, GFP_ATOMIC);
  970. if (!skb)
  971. return -ENOMEM;
  972. hdr = (void *)entry->data;
  973. cancel = (struct p54_txcancel *)skb_put(skb, sizeof(*cancel));
  974. cancel->req_id = hdr->req_id;
  975. priv->tx(dev, skb, 1);
  976. return 0;
  977. }
  978. static int p54_tx_fill(struct ieee80211_hw *dev, struct sk_buff *skb,
  979. struct ieee80211_tx_info *info, u8 *queue, size_t *extra_len,
  980. u16 *flags, u16 *aid)
  981. {
  982. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  983. struct p54_common *priv = dev->priv;
  984. int ret = 0;
  985. if (unlikely(ieee80211_is_mgmt(hdr->frame_control))) {
  986. if (ieee80211_is_beacon(hdr->frame_control)) {
  987. *aid = 0;
  988. *queue = 0;
  989. *extra_len = IEEE80211_MAX_TIM_LEN;
  990. *flags = P54_HDR_FLAG_DATA_OUT_TIMESTAMP;
  991. return 0;
  992. } else if (ieee80211_is_probe_resp(hdr->frame_control)) {
  993. *aid = 0;
  994. *queue = 2;
  995. *flags = P54_HDR_FLAG_DATA_OUT_TIMESTAMP |
  996. P54_HDR_FLAG_DATA_OUT_NOCANCEL;
  997. return 0;
  998. } else {
  999. *queue = 2;
  1000. ret = 0;
  1001. }
  1002. } else {
  1003. *queue += 4;
  1004. ret = 1;
  1005. }
  1006. switch (priv->mode) {
  1007. case NL80211_IFTYPE_STATION:
  1008. *aid = 1;
  1009. break;
  1010. case NL80211_IFTYPE_AP:
  1011. case NL80211_IFTYPE_ADHOC:
  1012. case NL80211_IFTYPE_MESH_POINT:
  1013. if (info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM) {
  1014. *aid = 0;
  1015. *queue = 3;
  1016. return 0;
  1017. }
  1018. if (info->control.sta)
  1019. *aid = info->control.sta->aid;
  1020. else
  1021. *flags |= P54_HDR_FLAG_DATA_OUT_NOCANCEL;
  1022. }
  1023. return ret;
  1024. }
  1025. static u8 p54_convert_algo(enum ieee80211_key_alg alg)
  1026. {
  1027. switch (alg) {
  1028. case ALG_WEP:
  1029. return P54_CRYPTO_WEP;
  1030. case ALG_TKIP:
  1031. return P54_CRYPTO_TKIPMICHAEL;
  1032. case ALG_CCMP:
  1033. return P54_CRYPTO_AESCCMP;
  1034. default:
  1035. return 0;
  1036. }
  1037. }
  1038. static int p54_tx(struct ieee80211_hw *dev, struct sk_buff *skb)
  1039. {
  1040. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  1041. struct ieee80211_tx_queue_stats *current_queue = NULL;
  1042. struct p54_common *priv = dev->priv;
  1043. struct p54_hdr *hdr;
  1044. struct p54_tx_data *txhdr;
  1045. size_t padding, len, tim_len = 0;
  1046. int i, j, ridx, ret;
  1047. u16 hdr_flags = 0, aid = 0;
  1048. u8 rate, queue, crypt_offset = 0;
  1049. u8 cts_rate = 0x20;
  1050. u8 rc_flags;
  1051. u8 calculated_tries[4];
  1052. u8 nrates = 0, nremaining = 8;
  1053. queue = skb_get_queue_mapping(skb);
  1054. ret = p54_tx_fill(dev, skb, info, &queue, &tim_len, &hdr_flags, &aid);
  1055. current_queue = &priv->tx_stats[queue];
  1056. if (unlikely((current_queue->len > current_queue->limit) && ret))
  1057. return NETDEV_TX_BUSY;
  1058. current_queue->len++;
  1059. current_queue->count++;
  1060. if ((current_queue->len == current_queue->limit) && ret)
  1061. ieee80211_stop_queue(dev, skb_get_queue_mapping(skb));
  1062. padding = (unsigned long)(skb->data - (sizeof(*hdr) + sizeof(*txhdr))) & 3;
  1063. len = skb->len;
  1064. if (info->control.hw_key) {
  1065. crypt_offset = ieee80211_get_hdrlen_from_skb(skb);
  1066. if (info->control.hw_key->alg == ALG_TKIP) {
  1067. u8 *iv = (u8 *)(skb->data + crypt_offset);
  1068. /*
  1069. * The firmware excepts that the IV has to have
  1070. * this special format
  1071. */
  1072. iv[1] = iv[0];
  1073. iv[0] = iv[2];
  1074. iv[2] = 0;
  1075. }
  1076. }
  1077. txhdr = (struct p54_tx_data *) skb_push(skb, sizeof(*txhdr) + padding);
  1078. hdr = (struct p54_hdr *) skb_push(skb, sizeof(*hdr));
  1079. if (padding)
  1080. hdr_flags |= P54_HDR_FLAG_DATA_ALIGN;
  1081. hdr->type = cpu_to_le16(aid);
  1082. hdr->rts_tries = info->control.rates[0].count;
  1083. /*
  1084. * we register the rates in perfect order, and
  1085. * RTS/CTS won't happen on 5 GHz
  1086. */
  1087. cts_rate = info->control.rts_cts_rate_idx;
  1088. memset(&txhdr->rateset, 0, sizeof(txhdr->rateset));
  1089. /* see how many rates got used */
  1090. for (i = 0; i < 4; i++) {
  1091. if (info->control.rates[i].idx < 0)
  1092. break;
  1093. nrates++;
  1094. }
  1095. /* limit tries to 8/nrates per rate */
  1096. for (i = 0; i < nrates; i++) {
  1097. /*
  1098. * The magic expression here is equivalent to 8/nrates for
  1099. * all values that matter, but avoids division and jumps.
  1100. * Note that nrates can only take the values 1 through 4.
  1101. */
  1102. calculated_tries[i] = min_t(int, ((15 >> nrates) | 1) + 1,
  1103. info->control.rates[i].count);
  1104. nremaining -= calculated_tries[i];
  1105. }
  1106. /* if there are tries left, distribute from back to front */
  1107. for (i = nrates - 1; nremaining > 0 && i >= 0; i--) {
  1108. int tmp = info->control.rates[i].count - calculated_tries[i];
  1109. if (tmp <= 0)
  1110. continue;
  1111. /* RC requested more tries at this rate */
  1112. tmp = min_t(int, tmp, nremaining);
  1113. calculated_tries[i] += tmp;
  1114. nremaining -= tmp;
  1115. }
  1116. ridx = 0;
  1117. for (i = 0; i < nrates && ridx < 8; i++) {
  1118. /* we register the rates in perfect order */
  1119. rate = info->control.rates[i].idx;
  1120. if (info->band == IEEE80211_BAND_5GHZ)
  1121. rate += 4;
  1122. /* store the count we actually calculated for TX status */
  1123. info->control.rates[i].count = calculated_tries[i];
  1124. rc_flags = info->control.rates[i].flags;
  1125. if (rc_flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE) {
  1126. rate |= 0x10;
  1127. cts_rate |= 0x10;
  1128. }
  1129. if (rc_flags & IEEE80211_TX_RC_USE_RTS_CTS)
  1130. rate |= 0x40;
  1131. else if (rc_flags & IEEE80211_TX_RC_USE_CTS_PROTECT)
  1132. rate |= 0x20;
  1133. for (j = 0; j < calculated_tries[i] && ridx < 8; j++) {
  1134. txhdr->rateset[ridx] = rate;
  1135. ridx++;
  1136. }
  1137. }
  1138. if (info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ)
  1139. hdr_flags |= P54_HDR_FLAG_DATA_OUT_SEQNR;
  1140. /* TODO: enable bursting */
  1141. hdr->flags = cpu_to_le16(hdr_flags);
  1142. hdr->tries = ridx;
  1143. txhdr->rts_rate_idx = 0;
  1144. if (info->control.hw_key) {
  1145. crypt_offset += info->control.hw_key->iv_len;
  1146. txhdr->key_type = p54_convert_algo(info->control.hw_key->alg);
  1147. txhdr->key_len = min((u8)16, info->control.hw_key->keylen);
  1148. memcpy(txhdr->key, info->control.hw_key->key, txhdr->key_len);
  1149. if (info->control.hw_key->alg == ALG_TKIP) {
  1150. if (unlikely(skb_tailroom(skb) < 12))
  1151. goto err;
  1152. /* reserve space for the MIC key */
  1153. len += 8;
  1154. memcpy(skb_put(skb, 8), &(info->control.hw_key->key
  1155. [NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY]), 8);
  1156. }
  1157. /* reserve some space for ICV */
  1158. len += info->control.hw_key->icv_len;
  1159. } else {
  1160. txhdr->key_type = 0;
  1161. txhdr->key_len = 0;
  1162. }
  1163. txhdr->crypt_offset = crypt_offset;
  1164. txhdr->hw_queue = queue;
  1165. if (current_queue)
  1166. txhdr->backlog = current_queue->len;
  1167. else
  1168. txhdr->backlog = 0;
  1169. memset(txhdr->durations, 0, sizeof(txhdr->durations));
  1170. txhdr->tx_antenna = (info->antenna_sel_tx == 0) ?
  1171. 2 : info->antenna_sel_tx - 1;
  1172. txhdr->output_power = priv->output_power;
  1173. txhdr->cts_rate = cts_rate;
  1174. if (padding)
  1175. txhdr->align[0] = padding;
  1176. hdr->len = cpu_to_le16(len);
  1177. /* modifies skb->cb and with it info, so must be last! */
  1178. if (unlikely(p54_assign_address(dev, skb, hdr, skb->len + tim_len)))
  1179. goto err;
  1180. priv->tx(dev, skb, 0);
  1181. return 0;
  1182. err:
  1183. skb_pull(skb, sizeof(*hdr) + sizeof(*txhdr) + padding);
  1184. if (current_queue) {
  1185. current_queue->len--;
  1186. current_queue->count--;
  1187. }
  1188. return NETDEV_TX_BUSY;
  1189. }
  1190. static int p54_setup_mac(struct ieee80211_hw *dev)
  1191. {
  1192. struct p54_common *priv = dev->priv;
  1193. struct sk_buff *skb;
  1194. struct p54_setup_mac *setup;
  1195. u16 mode;
  1196. skb = p54_alloc_skb(dev, P54_HDR_FLAG_CONTROL_OPSET, sizeof(*setup) +
  1197. sizeof(struct p54_hdr), P54_CONTROL_TYPE_SETUP,
  1198. GFP_ATOMIC);
  1199. if (!skb)
  1200. return -ENOMEM;
  1201. setup = (struct p54_setup_mac *) skb_put(skb, sizeof(*setup));
  1202. if (dev->conf.radio_enabled) {
  1203. switch (priv->mode) {
  1204. case NL80211_IFTYPE_STATION:
  1205. mode = P54_FILTER_TYPE_STATION;
  1206. break;
  1207. case NL80211_IFTYPE_AP:
  1208. mode = P54_FILTER_TYPE_AP;
  1209. break;
  1210. case NL80211_IFTYPE_ADHOC:
  1211. case NL80211_IFTYPE_MESH_POINT:
  1212. mode = P54_FILTER_TYPE_IBSS;
  1213. break;
  1214. default:
  1215. mode = P54_FILTER_TYPE_NONE;
  1216. break;
  1217. }
  1218. if (priv->filter_flags & FIF_PROMISC_IN_BSS)
  1219. mode |= P54_FILTER_TYPE_TRANSPARENT;
  1220. } else
  1221. mode = P54_FILTER_TYPE_RX_DISABLED;
  1222. setup->mac_mode = cpu_to_le16(mode);
  1223. memcpy(setup->mac_addr, priv->mac_addr, ETH_ALEN);
  1224. memcpy(setup->bssid, priv->bssid, ETH_ALEN);
  1225. setup->rx_antenna = 2; /* automatic */
  1226. setup->rx_align = 0;
  1227. if (priv->fw_var < 0x500) {
  1228. setup->v1.basic_rate_mask = cpu_to_le32(priv->basic_rate_mask);
  1229. memset(setup->v1.rts_rates, 0, 8);
  1230. setup->v1.rx_addr = cpu_to_le32(priv->rx_end);
  1231. setup->v1.max_rx = cpu_to_le16(priv->rx_mtu);
  1232. setup->v1.rxhw = cpu_to_le16(priv->rxhw);
  1233. setup->v1.wakeup_timer = cpu_to_le16(priv->wakeup_timer);
  1234. setup->v1.unalloc0 = cpu_to_le16(0);
  1235. } else {
  1236. setup->v2.rx_addr = cpu_to_le32(priv->rx_end);
  1237. setup->v2.max_rx = cpu_to_le16(priv->rx_mtu);
  1238. setup->v2.rxhw = cpu_to_le16(priv->rxhw);
  1239. setup->v2.timer = cpu_to_le16(priv->wakeup_timer);
  1240. setup->v2.truncate = cpu_to_le16(48896);
  1241. setup->v2.basic_rate_mask = cpu_to_le32(priv->basic_rate_mask);
  1242. setup->v2.sbss_offset = 0;
  1243. setup->v2.mcast_window = 0;
  1244. setup->v2.rx_rssi_threshold = 0;
  1245. setup->v2.rx_ed_threshold = 0;
  1246. setup->v2.ref_clock = cpu_to_le32(644245094);
  1247. setup->v2.lpf_bandwidth = cpu_to_le16(65535);
  1248. setup->v2.osc_start_delay = cpu_to_le16(65535);
  1249. }
  1250. priv->tx(dev, skb, 1);
  1251. return 0;
  1252. }
  1253. static int p54_scan(struct ieee80211_hw *dev, u16 mode, u16 dwell,
  1254. u16 frequency)
  1255. {
  1256. struct p54_common *priv = dev->priv;
  1257. struct sk_buff *skb;
  1258. struct p54_scan *chan;
  1259. unsigned int i;
  1260. void *entry;
  1261. __le16 freq = cpu_to_le16(frequency);
  1262. skb = p54_alloc_skb(dev, P54_HDR_FLAG_CONTROL_OPSET, sizeof(*chan) +
  1263. sizeof(struct p54_hdr), P54_CONTROL_TYPE_SCAN,
  1264. GFP_ATOMIC);
  1265. if (!skb)
  1266. return -ENOMEM;
  1267. chan = (struct p54_scan *) skb_put(skb, sizeof(*chan));
  1268. memset(chan->padding1, 0, sizeof(chan->padding1));
  1269. chan->mode = cpu_to_le16(mode);
  1270. chan->dwell = cpu_to_le16(dwell);
  1271. for (i = 0; i < priv->iq_autocal_len; i++) {
  1272. if (priv->iq_autocal[i].freq != freq)
  1273. continue;
  1274. memcpy(&chan->iq_autocal, &priv->iq_autocal[i],
  1275. sizeof(*priv->iq_autocal));
  1276. break;
  1277. }
  1278. if (i == priv->iq_autocal_len)
  1279. goto err;
  1280. for (i = 0; i < priv->output_limit_len; i++) {
  1281. if (priv->output_limit[i].freq != freq)
  1282. continue;
  1283. chan->val_barker = 0x38;
  1284. chan->val_bpsk = chan->dup_bpsk =
  1285. priv->output_limit[i].val_bpsk;
  1286. chan->val_qpsk = chan->dup_qpsk =
  1287. priv->output_limit[i].val_qpsk;
  1288. chan->val_16qam = chan->dup_16qam =
  1289. priv->output_limit[i].val_16qam;
  1290. chan->val_64qam = chan->dup_64qam =
  1291. priv->output_limit[i].val_64qam;
  1292. break;
  1293. }
  1294. if (i == priv->output_limit_len)
  1295. goto err;
  1296. entry = priv->curve_data->data;
  1297. for (i = 0; i < priv->curve_data->channels; i++) {
  1298. if (*((__le16 *)entry) != freq) {
  1299. entry += sizeof(__le16);
  1300. entry += sizeof(struct p54_pa_curve_data_sample) *
  1301. priv->curve_data->points_per_channel;
  1302. continue;
  1303. }
  1304. entry += sizeof(__le16);
  1305. chan->pa_points_per_curve = 8;
  1306. memset(chan->curve_data, 0, sizeof(*chan->curve_data));
  1307. memcpy(chan->curve_data, entry,
  1308. sizeof(struct p54_pa_curve_data_sample) *
  1309. min((u8)8, priv->curve_data->points_per_channel));
  1310. break;
  1311. }
  1312. if (priv->fw_var < 0x500) {
  1313. chan->v1.rssical_mul = cpu_to_le16(130);
  1314. chan->v1.rssical_add = cpu_to_le16(0xfe70);
  1315. } else {
  1316. chan->v2.rssical_mul = cpu_to_le16(130);
  1317. chan->v2.rssical_add = cpu_to_le16(0xfe70);
  1318. chan->v2.basic_rate_mask = cpu_to_le32(priv->basic_rate_mask);
  1319. memset(chan->v2.rts_rates, 0, 8);
  1320. }
  1321. priv->tx(dev, skb, 1);
  1322. return 0;
  1323. err:
  1324. printk(KERN_ERR "%s: frequency change failed\n", wiphy_name(dev->wiphy));
  1325. kfree_skb(skb);
  1326. return -EINVAL;
  1327. }
  1328. static int p54_set_leds(struct ieee80211_hw *dev, int mode, int link, int act)
  1329. {
  1330. struct p54_common *priv = dev->priv;
  1331. struct sk_buff *skb;
  1332. struct p54_led *led;
  1333. skb = p54_alloc_skb(dev, P54_HDR_FLAG_CONTROL_OPSET, sizeof(*led) +
  1334. sizeof(struct p54_hdr), P54_CONTROL_TYPE_LED,
  1335. GFP_ATOMIC);
  1336. if (!skb)
  1337. return -ENOMEM;
  1338. led = (struct p54_led *)skb_put(skb, sizeof(*led));
  1339. led->mode = cpu_to_le16(mode);
  1340. led->led_permanent = cpu_to_le16(link);
  1341. led->led_temporary = cpu_to_le16(act);
  1342. led->duration = cpu_to_le16(1000);
  1343. priv->tx(dev, skb, 1);
  1344. return 0;
  1345. }
  1346. #define P54_SET_QUEUE(queue, ai_fs, cw_min, cw_max, _txop) \
  1347. do { \
  1348. queue.aifs = cpu_to_le16(ai_fs); \
  1349. queue.cwmin = cpu_to_le16(cw_min); \
  1350. queue.cwmax = cpu_to_le16(cw_max); \
  1351. queue.txop = cpu_to_le16(_txop); \
  1352. } while(0)
  1353. static int p54_set_edcf(struct ieee80211_hw *dev)
  1354. {
  1355. struct p54_common *priv = dev->priv;
  1356. struct sk_buff *skb;
  1357. struct p54_edcf *edcf;
  1358. skb = p54_alloc_skb(dev, P54_HDR_FLAG_CONTROL_OPSET, sizeof(*edcf) +
  1359. sizeof(struct p54_hdr), P54_CONTROL_TYPE_DCFINIT,
  1360. GFP_ATOMIC);
  1361. if (!skb)
  1362. return -ENOMEM;
  1363. edcf = (struct p54_edcf *)skb_put(skb, sizeof(*edcf));
  1364. if (priv->use_short_slot) {
  1365. edcf->slottime = 9;
  1366. edcf->sifs = 0x10;
  1367. edcf->eofpad = 0x00;
  1368. } else {
  1369. edcf->slottime = 20;
  1370. edcf->sifs = 0x0a;
  1371. edcf->eofpad = 0x06;
  1372. }
  1373. /* (see prism54/isl_oid.h for further details) */
  1374. edcf->frameburst = cpu_to_le16(0);
  1375. edcf->round_trip_delay = cpu_to_le16(0);
  1376. edcf->flags = 0;
  1377. memset(edcf->mapping, 0, sizeof(edcf->mapping));
  1378. memcpy(edcf->queue, priv->qos_params, sizeof(edcf->queue));
  1379. priv->tx(dev, skb, 1);
  1380. return 0;
  1381. }
  1382. static int p54_init_stats(struct ieee80211_hw *dev)
  1383. {
  1384. struct p54_common *priv = dev->priv;
  1385. priv->cached_stats = p54_alloc_skb(dev, P54_HDR_FLAG_CONTROL,
  1386. sizeof(struct p54_hdr) + sizeof(struct p54_statistics),
  1387. P54_CONTROL_TYPE_STAT_READBACK, GFP_KERNEL);
  1388. if (!priv->cached_stats)
  1389. return -ENOMEM;
  1390. mod_timer(&priv->stats_timer, jiffies + HZ);
  1391. return 0;
  1392. }
  1393. static int p54_beacon_tim(struct sk_buff *skb)
  1394. {
  1395. /*
  1396. * the good excuse for this mess is ... the firmware.
  1397. * The dummy TIM MUST be at the end of the beacon frame,
  1398. * because it'll be overwritten!
  1399. */
  1400. struct ieee80211_mgmt *mgmt = (void *)skb->data;
  1401. u8 *pos, *end;
  1402. if (skb->len <= sizeof(mgmt))
  1403. return -EINVAL;
  1404. pos = (u8 *)mgmt->u.beacon.variable;
  1405. end = skb->data + skb->len;
  1406. while (pos < end) {
  1407. if (pos + 2 + pos[1] > end)
  1408. return -EINVAL;
  1409. if (pos[0] == WLAN_EID_TIM) {
  1410. u8 dtim_len = pos[1];
  1411. u8 dtim_period = pos[3];
  1412. u8 *next = pos + 2 + dtim_len;
  1413. if (dtim_len < 3)
  1414. return -EINVAL;
  1415. memmove(pos, next, end - next);
  1416. if (dtim_len > 3)
  1417. skb_trim(skb, skb->len - (dtim_len - 3));
  1418. pos = end - (dtim_len + 2);
  1419. /* add the dummy at the end */
  1420. pos[0] = WLAN_EID_TIM;
  1421. pos[1] = 3;
  1422. pos[2] = 0;
  1423. pos[3] = dtim_period;
  1424. pos[4] = 0;
  1425. return 0;
  1426. }
  1427. pos += 2 + pos[1];
  1428. }
  1429. return 0;
  1430. }
  1431. static int p54_beacon_update(struct ieee80211_hw *dev,
  1432. struct ieee80211_vif *vif)
  1433. {
  1434. struct p54_common *priv = dev->priv;
  1435. struct sk_buff *beacon;
  1436. int ret;
  1437. if (priv->cached_beacon) {
  1438. p54_tx_cancel(dev, priv->cached_beacon);
  1439. /* wait for the last beacon the be freed */
  1440. msleep(10);
  1441. }
  1442. beacon = ieee80211_beacon_get(dev, vif);
  1443. if (!beacon)
  1444. return -ENOMEM;
  1445. ret = p54_beacon_tim(beacon);
  1446. if (ret)
  1447. return ret;
  1448. ret = p54_tx(dev, beacon);
  1449. if (ret)
  1450. return ret;
  1451. priv->cached_beacon = beacon;
  1452. priv->tsf_high32 = 0;
  1453. priv->tsf_low32 = 0;
  1454. return 0;
  1455. }
  1456. static int p54_start(struct ieee80211_hw *dev)
  1457. {
  1458. struct p54_common *priv = dev->priv;
  1459. int err;
  1460. mutex_lock(&priv->conf_mutex);
  1461. err = priv->open(dev);
  1462. if (err)
  1463. goto out;
  1464. P54_SET_QUEUE(priv->qos_params[0], 0x0002, 0x0003, 0x0007, 47);
  1465. P54_SET_QUEUE(priv->qos_params[1], 0x0002, 0x0007, 0x000f, 94);
  1466. P54_SET_QUEUE(priv->qos_params[2], 0x0003, 0x000f, 0x03ff, 0);
  1467. P54_SET_QUEUE(priv->qos_params[3], 0x0007, 0x000f, 0x03ff, 0);
  1468. err = p54_set_edcf(dev);
  1469. if (err)
  1470. goto out;
  1471. err = p54_init_stats(dev);
  1472. if (err)
  1473. goto out;
  1474. memset(priv->bssid, ~0, ETH_ALEN);
  1475. priv->mode = NL80211_IFTYPE_MONITOR;
  1476. err = p54_setup_mac(dev);
  1477. if (err) {
  1478. priv->mode = NL80211_IFTYPE_UNSPECIFIED;
  1479. goto out;
  1480. }
  1481. out:
  1482. mutex_unlock(&priv->conf_mutex);
  1483. return err;
  1484. }
  1485. static void p54_stop(struct ieee80211_hw *dev)
  1486. {
  1487. struct p54_common *priv = dev->priv;
  1488. struct sk_buff *skb;
  1489. mutex_lock(&priv->conf_mutex);
  1490. del_timer(&priv->stats_timer);
  1491. p54_free_skb(dev, priv->cached_stats);
  1492. priv->cached_stats = NULL;
  1493. if (priv->cached_beacon)
  1494. p54_tx_cancel(dev, priv->cached_beacon);
  1495. while ((skb = skb_dequeue(&priv->tx_queue)))
  1496. kfree_skb(skb);
  1497. priv->cached_beacon = NULL;
  1498. priv->stop(dev);
  1499. priv->tsf_high32 = priv->tsf_low32 = 0;
  1500. priv->mode = NL80211_IFTYPE_UNSPECIFIED;
  1501. mutex_unlock(&priv->conf_mutex);
  1502. }
  1503. static int p54_add_interface(struct ieee80211_hw *dev,
  1504. struct ieee80211_if_init_conf *conf)
  1505. {
  1506. struct p54_common *priv = dev->priv;
  1507. mutex_lock(&priv->conf_mutex);
  1508. if (priv->mode != NL80211_IFTYPE_MONITOR) {
  1509. mutex_unlock(&priv->conf_mutex);
  1510. return -EOPNOTSUPP;
  1511. }
  1512. switch (conf->type) {
  1513. case NL80211_IFTYPE_STATION:
  1514. case NL80211_IFTYPE_ADHOC:
  1515. case NL80211_IFTYPE_AP:
  1516. case NL80211_IFTYPE_MESH_POINT:
  1517. priv->mode = conf->type;
  1518. break;
  1519. default:
  1520. mutex_unlock(&priv->conf_mutex);
  1521. return -EOPNOTSUPP;
  1522. }
  1523. memcpy(priv->mac_addr, conf->mac_addr, ETH_ALEN);
  1524. p54_setup_mac(dev);
  1525. p54_set_leds(dev, 1, 0, 0);
  1526. mutex_unlock(&priv->conf_mutex);
  1527. return 0;
  1528. }
  1529. static void p54_remove_interface(struct ieee80211_hw *dev,
  1530. struct ieee80211_if_init_conf *conf)
  1531. {
  1532. struct p54_common *priv = dev->priv;
  1533. mutex_lock(&priv->conf_mutex);
  1534. if (priv->cached_beacon)
  1535. p54_tx_cancel(dev, priv->cached_beacon);
  1536. priv->mode = NL80211_IFTYPE_MONITOR;
  1537. memset(priv->mac_addr, 0, ETH_ALEN);
  1538. memset(priv->bssid, 0, ETH_ALEN);
  1539. p54_setup_mac(dev);
  1540. mutex_unlock(&priv->conf_mutex);
  1541. }
  1542. static int p54_config(struct ieee80211_hw *dev, u32 changed)
  1543. {
  1544. int ret;
  1545. struct p54_common *priv = dev->priv;
  1546. struct ieee80211_conf *conf = &dev->conf;
  1547. mutex_lock(&priv->conf_mutex);
  1548. if (changed & IEEE80211_CONF_CHANGE_POWER)
  1549. priv->output_power = conf->power_level << 2;
  1550. if (changed & IEEE80211_CONF_CHANGE_RADIO_ENABLED) {
  1551. ret = p54_setup_mac(dev);
  1552. if (ret)
  1553. goto out;
  1554. }
  1555. if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
  1556. ret = p54_scan(dev, P54_SCAN_EXIT, 0,
  1557. conf->channel->center_freq);
  1558. if (ret)
  1559. goto out;
  1560. }
  1561. out:
  1562. mutex_unlock(&priv->conf_mutex);
  1563. return ret;
  1564. }
  1565. static int p54_config_interface(struct ieee80211_hw *dev,
  1566. struct ieee80211_vif *vif,
  1567. struct ieee80211_if_conf *conf)
  1568. {
  1569. struct p54_common *priv = dev->priv;
  1570. int ret = 0;
  1571. mutex_lock(&priv->conf_mutex);
  1572. if (conf->changed & IEEE80211_IFCC_BSSID) {
  1573. memcpy(priv->bssid, conf->bssid, ETH_ALEN);
  1574. ret = p54_setup_mac(dev);
  1575. if (ret)
  1576. goto out;
  1577. }
  1578. if (conf->changed & IEEE80211_IFCC_BEACON) {
  1579. ret = p54_scan(dev, P54_SCAN_EXIT, 0,
  1580. dev->conf.channel->center_freq);
  1581. if (ret)
  1582. goto out;
  1583. ret = p54_setup_mac(dev);
  1584. if (ret)
  1585. goto out;
  1586. ret = p54_beacon_update(dev, vif);
  1587. if (ret)
  1588. goto out;
  1589. ret = p54_set_edcf(dev);
  1590. if (ret)
  1591. goto out;
  1592. }
  1593. ret = p54_set_leds(dev, 1, !is_multicast_ether_addr(priv->bssid), 0);
  1594. out:
  1595. mutex_unlock(&priv->conf_mutex);
  1596. return ret;
  1597. }
  1598. static void p54_configure_filter(struct ieee80211_hw *dev,
  1599. unsigned int changed_flags,
  1600. unsigned int *total_flags,
  1601. int mc_count, struct dev_mc_list *mclist)
  1602. {
  1603. struct p54_common *priv = dev->priv;
  1604. *total_flags &= FIF_PROMISC_IN_BSS |
  1605. (*total_flags & FIF_PROMISC_IN_BSS) ?
  1606. FIF_FCSFAIL : 0;
  1607. priv->filter_flags = *total_flags;
  1608. if (changed_flags & FIF_PROMISC_IN_BSS)
  1609. p54_setup_mac(dev);
  1610. }
  1611. static int p54_conf_tx(struct ieee80211_hw *dev, u16 queue,
  1612. const struct ieee80211_tx_queue_params *params)
  1613. {
  1614. struct p54_common *priv = dev->priv;
  1615. int ret;
  1616. mutex_lock(&priv->conf_mutex);
  1617. if ((params) && !(queue > 4)) {
  1618. P54_SET_QUEUE(priv->qos_params[queue], params->aifs,
  1619. params->cw_min, params->cw_max, params->txop);
  1620. ret = p54_set_edcf(dev);
  1621. } else
  1622. ret = -EINVAL;
  1623. mutex_unlock(&priv->conf_mutex);
  1624. return ret;
  1625. }
  1626. static int p54_init_xbow_synth(struct ieee80211_hw *dev)
  1627. {
  1628. struct p54_common *priv = dev->priv;
  1629. struct sk_buff *skb;
  1630. struct p54_xbow_synth *xbow;
  1631. skb = p54_alloc_skb(dev, P54_HDR_FLAG_CONTROL_OPSET, sizeof(*xbow) +
  1632. sizeof(struct p54_hdr),
  1633. P54_CONTROL_TYPE_XBOW_SYNTH_CFG,
  1634. GFP_KERNEL);
  1635. if (!skb)
  1636. return -ENOMEM;
  1637. xbow = (struct p54_xbow_synth *)skb_put(skb, sizeof(*xbow));
  1638. xbow->magic1 = cpu_to_le16(0x1);
  1639. xbow->magic2 = cpu_to_le16(0x2);
  1640. xbow->freq = cpu_to_le16(5390);
  1641. memset(xbow->padding, 0, sizeof(xbow->padding));
  1642. priv->tx(dev, skb, 1);
  1643. return 0;
  1644. }
  1645. static void p54_statistics_timer(unsigned long data)
  1646. {
  1647. struct ieee80211_hw *dev = (struct ieee80211_hw *) data;
  1648. struct p54_common *priv = dev->priv;
  1649. BUG_ON(!priv->cached_stats);
  1650. priv->tx(dev, priv->cached_stats, 0);
  1651. }
  1652. static int p54_get_stats(struct ieee80211_hw *dev,
  1653. struct ieee80211_low_level_stats *stats)
  1654. {
  1655. struct p54_common *priv = dev->priv;
  1656. del_timer(&priv->stats_timer);
  1657. p54_statistics_timer((unsigned long)dev);
  1658. if (!wait_for_completion_interruptible_timeout(&priv->stats_comp, HZ)) {
  1659. printk(KERN_ERR "%s: device does not respond!\n",
  1660. wiphy_name(dev->wiphy));
  1661. return -EBUSY;
  1662. }
  1663. memcpy(stats, &priv->stats, sizeof(*stats));
  1664. return 0;
  1665. }
  1666. static int p54_get_tx_stats(struct ieee80211_hw *dev,
  1667. struct ieee80211_tx_queue_stats *stats)
  1668. {
  1669. struct p54_common *priv = dev->priv;
  1670. memcpy(stats, &priv->tx_stats[4], sizeof(stats[0]) * dev->queues);
  1671. return 0;
  1672. }
  1673. static void p54_bss_info_changed(struct ieee80211_hw *dev,
  1674. struct ieee80211_vif *vif,
  1675. struct ieee80211_bss_conf *info,
  1676. u32 changed)
  1677. {
  1678. struct p54_common *priv = dev->priv;
  1679. if (changed & BSS_CHANGED_ERP_SLOT) {
  1680. priv->use_short_slot = info->use_short_slot;
  1681. p54_set_edcf(dev);
  1682. }
  1683. if (changed & BSS_CHANGED_BASIC_RATES) {
  1684. if (dev->conf.channel->band == IEEE80211_BAND_5GHZ)
  1685. priv->basic_rate_mask = (info->basic_rates << 4);
  1686. else
  1687. priv->basic_rate_mask = info->basic_rates;
  1688. p54_setup_mac(dev);
  1689. if (priv->fw_var >= 0x500)
  1690. p54_scan(dev, P54_SCAN_EXIT, 0,
  1691. dev->conf.channel->center_freq);
  1692. }
  1693. if (changed & BSS_CHANGED_ASSOC) {
  1694. if (info->assoc) {
  1695. priv->aid = info->aid;
  1696. priv->wakeup_timer = info->beacon_int *
  1697. info->dtim_period * 5;
  1698. p54_setup_mac(dev);
  1699. }
  1700. }
  1701. }
  1702. static int p54_set_key(struct ieee80211_hw *dev, enum set_key_cmd cmd,
  1703. const u8 *local_address, const u8 *address,
  1704. struct ieee80211_key_conf *key)
  1705. {
  1706. struct p54_common *priv = dev->priv;
  1707. struct sk_buff *skb;
  1708. struct p54_keycache *rxkey;
  1709. u8 algo = 0;
  1710. if (modparam_nohwcrypt)
  1711. return -EOPNOTSUPP;
  1712. if (cmd == DISABLE_KEY)
  1713. algo = 0;
  1714. else {
  1715. switch (key->alg) {
  1716. case ALG_TKIP:
  1717. if (!(priv->privacy_caps & (BR_DESC_PRIV_CAP_MICHAEL |
  1718. BR_DESC_PRIV_CAP_TKIP)))
  1719. return -EOPNOTSUPP;
  1720. key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  1721. algo = P54_CRYPTO_TKIPMICHAEL;
  1722. break;
  1723. case ALG_WEP:
  1724. if (!(priv->privacy_caps & BR_DESC_PRIV_CAP_WEP))
  1725. return -EOPNOTSUPP;
  1726. key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  1727. algo = P54_CRYPTO_WEP;
  1728. break;
  1729. case ALG_CCMP:
  1730. if (!(priv->privacy_caps & BR_DESC_PRIV_CAP_AESCCMP))
  1731. return -EOPNOTSUPP;
  1732. key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  1733. algo = P54_CRYPTO_AESCCMP;
  1734. break;
  1735. default:
  1736. return -EINVAL;
  1737. }
  1738. }
  1739. if (key->keyidx > priv->rx_keycache_size) {
  1740. /*
  1741. * The device supports the choosen algorithm, but the firmware
  1742. * does not provide enough key slots to store all of them.
  1743. * So, incoming frames have to be decoded by the mac80211 stack,
  1744. * but we can still offload encryption for outgoing frames.
  1745. */
  1746. return 0;
  1747. }
  1748. mutex_lock(&priv->conf_mutex);
  1749. skb = p54_alloc_skb(dev, P54_HDR_FLAG_CONTROL_OPSET, sizeof(*rxkey) +
  1750. sizeof(struct p54_hdr), P54_CONTROL_TYPE_RX_KEYCACHE,
  1751. GFP_ATOMIC);
  1752. if (!skb) {
  1753. mutex_unlock(&priv->conf_mutex);
  1754. return -ENOMEM;
  1755. }
  1756. /* TODO: some devices have 4 more free slots for rx keys */
  1757. rxkey = (struct p54_keycache *)skb_put(skb, sizeof(*rxkey));
  1758. rxkey->entry = key->keyidx;
  1759. rxkey->key_id = key->keyidx;
  1760. rxkey->key_type = algo;
  1761. if (address)
  1762. memcpy(rxkey->mac, address, ETH_ALEN);
  1763. else
  1764. memset(rxkey->mac, ~0, ETH_ALEN);
  1765. if (key->alg != ALG_TKIP) {
  1766. rxkey->key_len = min((u8)16, key->keylen);
  1767. memcpy(rxkey->key, key->key, rxkey->key_len);
  1768. } else {
  1769. rxkey->key_len = 24;
  1770. memcpy(rxkey->key, key->key, 16);
  1771. memcpy(&(rxkey->key[16]), &(key->key
  1772. [NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY]), 8);
  1773. }
  1774. priv->tx(dev, skb, 1);
  1775. mutex_unlock(&priv->conf_mutex);
  1776. return 0;
  1777. }
  1778. static const struct ieee80211_ops p54_ops = {
  1779. .tx = p54_tx,
  1780. .start = p54_start,
  1781. .stop = p54_stop,
  1782. .add_interface = p54_add_interface,
  1783. .remove_interface = p54_remove_interface,
  1784. .set_tim = p54_set_tim,
  1785. .sta_notify = p54_sta_notify,
  1786. .set_key = p54_set_key,
  1787. .config = p54_config,
  1788. .config_interface = p54_config_interface,
  1789. .bss_info_changed = p54_bss_info_changed,
  1790. .configure_filter = p54_configure_filter,
  1791. .conf_tx = p54_conf_tx,
  1792. .get_stats = p54_get_stats,
  1793. .get_tx_stats = p54_get_tx_stats
  1794. };
  1795. struct ieee80211_hw *p54_init_common(size_t priv_data_len)
  1796. {
  1797. struct ieee80211_hw *dev;
  1798. struct p54_common *priv;
  1799. dev = ieee80211_alloc_hw(priv_data_len, &p54_ops);
  1800. if (!dev)
  1801. return NULL;
  1802. priv = dev->priv;
  1803. priv->mode = NL80211_IFTYPE_UNSPECIFIED;
  1804. priv->basic_rate_mask = 0x15f;
  1805. skb_queue_head_init(&priv->tx_queue);
  1806. dev->flags = IEEE80211_HW_RX_INCLUDES_FCS |
  1807. IEEE80211_HW_SIGNAL_DBM |
  1808. IEEE80211_HW_NOISE_DBM;
  1809. dev->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
  1810. BIT(NL80211_IFTYPE_ADHOC) |
  1811. BIT(NL80211_IFTYPE_AP) |
  1812. BIT(NL80211_IFTYPE_MESH_POINT);
  1813. dev->channel_change_time = 1000; /* TODO: find actual value */
  1814. priv->tx_stats[0].limit = 1; /* Beacon queue */
  1815. priv->tx_stats[1].limit = 1; /* Probe queue for HW scan */
  1816. priv->tx_stats[2].limit = 3; /* queue for MLMEs */
  1817. priv->tx_stats[3].limit = 3; /* Broadcast / MC queue */
  1818. priv->tx_stats[4].limit = 5; /* Data */
  1819. dev->queues = 1;
  1820. priv->noise = -94;
  1821. /*
  1822. * We support at most 8 tries no matter which rate they're at,
  1823. * we cannot support max_rates * max_rate_tries as we set it
  1824. * here, but setting it correctly to 4/2 or so would limit us
  1825. * artificially if the RC algorithm wants just two rates, so
  1826. * let's say 4/7, we'll redistribute it at TX time, see the
  1827. * comments there.
  1828. */
  1829. dev->max_rates = 4;
  1830. dev->max_rate_tries = 7;
  1831. dev->extra_tx_headroom = sizeof(struct p54_hdr) + 4 +
  1832. sizeof(struct p54_tx_data);
  1833. mutex_init(&priv->conf_mutex);
  1834. init_completion(&priv->eeprom_comp);
  1835. init_completion(&priv->stats_comp);
  1836. setup_timer(&priv->stats_timer, p54_statistics_timer,
  1837. (unsigned long)dev);
  1838. return dev;
  1839. }
  1840. EXPORT_SYMBOL_GPL(p54_init_common);
  1841. void p54_free_common(struct ieee80211_hw *dev)
  1842. {
  1843. struct p54_common *priv = dev->priv;
  1844. del_timer(&priv->stats_timer);
  1845. kfree_skb(priv->cached_stats);
  1846. kfree(priv->iq_autocal);
  1847. kfree(priv->output_limit);
  1848. kfree(priv->curve_data);
  1849. }
  1850. EXPORT_SYMBOL_GPL(p54_free_common);
  1851. static int __init p54_init(void)
  1852. {
  1853. return 0;
  1854. }
  1855. static void __exit p54_exit(void)
  1856. {
  1857. }
  1858. module_init(p54_init);
  1859. module_exit(p54_exit);