p54common.c 71 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687
  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. #ifdef CONFIG_P54_LEDS
  23. #include <linux/leds.h>
  24. #endif /* CONFIG_P54_LEDS */
  25. #include "p54.h"
  26. #include "p54common.h"
  27. static int modparam_nohwcrypt;
  28. module_param_named(nohwcrypt, modparam_nohwcrypt, bool, S_IRUGO);
  29. MODULE_PARM_DESC(nohwcrypt, "Disable hardware encryption.");
  30. MODULE_AUTHOR("Michael Wu <flamingice@sourmilk.net>");
  31. MODULE_DESCRIPTION("Softmac Prism54 common code");
  32. MODULE_LICENSE("GPL");
  33. MODULE_ALIAS("prism54common");
  34. static struct ieee80211_rate p54_bgrates[] = {
  35. { .bitrate = 10, .hw_value = 0, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  36. { .bitrate = 20, .hw_value = 1, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  37. { .bitrate = 55, .hw_value = 2, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  38. { .bitrate = 110, .hw_value = 3, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  39. { .bitrate = 60, .hw_value = 4, },
  40. { .bitrate = 90, .hw_value = 5, },
  41. { .bitrate = 120, .hw_value = 6, },
  42. { .bitrate = 180, .hw_value = 7, },
  43. { .bitrate = 240, .hw_value = 8, },
  44. { .bitrate = 360, .hw_value = 9, },
  45. { .bitrate = 480, .hw_value = 10, },
  46. { .bitrate = 540, .hw_value = 11, },
  47. };
  48. static struct ieee80211_channel p54_bgchannels[] = {
  49. { .center_freq = 2412, .hw_value = 1, },
  50. { .center_freq = 2417, .hw_value = 2, },
  51. { .center_freq = 2422, .hw_value = 3, },
  52. { .center_freq = 2427, .hw_value = 4, },
  53. { .center_freq = 2432, .hw_value = 5, },
  54. { .center_freq = 2437, .hw_value = 6, },
  55. { .center_freq = 2442, .hw_value = 7, },
  56. { .center_freq = 2447, .hw_value = 8, },
  57. { .center_freq = 2452, .hw_value = 9, },
  58. { .center_freq = 2457, .hw_value = 10, },
  59. { .center_freq = 2462, .hw_value = 11, },
  60. { .center_freq = 2467, .hw_value = 12, },
  61. { .center_freq = 2472, .hw_value = 13, },
  62. { .center_freq = 2484, .hw_value = 14, },
  63. };
  64. static struct ieee80211_supported_band band_2GHz = {
  65. .channels = p54_bgchannels,
  66. .n_channels = ARRAY_SIZE(p54_bgchannels),
  67. .bitrates = p54_bgrates,
  68. .n_bitrates = ARRAY_SIZE(p54_bgrates),
  69. };
  70. static struct ieee80211_rate p54_arates[] = {
  71. { .bitrate = 60, .hw_value = 4, },
  72. { .bitrate = 90, .hw_value = 5, },
  73. { .bitrate = 120, .hw_value = 6, },
  74. { .bitrate = 180, .hw_value = 7, },
  75. { .bitrate = 240, .hw_value = 8, },
  76. { .bitrate = 360, .hw_value = 9, },
  77. { .bitrate = 480, .hw_value = 10, },
  78. { .bitrate = 540, .hw_value = 11, },
  79. };
  80. static struct ieee80211_channel p54_achannels[] = {
  81. { .center_freq = 4920 },
  82. { .center_freq = 4940 },
  83. { .center_freq = 4960 },
  84. { .center_freq = 4980 },
  85. { .center_freq = 5040 },
  86. { .center_freq = 5060 },
  87. { .center_freq = 5080 },
  88. { .center_freq = 5170 },
  89. { .center_freq = 5180 },
  90. { .center_freq = 5190 },
  91. { .center_freq = 5200 },
  92. { .center_freq = 5210 },
  93. { .center_freq = 5220 },
  94. { .center_freq = 5230 },
  95. { .center_freq = 5240 },
  96. { .center_freq = 5260 },
  97. { .center_freq = 5280 },
  98. { .center_freq = 5300 },
  99. { .center_freq = 5320 },
  100. { .center_freq = 5500 },
  101. { .center_freq = 5520 },
  102. { .center_freq = 5540 },
  103. { .center_freq = 5560 },
  104. { .center_freq = 5580 },
  105. { .center_freq = 5600 },
  106. { .center_freq = 5620 },
  107. { .center_freq = 5640 },
  108. { .center_freq = 5660 },
  109. { .center_freq = 5680 },
  110. { .center_freq = 5700 },
  111. { .center_freq = 5745 },
  112. { .center_freq = 5765 },
  113. { .center_freq = 5785 },
  114. { .center_freq = 5805 },
  115. { .center_freq = 5825 },
  116. };
  117. static struct ieee80211_supported_band band_5GHz = {
  118. .channels = p54_achannels,
  119. .n_channels = ARRAY_SIZE(p54_achannels),
  120. .bitrates = p54_arates,
  121. .n_bitrates = ARRAY_SIZE(p54_arates),
  122. };
  123. int p54_parse_firmware(struct ieee80211_hw *dev, const struct firmware *fw)
  124. {
  125. struct p54_common *priv = dev->priv;
  126. struct bootrec_exp_if *exp_if;
  127. struct bootrec *bootrec;
  128. u32 *data = (u32 *)fw->data;
  129. u32 *end_data = (u32 *)fw->data + (fw->size >> 2);
  130. u8 *fw_version = NULL;
  131. size_t len;
  132. int i;
  133. int maxlen;
  134. if (priv->rx_start)
  135. return 0;
  136. while (data < end_data && *data)
  137. data++;
  138. while (data < end_data && !*data)
  139. data++;
  140. bootrec = (struct bootrec *) data;
  141. while (bootrec->data <= end_data &&
  142. (bootrec->data + (len = le32_to_cpu(bootrec->len))) <= end_data) {
  143. u32 code = le32_to_cpu(bootrec->code);
  144. switch (code) {
  145. case BR_CODE_COMPONENT_ID:
  146. priv->fw_interface = be32_to_cpup((__be32 *)
  147. bootrec->data);
  148. switch (priv->fw_interface) {
  149. case FW_LM86:
  150. case FW_LM20:
  151. case FW_LM87: {
  152. char *iftype = (char *)bootrec->data;
  153. printk(KERN_INFO "%s: p54 detected a LM%c%c "
  154. "firmware\n",
  155. wiphy_name(dev->wiphy),
  156. iftype[2], iftype[3]);
  157. break;
  158. }
  159. case FW_FMAC:
  160. default:
  161. printk(KERN_ERR "%s: unsupported firmware\n",
  162. wiphy_name(dev->wiphy));
  163. return -ENODEV;
  164. }
  165. break;
  166. case BR_CODE_COMPONENT_VERSION:
  167. /* 24 bytes should be enough for all firmwares */
  168. if (strnlen((unsigned char*)bootrec->data, 24) < 24)
  169. fw_version = (unsigned char*)bootrec->data;
  170. break;
  171. case BR_CODE_DESCR: {
  172. struct bootrec_desc *desc =
  173. (struct bootrec_desc *)bootrec->data;
  174. priv->rx_start = le32_to_cpu(desc->rx_start);
  175. /* FIXME add sanity checking */
  176. priv->rx_end = le32_to_cpu(desc->rx_end) - 0x3500;
  177. priv->headroom = desc->headroom;
  178. priv->tailroom = desc->tailroom;
  179. priv->privacy_caps = desc->privacy_caps;
  180. priv->rx_keycache_size = desc->rx_keycache_size;
  181. if (le32_to_cpu(bootrec->len) == 11)
  182. priv->rx_mtu = le16_to_cpu(desc->rx_mtu);
  183. else
  184. priv->rx_mtu = (size_t)
  185. 0x620 - priv->tx_hdr_len;
  186. maxlen = priv->tx_hdr_len + /* USB devices */
  187. sizeof(struct p54_rx_data) +
  188. 4 + /* rx alignment */
  189. IEEE80211_MAX_FRAG_THRESHOLD;
  190. if (priv->rx_mtu > maxlen && PAGE_SIZE == 4096) {
  191. printk(KERN_INFO "p54: rx_mtu reduced from %d "
  192. "to %d\n", priv->rx_mtu,
  193. maxlen);
  194. priv->rx_mtu = maxlen;
  195. }
  196. break;
  197. }
  198. case BR_CODE_EXPOSED_IF:
  199. exp_if = (struct bootrec_exp_if *) bootrec->data;
  200. for (i = 0; i < (len * sizeof(*exp_if) / 4); i++)
  201. if (exp_if[i].if_id == cpu_to_le16(0x1a))
  202. priv->fw_var = le16_to_cpu(exp_if[i].variant);
  203. break;
  204. case BR_CODE_DEPENDENT_IF:
  205. break;
  206. case BR_CODE_END_OF_BRA:
  207. case LEGACY_BR_CODE_END_OF_BRA:
  208. end_data = NULL;
  209. break;
  210. default:
  211. break;
  212. }
  213. bootrec = (struct bootrec *)&bootrec->data[len];
  214. }
  215. if (fw_version)
  216. printk(KERN_INFO "%s: FW rev %s - Softmac protocol %x.%x\n",
  217. wiphy_name(dev->wiphy), fw_version,
  218. priv->fw_var >> 8, priv->fw_var & 0xff);
  219. if (priv->fw_var < 0x500)
  220. printk(KERN_INFO "%s: you are using an obsolete firmware. "
  221. "visit http://wireless.kernel.org/en/users/Drivers/p54 "
  222. "and grab one for \"kernel >= 2.6.28\"!\n",
  223. wiphy_name(dev->wiphy));
  224. if (priv->fw_var >= 0x300) {
  225. /* Firmware supports QoS, use it! */
  226. priv->tx_stats[P54_QUEUE_AC_VO].limit = 3;
  227. priv->tx_stats[P54_QUEUE_AC_VI].limit = 4;
  228. priv->tx_stats[P54_QUEUE_AC_BE].limit = 3;
  229. priv->tx_stats[P54_QUEUE_AC_BK].limit = 2;
  230. dev->queues = P54_QUEUE_AC_NUM;
  231. }
  232. if (!modparam_nohwcrypt) {
  233. printk(KERN_INFO "%s: cryptographic accelerator "
  234. "WEP:%s, TKIP:%s, CCMP:%s\n",
  235. wiphy_name(dev->wiphy),
  236. (priv->privacy_caps & BR_DESC_PRIV_CAP_WEP) ? "YES" :
  237. "no", (priv->privacy_caps & (BR_DESC_PRIV_CAP_TKIP |
  238. BR_DESC_PRIV_CAP_MICHAEL)) ? "YES" : "no",
  239. (priv->privacy_caps & BR_DESC_PRIV_CAP_AESCCMP) ?
  240. "YES" : "no");
  241. if (priv->rx_keycache_size) {
  242. /*
  243. * NOTE:
  244. *
  245. * The firmware provides at most 255 (0 - 254) slots
  246. * for keys which are then used to offload decryption.
  247. * As a result the 255 entry (aka 0xff) can be used
  248. * safely by the driver to mark keys that didn't fit
  249. * into the full cache. This trick saves us from
  250. * keeping a extra list for uploaded keys.
  251. */
  252. priv->used_rxkeys = kzalloc(BITS_TO_LONGS(
  253. priv->rx_keycache_size), GFP_KERNEL);
  254. if (!priv->used_rxkeys)
  255. return -ENOMEM;
  256. }
  257. }
  258. return 0;
  259. }
  260. EXPORT_SYMBOL_GPL(p54_parse_firmware);
  261. static int p54_convert_rev0(struct ieee80211_hw *dev,
  262. struct pda_pa_curve_data *curve_data)
  263. {
  264. struct p54_common *priv = dev->priv;
  265. struct p54_pa_curve_data_sample *dst;
  266. struct pda_pa_curve_data_sample_rev0 *src;
  267. size_t cd_len = sizeof(*curve_data) +
  268. (curve_data->points_per_channel*sizeof(*dst) + 2) *
  269. curve_data->channels;
  270. unsigned int i, j;
  271. void *source, *target;
  272. priv->curve_data = kmalloc(sizeof(*priv->curve_data) + cd_len,
  273. GFP_KERNEL);
  274. if (!priv->curve_data)
  275. return -ENOMEM;
  276. priv->curve_data->entries = curve_data->channels;
  277. priv->curve_data->entry_size = sizeof(__le16) +
  278. sizeof(*dst) * curve_data->points_per_channel;
  279. priv->curve_data->offset = offsetof(struct pda_pa_curve_data, data);
  280. priv->curve_data->len = cd_len;
  281. memcpy(priv->curve_data->data, curve_data, sizeof(*curve_data));
  282. source = curve_data->data;
  283. target = ((struct pda_pa_curve_data *) priv->curve_data->data)->data;
  284. for (i = 0; i < curve_data->channels; i++) {
  285. __le16 *freq = source;
  286. source += sizeof(__le16);
  287. *((__le16 *)target) = *freq;
  288. target += sizeof(__le16);
  289. for (j = 0; j < curve_data->points_per_channel; j++) {
  290. dst = target;
  291. src = source;
  292. dst->rf_power = src->rf_power;
  293. dst->pa_detector = src->pa_detector;
  294. dst->data_64qam = src->pcv;
  295. /* "invent" the points for the other modulations */
  296. #define SUB(x,y) (u8)((x) - (y)) > (x) ? 0 : (x) - (y)
  297. dst->data_16qam = SUB(src->pcv, 12);
  298. dst->data_qpsk = SUB(dst->data_16qam, 12);
  299. dst->data_bpsk = SUB(dst->data_qpsk, 12);
  300. dst->data_barker = SUB(dst->data_bpsk, 14);
  301. #undef SUB
  302. target += sizeof(*dst);
  303. source += sizeof(*src);
  304. }
  305. }
  306. return 0;
  307. }
  308. static int p54_convert_rev1(struct ieee80211_hw *dev,
  309. struct pda_pa_curve_data *curve_data)
  310. {
  311. struct p54_common *priv = dev->priv;
  312. struct p54_pa_curve_data_sample *dst;
  313. struct pda_pa_curve_data_sample_rev1 *src;
  314. size_t cd_len = sizeof(*curve_data) +
  315. (curve_data->points_per_channel*sizeof(*dst) + 2) *
  316. curve_data->channels;
  317. unsigned int i, j;
  318. void *source, *target;
  319. priv->curve_data = kzalloc(cd_len + sizeof(*priv->curve_data),
  320. GFP_KERNEL);
  321. if (!priv->curve_data)
  322. return -ENOMEM;
  323. priv->curve_data->entries = curve_data->channels;
  324. priv->curve_data->entry_size = sizeof(__le16) +
  325. sizeof(*dst) * curve_data->points_per_channel;
  326. priv->curve_data->offset = offsetof(struct pda_pa_curve_data, data);
  327. priv->curve_data->len = cd_len;
  328. memcpy(priv->curve_data->data, curve_data, sizeof(*curve_data));
  329. source = curve_data->data;
  330. target = ((struct pda_pa_curve_data *) priv->curve_data->data)->data;
  331. for (i = 0; i < curve_data->channels; i++) {
  332. __le16 *freq = source;
  333. source += sizeof(__le16);
  334. *((__le16 *)target) = *freq;
  335. target += sizeof(__le16);
  336. for (j = 0; j < curve_data->points_per_channel; j++) {
  337. memcpy(target, source, sizeof(*src));
  338. target += sizeof(*dst);
  339. source += sizeof(*src);
  340. }
  341. source++;
  342. }
  343. return 0;
  344. }
  345. static const char *p54_rf_chips[] = { "NULL", "Duette3", "Duette2",
  346. "Frisbee", "Xbow", "Longbow", "NULL", "NULL" };
  347. static int p54_init_xbow_synth(struct ieee80211_hw *dev);
  348. static void p54_parse_rssical(struct ieee80211_hw *dev, void *data, int len,
  349. u16 type)
  350. {
  351. struct p54_common *priv = dev->priv;
  352. int offset = (type == PDR_RSSI_LINEAR_APPROXIMATION_EXTENDED) ? 2 : 0;
  353. int entry_size = sizeof(struct pda_rssi_cal_entry) + offset;
  354. int num_entries = (type == PDR_RSSI_LINEAR_APPROXIMATION) ? 1 : 2;
  355. int i;
  356. if (len != (entry_size * num_entries)) {
  357. printk(KERN_ERR "%s: unknown rssi calibration data packing "
  358. " type:(%x) len:%d.\n",
  359. wiphy_name(dev->wiphy), type, len);
  360. print_hex_dump_bytes("rssical:", DUMP_PREFIX_NONE,
  361. data, len);
  362. printk(KERN_ERR "%s: please report this issue.\n",
  363. wiphy_name(dev->wiphy));
  364. return;
  365. }
  366. for (i = 0; i < num_entries; i++) {
  367. struct pda_rssi_cal_entry *cal = data +
  368. (offset + i * entry_size);
  369. priv->rssical_db[i].mul = (s16) le16_to_cpu(cal->mul);
  370. priv->rssical_db[i].add = (s16) le16_to_cpu(cal->add);
  371. }
  372. }
  373. static void p54_parse_default_country(struct ieee80211_hw *dev,
  374. void *data, int len)
  375. {
  376. struct pda_country *country;
  377. if (len != sizeof(*country)) {
  378. printk(KERN_ERR "%s: found possible invalid default country "
  379. "eeprom entry. (entry size: %d)\n",
  380. wiphy_name(dev->wiphy), len);
  381. print_hex_dump_bytes("country:", DUMP_PREFIX_NONE,
  382. data, len);
  383. printk(KERN_ERR "%s: please report this issue.\n",
  384. wiphy_name(dev->wiphy));
  385. return;
  386. }
  387. country = (struct pda_country *) data;
  388. if (country->flags == PDR_COUNTRY_CERT_CODE_PSEUDO)
  389. regulatory_hint(dev->wiphy, country->alpha2);
  390. else {
  391. /* TODO:
  392. * write a shared/common function that converts
  393. * "Regulatory domain codes" (802.11-2007 14.8.2.2)
  394. * into ISO/IEC 3166-1 alpha2 for regulatory_hint.
  395. */
  396. }
  397. }
  398. static int p54_convert_output_limits(struct ieee80211_hw *dev,
  399. u8 *data, size_t len)
  400. {
  401. struct p54_common *priv = dev->priv;
  402. if (len < 2)
  403. return -EINVAL;
  404. if (data[0] != 0) {
  405. printk(KERN_ERR "%s: unknown output power db revision:%x\n",
  406. wiphy_name(dev->wiphy), data[0]);
  407. return -EINVAL;
  408. }
  409. if (2 + data[1] * sizeof(struct pda_channel_output_limit) > len)
  410. return -EINVAL;
  411. priv->output_limit = kmalloc(data[1] *
  412. sizeof(struct pda_channel_output_limit) +
  413. sizeof(*priv->output_limit), GFP_KERNEL);
  414. if (!priv->output_limit)
  415. return -ENOMEM;
  416. priv->output_limit->offset = 0;
  417. priv->output_limit->entries = data[1];
  418. priv->output_limit->entry_size =
  419. sizeof(struct pda_channel_output_limit);
  420. priv->output_limit->len = priv->output_limit->entry_size *
  421. priv->output_limit->entries +
  422. priv->output_limit->offset;
  423. memcpy(priv->output_limit->data, &data[2],
  424. data[1] * sizeof(struct pda_channel_output_limit));
  425. return 0;
  426. }
  427. static struct p54_cal_database *p54_convert_db(struct pda_custom_wrapper *src,
  428. size_t total_len)
  429. {
  430. struct p54_cal_database *dst;
  431. size_t payload_len, entries, entry_size, offset;
  432. payload_len = le16_to_cpu(src->len);
  433. entries = le16_to_cpu(src->entries);
  434. entry_size = le16_to_cpu(src->entry_size);
  435. offset = le16_to_cpu(src->offset);
  436. if (((entries * entry_size + offset) != payload_len) ||
  437. (payload_len + sizeof(*src) != total_len))
  438. return NULL;
  439. dst = kmalloc(sizeof(*dst) + payload_len, GFP_KERNEL);
  440. if (!dst)
  441. return NULL;
  442. dst->entries = entries;
  443. dst->entry_size = entry_size;
  444. dst->offset = offset;
  445. dst->len = payload_len;
  446. memcpy(dst->data, src->data, payload_len);
  447. return dst;
  448. }
  449. int p54_parse_eeprom(struct ieee80211_hw *dev, void *eeprom, int len)
  450. {
  451. struct p54_common *priv = dev->priv;
  452. struct eeprom_pda_wrap *wrap = NULL;
  453. struct pda_entry *entry;
  454. unsigned int data_len, entry_len;
  455. void *tmp;
  456. int err;
  457. u8 *end = (u8 *)eeprom + len;
  458. u16 synth = 0;
  459. wrap = (struct eeprom_pda_wrap *) eeprom;
  460. entry = (void *)wrap->data + le16_to_cpu(wrap->len);
  461. /* verify that at least the entry length/code fits */
  462. while ((u8 *)entry <= end - sizeof(*entry)) {
  463. entry_len = le16_to_cpu(entry->len);
  464. data_len = ((entry_len - 1) << 1);
  465. /* abort if entry exceeds whole structure */
  466. if ((u8 *)entry + sizeof(*entry) + data_len > end)
  467. break;
  468. switch (le16_to_cpu(entry->code)) {
  469. case PDR_MAC_ADDRESS:
  470. if (data_len != ETH_ALEN)
  471. break;
  472. SET_IEEE80211_PERM_ADDR(dev, entry->data);
  473. break;
  474. case PDR_PRISM_PA_CAL_OUTPUT_POWER_LIMITS:
  475. if (priv->output_limit)
  476. break;
  477. err = p54_convert_output_limits(dev, entry->data,
  478. data_len);
  479. if (err)
  480. goto err;
  481. break;
  482. case PDR_PRISM_PA_CAL_CURVE_DATA: {
  483. struct pda_pa_curve_data *curve_data =
  484. (struct pda_pa_curve_data *)entry->data;
  485. if (data_len < sizeof(*curve_data)) {
  486. err = -EINVAL;
  487. goto err;
  488. }
  489. switch (curve_data->cal_method_rev) {
  490. case 0:
  491. err = p54_convert_rev0(dev, curve_data);
  492. break;
  493. case 1:
  494. err = p54_convert_rev1(dev, curve_data);
  495. break;
  496. default:
  497. printk(KERN_ERR "%s: unknown curve data "
  498. "revision %d\n",
  499. wiphy_name(dev->wiphy),
  500. curve_data->cal_method_rev);
  501. err = -ENODEV;
  502. break;
  503. }
  504. if (err)
  505. goto err;
  506. }
  507. break;
  508. case PDR_PRISM_ZIF_TX_IQ_CALIBRATION:
  509. priv->iq_autocal = kmalloc(data_len, GFP_KERNEL);
  510. if (!priv->iq_autocal) {
  511. err = -ENOMEM;
  512. goto err;
  513. }
  514. memcpy(priv->iq_autocal, entry->data, data_len);
  515. priv->iq_autocal_len = data_len / sizeof(struct pda_iq_autocal_entry);
  516. break;
  517. case PDR_DEFAULT_COUNTRY:
  518. p54_parse_default_country(dev, entry->data, data_len);
  519. break;
  520. case PDR_INTERFACE_LIST:
  521. tmp = entry->data;
  522. while ((u8 *)tmp < entry->data + data_len) {
  523. struct bootrec_exp_if *exp_if = tmp;
  524. if (le16_to_cpu(exp_if->if_id) == 0xf)
  525. synth = le16_to_cpu(exp_if->variant);
  526. tmp += sizeof(struct bootrec_exp_if);
  527. }
  528. break;
  529. case PDR_HARDWARE_PLATFORM_COMPONENT_ID:
  530. if (data_len < 2)
  531. break;
  532. priv->version = *(u8 *)(entry->data + 1);
  533. break;
  534. case PDR_RSSI_LINEAR_APPROXIMATION:
  535. case PDR_RSSI_LINEAR_APPROXIMATION_DUAL_BAND:
  536. case PDR_RSSI_LINEAR_APPROXIMATION_EXTENDED:
  537. p54_parse_rssical(dev, entry->data, data_len,
  538. le16_to_cpu(entry->code));
  539. break;
  540. case PDR_RSSI_LINEAR_APPROXIMATION_CUSTOM: {
  541. __le16 *src = (void *) entry->data;
  542. s16 *dst = (void *) &priv->rssical_db;
  543. int i;
  544. if (data_len != sizeof(priv->rssical_db)) {
  545. err = -EINVAL;
  546. goto err;
  547. }
  548. for (i = 0; i < sizeof(priv->rssical_db) /
  549. sizeof(*src); i++)
  550. *(dst++) = (s16) le16_to_cpu(*(src++));
  551. }
  552. break;
  553. case PDR_PRISM_PA_CAL_OUTPUT_POWER_LIMITS_CUSTOM: {
  554. struct pda_custom_wrapper *pda = (void *) entry->data;
  555. if (priv->output_limit || data_len < sizeof(*pda))
  556. break;
  557. priv->output_limit = p54_convert_db(pda, data_len);
  558. }
  559. break;
  560. case PDR_PRISM_PA_CAL_CURVE_DATA_CUSTOM: {
  561. struct pda_custom_wrapper *pda = (void *) entry->data;
  562. if (priv->curve_data || data_len < sizeof(*pda))
  563. break;
  564. priv->curve_data = p54_convert_db(pda, data_len);
  565. }
  566. break;
  567. case PDR_END:
  568. /* make it overrun */
  569. entry_len = len;
  570. break;
  571. case PDR_MANUFACTURING_PART_NUMBER:
  572. case PDR_PDA_VERSION:
  573. case PDR_NIC_SERIAL_NUMBER:
  574. case PDR_REGULATORY_DOMAIN_LIST:
  575. case PDR_TEMPERATURE_TYPE:
  576. case PDR_PRISM_PCI_IDENTIFIER:
  577. case PDR_COUNTRY_INFORMATION:
  578. case PDR_OEM_NAME:
  579. case PDR_PRODUCT_NAME:
  580. case PDR_UTF8_OEM_NAME:
  581. case PDR_UTF8_PRODUCT_NAME:
  582. case PDR_COUNTRY_LIST:
  583. case PDR_ANTENNA_GAIN:
  584. case PDR_PRISM_INDIGO_PA_CALIBRATION_DATA:
  585. case PDR_REGULATORY_POWER_LIMITS:
  586. case PDR_RADIATED_TRANSMISSION_CORRECTION:
  587. case PDR_PRISM_TX_IQ_CALIBRATION:
  588. case PDR_BASEBAND_REGISTERS:
  589. case PDR_PER_CHANNEL_BASEBAND_REGISTERS:
  590. break;
  591. default:
  592. printk(KERN_INFO "%s: unknown eeprom code : 0x%x\n",
  593. wiphy_name(dev->wiphy),
  594. le16_to_cpu(entry->code));
  595. break;
  596. }
  597. entry = (void *)entry + (entry_len + 1)*2;
  598. }
  599. if (!synth || !priv->iq_autocal || !priv->output_limit ||
  600. !priv->curve_data) {
  601. printk(KERN_ERR "%s: not all required entries found in eeprom!\n",
  602. wiphy_name(dev->wiphy));
  603. err = -EINVAL;
  604. goto err;
  605. }
  606. priv->rxhw = synth & PDR_SYNTH_FRONTEND_MASK;
  607. if (priv->rxhw == PDR_SYNTH_FRONTEND_XBOW)
  608. p54_init_xbow_synth(dev);
  609. if (!(synth & PDR_SYNTH_24_GHZ_DISABLED))
  610. dev->wiphy->bands[IEEE80211_BAND_2GHZ] = &band_2GHz;
  611. if (!(synth & PDR_SYNTH_5_GHZ_DISABLED))
  612. dev->wiphy->bands[IEEE80211_BAND_5GHZ] = &band_5GHz;
  613. if ((synth & PDR_SYNTH_RX_DIV_MASK) == PDR_SYNTH_RX_DIV_SUPPORTED)
  614. priv->rx_diversity_mask = 3;
  615. if ((synth & PDR_SYNTH_TX_DIV_MASK) == PDR_SYNTH_TX_DIV_SUPPORTED)
  616. priv->tx_diversity_mask = 3;
  617. if (!is_valid_ether_addr(dev->wiphy->perm_addr)) {
  618. u8 perm_addr[ETH_ALEN];
  619. printk(KERN_WARNING "%s: Invalid hwaddr! Using randomly generated MAC addr\n",
  620. wiphy_name(dev->wiphy));
  621. random_ether_addr(perm_addr);
  622. SET_IEEE80211_PERM_ADDR(dev, perm_addr);
  623. }
  624. printk(KERN_INFO "%s: hwaddr %pM, MAC:isl38%02x RF:%s\n",
  625. wiphy_name(dev->wiphy),
  626. dev->wiphy->perm_addr,
  627. priv->version, p54_rf_chips[priv->rxhw]);
  628. return 0;
  629. err:
  630. if (priv->iq_autocal) {
  631. kfree(priv->iq_autocal);
  632. priv->iq_autocal = NULL;
  633. }
  634. if (priv->output_limit) {
  635. kfree(priv->output_limit);
  636. priv->output_limit = NULL;
  637. }
  638. if (priv->curve_data) {
  639. kfree(priv->curve_data);
  640. priv->curve_data = NULL;
  641. }
  642. printk(KERN_ERR "%s: eeprom parse failed!\n",
  643. wiphy_name(dev->wiphy));
  644. return err;
  645. }
  646. EXPORT_SYMBOL_GPL(p54_parse_eeprom);
  647. static int p54_rssi_to_dbm(struct ieee80211_hw *dev, int rssi)
  648. {
  649. struct p54_common *priv = dev->priv;
  650. int band = dev->conf.channel->band;
  651. if (priv->rxhw != PDR_SYNTH_FRONTEND_LONGBOW)
  652. return ((rssi * priv->rssical_db[band].mul) / 64 +
  653. priv->rssical_db[band].add) / 4;
  654. else
  655. /*
  656. * TODO: find the correct formula
  657. */
  658. return ((rssi * priv->rssical_db[band].mul) / 64 +
  659. priv->rssical_db[band].add) / 4;
  660. }
  661. static int p54_rx_data(struct ieee80211_hw *dev, struct sk_buff *skb)
  662. {
  663. struct p54_common *priv = dev->priv;
  664. struct p54_rx_data *hdr = (struct p54_rx_data *) skb->data;
  665. struct ieee80211_rx_status rx_status = {0};
  666. u16 freq = le16_to_cpu(hdr->freq);
  667. size_t header_len = sizeof(*hdr);
  668. u32 tsf32;
  669. u8 rate = hdr->rate & 0xf;
  670. /*
  671. * If the device is in a unspecified state we have to
  672. * ignore all data frames. Else we could end up with a
  673. * nasty crash.
  674. */
  675. if (unlikely(priv->mode == NL80211_IFTYPE_UNSPECIFIED))
  676. return 0;
  677. if (!(hdr->flags & cpu_to_le16(P54_HDR_FLAG_DATA_IN_FCS_GOOD))) {
  678. return 0;
  679. }
  680. if (hdr->decrypt_status == P54_DECRYPT_OK)
  681. rx_status.flag |= RX_FLAG_DECRYPTED;
  682. if ((hdr->decrypt_status == P54_DECRYPT_FAIL_MICHAEL) ||
  683. (hdr->decrypt_status == P54_DECRYPT_FAIL_TKIP))
  684. rx_status.flag |= RX_FLAG_MMIC_ERROR;
  685. rx_status.signal = p54_rssi_to_dbm(dev, hdr->rssi);
  686. rx_status.noise = priv->noise;
  687. if (hdr->rate & 0x10)
  688. rx_status.flag |= RX_FLAG_SHORTPRE;
  689. if (dev->conf.channel->band == IEEE80211_BAND_5GHZ)
  690. rx_status.rate_idx = (rate < 4) ? 0 : rate - 4;
  691. else
  692. rx_status.rate_idx = rate;
  693. rx_status.freq = freq;
  694. rx_status.band = dev->conf.channel->band;
  695. rx_status.antenna = hdr->antenna;
  696. tsf32 = le32_to_cpu(hdr->tsf32);
  697. if (tsf32 < priv->tsf_low32)
  698. priv->tsf_high32++;
  699. rx_status.mactime = ((u64)priv->tsf_high32) << 32 | tsf32;
  700. priv->tsf_low32 = tsf32;
  701. rx_status.flag |= RX_FLAG_TSFT;
  702. if (hdr->flags & cpu_to_le16(P54_HDR_FLAG_DATA_ALIGN))
  703. header_len += hdr->align[0];
  704. skb_pull(skb, header_len);
  705. skb_trim(skb, le16_to_cpu(hdr->len));
  706. ieee80211_rx_irqsafe(dev, skb, &rx_status);
  707. queue_delayed_work(dev->workqueue, &priv->work,
  708. msecs_to_jiffies(P54_STATISTICS_UPDATE));
  709. return -1;
  710. }
  711. static void inline p54_wake_free_queues(struct ieee80211_hw *dev)
  712. {
  713. struct p54_common *priv = dev->priv;
  714. int i;
  715. if (priv->mode == NL80211_IFTYPE_UNSPECIFIED)
  716. return ;
  717. for (i = 0; i < dev->queues; i++)
  718. if (priv->tx_stats[i + P54_QUEUE_DATA].len <
  719. priv->tx_stats[i + P54_QUEUE_DATA].limit)
  720. ieee80211_wake_queue(dev, i);
  721. }
  722. void p54_free_skb(struct ieee80211_hw *dev, struct sk_buff *skb)
  723. {
  724. struct p54_common *priv = dev->priv;
  725. struct ieee80211_tx_info *info;
  726. struct p54_tx_info *range;
  727. unsigned long flags;
  728. if (unlikely(!skb || !dev || !skb_queue_len(&priv->tx_queue)))
  729. return;
  730. /*
  731. * don't try to free an already unlinked skb
  732. */
  733. if (unlikely((!skb->next) || (!skb->prev)))
  734. return;
  735. spin_lock_irqsave(&priv->tx_queue.lock, flags);
  736. info = IEEE80211_SKB_CB(skb);
  737. range = (void *)info->rate_driver_data;
  738. if (skb->prev != (struct sk_buff *)&priv->tx_queue) {
  739. struct ieee80211_tx_info *ni;
  740. struct p54_tx_info *mr;
  741. ni = IEEE80211_SKB_CB(skb->prev);
  742. mr = (struct p54_tx_info *)ni->rate_driver_data;
  743. }
  744. if (skb->next != (struct sk_buff *)&priv->tx_queue) {
  745. struct ieee80211_tx_info *ni;
  746. struct p54_tx_info *mr;
  747. ni = IEEE80211_SKB_CB(skb->next);
  748. mr = (struct p54_tx_info *)ni->rate_driver_data;
  749. }
  750. __skb_unlink(skb, &priv->tx_queue);
  751. spin_unlock_irqrestore(&priv->tx_queue.lock, flags);
  752. dev_kfree_skb_any(skb);
  753. p54_wake_free_queues(dev);
  754. }
  755. EXPORT_SYMBOL_GPL(p54_free_skb);
  756. static struct sk_buff *p54_find_tx_entry(struct ieee80211_hw *dev,
  757. __le32 req_id)
  758. {
  759. struct p54_common *priv = dev->priv;
  760. struct sk_buff *entry;
  761. unsigned long flags;
  762. spin_lock_irqsave(&priv->tx_queue.lock, flags);
  763. entry = priv->tx_queue.next;
  764. while (entry != (struct sk_buff *)&priv->tx_queue) {
  765. struct p54_hdr *hdr = (struct p54_hdr *) entry->data;
  766. if (hdr->req_id == req_id) {
  767. spin_unlock_irqrestore(&priv->tx_queue.lock, flags);
  768. return entry;
  769. }
  770. entry = entry->next;
  771. }
  772. spin_unlock_irqrestore(&priv->tx_queue.lock, flags);
  773. return NULL;
  774. }
  775. static void p54_rx_frame_sent(struct ieee80211_hw *dev, struct sk_buff *skb)
  776. {
  777. struct p54_common *priv = dev->priv;
  778. struct p54_hdr *hdr = (struct p54_hdr *) skb->data;
  779. struct p54_frame_sent *payload = (struct p54_frame_sent *) hdr->data;
  780. struct sk_buff *entry;
  781. u32 addr = le32_to_cpu(hdr->req_id) - priv->headroom;
  782. struct p54_tx_info *range = NULL;
  783. unsigned long flags;
  784. int count, idx;
  785. spin_lock_irqsave(&priv->tx_queue.lock, flags);
  786. entry = (struct sk_buff *) priv->tx_queue.next;
  787. while (entry != (struct sk_buff *)&priv->tx_queue) {
  788. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(entry);
  789. struct p54_hdr *entry_hdr;
  790. struct p54_tx_data *entry_data;
  791. unsigned int pad = 0, frame_len;
  792. range = (void *)info->rate_driver_data;
  793. if (range->start_addr != addr) {
  794. entry = entry->next;
  795. continue;
  796. }
  797. if (entry->next != (struct sk_buff *)&priv->tx_queue) {
  798. struct ieee80211_tx_info *ni;
  799. struct p54_tx_info *mr;
  800. ni = IEEE80211_SKB_CB(entry->next);
  801. mr = (struct p54_tx_info *)ni->rate_driver_data;
  802. }
  803. __skb_unlink(entry, &priv->tx_queue);
  804. spin_unlock_irqrestore(&priv->tx_queue.lock, flags);
  805. frame_len = entry->len;
  806. entry_hdr = (struct p54_hdr *) entry->data;
  807. entry_data = (struct p54_tx_data *) entry_hdr->data;
  808. priv->tx_stats[entry_data->hw_queue].len--;
  809. priv->stats.dot11ACKFailureCount += payload->tries - 1;
  810. /*
  811. * Frames in P54_QUEUE_FWSCAN and P54_QUEUE_BEACON are
  812. * generated by the driver. Therefore tx_status is bogus
  813. * and we don't want to confuse the mac80211 stack.
  814. */
  815. if (unlikely(entry_data->hw_queue < P54_QUEUE_FWSCAN)) {
  816. if (entry_data->hw_queue == P54_QUEUE_BEACON)
  817. priv->cached_beacon = NULL;
  818. kfree_skb(entry);
  819. goto out;
  820. }
  821. /*
  822. * Clear manually, ieee80211_tx_info_clear_status would
  823. * clear the counts too and we need them.
  824. */
  825. memset(&info->status.ampdu_ack_len, 0,
  826. sizeof(struct ieee80211_tx_info) -
  827. offsetof(struct ieee80211_tx_info, status.ampdu_ack_len));
  828. BUILD_BUG_ON(offsetof(struct ieee80211_tx_info,
  829. status.ampdu_ack_len) != 23);
  830. if (entry_hdr->flags & cpu_to_le16(P54_HDR_FLAG_DATA_ALIGN))
  831. pad = entry_data->align[0];
  832. /* walk through the rates array and adjust the counts */
  833. count = payload->tries;
  834. for (idx = 0; idx < 4; idx++) {
  835. if (count >= info->status.rates[idx].count) {
  836. count -= info->status.rates[idx].count;
  837. } else if (count > 0) {
  838. info->status.rates[idx].count = count;
  839. count = 0;
  840. } else {
  841. info->status.rates[idx].idx = -1;
  842. info->status.rates[idx].count = 0;
  843. }
  844. }
  845. if (!(info->flags & IEEE80211_TX_CTL_NO_ACK) &&
  846. (!payload->status))
  847. info->flags |= IEEE80211_TX_STAT_ACK;
  848. if (payload->status & P54_TX_PSM_CANCELLED)
  849. info->flags |= IEEE80211_TX_STAT_TX_FILTERED;
  850. info->status.ack_signal = p54_rssi_to_dbm(dev,
  851. (int)payload->ack_rssi);
  852. /* Undo all changes to the frame. */
  853. switch (entry_data->key_type) {
  854. case P54_CRYPTO_TKIPMICHAEL: {
  855. u8 *iv = (u8 *)(entry_data->align + pad +
  856. entry_data->crypt_offset);
  857. /* Restore the original TKIP IV. */
  858. iv[2] = iv[0];
  859. iv[0] = iv[1];
  860. iv[1] = (iv[0] | 0x20) & 0x7f; /* WEPSeed - 8.3.2.2 */
  861. frame_len -= 12; /* remove TKIP_MMIC + TKIP_ICV */
  862. break;
  863. }
  864. case P54_CRYPTO_AESCCMP:
  865. frame_len -= 8; /* remove CCMP_MIC */
  866. break;
  867. case P54_CRYPTO_WEP:
  868. frame_len -= 4; /* remove WEP_ICV */
  869. break;
  870. }
  871. skb_trim(entry, frame_len);
  872. skb_pull(entry, sizeof(*hdr) + pad + sizeof(*entry_data));
  873. ieee80211_tx_status_irqsafe(dev, entry);
  874. goto out;
  875. }
  876. spin_unlock_irqrestore(&priv->tx_queue.lock, flags);
  877. out:
  878. p54_wake_free_queues(dev);
  879. }
  880. static void p54_rx_eeprom_readback(struct ieee80211_hw *dev,
  881. struct sk_buff *skb)
  882. {
  883. struct p54_hdr *hdr = (struct p54_hdr *) skb->data;
  884. struct p54_eeprom_lm86 *eeprom = (struct p54_eeprom_lm86 *) hdr->data;
  885. struct p54_common *priv = dev->priv;
  886. if (!priv->eeprom)
  887. return ;
  888. if (priv->fw_var >= 0x509) {
  889. memcpy(priv->eeprom, eeprom->v2.data,
  890. le16_to_cpu(eeprom->v2.len));
  891. } else {
  892. memcpy(priv->eeprom, eeprom->v1.data,
  893. le16_to_cpu(eeprom->v1.len));
  894. }
  895. complete(&priv->eeprom_comp);
  896. }
  897. static void p54_rx_stats(struct ieee80211_hw *dev, struct sk_buff *skb)
  898. {
  899. struct p54_common *priv = dev->priv;
  900. struct p54_hdr *hdr = (struct p54_hdr *) skb->data;
  901. struct p54_statistics *stats = (struct p54_statistics *) hdr->data;
  902. u32 tsf32;
  903. if (unlikely(priv->mode == NL80211_IFTYPE_UNSPECIFIED))
  904. return ;
  905. tsf32 = le32_to_cpu(stats->tsf32);
  906. if (tsf32 < priv->tsf_low32)
  907. priv->tsf_high32++;
  908. priv->tsf_low32 = tsf32;
  909. priv->stats.dot11RTSFailureCount = le32_to_cpu(stats->rts_fail);
  910. priv->stats.dot11RTSSuccessCount = le32_to_cpu(stats->rts_success);
  911. priv->stats.dot11FCSErrorCount = le32_to_cpu(stats->rx_bad_fcs);
  912. priv->noise = p54_rssi_to_dbm(dev, le32_to_cpu(stats->noise));
  913. p54_free_skb(dev, p54_find_tx_entry(dev, hdr->req_id));
  914. }
  915. static void p54_rx_trap(struct ieee80211_hw *dev, struct sk_buff *skb)
  916. {
  917. struct p54_common *priv = dev->priv;
  918. struct p54_hdr *hdr = (struct p54_hdr *) skb->data;
  919. struct p54_trap *trap = (struct p54_trap *) hdr->data;
  920. u16 event = le16_to_cpu(trap->event);
  921. u16 freq = le16_to_cpu(trap->frequency);
  922. switch (event) {
  923. case P54_TRAP_BEACON_TX:
  924. break;
  925. case P54_TRAP_RADAR:
  926. printk(KERN_INFO "%s: radar (freq:%d MHz)\n",
  927. wiphy_name(dev->wiphy), freq);
  928. break;
  929. case P54_TRAP_NO_BEACON:
  930. if (priv->vif)
  931. ieee80211_beacon_loss(priv->vif);
  932. break;
  933. case P54_TRAP_SCAN:
  934. break;
  935. case P54_TRAP_TBTT:
  936. break;
  937. case P54_TRAP_TIMER:
  938. break;
  939. default:
  940. printk(KERN_INFO "%s: received event:%x freq:%d\n",
  941. wiphy_name(dev->wiphy), event, freq);
  942. break;
  943. }
  944. }
  945. static int p54_rx_control(struct ieee80211_hw *dev, struct sk_buff *skb)
  946. {
  947. struct p54_hdr *hdr = (struct p54_hdr *) skb->data;
  948. switch (le16_to_cpu(hdr->type)) {
  949. case P54_CONTROL_TYPE_TXDONE:
  950. p54_rx_frame_sent(dev, skb);
  951. break;
  952. case P54_CONTROL_TYPE_TRAP:
  953. p54_rx_trap(dev, skb);
  954. break;
  955. case P54_CONTROL_TYPE_BBP:
  956. break;
  957. case P54_CONTROL_TYPE_STAT_READBACK:
  958. p54_rx_stats(dev, skb);
  959. break;
  960. case P54_CONTROL_TYPE_EEPROM_READBACK:
  961. p54_rx_eeprom_readback(dev, skb);
  962. break;
  963. default:
  964. printk(KERN_DEBUG "%s: not handling 0x%02x type control frame\n",
  965. wiphy_name(dev->wiphy), le16_to_cpu(hdr->type));
  966. break;
  967. }
  968. return 0;
  969. }
  970. /* returns zero if skb can be reused */
  971. int p54_rx(struct ieee80211_hw *dev, struct sk_buff *skb)
  972. {
  973. u16 type = le16_to_cpu(*((__le16 *)skb->data));
  974. if (type & P54_HDR_FLAG_CONTROL)
  975. return p54_rx_control(dev, skb);
  976. else
  977. return p54_rx_data(dev, skb);
  978. }
  979. EXPORT_SYMBOL_GPL(p54_rx);
  980. /*
  981. * So, the firmware is somewhat stupid and doesn't know what places in its
  982. * memory incoming data should go to. By poking around in the firmware, we
  983. * can find some unused memory to upload our packets to. However, data that we
  984. * want the card to TX needs to stay intact until the card has told us that
  985. * it is done with it. This function finds empty places we can upload to and
  986. * marks allocated areas as reserved if necessary. p54_rx_frame_sent or
  987. * p54_free_skb frees allocated areas.
  988. */
  989. static int p54_assign_address(struct ieee80211_hw *dev, struct sk_buff *skb,
  990. struct p54_hdr *data, u32 len)
  991. {
  992. struct p54_common *priv = dev->priv;
  993. struct sk_buff *entry;
  994. struct sk_buff *target_skb = NULL;
  995. struct ieee80211_tx_info *info;
  996. struct p54_tx_info *range;
  997. u32 last_addr = priv->rx_start;
  998. u32 largest_hole = 0;
  999. u32 target_addr = priv->rx_start;
  1000. unsigned long flags;
  1001. unsigned int left;
  1002. len = (len + priv->headroom + priv->tailroom + 3) & ~0x3;
  1003. if (!skb)
  1004. return -EINVAL;
  1005. spin_lock_irqsave(&priv->tx_queue.lock, flags);
  1006. left = skb_queue_len(&priv->tx_queue);
  1007. if (unlikely(left >= 28)) {
  1008. /*
  1009. * The tx_queue is nearly full!
  1010. * We have throttle normal data traffic, because we must
  1011. * have a few spare slots for control frames left.
  1012. */
  1013. ieee80211_stop_queues(dev);
  1014. queue_delayed_work(dev->workqueue, &priv->work,
  1015. msecs_to_jiffies(P54_TX_TIMEOUT));
  1016. if (unlikely(left == 32)) {
  1017. /*
  1018. * The tx_queue is now really full.
  1019. *
  1020. * TODO: check if the device has crashed and reset it.
  1021. */
  1022. spin_unlock_irqrestore(&priv->tx_queue.lock, flags);
  1023. return -ENOSPC;
  1024. }
  1025. }
  1026. entry = priv->tx_queue.next;
  1027. while (left--) {
  1028. u32 hole_size;
  1029. info = IEEE80211_SKB_CB(entry);
  1030. range = (void *)info->rate_driver_data;
  1031. hole_size = range->start_addr - last_addr;
  1032. if (!target_skb && hole_size >= len) {
  1033. target_skb = entry->prev;
  1034. hole_size -= len;
  1035. target_addr = last_addr;
  1036. }
  1037. largest_hole = max(largest_hole, hole_size);
  1038. last_addr = range->end_addr;
  1039. entry = entry->next;
  1040. }
  1041. if (!target_skb && priv->rx_end - last_addr >= len) {
  1042. target_skb = priv->tx_queue.prev;
  1043. largest_hole = max(largest_hole, priv->rx_end - last_addr - len);
  1044. if (!skb_queue_empty(&priv->tx_queue)) {
  1045. info = IEEE80211_SKB_CB(target_skb);
  1046. range = (void *)info->rate_driver_data;
  1047. target_addr = range->end_addr;
  1048. }
  1049. } else
  1050. largest_hole = max(largest_hole, priv->rx_end - last_addr);
  1051. if (!target_skb) {
  1052. spin_unlock_irqrestore(&priv->tx_queue.lock, flags);
  1053. ieee80211_stop_queues(dev);
  1054. return -ENOSPC;
  1055. }
  1056. info = IEEE80211_SKB_CB(skb);
  1057. range = (void *)info->rate_driver_data;
  1058. range->start_addr = target_addr;
  1059. range->end_addr = target_addr + len;
  1060. __skb_queue_after(&priv->tx_queue, target_skb, skb);
  1061. spin_unlock_irqrestore(&priv->tx_queue.lock, flags);
  1062. if (largest_hole < priv->headroom + sizeof(struct p54_hdr) +
  1063. 48 + IEEE80211_MAX_RTS_THRESHOLD + priv->tailroom)
  1064. ieee80211_stop_queues(dev);
  1065. data->req_id = cpu_to_le32(target_addr + priv->headroom);
  1066. return 0;
  1067. }
  1068. static struct sk_buff *p54_alloc_skb(struct ieee80211_hw *dev, u16 hdr_flags,
  1069. u16 payload_len, u16 type, gfp_t memflags)
  1070. {
  1071. struct p54_common *priv = dev->priv;
  1072. struct p54_hdr *hdr;
  1073. struct sk_buff *skb;
  1074. size_t frame_len = sizeof(*hdr) + payload_len;
  1075. if (frame_len > P54_MAX_CTRL_FRAME_LEN)
  1076. return NULL;
  1077. skb = __dev_alloc_skb(priv->tx_hdr_len + frame_len, memflags);
  1078. if (!skb)
  1079. return NULL;
  1080. skb_reserve(skb, priv->tx_hdr_len);
  1081. hdr = (struct p54_hdr *) skb_put(skb, sizeof(*hdr));
  1082. hdr->flags = cpu_to_le16(hdr_flags);
  1083. hdr->len = cpu_to_le16(payload_len);
  1084. hdr->type = cpu_to_le16(type);
  1085. hdr->tries = hdr->rts_tries = 0;
  1086. if (p54_assign_address(dev, skb, hdr, frame_len)) {
  1087. kfree_skb(skb);
  1088. return NULL;
  1089. }
  1090. return skb;
  1091. }
  1092. int p54_read_eeprom(struct ieee80211_hw *dev)
  1093. {
  1094. struct p54_common *priv = dev->priv;
  1095. struct p54_eeprom_lm86 *eeprom_hdr;
  1096. struct sk_buff *skb;
  1097. size_t eeprom_size = 0x2020, offset = 0, blocksize, maxblocksize;
  1098. int ret = -ENOMEM;
  1099. void *eeprom = NULL;
  1100. maxblocksize = EEPROM_READBACK_LEN;
  1101. if (priv->fw_var >= 0x509)
  1102. maxblocksize -= 0xc;
  1103. else
  1104. maxblocksize -= 0x4;
  1105. skb = p54_alloc_skb(dev, P54_HDR_FLAG_CONTROL, sizeof(*eeprom_hdr) +
  1106. maxblocksize, P54_CONTROL_TYPE_EEPROM_READBACK,
  1107. GFP_KERNEL);
  1108. if (!skb)
  1109. goto free;
  1110. priv->eeprom = kzalloc(EEPROM_READBACK_LEN, GFP_KERNEL);
  1111. if (!priv->eeprom)
  1112. goto free;
  1113. eeprom = kzalloc(eeprom_size, GFP_KERNEL);
  1114. if (!eeprom)
  1115. goto free;
  1116. eeprom_hdr = (struct p54_eeprom_lm86 *) skb_put(skb,
  1117. sizeof(*eeprom_hdr) + maxblocksize);
  1118. while (eeprom_size) {
  1119. blocksize = min(eeprom_size, maxblocksize);
  1120. if (priv->fw_var < 0x509) {
  1121. eeprom_hdr->v1.offset = cpu_to_le16(offset);
  1122. eeprom_hdr->v1.len = cpu_to_le16(blocksize);
  1123. } else {
  1124. eeprom_hdr->v2.offset = cpu_to_le32(offset);
  1125. eeprom_hdr->v2.len = cpu_to_le16(blocksize);
  1126. eeprom_hdr->v2.magic2 = 0xf;
  1127. memcpy(eeprom_hdr->v2.magic, (const char *)"LOCK", 4);
  1128. }
  1129. priv->tx(dev, skb);
  1130. if (!wait_for_completion_interruptible_timeout(&priv->eeprom_comp, HZ)) {
  1131. printk(KERN_ERR "%s: device does not respond!\n",
  1132. wiphy_name(dev->wiphy));
  1133. ret = -EBUSY;
  1134. goto free;
  1135. }
  1136. memcpy(eeprom + offset, priv->eeprom, blocksize);
  1137. offset += blocksize;
  1138. eeprom_size -= blocksize;
  1139. }
  1140. ret = p54_parse_eeprom(dev, eeprom, offset);
  1141. free:
  1142. kfree(priv->eeprom);
  1143. priv->eeprom = NULL;
  1144. p54_free_skb(dev, skb);
  1145. kfree(eeprom);
  1146. return ret;
  1147. }
  1148. EXPORT_SYMBOL_GPL(p54_read_eeprom);
  1149. static int p54_set_tim(struct ieee80211_hw *dev, struct ieee80211_sta *sta,
  1150. bool set)
  1151. {
  1152. struct p54_common *priv = dev->priv;
  1153. struct sk_buff *skb;
  1154. struct p54_tim *tim;
  1155. skb = p54_alloc_skb(dev, P54_HDR_FLAG_CONTROL_OPSET, sizeof(*tim),
  1156. P54_CONTROL_TYPE_TIM, GFP_ATOMIC);
  1157. if (!skb)
  1158. return -ENOMEM;
  1159. tim = (struct p54_tim *) skb_put(skb, sizeof(*tim));
  1160. tim->count = 1;
  1161. tim->entry[0] = cpu_to_le16(set ? (sta->aid | 0x8000) : sta->aid);
  1162. priv->tx(dev, skb);
  1163. return 0;
  1164. }
  1165. static int p54_sta_unlock(struct ieee80211_hw *dev, u8 *addr)
  1166. {
  1167. struct p54_common *priv = dev->priv;
  1168. struct sk_buff *skb;
  1169. struct p54_sta_unlock *sta;
  1170. skb = p54_alloc_skb(dev, P54_HDR_FLAG_CONTROL_OPSET, sizeof(*sta),
  1171. P54_CONTROL_TYPE_PSM_STA_UNLOCK, GFP_ATOMIC);
  1172. if (!skb)
  1173. return -ENOMEM;
  1174. sta = (struct p54_sta_unlock *)skb_put(skb, sizeof(*sta));
  1175. memcpy(sta->addr, addr, ETH_ALEN);
  1176. priv->tx(dev, skb);
  1177. return 0;
  1178. }
  1179. static void p54_sta_notify(struct ieee80211_hw *dev, struct ieee80211_vif *vif,
  1180. enum sta_notify_cmd notify_cmd,
  1181. struct ieee80211_sta *sta)
  1182. {
  1183. switch (notify_cmd) {
  1184. case STA_NOTIFY_ADD:
  1185. case STA_NOTIFY_REMOVE:
  1186. /*
  1187. * Notify the firmware that we don't want or we don't
  1188. * need to buffer frames for this station anymore.
  1189. */
  1190. p54_sta_unlock(dev, sta->addr);
  1191. break;
  1192. case STA_NOTIFY_AWAKE:
  1193. /* update the firmware's filter table */
  1194. p54_sta_unlock(dev, sta->addr);
  1195. break;
  1196. default:
  1197. break;
  1198. }
  1199. }
  1200. static int p54_tx_cancel(struct ieee80211_hw *dev, struct sk_buff *entry)
  1201. {
  1202. struct p54_common *priv = dev->priv;
  1203. struct sk_buff *skb;
  1204. struct p54_hdr *hdr;
  1205. struct p54_txcancel *cancel;
  1206. skb = p54_alloc_skb(dev, P54_HDR_FLAG_CONTROL_OPSET, sizeof(*cancel),
  1207. P54_CONTROL_TYPE_TXCANCEL, GFP_ATOMIC);
  1208. if (!skb)
  1209. return -ENOMEM;
  1210. hdr = (void *)entry->data;
  1211. cancel = (struct p54_txcancel *)skb_put(skb, sizeof(*cancel));
  1212. cancel->req_id = hdr->req_id;
  1213. priv->tx(dev, skb);
  1214. return 0;
  1215. }
  1216. static int p54_tx_fill(struct ieee80211_hw *dev, struct sk_buff *skb,
  1217. struct ieee80211_tx_info *info, u8 *queue, size_t *extra_len,
  1218. u16 *flags, u16 *aid)
  1219. {
  1220. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  1221. struct p54_common *priv = dev->priv;
  1222. int ret = 1;
  1223. switch (priv->mode) {
  1224. case NL80211_IFTYPE_MONITOR:
  1225. /*
  1226. * We have to set P54_HDR_FLAG_DATA_OUT_PROMISC for
  1227. * every frame in promiscuous/monitor mode.
  1228. * see STSW45x0C LMAC API - page 12.
  1229. */
  1230. *aid = 0;
  1231. *flags = P54_HDR_FLAG_DATA_OUT_PROMISC;
  1232. *queue += P54_QUEUE_DATA;
  1233. break;
  1234. case NL80211_IFTYPE_STATION:
  1235. *aid = 1;
  1236. if (unlikely(ieee80211_is_mgmt(hdr->frame_control))) {
  1237. *queue = P54_QUEUE_MGMT;
  1238. ret = 0;
  1239. } else
  1240. *queue += P54_QUEUE_DATA;
  1241. break;
  1242. case NL80211_IFTYPE_AP:
  1243. case NL80211_IFTYPE_ADHOC:
  1244. case NL80211_IFTYPE_MESH_POINT:
  1245. if (info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM) {
  1246. *aid = 0;
  1247. *queue = P54_QUEUE_CAB;
  1248. return 0;
  1249. }
  1250. if (unlikely(ieee80211_is_mgmt(hdr->frame_control))) {
  1251. if (ieee80211_is_probe_resp(hdr->frame_control)) {
  1252. *aid = 0;
  1253. *queue = P54_QUEUE_MGMT;
  1254. *flags = P54_HDR_FLAG_DATA_OUT_TIMESTAMP |
  1255. P54_HDR_FLAG_DATA_OUT_NOCANCEL;
  1256. return 0;
  1257. } else if (ieee80211_is_beacon(hdr->frame_control)) {
  1258. *aid = 0;
  1259. if (info->flags & IEEE80211_TX_CTL_INJECTED) {
  1260. /*
  1261. * Injecting beacons on top of a AP is
  1262. * not a good idea... nevertheless,
  1263. * it should be doable.
  1264. */
  1265. *queue += P54_QUEUE_DATA;
  1266. return 1;
  1267. }
  1268. *flags = P54_HDR_FLAG_DATA_OUT_TIMESTAMP;
  1269. *queue = P54_QUEUE_BEACON;
  1270. *extra_len = IEEE80211_MAX_TIM_LEN;
  1271. return 0;
  1272. } else {
  1273. *queue = P54_QUEUE_MGMT;
  1274. ret = 0;
  1275. }
  1276. } else
  1277. *queue += P54_QUEUE_DATA;
  1278. if (info->control.sta)
  1279. *aid = info->control.sta->aid;
  1280. if (info->flags & IEEE80211_TX_CTL_CLEAR_PS_FILT)
  1281. *flags |= P54_HDR_FLAG_DATA_OUT_NOCANCEL;
  1282. break;
  1283. }
  1284. return ret;
  1285. }
  1286. static u8 p54_convert_algo(enum ieee80211_key_alg alg)
  1287. {
  1288. switch (alg) {
  1289. case ALG_WEP:
  1290. return P54_CRYPTO_WEP;
  1291. case ALG_TKIP:
  1292. return P54_CRYPTO_TKIPMICHAEL;
  1293. case ALG_CCMP:
  1294. return P54_CRYPTO_AESCCMP;
  1295. default:
  1296. return 0;
  1297. }
  1298. }
  1299. static int p54_tx(struct ieee80211_hw *dev, struct sk_buff *skb)
  1300. {
  1301. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  1302. struct ieee80211_tx_queue_stats *current_queue;
  1303. struct p54_common *priv = dev->priv;
  1304. struct p54_hdr *hdr;
  1305. struct p54_tx_data *txhdr;
  1306. size_t padding, len, tim_len = 0;
  1307. int i, j, ridx, ret;
  1308. u16 hdr_flags = 0, aid = 0;
  1309. u8 rate, queue, crypt_offset = 0;
  1310. u8 cts_rate = 0x20;
  1311. u8 rc_flags;
  1312. u8 calculated_tries[4];
  1313. u8 nrates = 0, nremaining = 8;
  1314. queue = skb_get_queue_mapping(skb);
  1315. ret = p54_tx_fill(dev, skb, info, &queue, &tim_len, &hdr_flags, &aid);
  1316. current_queue = &priv->tx_stats[queue];
  1317. if (unlikely((current_queue->len > current_queue->limit) && ret))
  1318. return NETDEV_TX_BUSY;
  1319. current_queue->len++;
  1320. current_queue->count++;
  1321. if ((current_queue->len == current_queue->limit) && ret)
  1322. ieee80211_stop_queue(dev, skb_get_queue_mapping(skb));
  1323. padding = (unsigned long)(skb->data - (sizeof(*hdr) + sizeof(*txhdr))) & 3;
  1324. len = skb->len;
  1325. if (info->control.hw_key) {
  1326. crypt_offset = ieee80211_get_hdrlen_from_skb(skb);
  1327. if (info->control.hw_key->alg == ALG_TKIP) {
  1328. u8 *iv = (u8 *)(skb->data + crypt_offset);
  1329. /*
  1330. * The firmware excepts that the IV has to have
  1331. * this special format
  1332. */
  1333. iv[1] = iv[0];
  1334. iv[0] = iv[2];
  1335. iv[2] = 0;
  1336. }
  1337. }
  1338. txhdr = (struct p54_tx_data *) skb_push(skb, sizeof(*txhdr) + padding);
  1339. hdr = (struct p54_hdr *) skb_push(skb, sizeof(*hdr));
  1340. if (padding)
  1341. hdr_flags |= P54_HDR_FLAG_DATA_ALIGN;
  1342. hdr->type = cpu_to_le16(aid);
  1343. hdr->rts_tries = info->control.rates[0].count;
  1344. /*
  1345. * we register the rates in perfect order, and
  1346. * RTS/CTS won't happen on 5 GHz
  1347. */
  1348. cts_rate = info->control.rts_cts_rate_idx;
  1349. memset(&txhdr->rateset, 0, sizeof(txhdr->rateset));
  1350. /* see how many rates got used */
  1351. for (i = 0; i < 4; i++) {
  1352. if (info->control.rates[i].idx < 0)
  1353. break;
  1354. nrates++;
  1355. }
  1356. /* limit tries to 8/nrates per rate */
  1357. for (i = 0; i < nrates; i++) {
  1358. /*
  1359. * The magic expression here is equivalent to 8/nrates for
  1360. * all values that matter, but avoids division and jumps.
  1361. * Note that nrates can only take the values 1 through 4.
  1362. */
  1363. calculated_tries[i] = min_t(int, ((15 >> nrates) | 1) + 1,
  1364. info->control.rates[i].count);
  1365. nremaining -= calculated_tries[i];
  1366. }
  1367. /* if there are tries left, distribute from back to front */
  1368. for (i = nrates - 1; nremaining > 0 && i >= 0; i--) {
  1369. int tmp = info->control.rates[i].count - calculated_tries[i];
  1370. if (tmp <= 0)
  1371. continue;
  1372. /* RC requested more tries at this rate */
  1373. tmp = min_t(int, tmp, nremaining);
  1374. calculated_tries[i] += tmp;
  1375. nremaining -= tmp;
  1376. }
  1377. ridx = 0;
  1378. for (i = 0; i < nrates && ridx < 8; i++) {
  1379. /* we register the rates in perfect order */
  1380. rate = info->control.rates[i].idx;
  1381. if (info->band == IEEE80211_BAND_5GHZ)
  1382. rate += 4;
  1383. /* store the count we actually calculated for TX status */
  1384. info->control.rates[i].count = calculated_tries[i];
  1385. rc_flags = info->control.rates[i].flags;
  1386. if (rc_flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE) {
  1387. rate |= 0x10;
  1388. cts_rate |= 0x10;
  1389. }
  1390. if (rc_flags & IEEE80211_TX_RC_USE_RTS_CTS)
  1391. rate |= 0x40;
  1392. else if (rc_flags & IEEE80211_TX_RC_USE_CTS_PROTECT)
  1393. rate |= 0x20;
  1394. for (j = 0; j < calculated_tries[i] && ridx < 8; j++) {
  1395. txhdr->rateset[ridx] = rate;
  1396. ridx++;
  1397. }
  1398. }
  1399. if (info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ)
  1400. hdr_flags |= P54_HDR_FLAG_DATA_OUT_SEQNR;
  1401. /* TODO: enable bursting */
  1402. hdr->flags = cpu_to_le16(hdr_flags);
  1403. hdr->tries = ridx;
  1404. txhdr->rts_rate_idx = 0;
  1405. if (info->control.hw_key) {
  1406. txhdr->key_type = p54_convert_algo(info->control.hw_key->alg);
  1407. txhdr->key_len = min((u8)16, info->control.hw_key->keylen);
  1408. memcpy(txhdr->key, info->control.hw_key->key, txhdr->key_len);
  1409. if (info->control.hw_key->alg == ALG_TKIP) {
  1410. if (unlikely(skb_tailroom(skb) < 12))
  1411. goto err;
  1412. /* reserve space for the MIC key */
  1413. len += 8;
  1414. memcpy(skb_put(skb, 8), &(info->control.hw_key->key
  1415. [NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY]), 8);
  1416. }
  1417. /* reserve some space for ICV */
  1418. len += info->control.hw_key->icv_len;
  1419. memset(skb_put(skb, info->control.hw_key->icv_len), 0,
  1420. info->control.hw_key->icv_len);
  1421. } else {
  1422. txhdr->key_type = 0;
  1423. txhdr->key_len = 0;
  1424. }
  1425. txhdr->crypt_offset = crypt_offset;
  1426. txhdr->hw_queue = queue;
  1427. txhdr->backlog = current_queue->len;
  1428. memset(txhdr->durations, 0, sizeof(txhdr->durations));
  1429. txhdr->tx_antenna = ((info->antenna_sel_tx == 0) ?
  1430. 2 : info->antenna_sel_tx - 1) & priv->tx_diversity_mask;
  1431. if (priv->rxhw == PDR_SYNTH_FRONTEND_LONGBOW) {
  1432. txhdr->longbow.cts_rate = cts_rate;
  1433. txhdr->longbow.output_power = cpu_to_le16(priv->output_power);
  1434. } else {
  1435. txhdr->normal.output_power = priv->output_power;
  1436. txhdr->normal.cts_rate = cts_rate;
  1437. }
  1438. if (padding)
  1439. txhdr->align[0] = padding;
  1440. hdr->len = cpu_to_le16(len);
  1441. /* modifies skb->cb and with it info, so must be last! */
  1442. if (unlikely(p54_assign_address(dev, skb, hdr, skb->len + tim_len)))
  1443. goto err;
  1444. priv->tx(dev, skb);
  1445. queue_delayed_work(dev->workqueue, &priv->work,
  1446. msecs_to_jiffies(P54_TX_FRAME_LIFETIME));
  1447. return NETDEV_TX_OK;
  1448. err:
  1449. skb_pull(skb, sizeof(*hdr) + sizeof(*txhdr) + padding);
  1450. current_queue->len--;
  1451. current_queue->count--;
  1452. return NETDEV_TX_BUSY;
  1453. }
  1454. static int p54_setup_mac(struct ieee80211_hw *dev)
  1455. {
  1456. struct p54_common *priv = dev->priv;
  1457. struct sk_buff *skb;
  1458. struct p54_setup_mac *setup;
  1459. u16 mode;
  1460. skb = p54_alloc_skb(dev, P54_HDR_FLAG_CONTROL_OPSET, sizeof(*setup),
  1461. P54_CONTROL_TYPE_SETUP, GFP_ATOMIC);
  1462. if (!skb)
  1463. return -ENOMEM;
  1464. setup = (struct p54_setup_mac *) skb_put(skb, sizeof(*setup));
  1465. if (dev->conf.radio_enabled) {
  1466. switch (priv->mode) {
  1467. case NL80211_IFTYPE_STATION:
  1468. mode = P54_FILTER_TYPE_STATION;
  1469. break;
  1470. case NL80211_IFTYPE_AP:
  1471. mode = P54_FILTER_TYPE_AP;
  1472. break;
  1473. case NL80211_IFTYPE_ADHOC:
  1474. case NL80211_IFTYPE_MESH_POINT:
  1475. mode = P54_FILTER_TYPE_IBSS;
  1476. break;
  1477. case NL80211_IFTYPE_MONITOR:
  1478. mode = P54_FILTER_TYPE_PROMISCUOUS;
  1479. break;
  1480. default:
  1481. mode = P54_FILTER_TYPE_HIBERNATE;
  1482. break;
  1483. }
  1484. /*
  1485. * "TRANSPARENT and PROMISCUOUS are mutually exclusive"
  1486. * STSW45X0C LMAC API - page 12
  1487. */
  1488. if (((priv->filter_flags & FIF_PROMISC_IN_BSS) ||
  1489. (priv->filter_flags & FIF_OTHER_BSS)) &&
  1490. (mode != P54_FILTER_TYPE_PROMISCUOUS))
  1491. mode |= P54_FILTER_TYPE_TRANSPARENT;
  1492. } else
  1493. mode = P54_FILTER_TYPE_HIBERNATE;
  1494. setup->mac_mode = cpu_to_le16(mode);
  1495. memcpy(setup->mac_addr, priv->mac_addr, ETH_ALEN);
  1496. memcpy(setup->bssid, priv->bssid, ETH_ALEN);
  1497. setup->rx_antenna = 2 & priv->rx_diversity_mask; /* automatic */
  1498. setup->rx_align = 0;
  1499. if (priv->fw_var < 0x500) {
  1500. setup->v1.basic_rate_mask = cpu_to_le32(priv->basic_rate_mask);
  1501. memset(setup->v1.rts_rates, 0, 8);
  1502. setup->v1.rx_addr = cpu_to_le32(priv->rx_end);
  1503. setup->v1.max_rx = cpu_to_le16(priv->rx_mtu);
  1504. setup->v1.rxhw = cpu_to_le16(priv->rxhw);
  1505. setup->v1.wakeup_timer = cpu_to_le16(priv->wakeup_timer);
  1506. setup->v1.unalloc0 = cpu_to_le16(0);
  1507. } else {
  1508. setup->v2.rx_addr = cpu_to_le32(priv->rx_end);
  1509. setup->v2.max_rx = cpu_to_le16(priv->rx_mtu);
  1510. setup->v2.rxhw = cpu_to_le16(priv->rxhw);
  1511. setup->v2.timer = cpu_to_le16(priv->wakeup_timer);
  1512. setup->v2.truncate = cpu_to_le16(48896);
  1513. setup->v2.basic_rate_mask = cpu_to_le32(priv->basic_rate_mask);
  1514. setup->v2.sbss_offset = 0;
  1515. setup->v2.mcast_window = 0;
  1516. setup->v2.rx_rssi_threshold = 0;
  1517. setup->v2.rx_ed_threshold = 0;
  1518. setup->v2.ref_clock = cpu_to_le32(644245094);
  1519. setup->v2.lpf_bandwidth = cpu_to_le16(65535);
  1520. setup->v2.osc_start_delay = cpu_to_le16(65535);
  1521. }
  1522. priv->tx(dev, skb);
  1523. return 0;
  1524. }
  1525. static int p54_scan(struct ieee80211_hw *dev, u16 mode, u16 dwell)
  1526. {
  1527. struct p54_common *priv = dev->priv;
  1528. struct sk_buff *skb;
  1529. struct p54_hdr *hdr;
  1530. struct p54_scan_head *head;
  1531. struct p54_iq_autocal_entry *iq_autocal;
  1532. union p54_scan_body_union *body;
  1533. struct p54_scan_tail_rate *rate;
  1534. struct pda_rssi_cal_entry *rssi;
  1535. unsigned int i;
  1536. void *entry;
  1537. int band = dev->conf.channel->band;
  1538. __le16 freq = cpu_to_le16(dev->conf.channel->center_freq);
  1539. skb = p54_alloc_skb(dev, P54_HDR_FLAG_CONTROL_OPSET, sizeof(*head) +
  1540. 2 + sizeof(*iq_autocal) + sizeof(*body) +
  1541. sizeof(*rate) + 2 * sizeof(*rssi),
  1542. P54_CONTROL_TYPE_SCAN, GFP_ATOMIC);
  1543. if (!skb)
  1544. return -ENOMEM;
  1545. head = (struct p54_scan_head *) skb_put(skb, sizeof(*head));
  1546. memset(head->scan_params, 0, sizeof(head->scan_params));
  1547. head->mode = cpu_to_le16(mode);
  1548. head->dwell = cpu_to_le16(dwell);
  1549. head->freq = freq;
  1550. if (priv->rxhw == PDR_SYNTH_FRONTEND_LONGBOW) {
  1551. __le16 *pa_power_points = (__le16 *) skb_put(skb, 2);
  1552. *pa_power_points = cpu_to_le16(0x0c);
  1553. }
  1554. iq_autocal = (void *) skb_put(skb, sizeof(*iq_autocal));
  1555. for (i = 0; i < priv->iq_autocal_len; i++) {
  1556. if (priv->iq_autocal[i].freq != freq)
  1557. continue;
  1558. memcpy(iq_autocal, &priv->iq_autocal[i].params,
  1559. sizeof(struct p54_iq_autocal_entry));
  1560. break;
  1561. }
  1562. if (i == priv->iq_autocal_len)
  1563. goto err;
  1564. if (priv->rxhw == PDR_SYNTH_FRONTEND_LONGBOW)
  1565. body = (void *) skb_put(skb, sizeof(body->longbow));
  1566. else
  1567. body = (void *) skb_put(skb, sizeof(body->normal));
  1568. for (i = 0; i < priv->output_limit->entries; i++) {
  1569. __le16 *entry_freq = (void *) (priv->output_limit->data +
  1570. priv->output_limit->entry_size * i);
  1571. if (*entry_freq != freq)
  1572. continue;
  1573. if (priv->rxhw == PDR_SYNTH_FRONTEND_LONGBOW) {
  1574. memcpy(&body->longbow.power_limits,
  1575. (void *) entry_freq + sizeof(__le16),
  1576. priv->output_limit->entry_size);
  1577. } else {
  1578. struct pda_channel_output_limit *limits =
  1579. (void *) entry_freq;
  1580. body->normal.val_barker = 0x38;
  1581. body->normal.val_bpsk = body->normal.dup_bpsk =
  1582. limits->val_bpsk;
  1583. body->normal.val_qpsk = body->normal.dup_qpsk =
  1584. limits->val_qpsk;
  1585. body->normal.val_16qam = body->normal.dup_16qam =
  1586. limits->val_16qam;
  1587. body->normal.val_64qam = body->normal.dup_64qam =
  1588. limits->val_64qam;
  1589. }
  1590. break;
  1591. }
  1592. if (i == priv->output_limit->entries)
  1593. goto err;
  1594. entry = (void *)(priv->curve_data->data + priv->curve_data->offset);
  1595. for (i = 0; i < priv->curve_data->entries; i++) {
  1596. if (*((__le16 *)entry) != freq) {
  1597. entry += priv->curve_data->entry_size;
  1598. continue;
  1599. }
  1600. if (priv->rxhw == PDR_SYNTH_FRONTEND_LONGBOW) {
  1601. memcpy(&body->longbow.curve_data,
  1602. (void *) entry + sizeof(__le16),
  1603. priv->curve_data->entry_size);
  1604. } else {
  1605. struct p54_scan_body *chan = &body->normal;
  1606. struct pda_pa_curve_data *curve_data =
  1607. (void *) priv->curve_data->data;
  1608. entry += sizeof(__le16);
  1609. chan->pa_points_per_curve = 8;
  1610. memset(chan->curve_data, 0, sizeof(*chan->curve_data));
  1611. memcpy(chan->curve_data, entry,
  1612. sizeof(struct p54_pa_curve_data_sample) *
  1613. min((u8)8, curve_data->points_per_channel));
  1614. }
  1615. break;
  1616. }
  1617. if (i == priv->curve_data->entries)
  1618. goto err;
  1619. if ((priv->fw_var >= 0x500) && (priv->fw_var < 0x509)) {
  1620. rate = (void *) skb_put(skb, sizeof(*rate));
  1621. rate->basic_rate_mask = cpu_to_le32(priv->basic_rate_mask);
  1622. for (i = 0; i < sizeof(rate->rts_rates); i++)
  1623. rate->rts_rates[i] = i;
  1624. }
  1625. rssi = (struct pda_rssi_cal_entry *) skb_put(skb, sizeof(*rssi));
  1626. rssi->mul = cpu_to_le16(priv->rssical_db[band].mul);
  1627. rssi->add = cpu_to_le16(priv->rssical_db[band].add);
  1628. if (priv->rxhw == PDR_SYNTH_FRONTEND_LONGBOW) {
  1629. /* Longbow frontend needs ever more */
  1630. rssi = (void *) skb_put(skb, sizeof(*rssi));
  1631. rssi->mul = cpu_to_le16(priv->rssical_db[band].longbow_unkn);
  1632. rssi->add = cpu_to_le16(priv->rssical_db[band].longbow_unk2);
  1633. }
  1634. if (priv->fw_var >= 0x509) {
  1635. rate = (void *) skb_put(skb, sizeof(*rate));
  1636. rate->basic_rate_mask = cpu_to_le32(priv->basic_rate_mask);
  1637. for (i = 0; i < sizeof(rate->rts_rates); i++)
  1638. rate->rts_rates[i] = i;
  1639. }
  1640. hdr = (struct p54_hdr *) skb->data;
  1641. hdr->len = cpu_to_le16(skb->len - sizeof(*hdr));
  1642. priv->tx(dev, skb);
  1643. return 0;
  1644. err:
  1645. printk(KERN_ERR "%s: frequency change failed\n", wiphy_name(dev->wiphy));
  1646. p54_free_skb(dev, skb);
  1647. return -EINVAL;
  1648. }
  1649. static int p54_set_leds(struct ieee80211_hw *dev)
  1650. {
  1651. struct p54_common *priv = dev->priv;
  1652. struct sk_buff *skb;
  1653. struct p54_led *led;
  1654. skb = p54_alloc_skb(dev, P54_HDR_FLAG_CONTROL_OPSET, sizeof(*led),
  1655. P54_CONTROL_TYPE_LED, GFP_ATOMIC);
  1656. if (!skb)
  1657. return -ENOMEM;
  1658. led = (struct p54_led *) skb_put(skb, sizeof(*led));
  1659. led->flags = cpu_to_le16(0x0003);
  1660. led->mask[0] = led->mask[1] = cpu_to_le16(priv->softled_state);
  1661. led->delay[0] = cpu_to_le16(1);
  1662. led->delay[1] = cpu_to_le16(0);
  1663. priv->tx(dev, skb);
  1664. return 0;
  1665. }
  1666. #define P54_SET_QUEUE(queue, ai_fs, cw_min, cw_max, _txop) \
  1667. do { \
  1668. queue.aifs = cpu_to_le16(ai_fs); \
  1669. queue.cwmin = cpu_to_le16(cw_min); \
  1670. queue.cwmax = cpu_to_le16(cw_max); \
  1671. queue.txop = cpu_to_le16(_txop); \
  1672. } while(0)
  1673. static int p54_set_edcf(struct ieee80211_hw *dev)
  1674. {
  1675. struct p54_common *priv = dev->priv;
  1676. struct sk_buff *skb;
  1677. struct p54_edcf *edcf;
  1678. skb = p54_alloc_skb(dev, P54_HDR_FLAG_CONTROL_OPSET, sizeof(*edcf),
  1679. P54_CONTROL_TYPE_DCFINIT, GFP_ATOMIC);
  1680. if (!skb)
  1681. return -ENOMEM;
  1682. edcf = (struct p54_edcf *)skb_put(skb, sizeof(*edcf));
  1683. if (priv->use_short_slot) {
  1684. edcf->slottime = 9;
  1685. edcf->sifs = 0x10;
  1686. edcf->eofpad = 0x00;
  1687. } else {
  1688. edcf->slottime = 20;
  1689. edcf->sifs = 0x0a;
  1690. edcf->eofpad = 0x06;
  1691. }
  1692. /* (see prism54/isl_oid.h for further details) */
  1693. edcf->frameburst = cpu_to_le16(0);
  1694. edcf->round_trip_delay = cpu_to_le16(0);
  1695. edcf->flags = 0;
  1696. memset(edcf->mapping, 0, sizeof(edcf->mapping));
  1697. memcpy(edcf->queue, priv->qos_params, sizeof(edcf->queue));
  1698. priv->tx(dev, skb);
  1699. return 0;
  1700. }
  1701. static int p54_set_ps(struct ieee80211_hw *dev)
  1702. {
  1703. struct p54_common *priv = dev->priv;
  1704. struct sk_buff *skb;
  1705. struct p54_psm *psm;
  1706. u16 mode;
  1707. int i;
  1708. if (dev->conf.flags & IEEE80211_CONF_PS)
  1709. mode = P54_PSM | P54_PSM_BEACON_TIMEOUT | P54_PSM_DTIM |
  1710. P54_PSM_CHECKSUM | P54_PSM_MCBC;
  1711. else
  1712. mode = P54_PSM_CAM;
  1713. skb = p54_alloc_skb(dev, P54_HDR_FLAG_CONTROL_OPSET, sizeof(*psm),
  1714. P54_CONTROL_TYPE_PSM, GFP_ATOMIC);
  1715. if (!skb)
  1716. return -ENOMEM;
  1717. psm = (struct p54_psm *)skb_put(skb, sizeof(*psm));
  1718. psm->mode = cpu_to_le16(mode);
  1719. psm->aid = cpu_to_le16(priv->aid);
  1720. for (i = 0; i < ARRAY_SIZE(psm->intervals); i++) {
  1721. psm->intervals[i].interval =
  1722. cpu_to_le16(dev->conf.listen_interval);
  1723. psm->intervals[i].periods = cpu_to_le16(1);
  1724. }
  1725. psm->beacon_rssi_skip_max = 200;
  1726. psm->rssi_delta_threshold = 0;
  1727. psm->nr = 10;
  1728. psm->exclude[0] = 0;
  1729. priv->tx(dev, skb);
  1730. return 0;
  1731. }
  1732. static int p54_beacon_tim(struct sk_buff *skb)
  1733. {
  1734. /*
  1735. * the good excuse for this mess is ... the firmware.
  1736. * The dummy TIM MUST be at the end of the beacon frame,
  1737. * because it'll be overwritten!
  1738. */
  1739. struct ieee80211_mgmt *mgmt = (void *)skb->data;
  1740. u8 *pos, *end;
  1741. if (skb->len <= sizeof(mgmt))
  1742. return -EINVAL;
  1743. pos = (u8 *)mgmt->u.beacon.variable;
  1744. end = skb->data + skb->len;
  1745. while (pos < end) {
  1746. if (pos + 2 + pos[1] > end)
  1747. return -EINVAL;
  1748. if (pos[0] == WLAN_EID_TIM) {
  1749. u8 dtim_len = pos[1];
  1750. u8 dtim_period = pos[3];
  1751. u8 *next = pos + 2 + dtim_len;
  1752. if (dtim_len < 3)
  1753. return -EINVAL;
  1754. memmove(pos, next, end - next);
  1755. if (dtim_len > 3)
  1756. skb_trim(skb, skb->len - (dtim_len - 3));
  1757. pos = end - (dtim_len + 2);
  1758. /* add the dummy at the end */
  1759. pos[0] = WLAN_EID_TIM;
  1760. pos[1] = 3;
  1761. pos[2] = 0;
  1762. pos[3] = dtim_period;
  1763. pos[4] = 0;
  1764. return 0;
  1765. }
  1766. pos += 2 + pos[1];
  1767. }
  1768. return 0;
  1769. }
  1770. static int p54_beacon_update(struct ieee80211_hw *dev,
  1771. struct ieee80211_vif *vif)
  1772. {
  1773. struct p54_common *priv = dev->priv;
  1774. struct sk_buff *beacon;
  1775. int ret;
  1776. if (priv->cached_beacon) {
  1777. p54_tx_cancel(dev, priv->cached_beacon);
  1778. /* wait for the last beacon the be freed */
  1779. msleep(10);
  1780. }
  1781. beacon = ieee80211_beacon_get(dev, vif);
  1782. if (!beacon)
  1783. return -ENOMEM;
  1784. ret = p54_beacon_tim(beacon);
  1785. if (ret)
  1786. return ret;
  1787. ret = p54_tx(dev, beacon);
  1788. if (ret)
  1789. return ret;
  1790. priv->cached_beacon = beacon;
  1791. priv->tsf_high32 = 0;
  1792. priv->tsf_low32 = 0;
  1793. return 0;
  1794. }
  1795. static int p54_start(struct ieee80211_hw *dev)
  1796. {
  1797. struct p54_common *priv = dev->priv;
  1798. int err;
  1799. mutex_lock(&priv->conf_mutex);
  1800. err = priv->open(dev);
  1801. if (err)
  1802. goto out;
  1803. P54_SET_QUEUE(priv->qos_params[0], 0x0002, 0x0003, 0x0007, 47);
  1804. P54_SET_QUEUE(priv->qos_params[1], 0x0002, 0x0007, 0x000f, 94);
  1805. P54_SET_QUEUE(priv->qos_params[2], 0x0003, 0x000f, 0x03ff, 0);
  1806. P54_SET_QUEUE(priv->qos_params[3], 0x0007, 0x000f, 0x03ff, 0);
  1807. err = p54_set_edcf(dev);
  1808. if (err)
  1809. goto out;
  1810. memset(priv->bssid, ~0, ETH_ALEN);
  1811. priv->mode = NL80211_IFTYPE_MONITOR;
  1812. err = p54_setup_mac(dev);
  1813. if (err) {
  1814. priv->mode = NL80211_IFTYPE_UNSPECIFIED;
  1815. goto out;
  1816. }
  1817. queue_delayed_work(dev->workqueue, &priv->work, 0);
  1818. priv->softled_state = 0;
  1819. err = p54_set_leds(dev);
  1820. out:
  1821. mutex_unlock(&priv->conf_mutex);
  1822. return err;
  1823. }
  1824. static void p54_stop(struct ieee80211_hw *dev)
  1825. {
  1826. struct p54_common *priv = dev->priv;
  1827. struct sk_buff *skb;
  1828. mutex_lock(&priv->conf_mutex);
  1829. priv->mode = NL80211_IFTYPE_UNSPECIFIED;
  1830. priv->softled_state = 0;
  1831. p54_set_leds(dev);
  1832. #ifdef CONFIG_P54_LEDS
  1833. cancel_delayed_work_sync(&priv->led_work);
  1834. #endif /* CONFIG_P54_LEDS */
  1835. cancel_delayed_work_sync(&priv->work);
  1836. if (priv->cached_beacon)
  1837. p54_tx_cancel(dev, priv->cached_beacon);
  1838. priv->stop(dev);
  1839. while ((skb = skb_dequeue(&priv->tx_queue)))
  1840. kfree_skb(skb);
  1841. priv->cached_beacon = NULL;
  1842. priv->tsf_high32 = priv->tsf_low32 = 0;
  1843. mutex_unlock(&priv->conf_mutex);
  1844. }
  1845. static int p54_add_interface(struct ieee80211_hw *dev,
  1846. struct ieee80211_if_init_conf *conf)
  1847. {
  1848. struct p54_common *priv = dev->priv;
  1849. mutex_lock(&priv->conf_mutex);
  1850. if (priv->mode != NL80211_IFTYPE_MONITOR) {
  1851. mutex_unlock(&priv->conf_mutex);
  1852. return -EOPNOTSUPP;
  1853. }
  1854. priv->vif = conf->vif;
  1855. switch (conf->type) {
  1856. case NL80211_IFTYPE_STATION:
  1857. case NL80211_IFTYPE_ADHOC:
  1858. case NL80211_IFTYPE_AP:
  1859. case NL80211_IFTYPE_MESH_POINT:
  1860. priv->mode = conf->type;
  1861. break;
  1862. default:
  1863. mutex_unlock(&priv->conf_mutex);
  1864. return -EOPNOTSUPP;
  1865. }
  1866. memcpy(priv->mac_addr, conf->mac_addr, ETH_ALEN);
  1867. p54_setup_mac(dev);
  1868. mutex_unlock(&priv->conf_mutex);
  1869. return 0;
  1870. }
  1871. static void p54_remove_interface(struct ieee80211_hw *dev,
  1872. struct ieee80211_if_init_conf *conf)
  1873. {
  1874. struct p54_common *priv = dev->priv;
  1875. mutex_lock(&priv->conf_mutex);
  1876. priv->vif = NULL;
  1877. if (priv->cached_beacon)
  1878. p54_tx_cancel(dev, priv->cached_beacon);
  1879. priv->mode = NL80211_IFTYPE_MONITOR;
  1880. memset(priv->mac_addr, 0, ETH_ALEN);
  1881. memset(priv->bssid, 0, ETH_ALEN);
  1882. p54_setup_mac(dev);
  1883. mutex_unlock(&priv->conf_mutex);
  1884. }
  1885. static int p54_config(struct ieee80211_hw *dev, u32 changed)
  1886. {
  1887. int ret = 0;
  1888. struct p54_common *priv = dev->priv;
  1889. struct ieee80211_conf *conf = &dev->conf;
  1890. mutex_lock(&priv->conf_mutex);
  1891. if (changed & IEEE80211_CONF_CHANGE_POWER)
  1892. priv->output_power = conf->power_level << 2;
  1893. if (changed & IEEE80211_CONF_CHANGE_RADIO_ENABLED) {
  1894. ret = p54_setup_mac(dev);
  1895. if (ret)
  1896. goto out;
  1897. }
  1898. if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
  1899. ret = p54_scan(dev, P54_SCAN_EXIT, 0);
  1900. if (ret)
  1901. goto out;
  1902. }
  1903. if (changed & IEEE80211_CONF_CHANGE_PS) {
  1904. ret = p54_set_ps(dev);
  1905. if (ret)
  1906. goto out;
  1907. }
  1908. out:
  1909. mutex_unlock(&priv->conf_mutex);
  1910. return ret;
  1911. }
  1912. static void p54_configure_filter(struct ieee80211_hw *dev,
  1913. unsigned int changed_flags,
  1914. unsigned int *total_flags,
  1915. int mc_count, struct dev_mc_list *mclist)
  1916. {
  1917. struct p54_common *priv = dev->priv;
  1918. *total_flags &= FIF_PROMISC_IN_BSS |
  1919. FIF_OTHER_BSS;
  1920. priv->filter_flags = *total_flags;
  1921. if (changed_flags & (FIF_PROMISC_IN_BSS | FIF_OTHER_BSS))
  1922. p54_setup_mac(dev);
  1923. }
  1924. static int p54_conf_tx(struct ieee80211_hw *dev, u16 queue,
  1925. const struct ieee80211_tx_queue_params *params)
  1926. {
  1927. struct p54_common *priv = dev->priv;
  1928. int ret;
  1929. mutex_lock(&priv->conf_mutex);
  1930. if ((params) && !(queue > 4)) {
  1931. P54_SET_QUEUE(priv->qos_params[queue], params->aifs,
  1932. params->cw_min, params->cw_max, params->txop);
  1933. ret = p54_set_edcf(dev);
  1934. } else
  1935. ret = -EINVAL;
  1936. mutex_unlock(&priv->conf_mutex);
  1937. return ret;
  1938. }
  1939. static int p54_init_xbow_synth(struct ieee80211_hw *dev)
  1940. {
  1941. struct p54_common *priv = dev->priv;
  1942. struct sk_buff *skb;
  1943. struct p54_xbow_synth *xbow;
  1944. skb = p54_alloc_skb(dev, P54_HDR_FLAG_CONTROL_OPSET, sizeof(*xbow),
  1945. P54_CONTROL_TYPE_XBOW_SYNTH_CFG, GFP_KERNEL);
  1946. if (!skb)
  1947. return -ENOMEM;
  1948. xbow = (struct p54_xbow_synth *)skb_put(skb, sizeof(*xbow));
  1949. xbow->magic1 = cpu_to_le16(0x1);
  1950. xbow->magic2 = cpu_to_le16(0x2);
  1951. xbow->freq = cpu_to_le16(5390);
  1952. memset(xbow->padding, 0, sizeof(xbow->padding));
  1953. priv->tx(dev, skb);
  1954. return 0;
  1955. }
  1956. static void p54_work(struct work_struct *work)
  1957. {
  1958. struct p54_common *priv = container_of(work, struct p54_common,
  1959. work.work);
  1960. struct ieee80211_hw *dev = priv->hw;
  1961. struct sk_buff *skb;
  1962. if (unlikely(priv->mode == NL80211_IFTYPE_UNSPECIFIED))
  1963. return ;
  1964. /*
  1965. * TODO: walk through tx_queue and do the following tasks
  1966. * 1. initiate bursts.
  1967. * 2. cancel stuck frames / reset the device if necessary.
  1968. */
  1969. skb = p54_alloc_skb(dev, P54_HDR_FLAG_CONTROL,
  1970. sizeof(struct p54_statistics),
  1971. P54_CONTROL_TYPE_STAT_READBACK, GFP_KERNEL);
  1972. if (!skb)
  1973. return ;
  1974. priv->tx(dev, skb);
  1975. }
  1976. static int p54_get_stats(struct ieee80211_hw *dev,
  1977. struct ieee80211_low_level_stats *stats)
  1978. {
  1979. struct p54_common *priv = dev->priv;
  1980. memcpy(stats, &priv->stats, sizeof(*stats));
  1981. return 0;
  1982. }
  1983. static int p54_get_tx_stats(struct ieee80211_hw *dev,
  1984. struct ieee80211_tx_queue_stats *stats)
  1985. {
  1986. struct p54_common *priv = dev->priv;
  1987. memcpy(stats, &priv->tx_stats[P54_QUEUE_DATA],
  1988. sizeof(stats[0]) * dev->queues);
  1989. return 0;
  1990. }
  1991. static void p54_bss_info_changed(struct ieee80211_hw *dev,
  1992. struct ieee80211_vif *vif,
  1993. struct ieee80211_bss_conf *info,
  1994. u32 changed)
  1995. {
  1996. struct p54_common *priv = dev->priv;
  1997. int ret;
  1998. mutex_lock(&priv->conf_mutex);
  1999. if (changed & BSS_CHANGED_BSSID) {
  2000. memcpy(priv->bssid, info->bssid, ETH_ALEN);
  2001. ret = p54_setup_mac(dev);
  2002. if (ret)
  2003. goto out;
  2004. }
  2005. if (changed & BSS_CHANGED_BEACON) {
  2006. ret = p54_scan(dev, P54_SCAN_EXIT, 0);
  2007. if (ret)
  2008. goto out;
  2009. ret = p54_setup_mac(dev);
  2010. if (ret)
  2011. goto out;
  2012. ret = p54_beacon_update(dev, vif);
  2013. if (ret)
  2014. goto out;
  2015. }
  2016. /* XXX: this mimics having two callbacks... clean up */
  2017. out:
  2018. mutex_unlock(&priv->conf_mutex);
  2019. if (changed & (BSS_CHANGED_ERP_SLOT | BSS_CHANGED_BEACON)) {
  2020. priv->use_short_slot = info->use_short_slot;
  2021. p54_set_edcf(dev);
  2022. }
  2023. if (changed & BSS_CHANGED_BASIC_RATES) {
  2024. if (dev->conf.channel->band == IEEE80211_BAND_5GHZ)
  2025. priv->basic_rate_mask = (info->basic_rates << 4);
  2026. else
  2027. priv->basic_rate_mask = info->basic_rates;
  2028. p54_setup_mac(dev);
  2029. if (priv->fw_var >= 0x500)
  2030. p54_scan(dev, P54_SCAN_EXIT, 0);
  2031. }
  2032. if (changed & BSS_CHANGED_ASSOC) {
  2033. if (info->assoc) {
  2034. priv->aid = info->aid;
  2035. priv->wakeup_timer = info->beacon_int *
  2036. info->dtim_period * 5;
  2037. p54_setup_mac(dev);
  2038. }
  2039. }
  2040. }
  2041. static int p54_set_key(struct ieee80211_hw *dev, enum set_key_cmd cmd,
  2042. struct ieee80211_vif *vif, struct ieee80211_sta *sta,
  2043. struct ieee80211_key_conf *key)
  2044. {
  2045. struct p54_common *priv = dev->priv;
  2046. struct sk_buff *skb;
  2047. struct p54_keycache *rxkey;
  2048. int slot, ret = 0;
  2049. u8 algo = 0;
  2050. if (modparam_nohwcrypt)
  2051. return -EOPNOTSUPP;
  2052. mutex_lock(&priv->conf_mutex);
  2053. if (cmd == SET_KEY) {
  2054. switch (key->alg) {
  2055. case ALG_TKIP:
  2056. if (!(priv->privacy_caps & (BR_DESC_PRIV_CAP_MICHAEL |
  2057. BR_DESC_PRIV_CAP_TKIP))) {
  2058. ret = -EOPNOTSUPP;
  2059. goto out_unlock;
  2060. }
  2061. key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  2062. algo = P54_CRYPTO_TKIPMICHAEL;
  2063. break;
  2064. case ALG_WEP:
  2065. if (!(priv->privacy_caps & BR_DESC_PRIV_CAP_WEP)) {
  2066. ret = -EOPNOTSUPP;
  2067. goto out_unlock;
  2068. }
  2069. key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  2070. algo = P54_CRYPTO_WEP;
  2071. break;
  2072. case ALG_CCMP:
  2073. if (!(priv->privacy_caps & BR_DESC_PRIV_CAP_AESCCMP)) {
  2074. ret = -EOPNOTSUPP;
  2075. goto out_unlock;
  2076. }
  2077. key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  2078. algo = P54_CRYPTO_AESCCMP;
  2079. break;
  2080. default:
  2081. ret = -EOPNOTSUPP;
  2082. goto out_unlock;
  2083. }
  2084. slot = bitmap_find_free_region(priv->used_rxkeys,
  2085. priv->rx_keycache_size, 0);
  2086. if (slot < 0) {
  2087. /*
  2088. * The device supports the choosen algorithm, but the
  2089. * firmware does not provide enough key slots to store
  2090. * all of them.
  2091. * But encryption offload for outgoing frames is always
  2092. * possible, so we just pretend that the upload was
  2093. * successful and do the decryption in software.
  2094. */
  2095. /* mark the key as invalid. */
  2096. key->hw_key_idx = 0xff;
  2097. goto out_unlock;
  2098. }
  2099. } else {
  2100. slot = key->hw_key_idx;
  2101. if (slot == 0xff) {
  2102. /* This key was not uploaded into the rx key cache. */
  2103. goto out_unlock;
  2104. }
  2105. bitmap_release_region(priv->used_rxkeys, slot, 0);
  2106. algo = 0;
  2107. }
  2108. skb = p54_alloc_skb(dev, P54_HDR_FLAG_CONTROL_OPSET, sizeof(*rxkey),
  2109. P54_CONTROL_TYPE_RX_KEYCACHE, GFP_KERNEL);
  2110. if (!skb) {
  2111. bitmap_release_region(priv->used_rxkeys, slot, 0);
  2112. ret = -ENOSPC;
  2113. goto out_unlock;
  2114. }
  2115. rxkey = (struct p54_keycache *)skb_put(skb, sizeof(*rxkey));
  2116. rxkey->entry = slot;
  2117. rxkey->key_id = key->keyidx;
  2118. rxkey->key_type = algo;
  2119. if (sta)
  2120. memcpy(rxkey->mac, sta->addr, ETH_ALEN);
  2121. else
  2122. memset(rxkey->mac, ~0, ETH_ALEN);
  2123. if (key->alg != ALG_TKIP) {
  2124. rxkey->key_len = min((u8)16, key->keylen);
  2125. memcpy(rxkey->key, key->key, rxkey->key_len);
  2126. } else {
  2127. rxkey->key_len = 24;
  2128. memcpy(rxkey->key, key->key, 16);
  2129. memcpy(&(rxkey->key[16]), &(key->key
  2130. [NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY]), 8);
  2131. }
  2132. priv->tx(dev, skb);
  2133. key->hw_key_idx = slot;
  2134. out_unlock:
  2135. mutex_unlock(&priv->conf_mutex);
  2136. return ret;
  2137. }
  2138. #ifdef CONFIG_P54_LEDS
  2139. static void p54_update_leds(struct work_struct *work)
  2140. {
  2141. struct p54_common *priv = container_of(work, struct p54_common,
  2142. led_work.work);
  2143. int err, i, tmp, blink_delay = 400;
  2144. bool rerun = false;
  2145. /* Don't toggle the LED, when the device is down. */
  2146. if (priv->mode == NL80211_IFTYPE_UNSPECIFIED)
  2147. return ;
  2148. for (i = 0; i < ARRAY_SIZE(priv->leds); i++)
  2149. if (priv->leds[i].toggled) {
  2150. priv->softled_state |= BIT(i);
  2151. tmp = 70 + 200 / (priv->leds[i].toggled);
  2152. if (tmp < blink_delay)
  2153. blink_delay = tmp;
  2154. if (priv->leds[i].led_dev.brightness == LED_OFF)
  2155. rerun = true;
  2156. priv->leds[i].toggled =
  2157. !!priv->leds[i].led_dev.brightness;
  2158. } else
  2159. priv->softled_state &= ~BIT(i);
  2160. err = p54_set_leds(priv->hw);
  2161. if (err && net_ratelimit())
  2162. printk(KERN_ERR "%s: failed to update LEDs.\n",
  2163. wiphy_name(priv->hw->wiphy));
  2164. if (rerun)
  2165. queue_delayed_work(priv->hw->workqueue, &priv->led_work,
  2166. msecs_to_jiffies(blink_delay));
  2167. }
  2168. static void p54_led_brightness_set(struct led_classdev *led_dev,
  2169. enum led_brightness brightness)
  2170. {
  2171. struct p54_led_dev *led = container_of(led_dev, struct p54_led_dev,
  2172. led_dev);
  2173. struct ieee80211_hw *dev = led->hw_dev;
  2174. struct p54_common *priv = dev->priv;
  2175. if (priv->mode == NL80211_IFTYPE_UNSPECIFIED)
  2176. return ;
  2177. if (brightness) {
  2178. led->toggled++;
  2179. queue_delayed_work(priv->hw->workqueue, &priv->led_work,
  2180. HZ/10);
  2181. }
  2182. }
  2183. static int p54_register_led(struct ieee80211_hw *dev,
  2184. unsigned int led_index,
  2185. char *name, char *trigger)
  2186. {
  2187. struct p54_common *priv = dev->priv;
  2188. struct p54_led_dev *led = &priv->leds[led_index];
  2189. int err;
  2190. if (led->registered)
  2191. return -EEXIST;
  2192. snprintf(led->name, sizeof(led->name), "p54-%s::%s",
  2193. wiphy_name(dev->wiphy), name);
  2194. led->hw_dev = dev;
  2195. led->index = led_index;
  2196. led->led_dev.name = led->name;
  2197. led->led_dev.default_trigger = trigger;
  2198. led->led_dev.brightness_set = p54_led_brightness_set;
  2199. err = led_classdev_register(wiphy_dev(dev->wiphy), &led->led_dev);
  2200. if (err)
  2201. printk(KERN_ERR "%s: Failed to register %s LED.\n",
  2202. wiphy_name(dev->wiphy), name);
  2203. else
  2204. led->registered = 1;
  2205. return err;
  2206. }
  2207. static int p54_init_leds(struct ieee80211_hw *dev)
  2208. {
  2209. struct p54_common *priv = dev->priv;
  2210. int err;
  2211. /*
  2212. * TODO:
  2213. * Figure out if the EEPROM contains some hints about the number
  2214. * of available/programmable LEDs of the device.
  2215. */
  2216. INIT_DELAYED_WORK(&priv->led_work, p54_update_leds);
  2217. err = p54_register_led(dev, 0, "assoc",
  2218. ieee80211_get_assoc_led_name(dev));
  2219. if (err)
  2220. return err;
  2221. err = p54_register_led(dev, 1, "tx",
  2222. ieee80211_get_tx_led_name(dev));
  2223. if (err)
  2224. return err;
  2225. err = p54_register_led(dev, 2, "rx",
  2226. ieee80211_get_rx_led_name(dev));
  2227. if (err)
  2228. return err;
  2229. err = p54_register_led(dev, 3, "radio",
  2230. ieee80211_get_radio_led_name(dev));
  2231. if (err)
  2232. return err;
  2233. err = p54_set_leds(dev);
  2234. return err;
  2235. }
  2236. static void p54_unregister_leds(struct ieee80211_hw *dev)
  2237. {
  2238. struct p54_common *priv = dev->priv;
  2239. int i;
  2240. for (i = 0; i < ARRAY_SIZE(priv->leds); i++)
  2241. if (priv->leds[i].registered)
  2242. led_classdev_unregister(&priv->leds[i].led_dev);
  2243. }
  2244. #endif /* CONFIG_P54_LEDS */
  2245. static const struct ieee80211_ops p54_ops = {
  2246. .tx = p54_tx,
  2247. .start = p54_start,
  2248. .stop = p54_stop,
  2249. .add_interface = p54_add_interface,
  2250. .remove_interface = p54_remove_interface,
  2251. .set_tim = p54_set_tim,
  2252. .sta_notify = p54_sta_notify,
  2253. .set_key = p54_set_key,
  2254. .config = p54_config,
  2255. .bss_info_changed = p54_bss_info_changed,
  2256. .configure_filter = p54_configure_filter,
  2257. .conf_tx = p54_conf_tx,
  2258. .get_stats = p54_get_stats,
  2259. .get_tx_stats = p54_get_tx_stats
  2260. };
  2261. struct ieee80211_hw *p54_init_common(size_t priv_data_len)
  2262. {
  2263. struct ieee80211_hw *dev;
  2264. struct p54_common *priv;
  2265. dev = ieee80211_alloc_hw(priv_data_len, &p54_ops);
  2266. if (!dev)
  2267. return NULL;
  2268. priv = dev->priv;
  2269. priv->hw = dev;
  2270. priv->mode = NL80211_IFTYPE_UNSPECIFIED;
  2271. priv->basic_rate_mask = 0x15f;
  2272. skb_queue_head_init(&priv->tx_queue);
  2273. dev->flags = IEEE80211_HW_RX_INCLUDES_FCS |
  2274. IEEE80211_HW_SIGNAL_DBM |
  2275. IEEE80211_HW_NOISE_DBM;
  2276. dev->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
  2277. BIT(NL80211_IFTYPE_ADHOC) |
  2278. BIT(NL80211_IFTYPE_AP) |
  2279. BIT(NL80211_IFTYPE_MESH_POINT);
  2280. dev->channel_change_time = 1000; /* TODO: find actual value */
  2281. priv->tx_stats[P54_QUEUE_BEACON].limit = 1;
  2282. priv->tx_stats[P54_QUEUE_FWSCAN].limit = 1;
  2283. priv->tx_stats[P54_QUEUE_MGMT].limit = 3;
  2284. priv->tx_stats[P54_QUEUE_CAB].limit = 3;
  2285. priv->tx_stats[P54_QUEUE_DATA].limit = 5;
  2286. dev->queues = 1;
  2287. priv->noise = -94;
  2288. /*
  2289. * We support at most 8 tries no matter which rate they're at,
  2290. * we cannot support max_rates * max_rate_tries as we set it
  2291. * here, but setting it correctly to 4/2 or so would limit us
  2292. * artificially if the RC algorithm wants just two rates, so
  2293. * let's say 4/7, we'll redistribute it at TX time, see the
  2294. * comments there.
  2295. */
  2296. dev->max_rates = 4;
  2297. dev->max_rate_tries = 7;
  2298. dev->extra_tx_headroom = sizeof(struct p54_hdr) + 4 +
  2299. sizeof(struct p54_tx_data);
  2300. mutex_init(&priv->conf_mutex);
  2301. init_completion(&priv->eeprom_comp);
  2302. INIT_DELAYED_WORK(&priv->work, p54_work);
  2303. return dev;
  2304. }
  2305. EXPORT_SYMBOL_GPL(p54_init_common);
  2306. int p54_register_common(struct ieee80211_hw *dev, struct device *pdev)
  2307. {
  2308. int err;
  2309. err = ieee80211_register_hw(dev);
  2310. if (err) {
  2311. dev_err(pdev, "Cannot register device (%d).\n", err);
  2312. return err;
  2313. }
  2314. #ifdef CONFIG_P54_LEDS
  2315. err = p54_init_leds(dev);
  2316. if (err)
  2317. return err;
  2318. #endif /* CONFIG_P54_LEDS */
  2319. dev_info(pdev, "is registered as '%s'\n", wiphy_name(dev->wiphy));
  2320. return 0;
  2321. }
  2322. EXPORT_SYMBOL_GPL(p54_register_common);
  2323. void p54_free_common(struct ieee80211_hw *dev)
  2324. {
  2325. struct p54_common *priv = dev->priv;
  2326. kfree(priv->iq_autocal);
  2327. kfree(priv->output_limit);
  2328. kfree(priv->curve_data);
  2329. kfree(priv->used_rxkeys);
  2330. #ifdef CONFIG_P54_LEDS
  2331. p54_unregister_leds(dev);
  2332. #endif /* CONFIG_P54_LEDS */
  2333. }
  2334. EXPORT_SYMBOL_GPL(p54_free_common);